group_id
stringlengths
12
18
problem_description
stringlengths
85
3.02k
candidates
listlengths
3
20
aoj_2200_cpp
Problem D: Mr. Rito Post Office あなたは離島の郵便局に勤めるプログラマである.あなたの住んでいる地域は,複数の島々からなる.各島には一つ以上の港町がある.それらに加えて他の町や村があるかもしない.ある島から別の島に向かうためには船を使わなければならない.一つの島の中を回るには陸路が使えるが,海路を利用した方が速いこともある. 近年行われた郵便局の民営化をきっかけに,経費削減に向けて郵便配達員の人員整理が全国的に行われた.離島の郵便局もその例外ではなく,結果として郵便配達員は利藤さんただ一人となってしまった.その郵便局が集配を担当する地域は非常に広いため,一人で集配するのは大変な作業である.なので,どのようにすれば効率的に集配を行えるのか,利藤さんがあなたに助けを求めてきた. あなたの仕事は,利藤さんがたどるべき町や村の集配順序が与えられたときに,最短巡回経路を求めるプログラムを書くことである. 利藤さんは,決して指定された順序以外で集配業務を行うことができない.しかし,ある町や村から別の町や村へ移動する際に,他の町や村を経由して移動することは許可されている.また,利藤さんは島々を巡るための一隻の船を持っている. 例えば,町A,町B,村Cという集配順序が与えられた場合,町Aから町Bへ向かう際にどの町や村を経由してもかまわない.このとき,村Cを経由しても構わないが,集配順序を守るためには,一度町Bに行って集配をおこなってから,改めて村Cを訪れて集配をおこなわなければならない.また,町Aから海路を用いて町Bに向かい,町Bから陸路を用いて村Cに向かった場合,船を町Bに置いたままになる.したがって,次に海路を使いたい場合は町Bに戻る必要がある. 一つの町や村において複数回集配をしなければならない場合もある.たとえば,町A,村B,町C,村Bという集配順序が与えられるかもしれない.このとき,町Aから村Bをたどらずに町Cに向かった場合は,町Cでいきなり集配をおこなうことはできない.最初の村Bでの集配が終わっていないからである.町Cで集配を済ませた後で村Bを訪れて集配しても,一回目の村Bの集配を終わらせたことにはならない. 利藤さんは,はじめに必ずある港町に船とともにいる.利藤さんはベテランであるため,移動時間以外の集配作業にかかる時間は無視してよい.また,最後の町または村での集配業務が完了するまでの時間だけが問題であり,船をもとの位置に戻して郵便局に帰るまでの時間は考慮しなくてよい. Input 入力は複数のデータセットから構成される.各データセットの形式は次に示すとおりである. N M x 1 y 1 t 1 sl 1 x 2 y 2 t 2 sl 2 ... x M y M t M sl M R z 1 z 2 ... z R データセットの中の入力項目は,すべて非負の整数である.行中の入力項目の区切りは空白 1 個である. 最初の行は,陸路及び海路網の大きさを規定する. N (2 ≤ N ≤ 200) は,町または村の数である. それぞれの町または村には,1 から N までの固有の番号を割り当てる. M (1 ≤ M ≤ 10000) は,陸路と海路の合計本数である. 2 行目から 1 + M 行目は,陸路または海路の記述である. x i と y i (1 ≤ x i , y i ≤ N ) は両端の町または村の番号を表す. t i (1 ≤ t i ≤ 1000) はその陸路または海路の移動時間を表す. sl i は ‘L’ または ‘S’ のいずれかであり,Lは陸路を,Sは海路をそれぞれ表す. ある2つの町や村を直接結ぶ陸路または海路が2本以上存在することがある. それぞれの陸路または海路は双方向であり,すなわちどちらの向きにも移動できる. M + 2 行目の R (1 ≤ R ≤ 1000)は,利藤さんが担当する集配先の数を表す. M + 3 行目には,集配先の町や村の番号 z i (1 ≤ z i ≤ N ) が集配順に R 個並んでいる. 初期状態では利藤さんと船はともに港町 z 1 に存在する. 初期状態から集配先の町や村へは,必ず何らかの方法で移動することができる. 入力の終わりは,空白で区切られた2つの0を含む1行で示される. Output 入力の各データセットに対して,与えられた集配順序で利藤さんが町と村を巡回するために必要な最短移動時間を求め,1行に出力せよ. Sample Input 3 3 1 2 5 L 1 2 7 S 2 3 11 S 3 1 2 3 5 5 1 2 15 L 2 3 10 L 4 5 7 L 1 3 30 S 3 4 100 S 5 1 3 5 4 1 0 0 Output for the Sample Input 18 269
[ { "submission_id": "aoj_2200_11057001", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nconst int INF=0x3f3f3f3f;\nint main(){\n int n,m;\n while(scanf(\"%d%d\",&n,&m)!=EOF&&(n||m)){\n int d[2010][202];\n int l[202][202];\n int s[202][202];\n memset(d,0x3f,sizeof(d));\n memset(l,0x3f,sizeof(l));\n memset(s,0x3f,sizeof(s));\n for(int i=0;i<m;i++){\n int x,y,t;\n char ch[3];\n scanf(\"%d%d%d%s\",&x,&y,&t,ch);\n if(ch[0]=='L'){\n l[x][y]=min(l[x][y],t);\n l[y][x]=min(l[y][x],t);\n }else{\n s[x][y]=min(s[x][y],t);\n s[y][x]=min(s[y][x],t);\n }\n }\n int init;\n int r;\n scanf(\"%d\",&r);\n vector<int> g;\n g.push_back(0);\n scanf(\"%d\",&init);\n g.push_back(init);\n for(int i=1;i<r;i++){\n int x;\n scanf(\"%d\",&x);\n g.push_back(x);\n }\n for(int i=1;i<=n;i++){\n l[i][i]=s[i][i]=0;\n }\n for(int k=1;k<=n;k++){\n for(int i=1;i<=n;i++){\n for(int j=1;j<=n;j++){\n s[i][j]=min(s[i][j],s[i][k]+s[k][j]);\n l[i][j]=min(l[i][j],l[i][k]+l[k][j]);\n }\n }\n }\n for(int i=1;i<=n;i++){\n d[1][i]=min(d[1][i],s[g[1]][i]+l[g[1]][i]);\n }\n for(int i=1;i<=r;i++){\n for(int j=1;j<=n;j++){\n if(l[g[i]][j]>=INF) continue;\n for(int k=1;k<=n;k++){\n if(l[g[i-1]][k]>=INF) continue;\n if(j==k){\n if(l[g[i-1]][g[i]]!=INF){\n d[i][j]=min(d[i][j],d[i-1][j]+l[g[i-1]][g[i]]);\n }\n }else{\n if(l[g[i-1]][k]!=INF&&s[k][j]!=INF&&l[j][g[i]]!=INF){\n d[i][j]=min(d[i][j],d[i-1][k]+l[g[i-1]][k]+s[k][j]+l[j][g[i]]);\n }\n }\n }\n }\n }\n int ans=INF;\n for(int i=1;i<=n;i++){\n ans=min(ans,d[r][i]);\n }\n printf(\"%d\\n\",ans);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 390, "memory_kb": 5380, "score_of_the_acc": -0.2304, "final_rank": 16 }, { "submission_id": "aoj_2200_10853895", "code_snippet": "//\n// AOJ 2200 Mr. Rito Post Office\n//\n// Created by TaoSama on 2015-03-20\n// Copyright (c) 2015 TaoSama. All rights reserved.\n//\n#include <algorithm>\n#include <cctype>\n#include <cmath>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <iomanip>\n#include <iostream>\n#include <map>\n#include <queue>\n#include <string>\n#include <set>\n#include <vector>\n#define CLR(x,y) memset(x, y, sizeof(x))\n\nusing namespace std;\nconst int INF = 0x3f3f3f3f;\nconst int MOD = 1e9 + 7;\nconst int N = 1e5 + 10;\n\nint sea[205][205], land[205][205], dp[1005][205];\nint n, m, r, a[1005];\n\nint main() {\n#ifdef LOCAL\n freopen(\"in.txt\", \"r\", stdin);\n//\tfreopen(\"out.txt\",\"w\",stdout);\n#endif\n ios_base::sync_with_stdio(0);\n\n while(cin >> n >> m && (n + m)) {\n for(int i = 1; i <= n; ++i)\n for(int j = 1; j <= n; ++j)\n sea[i][j] = land[i][j] = i == j ? 0 : INF;\n\n for(int i = 1; i <= m; ++i) {\n int x, y, t; char c;\n cin >> x >> y >> t >> c;\n if(c == 'S') sea[x][y] = sea[y][x] = min(sea[x][y], t);\n else land[x][y] = land[y][x] = min(land[x][y], t);\n }\n\n cin >> r;\n for(int i = 1; i <= r; ++i) cin >> a[i];\n\n\n for(int k = 1; k <= n; ++k) {\n for(int i = 1; i <= n; ++i) {\n for(int j = 1; j <= n; ++j) {\n sea[i][j] = min(sea[i][j], sea[i][k] + sea[k][j]);\n land[i][j] = min(land[i][j], land[i][k] + land[k][j]);\n }\n }\n }\n\n\n memset(dp, 0x3f, sizeof dp);\n dp[1][a[1]] = 0;\n for(int i = 1; i <= r; ++i) {\n for(int j = 1; j <= n; ++j) {\n dp[i][j] = min(dp[i][j], dp[i - 1][j] + land[a[i - 1]][a[i]]);\n for(int k = 1; k <= n; ++k)\n dp[i][k] = min((long long)dp[i][k], (long long)dp[i - 1][j] +\n land[a[i - 1]][j] + sea[j][k] + land[k][a[i]]);\n }\n }\n cout << *min_element(dp[r]+1, dp[r] + n + 1) << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 170, "memory_kb": 4596, "score_of_the_acc": -0.0942, "final_rank": 10 }, { "submission_id": "aoj_2200_10772255", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nconst int INF = 1e9;\n\nvoid solve(int N, int M) {\n vector<vector<int>> L(N, vector<int>(N, INF)), S(N, vector<int>(N, INF));\n for (int i = 0; i < N; i++) L[i][i] = S[i][i] = 0;\n for (; M--;) {\n int x, y, t;\n char sl;\n cin >> x >> y >> t >> sl;\n x--, y--;\n if (sl == 'L') {\n L[x][y] = min(L[x][y], t);\n L[y][x] = min(L[y][x], t);\n } else {\n S[x][y] = min(S[x][y], t);\n S[y][x] = min(S[y][x], t);\n }\n }\n int R;\n cin >> R;\n vector<int> z(R);\n for (int& x : z) cin >> x, x--;\n\n for (int k = 0; k < N; k++) {\n for (int i = 0; i < N; i++) {\n for (int j = 0; j < N; j++) {\n L[i][j] = min(L[i][j], L[i][k] + L[k][j]);\n S[i][j] = min(S[i][j], S[i][k] + S[k][j]);\n }\n }\n }\n\n vector<int> dp(N, INF), ndp(N, INF);\n dp[z[0]] = 0;\n auto calc = [&](int pre, int nxt) {\n vector<int> join(N, INF);\n for (int i = 0; i < N; i++) {\n join[i] = min(join[i], dp[i] + L[pre][i]);\n ndp[i] = min(ndp[i], dp[i] + L[pre][nxt]);\n }\n for (int i = 0; i < N; i++) {\n for (int j = 0; j < N; j++) {\n join[j] = min(join[j], join[i] + S[i][j]);\n }\n }\n for (int i = 0; i < N; i++) {\n ndp[i] = min(ndp[i], join[i] + L[i][nxt]);\n dp[i] = INF;\n }\n swap(dp, ndp);\n };\n\n for (int i = 0; i + 1 < R; i++) calc(z[i], z[i + 1]);\n int ans = INF;\n for (int i = 0; i < N; i++) ans = min(ans, dp[i]);\n cout << ans << '\\n';\n}\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n int N, M;\n while (cin >> N >> M, N) solve(N, M);\n return 0;\n}", "accuracy": 1, "time_ms": 130, "memory_kb": 3520, "score_of_the_acc": -0.0009, "final_rank": 1 }, { "submission_id": "aoj_2200_10737409", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\nconstexpr int N = 200;\nlong long gl[200][200];\nlong long gs[200][200];\nint n, m, r;\n\nvoid floyd(long long (&g)[200][200]) {\n for (int k = 0; k < n; ++k) {\n for (int i = 0; i < n; ++i) {\n for (int j = 0; j < n; ++j) {\n if (g[i][k] == LLONG_MAX || g[k][j] == LLONG_MAX) continue;\n g[i][j] = min(g[i][j], g[i][k] + g[k][j]);\n }\n }\n }\n}\n\nvoid solve() {\n for (int i = 0; i < n; ++i) {\n for (int j = 0; j < n; ++j) {\n if (i == j) {\n gl[i][j] = 0;\n gs[i][j] = 0;\n continue;\n }\n gl[i][j] = LLONG_MAX;\n gs[i][j] = LLONG_MAX;\n }\n }\n for (int i = 0; i < m; ++i) {\n int a, b;\n long long c;\n char sl;\n cin >> a >> b >> c >> sl;\n --a, --b;\n if (sl == 'L') {\n gl[a][b] = min(gl[a][b], c);\n gl[b][a] = min(gl[b][a], c);\n } else {\n gs[a][b] = min(gs[a][b], c);\n gs[b][a] = min(gs[b][a], c);\n }\n }\n\n // for (int i = 0; i < n; ++i) {\n // for (int j = 0; j < n; ++j) {\n // cout << gl[i][j] << \" \";\n // }\n // cout << '\\n';\n // }\n\n // cout << \"---\\n\";\n\n floyd(gl);\n floyd(gs);\n\n // for (int i = 0; i < n; ++i) {\n // for (int j = 0; j < n; ++j) {\n // cout << gl[i][j] << \" \";\n // }\n // cout << '\\n';\n // }\n\n // cout << \"---\\n\";\n\n cin >> r;\n vector<long long> dp(n, LLONG_MAX);\n vector<long long> new_dp(n, LLONG_MAX);\n int last_lpos;\n cin >> last_lpos;\n --last_lpos;\n dp[last_lpos] = 0;\n\n for (int i = 1; i < r; ++i) {\n int lpos;\n cin >> lpos;\n --lpos;\n for (int spos = 0; spos < n; ++spos) {\n new_dp[spos] = LLONG_MAX;\n for (int last_spos = 0; last_spos < n; ++last_spos) {\n long long d;\n\n if (last_spos == spos) {\n if (dp[last_spos] == LLONG_MAX) continue;\n if (gl[last_lpos][lpos] == LLONG_MAX) continue;\n d = dp[last_spos] + gl[last_lpos][lpos];\n } else {\n if (dp[last_spos] == LLONG_MAX) continue;\n if (gl[last_lpos][last_spos] == LLONG_MAX) continue;\n if (gs[last_spos][spos] == LLONG_MAX) continue;\n if (gl[spos][lpos] == LLONG_MAX) continue;\n d = dp[last_spos] + gl[last_lpos][last_spos] +\n gs[last_spos][spos] + gl[spos][lpos];\n }\n new_dp[spos] = min(new_dp[spos], d);\n }\n }\n last_lpos = lpos;\n swap(dp, new_dp);\n }\n\n long long ans = LLONG_MAX;\n for (int i = 0; i < n; ++i) {\n ans = min(ans, dp[i]);\n }\n cout << ans << endl;\n}\n\nint main() {\n#ifdef LOCAL\n freopen(\"in.txt\", \"r\", stdin);\n#endif\n\n while (true) {\n cin >> n >> m;\n if (n == 0 && m == 0) break;\n solve();\n }\n}", "accuracy": 1, "time_ms": 350, "memory_kb": 3980, "score_of_the_acc": -0.1133, "final_rank": 11 }, { "submission_id": "aoj_2200_10661825", "code_snippet": "// (⁠◕⁠ᴗ⁠◕⁠✿⁠)\n\n// #pragma GCC target(\"avx2\")\n#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"unroll-loops\")\n#include <bits/stdc++.h>\n#define rep(i, n) for (ll i = 0; i < (n); i++)\n#define srep(i, s, n) for (ll i = s; i < (n); i++)\n#define len(x) ((int)(x).size())\n#define all(x) (x).begin(), (x).end()\nusing namespace std;\ntemplate<typename T> using vc = vector<T>;\ntemplate<typename T> using vv = vc<vc<T>>;\ntemplate<typename T> using vvv = vv<vc<T>>;\nusing vi = vc<int>;using vvi = vv<int>; using vvvi = vv<vi>;\nusing ll = long long;using vl = vc<ll>;using vvl = vv<ll>; using vvvl = vv<vl>;\nusing ld = long double; using vld = vc<ld>; using vvld = vc<vld>; using vvvld = vc<vvld>;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nconst ld pi = acos(-1.0);\nconst int inf = 0x3f3f3f3f;\nconst ll INF = 0x3f3f3f3f3f3f3f3f;\n// const ll mod = 1000000007;\nconst ll mod = 998244353;\ninline bool inside(ll y, ll x, ll H, ll W) {return 0 <= (y) and (y) < (H) and 0 <= (x) and (x) < (W); }\n\n#define debug(var) do { cout << #var << \" :\\n\"; view(var); } while(0)\ntemplate<typename T>void view(const T& e) {cout << e;}\ntemplate<typename T1, typename T2>void view(const pair<T1, T2>& p) {cout << \"{\" << p.first << \", \" << p.second << \"}\";}\ntemplate<typename T>void view(const vc<T>& v) {for (const auto& e : v) {view(e);cout << \" \";} cout << endl;}\ntemplate<typename T>void view(const vv<T>& vv) {for (const auto& v : vv) {view(v);cout << \" \";} cout << endl;}\ntemplate<typename T>void view(const set<T>& s) {for (const auto& e : s) {view(e);cout << \" \";} cout << endl;}\ntemplate<typename T>void view(const unordered_set<T>& s) {for (const auto& e : s) {view(e);cout << \" \";} cout << endl;}\ntemplate<typename T1, typename T2>void view(const map<T1, T2>& mp){for (const auto& e : mp) {view(e);cout << \" \";} cout << endl;}\n\nint N, M;\nvoid solve(){\n vvl riku(N, vl(N, INF)), umi(N, vl(N, INF));\n rep(i, N){\n riku[i][i] = 0;\n umi[i][i] = 0;\n }\n rep(i, M){\n int x, y; ll t; char sl; cin >> x >> y >> t >> sl; x--; y--;\n if (sl == 'L'){\n riku[x][y] = min(riku[x][y], t);\n riku[y][x] = min(riku[y][x], t);\n }else{\n umi[x][y] = min(umi[x][y], t);\n umi[y][x] = min(umi[y][x], t);\n }\n }\n for (int k = 0; k < N; k++) for (int i = 0; i < N; i++) for (int j = 0; j < N; j++){\n if (riku[i][k] != INF && riku[k][j] != INF){\n if (riku[i][j] > riku[i][k] + riku[k][j]){\n riku[i][j] = riku[i][k] + riku[k][j];\n }\n }\n if (umi[i][k] != INF && umi[k][j] != INF){\n if (umi[i][j] > umi[i][k] + umi[k][j]){\n umi[i][j] = umi[i][k] + umi[k][j];\n }\n }\n }\n int R; cin >> R;\n vi Z(R); rep(i, R) cin >> Z[i];\n rep(i, R) Z[i]--;\n vvl dp(R, vl(N, INF));\n dp[0][Z[0]] = 0;\n srep(i, 1, R) rep(j, N){\n rep(nj, N) if (riku[Z[i - 1]][nj] < INF && umi[nj][j] < INF && riku[j][Z[i]] < INF) dp[i][j] = min(dp[i][j], dp[i - 1][nj] + riku[Z[i - 1]][nj] + umi[nj][j] + riku[j][Z[i]]);\n dp[i][j] = min(dp[i][j], dp[i - 1][j] + riku[Z[i - 1]][Z[i]]);\n }\n cout << accumulate(all(dp[R - 1]), INF, [&](auto i, auto j){return min(i, j);}) << endl;\n}\n\nint main(){\n while (true){\n cin >> N >> M;\n if (N == 0) break;\n solve();\n }\n}", "accuracy": 1, "time_ms": 220, "memory_kb": 5632, "score_of_the_acc": -0.1882, "final_rank": 14 }, { "submission_id": "aoj_2200_10608856", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nbool solve() {\n int N, M;\n cin >> N >> M;\n if (N == 0 && M == 0) return false;\n vector<vector<long long>> L(N, vector<long long>(N, 1LL << 58));\n vector<vector<long long>> S(N, vector<long long>(N, 1LL << 58));\n while (M--) {\n int u, v, w;\n char c;\n cin >> u >> v >> w >> c;\n u--, v--;\n auto &D = (c == 'L' ? L : S);\n D[u][v] = min<long long>(D[u][v], w);\n D[v][u] = min<long long>(D[v][u], w);\n }\n for (int i = 0; i < N; i++) L[i][i] = S[i][i] = 0;\n for (int k = 0; k < N; k++) {\n for (int i = 0; i < N; i++) {\n for (int j = 0; j < N; j++) {\n L[i][j] = min(L[i][j], L[i][k] + L[k][j]);\n S[i][j] = min(S[i][j], S[i][k] + S[k][j]);\n }\n }\n }\n vector<long long> dp(N, 1LL << 58);\n int R, u;\n cin >> R >> u;\n u--;\n dp[u] = 0;\n while (--R) {\n int v;\n cin >> v;\n v--;\n vector<long long> ep(N, 1LL << 58);\n for (int i = 0; i < N; i++) {\n for (int j = 0; j < N; j++) {\n ep[j] = min<long long>(ep[j], dp[i] + L[u][i] + S[i][j] + L[j][v]);\n ep[j] = min<long long>(ep[j], dp[j] + L[u][v]);\n }\n }\n dp = ep;\n u = v;\n }\n cout << *min_element(dp.begin(), dp.end()) << '\\n';\n return true;\n}\n\nint main() {\n while (solve()) {}\n}", "accuracy": 1, "time_ms": 230, "memory_kb": 4096, "score_of_the_acc": -0.0789, "final_rank": 8 }, { "submission_id": "aoj_2200_10516839", "code_snippet": "#pragma GCC target(\"avx2\")\n#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"unroll-loops\")\n\n#ifndef ONLINE_JUDGE\n#define _GLIBCXX_DEBUG // 配列外参照のエラー\n#endif\n\n// スタンダードライブラリ\n#include <bits/stdc++.h>\nusing namespace std;\n\n// 多倍長整数\n// #if __has_include(<boost/multiprecision/cpp_int.hpp>)\n// #include <boost/multiprecision/cpp_int.hpp>\n// using lll =boost::multiprecision::int128_t;\n// #endif\n\n// atcoder関連\n#if __has_include(<atcoder/all>)\n#include <atcoder/all>\nusing namespace atcoder;\nusing mint = modint998244353;\nusing MINT = modint1000000007;\n#endif\n\n// 型関連\nusing ll = long long;\nusing lint = long long;\nusing pll = pair<ll, ll>;\nusing plll = tuple<ll, ll, ll>;\nusing pii = pair<int, int>;\nusing piii = tuple<ll, ll, ll>;\ntemplate <class T> using prique = priority_queue<T>;\n\n// 型定数\nconstexpr ll mod = 998244353;\nconstexpr ll MOD = 1000000007;\nint inf = 2e9;\nll INF = 1e18;\n#define endl '\\n';\n\n// 新規マクロ\n#define all(a) (a).begin(), (a).end()\n#define rep(i, N, limit) for (int i = (int)N; i < (int)limit; i++)\n\n///////////////////// vector関連\n// template <class T, size_t n, size_t idx = 0>\n// auto make_vec(const size_t (&d)[n], const T &init) noexcept {\n// if constexpr (idx < n)\n// return std::vector(d[idx], make_vec<T, n, idx + 1>(d, init));\n// else\n// return init;\n// }\n\ntemplate <class T, size_t n> auto make_vec(const size_t (&d)[n]) noexcept {\n return make_vec(d, T{});\n}\n\nint dx[4] = {1, 0, -1, 0};\nint dy[4] = {0, 1, 0, -1};\n\n/////////////////////////// print関連\ntemplate <typename T> void print(vector<T> A);\nvoid print();\ntemplate <class Head, class... Tail> void print(Head &&head, Tail &&...tail);\nvoid print(string S);\ntemplate <typename T> void print(vector<vector<T>> &F);\n\n////////////////////////////\ninline string input();\ninline ll I();\ninline pll II();\n\n/////////////////////出力関連\ninline void Yes(bool f);\n\nbool solve() {\n int N, M;\n cin >> N >> M;\n if (N == 0) {\n return false;\n }\n vector<vector<ll>> EL = vector<vector<ll>>(N, vector<ll>(N, INF));\n vector<vector<ll>> ES=vector<vector<ll>>(N,vector<ll>(N,INF));\n \n for (int i = 0; i < N; i++) {\n EL[i][i] = 0;\n ES[i][i] = 0;\n }\n for (int i = 0; i < M; i++) {\n int x, y;\n ll t;\n char sl;\n cin >> x >> y >> t >> sl;\n x--;\n y--;\n if (sl == 'L') {\n EL[x][y] = min(EL[x][y], t);\n EL[y][x] = min(EL[y][x], t);\n } else {\n ES[x][y] = min(ES[x][y], t);\n ES[y][x] = min(ES[y][x], t);\n }\n }\n\n for (int k = 0; k < N; k++) {\n for (int i = 0; i < N; i++) {\n for (int j = 0; j < N; j++) {\n EL[i][j]=min(EL[i][j],EL[i][k]+EL[k][j]);\n ES[i][j]=min(ES[i][j],ES[i][k]+ES[k][j]);\n }\n }\n }\n\n int R;\n cin >> R;\n vector<int> Z(R);\n for (int i = 0; i < R; i++) {\n cin >> Z[i];\n Z[i]--;\n }\n\n vector<vector<ll>> dp = vector<vector<ll>>(R, vector<ll>(N, INF));\n \n dp[0][Z[0]]=0;\n for (int i = 0; i < R - 1; i++) {\n for (int j = 0; j < N; j++) {\n if (dp[i][j] == INF) {\n continue;\n }\n dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + EL[Z[i]][Z[i + 1]]);\n\n for (int k = 0; k < N; k++) {\n dp[i+1][k]=min(dp[i+1][k],dp[i][j]+EL[Z[i]][j]+ES[j][k]+EL[k][Z[i+1]]);\n }\n }\n }\n ll ans = INF;\n for (int i = 0; i < N; i++) {\n ans=min(ans,dp[R-1][i]);\n }\n print(ans);\n\n return true;\n}\n\n///////////////////ここから//////////////////////\nint main() {\n ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n while (solve());\n}\n\n////////////////////print/////////////////////////\n// 1次元ベクトルを出力する\ntemplate <typename T> void print(vector<T> A) {\n if (A.size() == 0) {\n return;\n }\n for (size_t i = 0; i < A.size() - 1; i++) {\n cout << A[i] << ' ';\n }\n cout << A[A.size() - 1] << endl;\n return;\n}\n\n// 2次元ベクトルを出力する\ntemplate <typename T> void print(vector<vector<T>> &F) {\n for (size_t i = 0; i < F.size(); i++) {\n for (size_t j = 0; j < F[i].size(); j++) {\n cout << F[i][j];\n if (j < F[i].size() - 1) {\n cout << ' ';\n }\n }\n cout << endl;\n }\n}\n\n// 空白行を出力するprint\nvoid print() { cout << endl; }\n\n// 数値・文字列を空白区切りで出力する. print(a,b)など\ntemplate <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {\n cout << head;\n if (sizeof...(tail) != 0)\n cout << \" \";\n print(forward<Tail>(tail)...);\n}\n\nvoid print(string S) { cout << S << endl; }\n\n/////////////////////初期化///////////////////////\n\ninline string input() {\n string S;\n cin >> S;\n return S;\n}\ninline ll I() {\n ll ret;\n cin >> ret;\n return ret;\n}\ninline pll II() {\n pll ret;\n cin >> ret.first >> ret.second;\n return ret;\n}\n\n//////////////出力関連\n\ninline void Yes(bool f) {\n if (f) {\n cout << \"Yes\" << endl;\n } else {\n cout << \"No\" << endl;\n }\n}", "accuracy": 1, "time_ms": 2930, "memory_kb": 5888, "score_of_the_acc": -1.1749, "final_rank": 20 }, { "submission_id": "aoj_2200_10512407", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define all(...) std::begin(__VA_ARGS__), std::end(__VA_ARGS__)\n#define rall(...) std::rbegin(__VA_ARGS__), std::rend(__VA_ARGS__)\n#define OVERLOAD_REP(_1, _2, _3, _4, name, ...) name\n#define REP1(n) for(ll i=0;i<(n);i++)\n#define REP2(i, n) for (ll i=0;i<(n);i++)\n#define REP3(i, a, n) for (ll i=a;i<(n);i++)\n#define REP4(i, a, b, n) for(ll i=a;i<(n);i+=b)\n#define rep(...) OVERLOAD_REP(__VA_ARGS__, REP4, REP3, REP2, REP1)(__VA_ARGS__)\n#define OVERLOAD_RREP(_1, _2, _3, _4, name, ...) name\n#define RREP1(n) for(ll i=(n)-1;i>=0;i--)\n#define RREP2(i, n) for(ll i=(n)-1;i>=0;i--)\n#define RREP3(i, a, n) for(ll i=(n)-1;i>=(a);i--)\n#define RREP4(i, a, b, n) for(ll i=(n)-1;i>=(a);i-=(b))\n#define rrep(...) OVERLOAD_RREP(__VA_ARGS__, RREP4, RREP3, RREP2, RREP1)(__VA_ARGS__)\n#define uniq(a) sort(all(a));a.erase(unique(all(a)),end(a))\n#define len(n) (long long)(n).size()\nusing ll = long long;\nusing ld = long double;\nusing ull = unsigned long long;\nusing vi = vector<int>;\nusing vvi = vector<vi>;\nusing vvvi = vector<vvi>;\nusing vll = vector<ll>;\nusing vvll = vector<vll>;\nusing vvvll = vector<vvll>;\nusing vs = vector<string>;\nusing vvs = vector<vs>;\nusing vvvs = vector<vvs>;\nusing vld = vector<ld>;\nusing vvld = vector<vld>;\nusing vvvld = vector<vvld>;\nusing vc = vector<char>;\nusing vvc = vector<vc>;\nusing vvvc = vector<vvc>;\nusing pll = pair<ll,ll>;\nusing vpll = vector<pll>;\nusing vvpll = vector<vpll>;\n\nll intpow(ll a,ll b){\n ll ans = 1;\n while (b){\n if (b & 1){\n ans *= a;\n }\n a *= a;\n b /= 2;\n }\n return ans;\n}\nll modpow(ll a,ll b,ll c){\n ll ans = 1;\n while (b){\n if (b & 1){\n ans *= a;\n ans %= c;\n }\n a *= a;\n a %= c;\n b >>= 1;\n }\n return ans;\n}\n\ntemplate<class... T>\nvoid input(T&... a){\n (cin >> ... >> a);\n}\n\n#define INT(...) int __VA_ARGS__; input(__VA_ARGS__)\n#define LL(...) ll __VA_ARGS__; input(__VA_ARGS__)\n#define ULL(...) ull __VA_ARGS__; input(__VA_ARGS__)\n#define LD(...) ld __VA_ARGS__; input(__VA_ARGS__)\n#define STR(...) string __VA_ARGS__; input(__VA_ARGS__)\n#define CHA(...) char __VA_ARGS__; input(__VA_ARGS__)\n#define VLL(name,length) vll name(length);rep(i,length){cin >> name[i];}\n#define VVLL(name,h,w) vvll name(h,vll(w));rep(i,h)rep(j,w){cin >> name[i][j];}\n#define VVVLL(name,a,b,c) vvvll name(a,vvll(b,vll(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}\n#define VI(name,length) vi name(length);rep(i,length){cin >> name[i];}\n#define VVI(name,h,w) vvi name(h,vi(w));rep(i,h)rep(j,w){cin >> name[i][j];}\n#define VVVI(name,a,b,c) vvvi name(a,vvll(b,vi(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}\n#define VLD(name,length) vld name(length);rep(i,length){cin >> name[i];}\n#define VVLD(name,h,w) vvld name(h,vld(w));rep(i,h)rep(j,w){cin >> name[i][j];}\n#define VVVLD(name,a,b,c) vvvld name(a,vvld(b,vld(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}\n#define VC(name,length) vc name(length);rep(i,length){cin >> name[i];}\n#define VVC(name,h,w) vvc name(h,vc(w));rep(i,h)rep(j,w){cin >> name[i][j];}\n#define VVVC(name,a,b,c) vvvc name(a,vvc(b,vc(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}\n#define VS(name,length) vs name(length);rep(i,length){cin >> name[i];}\n#define VVS(name,h,w) vvs name(h,vs(w));rep(i,h)rep(j,w){cin >> name[i][j];}\n#define VVVS(name,a,b,c) vvvs name(a,vvs(b,vs(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}\n#define PLL(name) pll name;cin>>name.first>>name.second;\n#define VPLL(name,length) vpll name(length);rep(i,length){cin>>name[i].first>>name[i].second;}\n\nvoid print(){cout << \"\\n\";}\n\ntemplate <typename T1, typename T2>\nstd::ostream& operator<<(std::ostream& os, const std::pair<T1, T2>& p) {\n os << \"(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\n\ntemplate <typename T>\nstd::ostream& operator<<(std::ostream& os, const std::vector<T>& vec) {\n os << \"[\";\n for (size_t i = 0; i < vec.size(); ++i) {\n os << vec[i];\n if (i + 1 < vec.size()) os << \", \";\n }\n os << \"]\";\n return os;\n}\n\ntemplate <typename T1, typename T2>\nstd::ostream& operator<<(std::ostream& os, const std::vector<std::pair<T1, T2>>& a) {\n os << \"[\";\n for (size_t j = 0; j < a.size(); ++j) {\n os << \"(\" << a[j].first << \", \" << a[j].second << \")\";\n\t\tif (j + 1 < a.size()) os << \", \";\n }\n\tos << \"]\";\n return os;\n}\n\ntemplate <typename T1, typename T2>\nstd::ostream& operator<<(std::ostream& os, const std::vector<std::vector<std::pair<T1, T2>>>& mat) {\n\tos << \"[\";\n for (size_t i = 0; i < mat.size(); ++i) {\n\t\tos << \"[\";\n for (size_t j = 0; j < mat[i].size(); ++j) {\n os << \"(\" << mat[i][j].first << \", \" << mat[i][j].second << \")\";\n\t\t\tif (j + 1 < mat[i].size()) os << \", \";\n }\n\t\tos << \"]\";\n\t\tif (i + 1 < mat.size()) os << \", \";\n }\n\tos << \"]\";\n return os;\n}\n\ntemplate <typename T>\nstd::ostream& operator<<(std::ostream& os, const std::set<T>& s) {\n os << \"{\";\n bool first = true;\n for (const auto& x : s) {\n if (!first) os << \", \";\n os << x;\n first = false;\n }\n os << \"}\";\n return os;\n}\n\ntemplate <typename K, typename V>\nstd::ostream& operator<<(std::ostream& os, const std::map<K, V>& m) {\n os << \"{\";\n bool first = true;\n for (const auto& [key, val] : m) {\n if (!first) os << \", \";\n os << key << \": \" << val;\n first = false;\n }\n os << \"}\";\n return os;\n}\n\ntemplate<class T, class... Ts>\nvoid print(const T& a, const Ts&... b){cout << a;(cout << ... << (cout << ' ', b));cout << '\\n';}\n\nvoid write(){cout << \"\\n\";}\ntemplate<class T, class... Ts>\nvoid write(const T& a, const Ts&... b){cout << a;(cout << ... << (cout << ' ', b));cout << '\\n';}\nvoid write(vll x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << \" \";}else{cout << '\\n';}}}\nvoid write(vvll x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << \" \";}else{cout << '\\n';}}}\nvoid write(vi x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << \" \";}else{cout << '\\n';}}}\nvoid write(vvi x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << \" \";}else{cout << '\\n';}}}\nvoid write(vvvi x){rep(i,len(x))rep(j,len(x[i]))rep(k,len(x[i][j])){cout << x[i][j][k];if(k!=len(x[i][j])-1){cout << \" \";}else if(j!=len(x[i])-1){cout << \" | \";}else{cout << '\\n';}}}\nvoid write(vld x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << \" \";}else{cout << '\\n';}}}\nvoid write(vvld x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << \" \";}else{cout << '\\n';}}}\nvoid write(vvvld x){rep(i,len(x))rep(j,len(x[i]))rep(k,len(x[i][j])){cout << x[i][j][k];if(k!=len(x[i][j])-1){cout << \" \";}else if(j!=len(x[i])-1){cout << \" | \";}else{cout << '\\n';}}}\nvoid write(vc x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << \" \";}else{cout << '\\n';}}}\nvoid write(vvc x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << \"\";}else{cout << '\\n';}}}\nvoid write(vvvc x){rep(i,len(x))rep(j,len(x[i]))rep(k,len(x[i][j])){cout << x[i][j][k];if(k!=len(x[i][j])-1){cout << \" \";}else if(j!=len(x[i])-1){cout << \" | \";}else{cout << '\\n';}}}\nvoid write(vs x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << \"\\n\";}else{cout << '\\n';}}}\nvoid write(vvs x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << \" \";}else{cout << '\\n';}}}\nvoid write(vvvs x){rep(i,len(x))rep(j,len(x[i]))rep(k,len(x[i][j])){cout << x[i][j][k];if(k!=len(x[i][j])-1){cout << \" \";}else if(j!=len(x[i])-1){cout << \" | \";}else{cout << '\\n';}}}\nvoid write(pll x){cout << x.first << ' ' << x.second << '\\n';}\nvoid write(vpll x){rep(i,len(x)){cout << x[i].first << ' ' << x[i].second << '\\n';}}\nvoid write(vvpll x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j].first << ' ' << x[i][j].second;if(j!=len(x[i])-1){cout << \" \";}else{cout << '\\n';}}}\n\ntemplate <typename T>\nT sum(const std::vector<T>& v) {\n return std::accumulate(v.begin(), v.end(), T(0));\n}\n\ntemplate<class T> bool chmin(T& a, const T& b){ if(a > b){ a = b; return 1; } return 0; }\ntemplate<class T> bool chmax(T& a, const T& b){ if(a < b){ a = b; return 1; } return 0; }\ntemplate<class T, class U> bool chmin(T& a, const U& b){ if(a > T(b)){ a = b; return 1; } return 0; }\ntemplate<class T, class U> bool chmax(T& a, const U& b){ if(a < T(b)){ a = b; return 1; } return 0; }\n\nint main(){\n ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n while(1){\n LL(n,m);\n if(n + m == 0){break;}\n vvpll land_edge(n);\n vvpll sea_edge(n);\n vvll land_dist(n,vll(n,1LL<<60));\n vvll sea_dist(n,vll(n,1LL<<60));\n rep(i,m){\n LL(x,y,t);\n CHA(c);\n x--;\n y--;\n if(c == 'L'){\n land_edge[x].emplace_back(y,t);\n land_edge[y].emplace_back(x,t);\n chmin(land_dist[x][y],t);\n chmin(land_dist[y][x],t);\n }\n else{\n sea_edge[x].emplace_back(y,t);\n sea_edge[y].emplace_back(x,t);\n chmin(sea_dist[x][y],t);\n chmin(sea_dist[y][x],t);\n }\n }\n LL(r);\n VLL(z,r);\n rep(i,r){\n z[i]--;\n }\n\n \n rep(i,n){\n land_dist[i][i] = 0;\n }\n rep(k,n){\n rep(i,n){\n rep(j,n){\n chmin(land_dist[i][j],land_dist[i][k] + land_dist[k][j]);\n }\n }\n }\n\n \n rep(i,n){\n sea_dist[i][i] = 0;\n }\n rep(k,n){\n rep(i,n){\n rep(j,n){\n chmin(sea_dist[i][j],sea_dist[i][k] + sea_dist[k][j]);\n }\n }\n }\n\n vvll dp(r,vll(n,1LL<<60));\n dp[0][z[0]] = 0;\n rep(i,r-1){\n rep(j,n){\n chmin(dp[i+1][j],dp[i][j] + land_dist[z[i]][z[i+1]]);\n rep(k,n){\n chmin(dp[i+1][k],dp[i][j] + land_dist[z[i]][j] + sea_dist[j][k] + land_dist[k][z[i+1]]);\n }\n }\n }\n ll ans = 1LL<<60;\n rep(i,n){\n chmin(ans,dp[r-1][i]);\n }\n print(ans);\n }\n \n}", "accuracy": 1, "time_ms": 240, "memory_kb": 6192, "score_of_the_acc": -0.2365, "final_rank": 17 }, { "submission_id": "aoj_2200_10487262", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll=long long;\nusing ull=unsigned long long;\n#define rep(i, n) for (int i = 0; i < (int)(n); i++)\n#define loop(n) for (int tjh = 0; tjh < (int)(n); tjh++)\nconstexpr int maxN=200;constexpr int maxM=10000;\nconstexpr ll ma=100'000'000'000LL;\nsigned main() {\n cin.tie(0)->sync_with_stdio(0);//NOLINT\n cout<<fixed<<setprecision(16);\n int N,M,R,x,y,z0,z1;ll t;char sl;\n array<array<ll,maxN>,maxN>dl,ds;\n array<array<ll,maxN>,2>re0;int p=0,q=1;\n for (;cin>>N>>M,N;) {\n rep(i,N) {\n fill_n(dl[i].begin(),N,ma);fill_n(ds[i].begin(),N,ma);\n dl[i][i]=0;ds[i][i]=0;\n }\n loop(M) {\n cin>>x>>y>>t>>sl;x--;y--;\n if (sl=='L') {\n if (dl[x][y]>t)dl[x][y]=t;\n if (dl[y][x]>t)dl[y][x]=t;\n }else {\n if (ds[x][y]>t)ds[x][y]=t;\n if (ds[y][x]>t)ds[y][x]=t;\n }\n }\n rep(k,N)rep(i,N)rep(j,N) {\n if (dl[i][j]>dl[i][k]+dl[k][j])dl[i][j]=dl[i][k]+dl[k][j];\n if (ds[i][j]>ds[i][k]+ds[k][j])ds[i][j]=ds[i][k]+ds[k][j];\n }\n cin>>R>>z1;z1--;\n fill_n(re0[q].begin(),N,ma);\n re0[q][z1]=0;\n loop(R-1) {\n z0=z1;swap(p,q);cin>>z1;z1--;\n fill_n(re0[q].begin(),N,ma);\n rep(i,N)rep(j,N) {\n t=i==j?re0[p][i]+dl[z0][z1]:re0[p][i]+dl[z0][i]+ds[i][j]+dl[j][z1];\n if (re0[q][j]>t)re0[q][j]=t;\n }\n }\n cout<<*min_element(re0[q].begin(),re0[q].begin()+N)<<'\\n';\n }\n}", "accuracy": 1, "time_ms": 170, "memory_kb": 4096, "score_of_the_acc": -0.0575, "final_rank": 7 }, { "submission_id": "aoj_2200_10486730", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nconst ll INF = 4e18;\n\n/*-------------------------------------------------------*/\nint main(){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n\n for(;;){\n int N,M;\n if(!(cin>>N>>M) || (N==0 && M==0)) break;\n\n /* 1. 陸路・海路それぞれの隣接行列 */\n vector<vector<ll>> L(N, vector<ll>(N, INF));\n vector<vector<ll>> S(N, vector<ll>(N, INF));\n for(int i=0;i<N;++i) L[i][i]=S[i][i]=0;\n\n for(int i=0;i<M;++i){\n int x,y,t; char sl;\n cin>>x>>y>>t>>sl; --x;--y;\n if(sl=='L') L[x][y]=L[y][x]=min<ll>(L[x][y],t);\n else S[x][y]=S[y][x]=min<ll>(S[x][y],t);\n }\n\n /* 2. Floyd–Warshall (陸路と海路を独立に) */\n for(int k=0;k<N;++k)\n for(int i=0;i<N;++i) if(L[i][k]<INF)\n for(int j=0;j<N;++j) L[i][j]=min(L[i][j],L[i][k]+L[k][j]);\n for(int k=0;k<N;++k)\n for(int i=0;i<N;++i) if(S[i][k]<INF)\n for(int j=0;j<N;++j) S[i][j]=min(S[i][j],S[i][k]+S[k][j]);\n\n /* 3. 配達順 */\n int R; cin>>R;\n vector<int> Z(R);\n for(int &v:Z){cin>>v;--v;}\n\n /* 4. DP: dp[b] = 船が b にあり Z[i] を配達し終えた最短時間 */\n vector<ll> dp(N, INF), ndp(N);\n dp[Z[0]] = 0; // 初期位置:自分も船も Z[0]\n\n for(int k=0;k<R-1;++k){\n int cur = Z[k], nxt = Z[k+1];\n fill(ndp.begin(), ndp.end(), INF);\n\n /* 4-1. 陸路だけで行くケース */\n ll land_only = L[cur][nxt];\n for(int b=0;b<N;++b)\n if(dp[b] < INF && land_only < INF)\n ndp[b] = dp[b] + land_only;\n\n /* 4-2. 海路を使うケース(船に一度乗る) */\n /* pre[b] = dp[b] + L[cur][b] を一旦作る */\n vector<ll> pre(N, INF);\n for(int b=0;b<N;++b)\n if(dp[b] < INF && L[cur][b] < INF)\n pre[b] = dp[b] + L[cur][b];\n\n /* for each x : min_b { pre[b] + S[b][x] } */\n for(int b=0;b<N;++b) if(pre[b] < INF)\n for(int x=0;x<N;++x) if(S[b][x] < INF)\n ndp[x] = min(ndp[x], pre[b] + S[b][x] + L[x][nxt]);\n\n dp.swap(ndp); // 次のステップへ\n }\n\n /* 5. 最終ステップの最小値が答え */\n ll ans = *min_element(dp.begin(), dp.end());\n cout << ans << '\\n';\n }\n return 0;\n}", "accuracy": 1, "time_ms": 160, "memory_kb": 4112, "score_of_the_acc": -0.0551, "final_rank": 4 }, { "submission_id": "aoj_2200_10080892", "code_snippet": "#include <iostream>\n#include<algorithm>\n#include<cstring>\n#include<cmath>\n#include<queue>\n#include<vector>\n#include<stack>\n#include<set>\n#include<map>\n#include<cstdio>\n#include<iomanip>\n#include<sstream>\nusing namespace std;\n#define ll long long\n#define ft first\n#define sd second\n#define mst(a,b) memset(a,b,sizeof(a))\ntypedef pair<int, int> PII;\n\nconst int mod =1e9;\nconst int N = 1e5 + 10;\nconst int M = 2e4 + 10;\nconst ll inf = 0x3f3f3f3f3f3f;\n\nint t, l, r,ex, sy,s;\nint n,m,cnt=0;\nll ldist[1010][1010], sdist[1010][1010];\nll ord[1010];\nll cost[1010][1010];\n\nvoid floyd() {\n\tfor (int k = 1; k <= n; k++) {\n\t\tfor (int i = 1; i <= n; i++) {\n\t\t\tfor (int j = 1; j <= n; j++) {\n\t\t\t\tldist[i][j] = min(ldist[i][j], ldist[i][k] + ldist[k][j]);\n\t\t\t\tsdist[i][j] = min(sdist[i][j], sdist[i][k] + sdist[k][j]);\n\t\t\t}\n\t\t}\n\t}\n}\n\nvoid solve() {\n\twhile (cin >> n >> m) {\n\t\tif (n == 0 && m == 0)break;\n\t\tfor (int i = 1; i <= n; i++) {\n\t\t\tfor (int j = 1; j <= n; j++) {\n\t\t\t\tldist[i][j] = sdist[i][j] = inf;\n\t\t\t}\n\t\t}\n\t\twhile (m--) {\n\t\t\tll x, y, dist;\n\t\t\tchar s;\n\t\t\tbool f;\n\t\t\tcin >> x >> y >> dist >> s;\n\t\t\tif (s == 'L')\n\t\t\t\tldist[x][y] = ldist[y][x] = min(ldist[x][y], dist);\n\t\t\telse\n\t\t\t\tsdist[x][y] = sdist[y][x] = min(sdist[x][y], dist);\n\t\t}\n\t\tint q;\n\t\tcin >> q;\n\t\tfor (int i = 1; i <= q; i++)\n\t\t\tcin >> ord[i];\n\t\tfor (int i = 0; i <= q; i++) {\n\t\t\tfor (int j = 1; j <= n; j++) {\n\t\t\t\tcost[i][j] = inf;\n\t\t\t}\n\t\t}\n\t\tfloyd();\n\t\tfor (int i = 1; i <= n; i++)\n\t\t\tldist[i][i] = sdist[i][i] = 0;\n\t\tcost[1][ord[1]] = 0;\n\t\tfor (int i = 1; i <= q; i++) {\n\t\t\tfor (int j = 1; j <= n; j++) {\n\t\t\t\tcost[i][j] = min(cost[i][j], cost[i - 1][j] + ldist[ord[i - 1]][ord[i]]);\n\t\t\t\tfor (int k = 1; k <= n; k++) {\n\t\t\t\t\tcost[i][k] = min(cost[i][k], cost[i - 1][j] + ldist[ord[i - 1]][j] + sdist[j][k] + ldist[k][ord[i]]);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tll mi = 1e12;\n\t\tfor (int i = 1; i <= n; i++) {\n\t\t\tmi = min(mi, cost[q][i]);\n\t\t}\n\t\tcout << mi << endl;\n\t}\n}\n\nint main() {\n\tios::sync_with_stdio(0);\n\tcin.tie(0);\n\tcout.tie(0);\n\tt = 1;\n\t//cin >> t;\n\twhile (t--)\n\t\tsolve();\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 150, "memory_kb": 17116, "score_of_the_acc": -1.0071, "final_rank": 19 }, { "submission_id": "aoj_2200_9662812", "code_snippet": "#include <bits/stdc++.h>\n//#include <atcoder/all>\n#include <unordered_set>\n#include <functional>\nusing namespace std;\n//using namespace atcoder;\nusing ll = long long;\nusing P = pair<int, int>;\n#define rep(i, n) for (int i = 0; i < (n); i++)\n//void chmin(ll & a, ll b) { a = min(a, b); }\n//using mint = modint998244353;\n//using mint = modint1000000007;\n\ntemplate<class T> inline bool chmin(T & a, T b) { if (a > b) { a = b; return 1; } return 0; }\nconst ll INF = 1LL << 60;\n\nint main() {\n\tint N, M;\n\twhile (cin >> N >> M) {\n\t if(N==0 && M==0) break;\n\t\tvector<vector<ll>> dps(N, vector<ll>(N, INF)),dpl(N,vector<ll>(N,INF));\n\t\tfor (int v = 0; v < N; v++) dps[v][v] = dpl[v][v] = 0;\n\t\trep(e, M) {\n\t\t\tint x, y;\n\t\t\tll w;\n\t\t\tchar type;\n\t\t\tcin >> x >> y >> w >> type;\n\t\t\t--x, --y; \n\t\t\tif (type == 'L')chmin(dpl[x][y], w), chmin(dpl[y][x], w);\n\t\t\telse chmin(dps[x][y], w), chmin(dps[y][x], w);\n\t\t}\n\t\trep(k, N)rep(i, N)rep(j, N) {\n\t\t\tdpl[i][j] = min(dpl[i][j], dpl[i][k] + dpl[k][j]);\n\t\t\tdps[i][j] = min(dps[i][j], dps[i][k] + dps[k][j]);\n\t\t}\n\n\t\tint R;\n\t\tcin >> R;\n\t\tvector<int> v(R);\n\t\trep(i, R) cin >> v[i], v[i]--;\n\n\t\tvector<vector<ll>> dp(R, vector<ll>(N, INF));\n\t\tdp[0][v[0]] = 0;\n\t\trep(i, R - 1) {\n\t\t\trep(j, N) {\n\t\t\t\tdp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + dpl[v[i]][v[i + 1]]);\n\t\t\t\trep(k, N) {\n\t\t\t\t\tdp[i + 1][k] = min(dp[i + 1][k], dp[i][j] + dpl[v[i]][j] + dps[j][k] + dpl[k][v[i + 1]]);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tll res = INF;\n\t\trep(i, N) {\n\t\t\tres = min(res, dp[R - 1][i]);\n\t\t}\n\t\tcout << res << endl;\n\t}\n\n}", "accuracy": 1, "time_ms": 220, "memory_kb": 5508, "score_of_the_acc": -0.1791, "final_rank": 12 }, { "submission_id": "aoj_2200_9526107", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n#include <unistd.h>\n\nnamespace fastio {\nstatic constexpr uint32_t SZ = 1 << 17;\nchar ibuf[SZ];\nchar obuf[SZ];\nchar out[100];\n// pointer of ibuf, obuf\n\n\nuint32_t pil = 0, pir = 0, por = 0;\n\nstruct Pre {\n char num[10000][4];\n constexpr Pre() : num() {\n for (int i = 0; i < 10000; i++) {\n int n = i;\n for (int j = 3; j >= 0; j--) {\n num[i][j] = n % 10 | '0';\n n /= 10;\n }\n }\n }\n} constexpr pre;\n\ninline void load() {\n memmove(ibuf, ibuf + pil, pir - pil);\n pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin);\n pil = 0;\n if (pir < SZ)\n ibuf[pir++] = '\\n';\n}\n\ninline void flush() {\n fwrite(obuf, 1, por, stdout);\n por = 0;\n}\n\nvoid rd(char &c) {\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n}\n\nvoid rd(string &x) {\n x.clear();\n char c;\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n do {\n x += c;\n if (pil == pir)\n load();\n c = ibuf[pil++];\n } while (!isspace(c));\n}\n\ntemplate <typename T> void rd_real(T &x) {\n string s;\n rd(s);\n x = stod(s);\n}\n\ntemplate <typename T> void rd_integer(T &x) {\n if (pil + 100 > pir)\n load();\n char c;\n do\n c = ibuf[pil++];\n while (c < '-');\n bool minus = 0;\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (c == '-') {\n minus = 1, c = ibuf[pil++];\n }\n }\n x = 0;\n while ('0' <= c) {\n x = x * 10 + (c & 15), c = ibuf[pil++];\n }\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (minus)\n x = -x;\n }\n}\n\nvoid rd(int &x) {\n rd_integer(x);\n}\nvoid rd(ll &x) {\n rd_integer(x);\n}\nvoid rd(i128 &x) {\n rd_integer(x);\n}\nvoid rd(uint &x) {\n rd_integer(x);\n}\nvoid rd(ull &x) {\n rd_integer(x);\n}\nvoid rd(u128 &x) {\n rd_integer(x);\n}\nvoid rd(double &x) {\n rd_real(x);\n}\nvoid rd(long double &x) {\n rd_real(x);\n}\n\ntemplate <class T, class U> void rd(pair<T, U> &p) {\n return rd(p.first), rd(p.second);\n}\ntemplate <size_t N = 0, typename T> void rd_tuple(T &t) {\n if constexpr (N < std::tuple_size<T>::value) {\n auto &x = std::get<N>(t);\n rd(x);\n rd_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void rd(tuple<T...> &tpl) {\n rd_tuple(tpl);\n}\n\ntemplate <size_t N = 0, typename T> void rd(array<T, N> &x) {\n for (auto &d : x)\n rd(d);\n}\ntemplate <class T> void rd(vector<T> &x) {\n for (auto &d : x)\n rd(d);\n}\n\nvoid read() {}\ntemplate <class H, class... T> void read(H &h, T &...t) {\n rd(h), read(t...);\n}\n\nvoid wt(const char c) {\n if (por == SZ)\n flush();\n obuf[por++] = c;\n}\nvoid wt(const string s) {\n for (char c : s)\n wt(c);\n}\nvoid wt(const char *s) {\n size_t len = strlen(s);\n for (size_t i = 0; i < len; i++)\n wt(s[i]);\n}\n\ntemplate <typename T> void wt_integer(T x) {\n if (por > SZ - 100)\n flush();\n if (x < 0) {\n obuf[por++] = '-', x = -x;\n }\n int outi;\n for (outi = 96; x >= 10000; outi -= 4) {\n memcpy(out + outi, pre.num[x % 10000], 4);\n x /= 10000;\n }\n if (x >= 1000) {\n memcpy(obuf + por, pre.num[x], 4);\n por += 4;\n } else if (x >= 100) {\n memcpy(obuf + por, pre.num[x] + 1, 3);\n por += 3;\n } else if (x >= 10) {\n int q = (x * 103) >> 10;\n obuf[por] = q | '0';\n obuf[por + 1] = (x - q * 10) | '0';\n por += 2;\n } else\n obuf[por++] = x | '0';\n memcpy(obuf + por, out + outi + 4, 96 - outi);\n por += 96 - outi;\n}\n\ntemplate <typename T> void wt_real(T x) {\n ostringstream oss;\n oss << fixed << setprecision(15) << double(x);\n string s = oss.str();\n wt(s);\n}\n\nvoid wt(int x) {\n wt_integer(x);\n}\nvoid wt(ll x) {\n wt_integer(x);\n}\nvoid wt(i128 x) {\n wt_integer(x);\n}\nvoid wt(uint x) {\n wt_integer(x);\n}\nvoid wt(ull x) {\n wt_integer(x);\n}\nvoid wt(u128 x) {\n wt_integer(x);\n}\nvoid wt(double x) {\n wt_real(x);\n}\nvoid wt(long double x) {\n wt_real(x);\n}\n\ntemplate <class T, class U> void wt(const pair<T, U> val) {\n wt(val.first);\n wt(' ');\n wt(val.second);\n}\ntemplate <size_t N = 0, typename T> void wt_tuple(const T t) {\n if constexpr (N < std::tuple_size<T>::value) {\n if constexpr (N > 0) {\n wt(' ');\n }\n const auto x = std::get<N>(t);\n wt(x);\n wt_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void wt(tuple<T...> tpl) {\n wt_tuple(tpl);\n}\ntemplate <class T, size_t S> void wt(const array<T, S> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\ntemplate <class T> void wt(const vector<T> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\n\nvoid print() {\n wt('\\n');\n}\ntemplate <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {\n wt(head);\n if (sizeof...(Tail))\n wt(' ');\n print(forward<Tail>(tail)...);\n}\nvoid __attribute__((destructor)) _d() {\n flush();\n}\n} // namespace fastio\n\n\nusing fastio::flush;\nusing fastio::print;\nusing fastio::read;\n\ninline void first(bool i = true) {\n print(i ? \"first\" : \"second\");\n}\ninline void Alice(bool i = true) {\n print(i ? \"Alice\" : \"Bob\");\n}\ninline void Takahashi(bool i = true) {\n print(i ? \"Takahashi\" : \"Aoki\");\n}\ninline void yes(bool i = true) {\n print(i ? \"yes\" : \"no\");\n}\ninline void Yes(bool i = true) {\n print(i ? \"Yes\" : \"No\");\n}\ninline void No() {\n print(\"No\");\n}\ninline void YES(bool i = true) {\n print(i ? \"YES\" : \"NO\");\n}\ninline void NO() {\n print(\"NO\");\n}\ninline void Yay(bool i = true) {\n print(i ? \"Yay!\" : \":(\");\n}\ninline void Possible(bool i = true) {\n print(i ? \"Possible\" : \"Impossible\");\n}\ninline void POSSIBLE(bool i = true) {\n print(i ? \"POSSIBLE\" : \"IMPOSSIBLE\");\n}\n\n/**\n * @brief Fast IO\n */\n\nint main() {\nwhile(1) {\n int N, M;\n cin >> N >> M;\n if (N == 0 && M == 0) return 0;\n vector<vector<ll>> DL(N,vector<ll>(N,INF/4)), DS(N,vector<ll>(N,INF/4));\n rep(i,0,N) DL[i][i] = DS[i][i] = 0;\n rep(i,0,M) {\n ll U, V, D;\n char C;\n cin >> U >> V >> D >> C;\n U--, V--;\n if (C == 'L') chmin(DL[U][V], D), chmin(DL[V][U], D);\n else chmin(DS[U][V], D), chmin(DS[V][U], D);\n }\n rep(k,0,N) {\n rep(i,0,N) {\n rep(j,0,N) {\n chmin(DL[i][j], DL[i][k] + DL[k][j]);\n chmin(DS[i][j], DS[i][k] + DS[k][j]);\n }\n }\n }\n vector<ll> ANS(N, INF/4);\n int Q;\n int Now = 0;\n cin >> Q >> Now;\n Now--;\n ANS[Now] = 0;\n rep(_,0,Q-1) {\n int Z;\n cin >> Z;\n Z--;\n vector<ll> Next(N, INF/4);\n rep(i,0,N) {\n chmin(Next[i], ANS[i] + DL[Now][Z]);\n rep(j,0,N) {\n chmin(Next[j], ANS[i] + DL[Now][i] + DS[i][j] + DL[j][Z]);\n }\n }\n rep(i,0,N) ANS[i] = Next[i];\n Now = Z;\n }\n ll MIN = INF/4;\n rep(i,0,N) {\n chmin(MIN, ANS[i]);\n }\n cout << MIN << endl;\n}\n}", "accuracy": 1, "time_ms": 220, "memory_kb": 3824, "score_of_the_acc": -0.0554, "final_rank": 5 }, { "submission_id": "aoj_2200_9351059", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)\n#define rrep(i,start,end) for (long long i = start;i >= (long long)(end);i--)\n#define repn(i,end) for(long long i = 0; i <= (long long)(end); i++)\n#define reps(i,start,end) for(long long i = start; i < (long long)(end); i++)\n#define repsn(i,start,end) for(long long i = start; i <= (long long)(end); i++)\n#define each(p,a) for(auto &p:a)\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef long double ld;\ntypedef vector<long long> vll;\ntypedef vector<pair<long long ,long long>> vpll;\ntypedef vector<vector<long long>> vvll;\ntypedef set<ll> sll;\ntypedef map<long long , long long> mpll;\ntypedef pair<long long ,long long> pll;\ntypedef tuple<long long , long long , long long> tpl3;\n#define LL(...) ll __VA_ARGS__; input(__VA_ARGS__)\n#define LD(...) ld __VA_ARGS__; input(__VA_ARGS__)\n#define Str(...) string __VA_ARGS__; input(__VA_ARGS__)\n#define Ch(...) char __VA_ARGS__; input(__VA_ARGS__)\n#define all(a) (a).begin(),(a).end()\n#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );\n#define sz(x) (int)x.size()\n// << std::fixed << std::setprecision(10)\nconst ll INF = 1LL << 60;\nconst ld EPS = 1e-9;\n \ninline ll lfloor(ll x,ll m){return (x - ((x % m+ m)%m))/m;}\ninline ll positive_mod(ll a,ll m){return (a % m + m)%m;}\ninline ll popcnt(ull a){ return __builtin_popcountll(a);}\ntemplate<class T> bool chmin(T& a, T b){if(a > b){a = b;return true;}return false;}\ntemplate<class T> bool chmax(T& a, T b){if(a < b){a = b;return true;}return false;}\ntemplate<typename T> std::istream &operator>>(std::istream&is,std::vector<T>&v){for(T &in:v){is>>in;}return is;}\ntemplate<typename T> std::ostream &operator<<(std::ostream&os,const std::vector<T>&v){for(auto it=std::begin(v);it!=std::end(v);){os<<*it<<((++it)!=std::end(v)?\" \":\"\");}return os;}\ntemplate<typename T1, typename T2>std::ostream &operator<< (std::ostream &os, std::pair<T1,T2> p){os << \"{\" << p.first << \",\" << p.second << \"}\";return os;}\ntemplate<class... T>void input(T&... a){(cin >> ... >> a);}\nvoid print(){cout << endl;}\ntemplate<class T, class... Ts>void print(const T& a, const Ts&... b){cout << a;((cout << ' ' << b), ...);cout << endl;}\ntemplate<class T> void pspace(const T& a){ cout << a << ' ';}\nvoid perr(){cerr << endl;}\ntemplate<class T, class... Ts>void perr(const T& a, const Ts&... b){cerr << a;((cerr << ' ' << b), ...);cerr << endl;}\nvoid yes(bool i = true){ return print(i?\"yes\":\"no\"); }\nvoid Yes(bool i = true){ return print(i?\"Yes\":\"No\"); }\nvoid YES(bool i = true){ return print(i?\"YES\":\"NO\"); }\ntemplate <class T> vector<T> &operator++(vector<T> &v) {for(auto &e : v) e++;return v;}\ntemplate <class T> vector<T> operator++(vector<T> &v, signed) {auto res = v;for(auto &e : v) e++;return res;}\ntemplate <class T> vector<T> &operator--(vector<T> &v) {for(auto &e : v) e--;return v;}\ntemplate <class T> vector<T> operator--(vector<T> &v, signed) {auto res = v;for(auto &e : v) e--;return res;}\n//grid探索用\nvector<ll> _ta = {0,0,1,-1,1,1,-1,-1};\nvector<ll> _yo = {1,-1,0,0,1,-1,1,-1};\nbool isin(ll now_i,ll now_j,ll h,ll w){return (0<=now_i && now_i < h && 0 <= now_j && now_j < w);}\n \nll lpow(ll x,ll n){ll ans = 1;while(n >0){if(n & 1)ans *= x;x *= x;n >>= 1;}return ans;}\nll Modlpow(ll x,ll n,ll m){ll ans = 1;ll a = x%m;while(n >0){if(n & 1){ans *= a;ans%= m;}a *= a;a %= m;n >>= 1;}return ans;} \nconst ll MOD9 = 998244353LL;\nconst ll MOD10 = 1000000007LL;\n\n// for AOJ or ICPC or etc..\n// 全部が0だったらtrueを返す\ntemplate<class Tp> bool zero (const Tp &x) {return x == 0;}\ntemplate<class Tp, class... Args> bool zero (const Tp &x, const Args& ...args) {return zero(x) and zero(args...);}\n\n\n/*隣接行列を更新してくDPのイメージ\nつながってない辺は重みINF、繋がってたら辺の重みで初期化\nO(V^3)\n*/\nint warshall_floyd(vector<vector<ll>> &dist){\n int size = (int)dist.size();\n rep(k,size){\n rep(i,size){\n rep(j,size){\n if(i == j){\n chmin(dist[i][j],0LL);\n }\n if(dist[i][k] != INF && dist[k][j] != INF){\n chmin(dist[i][j],dist[i][k] + dist[k][j]);\n }\n }\n }\n }\n //負の閉路判定あれば−1大丈夫なら1\n rep(i,size){\n if(dist[i][i]<0){\n return -1;\n }\n }\n return 1;\n}\n\n// 変数をちゃんと全部受け取る!\nvoid solve(ll n,ll m){\n vvll ship(n,vll(n,INF)),road(n,vll(n,INF));\n rep(i,m){\n LL(x,y,l);\n Ch(c);\n x--;y--;\n if(c == 'S'){\n chmin(ship[x][y],l);\n chmin(ship[y][x],l);\n }else{\n chmin(road[x][y],l);\n chmin(road[y][x],l);\n }\n }\n warshall_floyd(ship);\n warshall_floyd(road);\n LL(r);\n vll z(r);cin >> z;z--;\n vvll dp(r,vll(n,INF));\n dp[0][z[0]] = 0;\n rep(i,r-1){\n ll now = z[i];\n ll to = z[i+1];\n rep(j,n){\n if(dp[i][j] == INF)continue;\n chmin(dp[i+1][j],dp[i][j] + road[now][to]);//船を置いたまま陸路で移動\n rep(k,n){\n if(j == k or ship[j][k] == INF){\n continue;\n }\n //now-> j -> k -> to\n ll cost = road[now][j] + ship[j][k] + road[k][to];\n chmin(dp[i+1][k],dp[i][j] + cost);\n }\n }\n }\n ll ans = INF;\n rep(i,n){\n chmin(ans,dp[r-1][i]);\n }\n cout << ans << endl;\n\n}\n \nint main(){\n ios::sync_with_stdio(false);cin.tie(nullptr);\n while(true){\n LL(n,m);//変数数調整\n if(zero(n,m))break;\n solve(n,m);\n }\n}", "accuracy": 1, "time_ms": 260, "memory_kb": 5396, "score_of_the_acc": -0.1852, "final_rank": 13 }, { "submission_id": "aoj_2200_9319824", "code_snippet": "#include <iostream>\n#include <algorithm>\nusing namespace std;\nconst int MAX_V = 200, INF = 200000;\nint dl[MAX_V][MAX_V], ds[MAX_V][MAX_V];\nint V;\nvoid warshall_floyd(int d[MAX_V][MAX_V]);\n\nconst int MAX_R = 1000;\nint N, M, R, xi, yi, ti;\nchar sli;\nint z[MAX_R], ans[2][MAX_V];\n\nint main() {\n while (cin >> N >> M) {\n if (N == 0 && M == 0)\n break;\n V = N;\n for (int i = 0; i < V; i++) {\n for (int j = 0; j < V; j++) {\n dl[i][j] = INF;\n ds[i][j] = INF;\n }\n }\n for (int i = 0; i < V; i++) {\n dl[i][i] = 0;\n ds[i][i] = 0;\n }\n for (int i = 0; i < M; i++) {\n cin >> xi >> yi >> ti >> sli;\n xi--; yi--;\n if (sli == 'L') {\n dl[xi][yi] = min(dl[xi][yi], ti);\n dl[yi][xi] = min(dl[yi][xi], ti);\n }\n if (sli == 'S') {\n ds[xi][yi] = min(ds[xi][yi], ti);\n ds[yi][xi] = min(ds[yi][xi], ti);\n }\n }\n cin >> R;\n for (int i = 0; i < R; i++) {\n cin >> z[i];\n z[i]--;\n }\n warshall_floyd(dl);\n warshall_floyd(ds);\n for (int i = 0; i < N; i++)\n ans[0][i] = (int)1e9;\n ans[0][z[0]] = 0;\n for (int i = 1; i < R; i++) {\n for (int j = 0; j < N; j++)\n ans[i & 1][j] = (int)1e9;\n for (int j = 0; j < N; j++) {\n for (int k = 0; k < N; k++) {\n ans[i & 1][k] = min(ans[i & 1][k], ans[(i - 1) & 1][j] + dl[z[i - 1]][j] + ds[j][k] + dl[k][z[i]]);\n if (j == k)\n ans[i & 1][k] = min(ans[i & 1][k], ans[(i - 1) & 1][j] + dl[z[i - 1]][z[i]]);\n }\n }\n }\n int tmp = INF;\n for (int i = 0; i < N; i++)\n tmp = min(tmp, ans[(R - 1) & 1][i]);\n cout << tmp << endl;\n }\n return 0;\n}\n\nvoid warshall_floyd(int d[MAX_V][MAX_V]) {\n for (int k = 0; k < V; k++) {\n for (int i = 0; i < V; i++) {\n for (int j = 0; j < V; j++)\n d[i][j] = min(d[i][j], d[i][k] + d[k][j]);\n }\n }\n}", "accuracy": 1, "time_ms": 180, "memory_kb": 3688, "score_of_the_acc": -0.0311, "final_rank": 2 }, { "submission_id": "aoj_2200_9313298", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i, a, n) for(ll i = a; i < n; i++)\n#define rrep(i, a, n) for(ll i = a; i >= n; i--)\n#define inr(l, x, r) (l <= x && x < r)\n#define ll long long\n#define ld long double\n#define pii pair<ll,ll>\n\nconstexpr ll IINF = 1001001001;\nconstexpr ll INF = 9e18;\n\ntemplate<class t,class u> void chmax(t&a,u b){if(a<b)a=b;}\ntemplate<class t,class u> void chmin(t&a,u b){if(b<a)a=b;}\n\n\nint main(){\n while(1){\n ll n, m;cin >> n >> m;\n if(n+m == 0) return 0;\n vector<vector<pii>> gl(n),gs(n);\n vector<vector<ll>> ldist(n,vector<ll>(n,1LL<<60)), sdist(n,vector<ll>(n,1LL<<60));\n rep(i, 0, n) ldist[i][i] = 0, sdist[i][i] = 0;\n rep(i, 0, m){\n ll s, t, d; cin >> s >> t >> d;\n s--;t--;\n char type; cin >> type;\n if(type == 'L'){\n gl[s].push_back({t, d});\n gl[t].push_back({s, d});\n chmin(ldist[s][t], d);\n chmin(ldist[t][s], d);\n }else{\n gs[s].push_back({t, d});\n gs[t].push_back({s, d});\n chmin(sdist[s][t], d);\n chmin(sdist[t][s], d);\n }\n }\n rep(k, 0, n)rep(i, 0, n)rep(j, 0, n){\n chmin(ldist[i][j],ldist[i][k]+ldist[k][j]);\n chmin(sdist[i][j],sdist[i][k]+sdist[k][j]);\n }\n ll r; cin >> r;\n vector<ll> tour(r);\n rep(i, 0, r){\n cin >> tour[i];\n tour[i]--;\n }\n vector<vector<ll>> dp(r+1, vector<ll>(n,(1LL<<60)));\n dp[0][tour[0]] = 0;\n rep(i, 0, r-1){\n rep(j, 0, n){\n chmin(dp[i+1][j], dp[i][j]+ldist[tour[i]][tour[i+1]]);\n rep(k, 0, n){\n chmin(dp[i+1][k], dp[i][j]+ldist[tour[i]][j]+sdist[j][k]+ldist[k][tour[i+1]]);\n }\n }\n }\n ll ans = (1LL<<60);\n rep(i, 0, n) chmin(ans, dp[r-1][i]);\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 270, "memory_kb": 5644, "score_of_the_acc": -0.207, "final_rank": 15 }, { "submission_id": "aoj_2200_9242725", "code_snippet": "#include <bits/stdc++.h>\n\nint solve() {\n int n, m;\n std::cin >> n >> m;\n if (n == 0) return 1;\n const int INF = 1e9;\n std::vector L(n, std::vector<int>(n, INF));\n std::vector S(n, std::vector<int>(n, INF));\n for (int i = 0; i < m; i++) {\n int u, v, t;\n char c;\n std::cin >> u >> v >> t >> c;\n u--; v--;\n if (c == 'L') {\n L[u][v] = std::min(L[u][v], t);\n L[v][u] = std::min(L[v][u], t);\n } else {\n S[u][v] = std::min(S[u][v], t);\n S[v][u] = std::min(S[v][u], t);\n }\n }\n for (int i = 0; i < n; i++) L[i][i] = S[i][i] = 0;\n for (int k = 0; k < n; k++) {\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n L[i][j] = std::min(L[i][j], L[i][k] + L[k][j]);\n S[i][j] = std::min(S[i][j], S[i][k] + S[k][j]);\n }\n }\n }\n int q;\n std::cin >> q;\n std::vector<int> z(q);\n for (int i = 0; i < q; i++) std::cin >> z[i], z[i]--;\n std::vector<int> dist(n, INF);\n dist[z[0]] = 0;\n for (int i = 0; i + 1 < q; i++) {\n std::vector<int> ndist(n, INF);\n // 今船がある場所\n for (int j = 0; j < n; j++) {\n if (dist[j] == INF) continue;\n // 次船がある場所\n for (int k = 0; k < n; k++) {\n if (j == k) {\n if (L[z[i]][z[i + 1]] != INF) {\n ndist[k] = std::min(ndist[k], dist[j] + L[z[i]][z[i + 1]]);\n }\n } else {\n if (L[z[i]][j] != INF && S[j][k] != INF && L[k][z[i + 1]] != INF) {\n ndist[k] = std::min(ndist[k], dist[j] + L[z[i]][j] + S[j][k] + L[k][z[i + 1]]);\n }\n }\n }\n }\n dist = std::move(ndist);\n }\n std::cout << *std::min_element(dist.begin(), dist.end()) << '\\n';\n return 0;\n}\n\nint main() {\n while (!solve());\n}", "accuracy": 1, "time_ms": 330, "memory_kb": 3716, "score_of_the_acc": -0.0867, "final_rank": 9 }, { "submission_id": "aoj_2200_9242120", "code_snippet": "// 18:10\n#include<bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nconst ll LINF = 1001001001001001001ll;\n\nstruct Warshall_Floyd{\n vector<vector<ll>> dist;\n int size;\n\n Warshall_Floyd(int N) : size(N), dist(N, vector<ll>(N, LINF)){\n for(int i = 0; i < N; i++) dist[i][i] = 0;\n }\n void add_edge(int u, int v, int w){\n dist[u][v] = w;\n }\n bool solve(){\n for(int k = 0; k < size; k++){\n for(int i = 0; i < size; i++){\n for(int j = 0; j < size; j++){\n if(dist[i][k] == LINF || dist[k][j] == LINF) continue;\n if(dist[i][j] > dist[i][k] + dist[k][j]){\n dist[i][j] = dist[i][k] + dist[k][j];\n }\n }\n }\n }\n for(int i = 0; i < size; i++){\n if(dist[i][i] < 0) return false;\n }\n return true;\n }\n};\n\nint main(){\n while(1){\n int N, M; cin >> N >> M;\n if(N == 0) break;\n Warshall_Floyd land(N), sea(N);\n for(int i = 0; i < M; i++){\n int x, y, t;\n char sl;\n cin >> x >> y >> t >> sl;\n x--, y--;\n if(sl == 'L'){\n land.add_edge(x, y, t);\n land.add_edge(y, x, t);\n }\n else{\n sea.add_edge(x, y, t);\n sea.add_edge(y, x, t);\n }\n }\n land.solve();\n sea.solve();\n\n vector<ll> dp1(N, LINF), dp2(N, LINF), nxt(N, LINF);\n int R; cin >> R;\n int start; cin >> start;\n start--;\n dp1[start] = 0;\n\n for(int _ = 1; _ < R; _++){\n int goal; cin >> goal;\n goal--;\n vector<ll> dp2(N, LINF), nxt(N, LINF);\n\n // only land\n for(int i = 0; i < N; i++){\n nxt[i] = min(nxt[i], dp1[i] + land.dist[start][goal]);\n }\n // land -> boat\n for(int i = 0; i < N; i++){\n dp2[i] = min(dp2[i], dp1[i] + land.dist[start][i]);\n }\n // dp2 -> nxt\n for(int i = 0; i < N; i++){\n // boat -> sea\n nxt[goal] = min(nxt[goal], dp2[i] + sea.dist[i][goal]);\n // boat -> sea -> land\n for(int j = 0; j < N; j++){\n nxt[j] = min(nxt[j], dp2[i] + sea.dist[i][j] + land.dist[j][goal]);\n }\n }\n\n swap(dp1, nxt);\n // for(int i = 0; i < N; i++) cout << dp1[i] << \" \";\n // cout << endl;\n start = goal;\n }\n\n ll ans = LINF;\n for(int i = 0; i < N; i++) ans = min(ans, dp1[i]);\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 230, "memory_kb": 3780, "score_of_the_acc": -0.0557, "final_rank": 6 }, { "submission_id": "aoj_2200_9131528", "code_snippet": "#include <algorithm>\n#include <cctype>\n#include <climits>\n#include <cstring>\n#include <iomanip>\n#include <iostream>\n#include <map>\n#include <math.h>\n#include <queue>\n#include <set>\n#include <sstream>\n#include <stdio.h>\n#include <string>\n#include <vector>\nusing namespace std;\ntypedef long long ll;\n\nconst int INF = INT_MAX;\n\n//Graph\nstruct edge{int to, cost;};\nclass Graph{\nprivate:\n vector<vector<edge>>neighbors;\n vector<ll>shortest;\n vector<int>prev;\n //this is for warshall froid.\n vector<vector<ll>>distances;\n int V;\n int E;\n\npublic:\n Graph(const int n){\n V = n;\n neighbors = vector<vector<edge>>(V);\n shortest = vector<ll>(V, LLONG_MAX);\n prev = vector<int>(V, -1);\n distances = vector<vector<ll>>(V, vector<ll>(V, LLONG_MAX));\n };\n\n void add_edge(const int f, const int t, const int c=INT_MAX){\n edge e; e.to = t; e.cost = c;\n neighbors[f].push_back(e);\n E++;\n }\n\n //Dijkstra\n typedef pair<ll, int> P;\n void dijkstra(const int s){\n fill(shortest.begin(), shortest.end(), LLONG_MAX);\n fill(prev.begin(), prev.end(), -1);\n priority_queue<P, vector<P>, greater<P>>pque;\n shortest[s] = 0;\n pque.push(P(0, s));\n\n while(!pque.empty()){\n P p = pque.top(); pque.pop();\n if(shortest[p.second] < p.first)continue;\n for(int i = 0; i < neighbors[p.second].size(); i++){\n edge e = neighbors[p.second][i];\n if(shortest[e.to] > shortest[p.second] + e.cost){\n shortest[e.to] = shortest[p.second] + e.cost;\n prev[e.to] = p.second;\n pque.push(P(shortest[e.to], e.to));\n }\n }\n } \n }\n\n //Bellman-Ford algorithm\n void Bellman_Ford(const int s){\n fill(shortest.begin(), shortest.end(), LLONG_MAX);\n fill(prev.begin(), prev.end(), -1);\n shortest[s] = 0;\n while(true){\n bool updated = false;\n for(int i = 0; i < V; i++){\n for(int j = 0; j < neighbors[i].size(); j++){\n edge e = neighbors[i][j];\n if(shortest[i] != LLONG_MAX && shortest[i] + e.cost < shortest[e.to]){\n shortest[e.to] = shortest[i] + e.cost;\n prev[e.to] = i;\n updated = true;\n }\n }\n }\n\n if(!updated)break;\n }\n }\n\n //Warshall_froid\n void warshall_froid(){\n distances = vector<vector<ll>>(V, vector<ll>(V, LLONG_MAX));\n for(int i = 0; i < V; i++){\n distances[i][i] = 0;\n for(int j = 0; j < neighbors[i].size(); j++){\n edge e = neighbors[i][j];\n distances[i][e.to] = e.cost;\n }\n }\n\n for(int k = 0; k < V; k++){\n for(int i = 0; i < V; i++){{\n for(int j = 0; j < V; j++){\n if(distances[i][k] != LLONG_MAX && distances[k][j] != LLONG_MAX) distances[i][j] = min(distances[i][j], distances[i][k] + distances[k][j]);\n }\n }\n }\n }\n }\n\n //find negative loop\n bool find_negative_loop(){\n fill(shortest.begin(), shortest.end(), 0);\n for(int i = 0; i < V; i++){\n for(int j = 0; j < V; j++){\n for(int k = 0; k < neighbors[j].size(); k++){\n edge e = neighbors[j][k];\n if(shortest[j] + e.cost < shortest[e.to]){\n shortest[e.to] = shortest[j] + e.cost;\n if(i = V-1)return true;\n }\n }\n }\n }\n return false;\n }\n\n //get the shortest path from s to g\n //make sure it is done after algorithm\n ll get_value(const int g){\n return shortest[g];\n }\n ll get_value(const int s, const int g){\n return distances[s][g];\n }\n\n\n //get the shortest path route from s to g\n //make sure it is done after algorithm\n vector<int> get_route(const int s, const int g){\n vector<int>routes;\n if(get_value(g) == LLONG_MAX)return routes;\n\n for(int t = g; prev[t] != -1; t = prev[t])routes.push_back(t);\n routes.push_back(s);\n reverse(routes.begin(), routes.end());\n return routes;\n }\n};\n\n\nint main(){\n while(true){\n int N, M; scanf(\"%d%d\", &N, &M);\n if(N == 0 && M == 0)break;\n Graph L(N);\n Graph S(N);\n\n for(int m = 0; m < M; m++){\n int x, y, t; char c; scanf(\"%d%d%d %c\", &x, &y, &t, &c);\n\n if(c == 'L'){\n L.add_edge(x-1, y-1, t);\n L.add_edge(y-1, x-1, t);\n }\n else{\n S.add_edge(x-1, y-1, t);\n S.add_edge(y-1, x-1, t);\n }\n }\n \n L.warshall_froid();\n S.warshall_froid();\n\n int R; scanf(\"%d\", &R);\n vector<vector<ll>>dp(R, vector<ll>(N, LLONG_MAX));\n vector<int>order(R);\n for(int r = 0; r < R; r++){\n scanf(\"%d\", &order[r]);\n }\n\n dp[0][order[0]-1] = 0;\n for(int i = 1; i < R; i++){\n for(int j = 0; j < N; j++){\n for(int k = 0; k < N; k++){\n if(j == k){\n if(dp[i-1][j] != LLONG_MAX && L.get_value(order[i-1]-1, order[i]-1) != LLONG_MAX)dp[i][j] = min(dp[i][j], dp[i-1][j]+L.get_value(order[i-1]-1, order[i]-1));\n }\n else{\n if(L.get_value(order[i-1]-1, k) != LLONG_MAX && dp[i-1][k] != LLONG_MAX && S.get_value(k, j) != LLONG_MAX && L.get_value(j, order[i]-1) != LLONG_MAX) dp[i][j] = min(dp[i][j], L.get_value(order[i-1]-1, k) + dp[i-1][k] + S.get_value(k, j) + L.get_value(j, order[i]-1));\n }\n }\n }\n }\n\n ll minimum = LLONG_MAX;\n for(int i = 0; i < N; i++){\n minimum = min(minimum, dp[R-1][i]);\n }\n\n printf(\"%lld\\n\", minimum);\n }\n\n}", "accuracy": 1, "time_ms": 460, "memory_kb": 5668, "score_of_the_acc": -0.2766, "final_rank": 18 }, { "submission_id": "aoj_2200_9002173", "code_snippet": "#line 2 \"cp-library/src/cp-template.hpp\"\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nusing i32 = int;\nusing u32 = unsigned int;\nusing i64 = long long;\nusing u64 = unsigned long long;\nusing i128 = __int128_t;\ntemplate < class T > bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }\ntemplate < class T > bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }\ntemplate < class T, class U > T ceil (T x, U y) { return (x > 0 ? (x + y - 1) / y : x / y); }\ntemplate < class T, class U > T floor(T x, U y) { return (x > 0 ? x / y : (x - y + 1) / y); }\nint popcnt(i32 x) { return __builtin_popcount(x); }\nint popcnt(u32 x) { return __builtin_popcount(x); }\nint popcnt(i64 x) { return __builtin_popcountll(x); }\nint popcnt(u64 x) { return __builtin_popcountll(x); }\n\n#line 2 \"cp-library/src/utility/rep_itr.hpp\"\ntemplate < class T > struct itr_rep {\n T i, d;\n constexpr itr_rep(const T i) noexcept : i(i), d(1) {}\n constexpr itr_rep(const T i, const T d) noexcept : i(i), d(d) {}\n void operator++() noexcept { i += d; }\n constexpr int operator*() const noexcept { return i; }\n constexpr bool operator!=(const itr_rep x) const noexcept { return d > 0 ? i < x.i : i > x.i; }\n};\n\ntemplate < class T > struct rep {\n const itr_rep< T > s, t;\n constexpr rep(const T t) noexcept : s(0), t(t) {}\n constexpr rep(const T s, const T t) noexcept : s(s), t(t) {}\n constexpr rep(const T s, const T t, const T d) noexcept : s(s, d), t(t, d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n\ntemplate < class T > struct revrep {\n const itr_rep < T > s, t;\n constexpr revrep(const T t) noexcept : s(t - 1, -1), t(-1, -1) {}\n constexpr revrep(const T s, const T t) noexcept : s(t - 1, -1), t(s - 1, -1) {}\n constexpr revrep(const T s, const T t, const T d) noexcept : s(t - 1, -d), t(s - 1, -d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n#line 3 \"cp-library/src/utility/io.hpp\"\n\n/* 128bit integer */\nistream& operator>>(istream& is, i128& x) {\n std::string s; is >> s;\n int pm = (s[0] == '-');\n x = 0;\n for(int i : rep(pm, int(s.size()))) x = x * 10 + (s[i] - '0');\n if(pm) x *= -1;\n return is;\n}\nostream& operator<<(ostream& os, const i128& x) {\n if(x == 0) return os << '0';\n i128 y = x;\n if(y < 0) { os << '-'; y *= -1; }\n std::vector<int> ny;\n while(y > 0) { ny.push_back(y % 10); y /= 10; }\n for(int i : revrep(ny.size())) os << ny[i];\n return os;\n}\n\ntemplate < class S, class T > istream& operator>>(istream& is, std::pair< S, T >& x) { is >> x.first >> x.second; return is; }\ntemplate < class S, class T > ostream& operator<<(ostream& os, const std::pair< S, T >& x) { os << x.first << \" \" << x.second; return os; }\n\nnamespace scanner {\n struct sca {\n template < class T > operator T() {\n T s; std::cin >> s; return s;\n }\n };\n struct vec {\n int n;\n vec(int n) : n(n) {}\n template < class T > operator std::vector< T >() {\n std::vector< T > v(n);\n for(T& x : v) std::cin >> x;\n return v;\n }\n };\n struct mat {\n int h, w;\n mat(int h, int w) : h(h), w(w) {}\n template < class T > operator std::vector< std::vector< T > >() {\n std::vector m(h, std::vector< T >(w));\n for(std::vector< T >& v : m) for(T& x : v) std::cin >> x;\n return m;\n }\n };\n struct speedup {\n speedup() {\n std::cin.tie(0);\n std::ios::sync_with_stdio(0);\n }\n } speedup_instance;\n}\nscanner::sca in() { return scanner::sca(); }\nscanner::vec in(int n) { return scanner::vec(n); }\nscanner::mat in(int h, int w) { return scanner::mat(h, w); }\n\nnamespace printer {\n void precision(int d) { std::cout << std::fixed << std::setprecision(d); }\n void flush() { std::cout.flush(); }\n}\n\ntemplate < class T >\nostream& operator<<(ostream& os, const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) { os << a[i]; if(i != n - 1) os << ' '; }\n return os;\n}\n\nint print() { std::cout << '\\n'; return 0; }\ntemplate < class head, class... tail > int print(head&& h, tail&&... t) {\n std::cout << h; if(sizeof...(tail)) std::cout << ' ';\n return print(std::forward<tail>(t)...);\n}\ntemplate < class T > int print_n(const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) std::cout << a[i] << \"\\n\";\n return 0;\n}\n\n\n#line 2 \"cp-library/src/utility/key_val.hpp\"\n\ntemplate < class K, class V >\nstruct key_val {\n K key; V val;\n key_val() {}\n key_val(K key, V val) : key(key), val(val) {}\n template < std::size_t Index >\n std::tuple_element_t< Index, key_val >& get() {\n if constexpr (Index == 0) return key;\n if constexpr (Index == 1) return val;\n }\n};\n\nnamespace std {\n\ntemplate < class K, class V > struct tuple_size < key_val< K, V > > : integral_constant< size_t, 2 > {};\ntemplate < class K, class V > struct tuple_element < 0, key_val< K, V > > { using type = K; };\ntemplate < class K, class V > struct tuple_element < 1, key_val< K, V > > { using type = V; };\n\n}\n#line 2 \"cp-library/src/utility/vec_op.hpp\"\ntemplate < class T > key_val< int, T > max_of(const vector< T >& a) {\n int i = std::max_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class T > key_val< int, T > min_of(const vector< T >& a) {\n int i = std::min_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class S, class T > S sum_of(const vector< T >& a) {\n S sum = 0;\n for(const T x : a) sum += x;\n return sum;\n}\ntemplate < class S, class T > vector< S > freq_of(const vector< T >& a, T L, T R) {\n vector< S > res(R - L, S(0));\n for(const T x : a) res[x - L] += 1;\n return res;\n}\ntemplate < class S, class T > struct prefix_sum {\n vector< S > s;\n prefix_sum(const vector< T >& a) : s(a) {\n s.insert(s.begin(), S(0));\n for(int i : rep(a.size())) s[i + 1] += s[i];\n }\n // [L, R)\n S sum(int L, int R) { return s[R] - s[L]; }\n};\n#line 3 \"cp-library/src/utility/heap.hpp\"\n\ntemplate < class T > using heap_min = std::priority_queue< T, std::vector< T >, std::greater< T > >;\ntemplate < class T > using heap_max = std::priority_queue< T, std::vector< T >, std::less< T > >;\n\n#line 27 \"cp-library/src/cp-template.hpp\"\n\n#line 1 \"cp-library/src/algorithm/bin_search.hpp\"\ntemplate < class T, class F >\nT bin_search(T ok, T ng, F f) {\n while(abs(ng - ok) > 1) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n\ntemplate < class T, class F >\nT bin_search_real(T ok, T ng, F f, int step = 80) {\n while(step--) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n#line 2 \"cp-library/src/algorithm/argsort.hpp\"\n\ntemplate < class T > std::vector< int > argsort(const std::vector< T > &a) {\n std::vector< int > ids((int)a.size());\n std::iota(ids.begin(), ids.end(), 0);\n std::sort(ids.begin(), ids.end(), [&](int i, int j) {\n return a[i] < a[j] || (a[i] == a[j] && i < j);\n });\n return ids;\n}\n#line 1 \"macro.hpp\"\nnamespace macro {\n\nusing size_type = int;\ntemplate < class container > void sort(container& a) { std::sort(std:: begin(a), std:: end(a)); }\ntemplate < class container > void rsort(container& a) { std::sort(std::rbegin(a), std::rend(a)); }\ntemplate < class container > void reverse(container& a) { std::reverse(std::begin(a), std::end(a)); }\ntemplate < class container > void unique(container& a) {\n std::sort(std::begin(a), std::end(a));\n a.erase(std::unique(std::begin(a), std::end(a)), std::end(a));\n}\ntemplate < class container > container sorted(const container& a) { container b = a; sort(b); return std::move(b); }\ntemplate < class container > container rsorted(const container& a) { container b = a; rsort(b); return std::move(b); }\ntemplate < class container, class compare > void sort(container& a, const compare& cmp) { std::sort(std::begin(a), std::end(a), cmp); }\ntemplate < class container, class compare > container sorted(const container& a, const compare& cmp) { container b = a; sort(b, cmp); return std::move(b); }\ntemplate < class container, class value > size_type lower_bound(const container& a, const value& x) { return std::lower_bound(std::begin(a), std::end(a), x) - std::begin(a); }\ntemplate < class container, class value > size_type upper_bound(const container& a, const value& x) { return std::upper_bound(std::begin(a), std::end(a), x) - std::begin(a); }\n\nconst std::vector<std::pair<size_type, size_type>> dir4 = { {+1, 0}, {-1, 0}, { 0, +1}, { 0, -1} };\nconst std::vector<std::pair<size_type, size_type>> dir8 = { {-1, -1}, {-1, 0}, {-1, +1}, { 0, -1}, { 0, +1}, {+1, -1}, {+1, 0}, {+1, +1} };\n\n#ifdef _DEBUG\n#define debug(x) std::cout << \"[\" << __LINE__ << \"] \" << #x << \": \" << x << std::endl\n#else\n#define debug(x)\n#endif\n\ntemplate < class container > void concat(container& a, const container& b) {\n a.insert(std::end(a), std::begin(b), std::end(b));\n}\nstd::vector<size_type> iota(const size_type n) {\n std::vector<size_type> I(n);\n std::iota(std::begin(I), std::end(I), 0);\n return I;\n}\ntemplate < class container > std::vector<size_type> sort_idx(const container& a) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return a[i] < a[j] or (a[i] == a[j] and i < j); });\n return I;\n}\ntemplate < class container, class compare > std::vector<size_type> sort_idx(const container& a, const compare& cmp) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return cmp(a[i], a[j]) or (a[i] == a[j] and i < j); });\n return std::move(I);\n}\n\nstruct grid {\n using size_type = int;\n size_type H, W;\n grid(const size_type H, const size_type W) : H(H), W(W) {}\n bool contains(const size_type i, const size_type j) {\n return 0 <= i and i < H and 0 <= j and j < W;\n }\n};\n\nusing f64 = long double;\n\n} // namespace macro\n\nusing namespace macro;\n#line 3 \"A.cpp\"\n\nint solve(int N, int M) {\n const int INF = 1e9;\n vector A(N, vector(N, INF)), B(N, vector(N, INF));\n for(int i : rep(M)) {\n int x = in(), y = in(), t = in(); char s = in(); x--, y--;\n if(s == 'L') {\n chmin(A[x][y], t);\n chmin(A[y][x], t);\n } else {\n chmin(B[x][y], t);\n chmin(B[y][x], t);\n }\n }\n\n for(int i : rep(N)) A[i][i] = 0;\n for(int i : rep(N)) B[i][i] = 0;\n for(int k : rep(N)) for(int i : rep(N)) for(int j : rep(N)) chmin(A[i][j], A[i][k] + A[k][j]);\n for(int k : rep(N)) for(int i : rep(N)) for(int j : rep(N)) chmin(B[i][j], B[i][k] + B[k][j]);\n\n int R = in();\n vector<int> z = in(R);\n for(int& i : z) i--;\n\n vector dp(N, INF);\n dp[z[0]] = 0;\n for(int i : rep(1, R)) {\n vector nt(N, INF);\n for(int v : rep(N)) if(dp[v] < INF) {\n if(A[z[i - 1]][z[i]] < INF)\n chmin(nt[v], dp[v] + A[z[i - 1]][z[i]]);\n if(A[z[i - 1]][v] < INF and B[v][z[i]] < INF)\n chmin(nt[z[i]], dp[v] + A[z[i - 1]][v] + B[v][z[i]]);\n for(int x : rep(N)) {\n if(A[z[i - 1]][v] < INF and B[v][x] < INF and A[x][z[i]] < INF)\n chmin(nt[x], dp[v] + A[z[i - 1]][v] + B[v][x] + A[x][z[i]]);\n }\n }\n dp = move(nt);\n }\n return min_of<int>(dp).val;\n}\n\nint main() {\n while(true) {\n int N = in(), M = in();\n if(make_pair(N, M) == make_pair(0, 0)) return 0;\n print(solve(N, M));\n }\n}", "accuracy": 1, "time_ms": 280, "memory_kb": 3508, "score_of_the_acc": -0.0536, "final_rank": 3 } ]
aoj_2199_cpp
Problem C: Differential Pulse Code Modulation 差分パルス符号変調は主に音声信号を圧縮する際に用いられる圧縮手法の一つである. 音声信号は計算機上では整数列(インパルス列)として扱われる.整数列は入力信号を一定時間間隔で標本化(サンプリング)し,振幅を記録したものである.一般にこの整数列は前後の値が近いという傾向がある.これを利用し,前後の値の差分を符号化し,圧縮率を向上させるのが差分パルス符号変調である. 本問題では差分の値をあらかじめ定められた値の集合から選ぶことを考える.この値の集合をコードブックと呼ぶことにする.復号化後の音声信号 y n は以下の式で定められる. y n = y n - 1 + C [ k n ] ここで k n はプログラムによって出力される出力系列, C [ j ] はコードブックの j 番目の値である.ただし y n は加算によって0未満の値となった場合は0に,255より大きい値となった場合は255にそれぞれ丸められる.また, y 0 の値は128とする. あなたの仕事は,入力信号とコードブックが与えられたときに,元の入力信号と復号化後の出力信号との差の二乗和が最小となるように出力系列を選んで,そのときの差の二乗和を出力するプログラムを書くことである. 例えば,コードブックとして {4, 2, 1, 0, -1, -2, -4} という値のセットを使って 131, 137 という列を圧縮する場合, y 0 = 128 , y 1 = 128 + 4 = 132 , y 2 = 132 + 4 = 136 という列に圧縮すると 二乗和が (131 - 132)^2 + (137 - 136)^2 = 2 と最小になる. また,同じくコードブックとして {4, 2, 1, 0, -1, -2, -4} という値のセットを使って 131, 123 という列を圧縮する場合, y 0 = 128 , y 1 = 128 + 1 = 129 , y 2 = 129 - 4 = 125 と,先程の例とは違って 131 により近づく +2 を採用しない方が (131 - 129) ^ 2 + (123 - 125) ^ 2 = 8 というより小さな二乗和が得られる. 上記 2つの例は sample input の最初の 2例である. Input 入力は複数のデータセットから構成される.各データセットの形式は次に示すとおりである. N M C 1 C 2 ... C M x 1 x 2 ... x N 最初の行は,入力データセットの大きさを規定する. N は圧縮する入力信号の長さ(サンプル数)である. M はコードブックに含まれる値の個数である. N 及び M は1 ≤ N ≤ 20000,1 ≤ M ≤ 16を満たす. これに続く M 行は,コードブックの記述である. C i はコードブックに含まれる i 番目の値を表す. C i は-255 ≤ C i ≤ 255を満たす. これに続く N 行は,入力信号の記述である. x i は入力信号を表す整数列の i 番目の値である. x i は0 ≤ x i ≤ 255を満たす. データセットの中の入力項目は,すべて整数である.入力の終りは,空白文字1個で区切られた2個のゼロのみからなる行で表される. Output 入力の各データセットに対して, 元の入力信号と復号化後の出力信号との差の二乗和の最小値を一行で出力せよ. Sample Input 2 7 4 2 1 0 -1 -2 -4 131 137 2 7 4 2 1 0 -1 -2 -4 131 123 10 7 -4 -2 -1 0 1 2 4 132 134 135 134 132 128 124 122 121 122 5 1 255 0 0 0 0 0 4 1 0 255 0 255 0 0 0 Output for the Sample Input 2 8 0 325125 65026
[ { "submission_id": "aoj_2199_11034201", "code_snippet": "#include <iostream>\n#include <vector>\n#include <cmath>\n#include <climits>\n\nusing namespace std;\n\nint main(){\n\n while(1){\n \n int N, M;\n cin >> N >> M;\n if (N==0 && M==0) break;\n\n vector<int> C(M);\n vector<int> X(N);\n\n for (int i=0; i<M; i++) cin >> C[i];\n for (int i=0; i<N; i++) cin >> X[i];\n\n vector<vector<int>> dp(N+1, vector<int>(256));\n for (int i=0; i<=N; i++) {\n for (int j=0; j<256; j++){\n dp[i][j] = INT_MAX;\n }\n }\n dp[0][128] = 0;\n\n for (int i=0; i<N; i++){\n for (int j=0; j<256; j++){\n if (dp[i][j] == INT_MAX) continue;\n for (int k=0; k<M; k++){\n int yn = j + C[k];\n yn = max(yn, 0);\n yn = min(yn, 255);\n int error = pow(X[i]-yn, 2);\n dp[i+1][yn] = min(dp[i+1][yn], dp[i][j]+error);\n }\n }\n }\n\n int ans = INT_MAX;\n for (int i=0; i<256; i++){\n ans = min(ans, dp[N][i]);\n }\n\n cout << ans << endl;\n\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 510, "memory_kb": 23992, "score_of_the_acc": -0.7855, "final_rank": 11 }, { "submission_id": "aoj_2199_10937400", "code_snippet": "#include <stdio.h>\n#include <cmath>\n#include <algorithm>\n#include <stack>\n#include <queue>\n#include <vector>\ntypedef long long int ll;\n\n#define BIG_NUM 2000000000\n\nusing namespace std;\n\nint N,M;\nint** dp;\n\nvoid func(){\n\tint codeTable[M];\n\n\tfor(int i = 0; i < M; i++){\n\t\tscanf(\"%d\",&codeTable[i]);\n\t}\n\n\tint num;\n\tdp[0][128] = 0;\n\n\tfor(int count = 1; count <= N; count++){\n\t\tscanf(\"%d\",&num);\n\n\t\tfor(int i = 0; i <= 255; i++){\n\t\t\tif(dp[count-1][i] != BIG_NUM){\n\t\t\t\tfor(int k = 0; k < M; k++){\n\t\t\t\t\tif(i+codeTable[k] > 255){\n\t\t\t\t\t\tdp[count][255] = min(dp[count][255],dp[count-1][i]+(num-255)*(num-255));\n\t\t\t\t\t}else if(i+codeTable[k] < 0){\n\t\t\t\t\t\tdp[count][0] = min(dp[count][0],dp[count-1][i]+(num)*(num));\n\t\t\t\t\t}else{\n\t\t\t\t\t\tdp[count][i+codeTable[k]] = min(dp[count][i+codeTable[k]],dp[count-1][i] + (num-(i+codeTable[k]))*(num-(i+codeTable[k])));\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tint minimum = BIG_NUM;\n\n\tfor(int i = 0; i <= 255; i++){\n\t\tminimum = min(minimum,dp[N][i]);\n\t}\n\n\tprintf(\"%d\\n\",minimum);\n\n}\n\nint main(){\n\n\tdp = new int*[20001];\n\tfor(int i = 0; i <= 20000; i++){\n\t\tdp[i] = new int[256];\n\t}\n\n\twhile(true){\n\t\tscanf(\"%d %d\",&N,&M);\n\t\tif(N == 0 && M == 0)break;\n\n\t\tfor(int i = 0; i <= N; i++){\n\t\t\tfor(int k = 0; k <= 255; k++)dp[i][k] = BIG_NUM;\n\t\t}\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 350, "memory_kb": 23204, "score_of_the_acc": -0.2758, "final_rank": 2 }, { "submission_id": "aoj_2199_10912987", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n// #include <atcoder/all>\n// using namespace atcoder;\nusing ll = long long;\nusing ld = long double;\nusing P = pair<ll,ll>;\nusing Board = vector<string>;\n// using mint = modint998244353;\nusing vl = vector<ll>;\nusing vvl = vector<vector<ll>>;\nusing vp = vector<P>;\nusing vvp = vector<vector<P>>;\nusing vb = vector<bool>;\nusing vvb = vector<vector<bool>>;\nusing vs = vector<string>;\nusing sl = set<ll>;\nusing Graph = vector<vector<ll>>;\nusing pq = priority_queue<ll>;\nusing pqg = priority_queue<ll, vector<ll>, greater<ll>>;\n#define rep(i, n) for (ll i = 0; i < (n); i++)\n#define nall(a) a.begin(), a.end()\nconst ll INF = 1LL << 60;\n\nint main(){\n ll n, m;\n while (cin>>n>>m, n+m) {\n vl code(m), palce(n);\n rep(i,m) cin >> code[i];\n rep(i,n) cin >> palce[i];\n vvl dp(n+1, vl(256, INF));\n dp[0][128] = 0;\n rep(i,n)rep(j,256) {\n if (dp[i][j] == INF) continue;\n rep(k,m) {\n ll y = max(0LL, min(255LL, j+code[k]));\n dp[i+1][y] = min(dp[i+1][y], dp[i][j]+(ll)pow(y-palce[i],2));\n }\n }\n ll ans = INF;\n rep(i,256) ans = min(ans, dp[n][i]);\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 580, "memory_kb": 43800, "score_of_the_acc": -1.2473, "final_rank": 18 }, { "submission_id": "aoj_2199_10865272", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n int N, M;\n int C[29], X[20009];\n long long INF = 10000000000000LL;\n\n while (cin >> N >> M) {\n if (N == 0 && M == 0) break;\n\n for (int i = 1; i <= M; i++) cin >> C[i];\n for (int i = 1; i <= N; i++) cin >> X[i];\n\n // dp[v][i] : i 個目まで見て y_i = v の最小コスト\n vector<vector<long long>> dp(256, vector<long long>(N + 1, INF));\n\n // y0 = 128 固定\n dp[128][0] = 0;\n\n for (int i = 1; i <= N; i++) {\n for (int p = 0; p <= 255; p++) {\n if (dp[p][i - 1] == INF) continue;\n for (int k = 1; k <= M; k++) {\n int t = p + C[k];\n int nxt;\n if (t < 0) nxt = 0;\n else if (t > 255) nxt = 255;\n else nxt = t;\n long long cost = dp[p][i - 1] + 1LL * (X[i] - nxt) * (X[i] - nxt);\n if (cost < dp[nxt][i]) dp[nxt][i] = cost;\n }\n }\n }\n\n long long ans = INF;\n for (int v = 0; v <= 255; v++) ans = min(ans, dp[v][N]);\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 660, "memory_kb": 44140, "score_of_the_acc": -1.5014, "final_rank": 19 }, { "submission_id": "aoj_2199_10864713", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i, a, n) for (int i = a; i < n; i++)\nusing ll = long long;\nusing ld = long double;\nusing pl = pair<ll, ll>;\nusing vl = vector<ll>;\nusing vvl = vector<vl>;\nconst int mod = 998244353;\nconst ll hashh = 2147483647;\n\nint main()\n{\n while (1)\n {\n ll n, m;\n cin >> n >> m;\n if (n == 0 && m == 0)\n break;\n vl c(m), x(n);\n rep(i, 0, m) cin >> c[i];\n rep(i, 0, n) cin >> x[i];\n vvl a(n, vl(300, 1000000000));\n rep(i, 0, m)\n {\n if (128 + c[i] < 0)\n {\n a[0][0] = min(a[0][0], x[0] * x[0]);\n }\n else if (128 + c[i] > 255)\n {\n a[0][255] = min(a[0][255], (x[0] - 255) * (x[0] - 255));\n }\n else\n {\n a[0][128 + c[i]] = min(a[0][128 + c[i]], (x[0] - (128 + c[i])) * (x[0] - (128 + c[i])));\n }\n }\n rep(i, 1, n)\n {\n rep(j, 0, 256)\n {\n rep(k, 0, m)\n {\n if (j + c[k] < 0)\n {\n a[i][0] = min(a[i][0], x[i] * x[i] + a[i - 1][j]);\n }\n else if (j + c[k] > 255)\n {\n a[i][255] = min(a[i][255], (x[i] - 255) * (x[i] - 255) + a[i - 1][j]);\n }\n else\n {\n a[i][j + c[k]] = min(a[i][j + c[k]], (x[i] - (j + c[k])) * (x[i] - (j + c[k])) + a[i - 1][j]);\n }\n }\n }\n }\n ll ans = 10000000000;\n rep(i, 0, 256)\n {\n ans = min(ans, a[n - 1][i]);\n }\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 460, "memory_kb": 50816, "score_of_the_acc": -0.9583, "final_rank": 13 }, { "submission_id": "aoj_2199_10854604", "code_snippet": "#include <bits/stdc++.h>\n#define ll long long\n#define ld long double\nusing namespace std;\n\nbool solve(){\n int n,m;\n cin >> n >> m;\n \n if(n == 0 && m == 0) return false;\n \n vector<vector<int>> dp(n+1,vector<int>(256,2e9));\n \n dp[0][128] = 0;\n \n vector<int> c(m);\n for(auto& i:c) cin >> i;\n \n int x;\n \n for(int i = 1;i <= n;i++){\n cin >> x;\n for(int j = 0;j < 256;j++){\n for(int k = 0;k < m;k++){\n int ind = max(0,min(j+c[k],255));\n \n dp[i][ind] = min(dp[i][ind],dp[i-1][j]+(x-ind)*(x-ind));\n }\n }\n }\n \n int ans = 2e9;\n \n for(int i = 0;i < 256;i++){\n ans = min(ans,dp[n][i]);\n }\n \n cout << ans << endl;\n \n return true;\n}\n\nint main(void){\n \n while(solve()) continue;\n \n}", "accuracy": 1, "time_ms": 420, "memory_kb": 23864, "score_of_the_acc": -0.5027, "final_rank": 6 }, { "submission_id": "aoj_2199_10854037", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll=long long;\n\nconst ll INF=1e15;\n\nvector<vector<ll>> dp(20001, vector<ll>(256,INF));\n\nint main() {\n int N, M;\n while(cin >> N >> M && N!=0 && M!=0){\n vector<ll> C(M), x(N);\n for(int i=0; i<M; i++){\n cin >> C[i];\n }\n for(int i=0; i<N; i++){\n cin >> x[i];\n }\n\n dp=vector<vector<ll>>(20001, vector<ll>(256,INF));\n dp[0][128]=0;\n for(int i=0; i<N; i++){\n for(int j=0; j<=255; j++){\n if(dp[i][j]!=INF){\n for(int k=0; k<M; k++){\n ll num=j+C[k];\n if(num<=0) num=0;\n if(num>=255) num=255;\n dp[i+1][num]=min(dp[i+1][num],dp[i][j]+(x[i]-num)*(x[i]-num));\n }\n }\n }\n }\n\n ll ans=INF;\n for(int i=0; i<256; i++){\n ans=min(ans,dp[N][i]);\n }\n cout << ans << '\\n';\n }\n}", "accuracy": 1, "time_ms": 530, "memory_kb": 84776, "score_of_the_acc": -1.5938, "final_rank": 20 }, { "submission_id": "aoj_2199_10853416", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\nint main() {\n vector<int> res_list;\n while (true) {\n int N, M; cin >> N >> M;\n if (N == 0 && M == 0) break;\n vector<int> C(M + 1);\n for (int i = 1; i <= M; ++i) cin >> C[i];\n vector<int> X(N + 1);\n for (int i= 1; i <= N; ++i) cin >> X[i];\n\n vector<int> dp(256, INT_MAX);\n dp[128] = 0;\n for (int i = 1; i <= N; ++i) {\n vector<int> nextDp(256, INT_MAX);\n for (int y_prev = 0; y_prev < 256; ++y_prev) {\n if (dp[y_prev] == INT_MAX) continue;\n for (int j = 1; j <= M; ++j) {\n int c = C[j];\n int y = y_prev + c;\n y = min(y, 255);\n y = max(y, 0);\n int diff = X[i] - y;\n nextDp[y] = min(nextDp[y], dp[y_prev] + diff * diff);\n }\n }\n dp = nextDp;\n }\n auto res = min_element(dp.begin(), dp.end());\n res_list.push_back(*res);\n }\n for (int res: res_list) cout << res << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 340, "memory_kb": 3272, "score_of_the_acc": 0, "final_rank": 1 }, { "submission_id": "aoj_2199_10853414", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\nint main() {\n vector<int> res_list;\n while (true) {\n int N, M; cin >> N >> M;\n if (N == 0 && M == 0) break;\n vector<int> C(M + 1);\n for (int i = 1; i <= M; ++i) cin >> C[i];\n vector<int> X(N + 1);\n for (int i= 1; i <= N; ++i) cin >> X[i];\n\n vector<int> dp(256, INT_MAX);\n dp[128] = 0;\n for (int i = 1; i <= N; ++i) {\n vector<int> nextDp(256, INT_MAX);\n for (int y_prev = 0; y_prev < 256; ++y_prev) {\n if (dp[y_prev] == INT_MAX) continue;\n for (int j = 1; j <= M; ++j) {\n int c = C[j];\n int y = y_prev + c;\n y = min(y, 255);\n y = max(y, 0);\n nextDp[y] = min(nextDp[y], dp[y_prev] + (int)pow(X[i] - y, 2));\n }\n }\n dp = nextDp;\n }\n auto res = min_element(dp.begin(), dp.end());\n res_list.push_back(*res);\n }\n for (int res: res_list) cout << res << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 470, "memory_kb": 3404, "score_of_the_acc": -0.4079, "final_rank": 5 }, { "submission_id": "aoj_2199_10850085", "code_snippet": "/*\nAOJ Problem C: Differential Pulse Code Modulation\n問題文の説明がわかりにく過ぎる\n<問題の意味>\n条件:\n コードブック = M個の整数の集合\n y0 = 128(固定値)\n yn = y_n-1 + (コードブックから選んだ任意の1つの値)\n xn = 入力信号(与えられる)\n -> Σ(xi-yi)^2が最小になるようにコードブックを選ぶ\n例:\nコードブック = {4, 2, 1, 0, -1, -2, -4} \n入力: x1=131,x2=137のとき\n出力: y0=128(固定)\n y1 = y0 + (コードブックから選んだ任意の1つの値)\n = 128 + 4\n = 132\n y2 = y1 + 4\n = 132 + 4\n = 136\nとすると、二乗和 = (x1-y1)^2 + (x2-y2)^2は最小になる\n\n別には、例えば以下のようなコードブックの選び方ができる\n y1=128+(-4)=124, y2=124+2=126\n -> 二乗和 = (x1-y1)^2 + (x2-y2)^2が上記の二乗和より大きくなるのでNG\n \n===================================================-\n方針1: DP(コードブック) -> WA\nコードブックの選び方をDPで全探索すればいけそう\n-----------\nWAの理由:\nある出力値からの遷移で、 出力値y:127,平方和:113 出力値y:128,平方和:113 の2つのパターンがあったときに先に出てきた、「出力値y:127,平方和:113」だけ残して、「出力値y:128,平方和:113」を捨ててしまうことがある → 最適な遷移が「出力値y:128,平方和:113」のときに間違いになってしまう\n(例)\nN=3\nコードブック C={1,2,0,−2}\n入力信号X=(137,120,134)\n-----------\n\ndp[i][j] = y0,...,yi-1を選んで、yi = yi-1 + コードブックのj番目の値としての二乗和が最小になるとき、[yiの値、二乗和]\n遷移: dp[i][j] -> dp[i+1][k] \n i:コードブックjを選んだときの最小二乗和になる選び方 -> i+1:コードブックkを選んだときの最小二乗和になる選び方\n\ndpテーブル\n C[0] C[1] ... C[M-1] (C[i]:コードブックのi番目の値)\n--------------------------\ny0\ny1\n...\nyn\n計算量: N*(M*M) <= 20000*16*16 = 8.2×10^7程度\n===================================================-\n方針2: DP(出力値) -> AC\n\ndp[i][y] = 出力値をi-1個(y0,...,yi-1)を選んで、i番目の出力値としてyを選んだときの平方和の最小値\n遷移: dp[i][y] -> M通り(コードブックの個数)\ndpテーブル\n 0 1 2 ... 256\n--------------------------\ny0\ny1\n...\nyn\n\n計算量: N*(256*M) <= 20000*256*M = 程度\n\nPythonでTLEなのでC++に変換\n*/\n\n#include <bits/stdc++.h>\nusing namespace std;\n\nconst int MAX_Y = 255;\nconst int INIT_Y = 128;\nconst double INF = 1e18;\n\nvoid calc_minsum(int N, int M) {\n vector<int> codebook(M);\n for (int i = 0; i < M; i++) cin >> codebook[i];\n vector<int> input_signals(N);\n for (int i = 0; i < N; i++) cin >> input_signals[i];\n\n vector<vector<double>> dp(N+1, vector<double>(MAX_Y+1, INF));\n dp[0][INIT_Y] = 0;\n\n for (int i = 0; i < N; i++) {\n int x = input_signals[i];\n for (int y = 0; y <= MAX_Y; y++) {\n if (dp[i][y] == INF) continue;\n for (int c : codebook) {\n int next_y = y + c;\n if (next_y < 0) next_y = 0;\n else if (next_y > MAX_Y) next_y = MAX_Y;\n double next_sum = dp[i][y] + (x - next_y) * (x - next_y);\n dp[i+1][next_y] = min(dp[i+1][next_y], next_sum);\n }\n }\n }\n\n double ans = INF;\n for (int y = 0; y <= MAX_Y; y++) {\n ans = min(ans, dp[N][y]);\n }\n cout << (long long)ans << endl;\n}\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n\n while (true) {\n int N, M;\n cin >> N >> M;\n if (N == 0 && M == 0) break;\n calc_minsum(N, M);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 390, "memory_kb": 44288, "score_of_the_acc": -0.6595, "final_rank": 9 }, { "submission_id": "aoj_2199_10848486", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n int N, M;\n while(cin >> N >> M, N) {\n vector<int> C(M);\n for(int i = 0; i < M; ++i) {\n cin >> C[i];\n }\n vector<int> x(N);\n for(int i = 0; i < N; ++i) {\n cin >> x[i];\n }\n vector<vector<int>> dp(N+1, vector<int>(256, 1e9));\n dp[0][128] = 0;\n for(int i = 0; i < N; ++i) {\n for(int j = 0; j < 256; ++j) {\n for(int k = 0; k < M; ++k) {\n int next = min(255, max(0, j + C[k]));\n dp[i+1][next] = min(dp[i+1][next], dp[i][j] + (next - x[i]) * (next - x[i]));\n }\n }\n }\n cout << *min_element(dp[N].begin(), dp[N].end()) << endl;\n }\n}", "accuracy": 1, "time_ms": 380, "memory_kb": 23908, "score_of_the_acc": -0.3782, "final_rank": 4 }, { "submission_id": "aoj_2199_10810452", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nconst int inf = (1<<30);\nint dp[20009][256];\n\nint main() {\n //cout << 1 << endl;\n while(true){\n int N, M;\n cin >> N >> M;\n if(N == 0 && M == 0){\n break;\n }\n\n vector<int> cb(M), in(N);\n for(int i = 0; i < M; i++){\n cin >> cb[i];\n }\n for(int i = 0; i < N; i++){\n cin >> in[i];\n }\n //cout << 1 << endl;\n\n for(int i = 0; i < N+1; i++){\n for(int j = 0; j < 256; j++){\n dp[i][j] = inf;\n }\n }\n dp[0][128] = 0;\n for(int i = 0; i < N; i++){\n for(int j = 0; j < 256; j++){\n if(dp[i][j] == inf) continue;\n\n for(int k = 0; k < M; k++){\n int num = j + cb[k];\n if(num < 0) num = 0;\n if(num > 255) num = 255;\n int gosa = abs(in[i] - num);\n dp[i+1][num] = min(dp[i+1][num], dp[i][j] + gosa * gosa);\n }\n }\n }\n\n int ans = inf;\n for(int i = 0; i < 256; i++){\n ans = min(dp[N][i], ans);\n }\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 380, "memory_kb": 23520, "score_of_the_acc": -0.3734, "final_rank": 3 }, { "submission_id": "aoj_2199_10806266", "code_snippet": "#include <iostream>\n#include <vector>\n#include <set>\n#include <algorithm>\n#include <queue>\n#include <cmath>\nusing namespace std;\n\nconst int START = 128;\nconst int LIMIT = 255;\nconst int INF = (1LL<<31) - 1;\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n\n while (true) {\n int N, M;\n cin >> N >> M;\n if (!N && !M) break;\n\n vector<int> code(M);\n for (int i = 0; i < M; ++i) cin >> code[i];\n\n vector<int> signal(N);\n for (int i = 0; i < N; ++i) cin >> signal[i];\n\n vector<vector<int>> memo(N+1, vector<int>(LIMIT+1, INF));\n \n for (int i = 0; i <= N; ++i) {\n int sig_idx = i-1;\n if (i == 0) {\n memo[0][START] = 0;\n }\n else {\n for (int j = 0; j <= LIMIT; ++j) {\n for (int k = 0; k < M; ++k) {\n if (memo[i-1][j] != INF) {\n int nxt = j + code[k];\n if (nxt <= 0) nxt = 0;\n if (LIMIT <= nxt) nxt = LIMIT;\n int add = (signal[sig_idx]-nxt)*(signal[sig_idx]-nxt);\n memo[i][nxt] = min(memo[i][nxt], memo[i-1][j] + add);\n }\n }\n }\n }\n }\n int result = INF;\n for (int i = 0; i <= LIMIT; ++i) {\n result = min(result, memo[N][i]);\n }\n cout << result << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 510, "memory_kb": 23992, "score_of_the_acc": -0.7855, "final_rank": 11 }, { "submission_id": "aoj_2199_10747162", "code_snippet": "#include <iostream>\n#include <vector>\n#include <iomanip>\n#include <algorithm>\n#include <tuple>\n#include <set>\n#include <map>\n#include <bitset>\n#include <cmath>\n#include <deque>\n#include <queue>\n#include <stack>\n#include <cassert>\n#include <unordered_set>\n#include <unordered_map>\n#include <cmath>\nusing namespace std;\ntypedef long long ll;\n#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)\n#define all(v) v.begin(), v.end()\n\nll llpow(ll a, ll t)\n{\n ll res = 1;\n rep(i, t) res *= a;\n return res;\n}\n\nll inf = std::numeric_limits<ll>::max();\n\nint main()\n{\n vector<vector<ll>> css;\n vector<vector<ll>> xss;\n while (true)\n {\n ll n, m;\n cin >> n >> m;\n if (!n && !m)\n {\n break;\n }\n vector<ll> cs(m);\n rep(i, m)\n {\n cin >> cs[i];\n }\n vector<ll> xs(n);\n rep(i, n)\n {\n cin >> xs[i];\n }\n css.push_back(cs);\n xss.push_back(xs);\n }\n vector<ll> ans;\n rep(i, css.size())\n {\n vector<ll> cs = css[i];\n vector<ll> xs = xss[i];\n ll n = xs.size();\n ll m = cs.size();\n vector<vector<ll>> dp(n, vector<ll>(256, inf));\n rep(i, n)\n {\n rep(j, 256)\n {\n rep(k, m)\n {\n if (!i)\n {\n if (128 + cs[k] == j)\n {\n dp[i][j] = min(dp[i][j], llpow(xs[i] - j, 2));\n continue;\n }\n if (j == 0 && 128 + cs[k] < 0)\n {\n dp[i][j] = min(dp[i][j], llpow(xs[i] - j, 2));\n continue;\n }\n if (j == 255 && 128 + cs[k] > 255)\n {\n dp[i][j] = min(dp[i][j], llpow(xs[i] - j, 2));\n continue;\n }\n continue;\n }\n if (j + cs[k] >= 0 && j + cs[k] <= 255 && dp[i - 1][j] != inf)\n {\n dp[i][j + cs[k]] = min(dp[i][j + cs[k]], dp[i - 1][j] + llpow(xs[i] - (j + cs[k]), 2));\n continue;\n }\n if (j + cs[k] < 0 && dp[i - 1][j] != inf)\n {\n dp[i][0] = min(dp[i][0], dp[i - 1][j] + llpow(xs[i], 2));\n continue;\n }\n if (j + cs[k] > 255 && dp[i - 1][j] != inf)\n {\n dp[i][255] = min(dp[i][255], dp[i - 1][j] + llpow(xs[i] - 255, 2));\n continue;\n }\n }\n }\n }\n ll mn = inf;\n rep(i, 256)\n {\n mn = min(mn, dp[n - 1][i]);\n }\n ans.push_back(mn);\n }\n rep(i, ans.size())\n {\n cout << ans[i] << endl;\n }\n}", "accuracy": 1, "time_ms": 540, "memory_kb": 44832, "score_of_the_acc": -1.1349, "final_rank": 16 }, { "submission_id": "aoj_2199_10703395", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing pll = pair<ll, ll>;\nconst ll INF = LLONG_MAX / 4;\nconst ll mod1 = 1000000007;\nconst ll mod9 = 998244353;\nconst ll Hash = 10000000000000061;\n#define all(a) (a).begin(),(a).end()\n#define rep(i, n) for (ll i = 0; (i) < (n); ++(i))\nll dx[8] = {1, 1, 0, -1, -1, -1, 0, 1};\nll dy[8] = {0, -1, -1, -1, 0, 1, 1, 1};\ntemplate <typename T>\nbool chmax(T &a, const T& b) {\n if (a < b) { a = b; return true; }\n return false;\n}\ntemplate <typename T>\nbool chmin(T &a, const T& b) {\n if (a > b) { a = b; return true; }\n return false;\n}\n\n#define EPS (1e-10)\n#define equals(a, b) (fabs((a) - (b)) < EPS)\nclass Point {\npublic:\n ld x;\n ld y;\n Point(ld _x = 0, ld _y = 0) : x(_x), y(_y) {}\n Point operator+(Point p) { return Point(x + p.x, y + p.y); }\n Point operator-(Point p) { return Point(x - p.x, y - p.y); }\n Point operator*(ld a) { return Point(a * x, a * y); }\n Point operator/(ld a) { return Point(x / a, y / a); }\n\n ld abs() { return sqrt(norm()); }\nld norm() { return x * x + y * y; }\n\n bool operator<(const Point& p) const {\n return !equals(x, p.x) ? x < p.x : y < p.y;\n }\n bool operator==(const Point& p) const {\n return fabs(x - p.x) < (1e-10) && fabs(y - p.y) < (1e-10);\n }\n};\ntypedef Point Vector;\nstruct Segment {\n Point A;\n Point B;\n};\ntypedef Segment Line;\nclass Circle {\npublic:\n Point C;\n ld r;\n Circle(Point C = Point(), ld r = 0.0) : C(C), r(r) {}\n};\ntypedef vector<Point> Polygon;\n\nint solve(){\n ll N, M; cin >> N >> M;\n if(N == 0) return 0;\n vector<ll> c(M); rep(i, M) cin >> c[i];\n sort(all(c));\n vector<ll> a(N); rep(i, N) cin >> a[i];\n ll W = 256;\n vector<vector<ll>> dp(N+1, vector<ll>(W, 4e18)); dp[0][128]= 0;\n rep(i, N){\n rep(k, W){\n if(dp[i][k] == 4e18) continue;\n rep(l, M){\n ll x = k + c[l];\n if(x < 0) x = 0;\n if(x > W-1) x = W-1;\n chmin(dp[i+1][x], dp[i][k] + (a[i] - x) * (a[i] - x));\n }\n }\n }\n ll ans = 4e18;\n rep(i, W) chmin(ans, dp[N][i]);\n cout << ans << endl;\n return 1;\n}\n\nint main(){\n cin.tie(nullptr);\n ios_base::sync_with_stdio(false);\n cout << fixed << setprecision(20);\n int t = 1;\n // cin >> t;\n while(t){\n t = solve();\n }\n}", "accuracy": 1, "time_ms": 520, "memory_kb": 43948, "score_of_the_acc": -1.0616, "final_rank": 15 }, { "submission_id": "aoj_2199_10637433", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing a2 = array<ll, 2>;\nusing a3 = array<ll, 3>;\n\ntemplate <typename A> void chmin(A &l, const A &r) {\n if(r < l)\n l = r;\n}\ntemplate <typename A> void chmax(A &l, const A &r) {\n if(l < r)\n l = r;\n}\n\nll mod = 998244353;\n\nvoid init() {}\n\nll n, m;\nvector<ll> v, w;\nvoid input() {\n cin >> n >> m;\n v.resize(m);\n w.resize(n);\n for(auto &x : v)\n cin >> x;\n for(auto &x : w)\n cin >> x;\n}\nvoid solve() {\n vector<vector<ll>> dp(n + 1, vector<ll>(256, 1e18));\n dp[0][128] = 0;\n for(int i = 0; i < n; i++) {\n for(int j = 0; j < m; j++) {\n for(int k = 0; k < 256; k++) {\n ll idx = max(0LL, min(255LL, k + v[j]));\n chmin(dp[i + 1][idx], dp[i][k] + (idx - w[i]) * (idx - w[i]));\n }\n }\n }\n cout << *min_element(dp.back().begin(), dp.back().end()) << endl;\n}\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(0);\n\n init();\n while(1) {\n input();\n if(n == 0)\n break;\n solve();\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 510, "memory_kb": 44212, "score_of_the_acc": -1.0336, "final_rank": 14 }, { "submission_id": "aoj_2199_10624984", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef long double ld;\n#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)\n#define rrep(i,start,end) for (ll i = start;i >= (ll)(end);i--)\n#define repn(i,end) for(ll i = 0; i <= (ll)(end); i++)\n#define reps(i,start,end) for(ll i = start; i < (ll)(end); i++)\n#define repsn(i,start,end) for(ll i = start; i <= (ll)(end); i++)\n#define each(p,a) for(auto &p:a)\ntypedef vector<ll> vll;\ntypedef vector<pair<ll ,ll>> vpll;\ntypedef vector<vector<ll>> vvll;\ntypedef set<ll> sll;\ntypedef map<ll , ll> mpll;\ntypedef pair<ll ,ll> pll;\ntypedef tuple<ll , ll , ll> tpl3;\n#define LL(...) ll __VA_ARGS__; input(__VA_ARGS__)\n#define LD(...) ld __VA_ARGS__; input(__VA_ARGS__)\n#define Str(...) string __VA_ARGS__; input(__VA_ARGS__)\n#define Ch(...) char __VA_ARGS__; input(__VA_ARGS__)\n#define all(a) (a).begin(),(a).end()\n#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );\n#define sz(x) (ll)x.size()\n// << std::fixed << std::setprecision(10)\nconst ll INF = 1LL << 60;\nconst ld EPS = 1e-9;\n \nll lceil(ll a,ll b){if(a%b==0){return a/b;}if(a>=0){return (a/b)+1;}else{return -((-a)/b);}}\nll lfloor(ll a,ll b){if(a%b==0){return a/b;}if(a>=0){return (a/b);}else{return -((-a)/b)-1;}}\ninline ll positive_mod(ll a,ll m){return (a % m + m)%m;}\ninline ll popcnt(ull a){ return __builtin_popcountll(a);}\n//0indexed\ninline ll topbit(ll a){assert(a != 0);return 63 - __builtin_clzll(a);}\ninline ll smlbit(ll a){assert(a != 0);return __builtin_ctzll(a);}\ntemplate<class T> bool chmin(T& a, T b){if(a > b){a = b;return true;}return false;}\ntemplate<class T> bool chmax(T& a, T b){if(a < b){a = b;return true;}return false;}\ntemplate<typename T> std::istream &operator>>(std::istream&is,std::vector<T>&v){for(T &in:v){is>>in;}return is;}\ntemplate<typename T> std::ostream &operator<<(std::ostream&os,const std::vector<T>&v){for(auto it=std::begin(v);it!=std::end(v);){os<<*it<<((++it)!=std::end(v)?\" \":\"\");}return os;}\ntemplate<typename T1, typename T2>std::ostream &operator<< (std::ostream &os, std::pair<T1,T2> p){os << \"{\" << p.first << \",\" << p.second << \"}\";return os;}\ntemplate<class... T>void input(T&... a){(cin >> ... >> a);}\nvoid print(){cout << endl;}\ntemplate<class T, class... Ts>void print(const T& a, const Ts&... b){cout << a;((cout << ' ' << b), ...);cout << endl;}\ntemplate<class T> void pspace(const T& a){ cout << a << ' ';}\nvoid perr(){cerr << endl;}\ntemplate<class T, class... Ts>void perr(const T& a, const Ts&... b){cerr << a;((cerr << ' ' << b), ...);cerr << endl;}\nvoid yes(bool i = true){ return print(i?\"yes\":\"no\"); }\nvoid Yes(bool i = true){ return print(i?\"Yes\":\"No\"); }\nvoid YES(bool i = true){ return print(i?\"YES\":\"NO\"); }\ntemplate <class T> vector<T> &operator++(vector<T> &v) {for(auto &e : v) e++;return v;}\ntemplate <class T> vector<T> operator++(vector<T> &v, signed) {auto res = v;for(auto &e : v) e++;return res;}\ntemplate <class T> vector<T> &operator--(vector<T> &v) {for(auto &e : v) e--;return v;}\ntemplate <class T> vector<T> operator--(vector<T> &v, signed) {auto res = v;for(auto &e : v) e--;return res;}\n//grid探索用\nvector<ll> _ta = {0,0,1,-1,1,1,-1,-1};\nvector<ll> _yo = {1,-1,0,0,1,-1,1,-1};\nbool isin(ll now_i,ll now_j,ll h,ll w){return (0<=now_i && now_i < h && 0 <= now_j && now_j < w);}\n \nll lpow(ll x,ll n){ll ans = 1;while(n >0){if(n & 1)ans *= x;x *= x;n >>= 1;}return ans;}\nll Modlpow(ll x,ll n,ll m){ll ans = 1;ll a = x%m;while(n >0){if(n & 1){ans *= a;ans%= m;}a *= a;a %= m;n >>= 1;}return ans;} \nconst ll MOD9 = 998244353LL;\nconst ll MOD10 = 1000000007LL;\n\nvoid solve(int n, int m){\n vector<ll> c(m);\n for (int i=0; i<m; ++i) cin >> c[i];\n vector<ll> x(n);\n for (int i=0; i<n; ++i) cin >> x[i];\n\n // int inf = 1 << 30;\n ll mx = 255;\n vector<vector<ll>> dp(n+1, vector<ll>(mx+1, INF));\n\n dp[0][128] = 0;\n for (ll i=0; i<n; ++i) {\n for (ll j=0; j<=mx; ++j)if(dp[i][j] < INF){\n for (ll k=0; k<m; ++k) {\n if (c[k] >= 0) {\n dp[i+1][min(mx, j+c[k])] = min(\n dp[i+1][min(mx, j+c[k])],dp[i][j] + (min(mx, j+c[k]) - x[i])*(min(mx, j+c[k]) - x[i])\n );\n } else {\n dp[i+1][max(0LL, j+c[k])] = min(\n dp[i+1][max(0LL, j+c[k])],dp[i][j] + (max(0LL, j+c[k]) - x[i])*(max(0LL, j+c[k]) - x[i])\n );\n }\n }\n }\n\n // cout << \"=====\" << endl;\n // for (auto xx: dp) {\n // for (auto x: xx) {\n // cout << x << ' ';\n // }\n // cout << endl;\n // }\n }\n ll ans = INF;\n for (int i=0; i<=mx; ++i) {\n ans = min(ans, dp[n][i]);\n }\n cout << ans << endl;\n}\n\nint main(){\n ios::sync_with_stdio(false);cin.tie(nullptr);\n while (true) {\n int n, m;\n cin >> n >> m;\n if (n == 0 && m == 0) break;\n solve(n, m);\n }\n}", "accuracy": 1, "time_ms": 370, "memory_kb": 44288, "score_of_the_acc": -0.597, "final_rank": 8 }, { "submission_id": "aoj_2199_10586000", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#define int long long\n\nint solve(){\n int n,m;\n cin>>n>>m;\n if(n==0 && m==0){\n return 0;\n }\n vector<int> mlist(m,0);\n for(int i=0;i<m;i++){\n cin>>mlist.at(i);\n }\n vector<int> xlist(n,0);\n for(int i=0;i<n;i++){\n cin>>xlist.at(i);\n }\n vector<vector<int>> dp(n+1,vector<int>(256,8e18));\n dp.at(0).at(128)=0;\n for(int i=0;i<n;i++){\n for(int j=0;j<256;j++){\n for(int k=0;k<m;k++){\n dp.at(i+1).at(max(0LL,min(255LL,j+mlist.at(k))))=min(dp.at(i).at(j)+(xlist.at(i)-(max(0LL,min(255LL,j+mlist.at(k)))))*(xlist.at(i)-(max(0LL,min(255LL,j+mlist.at(k))))),dp.at(i+1).at(max(0LL,min(255LL,j+mlist.at(k)))));\n }\n }\n }\n int ans=8e18;\n for(int j=0;j<256;j++){\n ans=min(ans,dp.at(n).at(j));\n }\n cout<<ans<<endl;\n return 1;\n}\n\n#undef int\nint main(){\n\t#define int long long\n\twhile(solve()){}\n}", "accuracy": 1, "time_ms": 570, "memory_kb": 44288, "score_of_the_acc": -1.222, "final_rank": 17 }, { "submission_id": "aoj_2199_10568178", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define ll long long \n#define INF 1e18\nint main(){\n ios_base::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n vector<ll> ans;\n while(1){\n ll n,m;cin>>n>>m;\n if(n==0&&m==0)break;\n vector<ll> a(m),b(n);\n for(auto& i:a)cin>>i;\n for(auto& i:b)cin>>i;\n vector<vector<ll>> dp(n+1,vector<ll>(259,INF));\n for(ll i=0;i<m;i++){\n if(128+a[i]<0)dp[0][0]=0;\n else if(128+a[i]>255)dp[0][255]=0;\n else dp[0][128+a[i]]=0;\n }\n for(ll i=0;i<n;i++){\n for(ll j=0;j<=255;j++){\n for(ll k=0;k<m;k++){\n if(j+a[k]<0)dp[i+1][0]=min(dp[i+1][0],dp[i][j]+(ll)pow(b[i]-j,2));\n else if(j+a[k]>255)dp[i+1][255]=min(dp[i+1][255],dp[i][j]+(ll)pow(b[i]-j,2));\n else dp[i+1][j+a[k]]=min(dp[i+1][j+a[k]],dp[i][j]+(ll)pow(b[i]-j,2));\n }\n }\n }ans.push_back(*min_element(dp[n].begin(),dp[n].end()));\n }\n for(auto& i:ans)cout<<i<<endl;\n}", "accuracy": 1, "time_ms": 420, "memory_kb": 44292, "score_of_the_acc": -0.7533, "final_rank": 10 }, { "submission_id": "aoj_2199_10526903", "code_snippet": "#include <bits/stdc++.h>\n\n#define paiza\n\n#ifndef paiza\n#include <boost/multiprecision/cpp_int.hpp>\n#include <atcoder/all>\n#endif\n\n#ifndef paiza\n#ifndef ONLINE_JUDGE\n#define GLIBCXX_DEBUG\n#define dev\n#endif\n#endif\n#define rep(i,n) for(ll i=0; i<(n); i++)\n#define rrep(i,n) for(ll i=1; i<=(n); i++)\n#define drep(i,n) for(ll i=(n)-1; i>=0; i--)\n#define xfor(i,s,e) for(ll i=(s); i<(e); i++)\n#define dfor(i,s,e) for(ll i=(s)-1; i>=(e); i--)\n#define all(v) v.begin(), v.end()\n#define rall(v) v.rbegin(), v.rend()\n#define chmax(x,y) x = max(x,y)\n#define chmin(x,y) x = min(x,y)\n#define popcnt(s) ll(popcount<ull>(uint64_t(s)))\nusing namespace std;using ll=long long;using ull=unsigned long long;using dl=long double;\ntemplate<typename T>istream&operator>>(istream&is,pair<T,T>&v){is>>v.first>>v.second;return is;}\ntemplate<typename T>istream&operator>>(istream&is,vector<T>&v){for(auto&in:v)is>>in;return is;}\ntemplate<typename T>ostream&operator<<(ostream&os,vector<T>&v){for(auto&in:v)os<<in<<' ';return os;}\nll modpow(ll a, ll b, ll mod) {ll res = 1;while (b > 0) {if (b & 1) res = (res * a) % mod;a = (a * a) % mod;b >>= 1;}return res;}\nll POW(ll a, ll b) {ll res = 1;while (b > 0) {if (b & 1) res = (res * a);a = (a * a);b >>= 1;}return res;}\n/*MOD MUST BE A PRIME NUMBER!!*/ll moddiv(const ll a, const ll b, const ll mod) {ll modinv = modpow(b, mod-2, mod);return (a * modinv) % mod;}\nconstexpr ll INF = 2e10;\nll nC2(const ll i){return i>2?i*(i-1)/2:0;}ll nC3(const ll i){return i>3?i*(i-1)*(i-2)/6:0;}ll nC4(const ll i){return i>4?i*(i-1)*(i-2)*(i-3)/24:0;}ll nC5(const ll i){return i>5?i*(i-1)*(i-2)*(i-3)*(i-4)/120:0;}ll nC6(const ll i){return i>6?i*(i-1)*(i-2)*(i-3)*(i-4)*(i-5)/720:0;}\nvoid warshall_floyd(vector<vector<ll>>& dist) {const ull n = dist.size();rep(k, n) rep(i, n) rep(j, n) {dist[i][j] = min(dist[i][j],dist[i][k] + dist[k][j]);}}\n#define YES {cout<<\"Yes\\n\";return 0;}\n#define NO {cout<<\"No\\n\";return 0;}\n#define yn YES else NO\n\n#ifndef paiza\n//using lll = boost::multiprecision::cpp_int;\nusing lll = boost::multiprecision::int128_t;\nusing namespace atcoder;\n//using mint = modint1000000007;\n//using mint = modint998244353;\nusing mint = modint; // custom mod\n//atcoder::modint::set_mod(m);\n// DSU == UnionFind\n#endif\n\n#define vc vector\nusing P = pair<ll, ll>;using vl = vc<ll>;using vb=vc<bool>;\nll _min(const vl& v) {ll smallest = INF;for(auto i : v) chmin(smallest, i);return smallest;}\nll _max(const vl& v) {ll biggest = -INF;for(auto i : v) chmax(biggest, i);return biggest;}\ntemplate<typename T>void cs(vector<T>&v){xfor(i,1,v.size())v[i]+=v[i-1];}\nbool operator<(pair<ll,ll> p1, pair<ll,ll> p2) {return p1.first == p2.first ? p1.second < p2.second : p1.first < p2.first;}\n\n#define endl \"\\n\"\n\nvl dx = {-1, 1, 0, 0, -1, 1, -1, 1};\nvl dy = {0, 0, 1, -1, -1, -1, 1, 1};\n\nconstexpr ll mod = 998244353;\n\nint main() {\n ios_base::sync_with_stdio(false);\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n cout << fixed << setprecision(50);\n\n while (true) {\n ll N,M;\n cin>>N>>M;\n if (N==0&&M==0)break;\n\n vl C(M), x(N);\n cin>>C>>x;\n\n auto round = [](ll i){return\n 256<=i ? 255\n : 0>i ? 0\n : i\n ;\n };\n auto cost = [](ll target, ll actual){return (target-actual)*(target-actual);};\n\n // dp[i][j] ... i個目の値を符号化した後にjにするときの最小の累積コスト\n vc<vl> dp(N, vc(256,INF));\n\n for (auto C_i: C) dp[0][round(128+C_i)] = cost(x[0], round(128+C_i));\n rep(i,N-1) { // 配るDP\n rep(j, 256) {\n for (const auto C_i : C) {\n chmin(dp[i+1][round(j+C_i)], dp[i][j]+cost(x[i+1], round(j+C_i)));\n }\n }\n }\n\n cout << *min_element(all(dp[N-1])) << endl;\n }\n}", "accuracy": 1, "time_ms": 350, "memory_kb": 44288, "score_of_the_acc": -0.5345, "final_rank": 7 } ]
aoj_2201_cpp
Problem E: Immortal Jewels English text is not available in this practice contest. ある時ある貴族が,ある貧乏な国のおてんばで勇敢なお姫様に惚れ込み結婚を申し込んだ.お姫様は貴族にある条件を出した.その条件とは「不死の宝石」と呼ばれている宝石を大量に持ってくることであった.不死の宝石は,ある山の特定の場所でしか取ることができない非常に希少な宝石である.しかも大変壊れやすいため,採取するには特別な方法が必要だった. 不死の宝石は円形をしており,二次元空間上に複数存在している.この宝石を取るには,ある特別な金属の棒でそれらを吸着する必要がある.金属の棒は無限の長さを持つ直線であり,太さは無視することができる.宝石は一つ一つ異なる強さの磁力を持っており,金属がその磁力に反応するほど十分に近ければ宝石は吸着される.具体的には,金属と宝石の表面との距離を d ,宝石の磁力の強さを m としたとき, 0 ≤ d ≤ m であれば宝石は金属に吸着される.逆に,金属の棒と宝石がその磁力よりも離れている場合は吸着できない.また,棒が少しでも宝石を貫通してしまった場合にも,その宝石が壊れてしまうため吸着できない. 例を見てみよう.下の図は二次元空間上に置かれた宝石の例である.宝石1から宝石6まであり,磁力はそれぞれ1, 0, 1, 1, 1, 2であるとする. 図 E-1: 宝石の配置例 上の図に加えて金属の棒を配置した例が下の図である.宝石を吸着した結果も表に示してある.この例の場合,宝石3は磁力の届く範囲より離れており,また宝石4は棒に貫通されているため吸着できないが,残りの4つは全て吸着することができる. 図 E-2: 金属の棒の配置例 宝石名 磁力 金属との距離 吸着できるか 宝石1 1 約0.21 できる 宝石2 0 0 できる 宝石3 1 約5.37 できない 宝石4 1 貫通している できない 宝石5 1 約0.97 できる 宝石6 2 約0.53 できる 表 E-3: 吸着結果 貴族は全財産を注ぎ込み,特別な金属の棒を必死に探し求めた.しかしながら,この金属も非常に貴重であったため,結局たった一本しか入手することができなかった.したがって吸着のチャンスはただ一回しか無い. あなたはある貴族に仕えるプログラマーである.あなたの仕事は,与えられた二次元上の宝石の配置に対して,金属の棒をうまく配置したときに,最大で何個の宝石を吸着することができるかを求めるプログラムを書くことである. Input 入力は複数のデータセットから成り, 1つのデータセットは以下の形式で与えられる. N x 1 y 1 r 1 m 1 x 2 y 2 r 2 m 2 ... x N y N r N m N データセットの最初の行は宝石の数 N (1 ≤ N ≤ 50) を表している.続く N 行の各行には4つの整数 x i , y i , r i , m i (-1000 ≤ x i , y i ≤ 1000, 1 ≤ r i ≤ 100, 0 ≤ m i ≤ 100) が記述されており,宝石の位置,大きさ,および磁力を表す.すなわち,宝石 i は中心を( x i , y i )とし半径が r i であるような円形をしており,その磁力は m i である.宝石は互いに重なり合うことは無い. 入力の終わりは,0のみからなる行で表される. Output 各データセットについて, 一度に吸着することができる宝石の最大数を1行に出力せよ. Sample Input 6 -2 -2 1 1 2 2 2 0 5 7 1 1 8 0 3 1 13 4 1 1 16 1 1 2 3 0 0 2 1 10 0 2 1 0 10 2 1 3 0 0 2 1 10 0 2 1 0 6 2 1 3 0 0 2 1 10 0 2 1 0 4 2 1 1 0 0 1 1 0 Output for the Sample Input 4 2 3 3 1
[ { "submission_id": "aoj_2201_10848639", "code_snippet": "#include <iostream>\n#include <cstdio>\n#include <vector>\n#include <cmath>\n#include <algorithm>\n\nusing namespace std;\nconst int maxn = 100 + 5;\nconst double PI = acos(-1);\n\nstruct Point\n{\n double x, y;\n Point(){}\n Point(double x_, double y_):x(x_),y(y_){}\n};\ntypedef Point Vector;\nint n;\n\nstruct Circle:public Point\n{\n Point c;\n double r, m;\n Circle(){}\n Circle(Point c, double r, double m):c(c.x, c.y),r(r),m(m){}\n Point getPoint(double a) {\n return Point(c.x + cos(a) * r, c.y + sin(a) * r);\n }\n};\nvector<Circle> p;\n\nVector operator + (Vector a, Vector b) { return Vector(a.x + b.x, a.y + b.y); }\nVector operator - (Point a, Point b) { return Point(a.x - b.x, a.y - b.y); }\nVector operator * (Vector a, double p) { return Vector(a.x * p, a.y * p); }\nVector operator / (Vector a, double p) { return Vector(a.x / p, a.y / p); }\n\nbool operator < (const Point& a, const Point& b)\n{\n return a.x < b.x || (a.x == b.x && a.y < b.y);\n}\n\nconst double eps = 1e-12;\nint dcmp(double x)\n{\n if(fabs(x) < eps) return 0;\n else return x < 0 ? -1 : 1;\n}\n\nbool operator == (const Point& a, const Point& b)\n{\n return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0;\n}\n\ndouble dot(Vector a, Vector b) { return a.x * b.x + a.y * b.y; }\ndouble length(Vector a) { return sqrt(dot(a, a)); }\ndouble angle(Vector a, Vector b) { return acos(dot(a, b) / length(a) / length(b)); }\ndouble angle(Vector v) { return atan2(v.y, v.x); }\ndouble cross(Vector a, Vector b) { return a.x * b.y - b.x * a.y; }\ndouble dist(Point p1,Point p2) { return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y)); }\n\ndouble distToLine(Point p, Point a, Point b) {\n\tVector v1 = b - a, v2 = p - a;\n\treturn fabs(cross(v1, v2)) / length(v1);\n}\n\nint getTangents(Circle A, Circle B, vector<Point>& a, vector<Point>& b)\n{\n\tint cnt = 0;\n\tif(A.r < B.r) { swap(A, B); swap(a, b); }\n\tint d2 = (A.c.x - B.c.x)*(A.c.x - B.c.x) + (A.c.y - B.c.y)*(A.c.y - B.c.y);\n\tint rdiff = A.r - B.r;\n\tint rsum = A.r + B.r;\n\tif(d2 < rdiff * rdiff)\treturn 0;\n\n\tdouble base = atan2(B.c.y - A.c.y, B.c.x - A.c.x);\n\tif(d2 == 0 && A.r == B.r)\treturn -1;\n\tif(d2 == rdiff * rdiff) {\n\t\ta.push_back(A.getPoint(base));\n\t\tb.push_back(B.getPoint(base));\n\t\treturn 1;\n\t}\n\n\t//有共切线\n\tdouble ang = acos((A.r - B.r) / sqrt(d2));\n\ta.push_back(A.getPoint(base + ang));\tb.push_back(B.getPoint(base + ang));\n\ta.push_back(A.getPoint(base - ang));\tb.push_back(B.getPoint(base - ang));\n\n\tif(d2 == rsum * rsum) {\n\t\ta.push_back(A.getPoint(base));\n\t\tb.push_back(B.getPoint(PI + base));\n\t} else if(d2 > rsum * rsum) {\n\t\tdouble ang = acos((A.r + B.r) / sqrt(d2));\n\t\ta.push_back(A.getPoint(base + ang));\tb.push_back(B.getPoint(PI + base + ang));\n\t\ta.push_back(A.getPoint(base - ang));\tb.push_back(B.getPoint(PI + base - ang));\n\t}\n\treturn a.size();\n}\n\nint cmp(double a, double b)\n{\n\tconst double diff = a - b;\n\tif (fabs(diff) < eps)\n\t\treturn 0;\n\telse if (diff < 0)\n\t\treturn -1;\n\telse\n\t\treturn 1;\n}\n\nint calculate(const Point& a, const Point& b)\n{\n\tint res = 0;\n\tfor(int i = 0; i < n; i++) {\n\t\tdouble d = distToLine(p[i].c, a, b);\n\t\tif(cmp(d, p[i].r) >= 0 && cmp(d, p[i].r + p[i].m) <= 0)\tres++;\n\t\tif(cmp(d, p[i].r) < 0)\treturn -1;\n\t}\n\treturn res;\n}\n\nint main()\n{\n\t//freopen(\"in.txt\", \"r\", stdin);\n while(~scanf(\"%d\", &n)) {\n\t\tif(n == 0)\tbreak;\n\t\tp.clear();\n\t\tdouble x, y, r, m;\n\t\tfor(int i = 0; i < n; i++) {\n\t\t\tscanf(\"%lf%lf%lf%lf\", &x, &y, &r, &m);\n\t\t\tp.push_back(Circle(Point(x, y), r, m));\n\t\t}\n\n\t\tvector<Point> a, b;\n\t\tfor(int i = 0; i < n; i++) {\n\t\t\tfor(int j = 0; j < n; j++) if(i != j) {\n\t\t\t\tCircle A1 = p[i], A2 = p[i];\n\t\t\t\tCircle B1 = p[j], B2 = p[j];\n\t\t\t\tB2.r += B2.m;\tA2.r += A2.m;\n\n\t\t\t\tgetTangents(A1, B1, a, b);\n\t\t\t\tgetTangents(A1, B2, a, b);\n\t\t\t\tgetTangents(A2, B1, a, b);\n\t\t\t\tgetTangents(A2, B2, a, b);\n\t\t\t}\n\t\t}\n\n\t\tint num = a.size(), ans = 1;\n\t\tfor(int i = 0; i < num; i++) {\n\t\t\tint res = count_if(p.begin(), p.end(), [&](const Circle& C) {\n\t\t\t\t\t\t\t\treturn cmp(distToLine(C.c, a[i], b[i]), C.r) >= 0\n\t\t\t\t\t\t\t\t&& cmp(distToLine(C.c, a[i], b[i]), C.r + C.m) <= 0; });\n\t\t\tans = max(ans, res);\n\t\t}\n\t\tprintf(\"%d\\n\", ans);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 230, "memory_kb": 6524, "score_of_the_acc": -0.9964, "final_rank": 18 }, { "submission_id": "aoj_2201_9557998", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n#include <unistd.h>\n\nnamespace fastio {\nstatic constexpr uint32_t SZ = 1 << 17;\nchar ibuf[SZ];\nchar obuf[SZ];\nchar out[100];\n// pointer of ibuf, obuf\n\n\nuint32_t pil = 0, pir = 0, por = 0;\n\nstruct Pre {\n char num[10000][4];\n constexpr Pre() : num() {\n for (int i = 0; i < 10000; i++) {\n int n = i;\n for (int j = 3; j >= 0; j--) {\n num[i][j] = n % 10 | '0';\n n /= 10;\n }\n }\n }\n} constexpr pre;\n\ninline void load() {\n memmove(ibuf, ibuf + pil, pir - pil);\n pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin);\n pil = 0;\n if (pir < SZ)\n ibuf[pir++] = '\\n';\n}\n\ninline void flush() {\n fwrite(obuf, 1, por, stdout);\n por = 0;\n}\n\nvoid rd(char &c) {\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n}\n\nvoid rd(string &x) {\n x.clear();\n char c;\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n do {\n x += c;\n if (pil == pir)\n load();\n c = ibuf[pil++];\n } while (!isspace(c));\n}\n\ntemplate <typename T> void rd_real(T &x) {\n string s;\n rd(s);\n x = stod(s);\n}\n\ntemplate <typename T> void rd_integer(T &x) {\n if (pil + 100 > pir)\n load();\n char c;\n do\n c = ibuf[pil++];\n while (c < '-');\n bool minus = 0;\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (c == '-') {\n minus = 1, c = ibuf[pil++];\n }\n }\n x = 0;\n while ('0' <= c) {\n x = x * 10 + (c & 15), c = ibuf[pil++];\n }\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (minus)\n x = -x;\n }\n}\n\nvoid rd(int &x) {\n rd_integer(x);\n}\nvoid rd(ll &x) {\n rd_integer(x);\n}\nvoid rd(i128 &x) {\n rd_integer(x);\n}\nvoid rd(uint &x) {\n rd_integer(x);\n}\nvoid rd(ull &x) {\n rd_integer(x);\n}\nvoid rd(u128 &x) {\n rd_integer(x);\n}\nvoid rd(double &x) {\n rd_real(x);\n}\nvoid rd(long double &x) {\n rd_real(x);\n}\n\ntemplate <class T, class U> void rd(pair<T, U> &p) {\n return rd(p.first), rd(p.second);\n}\ntemplate <size_t N = 0, typename T> void rd_tuple(T &t) {\n if constexpr (N < std::tuple_size<T>::value) {\n auto &x = std::get<N>(t);\n rd(x);\n rd_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void rd(tuple<T...> &tpl) {\n rd_tuple(tpl);\n}\n\ntemplate <size_t N = 0, typename T> void rd(array<T, N> &x) {\n for (auto &d : x)\n rd(d);\n}\ntemplate <class T> void rd(vector<T> &x) {\n for (auto &d : x)\n rd(d);\n}\n\nvoid read() {}\ntemplate <class H, class... T> void read(H &h, T &...t) {\n rd(h), read(t...);\n}\n\nvoid wt(const char c) {\n if (por == SZ)\n flush();\n obuf[por++] = c;\n}\nvoid wt(const string s) {\n for (char c : s)\n wt(c);\n}\nvoid wt(const char *s) {\n size_t len = strlen(s);\n for (size_t i = 0; i < len; i++)\n wt(s[i]);\n}\n\ntemplate <typename T> void wt_integer(T x) {\n if (por > SZ - 100)\n flush();\n if (x < 0) {\n obuf[por++] = '-', x = -x;\n }\n int outi;\n for (outi = 96; x >= 10000; outi -= 4) {\n memcpy(out + outi, pre.num[x % 10000], 4);\n x /= 10000;\n }\n if (x >= 1000) {\n memcpy(obuf + por, pre.num[x], 4);\n por += 4;\n } else if (x >= 100) {\n memcpy(obuf + por, pre.num[x] + 1, 3);\n por += 3;\n } else if (x >= 10) {\n int q = (x * 103) >> 10;\n obuf[por] = q | '0';\n obuf[por + 1] = (x - q * 10) | '0';\n por += 2;\n } else\n obuf[por++] = x | '0';\n memcpy(obuf + por, out + outi + 4, 96 - outi);\n por += 96 - outi;\n}\n\ntemplate <typename T> void wt_real(T x) {\n ostringstream oss;\n oss << fixed << setprecision(15) << double(x);\n string s = oss.str();\n wt(s);\n}\n\nvoid wt(int x) {\n wt_integer(x);\n}\nvoid wt(ll x) {\n wt_integer(x);\n}\nvoid wt(i128 x) {\n wt_integer(x);\n}\nvoid wt(uint x) {\n wt_integer(x);\n}\nvoid wt(ull x) {\n wt_integer(x);\n}\nvoid wt(u128 x) {\n wt_integer(x);\n}\nvoid wt(double x) {\n wt_real(x);\n}\nvoid wt(long double x) {\n wt_real(x);\n}\n\ntemplate <class T, class U> void wt(const pair<T, U> val) {\n wt(val.first);\n wt(' ');\n wt(val.second);\n}\ntemplate <size_t N = 0, typename T> void wt_tuple(const T t) {\n if constexpr (N < std::tuple_size<T>::value) {\n if constexpr (N > 0) {\n wt(' ');\n }\n const auto x = std::get<N>(t);\n wt(x);\n wt_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void wt(tuple<T...> tpl) {\n wt_tuple(tpl);\n}\ntemplate <class T, size_t S> void wt(const array<T, S> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\ntemplate <class T> void wt(const vector<T> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\n\nvoid print() {\n wt('\\n');\n}\ntemplate <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {\n wt(head);\n if (sizeof...(Tail))\n wt(' ');\n print(forward<Tail>(tail)...);\n}\nvoid __attribute__((destructor)) _d() {\n flush();\n}\n} // namespace fastio\n\n\nusing fastio::flush;\nusing fastio::print;\nusing fastio::read;\n\ninline void first(bool i = true) {\n print(i ? \"first\" : \"second\");\n}\ninline void Alice(bool i = true) {\n print(i ? \"Alice\" : \"Bob\");\n}\ninline void Takahashi(bool i = true) {\n print(i ? \"Takahashi\" : \"Aoki\");\n}\ninline void yes(bool i = true) {\n print(i ? \"yes\" : \"no\");\n}\ninline void Yes(bool i = true) {\n print(i ? \"Yes\" : \"No\");\n}\ninline void No() {\n print(\"No\");\n}\ninline void YES(bool i = true) {\n print(i ? \"YES\" : \"NO\");\n}\ninline void NO() {\n print(\"NO\");\n}\ninline void Yay(bool i = true) {\n print(i ? \"Yay!\" : \":(\");\n}\ninline void Possible(bool i = true) {\n print(i ? \"Possible\" : \"Impossible\");\n}\ninline void POSSIBLE(bool i = true) {\n print(i ? \"POSSIBLE\" : \"IMPOSSIBLE\");\n}\n\n/**\n * @brief Fast IO\n */\n\nusing T = double;\nconst T eps = 1e-12;\nusing Point = complex<T>;\nusing Poly = vector<Point>;\n#define X real()\n#define Y imag()\ntemplate <typename T> inline bool eq(const T &a, const T &b) {\n return fabs(a - b) < eps;\n}\nbool cmp(const Point &a, const Point &b) {\n auto sub = [&](Point a) {\n return (a.Y < 0 ? -1 : (a.Y == 0 && a.X >= 0 ? 0 : 1));\n };\n if (sub(a) != sub(b))\n return sub(a) < sub(b);\n return a.Y * b.X < a.X * b.Y;\n}\nstruct Line {\n Point a, b, dir;\n Line() {}\n Line(Point _a, Point _b) : a(_a), b(_b), dir(b - a) {}\n Line(T A, T B, T C) {\n if (eq(A, .0)) {\n a = Point(0, C / B), b = Point(1 / C / B);\n } else if (eq(B, .0)) {\n a = Point(C / A, 0), b = Point(C / A, 1);\n } else {\n a = Point(0, C / B), b = Point(C / A, 0);\n }\n }\n};\nstruct Segment : Line {\n Segment() {}\n Segment(Point _a, Point _b) : Line(_a, _b) {}\n};\nstruct Circle {\n Point p;\n T r;\n Circle() {}\n Circle(Point _p, T _r) : p(_p), r(_r) {}\n};\n\nistream &operator>>(istream &is, Point &p) {\n T x, y;\n is >> x >> y;\n p = Point(x, y);\n return is;\n}\nostream &operator<<(ostream &os, Point &p) {\n os << fixed << setprecision(12) << p.X << ' ' << p.Y;\n return os;\n}\nPoint unit(const Point &a) {\n return a / abs(a);\n}\nT dot(const Point &a, const Point &b) {\n return a.X * b.X + a.Y * b.Y;\n}\nT cross(const Point &a, const Point &b) {\n return a.X * b.Y - a.Y * b.X;\n}\nPoint rot(const Point &a, const T &theta) {\n return Point(cos(theta) * a.X - sin(theta) * a.Y,\n sin(theta) * a.X + cos(theta) * a.Y);\n}\nPoint rot90(const Point &a) {\n return Point(-a.Y, a.X);\n}\nT arg(const Point &a, const Point &b, const Point &c) {\n double ret = acos(dot(a - b, c - b) / abs(a - b) / abs(c - b));\n if (cross(a - b, c - b) < 0)\n ret = -ret;\n return ret;\n}\n\nPoint Projection(const Line &l, const Point &p) {\n T t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n return l.a + (l.a - l.b) * t;\n}\nPoint Projection(const Segment &l, const Point &p) {\n T t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n return l.a + (l.a - l.b) * t;\n}\nPoint Reflection(const Line &l, const Point &p) {\n return p + (Projection(l, p) - p) * 2.;\n}\nint ccw(const Point &a, Point b, Point c) {\n b -= a;\n c -= a;\n if (cross(b, c) > eps)\n return 1; // ccw\n\n if (cross(b, c) < -eps)\n return -1; // cw\n\n if (dot(b, c) < 0)\n return 2; // c,a,b\n\n if (norm(b) < norm(c))\n return -2; // a,b,c\n\n return 0; // a,c,b\n\n}\nbool isOrthogonal(const Line &a, const Line &b) {\n return eq(dot(a.b - a.a, b.b - b.a), .0);\n}\nbool isParallel(const Line &a, const Line &b) {\n return eq(cross(a.b - a.a, b.b - b.a), .0);\n}\nbool isIntersect(const Segment &a, const Segment &b) {\n return ccw(a.a, a.b, b.a) * ccw(a.a, a.b, b.b) <= 0 and\n ccw(b.a, b.b, a.a) * ccw(b.a, b.b, a.b) <= 0;\n}\nint isIntersect(const Circle &a, const Circle &b) {\n T d = abs(a.p - b.p);\n if (d > a.r + b.r + eps)\n return 4;\n if (eq(d, a.r + b.r))\n return 3;\n if (eq(d, abs(a.r - b.r)))\n return 1;\n if (d < abs(a.r - b.r) - eps)\n return 0;\n return 2;\n}\nT Dist(const Line &a, const Point &b) {\n Point c = Projection(a, b);\n return abs(c - b);\n}\nT Dist(const Segment &a, const Point &b) {\n if (dot(a.b - a.a, b - a.a) < eps)\n return abs(b - a.a);\n if (dot(a.a - a.b, b - a.b) < eps)\n return abs(b - a.b);\n return abs(cross(a.b - a.a, b - a.a)) / abs(a.b - a.a);\n}\nT Dist(const Segment &a, const Segment &b) {\n if (isIntersect(a, b))\n return .0;\n T res = min({Dist(a, b.a), Dist(a, b.b), Dist(b, a.a), Dist(b, a.b)});\n return res;\n}\nPoint Intersection(const Line &a, const Line &b) {\n T d1 = cross(a.b - a.a, b.b - b.a);\n T d2 = cross(a.b - a.a, a.b - b.a);\n if (eq(d1, 0.) and eq(d2, 0.))\n return b.a;\n return b.a + (b.b - b.a) * (d2 / d1);\n}\nPoly Intersection(const Circle &a, const Line &b) {\n Poly res;\n T d = Dist(b, a.p);\n if (d > a.r + eps)\n return res;\n Point h = Projection(b, a.p);\n if (eq(d, a.r)) {\n res.push_back(h);\n return res;\n }\n Point e = unit(b.b - b.a);\n T ph = sqrt(a.r * a.r - d * d);\n res.push_back(h - e * ph);\n res.push_back(h + e * ph);\n return res;\n}\nPoly Intersection(const Circle &a, const Segment &b) {\n Line c(b.a, b.b);\n Poly sub = Intersection(a, c);\n double xmi = min(b.a.X, b.b.X), xma = max(b.a.X, b.b.X);\n double ymi = min(b.a.Y, b.b.Y), yma = max(b.a.Y, b.b.Y);\n Poly res;\n rep(i, 0, sub.size()) {\n if (xmi <= sub[i].X + eps and sub[i].X - eps <= xma and\n ymi <= sub[i].Y + eps and sub[i].Y - eps <= yma) {\n res.push_back(sub[i]);\n }\n }\n return res;\n}\nPoly Intersection(const Circle &a, const Circle &b) {\n Poly res;\n int mode = isIntersect(a, b);\n T d = abs(a.p - b.p);\n if (mode == 4 or mode == 0)\n return res;\n if (mode == 3) {\n T t = a.r / (a.r + b.r);\n res.push_back(a.p + (b.p - a.p) * t);\n return res;\n }\n if (mode == 1) {\n if (b.r < a.r - eps) {\n res.push_back(a.p + (b.p - a.p) * (a.r / d));\n } else {\n res.push_back(b.p + (a.p - b.p) * (b.r / d));\n }\n return res;\n }\n T rc = (a.r * a.r + d * d - b.r * b.r) / d / 2.;\n T rs = sqrt(a.r * a.r - rc * rc);\n if (a.r - abs(rc) < eps)\n rs = 0;\n Point e = unit(b.p - a.p);\n res.push_back(a.p + rc * e + rs * e * Point(0, 1));\n res.push_back(a.p + rc * e + rs * e * Point(0, -1));\n return res;\n}\nPoly HalfplaneIntersection(vector<Line> &H) {\n sort(ALL(H), [&](Line &l1, Line &l2) { return cmp(l1.dir, l2.dir); });\n auto outside = [&](Line &L, Point p) -> bool {\n return cross(L.dir, p - L.a) < -eps;\n };\n deque<Line> deq;\n int sz = 0;\n rep(i, 0, SZ(H)) {\n while (sz > 1 and\n outside(H[i], Intersection(deq[sz - 1], deq[sz - 2]))) {\n deq.pop_back();\n sz--;\n }\n while (sz > 1 and outside(H[i], Intersection(deq[0], deq[1]))) {\n deq.pop_front();\n sz--;\n }\n if (sz > 0 and fabs(cross(H[i].dir, deq[sz - 1].dir)) < eps) {\n if (dot(H[i].dir, deq[sz - 1].dir) < 0) {\n return {};\n }\n if (outside(H[i], deq[sz - 1].a)) {\n deq.pop_back();\n sz--;\n } else\n continue;\n }\n deq.push_back(H[i]);\n sz++;\n }\n\n while (sz > 2 and outside(deq[0], Intersection(deq[sz - 1], deq[sz - 2]))) {\n deq.pop_back();\n sz--;\n }\n while (sz > 2 and outside(deq[sz - 1], Intersection(deq[0], deq[1]))) {\n deq.pop_front();\n sz--;\n }\n if (sz < 3)\n return {};\n deq.push_back(deq.front());\n Poly ret;\n rep(i, 0, sz) ret.push_back(Intersection(deq[i], deq[i + 1]));\n return ret;\n}\n\nT Area(const Poly &a) {\n T res = 0;\n int n = a.size();\n rep(i, 0, n) res += cross(a[i], a[(i + 1) % n]);\n return fabs(res / 2.);\n}\nT Area(const Poly &a, const Circle &b) {\n int n = a.size();\n if (n < 3)\n return .0;\n auto rec = [&](auto self, const Circle &c, const Point &p1,\n const Point &p2) {\n Point va = c.p - p1, vb = c.p - p2;\n T f = cross(va, vb), res = .0;\n if (eq(f, .0))\n return res;\n if (max(abs(va), abs(vb)) < c.r + eps)\n return f;\n if (Dist(Segment(p1, p2), c.p) > c.r - eps)\n return c.r * c.r * arg(vb * conj(va));\n auto u = Intersection(c, Segment(p1, p2));\n Poly sub;\n sub.push_back(p1);\n for (auto &x : u)\n sub.push_back(x);\n sub.push_back(p2);\n rep(i, 0, sub.size() - 1) res += self(self, c, sub[i], sub[i + 1]);\n return res;\n };\n T res = .0;\n rep(i, 0, n) res += rec(rec, b, a[i], a[(i + 1) % n]);\n return fabs(res / 2.);\n}\nT Area(const Circle &a, const Circle &b) {\n T d = abs(a.p - b.p);\n if (d >= a.r + b.r - eps)\n return .0;\n if (d <= abs(a.r - b.r) + eps) {\n T r = min(a.r, b.r);\n return M_PI * r * r;\n }\n T ath = acos((a.r * a.r + d * d - b.r * b.r) / d / a.r / 2.);\n T res = a.r * a.r * (ath - sin(ath * 2) / 2.);\n T bth = acos((b.r * b.r + d * d - a.r * a.r) / d / b.r / 2.);\n res += b.r * b.r * (bth - sin(bth * 2) / 2.);\n return fabs(res);\n}\nbool isConvex(const Poly &a) {\n int n = a.size();\n int cur, pre, nxt;\n rep(i, 0, n) {\n pre = (i - 1 + n) % n;\n nxt = (i + 1) % n;\n cur = i;\n if (ccw(a[pre], a[cur], a[nxt]) == -1)\n return 0;\n }\n return 1;\n}\nint isContained(const Poly &a,\n const Point &b) { // 0:not contain,1:on edge,2:contain\n\n bool res = 0;\n int n = a.size();\n rep(i, 0, n) {\n Point p = a[i] - b, q = a[(i + 1) % n] - b;\n if (p.Y > q.Y)\n swap(p, q);\n if (p.Y < eps and eps < q.Y and cross(p, q) > eps)\n res ^= 1;\n if (eq(cross(p, q), .0) and dot(p, q) < eps)\n return 1;\n }\n return (res ? 2 : 0);\n}\nPoly ConvexHull(Poly &a) {\n sort(ALL(a), [](const Point &p, const Point &q) {\n return (eq(p.Y, q.Y) ? p.X < q.X : p.Y < q.Y);\n });\n a.erase(unique(ALL(a)), a.end());\n int n = a.size(), k = 0;\n Poly res(n * 2);\n for (int i = 0; i < n; res[k++] = a[i++]) {\n while (k >= 2 and\n cross(res[k - 1] - res[k - 2], a[i] - res[k - 1]) < eps)\n k--;\n }\n for (int i = n - 2, t = k + 1; i >= 0; res[k++] = a[i--]) {\n while (k >= t and\n cross(res[k - 1] - res[k - 2], a[i] - res[k - 1]) < eps)\n k--;\n }\n res.resize(k - 1);\n return res;\n}\nT Diam(const Poly &a) {\n int n = a.size();\n int x = 0, y = 0;\n rep(i, 1, n) {\n if (a[i].Y > a[x].Y)\n x = i;\n if (a[i].Y < a[y].Y)\n y = i;\n }\n T res = abs(a[x] - a[y]);\n int i = x, j = y;\n do {\n if (cross(a[(i + 1) % n] - a[i], a[(j + 1) % n] - a[j]) < 0)\n i = (i + 1) % n;\n else\n j = (j + 1) % n;\n chmax(res, abs(a[i] - a[j]));\n } while (i != x or j != y);\n return res;\n}\nPoly Cut(const Poly &a, const Line &l) {\n int n = a.size();\n Poly res;\n rep(i, 0, n) {\n Point p = a[i], q = a[(i + 1) % n];\n if (ccw(l.a, l.b, p) != -1)\n res.push_back(p);\n if (ccw(l.a, l.b, p) * ccw(l.a, l.b, q) < 0)\n res.push_back(Intersection(Line(p, q), l));\n }\n return res;\n}\n\nT Closest(Poly &a) {\n int n = a.size();\n if (n <= 1)\n return 0;\n sort(ALL(a), [&](Point a, Point b) {\n return (eq(a.X, b.X) ? a.Y < b.Y : a.X < b.X);\n });\n Poly buf(n);\n auto rec = [&](auto self, int lb, int rb) -> T {\n if (rb - lb <= 1)\n return (T)INF;\n int mid = (lb + rb) >> 1;\n auto x = a[mid].X;\n T res = min(self(self, lb, mid), self(self, mid, rb));\n inplace_merge(a.begin() + lb, a.begin() + mid, a.begin() + rb,\n [&](auto p, auto q) { return p.Y < q.Y; });\n int ptr = 0;\n rep(i, lb, rb) {\n if (abs(a[i].X - x) >= res)\n continue;\n rep(j, 0, ptr) {\n auto sub = a[i] - buf[ptr - 1 - j];\n if (sub.Y >= res)\n break;\n chmin(res, abs(sub));\n }\n buf[ptr++] = a[i];\n }\n return res;\n };\n return rec(rec, 0, n);\n}\n\nCircle Incircle(const Point &a, const Point &b, const Point &c) {\n T A = abs(b - c), B = abs(c - a), C = abs(a - b);\n Point p(A * a.X + B * b.X + C * c.X, A * a.Y + B * b.Y + C * c.Y);\n p /= (A + B + C);\n T r = Dist(Line(a, b), p);\n return Circle(p, r);\n}\nCircle Circumcircle(const Point &a, const Point &b, const Point &c) {\n Line l1((a + b) / 2., (a + b) / 2. + (b - a) * Point(0, 1));\n Line l2((b + c) / 2., (b + c) / 2. + (c - b) * Point(0, 1));\n Point p = Intersection(l1, l2);\n return Circle(p, abs(p - a));\n}\nPoly tangent(const Point &a, const Circle &b) {\n return Intersection(b, Circle(a, sqrt(norm(b.p - a) - b.r * b.r)));\n}\nvector<Line> tangent(const Circle &a, const Circle &b) {\n vector<Line> res;\n T d = abs(a.p - b.p);\n if (eq(d, 0.))\n return res;\n Point u = unit(b.p - a.p);\n Point v = u * Point(0, 1);\n for (int t : {-1, 1}) {\n T h = (a.r + b.r * t) / d;\n if (eq(h * h, 1.)) {\n res.push_back(Line(a.p + (h > 0 ? u : -u) * a.r,\n a.p + (h > 0 ? u : -u) * a.r + v));\n } else if (1 > h * h) {\n Point U = u * h, V = v * sqrt(1 - h * h);\n res.push_back(Line(a.p + (U + V) * a.r, b.p - (U + V) * (b.r * t)));\n res.push_back(Line(a.p + (U - V) * a.r, b.p - (U - V) * (b.r * t)));\n }\n }\n return res;\n}\n\n/**\n * @brief Geometry\n */\n\nint COUNT(Line& L, vector<Point>& Center, vector<double>& R, vector<double>& M) {\n int N = Center.size();\n int Ret = 0;\n rep(i,0,N) {\n double D = Dist(L, Center[i]);\n if (R[i] - eps < D && D < R[i] + M[i] + eps) Ret++;\n }\n return Ret;\n}\n\nint main() {\nwhile(1) {\n int N;\n cin >> N;\n if (N == 0) return 0;\n vector<double> PX(N), PY(N), R(N), M(N);\n vector<Point> Center(N);\n vector<Circle> C1, C2;\n rep(i,0,N) {\n cin >> PX[i] >> PY[i] >> R[i] >> M[i];\n Center[i] = Point(PX[i],PY[i]);\n Circle c1(Center[i],R[i]), c2(Center[i],R[i]+M[i]);\n C1.push_back(c1);\n C2.push_back(c1), C2.push_back(c2);\n }\n int ANS = 1;\n rep(i,0,N*2) {\n rep(j,i+1,N*2) {\n if (i / 2 == j / 2) continue;\n vector<Line> LS = tangent(C2[i],C2[j]);\n for (Line L : LS) {\n chmax(ANS, COUNT(L,Center,R,M));\n }\n }\n }\n cout << ANS << endl;\n}\n}", "accuracy": 1, "time_ms": 290, "memory_kb": 3604, "score_of_the_acc": -0.2103, "final_rank": 12 }, { "submission_id": "aoj_2201_9384404", "code_snippet": "#include <bits/stdc++.h>\n//#include <ranges>\n#define _USE_MATH_DEFINES\n//#include <atcoder/all>\n//using namespace atcoder;\n//using mint = modint998244353;\n//using mint = modint1000000007;\n//using mint = modint;\nusing namespace std;\n\nusing ll = long long;\n#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)\n#define rep2(i, s, n) for (ll i = (s); i < (ll)(n); i++)\n#define rrep(i,a,b) for(int i=a;i>=b;i--)\n#define fore(i,a) for(auto &i:a)\n#define V vector<ll>\n#define Vi vector<int>\n#define Vd vector<double>\n#define Vb vector<bool>\n#define Vs vector<string>\n#define Vc vector<char>\n#define VV vector<V>\nusing P = pair<ll,ll>;\nusing G = vector<vector<int>>;\n#define VP vector<P>\ntemplate<typename T> using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;\n#define all(a) (a).begin(),(a).end()\n#define rall(a) (a).rbegin(),(a).rend()\n#define INF 1LL << 60\n#define inf 1e9\ntemplate <typename T>\nbool chmax(T &a, const T& b) {\n if (a < b) {\n a = b; // aをbで更新\n return true;\n }\nreturn false;\n}\ntemplate <typename T>\nbool chmin(T &a, const T& b) {\n if (a > b) {\n a = b; // aをbで更新\n return true;\n }\n return false;\n}\n\nlong long combi(long long n, long long k) {\n if (n == k || k == 0)\n return 1;\n else {\n return combi(n - 1, k - 1) + combi(n - 1, k);\n }\n}\n//整数かどうか\nbool isNumber(const string& str)\n{\n for (const char &c : str) {\n if (std::isdigit(c) == 0) return false;\n }\n return true;\n}\n///*\n//最大公約数\nll gcd(ll a, ll b){\n if(b==0){\n return a;\n }else{\n return gcd(b, a%b);\n }\n}\n//最小公倍数\nll lcm(ll a, ll b){\n ll g=gcd(a,b);\n return a/g*b;\n}\n//*/\n//int di[] = {-1,0,1,0};\n//int dj[] = {0,-1,0,1};\n\n//s = regex_replace(s, regex(\"あ\"), \"う\");\n/*stiring で char を検索するときは\n s.find(c)!=string::npos\n */\n\n\n/*//各桁の和\n int wa(int n){\n int sum =0;\n while(n>0){\n sum += n%10;\n n/=10;\n }\n return sum;\n}\n*/\n/*\n//階乗\n int ki(int i){\n int k = 1;\n for(int j = 1; j<=i; j++){\n k *= j;\n }\n return k;\n }\n*/\n/*log_x(b)\ndouble logN(double x, double b) {\n return log(x) / log(b);\n}\n*/\n//\n/*//エラトステネスの篩 main関数内にsolve();を書き込む!!\nconst ll N = 101010;//求める範囲\nVb isp(N+1,true);\nvoid solve(){\n isp[0] = false;\n isp[1] = false;\n for(ll i = 2; i+i<=N;i++){\n if(isp[i])for(ll j = 2; i*j<=N;j++)isp[i*j] = false;\n }\n}\n//\n*/\n//\n/*\n//約数列挙 O(√n)\nvector<long long> divisor(long long n) {\n vector<long long> ret;\n for (long long i = 1; i * i <= n; i++) {\n if (n % i == 0) {\n ret.push_back(i);\n if (i * i != n) ret.push_back(n / i);\n }\n }\n sort(ret.begin(), ret.end()); // 昇順に並べる\n return ret;\n}\n//\n*/\n//\n/*\n//素因数分解O(√n)\nmap< ll, ll > prime_factor(ll n) {\n map< ll, ll > ret;\n for(ll i = 2; i * i <= n; i++) {\n while(n % i == 0) {\n ret[i]++;\n n /= i;\n }\n }\n if(n != 1) ret[n] = 1;\n return ret;\n}\n//\n*/\n\n/*\nll modpow(ll x, ll n, ll mod){\n while(n){\n ll resu = 1;\n if(n&1)res = (res * x) %mod;\n x = (x*x)%mod;\n n>>=1;\n }\n return res;\n}\n*/\n/*\n//最小二乗法\n//aのb乗をmで割ったあまりを返す関数\n//変数aはa^1→a^2→a^4→a^8→…と変化\nll power(ll a,ll b, ll m){\n ll p = a,ans = 1;\n rep(i,60){\n ll wari = (1LL<<i);\n if((b/wari)%2==1){\n ans=(ans*p)%m;\n }\n p=(p*p)%m;\n }\n return ans;\n}\n*/\n//\n/*\ntemplate <typename T> bool next_combination(const T first, const T last, int k) {\n const T subset = first + k;\n // empty container | k = 0 | k == n \n if (first == last || first == subset || last == subset) {\n return false;\n }\n T src = subset;\n while (first != src) {\n src--;\n if (*src < *(last - 1)) {\n T dest = subset;\n while (*src >= *dest) {\n dest++;\n }\n iter_swap(src, dest);\n rotate(src + 1, dest + 1, last);\n rotate(subset, subset + (last - dest) - 1, last);\n return true;\n }\n }\n // restore\n rotate(first, subset, last);\n return false;\n}\n//\n*/\nint ctoi(char c){\n if(c=='1')return 1;\n else if(c=='2')return 2;\n else if(c=='3')return 3;\n else if(c=='4')return 4;\n else if(c=='5')return 5;\n else if(c=='6')return 6;\n else if(c=='7')return 7;\n else if(c=='8')return 8;\n else if(c=='9')return 9;\n else if(c=='0')return 0;\n else return -inf;\n}\n//vector<int> dx = {1,0,-1,0,1,-1,-1,1};\n//vector<int> dy = {0,1,0,-1,1,1,-1,-1};\n//int dx[4] = { 0, 1, 0, -1 }, dy[4] = { -1, 0, 1, 0 };\nint dx[8]={0,-1,-1,-1,0,1,1,1},dy[8]={1,1,0,-1,-1,-1,0,1};\n//#define mod 998244353\n//cout << mint.val() << endl;\n//cout << fixed << setprecision(15) << y << endl;\n\n//bit s に i番目のビットを立てる\n#define bittate(s,i) s | (1LL<<i)\n//bit sから i番目のビットを消す\n#define bitkeshi(s,i) s^(1LL<<i)\n//bit s が i番目のビットを含んでいるか\n#define bitcheck(s,i) (s>>i)&1LL\n//string str(bitset<32>(value).to_string<char, char_traits<char>, allocator<char> >());\n#define yes \"Yes\"\n#define no \"No\"\n#define Yes \"YES\"\n#define No \"NO\"\n\n\n#include<complex>\n\n\nnamespace geometry{\n using ld=long double;\n using point=complex<ld>;\n const ld eps=1e-9;\n const ld PI=acos(ld(-1));\n inline bool equal(const ld &a,const ld &b){\n return fabs(a-b)<eps;\n }\n //単位ベクトル\n point unitVector(const point &a){\n return a/abs(a);\n }\n //法線ベクトル\n point normalVector(const point &a){\n return a*point(0,1);\n }\n //内積: a*b=|a||b|cosθ\n ld dot(const point &a, const point &b){\n return (a.real()*b.real()+a.imag()*b.imag());\n }\n //外積: a×b=|a||b|sinθ\n ld cross(const point &a, const point &b){\n return (a.real()*b.imag()-a.imag()*b.real());\n }\n //反時計回りに回転(ラジアン)\n point rotate(const point &p, const ld &theta){\n return point(cos(theta)*p.real()-sin(theta)*p.imag(),\n sin(theta)*p.real()+cos(theta)*p.imag());\n }\n //ラジアンー>度\n ld radianTodegree(const ld &radian){return radian *180/PI;}\n //度ー>ラジアン\n ld degreeToradian(const ld &degree){return degree *PI/180;}\n //点の回転方向\n //点aを基準にしてb,cの位置関係\n int ccw(const point &a, point b,point c){\n b-=a,c-=a;\n //反時計回りのときー>1\n if(cross(b,c)>eps)return 1;\n //時計回りのときー>-1\n if(cross(b,c)<-eps)return -1;\n //c,a,bがこの順で同一直線上にあるとき\n if(dot(b,c)<0)return 2;\n //a,b,cがこの順で同一直線上にあるとき\n if(norm(b)<norm(c))return -2;\n //cが線分ab上にあるとき\n return 0;\n }\n //Line:直線\n //b-aで直線・線分\n struct Line{\n point a,b;\n Line()=default;\n Line(point a, point b) : a(a),b(b){}\n //ax+by=c\n Line(ld A, ld B, ld C){\n if(equal(A,0)){\n a=point(0,C/B),b=point(1,C/B);\n }else if(equal(B,0)){\n b=point(C/A,0),b=point(C/A,1);//ホントにb⁇\n }else{\n a=point(0,C/B),b=point(C/A,0);\n }\n }\n };\n //線分\n struct Segment : Line{\n Segment()=default;\n Segment(point a, point b):Line(a,b){}\n ld get_dist(){return abs(a-b);}\n };\n //円\n //pが中心の位置ベクトル,rは半径\n struct circle{\n point p;\n ld r;\n circle()=default;\n circle(point p, ld r) : p(p),r(r){};\n };\n //2直線の直交判定 内積0\n bool isOrthogonal(const Line &a, const Line &b){\n return equal(dot(a.b-a.a,b.b-b.a),0);\n }\n //2直線の平行判定 外積0\n bool isParaleel(const Line &a, const Line &b){\n return equal(cross(a.b-a.a,b.b-b.a),0);\n }\n //点cが直線ab上にあるか\n bool isPointOnLine(const point &a,const point &b, const point &c){\n return isParaleel(Line(a,b),Line(a,c));\n }\n //点cが線分ab上にあるか\n bool isPointOnSegment(const point &a, const point &b, const point &c){\n //|a-c|+|c-b|<=|a-b|なら線分上\n return (abs(a-c)+abs(c-b)<abs(a-b)+eps);\n }\n //直線lと点pのキョリ\n ld distanceBetweenLineAndPoint(const Line &l, const point &p){\n return abs(cross(l.b-l.a,p-l.a))/abs(l.b-l.a);\n }\n //線分lと点pのキョリを求める\n //点pから線分lのどこかへの最短距離\n ld distanceBetweenSegmentAndPoint(const Segment &l, const point &p){\n if(dot(l.b-l.a,p-l.a)<eps)return abs(p-l.a);\n if(dot(l.a-l.b,p-l.b)<eps)return abs(p-l.b);\n return abs(cross(l.b-l.a,p-l.a))/abs(l.b-l.a);\n }\n //直線s,tの交点\n point crossPoint(const Line &s, const Line &t){\n ld d1=cross(s.b-s.a,t.b-t.a);\n ld d2=cross(s.b-s.a,s.b-t.a);\n if(equal(abs(d1),0) and equal(abs(d2),0))return t.a;\n return t.a+(t.b-t.a)*(d2/d1);\n }\n //線分s,tの交点\n point crossPoint(const Segment &s,const Segment &t){\n return crossPoint(Line(s),Line(t));\n }\n //線分sと線分tが交差しているか\n //bound:線分の境界を含むか\n bool isIntersect(const Segment &s, const Segment &t, bool bound){\n return ccw(s.a,s.b,t.a)*ccw(s.a,s.b,t.b)<bound and\n ccw(t.a,t.b,s.a)*ccw(t.a,t.b,s.b)<bound;\n }\n //線分s,t間のキョリ\n ld distanceBetweenSegments(const Segment &s,const Segment &t){\n if(isIntersect(s,t,1))return (ld)(0);\n ld ans=distanceBetweenSegmentAndPoint(s,t.a);\n ans=min(ans,distanceBetweenSegmentAndPoint(s,t.b));\n ans=min(ans,distanceBetweenSegmentAndPoint(t,s.a));\n ans=min(ans,distanceBetweenSegmentAndPoint(t,s.b));\n return ans;\n }\n //射影(projection)\n //直線(線分)lに点pから引いた垂線の足を求める\n point projection(const Line &l, const point &p){\n ld t=dot(p-l.a,l.a-l.b)/norm(l.a-l.b);\n return l.a+(l.a-l.b)*t;\n }\n point projection(const Segment &l, const point &p){\n ld t=dot(p-l.a,l.a-l.b)/norm(l.a-l.b);\n return l.a+(l.a-l.b)*t;\n }\n //反射(reflection)\n //直線lを対象軸として点pと線対称の位置にある点\n point reflection(const Line &l, const point &p){\n return p+(projection(l,p)-p)*(ld)2.0;\n }\n //2円の交差判定\n //返り値は共通接戦の数\n int isIntersect(const circle &c1, const circle &c2){\n ld d=abs(c1.p-c2.p);\n //2円が離れているとき\n if(d>c1.r+c2.r+eps)return 4;\n //外接しているとき\n if(equal(d,c1.r+c2.r))return 3;\n //内接しているとき\n if(equal(d,abs(c1.r-c2.r)))return 1;\n //内包しているとき\n if(d<abs(c1.r-c2.r)-eps)return 0;\n return 2;\n }\n //2円の交点\n vector<point> crossPoint(const circle &c1, const circle &c2){\n vector<point>ret;\n int mode=isIntersect(c1,c2);\n //2円の中心間のキョリ\n ld d=abs(c1.p-c2.p);\n //2円が離れているとき\n if(mode==4)return ret;\n //一方の円が他方の円に内包されているとき\n if(mode==0)return ret;\n //2円が外接するとき\n if(mode==3){\n ld t=c1.r/(c1.r+c2.r);\n ret.emplace_back(c1.p+(c2.p-c1.p)*t);\n return ret;\n }\n //内接しているとき\n if(mode==1){\n if(c2.r<c1.r-eps){\n ret.emplace_back(c1.p+(c2.p-c1.p)*(c1.r/d));\n }else{\n ret.emplace_back(c2.p+(c1.p-c2.p)*(c2.r/d));\n }\n return ret;\n }\n //2円が重なるとき\n ld rc1=(c1.r*c1.r+d*d-c2.r*c2.r)/(2*d);\n ld rs1=sqrt(c1.r*c1.r-rc1*rc1);\n if(c1.r-abs(rc1)<eps)rs1=0;\n point e12=(c2.p-c1.p)/abs(c2.p-c1.p);\n ret.emplace_back(c1.p+rc1*e12+rs1*e12*point(0,1));\n ret.emplace_back(c1.p+rc1*e12+rs1*e12*point(0,-1));\n return ret;\n }\n //点pが円cの内部(円周上も含む)に入っているかどうか\n bool isIncircle(const circle &c, const point &p){\n ld d=abs(c.p-p);\n return (equal(d,c.r) or d<c.r-eps);\n }\n //円cと直線lの交点\n vector<point> crossPoint(const circle &c, const Line &l){\n vector<point> ret;\n ld d=distanceBetweenLineAndPoint(l,c.p);\n //交点を持たない\n if(d>c.r+eps)return ret;\n //接する\n point h=projection(l,c.p);\n if(equal(d,c.r)){\n ret.emplace_back(h);\n return ret;\n }\n point e=unitVector(l.b-l.a);\n ld ph=sqrt(c.r*c.r-d*d);\n ret.emplace_back(h-e*ph);\n ret.emplace_back(h+e*ph);\n return ret;\n }\n //点pを通る円cの接線\n //2本あるので接点のみを返す\n vector<point> tangentToCircle(const point &p, const circle &c){\n return crossPoint(c,circle(p,sqrt(norm(c.p-p)-c.r*c.r)));\n }\n //円の共通接線\n vector<Line> tangent(const circle &a, const circle &b){\n vector<Line>ret;\n //2円の中心間のキョリ\n ld g=abs(a.p-b.p);\n if(equal(g,0))return ret;\n point u=unitVector(b.p-a.p);\n point v=rotate(u,PI/2);\n for(int s : {-1,1}){\n ld h=(a.r+b.r*s)/g;\n if(equal(h*h,1)){\n ret.emplace_back(a.p+(h>0 ? u:-u)*a.r,\n a.p+(h>0 ? u:-u)*a.r+v);\n }else if(1-h*h>0){\n point U=u*h,W=v*sqrt(1-h*h);\n ret.emplace_back(a.p+(U+W)*a.r,\n b.p-(U+W)*(b.r*s));\n ret.emplace_back(a.p+(U-W)*a.r,\n b.p-(U-W)*(b.r*s));\n \n }\n }\n return ret;\n }\n //多角形の面積を求める\n ld polygonArea(const vector<point> &p){\n ld ret=0;\n int n=p.size();\n for(int i=0; i<n-1; i++)ret+=cross(p[i],p[i+1]);\n ret+=cross(p[n-1],p[0]);\n return ret*0.5;\n }\n //凸多角形かどうか\n bool isConvex(const vector<point> &p){\n int n=p.size();\n int now,pre,nxt;\n for(int i=0;i<n;i++){\n pre=(i-1+n)%n;\n nxt=(i+n)%n;\n now=i;\n if(ccw(p[pre],p[now],p[nxt])==-1)return false;\n }\n return true;\n }\n //凸包O(NlogN)\n vector<point> ConvexHull(vector<point> p){\n int n=(int)p.size(),k=0;\n sort(p.begin(),p.end(),[](const point &a, const point &b){\n return (a.real()!=b.real() ? a.real()<b.real() : a.imag()<b.imag());\n });\n vector<point> ch(2*n);\n //一直線上の3点を含める->(<eps)\n //含めない->(<eps)\n for(int i=0; i<n; ch[k++]=p[i++]){\n while(k>=2 and \n cross(ch[k-1]-ch[k-2],p[i]-ch[k-1])<eps)k--;\n }\n for(int i=n-2,t=k+1;i>=0;ch[k++]=p[i--]){\n while(k>=t and \n cross(ch[k-1]-ch[k-2],p[i]-ch[k-1])<eps)k--;\n }\n ch.resize(k-1);\n return ch;\n }\n //多角形gに点pが含まれているか\n //含まれる:2,辺上にある:1,含まれない:0\n int isContained(const vector<point> &g, const point &p){\n bool in =false;\n int n=(int)g.size();\n for(int i=0;i<n;i++){\n point a=g[i]-p,b=g[(i+1)%n]-p;\n if(imag(a)>imag(b))swap(a,b);\n if(imag(a)<=eps and eps<imag(b) and cross(a,b)<-eps)in=!in;\n if(cross(a,b)==0 and dot(a,b)<=0)return 1;\n }\n return (in ? 2 : 0);\n }\n // 凸多角形pを直線lで切断し、その左側を返す\n vector<point> convexCut(vector<point> p, Line l){\n vector<point> ret;\n int sz=(int)p.size();\n for(int i=0;i<sz;i++){\n point now=p[i];\n point nxt=p[i==sz-1 ? 0 : i+1];\n if(ccw(l.a,l.b,now)!=-1)ret.emplace_back(now);\n if(ccw(l.a,l.b,now)*ccw(l.a,l.b,nxt)<0){\n ret.emplace_back(crossPoint(Line(now,nxt),l));\n }\n }\n return ret;\n }\n}\n\nusing namespace geometry;\n\n\nll f(vector<point> &po, Line li,vector<ld>& r, vector<ld>& m,int n){\n ll re=0;\n rep(i,n){\n auto dis=distanceBetweenLineAndPoint(li,po[i]);\n if(r[i]<=dis+eps and dis<=r[i]+m[i]+eps)re++;\n }\n return re;\n}\n\n\n\nint main(){\n int n;\n while(cin >> n){\n if(n==0)break;\n //int n;\n vector<ld> r(n),m(n);\n vector<point> po(n);\n\n rep(i,n){\n long double x,y;\n cin >> x >> y >> r[i] >> m[i];\n po[i]=point(x,y);\n }\n if(n==1){\n cout << 1 << endl;\n continue;\n }\n ll ma=-1;\n rep(i,n)rep2(j,i+1,n){\n //金属金属\n circle a,b;\n vector<Line> kyotusessenn;\n a.p=po[i],a.r=r[i];\n b.p=po[j],b.r=r[j];\n kyotusessenn=tangent(a,b);\n fore(e,kyotusessenn){\n chmax(ma,f(po,e,r,m,n));\n }\n //金属磁力\n a.p=po[i],a.r=r[i];\n b.p=po[j],b.r=r[j]+m[j];\n kyotusessenn=tangent(a,b);\n fore(e,kyotusessenn){\n chmax(ma,f(po,e,r,m,n));\n }\n //磁力金属\n a.p=po[i],a.r=r[i]+m[i];\n b.p=po[j],b.r=r[j];\n kyotusessenn=tangent(a,b);\n fore(e,kyotusessenn){\n chmax(ma,f(po,e,r,m,n));\n }\n //磁力磁力\n a.p=po[i],a.r=r[i]+m[i];\n b.p=po[j],b.r=r[j]+m[j];\n kyotusessenn=tangent(a,b);\n fore(e,kyotusessenn){\n chmax(ma,f(po,e,r,m,n));\n }\n }\n cout << ma << endl;\n }\n\n \n}", "accuracy": 1, "time_ms": 460, "memory_kb": 3840, "score_of_the_acc": -0.3626, "final_rank": 14 }, { "submission_id": "aoj_2201_9350682", "code_snippet": "#line 1 \"2201.test.cpp\"\n#define PROBLEM \"https://onlinejudge.u-aizu.ac.jp/problems/2201\"\n\n#line 2 \"/home/zawatin/compro/cp-documentation/Src/Template/IOSetting.hpp\"\n\n#line 2 \"/home/zawatin/compro/cp-documentation/Src/Template/TypeAlias.hpp\"\n\n#include <cstdint>\n#include <cstddef>\n\nnamespace zawa {\n\nusing i16 = std::int16_t;\nusing i32 = std::int32_t;\nusing i64 = std::int64_t;\nusing i128 = __int128_t;\n\nusing u8 = std::uint8_t;\nusing u16 = std::uint16_t;\nusing u32 = std::uint32_t;\nusing u64 = std::uint64_t;\n\nusing usize = std::size_t;\n\n} // namespace zawa\n#line 4 \"/home/zawatin/compro/cp-documentation/Src/Template/IOSetting.hpp\"\n\n#include <iostream>\n#include <iomanip>\n\nnamespace zawa {\n\nvoid SetFastIO() {\n std::cin.tie(nullptr)->sync_with_stdio(false);\n}\n\nvoid SetPrecision(u32 dig) {\n std::cout << std::fixed << std::setprecision(dig);\n}\n\n} // namespace zawa\n#line 2 \"/home/zawatin/compro/cp-documentation/Src/GeometryR2/Real.hpp\"\n\n#line 4 \"/home/zawatin/compro/cp-documentation/Src/GeometryR2/Real.hpp\"\n\n#include <cmath>\n#include <cassert>\n\nnamespace zawa {\n\nnamespace geometryR2 {\n\nusing Real = long double;\n\nnamespace internal {\n\nReal EPS{1e-12};\nconstexpr i32 negative{-1};\nconstexpr i32 zero{};\nconstexpr i32 positive{1};\n\n} // namespace internal\n\nReal& Eps() {\n return internal::EPS;\n}\n\ni32 Sign(Real value) {\n if (value < -Eps()) return internal::negative;\n if (value > Eps()) return internal::positive;\n return internal::zero;\n}\n\nbool Zero(Real value) {\n return Sign(value) == internal::zero;\n}\n\nbool Positive(Real value) {\n return Sign(value) == internal::positive;\n}\n\nbool Negative(Real value) {\n return Sign(value) == internal::negative;\n}\n\nbool Equal(Real a, Real b) {\n return Zero(a - b);\n}\n\nbool Smaller(Real a, Real b) {\n return Negative(a - b);\n}\n\nbool Bigger(Real a, Real b) {\n return Positive(a - b);\n}\n\nReal Square(Real value) {\n return (Zero(value) ? value : value * value);\n}\n\nReal Sqrt(Real value) {\n assert(!Negative(value));\n return (Zero(value) ? value : sqrtl(value));\n}\n\nReal Abs(Real value) {\n return (Negative(value) ? -value : value);\n}\n\n} // namespace geometryR2\n \n} // namespace zawa\n#line 2 \"/home/zawatin/compro/cp-documentation/Src/GeometryR2/Point.hpp\"\n\n#line 2 \"/home/zawatin/compro/cp-documentation/Src/GeometryR2/Angle.hpp\"\n\n#line 4 \"/home/zawatin/compro/cp-documentation/Src/GeometryR2/Angle.hpp\"\n\n#line 6 \"/home/zawatin/compro/cp-documentation/Src/GeometryR2/Angle.hpp\"\n\nnamespace zawa {\n\nnamespace geometryR2 {\n\nconstexpr Real PI{acosl(-1)};\nconstexpr Real TAU{static_cast<Real>(2) * PI};\n\nconstexpr Real ArcToRadian(Real arc) {\n return (arc * PI) / static_cast<Real>(180);\n}\n\nconstexpr Real RadianToArc(Real radian) {\n return (radian * static_cast<Real>(180)) / PI;\n}\n\n} // namespace geometryR2\n\n} // namespace zawa\n#line 5 \"/home/zawatin/compro/cp-documentation/Src/GeometryR2/Point.hpp\"\n\n#line 9 \"/home/zawatin/compro/cp-documentation/Src/GeometryR2/Point.hpp\"\n\nnamespace zawa {\n\nnamespace geometryR2 {\n\nclass Point {\nprivate:\n Real x_{}, y_{};\npublic:\n /* constructor */\n Point() = default;\n Point(Real x, Real y) : x_{x}, y_{y} {}\n\n /* getter, setter */\n Real x() const {\n return x_;\n }\n Real& x() {\n return x_;\n }\n Real y() const {\n return y_;\n }\n Real& y() {\n return y_;\n }\n\n /* operator */\n Point& operator+=(const Point& rhs) {\n x_ += rhs.x();\n y_ += rhs.y();\n return *this;\n }\n friend Point operator+(const Point& lhs, const Point& rhs) {\n return Point{lhs} += rhs;\n }\n Point operator+() const {\n return *this;\n }\n Point& operator-=(const Point& rhs) {\n x_ -= rhs.x();\n y_ -= rhs.y();\n return *this;\n }\n friend Point operator-(const Point& lhs, const Point& rhs) {\n return Point{lhs} -= rhs;\n }\n Point operator-() const {\n return Point{} - *this;\n }\n Point& operator*=(Real k) {\n x_ *= k;\n y_ *= k;\n return *this;\n }\n friend Point operator*(Real k, const Point& p) {\n return Point{p} *= k;\n }\n friend Point operator*(const Point& p, Real k) {\n return Point{p} *= k;\n }\n Point& operator/=(Real k) {\n assert(!Zero(k));\n x_ /= k;\n y_ /= k;\n return *this;\n }\n friend Point operator/(Real k, const Point& p) {\n return Point{p} /= k;\n }\n friend Point operator/(const Point& p, Real k) {\n return Point{p} /= k;\n }\n friend bool operator==(const Point& lhs, const Point& rhs) {\n return Equal(lhs.x(), rhs.x()) and Equal(lhs.y(), rhs.y());\n }\n friend bool operator!=(const Point& lhs, const Point& rhs) {\n return !Equal(lhs.x(), rhs.x()) or !Equal(lhs.y(), rhs.y());\n }\n friend bool operator<(const Point& lhs, const Point& rhs) {\n return Smaller(lhs.x(), rhs.x()) or \n (Equal(lhs.x(), rhs.x()) and Smaller(lhs.y(), rhs.y()));\n }\n friend bool operator<=(const Point& lhs, const Point& rhs) {\n return Smaller(lhs.x(), rhs.x()) or \n (Equal(lhs.x(), rhs.x()) and (Smaller(lhs.y(), rhs.y()) or Equal(lhs.y(), rhs.y())));\n }\n friend bool operator>(const Point& lhs, const Point& rhs) {\n return Bigger(lhs.x(), rhs.x()) or\n (Equal(lhs.x(), rhs.x()) and Bigger(lhs.y(), rhs.y()));\n }\n friend bool operator>=(const Point& lhs, const Point& rhs) {\n return Bigger(lhs.x(), rhs.x()) or\n (Equal(lhs.x(), rhs.x()) and (Bigger(lhs.y(), rhs.y()) or Equal(lhs.y(), rhs.y())));\n }\n friend std::istream& operator>>(std::istream& is, Point& p) {\n is >> p.x_ >> p.y_;\n return is;\n }\n friend std::ostream& operator<<(std::ostream& os, const Point& p) {\n os << '(' << p.x_ << ',' << p.y_ << ')';\n return os;\n }\n \n /* member function */\n Real normSquare() const {\n return Square(x_) + Square(y_);\n }\n Real norm() const {\n return Sqrt(normSquare());\n }\n void normalize() {\n assert((*this) != Point{});\n (*this) /= norm(); \n }\n Point normalized() const {\n Point res{*this};\n res.normalize();\n return res;\n }\n Point rotated(Real radian) const {\n return Point{\n x_ * cosl(radian) - y_ * sinl(radian),\n x_ * sinl(radian) + y_ * cosl(radian)\n };\n }\n void rotate(Real radian) {\n *this = rotated(radian); \n }\n Point rotatedByArc(Real arc) const {\n return rotated(ArcToRadian(arc));\n }\n void rotateByArc(Real arc) {\n *this = rotatedByArc(arc);\n }\n Real argument() const {\n return (Negative(y_) ? TAU : static_cast<Real>(0)) + atan2l(y_, x_);\n }\n Real argumentByArc() const {\n return RadianToArc(argument());\n }\n\n /* friend function */\n friend Real Dot(const Point& lhs, const Point& rhs) {\n return lhs.x() * rhs.x() + lhs.y() * rhs.y();\n }\n friend Real Cross(const Point& lhs, const Point& rhs) {\n return lhs.x() * rhs.y() - lhs.y() * rhs.x();\n }\n friend Real Argument(const Point& lhs, const Point& rhs) {\n return rhs.argument() - lhs.argument();\n }\n friend bool ArgComp(const Point& lhs, const Point& rhs) {\n return Smaller(lhs.argument(), rhs.argument());\n }\n};\n\nusing Vector = Point;\n\n} // namespace geometryR2\n\n} // namespace zawa\n#line 2 \"/home/zawatin/compro/cp-documentation/Src/GeometryR2/Line.hpp\"\n\n#line 2 \"/home/zawatin/compro/cp-documentation/Src/GeometryR2/Relation.hpp\"\n\n#line 5 \"/home/zawatin/compro/cp-documentation/Src/GeometryR2/Relation.hpp\"\n\nnamespace zawa {\n\nnamespace geometryR2 {\n\nenum RELATION {\n // p0 -> p1 -> p2の順で直線上に並んでいる\n ONLINE_FRONT = -2,\n // (p1 - p0) -> (p2 - p0)が時計回りになっている\n CLOCKWISE,\n // p0 -> p2 -> p1の順で直線上に並んでいる\n ON_SEGMENT,\n // (p1 - p0) -> (p2 - p0)が反時計回りになっている\n COUNTER_CLOCKWISE,\n // p2 -> p0 -> p1、またはp1 -> p0 -> p2の順で直線上に並んでいる\n ONLINE_BACK\n};\n\nRELATION Relation(const Point& p0, const Point& p1, const Point& p2) {\n Point a{p1 - p0}, b{p2 - p0};\n if (Positive(Cross(a, b))) return COUNTER_CLOCKWISE;\n if (Negative(Cross(a, b))) return CLOCKWISE;\n if (Negative(Dot(a, b))) return ONLINE_BACK;\n if (Smaller(a.normSquare(), b.normSquare())) return ONLINE_FRONT;\n return ON_SEGMENT;\n};\n\n} // namespace geometryR2\n\n} // namespace zawa\n#line 5 \"/home/zawatin/compro/cp-documentation/Src/GeometryR2/Line.hpp\"\n\n#line 7 \"/home/zawatin/compro/cp-documentation/Src/GeometryR2/Line.hpp\"\n\nnamespace zawa {\n\nnamespace geometryR2 {\n\nclass Line {\nprivate:\n Point p0_{}, p1_{};\npublic:\n /* constructor */\n Line() = default;\n Line(const Point& p0, const Point& p1) : p0_{p0}, p1_{p1} {}\n // y = ax + b \n Line(Real a, Real b) : p0_{static_cast<Real>(0), b}, p1_{static_cast<Real>(1), a + b} {}\n\n /* getter, setter */\n const Point& p0() const {\n return p0_;\n }\n Point& p0() {\n return p0_;\n }\n const Point& p1() const {\n return p1_;\n }\n Point& p1() {\n return p1_;\n }\n\n /* operator */\n friend bool operator==(const Line& l0, const Line& l1) {\n return Zero(Cross(l0.p1() - l0.p0(), l1.p1() - l1.p0())) and Zero(Cross(l0.p1() - l0.p0(), l1.p1() - l0.p0()));\n }\n friend bool operator!=(const Line& l0, const Line& l1) {\n return !Zero(Cross(l0.p1() - l0.p0(), l1.p1() - l1.p0())) or !Zero(Cross(l0.p1() - l0.p0(), l1.p1() - l0.p0()));\n }\n\n /* member function */\n bool valid() const {\n return p0_ != p1_;\n }\n Vector slope() const {\n assert(valid());\n return Vector{p1() - p0()}.normalized();\n }\n};\n\n} // namespace geometryR2\n\n} // namespace zawa\n#line 2 \"/home/zawatin/compro/cp-documentation/Src/GeometryR2/Circle.hpp\"\n\n#line 2 \"/home/zawatin/compro/cp-documentation/Src/GeometryR2/Distance/PointAndPoint.hpp\"\n\n#line 4 \"/home/zawatin/compro/cp-documentation/Src/GeometryR2/Distance/PointAndPoint.hpp\"\n\nnamespace zawa {\n\nnamespace geometryR2 {\n\nReal Distance(const Point& p0, const Point& p1) {\n return Point{p1 - p0}.norm();\n}\n\nReal DistanceSquare(const Point& p0, const Point& p1) {\n return Point{p1 - p0}.normSquare();\n}\n\n} // namespace geometryR2\n\n} // namespace zawa\n#line 7 \"/home/zawatin/compro/cp-documentation/Src/GeometryR2/Circle.hpp\"\n\n#line 9 \"/home/zawatin/compro/cp-documentation/Src/GeometryR2/Circle.hpp\"\n#include <utility>\n\nnamespace zawa {\n\nnamespace geometryR2 {\n\nclass Circle {\nprivate:\n Point center_{};\n Real radius_{};\npublic:\n /* constructor */\n Circle() = default;\n Circle(const Point& center, Real radius) : center_{center}, radius_{radius} {\n assert(!Negative(radius));\n }\n Circle(Real x, Real y, Real r) : center_{x, y}, radius_{r} {\n assert(!Negative(r));\n }\n\n /* getter setter */\n const Point& center() const {\n return center_;\n }\n Point& center() {\n return center_;\n }\n Real radius() const {\n return radius_;\n }\n Real& radius() {\n return radius_;\n }\n\n /* operator */\n friend bool operator==(const Circle& lhs, const Circle& rhs) {\n return lhs.center() == rhs.center() and Equal(lhs.radius(), rhs.radius());\n }\n friend bool operator!=(const Circle& lhs, const Circle& rhs) {\n return lhs.center() != rhs.center() or !Equal(lhs.radius(), rhs.radius());\n }\n\n /* friend function */\n friend u32 NumberCommonTangent(const Circle& c0, const Circle& c1) {\n Real dist{DistanceSquare(c0.center(), c1.center())};\n Real down{Square(Abs(c0.radius() - c1.radius()))};\n if (Smaller(dist, down)) return 0;\n if (Equal(dist, down)) return 1;\n Real up{Square(c0.radius() + c1.radius())};\n if (Smaller(dist, up)) return 2;\n if (Equal(dist, up)) return 3;\n return 4;\n }\n};\n\n} // namespace geometryR2\n\n} // namespace zawa\n#line 2 \"/home/zawatin/compro/cp-documentation/Src/GeometryR2/Distance/LineAndPoint.hpp\"\n\n#line 7 \"/home/zawatin/compro/cp-documentation/Src/GeometryR2/Distance/LineAndPoint.hpp\"\n\n#line 9 \"/home/zawatin/compro/cp-documentation/Src/GeometryR2/Distance/LineAndPoint.hpp\"\n\nnamespace zawa {\n\nnamespace geometryR2 {\n\nReal Distance(const Line& l, const Point& p) {\n assert(l.valid());\n return Abs(Cross(p - l.p0(), l.p1() - l.p0())) / Distance(l.p1(), l.p0());\n}\n\nbool PointOnLine(const Line& l, const Point& p) {\n assert(l.valid());\n return Zero(Distance(l, p));\n}\n\n} // namespace geometryR2\n\n} // namespace zawa\n#line 2 \"/home/zawatin/compro/cp-documentation/Src/GeometryR2/Tangent/CommonTangentBetweenCircles.hpp\"\n\n#line 8 \"/home/zawatin/compro/cp-documentation/Src/GeometryR2/Tangent/CommonTangentBetweenCircles.hpp\"\n\n#include <vector>\n\nnamespace zawa {\n\nnamespace geometryR2 {\n\n std::vector<Line> CommonTangentBetweenCircles(const Circle& c0, const Circle& c1) {\n std::vector<Line> res;\n if (c0.center() == c1.center()) {\n return res; \n }\n res.reserve(4);\n Real g{Distance(c0.center(), c1.center())};\n Vector u{Vector{c1.center() - c0.center()}.normalized()};\n Vector v{u.rotated(PI / Real{2})};\n for (i32 s : { -1, 1 }) {\n Real h{(c0.radius() + c1.radius() * s) / g};\n if (Equal(Square(h), Real{1})) {\n res.emplace_back(c0.center() + (Positive(h) ? u : -u) * c0.radius(),\n c0.center() + (Positive(h) ? u : -u) * c0.radius() + v);\n \n }\n else if (Smaller(Square(h), Real{1})) {\n Point U{u * h}, V{v * Sqrt(Real{1} - Square(h))};\n res.emplace_back(c0.center() + (U + V) * c0.radius(),\n c1.center() - (U + V) * (c1.radius() * s));\n res.emplace_back(c0.center() + (U - V) * c0.radius(),\n c1.center() - (U - V) * (c1.radius() * s));\n }\n }\n return res;\n }\n\n} // namespace geometryR2\n\n} // namespace zawa\n#line 10 \"2201.test.cpp\"\n\n#line 12 \"2201.test.cpp\"\n\nusing namespace zawa;\nusing namespace geometryR2;\n\nbool solve() {\n int N;\n std::cin >> N;\n if (N == 0) return false;\n std::vector<Point> P(N);\n std::vector<Real> R(N), M(N);\n for (int i{} ; i < N ; i++) {\n std::cin >> P[i] >> R[i] >> M[i];\n }\n auto check{[&](const Line& L) -> int {\n int res{};\n for (int i{} ; i < N ; i++) {\n Real d{Distance(L, P[i])};\n if (Smaller(d, R[i])) continue;\n if (Bigger(d, R[i] + M[i])) continue;\n res++;\n }\n return res;\n }};\n int ans{1};\n for (int i{} ; i < N ; i++) {\n for (int j{i + 1} ; j < N ; j++) {\n for (int bit{} ; bit < 4 ; bit++) {\n Circle lhs{P[i], R[i] + ((bit & (1 << 0)) ? M[i] : Real{})};\n Circle rhs{P[j], R[j] + ((bit & (1 << 1)) ? M[j] : Real{})};\n for (const Line& L : CommonTangentBetweenCircles(lhs, rhs)) {\n ans = std::max(ans, check(L));\n }\n }\n }\n }\n std::cout << ans << '\\n';\n return true;\n}\n\nint main() {\n SetFastIO();\n while (solve()) ;\n}", "accuracy": 1, "time_ms": 220, "memory_kb": 3460, "score_of_the_acc": -0.1345, "final_rank": 9 }, { "submission_id": "aoj_2201_9350670", "code_snippet": "#include <bits/stdc++.h>\n\n\n\n\nnamespace zawa {\n\nusing i16 = std::int16_t;\nusing i32 = std::int32_t;\nusing i64 = std::int64_t;\nusing i128 = __int128_t;\n\nusing u8 = std::uint8_t;\nusing u16 = std::uint16_t;\nusing u32 = std::uint32_t;\nusing u64 = std::uint64_t;\n\nusing usize = std::size_t;\n\n} // namespace zawa\n\n\nnamespace zawa {\n\nvoid SetFastIO() {\n std::cin.tie(nullptr)->sync_with_stdio(false);\n}\n\nvoid SetPrecision(u32 dig) {\n std::cout << std::fixed << std::setprecision(dig);\n}\n\n} // namespace zawa\n\n\n\nnamespace zawa {\n\nnamespace geometryR2 {\n\nusing Real = long double;\n\nnamespace internal {\n\nReal EPS{1e-12};\nconstexpr i32 negative{-1};\nconstexpr i32 zero{};\nconstexpr i32 positive{1};\n\n} // namespace internal\n\nReal& Eps() {\n return internal::EPS;\n}\n\ni32 Sign(Real value) {\n if (value < -Eps()) return internal::negative;\n if (value > Eps()) return internal::positive;\n return internal::zero;\n}\n\nbool Zero(Real value) {\n return Sign(value) == internal::zero;\n}\n\nbool Positive(Real value) {\n return Sign(value) == internal::positive;\n}\n\nbool Negative(Real value) {\n return Sign(value) == internal::negative;\n}\n\nbool Equal(Real a, Real b) {\n return Zero(a - b);\n}\n\nbool Smaller(Real a, Real b) {\n return Negative(a - b);\n}\n\nbool Bigger(Real a, Real b) {\n return Positive(a - b);\n}\n\nReal Square(Real value) {\n return (Zero(value) ? value : value * value);\n}\n\nReal Sqrt(Real value) {\n assert(!Negative(value));\n return (Zero(value) ? value : sqrtl(value));\n}\n\nReal Abs(Real value) {\n return (Negative(value) ? -value : value);\n}\n\n} // namespace geometryR2\n \n} // namespace zawa\n\n\n\n\n\nnamespace zawa {\n\nnamespace geometryR2 {\n\nconstexpr Real PI{acosl(-1)};\nconstexpr Real TAU{static_cast<Real>(2) * PI};\n\nconstexpr Real ArcToRadian(Real arc) {\n return (arc * PI) / static_cast<Real>(180);\n}\n\nconstexpr Real RadianToArc(Real radian) {\n return (radian * static_cast<Real>(180)) / PI;\n}\n\n} // namespace geometryR2\n\n} // namespace zawa\n\n\nnamespace zawa {\n\nnamespace geometryR2 {\n\nclass Point {\nprivate:\n Real x_{}, y_{};\npublic:\n /* constructor */\n Point() = default;\n Point(Real x, Real y) : x_{x}, y_{y} {}\n\n /* getter, setter */\n Real x() const {\n return x_;\n }\n Real& x() {\n return x_;\n }\n Real y() const {\n return y_;\n }\n Real& y() {\n return y_;\n }\n\n /* operator */\n Point& operator+=(const Point& rhs) {\n x_ += rhs.x();\n y_ += rhs.y();\n return *this;\n }\n friend Point operator+(const Point& lhs, const Point& rhs) {\n return Point{lhs} += rhs;\n }\n Point operator+() const {\n return *this;\n }\n Point& operator-=(const Point& rhs) {\n x_ -= rhs.x();\n y_ -= rhs.y();\n return *this;\n }\n friend Point operator-(const Point& lhs, const Point& rhs) {\n return Point{lhs} -= rhs;\n }\n Point operator-() const {\n return Point{} - *this;\n }\n Point& operator*=(Real k) {\n x_ *= k;\n y_ *= k;\n return *this;\n }\n friend Point operator*(Real k, const Point& p) {\n return Point{p} *= k;\n }\n friend Point operator*(const Point& p, Real k) {\n return Point{p} *= k;\n }\n Point& operator/=(Real k) {\n assert(!Zero(k));\n x_ /= k;\n y_ /= k;\n return *this;\n }\n friend Point operator/(Real k, const Point& p) {\n return Point{p} /= k;\n }\n friend Point operator/(const Point& p, Real k) {\n return Point{p} /= k;\n }\n friend bool operator==(const Point& lhs, const Point& rhs) {\n return Equal(lhs.x(), rhs.x()) and Equal(lhs.y(), rhs.y());\n }\n friend bool operator!=(const Point& lhs, const Point& rhs) {\n return !Equal(lhs.x(), rhs.x()) or !Equal(lhs.y(), rhs.y());\n }\n friend bool operator<(const Point& lhs, const Point& rhs) {\n return Smaller(lhs.x(), rhs.x()) or \n (Equal(lhs.x(), rhs.x()) and Smaller(lhs.y(), rhs.y()));\n }\n friend bool operator<=(const Point& lhs, const Point& rhs) {\n return Smaller(lhs.x(), rhs.x()) or \n (Equal(lhs.x(), rhs.x()) and (Smaller(lhs.y(), rhs.y()) or Equal(lhs.y(), rhs.y())));\n }\n friend bool operator>(const Point& lhs, const Point& rhs) {\n return Bigger(lhs.x(), rhs.x()) or\n (Equal(lhs.x(), rhs.x()) and Bigger(lhs.y(), rhs.y()));\n }\n friend bool operator>=(const Point& lhs, const Point& rhs) {\n return Bigger(lhs.x(), rhs.x()) or\n (Equal(lhs.x(), rhs.x()) and (Bigger(lhs.y(), rhs.y()) or Equal(lhs.y(), rhs.y())));\n }\n friend std::istream& operator>>(std::istream& is, Point& p) {\n is >> p.x_ >> p.y_;\n return is;\n }\n friend std::ostream& operator<<(std::ostream& os, const Point& p) {\n os << '(' << p.x_ << ',' << p.y_ << ')';\n return os;\n }\n \n /* member function */\n Real normSquare() const {\n return Square(x_) + Square(y_);\n }\n Real norm() const {\n return Sqrt(normSquare());\n }\n void normalize() {\n assert((*this) != Point{});\n (*this) /= norm(); \n }\n Point normalized() const {\n Point res{*this};\n res.normalize();\n return res;\n }\n Point rotated(Real radian) const {\n return Point{\n x_ * cosl(radian) - y_ * sinl(radian),\n x_ * sinl(radian) + y_ * cosl(radian)\n };\n }\n void rotate(Real radian) {\n *this = rotated(radian); \n }\n Point rotatedByArc(Real arc) const {\n return rotated(ArcToRadian(arc));\n }\n void rotateByArc(Real arc) {\n *this = rotatedByArc(arc);\n }\n Real argument() const {\n return (Negative(y_) ? TAU : static_cast<Real>(0)) + atan2l(y_, x_);\n }\n Real argumentByArc() const {\n return RadianToArc(argument());\n }\n\n /* friend function */\n friend Real Dot(const Point& lhs, const Point& rhs) {\n return lhs.x() * rhs.x() + lhs.y() * rhs.y();\n }\n friend Real Cross(const Point& lhs, const Point& rhs) {\n return lhs.x() * rhs.y() - lhs.y() * rhs.x();\n }\n friend Real Argument(const Point& lhs, const Point& rhs) {\n return rhs.argument() - lhs.argument();\n }\n friend bool ArgComp(const Point& lhs, const Point& rhs) {\n return Smaller(lhs.argument(), rhs.argument());\n }\n};\n\nusing Vector = Point;\n\n} // namespace geometryR2\n\n} // namespace zawa\n\n\nnamespace zawa {\n\nnamespace geometryR2 {\n\nReal Distance(const Point& p0, const Point& p1) {\n return Point{p1 - p0}.norm();\n}\n\nReal DistanceSquare(const Point& p0, const Point& p1) {\n return Point{p1 - p0}.normSquare();\n}\n\n} // namespace geometryR2\n\n} // namespace zawa\n\n\nnamespace zawa {\n\nnamespace geometryR2 {\n\nclass Circle {\nprivate:\n Point center_{};\n Real radius_{};\npublic:\n /* constructor */\n Circle() = default;\n Circle(const Point& center, Real radius) : center_{center}, radius_{radius} {\n assert(!Negative(radius));\n }\n Circle(Real x, Real y, Real r) : center_{x, y}, radius_{r} {\n assert(!Negative(r));\n }\n\n /* getter setter */\n const Point& center() const {\n return center_;\n }\n Point& center() {\n return center_;\n }\n Real radius() const {\n return radius_;\n }\n Real& radius() {\n return radius_;\n }\n\n /* operator */\n friend bool operator==(const Circle& lhs, const Circle& rhs) {\n return lhs.center() == rhs.center() and Equal(lhs.radius(), rhs.radius());\n }\n friend bool operator!=(const Circle& lhs, const Circle& rhs) {\n return lhs.center() != rhs.center() or !Equal(lhs.radius(), rhs.radius());\n }\n\n /* friend function */\n friend u32 NumberCommonTangent(const Circle& c0, const Circle& c1) {\n Real dist{DistanceSquare(c0.center(), c1.center())};\n Real down{Square(Abs(c0.radius() - c1.radius()))};\n if (Smaller(dist, down)) return 0;\n if (Equal(dist, down)) return 1;\n Real up{Square(c0.radius() + c1.radius())};\n if (Smaller(dist, up)) return 2;\n if (Equal(dist, up)) return 3;\n return 4;\n }\n};\n\n} // namespace geometryR2\n\n} // namespace zawa\n\n\n\n\nnamespace zawa {\n\nnamespace geometryR2 {\n\nenum RELATION {\n // p0 -> p1 -> p2の順で直線上に並んでいる\n ONLINE_FRONT = -2,\n // (p1 - p0) -> (p2 - p0)が時計回りになっている\n CLOCKWISE,\n // p0 -> p2 -> p1の順で直線上に並んでいる\n ON_SEGMENT,\n // (p1 - p0) -> (p2 - p0)が反時計回りになっている\n COUNTER_CLOCKWISE,\n // p2 -> p0 -> p1、またはp1 -> p0 -> p2の順で直線上に並んでいる\n ONLINE_BACK\n};\n\nRELATION Relation(const Point& p0, const Point& p1, const Point& p2) {\n Point a{p1 - p0}, b{p2 - p0};\n if (Positive(Cross(a, b))) return COUNTER_CLOCKWISE;\n if (Negative(Cross(a, b))) return CLOCKWISE;\n if (Negative(Dot(a, b))) return ONLINE_BACK;\n if (Smaller(a.normSquare(), b.normSquare())) return ONLINE_FRONT;\n return ON_SEGMENT;\n};\n\n} // namespace geometryR2\n\n} // namespace zawa\n\n\nnamespace zawa {\n\nnamespace geometryR2 {\n\nclass Line {\nprivate:\n Point p0_{}, p1_{};\npublic:\n /* constructor */\n Line() = default;\n Line(const Point& p0, const Point& p1) : p0_{p0}, p1_{p1} {}\n // y = ax + b \n Line(Real a, Real b) : p0_{static_cast<Real>(0), b}, p1_{static_cast<Real>(1), a + b} {}\n\n /* getter, setter */\n const Point& p0() const {\n return p0_;\n }\n Point& p0() {\n return p0_;\n }\n const Point& p1() const {\n return p1_;\n }\n Point& p1() {\n return p1_;\n }\n\n /* operator */\n friend bool operator==(const Line& l0, const Line& l1) {\n return Zero(Cross(l0.p1() - l0.p0(), l1.p1() - l1.p0())) and Zero(Cross(l0.p1() - l0.p0(), l1.p1() - l0.p0()));\n }\n friend bool operator!=(const Line& l0, const Line& l1) {\n return !Zero(Cross(l0.p1() - l0.p0(), l1.p1() - l1.p0())) or !Zero(Cross(l0.p1() - l0.p0(), l1.p1() - l0.p0()));\n }\n\n /* member function */\n bool valid() const {\n return p0_ != p1_;\n }\n Vector slope() const {\n assert(valid());\n return Vector{p1() - p0()}.normalized();\n }\n};\n\n} // namespace geometryR2\n\n} // namespace zawa\n\n\nnamespace zawa {\n\nnamespace geometryR2 {\n\n std::vector<Line> CommonTangentBetweenCircles(const Circle& c0, const Circle& c1) {\n std::vector<Line> res;\n if (c0.center() == c1.center()) {\n return res; \n }\n res.reserve(4);\n Real g{Distance(c0.center(), c1.center())};\n Vector u{Vector{c1.center() - c0.center()}.normalized()};\n Vector v{u.rotated(PI / Real{2})};\n for (i32 s : { -1, 1 }) {\n Real h{(c0.radius() + c1.radius() * s) / g};\n if (Equal(Square(h), Real{1})) {\n res.emplace_back(c0.center() + (Positive(h) ? u : -u) * c0.radius(),\n c0.center() + (Positive(h) ? u : -u) * c0.radius() + v);\n \n }\n else if (Smaller(Square(h), Real{1})) {\n Point U{u * h}, V{v * Sqrt(Real{1} - Square(h))};\n res.emplace_back(c0.center() + (U + V) * c0.radius(),\n c1.center() - (U + V) * (c1.radius() * s));\n res.emplace_back(c0.center() + (U - V) * c0.radius(),\n c1.center() - (U - V) * (c1.radius() * s));\n }\n }\n return res;\n }\n\n} // namespace geometryR2\n\n} // namespace zawa\n\n\n\nnamespace zawa {\n\nnamespace geometryR2 {\n\nReal Distance(const Line& l, const Point& p) {\n assert(l.valid());\n return Abs(Cross(p - l.p0(), l.p1() - l.p0())) / Distance(l.p1(), l.p0());\n}\n\nbool PointOnLine(const Line& l, const Point& p) {\n assert(l.valid());\n return Zero(Distance(l, p));\n}\n\n} // namespace geometryR2\n\n} // namespace zawa\nusing namespace zawa;\nusing namespace geometryR2;\n\nbool solve() {\n int N;\n std::cin >> N;\n if (N == 0) return false;\n std::vector<Circle> C(2 * N);\n std::vector<Real> R(N), M(N);\n for (int i{} ; i < N ; i++) {\n Point c;\n Real r, m;\n std::cin >> c >> r >> m;\n R[i] = r;\n M[i] = m;\n C[2 * i + 0] = Circle{ c, r };\n C[2 * i + 1] = Circle{ c, r + m };\n }\n if (N == 1) {\n std::cout << 1 << '\\n';\n return true;\n }\n auto check{[&](const Line& L, int oki, int okj) -> int {\n int res{};\n for (int i{} ; i < N ; i++) {\n if (i == oki or i == okj) {\n res++;\n continue;\n }\n Real d{Distance(L, C[2 * i].center())}; \n if (Smaller(d, R[i])) continue;\n if (Bigger(d, R[i] + M[i])) continue;\n res++;\n }\n return res;\n }};\n int ans{};\n for (int i{} ; i < N ; i++) {\n for (int j{i + 1} ; j < N ; j++) {\n for (int bit{} ; bit < 4 ; bit++) {\n Circle lhs{(bit & (1 << 0)) ? C[2 * i + 1] : C[2 * i + 0]};\n Circle rhs{(bit & (1 << 1)) ? C[2 * j + 1] : C[2 * j + 0]};\n for (const auto& L : CommonTangentBetweenCircles(lhs, rhs)) {\n ans = std::max(ans, check(L, i, j));\n }\n }\n }\n }\n std::cout << ans << '\\n';\n return true;\n}\n\nint main() {\n SetFastIO();\n\n while (solve()) ;\n}", "accuracy": 1, "time_ms": 210, "memory_kb": 3400, "score_of_the_acc": -0.1126, "final_rank": 8 }, { "submission_id": "aoj_2201_7953287", "code_snippet": "#include <string.h>\n\n#include <algorithm>\n#include <array>\n#include <bitset>\n#include <cassert>\n#include <cfloat>\n#include <chrono>\n#include <ciso646>\n#include <climits>\n#include <cmath>\n#include <complex>\n#include <cstdio>\n#include <functional>\n#include <iomanip>\n#include <iostream>\n#include <map>\n#include <numeric>\n#include <optional>\n#include <queue>\n#include <random>\n#include <set>\n#include <stack>\n#include <string>\n#include <unordered_map>\n#include <unordered_set>\n#include <utility>\n#include <vector>\n\n#define REP_OVERLOAD(arg1, arg2, arg3, arg4, NAME, ...) NAME\n#define REP3(i, l, r, s) \\\n for (int i = int(l), rep3_r = int(r), rep3_s = int(s); i < rep3_r; \\\n i += rep3_s)\n#define REP2(i, l, r) REP3(i, l, r, 1)\n#define REP1(i, n) REP2(i, 0, n)\n#define rep(...) REP_OVERLOAD(__VA_ARGS__, REP3, REP2, REP1, )(__VA_ARGS__)\n#define repin(i, l, r) for (int i = int(l), repin_r = int(r); i <= repin_r; ++i)\n\n#define RREP_OVERLOAD(arg1, arg2, arg3, arg4, NAME, ...) NAME\n#define RREP3(i, l, r, s) \\\n for (int i = int(r) - 1, rrep3_l = int(l), rrep3_s = int(s); i >= rrep3_l; \\\n i -= rrep3_s)\n#define RREP2(i, l, r) RREP3(i, l, r, 1)\n#define RREP1(i, n) RREP2(i, 0, n)\n#define rrep(...) RREP_OVERLOAD(__VA_ARGS__, RREP3, RREP2, RREP1, )(__VA_ARGS__)\n#define rrepin(i, l, r) \\\n for (int i = int(r), rrepin_l = int(l); i >= rrepin_l; --i)\n\n\n#include <algorithm>\n#include <cmath>\n#include <iostream>\n\n#include <algorithm>\n#include <cassert>\n#include <vector>\n\nnamespace rklib {\n\n// a <- max(a, b)\ntemplate <class T>\nbool chmax(T &a, const T &b) {\n if (a < b) {\n a = b;\n return true;\n }\n return false;\n}\n\n// a <- min(a, b)\ntemplate <class T>\nbool chmin(T &a, const T &b) {\n if (a > b) {\n a = b;\n return true;\n }\n return false;\n}\n\n// if a < 0: a <- b\n// else: a <- min(a, b)\ntemplate <class T>\nbool chmin_non_negative(T &a, const T &b) {\n if (a < 0 || a > b) {\n a = b;\n return true;\n }\n return false;\n}\n\n// floor(num / den)\ntemplate <class T>\nT div_floor(T num, T den) {\n if (den < 0) num = -num, den = -den;\n return num >= 0 ? num / den : (num + 1) / den - 1;\n}\n\n// ceil(num / den)\ntemplate <class T>\nT div_ceil(T num, T den) {\n if (den < 0) num = -num, den = -den;\n return num <= 0 ? num / den : (num - 1) / den + 1;\n}\n\nnamespace internal {\n\ntemplate <class T>\nT remainder_count(T r, T b, T m) {\n return r / m * b + std::min(b, r % m);\n}\n\n} // namespace internal\n\n// Number of integer x s.t.\n// - x in [l, r)\n// - x mod m in [a, b)\ntemplate <class T>\nT remainder_count(T l, T r, T a, T b, T m) {\n assert(l >= 0);\n assert(m >= 1);\n\n if (l >= r || a >= b) return 0;\n if (m <= a || b < 0) return 0;\n chmax(a, T(0));\n chmin(b, m);\n\n auto res = internal::remainder_count(r, b, m);\n if (l >= 1) res -= internal::remainder_count(l, b, m);\n if (a >= 1) res -= internal::remainder_count(r, a, m);\n if (l >= 1 && a >= 1) res += internal::remainder_count(l, a, m);\n\n return res;\n}\n\n} // namespace rklib\n\n#include <vector>\n\nnamespace rklib {\n\nnamespace geo2d {\n\nusing real_num = long double;\nreal_num eps = 1e-9;\nconstexpr real_num PI = 3.14159265358979323846264338327950;\n\nvoid set_eps(real_num new_eps) { eps = new_eps; }\n\nint sgn(real_num x) {\n if (x < -eps) return -1;\n if (x > eps) return 1;\n return 0;\n}\n\nbool eq(real_num x, real_num y) { return sgn(x - y) == 0; }\n\nbool ge(real_num x, real_num y) { return sgn(x - y) == 1; }\n\nbool le(real_num x, real_num y) { return sgn(x - y) == -1; }\n\nbool geq(real_num x, real_num y) { return sgn(x - y) >= 0; }\n\nbool leq(real_num x, real_num y) { return sgn(x - y) <= 0; }\n\nstruct Point {\n real_num x, y;\n\n Point(real_num x = 0, real_num y = 0) : x(x), y(y) {}\n\n bool operator==(const Point& p) { return eq(x, p.x) && eq(y, p.y); }\n bool operator<(const Point& p) const {\n if (eq(x, p.x)) return le(y, p.y);\n return le(x, p.x);\n }\n\n Point& operator+=(const Point& rhs) {\n this->x += rhs.x;\n this->y += rhs.y;\n return *this;\n }\n Point& operator-=(const Point& rhs) {\n this->x -= rhs.x;\n this->y -= rhs.y;\n return *this;\n }\n Point& operator*=(const real_num& rhs) {\n this->x *= rhs;\n this->y *= rhs;\n return *this;\n }\n Point& operator/=(const real_num& rhs) {\n this->x /= rhs;\n this->y /= rhs;\n return *this;\n }\n\n friend Point operator+(const Point& lhs, const Point& rhs) {\n return Point(lhs) += rhs;\n }\n friend Point operator-(const Point& lhs, const Point& rhs) {\n return Point(lhs) -= rhs;\n }\n friend Point operator*(const Point& lhs, const real_num& rhs) {\n return Point(lhs) *= rhs;\n }\n friend Point operator*(const real_num& lhs, const Point& rhs) {\n return Point(rhs) *= lhs;\n }\n friend Point operator/(const Point& lhs, const real_num& rhs) {\n return Point(lhs) /= rhs;\n }\n friend real_num operator*(const Point& lhs, const Point& rhs) {\n return lhs.x * rhs.x + lhs.y * rhs.y;\n }\n friend real_num operator^(const Point& lhs, const Point& rhs) {\n return lhs.x * rhs.y - lhs.y * rhs.x;\n }\n\n friend std::istream& operator>>(std::istream& is, Point& p) {\n is >> p.x >> p.y;\n return is;\n }\n};\n\nusing Vec = Point;\nusing Points = std::vector<Point>;\nusing Polygon = Points;\n\nstruct Line {\n Point a;\n Vec v;\n\n Line() {}\n Line(Point a, Vec v) : a(a), v(v) {}\n};\n\nstruct Segment {\n Point a, b;\n\n Segment() {}\n Segment(Point a, Point b) : a(a), b(b) {}\n};\n\nreal_num norm(Point p) { return p.x * p.x + p.y * p.y; }\n\nreal_num abs(Point p) { return std::sqrt(norm(p)); }\n\nreal_num arg(Point p) { return std::atan2(p.y, p.x); }\n\nreal_num angle(Point a, Point b) { return arg({a * b, a ^ b}); }\n\nPoint rotation(Point p, real_num t) {\n return {p.x * std::cos(t) - p.y * std::sin(t),\n p.x * std::sin(t) + p.y * std::cos(t)};\n}\n\nPoint projection(Line l, Point p) {\n auto [a, v] = l;\n real_num t = v * (p - a) / norm(v);\n return a + v * t;\n}\n\nPoint reflection(Line l, Point p) { return projection(l, p) * 2 - p; }\n\nconstexpr int CCW_COUNTER_CLOCKWISE = 1;\nconstexpr int CCW_CLOCKWISE = -1;\nconstexpr int CCW_ONLINE_BACK = -2; // C->A->B\nconstexpr int CCW_ONLINE_FRONT = 2; // A->B->C\nconstexpr int CCW_ON_SEGMENT = 0; // A->C->B\n\nint ccw(Point a, Point b, Point c) {\n Vec v = b - a, w = c - a;\n if (ge(v ^ w, 0)) return CCW_COUNTER_CLOCKWISE;\n if (le(v ^ w, 0)) return CCW_CLOCKWISE;\n if (le(v * w, 0)) return CCW_ONLINE_BACK;\n if (le((a - b) * (c - b), 0)) return CCW_ONLINE_FRONT;\n return CCW_ON_SEGMENT;\n}\n\nbool is_parallel(Vec v, Vec w) { return eq(v ^ w, 0); }\n\nbool is_orthogonal(Vec v, Vec w) { return eq(v * w, 0); }\n\nbool has_intersection(Line l, Segment s) {\n auto [p, v] = l;\n auto [a, b] = s;\n return sgn(v ^ (a - p)) * sgn(v ^ (b - p)) <= 0;\n}\n\nbool has_intersection(Segment s1, Segment s2) {\n auto [a, b] = s1;\n auto [c, d] = s2;\n return ccw(a, b, c) * ccw(a, b, d) <= 0 && ccw(c, d, a) * ccw(c, d, b) <= 0;\n}\n\nPoint intersection(Line l1, Line l2) {\n auto [a, v] = l1;\n auto [b, w] = l2;\n real_num t = ((b - a) ^ w) / (v ^ w);\n return a + v * t;\n}\n\nPoint intersection(Segment s1, Segment s2) {\n auto [a, b] = s1;\n auto [c, d] = s2;\n return intersection(Line(a, b - a), Line(c, d - c));\n}\n\nreal_num distance(Line l, Point p) {\n auto [a, v] = l;\n return std::abs(v ^ (p - a) / abs(v));\n}\n\nreal_num distance(Segment s, Point p) {\n auto [a, b] = s;\n if (le((b - a) * (p - a), 0)) return abs(p - a);\n if (le((a - b) * (p - b), 0)) return abs(p - b);\n return distance(Line(a, b - a), p);\n}\n\nreal_num distance(Line l1, Line l2) {\n auto [a, v] = l1;\n auto [b, w] = l2;\n if (is_parallel(v, w)) return distance(l1, b);\n return 0;\n}\n\nreal_num distance(Line l, Segment s) {\n if (has_intersection(l, s)) return 0;\n return std::min(distance(l, s.a), distance(l, s.b));\n}\n\nreal_num distance(Segment s1, Segment s2) {\n if (has_intersection(s1, s2)) return 0;\n return std::min({distance(s1, s2.a), distance(s1, s2.b), distance(s2, s1.a),\n distance(s2, s1.b)});\n}\n\nreal_num area(Polygon& p) {\n real_num res = 0;\n for (size_t i = 0; i < p.size(); i++) {\n res += p[i] ^ p[(i + 1) % p.size()] / 2;\n }\n return std::abs(res);\n}\n\nbool is_convex(Polygon& p) {\n int n = p.size();\n bool flag1 = false, flag2 = false;\n for (int i = 0; i < n; i++) {\n int tmp = ccw(p[(i + n - 1) % n], p[i], p[(i + 1) % n]);\n if (tmp == CCW_COUNTER_CLOCKWISE) {\n if (flag2) return false;\n flag1 = true;\n } else if (tmp == CCW_CLOCKWISE) {\n if (flag1) return false;\n flag2 = true;\n }\n }\n return true;\n}\n\nint point_in_polygon(Point a, Polygon& p) {\n int n = p.size(), wn = 0;\n for (int i = 0; i < n; i++) {\n int j = (i + 1) % n;\n if (distance(Segment(p[i], p[j]), a) == 0)\n return 1;\n else if (p[i].y <= a.y && a.y < p[j].y) {\n wn += (ccw(a, p[i], p[j]) == CCW_COUNTER_CLOCKWISE);\n } else if (p[j].y <= a.y && a.y < p[i].y) {\n wn -= (ccw(a, p[i], p[j]) == CCW_CLOCKWISE);\n }\n }\n return wn == 0 ? 0 : 2;\n}\n\nPolygon convex_hull(Points p) {\n int n = p.size();\n sort(p.begin(), p.end());\n Polygon ch(2 * n);\n int k = 0;\n for (int i = 0; i < n; i++) {\n while (k > 1 && le((ch[k - 1] - ch[k - 2]) ^ (p[i] - ch[k - 1]), 0))\n --k;\n ch[k++] = p[i];\n }\n for (int i = n - 2, t = k; i >= 0; --i) {\n while (k > t && le((ch[k - 1] - ch[k - 2]) ^ (p[i] - ch[k - 1]), 0))\n --k;\n ch[k++] = p[i];\n }\n ch.resize(k - 1);\n return ch;\n}\n\nreal_num convex_cut(Polygon& p, Line l) {\n int n = p.size();\n auto [a, v] = l;\n Polygon q;\n for (int i = 0; i < n; ++i) {\n int j = (i + 1) % n;\n if (geq(v ^ (p[i] - a), 0)) q.push_back(p[i]);\n if (has_intersection(l, Segment(p[i], p[j])) &&\n !is_parallel(v, p[j] - p[i])) {\n q.push_back(intersection(l, {p[i], p[j] - p[i]}));\n }\n }\n return area(q);\n}\n\nPoint circumcenter(Point a, Point b, Point c) {\n Point m = (a + b) / 2, n = (a + c) / 2;\n Vec v = {-(b - a).y, (b - a).x}, w = {-(c - a).y, (c - a).x};\n return intersection(Line(m, v), Line(n, w));\n}\n\nPoint incenter(Point a, Point b, Point c) {\n real_num A = abs(b - c), B = abs(c - a), C = abs(a - b);\n return (a * A + b * B + c * C) / (A + B + C);\n}\n\nPoint orthocenter(Point a, Point b, Point c) {\n Vec v = {-(c - b).y, (c - b).x}, w = {-(c - a).y, (c - a).x};\n return intersection(Line(a, v), Line(b, w));\n}\n\nnamespace internal {\n\nstd::pair<real_num, std::pair<int, int>> closest_pair_rec(\n std::vector<std::pair<Point, int>>& p, int l, int r) {\n if (r - l <= 1)\n return {std::numeric_limits<real_num>::max(), {p.size(), p.size()}};\n\n int m = (l + r) / 2;\n real_num x = p[m].first.x;\n auto d = std::min(closest_pair_rec(p, l, m), closest_pair_rec(p, m, r));\n auto cmp = [](std::pair<Point, int> a, std::pair<Point, int> b) {\n return a.first.y < b.first.y;\n };\n std::inplace_merge(p.begin() + l, p.begin() + m, p.begin() + r, cmp);\n\n std::vector<std::pair<Point, int>> q;\n for (int i = l; i < r; ++i) {\n if (ge(std::abs(p[i].first.x - x), d.first)) continue;\n for (int j = int(q.size()) - 1; j >= 0; --j) {\n real_num dy = p[i].first.y - q[j].first.y;\n if (geq(dy, d.first)) break;\n chmin(d,\n {abs(p[i].first - q[j].first), {p[i].second, q[j].second}});\n }\n q.push_back(p[i]);\n }\n return d;\n}\n\n} // namespace internal\n\nstd::pair<real_num, std::pair<int, int>> closest_pair(Points& p) {\n std::vector<std::pair<Point, int>> pid(p.size());\n for (size_t i = 0; i < p.size(); i++) {\n pid[i] = {p[i], i};\n }\n std::sort(pid.begin(), pid.end());\n return internal::closest_pair_rec(pid, 0, int(p.size()));\n}\n\nstd::pair<real_num, std::pair<int, int>> farthest_pair(Polygon& p) {\n int n = p.size();\n if (n == 2) {\n return {abs(p[0] - p[1]), {0, 1}};\n }\n int i = 0, j = 0;\n for (int k = 0; k < n; ++k) {\n if (le(p[k].x, p[i].x)) i = k;\n if (ge(p[k].x, p[j].x)) j = k;\n }\n real_num d = 0;\n int a = i, b = j, si = i, sj = j;\n while (i != sj || j != si) {\n if (chmax(d, abs(p[i] - p[j]))) a = i, b = j;\n if (le((p[(i + 1) % n] - p[i]) ^ (p[(j + 1) % n] - p[j]), 0)) {\n i = (i + 1) % n;\n } else\n j = (j + 1) % n;\n }\n return {d, {a, b}};\n}\n\nvoid arg_sort(Points& p) {\n auto cmp = [&](Point& a, Point& b) {\n if (le(a.y, 0)) {\n if (le(b.y, 0)) {\n return ge(a ^ b, 0);\n } else {\n return true;\n }\n } else if (eq(a.y, 0)) {\n if (le(b.y, 0)) {\n return false;\n } else if (eq(b.y, 0)) {\n return ge(a.x, b.x);\n } else {\n return geq(a.x, 0);\n }\n } else {\n if (le(b.y, 0)) {\n return false;\n } else if (eq(b.y, 0)) {\n return le(b.x, 0);\n } else {\n return ge(a ^ b, 0);\n }\n }\n };\n std::sort(p.begin(), p.end(), cmp);\n}\n\nstruct Circle {\n Point c;\n real_num r;\n};\n\nint num_of_common_tangent(Circle c1, Circle c2) {\n if (c1.r < c2.r) std::swap(c1, c2);\n real_num d = abs(c1.c - c2.c), r = c1.r + c2.r;\n if (ge(d, r)) return 4;\n if (eq(d, r)) return 3;\n if (eq(d + c2.r, c1.r)) return 1;\n if (le(d + c2.r, c1.r)) return 0;\n return 2;\n}\n\nint num_of_intersection(Circle c, Line l) {\n real_num d = distance(l, c.c);\n if (leq(d, c.r)) return 2;\n if (eq(d, c.r)) return 1;\n return 0;\n}\n\nbool has_intersection(Circle c, Segment s) {\n auto [a, b] = s;\n return leq(distance(s, c.c), c.r) &&\n geq(std::max(abs(a - c.c), abs(b - c.c)), c.r);\n}\n\nPoints intersection(Circle c, Line l) {\n Points res;\n if (num_of_intersection(c, l) == 0) return res;\n Point p = projection(l, c.c);\n real_num t = std::sqrt(\n std::max(real_num(0), (c.r * c.r - norm(p - c.c)) / norm(l.v)));\n res.push_back(p + l.v * t);\n if (!eq(t, 0)) res.push_back(p - l.v * t);\n return res;\n}\n\nPoints intersection(Circle c, Segment s) {\n auto [a, b] = s;\n Points ps = intersection(c, Line(b, a - b));\n Points res;\n for (auto p : ps) {\n if (ccw(a, b, p) == CCW_ON_SEGMENT) res.push_back(p);\n }\n return res;\n}\n\nPoints intersection(Circle c1, Circle c2) {\n real_num r1 = c1.r, r2 = c2.r;\n Points res;\n Vec v = c2.c - c1.c, w = {-v.y, v.x};\n real_num d = abs(v);\n real_num x = (d * d + r1 * r1 - r2 * r2) / (2 * d);\n real_num y = std::sqrt(std::max(r1 * r1 - x * x, real_num(0)));\n res.push_back(c1.c + v * x / d + w * y / d);\n if (num_of_common_tangent(c1, c2) != 2) return res;\n res.push_back(c1.c + v * x / d - w * y / d);\n return res;\n}\n\nnamespace internal {\n\nreal_num area_of_intersection(Circle c, Segment s) {\n auto [a, b] = s;\n Vec va = a - c.c, vb = b - c.c;\n if (eq(va ^ vb, 0))\n return 0;\n else if (leq(abs(va), c.r) && leq(abs(vb), c.r))\n return (va ^ vb) / 2;\n else if (geq(distance(s, c.c), c.r))\n return c.r * c.r * angle(va, vb) / 2;\n else {\n auto ps = intersection(c, s);\n if (ps.size() == 1)\n return area_of_intersection(c, {a, ps[0]}) +\n area_of_intersection(c, {ps[0], b});\n else\n return area_of_intersection(c, {a, ps[0]}) +\n area_of_intersection(c, {ps[0], ps[1]}) +\n area_of_intersection(c, {ps[1], b});\n }\n}\n\n} // namespace internal\n\nreal_num area_of_intersection(Circle c, Polygon& p) {\n int n = p.size();\n real_num res = 0;\n for (int i = 0; i < n; ++i) {\n res += internal::area_of_intersection(c, {p[i], p[(i + 1) % n]});\n }\n return res;\n}\n\nreal_num area_of_intersection(Circle c1, Circle c2) {\n int num = num_of_common_tangent(c1, c2);\n if (num >= 3) return 0;\n real_num r1 = c1.r, r2 = c2.r;\n if (num <= 1) {\n real_num r = std::min(r1, r2);\n return PI * r * r;\n }\n real_num d = abs(c1.c - c2.c);\n real_num res = 0;\n for (int i = 0; i < 2; ++i) {\n real_num x = (d * d + r1 * r1 - r2 * r2) / (2 * d);\n real_num t = std::acos(x / r1) * 2;\n res += (t - std::sin(t)) * r1 * r1 / 2;\n std::swap(r1, r2);\n }\n return res;\n}\n\nPoints tangent(Circle c, Point p) {\n Points res;\n real_num d = abs(p - c.c);\n real_num t = std::acos(c.r / d);\n res.push_back(c.c + rotation(p - c.c, t) * c.r / d);\n res.push_back(c.c + rotation(p - c.c, -t) * c.r / d);\n return res;\n}\n\nstd::tuple<std::vector<Line>, Points, Points> common_tangent(Circle c1,\n Circle c2) {\n std::vector<Line> ls;\n Points ps, qs;\n int num = num_of_common_tangent(c1, c2);\n if (num >= 2) {\n real_num d = abs(c2.c - c1.c);\n real_num t = std::acos(std::abs(c1.r - c2.r) / d);\n if (le(c1.r, c2.r)) t = PI - t;\n\n ps.push_back(c1.c + rotation(c2.c - c1.c, t) * c1.r / d);\n qs.push_back(c2.c + rotation(c2.c - c1.c, t) * c2.r / d);\n ls.push_back({ps.back(), qs.back() - ps.back()});\n\n ps.push_back(c1.c + rotation(c2.c - c1.c, -t) * c1.r / d);\n qs.push_back(c2.c + rotation(c2.c - c1.c, -t) * c2.r / d);\n ls.push_back({ps.back(), qs.back() - ps.back()});\n }\n if (num == 4) {\n real_num d = abs(c2.c - c1.c);\n real_num L = d * c1.r / (c1.r + c2.r);\n real_num t = std::acos(c1.r / L);\n\n ps.push_back(c1.c + rotation(c2.c - c1.c, t) * c1.r / d);\n qs.push_back(c2.c + rotation(c1.c - c2.c, t) * c2.r / d);\n ls.push_back({ps.back(), qs.back() - ps.back()});\n\n ps.push_back(c1.c + rotation(c2.c - c1.c, -t) * c1.r / d);\n qs.push_back(c2.c + rotation(c1.c - c2.c, -t) * c2.r / d);\n ls.push_back({ps.back(), qs.back() - ps.back()});\n }\n if (num == 3 || num == 1) {\n Points tg = intersection(c1, c2);\n ps.push_back(tg[0]);\n qs.push_back(tg[0]);\n ls.push_back({tg[0], rotation(c2.c - c1.c, PI / 2)});\n }\n return {ls, ps, qs};\n}\n\nCircle minimum_bounding_circle(Points& p) {\n if (p.size() == 1) {\n return {p[0], 0};\n } else if (p.size() == 2) {\n Point c = (p[0] + p[1]) / 2;\n return {c, abs(p[0] - c)};\n } else {\n Point c;\n real_num r = std::numeric_limits<real_num>::max();\n Points ch = convex_hull(p);\n int K = ch.size();\n auto check = [&](Point tc, real_num tr) {\n for (int i = 0; i < K; ++i) {\n if (ge(abs(ch[i] - tc), tr)) return false;\n }\n return true;\n };\n for (int i = 0; i < K; ++i) {\n for (int j = i + 1; j < K; ++j) {\n Point tc = (ch[i] + ch[j]) / 2;\n real_num tr = abs(ch[i] - tc);\n if (check(tc, tr) && chmin(r, tr)) c = tc;\n for (int k = j + 1; k < K; ++k) {\n int ccw_flag = ccw(ch[i], ch[j], ch[k]);\n if (ccw_flag != CCW_COUNTER_CLOCKWISE &&\n ccw_flag != CCW_CLOCKWISE)\n continue;\n tc = circumcenter(ch[i], ch[j], ch[k]);\n tr = abs(ch[i] - tc);\n if (check(tc, tr) && chmin(r, tr)) c = tc;\n }\n }\n }\n return {c, r};\n }\n}\n\n} // namespace geo2d\n\n} // namespace rklib\n\n\nusing namespace std;\nusing namespace rklib;\n\nusing lint = long long;\nusing pii = pair<int, int>;\nusing pll = pair<lint, lint>;\n\nusing namespace geo2d;\n\nint main() {\n while (true) {\n int n;\n cin >> n;\n if (n == 0) break;\n\n vector<Circle> cs(2 * n);\n vector<double> x(n), y(n), r(n), m(n);\n rep(i, n) {\n cin >> x[i] >> y[i] >> r[i] >> m[i];\n cs[i] = {{x[i], y[i]}, r[i]};\n cs[i + n] = {{x[i], y[i]}, r[i] + m[i]};\n }\n\n if (n == 1) {\n cout << 1 << \"\\n\";\n continue;\n }\n\n int ans = 0;\n rep(i, cs.size()) {\n rep(j, cs.size()) {\n if (i % n == j % n) continue;\n\n auto tangent = common_tangent(cs[i], cs[j]);\n auto ls = get<0>(tangent);\n for (auto l : ls) {\n int cnt = 0;\n rep(k, n) {\n auto d = distance(l, cs[k].c);\n if (geq(d, r[k]) && leq(d, r[k] + m[k])) {\n ++cnt;\n }\n }\n chmax(ans, cnt);\n }\n }\n }\n cout << ans << \"\\n\";\n }\n}", "accuracy": 1, "time_ms": 350, "memory_kb": 3868, "score_of_the_acc": -0.3146, "final_rank": 13 }, { "submission_id": "aoj_2201_7206196", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)\n#define revrep(i, t, s) for (int i = (int)(t)-1; i >= (int)(s); --i)\n#define all(x) begin(x), end(x)\ntemplate <typename T> bool chmax(T& a, const T& b) { return a < b ? (a = b, 1) : 0; }\ntemplate <typename T> bool chmin(T& a, const T& b) { return a > b ? (a = b, 1) : 0; }\n\nusing T = double;\nusing Vec = std::complex<T>;\n\nconst T PI = std::acos(-1);\n\nconstexpr T eps = 1e-10;\ninline bool eq(T a, T b) { return std::abs(a - b) <= eps; }\ninline bool eq(Vec a, Vec b) { return std::abs(a - b) <= eps; }\ninline bool lt(T a, T b) { return a < b - eps; }\ninline bool leq(T a, T b) { return a <= b + eps; }\n\nstd::istream& operator>>(std::istream& is, Vec& p) {\n T x, y;\n is >> x >> y;\n p = {x, y};\n return is;\n}\n\nstruct Line {\n Vec p1, p2;\n Line() = default;\n Line(const Vec& p1, const Vec& p2) : p1(p1), p2(p2) {}\n Vec dir() const { return p2 - p1; }\n};\n\nstruct Segment : Line {\n using Line::Line;\n};\n\nstruct Circle {\n Vec c;\n T r;\n Circle() = default;\n Circle(const Vec& c, T r) : c(c), r(r) {}\n};\n\nusing Polygon = std::vector<Vec>;\n\nT dot(const Vec& a, const Vec& b) {\n return (std::conj(a) * b).real();\n}\n\nT cross(const Vec& a, const Vec& b) {\n return (std::conj(a) * b).imag();\n}\n\nVec rot(const Vec& a, T ang) {\n return a * Vec(std::cos(ang), std::sin(ang));\n}\n\nVec perp(const Vec& a) {\n return Vec(-a.imag(), a.real());\n}\n\nVec projection(const Line& l, const Vec& p) {\n return l.p1 + dot(p - l.p1, l.dir()) * l.dir() / std::norm(l.dir());\n}\n\nVec reflection(const Line& l, const Vec& p) {\n return T(2) * projection(l, p) - p;\n}\n\n// 0: collinear\n// 1: counter-clockwise\n// -1: clockwise\nint ccw(const Vec& a, const Vec& b, const Vec& c) {\n if (eq(cross(b - a, c - a), 0)) return 0;\n if (lt(cross(b - a, c - a), 0)) return -1;\n return 1;\n}\n\nvoid sort_by_arg(std::vector<Vec>& pts) {\n std::sort(pts.begin(), pts.end(), [&](auto& p, auto& q) {\n if ((p.imag() < 0) != (q.imag() < 0)) return (p.imag() < 0);\n if (cross(p, q) == 0) {\n if (p == Vec(0, 0)) return !(q.imag() < 0 || (q.imag() == 0 && q.real() > 0));\n if (q == Vec(0, 0)) return (p.imag() < 0 || (p.imag() == 0 && p.real() > 0));\n return (p.real() > q.real());\n }\n return (cross(p, q) > 0);\n });\n}\n\n// 0: inside\n// 1: inscribe\n// 2: intersect\n// 3: circumscribe\n// 4: outside\nint intersect(const Circle& c1, const Circle& c2) {\n T d = std::abs(c1.c - c2.c);\n if (lt(d, std::abs(c2.r - c1.r))) return 0;\n if (eq(d, std::abs(c2.r - c1.r))) return 1;\n if (eq(c1.r + c2.r, d)) return 3;\n if (lt(c1.r + c2.r, d)) return 4;\n return 2;\n}\n\nstd::vector<Vec> intersection(const Circle& c1, const Circle& c2) {\n T d = std::abs(c1.c - c2.c);\n if (lt(c1.r + c2.r, d)) return {}; // outside\n Vec e1 = (c2.c - c1.c) / std::abs(c2.c - c1.c);\n Vec e2 = perp(e1);\n if (lt(d, std::abs(c2.r - c1.r))) return {}; // contain\n if (eq(d, std::abs(c2.r - c1.r))) return {c1.c + c1.r*e1}; // tangent\n T x = (c1.r*c1.r - c2.r*c2.r + d*d) / (2*d);\n T y = std::sqrt(c1.r*c1.r - x*x);\n return {c1.c + x*e1 + y*e2, c1.c + x*e1 - y*e2};\n}\n\nstd::pair<Vec, Vec> tangent_points(const Circle& c, const Vec& p) {\n auto m = (p + c.c) / T(2);\n auto is = intersection(c, Circle(m, std::abs(p - m)));\n return {is[0], is[1]};\n}\n\nstd::vector<Line> common_tangents(Circle c1, Circle c2) {\n assert(!eq(c1.c, c2.c) || !eq(c1.r, c2.r));\n int cnt = intersect(c1, c2); // number of common tangents\n std::vector<Line> ret;\n if (cnt == 0) {\n return ret;\n }\n\n // external\n if (eq(c1.r, c2.r)) {\n auto d = c2.c - c1.c;\n Vec e(-d.imag(), d.real());\n e = e / std::abs(e) * c1.r;\n ret.push_back(Line(c1.c + e, c1.c + e + d));\n ret.push_back(Line(c1.c - e, c1.c - e + d));\n } else {\n auto p = (-c2.r*c1.c + c1.r*c2.c) / (c1.r - c2.r);\n if (cnt == 1) {\n Vec q(-p.imag(), p.real());\n return {Line(p, q)};\n } else {\n auto [a, b] = tangent_points(c1, p);\n ret.push_back(Line(a, p));\n ret.push_back(Line(b, p));\n }\n }\n\n // internal\n auto p = (c2.r*c1.c + c1.r*c2.c) / (c1.r + c2.r);\n if (cnt == 3) {\n Vec q(-p.imag(), p.real());\n ret.push_back(Line(p, q));\n } else if (cnt == 4) {\n auto [a, b] = tangent_points(c1, p);\n ret.push_back(Line(a, p));\n ret.push_back(Line(b, p));\n }\n\n return ret;\n}\n\nT dist(const Line& l, const Vec& p) {\n return std::abs(cross(p - l.p1, l.dir())) / std::abs(l.dir());\n}\n\n\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << fixed << setprecision(15);\n\n while (true) {\n int N;\n cin >> N;\n if (N == 0) break;\n vector<Vec> c(N);\n vector<T> r(N), m(N);\n vector<Circle> circles(2*N);\n rep(i,0,N) {\n cin >> c[i] >> r[i] >> m[i];\n circles[2*i] = Circle(c[i], r[i]);\n circles[2*i+1] = Circle(c[i], r[i]+m[i]);\n }\n vector<Line> lines;\n rep(i,0,2*N) {\n rep(j,i+1,2*N) {\n if (eq(circles[i].c, circles[j].c) && eq(circles[i].r, circles[j].r)) continue;\n auto res = common_tangents(circles[i], circles[j]);\n for (auto& line : res) lines.push_back(line);\n }\n }\n int ans = 1;\n for (auto line : lines) {\n int cnt = 0;\n rep(i,0,N) {\n auto d = dist(line, c[i]);\n if (leq(r[i], d) && leq(d, r[i]+m[i])) ++cnt;\n }\n chmax(ans, cnt);\n }\n cout << ans << \"\\n\";\n }\n}", "accuracy": 1, "time_ms": 160, "memory_kb": 4852, "score_of_the_acc": -0.4933, "final_rank": 17 }, { "submission_id": "aoj_2201_7126231", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, n) for (int i = 0; i < int(n); i++)\n#define per(i, n) for (int i = (n)NOP; 0 <= i; i--)\n#define rep2(i, l, r) for (int i = (l); i < int(r); i++)\n#define per2(i, l, r) for (int i = (r)NOP; int(l) <= i; i--)\n#define MM << \" \" <<\n#define pb push_back\n#define eb emplace_back\n#define all(x) begin(x), end(x)\n#define rall(x) rbegin(x), rend(x)\n#define sz(x) (int)x.size()\n\ntemplate<typename T>\nvoid print(const vector<T> &v, T x = 0) {\n int n = v.size();\n for (int i = 0; i < n; i++) cout << v[i] + x << (i == n - 1 ? '\\n' : ' ');\n if (v.empty()) cout << '\\n';\n}\n\ntemplate<class T>\nusing MaxHeap = priority_queue<T>;\ntemplate<class T>\nusing MinHeap = priority_queue<T, vector<T>, greater<T>>;\n\nusing ll = long long;\nusing pii = pair<int, int>;\nusing pll = pair<ll, ll>;\n\ntemplate<typename T>\nbool chmax(T &x, const T &y) {\n return (x < y) ? (x = y, true) : false;\n}\n\ntemplate<typename T>\nbool chmin(T &x, const T &y) {\n return (x > y) ? (x = y, true) : false;\n}\n\n\nusing Real = long double;\nusing Point = complex<Real>;\n\nconst Real EPS = 1e-10;\nconst Real pi = acos(-1.0);\n\nint sgn(Real a) { return (a < -EPS) ? -1 : (a > EPS) ? 1 : 0; }\n\nbool eq(Real a, Real b) { return sgn(b - a) == 0; }\n\nPoint operator*(const Point &p, const Real &d) {\n return Point(real(p) * d, imag(p) * d);\n}\n\nPoint operator/(const Point &p, const Real & d) {\n return p * (1 / d);\n}\n\nPoint rotate(const Point &p, const Real &t) {\n return p * Point(cos(t), sin(t));\n}\n\nReal dot(const Point &p, const Point &q) {\n return (p * conj(q)).real();\n}\n\nstruct Line {\n Point a, b;\n Line() {}\n Line(Point _a, Point _b) : a(_a), b(_b) {}\n};\n\nstruct Circle {\n Point p;\n Real r;\n Circle() {}\n Circle(Point _p, Real _r) : p(_p), r(_r) {}\n};\n\nvector<Line> tangent(Circle c1, Circle c2) {\n vector<Line> ret;\n if(c1.r < c2.r) swap(c1, c2);\n Real r = abs(c2.p - c1.p);\n if(eq(r, 0.0)) return ret;\n Point u = (c2.p - c1.p) / r;\n Point v = rotate(u, pi * 0.5);\n for(auto &s : {1.0, -1.0}) {\n Real h = (c1.r + c2.r * s) / r;\n if(eq(abs(h), 1.0)) {\n ret.eb(c1.p + u * c1.r, c1.p + (u + v) * c1.r);\n } else if(abs(h) < 1.0) {\n Point uu = u * h, vv = v * sqrt(1.0 - h * h);\n ret.eb(c1.p + (uu + vv) * c1.r, c2.p - (uu + vv) * c2.r * s);\n ret.eb(c1.p + (uu - vv) * c1.r, c2.p - (uu - vv) * c2.r * s);\n }\n }\n return ret;\n}\n\nPoint projection(const Line &l, const Point &p) {\n Real t = dot(p - l.a, l.b - l.a) / norm(l.b - l.a);\n return l.a + (l.b - l.a) * t;\n}\n\nReal distance(const Line &l, const Point &p) {\n return abs(p - projection(l, p));\n}\n\nint solve(int n) {\n vector<pair<Circle, Circle>> c(n);\n vector<Circle> b;\n rep(i,n) {\n Real x, y, r, m;\n cin >> x >> y >> r >> m;\n c[i].first = Circle({x, y}, r);\n c[i].second = Circle({x, y}, r + m);\n b.eb(c[i].first);\n b.eb(c[i].second);\n }\n\n if(n == 1) return 1;\n int ans = 0;\n int m = b.size();\n rep(i,m) rep(j,m) {\n for(auto &l : tangent(b[i], b[j])) {\n int now = 0;\n for(auto &k : c) {\n auto p = k.first.p;\n auto d = distance(l, p);\n if(k.first.r - EPS < d && d < k.second.r + EPS) ++now;\n }\n chmax(ans, now);\n }\n }\n return ans;\n}\n\nint main() {\n int n;\n vector<int> ans; \n while(1) {\n cin >> n;\n if(n == 0) break;\n ans.eb(solve(n));\n }\n for(auto &i : ans) cout << i << endl;\n}", "accuracy": 1, "time_ms": 2000, "memory_kb": 3800, "score_of_the_acc": -1.1331, "final_rank": 19 }, { "submission_id": "aoj_2201_7078760", "code_snippet": "#ifndef HIDDEN_IN_VS // 折りたたみ用\n\n// 警告の抑制\n#define _CRT_SECURE_NO_WARNINGS\n\n// ライブラリの読み込み\n#include <bits/stdc++.h>\nusing namespace std;\n\n// 型名の短縮\nusing ll = long long; // -2^63 ~ 2^63 = 9 * 10^18(int は -2^31 ~ 2^31 = 2 * 10^9)\nusing pii = pair<int, int>;\tusing pll = pair<ll, ll>;\tusing pil = pair<int, ll>;\tusing pli = pair<ll, int>;\nusing vi = vector<int>;\t\tusing vvi = vector<vi>;\t\tusing vvvi = vector<vvi>;\nusing vl = vector<ll>;\t\tusing vvl = vector<vl>;\t\tusing vvvl = vector<vvl>;\nusing vb = vector<bool>;\tusing vvb = vector<vb>;\t\tusing vvvb = vector<vvb>;\nusing vc = vector<char>;\tusing vvc = vector<vc>;\t\tusing vvvc = vector<vvc>;\nusing vd = vector<double>;\tusing vvd = vector<vd>;\t\tusing vvvd = vector<vvd>;\ntemplate <class T> using priority_queue_rev = priority_queue<T, vector<T>, greater<T>>;\nusing Graph = vvi;\n\n// 定数の定義\nconst double PI = acos(-1);\nconst vi DX = { 1, 0, -1, 0 }; // 4 近傍(下,右,上,左)\nconst vi DY = { 0, 1, 0, -1 };\nint INF = 1001001001; ll INFL = 4004004004004004004LL;\ndouble EPS = 1e-12;\n\n// 入出力高速化\nstruct fast_io { fast_io() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(18); } } fastIOtmp;\n\n// 汎用マクロの定義\n#define all(a) (a).begin(), (a).end()\n#define sz(x) ((int)(x).size())\n#define lbpos(a, x) (int)distance((a).begin(), std::lower_bound(all(a), x))\n#define ubpos(a, x) (int)distance((a).begin(), std::upper_bound(all(a), x))\n#define Yes(b) {cout << ((b) ? \"Yes\\n\" : \"No\\n\");}\n#define rep(i, n) for(int i = 0, i##_len = int(n); i < i##_len; ++i) // 0 から n-1 まで昇順\n#define repi(i, s, t) for(int i = int(s), i##_end = int(t); i <= i##_end; ++i) // s から t まで昇順\n#define repir(i, s, t) for(int i = int(s), i##_end = int(t); i >= i##_end; --i) // s から t まで降順\n#define repe(v, a) for(const auto& v : (a)) // a の全要素(変更不可能)\n#define repea(v, a) for(auto& v : (a)) // a の全要素(変更可能)\n#define repb(set, d) for(int set = 0; set < (1 << int(d)); ++set) // d ビット全探索(昇順)\n#define repp(a) sort(all(a)); for(bool a##_perm = true; a##_perm; a##_perm = next_permutation(all(a))) // a の順列全て(昇順)\n#define smod(n, m) ((((n) % (m)) + (m)) % (m)) // 非負mod\n#define uniq(a) {sort(all(a)); (a).erase(unique(all(a)), (a).end());} // 重複除去\n#define EXIT(a) {cout << (a) << endl; exit(0);} // 強制終了\n\n// 汎用関数の定義\ntemplate <class T> inline ll pow(T n, int k) { ll v = 1; rep(i, k) v *= n; return v; }\ntemplate <class T> inline bool chmax(T& M, const T& x) { if (M < x) { M = x; return true; } return false; } // 最大値を更新(更新されたら true を返す)\ntemplate <class T> inline bool chmin(T& m, const T& x) { if (m > x) { m = x; return true; } return false; } // 最小値を更新(更新されたら true を返す)\n\n// 演算子オーバーロード\ntemplate <class T, class U> inline istream& operator>>(istream& is, pair<T, U>& p) { is >> p.first >> p.second; return is; }\ntemplate <class T> inline istream& operator>>(istream& is, vector<T>& v) { repea(x, v) is >> x; return is; }\ntemplate <class T> inline vector<T>& operator--(vector<T>& v) { repea(x, v) --x; return v; }\ntemplate <class T> inline vector<T>& operator++(vector<T>& v) { repea(x, v) ++x; return v; }\n\n// 手元環境(Visual Studio)\n#ifdef _MSC_VER\n#include \"local.hpp\"\n// 提出用(gcc)\n#else\ninline int popcount(int n) { return __builtin_popcount(n); }\ninline int popcount(ll n) { return __builtin_popcountll(n); }\ninline int lsb(int n) { return n != 0 ? __builtin_ctz(n) : -1; }\ninline int lsb(ll n) { return n != 0 ? __builtin_ctzll(n) : -1; }\ninline int msb(int n) { return n != 0 ? (31 - __builtin_clz(n)) : -1; }\ninline int msb(ll n) { return n != 0 ? (63 - __builtin_clzll(n)) : -1; }\n#define gcd __gcd\n#define dump(...)\n#define dumpel(v)\n#define dump_list(v)\n#define input_from_file(f)\n#define output_to_file(f)\n#define Assert(b) { if (!(b)) while (1) cout << \"OLE\"; }\n#endif\n\n#endif // 折りたたみ用\n\n\n////--------------AtCoder 専用--------------\n//#include <atcoder/all>\n//using namespace atcoder;\n//\n////using mint = modint1000000007;\n//using mint = modint998244353;\n////using mint = modint; // mint::set_mod(m);\n//\n//istream& operator>>(istream& is, mint& x) { ll x_; is >> x_; x = x_; return is; }\n//ostream& operator<<(ostream& os, const mint& x) { os << x.val(); return os; }\n//using vm = vector<mint>; using vvm = vector<vm>; using vvvm = vector<vvm>;\n////----------------------------------------\n\n\n//【平面上の点,二次元ベクトル】\n/*\n* 平面における点/二次元ベクトルを表す構造体\n*\n* Point<T>() : O(1)\n*\t(0, 0) で初期化する.\n*\n* Point<T>(T x, T y) : O(1)\n*\t(x, y) で初期化する.\n*\n* p1 == p2, p1 != p2, p1 < p2, p1 > p2, p1 <= p2, p1 >= p2 : O(1)\n*\tx 座標優先,次いで y 座標の大小比較を行う.\n*\n* p1 + p2, p1 - p2, c * p, p * c, p / c : O(1)\n*\tベクトルとみなした加算,減算,スカラー倍,スカラー除算を行う.複合代入演算子も使用可.\n*\n* T sqnorm() : O(1)\n*\t自身の 2 乗ノルムを返す.\n*\n* double norm() : O(1)\n*\t自身のノルムを返す.\n*\n* Point<double> normalize() : O(1)\n*\t自身を正規化したベクトルを返す.\n*\n* T dot(Point<T> p) : O(1)\n*\t自身と p との内積を返す.\n*\n* T cross(Point<T> p) : O(1)\n*\t自身と p との外積を返す.\n*\n* double angle(Point<T> p) : O(1)\n*\t自身から p までの成す角度を返す.\n*/\ntemplate <class T> struct Point {\n\t// 点の x 座標,y 座標\n\tT x, y;\n\n\t// コンストラクタ\n\tPoint() : x(0), y(0) {}\n\tPoint(T x_, T y_) : x(x_), y(y_) {}\n\n\t// 代入\n\tPoint(const Point& old) = default;\n\tPoint& operator=(const Point& other) = default;\n\n\t// キャスト\n\toperator Point<ll>() const { return Point<ll>((ll)x, (ll)y); }\n\toperator Point<double>() const { return Point<double>((double)x, (double)y); }\n\n\t// 入出力\n\tfriend istream& operator>>(istream& is, Point& p) { is >> p.x >> p.y; return is; }\n\tfriend ostream& operator<<(ostream& os, const Point& p) { os << '(' << p.x << ',' << p.y << ')'; return os; }\n\n\t// 比較(x 座標優先)\n\tbool operator==(const Point& p) const { return x == p.x && y == p.y; }\n\tbool operator!=(const Point& p) const { return !(*this == p); }\n\tbool operator<(const Point& p) const { return x == p.x ? y < p.y : x < p.x; }\n\tbool operator>=(const Point& p) const { return !(*this < p); }\n\tbool operator>(const Point& p) const { return x == p.x ? y > p.y : x > p.x; }\n\tbool operator<=(const Point& p) const { return !(*this > p); }\n\n\t// 加算,減算,スカラー倍,スカラー除算\n\tPoint& operator+=(const Point& p) { x += p.x; y += p.y;\treturn *this; }\n\tPoint operator+(const Point& p) const { Point q(*this); return q += p; }\n\tPoint& operator-=(const Point& p) { x -= p.x; y -= p.y;\treturn *this; }\n\tPoint operator-(const Point& p) const { Point q(*this); return q -= p; }\n\tPoint& operator*=(const T& c) { x *= c; y *= c;\treturn *this; }\n\tPoint operator*(const T& c) const { Point q(*this); return q *= c; }\n\tPoint& operator/=(const T& c) { x /= c; y /= c;\treturn *this; }\n\tPoint operator/(const T& c) const { Point q(*this); return q /= c; }\n\tfriend Point operator*(const T& sc, const Point& p) { return p * sc; }\n\tPoint operator-() const { Point a = *this; return a *= -1; }\n\n\t// 二乗ノルム,ノルム,正規化\n\tT sqnorm() const { return x * x + y * y; }\n\tdouble norm() const { return sqrt((double)x * x + (double)y * y); }\n\tPoint<double> normalize() const { return Point<double>(*this) / norm(); }\n\n\t// 内積,外積,成す角度\n\tT dot(const Point& other) const { return x * other.x + y * other.y; }\n\tT cross(const Point& other) const { return x * other.y - y * other.x; }\n\tdouble angle(const Point& other) const {\n\t\treturn atan2(this->cross(other), this->dot(other));\n\t}\n};\n\n\n//【平面内の直線,線分】\n/*\n* {a, b} : 2 点 a, b を通る a → b 方向の有向直線を表す.\n*\n* その他,無向直線,有向線分,無向線分などを表すのにも用いる.\n*/\ntemplate <class T> using Line = pair<Point<T>, Point<T>>;\n\n\n//【平面内の円】\n/*\n* {p, r} : 点 p を中心とする半径 r の円を表す.\n*/\ntemplate <class T> using Circle = pair<Point<T>, T>;\n\n\n//【二円の共通接線】O(1)\n/*\n* 2 円 c1, c2 の共通接線の本数を返す.また c1 との接点があればその座標を ts に格納する.\n* c1 = c2 のときは -1 を返す.\n*/\nint common_tangent(const Circle<ll>& c1, const Circle<ll>& c2, vector<Point<double>>& ts) {\n\t// verify : https://onlinejudge.u-aizu.ac.jp/courses/library/4/CGL/all/CGL_7_G\n\n\tts.clear();\n\n\tif (c1 == c2) return -1;\n\n\t// 円 c1, c2 の中心と半径\n\tPoint<ll> o1 = c1.first; ll r1 = c1.second;\n\tPoint<ll> o2 = c2.first; ll r2 = c2.second;\n\n\t// o1 から o2 へのベクトル,半径の和と差\n\tPoint<ll> d = o2 - o1;\n\tll r_sum = r1 + r2, r_dif = abs(r1 - r2);\n\n\t// 中心間距離が円の半径の差より小さい場合 → 共通接線 0 本\n\tif (d.sqnorm() < r_dif * r_dif) return 0;\n\n\t// 中心間距離が円の半径の差に等しい場合 → 共通接線 1 本\n\tif (d.sqnorm() == r_dif * r_dif) {\n\t\tts.push_back(Point<double>(o1) + (r1 > r2 ? 1 : -1) * Point<double>(d) * (r1 / d.norm()));\n\t\treturn 1;\n\t}\n\n\t// 共通外接線を追加\n\tdouble x = r1 * r_dif / d.norm();\n\tdouble y = r1 * sqrt(1 - (double)r_dif * r_dif / d.sqnorm());\n\tPoint<double> nd = Point<double>(d) * (x / d.norm());\n\tPoint<double> nn = Point<double>(-(double)d.y, (double)d.x) * (y / d.norm());\n\tts.push_back(Point<double>(o1) + (r1 > r2 ? 1 : -1) * nd + nn);\n\tts.push_back(Point<double>(o1) + (r1 > r2 ? 1 : -1) * nd - nn);\n\n\t// 中心間距離が円の半径の和より大きい場合 → 共通接線 4 本\n\tif (d.sqnorm() > r_sum * r_sum) {\n\t\t// 共通内接線を追加\n\t\tdouble x = r1 * r_sum / d.norm();\n\t\tdouble y = r1 * sqrt(1 - (double)r_sum * r_sum / d.sqnorm());\n\t\tPoint<double> nd = Point<double>(d) * (x / d.norm());\n\t\tPoint<double> nn = Point<double>(-(double)d.y, (double)d.x) * (y / d.norm());\n\t\tts.push_back(Point<double>(o1) + nd + nn);\n\t\tts.push_back(Point<double>(o1) + nd - nn);\n\n\t\treturn 4;\n\t}\n\n\t// 中心間距離が円の半径の和に等しい場合 → 共通接線 3 本\n\tif (d.sqnorm() == r_sum * r_sum) {\n\t\tts.push_back(Point<double>(o1) + Point<double>(d) * (r1 / d.norm()));\n\t\treturn 3;\n\t}\n\n\t// その他の場合 → 共通接線 2 本\n\treturn 2;\n}\n\n\n//【90°回転】O(1)\n/*\n* 点 p を点 c を中心に 90° × i だけ回転した点を返す.\n*/\ntemplate <class T>\ninline Point<T> rotate90(const Point<T>& p, const Point<T>& c, int i) {\n\tPoint<T> q;\n\tswitch (smod(i, 4)) {\n\tcase 0:\n\t\tq.x = 1 * (p.x - c.x) - 0 * (p.y - c.y) + c.x;\n\t\tq.y = 0 * (p.x - c.x) + 1 * (p.y - c.y) + c.y;\n\t\tbreak;\n\tcase 1:\n\t\tq.x = 0 * (p.x - c.x) - 1 * (p.y - c.y) + c.x;\n\t\tq.y = 1 * (p.x - c.x) + 0 * (p.y - c.y) + c.y;\n\t\tbreak;\n\tcase 2:\n\t\tq.x = -1 * (p.x - c.x) - 0 * (p.y - c.y) + c.x;\n\t\tq.y = 0 * (p.x - c.x) + (-1) * (p.y - c.y) + c.y;\n\t\tbreak;\n\tcase 3:\n\t\tq.x = 0 * (p.x - c.x) - (-1) * (p.y - c.y) + c.x;\n\t\tq.y = -1 * (p.x - c.x) + 0 * (p.y - c.y) + c.y;\n\t\tbreak;\n\tdefault:;\n\t}\n\treturn q;\n}\n\n\n//【点と直線の距離】O(1)\n/*\n* 点 p と直線 l との距離を返す.\n*/\ntemplate <class T> inline double distance_P_L(const Point<T>& p, const Line<T>& l) {\n\tPoint<double> d = (l.second - l.first).normalize();\n\tPoint<double> n(-d.y, d.x);\n\tPoint<double> p2 = p - l.first;\n\treturn p2.dot(n);\n}\n\n\nbool solve() {\n\tint n;\n\tcin >> n;\n\n\tif (n == 0) return false;\n\n\tusing PL = Point<ll>;\n\tusing LL = Line<ll>;\n\tusing CL = Circle<ll>;\n\tusing PD = Point<double>;\n\tusing LD = Line<double>;\n\n\tvector<PL> p(n); vl r(n), m(n);\n\trep(i, n) cin >> p[i] >> r[i] >> m[i];\n\n\tvector<CL> cs(2 * n);\n\trep(i, n) {\n\t\tcs[2 * i] = CL{ p[i], r[i] };\n\t\tcs[2 * i + 1] = CL{ p[i], r[i] + m[i] };\n\t}\n\n\tint res = 1;\n\n\t// 2 円の組を全探索\n\trep(i1, 2 * n) repi(i2, i1 + 1, 2 * n - 1) {\n\t\tvector<PD> tp;\n\t\tint K = common_tangent(cs[i1], cs[i2], tp);\n\t\tif (K <= 0) continue;\n//\t\tdump(\"---------------------------\"); dump(\"cs[i1], cs[i2]:\", cs[i1], cs[i2]);\n\n\t\trep(k, K) {\n\t\t\tLD tl{tp[k], rotate90(PD(cs[i1].first), tp[k], 1)};\n//\t\t\tdump(\"tl:\", tl);\n\n\t\t\tint cnt = 0;\n\t\t\trep(j, n) {\n\t\t\t\tdouble d = abs(distance_P_L(PD(p[j]), tl));\n//\t\t\t\tdump(\"p[j], d:\", p[j], d);\n\n\t\t\t\tif (r[j] - 1e-10 < d && d < r[j] + m[j] + 1e-10) cnt++;\n\t\t\t}\n\n//\t\t\tdump(\"cnt:\", cnt);\n\t\t\tchmax(res, cnt);\n\t\t}\n\t}\n\n\tdump(\"ans:\", res);\n\tcout << res << endl;\n\n\treturn true;\n}\n\n\nint main() {\n\tinput_from_file(\"input.txt\");\n//\toutput_to_file(\"output.txt\");\n\t\n\twhile (solve());\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3376, "score_of_the_acc": -0.0145, "final_rank": 1 }, { "submission_id": "aoj_2201_7062783", "code_snippet": "#ifndef LOCAL\n#pragma GCC optimize (\"Ofast\")\n#pragma GCC optimize (\"unroll-loops\")\n#endif\n\n#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll=long long;\n#define int ll\n\n#define rng(i,a,b) for(int i=int(a);i<int(b);i++)\n#define rep(i,b) rng(i,0,b)\n#define gnr(i,a,b) for(int i=int(b)-1;i>=int(a);i--)\n#define per(i,b) gnr(i,0,b)\n#define pb push_back\n#define eb emplace_back\n#define a first\n#define b second\n#define bg begin()\n#define ed end()\n#define all(x) x.bg,x.ed\n#define si(x) int(x.size())\n#ifdef LOCAL\n#define dmp(x) cerr<<__LINE__<<\" \"<<#x<<\" \"<<x<<endl\n#else\n#define dmp(x) void(0)\n#endif\n\ntemplate<class t,class u> bool chmax(t&a,u b){if(a<b){a=b;return true;}else return false;}\ntemplate<class t,class u> bool chmin(t&a,u b){if(b<a){a=b;return true;}else return false;}\n\ntemplate<class t> using vc=vector<t>;\ntemplate<class t> using vvc=vc<vc<t>>;\n\nusing pi=pair<int,int>;\nusing vi=vc<int>;\n\ntemplate<class t,class u>\nostream& operator<<(ostream& os,const pair<t,u>& p){\n\treturn os<<\"{\"<<p.a<<\",\"<<p.b<<\"}\";\n}\n\ntemplate<class t> ostream& operator<<(ostream& os,const vc<t>& v){\n\tos<<\"{\";\n\tfor(auto e:v)os<<e<<\",\";\n\treturn os<<\"}\";\n}\n\n#define mp make_pair\n#define mt make_tuple\n#define one(x) memset(x,-1,sizeof(x))\n#define zero(x) memset(x,0,sizeof(x))\n#ifdef LOCAL\nvoid dmpr(ostream&os){os<<endl;}\ntemplate<class T,class... Args>\nvoid dmpr(ostream&os,const T&t,const Args&... args){\n\tos<<t<<\" \";\n\tdmpr(os,args...);\n}\n#define dmp2(...) dmpr(cerr,__LINE__,##__VA_ARGS__)\n#else\n#define dmp2(...) void(0)\n#endif\n\nusing uint=unsigned;\nusing ull=unsigned long long;\n\ntemplate<class t,size_t n>\nostream& operator<<(ostream&os,const array<t,n>&a){\n\treturn os<<vc<t>(all(a));\n}\n\ntemplate<int i,class T>\nvoid print_tuple(ostream&,const T&){\n}\n\ntemplate<int i,class T,class H,class ...Args>\nvoid print_tuple(ostream&os,const T&t){\n\tif(i)os<<\",\";\n\tos<<get<i>(t);\n\tprint_tuple<i+1,T,Args...>(os,t);\n}\n\ntemplate<class ...Args>\nostream& operator<<(ostream&os,const tuple<Args...>&t){\n\tos<<\"{\";\n\tprint_tuple<0,tuple<Args...>,Args...>(os,t);\n\treturn os<<\"}\";\n}\n\ntemplate<class t>\nvoid print(t x,int suc=1){\n\tcout<<x;\n\tif(suc==1)\n\t\tcout<<\"\\n\";\n\tif(suc==2)\n\t\tcout<<\" \";\n}\n\nll read(){\n\tll i;\n\tcin>>i;\n\treturn i;\n}\n\nvi readvi(int n,int off=0){\n\tvi v(n);\n\trep(i,n)v[i]=read()+off;\n\treturn v;\n}\n\npi readpi(int off=0){\n\tint a,b;cin>>a>>b;\n\treturn pi(a+off,b+off);\n}\n\ntemplate<class t,class u>\nvoid print(const pair<t,u>&p,int suc=1){\n\tprint(p.a,2);\n\tprint(p.b,suc);\n}\n\ntemplate<class t,class u>\nvoid print_offset(const pair<t,u>&p,ll off,int suc=1){\n\tprint(p.a+off,2);\n\tprint(p.b+off,suc);\n}\n\ntemplate<class T>\nvoid print(const vector<T>&v,int suc=1){\n\trep(i,v.size())\n\t\tprint(v[i],i==int(v.size())-1?suc:2);\n}\n\ntemplate<class T>\nvoid print_offset(const vector<T>&v,ll off,int suc=1){\n\trep(i,v.size())\n\t\tprint(v[i]+off,i==int(v.size())-1?suc:2);\n}\n\ntemplate<class T,size_t N>\nvoid print(const array<T,N>&v,int suc=1){\n\trep(i,N)\n\t\tprint(v[i],i==int(N)-1?suc:2);\n}\n\nstring readString(){\n\tstring s;\n\tcin>>s;\n\treturn s;\n}\n\ntemplate<class T>\nT sq(const T& t){\n\treturn t*t;\n}\n\nvoid YES(bool ex=true){\n\tcout<<\"YES\\n\";\n\tif(ex)exit(0);\n\t#ifdef LOCAL\n\tcout.flush();\n\t#endif\n}\nvoid NO(bool ex=true){\n\tcout<<\"NO\\n\";\n\tif(ex)exit(0);\n\t#ifdef LOCAL\n\tcout.flush();\n\t#endif\n}\nvoid Yes(bool ex=true){\n\tcout<<\"Yes\\n\";\n\tif(ex)exit(0);\n\t#ifdef LOCAL\n\tcout.flush();\n\t#endif\n}\nvoid No(bool ex=true){\n\tcout<<\"No\\n\";\n\tif(ex)exit(0);\n\t#ifdef LOCAL\n\tcout.flush();\n\t#endif\n}\n//#define CAPITAL\n/*\nvoid yes(bool ex=true){\n\t#ifdef CAPITAL\n\tcout<<\"YES\"<<\"\\n\";\n\t#else\n\tcout<<\"Yes\"<<\"\\n\";\n\t#endif\n\tif(ex)exit(0);\n\t#ifdef LOCAL\n\tcout.flush();\n\t#endif\n}\nvoid no(bool ex=true){\n\t#ifdef CAPITAL\n\tcout<<\"NO\"<<\"\\n\";\n\t#else\n\tcout<<\"No\"<<\"\\n\";\n\t#endif\n\tif(ex)exit(0);\n\t#ifdef LOCAL\n\tcout.flush();\n\t#endif\n}*/\nvoid possible(bool ex=true){\n\t#ifdef CAPITAL\n\tcout<<\"POSSIBLE\"<<\"\\n\";\n\t#else\n\tcout<<\"Possible\"<<\"\\n\";\n\t#endif\n\tif(ex)exit(0);\n\t#ifdef LOCAL\n\tcout.flush();\n\t#endif\n}\nvoid impossible(bool ex=true){\n\t#ifdef CAPITAL\n\tcout<<\"IMPOSSIBLE\"<<\"\\n\";\n\t#else\n\tcout<<\"Impossible\"<<\"\\n\";\n\t#endif\n\tif(ex)exit(0);\n\t#ifdef LOCAL\n\tcout.flush();\n\t#endif\n}\n\nconstexpr ll ten(int n){\n\treturn n==0?1:ten(n-1)*10;\n}\n\nconst ll infLL=LLONG_MAX/3;\n\n#ifdef int\nconst int inf=infLL;\n#else\nconst int inf=INT_MAX/2-100;\n#endif\n\nint topbit(signed t){\n\treturn t==0?-1:31-__builtin_clz(t);\n}\nint topbit(ll t){\n\treturn t==0?-1:63-__builtin_clzll(t);\n}\nint botbit(signed a){\n\treturn a==0?32:__builtin_ctz(a);\n}\nint botbit(ll a){\n\treturn a==0?64:__builtin_ctzll(a);\n}\nint botbit(ull a){\n\treturn a==0?64:__builtin_ctzll(a);\n}\nint popcount(signed t){\n\treturn __builtin_popcount(t);\n}\nint popcount(ll t){\n\treturn __builtin_popcountll(t);\n}\nint popcount(ull t){\n\treturn __builtin_popcountll(t);\n}\nbool ispow2(int i){\n\treturn i&&(i&-i)==i;\n}\nll mask(int i){\n\treturn (ll(1)<<i)-1;\n}\n\nbool inc(int a,int b,int c){\n\treturn a<=b&&b<=c;\n}\n\ntemplate<class t> void mkuni(vc<t>&v){\n\tsort(all(v));\n\tv.erase(unique(all(v)),v.ed);\n}\n\nll rand_int(ll l, ll r) { //[l, r]\n\t#ifdef LOCAL\n\tstatic mt19937_64 gen;\n\t#else\n\tstatic mt19937_64 gen(chrono::steady_clock::now().time_since_epoch().count());\n\t#endif\n\treturn uniform_int_distribution<ll>(l, r)(gen);\n}\n\ntemplate<class t>\nvoid myshuffle(vc<t>&a){\n\trep(i,si(a))swap(a[i],a[rand_int(0,i)]);\n}\n\ntemplate<class t>\nint lwb(const vc<t>&v,const t&a){\n\treturn lower_bound(all(v),a)-v.bg;\n}\n\nvvc<int> readGraph(int n,int m){\n\tvvc<int> g(n);\n\trep(i,m){\n\t\tint a,b;\n\t\tcin>>a>>b;\n\t\t//sc.read(a,b);\n\t\ta--;b--;\n\t\tg[a].pb(b);\n\t\tg[b].pb(a);\n\t}\n\treturn g;\n}\n\nvvc<int> readTree(int n){\n\treturn readGraph(n,n-1);\n}\n\nvc<ll> presum(const vi&a){\n\tvc<ll> s(si(a)+1);\n\trep(i,si(a))s[i+1]=s[i]+a[i];\n\treturn s;\n}\n\n//copied from yosupo's library\n//ptARTLY VERIFIED\n\n//USACO 2022 January ptlatinum C\n\n#define GEOF\n\n#ifdef GEOF\nusing ld=long double;\n//using ld=double;\nconst ld PI=acos(ld(-1));\n#else\nusing ld=ll;\n#endif\nconst ld eps=1e-9;\nint sgn(ld a){return a<-eps?-1:(a>eps?1:0);}\nint sgn(ld a,ld b){return sgn(a-b);}\n/*\nusing pt=complex<ld>;\n#define x real()\n#define y imag()\n*/\nstruct pt {\n ld x,y;\n //pt(ld _x = ld(0), ld _y = ld(0)) : x(_x), y(_y) {}\n pt():x(0),y(0){}\n pt(ld xx,ld yy):x(xx),y(yy){}\n pt operator+(const pt& r) const { return {x + r.x, y + r.y}; }\n pt operator-(const pt& r) const { return {x - r.x, y - r.y}; }\n pt operator*(const pt& r) const {\n return {x * r.x - y * r.y, x * r.y + y * r.x};\n }\n\n pt operator*(const ld& r) const { return {x * r, y * r}; }\n pt operator/(const ld& r) const { return {x / r, y / r}; }\n\n pt& operator+=(const pt& r) { return *this = *this + r; }\n pt& operator-=(const pt& r) { return *this = *this - r; }\n pt& operator*=(const pt& r) { return *this = *this * r; }\n pt& operator*=(const ld& r) { return *this = *this * r; }\n pt& operator/=(const ld& r) { return *this = *this / r; }\n\n pt operator-() const { return {-x, -y}; }\n\n bool operator<(const pt& r) const {\n return 2 * sgn(x, r.x) + sgn(y, r.y) < 0;\n }\n bool operator==(const pt& r) const { return sgn((*this - r).rabs()) == 0; }\n bool operator!=(const pt& r) const { return !(*this == r); }\n\n ld norm() const { return x * x + y * y; }\n ld rabs() const { return max(std::abs(x), std::abs(y)); } // robust abs\n pair<ld, ld> to_pair() const { return {x, y}; }\n #ifdef GEOF\n ld abs() const { return sqrt(norm()); }\n ld arg() const { return atan2(y, x); }\n static pt polar(ld le, ld th) { return {le * cos(th), le * sin(th)}; }\n\t#endif\n};\nistream& operator>>(istream& is, pt& p){\n\treturn is>>p.x>>p.y;\n}\nostream& operator<<(ostream& os, const pt& p) {\n return os << \"pt(\" << p.x << \", \" << p.y << \")\";\n}\nld norm(const pt&a){\n\treturn a.norm();\n}\n#ifdef GEOF\nld abs(const pt&a){\n\treturn sqrt(norm(a));\n}\n//XXII Opencup Gpt of Ural K\npt normalized(const pt&a){\n\treturn a/abs(a);\n}\nld arg(const pt&a){return a.arg();}\n//normalize to [-PI,PI)\n//Contest 2: ptKU Contest 1, ptTZ Summer 2022 Day 4\nld normarg(ld a){\n\tld res=fmod(a+PI,2*PI);\n\tif(res<0)res+=PI;\n\telse res-=PI;\n\treturn res;\n}\n//AOJ1183\n//arg between ab\n//assume given lengths are valid\nld arg(ld a,ld b,ld c){\n\treturn acos(min(max((a*a+b*b-c*c)/(2*a*b),ld(-1)),ld(1)));\n}\n#endif\nld norm(const pt&a,const pt&b){\n\treturn (a-b).norm();\n}\nld dot(const pt&a,const pt&b){return a.x*b.x+a.y*b.y;}\nld crs(const pt& a,const pt& b){return a.x*b.y-a.y*b.x;}\nld crs(const pt& a,const pt& b,const pt& c){return crs(b-a,c-a);}\nint ccw(const pt& a,const pt& b){return sgn(crs(a,b));}\nint ccw(const pt& a,const pt& b,const pt& c){return ccw(b-a,c-a);}\n//(-pi,0](0,pi]\nint argtype(const pt&a){\n\tif(sgn(a.y)==0)return a.x<0?1:0;\n\treturn a.y<0?0:1;\n}\nint argcmp(const pt&a,const pt&b){\n\tint at=argtype(a),bt=argtype(b);\n\tif(at!=bt)return at<bt?-1:1;\n\treturn -ccw(a,b);\n};\nbool argless(const pt&a,const pt&b){return argcmp(a,b)<0;}\n//(-2)[a,-1](0)[b,1](2)\nint bet(pt a,pt b,pt c){\n\tpt d=b-a;\n\tld e=dot(d,c-a);\n\tif(sgn(e)<=0)return sgn(e)-1;\n\treturn sgn(e-norm(d))+1;\n}\n//(a,b) を結ぶ直線を考え,x 座標との交点の y 座標を求める\n//(分子,分母)を返す\npair<ld,ld> xcut(const pt&a,const pt&b,ld x){\n\treturn mp(a.y*(b.x-x)-b.y*(a.x-x),b.x-a.x);\n}\n//XXII Opencup Gpt of Ural K\npt rot90(pt a){\n\treturn pt(-a.y,a.x);\n}\n#ifdef GEOF\nld xcutval(const pt&a,const pt&b,ld x){\n\tauto [p,q]=xcut(a,b,x);\n\treturn p/q;\n}\n//AOJ1183\n//Xmas2010 E\n//-+ の 順で返す\n//a の符号により,small/large が決まる\nint qeq(ld a,ld b,ld c,ld&d,ld&e){\n\tif(sgn(a)==0){\n\t\tif(sgn(b)==0)return 0;\n\t\td=-c/b;\n\t\treturn 1;\n\t}\n\tld f=b*b-4*a*c;\n\tif(sgn(f)<0)return 0;\n\tld g=sqrt(max(f,ld(0)));\n\td=(-b-g)/(2*a);\n\te=(-b+g)/(2*a);\n\treturn sgn(f)+1;\n}\n#endif\n\nusing ln=pair<pt,pt>;\npt dir(ln a){return a.b-a.a;}\npt eval(ln a,ld b){return a.a+dir(a)*b;}\nld crs(ln a,pt b){return crs(a.a,a.b,b);}\nint ccw(ln a,pt b){return ccw(a.a,a.b,b);}\nint bet(ln a,pt b){return bet(a.a,a.b,b);}\npt proj(ln a,pt b){\n\tpt c=dir(a);\n\treturn a.a+c*dot(c,b-a.a)/norm(c);\n}\npt refl(ln a,pt b){\n\treturn proj(a,b)*2-b;\n}\n//AOJ2201\nld dlp(ln a,pt b){\n\treturn abs(crs(a,b)/abs(dir(a)));\n}\n//AOJ0153\nld dsp(ln a,pt b){\n\tpt c=proj(a,b);\n\tif(abs(bet(a.a,a.b,c))<=1)return abs(b-c);\n\treturn min(abs(b-a.a),abs(b-a.b));\n}\n//AOJ1157\n//0-no,1-yes(endpoint),2-yes(innner),3-overelap\n//if the two line touch like this\n// x----x----x\n//it returns 1\nint iss(ln a,ln b){\n\tint c1=ccw(a.a,a.b,b.a),c2=ccw(a.a,a.b,b.b);\n\tint d1=ccw(b.a,b.b,a.a),d2=ccw(b.a,b.b,a.b);\n\tif(c1||c2||d1||d2)return 1-max(c1*c2,d1*d2);\n\tint f=bet(a.a,a.b,b.a),g=bet(a.a,a.b,b.b);\n\tif(max(f,g)==-2||min(f,g)==2)return 0;\n\tif(max(f,g)==-1||min(f,g)==1)return 1;\n\treturn 3;\n}\n//segment a intersects line b?\n//endpoints inclusive\nbool isl(ln a,ln b){\n\tint d1=ccw(b.a,b.b,a.a),d2=ccw(b.a,b.b,a.b);\n\treturn d1*d2<=0;\n}\n//AOJ1033\npt cll(ln a,ln b){\n\treturn eval(a,crs(b.a,b.b,a.a)/crs(dir(a),dir(b)));\n}\n//AOJ1157\nld dss(ln a,ln b){\n\tif(iss(a,b))return 0;\n\treturn min({dsp(a,b.a),dsp(a,b.b),dsp(b,a.a),dsp(b,a.b)});\n}\n//AOJ2160\n//反時計回り方向に伸びる垂直二等分線\nln vbis(pt a,pt b){\n\tpt c=(a+b)*ld(0.5),d=b-a;\n\treturn ln(c,pt(c.x-d.y,c.y+d.x));\n}\n\n//cm,cr を pt,C に書き換えたあとコンパイルだけ通した,verify はしてない(バカ?)\n//Contest 2: PKU Contest 1, PTZ Summer 2022 Day 4\nstruct C{\n\tpt c;\n\tld r;\n\tpt eval(ld a){\n\t\treturn c+pt::polar(r,a);\n\t}\n};\nistream& operator>>(istream& is,C& c){\n\treturn is>>c.c>>c.r;\n}\nostream& operator<<(ostream& os,const C& c){\n\treturn os<<\"C{\"<<c.c<<\",\"<<c.r<<\"}\";\n}\n\n//AOJ0153\n//0-no,1-edge,2-in\nint cont(C a,pt b){\n\treturn sgn(a.r-abs(b-a.c))+1;\n}\n//AOJ0153 円じゃなくて円盤\n//0-no,1-touch,2-cross\nint ids(C a,ln b){\n\treturn sgn(a.r-dsp(b,a.c))+1;\n}\n//AOJ0129 (touch以外)\n//0-no(in),1-touch(in),2-cross,3-touch(out),4-no(out)\nint ics(C a,ln b){\n\tint c=ids(a,b);\n\tif(c<=1)return 4-c;\n\treturn sgn(max(abs(b.a-a.c),abs(b.b-a.c))-a.r)+1;\n}\n//AOJ1183\n//eval(b,t) が a と重なる t を c,d に入れる\n//解の個数を返す\nint ccl(C a,ln b,ld&c,ld&d){\n\tpt e=dir(b);\n\tpt f=b.a-a.c;\n\treturn qeq(norm(e),2*dot(e,f),norm(f)-a.r*a.r,c,d);\n}\n//AOJ0023\n//Contest 2: PKU Contest 1, PTZ Summer 2022 Day 4 (2,7)\n//0-apart,1-coincide,2-a<b,3-a<=b,4-a>b,5-a>=b,6-a touch b,7-a cross b\nint icc(C a,C b){\n\tld c=abs(a.c-b.c);\n\tif(sgn(c)==0&&sgn(a.r-b.r)==0)return 1;\n\tint d=sgn(c+a.r-b.r);\n\tif(d<=0)return d+3;\n\tint e=sgn(c+b.r-a.r);\n\tif(e<=0)return e+5;\n\tint f=sgn(a.r+b.r-c);\n\tif(f>=0)return f+6;\n\treturn 0;\n}\n//AOJ2572\n//Contest 2: PKU Contest 1, PTZ Summer 2022 Day 4\n//args of two intersections r,l seen by a.c\n//assume two circles cross\npair<ld,ld> ccc(C a,C b){\n\tld c=arg(b.c-a.c);\n\tld d=arg(a.r,abs(b.c-a.c),b.r);\n\treturn make_pair(c-d,c+d);\n}\n//XXI Opencup GP of Siberia 5\nvc<pt> ccc_list(C a,C b){\n\tint v=icc(a,b);\n\tif(v==0||v==1||v==2||v==4)return {};\n\tauto z=ccc(a,b);\n\tif(v==3||v==5||v==6){\n\t\treturn {a.c+pt::polar(a.r,z.a)};\n\t}else{\n\t\treturn {a.c+pt::polar(a.r,z.a),a.c+pt::polar(a.r,z.b)};\n\t}\n}\n//Contest 2: PKU Contest 1, PTZ Summer 2022 Day 4\n//単位円で偏角 0-x で y*(-dx) を積分した値\nld cutareaarg(ld x){\n\treturn (x-sin(2*x)/2)/2;\n}\n//Contest 2: PKU Contest 1, PTZ Summer 2022 Day 4\nld cutareaarg(ld l,ld r){\n\treturn cutareaarg(r)-cutareaarg(l);\n}\n//Contest 4, PTZ 2022 Winter Day 6 (ICPC Camp Day 1) J\nld cutareaarg(C c,ld l,ld r){\n\tl=normarg(l);\n\tr=normarg(r);\n\treturn (l<=r?cutareaarg(l,r):cutareaarg(l,PI)+cutareaarg(-PI,r))\n\t\t\t*sq(c.r)-c.c.y*c.r*(cos(r)-cos(l));\n}\n//円 a の偏角 b の位置で接する直線\nln tanln(C a,ld b){\n\tpt c=a.eval(b);\n\treturn ln(c,c+pt::polar(1,b+PI/2));\n}\n//AOJ2201\n//Contest 4, PTZ 2022 Winter Day 6 (ICPC Camp Day 1)\n//a と b の共通外接線を引く\n//a の接点の偏角を返す\npair<ld,ld> tangent_points_ext(C a,C b){\n\tpt dif=b.c-a.c;\n\tld w=acos(max<ld>(-1,min<ld>(1,(a.r-b.r)/abs(dif))));\n\tld k=arg(dif);\n\treturn mp(normarg(k-w),normarg(k+w));\n}\npair<ln,ln> tangent_line_ext(C a,C b){\n\tauto [p,q]=tangent_points_ext(a,b);\n\treturn mp(tanln(a,p),tanln(a,q));\n}\n//AOJ2201\n//内接線\npair<ld,ld> tangent_points_in(C a,C b){\n\tpt dif=b.c-a.c;\n\tld w=acos(min<ld>(1,(a.r+b.r)/abs(dif)));\n\tld k=arg(dif);\n\treturn mp(normarg(k-w),normarg(k+w));\n}\npair<ln,ln> tangent_line_in(C a,C b){\n\tauto [p,q]=tangent_points_in(a,b);\n\treturn mp(tanln(a,p),tanln(a,q));\n}\n//AOJ2201(内接外接あたりの verify はできてない)\nvc<ln> tangent_lines(C a,C b){\n\tvc<ln> res;\n\tint v=icc(a,b);\n\tif(v==0||v==3||v==5||v==6||v==7){//out\n\t\tauto [p,q]=tangent_line_ext(a,b);\n\t\tres.pb(p);\n\t\tif(v!=3&&v!=5)res.pb(q); //内接しているときは外接線は 1 つ\n\t}\n\tif(v==0||v==6){//in\n\t\tauto [p,q]=tangent_line_in(a,b);\n\t\tres.pb(p);\n\t\tif(v!=6)res.pb(q); //外接しているときは内接線は 1 つ\n\t}\n\treturn res;\n}\n\n//極大な円だけ残す,重複も消す\n//AOJ1047\n//Contest 4, PTZ 2022 Winter Day 6 (ICPC Camp Day 1) J\n//Contest 5, PTZ 2021 Summer Day 2, NAC 2021 B\nvc<C> simplify_circle_sets(const vc<C>&rw){\n\tvc<C> cs;\n\tfor(auto cur:rw){\n\t\tbool ok=true;\n\t\trep(j,si(cs)){\n\t\t\tint w=icc(cs[j],cur);\n\t\t\tif(w==1||w==4||w==5){\n\t\t\t\tok=false;\n\t\t\t}\n\t\t}\n\t\tif(ok){\n\t\t\trep(j,si(cs)){\n\t\t\t\tint w=icc(cs[j],cur);\n\t\t\t\tif(w==1||w==2||w==3){\n\t\t\t\t\tcs.erase(cs.bg+j--);\n\t\t\t\t}\n\t\t\t}\n\t\t\tcs.pb(cur);\n\t\t}\n\t}\n\treturn cs;\n}\n\n//ByteCamp 2022 Day3 A\n//原点中心,半径 r の円の x 座標 a 以下の領域の面積\nld cutarea_sub(ld r,ld a){\n\tld theta=acos(max(min(a/r,ld(1)),ld(-1)));\n\treturn sq(r)*(PI-theta)+a*r*sin(theta);\n}\n//2020 Multi-Uni Day5 J\n//x座標 b 以上 c 以下の部分だけ取ってきて,その面積\nld cutarea(C a,ld b,ld c){\n\tchmax(b,a.c.x-a.r);\n\tchmin(c,a.c.x+a.r);\n\tb-=a.c.x;\n\tc-=a.c.x;\n\tif(sgn(c-b)<=0)return 0;\n\treturn cutarea_sub(a.r,c)-cutarea_sub(a.r,b);\n}\n\n//AOJ2423\nC circumc(pt a,pt b,pt c){\n\tb-=a;\n\tc-=a;\n\tpt r=c*norm(b)-b*norm(c);\n\tr=pt(r.y,-r.x)/(2*crs(b,c));\n\treturn {a+r,abs(r)};\n}\n//AOJ2423\nC mindisc(const vc<pt>& p,array<pt,3> q,int i,int j){\n\tif(i==int(p.size())){\n\t\tif(j==0)\n\t\t\treturn {{0,0},-1};\n\t\telse if(j==1)\n\t\t\treturn {q[0],0};\n\t\telse if(j==2)\n\t\t\treturn {(q[0]+q[1])*ld(0.5),abs(q[0]-q[1])/2};\n\t\telse if(j==3)\n\t\t\treturn circumc(q[0],q[1],q[2]);\n\t\telse\n\t\t\tassert(false);\n\t}\n\tC c=mindisc(p,q,i+1,j);\n\tif(sgn(abs(c.c-p[i])-c.r)==1){\n\t\tassert(j<3);\n\t\tq[j]=p[i];\n\t\treturn mindisc(p,q,i+1,j+1);\n\t}else\n\t\treturn c;\n}\nC mindisc(vc<pt> p){\n\tshuffle(all(p),mt19937());\n\treturn mindisc(p,array<pt,3>(),0,0);\n}\n\n//CF773F\n//p の中で k 点以上含むような円の最小半径\n\nvoid slv(){\n\tint n;cin>>n;if(n==0)exit(0);\n\tvc<pt> c(n);\n\tvc<ld> a(n),b(n);\n\trep(i,n){\n\t\tcin>>c[i]>>a[i]>>b[i];\n\t\tb[i]+=a[i];\n\t}\n\tint ans=0;\n\tauto work=[&](ln z){\n\t\tint cnt=0;\n\t\trep(i,n){\n\t\t\tld r=dlp(z,c[i]);\n\t\t\tif(sgn(a[i],r)<=0&&sgn(r,b[i])<=0)cnt++;\n\t\t}\n\t\tchmax(ans,cnt);\n\t};\n\trep(i,n){\n\t\tpt v=c[i];\n\t\tv.y-=a[i];\n\t\twork(ln(v,pt(v.x+1,v.y)));\n\t}\n\trep(i,n){\n\t\tpt v=c[i];\n\t\tv.y-=b[i];\n\t\twork(ln(v,pt(v.x+1,v.y)));\n\t}\n\trep(i,n)for(auto x:{a[i],b[i]})rng(j,i+1,n)for(auto y:{a[j],b[j]}){\n\t\tvc<ln> ls=tangent_lines(C{c[i],x},C{c[j],y});\n\t\tfor_each(all(ls),work);\n\t}\n\tcout<<ans<<endl;\n}\n\nsigned main(){\n\tcin.tie(0);\n\tios::sync_with_stdio(0);\n\tcout<<fixed<<setprecision(20);\n\t\n\twhile(1)slv();\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 3572, "score_of_the_acc": -0.1049, "final_rank": 6 }, { "submission_id": "aoj_2201_7062770", "code_snippet": "#ifndef LOCAL\n#pragma GCC optimize (\"Ofast\")\n#pragma GCC optimize (\"unroll-loops\")\n#endif\n\n#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll=long long;\n#define int ll\n\n#define rng(i,a,b) for(int i=int(a);i<int(b);i++)\n#define rep(i,b) rng(i,0,b)\n#define gnr(i,a,b) for(int i=int(b)-1;i>=int(a);i--)\n#define per(i,b) gnr(i,0,b)\n#define pb push_back\n#define eb emplace_back\n#define a first\n#define b second\n#define bg begin()\n#define ed end()\n#define all(x) x.bg,x.ed\n#define si(x) int(x.size())\n#ifdef LOCAL\n#define dmp(x) cerr<<__LINE__<<\" \"<<#x<<\" \"<<x<<endl\n#else\n#define dmp(x) void(0)\n#endif\n\ntemplate<class t,class u> bool chmax(t&a,u b){if(a<b){a=b;return true;}else return false;}\ntemplate<class t,class u> bool chmin(t&a,u b){if(b<a){a=b;return true;}else return false;}\n\ntemplate<class t> using vc=vector<t>;\ntemplate<class t> using vvc=vc<vc<t>>;\n\nusing pi=pair<int,int>;\nusing vi=vc<int>;\n\ntemplate<class t,class u>\nostream& operator<<(ostream& os,const pair<t,u>& p){\n\treturn os<<\"{\"<<p.a<<\",\"<<p.b<<\"}\";\n}\n\ntemplate<class t> ostream& operator<<(ostream& os,const vc<t>& v){\n\tos<<\"{\";\n\tfor(auto e:v)os<<e<<\",\";\n\treturn os<<\"}\";\n}\n\n#define mp make_pair\n#define mt make_tuple\n#define one(x) memset(x,-1,sizeof(x))\n#define zero(x) memset(x,0,sizeof(x))\n#ifdef LOCAL\nvoid dmpr(ostream&os){os<<endl;}\ntemplate<class T,class... Args>\nvoid dmpr(ostream&os,const T&t,const Args&... args){\n\tos<<t<<\" \";\n\tdmpr(os,args...);\n}\n#define dmp2(...) dmpr(cerr,__LINE__,##__VA_ARGS__)\n#else\n#define dmp2(...) void(0)\n#endif\n\nusing uint=unsigned;\nusing ull=unsigned long long;\n\ntemplate<class t,size_t n>\nostream& operator<<(ostream&os,const array<t,n>&a){\n\treturn os<<vc<t>(all(a));\n}\n\ntemplate<int i,class T>\nvoid print_tuple(ostream&,const T&){\n}\n\ntemplate<int i,class T,class H,class ...Args>\nvoid print_tuple(ostream&os,const T&t){\n\tif(i)os<<\",\";\n\tos<<get<i>(t);\n\tprint_tuple<i+1,T,Args...>(os,t);\n}\n\ntemplate<class ...Args>\nostream& operator<<(ostream&os,const tuple<Args...>&t){\n\tos<<\"{\";\n\tprint_tuple<0,tuple<Args...>,Args...>(os,t);\n\treturn os<<\"}\";\n}\n\ntemplate<class t>\nvoid print(t x,int suc=1){\n\tcout<<x;\n\tif(suc==1)\n\t\tcout<<\"\\n\";\n\tif(suc==2)\n\t\tcout<<\" \";\n}\n\nll read(){\n\tll i;\n\tcin>>i;\n\treturn i;\n}\n\nvi readvi(int n,int off=0){\n\tvi v(n);\n\trep(i,n)v[i]=read()+off;\n\treturn v;\n}\n\npi readpi(int off=0){\n\tint a,b;cin>>a>>b;\n\treturn pi(a+off,b+off);\n}\n\ntemplate<class t,class u>\nvoid print(const pair<t,u>&p,int suc=1){\n\tprint(p.a,2);\n\tprint(p.b,suc);\n}\n\ntemplate<class t,class u>\nvoid print_offset(const pair<t,u>&p,ll off,int suc=1){\n\tprint(p.a+off,2);\n\tprint(p.b+off,suc);\n}\n\ntemplate<class T>\nvoid print(const vector<T>&v,int suc=1){\n\trep(i,v.size())\n\t\tprint(v[i],i==int(v.size())-1?suc:2);\n}\n\ntemplate<class T>\nvoid print_offset(const vector<T>&v,ll off,int suc=1){\n\trep(i,v.size())\n\t\tprint(v[i]+off,i==int(v.size())-1?suc:2);\n}\n\ntemplate<class T,size_t N>\nvoid print(const array<T,N>&v,int suc=1){\n\trep(i,N)\n\t\tprint(v[i],i==int(N)-1?suc:2);\n}\n\nstring readString(){\n\tstring s;\n\tcin>>s;\n\treturn s;\n}\n\ntemplate<class T>\nT sq(const T& t){\n\treturn t*t;\n}\n\nvoid YES(bool ex=true){\n\tcout<<\"YES\\n\";\n\tif(ex)exit(0);\n\t#ifdef LOCAL\n\tcout.flush();\n\t#endif\n}\nvoid NO(bool ex=true){\n\tcout<<\"NO\\n\";\n\tif(ex)exit(0);\n\t#ifdef LOCAL\n\tcout.flush();\n\t#endif\n}\nvoid Yes(bool ex=true){\n\tcout<<\"Yes\\n\";\n\tif(ex)exit(0);\n\t#ifdef LOCAL\n\tcout.flush();\n\t#endif\n}\nvoid No(bool ex=true){\n\tcout<<\"No\\n\";\n\tif(ex)exit(0);\n\t#ifdef LOCAL\n\tcout.flush();\n\t#endif\n}\n//#define CAPITAL\n/*\nvoid yes(bool ex=true){\n\t#ifdef CAPITAL\n\tcout<<\"YES\"<<\"\\n\";\n\t#else\n\tcout<<\"Yes\"<<\"\\n\";\n\t#endif\n\tif(ex)exit(0);\n\t#ifdef LOCAL\n\tcout.flush();\n\t#endif\n}\nvoid no(bool ex=true){\n\t#ifdef CAPITAL\n\tcout<<\"NO\"<<\"\\n\";\n\t#else\n\tcout<<\"No\"<<\"\\n\";\n\t#endif\n\tif(ex)exit(0);\n\t#ifdef LOCAL\n\tcout.flush();\n\t#endif\n}*/\nvoid possible(bool ex=true){\n\t#ifdef CAPITAL\n\tcout<<\"POSSIBLE\"<<\"\\n\";\n\t#else\n\tcout<<\"Possible\"<<\"\\n\";\n\t#endif\n\tif(ex)exit(0);\n\t#ifdef LOCAL\n\tcout.flush();\n\t#endif\n}\nvoid impossible(bool ex=true){\n\t#ifdef CAPITAL\n\tcout<<\"IMPOSSIBLE\"<<\"\\n\";\n\t#else\n\tcout<<\"Impossible\"<<\"\\n\";\n\t#endif\n\tif(ex)exit(0);\n\t#ifdef LOCAL\n\tcout.flush();\n\t#endif\n}\n\nconstexpr ll ten(int n){\n\treturn n==0?1:ten(n-1)*10;\n}\n\nconst ll infLL=LLONG_MAX/3;\n\n#ifdef int\nconst int inf=infLL;\n#else\nconst int inf=INT_MAX/2-100;\n#endif\n\nint topbit(signed t){\n\treturn t==0?-1:31-__builtin_clz(t);\n}\nint topbit(ll t){\n\treturn t==0?-1:63-__builtin_clzll(t);\n}\nint botbit(signed a){\n\treturn a==0?32:__builtin_ctz(a);\n}\nint botbit(ll a){\n\treturn a==0?64:__builtin_ctzll(a);\n}\nint botbit(ull a){\n\treturn a==0?64:__builtin_ctzll(a);\n}\nint popcount(signed t){\n\treturn __builtin_popcount(t);\n}\nint popcount(ll t){\n\treturn __builtin_popcountll(t);\n}\nint popcount(ull t){\n\treturn __builtin_popcountll(t);\n}\nbool ispow2(int i){\n\treturn i&&(i&-i)==i;\n}\nll mask(int i){\n\treturn (ll(1)<<i)-1;\n}\n\nbool inc(int a,int b,int c){\n\treturn a<=b&&b<=c;\n}\n\ntemplate<class t> void mkuni(vc<t>&v){\n\tsort(all(v));\n\tv.erase(unique(all(v)),v.ed);\n}\n\nll rand_int(ll l, ll r) { //[l, r]\n\t#ifdef LOCAL\n\tstatic mt19937_64 gen;\n\t#else\n\tstatic mt19937_64 gen(chrono::steady_clock::now().time_since_epoch().count());\n\t#endif\n\treturn uniform_int_distribution<ll>(l, r)(gen);\n}\n\ntemplate<class t>\nvoid myshuffle(vc<t>&a){\n\trep(i,si(a))swap(a[i],a[rand_int(0,i)]);\n}\n\ntemplate<class t>\nint lwb(const vc<t>&v,const t&a){\n\treturn lower_bound(all(v),a)-v.bg;\n}\n\nvvc<int> readGraph(int n,int m){\n\tvvc<int> g(n);\n\trep(i,m){\n\t\tint a,b;\n\t\tcin>>a>>b;\n\t\t//sc.read(a,b);\n\t\ta--;b--;\n\t\tg[a].pb(b);\n\t\tg[b].pb(a);\n\t}\n\treturn g;\n}\n\nvvc<int> readTree(int n){\n\treturn readGraph(n,n-1);\n}\n\nvc<ll> presum(const vi&a){\n\tvc<ll> s(si(a)+1);\n\trep(i,si(a))s[i+1]=s[i]+a[i];\n\treturn s;\n}\n\n//copied from yosupo's library\n//ptARTLY VERIFIED\n\n//USACO 2022 January ptlatinum C\n\n#define GEOF\n\n#ifdef GEOF\nusing ld=long double;\n//using ld=double;\nconst ld PI=acos(ld(-1));\n#else\nusing ld=ll;\n#endif\nconst ld eps=1e-9;\nint sgn(ld a){return a<-eps?-1:(a>eps?1:0);}\nint sgn(ld a,ld b){return sgn(a-b);}\n/*\nusing pt=complex<ld>;\n#define x real()\n#define y imag()\n*/\nstruct pt {\n ld x,y;\n //pt(ld _x = ld(0), ld _y = ld(0)) : x(_x), y(_y) {}\n pt():x(0),y(0){}\n pt(ld xx,ld yy):x(xx),y(yy){}\n pt operator+(const pt& r) const { return {x + r.x, y + r.y}; }\n pt operator-(const pt& r) const { return {x - r.x, y - r.y}; }\n pt operator*(const pt& r) const {\n return {x * r.x - y * r.y, x * r.y + y * r.x};\n }\n\n pt operator*(const ld& r) const { return {x * r, y * r}; }\n pt operator/(const ld& r) const { return {x / r, y / r}; }\n\n pt& operator+=(const pt& r) { return *this = *this + r; }\n pt& operator-=(const pt& r) { return *this = *this - r; }\n pt& operator*=(const pt& r) { return *this = *this * r; }\n pt& operator*=(const ld& r) { return *this = *this * r; }\n pt& operator/=(const ld& r) { return *this = *this / r; }\n\n pt operator-() const { return {-x, -y}; }\n\n bool operator<(const pt& r) const {\n return 2 * sgn(x, r.x) + sgn(y, r.y) < 0;\n }\n bool operator==(const pt& r) const { return sgn((*this - r).rabs()) == 0; }\n bool operator!=(const pt& r) const { return !(*this == r); }\n\n ld norm() const { return x * x + y * y; }\n ld rabs() const { return max(std::abs(x), std::abs(y)); } // robust abs\n pair<ld, ld> to_pair() const { return {x, y}; }\n #ifdef GEOF\n ld abs() const { return sqrt(norm()); }\n ld arg() const { return atan2(y, x); }\n static pt polar(ld le, ld th) { return {le * cos(th), le * sin(th)}; }\n\t#endif\n};\nistream& operator>>(istream& is, pt& p){\n\treturn is>>p.x>>p.y;\n}\nostream& operator<<(ostream& os, const pt& p) {\n return os << \"pt(\" << p.x << \", \" << p.y << \")\";\n}\nld norm(const pt&a){\n\treturn a.norm();\n}\n#ifdef GEOF\nld abs(const pt&a){\n\treturn sqrt(norm(a));\n}\n//XXII Opencup Gpt of Ural K\npt normalized(const pt&a){\n\treturn a/abs(a);\n}\nld arg(const pt&a){return a.arg();}\n//normalize to [-PI,PI)\n//Contest 2: ptKU Contest 1, ptTZ Summer 2022 Day 4\nld normarg(ld a){\n\tld res=fmod(a+PI,2*PI);\n\tif(res<0)res+=PI;\n\telse res-=PI;\n\treturn res;\n}\n//AOJ1183\n//arg between ab\n//assume given lengths are valid\nld arg(ld a,ld b,ld c){\n\treturn acos(min(max((a*a+b*b-c*c)/(2*a*b),ld(-1)),ld(1)));\n}\n#endif\nld norm(const pt&a,const pt&b){\n\treturn (a-b).norm();\n}\nld dot(const pt&a,const pt&b){return a.x*b.x+a.y*b.y;}\nld crs(const pt& a,const pt& b){return a.x*b.y-a.y*b.x;}\nld crs(const pt& a,const pt& b,const pt& c){return crs(b-a,c-a);}\nint ccw(const pt& a,const pt& b){return sgn(crs(a,b));}\nint ccw(const pt& a,const pt& b,const pt& c){return ccw(b-a,c-a);}\n//(-pi,0](0,pi]\nint argtype(const pt&a){\n\tif(sgn(a.y)==0)return a.x<0?1:0;\n\treturn a.y<0?0:1;\n}\nint argcmp(const pt&a,const pt&b){\n\tint at=argtype(a),bt=argtype(b);\n\tif(at!=bt)return at<bt?-1:1;\n\treturn -ccw(a,b);\n};\nbool argless(const pt&a,const pt&b){return argcmp(a,b)<0;}\n//(-2)[a,-1](0)[b,1](2)\nint bet(pt a,pt b,pt c){\n\tpt d=b-a;\n\tld e=dot(d,c-a);\n\tif(sgn(e)<=0)return sgn(e)-1;\n\treturn sgn(e-norm(d))+1;\n}\n//(a,b) を結ぶ直線を考え,x 座標との交点の y 座標を求める\n//(分子,分母)を返す\npair<ld,ld> xcut(const pt&a,const pt&b,ld x){\n\treturn mp(a.y*(b.x-x)-b.y*(a.x-x),b.x-a.x);\n}\n//XXII Opencup Gpt of Ural K\npt rot90(pt a){\n\treturn pt(-a.y,a.x);\n}\n#ifdef GEOF\nld xcutval(const pt&a,const pt&b,ld x){\n\tauto [p,q]=xcut(a,b,x);\n\treturn p/q;\n}\n//AOJ1183\n//Xmas2010 E\n//-+ の 順で返す\n//a の符号により,small/large が決まる\nint qeq(ld a,ld b,ld c,ld&d,ld&e){\n\tif(sgn(a)==0){\n\t\tif(sgn(b)==0)return 0;\n\t\td=-c/b;\n\t\treturn 1;\n\t}\n\tld f=b*b-4*a*c;\n\tif(sgn(f)<0)return 0;\n\tld g=sqrt(max(f,ld(0)));\n\td=(-b-g)/(2*a);\n\te=(-b+g)/(2*a);\n\treturn sgn(f)+1;\n}\n#endif\n\nusing ln=pair<pt,pt>;\npt dir(ln a){return a.b-a.a;}\npt eval(ln a,ld b){return a.a+dir(a)*b;}\nld crs(ln a,pt b){return crs(a.a,a.b,b);}\nint ccw(ln a,pt b){return ccw(a.a,a.b,b);}\nint bet(ln a,pt b){return bet(a.a,a.b,b);}\npt proj(ln a,pt b){\n\tpt c=dir(a);\n\treturn a.a+c*dot(c,b-a.a)/norm(c);\n}\npt refl(ln a,pt b){\n\treturn proj(a,b)*2-b;\n}\n//AOJ2201\nld dlp(ln a,pt b){\n\treturn abs(crs(a,b)/abs(dir(a)));\n}\n//AOJ0153\nld dsp(ln a,pt b){\n\tpt c=proj(a,b);\n\tif(abs(bet(a.a,a.b,c))<=1)return abs(b-c);\n\treturn min(abs(b-a.a),abs(b-a.b));\n}\n//AOJ1157\n//0-no,1-yes(endpoint),2-yes(innner),3-overelap\n//if the two line touch like this\n// x----x----x\n//it returns 1\nint iss(ln a,ln b){\n\tint c1=ccw(a.a,a.b,b.a),c2=ccw(a.a,a.b,b.b);\n\tint d1=ccw(b.a,b.b,a.a),d2=ccw(b.a,b.b,a.b);\n\tif(c1||c2||d1||d2)return 1-max(c1*c2,d1*d2);\n\tint f=bet(a.a,a.b,b.a),g=bet(a.a,a.b,b.b);\n\tif(max(f,g)==-2||min(f,g)==2)return 0;\n\tif(max(f,g)==-1||min(f,g)==1)return 1;\n\treturn 3;\n}\n//segment a intersects line b?\n//endpoints inclusive\nbool isl(ln a,ln b){\n\tint d1=ccw(b.a,b.b,a.a),d2=ccw(b.a,b.b,a.b);\n\treturn d1*d2<=0;\n}\n//AOJ1033\npt cll(ln a,ln b){\n\treturn eval(a,crs(b.a,b.b,a.a)/crs(dir(a),dir(b)));\n}\n//AOJ1157\nld dss(ln a,ln b){\n\tif(iss(a,b))return 0;\n\treturn min({dsp(a,b.a),dsp(a,b.b),dsp(b,a.a),dsp(b,a.b)});\n}\n//AOJ2160\n//反時計回り方向に伸びる垂直二等分線\nln vbis(pt a,pt b){\n\tpt c=(a+b)*ld(0.5),d=b-a;\n\treturn ln(c,pt(c.x-d.y,c.y+d.x));\n}\n\n//cm,cr を pt,C に書き換えたあとコンパイルだけ通した,verify はしてない(バカ?)\n//Contest 2: PKU Contest 1, PTZ Summer 2022 Day 4\nstruct C{\n\tpt c;\n\tld r;\n\tpt eval(ld a){\n\t\treturn c+pt::polar(r,a);\n\t}\n};\nistream& operator>>(istream& is,C& c){\n\treturn is>>c.c>>c.r;\n}\nostream& operator<<(ostream& os,const C& c){\n\treturn os<<\"C{\"<<c.c<<\",\"<<c.r<<\"}\";\n}\n\n//AOJ0153\n//0-no,1-edge,2-in\nint cont(C a,pt b){\n\treturn sgn(a.r-abs(b-a.c))+1;\n}\n//AOJ0153 円じゃなくて円盤\n//0-no,1-touch,2-cross\nint ids(C a,ln b){\n\treturn sgn(a.r-dsp(b,a.c))+1;\n}\n//AOJ0129 (touch以外)\n//0-no(in),1-touch(in),2-cross,3-touch(out),4-no(out)\nint ics(C a,ln b){\n\tint c=ids(a,b);\n\tif(c<=1)return 4-c;\n\treturn sgn(max(abs(b.a-a.c),abs(b.b-a.c))-a.r)+1;\n}\n//AOJ1183\n//eval(b,t) が a と重なる t を c,d に入れる\n//解の個数を返す\nint ccl(C a,ln b,ld&c,ld&d){\n\tpt e=dir(b);\n\tpt f=b.a-a.c;\n\treturn qeq(norm(e),2*dot(e,f),norm(f)-a.r*a.r,c,d);\n}\n//AOJ0023\n//Contest 2: PKU Contest 1, PTZ Summer 2022 Day 4 (2,7)\n//0-apart,1-coincide,2-a<b,3-a<=b,4-a>b,5-a>=b,6-a touch b,7-a cross b\nint icc(C a,C b){\n\tld c=abs(a.c-b.c);\n\tif(sgn(c)==0&&sgn(a.r-b.r)==0)return 1;\n\tint d=sgn(c+a.r-b.r);\n\tif(d<=0)return d+3;\n\tint e=sgn(c+b.r-a.r);\n\tif(e<=0)return e+5;\n\tint f=sgn(a.r+b.r-c);\n\tif(f>=0)return f+6;\n\treturn 0;\n}\n//AOJ2572\n//Contest 2: PKU Contest 1, PTZ Summer 2022 Day 4\n//args of two intersections r,l seen by a.c\n//assume two circles cross\npair<ld,ld> ccc(C a,C b){\n\tld c=arg(b.c-a.c);\n\tld d=arg(a.r,abs(b.c-a.c),b.r);\n\treturn make_pair(c-d,c+d);\n}\n//XXI Opencup GP of Siberia 5\nvc<pt> ccc_list(C a,C b){\n\tint v=icc(a,b);\n\tif(v==0||v==1||v==2||v==4)return {};\n\tauto z=ccc(a,b);\n\tif(v==3||v==5||v==6){\n\t\treturn {a.c+pt::polar(a.r,z.a)};\n\t}else{\n\t\treturn {a.c+pt::polar(a.r,z.a),a.c+pt::polar(a.r,z.b)};\n\t}\n}\n//Contest 2: PKU Contest 1, PTZ Summer 2022 Day 4\n//単位円で偏角 0-x で y*(-dx) を積分した値\nld cutareaarg(ld x){\n\treturn (x-sin(2*x)/2)/2;\n}\n//Contest 2: PKU Contest 1, PTZ Summer 2022 Day 4\nld cutareaarg(ld l,ld r){\n\treturn cutareaarg(r)-cutareaarg(l);\n}\n//Contest 4, PTZ 2022 Winter Day 6 (ICPC Camp Day 1) J\nld cutareaarg(C c,ld l,ld r){\n\tl=normarg(l);\n\tr=normarg(r);\n\treturn (l<=r?cutareaarg(l,r):cutareaarg(l,PI)+cutareaarg(-PI,r))\n\t\t\t*sq(c.r)-c.c.y*c.r*(cos(r)-cos(l));\n}\n//円 a の偏角 b の位置で接する直線\nln tanln(C a,ld b){\n\tpt c=a.eval(b);\n\treturn ln(c,c+pt::polar(1,b+PI/2));\n}\n//AOJ2201\n//Contest 4, PTZ 2022 Winter Day 6 (ICPC Camp Day 1)\n//a と b の共通外接線を引く\n//a の接点の偏角を返す\npair<ld,ld> tangent_points_ext(C a,C b){\n\tpt dif=b.c-a.c;\n\tld w=acos((a.r-b.r)/abs(dif));\n\tld k=arg(dif);\n\treturn mp(normarg(k-w),normarg(k+w));\n}\npair<ln,ln> tangent_line_ext(C a,C b){\n\t/*auto [p,q]=tangent_points_ext(a,b);\n\treturn mp(tanln(a,p),tanln(a,q));*/\n\tauto [p,q]=tangent_points_ext(a,b);\n\tauto [r,s]=tangent_points_ext(b,a);\n\treturn mp(ln(a.eval(p),b.eval(s)),ln(a.eval(q),b.eval(r)));\n}\n//a>=b で内接しているケース\nln tangent_line_ext2(C a,C b){\n\treturn tanln(a,arg(b.c-a.c));\n}\n//AOJ2201\n//内接線\npair<ld,ld> tangent_points_in(C a,C b){\n\tpt dif=b.c-a.c;\n\tld w=acos((a.r+b.r)/abs(dif));\n\tld k=arg(dif);\n\treturn mp(normarg(k-w),normarg(k+w));\n}\npair<ln,ln> tangent_line_in(C a,C b){\n\t/*auto [p,q]=tangent_points_in(a,b);\n\treturn mp(tanln(a,p),tanln(a,q));*/\n\tauto [p,q]=tangent_points_in(a,b);\n\tauto [r,s]=tangent_points_in(b,a);\n\treturn mp(ln(a.eval(p),b.eval(r)),ln(a.eval(q),b.eval(s)));\n}\n//外接しているケース\nln tangent_line_in2(C a,C b){\n\treturn tanln(a,arg(b.c-a.c));\n}\n//AOJ2201(ext2,in2 あたりの verify はできてない)\nvc<ln> tangent_lines(C a,C b){\n\tvc<ln> res;\n\tint v=icc(a,b);\n\t//out\n\tif(v==0||v==6||v==7){\n\t\tauto [p,q]=tangent_line_ext(a,b);\n\t\tres.pb(p);\n\t\tres.pb(q);\n\t}\n\tif(v==5)res.pb(tangent_line_ext2(a,b));\n\tif(v==3)res.pb(tangent_line_ext2(b,a));\n\t//in\n\tif(v==0){\n\t\tauto [p,q]=tangent_line_in(a,b);\n\t\tres.pb(p);\n\t\tres.pb(q);\n\t}\n\tif(v==6)res.pb(tangent_line_in2(a,b));\n\treturn res;\n}\n\n//極大な円だけ残す,重複も消す\n//AOJ1047\n//Contest 4, PTZ 2022 Winter Day 6 (ICPC Camp Day 1) J\n//Contest 5, PTZ 2021 Summer Day 2, NAC 2021 B\nvc<C> simplify_circle_sets(const vc<C>&rw){\n\tvc<C> cs;\n\tfor(auto cur:rw){\n\t\tbool ok=true;\n\t\trep(j,si(cs)){\n\t\t\tint w=icc(cs[j],cur);\n\t\t\tif(w==1||w==4||w==5){\n\t\t\t\tok=false;\n\t\t\t}\n\t\t}\n\t\tif(ok){\n\t\t\trep(j,si(cs)){\n\t\t\t\tint w=icc(cs[j],cur);\n\t\t\t\tif(w==1||w==2||w==3){\n\t\t\t\t\tcs.erase(cs.bg+j--);\n\t\t\t\t}\n\t\t\t}\n\t\t\tcs.pb(cur);\n\t\t}\n\t}\n\treturn cs;\n}\n\n//ByteCamp 2022 Day3 A\n//原点中心,半径 r の円の x 座標 a 以下の領域の面積\nld cutarea_sub(ld r,ld a){\n\tld theta=acos(max(min(a/r,ld(1)),ld(-1)));\n\treturn sq(r)*(PI-theta)+a*r*sin(theta);\n}\n//2020 Multi-Uni Day5 J\n//x座標 b 以上 c 以下の部分だけ取ってきて,その面積\nld cutarea(C a,ld b,ld c){\n\tchmax(b,a.c.x-a.r);\n\tchmin(c,a.c.x+a.r);\n\tb-=a.c.x;\n\tc-=a.c.x;\n\tif(sgn(c-b)<=0)return 0;\n\treturn cutarea_sub(a.r,c)-cutarea_sub(a.r,b);\n}\n\n//AOJ2423\nC circumc(pt a,pt b,pt c){\n\tb-=a;\n\tc-=a;\n\tpt r=c*norm(b)-b*norm(c);\n\tr=pt(r.y,-r.x)/(2*crs(b,c));\n\treturn {a+r,abs(r)};\n}\n//AOJ2423\nC mindisc(const vc<pt>& p,array<pt,3> q,int i,int j){\n\tif(i==int(p.size())){\n\t\tif(j==0)\n\t\t\treturn {{0,0},-1};\n\t\telse if(j==1)\n\t\t\treturn {q[0],0};\n\t\telse if(j==2)\n\t\t\treturn {(q[0]+q[1])*ld(0.5),abs(q[0]-q[1])/2};\n\t\telse if(j==3)\n\t\t\treturn circumc(q[0],q[1],q[2]);\n\t\telse\n\t\t\tassert(false);\n\t}\n\tC c=mindisc(p,q,i+1,j);\n\tif(sgn(abs(c.c-p[i])-c.r)==1){\n\t\tassert(j<3);\n\t\tq[j]=p[i];\n\t\treturn mindisc(p,q,i+1,j+1);\n\t}else\n\t\treturn c;\n}\nC mindisc(vc<pt> p){\n\tshuffle(all(p),mt19937());\n\treturn mindisc(p,array<pt,3>(),0,0);\n}\n\n//CF773F\n//p の中で k 点以上含むような円の最小半径\n\nvoid slv(){\n\tint n;cin>>n;if(n==0)exit(0);\n\tvc<pt> c(n);\n\tvc<ld> a(n),b(n);\n\trep(i,n){\n\t\tcin>>c[i]>>a[i]>>b[i];\n\t\tb[i]+=a[i];\n\t}\n\tint ans=0;\n\tauto work=[&](ln z){\n\t\tint cnt=0;\n\t\trep(i,n){\n\t\t\tld r=dlp(z,c[i]);\n\t\t\tif(sgn(a[i],r)<=0&&sgn(r,b[i])<=0)cnt++;\n\t\t}\n\t\tchmax(ans,cnt);\n\t};\n\trep(i,n){\n\t\tpt v=c[i];\n\t\tv.y-=a[i];\n\t\twork(ln(v,pt(v.x+1,v.y)));\n\t}\n\trep(i,n){\n\t\tpt v=c[i];\n\t\tv.y-=b[i];\n\t\twork(ln(v,pt(v.x+1,v.y)));\n\t}\n\trep(i,n)for(auto x:{a[i],b[i]})rng(j,i+1,n)for(auto y:{a[j],b[j]}){\n\t\tvc<ln> ls=tangent_lines(C{c[i],x},C{c[j],y});\n\t\tfor_each(all(ls),work);\n\t}\n\tcout<<ans<<endl;\n}\n\nsigned main(){\n\tcin.tie(0);\n\tios::sync_with_stdio(0);\n\tcout<<fixed<<setprecision(20);\n\t\n\twhile(1)slv();\n}", "accuracy": 1, "time_ms": 120, "memory_kb": 3508, "score_of_the_acc": -0.0971, "final_rank": 5 }, { "submission_id": "aoj_2201_7062692", "code_snippet": "#ifndef LOCAL\n#pragma GCC optimize (\"Ofast\")\n#pragma GCC optimize (\"unroll-loops\")\n#endif\n\n#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll=long long;\n#define int ll\n\n#define rng(i,a,b) for(int i=int(a);i<int(b);i++)\n#define rep(i,b) rng(i,0,b)\n#define gnr(i,a,b) for(int i=int(b)-1;i>=int(a);i--)\n#define per(i,b) gnr(i,0,b)\n#define pb push_back\n#define eb emplace_back\n#define a first\n#define b second\n#define bg begin()\n#define ed end()\n#define all(x) x.bg,x.ed\n#define si(x) int(x.size())\n#ifdef LOCAL\n#define dmp(x) cerr<<__LINE__<<\" \"<<#x<<\" \"<<x<<endl\n#else\n#define dmp(x) void(0)\n#endif\n\ntemplate<class t,class u> bool chmax(t&a,u b){if(a<b){a=b;return true;}else return false;}\ntemplate<class t,class u> bool chmin(t&a,u b){if(b<a){a=b;return true;}else return false;}\n\ntemplate<class t> using vc=vector<t>;\ntemplate<class t> using vvc=vc<vc<t>>;\n\nusing pi=pair<int,int>;\nusing vi=vc<int>;\n\ntemplate<class t,class u>\nostream& operator<<(ostream& os,const pair<t,u>& p){\n\treturn os<<\"{\"<<p.a<<\",\"<<p.b<<\"}\";\n}\n\ntemplate<class t> ostream& operator<<(ostream& os,const vc<t>& v){\n\tos<<\"{\";\n\tfor(auto e:v)os<<e<<\",\";\n\treturn os<<\"}\";\n}\n\n#define mp make_pair\n#define mt make_tuple\n#define one(x) memset(x,-1,sizeof(x))\n#define zero(x) memset(x,0,sizeof(x))\n#ifdef LOCAL\nvoid dmpr(ostream&os){os<<endl;}\ntemplate<class T,class... Args>\nvoid dmpr(ostream&os,const T&t,const Args&... args){\n\tos<<t<<\" \";\n\tdmpr(os,args...);\n}\n#define dmp2(...) dmpr(cerr,__LINE__,##__VA_ARGS__)\n#else\n#define dmp2(...) void(0)\n#endif\n\nusing uint=unsigned;\nusing ull=unsigned long long;\n\ntemplate<class t,size_t n>\nostream& operator<<(ostream&os,const array<t,n>&a){\n\treturn os<<vc<t>(all(a));\n}\n\ntemplate<int i,class T>\nvoid print_tuple(ostream&,const T&){\n}\n\ntemplate<int i,class T,class H,class ...Args>\nvoid print_tuple(ostream&os,const T&t){\n\tif(i)os<<\",\";\n\tos<<get<i>(t);\n\tprint_tuple<i+1,T,Args...>(os,t);\n}\n\ntemplate<class ...Args>\nostream& operator<<(ostream&os,const tuple<Args...>&t){\n\tos<<\"{\";\n\tprint_tuple<0,tuple<Args...>,Args...>(os,t);\n\treturn os<<\"}\";\n}\n\ntemplate<class t>\nvoid print(t x,int suc=1){\n\tcout<<x;\n\tif(suc==1)\n\t\tcout<<\"\\n\";\n\tif(suc==2)\n\t\tcout<<\" \";\n}\n\nll read(){\n\tll i;\n\tcin>>i;\n\treturn i;\n}\n\nvi readvi(int n,int off=0){\n\tvi v(n);\n\trep(i,n)v[i]=read()+off;\n\treturn v;\n}\n\npi readpi(int off=0){\n\tint a,b;cin>>a>>b;\n\treturn pi(a+off,b+off);\n}\n\ntemplate<class t,class u>\nvoid print(const pair<t,u>&p,int suc=1){\n\tprint(p.a,2);\n\tprint(p.b,suc);\n}\n\ntemplate<class t,class u>\nvoid print_offset(const pair<t,u>&p,ll off,int suc=1){\n\tprint(p.a+off,2);\n\tprint(p.b+off,suc);\n}\n\ntemplate<class T>\nvoid print(const vector<T>&v,int suc=1){\n\trep(i,v.size())\n\t\tprint(v[i],i==int(v.size())-1?suc:2);\n}\n\ntemplate<class T>\nvoid print_offset(const vector<T>&v,ll off,int suc=1){\n\trep(i,v.size())\n\t\tprint(v[i]+off,i==int(v.size())-1?suc:2);\n}\n\ntemplate<class T,size_t N>\nvoid print(const array<T,N>&v,int suc=1){\n\trep(i,N)\n\t\tprint(v[i],i==int(N)-1?suc:2);\n}\n\nstring readString(){\n\tstring s;\n\tcin>>s;\n\treturn s;\n}\n\ntemplate<class T>\nT sq(const T& t){\n\treturn t*t;\n}\n\nvoid YES(bool ex=true){\n\tcout<<\"YES\\n\";\n\tif(ex)exit(0);\n\t#ifdef LOCAL\n\tcout.flush();\n\t#endif\n}\nvoid NO(bool ex=true){\n\tcout<<\"NO\\n\";\n\tif(ex)exit(0);\n\t#ifdef LOCAL\n\tcout.flush();\n\t#endif\n}\nvoid Yes(bool ex=true){\n\tcout<<\"Yes\\n\";\n\tif(ex)exit(0);\n\t#ifdef LOCAL\n\tcout.flush();\n\t#endif\n}\nvoid No(bool ex=true){\n\tcout<<\"No\\n\";\n\tif(ex)exit(0);\n\t#ifdef LOCAL\n\tcout.flush();\n\t#endif\n}\n//#define CAPITAL\n/*\nvoid yes(bool ex=true){\n\t#ifdef CAPITAL\n\tcout<<\"YES\"<<\"\\n\";\n\t#else\n\tcout<<\"Yes\"<<\"\\n\";\n\t#endif\n\tif(ex)exit(0);\n\t#ifdef LOCAL\n\tcout.flush();\n\t#endif\n}\nvoid no(bool ex=true){\n\t#ifdef CAPITAL\n\tcout<<\"NO\"<<\"\\n\";\n\t#else\n\tcout<<\"No\"<<\"\\n\";\n\t#endif\n\tif(ex)exit(0);\n\t#ifdef LOCAL\n\tcout.flush();\n\t#endif\n}*/\nvoid possible(bool ex=true){\n\t#ifdef CAPITAL\n\tcout<<\"POSSIBLE\"<<\"\\n\";\n\t#else\n\tcout<<\"Possible\"<<\"\\n\";\n\t#endif\n\tif(ex)exit(0);\n\t#ifdef LOCAL\n\tcout.flush();\n\t#endif\n}\nvoid impossible(bool ex=true){\n\t#ifdef CAPITAL\n\tcout<<\"IMPOSSIBLE\"<<\"\\n\";\n\t#else\n\tcout<<\"Impossible\"<<\"\\n\";\n\t#endif\n\tif(ex)exit(0);\n\t#ifdef LOCAL\n\tcout.flush();\n\t#endif\n}\n\nconstexpr ll ten(int n){\n\treturn n==0?1:ten(n-1)*10;\n}\n\nconst ll infLL=LLONG_MAX/3;\n\n#ifdef int\nconst int inf=infLL;\n#else\nconst int inf=INT_MAX/2-100;\n#endif\n\nint topbit(signed t){\n\treturn t==0?-1:31-__builtin_clz(t);\n}\nint topbit(ll t){\n\treturn t==0?-1:63-__builtin_clzll(t);\n}\nint botbit(signed a){\n\treturn a==0?32:__builtin_ctz(a);\n}\nint botbit(ll a){\n\treturn a==0?64:__builtin_ctzll(a);\n}\nint botbit(ull a){\n\treturn a==0?64:__builtin_ctzll(a);\n}\nint popcount(signed t){\n\treturn __builtin_popcount(t);\n}\nint popcount(ll t){\n\treturn __builtin_popcountll(t);\n}\nint popcount(ull t){\n\treturn __builtin_popcountll(t);\n}\nbool ispow2(int i){\n\treturn i&&(i&-i)==i;\n}\nll mask(int i){\n\treturn (ll(1)<<i)-1;\n}\n\nbool inc(int a,int b,int c){\n\treturn a<=b&&b<=c;\n}\n\ntemplate<class t> void mkuni(vc<t>&v){\n\tsort(all(v));\n\tv.erase(unique(all(v)),v.ed);\n}\n\nll rand_int(ll l, ll r) { //[l, r]\n\t#ifdef LOCAL\n\tstatic mt19937_64 gen;\n\t#else\n\tstatic mt19937_64 gen(chrono::steady_clock::now().time_since_epoch().count());\n\t#endif\n\treturn uniform_int_distribution<ll>(l, r)(gen);\n}\n\ntemplate<class t>\nvoid myshuffle(vc<t>&a){\n\trep(i,si(a))swap(a[i],a[rand_int(0,i)]);\n}\n\ntemplate<class t>\nint lwb(const vc<t>&v,const t&a){\n\treturn lower_bound(all(v),a)-v.bg;\n}\n\nvvc<int> readGraph(int n,int m){\n\tvvc<int> g(n);\n\trep(i,m){\n\t\tint a,b;\n\t\tcin>>a>>b;\n\t\t//sc.read(a,b);\n\t\ta--;b--;\n\t\tg[a].pb(b);\n\t\tg[b].pb(a);\n\t}\n\treturn g;\n}\n\nvvc<int> readTree(int n){\n\treturn readGraph(n,n-1);\n}\n\nvc<ll> presum(const vi&a){\n\tvc<ll> s(si(a)+1);\n\trep(i,si(a))s[i+1]=s[i]+a[i];\n\treturn s;\n}\n\n//copied from yosupo's library\n//ptARTLY VERIFIED\n\n//USACO 2022 January ptlatinum C\n\n#define GEOF\n\n#ifdef GEOF\nusing ld=long double;\n//using ld=double;\nconst ld PI=acos(ld(-1));\n#else\nusing ld=ll;\n#endif\nconst ld eps=1e-9;\nint sgn(ld a){return a<-eps?-1:(a>eps?1:0);}\nint sgn(ld a,ld b){return sgn(a-b);}\n/*\nusing pt=complex<ld>;\n#define x real()\n#define y imag()\n*/\nstruct pt {\n ld x,y;\n //pt(ld _x = ld(0), ld _y = ld(0)) : x(_x), y(_y) {}\n pt():x(0),y(0){}\n pt(ld xx,ld yy):x(xx),y(yy){}\n pt operator+(const pt& r) const { return {x + r.x, y + r.y}; }\n pt operator-(const pt& r) const { return {x - r.x, y - r.y}; }\n pt operator*(const pt& r) const {\n return {x * r.x - y * r.y, x * r.y + y * r.x};\n }\n\n pt operator*(const ld& r) const { return {x * r, y * r}; }\n pt operator/(const ld& r) const { return {x / r, y / r}; }\n\n pt& operator+=(const pt& r) { return *this = *this + r; }\n pt& operator-=(const pt& r) { return *this = *this - r; }\n pt& operator*=(const pt& r) { return *this = *this * r; }\n pt& operator*=(const ld& r) { return *this = *this * r; }\n pt& operator/=(const ld& r) { return *this = *this / r; }\n\n pt operator-() const { return {-x, -y}; }\n\n bool operator<(const pt& r) const {\n return 2 * sgn(x, r.x) + sgn(y, r.y) < 0;\n }\n bool operator==(const pt& r) const { return sgn((*this - r).rabs()) == 0; }\n bool operator!=(const pt& r) const { return !(*this == r); }\n\n ld norm() const { return x * x + y * y; }\n ld rabs() const { return max(std::abs(x), std::abs(y)); } // robust abs\n pair<ld, ld> to_pair() const { return {x, y}; }\n #ifdef GEOF\n ld abs() const { return sqrt(norm()); }\n ld arg() const { return atan2(y, x); }\n static pt polar(ld le, ld th) { return {le * cos(th), le * sin(th)}; }\n\t#endif\n};\nistream& operator>>(istream& is, pt& p){\n\treturn is>>p.x>>p.y;\n}\nostream& operator<<(ostream& os, const pt& p) {\n return os << \"pt(\" << p.x << \", \" << p.y << \")\";\n}\nld norm(const pt&a){\n\treturn a.norm();\n}\n#ifdef GEOF\nld abs(const pt&a){\n\treturn sqrt(norm(a));\n}\n//XXII Opencup Gpt of Ural K\npt normalized(const pt&a){\n\treturn a/abs(a);\n}\nld arg(const pt&a){return a.arg();}\n//normalize to [-PI,PI)\n//Contest 2: ptKU Contest 1, ptTZ Summer 2022 Day 4\nld normarg(ld a){\n\tld res=fmod(a+PI,2*PI);\n\tif(res<0)res+=PI;\n\telse res-=PI;\n\treturn res;\n}\n//AOJ1183\n//arg between ab\n//assume given lengths are valid\nld arg(ld a,ld b,ld c){\n\treturn acos(min(max((a*a+b*b-c*c)/(2*a*b),ld(-1)),ld(1)));\n}\n#endif\nld norm(const pt&a,const pt&b){\n\treturn (a-b).norm();\n}\nld dot(const pt&a,const pt&b){return a.x*b.x+a.y*b.y;}\nld crs(const pt& a,const pt& b){return a.x*b.y-a.y*b.x;}\nld crs(const pt& a,const pt& b,const pt& c){return crs(b-a,c-a);}\nint ccw(const pt& a,const pt& b){return sgn(crs(a,b));}\nint ccw(const pt& a,const pt& b,const pt& c){return ccw(b-a,c-a);}\n//(-pi,0](0,pi]\nint argtype(const pt&a){\n\tif(sgn(a.y)==0)return a.x<0?1:0;\n\treturn a.y<0?0:1;\n}\nint argcmp(const pt&a,const pt&b){\n\tint at=argtype(a),bt=argtype(b);\n\tif(at!=bt)return at<bt?-1:1;\n\treturn -ccw(a,b);\n};\nbool argless(const pt&a,const pt&b){return argcmp(a,b)<0;}\n//(-2)[a,-1](0)[b,1](2)\nint bet(pt a,pt b,pt c){\n\tpt d=b-a;\n\tld e=dot(d,c-a);\n\tif(sgn(e)<=0)return sgn(e)-1;\n\treturn sgn(e-norm(d))+1;\n}\n//(a,b) を結ぶ直線を考え,x 座標との交点の y 座標を求める\n//(分子,分母)を返す\npair<ld,ld> xcut(const pt&a,const pt&b,ld x){\n\treturn mp(a.y*(b.x-x)-b.y*(a.x-x),b.x-a.x);\n}\n//XXII Opencup Gpt of Ural K\npt rot90(pt a){\n\treturn pt(-a.y,a.x);\n}\n#ifdef GEOF\nld xcutval(const pt&a,const pt&b,ld x){\n\tauto [p,q]=xcut(a,b,x);\n\treturn p/q;\n}\n//AOJ1183\n//Xmas2010 E\n//-+ の 順で返す\n//a の符号により,small/large が決まる\nint qeq(ld a,ld b,ld c,ld&d,ld&e){\n\tif(sgn(a)==0){\n\t\tif(sgn(b)==0)return 0;\n\t\td=-c/b;\n\t\treturn 1;\n\t}\n\tld f=b*b-4*a*c;\n\tif(sgn(f)<0)return 0;\n\tld g=sqrt(max(f,ld(0)));\n\td=(-b-g)/(2*a);\n\te=(-b+g)/(2*a);\n\treturn sgn(f)+1;\n}\n#endif\n\nusing ln=pair<pt,pt>;\npt dir(ln a){return a.b-a.a;}\npt eval(ln a,ld b){return a.a+dir(a)*b;}\nint bet(ln a,pt b){return bet(a.a,a.b,b);}\nint crs(ln a,pt b){return crs(a.a,a.b,b);}\nint ccw(ln a,pt b){return ccw(a.a,a.b,b);}\npt proj(ln a,pt b){\n\tpt c=dir(a);\n\treturn a.a+c*dot(c,b-a.a)/norm(c);\n}\npt refl(ln a,pt b){\n\treturn proj(a,b)*2-b;\n}\n//AOJ2201\nld dlp(ln a,pt b){\n\treturn abs(crs(a,b)/abs(dir(a)));\n}\n//AOJ0153\nld dsp(ln a,pt b){\n\tpt c=proj(a,b);\n\tif(abs(bet(a.a,a.b,c))<=1)return abs(b-c);\n\treturn min(abs(b-a.a),abs(b-a.b));\n}\n//AOJ1157\n//0-no,1-yes(endpoint),2-yes(innner),3-overelap\n//if the two line touch like this\n// x----x----x\n//it returns 1\nint iss(ln a,ln b){\n\tint c1=ccw(a.a,a.b,b.a),c2=ccw(a.a,a.b,b.b);\n\tint d1=ccw(b.a,b.b,a.a),d2=ccw(b.a,b.b,a.b);\n\tif(c1||c2||d1||d2)return 1-max(c1*c2,d1*d2);\n\tint f=bet(a.a,a.b,b.a),g=bet(a.a,a.b,b.b);\n\tif(max(f,g)==-2||min(f,g)==2)return 0;\n\tif(max(f,g)==-1||min(f,g)==1)return 1;\n\treturn 3;\n}\n//segment a intersects line b?\n//endpoints inclusive\nbool isl(ln a,ln b){\n\tint d1=ccw(b.a,b.b,a.a),d2=ccw(b.a,b.b,a.b);\n\treturn d1*d2<=0;\n}\n//AOJ1033\npt cll(ln a,ln b){\n\treturn eval(a,crs(b.a,b.b,a.a)/crs(dir(a),dir(b)));\n}\n//AOJ1157\nld dss(ln a,ln b){\n\tif(iss(a,b))return 0;\n\treturn min({dsp(a,b.a),dsp(a,b.b),dsp(b,a.a),dsp(b,a.b)});\n}\n//AOJ2160\n//反時計回り方向に伸びる垂直二等分線\nln vbis(pt a,pt b){\n\tpt c=(a+b)*ld(0.5),d=b-a;\n\treturn ln(c,pt(c.x-d.y,c.y+d.x));\n}\n\n//cm,cr を pt,C に書き換えたあとコンパイルだけ通した,verify はしてない(バカ?)\n//Contest 2: PKU Contest 1, PTZ Summer 2022 Day 4\nstruct C{\n\tpt c;\n\tld r;\n\tpt eval(ld a){\n\t\treturn c+pt::polar(r,a);\n\t}\n};\nistream& operator>>(istream& is,C& c){\n\treturn is>>c.c>>c.r;\n}\nostream& operator<<(ostream& os,const C& c){\n\treturn os<<\"C{\"<<c.c<<\",\"<<c.r<<\"}\";\n}\n\n//AOJ0153\n//0-no,1-edge,2-in\nint cont(C a,pt b){\n\treturn sgn(a.r-abs(b-a.c))+1;\n}\n//AOJ0153 円じゃなくて円盤\n//0-no,1-touch,2-cross\nint ids(C a,ln b){\n\treturn sgn(a.r-dsp(b,a.c))+1;\n}\n//AOJ0129 (touch以外)\n//0-no(in),1-touch(in),2-cross,3-touch(out),4-no(out)\nint ics(C a,ln b){\n\tint c=ids(a,b);\n\tif(c<=1)return 4-c;\n\treturn sgn(max(abs(b.a-a.c),abs(b.b-a.c))-a.r)+1;\n}\n//AOJ1183\n//eval(b,t) が a と重なる t を c,d に入れる\n//解の個数を返す\nint ccl(C a,ln b,ld&c,ld&d){\n\tpt e=dir(b);\n\tpt f=b.a-a.c;\n\treturn qeq(norm(e),2*dot(e,f),norm(f)-a.r*a.r,c,d);\n}\n//AOJ0023\n//Contest 2: PKU Contest 1, PTZ Summer 2022 Day 4 (2,7)\n//0-apart,1-coincide,2-a<b,3-a<=b,4-a>b,5-a>=b,6-a touch b,7-a cross b\nint icc(C a,C b){\n\tld c=abs(a.c-b.c);\n\tif(sgn(c)==0&&sgn(a.r-b.r)==0)return 1;\n\tint d=sgn(c+a.r-b.r);\n\tif(d<=0)return d+3;\n\tint e=sgn(c+b.r-a.r);\n\tif(e<=0)return e+5;\n\tint f=sgn(a.r+b.r-c);\n\tif(f>=0)return f+6;\n\treturn 0;\n}\n//AOJ2572\n//Contest 2: PKU Contest 1, PTZ Summer 2022 Day 4\n//args of two intersections r,l seen by a.c\n//assume two circles cross\npair<ld,ld> ccc(C a,C b){\n\tld c=arg(b.c-a.c);\n\tld d=arg(a.r,abs(b.c-a.c),b.r);\n\treturn make_pair(c-d,c+d);\n}\n//XXI Opencup GP of Siberia 5\nvc<pt> ccc_list(C a,C b){\n\tint v=icc(a,b);\n\tif(v==0||v==1||v==2||v==4)return {};\n\tauto z=ccc(a,b);\n\tif(v==3||v==5||v==6){\n\t\treturn {a.c+pt::polar(a.r,z.a)};\n\t}else{\n\t\treturn {a.c+pt::polar(a.r,z.a),a.c+pt::polar(a.r,z.b)};\n\t}\n}\n//Contest 2: PKU Contest 1, PTZ Summer 2022 Day 4\n//単位円で偏角 0-x で y*(-dx) を積分した値\nld cutareaarg(ld x){\n\treturn (x-sin(2*x)/2)/2;\n}\n//Contest 2: PKU Contest 1, PTZ Summer 2022 Day 4\nld cutareaarg(ld l,ld r){\n\treturn cutareaarg(r)-cutareaarg(l);\n}\n//Contest 4, PTZ 2022 Winter Day 6 (ICPC Camp Day 1) J\nld cutareaarg(C c,ld l,ld r){\n\tl=normarg(l);\n\tr=normarg(r);\n\treturn (l<=r?cutareaarg(l,r):cutareaarg(l,PI)+cutareaarg(-PI,r))\n\t\t\t*sq(c.r)-c.c.y*c.r*(cos(r)-cos(l));\n}\n//円 a の偏角 b の位置で接する直線\nln tanln(C a,ld b){\n\tpt c=a.eval(b);\n\treturn ln(c,c+pt::polar(1,b+PI/2));\n}\n//AOJ2201\n//Contest 4, PTZ 2022 Winter Day 6 (ICPC Camp Day 1)\n//a と b の共通外接線を引く\n//a の接点の偏角を返す\npair<ld,ld> tangent_points_ext(C a,C b){\n\tpt dif=b.c-a.c;\n\tld w=acos((a.r-b.r)/abs(dif));\n\tld k=arg(dif);\n\treturn mp(normarg(k-w),normarg(k+w));\n}\npair<ln,ln> tangent_line_ext(C a,C b){\n\tauto [p,q]=tangent_points_ext(a,b);\n\treturn mp(tanln(a,p),tanln(a,q));\n}\n//a>=b で内接しているケース\nln tangent_line_ext2(C a,C b){\n\treturn tanln(a,arg(b.c-a.c));\n}\n//AOJ2201\n//内接線\npair<ld,ld> tangent_points_in(C a,C b){\n\tpt dif=b.c-a.c;\n\tld w=acos((a.r+b.r)/abs(dif));\n\tld k=arg(dif);\n\treturn mp(normarg(k-w),normarg(k+w));\n}\npair<ln,ln> tangent_line_in(C a,C b){\n\tauto [p,q]=tangent_points_in(a,b);\n\treturn mp(tanln(a,p),tanln(a,q));\n}\n//外接しているケース\nln tangent_line_in2(C a,C b){\n\treturn tanln(a,arg(b.c-a.c));\n}\n//AOJ2201(ext2,in2 あたりの verify はできてない)\nvc<ln> tangent_lines(C a,C b){\n\tvc<ln> res;\n\tint v=icc(a,b);\n\t//out\n\tif(v==0||v==6||v==7){\n\t\tauto [p,q]=tangent_line_ext(a,b);\n\t\tres.pb(p);\n\t\tres.pb(q);\n\t}\n\tif(v==5)res.pb(tangent_line_ext2(a,b));\n\tif(v==3)res.pb(tangent_line_ext2(b,a));\n\t//in\n\tif(v==0){\n\t\tauto [p,q]=tangent_line_in(a,b);\n\t\tres.pb(p);\n\t\tres.pb(q);\n\t}\n\tif(v==6)res.pb(tangent_line_in2(a,b));\n\treturn res;\n}\n\n//極大な円だけ残す,重複も消す\n//AOJ1047\n//Contest 4, PTZ 2022 Winter Day 6 (ICPC Camp Day 1) J\n//Contest 5, PTZ 2021 Summer Day 2, NAC 2021 B\nvc<C> simplify_circle_sets(const vc<C>&rw){\n\tvc<C> cs;\n\tfor(auto cur:rw){\n\t\tbool ok=true;\n\t\trep(j,si(cs)){\n\t\t\tint w=icc(cs[j],cur);\n\t\t\tif(w==1||w==4||w==5){\n\t\t\t\tok=false;\n\t\t\t}\n\t\t}\n\t\tif(ok){\n\t\t\trep(j,si(cs)){\n\t\t\t\tint w=icc(cs[j],cur);\n\t\t\t\tif(w==1||w==2||w==3){\n\t\t\t\t\tcs.erase(cs.bg+j--);\n\t\t\t\t}\n\t\t\t}\n\t\t\tcs.pb(cur);\n\t\t}\n\t}\n\treturn cs;\n}\n\n//ByteCamp 2022 Day3 A\n//原点中心,半径 r の円の x 座標 a 以下の領域の面積\nld cutarea_sub(ld r,ld a){\n\tld theta=acos(max(min(a/r,ld(1)),ld(-1)));\n\treturn sq(r)*(PI-theta)+a*r*sin(theta);\n}\n//2020 Multi-Uni Day5 J\n//x座標 b 以上 c 以下の部分だけ取ってきて,その面積\nld cutarea(C a,ld b,ld c){\n\tchmax(b,a.c.x-a.r);\n\tchmin(c,a.c.x+a.r);\n\tb-=a.c.x;\n\tc-=a.c.x;\n\tif(sgn(c-b)<=0)return 0;\n\treturn cutarea_sub(a.r,c)-cutarea_sub(a.r,b);\n}\n\n//AOJ2423\nC circumc(pt a,pt b,pt c){\n\tb-=a;\n\tc-=a;\n\tpt r=c*norm(b)-b*norm(c);\n\tr=pt(r.y,-r.x)/(2*crs(b,c));\n\treturn {a+r,abs(r)};\n}\n//AOJ2423\nC mindisc(const vc<pt>& p,array<pt,3> q,int i,int j){\n\tif(i==int(p.size())){\n\t\tif(j==0)\n\t\t\treturn {{0,0},-1};\n\t\telse if(j==1)\n\t\t\treturn {q[0],0};\n\t\telse if(j==2)\n\t\t\treturn {(q[0]+q[1])*ld(0.5),abs(q[0]-q[1])/2};\n\t\telse if(j==3)\n\t\t\treturn circumc(q[0],q[1],q[2]);\n\t\telse\n\t\t\tassert(false);\n\t}\n\tC c=mindisc(p,q,i+1,j);\n\tif(sgn(abs(c.c-p[i])-c.r)==1){\n\t\tassert(j<3);\n\t\tq[j]=p[i];\n\t\treturn mindisc(p,q,i+1,j+1);\n\t}else\n\t\treturn c;\n}\nC mindisc(vc<pt> p){\n\tshuffle(all(p),mt19937());\n\treturn mindisc(p,array<pt,3>(),0,0);\n}\n\n//CF773F\n//p の中で k 点以上含むような円の最小半径\n\nvoid slv(){\n\tint n;cin>>n;if(n==0)exit(0);\n\tvc<pt> c(n);\n\tvc<ld> a(n),b(n);\n\trep(i,n){\n\t\tcin>>c[i]>>a[i]>>b[i];\n\t\tb[i]+=a[i];\n\t}\n\tint ans=0;\n\tauto work=[&](ln z){\n\t\tint cnt=0;\n\t\trep(i,n){\n\t\t\tld r=dlp(z,c[i]);\n\t\t\tif(sgn(a[i],r)<=0&&sgn(r,b[i])<=0)cnt++;\n\t\t}\n\t\tchmax(ans,cnt);\n\t};\n\trep(i,n){\n\t\tpt v=c[i];\n\t\tv.y-=a[i];\n\t\twork(ln(v,pt(v.x+1,v.y)));\n\t}\n\trep(i,n){\n\t\tpt v=c[i];\n\t\tv.y-=b[i];\n\t\twork(ln(v,pt(v.x+1,v.y)));\n\t}\n\trep(i,n)for(auto x:{a[i],b[i]})rng(j,i+1,n)for(auto y:{a[j],b[j]}){\n\t\tvc<ln> ls=tangent_lines(C{c[i],x},C{c[j],y});\n\t\tfor_each(all(ls),work);\n\t}\n\tcout<<ans<<endl;\n}\n\nsigned main(){\n\tcin.tie(0);\n\tios::sync_with_stdio(0);\n\tcout<<fixed<<setprecision(20);\n\t\n\twhile(1)slv();\n}", "accuracy": 1, "time_ms": 110, "memory_kb": 3664, "score_of_the_acc": -0.1357, "final_rank": 10 }, { "submission_id": "aoj_2201_6897836", "code_snippet": "// お借り中\n// https://drken1215.hatenablog.com/entry/2018/09/17/195000\n// 参考\n// https://mayokoex.hatenablog.com/entry/2016/05/21/200343\n// http://www.geisya.or.jp/~mwm48961/koukou/kyori04.htm\n// https://hiraocafe.com/note/ennosessen.html\n// \n// 2 円の共通接線\n//\n// verified:\n// AOJ 2201 Immortal Jewels\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2201\n//\n\n\n#include <iostream>\n#include <vector>\n#include <cmath>\n#include <iomanip>\n#include <algorithm>\nusing namespace std;\n\n\n////////////////////////////\n// 基本要素 (点, 線分, 円)\n////////////////////////////\n\nusing DD = double;\nconst DD INF = 1LL << 60; // to be set appropriately\nconst DD EPS = 1e-10; // to be set appropriately\nconst DD PI = acos(-1.0);\nDD torad(int deg) { return (DD)(deg)*PI / 180; }\nDD todeg(DD ang) { return ang * 180 / PI; }\n\n/* Point */\nstruct Point {\n DD x, y;\n Point(DD x = 0.0, DD y = 0.0) : x(x), y(y) {}\n friend ostream& operator << (ostream& s, const Point& p) { return s << '(' << p.x << \", \" << p.y << ')'; }\n};\ninline Point operator + (const Point& p, const Point& q) { return Point(p.x + q.x, p.y + q.y); }\ninline Point operator - (const Point& p, const Point& q) { return Point(p.x - q.x, p.y - q.y); }\ninline Point operator * (const Point& p, DD a) { return Point(p.x * a, p.y * a); }\ninline Point operator * (DD a, const Point& p) { return Point(a * p.x, a * p.y); }\ninline Point operator * (const Point& p, const Point& q) { return Point(p.x * q.x - p.y * q.y, p.x * q.y + p.y * q.x); }\ninline Point operator / (const Point& p, DD a) { return Point(p.x / a, p.y / a); }\ninline Point conj(const Point& p) { return Point(p.x, -p.y); }\ninline Point rot(const Point& p, DD ang) { return Point(cos(ang) * p.x - sin(ang) * p.y, sin(ang) * p.x + cos(ang) * p.y); }\ninline Point rot90(const Point& p) { return Point(-p.y, p.x); }\ninline DD cross(const Point& p, const Point& q) { return p.x * q.y - p.y * q.x; }\ninline DD dot(const Point& p, const Point& q) { return p.x * q.x + p.y * q.y; }\ninline DD norm(const Point& p) { return dot(p, p); }\ninline DD abs(const Point& p) { return sqrt(dot(p, p)); }\ninline DD amp(const Point& p) { DD res = atan2(p.y, p.x); if (res < 0) res += PI * 2; return res; }\ninline bool eq(const Point& p, const Point& q) { return abs(p - q) < EPS; }\ninline bool operator < (const Point& p, const Point& q) { return (abs(p.x - q.x) > EPS ? p.x < q.x : p.y < q.y); }\ninline bool operator > (const Point& p, const Point& q) { return (abs(p.x - q.x) > EPS ? p.x > q.x : p.y > q.y); }\ninline Point operator / (const Point& p, const Point& q) { return p * conj(q) / norm(q); }\n\n/* Line */\nstruct Line : vector<Point> {\n Line(Point a = Point(0.0, 0.0), Point b = Point(0.0, 0.0)) {\n this->push_back(a);\n this->push_back(b);\n }\n friend ostream& operator << (ostream& s, const Line& l) { return s << '{' << l[0] << \", \" << l[1] << '}'; }\n};\n\n/* Circle */\nstruct Circle : Point {\n DD r;\n Circle(Point p = Point(0.0, 0.0), DD r = 0.0) : Point(p), r(r) {}\n friend ostream& operator << (ostream& s, const Circle& c) { return s << '(' << c.x << \", \" << c.y << \", \" << c.r << ')'; }\n};\n\n\n\n///////////////////////\n// 接線\n///////////////////////\n\n// 点と円\nvector<Point> tanline(const Point& p, const Circle& c) {\n vector<Point> res;\n DD d = norm(p - c);\n DD l = d - c.r * c.r;\n if (l < -EPS) return res;\n if (l <= 0.0) l = 0.0;\n Point cq = (p - c) * (c.r * c.r / d);\n Point qs = rot90((p - c) * (c.r * sqrt(l) / d));\n Point s1 = c + cq + qs, s2 = c + cq - qs;\n res.push_back(s1);\n res.push_back(s2);\n return res;\n}\n\n// 円と円の共通接線\nvector<Line> comtanline(Circle a, Circle b) {\n vector<Line> res;\n if (abs(a - b) > abs(a.r - b.r) + EPS) {\n if (abs(a.r - b.r) < EPS) {\n Point dir = b - a;\n dir = rot90(dir * (a.r / abs(dir)));\n res.push_back(Line(a + dir, b + dir));\n res.push_back(Line(a - dir, b - dir));\n }\n else {\n Point p = a * -b.r + b * a.r;\n p = p * (1.0 / (a.r - b.r));\n vector<Point> bs = tanline(p, a);\n vector<Point> as = tanline(p, b);\n for (int i = 0; i < min(as.size(), bs.size()); ++i) {\n res.push_back(Line(bs[i], as[i]));\n }\n }\n }\n if (abs(a - b) > a.r + b.r + EPS) {\n Point p = a * b.r + b * a.r;\n p = p * (1.0 / (a.r + b.r));\n vector<Point> bs = tanline(p, a);\n vector<Point> as = tanline(p, b);\n for (int i = 0; i < min(as.size(), bs.size()); ++i) {\n res.push_back(Line(bs[i], as[i]));\n }\n }\n return res;\n}\n\n\n\n\n///////////////////////\n// ソルバー\n///////////////////////\n\n// 距離\nPoint proj(const Point& p, const Line& l) {\n DD t = dot(p - l[0], l[1] - l[0]) / norm(l[1] - l[0]);\n return l[0] + (l[1] - l[0]) * t;\n}\nDD distancePL(const Point& p, const Line& l) {\n return abs(p - proj(p, l));\n}\n\n// カウント\nint count(Line l, vector<Circle> vec, vector<DD> d) {\n int res = 0;\n //cout << endl;\n for (int i = 0; i < vec.size(); ++i) {\n DD dis = distancePL(vec[i], l);\n if (dis >= vec[i].r - EPS && dis <= vec[i].r + d[i] + EPS) ++res;\n }\n return res;\n}\n\nint main() {\n int N;\n while (cin >> N) {\n if (N == 0) break;\n\n vector<Circle> vec(N);\n vector<DD> d(N);\n for (int i = 0; i < N; ++i) {\n cin >> vec[i].x >> vec[i].y >> vec[i].r >> d[i];\n }\n if (N == 1) cout << 1 << endl;\n else {\n int res = 0;\n for (int i = 0; i < N; ++i) {\n for (int j = i + 1; j < N; ++j) {\n vector<Circle> I(2, vec[i]); I[1].r += d[i];\n vector<Circle> J(2, vec[j]); J[1].r += d[j];\n for (int p = 0; p < 2; ++p) {\n for (int q = 0; q < 2; ++q) {\n vector<Line> L = comtanline(I[p], J[q]);\n for (int k = 0; k < L.size(); ++k) {\n int tmp = count(L[k], vec, d);\n res = max(res, tmp);\n }\n }\n }\n }\n }\n cout << res << endl;\n }\n }\n}", "accuracy": 1, "time_ms": 120, "memory_kb": 3544, "score_of_the_acc": -0.1072, "final_rank": 7 }, { "submission_id": "aoj_2201_6863698", "code_snippet": "//\n// 2 円の共通接線\n//\n// verified:\n// AOJ 2201 Immortal Jewels\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2201\n//\n// 以下参考\n// https://shogo82148.github.io/homepage/memo/geometry/line-circle.html\n// https://mayokoex.hatenablog.com/entry/2016/05/21/200343\n// http://shogo82148.github.io/homepage/memo/geometry/circle-circle.html\n\n\n#include <iostream>\n#include <vector>\n#include <cmath>\n#include <iomanip>\n#include <algorithm>\nusing namespace std;\n\n\n////////////////////////////\n// 基本要素 (点, 線分, 円)\n////////////////////////////\n\nusing DD = double;\nconst DD INF = 1LL << 60; // to be set appropriately\nconst DD EPS = 1e-10; // to be set appropriately\nconst DD PI = acos(-1.0);\nDD torad(int deg) { return (DD)(deg)*PI / 180; }\nDD todeg(DD ang) { return ang * 180 / PI; }\n\n/* Point */\nstruct Point {\n DD x, y;\n Point(DD x = 0.0, DD y = 0.0) : x(x), y(y) {}\n friend ostream& operator << (ostream& s, const Point& p) { return s << '(' << p.x << \", \" << p.y << ')'; }\n};\ninline Point operator + (const Point& p, const Point& q) { return Point(p.x + q.x, p.y + q.y); }\ninline Point operator - (const Point& p, const Point& q) { return Point(p.x - q.x, p.y - q.y); }\ninline Point operator * (const Point& p, DD a) { return Point(p.x * a, p.y * a); }\ninline Point operator * (DD a, const Point& p) { return Point(a * p.x, a * p.y); }\ninline Point operator * (const Point& p, const Point& q) { return Point(p.x * q.x - p.y * q.y, p.x * q.y + p.y * q.x); }\ninline Point operator / (const Point& p, DD a) { return Point(p.x / a, p.y / a); }\ninline Point conj(const Point& p) { return Point(p.x, -p.y); }\ninline Point rot(const Point& p, DD ang) { return Point(cos(ang) * p.x - sin(ang) * p.y, sin(ang) * p.x + cos(ang) * p.y); }\ninline Point rot90(const Point& p) { return Point(-p.y, p.x); }\ninline DD cross(const Point& p, const Point& q) { return p.x * q.y - p.y * q.x; }\ninline DD dot(const Point& p, const Point& q) { return p.x * q.x + p.y * q.y; }\ninline DD norm(const Point& p) { return dot(p, p); }\ninline DD abs(const Point& p) { return sqrt(dot(p, p)); }\ninline DD amp(const Point& p) { DD res = atan2(p.y, p.x); if (res < 0) res += PI * 2; return res; }\ninline bool eq(const Point& p, const Point& q) { return abs(p - q) < EPS; }\ninline bool operator < (const Point& p, const Point& q) { return (abs(p.x - q.x) > EPS ? p.x < q.x : p.y < q.y); }\ninline bool operator > (const Point& p, const Point& q) { return (abs(p.x - q.x) > EPS ? p.x > q.x : p.y > q.y); }\ninline Point operator / (const Point& p, const Point& q) { return p * conj(q) / norm(q); }\n\n/* Line */\nstruct Line : vector<Point> {\n Line(Point a = Point(0.0, 0.0), Point b = Point(0.0, 0.0)) {\n this->push_back(a);\n this->push_back(b);\n }\n friend ostream& operator << (ostream& s, const Line& l) { return s << '{' << l[0] << \", \" << l[1] << '}'; }\n};\n\n/* Circle */\nstruct Circle : Point {\n DD r;\n Circle(Point p = Point(0.0, 0.0), DD r = 0.0) : Point(p), r(r) {}\n friend ostream& operator << (ostream& s, const Circle& c) { return s << '(' << c.x << \", \" << c.y << \", \" << c.r << ')'; }\n};\n\n\n\n///////////////////////\n// 接線\n///////////////////////\n\n// 点と円\nvector<Point> tanline(const Point& p, const Circle& c) {\n vector<Point> res;\n DD d = norm(p - c);\n DD l = d - c.r * c.r;\n if (l < -EPS) return res;\n if (l <= 0.0) l = 0.0;\n Point cq = (p - c) * (c.r * c.r / d);\n Point qs = rot90((p - c) * (c.r * sqrt(l) / d));\n Point s1 = c + cq + qs, s2 = c + cq - qs;\n res.push_back(s1);\n res.push_back(s2);\n return res;\n}\n\n// 円と円の共通接線\nvector<Line> comtanline(Circle a, Circle b) {\n vector<Line> res;\n if (abs(a - b) > abs(a.r - b.r) + EPS) {\n if (abs(a.r - b.r) < EPS) {\n Point dir = b - a;\n dir = rot90(dir * (a.r / abs(dir)));\n res.push_back(Line(a + dir, b + dir));\n res.push_back(Line(a - dir, b - dir));\n }\n else {\n Point p = a * -b.r + b * a.r;\n p = p * (1.0 / (a.r - b.r));\n vector<Point> bs = tanline(p, a);\n vector<Point> as = tanline(p, b);\n for (int i = 0; i < min(as.size(), bs.size()); ++i) {\n res.push_back(Line(bs[i], as[i]));\n }\n }\n }\n if (abs(a - b) > a.r + b.r + EPS) {\n Point p = a * b.r + b * a.r;\n p = p * (1.0 / (a.r + b.r));\n vector<Point> bs = tanline(p, a);\n vector<Point> as = tanline(p, b);\n for (int i = 0; i < min(as.size(), bs.size()); ++i) {\n res.push_back(Line(bs[i], as[i]));\n }\n }\n return res;\n}\n\n\n\n\n///////////////////////\n// ソルバー\n///////////////////////\n\n// 距離\nPoint proj(const Point& p, const Line& l) {\n DD t = dot(p - l[0], l[1] - l[0]) / norm(l[1] - l[0]);\n return l[0] + (l[1] - l[0]) * t;\n}\nDD distancePL(const Point& p, const Line& l) {\n return abs(p - proj(p, l));\n}\n\n// カウント\nint count(Line l, vector<Circle> vec, vector<DD> d) {\n int res = 0;\n //cout << endl;\n for (int i = 0; i < vec.size(); ++i) {\n DD dis = distancePL(vec[i], l);\n if (dis >= vec[i].r - EPS && dis <= vec[i].r + d[i] + EPS) ++res;\n }\n return res;\n}\n\nint main() {\n int N;\n while (cin >> N) {\n if (N == 0) break;\n\n vector<Circle> vec(N);\n vector<DD> d(N);\n for (int i = 0; i < N; ++i) {\n cin >> vec[i].x >> vec[i].y >> vec[i].r >> d[i];\n }\n if (N == 1) cout << 1 << endl;\n else {\n int res = 0;\n for (int i = 0; i < N; ++i) {\n for (int j = i + 1; j < N; ++j) {\n vector<Circle> I(2, vec[i]); I[1].r += d[i];\n vector<Circle> J(2, vec[j]); J[1].r += d[j];\n for (int p = 0; p < 2; ++p) {\n for (int q = 0; q < 2; ++q) {\n vector<Line> L = comtanline(I[p], J[q]);\n for (int k = 0; k < L.size(); ++k) {\n int tmp = count(L[k], vec, d);\n res = max(res, tmp);\n }\n }\n }\n }\n }\n cout << res << endl;\n }\n }\n}", "accuracy": 1, "time_ms": 120, "memory_kb": 3448, "score_of_the_acc": -0.0804, "final_rank": 4 }, { "submission_id": "aoj_2201_6722081", "code_snippet": "#include <algorithm>\n#include <cassert>\n#include <cstddef>\n#include <cstdint>\n#include <iomanip>\n#include <iostream>\n#include <utility>\n#include <vector>\n\nnamespace luz {\n\n using isize = std::ptrdiff_t;\n using usize = std::size_t;\n\n using i32 = std::int32_t;\n using i64 = std::int64_t;\n using u32 = std::uint32_t;\n using u64 = std::uint64_t;\n \n} // namespace luz\n\nnamespace luz {\n\n struct rep {\n struct itr {\n usize i;\n constexpr itr(const usize i) noexcept : i(i) {}\n void operator++() noexcept { ++i; }\n constexpr usize operator*() const noexcept { return i; }\n constexpr bool operator!=(const itr x) const noexcept { return i != x.i; }\n };\n const itr f, l;\n constexpr rep(const usize f, const usize l) noexcept\n : f(std::min(f, l)), l(l) {}\n constexpr auto begin() const noexcept { return f; }\n constexpr auto end() const noexcept { return l; }\n };\n\n struct rrep {\n struct itr {\n usize i;\n constexpr itr(const usize i) noexcept : i(i) {}\n void operator++() noexcept { --i; }\n constexpr usize operator*() const noexcept { return i; }\n constexpr bool operator!=(const itr x) const noexcept { return i != x.i; }\n };\n const itr f, l;\n constexpr rrep(const usize f, const usize l) noexcept\n : f(l - 1), l(std::min(f, l) - 1) {}\n constexpr auto begin() const noexcept { return f; }\n constexpr auto end() const noexcept { return l; }\n };\n\n} // namespace luz\n\nnamespace luz {\n\n void set_fast_ios() {\n std::ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n }\n\n} // namespace luz\n\nnamespace luz {\n\n void io_set(usize precision) {\n std::cout << std::fixed << std::setprecision(precision);\n std::cerr << std::fixed << std::setprecision(precision);\n }\n\n} // namespace luz\n\nnamespace luz {\n\n template< typename T = i64 > T input() {\n T tmp;\n std::cin >> tmp;\n return tmp;\n }\n\n} // namespace luz\n\nnamespace luz {\n\n template< typename T >\n std::ostream &operator<<(std::ostream &os, std::vector< T > &vs) {\n for (usize i: rep(0, vs.size())) {\n os << vs[i] << (i + 1 != vs.size() ? \" \" : \"\");\n }\n return os;\n }\n\n template< typename T >\n std::istream &operator>>(std::istream &is, std::vector< T > &vs) {\n for (T &v: vs) {\n is >> v;\n }\n return is;\n }\n\n} // namespace luz\n\nnamespace luz {\n\n template< typename T1, typename T2 >\n std::ostream &operator<<(std::ostream &os, std::pair < T1, T2 > &p) {\n os << \"(\" << p.first << \", \" << p.second << \")\";\n return os;\n }\n\n template< typename T1, typename T2 >\n std::istream &operator>>(std::istream &is, std::pair< T1, T2 > &p) {\n is >> p.first >> p.second;\n return is;\n }\n\n} // namespace luz\n\nnamespace luz {\n\n template <typename T>\n std::vector<T> make_vector(usize a, T b) {\n return std::vector<T>(a, b);\n }\n\n template <typename... Ts>\n auto make_vector(usize a, Ts... ts) {\n return std::vector<decltype(make_vector(ts...))>(a, make_vector(ts...));\n }\n\n} // namespace luz\n\nnamespace luz {\n\n template <typename T1, typename T2>\n inline bool chmax(T1 &a, T2 b) {\n return a < b and (a = b, true);\n }\n\n template <typename T1, typename T2>\n inline bool chmin(T1 &a, T2 b) {\n return a > b and (a = b, true);\n }\n\n} // namespace luz\n\nusing namespace std;\n\n#include <complex>\n#include <cmath>\n#include <istream>\n#include <ostream>\n\n// base\nnamespace geometry {\n using namespace std;\n using real_number = long double;\n\n const real_number PI = acosl(-1);\n\n inline static real_number &eps() {\n static real_number EPS = 1e-10;\n return EPS;\n }\n\n static void set_eps(real_number EPS) {\n eps() = EPS;\n }\n\n inline int sign(real_number r) {\n set_eps(1e-10);\n if (r < -eps()) return -1;\n if (r > +eps()) return +1;\n return 0;\n }\n\n inline bool equals(real_number r1, real_number r2) {\n return sign(r1 - r2) == 0;\n }\n}\n\n// point\nnamespace geometry {\n using point = complex< real_number >;\n using points = vector< point >;\n\n istream &operator>>(istream &is, point &p) {\n real_number x, y;\n is >> x >> y;\n p = point(x, y);\n return is;\n }\n\n ostream &operator<<(ostream &os, const point &p) {\n return os << p.real() << \" \" << p.imag();\n }\n\n point operator*(const point &p, const real_number &k) {\n return point(p.real() * k, p.imag() * k);\n }\n\n point rotate(const real_number &theta, const point &p) {\n return point(cos(theta) * p.real() + sin(-theta) * p.imag(),\n sin(theta) * p.real() + cos(-theta) * p.imag());\n }\n\n bool equals(const point &a, const point &b) {\n return equals(a.real(), b.real()) and equals(a.imag(), b.imag());\n }\n}\n\nusing geometry::operator>>;\nusing geometry::operator<<;\n\n// circle\nnamespace geometry {\n struct circle {\n point p;\n real_number r;\n circle() {}\n circle(point p, real_number r) : p(p), r(r) {}\n };\n\n using circles = vector< circle >;\n}\n\n// line \nnamespace geometry {\n struct line {\n point a, b;\n\n line() = default;\n line(point a, point b) : a(a), b(b) {}\n };\n\n using lines = vector< line >;\n}\n\nnamespace geometry {\n lines tangent_cc(circle c1, circle c2) {\n lines ls;\n if (c1.r > c2.r) swap(c1, c2);\n\n real_number g = norm(c1.p - c2.p);\n if (sign(g) == 0) return ls;\n\n point u = (c1.p - c2.p) / sqrt(g);\n point v(-u.imag(), u.real());\n\n for (int s = 1; s >= -1; s -= 2) {\n real_number h = (c1.r * s + c2.r) / sqrt(g);\n if (sign(1 - norm(h)) == 0) {\n ls.emplace_back(c2.p + u * c2.r, c2.p + (u + v) * c2.r);\n } else if (sign(1 - norm(h)) > 0) {\n point uu = u * h;\n point vv = v * sqrt(1 - norm(h));\n ls.emplace_back(c2.p + (uu + vv) * c2.r, c1.p - (uu + vv) * c1.r * s);\n ls.emplace_back(c2.p + (uu - vv) * c2.r, c1.p - (uu - vv) * c1.r * s);\n }\n }\n\n return ls;\n }\n}\n\n// product\nnamespace geometry {\n real_number cross(const point &a, const point &b) {\n return a.real() * b.imag() - a.imag() * b.real();\n }\n\n real_number dot(const point &a, const point &b) {\n return a.real() * b.real() + a.imag() * b.imag();\n }\n}\n\n// projection\nnamespace geometry {\n point projection(const line &l, const point &p) {\n real_number t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n return l.a + (l.a - l.b) * t;\n }\n}\n\nnamespace geometry {\n real_number distance_lp(const line &l, const point &p) {\n point pr = projection(l, p);\n return abs(pr - p);\n }\n}\n\nusing namespace geometry;\n\nnamespace luz {\n\n void solve(int n) {\n circles originals(n);\n std::vector< real_number > ms(n);\n circles cs;\n\n for (usize i: rep(0, n)) {\n real_number x, y;\n real_number r, m;\n std::cin >> x >> y >> r >> m;\n\n cs.emplace_back(point(x, y), r + m);\n cs.emplace_back(point(x, y), r);\n originals[i] = circle(point(x, y), r);\n ms[i] = m;\n }\n\n int ans = 1;\n for (usize i: rep(0, cs.size())) {\n for (usize j: rep(i + 1, cs.size())) {\n lines ls = tangent_cc(cs[i], cs[j]);\n\n for (const line &l: ls) {\n\n int cnt = 0;\n for (usize ci: rep(0, n)) {\n const circle &c = originals[ci];\n real_number d = distance_lp(l, c.p);\n\n if (sign(d - c.r) >= 0 and sign(c.r + ms[ci] - d) >= 0) {\n cnt++;\n }\n }\n\n chmax(ans, cnt);\n \n }\n }\n }\n\n cout << ans << endl;\n }\n\n void main_() {\n int n;\n\n while (std::cin >> n, n) {\n solve(n);\n }\n }\n\n} // namespace luz\n\nint main() {\n luz::set_fast_ios();\n luz::io_set(15);\n\n luz::main_();\n}", "accuracy": 1, "time_ms": 870, "memory_kb": 3516, "score_of_the_acc": -0.4801, "final_rank": 16 }, { "submission_id": "aoj_2201_6721908", "code_snippet": "#include<bits/stdc++.h>\n#define rep(i,a,...) for(int i = (a)*(strlen(#__VA_ARGS__)!=0);i<(int)(strlen(#__VA_ARGS__)?__VA_ARGS__:(a));++i)\n#define per(i,a,...) for(int i = (strlen(#__VA_ARGS__)?__VA_ARGS__:(a))-1;i>=(int)(strlen(#__VA_ARGS__)?(a):0);--i)\n#define foreach(i, n) for(auto &i:(n))\n#define all(x) (x).begin(), (x).end()\n#define bit(x) (1ll << (x))\n#define lambda(RES_TYPE, ...) (function<RES_TYPE(__VA_ARGS__)>)[&](__VA_ARGS__) -> RES_TYPE\n#define method(FUNC_NAME, RES_TYPE, ...) function<RES_TYPE(__VA_ARGS__)> FUNC_NAME = lambda(RES_TYPE, __VA_ARGS__)\nusing namespace std;\nusing ll = long long;\nusing pii = pair<int,int>;\nusing pll = pair<ll,ll>;\n//const ll MOD = (ll)1e9+7;\nconst ll MOD = 998244353;\nconst int INF = (ll)1e9+7;\nconst ll INFLL = (ll)1e18;\ntemplate<class t>\nusing vvector = vector<vector<t>>;\ntemplate<class t>\nusing vvvector = vector<vector<vector<t>>>;\ntemplate<class t>\nusing priority_queuer = priority_queue<t, vector<t>, greater<t>>;\ntemplate<class t, class u> bool chmax(t &a, u b){if(a<b){a=b;return true;}return false;}\ntemplate<class t, class u> bool chmin(t &a, u b){if(a>b){a=b;return true;}return false;}\n#ifdef DEBUG\n#define debug(x) cout<<\"LINE \"<<__LINE__<<\": \"<<#x<<\" = \"<<x<<endl;\n#else\n#define debug(x) (void)0\n#endif\n\nnamespace templates{\n ll modpow(ll x, ll b,ll mod=MOD){\n ll res = 1;\n while(b){\n if(b&1)res = res * x % mod;\n x = x * x % mod;\n b>>=1;\n }\n return res;\n }\n\n ll modinv(ll x){\n return modpow(x, MOD-2);\n }\n\n bool was_output = false;\n\n void print();\n template <class t> void print(const vector<t> &);\n template <class t, class u> void print(const pair<t, u> &);\n template <class t> void print(const t&);\n template <class Head, class... Tail> void print(const Head&, const Tail&...);\n\n template <class t> void println(const vector<vector<t>>&);\n template <class t> void println(const vector<t>&);\n template <class t> void println(const t&);\n template <class Head, class... Tail> void println(const Head&, const Tail&...);\n void println();\n void newline();\n\n void print(){\n return;\n }\n\n template <class t>\n void print(const vector<t>&x){\n for(const t&i:x)print(i);\n }\n template <class t, class u>\n void print(const pair<t,u>&p){\n print(p.first);\n print(p.second);\n }\n template <class t>\n void print(const t&x){\n if(was_output)cout<<\" \";\n cout<<x;\n was_output = true;\n }\n template <class Head, class... Tail>\n void print(const Head&head,const Tail&...tail){\n print(head);\n print(tail...);\n }\n\n template<class t>\n void println(const vector<vector<t>>&x){\n for(vector<t> i:x)println(i);\n }\n template<class t>\n void println(const vector<t>&x){\n for(const t&i:x)print(i);\n println();\n }\n template <class t>\n void println(const t&x){\n print(x);\n println();\n }\n void println(){\n if(was_output){\n cout << endl;\n was_output = false;\n }\n }\n template <class Head, class... Tail>\n void println(const Head&head,const Tail&...tail){\n print(head);\n println(tail...);\n }\n\n void newline(){\n was_output = true;\n println();\n }\n\n template<class t>\n istream& operator>>(istream&is, vector<t>&x){\n for(auto &i:x)is >> i;\n return is;\n }\n\n template<class t, class u>\n istream& operator>>(istream&is, pair<t, u>&x){\n is >> x.first >> x.second;\n return is;\n }\n\n template<class t>\n ostream& operator<<(ostream&os, vector<t> &v){\n os << \"{\";\n for(t &i:v){\n os << i << \", \";\n }\n os << \"}\";\n return os;\n }\n\n template<class t = long long>\n t in(){\n t res; cin >> res; return res;\n }\n\n template<class t>\n vector<t> sorted(vector<t> line,function<bool(t,t)> comp=[](t a,t b){return a<b;}){\n sort(line.begin(),line.end(),comp);\n return line;\n }\n\n template<class t>\n vector<t> reversed(vector<t> line){\n reverse(line.begin(),line.end());\n return line;\n }\n string reversed(string str){\n reverse(str.begin(),str.end());\n return str;\n }\n\n long long gcd(long long a,long long b){\n while(b){\n a %= b;\n swap(a,b);\n }\n return a;\n }\n\n long long lcm(long long a,long long b){\n return a / gcd(a,b) * b;\n }\n\n class output_initializer{\n public:\n output_initializer(){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << setprecision(20);\n }\n };output_initializer OUTPUT_INITIALIZER_INSTANCE = output_initializer();\n}\n\nusing namespace templates;\n\nusing P = complex<double>;\nusing L = pair<P,P>;\n\nP norm(P x){\n return x / abs(x);\n}\n\ntemplate<class t>\nt square(t x){\n return x;\n}\n\nP polar(double x){\n return P(cos(x),sin(x));\n}\n\nconst double EPS = 1e-5;\n\nint sign(double x){\n if(abs(x) < EPS)return 0;\n if(x < 0)return -1;\n return 1;\n}\n\ndouble abss(P x){\n return abs(x) * abs(x);\n}\n\nvector<L> tangent_cc(P p1,P p2,double r1,double r2) {\n vector<L> res;\n if (r1 > r2){\n swap(r1,r2);\n swap(p1,p2);\n }\n\n double g = abss(p1 - p2);\n if (sign(g) == 0) return res;\n\n P u = (p1 - p2) / sqrt(g);\n P v(-u.imag(), u.real());\n\n for (int s = 1; s >= -1; s -= 2) {\n double h = (r1 * s + r2) / sqrt(g);\n if (sign(1 - abss(h)) == 0) {\n res.emplace_back(p2 + u * r2, p2 + (u + v) * r2);\n } else if (sign(1 - abss(h)) > 0) {\n P uu = u * h;\n P vv = v * sqrt(1 - abss(h));\n res.emplace_back(p2 + (uu + vv) * r2, p1 - (uu + vv) * r1 * double(s));\n res.emplace_back(p2 + (uu - vv) * r2, p1 - (uu - vv) * r1 * double(s));\n }\n }\n return res;\n}\n\ndouble dot(P v1,P v2){\n return v1.real() * v2.real() + v1.imag() * v2.imag();\n}\n\ndouble dist(P p,L l){\n P u = norm(l.first - l.second);\n P v(-u.imag(),u.real());\n return abs(dot(p - l.first,v));\n}\n\nint func(int n){\n vector<P> poss;\n vector<double> rs;\n vector<double> ms;\n rep(i,n){\n int x=in();\n int y=in();\n int r=in();\n int m=in();\n poss.emplace_back(x,y);\n rs.emplace_back(r);\n ms.emplace_back(r+m);\n }\n method(get_lines,vector<L>,int i,int j){\n vector<L> res;\n vector<L> a1 = tangent_cc(poss[i],poss[j],rs[i],rs[j]);\n vector<L> a2 = tangent_cc(poss[i],poss[j],ms[i],rs[j]);\n vector<L> a3 = tangent_cc(poss[i],poss[j],rs[i],ms[j]);\n vector<L> a4 = tangent_cc(poss[i],poss[j],ms[i],ms[j]);\n res.insert(res.end(),all(a1));\n res.insert(res.end(),all(a2));\n res.insert(res.end(),all(a3));\n res.insert(res.end(),all(a4));\n return res;\n };\n method(get_lines2,vector<L>,int i){\n vector<L> res;\n res.emplace_back(poss[i] + P(1,0) * rs[i],poss[i] + P(1,0) * rs[i] + P(0,1));\n res.emplace_back(poss[i] + P(1,0) * ms[i],poss[i] + P(1,0) * ms[i] + P(0,1));\n return res;\n };\n int res = 0;\n rep(i,n){\n rep(j,i+1,n){\n foreach(l,get_lines(i,j)){\n int sum = 0;\n rep(g,n){\n double p = dist(poss[g],l);\n if(rs[g] - EPS <= p and p <= ms[g] + EPS){\n ++sum;\n }\n }\n chmax(res,sum);\n }\n }\n }\n rep(i,n){\n foreach(l,get_lines2(i)){\n int sum = 0;\n rep(g,n){\n double p = dist(poss[g],l);\n if(rs[g] - EPS <= p and p <= ms[g] + EPS){\n ++sum;\n }\n }\n chmax(res,sum);\n }\n }\n return res;\n}\n\nint main(){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n while(true){\n int n=in();\n if(n==0)break;\n println(func(n));\n }\n return 0;\n}", "accuracy": 1, "time_ms": 150, "memory_kb": 3324, "score_of_the_acc": -0.0609, "final_rank": 3 }, { "submission_id": "aoj_2201_6025428", "code_snippet": "#include<bits/stdc++.h>\n#include <vector>\n#define rep(i,a,...) for(long long i = (a)*(strlen(#__VA_ARGS__)!=0);i<(long long)(strlen(#__VA_ARGS__)?__VA_ARGS__:(a));++i)\n#define per(i,a,...) for(long long i = (strlen(#__VA_ARGS__)?__VA_ARGS__:(a))-1;i>=(long long)(strlen(#__VA_ARGS__)?(a):0);--i)\n#define foreach(i, n) for(auto &i:(n))\n#define all(x) (x).begin(), (x).end()\n#define bit(x) (1ll << (x))\n#define lambda(RES_TYPE, ...) (function<RES_TYPE(__VA_ARGS__)>)[&](__VA_ARGS__) -> RES_TYPE\n#define method(FUNC_NAME, RES_TYPE, ...) function<RES_TYPE(__VA_ARGS__)> FUNC_NAME = lambda(RES_TYPE, __VA_ARGS__)\nusing namespace std;\nusing ll = long long;\nusing pii = pair<int,int>;\nusing pll = pair<ll,ll>;\n//const ll MOD = (ll)1e9+7;\nconst ll MOD = 998244353;\nconst int INF = (ll)1e9+7;\nconst ll INFLL = (ll)1e18;\nconst double EPS = 1e-7;\ntemplate<class t>\nusing vvector = vector<vector<t>>;\ntemplate<class t>\nusing vvvector = vector<vector<vector<t>>>;\ntemplate<class t>\nusing priority_queuer = priority_queue<t, vector<t>, greater<t>>;\ntemplate<class t, class u> bool chmax(t &a, u b){if(a<b){a=b;return true;}return false;}\ntemplate<class t, class u> bool chmin(t &a, u b){if(a>b){a=b;return true;}return false;}\n#ifdef DEBUG\n#define debug(x) cout<<\"LINE \"<<__LINE__<<\": \"<<#x<<\" = \"<<x<<endl;\n#else\n#define debug(x) (void)0\n#endif\n\n\n struct Point{\n double x,y;\n Point(){}\n Point(double x,double y):x(x),y(y){}\n Point operator+(Point p) {return Point(x+p.x,y+p.y);}\n Point operator-(Point p) {return Point(x-p.x,y-p.y);}\n Point operator*(double k){return Point(x*k,y*k);}\n Point operator/(double k){return Point(x/k,y/k);}\n double norm(){return x*x+y*y;}\n double abs(){return sqrt(norm());}\n\n bool operator<(const Point &p) const{\n return x!=p.x?x<p.x:y<p.y;\n //grid-point only\n //return !equals(x,p.x)?x<p.x:!equals(y,p.y)?y<p.y:0;\n }\n\n bool operator==(const Point &p) const{\n return fabs(x-p.x)<EPS and fabs(y-p.y)<EPS;\n }\n };\nnamespace templates{\n ll modpow(ll x, ll b,ll mod=MOD){\n ll res = 1;\n while(b){\n if(b&1)res = res * x % mod;\n x = x * x % mod;\n b>>=1;\n }\n return res;\n }\n\n ll modinv(ll x){\n return modpow(x, MOD-2);\n }\n\n bool was_output = false;\n\n void print();\n template <class t> void print(const vector<t> &);\n template <class t, class u> void print(const pair<t, u> &);\n template <class t> void print(const t&);\n template <class Head, class... Tail> void print(const Head&, const Tail&...);\n\n template <class t> void println(const vector<vector<t>>&);\n template <class t> void println(const vector<t>&);\n template <class t> void println(const t&);\n template <class Head, class... Tail> void println(const Head&, const Tail&...);\n void println();\n void newline();\n\n void print(){\n return;\n }\n\n template <class t>\n void print(const vector<t>&x){\n for(const t&i:x)print(i);\n }\n template <class t, class u>\n void print(const pair<t,u>&p){\n print(p.first);\n print(p.second);\n }\n template <class t>\n void print(const t&x){\n if(was_output)cout<<\" \";\n cout<<x;\n was_output = true;\n }\n template <class Head, class... Tail>\n void print(const Head&head,const Tail&...tail){\n print(head);\n print(tail...);\n }\n\n template<class t>\n void println(const vector<vector<t>>&x){\n for(vector<t> i:x)println(i);\n }\n template<class t>\n void println(const vector<t>&x){\n for(const t&i:x)print(i);\n println();\n }\n template <class t>\n void println(const t&x){\n print(x);\n println();\n }\n void println(){\n if(was_output){\n cout << endl;\n was_output = false;\n }\n }\n template <class Head, class... Tail>\n void println(const Head&head,const Tail&...tail){\n print(head);\n println(tail...);\n }\n\n void newline(){\n was_output = true;\n println();\n }\n\n template<class t>\n istream& operator>>(istream&is, vector<t>&x){\n for(auto &i:x)is >> i;\n return is;\n }\n\n template<class t, class u>\n istream& operator>>(istream&is, pair<t, u>&x){\n is >> x.first >> x.second;\n return is;\n }\n\n template<class t>\n ostream& operator<<(ostream&os, vector<t> &v){\n os << \"{\";\n for(t &i:v){\n os << i << \", \";\n }\n os << \"}\";\n return os;\n }\n\n template<class t = long long>\n t in(){\n t res; cin >> res; return res;\n }\n\n template<class t>\n vector<t> sorted(vector<t> line,function<bool(t,t)> comp=[](t a,t b){return a<b;}){\n sort(line.begin(),line.end(),comp);\n return line;\n }\n\n template<class t>\n vector<t> reversed(vector<t> line){\n reverse(line.begin(),line.end());\n return line;\n }\n string reversed(string str){\n reverse(str.begin(),str.end());\n return str;\n }\n\n long long gcd(long long a,long long b){\n while(b){\n a %= b;\n swap(a,b);\n }\n return a;\n }\n\n long long lcm(long long a,long long b){\n return a / gcd(a,b) * b;\n }\n\n class output_initializer{\n public:\n output_initializer(){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << setprecision(20);\n }\n };output_initializer OUTPUT_INITIALIZER_INSTANCE = output_initializer();\n}\n\nusing namespace templates;\n\ntemplate <class T>\nvector<T> make_vector(int n,T init){\n return vector<T>(n,init);\n}\n\ntemplate <class... tail>\nauto make_vector(int n, tail... args){\n return vector<decltype(make_vector(args...))>(n,make_vector(args...));\n}\nclass Vector2{\npublic:\n double x;\n double y;\n Vector2():x(0), y(0){}\n Vector2(double a, double b):x(a), y(b){}\n Vector2(const Vector2 &o):x(o.x), y(o.y){}\n\n Vector2& operator+=(Vector2 o){x+=o.x;y+=o.y;return *this;}\n Vector2& operator-=(Vector2 o){x-=o.x;y-=o.y;return *this; }\n Vector2& operator*=(double o){x*=o;y*=o;return *this;}\n Vector2& operator/=(double o){x/=o;y/=o;return *this;}\n\n Vector2 operator+(Vector2 o){return Vector2(*this)+=o;}\n Vector2 operator-(Vector2 o){return Vector2(*this)-=o;}\n Vector2 operator-(){return Vector2(-x,-y);}\n Vector2 operator*(double o){return Vector2(*this)*=o;}\n Vector2 operator*(Vector2 o){\n Vector2 res;\n res.x = x * o.x - y * o.y;\n res.y = x * o.y + y * o.x;\n return res;\n }\n Vector2 operator/(double o){return Vector2(*this)/=o;}\n\n double abs(){\n return sqrt(mag());\n }\n\n double mag(){\n return x*x+y*y;\n }\n\n Vector2 normal(){\n return (*this) / abs();\n }\n\n Vector2 rotate_90(){\n return Vector2(-y,x);\n }\n\n static double dot(Vector2 a,Vector2 b){\n return a.x * b.x + a.y * b.y;\n }\n\n static double cross(Vector2 a,Vector2 b){\n return a.x * b.y - a.y * b.x;\n }\n};\n\n\ndouble dot(Vector2 x, Vector2 y){\n return Vector2::dot(x,y);\n}\n\ndouble cross(Vector2 x, Vector2 y){\n return Vector2::cross(x,y);\n}\n\nvoid output(Vector2 x) { cout << x.x << \" \" << x.y << endl; }\n\nVector2 polar(double theta){\n return Vector2(cos(theta),sin(theta));\n}\n\n\nclass Line{\npublic:\n Vector2 f;\n Vector2 s;\n Line(){\n f = Vector2(0,0);\n s = Vector2(1,0);\n }\n Line(Point a,Point b):f(a.x,a.y),s(b.x,b.y){}\n\n Vector2 projection(Vector2 pos){\n pos -= f;\n Vector2 n = (s-f).normal();\n Vector2 res;\n res = n * dot(pos,n) + f;\n return res;\n }\n\n Vector2 reflection(Vector2 pos){\n Vector2 n;\n Vector2 res;\n pos -= f;\n double x = f.y-s.y;\n double y = s.x-f.x;\n n = Vector2(x,y).normal();\n res = pos + f - n * dot(pos,n) * 2;\n return res;\n }\n\n static bool is_cross(Line a,Line b){\n Vector2 ap = a.s - a.f;\n Vector2 bp = b.s - b.f;\n if(abs(Vector2::dot(ap,bp))<EPS){\n return true;\n }\n return false;\n }\n\n static bool is_cross_segment(Line a,Line b){\n if(Line::is_parallel(a,b)){\n return a.is_online(b.f) || a.is_online(b.s) || b.is_online(a.f) || b.is_online(a.s);\n }\n\t if(Vector2::cross(a.s-a.f,b.f-a.f)*Vector2::cross(a.s-a.f,b.s-a.f)>EPS)return false;\n\t if(Vector2::cross(b.s-b.f,a.f-b.f)*Vector2::cross(b.s-b.f,a.s-b.f)>EPS)return false;\nreturn true;\n }\n\n static bool is_parallel(Line a,Line b){\n Vector2 ap = a.s - a.f;\n Vector2 bp = b.s - b.f;\n if(abs(Vector2::cross(ap,bp))<EPS){\n return true;\n }\n return false;\n }\n\n static Vector2 cul_cross(Line a,Line b){\n if(a.is_online(b.f)){\n return b.f;\n }\n if(a.is_online(b.s)){\n return b.s;\n }\n if(b.is_online(a.f)){\n return a.f;\n }\n if(b.is_online(a.s)){\n return a.s;\n }\n Vector2 an = (a.s-a.f).rotate_90();\n if(Vector2::dot(b.f-a.f,an)<0){\n an = -an;\n }\n double dis_1 = Vector2::dot(b.f-a.f,an);\n double dis_2 = Vector2::dot(b.s-a.f,an);\n return b.f + (b.s - b.f) * dis_1 / (dis_1 - dis_2);\n }\n\n double cul_distance(Vector2 pos){\n Vector2 v = s-f;\n Vector2 n = v.normal();\n Vector2 n2 = Vector2(-n.y,n.x);\n return abs(dot(n2,pos-f));\n }\n\n static double cul_distance(Line a,Line b){\n if(Line::is_cross_segment(a,b))return 0;\n return min({a.cul_distance(b.f),a.cul_distance(b.s),\n b.cul_distance(a.f),b.cul_distance(a.s)});\n }\n\n bool is_online(Vector2 pos){\n return cul_distance(pos) < EPS;\n }\n};\n\nVector2 input_vector2(){\n\tdouble x,y;\n\tcin >> x >> y;\n\treturn Vector2(x,y);\n}\n\n\n typedef Point Vector;\n double norm(Vector a){\n return a.x*a.x+a.y*a.y;\n }\n\n Point orth(Point p) { return Point(-p.y, p.x); }\nbool equals(double a,double b){\n return abs(a-b) < EPS;\n} \n struct Circle{\n Point c;\n double r;\n Circle(){}\n Circle(Point c,double r):c(c),r(r){}\n };\n vector<Line> tangent(Circle c1,Circle c2){\n vector<Line> ls;\n if(c1.r<c2.r) swap(c1,c2);\n double g=norm(c1.c-c2.c);\n if(equals(g,0)) return ls;\n Point u=(c2.c-c1.c)/sqrt(g);\n Point v=orth(u);\n for(int s=1;s>=-1;s-=2){\n double h=(c1.r+s*c2.r)/sqrt(g);\n if(equals(1-h*h,0)){\n ls.emplace_back(c1.c+u*c1.r,c1.c+(u+v)*c1.r);\n }else if(1-h*h>0){\n Point uu=u*h,vv=v*sqrt(1-h*h);\n ls.emplace_back(c1.c+(uu+vv)*c1.r,c2.c-(uu+vv)*c2.r*s);\n ls.emplace_back(c1.c+(uu-vv)*c1.r,c2.c-(uu-vv)*c2.r*s);\n }\n }\n\n return ls;\n }\n\nvector<Line> tangents(Vector2 p1,double r1,Vector2 p2,double r2){\n return tangent(Circle(Point(p1.x,p1.y),r1),Circle(Point(p2.x,p2.y),r2));\n}\n\n\nint func(int n){\n vector<Vector2> ps(n);\n vector<double> rs(n);\n vector<double> ms(n);\n rep(i,n){\n ps[i] = input_vector2();\n rs[i] = in<double>();\n ms[i] = in<double>();\n }\n if(n==1)return 1;\n\n method(calc,int,Line l){\n int res = 0;\n rep(i,n){\n double d = l.cul_distance(ps[i]);\n if(rs[i] - EPS < d and d < EPS + ms[i] + rs[i]){\n ++res;\n }\n }\n return res;\n };\n\n int res = 0;\n\n rep(i,n){\n rep(j,i+1,n){\n rep(k,4){\n foreach(l,tangents(ps[i],rs[i]+((1&k)?0:ms[i]),ps[j],rs[j]+((2&k)?0:ms[j]))){\n chmax(res,calc(l));\n }\n }\n }\n }\n return res;\n}\n\nint main(){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n while(true){\n int n = in();\n if(n==0)break;\n println(func(n));\n }\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3472, "score_of_the_acc": -0.0465, "final_rank": 2 }, { "submission_id": "aoj_2201_6025124", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n// template {{{\nusing i32 = int;\nusing u32 = unsigned int;\nusing i64 = long long;\nusing u64 = unsigned long long;\n\n#define range(i, l, r) for (i64 i = (i64)(l); i < (i64)(r); (i) += 1)\n#define rrange(i, l, r) for (i64 i = (i64)(r) - 1; i >= (i64)(l); (i) -= 1)\n\n#define whole(f, x, ...) ([&](decltype((x)) container) { return (f)( begin(container), end(container), ## __VA_ARGS__); })(x)\n#define rwhole(f, x, ...) ([&](decltype((x)) container) { return (f)( rbegin(container), rend(container), ## __VA_ARGS__); })(x)\n\n#define debug(x) cerr << \"(\" << __LINE__ << \")\" << #x << \": \" << (x) << endl\n\nconstexpr i32 inf = 1001001001;\nconstexpr i64 infll = 1001001001001001001ll;\n\nconstexpr i32 dx[] = {0, -1, 1, 0, -1, 1, -1, 1}; \nconstexpr i32 dy[] = {-1, 0, 0, 1, -1, -1, 1, 1};\n\nstruct IoSetup { IoSetup(i32 x = 15){ cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(x); cerr << fixed << setprecision(x); } } iosetup;\n\ntemplate <typename T = i64> T input() { T x; cin >> x; return x; }\n\ntemplate <typename T> ostream &operator<<(ostream &os, vector<T> &v) { range(i, 0, v.size()) { os << v[i] << (i + 1 != (int)v.size() ? \" \" : \"\"); } return os; } \ntemplate <typename T> istream &operator>>(istream &is, vector<T> &v) { for (T &in : v) is >> in; return is; }\n\ntemplate <typename T1, typename T2> ostream &operator<<(ostream &os, pair<T1, T2> p) { os << \"(\" << p.first << \", \" << p.second << \")\"; return os; }\ntemplate <typename T1, typename T2> istream &operator>>(istream &is, pair<T1, T2> &p) { is >> p.first >> p.second; return is; }\n\ntemplate <typename T> vector<T> make_vector(size_t a, T b) { return vector<T>(a, b); }\ntemplate <typename... Ts> auto make_vector(size_t a, Ts... ts) { return vector<decltype(make_vector(ts...))>(a, make_vector(ts...)); }\n\ntemplate <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }\ntemplate <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }\n// }}}\n\n// base\nnamespace geometry {\n using namespace std;\n using real_number = long double;\n\n const real_number PI = acosl(-1);\n\n inline static real_number &eps() {\n static real_number EPS = 1e-10;\n return EPS;\n }\n\n static void set_eps(real_number EPS) {\n eps() = EPS;\n }\n\n inline int sign(real_number r) {\n set_eps(1e-10);\n if (r < -eps()) return -1;\n if (r > +eps()) return +1;\n return 0;\n }\n\n inline bool equals(real_number r1, real_number r2) {\n return sign(r1 - r2) == 0;\n }\n}\n\n// point\nnamespace geometry {\n using point = complex< real_number >;\n using points = vector< point >;\n\n istream &operator>>(istream &is, point &p) {\n real_number x, y;\n is >> x >> y;\n p = point(x, y);\n return is;\n }\n\n ostream &operator<<(ostream &os, const point &p) {\n return os << p.real() << \" \" << p.imag();\n }\n\n point operator*(const point &p, const real_number &k) {\n return point(p.real() * k, p.imag() * k);\n }\n\n point rotate(const real_number &theta, const point &p) {\n return point(cos(theta) * p.real() + sin(-theta) * p.imag(),\n sin(theta) * p.real() + cos(-theta) * p.imag());\n }\n\n bool equals(const point &a, const point &b) {\n return equals(a.real(), b.real()) and equals(a.imag(), b.imag());\n }\n}\n\n// circle\nnamespace geometry {\n struct circle {\n point p;\n real_number r;\n circle() {}\n circle(point p, real_number r) : p(p), r(r) {}\n };\n\n using circles = vector< circle >;\n}\n\n// line \nnamespace geometry {\n struct line {\n point a, b;\n\n line() = default;\n line(point a, point b) : a(a), b(b) {}\n };\n\n using lines = vector< line >;\n}\n\nnamespace geometry {\n lines tangent_cc(circle c1, circle c2) {\n lines ls;\n if (c1.r > c2.r) swap(c1, c2);\n\n real_number g = norm(c1.p - c2.p);\n if (sign(g) == 0) return ls;\n\n point u = (c1.p - c2.p) / sqrt(g);\n point v(-u.imag(), u.real());\n\n for (int s = 1; s >= -1; s -= 2) {\n real_number h = (c1.r * s + c2.r) / sqrt(g);\n if (sign(1 - norm(h)) == 0) {\n ls.emplace_back(c2.p + u * c2.r, c2.p + (u + v) * c2.r);\n } else if (sign(1 - norm(h)) > 0) {\n point uu = u * h;\n point vv = v * sqrt(1 - norm(h));\n ls.emplace_back(c2.p + (uu + vv) * c2.r, c1.p - (uu + vv) * c1.r * s);\n ls.emplace_back(c2.p + (uu - vv) * c2.r, c1.p - (uu - vv) * c1.r * s);\n }\n }\n\n return ls;\n }\n}\n\n// product\nnamespace geometry {\n real_number cross(const point &a, const point &b) {\n return a.real() * b.imag() - a.imag() * b.real();\n }\n\n real_number dot(const point &a, const point &b) {\n return a.real() * b.real() + a.imag() * b.imag();\n }\n}\n\n// projection\nnamespace geometry {\n point projection(const line &l, const point &p) {\n real_number t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n return l.a + (l.a - l.b) * t;\n }\n}\n\nnamespace geometry {\n real_number distance_lp(const line &l, const point &p) {\n point pr = projection(l, p);\n return abs(pr - p);\n }\n}\n\nnamespace geometry {\n points cross_point_cc(const circle &c1, const circle &c2) {\n real_number d = abs(c1.p - c2.p), r = c1.r + c2.r;\n if (sign(d - r) > 0 or sign(d + c1.r - c2.r) < 0) return {};\n \n real_number a = acos((norm(c1.r) - norm(c2.r) + norm(d)) / (2 * c1.r * d));\n real_number t = arg(c2.p - c1.p);\n point p = c1.p + polar(c1.r, t + a);\n point q = c1.p + polar(c1.r, t - a);\n if (equals(p.real(), q.real()) and equals(p.imag(), q.imag())) return {p};\n return {p, q};\n }\n}\n\nusing namespace geometry;\n\nvoid solve(int n) {\n points cs(n);\n vector< real_number > rs(n), ms(n);\n\n circles cirs;\n range(i, 0, n) {\n cin >> cs[i] >> rs[i] >> ms[i];\n\n cirs.emplace_back(cs[i], rs[i]);\n cirs.emplace_back(cs[i], rs[i] + ms[i]);\n }\n\n if (n == 1) {\n cout << 1 << endl;\n return;\n }\n\n lines ls;\n range(i, 0, cirs.size()) range(j, 0, i) {\n circle c1 = cirs[i], c2 = cirs[j];\n auto ts = tangent_cc(c1, c2);\n for (auto &l : ts) ls.emplace_back(l);\n\n auto ps = cross_point_cc(c1, c2);\n if ((int)ps.size() == 2) {\n ls.emplace_back(ps[0], ps[1]);\n }\n }\n\n int ans = 0;\n for (auto &l : ls) {\n int cnt = 0;\n \n range(i, 0, n) {\n real_number d = distance_lp(l, cs[i]);\n if (sign(d - rs[i]) < 0) continue;\n if (sign(rs[i] + ms[i] - d) >= 0) {\n cnt++;\n }\n }\n\n chmax(ans, cnt);\n }\n\n cout << ans << endl;\n}\n\nsigned main() {\n int n;\n\n while (cin >> n, n) {\n solve(n);\n }\n}", "accuracy": 1, "time_ms": 880, "memory_kb": 6900, "score_of_the_acc": -1.4315, "final_rank": 20 }, { "submission_id": "aoj_2201_6024038", "code_snippet": "#include <bits/stdc++.h>\n//#include <atcoder/all>\n//using namespace atcoder;\n#pragma GCC target (\"avx2\")\n#pragma GCC optimization (\"O3\")\n#pragma GCC optimization (\"unroll-loops\")\nusing namespace std;\n \ntypedef vector<int> VI;\ntypedef vector<VI> VVI;\ntypedef vector<string> VS;\ntypedef pair<int, int> PII;\ntypedef pair<int, int> pii;\ntypedef pair<long long, long long> PLL;\ntypedef pair<int, PII> TIII;\n \ntypedef long long ll;\ntypedef long double ld;\ntypedef unsigned long long ull;\n \n \n#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)\n#define REP(i, n) FOR(i, 0, n)\n#define rep(i, a, b) for (int i = a; i < (b); ++i)\n#define trav(a, x) for (auto &a : x)\n#define all(x) x.begin(), x.end()\n \n#define MOD 1000000007\n \ntemplate<class T1, class T2> inline bool chmax(T1 &a, T2 b) {if (a < b) {a = b; return true;} return false;}\ntemplate<class T1, class T2> inline bool chmin(T1 &a, T2 b) {if (a > b) {a = b; return true;} return false;}\nconst double EPS = 1e-8, PI = acos(-1);\nconst double pi = 3.141592653589793238462643383279;\n//ここから編集 \ntypedef string::const_iterator State;\nll GCD(ll a, ll b){\n return (b==0)?a:GCD(b, a%b);\n}\nll LCM(ll a, ll b){\n return a/GCD(a, b) * b;\n}\ntemplate< int mod >\nstruct ModInt {\n int x;\n \n ModInt() : x(0) {}\n \n ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}\n \n ModInt &operator+=(const ModInt &p) {\n if((x += p.x) >= mod) x -= mod;\n return *this;\n }\n \n ModInt &operator-=(const ModInt &p) {\n if((x += mod - p.x) >= mod) x -= mod;\n return *this;\n }\n \n ModInt &operator*=(const ModInt &p) {\n x = (int) (1LL * x * p.x % mod);\n return *this;\n }\n \n ModInt &operator/=(const ModInt &p) {\n *this *= p.inverse();\n return *this;\n }\n \n ModInt operator-() const { return ModInt(-x); }\n \n ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }\n \n ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }\n \n ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }\n \n ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }\n \n bool operator==(const ModInt &p) const { return x == p.x; }\n \n bool operator!=(const ModInt &p) const { return x != p.x; }\n \n ModInt inverse() const {\n int a = x, b = mod, u = 1, v = 0, t;\n while(b > 0) {\n t = a / b;\n swap(a -= t * b, b);\n swap(u -= t * v, v);\n }\n return ModInt(u);\n }\n \n ModInt pow(int64_t n) const {\n ModInt ret(1), mul(x);\n while(n > 0) {\n if(n & 1) ret *= mul;\n mul *= mul;\n n >>= 1;\n }\n return ret;\n }\n \n friend ostream &operator<<(ostream &os, const ModInt &p) {\n return os << p.x;\n }\n \n friend istream &operator>>(istream &is, ModInt &a) {\n int64_t t;\n is >> t;\n a = ModInt< mod >(t);\n return (is);\n }\n \n static int get_mod() { return mod; }\n};\n \nusing modint = ModInt< 1000000007 >;\ntemplate< typename T >\nstruct Combination {\n vector< T > _fact, _rfact, _inv;\n \n Combination(int sz) : _fact(sz + 1), _rfact(sz + 1), _inv(sz + 1) {\n _fact[0] = _rfact[sz] = _inv[0] = 1;\n for(int i = 1; i <= sz; i++) _fact[i] = _fact[i - 1] * i;\n _rfact[sz] /= _fact[sz];\n for(int i = sz - 1; i >= 0; i--) _rfact[i] = _rfact[i + 1] * (i + 1);\n for(int i = 1; i <= sz; i++) _inv[i] = _rfact[i] * _fact[i - 1];\n }\n \n inline T fact(int k) const { return _fact[k]; }\n \n inline T rfact(int k) const { return _rfact[k]; }\n \n inline T inv(int k) const { return _inv[k]; }\n \n T P(int n, int r) const {\n if(r < 0 || n < r) return 0;\n return fact(n) * rfact(n - r);\n }\n \n T C(int p, int q) const {\n if(q < 0 || p < q) return 0;\n return fact(p) * rfact(q) * rfact(p - q);\n }\n \n T H(int n, int r) const {\n if(n < 0 || r < 0) return (0);\n return r == 0 ? 1 : C(n + r - 1, r);\n }\n};\n \nll modpow(ll x, ll n, ll mod) {\n ll res = 1;\n x %= mod;\n if(x == 0) return 0;\n while(n) {\n if(n&1) res = (res * x) % mod;\n x = (x * x) % mod;\n n >>= 1;\n }\n return res;\n} \ninline long long mod(long long a, long long m) {\n return (a % m + m) % m;\n}\ntemplate<typename T> \nstruct BIT{\n int N;\n std::vector<T> node;\n BIT(){}\n BIT(int n){\n N = n;\n node.resize(N+10);\n }\n void build(int n) {\n N = n;\n node.resize(N+10);\n }\n /* a: 1-indexed */\n void add(int a, T x){\n for(int i=a; i<(int)node.size(); i += i & -i) node[i] += x;\n }\n\n /* [1, a] */\n T sum(int a){\n T res = 0;\n for(int x=a; x>0; x -= x & -x) res += node[x];\n return res;\n }\n\n /* [l, r] */\n T rangesum(int l, int r){\n return sum(r) - sum(l-1);\n }\n\n /* \n a1+a2+...+aw >= valとなるような最小のwを返す\n @verify https://codeforces.com/contest/992/problem/E\n */\n int lower_bound(T val) {\n if(val < 0) return 0;\n\n int res = 0;\n int n = 1; \n while (n < N) n *= 2;\n\n T tv=0;\n for (int k = n; k > 0; k /= 2) {\n if(res + k < N && node[res + k] < val){\n val -= node[res+k];\n res += k;\n }\n }\n return res+1; \n }\n};\n\nstruct UnionFind{\n std::vector<int> par;\n std::vector<int> siz;\n\n UnionFind(int sz_): par(sz_), siz(sz_) {\n for(int i=0; i<sz_; ++i) par[i] = i, siz[i] = 1;\n }\n\n void init(int sz_){\n par.resize(sz_);\n siz.resize(sz_);\n for(int i=0; i<sz_; ++i) par[i] = i, siz[i] = 1;\n }\n\n int root(int x){\n if(x == par[x]) return x;\n return par[x] = root(par[x]);\n }\n\n bool merge(int x, int y){\n x = root(x), y = root(y);\n if(x == y) return false;\n if(siz[x] < siz[y]) std::swap(x, y);\n siz[x] += siz[y];\n par[y] = x;\n return true;\n }\n\n bool issame(int x, int y){\n return root(x) == root(y);\n }\n\n int size(int x){\n return siz[root(x)];\n }\n};\nstruct RollingHash{\n\n using ull = unsigned long long;\n const ull mod = (1ULL << 61) - 1;\n const ull MASK30 = (1ULL << 30) - 1;\n const ull MASK31 = (1ULL << 31) - 1;\n\n const ull MASK61 = mod;\n\n ull base;\n int n;\n vector<ull> hash, pow;\n\n RollingHash(const string &s)\n {\n random_device rnd;\n mt19937_64 mt(rnd());\n base = 1001;\n \n n = (int)s.size();\n hash.assign(n+1, 0);\n pow.assign(n+1, 1);\n \n for(int i=0; i<n; i++){\n hash[i+1] = calc_mod(mul(hash[i], base) + s[i]);\n pow[i+1] = calc_mod(mul(pow[i], base));\n }\n }\n\n ull calc_mod(ull x){\n ull xu = x >> 61;\n ull xd = x & MASK61;\n ull res = xu + xd;\n if(res >= mod) res -= mod;\n return res;\n }\n\n ull mul(ull a, ull b){\n ull au = a >> 31;\n ull ad = a & MASK31;\n ull bu = b >> 31;\n ull bd = b & MASK31;\n ull mid = ad * bu + au * bd;\n ull midu = mid >> 30;\n ull midd = mid & MASK30;\n return calc_mod(au * bu * 2 + midu + (midd << 31) + ad * bd);\n }\n\n //[l,r)のハッシュ値\n inline ull get(int l, int r){\n ull res = calc_mod(hash[r] + mod*3-mul(hash[l], pow[r-l]));\n return res;\n }\n //string tのハッシュ値\n inline ull get(string t){\n ull res = 0;\n for(int i=0; i<t.size(); i++){\n res = calc_mod(mul(res, base)+t[i]);\n }\n return res;\n }\n //string s中のtの数をカウント\n inline int count(string t) {\n if(t.size() > n) return 0;\n auto hs = get(t);\n int res = 0;\n for(int i=0; i<n-t.size()+1; i++){\n if(get(i, i+t.size()) == hs) res++; \n }\n return res;\n }\n\n /* \n concat \n @verify https://codeforces.com/problemset/problem/514/C\n */\n inline ull concat(ull h1, ull h2, int h2len){\n return calc_mod(h2 + mul(h1, pow[h2len]));\n }\n\n // LCPを求める S[a:] T[b:]\n inline int LCP(int a, int b){\n int len = min((int)hash.size()-a, (int)hash.size()-b);\n \n int lb = -1, ub = len;\n while(ub-lb>1){\n int mid = (lb+ub)/2;\n\n if(get(a, a+mid) == get(b, b+mid)) lb = mid;\n else ub = mid;\n }\n return lb;\n }\n};\nusing Point = complex< double >;\nbool eq(double a, double b){ return fabs(a-b) < EPS; }\nnamespace std {\n bool operator<(const Point a, const Point b) {\n if(eq(a.real(),b.real())) return a.imag() < b.imag();\n return a.real() < b.real();\n }\n}\n\nistream &operator>> (istream &is, Point &p) {\n double a, b;\n is >> a >> b;\n p = Point(a, b);\n return is;\n}\n\nostream &operator<< (ostream &os, Point &p) {\n return os << fixed << setprecision(10) << p.real() << \" \" << p.imag();\n}\n\nbool operator<(const Point &a, const Point &b) {\n return a.real() != b.real() ? a.real() < b.real() : a.imag() < b.imag();\n}\n\n// rotate Φ(rad)\n// x = r * cos(θ + Φ)\n// = r * cos(θ) * cos(Φ) - r * sin(θ) * sin(Φ)\n// = x * cos(Φ) - y * sin(Φ) (∵ cos(θ) = x/r, sin(θ) = y/r) \nPoint rotate(double phi, const Point &p) {\n double x = p.real(), y = p.imag();\n return Point(x * cos(phi) - y * sin(phi), x * sin(phi) + y * cos(phi));\n}\n\ndouble radian_to_degree(double r) {\n return (r * 180.0 / PI);\n}\n\ndouble degree_to_radian(double d) {\n return (d * PI / 180.0);\n}\n\nstruct Line{\n Point a, b;\n\n Line() = default;\n\n Line(Point a, Point b) : a(a), b(b){}\n\n Line(double A, double B, double C){\n //ax + by = c\n if(eq(A, 0)){\n a = Point(0, C/B), b = Point(1, C/B);\n }else if(eq(B, 0)){\n a = Point(C/A, 0), b = Point(C/A, 1);\n }else{\n a = Point(0, C/B), b = Point(C/A, 0);\n }\n }\n\n friend istream &operator>>(istream &is, Line &a) {\n return is >> a.a >> a.b;\n }\n friend ostream &operator<<(ostream &os, Line &a) {\n return os << a.a << \" to \" << a.b;\n }\n};\n\nstruct Segment: Line {\n Segment() = default;\n\n Segment(Point a, Point b) : Line(a, b) {}\n};\n\nstruct Circle {\n Point p;\n double r;\n\n Circle() = default;\n\n Circle(Point p, double r): p(p), r(r){}\n};\n\nusing Points = vector<Point>;\nusing Polygon = vector<Point>;\nusing Segments = vector<Segment>;\nusing Lines = vector<Line>;\nusing Circles = vector<Circle>;\n\ndouble cross(const Point &a, const Point &b) {\n return a.real() * b.imag() - a.imag() * b.real();\n}\n\ndouble dot(const Point& a, const Point &b) {\n return a.real() * b.real() + a.imag() * b.imag();\n}\n\n//https://mathtrain.jp/projection\nPoint projection(const Line &l, const Point &p){\n double t = dot(p - l.a, l.a-l.b) / norm(l.a - l.b);\n return l.a + (l.a - l.b) * t;\n}\n\nPoint projection(const Segment &l, const Point &p){\n double t = dot(p - l.a, l.b-l.a) / norm(l.a - l.b);\n return l.a + (l.b - l.a) * t;\n}\n\nPoint reflection(const Line &l, const Point &p){\n return p + (projection(l, p) - p) * 2.0;\n}\n\nint ccw(const Point &a, const Point &b, const Point &c) {\n if(cross(b-a, c-a) > EPS) return 1; // \"COUNTER_CLOCKWISE\"\n if(cross(b-a, c-a) < -EPS) return -1; // \"CLOCKWISE\"\n if(dot(b-a, c-a) < -EPS) return 2; // \"ONLINE_BACK\" c-a-b\n if(norm(b-a) < norm(c-a) - EPS) return -2; // \"ONLINE_FRONT\" a-b-c\n return 0; // \"ON_SEGMENT\" a-c-b\n}\n\nbool parallel(const Line &a, const Line &b){\n return eq(cross(a.a-a.b, b.a-b.b), 0.0);\n}\nbool orthogonal(const Line &a, const Line &b){\n return eq(dot(a.a-a.b, b.a-b.b), 0.0);\n}\nenum { OUT, ON, IN };\n\nint contains(const Polygon& Q, const Point& p){\n bool in = false;\n for(int i=0; i<Q.size(); i++){\n Point a = Q[i] - p, b = Q[(i+1)%Q.size()]-p;\n if(a.imag() > b.imag()) swap(a, b);\n if(a.imag() <= 0 && 0 < b.imag() && cross(a, b) < 0) in = !in;\n if(cross(a, b) == 0 && dot(a, b) <= 0) return ON;\n }\n return in ? IN : OUT;\n}\nbool intersect(const Segment &s, const Point &p){\n return ccw(s.a, s.b, p) == 0;\n}\n\n//直線の交差判定\nbool intersect(const Line &a, const Line &b) {\n if(parallel(a, b)) return 0;\n return 1;\n}\n\n//線分が重なる/端点で交わるときtrue\nbool intersect(const Segment &a, const Segment &b) {\n return ccw(a.a, a.b, b.a) * ccw(a.a, a.b, b.b) <= 0 && ccw(b.a, b.b, a.a) * ccw(b.a, b.b, a.b) <= 0;\n}\n\n\nPoint crosspoint(const Line &a, const Line &b){\n double d = cross(b.b-b.a, a.b-a.a);\n if(eq(d, 0.0)) return Point(1e9, 1e9); \n \n return a.a + (a.b - a.a) * cross(b.b-b.a, b.b-a.a) / d;\n}\n\nPoint crosspoint(const Segment &a, const Segment &b){\n return crosspoint(Line(a.a, a.b), Line(b.a, b.b));\n}\ndouble distance(const Point &a, const Point &b){\n return abs(a - b);\n}\ndouble distance(const Line &l, const Point &p){\n return abs( cross(p - l.a, l.b-l.a) / abs(l.b-l.a) );\n}\ndouble distance(const Segment &s, const Point &p){\n Point r = projection(s, p);\n if(intersect(s, r)) return abs(r-p);\n return min(abs(s.a - p), abs(s.b - p));\n}\n// double distance(const Line &a, const Line &b){\n\n// }\n// double distance(const Line &a, const Segment &b){\n\n// }\ndouble distance(const Segment &a, const Segment &b) {\n return intersect(a, b) ? 0 : min({distance(a, b.a), distance(a, b.b), distance(b, a.a), distance(b, a.b)});\n}\n\n/* 2円の交点 */\nvector<Point> crosspointCC(Circle C1, Circle C2){\n vector<Point> ps;\n Point ab = C2.p - C1.p;\n double d = abs(ab);\n double rc = (C1.r * C1.r + d * d - C2.r * C2.r) / (2 * d);\n if(eq(d, 0) || C1.r < abs(rc)) return ps;\n\n double rs = sqrt(C1.r * C1.r - rc*rc);\n\n Point abN = ab * Point(0, rs/d);\n Point cp = C1.p + rc / d * ab;\n ps.push_back(cp + abN);\n if(!eq(norm(abN), 0))ps.push_back(cp-abN);\n return ps;\n}\n\nvector<Point> crosspointCL(Circle C, Line l){\n Point p = projection(l, C.p);\n\n Point e = (l.b-l.a)/abs(l.b-l.a);\n if(eq(distance(l, C.p), C.r)) {\n return vector<Point>{p, p};\n }\n double base = sqrt(C.r*C.r-norm(p-C.p));\n \n return vector<Point>{p+e*base, p-e*base};\n}\n\n/* 円同士の共通部分の面積を求める */\n// verify: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_I date: 2020/10/11\nlong double AreaofCC(Circle& C1, Circle& C2){\n if(C1.r > C2.r) swap(C1, C2);\n long double nor = norm(C1.p - C2.p);\n long double dist = sqrtl(nor);\n \n if(C1.r + C2.r < dist+EPS) return 0;\n\n if(dist + C1.r < C2.r + EPS) return C1.r * C1.r * PI;\n\n long double theta1 = acosl((nor + C1.r*C1.r - C2.r * C2.r)/(2*C1.r*dist));\n\n long double theta2 = acosl((nor + C2.r*C2.r - C1.r * C1.r)/(2*C2.r*dist));\n \n return (theta1 - sinl(theta1+ theta1) *0.5) * C1.r * C1.r + (theta2 - sinl(theta2+theta2) *0.5) * C2.r * C2.r;\n}\n\n\nPolygon convex_hull(Polygon &p)\n{\n int n = (int)p.size(), k = 0;\n if (n <= 2)\n return p;\n sort(p.begin(), p.end());\n vector<Point> ch(n * 2);\n\n for (int i = 0; i < n; ch[k++] = p[i++])\n {\n while (k >= 2 && cross(ch[k - 1] - ch[k - 2], p[i] - ch[k - 1]) < 0)\n --k;\n }\n\n for (int i = n - 2, t = k + 1; i >= 0; ch[k++] = p[i--])\n {\n while (k >= t && cross(ch[k - 1] - ch[k - 2], p[i] - ch[k - 1]) < 0)\n --k;\n }\n ch.resize(k - 1);\n return ch;\n}\ndouble convex_diameter(const Polygon &p)\n{\n int n = (int)p.size();\n if (n == 2)\n return abs(p[0] - p[1]);\n\n int is = 0, js = 0;\n for (int i = 1; i < n; ++i)\n {\n if (imag(p[i]) > imag(p[is]))\n is = i;\n if (imag(p[i]) < imag(p[js]))\n js = i;\n }\n\n double res = abs(p[is] - p[js]);\n int i, maxi, j, maxj;\n i = maxi = is;\n j = maxj = js;\n do\n {\n if (cross(p[(i + 1) % n] - p[i], p[(j + 1) % n] - p[j]) >= 0)\n j = (j + 1) % n;\n else\n i = (i + 1) % n;\n res = max(res, abs(p[i] - p[j]));\n } while (i != is || j != js);\n return res;\n}\n\nPolygon convex_cut(const Polygon &p, const Line l)\n{\n Polygon ret;\n for (int i = 0; i < p.size(); ++i)\n {\n Point now = p[i], nxt = p[(i + 1) % p.size()];\n if (ccw(l.a, l.b, now) != -1) //交点が線分l上にあるとき\n ret.push_back(now);\n if (ccw(l.a, l.b, now) * ccw(l.a, l.b, nxt) < 0)\n {\n ret.push_back(crosspoint(Line(now, nxt), l));\n }\n }\n return (ret);\n}\ndouble closestpair(Points &a, int l, int r){\n if(r-l<=1) return 1e20;\n int mid = (l+r)/2;\n\n double X = a[mid].real();\n double d = min(closestpair(a, l, mid), closestpair(a, mid, r));\n inplace_merge(a.begin()+l, a.begin()+mid, a.begin()+r, [](const Point& pa, const Point& pb){\n return pa.imag() < pb.imag();\n });\n\n Points b;\n for(int i=l; i<r; i++){\n if(abs(a[i].real()-X) >= d) continue;\n for(int j=b.size()-1; j>=0; j--){\n if(abs((a[i]-b[j]).imag()) >= d) break;\n d = min(d, abs(a[i]-b[j]));\n }\n b.push_back(a[i]);\n }\n return d;\n}\n\n/* 円の交差判定 */\n/* verify: http://judge.u-aizu.ac.jp/onlinejudge/finder.jsp?course=CGL date:2020/10/11 */\nint intersectionCC(Circle c1, Circle c2){\n if(c1.r > c2.r) swap(c1, c2);\n double d1 = abs(c1.p-c2.p); \n double d2 = c1.r + c2.r;\n if(d1 > d2) return 4; /* 互いに交点を持たない */\n else if(d1 == d2) return 3; /* 外接する場合 */\n else{\n if(c2.r == c1.r + d1) return 1; /* 内接する場合 */\n else if(c2.r > c1.r + d1) return 0; /* 包含 */\n else return 2; /* 交点を2つ持つ */\n }\n}\n\n/* 点集合が反時計回りに与えられる場合のみ */\ndouble Area(Points &g){\n double res = 0;\n int n = g.size();\n REP(i,n){\n res += cross(g[i], g[(i+1)%n]);\n }\n return res/2.0;\n}\n\n/* 内接円 */\n/* verify: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_B&lang=ja date: 2020/10/11 */\npair<Point, double> incircle_of_a_Triangle(Points &p){\n\n double a = abs(p[1]-p[2]), b = abs(p[2]-p[0]), c = abs(p[0]-p[1]);\n double s = (a+b+c) / 2.0;\n double S = sqrtl(s*(s-a)*(s-b)*(s-c));\n double r = S/s; \n \n Point pp = (a*p[0]+b*p[1]+c*p[2])/(a+b+c);\n return make_pair(pp, r);\n} \n\n/* 外接円 */\npair<Point, double> circumscribed_circle_of_a_triangle(Points &p){\n\n Point m1((p[0]+p[1])/2.0), m2((p[1]+p[2])/2.0);\n Point v((p[1]-p[0]).imag(), (p[0]-p[1]).real()), w((p[1]-p[2]).imag(), (p[2]-p[1]).real());\n\n Line l1(m1, Point(v+m1)), l2(m2, Point(w+m2));\n\n Point x = crosspoint(l1, l2);\n double r = abs(x-p[0]);\n return make_pair(x, r);\n}\n\n/* ある点pを通る円cの接線 */\nPoints tangent_to_a_circle(Point p, Circle C){\n Points ps;\n double d = abs(C.p-p);\n if(eq(d,0)){\n ps.push_back(p);\n }else if(d>EPS){\n \n double d2 = sqrt(d*d-C.r*C.r);\n \n long double theta = acosl(d2/d);\n //cout << theta << endl;\n Point pp = C.p - p;\n Point p2 = rotate(-theta, pp);\n\n Point e = p2/abs(p2);\n Point ppp = e*d2;\n ps.push_back(p + ppp);\n p2 = rotate(theta, pp);\n e = p2/abs(p2);\n ppp = e*d2;\n ps.push_back(p + ppp);\n \n }\n return ps;\n}\n\nLines tangent(Circle C1, Circle C2){\n Lines ls;\n if(C1.r < C2.r) swap(C1, C2);\n double d = norm(C1.p - C2.p);\n if(eq(d, 0)) return ls;\n Point u = (C2.p - C1.p) / sqrt(d);\n Point v = rotate(pi/2, u);\n\n for(double s: {-1, 1}) {\n double h = (C1.r + s * C2.r) / sqrt(d);\n if(eq(1-h*h, 0)) {\n ls.emplace_back(C1.p + u * C1.r, C1.p + (u+v) * C1.r);\n }else if(1-h*h>0){\n Point uu = u*h, vv = v*sqrt(1-h*h);\n ls.emplace_back(C1.p + (uu+vv) * C1.r, C2.p - (uu+vv) * C2.r * s);\n ls.emplace_back(C1.p + (uu-vv) * C1.r, C2.p - (uu-vv) * C2.r * s);\n }\n }\n return ls;\n}\nLine bisector(Point a, Point b){\n Point A = (a+b)*Point(0.5, 0); \n return Line(A, A+(b-a)*Point(0, PI/2));\n}\n\nPolygon voronoi_cell(Polygon Q, Points P, int s){\n REP(i,P.size()){\n if(i!=s){\n Q = convex_cut(Q, bisector(P[s], P[i]));\n }\n }\n return Q;\n}\nint main() { \n cin.tie(0);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(12);\n \n int n;\n while(cin >> n, n) {\n vector<double> x(n), y(n), r(n), m(n);\n REP(i,n) cin >> x[i] >> y[i] >> r[i] >> m[i];\n\n vector<Point> vp;\n int ans = 1;\n REP(i,n) {\n for(int j=i+1; j<n; j++) {\n {\n auto ls = tangent(Circle(Point(x[i], y[i]), r[i]+m[i]), Circle(Point(x[j], y[j]), r[j]+m[j]));\n for(auto l: ls) {\n int cnt = 0;\n REP(k,n) {\n double dist = distance(l, Point(x[k], y[k]));\n if(r[k] <= dist+EPS && dist <= r[k] + m[k] + EPS) cnt++;\n }\n ans = max(ans, cnt);\n }\n }\n {\n auto ls = tangent(Circle(Point(x[i], y[i]), r[i]), Circle(Point(x[j], y[j]), r[j]+m[j]));\n for(auto l: ls) {\n int cnt = 0;\n REP(k,n) {\n double dist = distance(l, Point(x[k], y[k]));\n if(r[k] <= dist+EPS && dist <= r[k] + m[k] + EPS) cnt++;\n }\n ans = max(ans, cnt);\n }\n }\n {\n auto ls = tangent(Circle(Point(x[i], y[i]), r[i]+m[i]), Circle(Point(x[j], y[j]), r[j]));\n for(auto l: ls) {\n int cnt = 0;\n REP(k,n) {\n double dist = distance(l, Point(x[k], y[k]));\n if(r[k] <= dist+EPS && dist <= r[k] + m[k] + EPS) cnt++;\n }\n ans = max(ans, cnt);\n }\n }\n {\n auto ls = tangent(Circle(Point(x[i], y[i]), r[i]), Circle(Point(x[j], y[j]), r[j]));\n for(auto l: ls) {\n int cnt = 0;\n REP(k,n) {\n double dist = distance(l, Point(x[k], y[k]));\n if(r[k] <= dist+EPS && dist <= r[k] + m[k] + EPS) cnt++;\n }\n ans = max(ans, cnt);\n }\n }\n }\n }\n\n cout << ans << '\\n';\n }\n return 0;\n}", "accuracy": 1, "time_ms": 130, "memory_kb": 3760, "score_of_the_acc": -0.1727, "final_rank": 11 }, { "submission_id": "aoj_2201_6023902", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define rep(i,a,b) for(int i=(int)(a);i<(int)(b);i++)\n#define ALL(v) (v).begin(),(v).end()\nusing ll=long long int;\nconst int inf = 0x3fffffff; const ll INF = 0x1fffffffffffffff; const double eps=1e-12;\ntemplate<typename T>inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;}\ntemplate<typename T>inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;}\n\nusing T=double;\nusing P=complex<T>;\nusing Poly=vector<P>;\n#define X real()\n#define Y imag()\ninline bool eq(const T& a,const T& b){\n return fabs(a-b)<eps;\n}\ninline bool cmp(const P& a,const P& b){\n return (eq(a.X,b.X)?a.Y<b.Y:a.X<b.X);\n}\nstruct Line{\n P a,b;\n Line(){}\n Line(P _a,P _b):a(_a),b(_b){}\n Line(T A,T B,T C){\n if(eq(A,.0)){\n a=P(0,C/B),b=P(1/C/B);\n }\n else if(eq(B,.0)){\n a=P(C/A,0),b=P(C/A,1);\n }\n else{\n a=P(0,C/B),b=P(C/A,0);\n }\n }\n};\nstruct Segment:Line{\n Segment(){}\n Segment(P _a,P _b):Line(_a,_b){}\n};\nstruct Circle{\n P p; T r;\n Circle(){}\n Circle(P _p,T _r):p(_p),r(_r){}\n};\n\nistream& operator>>(istream& is,P& p){\n T x,y; is>>x>>y; p=P(x,y);\n return is;\n}\nostream& operator<<(ostream& os,P& p){\n os<<fixed<<setprecision(12)<<p.X<<' '<<p.Y;\n return os;\n}\nP unit(const P& a){return a/abs(a);}\nT dot(const P& a,const P& b){\n return a.X*b.X+a.Y*b.Y;\n}\nT cross(const P& a,const P& b){\n return a.X*b.Y-a.Y*b.X;\n}\nP rot(const P& a,const T& theta){\n return P(cos(theta)*a.X-sin(theta)*a.Y,\n sin(theta)*a.X+cos(theta)*a.Y);\n}\nT arg(const P& a,const P& b,const P& c){\n return acos(dot(a-b,c-b)/abs(a-b)/abs(c-b));\n}\n\nP Projection(const Line&l,const P& p){\n T t=dot(p-l.a,l.a-l.b)/norm(l.a-l.b);\n return l.a+(l.a-l.b)*t;\n}\nP Projection(const Segment&l,const P& p){\n T t=dot(p-l.a,l.a-l.b)/norm(l.a-l.b);\n return l.a+(l.a-l.b)*t;\n}\nP Reflection(const Line& l,const P& p){\n return p+(Projection(l,p)-p)*2.;\n}\nint ccw(const P& a,P b,P c){\n b-=a; c-=a;\n if(cross(b,c)>eps)return 1; //ccw\n if(cross(b,c)<-eps)return -1; //cw\n if(dot(b,c)<0)return 2; //c,a,b\n if(norm(b)<norm(c))return -2; //a,b,c\n return 0; //a,c,b\n}\nbool isOrthogonal(const Line& a,const Line& b){\n return eq(dot(a.b-a.a,b.b-b.a),.0);\n}\nbool isParallel(const Line& a,const Line& b){\n return eq(cross(a.b-a.a,b.b-b.a),.0);\n}\nbool isIntersect(const Segment& a,const Segment& b){\n return ccw(a.a,a.b,b.a)*ccw(a.a,a.b,b.b)<=0 and\n ccw(b.a,b.b,a.a)*ccw(b.a,b.b,a.b)<=0;\n}\nint isIntersect(const Circle& a,const Circle& b){\n T d=abs(a.p-b.p);\n if(d>a.r+b.r+eps)return 4;\n if(eq(d,a.r+b.r))return 3;\n if(eq(d,abs(a.r-b.r)))return 1;\n if(d<abs(a.r-b.r)-eps)return 0;\n return 2;\n}\nT Dist(const Line& a,const P& b){\n P c=Projection(a,b);\n return abs(c-b);\n}\nT Dist(const Segment& a,const P& b){\n if(dot(a.b-a.a,b-a.a)<eps)return abs(b-a.a);\n if(dot(a.a-a.b,b-a.b)<eps)return abs(b-a.b);\n return abs(cross(a.b-a.a,b-a.a))/abs(a.b-a.a);\n}\nT Dist(const Segment& a,const Segment& b){\n if(isIntersect(a,b))return .0;\n T res=min({Dist(a,b.a),Dist(a,b.b),Dist(b,a.a),Dist(b,a.b)});\n return res;\n}\nP Intersection(const Line& a,const Line& b){\n T d1=cross(a.b-a.a,b.b-b.a);\n T d2=cross(a.b-a.a,a.b-b.a);\n if(eq(d1,0) and eq(d2,0))return b.a;\n return b.a+(b.b-b.a)*(d2/d1);\n}\nPoly Intersection(const Circle& a,const Line& b){\n Poly res;\n T d=Dist(b,a.p);\n if(d>a.r+eps)return res;\n P h=Projection(b,a.p);\n if(eq(d,a.r)){\n res.push_back(h);\n return res;\n }\n P e=unit(b.b-b.a);\n T ph=sqrt(a.r*a.r-d*d);\n res.push_back(h-e*ph);\n res.push_back(h+e*ph);\n return res;\n}\nPoly Intersection(const Circle& a,const Segment& b){\n Line c(b.a,b.b);\n Poly sub=Intersection(a,c);\n double xmi=min(b.a.X,b.b.X),xma=max(b.a.X,b.b.X);\n double ymi=min(b.a.Y,b.b.Y),yma=max(b.a.Y,b.b.Y);\n Poly res;\n rep(i,0,sub.size()){\n if(xmi<=sub[i].X+eps and sub[i].X-eps<=xma and\n ymi<=sub[i].Y+eps and sub[i].Y-eps<=yma){\n res.push_back(sub[i]);\n }\n }\n return res;\n}\nPoly Intersection(const Circle& a,const Circle& b){\n Poly res;\n int mode=isIntersect(a,b);\n T d=abs(a.p-b.p);\n if(mode==4 or mode==0)return res;\n if(mode==3){\n T t=a.r/(a.r+b.r);\n res.push_back(a.p+(b.p-a.p)*t);\n return res;\n }\n if(mode==1){\n if(b.r<a.r-eps){\n res.push_back(a.p+(b.p-a.p)*(a.r/d));\n }\n else{\n res.push_back(b.p+(a.p-b.p)*(b.r/d));\n }\n return res;\n }\n T rc=(a.r*a.r+d*d-b.r*b.r)/d/2.;\n T rs=sqrt(a.r*a.r-rc*rc);\n if(a.r-abs(rc)<eps)rs=0;\n P e=unit(b.p-a.p);\n res.push_back(a.p+rc*e+rs*e*P(0,1));\n res.push_back(a.p+rc*e+rs*e*P(0,-1));\n return res;\n}\nT Area(const Poly& a){\n T res=0;\n int n=a.size();\n rep(i,0,n)res+=cross(a[i],a[(i+1)%n]);\n return fabs(res/2.);\n}\nT Area(const Poly& a,const Circle& b){\n int n=a.size();\n if(n<3)return .0;\n auto rec=[&](auto self,const Circle& c,const P& p1,const P& p2){\n P va=c.p-p1,vb=c.p-p2;\n T f=cross(va,vb),res=.0;\n if(eq(f,.0))return res;\n if(max(abs(va),abs(vb))<c.r+eps)return f;\n if(Dist(Segment(p1,p2),c.p)>c.r-eps)return c.r*c.r*arg(vb*conj(va));\n auto u=Intersection(c,Segment(p1,p2));\n Poly sub;\n sub.push_back(p1);\n for(auto& x:u)sub.push_back(x);\n sub.push_back(p2);\n rep(i,0,sub.size()-1)res+=self(self,c,sub[i],sub[i+1]);\n return res;\n };\n T res=.0;\n rep(i,0,n)res+=rec(rec,b,a[i],a[(i+1)%n]);\n return fabs(res/2.);\n}\nT Area(const Circle& a,const Circle& b){\n T d=abs(a.p-b.p);\n if(d>=a.r+b.r-eps)return .0;\n if(d<=abs(a.r-b.r)+eps){\n T r=min(a.r,b.r);\n return M_PI*r*r;\n }\n T ath=acos((a.r*a.r+d*d-b.r*b.r)/d/a.r/2.);\n T res=a.r*a.r*(ath-sin(ath*2)/2.);\n T bth=acos((b.r*b.r+d*d-a.r*a.r)/d/b.r/2.);\n res+=b.r*b.r*(bth-sin(bth*2)/2.);\n return fabs(res);\n}\nbool isConvex(const Poly& a){\n int n=a.size();\n int cur,pre,nxt;\n rep(i,0,n){\n pre=(i-1+n)%n;\n nxt=(i+1)%n;\n cur=i;\n if(ccw(a[pre],a[cur],a[nxt])==-1)return 0;\n }\n return 1;\n}\nint isContained(const Poly& a,const P& b){\n bool res=0;\n int n=a.size();\n rep(i,0,n){\n P p=a[i]-b,q=a[(i+1)%n]-b;\n if(p.Y>q.Y)swap(p,q);\n if(p.Y<eps and eps<q.Y and cross(p,q)>eps)res^=1;\n if(eq(cross(p,q),.0) and dot(p,q)<eps)return 1;\n }\n return (res?2:0);\n}\nPoly ConvexHull(Poly& a){\n int n=a.size(),k=0;\n sort(ALL(a),[](const P& p,const P& q){\n return (eq(p.Y,q.Y)?p.X<q.X:p.Y<q.Y);\n });\n Poly res(n*2);\n for(int i=0;i<n;res[k++]=a[i++]){\n while(k>=2 and cross(res[k-1]-res[k-2],a[i]-res[k-1])<-eps)k--;\n }\n for(int i=n-2,t=k+1;i>=0;res[k++]=a[i--]){\n while(k>=t and cross(res[k-1]-res[k-2],a[i]-res[k-1])<-eps)k--;\n }\n res.resize(k-1); return res;\n}\nT Diam(const Poly& a){\n int n=a.size();\n int x=0,y=0;\n rep(i,1,n){\n if(a[i].Y>a[x].Y)x=i;\n if(a[i].Y<a[y].Y)y=i;\n }\n T res=abs(a[x]-a[y]);\n int i=x,j=y;\n do{\n if(cross(a[(i+1)%n]-a[i],a[(j+1)%n]-a[j])<0)i=(i+1)%n;\n else j=(j+1)%n;\n chmax(res,abs(a[i]-a[j]));\n }while(i!=x or j!=y);\n return res;\n}\nPoly Cut(const Poly& a,const Line& l){\n int n=a.size(); Poly res;\n rep(i,0,n){\n P p=a[i],q=a[(i+1)%n];\n if(ccw(l.a,l.b,p)!=-1)res.push_back(p);\n if(ccw(l.a,l.b,p)*ccw(l.a,l.b,q)<0)res.push_back(Intersection(Line(p,q),l));\n }\n return res;\n}\n\nT Closest(Poly& a){\n int n=a.size();\n if(n<=1)return 0;\n sort(ALL(a),cmp);\n Poly buf(n);\n auto rec=[&](auto self,int lb,int rb)->T{\n if(rb-lb<=1)return (T)INF;\n int mid=(lb+rb)>>1;\n auto x=a[mid].X;\n T res=min(self(self,lb,mid),self(self,mid,rb));\n inplace_merge(a.begin()+lb,a.begin()+mid,a.begin()+rb,\n [&](auto p,auto q){return p.Y<q.Y;});\n int ptr=0;\n rep(i,lb,rb){\n if(abs(a[i].X-x)>=res)continue;\n rep(j,0,ptr){\n auto sub=a[i]-buf[ptr-1-j];\n if(sub.Y>=res)break;\n chmin(res,abs(sub));\n }\n buf[ptr++]=a[i];\n }\n return res;\n };\n return rec(rec,0,n);\n}\n\nCircle Incircle(const P& a,const P& b,const P& c){\n T A=abs(b-c),B=abs(c-a),C=abs(a-b);\n P p(A*a.X+B*b.X+C*c.X,A*a.Y+B*b.Y+C*c.Y);\n p/=(A+B+C);\n T r=Dist(Line(a,b),p);\n return Circle(p,r);\n}\nCircle Circumcircle(const P& a,const P& b,const P& c){\n Line l1((a+b)/2.,(a+b)/2.+(b-a)*P(0,1));\n Line l2((b+c)/2.,(b+c)/2.+(c-b)*P(0,1));\n P p=Intersection(l1,l2);\n return Circle(p,abs(p-a));\n}\nPoly tangent(const P& a,const Circle& b){\n return Intersection(b,Circle(a,sqrt(norm(b.p-a)-b.r*b.r)));\n}\nvector<Line> tangent(const Circle& a,const Circle& b){\n vector<Line> res;\n T d=abs(a.p-b.p);\n if(eq(d,0))return res;\n P u=unit(b.p-a.p);\n P v=u*P(0,1);\n for(int t:{-1,1}){\n T h=(a.r+b.r*t)/d;\n if(eq(h*h,1)){\n res.push_back(Line(a.p+(h>0?u:-u)*a.r,\n a.p+(h>0?u:-u)*a.r+v));\n }\n else if(1>h*h){\n P U=u*h,V=v*sqrt(1-h*h);\n res.push_back(Line(a.p+(U+V)*a.r,b.p-(U+V)*(b.r*t)));\n res.push_back(Line(a.p+(U-V)*a.r,b.p-(U-V)*(b.r*t)));\n }\n }\n return res;\n}\n\nvoid solve(){\n int n;\n cin>>n;\n if(n==0)exit(0);\n vector<Circle> in(n),out(n),all;\n rep(i,0,n){\n int x,y,r1,r2;\n cin>>x>>y>>r1>>r2;\n r2+=r1;\n in[i]=Circle(P(x,y),r1);\n out[i]=Circle(P(x,y),r2);\n all.push_back(in[i]);\n all.push_back(out[i]);\n }\n \n if(n==1){\n puts(\"1\");\n return;\n }\n int res=0;\n rep(i,0,n*2)rep(j,0,i){\n vector<Line> cand=tangent(all[i],all[j]);\n for(auto& l:cand){\n int sub=0;\n rep(x,0,n){\n if(Intersection(in[x],l).size()>1)continue;\n if(Intersection(out[x],l).size()<1)continue;\n sub++;\n }\n chmax(res,sub);\n }\n }\n cout<<res<<'\\n';\n}\n\nint main(){\n for(;;)solve();\n return 0;\n}", "accuracy": 1, "time_ms": 700, "memory_kb": 3516, "score_of_the_acc": -0.3938, "final_rank": 15 } ]
aoj_2207_cpp
Problem D: 無矛盾な単位系 京、垓、{禾予}、穣、溝、澗、正、載、極、恒河沙、阿僧祇、那由他、不可思議、無量大数 分、厘、毛、糸、忽、微、繊、沙、塵、埃、渺、漠、模糊、逡巡、須臾、瞬息、弾指、刹那、六徳、虚空、清浄、阿頼耶、阿摩羅、涅槃寂静 これらが何か分かるだろうか。これらは全て10の累乗につけられた数詞である。 京 = 10 16 , 垓 = 10 20 , {禾予} = 10 24 , 穣 = 10 28 , 溝 = 10 32 , ... 分 = 10 -1 、厘 = 10 -2 、毛 = 10 -3 、糸 = 10 -4 、忽 = 10 -5 , ... ではこれらが実用的に使われているのを見たことはあるだろうか?無いのではなかろうか? 過去の先人達が熟考の末にこれらの命名を行ったにもかかわらず、これらの数詞が日の目を見られないとはなんと勿体無いことだろう。 この問題は、コンテスト参加者の皆様にこれらの数詞を知ってもらうために作られたものである。以下に問題の概要を示す。 1 km = 10 3 m 上記のような単位間の関係が入力として与えられる。この問題では、左辺における単位が右辺における単位の10の累乗倍となっているような単位間の関係だけが与えられる。 単位間の関係が矛盾しているとは、与えられた関係から以下の関係が同時に成り立つことをいう。 1 A = 10 x B 1 A = 10 y B ただし、 x≠yであり、 AとBは任意の単位。 入力として与えられた単位間の関係が矛盾しているかどうか判定せよ。 なお、入力は簡易のため指数表記で与えられる。 Input 入力の一行目に、単位間の関係の個数を示す整数 N (1≤ N ≤ 100) が与えられる。 続くN行に、単位間の関係が含まれる。 単位間の関係は以下のフォーマットで与えられる。 "1 A = 10^x B" A, B は単位を示す。AとBは相異なっており、それぞれ空白を含まないアルファベット小文字1〜16文字からなる。x は -100から100までの整数である。 "1 A = 10^x B" が定義されたとき、 "1 B = 10^-x A" も暗黙のうちに定義されているものとする。 任意の単位系のペアが2回以上定義されることはない。 すなわち、"1 kilobyte = 10^3 byte", "1 kilobyte = 10^1 byte" のような関係が、同時に入力に含まれることはない。 同様に、"1 kilobyte = 10^3 byte", "1 byte = 10^-3 kilobyte" のような関係が、同時に入力に含まれることはない。 Output 与えられた単位系が矛盾していなければ Yes を、 矛盾していれば No を出力せよ。 Notes on Test Cases 上記入力形式で複数のデータセットが与えられます。各データセットに対して上記出力形式で出力を行うプログラムを作成して下さい。 N が 0 のとき入力の終わりを示します。 Sample Input 3 1 km = 10^3 m 1 m = 10^2 cm 1 km = 10^5 cm 7 1 kilometre = 10^3 metre 1 megametre = 10^3 kilometre 1 metre = 10^-6 megametre 1 terametre = 10^3 gigametre 1 petametre = 10^3 terametre 1 gigametre = 10^-6 petametre 1 metre = 10^-15 petametre 4 1 a = 10^2 b 1 a = 10^3 c 1 b = 10^2 c 1 c = 10^1 d 4 1 acm = 10^2 icpc 1 icpc = 10^3 utpc 1 utpc = 10^4 topcoder 1 topcoder = 10^-1 acm 0 Output for Sample Input Yes Yes No No
[ { "submission_id": "aoj_2207_8526509", "code_snippet": "// #define _GLIBCXX_DEBUG 1\n#include <bits/stdc++.h>\n// #include <atcoder/all>\nusing namespace std;\n#define ll long long\n#define ull unsigned long long\n\n#define rep1(i, n) for (int i = 1; i <= n; i++)\n#define rep_0(i, n) for (int i = 0; i < n; i++)\n#define rep_1(i, s, n) for (int i = s; i < n + s; i++)\n#define rep_2(i, s, t, n) for (int i = s; i <= n; i += t)\n#define rep_overload(e1, e2, e3, e4, f, ...) f\n#define rep(...) rep_overload(__VA_ARGS__, rep_2, rep_1, rep_0)(__VA_ARGS__)\n#define repr(i, n) for (int i = n - 1; i >= 0; i--)\n#define repr1(i, n) for (int i = n; i > 0; i--)\n\nauto chmax = [](auto& a, auto b) {bool ret=a<b; if(ret)a=b; return ret; };\nauto chmin = [](auto& a, auto b) {bool ret=a>b; if(ret)a=b; return ret; };\ntemplate <typename T>\nusing pq_inv = priority_queue<T, vector<T>, greater<T>>;\nvector<int> dd4 = {1, 0, -1, 0, 1}; // rep(i,4) nh=h+dd4[i]; nw=w+dd4[i+1];\nvector<int> dd8 = {1, 0, -1, 0, 1, 1, -1, -1, 1}; // rep(i,8) nh=h+dd8[i]; nw=w+dd8[i+1];\n// using mint = atcoder::modint1000000007;\n// using mint = atcoder::modint998244353;\n\ntemplate <typename T>\nstruct WeightedUnionFind {\n vector<int> par, rank;\n vector<T> diff_weight;\n WeightedUnionFind(int n) {\n init(n);\n }\n void init(int n) {\n par.assign(n, -1);\n rank.assign(n, 0);\n diff_weight.assign(n, 0);\n }\n int root(int x) {\n if (par[x] == -1) return x;\n int r = root(par[x]);\n diff_weight[x] += diff_weight[par[x]];\n return par[x] = r;\n }\n T weight(int x) {\n root(x);\n return diff_weight[x];\n }\n\n bool is_same(int x, int y) {\n return root(x) == root(y);\n }\n bool merge(int x, int y, T w) {\n w += weight(x) - weight(y);\n x = root(x);\n y = root(y);\n if (x == y) return false;\n if (rank[x] < rank[y]) swap(x, y), w = -w;\n if (rank[x] == rank[y]) rank[x]++;\n par[y] = x;\n diff_weight[y] = w;\n return true;\n }\n\n T diff(int x, int y) {\n return weight(y) - weight(x);\n }\n};\n\nvoid solve(int N) {\n map<string, int> mp;\n WeightedUnionFind<int> wuf(300);\n bool ok = true;\n rep(i, N) {\n string s;\n while (true) {\n getline(cin, s);\n if (s != \"\") break;\n };\n // parse number from string input wigh regex\n // 1 kilometer = 10^3 m\n regex re(R\"(\\d+ (.*?) = 10\\^(-?\\d+) (.*))\");\n smatch m;\n regex_search(s, m, re);\n if (mp.count(m[1]) == 0) mp[m[1]] = mp.size();\n if (mp.count(m[3]) == 0) mp[m[3]] = mp.size();\n if (wuf.is_same(mp[m[1]], mp[m[3]])) {\n if (wuf.diff(mp[m[1]], mp[m[3]]) != stoi(m[2])) ok = false;\n } else {\n wuf.merge(mp[m[1]], mp[m[3]], stoi(m[2]));\n }\n }\n cout << (ok ? \"Yes\" : \"No\") << endl;\n}\n\nint main() {\n std::cin.tie(nullptr);\n // int T;\n // // cin >> T;\n // T = 1;\n // for (int i = 0; i < T; i++) {\n // // solve(i);\n // solve();\n // }\n\n while (true) {\n int N;\n cin >> N;\n if (N == 0) break;\n solve(N);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 190, "memory_kb": 3452, "score_of_the_acc": -1.9001, "final_rank": 19 }, { "submission_id": "aoj_2207_5346628", "code_snippet": "#include <iostream>\n#include <vector>\n#include<iomanip>\n#include<functional>\n#include<algorithm>\n#include<deque>\n#include<math.h>\n#include<set>\n#include<cstdio>\n#include<string>\n#include<queue>\n#include<complex>\n#include<numeric>\n#include<stack>\n#include<unordered_map>\n#include<map>\n#include <cassert>\nusing namespace std;\n#define rep(i,n) for(int i = 0;i<n;i++)\n#define req(i,n) for(ll i = 1;i<=n;i++)\n#define rrep(i,n) for(int i = n-1;i>=0;i--)\n#define ALL(a) a.begin(),a.end()\ntemplate<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }\ntemplate<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }\ntypedef long long ll;\ntypedef long double ld;\ntemplate<class Abel> struct UnionFind {\n vector<int> par, rank;\n vector<Abel> diff_weight;\n UnionFind(int n = 1, Abel SUM_UNITY = 0) {\n init(n, SUM_UNITY);\n }\n void init(int n = 1, Abel SUM_UNITY = 0) {\n par.resize(n); rank.resize(n); diff_weight.resize(n);\n for (int i = 0; i < n; ++i) par[i] = i, rank[i] = 0, diff_weight[i] = SUM_UNITY;\n }\n int root(int x) {\n if (par[x] == x) {\n return x;\n }\n else {\n int r = root(par[x]);\n diff_weight[x] += diff_weight[par[x]];\n return par[x] = r;\n }\n }\n Abel weight(int x) {\n root(x);\n return diff_weight[x];\n }\n bool same(int x, int y) {\n return root(x) == root(y);\n }\n bool merge(int x, int y, Abel w) {\n w += weight(x); w -= weight(y);\n x = root(x); y = root(y);\n if (x == y) return false;\n if (rank[x] < rank[y]) swap(x, y), w = -w;\n if (rank[x] == rank[y]) ++rank[x];\n par[y] = x;\n diff_weight[y] = w;\n return true;\n }\n Abel diff(int x, int y) {\n return weight(y) - weight(x);\n }\n};\nint main() {\n int n, m;\n while (cin >> n,n) {\n bool f = 1; int id = 0,a,b; map<string, int> mp;\n UnionFind<int> uf(200);\n rep(i, n) {\n char c; string s, t, u, v;\n cin >> c >> s >> t >> u >> v;\n if (!mp.count(s)) mp[s] = id++;\n a = mp[s];\n if (!mp.count(v)) mp[v] = id++;\n b = mp[v]; int dif;\n stringstream si(u.substr(3));\n si >> dif;\n if (uf.same(a, b)) {\n int cur = uf.diff(a, b);\n if (dif != cur)f = 0;\n }\n else uf.merge(a, b, dif);\n }if (f) puts(\"Yes\");\n else puts(\"No\");\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3120, "score_of_the_acc": -0.3919, "final_rank": 6 }, { "submission_id": "aoj_2207_5015435", "code_snippet": "#line 2 \"/home/yuruhiya/programming/library/template/template.cpp\"\n#include <bits/stdc++.h>\n#line 6 \"/home/yuruhiya/programming/library/template/constants.cpp\"\nusing namespace std;\n\n#define rep(i, n) for (int i = 0; i < (n); ++i)\n#define FOR(i, m, n) for (int i = (m); i < (n); ++i)\n#define rrep(i, n) for (int i = (n)-1; i >= 0; --i)\n#define rfor(i, m, n) for (int i = (m); i >= (n); --i)\n#define unless(c) if (!(c))\n#define all(x) (x).begin(), (x).end()\n#define rall(x) (x).rbegin(), (x).rend()\n#define range_it(a, l, r) (a).begin() + (l), (a).begin() + (r)\n\nusing namespace std;\nusing ll = long long;\nusing LD = long double;\nusing VB = vector<bool>;\nusing VVB = vector<VB>;\nusing VI = vector<int>;\nusing VVI = vector<VI>;\nusing VL = vector<ll>;\nusing VVL = vector<VL>;\nusing VS = vector<string>;\nusing VD = vector<LD>;\nusing PII = pair<int, int>;\nusing VP = vector<PII>;\nusing PLL = pair<ll, ll>;\nusing VPL = vector<PLL>;\ntemplate <class T> using PQ = priority_queue<T>;\ntemplate <class T> using PQS = priority_queue<T, vector<T>, greater<T>>;\nconstexpr int inf = 1e9;\nconstexpr long long inf_ll = 1e18, MOD = 1000000007;\nconstexpr long double PI = 3.14159265358979323846, EPS = 1e-12;\n#line 7 \"/home/yuruhiya/programming/library/template/Input.cpp\"\nusing namespace std;\n\n#ifdef _WIN32\n#define getchar_unlocked _getchar_nolock\n#define putchar_unlocked _putchar_nolock\n#define fwrite_unlocked fwrite\n#define fflush_unlocked fflush\n#endif\nclass Scanner {\n\tstatic int gc() {\n\t\treturn getchar_unlocked();\n\t}\n\tstatic char next_char() {\n\t\tchar c;\n\t\tread(c);\n\t\treturn c;\n\t}\n\ttemplate <class T> static void read(T& v) {\n\t\tcin >> v;\n\t}\n\tstatic void read(char& v) {\n\t\twhile (isspace(v = gc()))\n\t\t\t;\n\t}\n\tstatic void read(bool& v) {\n\t\tv = next_char() != '0';\n\t}\n\tstatic void read(string& v) {\n\t\tv.clear();\n\t\tfor (char c = next_char(); !isspace(c); c = gc()) v += c;\n\t}\n\tstatic void read(int& v) {\n\t\tv = 0;\n\t\tbool neg = false;\n\t\tchar c = next_char();\n\t\tif (c == '-') {\n\t\t\tneg = true;\n\t\t\tc = gc();\n\t\t}\n\t\tfor (; isdigit(c); c = gc()) v = v * 10 + (c - '0');\n\t\tif (neg) v = -v;\n\t}\n\tstatic void read(long long& v) {\n\t\tv = 0;\n\t\tbool neg = false;\n\t\tchar c = next_char();\n\t\tif (c == '-') {\n\t\t\tneg = true;\n\t\t\tc = gc();\n\t\t}\n\t\tfor (; isdigit(c); c = gc()) v = v * 10 + (c - '0');\n\t\tif (neg) v = -v;\n\t}\n\tstatic void read(double& v) {\n\t\tv = 0;\n\t\tdouble dp = 1;\n\t\tbool neg = false, after_dp = false;\n\t\tchar c = next_char();\n\t\tif (c == '-') {\n\t\t\tneg = true;\n\t\t\tc = gc();\n\t\t}\n\t\tfor (; isdigit(c) || c == '.'; c = gc()) {\n\t\t\tif (c == '.') {\n\t\t\t\tafter_dp = true;\n\t\t\t} else if (after_dp) {\n\t\t\t\tv += (c - '0') * (dp *= 0.1);\n\t\t\t} else {\n\t\t\t\tv = v * 10 + (c - '0');\n\t\t\t}\n\t\t}\n\t\tif (neg) v = -v;\n\t}\n\tstatic void read(long double& v) {\n\t\tv = 0;\n\t\tlong double dp = 1;\n\t\tbool neg = false, after_dp = false;\n\t\tchar c = next_char();\n\t\tif (c == '-') {\n\t\t\tneg = true;\n\t\t\tc = gc();\n\t\t}\n\t\tfor (; isdigit(c) || c == '.'; c = gc()) {\n\t\t\tif (c == '.') {\n\t\t\t\tafter_dp = true;\n\t\t\t} else if (after_dp) {\n\t\t\t\tv += (c - '0') * (dp *= 0.1);\n\t\t\t} else {\n\t\t\t\tv = v * 10 + (c - '0');\n\t\t\t}\n\t\t}\n\t\tif (neg) v = -v;\n\t}\n\ttemplate <class T, class U> static void read(pair<T, U>& v) {\n\t\tread(v.first);\n\t\tread(v.second);\n\t}\n\ttemplate <class T> static void read(vector<T>& v) {\n\t\tfor (auto& e : v) read(e);\n\t}\n\ttemplate <size_t N = 0, class T> static void read_tuple_impl(T& v) {\n\t\tif constexpr (N < tuple_size_v<T>) {\n\t\t\tread(get<N>(v));\n\t\t\tread_tuple_impl<N + 1>(v);\n\t\t}\n\t}\n\ttemplate <class... T> static void read(tuple<T...>& v) {\n\t\tread_tuple_impl(v);\n\t}\n\tstruct ReadVectorHelper {\n\t\tsize_t n;\n\t\tReadVectorHelper(size_t _n) : n(_n) {}\n\t\ttemplate <class T> operator vector<T>() {\n\t\t\tvector<T> v(n);\n\t\t\tread(v);\n\t\t\treturn v;\n\t\t}\n\t};\n\tstruct Read2DVectorHelper {\n\t\tsize_t n, m;\n\t\tRead2DVectorHelper(const pair<size_t, size_t>& nm) : n(nm.first), m(nm.second) {}\n\t\ttemplate <class T> operator vector<vector<T>>() {\n\t\t\tvector<vector<T>> v(n, vector<T>(m));\n\t\t\tread(v);\n\t\t\treturn v;\n\t\t}\n\t};\n\npublic:\n\tstring read_line() const {\n\t\tstring v;\n\t\tfor (char c = next_char(); c != '\\n' && c != '\\0'; c = gc()) v += c;\n\t\treturn v;\n\t}\n\ttemplate <class T> T read() const {\n\t\tT v;\n\t\tread(v);\n\t\treturn v;\n\t}\n\ttemplate <class T> vector<T> read_vector(size_t n) const {\n\t\tvector<T> a(n);\n\t\tread(a);\n\t\treturn a;\n\t}\n\ttemplate <class T> operator T() const {\n\t\treturn read<T>();\n\t}\n\tint operator--(int) const {\n\t\treturn read<int>() - 1;\n\t}\n\tReadVectorHelper operator[](size_t n) const {\n\t\treturn ReadVectorHelper(n);\n\t}\n\tRead2DVectorHelper operator[](const pair<size_t, size_t>& nm) const {\n\t\treturn Read2DVectorHelper(nm);\n\t}\n\tvoid operator()() const {}\n\ttemplate <class H, class... T> void operator()(H&& h, T&&... t) const {\n\t\tread(h);\n\t\toperator()(forward<T>(t)...);\n\t}\n\nprivate:\n\ttemplate <template <class...> class, class...> struct Multiple;\n\ttemplate <template <class...> class V, class Head, class... Tail>\n\tstruct Multiple<V, Head, Tail...> {\n\t\ttemplate <class... Args> using vec = V<vector<Head>, Args...>;\n\t\tusing type = typename Multiple<vec, Tail...>::type;\n\t};\n\ttemplate <template <class...> class V> struct Multiple<V> { using type = V<>; };\n\ttemplate <class... T> using multiple_t = typename Multiple<tuple, T...>::type;\n\ttemplate <size_t N = 0, class T> void multiple_impl(T& t) const {\n\t\tif constexpr (N < tuple_size_v<T>) {\n\t\t\tauto& vec = get<N>(t);\n\t\t\tusing V = typename remove_reference_t<decltype(vec)>::value_type;\n\t\t\tvec.push_back(read<V>());\n\t\t\tmultiple_impl<N + 1>(t);\n\t\t}\n\t}\n\npublic:\n\ttemplate <class... T> auto multiple(size_t h) const {\n\t\tmultiple_t<T...> result;\n\t\twhile (h--) multiple_impl(result);\n\t\treturn result;\n\t}\n} in;\n#define inputs(T, ...) \\\n\tT __VA_ARGS__; \\\n\tin(__VA_ARGS__)\n#define ini(...) inputs(int, __VA_ARGS__)\n#define inl(...) inputs(long long, __VA_ARGS__)\n#define ins(...) inputs(string, __VA_ARGS__)\n#line 7 \"/home/yuruhiya/programming/library/template/Output.cpp\"\n#include <charconv>\n#line 10 \"/home/yuruhiya/programming/library/template/Output.cpp\"\nusing namespace std;\n\nstruct BoolStr {\n\tconst char *t, *f;\n\tBoolStr(const char* _t, const char* _f) : t(_t), f(_f) {}\n} Yes(\"Yes\", \"No\"), yes(\"yes\", \"no\"), YES(\"YES\", \"NO\"), Int(\"1\", \"0\");\nstruct DivStr {\n\tconst char *d, *l;\n\tDivStr(const char* _d, const char* _l) : d(_d), l(_l) {}\n} spc(\" \", \"\\n\"), no_spc(\"\", \"\\n\"), end_line(\"\\n\", \"\\n\"), comma(\",\", \"\\n\"),\n no_endl(\" \", \"\");\nclass Output {\n\tBoolStr B{Yes};\n\tDivStr D{spc};\n\npublic:\n\tvoid put(int v) const {\n\t\tchar buf[12]{};\n\t\tif (auto [ptr, e] = to_chars(begin(buf), end(buf), v); e == errc{}) {\n\t\t\tfwrite(buf, sizeof(char), ptr - buf, stdout);\n\t\t} else {\n\t\t\tassert(false);\n\t\t}\n\t}\n\tvoid put(long long v) const {\n\t\tchar buf[21]{};\n\t\tif (auto [ptr, e] = to_chars(begin(buf), end(buf), v); e == errc{}) {\n\t\t\tfwrite(buf, sizeof(char), ptr - buf, stdout);\n\t\t} else {\n\t\t\tassert(false);\n\t\t}\n\t}\n\tvoid put(bool v) const {\n\t\tput(v ? B.t : B.f);\n\t}\n\tvoid put(char v) const {\n\t\tputchar_unlocked(v);\n\t}\n\tvoid put(const char* v) const {\n\t\tfwrite_unlocked(v, 1, strlen(v), stdout);\n\t}\n\tvoid put(double v) const {\n\t\tprintf(\"%.20f\", v);\n\t}\n\tvoid put(long double v) const {\n\t\tprintf(\"%.20Lf\", v);\n\t}\n\ttemplate <class T> void put(const T& v) const {\n\t\tcout << v;\n\t}\n\ttemplate <class T, class U> void put(const pair<T, U>& v) const {\n\t\tput(v.first);\n\t\tput(D.d);\n\t\tput(v.second);\n\t}\n\ttemplate <class InputIterater>\n\tvoid put_range(const InputIterater& begin, const InputIterater& end) const {\n\t\tfor (InputIterater i = begin; i != end; ++i) {\n\t\t\tif (i != begin) put(D.d);\n\t\t\tput(*i);\n\t\t}\n\t}\n\ttemplate <class T> void put(const vector<T>& v) const {\n\t\tput_range(v.begin(), v.end());\n\t}\n\ttemplate <class T, size_t N> void put(const array<T, N>& v) const {\n\t\tput_range(v.begin(), v.end());\n\t}\n\ttemplate <class T> void put(const vector<vector<T>>& v) const {\n\t\tfor (size_t i = 0; i < v.size(); ++i) {\n\t\t\tif (i) put(D.l);\n\t\t\tput(v[i]);\n\t\t}\n\t}\n\n\tOutput() = default;\n\tOutput(const BoolStr& _boolstr, const DivStr& _divstr) : B(_boolstr), D(_divstr) {}\n\tOutput& operator()() {\n\t\tput(D.l);\n\t\treturn *this;\n\t}\n\ttemplate <class H> Output& operator()(H&& h) {\n\t\tput(h);\n\t\tput(D.l);\n\t\treturn *this;\n\t}\n\ttemplate <class H, class... T> Output& operator()(H&& h, T&&... t) {\n\t\tput(h);\n\t\tput(D.d);\n\t\treturn operator()(forward<T>(t)...);\n\t}\n\ttemplate <class InputIterator>\n\tOutput& range(const InputIterator& begin, const InputIterator& end) {\n\t\tput_range(begin, end);\n\t\tput(D.l);\n\t\treturn *this;\n\t}\n\ttemplate <class T> Output& range(const T& a) {\n\t\trange(a.begin(), a.end());\n\t\treturn *this;\n\t}\n\ttemplate <class... T> void exit(T&&... t) {\n\t\toperator()(forward<T>(t)...);\n\t\tstd::exit(EXIT_SUCCESS);\n\t}\n\tOutput& flush() {\n\t\tfflush_unlocked(stdout);\n\t\treturn *this;\n\t}\n\tOutput& set(const BoolStr& b) {\n\t\tB = b;\n\t\treturn *this;\n\t}\n\tOutput& set(const DivStr& d) {\n\t\tD = d;\n\t\treturn *this;\n\t}\n\tOutput& set(const char* t, const char* f) {\n\t\tB = BoolStr(t, f);\n\t\treturn *this;\n\t}\n} out;\n#line 3 \"/home/yuruhiya/programming/library/template/Step.cpp\"\nusing namespace std;\n\ntemplate <class T> struct Step {\n\tusing value_type = T;\n\n\tclass iterator {\n\t\tvalue_type a, b, c;\n\n\tpublic:\n\t\tconstexpr iterator() : a(value_type()), b(value_type()), c(value_type()) {}\n\t\tconstexpr iterator(value_type _b, value_type _c, value_type _s)\n\t\t : a(_b), b(_c), c(_s) {}\n\t\tconstexpr iterator& operator++() {\n\t\t\t--b;\n\t\t\ta += c;\n\t\t\treturn *this;\n\t\t}\n\t\tconstexpr iterator operator++(int) {\n\t\t\titerator tmp = *this;\n\t\t\t--b;\n\t\t\ta += c;\n\t\t\treturn tmp;\n\t\t}\n\t\tconstexpr const value_type& operator*() const {\n\t\t\treturn a;\n\t\t}\n\t\tconstexpr const value_type* operator->() const {\n\t\t\treturn &a;\n\t\t}\n\t\tconstexpr bool operator==(const iterator& i) const {\n\t\t\treturn b == i.b;\n\t\t}\n\t\tconstexpr bool operator!=(const iterator& i) const {\n\t\t\treturn !(b == i.b);\n\t\t}\n\t\tconstexpr value_type start() const {\n\t\t\treturn a;\n\t\t}\n\t\tconstexpr value_type size() const {\n\t\t\treturn b;\n\t\t}\n\t\tconstexpr value_type step() const {\n\t\t\treturn c;\n\t\t}\n\t};\n\tconstexpr Step(value_type b, value_type c, value_type s) : be(b, c, s) {}\n\tconstexpr iterator begin() const {\n\t\treturn be;\n\t}\n\tconstexpr iterator end() const {\n\t\treturn en;\n\t}\n\tconstexpr value_type start() const {\n\t\treturn be.start();\n\t}\n\tconstexpr value_type size() const {\n\t\treturn be.size();\n\t}\n\tconstexpr value_type step() const {\n\t\treturn be.step();\n\t}\n\tconstexpr value_type sum() const {\n\t\treturn start() * size() + step() * (size() * (size() - 1) / 2);\n\t}\n\toperator vector<value_type>() const {\n\t\treturn to_a();\n\t}\n\tauto to_a() const {\n\t\tvector<value_type> result;\n\t\tresult.reserve(size());\n\t\tfor (auto i : *this) {\n\t\t\tresult.push_back(i);\n\t\t}\n\t\treturn result;\n\t}\n\nprivate:\n\titerator be, en;\n};\ntemplate <class T> constexpr auto step(T a) {\n\treturn Step<T>(0, a, 1);\n}\ntemplate <class T> constexpr auto step(T a, T b) {\n\treturn Step<T>(a, b - a, 1);\n}\ntemplate <class T> constexpr auto step(T a, T b, T c) {\n\treturn Step<T>(a, a < b ? (b - a - 1) / c + 1 : 0, c);\n}\n#line 8 \"/home/yuruhiya/programming/library/template/Ruby.cpp\"\nusing namespace std;\n\ntemplate <class F> struct Callable {\n\tF func;\n\tCallable(const F& f) : func(f) {}\n};\ntemplate <class T, class F> auto operator|(const T& v, const Callable<F>& c) {\n\treturn c.func(v);\n}\n\nstruct Sort_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\tsort(begin(v), end(v), f);\n\t\t\treturn v;\n\t\t});\n\t}\n\ttemplate <class T> friend auto operator|(T v, [[maybe_unused]] const Sort_impl& c) {\n\t\tsort(begin(v), end(v));\n\t\treturn v;\n\t}\n} Sort;\nstruct SortBy_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\tsort(begin(v), end(v),\n\t\t\t [&](const auto& i, const auto& j) { return f(i) < f(j); });\n\t\t\treturn v;\n\t\t});\n\t}\n} SortBy;\nstruct RSort_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\tsort(rbegin(v), rend(v), f);\n\t\t\treturn v;\n\t\t});\n\t}\n\ttemplate <class T> friend auto operator|(T v, [[maybe_unused]] const RSort_impl& c) {\n\t\tsort(rbegin(v), rend(v));\n\t\treturn v;\n\t}\n} RSort;\nstruct RSortBy_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\tsort(begin(v), end(v),\n\t\t\t [&](const auto& i, const auto& j) { return f(i) > f(j); });\n\t\t\treturn v;\n\t\t});\n\t}\n} RSortBy;\nstruct Reverse_impl {\n\ttemplate <class T> friend auto operator|(T v, const Reverse_impl& c) {\n\t\treverse(begin(v), end(v));\n\t\treturn v;\n\t}\n} Reverse;\nstruct Unique_impl {\n\ttemplate <class T> friend auto operator|(T v, const Unique_impl& c) {\n\t\tv.erase(unique(begin(v), end(v), end(v)));\n\t\treturn v;\n\t}\n} Unique;\nstruct Uniq_impl {\n\ttemplate <class T> friend auto operator|(T v, const Uniq_impl& c) {\n\t\tsort(begin(v), end(v));\n\t\tv.erase(unique(begin(v), end(v)), end(v));\n\t\treturn v;\n\t}\n} Uniq;\nstruct Rotate_impl {\n\tauto operator()(int&& left) {\n\t\treturn Callable([&](auto v) {\n\t\t\tint s = static_cast<int>(size(v));\n\t\t\tassert(-s <= left && left <= s);\n\t\t\tif (0 <= left) {\n\t\t\t\trotate(begin(v), begin(v) + left, end(v));\n\t\t\t} else {\n\t\t\t\trotate(begin(v), end(v) + left, end(v));\n\t\t\t}\n\t\t\treturn v;\n\t\t});\n\t}\n} Rotate;\nstruct Max_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) { return *max_element(begin(v), end(v), f); });\n\t}\n\ttemplate <class T> friend auto operator|(T v, const Max_impl& c) {\n\t\treturn *max_element(begin(v), end(v));\n\t}\n} Max;\nstruct Min_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) { return *min_element(begin(v), end(v), f); });\n\t}\n\ttemplate <class T> friend auto operator|(T v, const Min_impl& c) {\n\t\treturn *min_element(begin(v), end(v));\n\t}\n} Min;\nstruct MaxPos_impl {\n\ttemplate <class T> friend auto operator|(T v, const MaxPos_impl& c) {\n\t\treturn max_element(begin(v), end(v)) - begin(v);\n\t}\n} MaxPos;\nstruct MinPos_impl {\n\ttemplate <class T> friend auto operator|(T v, const MinPos_impl& c) {\n\t\treturn min_element(begin(v), end(v)) - begin(v);\n\t}\n} MinPos;\nstruct MaxBy_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\tauto max_it = begin(v);\n\t\t\tauto max_val = f(*max_it);\n\t\t\tfor (auto it = next(begin(v)); it != end(v); ++it) {\n\t\t\t\tif (auto val = f(*it); max_val < val) {\n\t\t\t\t\tmax_it = it;\n\t\t\t\t\tmax_val = val;\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn *max_it;\n\t\t});\n\t}\n} MaxBy;\nstruct MinBy_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\tauto min_it = begin(v);\n\t\t\tauto min_val = f(*min_it);\n\t\t\tfor (auto it = next(begin(v)); it != end(v); ++it) {\n\t\t\t\tif (auto val = f(*it); min_val > val) {\n\t\t\t\t\tmin_it = it;\n\t\t\t\t\tmin_val = val;\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn *min_it;\n\t\t});\n\t}\n} MinBy;\nstruct MaxOf_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\tauto max_val = f(*begin(v));\n\t\t\tfor (auto it = next(begin(v)); it != end(v); ++it) {\n\t\t\t\tif (auto val = f(*it); max_val < val) {\n\t\t\t\t\tmax_val = val;\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn max_val;\n\t\t});\n\t}\n} MaxOf;\nstruct MinOf_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\tauto min_val = f(*begin(v));\n\t\t\tfor (auto it = next(begin(v)); it != end(v); ++it) {\n\t\t\t\tif (auto val = f(*it); min_val > val) {\n\t\t\t\t\tmin_val = val;\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn min_val;\n\t\t});\n\t}\n} MinOf;\nstruct Count_impl {\n\ttemplate <class V> auto operator()(const V& val) {\n\t\treturn Callable([&](auto v) { return count(begin(v), end(v), val); });\n\t}\n} Count;\nstruct CountIf_impl {\n\ttemplate <class F> auto operator()(const F& f) {\n\t\treturn Callable([&](auto v) { return count_if(begin(v), end(v), f); });\n\t}\n} CountIf;\nstruct Index_impl {\n\ttemplate <class V> auto operator()(const V& val) {\n\t\treturn Callable([&](auto v) -> optional<int> {\n\t\t\tauto result = find(begin(v), end(v), val);\n\t\t\treturn result != end(v) ? optional(result - begin(v)) : nullopt;\n\t\t});\n\t}\n} Index;\nstruct IndexIf_impl {\n\ttemplate <class F> auto operator()(const F& f) {\n\t\treturn Callable([&](auto v) -> optional<int> {\n\t\t\tauto result = find_if(begin(v), end(v), f);\n\t\t\treturn result != end(v) ? optional(result - begin(v)) : nullopt;\n\t\t});\n\t}\n} IndexIf;\nstruct FindIf_impl {\n\ttemplate <class F> auto operator()(const F& f) {\n\t\treturn Callable([&](auto v) -> optional<typename decltype(v)::value_type> {\n\t\t\tauto result = find_if(begin(v), end(v), f);\n\t\t\treturn result != end(v) ? optional(*result) : nullopt;\n\t\t});\n\t}\n} FindIf;\nstruct Sum_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\treturn accumulate(next(begin(v)), end(v), f(*begin(v)),\n\t\t\t [&](const auto& a, const auto& b) { return a + f(b); });\n\t\t});\n\t}\n\ttemplate <class T> friend auto operator|(T v, const Sum_impl& c) {\n\t\treturn accumulate(begin(v), end(v), typename T::value_type{});\n\t}\n} Sum;\nstruct Includes {\n\ttemplate <class V> auto operator()(const V& val) {\n\t\treturn Callable([&](auto v) { return find(begin(v), end(v), val) != end(v); });\n\t}\n} Includes;\nstruct IncludesIf_impl {\n\ttemplate <class F> auto operator()(const F& f) {\n\t\treturn Callable([&](auto v) { return find_if(begin(v), end(v), f) != end(v); });\n\t}\n} IncludesIf;\nstruct RemoveIf_impl {\n\ttemplate <class F> auto operator()(const F& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\tv.erase(remove_if(begin(v), end(v), f), end(v));\n\t\t\treturn v;\n\t\t});\n\t}\n} RemoveIf;\nstruct Each_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\tfor (const auto& i : v) {\n\t\t\t\tf(i);\n\t\t\t}\n\t\t});\n\t}\n} Each;\nstruct Select_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\tusing value_type = typename decltype(v)::value_type;\n\t\t\tvector<value_type> result;\n\t\t\tfor (const auto& i : v) {\n\t\t\t\tif (f(i)) result.push_back(i);\n\t\t\t}\n\t\t\treturn result;\n\t\t});\n\t}\n} Select;\nstruct Map_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\tusing result_type = invoke_result_t<F, typename decltype(v)::value_type>;\n\t\t\tvector<result_type> result;\n\t\t\tresult.reserve(size(v));\n\t\t\tfor (const auto& i : v) {\n\t\t\t\tresult.push_back(f(i));\n\t\t\t}\n\t\t\treturn result;\n\t\t});\n\t}\n} Map;\nstruct Indexed_impl {\n\ttemplate <class T> friend auto operator|(const T& v, Indexed_impl& c) {\n\t\tusing value_type = typename T::value_type;\n\t\tvector<pair<value_type, int>> result;\n\t\tresult.reserve(size(v));\n\t\tint index = 0;\n\t\tfor (const auto& i : v) {\n\t\t\tresult.emplace_back(i, index++);\n\t\t}\n\t\treturn result;\n\t}\n} Indexed;\nstruct AllOf_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\tfor (const auto& i : v) {\n\t\t\t\tif (!f(i)) return false;\n\t\t\t}\n\t\t\treturn true;\n\t\t});\n\t}\n} AllOf;\nstruct AnyOf_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\tfor (const auto& i : v) {\n\t\t\t\tif (f(i)) return true;\n\t\t\t}\n\t\t\treturn false;\n\t\t});\n\t}\n} AnyOf;\nstruct NoneOf_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\tfor (const auto& i : v) {\n\t\t\t\tif (f(i)) return false;\n\t\t\t}\n\t\t\treturn true;\n\t\t});\n\t}\n} NoneOf;\n\nstruct Tally_impl {\n\ttemplate <class F> auto operator()(size_t max_val) {\n\t\treturn Callable([&](auto v) {\n\t\t\tvector<size_t> result(max_val);\n\t\t\tfor (const auto& i : v) {\n\t\t\t\tresult[static_cast<size_t>(i)]++;\n\t\t\t}\n\t\t\treturn result;\n\t\t});\n\t}\n\ttemplate <class T, class value_type = typename T::value_type>\n\tfriend auto operator|(const T& v, Tally_impl& c) {\n\t\tmap<value_type, size_t> result;\n\t\tfor (const auto& i : v) {\n\t\t\tresult[i]++;\n\t\t}\n\t\treturn result;\n\t}\n} Tally;\n\ntemplate <class T> auto operator*(const vector<T>& a, size_t n) {\n\tT result;\n\tfor (size_t i = 0; i < n; ++i) {\n\t\tresult.insert(result.end(), a.begin(), a.end());\n\t}\n\treturn result;\n}\nauto operator*(string a, size_t n) {\n\tstring result;\n\tfor (size_t i = 0; i < n; ++i) {\n\t\tresult += a;\n\t}\n\treturn result;\n}\ntemplate <class T, class U> auto& operator<<(vector<T>& a, const U& b) {\n\ta.insert(a.end(), all(b));\n\treturn a;\n}\ntemplate <class T> auto& operator<<(string& a, const T& b) {\n\ta.insert(a.end(), all(b));\n\treturn a;\n}\ntemplate <class T, class U> auto operator+(vector<T> a, const U& b) {\n\ta << b;\n\treturn a;\n}\ntemplate <class T> auto operator+(string a, const T& b) {\n\ta << b;\n\treturn a;\n}\n#line 6 \"/home/yuruhiya/programming/library/template/functions.cpp\"\nusing namespace std;\n\ntemplate <class T> int sz(const T& v) {\n\treturn v.size();\n}\ntemplate <class T, class U> int lower_index(const T& a, const U& v) {\n\treturn lower_bound(all(a), v) - a.begin();\n}\ntemplate <class T, class U> int upper_index(const T& a, const U& v) {\n\treturn upper_bound(all(a), v) - a.begin();\n}\ntemplate <class T> auto Slice(const T& v, size_t i, size_t len) {\n\treturn i < v.size() ? T(v.begin() + i, v.begin() + min(i + len, v.size())) : T();\n}\ntemplate <class T> T div_ceil(T n, T m) {\n\treturn (n + m - 1) / m;\n}\ntemplate <class T> T div_ceil2(T n, T m) {\n\treturn div_ceil(n, m) * m;\n}\ntemplate <class T> T triangle(T n) {\n\treturn (n & 1) ? (n + 1) / 2 * n : n / 2 * (n + 1);\n}\ntemplate <class T> T nC2(T n) {\n\treturn (n & 1) ? (n - 1) / 2 * n : n / 2 * (n - 1);\n}\ntemplate <class T> T middle(const T& l, const T& r) {\n\treturn l + (r - l) / 2;\n}\ntemplate <class T> bool chmax(T& a, const T& b) {\n\tif (a < b) {\n\t\ta = b;\n\t\treturn true;\n\t}\n\treturn false;\n}\ntemplate <class T> bool chmin(T& a, const T& b) {\n\tif (a > b) {\n\t\ta = b;\n\t\treturn true;\n\t}\n\treturn false;\n}\ntemplate <class T> bool in_range(const T& v, const T& min, const T& max) {\n\treturn min <= v && v < max;\n}\ntemplate <class T> bool in_square(T n) {\n\tT s = sqrt(n);\n\treturn s * s == n || (s + 1) * (s + 1) == n;\n}\ntemplate <class T = long long> T BIT(int b) {\n\treturn T(1) << b;\n}\ntemplate <class T, class U = typename T::value_type> U Gcdv(const T& v) {\n\treturn accumulate(next(v.begin()), v.end(), U(*v.begin()), gcd<U, U>);\n}\ntemplate <class T, class U = typename T::value_type> U Lcmv(const T& v) {\n\treturn accumulate(next(v.begin()), v.end(), U(*v.begin()), lcm<U, U>);\n}\ntemplate <class T, class U> T Pow(T a, U n) {\n\tT result = 1;\n\twhile (n > 0) {\n\t\tif (n & 1) {\n\t\t\tresult *= a;\n\t\t\tn--;\n\t\t} else {\n\t\t\ta *= a;\n\t\t\tn >>= 1;\n\t\t}\n\t}\n\treturn result;\n}\ntemplate <class T, class U> T Powmod(T a, U n, T mod) {\n\tT result = 1;\n\twhile (n > 0) {\n\t\tif (n & 1) {\n\t\t\tresult = result * a % mod;\n\t\t\tn--;\n\t\t} else {\n\t\t\ta = a * a % mod;\n\t\t\tn >>= 1;\n\t\t}\n\t}\n\treturn result;\n}\nnamespace internal {\n\ttemplate <class T, size_t N> auto make_vector(vector<int>& sizes, const T& init) {\n\t\tif constexpr (N == 1) {\n\t\t\treturn vector(sizes[0], init);\n\t\t} else {\n\t\t\tint size = sizes[N - 1];\n\t\t\tsizes.pop_back();\n\t\t\treturn vector(size, make_vector<T, N - 1>(sizes, init));\n\t\t}\n\t}\n} // namespace internal\ntemplate <class T, size_t N>\nauto make_vector(const int (&sizes)[N], const T& init = T()) {\n\tvector s(rbegin(sizes), rend(sizes));\n\treturn internal::make_vector<T, N>(s, init);\n}\n#line 9 \"/home/yuruhiya/programming/library/template/template.cpp\"\n#if __has_include(<library/dump.hpp>)\n#include <library/dump.hpp>\n#define LOCAL\n#else\n#define dump(...) ((void)0)\n#endif\n\ntemplate <class T> constexpr T oj_local(const T& oj, const T& local) {\n#ifndef LOCAL\n\treturn oj;\n#else\n\treturn local;\n#endif\n}\n#line 4 \"/home/yuruhiya/programming/library/DataStructure/WeightedUnionFind.cpp\"\nusing namespace std;\n\ntemplate <class T> class WeightedUnionFind {\n\tvector<int> par_m, rank_m;\n\tvector<T> weight_m;\n\npublic:\n\tWeightedUnionFind(int n) : par_m(n), rank_m(n, 0), weight_m(n, 0) {\n\t\tfor (int i = 0; i < n; ++i) par_m[i] = i;\n\t}\n\tint root(int x) {\n\t\tif (par_m[x] == x) {\n\t\t\treturn x;\n\t\t} else {\n\t\t\tint r = root(par_m[x]);\n\t\t\tweight_m[x] += weight_m[par_m[x]];\n\t\t\treturn par_m[x] = r;\n\t\t}\n\t}\n\tT weight(int x) {\n\t\troot(x);\n\t\treturn weight_m[x];\n\t}\n\tbool same(int x, int y) {\n\t\treturn root(x) == root(y);\n\t}\n\tbool merge(int x, int y, T w) {\n\t\tw += weight(x);\n\t\tw -= weight(y);\n\t\tx = root(x);\n\t\ty = root(y);\n\t\tif (x == y) return false;\n\t\tif (rank_m[x] < rank_m[y]) {\n\t\t\tswap(x, y);\n\t\t\tw = -w;\n\t\t}\n\t\tif (rank_m[x] == rank_m[y]) rank_m[x]++;\n\t\tpar_m[y] = x;\n\t\tweight_m[y] = w;\n\t\treturn true;\n\t}\n\tT diff(int x, int y) {\n\t\treturn weight(y) - weight(x);\n\t}\n};\n#line 3 \"a.cpp\"\n\nbool solve(int n) {\n\tVS str = step(n) | Map([&](int i) { return in.read_line(); });\n\tdump(str);\n\n\tint k = 0;\n\tmap<string, int> index;\n\tWeightedUnionFind<int> uf(200);\n\n\tfor (const string& s : str) {\n\t\tsmatch result;\n\t\tregex_search(s, result, regex(\"1 (\\\\w+) = 10\\\\^([\\\\-\\\\d]+) (\\\\w+)\"));\n\t\tstring s1 = result[1].str(), s2 = result[3].str();\n\t\tint i1 = index.count(s1) ? index[s1] : index[s1] = k++;\n\t\tint i2 = index.count(s2) ? index[s2] : index[s2] = k++;\n\t\tdump(result[2].str());\n\t\tint val = stoi(result[2].str());\n\t\tif (uf.same(i1, i2)) {\n\t\t\tif (uf.diff(i1, i2) != val) {\n\t\t\t\treturn false;\n\t\t\t}\n\t\t} else {\n\t\t\tuf.merge(i1, i2, val);\n\t\t}\n\t}\n\treturn true;\n}\n\nint main() {\n\tfor (int n = in; n; n = in) {\n\t\tout(solve(n));\n\t}\n}", "accuracy": 1, "time_ms": 200, "memory_kb": 3480, "score_of_the_acc": -2, "final_rank": 20 }, { "submission_id": "aoj_2207_4631181", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntemplate<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }\ntemplate<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }\nusing ll = long long;\nusing P = pair<ll, ll>;\nll GCD(ll a, ll b) { return b?GCD(b, a%b):a; }\nll LCM(ll a, ll b) { return a/GCD(a, b)*b; }\n\ntemplate<class Abel> struct UnionFind {\n vector<int> par;\n vector<int> rank;\n vector<Abel> diff_weight;\n\n UnionFind(int n = 1, Abel SUM_UNITY = 0) {\n init(n, SUM_UNITY);\n }\n\n void init(int n = 1, Abel SUM_UNITY = 0) {\n par.resize(n); rank.resize(n); diff_weight.resize(n);\n for(int i = 0; i < n; ++i) {\n par[i] = i; rank[i] = 0; diff_weight[i] = SUM_UNITY;\n }\n }\n\n int find(int x) {\n if(par[x] == x) return x;\n else {\n int r = find(par[x]);\n diff_weight[x] += diff_weight[par[x]]; // 累積和を取る\n return par[x] = r;\n }\n }\n\n Abel weight(int x) {\n find(x);\n return diff_weight[x];\n }\n\n bool same(int x, int y) {\n return find(x) == find(y);\n }\n\n bool unite(int x, int y, Abel w) {\n w += weight(x); w -= weight(y); // xとyそれぞれについて、rootとの重み差分を補正\n x = find(x); y = find(y);\n if(x == y) return false;\n if(rank[x] < rank[y]) swap(x, y), w = -w; // rank[x] >= rank[y]になるようにswap\n if(rank[x] == rank[y]) ++rank[x];\n par[y] = x;\n diff_weight[y] = w; // xがyの親になるので、xとyの差分をdiff_weight[y]に記録\n return true;\n }\n\n // 重みを取得する\n Abel diff(int x, int y) {\n return weight(y) - weight(x);\n }\n};\n\nint n;\n\nint xcalc(string z) {\n int zlen = z.length();\n int zfin = zlen - 3;\n string subz = z.substr(3, zfin);\n int ret = 0;\n if(subz.at(0) == '-') {\n subz.at(0) = '0';\n ret = stoi(subz);\n ret = -ret;\n }else {\n ret = stoi(subz);\n }\n return ret;\n}\n\nint main() {\n while(1) {\n cin >> n;\n if(n == 0) break;\n UnionFind<ll> uf(200);\n bool flg = true;\n map<string, int> mdata;\n int cnt = 0;\n for(int i = 0; i < n; ++i) {\n int one;\n string A, eq, x, B;\n cin >> one >> A >> eq >> x >> B;\n if(mdata[A] == 0) {\n mdata[A] = cnt+1; cnt++;\n }\n if(mdata[B] == 0) {\n mdata[B] = cnt+1; cnt++;\n }\n\n int ch = xcalc(x);\n if(uf.same(mdata[A]-1, mdata[B]-1)) {\n // もし繋がっていたら\n if(uf.diff(mdata[A]-1, mdata[B]-1) != ch) flg = false;\n }else {\n // 繋がっていなかったら\n uf.unite(mdata[A]-1, mdata[B]-1, ch);\n }\n }\n\n if(flg) cout << \"Yes\" << endl;\n else cout << \"No\" << endl;\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3116, "score_of_the_acc": -0.3851, "final_rank": 5 }, { "submission_id": "aoj_2207_4290850", "code_snippet": "#include <iostream>\n#include <sstream>\n#include <string>\n#include <vector>\n#include <cstdio>\n#include <map>\nusing namespace std;\n\ntemplate<class Abel> struct UnionFind {\n vector<int> par;\n vector<int> rank;\n vector<Abel> diff_weight;\n\n UnionFind(int n = 1, Abel SUM_UNITY = 0) {\n init(n, SUM_UNITY);\n }\n\n void init(int n = 1, Abel SUM_UNITY = 0) {\n par.resize(n); rank.resize(n); diff_weight.resize(n);\n for (int i = 0; i < n; ++i) par[i] = i, rank[i] = 0, diff_weight[i] = SUM_UNITY;\n }\n\n int root(int x) {\n if (par[x] == x) {\n return x;\n }\n else {\n int r = root(par[x]);\n diff_weight[x] += diff_weight[par[x]];\n return par[x] = r;\n }\n }\n\n Abel weight(int x) {\n root(x);\n return diff_weight[x];\n }\n\n bool issame(int x, int y) {\n return root(x) == root(y);\n }\n\n bool merge(int x, int y, Abel w) {\n w += weight(x); w -= weight(y);\n x = root(x); y = root(y);\n if (x == y) return false;\n if (rank[x] < rank[y]) swap(x, y), w = -w;\n if (rank[x] == rank[y]) ++rank[x];\n par[y] = x;\n diff_weight[y] = w;\n return true;\n }\n\n Abel diff(int x, int y) {\n return weight(y) - weight(x);\n }\n};\n\nvector<string> split_native(const string &s, char delim) {\n vector<string> elems;\n string item;\n for (char ch: s) {\n if (ch == delim) {\n if (!item.empty())\n elems.push_back(item);\n item.clear();\n }\n else {\n item += ch;\n }\n }\n if (!item.empty())\n elems.push_back(item);\n return elems;\n}\n\nint main(){\n int N;\n\n while(cin >> N){\n if(N ==0) break;\n\n UnionFind<int> UF(200);\n map<string, int> m;\n int idx = 0;\n bool flag = true;\n\n for(int i=0; i<N; ++i){\n string one, unit1, equal, val, unit2;\n cin >> one >> unit1 >> equal >> val >> unit2;\n if(m.count(unit1) == 0){\n m[unit1] = idx;\n ++idx;\n }\n int id1 = m[unit1];\n\n if(m.count(unit2) == 0){\n m[unit2] = idx;\n ++idx;\n }\n int id2 = m[unit2]; \n\n int w;\n vector<string> vec;\n vec = split_native(val, '^');\n w = stoi(vec[1]);\n\n if(UF.issame(id1, id2)){\n int diff = UF.diff(id1, id2);\n if(diff != w) flag = false;\n }else{\n UF.merge(id1, id2, w);\n } \n }\n if(flag){\n puts(\"Yes\");\n }else{\n puts(\"No\");\n }\n } \n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3192, "score_of_the_acc": -0.5135, "final_rank": 13 }, { "submission_id": "aoj_2207_4290849", "code_snippet": "#include <iostream>\n#include <sstream>\n#include <string>\n#include <vector>\n#include <cstdio>\n#include <map>\nusing namespace std;\n\ntemplate<class Abel> struct UnionFind {\n vector<int> par;\n vector<int> rank;\n vector<Abel> diff_weight;\n\n UnionFind(int n = 1, Abel SUM_UNITY = 0) {\n init(n, SUM_UNITY);\n }\n\n void init(int n = 1, Abel SUM_UNITY = 0) {\n par.resize(n); rank.resize(n); diff_weight.resize(n);\n for (int i = 0; i < n; ++i) par[i] = i, rank[i] = 0, diff_weight[i] = SUM_UNITY;\n }\n\n int root(int x) {\n if (par[x] == x) {\n return x;\n }\n else {\n int r = root(par[x]);\n diff_weight[x] += diff_weight[par[x]];\n return par[x] = r;\n }\n }\n\n Abel weight(int x) {\n root(x);\n return diff_weight[x];\n }\n\n bool issame(int x, int y) {\n return root(x) == root(y);\n }\n\n bool merge(int x, int y, Abel w) {\n w += weight(x); w -= weight(y);\n x = root(x); y = root(y);\n if (x == y) return false;\n if (rank[x] < rank[y]) swap(x, y), w = -w;\n if (rank[x] == rank[y]) ++rank[x];\n par[y] = x;\n diff_weight[y] = w;\n return true;\n }\n\n Abel diff(int x, int y) {\n return weight(y) - weight(x);\n }\n};\n\n// vector<string> split_native(const string &s, char delim) {\n// vector<string> elems;\n// string item;\n// for (char ch: s) {\n// if (ch == delim) {\n// if (!item.empty())\n// elems.push_back(item);\n// item.clear();\n// }\n// else {\n// item += ch;\n// }\n// }\n// if (!item.empty())\n// elems.push_back(item);\n// return elems;\n// }\n\nint main(){\n int N;\n\n while(cin >> N){\n if(N ==0) break;\n\n UnionFind<int> UF(200);\n map<string, int> m;\n int idx = 0;\n bool flag = true;\n\n for(int i=0; i<N; ++i){\n string one, unit1, equal, val, unit2;\n cin >> one >> unit1 >> equal >> val >> unit2;\n if(m.count(unit1) == 0){\n m[unit1] = idx;\n ++idx;\n }\n int id1 = m[unit1];\n\n if(m.count(unit2) == 0){\n m[unit2] = idx;\n ++idx;\n }\n int id2 = m[unit2]; \n\n // int w;\n // vector<string> vec;\n // vec = split_native(val, '^');\n // w = stoi(vec[1]);\n\n // 単位換算の倍率の取得\n stringstream si(val.substr(3));\n int w;\n si >> w; // \"1 kilometre = 10^3 metre\" の 3 の部分を取得\n\n\n if(UF.issame(id1, id2)){\n int diff = UF.diff(id1, id2);\n if(diff != w) flag = false;\n }else{\n UF.merge(id1, id2, w);\n } \n }\n if(flag){\n puts(\"Yes\");\n }else{\n puts(\"No\");\n }\n } \n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3132, "score_of_the_acc": -0.4122, "final_rank": 8 }, { "submission_id": "aoj_2207_4290847", "code_snippet": "#include <iostream>\n#include <sstream>\n#include <string>\n#include <vector>\n#include <cstdio>\n#include <map>\nusing namespace std;\n\ntemplate<class Abel> struct UnionFind {\n vector<int> par;\n vector<int> rank;\n vector<Abel> diff_weight;\n\n UnionFind(int n = 1, Abel SUM_UNITY = 0) {\n init(n, SUM_UNITY);\n }\n\n void init(int n = 1, Abel SUM_UNITY = 0) {\n par.resize(n); rank.resize(n); diff_weight.resize(n);\n for (int i = 0; i < n; ++i) par[i] = i, rank[i] = 0, diff_weight[i] = SUM_UNITY;\n }\n\n int root(int x) {\n if (par[x] == x) {\n return x;\n }\n else {\n int r = root(par[x]);\n diff_weight[x] += diff_weight[par[x]];\n return par[x] = r;\n }\n }\n\n Abel weight(int x) {\n root(x);\n return diff_weight[x];\n }\n\n bool issame(int x, int y) {\n return root(x) == root(y);\n }\n\n bool merge(int x, int y, Abel w) {\n w += weight(x); w -= weight(y);\n x = root(x); y = root(y);\n if (x == y) return false;\n if (rank[x] < rank[y]) swap(x, y), w = -w;\n if (rank[x] == rank[y]) ++rank[x];\n par[y] = x;\n diff_weight[y] = w;\n return true;\n }\n\n Abel diff(int x, int y) {\n return weight(y) - weight(x);\n }\n};\n\n// vector<string> split_native(const string &s, char delim) {\n// vector<string> elems;\n// string item;\n// for (char ch: s) {\n// if (ch == delim) {\n// if (!item.empty())\n// elems.push_back(item);\n// item.clear();\n// }\n// else {\n// item += ch;\n// }\n// }\n// if (!item.empty())\n// elems.push_back(item);\n// return elems;\n// }\n\nint main(){\n int N;\n\n while(cin >> N){\n if(N ==0) break;\n\n UnionFind<int> UF(200);\n map<string, int> m;\n int idx = 0;\n bool flag = true;\n\n for(int i=0; i<N; ++i){\n string one, unit1, equal, val, unit2;\n cin >> one >> unit1 >> equal >> val >> unit2;\n if(m.count(unit1) == 0){\n m[unit1] = idx;\n ++idx;\n }\n int id1 = m[unit1];\n\n if(m.count(unit2) == 0){\n m[unit2] = idx;\n ++idx;\n }\n int id2 = m[unit2]; \n\n // int w;\n // vector<string> vec;\n // vec = split_native(val, '^');\n // w = stoi(vec[1]);\n\n // 単位換算の倍率の取得\n stringstream si(val.substr(3));\n int w;\n si >> w; // \"1 kilometre = 10^3 metre\" の 3 の部分を取得\n\n\n if(UF.issame(id1, id2)){\n int diff = UF.diff(id1, id2);\n if(diff != w) flag = false;\n }else{\n UF.merge(id1, id2, w);\n } \n }\n\n if (flag) puts(\"Yes\");\n else puts(\"No\");\n } \n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3092, "score_of_the_acc": -0.3446, "final_rank": 4 }, { "submission_id": "aoj_2207_4290844", "code_snippet": "#include <iostream>\n#include <sstream>\n#include <vector>\n#include <string>\n#include <map>\nusing namespace std;\n\ntemplate<class Abel> struct UnionFind {\n vector<int> par;\n vector<int> rank;\n vector<Abel> diff_weight;\n\n UnionFind(int n = 1, Abel SUM_UNITY = 0) {\n init(n, SUM_UNITY);\n }\n\n void init(int n = 1, Abel SUM_UNITY = 0) {\n par.resize(n); rank.resize(n); diff_weight.resize(n);\n for (int i = 0; i < n; ++i) par[i] = i, rank[i] = 0, diff_weight[i] = SUM_UNITY;\n }\n\n int root(int x) {\n if (par[x] == x) {\n return x;\n }\n else {\n int r = root(par[x]);\n diff_weight[x] += diff_weight[par[x]];\n return par[x] = r;\n }\n }\n\n Abel weight(int x) {\n root(x);\n return diff_weight[x];\n }\n\n bool issame(int x, int y) {\n return root(x) == root(y);\n }\n\n bool merge(int x, int y, Abel w) {\n w += weight(x); w -= weight(y);\n x = root(x); y = root(y);\n if (x == y) return false;\n if (rank[x] < rank[y]) swap(x, y), w = -w;\n if (rank[x] == rank[y]) ++rank[x];\n par[y] = x;\n diff_weight[y] = w;\n return true;\n }\n\n Abel diff(int x, int y) {\n return weight(y) - weight(x);\n }\n};\n\nint main() {\n int N;\n while (cin >> N) {\n if (N == 0) break;\n bool ok = true;\n UnionFind<int> uf(200); // 単位個数は最悪で 200 個\n map<string,int> str2id; // 単位 -> id の対応\n int final_id = 0;\n for (int i = 0; i < N; ++i) {\n string ichi, unit1, equal, val, unit2;\n cin >> ichi >> unit1 >> equal >> val >> unit2;\n\n // 1個目の単位の番号\n if (!str2id.count(unit1)) {\n str2id[unit1] = final_id++;\n }\n int id1 = str2id[unit1];\n\n // 2個目の単位の番号\n if (!str2id.count(unit2)) {\n str2id[unit2] = final_id++;\n }\n int id2 = str2id[unit2];\n\n // 単位換算の倍率の取得\n stringstream si(val.substr(3));\n int diff;\n si >> diff; // \"1 kilometre = 10^3 metre\" の 3 の部分を取得\n\n // 重み付き Union-Find 木の処理\n if (uf.issame(id1, id2)) {\n int curdiff = uf.diff(id1, id2);\n if (diff != curdiff) ok = false;\n }\n else {\n uf.merge(id1, id2, diff);\n }\n }\n\n if (ok) puts(\"Yes\");\n else puts(\"No\");\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3188, "score_of_the_acc": -0.5068, "final_rank": 12 }, { "submission_id": "aoj_2207_3835304", "code_snippet": "#include <algorithm>\n#include <iostream>\n#include <vector>\n#include <sstream>\n#include <map>\nusing namespace std;\n\ntemplate<typename T>\nstruct WeightedUnionFind {\n vector<int> data;\n vector<T> ws;\n WeightedUnionFind(int sz) : data(sz, -1), ws(sz) { }\n bool merge(int x, int y, T w) { // ws(y) = ws(x) + w\n w += weight(x); w -= weight(y);\n if ((x = root(x)) == (y = root(y))) return false;\n if (data[x] > data[y]) swap(x, y), w = -w;\n data[x] += data[y]; data[y] = x; ws[y] = w;\n return true;\n }\n bool find(int x, int y) { return root(x) == root(y); }\n T diff(int x, int y) { return weight(y) - weight(x); }\n T weight(int t) { root(t); return ws[t]; }\n int root(int k) {\n if (data[k] < 0) return k;\n int par = root(data[k]);\n ws[k] += ws[data[k]];\n return data[k] = par;\n }\n};\n\ntemplate<typename F, typename T>\nvoid convert(const F &f, T &t) { stringstream ss; ss << f; ss >> t; }\n\nint main() {\n int N;\n while (cin >> N, N) {\n bool ok = true;\n WeightedUnionFind<int> wuf(200);\n map<string, int> str2id;\n int cnt = 0;\n for (int i = 0; i < N; ++i) {\n string one, unit1, equal, val, unit2;\n cin >> one >> unit1 >> equal >> val >> unit2;\n if (!str2id.count(unit1)) str2id[unit1] = cnt++;\n int id1 = str2id[unit1];\n if (!str2id.count(unit2)) str2id[unit2] = cnt++;\n int id2 = str2id[unit2];\n int diff;\n convert(val.substr(3), diff);\n if (wuf.find(id1, id2)) {\n int curdiff = wuf.diff(id1, id2);\n if (diff != curdiff) ok = false;\n } else {\n wuf.merge(id1, id2, diff);\n }\n }\n puts(ok ? \"Yes\" : \"No\");\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3124, "score_of_the_acc": -0.3986, "final_rank": 7 }, { "submission_id": "aoj_2207_3835300", "code_snippet": "#include <algorithm>\n#include <cstdio>\n#include <vector>\n#include <string>\n#include <map>\nusing namespace std;\n\ntemplate <typename T>\nstruct Edge { int src, dst; T cost;\n Edge(int dst, T cost) : src(-1), dst(dst), cost(cost) { }\n Edge(int src, int dst, T cost) : src(src), dst(dst), cost(cost) { }\n};\ntemplate <typename T> using Edges = vector<Edge<T>>;\ntemplate <typename T> using WeightedGraph = vector<Edges<T>>;\ntemplate <typename T> using Matrix = vector<vector<T>>;\n\ntemplate <typename T>\nvoid warshall_floyd(Matrix<T> &g) {\n const T INF = numeric_limits<T>::max();\n for (int k = 0; k < g.size(); k++) {\n for (int i = 0; i < g.size(); i++) if (g[i][k] != INF) {\n for (int j = 0; j < g.size(); j++) if (g[k][j] != INF) {\n g[i][j] = min(g[i][j], g[i][k] + g[k][j]);\n }\n }\n }\n}\n\nint main() {\n const auto INF = numeric_limits<int>::max();\n int N;\n while (scanf(\"%d%*c\", &N), N) {\n map<string, int> unit2id;\n Matrix<int> mat(200, vector<int>(200, INF));\n for (int i = 0; i < 200; i++) mat[i][i] = 0;\n int n = 0;\n while (N--) {\n char s[32], t[32]; int val;\n scanf(\"1 %s = 10^%d %s\", s, &val, t); getchar();\n string s1 = s, s2 = t;\n if (!unit2id.count(s1)) unit2id[s1] = n++;\n if (!unit2id.count(s2)) unit2id[s2] = n++;\n int id1 = unit2id[s1], id2 = unit2id[s2];\n mat[id1][id2] = val;\n mat[id2][id1] = -val;\n }\n warshall_floyd(mat);\n bool ok = true;\n for (int i = 0; i < n; i++) ok &= mat[i][i] == 0;\n puts(ok ? \"Yes\" : \"No\");\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 2892, "score_of_the_acc": -0.0068, "final_rank": 2 }, { "submission_id": "aoj_2207_3741718", "code_snippet": "#include<iostream>\n#include<vector>\n#include<map>\n#include<queue>\nusing namespace std;\n\nconst int INF = 1<<29;\nint main(){\n int n;\n while(cin >> n, n){\n map<string, int> enc;\n vector<vector<int>> path(200, vector<int>(200, INF));\n int cnt = 0;\n string x, a, b, c;\n for(int i = 0; i < n; i++){\n cin >> x >> a >> x >> c >> b;\n int val = stoi(c.substr(3));\n if(enc.count(a) == 0) enc[a] = cnt++;\n if(enc.count(b) == 0) enc[b] = cnt++;\n path[enc[a]][enc[b]] = val;\n path[enc[b]][enc[a]] = -val;\n }\n // run dijkstra from all vertexes\n bool valid = true;\n for(int i = 0; i < cnt && valid; i++){\n vector<int> dp(cnt, INF);\n dp[i] = 0;\n queue<int> q;\n q.push(i);\n while(valid && !q.empty()){\n int pos = q.front(); q.pop();\n for(int j = 0; j < cnt; j++){\n if(path[pos][j] == INF) continue;\n if(dp[j] == INF){\n dp[j] = dp[pos]+path[pos][j];\n q.push(j);\n }else if(dp[pos]+path[pos][j] != dp[j]){\n valid = false;\n break;\n }\n }\n }\n }\n cout << (valid ? \"Yes\" : \"No\") << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3336, "score_of_the_acc": -0.7568, "final_rank": 17 }, { "submission_id": "aoj_2207_3699912", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define int long long\n\ntemplate <typename T>\nstruct UF {\n int n;\n T d;\n vector<int> r, p;\n vector<T> ws;\n UF() {}\n UF(int sz, T d_) :\n n(sz), d(d_), r(n, 1), p(n), ws(n, d) { iota(p.begin(), p.end(), 0); }\n int find(int x) {\n if ( x == p[x] ) {\n return x;\n } else {\n int t = find(p[x]);\n ws[x] += ws[p[x]];\n return p[x] = t;\n }\n }\n\n T weight(int x) {\n find(x);\n return ws[x];\n }\n\n bool same(int x, int y) {\n return find(x) == find(y);\n }\n\n void unite(int x, int y, T w) {\n w += weight(x);\n w -= weight(y);\n x = find(x); y = find(y);\n if ( x == y ) return;\n if ( r[x] < r[y] ) swap(x, y), w = -w;\n r[x] += r[y];\n p[y] = x;\n ws[y] = w;\n }\n\n T diff(int x, int y) {\n return weight(y) - weight(x); \n }\n};\n\nsigned main(){\n\tint n;\n\twhile(cin>>n,n){\n\t\tUF<int> U(2*n,0);\n\t\tmap<string,int>M;\n\t\tint cnt=0,F=0;\n\t\tfor(int i=0;i<n;i++){\n\t\t\tint a,D;\n\t\t\tstring b,c,e,d;\n\t\t\tcin>>a>>b>>c>>d>>e;\n\t\t\td=d.substr(3);\n\t\t\tstringstream SS(d);\n\t\t\tSS>>D;\n\t\t\t//cout<<D<<endl;\n\t\t\tif(!M.count(b)) M[b]=cnt++;\n\t\t\tif(!M.count(e)) M[e]=cnt++;\n\t\t\tif(U.same(M[b],M[e])&&U.diff(M[b],M[e])!=D)F=1;\n\t\t\tU.unite(M[b],M[e],D);\n\t\t\t//cout<<F<<endl;\n\t\t}\n\t\tif(F)goto L;\n\t\tcout<<\"Yes\"<<endl;\n\t\tif(0){\n\t\t\tL:;\n\t\t\tcout<<\"No\"<<endl;\n\t\t}\n\t}\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3184, "score_of_the_acc": -0.5, "final_rank": 11 }, { "submission_id": "aoj_2207_3699267", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define int long long\n\ntemplate <typename T>\nstruct UF {\n int n;\n T d;\n vector<int> r, p;\n vector<T> ws;\n UF() {}\n UF(int sz, T d_) :\n n(sz), d(d_), r(n, 1), p(n), ws(n, d) { iota(p.begin(), p.end(), 0); }\n int find(int x) {\n if ( x == p[x] ) {\n return x;\n } else {\n int t = find(p[x]);\n ws[x] += ws[p[x]];\n return p[x] = t;\n }\n }\n\n T weight(int x) {\n find(x);\n return ws[x];\n }\n\n bool same(int x, int y) {\n return find(x) == find(y);\n }\n\n void unite(int x, int y, T w) {\n w += weight(x);\n w -= weight(y);\n x = find(x); y = find(y);\n if ( x == y ) return;\n if ( r[x] < r[y] ) swap(x, y), w = -w;\n r[x] += r[y];\n p[y] = x;\n ws[y] = w;\n }\n\n T diff(int x, int y) {\n return weight(y) - weight(x); \n }\n};\n\nsigned main() {\n int n;\n while ( cin >> n, n ) {\n bool flag = true; \n UF<int> uf(2*n+1, 0);\n map<string, int> stoi;\n int cnt = 0;\n for ( int i = 0; i < n; i++ ) {\n int a;\n string b, c, d, e;\n cin >> a >> b >> c >> d >> e; \n if ( !flag ) continue;\n if ( !stoi.count(b) ) stoi[b] = cnt++;\n if ( !stoi.count(e) ) stoi[e] = cnt++;\n\n int numd = 0;\n for ( int i = 0; i < (int)d.size(); i++ ) {\n\tif ( d[i] == '^' ) {\n\t i++;\t \n\t int mi = 1;\t \n\t if ( d[i] == '-' ) mi = -1, i++;\n\t for ( ; i < (int)d.size(); i++ ) {\n\t numd *= 10;\n\t numd += d[i]-'0';\t \n\t }\n\t numd *= mi; \n\t}\n }\n // cout << stoi[b] << \" \" << stoi[e] << endl;\n if ( uf.same(stoi[b], stoi[e]) && uf.diff(stoi[b], stoi[e]) != numd ) {\t\n\tflag = false;\n\tcontinue;\t\n }\n uf.unite(stoi[b], stoi[e], numd);\n // cout << uf.diff(stoi[e], stoi[b]) << endl;\n }\n\n if ( flag ) cout << \"Yes\" << endl;\n else cout << \"No\" << endl;\n }\n \n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3180, "score_of_the_acc": -0.4932, "final_rank": 10 }, { "submission_id": "aoj_2207_3426392", "code_snippet": "#include <algorithm>\n#include <cstdio>\n#include <vector>\n#include <string>\n#include <map>\nusing namespace std;\nconst int INF = (int)1e9 + 7;\n\nint main() {\n int N;\n while (scanf(\"%d%*c\", &N), N) {\n map<string, int> unit2id;\n vector<vector<int>> dp(200, vector<int>(200, INF));\n int n = 0;\n while (N--) {\n char s[32], t[32]; int val;\n scanf(\"1 %s = 10^%d %s\", s, &val, t); getchar();\n string s1 = s, s2 = t;\n if (unit2id.find(s1) == unit2id.end()) unit2id[s1] = n++;\n if (unit2id.find(s2) == unit2id.end()) unit2id[s2] = n++;\n int id1 = unit2id[s1], id2 = unit2id[s2];\n dp[id1][id2] = val;\n dp[id2][id1] = -val;\n }\n // WarshallFloyd\n for (int k = 0; k < n; k++) {\n for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) {\n if (dp[i][k] != INF && dp[k][j] != INF) {\n dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j]);\n }\n }\n }\n bool ok = true;\n for (int i = 0; i < n; i++) if (dp[i][i] != 0) ok = false;\n puts(ok ? \"Yes\" : \"No\");\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 2888, "score_of_the_acc": 0, "final_rank": 1 }, { "submission_id": "aoj_2207_3426266", "code_snippet": "#include <algorithm>\n#include <iostream>\n#include <vector>\n#include <sstream>\n#include <map>\nusing namespace std;\n\ntemplate<class Abel> struct UnionFind {\n vector<int> par;\n vector<int> rank;\n vector<Abel> diff_weight;\n UnionFind(int n = 1, Abel SUM_UNITY = 0) { init(n, SUM_UNITY); }\n void init(int n = 1, Abel SUM_UNITY = 0) {\n par.resize(n); rank.resize(n); diff_weight.resize(n);\n for (int i = 0; i < n; ++i) {\n par[i] = i, rank[i] = 0, diff_weight[i] = SUM_UNITY;\n }\n }\n int root(int x) {\n if (par[x] == x) {\n return x;\n } else {\n int r = root(par[x]);\n diff_weight[x] += diff_weight[par[x]];\n return par[x] = r;\n }\n }\n Abel weight(int x) { root(x); return diff_weight[x]; }\n bool issame(int x, int y) { return root(x) == root(y); }\n // weight(y) - weight(x) = w となるように merge\n bool merge(int x, int y, Abel w) {\n w += weight(x); w -= weight(y);\n if ((x = root(x)) == (y = root(y))) return false;\n if (rank[x] < rank[y]) swap(x, y), w = -w;\n if (rank[x] == rank[y]) ++rank[x];\n par[y] = x; diff_weight[y] = w;\n return true;\n }\n Abel diff(int x, int y) { return weight(y) - weight(x); }\n};\n\nint main() {\n int N;\n while (cin >> N, N) {\n bool ok = true;\n UnionFind<int> uf(200);\n map<string, int> str2id;\n int cnt = 0;\n for (int i = 0; i < N; ++i) {\n string one, unit1, equal, val, unit2;\n cin >> one >> unit1 >> equal >> val >> unit2;\n if (!str2id.count(unit1)) str2id[unit1] = cnt++;\n int id1 = str2id[unit1];\n if (!str2id.count(unit2)) str2id[unit2] = cnt++;\n int id2 = str2id[unit2];\n stringstream ss(val.substr(3));\n int diff;\n ss >> diff;\n if (uf.issame(id1, id2)) {\n int curdiff = uf.diff(id1, id2);\n if (diff != curdiff) ok = false;\n } else {\n uf.merge(id1, id2, diff);\n }\n }\n puts(ok ? \"Yes\" : \"No\");\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3196, "score_of_the_acc": -0.5203, "final_rank": 14 }, { "submission_id": "aoj_2207_2757181", "code_snippet": "#include <iostream>\n#include <sstream>\n#include <vector>\n#include <string>\n#include <map>\nusing namespace std;\n\ntemplate<class Abel> struct UnionFind {\n\tvector<int> par;\n\tvector<int> rank;\n\tvector<Abel> diff_weight;\n\n\tUnionFind(int n = 1, Abel SUM_UNITY = 0) {\n\t\tinit(n, SUM_UNITY);\n\t}\n\n\tvoid init(int n = 1, Abel SUM_UNITY = 0) {\n\t\tpar.resize(n); rank.resize(n); diff_weight.resize(n);\n\t\tfor (int i = 0; i < n; ++i) par[i] = i, rank[i] = 0, diff_weight[i] = SUM_UNITY;\n\t}\n\n\tint root(int x) {\n\t\tif (par[x] == x) {\n\t\t\treturn x;\n\t\t}\n\t\telse {\n\t\t\tint r = root(par[x]);\n\t\t\tdiff_weight[x] += diff_weight[par[x]];\n\t\t\treturn par[x] = r;\n\t\t}\n\t}\n\n\tAbel weight(int x) {\n\t\troot(x);\n\t\treturn diff_weight[x];\n\t}\n\n\tbool issame(int x, int y) {\n\t\treturn root(x) == root(y);\n\t}\n\n\tbool merge(int x, int y, Abel w) {\n\t\tw += weight(x); w -= weight(y);\n\t\tx = root(x); y = root(y);\n\t\tif (x == y) return false;\n\t\tif (rank[x] < rank[y]) swap(x, y), w = -w;\n\t\tif (rank[x] == rank[y]) ++rank[x];\n\t\tpar[y] = x;\n\t\tdiff_weight[y] = w;\n\t\treturn true;\n\t}\n\n\tAbel diff(int x, int y) {\n\t\treturn weight(y) - weight(x);\n\t}\n};\n\nint main() {\n int N;\n while (cin >> N) {\n if (N == 0) break;\n bool ok = true;\n UnionFind<int> uf(200); // 単位個数は最悪で 200 個\n map<string,int> str2id; // 単位 -> id の対応\n int final_id = 0;\n for (int i = 0; i < N; ++i) {\n string ichi, unit1, equal, val, unit2;\n cin >> ichi >> unit1 >> equal >> val >> unit2;\n \n // 1個目の単位の番号\n if (!str2id.count(unit1)) {\n str2id[unit1] = final_id++;\n }\n int id1 = str2id[unit1];\n \n // 2個目の単位の番号\n if (!str2id.count(unit2)) {\n str2id[unit2] = final_id++;\n }\n int id2 = str2id[unit2];\n \n // 単位換算\n stringstream si(val.substr(3));\n int diff;\n si >> diff; // \"1 kilometre = 10^3 metre\" の 3 の部分を取得\n \n // 重み付き Union-Find 木の処理\n if (uf.issame(id1, id2)) {\n int curdiff = uf.diff(id1, id2);\n if (diff != curdiff) ok = false;\n }\n else {\n uf.merge(id1, id2, diff);\n }\n }\n \n if (ok) puts(\"Yes\");\n else puts(\"No\");\n\t}\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3032, "score_of_the_acc": -0.2432, "final_rank": 3 }, { "submission_id": "aoj_2207_2642272", "code_snippet": "#include \"bits/stdc++.h\"\n#include<unordered_map>\n#include<unordered_set>\n#pragma warning(disable:4996)\nusing namespace std;\n\n\nstruct aUnionFind {\n\tvector<pair<int, long long int>> data;\n\taUnionFind(int size) : data(size, make_pair(-1, 0)) { }\n\n\t//y is w bigger than x\n\tbool unionSet(const int x, const int y, const long long int w) {\n\t\tconst int rx(root(x).first), ry(root(y).first);\n\t\tif (rx != ry) {\n\t\t\tdata[rx].first += data[ry].first; data[ry].first = rx;\n\t\t\tdata[ry].second = w + data[x].second - data[y].second;\n\t\t\treturn true;\n\t\t}\n\t\telse {\n\t\t\treturn root(x).second + w == root(y).second;\n\t\t}\n\t}\n\n\t//x is belong to first\n\t//x is second bigger than first\n\tpair<int, long long int> root(const int x) {\n\t\tif (data[x].first < 0) {\n\t\t\treturn make_pair(x, 0);\n\t\t}\n\t\telse {\n\t\t\tpair<int, long long int>ndata;\n\t\t\tndata.first = root(data[x].first).first;\n\t\t\tndata.second = data[x].second + root(data[x].first).second;\n\t\t\treturn data[x] = ndata;\n\t\t}\n\t}\n\tint size(const int x) {\n\t\treturn -data[root(x).first].first;\n\t}\n};\n\n\n\nint main() {\n\t\n\twhile (true) {\n\t\tint T;cin>>T;\n\t\tif(!T)break;\n\t\tmap<string,int>mp;\n\t\tint id=0;\n\t\taUnionFind uf(1000);\n\t\tbool ok=true;\n\t\twhile (T--) {\n\t\t\tstring one,name1,eq,num,name2;\n\t\t\tcin>>one>>name1>>eq>>num>>name2;\n\n\t\t\tif (mp.find(name1) == mp.end()) {\n\t\t\t\tmp[name1]=id++;\n\t\t\t}\n\t\t\tif (mp.find(name2) == mp.end()) {\n\t\t\t\tmp[name2]=id++;\n\t\t\t}\n\n\t\t\tstring n_st(num);\n\t\t\tint anum=stoi(n_st.substr(3));\n\n\t\t\tif (uf.root(mp[name1]).first != uf.root(mp[name2]).first) {\n\t\t\t\tuf.unionSet(mp[name1],mp[name2],anum);\n\t\t\t}\n\t\t\telse {\n\t\t\t\tif (uf.root(mp[name1]).second + anum == uf.root(mp[name2]).second) {\n\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tok=false;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif(ok)cout<<\"Yes\"<<endl;\n\t\telse cout<<\"No\"<<endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3196, "score_of_the_acc": -0.5203, "final_rank": 14 }, { "submission_id": "aoj_2207_2625922", "code_snippet": "#include <stdio.h>\n#include <cmath>\n#include <algorithm>\n#include <cfloat>\n#include <stack>\n#include <queue>\n#include <vector>\n#include <string>\n#include <iostream>\n#include <set>\n#include <map>\n#include <time.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\nstruct Info{\n\tint loc;\n};\n\nint N;\nchar name_table[200][17];\nchar left_table[100][17],relative[100][17],right_table[100][17];\n\nbool strCmp(char* base, char* comp){\n\tint length1,length2;\n\tfor(length1=0;base[length1] != '\\0';length1++);\n\tfor(length2=0;comp[length2] != '\\0';length2++);\n\tif(length1 != length2)return false;\n\n\tfor(int i=0;base[i] != '\\0'; i++){\n\t\tif(base[i] != comp[i])return false;\n\t}\n\treturn true;\n}\n\nvoid strcpy(char* to,char* str){\n\tfor(int i=0;str[i] != '\\0';i++){\n\t\tto[i] = str[i];\n\t\tto[i+1] = '\\0';\n\t}\n}\n\nInfo info[200];\n\nint get_relative(char buf[17]){\n\n\tint ret = 0;\n\n\tif(buf[3] == '-'){\n\t\tfor(int i = 4; buf[i] != '\\0'; i++){\n\t\t\tret = 10*ret+buf[i]-'0';\n\t\t}\n\t\tret*= -1;\n\t}else{\n\t\tfor(int i = 3; buf[i] != '\\0'; i++){\n\t\t\tret = 10*ret+buf[i]-'0';\n\t\t}\n\t}\n\treturn ret;\n}\n\nvoid func(){\n\n\tfor(int i = 0; i < 100; i++){\n\t\tinfo[i].loc = -1;\n\t}\n\n\tint tmp;\n\tchar buf[2];\n\n\tfor(int i = 0; i < N; i++){\n\t\tscanf(\"%d %s %s %s %s\",&tmp,left_table[i],buf,relative[i],right_table[i]);\n\t}\n\n\tstrcpy(name_table[0],left_table[0]);\n\tstrcpy(name_table[1],right_table[0]);\n\n\tint index = 2;\n\ttmp = get_relative(relative[0]);\n\tinfo[0].loc = 10000;\n\tinfo[1].loc = 10000-tmp;\n\n\tint used_num = 1;\n\tint left_index,right_index;\n\tint eq_num;\n\n\tbool FLG;\n\n\tvector<int> V;\n\tfor(int i = 1; i < N; i++)V.push_back(i);\n\tint V_loc;\n\n\twhile(used_num < N){\n\n\t\tFLG = false;\n\n\t\tfor(int i = 0; i < V.size(); i++){\n\n\t\t\tleft_index = -1;\n\t\t\tfor(int k = 0; k < index; k++){\n\t\t\t\tif(strCmp(name_table[k],left_table[V[i]])){\n\t\t\t\t\tleft_index = k;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tright_index = -1;\n\t\t\tfor(int k = 0; k < index; k++){\n\t\t\t\tif(strCmp(name_table[k],right_table[V[i]])){\n\t\t\t\t\tright_index = k;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif(left_index != -1 || right_index != -1){\n\t\t\t\tFLG = true;\n\t\t\t\teq_num = V[i];\n\t\t\t\tV_loc = i;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\n\t\tif(!FLG){\n\t\t\tfor(int i = 0; i < V.size(); i++){\n\n\t\t\t\teq_num = V[i];\n\t\t\t\tV_loc = i;\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\tV.erase(V.begin()+V_loc);\n\t\t\tused_num++;\n\n\t\t\tstrcpy(name_table[index],left_table[eq_num]);\n\t\t\tstrcpy(name_table[index+1],right_table[eq_num]);\n\t\t\ttmp = get_relative(relative[eq_num]);\n\t\t\tinfo[index].loc = 10000;\n\t\t\tinfo[index+1].loc = info[index].loc-tmp;\n\t\t\tindex += 2;\n\t\t\tcontinue;\n\t\t}\n\n\t\tV.erase(V.begin()+V_loc);\n\t\tused_num++;\n\n\t\tif(left_index == -1){\n\t\t\tstrcpy(name_table[index],left_table[eq_num]);\n\t\t\ttmp = get_relative(relative[eq_num]);\n\t\t\tinfo[index].loc = info[right_index].loc+tmp;\n\t\t\tindex++;\n\t\t}else if(right_index == -1){\n\t\t\tstrcpy(name_table[index],right_table[eq_num]);\n\t\t\ttmp = get_relative(relative[eq_num]);\n\t\t\tinfo[index].loc = info[left_index].loc-tmp;\n\t\t\tindex++;\n\t\t}else{\n\t\t\ttmp = get_relative(relative[eq_num]);\n\n\t\t\tif(info[left_index].loc-info[right_index].loc != tmp){\n\t\t\t\tprintf(\"No\\n\");\n\t\t\t\treturn;\n\t\t\t}\n\t\t}\n\t}\n\n\tprintf(\"Yes\\n\");\n}\n\nint main(){\n\n\twhile(true){\n\t\tscanf(\"%d\",&N);\n\t\tif(N == 0)break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3128, "score_of_the_acc": -0.5633, "final_rank": 16 }, { "submission_id": "aoj_2207_2625906", "code_snippet": "#include <stdio.h>\n#include <cmath>\n#include <algorithm>\n#include <cfloat>\n#include <stack>\n#include <queue>\n#include <vector>\n#include <string>\n#include <iostream>\n#include <set>\n#include <map>\n#include <time.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\nstruct Info{\n\tint loc;\n};\n\nint N;\nchar name_table[200][17];\nchar left_table[100][17],relative[100][17],right_table[100][17];\n\nbool strCmp(char* base, char* comp){\n\tint length1,length2;\n\tfor(length1=0;base[length1] != '\\0';length1++);\n\tfor(length2=0;comp[length2] != '\\0';length2++);\n\tif(length1 != length2)return false;\n\n\tfor(int i=0;base[i] != '\\0'; i++){\n\t\tif(base[i] != comp[i])return false;\n\t}\n\treturn true;\n}\n\nvoid strcpy(char* to,char* str){\n\tfor(int i=0;str[i] != '\\0';i++){\n\t\tto[i] = str[i];\n\t\tto[i+1] = '\\0';\n\t}\n}\n\nInfo info[200];\nbool used[100];\n\nint get_relative(char buf[17]){\n\n\tint ret = 0;\n\n\tif(buf[3] == '-'){\n\t\tfor(int i = 4; buf[i] != '\\0'; i++){\n\t\t\tret = 10*ret+buf[i]-'0';\n\t\t}\n\t\tret*= -1;\n\t}else{\n\t\tfor(int i = 3; buf[i] != '\\0'; i++){\n\t\t\tret = 10*ret+buf[i]-'0';\n\t\t}\n\t}\n\treturn ret;\n}\n\nvoid func(){\n\n\tfor(int i = 0; i < 100; i++){\n\t\tinfo[i].loc = -1;\n\t\tused[i] = false;\n\t}\n\n\tint tmp;\n\tchar buf[2];\n\n\tfor(int i = 0; i < N; i++){\n\t\tscanf(\"%d %s %s %s %s\",&tmp,left_table[i],buf,relative[i],right_table[i]);\n\t}\n\n\tstrcpy(name_table[0],left_table[0]);\n\tstrcpy(name_table[1],right_table[0]);\n\n\tint index = 2;\n\ttmp = get_relative(relative[0]);\n\tinfo[0].loc = 10000;\n\tinfo[1].loc = 10000-tmp;\n\tused[0] = true;\n\n\tint used_num = 1;\n\tint left_index,right_index;\n\tint eq_num;\n\n\tbool FLG;\n\n\twhile(used_num < N){\n\n\t\tFLG = false;\n\n\t\tfor(int i = 1; i < N; i++){\n\t\t\tif(used[i])continue;\n\n\t\t\tleft_index = -1;\n\t\t\tfor(int k = 0; k < index; k++){\n\t\t\t\tif(strCmp(name_table[k],left_table[i])){\n\t\t\t\t\tleft_index = k;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tright_index = -1;\n\t\t\tfor(int k = 0; k < index; k++){\n\t\t\t\tif(strCmp(name_table[k],right_table[i])){\n\t\t\t\t\tright_index = k;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif(left_index != -1 || right_index != -1){\n\t\t\t\tFLG = true;\n\t\t\t\teq_num = i;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\n\t\tif(!FLG){\n\t\t\tfor(int i = 1; i < N; i++){\n\t\t\t\tif(used[i])continue;\n\n\t\t\t\teq_num = i;\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\tused[eq_num] = true;\n\t\t\tused_num++;\n\n\t\t\tstrcpy(name_table[index],left_table[eq_num]);\n\t\t\tstrcpy(name_table[index+1],right_table[eq_num]);\n\t\t\ttmp = get_relative(relative[eq_num]);\n\t\t\tinfo[index].loc = 10000;\n\t\t\tinfo[index+1].loc = info[index].loc-tmp;\n\t\t\tindex += 2;\n\t\t\tcontinue;\n\t\t}\n\n\t\tused[eq_num] = true;\n\t\tused_num++;\n\n\t\tif(left_index == -1){\n\t\t\tstrcpy(name_table[index],left_table[eq_num]);\n\t\t\ttmp = get_relative(relative[eq_num]);\n\t\t\tinfo[index].loc = info[right_index].loc+tmp;\n\t\t\tindex++;\n\t\t}else if(right_index == -1){\n\t\t\tstrcpy(name_table[index],right_table[eq_num]);\n\t\t\ttmp = get_relative(relative[eq_num]);\n\t\t\tinfo[index].loc = info[left_index].loc-tmp;\n\t\t\tindex++;\n\t\t}else{\n\t\t\ttmp = get_relative(relative[eq_num]);\n\n\t\t\tif(info[left_index].loc-info[right_index].loc != tmp){\n\t\t\t\tprintf(\"No\\n\");\n\t\t\t\treturn;\n\t\t\t}\n\t\t}\n\t}\n\n\tprintf(\"Yes\\n\");\n}\n\nint main(){\n\n\twhile(true){\n\t\tscanf(\"%d\",&N);\n\t\tif(N == 0)break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3068, "score_of_the_acc": -0.4619, "final_rank": 9 }, { "submission_id": "aoj_2207_2123222", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nint G[200][200];\nint tmp[200][200];\n\nmap<string,int> mp;\n\nint func(string s){\n if(mp.count(s)==0){\n int res=mp.size();\n mp[s]=res;\n return res;\n }else{\n return mp[s];\n }\n}\n\nint getNum(string s){\n for(int i=0;i<(int)s.size();i++)\n if(s[i]=='^')s[i]=' ';\n stringstream ss(s);\n int res;\n ss>>res;\n ss>>res;\n return res;\n}\n\nvoid init(){\n fill(G[0],G[200], 1e9);\n mp.clear();\n for(int i=0;i<200;i++)\n G[i][i]=0;\n}\n\nint main(){\n int n;\n while(1){\n cin>>n;\n if(n==0)break;\n init();\n bool ans=true;\n for(int i=0;i<n;i++){\n string A,B,C,D,E;\n cin>>A>>B>>C>>D>>E;\n int a=func(B);\n int b=func(E);\n int c=getNum(D);\n if(G[b][a]!=1e9&&G[b][a]!=c)ans=false;\n G[b][a]=c;\n G[a][b]=-c;\n }\n int size=mp.size();\n for(int i=0;i<size;i++){\n for(int j=0;j<size;j++){\n tmp[i][j]=G[i][j];\n }\n }\n \n for(int k=0;k<size;k++)\n for(int i=0;i<size;i++)\n for(int j=0;j<size;j++)\n G[i][j]=min(G[i][j],G[i][k]+G[k][j]);\n\n for(int i=0;i<size;i++)\n for(int j=0;j<size;j++)\n if(tmp[i][j]!=1e9 && G[i][j]!=tmp[i][j])ans=false;\n\n cout<< (ans?\"Yes\":\"No\") <<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3464, "score_of_the_acc": -1.0256, "final_rank": 18 } ]
aoj_2210_cpp
Problem G: 天体観測 2012年のある日のこと。 北の田舎町に住むあなたは、愛娘を連れて展望台で星見をすることにした。 この町は都会と違って街明かりが少なく、空気も澄んでいるので、晴れた日は満天の星空が広がっている。 天文にそこそこ詳しいあなたは季節ごとの有名な星や星座、それにまつわる神話ぐらいは知っているのだが、 なにせここは星降る町、名前のわからない星座もたくさん見えている。 この前の星見のときにそういった星々に関する娘の質問に答えられなかったあなたは、 星見の時刻にいったいどの星座が見えるのかを調べたいと思った。 特定の日時にどの星が見えるのかを知るには、星座早見盤というアイテムが有用である。 最近では携帯端末のGPSと連携した星座早見盤アプリも提供されている。 だが、あいにくあなたの手元には星座早見盤がなく、携帯は七年前の機種で、最新のアプリが動くわけがなかった。 詰んだと思ったあなたが最後の望みをかけてハードディスクを漁ったところ、 なんと2012年1月1日の午前零時時点での各星座を構成する星々の位置を記したファイルを発見した。 これがあれば任意の日時の星の位置がわかるはず! あなたは急いでどの星座が見えるのかを出力するプログラムを書くことにした。 星々の情報は、観測者を中心とした天球上の座標群として与えられている。 座標は方位角と高度のペアで表されている。 方位角は真北を0度、真東を90度というように時計回りに測った0度以上360度未満の角度であり、 高度は地平線上を0度、天頂を90度度、天底を-90度とした-90度以上90度以下の角度である。 各天体は北極星と観測者を結ぶ直線を回転軸として、天球上を一定速度で反時計回りに回転している。 その回転速度は24時間あたり(1+1/365.24)×360度である。 北極星は真北方向の高度43.2度の点にある。 ある星座が見えるとは、その星座を構成するすべての星が地平線より上にあることをいう。 テストデータにおいて、当該時刻に地平線から高度±0.01度以内に星があることはないとしてよい。 なお、地軸のズレ、天体との距離などは考えなくてよい。 また、2012年は閏年であることに注意せよ。 Input 入力は以下のような形式で与えられる。 mon / day hh : mm n name 1 m 1 a 11 h 11 a 12 h 12 ... a 1m 1 h 1m 1 name 2 m 2 a 21 h 21 ... 一行目には星見をする2012年のある日時が24時制で与えられる。 二行目の n は見えるかどうか判定したい星座の数を表す。 残りの行はその n 個の星座の2012年1月1日 00:00時点での位置情報である。 各星座の情報の先頭行には、星座名 name i と星座を構成する星の数 m i が与えられる。 続く m i 行に、各星の方位角 a ij と高度 h ij が与えられる。 これらの二つの数値は小数点以下三桁まで記述された浮動小数点数である。 入力の数値は、1 ≤ n ≤ 1000、1 ≤ m i ≤ 1000、0.000 ≤ a ij < 360.000、-90.000 ≤ h ij ≤ 90.000を満たす。 また、星座名はアルファベットの大文字小文字からなり、長さが50文字を超えることはない。 サンプルにあるとおり、「夏の大三角」や「北斗七星」など、88星座に入っていないものが与えられることもある。 なお、時刻は日没後の時刻とはかぎらないが、気にせず星が見えるものとしてよい。 Output 入力に与えられた順に、指定された日時に見える星座の一覧を出力せよ。 一つも星座が見えないようなテストデータはないと仮定してよい。 Notes on Test Cases 上記入力形式で複数のデータセットが与えられます。 入力の1行目にデータセットの数Tが与えられます。 各データセットに対して上記出力形式で出力を行うプログラムを作成して下さい。 Sample Input 3 7/7 18:30 3 Aquila 8 359.216 -32.936 358.624 -49.913 352.155 -43.404 346.852 -35.279 345.110 -36.796 342.448 -44.407 343.088 -38.981 336.070 -43.369 Cygnus 11 357.838 6.615 355.808 5.107 353.475 -18.553 348.868 -10.771 347.949 1.298 344.907 -4.637 342.909 1.163 338.483 -9.293 338.854 -1.786 338.515 1.304 331.752 -10.737 Lyra 4 5.063 -7.812 2.708 -13.386 1.327 -2.841 0.815 -14.109 9/3 02:40 3 SevenStars 7 40.483 50.729 48.968 50.637 50.421 42.796 44.855 40.960 43.493 35.637 42.111 31.430 44.997 25.256 SummerTriangle 3 342.909 1.163 345.110 -36.796 5.063 -7.812 WinterTriangle 3 165.368 51.176 207.400 51.262 184.960 31.381 7/7 19:00 91 Andromeda 25 300.224 10.559 313.114 22.235 304.593 17.311 305.145 18.508 299.557 18.169 295.825 15.561 296.928 16.703 290.573 13.699 304.020 24.766 300.872 24.287 296.636 24.636 304.470 32.153 303.783 32.893 298.697 32.261 306.181 35.986 296.619 37.096 301.005 41.728 319.394 10.944 323.303 16.879 313.853 10.187 313.295 13.504 317.463 18.294 315.012 16.124 315.502 17.165 316.369 19.322 Antila 5 150.199 5.822 143.663 11.282 144.808 2.868 138.069 2.334 136.680 -6.080 Apus 6 159.498 -45.403 165.358 -47.196 157.404 -51.717 166.529 -52.133 167.065 -52.812 162.765 -60.430 Aquarius 28 318.790 -48.408 321.209 -45.287 318.270 -46.509 310.269 -47.366 305.6 ...(truncated)
[ { "submission_id": "aoj_2210_3086590", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <cmath>\n#include <iomanip>\nusing namespace std;\nconst double EPS = 1e-8;\nconst double INF = 1e18;\nconst double PI = acos(-1);\n\nbool epsleq(const double &a, const double &b){\n return a +EPS < b;\n}\nbool epseq(const double &a, const double &b){\n return abs(a-b) < EPS;\n}\n\nstruct Point{\n double x,y,z;\n Point(double x, double y, double z):x(x), y(y), z(z){}\n Point(){}\n Point &operator +=(const Point &a){ x+=a.x; y+=a.y; z+=a.z; return *this; }\n Point &operator -=(const Point &a){ x-=a.x; y-=a.y; z-=a.z; return *this; }\n Point &operator *=(const double &a){ x*=a; y*=a; z*=a; return *this; }\n Point operator +(const Point &a) const{ return Point(x+a.x, y+a.y, z+a.z); }\n Point operator -(const Point &a) const{ return Point(x-a.x, y-a.y, z-a.z); }\n Point operator *(const double &a) const{ return Point(a*x, a*y, a*z); }\n Point operator -() { return Point(-x, -y, -z); }\n bool operator <(const Point &a) const{\n return !epseq(x, a.x)? x < a.x: !epseq(y, a.y)? y < a.y: epsleq(z, a.z);\n }\n bool operator ==(const Point &a) const{\n double dx=x-a.x, dy=y-a.y, dz=z-a.z;\n return sqrt(dx*dx +dy*dy +dz*dz) < EPS;\n }\n};\n\ndouble dot(const Point &a, const Point &b){\n return a.x*b.x +a.y*b.y +a.z*b.z;\n}\nPoint cross(const Point &a, const Point &b){\n return Point(a.y*b.z -a.z*b.y, a.z*b.x -a.x*b.z, a.x*b.y -a.y*b.x);\n}\ndouble abs(const Point &a){\n return sqrt(dot(a, a));\n}\nPoint unit(const Point &a){\n return a *(1/abs(a));\n}\n\nPoint rotate(const Point &p, const Point &axis, double angle){\n Point n = unit(axis);\n double SIN = sin(angle), COS = cos(angle);\n double vec[3] = {p.x, p.y, p.z};\n double mat[3][3] = {{COS +n.x*n.x*(1-COS), n.x*n.y*(1-COS) -n.z*SIN, n.x*n.z*(1-COS) +n.y*SIN},\n {n.x*n.y*(1-COS) +n.z*SIN, COS +n.y*n.y*(1-COS), n.y*n.z*(1-COS) -n.x*SIN},\n {n.x*n.z*(1-COS) -n.y*SIN, n.y*n.z*(1-COS) +n.x*SIN, COS +n.z*n.z*(1-COS)}};\n double ret[3] = {};\n for(int i=0; i<3; i++){\n for(int j=0; j<3; j++){\n ret[i] += mat[i][j] *vec[j];\n }\n }\n return Point(ret[0], ret[1], ret[2]);\n}\n\nint md[12] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};\n\nint main(){\n int ds;\n cin >> ds;\n for(int rep=0; rep<ds; rep++){\n int mon,day,hh,mm;\n scanf(\"%d/%d %d:%d\", &mon, &day, &hh, &mm);\n for(int i=0; i<mon-1; i++){\n day += md[i];\n }\n mm += 60 *(24*(day-1) +hh);\n \n double angle = (1+1/365.24)*360 *mm/24/60 *PI/180;\n\n vector<string> ans;\n Point pole(cos(43.2/180*PI), 0, sin(43.2/180*PI));\n int n;\n cin >> n;\n for(int i=0; i<n; i++){\n string name;\n int size;\n cin >> name >> size;\n bool canlook = true;\n for(int j=0; j<size; j++){\n double theta, phi;\n cin >> theta >> phi;\n theta *= PI/180;\n phi *= PI/180;\n Point p(cos(theta)*cos(phi), sin(theta)*cos(phi), sin(phi));\n p = rotate(p, pole, angle);\n if(p.z < 0){\n canlook = false;\n }\n }\n if(canlook){\n ans.push_back(name);\n }\n }\n for(string s: ans){\n cout << s << endl;\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3736, "score_of_the_acc": -1, "final_rank": 8 }, { "submission_id": "aoj_2210_1434960", "code_snippet": "#include<cstdio>\n#include<cstdlib>\n#include<cstring>\n#include<cmath>\n#include<iostream>\n#include<string>\n#include<vector>\n#include<map>\n#include<set>\n#include<list>\n#include<queue>\n#include<deque>\n#include<algorithm>\n#include<numeric>\n#include<utility>\n#include<complex>\n#include<functional>\n \nusing namespace std;\n\n/* constant */\n\nconst int MAX_N = 1000;\nconst int MAX_M = 1000;\n\nconst double NP_DEG = 43.2;\nconst double RDPM = (1.0 + 1.0 / 365.24) * 360 / (24 * 60);\n\nconst double PI = acos(-1.0);\nconst double DELTA = sin(0.01 * PI / 180);\n\nconst int mdays[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};\n\n/* typedef */\n\ntemplate <typename T>\nstruct Pt3D {\n T x, y, z;\n\n Pt3D() {}\n Pt3D(T _x, T _y, T _z) { x = _x; y = _y; z = _z; }\n\n bool operator==(const Pt3D<T>& pt) const {\n return x == pt.x && y == pt.y && z == pt.z;\n }\n\n bool operator<(const Pt3D<T>& pt) const {\n return x < pt.x || (x == pt.x && (y < pt.y || (y == pt.y && z < pt.z)));\n }\n \n Pt3D<T> operator+(const Pt3D<T>& pt) const {\n return Pt3D<T>(x + pt.x, y + pt.y, z + pt.z);\n }\n\n Pt3D<T> operator-(const Pt3D<T>& pt) const {\n return Pt3D<T>(x - pt.x, y - pt.y, z - pt.z);\n }\n \n Pt3D<T> operator-() const { return Pt3D<T>(-x, -y, -z); }\n Pt3D<T> operator*(const T& t) const { return Pt3D<T>(x * t, y * t, z * t); }\n Pt3D<T> operator/(const T& t) const { return Pt3D<T>(x / t, y / t, z / t); }\n\n T dot(const Pt3D<T>& v) const { return x * v.x + y * v.y + z * v.z; }\n\n Pt3D<T> cross(const Pt3D<T>& v) const {\n return Pt3D<T>(y * v.z - z * v.y, z * v.x - x * v.z, x * v.y - y * v.x);\n }\n\n T d2() const { return x * x + y * y + z * z; }\n double d() const { return sqrt(d2()); }\n Pt3D<T> normalize() const { return *this / d(); }\n\n Pt3D<T> rotx(double th) {\n double c = cos(th), s = sin(th);\n return Pt3D<T>(x, c * y - s * z, s * y + c * z);\n }\n \n Pt3D<T> roty(double th) {\n double c = cos(th), s = sin(th);\n return Pt3D<T>(s * z + c * x, y, c * z - s * x);\n }\n \n Pt3D<T> rotz(double th) {\n double c = cos(th), s = sin(th);\n return Pt3D<T>(c * x - s * y, s * x + c * y, z);\n }\n \n void print(string format) {\n printf((\"(\" + format + \", \" + format + \", \" + format + \")\\n\").c_str(),\n x, y, z);\n }\n void print() { print(\"%.6lf\"); }\n};\n\ntypedef Pt3D<double> pt3d;\n\n/* global variables */\n\n/* subroutines */\n\nint datetime2min(string md, string hm) {\n int m = 0, d = 0, pos = 0;\n\n while (md[pos] != '/') m = m * 10 + (md[pos++] - '0');\n pos++;\n while (pos < md.length()) d = d * 10 + (md[pos++] - '0');\n\n int hh = (hm[0] - '0') * 10 + (hm[1] - '0');\n int mm = (hm[3] - '0') * 10 + (hm[4] - '0');\n //printf(\"%d/%d %02d:%02d\\n\", m, d, hh, mm);\n\n int doy = d;\n for (int i = 2; i <= m; i++) doy += mdays[i - 2];\n \n return (((doy - 1) * 24) + hh) * 60 + mm;\n}\n\n/* main */\n\nint main() {\n double dth = PI * (90.0 - NP_DEG) / 180;\n\n int tn;\n cin >> tn;\n\n while (tn--) {\n string md, hm;\n cin >> md >> hm;\n\n int min = datetime2min(md, hm);\n double dphi = -PI * RDPM / 180 * min;\n \n int n, m;\n string name;\n cin >> n;\n\n while (n--) {\n cin >> name >> m;\n\n bool ok = true;\n\n while (m--) {\n\tdouble d0, d1;\n\tcin >> d0 >> d1;\n\n\tif (ok) {\n\t double phi = (90.0 - d0) * PI / 180;\n\t double th = d1 * PI / 180;\n\n\t double cp = cos(phi), sp = sin(phi);\n\t double ct = cos(th), st = sin(th);\n\n\t pt3d p0(cp * ct, sp * ct, st);\n\t pt3d p1 = p0.rotx(dth).rotz(dphi).rotx(-dth);\n\n\t if (p1.z <= DELTA) ok = false;\n\t //cout << name << \": \";p1.print();\n\t}\n }\n\n if (ok) cout << name << endl;\n }\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 1244, "score_of_the_acc": -0.533, "final_rank": 3 }, { "submission_id": "aoj_2210_947370", "code_snippet": "#include <bits/stdc++.h>\n#define REP(i,n) for(int i=0; i<(int)(n); ++i)\n\nusing namespace std;\n\ndouble calc(int m, int d, int hh, int mm) {\n vector<int> days(13, 31);\n days[0] = 0;\n days[2] = 29;\n for(int i : {4, 6, 9, 11}) {\n days[i] = 30;\n }\n for(int i = 2; i <= 12; i++) {\n days[i] += days[i - 1];\n }\n d += days[m - 1];\n return (d - 1) + hh / 24.0 + mm / (24.0 * 60.0);\n}\n\nvoid rotate(double th, double &a, double &b) {\n double na = cos(th) * a + -sin(th) * b;\n double nb = sin(th) * a + cos(th) * b;\n a = na;\n b = nb;\n}\n\ndouble to_radian(double degree) {\n return degree / 180.0 * M_PI;\n}\nvoid make(double a, double b, double& x, double& y, double& z) {\n x = cos(a);\n y = sin(a);\n z = tan(b);\n}\n\nconst double EPS = 1e-8;\n\nint main(){\n int T;\n cin >> T;\n for(int casenum = 0; casenum < T; casenum++){\n int mon, day, hh, mm;\n scanf(\"%d/%d\", &mon, &day);\n scanf(\"%d:%d\", &hh, &mm);\n double theta = to_radian( (1.0 + 1.0 / 365.24) * 360.0 * calc(mon, day, hh, mm) );\n const double P = to_radian( 43.2 );\n int n;\n cin >> n;\n for(int i = 0; i < n; i++) {\n string name;\n int m;\n cin >> name >> m;\n bool ok = true;\n // cout << name << endl;\n for(int j = 0; j < m; j++) {\n double a, b;\n cin >> a >> b;\n double x, y, z;\n make(to_radian(a), to_radian(b), x, y, z);\n // printf(\"(%f, %f, %f)\\n\", x, y, z);\n rotate(-P, x, z);\n // printf(\"->(%f, %f, %f)\\n\", x, y, z);\n rotate(theta, y, z);\n // printf(\"->(%f, %f, %f)\\n\", x, y, z);\n rotate(P, x, z);\n // printf(\"->(%f, %f, %f)\\n\", x, y, z);\n if(z < 0.0 + EPS) {\n ok = false;\n }\n }\n if(ok) {\n cout << name << endl;\n }\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 1344, "score_of_the_acc": -0.5597, "final_rank": 4 }, { "submission_id": "aoj_2210_371702", "code_snippet": "#include<cmath>\n#include<cstdio>\n\n#define rep(i,n) for(int i=0;i<(n);i++)\n\nusing namespace std;\n\nconst double PI=acos(-1);\n\ntemplate<class T>\nstruct point3{\n\tT x,y,z;\n\tpoint3 operator+(const point3 &a)const{ return (point3){x+a.x,y+a.y,z+a.z}; }\n};\n\ntemplate<class T>\npoint3<T> operator*(T c,const point3<T> &a){\n\treturn (point3<T>){c*a.x,c*a.y,c*a.z};\n}\n\ntemplate<class T>\nT dot(const point3<T> &a,const point3<T> &b){ return a.x*b.x+a.y*b.y+a.z*b.z; }\n\ntemplate<class T>\npoint3<T> cross(const point3<T> &a,const point3<T> &b){\n\treturn (point3<T>){a.y*b.z-a.z*b.y,a.z*b.x-a.x*b.z,a.x*b.y-a.y*b.x};\n}\n\n// 2012 ”N 1 ŒŽ 1 “ú 0:00 ‚©‚ç 2012 ”N mon ŒŽ day “ú hh:mm ‚܂ł̌o‰ßŽžŠÔ\ndouble calc_hours(int mon,int day,int hh,int mm){\n\tconst int a[]={-1,31,29,31,30,31,30,31,31,30,31,30,31};\n\tint d=day-1;\n\tfor(int i=1;i<mon;i++) d+=a[i];\n\treturn 24*d+hh+mm/60.0;\n}\n\n// ƒxƒNƒgƒ‹ p ‚ðŽ² a ‚Ü‚í‚è‚É t [rad] ‚¾‚¯‰ñ“]‚µ‚½ƒxƒNƒgƒ‹‚ð‹‚ß‚é\npoint3<double> rot(const point3<double> &p,const point3<double> &a,double t){\n\treturn cos(t)*p+(1-cos(t))*dot(p,a)*a+sin(t)*cross(a,p);\n}\n\nint n; // ¯‚̐”\ndouble lat[1000],lon[1000]; // ˆÜ“x, Œo“x\n\nbool check(double h){\n\t// x ޲³‚Ì•ûŒü 43.2 “x‚É–k‹É¯‚ª‚ ‚邯‚µ‚Ä‚¢‚é\n\tpoint3<double> a={cos(43.2*PI/180),0,sin(43.2*PI/180)}; // ‰ñ“]޲\n\trep(i,n){\n\t\t// ¯ i ‚Ì•ûŒüƒxƒNƒgƒ‹\n\t\tpoint3<double> p={cos(lon[i])*cos(lat[i]),sin(lon[i])*cos(lat[i]),sin(lat[i])};\n\t\tif(rot(p,a,(1+1/365.24)*2*PI*h/24).z<0) return false;\n\t}\n\treturn true;\n}\n\nint main(){\n\tint T; scanf(\"%d\",&T);\n\twhile(T--){\n\t\tint mon,day,hh,mm;\n\t\tscanf(\"%d/%d%d:%d\",&mon,&day,&hh,&mm);\n\t\tdouble h=calc_hours(mon,day,hh,mm);\n\n\t\tint q; scanf(\"%d\",&q);\n\t\twhile(q--){\n\t\t\tchar name[64];\n\t\t\tscanf(\"%s%d\",name,&n);\n\t\t\trep(i,n){\n\t\t\t\tscanf(\"%lf%lf\",lon+i,lat+i);\n\t\t\t\tlon[i]*=PI/180;\n\t\t\t\tlat[i]*=PI/180;\n\t\t\t}\n\t\t\tif(check(h)) puts(name);\n\t\t}\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 0, "score_of_the_acc": 0, "final_rank": 1 }, { "submission_id": "aoj_2210_328710", "code_snippet": "#include<iostream>\n#include<cmath>\n#include<complex>\n#include<iomanip>\n#include<cassert>\n#include<vector>\n#include<ctime>\n#include<string>\n#include<cstdio>\n \nusing namespace std;\n \nenum CCW{FRONT=0x01,RIGHT=0x02,BACK=0x04,LEFT=0x08,OVER=0x10};\ntypedef double elem;\ntypedef complex<elem> point2, vec2;\ntypedef pair<point2,point2> line2, seg2;\n \nconst elem eps = 1e-5;\nconst elem pi = 2*acos(0);\nconst elem infty = 1e50;\n\nstruct point3{\n elem x,y,z;\n point3(){}\n point3(elem x, elem y, elem z):x(x),y(y),z(z){}\n point3 operator+(const point3 &d)const{\n return point3(x+d.x,y+d.y,z+d.z);\n }\n point3 operator-(const point3 &d)const{\n return point3(x-d.x,y-d.y,z-d.z);\n }\n point3 operator*(elem d)const{\n return point3(x*d,y*d,z*d);\n }\n elem len()const{\n return sqrt(x*x + y*y + z*z);\n }\n point3 unit()const{\n double n = this->len();\n return point3(x/n,y/n,z/n);\n }\n elem dot(const point3 &d)const{\n return x*d.x + y*d.y + z*d.z;\n }\n point3 cross(const point3& d)const{\n return point3(y*d.z-z*d.y,z*d.x-x*d.z,x*d.y-y*d.x);\n }\n point3 rot(const point3 &u, double theta)\n {\n double cs = cos(theta);\n double ss = sin(theta);\n double r11 = cs + u.x * u.x * (1-cs);\n double r12 = u.x * u.y * (1-cs) - u.z * ss;\n double r13 = u.x * u.z * (1-cs) + u.y * ss;\n double r21 = u.y * u.x * (1-cs) + u.z * ss;\n double r22 = cs + u.y * u.y * (1-cs);\n double r23 = u.y * u.z * (1-cs) - u.x * ss;\n double r31 = u.z * u.x * (1-cs) - u.y * ss;\n double r32 = u.z * u.y * (1-cs) + u.x * ss;\n double r33 = cs + u.z * u.z * (1-cs);\n\n point3 ret;\n ret.x = r11 * x + r12 * y + r13 * z;\n ret.y = r21 * x + r22 * y + r23 * z;\n ret.z = r31 * x + r32 * y + r33 * z;\n return ret;\n }\n};\n\nstruct Stella{\n string name;\n vector<point3> v;\n void rot(const point3 &u, double theta){\n //cout << name << endl;\n for(int i = 0; i < (int)v.size(); ++i){\n point3 t = v[i].rot(u,theta);\n //cout << t.x << ' ' << t.y << ' ' << t.z << endl;\n v[i] = v[i].rot(u,theta);\n }\n }\n bool visible()const{\n for(int i = 0; i < (int)v.size(); ++i){\n if( v[i].z < 0 ) return false;\n }\n return true;\n }\n};\n\ndouble deg2rad(double deg){return deg/180.0 * pi;}\npoint2 rot(point2 p, elem theta){ return p * polar((elem)1.0, theta); }\npoint3 conv(double l,double r)\n{\n l = deg2rad(l);\n r = deg2rad(r);\n point3 t(0,1,0); // 0,0\n point3 ret = t.rot(point3(0,0,1),deg2rad(360-l));\n //point3 ret = t.rot(point3(0,0,1),deg2rad(l));\n ret = ret.rot(point3(1,0,0),deg2rad(r));\n return point3(sin(l)*cos(r),sin(l)*sin(r),cos(l));\n //return ret;\n}\n\nconst double unit24 = (1+1./365.24)*360;\n\nint main()\n{\n int T;\n cin >> T;\n for(int tc=1;tc<=T;++tc){\n int mon,day,h,m;\n char dam;\n struct tm time_base;\n struct tm time_cur;\n point3 pole = conv(90-43.2,90);\n \n time_base.tm_year = 2012 - 1900;\n time_base.tm_mon = 1 - 1;\n time_base.tm_mday = 1;\n time_base.tm_hour = 0;\n time_base.tm_min = 0;\n time_base.tm_sec = 0;\n time_base.tm_isdst = -1;\n \n cin >> mon >> dam >> day >> h >> dam >> m;\n // cout << mon << ' ' << day << ' ' << h << ' ' << m << endl;\n\n time_cur.tm_year = 2012 - 1900;\n time_cur.tm_mon = mon - 1;\n time_cur.tm_mday = day;\n time_cur.tm_hour = h;\n time_cur.tm_min = m;\n time_cur.tm_sec = 0;\n time_cur.tm_isdst = -1;\n \n time_t base1 = mktime(&time_base);\n time_t cur1 = mktime(&time_cur);\n \n double elapsed_days = (cur1-base1)/(3600.*24);\n // cout << elapsed_days << endl;\n \n int n;\n cin >> n;\n vector< Stella > vs;\n for(int i = 0; i < n; ++i){\n Stella s;\n int k;\n cin >> s.name >> k;\n for(int j = 0; j < k; ++j){\n double l,r;\n cin >> l >> r;\n s.v.push_back(conv(90-r,90-l));\n }\n vs.push_back(s);\n }\n\n //cout << vs.size() << endl;\n double degree = (unit24) * elapsed_days;\n // while(degree>360)degree -= 360;\n for(int i = 0; i < (int)vs.size(); ++i){\n vs[i].rot( pole, -deg2rad(degree) );\n }\n for(int i = 0; i < (int)vs.size(); ++i){\n if( vs[i].visible() ){\n cout << vs[i].name << endl;\n }\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 1100, "score_of_the_acc": -0.8944, "final_rank": 6 }, { "submission_id": "aoj_2210_328709", "code_snippet": "#include<iostream>\n#include<cmath>\n#include<complex>\n#include<iomanip>\n#include<cassert>\n#include<vector>\n#include<ctime>\n#include<string>\n#include<cstdio>\n \nusing namespace std;\n \nenum CCW{FRONT=0x01,RIGHT=0x02,BACK=0x04,LEFT=0x08,OVER=0x10};\ntypedef double elem;\ntypedef complex<elem> point2, vec2;\ntypedef pair<point2,point2> line2, seg2;\n \nconst elem eps = 1e-5;\nconst elem pi = 2*acos(0);\nconst elem infty = 1e50;\n\nstruct point3{\n elem x,y,z;\n point3(){}\n point3(elem x, elem y, elem z):x(x),y(y),z(z){}\n point3 operator+(const point3 &d)const{\n return point3(x+d.x,y+d.y,z+d.z);\n }\n point3 operator-(const point3 &d)const{\n return point3(x-d.x,y-d.y,z-d.z);\n }\n point3 operator*(elem d)const{\n return point3(x*d,y*d,z*d);\n }\n elem len()const{\n return sqrt(x*x + y*y + z*z);\n }\n point3 unit()const{\n double n = this->len();\n return point3(x/n,y/n,z/n);\n }\n elem dot(const point3 &d)const{\n return x*d.x + y*d.y + z*d.z;\n }\n point3 cross(const point3& d)const{\n return point3(y*d.z-z*d.y,z*d.x-x*d.z,x*d.y-y*d.x);\n }\n point3 rot(const point3 &u, double theta)\n {\n double cs = cos(theta);\n double ss = sin(theta);\n double r11 = cs + u.x * u.x * (1-cs);\n double r12 = u.x * u.y * (1-cs) - u.z * ss;\n double r13 = u.x * u.z * (1-cs) + u.y * ss;\n double r21 = u.y * u.x * (1-cs) + u.z * ss;\n double r22 = cs + u.y * u.y * (1-cs);\n double r23 = u.y * u.z * (1-cs) - u.x * ss;\n double r31 = u.z * u.x * (1-cs) - u.y * ss;\n double r32 = u.z * u.y * (1-cs) + u.x * ss;\n double r33 = cs + u.z * u.z * (1-cs);\n\n point3 ret;\n ret.x = r11 * x + r12 * y + r13 * z;\n ret.y = r21 * x + r22 * y + r23 * z;\n ret.z = r31 * x + r32 * y + r33 * z;\n return ret;\n }\n};\n\nstruct Stella{\n string name;\n vector<point3> v;\n void rot(const point3 &u, double theta){\n //cout << name << endl;\n for(int i = 0; i < (int)v.size(); ++i){\n point3 t = v[i].rot(u,theta);\n //cout << t.x << ' ' << t.y << ' ' << t.z << endl;\n v[i] = v[i].rot(u,theta);\n }\n }\n bool visible()const{\n for(int i = 0; i < (int)v.size(); ++i){\n if( v[i].z < 0 ) return false;\n }\n return true;\n }\n};\n\ndouble deg2rad(double deg){return deg/180.0 * pi;}\npoint2 rot(point2 p, elem theta){ return p * polar((elem)1.0, theta); }\npoint3 conv(double l,double r)\n{\n l = deg2rad(l);\n r = deg2rad(r);\n point3 t(0,1,0); // 0,0\n point3 ret = t.rot(point3(0,0,1),deg2rad(360-l));\n //point3 ret = t.rot(point3(0,0,1),deg2rad(l));\n ret = ret.rot(point3(1,0,0),deg2rad(r));\n return point3(cos(l)*cos(r),sin(l)*cos(r),sin(r));\n //return ret;\n}\n\nconst double unit24 = (1+1./365.24)*360;\n\nint main()\n{\n int T;\n cin >> T;\n for(int tc=1;tc<=T;++tc){\n int mon,day,h,m;\n char dam;\n struct tm time_base;\n struct tm time_cur;\n point3 pole = conv(180,43.2);\n \n time_base.tm_year = 2012 - 1900;\n time_base.tm_mon = 1 - 1;\n time_base.tm_mday = 1;\n time_base.tm_hour = 0;\n time_base.tm_min = 0;\n time_base.tm_sec = 0;\n time_base.tm_isdst = -1;\n \n cin >> mon >> dam >> day >> h >> dam >> m;\n // cout << mon << ' ' << day << ' ' << h << ' ' << m << endl;\n\n time_cur.tm_year = 2012 - 1900;\n time_cur.tm_mon = mon - 1;\n time_cur.tm_mday = day;\n time_cur.tm_hour = h;\n time_cur.tm_min = m;\n time_cur.tm_sec = 0;\n time_cur.tm_isdst = -1;\n \n time_t base1 = mktime(&time_base);\n time_t cur1 = mktime(&time_cur);\n \n double elapsed_days = (cur1-base1)/(3600.*24);\n // cout << elapsed_days << endl;\n \n int n;\n cin >> n;\n vector< Stella > vs;\n for(int i = 0; i < n; ++i){\n Stella s;\n int k;\n cin >> s.name >> k;\n for(int j = 0; j < k; ++j){\n double l,r;\n cin >> l >> r;\n s.v.push_back(conv(180-l,r));\n }\n vs.push_back(s);\n }\n\n //cout << vs.size() << endl;\n double degree = (unit24) * elapsed_days;\n // while(degree>360)degree -= 360;\n for(int i = 0; i < (int)vs.size(); ++i){\n vs[i].rot( pole, -deg2rad(degree) );\n }\n for(int i = 0; i < (int)vs.size(); ++i){\n if( vs[i].visible() ){\n cout << vs[i].name << endl;\n }\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 1100, "score_of_the_acc": -0.8944, "final_rank": 6 }, { "submission_id": "aoj_2210_328659", "code_snippet": "#include<iostream>\n#include<vector>\n#include<cmath>\nusing namespace std;\n#define REP(i,b,n) for(int i=b;i<n;i++)\n#define rep(i,n) REP(i,0,n)\ntypedef long long ll;\nconst ll mod = 1000000;\nconst double pi = acos(-1);\ndouble polar;\n\nvoid rotate(double &x,double &y,double theta){\n double tx=x,ty=y;\n x=tx*cos(theta)-ty*sin(theta);\n y=tx*sin(theta)+ty*cos(theta);\n}\n\ndouble getAngle(int month,int day,int hour,int minute){\n static double unit=((1+1/365.24)*360)/(24*60);\n static int daylist[]={31,29,31,30,31,30,31,31,30,31,30,31};\n double daytotal=0;\n rep(i,12){\n if (month == i+1)break;\n daytotal+=daylist[i];\n }\n daytotal+=day-1;\n return (daytotal*60*24+hour*60+minute)*unit;\n}\n\nbool isok(vector<double> &x,vector<double> &y,vector<double> &z,double angle){\n int n=x.size();\n rep(i,n){\n rotate(x[i],z[i],pi/2-polar);\n }\n rep(i,n){\n rotate(x[i],y[i],angle);\n }\n rep(i,n){\n rotate(x[i],z[i],-(pi/2-polar));\n }\n //rep(i,n)cout << z[i] <<\" \";cout << endl;\n\n rep(i,n){\n double ang=asin(z[i]);\n if (ang < 0.0)return false;\n }\n return true;\n}\n\nmain(){\n polar=43.2/180*pi;\n int te;\n cin>>te;\n while(te--){\n int month,day,hour,minute;\n char dummy;\n cin>>month>>dummy>>day>>hour>>dummy>>minute;\n double angle=getAngle(month,day,hour,minute);\n angle=angle/180*pi;\n int n;\n cin>>n;\n rep(i,n){\n string name;int m;\n cin>>name>>m;\n vector<double> x(m),y(m),z(m);\n rep(j,m){\n\tdouble ang,h;\n\tcin>>ang>>h;\n\tang=ang;\n\tang=ang/180*pi;\n\th=h/180*pi;\n\tz[j]=sin(h);\n\tx[j]=cos(h)*cos(ang);\n\ty[j]=cos(h)*sin(ang);\n\t//cout << sqrt(x[j]*x[j]+y[j]*y[j]+z[j]*z[j]) << endl;\n }\n //cout <<\"check \" << name << endl;\n if (isok(x,y,z,angle))cout << name << endl;\n }\n //cout <<\"tc end \" << endl << endl;\n }\n return false;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 988, "score_of_the_acc": -1.0645, "final_rank": 9 }, { "submission_id": "aoj_2210_275265", "code_snippet": "#include <iostream>\n#include <cstdio>\n#include <vector>\n#include <algorithm>\n#include <complex>\n#include <queue>\n#include <map>\n#include <set>\n#include <cstring>\n#include <cstdlib>\n#include <string>\n#include <cmath>\n#include <valarray>\nusing namespace std;\n\n#define REP(i,n) for(int i=0;i<(int)n;++i)\n#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)\n#define ALL(c) (c).begin(), (c).end()\nconst int INF = 1<<29;\nconst double PI = 3.1415926535;\n\ndouble rad(double d) {\n return d*PI/180;\n}\n\ntypedef double number;\ntypedef valarray<number> point;\n\npoint coord(double R, double lon, double lat) {\n point res(3);\n res[0] = R*cos(lon)*sin(lat);\n res[1] = R*sin(lon)*sin(lat);\n res[2] = R*cos(lat);\n return res;\n}\nnumber dot(const point& a, const point& b) {\n return (a * b).sum();\n}\npoint cross(const point& a, const point& b) {\n return a.cshift(+1)*b.cshift(-1)\n - a.cshift(-1)*b.cshift(+1);\n}\n// ツベツクツトツδ仰づ慊づュツづィツづ個嘉アツ転\npoint rotate(point axis, double ang, point a) {\n return a * cos(ang) + (1.0 - cos(ang)) * dot(a, axis) * axis + cross(axis, a) * sin(ang);\n}\n\nbool judge(double a, double h, double ang) {\n return (rotate(coord(1, 0, rad(46.8)), -ang, coord(10,-a,h))[2] >= 0);\n}\n\nint main() {\n int month[12] = {31,29,31,30,31,30,31,31,30,31,30,31};\n int T;\n cin >> T;\n while(T--) {\n int mon,day,hh,mm;\n scanf(\"%d/%d %d:%d\",&mon,&day,&hh,&mm);\n int min = 0;\n REP(i, mon-1)\n min += month[i] * 24 * 60;\n min += (day-1) * 24 * 60 + hh * 60 + mm;\n double ang = rad((1.0+1.0/365.24) * 360 * min / (24 * 60));\n// cout << min << \" \" << ang << endl;\n //ang = mod(ang, (2*PI));\n int n;\n cin >> n;\n REP(i,n) {\n string name;\n cin >> name;\n int m;\n cin >> m;\n bool f = 1;\n REP(j,m) {\n double a,b;\n cin >> a >> b;\n b = 90.0 - b;\n if (!judge(rad(a), rad(b), ang)) {\n f = 0;\n }\n }\n if (f)\n cout << name <<endl;\n }\n }\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 1008, "score_of_the_acc": -1.0698, "final_rank": 10 }, { "submission_id": "aoj_2210_275191", "code_snippet": "#include <iostream>\n#include <cstdio>\n#include <vector>\n#include <algorithm>\n#include <complex>\n#include <queue>\n#include <map>\n#include <set>\n#include <cstring>\n#include <cstdlib>\n#include <string>\n#include <cmath>\nusing namespace std;\n\n#define REP(i,n) for(int i=0;i<(int)n;++i)\n#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)\n#define ALL(c) (c).begin(), (c).end()\nconst int INF = 1<<29;\nconst double PI = 3.1415926535;\n\ndouble rad(double d) {\n return d*PI/180;\n}\nstruct P {\n double x,y,z;\n P(double x, double y, double z) :x(x), y(y), z(z) {}\n};\n// Œo“x‚ƈܓx‚©‚çÀ•W‚ðŒvŽZB‹…‚Ì’†S‚ªŒ´“_B\n// lon : ‚O“x‚©‚瓌‚Ü‚í‚è\n// lat : –k‹É‚©‚ç“ì‹É•ûŒü\n// Œo“x‚OˆÜ“x‚O•ûŒü‚ª‚˜Ž²A“ŒŒo‚X‚OˆÜ“x‚O‚ª‚™Ž²@–k‹É‚ª‚šŽ²\ndouble R = 10;\nP coord(double lon, double lat) {\n double x = R*cos(lon)*sin(lat);\n double y = R*sin(lon)*sin(lat);\n double z = R*cos(lat);\n return P(x,y,z);\n}\n// ŽOŽŸŒ³‚̉ñ“]s—ñ‚ð—p‚¢‚½Še޲‰ñ‚è‚̉ñ“]\nP rotatex(P a, double ang) {\n return P(a.x, a.y*cos(ang)-a.z*sin(ang), a.y*sin(ang)+a.z*cos(ang));\n}\nP rotatey(P a, double ang) {\n return P(a.x*cos(ang)+a.z*sin(ang), a.y, -a.x*sin(ang)+a.z*cos(ang));\n}\nP rotatez(P a, double ang) {\n return P(a.x*cos(ang)-a.y*sin(ang), a.x*sin(ang)+a.y*cos(ang), a.z);\n}\n// lon, lat ‚Íã‚Æ“¯‚¶\nP rotate(P a, double lon, double lat, double ang) {\n P tmp = rotatey(rotatez(a,-lon),-lat);\n tmp = rotatez(tmp, -ang);\n tmp = rotatez(rotatey(tmp, lat), lon);\n return tmp;\n}\nbool judge(double a, double h, double ang) {\n return (rotate(coord(-a,h), 0, rad(46.8), ang).z >= 0);\n}\n\nint main() {\n int month[12] = {31,29,31,30,31,30,31,31,30,31,30,31};\n int T;\n cin >> T;\n while(T--) {\n int mon,day,hh,mm;\n scanf(\"%d/%d %d:%d\",&mon,&day,&hh,&mm);\n int min = 0;\n REP(i, mon-1)\n min += month[i] * 24 * 60;\n min += (day-1) * 24 * 60 + hh * 60 + mm;\n double ang = rad((1.0+1.0/365.24) * 360 * min / (24 * 60));\n// cout << min << \" \" << ang << endl;\n //ang = mod(ang, (2*PI));\n int n;\n cin >> n;\n REP(i,n) {\n string name;\n cin >> name;\n int m;\n cin >> m;\n bool f = 1;\n REP(j,m) {\n double a,b;\n cin >> a >> b;\n b = 90.0 - b;\n if (!judge(rad(a), rad(b), ang)) {\n f = 0;\n }\n }\n if (f)\n cout << name <<endl;\n }\n }\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 1004, "score_of_the_acc": -0.8687, "final_rank": 5 }, { "submission_id": "aoj_2210_224999", "code_snippet": "#include<stdio.h>\n#include <math.h>\n#include <vector>\n#include <string>\n\nstd::vector<std::string> ans;\n\nvoid starCalc(int n,double round){\n\tint m;\n\tchar name[51];\n\tdouble er,nr,x,y,z,x1,y1,z1;\n\tdouble c1,s1;\n\tdouble c2,s2;\n\tc1=cos(43.2/180.0*M_PI);\n\ts1=sin(43.2/180.0*M_PI);\n\tc2=cos(round/180.0*M_PI);\n\ts2=sin(round/180.0*M_PI);\n\t\n\tbool seeInOK;\n\tfor(int i=0;i<n;i++){\n\t\tscanf(\"%s %d\",name,&m);\n\t\tseeInOK=true;\n\t\tfor(int j=0;j<m;j++){\n\t\t\tscanf(\"%lf %lf\",&er,&nr);\n\t\t\tif(seeInOK==false) continue;\n\t\t\tx=cos(nr/180.0*M_PI);\n\t\t\tz=sin(nr/180.0*M_PI);\n\t\t\ty=0;\n\t\t\t\n\t\t\tx1=x*cos(er/180.0*M_PI);\n\t\t\ty1=-x*sin(er/180.0*M_PI);\n\t\t\tz1=z;\n\t\t\tx=x1;y=y1;z=z1;\n\t\t\t\n\t\t\tx1=x*c1+z*s1;\n\t\t\ty1=y;\n\t\t\tz1=-x*s1+c1*z;\n\t\t\t//printf(\"(a %lf %lf %lf %lf)\\n\",x1,y1,z1,x1*x1+y1*y1+z1*z1);\n\t\t\t\n\t\t\tx=x1;\n\t\t\ty=y1*c2+z1*s2;\n\t\t\tz=-y1*s2+z1*c2;\n\t\t\t//printf(\"(b %lf %lf %lf %lf)\\n\",x,y,z,x*x+y*y+z*z);\n\t\t\t\n\t\t\t//x1=c1*x-s1*z;\n\t\t\t//y1=y;\n\t\t\tz1=s1*x+z*c1;\n\t\t\t//printf(\"<%lf %lf %lf %lf>\\n\",x1,y1,z1,x1*x1+y1*y1+z1*z1);\n\t\t\tif(z1<0) seeInOK=false;\n\t\t}\n\t\tif(seeInOK==true){\n\t\t\tans.push_back(name);\n\t\t}\n\t}\n}\n\n\nint main(){\n\tint mon,dd,hh,mm;\n\tdouble time,round;\n\tint mons[12]={0,31,60,91,121,152,182,213,244,274,305,335};\n\tint n,m;\n\tscanf(\"%d\",&n);\n\tfor(int i=0;i<n;i++){\n\t\tscanf(\"%d/%d %d:%d\",&mon,&dd,&hh,&mm);\n\t\tscanf(\"%d\",&m);\n\t\ttime=mons[mon-1]+dd+hh/24.0+mm/(60.0*24.0)-1.0;\n\t\tround=time*(1+1/365.24)*360.0;\n\t\tstarCalc(m,round);\n\t}\n\tstd::vector<std::string>::iterator it=ans.begin();\n\twhile(it!=ans.end()){\n\t\tprintf(\"%s\\n\",(*it).c_str());\n\t\tit++;\n\t}\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 868, "score_of_the_acc": -0.2323, "final_rank": 2 }, { "submission_id": "aoj_2210_101353", "code_snippet": "#include <stdio.h>\n#include <string.h>\n#include <algorithm>\n#include <iostream>\n#include <math.h>\n#include <assert.h>\n#include <vector>\n\nusing namespace std;\ntypedef long long ll;\nstatic const double EPS = 1e-9;\nstatic const double PI = acos(-1.0);\n\n#define REP(i, n) for (int i = 0; i < (int)(n); i++)\n#define FOR(i, s, n) for (int i = (s); i < (int)(n); i++)\n#define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++)\n#define FORIT(it, c) for (__typeof((c).begin())it = (c).begin(); it != (c).end(); it++)\n#define MEMSET(v, h) memset((v), h, sizeof(v))\n\ntypedef vector<double> Array;\ntypedef vector<Array> Matrix;\n\nArray mul(const Matrix &matrix, const Array &vect) {\n const int w = matrix[0].size();\n const int h = matrix.size();\n Array ret(h, 0);\n REP(y, h) {\n REP(x, w) {\n ret[y] += matrix[y][x] * vect[x];\n }\n }\n return ret;\n}\n\nMatrix mul(const Matrix &matrix1, const Matrix &matrix2) {\n const int in = matrix1[0].size();\n const int h = matrix1.size();\n const int w = matrix2[0].size();\n Matrix ret(h, Array(w, 0));\n REP(y, h) {\n REP(x, w) {\n REP(i, in) {\n ret[y][x] += matrix1[y][i] * matrix2[i][x];\n }\n }\n }\n return ret;\n}\n\nArray XRotate(const Array vect, const double rad) {\n Matrix matrix(3, Array(3, 0));\n matrix[0][0] = 1.0;\n matrix[1][1] = cos(rad);\n matrix[1][2] = sin(rad);\n matrix[2][1] = -sin(rad);\n matrix[2][2] = cos(rad);\n return mul(matrix, vect);\n}\n\nArray YRotate(const Array vect, const double rad) {\n Matrix matrix(3, Array(3, 0));\n matrix[1][1] = 1.0;\n matrix[0][0] = cos(rad);\n matrix[0][2] = -sin(rad);\n matrix[2][0] = sin(rad);\n matrix[2][2] = cos(rad);\n return mul(matrix, vect);\n}\n\nArray ZRotate(const Array vect, const double rad) {\n Matrix matrix(3, Array(3, 0));\n matrix[2][2] = 1.0;\n matrix[0][0] = cos(rad);\n matrix[0][1] = sin(rad);\n matrix[1][0] = -sin(rad);\n matrix[1][1] = cos(rad);\n return mul(matrix, vect);\n}\n\nArray normalize(const Array vect) {\n const int n = vect.size();\n double sum = 0;\n Array ret(n, 0);\n REP(i, n) {\n sum += vect[i] * vect[i];\n }\n sum = sqrt(sum);\n REP(i, n) {\n ret[i] = vect[i] / sum;\n }\n return ret;\n}\n\nchar str[1000];\n\nconst int month[12] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };\nint main() {\nint test;\nscanf(\"%d\", &test);\nwhile (test--) {\n int m, d, h, minute;\n scanf(\"%d/%d %d:%d\", &m, &d, &h, &minute);\n REP(i, m - 1) {\n d += month[i];\n }\n d--;\n h += d * 24;\n minute += h * 60;\n double rad = (1.0 + 1.0 / 365.24) * 360.0 * minute / 60.0 / 24.0 * 2 * PI / 360.0;\n int n;\n scanf(\"%d\", &n);\n Array star(3);\n REP(i, n) {\n int m;\n scanf(\"%s %d\", str, &m);\n bool see = true;\n REP(j, m) {\n double a, h;\n scanf(\"%lf %lf\", &a, &h);\n a = (90 - a) * PI / 180.0;\n h = h * PI / 180.0;\n star[2] = sin(h);\n double d = sqrt(1 - star[2] * star[2]);\n star[0] = d * cos(a);\n star[1] = d * sin(a);\n star = XRotate(star, -(90.0 - 43.2) * PI / 180.0);\n star = ZRotate(star, rad);\n star = XRotate(star, -(43.2 - 90.0) * PI / 180.0);\n if (star[2] < 0) { see = false; }\n }\n if (see) { printf(\"%s\\n\", str); }\n }\n}\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 952, "score_of_the_acc": -1.2548, "final_rank": 11 } ]
aoj_2209_cpp
Problem F: UTF-8 UTF-8はマルチバイト文字を符号化する手法の一つである。 文字は計算機上ではバイト列として扱われる。英語だけであればラテンアルファベットおよび数字、記号を合わせても 1byteで表現できるが、残念ながら世界中で利用されている文字を1byteで表現することはできないため、複数のバイトを用いて文字を表す必要がある。 ここで例えば"あ"という文字に12354(0x3042)という2バイト列を割り当てたとき、そのままバイト列を並べると0x30,0x42と2文字なのか0x3042で1文字なのかが区別がつかない。 このため、マルチバイト文字符号化方式のUTF-8では始めの1バイト目で続くバイト列の長さが分かるようにすることにより、この問題を克服している。具体的なビットパターンは以下のようになる。 バイト長 ビットパターン 1 0xxxxxxx 2 110yyyyx 10xxxxxx 3 1110yyyy 10yxxxxx 10xxxxxx 4 11110yyy 10yyxxxx 10xxxxxx 10xxxxxx ここでxは0/1の任意のビットとする。またyも0/1の任意のビットとなるがどれか一つは1である必要がある。また全ての文字は1,2,3,4バイト文字のいずれかであるとする。 ここでyのビットが少なくとも一つは1であるという制約は以下のような理由による。 例えば'/'(0x2f)をバイト列にエンコードする際に0x2fと1バイトで符号化するないしは0xc0 0xafと2バイトで符号化する方法の二つがあるが、このような曖昧性を許すとセキュリティ上の脅威のもととなる可能性があるためである。 以上の話を聞いたあなたはUTF-8がどの程度の自由度があるのかが気になった。具体的には与えられたバイト列の一部のビットが不明な時にUTF-8として可能なバイト列の組み合わせがどの程度あるかということである。 このとき可能なバイト列の組み合わせの個数を求め、1,000,000で割った余りを出力せよ。 Input N b 1 b 2 ... b N Nはバイト列の個数, b i はバイト列である。 1 ≤ N ≤ 1000 を満たしている。 b i は記号{0/1/x}が8個並んでいる。xはビットが不明であることを示している。 Output 可能なバイト列の組み合わせの個数を1,000,000で割った余りを出力せよ。 Notes on Test Cases 上記入力形式で複数のデータセットが与えられます。各データセットに対して上記出力形式で出力を行うプログラムを作成して下さい。 N が 0 のとき入力の終わりを示します。 Sample Input 1 xxxxxxxx 3 11100000 10x00000 10111111 3 11100000 10000000 10111111 4 xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx 0 Output for Sample Input 128 1 0 778240
[ { "submission_id": "aoj_2209_3699896", "code_snippet": "#include <bits/stdc++.h>\n#define r(i,n) for(int i=0;i<n;i++)\n#define int long long\nusing namespace std;\ntypedef pair<int,int>P;\ntypedef pair<int,P>P2;\n#define F first\n#define S second\n\nint calc1(string s){\n\tif(s[0]=='1')return 0;\n\tint x=1;\n\tr(i,7){\n\t\tif(s[i+1]=='x')x*=2;\n\t}\n\treturn x;\n}\n\nint calc2(string s){\n\tint y=0;\n\tstring p=\"110yyyyx10xxxxxx\";\n\tr(i,16){\n\t\tif(p[i]=='y'&&(s[i]=='1'||s[i]=='x')) y=1;\n\t\tif(isdigit(p[i])&&isdigit(s[i])&&p[i]!=s[i])return 0;\n\t}\n\tif( !y )return 0;\n\tint dp[33][2]={};\n\tdp[0][0]=1;\n\tr(i,16){\n\t\tr(j,2){\n\t\t\tif(isdigit(p[i])){\n\t\t\t\tdp[i+1][j] += dp[i][j];\n\t\t\t}\n\t\t\tif(p[i]=='x'){\n\t\t\t\tint x=(isdigit(s[i])?1:2);\n\t\t\t\tdp[i+1][j] += dp[i][j]*x;\n\t\t\t}\n\t\t\tif(p[i]=='y'){\n\t\t\t\tif(s[i]=='1'){\n\t\t\t\t\tdp[i+1][1] += dp[i][j];\n\t\t\t\t}\n\t\t\t\tif(s[i]=='0'){\n\t\t\t\t\tdp[i+1][j] += dp[i][j];\n\t\t\t\t}\n\t\t\t\tif(s[i]=='x'){\n\t\t\t\t\tdp[i+1][1] += dp[i][j];\n\t\t\t\t\tdp[i+1][j] += dp[i][j];\n\t\t\t\t}\n\t\t\t}\n\t\t\tdp[i+1][j] %= 1000000;\n\t\t}\n\t}\n\treturn dp[16][1];\n}\n\nint calc3(string s){\n\tint y=0;\n\tstring p=\"1110yyyy10yxxxxx10xxxxxx\";\n\tr(i,24){\n\t\tif(p[i]=='y'&&(s[i]=='1'||s[i]=='x')) y=1;\n\t\t//cout<<i<<endl;\n\t\tif(isdigit(p[i])&&isdigit(s[i])&&p[i]!=s[i])return 0;\n\t}\n\t//cout<<y<<endl;\n\tif( !y )return 0;\n\tint dp[33][2]={};\n\tdp[0][0]=1;\n\tr(i,24){\n\t\tr(j,2){\n\t\t\tif(isdigit(p[i])){\n\t\t\t\tdp[i+1][j] += dp[i][j];\n\t\t\t}\n\t\t\tif(p[i]=='x'){\n\t\t\t\tint x=(isdigit(s[i])?1:2);\n\t\t\t\tdp[i+1][j] += dp[i][j]*x;\n\t\t\t}\n\t\t\tif(p[i]=='y'){\n\t\t\t\tif(s[i]=='1'){\n\t\t\t\t\tdp[i+1][1] += dp[i][j];\n\t\t\t\t}\n\t\t\t\tif(s[i]=='0'){\n\t\t\t\t\tdp[i+1][j] += dp[i][j];\n\t\t\t\t}\n\t\t\t\tif(s[i]=='x'){\n\t\t\t\t\tdp[i+1][1] += dp[i][j];\n\t\t\t\t\tdp[i+1][j] += dp[i][j];\n\t\t\t\t}\n\t\t\t}\n\t\t\tdp[i+1][j] %= 1000000;\n\t\t}\n\t}\n\treturn dp[24][1];\n}\n\nint calc4(string s){\n\tint y=0;\n\tstring p=\"11110yyy10yyxxxx10xxxxxx10xxxxxx\";\n\tr(i,32){\n\t\tif(p[i]=='y'&&(s[i]=='1'||s[i]=='x')) y=1;\n\t\tif(isdigit(p[i])&&isdigit(s[i])&&p[i]!=s[i])return 0;\n\t}\n\tif( !y )return 0;\n\tint dp[33][2]={};\n\tdp[0][0]=1;\n\tr(i,32){\n\t\tr(j,2){\n\t\t\tif(isdigit(p[i])){\n\t\t\t\tdp[i+1][j] += dp[i][j];\n\t\t\t}\n\t\t\tif(p[i]=='x'){\n\t\t\t\tint x=(isdigit(s[i])?1:2);\n\t\t\t\tdp[i+1][j] += dp[i][j]*x;\n\t\t\t}\n\t\t\tif(p[i]=='y'){\n\t\t\t\tif(s[i]=='1'){\n\t\t\t\t\tdp[i+1][1] += dp[i][j];\n\t\t\t\t}\n\t\t\t\tif(s[i]=='0'){\n\t\t\t\t\tdp[i+1][j] += dp[i][j];\n\t\t\t\t}\n\t\t\t\tif(s[i]=='x'){\n\t\t\t\t\tdp[i+1][1] += dp[i][j];\n\t\t\t\t\tdp[i+1][j] += dp[i][j];\n\t\t\t\t}\n\t\t\t}\n\t\t\tdp[i+1][j] %= 1000000;\n\t\t}\n\t}\n\treturn dp[32][1];\n}\n\nint n;\nstring s[1111];\nint dp[1111];\n\nsigned main(){\n\twhile(cin>>n,n){\n\t\tr(i,n)cin>>s[i];\n\t\tmemset(dp,0,sizeof(dp));\n\t\tdp[0] = 1;\n\t\tfor(int i=0;i<n;i++){\n\t\t\tif( i+1 <= n ){\n\t\t\t\tint x = calc1(s[i]);\n\t\t\t\tdp[i+1] += dp[i]*x;\n\t\t\t\tdp[i+1] %= 1000000;\n\t\t\t}\n\t\t\tif( i+2 <= n ){\n\t\t\t\tint x = calc2(s[i]+s[i+1]);\n\t\t\t\tdp[i+2] += dp[i]*x;\n\t\t\t\tdp[i+2] %= 1000000;\n\t\t\t}\n\t\t\tif( i+3 <= n ){\n\t\t\t\tint x = calc3(s[i]+s[i+1]+s[i+2]);\n\t\t\t\t//cout<<x<<endl;\n\t\t\t\tdp[i+3] += dp[i]*x;\n\t\t\t\tdp[i+3] %= 1000000;\n\t\t\t}\n\t\t\tif( i+4 <= n ){\n\t\t\t\tint x = calc4(s[i]+s[i+1]+s[i+2]+s[i+3]);\n\t\t\t\tdp[i+4] += dp[i]*x;\n\t\t\t\tdp[i+4] %= 1000000;\n\t\t\t}\n\t\t}\n\t\tcout<<dp[n]<<endl;\n\t}\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3144, "score_of_the_acc": -0.1983, "final_rank": 13 }, { "submission_id": "aoj_2209_3699886", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing Int = long long;\nusing ll = long long;\n\ntemplate<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b)a=b;};\ntemplate<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b)a=b;};\n\nconst Int MOD = 1e6;\nconst Int MAX = 1024;\nInt n;\nInt dp[5][4][2][MAX];\nstring ss[MAX];\n\nInt check(Int v,string s){\n for(Int i=0;i<8;i++){\n if((s[i]!='x')&&((s[i]-'0')!=(v&1))) return 0;\n v>>=1;\n }\n return 1;\n}\n\nInt rf[5][4],mc[5][4],ms[5][4];\nInt match(Int a,Int b,Int l){\n return (a>>(8-l))==(b>>(8-l));\n}\n\nInt dfs(Int len,Int i,Int y,Int p){\n if(p==n) return (len==1&&i==0&&y==1);\n Int &res=dp[len][i][y][p];\n if(~res) return res;\n res=0;\n\n for(Int bit=0;bit<256;bit++){\n if(!check(bit,ss[p])) continue;\n if(!match(bit,rf[len][i],mc[len][i])) continue;\n if(i+1==len){\n if(y){\n for(Int nxt=1;nxt<=4;nxt++){\n Int ni=0;\n Int ny=(nxt==1);\n Int np=p+1;\n res+=dfs(nxt,ni,ny,np);\n res%=MOD;\n }\n }\n }else{\n Int ni=i+1;\n Int ny=y||(ms[len][i]&bit);\n Int np=p+1;\n res+=dfs(len,ni,ny,np);\n res%=MOD;\n }\n }\n return res;\n}\n\nsigned main(){\n cin.tie(0);\n ios::sync_with_stdio(0);\n\n rf[1][0]=0b00000000;\n\n rf[2][0]=0b11000000;\n rf[2][1]=0b10000000;\n\n rf[3][0]=0b11100000;\n rf[3][1]=0b10000000;\n rf[3][2]=0b10000000;\n\n rf[4][0]=0b11110000;\n rf[4][1]=0b10000000;\n rf[4][2]=0b10000000;\n rf[4][3]=0b10000000;\n\n mc[1][0]=1;\n\n mc[2][0]=3;\n mc[2][1]=2;\n\n mc[3][0]=4;\n mc[3][1]=2;\n mc[3][2]=2;\n\n mc[4][0]=5;\n mc[4][1]=2;\n mc[4][2]=2;\n mc[4][3]=2;\n\n ms[1][0]=0b00000000;\n\n ms[2][0]=0b00011110;\n ms[2][1]=0b00000000;\n\n ms[3][0]=0b00001111;\n ms[3][1]=0b00100000;\n ms[3][2]=0b00000000;\n\n ms[4][0]=0b00000111;\n ms[4][1]=0b00110000;\n ms[4][2]=0b00000000;\n ms[4][3]=0b00000000;\n\n while(cin>>n,n){\n memset(dp,-1,sizeof(dp));\n for(Int i=0;i<n;i++){\n cin>>ss[i];\n reverse(ss[i].begin(),ss[i].end());\n }\n Int ans=0;\n for(Int len=1;len<=4;len++){\n ans+=dfs(len,0,(len==1),0);\n ans%=MOD;\n }\n cout<<ans<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1030, "memory_kb": 3632, "score_of_the_acc": -1.2468, "final_rank": 20 }, { "submission_id": "aoj_2209_3699533", "code_snippet": "#include<bits/stdc++.h>\n#define LL long long\n#define REP(i,n) for(int i=0;i<(n);++i)\n#define REPA(i,n) for(int i=1;i<(n);++i)\n#define PII pair<int,int>\n#define PLI pair<long long, int>\n#define PLL pair<long long, long long>\n#define MOD ((int)1e6)\n#define INF ((int)1e9)\n#define INFLL ((LL)1e18)\n#define ALL(x) (x).begin(),(x).end()\n#define ctoi(x) (x - 'a') \n#define CTOI(x) (x - 'A')\n#define BIT(x) (1 << (x))\n#define PI (acos(-1))\nusing namespace std;\n\nLL modinv(LL a){\n LL b = MOD, u = 1, v = 0;\n while(b){\n LL t = a / b;\n a -= t * b;swap(a, b);\n u -= t * v;swap(u, v);\n }\n u%=MOD;\n if(u < 0) u += MOD;\n return u;\n\n}\nLL exp(int a, int b){\n LL res = 1;\n LL sum = a;\n while(b){\n if(b&1)res = (res * sum) % MOD;\n sum = (sum * sum) % MOD;\n b>>=1;\n }\n return res;\n}\nint N;\nvector<string> bytes;\nvector<int> dp;\nLL count(string a, string b){\n bool check=false;\n for(auto i:b)if(i=='y'){\n check=true;\n break;\n }\n if(check){\n bool flag = false;\n int xc = 0;\n int yc = 0;\n REP(i, a.size()){\n if(b[i]=='1'){\n if(a[i]=='0')return 0;\n }\n if(b[i]=='0'){\n if(a[i]=='1')return 0;\n }\n if(b[i]=='x'){\n if(a[i]=='x')++xc;\n }\n if(b[i]=='y'){\n if(a[i]=='1')flag=true;\n if(a[i]=='x')++yc;\n }\n }\n if(flag){\n return exp(2, xc + yc);\n }else{\n if(yc==0)return 0;\n return (exp(2, yc) - 1) * exp(2, xc);\n }\n }else{\n int count = 0;\n REP(i, a.size()){\n if(b[i]=='x'){\n if(a[i]=='x')++count;\n }else{\n if((a[i]!='x') and (a[i] != b[i]))return 0;\n }\n }\n return exp(2, count);\n }\n} \n\nLL func(int place){\n if(place == N){\n return 1;\n }\n if(dp[place]>=0)return dp[place];\n LL res = (LL)func(place+1) * count(bytes[place], \"0xxxxxxx\");\n res %= MOD;\n if(place+2 <= N){\n res += (LL)func(place+2) * count(bytes[place]+bytes[place+1], \"110yyyyx10xxxxxx\");\n res %= MOD;\n }\n if(place+3 <= N){\n string str=\"\";\n for(int i=0;i<3;++i)str += bytes[place+i];\n res += (LL)func(place+3) * count(str, \"1110yyyy10yxxxxx10xxxxxx\");\n res %= MOD;\n }\n if(place+4 <= N){\n string str=\"\";\n for(int i=0;i<4;++i)str += bytes[place+i];\n res += (LL)func(place+4) * count(str, \"11110yyy10yyxxxx10xxxxxx10xxxxxx\");\n res %= MOD;\n }\n return dp[place] = res;\n}\n\nint main(){\n while(cin >> N && N){\n bytes = vector<string>(N);\n for(auto &i:bytes)cin >> i;\n dp = vector<int>(N, -1);\n cout << func(0) << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3288, "score_of_the_acc": -0.2224, "final_rank": 14 }, { "submission_id": "aoj_2209_3699348", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing Int = long long;\nusing ll = long long;\n\ntemplate<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b)a=b;};\ntemplate<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b)a=b;};\n\nconst Int MOD = 1e6;\nconst Int MAX = 1024;\nInt n;\nInt dp[5][4][2][MAX];\nstring ss[MAX];\n\nInt check(Int v,string s){\n for(Int i=0;i<8;i++){\n if((s[i]!='x')&&((s[i]-'0')!=(v&1))) return 0;\n v>>=1;\n }\n return 1;\n}\n\nInt rf[5][4],mc[5][4],ms[5][4];\nInt match(Int a,Int b,Int l){\n return (a>>(8-l))==(b>>(8-l));\n}\n\nInt dfs(Int len,Int i,Int y,Int p){\n if(p==n) return (len==1&&i==0&&y==1);\n Int &res=dp[len][i][y][p];\n if(~res) return res;\n res=0;\n \n for(Int bit=0;bit<256;bit++){\n if(!check(bit,ss[p])) continue;\n if(!match(bit,rf[len][i],mc[len][i])) continue;\n if(i+1==len){\n if(y){\n for(Int nxt=1;nxt<=4;nxt++){\n Int ni=0;\n Int ny=(nxt==1);\n Int np=p+1;\n res+=dfs(nxt,ni,ny,np);\n res%=MOD;\n }\n }\n }else{\n Int ni=i+1;\n Int ny=y||(ms[len][i]&bit);\n Int np=p+1;\n res+=dfs(len,ni,ny,np);\n res%=MOD;\n }\n }\n return res;\n}\n\nsigned main(){\n cin.tie(0);\n ios::sync_with_stdio(0); \n\n rf[1][0]=0b00000000;\n \n rf[2][0]=0b11000000;\n rf[2][1]=0b10000000;\n\n rf[3][0]=0b11100000;\n rf[3][1]=0b10000000;\n rf[3][2]=0b10000000;\n\n rf[4][0]=0b11110000;\n rf[4][1]=0b10000000;\n rf[4][2]=0b10000000;\n rf[4][3]=0b10000000;\n \n mc[1][0]=1;\n \n mc[2][0]=3;\n mc[2][1]=2;\n\n mc[3][0]=4;\n mc[3][1]=2;\n mc[3][2]=2;\n\n mc[4][0]=5;\n mc[4][1]=2;\n mc[4][2]=2;\n mc[4][3]=2;\n \n ms[1][0]=0b00000000;\n \n ms[2][0]=0b00011110;\n ms[2][1]=0b00000000;\n\n ms[3][0]=0b00001111;\n ms[3][1]=0b00100000;\n ms[3][2]=0b00000000;\n\n ms[4][0]=0b00000111;\n ms[4][1]=0b00110000;\n ms[4][2]=0b00000000;\n ms[4][3]=0b00000000;\n \n while(cin>>n,n){\n memset(dp,-1,sizeof(dp));\n for(Int i=0;i<n;i++){\n cin>>ss[i];\n reverse(ss[i].begin(),ss[i].end());\n }\n Int ans=0;\n for(Int len=1;len<=4;len++){\n ans+=dfs(len,0,(len==1),0);\n ans%=MOD;\n }\n cout<<ans<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1030, "memory_kb": 3588, "score_of_the_acc": -1.2424, "final_rank": 19 }, { "submission_id": "aoj_2209_1686675", "code_snippet": "#include \"bits/stdc++.h\"\n#include<unordered_map>\n#include<unordered_set>\n#pragma warning(disable:4996)\nusing namespace std;\nusing ld = long double;\ntemplate<class T>\nusing Table = vector<vector<T>>;\n\n\n\nconst int mod = 1000000;\nstruct Mod {\npublic:\n\tint num;\n\tMod() : Mod(0) { ; }\n\tMod(long long int n) : num((n % mod + mod) % mod) {\n\t\tstatic_assert(mod<INT_MAX / 2, \"mod is too big, please make num 'long long int' from 'int'\");\n\t}\n\tMod(int n) : Mod(static_cast<long long int>(n)) { ; }\n\toperator int() { return num; }\n};\n\nMod operator+(const Mod a, const Mod b) { return Mod((a.num + b.num) % mod); }\nMod operator+(const long long int a, const Mod b) { return Mod(a) + b; }\nMod operator+(const Mod a, const long long int b) { return b + a; }\nMod operator++(Mod &a) { return a + Mod(1); }\nMod operator-(const Mod a, const Mod b) { return Mod((mod + a.num - b.num) % mod); }\nMod operator-(const long long int a, const Mod b) { return Mod(a) - b; }\nMod operator--(Mod &a) { return a - Mod(1); }\nMod operator*(const Mod a, const Mod b) { return Mod(((long long)a.num * b.num) % mod); }\nMod operator*(const long long int a, const Mod b) { return Mod(a)*b; }\nMod operator*(const Mod a, const long long int b) { return Mod(b)*a; }\nMod operator*(const Mod a, const int b) { return Mod(b)*a; }\nMod operator+=(Mod &a, const Mod b) { return a = a + b; }\nMod operator+=(long long int &a, const Mod b) { return a = a + b; }\nMod operator-=(Mod &a, const Mod b) { return a = a - b; }\nMod operator-=(long long int &a, const Mod b) { return a = a - b; }\nMod operator*=(Mod &a, const Mod b) { return a = a * b; }\nMod operator*=(long long int &a, const Mod b) { return a = a * b; }\nMod operator*=(Mod& a, const long long int &b) { return a = a * b; }\nMod operator^(const Mod a, const int n) {\n\tif (n == 0) return Mod(1);\n\tMod res = (a * a) ^ (n / 2);\n\tif (n % 2) res = res * a;\n\treturn res;\n}\nMod mod_pow(const Mod a, const int n) {\n\tif (n == 0) return Mod(1);\n\tMod res = mod_pow((a * a), (n / 2));\n\tif (n % 2) res = res * a;\n\treturn res;\n}\nMod inv(const Mod a) { return a ^ (mod - 2); }\nMod operator/(const Mod a, const Mod b) {\n\tassert(b.num != 0);\n\treturn a * inv(b);\n}\nMod operator/(const long long int a, const Mod b) {\n\tassert(b.num != 0);\n\treturn Mod(a) * inv(b);\n}\nMod operator/=(Mod &a, const Mod b) {\n\tassert(b.num != 0);\n\treturn a = a * inv(b);\n}\n\n#define MAX_MOD_N 1024000\n\nMod fact[MAX_MOD_N], factinv[MAX_MOD_N];\nvoid init() {\n\tfact[0] = Mod(1); factinv[0] = 1;\n\tfor (int i = 0; i < MAX_MOD_N - 1; ++i) {\n\t\tfact[i + 1] = fact[i] * Mod(i + 1);\n\t\tfactinv[i + 1] = factinv[i] / Mod(i + 1);\n\t}\n}\nMod comb(const int a, const int b) {\n\treturn fact[a] * factinv[b] * factinv[a - b];\n}\nint main() {\n\twhile (1) {\n\t\tint N; cin >> N;\n\t\tif (!N)break;\n\t\tMod dp[1001][7][2];\n\t\tfor (int i = 0; i < 1001; ++i) {\n\t\t\tfor (int j = 0; j < 7; ++j) {\n\t\t\t\tfor (int k = 0; k < 2; ++k) {\n\t\t\t\t\tdp[i][j][k] = 0;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tdp[0][0][0] = 1;\n\t\tfor (int i = 0; i < N; ++i) {\n\t\t\tstring st; cin >> st; \n\t\t\tfor (int b = 0; b < 256; ++b) {\n\t\t\t\tbitset<8>bs(b);\n\t\t\t\tbool ok = true;\n\t\t\t\tfor (int n = 0; n < 8; ++n) {\n\t\t\t\t\tif (st[n] == '0'&&bs[7-n] == 1)ok = false;\n\t\t\t\t\tif (st[n] == '1'&&bs[7-n] == 0)ok = false;\n\t\t\t\t}\n\t\t\t\tif (!ok)continue;\n\t\t\t\tfor (int j = 0; j < 7; ++j) {\n\t\t\t\t\tfor (int k = 0; k< 2; ++k) {\n\t\t\t\t\t\tbool valid = true;\n\t\t\t\t\t\tswitch (j)\n\t\t\t\t\t\t{\n\t\t\t\t\t\tcase 0:\n\t\t\t\t\t\t\tif (!((b ^ 0b00000000) & 0b10000000)) {\n\t\t\t\t\t\t\t\tdp[i + 1][0][0] += dp[i][j][k];\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tif (!((b ^ 0b11000000) & 0b11100000)) {\n\t\t\t\t\t\t\t\tif (b & 0b00011110) {\n\t\t\t\t\t\t\t\t\tdp[i + 1][1][1] += dp[i][j][k];\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tif (!((b ^ 0b11100000) & 0b11110000)) {\n\t\t\t\t\t\t\t\tif (b & 0b00001111) {\n\t\t\t\t\t\t\t\t\tdp[i + 1][2][1] += dp[i][j][k];\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\telse {\n\t\t\t\t\t\t\t\t\tdp[i + 1][2][0] += dp[i][j][k];\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tif (!((b ^ 0b11110000) & 0b11111000)) {\n\t\t\t\t\t\t\t\tif (b & 0b00000111) {\n\t\t\t\t\t\t\t\t\tdp[i + 1][4][1] += dp[i][j][k];\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\telse {\n\t\t\t\t\t\t\t\t\tdp[i + 1][4][0] += dp[i][j][k];\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase 1:\n\t\t\t\t\t\t\tif (!((b ^ 0b10000000) & 0b11000000)) {\n\t\t\t\t\t\t\t\tdp[i + 1][0][0] += dp[i][j][k];\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase 2:\n\t\t\t\t\t\t\tif (!((b ^ 0b10000000) & 0b11000000)) {\n\t\t\t\t\t\t\t\tif (k || (b & 0b00100000)) {\n\t\t\t\t\t\t\t\t\tdp[i + 1][3][1] += dp[i][j][k];\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase 3:\n\t\t\t\t\t\t\tif (!((b ^ 0b10000000) & 0b11000000)) {\n\t\t\t\t\t\t\t\tdp[i + 1][0][0] += dp[i][j][k];\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase 4:\n\t\t\t\t\t\t\tif (!((b ^ 0b10000000) & 0b11000000)) {\n\t\t\t\t\t\t\t\tif (k || (b & 0b00110000)) {\n\t\t\t\t\t\t\t\t\tdp[i + 1][5][1] += dp[i][j][k];\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase 5:\n\t\t\t\t\t\t\tif (!((b ^ 0b10000000) & 0b11000000)) {\n\t\t\t\t\t\t\t\tdp[i + 1][6][1] += dp[i][j][k];\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase 6:\n\t\t\t\t\t\t\tif (!((b ^ 0b10000000) & 0b11000000)) {\n\t\t\t\t\t\t\t\tdp[i + 1][0][0] += dp[i][j][k];\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tMod ans = dp[N][0][0];\n\t\tcout << ans << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 140, "memory_kb": 11212, "score_of_the_acc": -1.1275, "final_rank": 18 }, { "submission_id": "aoj_2209_1445040", "code_snippet": "#include <iostream>\n#include <string>\n#include <vector>\n#include <map>\n#include <cmath>\n\nenum { PATTERN_1, PATTERN_2, PATTERN_3, PATTERN_4 };\ntypedef size_t variation_t;\n\n\nbool can_match(\n const std::string &str, size_t offset, size_t size,\n const std::string &query)\n{\n for (int i = 0; i < size; ++i)\n if (str.at(offset + i) != 'x' and\n str.at(offset + i) != query.at(i))\n return false;\n\n return true;\n}\n\n\nbool all_zero(const std::string &byte, size_t offset, size_t size)\n{\n for (int i = offset; i < offset + size; ++i)\n if (byte.at(i) != '0')\n return false;\n return true;\n}\n\n\nvariation_t variation(const std::string &byte, size_t offset, size_t size)\n{\n size_t count(0);\n \n for (size_t i = offset; i < offset + size; ++i)\n if (byte.at(i) == 'x')\n ++count;\n \n return (variation_t)(std::pow(2.0, (double)count)) % 1000000;\n}\n\n\nbool can_be_pattern_of(\n int pattern, std::vector<std::string> &input, size_t offset)\n{\n#define getbyte(i) input.at(offset + i)\n switch (pattern)\n {\n case PATTERN_1:\n if (input.size() - offset >= 1)\n {\n char c = getbyte(0).at(0);\n return (c == '0') or (c == 'x');\n }\n \n case PATTERN_2:\n if (input.size() - offset >= 2)\n {\n return\n can_match(getbyte(0), 0, 3, \"110\")\n and can_match(getbyte(1), 0, 2, \"10\")\n and not all_zero(getbyte(0), 3, 4);\n }\n break;\n \n case PATTERN_3:\n if (input.size() - offset >= 3)\n {\n return\n can_match(getbyte(0), 0, 4, \"1110\")\n and can_match(getbyte(1), 0, 2, \"10\")\n and can_match(getbyte(2), 0, 2, \"10\")\n and (not all_zero(getbyte(0), 4, 4) or\n not all_zero(getbyte(1), 2, 1));\n }\n \n case PATTERN_4:\n if (input.size() - offset >= 4)\n {\n return\n can_match(getbyte(0), 0, 5, \"11110\")\n and can_match(getbyte(1), 0, 2, \"10\")\n and can_match(getbyte(2), 0, 2, \"10\")\n and can_match(getbyte(3), 0, 2, \"10\")\n and (not all_zero(getbyte(0), 5, 3) or\n not all_zero(getbyte(1), 2, 2));\n }\n }\n \n return false;\n#undef getbyte\n}\n\n\nvariation_t count(\n std::vector<std::string> &input, size_t offset,\n std::map<size_t, variation_t> *memo)\n{\n if (offset >= input.size()) return 1;\n\n std::map<size_t, variation_t>::iterator found = memo->find(offset);\n if (found != memo->end())\n return found->second;\n \n#define getbyte(i) input.at(offset + i)\n variation_t out(0);\n\n if (can_be_pattern_of(PATTERN_1, input, offset))\n {\n variation_t var_x = variation(getbyte(0), 1, 7);\n out += var_x * count(input, offset + 1, memo);\n out %= 1000000;\n }\n \n if (can_be_pattern_of(PATTERN_2, input, offset))\n {\n variation_t var_x =\n variation(getbyte(0), 7, 1) *\n variation(getbyte(1), 2, 6);\n\n variation_t var_y = variation(getbyte(0), 3, 4);\n if (getbyte(0).substr(3, 4).find('1') == std::string::npos)\n --var_y;\n \n out += var_x * var_y * count(input, offset + 2, memo);\n out %= 1000000;\n }\n \n if (can_be_pattern_of(PATTERN_3, input, offset))\n {\n variation_t var_x =\n variation(getbyte(1), 3, 5) *\n variation(getbyte(2), 2, 6);\n\n variation_t var_y =\n variation(getbyte(0), 4, 4) *\n variation(getbyte(1), 2, 1);\n if (getbyte(0).substr(4, 4).find('1') == std::string::npos\n and getbyte(1).at(2) != '1')\n --var_y;\n \n out += var_x * var_y * count(input, offset + 3, memo);\n out %= 1000000;\n }\n \n if (can_be_pattern_of(PATTERN_4, input, offset))\n {\n variation_t var_x =\n variation(getbyte(1), 4, 4) *\n variation(getbyte(2), 2, 6) *\n variation(getbyte(3), 2, 6);\n\n variation_t var_y =\n variation(getbyte(0), 5, 3) *\n variation(getbyte(1), 2, 2);\n if (getbyte(0).substr(5, 3).find('1') == std::string::npos\n and getbyte(1).substr(2, 2).find('1') == std::string::npos)\n --var_y;\n \n out += var_x * var_y * count(input, offset + 4, memo);\n out %= 1000000;\n }\n\n (*memo)[offset] = out; // MEMORIZE\n \n return out;\n#undef getbyte\n}\n\n\nint main()\n{\n while (true)\n {\n int n;\n std::cin >> n;\n\n if (n == 0) break;\n\n std::vector<std::string> input;\n for (int i = 0; i < n; ++i)\n {\n input.push_back(std::string());\n std::cin >> input.back();\n }\n\n std::map<size_t, variation_t> memo;\n std::cout << count(input, 0, &memo) << std::endl;\n }\n \n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 1576, "score_of_the_acc": -0.0523, "final_rank": 10 }, { "submission_id": "aoj_2209_1434624", "code_snippet": "#include<cstdio>\n#include<cstdlib>\n#include<cstring>\n#include<cmath>\n#include<iostream>\n#include<string>\n#include<vector>\n#include<map>\n#include<set>\n#include<list>\n#include<queue>\n#include<deque>\n#include<algorithm>\n#include<numeric>\n#include<utility>\n#include<complex>\n#include<functional>\n \nusing namespace std;\n\n/* constant */\n\nconst int MAX_N = 1000;\nconst long long MOD = 1000000LL;\n\n/* typedef */\n\ntypedef long long ll;\ntypedef unsigned char uchar;\n\n/* global variables */\n\nint n;\nuchar bytes[MAX_N], masks[MAX_N];\nll dp[MAX_N + 1];\nint bcs[256];\n\n/* subroutines */\n\nvoid str2bits(string str, uchar& byte, uchar& mask) {\n byte = mask = 0;\n for (int i = 7, b = 1; i >= 0; i--, b <<= 1) {\n if (str[i] == 'x') mask |= b;\n else if (str[i] == '1') byte |= b;\n }\n}\n\nint bcount(uchar bits) {\n int count = 0;\n for (; bits > 0; bits >>= 1)\n if (bits & 1) count++;\n return count;\n}\n\nll utf8_1(int i) {\n if (i > n - 1 ||\n (bytes[i] & 0x80) != 0) return 0LL;\n uchar m0 = masks[i] & 0x7f;\n return (1LL << bcs[m0]);\n}\n\nll utf8_2(int i) {\n if (i > n - 2 ||\n (bytes[i] & 0xe0) != (0xc0 & ~masks[i]) ||\n (bytes[i + 1] & 0xc0) != (0x80 & ~masks[i + 1])) return 0LL;\n\n int ybc = bcs[bytes[i] & 0x1e];\n int ymc = bcs[masks[i] & 0x1e];\n if (ybc == 0 && ymc == 0) return 0LL;\n\n ll yn = 1LL << ymc;\n if (ybc == 0) yn--;\n \n uchar m0 = masks[i] & 0x01;\n uchar m1 = masks[i + 1] & 0x3f;\n return yn * (1LL << (bcs[m0] + bcs[m1]));\n}\n\nll utf8_3(int i) {\n if (i > n - 3 ||\n (bytes[i] & 0xf0) != (0xe0 & ~masks[i]) ||\n (bytes[i + 1] & 0xc0) != (0x80 & ~masks[i + 1]) ||\n (bytes[i + 2] & 0xc0) != (0x80 & ~masks[i + 2])) return 0LL;\n\n int ybc = bcs[bytes[i] & 0x0f] + bcs[bytes[i + 1] & 0x20];\n int ymc = bcs[masks[i] & 0x0f] + bcs[masks[i + 1] & 0x20];\n if (ybc == 0 && ymc == 0) return 0LL;\n\n ll yn = 1LL << ymc;\n if (ybc == 0) yn--;\n \n uchar m1 = masks[i + 1] & 0x1f;\n uchar m2 = masks[i + 2] & 0x3f;\n return yn * (1LL << (bcs[m1] + bcs[m2]));\n}\n\nll utf8_4(int i) {\n if (i > n - 4 ||\n (bytes[i] & 0xf8) != (0xf0 & ~masks[i]) ||\n (bytes[i + 1] & 0xc0) != (0x80 & ~masks[i + 1]) ||\n (bytes[i + 2] & 0xc0) != (0x80 & ~masks[i + 2]) ||\n (bytes[i + 3] & 0xc0) != (0x80 & ~masks[i + 3])) return 0LL;\n\n int ybc = bcs[bytes[i] & 0x07] + bcs[bytes[i + 1] & 0x30];\n int ymc = bcs[masks[i] & 0x07] + bcs[masks[i + 1] & 0x30];\n if (ybc == 0 && ymc == 0) return 0LL;\n\n ll yn = 1LL << ymc;\n if (ybc == 0) yn--;\n \n uchar m1 = masks[i + 1] & 0x0f;\n uchar m2 = masks[i + 2] & 0x3f;\n uchar m3 = masks[i + 3] & 0x3f;\n return yn * (1LL << (bcs[m1] + bcs[m2] + bcs[m3]));\n}\n\n/* main */\n\nint main() {\n for (int b = 0; b < 256; b++) bcs[b] = bcount((uchar)b);\n\n for (;;) {\n cin >> n;\n if (n == 0) break;\n\n for (int i = 0; i < n; i++) {\n string str;\n cin >> str;\n str2bits(str, bytes[i], masks[i]);\n //cout << str; printf(\"=%x/%x\\n\", bytes[i], masks[i]);\n }\n\n memset(dp, 0, sizeof(dp));\n dp[n] = 1LL;\n\n for (int i = n - 1; i >= 0; i--) {\n dp[i] = (dp[i] + utf8_1(i) * dp[i + 1]) % MOD;\n if (i < n - 1) dp[i] = (dp[i] + utf8_2(i) * dp[i + 2]) % MOD;\n if (i < n - 2) dp[i] = (dp[i] + utf8_3(i) * dp[i + 3]) % MOD;\n if (i < n - 3) dp[i] = (dp[i] + utf8_4(i) * dp[i + 4]) % MOD;\n }\n\n cout << dp[0] << endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1220, "score_of_the_acc": -0.0072, "final_rank": 1 }, { "submission_id": "aoj_2209_1262787", "code_snippet": "#include <iostream>\n#include <complex>\n#include <sstream>\n#include <string>\n#include <algorithm>\n#include <deque>\n#include <list>\n#include <map>\n#include <numeric>\n#include <queue>\n#include <vector>\n#include <set>\n#include <limits>\n#include <cstdio>\n#include <cctype>\n#include <cmath>\n#include <cstring>\n#include <cstdlib>\n#include <ctime>\nusing namespace std;\n#define REP(i, j) for(int i = 0; i < (int)(j); ++i)\n#define FOR(i, j, k) for(int i = (int)(j); i < (int)(k); ++i)\n#define SORT(v) sort((v).begin(), (v).end())\n#define REVERSE(v) reverse((v).begin(), (v).end())\ntypedef long long int Int;\nconst int L = 8;\nconst Int MOD = 1000000;\n\nvoid countXY(Int ys, Int yt, Int xs, Int xt, Int &x, Int &y, string &s){\n if(ys != -1) FOR(i, ys, yt) if(s[i] == 'x') ++y;\n if(xs != -1) FOR(i, xs, xt) if(s[i] == 'x') ++x;\n}\n\nInt countPattern(Int x, Int y, string s){\n bool t = 1;\n REP(i, s.length()) if(s[i] == '1') t = 0;\n Int a = 1, b = 1;\n REP(i, x) a = (a * 2 % MOD);\n REP(j, y) b = (b * 2 % MOD);\n if(t) b = (b - 1 + MOD) % MOD;\n return (a % MOD) * (b % MOD) % MOD;\n}\n\nbool check(string s){\n bool f = 0;\n REP(i, s.length()){\n if(s[i] == 'x') return 1;\n if(s[i] == '1') f = 1;\n }\n return f;\n}\n\nbool check_pref(string &s, string tar){\n REP(i, tar.length()) if(s[i] != 'x' && s[i] != tar[i]) return 0;\n return 1;\n}\n\nint main() {\n Int N;\n while(cin >>N && N){\n vector<string> v(N);\n REP(i, N) cin >>v[i];\n vector<Int> dp(N + 1, 0);\n dp[0] = 1;\n REP(i, N){\n if(check_pref(v[i], \"0\")){\n Int x = 0, y = 0;\n countXY(-1, -1, 1, L, x, y, v[i]);\n dp[i + 1] = ((dp[i] * countPattern(x, y, \"1\") % MOD) + dp[i + 1]) % MOD;\n }\n if(i + 1 < N && check_pref(v[i], \"110\") && check_pref(v[i + 1], \"10\") && check(v[i].substr(3, 4))){\n Int x = 0, y = 0;\n countXY(3, L - 1, L - 1, L, x, y, v[i]);\n countXY(-1, -1, 2, L, x, y, v[i + 1]);\n dp[i + 2] = ((dp[i] * countPattern(x, y, v[i].substr(3, 4)) % MOD) + dp[i + 2]) % MOD;\n }\n if(i + 2 < N && check_pref(v[i], \"1110\") && check_pref(v[i + 1], \"10\") && check_pref(v[i + 2], \"10\") && check(v[i].substr(4, 4) + v[i + 1].substr(2, 1))){\n Int x = 0, y = 0;\n countXY(4, L, -1, -1, x, y, v[i]);\n countXY(2, 3, 3, L, x, y, v[i + 1]);\n countXY(-1, -1, 2, L, x, y, v[i + 2]);\n dp[i + 3] = ((dp[i] * countPattern(x, y, v[i].substr(4, 4) + v[i + 1].substr(2, 1)) % MOD) + dp[i + 3]) % MOD;\n }\n if(i + 3 < N && check_pref(v[i], \"11110\") && check_pref(v[i + 1], \"10\") && check_pref(v[i + 2], \"10\") && check_pref(v[i + 3], \"10\") && check(v[i].substr(5, 3) + v[i + 1].substr(2, 2))){\n Int x = 0, y = 0;\n countXY(5, L, -1, -1, x, y, v[i]);\n countXY(2, 4, 4, L, x, y, v[i + 1]);\n countXY(-1, -1, 2, L, x, y, v[i + 2]);\n countXY(-1, -1, 2, L, x, y, v[i + 3]);\n dp[i + 4] = ((dp[i] * countPattern(x, y, v[i].substr(5, 3) + v[i + 1].substr(2, 2)) % MOD) + dp[i + 4]) % MOD;\n }\n }\n cout <<dp[N] <<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 1276, "score_of_the_acc": -0.0323, "final_rank": 8 }, { "submission_id": "aoj_2209_1133960", "code_snippet": "#include<stdio.h>\n#include<algorithm>\nusing namespace std;\nchar str[1100][10];\nint mod=1000000;\nint dp[1100][5][5];\nint t[12];\nint main(){\n\tint a;\n\twhile(scanf(\"%d\",&a),a){\n\t\tfor(int i=0;i<a;i++)scanf(\"%s\",str[i]);\n\t\tfor(int i=0;i<1100;i++)for(int j=0;j<5;j++)for(int k=0;k<5;k++)\n\t\t\tdp[i][j][k]=0;\n\t\tdp[a][0][0]=1;\n\t\tfor(int i=a-1;i>=0;i--){\n\t\t\tfor(int j=0;j<12;j++)t[j]=0;\n\t\t\tfor(int j=0;j<256;j++){\n\t\t\t\tbool ok=true;\n\t\t\t\tfor(int k=0;k<8;k++){\n\t\t\t\t\tif((j&(1<<k))&&str[i][k]=='0')ok=false;\n\t\t\t\t\tif(!(j&(1<<k))&&str[i][k]=='1')ok=false;\n\t\t\t\t}\n\t\t\t\tif(ok){\n\t\t\t\t\tif(j%2==0)t[0]++;\n\t\t\t\t\telse if(j%4==1){\n\t\t\t\t\t\tif(j&4)t[1]++;\n\t\t\t\t\t\telse if(j&8)t[2]++;\n\t\t\t\t\t\telse t[3]++;\n\t\t\t\t\t}else if(j%8==3){\n\t\t\t\t\t\tif(j&120)t[4]++;\n\t\t\t\t\t}else if(j%16==7){\n\t\t\t\t\t\tif(j&240)t[5]++;\n\t\t\t\t\t\telse t[6]++;\n\t\t\t\t\t}else if(j%32==15){\n\t\t\t\t\t\tif(j&224)t[7]++;\n\t\t\t\t\t\telse t[8]++;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t//\tfor(int j=0;j<9;j++)printf(\"%d \",t[j]);printf(\"\\n\");\n\t\t\tdp[i][0][0]=(dp[i+1][0][0]*t[0]+(dp[i+1][1][0])*t[4]+\n\t\t\tdp[i+1][2][0]*t[5]+dp[i+1][2][1]*(t[5]+t[6])+dp[i+1][3][0]*t[7]+dp[i+1][3][1]*(t[7]+t[8]))%mod;\n\t\t\tdp[i][1][0]=(dp[i+1][0][0]*(t[1]+t[2]+t[3]))%mod;\n\t\t\tdp[i][2][0]=(dp[i+1][1][0]*(t[2]+t[3]))%mod;\n\t\t\tdp[i][2][1]=(dp[i+1][1][0]*t[1])%mod;\n\t\t\tdp[i][3][0]=((dp[i+1][2][0]+dp[i+1][2][1])*t[3])%mod;\n\t\t\tdp[i][3][1]=((dp[i+1][2][0]+dp[i+1][2][1])*(t[1]+t[2]))%mod;\n\t\t}\n\t\tprintf(\"%d\\n\",dp[0][0][0]);\n\t}\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 1148, "score_of_the_acc": -0.049, "final_rank": 9 }, { "submission_id": "aoj_2209_1124287", "code_snippet": "#include <cstdio>\n#include <cstdlib>\n#include <cmath>\n#include <climits>\n#include <cfloat>\n#include <map>\n#include <utility>\n#include <set>\n#include <iostream>\n#include <memory>\n#include <string>\n#include <vector>\n#include <algorithm>\n#include <functional>\n#include <sstream>\n#include <complex>\n#include <stack>\n#include <queue>\n#include <cstring>\nusing namespace std;\nstatic const double EPS = 1e-10;\ntypedef long long ll;\n#define rep(i,n) for(int i=0;i<n;i++)\n#define rev(i,n) for(int i=n-1;i>=0;i--)\n#define sz(a) a.size()\n#define all(a) a.begin(),a.end()\n#define mp(a,b) make_pair(a,b)\n#define pb(a) push_back(a)\n#define SS stringstream\n#define DBG1(a) rep(_X,sz(a)){printf(\"%d \",a[_X]);}puts(\"\");\n#define DBG2(a) rep(_X,sz(a)){rep(_Y,sz(a[_X]))printf(\"%d \",a[_X][_Y]);puts(\"\");}\n#define bitcount(b) __builtin_popcount(b)\n#define REP(i, s, e) for ( int i = s; i <= e; i++ ) \n\nstring b;\nstring p[4] = {\"0xxxxxxx\",\"110yyyyx10xxxxxx\",\"1110yyyy10yxxxxx10xxxxxx\",\"11110yyy10yyxxxx10xxxxxx10xxxxxx\"};\n\nint dp[8010][4][33][2];\nint dfs(int x,int pat,int pos,int done){\n\tint &res = dp[x][pat][pos][done];\n\tif( res != -1 ) return res;\n\tint ans = 0;\n\tif( pos == p[pat].size() ){\n\t\tif( !done ) return 0;\n\t\tif( x == b.size() ) return 1;\n\t\tans += dfs(x,0,0,1);\n\t\tans += dfs(x,1,0,0);\n\t\tans += dfs(x,2,0,0);\n\t\tans += dfs(x,3,0,0);\n\t\tans %= 1000000;\n\t\treturn ans;\n\t}\n\tif( x == b.size() ) return 0;\n\tfor(int num = '0' ; num <= '1' ; num++){\n\t\tif( b[x] != 'x' && b[x] != num ) continue;\n\t\tint nextDone = done;\n\t\tif( p[pat][pos] == 'y' && num == '1' ) nextDone = 1;\n\t\tif( p[pat][pos] != 'x' && p[pat][pos] != 'y' && p[pat][pos] != num ) continue;\n\t\tans += dfs(x+1,pat,pos+1,nextDone);\n\t}\n\tans %= 1000000;\n\treturn res = ans;\n}\nint main(){\n\tint N;\n\twhile(cin >> N && N){\n\t\tmemset(dp,-1,sizeof(dp));\n\t\tb = \"\";\n\t\tfor(int i = 0 ; i < N ; i++){\n\t\t\tstring s;\n\t\t\tcin >> s;\n\t\t\tb += s;\n\t\t}\n\t\tint ans = 0;\n\t\tans += dfs(0,0,0,1);\n\t\tans += dfs(0,1,0,0);\n\t\tans += dfs(0,2,0,0);\n\t\tans += dfs(0,3,0,0);\n\t\tans %= 1000000;\n\t\tcout << ans << endl;\n\t}\n\t\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 10340, "score_of_the_acc": -0.9526, "final_rank": 17 }, { "submission_id": "aoj_2209_1071933", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nstring pat[4] = {\"0xxxxxxx\",\"110yyyyx10xxxxxx\",\"1110yyyy10yxxxxx10xxxxxx\",\"11110yyy10yyxxxx10xxxxxx10xxxxxx\"};\nint N;\nmap< pair< int , bool > , int > used;\nvector< string > data;\nint rec2( int idx, const string& str, const string& patan, bool y_flag){\n if(idx == str.size()){\n return y_flag;\n }\n int ret = 0;\n if(used.find( make_pair( idx, y_flag)) != used.end()) return used[make_pair( idx, y_flag)];\n if(isdigit(str[idx])){\n if(isdigit(patan[idx])){\n if(str[idx] != patan[idx]) return 0;\n else ret = rec2( idx + 1, str, patan, y_flag);\n } else {\n ret = rec2( idx + 1, str, patan, y_flag|(str[idx]=='1'&&patan[idx]=='y'));\n }\n } else {\n if(isdigit(patan[idx])){\n ret = rec2( idx + 1, str, patan, y_flag);\n } else {\n ret = rec2( idx + 1, str, patan, y_flag|(patan[idx]=='y')) + rec2( idx + 1, str, patan, y_flag);\n }\n }\n return used[make_pair( idx, y_flag)] = ret % 1000000;\n}\nint rec( int idx, const string& str, const string& patan, bool y_flag){\n used.clear();\n return rec2( idx, str, patan, y_flag);\n}\nint main(){\n long long dp[1001] = {};\n while(cin >> N, N){\n for(int i = 0; i < N; i++){\n string s;\n cin >> s;\n data.push_back(s);\n }\n for(int i = 0; i < N; i++){\n if(i == 0){\n dp[i] = rec( 0, data[0], pat[0], true);\n dp[i] %= 1000000;\n } else if(i == 1){\n dp[i] = dp[0] * rec( 0, data[i], pat[0], true);\n dp[i] %= 1000000;\n dp[i] += rec( 0, data[0] + data[1], pat[1], false);\n dp[i] %= 1000000;\n } else if(i == 2){\n dp[i] = dp[1] * rec( 0, data[2], pat[0], true);\n dp[i] %= 1000000;\n dp[i] += dp[0] * rec( 0, data[1] + data[2], pat[1], false);\n dp[i] %= 1000000;\n dp[i] += rec( 0, data[0] + data[1] + data[2], pat[2], false);\n dp[i] %= 1000000;\n } else if(i == 3){\n dp[i] = dp[2] * rec( 0, data[3], pat[0], true);\n dp[i] %= 1000000;\n dp[i] += dp[1] * rec( 0, data[2] + data[3], pat[1], false);\n dp[i] %= 1000000;\n dp[i] += dp[0] * rec( 0, data[1] + data[2] + data[3], pat[2], false);\n dp[i] %= 1000000;\n dp[i] += rec( 0, data[0] + data[1] + data[2] + data[3], pat[3], false);\n dp[i] %= 1000000;\n } else {\n dp[i] = dp[i - 1] * rec( 0, data[i], pat[0], true);\n dp[i] %= 1000000;\n dp[i] += dp[i - 2] * rec( 0, data[i - 1] + data[i], pat[1], false);\n dp[i] %= 1000000;\n dp[i] += dp[i - 3] * rec( 0, data[i - 2] + data[i - 1] + data[i], pat[2], false);\n dp[i] %= 1000000;\n dp[i] += dp[i - 4] * rec( 0, data[i - 3] + data[i - 2] + data[i - 1] + data[i], pat[3], false);\n dp[i] %= 1000000;\n }\n }\n cout << dp[N - 1] << endl;\n\n data.clear();\n }\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 1280, "score_of_the_acc": -0.0817, "final_rank": 11 }, { "submission_id": "aoj_2209_1026206", "code_snippet": "#include<iostream>\n#include<string>\n#include<vector>\n\nusing namespace std;\n\nint main(){\n for(int N;cin>>N,N;){\n string b[1000];\n for(int i=0;i<N;i++){\n cin>>b[i];\n }\n int dp[1001]={1};\n for(int i=0;i<N;i++){\n vector<string> bp[4]={\n\t{\"0xxxxxxx\"},\n\t{\"110yyyyx\",\"10xxxxxx\"},\n\t{\"1110yyyy\",\"10yxxxxx\",\"10xxxxxx\"},\n\t{\"11110yyy\",\"10yyxxxx\",\"10xxxxxx\",\"10xxxxxx\"}\n };\n for(int j=0;j<4;j++){\n\tif(N-i<bp[j].size())continue;\n\tbool f=false;\n\tint x=0,y=0;\n\tbool yz=false;\n\tfor(int k=0;k<bp[j].size();k++){\n\t for(int l=0;l<8;l++){\n\t if(bp[j][k][l]=='0'||bp[j][k][l]=='1'){\n\t f|=b[i+k][l]==(bp[j][k][l]^'0'^'1');\n\t }else if(bp[j][k][l]=='y'){\n\t y+=b[i+k][l]=='x';\n\t yz|=b[i+k][l]=='1';\n\t }else{\n\t x+=b[i+k][l]=='x';\n\t }\n\t }\n\t}\n\tif(!f){\n\t long long yf=(j==0)?1:(1LL<<y)-!yz;\n\t const int M=1000000;\n\t dp[i+bp[j].size()]=(dp[i+bp[j].size()]+(yf<<x)%M*dp[i])%M;\n\t}\n }\n }\n cout<<dp[N]<<endl;\n }\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 1272, "score_of_the_acc": -0.0319, "final_rank": 7 }, { "submission_id": "aoj_2209_671790", "code_snippet": "#include <iostream>\n#include <algorithm>\n#include <string>\n#include <vector>\nusing namespace std;\n\ntypedef long long lli;\n\nconst lli MOD = 1000000;\nint n;\nstring str[1000];\nvector<string> pat[4];\n\nvoid init(){\n for(int i=0;i<4;i++) pat[i].clear();\n pat[0].push_back(\"0xxxxxxx\");\n\n pat[1].push_back(\"110yyyyx\");\n pat[1].push_back(\"10xxxxxx\");\n\n pat[2].push_back(\"1110yyyy\");\n pat[2].push_back(\"10yxxxxx\");\n pat[2].push_back(\"10xxxxxx\");\n\n pat[3].push_back(\"11110yyy\");\n pat[3].push_back(\"10yyxxxx\");\n pat[3].push_back(\"10xxxxxx\");\n pat[3].push_back(\"10xxxxxx\");\n}\n\nvoid solve(){\n lli dp[1001][4][4][8][2];\n for(int i=0;i<n;i++) for(int j=0;j<4;j++) for(int k=0;k<4;k++) for(int l2=0;l2<8;l2++) for(int l=0;l<2;l++) dp[i][j][k][l2][l] = 0;\n\n for(int i=0;i<n;i++){\n for(int j=0;j<4;j++){\n if(pat[j][0][0] == '1' && str[i][0] != '0' || pat[j][0][0] == '0' && str[i][0] != '1'){\n int pos = pat[j][0][0] == '0';\n if(i == 0) dp[i][j][0][0][pos] = 1;\n else{\n for(int k=0;k<4;k++) dp[i][j][0][0][pos] = (dp[i][j][0][0][pos] + dp[i-1][k][k][7][1]) % MOD;\n\n for(int k=0;k<j;k++)\n for(int l=0;l<2;l++)\n dp[i][j][k+1][0][l] = (dp[i][j][k+1][0][l] + dp[i-1][j][k][7][l]) % MOD;\n }\n }\n }\n\n for(int j=0;j<4;j++){\n for(int k=0;k<=j;k++){\n for(int l=1;l<8;l++){\n for(int l2=0;l2<2;l2++){\n if(pat[j][k][l] == '1' && str[i][l] != '0' || pat[j][k][l] == '0' && str[i][l] != '1') dp[i][j][k][l][l2] = (dp[i][j][k][l][l2] + dp[i][j][k][l-1][l2]) % MOD;\n if(pat[j][k][l] == 'y'){\n if(str[i][l] == '0' || str[i][l] == 'x') dp[i][j][k][l][l2] = (dp[i][j][k][l][l2] + dp[i][j][k][l-1][l2]) % MOD;\n if(str[i][l] == '1' || str[i][l] == 'x') dp[i][j][k][l][1] = (dp[i][j][k][l][1] + dp[i][j][k][l-1][l2]) % MOD;\n }\n if(pat[j][k][l] == 'x'){\n if(str[i][l] == 'x') dp[i][j][k][l][l2] = (dp[i][j][k][l][l2] + dp[i][j][k][l-1][l2] * 2) % MOD;\n else dp[i][j][k][l][l2] = (dp[i][j][k][l][l2] + dp[i][j][k][l-1][l2]) % MOD;\n }\n }\n }\n }\n }\n }\n\n lli ans = 0;\n for(int i=0;i<4;i++) ans = (ans + dp[n-1][i][i][7][1]) % MOD;\n cout << ans << endl;\n\n}\n\nint main(){\n init();\n while(cin >> n && n){\n for(int i=0;i<n;i++) cin >> str[i];\n solve();\n }\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3268, "score_of_the_acc": -0.2401, "final_rank": 15 }, { "submission_id": "aoj_2209_670310", "code_snippet": "#include <iostream>\n#include <algorithm>\n#include <string>\n \nusing namespace std;\n \ntypedef long long lli;\n \nconst lli N = 1000;\nconst lli M = 1000000;\n \nlli n, dp[N+1], t[17];\nstring data[N];\nstring pat[4][4] = {\n {\"0xxxxxxx\", \"\", \"\", \"\"},\n {\"110yyyyx\", \"10xxxxxx\", \"\", \"\"},\n {\"1110yyyy\", \"10yxxxxx\", \"10xxxxxx\", \"\"},\n {\"11110yyy\", \"10yyxxxx\", \"10xxxxxx\", \"10xxxxxx\"},\n};\n \nmain(){\n t[0] = 1;\n for(lli i=1;i<17;i++) t[i] = (t[i-1] * 2) % M;\n while(cin >> n && n){\n for(lli i=0;i<n;i++) cin >> data[i];\n fill(dp, dp+n+1, 0);\n dp[0] = 1;\n for(lli i=0;i<n;i++){\n for(lli j=0;j<4&&i+j<n;j++){\n bool f = true;\n int f_y = 0;\n if(j == 0) f_y = true;\n lli cnt[2] = {0, 0};\n for(lli k=0;k<=j;k++){\n for(lli l=0;l<8;l++){\n if(data[i+k][l] != 'x' && pat[j][k][l] != 'x' && pat[j][k][l] != 'y' && data[i+k][l] != pat[j][k][l]){\nf = false;\n }\n if(data[i+k][l] == '1' && pat[j][k][l] == 'y') f_y = 2;\n if(data[i+k][l] == 'x'){\n if(pat[j][k][l] == 'x') cnt[0]++;\n if(pat[j][k][l] == 'y'){\n cnt[1]++;\n f_y = max(f_y, 1);\n }\n }\n }\n }\n if(!f_y) f = false;\n if(f){\n lli val = t[cnt[0]];\n if(cnt[1]){\n if(f_y == 2) val = (val * t[cnt[1]]) % M;\n else val = (val * ((t[cnt[1]] - 1 + M) % M)) % M;\n }\n dp[i+j+1] = (dp[i+j+1] + dp[i] * val) % M;\n }\n }\n }\n cout << dp[n] << endl;\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 1272, "score_of_the_acc": -0.0221, "final_rank": 2 }, { "submission_id": "aoj_2209_669862", "code_snippet": "#include <iostream>\n#include <algorithm>\n#include <string>\n\nusing namespace std;\n\ntypedef long long lli;\n\nconst lli N = 1000;\nconst lli M = 1000000;\n\nlli n, dp[N+1], t[1001];\nstring data[N];\nstring pat[4][4] = {\n {\"0xxxxxxx\", \"\", \"\", \"\"},\n {\"110yyyyx\", \"10xxxxxx\", \"\", \"\"},\n {\"1110yyyy\", \"10yxxxxx\", \"10xxxxxx\", \"\"},\n {\"11110yyy\", \"10yyxxxx\", \"10xxxxxx\", \"10xxxxxx\"},\n};\n\nmain(){\n t[0] = 1;\n for(lli i=1;i<1001;i++) t[i] = (t[i-1] * 2) % M;\n while(cin >> n && n){\n for(lli i=0;i<n;i++) cin >> data[i];\n fill(dp, dp+n+1, 0);\n dp[0] = 1;\n for(lli i=0;i<n;i++){\n for(lli j=0;j<4&&i+j<n;j++){\n bool f = true;\n bool f_y = false;\n bool f_y2 = false;\n if(j == 0) f_y = true;\n lli cnt[2] = {0, 0};\n for(lli k=0;k<=j;k++){\n for(lli l=0;l<8;l++){\n if(data[i+k][l] != 'x' && pat[j][k][l] != 'x' && pat[j][k][l] != 'y' && data[i+k][l] != pat[j][k][l]){\n //cout << \"# \" << j << ' ' << k << ' ' << l << endl;\nf = false;\n }\n if(data[i+k][l] == '1' && pat[j][k][l] == 'y') f_y = f_y2 = true;\n if(data[i+k][l] == 'x'){\n if(pat[j][k][l] == 'x') cnt[0]++;\n if(pat[j][k][l] == 'y'){\n cnt[1]++;\n f_y = true;\n }\n }\n }\n }\n //cout << i << ' ' << j << ' ' << f << endl;\n if(!f_y) f = false;\n if(f){\n lli val = t[cnt[0]];;\n //cout << cnt[0] << ' ' << cnt[1] << endl;\n if(cnt[1]){\n if(f_y2) val = (val * t[cnt[1]]) % M;\n else val = (val * ((t[cnt[1]] - 1 + M) % M)) % M;\n }\n //cout << val << endl;\n dp[i+j+1] = (dp[i+j+1] + dp[i] * val) % M;\n }\n }\n }\n cout << dp[n] << endl;\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 1280, "score_of_the_acc": -0.0229, "final_rank": 3 }, { "submission_id": "aoj_2209_600885", "code_snippet": "#define _USE_MATH_DEFINES\n#define INF 0x3f3f3f3f\n#include <cstdio>\n#include <iostream>\n#include <sstream>\n#include <cmath>\n#include <cstdlib>\n#include <algorithm>\n#include <queue>\n#include <stack>\n#include <limits>\n#include <map>\n#include <string>\n#include <cstring>\n#include <set>\n#include <deque>\n#include <bitset>\n#include <list>\n#include <cctype>\n#include <utility>\n \nusing namespace std;\n \ntypedef long long ll;\ntypedef pair <int,int> P;\ntypedef pair <int,P > PP;\n \n//int tx[] = {0,1,0,-1};\n//int ty[] = {-1,a0,1,0};\n \nconst int tx[] = {0,1,1,0};\nconst int ty[] = {0,0,1,1};\n \nstatic const double EPS = 1e-8;\n \nll mypow(ll x,int power){\n ll res = 1;\n while(power>0){\n if(power & 1) res *= x;\n x *= x;\n power >>= 1;\n }\n return res;\n}\n \nvoid init_format(vector<vector<string> >& bit_format){\n bit_format.resize(4);\n \n bit_format[0].push_back(\"0xxxxxxx\");\n \n bit_format[1].push_back(\"110yyyyx\");\n bit_format[1].push_back(\"10xxxxxx\");\n \n bit_format[2].push_back(\"1110yyyy\");\n bit_format[2].push_back(\"10yxxxxx\");\n bit_format[2].push_back(\"10xxxxxx\");\n \n bit_format[3].push_back(\"11110yyy\");\n bit_format[3].push_back(\"10yyxxxx\");\n bit_format[3].push_back(\"10xxxxxx\");\n bit_format[3].push_back(\"10xxxxxx\");\n}\n \nll CountPatterns(int start,int last,const vector<vector<string> >& bit_format,const vector<string>& bits,int len){\n bool isok = true;\n \n int x_num = 0;\n int y_num = 0;\n int y1_num = 0;\n int y0_num = 0;\n \n for(int i=start,k=0;i<bits.size() && i <= last && k< bit_format[len-1].size();i++,k++){\n \n for(int j=0;j<bits[i].size() && j< bit_format[len-1][k].size();j++){\n \n if(bits[i][j]=='x'){\n if(bit_format[len-1][k][j] == 'x'){\n x_num++;\n }\n \n else if(bit_format[len-1][k][j] == 'y'){\n y_num++;\n }\n }\n \n else if(bits[i][j]=='1'){\n if(bit_format[len-1][k][j] =='0'){\n isok = false;\n }\n \n else if(bit_format[len-1][k][j] == 'y'){\n y1_num++;\n }\n else{\n continue;\n }\n }\n else if(bits[i][j] == '0'){\n if(bit_format[len-1][k][j] =='1'){\n isok = false;\n }\n else if(bit_format[len-1][k][j] == 'y'){\n y0_num++;\n }\n }\n }\n }\n \n if(y_num==0 && y1_num==0 && y0_num > 0) isok = false;\n \n ll x_patterns = mypow(2,x_num);\n ll y_patterns = 1;\n \n if(y1_num > 0){\n y_patterns = mypow(2,y_num);\n }\n else{\n y_patterns = mypow(2,y_num)-1;\n }\n \n if(y_patterns == 0) y_patterns = 1;\n if(!isok) return 0;\n \n x_patterns %= 1000000;\n y_patterns %= 1000000;\n \n return x_patterns*y_patterns % 1000000LL;\n}\n \nll dp[1001];\n \nint main(){\n vector<vector<string> > bit_format;\n init_format(bit_format);\n \n int N;\n while(cin >> N){\n if(N==0) break;\n \n string line;\n vector<string> bits;\n for(int i=0;i<N;i++){\n string line;\n cin >> line;\n bits.push_back(line);\n }\n \n \n memset(dp,0,sizeof(dp));\n for(int start=0;start<N ;start++){\n for(int len = 4; len >=1; len--){\n if(start-len+1 < 0) continue;\n ll patterns = CountPatterns(start-len+1,start,bit_format,bits,len); \n dp[start] += patterns * (start-len >=0 ? dp[start-len] : 1);\n dp[start] %= 1000000LL;\n }\n }\n printf(\"%ld\\n\",dp[N-1]%1000000LL);\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 1284, "score_of_the_acc": -0.0233, "final_rank": 4 }, { "submission_id": "aoj_2209_579296", "code_snippet": "#define _USE_MATH_DEFINES\n#define INF 0x3f3f3f3f\n#include <cstdio>\n#include <iostream>\n#include <sstream>\n#include <cmath>\n#include <cstdlib>\n#include <algorithm>\n#include <queue>\n#include <stack>\n#include <limits>\n#include <map>\n#include <string>\n#include <cstring>\n#include <set>\n#include <deque>\n#include <bitset>\n#include <list>\n#include <cctype>\n#include <utility>\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef pair <int,int> P;\ntypedef pair <int,P > PP;\n\n//int tx[] = {0,1,0,-1};\n//int ty[] = {-1,a0,1,0};\n\nconst int tx[] = {0,1,1,0};\nconst int ty[] = {0,0,1,1};\n\nstatic const double EPS = 1e-8;\n\nll mypow(ll x,int power){\n\tll res = 1;\n\twhile(power>0){\n\t\tif(power & 1) res *= x;\n\t\tx *= x;\n\t\tpower >>= 1;\n\t}\n\treturn res;\n}\n\nvoid init_format(vector<vector<string> >& bit_format){\n\tbit_format.resize(4);\n\n\tbit_format[0].push_back(\"0xxxxxxx\");\n\n\tbit_format[1].push_back(\"110yyyyx\");\n\tbit_format[1].push_back(\"10xxxxxx\");\n\n\tbit_format[2].push_back(\"1110yyyy\");\n\tbit_format[2].push_back(\"10yxxxxx\");\n\tbit_format[2].push_back(\"10xxxxxx\");\n\n\tbit_format[3].push_back(\"11110yyy\");\n\tbit_format[3].push_back(\"10yyxxxx\");\n\tbit_format[3].push_back(\"10xxxxxx\");\n\tbit_format[3].push_back(\"10xxxxxx\");\n}\n\nll CountPatterns(int start,int last,const vector<vector<string> >& bit_format,const vector<string>& bits,int len){\n\tbool isok = true;\n\n\tint x_num = 0;\n\tint y_num = 0;\n\tint y1_num = 0;\n\tint y0_num = 0;\n\n\tfor(int i=start,k=0;i<bits.size() && i <= last && k< bit_format[len-1].size();i++,k++){\n\n\t\tfor(int j=0;j<bits[i].size() && j< bit_format[len-1][k].size();j++){\n\n\t\t\tif(bits[i][j]=='x'){\n\t\t\t\tif(bit_format[len-1][k][j] == 'x'){\n\t\t\t\t\tx_num++;\n\t\t\t\t}\n\n\t\t\t\telse if(bit_format[len-1][k][j] == 'y'){\n\t\t\t\t\ty_num++;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\telse if(bits[i][j]=='1'){\n\t\t\t\tif(bit_format[len-1][k][j] =='0'){\n\t\t\t\t\tisok = false;\n\t\t\t\t}\n\n\t\t\t\telse if(bit_format[len-1][k][j] == 'y'){\n\t\t\t\t\ty1_num++;\n\t\t\t\t}\n\t\t\t\telse{\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t}\n\t\t\telse if(bits[i][j] == '0'){\n\t\t\t\tif(bit_format[len-1][k][j] =='1'){\n\t\t\t\t\tisok = false;\n\t\t\t\t}\n\t\t\t\telse if(bit_format[len-1][k][j] == 'y'){\n\t\t\t\t\ty0_num++;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tif(y_num==0 && y1_num==0 && y0_num > 0) isok = false;\n\n\tll x_patterns = mypow(2,x_num);\n\tll y_patterns = 1;\n\n\tif(y1_num > 0){\n\t\ty_patterns = mypow(2,y_num);\n\t}\n\telse{\n\t\ty_patterns = mypow(2,y_num)-1;\n\t}\n\n\tif(y_patterns == 0) y_patterns = 1;\n\tif(!isok) return 0;\n\n\tx_patterns %= 1000000;\n\ty_patterns %= 1000000;\n\n\treturn x_patterns*y_patterns % 1000000LL;\n}\n\nll dp[1001];\n\nint main(){\n\tvector<vector<string> > bit_format;\n\tinit_format(bit_format);\n\n\tint N;\n\twhile(cin >> N){\n\t\tif(N==0) break;\n\n\t\tstring line;\n\t\tvector<string> bits;\n\t\tfor(int i=0;i<N;i++){\n\t\t\tstring line;\n\t\t\tcin >> line;\n\t\t\tbits.push_back(line);\n\t\t}\n\n\t\t\n\t\tmemset(dp,0,sizeof(dp));\n\t\tfor(int start=0;start<N ;start++){\n\t\t\tfor(int len = 4; len >=1; len--){\n\t\t\t\tif(start-len+1 < 0) continue;\n\t\t\t\tll patterns = CountPatterns(start-len+1,start,bit_format,bits,len);\t\n\t\t\t\tdp[start] += patterns * (start-len >=0 ? dp[start-len] : 1);\n\t\t\t\tdp[start] %= 1000000LL;\n\t\t\t}\n\t\t}\n\t\tprintf(\"%ld\\n\",dp[N-1]%1000000LL);\n\t}\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 1284, "score_of_the_acc": -0.0233, "final_rank": 4 }, { "submission_id": "aoj_2209_579203", "code_snippet": "#define _USE_MATH_DEFINES\n#define INF 0x3f3f3f3f\n#include <cstdio>\n#include <iostream>\n#include <sstream>\n#include <cmath>\n#include <cstdlib>\n#include <algorithm>\n#include <queue>\n#include <stack>\n#include <limits>\n#include <map>\n#include <string>\n#include <cstring>\n#include <set>\n#include <deque>\n#include <bitset>\n#include <list>\n#include <cctype>\n#include <utility>\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef pair <int,int> P;\ntypedef pair <int,P > PP;\n\n//int tx[] = {0,1,0,-1};\n//int ty[] = {-1,a0,1,0};\n\nconst int tx[] = {0,1,1,0};\nconst int ty[] = {0,0,1,1};\n\nstatic const double EPS = 1e-8;\n\nll mypow(ll x,int power){\n\tll res = 1;\n\twhile(power>0){\n\t\tif(power & 1) res *= x;\n\t\tx *= x;\n\t\tpower >>= 1;\n\t}\n\treturn res;\n}\n\nvoid init_format(vector<vector<string> >& bit_format){\n\tbit_format.resize(4);\n\n\tbit_format[0].push_back(\"0xxxxxxx\");\n\n\tbit_format[1].push_back(\"110yyyyx\");\n\tbit_format[1].push_back(\"10xxxxxx\");\n\n\tbit_format[2].push_back(\"1110yyyy\");\n\tbit_format[2].push_back(\"10yxxxxx\");\n\tbit_format[2].push_back(\"10xxxxxx\");\n\n\tbit_format[3].push_back(\"11110yyy\");\n\tbit_format[3].push_back(\"10yyxxxx\");\n\tbit_format[3].push_back(\"10xxxxxx\");\n\tbit_format[3].push_back(\"10xxxxxx\");\n}\n\nll CountPatterns(int start,int last,const vector<vector<string> >& bit_format,const vector<string>& bits,int len){\n\tbool isok = true;\n\n\tint x_num = 0;\n\tint y_num = 0;\n\tint y1_num = 0;\n\tint y0_num = 0;\n\n\tfor(int i=start,k=0;i<bits.size() && i <= last && k< bit_format[len-1].size();i++,k++){\n\n\t\tfor(int j=0;j<bits[i].size() && j< bit_format[len-1][k].size();j++){\n\n\t\t\tif(bits[i][j]=='x'){\n\t\t\t\tif(bit_format[len-1][k][j] == 'x'){\n\t\t\t\t\tx_num++;\n\t\t\t\t}\n\n\t\t\t\telse if(bit_format[len-1][k][j] == 'y'){\n\t\t\t\t\ty_num++;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\telse if(bits[i][j]=='1'){\n\t\t\t\tif(bit_format[len-1][k][j] =='0'){\n\t\t\t\t\tisok = false;\n\t\t\t\t}\n\n\t\t\t\telse if(bit_format[len-1][k][j] == 'y'){\n\t\t\t\t\ty1_num++;\n\t\t\t\t}\n\t\t\t\telse{\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t}\n\t\t\telse if(bits[i][j] == '0'){\n\t\t\t\tif(bit_format[len-1][k][j] =='1'){\n\t\t\t\t\tisok = false;\n\t\t\t\t}\n\t\t\t\telse if(bit_format[len-1][k][j] == 'y'){\n\t\t\t\t\ty0_num++;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tif(y_num==0 && y1_num==0 && y0_num > 0) isok = false;\n\n\tint x_patterns = mypow(2,x_num);\n\tint y_patterns = 1;\n\n\tif(y1_num > 0){\n\t\ty_patterns = mypow(2,y_num);\n\t}\n\telse{\n\t\ty_patterns = mypow(2,y_num)-1;\n\t}\n\n\tif(y_patterns == 0) y_patterns = 1;\n\tif(!isok) return 0;\n\n\tx_patterns %= 1000000;\n\ty_patterns %= 1000000;\n\n\treturn x_patterns*y_patterns % 1000000LL;\n}\n\nll dp[1001];\n\nint main(){\n\tvector<vector<string> > bit_format;\n\tinit_format(bit_format);\n\n\tint N;\n\twhile(cin >> N){\n\t\tif(N==0) break;\n\n\t\tstring line;\n\t\tvector<string> bits;\n\t\tfor(int i=0;i<N;i++){\n\t\t\tstring line;\n\t\t\tcin >> line;\n\t\t\tbits.push_back(line);\n\t\t}\n\n\t\t\n\t\tmemset(dp,0,sizeof(dp));\n\t\tfor(int start=0;start<N ;start++){\n\t\t\tfor(int len = 4; len >=1; len--){\n\t\t\t\tif(start-len+1 < 0) continue;\n\t\t\t\tll patterns = CountPatterns(start-len+1,start,bit_format,bits,len);\t\n\t\t\t\tdp[start] += patterns * (start-len >=0 ? dp[start-len] : 1);\n\t\t\t\tdp[start] %= 1000000LL;\n\t\t\t\t//printf(\"start-len+1:%d start:%d \\n\",start-len+1,start);\n\t\t\t}\n\t\t}\n\n\t\t//for(int i=0;i<N;i++){\n\t\t//\tfor(int j=i+1;j<N;j++){\n\t\t//\t\tfor(int k=i;k+1<=j;k++){\n\t\t//\t\t\tdp[i][j] += (dp[i][k]%1000000LL) * (dp[k+1][j]%1000000LL);\n\t\t//\t\t}\n\t\t//\t}\n\t\t//}\n\n\t\tprintf(\"%ld\\n\",dp[N-1]%1000000LL);\n\t}\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 1284, "score_of_the_acc": -0.0233, "final_rank": 4 }, { "submission_id": "aoj_2209_565965", "code_snippet": "#include<stdio.h>\n#include<set>\n#include<string>\n#include<iostream>\n#include<string.h>\nstd::set<std::string> patterns[5][4],patternsAllY0[5][4];//yがめんどくさいのでyの全パタンを最初に決定しておく\n\nlong long int mod=1000000;\n\nvoid createCheckPattern(std::string str,int p,int no1,int no2,int yCount,bool hitY){\n\tif(p==8){\n\t\tif(yCount==0&&hitY==true)patternsAllY0[no1][no2].insert(str);\n\t\telse patterns[no1][no2].insert(str);\n\t\treturn ;\n\t}\n\t\n\tchar c=str[p];\n\tif(c=='x'||c=='y'){\n\t\tstr[p]='0';\n\t\tbool hit=(c=='y');\n\t\tcreateCheckPattern(str,p+1,no1,no2,yCount,hitY||hit);\n\t\tstr[p]='1';\n\t\tcreateCheckPattern(str,p+1,no1,no2,yCount+hit,hitY||hit);\n\t\tstr[p]=c;\n\t}else{\n\t\tcreateCheckPattern(str,p+1,no1,no2,yCount,hitY);\n\t}\n}\n\nvoid patternSet(){\n\tcreateCheckPattern(\"0xxxxxxx\",0,1,0,0,false);//1バイト\n\t\n\tcreateCheckPattern(\"110yyyyx\",0,2,0,0,false);//2バイト1つめ文字\n\tcreateCheckPattern(\"10xxxxxx\",0,2,1,0,false);//2バイト2つ目\n\t\n\tcreateCheckPattern(\"1110yyyy\",0,3,0,0,false);//3バイト1つ目\n\tcreateCheckPattern(\"10yxxxxx\",0,3,1,0,false);//3バイト2つ目\n\tcreateCheckPattern(\"10xxxxxx\",0,3,2,0,false);//3バイト3つ目\n\t\n\tcreateCheckPattern(\"11110yyy\",0,4,0,0,false);//4バイト1つ目\n\tcreateCheckPattern(\"10yyxxxx\",0,4,1,0,false);//4バイト2つ目\n\tcreateCheckPattern(\"10xxxxxx\",0,4,2,0,false);//4バイト3つ目\n\tcreateCheckPattern(\"10xxxxxx\",0,4,3,0,false);//4バイト4つ目\n\t/*for(int i=1;i<=4;i++){\n\t\tfor(int j=0;j<i;j++)printf(\"(%d %d %d)\",patterns[i][j].size(),i,j);\n\t\tprintf(\"\\n\");\n\t\tfor(int j=0;j<i;j++)printf(\"(%d %d %d)\",patternsAllY0[i][j].size(),i,j);\n\t\tprintf(\"\\n\\n\");\n\t}*/\n}\n\nvoid saiki(std::string& str,int p,int no1,int no2,long long int &re1,long long int& re2){\n\tif(p==0)re1=re2=0;\n\tif(p==8){\n\t\tif(patterns[no1][no2].find(str)!=patterns[no1][no2].end())re1++;\n\t\telse if(patternsAllY0[no1][no2].find(str)!=patternsAllY0[no1][no2].end())re2++;\n\t}else{\n\t\tif(str[p]=='x'){\n\t\t\tstr[p]='0';\n\t\t\tsaiki(str,p+1,no1,no2,re1,re2);\n\t\t\tstr[p]='1';\n\t\t\tsaiki(str,p+1,no1,no2,re1,re2);\n\t\t\tstr[p]='x';\n\t\t}else{\n\t\t\tsaiki(str,p+1,no1,no2,re1,re2);\n\t\t}\n\t}\n}\n\nvoid calc(int n){\n\tstd::string strs[1002];\n\tfor(int i=0;i<n;i++){\n\t\tstd::cin>>strs[i];\n\t}\n\tlong long int memo[1010]={1,0};\n\n\tfor(int i=0;i<n;i++){\n\t\tif(i+4<=n){\n\t\t\tlong long int ts[2][4];\n\t\t\tmemset(ts,0,sizeof(ts));\n\t\t\tsaiki(strs[i+0],0,4,0,ts[0][0],ts[1][0]);\n\t\t\tsaiki(strs[i+1],0,4,1,ts[0][1],ts[1][1]);\n\t\t\tsaiki(strs[i+2],0,4,2,ts[0][2],ts[1][2]);\n\t\t\tsaiki(strs[i+3],0,4,3,ts[0][3],ts[1][3]);\n\t\t\tlong long int t=0;\n\t\t\tfor(int s=0;s<2;s++){\n\t\t\t\tfor(int j=0;j<2;j++){\n\t\t\t\t\tfor(int k=0;k<2;k++){\n\t\t\t\t\t\tfor(int L=0;L<2;L++){\n\t\t\t\t\t\t\tt+=ts[s][0]*ts[j][1]*ts[k][2]*ts[L][3];\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tt-=ts[1][0]*ts[1][1]*ts[0][2]*ts[0][3];\n\t\t\tt%=mod;\n\t\t\tmemo[i+4]=(memo[i+4]+memo[i]*t)%mod;\n\t\t}\n\t\tif(i+3<=n){\n\t\t\tlong long int ts[2][4];\n\t\t\tmemset(ts,0,sizeof(ts));\n\t\t\tsaiki(strs[i+0],0,3,0,ts[0][0],ts[1][0]);\n\t\t\tsaiki(strs[i+1],0,3,1,ts[0][1],ts[1][1]);\n\t\t\tsaiki(strs[i+2],0,3,2,ts[0][2],ts[1][2]);\n\t\t\tlong long int t=0;\n\t\t\tfor(int s=0;s<2;s++){\n\t\t\t\tfor(int j=0;j<2;j++){\n\t\t\t\t\tfor(int k=0;k<2;k++){\n\t\t\t\t\t\tt+=ts[s][0]*ts[j][1]*ts[k][2];\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tt-=ts[1][0]*ts[1][1]*ts[0][2];\n\t\t\tt%=mod;\n\t\t\tmemo[i+3]=(memo[i+3]+memo[i]*t)%mod;\n\t\t}\n\t\tif(i+2<=n){\n\t\t\tlong long int ts[2][4];\n\t\t\tmemset(ts,0,sizeof(ts));\n\t\t\tsaiki(strs[i+0],0,2,0,ts[0][0],ts[1][0]);\n\t\t\tsaiki(strs[i+1],0,2,1,ts[0][1],ts[1][1]);\n\t\t\tlong long int t=0;\n\t\t\tfor(int s=0;s<2;s++){\n\t\t\t\tfor(int j=0;j<2;j++){\n\t\t\t\t\tt+=ts[s][0]*ts[j][1];\n\t\t\t\t}\n\t\t\t}\n\t\t\tt-=ts[1][0]*ts[0][1];\n\t\t\tt%=mod;\n\t\t\tmemo[i+2]=(memo[i+2]+memo[i]*t)%mod;\n\t\t}\n\t\tif(i+1<=n){\n\t\t\tlong long int ts[2][4];\n\t\t\tmemset(ts,0,sizeof(ts));\n\t\t\tlong long int t=0;\n\t\t\tsaiki(strs[i],0,1,0,ts[0][0],ts[1][0]);\n\t\t\tmemo[i+1]=(memo[i+1]+memo[i]*ts[0][0])%mod;\n\t\t}\n\t}\n\tstd::cout<<memo[n]%mod<<\"\\n\";\n}\n\nint main(){\n\tint n;\n\tpatternSet();\n\tchar text[1000][10];\n\twhile(1){\n\t\tscanf(\"%d\",&n);\n\t\tif(n==0)break;\n\t\tcalc(n);\n\t}\n}", "accuracy": 1, "time_ms": 750, "memory_kb": 1364, "score_of_the_acc": -0.747, "final_rank": 16 }, { "submission_id": "aoj_2209_538579", "code_snippet": "#include <iostream>\n#include <sstream>\n#include <string>\n#include <algorithm>\n#include <vector>\n#include <stack>\n#include <queue>\n#include <set>\n#include <map>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <cmath>\n#include <cassert>\n\nusing namespace std;\n\n#define FOR(i,k,n) for(int i=(k); i<(int)(n); ++i)\n#define REP(i,n) FOR(i,0,n)\n#define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)\n\ntemplate<class T> void debug(T begin, T end){ for(T i = begin; i != end; ++i) cerr<<*i<<\" \"; cerr<<endl; }\ninline bool valid(int x, int y, int W, int H){ return (x >= 0 && y >= 0 && x < W && y < H); }\n\ntypedef long long ll;\nconst int INF = 100000000;\nconst double EPS = 1e-8;\nconst int MOD = 1000000007;\nint dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};\nint dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};\nll memo[1001];\nint calc(int idx, vector<string>& v){\n int N = v.size();\n if(idx == N) return 1;\n if(memo[idx] != -1) return memo[idx];\n ll &res = memo[idx];\n res = 0;\n string s;\n string mask[4] = {\n \"0xxxxxxx\",\n \"110yyyyx10xxxxxx\",\n \"1110yyyy10yxxxxx10xxxxxx\",\n \"11110yyy10yyxxxx10xxxxxx10xxxxxx\"\n };\n for(int l = 0; l < 4; l++){\n if(idx + l == N) return res;\n s += v[idx + l];\n bool y_judge[256] = {};\n bool ok = true;\n REP(i, mask[l].size()) {\n if(mask[l][i] == 'y'){\n y_judge[s[i]] = true;\n }\n if(mask[l][i] == '0' && s[i] == '1') ok = false;\n if(mask[l][i] == '1' && s[i] == '0') ok = false;\n }\n if(l >= 1 && !y_judge['x'] && !y_judge['1']) ok = false;\n if(!ok) continue;\n ll cnt1 = 1, cnt2 = 1;\n REP(i, mask[l].size()){\n if(mask[l][i] == 'y' && s[i] == 'x') cnt1 <<= 1;\n if(mask[l][i] == 'x' && s[i] == 'x') cnt2 <<= 1;\n }\n if(l >= 1 && !y_judge['1']) cnt1--;\n res += cnt1 * cnt2 * calc(idx + 1 + l, v);\n res %= 1000000;\n }\n return res;\n}\nint main(){\n int N;\n while(cin>>N && N){\n vector<string> v(N);\n REP(i, N) cin>>v[i];\n memset(memo, -1, sizeof(memo));\n cout<<calc(0, v)<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 1992, "score_of_the_acc": -0.0937, "final_rank": 12 } ]
aoj_2208_cpp
Problem E: トーマス・ライトの憂鬱 昨日の夕方の話。 普段通り大学での講義が終わり、日課であるキャンパスでの猫への餌付けを済ませた後、家に帰り着くと、作業着を着た人たちが僕の家の扉を取り替える工事をしていた。 それだけなら特に僕の頭痛の種を増やすような出来事ではないのだが、取り外されて去っていく慣れ親しんだ鍵とレバーがついている茶色の扉が非常に平凡であるのに対して、今からまさに取り付けられようとしている黒色の扉が非常に奇妙なもので、ああこれはまたうちの親父が何かを思いついてしまったのだなと容易に想像がついてしまうのであった。 その偏屈親父が言うには、鍵は無くす可能性があるので信用することが出来ない。手元に鍵があったとしても、目を離している隙に鍵の複製が作られてしまったかもしれない。物にセキュリティーを頼る時代はもう終わりだ。これからはパスワードを家族の各人が覚え、そうすることによって世界征服を目論む悪の科学者から我輩の発明品を守るのだ、ということらしい。 新しく設置された扉には縦にN個ずつ、横にN個ずつの正方形をかたちどるように、縦にN+1本、横にN+1本の線が薄青色で引かれていた。 一行目の正方形の左にはA、二行目の正方形の左にはB、というように、ご丁寧に行番号が書かれている。 一列目の正方形の上には1、二列目の正方形の上には2、と、列番号はどうやら数字のようだ。 N 2 個の正方形には各々スイッチが一つずつ配置されており、OFFの状態だと真っ黒だが、ONの状態だと赤い丸が浮かび上がる、というセキュリティーを考えるならもう少しやりようがあるだろうというギミックが付いている。 そして全てのスイッチのON/OFFを正しく合わせたときのみ、扉が開く、という派手好きの親父が大満足な仕様となっているようだ。 さて、ここでの僕の問題は、このパスワードをきちんと覚えられる自信が無い、ということだ。 自分で言うのも何なのだが、僕は頭がそんなに悪くないつもりなので、パスワードの大体は覚えていられるだろうと思うのだが、機械という物は融通が効かないものだからほんのちょっと間違っただけで、寒空の下で夜を過ごすことになりかねない。 しかし、パスワードを書き写して持ち運べば、親父に見つかったときに小遣い抜きなどの理不尽な制裁が加えられることが予想される。 そこで僕はある作戦を思いついたんだ。 各行、各列の正方形に含まれる赤丸の個数をメモしてそれを持ち運ぼう。 一行目のN個の正方形に含まれる赤丸の個数、二行目のN個の....、 一列目のN個の正方形に含まれる赤丸の個数、二行目のN個の....、 という風に2N個の数字をメモしておけば、もし他の人に見られてもただの数字の羅列だ。何が何だかきっと分からないだろう。 やあわざわざここまで読んでくれた君。 ああそうだ、この作戦にはもしかしたら致命的な欠陥があるかもしれない。 複数のパスワードが、同じメモになってしまうかもしれないのだ。 そうなると、寒空(略)ということになりかねない。我が身の健康を揺るがす一大問題だ。 そこで君には、このメモの数字を入力すると、復元可能なパスワードが一つに定まるかどうかを判定するプログラムを書いてほしい。 一つに定まるなら、Yes、そうでないなら、Noと出力するだけのプログラムさ。簡単だろう? もしかしたらメモをとった時に赤丸を数え間違ったかもしれない。その場合は復元できるパスワードが1つも存在しないかもしれないが、その時は迷わずNoと返してくれ。 ああそうそう、間違っても僕の家に勝手に入ろうとはしないでくれよ。親父の趣味の危ない機械や罠が並んでいるから、命の保証が出来ないからね。 Input 入力は次のような形式で与えられる。 N 1行目の和 2行目の和 ... 1列目の和 2列目の和 ... ただし、N は 1 ≤ N ≤ 10000 を満たす整数である。 Output 問題文に従って、YesもしくはNoを出力せよ。 Notes on Test Cases 上記入力形式で複数のデータセットが与えられます。各データセットに対して上記出力形式で出力を行うプログラムを作成して下さい。 N が 0 のとき入力の終わりを示します。 Sample Input 2 1 2 2 1 3 2 1 2 2 1 2 10 0 1 1 2 5 5 5 8 8 9 0 1 3 3 3 6 6 6 7 9 0 Output for Sample Input Yes No Yes
[ { "submission_id": "aoj_2208_10852901", "code_snippet": "#include <algorithm>\n#include <vector>\n#include <iostream>\nusing namespace std;\nint main() {\n\tint n,t;\n\tfor(;(cin>>n),n;){\n\t\tvector<int> r,l;\n\t\tbool ok=true;\n\t\tfor(int i=0;i<n;i++)(cin>>t),r.push_back(t);\n\t\tfor(int i=0;i<n;i++)(cin>>t),l.push_back(t);\n\t\tstd::sort(r.begin(),r.end(),greater<int>());\n\t\tstd::sort(l.begin(),l.end(),greater<int>());\n\t\tfor(int i=0;i<n;i++){\n\t\t\tfor(int j=0;j<r[i];j++){\n\t\t\t\tl[j]--;\n\t\t\t\tif(l[j]<0)ok=false;\n\t\t\t}\n\t\t}\n\t\tfor(int i=0;i<n;i++){\n\t\t\tif(l[i]!=0)ok=false;\n\t\t}\n\t\tif(ok){\n\t\t\tcout<<\"Yes\"<<endl;\n\t\t}else{\n\t\t\tcout<<\"No\"<<endl;\n\t\t}\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3552, "score_of_the_acc": -0.7274, "final_rank": 16 }, { "submission_id": "aoj_2208_2645458", "code_snippet": "#include <stdio.h>\n#include <cmath>\n#include <algorithm>\n#include <cfloat>\n#include <stack>\n#include <queue>\n#include <vector>\n#include <string>\n#include <iostream>\n#include <set>\n#include <map>\n#include <time.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\nint N;\n\nint yoko[10000],tate[10000],yoko_sum,tate_sum,tate_num;\n\n\nvoid func(){\n\n\tyoko_sum = tate_sum = 0;\n\n\tfor(int i = 0; i < N; i++){\n\t\tscanf(\"%d\",&yoko[i]);\n\t\tyoko_sum += yoko[i];\n\t}\n\tsort(yoko,yoko+N);\n\treverse(yoko,yoko+N);\n\n\tint tate_num = 0;\n\n\tfor(int i = 0; i < N; i++){\n\t\tscanf(\"%d\",&tate[i]);\n\t\ttate_sum += tate[i];\n\t\tif(tate[i] > 0)tate_num++;\n\t}\n\n\tif(yoko_sum != tate_sum){\n\t\tprintf(\"No\\n\");\n\t\treturn;\n\t}\n\n\tfor(int i = 0; i < N; i++){\n\t\tif(yoko[i] != tate_num){\n\t\t\tprintf(\"No\\n\");\n\t\t\treturn;\n\t\t}\n\t\tfor(int k = 0; k < N; k++){\n\t\t\tif(tate[k] > 0){\n\t\t\t\ttate[k]--;\n\t\t\t\tif(tate[k] == 0)tate_num--;\n\t\t\t}\n\t\t}\n\t}\n\n\tprintf(\"Yes\\n\");\n}\n\nint main(){\n\n\twhile(true){\n\t\tscanf(\"%d\",&N);\n\t\tif(N == 0)break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 3276, "score_of_the_acc": -0.7048, "final_rank": 15 }, { "submission_id": "aoj_2208_1779387", "code_snippet": "#include \"bits/stdc++.h\"\n#include <cassert>\nusing namespace std;\ntypedef long long ll;\ntypedef pair<int,int> pii;\n#define rep(i,n) for(ll i=0;i<(ll)(n);i++)\n#define all(a) (a).begin(),(a).end()\n#define vi vector<int>\n#define pb push_back\n#define INF 999999999\n//#define INF (1LL<<59)\n\n\nint main(){\n\tint n;\n\twhile(cin>>n&&n){\n\t\tvi r(n),c(n);\n\t\trep(i,n)cin>>r[i];\n\t\trep(i,n)cin>>c[i];\n\t\t\n\t\tsort(all(r),greater<int>());\n\t\tsort(all(c),greater<int>());\n\t\t\n\t\trep(col,n){\n\t\t\trep(row,n){\n\t\t\t\tif(r[row]>0){\n\t\t\t\t\tr[row]--;\n\t\t\t\t\tc[col]--;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\t\n\t\tbool can=true;\n\t\trep(i,n){\n\t\t\tif(c[i]!=0)can=false;\n\t\t}\n\t\trep(i,n){\n\t\t\tif(r[i]!=0)can=false;\n\t\t}\n\t\t\n\t\tif(can)cout<<\"Yes\"<<endl;\n\t\telse cout<<\"No\"<<endl;\n\t}\n}", "accuracy": 1, "time_ms": 110, "memory_kb": 3136, "score_of_the_acc": -0.7307, "final_rank": 17 }, { "submission_id": "aoj_2208_1434274", "code_snippet": "#include<cstdio>\n#include<cstdlib>\n#include<cstring>\n#include<cmath>\n#include<iostream>\n#include<string>\n#include<vector>\n#include<map>\n#include<set>\n#include<list>\n#include<queue>\n#include<deque>\n#include<algorithm>\n#include<numeric>\n#include<utility>\n#include<complex>\n#include<functional>\n \nusing namespace std;\n\n/* constant */\n\nconst int MAX_N = 10000;\n\n/* typedef */\n\n/* global variables */\n\nint n;\nint hsums[MAX_N], vsums[MAX_N];\n\n/* subroutines */\n\n/* main */\n\nint main() {\n for (;;) {\n cin >> n;\n if (n == 0) break;\n\n for (int i = 0; i < n; i++) cin >> hsums[i];\n for (int i = 0; i < n; i++) cin >> vsums[i];\n sort(hsums, hsums + n);\n sort(vsums, vsums + n);\n\n int hmin = 0, hmax = n, hp0 = 0, hp1 = n - 1;\n int vmin = 0, vmax = n, vp0 = 0, vp1 = n - 1;\n bool changed = true;\n\n while (changed) {\n changed = false;\n\n while (hp0 <= hp1 && hsums[hp0] == hmin)\n\thp0++, vmax--, changed = true;\n while (hp0 <= hp1 && hsums[hp1] == hmax)\n\thp1--, vmin++, changed = true;\n\n if (hp0 <= hp1 && (hsums[hp0] < hmin || hsums[hp1] > hmax)) break;\n\n while (vp0 <= vp1 && vsums[vp0] == vmin)\n\tvp0++, hmax--, changed = true;\n while (vp0 <= vp1 && vsums[vp1] == vmax)\n\tvp1--, hmin++, changed = true;\n\t\n if (vp0 <= vp1 && (vsums[vp0] < vmin || vsums[vp1] > vmax)) break;\n }\n\n bool ok = (hp0 > hp1 && vp0 > vp1);\n cout << (ok ? \"Yes\" : \"No\") << endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1248, "score_of_the_acc": -0.248, "final_rank": 2 }, { "submission_id": "aoj_2208_1262717", "code_snippet": "#include <iostream>\n#include <complex>\n#include <sstream>\n#include <string>\n#include <algorithm>\n#include <deque>\n#include <list>\n#include <map>\n#include <numeric>\n#include <queue>\n#include <vector>\n#include <set>\n#include <limits>\n#include <cstdio>\n#include <cctype>\n#include <cmath>\n#include <cstring>\n#include <cstdlib>\n#include <ctime>\nusing namespace std;\n#define REP(i, j) for(int i = 0; i < (int)(j); ++i)\n#define FOR(i, j, k) for(int i = (int)(j); i < (int)(k); ++i)\n#define SORT(v) sort((v).begin(), (v).end())\n#define REVERSE(v) reverse((v).begin(), (v).end())\ntypedef complex<double> P;\n\nint N;\n\nbool solve(vector<int> &H, vector<int> &W){\n REP(i, N) REP(j, N) if(W[j] > 0) { W[j]--; H[i]--; }\n REP(i, N) if(H[i] != 0) return 0;\n return 1;\n}\n\nint main() {\n while(cin >>N && N){\n vector<int> H(N), W(N);\n REP(i, N) cin >>H[i];\n REP(i, N) cin >>W[i];\n SORT(H); REVERSE(H);\n cout <<(solve(H, W) ? \"Yes\" : \"No\") <<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 140, "memory_kb": 1284, "score_of_the_acc": -0.395, "final_rank": 10 }, { "submission_id": "aoj_2208_898498", "code_snippet": "#include<iostream>\n#include<algorithm>\n#include<queue>\n\nusing namespace std;\n\n#define rep(i, n) for (int i = 0; i < int(n); ++i)\n\nint main() {\n while (true) {\n int n;\n cin >> n;\n if (n == 0) break;\n deque<int> a(n), b(n);\n rep (i, n) cin >> a[i];\n rep (i, n) cin >> b[i];\n sort(a.begin(), a.end());\n sort(b.begin(), b.end());\n int na = 0, nb = 0;\n rep (i, n) if (a[i] == 0) ++na;\n rep (i, n) if (b[i] == 0) ++nb;\n rep (_, 2 * n) {\n if (a[n - 1] > 0 && a[n - 1] + nb == n) {\n\trep (i, n) if (b[i] > 0) {\n\t --b[i];\n\t if (b[i] == 0) ++nb;\n\t}\n\t++na;\n\ta.push_front(0);\n\ta.pop_back();\n }\n if (b[n - 1] > 0 && b[n - 1] + na == n) {\n\trep (i, n) if (a[i] > 0) {\n\t --a[i];\n\t if (a[i] == 0) ++na;\n\t}\n\t++nb;\n\tb.push_front(0);\n\tb.pop_back();\n }\n }\n if (na == n && nb == n) {\n cout << \"Yes\" << endl;\n } else {\n cout << \"No\" << endl;\n }\n }\n}", "accuracy": 1, "time_ms": 350, "memory_kb": 1312, "score_of_the_acc": -0.6263, "final_rank": 14 }, { "submission_id": "aoj_2208_877896", "code_snippet": "#include <iostream>\n#include <algorithm>\nusing namespace std;\n\nint main() {\n \n int n;\n int w[10000], h[10000];\n while(cin >> n && n) {\n for(int i=0; i<n; i++) cin >> w[i];\n for(int i=0; i<n; i++) cin >> h[i];\n sort(h, h+n, greater<int>());\n \n for(int i=0; i<n; i++)\n for(int j=0; j<n; j++)\n\tif(w[j]) w[j] --, h[i] --;\n \n bool ok = 1;\n for(int i=0; i<n; i++)\n if(h[i]) { ok = 0; }\n \n if(ok) cout << \"Yes\" << endl;\n else cout << \"No\"<< endl;\n }\n \n return 0;\n}", "accuracy": 1, "time_ms": 150, "memory_kb": 1248, "score_of_the_acc": -0.3986, "final_rank": 11 }, { "submission_id": "aoj_2208_874035", "code_snippet": "#include<iostream>\n#include<algorithm>\n\n#define REP(i,s,n) for(int i=s;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n#define MAX 10010\n\nusing namespace std;\n\nint r[MAX],c[MAX];\n\nint main(){\n int n;\n while(cin >> n,n){\n rep(i,n)cin >> r[i]; \n rep(i,n)cin >> c[i];\n sort(r,r+n,greater<int>());\n sort(c,c+n,greater<int>());\n bool judge = true;\n if(r[0] > n || c[0] > n)judge = false;\n rep(i,n){\n if(!judge)break;\n rep(j,c[i])r[j]--;\n }\n rep(i,n)if(r[i] != 0)judge = false;\n cout << (judge?\"Yes\":\"No\") << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 1248, "score_of_the_acc": -0.291, "final_rank": 4 }, { "submission_id": "aoj_2208_787293", "code_snippet": "#include<iostream>\n#include<algorithm>\n#include<vector>\nusing namespace std;\n#define INF ( 1 << 28 )\n#define reps(i,j,n) for(int i = (j) ; i < (n) ; i++ )\n#define rep(i,n) reps(i,0,n)\n\nint main(){\n int N;\n while(cin >> N , N){\n bool flg = true;\n vector<int> a(N),b(N);\n rep(i,N) cin >> a[i];\n rep(i,N) cin >> b[i];\n sort(b.rbegin(),b.rend());\n rep(i,N){\n rep(j,N) if(a[j] > 0) a[j]--,b[i]--;\n if(b[i] != 0){\n flg = false;\n break;\n }\n }\n cout << (flg ? \"Yes\" : \"No\") << endl;\n }\n}", "accuracy": 1, "time_ms": 140, "memory_kb": 1268, "score_of_the_acc": -0.3918, "final_rank": 9 }, { "submission_id": "aoj_2208_605051", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\nusing namespace std;\nint main()\n{\n for(;;){\n int n;\n cin >> n;\n if(n==0) break;\n vector<int> r(n),c(n);\n for(int i=0; i<n; i++){\n cin >> r[i];\n }\n for(int i=0; i<n; i++){\n cin >> c[i];\n }\n sort(r.begin(),r.end());\n sort(c.rbegin(),c.rend());\n bool bOK = true;\n for(int i=0; i<n; i++){\n int count = 0;\n for(int j=0; j<n; j++){\n if(r[j] > 0){\n count++;\n r[j]--;\n }\n }\n if(c[i] != count){\n bOK = false;\n break;\n }\n }\n if(bOK) cout << \"Yes\" << endl;\n else cout << \"No\" << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 1284, "score_of_the_acc": -0.3197, "final_rank": 6 }, { "submission_id": "aoj_2208_600882", "code_snippet": "#define _USE_MATH_DEFINES\n#define INF 0x3f3f3f3f\n#include <cstdio>\n#include <iostream>\n#include <sstream>\n#include <cmath>\n#include <cstdlib>\n#include <algorithm>\n#include <queue>\n#include <stack>\n#include <limits>\n#include <map>\n#include <string>\n#include <cstring>\n#include <set>\n#include <deque>\n#include <bitset>\n#include <list>\n#include <cctype>\n#include <utility>\n \nusing namespace std;\n \ntypedef long long ll;\ntypedef pair <int,int> P;\ntypedef pair <int,P > PP;\n \nint tx[] = {0,1,0,-1};\nint ty[] = {-1,0,1,0};\n \nstatic const double EPS = 1e-8;\n \n \n \nint main(){\n int N;\n while(~scanf(\"%d\",&N)){\n if(N==0) break;\n \n vector<int> row;\n vector<int> col;\n for(int i=0;i<N;i++){\n int num;\n cin >> num;\n row.push_back(num);\n }\n \n for(int i=0;i<N;i++){\n int num;\n cin >> num;\n col.push_back(num);\n }\n sort(col.begin(),col.end(),greater<int>());\n \n bool isok = true;\n for(int i=0;i<col.size();i++){ \n for(int j=0;j<row.size();j++){\n\tif(row[j] > 0){\n\t col[i]--;\n\t row[j]--;\n\t}\n }\n }\n \n if(count(col.begin(),col.end(),0) != col.size()\n || count(row.begin(),row.end(),0) != row.size()){\n isok = false;\n }\n \n printf(\"%s\\n\",isok ? \"Yes\" : \"No\");\n }\n}", "accuracy": 1, "time_ms": 160, "memory_kb": 1360, "score_of_the_acc": -0.4316, "final_rank": 12 }, { "submission_id": "aoj_2208_571142", "code_snippet": "#define _USE_MATH_DEFINES\n#define INF 0x3f3f3f3f\n#include <cstdio>\n#include <iostream>\n#include <sstream>\n#include <cmath>\n#include <cstdlib>\n#include <algorithm>\n#include <queue>\n#include <stack>\n#include <limits>\n#include <map>\n#include <string>\n#include <cstring>\n#include <set>\n#include <deque>\n#include <bitset>\n#include <list>\n#include <cctype>\n#include <utility>\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef pair <int,int> P;\ntypedef pair <int,P > PP;\n\nint tx[] = {0,1,0,-1};\nint ty[] = {-1,0,1,0};\n\nstatic const double EPS = 1e-8;\n\n\n\nint main(){\n\tint N;\n\twhile(~scanf(\"%d\",&N)){\n\t\tif(N==0) break;\n\n\t\tvector<int> row;\n\t\tvector<int> col;\n\t\tfor(int i=0;i<N;i++){\n\t\t\tint num;\n\t\t\tcin >> num;\n\t\t\trow.push_back(num);\n\t\t}\n\n\t\tfor(int i=0;i<N;i++){\n\t\t\tint num;\n\t\t\tcin >> num;\n\t\t\tcol.push_back(num);\n\t\t}\n\t\tsort(col.begin(),col.end(),greater<int>());\n\n\t\tbool isok = true;\n\t\tfor(int i=0;i<col.size();i++){\t\t\t\n\t\t\tfor(int j=0;j<row.size();j++){\n\t\t\t\tif(row[j] > 0){\n\t\t\t\t\tcol[i]--;\n\t\t\t\t\trow[j]--;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tif(count(col.begin(),col.end(),0) != col.size()\n\t\t\t|| count(row.begin(),row.end(),0) != row.size()){\n\t\t\t\tisok = false;\n\t\t}\n\n\t\tprintf(\"%s\\n\",isok ? \"Yes\" : \"No\");\n\t}\n}", "accuracy": 1, "time_ms": 170, "memory_kb": 1360, "score_of_the_acc": -0.4423, "final_rank": 13 }, { "submission_id": "aoj_2208_538570", "code_snippet": "#include <iostream>\n#include <sstream>\n#include <string>\n#include <algorithm>\n#include <vector>\n#include <stack>\n#include <queue>\n#include <set>\n#include <map>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <cmath>\n#include <cassert>\n\nusing namespace std;\n\n#define FOR(i,k,n) for(int i=(k); i<(int)(n); ++i)\n#define REP(i,n) FOR(i,0,n)\n#define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)\n\ntemplate<class T> void debug(T begin, T end){ for(T i = begin; i != end; ++i) cerr<<*i<<\" \"; cerr<<endl; }\ninline bool valid(int x, int y, int W, int H){ return (x >= 0 && y >= 0 && x < W && y < H); }\n\ntypedef long long ll;\nconst int INF = 100000000;\nconst double EPS = 1e-8;\nconst int MOD = 1000000007;\nint dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};\nint dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};\nbool solve(vector<int>& a, vector<int>& b){\n int N = a.size();\n sort(a.begin(), a.end(), greater<int>());\n sort(b.begin(), b.end(), greater<int>());\n for(int i = 0; i < N; i++){\n int x = a[i];\n if(x == 0) break;\n if(x > N) return false;\n if(b[x - 1] <= 0) return false;\n if(x != N && b[x] == b[x - 1]) return false;\n for(int j = 0; j < x; j++){ b[j] --; }\n }\n for(int i = 0; i < N; i++) if(b[i] != 0) return false;\n return true;\n}\n\nint main(){\n int N;\n while(cin>>N && N){\n vector<int> a(N), b(N);\n REP(i, N) cin>>a[i];\n REP(i, N) cin>>b[i];\n if(solve(a, b)) cout<<\"Yes\"<<endl;\n else cout<<\"No\"<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 1284, "score_of_the_acc": -0.2874, "final_rank": 3 }, { "submission_id": "aoj_2208_538569", "code_snippet": "#include <iostream>\n#include <sstream>\n#include <string>\n#include <algorithm>\n#include <vector>\n#include <stack>\n#include <queue>\n#include <set>\n#include <map>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <cmath>\n#include <cassert>\n\nusing namespace std;\n\n#define FOR(i,k,n) for(int i=(k); i<(int)(n); ++i)\n#define REP(i,n) FOR(i,0,n)\n#define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)\n\ntemplate<class T> void debug(T begin, T end){ for(T i = begin; i != end; ++i) cerr<<*i<<\" \"; cerr<<endl; }\ninline bool valid(int x, int y, int W, int H){ return (x >= 0 && y >= 0 && x < W && y < H); }\n\ntypedef long long ll;\nconst int INF = 100000000;\nconst double EPS = 1e-8;\nconst int MOD = 1000000007;\nint dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};\nint dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};\nbool solve(vector<int>& a, vector<int>& b){\n int N = a.size();\n sort(a.begin(), a.end(), greater<int>());\n sort(b.begin(), b.end(), greater<int>());\n for(int i = 0; i < N; i++){\n int x = a[i];\n if(x == 0) break;\n if(x > N) return false;\n if(b[x - 1] <= 0) return false;\n if(x != N && b[x] == b[x - 1]) return false;\n for(int j = 0; j < x; j++){ b[j] --; }\n sort(b.begin(), b.end(), greater<int>());\n }\n for(int i = 0; i < N; i++) if(b[i] != 0) return false;\n return true;\n}\n\nint main(){\n int N;\n while(cin>>N && N){\n vector<int> a(N), b(N);\n REP(i, N) cin>>a[i];\n REP(i, N) cin>>b[i];\n if(solve(a, b)) cout<<\"Yes\"<<endl;\n else cout<<\"No\"<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 940, "memory_kb": 1284, "score_of_the_acc": -1.2552, "final_rank": 20 }, { "submission_id": "aoj_2208_530111", "code_snippet": "#include <cstdio>\n#include <cstdlib>\n#include <algorithm>\n#include <list>\n\nusing namespace std;\n\nint\nmain (\n int argc,\n char *argv[ ]\n )\n{\n list<int>::iterator it;\n int i;\n\n for ( ; ; )\n {\n list<int> h, v;\n int n;\n\n scanf ( \"%d\", &n );\n if ( n == 0 ) break ;\n for ( i = 0; i < n; ++i )\n {\n int d;\n\n scanf ( \"%d\", &d );\n h.push_back ( d );\n }\n for ( i = 0; i < n; ++i )\n {\n int d;\n\n scanf ( \"%d\", &d );\n v.push_back ( d );\n }\n\n for ( ; ; )\n {\n it = find ( h.begin ( ), h.end ( ), 0 );\n if ( it != h.end ( ) )\n {\n h.erase ( it );\n continue ;\n }\n it = find ( v.begin ( ), v.end ( ), 0 );\n if ( it != v.end ( ) )\n {\n v.erase ( it );\n continue ;\n }\n\n it = find ( h.begin ( ), h.end ( ), v.size ( ) );\n if ( it != h.end ( ) )\n {\n h.erase ( it );\n for ( it = v.begin ( ); it != v.end ( ); ++it )\n {\n *it -= 1;\n }\n continue ;\n }\n it = find ( v.begin ( ), v.end ( ), h.size ( ) );\n if ( it != v.end ( ) )\n {\n v.erase ( it );\n for ( it = h.begin ( ); it != h.end ( ); ++it )\n {\n *it -= 1;\n }\n continue ;\n }\n\n break ;\n }\n \n puts ( ( h.size ( ) == 0 && v.size ( ) == 0 )\n ? \"Yes\" : \"No\" );\n }\n\n return ( EXIT_SUCCESS );\n}", "accuracy": 1, "time_ms": 580, "memory_kb": 1644, "score_of_the_acc": -0.9396, "final_rank": 18 }, { "submission_id": "aoj_2208_522620", "code_snippet": "//10\n#include<iostream>\n#include<vector>\n#include<algorithm>\n#include<functional>\n\nusing namespace std;\n\nint main(){\n for(int n;cin>>n,n;){\n vector<int> c(n),r(n);\n for(int i=0;i<n;i++){\n cin>>c[i];\n }\n sort(c.begin(),c.end());\n for(int i=0;i<n;i++){\n cin>>r[i];\n }\n sort(r.begin(),r.end(),greater<int>());\n for(;;){\n bool u=false;\n for(;;){\n\tif(c.empty())goto end;\n\tif(c.back()!=r.size())break;\n\tc.pop_back();\n\tu=true;\n\tfor(int i=0;i<r.size();i++){\n\t r[i]--;\n\t}\n }\n for(;;){\n\tif(r.empty())goto end;\n\tif(r.back())break;\n\tr.pop_back();\n\tu=true;\n }\n if(!u)break;\n }\n end:\n cout<<((c.empty()||r.empty())?\"Yes\":\"No\")<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 1288, "score_of_the_acc": -0.299, "final_rank": 5 }, { "submission_id": "aoj_2208_371687", "code_snippet": "#include<cstdio>\n#include<algorithm>\n\n#define rep(i,n) for(int i=0;i<(n);i++)\n\nusing namespace std;\n\nint n,row[10000],col[10000];\n\nbool solve(){\n\tsort(row,row+n);\n\tfor(int i=n-1;i>=0;i--){\n\t\trep(j,n){\n\t\t\tif(col[j]>0) col[j]--, row[i]--;\n\t\t}\n\t\tif(row[i]!=0) return false;\n\t}\n\treturn true;\n}\n\nint main(){\n\tfor(;scanf(\"%d\",&n),n;){\n\t\trep(i,n) scanf(\"%d\",row+i);\n\t\trep(j,n) scanf(\"%d\",col+j);\n\t\tputs(solve()?\"Yes\":\"No\");\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 160, "memory_kb": 808, "score_of_the_acc": -0.3219, "final_rank": 7 }, { "submission_id": "aoj_2208_350399", "code_snippet": "#include <iostream>\n#include <algorithm>\n\nusing namespace std;\n\nconst int N = 10000;\nint n;\nint r[N];\nint c[N];\n\nvoid solve()\n{\n\tsort(r, r + n);\n\tsort(c, c + n);\n\tfor (int i = 0; i < n; i++) {\n\t\tif (r[n - i - 1] != (c + n - lower_bound(c, c + n, i + 1))) {\n\t\t\tcout << \"No\" << endl;\n\t\t\treturn;\n\t\t}\n\t}\n\tcout << \"Yes\" << endl;\n}\n\nint main()\n{\n\twhile (cin >> n, n) {\n\t\tfor (int i = 0; i < n; i++) {\n\t\t\tcin >> r[i];\n\t\t}\n\t\tfor (int i = 0; i < n; i++) {\n\t\t\tcin >> c[i];\n\t\t}\n\t\tsolve();\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 0, "score_of_the_acc": 0, "final_rank": 1 }, { "submission_id": "aoj_2208_296082", "code_snippet": "#include <iostream>\n#include <sstream>\n#include <fstream>\n#include <vector>\n#include <list>\n#include <set>\n#include <map>\n#include <stack>\n#include <queue>\n#include <algorithm>\n#include <numeric>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <climits>\n#include <cmath>\nusing namespace std;\n\nint W[10001];\nint H[10001];\n\nint main() {\n int n;\n while ( cin >> n && n ) {\n for ( int i = 0; i < n; i++ ) {\n cin >> W[i];\n }\n for ( int i = 0; i < n; i++ ) {\n cin >> H[i];\n }\n sort( H, H+n, greater<int>() );\n\n for ( int i = 0; i < n; i++ ) {\n for ( int j = 0; j < n; j++ ) {\n if ( W[j] > 0 ) {\n W[j]--;\n H[i]--;\n }\n }\n }\n\n bool flag = true;\n for ( int i = 0; i < n; i++ ) {\n if ( H[i] != 0 ) flag = false;\n }\n cout << ( flag ? \"Yes\" : \"No\" ) << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 180, "memory_kb": 928, "score_of_the_acc": -0.3672, "final_rank": 8 }, { "submission_id": "aoj_2208_278035", "code_snippet": "#include <iostream>\n#include <algorithm>\n#include <vector>\n#include <cstring>\n#include <cstdlib>\n\nusing namespace std;\ntypedef long long ll;\n\nint n;\nint aa[10001];\nint bb[10001];\n\nconst int MAX_N=10001;\nconst int MAX_Q=10001;\nconst int DAT_SIZE=(1<<18)-1;\n//int N,Q;\n//int A[MAX_N];\n//char T[MAX_Q];\n//int L[MAX_Q],R[MAX_Q],X[MAX_Q];\n\n// segmentation tree\nll data[DAT_SIZE];\nll datb[DAT_SIZE];\n\n// [a,b)‚Éx‚ð‰ÁŽZ\n// k‚͐ߓ_‚̔ԍ†‚ŁA‹æŠÔ[l,r)‚ɑΉž\nvoid add(int a,int b,int x,int k,int l,int r){\n if(a<=l&&r<=b)\n data[k]+=x;\n else if(l<b&&a<r){\n datb[k]+=(min(b,r)-max(a,l))*x;\n add(a,b,x,k*2+1,l,(l+r)/2);\n add(a,b,x,k*2+2,(l+r)/2,r);\n }\n}\n// [a,b)‚̘a‚ðŒvŽZ\n// k‚͐ߓ_‚̔ԍ†‚ŁA‹æŠÔ[l,r)‚ɑΉž\nll sum(int a,int b,int k,int l,int r){\n if(b<=l||r<=a)\n return 0;\n else if(a<=l&&r<=b)\n return data[k]*(r-l)+datb[k];\n ll res=(min(b,r)-max(a,l))*data[k];\n res+=sum(a,b,k*2+1,l,(l+r)/2);\n res+=sum(a,b,k*2+2,(l+r)/2,r);\n return res;\n}\n\nint main(){\n\n while(cin>>n&&n!=0){\n // ƒ[ƒƒNƒŠƒA\n memset(data,0,sizeof(data));\n memset(datb,0,sizeof(datb));\n for(int i = 0; i < n; i++)cin>>aa[i];\n for(int i = 0; i < n; i++)cin>>bb[i];\n sort(aa,aa+n);\n sort(bb,bb+n);\n int havingZeroCow=-1;\n // Œ»Ý0‚ðŽ‚Â‚à‚Á‚Æ‚à‰E‚Ì—ñ\n for(int i = 0; i < n; i++){\n if(bb[i]==0)\n havingZeroCow=i;\n else\n break;\n }\n // seg tree‚ɏ‰Šú’l‚ð‰ÁŽZ\n for(int i = 0; i < n; i++)\n add(i,i+1,bb[i],0,0,n);\n bool f=false;\n // ‚à‚Á‚Æ‚à‘å‚«‚¢”Žš‚ðŽ‚Âs‚©‚珇”Ԃɏˆ—\n for(int i = n-1; i >= 0; i--){\n // ‰E‚©‚珇”Ô‚É–„‚߂Ă¢‚Á‚āAÅ‰‚ɋ󗓂ɂȂé—ñ\n int firstEmpty=n-aa[i]-1;\n if(firstEmpty==-1||sum(firstEmpty,firstEmpty+1,0,0,n)==0)\n add(firstEmpty+1,n,-1,0,0,n);\n else{\n f=true;\n break;\n }\n }\n if(f)\n cout<<\"No\"<<endl;\n else\n cout<<\"Yes\"<<endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 5032, "score_of_the_acc": -1.0323, "final_rank": 19 } ]
aoj_2213_cpp
Problem J: 多項式の解の個数 きたまさ君は大学でさまざまな変換について研究している。最近、きたまさ君が興味を持っている変換は次のようなものである。 N+1個の整数 a 0 , ..., a N を固定し、整数 z を入力とする。また P を素数とする。 t = ( a N z N + a N-1 z N-1 + ... + a 2 z 2 + a 1 z + a 0 ) mod P きたまさ君はこの変換によって t = 0 になってしまう z がいくつもあることに気がついた。 そこで、きたまさ君は友人でありスーパープログラマーでもあるあなたに、変換後に t = 0 になる z が 0〜P-1 にいくつあるかを計算してもらうことにした。 Input 入力は次の形式で与えられる。 N P a 0 a 1 ... a N 入力の1行目には整数Nと素数Pがスペース文字で区切られて与えられる。これらは、問題文で与えられたとおりである。 0 ≤ N ≤ 100, 2 ≤ P ≤ 10 9 を満たす。 次の1行には a 0 〜a N がスペース文字で区切られて与えられる。これらは |a i | ≤ 10 9 を満たす整数である。また a N ≠ 0 である。 Output 変換後に0になるような z (0 ≤ z < P) の個数を出力せよ。 Notes on Test Cases 上記入力形式で複数のデータセットが与えられます。各データセットに対して上記出力形式で出力を行うプログラムを作成して下さい。 N, P がともに 0 のとき入力の終わりを示します。 Sample Input 2 3 1 2 1 2 3 1 2 6 0 0 Output for Sample Input 1 1
[ { "submission_id": "aoj_2213_10806496", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n// min non-negative i such that n <= 2^i\nint ceil_pow2(int n) {\n int i = 0;\n while ((1U << i) < (unsigned int)(n)) i++;\n return i;\n}\n\n// min non-negative i such that (x & (1 << i)) != 0\nint bsf(int x) { return __builtin_ctz(x); }\nint bsf(unsigned int x) { return __builtin_ctz(x); }\nint bsf(long long x) { return __builtin_ctzll(x); }\nint bsf(unsigned long long x) { return __builtin_ctzll(x); }\n\n// safe mod\ntemplate<class T_VAL, class T_MOD>\nconstexpr T_VAL safe_mod(T_VAL a, T_MOD m) {\n assert(m > 0);\n a %= m;\n if (a < 0) a += m;\n return a;\n}\n\n// mod pow\ntemplate<class T_VAL, class T_MOD>\nconstexpr T_VAL mod_pow(T_VAL a, T_VAL n, T_MOD m) {\n T_VAL res = 1;\n while (n > 0) {\n if (n % 2 == 1) res = res * a % m;\n a = a * a % m;\n n >>= 1;\n }\n return res;\n}\n\n// mod inv\ntemplate<class T_VAL, class T_MOD>\nconstexpr T_VAL mod_inv(T_VAL a, T_MOD m) {\n T_VAL b = m, u = 1, v = 0;\n while (b > 0) {\n T_VAL t = a / b;\n a -= t * b, swap(a, b);\n u -= t * v, swap(u, v);\n }\n u %= m;\n if (u < 0) u += m;\n return u;\n}\n\n// modint\ntemplate<int MOD = 998244353, bool PRIME = true> struct Fp {\n // inner value\n unsigned int val;\n \n // constructor\n constexpr Fp() : val(0) { }\n template<std::signed_integral T> constexpr Fp(T v) {\n long long tmp = (long long)(v % (long long)(get_umod()));\n if (tmp < 0) tmp += get_umod();\n val = (unsigned int)(tmp);\n }\n template<std::unsigned_integral T> constexpr Fp(T v) {\n val = (unsigned int)(v % get_umod());\n }\n constexpr long long get() const { return val; }\n constexpr static int get_mod() { return MOD; }\n constexpr static unsigned int get_umod() { return MOD; }\n \n // arithmetic operators\n constexpr Fp operator + () const { return Fp(*this); }\n constexpr Fp operator - () const { return Fp() - Fp(*this); }\n constexpr Fp operator + (const Fp &r) const { return Fp(*this) += r; }\n constexpr Fp operator - (const Fp &r) const { return Fp(*this) -= r; }\n constexpr Fp operator * (const Fp &r) const { return Fp(*this) *= r; }\n constexpr Fp operator / (const Fp &r) const { return Fp(*this) /= r; }\n constexpr Fp& operator += (const Fp &r) {\n val += r.val;\n if (val >= get_umod()) val -= get_umod();\n return *this;\n }\n constexpr Fp& operator -= (const Fp &r) {\n val -= r.val;\n if (val >= get_umod()) val += get_umod();\n return *this;\n }\n constexpr Fp& operator *= (const Fp &r) {\n unsigned long long tmp = val;\n tmp *= r.val;\n val = (unsigned int)(tmp % get_umod());\n return *this;\n }\n constexpr Fp& operator /= (const Fp &r) {\n return *this = *this * r.inv(); \n }\n constexpr Fp pow(long long n) const {\n assert(n >= 0);\n Fp res(1), mul(*this);\n while (n) {\n if (n & 1) res *= mul;\n mul *= mul;\n n >>= 1;\n }\n return res;\n }\n constexpr Fp inv() const {\n if (PRIME) {\n assert(val);\n return pow(get_umod() - 2);\n } else {\n assert(val);\n return mod_inv((long long)(val), get_umod());\n }\n }\n\n // other operators\n constexpr bool operator == (const Fp &r) const {\n return this->val == r.val;\n }\n constexpr bool operator != (const Fp &r) const {\n return this->val != r.val;\n }\n constexpr bool operator < (const Fp &r) const {\n return this->val < r.val;\n }\n constexpr bool operator > (const Fp &r) const {\n return this->val > r.val;\n }\n constexpr bool operator <= (const Fp &r) const {\n return this->val <= r.val;\n }\n constexpr bool operator >= (const Fp &r) const {\n return this->val >= r.val;\n }\n constexpr Fp& operator ++ () {\n ++val;\n if (val == get_umod()) val = 0;\n return *this;\n }\n constexpr Fp& operator -- () {\n if (val == 0) val = get_umod();\n --val;\n return *this;\n }\n constexpr Fp operator ++ (int) {\n Fp res = *this;\n ++*this;\n return res;\n }\n constexpr Fp operator -- (int) {\n Fp res = *this;\n --*this;\n return res;\n }\n friend constexpr istream& operator >> (istream &is, Fp<MOD> &x) {\n long long tmp = 1;\n is >> tmp;\n tmp = tmp % (long long)(get_umod());\n if (tmp < 0) tmp += get_umod();\n x.val = (unsigned int)(tmp);\n return is;\n }\n friend constexpr ostream& operator << (ostream &os, const Fp<MOD> &x) {\n return os << x.val;\n }\n friend constexpr Fp<MOD> pow(const Fp<MOD> &r, long long n) {\n return r.pow(n);\n }\n friend constexpr Fp<MOD> inv(const Fp<MOD> &r) {\n return r.inv();\n }\n};\n\n// dynamic modint\nstruct DynamicModint {\n using mint = DynamicModint;\n \n // static menber\n static int MOD;\n \n // inner value\n unsigned int val;\n \n // constructor\n DynamicModint() : val(0) { }\n template<std::signed_integral T> DynamicModint(T v) {\n long long tmp = (long long)(v % (long long)(get_umod()));\n if (tmp < 0) tmp += get_umod();\n val = (unsigned int)(tmp);\n }\n template<std::unsigned_integral T> DynamicModint(T v) {\n val = (unsigned int)(v % get_umod());\n }\n long long get() const { return val; }\n static int get_mod() { return MOD; }\n static unsigned int get_umod() { return MOD; }\n static void set_mod(int mod) { MOD = mod; }\n \n // arithmetic operators\n mint operator + () const { return mint(*this); }\n mint operator - () const { return mint() - mint(*this); }\n mint operator + (const mint &r) const { return mint(*this) += r; }\n mint operator - (const mint &r) const { return mint(*this) -= r; }\n mint operator * (const mint &r) const { return mint(*this) *= r; }\n mint operator / (const mint &r) const { return mint(*this) /= r; }\n mint& operator += (const mint &r) {\n val += r.val;\n if (val >= get_umod()) val -= get_umod();\n return *this;\n }\n mint& operator -= (const mint &r) {\n val -= r.val;\n if (val >= get_umod()) val += get_umod();\n return *this;\n }\n mint& operator *= (const mint &r) {\n unsigned long long tmp = val;\n tmp *= r.val;\n val = (unsigned int)(tmp % get_umod());\n return *this;\n }\n mint& operator /= (const mint &r) {\n return *this = *this * r.inv(); \n }\n mint pow(long long n) const {\n assert(n >= 0);\n mint res(1), mul(*this);\n while (n) {\n if (n & 1) res *= mul;\n mul *= mul;\n n >>= 1;\n }\n return res;\n }\n mint inv() const {\n assert(val);\n return mod_inv((long long)(val), get_umod());\n }\n\n // other operators\n bool operator == (const mint &r) const {\n return this->val == r.val;\n }\n bool operator != (const mint &r) const {\n return this->val != r.val;\n }\n bool operator < (const mint &r) const {\n return this->val < r.val;\n }\n bool operator > (const mint &r) const {\n return this->val > r.val;\n }\n bool operator <= (const mint &r) const {\n return this->val <= r.val;\n }\n bool operator >= (const mint &r) const {\n return this->val >= r.val;\n }\n mint& operator ++ () {\n ++val;\n if (val == get_umod()) val = 0;\n return *this;\n }\n mint& operator -- () {\n if (val == 0) val = get_umod();\n --val;\n return *this;\n }\n mint operator ++ (int) {\n mint res = *this;\n ++*this;\n return res;\n }\n mint operator -- (int) {\n mint res = *this;\n --*this;\n return res;\n }\n friend istream& operator >> (istream &is, mint &x) {\n long long tmp = 1;\n is >> tmp;\n tmp = tmp % (long long)(get_umod());\n if (tmp < 0) tmp += get_umod();\n x.val = (unsigned int)(tmp);\n return is;\n }\n friend ostream& operator << (ostream &os, const mint &x) {\n return os << x.val;\n }\n friend mint pow(const mint &r, long long n) {\n return r.pow(n);\n }\n friend mint inv(const mint &r) {\n return r.inv();\n }\n};\nint DynamicModint::MOD;\n\n// Binomial coefficient\ntemplate<class mint> struct BiCoef {\n vector<mint> fact_, inv_, finv_;\n constexpr BiCoef() {}\n constexpr BiCoef(int n) : fact_(n, 1), inv_(n, 1), finv_(n, 1) {\n init(n);\n }\n constexpr void init(int n) {\n fact_.assign(n, 1), inv_.assign(n, 1), finv_.assign(n, 1);\n int MOD = fact_[0].get_mod();\n for(int i = 2; i < n; i++){\n fact_[i] = fact_[i-1] * i;\n inv_[i] = -inv_[MOD%i] * (MOD/i);\n finv_[i] = finv_[i-1] * inv_[i];\n }\n }\n constexpr mint com(int n, int k) const {\n if (n < k || n < 0 || k < 0) return 0;\n return fact_[n] * finv_[k] * finv_[n-k];\n }\n constexpr mint fact(int n) const {\n if (n < 0) return 0;\n return fact_[n];\n }\n constexpr mint inv(int n) const {\n if (n < 0) return 0;\n return inv_[n];\n }\n constexpr mint finv(int n) const {\n if (n < 0) return 0;\n return finv_[n];\n }\n};\n\n// calc primitive root\nconstexpr int calc_primitive_root(long long m) {\n if (m == 1) return -1;\n if (m == 2) return 1;\n if (m == 998244353) return 3;\n if (m == 167772161) return 3;\n if (m == 469762049) return 3;\n if (m == 754974721) return 11;\n if (m == 645922817) return 3;\n if (m == 897581057) return 3;\n return -1;\n}\n\n// NTT setup\ntemplate<class mint, int MOD = mint::get_mod(), int g = calc_primitive_root(mint::get_mod())>\nstruct ntt_setup {\n static constexpr int bsf_constexpr(unsigned int x) {\n int i = 0;\n while (!(x & (1 << i))) i++;\n return i;\n };\n\n static constexpr int rank = bsf_constexpr(MOD - 1);\n array<mint, rank + 1> root, iroot; // root[i]^(2^i) = 1, root[i] * iroot[i] = 1\n array<mint, max(0, rank - 1)> rate2, irate2;\n array<mint, max(0, rank - 2)> rate3, irate3;\n\n ntt_setup() {\n root[rank] = mint(g).pow((MOD - 1) >> rank);\n iroot[rank] = root[rank].inv();\n for (int i = rank - 1; i >= 0; i--) {\n root[i] = root[i + 1] * root[i + 1];\n iroot[i] = iroot[i + 1] * iroot[i + 1];\n }\n mint prod = 1, iprod = 1;\n for (int i = 0; i < rank - 1; i++) {\n rate2[i] = root[i + 2] * prod;\n irate2[i] = iroot[i + 2] * iprod;\n prod *= iroot[i + 2];\n iprod *= root[i + 2];\n }\n prod = 1, iprod = 1;\n for (int i = 0; i < rank - 2; i++) {\n rate3[i] = root[i + 3] * prod;\n irate3[i] = iroot[i + 3] * iprod;\n prod *= iroot[i + 3];\n iprod *= root[i + 3];\n }\n }\n};\n\n// NTT transformation\ntemplate<class mint, int MOD = mint::get_mod()> \nvoid ntt_trans(vector<mint> &v) {\n int n = (int)v.size();\n int h = ceil_pow2(n);\n static const ntt_setup<mint> setup;\n\n int len = 0;\n while (len < h) {\n if (h - len == 1) {\n int p = 1 << (h - len - 1);\n mint rot = 1;\n for (int s = 0; s < (1 << len); s++) {\n int offset = s << (h - len);\n for (int i = 0; i < p; i++) {\n auto l = v[i + offset];\n auto r = v[i + offset + p] * rot;\n v[i + offset] = l + r;\n v[i + offset + p] = l - r;\n }\n if (s + 1 != (1 << len)) {\n rot *= setup.rate2[bsf(~(unsigned int)(s))];\n }\n }\n len++;\n } else {\n int p = 1 << (h - len - 2);\n mint rot = 1, imag = setup.root[2];\n for (int s = 0; s < (1 << len); s++) {\n mint rot2 = rot * rot, rot3 = rot2 * rot;\n int offset = s << (h - len);\n for (int i = 0; i < p; i++) {\n auto mod2 = 1ULL * MOD * MOD;\n auto a0 = 1ULL * v[i + offset].val;\n auto a1 = 1ULL * v[i + offset + p].val * rot.val;\n auto a2 = 1ULL * v[i + offset + p * 2].val * rot2.val;\n auto a3 = 1ULL * v[i + offset + p * 3].val * rot3.val;\n auto tmp = 1ULL * mint(a1 + mod2 - a3).val * imag.val;\n auto na2 = mod2 - a2;\n v[i + offset] = a0 + a2 + a1 + a3;\n v[i + offset + p] = a0 + a2 + (mod2 * 2 - (a1 + a3));\n v[i + offset + p * 2] = a0 + na2 + tmp;\n v[i + offset + p * 3] = a0 + na2 + (mod2 - tmp);\n }\n if (s + 1 != (1 << len)) {\n rot *= setup.rate3[bsf(~(unsigned int)(s))];\n }\n }\n len += 2;\n }\n }\n}\n\n// NTT inv-transformation\ntemplate<class mint, int MOD = mint::get_mod()> \nvoid ntt_trans_inv(vector<mint> &v) {\n int n = (int)v.size();\n int h = ceil_pow2(n);\n static const ntt_setup<mint> setup;\n\n int len = h;\n while (len) {\n if (len == 1) {\n int p = 1 << (h - len);\n mint irot = 1;\n for (int s = 0; s < (1 << (len - 1)); s++) {\n int offset = s << (h - len + 1);\n for (int i = 0; i < p; i++) {\n auto l = v[i + offset];\n auto r = v[i + offset + p];\n v[i + offset] = l + r;\n v[i + offset + p] = (unsigned long long)((long long)(MOD) + l.val - r.val) * irot.val;\n }\n if (s + 1 != (1 << (len - 1))) {\n irot *= setup.irate2[bsf(~(unsigned int)(s))];\n }\n }\n len--;\n } else {\n int p = 1 << (h - len);\n mint irot = 1, iimag = setup.iroot[2];\n for (int s = 0; s < (1 << (len - 2)); s++) {\n mint irot2 = irot * irot, irot3 = irot2 * irot;\n int offset = s << (h - len + 2);\n for (int i = 0; i < p; i++) {\n auto a0 = 1ULL * v[i + offset].val;\n auto a1 = 1ULL * v[i + offset + p].val;\n auto a2 = 1ULL * v[i + offset + p * 2].val;\n auto a3 = 1ULL * v[i + offset + p * 3].val;\n auto tmp = 1ULL * mint((MOD + a2 - a3) * iimag.val).val;\n v[i + offset] = a0 + a1 + a2 + a3;\n v[i + offset + p] = (a0 + (MOD - a1) + tmp) * irot.val;\n v[i + offset + p * 2] = (a0 + a1 + (MOD - a2) + (MOD - a3)) * irot2.val;\n v[i + offset + p * 3] = (a0 + (MOD - a1) + (MOD - tmp)) * irot3.val;\n }\n if (s + 1 != (1 << (len - 2))) {\n irot *= setup.irate3[bsf(~(unsigned int)(s))];\n }\n }\n len -= 2;\n }\n }\n mint in = mint(n).inv();\n for (int i = 0; i < n; i++) v[i] *= in;\n}\n\n// naive convolution\ntemplate<class T>\nvector<T> sub_convolution_naive(const vector<T> &a, const vector<T> &b) {\n int n = (int)a.size(), m = (int)b.size();\n vector<T> res(n + m - 1);\n if (n < m) {\n for (int j = 0; j < m; j++) for (int i = 0; i < n; i++) res[i + j] += a[i] * b[j];\n } else {\n for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) res[i + j] += a[i] * b[j];\n }\n return res;\n}\n\n// ntt convolution\ntemplate<class mint>\nvector<mint> sub_convolution_ntt(vector<mint> a, vector<mint> b) {\n int MOD = mint::get_mod();\n int n = (int)a.size(), m = (int)b.size();\n if (!n || !m) return {};\n int z = (int)bit_ceil((unsigned int)(n + m - 1));\n assert((MOD - 1) % z == 0);\n a.resize(z), b.resize(z);\n ntt_trans(a), ntt_trans(b);\n for (int i = 0; i < z; i++) a[i] *= b[i];\n ntt_trans_inv(a);\n a.resize(n + m - 1);\n return a;\n}\n\n// convolution in general mod\ntemplate<class mint>\nvector<mint> convolution(const vector<mint> &a, const vector<mint> &b) {\n int n = (int)a.size(), m = (int)b.size();\n if (!n || !m) return {};\n if (min(n, m) <= 60) return sub_convolution_naive(std::move(a), std::move(b));\n if constexpr (std::is_same_v<mint, Fp<998244353>>) return sub_convolution_ntt(a, b);\n\n static constexpr int MOD0 = 754974721; // 2^24\n static constexpr int MOD1 = 167772161; // 2^25\n static constexpr int MOD2 = 469762049; // 2^26\n using mint0 = Fp<MOD0>;\n using mint1 = Fp<MOD1>;\n using mint2 = Fp<MOD2>;\n static const mint1 imod0 = 95869806; // modinv(MOD0, MOD1);\n static const mint2 imod1 = 104391568; // modinv(MOD1, MOD2);\n static const mint2 imod01 = 187290749; // imod1 / MOD0;\n\n vector<mint0> a0(n, 0), b0(m, 0);\n vector<mint1> a1(n, 0), b1(m, 0);\n vector<mint2> a2(n, 0), b2(m, 0);\n for (int i = 0; i < n; ++i) a0[i] = a[i].val, a1[i] = a[i].val, a2[i] = a[i].val;\n for (int i = 0; i < m; ++i) b0[i] = b[i].val, b1[i] = b[i].val, b2[i] = b[i].val;\n auto c0 = sub_convolution_ntt(std::move(a0), std::move(b0));\n auto c1 = sub_convolution_ntt(std::move(a1), std::move(b1));\n auto c2 = sub_convolution_ntt(std::move(a2), std::move(b2));\n\n vector<mint> res(n + m - 1);\n mint mod0 = MOD0, mod01 = mod0 * MOD1;\n for (int i = 0; i < n + m - 1; ++i) {\n unsigned int y0 = c0[i].val;\n unsigned int y1 = (imod0 * (c1[i] - y0)).val;\n unsigned int y2 = (imod01 * (c2[i] - y0) - imod1 * y1).val;\n res[i] = mod01 * y2 + mod0 * y1 + y0;\n }\n return res;\n}\n\n\n//------------------------------//\n// FPS\n//------------------------------//\n\n// Formal Power Series\ntemplate<class mint> struct FPS : vector<mint> {\n static const int SPARSE_BOARDER = 50;\n using vector<mint>::vector;\n \n // constructor\n constexpr FPS(const vector<mint> &r) : vector<mint>(r) {}\n \n // core operator\n constexpr FPS pre(int siz) const {\n return FPS(begin(*this), begin(*this) + min((int)this->size(), siz));\n }\n constexpr FPS rev() const {\n FPS res = *this;\n reverse(begin(res), end(res));\n return res;\n }\n constexpr FPS& normalize() {\n while (!this->empty() && this->back() == 0) this->pop_back();\n return *this;\n }\n constexpr mint eval(const mint &v) {\n mint res = 0;\n for (int i = (int)this->size()-1; i >= 0; --i) {\n res *= v;\n res += (*this)[i];\n }\n return res;\n }\n constexpr int count_terms() const {\n int res = 0;\n for (int i = 0; i < (int)this->size(); i++) if ((*this)[i] != mint(0)) res++;\n return res;\n }\n \n // basic operator\n constexpr FPS operator - () const noexcept {\n FPS res = (*this);\n for (int i = 0; i < (int)res.size(); ++i) res[i] = -res[i];\n return res;\n }\n constexpr FPS operator + (const mint &v) const { return FPS(*this) += v; }\n constexpr FPS operator + (const FPS &r) const { return FPS(*this) += r; }\n constexpr FPS operator - (const mint &v) const { return FPS(*this) -= v; }\n constexpr FPS operator - (const FPS &r) const { return FPS(*this) -= r; }\n constexpr FPS operator * (const mint &v) const { return FPS(*this) *= v; }\n constexpr FPS operator * (const FPS &r) const { return FPS(*this) *= r; }\n constexpr FPS operator / (const mint &v) const { return FPS(*this) /= v; }\n constexpr FPS operator / (const FPS &r) const { return FPS(*this) /= r; }\n constexpr FPS operator % (const FPS &r) const { return FPS(*this) %= r; }\n constexpr FPS operator << (int x) const { return FPS(*this) <<= x; }\n constexpr FPS operator >> (int x) const { return FPS(*this) >>= x; }\n constexpr FPS& operator += (const mint &v) {\n if (this->empty()) this->reserve(1), this->resize(1);\n (*this)[0] += v;\n return *this;\n }\n constexpr FPS& operator += (const FPS &r) {\n if (r.size() > this->size()) this->reserve(r.size()), this->resize(r.size());\n for (int i = 0; i < (int)r.size(); ++i) (*this)[i] += r[i];\n return this->normalize();\n }\n constexpr FPS& operator -= (const mint &v) {\n if (this->empty()) this->reserve(1), this->resize(1);\n (*this)[0] -= v;\n return *this;\n }\n constexpr FPS& operator -= (const FPS &r) {\n if (r.size() > this->size()) this->reserve(r.size()), this->resize(r.size());\n for (int i = 0; i < (int)r.size(); ++i) (*this)[i] -= r[i];\n return this->normalize();\n }\n constexpr FPS& operator *= (const mint &v) {\n for (int i = 0; i < (int)this->size(); ++i) (*this)[i] *= v;\n return *this;\n }\n constexpr FPS& operator *= (const FPS &r) {\n return *this = convolution((*this), r);\n }\n constexpr FPS& operator /= (const mint &v) {\n assert(v != 0);\n mint iv = v.inv();\n for (int i = 0; i < (int)this->size(); ++i) (*this)[i] *= iv;\n return *this;\n }\n \n // division, r must be normalized (r.back() must not be 0)\n constexpr FPS& operator /= (const FPS &r) {\n assert(!r.empty());\n assert(r.back() != 0);\n this->normalize();\n if (this->size() < r.size()) {\n this->clear();\n return *this;\n }\n int need = (int)this->size() - (int)r.size() + 1;\n *this = (rev().pre(need) * r.rev().inv(need)).pre(need).rev();\n return *this;\n }\n constexpr FPS& operator %= (const FPS &r) {\n assert(!r.empty());\n assert(r.back() != 0);\n this->normalize();\n FPS q = (*this) / r;\n return *this -= q * r;\n }\n constexpr FPS& operator <<= (int x) {\n FPS res(x, 0);\n res.insert(res.end(), begin(*this), end(*this));\n return *this = res;\n }\n constexpr FPS& operator >>= (int x) {\n FPS res;\n res.insert(res.end(), begin(*this) + x, end(*this));\n return *this = res;\n }\n\n // advanced operation\n // df/dx\n constexpr FPS diff() const {\n int n = (int)this->size();\n if (n <= 0) return FPS();\n FPS res(n-1);\n for (int i = 1; i < n; ++i) res[i-1] = (*this)[i] * i;\n return res;\n }\n \n // \\int f dx\n constexpr FPS integral() const {\n int n = (int)this->size();\n FPS res(n+1, 0);\n for (int i = 0; i < n; ++i) res[i+1] = (*this)[i] / (i+1);\n return res;\n }\n \n // inv(f), f[0] must not be 0\n constexpr FPS inv(int deg = -1) const {\n if (count_terms() <= SPARSE_BOARDER) return inv_sparse(deg);\n if constexpr (std::is_same_v<mint, Fp<998244353>>) return inv_ntt_friendly(deg);\n assert(this->size() >= 1 && (*this)[0] != 0);\n if (deg < 0) deg = (int)this->size();\n FPS res({mint(1) / (*this)[0]});\n for (int d = 1; d < deg; d <<= 1) {\n res = (res + res - res * res * pre(d << 1)).pre(d << 1);\n }\n res.resize(deg);\n return res;\n }\n constexpr FPS inv_ntt_friendly(int deg = -1) const {\n assert(this->size() >= 1 && (*this)[0] != 0);\n if (deg < 0) deg = (int)this->size();\n FPS res(deg);\n res[0] = mint(1) / (*this)[0];\n for (int d = 1; d < deg; d <<= 1) {\n FPS g(d * 2), h(d * 2);\n mint iv = mint(d * 2).inv();\n for (int i = 0; i < min((int)this->size(), d * 2); i++) g[i] = (*this)[i];\n for (int i = 0; i < d; i++) h[i] = res[i];\n ntt_trans(g), ntt_trans(h);\n for (int i = 0; i < d * 2; i++) g[i] *= h[i];\n ntt_trans_inv(g);\n for (int i = 0; i < d; i++) g[i] = 0;\n ntt_trans(g);\n for (int i = 0; i < d * 2; i++) g[i] *= h[i];\n ntt_trans_inv(g);\n for (int i = d; i < min(deg, d * 2); i++) res[i] = -g[i];\n }\n return res.pre(deg);\n }\n constexpr FPS inv_sparse(int deg = -1) const {\n assert(this->size() >= 1 && (*this)[0] != 0);\n if (deg < 0) deg = (int)this->size();\n vector<pair<int, mint>> dat;\n for (int i = 1; i < (int)this->size(); i++) if ((*this)[i] != mint(0)) {\n dat.emplace_back(i, (*this)[i]);\n }\n vector<mint> res(deg);\n res[0] = (*this)[0].inv();\n for (int i = 1; i < deg; i++) {\n mint r = 0;\n for (auto &&[k, val] : dat) {\n if (k > i) break;\n r -= val * res[i - k];\n }\n res[i] = r * res[0];\n }\n return res;\n }\n \n // friend operators\n friend constexpr FPS inv(const FPS &f, int deg = -1) { return f.inv(deg); }\n};\n\n// find f(x)^n mod g(x)\ntemplate<class mint, class T_VAL = long long> \nFPS<mint> mod_pow(const FPS<mint> &f, T_VAL e, const FPS<mint> &mod) {\n assert(!mod.empty());\n auto iv = mod.rev().inv();\n auto calc_quo = [&](const FPS<mint> &pol) -> FPS<mint> {\n if (pol.size() < mod.size()) return FPS<mint>();\n int deg = (int)pol.size() - (int)mod.size() + 1;\n return (pol.rev().pre(deg) * iv.pre(deg)).pre(deg).rev();\n };\n FPS<mint> res{1}, b(f);\n while (e) {\n if (e & 1) res *= b, res -= calc_quo(res) * mod;\n b *= b;\n b -= calc_quo(b) * mod;\n e >>= 1;\n assert(b.size() + 1 <= mod.size());\n assert(res.size() + 1 <= mod.size());\n }\n return res;\n}\n\n// polynomial gcd\ntemplate<class mint>\nstruct MatrixFPS22 {\n FPS<mint> a00, a01, a10, a11;\n MatrixFPS22() {}\n MatrixFPS22(const FPS<mint> &a00, const FPS<mint> &a01, const FPS<mint> &a10, const FPS<mint> &a11)\n : a00(a00), a01(a01), a10(a10), a11(a11) {}\n\n MatrixFPS22 &operator *= (const MatrixFPS22 &r) {\n FPS<mint> A00 = a00 * r.a00 + a01 * r.a10;\n FPS<mint> A01 = a00 * r.a01 + a01 * r.a11;\n FPS<mint> A10 = a10 * r.a00 + a11 * r.a10;\n FPS<mint> A11 = a10 * r.a01 + a11 * r.a11;\n swap(A00, a00), swap(A01, a01), swap(A10, a10), swap(A11, a11);\n return *this;\n }\n static MatrixFPS22 get_identity() { \n return MatrixFPS22(FPS<mint>{mint(1)}, FPS<mint>(), FPS<mint>(), FPS<mint>{mint(1)});\n }\n MatrixFPS22 operator * (const MatrixFPS22 &r) const { return MatrixFPS22(*this) *= r; }\n};\n\ntemplate<class mint> pair<FPS<mint>, FPS<mint>> operator * (\nconst MatrixFPS22<mint> &m, const pair<FPS<mint>, FPS<mint>> &a) {\n FPS<mint> b0 = m.a00 * a.first + m.a01 * a.second;\n FPS<mint> b1 = m.a10 * a.first + m.a11 * a.second;\n return {b0, b1};\n}\n\ntemplate<class mint> MatrixFPS22<mint> mat_poly_half_gcd(pair<FPS<mint>, FPS<mint>> p) {\n auto naive_gcd = [](MatrixFPS22<mint> &m, pair<FPS<mint>, FPS<mint>> &p) -> void {\n FPS<mint> q = p.first / p.second, r = p.first - p.second * q;\n FPS<mint> b10 = m.a00 - m.a10 * q, b11 = m.a01 - m.a11 * q;\n swap(b10, m.a10), swap(b11, m.a11), swap(b10, m.a00), swap(b11, m.a01);\n p = {p.second, r};\n };\n int N = p.first.size(), M = p.second.size(), K = (N + 1) / 2;\n if (M <= K) return MatrixFPS22<mint>::get_identity();\n MatrixFPS22<mint> m1 = mat_poly_half_gcd(make_pair(p.first >> K, p.second >> K));\n p = m1 * p;\n if ((int)p.second.size() <= K) return m1;\n naive_gcd(m1, p);\n if ((int)p.second.size() <= K) return m1;\n int j = K * 2 - (int)p.first.size() + 1;\n p.first >>= j, p.second >>= j;\n return mat_poly_half_gcd(p) * m1;\n}\n\ntemplate<class mint> MatrixFPS22<mint> mat_poly_gcd(const FPS<mint> &a, const FPS<mint> &b) {\n auto naive_gcd = [](MatrixFPS22<mint> &m, pair<FPS<mint>, FPS<mint>> &p) -> void {\n FPS<mint> q = p.first / p.second, r = p.first - p.second * q;\n FPS<mint> b10 = m.a00 - m.a10 * q, b11 = m.a01 - m.a11 * q;\n swap(b10, m.a10), swap(b11, m.a11), swap(b10, m.a00), swap(b11, m.a01);\n p = {p.second, r};\n };\n pair<FPS<mint>, FPS<mint>> p{a, b};\n p.first.normalize(), p.second.normalize();\n int N = (int)p.first.size(), M = (int)p.second.size();\n if (N < M) {\n MatrixFPS22<mint> mat = mat_poly_gcd(p.second, p.first);\n swap(mat.a00, mat.a01);\n swap(mat.a10, mat.a11);\n return mat;\n }\n MatrixFPS22<mint> res = MatrixFPS22<mint>::get_identity();\n while (true) {\n MatrixFPS22<mint> m1 = mat_poly_half_gcd(p);\n p = m1 * p;\n if (p.second.empty()) return m1 * res;\n naive_gcd(m1, p);\n if (p.second.empty()) return m1 * res;\n res = m1 * res;\n }\n}\n\ntemplate<class mint> FPS<mint> poly_gcd(const FPS<mint> &a, const FPS<mint> &b) {\n pair<FPS<mint>, FPS<mint>> p(a, b);\n MatrixFPS22<mint> m = mat_poly_gcd(a, b);\n p = m * p;\n if (!p.first.empty()) {\n mint coef = p.first.back().inv();\n for (auto &x : p.first) x *= coef;\n }\n return p.first;\n}\n\nvoid AOJ_2213() {\n using mint = DynamicModint;\n long long N, P;\n while (cin >> N >> P, N) {\n mint::set_mod(P);\n FPS<mint> a(N+1);\n for (int i = 0; i <= N; i++) {\n long long val;\n cin >> val;\n a[i] = val;\n }\n a.normalize();\n if (a == FPS<mint>()) {\n cout << P << '\\n';\n continue;\n }\n\n FPS<mint> x({0, 1});\n auto xp = mod_pow(x, P, a);\n auto f = xp - x;\n auto g = poly_gcd(f, a);\n cout << (int)g.size() - 1 << '\\n';\n }\n}\n\n\nint main() {\n AOJ_2213();\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3712, "score_of_the_acc": -1, "final_rank": 5 }, { "submission_id": "aoj_2213_10215529", "code_snippet": "// AOJ #2213\n// The Number of Solutions for a Polynomial 2025.2.13\n\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n \nll modP;\n \ninline ll modAdd(ll a, ll b) {\n a %= modP; b %= modP;\n ll s = a + b;\n s %= modP;\n return s;\n}\n \ninline ll modSub(ll a, ll b) {\n a %= modP; b %= modP;\n ll s = a - b;\n s %= modP;\n if(s < 0) s += modP;\n return s;\n}\n \ninline ll modMul(ll a, ll b) {\n a %= modP; b %= modP;\n __int128 t = a;\n t *= b;\n t %= modP;\n return (ll)t;\n}\n \nll modPow(ll a, ll b) {\n ll res = 1;\n a %= modP;\n while(b > 0) {\n if(b & 1) res = modMul(res, a);\n a = modMul(a, a);\n b >>= 1;\n }\n return res;\n}\n \nll modInv(ll a) {\n return modPow(a, modP - 2);\n}\n \ntypedef vector<ll> Poly;\n \nvoid polyTrim(Poly &a) {\n while(a.size() > 1 && a.back() % modP == 0) a.pop_back();\n if(a.empty()) a.push_back(0);\n}\n \nPoly polyAdd(const Poly &A, const Poly &B) {\n size_t n = max(A.size(), B.size());\n Poly C(n,0);\n for (int i = 0; i < n; i++){\n ll ca = (i < A.size() ? A[i] : 0);\n ll cb = (i < B.size() ? B[i] : 0);\n C[i] = modAdd(ca, cb);\n }\n polyTrim(C);\n return C;\n}\n \nPoly polySub(const Poly &A, const Poly &B) {\n size_t n = max(A.size(), B.size());\n Poly C(n,0);\n for (int i = 0; i < n; i++){\n ll ca = (i < A.size() ? A[i] : 0);\n ll cb = (i < B.size() ? B[i] : 0);\n C[i] = modSub(ca, cb);\n }\n polyTrim(C);\n return C;\n}\n \nPoly polyMulSimple(const Poly &A, const Poly &B) {\n Poly C(A.size() + B.size() - 1, 0);\n for (int i = 0; i < A.size(); i++){\n for (size_t j = 0; j < B.size(); j++){\n C[i+j] = modAdd(C[i+j], modMul(A[i], B[j]));\n }\n }\n polyTrim(C);\n return C;\n}\n \npair<Poly, Poly> polyDivMod(Poly A, const Poly &B) {\n polyTrim(A);\n Poly b = B; \n polyTrim(b);\n Poly Q(max((int)A.size() - (int)b.size() + 1, 0), 0);\n ll invLead = modInv(b.back());\n while(A.size() >= b.size() && !(A.size() == 1 && A[0] == 0)) {\n polyTrim(A);\n int d = A.size() - b.size();\n ll factor = modMul(A.back(), invLead);\n if(Q.size() < d+1) Q.resize(d+1, 0);\n Q[d] = factor;\n for (size_t i = 0; i < b.size(); i++){\n int idx = i + d;\n A[idx] = modSub(A[idx], modMul(factor, b[i]));\n }\n polyTrim(A);\n }\n polyTrim(Q);\n polyTrim(A);\n return {Q, A};\n}\n \nPoly polyMod(const Poly &A, const Poly &B) {\n auto pr = polyDivMod(A, B);\n return pr.second;\n}\n \nPoly polyGCD(Poly A, Poly B) {\n polyTrim(A);\n polyTrim(B);\n while(!(B.size() == 1 && B[0] == 0)) {\n Poly r = polyMod(A, B);\n A = B;\n B = r;\n }\n if(!(A.size() == 1 && A[0] == 0)){\n ll invLead = modInv(A.back());\n for(auto &coef : A)\n coef = modMul(coef, invLead);\n }\n polyTrim(A);\n return A;\n}\n \nPoly polyMulMod(const Poly &A, const Poly &B, const Poly &modPoly) {\n Poly prod = polyMulSimple(A, B);\n Poly rem = polyMod(prod, modPoly);\n return rem;\n}\n \nPoly polyPow(Poly base, ll exp, const Poly &modPoly) {\n Poly result = {1};\n while(exp > 0){\n if(exp & 1) result = polyMulMod(result, base, modPoly);\n base = polyMulMod(base, base, modPoly);\n exp >>= 1;\n }\n return result;\n}\n \n \nint main(){\n ios::sync_with_stdio(0);\n cin.tie(0);\n \n while(true){\n int N;\n cin >> N >> modP;\n if(modP == 0) break;\n \n Poly poly;\n poly.resize(N+1, 0);\n for (int i = 0; i <= N; i++){\n ll coeff;\n cin >> coeff;\n coeff %= modP;\n if(coeff < 0) coeff += modP;\n poly[i] = coeff;\n }\n \n polyTrim(poly);\n \n if(poly.size() == 1 && poly[0] == 0){\n cout << modP << endl;\n continue;\n }\n \n if(poly.size() == 1){\n cout << 0 << endl;\n continue;\n }\n \n {\n ll invLead = modInv(poly.back());\n for (auto &coef : poly)\n coef = modMul(coef, invLead);\n polyTrim(poly);\n }\n \n Poly modPoly = poly;\n Poly Xpoly = {0, 1};\n Poly XtoP = polyPow(Xpoly, modP, modPoly);\n Poly diffPoly = polySub(XtoP, Xpoly);\n Poly g = polyGCD(modPoly, diffPoly);\n int numRoots = (g.size() == 1 && g[0] == 0) ? 0 : (int)g.size() - 1;\n cout << numRoots << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 110, "memory_kb": 3440, "score_of_the_acc": -1.3381, "final_rank": 13 }, { "submission_id": "aoj_2213_8290142", "code_snippet": "//\n// 多項式アルゴリズム (by NTT, FPS)\n//  ・係数は p を素数として Fp 体を想定\n//\n// verified:\n// Yosupo Library Checker - Convolution (Mod 1,000,000,007)\n// https://judge.yosupo.jp/problem/convolution_mod_1000000007\n//\n// Yosupo Library Checker - Division of Polynomials\n// https://judge.yosupo.jp/problem/division_of_polynomials\n//\n// Yosupo Library Checker - Polynomial Taylor Shift\n// https://judge.yosupo.jp/problem/polynomial_taylor_shift\n//\n// AGC 005 F - Many Easy Problems (Polynomial Taylor Shift % mod 924844033)\n// https://atcoder.jp/contests/agc005/tasks/agc005_f\n//\n// AOJ 2213 The Number of Solutions for a Polynomial (多項式の割り算 mod Any, 多項式 GCD)\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2213\n//\n\n\n#include <bits/stdc++.h>\nusing namespace std;\n\n\n// modint\ntemplate<int MOD> struct Fp {\n // inner value\n long long val;\n \n // constructor\n constexpr Fp() : val(0) { }\n constexpr Fp(long long v) : val(v % MOD) {\n if (val < 0) val += MOD;\n }\n constexpr long long get() const { return val; }\n constexpr int get_mod() const { return MOD; }\n \n // arithmetic operators\n constexpr Fp operator + () const { return Fp(*this); }\n constexpr Fp operator - () const { return Fp(0) - Fp(*this); }\n constexpr Fp operator + (const Fp &r) const { return Fp(*this) += r; }\n constexpr Fp operator - (const Fp &r) const { return Fp(*this) -= r; }\n constexpr Fp operator * (const Fp &r) const { return Fp(*this) *= r; }\n constexpr Fp operator / (const Fp &r) const { return Fp(*this) /= r; }\n constexpr Fp& operator += (const Fp &r) {\n val += r.val;\n if (val >= MOD) val -= MOD;\n return *this;\n }\n constexpr Fp& operator -= (const Fp &r) {\n val -= r.val;\n if (val < 0) val += MOD;\n return *this;\n }\n constexpr Fp& operator *= (const Fp &r) {\n val = val * r.val % MOD;\n return *this;\n }\n constexpr Fp& operator /= (const Fp &r) {\n long long a = r.val, b = MOD, u = 1, v = 0;\n while (b) {\n long long t = a / b;\n a -= t * b, swap(a, b);\n u -= t * v, swap(u, v);\n }\n val = val * u % MOD;\n if (val < 0) val += MOD;\n return *this;\n }\n constexpr Fp pow(long long n) const {\n Fp res(1), mul(*this);\n while (n > 0) {\n if (n & 1) res *= mul;\n mul *= mul;\n n >>= 1;\n }\n return res;\n }\n constexpr Fp inv() const {\n Fp res(1), div(*this);\n return res / div;\n }\n\n // other operators\n constexpr bool operator == (const Fp &r) const {\n return this->val == r.val;\n }\n constexpr bool operator != (const Fp &r) const {\n return this->val != r.val;\n }\n constexpr Fp& operator ++ () {\n ++val;\n if (val >= MOD) val -= MOD;\n return *this;\n }\n constexpr Fp& operator -- () {\n if (val == 0) val += MOD;\n --val;\n return *this;\n }\n constexpr Fp operator ++ (int) const {\n Fp res = *this;\n ++*this;\n return res;\n }\n constexpr Fp operator -- (int) const {\n Fp res = *this;\n --*this;\n return res;\n }\n friend constexpr istream& operator >> (istream &is, Fp<MOD> &x) {\n is >> x.val;\n x.val %= MOD;\n if (x.val < 0) x.val += MOD;\n return is;\n }\n friend constexpr ostream& operator << (ostream &os, const Fp<MOD> &x) {\n return os << x.val;\n }\n friend constexpr Fp<MOD> pow(const Fp<MOD> &r, long long n) {\n return r.pow(n);\n }\n friend constexpr Fp<MOD> inv(const Fp<MOD> &r) {\n return r.inv();\n }\n};\n\nnamespace NTT {\n long long modpow(long long a, long long n, int mod) {\n long long res = 1;\n while (n > 0) {\n if (n & 1) res = res * a % mod;\n a = a * a % mod;\n n >>= 1;\n }\n return res;\n }\n\n long long modinv(long long a, int mod) {\n long long b = mod, u = 1, v = 0;\n while (b) {\n long long t = a / b;\n a -= t * b, swap(a, b);\n u -= t * v, swap(u, v);\n }\n u %= mod;\n if (u < 0) u += mod;\n return u;\n }\n\n int calc_primitive_root(int mod) {\n if (mod == 2) return 1;\n if (mod == 167772161) return 3;\n if (mod == 469762049) return 3;\n if (mod == 754974721) return 11;\n if (mod == 998244353) return 3;\n int divs[20] = {};\n divs[0] = 2;\n int cnt = 1;\n long long x = (mod - 1) / 2;\n while (x % 2 == 0) x /= 2;\n for (long long i = 3; i * i <= x; i += 2) {\n if (x % i == 0) {\n divs[cnt++] = i;\n while (x % i == 0) x /= i;\n }\n }\n if (x > 1) divs[cnt++] = x;\n for (int g = 2;; g++) {\n bool ok = true;\n for (int i = 0; i < cnt; i++) {\n if (modpow(g, (mod - 1) / divs[i], mod) == 1) {\n ok = false;\n break;\n }\n }\n if (ok) return g;\n }\n }\n\n int get_fft_size(int N, int M) {\n int size_a = 1, size_b = 1;\n while (size_a < N) size_a <<= 1;\n while (size_b < M) size_b <<= 1;\n return max(size_a, size_b) << 1;\n }\n\n // number-theoretic transform\n template<class mint> void trans(vector<mint> &v, bool inv = false) {\n if (v.empty()) return;\n int N = (int)v.size();\n int MOD = v[0].get_mod();\n int PR = calc_primitive_root(MOD);\n static bool first = true;\n static vector<long long> vbw(30), vibw(30);\n if (first) {\n first = false;\n for (int k = 0; k < 30; ++k) {\n vbw[k] = modpow(PR, (MOD - 1) >> (k + 1), MOD);\n vibw[k] = modinv(vbw[k], MOD);\n }\n }\n for (int i = 0, j = 1; j < N - 1; j++) {\n for (int k = N >> 1; k > (i ^= k); k >>= 1);\n if (i > j) swap(v[i], v[j]);\n }\n for (int k = 0, t = 2; t <= N; ++k, t <<= 1) {\n long long bw = vbw[k];\n if (inv) bw = vibw[k];\n for (int i = 0; i < N; i += t) {\n mint w = 1;\n for (int j = 0; j < t/2; ++j) {\n int j1 = i + j, j2 = i + j + t/2;\n mint c1 = v[j1], c2 = v[j2] * w;\n v[j1] = c1 + c2;\n v[j2] = c1 - c2;\n w *= bw;\n }\n }\n }\n if (inv) {\n long long invN = modinv(N, MOD);\n for (int i = 0; i < N; ++i) v[i] = v[i] * invN;\n }\n }\n\n // for garner\n static constexpr int MOD0 = 754974721;\n static constexpr int MOD1 = 167772161;\n static constexpr int MOD2 = 469762049;\n using mint0 = Fp<MOD0>;\n using mint1 = Fp<MOD1>;\n using mint2 = Fp<MOD2>;\n static const mint1 imod0 = 95869806; // modinv(MOD0, MOD1);\n static const mint2 imod1 = 104391568; // modinv(MOD1, MOD2);\n static const mint2 imod01 = 187290749; // imod1 / MOD0;\n\n // small case (T = mint, long long)\n template<class T> vector<T> naive_mul(const vector<T> &A, const vector<T> &B) {\n if (A.empty() || B.empty()) return {};\n int N = (int)A.size(), M = (int)B.size();\n vector<T> res(N + M - 1);\n for (int i = 0; i < N; ++i)\n for (int j = 0; j < M; ++j)\n res[i + j] += A[i] * B[j];\n return res;\n }\n\n // mint\n template<class mint> vector<mint> mul(const vector<mint> &A, const vector<mint> &B) {\n if (A.empty() || B.empty()) return {};\n int N = (int)A.size(), M = (int)B.size();\n if (min(N, M) < 30) return naive_mul(A, B);\n int MOD = A[0].get_mod();\n int size_fft = get_fft_size(N, M);\n if (MOD == 998244353) {\n vector<mint> a(size_fft), b(size_fft), c(size_fft);\n for (int i = 0; i < N; ++i) a[i] = A[i];\n for (int i = 0; i < M; ++i) b[i] = B[i];\n trans(a), trans(b);\n vector<mint> res(size_fft);\n for (int i = 0; i < size_fft; ++i) res[i] = a[i] * b[i];\n trans(res, true);\n res.resize(N + M - 1);\n return res;\n }\n vector<mint0> a0(size_fft, 0), b0(size_fft, 0), c0(size_fft, 0);\n vector<mint1> a1(size_fft, 0), b1(size_fft, 0), c1(size_fft, 0);\n vector<mint2> a2(size_fft, 0), b2(size_fft, 0), c2(size_fft, 0);\n for (int i = 0; i < N; ++i)\n a0[i] = A[i].val, a1[i] = A[i].val, a2[i] = A[i].val;\n for (int i = 0; i < M; ++i)\n b0[i] = B[i].val, b1[i] = B[i].val, b2[i] = B[i].val;\n trans(a0), trans(a1), trans(a2), trans(b0), trans(b1), trans(b2);\n for (int i = 0; i < size_fft; ++i) {\n c0[i] = a0[i] * b0[i];\n c1[i] = a1[i] * b1[i];\n c2[i] = a2[i] * b2[i];\n }\n trans(c0, true), trans(c1, true), trans(c2, true);\n mint mod0 = MOD0, mod01 = mod0 * MOD1;\n vector<mint> res(N + M - 1);\n for (int i = 0; i < N + M - 1; ++i) {\n int y0 = c0[i].val;\n int y1 = (imod0 * (c1[i] - y0)).val;\n int y2 = (imod01 * (c2[i] - y0) - imod1 * y1).val;\n res[i] = mod01 * y2 + mod0 * y1 + y0;\n }\n return res;\n }\n};\n\n// Polynomial\ntemplate<typename mint> struct Poly : vector<mint> {\n using vector<mint>::vector;\n \n // constructor\n constexpr Poly(const vector<mint> &r) : vector<mint>(r) {}\n \n // core operator\n constexpr mint eval(const mint &v) {\n mint res = 0;\n for (int i = (int)this->size()-1; i >= 0; --i) {\n res *= v;\n res += (*this)[i];\n }\n return res;\n }\n constexpr Poly& normalize() {\n while (!this->empty() && this->back() == 0) this->pop_back();\n return *this;\n }\n \n // basic operator\n constexpr Poly operator - () const noexcept {\n Poly res = (*this);\n for (int i = 0; i < (int)res.size(); ++i) res[i] = -res[i];\n return res;\n }\n constexpr Poly operator + (const mint &v) const { return Poly(*this) += v; }\n constexpr Poly operator + (const Poly &r) const { return Poly(*this) += r; }\n constexpr Poly operator - (const mint &v) const { return Poly(*this) -= v; }\n constexpr Poly operator - (const Poly &r) const { return Poly(*this) -= r; }\n constexpr Poly operator * (const mint &v) const { return Poly(*this) *= v; }\n constexpr Poly operator * (const Poly &r) const { return Poly(*this) *= r; }\n constexpr Poly operator / (const mint &v) const { return Poly(*this) /= v; }\n constexpr Poly operator / (const Poly &r) const { return Poly(*this) /= r; }\n constexpr Poly operator % (const Poly &r) const { return Poly(*this) %= r; }\n constexpr Poly operator << (int x) const { return Poly(*this) <<= x; }\n constexpr Poly operator >> (int x) const { return Poly(*this) >>= x; }\n constexpr Poly& operator += (const mint &v) {\n if (this->empty()) this->resize(1);\n (*this)[0] += v;\n return *this;\n }\n constexpr Poly& operator += (const Poly &r) {\n if (r.size() > this->size()) this->resize(r.size());\n for (int i = 0; i < (int)r.size(); ++i) (*this)[i] += r[i];\n return this->normalize();\n }\n constexpr Poly& operator -= (const mint &v) {\n if (this->empty()) this->resize(1);\n (*this)[0] -= v;\n return *this;\n }\n constexpr Poly& operator -= (const Poly &r) {\n if (r.size() > this->size()) this->resize(r.size());\n for (int i = 0; i < (int)r.size(); ++i) (*this)[i] -= r[i];\n return this->normalize();\n }\n constexpr Poly& operator *= (const mint &v) {\n for (int i = 0; i < (int)this->size(); ++i) (*this)[i] *= v;\n return *this;\n }\n constexpr Poly& operator *= (const Poly &r) {\n return *this = NTT::mul((*this), r);\n }\n constexpr Poly& operator <<= (int x) {\n Poly res(x, 0);\n res.insert(res.end(), begin(*this), end(*this));\n return *this = res;\n }\n constexpr Poly& operator >>= (int x) {\n Poly res;\n res.insert(res.end(), begin(*this) + x, end(*this));\n return *this = res;\n }\n \n // division, pow\n constexpr Poly& operator /= (const mint &v) {\n assert(v != 0);\n mint iv = modinv(v);\n for (int i = 0; i < (int)this->size(); ++i) (*this)[i] *= iv;\n return *this;\n }\n constexpr Poly& operator /= (const Poly &r) {\n assert(!r.empty());\n assert(r.back() != 0);\n this->normalize();\n if (this->size() < r.size()) {\n this->clear();\n return *this;\n }\n int need = (int)this->size() - (int)r.size() + 1;\n *this = (rev().pre(need) * r.rev().inner_inv(need)).pre(need).rev();\n return *this;\n }\n constexpr Poly& operator %= (const Poly &r) {\n assert(!r.empty());\n assert(r.back() != 0);\n this->normalize();\n Poly q = (*this) / r;\n return *this -= q * r;\n }\n \n // FPS functions\n constexpr Poly pre(int siz) const {\n return Poly(begin(*this), begin(*this) + min((int)this->size(), siz));\n }\n constexpr Poly rev() const {\n Poly res = *this;\n reverse(begin(res), end(res));\n return res;\n }\n \n // df/dx\n constexpr Poly diff() const {\n int n = (int)this->size();\n Poly res(n-1);\n for (int i = 1; i < n; ++i) res[i-1] = (*this)[i] * i;\n return res;\n }\n \n // \\int f dx\n constexpr Poly integral() const {\n int n = (int)this->size();\n Poly res(n+1, 0);\n for (int i = 0; i < n; ++i) res[i+1] = (*this)[i] / (i+1);\n return res;\n }\n\n // inv(f), f[0] must not be 0\n constexpr Poly inner_inv(int deg) const {\n assert((*this)[0] != 0);\n if (deg < 0) deg = (int)this->size();\n Poly res({mint(1) / (*this)[0]});\n for (int i = 1; i < deg; i <<= 1) {\n res = (res + res - res * res * pre(i << 1)).pre(i << 1);\n }\n res.resize(deg);\n return res;\n }\n constexpr Poly inner_inv() const {\n return inner_inv((int)this->size());\n }\n \n // log(f) = \\int f'/f dx, f[0] must be 1\n constexpr Poly inner_log(int deg) const {\n assert((*this)[0] == 1);\n Poly res = (diff() * inner_inv(deg)).integral();\n res.resize(deg);\n return res;\n }\n constexpr Poly inner_log() const {\n return inner_log((int)this->size());\n }\n \n // exp(f), f[0] must be 0\n constexpr Poly inner_exp(int deg) const {\n assert((*this)[0] == 0);\n Poly res(1, 1);\n for (int i = 1; i < deg; i <<= 1) {\n res = res * (pre(i << 1) - res.inner_log(i << 1) + 1).pre(i << 1);\n }\n res.resize(deg);\n return res;\n }\n constexpr Poly inner_exp() const {\n return inner_exp((int)this->size());\n }\n \n // pow(f) = exp(e * log f)\n constexpr Poly inner_pow(long long e, int deg) const {\n if (e == 0) {\n Poly res(deg, 0);\n res[0] = 1;\n return res;\n }\n long long i = 0;\n while (i < (int)this->size() && (*this)[i] == 0) ++i;\n if (i == (int)this->size() || i > (deg - 1) / e) return Poly(deg, 0);\n mint k = (*this)[i];\n Poly res = ((((*this) >> i) / k).inner_log(deg) * e).inner_exp(deg)\n * mint(k).inner_pow(e) << (e * i);\n res.resize(deg);\n return res;\n }\n constexpr Poly inner_pow(long long e) const {\n return inner_pow(e, (int)this->size());\n }\n};\n\n\n/*/////////////////////////////*/\n// Polynomial Algorithms\n/*/////////////////////////////*/\n\n// Binomial coefficient\ntemplate<class T> struct BiCoef {\n vector<T> fact_, inv_, finv_;\n constexpr BiCoef() {}\n constexpr BiCoef(int n) : fact_(n, 1), inv_(n, 1), finv_(n, 1) {\n init(n);\n }\n constexpr void init(int n) {\n fact_.assign(n, 1), inv_.assign(n, 1), finv_.assign(n, 1);\n int MOD = fact_[0].get_mod();\n for(int i = 2; i < n; i++){\n fact_[i] = fact_[i-1] * i;\n inv_[i] = -inv_[MOD%i] * (MOD/i);\n finv_[i] = finv_[i-1] * inv_[i];\n }\n }\n constexpr T com(int n, int k) const {\n if (n < k || n < 0 || k < 0) return 0;\n return fact_[n] * finv_[k] * finv_[n-k];\n }\n constexpr T fact(int n) const {\n if (n < 0) return 0;\n return fact_[n];\n }\n constexpr T inv(int n) const {\n if (n < 0) return 0;\n return inv_[n];\n }\n constexpr T finv(int n) const {\n if (n < 0) return 0;\n return finv_[n];\n }\n};\n\n// Polynomial Taylor Shift\n// given: f(x), c\n// find: coefficients of f(x + c)\ntemplate<class mint> Poly<mint> PolynomialTaylorShift(const Poly<mint> &f, long long c) {\n int N = (int)f.size() - 1;\n BiCoef<mint> bc(N + 1);\n \n // convolution\n Poly<mint> p(N + 1), q(N + 1);\n for (int i = 0; i <= N; ++i) {\n p[i] = f[i] * bc.fact(i);\n q[N - i] = mint(c).pow(i) * bc.finv(i);\n }\n Poly<mint> pq = p * q;\n \n // result\n Poly<mint> res(N + 1);\n for (int i = 0; i <= N; ++i) res[i] = pq[i + N] * bc.finv(i);\n return res;\n}\n\n\n/*/////////////////////////////*/\n// for any mod\n/*/////////////////////////////*/\n\n// dynamic modint\nstruct DynamicModint {\n using mint = DynamicModint;\n \n // static menber\n static int MOD;\n \n // inner value\n long long val;\n \n // constructor\n DynamicModint() : val(0) { }\n DynamicModint(long long v) : val(v % MOD) {\n if (val < 0) val += MOD;\n }\n long long get() const { return val; }\n static int get_mod() { return MOD; }\n static void set_mod(int mod) { MOD = mod; }\n \n // arithmetic operators\n mint operator + () const { return mint(*this); }\n mint operator - () const { return mint(0) - mint(*this); }\n mint operator + (const mint &r) const { return mint(*this) += r; }\n mint operator - (const mint &r) const { return mint(*this) -= r; }\n mint operator * (const mint &r) const { return mint(*this) *= r; }\n mint operator / (const mint &r) const { return mint(*this) /= r; }\n mint& operator += (const mint &r) {\n val += r.val;\n if (val >= MOD) val -= MOD;\n return *this;\n }\n mint& operator -= (const mint &r) {\n val -= r.val;\n if (val < 0) val += MOD;\n return *this;\n }\n mint& operator *= (const mint &r) {\n val = val * r.val % MOD;\n return *this;\n }\n mint& operator /= (const mint &r) {\n long long a = r.val, b = MOD, u = 1, v = 0;\n while (b) {\n long long t = a / b;\n a -= t * b, swap(a, b);\n u -= t * v, swap(u, v);\n }\n val = val * u % MOD;\n if (val < 0) val += MOD;\n return *this;\n }\n mint pow(long long n) const {\n mint res(1), mul(*this);\n while (n > 0) {\n if (n & 1) res *= mul;\n mul *= mul;\n n >>= 1;\n }\n return res;\n }\n mint inv() const {\n mint res(1), div(*this);\n return res / div;\n }\n\n // other operators\n bool operator == (const mint &r) const {\n return this->val == r.val;\n }\n bool operator != (const mint &r) const {\n return this->val != r.val;\n }\n mint& operator ++ () {\n ++val;\n if (val >= MOD) val -= MOD;\n return *this;\n }\n mint& operator -- () {\n if (val == 0) val += MOD;\n --val;\n return *this;\n }\n mint operator ++ (int) {\n mint res = *this;\n ++*this;\n return res;\n }\n mint operator -- (int) {\n mint res = *this;\n --*this;\n return res;\n }\n friend istream& operator >> (istream &is, mint &x) {\n is >> x.val;\n x.val %= x.get_mod();\n if (x.val < 0) x.val += x.get_mod();\n return is;\n }\n friend ostream& operator << (ostream &os, const mint &x) {\n return os << x.val;\n }\n friend mint pow(const mint &r, long long n) {\n return r.pow(n);\n }\n friend mint inv(const mint &r) {\n return r.inv();\n }\n};\n\nint DynamicModint::MOD;\n\n\n\n/*/////////////////////////////*/\n// Examples\n/*/////////////////////////////*/\n\nvoid Yosupo_Convolution_mod_1000000007() {\n const int MOD = 1000000007;\n using mint = Fp<MOD>;\n \n int N, M;\n cin >> N >> M;\n Poly<mint> a(N), b(M);\n for (int i = 0; i < N; ++i) cin >> a[i];\n for (int i = 0; i < M; ++i) cin >> b[i];\n Poly<mint> res = a * b;\n for (auto v : res) cout << v << \" \";\n cout << endl;\n}\n\nvoid Yosupo_Division_of_Polynomials() {\n const int MOD = 998244353;\n using mint = Fp<MOD>;\n \n int N, M;\n cin >> N >> M;\n Poly<mint> f(N), g(M);\n for (int i = 0; i < N; ++i) cin >> f[i];\n for (int i = 0; i < M; ++i) cin >> g[i];\n \n Poly<mint> q = f / g, r = f - g * q;\n cout << q.size() << \" \" << r.size() << endl;\n for (auto v : q) cout << v << \" \";\n cout << endl;\n for (auto v : r) cout << v << \" \";\n cout << endl;\n}\n\nvoid Yosupo_Polynomial_Taylor_Shift() {\n const int MOD = 998244353;\n using mint = Fp<MOD>;\n \n long long N, c;\n cin >> N >> c;\n Poly<mint> a(N);\n for (int i = 0; i < N; ++i) cin >> a[i];\n \n Poly<mint> res = PolynomialTaylorShift(a, c);\n for (int i = 0; i < N; ++i) cout << res[i] << \" \";\n cout << endl;\n}\n\n// AGC 005 F\nvoid AGC_005_F() {\n const int MOD = 924844033;\n using mint = Fp<MOD>;\n \n int N;\n cin >> N;\n BiCoef<mint> bc(N + 1);\n vector<vector<int>> G(N);\n for (int i = 0; i < N-1; ++i) {\n int a, b;\n cin >> a >> b;\n --a, --b;\n G[a].push_back(b);\n G[b].push_back(a);\n }\n \n Poly<mint> f(N+1, 0);\n vector<int> si(N, 1);\n auto rec = [&](auto self, int v, int p = -1) -> void {\n for (auto ch : G[v]) {\n if (ch == p) continue;\n self(self, ch, v);\n ++f[si[ch]];\n si[v] += si[ch];\n }\n ++f[N - si[v]];\n };\n rec(rec, 0);\n \n Poly<mint> g = PolynomialTaylorShift(f, 1);\n for (int k = 1; k <= N; ++k) cout << bc.com(N, k) * N - g[k] << endl;\n}\n\n\n#define REP(i, n) for (long long i = 0; i < (long long)(n); ++i)\n#define REP2(i, a, b) for (long long i = a; i < (long long)(b); ++i)\n#define COUT(x) cout << #x << \" = \" << (x) << \" (L\" << __LINE__ << \")\" << endl\ntemplate<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P)\n{ return s << '<' << P.first << \", \" << P.second << '>'; }\ntemplate<class T> ostream& operator << (ostream &s, vector<T> P)\n{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << \" \"; } s << P[i]; } return s; }\ntemplate<class T> ostream& operator << (ostream &s, deque<T> P)\n{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << \" \"; } s << P[i]; } return s; }\ntemplate<class T> ostream& operator << (ostream &s, vector<vector<T> > P)\n{ for (int i = 0; i < P.size(); ++i) { s << endl << P[i]; } return s << endl; }\ntemplate<class T> ostream& operator << (ostream &s, set<T> P)\n{ for(auto it : P) { s << \"<\" << it << \"> \"; } return s; }\ntemplate<class T> ostream& operator << (ostream &s, multiset<T> P)\n{ for(auto it : P) { s << \"<\" << it << \"> \"; } return s; }\ntemplate<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P)\n{ for(auto it : P) { s << \"<\" << it.first << \"->\" << it.second << \"> \"; } return s; }\n\n\n\n// AOJ 2213\nvoid AOJ_2213() {\n using mint = DynamicModint;\n \n auto pow = [&](const Poly<mint> &f, long long n, const Poly<mint> &m) -> Poly<mint> {\n Poly<mint> res = {1}, mul = f;\n while (n > 0) {\n if (n & 1) res = res * mul % m;\n mul = mul * mul % m;\n n >>= 1;\n }\n return res;\n };\n auto gcd = [&](auto self, const Poly<mint> &a, const Poly<mint> &b) -> Poly<mint> {\n if (b.empty()) return a;\n else return self(self, b, a % b);\n };\n \n long long N, P;\n while (cin >> N >> P) {\n if (N == 0) break;\n mint::set_mod(P);\n \n Poly<mint> f(N+1);\n for (int i = 0; i < N+1; ++i) cin >> f[i];\n f.normalize();\n if (f.empty()) {\n cout << P << endl;\n continue;\n }\n Poly<mint> x = {0, 1};\n Poly<mint> g = pow(x, P, f) - x;\n Poly<mint> res = gcd(gcd, f, g);\n cout << (int)res.size() - 1 << endl;\n }\n}\n\n\nint main() {\n //Yosupo_Convolution_mod_1000000007();\n //Yosupo_Division_of_Polynomials();\n //Yosupo_Polynomial_Taylor_Shift();\n //AGC_005_F();\n AOJ_2213();\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3508, "score_of_the_acc": -1.049, "final_rank": 7 }, { "submission_id": "aoj_2213_4903764", "code_snippet": "//\n// Formal Power Series (on runtime mod)\n//\n// verified:\n// Yosupo Judge\n// https://judge.yosupo.jp/problem/inv_of_formal_power_series\n//\n// AOJ 2213 The Number of Solutions for a Polynomial\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2213\n//\n\n\n#include <bits/stdc++.h>\nusing namespace std;\n\n#define COUT(x) cout << #x << \" = \" << (x) << \" (L\" << __LINE__ << \")\" << endl\ntemplate<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P)\n{ return s << '<' << P.first << \", \" << P.second << '>'; }\ntemplate<class T> ostream& operator << (ostream &s, vector<T> P)\n{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << \" \"; } s << P[i]; } return s; }\ntemplate<class T> ostream& operator << (ostream &s, vector<vector<T> > P)\n{ for (int i = 0; i < P.size(); ++i) { s << endl << P[i]; } return s << endl; }\ntemplate<class T> ostream& operator << (ostream &s, set<T> P)\n{ for(auto it : P) { s << \"<\" << it << \"> \"; } return s << endl; }\ntemplate<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P)\n{ for(auto it : P) { s << \"<\" << it.first << \"->\" << it.second << \"> \"; } return s << endl; }\n\n\n// modint (replace MODS[0] on runtime)\nvector<int> MODS = { 1000000007, 754974721, 167772161, 469762049 };\ntemplate<int IND = 0> struct Fp {\n long long val;\n constexpr Fp(long long v = 0) noexcept : val(v % MODS[IND]) {\n if (val < 0) val += MODS[IND];\n }\n constexpr int getmod() const { return MODS[IND]; }\n constexpr Fp operator - () const noexcept {\n return val ? MODS[IND] - val : 0;\n }\n constexpr Fp operator + (const Fp& r) const noexcept { return Fp(*this) += r; }\n constexpr Fp operator - (const Fp& r) const noexcept { return Fp(*this) -= r; }\n constexpr Fp operator * (const Fp& r) const noexcept { return Fp(*this) *= r; }\n constexpr Fp operator / (const Fp& r) const noexcept { return Fp(*this) /= r; }\n constexpr Fp& operator += (const Fp& r) noexcept {\n val += r.val;\n if (val >= MODS[IND]) val -= MODS[IND];\n return *this;\n }\n constexpr Fp& operator -= (const Fp& r) noexcept {\n val -= r.val;\n if (val < 0) val += MODS[IND];\n return *this;\n }\n constexpr Fp& operator *= (const Fp& r) noexcept {\n val = val * r.val % MODS[IND];\n return *this;\n }\n constexpr Fp& operator /= (const Fp& r) noexcept {\n long long a = r.val, b = MODS[IND], u = 1, v = 0;\n while (b) {\n long long t = a / b;\n a -= t * b; swap(a, b);\n u -= t * v; swap(u, v);\n }\n val = val * u % MODS[IND];\n if (val < 0) val += MODS[IND];\n return *this;\n }\n constexpr bool operator == (const Fp& r) const noexcept {\n return this->val == r.val;\n }\n constexpr bool operator != (const Fp& r) const noexcept {\n return this->val != r.val;\n }\n friend constexpr istream& operator >> (istream& is, Fp<IND>& x) noexcept {\n is >> x.val;\n x.val %= MODS[IND];\n if (x.val < 0) x.val += MODS[IND];\n return is;\n }\n friend constexpr ostream& operator << (ostream& os, const Fp<IND>& x) noexcept {\n return os << x.val;\n }\n friend constexpr Fp<IND> modpow(const Fp<IND>& r, long long n) noexcept {\n if (n == 0) return 1;\n auto t = modpow(r, n / 2);\n t = t * t;\n if (n & 1) t = t * r;\n return t;\n }\n friend constexpr Fp<IND> modinv(const Fp<IND>& r) noexcept {\n long long a = r.val, b = MODS[IND], u = 1, v = 0;\n while (b) {\n long long t = a / b;\n a -= t * b, swap(a, b);\n u -= t * v, swap(u, v);\n }\n return Fp<IND>(u);\n }\n};\n\nnamespace NTT { \n long long modpow(long long a, long long n, int mod) {\n long long res = 1;\n while (n > 0) {\n if (n & 1) res = res * a % mod;\n a = a * a % mod;\n n >>= 1;\n }\n return res;\n }\n\n long long modinv(long long a, int mod) {\n long long b = mod, u = 1, v = 0;\n while (b) {\n long long t = a / b;\n a -= t * b, swap(a, b);\n u -= t * v, swap(u, v);\n }\n u %= mod;\n if (u < 0) u += mod;\n return u;\n }\n\n int calc_primitive_root(int mod) {\n if (mod == 2) return 1;\n if (mod == 167772161) return 3;\n if (mod == 469762049) return 3;\n if (mod == 754974721) return 11;\n if (mod == 998244353) return 3;\n int divs[20] = {};\n divs[0] = 2;\n int cnt = 1;\n long long x = (mod - 1) / 2;\n while (x % 2 == 0) x /= 2;\n for (long long i = 3; i * i <= x; i += 2) {\n if (x % i == 0) {\n divs[cnt++] = i;\n while (x % i == 0) x /= i;\n }\n }\n if (x > 1) divs[cnt++] = x;\n for (int g = 2;; g++) {\n bool ok = true;\n for (int i = 0; i < cnt; i++) {\n if (modpow(g, (mod - 1) / divs[i], mod) == 1) {\n ok = false;\n break;\n }\n }\n if (ok) return g;\n }\n }\n\n int get_fft_size(int N, int M) {\n int size_a = 1, size_b = 1;\n while (size_a < N) size_a <<= 1;\n while (size_b < M) size_b <<= 1;\n return max(size_a, size_b) << 1;\n }\n\n // number-theoretic transform\n set<int> setmod;\n template<class mint> void trans(vector<mint>& v, bool inv = false) {\n if (v.empty()) return;\n int N = (int)v.size();\n int MOD = v[0].getmod();\n int PR = calc_primitive_root(MOD);\n static vector<long long> vbw(30), vibw(30);\n if (!setmod.count(MOD)) {\n setmod.insert(MOD);\n for (int k = 0; k < 30; ++k) {\n vbw[k] = modpow(PR, (MOD - 1) >> (k + 1), MOD);\n vibw[k] = modinv(vbw[k], MOD);\n }\n }\n for (int i = 0, j = 1; j < N - 1; j++) {\n for (int k = N >> 1; k > (i ^= k); k >>= 1);\n if (i > j) swap(v[i], v[j]);\n }\n for (int k = 0, t = 2; t <= N; ++k, t <<= 1) {\n long long bw = vbw[k];\n if (inv) bw = vibw[k];\n for (int i = 0; i < N; i += t) {\n mint w = 1;\n for (int j = 0; j < t/2; ++j) {\n int j1 = i + j, j2 = i + j + t/2;\n mint c1 = v[j1], c2 = v[j2] * w;\n v[j1] = c1 + c2;\n v[j2] = c1 - c2;\n w *= bw;\n }\n }\n }\n if (inv) {\n long long invN = modinv(N, MOD);\n for (int i = 0; i < N; ++i) v[i] = v[i] * invN;\n }\n }\n\n // for garner\n static constexpr int IND1 = 1; //754974721;\n static constexpr int IND2 = 2; //167772161;\n static constexpr int IND3 = 3; //469762049;\n using mint1 = Fp<IND1>;\n using mint2 = Fp<IND2>;\n using mint3 = Fp<IND3>;\n static const mint2 imod1 = 95869806; // modinv(MOD1, MOD2);\n static const mint3 imod2 = 104391568; // modinv(MOD2, MOD3);\n static const mint3 imod12 = 187290749; // imod2 / MOD1;\n\n // small case (T = mint, long long)\n template<class T> vector<T> naive_mul \n (const vector<T>& A, const vector<T>& B) {\n if (A.empty() || B.empty()) return {};\n int N = (int)A.size(), M = (int)B.size();\n vector<T> res(N + M - 1);\n for (int i = 0; i < N; ++i)\n for (int j = 0; j < M; ++j)\n res[i + j] += A[i] * B[j];\n return res;\n }\n\n // mint\n template<class mint> vector<mint> mul\n (const vector<mint>& A, const vector<mint>& B) {\n if (A.empty() || B.empty()) return {};\n int N = (int)A.size(), M = (int)B.size();\n if (min(N, M) < 80) return naive_mul(A, B);\n int MOD = A[0].getmod();\n int size_fft = get_fft_size(N, M);\n if (MOD == 998244353) {\n vector<mint> a(size_fft), b(size_fft), c(size_fft);\n for (int i = 0; i < N; ++i) a[i] = A[i];\n for (int i = 0; i < M; ++i) b[i] = B[i];\n trans(a), trans(b);\n vector<mint> res(size_fft);\n for (int i = 0; i < size_fft; ++i) res[i] = a[i] * b[i];\n trans(res, true);\n res.resize(N + M - 1);\n return res;\n }\n vector<mint1> a1(size_fft, 0), b1(size_fft, 0), c1(size_fft, 0);\n vector<mint2> a2(size_fft, 0), b2(size_fft, 0), c2(size_fft, 0);\n vector<mint3> a3(size_fft, 0), b3(size_fft, 0), c3(size_fft, 0);\n for (int i = 0; i < N; ++i)\n a1[i] = A[i].val, a2[i] = A[i].val, a3[i] = A[i].val;\n for (int i = 0; i < M; ++i)\n b1[i] = B[i].val, b2[i] = B[i].val, b3[i] = B[i].val;\n trans(a1), trans(a2), trans(a3), trans(b1), trans(b2), trans(b3);\n for (int i = 0; i < size_fft; ++i) {\n c1[i] = a1[i] * b1[i];\n c2[i] = a2[i] * b2[i];\n c3[i] = a3[i] * b3[i];\n }\n trans(c1, true), trans(c2, true), trans(c3, true);\n mint mod1 = MODS[IND1], mod12 = mod1 * MODS[IND2];\n vector<mint> res(N + M - 1);\n for (int i = 0; i < N + M - 1; ++i) {\n int y1 = c1[i].val;\n int y2 = (imod1 * (c2[i] - y1)).val;\n int y3 = (imod12 * (c3[i] - y1) - imod2 * y2).val;\n res[i] = mod12 * y3 + mod1 * y2 + y1;\n }\n return res;\n }\n};\n\n// Binomial Coefficient\ntemplate<class T> struct BiCoef {\n vector<T> fact_, inv_, finv_;\n constexpr BiCoef() {}\n constexpr BiCoef(int n) noexcept : fact_(n, 1), inv_(n, 1), finv_(n, 1) {\n init(n);\n }\n constexpr void init(int n) noexcept {\n fact_.assign(n, 1), inv_.assign(n, 1), finv_.assign(n, 1);\n int MOD = fact_[0].getmod();\n for(int i = 2; i < n; i++){\n fact_[i] = fact_[i-1] * i;\n inv_[i] = -inv_[MOD%i] * (MOD/i);\n finv_[i] = finv_[i-1] * inv_[i];\n }\n }\n constexpr T com(int n, int k) const noexcept {\n if (n < k || n < 0 || k < 0) return 0;\n return fact_[n] * finv_[k] * finv_[n-k];\n }\n constexpr T fact(int n) const noexcept {\n if (n < 0) return 0;\n return fact_[n];\n }\n constexpr T inv(int n) const noexcept {\n if (n < 0) return 0;\n return inv_[n];\n }\n constexpr T finv(int n) const noexcept {\n if (n < 0) return 0;\n return finv_[n];\n }\n};\n\n// Formal Power Series\ntemplate <typename mint> struct FPS : vector<mint> {\n using vector<mint>::vector;\n \n // constructor\n FPS(const vector<mint>& r) : vector<mint>(r) {}\n \n // core operator\n inline FPS pre(int siz) const {\n return FPS(begin(*this), begin(*this) + min((int)this->size(), siz));\n }\n inline FPS rev() const {\n FPS res = *this;\n reverse(begin(res), end(res));\n return res;\n }\n inline FPS& normalize() {\n while (!this->empty() && this->back() == 0) this->pop_back();\n return *this;\n }\n \n // basic operator\n inline FPS operator - () const noexcept {\n FPS res = (*this);\n for (int i = 0; i < (int)res.size(); ++i) res[i] = -res[i];\n return res;\n }\n inline FPS operator + (const mint& v) const { return FPS(*this) += v; }\n inline FPS operator + (const FPS& r) const { return FPS(*this) += r; }\n inline FPS operator - (const mint& v) const { return FPS(*this) -= v; }\n inline FPS operator - (const FPS& r) const { return FPS(*this) -= r; }\n inline FPS operator * (const mint& v) const { return FPS(*this) *= v; }\n inline FPS operator * (const FPS& r) const { return FPS(*this) *= r; }\n inline FPS operator / (const mint& v) const { return FPS(*this) /= v; }\n inline FPS operator << (int x) const { return FPS(*this) <<= x; }\n inline FPS operator >> (int x) const { return FPS(*this) >>= x; }\n inline FPS& operator += (const mint& v) {\n if (this->empty()) this->resize(1);\n (*this)[0] += v;\n return *this;\n }\n inline FPS& operator += (const FPS& r) {\n if (r.size() > this->size()) this->resize(r.size());\n for (int i = 0; i < (int)r.size(); ++i) (*this)[i] += r[i];\n return this->normalize();\n }\n inline FPS& operator -= (const mint& v) {\n if (this->empty()) this->resize(1);\n (*this)[0] -= v;\n return *this;\n }\n inline FPS& operator -= (const FPS& r) {\n if (r.size() > this->size()) this->resize(r.size());\n for (int i = 0; i < (int)r.size(); ++i) (*this)[i] -= r[i];\n return this->normalize();\n }\n inline FPS& operator *= (const mint& v) {\n for (int i = 0; i < (int)this->size(); ++i) (*this)[i] *= v;\n return *this;\n }\n inline FPS& operator *= (const FPS& r) {\n return *this = NTT::mul((*this), r);\n }\n inline FPS& operator /= (const mint& v) {\n assert(v != 0);\n mint iv = modinv(v);\n for (int i = 0; i < (int)this->size(); ++i) (*this)[i] *= iv;\n return *this;\n }\n inline FPS& operator <<= (int x) {\n FPS res(x, 0);\n res.insert(res.end(), begin(*this), end(*this));\n return *this = res;\n }\n inline FPS& operator >>= (int x) {\n FPS res;\n res.insert(res.end(), begin(*this) + x, end(*this));\n return *this = res;\n }\n inline mint eval(const mint& v){\n mint res = 0;\n for (int i = (int)this->size()-1; i >= 0; --i) {\n res *= v;\n res += (*this)[i];\n }\n return res;\n }\n inline friend FPS gcd(const FPS& f, const FPS& g) {\n if (g.empty()) return f;\n return gcd(g, f % g);\n }\n\n // advanced operation\n // df/dx\n inline friend FPS diff(const FPS& f) {\n int n = (int)f.size();\n FPS res(n-1);\n for (int i = 1; i < n; ++i) res[i-1] = f[i] * i;\n return res;\n }\n\n // \\int f dx\n inline friend FPS integral(const FPS& f) {\n int n = (int)f.size();\n FPS res(n+1, 0);\n for (int i = 0; i < n; ++i) res[i+1] = f[i] / (i+1);\n return res;\n }\n\n // inv(f), f[0] must not be 0\n inline friend FPS inv(const FPS& f, int deg) {\n assert(f[0] != 0);\n if (deg < 0) deg = (int)f.size();\n FPS res({mint(1) / f[0]});\n for (int i = 1; i < deg; i <<= 1) {\n res = (res + res - res * res * f.pre(i << 1)).pre(i << 1);\n }\n res.resize(deg);\n return res;\n }\n inline friend FPS inv(const FPS& f) {\n return inv(f, f.size());\n }\n\n // division, r must be normalized (r.back() must not be 0)\n inline FPS& operator /= (const FPS& r) {\n assert(!r.empty());\n assert(r.back() != 0);\n this->normalize();\n if (this->size() < r.size()) {\n this->clear();\n return *this;\n }\n int need = (int)this->size() - (int)r.size() + 1;\n *this = ((*this).rev().pre(need) * inv(r.rev(), need)).pre(need).rev();\n return *this;\n }\n inline FPS& operator %= (const FPS &r) {\n assert(!r.empty());\n assert(r.back() != 0);\n this->normalize();\n FPS q = (*this) / r;\n return *this -= q * r;\n }\n inline FPS operator / (const FPS& r) const { return FPS(*this) /= r; }\n inline FPS operator % (const FPS& r) const { return FPS(*this) %= r; }\n\n // log(f) = \\int f'/f dx, f[0] must be 1\n inline friend FPS log(const FPS& f, int deg) {\n assert(f[0] == 1);\n FPS res = integral(diff(f) * inv(f, deg));\n res.resize(deg);\n return res;\n }\n inline friend FPS log(const FPS& f) {\n return log(f, f.size());\n }\n\n // exp(f), f[0] must be 0\n inline friend FPS exp(const FPS& f, int deg) {\n assert(f[0] == 0);\n FPS res(1, 1);\n for (int i = 1; i < deg; i <<= 1) {\n res = res * (f.pre(i<<1) - log(res, i<<1) + 1).pre(i<<1);\n }\n res.resize(deg);\n return res;\n }\n inline friend FPS exp(const FPS& f) {\n return exp(f, f.size());\n }\n\n // pow(f) = exp(e * log f)\n inline friend FPS pow(const FPS& f, long long e, int deg) {\n long long i = 0;\n while (i < (int)f.size() && f[i] == 0) ++i;\n if (i == (int)f.size()) return FPS(deg, 0);\n if (i * e >= deg) return FPS(deg, 0);\n mint k = f[i];\n FPS res = exp(log((f >> i) / k) * e) * modpow(k, e) << (e * i);\n res.resize(deg);\n return res;\n }\n inline friend FPS pow(const FPS& f, long long e) {\n return pow(f, e, f.size());\n }\n\n // sqrt(f), f[0] must be 1\n inline friend FPS sqrt_base(const FPS& f, int deg) {\n assert(f[0] == 1);\n mint inv2 = mint(1) / 2;\n FPS res(1, 1);\n for (int i = 1; i < deg; i <<= 1) {\n res = (res + f.pre(i << 1) * inv(res, i << 1)).pre(i << 1);\n for (mint& x : res) x *= inv2;\n }\n res.resize(deg);\n return res;\n }\n inline friend FPS sqrt_base(const FPS& f) {\n return sqrt_base(f, f.size());\n }\n};\n\nusing mint = Fp<>;\n\n\n\n////////////////////////////////////////\n// solver\n////////////////////////////////////////\n\nFPS<mint> modpow(const FPS<mint> &f, long long n, const FPS<mint> &m) {\n if (n == 0) return FPS<mint>(1, 1);\n auto t = modpow(f, n / 2, m);\n t = (t * t) % m;\n if (n & 1) t = (t * f) % m;\n auto q = t / m;\n auto r = t % m;\n return t;\n}\n\nvoid AOJ2213() {\n long long N, P;\n while (cin >> N >> P) {\n if (N == 0) break;\n MODS[0] = P;\n\t\t\n FPS<mint> f(N+1);\n for (int i = 0; i < N+1; ++i) cin >> f[i];\n f.normalize();\n if (f.empty()) { \n cout << P << endl;\n continue; \n }\n \n FPS<mint> x(2, 0);\n x[1] = 1; // 多項式 x\n FPS<mint> g = modpow(x, P, f) - x;\n FPS<mint> res = gcd(f, g);\n cout << (int)res.size() - 1 << endl;\n }\n}\n\nint main() {\n AOJ2213();\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 3640, "score_of_the_acc": -1.1965, "final_rank": 10 }, { "submission_id": "aoj_2213_4896110", "code_snippet": "//\n// Formal Power Series (on runtime mod)\n//\n// verified:\n// Yosupo Judge\n// https://judge.yosupo.jp/problem/inv_of_formal_power_series\n//\n// AOJ 2213 The Number of Solutions for a Polynomial\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2213\n//\n\n\n#include <bits/stdc++.h>\nusing namespace std;\n\n#define COUT(x) cout << #x << \" = \" << (x) << \" (L\" << __LINE__ << \")\" << endl\ntemplate<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P)\n{ return s << '<' << P.first << \", \" << P.second << '>'; }\ntemplate<class T> ostream& operator << (ostream &s, vector<T> P)\n{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << \" \"; } s << P[i]; } return s; }\ntemplate<class T> ostream& operator << (ostream &s, vector<vector<T> > P)\n{ for (int i = 0; i < P.size(); ++i) { s << endl << P[i]; } return s << endl; }\ntemplate<class T> ostream& operator << (ostream &s, set<T> P)\n{ for(auto it : P) { s << \"<\" << it << \"> \"; } return s << endl; }\ntemplate<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P)\n{ for(auto it : P) { s << \"<\" << it.first << \"->\" << it.second << \"> \"; } return s << endl; }\n\n\n// modint (replace MODS[0] on runtime)\nvector<int> MODS = { 1000000007, 754974721, 167772161, 469762049 };\ntemplate<int IND = 0> struct Fp {\n long long val;\n constexpr Fp(long long v = 0) noexcept : val(v % MODS[IND]) {\n if (val < 0) val += MODS[IND];\n }\n constexpr int getmod() const { return MODS[IND]; }\n constexpr Fp operator - () const noexcept {\n return val ? MODS[IND] - val : 0;\n }\n constexpr Fp operator + (const Fp& r) const noexcept { return Fp(*this) += r; }\n constexpr Fp operator - (const Fp& r) const noexcept { return Fp(*this) -= r; }\n constexpr Fp operator * (const Fp& r) const noexcept { return Fp(*this) *= r; }\n constexpr Fp operator / (const Fp& r) const noexcept { return Fp(*this) /= r; }\n constexpr Fp& operator += (const Fp& r) noexcept {\n val += r.val;\n if (val >= MODS[IND]) val -= MODS[IND];\n return *this;\n }\n constexpr Fp& operator -= (const Fp& r) noexcept {\n val -= r.val;\n if (val < 0) val += MODS[IND];\n return *this;\n }\n constexpr Fp& operator *= (const Fp& r) noexcept {\n val = val * r.val % MODS[IND];\n return *this;\n }\n constexpr Fp& operator /= (const Fp& r) noexcept {\n long long a = r.val, b = MODS[IND], u = 1, v = 0;\n while (b) {\n long long t = a / b;\n a -= t * b; swap(a, b);\n u -= t * v; swap(u, v);\n }\n val = val * u % MODS[IND];\n if (val < 0) val += MODS[IND];\n return *this;\n }\n constexpr bool operator == (const Fp& r) const noexcept {\n return this->val == r.val;\n }\n constexpr bool operator != (const Fp& r) const noexcept {\n return this->val != r.val;\n }\n friend constexpr istream& operator >> (istream& is, Fp<IND>& x) noexcept {\n is >> x.val;\n x.val %= MODS[IND];\n if (x.val < 0) x.val += MODS[IND];\n return is;\n }\n friend constexpr ostream& operator << (ostream& os, const Fp<IND>& x) noexcept {\n return os << x.val;\n }\n friend constexpr Fp<IND> modpow(const Fp<IND>& r, long long n) noexcept {\n if (n == 0) return 1;\n auto t = modpow(r, n / 2);\n t = t * t;\n if (n & 1) t = t * r;\n return t;\n }\n friend constexpr Fp<IND> modinv(const Fp<IND>& r) noexcept {\n long long a = r.val, b = MODS[IND], u = 1, v = 0;\n while (b) {\n long long t = a / b;\n a -= t * b, swap(a, b);\n u -= t * v, swap(u, v);\n }\n return Fp<IND>(u);\n }\n};\n\nnamespace NTT { \n long long modpow(long long a, long long n, int mod) {\n long long res = 1;\n while (n > 0) {\n if (n & 1) res = res * a % mod;\n a = a * a % mod;\n n >>= 1;\n }\n return res;\n }\n\n long long modinv(long long a, int mod) {\n long long b = mod, u = 1, v = 0;\n while (b) {\n long long t = a / b;\n a -= t * b, swap(a, b);\n u -= t * v, swap(u, v);\n }\n u %= mod;\n if (u < 0) u += mod;\n return u;\n }\n\n int calc_primitive_root(int mod) {\n if (mod == 2) return 1;\n if (mod == 167772161) return 3;\n if (mod == 469762049) return 3;\n if (mod == 754974721) return 11;\n if (mod == 998244353) return 3;\n int divs[20] = {};\n divs[0] = 2;\n int cnt = 1;\n long long x = (mod - 1) / 2;\n while (x % 2 == 0) x /= 2;\n for (long long i = 3; i * i <= x; i += 2) {\n if (x % i == 0) {\n divs[cnt++] = i;\n while (x % i == 0) x /= i;\n }\n }\n if (x > 1) divs[cnt++] = x;\n for (int g = 2;; g++) {\n bool ok = true;\n for (int i = 0; i < cnt; i++) {\n if (modpow(g, (mod - 1) / divs[i], mod) == 1) {\n ok = false;\n break;\n }\n }\n if (ok) return g;\n }\n }\n\n int get_fft_size(int N, int M) {\n int size_a = 1, size_b = 1;\n while (size_a < N) size_a <<= 1;\n while (size_b < M) size_b <<= 1;\n return max(size_a, size_b) << 1;\n }\n\n // number-theoretic transform\n set<int> setmod;\n template<class mint> void trans(vector<mint>& v, bool inv = false) {\n if (v.empty()) return;\n int N = (int)v.size();\n int MOD = v[0].getmod();\n int PR = calc_primitive_root(MOD);\n static vector<long long> vbw(30), vibw(30);\n if (!setmod.count(MOD)) {\n setmod.insert(MOD);\n for (int k = 0; k < 30; ++k) {\n vbw[k] = modpow(PR, (MOD - 1) >> (k + 1), MOD);\n vibw[k] = modinv(vbw[k], MOD);\n }\n }\n for (int i = 0, j = 1; j < N - 1; j++) {\n for (int k = N >> 1; k > (i ^= k); k >>= 1);\n if (i > j) swap(v[i], v[j]);\n }\n for (int k = 0, t = 2; t <= N; ++k, t <<= 1) {\n long long bw = vbw[k];\n if (inv) bw = vibw[k];\n for (int i = 0; i < N; i += t) {\n mint w = 1;\n for (int j = 0; j < t/2; ++j) {\n int j1 = i + j, j2 = i + j + t/2;\n mint c1 = v[j1], c2 = v[j2] * w;\n v[j1] = c1 + c2;\n v[j2] = c1 - c2;\n w *= bw;\n }\n }\n }\n if (inv) {\n long long invN = modinv(N, MOD);\n for (int i = 0; i < N; ++i) v[i] = v[i] * invN;\n }\n }\n\n // for garner\n static constexpr int IND1 = 1; //754974721;\n static constexpr int IND2 = 2; //167772161;\n static constexpr int IND3 = 3; //469762049;\n using mint1 = Fp<IND1>;\n using mint2 = Fp<IND2>;\n using mint3 = Fp<IND3>;\n static const mint2 imod1 = 95869806; // modinv(MOD1, MOD2);\n static const mint3 imod2 = 104391568; // modinv(MOD2, MOD3);\n static const mint3 imod12 = 187290749; // imod2 / MOD1;\n\n // small case (T = mint, long long)\n template<class T> vector<T> naive_mul \n (const vector<T>& A, const vector<T>& B) {\n if (A.empty() || B.empty()) return {};\n int N = (int)A.size(), M = (int)B.size();\n vector<T> res(N + M - 1);\n for (int i = 0; i < N; ++i)\n for (int j = 0; j < M; ++j)\n res[i + j] += A[i] * B[j];\n return res;\n }\n\n // mint\n template<class mint> vector<mint> mul\n (const vector<mint>& A, const vector<mint>& B) {\n if (A.empty() || B.empty()) return {};\n int N = (int)A.size(), M = (int)B.size();\n if (min(N, M) < 80) return naive_mul(A, B);\n int MOD = A[0].getmod();\n int size_fft = get_fft_size(N, M);\n if (MOD == 998244353) {\n vector<mint> a(size_fft), b(size_fft), c(size_fft);\n for (int i = 0; i < N; ++i) a[i] = A[i];\n for (int i = 0; i < M; ++i) b[i] = B[i];\n trans(a), trans(b);\n vector<mint> res(size_fft);\n for (int i = 0; i < size_fft; ++i) res[i] = a[i] * b[i];\n trans(res, true);\n res.resize(N + M - 1);\n return res;\n }\n vector<mint1> a1(size_fft, 0), b1(size_fft, 0), c1(size_fft, 0);\n vector<mint2> a2(size_fft, 0), b2(size_fft, 0), c2(size_fft, 0);\n vector<mint3> a3(size_fft, 0), b3(size_fft, 0), c3(size_fft, 0);\n for (int i = 0; i < N; ++i)\n a1[i] = A[i].val, a2[i] = A[i].val, a3[i] = A[i].val;\n for (int i = 0; i < M; ++i)\n b1[i] = B[i].val, b2[i] = B[i].val, b3[i] = B[i].val;\n trans(a1), trans(a2), trans(a3), trans(b1), trans(b2), trans(b3);\n for (int i = 0; i < size_fft; ++i) {\n c1[i] = a1[i] * b1[i];\n c2[i] = a2[i] * b2[i];\n c3[i] = a3[i] * b3[i];\n }\n trans(c1, true), trans(c2, true), trans(c3, true);\n mint mod1 = MODS[IND1], mod12 = mod1 * MODS[IND2];\n vector<mint> res(N + M - 1);\n for (int i = 0; i < N + M - 1; ++i) {\n int y1 = c1[i].val;\n int y2 = (imod1 * (c2[i] - y1)).val;\n int y3 = (imod12 * (c3[i] - y1) - imod2 * y2).val;\n res[i] = mod12 * y3 + mod1 * y2 + y1;\n }\n return res;\n }\n};\n\n// Binomial Coefficient\ntemplate<class T> struct BiCoef {\n vector<T> fact_, inv_, finv_;\n constexpr BiCoef() {}\n constexpr BiCoef(int n) noexcept : fact_(n, 1), inv_(n, 1), finv_(n, 1) {\n init(n);\n }\n constexpr void init(int n) noexcept {\n fact_.assign(n, 1), inv_.assign(n, 1), finv_.assign(n, 1);\n int MOD = fact_[0].getmod();\n for(int i = 2; i < n; i++){\n fact_[i] = fact_[i-1] * i;\n inv_[i] = -inv_[MOD%i] * (MOD/i);\n finv_[i] = finv_[i-1] * inv_[i];\n }\n }\n constexpr T com(int n, int k) const noexcept {\n if (n < k || n < 0 || k < 0) return 0;\n return fact_[n] * finv_[k] * finv_[n-k];\n }\n constexpr T fact(int n) const noexcept {\n if (n < 0) return 0;\n return fact_[n];\n }\n constexpr T inv(int n) const noexcept {\n if (n < 0) return 0;\n return inv_[n];\n }\n constexpr T finv(int n) const noexcept {\n if (n < 0) return 0;\n return finv_[n];\n }\n};\n\n// Formal Power Series\ntemplate <typename mint> struct FPS : vector<mint> {\n using vector<mint>::vector;\n \n // constructor\n FPS(const vector<mint>& r) : vector<mint>(r) {}\n \n // core operator\n inline FPS pre(int siz) const {\n return FPS(begin(*this), begin(*this) + min((int)this->size(), siz));\n }\n inline FPS rev() const {\n FPS res = *this;\n reverse(begin(res), end(res));\n return res;\n }\n inline FPS& normalize() {\n while (!this->empty() && this->back() == 0) this->pop_back();\n return *this;\n }\n \n // basic operator\n inline FPS operator - () const noexcept {\n FPS res = (*this);\n for (int i = 0; i < (int)res.size(); ++i) res[i] = -res[i];\n return res;\n }\n inline FPS operator + (const mint& v) const { return FPS(*this) += v; }\n inline FPS operator + (const FPS& r) const { return FPS(*this) += r; }\n inline FPS operator - (const mint& v) const { return FPS(*this) -= v; }\n inline FPS operator - (const FPS& r) const { return FPS(*this) -= r; }\n inline FPS operator * (const mint& v) const { return FPS(*this) *= v; }\n inline FPS operator * (const FPS& r) const { return FPS(*this) *= r; }\n inline FPS operator / (const mint& v) const { return FPS(*this) /= v; }\n inline FPS operator << (int x) const { return FPS(*this) <<= x; }\n inline FPS operator >> (int x) const { return FPS(*this) >>= x; }\n inline FPS& operator += (const mint& v) {\n if (this->empty()) this->resize(1);\n (*this)[0] += v;\n return *this;\n }\n inline FPS& operator += (const FPS& r) {\n if (r.size() > this->size()) this->resize(r.size());\n for (int i = 0; i < (int)r.size(); ++i) (*this)[i] += r[i];\n return this->normalize();\n }\n inline FPS& operator -= (const mint& v) {\n if (this->empty()) this->resize(1);\n (*this)[0] -= v;\n return *this;\n }\n inline FPS& operator -= (const FPS& r) {\n if (r.size() > this->size()) this->resize(r.size());\n for (int i = 0; i < (int)r.size(); ++i) (*this)[i] -= r[i];\n return this->normalize();\n }\n inline FPS& operator *= (const mint& v) {\n for (int i = 0; i < (int)this->size(); ++i) (*this)[i] *= v;\n return *this;\n }\n inline FPS& operator *= (const FPS& r) {\n return *this = NTT::mul((*this), r);\n }\n inline FPS& operator /= (const mint& v) {\n assert(v != 0);\n mint iv = modinv(v);\n for (int i = 0; i < (int)this->size(); ++i) (*this)[i] *= iv;\n return *this;\n }\n inline FPS& operator <<= (int x) {\n FPS res(x, 0);\n res.insert(res.end(), begin(*this), end(*this));\n return *this = res;\n }\n inline FPS& operator >>= (int x) {\n FPS res;\n res.insert(res.end(), begin(*this) + x, end(*this));\n return *this = res;\n }\n inline mint eval(const mint& v){\n mint res = 0;\n for (int i = (int)this->size()-1; i >= 0; --i) {\n res *= v;\n res += (*this)[i];\n }\n return res;\n }\n inline friend FPS gcd(const FPS& f, const FPS& g) {\n if (g.empty()) return f;\n return gcd(g, f % g);\n }\n\n // advanced operation\n // df/dx\n inline friend FPS diff(const FPS& f) {\n int n = (int)f.size();\n FPS res(n-1);\n for (int i = 1; i < n; ++i) res[i-1] = f[i] * i;\n return res;\n }\n\n // \\int f dx\n inline friend FPS integral(const FPS& f) {\n int n = (int)f.size();\n FPS res(n+1, 0);\n for (int i = 0; i < n; ++i) res[i+1] = f[i] / (i+1);\n return res;\n }\n\n // inv(f), f[0] must not be 0\n inline friend FPS inv(const FPS& f, int deg) {\n assert(f[0] != 0);\n if (deg < 0) deg = (int)f.size();\n FPS res({mint(1) / f[0]});\n for (int i = 1; i < deg; i <<= 1) {\n res = (res + res - res * res * f.pre(i << 1)).pre(i << 1);\n }\n return res.pre(deg);\n }\n inline friend FPS inv(const FPS& f) {\n return inv(f, f.size());\n }\n\n // division, r must be normalized (r.back() must not be 0)\n inline FPS& operator /= (const FPS& r) {\n assert(!r.empty());\n assert(r.back() != 0);\n this->normalize();\n if (this->size() < r.size()) {\n this->clear();\n return *this;\n }\n int need = (int)this->size() - (int)r.size() + 1;\n *this = ((*this).rev().pre(need) * inv(r.rev(), need)).pre(need).rev();\n return *this;\n }\n inline FPS& operator %= (const FPS &r) {\n assert(!r.empty());\n assert(r.back() != 0);\n this->normalize();\n FPS q = (*this) / r;\n return *this -= q * r;\n }\n inline FPS operator / (const FPS& r) const { return FPS(*this) /= r; }\n inline FPS operator % (const FPS& r) const { return FPS(*this) %= r; }\n\n // log(f) = \\int f'/f dx, f[0] must be 1\n inline friend FPS log(const FPS& f, int deg) {\n assert(f[0] == 1);\n FPS res = integral(diff(f) * inv(f, deg));\n res.resize(deg);\n return res;\n }\n inline friend FPS log(const FPS& f) {\n return log(f, f.size());\n }\n\n // exp(f), f[0] must be 0\n inline friend FPS exp(const FPS& f, int deg) {\n assert(f[0] == 0);\n FPS res(1, 1);\n for (int i = 1; i < deg; i <<= 1) {\n res = res * (f.pre(i<<1) - log(res, i<<1) + 1).pre(i<<1);\n }\n res.resize(deg);\n return res;\n }\n inline friend FPS exp(const FPS& f) {\n return exp(f, f.size());\n }\n\n // pow(f) = exp(e * log f)\n inline friend FPS pow(const FPS& f, long long e, int deg) {\n long long i = 0;\n while (i < (int)f.size() && f[i] == 0) ++i;\n if (i == (int)f.size()) return FPS(deg, 0);\n if (i * e >= deg) return FPS(deg, 0);\n mint k = f[i];\n FPS res = exp(log((f >> i) / k) * e) * modpow(k, e) << (e * i);\n res.resize(deg);\n return res;\n }\n inline friend FPS pow(const FPS& f, long long e) {\n return pow(f, e, f.size());\n }\n\n // sqrt(f), f[0] must be 1\n inline friend FPS sqrt(const FPS& f, int deg) {\n assert(f[0] == 1);\n int siz = 1;\n mint inv2 = mint(1) / 2;\n FPS res(1, 1);\n while (siz < deg) {\n siz <<= 1;\n FPS tmp(min(siz, (int)f.size()));\n for (int i = 0; i < (int)tmp.size(); ++i) tmp[i] = f[i];\n res += tmp * inv(f, siz);\n res.resize(siz);\n for (mint& x : res) res *= inv2;\n }\n return res;\n }\n inline friend FPS sqrt(const FPS& f) {\n return sqrt(f, f.size());\n }\n};\n\nusing mint = Fp<>;\n\nFPS<mint> modpow(const FPS<mint> &f, long long n, const FPS<mint> &m) {\n if (n == 0) return FPS<mint>(1, 1);\n auto t = modpow(f, n / 2, m);\n t = (t * t) % m;\n if (n & 1) t = (t * f) % m;\n auto q = t / m;\n auto r = t % m;\n return t;\n}\n\nint main() {\n long long N, P;\n while (cin >> N >> P) {\n if (N == 0) break;\n MODS[0] = P;\n\t\t\n FPS<mint> f(N+1);\n for (int i = 0; i < N+1; ++i) cin >> f[i];\n f.normalize();\n if (f.empty()) { \n cout << P << endl;\n continue; \n }\n \n FPS<mint> x(2, 0);\n x[1] = 1; // 多項式 x\n FPS<mint> g = modpow(x, P, f) - x;\n FPS<mint> res = gcd(f, g);\n cout << (int)res.size() - 1 << endl;\n }\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 3332, "score_of_the_acc": -1.0646, "final_rank": 8 }, { "submission_id": "aoj_2213_4895986", "code_snippet": "//\n// Formal Power Series (on runtime mod)\n//\n// verified:\n// Yosupo Judge\n// https://judge.yosupo.jp/problem/inv_of_formal_power_series\n//\n// AOJ 2213 The Number of Solutions for a Polynomial\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2213\n//\n\n\n#include <bits/stdc++.h>\nusing namespace std;\n\n#define COUT(x) cout << #x << \" = \" << (x) << \" (L\" << __LINE__ << \")\" << endl\ntemplate<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P)\n{ return s << '<' << P.first << \", \" << P.second << '>'; }\ntemplate<class T> ostream& operator << (ostream &s, vector<T> P)\n{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << \" \"; } s << P[i]; } return s; }\ntemplate<class T> ostream& operator << (ostream &s, vector<vector<T> > P)\n{ for (int i = 0; i < P.size(); ++i) { s << endl << P[i]; } return s << endl; }\ntemplate<class T> ostream& operator << (ostream &s, set<T> P)\n{ for(auto it : P) { s << \"<\" << it << \"> \"; } return s << endl; }\ntemplate<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P)\n{ for(auto it : P) { s << \"<\" << it.first << \"->\" << it.second << \"> \"; } return s << endl; }\n\n\n// modint (replace MODS[0] on runtime)\nvector<int> MODS = { 1000000007, 754974721, 167772161, 469762049 };\ntemplate<int IND = 0> struct Fp {\n long long val;\n constexpr Fp(long long v = 0) noexcept : val(v % MODS[IND]) {\n if (val < 0) val += MODS[IND];\n }\n constexpr int getmod() const { return MODS[IND]; }\n constexpr Fp operator - () const noexcept {\n return val ? MODS[IND] - val : 0;\n }\n constexpr Fp operator + (const Fp& r) const noexcept { return Fp(*this) += r; }\n constexpr Fp operator - (const Fp& r) const noexcept { return Fp(*this) -= r; }\n constexpr Fp operator * (const Fp& r) const noexcept { return Fp(*this) *= r; }\n constexpr Fp operator / (const Fp& r) const noexcept { return Fp(*this) /= r; }\n constexpr Fp& operator += (const Fp& r) noexcept {\n val += r.val;\n if (val >= MODS[IND]) val -= MODS[IND];\n return *this;\n }\n constexpr Fp& operator -= (const Fp& r) noexcept {\n val -= r.val;\n if (val < 0) val += MODS[IND];\n return *this;\n }\n constexpr Fp& operator *= (const Fp& r) noexcept {\n val = val * r.val % MODS[IND];\n return *this;\n }\n constexpr Fp& operator /= (const Fp& r) noexcept {\n long long a = r.val, b = MODS[IND], u = 1, v = 0;\n while (b) {\n long long t = a / b;\n a -= t * b; swap(a, b);\n u -= t * v; swap(u, v);\n }\n val = val * u % MODS[IND];\n if (val < 0) val += MODS[IND];\n return *this;\n }\n constexpr bool operator == (const Fp& r) const noexcept {\n return this->val == r.val;\n }\n constexpr bool operator != (const Fp& r) const noexcept {\n return this->val != r.val;\n }\n friend constexpr istream& operator >> (istream& is, Fp<IND>& x) noexcept {\n is >> x.val;\n x.val %= MODS[IND];\n if (x.val < 0) x.val += MODS[IND];\n return is;\n }\n friend constexpr ostream& operator << (ostream& os, const Fp<IND>& x) noexcept {\n return os << x.val;\n }\n friend constexpr Fp<IND> modpow(const Fp<IND>& r, long long n) noexcept {\n if (n == 0) return 1;\n auto t = modpow(r, n / 2);\n t = t * t;\n if (n & 1) t = t * r;\n return t;\n }\n friend constexpr Fp<IND> modinv(const Fp<IND>& r) noexcept {\n long long a = r.val, b = MODS[IND], u = 1, v = 0;\n while (b) {\n long long t = a / b;\n a -= t * b, swap(a, b);\n u -= t * v, swap(u, v);\n }\n return Fp<IND>(u);\n }\n};\n\nnamespace NTT { \n long long modpow(long long a, long long n, int mod) {\n long long res = 1;\n while (n > 0) {\n if (n & 1) res = res * a % mod;\n a = a * a % mod;\n n >>= 1;\n }\n return res;\n }\n\n long long modinv(long long a, int mod) {\n long long b = mod, u = 1, v = 0;\n while (b) {\n long long t = a / b;\n a -= t * b, swap(a, b);\n u -= t * v, swap(u, v);\n }\n u %= mod;\n if (u < 0) u += mod;\n return u;\n }\n\n int calc_primitive_root(int mod) {\n if (mod == 2) return 1;\n if (mod == 167772161) return 3;\n if (mod == 469762049) return 3;\n if (mod == 754974721) return 11;\n if (mod == 998244353) return 3;\n int divs[20] = {};\n divs[0] = 2;\n int cnt = 1;\n long long x = (mod - 1) / 2;\n while (x % 2 == 0) x /= 2;\n for (long long i = 3; i * i <= x; i += 2) {\n if (x % i == 0) {\n divs[cnt++] = i;\n while (x % i == 0) x /= i;\n }\n }\n if (x > 1) divs[cnt++] = x;\n for (int g = 2;; g++) {\n bool ok = true;\n for (int i = 0; i < cnt; i++) {\n if (modpow(g, (mod - 1) / divs[i], mod) == 1) {\n ok = false;\n break;\n }\n }\n if (ok) return g;\n }\n }\n\n int get_fft_size(int N, int M) {\n int size_a = 1, size_b = 1;\n while (size_a < N) size_a <<= 1;\n while (size_b < M) size_b <<= 1;\n return max(size_a, size_b) << 1;\n }\n\n // number-theoretic transform\n set<int> setmod;\n template<class mint> void trans(vector<mint>& v, bool inv = false) {\n if (v.empty()) return;\n int N = (int)v.size();\n int MOD = v[0].getmod();\n int PR = calc_primitive_root(MOD);\n static vector<long long> vbw(30), vibw(30);\n if (!setmod.count(MOD)) {\n setmod.insert(MOD);\n for (int k = 0; k < 30; ++k) {\n vbw[k] = modpow(PR, (MOD - 1) >> (k + 1), MOD);\n vibw[k] = modinv(vbw[k], MOD);\n }\n }\n for (int i = 0, j = 1; j < N - 1; j++) {\n for (int k = N >> 1; k > (i ^= k); k >>= 1);\n if (i > j) swap(v[i], v[j]);\n }\n for (int k = 0, t = 2; t <= N; ++k, t <<= 1) {\n long long bw = vbw[k];\n if (inv) bw = vibw[k];\n for (int i = 0; i < N; i += t) {\n mint w = 1;\n for (int j = 0; j < t/2; ++j) {\n int j1 = i + j, j2 = i + j + t/2;\n mint c1 = v[j1], c2 = v[j2] * w;\n v[j1] = c1 + c2;\n v[j2] = c1 - c2;\n w *= bw;\n }\n }\n }\n if (inv) {\n long long invN = modinv(N, MOD);\n for (int i = 0; i < N; ++i) v[i] = v[i] * invN;\n }\n }\n\n // for garner\n static constexpr int IND1 = 1; //754974721;\n static constexpr int IND2 = 2; //167772161;\n static constexpr int IND3 = 3; //469762049;\n using mint1 = Fp<IND1>;\n using mint2 = Fp<IND2>;\n using mint3 = Fp<IND3>;\n static const mint2 imod1 = 95869806; // modinv(MOD1, MOD2);\n static const mint3 imod2 = 104391568; // modinv(MOD2, MOD3);\n static const mint3 imod12 = 187290749; // imod2 / MOD1;\n\n // small case (T = mint, long long)\n template<class T> vector<T> naive_mul \n (const vector<T>& A, const vector<T>& B) {\n if (A.empty() || B.empty()) return {};\n int N = (int)A.size(), M = (int)B.size();\n vector<T> res(N + M - 1);\n for (int i = 0; i < N; ++i)\n for (int j = 0; j < M; ++j)\n res[i + j] += A[i] * B[j];\n return res;\n }\n\n // mint\n template<class mint> vector<mint> mul\n (const vector<mint>& A, const vector<mint>& B) {\n if (A.empty() || B.empty()) return {};\n int N = (int)A.size(), M = (int)B.size();\n if (min(N, M) < 80) return naive_mul(A, B);\n int MOD = A[0].getmod();\n int size_fft = get_fft_size(N, M);\n if (MOD == 998244353) {\n vector<mint> a(size_fft), b(size_fft), c(size_fft);\n for (int i = 0; i < N; ++i) a[i] = A[i];\n for (int i = 0; i < M; ++i) b[i] = B[i];\n trans(a), trans(b);\n vector<mint> res(size_fft);\n for (int i = 0; i < size_fft; ++i) res[i] = a[i] * b[i];\n trans(res, true);\n res.resize(N + M - 1);\n return res;\n }\n vector<mint1> a1(size_fft, 0), b1(size_fft, 0), c1(size_fft, 0);\n vector<mint2> a2(size_fft, 0), b2(size_fft, 0), c2(size_fft, 0);\n vector<mint3> a3(size_fft, 0), b3(size_fft, 0), c3(size_fft, 0);\n for (int i = 0; i < N; ++i)\n a1[i] = A[i].val, a2[i] = A[i].val, a3[i] = A[i].val;\n for (int i = 0; i < M; ++i)\n b1[i] = B[i].val, b2[i] = B[i].val, b3[i] = B[i].val;\n trans(a1), trans(a2), trans(a3), trans(b1), trans(b2), trans(b3);\n for (int i = 0; i < size_fft; ++i) {\n c1[i] = a1[i] * b1[i];\n c2[i] = a2[i] * b2[i];\n c3[i] = a3[i] * b3[i];\n }\n trans(c1, true), trans(c2, true), trans(c3, true);\n mint mod1 = MODS[IND1], mod12 = mod1 * MODS[IND2];\n vector<mint> res(N + M - 1);\n for (int i = 0; i < N + M - 1; ++i) {\n int y1 = c1[i].val;\n int y2 = (imod1 * (c2[i] - y1)).val;\n int y3 = (imod12 * (c3[i] - y1) - imod2 * y2).val;\n res[i] = mod12 * y3 + mod1 * y2 + y1;\n }\n return res;\n }\n};\n\n// Binomial Coefficient\ntemplate<class T> struct BiCoef {\n vector<T> fact_, inv_, finv_;\n constexpr BiCoef() {}\n constexpr BiCoef(int n) noexcept : fact_(n, 1), inv_(n, 1), finv_(n, 1) {\n init(n);\n }\n constexpr void init(int n) noexcept {\n fact_.assign(n, 1), inv_.assign(n, 1), finv_.assign(n, 1);\n int MOD = fact_[0].getmod();\n for(int i = 2; i < n; i++){\n fact_[i] = fact_[i-1] * i;\n inv_[i] = -inv_[MOD%i] * (MOD/i);\n finv_[i] = finv_[i-1] * inv_[i];\n }\n }\n constexpr T com(int n, int k) const noexcept {\n if (n < k || n < 0 || k < 0) return 0;\n return fact_[n] * finv_[k] * finv_[n-k];\n }\n constexpr T fact(int n) const noexcept {\n if (n < 0) return 0;\n return fact_[n];\n }\n constexpr T inv(int n) const noexcept {\n if (n < 0) return 0;\n return inv_[n];\n }\n constexpr T finv(int n) const noexcept {\n if (n < 0) return 0;\n return finv_[n];\n }\n};\n\n// Formal Power Series\ntemplate <typename mint> struct FPS : vector<mint> {\n using vector<mint>::vector;\n \n // constructor\n FPS(const vector<mint>& r) : vector<mint>(r) {}\n \n // core operator\n inline FPS pre(int siz) const {\n return FPS(begin(*this), begin(*this) + min((int)this->size(), siz));\n }\n inline FPS rev() const {\n FPS res = *this;\n reverse(begin(res), end(res));\n return res;\n }\n inline FPS& normalize() {\n while (!this->empty() && this->back() == 0) this->pop_back();\n return *this;\n }\n \n // basic operator\n inline FPS operator - () const noexcept {\n FPS res = (*this);\n for (int i = 0; i < (int)res.size(); ++i) res[i] = -res[i];\n return res;\n }\n inline FPS operator + (const mint& v) const { return FPS(*this) += v; }\n inline FPS operator + (const FPS& r) const { return FPS(*this) += r; }\n inline FPS operator - (const mint& v) const { return FPS(*this) -= v; }\n inline FPS operator - (const FPS& r) const { return FPS(*this) -= r; }\n inline FPS operator * (const mint& v) const { return FPS(*this) *= v; }\n inline FPS operator * (const FPS& r) const { return FPS(*this) *= r; }\n inline FPS operator / (const mint& v) const { return FPS(*this) /= v; }\n inline FPS operator / (const FPS& r) const { return FPS(*this) /= r; }\n inline FPS operator % (const FPS& r) const { return FPS(*this) %= r; }\n inline FPS operator << (int x) const { return FPS(*this) <<= x; }\n inline FPS operator >> (int x) const { return FPS(*this) >>= x; }\n inline FPS& operator += (const mint& v) {\n if (this->empty()) this->resize(1);\n (*this)[0] += v;\n return *this;\n }\n inline FPS& operator += (const FPS& r) {\n if (r.size() > this->size()) this->resize(r.size());\n for (int i = 0; i < (int)r.size(); ++i) (*this)[i] += r[i];\n return this->normalize();\n }\n inline FPS& operator -= (const mint& v) {\n if (this->empty()) this->resize(1);\n (*this)[0] -= v;\n return *this;\n }\n inline FPS& operator -= (const FPS& r) {\n if (r.size() > this->size()) this->resize(r.size());\n for (int i = 0; i < (int)r.size(); ++i) (*this)[i] -= r[i];\n return this->normalize();\n }\n inline FPS& operator *= (const mint& v) {\n for (int i = 0; i < (int)this->size(); ++i) (*this)[i] *= v;\n return *this;\n }\n inline FPS& operator *= (const FPS& r) {\n return *this = NTT::mul((*this), r);\n }\n inline FPS& operator /= (const mint& v) {\n assert(v != 0);\n mint iv = modinv(v);\n for (int i = 0; i < (int)this->size(); ++i) (*this)[i] *= iv;\n return *this;\n }\n inline FPS& operator /= (const FPS& r) {\n if (this->size() < r.size()) {\n this->clear();\n return *this;\n }\n int need = (int)this->size() - (int)r.size() + 1;\n *this = (this->rev().pre(need) * inv(r.rev(), need)).pre(need).rev();\n return *this;\n }\n inline FPS& operator %= (const FPS &r) {\n FPS q = (*this) / r;\n return *this -= q * r;\n }\n inline FPS& operator <<= (int x) {\n FPS res(x, 0);\n res.insert(res.end(), begin(*this), end(*this));\n return *this = res;\n }\n inline FPS& operator >>= (int x) {\n FPS res;\n res.insert(res.end(), begin(*this) + x, end(*this));\n return *this = res;\n }\n inline mint eval(const mint& v){\n mint res = 0;\n for (int i = (int)this->size()-1; i >= 0; --i) {\n res *= v;\n res += (*this)[i];\n }\n return res;\n }\n inline friend FPS gcd(const FPS& f, const FPS& g) {\n if (g.empty()) return f;\n return gcd(g, f % g);\n }\n\n // advanced operation\n // df/dx\n inline friend FPS diff(const FPS& f) {\n int n = (int)f.size();\n FPS res(n-1);\n for (int i = 1; i < n; ++i) res[i-1] = f[i] * i;\n return res;\n }\n\n // \\int f dx\n inline friend FPS integral(const FPS& f) {\n int n = (int)f.size();\n FPS res(n+1, 0);\n for (int i = 0; i < n; ++i) res[i+1] = f[i] / (i+1);\n return res;\n }\n\n // inv(f), f[0] must not be 0\n inline friend FPS inv(const FPS& f, int deg) {\n assert(f[0] != 0);\n if (deg < 0) deg = (int)f.size();\n FPS res({mint(1) / f[0]});\n for (int i = 1; i < deg; i <<= 1) {\n res = (res + res - res * res * f.pre(i << 1)).pre(i << 1);\n }\n return res.pre(deg);\n }\n inline friend FPS inv(const FPS& f) {\n return inv(f, f.size());\n }\n\n // log(f) = \\int f'/f dx, f[0] must be 1\n inline friend FPS log(const FPS& f, int deg) {\n assert(f[0] == 1);\n FPS res = integral(diff(f) * inv(f, deg));\n res.resize(deg);\n return res;\n }\n inline friend FPS log(const FPS& f) {\n return log(f, f.size());\n }\n\n // exp(f), f[0] must be 0\n inline friend FPS exp(const FPS& f, int deg) {\n assert(f[0] == 0);\n FPS res(1, 1);\n for (int i = 1; i < deg; i <<= 1) {\n res = res * (f.pre(i<<1) - log(res, i<<1) + 1).pre(i<<1);\n }\n res.resize(deg);\n return res;\n }\n inline friend FPS exp(const FPS& f) {\n return exp(f, f.size());\n }\n\n // pow(f) = exp(e * log f)\n inline friend FPS pow(const FPS& f, long long e, int deg) {\n long long i = 0;\n while (i < (int)f.size() && f[i] == 0) ++i;\n if (i == (int)f.size()) return FPS(deg, 0);\n if (i * e >= deg) return FPS(deg, 0);\n mint k = f[i];\n FPS res = exp(log((f >> i) / k) * e) * modpow(k, e) << (e * i);\n res.resize(deg);\n return res;\n }\n inline friend FPS pow(const FPS& f, long long e) {\n return pow(f, e, f.size());\n }\n\n // sqrt(f), f[0] must be 1\n inline friend FPS sqrt(const FPS& f, int deg) {\n assert(f[0] == 1);\n int siz = 1;\n mint inv2 = mint(1) / 2;\n FPS res(1, 1);\n while (siz < deg) {\n siz <<= 1;\n FPS tmp(min(siz, (int)f.size()));\n for (int i = 0; i < (int)tmp.size(); ++i) tmp[i] = f[i];\n res += tmp * inv(f, siz);\n res.resize(siz);\n for (mint& x : res) res *= inv2;\n }\n return res;\n }\n inline friend FPS sqrt(const FPS& f) {\n return sqrt(f, f.size());\n }\n};\nusing mint = Fp<>;\n\nFPS<mint> modpow(const FPS<mint> &f, long long n, const FPS<mint> &m) {\n if (n == 0) return FPS<mint>(1, 1);\n auto t = modpow(f, n / 2, m);\n t = (t * t) % m;\n if (n & 1) t = (t * f) % m;\n auto q = t / m;\n auto r = t % m;\n return t;\n}\n\nint main() {\n long long N, P;\n while (cin >> N >> P) {\n if (N == 0) break;\n MODS[0] = P;\n\t\t\n FPS<mint> f(N+1);\n for (int i = 0; i < N+1; ++i) cin >> f[i];\n f.normalize();\n if (f.empty()) { \n cout << P << endl;\n continue; \n }\n \n FPS<mint> x(2, 0);\n x[1] = 1; // 多項式 x\n FPS<mint> g = modpow(x, P, f) - x;\n FPS<mint> res = gcd(f, g);\n cout << (int)res.size() - 1 << endl;\n }\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 3500, "score_of_the_acc": -1.1365, "final_rank": 9 }, { "submission_id": "aoj_2213_4895385", "code_snippet": "//\n// Formal Power Series (on runtime mod)\n//\n// verified:\n// Yosupo Judge\n// https://judge.yosupo.jp/problem/inv_of_formal_power_series\n//\n// AOJ 2213 The Number of Solutions for a Polynomial\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2213\n//\n\n\n#include <bits/stdc++.h>\nusing namespace std;\n\n#define COUT(x) cout << #x << \" = \" << (x) << \" (L\" << __LINE__ << \")\" << endl\ntemplate<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P)\n{ return s << '<' << P.first << \", \" << P.second << '>'; }\ntemplate<class T> ostream& operator << (ostream &s, vector<T> P)\n{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << \" \"; } s << P[i]; } return s; }\ntemplate<class T> ostream& operator << (ostream &s, vector<vector<T> > P)\n{ for (int i = 0; i < P.size(); ++i) { s << endl << P[i]; } return s << endl; }\ntemplate<class T> ostream& operator << (ostream &s, set<T> P)\n{ for(auto it : P) { s << \"<\" << it << \"> \"; } return s << endl; }\ntemplate<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P)\n{ for(auto it : P) { s << \"<\" << it.first << \"->\" << it.second << \"> \"; } return s << endl; }\n\n\n// modint (replace MODS[0] on runtime)\nvector<int> MODS = { 1000000007, 754974721, 167772161, 469762049 };\ntemplate<int IND = 0> struct Fp {\n long long val;\n constexpr Fp(long long v = 0) noexcept : val(v % MODS[IND]) {\n if (val < 0) val += MODS[IND];\n }\n constexpr int getmod() const { return MODS[IND]; }\n constexpr Fp operator - () const noexcept {\n return val ? MODS[IND] - val : 0;\n }\n constexpr Fp operator + (const Fp& r) const noexcept { return Fp(*this) += r; }\n constexpr Fp operator - (const Fp& r) const noexcept { return Fp(*this) -= r; }\n constexpr Fp operator * (const Fp& r) const noexcept { return Fp(*this) *= r; }\n constexpr Fp operator / (const Fp& r) const noexcept { return Fp(*this) /= r; }\n constexpr Fp& operator += (const Fp& r) noexcept {\n val += r.val;\n if (val >= MODS[IND]) val -= MODS[IND];\n return *this;\n }\n constexpr Fp& operator -= (const Fp& r) noexcept {\n val -= r.val;\n if (val < 0) val += MODS[IND];\n return *this;\n }\n constexpr Fp& operator *= (const Fp& r) noexcept {\n val = val * r.val % MODS[IND];\n return *this;\n }\n constexpr Fp& operator /= (const Fp& r) noexcept {\n long long a = r.val, b = MODS[IND], u = 1, v = 0;\n while (b) {\n long long t = a / b;\n a -= t * b; swap(a, b);\n u -= t * v; swap(u, v);\n }\n val = val * u % MODS[IND];\n if (val < 0) val += MODS[IND];\n return *this;\n }\n constexpr bool operator == (const Fp& r) const noexcept {\n return this->val == r.val;\n }\n constexpr bool operator != (const Fp& r) const noexcept {\n return this->val != r.val;\n }\n friend constexpr istream& operator >> (istream& is, Fp<IND>& x) noexcept {\n is >> x.val;\n x.val %= MODS[IND];\n if (x.val < 0) x.val += MODS[IND];\n return is;\n }\n friend constexpr ostream& operator << (ostream& os, const Fp<IND>& x) noexcept {\n return os << x.val;\n }\n friend constexpr Fp<IND> modpow(const Fp<IND>& r, long long n) noexcept {\n if (n == 0) return 1;\n auto t = modpow(r, n / 2);\n t = t * t;\n if (n & 1) t = t * r;\n return t;\n }\n friend constexpr Fp<IND> modinv(const Fp<IND>& r) noexcept {\n long long a = r.val, b = MODS[IND], u = 1, v = 0;\n while (b) {\n long long t = a / b;\n a -= t * b, swap(a, b);\n u -= t * v, swap(u, v);\n }\n return Fp<IND>(u);\n }\n};\n\nnamespace NTT { \n long long modpow(long long a, long long n, int mod) {\n long long res = 1;\n while (n > 0) {\n if (n & 1) res = res * a % mod;\n a = a * a % mod;\n n >>= 1;\n }\n return res;\n }\n\n long long modinv(long long a, int mod) {\n long long b = mod, u = 1, v = 0;\n while (b) {\n long long t = a / b;\n a -= t * b, swap(a, b);\n u -= t * v, swap(u, v);\n }\n u %= mod;\n if (u < 0) u += mod;\n return u;\n }\n\n int calc_primitive_root(int mod) {\n if (mod == 2) return 1;\n if (mod == 167772161) return 3;\n if (mod == 469762049) return 3;\n if (mod == 754974721) return 11;\n if (mod == 998244353) return 3;\n int divs[20] = {};\n divs[0] = 2;\n int cnt = 1;\n long long x = (mod - 1) / 2;\n while (x % 2 == 0) x /= 2;\n for (long long i = 3; i * i <= x; i += 2) {\n if (x % i == 0) {\n divs[cnt++] = i;\n while (x % i == 0) x /= i;\n }\n }\n if (x > 1) divs[cnt++] = x;\n for (int g = 2;; g++) {\n bool ok = true;\n for (int i = 0; i < cnt; i++) {\n if (modpow(g, (mod - 1) / divs[i], mod) == 1) {\n ok = false;\n break;\n }\n }\n if (ok) return g;\n }\n }\n\n int get_fft_size(int N, int M) {\n int size_a = 1, size_b = 1;\n while (size_a < N) size_a <<= 1;\n while (size_b < M) size_b <<= 1;\n return max(size_a, size_b) << 1;\n }\n\n // number-theoretic transform\n set<int> setmod;\n template<class mint> void trans(vector<mint>& v, bool inv = false) {\n if (v.empty()) return;\n int N = (int)v.size();\n int MOD = v[0].getmod();\n int PR = calc_primitive_root(MOD);\n static vector<long long> vbw(30), vibw(30);\n if (!setmod.count(MOD)) {\n setmod.insert(MOD);\n for (int k = 0; k < 30; ++k) {\n vbw[k] = modpow(PR, (MOD - 1) >> (k + 1), MOD);\n vibw[k] = modinv(vbw[k], MOD);\n }\n }\n for (int i = 0, j = 1; j < N - 1; j++) {\n for (int k = N >> 1; k > (i ^= k); k >>= 1);\n if (i > j) swap(v[i], v[j]);\n }\n for (int k = 0, t = 2; t <= N; ++k, t <<= 1) {\n long long bw = vbw[k];\n if (inv) bw = vibw[k];\n for (int i = 0; i < N; i += t) {\n mint w = 1;\n for (int j = 0; j < t/2; ++j) {\n int j1 = i + j, j2 = i + j + t/2;\n mint c1 = v[j1], c2 = v[j2] * w;\n v[j1] = c1 + c2;\n v[j2] = c1 - c2;\n w *= bw;\n }\n }\n }\n if (inv) {\n long long invN = modinv(N, MOD);\n for (int i = 0; i < N; ++i) v[i] = v[i] * invN;\n }\n }\n\n // for garner\n static constexpr int IND1 = 1; //754974721;\n static constexpr int IND2 = 2; //167772161;\n static constexpr int IND3 = 3; //469762049;\n using mint1 = Fp<IND1>;\n using mint2 = Fp<IND2>;\n using mint3 = Fp<IND3>;\n static const mint2 imod1 = 95869806; // modinv(MOD1, MOD2);\n static const mint3 imod2 = 104391568; // modinv(MOD2, MOD3);\n static const mint3 imod12 = 187290749; // imod2 / MOD1;\n\n // small case (T = mint, long long)\n template<class T> vector<T> naive_mul \n (const vector<T>& A, const vector<T>& B) {\n if (A.empty() || B.empty()) return {};\n int N = (int)A.size(), M = (int)B.size();\n vector<T> res(N + M - 1);\n for (int i = 0; i < N; ++i)\n for (int j = 0; j < M; ++j)\n res[i + j] += A[i] * B[j];\n return res;\n }\n\n // mint\n template<class mint> vector<mint> mul\n (const vector<mint>& A, const vector<mint>& B) {\n if (A.empty() || B.empty()) return {};\n int N = (int)A.size(), M = (int)B.size();\n if (min(N, M) < 80) return naive_mul(A, B);\n int MOD = A[0].getmod();\n int size_fft = get_fft_size(N, M);\n if (MOD == 998244353) {\n vector<mint> a(size_fft), b(size_fft), c(size_fft);\n for (int i = 0; i < N; ++i) a[i] = A[i];\n for (int i = 0; i < M; ++i) b[i] = B[i];\n trans(a), trans(b);\n vector<mint> res(size_fft);\n for (int i = 0; i < size_fft; ++i) res[i] = a[i] * b[i];\n trans(res, true);\n res.resize(N + M - 1);\n return res;\n }\n vector<mint1> a1(size_fft, 0), b1(size_fft, 0), c1(size_fft, 0);\n vector<mint2> a2(size_fft, 0), b2(size_fft, 0), c2(size_fft, 0);\n vector<mint3> a3(size_fft, 0), b3(size_fft, 0), c3(size_fft, 0);\n for (int i = 0; i < N; ++i)\n a1[i] = A[i].val, a2[i] = A[i].val, a3[i] = A[i].val;\n for (int i = 0; i < M; ++i)\n b1[i] = B[i].val, b2[i] = B[i].val, b3[i] = B[i].val;\n trans(a1), trans(a2), trans(a3), trans(b1), trans(b2), trans(b3);\n for (int i = 0; i < size_fft; ++i) {\n c1[i] = a1[i] * b1[i];\n c2[i] = a2[i] * b2[i];\n c3[i] = a3[i] * b3[i];\n }\n trans(c1, true), trans(c2, true), trans(c3, true);\n mint mod1 = MODS[IND1], mod12 = mod1 * MODS[IND2];\n vector<mint> res(N + M - 1);\n for (int i = 0; i < N + M - 1; ++i) {\n int y1 = c1[i].val;\n int y2 = (imod1 * (c2[i] - y1)).val;\n int y3 = (imod12 * (c3[i] - y1) - imod2 * y2).val;\n res[i] = mod12 * y3 + mod1 * y2 + y1;\n }\n return res;\n }\n};\n\n// Binomial Coefficient\ntemplate<class T> struct BiCoef {\n vector<T> fact_, inv_, finv_;\n constexpr BiCoef() {}\n constexpr BiCoef(int n) noexcept : fact_(n, 1), inv_(n, 1), finv_(n, 1) {\n init(n);\n }\n constexpr void init(int n) noexcept {\n fact_.assign(n, 1), inv_.assign(n, 1), finv_.assign(n, 1);\n int MOD = fact_[0].getmod();\n for(int i = 2; i < n; i++){\n fact_[i] = fact_[i-1] * i;\n inv_[i] = -inv_[MOD%i] * (MOD/i);\n finv_[i] = finv_[i-1] * inv_[i];\n }\n }\n constexpr T com(int n, int k) const noexcept {\n if (n < k || n < 0 || k < 0) return 0;\n return fact_[n] * finv_[k] * finv_[n-k];\n }\n constexpr T fact(int n) const noexcept {\n if (n < 0) return 0;\n return fact_[n];\n }\n constexpr T inv(int n) const noexcept {\n if (n < 0) return 0;\n return inv_[n];\n }\n constexpr T finv(int n) const noexcept {\n if (n < 0) return 0;\n return finv_[n];\n }\n};\n\n// Formal Power Series\ntemplate <typename mint> struct FPS : vector<mint> {\n using vector<mint>::vector;\n \n // constructor\n FPS(const vector<mint>& r) : vector<mint>(r) {}\n \n // core operator\n inline FPS pre(int siz) const {\n return FPS(begin(*this), begin(*this) + min((int)this->size(), siz));\n }\n inline FPS rev() const {\n FPS res = *this;\n reverse(begin(res), end(res));\n return res;\n }\n inline FPS& normalize() {\n while (!this->empty() && this->back() == 0) this->pop_back();\n return *this;\n }\n \n // basic operator\n inline FPS operator - () const noexcept {\n FPS res = (*this);\n for (int i = 0; i < (int)res.size(); ++i) res[i] = -res[i];\n return res;\n }\n inline FPS operator + (const mint& v) const { return FPS(*this) += v; }\n inline FPS operator + (const FPS& r) const { return FPS(*this) += r; }\n inline FPS operator - (const mint& v) const { return FPS(*this) -= v; }\n inline FPS operator - (const FPS& r) const { return FPS(*this) -= r; }\n inline FPS operator * (const mint& v) const { return FPS(*this) *= v; }\n inline FPS operator * (const FPS& r) const { return FPS(*this) *= r; }\n inline FPS operator / (const mint& v) const { return FPS(*this) /= v; }\n inline FPS operator / (const FPS& r) const { return FPS(*this) /= r; }\n inline FPS operator % (const FPS& r) const { return FPS(*this) %= r; }\n inline FPS operator << (int x) const { return FPS(*this) <<= x; }\n inline FPS operator >> (int x) const { return FPS(*this) >>= x; }\n inline FPS& operator += (const mint& v) {\n if (this->empty()) this->resize(1);\n (*this)[0] += v;\n return *this;\n }\n inline FPS& operator += (const FPS& r) {\n if (r.size() > this->size()) this->resize(r.size());\n for (int i = 0; i < (int)r.size(); ++i) (*this)[i] += r[i];\n return this->normalize();\n }\n inline FPS& operator -= (const mint& v) {\n if (this->empty()) this->resize(1);\n (*this)[0] -= v;\n return *this;\n }\n inline FPS& operator -= (const FPS& r) {\n if (r.size() > this->size()) this->resize(r.size());\n for (int i = 0; i < (int)r.size(); ++i) (*this)[i] -= r[i];\n return this->normalize();\n }\n inline FPS& operator *= (const mint& v) {\n for (int i = 0; i < (int)this->size(); ++i) (*this)[i] *= v;\n return *this;\n }\n inline FPS& operator *= (const FPS& r) {\n return *this = NTT::mul((*this), r);\n }\n inline FPS& operator /= (const mint& v) {\n assert(v != 0);\n mint iv = modinv(v);\n for (int i = 0; i < (int)this->size(); ++i) (*this)[i] *= iv;\n return *this;\n }\n inline FPS& operator /= (const FPS& r) {\n if (this->size() < r.size()) {\n this->clear();\n return *this;\n }\n int need = (int)this->size() - (int)r.size() + 1;\n *this = (this->rev() * inv(r.rev(), need)).pre(need).rev();\n //*this = (this->rev().pre(need) * inv(r.rev(), need)).pre(need).rev();\n return *this;\n }\n inline FPS& operator %= (const FPS &r) {\n FPS q = (*this) / r;\n return *this -= q * r;\n }\n inline FPS& operator <<= (int x) {\n FPS res(x, 0);\n res.insert(res.end(), begin(*this), end(*this));\n return *this = res;\n }\n inline FPS& operator >>= (int x) {\n FPS res;\n res.insert(res.end(), begin(*this) + x, end(*this));\n return *this = res;\n }\n inline mint eval(const mint& v){\n mint res = 0;\n for (int i = (int)this->size()-1; i >= 0; --i) {\n res *= v;\n res += (*this)[i];\n }\n return res;\n }\n inline friend FPS gcd(const FPS& f, const FPS& g) {\n if (g.empty()) return f;\n return gcd(g, f % g);\n }\n\n // advanced operation\n // df/dx\n inline friend FPS diff(const FPS& f) {\n int n = (int)f.size();\n FPS res(n-1);\n for (int i = 1; i < n; ++i) res[i-1] = f[i] * i;\n return res;\n }\n\n // \\int f dx\n inline friend FPS integral(const FPS& f) {\n int n = (int)f.size();\n FPS res(n+1, 0);\n for (int i = 0; i < n; ++i) res[i+1] = f[i] / (i+1);\n return res;\n }\n\n // inv(f), f[0] must not be 0\n inline friend FPS inv(const FPS& f, int deg) {\n assert(f[0] != 0);\n if (deg < 0) deg = (int)f.size();\n FPS res({mint(1) / f[0]});\n for (int i = 1; i < deg; i <<= 1) {\n res = (res + res - res * res * f.pre(i << 1)).pre(i << 1);\n }\n return res.pre(deg);\n }\n inline friend FPS inv(const FPS& f) {\n return inv(f, f.size());\n }\n\n // log(f) = \\int f'/f dx, f[0] must be 1\n inline friend FPS log(const FPS& f, int deg) {\n assert(f[0] == 1);\n FPS res = integral(diff(f) * inv(f, deg));\n res.resize(deg);\n return res;\n }\n inline friend FPS log(const FPS& f) {\n return log(f, f.size());\n }\n\n // exp(f), f[0] must be 0\n inline friend FPS exp(const FPS& f, int deg) {\n assert(f[0] == 0);\n FPS res(1, 1);\n for (int i = 1; i < deg; i <<= 1) {\n res = res * (f.pre(i<<1) - log(res, i<<1) + 1).pre(i<<1);\n }\n res.resize(deg);\n return res;\n }\n inline friend FPS exp(const FPS& f) {\n return exp(f, f.size());\n }\n\n // pow(f) = exp(e * log f)\n inline friend FPS pow(const FPS& f, long long e, int deg) {\n long long i = 0;\n while (i < (int)f.size() && f[i] == 0) ++i;\n if (i == (int)f.size()) return FPS(deg, 0);\n if (i * e >= deg) return FPS(deg, 0);\n mint k = f[i];\n FPS res = exp(log((f >> i) / k) * e) * modpow(k, e) << (e * i);\n res.resize(deg);\n return res;\n }\n inline friend FPS pow(const FPS& f, long long e) {\n return pow(f, e, f.size());\n }\n\n // sqrt(f), f[0] must be 1\n inline friend FPS sqrt(const FPS& f, int deg) {\n assert(f[0] == 1);\n int siz = 1;\n mint inv2 = mint(1) / 2;\n FPS res(1, 1);\n while (siz < deg) {\n siz <<= 1;\n FPS tmp(min(siz, (int)f.size()));\n for (int i = 0; i < (int)tmp.size(); ++i) tmp[i] = f[i];\n res += tmp * inv(f, siz);\n res.resize(siz);\n for (mint& x : res) res *= inv2;\n }\n return res;\n }\n inline friend FPS sqrt(const FPS& f) {\n return sqrt(f, f.size());\n }\n};\nusing mint = Fp<>;\n\nFPS<mint> modpow(const FPS<mint> &f, long long n, const FPS<mint> &m) {\n if (n == 0) return FPS<mint>(1, 1);\n auto t = modpow(f, n / 2, m);\n t = (t * t) % m;\n if (n & 1) t = (t * f) % m;\n auto q = t / m;\n auto r = t % m;\n return t;\n}\n\nint main() {\n long long N, P;\n while (cin >> N >> P) {\n if (N == 0) break;\n MODS[0] = P;\n\t\t\n FPS<mint> f(N+1);\n for (int i = 0; i < N+1; ++i) cin >> f[i];\n f.normalize();\n if (f.empty()) { \n cout << P << endl;\n continue; \n }\n \n FPS<mint> x(2, 0);\n x[1] = 1; // 多項式 x\n FPS<mint> g = modpow(x, P, f) - x;\n FPS<mint> res = gcd(f, g);\n cout << (int)res.size() - 1 << endl;\n }\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 3616, "score_of_the_acc": -1.2316, "final_rank": 11 }, { "submission_id": "aoj_2213_4895368", "code_snippet": "//\n// Formal Power Series (on runtime mod)\n//\n// verified:\n// Yosupo Judge\n// https://judge.yosupo.jp/problem/inv_of_formal_power_series\n//\n// AOJ 2213 The Number of Solutions for a Polynomial\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2213\n//\n\n\n#include <bits/stdc++.h>\nusing namespace std;\n\n#define COUT(x) cout << #x << \" = \" << (x) << \" (L\" << __LINE__ << \")\" << endl\ntemplate<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P)\n{ return s << '<' << P.first << \", \" << P.second << '>'; }\ntemplate<class T> ostream& operator << (ostream &s, vector<T> P)\n{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << \" \"; } s << P[i]; } return s; }\ntemplate<class T> ostream& operator << (ostream &s, vector<vector<T> > P)\n{ for (int i = 0; i < P.size(); ++i) { s << endl << P[i]; } return s << endl; }\ntemplate<class T> ostream& operator << (ostream &s, set<T> P)\n{ for(auto it : P) { s << \"<\" << it << \"> \"; } return s << endl; }\ntemplate<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P)\n{ for(auto it : P) { s << \"<\" << it.first << \"->\" << it.second << \"> \"; } return s << endl; }\n\n\n// modint (replace MODS[0] on runtime)\nvector<int> MODS = { 1000000007, 754974721, 167772161, 469762049 };\ntemplate<int IND = 0> struct Fp {\n long long val;\n constexpr Fp(long long v = 0) noexcept : val(v % MODS[IND]) {\n if (val < 0) val += MODS[IND];\n }\n constexpr int getmod() const { return MODS[IND]; }\n constexpr Fp operator - () const noexcept {\n return val ? MODS[IND] - val : 0;\n }\n constexpr Fp operator + (const Fp& r) const noexcept { return Fp(*this) += r; }\n constexpr Fp operator - (const Fp& r) const noexcept { return Fp(*this) -= r; }\n constexpr Fp operator * (const Fp& r) const noexcept { return Fp(*this) *= r; }\n constexpr Fp operator / (const Fp& r) const noexcept { return Fp(*this) /= r; }\n constexpr Fp& operator += (const Fp& r) noexcept {\n val += r.val;\n if (val >= MODS[IND]) val -= MODS[IND];\n return *this;\n }\n constexpr Fp& operator -= (const Fp& r) noexcept {\n val -= r.val;\n if (val < 0) val += MODS[IND];\n return *this;\n }\n constexpr Fp& operator *= (const Fp& r) noexcept {\n val = val * r.val % MODS[IND];\n return *this;\n }\n constexpr Fp& operator /= (const Fp& r) noexcept {\n long long a = r.val, b = MODS[IND], u = 1, v = 0;\n while (b) {\n long long t = a / b;\n a -= t * b; swap(a, b);\n u -= t * v; swap(u, v);\n }\n val = val * u % MODS[IND];\n if (val < 0) val += MODS[IND];\n return *this;\n }\n constexpr bool operator == (const Fp& r) const noexcept {\n return this->val == r.val;\n }\n constexpr bool operator != (const Fp& r) const noexcept {\n return this->val != r.val;\n }\n friend constexpr istream& operator >> (istream& is, Fp<IND>& x) noexcept {\n is >> x.val;\n x.val %= MODS[IND];\n if (x.val < 0) x.val += MODS[IND];\n return is;\n }\n friend constexpr ostream& operator << (ostream& os, const Fp<IND>& x) noexcept {\n return os << x.val;\n }\n friend constexpr Fp<IND> modpow(const Fp<IND>& r, long long n) noexcept {\n if (n == 0) return 1;\n auto t = modpow(r, n / 2);\n t = t * t;\n if (n & 1) t = t * r;\n return t;\n }\n friend constexpr Fp<IND> modinv(const Fp<IND>& r) noexcept {\n long long a = r.val, b = MODS[IND], u = 1, v = 0;\n while (b) {\n long long t = a / b;\n a -= t * b, swap(a, b);\n u -= t * v, swap(u, v);\n }\n return Fp<IND>(u);\n }\n};\n\nnamespace NTT { \n long long modpow(long long a, long long n, int mod) {\n long long res = 1;\n while (n > 0) {\n if (n & 1) res = res * a % mod;\n a = a * a % mod;\n n >>= 1;\n }\n return res;\n }\n\n long long modinv(long long a, int mod) {\n long long b = mod, u = 1, v = 0;\n while (b) {\n long long t = a / b;\n a -= t * b, swap(a, b);\n u -= t * v, swap(u, v);\n }\n u %= mod;\n if (u < 0) u += mod;\n return u;\n }\n\n int calc_primitive_root(int mod) {\n if (mod == 2) return 1;\n if (mod == 167772161) return 3;\n if (mod == 469762049) return 3;\n if (mod == 754974721) return 11;\n if (mod == 998244353) return 3;\n int divs[20] = {};\n divs[0] = 2;\n int cnt = 1;\n long long x = (mod - 1) / 2;\n while (x % 2 == 0) x /= 2;\n for (long long i = 3; i * i <= x; i += 2) {\n if (x % i == 0) {\n divs[cnt++] = i;\n while (x % i == 0) x /= i;\n }\n }\n if (x > 1) divs[cnt++] = x;\n for (int g = 2;; g++) {\n bool ok = true;\n for (int i = 0; i < cnt; i++) {\n if (modpow(g, (mod - 1) / divs[i], mod) == 1) {\n ok = false;\n break;\n }\n }\n if (ok) return g;\n }\n }\n\n int get_fft_size(int N, int M) {\n int size_a = 1, size_b = 1;\n while (size_a < N) size_a <<= 1;\n while (size_b < M) size_b <<= 1;\n return max(size_a, size_b) << 1;\n }\n\n // number-theoretic transform\n set<int> setmod;\n template<class mint> void trans(vector<mint>& v, bool inv = false) {\n if (v.empty()) return;\n int N = (int)v.size();\n int MOD = v[0].getmod();\n int PR = calc_primitive_root(MOD);\n static vector<long long> vbw(30), vibw(30);\n if (!setmod.count(MOD)) {\n setmod.insert(MOD);\n for (int k = 0; k < 30; ++k) {\n vbw[k] = modpow(PR, (MOD - 1) >> (k + 1), MOD);\n vibw[k] = modinv(vbw[k], MOD);\n }\n }\n for (int i = 0, j = 1; j < N - 1; j++) {\n for (int k = N >> 1; k > (i ^= k); k >>= 1);\n if (i > j) swap(v[i], v[j]);\n }\n for (int k = 0, t = 2; t <= N; ++k, t <<= 1) {\n long long bw = vbw[k];\n if (inv) bw = vibw[k];\n for (int i = 0; i < N; i += t) {\n mint w = 1;\n for (int j = 0; j < t/2; ++j) {\n int j1 = i + j, j2 = i + j + t/2;\n mint c1 = v[j1], c2 = v[j2] * w;\n v[j1] = c1 + c2;\n v[j2] = c1 - c2;\n w *= bw;\n }\n }\n }\n if (inv) {\n long long invN = modinv(N, MOD);\n for (int i = 0; i < N; ++i) v[i] = v[i] * invN;\n }\n }\n\n // for garner\n static constexpr int IND1 = 1; //754974721;\n static constexpr int IND2 = 2; //167772161;\n static constexpr int IND3 = 3; //469762049;\n using mint1 = Fp<IND1>;\n using mint2 = Fp<IND2>;\n using mint3 = Fp<IND3>;\n static const mint2 imod1 = 95869806; // modinv(MOD1, MOD2);\n static const mint3 imod2 = 104391568; // modinv(MOD2, MOD3);\n static const mint3 imod12 = 187290749; // imod2 / MOD1;\n\n // small case (T = mint, long long)\n template<class T> vector<T> naive_mul \n (const vector<T>& A, const vector<T>& B) {\n if (A.empty() || B.empty()) return {};\n int N = (int)A.size(), M = (int)B.size();\n vector<T> res(N + M - 1);\n for (int i = 0; i < N; ++i)\n for (int j = 0; j < M; ++j)\n res[i + j] += A[i] * B[j];\n return res;\n }\n\n // mint\n template<class mint> vector<mint> mul\n (const vector<mint>& A, const vector<mint>& B) {\n if (A.empty() || B.empty()) return {};\n int N = (int)A.size(), M = (int)B.size();\n if (min(N, M) < 50) return naive_mul(A, B);\n int MOD = A[0].getmod();\n int size_fft = get_fft_size(N, M);\n if (MOD == 998244353) {\n vector<mint> a(size_fft), b(size_fft), c(size_fft);\n for (int i = 0; i < N; ++i) a[i] = A[i];\n for (int i = 0; i < M; ++i) b[i] = B[i];\n trans(a), trans(b);\n vector<mint> res(size_fft);\n for (int i = 0; i < size_fft; ++i) res[i] = a[i] * b[i];\n trans(res, true);\n res.resize(N + M - 1);\n return res;\n }\n vector<mint1> a1(size_fft, 0), b1(size_fft, 0), c1(size_fft, 0);\n vector<mint2> a2(size_fft, 0), b2(size_fft, 0), c2(size_fft, 0);\n vector<mint3> a3(size_fft, 0), b3(size_fft, 0), c3(size_fft, 0);\n for (int i = 0; i < N; ++i)\n a1[i] = A[i].val, a2[i] = A[i].val, a3[i] = A[i].val;\n for (int i = 0; i < M; ++i)\n b1[i] = B[i].val, b2[i] = B[i].val, b3[i] = B[i].val;\n trans(a1), trans(a2), trans(a3), trans(b1), trans(b2), trans(b3);\n for (int i = 0; i < size_fft; ++i) {\n c1[i] = a1[i] * b1[i];\n c2[i] = a2[i] * b2[i];\n c3[i] = a3[i] * b3[i];\n }\n trans(c1, true), trans(c2, true), trans(c3, true);\n mint mod1 = MODS[IND1], mod12 = mod1 * MODS[IND2];\n vector<mint> res(N + M - 1);\n for (int i = 0; i < N + M - 1; ++i) {\n int y1 = c1[i].val;\n int y2 = (imod1 * (c2[i] - y1)).val;\n int y3 = (imod12 * (c3[i] - y1) - imod2 * y2).val;\n res[i] = mod12 * y3 + mod1 * y2 + y1;\n }\n return res;\n }\n};\n\n// Binomial Coefficient\ntemplate<class T> struct BiCoef {\n vector<T> fact_, inv_, finv_;\n constexpr BiCoef() {}\n constexpr BiCoef(int n) noexcept : fact_(n, 1), inv_(n, 1), finv_(n, 1) {\n init(n);\n }\n constexpr void init(int n) noexcept {\n fact_.assign(n, 1), inv_.assign(n, 1), finv_.assign(n, 1);\n int MOD = fact_[0].getmod();\n for(int i = 2; i < n; i++){\n fact_[i] = fact_[i-1] * i;\n inv_[i] = -inv_[MOD%i] * (MOD/i);\n finv_[i] = finv_[i-1] * inv_[i];\n }\n }\n constexpr T com(int n, int k) const noexcept {\n if (n < k || n < 0 || k < 0) return 0;\n return fact_[n] * finv_[k] * finv_[n-k];\n }\n constexpr T fact(int n) const noexcept {\n if (n < 0) return 0;\n return fact_[n];\n }\n constexpr T inv(int n) const noexcept {\n if (n < 0) return 0;\n return inv_[n];\n }\n constexpr T finv(int n) const noexcept {\n if (n < 0) return 0;\n return finv_[n];\n }\n};\n\n// Formal Power Series\ntemplate <typename mint> struct FPS : vector<mint> {\n using vector<mint>::vector;\n \n // constructor\n FPS(const vector<mint>& r) : vector<mint>(r) {}\n \n // core operator\n inline FPS pre(int siz) const {\n return FPS(begin(*this), begin(*this) + min((int)this->size(), siz));\n }\n inline FPS rev() const {\n FPS res = *this;\n reverse(begin(res), end(res));\n return res;\n }\n inline FPS& normalize() {\n while (!this->empty() && this->back() == 0) this->pop_back();\n return *this;\n }\n \n // basic operator\n inline FPS operator - () const noexcept {\n FPS res = (*this);\n for (int i = 0; i < (int)res.size(); ++i) res[i] = -res[i];\n return res;\n }\n inline FPS operator + (const mint& v) const { return FPS(*this) += v; }\n inline FPS operator + (const FPS& r) const { return FPS(*this) += r; }\n inline FPS operator - (const mint& v) const { return FPS(*this) -= v; }\n inline FPS operator - (const FPS& r) const { return FPS(*this) -= r; }\n inline FPS operator * (const mint& v) const { return FPS(*this) *= v; }\n inline FPS operator * (const FPS& r) const { return FPS(*this) *= r; }\n inline FPS operator / (const mint& v) const { return FPS(*this) /= v; }\n inline FPS operator / (const FPS& r) const { return FPS(*this) /= r; }\n inline FPS operator % (const FPS& r) const { return FPS(*this) %= r; }\n inline FPS operator << (int x) const { return FPS(*this) <<= x; }\n inline FPS operator >> (int x) const { return FPS(*this) >>= x; }\n inline FPS& operator += (const mint& v) {\n if (this->empty()) this->resize(1);\n (*this)[0] += v;\n return *this;\n }\n inline FPS& operator += (const FPS& r) {\n if (r.size() > this->size()) this->resize(r.size());\n for (int i = 0; i < (int)r.size(); ++i) (*this)[i] += r[i];\n return this->normalize();\n }\n inline FPS& operator -= (const mint& v) {\n if (this->empty()) this->resize(1);\n (*this)[0] -= v;\n return *this;\n }\n inline FPS& operator -= (const FPS& r) {\n if (r.size() > this->size()) this->resize(r.size());\n for (int i = 0; i < (int)r.size(); ++i) (*this)[i] -= r[i];\n return this->normalize();\n }\n inline FPS& operator *= (const mint& v) {\n for (int i = 0; i < (int)this->size(); ++i) (*this)[i] *= v;\n return *this;\n }\n inline FPS& operator *= (const FPS& r) {\n return *this = NTT::mul((*this), r);\n }\n inline FPS& operator /= (const mint& v) {\n assert(v != 0);\n mint iv = modinv(v);\n for (int i = 0; i < (int)this->size(); ++i) (*this)[i] *= iv;\n return *this;\n }\n inline FPS& operator /= (const FPS& r) {\n if (this->size() < r.size()) {\n this->clear();\n return *this;\n }\n int need = (int)this->size() - (int)r.size() + 1;\n *this = (this->rev() * inv(r.rev(), need)).pre(need).rev();\n //*this = (this->rev().pre(need) * inv(r.rev(), need)).pre(need).rev();\n return *this;\n }\n inline FPS& operator %= (const FPS &r) {\n FPS q = (*this) / r;\n return *this -= q * r;\n }\n inline FPS& operator <<= (int x) {\n FPS res(x, 0);\n res.insert(res.end(), begin(*this), end(*this));\n return *this = res;\n }\n inline FPS& operator >>= (int x) {\n FPS res;\n res.insert(res.end(), begin(*this) + x, end(*this));\n return *this = res;\n }\n inline mint eval(const mint& v){\n mint res = 0;\n for (int i = (int)this->size()-1; i >= 0; --i) {\n res *= v;\n res += (*this)[i];\n }\n return res;\n }\n inline friend FPS gcd(const FPS& f, const FPS& g) {\n if (g.empty()) return f;\n return gcd(g, f % g);\n }\n\n // advanced operation\n // df/dx\n inline friend FPS diff(const FPS& f) {\n int n = (int)f.size();\n FPS res(n-1);\n for (int i = 1; i < n; ++i) res[i-1] = f[i] * i;\n return res;\n }\n\n // \\int f dx\n inline friend FPS integral(const FPS& f) {\n int n = (int)f.size();\n FPS res(n+1, 0);\n for (int i = 0; i < n; ++i) res[i+1] = f[i] / (i+1);\n return res;\n }\n\n // inv(f), f[0] must not be 0\n inline friend FPS inv(const FPS& f, int deg) {\n assert(f[0] != 0);\n if (deg < 0) deg = (int)f.size();\n FPS res({mint(1) / f[0]});\n for (int i = 1; i < deg; i <<= 1) {\n res = (res + res - res * res * f.pre(i << 1)).pre(i << 1);\n }\n return res.pre(deg);\n }\n inline friend FPS inv(const FPS& f) {\n return inv(f, f.size());\n }\n\n // log(f) = \\int f'/f dx, f[0] must be 1\n inline friend FPS log(const FPS& f, int deg) {\n assert(f[0] == 1);\n FPS res = integral(diff(f) * inv(f, deg));\n res.resize(deg);\n return res;\n }\n inline friend FPS log(const FPS& f) {\n return log(f, f.size());\n }\n\n // exp(f), f[0] must be 0\n inline friend FPS exp(const FPS& f, int deg) {\n assert(f[0] == 0);\n FPS res(1, 1);\n for (int i = 1; i < deg; i <<= 1) {\n res = res * (f.pre(i<<1) - log(res, i<<1) + 1).pre(i<<1);\n }\n res.resize(deg);\n return res;\n }\n inline friend FPS exp(const FPS& f) {\n return exp(f, f.size());\n }\n\n // pow(f) = exp(e * log f)\n inline friend FPS pow(const FPS& f, long long e, int deg) {\n long long i = 0;\n while (i < (int)f.size() && f[i] == 0) ++i;\n if (i == (int)f.size()) return FPS(deg, 0);\n if (i * e >= deg) return FPS(deg, 0);\n mint k = f[i];\n FPS res = exp(log((f >> i) / k) * e) * modpow(k, e) << (e * i);\n res.resize(deg);\n return res;\n }\n inline friend FPS pow(const FPS& f, long long e) {\n return pow(f, e, f.size());\n }\n\n // sqrt(f), f[0] must be 1\n inline friend FPS sqrt(const FPS& f, int deg) {\n assert(f[0] == 1);\n int siz = 1;\n mint inv2 = mint(1) / 2;\n FPS res(1, 1);\n while (siz < deg) {\n siz <<= 1;\n FPS tmp(min(siz, (int)f.size()));\n for (int i = 0; i < (int)tmp.size(); ++i) tmp[i] = f[i];\n res += tmp * inv(f, siz);\n res.resize(siz);\n for (mint& x : res) res *= inv2;\n }\n return res;\n }\n inline friend FPS sqrt(const FPS& f) {\n return sqrt(f, f.size());\n }\n};\nusing mint = Fp<>;\n\nFPS<mint> modpow(const FPS<mint> &f, long long n, const FPS<mint> &m) {\n if (n == 0) return FPS<mint>(1, 1);\n auto t = modpow(f, n / 2, m);\n t = (t * t) % m;\n if (n & 1) t = (t * f) % m;\n auto q = t / m;\n auto r = t % m;\n return t;\n}\n\nint main() {\n long long N, P;\n while (cin >> N >> P) {\n if (N == 0) break;\n MODS[0] = P;\n\t\t\n FPS<mint> f(N+1);\n for (int i = 0; i < N+1; ++i) cin >> f[i];\n f.normalize();\n if (f.empty()) { \n cout << P << endl;\n continue; \n }\n \n FPS<mint> x(2, 0);\n x[1] = 1; // 多項式 x\n FPS<mint> g = modpow(x, P, f) - x;\n FPS<mint> res = gcd(f, g);\n cout << (int)res.size() - 1 << endl;\n }\n}", "accuracy": 1, "time_ms": 90, "memory_kb": 3488, "score_of_the_acc": -1.2677, "final_rank": 12 }, { "submission_id": "aoj_2213_4895360", "code_snippet": "//\n// Formal Power Series (on runtime mod)\n//\n// verified:\n// Yosupo Judge\n// https://judge.yosupo.jp/problem/inv_of_formal_power_series\n//\n// AOJ 2213 The Number of Solutions for a Polynomial\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2213\n//\n\n\n#include <bits/stdc++.h>\nusing namespace std;\n\n#define COUT(x) cout << #x << \" = \" << (x) << \" (L\" << __LINE__ << \")\" << endl\ntemplate<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P)\n{ return s << '<' << P.first << \", \" << P.second << '>'; }\ntemplate<class T> ostream& operator << (ostream &s, vector<T> P)\n{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << \" \"; } s << P[i]; } return s; }\ntemplate<class T> ostream& operator << (ostream &s, vector<vector<T> > P)\n{ for (int i = 0; i < P.size(); ++i) { s << endl << P[i]; } return s << endl; }\ntemplate<class T> ostream& operator << (ostream &s, set<T> P)\n{ for(auto it : P) { s << \"<\" << it << \"> \"; } return s << endl; }\ntemplate<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P)\n{ for(auto it : P) { s << \"<\" << it.first << \"->\" << it.second << \"> \"; } return s << endl; }\n\n\n// modint (replace MODS[0] on runtime)\nvector<int> MODS = { 1000000007, 754974721, 167772161, 469762049 };\ntemplate<int IND = 0> struct Fp {\n long long val;\n constexpr Fp(long long v = 0) noexcept : val(v % MODS[IND]) {\n if (val < 0) val += MODS[IND];\n }\n constexpr int getmod() const { return MODS[IND]; }\n constexpr Fp operator - () const noexcept {\n return val ? MODS[IND] - val : 0;\n }\n constexpr Fp operator + (const Fp& r) const noexcept { return Fp(*this) += r; }\n constexpr Fp operator - (const Fp& r) const noexcept { return Fp(*this) -= r; }\n constexpr Fp operator * (const Fp& r) const noexcept { return Fp(*this) *= r; }\n constexpr Fp operator / (const Fp& r) const noexcept { return Fp(*this) /= r; }\n constexpr Fp& operator += (const Fp& r) noexcept {\n val += r.val;\n if (val >= MODS[IND]) val -= MODS[IND];\n return *this;\n }\n constexpr Fp& operator -= (const Fp& r) noexcept {\n val -= r.val;\n if (val < 0) val += MODS[IND];\n return *this;\n }\n constexpr Fp& operator *= (const Fp& r) noexcept {\n val = val * r.val % MODS[IND];\n return *this;\n }\n constexpr Fp& operator /= (const Fp& r) noexcept {\n long long a = r.val, b = MODS[IND], u = 1, v = 0;\n while (b) {\n long long t = a / b;\n a -= t * b; swap(a, b);\n u -= t * v; swap(u, v);\n }\n val = val * u % MODS[IND];\n if (val < 0) val += MODS[IND];\n return *this;\n }\n constexpr bool operator == (const Fp& r) const noexcept {\n return this->val == r.val;\n }\n constexpr bool operator != (const Fp& r) const noexcept {\n return this->val != r.val;\n }\n friend constexpr istream& operator >> (istream& is, Fp<IND>& x) noexcept {\n is >> x.val;\n x.val %= MODS[IND];\n if (x.val < 0) x.val += MODS[IND];\n return is;\n }\n friend constexpr ostream& operator << (ostream& os, const Fp<IND>& x) noexcept {\n return os << x.val;\n }\n friend constexpr Fp<IND> modpow(const Fp<IND>& r, long long n) noexcept {\n if (n == 0) return 1;\n auto t = modpow(r, n / 2);\n t = t * t;\n if (n & 1) t = t * r;\n return t;\n }\n friend constexpr Fp<IND> modinv(const Fp<IND>& r) noexcept {\n long long a = r.val, b = MODS[IND], u = 1, v = 0;\n while (b) {\n long long t = a / b;\n a -= t * b, swap(a, b);\n u -= t * v, swap(u, v);\n }\n return Fp<IND>(u);\n }\n};\n\nnamespace NTT { \n long long modpow(long long a, long long n, int mod) {\n long long res = 1;\n while (n > 0) {\n if (n & 1) res = res * a % mod;\n a = a * a % mod;\n n >>= 1;\n }\n return res;\n }\n\n long long modinv(long long a, int mod) {\n long long b = mod, u = 1, v = 0;\n while (b) {\n long long t = a / b;\n a -= t * b, swap(a, b);\n u -= t * v, swap(u, v);\n }\n u %= mod;\n if (u < 0) u += mod;\n return u;\n }\n\n int calc_primitive_root(int mod) {\n if (mod == 2) return 1;\n if (mod == 167772161) return 3;\n if (mod == 469762049) return 3;\n if (mod == 754974721) return 11;\n if (mod == 998244353) return 3;\n int divs[20] = {};\n divs[0] = 2;\n int cnt = 1;\n long long x = (mod - 1) / 2;\n while (x % 2 == 0) x /= 2;\n for (long long i = 3; i * i <= x; i += 2) {\n if (x % i == 0) {\n divs[cnt++] = i;\n while (x % i == 0) x /= i;\n }\n }\n if (x > 1) divs[cnt++] = x;\n for (int g = 2;; g++) {\n bool ok = true;\n for (int i = 0; i < cnt; i++) {\n if (modpow(g, (mod - 1) / divs[i], mod) == 1) {\n ok = false;\n break;\n }\n }\n if (ok) return g;\n }\n }\n\n int get_fft_size(int N, int M) {\n int size_a = 1, size_b = 1;\n while (size_a < N) size_a <<= 1;\n while (size_b < M) size_b <<= 1;\n return max(size_a, size_b) << 1;\n }\n\n // number-theoretic transform\n set<int> setmod;\n template<class mint> void trans(vector<mint>& v, bool inv = false) {\n if (v.empty()) return;\n int N = (int)v.size();\n int MOD = v[0].getmod();\n int PR = calc_primitive_root(MOD);\n static vector<long long> vbw(30), vibw(30);\n if (!setmod.count(MOD)) {\n setmod.insert(MOD);\n for (int k = 0; k < 30; ++k) {\n vbw[k] = modpow(PR, (MOD - 1) >> (k + 1), MOD);\n vibw[k] = modinv(vbw[k], MOD);\n }\n }\n for (int i = 0, j = 1; j < N - 1; j++) {\n for (int k = N >> 1; k > (i ^= k); k >>= 1);\n if (i > j) swap(v[i], v[j]);\n }\n for (int k = 0, t = 2; t <= N; ++k, t <<= 1) {\n long long bw = vbw[k];\n if (inv) bw = vibw[k];\n for (int i = 0; i < N; i += t) {\n mint w = 1;\n for (int j = 0; j < t/2; ++j) {\n int j1 = i + j, j2 = i + j + t/2;\n mint c1 = v[j1], c2 = v[j2] * w;\n v[j1] = c1 + c2;\n v[j2] = c1 - c2;\n w *= bw;\n }\n }\n }\n if (inv) {\n long long invN = modinv(N, MOD);\n for (int i = 0; i < N; ++i) v[i] = v[i] * invN;\n }\n }\n\n // for garner\n static constexpr int IND1 = 1; //754974721;\n static constexpr int IND2 = 2; //167772161;\n static constexpr int IND3 = 3; //469762049;\n using mint1 = Fp<IND1>;\n using mint2 = Fp<IND2>;\n using mint3 = Fp<IND3>;\n static const mint2 imod1 = 95869806; // modinv(MOD1, MOD2);\n static const mint3 imod2 = 104391568; // modinv(MOD2, MOD3);\n static const mint3 imod12 = 187290749; // imod2 / MOD1;\n\n // small case (T = mint, long long)\n template<class T> vector<T> naive_mul \n (const vector<T>& A, const vector<T>& B) {\n if (A.empty() || B.empty()) return {};\n int N = (int)A.size(), M = (int)B.size();\n vector<T> res(N + M - 1);\n for (int i = 0; i < N; ++i)\n for (int j = 0; j < M; ++j)\n res[i + j] += A[i] * B[j];\n return res;\n }\n\n // mint\n template<class mint> vector<mint> mul\n (const vector<mint>& A, const vector<mint>& B) {\n if (A.empty() || B.empty()) return {};\n int N = (int)A.size(), M = (int)B.size();\n if (min(N, M) < 2) return naive_mul(A, B);\n int MOD = A[0].getmod();\n int size_fft = get_fft_size(N, M);\n if (MOD == 998244353) {\n vector<mint> a(size_fft), b(size_fft), c(size_fft);\n for (int i = 0; i < N; ++i) a[i] = A[i];\n for (int i = 0; i < M; ++i) b[i] = B[i];\n trans(a), trans(b);\n vector<mint> res(size_fft);\n for (int i = 0; i < size_fft; ++i) res[i] = a[i] * b[i];\n trans(res, true);\n res.resize(N + M - 1);\n return res;\n }\n vector<mint1> a1(size_fft, 0), b1(size_fft, 0), c1(size_fft, 0);\n vector<mint2> a2(size_fft, 0), b2(size_fft, 0), c2(size_fft, 0);\n vector<mint3> a3(size_fft, 0), b3(size_fft, 0), c3(size_fft, 0);\n for (int i = 0; i < N; ++i)\n a1[i] = A[i].val, a2[i] = A[i].val, a3[i] = A[i].val;\n for (int i = 0; i < M; ++i)\n b1[i] = B[i].val, b2[i] = B[i].val, b3[i] = B[i].val;\n trans(a1), trans(a2), trans(a3), trans(b1), trans(b2), trans(b3);\n for (int i = 0; i < size_fft; ++i) {\n c1[i] = a1[i] * b1[i];\n c2[i] = a2[i] * b2[i];\n c3[i] = a3[i] * b3[i];\n }\n trans(c1, true), trans(c2, true), trans(c3, true);\n mint mod1 = MODS[IND1], mod12 = mod1 * MODS[IND2];\n vector<mint> res(N + M - 1);\n for (int i = 0; i < N + M - 1; ++i) {\n int y1 = c1[i].val;\n int y2 = (imod1 * (c2[i] - y1)).val;\n int y3 = (imod12 * (c3[i] - y1) - imod2 * y2).val;\n res[i] = mod12 * y3 + mod1 * y2 + y1;\n }\n return res;\n }\n};\n\n// Binomial Coefficient\ntemplate<class T> struct BiCoef {\n vector<T> fact_, inv_, finv_;\n constexpr BiCoef() {}\n constexpr BiCoef(int n) noexcept : fact_(n, 1), inv_(n, 1), finv_(n, 1) {\n init(n);\n }\n constexpr void init(int n) noexcept {\n fact_.assign(n, 1), inv_.assign(n, 1), finv_.assign(n, 1);\n int MOD = fact_[0].getmod();\n for(int i = 2; i < n; i++){\n fact_[i] = fact_[i-1] * i;\n inv_[i] = -inv_[MOD%i] * (MOD/i);\n finv_[i] = finv_[i-1] * inv_[i];\n }\n }\n constexpr T com(int n, int k) const noexcept {\n if (n < k || n < 0 || k < 0) return 0;\n return fact_[n] * finv_[k] * finv_[n-k];\n }\n constexpr T fact(int n) const noexcept {\n if (n < 0) return 0;\n return fact_[n];\n }\n constexpr T inv(int n) const noexcept {\n if (n < 0) return 0;\n return inv_[n];\n }\n constexpr T finv(int n) const noexcept {\n if (n < 0) return 0;\n return finv_[n];\n }\n};\n\n// Formal Power Series\ntemplate <typename mint> struct FPS : vector<mint> {\n using vector<mint>::vector;\n \n // constructor\n FPS(const vector<mint>& r) : vector<mint>(r) {}\n \n // core operator\n inline FPS pre(int siz) const {\n return FPS(begin(*this), begin(*this) + min((int)this->size(), siz));\n }\n inline FPS rev() const {\n FPS res = *this;\n reverse(begin(res), end(res));\n return res;\n }\n inline FPS& normalize() {\n while (!this->empty() && this->back() == 0) this->pop_back();\n return *this;\n }\n \n // basic operator\n inline FPS operator - () const noexcept {\n FPS res = (*this);\n for (int i = 0; i < (int)res.size(); ++i) res[i] = -res[i];\n return res;\n }\n inline FPS operator + (const mint& v) const { return FPS(*this) += v; }\n inline FPS operator + (const FPS& r) const { return FPS(*this) += r; }\n inline FPS operator - (const mint& v) const { return FPS(*this) -= v; }\n inline FPS operator - (const FPS& r) const { return FPS(*this) -= r; }\n inline FPS operator * (const mint& v) const { return FPS(*this) *= v; }\n inline FPS operator * (const FPS& r) const { return FPS(*this) *= r; }\n inline FPS operator / (const mint& v) const { return FPS(*this) /= v; }\n inline FPS operator / (const FPS& r) const { return FPS(*this) /= r; }\n inline FPS operator % (const FPS& r) const { return FPS(*this) %= r; }\n inline FPS operator << (int x) const { return FPS(*this) <<= x; }\n inline FPS operator >> (int x) const { return FPS(*this) >>= x; }\n inline FPS& operator += (const mint& v) {\n if (this->empty()) this->resize(1);\n (*this)[0] += v;\n return *this;\n }\n inline FPS& operator += (const FPS& r) {\n if (r.size() > this->size()) this->resize(r.size());\n for (int i = 0; i < (int)r.size(); ++i) (*this)[i] += r[i];\n return this->normalize();\n }\n inline FPS& operator -= (const mint& v) {\n if (this->empty()) this->resize(1);\n (*this)[0] -= v;\n return *this;\n }\n inline FPS& operator -= (const FPS& r) {\n if (r.size() > this->size()) this->resize(r.size());\n for (int i = 0; i < (int)r.size(); ++i) (*this)[i] -= r[i];\n return this->normalize();\n }\n inline FPS& operator *= (const mint& v) {\n for (int i = 0; i < (int)this->size(); ++i) (*this)[i] *= v;\n return *this;\n }\n inline FPS& operator *= (const FPS& r) {\n return *this = NTT::mul((*this), r);\n }\n inline FPS& operator /= (const mint& v) {\n assert(v != 0);\n mint iv = modinv(v);\n for (int i = 0; i < (int)this->size(); ++i) (*this)[i] *= iv;\n return *this;\n }\n inline FPS& operator /= (const FPS& r) {\n if (this->size() < r.size()) {\n this->clear();\n return *this;\n }\n int need = (int)this->size() - (int)r.size() + 1;\n *this = (this->rev() * inv(r.rev(), need)).pre(need).rev();\n //*this = (this->rev().pre(need) * inv(r.rev(), need)).pre(need).rev();\n return *this;\n }\n inline FPS& operator %= (const FPS &r) {\n FPS q = (*this) / r;\n return *this -= q * r;\n }\n inline FPS& operator <<= (int x) {\n FPS res(x, 0);\n res.insert(res.end(), begin(*this), end(*this));\n return *this = res;\n }\n inline FPS& operator >>= (int x) {\n FPS res;\n res.insert(res.end(), begin(*this) + x, end(*this));\n return *this = res;\n }\n inline mint eval(const mint& v){\n mint res = 0;\n for (int i = (int)this->size()-1; i >= 0; --i) {\n res *= v;\n res += (*this)[i];\n }\n return res;\n }\n inline friend FPS gcd(const FPS& f, const FPS& g) {\n if (g.empty()) return f;\n return gcd(g, f % g);\n }\n\n // advanced operation\n // df/dx\n inline friend FPS diff(const FPS& f) {\n int n = (int)f.size();\n FPS res(n-1);\n for (int i = 1; i < n; ++i) res[i-1] = f[i] * i;\n return res;\n }\n\n // \\int f dx\n inline friend FPS integral(const FPS& f) {\n int n = (int)f.size();\n FPS res(n+1, 0);\n for (int i = 0; i < n; ++i) res[i+1] = f[i] / (i+1);\n return res;\n }\n\n // inv(f), f[0] must not be 0\n inline friend FPS inv(const FPS& f, int deg) {\n assert(f[0] != 0);\n if (deg < 0) deg = (int)f.size();\n FPS res({mint(1) / f[0]});\n for (int i = 1; i < deg; i <<= 1) {\n res = (res + res - res * res * f.pre(i << 1)).pre(i << 1);\n }\n return res.pre(deg);\n }\n inline friend FPS inv(const FPS& f) {\n return inv(f, f.size());\n }\n\n // log(f) = \\int f'/f dx, f[0] must be 1\n inline friend FPS log(const FPS& f, int deg) {\n assert(f[0] == 1);\n FPS res = integral(diff(f) * inv(f, deg));\n res.resize(deg);\n return res;\n }\n inline friend FPS log(const FPS& f) {\n return log(f, f.size());\n }\n\n // exp(f), f[0] must be 0\n inline friend FPS exp(const FPS& f, int deg) {\n assert(f[0] == 0);\n FPS res(1, 1);\n for (int i = 1; i < deg; i <<= 1) {\n res = res * (f.pre(i<<1) - log(res, i<<1) + 1).pre(i<<1);\n }\n res.resize(deg);\n return res;\n }\n inline friend FPS exp(const FPS& f) {\n return exp(f, f.size());\n }\n\n // pow(f) = exp(e * log f)\n inline friend FPS pow(const FPS& f, long long e, int deg) {\n long long i = 0;\n while (i < (int)f.size() && f[i] == 0) ++i;\n if (i == (int)f.size()) return FPS(deg, 0);\n if (i * e >= deg) return FPS(deg, 0);\n mint k = f[i];\n FPS res = exp(log((f >> i) / k) * e) * modpow(k, e) << (e * i);\n res.resize(deg);\n return res;\n }\n inline friend FPS pow(const FPS& f, long long e) {\n return pow(f, e, f.size());\n }\n\n // sqrt(f), f[0] must be 1\n inline friend FPS sqrt(const FPS& f, int deg) {\n assert(f[0] == 1);\n int siz = 1;\n mint inv2 = mint(1) / 2;\n FPS res(1, 1);\n while (siz < deg) {\n siz <<= 1;\n FPS tmp(min(siz, (int)f.size()));\n for (int i = 0; i < (int)tmp.size(); ++i) tmp[i] = f[i];\n res += tmp * inv(f, siz);\n res.resize(siz);\n for (mint& x : res) res *= inv2;\n }\n return res;\n }\n inline friend FPS sqrt(const FPS& f) {\n return sqrt(f, f.size());\n }\n};\nusing mint = Fp<>;\n\nFPS<mint> modpow(const FPS<mint> &f, long long n, const FPS<mint> &m) {\n if (n == 0) return FPS<mint>(1, 1);\n auto t = modpow(f, n / 2, m);\n t = (t * t) % m;\n if (n & 1) t = (t * f) % m;\n auto q = t / m;\n auto r = t % m;\n return t;\n}\n\nint main() {\n /*\n MODS[0] = 1000000007;\n FPS<mint> x = {1, 2, 3, 2, 8, 32, 4, 42, 42, 42, 42};\n FPS<mint> y = {1, 5, 1, 2, 42};\n FPS<mint> q = x / y;\n FPS<mint> r = x % y;\n COUT(q);\n COUT(r);\n COUT(y * q + r);\n COUT(x * y);\n */\n\n long long N, P;\n while (cin >> N >> P) {\n if (N == 0) break;\n MODS[0] = P;\n\t\t\n FPS<mint> f(N+1);\n for (int i = 0; i < N+1; ++i) cin >> f[i];\n f.normalize();\n\t\tif (f.empty()) { \n\t\t\tcout << P << endl;\n\t\t\tcontinue; \n\t\t}\n\n FPS<mint> x(2, 0);\n x[1] = 1; // 多項式 x\n\t\tFPS<mint> g = modpow(x, P, f) - x;\n\t\tFPS<mint> res = gcd(f, g);\n\n\t\tcout << (int)res.size() - 1 << endl;\n }\n}", "accuracy": 1, "time_ms": 230, "memory_kb": 3644, "score_of_the_acc": -1.9709, "final_rank": 14 }, { "submission_id": "aoj_2213_3228798", "code_snippet": "#include <iostream>\n#include <vector>\n#include <cmath>\nusing namespace std;\n\n\n// Fp体上の多項式(MOD = P)\ninline long long mod(long long a, long long m) {\n\treturn (a % m + m) % m;\n}\n\nlong long pow(long long a, long long n, long long m) {\n\tif (n == 0) return 1 % m;\n\tlong long t = pow(a, n / 2, m);\n\tt = mod(t * t, m);\n\tif (n & 1) t = mod(t * a, m);\n\treturn t;\n}\n\nlong long inv(long long a, long long m) {\n\tlong long b = m, u = 1, v = 0;\n\twhile (b) {\n\t\tlong long t = a / b;\n\t\ta -= t*b; swap(a, b);\n\t\tu -= t*v; swap(u, v);\n\t}\n\treturn mod(u, m);\n}\n\nstruct ModPolynomial : vector<long long> {\n\tlong long MOD = 1000000007; // to be set\n\tModPolynomial() : vector<long long>(1, 0) { }\n\tModPolynomial(long long num) : vector<long long>(1, num) { }\n\tModPolynomial(int size, long long num) : vector<long long>(size, num) { }\n\tModPolynomial(vector<long long> vec) { (*this) = vec; }\n\n\tvoid init(long long num) { *this = vector<long long>(1, num); }\n\tvoid init(int size, long long num) { *this = vector<long long>(size, num); }\n\tvoid init(vector<long long> vec) { *this = vec; }\n\tModPolynomial& normalize() {\n\t\tfor (int i = 0; i < (*this).size(); ++i) (*this)[i] = mod((*this)[i], MOD);\n\t\twhile ((*this).size() > 1 && (*this).back() == 0) (*this).pop_back();\n\t\treturn (*this);\n\t}\n\n\tinline ModPolynomial operator - () {\n\t\tfor (int i = 0; i < (*this).size(); ++i) (*this)[i] = (*this)[i] * (-1);\n\t\treturn (*this).normalize();\n\t}\n\tinline const ModPolynomial& operator += (const ModPolynomial &x);\n\tinline const ModPolynomial& operator -= (const ModPolynomial &x);\n\tinline const ModPolynomial& operator *= (long long x);\n\tinline const ModPolynomial& operator *= (const ModPolynomial &x);\n\tinline const ModPolynomial& operator /= (long long x);\n\tinline const ModPolynomial& operator /= (const ModPolynomial &x);\n\tinline const ModPolynomial& operator %= (long long x);\n\tinline const ModPolynomial& operator %= (const ModPolynomial &x);\n};\n\ninline bool operator > (ModPolynomial x, ModPolynomial y) { return x.size() > y.size(); }\ninline bool operator < (ModPolynomial x, ModPolynomial y) { return y > x; }\ninline bool operator <= (ModPolynomial x, ModPolynomial y) { return !(y < x); }\ninline bool operator >= (ModPolynomial x, ModPolynomial y) { return !(x < y); }\ninline bool operator != (ModPolynomial x, ModPolynomial y) { return x < y || y < x; }\ninline bool operator == (ModPolynomial x, ModPolynomial y) { return !(x < y) && !(y < x); }\n\nostream &operator << (ostream &os, ModPolynomial x) {\n\tfor (int i = (int)x.size() - 1; i >= 0; --i) {\n\t\tif (i != (int)x.size() - 1) os << abs(x[i]); else os << x[i];\n\t\tif (i != 0) {\n\t\t\tos << \"x\"; if (i != 1) os << \"^\" << i; os << \" \";\n\t\t\tif (x[i - 1] < 0) os << \"- \"; else os << \"+ \";\n\t\t}\n\t}\n\treturn os;\n}\n\ninline ModPolynomial operator + (ModPolynomial x, ModPolynomial y) {\n\tx.normalize(); y.normalize();\n\twhile (x.size() > y.size()) y.push_back(0);\n\twhile (y.size() > x.size()) x.push_back(0);\n\tModPolynomial z((int)x.size(), 0);\n\tz.MOD = x.MOD;\n\tfor (int i = 0; i < (int)x.size(); ++i) z[i] = x[i] + y[i];\n\treturn z.normalize();\n}\ninline ModPolynomial operator - (ModPolynomial x, ModPolynomial y) {\n\ty = -y;\n\treturn x + y;\n}\ninline ModPolynomial operator * (ModPolynomial x, long long a) {\n\tx.normalize();\n\tModPolynomial z((int)x.size(), 0);\n\tz.MOD = x.MOD;\n\tfor (int i = 0; i < (int)x.size(); ++i) z[i] = mod(x[i] * a, x.MOD);\n\treturn z.normalize();\n}\ninline ModPolynomial operator * (ModPolynomial x, ModPolynomial y) {\n\tx.normalize(); y.normalize();\n\tModPolynomial z((int)x.size() + (int)y.size() - 1, 0);\n\tz.MOD = x.MOD;\n\tfor (int i = 0; i < (int)x.size(); ++i) for (int j = 0; j < (int)y.size(); ++j) z[i + j] = mod(z[i + j] + x[i] * y[j], z.MOD);\n\treturn z.normalize();\n}\npair<ModPolynomial, long long> divmod(ModPolynomial x, long long a) {\n\tx.normalize();\n\tfor (int i = (int)x.size() - 1; i >= 0; --i) x[i] = mod(x[i] * inv(a, x.MOD), x.MOD);\n\treturn pair<ModPolynomial, long long>(x, 0);\n}\nModPolynomial operator / (ModPolynomial x, long long a) {\n\tx.normalize();\n\treturn divmod(x, a).first;\n}\nlong long operator % (ModPolynomial x, long long a) {\n\tx.normalize();\n\treturn divmod(x, a).second;\n}\npair<ModPolynomial, ModPolynomial> divmod(ModPolynomial x, ModPolynomial y) {\n\tx.normalize(); y.normalize();\n\tif (x.size() < y.size()) return make_pair(0, x);\n\tif (y.size() == 1) return divmod(x, y.back());\n\tModPolynomial q((int)x.size() - (int)y.size() + 1, 0);\n\tfor (int i = (int)x.size() - 1; i >= (int)y.size() - 1; --i) {\n\t\tlong long div = mod(x[i] * inv(y.back(), x.MOD), x.MOD);\n\t\tq[i - (int)y.size() + 1] = div;\n\t\tfor (int j = 0; j < (int)y.size(); ++j) x[i + j - (int)y.size() + 1] = mod(x[i + j - (int)y.size() + 1] - y[j] * div, x.MOD);\n\t}\n\treturn make_pair(q, x.normalize());\n}\nModPolynomial operator / (ModPolynomial x, ModPolynomial y) {\n\tx.normalize(); y.normalize();\n\treturn divmod(x, y).first;\n}\nModPolynomial operator % (ModPolynomial x, ModPolynomial y) {\n\tx.normalize(); y.normalize();\n\treturn divmod(x, y).second;\n}\nModPolynomial pow(ModPolynomial a, long long n) {\n\ta.normalize();\n\tModPolynomial res(1, 1);\n\tres.MOD = a.MOD;\n\twhile (n > 0) { if (n & 1) { res = res * a; } a = a * a; n >>= 1; }\n\treturn res;\n}\nModPolynomial pow(ModPolynomial a, long long n, ModPolynomial PMOD) {\n\ta.normalize(); PMOD.normalize();\n\tModPolynomial res(1, 1);\n\tres.MOD = a.MOD;\n\twhile (n > 0) { if (n & 1) { res = (res * a) % PMOD; } a = (a * a) % PMOD; n >>= 1; }\n\treturn res;\n}\nModPolynomial gcd(ModPolynomial x, ModPolynomial y) {\n\tx.normalize(); y.normalize();\n\tif (y.size() == 1 && y.back() == 0) return x;\n\telse return gcd(y, x%y);\n}\ninline const ModPolynomial& ModPolynomial::operator += (const ModPolynomial &x) { *this = *this + x; return *this; }\ninline const ModPolynomial& ModPolynomial::operator -= (const ModPolynomial &x) { *this = *this - x; return *this; }\ninline const ModPolynomial& ModPolynomial::operator *= (long long x) { *this = *this * x; return *this; }\ninline const ModPolynomial& ModPolynomial::operator *= (const ModPolynomial &x) { *this = *this * x; return *this; }\ninline const ModPolynomial& ModPolynomial::operator /= (long long x) { *this = *this / x; return *this; }\ninline const ModPolynomial& ModPolynomial::operator /= (const ModPolynomial &x) { *this = *this / x; return *this; }\ninline const ModPolynomial& ModPolynomial::operator %= (long long x) { *this = *this % x; return *this; }\ninline const ModPolynomial& ModPolynomial::operator %= (const ModPolynomial &x) { *this = *this % x; return *this; }\n\n\n\n\nint main() {\n\tlong long N, P;\n\twhile (cin >> N >> P) {\n\t\tif (N == 0) break;\n\n\t\tModPolynomial f(N + 1, 0);\n\t\tf.MOD = P;\n\t\tfor (int i = 0; i < N + 1; ++i) cin >> f[i];\n\t\tf.normalize();\n\n\t\tif (f.size() == 1 && f.back() == 0) { \n\t\t\tcout << P << endl;\n\t\t\tcontinue; \n\t\t}\n\n\t\tModPolynomial x(2, 0); // 多項式 x\n\t\tx.MOD = P;\n\t\tx[1] = 1;\n\t\tModPolynomial xp_minus_x = pow(x, P, f) - x;\n\t\tModPolynomial g = gcd(f, xp_minus_x);\n\n\t\tcout << (int)g.size() - 1 << endl;\n\t}\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 3296, "score_of_the_acc": -1.0037, "final_rank": 6 }, { "submission_id": "aoj_2213_1120981", "code_snippet": "#include <iostream>\n#include <sstream>\n#include <cstdio>\n#include <cstdlib>\n#include <cmath>\n#include <ctime>\n#include <cstring>\n#include <string>\n#include <vector>\n#include <stack>\n#include <queue>\n#include <deque>\n#include <map>\n#include <set>\n#include <bitset>\n#include <numeric>\n#include <utility>\n#include <iomanip>\n#include <algorithm>\n#include <functional>\nusing namespace std;\n\ntypedef long long ll;\ntypedef vector<int> vint;\ntypedef vector<long long> vll;\ntypedef pair<int,int> pint;\ntypedef pair<long long, long long> pll;\n\n#define MP make_pair\n#define PB push_back\n#define ALL(s) (s).begin(),(s).end()\n#define EACH(i, s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)\n#define COUT(x) cout << #x << \" = \" << (x) << \" (L\" << __LINE__ << \")\" << endl\n\ntemplate<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }\ntemplate<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }\ntemplate<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P) \n{ return s << '<' << P.first << \", \" << P.second << '>'; }\ntemplate<class T> ostream& operator << (ostream &s, vector<T> P) \n{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << \" \"; } s << P[i]; } return s; }\ntemplate<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P) \n{ EACH(it, P) { s << \"<\" << it->first << \"->\" << it->second << \"> \"; } return s; }\n\n\n\n\n\n\n\nlong long MOD;\n\ninline long long mod(long long a, long long m) {\n return (a % m + m) % m;\n}\n\nlong long pow(long long a, long long n, long long m) {\n if (n == 0) return 1 % m;\n long long t = pow(a, n/2, m);\n t = mod(t * t, m);\n if (n & 1) t = mod(t * a, m);\n return t;\n}\n\nlong long inv(long long a, long long m) {\n long long b = m, u = 1, v = 0;\n while (b) {\n long long t = a/b;\n a -= t*b; swap(a, b);\n u -= t*v; swap(u, v);\n }\n return mod(u, m);\n}\n\nstruct ModPolynomial : vector<long long> {\n ModPolynomial() : vector<long long>(1, 0) { }\n ModPolynomial(long long num) : vector<long long>(1, num) { }\n ModPolynomial(int size, long long num) : vector<long long>(size, num) { }\n ModPolynomial(vector<long long> vec) { (*this) = vec; }\n \n ModPolynomial& normalize() {\n for (int i = 0; i < (*this).size(); ++i) (*this)[i] = mod((*this)[i], MOD);\n while ( (*this).size() > 1 && (*this).back() == 0 ) (*this).pop_back();\n return (*this);\n }\n \n inline ModPolynomial operator - () { \n for (int i = 0; i < (*this).size(); ++i) (*this)[i] = (*this)[i] * (-1); \n return (*this).normalize();\n }\n};\n\nostream &operator << (ostream &os, ModPolynomial x) {\n for (int i = x.size()-1; i >= 0; --i) { \n if (i != x.size()-1) os << abs(x[i]); else os << x[i];\n if (i != 0) {\n os << \"x\"; if (i != 1) os << \"^\" << i; os << \" \";\n if (x[i-1] < 0) os << \"- \"; else os << \"+ \";\n }\n }\n return os;\n}\n\ninline ModPolynomial operator + (ModPolynomial x, ModPolynomial y) {\n while (x.size() > y.size()) y.push_back(0);\n while (y.size() > x.size()) x.push_back(0);\n ModPolynomial z((int)x.size(), 0);\n for (int i = 0; i < x.size(); ++i) z[i] = x[i] + y[i];\n return z.normalize();\n} \ninline ModPolynomial operator - (ModPolynomial x, ModPolynomial y) {\n y = -y;\n return x + y;\n}\ninline ModPolynomial operator * (ModPolynomial x, ModPolynomial y) {\n ModPolynomial z((int)x.size() + (int)y.size() - 1, 0);\n for (int i = 0; i < x.size(); ++i) for (int j = 0; j < y.size(); ++j) z[i+j] = mod(z[i+j] + x[i] * y[j], MOD);\n return z.normalize();\n}\npair<ModPolynomial,long long> divmod(ModPolynomial x, long long a) {\n for (int i = x.size()-1; i >= 0; --i) x[i] = mod(x[i] * inv(a, MOD), MOD);\n return pair<ModPolynomial,long long>(x, 0);\n}\npair<ModPolynomial,ModPolynomial> divmod(ModPolynomial x, ModPolynomial y) {\n if (x.size() < y.size()) return make_pair(0, x);\n if (y.size() == 1) return divmod(x, y.back());\n ModPolynomial q((int)x.size() - (int)y.size() + 1, 0);\n for (int i = x.size()-1; i >= y.size()-1; --i) {\n long long div = mod(x[i] * inv(y.back(), MOD), MOD);\n q[i-(int)y.size()+1] = div;\n for (int j = 0; j < y.size(); ++j) x[i+j-(int)y.size()+1] = mod(x[i+j-(int)y.size()+1] - y[j] * div, MOD);\n }\n return make_pair(q, x.normalize());\n}\nModPolynomial operator % (ModPolynomial x, ModPolynomial y) {\n return divmod(x, y).second;\n}\nModPolynomial pow(ModPolynomial a, long long n, ModPolynomial PMOD) {\n ModPolynomial res(1, 1);\n while (n > 0) { if (n & 1) { res = (res * a) % PMOD; } a = (a * a) % PMOD; n >>= 1;}\n return res;\n}\nModPolynomial gcd(ModPolynomial x, ModPolynomial y) {\n if (y.size() == 1 && y.back() == 0) return x;\n else return gcd(y, x%y);\n}\nModPolynomial generateX() { ModPolynomial X(2, 0); X[1] = 1; return X; }\nModPolynomial EX = generateX();\n\n\n\nlong long N, P, a;\n\nint main() {\n while (cin >> N >> P) {\n if (N == 0) break;\n \n MOD = P;\n ModPolynomial pol(N+1, 0);\n for (int i = 0; i < N+1; ++i) cin >> pol[i];\n pol.normalize();\n \n if (pol.size() == 1 && pol.back() == 0) { cout << MOD << endl; continue; }\n \n ModPolynomial check = pow(EX, MOD, pol) - EX;\n ModPolynomial G = gcd(pol, check);\n \n cout << G.size() - 1 << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 1376, "score_of_the_acc": -0.2273, "final_rank": 1 }, { "submission_id": "aoj_2213_1120980", "code_snippet": "#include <iostream>\n#include <sstream>\n#include <cstdio>\n#include <cstdlib>\n#include <cmath>\n#include <ctime>\n#include <cstring>\n#include <string>\n#include <vector>\n#include <stack>\n#include <queue>\n#include <deque>\n#include <map>\n#include <set>\n#include <bitset>\n#include <numeric>\n#include <utility>\n#include <iomanip>\n#include <algorithm>\n#include <functional>\nusing namespace std;\n\ntypedef long long ll;\ntypedef vector<int> vint;\ntypedef vector<long long> vll;\ntypedef pair<int,int> pint;\ntypedef pair<long long, long long> pll;\n\n#define MP make_pair\n#define PB push_back\n#define ALL(s) (s).begin(),(s).end()\n#define EACH(i, s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)\n#define COUT(x) cout << #x << \" = \" << (x) << \" (L\" << __LINE__ << \")\" << endl\n\ntemplate<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }\ntemplate<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }\ntemplate<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P) \n{ return s << '<' << P.first << \", \" << P.second << '>'; }\ntemplate<class T> ostream& operator << (ostream &s, vector<T> P) \n{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << \" \"; } s << P[i]; } return s; }\ntemplate<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P) \n{ EACH(it, P) { s << \"<\" << it->first << \"->\" << it->second << \"> \"; } return s; }\n\n\n\n\n\n\n\nlong long MOD;\n\ninline long long mod(long long a, long long m) {\n return (a % m + m) % m;\n}\n\nlong long pow(long long a, long long n, long long m) {\n if (n == 0) return 1 % m;\n long long t = pow(a, n/2, m);\n t = mod(t * t, m);\n if (n & 1) t = mod(t * a, m);\n return t;\n}\n\nlong long inv(long long a, long long m) {\n long long b = m, u = 1, v = 0;\n while (b) {\n long long t = a/b;\n a -= t*b; swap(a, b);\n u -= t*v; swap(u, v);\n }\n return mod(u, m);\n}\n\nstruct ModPolynomial : vector<long long> {\n ModPolynomial() : vector<long long>(1, 0) { }\n ModPolynomial(long long num) : vector<long long>(1, num) { }\n ModPolynomial(int size, long long num) : vector<long long>(size, num) { }\n ModPolynomial(vector<long long> vec) { (*this) = vec; }\n \n ModPolynomial& normalize() {\n for (int i = 0; i < (*this).size(); ++i) (*this)[i] = mod((*this)[i], MOD);\n while ( (*this).size() > 1 && (*this).back() == 0 ) (*this).pop_back();\n return (*this);\n }\n \n inline ModPolynomial operator - () { \n for (int i = 0; i < (*this).size(); ++i) (*this)[i] = (*this)[i] * (-1); \n return (*this).normalize();\n }\n inline const ModPolynomial& operator += (const ModPolynomial &x);\n inline const ModPolynomial& operator -= (const ModPolynomial &x);\n inline const ModPolynomial& operator *= (long long x);\n inline const ModPolynomial& operator *= (const ModPolynomial &x);\n inline const ModPolynomial& operator /= (long long x);\n inline const ModPolynomial& operator /= (const ModPolynomial &x);\n inline const ModPolynomial& operator %= (long long x);\n inline const ModPolynomial& operator %= (const ModPolynomial &x);\n};\n\ninline bool operator > (ModPolynomial x, ModPolynomial y) { return x.size() > y.size(); }\ninline bool operator < (ModPolynomial x, ModPolynomial y) { return y > x; }\ninline bool operator <= (ModPolynomial x, ModPolynomial y) { return !(y < x); }\ninline bool operator >= (ModPolynomial x, ModPolynomial y) { return !(x < y); }\ninline bool operator != (ModPolynomial x, ModPolynomial y) { return x < y || y < x; }\ninline bool operator == (ModPolynomial x, ModPolynomial y) { return !(x < y) && !(y < x); }\n\nostream &operator << (ostream &os, ModPolynomial x) {\n for (int i = x.size()-1; i >= 0; --i) { \n if (i != x.size()-1) os << abs(x[i]); else os << x[i];\n if (i != 0) {\n os << \"x\"; if (i != 1) os << \"^\" << i; os << \" \";\n if (x[i-1] < 0) os << \"- \"; else os << \"+ \";\n }\n }\n return os;\n}\n\ninline ModPolynomial operator + (ModPolynomial x, ModPolynomial y) {\n //x.normalize(); y.normalize();\n while (x.size() > y.size()) y.push_back(0);\n while (y.size() > x.size()) x.push_back(0);\n ModPolynomial z((int)x.size(), 0);\n for (int i = 0; i < x.size(); ++i) z[i] = x[i] + y[i];\n return z.normalize();\n} \ninline ModPolynomial operator - (ModPolynomial x, ModPolynomial y) {\n y = -y;\n return x + y;\n}\ninline ModPolynomial operator * (ModPolynomial x, ModPolynomial y) {\n //x.normalize(); y.normalize();\n ModPolynomial z((int)x.size() + (int)y.size() - 1, 0);\n for (int i = 0; i < x.size(); ++i) for (int j = 0; j < y.size(); ++j) z[i+j] = mod(z[i+j] + x[i] * y[j], MOD);\n return z.normalize();\n}\npair<ModPolynomial,long long> divmod(ModPolynomial x, long long a) {\n //x.normalize();\n for (int i = x.size()-1; i >= 0; --i) x[i] = mod(x[i] * inv(a, MOD), MOD);\n return pair<ModPolynomial,long long>(x, 0);\n}\npair<ModPolynomial,ModPolynomial> divmod(ModPolynomial x, ModPolynomial y) {\n //x.normalize(); y.normalize();\n if (x.size() < y.size()) return make_pair(0, x);\n if (y.size() == 1) return divmod(x, y.back());\n ModPolynomial q((int)x.size() - (int)y.size() + 1, 0);\n for (int i = x.size()-1; i >= y.size()-1; --i) {\n long long div = mod(x[i] * inv(y.back(), MOD), MOD);\n q[i-(int)y.size()+1] = div;\n for (int j = 0; j < y.size(); ++j) x[i+j-(int)y.size()+1] = mod(x[i+j-(int)y.size()+1] - y[j] * div, MOD);\n }\n return make_pair(q, x.normalize());\n}\nModPolynomial operator % (ModPolynomial x, ModPolynomial y) {\n //x.normalize(); y.normalize();\n return divmod(x, y).second;\n}\nModPolynomial pow(ModPolynomial a, long long n, ModPolynomial PMOD) {\n //a.normalize(); PMOD.normalize();\n ModPolynomial res(1, 1);\n while (n > 0) { if (n & 1) { res = (res * a) % PMOD; } a = (a * a) % PMOD; n >>= 1;}\n return res;\n}\nModPolynomial gcd(ModPolynomial x, ModPolynomial y) {\n //x.normalize(); y.normalize();\n if (y.size() == 1 && y.back() == 0) return x;\n else return gcd(y, x%y);\n}\nModPolynomial generateX() { ModPolynomial X(2, 0); X[1] = 1; return X; }\nModPolynomial EX = generateX();\n\n\n\nlong long N, P, a;\n\nint main() {\n //freopen( \"/Users/macuser/Dropbox/Contest/test_00.in\", \"r\", stdin );\n while (cin >> N >> P) {\n if (N == 0) break;\n \n MOD = P;\n ModPolynomial pol(N+1, 0);\n for (int i = 0; i < N+1; ++i) cin >> pol[i];\n pol.normalize();\n \n if (pol.size() == 1 && pol.back() == 0) { cout << MOD << endl; continue; }\n \n ModPolynomial check = pow(EX, MOD, pol) - EX;\n ModPolynomial G = gcd(pol, check);\n \n cout << G.size() - 1 << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 1376, "score_of_the_acc": -0.2273, "final_rank": 1 }, { "submission_id": "aoj_2213_1120978", "code_snippet": "#include <iostream>\n#include <sstream>\n#include <cstdio>\n#include <cstdlib>\n#include <cmath>\n#include <ctime>\n#include <cstring>\n#include <string>\n#include <vector>\n#include <stack>\n#include <queue>\n#include <deque>\n#include <map>\n#include <set>\n#include <bitset>\n#include <numeric>\n#include <utility>\n#include <iomanip>\n#include <algorithm>\n#include <functional>\nusing namespace std;\n\ntypedef long long ll;\ntypedef vector<int> vint;\ntypedef vector<long long> vll;\ntypedef pair<int,int> pint;\ntypedef pair<long long, long long> pll;\n\n#define MP make_pair\n#define PB push_back\n#define ALL(s) (s).begin(),(s).end()\n#define EACH(i, s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)\n#define COUT(x) cout << #x << \" = \" << (x) << \" (L\" << __LINE__ << \")\" << endl\n\ntemplate<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }\ntemplate<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }\ntemplate<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P) \n{ return s << '<' << P.first << \", \" << P.second << '>'; }\ntemplate<class T> ostream& operator << (ostream &s, vector<T> P) \n{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << \" \"; } s << P[i]; } return s; }\ntemplate<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P) \n{ EACH(it, P) { s << \"<\" << it->first << \"->\" << it->second << \"> \"; } return s; }\n\n\n\n\n\n\n\nlong long MOD;\n\ninline long long mod(long long a, long long m) {\n return (a % m + m) % m;\n}\n\nlong long pow(long long a, long long n, long long m) {\n if (n == 0) return 1 % m;\n long long t = pow(a, n/2, m);\n t = mod(t * t, m);\n if (n & 1) t = mod(t * a, m);\n return t;\n}\n\nlong long inv(long long a, long long m) {\n long long b = m, u = 1, v = 0;\n while (b) {\n long long t = a/b;\n a -= t*b; swap(a, b);\n u -= t*v; swap(u, v);\n }\n return mod(u, m);\n}\n\nstruct ModPolynomial : vector<long long> {\n ModPolynomial() : vector<long long>(1, 0) { }\n ModPolynomial(long long num) : vector<long long>(1, num) { }\n ModPolynomial(int size, long long num) : vector<long long>(size, num) { }\n ModPolynomial(vector<long long> vec) { (*this) = vec; }\n \n ModPolynomial& normalize() {\n for (int i = 0; i < (*this).size(); ++i) (*this)[i] = mod((*this)[i], MOD);\n while ( (*this).size() > 1 && (*this).back() == 0 ) (*this).pop_back();\n return (*this);\n }\n \n inline ModPolynomial operator - () { \n for (int i = 0; i < (*this).size(); ++i) (*this)[i] = (*this)[i] * (-1); \n return (*this).normalize();\n }\n inline const ModPolynomial& operator += (const ModPolynomial &x);\n inline const ModPolynomial& operator -= (const ModPolynomial &x);\n inline const ModPolynomial& operator *= (long long x);\n inline const ModPolynomial& operator *= (const ModPolynomial &x);\n inline const ModPolynomial& operator /= (long long x);\n inline const ModPolynomial& operator /= (const ModPolynomial &x);\n inline const ModPolynomial& operator %= (long long x);\n inline const ModPolynomial& operator %= (const ModPolynomial &x);\n};\n\ninline bool operator > (ModPolynomial x, ModPolynomial y) { return x.size() > y.size(); }\ninline bool operator < (ModPolynomial x, ModPolynomial y) { return y > x; }\ninline bool operator <= (ModPolynomial x, ModPolynomial y) { return !(y < x); }\ninline bool operator >= (ModPolynomial x, ModPolynomial y) { return !(x < y); }\ninline bool operator != (ModPolynomial x, ModPolynomial y) { return x < y || y < x; }\ninline bool operator == (ModPolynomial x, ModPolynomial y) { return !(x < y) && !(y < x); }\n\nostream &operator << (ostream &os, ModPolynomial x) {\n for (int i = x.size()-1; i >= 0; --i) { \n if (i != x.size()-1) os << abs(x[i]); else os << x[i];\n if (i != 0) {\n os << \"x\"; if (i != 1) os << \"^\" << i; os << \" \";\n if (x[i-1] < 0) os << \"- \"; else os << \"+ \";\n }\n }\n return os;\n}\n\ninline ModPolynomial operator + (ModPolynomial x, ModPolynomial y) {\n x.normalize(); y.normalize();\n while (x.size() > y.size()) y.push_back(0);\n while (y.size() > x.size()) x.push_back(0);\n ModPolynomial z((int)x.size(), 0);\n for (int i = 0; i < x.size(); ++i) z[i] = x[i] + y[i];\n return z.normalize();\n} \ninline ModPolynomial operator - (ModPolynomial x, ModPolynomial y) {\n y = -y;\n return x + y;\n}\ninline ModPolynomial operator * (ModPolynomial x, long long a) {\n x.normalize();\n ModPolynomial z((int)x.size(), 0);\n for (int i = 0; i < x.size(); ++i) z[i] = mod(x[i] * a, MOD);\n return z.normalize();\n}\ninline ModPolynomial operator * (ModPolynomial x, ModPolynomial y) {\n //x.normalize(); y.normalize();\n ModPolynomial z((int)x.size() + (int)y.size() - 1, 0);\n for (int i = 0; i < x.size(); ++i) for (int j = 0; j < y.size(); ++j) z[i+j] = mod(z[i+j] + x[i] * y[j], MOD);\n return z.normalize();\n}\npair<ModPolynomial,long long> divmod(ModPolynomial x, long long a) {\n x.normalize();\n for (int i = x.size()-1; i >= 0; --i) x[i] = mod(x[i] * inv(a, MOD), MOD);\n return pair<ModPolynomial,long long>(x, 0);\n}\nModPolynomial operator / (ModPolynomial x, long long a) {\n x.normalize();\n return divmod(x, a).first;\n}\nlong long operator % (ModPolynomial x, long long a) {\n x.normalize();\n return divmod(x, a).second;\n}\npair<ModPolynomial,ModPolynomial> divmod(ModPolynomial x, ModPolynomial y) {\n x.normalize(); y.normalize();\n if (x.size() < y.size()) return make_pair(0, x);\n if (y.size() == 1) return divmod(x, y.back());\n ModPolynomial q((int)x.size() - (int)y.size() + 1, 0);\n for (int i = x.size()-1; i >= y.size()-1; --i) {\n long long div = mod(x[i] * inv(y.back(), MOD), MOD);\n q[i-(int)y.size()+1] = div;\n for (int j = 0; j < y.size(); ++j) x[i+j-(int)y.size()+1] = mod(x[i+j-(int)y.size()+1] - y[j] * div, MOD);\n }\n return make_pair(q, x.normalize());\n}\nModPolynomial operator / (ModPolynomial x, ModPolynomial y) {\n //x.normalize(); y.normalize();\n return divmod(x, y).first;\n}\nModPolynomial operator % (ModPolynomial x, ModPolynomial y) {\n //x.normalize(); y.normalize();\n return divmod(x, y).second;\n}\nModPolynomial pow(ModPolynomial a, long long n) {\n a.normalize();\n ModPolynomial res(1, 1);\n while (n > 0) { if (n & 1) { res = res * a; } a = a * a; n >>= 1;}\n return res;\n}\nModPolynomial pow(ModPolynomial a, long long n, ModPolynomial PMOD) {\n a.normalize(); PMOD.normalize();\n ModPolynomial res(1, 1);\n while (n > 0) { if (n & 1) { res = (res * a) % PMOD; } a = (a * a) % PMOD; n >>= 1;}\n return res;\n}\nModPolynomial gcd(ModPolynomial x, ModPolynomial y) {\n x.normalize(); y.normalize();\n if (y.size() == 1 && y.back() == 0) return x;\n else return gcd(y, x%y);\n}\ninline const ModPolynomial& ModPolynomial::operator += (const ModPolynomial &x) {*this = *this + x; return *this;}\ninline const ModPolynomial& ModPolynomial::operator -= (const ModPolynomial &x) {*this = *this - x; return *this;}\ninline const ModPolynomial& ModPolynomial::operator *= (long long x) {*this = *this * x; return *this;}\ninline const ModPolynomial& ModPolynomial::operator *= (const ModPolynomial &x) {*this = *this * x; return *this;}\ninline const ModPolynomial& ModPolynomial::operator /= (long long x) {*this = *this / x; return *this;}\ninline const ModPolynomial& ModPolynomial::operator /= (const ModPolynomial &x) {*this = *this / x; return *this;}\ninline const ModPolynomial& ModPolynomial::operator %= (long long x) {*this = *this % x; return *this;}\ninline const ModPolynomial& ModPolynomial::operator %= (const ModPolynomial &x) {*this = *this % x; return *this;}\nModPolynomial generateX() { ModPolynomial X(2, 0); X[1] = 1; return X; }\nModPolynomial EX = generateX();\n\n\n\nlong long N, P, a;\n\nint main() {\n //freopen( \"/Users/macuser/Dropbox/Contest/test_00.in\", \"r\", stdin );\n \n// MOD = 11;\n// ModPolynomial p(3, 0.0), q(4, 0.0);\n// p[0] = 8; p[1] = 7; p[2] = -2;\n// q[0] = 9; q[1] = -5; q[2] = -4; q[3] = 6;\n// \n// p.normalize(); q.normalize();\n// COUT(p);\n// COUT(q);\n// COUT(p+q);\n// COUT(p-q);\n// COUT(q*p);\n// COUT(p*q);\n// COUT(q/p);\n// COUT(q%p);\n// COUT(p/q);\n// COUT(p%q);\n// COUT(gcd(p,q));\n \n while (cin >> N >> P) {\n if (N == 0) break;\n \n MOD = P;\n ModPolynomial pol(N+1, 0);\n for (int i = 0; i < N+1; ++i) cin >> pol[i];\n pol.normalize();\n \n //COUT(pol);\n \n if (pol.size() == 1 && pol.back() == 0) { cout << MOD << endl; continue; }\n \n ModPolynomial check = pow(EX, MOD, pol) - EX;\n \n //COUT(check);\n \n ModPolynomial G = gcd(pol, check);\n \n cout << G.size() - 1 << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 1380, "score_of_the_acc": -0.2744, "final_rank": 3 }, { "submission_id": "aoj_2213_1120977", "code_snippet": "#include <iostream>\n#include <sstream>\n#include <cstdio>\n#include <cstdlib>\n#include <cmath>\n#include <ctime>\n#include <cstring>\n#include <string>\n#include <vector>\n#include <stack>\n#include <queue>\n#include <deque>\n#include <map>\n#include <set>\n#include <bitset>\n#include <numeric>\n#include <utility>\n#include <iomanip>\n#include <algorithm>\n#include <functional>\nusing namespace std;\n\ntypedef long long ll;\ntypedef vector<int> vint;\ntypedef vector<long long> vll;\ntypedef pair<int,int> pint;\ntypedef pair<long long, long long> pll;\n\n#define MP make_pair\n#define PB push_back\n#define ALL(s) (s).begin(),(s).end()\n#define EACH(i, s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)\n#define COUT(x) cout << #x << \" = \" << (x) << \" (L\" << __LINE__ << \")\" << endl\n\ntemplate<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }\ntemplate<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }\ntemplate<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P) \n{ return s << '<' << P.first << \", \" << P.second << '>'; }\ntemplate<class T> ostream& operator << (ostream &s, vector<T> P) \n{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << \" \"; } s << P[i]; } return s; }\ntemplate<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P) \n{ EACH(it, P) { s << \"<\" << it->first << \"->\" << it->second << \"> \"; } return s; }\n\n\n\n\n\n\n\nlong long MOD;\n\ninline long long mod(long long a, long long m) {\n return (a % m + m) % m;\n}\n\nlong long pow(long long a, long long n, long long m) {\n if (n == 0) return 1 % m;\n long long t = pow(a, n/2, m);\n t = mod(t * t, m);\n if (n & 1) t = mod(t * a, m);\n return t;\n}\n\nlong long inv(long long a, long long m) {\n long long b = m, u = 1, v = 0;\n while (b) {\n long long t = a/b;\n a -= t*b; swap(a, b);\n u -= t*v; swap(u, v);\n }\n return mod(u, m);\n}\n\nstruct ModPolynomial : vector<long long> {\n ModPolynomial() : vector<long long>(1, 0) { }\n ModPolynomial(long long num) : vector<long long>(1, num) { }\n ModPolynomial(int size, long long num) : vector<long long>(size, num) { }\n ModPolynomial(vector<long long> vec) { (*this) = vec; }\n \n ModPolynomial& normalize() {\n for (int i = 0; i < (*this).size(); ++i) (*this)[i] = mod((*this)[i], MOD);\n while ( (*this).size() > 1 && (*this).back() == 0 ) (*this).pop_back();\n return (*this);\n }\n \n inline ModPolynomial operator - () { \n for (int i = 0; i < (*this).size(); ++i) (*this)[i] = (*this)[i] * (-1); \n return (*this).normalize();\n }\n inline const ModPolynomial& operator += (const ModPolynomial &x);\n inline const ModPolynomial& operator -= (const ModPolynomial &x);\n inline const ModPolynomial& operator *= (long long x);\n inline const ModPolynomial& operator *= (const ModPolynomial &x);\n inline const ModPolynomial& operator /= (long long x);\n inline const ModPolynomial& operator /= (const ModPolynomial &x);\n inline const ModPolynomial& operator %= (long long x);\n inline const ModPolynomial& operator %= (const ModPolynomial &x);\n};\n\ninline bool operator > (ModPolynomial x, ModPolynomial y) { return x.size() > y.size(); }\ninline bool operator < (ModPolynomial x, ModPolynomial y) { return y > x; }\ninline bool operator <= (ModPolynomial x, ModPolynomial y) { return !(y < x); }\ninline bool operator >= (ModPolynomial x, ModPolynomial y) { return !(x < y); }\ninline bool operator != (ModPolynomial x, ModPolynomial y) { return x < y || y < x; }\ninline bool operator == (ModPolynomial x, ModPolynomial y) { return !(x < y) && !(y < x); }\n\nostream &operator << (ostream &os, ModPolynomial x) {\n for (int i = x.size()-1; i >= 0; --i) { \n if (i != x.size()-1) os << abs(x[i]); else os << x[i];\n if (i != 0) {\n os << \"x\"; if (i != 1) os << \"^\" << i; os << \" \";\n if (x[i-1] < 0) os << \"- \"; else os << \"+ \";\n }\n }\n return os;\n}\n\ninline ModPolynomial operator + (ModPolynomial x, ModPolynomial y) {\n x.normalize(); y.normalize();\n while (x.size() > y.size()) y.push_back(0);\n while (y.size() > x.size()) x.push_back(0);\n ModPolynomial z((int)x.size(), 0);\n for (int i = 0; i < x.size(); ++i) z[i] = x[i] + y[i];\n return z.normalize();\n} \ninline ModPolynomial operator - (ModPolynomial x, ModPolynomial y) {\n y = -y;\n return x + y;\n}\ninline ModPolynomial operator * (ModPolynomial x, long long a) {\n x.normalize();\n ModPolynomial z((int)x.size(), 0);\n for (int i = 0; i < x.size(); ++i) z[i] = mod(x[i] * a, MOD);\n return z.normalize();\n}\ninline ModPolynomial operator * (ModPolynomial x, ModPolynomial y) {\n x.normalize(); y.normalize();\n ModPolynomial z((int)x.size() + (int)y.size() - 1, 0);\n for (int i = 0; i < x.size(); ++i) for (int j = 0; j < y.size(); ++j) z[i+j] = mod(z[i+j] + x[i] * y[j], MOD);\n return z.normalize();\n}\npair<ModPolynomial,long long> divmod(ModPolynomial x, long long a) {\n x.normalize();\n for (int i = x.size()-1; i >= 0; --i) x[i] = mod(x[i] * inv(a, MOD), MOD);\n return pair<ModPolynomial,long long>(x, 0);\n}\nModPolynomial operator / (ModPolynomial x, long long a) {\n x.normalize();\n return divmod(x, a).first;\n}\nlong long operator % (ModPolynomial x, long long a) {\n x.normalize();\n return divmod(x, a).second;\n}\npair<ModPolynomial,ModPolynomial> divmod(ModPolynomial x, ModPolynomial y) {\n x.normalize(); y.normalize();\n if (x.size() < y.size()) return make_pair(0, x);\n if (y.size() == 1) return divmod(x, y.back());\n ModPolynomial q((int)x.size() - (int)y.size() + 1, 0);\n for (int i = x.size()-1; i >= y.size()-1; --i) {\n long long div = mod(x[i] * inv(y.back(), MOD), MOD);\n q[i-(int)y.size()+1] = div;\n for (int j = 0; j < y.size(); ++j) x[i+j-(int)y.size()+1] = mod(x[i+j-(int)y.size()+1] - y[j] * div, MOD);\n }\n return make_pair(q, x.normalize());\n}\nModPolynomial operator / (ModPolynomial x, ModPolynomial y) {\n x.normalize(); y.normalize();\n return divmod(x, y).first;\n}\nModPolynomial operator % (ModPolynomial x, ModPolynomial y) {\n x.normalize(); y.normalize();\n return divmod(x, y).second;\n}\nModPolynomial pow(ModPolynomial a, long long n) {\n a.normalize();\n ModPolynomial res(1, 1);\n while (n > 0) { if (n & 1) { res = res * a; } a = a * a; n >>= 1;}\n return res;\n}\nModPolynomial pow(ModPolynomial a, long long n, ModPolynomial PMOD) {\n a.normalize(); PMOD.normalize();\n ModPolynomial res(1, 1);\n while (n > 0) { if (n & 1) { res = (res * a) % PMOD; } a = (a * a) % PMOD; n >>= 1;}\n return res;\n}\nModPolynomial gcd(ModPolynomial x, ModPolynomial y) {\n x.normalize(); y.normalize();\n if (y.size() == 1 && y.back() == 0) return x;\n else return gcd(y, x%y);\n}\ninline const ModPolynomial& ModPolynomial::operator += (const ModPolynomial &x) {*this = *this + x; return *this;}\ninline const ModPolynomial& ModPolynomial::operator -= (const ModPolynomial &x) {*this = *this - x; return *this;}\ninline const ModPolynomial& ModPolynomial::operator *= (long long x) {*this = *this * x; return *this;}\ninline const ModPolynomial& ModPolynomial::operator *= (const ModPolynomial &x) {*this = *this * x; return *this;}\ninline const ModPolynomial& ModPolynomial::operator /= (long long x) {*this = *this / x; return *this;}\ninline const ModPolynomial& ModPolynomial::operator /= (const ModPolynomial &x) {*this = *this / x; return *this;}\ninline const ModPolynomial& ModPolynomial::operator %= (long long x) {*this = *this % x; return *this;}\ninline const ModPolynomial& ModPolynomial::operator %= (const ModPolynomial &x) {*this = *this % x; return *this;}\nModPolynomial generateX() { ModPolynomial X(2, 0); X[1] = 1; return X; }\nModPolynomial EX = generateX();\n\n\n\nlong long N, P, a;\n\nint main() {\n //freopen( \"/Users/macuser/Dropbox/Contest/test_00.in\", \"r\", stdin );\n \n// MOD = 11;\n// ModPolynomial p(3, 0.0), q(4, 0.0);\n// p[0] = 8; p[1] = 7; p[2] = -2;\n// q[0] = 9; q[1] = -5; q[2] = -4; q[3] = 6;\n// \n// p.normalize(); q.normalize();\n// COUT(p);\n// COUT(q);\n// COUT(p+q);\n// COUT(p-q);\n// COUT(q*p);\n// COUT(p*q);\n// COUT(q/p);\n// COUT(q%p);\n// COUT(p/q);\n// COUT(p%q);\n// COUT(gcd(p,q));\n \n while (cin >> N >> P) {\n if (N == 0) break;\n \n MOD = P;\n ModPolynomial pol(N+1, 0);\n for (int i = 0; i < N+1; ++i) cin >> pol[i];\n pol.normalize();\n \n //COUT(pol);\n \n if (pol.size() == 1 && pol.back() == 0) { cout << MOD << endl; continue; }\n \n ModPolynomial check = pow(EX, MOD, pol) - EX;\n \n //COUT(check);\n \n ModPolynomial G = gcd(pol, check);\n \n cout << G.size() - 1 << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 1388, "score_of_the_acc": -0.2779, "final_rank": 4 } ]
aoj_2211_cpp
Problem H: 迷い猫、走った 洋菓子専門店ストレイキャッツではたくさんの猫が飼われている。猫の数があまりにも多いため、この店のスタッフである希は餌やりに困っていた。 いくつかの場所に餌を置いても、猫は貪欲なので最も近い餌に向かってしまい、一つの餌入れにたくさんの猫が集中してしまう。 あなたは希のために、できるだけ猫が集中しない餌の配置を求める問題を解くことにした。 問題を簡単にするため、猫は平面上の y>0 の部分にいて餌を置くまで動かないとする。 猫は N 個の集団に分かれていて、一箇所に何匹もいるかもしれない。 餌入れは y=0 上の M 点に固定してあり、この中からいくつか選んで餌を入れることになる。 餌を入れると、猫は(ユークリッド距離で)一番近い餌の入った餌入れに向かうが、同じ距離に二つあるときはx座標が小さいほうに向かうものとする。 この条件の下で、最も猫が集まっている餌入れの猫の数を最小化しなさい。 Input 入力は以下のような形式で与えられる。 N M x 1 y 1 C 1 ... x N y N C N x 1 ... x M 入力の1行目には、2つの整数 N, M がスペース文字で区切られて与えられる。これらは、問題文で指定したとおりである。0 ≤ N ≤ 1000 および 1 ≤ M ≤ 1000 を満たす。 続く N 行には、それぞれの猫の集団について1行に1つずつ、集団の位置の x, y 座標を表す整数の組 (-10000 ≤ x ≤ 10000, 0 < y ≤ 10000)と何匹集まっているかを表す整数C(1 ≤ C ≤ 100000)が与えられる。 その後のM行には、それぞれの餌入れについて1行に1つずつ、餌入れの位置のx座標を表す整数(-10000 ≤ x ≤ 10000)が与えられる。 同じ点に、二つ以上の餌入れが存在することはない。 Output 最も猫が集まっている餌入れの猫の数の最小値を出力せよ。 Notes on Test Cases 上記入力形式で複数のデータセットが与えられます。各データセットに対して上記出力形式で出力を行うプログラムを作成して下さい。 N, M がともに 0 のとき入力の終わりを示します。 Sample Input 3 3 -1 1 1 0 1 1 1 1 1 -3 0 3 5 5 7 3 5 -9 2 7 4 9 6 10 5 9 -9 7 9 0 -1 -2 -9 -8 0 0 Output for Sample Input 2 20
[ { "submission_id": "aoj_2211_10215478", "code_snippet": "// AOJ #2211\n// Stray Cats, Run... 2025.2.13\n\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n \nstruct State { int i, j; };\n \nbool feasible(ll L, const vector<int>& xGroups, const vector<ll>& weights,\n const vector<ll>& prefix, const vector<int>& bowls) {\n int n = xGroups.size();\n int m = bowls.size();\n if(n == 0) return true;\n \n vector<int> nextRange(n, 0);\n int r = 0;\n for (int s = 0; s < n; s++) {\n if(r < s) r = s;\n while(r < n && (prefix[r+1] - prefix[s]) <= L) r++;\n nextRange[s] = r - 1;\n }\n \n vector<vector<bool>> visited(n, vector<bool>(m, false));\n deque<State> dq;\n \n int firstMax = nextRange[0];\n for (int j = 0; j < m; j++){\n for (int r = 0; r <= firstMax; r++){\n if(!visited[r][j]){\n visited[r][j] = true;\n dq.push_back({r, j});\n }\n }\n }\n \n while(!dq.empty()){\n State cur = dq.front();\n dq.pop_front();\n int i = cur.i, j = cur.j;\n if(i == n-1) return true;\n \n int nextStart = i + 1;\n ll b_curr = bowls[j];\n int lowerBound = max(bowls[j] + 1, 2 * xGroups[i] - (int)b_curr);\n int upperBound = 2 * xGroups[nextStart] - (int)b_curr;\n \n int kLow, kHigh;\n {\n int lo = j+1, hi = m;\n while(lo < hi){\n int mid = (lo + hi) / 2;\n if(bowls[mid] < lowerBound) lo = mid + 1;\n else hi = mid;\n }\n kLow = lo;\n }\n {\n int lo = j+1, hi = m;\n while(lo < hi){\n int mid = (lo + hi) / 2;\n if(bowls[mid] < upperBound) lo = mid + 1;\n else hi = mid;\n }\n kHigh = lo;\n }\n \n int newMax = nextRange[nextStart];\n for (int k = kLow; k < kHigh; k++){\n for (int rNew = nextStart; rNew <= newMax; rNew++){\n if(!visited[rNew][k]){\n visited[rNew][k] = true;\n dq.push_back({rNew, k});\n }\n }\n }\n }\n return false;\n}\n \nint main(){\n ios::sync_with_stdio(0);\n cin.tie(0);\n \n while(true){\n int N, M;\n cin >> N >> M;\n if(M==0) break;\n \n map<int, ll> catMap;\n for (int i = 0; i < N; i++){\n int x, y; ll c;\n cin >> x >> y >> c;\n catMap[x] += c;\n }\n vector<int> xGroups;\n vector<ll> weights;\n for(auto &p : catMap){\n xGroups.push_back(p.first);\n weights.push_back(p.second);\n }\n int n = xGroups.size();\n \n vector<int> bowls(M);\n for (int i = 0; i < M; i++) cin >> bowls[i];\n\n sort(bowls.begin(), bowls.end());\n \n vector<ll> prefix(n+1, 0);\n for (int i = 0; i < n; i++)\n prefix[i+1] = prefix[i] + weights[i];\n \n ll low = 0, high = prefix[n];\n for (int i = 0; i < n; i++) low = max(low, weights[i]);\n \n ll ans = high;\n while(low <= high){\n ll mid = (low + high) / 2;\n if(feasible(mid, xGroups, weights, prefix, bowls)){\n ans = mid;\n high = mid - 1;\n } else low = mid + 1;\n }\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1480, "memory_kb": 9284, "score_of_the_acc": -0.9611, "final_rank": 16 }, { "submission_id": "aoj_2211_9061913", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef int64_t ll;\n\n\nint main(){\n\n int n,m;\n do{\n cin >> n >> m;\n if(m==0) break;\n // cout << n << \" \" << m << endl;\n \n vector<pair<int,int>> cat(n);\n vector<int> e(m);\n int y,emax=0;;\n for(int i=0;i<n;i++){ \n cin >> cat[i].first >> y >> cat[i].second;\n emax+=cat[i].second;\n }\n for(int i=0;i<m;i++){ \n cin >> e[i];\n }\n\n if(n==0){\n cout << 0 << endl;\n continue;\n }\n\n if(m==1){\n cout << emax << endl;\n continue;\n }\n\n sort(e.begin(),e.end());\n vector<vector<pair<int,int>>> cate(n);\n for(int i=0;i<n;i++){\n for(int j=0;j<m;j++){\n cate[i].emplace_back(abs(cat[i].first-e[j]),j);\n }\n sort(cate[i].begin(),cate[i].end());\n }\n\n int lft=0,rit=emax;\n vector<int> tmp(m);\n while(rit-lft>1){\n vector<bool> used(m,true);\n vector<int> c(n,0);\n int mcnt=m,flg=0;\n \n int mid=(lft+rit)/2;\n \n while((flg==0)&&(mcnt>0)){\n tmp.assign(m,0);\n for(int i=0;i<n;i++){\n //cout << i << \" \" << cate[i].size() << \" \";\n while(!used[cate[i][c[i]].second]) c[i]++;\n //cout << c[i] << endl;\n tmp[cate[i][c[i]].second]+=cat[i].second;\n }\n flg=1;\n for(int i=0;i<m;i++){\n if(!used[i]) continue;\n if(tmp[i]>mid){\n used[i]=false;\n mcnt--;\n flg=0;\n }\n }\n }\n if(flg==1) rit=mid;\n else lft=mid;\n //cout << lft << \":\" << rit << endl; \n }\n cout << rit << endl;\n }while(true);\n return 0;\n \n}", "accuracy": 1, "time_ms": 500, "memory_kb": 11180, "score_of_the_acc": -0.9733, "final_rank": 17 }, { "submission_id": "aoj_2211_4380166", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 99999999999999999\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n#define SIZE 1005\n\nstruct Info{\n\n\tbool operator<(const struct Info &arg) const{\n\n\t\treturn x < arg.x;\n\t}\n\tint x,y,num;\n};\n\nint N,M;\nint X[SIZE],SUM[SIZE];\nint table_L[SIZE][SIZE],table_R[SIZE][SIZE];\nint dp[SIZE];\nInfo info[SIZE];\n\n\nbool isOK(int num){\n\n\tfor(int i = 0; i < M; i++){\n\n\t\tdp[i] = BIG_NUM;\n\t}\n\n\tint l,r,mid;\n\n\tif(info[N-1].x < X[M-1]){\n\n\t\tdp[M-1] = 0;\n\t}else{\n\n\t\tl = 0,r = N-1,mid = (l+r)/2;\n\t\tint L = r;\n\n\t\twhile(l <= r){\n\n\t\t\tif(info[mid].x >= X[M-1]){\n\n\t\t\t\tL = mid;\n\t\t\t\tr = mid-1; //より左へ\n\t\t\t}else{\n\n\t\t\t\tl = mid+1;\n\t\t\t}\n\t\t\tmid = (l+r)/2;\n\t\t}\n\n\t\tif(L == 0){\n\n\t\t\tdp[M-1] = SUM[N-1];\n\n\t\t}else{\n\n\t\t\tdp[M-1] = SUM[N-1]-SUM[L-1];\n\t\t}\n\t\tif(dp[M-1] > num){\n\n\t\t\treturn false;\n\t\t}\n\t}\n\n\tint R;\n\n\tfor(int i = M-1; i >= 0; i--){\n\t\tfor(int k = i+1; k < M; k++){\n\t\t\tif(dp[k]+table_R[i][k] > num)continue;\n\n\t\t\tdp[i] = min(dp[i],table_L[i][k]);\n\t\t}\n\n\t\tif(dp[i] > num)continue;\n\n\t\tif(info[0].x > X[i]){\n\n\t\t\treturn true;\n\t\t}\n\n\t\tl = 0,r = N-1,mid = (l+r)/2;\n\t\tR = 0;\n\n\t\twhile(l <= r){\n\n\t\t\tif(info[mid].x < X[i]){\n\n\t\t\t\tR = mid;\n\t\t\t\tl = mid+1;\n\t\t\t}else{\n\n\t\t\t\tr = mid-1;\n\t\t\t}\n\t\t\tmid = (l+r)/2;\n\t\t}\n\n\t\tif(SUM[R]+dp[i] <= num){\n\n\t\t\treturn true;\n\t\t}\n\t}\n\n\treturn false;\n}\n\n\nvoid func(){\n\n\tint total = 0;\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tscanf(\"%d %d %d\",&info[i].x,&info[i].y,&info[i].num);\n\t\ttotal += info[i].num;\n\t}\n\n\tfor(int i = 0; i < M; i++){\n\n\t\tscanf(\"%d\",&X[i]);\n\t}\n\n\tsort(info,info+N);\n\n\tSUM[0] = info[0].num;\n\tfor(int i = 1; i < N; i++){\n\n\t\tSUM[i] = SUM[i-1]+info[i].num;\n\t}\n\n\tsort(X,X+M);\n\n\tint L,R;\n\tint l,r,mid;\n\tint MID,center;\n\n\tfor(int left = 0; left < M-1; left++){\n\t\tfor(int right = left+1; right < M; right++){\n\n\t\t\tif(info[N-1].x < X[left] || info[0].x >= X[right]){\n\t\t\t\ttable_L[left][right] = 0;\n\t\t\t\ttable_R[left][right] = 0;\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tl = 0,r = N-1,mid = (l+r)/2;\n\t\t\tL = r;\n\n\t\t\twhile(l <= r){\n\n\t\t\t\tif(info[mid].x >= X[left]){\n\n\t\t\t\t\tL = mid;\n\t\t\t\t\tr = mid-1;\n\t\t\t\t}else{\n\n\t\t\t\t\tl = mid+1;\n\t\t\t\t}\n\t\t\t\tmid = (l+r)/2;\n\t\t\t}\n\n\t\t\tl = 0,r = N-1,mid=(l+r)/2;\n\t\t\tR = 0;\n\n\t\t\twhile(l <= r){\n\n\t\t\t\tif(info[mid].x < X[right]){\n\n\t\t\t\t\tR = mid;\n\t\t\t\t\tl = mid+1;\n\t\t\t\t}else{\n\n\t\t\t\t\tr = mid-1;\n\t\t\t\t}\n\t\t\t\tmid = (l+r)/2;\n\t\t\t}\n\n\t\t\tif(L > R){\n\n\t\t\t\ttable_L[left][right] = 0;\n\t\t\t\ttable_R[left][right] = 0;\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tMID = (X[left]+X[right])/2;\n\n\t\t\tif(X[right]-MID < MID-X[left]){\n\n\t\t\t\tMID--;\n\t\t\t}\n\n\t\t\tl = L,r = R,mid = (l+r)/2;\n\t\t\tcenter = -1;\n\t\t\twhile(l <= r){\n\t\t\t\tif(info[mid].x <= MID){\n\n\t\t\t\t\tcenter = mid;\n\t\t\t\t\tl = mid+1;\n\t\t\t\t}else{\n\n\t\t\t\t\tr = mid-1;\n\t\t\t\t}\n\t\t\t\tmid = (l+r)/2;\n\t\t\t}\n\n\t\t\tif(center == -1){\n\n\t\t\t\ttable_L[left][right] = 0;\n\t\t\t\tif(L == 0){\n\n\t\t\t\t\ttable_R[left][right] = SUM[R];\n\n\t\t\t\t}else{\n\n\t\t\t\t\ttable_R[left][right] = SUM[R]-SUM[L-1];\n\t\t\t\t}\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\ttable_R[left][right] = SUM[R]-SUM[center];\n\n\t\t\tif(L == 0){\n\n\t\t\t\ttable_L[left][right] = SUM[center];\n\t\t\t}else{\n\n\t\t\t\ttable_L[left][right] = SUM[center]-SUM[L-1];\n\t\t\t}\n\t\t}\n\t}\n\n\tl = 0,r = total,mid = (l+r)/2;\n\tint ans = r;\n\n\twhile(l <= r){\n\t\tif(isOK(mid)){\n\n\t\t\tans = mid;\n\t\t\tr = mid-1;\n\n\t\t}else{\n\n\t\t\tl = mid+1;\n\t\t}\n\t\tmid = (l+r)/2;\n\t}\n\n\tprintf(\"%d\\n\",ans);\n}\n\nint main(){\n\n\twhile(true){\n\t\tscanf(\"%d %d\",&N,&M);\n\t\tif(N == 0 && M == 0)break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 210, "memory_kb": 11008, "score_of_the_acc": -0.9115, "final_rank": 13 }, { "submission_id": "aoj_2211_4380164", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 99999999999999999\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n#define SIZE 1005\n\nstruct Info{\n\n\tbool operator<(const struct Info &arg) const{\n\n\t\treturn x < arg.x;\n\t}\n\tint x,y,num;\n};\n\nint N,M;\nint X[SIZE],SUM[SIZE];\nint table_L[SIZE][SIZE],table_R[SIZE][SIZE];\nint dp[SIZE];\nInfo info[SIZE];\n\n\nbool isOK(int num){\n\n\tfor(int i = 0; i < M; i++){\n\n\t\tdp[i] = BIG_NUM;\n\t}\n\n\tint l,r,mid;\n\n\tif(info[N-1].x < X[M-1]){\n\n\t\tdp[M-1] = 0;\n\t}else{\n\n\t\tl = 0,r = N-1,mid = (l+r)/2;\n\t\tint L = r;\n\n\t\twhile(l <= r){\n\n\t\t\tif(info[mid].x >= X[M-1]){\n\n\t\t\t\tL = mid;\n\t\t\t\tr = mid-1; //より左へ\n\t\t\t}else{\n\n\t\t\t\tl = mid+1;\n\t\t\t}\n\t\t\tmid = (l+r)/2;\n\t\t}\n\n\t\tif(L == 0){\n\n\t\t\tdp[M-1] = SUM[N-1];\n\n\t\t}else{\n\n\t\t\tdp[M-1] = SUM[N-1]-SUM[L-1];\n\t\t}\n\t\tif(dp[M-1] > num){ //最右端の猫の数は純増なので、最小の場合が不可ならfalse\n\n\t\t\treturn false;\n\t\t}\n\t}\n\n\tint R;\n\n\tfor(int i = M-1; i >= 0; i--){\n\t\tfor(int k = i+1; k < M; k++){\n\t\t\tif(dp[k]+table_R[i][k] > num)continue;\n\n\t\t\tdp[i] = min(dp[i],table_L[i][k]);//地点iの直近の右をkとした時の、右から入ってくる猫\n\t\t}\n\n\t\tif(dp[i] > num)continue;\n\n\t\tif(info[0].x > X[i]){ //自分より左に猫がいない\n\n\t\t\treturn true;\n\t\t}\n\n\t\tl = 0,r = N-1,mid = (l+r)/2;\n\t\tR = 0;\n\n\t\twhile(l <= r){\n\n\t\t\tif(info[mid].x < X[i]){\n\n\t\t\t\tR = mid;\n\t\t\t\tl = mid+1;\n\t\t\t}else{\n\n\t\t\t\tr = mid-1;\n\t\t\t}\n\t\t\tmid = (l+r)/2;\n\t\t}\n\n\t\tif(SUM[R]+dp[i] <= num){ //iが左端となれば、適切な割り当て可能\n\n\t\t\treturn true;\n\t\t}\n\t}\n\n\treturn false;\n}\n\n\nvoid func(){\n\n\tint total = 0;\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tscanf(\"%d %d %d\",&info[i].x,&info[i].y,&info[i].num);\n\t\ttotal += info[i].num;\n\t}\n\n\tfor(int i = 0; i < M; i++){\n\n\t\tscanf(\"%d\",&X[i]);\n\t}\n\n\tsort(info,info+N);\n\n\tSUM[0] = info[0].num;\n\tfor(int i = 1; i < N; i++){\n\n\t\tSUM[i] = SUM[i-1]+info[i].num;\n\t}\n\n\tsort(X,X+M);\n\n\tint L,R;\n\tint l,r,mid;\n\tint MID,center;\n\n\tfor(int left = 0; left < M-1; left++){\n\t\tfor(int right = left+1; right < M; right++){\n\n\t\t\t//両地点の間に猫がいないならSKIP\n\t\t\tif(info[N-1].x < X[left] || info[0].x >= X[right]){\n\t\t\t\ttable_L[left][right] = 0;\n\t\t\t\ttable_R[left][right] = 0;\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tl = 0,r = N-1,mid = (l+r)/2;\n\t\t\tL = r;\n\n\t\t\twhile(l <= r){\n\n\t\t\t\tif(info[mid].x >= X[left]){\n\n\t\t\t\t\tL = mid;\n\t\t\t\t\tr = mid-1;\n\t\t\t\t}else{\n\n\t\t\t\t\tl = mid+1;\n\t\t\t\t}\n\t\t\t\tmid = (l+r)/2;\n\t\t\t}\n\n\t\t\tl = 0,r = N-1,mid=(l+r)/2;\n\t\t\tR = 0;\n\n\t\t\twhile(l <= r){\n\n\t\t\t\tif(info[mid].x < X[right]){\n\n\t\t\t\t\tR = mid;\n\t\t\t\t\tl = mid+1;\n\t\t\t\t}else{\n\n\t\t\t\t\tr = mid-1;\n\t\t\t\t}\n\t\t\t\tmid = (l+r)/2;\n\t\t\t}\n\n\t\t\tif(L > R){ //両地点の間に猫なし\n\n\t\t\t\ttable_L[left][right] = 0;\n\t\t\t\ttable_R[left][right] = 0;\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tMID = (X[left]+X[right])/2;\n\n\t\t\t//和がマイナスの場合あり\n\t\t\tif(X[right]-MID < MID-X[left]){\n\n\t\t\t\tMID--;\n\t\t\t}\n\n\t\t\t//x座標がMID以下となる最大の位置\n\t\t\tl = L,r = R,mid = (l+r)/2;\n\t\t\tcenter = -1;\n\t\t\twhile(l <= r){\n\t\t\t\tif(info[mid].x <= MID){\n\n\t\t\t\t\tcenter = mid;\n\t\t\t\t\tl = mid+1;\n\t\t\t\t}else{\n\n\t\t\t\t\tr = mid-1;\n\t\t\t\t}\n\t\t\t\tmid = (l+r)/2;\n\t\t\t}\n\n\t\t\tif(center == -1){ //区間の全てがrightへ向かう\n\n\t\t\t\ttable_L[left][right] = 0;\n\t\t\t\tif(L == 0){\n\n\t\t\t\t\ttable_R[left][right] = SUM[R];\n\n\t\t\t\t}else{\n\n\t\t\t\t\ttable_R[left][right] = SUM[R]-SUM[L-1];\n\t\t\t\t}\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\ttable_R[left][right] = SUM[R]-SUM[center]; //rightの左からrightへ向かう\n\n\t\t\tif(L == 0){\n\n\t\t\t\ttable_L[left][right] = SUM[center]; //leftの右からleftへ向かう\n\t\t\t}else{\n\n\t\t\t\ttable_L[left][right] = SUM[center]-SUM[L-1];\n\t\t\t}\n\t\t}\n\t}\n\n\tl = 0,r = total,mid = (l+r)/2;\n\tint ans = r;\n\n\twhile(l <= r){\n\t\tif(isOK(mid)){\n\n\t\t\tans = mid;\n\t\t\tr = mid-1;\n\n\t\t}else{\n\n\t\t\tl = mid+1;\n\t\t}\n\t\tmid = (l+r)/2;\n\t}\n\n\tprintf(\"%d\\n\",ans);\n}\n\nint main(){\n\n\twhile(true){\n\t\tscanf(\"%d %d\",&N,&M);\n\t\tif(N == 0 && M == 0)break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 210, "memory_kb": 11144, "score_of_the_acc": -0.9236, "final_rank": 15 }, { "submission_id": "aoj_2211_4380163", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 99999999999999999\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n#define SIZE 1005\n\nstruct Info{\n\n\tbool operator<(const struct Info &arg) const{\n\n\t\treturn x < arg.x;\n\t}\n\tint x,y,num;\n};\n\nint N,M;\nint X[SIZE],SUM[SIZE];\nint table_L[SIZE][SIZE],table_R[SIZE][SIZE];\nint dp[SIZE];\nInfo info[SIZE];\n\n\nbool isOK(int num){\n\n\tfor(int i = 0; i < M; i++){\n\n\t\tdp[i] = BIG_NUM;\n\t}\n\n\tint l,r,mid;\n\n\tif(info[N-1].x < X[M-1]){\n\n\t\tdp[M-1] = 0;\n\t}else{\n\n\t\tl = 0,r = N-1,mid = (l+r)/2;\n\t\tint L = r;\n\n\t\twhile(l <= r){\n\n\t\t\tif(info[mid].x >= X[M-1]){\n\n\t\t\t\tL = mid;\n\t\t\t\tr = mid-1; //より左へ\n\t\t\t}else{\n\n\t\t\t\tl = mid+1;\n\t\t\t}\n\t\t\tmid = (l+r)/2;\n\t\t}\n\n\t\tif(L == 0){\n\n\t\t\tdp[M-1] = SUM[N-1];\n\n\t\t}else{\n\n\t\t\tdp[M-1] = SUM[N-1]-SUM[L-1];\n\t\t}\n\t\tif(dp[M-1] > num){ //最右端の猫の数は純増なので、最小の場合が不可ならfalse\n\n\t\t\treturn false;\n\t\t}\n\t}\n\n\tint R;\n\n\tfor(int i = M-1; i >= 0; i--){\n\t\tfor(int k = i+1; k < M; k++){\n\t\t\tif(dp[k]+table_R[i][k] > num)continue;\n\n\t\t\tdp[i] = min(dp[i],table_L[i][k]);//地点iの直近の右をkとした時の、右から入ってくる猫\n\t\t}\n\n\t\tif(dp[i] > num)continue;\n\n\t\tif(info[0].x > X[i]){ //自分より左に猫がいない\n\n\t\t\treturn true;\n\t\t}\n\n\t\t//printf(\"dp[%d]:%d\\n\",i,dp[i]);\n\n\t\tl = 0,r = N-1,mid = (l+r)/2;\n\t\tR = 0;\n\n\t\twhile(l <= r){\n\n\t\t\tif(info[mid].x < X[i]){\n\n\t\t\t\tR = mid;\n\t\t\t\tl = mid+1;\n\t\t\t}else{\n\n\t\t\t\tr = mid-1;\n\t\t\t}\n\t\t\tmid = (l+r)/2;\n\t\t}\n\n\t\tif(SUM[R]+dp[i] <= num){ //iが左端となれば、適切な割り当て可能\n\n\t\t\treturn true;\n\t\t}\n\t}\n\n\treturn false;\n}\n\n\nvoid func(){\n\n\tint total = 0;\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tscanf(\"%d %d %d\",&info[i].x,&info[i].y,&info[i].num);\n\t\ttotal += info[i].num;\n\t}\n\n\t//printf(\"total:%d\\n\",total);\n\tfor(int i = 0; i < M; i++){\n\n\t\tscanf(\"%d\",&X[i]);\n\t}\n\n\tsort(info,info+N);\n\n\tSUM[0] = info[0].num;\n\tfor(int i = 1; i < N; i++){\n\n\t\tSUM[i] = SUM[i-1]+info[i].num;\n\t}\n\n\tsort(X,X+M);\n\n\tint L,R;\n\tint l,r,mid;\n\tint MID,center;\n\n\tfor(int left = 0; left < M-1; left++){\n\t\tfor(int right = left+1; right < M; right++){\n\n\t\t\t//両地点の間に猫がいないならSKIP\n\t\t\tif(info[N-1].x < X[left] || info[0].x >= X[right]){\n\t\t\t\ttable_L[left][right] = 0;\n\t\t\t\ttable_R[left][right] = 0;\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tl = 0,r = N-1,mid = (l+r)/2;\n\t\t\tL = r;\n\n\t\t\twhile(l <= r){\n\n\t\t\t\tif(info[mid].x >= X[left]){\n\n\t\t\t\t\tL = mid;\n\t\t\t\t\tr = mid-1;\n\t\t\t\t}else{\n\n\t\t\t\t\tl = mid+1;\n\t\t\t\t}\n\t\t\t\tmid = (l+r)/2;\n\t\t\t}\n\n\t\t\tl = 0,r = N-1,mid=(l+r)/2;\n\t\t\tR = 0;\n\n\t\t\twhile(l <= r){\n\n\t\t\t\tif(info[mid].x < X[right]){\n\n\t\t\t\t\tR = mid;\n\t\t\t\t\tl = mid+1;\n\t\t\t\t}else{\n\n\t\t\t\t\tr = mid-1;\n\t\t\t\t}\n\t\t\t\tmid = (l+r)/2;\n\t\t\t}\n\n\t\t\tif(L > R){ //両地点の間に猫なし\n\n\t\t\t\ttable_L[left][right] = 0;\n\t\t\t\ttable_R[left][right] = 0;\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tMID = (X[left]+X[right])/2;\n\n\t\t\t//和がマイナスの場合あり\n\t\t\tif(X[right]-MID < MID-X[left]){\n\n\t\t\t\tMID--;\n\t\t\t}\n\n\t\t\t/*printf(\"\\nX[left]:%d X[right]:%d\\n\",X[left],X[right]);\n\t\t\t\t\t\tprintf(\"L:%d R:%d MID:%d\\n\",L,R,MID);*/\n\n\n\t\t\t//x座標がMID以下となる最大の位置\n\t\t\tl = L,r = R,mid = (l+r)/2;\n\t\t\tcenter = -1;\n\t\t\twhile(l <= r){\n\t\t\t\tif(info[mid].x <= MID){\n\n\t\t\t\t\tcenter = mid;\n\t\t\t\t\tl = mid+1;\n\t\t\t\t}else{\n\n\t\t\t\t\tr = mid-1;\n\t\t\t\t}\n\t\t\t\tmid = (l+r)/2;\n\t\t\t}\n\n\t\t\t//printf(\"center:%d\\n\",center);\n\n\t\t\tif(center == -1){ //区間の全てがrightへ向かう\n\n\t\t\t\ttable_L[left][right] = 0;\n\t\t\t\tif(L == 0){\n\n\t\t\t\t\ttable_R[left][right] = SUM[R];\n\n\t\t\t\t}else{\n\n\t\t\t\t\ttable_R[left][right] = SUM[R]-SUM[L-1];\n\t\t\t\t}\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\ttable_R[left][right] = SUM[R]-SUM[center]; //rightの左からrightへ向かう\n\n\t\t\tif(L == 0){\n\n\t\t\t\ttable_L[left][right] = SUM[center]; //leftの右からleftへ向かう\n\t\t\t}else{\n\n\t\t\t\ttable_L[left][right] = SUM[center]-SUM[L-1];\n\t\t\t}\n\n\t\t\t//printf(\"L:%d R:%d\\n\",table_L[left][right],table_R[left][right]);\n\t\t}\n\t}\n\n\tl = 0,r = total,mid = (l+r)/2;\n\t//l = 29,r = 29,mid = (l+r)/2;\n\tint ans = r;\n\n\twhile(l <= r){\n\t\tif(isOK(mid)){\n\n\t\t\tans = mid;\n\t\t\tr = mid-1;\n\n\t\t}else{\n\n\t\t\tl = mid+1;\n\t\t}\n\t\tmid = (l+r)/2;\n\t}\n\n\tprintf(\"%d\\n\",ans);\n}\n\nint main(){\n\n\twhile(true){\n\t\tscanf(\"%d %d\",&N,&M);\n\t\tif(N == 0 && M == 0)break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 210, "memory_kb": 11024, "score_of_the_acc": -0.9129, "final_rank": 14 }, { "submission_id": "aoj_2211_3699256", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing Int = long long;\nusing ll = long long;\n\ntemplate<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b)a=b;};\ntemplate<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b)a=b;};\n\nll n,m;\nvector<pair<ll,ll>> C;\nvector<ll> A;\nconst ll INF=1e9;\n\nbool judge(ll x){\n vector<ll> dp(m,INF);\n //(l,r]\n auto cul=[&](ll l,ll r){\n auto I=--lower_bound(C.begin(),C.end(),pair<ll,ll>(r,INF));\n auto J=--lower_bound(C.begin(),C.end(),pair<ll,ll>(l,INF));\n return (I->second)-(J->second);\n };\n dp[0]=cul(-INF,A[0]);\n for(Int i=1;i<m;i++){\n for(Int j=0;j<i;j++){\n ll m=(A[j]+A[i])/2;\n \n if(dp[j]+cul(A[j],m)<=x){\n dp[i]=min(dp[i],cul(m,A[i]));\n }\n //cout<<j<<\" \"<<i<<\":\"<<dp[j]<<\" \"<<dp[i]<<\":\"<<m<<\":\";\n //cout<<cul(A[j],m)<<\" \"<<cul(m,A[i])<<endl;\n }\n }\n return dp.back()+cul(A.back(),INF)<=x;\n}\n\n\nsigned main(){\n cin.tie(0);\n ios::sync_with_stdio(0);\n while(cin>>n>>m,m){\n C.resize(n);\n A.resize(m);\n for(Int i=0;i<n;i++){\n Int y;\n cin>>C[i].first>>y>>C[i].second;\n C[i].first*=2;\n }\n for(Int i=0;i<m;i++){\n cin>>A[i];\n A[i]*=2;\n }\n if(n==0){cout<<0<<endl; continue;}\n sort(A.begin(),A.end());\n C.emplace_back(-2*INF,0);\n sort(C.begin(),C.end());\n for(Int i=1;i<=n;i++){C[i].second+=C[i-1].second;}\n C.push_back(C[n]);\n C.back().first=INF*2;\n //for(auto p:C) cout<<p.first<<\" \"<<p.second<<endl;\n\n // judge(2);\n \n ll l=0,r=INF;\n while(l+1<r){\n ll m=(l+r)>>1;\n if(judge(m)){r=m;}\n else{l=m;}\n }\n cout<<r<<endl; \n }\n \n \n return 0;\n}", "accuracy": 1, "time_ms": 5520, "memory_kb": 3156, "score_of_the_acc": -1.0616, "final_rank": 20 }, { "submission_id": "aoj_2211_1193179", "code_snippet": "//Name: Stray Cats, Run...\n//Level: 4\n//Category: 最大値最小化,二分探索,Binary search,貪欲,Greedy\n//Note: \n\n/**\n * 最大値の最小化なので、最大値について二分探索する。\n *\n * 最初にすべての餌入れに餌を入れるものとする。\n * このとき、最大値を超えて集まっている餌入れがあった場合、その餌入れは使わないようにしないと解消できない。\n * したがって、その餌入れを除いて再配置する。\n * これを繰り返し、条件を満たすことができるかを試していく。\n *\n * 再配置はナイーブにやると O(NM) かかるが、あらかじめ猫がいる全ての点に対して、餌入れを近い順にソートしておくと\n * 1つの最大値についての計算全体で O(NM) にできる。\n *\n * オーダーは O((N^2 + NM) log MC)。\n */\n#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <cassert>\n\nusing namespace std;\n\ntypedef pair<long long,long long> P;\n\nstruct Cat {\n P pos;\n int c;\n};\n\ninline long long norm(const P &a, const P &b) {\n const auto dx = a.first - b.first;\n const auto dy = a.second - b.second;\n return dx*dx + dy*dy;\n}\n\nbool solve(bool first) {\n int N, M;\n if(!(cin >> N >> M)) return false;\n if(!N && !M) return false;\n\n vector<Cat> cats(N);\n long long cat_sum = 0;\n for(int i = 0; i < N; ++i) {\n long long x, y;\n int c;\n cin >> x >> y >> c;\n cats[i] = Cat{P(x, y), c};\n cat_sum += c;\n }\n\n vector<P> ps(M);\n for(int i = 0; i < M; ++i) {\n long long x;\n cin >> x;\n ps[i] = P(x, 0);\n }\n sort(begin(ps), end(ps));\n\n vector<vector<int>> ord(N);\n for(int i = 0; i < N; ++i) {\n vector<pair<long long,int>> v;\n for(int j = 0; j < M; ++j) {\n v.emplace_back(norm(cats[i].pos, ps[j]), j);\n }\n sort(begin(v), end(v));\n for(auto e : v) {\n ord[i].push_back(e.second);\n }\n }\n\n long long left = 0, right = cat_sum;\n vector<long long> buf(M, 0);\n while(left+1 < right) {\n const long long LIM = (left + right) / 2;\n vector<bool> avail(M, true);\n vector<int> cursor(N, 0);\n int remain = M;\n bool ok = false;\n while(!ok && remain > 0) {\n fill(begin(buf), end(buf), 0);\n for(int i = 0; i < N; ++i) {\n while(!avail[ord[i][cursor[i]]]) ++cursor[i];\n assert(cursor[i] < ord[i].size());\n const int dest = ord[i][cursor[i]];\n buf[dest] += cats[i].c;\n }\n ok = true;\n for(int i = 0; i < M; ++i) {\n if(buf[i] > LIM) {\n avail[i] = false;\n --remain;\n ok = false;\n }\n }\n }\n if(ok) {\n right = LIM;\n } else {\n left = LIM;\n }\n }\n cout << right << endl;\n return true;\n}\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(0);\n cout.setf(ios::fixed);\n cout.precision(10);\n\n bool first = true;\n while(solve(first)) {\n first = false;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1050, "memory_kb": 5248, "score_of_the_acc": -0.532, "final_rank": 11 }, { "submission_id": "aoj_2211_569159", "code_snippet": "#include<cstdio>\n#include<algorithm>\n\n#define rep(i,n) for(int i=0;i<(n);i++)\n\nusing namespace std;\n\nconst int INF=1<<29;\n\nvoid pairsort(int n,int *a,int *b){\n\tpair<int,int> c[1000];\n\trep(i,n) c[i]=make_pair(a[i],b[i]);\n\tsort(c,c+n);\n\trep(i,n) a[i]=c[i].first, b[i]=c[i].second;\n}\n\nint n,m;\nint x[1000],num[1000],esa[1000];\n\nint sum[1001];\nint count_cat(int l,int r){ // [l, r] にいるねこの数\n\tint i=lower_bound(x,x+n,l)-x;\n\tint j=upper_bound(x,x+n,r)-x;\n\treturn sum[j]-sum[i];\n}\n\nbool check(int ub){\n\t// dp[i] := ( esa[i] にごはんを置いたときに右から来るねこの最小数 )\n\tint dp[1000];\n\tdp[m-1]=count_cat(esa[m-1]+1,20000);\n\tfor(int i=m-2;i>=0;i--){\n\t\tdp[i]=INF;\n\t\tfor(int j=i+1;j<m;j++){\n\t\t\tint a=count_cat(esa[i]+1,(esa[i]+esa[j])/2); // (esa[i], (esa[i]+esa[j])/2] にいるねこの数\n\t\t\tint b=count_cat((esa[i]+esa[j])/2+1,esa[j]); // ((esa[i]+esa[j])/2, esa[j]] にいるねこの数\n\t\t\tif(b+dp[j]<=ub){\n\t\t\t\tdp[i]=min(dp[i],a);\n\t\t\t}\n\t\t}\n\t}\n\n\trep(i,m){\n\t\tif(dp[i]+count_cat(0,esa[i])<=ub) return true;\n\t}\n\treturn false;\n}\n\nint main(){\n\twhile(scanf(\"%d%d\",&n,&m),m){\n\t\trep(i,n) scanf(\"%d%*d%d\",x+i,num+i), x[i]+=10000;\n\t\trep(i,m) scanf(\"%d\",esa+i), esa[i]+=10000; // あとで /2 するとき 0 方向へ丸められるのがイヤなので >=0 にしておく\n\n\t\tpairsort(n,x,num);\n\t\tsort(esa,esa+m);\n\n\t\trep(i,n) sum[i+1]=sum[i]+num[i];\n\n\t\tint lo=0,hi=100000000;\n\t\twhile(lo<hi){\n\t\t\tint mi=(lo+hi)/2;\n\t\t\tif(check(mi)) hi=mi; else lo=mi+1;\n\t\t}\n\t\tprintf(\"%d\\n\",lo);\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 6450, "memory_kb": 1072, "score_of_the_acc": -1.0246, "final_rank": 18 }, { "submission_id": "aoj_2211_558303", "code_snippet": "#include <iostream>\n#include <cstdio>\n#include <vector>\n#include <algorithm>\n#include <complex>\n#include <queue>\n#include <map>\n#include <set>\n#include <cstring>\n#include <cstdlib>\n#include <string>\n#include <cmath>\nusing namespace std;\n\n#define REP(i,n) for(int i=0;i<(int)n;++i)\n#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)\n#define ALL(c) (c).begin(), (c).end()\n#define chmax(a,b) (a<b?(a=b,1):0)\n#define chmin(a,b) (a>b?(a=b,1):0)\n#define valid(y,x,h,w) (0<=y&&y<h&&0<=x&&x<w)\nconst int INF = 1<<30;\n\nint n,m;\nstruct Cat {\n int x,y,C;\n Cat() {}\n Cat(int x, int C) :x(x),C(C) {}\n bool operator<(const Cat &b) const {\n return x != b.x ? x<b.x : C<b.C;\n }\n} cat[1000];\nint X[1000];\nint dp[1000];\nint sum[1001];\nint numL[1000][1000];\nint numR[1000][1000];\n\nbool ok(int k) {\n REP(i,m) dp[i] = INF;\n int c = upper_bound(cat,cat+n,Cat(X[m-1],INF))-cat;\n dp[m-1] = sum[n]-sum[c];\n for (int i=m-1; i>=0; --i) {\n for (int j=i+1; j<m; ++j) {\n if (numR[i][j]+dp[j] <= k)\n chmin(dp[i], numL[i][j]);\n }\n int a = upper_bound(cat,cat+n,Cat(X[i],INF))-cat;\n if (dp[i] + sum[a] <= k) return 1;\n }\n return 0;\n}\n\nint main() {\n while(cin>>n>>m,n||m) {\n REP(i,n) cin>>cat[i].x>>cat[i].y>>cat[i].C;\n sort(cat,cat+n);\n REP(i,m) cin>>X[i];\n sort(X,X+m);\n REP(i,n) {\n sum[i+1] = sum[i] + cat[i].C;\n }\n // int id=0;\n // REP(i,m) {\n // while(id<n && cat[id].x<=X[i]) id++;\n // sum2[i] = sum[id];\n // }\n for (int i=0; i<m-1; ++i) {\n for (int j=i+1; j<m; ++j) {\n int a = upper_bound(cat,cat+n,Cat(X[i],INF))-cat;\n int mid = (X[i]+X[j])/2;\n if (X[i]+X[j]<0) mid = (X[i]+X[j]-1)/2;\n int b = upper_bound(cat,cat+n,Cat(mid,INF))-cat;\n int c = upper_bound(cat,cat+n,Cat(X[j],INF))-cat;\n numL[i][j] = sum[b]-sum[a];\n numR[i][j] = sum[c]-sum[b];\n // cout << i << \" \" << j << \" \" << numL[i][j] << \" \" << numR[i][j] << endl;\n }\n }\n int low=-1,high=INF;\n while(low+1<high) {\n int mid = (low+high)/2;\n if (ok(mid)) high = mid;\n else low = mid;\n }\n cout << high << endl;\n }\n}", "accuracy": 1, "time_ms": 290, "memory_kb": 9016, "score_of_the_acc": -0.7465, "final_rank": 12 }, { "submission_id": "aoj_2211_451173", "code_snippet": "#include <cstdio>\n#include <iostream>\n#include <sstream>\n#include <iomanip>\n#include <algorithm>\n#include <cmath>\n#include <string>\n#include <vector>\n#include <list>\n#include <queue>\n#include <stack>\n#include <set>\n#include <map>\n#include <bitset>\n#include <numeric>\n#include <climits>\n#include <cfloat>\nusing namespace std;\n\nint main()\n{\n for(;;){\n int n, m; // 猫の集団の数、餌入れの数\n cin >> n >> m;\n if(m == 0)\n return 0;\n\n vector<int> cx(n), cy(n), catNum(n);\n for(int i=0; i<n; ++i)\n cin >> cx[i] >> cy[i] >> catNum[i];\n\n vector<int> x(m);\n for(int i=0; i<m; ++i)\n cin >> x[i];\n\n if(n == 0){\n cout << 0 << endl;\n continue;\n }\n\n vector<vector<pair<pair<int, int>, int> > > dist(n, vector<pair<pair<int, int>, int> >(m));\n vector<int> eat(m, 0);\n for(int i=0; i<n; ++i){\n for(int j=0; j<m; ++j){\n dist[i][j].first.first = (cx[i] - x[j]) * (cx[i] - x[j]) + cy[i] * cy[i];\n dist[i][j].first.second = x[j];\n dist[i][j].second = j;\n }\n sort(dist[i].begin(), dist[i].end());\n eat[dist[i][0].second] += catNum[i];\n }\n\n vector<int> select(n, 0);\n vector<bool> erase(m, false);\n int ret = *max_element(eat.begin(), eat.end());\n for(int i=0; i<m-1; ++i){\n int k = max_element(eat.begin(), eat.end()) - eat.begin();\n erase[k] = true;\n for(int j=0; j<n; ++j){\n eat[dist[j][select[j]].second] -= catNum[j];\n while(erase[dist[j][select[j]].second])\n ++ select[j];\n eat[dist[j][select[j]].second] += catNum[j];\n }\n ret = min(ret, *max_element(eat.begin(), eat.end()));\n }\n\n cout << ret << endl;\n }\n}", "accuracy": 1, "time_ms": 500, "memory_kb": 12000, "score_of_the_acc": -1.0465, "final_rank": 19 }, { "submission_id": "aoj_2211_353428", "code_snippet": "//include\n//------------------------------------------\n#include <vector>\n#include <list>\n#include <map>\n#include <set>\n#include <stack>\n#include <queue>\n#include <bitset>\n#include <algorithm>\n#include <numeric>\n#include <utility>\n#include <sstream>\n#include <iostream>\n#include <iomanip>\n#include <cstdio>\n#include <cmath>\n#include <string>\n\nusing namespace std;\n\n//conversion\n//------------------------------------------\ninline int toInt(string s) {int v; istringstream sin(s); sin >> v; return v;}\ntemplate<class T> inline string toStr(T x) {ostringstream sout; sout << x; return sout.str();}\n\n//math\n//-------------------------------------------\ntemplate<class T> inline T sqr(T x) {return x*x;}\n\n//typedef\n//------------------------------------------\ntypedef vector<int> VI;\ntypedef vector<VI> VVI;\ntypedef vector<string> VS;\ntypedef pair<int, int> PII;\ntypedef long long LL;\n\n//container util\n//------------------------------------------\n#define ALL(a, n) (a), (a) + (n))\n#define VALL(a) (a).begin(),(a).end()\n#define VADD(v, e) (v).push_back(e)\n#define SADD(s, e) (s).insert(e)\n#define STADD(s, e) (s).push(e)\n#define QADD(q, e) (q).push(e)\n#define PQADD(q, e) (q).push(e)\n#define STPOP(s, e) typeof((q).front()) e = (q).front(); (q).pop()\n#define QPOP(q) typeof((q).front()) e = (q).front(); (q).pop()\n#define PQPOP(q) typeof((q).top()) e = (q).front(); (q).pop()\n#define MP make_pair\n#define PF(p) (p).first\n#define PS(p) (p).second\n#define SZ(a) int((a).size())\n#define EACH(i, c) for(typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)\n#define EXIST(s, e) ((s).find(e) != (s).end())\n#define VSORT(c) sort(VALL(c))\n#define RSORT(a, n) sort(ALL(a, n), greater<typeof(a[0])>())\n#define VRSORT(c) sort(VALL(c), greater<typeof(a[0])>())\n#define INDEX(a, n, x) find(ALL(a, n), x) - a\n#define VINDEX(v, x) find(VALL(v), x) - v.begin()\n#define BOUND(a, n, x) lower_bound(ALL(a, n), x) - a\n#define VBOUND(v, x) lower_bound(VALL(v), x) - v.begin()\n\n//repetition\n//------------------------------------------\n#define FOR(i, a, b) for (int i = (a);i < (b); ++i)\n#define RFOR(i, a, b) for (int i = (b) - 1; i >= (a); --i)\n#define REP(i, n) FOR(i, 0, n)\n#define RREP(i, n) RFOR(i, 0, n)\n\n//IO\n//------------------------------------------\n#define LF(x) cout << (x) << endl;\n#define LF2(x, y) cout << (x) << \" \" << (y) << endl;\n#define LFD(x, w) cout.width = (w); cout << (x) << endl;\n#define LFDA(a, n, w) REP(i, n) cout.width = (w); cout << a[i] << endl;\n#define LFP(x, w) cout << setprecision((w)); cout << setiosflags(ios::fixed); cout << (x) << endl;\n#define LFP2(x, y, w) cout << setprecision((w)); cout << setiosflags(ios::fixed); cout << (x) << \" \" << (y) << endl;\n\n//constant\n//--------------------------------------------\nconst double EPS = 1e-10;\nconst double PI = acos(-1.0);\nconst int INF = 1e9;\n\n//clear memory\n//--------------------------------------------\n#define CLR(a) memset((a), 0 , sizeof(a))\n\n//debug\n//--------------------------------------------\n#define DUMP(x) cerr << #x << \" = \" << (x) << endl;\n#define DEBUG(x) cerr << #x << \" = \" << (x) << \" (L\" << __LINE__ << \")\" << endl;\n#define DUMPA(a, n) cerr << #a << \" = {\" << a[0]; FOR(i, 1, n) { cout << \", \" << a[i]; } cerr << \"}\" << endl;\n#define DEBUGA(a, n) cerr << #a << \" = {\" << a[0]; FOR(i, 1, n) { cout << \", \" << a[i]; } cerr << \"} (L\" << __LINE__ << \")\" << endl;\n#define DUMPAA(a, n, m) REP(i, n) {REP(j, m) {cout << a[i][j] << \" \";} cout << endl;}\n#define DEBUGAA(a, n, m) DEUMPAA(a, n, m) cout << \"(L\" << __LINE__ << \")\" << endl;\n\nconst int N = 1000;\nconst int M = 1000;\n\nint n, m;\nint x[N];\nint y[N];\nint c[N];\nint fx[M];\nmap<int, int> dp;\nmap<int, int> dp2;\n\nvoid init()\n{\n}\n\nvoid solve()\n{\n\tdp.clear();\n\tdp2.clear();\n\tREP(i, m) {\n\t\tdp[fx[i]] = 0;\n\t\tdp2[-fx[i]] = 0;\n\t}\n\tdp[INF] = 0;\n\tdp[-INF] = 0;\n\tint ans = INF;\n\tREP(i, m) {\n\t\tEACH(j, dp) {\n\t\t\tdp[PF(*j)] = 0;\n\t\t\tdp2[-PF(*j)] = 0;\n\t\t}\n\t\tREP(j, n) {\n\t\t\tmap<int, int>::iterator i1 = dp.lower_bound(x[j]);\n\t\t\tmap<int, int>::iterator i2 = dp2.upper_bound(-x[j]);\n\t\t\tint x1 = PF(*i1);\n\t\t\tint x2 = -PF(*i2);\n\t\t\tif (x1 - x[j] < x[j] - x2) {\n\t\t\t\tdp[x1] += c[j];\n\t\t\t}\n\t\t\telse {\n\t\t\t\tdp[x2] += c[j];\n\t\t\t}\n\t\t}\n\t\tint u = 0;\n\t\tint k;\n\t\tEACH(j, dp) {\n\t\t\tif (u < PS(*j)) {\n\t\t\t\tu = PS(*j);\n\t\t\t\tk = PF(*j);\n\t\t\t}\n\t\t}\n\t\tans = min(ans, u);\n\t\tdp.erase(k);\n\t\tdp2.erase(-k);\n\t}\n\tLF(ans);\n}\n\nint main()\n{\n\tinit();\n\twhile (cin >> n >> m, m) {\n\t\tREP(i, n) cin >> x[i] >> y[i] >> c[i];\n\t\tREP(i, m) cin >> fx[i];\n\t\tsolve();\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 890, "memory_kb": 1000, "score_of_the_acc": -0.1272, "final_rank": 6 }, { "submission_id": "aoj_2211_328661", "code_snippet": "//BinarySearch + DynamicProgramming\n//\n#include<iostream>\n#include<cassert>\n#include<cstdlib>\n#include<algorithm>\n#include<numeric>\nusing namespace std;\n#define REP(i,b,n) for(int i=b;i<n;i++)\n#define rep(i,n) REP(i,0,n)\ntypedef long long ll;\nconst int N = 1000;\nint adj[N][N];\nint r[N];\n\nint bestpoint(int n,int *esa,int l,int y,int x,ll tdist){\n int r=n-1;\n int ret=-1;\n if (esa[r] < x)return r;\n while(l <= r){\n int mid=(l+r)/2;\n ll mdist=esa[mid]-x;\n assert(mdist >= 0);\n if (mdist < tdist){\n ret=mid;\n l=mid+1;\n }else {\n r=mid-1;\n }\n }\n return ret;\n}\n\nvoid setdata(int n,int *esa,int m,int *y,int *x,int *num){\n rep(i,n){\n REP(j,i+1,n)adj[i][j]=0;\n rep(j,m){\n if (esa[i] < x[j]){\n\tll tdist=llabs(esa[i]-x[j]);\n\tint ind=bestpoint(n,esa,r[j],y[j],x[j],tdist);\n\t//cout <<\"check \" << i <<\" \" << j <<\" : \" << esa[i] <<\" \" << x[j] << \" \" << ind << endl;\n\tif (ind == -1)ind=r[j]-1;\n\tif (ind >= 0)adj[i][ind]+=num[j];\n }\n }\n for(int j=n-2;j>i;j--){\n adj[i][j]+=adj[i][j+1];\n }\n\n /*\n for(int k=m-1;k>=0;k--){\n ll tmp=llabs(esa[i]-x[k]);\n REP(j,i+1,n){\n\tif (tmp > llabs(esa[j]-x[k]))adj[i][j]+=num[k];\n }\n }\n */\n }\n}\n\nint dp[N];\nbool isok(int n,int lim,int total){\n rep(i,n){\n dp[i]=total;\n rep(j,i){\n if (dp[j]-adj[j][i] > lim)continue;\n dp[i]=min(dp[i],adj[j][i]);\n }\n }\n rep(i,n)if (dp[i] <= lim)return true;\n return false;\n}\n\nint solve(int n,int total){\n int l=0,r=total;\n int ret=total;\n while(l <= r){\n int mid=(l+r)/2;\n if (isok(n,mid,total))ret=mid,r=mid-1;\n else l=mid+1;\n }\n return ret;\n}\n\nmain(){\n int n,m;\n static int x[N],y[N],num[N],esa[N];\n while(cin>>m>>n){\n if (m == 0 && n == 0)break;\n int total=0;\n rep(i,m)cin>>x[i]>>y[i]>>num[i],total+=num[i];\n rep(i,n)cin>>esa[i];\n sort(esa,esa+n);\n rep(i,m){\n r[i]=n-1;\n for(int j=n-1;j>=0;j--){\n\tif (x[i] <= esa[j] )r[i]=j;\n }\n }\n setdata(n,esa,m,y,x,num);\n //int total=accumulate(num,num+m,0);\n cout << solve(n,total) << endl;\n }\n return false;\n}", "accuracy": 1, "time_ms": 500, "memory_kb": 4812, "score_of_the_acc": -0.4049, "final_rank": 9 }, { "submission_id": "aoj_2211_328658", "code_snippet": "#include<iostream>\n#include<cassert>\n#include<cstdlib>\n#include<algorithm>\n#include<numeric>\nusing namespace std;\n#define REP(i,b,n) for(int i=b;i<n;i++)\n#define rep(i,n) REP(i,0,n)\ntypedef long long ll;\nconst int N = 1000;\nint adj[N][N];\nint r[N];\n\nint bestpoint(int n,int *esa,int l,int y,int x,ll tdist){\n int r=n-1;\n int ret=-1;\n if (esa[r] < x)return r;\n while(l <= r){\n int mid=(l+r)/2;\n ll mdist=esa[mid]-x;\n assert(mdist >= 0);\n if (mdist < tdist){\n ret=mid;\n l=mid+1;\n }else {\n r=mid-1;\n }\n }\n return ret;\n}\n\nvoid setdata(int n,int *esa,int m,int *y,int *x,int *num){\n rep(i,n){\n REP(j,i+1,n)adj[i][j]=0;\n rep(j,m){\n if (esa[i] < x[j]){\n\tll tdist=llabs(esa[i]-x[j]);\n\tint ind=bestpoint(n,esa,r[j],y[j],x[j],tdist);\n\t//cout <<\"check \" << i <<\" \" << j <<\" : \" << esa[i] <<\" \" << x[j] << \" \" << ind << endl;\n\tif (ind == -1)ind=r[j]-1;\n\tif (ind >= 0)adj[i][ind]+=num[j];\n }\n }\n for(int j=n-2;j>i;j--){\n adj[i][j]+=adj[i][j+1];\n }\n\n /*\n for(int k=m-1;k>=0;k--){\n ll tmp=llabs(esa[i]-x[k]);\n REP(j,i+1,n){\n\tif (tmp > llabs(esa[j]-x[k]))adj[i][j]+=num[k];\n }\n }\n */\n }\n}\n\nint dp[N];\nbool isok(int n,int lim,int total){\n rep(i,n){\n dp[i]=total;\n rep(j,i){\n if (dp[j]-adj[j][i] > lim)continue;\n dp[i]=min(dp[i],adj[j][i]);\n }\n }\n rep(i,n)if (dp[i] <= lim)return true;\n return false;\n}\n\nint solve(int n,int total){\n int l=0,r=total;\n int ret=total;\n while(l <= r){\n int mid=(l+r)/2;\n if (isok(n,mid,total))ret=mid,r=mid-1;\n else l=mid+1;\n }\n return ret;\n}\n\nmain(){\n int n,m;\n static int x[N],y[N],num[N],esa[N];\n while(cin>>m>>n){\n if (m == 0 && n == 0)break;\n int total=0;\n rep(i,m)cin>>x[i]>>y[i]>>num[i],total+=num[i];\n rep(i,n)cin>>esa[i];\n sort(esa,esa+n);\n rep(i,m){\n r[i]=n-1;\n for(int j=n-1;j>=0;j--){\n\tif (x[i] <= esa[j] )r[i]=j;\n }\n }\n setdata(n,esa,m,y,x,num);\n //int total=accumulate(num,num+m,0);\n cout << solve(n,total) << endl;\n }\n return false;\n}", "accuracy": 1, "time_ms": 510, "memory_kb": 4808, "score_of_the_acc": -0.4062, "final_rank": 10 }, { "submission_id": "aoj_2211_203312", "code_snippet": "#include<cstdio>\n#include<cstring>\n#include<vector>\n#include<map>\n#include<algorithm>\n#define MAX_N 1010\n#define MAX_M 1010\n#define MAX_X 10000\n#define MAX 100000000\n#define INF 1000000000\nusing namespace std;\nint N, M, C[MAX_N], cx[MAX_N], px[MAX_M];\npair<int, int> t[MAX_N];\nint P[MAX_M], dp[MAX_M];\nint cc[MAX_N][MAX_N];\nvoid init(){\n\tC[N] = 0;\tcx[N++] = -INF;\n\tfor(int i=0;i<N;++i){\n\t\tt[i] = make_pair(cx[i], C[i]);\n\t}\n\tsort(t, t+N);\n\tfor(int i=0;i<N;++i){\n\t\tcx[i] = t[i].first;\n\t\tC[i] = t[i].second;\n\t}\n\tfor(int i=1;i<N;++i){\n\t\tC[i] += C[i-1];\n\t}\n\tpx[M++] = INF;\n\tsort(px, px + M);\n\tfor(int i=0;i<M;++i){\n\t\tint index = upper_bound(cx, cx+N, px[i]) - cx -1;\n\t\tP[i] = C[index];\n\t}\n\tfor(int i=0;i<M;++i){\n\t\tfor(int j=i;j<M;++j){\n\t\t\tint center = (px[j] - px[i]) / 2 + px[i];\n\t\t\tint ci = upper_bound(cx, cx + N, center) - cx -1;\n\t\t\tcc[i][j] = ci;\n\t\t}\n\t}\n}\nbool check(int limit){\n\t//printf(\"limit:%d\\n\", limit);\n\tfor(int i=0;i<M;++i){\n\t\tdp[i] = P[i];\n\t\tfor(int j=0;j<i;++j){\n\t\t\t//int center = (px[i] - px[j]) / 2 + px[j];\n\t\t\tint ci, cn, ln, rn;\n\t\t\t//ci = upper_bound(cx, cx + N, center) - cx -1;\n\t\t\tci = cc[j][i];\n\t\t\t//printf(\"(%d, %d)<-(%d, %d)->(%d, %d)\\n\", j, px[j], ci, cx[ci], i, px[i]);\n\t\t\tcn = C[ci];\tln = P[j];\trn = P[i];\n\t\t\tif(dp[j] + (cn - ln) <= limit){\n\t\t\t\tdp[i] = min(dp[i], rn - cn);\n\t\t\t\t//printf(\"%d:%d\\n\", i, dp[i]);\n\t\t\t}\n\t\t}\n\t\t//if(dp[i] > limit) return false;\n\t}\n\treturn dp[M-1] == 0;\n\t//return true;\n}\nint solve(){\n\tinit();\n\t/*\n\tfor(int i=0;i<N;++i){\n\t\tprintf(\"(%d, %d)\", cx[i], C[i]);\n\t}\n\tprintf(\"\\n\");\n\tfor(int i=0;i<M;++i){\n\t\tprintf(\"(%d, %d)\", px[i], P[i]);\n\t}\n\tprintf(\"\\n\");\n\t*/\n\tint l, c, u, res;\n\tl = 0;\tu = C[N-1];\n\tres = -1;\n\twhile(l <= u){\n\t\tc = (l + u) / 2;\n\t\t//printf(\"%d<%d>%d\\n\", l, c, u);\n\t\tbool f = check(c);\n\t\tif(f){\n\t\t\tres = c;\n\t\t\tu = c - 1;\n\t\t}\n\t\telse{\n\t\t\tl = c + 1;\n\t\t}\n\t}\n\treturn res;\n}\nint main(){\n\tfor(;;){\n\t\tscanf(\"%d%d\", &N, &M);\n\t\tif(N == 0 && M == 0) return 0;\n\t\tfor(int i=0;i<N;++i){\n\t\t\tint y;\n\t\t\tscanf(\"%d%d%d\", &cx[i], &y, &C[i]);\n\t\t}\n\t\tfor(int i=0;i<M;++i){\n\t\t\tscanf(\"%d\", &px[i]);\n\t\t}\n\t\tprintf(\"%d\\n\", solve());\n\t}\n}", "accuracy": 1, "time_ms": 370, "memory_kb": 4748, "score_of_the_acc": -0.3784, "final_rank": 8 }, { "submission_id": "aoj_2211_202957", "code_snippet": "#include<cstdio>\n#include<cstring>\n#include<vector>\n#include<map>\n#include<algorithm>\n#define MAX_N 1010\n#define MAX_M 1010\n#define MAX_X 10000\n#define MAX 100000000\n#define INF 1000000000\nusing namespace std;\nint N, M, C[MAX_N], cx[MAX_N], px[MAX_M];\npair<int, int> t[MAX_N];\nint P[MAX_M], dp[MAX_M];\nvoid init(){\n\tC[N] = 0;\tcx[N++] = -INF;\n\tfor(int i=0;i<N;++i){\n\t\tt[i] = make_pair(cx[i], C[i]);\n\t}\n\tsort(t, t+N);\n\tfor(int i=0;i<N;++i){\n\t\tcx[i] = t[i].first;\n\t\tC[i] = t[i].second;\n\t}\n\tfor(int i=1;i<N;++i){\n\t\tC[i] += C[i-1];\n\t}\n\tpx[M++] = INF;\n\tsort(px, px + M);\n\tfor(int i=0;i<M;++i){\n\t\tint index = upper_bound(cx, cx+N, px[i]) - cx -1;\n\t\tP[i] = C[index];\n\t}\n}\nbool check(int limit){\n\t//printf(\"limit:%d\\n\", limit);\n\tfor(int i=0;i<M;++i){\n\t\tdp[i] = P[i];\n\t\tfor(int j=0;j<i;++j){\n\t\t\tint center = (px[i] - px[j]) / 2 + px[j];\n\t\t\tint ci, cn, ln, rn;\n\t\t\tci = upper_bound(cx, cx + N, center) - cx -1;\n\t\t\t//printf(\"(%d, %d)<-(%d, %d)->(%d, %d)\\n\", j, px[j], ci, cx[ci], i, px[i]);\n\t\t\tcn = C[ci];\tln = P[j];\trn = P[i];\n\t\t\tif(dp[j] + (cn - ln) <= limit){\n\t\t\t\tdp[i] = min(dp[i], rn - cn);\n\t\t\t\t//printf(\"%d:%d\\n\", i, dp[i]);\n\t\t\t}\n\t\t}\n\t\t//if(dp[i] > limit) return false;\n\t}\n\treturn dp[M-1] == 0;\n\t//return true;\n}\nint solve(){\n\tinit();\n\t/*\n\tfor(int i=0;i<N;++i){\n\t\tprintf(\"(%d, %d)\", cx[i], C[i]);\n\t}\n\tprintf(\"\\n\");\n\tfor(int i=0;i<M;++i){\n\t\tprintf(\"(%d, %d)\", px[i], P[i]);\n\t}\n\tprintf(\"\\n\");\n\t*/\n\tint l, c, u, res;\n\tl = 0;\tu = C[N-1];\n\tres = -1;\n\twhile(l <= u){\n\t\tc = (l + u) / 2;\n\t\t//printf(\"%d<%d>%d\\n\", l, c, u);\n\t\tbool f = check(c);\n\t\tif(f){\n\t\t\tres = c;\n\t\t\tu = c - 1;\n\t\t}\n\t\telse{\n\t\t\tl = c + 1;\n\t\t}\n\t}\n\treturn res;\n}\nint main(){\n\tfor(;;){\n\t\tscanf(\"%d%d\", &N, &M);\n\t\tif(N == 0 && M == 0) return 0;\n\t\tfor(int i=0;i<N;++i){\n\t\t\tint y;\n\t\t\tscanf(\"%d%d%d\", &cx[i], &y, &C[i]);\n\t\t}\n\t\tfor(int i=0;i<M;++i){\n\t\t\tscanf(\"%d\", &px[i]);\n\t\t}\n\t\tprintf(\"%d\\n\", solve());\n\t}\n}", "accuracy": 1, "time_ms": 2010, "memory_kb": 796, "score_of_the_acc": -0.2885, "final_rank": 7 }, { "submission_id": "aoj_2211_136318", "code_snippet": "#include<iostream>\n#include<map>\n#include<algorithm>\n#include<cassert>\n#include<vector>\n#include<cstring>\nusing namespace std;\n\nint dp[1000], catsum[20010];\nvector<pair< int, int > > cats;\nvector<int> foods;\nint N,M,lmost;\n\nbool isable(int mid) {\n memset(dp, -1, sizeof(dp));\n dp[M-1] = 0;\n\n for(int i=M-2; i>=0; --i) {\n int ret = 1<<29;\n for(int j=i+1; j<M; ++j) {\n int right = catsum[foods[j]] - catsum[(foods[i]+foods[j])/2];;\n int left = catsum[(foods[i]+foods[j])/2] - catsum[foods[i]];\n\n if(right + dp[j] <= mid) ret = min(left, ret);\n }\n dp[i] = ret;\n }\n if(dp[0] + catsum[foods[0]] <= mid) return true;\n return false;\n}\n\nint main() {\n int x,y,c,totalcats = 0;\n while(cin>>N>>M, N|M) {\n\n memset(catsum, 0, sizeof(catsum));\n foods.clear();\n cats.clear();\n totalcats = 0;\n\n for(int i=0; i<N; ++i) {\n cin>>x>>y>>c;\n x += 10000;\n totalcats += c;\n cats.push_back(make_pair(x,c));\n }\n\n for(int i=0; i<M; ++i) {\n cin>>c;\n foods.push_back(c+10000);\n }\n\n/*\n if(M == 1) {\n cout<<totalcats<<endl;\n continue;\n }\n*/\n\n sort(cats.begin(), cats.end());\n sort(foods.begin(), foods.end());\n\n for(int i=0; i<N; ++i) {\n if(cats[i].first <= foods[0]) cats[i].first = foods[0];\n if(cats[i].first >= foods[M-1]) cats[i].first = foods[M-1];\n catsum[cats[i].first] += cats[i].second;\n }\n\n for(int i=0,ss = 0; i<20010; ++i) {\n ss += catsum[i];\n catsum[i] = ss;\n }\n\n int low = -1,hi = totalcats;\n while(low+1<hi) {\n int mid = (low+hi)/2;\n if(isable(mid)) hi = mid;\n else low = mid;\n }\n cout<<hi<<endl;\n }\n}", "accuracy": 1, "time_ms": 260, "memory_kb": 1020, "score_of_the_acc": -0.028, "final_rank": 2 }, { "submission_id": "aoj_2211_136317", "code_snippet": "#include<iostream>\n#include<map>\n#include<algorithm>\n#include<cassert>\n#include<vector>\n#include<cstring>\nusing namespace std;\n\nint dp[1000], catsum[20010];\nvector<pair< int, int > > cats;\nvector<int> foods;\nint N,M,lmost;\n\nbool isable(int mid) {\n memset(dp, -1, sizeof(dp));\n\n for(int i=M-1; i>=0; --i) {\n if(i == M-1) {\n dp[i] = 0;\n continue;\n }\n int ret = 1<<29;\n for(int j=i+1; j<M; ++j) {\n int right = catsum[foods[j]] - catsum[(foods[i]+foods[j])/2];;\n int left = catsum[(foods[i]+foods[j])/2] - catsum[foods[i]];\n\n if(right + dp[j] <= mid) ret = min(left, ret);\n }\n dp[i] = ret;\n }\n if(dp[0] + catsum[foods[0]] <= mid) return true;\n return false;\n}\n\nint main() {\n int x,y,c,totalcats = 0;\n while(cin>>N>>M, N|M) {\n\n memset(catsum, 0, sizeof(catsum));\n foods.clear();\n cats.clear();\n totalcats = 0;\n\n for(int i=0; i<N; ++i) {\n cin>>x>>y>>c;\n x += 10000;\n totalcats += c;\n cats.push_back(make_pair(x,c));\n }\n\n for(int i=0; i<M; ++i) {\n cin>>c;\n foods.push_back(c+10000);\n }\n\n if(M == 1) {\n cout<<totalcats<<endl;\n continue;\n }\n\n sort(cats.begin(), cats.end());\n sort(foods.begin(), foods.end());\n\n lmost = 0;\n for(int i=0; i<N; ++i) {\n\n if(cats[i].first <= foods[0]) {\n cats[i].first = foods[0];\n lmost += cats[i].second;\n }\n if(cats[i].first >= foods[M-1]) cats[i].first = foods[M-1];\n catsum[cats[i].first] += cats[i].second;\n }\n\n int ss = 0;\n for(int i=0; i<20010; ++i) {\n ss += catsum[i];\n catsum[i] = ss;\n }\n\n int low = -1,hi = totalcats;\n while(low+1<hi) {\n int mid = (low+hi)/2;\n if(isable(mid)) hi = mid;\n else low = mid;\n }\n cout<<hi<<endl;\n }\n}", "accuracy": 1, "time_ms": 260, "memory_kb": 1016, "score_of_the_acc": -0.0276, "final_rank": 1 }, { "submission_id": "aoj_2211_136312", "code_snippet": "#include<iostream>\n#include<map>\n#include<algorithm>\n#include<cassert>\n#include<vector>\n#include<cstring>\nusing namespace std;\n\nint dp[1000], catsum[20010];\nvector<pair< int, int > > cats;\nvector<int> foods;\nint N,M,lmost;\n\nbool isable(int mid) {\n memset(dp, -1, sizeof(dp));\n\n for(int i=M-1; i>=0; --i) {\n if(i == M-1) {\n dp[i] = 0;\n continue;\n }\n int ret = 1<<29;\n for(int j=i+1; j<M; ++j) {\n int right = catsum[foods[j]] - catsum[(foods[i]+foods[j])/2];;\n int left = catsum[(foods[i]+foods[j])/2] - catsum[foods[i]];\n\n //cout<<i<<\" \"<<j<<\" \"<<right<<\" \"<<left<<endl;\n if(right + dp[j] <= mid) ret = min(left, ret);\n }\n dp[i] = ret;\n }\n if(dp[0] + lmost <= mid) return true;\n return false;\n}\n\nint main() {\n int x,y,c,totalcats = 0;\n while(cin>>N>>M, N|M) {\n\n memset(catsum, 0, sizeof(catsum));\n foods.clear();\n cats.clear();\n totalcats = 0;\n\n for(int i=0; i<N; ++i) {\n cin>>x>>y>>c;\n x += 10000;\n totalcats += c;\n cats.push_back(make_pair(x,c));\n }\n\n for(int i=0; i<M; ++i) {\n cin>>c;\n foods.push_back(c+10000);\n }\n\n if(M == 1) {\n cout<<totalcats<<endl;\n continue;\n }\n\n sort(cats.begin(), cats.end());\n sort(foods.begin(), foods.end());\n\n //ˆê”ԍ¶‚Ì‚¦‚³‚¢‚ê‚æ‚荶‚Ì”L‚ð‚¦‚³‚¢‚ê‚Ì•”•ª‚É‚ ‚킹‚é\n //‰E‚à“¯—l\n lmost = 0;\n for(int i=0; i<N; ++i) {\n\n if(cats[i].first <= foods[0]) {\n cats[i].first = foods[0];\n lmost += cats[i].second;\n }\n if(cats[i].first >= foods[M-1]) cats[i].first = foods[M-1];\n catsum[cats[i].first] += cats[i].second;\n //cout<<\"add \"<<cats[i].first<<endl;\n }\n\n int ss = 0;\n for(int i=0; i<20010; ++i) {\n ss += catsum[i];\n catsum[i] = ss;\n }\n\n int low = -1,hi = totalcats;\n while(low+1<hi) {\n int mid = (low+hi)/2;\n //cout<<mid<<\" \"<<low<<\" \"<<hi<<endl;\n if(isable(mid)) hi = mid;\n else low = mid;\n }\n cout<<hi<<endl;\n }\n}", "accuracy": 1, "time_ms": 260, "memory_kb": 1020, "score_of_the_acc": -0.028, "final_rank": 2 }, { "submission_id": "aoj_2211_130795", "code_snippet": "#include<iostream>\n#include<algorithm>\n#include<vector>\n#include<stack>\n#include<map>\n#include<set>\n#include<queue>\n#include<cstdio>\n#include<climits>\n#include<cmath>\n#include<cstring>\n#include<string>\n#include<sstream>\n#include<numeric>\n#include<cassert>\n\n#define f first\n#define s second\n#define mp make_pair\n\n#define REP(i,n) for(int i=0; i<(int)(n); i++)\n#define print(x) printf(\"%d\\n\",x)\n\nusing namespace std;\n\npair<int,pair<int,int> > cat[1000];\nint catx[1000];\nint caty[1000];\nint catc[1000];\nint esa[1000];\nint n,m;\nint acc;\n\nbool can(int lim){\n vector<int> e(esa, esa+m);\n while(true){\n if(e.size() <= 1){\n if(lim >= acc)\n break;\n else\n return false;\n }\n\n int mm = e.size();\n vector<int> counts(mm);\n int ep = 0;\n int next = e[1];\n int maxc = -1;\n int maxp;\n\n REP(i,n){\n if(ep == mm - 1){\n counts[ep] += catc[i];\n }else{\n if(catx[i] <= e[ep+1]){\n#define ABS(x) ((x) < 0 ? -(x) : (x))\n int da = ABS(e[ep ] - catx[i]);\n int db = ABS(e[ep+1] - catx[i]);\n if(da <= db){\n counts[ep] += catc[i];\n }else{\n counts[ep+1] += catc[i];\n }\n }else{\n while(ep != mm-1 && catx[i] > e[ep+1]) ep++;\n i--;\n }\n }\n if(maxc < counts[ep]){\n maxc = counts[ep];\n maxp = ep;\n }\n if(ep != mm-1 && maxc < counts[ep+1]){\n maxc = counts[ep+1];\n maxp = ep+1;\n }\n }\n\n //printf(\"counts: \");\n //REP(i,mm) printf(\"(%d):%d \",e[i],counts[i]); puts(\"\");\n if(maxc <= lim) break;\n if(maxp == 0 || maxp == mm - 1) return false;\n if(counts[maxp - 1] > lim && counts[maxp + 1] > lim) return false;\n e.erase(e.begin() + maxp);\n }\n return true;\n}\n\nint main(){\n while(scanf(\"%d%d\",&n,&m), n+m){\n int tmp;\n REP(i,n) scanf(\"%d%d%d\",&cat[i].f,&cat[i].s.f,&cat[i].s.s);\n REP(i,m) scanf(\"%d\",esa+i);\n if(n == 0){\n puts(\"0\");\n continue;\n }\n\n sort(cat, cat+n);\n sort(esa, esa+m);\n REP(i,n) catx[i] = cat[i].f;\n REP(i,n) caty[i] = cat[i].s.f * cat[i].s.f;\n REP(i,n) catc[i] = cat[i].s.s;\n\n int high = acc = accumulate(catc, catc+n, 0);\n int low = *max_element(catc, catc+n);\n\n while(high >= low){\n int mid = (high + low) / 2;\n //printf(\"%d %d %d\\n\",low,mid,high);\n if(can(mid)){\n high = mid - 1;\n }else{\n low = mid + 1;\n }\n }\n low = max(*max_element(catc, catc+n), low - 2);\n while(!can(low)) low++;\n print(low);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 400, "memory_kb": 960, "score_of_the_acc": -0.0451, "final_rank": 4 }, { "submission_id": "aoj_2211_130793", "code_snippet": "#include<iostream>\n#include<algorithm>\n#include<vector>\n#include<stack>\n#include<map>\n#include<set>\n#include<queue>\n#include<cstdio>\n#include<climits>\n#include<cmath>\n#include<cstring>\n#include<string>\n#include<sstream>\n#include<numeric>\n#include<cassert>\n\n#define f first\n#define s second\n#define mp make_pair\n\n#define REP(i,n) for(int i=0; i<(int)(n); i++)\n#define print(x) printf(\"%d\\n\",x)\n\nusing namespace std;\n\npair<int,pair<int,int> > cat[1000];\nint catx[1000];\nint caty[1000];\nint catc[1000];\nint esa[1000];\nint n,m;\nint acc;\n\nbool can(int lim){\n vector<int> e(esa, esa+m);\n while(true){\n if(e.size() <= 1){\n if(lim >= acc)\n break;\n else\n return false;\n }\n\n int mm = e.size();\n vector<int> counts(mm);\n int ep = 0;\n int next = e[1];\n int maxc = -1;\n int maxp;\n\n REP(i,n){\n if(ep == mm - 1){\n counts[ep] += catc[i];\n }else{\n if(catx[i] <= e[ep+1]){\n#define ABS(x) ((x) < 0 ? -(x) : (x))\n int da = ABS(e[ep ] - catx[i]);\n int db = ABS(e[ep+1] - catx[i]);\n if(da <= db){\n counts[ep] += catc[i];\n }else{\n counts[ep+1] += catc[i];\n }\n }else{\n while(ep != mm-1 && catx[i] > e[ep+1]) ep++;\n i--;\n }\n }\n if(maxc < counts[ep]){\n maxc = counts[ep];\n maxp = ep;\n }\n if(ep != mm-1 && maxc < counts[ep+1]){\n maxc = counts[ep+1];\n maxp = ep+1;\n }\n }\n\n //printf(\"counts: \");\n //REP(i,mm) printf(\"(%d):%d \",e[i],counts[i]); puts(\"\");\n if(maxc <= lim) break;\n if(maxp == 0 || maxp == mm - 1) return false;\n //if(counts[maxp - 1] > lim && counts[maxp + 1] > lim) return false;\n e.erase(e.begin() + maxp);\n }\n return true;\n}\n\nint main(){\n while(scanf(\"%d%d\",&n,&m), n+m){\n int tmp;\n REP(i,n) scanf(\"%d%d%d\",&cat[i].f,&cat[i].s.f,&cat[i].s.s);\n REP(i,m) scanf(\"%d\",esa+i);\n if(n == 0){\n puts(\"0\");\n continue;\n }\n\n sort(cat, cat+n);\n sort(esa, esa+m);\n REP(i,n) catx[i] = cat[i].f;\n REP(i,n) caty[i] = cat[i].s.f * cat[i].s.f;\n REP(i,n) catc[i] = cat[i].s.s;\n\n int high = acc = accumulate(catc, catc+n, 0);\n int low = *max_element(catc, catc+n);\n\n while(high >= low){\n int mid = (high + low) / 2;\n //printf(\"%d %d %d\\n\",low,mid,high);\n if(can(mid)){\n high = mid - 1;\n }else{\n low = mid + 1;\n }\n }\n low = max(*max_element(catc, catc+n), low - 2);\n while(!can(low)) low++;\n print(low);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 430, "memory_kb": 956, "score_of_the_acc": -0.0495, "final_rank": 5 } ]
aoj_2212_cpp
Problem I: 盗まれた宝石 パル王国の国宝である宝石が盗賊たちに盗まれた。 冒険者のあなたはその噂を聞きつけて盗賊のアジトに向かい、なんとか宝石を取り戻すことが出来た。 ところが、宝石をパル王国の城へ返しに出向いたところ、城の守衛兵から、 「我が王はまだあなたを完全に信用していない。その宝石が本物かどうか怪しいし、そもそも宝石を取り戻したというのがウソで、王の命を狙う盗賊の仲間なのではないか?と疑っておられる。」 といわれた。 あなたは、守衛兵から本当に信頼できる人物かどうか試すための試練を与えられた。試練の内容は、 「城の地下倉庫へ行き指定された場所に宝石を置け。そこに宝石が本物だったときに反応する魔法陣が書かれてある。ただしあなたが怪しい行動を起こさないようにするため、地下倉庫の入り口から指定された場所に向かうまで、守衛兵が言った移動パターンをとってはならない。」 というものだった。 例えば、下図にある地下倉庫の形状、禁止パターンに対し、図にかかれてあるような移動パターンを取ってはならない。移動パターンの一部(赤くハイライトされている)が、禁止パターンの中に含まれるからである。(また、この移動パターンの中の2,3番目の動き「↓↓」も禁止パターンの中に含まれる) 一方、 下図のような移動パターンは、移動パターンのどの部分も禁止パターンの中に含まれないので許される。 入力として、地下倉庫の形状、禁止パターンが与えられる。禁止パターンを取らずに、入り口から魔法陣へ移動するのに最低限必要な移動回数を求めよ。 Input 入力には、地下倉庫の形状と禁止パターンが含まれる。 地下倉庫の形状は、以下のように表される。 まず、二つの整数 N,M が与えられる。それぞれ地下倉庫の行数と列数を意味する。(1 ≤ N,M ≤ 50) 続いて、それぞれM個の文字からなる文字列がN行与えられる。 含まれる文字とその意味は以下の通りである。 文字 意味 S 地下倉庫の入り口。一つの地下倉庫につき必ず一つだけ含まれる。 G 魔法陣。一つの地下倉庫につき必ず一つだけ含まれる。 . 通路。(禁止パターンをとらなければ)通ることが出来る。 # 壁。壁の中を通ることはできない。 次に、禁止パターンが与えられる。禁止パターンは以下のように表される。 まず、一つの整数 P が与えられる。禁止パターンの数を意味する。(0 ≤ P ≤ 10) 続いて、禁止パターンを意味する文字列がP行にわたって与えられる。 禁止パターンに含まれる文字とその意味は以下の通りである。 文字 意味 U ↑の移動。 R →の移動。 D ↓の移動。 L ←の移動。 禁止パターンの長さは1以上、10以下である。 ある禁止パターンが、別の禁止パターンの部分文字列となっていることもある。また、同一の禁止パターンが含まれることもある。 Output 最低限必要な移動回数を意味する整数を出力せよ。魔法陣へ辿りつけない場合は -1 を出力せよ。 Notes on Test Cases 上記入力形式で複数のデータセットが与えられます。各データセットに対して上記出力形式で出力を行うプログラムを作成して下さい。 n, m が 0 のとき入力の終わりを示します。 Sample Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output for Sample Input 13 60 7 10 -1
[ { "submission_id": "aoj_2212_10853818", "code_snippet": "#include<cstdio>\n#include<cstdlib>\n#include<cstring>\n#include<cctype>\n#include<iostream>\n#include<algorithm>\n#include<queue>\n#include<set>\n#include<map>\n#define ULL unsigned long long\n#define LL long long\nusing namespace std;\nconst int MAX_SIZE=55,MAX_P=15,max_ch=4,M=105,INF=0x3f3f3f3f;\nint n,m;\nstruct aho_corasick{\n int next[M][max_ch],fail[M],id[300];\n bool mark[M];\n int root,L;\n int newnode(){\n for(int j=0;j<max_ch;j++) next[L][j]=-1;\n return L++;\n }\n void init(){\n L=0;\n memset(mark,0,sizeof(mark));\n root=newnode();\n id['L']=0;\n id['R']=1;\n id['U']=2;\n id['D']=3;\n }\n void insert(char buf[]){\n int len=strlen(buf);\n int now=root;\n for(int j=0;j<len;j++){\n if(next[now][id[buf[j]]]==-1) next[now][id[buf[j]]]=newnode();\n now=next[now][id[buf[j]]];\n }\n mark[now]=1;\n }\n void build(){\n queue<int>Q;\n fail[root]=root;\n for(int j=0;j<max_ch;j++){\n if(next[root][j]==-1) next[root][j]=root;\n else{\n fail[next[root][j]]=root;\n Q.push(next[root][j]);\n }\n }\n while(!Q.empty()){\n int now=Q.front();\n if(mark[fail[now]]) mark[now]=1;\n Q.pop();\n for(int j=0;j<max_ch;j++){\n if(next[now][j]==-1) next[now][j]=next[fail[now]][j];\n else{\n fail[next[now][j]]=next[fail[now]][j];\n Q.push(next[now][j]);\n }\n }\n }\n }\n void query(int m){\n }\n}ac;\nstruct node{\n int x,y,z;\n};\nint rx[]={0,0,-1,1};\nint ry[]={-1,1,0,0};\nchar mp[MAX_SIZE][MAX_SIZE];\nint dis[MAX_SIZE][MAX_SIZE][M];\nint sx,sy,tx,ty;\nqueue<node>Q;\nbool check(int x,int y){\n return x>=1&&x<=n&&y>=1&&y<=m&&mp[x][y]!='#';\n}\nint bfs(){\n memset(dis,63,sizeof(dis));\n while(!Q.empty()) Q.pop();\n node now;\n now.x=sx;now.y=sy;now.z=0;\n dis[sx][sy][0]=0;\n Q.push(now);\n while(!Q.empty()){\n //printf(\"%d\\n\",1);\n now=Q.front();Q.pop();\n //printf(\"%d %d %d %d\\n\",now.x,now.y,now.z,dis[now.x][now.y][now.z]);\n for(int j=0;j<4;j++){\n int x=now.x+rx[j],y=now.y+ry[j];\n if(!check(x,y)) continue;\n int z=ac.next[now.z][j];\n if(ac.mark[z]) continue;\n if(dis[x][y][z]!=INF) continue;\n dis[x][y][z]=dis[now.x][now.y][now.z]+1;\n if(x==tx&&y==ty) return dis[x][y][z];\n Q.push({x,y,z});\n }\n }\n return -1;\n}\nint main() {\n //printf(\"%d\\n\",INF);\n while(scanf(\"%d%d\",&n,&m)!=EOF&&n&&m){\n ac.init();\n for(int j=1;j<=n;j++){\n scanf(\"%s\",mp[j]+1);\n }\n int p;\n scanf(\"%d\",&p);\n while(p--){\n char s[15];\n scanf(\"%s\",s);\n ac.insert(s);\n }\n //printf(\"1\\n\");\n ac.build();\n //printf(\"1\\n\");\n for(int j=1;j<=n;j++){\n for(int k=1;k<=m;k++){\n if(mp[j][k]=='S') sx=j,sy=k;\n if(mp[j][k]=='G') tx=j,ty=k;\n }\n }\n //printf(\"%d\\n\",ac.next[0][0]);\n printf(\"%d\\n\",bfs());\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 4776, "score_of_the_acc": -0.1539, "final_rank": 9 }, { "submission_id": "aoj_2212_10501899", "code_snippet": "/*\n7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0\n\n*/\n#include<iostream>\n#include<string>\n#include<queue>\n#include<vector>\n#include<cassert>\n#include<random>\n#include<set>\n#include<map>\n#include<cassert>\n#include<unordered_map>\n#include<bitset>\n#include<numeric>\n#include<algorithm>\n#include<optional>\n#include<tuple>\n#include<unordered_set>\n#include<cmath>\nusing namespace std;\ntypedef long long ll;\nconst int inf=1<<30;\nconst ll INF=1LL<<62;\nconst ll MOD=998244353;\nconst ll MAXA=1e6;\n\nusing P=pair<int,int>;\nstruct State{\n P pos;//今いる座標の位置\n int node;//trie木でのノードのインデックス\n int dist;//スタートからの歩数\n string str;//デバッグのために使用\n State(P pos_,int node_,int dist_):pos(pos_),node(node_),dist(dist_){str=\"\";};\n};\n\n\nstruct Aho_Corasick{\n typedef std::unordered_map<char,int> MP;\n\n std::vector<MP> trie;\n std::vector<int> cnt;//cnt,treeの大きさは同じ\n const int root=0;\n \n Aho_Corasick(){\n trie.push_back(MP());\n cnt.push_back(0);\n }\n\n std::unordered_map<int,std::vector<std::string>> output;\n\n void add(const std::vector<std::string>& terms){\n for(const std::string& term:terms){\n add(term);\n }\n }\n\n //計算量は挿入する文字列の長さの和\n int add(const std::string& string){\n int now=root;//根\n for(char c:string){\n if(trie[now].count(c)){\n now=trie[now][c];\n }else{\n int pre=now;\n now=trie.size();\n cnt.push_back(0);\n trie.push_back(MP());\n trie[pre][c]=now;\n }\n }\n\n cnt[now]++;//終端文字列\n\n output[now].push_back(string);//終端ノードに対してsを入れる\n \n\n return now;\n }\n\n void match(const std::string& string){\n\n std::cout<<\"target: string=\"<<string<<std::endl;\n \n int now=0;\n \n for(int i=0;i<string.size();i++){\n char c=string[i];\n //今の状態nowからcを用いて遷移する場所を探す.\n while(!go(now,c)){\n now=failure[now];\n }\n //go(now,string[i]).firstがtrue\n now=*go(now,c);\n\n for(const auto& x:output[now]){\n //ノードnowで終わる場合\n std::cout<<\"start=\"<<i-x.size()+1<<\",goal=\"<<i<<\",string:\"<<x<<std::endl;\n }\n }\n \n }\n\n\n std::vector<int> failure;\n std::vector<int> dist;\n \n\n //ノードnowから文字列cをうけていく場所\n std::optional<int> go(int now,char c){\n \n if(trie[now].count(c)){\n return trie[now][c];\n }else if(now==root) return root;\n else return {};\n \n }\n\n void bfs(){\n \n failure=std::vector<int>(trie.size(),0);\n dist=std::vector<int>(trie.size(),-1);\n std::queue<int> que;\n \n dist[root]=0;\n que.push(root);\n\n while(!que.empty()){\n int now=que.front();\n que.pop();\n \n for(auto [c,to]:trie[now]){\n //nowのキーc,toを取り出す\n //trie[now][c]=toとなる\n \n if(now!=root){\n int f=failure[now];//nowで失敗した場合に行きつく場所\n \n while(!go(f,c)){\n f=failure[f];\n }\n //go(f,c).firstがtrueつまり状態fの次に文字cで行ける場所がある もしくはfがroot\n failure[to]=*go(f,c);\n\n for(const auto& string:output[failure[to]]){\n //ノードfailure[to]で終わる文字列たちをtoにもいれる\n output[to].push_back(string);\n }\n \n }\n\n que.push(to);\n dist[to]=dist[now]+1;\n \n }\n\n }\n \n\n }\n \n};\n\n\n//U,R,D,L\nconst int dx[]={0,1,0,-1};\nconst int dy[]={-1,0,1,0};\nconst string dir=\"URDL\";\n\nvector<int> ans;\n\nbool solve(){\n int n,m;\n cin>>n>>m;\n if(n==0 && m==0)return false;\n\n P start,goal;\n vector<string> s(n);\n for(int i=0;i<n;i++){\n cin>>s[i];\n for(int j=0;j<m;j++){\n if(s[i][j]=='S'){\n start={i,j};\n }\n if(s[i][j]=='G'){\n goal={i,j};\n }\n }\n }\n int p;\n cin>>p;\n\n Aho_Corasick trie;\n vector<string> terms(p);\n for(int i=0;i<p;i++){\n cin>>terms[i];\n trie.add(terms[i]);\n }\n trie.bfs();\n int sz=trie.trie.size();\n \n using TP=tuple<int,int,int>;\n queue<State> que;\n map<TP,int> used;//同じ状態を二度と踏まないようにするため\n que.push(State{start,trie.root,0});//初期状態\n used[make_tuple(start.first,start.second,trie.root)]=0;\n const int inf=1<<30;\n int res=inf;\n\n while(!que.empty()){\n State now_state=que.front();\n P pos=now_state.pos;\n int node=now_state.node;\n int d=now_state.dist;\n \n que.pop();\n if(pos==goal){\n //cout<<now_state.str<<endl;\n res=d;\n break;\n \n }\n\n for(int k=0;k<4;k++){\n int nx = pos.second + dx[k];\n int ny = pos.first + dy[k];\n\n if(nx<0 || m<=nx || ny<0 || n<=ny) continue;\n if(s[ny][nx]=='#') continue;\n\n //いまいるnodeからdir[k]をつけ足す場合にどこへいくか\n optional<int> res=trie.go(node,dir[k]);\n \n int next_node=0;\n if(!trie.go(node,dir[k])){\n //失敗した場合に,そこから文字dir[k]を用いて次に行くべき場所\n int f=trie.failure[node];\n while(!trie.go(f,dir[k])){\n f=trie.failure[f];\n }\n next_node=*trie.go(f,dir[k]);\n\n\n }else{\n //次に行くべき場所.確実にわかっている\n next_node=*trie.go(node,dir[k]);\n }\n \n if(trie.output[next_node].size()==0){\n //next_nodeは終端文字でない\n State next_state{P{ny,nx},next_node,d+1};\n \n next_state.str=now_state.str+dir[k];//debugの際に使用\n\n if(!used.count(make_tuple(ny,nx,next_node))){\n used[make_tuple(ny,nx,next_node)]=d+1;\n que.push(next_state);\n \n }\n }\n \n\n\n }\n }\n\n res=(res==inf?-1:res);\n ans.push_back(res);\n\n return true;\n\n}\n\n\nint main(){\n \n while(1){\n if(!solve()){\n break;\n }\n }\n \n\n for(int v:ans){\n cout<<v<<endl;\n }\n}", "accuracy": 1, "time_ms": 260, "memory_kb": 10140, "score_of_the_acc": -1.7143, "final_rank": 17 }, { "submission_id": "aoj_2212_9313261", "code_snippet": "#if 1\n#include<cmath>\n#include<limits>\n#include<iostream>\n#include<algorithm>\n#include<functional>\n#include<string>\n#include<map>\n#include<set>\n#include<ctime>\n#include<queue>\n#include<vector>\n#include<tuple>\n#include<complex>\n// #include<format>\n#ifdef _INTEGRAL_MAX_BITS\n// #include\"C:\\Users\\Spare\\source\\repos\\Competitive_programming\\Competitive_programming\\debugging.h\"\n#endif\n#ifndef DEBUGGING\n#define MY_MAIN int main()\n#define REDIRCT_IO ;\n#define RESTORE_IO ;\n#endif\n#define sc static\n#define ct const\n#define ft first\n#define sd second\nusing namespace std;\n// using namespace atcoder;\n// using namespace Eigen;\nusing ld = long double;\nusing ll = long long;\nusing cchar = const char;\nusing cint = const int;\nusing cll = const ll;\nusing uchar = unsigned char;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nusing cuchar = const uchar;\nusing cuint = const uint;\nusing cull = const ull;\nusing clex = complex<int>;\ntemplate<class TYPE> static const TYPE INF = numeric_limits<TYPE>::max();\n#define rep(i, n) for(int i = (0), fend = n; i < fend; ++i)\n#define all(container) container.begin(), container.end()\nstatic const string ascii_lowercase = \"abcdefghijklmnopqrstuvwxyz\";\nstatic const string ascii_uppercase = \"ABCDEFGHIJKLMNOPQRSTUVWXYZ\";\ncint maxN = 52, maxL = 11, maxP = 11;\nint dp[maxN][maxN][maxL * maxP], N, M;\nchar field[maxN][maxN];\nclex I(0, 1), dir(1, 0);\nbool ng[maxL * maxP];\nint ensuing[maxL * maxP][4];\nstring P[maxP];\nconst string RDLU = \"RDLU\";\ncint inf = INF<int> / 2;\nstruct elmt { \n clex position; int t, score; \n elmt(clex c, int i, int i2) :position(c), t(i), score(i2){}\n};\n\nint main() {\n REDIRCT_IO;\n while (true) {\n cin >> N >> M;\n if (!N)break;\n rep(i, N + 2) { field[i][0] = field[i][M + 1] = '#'; }\n rep(i, M + 2) { field[0][i] = field[N + 1][i] = '#'; }\n clex start, goal;\n rep(i, N)rep(j, M) {\n char c;\n cin >> c;\n field[i + 1][j + 1] = c;\n if (c == 'S')start = clex(j + 1, i + 1);\n else if (c == 'G')goal = clex(j + 1, i + 1);\n }\n int p;\n cin >> p;\n rep(i, p) {\n cin >> P[i];\n }\n vector<string> pfx;\n rep(i, p) {\n for (int j = 0; j <= P[i].length(); ++j) {\n pfx.push_back(P[i].substr(0, j));\n }\n }\n sort(all(pfx));\n pfx.erase(unique(all(pfx)), pfx.end());\n int K = pfx.size();\n fill(ensuing[0], ensuing[maxL * maxP], 0);\n fill(ng, ng + maxL * maxP, false);\n rep(i, K) {\n rep(j, p) {\n ng[i] |= P[j].length() <= pfx[i].length()\n && pfx[i].substr(pfx[i].length() - P[j].length(), P[j].length()) == P[j];\n }\n rep(j, 4) {\n string s = pfx[i] + RDLU[j];\n int k;\n while (true) {\n k = lower_bound(all(pfx), s) - pfx.begin();\n if (k < K && pfx[k] == s)break;\n s = s.substr(1);\n }\n ensuing[i][j] = k;\n }\n }\n deque<elmt> deq;\n deq.push_back(elmt(start, 0, 0));\n fill(dp[0][0], dp[maxN][0], inf);\n while (!deq.empty()) {\n elmt el = deq.front(); deq.pop_front();\n clex p = el.position; int t = el.t; int s = el.score;\n if (dp[p.imag()][p.real()][t] <= s)continue;\n dp[p.imag()][p.real()][t] = s;\n rep(i, 4) {\n clex to = p + dir;\n int ensu = ensuing[t][i];\n if (field[to.imag()][to.real()] != '#' && !ng[ensu] && s + 1 < dp[to.imag()][to.real()][ensu]) {\n deq.push_back(elmt(to, ensu, s + 1));\n }\n dir *= I;\n }\n }\n int ans = *min_element(dp[goal.imag()][goal.real()], dp[goal.imag()][goal.real()] + maxL * maxP);\n cout << (ans < inf ? ans : -1) << endl;\n }\n RESTORE_IO;\n return 0;\n}\n#endif", "accuracy": 1, "time_ms": 20, "memory_kb": 4700, "score_of_the_acc": -0.1705, "final_rank": 11 }, { "submission_id": "aoj_2212_9038639", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef int64_t ll;\n\nstruct Node{\n int pre;\n char prec;\n map<char,int> mp;\n bool ed;\n int flgo;\n \n Node(int id,char c=' '){pre=id;prec=c;ed=false;flgo=0;}\n};\n\nstruct Aho_Corasick{\n vector<Node> acnode;\n int idx;\n \n Aho_Corasick(){\n idx=0;\n acnode.push_back(Node(-1));\n }\n\n void add(vector<string> &kns){\n for(auto &kn : kns) add(kn);\n }\n \n int add(string &st){\n int nm=0;\n for(char c : st){\n if(acnode[nm].mp.count(c)) nm=acnode[nm].mp[c];\n else{\n idx++;\n acnode[nm].mp[c]=idx;\n acnode.push_back(Node(nm,c));\n nm=idx;\n } \n }\n acnode[nm].ed=true;\n return nm;\n }\n \n int go(int nm,char c){\n if(acnode[nm].mp.count(c)) return acnode[nm].mp[c];\n else if(nm==0) return 0;\n else return -1;\n }\n \n void dfs(){\n int nm=0;\n queue<int> que;\n que.push(nm);\n while(!que.empty()){\n nm=que.front();\n que.pop();\n for(auto [c,to]:acnode[nm].mp){\n if(nm!=0){\n int f=acnode[nm].flgo;\n while(go(f,c)==-1){\n f=acnode[f].flgo;\n }\n acnode[to].flgo=go(f,c);\n if(acnode[acnode[to].flgo].ed) acnode[to].ed=true;\n }\n que.push(to);\n }\n }\n return;\n }\n\n};\n\nint main(){\n\n int n,m;\n vector<vector<int>> nxo={{-1,0},{1,0},{0,-1},{0,1}};\n vector<char> nxc={'U','D','L','R'};\n do{\n cin >> n >> m;\n if(n==0) break;\n \n vector g(n,vector<char>(m));\n int sn,sm,gn,gm;\n for(int i=0;i<n;i++){\n for(int j=0;j<m;j++){\n cin >> g[i][j];\n if(g[i][j]=='S'){ \n sn=i;sm=j;\n }else if(g[i][j]=='G'){ \n gn=i;gm=j;\n }\n }\n }\n /*for(int i=0;i<n;i++){\n for(int j=0;j<m;j++) cout << g[i][j];\n cout << endl;\n }\n break;*/\n \n int p;\n cin >> p;\n vector<string> kns;\n string st;\n for(int i=0;i<p;i++){\n cin >> st;\n kns.push_back(st);\n }\n \n Aho_Corasick ac;\n ac.add(kns);\n ac.dfs();\n /* for(int i=0;i<=ac.idx;i++){\n cout << ac.acnode[i].pre << \" \" << i << \" \" << ac.acnode[i].prec << \" \" <<ac.acnode[i].flgo << \" \" << ac.acnode[i].ed << endl;\n }*/\n \n typedef pair<tuple<int,int,int>,int> PT;\n queue<PT> que;\n map<tuple<int,int,int>,int> used;\n PT stt={{sn,sm,0},0};\n que.push(stt);\n used[{sn,sm,0}]=1;\n int res=INT_MAX;\n int cn,cm,cnd,cnt;\n while(!que.empty()){\n stt=que.front();\n que.pop();\n tuple<int,int,int> tp;\n tie(tp,cnt)=stt;\n tie(cn,cm,cnd)=tp;\n \n if((cn==gn)&&(cm==gm)){\n res=cnt;\n break;\n }\n \n for(int i=0;i<4;i++){\n int nxn=cn+nxo[i][0],nxm=cm+nxo[i][1];\n if((nxn<0)||(nxn>=n)||(nxm<0)||(nxm>=m)) continue;\n if(g[nxn][nxm]=='#') continue;\n \n int nxnd=0;\n if(ac.go(cnd,nxc[i])==-1){\n int f=ac.acnode[cnd].flgo;\n while(ac.go(f,nxc[i])==-1){\n f=ac.acnode[f].flgo;\n }\n nxnd=ac.go(f,nxc[i]);\n }else nxnd=ac.go(cnd,nxc[i]);\n //cout << cnd << \" \" << nxc[i] << \" \" << nxnd << endl; \n if(ac.acnode[nxnd].ed==false){\n tp={nxn,nxm,nxnd};\n if(used.count(tp)==0){\n used[tp]=1;\n que.push({tp,cnt+1});\n //cout << cnt+1 << \" \" << nxn << \" \" << nxm << \" \" << nxnd << endl;\n }\n }\n }\n }\n cout << (res==INT_MAX ? -1 : res) << endl;\n \n }while(true);\n return 0;\n \n}", "accuracy": 1, "time_ms": 160, "memory_kb": 9356, "score_of_the_acc": -1.3049, "final_rank": 13 }, { "submission_id": "aoj_2212_8882730", "code_snippet": "/*\nAC\n*/\n#include<iostream>\n#include<set>\n#include<algorithm>\n#include<vector>\n#include<string>\n#include<set>\n#include<map>\n#include<numeric>\n#include<queue>\n#include<cmath>\n#include<cassert>\n#include<unordered_map>\n#include<optional>\n#include<tuple>\nusing namespace std;\ntypedef long long ll;\nconst ll INF=1LL<<60;\ntypedef pair<int,int> P;\ntypedef pair<int,P> PP;\nconst ll MOD=998244353;\n\n\nstruct Aho_Corasick{\n typedef unordered_map<char,int> MP;\n\n vector<MP> trie;\n vector<int> cnt;//cnt,treeの大きさは同じ\n const int root=0;\n \n Aho_Corasick(){\n trie.push_back(MP());\n cnt.push_back(0);\n }\n\n unordered_map<int,vector<string>> output;\n\n void add(const vector<string>& terms){\n for(const string& term:terms){\n add(term);\n }\n }\n\n //計算量は挿入する文字列の長さの和\n int add(const string& string){\n int now=root;//根\n for(char c:string){\n if(trie[now].count(c)){\n now=trie[now][c];\n }else{\n int pre=now;\n now=trie.size();\n cnt.push_back(0);\n trie.push_back(MP());\n trie[pre][c]=now;\n }\n }\n\n cnt[now]++;//終端文字列\n\n output[now].push_back(string);//終端ノードに対してsを入れる\n \n\n return now;\n }\n\n void match(const string& string){\n\n cout<<\"target: string=\"<<string<<endl;\n \n int now=0;\n \n for(int i=0;i<string.size();i++){\n char c=string[i];\n //今の状態nowからcを用いて遷移する場所を探す.\n while(!go(now,c)){\n now=failure[now];\n }\n //go(now,string[i]).firstがtrue\n now=*go(now,c);\n\n for(const auto& x:output[now]){\n //ノードnowで終わる場合\n cout<<\"start=\"<<i-x.size()+1<<\",goal=\"<<i<<\",string:\"<<x<<endl;\n }\n }\n \n }\n\n vector<int> failure;\n vector<int> dist;\n \n\n //ノードnowから文字列cをうけていく場所\n optional<int> go(int now,char c){\n \n if(trie[now].count(c)){\n return trie[now][c];\n }else if(now==root) return root;\n else return {};\n \n }\n\n void bfs(){\n \n failure=vector<int>(trie.size(),0);\n dist=vector<int>(trie.size(),-1);\n queue<int> que;\n \n dist[root]=0;\n que.push(root);\n\n while(!que.empty()){\n int now=que.front();\n que.pop();\n \n for(auto [c,to]:trie[now]){\n //nowのキーc,toを取り出す\n //trie[now][c]=toとなる\n \n if(now!=root){\n int f=failure[now];//nowで失敗した場合に行きつく場所\n \n while(!go(f,c)){\n f=failure[f];\n }\n //go(f,c).firstがtrueつまり状態fの次に文字cで行ける場所がある もしくはfがroot\n failure[to]=*go(f,c);\n\n for(const auto& string:output[failure[to]]){\n //ノードfailure[to]で終わる文字列たちをtoにもいれる\n output[to].push_back(string);\n }\n \n }\n\n que.push(to);\n dist[to]=dist[now]+1;\n \n }\n\n }\n \n\n }\n \n};\n\nstruct State{\n \n P pos;\n int node;\n int dist;\n string str;//デバッグのために使用\n State(P pos_,int node_,int dist_):pos(pos_),node(node_),dist(dist_){str=\"\";};\n};\n\n\n//U,R,D,L\nconst int dx[]={0,1,0,-1};\nconst int dy[]={-1,0,1,0};\nconst string dir=\"URDL\";\n\nvector<int> ans;\n\nbool solve(){\n int n,m;\n cin>>n>>m;\n if(n==0 && m==0)return false;\n\n P start,goal;\n vector<string> s(n);\n for(int i=0;i<n;i++){\n cin>>s[i];\n for(int j=0;j<m;j++){\n if(s[i][j]=='S'){\n start={i,j};\n }\n if(s[i][j]=='G'){\n goal={i,j};\n }\n }\n }\n int p;\n cin>>p;\n\n Aho_Corasick trie;\n vector<string> terms(p);\n for(int i=0;i<p;i++){\n cin>>terms[i];\n trie.add(terms[i]);\n }\n trie.bfs();\n int sz=trie.trie.size();\n \n using TP=tuple<int,int,int>;\n queue<State> que;\n map<TP,int> used;//同じ状態を二度と踏まないようにするため\n que.push(State{start,trie.root,0});\n used[make_tuple(start.first,start.second,trie.root)]=0;\n const int inf=1<<30;\n int res=inf;\n\n while(!que.empty()){\n State now_state=que.front();\n P pos=now_state.pos;\n int node=now_state.node;\n int d=now_state.dist;\n \n que.pop();\n if(pos==goal){\n //cout<<now_state.str<<endl;\n res=d;\n break;\n \n }\n\n for(int k=0;k<4;k++){\n int nx = pos.second + dx[k];\n int ny = pos.first + dy[k];\n\n if(nx<0 || m<=nx || ny<0 || n<=ny) continue;\n if(s[ny][nx]=='#') continue;\n\n //いまいるnodeからdir[k]をつけ足す場合にどこへいくか\n optional<int> res=trie.go(node,dir[k]);\n \n int next_node=0;\n if(!trie.go(node,dir[k])){\n //失敗した場合に,そこから文字dir[k]を用いて次に行くべき場所\n int f=trie.failure[node];\n while(!trie.go(f,dir[k])){\n f=trie.failure[f];\n }\n next_node=*trie.go(f,dir[k]);\n\n\n }else{\n //次に行くべき場所.確実にわかっている\n next_node=*trie.go(node,dir[k]);\n }\n \n if(trie.output[next_node].size()==0){\n //next_nodeは終端文字でない\n State next_state{P{ny,nx},next_node,d+1};\n \n next_state.str=now_state.str+dir[k];//debugの際に使用\n\n if(!used.count(make_tuple(ny,nx,next_node))){\n used[make_tuple(ny,nx,next_node)]=d+1;\n que.push(next_state);\n \n }\n }\n \n\n\n }\n }\n\n res=(res==inf?-1:res);\n ans.push_back(res);\n\n return true;\n\n}\n\n\nint main(){\n \n while(1){\n if(!solve()){\n break;\n }\n }\n \n\n for(int v:ans){\n cout<<v<<endl;\n }\n\n\n\n}", "accuracy": 1, "time_ms": 360, "memory_kb": 9884, "score_of_the_acc": -1.9596, "final_rank": 18 }, { "submission_id": "aoj_2212_8882702", "code_snippet": "/*\nAC\n\n*/\n#include<iostream>\n#include<set>\n#include<algorithm>\n#include<vector>\n#include<string>\n#include<set>\n#include<map>\n#include<numeric>\n#include<queue>\n#include<cmath>\n#include<cassert>\n#include<unordered_map>\n#include<optional>\n#include<tuple>\nusing namespace std;\ntypedef long long ll;\nconst ll INF=1LL<<60;\ntypedef pair<int,int> P;\ntypedef pair<int,P> PP;\nconst ll MOD=998244353;\n\n\nstruct Trie{\n typedef unordered_map<char,int> MP;\n\n vector<MP> tree;\n vector<int> cnt;//cnt,treeの大きさは同じ\n const int root=0;\n Trie(){\n tree.push_back(MP());\n cnt.push_back(0);\n }\n\n unordered_map<int,vector<string>> output;\n\n void add(const vector<string>& terms){\n for(const string& term:terms){\n add(term);\n }\n }\n\n //計算量は挿入する文字列の長さの和\n int add(const string& string){\n int now=root;//根\n for(char c:string){\n if(tree[now].count(c)){\n now=tree[now][c];\n }else{\n int pre=now;\n now=tree.size();\n cnt.push_back(0);\n tree.push_back(MP());\n tree[pre][c]=now;\n }\n }\n\n cnt[now]++;//終端文字列\n\n output[now].push_back(string);//終端ノードに対してsを入れる\n \n\n return now;\n }\n\n void match(const string& string){\n\n cout<<\"target: string=\"<<string<<endl;\n \n int now=0;\n \n for(int i=0;i<string.size();i++){\n char c=string[i];\n //今の状態nowからcを用いて遷移する場所を探す.\n while(!go(now,c)){\n now=failure[now];\n }\n //go(now,string[i]).firstがtrue\n now=*go(now,c);\n\n for(const auto& x:output[now]){\n //ノードnowで終わる場合\n cout<<\"start=\"<<i-x.size()+1<<\",goal=\"<<i<<\",string:\"<<x<<endl;\n }\n }\n \n }\n\n vector<int> failure;\n vector<int> dist;\n \n\n //ノードnowから文字列cをうけていく場所\n optional<int> go(int now,char c){\n \n if(tree[now].count(c)){\n return tree[now][c];\n }else if(now==root) return root;\n else return {};\n \n }\n\n void bfs(){\n \n failure=vector<int>(tree.size(),0);\n dist=vector<int>(tree.size(),-1);\n queue<int> que;\n \n dist[root]=0;\n que.push(root);\n\n while(!que.empty()){\n int now=que.front();\n que.pop();\n \n for(auto [c,to]:tree[now]){\n //nowのキーc,toを取り出す\n //tree[now][c]=toとなる\n \n if(now!=root){\n int f=failure[now];//nowで失敗した場合に行きつく場所\n \n while(!go(f,c)){\n f=failure[f];\n }\n //go(f,c).firstがtrueつまり状態fの次に文字cで行ける場所がある もしくはfがroot\n failure[to]=*go(f,c);\n\n for(const auto& string:output[failure[to]]){\n //ノードfailure[to]で終わる文字列たちをtoにもいれる\n output[to].push_back(string);\n }\n \n }\n\n que.push(to);\n dist[to]=dist[now]+1;\n \n }\n\n }\n \n\n }\n \n};\n\nstruct State{\n \n P pos;\n int node;\n int dist;\n string str;//デバッグのために使用\n State(P pos_,int node_,int dist_):pos(pos_),node(node_),dist(dist_){str=\"\";};\n};\n\n\n//U,R,D,L\nconst int dx[]={0,1,0,-1};\nconst int dy[]={-1,0,1,0};\nconst string dir=\"URDL\";\n\nvector<int> ans;\n\nbool solve(){\n int n,m;\n cin>>n>>m;\n if(n==0 && m==0)return false;\n\n P start,goal;\n vector<string> s(n);\n for(int i=0;i<n;i++){\n cin>>s[i];\n for(int j=0;j<m;j++){\n if(s[i][j]=='S'){\n start={i,j};\n }\n if(s[i][j]=='G'){\n goal={i,j};\n }\n }\n }\n int p;\n cin>>p;\n\n Trie trie;\n vector<string> terms(p);\n for(int i=0;i<p;i++){\n cin>>terms[i];\n trie.add(terms[i]);\n }\n trie.bfs();\n int sz=trie.tree.size();\n \n using TP=tuple<int,int,int>;\n queue<State> que;\n map<TP,int> used;//同じ状態を二度と踏まないようにするため\n que.push(State{start,trie.root,0});\n used[make_tuple(start.first,start.second,trie.root)]=0;\n const int inf=1<<30;\n int res=inf;\n\n while(!que.empty()){\n State now_state=que.front();\n P pos=now_state.pos;\n int node=now_state.node;\n int d=now_state.dist;\n \n que.pop();\n if(pos==goal){\n //cout<<now_state.str<<endl;\n res=d;\n break;\n \n }\n\n for(int k=0;k<4;k++){\n int nx = pos.second + dx[k];\n int ny = pos.first + dy[k];\n\n if(nx<0 || m<=nx || ny<0 || n<=ny) continue;\n if(s[ny][nx]=='#') continue;\n\n //いまいるnodeからdir[k]をつけ足す場合にどこへいくか\n optional<int> res=trie.go(node,dir[k]);\n \n int next_node=0;\n if(!trie.go(node,dir[k])){\n //失敗した場合に,そこから文字dir[k]を用いて次に行くべき場所\n int f=trie.failure[node];\n while(!trie.go(f,dir[k])){\n f=trie.failure[f];\n }\n next_node=*trie.go(f,dir[k]);\n\n\n }else{\n //次に行くべき場所.確実にわかっている\n next_node=*trie.go(node,dir[k]);\n }\n \n if(trie.output[next_node].size()==0){\n //next_nodeは終端文字でない\n State next_state{P{ny,nx},next_node,d+1};\n \n next_state.str=now_state.str+dir[k];//debugの際に使用\n\n if(!used.count(make_tuple(ny,nx,next_node))){\n used[make_tuple(ny,nx,next_node)]=d+1;\n que.push(next_state);\n \n }\n }\n \n\n\n }\n }\n\n res=(res==inf?-1:res);\n ans.push_back(res);\n\n return true;\n\n}\n\n\nint main(){\n \n while(1){\n if(!solve()){\n break;\n }\n }\n \n\n for(int v:ans){\n cout<<v<<endl;\n }\n\n\n\n}", "accuracy": 1, "time_ms": 360, "memory_kb": 9884, "score_of_the_acc": -1.9596, "final_rank": 18 }, { "submission_id": "aoj_2212_8882657", "code_snippet": "/*\n途中までaho-corasick\n\nhttps://naoya-2.hatenadiary.org/entry/20090405/aho_corasick\n\n*/\n#include<iostream>\n#include<set>\n#include<algorithm>\n#include<vector>\n#include<string>\n#include<set>\n#include<map>\n#include<numeric>\n#include<queue>\n#include<cmath>\n#include<cassert>\n#include<unordered_map>\n#include<optional>\n#include<tuple>\nusing namespace std;\ntypedef long long ll;\nconst ll INF=1LL<<60;\ntypedef pair<int,int> P;\ntypedef pair<int,P> PP;\nconst ll MOD=998244353;\n\n\nstruct Trie{\n typedef unordered_map<char,int> MP;\n\n vector<MP> tree;\n vector<int> cnt;//cnt,treeの大きさは同じ\n const int root=0;\n Trie(){\n tree.push_back(MP());\n cnt.push_back(0);\n }\n\n unordered_map<int,vector<string>> output;\n\n void add(const vector<string>& terms){\n for(const string& term:terms){\n add(term);\n }\n }\n\n //計算量は挿入する文字列の長さの和\n int add(const string& string){\n int now=root;//根\n for(char c:string){\n if(tree[now].count(c)){\n now=tree[now][c];\n }else{\n int pre=now;\n now=tree.size();\n cnt.push_back(0);\n tree.push_back(MP());\n tree[pre][c]=now;\n }\n }\n\n cnt[now]++;//終端文字列\n\n output[now].push_back(string);//終端ノードに対してsを入れる\n //cout<<\"s=\"<<string<<\",now=\"<<now<<endl;\n\n return now;\n }\n\n void match(const string& string){\n\n cout<<\"target: string=\"<<string<<endl;\n \n int now=0;\n \n for(int i=0;i<string.size();i++){\n char c=string[i];\n //今の状態nowからcを用いて遷移する場所を探す.\n while(!go(now,c)){\n now=failure[now];\n }\n //go(now,string[i]).firstがtrue\n now=*go(now,c);\n\n for(const auto& x:output[now]){\n //ノードnowで終わる場合\n cout<<\"start=\"<<i-x.size()+1<<\",goal=\"<<i<<\",string:\"<<x<<endl;\n }\n }\n \n }\n\n vector<int> failure;\n vector<int> dist;\n \n\n //ノードnowから文字列cをうけていく場所\n optional<int> go(int now,char c){\n \n if(tree[now].count(c)){\n return tree[now][c];\n }else if(now==root) return root;\n else return {};\n \n }\n\n void bfs(){\n \n failure=vector<int>(tree.size(),0);\n dist=vector<int>(tree.size(),-1);\n queue<int> que;\n \n dist[root]=0;\n que.push(root);\n\n while(!que.empty()){\n int now=que.front();\n que.pop();\n //cout<<\"now=\"<<now<<endl;\n \n for(auto [c,to]:tree[now]){\n //nowのキーc,toを取り出す\n //tree[now][c]=toとなる\n //cout<<\"c=\"<<c<<\",to=\"<<to<<endl;\n \n if(now!=root){\n //cout<<\"now=\"<<now<<endl;\n int f=failure[now];//nowで失敗した場合に行きつく場所\n //cout<<\"finit=\"<<f<<endl;\n\n while(!go(f,c)){\n //while(f!=root && !tree[f].count(c)){\n //cout<<\"f=\"<<f<<endl;\n f=failure[f];\n }\n //cout<<\"f=\"<<f<<endl;\n //cout<<\"go(f,c).second=\"<<go(f,c).second<<endl;\n //go(f,c).firstがtrueつまり状態fの次に文字cで行ける場所がある もしくはfがroot\n //failure[to]=(go(f,c).second);\n failure[to]=*go(f,c);\n\n //failure[to]=(tree[f].count(c)?tree[f][c]:root);//(go(f,c).second);\n \n for(const auto& string:output[failure[to]]){\n //ノードfailure[to]で終わる文字列たちをtoにもいれる\n output[to].push_back(string);\n }\n \n }\n\n que.push(to);\n dist[to]=dist[now]+1;\n \n }\n\n }\n \n\n }\n \n};\n\nstruct State{\n \n P pos;\n int node;\n int dist;\n string str;\n State(P pos_,int node_,int dist_):pos(pos_),node(node_),dist(dist_){str=\"\";};\n};\n\n\n//U,R,D,L\nconst int dx[]={0,1,0,-1};\nconst int dy[]={-1,0,1,0};\nconst string dir=\"URDL\";\n\nvector<int> ans;\n\nbool solve(){\n int n,m;\n cin>>n>>m;\n if(n==0 && m==0)return false;\n\n P start,goal;\n vector<string> s(n);\n for(int i=0;i<n;i++){\n cin>>s[i];\n for(int j=0;j<m;j++){\n if(s[i][j]=='S'){\n start={i,j};\n }\n if(s[i][j]=='G'){\n goal={i,j};\n }\n }\n }\n int p;\n cin>>p;\n\n Trie trie;\n vector<string> terms(p);\n for(int i=0;i<p;i++){\n cin>>terms[i];\n trie.add(terms[i]);\n }\n trie.bfs();\n int sz=trie.tree.size();\n /*\n for(int i=0;i<sz;i++){\n cout<<\"trie.failure[\"<<i<<\"]=\"<<trie.failure[i]<<endl;\n }\n */\n \n //cout<<\"te\"<<endl;\n queue<State> que;\n que.push(State{start,trie.root,0});\n typedef tuple<int,int,int> TP;\n map<TP,int> used;//同じ状態を二度と踏まないようにするため\n used[make_tuple(start.first,start.second,trie.root)]=0;\n const int inf=1<<30;\n int res=inf;\n\n while(!que.empty()){\n\n //auto [pos,node,d]=que.front();\n auto now_state=que.front();\n P pos=now_state.pos;\n int node=now_state.node;\n int d=now_state.dist;\n //cout<<now_state.str<<endl;\n que.pop();\n if(pos==goal){\n //cout<<\"ans string=\"<<now_state.str<<endl;\n\n res=d;\n break;\n \n }\n\n for(int k=0;k<4;k++){\n int nx = pos.second + dx[k];\n int ny = pos.first + dy[k];\n\n if(nx<0 || m<=nx || ny<0 || n<=ny) continue;\n if(s[ny][nx]=='#') continue;\n\n //いまいるnodeからdir[k]をつけ足す場合にどこへいくか\n optional<int> res=trie.go(node,dir[k]);\n \n int next_node=0;\n if(!trie.go(node,dir[k])){\n //失敗した場合に,そこから文字dir[k]を用いて次に行くべき場所\n int f=trie.failure[node];\n while(!trie.go(f,dir[k])){\n f=trie.failure[f];\n }\n next_node=*trie.go(f,dir[k]);\n\n\n }else{\n //次に行くべき場所.確実にわかっている\n next_node=*trie.go(node,dir[k]);\n }\n \n //cout<<\"from=\"<<node<<\",char c=\"<<dir[k]<<\",next_node=\"<<next_node<<endl;\n \n //cout<<\"next_node=\"<<next_node<<endl;\n if(trie.output[next_node].size()==0){\n //next_nodeは終端文字でない\n State next_state{P{ny,nx},next_node,d+1};\n \n next_state.str=now_state.str+dir[k];\n\n if(!used.count(make_tuple(ny,nx,next_node))){\n used[make_tuple(ny,nx,next_node)]=d+1;\n que.push(next_state);\n \n }\n }\n \n\n\n }\n }\n\n res=(res==inf?-1:res);\n //cout<<\"res=\"<<res<<endl;\n ans.push_back(res);\n\n return true;\n\n}\n\n\nint main(){\n \n while(1){\n if(!solve()){\n break;\n }\n }\n \n\n for(int v:ans){\n cout<<v<<endl;\n }\n\n\n\n}", "accuracy": 1, "time_ms": 360, "memory_kb": 9908, "score_of_the_acc": -1.9634, "final_rank": 20 }, { "submission_id": "aoj_2212_8423846", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define repd(i,a,b) for (ll i=(a);i<(b);i++)\n#define rep(i,n) repd(i,0,n)\n#define all(x) (x).begin(),(x).end()\ntemplate<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }\ntemplate<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }\ntypedef long long ll;\ntypedef pair<ll,ll> P;\ntypedef vector<ll> vec;\nusing Graph = vector<vector<ll>>;\nconst long long INF = 1LL<<60;\nconst long long MOD = 1000000007;\n \n// Aho-Corasick\n// https://youtu.be/BYoRvdgI5EU?t=9633\nstruct Aho {\n using MP = unordered_map<char,int>;\n vector<MP> to;\n vector<int> cnt, fail;\n Aho(): to(1), cnt(1) {}\n int add(const string& s) {\n int v = 0;\n for (char c : s) {\n if (!to[v].count(c)) {\n to[v][c] = to.size();\n to.push_back(MP());\n cnt.push_back(0);\n }\n v = to[v][c];\n }\n cnt[v]++;\n return v;\n }\n void init() {\n fail = vector<int>(to.size(), -1);\n queue<int> q;\n q.push(0);\n while (!q.empty()) {\n int v = q.front(); q.pop();\n for (auto [c,u] : to[v]) {\n fail[u] = (*this)(fail[v],c);\n cnt[u] += cnt[fail[u]];\n q.push(u);\n }\n }\n }\n int operator()(int v, char c) const {\n while (v != -1) {\n auto it = to[v].find(c);\n if (it != to[v].end()) return it->second;\n v = fail[v];\n }\n return 0;\n }\n int operator[](int v) const { return cnt[v];}\n};\n\nusing F=tuple<int,int,int,int>;\n\nint main()\n{ \n ios::sync_with_stdio(false);\n cin.tie(0);\n int n,m;\n while(cin>>n>>m){\n if(n==0&&m==0)break;\n vector<string> g(n);\n rep(i,n)cin>>g[i];\n ll q;cin>>q;\n Aho aho;\n rep(qi,q){\n string s;cin>>s;\n aho.add(s);\n }\n aho.init();\n ll sz=aho.to.size();\n vector dp(n,vector(m,vector<int>(sz,1e9)));\n ll fx,fy;\n ll gx,gy;\n rep(i,n)rep(j,m){\n if(g[i][j]=='S')fx=i,fy=j;\n if(g[i][j]=='G')gx=i,gy=j;\n }\n queue<F> que;\n dp[fx][fy][0]=0;\n que.push({fx,fy,0,0});\n vec dx={-1,1,0,0},dy={0,0,1,-1};\n vector<char> memo={'U','D','R','L'};\n while(que.size()){\n auto[cx,cy,cz,cw]=que.front();\n que.pop();\n rep(i,4){\n ll nx=cx+dx[i];\n ll ny=cy+dy[i];\n ll nz=aho(cz,memo[i]);\n if(aho[nz])continue;\n if(nx<0||nx>=n||ny<0||ny>=m)continue;\n if(g[nx][ny]=='#')continue;\n if(chmin(dp[nx][ny][nz],cw+1)){\n que.push({nx,ny,nz,cw+1});\n }\n }\n }\n int ans=1e9;\n rep(i,sz){\n chmin(ans,dp[gx][gy][i]);\n }\n if(ans==1e9)ans=-1;\n cout<<ans<<'\\n';\n }\n return 0;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 3952, "score_of_the_acc": -0.1668, "final_rank": 10 }, { "submission_id": "aoj_2212_6944196", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long int;\nusing ull = unsigned long long int;\nusing P = pair<ll, ll>;\nusing P3 = pair<ll, P>;\nusing PP = pair<P, P>;\nconstexpr int INF32 = 1 << 30;\nconstexpr ll INF64 = 1LL << 62;\nconstexpr ll MOD = 1000000007;\n// constexpr ll MOD = 998244353;\nconstexpr int di[] = {0, 1, 0, -1};\nconstexpr int dj[] = {1, 0, -1, 0};\nconstexpr int di8[] = {0, 1, 1, 1, 0, -1, -1, -1};\nconstexpr int dj8[] = {1, 1, 0, -1, -1, -1, 0, 1};\nconstexpr double EPS = 1e-10;\nconst double PI = acos(-1);\n\n#define ALL(v) (v).begin(), (v).end()\n#define REP(i, n) for (int i = 0, i_len = n; i < i_len; ++i)\n\ntemplate<typename T1,typename T2> bool chmax(T1 &a, T2 b) { if (a<b) { a=b; return 1; } return 0; }\ntemplate<typename T1,typename T2> bool chmin(T1 &a, T2 b) { if (b<a) { a=b; return 1; } return 0; }\n\ntemplate <int64_t Modulus>\nstruct Modint {\n using mint = Modint;\n long long v;\n Modint() : v(0) {}\n Modint(long long x) {\n x %= Modulus;\n if (x < 0) x += Modulus;\n v = x;\n }\n const long long& val() const { return v; }\n // 代入演算子\n mint& operator+=(const mint rhs) {\n v += rhs.v;\n if (v >= Modulus) v -= Modulus;\n return *this;\n }\n mint& operator-=(const mint rhs) {\n if (v < rhs.v) v += Modulus;\n v -= rhs.v;\n return *this;\n }\n mint& operator*=(const mint rhs) {\n v = v * rhs.v % Modulus;\n return *this;\n }\n mint& operator/=(mint rhs) { return *this = *this * rhs.inv(); }\n // 累乗, 逆元\n mint pow(long long n) const {\n mint x = *this, res = 1;\n while (n) {\n if (n & 1) res *= x;\n x *= x;\n n >>= 1;\n }\n return res;\n }\n mint inv() const { return pow(Modulus - 2); }\n // 算術演算子\n mint operator+(const mint rhs) const { return mint(*this) += rhs; }\n mint operator-(const mint rhs) const { return mint(*this) -= rhs; }\n mint operator*(const mint rhs) const { return mint(*this) *= rhs; }\n mint operator/(const mint rhs) const { return mint(*this) /= rhs; }\n mint operator-() const { return mint() - *this; } // 単項\n // 入出力ストリーム\n friend ostream& operator<<(ostream& os, const mint& p) { return os << p.v; }\n friend istream& operator>>(istream& is, mint& p) {\n long long t;\n is >> t;\n p = mint(t);\n return (is);\n }\n};\n\nusing mint = Modint<MOD>;\n\nstruct AhoCorasick {\n private:\n static constexpr int C_SIZE = 26; // C_SIZE : 文字の種類数\n static constexpr int C_BEGIN = 'a'; // C_BEGIN : 開始文字\n struct Node {\n array<int, C_SIZE> to = {}; // to[c]=文字cによる遷移先ノード, 存在しなければ-1\n int id = -1; // そのノードでマッチする文字列のID, 存在しなければ-1\n int fail = ROOT;\n int sfx = ROOT;\n Node() { to.fill(-1); }\n };\n\n public:\n static constexpr int ROOT = 0;\n vector<Node> nodes;\n vector<string> patterns; // 追加した文字列\n AhoCorasick() : nodes(1) {}\n // nodes[idx]から文字cで遷移したときの頂点のindex\n int next_state(int now, char c) {\n return nodes[now].to[c - C_BEGIN];\n }\n // 文字列の追加\n void insert(const string& str) {\n patterns.push_back(str);\n int now = ROOT;\n for (char c : str) {\n int k = c - C_BEGIN;\n int& nxt = nodes[now].to[k];\n if (nxt == -1) {\n now = nxt = int(nodes.size());\n nodes.push_back(Node());\n } else {\n now = nxt;\n }\n }\n nodes[now].id = int(patterns.size())-1;\n }\n void build() {\n queue<int> que;\n for (int& x : nodes[ROOT].to) {\n if (x == -1) {\n x = ROOT;\n } else {\n que.push(x);\n }\n }\n while (!que.empty()) {\n int now = que.front();\n que.pop();\n int fail = nodes[now].fail;\n for (int k = 0; k < C_SIZE; k++) {\n int& nxt = nodes[now].to[k];\n if (nxt == -1) { // 遷移失敗\n nxt = nodes[fail].to[k]; // failure link で遷移してから遷移\n } else { // 遷移成功\n nodes[nxt].fail = nodes[fail].to[k]; // 遷移先のfailure linkを求める\n que.push(nxt);\n }\n }\n nodes[now].sfx = (nodes[fail].id != -1 ? fail : nodes[fail].sfx);\n }\n }\n // stateにマッチするすべてのパターンのidを返す\n vector<int> match(int state) {\n vector<int> res;\n int now = state;\n while (now != ROOT) {\n if (nodes[now].id != -1) res.push_back(nodes[now].id);\n now = nodes[now].sfx;\n }\n return res;\n }\n // 文字列に対応するnodeのindexを検索, 存在しなければ-1\n vector<vector<int>> search(const string& str) {\n vector<vector<int>> res;\n int now = ROOT;\n for (auto c : str) {\n int nxt = next_state(now, c);\n res.emplace_back(match(nxt));\n now = nxt;\n }\n return res;\n }\n};\n\n\n\n\nint solve() {\n int n, m, p;\n cin >> n >> m;\n if(n == 0) return 1;\n vector<string> s(n);\n for(int i=0;i<n;i++) cin >> s[i];\n cin >> p;\n AhoCorasick ac;\n for(int i=0;i<p;i++){\n string t;\n cin >> t;\n for(char& c : t) c = tolower(c);\n ac.insert(t);\n }\n ac.build();\n int si, sj, gi, gj;\n for(int i=0;i<n;i++){\n for(int j=0;j<m;j++){\n if(s[i][j] == 'S'){\n si = i;\n sj = j;\n }\n if(s[i][j] == 'G'){\n gi = i;\n gj = j;\n }\n }\n }\n const string dir = \"rdlu\";\n int l = ac.nodes.size();\n int dist[50][50][100]{};\n for(int i=0;i<n;i++){\n for(int j=0;j<m;j++){\n fill(dist[i][j], dist[i][j]+l, INF32);\n }\n }\n queue<tuple<int,int,int>> que;\n que.push({si,sj,ac.ROOT});\n dist[si][sj][ac.ROOT] = 0;\n while(!que.empty()){\n auto [i,j,now] = que.front();\n que.pop();\n for(int k=0;k<4;k++){\n int ni = i+di[k], nj = j+dj[k];\n if(ni<0 || ni >= n || nj<0 || nj>=m) continue;\n if(s[ni][nj] == '#') continue;\n int nxt = ac.next_state(now, dir[k]);\n if(ac.match(nxt).size() > 0) continue;\n if(chmin(dist[ni][nj][nxt], dist[i][j][now]+1)){\n que.push({ni,nj,nxt});\n }\n }\n }\n int ans = INF32;\n for(int i=0;i<l;i++){\n chmin(ans, dist[gi][gj][i]);\n }\n cout << (ans < INF32 ? ans : -1) << endl;\n return 0;\n}\n\nint main() {\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n // solve();\n while(!solve());\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 4356, "score_of_the_acc": -0.1163, "final_rank": 7 }, { "submission_id": "aoj_2212_5286728", "code_snippet": "#define _USE_MATH_DEFINES\n#include <bits/stdc++.h>\nusing namespace std;\n/*\n#include <atcoder/all>\nusing namespace atcoder;\n*/\n/*\n#include <boost/multiprecision/cpp_int.hpp>\n#include <boost/multiprecision/cpp_dec_float.hpp>\nusing bll = boost::multiprecision::cpp_int;\nusing bdouble = boost::multiprecision::number<boost::multiprecision::cpp_dec_float<100>>;\nusing namespace boost::multiprecision;\n*/\n#if defined(LOCAL_TEST) || defined(LOCAL_DEV)\n\t#define BOOST_STACKTRACE_USE_ADDR2LINE\n\t#define BOOST_STACKTRACE_ADDR2LINE_LOCATION /usr/local/opt/binutils/bin/addr2line\n\t#define _GNU_SOURCE 1\n\t#include <boost/stacktrace.hpp>\n#endif\n#ifdef LOCAL_TEST\n\tnamespace std {\n\t\ttemplate<typename T> class dvector : public std::vector<T> {\n\t\tpublic:\n\t\t\tdvector() : std::vector<T>() {}\n\t\t\texplicit dvector(size_t n, const T& value = T()) : std::vector<T>(n, value) {}\n\t\t\tdvector(const std::vector<T>& v) : std::vector<T>(v) {}\n\t\t\tdvector(const std::initializer_list<T> il) : std::vector<T>(il) {}\n\t\t\tdvector(const std::string::iterator first, const std::string::iterator last) : std::vector<T>(first, last) {}\n\t\t\tdvector(const typename std::vector<T>::iterator first, const typename std::vector<T>::iterator last) : std::vector<T>(first, last) {}\n\t\t\tdvector(const typename std::vector<T>::reverse_iterator first, const typename std::vector<T>::reverse_iterator last) : std::vector<T>(first, last) {}\n\t\t\tdvector(const typename std::vector<T>::const_iterator first, const typename std::vector<T>::const_iterator last) : std::vector<T>(first, last) {}\n\t\t\tdvector(const typename std::vector<T>::const_reverse_iterator first, const typename std::vector<T>::const_reverse_iterator last) : std::vector<T>(first, last) {}\n\t\t\tT& operator[](size_t n) {\n\t\t\t\tif (this->size() <= n) { std::cerr << boost::stacktrace::stacktrace() << '\\n' << \"vector::_M_range_check: __n (which is \" << n << \") >= this->size() (which is \" << this->size() << \")\" << '\\n'; } return this->at(n);\n\t\t\t}\n\t\t\tconst T& operator[](size_t n) const {\n\t\t\t\tif (this->size() <= n) { std::cerr << boost::stacktrace::stacktrace() << '\\n' << \"vector::_M_range_check: __n (which is \" << n << \") >= this->size() (which is \" << this->size() << \")\" << '\\n'; } return this->at(n);\n\t\t\t}\n\t\t};\n\t\ttemplate<typename T, typename Compare = less<T>, class Allocator = allocator<T>> class dmultiset : public std::multiset<T,Compare,Allocator> {\n\t\tpublic:\n\t\t\tdmultiset() : std::multiset<T,Compare,Allocator>() {}\n\t\t\tdmultiset(const std::multiset<T,Compare,Allocator>& m) : std::multiset<T,Compare,Allocator>(m) {}\n\t\t\tdmultiset(const std::initializer_list<T> il) : std::multiset<T,Compare,Allocator>(il) {}\n\t\t\tdmultiset(const Compare& comp) : std::multiset<T,Compare,Allocator>(comp) {}\n\t\t\tconst typename std::multiset<T,Compare,Allocator>::iterator erase(const typename std::multiset<T,Compare,Allocator>::iterator it) {\n\t\t\t\treturn std::multiset<T,Compare,Allocator>::erase(it);\n\t\t\t}\n\t\t\tsize_t erase([[maybe_unused]] const T& x) {\n\t\t\t\tstd::cerr << boost::stacktrace::stacktrace() << '\\n'; assert(false);\n\t\t\t}\n\t\t\tsize_t erase_all_elements(const T& x) {\n\t\t\t\treturn std::multiset<T,Compare,Allocator>::erase(x);\n\t\t\t}\n\t\t};\n\t}\n\tclass dbool {\n\tprivate:\n\t\tbool boolvalue;\n\tpublic:\n\t\tdbool() : boolvalue(false) {}\n\t\tdbool(bool b) : boolvalue(b) {}\n\t\toperator bool&() { return boolvalue; }\n\t\toperator const bool&() const { return boolvalue; }\n\t};\n\t#define vector dvector\n\t#define multiset dmultiset\n\t#define bool dbool\n\tclass SIGFPE_exception : std::exception {};\n\tclass SIGSEGV_exception : std::exception {};\n\tvoid catch_SIGFPE([[maybe_unused]] int e) { std::cerr << boost::stacktrace::stacktrace() << '\\n'; throw SIGFPE_exception(); }\n\tvoid catch_SIGSEGV([[maybe_unused]] int e) { std::cerr << boost::stacktrace::stacktrace() << '\\n'; throw SIGSEGV_exception(); }\n\tsigned convertedmain();\n\tsigned main() { signal(SIGFPE, catch_SIGFPE); signal(SIGSEGV, catch_SIGSEGV); return convertedmain(); }\n\t#define main() convertedmain()\n#else\n\t#define erase_all_elements erase\n#endif\n#ifdef LOCAL_DEV\n\ttemplate<typename T1, typename T2> std::ostream& operator<<(std::ostream& s, const std::pair<T1, T2>& p) {\n\t\treturn s << \"(\" << p.first << \", \" << p.second << \")\"; }\n\ttemplate <typename T, size_t N> std::ostream& operator<<(std::ostream& s, const std::array<T, N>& a) {\n\t\ts << \"{ \"; for (size_t i = 0; i < N; ++i){ s << a[i] << \"\\t\"; } s << \"}\"; return s; }\n\ttemplate<typename T> std::ostream& operator<<(std::ostream& s, const std::set<T>& se) {\n\t\ts << \"{ \"; for (auto itr = se.begin(); itr != se.end(); ++itr){ s << (*itr) << \"\\t\"; } s << \"}\"; return s; }\n\ttemplate<typename T> std::ostream& operator<<(std::ostream& s, const std::multiset<T>& se) {\n\t\ts << \"{ \"; for (auto itr = se.begin(); itr != se.end(); ++itr){ s << (*itr) << \"\\t\"; } s << \"}\"; return s; }\n\ttemplate<typename T1, typename T2> std::ostream& operator<<(std::ostream& s, const std::map<T1, T2>& m) {\n\t\ts << \"{\\n\"; for (auto itr = m.begin(); itr != m.end(); ++itr){ s << \"\\t\" << (*itr).first << \" : \" << (*itr).second << \"\\n\"; } s << \"}\"; return s; }\n\ttemplate<typename T> std::ostream& operator<<(std::ostream& s, const std::deque<T>& v) {\n\t\tfor (size_t i = 0; i < v.size(); ++i){ s << v[i]; if (i < v.size() - 1) s << \"\\t\"; } return s; }\n\ttemplate<typename T> std::ostream& operator<<(std::ostream& s, const std::vector<T>& v) {\n\t\tfor (size_t i = 0; i < v.size(); ++i){ s << v[i]; if (i < v.size() - 1) s << \"\\t\"; } return s; }\n\ttemplate<typename T> std::ostream& operator<<(std::ostream& s, const std::vector<std::vector<T>>& vv) {\n\t\ts << \"\\\\\\n\"; for (size_t i = 0; i < vv.size(); ++i){ s << vv[i] << \"\\n\"; } return s; }\n\tvoid debug_impl() { std::cerr << '\\n'; }\n\ttemplate<typename Head, typename... Tail> void debug_impl(Head head, Tail... tail) { std::cerr << \" \" << head << (sizeof...(tail) ? \",\" : \"\"); debug_impl(tail...); }\n\t#define debug(...) do { std::cerr << \":\" << __LINE__ << \" (\" << #__VA_ARGS__ << \") =\"; debug_impl(__VA_ARGS__); } while (false)\n\tconstexpr inline long long prodlocal([[maybe_unused]] long long prod, [[maybe_unused]] long long local) { return local; }\n#else\n\t#define debug(...) do {} while (false)\n\tconstexpr inline long long prodlocal([[maybe_unused]] long long prod, [[maybe_unused]] long long local) { return prod; }\n#endif\n//#define int long long\nusing ll = long long;\n//INT_MAX = (1<<31)-1 = 2147483647, INT64_MAX = (1LL<<63)-1 = 9223372036854775807\nconstexpr ll INF = numeric_limits<ll>::max() == INT_MAX ? (ll)1e9 + 7 : (ll)1e18;\nconstexpr ll MOD = (ll)1e9 + 7; //primitive root = 5\n//constexpr ll MOD = 998244353; //primitive root = 3\nconstexpr double EPS = 1e-9;\nconstexpr ll dx[4] = {1, 0, -1, 0};\nconstexpr ll dy[4] = {0, 1, 0, -1};\nconstexpr ll dx8[8] = {1, 0, -1, 0, 1, 1, -1, -1};\nconstexpr ll dy8[8] = {0, 1, 0, -1, 1, -1, 1, -1};\n#define rep(i, n) for(ll i=0, i##_length=(n); i< i##_length; ++i)\n#define repeq(i, n) for(ll i=1, i##_length=(n); i<=i##_length; ++i)\n#define rrep(i, n) for(ll i=(n)-1; i>=0; --i)\n#define rrepeq(i, n) for(ll i=(n) ; i>=1; --i)\n#define all(v) (v).begin(), (v).end()\n#define rall(v) (v).rbegin(), (v).rend()\nvoid p() { std::cout << '\\n'; }\ntemplate<typename Head, typename... Tail> void p(Head head, Tail... tail) { std::cout << head << (sizeof...(tail) ? \" \" : \"\"); p(tail...); }\ntemplate<typename T> inline void pv(std::vector<T>& v) { for(ll i=0, N=v.size(); i<N; i++) std::cout << v[i] << \" \\n\"[i==N-1]; }\ntemplate<typename T> inline bool chmax(T& a, T b) { return a < b && (a = b, true); }\ntemplate<typename T> inline bool chmin(T& a, T b) { return a > b && (a = b, true); }\ntemplate<typename T> inline void uniq(std::vector<T>& v) { v.erase(std::unique(v.begin(), v.end()), v.end()); }\ntemplate<typename T> inline ll sz(T& v) { return v.size(); }\n\n/*-----8<-----template-----8<-----*/\n\n//[lib]trie木.cpp\n/*\nあるノードの配下の全てのノードには自身に対応する文字列に共通するプレフィックスがあり, \nルートには空の文字列が対応している。\n\n計算量:\n add O(|S|)\n query O(|S|)\n\nexist は子供以下に追加された文字列の個数, \naccept はそのノードにマッチする全ての追加された文字列の番号が格納される。\n\nTrie< char_size, margin >(): \n 文字列の種類数 char_size, 開始文字が margin のトライ木を作成する。\nadd(S): 文字列 S をトライ木に追加する\nquery(S, f, sindex): 文字列 S の sindex 文字目から一致する文字列を検索する。\n 一致した文字列ごとに関数 f が呼び出される。\nsize(): ノード数を返す\ncount(): 存在する文字列の個数を返す\n*/\ntemplate< ll char_size >\nclass TrieNode {\npublic:\n\tll nxt[char_size];\n\n\tll exist;\n\tvector< ll > accept;\n\n\tTrieNode() : exist(0) {\n\t\tmemset(nxt, -1, sizeof(nxt));\n\t}\n};\ntemplate< ll char_size, ll margin >\nclass Trie {\npublic:\n\tusing Node = TrieNode< char_size >;\n\n\tvector< Node > nodes;\n\tll root;\n\n\tTrie() : root(0) {\n\t\tnodes.push_back(Node());\n\t}\n\n\tvoid update_direct(ll node, ll id) {\n\t\tnodes[node].accept.push_back(id);\n\t}\n\n\tvoid update_child(ll node) {\n\t\t++nodes[node].exist;\n\t}\n\n\tvoid add(const string &str, ll id) {\n\t\tll node_index = 0;\n\t\tfor(ll str_index = 0; str_index < (ll)str.size(); ++str_index){\n\t\t\tconst ll c = str[str_index] - margin;\n\t\t\tif(nodes[node_index].nxt[c] == -1) {\n\t\t\t\tnodes[node_index].nxt[c] = (ll) nodes.size();\n\t\t\t\tnodes.push_back(Node());\n\t\t\t}\n\t\t\tupdate_child(node_index);\n\t\t\tnode_index = nodes[node_index].nxt[c];\n\t\t}\n\t\tupdate_direct(node_index, id);\n\t}\n\n\tvoid add(const string &str) {\n\t\tadd(str, nodes[0].exist);\n\t}\n\n\ttemplate<typename F>\n\tvoid query(const string &str, const F &f, ll str_index = 0) {\n\t\tll node_index = 0;\n\t\tfor(; str_index <= (ll)str.size(); ++str_index){\n\t\t\tfor(auto&& idx : nodes[node_index].accept) f(idx);\n\t\t\tif(str_index == (ll)str.size())break;\n\t\t\tconst ll c = str[str_index] - margin;\n\t\t\tif(nodes[node_index].nxt[c] == -1) break;\n\t\t\tnode_index = nodes[node_index].nxt[c];\n\t\t}\n\t}\n\n\tll count() const {\n\t\treturn (nodes[0].exist);\n\t}\n\n\tll size() const {\n\t\treturn nodes.size();\n\t}\n};\n\n//[lib]Aho-Corasick.cpp\n//[depends on]trie木.cpp\n/*\nイメージ図は http://algoogle.hadrori.jp/algorithm/aho-corasick.html\n複数文字列に対するパターンマッチングオートマトンを構築する。\n計算量 build() O(∑|Si|) Si はAho-Corasickに追加した文字列, クエリ O(1)\nadd(S): 文字列 S をトライ木に追加する\nbuild(): 現在のTrieをもとにオートマトンを構築する\nmatch(T, now): 現状態が now で, 文字列 T が現れた時に, 新たに各パターン文字列にマッチした回数を返す\nmove(c, now): 現状態が now で, 文字 c/文字列 T が現れたときに, 新たにパターン文字列にマッチした個数と, 次状態をpairで返す\n*/\ntemplate< ll char_size, ll margin >\nstruct AhoCorasick : Trie< char_size + 1, margin > {\n\tusing Trie< char_size + 1, margin >::Trie;\n\n\tconst ll FAIL = char_size;\n\tvector< ll > correct;\n\n\tvoid build(bool heavy = true) {\n\t\tcorrect.resize(this->size());\n\t\tfor(ll i = 0; i < this->size(); i++) {\n\t\t\tcorrect[i] = (ll) this->nodes[i].accept.size();\n\t\t}\n\t\tqueue< ll > que;\n\t\tfor(ll i = 0; i <= char_size; i++) {\n\t\t\tif(~this->nodes[0].nxt[i]) {\n\t\t\t\tthis->nodes[this->nodes[0].nxt[i]].nxt[FAIL] = 0;\n\t\t\t\tque.emplace(this->nodes[0].nxt[i]);\n\t\t\t} else {\n\t\t\t\tthis->nodes[0].nxt[i] = 0;\n\t\t\t}\n\t\t}\n\t\twhile(!que.empty()) {\n\t\t\tauto &now = this->nodes[que.front()];\n\t\t\tll fail = now.nxt[FAIL];\n\t\t\tcorrect[que.front()] += correct[fail];\n\t\t\tque.pop();\n\t\t\tfor(ll i = 0; i < char_size; i++) {\n\t\t\t\tif(~now.nxt[i]) {\n\t\t\t\t\tthis->nodes[now.nxt[i]].nxt[FAIL] = this->nodes[fail].nxt[i];\n\t\t\t\t\tif(heavy) {\n\t\t\t\t\t\tauto &u = this->nodes[now.nxt[i]].accept;\n\t\t\t\t\t\tauto &v = this->nodes[this->nodes[fail].nxt[i]].accept;\n\t\t\t\t\t\tvector< ll > accept;\n\t\t\t\t\t\tset_union(begin(u), end(u), begin(v), end(v), back_inserter(accept));\n\t\t\t\t\t\tu = accept;\n\t\t\t\t\t}\n\t\t\t\t\tque.emplace(now.nxt[i]);\n\t\t\t\t} else {\n\t\t\t\t\tnow.nxt[i] = this->nodes[fail].nxt[i];\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tmap< ll, ll > match(const string &str, ll now = 0) {\n\t\tmap< ll, ll > result;\n\t\tfor(auto &c : str) {\n\t\t\tnow = this->nodes[now].nxt[c - margin];\n\t\t\tfor(auto &v : this->nodes[now].accept) result[v] += 1;\n\t\t}\n\t\treturn result;\n\t}\n\n\tpair< ll, ll > move(const char &c, ll now = 0) {\n\t\tnow = this->nodes[now].nxt[c - margin];\n\t\treturn {correct[now], now};\n\t}\n\n\tpair< ll, ll > move(const string &str, ll now = 0) {\n\t\tll sum = 0;\n\t\tfor(auto &c : str) {\n\t\t\tauto nxt = move(c, now);\n\t\t\tsum += nxt.first;\n\t\t\tnow = nxt.second;\n\t\t}\n\t\treturn {sum, now};\n\t}\n};\n\n/*-----8<-----library-----8<-----*/\n\nvoid solve() {\n\twhile (1) {\n\t\tll H, W;\n\t\tcin >> H >> W;\n\t\tif (H == 0 && W == 0) break;\n\t\tvector<string> S(H);\n\t\trep(i, H) cin >> S[i];\n\t\tll N;\n\t\tcin >> N;\n\t\tAhoCorasick<26,'A'> aho;\n\t\trep(i,N){\n\t\t\tstring a;\n\t\t\tcin >> a;\n\t\t\taho.add(a);\n\t\t}\n\t\taho.build();\n\n\t\tvector<char> v{'D','R','U','L'};\n\t\t\n\t\tll start = -1, goal = -1;\n\t\trep(i, H) rep(j, W) {\n\t\t\tif(S[i][j]=='S'){\n\t\t\t\tstart = i * W + j;\n\t\t\t}\n\t\t\tif(S[i][j]=='G'){\n\t\t\t\tgoal = i * W + j;\n\t\t\t}\n\t\t}\n\n\t\tqueue<array<ll,3>> que;\n\t\tvector<map<ll, ll>> seen(H * W);\n\t\tarray<ll,3> startpos = {start/W, start%W};\n\t\tque.push(startpos);\n\t\tseen[start][0] = 0;\n\n\t\twhile(!que.empty()) {\n\t\t\tauto [x, y, pos] = que.front();\n\t\t\tque.pop();\n\t\t\trep(k, 4) {\n\t\t\t\tll nx = dx[k] + x;\n\t\t\t\tll ny = dy[k] + y;\n\t\t\t\tif(nx <= -1 || ny <= -1 || nx >= H || ny >= W) continue;\n\t\t\t\tif(S[nx][ny] == '#') continue;\n\t\t\t\t\n\t\t\t\t//seen[nx][ny]の書き換えはここでやること\n\t\t\t\tauto [count, nextpos] = aho.move(v[k], pos);\n\t\t\t\tif (count) continue;\n\t\t\t\tll tmp = INF;\n\t\t\t\tauto it = seen[nx * W + ny].find(nextpos);\n\t\t\t\tif (it != seen[nx * W + ny].end()){\n\t\t\t\t\ttmp = seen[nx * W + ny][nextpos];\n\t\t\t\t}\n\t\t\t\tif (tmp > seen[x * W + y][pos] + 1) {\n\t\t\t\t\tseen[nx * W + ny][nextpos] = seen[x * W + y][pos] + 1;\n\t\t\t\t\tque.push({nx, ny, nextpos});\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tll ans = INF;\n\t\tfor (auto&& t : seen[goal]) {\n\t\t\tchmin(ans, t.second);\n\t\t}\n\t\tif (ans == INF) ans = -1;\n\t\tp(ans);\n\t}\n}\n\nsigned main() {\n\tstd::cin.tie(nullptr);\n\tstd::ios::sync_with_stdio(false);\n\t//ll Q; cin >> Q; while(Q--)solve();\n\tsolve();\n\treturn 0;\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 9476, "score_of_the_acc": -1.0953, "final_rank": 12 }, { "submission_id": "aoj_2212_4945950", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define REP(i, n) for (int i=0; i<(n); ++i)\n#define RREP(i, n) for (int i=(int)(n)-1; i>=0; --i)\n#define FOR(i, a, n) for (int i=(a); i<(n); ++i)\n#define RFOR(i, a, n) for (int i=(int)(n)-1; i>=(a); --i)\n\n#define SZ(x) ((int)(x).size())\n#define ALL(x) (x).begin(),(x).end()\n\n#define DUMP(x) cerr<<#x<<\" = \"<<(x)<<endl\n#define DEBUG(x) cerr<<#x<<\" = \"<<(x)<<\" (L\"<<__LINE__<<\")\"<<endl;\n\ntemplate<class T>\nostream &operator<<(ostream &os, const vector <T> &v) {\n os << \"[\";\n REP(i, SZ(v)) {\n if (i) os << \", \";\n os << v[i];\n }\n return os << \"]\";\n}\n\ntemplate<class T, class U>\nostream &operator<<(ostream &os, const pair <T, U> &p) {\n return os << \"(\" << p.first << \" \" << p.second << \")\";\n}\n\ntemplate<class T>\nbool chmax(T &a, const T &b) {\n if (a < b) {\n a = b;\n return true;\n }\n return false;\n}\n\ntemplate<class T>\nbool chmin(T &a, const T &b) {\n if (b < a) {\n a = b;\n return true;\n }\n return false;\n}\n\nusing ll = long long;\nusing ull = unsigned long long;\nusing ld = long double;\nusing P = pair<int, int>;\nusing vi = vector<int>;\nusing vll = vector<ll>;\nusing vvi = vector<vi>;\nusing vvll = vector<vll>;\n\nconst ll MOD = 1e9 + 7;\nconst int INF = INT_MAX / 2;\nconst ll LINF = LLONG_MAX / 2;\nconst ld eps = 1e-9;\n\n\ntemplate<int m>\nstruct mint {\n int x;\n\n mint(int x = 0) : x(((x % m) + m) % m) {}\n\n mint operator-() { return x ? m - x : 0; }\n\n mint &operator+=(mint r) {\n if ((x += r.x) >= m) x -= m;\n return *this;\n }\n\n mint &operator-=(mint r) {\n if ((x -= r.x) < 0) x += m;\n return *this;\n }\n\n mint &operator*=(mint r) {\n x = ((ll) x * r.x) % m;\n return *this;\n }\n\n mint inv() {\n int a = x, b = m, u = 1, v = 0, t;\n while (b > 0) {\n t = a / b;\n swap(a -= t * b, b);\n swap(u -= t * v, v);\n }\n return u;\n }\n\n mint &operator/=(mint r) { return *this *= r.inv(); }\n\n friend mint operator+(mint l, mint r) { return l += r; }\n\n friend mint operator-(mint l, mint r) { return l -= r; }\n\n friend mint operator*(mint l, mint r) { return l *= r; }\n\n friend mint operator/(mint l, mint r) { return l /= r; }\n\n mint pow(ll n) {\n mint ret = 1, tmp = *this;\n while (n) {\n if (n & 1) ret *= tmp;\n tmp *= tmp, n >>= 1;\n }\n return ret;\n }\n\n friend bool operator==(mint l, mint r) { return l.x == r.x; }\n\n friend bool operator!=(mint l, mint r) { return l.x != r.x; }\n\n friend ostream &operator<<(ostream &os, mint a) {\n return os << a.x;\n }\n\n friend istream &operator>>(istream &is, mint &a) {\n int x;\n is >> x;\n a = x;\n return is;\n }\n};\n\n\ntemplate<typename T>\nstruct Combination {\n int _n = 1;\n vector<T> _fact{1}, _rfact{1};\n\n void extend(int n) {\n if (n <= _n) return;\n _fact.resize(n);\n _rfact.resize(n);\n for (int i = _n; i < n; ++i) _fact[i] = _fact[i - 1] * i;\n _rfact[n - 1] = 1 / _fact[n - 1];\n for (int i = n - 1; i > _n; --i) _rfact[i - 1] = _rfact[i] * i;\n _n = n;\n }\n\n T fact(int k) {\n extend(k + 1);\n return _fact.at(k);\n }\n\n T rfact(int k) {\n extend(k + 1);\n return _rfact.at(k);\n }\n\n T P(int n, int r) {\n if (r < 0 or n < r) return 0;\n return fact(n) * rfact(n - r);\n }\n\n T C(int n, int r) {\n if (r < 0 or n < r) return 0;\n return fact(n) * rfact(r) * rfact(n - r);\n }\n\n T H(int n, int r) {\n return (n == 0 and r == 0) ? 1 : C(n + r - 1, r);\n }\n};\n\n/**\n * Trie\n * @tparam char_size 文字の種類数. ASCIIで連続していること.\n * @tparam margin 0番目の文字 (e.g. 'a')\n * @usage\n * AhoCorasick PMA(26,'a');\n * PMA.add(\"pattern\");\n * int now = root;\n * string s;\n * for (char c : s) {\n * while (PMA.nodes[now].nxt[c - margin] == -1) {\n * now = PMA.nodes[now].nxt[FAIL];\n * }\n * now = PMA.nodes[now].nxt[c - margin];\n * // nowはsをcまで読んだときのノード.\n * // ここでPMA.nodes[now].acceptとかを利用していろいろやる\n * }\n */\ntemplate<int char_size, int margin>\nstruct AhoCorasick {\n const int root = 0, FAIL = char_size;\n /**\n * nxt[FAIL]:\n * 次の文字がcのときに nxt[c - margin] == -1 であった場合,\n * nodes[nxt[FAIL]][c - margin]に進む\n */\n struct Node {\n vector<int> nxt, accept;\n Node() : nxt(char_size + 1, -1) {}\n };\n vector<Node> nodes = {Node()};\n\n /**\n * @brief パターンの追加 O(|s|)\n * @param s 追加するパターン\n * @param id パターンのid. sの終端に対応するノードのacceptに追加される.\n */\n void add(const string& s, int id = 0) {\n int now = root;\n for (char c : s) {\n if (nodes[now].nxt[c - margin] == -1) {\n nodes[now].nxt[c - margin] = nodes.size();\n nodes.push_back(Node());\n }\n now = nodes[now].nxt[c - margin];\n }\n nodes[now].accept.push_back(id);\n }\n\n /**\n * @brief PMAの構築.\n * 構築後, 文字列sの終点に対応するNodeには\n * sのsuffixにマッチするパターンのidが格納される\n */\n void build() {\n queue<int> que;\n for (int i = 0; i < char_size; ++i) {\n if (nodes[root].nxt[i] == -1) {\n nodes[root].nxt[i] = root;\n } else {\n que.push(nodes[root].nxt[i]);\n nodes[nodes[root].nxt[i]].nxt[FAIL] = root;\n }\n }\n while (!que.empty()) {\n int now = que.front(); que.pop();\n for (int i = 0; i < char_size; ++i) {\n if (nodes[now].nxt[i] == -1) continue;\n int fail = nodes[now].nxt[FAIL];\n while (nodes[fail].nxt[i] == -1) {\n fail = nodes[fail].nxt[FAIL];\n }\n nodes[nodes[now].nxt[i]].nxt[FAIL] = nodes[fail].nxt[i];\n auto &u = nodes[nodes[now].nxt[i]].accept;\n auto &v = nodes[nodes[fail].nxt[i]].accept;\n vector<int> accept;\n set_union(u.begin(), u.end(), v.begin(), v.end(),\n back_inserter(accept));\n u = accept;\n que.push(nodes[now].nxt[i]);\n }\n }\n }\n\n friend ostream& operator<<(ostream& os, const AhoCorasick& pma) {\n const int n = pma.nodes.size();\n vector<bool> used(n);\n function<void(int)> dfs = [&](int now) {\n used[now] = true;\n os << \"{\" << now << \" \" << \"#:\" << pma[now].nxt[pma.FAIL];\n for (int i = 0; i < char_size; ++i) {\n int nxt = pma[now].nxt[i];\n if (nxt != -1) {\n os << \", \" << (char)(i + margin) << \":\";\n if (!used[nxt]) dfs(nxt);\n else os << nxt;\n }\n }\n os << \"}\";\n };\n dfs(pma.root);\n return os;\n }\n};\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(10);\n\n map<char,int> dy, dx;\n dy['U'] = -1, dy['R'] = 0, dy['D'] = 1, dy['L'] = 0;\n dx['U'] = 0, dx['R'] = 1, dx['D'] = 0, dx['L'] = -1;\n\n while (true) {\n int n, m; cin >> n >> m;\n if (n == 0) break;\n vector<string> board(n);\n int si, sj;\n for (int i = 0; i < n; ++i) {\n cin >> board[i];\n for (int j = 0; j < m; ++j) {\n if (board[i][j] == 'S') {\n si = i, sj = j;\n }\n }\n }\n\n AhoCorasick<26,'A'> pma;\n int P; cin >> P;\n for (int i = 0; i < P; ++i) {\n string p; cin >> p;\n pma.add(p, i);\n }\n pma.build();\n\n queue<tuple<int,int,int,int>> que;\n que.emplace(si, sj, 0, pma.root);\n map<tuple<int,int,int>,bool> used;\n used[make_tuple(si, sj, pma.root)] = true;\n\n int ans = -1;\n\n while (!que.empty()) {\n int i, j, d, now;\n tie(i, j, d, now) = que.front(); que.pop();\n if (board[i][j] == 'G') {\n ans = d;\n break;\n }\n for (char dir : { 'U', 'R', 'D', 'L' }) {\n int tmp = now;\n int y = i + dy[dir], x = j + dx[dir];\n if (y < 0 or n <= y or x < 0 or m <= x or\n board[y][x] == '#') continue;\n\n while (pma.nodes[tmp].nxt[dir - 'A'] == -1) {\n tmp = pma.nodes[tmp].nxt[pma.FAIL];\n }\n tmp = pma.nodes[tmp].nxt[dir - 'A'];\n if (!pma.nodes[tmp].accept.empty()) {\n continue;\n }\n if (!used.count(make_tuple(y, x, tmp))) {\n used[make_tuple(y, x, tmp)] = true;\n que.emplace(y, x, d + 1, tmp);\n }\n }\n }\n\n cout << ans << endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 180, "memory_kb": 9172, "score_of_the_acc": -1.333, "final_rank": 14 }, { "submission_id": "aoj_2212_4908215", "code_snippet": "#define MOD_TYPE 1\n\n#pragma region Macros\n\n#include <bits/stdc++.h>\nusing namespace std;\n\n#if 0\n#include <boost/multiprecision/cpp_int.hpp>\n#include <boost/multiprecision/cpp_dec_float.hpp>\nusing Int = boost::multiprecision::cpp_int;\nusing lld = boost::multiprecision::cpp_dec_float_100;\n#endif\n#if 1\n#pragma GCC target(\"avx2\")\n#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"unroll-loops\")\n#endif\nusing ll = long long int;\nusing ld = long double;\nusing pii = pair<int, int>;\nusing pll = pair<ll, ll>;\nusing pld = pair<ld, ld>;\ntemplate <typename Q_type>\nusing smaller_queue = priority_queue<Q_type, vector<Q_type>, greater<Q_type>>;\n\nconstexpr ll MOD = (MOD_TYPE == 1 ? (ll)(1e9 + 7) : 998244353);\n//constexpr ll MOD = 1;\nconstexpr int INF = (int)1e9 + 10;\nconstexpr ll LINF = (ll)4e18;\nconstexpr double PI = acos(-1.0);\nconstexpr double EPS = 1e-9;\nconstexpr int Dx[] = {0, 0, -1, 1, -1, 1, -1, 1, 0};\nconstexpr int Dy[] = {1, -1, 0, 0, -1, -1, 1, 1, 0};\n\n#define REP(i, m, n) for (ll i = m; i < (ll)(n); ++i)\n#define rep(i, n) REP(i, 0, n)\n#define REPI(i, m, n) for (int i = m; i < (int)(n); ++i)\n#define repi(i, n) REPI(i, 0, n)\n#define MP make_pair\n#define MT make_tuple\n#define YES(n) cout << ((n) ? \"YES\" : \"NO\") << \"\\n\"\n#define Yes(n) cout << ((n) ? \"Yes\" : \"No\") << \"\\n\"\n#define possible(n) cout << ((n) ? \"possible\" : \"impossible\") << \"\\n\"\n#define Possible(n) cout << ((n) ? \"Possible\" : \"Impossible\") << \"\\n\"\n#define Yay(n) cout << ((n) ? \"Yay!\" : \":(\") << \"\\n\"\n#define all(v) v.begin(), v.end()\n#define NP(v) next_permutation(all(v))\n#define dbg(x) cerr << #x << \":\" << x << \"\\n\";\n\nstruct io_init\n{\n io_init()\n {\n cin.tie(0);\n ios::sync_with_stdio(false);\n cout << setprecision(30) << setiosflags(ios::fixed);\n };\n} io_init;\ntemplate <typename T>\ninline bool chmin(T &a, T b)\n{\n if (a > b)\n {\n a = b;\n return true;\n }\n return false;\n}\ntemplate <typename T>\ninline bool chmax(T &a, T b)\n{\n if (a < b)\n {\n a = b;\n return true;\n }\n return false;\n}\ninline ll CEIL(ll a, ll b)\n{\n return (a + b - 1) / b;\n}\ntemplate <typename A, size_t N, typename T>\ninline void Fill(A (&array)[N], const T &val)\n{\n fill((T *)array, (T *)(array + N), val);\n}\ntemplate <typename T, typename U>\nconstexpr istream &operator>>(istream &is, pair<T, U> &p) noexcept\n{\n is >> p.first >> p.second;\n return is;\n}\ntemplate <typename T, typename U>\nconstexpr ostream &operator<<(ostream &os, pair<T, U> &p) noexcept\n{\n os << p.first << \" \" << p.second;\n return os;\n}\n#pragma endregion\n\ntemplate <char MIN_CHAR = 'a', int ALPHABET = 26>\nstruct AhoCorasick\n{\n struct node\n {\n // suff : 先頭の文字を最小限消してグラフに存在する頂点にするときの行先の頂点\n // dict : 先頭の文字を最小限消して辞書に存在する単語にするときの行先の頂点\n // depth : Trie木における深さ(省略可能)\n // word_index : このノードで終わる根からの単語のindex(祖先は含まない。なければ-1)\n // word_count : このノードで終わる単語の総数 count_total_matches()で使用\n // link : Trie及びsuffixの辺の接続先頂点(なければ-1)\n int suff = -1, dict = -1, depth = 0;\n int word_index = -1, word_count = 0;\n int link[ALPHABET];\n node() { fill(link, link + ALPHABET, -1); }\n int &operator[](char c) { return link[c - MIN_CHAR]; }\n };\n\n // nodes : 頂点集合\n // W : 現在の単語数\n // word_location : 各単語の(単語リストの)index\n // word_indices_by_depth : 単語のindexをdepthの降順に並べたもの\n // defer : 各単語のTrie木の最後の頂点のindex\n vector<node> nodes;\n int W;\n vector<int> word_location;\n vector<int> word_indices_by_depth;\n vector<int> defer;\n\n AhoCorasick(const vector<string> &words = {})\n {\n build(words);\n }\n\n // suffixを親とする木の隣接リスト これの上でDPやクエリ処理を行うことが多い\n vector<vector<int>> build_suffix_adj() const\n {\n vector<vector<int>> adj(nodes.size());\n for (int i = 1; i < int(nodes.size()); i++)\n adj[nodes[i].suff].push_back(i);\n return adj;\n }\n\n int get_or_add_child(int current, char c)\n {\n if (nodes[current][c] >= 0)\n return nodes[current][c];\n int index = int(nodes.size());\n nodes[current][c] = index;\n nodes.emplace_back();\n nodes.back().depth = nodes[current].depth + 1;\n return index;\n }\n\n int add_word(const string &word, int word_index)\n {\n assert(!nodes.empty());\n int current = 0;\n for (char c : word)\n current = get_or_add_child(current, c);\n if (nodes[current].word_index < 0)\n nodes[current].word_index = word_index;\n nodes[current].word_count++;\n return current;\n }\n\n // locationからcを追加したときの行き先 O(1)\n int get_suffix_link(int location, char c) const\n {\n if (location >= 0)\n location = nodes[location].link[c - MIN_CHAR];\n return max(location, 0);\n }\n\n void build(const vector<string> &words)\n {\n nodes = {node()};\n W = int(words.size());\n word_location.resize(W);\n defer.resize(W);\n int max_depth = 0;\n\n for (int i = 0; i < W; i++)\n {\n word_location[i] = add_word(words[i], i);\n max_depth = max(max_depth, int(words[i].size()));\n defer[i] = nodes[word_location[i]].word_index;\n }\n\n // depthの降順に単語indexのリストを作成\n word_indices_by_depth.resize(W);\n vector<int> depth_freq(max_depth + 1, 0);\n\n for (int i = 0; i < W; i++)\n depth_freq[words[i].size()]++;\n\n for (int i = max_depth - 1; i >= 0; i--)\n depth_freq[i] += depth_freq[i + 1];\n\n for (int i = 0; i < W; i++)\n word_indices_by_depth[--depth_freq[words[i].size()]] = i;\n\n // depth順のBFSでsuffix parentを求める\n vector<int> q = {0};\n\n for (int i = 0; i < int(q.size()); i++)\n {\n int current = q[i];\n\n for (char c = MIN_CHAR; c < MIN_CHAR + ALPHABET; c++)\n {\n int &index = nodes[current][c];\n if (index >= 0)\n {\n // currentのsuffix parentで子cを持つものが見つかるまで走査して\n // indexのsuffix parentを見つける\n int suffix_parent = get_suffix_link(nodes[current].suff, c);\n nodes[index].suff = suffix_parent;\n nodes[index].word_count += nodes[suffix_parent].word_count;\n nodes[index].dict = nodes[suffix_parent].word_index < 0 ? nodes[suffix_parent].dict : suffix_parent;\n q.push_back(index);\n }\n else\n {\n index = get_suffix_link(nodes[current].suff, c);\n }\n }\n }\n }\n\n // 辞書内のそれぞれの単語がtextに何個含まれているか O(text length + num words)\n vector<int> count_matches(const string &text) const\n {\n vector<int> matches(W, 0);\n int current = 0;\n\n for (char c : text)\n {\n current = get_suffix_link(current, c);\n int dict_node = nodes[current].word_index < 0 ? nodes[current].dict : current;\n\n if (dict_node >= 0)\n matches[nodes[dict_node].word_index]++;\n }\n\n // depthの降順に見る\n for (int word_index : word_indices_by_depth)\n {\n int location = word_location[word_index];\n int dict_node = nodes[location].dict;\n\n if (dict_node >= 0)\n matches[nodes[dict_node].word_index] += matches[word_index];\n }\n\n for (int i = 0; i < W; i++)\n matches[i] = matches[defer[i]];\n\n return matches;\n }\n\n // textに含まれる辞書内の単語で、textのi文字目で終わるものの個数 O(text length)\n vector<int> count_matches_by_position(const string &text) const\n {\n vector<int> matches(text.size());\n int current = 0;\n\n for (int i = 0; i < int(text.size()); i++)\n {\n current = get_suffix_link(current, text[i]);\n matches[i] = nodes[current].word_count;\n }\n\n return matches;\n }\n\n // textに辞書内の単語が合計何個含まれているか O(text length)\n int64_t count_total_matches(const string &text) const\n {\n int64_t matches = 0;\n int current = 0;\n\n for (char c : text)\n {\n current = get_suffix_link(current, c);\n matches += nodes[current].word_count;\n }\n\n return matches;\n }\n};\n\nmap<char, char> tran;\n\nint dp[50][50][110];\n\nvoid solve()\n{\n int n, m;\n cin >> n >> m;\n if (n == 0 and m == 0)\n exit(0);\n vector<string> s(n);\n rep(i, n) cin >> s[i];\n\n int p;\n cin >> p;\n vector<string> ban(p);\n rep(i, p)\n {\n cin >> ban[i];\n for (auto &&c : ban[i])\n c = tran[c];\n }\n\n AhoCorasick<'0', 4> aho(ban);\n int si, sj, gi, gj;\n rep(i, n) rep(j, m)\n {\n if (s[i][j] == 'S')\n si = i, sj = j;\n if (s[i][j] == 'G')\n gi = i, gj = j;\n }\n queue<tuple<int, int, int>> que;\n que.push({si, sj, 0});\n rep(i, n) rep(j, m) rep(state, aho.nodes.size()) dp[i][j][state] = -1;\n dp[si][sj][0] = 0;\n\n while (!que.empty())\n {\n auto [i, j, state] = que.front();\n que.pop();\n\n // 左\n if (j > 0)\n {\n int next_state = aho.get_suffix_link(state, '0');\n if (!aho.nodes[next_state].word_count and dp[i][j - 1][next_state] == -1 and s[i][j - 1] != '#')\n {\n dp[i][j - 1][next_state] = dp[i][j][state] + 1;\n que.push({i, j - 1, next_state});\n }\n }\n\n // 右\n if (j < m - 1)\n {\n int next_state = aho.get_suffix_link(state, '1');\n if (!aho.nodes[next_state].word_count and dp[i][j + 1][next_state] == -1 and s[i][j + 1] != '#')\n {\n\n dp[i][j + 1][next_state] = dp[i][j][state] + 1;\n que.push({i, j + 1, next_state});\n }\n }\n\n // 上\n if (i > 0)\n {\n int next_state = aho.get_suffix_link(state, '2');\n if (!aho.nodes[next_state].word_count and dp[i - 1][j][next_state] == -1 and s[i - 1][j] != '#')\n {\n dp[i - 1][j][next_state] = dp[i][j][state] + 1;\n que.push({i - 1, j, next_state});\n }\n }\n\n // 下\n if (i < n - 1)\n {\n int next_state = aho.get_suffix_link(state, '3');\n if (!aho.nodes[next_state].word_count and dp[i + 1][j][next_state] == -1 and s[i + 1][j] != '#')\n {\n dp[i + 1][j][next_state] = dp[i][j][state] + 1;\n que.push({i + 1, j, next_state});\n }\n }\n }\n\n int Min = INF;\n rep(state, aho.nodes.size())\n {\n if (~dp[gi][gj][state])\n chmin(Min, dp[gi][gj][state]);\n }\n cout << (Min == INF ? -1 : Min) << \"\\n\";\n}\n\nint main()\n{\n tran['L'] = '0';\n tran['R'] = '1';\n tran['U'] = '2';\n tran['D'] = '3';\n while (1)\n {\n solve();\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 4268, "score_of_the_acc": -0.0738, "final_rank": 4 }, { "submission_id": "aoj_2212_4852321", "code_snippet": "#include <algorithm>\n#include <cmath>\n#include <cstdio>\n#include <cstring>\n#include <queue>\nusing namespace std;\n#define inf 0x3f3f3f3f\n#define maxn 55\n#define maxp 105\nstruct node\n{\n int x, y, s, d;\n node() {}\n node(int x, int y, int s, int d) : x(x), y(y), s(s), d(d) {}\n};\nint dx[4] = {-1, 1, 0, 0}, dy[4] = {0, 0, 1, -1};\nint N, M, P, sx, sy, tx, ty, ns, id['Z' + 1];\nint trie[maxp][4], fail[maxp], flag[maxp], used[maxn][maxn][maxp];\nchar mat[maxn][maxn];\n\nvoid insert(char *s)\n{\n int len = strlen(s), p = 0;\n for (int i = 0; i < len; i++)\n {\n int c = id[s[i]];\n if (!trie[p][c])\n {\n memset(trie[ns], 0, sizeof(trie[ns]));\n trie[p][c] = ns++;\n }\n p = trie[p][c];\n }\n flag[p] = 1;\n}\n\nvoid getFail()\n{\n queue<int> q;\n for (int i = 0; i < 4; i++)\n {\n if (trie[0][i])\n {\n fail[trie[0][i]] = 0;\n q.push(trie[0][i]);\n }\n }\n while (!q.empty())\n {\n int p = q.front();\n q.pop();\n if (flag[fail[p]])\n {\n flag[p] = 1;\n }\n for (int i = 0; i < 4; i++)\n {\n if (trie[p][i])\n {\n fail[trie[p][i]] = trie[fail[p]][i];\n q.push(trie[p][i]);\n }\n else\n {\n trie[p][i] = trie[fail[p]][i];\n }\n }\n }\n}\n\nint main()\n{\n id['U'] = 0, id['D'] = 1, id['R'] = 2, id['L'] = 3;\n while (~scanf(\"%d%d\", &N, &M) && (N | M))\n {\n for (int i = 0; i < N; i++)\n {\n scanf(\" %s\", mat[i]);\n for (int j = 0; j < M; j++)\n {\n if (mat[i][j] == 'S')\n {\n sx = i, sy = j;\n }\n if (mat[i][j] == 'G')\n {\n tx = i, ty = j;\n }\n }\n }\n scanf(\"%d\", &P);\n ns = 1;\n memset(flag, 0, sizeof(flag));\n memset(trie[0], 0, sizeof(trie[0]));\n for (int i = 0; i < P; i++)\n {\n char s[15];\n scanf(\" %s\", s);\n insert(s);\n }\n getFail();\n int res = inf;\n queue<node> q;\n q.push(node(sx, sy, 0, 0));\n memset(used, 0, sizeof(used));\n while (!q.empty())\n {\n node p = q.front();\n q.pop();\n int x = p.x, y = p.y, s = p.s, d = p.d;\n if (x == tx && y == ty)\n {\n res = d;\n break;\n }\n if (used[x][y][s])\n {\n continue;\n }\n used[x][y][s] = 1;\n for (int i = 0; i < 4; i++)\n {\n if (flag[trie[s][i]])\n {\n continue;\n }\n int nx = x + dx[i], ny = y + dy[i];\n if (nx >= 0 && nx < N && ny >= 0 && ny < M && mat[nx][ny] != '#')\n {\n q.push(node(nx, ny, trie[s][i], d + 1));\n }\n }\n }\n printf(\"%d\\n\", res == inf ? -1 : res);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 4132, "score_of_the_acc": -0.0524, "final_rank": 2 }, { "submission_id": "aoj_2212_4648720", "code_snippet": "#include<cstdio>\n#include<cstring>\n#include<algorithm>\n#include<string>\n#include<vector>\n#include<queue>\nusing namespace std;\ntypedef pair<int, int> P;\nint n, m, p, K, ans;\nconst char UDLR[4] = {'U', 'D', 'L', 'R'};\nconst int dx[4] = {-1, 1, 0, 0}, dy[4] = {0, 0, -1, 1};\nchar map[52][52], tmp[15];\nstring patterns[15];\nvector<string> pfx;\nint next_state[150][4], map_state[52][52][150]; \nbool ng[150];\nqueue< P > que;\n\n\nbool check(int x, int y){\n return x>=0&&x<n&&y>=0&&y<m&&map[x][y]!='#';\n}\n\n\nvoid bfs(){\n memset(map_state, 255, sizeof(map_state));\n int sx, sy, ex, ey;\n for(int i=0; i<n; i++)\n for(int j=0; j<m; j++)\n switch (map[i][j]){\n case 'S':\n sx = i;\n sy = j;\n break;\n case 'G':\n ex = i;\n ey = j;\n break; \n default:\n break;\n }\n\n while(!que.empty()) que.pop();\n map_state[sx][sy][0] = 0;\n que.push(P(sx*m+sy, 0));\n\n bool reach=false;\n int tx, ty, tk, xx, yy, kk;\n ans = -1;\n while(!que.empty()){\n tx = que.front().first / m; ty = que.front().first % m; tk = que.front().second; \n que.pop();\n for(int i=0; i<4; i++){\n kk = next_state[tk][i];\n if(!ng[kk] && check(tx+dx[i], ty+dy[i])){\n // state kk is not forbidden\n xx = tx + dx[i];\n yy = ty + dy[i];\n if(map_state[xx][yy][kk] == -1){\n map_state[xx][yy][kk] = map_state[tx][ty][tk] + 1;\n que.push(P(xx*m+yy, kk));\n if(xx==ex && yy==ey){\n reach = true;\n ans = map_state[xx][yy][kk];\n break;\n }\n }\n } \n }\n if(reach) break;\n }\n printf(\"%d\\n\", ans);\n}\n\n\nint main(){\n //freopen(\"in.txt\", \"r\", stdin);\n int k;\n while(scanf(\"%d %d\", &n, &m), n!=0 && m!=0){\n for(int i=0; i<n; i++)\n scanf(\"%s\", map[i]);\n scanf(\"%d\", &p);\n pfx.clear();\n for(int i=0; i<p; i++){\n scanf(\"%s\", tmp);\n patterns[i].assign(tmp);\n for(int j=0; j<=patterns[i].length(); j++)\n pfx.push_back(patterns[i].substr(0, j));\n }\n pfx.push_back(string(\"\"));\n sort(pfx.begin(), pfx.end());\n pfx.erase(unique(pfx.begin(), pfx.end()), pfx.end());\n K = pfx.size();\n\n for(int i=0; i<K; i++){\n ng[i] = false;\n for(int j=0; j<p; j++){\n ng[i] |= patterns[j].length()<=pfx[i].length() \n && pfx[i].substr(pfx[i].length()-patterns[j].length(), patterns[j].length()) == patterns[j];\n }\n for(int j=0; j<4; j++){\n string s = pfx[i] + UDLR[j];\n for(;;){\n k = lower_bound(pfx.begin(), pfx.end(), s) - pfx.begin();\n if(k<K && pfx[k]==s) break;\n s = s.substr(1);\n }\n next_state[i][j] = k;\n }\n }\n\n bfs();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 4448, "score_of_the_acc": -0.1022, "final_rank": 6 }, { "submission_id": "aoj_2212_4369228", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\ntemplate<class T>\nbool chmax(T& a, const T& b) {\n if (a < b) { a = b; return true; }\n return false;\n}\ntemplate<class T>\nbool chmin(T& a, const T& b) {\n if (b < a) { a = b; return true; }\n return false;\n}\n\n// std::vector Declaration\ntemplate<typename T>\nvector<T> make_v(size_t a) { return vector<T>(a); }\ntemplate<typename T, typename... Ts>\nauto make_v(size_t a, Ts... ts) {\n return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));\n}\n\n// std::vector Declaration and Initialization\ntemplate<typename T>\nvector<T> make_vector(size_t a, T x) { return vector<T>(a, x); }\ntemplate<typename T, typename U, typename... Ts>\nauto make_vector(size_t a, U b, Ts... ts) {\n return vector<decltype(make_vector<T>(b,ts...))>(a, make_vector<T>(b, ts...));\n}\n\n// std::vector Input\ntemplate<typename T>\nistream& operator>>(istream& is, vector<T>& v) {\n for (auto &e : v) is >> e;\n return is;\n}\n\n// std::vector Debug\ntemplate<typename T>\nostream& operator<<(ostream& os, const vector<T>& v) {\n os << \"[\";\n bool a = 1;\n for (auto e : v) {\n os << (a ? \"\" : \" \");\n os << e;\n a = 0;\n }\n os << \"]\";\n return os;\n}\n\n// std::array Debug\ntemplate<typename T, size_t n>\nostream& operator<<(ostream& os, const array<T, n>& v) {\n os << \"[\";\n bool a = 1;\n for (auto e : v) {\n os << (a ? \"\" : \" \");\n os << e;\n a = 0;\n }\n os << \"]\";\n return os;\n}\n\n// std::deque Debug\ntemplate<typename T>\nostream& operator<<(ostream& os, const deque<T>& d) {\n os << \"[\";\n bool a = 1;\n for (auto e : d) {\n os << (a ? \"\" : \" \");\n os << e;\n a = 0;\n }\n os << \"]\";\n return os;\n}\n\n// std::pair Debug\ntemplate<typename T, typename U>\nostream& operator<<(ostream& os, const pair<T, U>& p) {\n os << \"(\" << p.first << \" \" << p.second << \")\";\n return os;\n}\n\n// std::set Debug\ntemplate<typename T>\nostream& operator<<(ostream& os, const set<T>& st) {\n os << \"{\";\n bool a = 1;\n for (auto e : st) {\n os << (a ? \"\" : \" \");\n os << e;\n a = 0;\n }\n os << \"}\";\n return os;\n}\n\n// std::multiset Debug\ntemplate<typename T>\nostream& operator<<(ostream& os, const multiset<T>& st) {\n os << \"{\";\n bool a = 1;\n for (auto e : st) {\n os << (a ? \"\" : \" \");\n os << e;\n a = 0;\n }\n os << \"}\";\n return os;\n}\n\n// std::map Debug\ntemplate<typename T, typename U>\nostream& operator<<(ostream& os, const map<T, U>& mp) {\n os << \"{\";\n bool a = 1;\n for (auto e : mp) {\n os << (a ? \"\" : \" \");\n os << e.first << \":\" << e.second;\n a = 0;\n }\n os << \"}\";\n return os;\n}\n\n// std::tuple Debug\ntemplate<int N, class Tuple>\nvoid out(ostream& os, const Tuple& t){}\ntemplate<int N, class Tuple, class H, class ...Ts>\nvoid out(ostream& os, const Tuple& t) {\n if (N) os << \" \";\n os << get<N>(t);\n out<N+1,Tuple,Ts...>(os, t);\n}\ntemplate<class ...Ts>\nostream& operator<<(ostream& os, const tuple<Ts...>& t) {\n os << \"(\";\n out<0,tuple<Ts...>,Ts...>(os, t);\n os << \")\";\n return os;\n}\n\n// Debug\n#define DUMP(x) cerr<<#x<<\" = \"<<(x)<<endl\n\n// Weighted edge\ntemplate<typename T>\nstruct edge {\n int src, to;\n T cost;\n\n edge() {}\n edge(int to, T cost) : src(-1), to(to), cost(cost) {}\n edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}\n\n friend ostream& operator<<(ostream& os, const edge& e) {\n return os << \"(\" << e.src << \"->\" << e.to << \":\" << e.cost << \")\";\n }\n};\n\nusing LL = int64_t;\n\n#define fs first\n#define sc second\n\nconst int64_t MOD = 1e9+7;\n\ntemplate<int char_size, int margin>\nstruct Trie {\n struct Node {\n vector<Node*> next;\n vector<int> accept;\n int depth;\n Node(int depth) : next(char_size, nullptr), depth(depth) {}\n friend ostream& operator<<(ostream& os, const Node* t) {\n os << \"{\";\n bool a = 0;\n for (int i = 0; i < char_size; ++i) {\n if (t->next[i] == nullptr) continue;\n if (a) os << \", \"; a = 1;\n os << (char)(i + margin);\n if (t->next[i]->depth > t->depth) os << \": \" << t->next[i];\n }\n return os << \"}\";\n }\n };\n\n Node* root;\n Trie() { root = new Node(0); }\n\n void add(const string& s, int id = 0) {\n Node* now = root;\n for (char c : s) {\n if (now->next[c - margin] == nullptr) {\n now->next[c - margin] = new Node(now->depth + 1);\n }\n now = now->next[c - margin];\n }\n now->accept.push_back(id);\n }\n\n friend ostream& operator<<(ostream& os, const Trie& t) {\n return os << t.root;\n }\n};\n\ntemplate<int char_size, int margin>\nstruct AhoCorasick {\n Trie<char_size + 1, margin> trie;\n const int FAIL = char_size;\n\n AhoCorasick() : trie() {}\n\n void add(const string& s, int id = 0) {\n trie.add(s, id);\n }\n\n using Node = typename Trie<char_size + 1, margin>::Node;\n\n void build() {\n queue<Node*> que;\n for (int i = 0; i < char_size; ++i) {\n if (trie.root->next[i]) {\n que.push(trie.root->next[i]);\n trie.root->next[i]->next[FAIL] = trie.root;\n } else {\n trie.root->next[i] = trie.root;\n }\n }\n while (!que.empty()) {\n Node* now = que.front(); que.pop();\n for (int i = 0; i < char_size; ++i) {\n if (now->next[i] == nullptr) continue;\n Node* fail = now->next[FAIL];\n while (fail->next[i] == nullptr) {\n fail = fail->next[FAIL];\n }\n now->next[i]->next[FAIL] = fail->next[i];\n auto &u = now->next[i]->accept;\n auto &v = fail->next[i]->accept;\n vector<int> accept;\n set_union(u.begin(), u.end(), v.begin(), v.end(),\n back_inserter(accept));\n u = accept;\n que.push(now->next[i]);\n }\n }\n }\n\n map<int,int> match(const string& str) {\n map<int,int> ret;\n Node* now = trie.root;\n for (char c : str) {\n while (now->next[c - margin] == nullptr) now = now->next[FAIL];\n now = now->next[c - margin];\n for (int id : now->accept) ++ret[id];\n }\n return ret;\n }\n\n friend ostream& operator<<(ostream& os, const AhoCorasick& a) {\n return os << a.trie;\n }\n};\n\n\nint main()\n{\n cin.tie(0);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(10);\n\n map<char,int> dy, dx;\n dy['U'] = -1, dy['R'] = 0, dy['D'] = 1, dy['L'] = 0;\n dx['U'] = 0, dx['R'] = 1, dx['D'] = 0, dx['L'] = -1;\n\n while (true) {\n int n, m; cin >> n >> m;\n if (n == 0) break;\n vector<string> board(n);\n int si, sj;\n for (int i = 0; i < n; ++i) {\n cin >> board[i];\n for (int j = 0; j < m; ++j) {\n if (board[i][j] == 'S') {\n si = i, sj = j;\n }\n }\n }\n\n AhoCorasick<26,'A'> aho;\n int P; cin >> P;\n for (int i = 0; i < P; ++i) {\n string p; cin >> p;\n aho.add(p, i);\n }\n aho.build();\n\n using Node = AhoCorasick<26,'A'>::Node;\n\n queue<tuple<int,int,int,Node*>> que;\n que.emplace(si, sj, 0, aho.trie.root);\n map<tuple<int,int,Node*>,bool> used;\n used[make_tuple(si, sj, aho.trie.root)] = true;\n\n int ans = -1;\n\n while (!que.empty()) {\n int i, j, d; Node* now;\n tie(i, j, d, now) = que.front(); que.pop();\n if (board[i][j] == 'G') {\n ans = d;\n break;\n }\n for (char dir : { 'U', 'R', 'D', 'L' }) {\n Node* tmp = now;\n int y = i + dy[dir], x = j + dx[dir];\n if (y < 0 or n <= y or x < 0 or m <= x or\n board[y][x] == '#') continue;\n\n while (tmp->next[dir - 'A'] == nullptr) {\n tmp = tmp->next[26];\n }\n tmp = tmp->next[dir - 'A'];\n if (tmp->accept.size()) {\n continue;\n }\n if (!used[make_tuple(y, x, tmp)]) {\n used[make_tuple(y, x, tmp)] = true;\n que.emplace(y, x, d + 1, tmp);\n }\n }\n }\n\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 190, "memory_kb": 9792, "score_of_the_acc": -1.4594, "final_rank": 16 }, { "submission_id": "aoj_2212_4369201", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\ntemplate<class T>\nbool chmax(T& a, const T& b) {\n if (a < b) { a = b; return true; }\n return false;\n}\ntemplate<class T>\nbool chmin(T& a, const T& b) {\n if (b < a) { a = b; return true; }\n return false;\n}\n\n// std::vector Declaration\ntemplate<typename T>\nvector<T> make_v(size_t a) { return vector<T>(a); }\ntemplate<typename T, typename... Ts>\nauto make_v(size_t a, Ts... ts) {\n return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));\n}\n\n// std::vector Declaration and Initialization\ntemplate<typename T>\nvector<T> make_vector(size_t a, T x) { return vector<T>(a, x); }\ntemplate<typename T, typename U, typename... Ts>\nauto make_vector(size_t a, U b, Ts... ts) {\n return vector<decltype(make_vector<T>(b,ts...))>(a, make_vector<T>(b, ts...));\n}\n\n// std::vector Input\ntemplate<typename T>\nistream& operator>>(istream& is, vector<T>& v) {\n for (auto &e : v) is >> e;\n return is;\n}\n\n// std::vector Debug\ntemplate<typename T>\nostream& operator<<(ostream& os, const vector<T>& v) {\n os << \"[\";\n bool a = 1;\n for (auto e : v) {\n os << (a ? \"\" : \" \");\n os << e;\n a = 0;\n }\n os << \"]\";\n return os;\n}\n\n// std::array Debug\ntemplate<typename T, size_t n>\nostream& operator<<(ostream& os, const array<T, n>& v) {\n os << \"[\";\n bool a = 1;\n for (auto e : v) {\n os << (a ? \"\" : \" \");\n os << e;\n a = 0;\n }\n os << \"]\";\n return os;\n}\n\n// std::deque Debug\ntemplate<typename T>\nostream& operator<<(ostream& os, const deque<T>& d) {\n os << \"[\";\n bool a = 1;\n for (auto e : d) {\n os << (a ? \"\" : \" \");\n os << e;\n a = 0;\n }\n os << \"]\";\n return os;\n}\n\n// std::pair Debug\ntemplate<typename T, typename U>\nostream& operator<<(ostream& os, const pair<T, U>& p) {\n os << \"(\" << p.first << \" \" << p.second << \")\";\n return os;\n}\n\n// std::set Debug\ntemplate<typename T>\nostream& operator<<(ostream& os, const set<T>& st) {\n os << \"{\";\n bool a = 1;\n for (auto e : st) {\n os << (a ? \"\" : \" \");\n os << e;\n a = 0;\n }\n os << \"}\";\n return os;\n}\n\n// std::multiset Debug\ntemplate<typename T>\nostream& operator<<(ostream& os, const multiset<T>& st) {\n os << \"{\";\n bool a = 1;\n for (auto e : st) {\n os << (a ? \"\" : \" \");\n os << e;\n a = 0;\n }\n os << \"}\";\n return os;\n}\n\n// std::map Debug\ntemplate<typename T, typename U>\nostream& operator<<(ostream& os, const map<T, U>& mp) {\n os << \"{\";\n bool a = 1;\n for (auto e : mp) {\n os << (a ? \"\" : \" \");\n os << e.first << \":\" << e.second;\n a = 0;\n }\n os << \"}\";\n return os;\n}\n\n// std::tuple Debug\ntemplate<int N, class Tuple>\nvoid out(ostream& os, const Tuple& t){}\ntemplate<int N, class Tuple, class H, class ...Ts>\nvoid out(ostream& os, const Tuple& t) {\n if (N) os << \" \";\n os << get<N>(t);\n out<N+1,Tuple,Ts...>(os, t);\n}\ntemplate<class ...Ts>\nostream& operator<<(ostream& os, const tuple<Ts...>& t) {\n os << \"(\";\n out<0,tuple<Ts...>,Ts...>(os, t);\n os << \")\";\n return os;\n}\n\n// Debug\n#define DUMP(x) cerr<<#x<<\" = \"<<(x)<<endl\n\n// Weighted edge\ntemplate<typename T>\nstruct edge {\n int src, to;\n T cost;\n\n edge() {}\n edge(int to, T cost) : src(-1), to(to), cost(cost) {}\n edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}\n\n friend ostream& operator<<(ostream& os, const edge& e) {\n return os << \"(\" << e.src << \"->\" << e.to << \":\" << e.cost << \")\";\n }\n};\n\nusing LL = int64_t;\n\n#define fs first\n#define sc second\n\nconst int64_t MOD = 1e9+7;\n\ntemplate<int char_size, int margin>\nstruct Trie {\n struct Node {\n Node* next[char_size];\n vector<int> accept;\n Node() : accept(0) {\n for (int i = 0; i < char_size; i++)\n next[i] = nullptr;\n }\n };\n\n Node* root;\n Trie() { root = new Node(); }\n\n void add(const string& s, int id = 0) {\n Node* now = root;\n for (char c : s) {\n if (now->next[c - margin] == nullptr) {\n now->next[c - margin] = new Node();\n }\n now = now->next[c - margin];\n }\n now->accept.push_back(id);\n }\n};\n\ntemplate<int char_size, int margin>\nstruct AhoCorasick {\n Trie<char_size + 1, margin> trie;\n const int FAIL = char_size;\n\n AhoCorasick() : trie() {}\n\n void add(const string& s, int id = 0) {\n trie.add(s, id);\n }\n\n using node_t = typename Trie<char_size + 1, margin>::Node;\n\n void build() {\n queue<node_t*> que;\n for (int i = 0; i < char_size; ++i) {\n if (trie.root->next[i]) {\n que.push(trie.root->next[i]);\n trie.root->next[i]->next[FAIL] = trie.root;\n } else {\n trie.root->next[i] = trie.root;\n }\n }\n while (!que.empty()) {\n node_t* now = que.front(); que.pop();\n for (int i = 0; i < char_size; ++i) {\n if (now->next[i] == nullptr) continue;\n node_t* fail = now->next[FAIL];\n while (fail->next[i] == nullptr) {\n fail = fail->next[FAIL];\n }\n now->next[i]->next[FAIL] = fail->next[i];\n auto &u = now->next[i]->accept;\n auto &v = fail->next[i]->accept;\n vector<int> accept;\n set_union(u.begin(), u.end(), v.begin(), v.end(),\n back_inserter(accept));\n u = accept;\n que.push(now->next[i]);\n }\n }\n }\n\n map<int,int> match(const string& str) {\n map<int,int> ret;\n node_t* now = trie.root;\n for (char c : str) {\n while (now->next[c - margin] == nullptr) now = now->next[FAIL];\n now = now->next[c - margin];\n for (int id : now->accept) ++ret[id];\n }\n return ret;\n }\n};\n\n\nint main()\n{\n cin.tie(0);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(10);\n\n map<char,int> dy, dx;\n dy['U'] = -1, dy['R'] = 0, dy['D'] = 1, dy['L'] = 0;\n dx['U'] = 0, dx['R'] = 1, dx['D'] = 0, dx['L'] = -1;\n\n while (true) {\n int n, m; cin >> n >> m;\n if (n == 0) break;\n vector<string> board(n);\n int si, sj;\n for (int i = 0; i < n; ++i) {\n cin >> board[i];\n for (int j = 0; j < m; ++j) {\n if (board[i][j] == 'S') {\n si = i, sj = j;\n }\n }\n }\n\n AhoCorasick<26,'A'> aho;\n\n int P; cin >> P;\n for (int i = 0; i < P; ++i) {\n string p; cin >> p;\n aho.add(p, i);\n }\n\n aho.build();\n\n using node_t = AhoCorasick<26,'A'>::node_t;\n\n queue<tuple<int,int,int,node_t*>> que;\n que.emplace(si, sj, 0, aho.trie.root);\n map<tuple<int,int,node_t*>,bool> used;\n used[make_tuple(si, sj, aho.trie.root)] = true;\n\n int ans = -1;\n\n while (!que.empty()) {\n int i, j, d; node_t* now;\n tie(i, j, d, now) = que.front(); que.pop();\n if (board[i][j] == 'G') {\n ans = d;\n break;\n }\n for (char dir : { 'U', 'R', 'D', 'L' }) {\n node_t* tmp = now;\n int y = i + dy[dir], x = j + dx[dir];\n if (y < 0 or n <= y or x < 0 or m <= x or\n board[y][x] == '#') continue;\n\n while (tmp->next[dir - 'A'] == nullptr) {\n tmp = tmp->next[26];\n }\n tmp = tmp->next[dir - 'A'];\n if (tmp->accept.size()) {\n continue;\n }\n if (!used[make_tuple(y, x, tmp)]) {\n used[make_tuple(y, x, tmp)] = true;\n que.emplace(y, x, d + 1, tmp);\n }\n }\n }\n\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 180, "memory_kb": 9756, "score_of_the_acc": -1.4251, "final_rank": 15 }, { "submission_id": "aoj_2212_4207211", "code_snippet": "#include <iostream>\n#include <cstdio>\n#include <cstring>\n#include <algorithm>\n#include <cmath>\n#include <queue>\n#include <utility>\n#include <tuple>\n#define maxn 55\n#define maxV 105\n#define mt make_tuple\n\nusing namespace std;\ntypedef long long ll;\ntypedef tuple<int, int, int> T;\nstruct node{\n int chi[4], fail;\n bool forbid;\n node(){\n memset(chi, 0, sizeof(chi));\n fail = forbid = 0;\n }\n}a[maxV]; int V;\n// root: node 1.\nnamespace acAuto{\n void init(){\n for(int i = 1;i <= V;i++) a[i] = node();\n V = 1;\n }\n void ins(int* s, int n){ // |S| = n.\n int u = 1;\n for(int i = 1;i <= n;i++){\n if(!a[u].chi[s[i]]) a[u].chi[s[i]] = ++V;\n u = a[u].chi[s[i]];\n }\n a[u].forbid = true;\n }\n void build(){\n queue<int> que;\n que.push(1);\n while(!que.empty()){\n int u = que.front();\n que.pop();\n for(int i = 0;i < 4;i++){\n int v = a[u].chi[i], f = a[u].fail;\n if(v){\n if(f) a[v].fail = a[f].chi[i];\n else a[v].fail = 1;\n if(a[a[v].fail].forbid) a[v].forbid = true;\n que.push(v);\n }else{\n if(f) a[u].chi[i] = a[f].chi[i];\n else a[u].chi[i] = 1;\n }\n }\n }\n }\n}\nconst int dx[] = {-1, 0, 1, 0};\nconst int dy[] = {0, 1, 0, -1};\nint idx[135], n, m, p, sidx[15], rs, cs, rg, cg;\nchar grid[maxn][maxn], s[15];\nint f[maxn][maxn][maxV];\nvoid solve(){\n acAuto::init();\n for(int i = 1;i <= n;i++) scanf(\"%s\", grid[i] + 1);\n scanf(\"%d\", &p);\n for(int i = 1;i <= p;i++){\n scanf(\"%s\", s + 1);\n int l = strlen(s + 1);\n for(int j = 1;j <= l;j++) sidx[j] = idx[(int)s[j]];\n acAuto::ins(sidx, l);\n }\n acAuto::build();\n for(int i = 0;i <= n + 1;i++) grid[i][0] = grid[i][m + 1] = '#';\n for(int i = 1;i <= m;i++) grid[0][i] = grid[n + 1][i] = '#';\n for(int i = 1;i <= n;i++){\n for(int j = 1;j <= m;j++){\n if(grid[i][j] == 'S') rs = i, cs = j;\n else if(grid[i][j] == 'G') rg = i, cg = j;\n }\n }\n memset(f, 0x3f, sizeof(f));\n f[rs][cs][1] = 0;\n queue<T> que;\n que.push(mt(rs, cs, 1));\n while(!que.empty()){\n int x = get<0>(que.front()), y = get<1>(que.front()), u = get<2>(que.front());\n que.pop();\n if(x == rg && y == cg){\n printf(\"%d\\n\", f[x][y][u]);\n return;\n }\n for(int d = 0;d < 4;d++){\n int cx = x + dx[d], cy = y + dy[d], cu = a[u].chi[d];\n if(grid[cx][cy] != '#' && !a[cu].forbid && f[x][y][u] + 1 < f[cx][cy][cu]){\n f[cx][cy][cu] = f[x][y][u] + 1;\n que.push(mt(cx, cy, cu));\n }\n }\n }\n puts(\"-1\");\n}\nint main(){\n idx['U'] = 0, idx['R'] = 1, idx['D'] = 2, idx['L'] = 3;\n while(~scanf(\"%d%d\", &n, &m) && n) solve();\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 4564, "score_of_the_acc": -0.1205, "final_rank": 8 }, { "submission_id": "aoj_2212_3986517", "code_snippet": "#include <bits/stdc++.h>\n\ntemplate<class Converter, class SuffixInfo, int NODE_NUM = 1000000>\nclass AhoCorasick {\npublic:\n using value_structure = typename Converter::value_structure;\n using size_type = std::uint64_t;\n static constexpr size_type num_of_kinds = Converter::num_of_kinds;\n\n using value_type = typename SuffixInfo::value_type;\n using merged_value_type = typename SuffixInfo::merged_value_type;\n\n struct Node {\n size_type size, end, len, faillink;\n value_type val;\n merged_value_type suf_info;\n std::array<int, num_of_kinds> ch;\n Node () : size(0), end(0), len(0), val(SuffixInfo::identity()),\n suf_info(SuffixInfo::midentity()), faillink(0) { ch.fill(-1); }\n };\n\nprivate:\n std::vector<Node> node;\n\npublic:\n AhoCorasick() {\n node.reserve(NODE_NUM);\n node.push_back(Node());\n }\n\n void insert(const value_structure& v, size_type num = 1, value_type val = value_type(), size_type k = 0, size_type idx = 0) {\n node[idx].size += num;\n if (k == v.size()) {\n node[idx].end += num;\n node[idx].val = SuffixInfo::operation(node[idx].val, val);\n return;\n }\n size_type nxt = Converter::convert(v[k]);\n if (node[idx].ch[nxt] == -1) {\n node.push_back(Node());\n node[idx].ch[nxt] = node.size()-1;\n node.back().len = k+1;\n }\n insert(v, num, val, k+1, node[idx].ch[nxt]);\n }\n\n size_type count_prefix(const value_structure& v, size_type k = 0, size_type idx = 0) {\n if (v.size() == k) return node[idx].size;\n size_type nxt = Converter::convert(v[k]);\n if (node[idx].ch[nxt] == -1) return 0;\n return count_prefix(v, k+1, node[idx].ch[nxt]);\n }\n\n size_type count(const value_structure& v, size_type k = 0, size_type idx = 0) {\n if (v.size() == k) return node[idx].end;\n size_type nxt = Converter::convert(v[k]);\n if (node[idx].ch[nxt] == -1) return 0;\n return count(v, k+1, node[idx].ch[nxt]);\n }\n\n template<typename F>\n void query(const value_structure& v, const F& f, size_type k = 0, size_type idx = 0) {\n if (node[idx].size > 0) f(node[idx]);\n if (v.size() == k) return;\n size_type nxt = Converter::convert(v[k]);\n if (node[idx].ch[nxt] == -1) return;\n query(v, f, k+1, node[idx].ch[nxt]);\n }\n\n size_type proceed(size_type k, size_type c, bool need_convert = 1) {\n if (need_convert) c = Converter::convert(c);\n while (node[k].ch[c] == -1) k = node[k].faillink;\n return node[k].ch[c];\n }\n\n void build() {\n std::queue<size_type> que;\n for (int i = 0; i < num_of_kinds; i++) {\n if (node[0].ch[i] == -1) node[0].ch[i] = 0;\n else {\n que.push(node[0].ch[i]);\n SuffixInfo::merge(node[node[0].ch[i]].suf_info, node[node[0].ch[i]].val);\n }\n }\n while (que.size()) {\n int k = que.front();\n que.pop();\n for (int i = 0; i < num_of_kinds; i++) {\n if (node[k].ch[i] == -1) continue;\n size_type nx = node[k].ch[i];\n node[nx].faillink = proceed(node[k].faillink, i, false);\n\n SuffixInfo::merge(node[nx].suf_info, node[nx].val);\n SuffixInfo::merge(node[nx].suf_info, node[node[nx].faillink].suf_info);\n que.push(nx);\n }\n }\n }\n\n const Node& operator[](size_type k) {\n return node[k];\n }\n};\n\nint dy[4] = {-1, 1, 0, 0};\nint dx[4] = {0, 0, -1, 1};\n\nclass Converter {\npublic:\n using value_structure = std::string;\n using value_type = typename value_structure::value_type;\n static constexpr std::size_t num_of_kinds = 4;\n static std::size_t convert(const value_type& v) {\n switch (v) {\n case 'U': return 0;\n case 'D': return 1;\n case 'L': return 2;\n case 'R': return 3;\n }\n }\n};\n\nclass SuffixInfo {\npublic:\n using value_type = int;\n using merged_value_type = int;\n static value_type identity() { return 0; }\n static merged_value_type midentity() { return 0; }\n static value_type operation(const value_type& a, const value_type& b) {\n return a | b;\n }\n static void merge(merged_value_type& a, const merged_value_type& b) {\n a |= b;\n }\n// static void merge(merged_value_type& a, const value_type& b) {\n// a |= b;\n// }\n};\n\nvoid solve() {\n using namespace std;\n AhoCorasick<Converter, SuffixInfo> ah;\n int n, m;\n cin >> n >> m;\n if (n + m == 0) exit(0);\n vector<string> f(n);\n string dir=\"UDLR\";\n pair<int, int> st, goal;\n for (int i = 0; i < n; i++) {\n cin >> f[i];\n for (int j = 0; j < m; j++) {\n if (f[i][j] == 'S') st = {i, j};\n if (f[i][j] == 'G') goal = {i, j};\n }\n }\n auto in = [&](int y, int x) {\n return 0<=y&&y<n&&0<=x&&x<m&&f[y][x]!='#';\n };\n int P;\n cin >> P;\n for (int i = 0; i < P; i++) {\n string s;\n cin >> s;\n ah.insert(s, 1, 1);\n }\n ah.build();\n using T = tuple<int, int, int, int>; // d, y, x, v\n vector<vector<vector<int>>> d(n, vector<vector<int>>(m, vector<int>(110, (int)1e9)));\n queue<T> que;\n que.emplace(0, st.first, st.second, 0);\n d[st.first][st.second][0] = 0;\n while (que.size()) {\n int dd, ny, nx, v;\n tie(dd, ny, nx, v) = que.front(); que.pop();\n for (int i = 0; i < 4; i++) {\n int yy = ny+dy[i], xx = nx+dx[i], vv = ah.proceed(v, dir[i]);\n if (!in(yy, xx)) continue;\n if (ah[vv].suf_info) {\n continue;\n } else {\n if (d[yy][xx][vv] > dd + 1) {\n d[yy][xx][vv] = dd + 1;\n que.emplace(dd+1, yy, xx, vv);\n }\n }\n }\n }\n int res = 1e9;\n for (int i = 0; i < 110; i++) {\n res = min(res, d[goal.first][goal.second][i]);\n }\n if (res == 1e9) {\n cout << -1 << endl;\n } else {\n cout << res << endl;\n }\n}\n\nint main(void) {\n while(1) {\n solve();\n }\n}\n\n/*\nverify: https://tenka1-2016-final-open.contest.atcoder.jp/submissions/8400970\n\ntemplate<class Converter, class SuffixInfo>\nclass AhoCorasick\n\nConverter:\n\t- 要求\n\t\t- value_structure: std::string, std::vectorなどの列構造\n\t\t\t- size, operator[] が必要\n\n\t\t- static constexpr std::size_t num_of_kinds\n\t\t\t- 現れる値の種類数\n\n\t\t- static convert(value_type) -> size_t\n\t\t\t- 値を[0, num_of_kinds)に変換する\n\nSuffixInfo:\n - 要求\n - value_type: それぞれの文字列が持つ値の型\n\n - merged_value_type: suffixが一致する要素の情報をまとめたもの\n\n - identity -> value_type\n - value_typeの単位元(初期値)\n\n - midentity -> merged_value_type\n - merged_value_typeの初期値(単位元とは???)\n\n - operation(const value_type&, const value_type&)\n\n - merge(merged_value_type&, const value_type&)\n - merge(merged_value_type&, const merged_value_type&)\n\n\nAhoCorasick\n\t- 提供\n\t\t- Node\n\t\t\t- size\n\t\t\t\t- 部分木に単語がいくつあるか\n\n\t\t\t- end\n\t\t\t\t- その頂点で終わる単語がいくつあるか\n\n\t\t\t- len\n\t\t\t\t- その頂点までで何文字あるか\n\t\t\t\n\t\t\t- ch[num_of_kinds]\n\t\t\t\t- それぞれの子のpoolでのindex(存在しなければ-1)\n\n\t\t- insert(value_structure v, num, k)\n\t\t\t- O(|v|)\n\t\t\t- num個のv[k..)を挿入する\n\n\t\t- count_prefix(value_structure v, k)\n\t\t\t- O(|v|)\n\t\t\t- v[k..)をprefixとして含むものがいくつあるか返す\n\n\t\t- count(value_structure v, k)\n\t\t\t- O(|v|)\n\t\t\t- v[k..)がいくつあるか返す\n\n\t\t- query(value_structure v, F f, k)\n\t\t\t- O(|v|)\n\t\t\t- v[k..)のprefixそれぞれについてf(node)を呼び出す\n\n - build\n\n - proceed(k, c)\n - 今いる頂点がkのとき、次の文字がcのときに進む頂点を返す\n */", "accuracy": 1, "time_ms": 20, "memory_kb": 4148, "score_of_the_acc": -0.0835, "final_rank": 5 }, { "submission_id": "aoj_2212_3973560", "code_snippet": "#include \"bits/stdc++.h\"\n\n#define REP(i, n) for (int i = 0; i < n; ++i)\n// https://onlinejudge.u-aizu.ac.jp/status/users/EtoNagisa/submissions/1/2212/judge/3966278/C++14\nusing namespace std;\n\n#include \"bits/stdc++.h\"\n\nstruct lower_alphabet {\n static constexpr std::size_t char_size = 26;\n static constexpr char min_char = 'a';\n};\nstruct upper_alphabet {\n static constexpr std::size_t char_size = 26;\n static constexpr char min_char = 'A';\n};\nstruct general_char {\n static constexpr std::size_t char_size = 256;\n static constexpr char min_char = 0;\n};\ntemplate <typename character>\nclass aho_corasick {\n public:\n static constexpr std::size_t invalid_index = -1;\n struct PMA {\n std::size_t fail; // index of state correspond to fail suffix\n std::vector<std::size_t> next, accept;\n std::set<std::size_t> r_fail;\n PMA() : fail(0), next(character::char_size, invalid_index) {}\n };\n std::vector<PMA> nodes;\n std::size_t cnt;\n\n aho_corasick() : cnt(0) {}\n void build(const std::vector<std::string> &dict) {\n add_node();\n for (std::size_t i = 0; i < dict.size(); ++i) {\n std::size_t now = 0;\n for (std::size_t j = 0; j < dict[i].size(); ++j) {\n std::size_t t = static_cast<size_t>(dict[i][j] - character::min_char);\n if (nodes[now].next[t] == invalid_index) {\n std::size_t next = size();\n add_node();\n nodes[now].next[t] = next;\n }\n now = nodes[now].next[t];\n }\n nodes[now].accept.push_back(i);\n }\n std::queue<std::size_t> q;\n for (std::size_t i = 0; i < character::char_size; ++i) {\n if (nodes[0].next[i] == invalid_index) {\n nodes[0].next[i] = 0;\n } else {\n set_fail(nodes[0].next[i], 0);\n q.push(nodes[0].next[i]);\n }\n }\n while (!q.empty()) {\n std::size_t p = q.front();\n q.pop();\n for (std::size_t i = 0; i < character::char_size; ++i) {\n if (nodes[p].next[i] != invalid_index) {\n std::size_t tmp = next(nodes[p].fail, i + character::min_char);\n set_fail(nodes[p].next[i], tmp);\n for (std::size_t ac : nodes[tmp].accept) {\n nodes[nodes[p].next[i]].accept.push_back(ac);\n }\n q.push(nodes[p].next[i]);\n }\n }\n }\n }\n void add(const std::string &s) {\n std::size_t start = -1, now = 0;\n for (std::size_t i = 0; i < s.size(); ++i) {\n std::size_t t = static_cast<size_t>(s[i] - character::min_char);\n if (nodes[now].next[t] == invalid_index) {\n start = i;\n break;\n }\n now = nodes[now].next[t];\n }\n for (std::size_t i = start; i < s.size(); ++i) {\n std::size_t t = static_cast<size_t>(s[i] - character::min_char);\n std::size_t nxt = size();\n add_node();\n nodes[now].next[t] = nxt;\n std::size_t tmp = next(nodes[now].fail, s[i]);\n set_fail(nxt, tmp);\n for (std::size_t ac : nodes[tmp]) {\n nodes[nxt].accept.push_back(ac);\n }\n\n for (std::size_t p : nodes[now].r_fail) {\n if (nodes[p].next[t] != invalid_index) {\n set_fail(nodes[p].next[t], nxt);\n }\n }\n now = nxt;\n }\n }\n std::size_t size() const { return cnt; }\n std::size_t next(std::size_t now, char c) const {\n std::size_t t = static_cast<std::size_t>(c - character::min_char);\n while (nodes[now].next[t] == invalid_index) now = nodes[now].fail;\n return nodes[now].next[t];\n }\n std::vector<std::vector<std::size_t>> match(const std::string &s) const {\n std::size_t now = 0;\n std::vector<std::vector<std::size_t>> ret(s.size());\n for (std::size_t i = 0; i < s.size(); ++i) {\n now = next(now, s[i]);\n for (auto ac : nodes[now].accept) {\n ret[i].push_back(ac);\n }\n }\n }\n\n private:\n void add_node() {\n nodes.emplace_back();\n cnt++;\n }\n void set_fail(std::size_t from, std::size_t to) {\n nodes[nodes[from].fail].r_fail.erase(from);\n nodes[from].fail = to;\n nodes[to].r_fail.insert(from);\n }\n};\nint dx[4] = {1, 0, -1, 0};\nint dy[4] = {0, 1, 0, -1};\nstring mv = \"DRUL\";\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n int n, m;\n while (cin >> n >> m, n) {\n vector<string> v(n);\n REP(i, n) cin >> v[i];\n int qu;\n cin >> qu;\n vector<string> p(qu);\n REP(i, qu) cin >> p[i];\n aho_corasick<upper_alphabet> ac;\n ac.build(p);\n int sx = -1, sy = -1, tx = -1, ty = -1;\n REP(i, n) REP(j, m) {\n if (v[i][j] == 'S') {\n sx = i;\n sy = j;\n } else if (v[i][j] == 'G') {\n tx = i;\n ty = j;\n }\n }\n int k = ac.nodes.size();\n vector<vector<vector<int>>> dp(n,\n vector<vector<int>>(m, vector<int>(k, 1e9)));\n dp[sx][sy][0] = 0;\n queue<pair<pair<int, int>, int>> q;\n q.push({{sx, sy}, 0});\n while (!q.empty()) {\n auto c = q.front();\n q.pop();\n int x = c.first.first, y = c.first.second, s = c.second;\n REP(d, 4) {\n int nx = x + dx[d], ny = y + dy[d], ns = ac.next(s, mv[d]);\n if (nx < 0 || nx >= n || ny < 0 || ny >= m || v[nx][ny] == '#' ||\n ac.nodes[ns].accept.size() > 0)\n continue;\n if (dp[nx][ny][ns] > dp[x][y][s] + 1) {\n dp[nx][ny][ns] = dp[x][y][s] + 1;\n q.push({{nx, ny}, ns});\n }\n }\n }\n int ans = 1e9;\n REP(i, k) ans = min(ans, dp[tx][ty][i]);\n if (ans == 1e9)\n cout << -1 << endl;\n else\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3800, "score_of_the_acc": -0.0286, "final_rank": 1 }, { "submission_id": "aoj_2212_3973495", "code_snippet": "#include \"bits/stdc++.h\"\n\n#define REP(i, n) for (int i = 0; i < n; ++i)\n// https://onlinejudge.u-aizu.ac.jp/status/users/EtoNagisa/submissions/1/2212/judge/3966278/C++14\nusing namespace std;\n\n#include \"bits/stdc++.h\"\n\nstruct lower_alphabet {\n static constexpr std::size_t char_size = 26;\n static constexpr char min_char = 'a';\n};\nstruct upper_alphabet {\n static constexpr std::size_t char_size = 26;\n static constexpr char min_char = 'A';\n};\nstruct general_char {\n static constexpr std::size_t char_size = 256;\n static constexpr char min_char = 0;\n};\ntemplate <typename character>\nclass aho_corasick {\n public:\n static constexpr std::size_t invalid_index = -1;\n struct PMA {\n std::size_t fail; // index of state correspond to fail suffix\n std::vector<std::size_t> next, accept;\n std::set<std::size_t> r_fail;\n PMA() : fail(0), next(character::char_size, invalid_index) {}\n };\n std::vector<PMA> nodes;\n std::size_t cnt;\n\n aho_corasick() : cnt(0) {}\n void build(const std::vector<std::string> &dict) {\n add_node();\n for (std::size_t i = 0; i < dict.size(); ++i) {\n std::size_t now = 0;\n for (std::size_t j = 0; j < dict[i].size(); ++j) {\n std::size_t t = static_cast<size_t>(dict[i][j] - character::min_char);\n if (nodes[now].next[t] == invalid_index) {\n std::size_t next = size();\n add_node();\n nodes[now].next[t] = next;\n }\n now = nodes[now].next[t];\n }\n nodes[now].accept.push_back(i);\n }\n std::queue<std::size_t> q;\n for (std::size_t i = 0; i < character::char_size; ++i) {\n if (nodes[0].next[i] == invalid_index) {\n nodes[0].next[i] = 0;\n } else {\n set_fail(nodes[0].next[i], 0);\n q.push(nodes[0].next[i]);\n }\n }\n while (!q.empty()) {\n std::size_t p = q.front();\n q.pop();\n for (std::size_t i = 0; i < character::char_size; ++i) {\n if (nodes[p].next[i] != invalid_index) {\n std::size_t tmp = next(nodes[p].fail, i + character::min_char);\n set_fail(nodes[p].next[i], tmp);\n for (std::size_t ac : nodes[tmp].accept) {\n nodes[nodes[p].next[i]].accept.push_back(ac);\n }\n q.push(nodes[p].next[i]);\n }\n }\n }\n }\n\n std::size_t size() const { return cnt; }\n std::size_t next(std::size_t now, char c) const {\n std::size_t t = static_cast<std::size_t>(c - character::min_char);\n while (nodes[now].next[t] == invalid_index) now = nodes[now].fail;\n return nodes[now].next[t];\n }\n std::vector<std::vector<std::size_t>> match(const std::string &s) const {\n std::size_t now = 0;\n std::vector<std::vector<std::size_t>> ret(s.size());\n for (std::size_t i = 0; i < s.size(); ++i) {\n now = next(now, s[i]);\n for (auto ac : nodes[now].accept) {\n ret[i].push_back(ac);\n }\n }\n }\n\n private:\n void add_node() {\n nodes.emplace_back();\n cnt++;\n }\n void set_fail(std::size_t from, std::size_t to) {\n nodes[nodes[from].fail].r_fail.erase(from);\n nodes[from].fail = to;\n nodes[to].r_fail.insert(from);\n }\n};\nint dx[4] = {1, 0, -1, 0};\nint dy[4] = {0, 1, 0, -1};\nstring mv = \"DRUL\";\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n int n, m;\n while (cin >> n >> m, n) {\n vector<string> v(n);\n REP(i, n) cin >> v[i];\n int qu;\n cin >> qu;\n vector<string> p(qu);\n REP(i, qu) cin >> p[i];\n aho_corasick<upper_alphabet> ac;\n ac.build(p);\n int sx = -1, sy = -1, tx = -1, ty = -1;\n REP(i, n) REP(j, m) {\n if (v[i][j] == 'S') {\n sx = i;\n sy = j;\n } else if (v[i][j] == 'G') {\n tx = i;\n ty = j;\n }\n }\n int k = ac.nodes.size();\n vector<vector<vector<int>>> dp(n,\n vector<vector<int>>(m, vector<int>(k, 1e9)));\n dp[sx][sy][0] = 0;\n queue<pair<pair<int, int>, int>> q;\n q.push({{sx, sy}, 0});\n while (!q.empty()) {\n auto c = q.front();\n q.pop();\n int x = c.first.first, y = c.first.second, s = c.second;\n REP(d, 4) {\n int nx = x + dx[d], ny = y + dy[d], ns = ac.next(s, mv[d]);\n if (nx < 0 || nx >= n || ny < 0 || ny >= m || v[nx][ny] == '#' ||\n ac.nodes[ns].accept.size() > 0)\n continue;\n if (dp[nx][ny][ns] > dp[x][y][s] + 1) {\n dp[nx][ny][ns] = dp[x][y][s] + 1;\n q.push({{nx, ny}, ns});\n }\n }\n }\n int ans = 1e9;\n REP(i, k) ans = min(ans, dp[tx][ty][i]);\n if (ans == 1e9)\n cout << -1 << endl;\n else\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3868, "score_of_the_acc": -0.0679, "final_rank": 3 } ]
aoj_2203_cpp
Problem G: Magical Island 2 まだ魔法があたりまえのように存在した時代である.ある魔導士の一族は,魔力によって作られた正方形の形の島に住んでいた. ある時,この島に危機が訪れた.帝国が大陸間弾道魔導ミサイルを開発し,この島を照準に合わせてきたのである.この世界の魔法は地属性,水属性,火属性,風属性などに分類でき,魔導ミサイルもその四つの属性のいずれかに属していた.島の四隅にはそれぞれ地の水晶,水の水晶,火の水晶,風の水晶が配置されていて,水晶に魔力を送り込むことで対応する属性の魔法攻撃を防ぐことがきでる. 魔導士達は帝国が開発した大陸間弾道魔導ミサイルによる攻撃をかろうじて退けることができた.しかし,帝国は新たに闇属性に属する大陸間弾道魔導ミサイルを開発し,再びこの島に攻撃を仕掛けてきた.闇属性に属する魔法はこの島に設置されている水晶で防ぐことはできない.そこで魔導士達は秘術としてこの島に伝えられる光属性の防御陣を用いてミサイルを防ぐ事にした. この光属性の防御陣はある特定の形状をした魔法陣を大地に描くことにより発動する. 魔法陣は半径 R の円周を M 等分し K 個おきに直線で結んだ図形である.魔法陣は任意の大きさで描くことができるが魔法の発動には方角が重要なので,一つの角が真北を向くように描かなければならない.防御陣の効果が及ぶ範囲は描かれた魔法陣の内部(境界を含む)と一致する.またこの魔法の詠唱に必要なエネルギーは描いた魔法陣の半径 R と等しい. 図G-1は M = 4, K = 1 の場合である.灰色の部分が魔方陣の効果が及ぶ領域である. 図G-2は M = 6, K = 2 の場合である.図のように複数の図形が生じる場合は,それらの図形のいずれかに含まれていれば防御陣としての効果が及ぶ. 図G-1: M = 4 , K = 1 の場合 小さい丸が集落を表す 図G-2: M = 6 , K = 2 の場合 あなたの仕事は,集落の位置,および M と K の値が与えられたときに,全ての集落をカバーするために必要となるエネルギーの最低量を求めるプログラムを書くことである. Input 入力は複数のデータセットからなる. 各データセットは以下の形式で与えられる. N M K x 1 y 1 x 2 y 2 ... x N y N 最初の一行は 3 つの整数 N , M , K からなる. N は集落の数を表す. M , K については問題文に記したとおりである. 2 ≤ N ≤ 50, 3 ≤ M ≤ 50, 1 ≤ K ≤ ( M - 1) / 2を仮定してよい. 続く N 行は集落の位置を表す. 各行は二つの整数 x i と y i からなる. x i と y i は i 番目の集落のx座標とy座標を表す. -1000 ≤ x i , y i ≤ 1000 を仮定して良い.y軸の正方向が北を指す. 入力の終わりはスペースで区切られた3個のゼロからなる. Output 各データセットに対し,魔法陣の最小の半径 R を1行に出力せよ. 出力する値は10 -6 以下の誤差を含んでいても構わない.値は小数点以下何桁表示しても構わない. Sample Input 4 4 1 1 0 0 1 -1 0 0 -1 5 6 2 1 1 4 4 2 -4 -4 2 1 5 0 0 0 Output for the Sample Input 1.000000000 6.797434948
[ { "submission_id": "aoj_2203_8216177", "code_snippet": "#include <algorithm>\n#include <assert.h>\n#include <bitset>\n#include <climits>\n#include <cmath>\n#include <complex>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <ctime>\n#include <deque>\n#include <iomanip>\n#include <iostream>\n#include <iterator>\n#include <list>\n#include <map>\n#include <queue>\n#include <set>\n#include <sstream>\n#include <stack>\n#include <string>\n#include <unordered_map>\n#include <valarray>\n#include <vector>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned int uint;\ntypedef unsigned char uchar;\ntypedef unsigned long long ull;\ntypedef pair<int, int> pii;\ntypedef pair<ll, ll> pll;\ntypedef vector<int> vi;\n#define REP(i, x) for (int i = 0; i < (int)(x); i++)\n#define REPS(i, x) for (int i = 1; i <= (int)(x); i++)\n#define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--)\n#define RREPS(i, x) for (int i = ((int)(x)); i > 0; i--)\n#define FOR(i, c) \\\n for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)\n#define RFOR(i, c) \\\n for (__typeof((c).rbegin()) i = (c).rbegin(); i != (c).rend(); i++)\n#define ALL(container) (container).begin(), (container).end()\n#define RALL(container) (container).rbegin(), (container).rend()\n#define SZ(container) ((int)container.size())\n#define mp(a, b) make_pair(a, b)\n#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());\ntemplate <class T> bool chmax(T &a, const T &b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <class T> bool chmin(T &a, const T &b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <class T> ostream &operator<<(ostream &os, const vector<T> &t) {\n os << \"[\";\n FOR(it, t) {\n if (it != t.begin())\n os << \",\";\n os << *it;\n }\n os << \"]\";\n return os;\n}\ntemplate <class T> ostream &operator<<(ostream &os, const set<T> &t) {\n os << \"{\";\n FOR(it, t) {\n if (it != t.begin())\n os << \",\";\n os << *it;\n }\n os << \"}\";\n return os;\n}\ntemplate <class S, class T>\nostream &operator<<(ostream &os, const pair<S, T> &t) {\n return os << \"(\" << t.first << \",\" << t.second << \")\";\n}\ntemplate <class S, class T>\npair<S, T> operator+(const pair<S, T> &s, const pair<S, T> &t) {\n return pair<S, T>(s.first + t.first, s.second + t.second);\n}\ntemplate <class S, class T>\npair<S, T> operator-(const pair<S, T> &s, const pair<S, T> &t) {\n return pair<S, T>(s.first - t.first, s.second - t.second);\n}\nnamespace geom {\n#define X real()\n#define Y imag()\n#define at(i) ((*this)[i])\n#define SELF (*this)\nenum { TRUE = 1, FALSE = 0, BORDER = -1 };\ntypedef int BOOL;\ntypedef double R;\nconst R INF = 1e8;\nR EPS = 1e-6;\nconst R PI = 3.1415926535897932384626;\ninline int sig(const R &x) { return (abs(x) < EPS ? 0 : x > 0 ? 1 : -1); }\ninline BOOL less(const R &x, const R &y) { return sig(x - y) ? x < y : BORDER; }\ntypedef complex<R> P;\ninline R norm(const P &p) { return p.X * p.X + p.Y * p.Y; }\ninline R inp(const P &a, const P &b) { return (conj(a) * b).X; }\ninline R outp(const P &a, const P &b) { return (conj(a) * b).Y; }\ninline P unit(const P &p) { return p / abs(p); }\ninline P proj(const P &s, const P &t) { return t * inp(s, t) / norm(t); }\ninline int ccw(const P &s, const P &t, const P &p, int adv = 0) {\n int res = sig(outp(t - s, p - s));\n if (res || !adv)\n return res;\n if (sig(inp(t - s, p - s)) < 0)\n return -2;\n if (sig(inp(s - t, p - t)) < 0)\n return 2;\n return 0;\n}\nstruct L : public vector<P> {\n L(const P &p1, const P &p2) {\n this->push_back(p1);\n this->push_back(p2);\n }\n L() {}\n inline P dir() const { return at(1) - at(0); }\n BOOL online(const P &p) const { return !sig(outp(p - at(0), dir())); }\n};\nstruct S : public L {\n S(const P &p1, const P &p2) : L(p1, p2) {}\n S() {}\n BOOL online(const P &p) const {\n if (!sig(norm(p - at(0))) || !sig(norm(p - at(1))))\n return BORDER;\n return !sig(outp(p - at(0), dir())) && inp(p - at(0), dir()) > -EPS &&\n inp(p - at(1), -dir()) > -EPS\n ? TRUE\n : FALSE;\n return !sig(abs(at(0) - p) + abs(at(1) - p) - abs(at(0) - at(1)));\n }\n};\nP crosspoint(const L &l, const L &m);\nstruct G : public vector<P> {\n G(size_type size = 0) : vector(size) {}\n S edge(int i) const { return S(at(i), at(i + 1 == size() ? 0 : i + 1)); }\n};\ninline BOOL intersect(const S &s, const L &l) {\n return (sig(outp(l.dir(), s[0] - l[0])) * sig(outp(l.dir(), s[1] - l[0])) <=\n 0);\n}\ninline P crosspoint(const L &l, const L &m) {\n R A = outp(l.dir(), m.dir()), B = outp(l.dir(), l[1] - m[0]);\n if (!sig(abs(A)) && !sig(abs(B)))\n return m[0];\n if (abs(A) < EPS)\n assert(false);\n return m[0] + B / A * (m[1] - m[0]);\n}\nstruct Arrangement {\n struct AEdge {\n int u, v, t;\n R cost;\n AEdge(int u = 0, int v = 0, int t = 0, R cost = 0)\n : u(u), v(v), t(t), cost(cost) {}\n };\n typedef vector<vector<AEdge>> AGraph;\n vector<P> p;\n AGraph g;\n Arrangement() {}\n Arrangement(vector<S> seg) {\n int m = seg.size();\n REP(i, m) {\n p.push_back(seg[i][0]);\n p.push_back(seg[i][1]);\n REP(j, i)\n if (sig(outp(seg[i].dir(), seg[j].dir())) &&\n intersect(seg[i], seg[j]) == TRUE)\n p.push_back(crosspoint(seg[i], seg[j]));\n }\n sort(ALL(p));\n UNIQUE(p);\n int n = p.size();\n g.resize(n);\n REP(i, m) {\n S &s = seg[i];\n vector<pair<R, int>> ps;\n REP(j, n) if (s.online(p[j])) ps.emplace_back(norm(p[j] - s[0]), j);\n sort(ALL(ps));\n REP(j, (int)ps.size() - 1) {\n const int u = ps[j].second;\n const int v = ps[j + 1].second;\n g[u].emplace_back(u, v, 0, abs(p[u] - p[v]));\n g[v].emplace_back(v, u, 0, abs(p[u] - p[v]));\n }\n }\n }\n int getIdx(P q) {\n auto it = lower_bound(ALL(p), q);\n if (it == p.end() || *it != q)\n return -1;\n return it - p.begin();\n }\n};\nstruct DualGraph {\n struct DEdge {\n int u, v, f, l;\n R a;\n DEdge(int u, int v, R a) : u(u), v(v), f(0), l(0) {\n while (PI < a)\n a -= 2 * PI;\n while (a < -PI)\n a += 2 * PI;\n this->a = a;\n }\n bool operator==(const DEdge &opp) const { return v == opp.v; }\n bool operator<(const DEdge &opp) const { return a > opp.a; }\n bool operator<(const R &opp) const { return a > opp; }\n friend ostream &operator<<(ostream &os, const DEdge &t) {\n return os << \"(\" << t.u << \",\" << t.v << \",\" << t.a * 180 / PI << \")\";\n }\n };\n int n;\n vector<P> p;\n vector<vector<DEdge>> g;\n DualGraph(const vector<P> &p) : p(p), g(p.size()), n(p.size()) {}\n void add_edge(const int s, const int t) {\n R a = arg(p[t] - p[s]);\n g[s].emplace_back(s, t, a);\n g[t].emplace_back(t, s, a > 0 ? a - PI : a + PI);\n }\n void add_polygon(int s, G &t, R a) {\n auto e = lower_bound(ALL(g[s]), a - EPS);\n if (e == g[s].end())\n e = g[s].begin();\n if (e->f)\n return;\n e->f = 1;\n t.push_back(p[s]);\n add_polygon(e->v, t, e->a > 0 ? e->a - PI : e->a + PI);\n }\n G dual() {\n REP(i, n) {\n sort(ALL(g[i]));\n UNIQUE(g[i]);\n }\n int s = min_element(ALL(p)) - p.begin();\n G poly;\n add_polygon(s, poly, -PI * (R).5);\n return poly;\n }\n};\n#undef SELF\n#undef at\n} // namespace geom\nusing namespace geom;\nnamespace std {\nbool operator<(const P &a, const P &b) {\n return sig(a.X - b.X) ? a.X < b.X : a.Y + EPS < b.Y;\n}\nbool operator==(const P &a, const P &b) { return abs(a - b) < EPS; }\nistream &operator>>(istream &is, P &p) {\n R x, y;\n is >> x >> y;\n p = P(x, y);\n return is;\n}\n} // namespace std\nint n, m, k;\nstruct MSQ : public G {\n MSQ() {}\n vector<P> p;\n vector<S> s;\n int m, k;\n MSQ(int m, int k) : m(m), k(k) {\n REP(i, m) p.push_back(polar((R)1, 2 * PI * i / m + PI * (R).5));\n REP(i, m) s.emplace_back(p[i], p[(i + k) % m]);\n Arrangement a(s);\n DualGraph dg(a.p);\n REP(i, a.g.size()) REP(j, a.g[i].size()) {\n int u = a.g[i][j].u;\n int v = a.g[i][j].v;\n if (u < v)\n dg.add_edge(u, v);\n }\n (G &)(*this) = dg.dual();\n reverse(this->begin(), this->end());\n }\n void copy(R r, P c, MSQ &msq) const {\n msq.resize(size());\n msq.p.resize(p.size());\n msq.s.resize(s.size());\n msq.m = m;\n msq.k = k;\n REP(i, size()) msq[i] = at(i) * r + c;\n REP(i, p.size()) msq.p[i] = p[i] * r + c;\n REP(i, s.size()) msq.s[i] = S(msq.p[i], msq.p[(i + k) % m]);\n }\n S segment(int i) const { return s[i]; }\n};\nint convex_contains(const MSQ &msq, const P &g, const P &p) {\n const int n = msq.size();\n int a = 0, b = n;\n const P pg = p - g;\n while (a + 1 < b) {\n int c = (a + b) / 2;\n if (outp(msq[a] - g, pg) > 0 && outp(msq[c] - g, pg) < 0)\n b = c;\n else\n a = c;\n }\n b %= n;\n if (outp(msq[a] - p, msq[b] - p) < -EPS)\n return 0;\n return 1;\n}\nbool check(const MSQ &temp, R r, const vector<P> &vil, int i, int j) {\n MSQ msq;\n L l = temp.segment(j);\n P gp = vil[i] - l[0] * r;\n temp.copy(r, gp, msq);\n vector<P> p;\n P b(-INF, -INF), u(+INF, +INF);\n REP(i, n) {\n L l2(vil[i], vil[i] + l.dir());\n int f = 0;\n P ll(INF, INF), rr(-INF, -INF);\n REP(j, m) {\n const S s = msq.segment(j);\n if (intersect(s, l2)) {\n if (sig(outp(s.dir(), l2.dir()))) {\n const P q = crosspoint(s, l2) - l2[0];\n p.push_back(q);\n ll = min(ll, q);\n rr = max(rr, q);\n }\n f = 1;\n }\n }\n u = min(rr, u);\n b = max(ll, b);\n if (!f)\n return false;\n }\n FOR(q, p) {\n if (*q < b || u < *q)\n continue;\n if ([&]() {\n REP(i, n) if (!convex_contains(msq, gp, vil[i] + *q)) return 0;\n return 1;\n }())\n return true;\n }\n return false;\n}\nint main() {\n ios::sync_with_stdio(false);\n while (cin >> n >> m >> k, n) {\n MSQ temp(m, k);\n vector<P> vil(n);\n REP(i, n) cin >> vil[i];\n R best = 2000;\n REP(i, n) REP(j, m) {\n if (!check(temp, best - EPS, vil, i, j))\n continue;\n R l = EPS, r = best;\n REP(itr, 40) {\n R m = (l + r) * (R).5;\n if (check(temp, m, vil, i, j))\n r = m;\n else\n l = m;\n }\n best = r;\n }\n printf(\"%.10f\\n\", (double)best);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1740, "memory_kb": 4244, "score_of_the_acc": -0.9407, "final_rank": 12 }, { "submission_id": "aoj_2203_8216176", "code_snippet": "#include <algorithm>\n#include <assert.h>\n#include <bitset>\n#include <climits>\n#include <cmath>\n#include <complex>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <ctime>\n#include <deque>\n#include <iomanip>\n#include <iostream>\n#include <iterator>\n#include <list>\n#include <map>\n#include <queue>\n#include <set>\n#include <sstream>\n#include <stack>\n#include <string>\n#include <unordered_map>\n#include <valarray>\n#include <vector>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned int uint;\ntypedef unsigned char uchar;\ntypedef unsigned long long ull;\ntypedef pair<int, int> pii;\ntypedef pair<ll, ll> pll;\ntypedef vector<int> vi;\n#define REP(i, x) for (int i = 0; i < (int)(x); i++)\n#define REPS(i, x) for (int i = 1; i <= (int)(x); i++)\n#define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--)\n#define RREPS(i, x) for (int i = ((int)(x)); i > 0; i--)\n#define FOR(i, c) \\\n for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)\n#define RFOR(i, c) \\\n for (__typeof((c).rbegin()) i = (c).rbegin(); i != (c).rend(); i++)\n#define ALL(container) (container).begin(), (container).end()\n#define RALL(container) (container).rbegin(), (container).rend()\n#define SZ(container) ((int)container.size())\n#define mp(a, b) make_pair(a, b)\n#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());\ntemplate <class T> bool chmax(T &a, const T &b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <class T> bool chmin(T &a, const T &b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <class T> ostream &operator<<(ostream &os, const vector<T> &t) {\n os << \"[\";\n FOR(it, t) {\n if (it != t.begin())\n os << \",\";\n os << *it;\n }\n os << \"]\";\n return os;\n}\ntemplate <class T> ostream &operator<<(ostream &os, const set<T> &t) {\n os << \"{\";\n FOR(it, t) {\n if (it != t.begin())\n os << \",\";\n os << *it;\n }\n os << \"}\";\n return os;\n}\ntemplate <class S, class T>\nostream &operator<<(ostream &os, const pair<S, T> &t) {\n return os << \"(\" << t.first << \",\" << t.second << \")\";\n}\ntemplate <class S, class T>\npair<S, T> operator+(const pair<S, T> &s, const pair<S, T> &t) {\n return pair<S, T>(s.first + t.first, s.second + t.second);\n}\ntemplate <class S, class T>\npair<S, T> operator-(const pair<S, T> &s, const pair<S, T> &t) {\n return pair<S, T>(s.first - t.first, s.second - t.second);\n}\nnamespace geom {\n#define X real()\n#define Y imag()\n#define at(i) ((*this)[i])\n#define SELF (*this)\nenum { TRUE = 1, FALSE = 0, BORDER = -1 };\ntypedef int BOOL;\ntypedef double R;\nconst R INF = 1e8;\nR EPS = 1e-6;\nconst R PI = 3.1415926535897932384626;\ninline int sig(const R &x) { return (abs(x) < EPS ? 0 : x > 0 ? 1 : -1); }\ninline BOOL less(const R &x, const R &y) { return sig(x - y) ? x < y : BORDER; }\ntypedef complex<R> P;\ninline R norm(const P &p) { return p.X * p.X + p.Y * p.Y; }\ninline R inp(const P &a, const P &b) { return (conj(a) * b).X; }\ninline R outp(const P &a, const P &b) { return (conj(a) * b).Y; }\ninline P unit(const P &p) { return p / abs(p); }\ninline P proj(const P &s, const P &t) { return t * inp(s, t) / norm(t); }\ninline int ccw(const P &s, const P &t, const P &p, int adv = 0) {\n int res = sig(outp(t - s, p - s));\n if (res || !adv)\n return res;\n if (sig(inp(t - s, p - s)) < 0)\n return -2;\n if (sig(inp(s - t, p - t)) < 0)\n return 2;\n return 0;\n}\nstruct L : public vector<P> {\n L(const P &p1, const P &p2) {\n this->push_back(p1);\n this->push_back(p2);\n }\n L() {}\n P dir() const { return at(1) - at(0); }\n BOOL online(const P &p) const { return !sig(outp(p - at(0), dir())); }\n};\nstruct S : public L {\n S(const P &p1, const P &p2) : L(p1, p2) {}\n S() {}\n BOOL online(const P &p) const {\n if (!sig(norm(p - at(0))) || !sig(norm(p - at(1))))\n return BORDER;\n return !sig(outp(p - at(0), dir())) && inp(p - at(0), dir()) > -EPS &&\n inp(p - at(1), -dir()) > -EPS\n ? TRUE\n : FALSE;\n return !sig(abs(at(0) - p) + abs(at(1) - p) - abs(at(0) - at(1)));\n }\n};\nP crosspoint(const L &l, const L &m);\nstruct G : public vector<P> {\n G(size_type size = 0) : vector(size) {}\n S edge(int i) const { return S(at(i), at(i + 1 == size() ? 0 : i + 1)); }\n};\ninline BOOL intersect(const S &s, const L &l) {\n return (sig(outp(l.dir(), s[0] - l[0])) * sig(outp(l.dir(), s[1] - l[0])) <=\n 0);\n}\ninline P crosspoint(const L &l, const L &m) {\n R A = outp(l.dir(), m.dir()), B = outp(l.dir(), l[1] - m[0]);\n if (!sig(abs(A)) && !sig(abs(B)))\n return m[0];\n if (abs(A) < EPS)\n assert(false);\n return m[0] + B / A * (m[1] - m[0]);\n}\nstruct Arrangement {\n struct AEdge {\n int u, v, t;\n R cost;\n AEdge(int u = 0, int v = 0, int t = 0, R cost = 0)\n : u(u), v(v), t(t), cost(cost) {}\n };\n typedef vector<vector<AEdge>> AGraph;\n vector<P> p;\n AGraph g;\n Arrangement() {}\n Arrangement(vector<S> seg) {\n int m = seg.size();\n REP(i, m) {\n p.push_back(seg[i][0]);\n p.push_back(seg[i][1]);\n REP(j, i)\n if (sig(outp(seg[i].dir(), seg[j].dir())) &&\n intersect(seg[i], seg[j]) == TRUE)\n p.push_back(crosspoint(seg[i], seg[j]));\n }\n sort(ALL(p));\n UNIQUE(p);\n int n = p.size();\n g.resize(n);\n REP(i, m) {\n S &s = seg[i];\n vector<pair<R, int>> ps;\n REP(j, n) if (s.online(p[j])) ps.emplace_back(norm(p[j] - s[0]), j);\n sort(ALL(ps));\n REP(j, (int)ps.size() - 1) {\n const int u = ps[j].second;\n const int v = ps[j + 1].second;\n g[u].emplace_back(u, v, 0, abs(p[u] - p[v]));\n g[v].emplace_back(v, u, 0, abs(p[u] - p[v]));\n }\n }\n }\n int getIdx(P q) {\n auto it = lower_bound(ALL(p), q);\n if (it == p.end() || *it != q)\n return -1;\n return it - p.begin();\n }\n};\nstruct DualGraph {\n struct DEdge {\n int u, v, f, l;\n R a;\n DEdge(int u, int v, R a) : u(u), v(v), f(0), l(0) {\n while (PI < a)\n a -= 2 * PI;\n while (a < -PI)\n a += 2 * PI;\n this->a = a;\n }\n bool operator==(const DEdge &opp) const { return v == opp.v; }\n bool operator<(const DEdge &opp) const { return a > opp.a; }\n bool operator<(const R &opp) const { return a > opp; }\n friend ostream &operator<<(ostream &os, const DEdge &t) {\n return os << \"(\" << t.u << \",\" << t.v << \",\" << t.a * 180 / PI << \")\";\n }\n };\n int n;\n vector<P> p;\n vector<vector<DEdge>> g;\n DualGraph(const vector<P> &p) : p(p), g(p.size()), n(p.size()) {}\n void add_edge(const int s, const int t) {\n R a = arg(p[t] - p[s]);\n g[s].emplace_back(s, t, a);\n g[t].emplace_back(t, s, a > 0 ? a - PI : a + PI);\n }\n void add_polygon(int s, G &t, R a) {\n auto e = lower_bound(ALL(g[s]), a - EPS);\n if (e == g[s].end())\n e = g[s].begin();\n if (e->f)\n return;\n e->f = 1;\n t.push_back(p[s]);\n add_polygon(e->v, t, e->a > 0 ? e->a - PI : e->a + PI);\n }\n G dual() {\n REP(i, n) {\n sort(ALL(g[i]));\n UNIQUE(g[i]);\n }\n int s = min_element(ALL(p)) - p.begin();\n G poly;\n add_polygon(s, poly, -PI * (R).5);\n return poly;\n }\n};\n#undef SELF\n#undef at\n} // namespace geom\nusing namespace geom;\nnamespace std {\nbool operator<(const P &a, const P &b) {\n return sig(a.X - b.X) ? a.X < b.X : a.Y + EPS < b.Y;\n}\nbool operator==(const P &a, const P &b) { return abs(a - b) < EPS; }\nistream &operator>>(istream &is, P &p) {\n R x, y;\n is >> x >> y;\n p = P(x, y);\n return is;\n}\n} // namespace std\nint n, m, k;\nstruct MSQ : public G {\n MSQ() {}\n vector<P> p;\n vector<S> s;\n int m, k;\n MSQ(int m, int k) : m(m), k(k) {\n REP(i, m) p.push_back(polar((R)1, 2 * PI * i / m + PI * (R).5));\n REP(i, m) s.emplace_back(p[i], p[(i + k) % m]);\n Arrangement a(s);\n DualGraph dg(a.p);\n REP(i, a.g.size()) REP(j, a.g[i].size()) {\n int u = a.g[i][j].u;\n int v = a.g[i][j].v;\n if (u < v)\n dg.add_edge(u, v);\n }\n (G &)(*this) = dg.dual();\n reverse(this->begin(), this->end());\n }\n void copy(R r, P c, MSQ &msq) const {\n msq.resize(size());\n msq.p.resize(p.size());\n msq.s.resize(s.size());\n msq.m = m;\n msq.k = k;\n REP(i, size()) msq[i] = at(i) * r + c;\n REP(i, p.size()) msq.p[i] = p[i] * r + c;\n REP(i, s.size()) msq.s[i] = S(msq.p[i], msq.p[(i + k) % m]);\n }\n S segment(int i) const { return s[i]; }\n};\nint convex_contains(const MSQ &msq, const P &g, const P &p) {\n const int n = msq.size();\n int a = 0, b = n;\n const P pg = p - g;\n while (a + 1 < b) {\n int c = (a + b) / 2;\n if (outp(msq[a] - g, pg) > 0 && outp(msq[c] - g, pg) < 0)\n b = c;\n else\n a = c;\n }\n b %= n;\n if (outp(msq[a] - p, msq[b] - p) < -EPS)\n return 0;\n return 1;\n}\nbool check(const MSQ &temp, R r, const vector<P> &shoolack, int i, int j) {\n MSQ msq;\n L l = temp.segment(j);\n P gp = shoolack[i] - l[0] * r;\n temp.copy(r, gp, msq);\n vector<P> p;\n P b(-INF, -INF), u(+INF, +INF);\n REP(i, n) {\n L l2(shoolack[i], shoolack[i] + l.dir());\n int f = 0;\n P ll(INF, INF), rr(-INF, -INF);\n REP(j, m) {\n const S s = msq.segment(j);\n if (intersect(s, l2)) {\n if (sig(outp(s.dir(), l2.dir()))) {\n const P q = crosspoint(s, l2) - l2[0];\n p.push_back(q);\n ll = min(ll, q);\n rr = max(rr, q);\n }\n f = 1;\n }\n }\n u = min(rr, u);\n b = max(ll, b);\n if (!f)\n return false;\n }\n FOR(q, p) {\n if (*q < b || u < *q)\n continue;\n if ([&]() {\n REP(i, n) if (!convex_contains(msq, gp, shoolack[i] + *q)) return 0;\n return 1;\n }())\n return true;\n }\n return false;\n}\nint main() {\n ios::sync_with_stdio(false);\n while (cin >> n >> m >> k, n) {\n MSQ temp(m, k);\n vector<P> shoolack(n);\n REP(i, n) cin >> shoolack[i];\n R best = 2000;\n REP(i, n) REP(j, m) {\n if (!check(temp, best - EPS, shoolack, i, j))\n continue;\n R l = EPS, r = best;\n REP(itr, 50) {\n R m = (l + r) * (R).5;\n if (check(temp, m, shoolack, i, j))\n r = m;\n else\n l = m;\n }\n best = r;\n }\n printf(\"%.10f\\n\", (double)best);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1840, "memory_kb": 4432, "score_of_the_acc": -1.0161, "final_rank": 17 }, { "submission_id": "aoj_2203_8113290", "code_snippet": "#include <algorithm>\n#include <assert.h>\n#include <bitset>\n#include <climits>\n#include <cmath>\n#include <complex>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <ctime>\n#include <deque>\n#include <iomanip>\n#include <iostream>\n#include <iterator>\n#include <list>\n#include <map>\n#include <queue>\n#include <set>\n#include <sstream>\n#include <stack>\n#include <string>\n#include <unordered_map>\n#include <valarray>\n#include <vector>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned int uint;\ntypedef unsigned char uchar;\ntypedef unsigned long long ull;\ntypedef pair<int, int> pii;\ntypedef pair<ll, ll> pll;\ntypedef vector<int> vi;\n#define REP(i, x) for (int i = 0; i < (int)(x); i++)\n#define REPS(i, x) for (int i = 1; i <= (int)(x); i++)\n#define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--)\n#define RREPS(i, x) for (int i = ((int)(x)); i > 0; i--)\n#define FOR(i, c) \\\n for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)\n#define RFOR(i, c) \\\n for (__typeof((c).rbegin()) i = (c).rbegin(); i != (c).rend(); i++)\n#define ALL(container) (container).begin(), (container).end()\n#define RALL(container) (container).rbegin(), (container).rend()\n#define SZ(container) ((int)container.size())\n#define mp(a, b) make_pair(a, b)\n#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());\ntemplate <class T> bool chmax(T &a, const T &b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <class T> bool chmin(T &a, const T &b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <class T> ostream &operator<<(ostream &os, const vector<T> &t) {\n os << \"[\";\n FOR(it, t) {\n if (it != t.begin())\n os << \",\";\n os << *it;\n }\n os << \"]\";\n return os;\n}\ntemplate <class T> ostream &operator<<(ostream &os, const set<T> &t) {\n os << \"{\";\n FOR(it, t) {\n if (it != t.begin())\n os << \",\";\n os << *it;\n }\n os << \"}\";\n return os;\n}\ntemplate <class S, class T>\nostream &operator<<(ostream &os, const pair<S, T> &t) {\n return os << \"(\" << t.first << \",\" << t.second << \")\";\n}\ntemplate <class S, class T>\npair<S, T> operator+(const pair<S, T> &s, const pair<S, T> &t) {\n return pair<S, T>(s.first + t.first, s.second + t.second);\n}\ntemplate <class S, class T>\npair<S, T> operator-(const pair<S, T> &s, const pair<S, T> &t) {\n return pair<S, T>(s.first - t.first, s.second - t.second);\n}\nnamespace geom {\n#define X real()\n#define Y imag()\n#define at(i) ((*this)[i])\n#define SELF (*this)\nenum { TRUE = 1, FALSE = 0, BORDER = -1 };\ntypedef int BOOL;\ntypedef double R;\nconst R INF = 1e8;\nR EPS = 1e-6;\nconst R PI = 3.1415926535897932384626;\ninline int sig(const R &x) { return (abs(x) < EPS ? 0 : x > 0 ? 1 : -1); }\ninline BOOL less(const R &x, const R &y) { return sig(x - y) ? x < y : BORDER; }\ntypedef complex<R> P;\ninline R norm(const P &p) { return p.X * p.X + p.Y * p.Y; }\ninline R inp(const P &a, const P &b) { return (conj(a) * b).X; }\ninline R outp(const P &a, const P &b) { return (conj(a) * b).Y; }\ninline P unit(const P &p) { return p / abs(p); }\ninline P proj(const P &s, const P &t) { return t * inp(s, t) / norm(t); }\ninline int ccw(const P &s, const P &t, const P &p, int adv = 0) {\n int res = sig(outp(t - s, p - s));\n if (res || !adv)\n return res;\n if (sig(inp(t - s, p - s)) < 0)\n return -2;\n if (sig(inp(s - t, p - t)) < 0)\n return 2;\n return 0;\n}\nstruct L : public vector<P> {\n L(const P &p1, const P &p2) {\n this->push_back(p1);\n this->push_back(p2);\n }\n L() {}\n P dir() const { return at(1) - at(0); }\n BOOL online(const P &p) const { return !sig(outp(p - at(0), dir())); }\n};\nstruct S : public L {\n S(const P &p1, const P &p2) : L(p1, p2) {}\n S() {}\n BOOL online(const P &p) const {\n if (!sig(norm(p - at(0))) || !sig(norm(p - at(1))))\n return BORDER;\n return !sig(outp(p - at(0), dir())) && inp(p - at(0), dir()) > -EPS &&\n inp(p - at(1), -dir()) > -EPS\n ? TRUE\n : FALSE;\n return !sig(abs(at(0) - p) + abs(at(1) - p) - abs(at(0) - at(1)));\n }\n};\nP crosspoint(const L &l, const L &m);\nstruct G : public vector<P> {\n G(size_type size = 0) : vector(size) {}\n S edge(int i) const { return S(at(i), at(i + 1 == size() ? 0 : i + 1)); }\n};\ninline BOOL intersect(const S &s, const L &l) {\n return (sig(outp(l.dir(), s[0] - l[0])) * sig(outp(l.dir(), s[1] - l[0])) <=\n 0);\n}\ninline P crosspoint(const L &l, const L &m) {\n R A = outp(l.dir(), m.dir()), B = outp(l.dir(), l[1] - m[0]);\n if (!sig(abs(A)) && !sig(abs(B)))\n return m[0];\n if (abs(A) < EPS)\n assert(false);\n return m[0] + B / A * (m[1] - m[0]);\n}\nstruct Arrangement {\n struct AEdge {\n int u, v, t;\n R cost;\n AEdge(int u = 0, int v = 0, int t = 0, R cost = 0)\n : u(u), v(v), t(t), cost(cost) {}\n };\n typedef vector<vector<AEdge>> AGraph;\n vector<P> p;\n AGraph g;\n Arrangement() {}\n Arrangement(vector<S> seg) {\n int m = seg.size();\n REP(i, m) {\n p.push_back(seg[i][0]);\n p.push_back(seg[i][1]);\n REP(j, i)\n if (sig(outp(seg[i].dir(), seg[j].dir())) &&\n intersect(seg[i], seg[j]) == TRUE)\n p.push_back(crosspoint(seg[i], seg[j]));\n }\n sort(ALL(p));\n UNIQUE(p);\n int n = p.size();\n g.resize(n);\n REP(i, m) {\n S &s = seg[i];\n vector<pair<R, int>> ps;\n REP(j, n) if (s.online(p[j])) ps.emplace_back(norm(p[j] - s[0]), j);\n sort(ALL(ps));\n REP(j, (int)ps.size() - 1) {\n const int u = ps[j].second;\n const int v = ps[j + 1].second;\n g[u].emplace_back(u, v, 0, abs(p[u] - p[v]));\n g[v].emplace_back(v, u, 0, abs(p[u] - p[v]));\n }\n }\n }\n int getIdx(P q) {\n auto it = lower_bound(ALL(p), q);\n if (it == p.end() || *it != q)\n return -1;\n return it - p.begin();\n }\n};\nstruct DualGraph {\n struct DEdge {\n int u, v, f, l;\n R a;\n DEdge(int u, int v, R a) : u(u), v(v), f(0), l(0) {\n while (PI < a)\n a -= 2 * PI;\n while (a < -PI)\n a += 2 * PI;\n this->a = a;\n }\n bool operator==(const DEdge &opp) const { return v == opp.v; }\n bool operator<(const DEdge &opp) const { return a > opp.a; }\n bool operator<(const R &opp) const { return a > opp; }\n friend ostream &operator<<(ostream &os, const DEdge &t) {\n return os << \"(\" << t.u << \",\" << t.v << \",\" << t.a * 180 / PI << \")\";\n }\n };\n int n;\n vector<P> p;\n vector<vector<DEdge>> g;\n DualGraph(const vector<P> &p) : p(p), g(p.size()), n(p.size()) {}\n void add_edge(const int s, const int t) {\n R a = arg(p[t] - p[s]);\n g[s].emplace_back(s, t, a);\n g[t].emplace_back(t, s, a > 0 ? a - PI : a + PI);\n }\n void add_polygon(int s, G &t, R a) {\n auto e = lower_bound(ALL(g[s]), a - EPS);\n if (e == g[s].end())\n e = g[s].begin();\n if (e->f)\n return;\n e->f = 1;\n t.push_back(p[s]);\n add_polygon(e->v, t, e->a > 0 ? e->a - PI : e->a + PI);\n }\n G dual() {\n REP(i, n) {\n sort(ALL(g[i]));\n UNIQUE(g[i]);\n }\n int s = min_element(ALL(p)) - p.begin();\n G poly;\n add_polygon(s, poly, -PI * (R).5);\n return poly;\n }\n};\n#undef SELF\n#undef at\n} // namespace geom\nusing namespace geom;\nnamespace std {\nbool operator<(const P &a, const P &b) {\n return sig(a.X - b.X) ? a.X < b.X : a.Y + EPS < b.Y;\n}\nbool operator==(const P &a, const P &b) { return abs(a - b) < EPS; }\nistream &operator>>(istream &is, P &p) {\n R x, y;\n is >> x >> y;\n p = P(x, y);\n return is;\n}\n} // namespace std\nint n, m, k;\nstruct MSQ : public G {\n MSQ() {}\n vector<P> p;\n vector<S> s;\n int m, k;\n MSQ(int m, int k) : m(m), k(k) {\n REP(i, m) p.push_back(polar((R)1, 2 * PI * i / m + PI * (R).5));\n REP(i, m) s.emplace_back(p[i], p[(i + k) % m]);\n Arrangement a(s);\n DualGraph dg(a.p);\n REP(i, a.g.size()) REP(j, a.g[i].size()) {\n int u = a.g[i][j].u;\n int v = a.g[i][j].v;\n if (u < v)\n dg.add_edge(u, v);\n }\n (G &)(*this) = dg.dual();\n reverse(this->begin(), this->end());\n }\n void copy(R r, P c, MSQ &msq) const {\n msq.resize(size());\n msq.p.resize(p.size());\n msq.s.resize(s.size());\n msq.m = m;\n msq.k = k;\n REP(i, size()) msq[i] = at(i) * r + c;\n REP(i, p.size()) msq.p[i] = p[i] * r + c;\n REP(i, s.size()) msq.s[i] = S(msq.p[i], msq.p[(i + k) % m]);\n }\n S segment(int i) const { return s[i]; }\n};\nint convex_contains(const MSQ &msq, const P &g, const P &p) {\n const int n = msq.size();\n int a = 0, b = n;\n const P pg = p - g;\n while (a + 1 < b) {\n int c = (a + b) / 2;\n if (outp(msq[a] - g, pg) > 0 && outp(msq[c] - g, pg) < 0)\n b = c;\n else\n a = c;\n }\n b %= n;\n if (outp(msq[a] - p, msq[b] - p) < -EPS)\n return 0;\n return 1;\n}\nbool check(const MSQ &temp, R r, const vector<P> &shoolack, int i, int j) {\n MSQ msq;\n L l = temp.segment(j);\n P gp = shoolack[i] - l[0] * r;\n temp.copy(r, gp, msq);\n vector<P> p;\n P b(-INF, -INF), u(+INF, +INF);\n REP(i, n) {\n L l2(shoolack[i], shoolack[i] + l.dir());\n int f = 0;\n P ll(INF, INF), rr(-INF, -INF);\n REP(j, m) {\n const S s = msq.segment(j);\n if (intersect(s, l2)) {\n if (sig(outp(s.dir(), l2.dir()))) {\n const P q = crosspoint(s, l2) - l2[0];\n p.push_back(q);\n ll = min(ll, q);\n rr = max(rr, q);\n }\n f = 1;\n }\n }\n u = min(rr, u);\n b = max(ll, b);\n if (!f)\n return false;\n }\n FOR(q, p) {\n if (*q < b || u < *q)\n continue;\n if ([&]() {\n REP(i, n) if (!convex_contains(msq, gp, shoolack[i] + *q)) return 0;\n return 1;\n }())\n return true;\n }\n return false;\n}\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(0);\n ios::sync_with_stdio(false);\n while (cin >> n >> m >> k, n) {\n MSQ temp(m, k);\n vector<P> shoolack(n);\n REP(i, n) cin >> shoolack[i];\n R best = 2000;\n REP(i, n) REP(j, m) {\n if (!check(temp, best - EPS, shoolack, i, j))\n continue;\n R l = EPS, r = best;\n REP(itr, 50) {\n R m = (l + r) * (R).5;\n if (check(temp, m, shoolack, i, j))\n r = m;\n else\n l = m;\n }\n best = r;\n }\n printf(\"%.10f\\n\", (double)best);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1800, "memory_kb": 4416, "score_of_the_acc": -1.0046, "final_rank": 15 }, { "submission_id": "aoj_2203_8113282", "code_snippet": "#pragma GCC optimize(\"Ofast\")\n#pragma GCC optimize(\"unroll-loops\")\n#include <algorithm>\n#include <assert.h>\n#include <bitset>\n#include <climits>\n#include <cmath>\n#include <complex>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <ctime>\n#include <deque>\n#include <iomanip>\n#include <iostream>\n#include <iterator>\n#include <list>\n#include <map>\n#include <queue>\n#include <set>\n#include <sstream>\n#include <stack>\n#include <string>\n#include <unordered_map>\n#include <valarray>\n#include <vector>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned int uint;\ntypedef unsigned char uchar;\ntypedef unsigned long long ull;\ntypedef pair<int, int> pii;\ntypedef pair<ll, ll> pll;\ntypedef vector<int> vi;\n#define REP(i, x) for (int i = 0; i < (int)(x); i++)\n#define REPS(i, x) for (int i = 1; i <= (int)(x); i++)\n#define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--)\n#define RREPS(i, x) for (int i = ((int)(x)); i > 0; i--)\n#define FOR(i, c) \\\n for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)\n#define RFOR(i, c) \\\n for (__typeof((c).rbegin()) i = (c).rbegin(); i != (c).rend(); i++)\n#define ALL(container) (container).begin(), (container).end()\n#define RALL(container) (container).rbegin(), (container).rend()\n#define SZ(container) ((int)container.size())\n#define mp(a, b) make_pair(a, b)\n#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());\ntemplate <class T> bool chmax(T &a, const T &b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <class T> bool chmin(T &a, const T &b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <class T> ostream &operator<<(ostream &os, const vector<T> &t) {\n os << \"[\";\n FOR(it, t) {\n if (it != t.begin())\n os << \",\";\n os << *it;\n }\n os << \"]\";\n return os;\n}\ntemplate <class T> ostream &operator<<(ostream &os, const set<T> &t) {\n os << \"{\";\n FOR(it, t) {\n if (it != t.begin())\n os << \",\";\n os << *it;\n }\n os << \"}\";\n return os;\n}\ntemplate <class S, class T>\nostream &operator<<(ostream &os, const pair<S, T> &t) {\n return os << \"(\" << t.first << \",\" << t.second << \")\";\n}\ntemplate <class S, class T>\npair<S, T> operator+(const pair<S, T> &s, const pair<S, T> &t) {\n return pair<S, T>(s.first + t.first, s.second + t.second);\n}\ntemplate <class S, class T>\npair<S, T> operator-(const pair<S, T> &s, const pair<S, T> &t) {\n return pair<S, T>(s.first - t.first, s.second - t.second);\n}\nnamespace geom {\n#define X real()\n#define Y imag()\n#define at(i) ((*this)[i])\n#define SELF (*this)\nenum { TRUE = 1, FALSE = 0, BORDER = -1 };\ntypedef int BOOL;\ntypedef double R;\nconst R INF = 1e8;\nR EPS = 1e-6;\nconst R PI = 3.1415926535897932384626;\ninline int sig(const R &x) { return (abs(x) < EPS ? 0 : x > 0 ? 1 : -1); }\ninline BOOL less(const R &x, const R &y) { return sig(x - y) ? x < y : BORDER; }\ntypedef complex<R> P;\ninline R norm(const P &p) { return p.X * p.X + p.Y * p.Y; }\ninline R inp(const P &a, const P &b) { return (conj(a) * b).X; }\ninline R outp(const P &a, const P &b) { return (conj(a) * b).Y; }\ninline P unit(const P &p) { return p / abs(p); }\ninline P proj(const P &s, const P &t) { return t * inp(s, t) / norm(t); }\ninline int ccw(const P &s, const P &t, const P &p, int adv = 0) {\n int res = sig(outp(t - s, p - s));\n if (res || !adv)\n return res;\n if (sig(inp(t - s, p - s)) < 0)\n return -2;\n if (sig(inp(s - t, p - t)) < 0)\n return 2;\n return 0;\n}\nstruct L : public vector<P> {\n L(const P &p1, const P &p2) {\n this->push_back(p1);\n this->push_back(p2);\n }\n L() {}\n P dir() const { return at(1) - at(0); }\n BOOL online(const P &p) const { return !sig(outp(p - at(0), dir())); }\n};\nstruct S : public L {\n S(const P &p1, const P &p2) : L(p1, p2) {}\n S() {}\n BOOL online(const P &p) const {\n if (!sig(norm(p - at(0))) || !sig(norm(p - at(1))))\n return BORDER;\n return !sig(outp(p - at(0), dir())) && inp(p - at(0), dir()) > -EPS &&\n inp(p - at(1), -dir()) > -EPS\n ? TRUE\n : FALSE;\n return !sig(abs(at(0) - p) + abs(at(1) - p) - abs(at(0) - at(1)));\n }\n};\nP crosspoint(const L &l, const L &m);\nstruct G : public vector<P> {\n G(size_type size = 0) : vector(size) {}\n S edge(int i) const { return S(at(i), at(i + 1 == size() ? 0 : i + 1)); }\n};\ninline BOOL intersect(const S &s, const L &l) {\n return (sig(outp(l.dir(), s[0] - l[0])) * sig(outp(l.dir(), s[1] - l[0])) <=\n 0);\n}\ninline P crosspoint(const L &l, const L &m) {\n R A = outp(l.dir(), m.dir()), B = outp(l.dir(), l[1] - m[0]);\n if (!sig(abs(A)) && !sig(abs(B)))\n return m[0];\n if (abs(A) < EPS)\n assert(false);\n return m[0] + B / A * (m[1] - m[0]);\n}\nstruct Arrangement {\n struct AEdge {\n int u, v, t;\n R cost;\n AEdge(int u = 0, int v = 0, int t = 0, R cost = 0)\n : u(u), v(v), t(t), cost(cost) {}\n };\n typedef vector<vector<AEdge>> AGraph;\n vector<P> p;\n AGraph g;\n Arrangement() {}\n Arrangement(vector<S> seg) {\n int m = seg.size();\n REP(i, m) {\n p.push_back(seg[i][0]);\n p.push_back(seg[i][1]);\n REP(j, i)\n if (sig(outp(seg[i].dir(), seg[j].dir())) &&\n intersect(seg[i], seg[j]) == TRUE)\n p.push_back(crosspoint(seg[i], seg[j]));\n }\n sort(ALL(p));\n UNIQUE(p);\n int n = p.size();\n g.resize(n);\n REP(i, m) {\n S &s = seg[i];\n vector<pair<R, int>> ps;\n REP(j, n) if (s.online(p[j])) ps.emplace_back(norm(p[j] - s[0]), j);\n sort(ALL(ps));\n REP(j, (int)ps.size() - 1) {\n const int u = ps[j].second;\n const int v = ps[j + 1].second;\n g[u].emplace_back(u, v, 0, abs(p[u] - p[v]));\n g[v].emplace_back(v, u, 0, abs(p[u] - p[v]));\n }\n }\n }\n int getIdx(P q) {\n auto it = lower_bound(ALL(p), q);\n if (it == p.end() || *it != q)\n return -1;\n return it - p.begin();\n }\n};\nstruct DualGraph {\n struct DEdge {\n int u, v, f, l;\n R a;\n DEdge(int u, int v, R a) : u(u), v(v), f(0), l(0) {\n while (PI < a)\n a -= 2 * PI;\n while (a < -PI)\n a += 2 * PI;\n this->a = a;\n }\n bool operator==(const DEdge &opp) const { return v == opp.v; }\n bool operator<(const DEdge &opp) const { return a > opp.a; }\n bool operator<(const R &opp) const { return a > opp; }\n friend ostream &operator<<(ostream &os, const DEdge &t) {\n return os << \"(\" << t.u << \",\" << t.v << \",\" << t.a * 180 / PI << \")\";\n }\n };\n int n;\n vector<P> p;\n vector<vector<DEdge>> g;\n DualGraph(const vector<P> &p) : p(p), g(p.size()), n(p.size()) {}\n void add_edge(const int s, const int t) {\n R a = arg(p[t] - p[s]);\n g[s].emplace_back(s, t, a);\n g[t].emplace_back(t, s, a > 0 ? a - PI : a + PI);\n }\n void add_polygon(int s, G &t, R a) {\n auto e = lower_bound(ALL(g[s]), a - EPS);\n if (e == g[s].end())\n e = g[s].begin();\n if (e->f)\n return;\n e->f = 1;\n t.push_back(p[s]);\n add_polygon(e->v, t, e->a > 0 ? e->a - PI : e->a + PI);\n }\n G dual() {\n REP(i, n) {\n sort(ALL(g[i]));\n UNIQUE(g[i]);\n }\n int s = min_element(ALL(p)) - p.begin();\n G poly;\n add_polygon(s, poly, -PI * (R).5);\n return poly;\n }\n};\n#undef SELF\n#undef at\n} // namespace geom\nusing namespace geom;\nnamespace std {\nbool operator<(const P &a, const P &b) {\n return sig(a.X - b.X) ? a.X < b.X : a.Y + EPS < b.Y;\n}\nbool operator==(const P &a, const P &b) { return abs(a - b) < EPS; }\nistream &operator>>(istream &is, P &p) {\n R x, y;\n is >> x >> y;\n p = P(x, y);\n return is;\n}\n} // namespace std\nint n, m, k;\nstruct MSQ : public G {\n MSQ() {}\n vector<P> p;\n vector<S> s;\n int m, k;\n MSQ(int m, int k) : m(m), k(k) {\n REP(i, m) p.push_back(polar((R)1, 2 * PI * i / m + PI * (R).5));\n REP(i, m) s.emplace_back(p[i], p[(i + k) % m]);\n Arrangement a(s);\n DualGraph dg(a.p);\n REP(i, a.g.size()) REP(j, a.g[i].size()) {\n int u = a.g[i][j].u;\n int v = a.g[i][j].v;\n if (u < v)\n dg.add_edge(u, v);\n }\n (G &)(*this) = dg.dual();\n reverse(this->begin(), this->end());\n }\n void copy(R r, P c, MSQ &msq) const {\n msq.resize(size());\n msq.p.resize(p.size());\n msq.s.resize(s.size());\n msq.m = m;\n msq.k = k;\n REP(i, size()) msq[i] = at(i) * r + c;\n REP(i, p.size()) msq.p[i] = p[i] * r + c;\n REP(i, s.size()) msq.s[i] = S(msq.p[i], msq.p[(i + k) % m]);\n }\n S segment(int i) const { return s[i]; }\n};\nint convex_contains(const MSQ &msq, const P &g, const P &p) {\n const int n = msq.size();\n int a = 0, b = n;\n const P pg = p - g;\n while (a + 1 < b) {\n int c = (a + b) / 2;\n if (outp(msq[a] - g, pg) > 0 && outp(msq[c] - g, pg) < 0)\n b = c;\n else\n a = c;\n }\n b %= n;\n if (outp(msq[a] - p, msq[b] - p) < -EPS)\n return 0;\n return 1;\n}\nbool check(const MSQ &temp, R r, const vector<P> &shoolack, int i, int j) {\n MSQ msq;\n L l = temp.segment(j);\n P gp = shoolack[i] - l[0] * r;\n temp.copy(r, gp, msq);\n vector<P> p;\n P b(-INF, -INF), u(+INF, +INF);\n REP(i, n) {\n L l2(shoolack[i], shoolack[i] + l.dir());\n int f = 0;\n P ll(INF, INF), rr(-INF, -INF);\n REP(j, m) {\n const S s = msq.segment(j);\n if (intersect(s, l2)) {\n if (sig(outp(s.dir(), l2.dir()))) {\n const P q = crosspoint(s, l2) - l2[0];\n p.push_back(q);\n ll = min(ll, q);\n rr = max(rr, q);\n }\n f = 1;\n }\n }\n u = min(rr, u);\n b = max(ll, b);\n if (!f)\n return false;\n }\n FOR(q, p) {\n if (*q < b || u < *q)\n continue;\n if ([&]() {\n REP(i, n) if (!convex_contains(msq, gp, shoolack[i] + *q)) return 0;\n return 1;\n }())\n return true;\n }\n return false;\n}\nint main() {\n ios::sync_with_stdio(false);\n while (cin >> n >> m >> k, n) {\n MSQ temp(m, k);\n vector<P> shoolack(n);\n REP(i, n) cin >> shoolack[i];\n R best = 2000;\n REP(i, n) REP(j, m) {\n if (!check(temp, best - EPS, shoolack, i, j))\n continue;\n R l = EPS, r = best;\n REP(itr, 50) {\n R m = (l + r) * (R).5;\n if (check(temp, m, shoolack, i, j))\n r = m;\n else\n l = m;\n }\n best = r;\n }\n printf(\"%.10f\\n\", (double)best);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 2000, "memory_kb": 4216, "score_of_the_acc": -0.9736, "final_rank": 13 }, { "submission_id": "aoj_2203_8113281", "code_snippet": "#pragma GCC optimize(\"O3\")\n#pragma GCC target(\"sse4\")\n#include <algorithm>\n#include <assert.h>\n#include <bitset>\n#include <climits>\n#include <cmath>\n#include <complex>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <ctime>\n#include <deque>\n#include <iomanip>\n#include <iostream>\n#include <iterator>\n#include <list>\n#include <map>\n#include <queue>\n#include <set>\n#include <sstream>\n#include <stack>\n#include <string>\n#include <unordered_map>\n#include <valarray>\n#include <vector>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned int uint;\ntypedef unsigned char uchar;\ntypedef unsigned long long ull;\ntypedef pair<int, int> pii;\ntypedef pair<ll, ll> pll;\ntypedef vector<int> vi;\n#define REP(i, x) for (int i = 0; i < (int)(x); i++)\n#define REPS(i, x) for (int i = 1; i <= (int)(x); i++)\n#define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--)\n#define RREPS(i, x) for (int i = ((int)(x)); i > 0; i--)\n#define FOR(i, c) \\\n for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)\n#define RFOR(i, c) \\\n for (__typeof((c).rbegin()) i = (c).rbegin(); i != (c).rend(); i++)\n#define ALL(container) (container).begin(), (container).end()\n#define RALL(container) (container).rbegin(), (container).rend()\n#define SZ(container) ((int)container.size())\n#define mp(a, b) make_pair(a, b)\n#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());\ntemplate <class T> bool chmax(T &a, const T &b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <class T> bool chmin(T &a, const T &b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <class T> ostream &operator<<(ostream &os, const vector<T> &t) {\n os << \"[\";\n FOR(it, t) {\n if (it != t.begin())\n os << \",\";\n os << *it;\n }\n os << \"]\";\n return os;\n}\ntemplate <class T> ostream &operator<<(ostream &os, const set<T> &t) {\n os << \"{\";\n FOR(it, t) {\n if (it != t.begin())\n os << \",\";\n os << *it;\n }\n os << \"}\";\n return os;\n}\ntemplate <class S, class T>\nostream &operator<<(ostream &os, const pair<S, T> &t) {\n return os << \"(\" << t.first << \",\" << t.second << \")\";\n}\ntemplate <class S, class T>\npair<S, T> operator+(const pair<S, T> &s, const pair<S, T> &t) {\n return pair<S, T>(s.first + t.first, s.second + t.second);\n}\ntemplate <class S, class T>\npair<S, T> operator-(const pair<S, T> &s, const pair<S, T> &t) {\n return pair<S, T>(s.first - t.first, s.second - t.second);\n}\nnamespace geom {\n#define X real()\n#define Y imag()\n#define at(i) ((*this)[i])\n#define SELF (*this)\nenum { TRUE = 1, FALSE = 0, BORDER = -1 };\ntypedef int BOOL;\ntypedef double R;\nconst R INF = 1e8;\nR EPS = 1e-6;\nconst R PI = 3.1415926535897932384626;\ninline int sig(const R &x) { return (abs(x) < EPS ? 0 : x > 0 ? 1 : -1); }\ninline BOOL less(const R &x, const R &y) { return sig(x - y) ? x < y : BORDER; }\ntypedef complex<R> P;\ninline R norm(const P &p) { return p.X * p.X + p.Y * p.Y; }\ninline R inp(const P &a, const P &b) { return (conj(a) * b).X; }\ninline R outp(const P &a, const P &b) { return (conj(a) * b).Y; }\ninline P unit(const P &p) { return p / abs(p); }\ninline P proj(const P &s, const P &t) { return t * inp(s, t) / norm(t); }\ninline int ccw(const P &s, const P &t, const P &p, int adv = 0) {\n int res = sig(outp(t - s, p - s));\n if (res || !adv)\n return res;\n if (sig(inp(t - s, p - s)) < 0)\n return -2;\n if (sig(inp(s - t, p - t)) < 0)\n return 2;\n return 0;\n}\nstruct L : public vector<P> {\n L(const P &p1, const P &p2) {\n this->push_back(p1);\n this->push_back(p2);\n }\n L() {}\n P dir() const { return at(1) - at(0); }\n BOOL online(const P &p) const { return !sig(outp(p - at(0), dir())); }\n};\nstruct S : public L {\n S(const P &p1, const P &p2) : L(p1, p2) {}\n S() {}\n BOOL online(const P &p) const {\n if (!sig(norm(p - at(0))) || !sig(norm(p - at(1))))\n return BORDER;\n return !sig(outp(p - at(0), dir())) && inp(p - at(0), dir()) > -EPS &&\n inp(p - at(1), -dir()) > -EPS\n ? TRUE\n : FALSE;\n return !sig(abs(at(0) - p) + abs(at(1) - p) - abs(at(0) - at(1)));\n }\n};\nP crosspoint(const L &l, const L &m);\nstruct G : public vector<P> {\n G(size_type size = 0) : vector(size) {}\n S edge(int i) const { return S(at(i), at(i + 1 == size() ? 0 : i + 1)); }\n};\ninline BOOL intersect(const S &s, const L &l) {\n return (sig(outp(l.dir(), s[0] - l[0])) * sig(outp(l.dir(), s[1] - l[0])) <=\n 0);\n}\ninline P crosspoint(const L &l, const L &m) {\n R A = outp(l.dir(), m.dir()), B = outp(l.dir(), l[1] - m[0]);\n if (!sig(abs(A)) && !sig(abs(B)))\n return m[0];\n if (abs(A) < EPS)\n assert(false);\n return m[0] + B / A * (m[1] - m[0]);\n}\nstruct Arrangement {\n struct AEdge {\n int u, v, t;\n R cost;\n AEdge(int u = 0, int v = 0, int t = 0, R cost = 0)\n : u(u), v(v), t(t), cost(cost) {}\n };\n typedef vector<vector<AEdge>> AGraph;\n vector<P> p;\n AGraph g;\n Arrangement() {}\n Arrangement(vector<S> seg) {\n int m = seg.size();\n REP(i, m) {\n p.push_back(seg[i][0]);\n p.push_back(seg[i][1]);\n REP(j, i)\n if (sig(outp(seg[i].dir(), seg[j].dir())) &&\n intersect(seg[i], seg[j]) == TRUE)\n p.push_back(crosspoint(seg[i], seg[j]));\n }\n sort(ALL(p));\n UNIQUE(p);\n int n = p.size();\n g.resize(n);\n REP(i, m) {\n S &s = seg[i];\n vector<pair<R, int>> ps;\n REP(j, n) if (s.online(p[j])) ps.emplace_back(norm(p[j] - s[0]), j);\n sort(ALL(ps));\n REP(j, (int)ps.size() - 1) {\n const int u = ps[j].second;\n const int v = ps[j + 1].second;\n g[u].emplace_back(u, v, 0, abs(p[u] - p[v]));\n g[v].emplace_back(v, u, 0, abs(p[u] - p[v]));\n }\n }\n }\n int getIdx(P q) {\n auto it = lower_bound(ALL(p), q);\n if (it == p.end() || *it != q)\n return -1;\n return it - p.begin();\n }\n};\nstruct DualGraph {\n struct DEdge {\n int u, v, f, l;\n R a;\n DEdge(int u, int v, R a) : u(u), v(v), f(0), l(0) {\n while (PI < a)\n a -= 2 * PI;\n while (a < -PI)\n a += 2 * PI;\n this->a = a;\n }\n bool operator==(const DEdge &opp) const { return v == opp.v; }\n bool operator<(const DEdge &opp) const { return a > opp.a; }\n bool operator<(const R &opp) const { return a > opp; }\n friend ostream &operator<<(ostream &os, const DEdge &t) {\n return os << \"(\" << t.u << \",\" << t.v << \",\" << t.a * 180 / PI << \")\";\n }\n };\n int n;\n vector<P> p;\n vector<vector<DEdge>> g;\n DualGraph(const vector<P> &p) : p(p), g(p.size()), n(p.size()) {}\n void add_edge(const int s, const int t) {\n R a = arg(p[t] - p[s]);\n g[s].emplace_back(s, t, a);\n g[t].emplace_back(t, s, a > 0 ? a - PI : a + PI);\n }\n void add_polygon(int s, G &t, R a) {\n auto e = lower_bound(ALL(g[s]), a - EPS);\n if (e == g[s].end())\n e = g[s].begin();\n if (e->f)\n return;\n e->f = 1;\n t.push_back(p[s]);\n add_polygon(e->v, t, e->a > 0 ? e->a - PI : e->a + PI);\n }\n G dual() {\n REP(i, n) {\n sort(ALL(g[i]));\n UNIQUE(g[i]);\n }\n int s = min_element(ALL(p)) - p.begin();\n G poly;\n add_polygon(s, poly, -PI * (R).5);\n return poly;\n }\n};\n#undef SELF\n#undef at\n} // namespace geom\nusing namespace geom;\nnamespace std {\nbool operator<(const P &a, const P &b) {\n return sig(a.X - b.X) ? a.X < b.X : a.Y + EPS < b.Y;\n}\nbool operator==(const P &a, const P &b) { return abs(a - b) < EPS; }\nistream &operator>>(istream &is, P &p) {\n R x, y;\n is >> x >> y;\n p = P(x, y);\n return is;\n}\n} // namespace std\nint n, m, k;\nstruct MSQ : public G {\n MSQ() {}\n vector<P> p;\n vector<S> s;\n int m, k;\n MSQ(int m, int k) : m(m), k(k) {\n REP(i, m) p.push_back(polar((R)1, 2 * PI * i / m + PI * (R).5));\n REP(i, m) s.emplace_back(p[i], p[(i + k) % m]);\n Arrangement a(s);\n DualGraph dg(a.p);\n REP(i, a.g.size()) REP(j, a.g[i].size()) {\n int u = a.g[i][j].u;\n int v = a.g[i][j].v;\n if (u < v)\n dg.add_edge(u, v);\n }\n (G &)(*this) = dg.dual();\n reverse(this->begin(), this->end());\n }\n void copy(R r, P c, MSQ &msq) const {\n msq.resize(size());\n msq.p.resize(p.size());\n msq.s.resize(s.size());\n msq.m = m;\n msq.k = k;\n REP(i, size()) msq[i] = at(i) * r + c;\n REP(i, p.size()) msq.p[i] = p[i] * r + c;\n REP(i, s.size()) msq.s[i] = S(msq.p[i], msq.p[(i + k) % m]);\n }\n S segment(int i) const { return s[i]; }\n};\nint convex_contains(const MSQ &msq, const P &g, const P &p) {\n const int n = msq.size();\n int a = 0, b = n;\n const P pg = p - g;\n while (a + 1 < b) {\n int c = (a + b) / 2;\n if (outp(msq[a] - g, pg) > 0 && outp(msq[c] - g, pg) < 0)\n b = c;\n else\n a = c;\n }\n b %= n;\n if (outp(msq[a] - p, msq[b] - p) < -EPS)\n return 0;\n return 1;\n}\nbool check(const MSQ &temp, R r, const vector<P> &shoolack, int i, int j) {\n MSQ msq;\n L l = temp.segment(j);\n P gp = shoolack[i] - l[0] * r;\n temp.copy(r, gp, msq);\n vector<P> p;\n P b(-INF, -INF), u(+INF, +INF);\n REP(i, n) {\n L l2(shoolack[i], shoolack[i] + l.dir());\n int f = 0;\n P ll(INF, INF), rr(-INF, -INF);\n REP(j, m) {\n const S s = msq.segment(j);\n if (intersect(s, l2)) {\n if (sig(outp(s.dir(), l2.dir()))) {\n const P q = crosspoint(s, l2) - l2[0];\n p.push_back(q);\n ll = min(ll, q);\n rr = max(rr, q);\n }\n f = 1;\n }\n }\n u = min(rr, u);\n b = max(ll, b);\n if (!f)\n return false;\n }\n FOR(q, p) {\n if (*q < b || u < *q)\n continue;\n if ([&]() {\n REP(i, n) if (!convex_contains(msq, gp, shoolack[i] + *q)) return 0;\n return 1;\n }())\n return true;\n }\n return false;\n}\nint main() {\n ios::sync_with_stdio(false);\n while (cin >> n >> m >> k, n) {\n MSQ temp(m, k);\n vector<P> shoolack(n);\n REP(i, n) cin >> shoolack[i];\n R best = 2000;\n REP(i, n) REP(j, m) {\n if (!check(temp, best - EPS, shoolack, i, j))\n continue;\n R l = EPS, r = best;\n REP(itr, 50) {\n R m = (l + r) * (R).5;\n if (check(temp, m, shoolack, i, j))\n r = m;\n else\n l = m;\n }\n best = r;\n }\n printf(\"%.10f\\n\", (double)best);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 2090, "memory_kb": 4272, "score_of_the_acc": -1.0058, "final_rank": 16 }, { "submission_id": "aoj_2203_8113280", "code_snippet": "#pragma GCC optimize(\"Ofast\")\n#pragma GCC target(\"avx,avx2,fma\")\n#include <algorithm>\n#include <assert.h>\n#include <bitset>\n#include <climits>\n#include <cmath>\n#include <complex>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <ctime>\n#include <deque>\n#include <iomanip>\n#include <iostream>\n#include <iterator>\n#include <list>\n#include <map>\n#include <queue>\n#include <set>\n#include <sstream>\n#include <stack>\n#include <string>\n#include <unordered_map>\n#include <valarray>\n#include <vector>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned int uint;\ntypedef unsigned char uchar;\ntypedef unsigned long long ull;\ntypedef pair<int, int> pii;\ntypedef pair<ll, ll> pll;\ntypedef vector<int> vi;\n#define REP(i, x) for (int i = 0; i < (int)(x); i++)\n#define REPS(i, x) for (int i = 1; i <= (int)(x); i++)\n#define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--)\n#define RREPS(i, x) for (int i = ((int)(x)); i > 0; i--)\n#define FOR(i, c) \\\n for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)\n#define RFOR(i, c) \\\n for (__typeof((c).rbegin()) i = (c).rbegin(); i != (c).rend(); i++)\n#define ALL(container) (container).begin(), (container).end()\n#define RALL(container) (container).rbegin(), (container).rend()\n#define SZ(container) ((int)container.size())\n#define mp(a, b) make_pair(a, b)\n#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());\ntemplate <class T> bool chmax(T &a, const T &b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <class T> bool chmin(T &a, const T &b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <class T> ostream &operator<<(ostream &os, const vector<T> &t) {\n os << \"[\";\n FOR(it, t) {\n if (it != t.begin())\n os << \",\";\n os << *it;\n }\n os << \"]\";\n return os;\n}\ntemplate <class T> ostream &operator<<(ostream &os, const set<T> &t) {\n os << \"{\";\n FOR(it, t) {\n if (it != t.begin())\n os << \",\";\n os << *it;\n }\n os << \"}\";\n return os;\n}\ntemplate <class S, class T>\nostream &operator<<(ostream &os, const pair<S, T> &t) {\n return os << \"(\" << t.first << \",\" << t.second << \")\";\n}\ntemplate <class S, class T>\npair<S, T> operator+(const pair<S, T> &s, const pair<S, T> &t) {\n return pair<S, T>(s.first + t.first, s.second + t.second);\n}\ntemplate <class S, class T>\npair<S, T> operator-(const pair<S, T> &s, const pair<S, T> &t) {\n return pair<S, T>(s.first - t.first, s.second - t.second);\n}\nnamespace geom {\n#define X real()\n#define Y imag()\n#define at(i) ((*this)[i])\n#define SELF (*this)\nenum { TRUE = 1, FALSE = 0, BORDER = -1 };\ntypedef int BOOL;\ntypedef double R;\nconst R INF = 1e8;\nR EPS = 1e-6;\nconst R PI = 3.1415926535897932384626;\ninline int sig(const R &x) { return (abs(x) < EPS ? 0 : x > 0 ? 1 : -1); }\ninline BOOL less(const R &x, const R &y) { return sig(x - y) ? x < y : BORDER; }\ntypedef complex<R> P;\ninline R norm(const P &p) { return p.X * p.X + p.Y * p.Y; }\ninline R inp(const P &a, const P &b) { return (conj(a) * b).X; }\ninline R outp(const P &a, const P &b) { return (conj(a) * b).Y; }\ninline P unit(const P &p) { return p / abs(p); }\ninline P proj(const P &s, const P &t) { return t * inp(s, t) / norm(t); }\ninline int ccw(const P &s, const P &t, const P &p, int adv = 0) {\n int res = sig(outp(t - s, p - s));\n if (res || !adv)\n return res;\n if (sig(inp(t - s, p - s)) < 0)\n return -2;\n if (sig(inp(s - t, p - t)) < 0)\n return 2;\n return 0;\n}\nstruct L : public vector<P> {\n L(const P &p1, const P &p2) {\n this->push_back(p1);\n this->push_back(p2);\n }\n L() {}\n P dir() const { return at(1) - at(0); }\n BOOL online(const P &p) const { return !sig(outp(p - at(0), dir())); }\n};\nstruct S : public L {\n S(const P &p1, const P &p2) : L(p1, p2) {}\n S() {}\n BOOL online(const P &p) const {\n if (!sig(norm(p - at(0))) || !sig(norm(p - at(1))))\n return BORDER;\n return !sig(outp(p - at(0), dir())) && inp(p - at(0), dir()) > -EPS &&\n inp(p - at(1), -dir()) > -EPS\n ? TRUE\n : FALSE;\n return !sig(abs(at(0) - p) + abs(at(1) - p) - abs(at(0) - at(1)));\n }\n};\nP crosspoint(const L &l, const L &m);\nstruct G : public vector<P> {\n G(size_type size = 0) : vector(size) {}\n S edge(int i) const { return S(at(i), at(i + 1 == size() ? 0 : i + 1)); }\n};\ninline BOOL intersect(const S &s, const L &l) {\n return (sig(outp(l.dir(), s[0] - l[0])) * sig(outp(l.dir(), s[1] - l[0])) <=\n 0);\n}\ninline P crosspoint(const L &l, const L &m) {\n R A = outp(l.dir(), m.dir()), B = outp(l.dir(), l[1] - m[0]);\n if (!sig(abs(A)) && !sig(abs(B)))\n return m[0];\n if (abs(A) < EPS)\n assert(false);\n return m[0] + B / A * (m[1] - m[0]);\n}\nstruct Arrangement {\n struct AEdge {\n int u, v, t;\n R cost;\n AEdge(int u = 0, int v = 0, int t = 0, R cost = 0)\n : u(u), v(v), t(t), cost(cost) {}\n };\n typedef vector<vector<AEdge>> AGraph;\n vector<P> p;\n AGraph g;\n Arrangement() {}\n Arrangement(vector<S> seg) {\n int m = seg.size();\n REP(i, m) {\n p.push_back(seg[i][0]);\n p.push_back(seg[i][1]);\n REP(j, i)\n if (sig(outp(seg[i].dir(), seg[j].dir())) &&\n intersect(seg[i], seg[j]) == TRUE)\n p.push_back(crosspoint(seg[i], seg[j]));\n }\n sort(ALL(p));\n UNIQUE(p);\n int n = p.size();\n g.resize(n);\n REP(i, m) {\n S &s = seg[i];\n vector<pair<R, int>> ps;\n REP(j, n) if (s.online(p[j])) ps.emplace_back(norm(p[j] - s[0]), j);\n sort(ALL(ps));\n REP(j, (int)ps.size() - 1) {\n const int u = ps[j].second;\n const int v = ps[j + 1].second;\n g[u].emplace_back(u, v, 0, abs(p[u] - p[v]));\n g[v].emplace_back(v, u, 0, abs(p[u] - p[v]));\n }\n }\n }\n int getIdx(P q) {\n auto it = lower_bound(ALL(p), q);\n if (it == p.end() || *it != q)\n return -1;\n return it - p.begin();\n }\n};\nstruct DualGraph {\n struct DEdge {\n int u, v, f, l;\n R a;\n DEdge(int u, int v, R a) : u(u), v(v), f(0), l(0) {\n while (PI < a)\n a -= 2 * PI;\n while (a < -PI)\n a += 2 * PI;\n this->a = a;\n }\n bool operator==(const DEdge &opp) const { return v == opp.v; }\n bool operator<(const DEdge &opp) const { return a > opp.a; }\n bool operator<(const R &opp) const { return a > opp; }\n friend ostream &operator<<(ostream &os, const DEdge &t) {\n return os << \"(\" << t.u << \",\" << t.v << \",\" << t.a * 180 / PI << \")\";\n }\n };\n int n;\n vector<P> p;\n vector<vector<DEdge>> g;\n DualGraph(const vector<P> &p) : p(p), g(p.size()), n(p.size()) {}\n void add_edge(const int s, const int t) {\n R a = arg(p[t] - p[s]);\n g[s].emplace_back(s, t, a);\n g[t].emplace_back(t, s, a > 0 ? a - PI : a + PI);\n }\n void add_polygon(int s, G &t, R a) {\n auto e = lower_bound(ALL(g[s]), a - EPS);\n if (e == g[s].end())\n e = g[s].begin();\n if (e->f)\n return;\n e->f = 1;\n t.push_back(p[s]);\n add_polygon(e->v, t, e->a > 0 ? e->a - PI : e->a + PI);\n }\n G dual() {\n REP(i, n) {\n sort(ALL(g[i]));\n UNIQUE(g[i]);\n }\n int s = min_element(ALL(p)) - p.begin();\n G poly;\n add_polygon(s, poly, -PI * (R).5);\n return poly;\n }\n};\n#undef SELF\n#undef at\n} // namespace geom\nusing namespace geom;\nnamespace std {\nbool operator<(const P &a, const P &b) {\n return sig(a.X - b.X) ? a.X < b.X : a.Y + EPS < b.Y;\n}\nbool operator==(const P &a, const P &b) { return abs(a - b) < EPS; }\nistream &operator>>(istream &is, P &p) {\n R x, y;\n is >> x >> y;\n p = P(x, y);\n return is;\n}\n} // namespace std\nint n, m, k;\nstruct MSQ : public G {\n MSQ() {}\n vector<P> p;\n vector<S> s;\n int m, k;\n MSQ(int m, int k) : m(m), k(k) {\n REP(i, m) p.push_back(polar((R)1, 2 * PI * i / m + PI * (R).5));\n REP(i, m) s.emplace_back(p[i], p[(i + k) % m]);\n Arrangement a(s);\n DualGraph dg(a.p);\n REP(i, a.g.size()) REP(j, a.g[i].size()) {\n int u = a.g[i][j].u;\n int v = a.g[i][j].v;\n if (u < v)\n dg.add_edge(u, v);\n }\n (G &)(*this) = dg.dual();\n reverse(this->begin(), this->end());\n }\n void copy(R r, P c, MSQ &msq) const {\n msq.resize(size());\n msq.p.resize(p.size());\n msq.s.resize(s.size());\n msq.m = m;\n msq.k = k;\n REP(i, size()) msq[i] = at(i) * r + c;\n REP(i, p.size()) msq.p[i] = p[i] * r + c;\n REP(i, s.size()) msq.s[i] = S(msq.p[i], msq.p[(i + k) % m]);\n }\n S segment(int i) const { return s[i]; }\n};\nint convex_contains(const MSQ &msq, const P &g, const P &p) {\n const int n = msq.size();\n int a = 0, b = n;\n const P pg = p - g;\n while (a + 1 < b) {\n int c = (a + b) / 2;\n if (outp(msq[a] - g, pg) > 0 && outp(msq[c] - g, pg) < 0)\n b = c;\n else\n a = c;\n }\n b %= n;\n if (outp(msq[a] - p, msq[b] - p) < -EPS)\n return 0;\n return 1;\n}\nbool check(const MSQ &temp, R r, const vector<P> &shoolack, int i, int j) {\n MSQ msq;\n L l = temp.segment(j);\n P gp = shoolack[i] - l[0] * r;\n temp.copy(r, gp, msq);\n vector<P> p;\n P b(-INF, -INF), u(+INF, +INF);\n REP(i, n) {\n L l2(shoolack[i], shoolack[i] + l.dir());\n int f = 0;\n P ll(INF, INF), rr(-INF, -INF);\n REP(j, m) {\n const S s = msq.segment(j);\n if (intersect(s, l2)) {\n if (sig(outp(s.dir(), l2.dir()))) {\n const P q = crosspoint(s, l2) - l2[0];\n p.push_back(q);\n ll = min(ll, q);\n rr = max(rr, q);\n }\n f = 1;\n }\n }\n u = min(rr, u);\n b = max(ll, b);\n if (!f)\n return false;\n }\n FOR(q, p) {\n if (*q < b || u < *q)\n continue;\n if ([&]() {\n REP(i, n) if (!convex_contains(msq, gp, shoolack[i] + *q)) return 0;\n return 1;\n }())\n return true;\n }\n return false;\n}\nint main() {\n ios::sync_with_stdio(false);\n while (cin >> n >> m >> k, n) {\n MSQ temp(m, k);\n vector<P> shoolack(n);\n REP(i, n) cin >> shoolack[i];\n R best = 2000;\n REP(i, n) REP(j, m) {\n if (!check(temp, best - EPS, shoolack, i, j))\n continue;\n R l = EPS, r = best;\n REP(itr, 50) {\n R m = (l + r) * (R).5;\n if (check(temp, m, shoolack, i, j))\n r = m;\n else\n l = m;\n }\n best = r;\n }\n printf(\"%.10f\\n\", (double)best);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 3340, "memory_kb": 4424, "score_of_the_acc": -1.2547, "final_rank": 19 }, { "submission_id": "aoj_2203_4885113", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 1000000000000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n\n\nstruct Point{\n\tPoint(double arg_x,double arg_y){\n\t\tx = arg_x;\n\t\ty = arg_y;\n\t}\n\n\tPoint(){\n\t\tx = y = 0.0;\n\t}\n\n\tPoint operator + (Point p){ return Point(x+p.x,y+p.y); }\n\tPoint operator - (Point p){ return Point(x-p.x,y-p.y);}\n\tPoint operator * (double a){ return Point(a*x,a*y); }\n\tPoint operator / (double a){ return Point(x/a,y/a); }\n\n\tdouble abs(){ return sqrt(norm()); }\n\tdouble norm(){ return x*x + y*y; }\n\n\tbool operator<(const Point &p) const{\n\t\treturn x != p.x? x < p.x: y < p.y;\n\t}\n\n\tbool operator == (const Point &p) const{\n\t\treturn fabs(x-p.x) < EPS && fabs(y-p.y) < EPS;\n\t}\n\n\n\tvoid debug(){\n\n\t\tprintf(\"(%.3lf,%.3lf)\\n\",x,y);\n\t}\n\n\tdouble x,y;\n};\n\ntypedef Point Vector;\ntypedef vector<Point> Polygon;\n\nstruct Line{\n\tLine(){\n\n\t}\n\tLine(Point a,Point b){\n\t\tp[0] = a;\n\t\tp[1] = b;\n\t}\n\t/*void outPut(){\n\n\t\tprintf(\"(%.3lf,%.3lf)-(%.3lf,%.3lf)\\n\",p[0].x,p[0].y,p[1].x,p[1].y);\n\t}*/\n\tPoint p[2];\n};\n\n\nint N,M,K;\nPoint point[55];\nPolygon STAR,rev_STAR;\n\n\ndouble norm(Vector a){\n\treturn a.x*a.x+a.y*a.y;\n}\n\ndouble abs(Vector a){\n\treturn sqrt(norm(a));\n}\n\ndouble cross(Vector a,Vector b){\n return a.x*b.y-a.y*b.x;\n}\n\ndouble dot(Vector a,Vector b){\n return a.x*b.x + a.y*b.y;\n}\n\nstatic const int COUNTER_CLOCKWISE = 1;\nstatic const int CLOCKWISE = -1;\nstatic const int ONLINE_BACK = 2;\nstatic const int ONLINE_FRONT = -2;\nstatic const int ON_SEGMENT = 0;\n\nint ccw(Point p0,Point p1,Point p2){\n\n\tVector a = p1 - p0;\n\tVector b = p2 - p0;\n\n\tif(cross(a,b) > EPS)return COUNTER_CLOCKWISE;\n\tif(cross(a,b) < -EPS)return CLOCKWISE;\n\tif(dot(a,b) < -EPS)return ONLINE_BACK;\n\tif(a.norm() < b.norm())return ONLINE_FRONT;\n\n\treturn ON_SEGMENT;\n}\n\n\nint func(double x1,double y1,double x2, double y2, double xp, double yp){\n\tdouble naiseki,norm1,norm2,gaiseki;\n\tnorm1 = sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));\n\tnorm2 = sqrt((xp-x1)*(xp-x1)+(yp-y1)*(yp-y1));\n\tnaiseki = (xp-x1)*(x2-x1)+(yp-y1)*(y2-y1);\n\tgaiseki = (x2-x1)*(yp-y1)-(xp-x1)*(y2-y1);\n\tif(gaiseki > EPS){\n\t\treturn 1;\n\t}else if(gaiseki < -EPS){\n\t\treturn -1;\n\t}\n\tif(naiseki < -EPS){\n\t\treturn 2;\n\t}\n\n\tif(norm1 < norm2){\n\t\treturn -2;\n\t}\n\treturn 0;\n}\n\n\n//★★直線ではなく、線分の交差判定★★\nbool is_Cross(Line a,Line b){\n\n\tif(func(a.p[0].x,a.p[0].y,a.p[1].x,a.p[1].y,b.p[0].x,b.p[0].y)*\n\t\t\tfunc(a.p[0].x,a.p[0].y,a.p[1].x,a.p[1].y,b.p[1].x,b.p[1].y) <= 0 &&\n\t\t\tfunc(b.p[0].x,b.p[0].y,b.p[1].x,b.p[1].y,a.p[0].x,a.p[0].y)*\n\t\t\tfunc(b.p[0].x,b.p[0].y,b.p[1].x,b.p[1].y,a.p[1].x,a.p[1].y) <= 0){\n\t\treturn true;\n\t}\n\treturn false;\n}\n\n\n\nPoint calc_minus(Point a,Point b){\n Point ret;\n\n ret.x = a.x-b.x;\n ret.y = a.y-b.y;\n\n return ret;\n}\n\ndouble calc_len(Vector a){\n return sqrt(a.x*a.x+a.y*a.y);\n}\n\n//線分ではなく直線と点の距離\ndouble getDistanceLP(Line l,Point p){\n return fabs(cross(calc_minus(l.p[1],l.p[0]),calc_minus(p,l.p[0]))/calc_len(calc_minus(l.p[1],l.p[0])));\n}\n\n//点と線分の距離\ndouble getDistanceSP(Line l,Point p){\n\tif(dot(calc_minus(l.p[1],l.p[0]),calc_minus(p,l.p[0])) < 0.0)return calc_len(calc_minus(p,l.p[0]));\n\tif(dot(calc_minus(l.p[0],l.p[1]),calc_minus(p,l.p[1])) < 0.0)return calc_len(calc_minus(p,l.p[1]));\n\treturn getDistanceLP(l,p);\n}\n\n//交点を求める関数\nPoint calc_Cross_Point(double x1,double x2,double x3,double x4,double y1,double y2,double y3,double y4){\n\tPoint ret;\n\tret.x = ((x2-x1)*(y3*(x4-x3)+x3*(y3-y4))-(x4-x3)*(y1*(x2-x1)+x1*(y1-y2)))/((y2-y1)*(x4-x3)-(y4-y3)*(x2-x1));\n\tif(fabs(x1-x2) > EPS){\n\t\tret.y = ((y2-y1)*ret.x+y1*(x2-x1)+x1*(y1-y2))/(x2-x1);\n\t}else{\n\t\tret.y = ((y4-y3)*ret.x+y3*(x4-x3)+x3*(y3-y4))/(x4-x3);\n\t}\n\treturn ret;\n}\n\n//インタフェース関数\nPoint calc_Cross_Point(Point a,Point b,Point c,Point d){\n\treturn calc_Cross_Point(a.x,b.x,c.x,d.x,a.y,b.y,c.y,d.y);\n}\n\nPoint calc_Cross_Point(Line A,Line B){\n\n\tif(getDistanceSP(B,A.p[0]) < EPS){\n\n\t\treturn A.p[0];\n\n\t}else if(getDistanceSP(B,A.p[1]) < EPS){\n\n\t\treturn A.p[1];\n\n\t}else if(getDistanceSP(A,B.p[0]) < EPS){\n\n\t\treturn B.p[0];\n\n\t}else if(getDistanceSP(A,B.p[1]) < EPS){\n\n\t\treturn B.p[1];\n\t}\n\n\treturn calc_Cross_Point(A.p[0],A.p[1],B.p[0],B.p[1]);\n}\n\n/*点moveをbaseを中心にradラジアン回転させる\nrad > 0なら反時計回り、rad < 0なら時計周り\n*/\nPoint rotate(Point base,Point move,double rad){\n\n\tPoint ret;\n\tmove = move-base;\n\n\tret.x = move.x*cos(rad)-move.y*sin(rad);\n\tret.y = move.x*sin(rad)+move.y*cos(rad);\n\n\tret = ret+base;\n\n\treturn ret;\n}\n\n/*\n * IN 2\n * ON 1\n * OUT 0\n *\n */\nint contains(Polygon g,Point p){\n\tint n = g.size();\n\tbool x = false;\n\tfor(int i = 0; i < n; i++){\n\t\tPoint a = g[i]-p,b = g[(i+1)%n]-p;\n\t\tif(abs(cross(a,b)) < EPS && dot(a,b) < EPS)return 1;\n\t\tif(a.y > b.y)swap(a,b);\n\t\tif(a.y < EPS && EPS < b.y && cross(a,b) > EPS) x = !x;\n\t}\n\treturn (x ? 2:0);\n}\n\n\nbool is_OK(int p_id,int edge_id,double R){\n\n\tPolygon work_STAR = STAR;\n\tfor(int i = 0; i < work_STAR.size(); i++){\n\n\t\twork_STAR[i] = work_STAR[i]*R;\n\t}\n\n\tPolygon work_REV = rev_STAR;\n\tfor(int i = 0; i < work_REV.size(); i++){\n\n\t\twork_REV[i] = work_REV[i]*R;\n\t}\n\n\tint SIZE = work_REV.size();\n\n\tLine base_line = Line(work_REV[edge_id]+point[p_id],work_REV[(edge_id+1)%SIZE]+point[p_id]);\n\n\tvector<Point> P;\n\n\tfor(int i = 0; i < N; i++){\n\t\tfor(int k = 0; k < SIZE; k++){\n\t\t\tLine work_line = Line(work_REV[k]+point[i],work_REV[(k+1)%SIZE]+point[i]);\n\n\t\t\tif(!is_Cross(base_line,work_line))continue;\n\n\t\t\tP.push_back(calc_Cross_Point(base_line,work_line));\n\t\t}\n\t}\n\n\tfor(int i = 0; i < P.size(); i++){\n\t\tbool FLG = true;\n\t\tfor(int k = 0; k < N; k++){\n\t\t\tif(contains(work_STAR,point[k]-P[i]) == 0){\n\n\t\t\t\tFLG = false;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tif(FLG)return true;\n\t}\n\n\treturn false;\n}\n\n\nvoid func(){\n\n\tSTAR.clear();\n\trev_STAR.clear();\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tscanf(\"%lf %lf\",&point[i].x,&point[i].y);\n\t}\n\n\tPoint center = Point(0,0);\n\tPoint tmp_p = Point(0,1);\n\tdouble tmp_rad = (2*M_PI)/M;\n\n\tvector<Point> vec;\n\n\tfor(int i = 0; i < M; i++){\n\n\t\tvec.push_back(rotate(center,tmp_p,tmp_rad*i));\n\t}\n\n\tfor(int i = 0; i < vec.size(); i++){\n\n\t\tSTAR.push_back(vec[i]);\n\n\t\tif(K >= 2){\n\t\t\tLine line1 = Line(vec[i],vec[(i+K)%M]);\n\t\t\tLine line2 = Line(vec[(i+1)%M],vec[(i+1-K+M)%M]);\n\n\t\t\tSTAR.push_back(calc_Cross_Point(line1,line2));\n\t\t}\n\t}\n\n\ttmp_p = Point(0,-1);\n\tvec.clear();\n\n\tfor(int i = 0; i < M; i++){\n\n\t\tvec.push_back(rotate(center,tmp_p,tmp_rad*i));\n\t}\n\n\tfor(int i = 0; i < vec.size(); i++){\n\n\t\trev_STAR.push_back(vec[i]);\n\n\t\tif(K >=2){\n\t\t\tLine line1 = Line(vec[i],vec[(i+K)%M]);\n\t\t\tLine line2 = Line(vec[(i+1)%M],vec[(i+1-K+M)%M]);\n\n\t\t\trev_STAR.push_back(calc_Cross_Point(line1,line2));\n\t\t}\n\t}\n\n\tdouble ans = 5000;\n\n\tfor(int i = 0; i < N; i++){\n\t\tfor(int k = 0; k < STAR.size(); k++){\n\t\t\tif(is_OK(i,k,ans)){\n\t\t\t\tdouble left = 0,right = ans;\n\t\t\t\tdouble mid = (left+right)/2;\n\n\t\t\t\twhile(left+EPS < right){\n\t\t\t\t\tif(is_OK(i,k,mid)){\n\n\t\t\t\t\t\tans = mid;\n\t\t\t\t\t\tright = mid-EPS;\n\n\t\t\t\t\t}else{\n\n\t\t\t\t\t\tleft = mid+EPS;\n\t\t\t\t\t}\n\t\t\t\t\tmid = (left+right)/2;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tprintf(\"%.10lf\\n\",ans);\n}\n\n\nint main(){\n\n\twhile(true){\n\n\t\tscanf(\"%d %d %d\",&N,&M,&K);\n\t\tif(N == 0 && M == 0 && K == 0)break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 7100, "memory_kb": 3604, "score_of_the_acc": -1.6004, "final_rank": 20 }, { "submission_id": "aoj_2203_2836937", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <iomanip>\n#include <complex>\n#include <cmath>\n#include <array>\nusing namespace std;\n\nconst double EPS = 1e-8;\nconst double INF = 1e12;\nconst double PI = acos(-1);\n#define EQ(n,m) (abs((n)-(m)) < EPS)\n#define X real()\n#define Y imag()\n\ntypedef complex<double> P;\ntypedef vector<P> VP;\nstruct L : array<P, 2>{\n L(const P& a, const P& b){ at(0)=a; at(1)=b; }\n L(){}\n};\n\ndouble dot(P a, P b){\n return (conj(a)*b).X;\n}\ndouble cross(P a, P b){\n return (conj(a)*b).Y;\n}\nint ccw(P a, P b, P c){\n b -= a;\n c -= a;\n if(cross(b,c) > EPS) return +1; //ccw\n if(cross(b,c) < -EPS) return -1; //cw\n if(dot(b,c) < -EPS) return +2; //c-a-b\n if(abs(c)-abs(b) > EPS) return -2; //a-b-c\n return 0; //a-c-b\n}\nP rotate(const P &p, double rad){\n return p *P(cos(rad), sin(rad));\n}\n\nbool intersectSS(const L& a, const L& b){\n return ( ccw(a[0],a[1],b[0]) *ccw(a[0],a[1],b[1]) <= 0 ) &&\n ( ccw(b[0],b[1],a[0]) *ccw(b[0],b[1],a[1]) <= 0 );\n}\nP crosspointLL(const L &l, const L &m) {\n double A = cross(l[1]-l[0], m[1]-m[0]);\n double B = cross(l[1]-l[0], l[1]-m[0]);\n return m[0] + B/A *(m[1]-m[0]);\n}\nbool isParallel(const P &a, const P &b){\n return abs(cross(a,b)) < EPS;\n}\nbool isParallel(const L &a, const L &b){\n return isParallel(a[1]-a[0], b[1]-b[0]);\n}\n\n//(0,0)-center\nbool in_convex(const P &p, const VP &poly){\n\tint n = poly.size();\n\tint a = 0, b = n;\n\twhile(a+1 < b){\n\t\tint c = (a + b)/2;\n\t\tif(cross(poly[a], poly[c]) > 0){\n\t\t\tif(cross(poly[a], p)>0 && cross(poly[c], p)<0) b = c;\n\t\t\telse a = c;\n\t\t}else{\n\t\t\tif(cross(poly[a], p)<0 && cross(poly[c], p)>0) a = c;\n\t\t\telse b = c;\n\t\t}\n\t}\n\tb %= n;\n\tif(cross(poly[b] -poly[a], p -poly[a]) > -EPS) return true;\n\treturn false;\n}\n\nVP makeMagicCircle(int m, int k){\n\tVP v(m);\n\tfor(int i=0; i<m; i++){\n\t\tv[i] = rotate(P(0, 1), 2*PI *i/m);\n\t}\n\tVP ret;\n\tfor(int i=0; i<m; i++){\n\t\tret.push_back(v[i]);\n\t\tif(k >= 2){\n\t\t\tret.push_back(crosspointLL(L(v[i], v[(i+k)%m]), L(v[(i+1)%m], v[(i+1-k+m)%m])));\n\t\t}\n\t}\n\treturn ret;\n}\nbool canEnclose(int pidx, int eidx, double scale, const VP &v, const VP &poly){\n\tint n = v.size();\n\tint m = poly.size();\n\tL edge(-scale *poly[eidx] +v[pidx], -scale *poly[(eidx+1)%m] +v[pidx]);\n\tfor(int pidx2=0; pidx2<n; pidx2++){\n\t\tVP cand;\n\t\tfor(int i=0; i<m; i++){\n\t\t\tL e(-scale *poly[i] +v[pidx2], -scale *poly[(i+1)%m] +v[pidx2]);\n\t\t\tif(!isParallel(e, edge) && intersectSS(e, edge)){\n\t\t\t\tcand.push_back(crosspointLL(e, edge));\n\t\t\t}\n\t\t}\n\t\t\n\t\tfor(int i=0; i<(int)cand.size(); i++){\n\t\t\tVP mc = poly;\n\t\t\tfor(int j=0; j<m; j++){\n\t\t\t\tmc[j] = scale *poly[j];\n\t\t\t}\n\t\t\tbool enclose = true;\n\t\t\tfor(int j=0; j<n; j++){\n\t\t\t\tif(!in_convex(v[j]-cand[i], mc)){\n\t\t\t\t\tenclose = false;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(enclose) return true;\n\t\t}\n\t}\n\treturn false;\n}\ndouble solve(int m, int k, const VP &v){\n\tdouble ret = 2000;\n\tVP poly = makeMagicCircle(m, k);\n\tfor(int i=0; i<(int)v.size(); i++){\n\t\tfor(int j=0; j<(int)poly.size(); j++){\n\t\t\tif(canEnclose(i, j, ret, v, poly)){\n\t\t\t\tdouble lb = 0, ub = ret;\n\t\t\t\twhile(ub-lb > 1e-6){\n\t\t\t\t\tdouble mid = (lb + ub)/2;\n\t\t\t\t\tif(canEnclose(i, j, mid, v, poly)){\n\t\t\t\t\t\tub = mid;\n\t\t\t\t\t}else{\n\t\t\t\t\t\tlb = mid;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tret = (lb + ub)/2;\n\t\t\t}\n\t\t}\n\t}\n\treturn ret;\n}\n\nint main(){\n\tcout << fixed;\n\tcout << setprecision(10);\n\twhile(1){\n\t\tint n,m,k;\n\t\tcin >> n >> m >> k;\n\t\tif(n==0) break;\n\t\t\n\t\tVP v(n);\n\t\tfor(int i=0; i<n; i++){\n\t\t\tint x,y;\n\t\t\tcin >> x >> y;\n\t\t\tv[i] = P(x, y);\n\t\t}\n\t\tcout << solve(m, k, v) << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 4210, "memory_kb": 3508, "score_of_the_acc": -1.1054, "final_rank": 18 }, { "submission_id": "aoj_2203_2391188", "code_snippet": "#include<cmath>\n#include<cstdio>\n#include<vector>\n#include<algorithm>\n \n#define rep(i,n) for(int i=0;i<(n);i++)\n \nusing namespace std;\n \nconst double EPS=1e-7;\nconst double PI=acos(-1);\n \ntemplate<class T>\nstruct point{\n T x,y;\n point operator+(const point &a)const{ return (point){x+a.x,y+a.y}; }\n point operator-(const point &a)const{ return (point){x-a.x,y-a.y}; }\n};\n \ntemplate<class T>\npoint<T> operator*(T c,const point<T> &a){ return (point<T>){c*a.x,c*a.y}; }\n \nbool operator==(const point<double> &a,const point<double> &b){\n return abs(a.x-b.x)<EPS && abs(a.y-b.y)<EPS;\n}\n \ntemplate<class T>\ndouble abs(const point<T> &a){ return sqrt(a.x*a.x+a.y*a.y); }\n \ntemplate<class T>\nT cross(const point<T> &a,const point<T> &b){ return a.x*b.y-a.y*b.x; }\n \npoint<double> rot(const point<double> &a,double theta){\n return (point<double>){a.x*cos(theta)-a.y*sin(theta),a.x*sin(theta)+a.y*cos(theta)};\n}\n \ntemplate<class T>\ndouble arg(const point<T> &a){\n double t=atan2(a.y,a.x);\n return t<0?t+2*PI:t;\n}\n \ntemplate<class T>\nstruct segment{ point<T> a,b; };\n \nenum{CCW=1,CW=-1,ON=0};\nint ccw(const point<double> &a,const point<double> &b,const point<double> &c){\n double rdir=cross(b-a,c-a);\n if(rdir> EPS) return CCW;\n if(rdir<-EPS) return CW;\n return ON;\n}\n \ntemplate<class T>\ndouble dist(const point<T> &a,const point<T> &b){\n return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));\n}\n \nbool cover(const segment<double> &S,const point<double> &p){\n return dist(S.a,p)+dist(p,S.b)<dist(S.a,S.b)+EPS;\n}\n \nbool intersect(const segment<double> &S1,const segment<double> &S2){\n if(max(S1.a.x,S1.b.x)+EPS<min(S2.a.x,S2.b.x)\n || max(S1.a.y,S1.b.y)+EPS<min(S2.a.y,S2.b.y)\n || max(S2.a.x,S2.b.x)+EPS<min(S1.a.x,S1.b.x)\n || max(S2.a.y,S2.b.y)+EPS<min(S1.a.y,S1.b.y)) return false;\n return ccw(S1.a,S1.b,S2.a)*ccw(S1.a,S1.b,S2.b)<=0\n && ccw(S2.a,S2.b,S1.a)*ccw(S2.a,S2.b,S1.b)<=0;\n}\n \npoint<double> get_intersect(const segment<double> &S1,const segment<double> &S2){\n double a1=cross(S1.b-S1.a,S2.b-S2.a);\n double a2=cross(S1.b-S1.a,S1.b-S2.a);\n if(abs(a1)<EPS){\n if(cover(S1,S2.a)) return S2.a;\n if(cover(S1,S2.b)) return S2.b;\n if(cover(S2,S1.a)) return S1.a;\n return S1.b;\n }\n return S2.a+a2/a1*(S2.b-S2.a);\n}\n \nint n,m,K;\npoint<double> P[50]; // ???\n \npoint<double> star[50]; // ???????????¨??? m ??????????????????\n \nvector< pair<double,double> > in; // ?????????????????¨??????????????????????????????????????±. ??????????????????????????????????????? <?§????,????????????????????¢>\npoint<double> in2[100]; // ?\\???§?¨???§???????????? in ?????´?????§?¨??????´???????????? ( ?????????????????? )\n \nvoid get_intersect(const point<double> *G1,const point<double> *G2,int &sz,point<double> *res){\n segment<double> S1[50],S2[50]; // ?????????\n rep(i,m){\n S1[i].a=G1[i];\n S1[i].b=G1[(i+K)%m];\n S2[i].a=G2[i];\n S2[i].b=G2[(i+K)%m];\n }\n rep(i,m) rep(j,m) if(intersect(S1[i],S2[j])) res[sz++]=get_intersect(S1[i],S2[j]);\n}\n \nbool cover(double R,const point<double> &cen,const point<double> &p){\n if(p==cen) return true;\n \n double phi=arg(p-cen),d=dist(p,cen);\n if(d>R+EPS) return false;\n \n if(phi<in[0].first+EPS) phi+=2*PI;\n int i=upper_bound(in.begin(),in.end(),make_pair(phi,0.0))-in.begin()-1;\n return ccw(R*in2[i],R*in2[(i+1)%in.size()],p-cen)!=CW;\n}\n \n// p1, p2 : ???????¢????????????????????????????\nbool check(double R,const point<double> &p1,const point<double> &p2){\n point<double> G1[50],G2[50]; // ?????????????????¢?????????, ???????????? p1, p2 ??????????????\\???????????????????§????????????????\n rep(k,m){\n point<double> p=R*(point<double>){star[k].x,-star[k].y};\n G1[k]=p+p1;\n G2[k]=p+p2;\n }\n \n int sz=0;\n static point<double> Q[50*50]; // ???????????????????£????\n get_intersect(G1,G2,sz,Q);\n Q[sz++]=(point<double>){0,0}; // ???????????\\???????????¨?????????\n \n rep(k,sz){\n bool ok=true;\n rep(l,n) if(!cover(R,Q[k],P[l])) { ok=false; break; }\n if(ok) return true;\n }\n return false;\n}\n \nint main(){\n for(;scanf(\"%d%d%d\",&n,&m,&K),n;){\n rep(i,n) scanf(\"%lf%lf\",&P[i].x,&P[i].y);\n \n rep(i,m) star[i]=rot((point<double>){1,0},2*i*PI/m+PI/2);\n \n in.clear();\n rep(i,m){\n in.push_back(make_pair(arg(star[i]),abs(star[i])));\n if(K>1){\n segment<double> S1={star[i],star[(i+K)%m]};\n segment<double> S2={star[(i+1)%m],star[(i+1-K+m)%m]};\n point<double> p=get_intersect(S1,S2);\n in.push_back(make_pair(arg(p),abs(p)));\n }\n }\n sort(in.begin(),in.end());\n rep(i,in.size()){\n in2[i].x=in[i].second*cos(in[i].first);\n in2[i].y=in[i].second*sin(in[i].first);\n }\n \n double ans=1e4;\n // p, q ????????? R1 ??????????¢??????¨???????????¨??§????????¨?????????, ?????? R2(>R1) ??????????¢????????????¨??§????????????????????????????????????(??¨??????)?????§??????????§£???\n rep(i,n) for(int j=i+1;j<n;j++) { // ???????¢????????????????????±??????????\n if(!check(ans+EPS,P[i],P[j])) continue; // ?§£??????????????????????????????????????§????????¢?´¢?????????\n \n double lo=0,hi=ans;\n while(hi-lo>1e-6){\n double mi=(lo+hi)/2;\n if(check(mi,P[i],P[j])) hi=mi; else lo=mi;\n }\n ans=(lo+hi)/2;\n }\n \n printf(\"%.9f\\n\",ans);\n }\n \n return 0;\n}", "accuracy": 1, "time_ms": 3120, "memory_kb": 3144, "score_of_the_acc": -0.8153, "final_rank": 11 }, { "submission_id": "aoj_2203_1430697", "code_snippet": "#include<stdio.h>\n#include<algorithm>\n#include<vector>\n#include<math.h>\nusing namespace std;\nconst double EPS = 1e-10;\nconst double INF = 1e+10;\nconst double PI = acos(-1);\nint sig(double r) { return (r < -EPS) ? -1 : (r > +EPS) ? +1 : 0; }\ninline double ABS(double a){return max(a,-a);}\nstruct Pt {\n\tdouble x, y;\n\tPt() {}\n\tPt(double x, double y) : x(x), y(y) {}\n\tPt operator+(const Pt &a) const { return Pt(x + a.x, y + a.y); }\n\tPt operator-(const Pt &a) const { return Pt(x - a.x, y - a.y); }\n\tPt operator*(const Pt &a) const { return Pt(x * a.x - y * a.y, x * a.y + y * a.x); }\n\tPt operator-() const { return Pt(-x, -y); }\n\tPt operator*(const double &k) const { return Pt(x * k, y * k); }\n\tPt operator/(const double &k) const { return Pt(x / k, y / k); }\n\tdouble ABS() const { return sqrt(x * x + y * y); }\n\tdouble abs2() const { return x * x + y * y; }\n\tdouble arg() const { return atan2(y, x); }\n\tdouble dot(const Pt &a) const { return x * a.x + y * a.y; }\n\tdouble det(const Pt &a) const { return x * a.y - y * a.x; }\n};\ndouble tri(const Pt &a, const Pt &b, const Pt &c) { return (b - a).det(c - a); }\nint iSP(Pt a, Pt b, Pt c) {\n\tint s = sig((b - a).det(c - a));\n\tif (s) return s;\n\tif (sig((b - a).dot(c - a)) < 0) return -2; // c-a-b\n\tif (sig((a - b).dot(c - b)) < 0) return +2; // a-b-c\n\treturn 0;\n}\nint iLL(Pt a, Pt b, Pt c, Pt d) {\n\tif (sig((b - a).det(d - c))) return 1; // intersect\n\tif (sig((b - a).det(c - a))) return 0; // parallel\n\treturn -1; // correspond\n}\nbool iLS(Pt a, Pt b, Pt c, Pt d) {\n\treturn (sig(tri(a, b, c)) * sig(tri(a, b, d)) <= 0);\n}\nbool iSS(Pt a, Pt b, Pt c, Pt d) {\n\treturn (iSP(a, b, c) * iSP(a, b, d) <= 0 && iSP(c, d, a) * iSP(c, d, b) <= 0);\n}\nbool iSSstrict(Pt a, Pt b, Pt c, Pt d) {\n\treturn (sig(tri(a, b, c)) * sig(tri(a, b, d)) < 0 && sig(tri(c, d, a)) * sig(tri(c, d, b)) < 0);\n}\nPt pLL(Pt a, Pt b, Pt c, Pt d) {\n\tb = b - a; d = d - c; return a + b * (c - a).det(d) / b.det(d);\n}\ndouble dLP(Pt a, Pt b, Pt c) {\n\treturn ABS(tri(a, b, c)) / (b - a).ABS();\n}\nbool intri(Pt a,Pt b,Pt c,Pt d){\n\treturn iSP(a,d,b)>=0&&iSP(b,d,c)>=0&&iSP(c,d,a)>=0;\n}\nPt p[60];\nint A,B,C;\nint check(int at,int s,double r){\n\tPt vec=Pt(cos(-PI/2-PI*2/B*s),sin(-PI/2-PI*2/B*s))*r;\n\tPt vec2=Pt(cos(-PI/2-PI*2/B*(s+C)),sin(-PI/2-PI*2/B*(s+C)))*r;\n\tpair<Pt,Pt> l1=make_pair(p[at]+vec,p[at]+vec2);\n\tvector<Pt>hb;\n\t\n\tdouble SL=-9999999;\n\tdouble SR=9999999;\n\tfor(int i=0;i<A;i++){\n\t\tif(at==i)continue;\n\t\tdouble R=-99999999;\n\t\tdouble L=99999999;\n\t\tfor(int j=0;j<B;j++){\n\t\t\tvec=Pt(cos(-PI/2-PI*2/B*j),sin(-PI/2-PI*2/B*j))*r;\n\t\t\tvec2=Pt(cos(-PI/2-PI*2/B*(j+C)),sin(-PI/2-PI*2/B*(j+C)))*r;\n\t\t\tpair<Pt,Pt>l2=make_pair(p[i]+vec,p[i]+vec2);\n\t\t\tint q=iLL(l1.first,l1.second,l2.first,l2.second);\n\t\t\tif(q==1&&iSS(l1.first,l1.second,l2.first,l2.second)){\n\t\t\t\tPt sp=pLL(l1.first,l1.second,l2.first,l2.second);\n\t\t\t\tL=min(L,sp.x*114.514+sp.y);\n\t\t\t\tR=max(R,sp.x*114.514+sp.y);\n\t\t\n\t\t\t\tif(sp.ABS()<100000000&&((p[0]-sp).ABS())<r+EPS){\n\t\t\t\t\thb.push_back(sp);\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(q==-1){L=-99999999;R=99999999;}\n\t\t}\n\t\tSR=min(SR,R);\n\t\tSL=max(SL,L);\n\t}\n\tPt pb=pLL(Pt(1,0),Pt(cos(PI*2*C/B),sin(PI*2*C/B)),Pt(0,0),Pt(cos(PI/B),sin(PI/B)))*r;\n\tdouble ml=pb.ABS();\n\t\n\tfor(int i=0;i<hb.size();i++){\n\t\tif(SL<SR&&hb[i].x*114.514+hb[i].y+EPS<SL)continue;\n\t\tif(SL<SR&&hb[i].x*114.514+hb[i].y>SR+EPS)continue;\n\t\t\n\t\tbool dame=false;\n\t\tfor(int j=0;j<A;j++){\n\t\t\tdouble dis=(hb[i]-p[j]).ABS();\n\t\t\tif(dis<ml+EPS)continue;\n\t\t\tif(dis>r+EPS){dame=true;break;}\n\t\t\tdouble th=(p[j]-hb[i]).arg()-PI/2;\n\t\t\tif(th<EPS)th+=2*PI;\n\t\t\tdouble nx=PI*2/B*((int)(th*B/PI/2));\n\t\t//\tif(nx+EPS>th+PI*2)nx-=PI*2;\n\t\t\tdouble sa=ABS(nx-th);\n\t\t\tsa=min(sa,ABS(PI*2/B-sa));\n\t\t\t//Pt pa=pLL(Pt(1,0),Pt(cos(PI*2*C/B),sin(PI*2*C/B)),Pt(0,0),Pt(cos(sa),sin(sa)))*r;\n\t\t\tif(iSSstrict(Pt(1,0),Pt(cos(PI*2*C/B),sin(PI*2*C/B)),Pt(0,0),Pt(cos(sa),sin(sa))*(dis/r-EPS))){\n\t\t\t//pa.ABS()+EPS<(hb[i]-p[j]).ABS()){\n\t\t\t\tdame=true;break;\n\t\t\t}\n\t\t}\n\t\tif(!dame){return 1;}\n\t}\n\treturn 0;\n}\nint main(){\n\tint a,b,c;\n\twhile(scanf(\"%d%d%d\",&a,&b,&c),a){\n\t\tA=a;B=b;C=c;\n\t\tfor(int i=0;i<a;i++){\n\t\t\tdouble X,Y;scanf(\"%lf%lf\",&X,&Y);\n\t\t\tp[i]=Pt(X,Y);\n\t\t}\n\t\tdouble ret=2000;\n\t\tdouble kakai=0;\n\t\tfor(int i=0;i<a;i++)for(int j=i+1;j<a;j++)kakai=max(kakai,(p[i]-p[j]).ABS()/2);\n\t\tvector<pair<int,int> > rp;\n\t\tfor(int i=0;i<a;i++)for(int j=0;j<b;j++)rp.push_back(make_pair(i,j));\n\t\t//random_shuffle(rp.begin(),rp.end());\n\t\tfor(int x=0;x<rp.size();x++){\n\t\t\tif(a==50&&b==50&&c==24&&rand()%15==0)continue;\n\t\t\tint i=rp[x].first;\n\t\t\tint j=rp[x].second;\n\t\t\tif(check(i,j,ret-1e-7)){\n\t\t\t\tdouble left=kakai;\n\t\t\t\tdouble right=ret;\n\t\t\t\tfor(int k=0;k<50;k++){\n\t\t\t\t\tdouble M=(left+right)/2;\n\t\t\t\t\tif(check(i,j,M))right=M;\n\t\t\t\t\telse left=M;\n\t\t\t\t\tif(right-left<1e-8)break;\n\t\t\t\t}\n\t\t\t\tret=min(ret,left);\n\t\t\t}\n\t\t\t//printf(\"%Lf\\n\",ret);\n\t\t}\n\t\tprintf(\"%.12f\\n\",ret);\n\t}\n}", "accuracy": 1, "time_ms": 7960, "memory_kb": 1264, "score_of_the_acc": -1, "final_rank": 14 }, { "submission_id": "aoj_2203_1158775", "code_snippet": "#include <cstdio>\n#include <cmath>\n#include <cstring>\n#include <cstdlib>\n#include <climits>\n#include <ctime>\n#include <queue>\n#include <stack>\n#include <algorithm>\n#include <list>\n#include <vector>\n#include <set>\n#include <map>\n#include <iostream>\n#include <deque>\n#include <complex>\n#include <string>\n#include <iomanip>\n#include <sstream>\n#include <bitset>\n#include <valarray>\n#include <unordered_map>\n#include <iterator>\n#include <assert.h>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned int uint;\ntypedef unsigned char uchar;\ntypedef unsigned long long ull;\ntypedef pair<int, int> pii;\ntypedef pair<ll, ll> pll;\ntypedef vector<int> vi;\n \n#define REP(i,x) for(int i=0;i<(int)(x);i++)\n#define REPS(i,x) for(int i=1;i<=(int)(x);i++)\n#define RREP(i,x) for(int i=((int)(x)-1);i>=0;i--)\n#define RREPS(i,x) for(int i=((int)(x));i>0;i--)\n#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();i++)\n#define RFOR(i,c) for(__typeof((c).rbegin())i=(c).rbegin();i!=(c).rend();i++)\n#define ALL(container) (container).begin(), (container).end()\n#define RALL(container) (container).rbegin(), (container).rend()\n#define SZ(container) ((int)container.size())\n#define mp(a,b) make_pair(a, b)\n#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );\n \ntemplate<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }\ntemplate<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }\ntemplate<class T> ostream& operator<<(ostream &os, const vector<T> &t) {\nos<<\"[\"; FOR(it,t) {if(it!=t.begin()) os<<\",\"; os<<*it;} os<<\"]\"; return os;\n}\ntemplate<class T> ostream& operator<<(ostream &os, const set<T> &t) {\nos<<\"{\"; FOR(it,t) {if(it!=t.begin()) os<<\",\"; os<<*it;} os<<\"}\"; return os;\n}\ntemplate<class S, class T> ostream& operator<<(ostream &os, const pair<S,T> &t) { return os<<\"(\"<<t.first<<\",\"<<t.second<<\")\";}\ntemplate<class S, class T> pair<S,T> operator+(const pair<S,T> &s, const pair<S,T> &t){ return pair<S,T>(s.first+t.first, s.second+t.second);}\ntemplate<class S, class T> pair<S,T> operator-(const pair<S,T> &s, const pair<S,T> &t){ return pair<S,T>(s.first-t.first, s.second-t.second);}\n \nnamespace geom{\n#define X real()\n#define Y imag()\n#define at(i) ((*this)[i])\n#define SELF (*this)\n\tenum {TRUE = 1, FALSE = 0, BORDER = -1};\n\ttypedef int BOOL;\n\ttypedef double R;\n\tconst R INF = 1e8;\n\tR EPS = 1e-6;\n\tconst R PI = 3.1415926535897932384626;\n\tinline int sig(const R &x) { return (abs(x) < EPS ? 0 : x > 0 ? 1 : -1); }\n\tinline BOOL less(const R &x, const R &y) {return sig(x-y) ? x < y : BORDER;}\n\ttypedef complex<R> P;\n\tinline R norm(const P &p){return p.X*p.X+p.Y*p.Y;}\n\tinline R inp(const P& a, const P& b){return (conj(a)*b).X;}\n\tinline R outp(const P& a, const P& b){return (conj(a)*b).Y;}\n\tinline P unit(const P& p){return p/abs(p);}\n\tinline P proj(const P &s, const P &t){return t*inp(s, t)/norm(t);}\n\tinline int ccw(const P &s, const P &t, const P &p, int adv=0){\n\t\tint res = sig(outp(t-s, p-s));\n\t\tif(res || !adv) return res;\n\t\tif(sig(inp(t-s, p-s)) < 0) return -2;\t// p-s-t\n\t\tif(sig(inp(s-t, p-t)) < 0) return 2; // s-t-p\n\t\treturn 0;\t\t\t\t\t\t\t // s-p-t\n\t}\n\t \n\t \n\tstruct L : public vector<P>{ // line\n\t\tL(const P &p1, const P &p2){this->push_back(p1);this->push_back(p2);}\n\t\tL(){}\n\t\tinline P dir()const {return at(1) - at(0);}\n\t\tBOOL online(const P &p)const {return !sig(outp(p-at(0), dir()));}\n\t};\n\tstruct S : public L{\t// segment\n\t\tS(const P &p1, const P &p2):L(p1, p2){}\n\t\tS(){}\n\t\tBOOL online(const P &p)const {\n\t\t\tif(!sig(norm(p - at(0))) || !sig(norm(p - at(1)))) return BORDER;\n\t\t\treturn !sig(outp(p-at(0), dir())) && inp(p-at(0), dir()) > -EPS && inp(p-at(1), -dir()) > -EPS ? TRUE: FALSE;\n\t\t\treturn !sig(abs(at(0)-p) + abs(at(1) - p) - abs(at(0) - at(1)));\n\t\t}\n\t};\n\tP crosspoint(const L &l, const L &m);\n\tstruct G : public vector<P>{\n\t\tG(size_type size=0):vector(size){}\n\t\tS edge(int i)const {return S(at(i), at(i+1 == size() ? 0 : i+1));}\n\t};\n \n\tinline BOOL intersect(const S &s, const L &l){\n\t\treturn (sig(outp(l.dir(), s[0]-l[0])) * sig(outp(l.dir(), s[1]-l[0])) <= 0);\n\t}\n\tinline P crosspoint(const L &l, const L &m){\n\t\tR A = outp(l.dir(), m.dir()), B = outp(l.dir(), l[1] - m[0]);\n\t\tif(!sig(abs(A)) && !sig(abs(B))) return m[0]; // same line\n\t\tif(abs(A) < EPS) assert(false);\n\t\treturn m[0] + B / A * (m[1] - m[0]);\n\t}\n\t \n\tstruct Arrangement{\n\t\tstruct AEdge{\n\t\t\tint u, v, t;\n\t\t\tR cost;\n\t\t\tAEdge(int u=0, int v=0, int t=0, R cost=0)\n\t\t\t\t:u(u), v(v), t(t), cost(cost){}\n\t\t};\n\t\ttypedef vector<vector<AEdge>> AGraph;\n\t\tvector<P> p;\n\t\tAGraph g;\n\t\tArrangement(){}\n\t\tArrangement(vector<S> seg){\n\t\t\tint m = seg.size();\n\t\t\tREP(i, m){\n\t\t\t\tp.push_back(seg[i][0]);\n\t\t\t\tp.push_back(seg[i][1]);\n\t\t\t\tREP(j, i) if(sig(outp(seg[i].dir(), seg[j].dir())) && intersect(seg[i], seg[j]) == TRUE)\n\t\t\t\t\tp.push_back(crosspoint(seg[i], seg[j]));\n\t\t\t}\n\t\t\tsort(ALL(p)); UNIQUE(p);\n\t\t\tint n=p.size();\n\t\t\tg.resize(n);\n\t\t\tREP(i, m){\n\t\t\t\tS &s = seg[i];\n\t\t\t\tvector<pair<R, int>> ps;\n\t\t\t\tREP(j, n) if(s.online(p[j])) ps.emplace_back(norm(p[j] - s[0]), j);\n\t\t\t\tsort(ALL(ps));\n\t\t\t\tREP(j, (int)ps.size()-1){\n\t\t\t\t\tconst int u=ps[j].second;\n\t\t\t\t\tconst int v=ps[j+1].second;\n\t\t\t\t\tg[u].emplace_back(u, v, 0, abs(p[u] - p[v]));\n\t\t\t\t\tg[v].emplace_back(v, u, 0, abs(p[u] - p[v]));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\t \n\t\tint getIdx(P q){\n\t\t\tauto it = lower_bound(ALL(p), q);\n\t\t\tif(it == p.end() || *it != q) return -1;\n\t\t\treturn it - p.begin();\n\t\t}\n\t};\n \n\tstruct DualGraph{\n\t\tstruct DEdge{\n\t\t\tint u, v, f, l;\n\t\t\tR a;\n\t\t\tDEdge(int u, int v, R a):u(u),v(v),f(0),l(0){\n\t\t\t\twhile(PI < a) a -= 2*PI;\n\t\t\t\twhile(a < -PI) a += 2*PI;\n\t\t\t\tthis->a = a;\n\t\t\t}\n\t\t\tbool operator==(const DEdge &opp) const{\n\t\t\t\treturn v==opp.v;\n\t\t\t}\n\t\t\tbool operator<(const DEdge &opp) const{\n\t\t\t\treturn a>opp.a;\n\t\t\t}\n\t\t\tbool operator<(const R &opp) const{\n\t\t\t\treturn a>opp;\n\t\t\t}\n\t\t\tfriend ostream& operator<<(ostream &os, const DEdge &t) { return os<<\"(\"<<t.u<<\",\"<<t.v<<\",\"<<t.a*180/PI<<\")\";}\n\t\t};\n\t\t \n\t\tint n;\n\t\tvector<P> p;\n\t\tvector<vector<DEdge>> g;\n\t\tDualGraph(const vector<P> &p):p(p),g(p.size()),n(p.size()){}\n\t\tvoid add_edge(const int s, const int t){\n\t\t\tR a = arg(p[t]-p[s]);\n\t\t\tg[s].emplace_back(s, t, a);\n\t\t\tg[t].emplace_back(t, s, a > 0 ? a-PI : a+PI);\n\t\t}\n\t\tvoid add_polygon(int s, G &t, R a){\n\t\t\tauto e = lower_bound(ALL(g[s]), a-EPS);\n\t\t\tif(e == g[s].end()) e = g[s].begin();\n\t\t\tif(e->f) return;\n\t\t\te->f = 1;\n\t\t\tt.push_back(p[s]);\n\t\t\tadd_polygon(e->v, t, e->a > 0 ? e->a-PI : e->a+PI);\n\t\t}\n\t\t \n\t\tG dual(){\n\t\t\tREP(i, n){\n\t\t\t\tsort(ALL(g[i]));\n\t\t\t\tUNIQUE(g[i]);\n\t\t\t}\n\t\t\tint s = min_element(ALL(p)) - p.begin();\n\t\t\tG poly;\n\t\t\tadd_polygon(s, poly, -PI*(R).5);\n\t\t\treturn poly;\n\t\t}\n\t};\n \n#undef SELF\n#undef at\n}\n \nusing namespace geom;\n \nnamespace std{\n\tbool operator<(const P &a, const P &b){return sig(a.X-b.X) ? a.X < b.X : a.Y+EPS < b.Y;}\n\tbool operator==(const P &a, const P &b){return abs(a-b) < EPS;}\n\tistream& operator>>(istream &is, P &p){R x,y;is>>x>>y;p=P(x, y);return is;}\n}\n \nint n, m, k;\nvector<P> vil;\nstruct MSQ : public G{\n\tMSQ(){}\n\tvector<P> p;\n\tvector<S> s;\n\tint m, k;\n\tMSQ(int m, int k):m(m), k(k){\n\t\tREP(i, m) p.push_back(polar((R)1, 2*PI*i/m + PI*(R).5));\n\t\tREP(i, m) s.emplace_back(p[i], p[(i+k)%m]);\n\t\tArrangement a(s);\n\t\tDualGraph dg(a.p);\n\t\tREP(i, a.g.size())REP(j, a.g[i].size()){\n\t\t\tint u = a.g[i][j].u;\n\t\t\tint v = a.g[i][j].v;\n\t\t\tif(u < v) dg.add_edge(u, v);\n\t\t}\n\t\t(G &)(*this) = dg.dual();\n\t\treverse(this->begin(), this->end());\n\t}\n\tvoid copy(R r, P c, MSQ &msq)const {\n\t\tmsq.resize(size());\n\t\tmsq.p.resize(p.size());\n\t\tmsq.s.resize(s.size());\n\t\tmsq.m = m;\n\t\tmsq.k = k;\n\t\tREP(i, size()) msq[i] = at(i)*r + c;\n\t\tREP(i, p.size()) msq.p[i] = p[i]*r + c;\n\t\tREP(i, s.size()) msq.s[i] = S(msq.p[i], msq.p[(i+k)%m]);\n\t}\n\tconst S &segment(int i)const {\n\t\treturn s[i];\n\t}\n};\nint convex_contains(const MSQ &msq, const P &g, const P &p) {\n\tconst int n = msq.size();\n\tint a = 0, b = n;\n\tconst P pg = p-g;\n\twhile (a+1 < b) { // invariant: c is in fan g-P[a]-P[b]\n\t\tint c = (a + b) / 2;\n\t\tif (outp(msq[a]-g, pg) > 0 && outp(msq[c]-g, pg) < 0) b = c;\n\t\telse a = c;\n\t}\n\tb %= n;\n\tif (outp(msq[a] - p, msq[b] - p) < -EPS) return 0;\n\treturn 1;\n}\nbool check(const MSQ & temp, const R &r, const int &i, const int &j){\n\tMSQ msq;\n\tP gp = vil[i] - temp.segment(j)[0]*r;\n\ttemp.copy(r, gp, msq);\n\tconst S &l = msq.segment(j);\n\tvector<P> p;\n\tP b(0), u = l.dir();\n\tif(u < b) swap(b, u);\n\tRREP(i, n){\n\t\tS l2(vil[i], vil[i] + l.dir());\n\t\tint f = 0;\n\t\tP ll(INF, INF), rr(-INF, -INF);\n\t\tREP(j, m){\n\t\t\tconst S &s = msq.segment(j);\n\t\t\tif(intersect(s, l2)){\n\t\t\t\tconst P q = crosspoint(s, l2) - l2[0];\n\t\t\t\tp.push_back(q);\n\t\t\t\tll = min(ll, q);\n\t\t\t\trr = max(rr, q);\n\t\t\t\tf = 1;\n\t\t\t}\n\t\t}\n\t\tu = min(rr, u);\n\t\tb = max(ll, b);\n\t\tif(!f || u < b) return false;\n\t}\n\tFOR(q, p){\n\t\tif(*q < b || u < *q) continue;\n\t\tif([&](){\n\t\t\tREP(i, n) if(!convex_contains(msq, gp, vil[i] + *q)) return 0;\n\t\t\treturn 1;\n\t\t}()) return true;\n\t}\n\treturn false;\n}\n \nint main(){\n\tios::sync_with_stdio(false);\n\twhile(cin >> n >> m >> k, n){\n\t\tMSQ temp(m, k);\n\t\tvil = vector<P>(n);\n\t\tREP(i, n) cin >> vil[i];\n\t\tR best = 2000;\n\t\tREP(i, n)REP(j, m){\n\t\t\tif(!check(temp, best-EPS, i, j)) continue;\n\t\t\tR l=1., r = best;\n\t\t\twhile(r-l>1e-6){\n\t\t\t\tR m = (l+r)*(R).5;\n\t\t\t\tif(check(temp, m, i, j)) r = m;\n\t\t\t\telse l = m;\n\t\t\t}\n\t\t\tbest = r;\n\t\t}\n\t\tprintf(\"%.10f\\n\", (double)best);\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 2000, "memory_kb": 1648, "score_of_the_acc": -0.163, "final_rank": 1 }, { "submission_id": "aoj_2203_1158772", "code_snippet": "#include <cstdio>\n#include <cmath>\n#include <cstring>\n#include <cstdlib>\n#include <climits>\n#include <ctime>\n#include <queue>\n#include <stack>\n#include <algorithm>\n#include <list>\n#include <vector>\n#include <set>\n#include <map>\n#include <iostream>\n#include <deque>\n#include <complex>\n#include <string>\n#include <iomanip>\n#include <sstream>\n#include <bitset>\n#include <valarray>\n#include <unordered_map>\n#include <iterator>\n#include <assert.h>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned int uint;\ntypedef unsigned char uchar;\ntypedef unsigned long long ull;\ntypedef pair<int, int> pii;\ntypedef pair<ll, ll> pll;\ntypedef vector<int> vi;\n \n#define REP(i,x) for(int i=0;i<(int)(x);i++)\n#define REPS(i,x) for(int i=1;i<=(int)(x);i++)\n#define RREP(i,x) for(int i=((int)(x)-1);i>=0;i--)\n#define RREPS(i,x) for(int i=((int)(x));i>0;i--)\n#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();i++)\n#define RFOR(i,c) for(__typeof((c).rbegin())i=(c).rbegin();i!=(c).rend();i++)\n#define ALL(container) (container).begin(), (container).end()\n#define RALL(container) (container).rbegin(), (container).rend()\n#define SZ(container) ((int)container.size())\n#define mp(a,b) make_pair(a, b)\n#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );\n \ntemplate<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }\ntemplate<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }\ntemplate<class T> ostream& operator<<(ostream &os, const vector<T> &t) {\nos<<\"[\"; FOR(it,t) {if(it!=t.begin()) os<<\",\"; os<<*it;} os<<\"]\"; return os;\n}\ntemplate<class T> ostream& operator<<(ostream &os, const set<T> &t) {\nos<<\"{\"; FOR(it,t) {if(it!=t.begin()) os<<\",\"; os<<*it;} os<<\"}\"; return os;\n}\ntemplate<class S, class T> ostream& operator<<(ostream &os, const pair<S,T> &t) { return os<<\"(\"<<t.first<<\",\"<<t.second<<\")\";}\ntemplate<class S, class T> pair<S,T> operator+(const pair<S,T> &s, const pair<S,T> &t){ return pair<S,T>(s.first+t.first, s.second+t.second);}\ntemplate<class S, class T> pair<S,T> operator-(const pair<S,T> &s, const pair<S,T> &t){ return pair<S,T>(s.first-t.first, s.second-t.second);}\n \nnamespace geom{\n#define X real()\n#define Y imag()\n#define at(i) ((*this)[i])\n#define SELF (*this)\n\tenum {TRUE = 1, FALSE = 0, BORDER = -1};\n\ttypedef int BOOL;\n\ttypedef double R;\n\tconst R INF = 1e8;\n\tR EPS = 1e-6;\n\tconst R PI = 3.1415926535897932384626;\n\tinline int sig(const R &x) { return (abs(x) < EPS ? 0 : x > 0 ? 1 : -1); }\n\tinline BOOL less(const R &x, const R &y) {return sig(x-y) ? x < y : BORDER;}\n\ttypedef complex<R> P;\n\tinline R norm(const P &p){return p.X*p.X+p.Y*p.Y;}\n\tinline R inp(const P& a, const P& b){return (conj(a)*b).X;}\n\tinline R outp(const P& a, const P& b){return (conj(a)*b).Y;}\n\tinline P unit(const P& p){return p/abs(p);}\n\tinline P proj(const P &s, const P &t){return t*inp(s, t)/norm(t);}\n\tinline int ccw(const P &s, const P &t, const P &p, int adv=0){\n\t\tint res = sig(outp(t-s, p-s));\n\t\tif(res || !adv) return res;\n\t\tif(sig(inp(t-s, p-s)) < 0) return -2;\t// p-s-t\n\t\tif(sig(inp(s-t, p-t)) < 0) return 2; // s-t-p\n\t\treturn 0;\t\t\t\t\t\t\t // s-p-t\n\t}\n\t \n\t \n\tstruct L : public vector<P>{ // line\n\t\tL(const P &p1, const P &p2){this->push_back(p1);this->push_back(p2);}\n\t\tL(){}\n\t\tinline P dir()const {return at(1) - at(0);}\n\t\tBOOL online(const P &p)const {return !sig(outp(p-at(0), dir()));}\n\t};\n\tstruct S : public L{\t// segment\n\t\tS(const P &p1, const P &p2):L(p1, p2){}\n\t\tS(){}\n\t\tBOOL online(const P &p)const {\n\t\t\tif(!sig(norm(p - at(0))) || !sig(norm(p - at(1)))) return BORDER;\n\t\t\treturn !sig(outp(p-at(0), dir())) && inp(p-at(0), dir()) > -EPS && inp(p-at(1), -dir()) > -EPS ? TRUE: FALSE;\n\t\t\treturn !sig(abs(at(0)-p) + abs(at(1) - p) - abs(at(0) - at(1)));\n\t\t}\n\t};\n\tP crosspoint(const L &l, const L &m);\n\tstruct G : public vector<P>{\n\t\tG(size_type size=0):vector(size){}\n\t\tS edge(int i)const {return S(at(i), at(i+1 == size() ? 0 : i+1));}\n\t};\n \n\tinline BOOL intersect(const S &s, const L &l){\n\t\treturn (sig(outp(l.dir(), s[0]-l[0])) * sig(outp(l.dir(), s[1]-l[0])) <= 0);\n\t}\n\tinline P crosspoint(const L &l, const L &m){\n\t\tR A = outp(l.dir(), m.dir()), B = outp(l.dir(), l[1] - m[0]);\n\t\tif(!sig(abs(A)) && !sig(abs(B))) return m[0]; // same line\n\t\tif(abs(A) < EPS) assert(false);\n\t\treturn m[0] + B / A * (m[1] - m[0]);\n\t}\n\t \n\tstruct Arrangement{\n\t\tstruct AEdge{\n\t\t\tint u, v, t;\n\t\t\tR cost;\n\t\t\tAEdge(int u=0, int v=0, int t=0, R cost=0)\n\t\t\t\t:u(u), v(v), t(t), cost(cost){}\n\t\t};\n\t\ttypedef vector<vector<AEdge>> AGraph;\n\t\tvector<P> p;\n\t\tAGraph g;\n\t\tArrangement(){}\n\t\tArrangement(vector<S> seg){\n\t\t\tint m = seg.size();\n\t\t\tREP(i, m){\n\t\t\t\tp.push_back(seg[i][0]);\n\t\t\t\tp.push_back(seg[i][1]);\n\t\t\t\tREP(j, i) if(sig(outp(seg[i].dir(), seg[j].dir())) && intersect(seg[i], seg[j]) == TRUE)\n\t\t\t\t\tp.push_back(crosspoint(seg[i], seg[j]));\n\t\t\t}\n\t\t\tsort(ALL(p)); UNIQUE(p);\n\t\t\tint n=p.size();\n\t\t\tg.resize(n);\n\t\t\tREP(i, m){\n\t\t\t\tS &s = seg[i];\n\t\t\t\tvector<pair<R, int>> ps;\n\t\t\t\tREP(j, n) if(s.online(p[j])) ps.emplace_back(norm(p[j] - s[0]), j);\n\t\t\t\tsort(ALL(ps));\n\t\t\t\tREP(j, (int)ps.size()-1){\n\t\t\t\t\tconst int u=ps[j].second;\n\t\t\t\t\tconst int v=ps[j+1].second;\n\t\t\t\t\tg[u].emplace_back(u, v, 0, abs(p[u] - p[v]));\n\t\t\t\t\tg[v].emplace_back(v, u, 0, abs(p[u] - p[v]));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\t \n\t\tint getIdx(P q){\n\t\t\tauto it = lower_bound(ALL(p), q);\n\t\t\tif(it == p.end() || *it != q) return -1;\n\t\t\treturn it - p.begin();\n\t\t}\n\t};\n \n\tstruct DualGraph{\n\t\tstruct DEdge{\n\t\t\tint u, v, f, l;\n\t\t\tR a;\n\t\t\tDEdge(int u, int v, R a):u(u),v(v),f(0),l(0){\n\t\t\t\twhile(PI < a) a -= 2*PI;\n\t\t\t\twhile(a < -PI) a += 2*PI;\n\t\t\t\tthis->a = a;\n\t\t\t}\n\t\t\tbool operator==(const DEdge &opp) const{\n\t\t\t\treturn v==opp.v;\n\t\t\t}\n\t\t\tbool operator<(const DEdge &opp) const{\n\t\t\t\treturn a>opp.a;\n\t\t\t}\n\t\t\tbool operator<(const R &opp) const{\n\t\t\t\treturn a>opp;\n\t\t\t}\n\t\t\tfriend ostream& operator<<(ostream &os, const DEdge &t) { return os<<\"(\"<<t.u<<\",\"<<t.v<<\",\"<<t.a*180/PI<<\")\";}\n\t\t};\n\t\t \n\t\tint n;\n\t\tvector<P> p;\n\t\tvector<vector<DEdge>> g;\n\t\tDualGraph(const vector<P> &p):p(p),g(p.size()),n(p.size()){}\n\t\tvoid add_edge(const int s, const int t){\n\t\t\tR a = arg(p[t]-p[s]);\n\t\t\tg[s].emplace_back(s, t, a);\n\t\t\tg[t].emplace_back(t, s, a > 0 ? a-PI : a+PI);\n\t\t}\n\t\tvoid add_polygon(int s, G &t, R a){\n\t\t\tauto e = lower_bound(ALL(g[s]), a-EPS);\n\t\t\tif(e == g[s].end()) e = g[s].begin();\n\t\t\tif(e->f) return;\n\t\t\te->f = 1;\n\t\t\tt.push_back(p[s]);\n\t\t\tadd_polygon(e->v, t, e->a > 0 ? e->a-PI : e->a+PI);\n\t\t}\n\t\t \n\t\tG dual(){\n\t\t\tREP(i, n){\n\t\t\t\tsort(ALL(g[i]));\n\t\t\t\tUNIQUE(g[i]);\n\t\t\t}\n\t\t\tint s = min_element(ALL(p)) - p.begin();\n\t\t\tG poly;\n\t\t\tadd_polygon(s, poly, -PI*(R).5);\n\t\t\treturn poly;\n\t\t}\n\t};\n \n#undef SELF\n#undef at\n}\n \nusing namespace geom;\n \nnamespace std{\n\tbool operator<(const P &a, const P &b){return sig(a.X-b.X) ? a.X < b.X : a.Y+EPS < b.Y;}\n\tbool operator==(const P &a, const P &b){return abs(a-b) < EPS;}\n\tistream& operator>>(istream &is, P &p){R x,y;is>>x>>y;p=P(x, y);return is;}\n}\n \nint n, m, k;\nvector<P> vil;\nstruct MSQ : public G{\n\tMSQ(){}\n\tvector<P> p;\n\tvector<S> s;\n\tint m, k;\n\tMSQ(int m, int k):m(m), k(k){\n\t\tREP(i, m) p.push_back(polar((R)1, 2*PI*i/m + PI*(R).5));\n\t\tREP(i, m) s.emplace_back(p[i], p[(i+k)%m]);\n\t\tArrangement a(s);\n\t\tDualGraph dg(a.p);\n\t\tREP(i, a.g.size())REP(j, a.g[i].size()){\n\t\t\tint u = a.g[i][j].u;\n\t\t\tint v = a.g[i][j].v;\n\t\t\tif(u < v) dg.add_edge(u, v);\n\t\t}\n\t\t(G &)(*this) = dg.dual();\n\t\treverse(this->begin(), this->end());\n\t}\n\tvoid copy(R r, P c, MSQ &msq)const {\n\t\tmsq.resize(size());\n\t\tmsq.p.resize(p.size());\n\t\tmsq.s.resize(s.size());\n\t\tmsq.m = m;\n\t\tmsq.k = k;\n\t\tREP(i, size()) msq[i] = at(i)*r + c;\n\t\tREP(i, p.size()) msq.p[i] = p[i]*r + c;\n\t\tREP(i, s.size()) msq.s[i] = S(msq.p[i], msq.p[(i+k)%m]);\n\t}\n\tS segment(int i)const {\n\t\treturn s[i];\n\t}\n};\nint convex_contains(const MSQ &msq, const P &g, const P &p) {\n\tconst int n = msq.size();\n\tint a = 0, b = n;\n\tconst P pg = p-g;\n\twhile (a+1 < b) { // invariant: c is in fan g-P[a]-P[b]\n\t\tint c = (a + b) / 2;\n\t\tif (outp(msq[a]-g, pg) > 0 && outp(msq[c]-g, pg) < 0) b = c;\n\t\telse a = c;\n\t}\n\tb %= n;\n\tif (outp(msq[a] - p, msq[b] - p) < -EPS) return 0;\n\treturn 1;\n}\nbool check(const MSQ & temp, R r, int i, int j){\n\tMSQ msq;\n\tP gp = vil[i] - temp.segment(j)[0]*r;\n\ttemp.copy(r, gp, msq);\n\tL l = msq.segment(j);\n\tvector<P> p;\n\tP b(0), u = l.dir();\n\tif(u < b) swap(b, u);\n\tRREP(i, n){\n\t\tS l2(vil[i], vil[i] + l.dir());\n\t\tint f = 0;\n\t\tP ll(INF, INF), rr(-INF, -INF);\n\t\tREP(j, m){\n\t\t\tconst S s = msq.segment(j);\n\t\t\tif(intersect(s, l2)){\n\t\t\t\tconst P q = crosspoint(s, l2) - l2[0];\n\t\t\t\tp.push_back(q);\n\t\t\t\tll = min(ll, q);\n\t\t\t\trr = max(rr, q);\n\t\t\t\tf = 1;\n\t\t\t}\n\t\t}\n\t\tu = min(rr, u);\n\t\tb = max(ll, b);\n\t\tif(!f || u < b) return false;\n\t}\n\tFOR(q, p){\n\t\tif(*q < b || u < *q) continue;\n\t\tif([&](){\n\t\t\tREP(i, n) if(!convex_contains(msq, gp, vil[i] + *q)) return 0;\n\t\t\treturn 1;\n\t\t}()) return true;\n\t}\n\treturn false;\n}\n \nint main(){\n\tios::sync_with_stdio(false);\n\twhile(cin >> n >> m >> k, n){\n\t\tMSQ temp(m, k);\n\t\tvil = vector<P>(n);\n\t\tREP(i, n) cin >> vil[i];\n\t\tR best = 2000;\n\t\tREP(i, n)REP(j, m){\n\t\t\tif(!check(temp, best-EPS, i, j)) continue;\n\t\t\tR l=1., r = best;\n\t\t\twhile(r-l>1e-6){\n\t\t\t\tR m = (l+r)*(R).5;\n\t\t\t\tif(check(temp, m, i, j)) r = m;\n\t\t\t\telse l = m;\n\t\t\t}\n\t\t\tbest = r;\n\t\t}\n\t\tprintf(\"%.10f\\n\", (double)best);\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 2490, "memory_kb": 1648, "score_of_the_acc": -0.2418, "final_rank": 3 }, { "submission_id": "aoj_2203_1158767", "code_snippet": "#include <cstdio>\n#include <cmath>\n#include <cstring>\n#include <cstdlib>\n#include <climits>\n#include <ctime>\n#include <queue>\n#include <stack>\n#include <algorithm>\n#include <list>\n#include <vector>\n#include <set>\n#include <map>\n#include <iostream>\n#include <deque>\n#include <complex>\n#include <string>\n#include <iomanip>\n#include <sstream>\n#include <bitset>\n#include <valarray>\n#include <unordered_map>\n#include <iterator>\n#include <assert.h>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned int uint;\ntypedef unsigned char uchar;\ntypedef unsigned long long ull;\ntypedef pair<int, int> pii;\ntypedef pair<ll, ll> pll;\ntypedef vector<int> vi;\n \n#define REP(i,x) for(int i=0;i<(int)(x);i++)\n#define REPS(i,x) for(int i=1;i<=(int)(x);i++)\n#define RREP(i,x) for(int i=((int)(x)-1);i>=0;i--)\n#define RREPS(i,x) for(int i=((int)(x));i>0;i--)\n#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();i++)\n#define RFOR(i,c) for(__typeof((c).rbegin())i=(c).rbegin();i!=(c).rend();i++)\n#define ALL(container) (container).begin(), (container).end()\n#define RALL(container) (container).rbegin(), (container).rend()\n#define SZ(container) ((int)container.size())\n#define mp(a,b) make_pair(a, b)\n#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );\n \ntemplate<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }\ntemplate<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }\ntemplate<class T> ostream& operator<<(ostream &os, const vector<T> &t) {\nos<<\"[\"; FOR(it,t) {if(it!=t.begin()) os<<\",\"; os<<*it;} os<<\"]\"; return os;\n}\ntemplate<class T> ostream& operator<<(ostream &os, const set<T> &t) {\nos<<\"{\"; FOR(it,t) {if(it!=t.begin()) os<<\",\"; os<<*it;} os<<\"}\"; return os;\n}\ntemplate<class S, class T> ostream& operator<<(ostream &os, const pair<S,T> &t) { return os<<\"(\"<<t.first<<\",\"<<t.second<<\")\";}\ntemplate<class S, class T> pair<S,T> operator+(const pair<S,T> &s, const pair<S,T> &t){ return pair<S,T>(s.first+t.first, s.second+t.second);}\ntemplate<class S, class T> pair<S,T> operator-(const pair<S,T> &s, const pair<S,T> &t){ return pair<S,T>(s.first-t.first, s.second-t.second);}\n \nnamespace geom{\n#define X real()\n#define Y imag()\n#define at(i) ((*this)[i])\n#define SELF (*this)\n\tenum {TRUE = 1, FALSE = 0, BORDER = -1};\n\ttypedef int BOOL;\n\ttypedef double R;\n\tconst R INF = 1e8;\n\tR EPS = 1e-6;\n\tconst R PI = 3.1415926535897932384626;\n\tinline int sig(const R &x) { return (abs(x) < EPS ? 0 : x > 0 ? 1 : -1); }\n\tinline BOOL less(const R &x, const R &y) {return sig(x-y) ? x < y : BORDER;}\n\ttypedef complex<R> P;\n\tinline R norm(const P &p){return p.X*p.X+p.Y*p.Y;}\n\tinline R inp(const P& a, const P& b){return (conj(a)*b).X;}\n\tinline R outp(const P& a, const P& b){return (conj(a)*b).Y;}\n\tinline P unit(const P& p){return p/abs(p);}\n\tinline P proj(const P &s, const P &t){return t*inp(s, t)/norm(t);}\n\tinline int ccw(const P &s, const P &t, const P &p, int adv=0){\n\t\tint res = sig(outp(t-s, p-s));\n\t\tif(res || !adv) return res;\n\t\tif(sig(inp(t-s, p-s)) < 0) return -2;\t// p-s-t\n\t\tif(sig(inp(s-t, p-t)) < 0) return 2; // s-t-p\n\t\treturn 0;\t\t\t\t\t\t\t // s-p-t\n\t}\n\t \n\t \n\tstruct L : public vector<P>{ // line\n\t\tL(const P &p1, const P &p2){this->push_back(p1);this->push_back(p2);}\n\t\tL(){}\n\t\tinline P dir()const {return at(1) - at(0);}\n\t\tBOOL online(const P &p)const {return !sig(outp(p-at(0), dir()));}\n\t};\n\tstruct S : public L{\t// segment\n\t\tS(const P &p1, const P &p2):L(p1, p2){}\n\t\tS(){}\n\t\tBOOL online(const P &p)const {\n\t\t\tif(!sig(norm(p - at(0))) || !sig(norm(p - at(1)))) return BORDER;\n\t\t\treturn !sig(outp(p-at(0), dir())) && inp(p-at(0), dir()) > -EPS && inp(p-at(1), -dir()) > -EPS ? TRUE: FALSE;\n\t\t\treturn !sig(abs(at(0)-p) + abs(at(1) - p) - abs(at(0) - at(1)));\n\t\t}\n\t};\n\tP crosspoint(const L &l, const L &m);\n\tstruct G : public vector<P>{\n\t\tG(size_type size=0):vector(size){}\n\t\tS edge(int i)const {return S(at(i), at(i+1 == size() ? 0 : i+1));}\n\t};\n \n\tinline BOOL intersect(const S &s, const L &l){\n\t\treturn (sig(outp(l.dir(), s[0]-l[0])) * sig(outp(l.dir(), s[1]-l[0])) <= 0);\n\t}\n\tinline P crosspoint(const L &l, const L &m){\n\t\tR A = outp(l.dir(), m.dir()), B = outp(l.dir(), l[1] - m[0]);\n\t\tif(!sig(abs(A)) && !sig(abs(B))) return m[0]; // same line\n\t\tif(abs(A) < EPS) assert(false);\n\t\treturn m[0] + B / A * (m[1] - m[0]);\n\t}\n\t \n\tstruct Arrangement{\n\t\tstruct AEdge{\n\t\t\tint u, v, t;\n\t\t\tR cost;\n\t\t\tAEdge(int u=0, int v=0, int t=0, R cost=0)\n\t\t\t\t:u(u), v(v), t(t), cost(cost){}\n\t\t};\n\t\ttypedef vector<vector<AEdge>> AGraph;\n\t\tvector<P> p;\n\t\tAGraph g;\n\t\tArrangement(){}\n\t\tArrangement(vector<S> seg){\n\t\t\tint m = seg.size();\n\t\t\tREP(i, m){\n\t\t\t\tp.push_back(seg[i][0]);\n\t\t\t\tp.push_back(seg[i][1]);\n\t\t\t\tREP(j, i) if(sig(outp(seg[i].dir(), seg[j].dir())) && intersect(seg[i], seg[j]) == TRUE)\n\t\t\t\t\tp.push_back(crosspoint(seg[i], seg[j]));\n\t\t\t}\n\t\t\tsort(ALL(p)); UNIQUE(p);\n\t\t\tint n=p.size();\n\t\t\tg.resize(n);\n\t\t\tREP(i, m){\n\t\t\t\tS &s = seg[i];\n\t\t\t\tvector<pair<R, int>> ps;\n\t\t\t\tREP(j, n) if(s.online(p[j])) ps.emplace_back(norm(p[j] - s[0]), j);\n\t\t\t\tsort(ALL(ps));\n\t\t\t\tREP(j, (int)ps.size()-1){\n\t\t\t\t\tconst int u=ps[j].second;\n\t\t\t\t\tconst int v=ps[j+1].second;\n\t\t\t\t\tg[u].emplace_back(u, v, 0, abs(p[u] - p[v]));\n\t\t\t\t\tg[v].emplace_back(v, u, 0, abs(p[u] - p[v]));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\t \n\t\tint getIdx(P q){\n\t\t\tauto it = lower_bound(ALL(p), q);\n\t\t\tif(it == p.end() || *it != q) return -1;\n\t\t\treturn it - p.begin();\n\t\t}\n\t};\n \n\tstruct DualGraph{\n\t\tstruct DEdge{\n\t\t\tint u, v, f, l;\n\t\t\tR a;\n\t\t\tDEdge(int u, int v, R a):u(u),v(v),f(0),l(0){\n\t\t\t\twhile(PI < a) a -= 2*PI;\n\t\t\t\twhile(a < -PI) a += 2*PI;\n\t\t\t\tthis->a = a;\n\t\t\t}\n\t\t\tbool operator==(const DEdge &opp) const{\n\t\t\t\treturn v==opp.v;\n\t\t\t}\n\t\t\tbool operator<(const DEdge &opp) const{\n\t\t\t\treturn a>opp.a;\n\t\t\t}\n\t\t\tbool operator<(const R &opp) const{\n\t\t\t\treturn a>opp;\n\t\t\t}\n\t\t\tfriend ostream& operator<<(ostream &os, const DEdge &t) { return os<<\"(\"<<t.u<<\",\"<<t.v<<\",\"<<t.a*180/PI<<\")\";}\n\t\t};\n\t\t \n\t\tint n;\n\t\tvector<P> p;\n\t\tvector<vector<DEdge>> g;\n\t\tDualGraph(const vector<P> &p):p(p),g(p.size()),n(p.size()){}\n\t\tvoid add_edge(const int s, const int t){\n\t\t\tR a = arg(p[t]-p[s]);\n\t\t\tg[s].emplace_back(s, t, a);\n\t\t\tg[t].emplace_back(t, s, a > 0 ? a-PI : a+PI);\n\t\t}\n\t\tvoid add_polygon(int s, G &t, R a){\n\t\t\tauto e = lower_bound(ALL(g[s]), a-EPS);\n\t\t\tif(e == g[s].end()) e = g[s].begin();\n\t\t\tif(e->f) return;\n\t\t\te->f = 1;\n\t\t\tt.push_back(p[s]);\n\t\t\tadd_polygon(e->v, t, e->a > 0 ? e->a-PI : e->a+PI);\n\t\t}\n\t\t \n\t\tG dual(){\n\t\t\tREP(i, n){\n\t\t\t\tsort(ALL(g[i]));\n\t\t\t\tUNIQUE(g[i]);\n\t\t\t}\n\t\t\tint s = min_element(ALL(p)) - p.begin();\n\t\t\tG poly;\n\t\t\tadd_polygon(s, poly, -PI*(R).5);\n\t\t\treturn poly;\n\t\t}\n\t};\n \n#undef SELF\n#undef at\n}\n \nusing namespace geom;\n \nnamespace std{\n\tbool operator<(const P &a, const P &b){return sig(a.X-b.X) ? a.X < b.X : a.Y+EPS < b.Y;}\n\tbool operator==(const P &a, const P &b){return abs(a-b) < EPS;}\n\tistream& operator>>(istream &is, P &p){R x,y;is>>x>>y;p=P(x, y);return is;}\n}\n \nint n, m, k;\nstruct MSQ : public G{\n\tMSQ(){}\n\tvector<P> p;\n\tvector<S> s;\n\tint m, k;\n\tMSQ(int m, int k):m(m), k(k){\n\t\tREP(i, m) p.push_back(polar((R)1, 2*PI*i/m + PI*(R).5));\n\t\tREP(i, m) s.emplace_back(p[i], p[(i+k)%m]);\n\t\tArrangement a(s);\n\t\tDualGraph dg(a.p);\n\t\tREP(i, a.g.size())REP(j, a.g[i].size()){\n\t\t\tint u = a.g[i][j].u;\n\t\t\tint v = a.g[i][j].v;\n\t\t\tif(u < v) dg.add_edge(u, v);\n\t\t}\n\t\t(G &)(*this) = dg.dual();\n\t\treverse(this->begin(), this->end());\n\t}\n\tvoid copy(R r, P c, MSQ &msq)const {\n\t\tmsq.resize(size());\n\t\tmsq.p.resize(p.size());\n\t\tmsq.s.resize(s.size());\n\t\tmsq.m = m;\n\t\tmsq.k = k;\n\t\tREP(i, size()) msq[i] = at(i)*r + c;\n\t\tREP(i, p.size()) msq.p[i] = p[i]*r + c;\n\t\tREP(i, s.size()) msq.s[i] = S(msq.p[i], msq.p[(i+k)%m]);\n\t}\n\tS segment(int i)const {\n\t\treturn s[i];\n\t}\n};\nint convex_contains(const MSQ &msq, const P &g, const P &p) {\n\tconst int n = msq.size();\n\tint a = 0, b = n;\n\tconst P pg = p-g;\n\twhile (a+1 < b) { // invariant: c is in fan g-P[a]-P[b]\n\t\tint c = (a + b) / 2;\n\t\tif (outp(msq[a]-g, pg) > 0 && outp(msq[c]-g, pg) < 0) b = c;\n\t\telse a = c;\n\t}\n\tb %= n;\n\tif (outp(msq[a] - p, msq[b] - p) < -EPS) return 0;\n\treturn 1;\n}\nbool check(const MSQ & temp, R r, const vector<P> &vil, int i, int j){\n\tMSQ msq;\n\tP gp = vil[i] - temp.segment(j)[0]*r;\n\ttemp.copy(r, gp, msq);\n\tL l = msq.segment(j);\n\tvector<P> p;\n\tP b(0), u = l.dir();\n\tif(u < b) swap(b, u);\n\tREP(i, n){\n\t\tS l2(vil[i], vil[i] + l.dir());\n\t\tint f = 0;\n\t\tP ll(INF, INF), rr(-INF, -INF);\n\t\tREP(j, m){\n\t\t\tconst S s = msq.segment(j);\n\t\t\tif(intersect(s, l2)){\n\t\t\t\tconst P q = crosspoint(s, l2) - l2[0];\n\t\t\t\tp.push_back(q);\n\t\t\t\tll = min(ll, q);\n\t\t\t\trr = max(rr, q);\n\t\t\t\tf = 1;\n\t\t\t}\n\t\t}\n\t\tu = min(rr, u);\n\t\tb = max(ll, b);\n\t\tif(!f || u < b) return false;\n\t}\n\tFOR(q, p){\n\t\tif(*q < b || u < *q) continue;\n\t\tif([&](){\n\t\t\tREP(i, n) if(!convex_contains(msq, gp, vil[i] + *q)) return 0;\n\t\t\treturn 1;\n\t\t}()) return true;\n\t}\n\treturn false;\n}\n \nint main(){\n\tios::sync_with_stdio(false);\n\twhile(cin >> n >> m >> k, n){\n\t\tMSQ temp(m, k);\n\t\tvector<P> vil(n);\n\t\tREP(i, n) cin >> vil[i];\n\t\tR best = 2000;\n\t\tREP(i, n)REP(j, m){\n\t\t\tif(!check(temp, best-EPS, vil, i, j)) continue;\n\t\t\tR l=1., r = best;\n\t\t\twhile(r-l>1e-6){\n\t\t\t\tR m = (l+r)*(R).5;\n\t\t\t\tif(check(temp, m, vil, i, j)) r = m;\n\t\t\t\telse l = m;\n\t\t\t}\n\t\t\tbest = r;\n\t\t}\n\t\tprintf(\"%.10f\\n\", (double)best);\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 2500, "memory_kb": 1648, "score_of_the_acc": -0.2434, "final_rank": 4 }, { "submission_id": "aoj_2203_1158764", "code_snippet": "#include <cstdio>\n#include <cmath>\n#include <cstring>\n#include <cstdlib>\n#include <climits>\n#include <ctime>\n#include <queue>\n#include <stack>\n#include <algorithm>\n#include <list>\n#include <vector>\n#include <set>\n#include <map>\n#include <iostream>\n#include <deque>\n#include <complex>\n#include <string>\n#include <iomanip>\n#include <sstream>\n#include <bitset>\n#include <valarray>\n#include <unordered_map>\n#include <iterator>\n#include <assert.h>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned int uint;\ntypedef unsigned char uchar;\ntypedef unsigned long long ull;\ntypedef pair<int, int> pii;\ntypedef pair<ll, ll> pll;\ntypedef vector<int> vi;\n \n#define REP(i,x) for(int i=0;i<(int)(x);i++)\n#define REPS(i,x) for(int i=1;i<=(int)(x);i++)\n#define RREP(i,x) for(int i=((int)(x)-1);i>=0;i--)\n#define RREPS(i,x) for(int i=((int)(x));i>0;i--)\n#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();i++)\n#define RFOR(i,c) for(__typeof((c).rbegin())i=(c).rbegin();i!=(c).rend();i++)\n#define ALL(container) (container).begin(), (container).end()\n#define RALL(container) (container).rbegin(), (container).rend()\n#define SZ(container) ((int)container.size())\n#define mp(a,b) make_pair(a, b)\n#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );\n \ntemplate<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }\ntemplate<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }\ntemplate<class T> ostream& operator<<(ostream &os, const vector<T> &t) {\nos<<\"[\"; FOR(it,t) {if(it!=t.begin()) os<<\",\"; os<<*it;} os<<\"]\"; return os;\n}\ntemplate<class T> ostream& operator<<(ostream &os, const set<T> &t) {\nos<<\"{\"; FOR(it,t) {if(it!=t.begin()) os<<\",\"; os<<*it;} os<<\"}\"; return os;\n}\ntemplate<class S, class T> ostream& operator<<(ostream &os, const pair<S,T> &t) { return os<<\"(\"<<t.first<<\",\"<<t.second<<\")\";}\ntemplate<class S, class T> pair<S,T> operator+(const pair<S,T> &s, const pair<S,T> &t){ return pair<S,T>(s.first+t.first, s.second+t.second);}\ntemplate<class S, class T> pair<S,T> operator-(const pair<S,T> &s, const pair<S,T> &t){ return pair<S,T>(s.first-t.first, s.second-t.second);}\n \nnamespace geom{\n#define X real()\n#define Y imag()\n#define at(i) ((*this)[i])\n#define SELF (*this)\n\tenum {TRUE = 1, FALSE = 0, BORDER = -1};\n\ttypedef int BOOL;\n\ttypedef double R;\n\tconst R INF = 1e8;\n\tR EPS = 1e-6;\n\tconst R PI = 3.1415926535897932384626;\n\tinline int sig(const R &x) { return (abs(x) < EPS ? 0 : x > 0 ? 1 : -1); }\n\tinline BOOL less(const R &x, const R &y) {return sig(x-y) ? x < y : BORDER;}\n\ttypedef complex<R> P;\n\tinline R norm(const P &p){return p.X*p.X+p.Y*p.Y;}\n\tinline R inp(const P& a, const P& b){return (conj(a)*b).X;}\n\tinline R outp(const P& a, const P& b){return (conj(a)*b).Y;}\n\tinline P unit(const P& p){return p/abs(p);}\n\tinline P proj(const P &s, const P &t){return t*inp(s, t)/norm(t);}\n\tinline int ccw(const P &s, const P &t, const P &p, int adv=0){\n\t\tint res = sig(outp(t-s, p-s));\n\t\tif(res || !adv) return res;\n\t\tif(sig(inp(t-s, p-s)) < 0) return -2;\t// p-s-t\n\t\tif(sig(inp(s-t, p-t)) < 0) return 2; // s-t-p\n\t\treturn 0;\t\t\t\t\t\t\t // s-p-t\n\t}\n\t \n\t \n\tstruct L : public vector<P>{ // line\n\t\tL(const P &p1, const P &p2){this->push_back(p1);this->push_back(p2);}\n\t\tL(){}\n\t\tinline P dir()const {return at(1) - at(0);}\n\t\tBOOL online(const P &p)const {return !sig(outp(p-at(0), dir()));}\n\t};\n\tstruct S : public L{\t// segment\n\t\tS(const P &p1, const P &p2):L(p1, p2){}\n\t\tS(){}\n\t\tBOOL online(const P &p)const {\n\t\t\tif(!sig(norm(p - at(0))) || !sig(norm(p - at(1)))) return BORDER;\n\t\t\treturn !sig(outp(p-at(0), dir())) && inp(p-at(0), dir()) > -EPS && inp(p-at(1), -dir()) > -EPS ? TRUE: FALSE;\n\t\t\treturn !sig(abs(at(0)-p) + abs(at(1) - p) - abs(at(0) - at(1)));\n\t\t}\n\t};\n\tP crosspoint(const L &l, const L &m);\n\tstruct G : public vector<P>{\n\t\tG(size_type size=0):vector(size){}\n\t\tS edge(int i)const {return S(at(i), at(i+1 == size() ? 0 : i+1));}\n\t};\n \n\tinline BOOL intersect(const S &s, const L &l){\n\t\treturn (sig(outp(l.dir(), s[0]-l[0])) * sig(outp(l.dir(), s[1]-l[0])) <= 0);\n\t}\n\tinline P crosspoint(const L &l, const L &m){\n\t\tR A = outp(l.dir(), m.dir()), B = outp(l.dir(), l[1] - m[0]);\n\t\tif(!sig(abs(A)) && !sig(abs(B))) return m[0]; // same line\n\t\tif(abs(A) < EPS) assert(false);\n\t\treturn m[0] + B / A * (m[1] - m[0]);\n\t}\n\t \n\tstruct Arrangement{\n\t\tstruct AEdge{\n\t\t\tint u, v, t;\n\t\t\tR cost;\n\t\t\tAEdge(int u=0, int v=0, int t=0, R cost=0)\n\t\t\t\t:u(u), v(v), t(t), cost(cost){}\n\t\t};\n\t\ttypedef vector<vector<AEdge>> AGraph;\n\t\tvector<P> p;\n\t\tAGraph g;\n\t\tArrangement(){}\n\t\tArrangement(vector<S> seg){\n\t\t\tint m = seg.size();\n\t\t\tREP(i, m){\n\t\t\t\tp.push_back(seg[i][0]);\n\t\t\t\tp.push_back(seg[i][1]);\n\t\t\t\tREP(j, i) if(sig(outp(seg[i].dir(), seg[j].dir())) && intersect(seg[i], seg[j]) == TRUE)\n\t\t\t\t\tp.push_back(crosspoint(seg[i], seg[j]));\n\t\t\t}\n\t\t\tsort(ALL(p)); UNIQUE(p);\n\t\t\tint n=p.size();\n\t\t\tg.resize(n);\n\t\t\tREP(i, m){\n\t\t\t\tS &s = seg[i];\n\t\t\t\tvector<pair<R, int>> ps;\n\t\t\t\tREP(j, n) if(s.online(p[j])) ps.emplace_back(norm(p[j] - s[0]), j);\n\t\t\t\tsort(ALL(ps));\n\t\t\t\tREP(j, (int)ps.size()-1){\n\t\t\t\t\tconst int u=ps[j].second;\n\t\t\t\t\tconst int v=ps[j+1].second;\n\t\t\t\t\tg[u].emplace_back(u, v, 0, abs(p[u] - p[v]));\n\t\t\t\t\tg[v].emplace_back(v, u, 0, abs(p[u] - p[v]));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\t \n\t\tint getIdx(P q){\n\t\t\tauto it = lower_bound(ALL(p), q);\n\t\t\tif(it == p.end() || *it != q) return -1;\n\t\t\treturn it - p.begin();\n\t\t}\n\t};\n \n\tstruct DualGraph{\n\t\tstruct DEdge{\n\t\t\tint u, v, f, l;\n\t\t\tR a;\n\t\t\tDEdge(int u, int v, R a):u(u),v(v),f(0),l(0){\n\t\t\t\twhile(PI < a) a -= 2*PI;\n\t\t\t\twhile(a < -PI) a += 2*PI;\n\t\t\t\tthis->a = a;\n\t\t\t}\n\t\t\tbool operator==(const DEdge &opp) const{\n\t\t\t\treturn v==opp.v;\n\t\t\t}\n\t\t\tbool operator<(const DEdge &opp) const{\n\t\t\t\treturn a>opp.a;\n\t\t\t}\n\t\t\tbool operator<(const R &opp) const{\n\t\t\t\treturn a>opp;\n\t\t\t}\n\t\t\tfriend ostream& operator<<(ostream &os, const DEdge &t) { return os<<\"(\"<<t.u<<\",\"<<t.v<<\",\"<<t.a*180/PI<<\")\";}\n\t\t};\n\t\t \n\t\tint n;\n\t\tvector<P> p;\n\t\tvector<vector<DEdge>> g;\n\t\tDualGraph(const vector<P> &p):p(p),g(p.size()),n(p.size()){}\n\t\tvoid add_edge(const int s, const int t){\n\t\t\tR a = arg(p[t]-p[s]);\n\t\t\tg[s].emplace_back(s, t, a);\n\t\t\tg[t].emplace_back(t, s, a > 0 ? a-PI : a+PI);\n\t\t}\n\t\tvoid add_polygon(int s, G &t, R a){\n\t\t\tauto e = lower_bound(ALL(g[s]), a-EPS);\n\t\t\tif(e == g[s].end()) e = g[s].begin();\n\t\t\tif(e->f) return;\n\t\t\te->f = 1;\n\t\t\tt.push_back(p[s]);\n\t\t\tadd_polygon(e->v, t, e->a > 0 ? e->a-PI : e->a+PI);\n\t\t}\n\t\t \n\t\tG dual(){\n\t\t\tREP(i, n){\n\t\t\t\tsort(ALL(g[i]));\n\t\t\t\tUNIQUE(g[i]);\n\t\t\t}\n\t\t\tint s = min_element(ALL(p)) - p.begin();\n\t\t\tG poly;\n\t\t\tadd_polygon(s, poly, -PI*(R).5);\n\t\t\treturn poly;\n\t\t}\n\t};\n \n#undef SELF\n#undef at\n}\n \nusing namespace geom;\n \nnamespace std{\n\tbool operator<(const P &a, const P &b){return sig(a.X-b.X) ? a.X < b.X : a.Y+EPS < b.Y;}\n\tbool operator==(const P &a, const P &b){return abs(a-b) < EPS;}\n\tistream& operator>>(istream &is, P &p){R x,y;is>>x>>y;p=P(x, y);return is;}\n}\n \nint n, m, k;\nstruct MSQ : public G{\n\tMSQ(){}\n\tvector<P> p;\n\tvector<S> s;\n\tint m, k;\n\tMSQ(int m, int k):m(m), k(k){\n\t\tREP(i, m) p.push_back(polar((R)1, 2*PI*i/m + PI*(R).5));\n\t\tREP(i, m) s.emplace_back(p[i], p[(i+k)%m]);\n\t\tArrangement a(s);\n\t\tDualGraph dg(a.p);\n\t\tREP(i, a.g.size())REP(j, a.g[i].size()){\n\t\t\tint u = a.g[i][j].u;\n\t\t\tint v = a.g[i][j].v;\n\t\t\tif(u < v) dg.add_edge(u, v);\n\t\t}\n\t\t(G &)(*this) = dg.dual();\n\t\treverse(this->begin(), this->end());\n\t}\n\tvoid copy(R r, P c, MSQ &msq)const {\n\t\tmsq.resize(size());\n\t\tmsq.p.resize(p.size());\n\t\tmsq.s.resize(s.size());\n\t\tmsq.m = m;\n\t\tmsq.k = k;\n\t\tREP(i, size()) msq[i] = at(i)*r + c;\n\t\tREP(i, p.size()) msq.p[i] = p[i]*r + c;\n\t\tREP(i, s.size()) msq.s[i] = S(msq.p[i], msq.p[(i+k)%m]);\n\t}\n\tS segment(int i)const {\n\t\treturn s[i];\n\t}\n};\nint convex_contains(const MSQ &msq, const P &g, const P &p) {\n\tconst int n = msq.size();\n\tint a = 0, b = n;\n\tconst P pg = p-g;\n\twhile (a+1 < b) { // invariant: c is in fan g-P[a]-P[b]\n\t\tint c = (a + b) / 2;\n\t\tif (outp(msq[a]-g, pg) > 0 && outp(msq[c]-g, pg) < 0) b = c;\n\t\telse a = c;\n\t}\n\tb %= n;\n\tif (outp(msq[a] - p, msq[b] - p) < -EPS) return 0;\n\treturn 1;\n}\nbool check(const MSQ & temp, R r, const vector<P> &vil, int i, int j){\n\tMSQ msq;\n\tP gp = vil[i] - temp.segment(j)[0]*r;\n\ttemp.copy(r, gp, msq);\n\tL l = msq.segment(j);\n\tvector<P> p;\n\tP b(0), u = l.dir();\n\tif(u < b) swap(b, u);\n\tREP(i, n){\n\t\tS l2(vil[i], vil[i] + l.dir());\n\t\tint f = 0;\n\t\tP ll(INF, INF), rr(-INF, -INF);\n\t\tREP(j, m){\n\t\t\tconst S s = msq.segment(j);\n\t\t\tif(intersect(s, l2)){\n\t\t\t\tconst P q = crosspoint(s, l2) - l2[0];\n\t\t\t\tp.push_back(q);\n\t\t\t\tll = min(ll, q);\n\t\t\t\trr = max(rr, q);\n\t\t\t\tf = 1;\n\t\t\t}\n\t\t}\n\t\tu = min(rr, u);\n\t\tb = max(ll, b);\n\t\tif(!f) return false;\n\t}\n\tFOR(q, p){\n\t\tif(*q < b || u < *q) continue;\n\t\tif([&](){\n\t\t\tREP(i, n) if(!convex_contains(msq, gp, vil[i] + *q)) return 0;\n\t\t\treturn 1;\n\t\t}()) return true;\n\t}\n\treturn false;\n}\n \nint main(){\n\tios::sync_with_stdio(false);\n\twhile(cin >> n >> m >> k, n){\n\t\tMSQ temp(m, k);\n\t\tvector<P> vil(n);\n\t\tREP(i, n) cin >> vil[i];\n\t\tR best = 2000;\n\t\tREP(i, n)REP(j, m){\n\t\t\tif(!check(temp, best-EPS, vil, i, j)) continue;\n\t\t\tR l=1., r = best;\n\t\t\twhile(r-l>1e-6){\n\t\t\t\tR m = (l+r)*(R).5;\n\t\t\t\tif(check(temp, m, vil, i, j)) r = m;\n\t\t\t\telse l = m;\n\t\t\t}\n\t\t\tbest = r;\n\t\t}\n\t\tprintf(\"%.10f\\n\", (double)best);\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 2680, "memory_kb": 1648, "score_of_the_acc": -0.2723, "final_rank": 7 }, { "submission_id": "aoj_2203_1158762", "code_snippet": "#include <cstdio>\n#include <cmath>\n#include <cstring>\n#include <cstdlib>\n#include <climits>\n#include <ctime>\n#include <queue>\n#include <stack>\n#include <algorithm>\n#include <list>\n#include <vector>\n#include <set>\n#include <map>\n#include <iostream>\n#include <deque>\n#include <complex>\n#include <string>\n#include <iomanip>\n#include <sstream>\n#include <bitset>\n#include <valarray>\n#include <unordered_map>\n#include <iterator>\n#include <assert.h>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned int uint;\ntypedef unsigned char uchar;\ntypedef unsigned long long ull;\ntypedef pair<int, int> pii;\ntypedef pair<ll, ll> pll;\ntypedef vector<int> vi;\n \n#define REP(i,x) for(int i=0;i<(int)(x);i++)\n#define REPS(i,x) for(int i=1;i<=(int)(x);i++)\n#define RREP(i,x) for(int i=((int)(x)-1);i>=0;i--)\n#define RREPS(i,x) for(int i=((int)(x));i>0;i--)\n#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();i++)\n#define RFOR(i,c) for(__typeof((c).rbegin())i=(c).rbegin();i!=(c).rend();i++)\n#define ALL(container) (container).begin(), (container).end()\n#define RALL(container) (container).rbegin(), (container).rend()\n#define SZ(container) ((int)container.size())\n#define mp(a,b) make_pair(a, b)\n#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );\n \ntemplate<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }\ntemplate<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }\ntemplate<class T> ostream& operator<<(ostream &os, const vector<T> &t) {\nos<<\"[\"; FOR(it,t) {if(it!=t.begin()) os<<\",\"; os<<*it;} os<<\"]\"; return os;\n}\ntemplate<class T> ostream& operator<<(ostream &os, const set<T> &t) {\nos<<\"{\"; FOR(it,t) {if(it!=t.begin()) os<<\",\"; os<<*it;} os<<\"}\"; return os;\n}\ntemplate<class S, class T> ostream& operator<<(ostream &os, const pair<S,T> &t) { return os<<\"(\"<<t.first<<\",\"<<t.second<<\")\";}\ntemplate<class S, class T> pair<S,T> operator+(const pair<S,T> &s, const pair<S,T> &t){ return pair<S,T>(s.first+t.first, s.second+t.second);}\ntemplate<class S, class T> pair<S,T> operator-(const pair<S,T> &s, const pair<S,T> &t){ return pair<S,T>(s.first-t.first, s.second-t.second);}\n \nnamespace geom{\n#define X real()\n#define Y imag()\n#define at(i) ((*this)[i])\n#define SELF (*this)\n\tenum {TRUE = 1, FALSE = 0, BORDER = -1};\n\ttypedef int BOOL;\n\ttypedef double R;\n\tconst R INF = 1e8;\n\tR EPS = 1e-6;\n\tconst R PI = 3.1415926535897932384626;\n\tinline int sig(const R &x) { return (abs(x) < EPS ? 0 : x > 0 ? 1 : -1); }\n\tinline BOOL less(const R &x, const R &y) {return sig(x-y) ? x < y : BORDER;}\n\ttypedef complex<R> P;\n\tinline R norm(const P &p){return p.X*p.X+p.Y*p.Y;}\n\tinline R inp(const P& a, const P& b){return (conj(a)*b).X;}\n\tinline R outp(const P& a, const P& b){return (conj(a)*b).Y;}\n\tinline P unit(const P& p){return p/abs(p);}\n\tinline P proj(const P &s, const P &t){return t*inp(s, t)/norm(t);}\n\tinline int ccw(const P &s, const P &t, const P &p, int adv=0){\n\t\tint res = sig(outp(t-s, p-s));\n\t\tif(res || !adv) return res;\n\t\tif(sig(inp(t-s, p-s)) < 0) return -2;\t// p-s-t\n\t\tif(sig(inp(s-t, p-t)) < 0) return 2; // s-t-p\n\t\treturn 0;\t\t\t\t\t\t\t // s-p-t\n\t}\n\t \n\t \n\tstruct L : public vector<P>{ // line\n\t\tL(const P &p1, const P &p2){this->push_back(p1);this->push_back(p2);}\n\t\tL(){}\n\t\tinline P dir()const {return at(1) - at(0);}\n\t\tBOOL online(const P &p)const {return !sig(outp(p-at(0), dir()));}\n\t};\n\tstruct S : public L{\t// segment\n\t\tS(const P &p1, const P &p2):L(p1, p2){}\n\t\tS(){}\n\t\tBOOL online(const P &p)const {\n\t\t\tif(!sig(norm(p - at(0))) || !sig(norm(p - at(1)))) return BORDER;\n\t\t\treturn !sig(outp(p-at(0), dir())) && inp(p-at(0), dir()) > -EPS && inp(p-at(1), -dir()) > -EPS ? TRUE: FALSE;\n\t\t\treturn !sig(abs(at(0)-p) + abs(at(1) - p) - abs(at(0) - at(1)));\n\t\t}\n\t};\n\tP crosspoint(const L &l, const L &m);\n\tstruct G : public vector<P>{\n\t\tG(size_type size=0):vector(size){}\n\t\tS edge(int i)const {return S(at(i), at(i+1 == size() ? 0 : i+1));}\n\t};\n \n\tinline BOOL intersect(const S &s, const L &l){\n\t\treturn (sig(outp(l.dir(), s[0]-l[0])) * sig(outp(l.dir(), s[1]-l[0])) <= 0);\n\t}\n\tinline P crosspoint(const L &l, const L &m){\n\t\tR A = outp(l.dir(), m.dir()), B = outp(l.dir(), l[1] - m[0]);\n\t\tif(!sig(abs(A)) && !sig(abs(B))) return m[0]; // same line\n\t\tif(abs(A) < EPS) assert(false);\n\t\treturn m[0] + B / A * (m[1] - m[0]);\n\t}\n\t \n\tstruct Arrangement{\n\t\tstruct AEdge{\n\t\t\tint u, v, t;\n\t\t\tR cost;\n\t\t\tAEdge(int u=0, int v=0, int t=0, R cost=0)\n\t\t\t\t:u(u), v(v), t(t), cost(cost){}\n\t\t};\n\t\ttypedef vector<vector<AEdge>> AGraph;\n\t\tvector<P> p;\n\t\tAGraph g;\n\t\tArrangement(){}\n\t\tArrangement(vector<S> seg){\n\t\t\tint m = seg.size();\n\t\t\tREP(i, m){\n\t\t\t\tp.push_back(seg[i][0]);\n\t\t\t\tp.push_back(seg[i][1]);\n\t\t\t\tREP(j, i) if(sig(outp(seg[i].dir(), seg[j].dir())) && intersect(seg[i], seg[j]) == TRUE)\n\t\t\t\t\tp.push_back(crosspoint(seg[i], seg[j]));\n\t\t\t}\n\t\t\tsort(ALL(p)); UNIQUE(p);\n\t\t\tint n=p.size();\n\t\t\tg.resize(n);\n\t\t\tREP(i, m){\n\t\t\t\tS &s = seg[i];\n\t\t\t\tvector<pair<R, int>> ps;\n\t\t\t\tREP(j, n) if(s.online(p[j])) ps.emplace_back(norm(p[j] - s[0]), j);\n\t\t\t\tsort(ALL(ps));\n\t\t\t\tREP(j, (int)ps.size()-1){\n\t\t\t\t\tconst int u=ps[j].second;\n\t\t\t\t\tconst int v=ps[j+1].second;\n\t\t\t\t\tg[u].emplace_back(u, v, 0, abs(p[u] - p[v]));\n\t\t\t\t\tg[v].emplace_back(v, u, 0, abs(p[u] - p[v]));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\t \n\t\tint getIdx(P q){\n\t\t\tauto it = lower_bound(ALL(p), q);\n\t\t\tif(it == p.end() || *it != q) return -1;\n\t\t\treturn it - p.begin();\n\t\t}\n\t};\n \n\tstruct DualGraph{\n\t\tstruct DEdge{\n\t\t\tint u, v, f, l;\n\t\t\tR a;\n\t\t\tDEdge(int u, int v, R a):u(u),v(v),f(0),l(0){\n\t\t\t\twhile(PI < a) a -= 2*PI;\n\t\t\t\twhile(a < -PI) a += 2*PI;\n\t\t\t\tthis->a = a;\n\t\t\t}\n\t\t\tbool operator==(const DEdge &opp) const{\n\t\t\t\treturn v==opp.v;\n\t\t\t}\n\t\t\tbool operator<(const DEdge &opp) const{\n\t\t\t\treturn a>opp.a;\n\t\t\t}\n\t\t\tbool operator<(const R &opp) const{\n\t\t\t\treturn a>opp;\n\t\t\t}\n\t\t\tfriend ostream& operator<<(ostream &os, const DEdge &t) { return os<<\"(\"<<t.u<<\",\"<<t.v<<\",\"<<t.a*180/PI<<\")\";}\n\t\t};\n\t\t \n\t\tint n;\n\t\tvector<P> p;\n\t\tvector<vector<DEdge>> g;\n\t\tDualGraph(const vector<P> &p):p(p),g(p.size()),n(p.size()){}\n\t\tvoid add_edge(const int s, const int t){\n\t\t\tR a = arg(p[t]-p[s]);\n\t\t\tg[s].emplace_back(s, t, a);\n\t\t\tg[t].emplace_back(t, s, a > 0 ? a-PI : a+PI);\n\t\t}\n\t\tvoid add_polygon(int s, G &t, R a){\n\t\t\tauto e = lower_bound(ALL(g[s]), a-EPS);\n\t\t\tif(e == g[s].end()) e = g[s].begin();\n\t\t\tif(e->f) return;\n\t\t\te->f = 1;\n\t\t\tt.push_back(p[s]);\n\t\t\tadd_polygon(e->v, t, e->a > 0 ? e->a-PI : e->a+PI);\n\t\t}\n\t\t \n\t\tG dual(){\n\t\t\tREP(i, n){\n\t\t\t\tsort(ALL(g[i]));\n\t\t\t\tUNIQUE(g[i]);\n\t\t\t}\n\t\t\tint s = min_element(ALL(p)) - p.begin();\n\t\t\tG poly;\n\t\t\tadd_polygon(s, poly, -PI*(R).5);\n\t\t\treturn poly;\n\t\t}\n\t};\n \n#undef SELF\n#undef at\n}\n \nusing namespace geom;\n \nnamespace std{\n\tbool operator<(const P &a, const P &b){return sig(a.X-b.X) ? a.X < b.X : a.Y+EPS < b.Y;}\n\tbool operator==(const P &a, const P &b){return abs(a-b) < EPS;}\n\tistream& operator>>(istream &is, P &p){R x,y;is>>x>>y;p=P(x, y);return is;}\n}\n \nint n, m, k;\nstruct MSQ : public G{\n\tMSQ(){}\n\tvector<P> p;\n\tvector<S> s;\n\tint m, k;\n\tMSQ(int m, int k):m(m), k(k){\n\t\tREP(i, m) p.push_back(polar((R)1, 2*PI*i/m + PI*(R).5));\n\t\tREP(i, m) s.emplace_back(p[i], p[(i+k)%m]);\n\t\tArrangement a(s);\n\t\tDualGraph dg(a.p);\n\t\tREP(i, a.g.size())REP(j, a.g[i].size()){\n\t\t\tint u = a.g[i][j].u;\n\t\t\tint v = a.g[i][j].v;\n\t\t\tif(u < v) dg.add_edge(u, v);\n\t\t}\n\t\t(G &)(*this) = dg.dual();\n\t\treverse(this->begin(), this->end());\n\t}\n\tvoid copy(R r, P c, MSQ &msq)const {\n\t\tmsq.resize(size());\n\t\tmsq.p.resize(p.size());\n\t\tmsq.s.resize(s.size());\n\t\tmsq.m = m;\n\t\tmsq.k = k;\n\t\tREP(i, size()) msq[i] = at(i)*r + c;\n\t\tREP(i, p.size()) msq.p[i] = p[i]*r + c;\n\t\tREP(i, s.size()) msq.s[i] = S(msq.p[i], msq.p[(i+k)%m]);\n\t}\n\tS segment(int i)const {\n\t\treturn s[i];\n\t}\n};\nint convex_contains(const MSQ &msq, const P &g, const P &p) {\n\tconst int n = msq.size();\n\tint a = 0, b = n;\n\tconst P pg = p-g;\n\twhile (a+1 < b) { // invariant: c is in fan g-P[a]-P[b]\n\t\tint c = (a + b) / 2;\n\t\tif (outp(msq[a]-g, pg) > 0 && outp(msq[c]-g, pg) < 0) b = c;\n\t\telse a = c;\n\t}\n\tb %= n;\n\tif (outp(msq[a] - p, msq[b] - p) < -EPS) return 0;\n\treturn 1;\n}\nbool check(const MSQ & temp, R r, const vector<P> &vil, int i, int j){\n\tMSQ msq;\n\tL l = temp.segment(j);\n\tP gp = vil[i] - l[0]*r;\n\ttemp.copy(r, gp, msq);\n\tvector<P> p;\n\tP b(0), u = msq.segment(j)[1];\n\tif(u < b) swap(b, u);\n\tREP(i, n){\n\t\tS l2(vil[i], vil[i] + msq.segment(j).dir());\n\t\tint f = 0;\n\t\tP ll(INF, INF), rr(-INF, -INF);\n\t\tREP(j, m){\n\t\t\tconst S s = msq.segment(j);\n\t\t\tif(intersect(s, l2)){\n\t\t\t\t\tconst P q = crosspoint(s, l2) - l2[0];\n\t\t\t\t\tp.push_back(q);\n\t\t\t\t\tll = min(ll, q);\n\t\t\t\t\trr = max(rr, q);\n\t\t\t\tf = 1;\n\t\t\t}\n\t\t}\n\t\tu = min(rr, u);\n\t\tb = max(ll, b);\n\t\tif(!f) return false;\n\t}\n\tFOR(q, p){\n\t\tif(*q < b || u < *q) continue;\n\t\tif([&](){\n\t\t\tREP(i, n) if(!convex_contains(msq, gp, vil[i] + *q)) return 0;\n\t\t\treturn 1;\n\t\t}()) return true;\n\t}\n\treturn false;\n}\n \nint main(){\n\tios::sync_with_stdio(false);\n\twhile(cin >> n >> m >> k, n){\n\t\tMSQ temp(m, k);\n\t\tvector<P> vil(n);\n\t\tREP(i, n) cin >> vil[i];\n\t\tR best = 2000;\n\t\tREP(i, n)REP(j, m){\n\t\t\tif(!check(temp, best-EPS, vil, i, j)) continue;\n\t\t\tR l=1., r = best;\n\t\t\twhile(r-l>1e-6){\n\t\t\t\tR m = (l+r)*(R).5;\n\t\t\t\tif(check(temp, m, vil, i, j)) r = m;\n\t\t\t\telse l = m;\n\t\t\t}\n\t\t\tbest = r;\n\t\t}\n\t\tprintf(\"%.10f\\n\", (double)best);\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 2640, "memory_kb": 1652, "score_of_the_acc": -0.2672, "final_rank": 5 }, { "submission_id": "aoj_2203_1158761", "code_snippet": "#include <cstdio>\n#include <cmath>\n#include <cstring>\n#include <cstdlib>\n#include <climits>\n#include <ctime>\n#include <queue>\n#include <stack>\n#include <algorithm>\n#include <list>\n#include <vector>\n#include <set>\n#include <map>\n#include <iostream>\n#include <deque>\n#include <complex>\n#include <string>\n#include <iomanip>\n#include <sstream>\n#include <bitset>\n#include <valarray>\n#include <unordered_map>\n#include <iterator>\n#include <assert.h>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned int uint;\ntypedef unsigned char uchar;\ntypedef unsigned long long ull;\ntypedef pair<int, int> pii;\ntypedef pair<ll, ll> pll;\ntypedef vector<int> vi;\n \n#define REP(i,x) for(int i=0;i<(int)(x);i++)\n#define REPS(i,x) for(int i=1;i<=(int)(x);i++)\n#define RREP(i,x) for(int i=((int)(x)-1);i>=0;i--)\n#define RREPS(i,x) for(int i=((int)(x));i>0;i--)\n#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();i++)\n#define RFOR(i,c) for(__typeof((c).rbegin())i=(c).rbegin();i!=(c).rend();i++)\n#define ALL(container) (container).begin(), (container).end()\n#define RALL(container) (container).rbegin(), (container).rend()\n#define SZ(container) ((int)container.size())\n#define mp(a,b) make_pair(a, b)\n#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );\n \ntemplate<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }\ntemplate<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }\ntemplate<class T> ostream& operator<<(ostream &os, const vector<T> &t) {\nos<<\"[\"; FOR(it,t) {if(it!=t.begin()) os<<\",\"; os<<*it;} os<<\"]\"; return os;\n}\ntemplate<class T> ostream& operator<<(ostream &os, const set<T> &t) {\nos<<\"{\"; FOR(it,t) {if(it!=t.begin()) os<<\",\"; os<<*it;} os<<\"}\"; return os;\n}\ntemplate<class S, class T> ostream& operator<<(ostream &os, const pair<S,T> &t) { return os<<\"(\"<<t.first<<\",\"<<t.second<<\")\";}\ntemplate<class S, class T> pair<S,T> operator+(const pair<S,T> &s, const pair<S,T> &t){ return pair<S,T>(s.first+t.first, s.second+t.second);}\ntemplate<class S, class T> pair<S,T> operator-(const pair<S,T> &s, const pair<S,T> &t){ return pair<S,T>(s.first-t.first, s.second-t.second);}\n \nnamespace geom{\n#define X real()\n#define Y imag()\n#define at(i) ((*this)[i])\n#define SELF (*this)\n\tenum {TRUE = 1, FALSE = 0, BORDER = -1};\n\ttypedef int BOOL;\n\ttypedef double R;\n\tconst R INF = 1e8;\n\tR EPS = 1e-6;\n\tconst R PI = 3.1415926535897932384626;\n\tinline int sig(const R &x) { return (abs(x) < EPS ? 0 : x > 0 ? 1 : -1); }\n\tinline BOOL less(const R &x, const R &y) {return sig(x-y) ? x < y : BORDER;}\n\ttypedef complex<R> P;\n\tinline R norm(const P &p){return p.X*p.X+p.Y*p.Y;}\n\tinline R inp(const P& a, const P& b){return (conj(a)*b).X;}\n\tinline R outp(const P& a, const P& b){return (conj(a)*b).Y;}\n\tinline P unit(const P& p){return p/abs(p);}\n\tinline P proj(const P &s, const P &t){return t*inp(s, t)/norm(t);}\n\tinline int ccw(const P &s, const P &t, const P &p, int adv=0){\n\t\tint res = sig(outp(t-s, p-s));\n\t\tif(res || !adv) return res;\n\t\tif(sig(inp(t-s, p-s)) < 0) return -2;\t// p-s-t\n\t\tif(sig(inp(s-t, p-t)) < 0) return 2; // s-t-p\n\t\treturn 0;\t\t\t\t\t\t\t // s-p-t\n\t}\n\t \n\t \n\tstruct L : public vector<P>{ // line\n\t\tL(const P &p1, const P &p2){this->push_back(p1);this->push_back(p2);}\n\t\tL(){}\n\t\tinline P dir()const {return at(1) - at(0);}\n\t\tBOOL online(const P &p)const {return !sig(outp(p-at(0), dir()));}\n\t};\n\tstruct S : public L{\t// segment\n\t\tS(const P &p1, const P &p2):L(p1, p2){}\n\t\tS(){}\n\t\tBOOL online(const P &p)const {\n\t\t\tif(!sig(norm(p - at(0))) || !sig(norm(p - at(1)))) return BORDER;\n\t\t\treturn !sig(outp(p-at(0), dir())) && inp(p-at(0), dir()) > -EPS && inp(p-at(1), -dir()) > -EPS ? TRUE: FALSE;\n\t\t\treturn !sig(abs(at(0)-p) + abs(at(1) - p) - abs(at(0) - at(1)));\n\t\t}\n\t};\n\tP crosspoint(const L &l, const L &m);\n\tstruct G : public vector<P>{\n\t\tG(size_type size=0):vector(size){}\n\t\tS edge(int i)const {return S(at(i), at(i+1 == size() ? 0 : i+1));}\n\t};\n \n\tinline BOOL intersect(const S &s, const L &l){\n\t\treturn (sig(outp(l.dir(), s[0]-l[0])) * sig(outp(l.dir(), s[1]-l[0])) <= 0);\n\t}\n\tinline P crosspoint(const L &l, const L &m){\n\t\tR A = outp(l.dir(), m.dir()), B = outp(l.dir(), l[1] - m[0]);\n\t\tif(!sig(abs(A)) && !sig(abs(B))) return m[0]; // same line\n\t\tif(abs(A) < EPS) assert(false);\n\t\treturn m[0] + B / A * (m[1] - m[0]);\n\t}\n\t \n\tstruct Arrangement{\n\t\tstruct AEdge{\n\t\t\tint u, v, t;\n\t\t\tR cost;\n\t\t\tAEdge(int u=0, int v=0, int t=0, R cost=0)\n\t\t\t\t:u(u), v(v), t(t), cost(cost){}\n\t\t};\n\t\ttypedef vector<vector<AEdge>> AGraph;\n\t\tvector<P> p;\n\t\tAGraph g;\n\t\tArrangement(){}\n\t\tArrangement(vector<S> seg){\n\t\t\tint m = seg.size();\n\t\t\tREP(i, m){\n\t\t\t\tp.push_back(seg[i][0]);\n\t\t\t\tp.push_back(seg[i][1]);\n\t\t\t\tREP(j, i) if(sig(outp(seg[i].dir(), seg[j].dir())) && intersect(seg[i], seg[j]) == TRUE)\n\t\t\t\t\tp.push_back(crosspoint(seg[i], seg[j]));\n\t\t\t}\n\t\t\tsort(ALL(p)); UNIQUE(p);\n\t\t\tint n=p.size();\n\t\t\tg.resize(n);\n\t\t\tREP(i, m){\n\t\t\t\tS &s = seg[i];\n\t\t\t\tvector<pair<R, int>> ps;\n\t\t\t\tREP(j, n) if(s.online(p[j])) ps.emplace_back(norm(p[j] - s[0]), j);\n\t\t\t\tsort(ALL(ps));\n\t\t\t\tREP(j, (int)ps.size()-1){\n\t\t\t\t\tconst int u=ps[j].second;\n\t\t\t\t\tconst int v=ps[j+1].second;\n\t\t\t\t\tg[u].emplace_back(u, v, 0, abs(p[u] - p[v]));\n\t\t\t\t\tg[v].emplace_back(v, u, 0, abs(p[u] - p[v]));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\t \n\t\tint getIdx(P q){\n\t\t\tauto it = lower_bound(ALL(p), q);\n\t\t\tif(it == p.end() || *it != q) return -1;\n\t\t\treturn it - p.begin();\n\t\t}\n\t};\n \n\tstruct DualGraph{\n\t\tstruct DEdge{\n\t\t\tint u, v, f, l;\n\t\t\tR a;\n\t\t\tDEdge(int u, int v, R a):u(u),v(v),f(0),l(0){\n\t\t\t\twhile(PI < a) a -= 2*PI;\n\t\t\t\twhile(a < -PI) a += 2*PI;\n\t\t\t\tthis->a = a;\n\t\t\t}\n\t\t\tbool operator==(const DEdge &opp) const{\n\t\t\t\treturn v==opp.v;\n\t\t\t}\n\t\t\tbool operator<(const DEdge &opp) const{\n\t\t\t\treturn a>opp.a;\n\t\t\t}\n\t\t\tbool operator<(const R &opp) const{\n\t\t\t\treturn a>opp;\n\t\t\t}\n\t\t\tfriend ostream& operator<<(ostream &os, const DEdge &t) { return os<<\"(\"<<t.u<<\",\"<<t.v<<\",\"<<t.a*180/PI<<\")\";}\n\t\t};\n\t\t \n\t\tint n;\n\t\tvector<P> p;\n\t\tvector<vector<DEdge>> g;\n\t\tDualGraph(const vector<P> &p):p(p),g(p.size()),n(p.size()){}\n\t\tvoid add_edge(const int s, const int t){\n\t\t\tR a = arg(p[t]-p[s]);\n\t\t\tg[s].emplace_back(s, t, a);\n\t\t\tg[t].emplace_back(t, s, a > 0 ? a-PI : a+PI);\n\t\t}\n\t\tvoid add_polygon(int s, G &t, R a){\n\t\t\tauto e = lower_bound(ALL(g[s]), a-EPS);\n\t\t\tif(e == g[s].end()) e = g[s].begin();\n\t\t\tif(e->f) return;\n\t\t\te->f = 1;\n\t\t\tt.push_back(p[s]);\n\t\t\tadd_polygon(e->v, t, e->a > 0 ? e->a-PI : e->a+PI);\n\t\t}\n\t\t \n\t\tG dual(){\n\t\t\tREP(i, n){\n\t\t\t\tsort(ALL(g[i]));\n\t\t\t\tUNIQUE(g[i]);\n\t\t\t}\n\t\t\tint s = min_element(ALL(p)) - p.begin();\n\t\t\tG poly;\n\t\t\tadd_polygon(s, poly, -PI*(R).5);\n\t\t\treturn poly;\n\t\t}\n\t};\n \n#undef SELF\n#undef at\n}\n \nusing namespace geom;\n \nnamespace std{\n\tbool operator<(const P &a, const P &b){return sig(a.X-b.X) ? a.X < b.X : a.Y+EPS < b.Y;}\n\tbool operator==(const P &a, const P &b){return abs(a-b) < EPS;}\n\tistream& operator>>(istream &is, P &p){R x,y;is>>x>>y;p=P(x, y);return is;}\n}\n \nint n, m, k;\nstruct MSQ : public G{\n\tMSQ(){}\n\tvector<P> p;\n\tvector<S> s;\n\tint m, k;\n\tMSQ(int m, int k):m(m), k(k){\n\t\tREP(i, m) p.push_back(polar((R)1, 2*PI*i/m + PI*(R).5));\n\t\tREP(i, m) s.emplace_back(p[i], p[(i+k)%m]);\n\t\tArrangement a(s);\n\t\tDualGraph dg(a.p);\n\t\tREP(i, a.g.size())REP(j, a.g[i].size()){\n\t\t\tint u = a.g[i][j].u;\n\t\t\tint v = a.g[i][j].v;\n\t\t\tif(u < v) dg.add_edge(u, v);\n\t\t}\n\t\t(G &)(*this) = dg.dual();\n\t\treverse(this->begin(), this->end());\n\t}\n\tvoid copy(R r, P c, MSQ &msq)const {\n\t\tmsq.resize(size());\n\t\tmsq.p.resize(p.size());\n\t\tmsq.s.resize(s.size());\n\t\tmsq.m = m;\n\t\tmsq.k = k;\n\t\tREP(i, size()) msq[i] = at(i)*r + c;\n\t\tREP(i, p.size()) msq.p[i] = p[i]*r + c;\n\t\tREP(i, s.size()) msq.s[i] = S(msq.p[i], msq.p[(i+k)%m]);\n\t}\n\tS segment(int i)const {\n\t\treturn s[i];\n\t}\n};\nint convex_contains(const MSQ &msq, const P &g, const P &p) {\n\tconst int n = msq.size();\n\tint a = 0, b = n;\n\tconst P pg = p-g;\n\twhile (a+1 < b) { // invariant: c is in fan g-P[a]-P[b]\n\t\tint c = (a + b) / 2;\n\t\tif (outp(msq[a]-g, pg) > 0 && outp(msq[c]-g, pg) < 0) b = c;\n\t\telse a = c;\n\t}\n\tb %= n;\n\tif (outp(msq[a] - p, msq[b] - p) < -EPS) return 0;\n\treturn 1;\n}\nbool check(const MSQ & temp, R r, const vector<P> &vil, int i, int j){\n\tMSQ msq;\n\tL l = temp.segment(j);\n\tP gp = vil[i] - l[0]*r;\n\ttemp.copy(r, gp, msq);\n\tvector<P> p;\n\tP b(0), u = msq.segment(j)[1];\n\tif(u < b) swap(b, u);\n\tREP(i, n){\n\t\tS l2(vil[i], vil[i] + msq.segment(j).dir());\n\t\tint f = 0;\n\t\tP ll(INF, INF), rr(-INF, -INF);\n\t\tREP(j, m){\n\t\t\tconst S s = msq.segment(j);\n\t\t\tif(intersect(s, l2)){\n\t\t\t\tif(sig(outp(s.dir(), l2.dir()))){\n\t\t\t\t\tconst P q = crosspoint(s, l2) - l2[0];\n\t\t\t\t\tp.push_back(q);\n\t\t\t\t\tll = min(ll, q);\n\t\t\t\t\trr = max(rr, q);\n\t\t\t\t}\n\t\t\t\tf = 1;\n\t\t\t}\n\t\t}\n\t\tu = min(rr, u);\n\t\tb = max(ll, b);\n\t\tif(!f) return false;\n\t}\n\tFOR(q, p){\n\t\tif(*q < b || u < *q) continue;\n\t\tif([&](){\n\t\t\tREP(i, n) if(!convex_contains(msq, gp, vil[i] + *q)) return 0;\n\t\t\treturn 1;\n\t\t}()) return true;\n\t}\n\treturn false;\n}\n \nint main(){\n\tios::sync_with_stdio(false);\n\twhile(cin >> n >> m >> k, n){\n\t\tMSQ temp(m, k);\n\t\tvector<P> vil(n);\n\t\tREP(i, n) cin >> vil[i];\n\t\tR best = 2000;\n\t\tREP(i, n)REP(j, m){\n\t\t\tif(!check(temp, best-EPS, vil, i, j)) continue;\n\t\t\tR l=1., r = best;\n\t\t\twhile(r-l>1e-6){\n\t\t\t\tR m = (l+r)*(R).5;\n\t\t\t\tif(check(temp, m, vil, i, j)) r = m;\n\t\t\t\telse l = m;\n\t\t\t}\n\t\t\tbest = r;\n\t\t}\n\t\tprintf(\"%.10f\\n\", (double)best);\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 2680, "memory_kb": 1648, "score_of_the_acc": -0.2723, "final_rank": 7 }, { "submission_id": "aoj_2203_1158431", "code_snippet": "#include <cstdio>\n#include <cmath>\n#include <cstring>\n#include <cstdlib>\n#include <climits>\n#include <ctime>\n#include <queue>\n#include <stack>\n#include <algorithm>\n#include <list>\n#include <vector>\n#include <set>\n#include <map>\n#include <iostream>\n#include <deque>\n#include <complex>\n#include <string>\n#include <iomanip>\n#include <sstream>\n#include <bitset>\n#include <valarray>\n#include <unordered_map>\n#include <iterator>\n#include <assert.h>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned int uint;\ntypedef unsigned char uchar;\ntypedef unsigned long long ull;\ntypedef pair<int, int> pii;\ntypedef pair<ll, ll> pll;\ntypedef vector<int> vi;\n \n#define REP(i,x) for(int i=0;i<(int)(x);i++)\n#define REPS(i,x) for(int i=1;i<=(int)(x);i++)\n#define RREP(i,x) for(int i=((int)(x)-1);i>=0;i--)\n#define RREPS(i,x) for(int i=((int)(x));i>0;i--)\n#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();i++)\n#define RFOR(i,c) for(__typeof((c).rbegin())i=(c).rbegin();i!=(c).rend();i++)\n#define ALL(container) (container).begin(), (container).end()\n#define RALL(container) (container).rbegin(), (container).rend()\n#define SZ(container) ((int)container.size())\n#define mp(a,b) make_pair(a, b)\n#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );\n \ntemplate<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }\ntemplate<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }\ntemplate<class T> ostream& operator<<(ostream &os, const vector<T> &t) {\nos<<\"[\"; FOR(it,t) {if(it!=t.begin()) os<<\",\"; os<<*it;} os<<\"]\"; return os;\n}\ntemplate<class T> ostream& operator<<(ostream &os, const set<T> &t) {\nos<<\"{\"; FOR(it,t) {if(it!=t.begin()) os<<\",\"; os<<*it;} os<<\"}\"; return os;\n}\ntemplate<class S, class T> ostream& operator<<(ostream &os, const pair<S,T> &t) { return os<<\"(\"<<t.first<<\",\"<<t.second<<\")\";}\ntemplate<class S, class T> pair<S,T> operator+(const pair<S,T> &s, const pair<S,T> &t){ return pair<S,T>(s.first+t.first, s.second+t.second);}\ntemplate<class S, class T> pair<S,T> operator-(const pair<S,T> &s, const pair<S,T> &t){ return pair<S,T>(s.first-t.first, s.second-t.second);}\n \nnamespace geom{\n#define X real()\n#define Y imag()\n#define at(i) ((*this)[i])\n#define SELF (*this)\n\tenum {TRUE = 1, FALSE = 0, BORDER = -1};\n\ttypedef int BOOL;\n\ttypedef double R;\n\tconst R INF = 1e8;\n\tR EPS = 1e-6;\n\tconst R PI = 3.1415926535897932384626;\n\tinline int sig(const R &x) { return (abs(x) < EPS ? 0 : x > 0 ? 1 : -1); }\n\tinline BOOL less(const R &x, const R &y) {return sig(x-y) ? x < y : BORDER;}\n\ttypedef complex<R> P;\n\tinline R norm(const P &p){return p.X*p.X+p.Y*p.Y;}\n\tinline R inp(const P& a, const P& b){return (conj(a)*b).X;}\n\tinline R outp(const P& a, const P& b){return (conj(a)*b).Y;}\n\tinline P unit(const P& p){return p/abs(p);}\n\tinline P proj(const P &s, const P &t){return t*inp(s, t)/norm(t);}\n\tinline int ccw(const P &s, const P &t, const P &p, int adv=0){\n\t\tint res = sig(outp(t-s, p-s));\n\t\tif(res || !adv) return res;\n\t\tif(sig(inp(t-s, p-s)) < 0) return -2;\t// p-s-t\n\t\tif(sig(inp(s-t, p-t)) < 0) return 2; // s-t-p\n\t\treturn 0;\t\t\t\t\t\t\t // s-p-t\n\t}\n\t \n\t \n\tstruct L : public vector<P>{ // line\n\t\tL(const P &p1, const P &p2){this->push_back(p1);this->push_back(p2);}\n\t\tL(){}\n\t\tinline P dir()const {return at(1) - at(0);}\n\t\tBOOL online(const P &p)const {return !sig(outp(p-at(0), dir()));}\n\t};\n\tstruct S : public L{\t// segment\n\t\tS(const P &p1, const P &p2):L(p1, p2){}\n\t\tS(){}\n\t\tBOOL online(const P &p)const {\n\t\t\tif(!sig(norm(p - at(0))) || !sig(norm(p - at(1)))) return BORDER;\n\t\t\treturn !sig(outp(p-at(0), dir())) && inp(p-at(0), dir()) > -EPS && inp(p-at(1), -dir()) > -EPS ? TRUE: FALSE;\n\t\t\treturn !sig(abs(at(0)-p) + abs(at(1) - p) - abs(at(0) - at(1)));\n\t\t}\n\t};\n\tP crosspoint(const L &l, const L &m);\n\tstruct G : public vector<P>{\n\t\tG(size_type size=0):vector(size){}\n\t\tS edge(int i)const {return S(at(i), at(i+1 == size() ? 0 : i+1));}\n\t};\n \n\tinline BOOL intersect(const S &s, const L &l){\n\t\treturn (sig(outp(l.dir(), s[0]-l[0])) * sig(outp(l.dir(), s[1]-l[0])) <= 0);\n\t}\n\tinline P crosspoint(const L &l, const L &m){\n\t\tR A = outp(l.dir(), m.dir()), B = outp(l.dir(), l[1] - m[0]);\n\t\tif(!sig(abs(A)) && !sig(abs(B))) return m[0]; // same line\n\t\tif(abs(A) < EPS) assert(false);\n\t\treturn m[0] + B / A * (m[1] - m[0]);\n\t}\n\t \n\tstruct Arrangement{\n\t\tstruct AEdge{\n\t\t\tint u, v, t;\n\t\t\tR cost;\n\t\t\tAEdge(int u=0, int v=0, int t=0, R cost=0)\n\t\t\t\t:u(u), v(v), t(t), cost(cost){}\n\t\t};\n\t\ttypedef vector<vector<AEdge>> AGraph;\n\t\tvector<P> p;\n\t\tAGraph g;\n\t\tArrangement(){}\n\t\tArrangement(vector<S> seg){\n\t\t\tint m = seg.size();\n\t\t\tREP(i, m){\n\t\t\t\tp.push_back(seg[i][0]);\n\t\t\t\tp.push_back(seg[i][1]);\n\t\t\t\tREP(j, i) if(sig(outp(seg[i].dir(), seg[j].dir())) && intersect(seg[i], seg[j]) == TRUE)\n\t\t\t\t\tp.push_back(crosspoint(seg[i], seg[j]));\n\t\t\t}\n\t\t\tsort(ALL(p)); UNIQUE(p);\n\t\t\tint n=p.size();\n\t\t\tg.resize(n);\n\t\t\tREP(i, m){\n\t\t\t\tS &s = seg[i];\n\t\t\t\tvector<pair<R, int>> ps;\n\t\t\t\tREP(j, n) if(s.online(p[j])) ps.emplace_back(norm(p[j] - s[0]), j);\n\t\t\t\tsort(ALL(ps));\n\t\t\t\tREP(j, (int)ps.size()-1){\n\t\t\t\t\tconst int u=ps[j].second;\n\t\t\t\t\tconst int v=ps[j+1].second;\n\t\t\t\t\tg[u].emplace_back(u, v, 0, abs(p[u] - p[v]));\n\t\t\t\t\tg[v].emplace_back(v, u, 0, abs(p[u] - p[v]));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\t \n\t\tint getIdx(P q){\n\t\t\tauto it = lower_bound(ALL(p), q);\n\t\t\tif(it == p.end() || *it != q) return -1;\n\t\t\treturn it - p.begin();\n\t\t}\n\t};\n \n\tstruct DualGraph{\n\t\tstruct DEdge{\n\t\t\tint u, v, f, l;\n\t\t\tR a;\n\t\t\tDEdge(int u, int v, R a):u(u),v(v),f(0),l(0){\n\t\t\t\twhile(PI < a) a -= 2*PI;\n\t\t\t\twhile(a < -PI) a += 2*PI;\n\t\t\t\tthis->a = a;\n\t\t\t}\n\t\t\tbool operator==(const DEdge &opp) const{\n\t\t\t\treturn v==opp.v;\n\t\t\t}\n\t\t\tbool operator<(const DEdge &opp) const{\n\t\t\t\treturn a>opp.a;\n\t\t\t}\n\t\t\tbool operator<(const R &opp) const{\n\t\t\t\treturn a>opp;\n\t\t\t}\n\t\t\tfriend ostream& operator<<(ostream &os, const DEdge &t) { return os<<\"(\"<<t.u<<\",\"<<t.v<<\",\"<<t.a*180/PI<<\")\";}\n\t\t};\n\t\t \n\t\tint n;\n\t\tvector<P> p;\n\t\tvector<vector<DEdge>> g;\n\t\tDualGraph(const vector<P> &p):p(p),g(p.size()),n(p.size()){}\n\t\tvoid add_edge(const int s, const int t){\n\t\t\tR a = arg(p[t]-p[s]);\n\t\t\tg[s].emplace_back(s, t, a);\n\t\t\tg[t].emplace_back(t, s, a > 0 ? a-PI : a+PI);\n\t\t}\n\t\tvoid add_polygon(int s, G &t, R a){\n\t\t\tauto e = lower_bound(ALL(g[s]), a-EPS);\n\t\t\tif(e == g[s].end()) e = g[s].begin();\n\t\t\tif(e->f) return;\n\t\t\te->f = 1;\n\t\t\tt.push_back(p[s]);\n\t\t\tadd_polygon(e->v, t, e->a > 0 ? e->a-PI : e->a+PI);\n\t\t}\n\t\t \n\t\tG dual(){\n\t\t\tREP(i, n){\n\t\t\t\tsort(ALL(g[i]));\n\t\t\t\tUNIQUE(g[i]);\n\t\t\t}\n\t\t\tint s = min_element(ALL(p)) - p.begin();\n\t\t\tG poly;\n\t\t\tadd_polygon(s, poly, -PI*(R).5);\n\t\t\treturn poly;\n\t\t}\n\t};\n \n#undef SELF\n#undef at\n}\n \nusing namespace geom;\n \nnamespace std{\n\tbool operator<(const P &a, const P &b){return sig(a.X-b.X) ? a.X < b.X : a.Y+EPS < b.Y;}\n\tbool operator==(const P &a, const P &b){return abs(a-b) < EPS;}\n\tistream& operator>>(istream &is, P &p){R x,y;is>>x>>y;p=P(x, y);return is;}\n}\n \nint n, m, k;\nstruct MSQ : public G{\n\tMSQ(){}\n\tvector<P> p;\n\tvector<S> s;\n\tint m, k;\n\tMSQ(int m, int k):m(m), k(k){\n\t\tREP(i, m) p.push_back(polar((R)1, 2*PI*i/m + PI*(R).5));\n\t\tREP(i, m) s.emplace_back(p[i], p[(i+k)%m]);\n\t\tArrangement a(s);\n\t\tDualGraph dg(a.p);\n\t\tREP(i, a.g.size())REP(j, a.g[i].size()){\n\t\t\tint u = a.g[i][j].u;\n\t\t\tint v = a.g[i][j].v;\n\t\t\tif(u < v) dg.add_edge(u, v);\n\t\t}\n\t\t(G &)(*this) = dg.dual();\n\t\treverse(this->begin(), this->end());\n\t}\n\tvoid copy(R r, P c, MSQ &msq)const {\n\t\tmsq.resize(size());\n\t\tmsq.p.resize(p.size());\n\t\tmsq.s.resize(s.size());\n\t\tmsq.m = m;\n\t\tmsq.k = k;\n\t\tREP(i, size()) msq[i] = at(i)*r + c;\n\t\tREP(i, p.size()) msq.p[i] = p[i]*r + c;\n\t\tREP(i, s.size()) msq.s[i] = S(msq.p[i], msq.p[(i+k)%m]);\n\t}\n\tS segment(int i)const {\n\t\treturn s[i];\n\t}\n};\nint convex_contains(const MSQ &msq, const P &g, const P &p) {\n\tconst int n = msq.size();\n\tint a = 0, b = n;\n\tconst P pg = p-g;\n\twhile (a+1 < b) { // invariant: c is in fan g-P[a]-P[b]\n\t\tint c = (a + b) / 2;\n\t\tif (outp(msq[a]-g, pg) > 0 && outp(msq[c]-g, pg) < 0) b = c;\n\t\telse a = c;\n\t}\n\tb %= n;\n\tif (outp(msq[a] - p, msq[b] - p) < -EPS) return 0;\n\treturn 1;\n}\nbool check(const MSQ & temp, R r, const vector<P> &vil, int i, int j){\n\tMSQ msq;\n\tL l = temp.segment(j);\n\tP gp = vil[i] - l[0]*r;\n\ttemp.copy(r, gp, msq);\n\tvector<P> p;\n\tP b(0), u = msq.segment(j)[1];\n\tif(u < b) swap(b, u);\n\tREP(i, n){\n\t\tL l2(vil[i], vil[i] + l.dir());\n\t\tint f = 0;\n\t\tP ll(INF, INF), rr(-INF, -INF);\n\t\tREP(j, m){\n\t\t\tconst S s = msq.segment(j);\n\t\t\tif(intersect(s, l2)){\n\t\t\t\tif(sig(outp(s.dir(), l2.dir()))){\n\t\t\t\t\tconst P q = crosspoint(s, l2) - l2[0];\n\t\t\t\t\tp.push_back(q);\n\t\t\t\t\tll = min(ll, q);\n\t\t\t\t\trr = max(rr, q);\n\t\t\t\t}\n\t\t\t\tf = 1;\n\t\t\t}\n\t\t}\n\t\tu = min(rr, u);\n\t\tb = max(ll, b);\n\t\tif(!f) return false;\n\t}\n\tFOR(q, p){\n\t\tif(*q < b || u < *q) continue;\n\t\tif([&](){\n\t\t\tREP(i, n) if(!convex_contains(msq, gp, vil[i] + *q)) return 0;\n\t\t\treturn 1;\n\t\t}()) return true;\n\t}\n\treturn false;\n}\n \nint main(){\n\tios::sync_with_stdio(false);\n\twhile(cin >> n >> m >> k, n){\n\t\tMSQ temp(m, k);\n\t\tvector<P> vil(n);\n\t\tREP(i, n) cin >> vil[i];\n\t\tR best = 2000;\n\t\tREP(i, n)REP(j, m){\n\t\t\tif(!check(temp, best-EPS, vil, i, j)) continue;\n\t\t\tR l=1., r = best;\n\t\t\twhile(r-l>1e-6){\n\t\t\t\tR m = (l+r)*(R).5;\n\t\t\t\tif(check(temp, m, vil, i, j)) r = m;\n\t\t\t\telse l = m;\n\t\t\t}\n\t\t\tbest = r;\n\t\t}\n\t\tprintf(\"%.10f\\n\", (double)best);\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 2670, "memory_kb": 1648, "score_of_the_acc": -0.2707, "final_rank": 6 }, { "submission_id": "aoj_2203_1158430", "code_snippet": "#include <cstdio>\n#include <cmath>\n#include <cstring>\n#include <cstdlib>\n#include <climits>\n#include <ctime>\n#include <queue>\n#include <stack>\n#include <algorithm>\n#include <list>\n#include <vector>\n#include <set>\n#include <map>\n#include <iostream>\n#include <deque>\n#include <complex>\n#include <string>\n#include <iomanip>\n#include <sstream>\n#include <bitset>\n#include <valarray>\n#include <unordered_map>\n#include <iterator>\n#include <assert.h>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned int uint;\ntypedef unsigned char uchar;\ntypedef unsigned long long ull;\ntypedef pair<int, int> pii;\ntypedef pair<ll, ll> pll;\ntypedef vector<int> vi;\n \n#define REP(i,x) for(int i=0;i<(int)(x);i++)\n#define REPS(i,x) for(int i=1;i<=(int)(x);i++)\n#define RREP(i,x) for(int i=((int)(x)-1);i>=0;i--)\n#define RREPS(i,x) for(int i=((int)(x));i>0;i--)\n#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();i++)\n#define RFOR(i,c) for(__typeof((c).rbegin())i=(c).rbegin();i!=(c).rend();i++)\n#define ALL(container) (container).begin(), (container).end()\n#define RALL(container) (container).rbegin(), (container).rend()\n#define SZ(container) ((int)container.size())\n#define mp(a,b) make_pair(a, b)\n#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );\n \ntemplate<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }\ntemplate<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }\ntemplate<class T> ostream& operator<<(ostream &os, const vector<T> &t) {\nos<<\"[\"; FOR(it,t) {if(it!=t.begin()) os<<\",\"; os<<*it;} os<<\"]\"; return os;\n}\ntemplate<class T> ostream& operator<<(ostream &os, const set<T> &t) {\nos<<\"{\"; FOR(it,t) {if(it!=t.begin()) os<<\",\"; os<<*it;} os<<\"}\"; return os;\n}\ntemplate<class S, class T> ostream& operator<<(ostream &os, const pair<S,T> &t) { return os<<\"(\"<<t.first<<\",\"<<t.second<<\")\";}\ntemplate<class S, class T> pair<S,T> operator+(const pair<S,T> &s, const pair<S,T> &t){ return pair<S,T>(s.first+t.first, s.second+t.second);}\ntemplate<class S, class T> pair<S,T> operator-(const pair<S,T> &s, const pair<S,T> &t){ return pair<S,T>(s.first-t.first, s.second-t.second);}\n \nnamespace geom{\n#define X real()\n#define Y imag()\n#define at(i) ((*this)[i])\n#define SELF (*this)\n\tenum {TRUE = 1, FALSE = 0, BORDER = -1};\n\ttypedef int BOOL;\n\ttypedef double R;\n\tconst R INF = 1e8;\n\tR EPS = 1e-6;\n\tconst R PI = 3.1415926535897932384626;\n\tinline int sig(const R &x) { return (abs(x) < EPS ? 0 : x > 0 ? 1 : -1); }\n\tinline BOOL less(const R &x, const R &y) {return sig(x-y) ? x < y : BORDER;}\n\ttypedef complex<R> P;\n\tinline R norm(const P &p){return p.X*p.X+p.Y*p.Y;}\n\tinline R inp(const P& a, const P& b){return (conj(a)*b).X;}\n\tinline R outp(const P& a, const P& b){return (conj(a)*b).Y;}\n\tinline P unit(const P& p){return p/abs(p);}\n\tinline P proj(const P &s, const P &t){return t*inp(s, t)/norm(t);}\n\tinline int ccw(const P &s, const P &t, const P &p, int adv=0){\n\t\tint res = sig(outp(t-s, p-s));\n\t\tif(res || !adv) return res;\n\t\tif(sig(inp(t-s, p-s)) < 0) return -2;\t// p-s-t\n\t\tif(sig(inp(s-t, p-t)) < 0) return 2; // s-t-p\n\t\treturn 0;\t\t\t\t\t\t\t // s-p-t\n\t}\n\t \n\t \n\tstruct L : public vector<P>{ // line\n\t\tL(const P &p1, const P &p2){this->push_back(p1);this->push_back(p2);}\n\t\tL(){}\n\t\tinline P dir()const {return at(1) - at(0);}\n\t\tBOOL online(const P &p)const {return !sig(outp(p-at(0), dir()));}\n\t};\n\tstruct S : public L{\t// segment\n\t\tS(const P &p1, const P &p2):L(p1, p2){}\n\t\tS(){}\n\t\tBOOL online(const P &p)const {\n\t\t\tif(!sig(norm(p - at(0))) || !sig(norm(p - at(1)))) return BORDER;\n\t\t\treturn !sig(outp(p-at(0), dir())) && inp(p-at(0), dir()) > -EPS && inp(p-at(1), -dir()) > -EPS ? TRUE: FALSE;\n\t\t\treturn !sig(abs(at(0)-p) + abs(at(1) - p) - abs(at(0) - at(1)));\n\t\t}\n\t};\n\tP crosspoint(const L &l, const L &m);\n\tstruct G : public vector<P>{\n\t\tG(size_type size=0):vector(size){}\n\t\tS edge(int i)const {return S(at(i), at(i+1 == size() ? 0 : i+1));}\n\t};\n \n\tinline BOOL intersect(const S &s, const L &l){\n\t\treturn (sig(outp(l.dir(), s[0]-l[0])) * sig(outp(l.dir(), s[1]-l[0])) <= 0);\n\t}\n\tinline P crosspoint(const L &l, const L &m){\n\t\tR A = outp(l.dir(), m.dir()), B = outp(l.dir(), l[1] - m[0]);\n\t\tif(!sig(abs(A)) && !sig(abs(B))) return m[0]; // same line\n\t\treturn m[0] + B / A * (m[1] - m[0]);\n\t}\n\t \n\tstruct Arrangement{\n\t\tstruct AEdge{\n\t\t\tint u, v, t;\n\t\t\tR cost;\n\t\t\tAEdge(int u=0, int v=0, int t=0, R cost=0)\n\t\t\t\t:u(u), v(v), t(t), cost(cost){}\n\t\t};\n\t\ttypedef vector<vector<AEdge>> AGraph;\n\t\tvector<P> p;\n\t\tAGraph g;\n\t\tArrangement(){}\n\t\tArrangement(vector<S> seg){\n\t\t\tint m = seg.size();\n\t\t\tREP(i, m){\n\t\t\t\tp.push_back(seg[i][0]);\n\t\t\t\tp.push_back(seg[i][1]);\n\t\t\t\tREP(j, i) if(sig(outp(seg[i].dir(), seg[j].dir())) && intersect(seg[i], seg[j]) == TRUE)\n\t\t\t\t\tp.push_back(crosspoint(seg[i], seg[j]));\n\t\t\t}\n\t\t\tsort(ALL(p)); UNIQUE(p);\n\t\t\tint n=p.size();\n\t\t\tg.resize(n);\n\t\t\tREP(i, m){\n\t\t\t\tS &s = seg[i];\n\t\t\t\tvector<pair<R, int>> ps;\n\t\t\t\tREP(j, n) if(s.online(p[j])) ps.emplace_back(norm(p[j] - s[0]), j);\n\t\t\t\tsort(ALL(ps));\n\t\t\t\tREP(j, (int)ps.size()-1){\n\t\t\t\t\tconst int u=ps[j].second;\n\t\t\t\t\tconst int v=ps[j+1].second;\n\t\t\t\t\tg[u].emplace_back(u, v, 0, abs(p[u] - p[v]));\n\t\t\t\t\tg[v].emplace_back(v, u, 0, abs(p[u] - p[v]));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\t \n\t\tint getIdx(P q){\n\t\t\tauto it = lower_bound(ALL(p), q);\n\t\t\tif(it == p.end() || *it != q) return -1;\n\t\t\treturn it - p.begin();\n\t\t}\n\t};\n \n\tstruct DualGraph{\n\t\tstruct DEdge{\n\t\t\tint u, v, f, l;\n\t\t\tR a;\n\t\t\tDEdge(int u, int v, R a):u(u),v(v),f(0),l(0){\n\t\t\t\twhile(PI < a) a -= 2*PI;\n\t\t\t\twhile(a < -PI) a += 2*PI;\n\t\t\t\tthis->a = a;\n\t\t\t}\n\t\t\tbool operator==(const DEdge &opp) const{\n\t\t\t\treturn v==opp.v;\n\t\t\t}\n\t\t\tbool operator<(const DEdge &opp) const{\n\t\t\t\treturn a>opp.a;\n\t\t\t}\n\t\t\tbool operator<(const R &opp) const{\n\t\t\t\treturn a>opp;\n\t\t\t}\n\t\t\tfriend ostream& operator<<(ostream &os, const DEdge &t) { return os<<\"(\"<<t.u<<\",\"<<t.v<<\",\"<<t.a*180/PI<<\")\";}\n\t\t};\n\t\t \n\t\tint n;\n\t\tvector<P> p;\n\t\tvector<vector<DEdge>> g;\n\t\tDualGraph(const vector<P> &p):p(p),g(p.size()),n(p.size()){}\n\t\tvoid add_edge(const int s, const int t){\n\t\t\tR a = arg(p[t]-p[s]);\n\t\t\tg[s].emplace_back(s, t, a);\n\t\t\tg[t].emplace_back(t, s, a > 0 ? a-PI : a+PI);\n\t\t}\n\t\tvoid add_polygon(int s, G &t, R a){\n\t\t\tauto e = lower_bound(ALL(g[s]), a-EPS);\n\t\t\tif(e == g[s].end()) e = g[s].begin();\n\t\t\tif(e->f) return;\n\t\t\te->f = 1;\n\t\t\tt.push_back(p[s]);\n\t\t\tadd_polygon(e->v, t, e->a > 0 ? e->a-PI : e->a+PI);\n\t\t}\n\t\t \n\t\tG dual(){\n\t\t\tREP(i, n){\n\t\t\t\tsort(ALL(g[i]));\n\t\t\t\tUNIQUE(g[i]);\n\t\t\t}\n\t\t\tint s = min_element(ALL(p)) - p.begin();\n\t\t\tG poly;\n\t\t\tadd_polygon(s, poly, -PI*(R).5);\n\t\t\treturn poly;\n\t\t}\n\t};\n \n#undef SELF\n#undef at\n}\n \nusing namespace geom;\n \nnamespace std{\n\tbool operator<(const P &a, const P &b){return sig(a.X-b.X) ? a.X < b.X : a.Y+EPS < b.Y;}\n\tbool operator==(const P &a, const P &b){return abs(a-b) < EPS;}\n\tistream& operator>>(istream &is, P &p){R x,y;is>>x>>y;p=P(x, y);return is;}\n}\n \nint n, m, k;\nstruct MSQ : public G{\n\tMSQ(){}\n\tvector<P> p;\n\tvector<S> s;\n\tint m, k;\n\tMSQ(int m, int k):m(m), k(k){\n\t\tREP(i, m) p.push_back(polar((R)1, 2*PI*i/m + PI*(R).5));\n\t\tREP(i, m) s.emplace_back(p[i], p[(i+k)%m]);\n\t\tArrangement a(s);\n\t\tDualGraph dg(a.p);\n\t\tREP(i, a.g.size())REP(j, a.g[i].size()){\n\t\t\tint u = a.g[i][j].u;\n\t\t\tint v = a.g[i][j].v;\n\t\t\tif(u < v) dg.add_edge(u, v);\n\t\t}\n\t\t(G &)(*this) = dg.dual();\n\t\treverse(this->begin(), this->end());\n\t}\n\tvoid copy(R r, P c, MSQ &msq)const {\n\t\tmsq.resize(size());\n\t\tmsq.p.resize(p.size());\n\t\tmsq.s.resize(s.size());\n\t\tmsq.m = m;\n\t\tmsq.k = k;\n\t\tREP(i, size()) msq[i] = at(i)*r + c;\n\t\tREP(i, p.size()) msq.p[i] = p[i]*r + c;\n\t\tREP(i, s.size()) msq.s[i] = S(msq.p[i], msq.p[(i+k)%m]);\n\t}\n\tS segment(int i)const {\n\t\treturn s[i];\n\t}\n};\nint convex_contains(const MSQ &msq, const P &g, const P &p) {\n\tconst int n = msq.size();\n\tint a = 0, b = n;\n\tconst P pg = p-g;\n\twhile (a+1 < b) { // invariant: c is in fan g-P[a]-P[b]\n\t\tint c = (a + b) / 2;\n\t\tif (outp(msq[a]-g, pg) > 0 && outp(msq[c]-g, pg) < 0) b = c;\n\t\telse a = c;\n\t}\n\tb %= n;\n\tif (outp(msq[a] - p, msq[b] - p) < -EPS) return 0;\n\treturn 1;\n}\nbool check(const MSQ & temp, R r, const vector<P> &vil, int i, int j){\n\tMSQ msq;\n\tL l = temp.segment(j);\n\tP gp = vil[i] - l[0]*r;\n\ttemp.copy(r, gp, msq);\n\tvector<P> p;\n\tP b(0), u = msq.segment(j)[1];\n\tif(u < b) swap(b, u);\n\tREP(i, n){\n\t\tL l2(vil[i], vil[i] + l.dir());\n\t\tP ll(INF, INF), rr(-INF, -INF);\n\t\tREP(j, m){\n\t\t\tconst S s = msq.segment(j);\n\t\t\tif(intersect(s, l2)){\n\t\t\t\tconst P q = crosspoint(s, l2) - l2[0];\n\t\t\t\tp.push_back(q);\n\t\t\t\tll = min(ll, q);\n\t\t\t\trr = max(rr, q);\n\t\t\t}\n\t\t}\n\t\tu = min(rr, u);\n\t\tb = max(ll, b);\n\t}\n\tFOR(q, p){\n\t\tif(*q < b || u < *q) continue;\n\t\tif([&](){\n\t\t\tREP(i, n) if(!convex_contains(msq, gp, vil[i] + *q)) return 0;\n\t\t\treturn 1;\n\t\t}()) return true;\n\t}\n\treturn false;\n}\n \nint main(){\n\tios::sync_with_stdio(false);\n\twhile(cin >> n >> m >> k, n){\n\t\tMSQ temp(m, k);\n\t\tvector<P> vil(n);\n\t\tREP(i, n) cin >> vil[i];\n\t\tR best = 2000;\n\t\tREP(i, n)REP(j, m){\n\t\t\tif(!check(temp, best-EPS, vil, i, j)) continue;\n\t\t\tR l=1., r = best;\n\t\t\twhile(r-l>1e-6){\n\t\t\t\tR m = (l+r)*(R).5;\n\t\t\t\tif(check(temp, m, vil, i, j)) r = m;\n\t\t\t\telse l = m;\n\t\t\t}\n\t\t\tbest = r;\n\t\t}\n\t\tprintf(\"%.10f\\n\", (double)best);\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 3750, "memory_kb": 1648, "score_of_the_acc": -0.4444, "final_rank": 10 }, { "submission_id": "aoj_2203_1158429", "code_snippet": "#include <cstdio>\n#include <cmath>\n#include <cstring>\n#include <cstdlib>\n#include <climits>\n#include <ctime>\n#include <queue>\n#include <stack>\n#include <algorithm>\n#include <list>\n#include <vector>\n#include <set>\n#include <map>\n#include <iostream>\n#include <deque>\n#include <complex>\n#include <string>\n#include <iomanip>\n#include <sstream>\n#include <bitset>\n#include <valarray>\n#include <unordered_map>\n#include <iterator>\n#include <assert.h>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned int uint;\ntypedef unsigned char uchar;\ntypedef unsigned long long ull;\ntypedef pair<int, int> pii;\ntypedef pair<ll, ll> pll;\ntypedef vector<int> vi;\n \n#define REP(i,x) for(int i=0;i<(int)(x);i++)\n#define REPS(i,x) for(int i=1;i<=(int)(x);i++)\n#define RREP(i,x) for(int i=((int)(x)-1);i>=0;i--)\n#define RREPS(i,x) for(int i=((int)(x));i>0;i--)\n#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();i++)\n#define RFOR(i,c) for(__typeof((c).rbegin())i=(c).rbegin();i!=(c).rend();i++)\n#define ALL(container) (container).begin(), (container).end()\n#define RALL(container) (container).rbegin(), (container).rend()\n#define SZ(container) ((int)container.size())\n#define mp(a,b) make_pair(a, b)\n#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );\n \ntemplate<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }\ntemplate<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }\ntemplate<class T> ostream& operator<<(ostream &os, const vector<T> &t) {\nos<<\"[\"; FOR(it,t) {if(it!=t.begin()) os<<\",\"; os<<*it;} os<<\"]\"; return os;\n}\ntemplate<class T> ostream& operator<<(ostream &os, const set<T> &t) {\nos<<\"{\"; FOR(it,t) {if(it!=t.begin()) os<<\",\"; os<<*it;} os<<\"}\"; return os;\n}\ntemplate<class S, class T> ostream& operator<<(ostream &os, const pair<S,T> &t) { return os<<\"(\"<<t.first<<\",\"<<t.second<<\")\";}\ntemplate<class S, class T> pair<S,T> operator+(const pair<S,T> &s, const pair<S,T> &t){ return pair<S,T>(s.first+t.first, s.second+t.second);}\ntemplate<class S, class T> pair<S,T> operator-(const pair<S,T> &s, const pair<S,T> &t){ return pair<S,T>(s.first-t.first, s.second-t.second);}\n \nnamespace geom{\n#define X real()\n#define Y imag()\n#define at(i) ((*this)[i])\n#define SELF (*this)\n\tenum {TRUE = 1, FALSE = 0, BORDER = -1};\n\ttypedef int BOOL;\n\ttypedef double R;\n\tconst R INF = 1e8;\n\tR EPS = 1e-6;\n\tconst R PI = 3.1415926535897932384626;\n\tinline int sig(const R &x) { return (abs(x) < EPS ? 0 : x > 0 ? 1 : -1); }\n\tinline BOOL less(const R &x, const R &y) {return sig(x-y) ? x < y : BORDER;}\n\ttypedef complex<R> P;\n\tinline R norm(const P &p){return p.X*p.X+p.Y*p.Y;}\n\tinline R inp(const P& a, const P& b){return (conj(a)*b).X;}\n\tinline R outp(const P& a, const P& b){return (conj(a)*b).Y;}\n\tinline P unit(const P& p){return p/abs(p);}\n\tinline P proj(const P &s, const P &t){return t*inp(s, t)/norm(t);}\n\tinline int ccw(const P &s, const P &t, const P &p, int adv=0){\n\t\tint res = sig(outp(t-s, p-s));\n\t\tif(res || !adv) return res;\n\t\tif(sig(inp(t-s, p-s)) < 0) return -2;\t// p-s-t\n\t\tif(sig(inp(s-t, p-t)) < 0) return 2; // s-t-p\n\t\treturn 0;\t\t\t\t\t\t\t // s-p-t\n\t}\n\t \n\t \n\tstruct L : public vector<P>{ // line\n\t\tL(const P &p1, const P &p2){this->push_back(p1);this->push_back(p2);}\n\t\tL(){}\n\t\tinline P dir()const {return at(1) - at(0);}\n\t\tBOOL online(const P &p)const {return !sig(outp(p-at(0), dir()));}\n\t};\n\tstruct S : public L{\t// segment\n\t\tS(const P &p1, const P &p2):L(p1, p2){}\n\t\tS(){}\n\t\tBOOL online(const P &p)const {\n\t\t\tif(!sig(norm(p - at(0))) || !sig(norm(p - at(1)))) return BORDER;\n\t\t\treturn !sig(outp(p-at(0), dir())) && inp(p-at(0), dir()) > -EPS && inp(p-at(1), -dir()) > -EPS ? TRUE: FALSE;\n\t\t\treturn !sig(abs(at(0)-p) + abs(at(1) - p) - abs(at(0) - at(1)));\n\t\t}\n\t};\n\tP crosspoint(const L &l, const L &m);\n\tstruct G : public vector<P>{\n\t\tG(size_type size=0):vector(size){}\n\t\tS edge(int i)const {return S(at(i), at(i+1 == size() ? 0 : i+1));}\n\t};\n \n\tinline BOOL intersect(const S &s, const L &l){\n\t\treturn (sig(outp(l.dir(), s[0]-l[0])) * sig(outp(l.dir(), s[1]-l[0])) <= 0);\n\t}\n\tinline P crosspoint(const L &l, const L &m){\n\t\tR A = outp(l.dir(), m.dir()), B = outp(l.dir(), l[1] - m[0]);\n\t\tif(!sig(abs(A)) && !sig(abs(B))) return m[0]; // same line\n\t\treturn m[0] + B / A * (m[1] - m[0]);\n\t}\n\t \n\tstruct Arrangement{\n\t\tstruct AEdge{\n\t\t\tint u, v, t;\n\t\t\tR cost;\n\t\t\tAEdge(int u=0, int v=0, int t=0, R cost=0)\n\t\t\t\t:u(u), v(v), t(t), cost(cost){}\n\t\t};\n\t\ttypedef vector<vector<AEdge>> AGraph;\n\t\tvector<P> p;\n\t\tAGraph g;\n\t\tArrangement(){}\n\t\tArrangement(vector<S> seg){\n\t\t\tint m = seg.size();\n\t\t\tREP(i, m){\n\t\t\t\tp.push_back(seg[i][0]);\n\t\t\t\tp.push_back(seg[i][1]);\n\t\t\t\tREP(j, i) if(sig(outp(seg[i].dir(), seg[j].dir())) && intersect(seg[i], seg[j]) == TRUE)\n\t\t\t\t\tp.push_back(crosspoint(seg[i], seg[j]));\n\t\t\t}\n\t\t\tsort(ALL(p)); UNIQUE(p);\n\t\t\tint n=p.size();\n\t\t\tg.resize(n);\n\t\t\tREP(i, m){\n\t\t\t\tS &s = seg[i];\n\t\t\t\tvector<pair<R, int>> ps;\n\t\t\t\tREP(j, n) if(s.online(p[j])) ps.emplace_back(norm(p[j] - s[0]), j);\n\t\t\t\tsort(ALL(ps));\n\t\t\t\tREP(j, (int)ps.size()-1){\n\t\t\t\t\tconst int u=ps[j].second;\n\t\t\t\t\tconst int v=ps[j+1].second;\n\t\t\t\t\tg[u].emplace_back(u, v, 0, abs(p[u] - p[v]));\n\t\t\t\t\tg[v].emplace_back(v, u, 0, abs(p[u] - p[v]));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\t \n\t\tint getIdx(P q){\n\t\t\tauto it = lower_bound(ALL(p), q);\n\t\t\tif(it == p.end() || *it != q) return -1;\n\t\t\treturn it - p.begin();\n\t\t}\n\t};\n \n\tstruct DualGraph{\n\t\tstruct DEdge{\n\t\t\tint u, v, f, l;\n\t\t\tR a;\n\t\t\tDEdge(int u, int v, R a):u(u),v(v),f(0),l(0){\n\t\t\t\twhile(PI < a) a -= 2*PI;\n\t\t\t\twhile(a < -PI) a += 2*PI;\n\t\t\t\tthis->a = a;\n\t\t\t}\n\t\t\tbool operator==(const DEdge &opp) const{\n\t\t\t\treturn v==opp.v;\n\t\t\t}\n\t\t\tbool operator<(const DEdge &opp) const{\n\t\t\t\treturn a>opp.a;\n\t\t\t}\n\t\t\tbool operator<(const R &opp) const{\n\t\t\t\treturn a>opp;\n\t\t\t}\n\t\t\tfriend ostream& operator<<(ostream &os, const DEdge &t) { return os<<\"(\"<<t.u<<\",\"<<t.v<<\",\"<<t.a*180/PI<<\")\";}\n\t\t};\n\t\t \n\t\tint n;\n\t\tvector<P> p;\n\t\tvector<vector<DEdge>> g;\n\t\tDualGraph(const vector<P> &p):p(p),g(p.size()),n(p.size()){}\n\t\tvoid add_edge(const int s, const int t){\n\t\t\tR a = arg(p[t]-p[s]);\n\t\t\tg[s].emplace_back(s, t, a);\n\t\t\tg[t].emplace_back(t, s, a > 0 ? a-PI : a+PI);\n\t\t}\n\t\tvoid add_polygon(int s, G &t, R a){\n\t\t\tauto e = lower_bound(ALL(g[s]), a-EPS);\n\t\t\tif(e == g[s].end()) e = g[s].begin();\n\t\t\tif(e->f) return;\n\t\t\te->f = 1;\n\t\t\tt.push_back(p[s]);\n\t\t\tadd_polygon(e->v, t, e->a > 0 ? e->a-PI : e->a+PI);\n\t\t}\n\t\t \n\t\tG dual(){\n\t\t\tREP(i, n){\n\t\t\t\tsort(ALL(g[i]));\n\t\t\t\tUNIQUE(g[i]);\n\t\t\t}\n\t\t\tint s = min_element(ALL(p)) - p.begin();\n\t\t\tG poly;\n\t\t\tadd_polygon(s, poly, -PI*(R).5);\n\t\t\treturn poly;\n\t\t}\n\t};\n \n#undef SELF\n#undef at\n}\n \nusing namespace geom;\n \nnamespace std{\n\tbool operator<(const P &a, const P &b){return sig(a.X-b.X) ? a.X < b.X : a.Y+EPS < b.Y;}\n\tbool operator==(const P &a, const P &b){return abs(a-b) < EPS;}\n\tistream& operator>>(istream &is, P &p){R x,y;is>>x>>y;p=P(x, y);return is;}\n}\n \nint n, m, k;\nstruct MSQ : public G{\n\tMSQ(){}\n\tvector<P> p;\n\tvector<S> s;\n\tint m, k;\n\tMSQ(int m, int k):m(m), k(k){\n\t\tREP(i, m) p.push_back(polar((R)1, 2*PI*i/m + PI*(R).5));\n\t\tREP(i, m) s.emplace_back(p[i], p[(i+k)%m]);\n\t\tArrangement a(s);\n\t\tDualGraph dg(a.p);\n\t\tREP(i, a.g.size())REP(j, a.g[i].size()){\n\t\t\tint u = a.g[i][j].u;\n\t\t\tint v = a.g[i][j].v;\n\t\t\tif(u < v) dg.add_edge(u, v);\n\t\t}\n\t\t(G &)(*this) = dg.dual();\n\t\treverse(this->begin(), this->end());\n\t}\n\tvoid copy(R r, P c, MSQ &msq)const {\n\t\tmsq.resize(size());\n\t\tmsq.p.resize(p.size());\n\t\tmsq.s.resize(s.size());\n\t\tmsq.m = m;\n\t\tmsq.k = k;\n\t\tREP(i, size()) msq[i] = at(i)*r + c;\n\t\tREP(i, p.size()) msq.p[i] = p[i]*r + c;\n\t\tREP(i, s.size()) msq.s[i] = S(msq.p[i], msq.p[(i+k)%m]);\n\t}\n\tS segment(int i)const {\n\t\treturn s[i];\n\t}\n};\nint convex_contains(const MSQ &msq, const P &g, const P &p) {\n\tconst int n = msq.size();\n\tint a = 0, b = n;\n\tconst P pg = p-g;\n\twhile (a+1 < b) { // invariant: c is in fan g-P[a]-P[b]\n\t\tint c = (a + b) / 2;\n\t\tif (outp(msq[a]-g, pg) > 0 && outp(msq[c]-g, pg) < 0) b = c;\n\t\telse a = c;\n\t}\n\tb %= n;\n\tif (outp(msq[a] - p, msq[b] - p) < -EPS) return 0;\n\treturn 1;\n}\nbool check(const MSQ & temp, R r, const vector<P> &vil, int i, int j){\n\tMSQ msq;\n\tL l = temp.segment(j);\n\tP gp = vil[i] - l[0]*r;\n\ttemp.copy(r, gp, msq);\n\tvector<P> p;\n\tP b(0), u = msq.segment(j)[1];\n\tif(u < b) swap(b, u);\n\tREP(i, n){\n\t\tL l2(vil[i], vil[i] + l.dir());\n\t\tint f = 0;\n\t\tP ll(INF, INF), rr(-INF, -INF);\n\t\tREP(j, m){\n\t\t\tconst S s = msq.segment(j);\n\t\t\tif(intersect(s, l2)){\n\t\t\t\tconst P q = crosspoint(s, l2) - l2[0];\n\t\t\t\tp.push_back(q);\n\t\t\t\tll = min(ll, q);\n\t\t\t\trr = max(rr, q);\n\t\t\t\tf = 1;\n\t\t\t}\n\t\t}\n\t\tu = min(rr, u);\n\t\tb = max(ll, b);\n\t\tif(!f) return false;\n\t}\n\tFOR(q, p){\n\t\tif(*q < b || u < *q) continue;\n\t\tif([&](){\n\t\t\tREP(i, n) if(!convex_contains(msq, gp, vil[i] + *q)) return 0;\n\t\t\treturn 1;\n\t\t}()) return true;\n\t}\n\treturn false;\n}\n \nint main(){\n\tios::sync_with_stdio(false);\n\twhile(cin >> n >> m >> k, n){\n\t\tMSQ temp(m, k);\n\t\tvector<P> vil(n);\n\t\tREP(i, n) cin >> vil[i];\n\t\tR best = 2000;\n\t\tREP(i, n)REP(j, m){\n\t\t\tif(!check(temp, best-EPS, vil, i, j)) continue;\n\t\t\tR l=1., r = best;\n\t\t\twhile(r-l>1e-6){\n\t\t\t\tR m = (l+r)*(R).5;\n\t\t\t\tif(check(temp, m, vil, i, j)) r = m;\n\t\t\t\telse l = m;\n\t\t\t}\n\t\t\tbest = r;\n\t\t}\n\t\tprintf(\"%.10f\\n\", (double)best);\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 2720, "memory_kb": 1524, "score_of_the_acc": -0.2396, "final_rank": 2 }, { "submission_id": "aoj_2203_1158428", "code_snippet": "#include <cstdio>\n#include <cmath>\n#include <cstring>\n#include <cstdlib>\n#include <climits>\n#include <ctime>\n#include <queue>\n#include <stack>\n#include <algorithm>\n#include <list>\n#include <vector>\n#include <set>\n#include <map>\n#include <iostream>\n#include <deque>\n#include <complex>\n#include <string>\n#include <iomanip>\n#include <sstream>\n#include <bitset>\n#include <valarray>\n#include <unordered_map>\n#include <iterator>\n#include <assert.h>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned int uint;\ntypedef unsigned char uchar;\ntypedef unsigned long long ull;\ntypedef pair<int, int> pii;\ntypedef pair<ll, ll> pll;\ntypedef vector<int> vi;\n \n#define REP(i,x) for(int i=0;i<(int)(x);i++)\n#define REPS(i,x) for(int i=1;i<=(int)(x);i++)\n#define RREP(i,x) for(int i=((int)(x)-1);i>=0;i--)\n#define RREPS(i,x) for(int i=((int)(x));i>0;i--)\n#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();i++)\n#define RFOR(i,c) for(__typeof((c).rbegin())i=(c).rbegin();i!=(c).rend();i++)\n#define ALL(container) (container).begin(), (container).end()\n#define RALL(container) (container).rbegin(), (container).rend()\n#define SZ(container) ((int)container.size())\n#define mp(a,b) make_pair(a, b)\n#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );\n \ntemplate<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }\ntemplate<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }\ntemplate<class T> ostream& operator<<(ostream &os, const vector<T> &t) {\nos<<\"[\"; FOR(it,t) {if(it!=t.begin()) os<<\",\"; os<<*it;} os<<\"]\"; return os;\n}\ntemplate<class T> ostream& operator<<(ostream &os, const set<T> &t) {\nos<<\"{\"; FOR(it,t) {if(it!=t.begin()) os<<\",\"; os<<*it;} os<<\"}\"; return os;\n}\ntemplate<class S, class T> ostream& operator<<(ostream &os, const pair<S,T> &t) { return os<<\"(\"<<t.first<<\",\"<<t.second<<\")\";}\ntemplate<class S, class T> pair<S,T> operator+(const pair<S,T> &s, const pair<S,T> &t){ return pair<S,T>(s.first+t.first, s.second+t.second);}\ntemplate<class S, class T> pair<S,T> operator-(const pair<S,T> &s, const pair<S,T> &t){ return pair<S,T>(s.first-t.first, s.second-t.second);}\n \nnamespace geom{\n#define X real()\n#define Y imag()\n#define at(i) ((*this)[i])\n#define SELF (*this)\n\tenum {TRUE = 1, FALSE = 0, BORDER = -1};\n\ttypedef int BOOL;\n\ttypedef double R;\n\tconst R INF = 1e8;\n\tR EPS = 1e-6;\n\tconst R PI = 3.1415926535897932384626;\n\tinline int sig(const R &x) { return (abs(x) < EPS ? 0 : x > 0 ? 1 : -1); }\n\tinline BOOL less(const R &x, const R &y) {return sig(x-y) ? x < y : BORDER;}\n\ttypedef complex<R> P;\n\tinline R norm(const P &p){return p.X*p.X+p.Y*p.Y;}\n\tinline R inp(const P& a, const P& b){return (conj(a)*b).X;}\n\tinline R outp(const P& a, const P& b){return (conj(a)*b).Y;}\n\tinline P unit(const P& p){return p/abs(p);}\n\tinline P proj(const P &s, const P &t){return t*inp(s, t)/norm(t);}\n\tinline int ccw(const P &s, const P &t, const P &p, int adv=0){\n\t\tint res = sig(outp(t-s, p-s));\n\t\tif(res || !adv) return res;\n\t\tif(sig(inp(t-s, p-s)) < 0) return -2;\t// p-s-t\n\t\tif(sig(inp(s-t, p-t)) < 0) return 2; // s-t-p\n\t\treturn 0;\t\t\t\t\t\t\t // s-p-t\n\t}\n\t \n\t \n\tstruct L : public vector<P>{ // line\n\t\tL(const P &p1, const P &p2){this->push_back(p1);this->push_back(p2);}\n\t\tL(){}\n\t\tinline P dir()const {return at(1) - at(0);}\n\t\tBOOL online(const P &p)const {return !sig(outp(p-at(0), dir()));}\n\t};\n\tstruct S : public L{\t// segment\n\t\tS(const P &p1, const P &p2):L(p1, p2){}\n\t\tS(){}\n\t\tBOOL online(const P &p)const {\n\t\t\tif(!sig(norm(p - at(0))) || !sig(norm(p - at(1)))) return BORDER;\n\t\t\treturn !sig(outp(p-at(0), dir())) && inp(p-at(0), dir()) > -EPS && inp(p-at(1), -dir()) > -EPS ? TRUE: FALSE;\n\t\t\treturn !sig(abs(at(0)-p) + abs(at(1) - p) - abs(at(0) - at(1)));\n\t\t}\n\t};\n\tP crosspoint(const L &l, const L &m);\n\tstruct G : public vector<P>{\n\t\tG(size_type size=0):vector(size){}\n\t\tS edge(int i)const {return S(at(i), at(i+1 == size() ? 0 : i+1));}\n\t};\n \n\tinline BOOL intersect(const S &s, const L &l){\n\t\treturn (sig(outp(l.dir(), s[0]-l[0])) * sig(outp(l.dir(), s[1]-l[0])) <= 0);\n\t}\n\tinline P crosspoint(const L &l, const L &m){\n\t\tR A = outp(l.dir(), m.dir()), B = outp(l.dir(), l[1] - m[0]);\n\t\tif(!sig(abs(A)) && !sig(abs(B))) return m[0]; // same line\n\t\tif(abs(A) < EPS) assert(false);\n\t\treturn m[0] + B / A * (m[1] - m[0]);\n\t}\n\t \n\tstruct Arrangement{\n\t\tstruct AEdge{\n\t\t\tint u, v, t;\n\t\t\tR cost;\n\t\t\tAEdge(int u=0, int v=0, int t=0, R cost=0)\n\t\t\t\t:u(u), v(v), t(t), cost(cost){}\n\t\t};\n\t\ttypedef vector<vector<AEdge>> AGraph;\n\t\tvector<P> p;\n\t\tAGraph g;\n\t\tArrangement(){}\n\t\tArrangement(vector<S> seg){\n\t\t\tint m = seg.size();\n\t\t\tREP(i, m){\n\t\t\t\tp.push_back(seg[i][0]);\n\t\t\t\tp.push_back(seg[i][1]);\n\t\t\t\tREP(j, i) if(sig(outp(seg[i].dir(), seg[j].dir())) && intersect(seg[i], seg[j]) == TRUE)\n\t\t\t\t\tp.push_back(crosspoint(seg[i], seg[j]));\n\t\t\t}\n\t\t\tsort(ALL(p)); UNIQUE(p);\n\t\t\tint n=p.size();\n\t\t\tg.resize(n);\n\t\t\tREP(i, m){\n\t\t\t\tS &s = seg[i];\n\t\t\t\tvector<pair<R, int>> ps;\n\t\t\t\tREP(j, n) if(s.online(p[j])) ps.emplace_back(norm(p[j] - s[0]), j);\n\t\t\t\tsort(ALL(ps));\n\t\t\t\tREP(j, (int)ps.size()-1){\n\t\t\t\t\tconst int u=ps[j].second;\n\t\t\t\t\tconst int v=ps[j+1].second;\n\t\t\t\t\tg[u].emplace_back(u, v, 0, abs(p[u] - p[v]));\n\t\t\t\t\tg[v].emplace_back(v, u, 0, abs(p[u] - p[v]));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\t \n\t\tint getIdx(P q){\n\t\t\tauto it = lower_bound(ALL(p), q);\n\t\t\tif(it == p.end() || *it != q) return -1;\n\t\t\treturn it - p.begin();\n\t\t}\n\t};\n \n\tstruct DualGraph{\n\t\tstruct DEdge{\n\t\t\tint u, v, f, l;\n\t\t\tR a;\n\t\t\tDEdge(int u, int v, R a):u(u),v(v),f(0),l(0){\n\t\t\t\twhile(PI < a) a -= 2*PI;\n\t\t\t\twhile(a < -PI) a += 2*PI;\n\t\t\t\tthis->a = a;\n\t\t\t}\n\t\t\tbool operator==(const DEdge &opp) const{\n\t\t\t\treturn v==opp.v;\n\t\t\t}\n\t\t\tbool operator<(const DEdge &opp) const{\n\t\t\t\treturn a>opp.a;\n\t\t\t}\n\t\t\tbool operator<(const R &opp) const{\n\t\t\t\treturn a>opp;\n\t\t\t}\n\t\t\tfriend ostream& operator<<(ostream &os, const DEdge &t) { return os<<\"(\"<<t.u<<\",\"<<t.v<<\",\"<<t.a*180/PI<<\")\";}\n\t\t};\n\t\t \n\t\tint n;\n\t\tvector<P> p;\n\t\tvector<vector<DEdge>> g;\n\t\tDualGraph(const vector<P> &p):p(p),g(p.size()),n(p.size()){}\n\t\tvoid add_edge(const int s, const int t){\n\t\t\tR a = arg(p[t]-p[s]);\n\t\t\tg[s].emplace_back(s, t, a);\n\t\t\tg[t].emplace_back(t, s, a > 0 ? a-PI : a+PI);\n\t\t}\n\t\tvoid add_polygon(int s, G &t, R a){\n\t\t\tauto e = lower_bound(ALL(g[s]), a-EPS);\n\t\t\tif(e == g[s].end()) e = g[s].begin();\n\t\t\tif(e->f) return;\n\t\t\te->f = 1;\n\t\t\tt.push_back(p[s]);\n\t\t\tadd_polygon(e->v, t, e->a > 0 ? e->a-PI : e->a+PI);\n\t\t}\n\t\t \n\t\tG dual(){\n\t\t\tREP(i, n){\n\t\t\t\tsort(ALL(g[i]));\n\t\t\t\tUNIQUE(g[i]);\n\t\t\t}\n\t\t\tint s = min_element(ALL(p)) - p.begin();\n\t\t\tG poly;\n\t\t\tadd_polygon(s, poly, -PI*(R).5);\n\t\t\treturn poly;\n\t\t}\n\t};\n \n#undef SELF\n#undef at\n}\n \nusing namespace geom;\n \nnamespace std{\n\tbool operator<(const P &a, const P &b){return sig(a.X-b.X) ? a.X < b.X : a.Y+EPS < b.Y;}\n\tbool operator==(const P &a, const P &b){return abs(a-b) < EPS;}\n\tistream& operator>>(istream &is, P &p){R x,y;is>>x>>y;p=P(x, y);return is;}\n}\n \nint n, m, k;\nstruct MSQ : public G{\n\tMSQ(){}\n\tvector<P> p;\n\tvector<S> s;\n\tint m, k;\n\tMSQ(int m, int k):m(m), k(k){\n\t\tREP(i, m) p.push_back(polar((R)1, 2*PI*i/m + PI*(R).5));\n\t\tREP(i, m) s.emplace_back(p[i], p[(i+k)%m]);\n\t\tArrangement a(s);\n\t\tDualGraph dg(a.p);\n\t\tREP(i, a.g.size())REP(j, a.g[i].size()){\n\t\t\tint u = a.g[i][j].u;\n\t\t\tint v = a.g[i][j].v;\n\t\t\tif(u < v) dg.add_edge(u, v);\n\t\t}\n\t\t(G &)(*this) = dg.dual();\n\t\treverse(this->begin(), this->end());\n\t}\n\tvoid copy(R r, P c, MSQ &msq)const {\n\t\tmsq.resize(size());\n\t\tmsq.p.resize(p.size());\n\t\tmsq.s.resize(s.size());\n\t\tmsq.m = m;\n\t\tmsq.k = k;\n\t\tREP(i, size()) msq[i] = at(i)*r + c;\n\t\tREP(i, p.size()) msq.p[i] = p[i]*r + c;\n\t\tREP(i, s.size()) msq.s[i] = S(msq.p[i], msq.p[(i+k)%m]);\n\t}\n\tS segment(int i)const {\n\t\treturn s[i];\n\t}\n};\nint convex_contains(const MSQ &msq, const P &g, const P &p) {\n\tconst int n = msq.size();\n\tint a = 0, b = n;\n\tconst P pg = p-g;\n\twhile (a+1 < b) { // invariant: c is in fan g-P[a]-P[b]\n\t\tint c = (a + b) / 2;\n\t\tif (outp(msq[a]-g, pg) > 0 && outp(msq[c]-g, pg) < 0) b = c;\n\t\telse a = c;\n\t}\n\tb %= n;\n\tif (outp(msq[a] - p, msq[b] - p) < -EPS) return 0;\n\treturn 1;\n}\nbool check(const MSQ & temp, R r, const vector<P> &vil, int i, int j){\n\tMSQ msq;\n\tL l = temp.segment(j);\n\tP gp = vil[i] - l[0]*r;\n\ttemp.copy(r, gp, msq);\n\tvector<P> p;\n\tP b(0), u = msq.segment(j)[1];\n\tif(u < b) swap(b, u);\n\tREP(i, n){\n\t\tL l2(vil[i], vil[i] + l.dir());\n\t\tint f = 0;\n\t\tP ll(INF, INF), rr(-INF, -INF);\n\t\tREP(j, m){\n\t\t\tconst S s = msq.segment(j);\n\t\t\tif(intersect(s, l2)){\n\t\t\t\tif(sig(outp(s.dir(), l2.dir()))){\n\t\t\t\t\tconst P q = crosspoint(s, l2) - l2[0];\n\t\t\t\t\tp.push_back(q);\n\t\t\t\t\tll = min(ll, q);\n\t\t\t\t\trr = max(rr, q);\n\t\t\t\t}\n\t\t\t\tf = 1;\n\t\t\t}\n\t\t}\n\t\tu = min(rr, u);\n\t\tb = max(ll, b);\n\t\tif(!f) return false;\n\t}\n\tFOR(q, p){\n\t\tif(*q < b || u < *q) continue;\n\t\tif([&](){\n\t\t\tREP(i, n) if(!convex_contains(msq, gp, vil[i] + *q)) return 0;\n\t\t\treturn 1;\n\t\t}()) return true;\n\t}\n\treturn false;\n}\n \nint main(){\n\tios::sync_with_stdio(false);\n\twhile(cin >> n >> m >> k, n){\n\t\tMSQ temp(m, k);\n\t\tvector<P> vil(n);\n\t\tREP(i, n) cin >> vil[i];\n\t\tR best = 2000;\n\t\tRREP(i, n)RREP(j, m){\n\t\t\tif(!check(temp, best-EPS, vil, i, j)) continue;\n\t\t\tR l=1., r = best;\n\t\t\twhile(r-l>1e-6){\n\t\t\t\tR m = (l+r)*(R).5;\n\t\t\t\tif(check(temp, m, vil, i, j)) r = m;\n\t\t\t\telse l = m;\n\t\t\t}\n\t\t\tbest = r;\n\t\t}\n\t\tprintf(\"%.10f\\n\", (double)best);\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 2720, "memory_kb": 1648, "score_of_the_acc": -0.2788, "final_rank": 9 } ]
aoj_2216_cpp
Problem A: Summer of KMC Description KMCはコミックマーケットと呼ばれる同人即売会で毎年CDを販売している。 コミックマーケットでCDを売ることになったFであったが、Fの人気により、KMCの売り場には人が殺到し、お釣りの計算が追いつかない状態となった。そこでFは金額を入力すると即座にお釣りを出力するプログラムを書くことにした。 KMCが釣り銭として用意しているのは、100円玉と500円玉と1000円札のみである。これらの硬貨・札は無限にあるものと考えて良い。お釣りは、硬貨・札の個数が最小になるように選ぶ。また、KMCが販売するCDの値段は100の倍数であり、購入者が支払う金額も100の倍数である。 Input 入力は複数のテストケースから成る。 各テストケースは2つの正整数A, Bから成る。AはCDの値段であり、Bは購入者が支払った金額である。A, Bは100の倍数であり、100000000を超えない。また、A ≦ B である。 入力は0 0で終わる。 Output 各テストケースに対して、お釣りとして差し出すべき 100円玉、500円玉、1000円札の個数・枚数を一行でこの順番に出力する。各数字の間にはスペースを1つ入れる。 Sample Input 500 1000 100 10000 400 700 600 5000 10000 10000 0 0 Output for Sample Input 0 1 0 4 1 9 3 0 0 4 0 4 0 0 0
[ { "submission_id": "aoj_2216_3702261", "code_snippet": "#include <iostream>\n#include <string>\n#include <algorithm>\n#include <functional>\n#include <vector>\n#include <utility>\n#include <cstring>\n#include <iomanip>\n#include <numeric>\n#include <limits>\n#include <cmath>\n#include <cassert>\n\nusing namespace std;\nusing ll = long long;\n\nconst int INF = 1<<30;\nconst int MOD = (int)1e9 + 7;\nconst int MAX_N = (int)1e5 + 5;\n#define debug(x) cout << #x << \": \" << x << endl\n\nsigned main(void)\n{\n cin.tie(0);\n ios::sync_with_stdio(false);\n int A, B;\n while(cin >> A >> B, A or B)\n {\n int coins[] = {1000, 500, 100};\n vector<int> cnt(3, 0);\n int M = B - A;\n int pos = 0;\n while(M > 0)\n {\n if(M - coins[pos] >= 0)\n {\n cnt[pos]++;\n M -= coins[pos];\n }\n else pos++;\n }\n for(int i = 2; i >= 0; i--)\n {\n if(i != 2) cout << \" \";\n cout << cnt[i];\n }\n cout << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3128, "score_of_the_acc": -1.25, "final_rank": 20 }, { "submission_id": "aoj_2216_2947846", "code_snippet": "#include<bits/stdc++.h>\n\nusing namespace std;\n\nint main()\n{\n while(1){\n int A,B;\n cin>>A;\n cin>>B;\n\n if(A == 0 && B == 0){\n break;\n }\n\n int money = B - A;\n int thousand = 0;\n int fhundred = 0;\n int hundred = 0;\n\n if(money >= 1000){\n while(1){\n ++thousand; \n money = money - 1000;\n if(money < 1000){\n break;\n }\n }\n }\n\n if(money >= 500){\n while(1){\n ++fhundred; \n money = money - 500;\n if(money < 500){\n break;\n }\n }\n }\n\n if(money >= 100){\n while(1){\n ++hundred; \n money = money - 100;\n if(money < 100){\n break;\n }\n }\n }\n\n cout<<hundred<<\" \"<<fhundred<<\" \"<<thousand<<endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3072, "score_of_the_acc": -0.9734, "final_rank": 16 }, { "submission_id": "aoj_2216_2947820", "code_snippet": "#include<bits/stdc++.h>\n\nusing namespace std;\n\nint main(){\n\n int a,b;\n\n while(cin>>a>>b,a||b){\n\tint change=b-a;\n\tint c100=0;\n\tint c500=0;\n\tint c1000=0;\n\n\twhile(1){\n\t if(change/1000==0)\n\t\tbreak;\n\t else{\n\t\tc1000++;\n\t\tchange-=1000;\n\t }\n\t}\n\n\twhile(1){\n\t if(change/500==0)\n\t\tbreak;\n\t else{\n\t\tc500++;\n\t\tchange-=500;\n\t }\n\t}\n\n\twhile(1){\n\t if(change/100==0)\n\t\tbreak;\n\t else{\n\t\tc100++;\n\t\tchange-=100;\n\t }\n\t}\n\n\tcout<<c100<<\" \"<<c500<<\" \"<<c1000<<endl;\n\t\n }\n \n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3080, "score_of_the_acc": -0.9772, "final_rank": 18 }, { "submission_id": "aoj_2216_2655982", "code_snippet": "#include <iostream>\n#include <cmath>\n#include <vector>\n#include <string>\n#include <map>\n#include <algorithm>\n#include <tuple>\n#include <set>\n#include <stack>\n#include <queue>\n#include <deque>\n#define REP(i, n) for(LL i = 0;i < n;i++)\n#define REPR(i, n) for(LL i = n;i >= 0;i--)\n#define FOR(i, m, n) for(LL i = m;i < n;i++)\n#define FORR(i, m, n) for(LL i = m;i >= n;i--)\n#define SORT(v, n) sort(v, v+n);\n#define VSORT(v) sort(v.begin(), v.end());\n#define pb(a) push_back(a)\n#define all(x) (x).begin(),(x).end()\n#define INF 999999999\n#define MOD 1000000007\nusing namespace std;\ntypedef long long LL;\ntypedef pair<int, int> P;\ntypedef pair<LL, LL> LP;\ntypedef pair<int, P> PP;\ntypedef pair<LL, LP> LPP;\nint dy[]={0, 0, 1, -1, 0};\nint dx[]={1, -1, 0, 0, 0};\n\n/*************** using variables ***************/\nint a, b;\nint hyaku, gohyaku, sen;\n/**********************************************/\n\nint main(){\n while(cin >> a >> b, a && b){\n hyaku = 0; gohyaku = 0; sen = 0;\n int zan = b - a;\n while(zan != 0){\n if(zan - 1000 >= 0){\n zan -= 1000;\n sen++;\n }else if(zan - 500 >= 0){\n zan -= 500;\n gohyaku++;\n }else{\n zan -= 100;\n hyaku++;\n }\n }\n cout << hyaku << \" \" << gohyaku << \" \" << sen << endl;\n }\n\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3076, "score_of_the_acc": -0.9753, "final_rank": 17 }, { "submission_id": "aoj_2216_2156757", "code_snippet": "#include<iostream>\n#include<vector>\n#include<string>\n#include<algorithm>\n#include<set>\nusing namespace std;\nint main() {\n\tint a, b;\n\twhile(cin>>a>>b&&a!=0&&b!=0){\n\t\tint h = 0, g = 0, s = 0,o=b-a;\n\t\twhile (o >= 1000) {\n\t\t\ts++;\n\t\t\to -= 1000;\n\t\t}\n\t\twhile (o >= 500) {\n\t\t\tg++;\n\t\t\to -= 500;\n\t\t}\n\t\twhile (o >= 100) {\n\t\t\th++;\n\t\t\to -= 100;\n\t\t}\n\t\tcout << h << \" \" << g << \" \" << s << endl;\n\t}\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3060, "score_of_the_acc": -0.9677, "final_rank": 15 }, { "submission_id": "aoj_2216_1600885", "code_snippet": "#include <cstdio>\n\nint main(){\n\tint a,b;\n\twhile(scanf(\"%d %d\", &a, &b)){\n\t\tif(a == 0 and b == 0) break;\n\t\tb -= a;\n\t\tint sen = 0, ghy = 0, hya = 0;\n\t\twhile(b >= 1000){\n\t\t\tb -= 1000;\n\t\t\tsen++;\n\t\t}\n\t\twhile(b >= 500){\n\t\t\tb -= 500;\n\t\t\tghy++;\n\t\t}\n\t\twhile(b >= 100){\n\t\t\tb -= 100;\n\t\t\thya++;\n\t\t}\n\t\tprintf(\"%d %d %d\\n\", hya, ghy, sen);\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1028, "score_of_the_acc": -0.0019, "final_rank": 2 }, { "submission_id": "aoj_2216_1550860", "code_snippet": "#include <cstdio>\n#include <algorithm>\n#include <iostream>\n#include <vector>\n#include <string>\n#include <cmath>\nusing namespace std;\n\nint main(){\n\tlong a,b,x,y,z;\n\twhile(1){\n\t\tcin >> a >> b;\n\t\tif((a==0)&&(b==0)){\n\t\t\treturn 0;\n\t\t}\n\t\ta=(b-a)/100;\n\t\tfor(x=0;a>=10;x++){\n\t\t\ta-=10;\n\t\t}\n\t\tfor(y=0;a>=5;y++){\n\t\t\ta-=5;\n\t\t}\n\t\tfor(z=0;a>=1;z++){\n\t\t\ta-=1;\n\t\t}\n\t\tcout << z <<\" \"<< y <<\" \"<<x<<\"\\n\";\n\t}\nreturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1160, "score_of_the_acc": -0.0646, "final_rank": 3 }, { "submission_id": "aoj_2216_1501825", "code_snippet": "#include <cstdio>\n\nint c[3]={1000,500,100};\nint cnt[3];\nint main(void){\n\twhile(1){\n\t\tint a,b;\n\t\tscanf(\"%d %d\",&a,&b);\n\t\tif(a+b==0)break;\n\t\tint rest=b-a;\n\t\tfor(int i=0;i<3;i++){\n\t\t\tcnt[i]=0;\n\t\t\twhile(rest>=c[i]){\n\t\t\t\tcnt[i]++;\n\t\t\t\trest-=c[i];\n\t\t\t}\n\t\t}\n\t\tprintf(\"%d %d %d\\n\",cnt[2],cnt[1],cnt[0]);\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1024, "score_of_the_acc": 0, "final_rank": 1 }, { "submission_id": "aoj_2216_1472734", "code_snippet": "#include <iostream>\nusing namespace std;\n\nint main() {\n\n int A,B;\n while(1) {\n cin >> A >> B;\n if(A == 0 && B == 0) break;\n\n int ans[3] ={};\n B -= A;\n while(B != 0) {\n if(B >= 1000) {\n ans[2]++;\n B -= 1000;\n }\n else if(B >= 500) {\n ans[1]++;\n B -= 500;\n }\n else if(B >= 100) {\n ans[0]++;\n B -=100;\n }\n }\n\n cout << ans[0] <<\" \"<<ans[1]<<\" \"<<ans[2]<<endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 1160, "score_of_the_acc": -0.148, "final_rank": 8 }, { "submission_id": "aoj_2216_1024770", "code_snippet": "#include<iostream>\n#include<string>\n#include<algorithm>\n#include<map>\n#include<set>\n#include<utility>\n#include<vector>\n#include<cmath>\n#include<cstdio>\n#define loop(i,a,b) for(int i=a;i<b;i++) \n#define rep(i,a) loop(i,0,a)\n#define pb push_back\n#define mp make_pair\n#define it ::iterator\n#define all(in) in.begin(),in.end()\nconst double PI=acos(-1);\nconst double ESP=1e-10;\nusing namespace std;\nint m[3]={1000,500,100};\nint main(){\n int a,b;\n while(cin>>a>>b,a||b){\n int tmp=b-a;\n int ans[3]={0};\n rep(i,3){\n while(tmp>=m[i]){\n\ttmp-=m[i];\n\tans[i]++;\n }\n }\n cout<<ans[2]<<\" \"<<ans[1]<<\" \"<<ans[0]<<endl;\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 1164, "score_of_the_acc": -0.1499, "final_rank": 13 }, { "submission_id": "aoj_2216_958961", "code_snippet": "#include<iostream>\nusing namespace std;\n\nint main(){\n\tint c[3];\n\tint a,b;\n\twhile(cin>>a>>b,a!=0&&b!=0){\n\t\tfor(int i=0;i<3;i++)c[i]=0;\n\t\tb-=a;\n\t\twhile(1000<=b){b-=1000;c[2]++;}\n\t\twhile(500<=b){b-=500;c[1]++;}\n\t\twhile(0<b){b-=100;c[0]++;}\n\t\t\n\t\tfor(int i=0;i<2;i++)cout<<c[i]<<\" \";\n\t\tcout<<c[2]<<endl;\n\t}\n\n\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 1160, "score_of_the_acc": -0.148, "final_rank": 8 }, { "submission_id": "aoj_2216_955705", "code_snippet": "//include\n//------------------------------------------\n#include <vector>\n#include <list>\n#include <map>\n#include <set>\n#include <deque>\n#include <stack>\n#include <bitset>\n#include <algorithm>\n#include <functional>\n#include <numeric>\n#include <utility>\n#include <sstream>\n#include <iostream>\n#include <iomanip>\n#include <cstdio>\n#include <cmath>\n#include <cstdlib>\n#include <cctype>\n#include <string>\n#include <cstring>\n#include <ctime>\n\nusing namespace std;\n\n//conversion\n//------------------------------------------\ninline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}\ntemplate<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}\n\n//math\n//-------------------------------------------\ntemplate<class T> inline T sqr(T x) {return x*x;}\n\n//typedef\n//------------------------------------------\ntypedef vector<int> VI;\ntypedef vector<VI> VVI;\ntypedef vector<string> VS;\ntypedef pair<int, int> PII;\ntypedef long long LL;\ntypedef unsigned long long ULL;\n\n//container util\n//------------------------------------------\n#define ALL(a) (a).begin(),(a).end()\n#define RALL(a) (a).rbegin(), (a).rend()\n#define PB push_back\n#define MP make_pair\n#define SZ(a) int((a).size())\n#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)\n#define EXIST(s,e) ((s).find(e)!=(s).end())\n#define SORT(c) sort((c).begin(),(c).end())\n\n//repetition\n//------------------------------------------\n#define FOR(i,a,b) for(int i=(a);i<(b);++i)\n#define REP(i,n) FOR(i,0,n)\n\n//constant\n//--------------------------------------------\nconst double EPS = 1e-10;\nconst double PI = acos(-1.0);\n\n//clear memory\n#define CLR(a) memset((a), 0 ,sizeof(a))\n\n//debug\n#define dump(x) cerr << #x << \" = \" << (x) << endl;\n#define debug(x) cerr << #x << \" = \" << (x) << \" (L\" << __LINE__ << \")\" << \" \" << __FILE__ << endl;\n\nint main() {\n int a, b;\n int coin[3] = {1000, 500, 100};\n int num[3];\n\n while(1) {\n cin >> a >> b;\n if(a+b == 0) break;\n\n int money = b-a;\n fill(num, num+3, 0);\n\n REP(i, 3) {\n if(money/coin[i] > 0) {\n\twhile(money/coin[i] != 0) {\n\t money -= coin[i];\n\t num[i]++;\n\t}\n }\n }\n cout << num[2] << ' ' << num[1] << ' ' << num[0] << endl;\n }\n}", "accuracy": 1, "time_ms": 130, "memory_kb": 1160, "score_of_the_acc": -1.0646, "final_rank": 19 }, { "submission_id": "aoj_2216_924643", "code_snippet": "#include <iostream>\n#include <cstdio>\n#include <cstring>\n#include <cstdlib>\n#include <cmath>\n#include <vector>\n#include <string>\n#include <map>\n#include <queue>\n#include <stack>\n#include <algorithm>\n\n#define rep(i,j) REP((i), 0, (j))\n#define REP(i,j,k) for(int i=(j);(i)<(k);++i)\n#define between(a,x,b) ((a)<=(x)&&(x)<=(b))\nusing namespace std;\n\nint main(){\n int a, b;\n while(scanf(\"%d%d\", &a, &b) && (a||b)){\n int change = b - a;\n int res[] = {0,0,0};\n while(change){\n if(change >= 1000){ change-=1000; res[2]++;}\n else if(change >= 500){ change-=500; res[1]++;}\n else {change-=100; res[0]++;}\n }\n\n printf(\"%d %d %d\\n\", res[0], res[1], res[2]);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 1188, "score_of_the_acc": -0.1613, "final_rank": 14 }, { "submission_id": "aoj_2216_919388", "code_snippet": "/*\n\t0071:Summer of KMC\n*/\n#include\t<iostream>\n\nusing namespace std;\n\nint main(void) {\n\twhile(1) {\n\t\tint a, b;\n\t\tint C[3] = { 0 };\n\t\tcin >> a >> b;\n\t\t\n\t\tif(a == 0 && b == 0) { break; }\n\t\t\n\t\tb -= a;\n\t\t\n\t\twhile(b >= 1000 != 0) { C[2]++; b -= 1000; }\n\t\twhile(b >= 500 != 0) { C[1]++; b -= 500; }\n\t\twhile(b >= 100 != 0) { C[0]++; b -= 100; }\n\t\t\n\t\tcout << C[0] << ' ' << C[1] << ' ' << C[2] << endl;\n\t}\n\t\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1160, "score_of_the_acc": -0.0646, "final_rank": 3 }, { "submission_id": "aoj_2216_870902", "code_snippet": "#include<cstdio>\n#include<iostream>\n\nusing namespace std;\n \nint main(void){\n \n int A,B;\n int turi;\n int money[3];\n int neda[3]={100,500,1000};\n\n while(1){\n\n\t cin>>A>>B;\n\t if(A==0&&B==0) break;\n\t turi=B-A;\n\n\t for(int i=2;i>=0;i--){\n\t\t money[i]=turi/neda[i];\n\t\t turi%=neda[i];\n\t }\n\n\t for(int i=0;i<2;i++){\n\t\t cout<<money[i]<<\" \";\n\t }\n\t cout<<money[2]<<endl;\n\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1160, "score_of_the_acc": -0.0646, "final_rank": 3 }, { "submission_id": "aoj_2216_844480", "code_snippet": "#include <iostream>\n#include <queue>\n#include <stack>\n#include <vector>\n#include <stdio.h>\n#include <algorithm>\n#include <string.h>\n#include <string>\n#include <cmath>\n#include <complex>\n#include <map>\nusing namespace std;\n\n#define rep(i,n) for(int i=0;i<int(n);++i)\n#define ALL(v) (v).begin(),(v).end()\n#define PB push_back\n#define EPS 1e-8\n#define F first\n#define S second\n\nstatic const double PI=6*asin(0.5);\ntypedef long long ll;\ntypedef complex<double> CP;\ntypedef pair<long long,int> P;\nstatic const int INF=1<<24;\n\nint main(){\n\tint s,g,h;\n\tint a,b;\n\t\n\twhile(cin>>a>>b,a||b){\n\t\ts=g=h=0;\n\t\tint t=b-a;\n\t\twhile(t>=1000){\n\t\t\tt-=1000;\n\t\t\ts++;\n\t\t}\n\t\twhile(t>=500){\n\t\t\tt-=500;\n\t\t\tg++;\n\t\t}\n\t\twhile(t>=100){\n\t\t\tt-=100;\n\t\t\th++;\n\t\t}\n\t\tcout<<h<<\" \"<<g<<\" \"<<s<<endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1160, "score_of_the_acc": -0.0646, "final_rank": 3 }, { "submission_id": "aoj_2216_833966", "code_snippet": "#include <iostream>\nusing namespace std;\n\nint main(){\n int a, b;\n while((cin >> a >> b), a, b){\n int n1=0, n2=0, n3=0;\n a = b-a;\n while(a>=1000){\n n3++;\n a-=1000;\n }\n while(a>=500){\n n2++;\n a-=500;\n }\n while(a>=100){\n n1++;\n a-=100;\n }\n cout << n1 << \" \" << n2 << \" \" << n3 << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 1160, "score_of_the_acc": -0.148, "final_rank": 8 }, { "submission_id": "aoj_2216_809768", "code_snippet": "#include <iostream>\nusing namespace std;\n\nint main() {\n int n,m;\n while(cin >> n >> m && (n || m)) {\n n=m-n;\n int a[3]={0,0,0};\n while(n>=1000) {\n n-=1000;\n a[2]++;\n }\n while(n>=500) {\n n-=500;\n a[1]++;\n }\n while(n>=100) {\n n-=100;\n a[0]++;\n }\n cout << a[0] << \" \" << a[1] << \" \" << a[2] << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 1160, "score_of_the_acc": -0.148, "final_rank": 8 }, { "submission_id": "aoj_2216_768054", "code_snippet": "#include<iostream>\nusing namespace std;\nint main(){\n int CD,money,oo,go,ooo;\n while(1){\n cin >> CD >> money;\n if(CD==0 && money==0){\n break;\n }\n oo=0;\n go=0;\n ooo=0;\n\n money-=CD;\n\n while(money!=0){\n if(money>=1000){\n\tmoney-=1000;\n\tooo++;\n }\n else if(money>=500){\n\tmoney-=500;\n\tgo++;\n }\n else if(money>=100){\n\tmoney-=100;\n\too++;\n }\n }\n cout << oo << \" \" << go << \" \" << ooo << endl;\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 1160, "score_of_the_acc": -0.148, "final_rank": 8 }, { "submission_id": "aoj_2216_756913", "code_snippet": "#include <iostream>\n#include <vector>\n#include <stack>\n#include <queue>\n#include <map>\n#include <bitset>\n#include <algorithm>\n#include <cstdio>\n#include <cstdlib>\n#include <cmath>\n#include <cstring>\n\n#define X first\n#define Y second\nusing namespace std;\n\nint main(){\n\tint kind[] = {1000, 500, 100};\n\tint n, m;\n\twhile(cin >> n >> m && (n!=0||m!=0)){\n\t\tint count[] = {0, 0, 0};\n\t\tint oturi=m-n;\n\t\tfor(int i = 0; i < 3; i++){\n\t\t\twhile(oturi>=kind[i]){\n\t\t\t\tcount[i]++;\n\t\t\t\toturi -= kind[i];\n\t\t\t}\n\t\t}\n\t\tcout << count[2] << \" \" << count[1] << \" \" << count[0] << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1160, "score_of_the_acc": -0.0646, "final_rank": 3 } ]
aoj_2214_cpp
Problem K: ワープホール 20XX年、遠く離れた場所に対する効率的な物質転送方式が確立され、人類の宇宙への進出はますます加速されることになった。この転送方式は、より遠くに物質を転送しようとすると、原則的により大きな物質の転送が可能になる点で革新的であった。転送方式の詳細を次に記す。 まず転送対象となる物質は、スタート座標(1, 1)にて単位質量毎のパーティクルに分割される。各パーティクルには、各々異なる波動エネルギーが与えられなければならない。波動エネルギーは、 V と H からなる任意の長さの文字列で表現され、パーティクルの宇宙空間における漂い方を規定する。 V は座標上(+1, 0)の移動を意味し、 H は(0, +1)の移動を意味する。例えば、 VHHV という波動エネルギーが与えられたパーティクルは、(2, 1), (2, 2), (2, 3) という軌跡で宇宙を漂った後、座標(3, 3)に辿り着く。 また、宇宙空間には複数のワープホールが存在し、パーティクルの軌跡に影響を与える。ワープホールには入口と出口があり、パーティクルがワープホールの入口に移動した場合、必ず出口の座標にワープすることになる。i番目のワープホールの入口と出口の座標を(a i , b i ), (c i , d i )とすると、a i ≤ c i , b i ≤ d i , (a i , b i ) ≠ (c i , d i )の条件が満たされており、ワープホールの入口は、他のワープホールの入口と同じ座標や、(1, 1)には存在しないことが保証されている。ただし、複数の出口が同じ場所にあったり、あるワープホールの出口に別のワープホールの入口がある場合はある(この場合は連続でワープする)。 例えば、(1, 2)を入口とし、(3, 2)を出口とするワープホールが存在した場合、 HH と波動エネルギーを与えられたパーティクルは、(1, 2)へ移動後、(3, 2)へワープし、 (3, 3)へと辿り着く。 パーティクルは波動エネルギーを与えられると一瞬でそれに従って移動する。全てのパーティクルが同時に目的地点へ辿り着くと、自動的に元の物質へと再構成される。 あなたは宇宙開発機構のプログラマである。転送の目的座標 (N, M) と K 個のワープホールの座標対が与えられるので、一度に転送可能な物質の最大質量を求めるプログラムを書いてほしい。答えは非常に大きい数になる可能性があるため、1,000,000,007で割った余りを出力せよ。 Input N M K a 1 b 1 c 1 d 1 ... a K b K c K d K 1 ≤ N,M ≤ 10 5 , 0 ≤ K ≤ 10 3 を満たす。また、 任意の 1 ≤ i ≤ K に対して a i ≤ c i , b i ≤ d i , (a i , b i ) ≠ (c i , d i ) を満たす。 Output 最大質量を 1,000,000,007 で割ったあまりを1行で出力せよ。 Notes on Test Cases 上記入力形式で複数のデータセットが与えられます。各データセットに対して上記出力形式で出力を行うプログラムを作成して下さい。 N, M, K がすべて 0 のとき入力の終わりを示します。 Sample Input 4 4 1 2 2 3 3 1 4 1 1 2 1 3 5 5 2 2 2 3 4 3 3 5 3 5 5 3 4 4 5 5 2 2 3 3 3 3 4 4 100000 100000 1 2 2 99999 99999 1 1 0 0 0 0 Output for Sample Input 12 1 26 18 615667476 1
[ { "submission_id": "aoj_2214_10866029", "code_snippet": "#include <bits/stdc++.h>\n\n#define rep(i, j, k) for (int i = j; i < k; ++i)\n#define irep(i, j, k) for (int i = j - 1; i >= k; --i)\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef long double ld;\ntypedef pair<int, int > Pii;\ntypedef pair<Pii, Pii > Tii;\n\nconst double pi = acos(-1.0);\nconst int INF = INT_MAX;\nconst ll LLINF = LLONG_MAX;\nconst int MAX_N = 1e5 + 10;\nconst int MOD = 1e9 + 7;\n\ninline int add(int a, int b) { return (a + b) % MOD;}\ninline int mul(int a, int b) { return 1ll * a * b % MOD;}\ninline int del(int a, int b) { return (a - b + MOD) % MOD;}\n\nint N, M, K, dp[MAX_N], a, b, c, d;\nint fact[2 * MAX_N];\nvector<Tii> v;\n\nvoid init_fact() {\n\tfact[0] = 1;\n\trep(i, 1, 2 * MAX_N) fact[i] = mul(i, fact[i - 1]);\n}\nint inv(int x) {\n\treturn x == 1? 1 : (MOD - mul((MOD / x), inv(MOD % x)) % MOD) % MOD;\n}\nint C(int n, int k) {\n\treturn mul(mul(fact[n], inv(fact[k])), inv(fact[n - k]));\n}\nint cal(Pii p1, Pii p2) {\n\tif (p2.first < p1.first || p2.second < p1.second) return 0;\n\treturn C(p2.first + p2.second - p1.first - p1.second, p2.first - p1.first);\n}\nvoid solve() {\n\tmemset(dp, 0, sizeof dp);\n\tsort(v.begin(), v.end());\n\tv.push_back(Tii(Pii(N, M), Pii(N + 1, M + 1)));\n\trep(i, 0, K + 1) {\n\t\tdp[i] = C(v[i].first.first + v[i].first.second, v[i].first.first);\n\t\trep(j, 0, i) \n\t\t\tdp[i] = add(dp[i], mul(dp[j], del(cal(v[j].second, v[i].first), cal(v[j].first, v[i].first))));\n\t}\n\tprintf(\"%d\\n\", dp[K]);\n}\nint main(int argc, char const *argv[])\n{\n\tinit_fact();\n\twhile (scanf(\"%d%d%d\", &N, &M, &K), N + M + K) {\n\t\t--N, --M;\n\t\tv.clear();\n\t\trep(i, 0, K) {\n\t\t\tscanf(\"%d%d%d%d\", &a, &b, &c, &d);\n\t\t\tv.push_back(Tii(Pii(a - 1, b - 1), Pii(c - 1, d - 1)));\n\t\t}\n\t\tsolve();\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 610, "memory_kb": 4576, "score_of_the_acc": -0.7575, "final_rank": 10 }, { "submission_id": "aoj_2214_9664007", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nconst long long MOD = 1000000007;\n\nlong long modPow(long long base, long long exp, long long mod) {\n long long res = 1;\n while (exp > 0) {\n if (exp % 2 == 1) res = (res * base) % mod;\n base = (base * base) % mod;\n exp /= 2;\n }\n return res;\n}\n\nstruct Factorial {\n vector<long long> fact;\n \n Factorial(int n) {\n fact.resize(n + 1);\n fact[0] = 1;\n for (int i = 1; i <= n; ++i) {\n fact[i] = (fact[i - 1] * i) % MOD;\n }\n }\n\n long long comb(int a, int b) {\n if (b > a || b < 0) return 0;\n long long num = fact[a];\n long long denom = (fact[b] * fact[a - b]) % MOD;\n return (num * modPow(denom, MOD - 2, MOD)) % MOD;\n }\n\n long long paths(int sx, int sy, int tx, int ty) {\n if (sx > tx || sy > ty) return 0;\n int totalMoves = (tx - sx) + (ty - sy);\n return comb(totalMoves, tx - sx);\n }\n};\n\nstruct Warp {\n int x1, y1, x2, y2;\n};\n\nint main() {\n ios::sync_with_stdio(0);\n cin.tie(0);\n\n int n, m, k;\n Factorial fact(200000);\n\n while (cin >> m >> n >> k) {\n if (n == 0 && m == 0 && k == 0) break;\n\n vector<Warp> warps(k);\n for (int i = 0; i < k; ++i) {\n cin >> warps[i].x1 >> warps[i].y1 >> warps[i].x2 >> warps[i].y2;\n warps[i].x1--; warps[i].y1--;\n warps[i].x2--; warps[i].y2--;\n }\n\n warps.push_back({m - 1, n - 1, m - 1, n - 1});\n sort(warps.begin(), warps.end(), [](const Warp &a, const Warp &b) {\n if (a.x1 != b.x1) return a.x1 < b.x1;\n return a.y1 < b.y1;\n });\n\n vector<long long> dp(warps.size(), 0);\n for (int i = 0; i < warps.size(); ++i) {\n dp[i] = fact.paths(0, 0, warps[i].x1, warps[i].y1);\n for (int j = 0; j < i; ++j) {\n long long warpPaths = fact.paths(warps[j].x2, warps[j].y2, warps[i].x1, warps[i].y1);\n long long directPaths = fact.paths(warps[j].x1, warps[j].y1, warps[i].x1, warps[i].y1);\n dp[i] = (dp[i] + dp[j] * ((warpPaths - directPaths + MOD) % MOD)) % MOD;\n }\n }\n cout << dp.back() << endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 220, "memory_kb": 4632, "score_of_the_acc": -0.3102, "final_rank": 5 }, { "submission_id": "aoj_2214_4898416", "code_snippet": "/*\n对k个虫洞使用增量法,按起点坐标pair由大到小加入影响。\n影响使用容斥计算即可。\n代码中f[i] = 在目前的影响下(x[i], y[i])到终点的方案数。\n*/ \n#include <iostream>\n#include <cstdio>\n#include <cstring>\n#include <algorithm>\n#include <cmath>\n#include <utility>\n#include <queue>\nusing namespace std;\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef long double ldouble;\nconst int LEN = 100000;\n\nstruct fastio {\n int it, len;\n char s[LEN + 5];\n fastio() {\n it = len = 0;\n }\n char get() {\n if (it < len) return s[it++];\n it = 0, len = fread(s, 1, LEN, stdin);\n return len ? s[it++] : EOF;\n }\n bool notend() {\n char c;\n for (c = get(); c == ' ' || c == '\\n' || c == '\\r'; c = get());\n if (it) it--;\n return c != EOF;\n }\n void put(char c) {\n if (it == LEN) fwrite(s,1,LEN,stdout), it = 0;\n s[it++] = c;\n }\n void flush() {\n fwrite(s, 1, it, stdout);\n }\n}buff, bufo;\ninline int getint() {\n char c; int res = 0, sig = 1;\n for (c = buff.get(); c < '0' || c > '9'; c = buff.get()) if(c == '-') sig = -1;\n for (; c >= '0' && c <= '9'; c = buff.get()) res = res * 10 + (c - '0');\n return sig * res;\n}\ninline ll getll() {\n char c; ll res = 0, sig = 1;\n for (c = buff.get(); c < '0' || c > '9'; c = buff.get()) if (c == '-') sig = -1;\n for (; c >= '0' && c <= '9'; c = buff.get()) res = res * 10 + (c - '0');\n return sig * res;\n}\ninline void putint(int x, char suf) {\n if (!x) bufo.put('0');\n else {\n if (x < 0) bufo.put('-'), x = -x;\n int k = 0; char s[15];\n while (x) {\n s[++k] = x % 10 + '0';\n x /= 10;\n }\n for (; k; k--) bufo.put(s[k]);\n }\n bufo.put(suf);\n}\ninline void putll(ll x, char suf) {\n if (!x) bufo.put('0');\n else {\n if (x < 0) bufo.put('-'), x = -x;\n int k = 0; char s[25];\n while (x) {\n s[++k] = x % 10 + '0';\n x /= 10;\n }\n for (; k; k--) bufo.put(s[k]);\n }\n bufo.put(suf);\n}\ninline char get_char() {\n char c;\n for (c = buff.get(); c == ' ' || c == '\\n' || c == '\\r'; c = buff.get());\n return c;\n}\n\n#define maxn 200005\n#define maxk 1005\n#define mp make_pair\nconst ll mod = 1000000007;\n\nll modpow(ll x, ll y) {\n ll res = 1;\n while (y) {\n if (y & 1) (res *= x) %= mod;\n y >>= 1;\n (x *= x) %= mod;\n }\n return res;\n}\nll modInv(ll x) { return modpow(x, mod - 2); }\n\nll fct[maxn], invFct[maxn];\nll C(int x, int y) {\n return fct[x] * invFct[y] % mod * invFct[x - y] % mod;\n}\n\nint n, m, k, x[maxk << 1], y[maxk << 1];\nll f[maxk << 1];\nstruct Hole {\n int a, b, c, d;\n bool operator < (const Hole &h) const {\n return mp(a, b) > mp(h.a, h.b);\n }\n}h[maxk];\n\nvoid solve() {\n for (int i = 1; i <= k; i++) {\n h[i].a = getint(), h[i].b = getint(),\n h[i].c = getint(), h[i].d = getint();\n }\n sort(h + 1, h + k + 1);\n x[1] = y[1] = 1;\n x[2 * k + 2] = n, y[2 * k + 2] = m;\n for (int i = 1; i <= k; i++) {\n x[i * 2] = h[i].a, y[i * 2] = h[i].b;\n x[i * 2 + 1] = h[i].c, y[i * 2 + 1] = h[i].d;\n }\n int p = 2 * k + 2;\n for (int i = 1; i <= p; i++) f[i] = C(n - x[i] + m - y[i], n - x[i]);\n for (int i = 1; i <= k; i++) {\n ll coe = f[i * 2 + 1] - f[i * 2];\n for (int j = 1; j <= p; j++) {\n if (x[j] <= x[i * 2] && y[j] <= y[i * 2]) {\n (f[j] += C(x[i * 2] - x[j] + y[i * 2] - y[j], x[i * 2] - x[j]) * coe) %= mod;\n }\n }\n }\n putll((f[1] + mod) % mod, '\\n');\n}\n\nint main() {\n fct[0] = 1;\n for (int i = 1; i <= 200000; i++) fct[i] = fct[i - 1] * i % mod;\n invFct[200000] = modInv(fct[200000]);\n for (int i = 200000; i >= 1; i--) invFct[i - 1] = invFct[i] * i % mod;\n while ((n = getint()) + (m = getint()) + (k = getint())) solve();\n bufo.flush();\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 6396, "score_of_the_acc": -0.2615, "final_rank": 3 }, { "submission_id": "aoj_2214_4300678", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 99999999999999999\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n\n#define SIZE 200005\n\nstruct Info{\n\tInfo(ll arg_row1,ll arg_col1,ll arg_row2,ll arg_col2){\n\t\trow1 = arg_row1;\n\t\tcol1 = arg_col1;\n\t\trow2 = arg_row2;\n\t\tcol2 = arg_col2;\n\t}\n\tbool operator<(const struct Info &arg) const{\n\n\t\tif(row1 != arg.row1){\n\n\t\t\treturn row1 < arg.row1;\n\t\t}else{\n\n\t\t\treturn col1 < arg.col1;\n\t\t}\n\t}\n\n\tll row1,col1,row2,col2;\n};\n\nll H,W,K;\nll dp[1005];\nll fact[SIZE];\n\n\nll extgcd(ll a,ll b,ll &x,ll &y){\n\tll d = a;\n\tif(b != 0){\n\t\td = extgcd(b,a%b,y,x);\n\t\ty -= (a/b)*x;\n\t}else{\n\t\tx = 1;\n\t\ty = 0;\n\t}\n\treturn d;\n}\n\nll mod_pow(ll x,ll count, ll mod){\n\n\tif(count == 0)return 1;\n\tll ret = mod_pow((x*x)%mod,count/2,mod);\n\tif(count%2 == 1){\n\n\t\tret = (ret*x)%mod;\n\t}\n\treturn ret;\n}\n\n\nll mod_inverse(ll a,ll m){\n ll x,y;\n extgcd(a,m,x,y);\n return (m+x%m)%m;\n}\n\nll mod_fact(ll n,ll p,ll &e){\n\te = 0;\n\tif(n == 0)return 1;\n\n\tint res = mod_fact(n/p,p,e);\n\te += n/p;\n\n\tif(n/p%2 != 0)return res*(p-fact[n%p])%p;\n\treturn res*fact[n%p]%p;\n}\n\n\nll nCm(ll n,ll m,ll p){\n if(n < m) return 0;\n ll e1,e2,e3;\n ll a1 = mod_fact(n,p,e1),a2 = mod_fact(m,p,e2),a3 = mod_fact(n-m,p,e3);\n if(e1 > e2+e3)return 0;\n return a1 * mod_inverse(a2*a3%p,p)%p;\n}\n\nvoid func(){\n\n\tvector<Info> V;\n\n\tll col1,row1,col2,row2;\n\n\tfor(int loop = 0; loop < K; loop++){\n\n\t\tscanf(\"%lld %lld %lld %lld\",&col1,&row1,&col2,&row2);\n\n\t\tV.push_back(Info(row1,col1,row2,col2));\n\t}\n\n\tsort(V.begin(),V.end());\n\n\tll minus,A,B;\n\n\tfor(int i = 0; i < V.size(); i++){\n\n\t\tdp[i] = nCm(V[i].row1+V[i].col1-2,V[i].row1-1,MOD);\n\n\t\tfor(int k = 0; k <= i-1; k++){\n\t\t\tif(V[k].col1 > V[i].col1)continue;\n\n\t\t\tA = nCm((V[i].row1-V[k].row1+V[i].col1-V[k].col1),V[i].row1-V[k].row1,MOD);\n\t\t\tif(V[k].col2 > V[i].col1 || V[k].row2 > V[i].row1){\n\n\t\t\t\tB = 0;\n\n\t\t\t}else{\n\n\t\t\t\tB = nCm((V[i].row1-V[k].row2+V[i].col1-V[k].col2),V[i].row1-V[k].row2,MOD);\n\t\t\t}\n\n\t\t\tminus = (A-B+MOD)%MOD;\n\t\t\tminus *= dp[k];\n\t\t\tminus %= MOD;\n\n\t\t\tdp[i] = (dp[i]-minus+MOD)%MOD;\n\t\t}\n\t}\n\n\tll ans = nCm(H+W-2,H-1,MOD);\n\n\tfor(int i = 0; i < V.size(); i++){\n\n\t\tA = nCm(H-V[i].row1+W-V[i].col1,H-V[i].row1,MOD);\n\t\tB = nCm(H-V[i].row2+W-V[i].col2,H-V[i].row2,MOD);\n\n\t\tminus = (A-B+MOD)%MOD;\n\t\tminus *= dp[i];\n\t\tminus %= MOD;\n\n\t\tans = (ans-minus+MOD)%MOD;\n\t}\n\n\tprintf(\"%lld\\n\",ans);\n}\n\n\nint main(){\n\n\tfact[0] = 1;\n\tfor(ll i = 1; i < SIZE; i++){\n\t\tfact[i] = i*fact[i-1];\n\t\tfact[i] %= MOD;\n\t}\n\n\twhile(true){\n\n\t\tscanf(\"%lld %lld %lld\",&W,&H,&K);\n\t\tif(H == 0 && W == 0 && K == 0)break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 720, "memory_kb": 4724, "score_of_the_acc": -0.9018, "final_rank": 11 }, { "submission_id": "aoj_2214_4295973", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define ll long long\n#define inc(i, l, r) for (int i = l; i <= r; i++)\n#define dec(i, l, r) for (int i = l; i >= r; i--)\n#define pii pair<int, int>\n#define fi first\n#define se second\n#define pb push_back\n\nconst int maxn = 1e3 + 5;\nconst int maxnum = 2e5;\nconst int mod = 1e9 + 7;\n\nint add(int a, int b) { return (a + b) % mod; }\nint sub(int a, int b) { return (a - b + mod) % mod; }\nint mul(int a, int b) { return 1LL * a * b % mod; }\n\nstruct worm {\n int x1, y1, x2, y2;\n bool operator<(const worm &o) const {\n if (x1 != o.x1) return x1 < o.x1;\n return y1 < o.y1;\n }\n} w[maxn];\n\nint dp[maxn], n, m, k;\n\ninline ll ksm(ll _a, ll _n) {\n ll _r = 1;\n while (_n) {\n if (_n & 1) _r = _r * _a % mod;\n _a = _a * _a % mod;\n _n >>= 1;\n }\n return _r;\n}\n\nint fac[maxnum + 5];\nint C(int _n, int _m) {\n return mul(mul(fac[_n], ksm(fac[_n - _m], mod - 2)), ksm(fac[_m], mod - 2));\n}\n\nint cal(int x1, int y1, int x2, int y2) {\n if (x1 > x2 || y1 > y2) return 0;\n return C(x2 - x1 + y2 - y1, x2 - x1);\n}\n\nint main() {\n fac[0] = 1;\n inc(i, 1, maxnum) fac[i] = mul(fac[i - 1], i);\n while (scanf(\"%d %d %d\", &n, &m, &k) != EOF && n) {\n inc(i, 0, k - 1)\n scanf(\"%d %d %d %d\", &w[i].x1, &w[i].y1, &w[i].x2, &w[i].y2);\n\n sort(w, w + k);\n w[k] = {n, m, n, m};\n for (int i = 0; i <= k; i++) {\n dp[i] = cal(1, 1, w[i].x1, w[i].y1);\n for (int j = 0; j < i; j++) {\n dp[i] = sub(\n dp[i], mul(dp[j], cal(w[j].x1, w[j].y1, w[i].x1, w[i].y1)));\n dp[i] = add(\n dp[i], mul(dp[j], cal(w[j].x2, w[j].y2, w[i].x1, w[i].y1)));\n }\n }\n printf(\"%d\\n\", dp[k]);\n }\n}", "accuracy": 1, "time_ms": 490, "memory_kb": 4036, "score_of_the_acc": -0.5581, "final_rank": 9 }, { "submission_id": "aoj_2214_3350074", "code_snippet": "#include <iostream>\n#include <bits/stdc++.h>\n \nusing namespace std;\n \n#define MOD 1000000007\n \ninline int add(int a, int b)\n{ return (a + b) % MOD; }\n \ninline int sub(int a, int b)\n{ return (a - b + MOD) % MOD; }\n \ninline int mul(int a, int b)\n{ return 1LL * a * b % MOD; }\n \n// 虫洞\nstruct warp\n{\n int x1, y1, x2, y2;\n \n bool operator<(const warp &w) const\n {\n if (x1 != w.x1)\n return x1 < w.x1;\n return y1 < w.y1;\n }\n};\n \n// return d = gcd(a, b) = ax + by\nint extgcd(int a, int b, int &x, int &y)\n{\n int g = a;\n x = 1;\n y = 0;\n if (b != 0)\n g = extgcd(b, a % b, y, x), y -= a / b * x;\n return g;\n}\n \n// 求a的逆元\nint inv(int a)\n{\n int x, y;\n extgcd(a, MOD, x, y);\n return (MOD + x % MOD) % MOD;\n}\n \nint fact[200010]; // 阶乘n!\nvoid init_factorial()\n{\n fact[0] = 1;\n for (int i = 0; i < 200000; ++i)\n fact[i + 1] = mul(fact[i], i + 1);\n}\n \n// 组合数(从n个序列空位中挑出k个)\ninline int nck(int n, int k)\n{ return mul(mul(fact[n], inv(fact[n - k])), inv(fact[k])); }\n \n \nint N, M, K, dp[100010];\n// dp[i] := 到第i个虫洞入口时的方案数\nvector<warp> warps;\n \ninline int calc(int x1, int y1, int x2, int y2) // 无虫洞模式下s到t的方案数\n{\n if (x2 < x1 or y2 < y1)\n return 0;\n return nck(x2 - x1 + y2 - y1, x2 - x1);\n}\n \nint solve()\n{\n memset(dp, 0, sizeof(dp));\n sort(begin(warps), end(warps));\n warps.push_back((warp) {N, M, N + 1, M + 1});\n for (int i = 0; i <= K; ++i)\n {\n dp[i] = nck(warps[i].x1 + warps[i].y1, warps[i].y1);\n for (int j = 0; j < i; ++j)\n {\n dp[i] = add(dp[i], mul(dp[j], sub(calc(warps[j].x2, warps[j].y2, warps[i].x1, warps[i].y1), // j的终点到i的起点(紫色)\n calc(warps[j].x1, warps[j].y1, warps[i].x1, warps[i].y1)))); // j的起点到i的起点\n }\n }\n return dp[K];\n}\n \nbool input()\n{\n warps.clear();\n scanf(\"%d%d%d\", &N, &M, &K);\n --N;\n --M;\n int a, b, c, d;\n for (int i = 0; i < K; ++i)\n {\n scanf(\"%d%d%d%d\", &a, &b, &c, &d);\n warps.push_back((warp) {a - 1, b - 1, c - 1, d - 1});\n }\n return N + 1 or M + 1 or K;\n}\n \nint main()\n{\n init_factorial();\n while (input())\n cout << solve() << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 410, "memory_kb": 4316, "score_of_the_acc": -0.4961, "final_rank": 7 }, { "submission_id": "aoj_2214_3113026", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nconst int mod = 1e9+7;\ninline int add(int a, int b) { return (a+b)%mod;}\ninline int sub(int a, int b) { return (a-b+mod)%mod;}\ninline int mul(int a, int b) { return 1LL*a*b%mod;}\n\nstruct warp\n{\n int sx, sy, tx, ty;\n bool operator<(const warp &w) const {\n if(sx != w.sx) return sx < w.sx;\n return sy < w.sy;\n }\n};\n\nint extgcd(int a, int b, int &x, int &y)\n{\n int g = a; x = 1; y = 0;\n if(b != 0) g = extgcd(b,a%b,y,x), y -= a/b*x;\n return g;\n}\nint inv(int a) {\n int x, y; extgcd(a,mod,x,y);\n return (mod+x%mod)%mod;\n}\nint fact[200010];\ninline int nck(int n, int k) { return mul(mul(fact[n], inv(fact[n-k])), inv(fact[k]));}\n\n\nint n, m, k, dp[100010];\n// dp[i] := i番目のワープホールの入り口までの場合の数\nvector<warp> warps;\n\ninline int calc(int sx, int sy, int tx, int ty)\n{\n if(tx < sx or ty < sy) return 0;\n return nck(tx-sx+ty-sy, tx-sx);\n}\n\nint solve()\n{\n memset(dp,0,sizeof(dp));\n sort(begin(warps),end(warps));\n warps.push_back((warp){n,m, n+1, m+1});\n for (int i = 0; i <= k; i++) {\n dp[i] = nck(warps[i].sx+warps[i].sy, warps[i].sy);\n for (int j = 0; j < i; j++) {\n dp[i] = add(dp[i], mul(dp[j], sub(calc(warps[j].tx, warps[j].ty, warps[i].sx,warps[i].sy),\n calc(warps[j].sx, warps[j].sy, warps[i].sx,warps[i].sy))));\n }\n }\n return dp[k];\n}\n\nbool input()\n{\n warps.clear();\n cin >> n >> m >> k;\n n--; m--;\n int a, b, c, d;\n for (int i = 0; i < k; i++) {\n cin >> a >> b >> c >> d;\n warps.push_back((warp){a-1,b-1,c-1,d-1});\n }\n return n+1 or m+1 or k;\n}\n\nint main()\n{\n fact[0] = 1;\n for (int i = 0; i < 200000; i++) fact[i+1] = mul(fact[i], i+1);\n while(input()) cout << solve() << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 410, "memory_kb": 4256, "score_of_the_acc": -0.4895, "final_rank": 6 }, { "submission_id": "aoj_2214_2519720", "code_snippet": "#include <iostream>\n#include <bits/stdc++.h>\n \nusing namespace std;\n \n#define MOD 1000000007\n \ninline int add(int a, int b)\n{ return (a + b) % MOD; }\n \ninline int sub(int a, int b)\n{ return (a - b + MOD) % MOD; }\n \ninline int mul(int a, int b)\n{ return 1LL * a * b % MOD; }\n \n// ????´?\nstruct warp\n{\n int x1, y1, x2, y2;\n \n bool operator<(const warp &w) const\n {\n if (x1 != w.x1)\n return x1 < w.x1;\n return y1 < w.y1;\n }\n};\n \n// return d = gcd(a, b) = ax + by\nint extgcd(int a, int b, int &x, int &y)\n{\n int g = a;\n x = 1;\n y = 0;\n if (b != 0)\n g = extgcd(b, a % b, y, x), y -= a / b * x;\n return g;\n}\n \n// ?±?a?????????\nint inv(int a)\n{\n int x, y;\n extgcd(a, MOD, x, y);\n return (MOD + x % MOD) % MOD;\n}\n \nint fact[200010]; // ??¶???n!\nvoid init_factorial()\n{\n fact[0] = 1;\n for (int i = 0; i < 200000; ++i)\n fact[i + 1] = mul(fact[i], i + 1);\n}\n \n// ????????°??????n????????????????????????k??????\ninline int nck(int n, int k)\n{ return mul(mul(fact[n], inv(fact[n - k])), inv(fact[k])); }\n \n \nint N, M, K, dp[100010];\n// dp[i] := ??°?¬¬i???????´???\\??£??¶???????????°\nvector<warp> warps;\n \ninline int calc(int x1, int y1, int x2, int y2) // ???????´??¨???????s??°t???????????°\n{\n if (x2 < x1 or y2 < y1)\n return 0;\n return nck(x2 - x1 + y2 - y1, x2 - x1);\n}\n \nint solve()\n{\n memset(dp, 0, sizeof(dp));\n sort(begin(warps), end(warps));\n warps.push_back((warp) {N, M, N + 1, M + 1});\n for (int i = 0; i <= K; ++i)\n {\n dp[i] = nck(warps[i].x1 + warps[i].y1, warps[i].y1);\n for (int j = 0; j < i; ++j)\n {\n dp[i] = add(dp[i], mul(dp[j], sub(calc(warps[j].x2, warps[j].y2, warps[i].x1, warps[i].y1), // j???????????°i?????????????´???????\n calc(warps[j].x1, warps[j].y1, warps[i].x1, warps[i].y1)))); // j???????????°i?????????\n }\n }\n return dp[K];\n}\n \nbool input()\n{\n warps.clear();\n scanf(\"%d%d%d\", &N, &M, &K);\n --N;\n --M;\n int a, b, c, d;\n for (int i = 0; i < K; ++i)\n {\n scanf(\"%d%d%d%d\", &a, &b, &c, &d);\n warps.push_back((warp) {a - 1, b - 1, c - 1, d - 1});\n }\n return N + 1 or M + 1 or K;\n}\n \nint main()\n{\n init_factorial();\n while (input())\n cout << solve() << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 420, "memory_kb": 4440, "score_of_the_acc": -0.5215, "final_rank": 8 }, { "submission_id": "aoj_2214_2272727", "code_snippet": "/*\n * AOJ 2214: Warp Hall\n * ?¢??????????(1,1)?????°??????(m,n)???????¬??????????????????°?????????????????°????´???????????????´??\\??°???????????????????´????????????????????°??§??????????\n * ?±????????????°DP\n * ???????????????????´?i(ai,bi)??°(ci,di)???(1,1)??°(ai,bi)???????????°???di??????????????\\????´???????????????¢????????????????????????????????£???L??????????????????di????°?????´??????????DP?±???°?????°??????????´???\\??£???????????°???\n */\n\n\n#include <cstdio>\n#include <cstring>\n#include <algorithm>\n\nusing namespace std;\n\ntypedef long long LL;\n\nconst LL MOD = 1000000007ll;\n\ninline LL add(LL a, LL b) { return (a + b) % MOD; }\n\ninline LL sub(LL a, LL b) { return (a - b + MOD) % MOD; }\n\ninline LL mul(LL a, LL b) { return a * b % MOD; }\n\nLL f[200010];\n\nLL Inverse(LL a, LL p = MOD) {\n if (a == 1) return 1;\n return sub(0, mul(p / a, Inverse(p % a, p)));\n}\n\nLL C(LL a, LL b) {\n return mul(mul(f[a], Inverse(f[b])), Inverse(f[a - b]));\n}\n\nstruct Warp {\n int sx, sy, tx, ty;\n\n bool operator<(const Warp &w) const {\n if (sx != w.sx) return sx < w.sx;\n return sy < w.sy;\n }\n} w[1010];\n\nLL d[1010];\nint m, n, k;\n\nLL Gao(LL sx, LL sy, LL tx, LL ty) {\n if (sx > tx || sy > ty) return 0;\n// printf(\"%lld %lld %lld %lld\\n\", sx, sy, tx, ty);\n return C(tx - sx + ty - sy, tx - sx);\n}\n\nint main() {\n f[0] = 1;\n for (LL i = 1; i <= 200000; ++i) {\n f[i] = mul(f[i - 1], i);\n }\n while (scanf(\"%d%d%d\", &m, &n, &k) != EOF && m + n + k > 0) {\n for (int i = 0; i < k; ++i) {\n scanf(\"%d%d%d%d\", &w[i].sx, &w[i].sy, &w[i].tx, &w[i].ty);\n --w[i].sx;\n --w[i].sy;\n --w[i].tx;\n --w[i].ty;\n }\n memset(d, 0, sizeof(d));\n sort(w, w + k);\n w[k].sx = m - 1;\n w[k].sy = n - 1;\n for (int i = 0; i <= k; ++i) {\n d[i] = C(w[i].sx + w[i].sy, w[i].sx);\n// printf(\"%d %lld\\n\", i, d[i]);\n for (int j = 0; j < i; ++j) {\n// printf(\"--%d %d\\n\", i, j);\n d[i] = add(d[i],\n mul(d[j],\n sub(Gao(w[j].tx, w[j].ty, w[i].sx, w[i].sy),\n Gao(w[j].sx, w[j].sy, w[i].sx, w[i].sy))));\n// printf(\"--%d %d\\n\", i, j);\n }\n// printf(\"%d %lld\\n\", i, d[i]);\n }\n printf(\"%lld\\n\", d[k]);\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 870, "memory_kb": 4344, "score_of_the_acc": -1.0341, "final_rank": 12 }, { "submission_id": "aoj_2214_2047740", "code_snippet": "#include <cmath>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <iostream>\n#include <algorithm>\n\nusing namespace std;\n\ntypedef long long LL;\n\nconst LL P = 1000000007LL;\n\nint exgcd(int a, int b, int &x, int &y)\n{\n\tif (b == 0)\n\t{\n\t\tx = 1;\n\t\ty = 0;\n\t\treturn a;\n\t}\n\telse\n\t{\n\t\tint t = exgcd(b, a % b, y, x);\n\t\ty -= a / b * x;\n\t\treturn t;\n\t}\n}\n\nint inv(int a)\n{\n\tint x, y;\n\texgcd(a, P, x, y);\n\treturn (x % P + P) % P;\n}\n\nLL fac[200010];\n\nvoid factorial(void)\n{\n\tfac[0] = 1;\n\tfor (int i = 1; i <= 200000; ++i)\n\t{\n\t\tfac[i] = fac[i - 1] * i;\n\n\t\tif (fac[i] >= P)\n\t\t\tfac[i] %= P;\n\t}\n}\n\nclass warp\n{\npublic:\n\tint sx, sy;\n\tint ex, ey;\n\n\twarp(void) {};\n\twarp(int _sx, int _sy, int _ex, int _ey) :\n\t\tsx(_sx), sy(_sy), ex(_ex), ey(_ey) {};\n\n\tfriend bool operator < (const warp &a, const warp &b)\n\t{\n\t\tif (a.sx != b.sx)\n\t\t\treturn a.sx < b.sx;\n\t\telse\n\t\t\treturn a.sy < b.sy;\n\t}\n};\n\nint n, m, k;\n\nLL dp[100005];\n\nwarp warps[100005];\n\nLL calc(int a, int b)\n{\n\tif (a < 0 || b < 0)return 0LL;\n\tif (a == 0 || b == 0)return 1LL;\n\treturn fac[a + b] * inv(fac[a]) % P * inv(fac[b]) % P;\n}\n\nsigned main(void)\n{\n\tfactorial();\n\n\twhile (cin >> n >> m >> k, n || m || k)\n\t{\n\t\tfor (int i = 1; i <= k; ++i)\n\t\t{\n\t\t\tint sx, sy, ex, ey;\n\t\t\tcin >> sx >> sy >> ex >> ey;\n\t\t\twarps[i] = warp(sx, sy, ex, ey);\n\t\t}\n\n\t\tmemset(dp, 0, sizeof(dp));\n\n\t\tsort(warps + 1, warps + 1 + k);\n\n\t\twarps[++k] = warp(n, m, n, m);\n\n\t\tfor (int i = 1; i <= k; ++i)\n\t\t{\n\t\t\tdp[i] = calc(warps[i].sx - 1, warps[i].sy - 1);\n\n\t\t\tfor (int j = 1; j < i; ++j)\n\t\t\t{\n\t\t\t\tdp[i] += (calc(warps[i].sx - warps[j].ex, warps[i].sy - warps[j].ey) * dp[j]) % P;\n\t\t\t\tdp[i] -= (calc(warps[i].sx - warps[j].sx, warps[i].sy - warps[j].sy) * dp[j]) % P;\n\t\t\t\tdp[i] = (dp[i] % P + P) % P;\n\t\t\t}\n\t\t}\n\n\t\tcout << dp[k] << endl;\n\t}\n\n//\tsystem(\"pause\");\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 5396, "score_of_the_acc": -0.2321, "final_rank": 2 }, { "submission_id": "aoj_2214_1438022", "code_snippet": "#include<cstdio>\n#include<cstdlib>\n#include<cstring>\n#include<cmath>\n#include<iostream>\n#include<string>\n#include<vector>\n#include<map>\n#include<set>\n#include<list>\n#include<queue>\n#include<deque>\n#include<algorithm>\n#include<numeric>\n#include<utility>\n#include<complex>\n#include<functional>\n \nusing namespace std;\n\n/* constant */\n\nconst int MAX_N = 100000;\nconst int MAX_M = 100000;\nconst int MAX_K = 1000;\n\nconst int MAX_F = MAX_N + MAX_M;\nconst long long MOD = 1000000007;\n\n/* typedef */\n\ntypedef long long ll;\n\nstruct WH {\n int x0, y0, x1, y1;\n WH() {}\n WH(int _x0, int _y0, int _x1, int _y1): x0(_x0), y0(_y0), x1(_x1), y1(_y1) {}\n bool operator<(const WH& w) const {\n return x0 < w.x0 || (x0 == w.x0 && y0 < w.y0);\n }\n};\n\n/* global variables */\n\nint n, m, k;\nWH whs[MAX_K];\nll facts[MAX_F + 1], ifacts[MAX_F + 1];\nll dp[MAX_K];\n\n/* subroutines */\n\nll powmod(ll a, int b) {\t// a^b\n ll p = 1;\n while (b > 0) {\n if (b & 1) p = (p * a) % MOD;\n a = (a * a) % MOD;\n b >>= 1;\n }\n return p;\n}\n\nll comb(ll a, ll b) {\t// a_C_b (a >= b)\n ll f0 = facts[a], if0 = ifacts[b], if1 = ifacts[a - b];\n return (((f0 * if0) % MOD) * if1) % MOD;\n}\n\nll f(ll x0, ll y0, ll x1, ll y1) {\n ll dx = x1 - x0, dy = y1 - y0;\n return (dx < 0 || dy < 0) ? 0LL : comb(dx + dy, dx);\n}\n\n/* main */\n\nint main() {\n facts[0] = ifacts[0] = 1;\n for (ll i = 1; i <= MAX_F; i++) {\n facts[i] = (facts[i - 1] * i) % MOD;\n ifacts[i] = powmod(facts[i], MOD - 2);\n }\n\n for (;;) {\n cin >> n >> m >> k;\n if (n == 0) break;\n\n for (int i = 0; i < k; i++)\n cin >> whs[i].x0 >> whs[i].y0 >> whs[i].x1 >> whs[i].y1;\n sort(whs, whs + k);\n\n for (int i = 0; i < k; i++) {\n dp[i] = f(1, 1, whs[i].x0, whs[i].y0);\n\n for (int j = 0; j < i; j++) {\n\tll df =\n\t (f(whs[j].x1, whs[j].y1, whs[i].x0, whs[i].y0) -\n\t f(whs[j].x0, whs[j].y0, whs[i].x0, whs[i].y0) + MOD) % MOD;\n\tdp[i] = (dp[i] + (dp[j] * df) % MOD) % MOD;\n }\n }\n\n ll count = f(1, 1, n, m);\n\n for (int i = 0; i < k; i++) {\n ll df =\n\t(f(whs[i].x1, whs[i].y1, n, m) -\n\t f(whs[i].x0, whs[i].y0, n, m) + MOD) % MOD;\n count = (count + (dp[i] * df) % MOD) % MOD;\n }\n\n cout << count << endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 4308, "score_of_the_acc": -0.0999, "final_rank": 1 }, { "submission_id": "aoj_2214_1272191", "code_snippet": "#include<stdio.h>\n#include<algorithm>\nusing namespace std;\nint mod=1000000007;\nint X1[1100];\nint Y1[1100];\nint X2[1100];\nint Y2[1100];\npair<pair<int,int>,pair<int,int> > p[1100];\nlong long fact[210000];\nlong long inv[210000];\nlong long factinv[210000];\nlong long dp[1100];\nlong long getinv(long long a){\n\tint b=mod-2;\n\tlong long ret=1;\n\tlong long now=a;\n\twhile(b){\n\t\tif(b%2)ret=ret*now%mod;\n\t\tb/=2;\n\t\tnow=now*now%mod;\n\t}\n\t//printf(\"%lld: %lld\\n\",a,ret);\n\treturn ret;\n}\nlong long C(long long n,long long k){\n\tif(n<0||k<0)return 0;\n\tn+=k;\n\treturn fact[n]*factinv[k]%mod*factinv[n-k]%mod;\n}\nint main(){\n\tint a,b,c;\n\tfact[0]=1;\n\tfor(int i=1;i<210000;i++)fact[i]=fact[i-1]*i%mod;\n\tinv[1]=1;\n\tfor(int i=2;i<210000;i++)inv[i]=(mod-(mod/i)*inv[mod%i]%mod)%mod;\n\tfactinv[0]=1;\n\tfor(int i=1;i<210000;i++)factinv[i]=factinv[i-1]*inv[i]%mod;\n\twhile(scanf(\"%d%d%d\",&a,&b,&c),a){\n\t\tfor(int i=1;i<=c;i++){\n\t\t\tscanf(\"%d%d%d%d\",X1+i,Y1+i,X2+i,Y2+i);\n\t\t\tX1[i]--;X2[i]--;Y1[i]--;Y2[i]--;\n\t\t\tp[i]=make_pair(make_pair(X1[i],Y1[i]),make_pair(X2[i],Y2[i]));\n\t\t}\n\t\tstd::sort(p+1,p+c+1);\n\t\ta--;b--;\n\t\tfor(int i=0;i<c+2;i++)dp[i]=0;\n\t\tfor(int i=1;i<=c;i++){\n\t\t\tint row=p[i].first.first;\n\t\t\tint col=p[i].first.second;\n\t\t\tdp[i]=C(row,col);\n\t\t\tfor(int j=1;j<i;j++){\n\t\t\t\t//if(row<p[j].second.first||col<p[j].second.second)continue;\n\t\t\t\tdp[i]=(dp[i]+dp[j]*(mod+C(row-p[j].second.first,col-p[j].second.second)-C(row-p[j].first.first,col-p[j].first.second)))%mod;\n\t\t\t}\n\t\t}\n\t\tlong long ret=C(a,b);\n\t\tfor(int i=1;i<=c;i++)ret=(ret+dp[i]*(mod+C(a-p[i].second.first,b-p[i].second.second)-C(a-p[i].first.first,b-p[i].first.second)))%mod;\n\t\tprintf(\"%lld\\n\",ret);\n\t}\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 5996, "score_of_the_acc": -0.2986, "final_rank": 4 }, { "submission_id": "aoj_2214_467608", "code_snippet": "#include <cstdio>\n#include <algorithm>\n#include <cstring>\nusing namespace std;\n\ntypedef long long int64;\nconst int64 MOD = (int64)( 1e9 + 7 );\n\nconst int MAX_N = int(1e5);\nconst int MAX_K = int(1e3);\n\nstruct WarpHole {\n\tint sy, sx, gy, gx;\n\n\tWarpHole(){}\n\tWarpHole(int sy, int sx, int gy, int gx):\n\t\tsy(sy), sx(sx), gy(gy), gx(gx)\n\t{}\n};\n\nbool operator< (const WarpHole& lhs, const WarpHole& rhs) {\n\treturn make_pair(lhs.sy, lhs.sx) != make_pair(rhs.sy, rhs.sx) ?\n\t\t\tmake_pair(lhs.sy, lhs.sx) < make_pair(rhs.sy, rhs.sx) :\n\t\t\tmake_pair(lhs.gy, lhs.gx) != make_pair(rhs.gy, rhs.gx);\n}\n\nint N, M, K;\nint64 fact[2*MAX_N + 1], invFact[2*MAX_N + 1];\nWarpHole warps[MAX_K + 1];\nbool visited[MAX_K + 1][MAX_K + 1];\nint64 memo[MAX_K + 1][MAX_K + 1];\n\nint64 calcPow(int64 x, int64 n, int64 mod = MOD) {\n\tint64 ret = 1;\n\tfor (; n; n >>= 1, x = x * x % mod) if (n&1) {\n\t\tret = ret * x % mod;\n\t}\n\treturn ret;\n}\n\n\nint64 getComb(int n, int k) {\n\treturn fact[n] * invFact[n-k] % MOD * invFact[k] % MOD;\n}\n\nint64 getWay(int y, int x) {\n\tif (y < 0 || x < 0) {\n\t\treturn 0;\n\t}\n\treturn getComb(y + x, x);\n}\n\nvoid prepare() {\n\tfact[0] = invFact[0] = 1;\n\tfor (int i = 0; i < 2*MAX_N; ++i) {\n\t\tfact[i+1] = (i + 1) * fact[i] % MOD;\n\t\tinvFact[i+1] = calcPow(fact[i+1], MOD - 2, MOD);\n\t}\n}\n\nbool init() {\n\tscanf(\"%d%d%d\", &N, &M, &K);\n\tfor (int i = 0; i < K; ++i) {\n\t\tint sy, sx, gy, gx;\n\t\tscanf(\"%d%d%d%d\", &sy, &sx, &gy, &gx);\n\t\twarps[i] = WarpHole(sy,sx,gy,gx);\n\t}\n\n\tmemset(visited, false, sizeof(visited));\n\treturn N > 0;\n}\n\n//[0,k)までのワープホールが機能しているときの、\n//(1,1)からt番目のワープホールの入り口までの経路の個数を返す\nint64 rec(const int k, const int t) {\n\t//memorize\n\tif (visited[k][t]) {\n\t\treturn memo[k][t];\n\t}\n\tvisited[k][t] = true;\n\n\tint64& ret = memo[k][t];\n\n\t//base\n\tif ( k == 0 ) {\n\t\treturn ret = getWay(warps[t].sy - 1, warps[t].sx - 1);\n\t}\n\n\t//calc\n\tconst int64 inOut = getWay(warps[t].sy - warps[k-1].sy, warps[t].sx - warps[k-1].sx),\n\t\t\toutOut = getWay(warps[t].sy - warps[k-1].gy, warps[t].sx - warps[k-1].gx);\n\tret = (rec(k-1, t) - rec(k-1,k-1) * (inOut - outOut) % MOD + MOD) % MOD;\n\n\treturn ret;\n}\n\nint solve() {\n\t\n\tfor (int i = 0; i < K - 1; ++i) {\n\t\tswap(warps[i], *min_element(warps + i, warps + K));\n\t}\n\twarps[K] = WarpHole(N, M, N, M);\n\n\treturn int(rec(K, K));\n}\n\nint main() {\n\tprepare();\n\tfor (;init();) {\n\t\tprintf(\"%d\\n\", solve());\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 13060, "score_of_the_acc": -1.0698, "final_rank": 13 } ]
aoj_2217_cpp
Problem B: Let's JUMPSTYLE Description 踊りの好きなFは,とあるダンスホールでJUMPSTYLEと呼ばれるハードコアな踊りの練習をすることにした. フロアはN×Nの格子状にタイルが敷き詰められている. 踊りの練習をサポートするため,各タイルには次のステップに飛び移るべきタイルの座標が書かれている.Fは日々の練習によって強靭な運動神経を持っており,フロア内の任意のタイルへ飛び移ることができる. Fは,十分に長い時間この踊りを続けていけば,いつか定常状態に入り,同じルートをただループする状態になることに気付いた.Fはこのようなループがこのフロアにいくつ存在するのか疑問に思い,プログラマであるあなたに相談することにした. Input 入力は複数のテストケースから成る. 各テストケースはフロアの1辺の長さNを含む1行で始まる.(1 ≦ N ≦ 100) 続くN行は,フロアの各タイルに書いてあるジャンプ先の座標を以下のように表している. は,それぞれ座標(i, j)にあるタイルに書いてあるジャンプ先のx座標およびy座標を表す.座標は0-originであり,すべての座標の値は0以上N未満の整数である. 入力は0のみから成る行で終わる. Output 各テストケースに対し,異なるループの個数を1行で出力する. Sample Input 1 0 0 2 1 1 0 1 1 0 0 0 2 1 1 0 1 1 1 1 0 3 0 1 2 2 2 1 0 2 1 2 2 1 0 0 0 1 1 1 4 3 2 2 0 3 2 2 1 1 1 0 3 1 1 3 1 0 3 2 3 3 0 2 3 1 1 1 1 3 2 1 3 0 Output for Sample Input 1 2 1 2 3
[ { "submission_id": "aoj_2217_3778511", "code_snippet": "#include <iostream>\n\nusing namespace std;\n\nint w[101][101],h[101][101];\nbool b[101][101];\n\nint dfs(int x,int y)\n{\n \n if(x == -1 || y == -1) return 0;\n if(b[x][y]){\n w[x][y] = -1;\n h[x][y] = -1;\n return 1;\n }\n \n b[x][y] = true;\n \n int rec;\n rec = dfs(w[x][y],h[x][y]);\n b[x][y] = false;\n return rec;\n}\n\nint main()\n{\n int n,sum;\n \n while(cin >> n,n!=0){\n for(int i=0;i<n;i++){\n for(int j=0;j<n;j++){\n cin >> w[j][i] >> h[j][i];\n }\n }\n \n int sum = 0;\n for(int i=0;i<n;i++){\n for(int j=0;j<n;j++){\n sum += dfs(i,j);\n }\n }\n \n cout << sum << endl;\n }\n \n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3188, "score_of_the_acc": -0.1111, "final_rank": 13 }, { "submission_id": "aoj_2217_2954816", "code_snippet": "#include <iostream>\n#include <cstring>\n#include <stack>\n#include <set>\n#include <stdio.h>\n#include <string>\n#include <vector>\n#include <algorithm>\n#include <limits.h>\nusing namespace std;\ntypedef vector<int> vi;\ntypedef vector<pair<int,int> > vp;\nvector<pair<double,int> > member;\nconst double eps=1e-9;\ntypedef pair<int,int> p;\nbool loop[105][105];\nint board[105][205];\nvector<p> a;\nint main()\n{\n int n;\n while(1)\n {\n int ans=0;\n cin >> n;\n if(!n) break;\n memset(loop,false,sizeof(loop));\n for(int i=0;i<n;i++)\n\t{\n\t for(int j=0;j<n;j++)\n\t {\n\t int x,y;\n\t cin >> x >> y;\n\t board[i][2*j]=x;\n\t board[i][2*j+1]=y;\n\t }\n\t}\n for(int i=0;i<n;i++)\n\t{\n\t for(int j=0;j<n;j++)\n\t {\n\t set<p> st;\n\t st.clear();\n\t int nowx=j;\n\t int nowy=i;\n\t if(loop[i][j]) continue;\n\t vector<p> tmp;\n\t while(1)\n\t\t{\n\t\t //cout << \" nowy nowx (\" << nowy << ' ' << nowx << \" )\" << endl;\n\t\t auto it=st.find(p(nowx,nowy));\n\t\t if(it!=st.end())\n\t\t {\n\t\t bool forg=false;\n\t\t for(int k=0;k<tmp.size();k++)\n\t\t\t{\n\t\t\t if(!loop[tmp[k].second][tmp[k].first]) loop[tmp[k].second][tmp[k].first]=true;\n\t\t\t else \n\t\t\t {\n\t\t\t forg=true;\n\t\t\t }\n\t\t\t}\n\t\t if(!forg) ans++;\n\t\t break;\n\t\t }\n\t\t else\n\t\t {\n\t\t // cout << nowy << ' ' << nowx << endl;\n\t\t st.insert(p(nowx,nowy));\n\t\t tmp.push_back(p(nowx,nowy));\n\t\t int newnowx=board[nowy][nowx*2];\n\t\t int newnowy=board[nowy][(nowx*2)+1];\n\t\t nowx=newnowx;\n\t\t nowy=newnowy;\n\t\t }\n\t\t}\n\t }\n\t}\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3232, "score_of_the_acc": -0.1135, "final_rank": 14 }, { "submission_id": "aoj_2217_2954634", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nbool dfs(int si, int sj, int i, int j, vector<vector<pair<int, int> > > a, vector<vector<bool> >& used, vector<vector<bool> > & used2){\n\n \n if(i == si && j == sj) return true;\n\n int nexti = a[i][j].first;\n int nextj = a[i][j].second;\n\n if(nexti == si && nextj == sj) {\n used[nexti][nextj] = true;\n return true;\n }\n\n used2[i][j] = true;\n if(used[nexti][nextj]) return false;\n if(used2[nexti][nextj]) return false;\n\n bool flag = dfs(si, sj, nexti, nextj, a, used, used2);\n\n if(flag){\n used[i][j] = true;\n }\n\n return flag;\n}\n\nint main(){\n \n while(1){\n int n; cin >> n;\n if(!n) break;\n vector<vector<pair<int, int> > > a(n, vector<pair<int, int> > (n));\n\n for(int i = 0; i < n; i++){\n for(int j = 0; j < n; j++){\n int x, y; cin >> x >> y;\n a[i][j].first = y;\n a[i][j].second = x;\n }\n }\n\n vector<vector<bool> > used(n, vector<bool> (n, false));\n\n int ans = 0;\n for(int i = 0; i < n; i++){\n for(int j = 0; j < n; j++){\n if(!used[i][j]){\n int nexti = a[i][j].first;\n int nextj = a[i][j].second;\n if(used[nexti][nextj]) continue;\n vector<vector<bool> > used2(n, vector<bool> (n, false));\n used2[i][j] = true;\n if(dfs(i, j, nexti, nextj, a, used, used2)){\n ans++;\n //cout << i << \" \" << j << endl;\n }\n }\n }\n }\n cout << ans << endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 620, "memory_kb": 19380, "score_of_the_acc": -2, "final_rank": 20 }, { "submission_id": "aoj_2217_2485986", "code_snippet": "#include <vector>\n#include <iostream>\n#include <utility>\n#include <algorithm>\n#include <string>\n#include <deque>\n#include <queue>\n#include <tuple>\n#include <queue>\n#include <functional>\n#include <cmath>\n#include <iomanip>\n#include <map>\n#include <set>\n#include <numeric>\n#include <unordered_map>\n#include <unordered_set>\n#include <complex>\n#include <iterator>\n#include <array>\n#include <memory>\n#include <stack>\n#define vi vector<int>\n#define vvi vector<vector<int> >\n#define ll long long int\n#define vl vector<ll>\n#define vvl vector<vector<ll>>\n#define vb vector<bool>\n#define vc vector<char>\n#define vs vector<string>\n#define ld long double\n#define INF 1e9\n#define EPS 0.0000000001\n#define rep(i,n) for(int i=0;i<n;i++)\n#define loop(i,s,n) for(int i=s;i<n;i++)\n#define all(in) in.begin(), in.end()\ntemplate<class T, class S> void cmin(T &a, const S &b) { if (a > b)a = b; }\ntemplate<class T, class S> void cmax(T &a, const S &b) { if (a < b)a = b; }\n#define MAX 9999999\nusing namespace std;\ntypedef pair<int, int> pii;\ntypedef pair<double,double>pdd;\ntypedef pair<ll,ll>pll;\n\nclass unionfind {\n vector<int> par, rank, size_;\npublic:\n unionfind(int n) :par(n), rank(n), size_(n, 1) {\n iota(all(par), 0);\n }\n int find(int x) {\n if (par[x] == x)return x;\n return par[x] = find(par[x]);\n }\n void unite(int x, int y) {\n x = find(x), y = find(y);\n if (x == y)return;\n if (rank[x] < rank[y])swap(x, y);\n par[y] = x;\n size_[x] += size_[y];\n if (rank[x] == rank[y])rank[x]++;\n }\n bool same(int x, int y) {\n return find(x) == find(y);\n }\n int size(int x) {\n return size_[find(x)];\n }\n};\n\nint main(){\n int n;\n while(cin>>n,n){\n int ans=0;\n vector<vector<pii>>graph(n,vector<pii>(n,pii(0,0)));\n rep(i,n)rep(j,n)cin>>graph[i][j].second>>graph[i][j].first;\n vector<vector<bool>>flag(n,vector<bool>(n,true));\n rep(i,n)rep(j,n){\n if(!flag[i][j])continue;\n vector<vector<bool>>temp=flag;\n map<pii,int>mp;\n bool IsHeyro=false;\n pii now=pii(i,j);\n while(1){\n if(mp[now]!=0){ans++; IsHeyro=true;;break;}\n if(flag[now.first][now.second]==false)break;\n mp[now]++;\n now=graph[now.first][now.second];\n }\n if(IsHeyro)for(auto itr=mp.begin(); itr!=mp.end();itr++)flag[itr->first.first][itr->first.second]=false;\n \n }\n cout<<ans<<endl;\n }\n}", "accuracy": 1, "time_ms": 90, "memory_kb": 3180, "score_of_the_acc": -0.2418, "final_rank": 16 }, { "submission_id": "aoj_2217_2479572", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nint main(){\n int n;\n while(cin >> n , n){\n int px[n][n] , py[n][n];\n int x , y;\n for(int i = 0; i < n; i++)\n for(int j = 0; j < n; j++)\n cin >> px[i][j] >> py[i][j];\n int visited[n][n] = {};\n int ans = 0;\n for(int i = 0; i < n; i++)\n for(int j = 0; j < n; j++){\n stack <pair <int , int> > st;\n if(visited[i][j] == 2)continue;\n visited[i][j] = 1;\n st.push(make_pair(i,j));\n int y = py[i][j] , x = px[i][j];\n while(1){\n if(visited[y][x] != 0){\n if(visited[y][x] == 1){\n ans++;\n visited[y][x] = 2;\n }\n while(!st.empty()){\n if(visited[st.top().first][st.top().second] != 2)\n visited[st.top().first][st.top().second] = 0;\n st.pop();\n }\n break;\n }\n else{\n visited[y][x] = 1;\n st.push(make_pair(y,x));\n }\n int kari = y;\n y = py[y][x];\n x = px[kari][x];\n kari = y;\n }\n }\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3232, "score_of_the_acc": -0.1135, "final_rank": 14 }, { "submission_id": "aoj_2217_2402326", "code_snippet": "#include<iostream>\n#include<map>\n#include<vector>\nusing namespace std;\ntypedef pair<int,int> mypair;\nvoid jump(int x,int y,int acc,int n,vector<vector<bool> >&map,vector<vector<mypair> > &map2){\n\tif(map[x][y]){\nint sx=map2[x][y].first;\nint sy=map2[x][y].second;\nif(acc==n*n+1)map[x][y]=false;\nacc++;\njump(sx,sy,acc,n,map,map2);\n}\n}\n\nint main(void){\nint n;\nwhile(1){\ncin>>n;\nif(n==0)break;\nvector<vector<bool> >map(n,vector<bool>(n,true));\nvector<vector<mypair> >map2(n,vector<mypair>(n));\nfor(int i=0;i<n;i++){\n\tfor(int j=0;j<n;j++){\n\t\tcin>>map2[j][i].first>>map2[j][i].second;\n\t}\n}int t=0;\nfor(int i=0;i<n;i++){\n\tfor(int j=0;j<n;j++){\njump(i,j,0,n,map,map2);\n\t}\n}\nfor(int i=0;i<n;i++){\n\tfor(int j=0;j<n;j++){\nif(map[i][j]==false)t++;\n\t}\n}\ncout<<t<<endl;\n}\nreturn 0;\n}", "accuracy": 1, "time_ms": 350, "memory_kb": 3084, "score_of_the_acc": -0.6628, "final_rank": 19 }, { "submission_id": "aoj_2217_2318316", "code_snippet": "#include<bits/stdc++.h>\n#define x second\n#define y first\nusing namespace std;\npair<int,int>a[101][101];\nint col[101][101];\nint n;\nvoid dfs(int y1,int x1){\n col[y1][x1]=1;\n if(!col[a[y1][x1].y][a[y1][x1].x])dfs(a[y1][x1].y,a[y1][x1].x);\n for(int i=0;i<n;i++)\n for(int j=0;j<n;j++)\n if(!col[i][j]&&a[i][j].y==y1&&a[i][j].x==x1)\n dfs(i,j);\n}\nint main(){\n while(cin>>n,n){\n memset(col,0,sizeof(col));\n memset(a,0,sizeof(a));\n for(int i=0;i<n;i++)\n for(int j=0;j<n;j++)\n cin>>a[i][j].x>>a[i][j].y;\n int sum=0;\n for(int i=0;i<n;i++)\n for(int j=0;j<n;j++)\n if(!col[i][j])dfs(i,j),sum++;\n cout<<sum<<endl;\n }\n}", "accuracy": 1, "time_ms": 140, "memory_kb": 3204, "score_of_the_acc": -0.3251, "final_rank": 17 }, { "submission_id": "aoj_2217_1771773", "code_snippet": "#include<iostream>\n#include<queue>\nusing namespace std;\nint x[10000], a, b, n, c[10000], d[10000];\nint main() {\n\twhile (true) {\n\t\tfor (int i = 0; i < 10000; i++) { d[i] = 0; }\n\t\tcin >> n; if (n == 0) { break; }\n\t\tfor (int i = 0; i < n*n; i++) {\n\t\t\tcin >> a >> b;\n\t\t\tx[i] = b * n + a;\n\t\t}\n\t\tint cnt = 0;\n\t\tfor (int i = 0; i < n*n; i++) {\n\t\t\tif(d[i]==0)\n\t\t\tfor (int j = 0; j < 10000; j++) { c[j] = 0; }\n\t\t\tint cx = i;\n\t\t\twhile (true) {\n\t\t\t\tc[cx] = 1;\n\t\t\t\tcx = x[cx];\n\t\t\t\tif (c[cx] == 1) { break; }\n\t\t\t}\n\t\t\tint C = 0;\n\t\t\tfor (int j = 0; j < 10000; j++) {\n\t\t\t\tif (c[j] == 1 && d[j] >= 1) {\n\t\t\t\t\tC = d[j];\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (C == 0) {\n\t\t\t\tcnt++; C = cnt;\n\t\t\t}\n\t\t\tfor (int j = 0; j < 10000; j++) {\n\t\t\t\tif (c[j] == 1) { d[j] = C; }\n\t\t\t}\n\t\t}\n\t\tcout << cnt << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 350, "memory_kb": 1276, "score_of_the_acc": -0.5635, "final_rank": 18 }, { "submission_id": "aoj_2217_1708783", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nbool used[100][100];\nint n,p[2][100][100],sx,sy;\nbool dfs(int x,int y){\n if(sx==x&&sy==y)return 1;\n if(used[y][x])return 0;\n used[y][x]=1;\n return used[y][x]=dfs(p[0][y][x],p[1][y][x]);\n}\nint main(){\n while(cin>>n&&n){\n int c=0;\n for(int i=0;i<n;i++)\n for(int j=0;j<n;j++)\n\tcin>>p[0][i][j]>>p[1][i][j],used[i][j]=0;\n for(int i=0;i<n;i++)\n for(int j=0;j<n;j++)if(!used[i][j])sx=j,sy=i,c+=dfs(p[0][i][j],p[1][i][j]);\n cout<<c<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1248, "score_of_the_acc": -0.0046, "final_rank": 3 }, { "submission_id": "aoj_2217_1498090", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\ninline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}\ntemplate<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}\ntemplate<class T> inline T sqr(T x) {return x*x;}\n\ntypedef vector<int> vi;\ntypedef vector<vi> vvi;\ntypedef vector<string> vs;\ntypedef pair<int, int> pii;\ntypedef long long ll;\n\n#define all(a) (a).begin(),(a).end()\n#define rall(a) (a).rbegin(), (a).rend()\n#define pb push_back\n#define mp make_pair\n#define each(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)\n#define exist(s,e) ((s).find(e)!=(s).end())\n#define range(i,a,b) for(int i=(a);i<(b);++i)\n#define rep(i,n) range(i,0,n)\n#define clr(a,b) memset((a), (b) ,sizeof(a))\n#define dump(x) cerr << #x << \" = \" << (x) << endl;\n#define debug(x) cerr << #x << \" = \" << (x) << \" (L\" << __LINE__ << \")\" << \" \" << __FILE__ << endl;\n\nconst double eps = 1e-10;\nconst double pi = acos(-1.0);\nconst ll INF =1LL << 62;\nconst int inf =1 << 29;\n\nint n;\nint x[110][110],y[110][110];\nint visited[110][110];\n\nint main(void){\n\twhile(cin >> n,n){\n\t\tint ans=0;\n\t\trep(i,n)rep(j,n) visited[i][j]=0;\n\t\trep(i,n)rep(j,n) cin >> x[i][j] >> y[i][j];\n\t\trep(yy,n)rep(xx,n){\n\t\t\tif(visited[yy][xx]) continue;\n\t\t\tvisited[yy][xx]=-1;\n\t\t\tint cx=x[yy][xx],cy=y[yy][xx];\n\t\t\twhile(visited[cy][cx]==0){\n\t\t\t\tvisited[cy][cx]=-1;\n\t\t\t\tint nx=x[cy][cx],ny=y[cy][cx];\n\t\t\t\tswap(cx,nx),swap(cy,ny);\n\t\t\t}\n\n\t\t\tint cur=visited[cy][cx];\n\t\t\tif(cur==-1) cur=++ans;\n\t\t\tcx=xx,cy=yy;\n\t\t\twhile(visited[cy][cx]==-1){\n\t\t\t\tvisited[cy][cx]=cur;\n\t\t\t\tint nx=x[cy][cx],ny=y[cy][cx];\n\t\t\t\tswap(cx,nx),swap(cy,ny);\n\t\t\t}\n\t\t}\n\t\tcout << ans << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1296, "score_of_the_acc": -0.0072, "final_rank": 7 }, { "submission_id": "aoj_2217_1438140", "code_snippet": "#include<cstdio>\n#include<cstdlib>\n#include<cstring>\n#include<cmath>\n#include<iostream>\n#include<string>\n#include<vector>\n#include<map>\n#include<set>\n#include<list>\n#include<queue>\n#include<deque>\n#include<algorithm>\n#include<numeric>\n#include<utility>\n#include<complex>\n#include<functional>\n \nusing namespace std;\n\n/* constant */\n\nconst int MAX_N = 100;\nconst int MAX_NN = MAX_N * MAX_N;\n\n/* typedef */\n\ntypedef vector<int> vi;\n\n/* global variables */\n\nint n, nn;\nint flds[MAX_NN], ids[MAX_NN];\n\n/* subroutines */\n\n/* main */\n\nint main() {\n for (;;) {\n cin >> n;\n if (n == 0) break;\n nn = n * n;\n\n for (int pos = 0; pos < nn; pos++) {\n int x, y;\n cin >> x >> y;\n flds[pos] = y * n + x;\n }\n\n memset(ids, 0, sizeof(ids));\n\n int id = 0;\n vi fs;\n\n for (int pos0 = 0; pos0 < nn; pos0++) {\n if (ids[pos0] > 0) continue;\n\n ids[pos0] = ++id;\n fs.clear();\n fs.push_back(pos0);\n \n int pos = flds[pos0];\n while (! ids[pos]) {\n\tids[pos] = id;\n\tfs.push_back(pos);\n\tpos = flds[pos];\n }\n\n int id0 = ids[pos];\n if (id0 != id) {\n\tfor (vi::iterator vit = fs.begin(); vit != fs.end(); vit++)\n\t ids[*vit] = id0;\n\t--id;\n }\n }\n\n cout << id << endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1300, "score_of_the_acc": -0.0075, "final_rank": 8 }, { "submission_id": "aoj_2217_1433348", "code_snippet": "#include \"bits/stdc++.h\"\n\nusing namespace std;\n\ntypedef vector<int> vi;\ntypedef pair<int,int> pii;\ntypedef long long ll;\n\n#define rep(i,n) for(ll i=0;i<(ll)(n);i++)\n#define all(a) (a).begin(),(a).end()\n#define pb push_back\n\nint par[20000];\nint Rank[20000];\n\nvoid init(int n){\n rep(i,n){\n par[i]=i;\n Rank[i]=0;\n }\n}\n\nint find(int x){\n if(par[x]==x) {\n return x;\n }else{\n return par[x]=find(par[x]);\n }\n}\n\nvoid unite(int x,int y){\n x=find(x);\n y=find(y);\n if(x==y) return;\n \n if(Rank[x]<Rank[y]){\n par[x]=y;\n }else{\n par[y]=x;\n if(Rank[x]==Rank[y])Rank[x]++;\n }\n}\n\n\n\nint main(){\n\tint n;\n\twhile(cin>>n){\n\t\tif(n==0)break;\n\n\t\tpii data[200][200];\n\t\trep(i,n){\n\t\t\trep(j,n){\n\t\t\t\tint a,b;\n\t\t\t\tcin>>a>>b;\n\t\t\t\tdata[i][j]=make_pair(a,b);\n\t\t\t}\n\t\t}\n\n\t\tinit(20000);\n\n\t\trep(i,n){\n\t\t\trep(j,n){\n\t\t\t\tunite( n*i+j , n*data[i][j].second+data[i][j].first );\n\t\t\t}\n\t\t}\n\t\tbool used[20000]={};\n\t\tint c=0;\n\t\trep(i,n)rep(j,n)used[ find(n*i+j) ]=true;\n\t\trep(i,20000)if(used[i])c++;\n\t\tcout<<c<<endl;\n\t}\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 1652, "score_of_the_acc": -0.0432, "final_rank": 11 }, { "submission_id": "aoj_2217_1410862", "code_snippet": "#include \"bits/stdc++.h\"\n\nusing namespace std;\n\ntypedef vector<int> vi;\ntypedef pair<int,int> pii;\ntypedef long long ll;\n\n#define rep(i,n) for(ll i=0;i<(ll)(n);i++)\n#define all(a) (a).begin(),(a).end()\n#define pb push_back\n#ifdef Debug\n#define dump(x) cerr << #x << \" = \" << (x) << endl\n#else\n#define dump(x)\n#endif\n\n\nint par[10000];\nint Rank[10000];\n\nvoid init(int n){\n rep(i,n){\n par[i]=i;\n Rank[i]=0;\n }\n}\n\nint find(int x){\n if(par[x]==x) {\n return x;\n }else{\n return par[x]=find(par[x]);\n }\n}\n\nvoid unite(int x,int y){\n x=find(x);\n y=find(y);\n if(x==y) return;\n \n if(Rank[x]<Rank[y]){\n par[x]=y;\n }else{\n par[y]=x;\n if(Rank[x]==Rank[y])Rank[x]++;\n }\n}\n\n\n\n\nint main(){\n\tint n;\n\twhile(cin>>n){\n\t\tif(n==0)break;\n\t\tpii data[110][110]={};\n\t\trep(i,n)\n\t\t\trep(j,n)\n\t\t\t\tcin>>data[i][j].first>>data[i][j].second;\n\n\t\tinit(n*n);\n\n\t\trep(i,n){\n\t\t\trep(j,n){\n\t\t\t\tunite(n*i+j,n*data[i][j].second+data[i][j].first);\n\t\t\t}\n\t\t}\n\t\tbool used[10000]={};\n\t\tint sum=0;\n\t\trep(i,n*n){\n\t\t\tint aa=find(i);\n\t\t\tif( used[ aa ]==false ){\n\t\t\t\tsum++;\n\t\t\t\tused[aa]=true;\n\t\t\t}\n\t\t}\n\t\tcout<<sum<<endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1348, "score_of_the_acc": -0.0101, "final_rank": 10 }, { "submission_id": "aoj_2217_1268654", "code_snippet": "#include<cstdio>\n#include<algorithm>\n#include<vector>\n#include<queue>\n#include<string>\n#include<iostream>\n#include<cmath>\n#include<map>\n#include<set>\n#include<climits>\nusing namespace std;\ntypedef vector<string>vs;\ntypedef vector<int>vi;\ntypedef vector<vi>vvi;\ntypedef vector<double>vd;\ntypedef pair<int,int>pii;\ntypedef long long ll;\ntypedef pair<ll,ll>pll;\ntypedef vector<ll>vl;\n#define rrep(i,x,n) for(int i=(x);i<(n);++i)\n#define rep(i,x) rrep(i,0,(x))\n#define fi first\n#define se second\n#define each(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();++i)\n#define all(c) (c).begin(),(c).end()\n#define rall(c) (c).rbegin(),(c).rend()\n#define pb push_back\n#define maxs(a,b) (a)=max(a,b)\n#define mins(a,b) (a)=min(a,b)\n\nint main(){\n int N;\n while(scanf(\"%d\",&N),N){\n vi V(N*N);\n rep(i,N*N){\n int x,y;\n scanf(\"%d%d\",&x,&y);\n V[i]=y*N+x;\n }\n int cnt=0;\n bool used[10000]={0};\n rep(i,N*N){\n if(used[i])continue;\n int pos=i;\n bool u[10000]={0};\n while(!u[pos]){\n u[pos]=true;\n pos=V[pos];\n }\n if(pos!=i)continue;\n cnt++;\n rep(i,N*N)used[i]|=u[i];\n }\n printf(\"%d\\n\",cnt);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 1252, "score_of_the_acc": -0.0868, "final_rank": 12 }, { "submission_id": "aoj_2217_1164679", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\n#define ALL(v) (v).begin(), (v).end()\n\nclass DisjointSet{\nprivate:\n vector< int > rank, p;\n void link(int x,int y){\n if(rank[x] > rank[y]){\n p[y] = x;\n }else{\n p[x] = y;\n if(rank[x] == rank[y]) rank[y]++;\n }\n }\npublic:\n DisjointSet(int size){\n rank.resize(size,0);\n p.resize(size,0);\n for(int i = 0; i < size; i++){\n makeSet(i);\n }\n }\n void makeSet(int x){\n p[x] = x, rank[x] = 0;\n }\n void Union(int x,int y){\n link(findSet(x),findSet(y));\n }\n int findSet(int x){\n return( x != p[x] ? p[x] = findSet(p[x]) : p[x]);\n }\n};\n\nint main()\n{\n int N;\n while(scanf(\"%d\", &N), N){\n DisjointSet UnionFind(N * N);\n for(int i = 0; i < N; i++){\n for(int j = 0; j < N; j++){\n int x, y;\n scanf(\"%d %d\", &x, &y);\n UnionFind.Union( i * N + j, y * N + x);\n }\n }\n vector< bool > size(N * N, false);\n for(int i = 0; i < N * N; i++){\n size[UnionFind.findSet(i)] = true;\n }\n cout << count(ALL(size), true) << endl;\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1284, "score_of_the_acc": -0.0066, "final_rank": 6 }, { "submission_id": "aoj_2217_1164676", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\n#define ALL(v) (v).begin(), (v).end()\n\nclass DisjointSet{\nprivate:\n vector<int> rank, p;\n void link(int x,int y){\n if(rank[x] > rank[y]){\n p[y] = x;\n }else{\n p[x] = y;\n if(rank[x] == rank[y]) rank[y]++;\n }\n }\npublic:\n DisjointSet(int size){\n rank.resize(size,0);\n p.resize(size,0);\n for(int i = 0; i < size; i++){\n makeSet(i);\n }\n }\n void makeSet(int x){\n p[x] = x, rank[x] = 0;\n }\n void Union(int x,int y){\n link(findSet(x),findSet(y));\n }\n int findSet(int x){\n return( x != p[x] ? p[x] = findSet(p[x]) : p[x]);\n }\n};\n\nint main()\n{\n int N;\n while(cin >> N, N){\n DisjointSet UnionFind(N * N);\n for(int i = 0; i < N; i++){\n for(int j = 0; j < N; j++){\n int x, y;\n cin >> x >> y;\n UnionFind.Union( i * N + j, y * N + x);\n }\n }\n vector< bool > size(N * N, false);\n for(int i = 0; i < N * N; i++){\n size[UnionFind.findSet(i)] = true;\n }\n cout << count(ALL(size), true) << endl;\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1276, "score_of_the_acc": -0.0061, "final_rank": 5 }, { "submission_id": "aoj_2217_1162980", "code_snippet": "#include<iostream>\n#include<algorithm>\n#include<cstring>\nusing namespace std;\n\n\nbool visited1[100][100];\nbool visited2[100][100];\nint dx[100][100];\nint dy[100][100];\n\n\nbool f(int y,int x){\n\tif(visited1[y][x]){\n\t\treturn visited2[y][x];\n\t}\n\tvisited1[y][x]=true;\n\tvisited2[y][x]=true;\n\tbool res=f(dy[y][x],dx[y][x]);\n\tvisited2[y][x]=false;\n\treturn res;\n}\n\nint main(){\n\tint n;\n\twhile(cin>>n&&n){\n\t\tmemset(visited1,false,sizeof(visited1));\n\t\tfor(int y=0;y<n;y++){\n\t\t\tfor(int x=0;x<n;x++){\n\t\t\t\tcin>>dx[y][x]>>dy[y][x];\n\t\t\t}\n\t\t}\n\t\tint ans=0;\n\t\tfor(int y=0;y<n;y++){\n\t\t\tfor(int x=0;x<n;x++){\n\t\t\t\tif(!visited1[y][x])ans+=f(y,x);\n\t\t\t}\n\t\t}\n\t\tcout<<ans<<endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1264, "score_of_the_acc": -0.0055, "final_rank": 4 }, { "submission_id": "aoj_2217_1007575", "code_snippet": "#include<stdio.h>\n#include<algorithm>\nint x[110][110];\nint y[110][110];\nint v[110][110];\nint main(){\n\tint a;\n\twhile(scanf(\"%d\",&a),a){\n\t\tfor(int i=0;i<a;i++)\n\t\t\tfor(int j=0;j<a;j++)\n\t\t\t\tscanf(\"%d%d\",&y[i][j],&x[i][j]);\n\t\tint ret=0;\n\t\tfor(int i=0;i<a;i++)\n\t\t\tfor(int j=0;j<a;j++)\n\t\t\t\tv[i][j]=0;\n\t\tfor(int i=0;i<a;i++){\n\t\t\tfor(int j=0;j<a;j++){\n\t\t\t\tint row=i;\n\t\t\t\tint col=j;\n\t\t\t\tint s,t;\n\n\t\t\t\twhile(!v[row][col]){\n\t\t\t\t\tv[row][col]=1;\n\t\t\t\t\ts=x[row][col];\n\t\t\t\t\tt=y[row][col];\n\t\t\t\t\trow=s;col=t;\n\t\t\t\t}\n\t\t\t\tif(v[row][col]==1)ret++;\n\t\t\t\trow=i;col=j;\n\t\t\t\twhile(v[row][col]!=2){\n\t\t\t\t\tv[row][col]=2;\n\t\t\t\t\ts=x[row][col];\n\t\t\t\t\tt=y[row][col];\n\t\t\t\t\trow=s;col=t;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tprintf(\"%d\\n\",ret);\n\t}\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1164, "score_of_the_acc": 0, "final_rank": 1 }, { "submission_id": "aoj_2217_1000420", "code_snippet": "#include <map>\n#include <set>\n#include <list>\n#include <cmath>\n#include <queue>\n#include <stack>\n#include <cstdio>\n#include <string>\n#include <vector>\n#include <complex>\n#include <cstdlib>\n#include <cstring>\n#include <numeric>\n#include <sstream>\n#include <iostream>\n#include <algorithm>\n#include <functional>\n\n#define mp make_pair\n#define pb push_back\n#define all(x) (x).begin(),(x).end()\n#define rep(i,n) for(int i=0;i<(n);i++)\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef vector<bool> vb;\ntypedef vector<int> vi;\ntypedef vector<vb> vvb;\ntypedef vector<vi> vvi;\ntypedef pair<int,int> pii;\n\nconst int INF=1<<29;\nconst double EPS=1e-9;\n\nconst int dx[]={1,0,-1,0},dy[]={0,-1,0,1};\nint n;\npii board[110][110];\nint visit[110][110];\n\nbool check(int px,int py,int level){\n\tint nx,ny;\n\twhile(visit[py][px] == 0){\n\t\tvisit[py][px] = level;\n\t\tnx = board[py][px].first;\n\t\tny = board[py][px].second;\n\t\tpx = nx;\n\t\tpy = ny;\n\t}\n\tif(visit[py][px] == level)return true;\n\treturn false;\n}\n\nint main(){\n\twhile(cin>>n,n){\n\n\t\tfor(int i = 0;i < n;i++){\n\t\t\tfor(int j = 0;j < n;j++){\n\t\t\t\tcin>>board[i][j].first>>board[i][j].second;\n\t\t\t}\n\t\t}\n\t\tmemset(visit, 0, sizeof(visit));\n\t\tint level = 0;\n\t\tint ans = 0;\n\t\tfor(int i = 0;i < n;i++){\n\t\t\tfor(int j = 0;j < n;j++){\n\t\t\t\tif(visit[i][j] == 0){\n\t\t\t\t\tlevel++;\n\t\t\t\t\tif(check(j, i, level)){\n\t\t\t\t\t\tans++;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tcout <<ans<<endl;\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1308, "score_of_the_acc": -0.0079, "final_rank": 9 }, { "submission_id": "aoj_2217_978749", "code_snippet": "#include<iostream>\n#include<string>\n#include<algorithm>\n#include<map>\n#include<vector>\n#include<cmath>\n#include<cstdio>\n#define loop(i,a,b) for(int i=a;i<b;i++) \n#define rep(i,a) loop(i,0,a)\n#define pb(in,tmp) in.push_back(tmp)\n#define all(in) in.begin(),in.end()\n#define PI acos(-1)\nusing namespace std;\nint tile[100][200]={0};\nint co=100;\nvoid ride(int i,int j,int tile[100][200],int& count){\n int tmp=tile[i][2*j];\n tile[i][2*j]=co;\n if(tile[tile[i][2*j+1]][2*tmp]==co){count++;return;}\n else if(tile[tile[i][2*j+1]][2*tmp]>100&&tile[tile[i][2*j+1]][2*tmp]<co)return;\n else ride(tile[i][2*j+1],tmp,tile,count);\n\n}\nint main(){\n int n;\n while(cin>>n,n){\n int count=0;\n rep(i,n){\n rep(j,2*n){\n\tcin>>tile[i][j];\n }\n }\n rep(i,n){\n rep(j,n){\n\tco++;\n\tif(tile[i][2*j]>100)continue;\n\tride(i,j,tile,count);\n }\n } \n cout<<count<<endl;\n }\n\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1236, "score_of_the_acc": -0.004, "final_rank": 2 } ]
aoj_2219_cpp
Problem D: THE BYDOLM@STER Description THE BYDOLM@STER(バイドルマスター)とは1rem社より2010/4/1にEXIDNAで発売が予定されている育成シミュレーションゲームである。一応断っておくが、今月初めにネットワーク接続サービスが停止した某アーケードゲームとはたぶん関係が無い。 このゲームはバイドルたちからプロデュースするユニット(編隊)のメンバーを選択し、メンバーたちとのレッスンやコミュニケーションを通じて、彼女(彼)らをバイドルの頂点、トップバイドルに育て上げるゲームである。 各バイドルには能力値としてボーカル、ダンス、ルックスの3つのパラメータを持ち、ユニットの能力値はユニットに属している全てのバイドルのパラメータの合計値となる。ユニットの3つの能力値のうち最大の物がユニットのランクとなる。 ユニットに人数制限は無く、メンバーの人数が1体でも、3体でも、100体でもユニットとして活動できる。もちろん同じバイドルを複数雇うこと出来るが、バイドルを雇うためには費用がかかるのでこれを考慮に入れなければならない。 プロデューサーであるあなたは最高のユニットを作るためにプログラムを書いて計算することにした。 Input 入力は複数のテストケースからなる。 各テストケースの1行目にはバイドルの数Nと使用可能な費用Mが与えられる。(1<=N,M<=300) 次の2*N行には各バイドルに関しての情報が書かれている。 バイドルの情報の1行目にはバイドルの名前。バイドルの名前はアルファベットと空白から成り、30文字を以下である。また同一名のバイドルは存在しない。 2行目には整数C、V、D、Lが与えられる。Cはそのバイドルを1体雇用するコスト、Vはボーカル、Dはダンス、Lはルックスの能力値を表す。(1<=C,V,D,L<=300) 入力はEOFで終わる。 Output 与えられた費用の中で作れるユニットのランクの最大値を答えよ。 ユニットが作れない場合は0を出力せよ。 Sample Input 3 10 Dobkeradops 7 5 23 10 PataPata 1 1 2 1 dop 5 3 11 14 2 300 Bydo System Alpha 7 11 4 7 Green Inferno 300 300 300 300 Output for Sample Input 29 462
[ { "submission_id": "aoj_2219_8860773", "code_snippet": "#include <algorithm>\n#include <cfloat>\n#include <climits>\n#include <cmath>\n#include <complex>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <ctime>\n#include <functional>\n#include <iostream>\n#include <list>\n#include <map>\n#include <memory>\n#include <numeric>\n#include <queue>\n#include <set>\n#include <sstream>\n#include <stack>\n#include <string>\n#include <utility>\n#include <vector>\nusing namespace std;\n#ifdef _MSC_VER\n#define __typeof__ decltype\ntemplate <class T> int __builtin_popcount(T n) {\n return n ? 1 + __builtin_popcount(n & (n - 1)) : 0;\n}\n#endif\n#define foreach(it, c) \\\n for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it)\n#define all(c) (c).begin(), (c).end()\n#define rall(c) (c).rbegin(), (c).rend()\n#define CLEAR(arr, val) memset(arr, val, sizeof(arr))\n#define rep(i, n) for (int i = 0; i < n; ++i)\ntemplate <class T> void max_swap(T &a, const T &b) { a = max(a, b); }\ntemplate <class T> void min_swap(T &a, const T &b) { a = min(a, b); }\ntypedef long long ll;\ntypedef pair<int, int> pint;\nconst double PI = acos(-1.0);\nconst int dx[] = {0, 1, 0, -1};\nconst int dy[] = {1, 0, -1, 0};\nint n, m;\nint dpru(int *c, int *v) {\n int dp[2][333]; // Reduced the size of the dp array\n CLEAR(dp, 0);\n for (int i = 0; i < n; ++i) {\n for (int j = 0; j <= m; ++j) {\n dp[(i + 1)%2][j] = dp[i%2][j]; // Use the current and previous rows only\n if (j >= c[i])\n max_swap(dp[(i + 1)%2][j], dp[(i + 1)%2][j - c[i]] + v[i]); // Use the current and previous rows only\n }\n }\n return dp[n%2][m]; // Return the value from the current row\n}\nint main() {\n while (cin >> n >> m) {\n int c[333], v[3][333];\n rep(i, n) {\n string s;\n int t;\n getline(cin, s);\n getline(cin, s);\n cin >> t;\n c[i] = t;\n rep(j, 3) cin >> v[j][i];\n }\n int res = 0;\n rep(i, 3) max_swap(res, dpru(c, v[i]));\n cout << res << endl;\n }\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 3088, "score_of_the_acc": -0.2669, "final_rank": 4 }, { "submission_id": "aoj_2219_8860765", "code_snippet": "#include <algorithm>\n#include <cfloat>\n#include <climits>\n#include <cmath>\n#include <complex>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <ctime>\n#include <functional>\n#include <iostream>\n#include <list>\n#include <map>\n#include <memory>\n#include <numeric>\n#include <queue>\n#include <set>\n#include <sstream>\n#include <stack>\n#include <string>\n#include <utility>\n#include <vector>\nusing namespace std;\n\n#ifdef _MSC_VER\n#define __typeof__ decltype\ntemplate <class T> int __builtin_popcount(T n) {\n return n ? 1 + __builtin_popcount(n & (n - 1)) : 0;\n}\n#endif\n\n#define foreach(it, c) \\\n for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it)\n#define all(c) (c).begin(), (c).end()\n#define rall(c) (c).rbegin(), (c).rend()\n#define CLEAR(arr, val) memset(arr, val, sizeof(arr))\n#define rep(i, n) for (int i = 0; i < n; ++i)\n\ntypedef long long ll;\ntypedef pair<int, int> pint;\n\nconst double PI = acos(-1.0);\nconst int dx[] = {0, 1, 0, -1};\nconst int dy[] = {1, 0, -1, 0};\n\nint n, m;\n\nint dpru(int *c, int *v) {\n int dp[333][333];\n CLEAR(dp, 0);\n for (int i = 0; i < n; ++i) {\n for (int j = 0; j <= m; ++j) {\n dp[i + 1][j] = dp[i][j];\n if (j >= c[i])\n dp[i + 1][j] = max(dp[i + 1][j], dp[i + 1][j - c[i]] + v[i]);\n }\n }\n return dp[n][m];\n}\n\nint main() {\n while (cin >> n >> m) {\n int c[333], v[3][333];\n rep(i, n) {\n string s;\n int t;\n getline(cin, s);\n getline(cin, s);\n cin >> t;\n c[i] = t;\n rep(j, 3) cin >> v[j][i];\n }\n int res = 0;\n rep(i, 3) res = max(res, dpru(c, v[i]));\n cout << res << endl;\n }\n}", "accuracy": 1, "time_ms": 150, "memory_kb": 3536, "score_of_the_acc": -0.7015, "final_rank": 8 }, { "submission_id": "aoj_2219_8835682", "code_snippet": "#include <algorithm>\n#include <iostream>\n#include <cstring>\nusing namespace std;\n\nconst int MAX_N = 333;\nconst int MAX_M = 333;\n\nint dp[MAX_M];\nint n, m;\n\nint dpru(int *c, int *v) {\n memset(dp, 0, sizeof(dp));\n for (int i = 0; i < n; ++i) {\n for (int j = 0; j <= m; ++j) {\n dp[j] = max(dp[j], (j >= c[i] ? dp[j - c[i]] + v[i] : 0));\n }\n }\n return dp[m];\n}\n\nint main() {\n while (cin >> n >> m) {\n int c[MAX_N], v[3][MAX_N];\n for (int i = 0; i < n; ++i) {\n string s;\n int t;\n getline(cin, s);\n getline(cin, s);\n cin >> t;\n c[i] = t;\n for (int j = 0; j < 3; ++j) {\n cin >> v[j][i];\n }\n }\n int res = 0;\n for (int i = 0; i < 3; ++i) {\n res = max(res, dpru(c, v[i]));\n }\n cout << res << endl;\n }\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 2988, "score_of_the_acc": -0.2188, "final_rank": 2 }, { "submission_id": "aoj_2219_8835661", "code_snippet": "#define _USE_MATH_DEFINES\n#define INF 0x3f3f3f3f\n#include <algorithm>\n#include <bitset>\n#include <cctype>\n#include <cmath>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <deque>\n#include <iostream>\n#include <limits>\n#include <list>\n#include <map>\n#include <queue>\n#include <set>\n#include <sstream>\n#include <stack>\n#include <string>\n#include <utility>\nusing namespace std;\ntypedef long long ll;\ntypedef pair<int, int> P;\ntypedef pair<int, P> PP;\nint tx[] = {0, 1, 0, -1};\nint ty[] = {-1, 0, 1, 0};\nstatic const double EPS = 1e-8;\nclass Data {\npublic:\n string mName;\n int mC, mV, mD, mL;\n Data(string _n, int _c, int _v, int _d, int _l) {\n mName = _n;\n mC = _c;\n mV = _v;\n mD = _d;\n mL = _l;\n }\n};\nint dp[301][3];\nint main() {\n int N, M;\n string line;\n stringstream ss;\n while (getline(cin, line)) {\n ss << line;\n ss >> N >> M;\n ss.clear();\n vector<Data> idols;\n for (int i = 0; i < N; i++) {\n getline(cin, line);\n string name = line;\n getline(cin, line);\n ss << line;\n int C, V, D, L;\n ss >> C >> V >> D >> L;\n ss.clear();\n idols.push_back(Data(name, C, V, D, L));\n }\n memset(dp, 0, sizeof(dp));\n for (int i = 0; i <= M; i++) {\n for (int j = 0; j < idols.size(); j++) {\n for (int k = 0; k <= 300; k++) {\n if (i - idols[j].mC * k < 0)\n break;\n dp[i][0] =\n max(dp[i - idols[j].mC * k][0] + idols[j].mV * k, dp[i][0]);\n dp[i][1] =\n max(dp[i - idols[j].mC * k][1] + idols[j].mD * k, dp[i][1]);\n dp[i][2] =\n max(dp[i - idols[j].mC * k][2] + idols[j].mL * k, dp[i][2]);\n }\n }\n }\n int maxv = 0;\n for (int i = 0; i <= M; i++) {\n for (int j = 0; j < 3; j++) {\n maxv = max(dp[i][j], maxv);\n }\n }\n printf(\"%d\\n\", maxv);\n }\n}", "accuracy": 1, "time_ms": 280, "memory_kb": 3956, "score_of_the_acc": -1.31, "final_rank": 16 }, { "submission_id": "aoj_2219_8822176", "code_snippet": "#include <algorithm>\n#include <iostream>\n#include <string>\n#include <cstring>\nusing namespace std;\n\n#define CLEAR(arr, val) memset(arr, val, sizeof(arr))\n#define rep(i, n) for (int i = 0; i < n; ++i)\ntemplate <class T> void max_swap(T &a, const T &b) { a = max(a, b); }\n\nint dpru(int *c, int *v, int n, int m) {\n int dp[333][333];\n CLEAR(dp, 0);\n for (int i = 0; i < n; ++i) {\n for (int j = 0; j <= m; ++j) {\n dp[i + 1][j] = dp[i][j];\n if (j >= c[i])\n max_swap(dp[i + 1][j], dp[i + 1][j - c[i]] + v[i]);\n }\n }\n return dp[n][m];\n}\n\nint main() {\n int n, m;\n while (cin >> n >> m) {\n int c[333], v[3][333];\n rep(i, n) {\n string s;\n int t;\n getline(cin, s);\n getline(cin, s);\n cin >> t;\n c[i] = t;\n rep(j, 3) cin >> v[j][i];\n }\n int res = 0;\n rep(i, 3) max_swap(res, dpru(c, v[i], n, m));\n cout << res << '\\n';\n }\n}", "accuracy": 1, "time_ms": 150, "memory_kb": 3640, "score_of_the_acc": -0.7516, "final_rank": 11 }, { "submission_id": "aoj_2219_8822167", "code_snippet": "#define _USE_MATH_DEFINES\n#define INF 0x3f3f3f3f\n#include <algorithm>\n#include <bitset>\n#include <cctype>\n#include <cmath>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <deque>\n#include <iostream>\n#include <limits>\n#include <list>\n#include <map>\n#include <queue>\n#include <set>\n#include <sstream>\n#include <stack>\n#include <string>\n#include <utility>\nusing namespace std;\ntypedef long long ll;\ntypedef pair<int, int> P;\ntypedef pair<int, P> PP;\nint tx[] = {0, 1, 0, -1};\nint ty[] = {-1, 0, 1, 0};\nstatic const double EPS = 1e-8;\nclass Data {\npublic:\n string mName;\n int mC, mV, mD, mL;\n Data(string _n, int _c, int _v, int _d, int _l) {\n mName = _n;\n mC = _c;\n mV = _v;\n mD = _d;\n mL = _l;\n }\n};\nint dp[301][3];\nint main() {\n int N, M;\n string line;\n stringstream ss;\n while (getline(cin, line)) {\n ss << line;\n ss >> N >> M;\n ss.clear();\n vector<Data> idols;\n for (int i = 0; i < N; i++) {\n getline(cin, line);\n string name = line;\n getline(cin, line);\n ss << line;\n int C, V, D, L;\n ss >> C >> V >> D >> L;\n ss.clear();\n idols.push_back(Data(name, C, V, D, L));\n }\n memset(dp, 0, sizeof(dp));\n int size = idols.size();\n for (int i = 0; i <= M; i++) {\n for (int j = 0; j < size; j++) {\n for (int k = 0; k <= 300; k++) {\n if (i - idols[j].mC * k < 0)\n break;\n int valC = dp[i - idols[j].mC * k][0] + idols[j].mV * k;\n int valD = dp[i - idols[j].mC * k][1] + idols[j].mD * k;\n int valL = dp[i - idols[j].mC * k][2] + idols[j].mL * k;\n dp[i][0] = max(valC, dp[i][0]);\n dp[i][1] = max(valD, dp[i][1]);\n dp[i][2] = max(valL, dp[i][2]);\n }\n }\n }\n int maxv = 0;\n for (int i = 0; i <= M; i++) {\n for (int j = 0; j < 3; j++) {\n maxv = max(dp[i][j], maxv);\n }\n }\n printf(\"%d\\n\", maxv);\n }\n}", "accuracy": 1, "time_ms": 250, "memory_kb": 3916, "score_of_the_acc": -1.197, "final_rank": 14 }, { "submission_id": "aoj_2219_8822157", "code_snippet": "#define _USE_MATH_DEFINES\n#define INF 0x3f3f3f3f\n#include <algorithm>\n#include <bitset>\n#include <cctype>\n#include <cmath>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <deque>\n#include <iostream>\n#include <limits>\n#include <list>\n#include <map>\n#include <queue>\n#include <set>\n#include <sstream>\n#include <stack>\n#include <string>\n#include <utility>\nusing namespace std;\ntypedef long long ll;\ntypedef pair<int, int> P;\ntypedef pair<int, P> PP;\nint tx[] = {0, 1, 0, -1};\nint ty[] = {-1, 0, 1, 0};\nstatic const double EPS = 1e-8;\nclass Data {\npublic:\n string mName;\n int mC, mV, mD, mL;\n Data(string _n, int _c, int _v, int _d, int _l) {\n mName = _n;\n mC = _c;\n mV = _v;\n mD = _d;\n mL = _l;\n }\n};\nint dp[301][3];\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n int N, M;\n string line;\n stringstream ss;\n while (getline(cin, line)) {\n ss << line;\n ss >> N >> M;\n ss.clear();\n vector<Data> idols;\n idols.reserve(N);\n for (int i = 0; i < N; i++) {\n getline(cin, line);\n string name = line;\n getline(cin, line);\n ss << line;\n int C, V, D, L;\n ss >> C >> V >> D >> L;\n ss.clear();\n idols.push_back(Data(name, C, V, D, L));\n }\n memset(dp, 0, sizeof(dp));\n for (int j = 0; j < idols.size(); j++) {\n for (int i = 0; i <= M; i++) {\n for (int k = 0; k <= i/idols[j].mC; k++) {\n if (i - idols[j].mC * k < 0)\n break;\n dp[i][0] =\n max(dp[i - idols[j].mC * k][0] + idols[j].mV * k, dp[i][0]);\n dp[i][1] =\n max(dp[i - idols[j].mC * k][1] + idols[j].mD * k, dp[i][1]);\n dp[i][2] =\n max(dp[i - idols[j].mC * k][2] + idols[j].mL * k, dp[i][2]);\n }\n }\n }\n int maxv = 0;\n for (int i = 0; i <= M; i++) {\n for (int j = 0; j < 3; j++) {\n maxv = max(dp[i][j], maxv);\n }\n }\n printf(\"%d\\n\", maxv);\n }\n}", "accuracy": 1, "time_ms": 320, "memory_kb": 3944, "score_of_the_acc": -1.4293, "final_rank": 19 }, { "submission_id": "aoj_2219_8809479", "code_snippet": "#define _USE_MATH_DEFINES\n#define INF 0x3f3f3f3f\n#include <algorithm>\n#include <bitset>\n#include <cctype>\n#include <cmath>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <deque>\n#include <iostream>\n#include <limits>\n#include <list>\n#include <map>\n#include <queue>\n#include <set>\n#include <sstream>\n#include <stack>\n#include <string>\n#include <utility>\nusing namespace std;\ntypedef long long ll;\ntypedef pair<int, int> P;\ntypedef pair<int, P> PP;\nint tx[] = {0, 1, 0, -1};\nint ty[] = {-1, 0, 1, 0};\nstatic const double EPS = 1e-8;\nclass Data {\npublic:\n string mName;\n int mC, mV, mD, mL;\n Data(string _n, int _c, int _v, int _d, int _l) {\n mName = _n;\n mC = _c;\n mV = _v;\n mD = _d;\n mL = _l;\n }\n};\nint dp[301][3];\nint main() {\n int N, M;\n string line;\n stringstream ss;\n while (getline(cin, line)) {\n ss << line;\n ss >> N >> M;\n ss.clear();\n vector<Data> idols;\n for (int i = 0; i < N; i++) {\n getline(cin, line);\n string name = line;\n getline(cin, line);\n ss << line;\n int C, V, D, L;\n ss >> C >> V >> D >> L;\n ss.clear();\n idols.push_back(Data(name, C, V, D, L));\n }\n memset(dp, 0, sizeof(dp));\n for (int j = 0; j < idols.size(); j++) {\n for (int i = 0; i <= M; i++) {\n for (int k = 0; k <= 300; k++) {\n if (i - idols[j].mC * k < 0)\n break;\n dp[i][0] =\n max(dp[i - idols[j].mC * k][0] + idols[j].mV * k, dp[i][0]);\n dp[i][1] =\n max(dp[i - idols[j].mC * k][1] + idols[j].mD * k, dp[i][1]);\n dp[i][2] =\n max(dp[i - idols[j].mC * k][2] + idols[j].mL * k, dp[i][2]);\n }\n }\n }\n int maxv = 0;\n for (int i = 0; i <= M; i++) {\n for (int j = 0; j < 3; j++) {\n maxv = max(dp[i][j], maxv);\n }\n }\n printf(\"%d\\n\", maxv);\n }\n}", "accuracy": 1, "time_ms": 260, "memory_kb": 3920, "score_of_the_acc": -1.2302, "final_rank": 15 }, { "submission_id": "aoj_2219_8809477", "code_snippet": "#include <algorithm>\n#include <cstring>\n#include <iostream>\nusing namespace std;\n\n#define rep(i, n) for (int i = 0; i < n; ++i)\n\nconst int dx[] = {0, 1, 0, -1};\nconst int dy[] = {1, 0, -1, 0};\n\nint n, m;\n\nint dpru(int *c, int *v) {\n int dp[333][333];\n memset(dp, 0, sizeof(dp));\n for (int i = 0; i < n; ++i) {\n for (int j = 0; j <= m; ++j) {\n dp[i + 1][j] = dp[i][j];\n if (j >= c[i]) {\n int temp = dp[i + 1][j - c[i]] + v[i];\n if (temp > dp[i + 1][j]) {\n dp[i + 1][j] = temp;\n }\n }\n }\n }\n return dp[n][m];\n}\n\nint main() {\n while (cin >> n >> m) {\n int c[333], v[999];\n memset(c, 0, sizeof(c));\n memset(v, 0, sizeof(v));\n rep(i, n) {\n string s;\n int t;\n getline(cin, s);\n getline(cin, s);\n cin >> t;\n c[i] = t;\n rep(j, 3) cin >> v[i + j * n];\n }\n int res = 0;\n rep(i, 3) {\n int temp = dpru(c, v + i * n);\n if (temp > res) {\n res = temp;\n }\n }\n cout << res << endl;\n }\n}", "accuracy": 1, "time_ms": 160, "memory_kb": 3564, "score_of_the_acc": -0.7462, "final_rank": 9 }, { "submission_id": "aoj_2219_8216207", "code_snippet": "#define _USE_MATH_DEFINES\n#define INF 0x3f3f3f3f\n#include <algorithm>\n#include <bitset>\n#include <cctype>\n#include <cmath>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <deque>\n#include <iostream>\n#include <limits>\n#include <list>\n#include <map>\n#include <queue>\n#include <set>\n#include <sstream>\n#include <stack>\n#include <string>\n#include <utility>\nusing namespace std;\ntypedef long long ll;\ntypedef pair<int, int> P;\ntypedef pair<int, P> PP;\nint tx[] = {0, 1, 0, -1};\nint ty[] = {-1, 0, 1, 0};\nstatic const double EPS = 1e-8;\nclass Data {\npublic:\n string mName;\n int mC, mV, mD, mL;\n Data(string _n, int _c, int _v, int _d, int _l) {\n mName = _n;\n mC = _c;\n mV = _v;\n mD = _d;\n mL = _l;\n }\n};\nint dp[301][3];\nint main() {\n int N, M;\n string line;\n stringstream ss;\n while (getline(cin, line)) {\n ss << line;\n ss >> N >> M;\n ss.clear();\n vector<Data> idols;\n for (int i = 0; i < N; i++) {\n getline(cin, line);\n string name = line;\n getline(cin, line);\n ss << line;\n int C, V, D, L;\n ss >> C >> V >> D >> L;\n ss.clear();\n idols.push_back(Data(name, C, V, D, L));\n }\n memset(dp, 0, sizeof(dp));\n for (int i = 0; i <= M; i++) {\n for (int j = 0; j < idols.size(); j++) {\n for (int k = 0; k <= M; k++) {\n if (i - idols[j].mC * k < 0)\n break;\n dp[i][0] =\n max(dp[i - idols[j].mC * k][0] + idols[j].mV * k, dp[i][0]);\n dp[i][1] =\n max(dp[i - idols[j].mC * k][1] + idols[j].mD * k, dp[i][1]);\n dp[i][2] =\n max(dp[i - idols[j].mC * k][2] + idols[j].mL * k, dp[i][2]);\n }\n }\n }\n int maxv = 0;\n for (int i = 0; i <= M; i++) {\n for (int j = 0; j < 3; j++) {\n maxv = max(dp[i][j], maxv);\n }\n }\n printf(\"%d\\n\", maxv);\n }\n}", "accuracy": 1, "time_ms": 270, "memory_kb": 4064, "score_of_the_acc": -1.3308, "final_rank": 17 }, { "submission_id": "aoj_2219_8216206", "code_snippet": "#define _USE_MATH_DEFINES\n#define INF 0x3f3f3f3f\n#include <algorithm>\n#include <bitset>\n#include <cctype>\n#include <cmath>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <deque>\n#include <iostream>\n#include <limits>\n#include <list>\n#include <map>\n#include <queue>\n#include <set>\n#include <sstream>\n#include <stack>\n#include <string>\n#include <utility>\nusing namespace std;\ntypedef long long ll;\ntypedef pair<int, int> P;\ntypedef pair<int, P> PP;\nint tx[] = {0, 1, 0, -1};\nint ty[] = {-1, 0, 1, 0};\nstatic const double EPS = 1e-8;\nclass Data {\npublic:\n string mName;\n int mC, mV, mD, mL;\n Data(string _n, int _c, int _v, int _d, int _l) {\n mName = _n;\n mC = _c;\n mV = _v;\n mD = _d;\n mL = _l;\n }\n};\nint dp[301][3];\nint main() {\n int N, M;\n string line;\n stringstream ss;\n while (getline(cin, line)) {\n ss << line;\n ss >> N >> M;\n ss.clear();\n vector<Data> idols;\n for (int i = 0; i < N; i++) {\n getline(cin, line);\n string name = line;\n getline(cin, line);\n ss << line;\n int C, V, D, L;\n ss >> C >> V >> D >> L;\n ss.clear();\n idols.push_back(Data(name, C, V, D, L));\n }\n memset(dp, 0, sizeof(dp));\n for (int i = 0; i <= M; i++) {\n for (int j = 0; j < idols.size(); j++) {\n for (int k = 0; k <= 300; k++) {\n if (i - idols[j].mC * k < 0)\n break;\n dp[i][0] =\n max(dp[i - idols[j].mC * k][0] + idols[j].mV * k, dp[i][0]);\n dp[i][1] =\n max(dp[i - idols[j].mC * k][1] + idols[j].mD * k, dp[i][1]);\n dp[i][2] =\n max(dp[i - idols[j].mC * k][2] + idols[j].mL * k, dp[i][2]);\n }\n }\n }\n int maxv = 0;\n for (int i = 0; i <= M; i++) {\n for (int j = 0; j < 3; j++) {\n maxv = max(dp[i][j], maxv);\n }\n }\n printf(\"%d\\n\", maxv);\n }\n}", "accuracy": 1, "time_ms": 280, "memory_kb": 4064, "score_of_the_acc": -1.3621, "final_rank": 18 }, { "submission_id": "aoj_2219_8216192", "code_snippet": "#include <algorithm>\n#include <cfloat>\n#include <climits>\n#include <cmath>\n#include <complex>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <ctime>\n#include <functional>\n#include <iostream>\n#include <list>\n#include <map>\n#include <memory>\n#include <numeric>\n#include <queue>\n#include <set>\n#include <sstream>\n#include <stack>\n#include <string>\n#include <utility>\n#include <vector>\nusing namespace std;\n#ifdef _MSC_VER\n#define __typeof__ decltype\ntemplate <class T> int __builtin_popcount(T n) {\n return n ? 1 + __builtin_popcount(n & (n - 1)) : 0;\n}\n#endif\n#define foreach(it, c) \\\n for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it)\n#define all(c) (c).begin(), (c).end()\n#define rall(c) (c).rbegin(), (c).rend()\n#define CLEAR(arr, val) memset(arr, val, sizeof(arr))\n#define rep(i, n) for (int i = 0; i < n; ++i)\ntemplate <class T> void max_swap(T &a, const T &b) { a = max(a, b); }\ntemplate <class T> void min_swap(T &a, const T &b) { a = min(a, b); }\ntypedef long long ll;\ntypedef pair<int, int> pint;\nconst double PI = acos(-1.0);\nconst int dx[] = {0, 1, 0, -1};\nconst int dy[] = {1, 0, -1, 0};\nint n, m;\nint dpru(int *c, int *v) {\n int dp[333][333];\n CLEAR(dp, 0);\n for (int i = 0; i < n; ++i) {\n for (int j = 0; j <= m; ++j) {\n dp[i + 1][j] = dp[i][j];\n if (j >= c[i])\n max_swap(dp[i + 1][j], dp[i + 1][j - c[i]] + v[i]);\n }\n }\n return dp[n][m];\n}\nint main() {\n while (~scanf(\"%d%d\", &n, &m)) {\n int c[333], v[3][333];\n rep(i, n) {\n getchar();\n while (getchar() != '\\n')\n ;\n int t;\n scanf(\"%d\", &t);\n c[i] = t;\n rep(j, 3) scanf(\"%d\", v[j] + i);\n }\n int res = 0;\n rep(i, 3) max_swap(res, dpru(c, v[i]));\n printf(\"%d\\n\", res);\n }\n}", "accuracy": 1, "time_ms": 110, "memory_kb": 3548, "score_of_the_acc": -0.5822, "final_rank": 6 }, { "submission_id": "aoj_2219_8216191", "code_snippet": "#include <algorithm>\n#include <cfloat>\n#include <climits>\n#include <cmath>\n#include <complex>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <ctime>\n#include <functional>\n#include <iostream>\n#include <list>\n#include <map>\n#include <memory>\n#include <numeric>\n#include <queue>\n#include <set>\n#include <sstream>\n#include <stack>\n#include <string>\n#include <utility>\n#include <vector>\nusing namespace std;\n#ifdef _MSC_VER\n#define __typeof__ decltype\ntemplate <class T> int __builtin_popcount(T n) {\n return n ? 1 + __builtin_popcount(n & (n - 1)) : 0;\n}\n#endif\n#define foreach(it, c) \\\n for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it)\n#define all(c) (c).begin(), (c).end()\n#define rall(c) (c).rbegin(), (c).rend()\n#define CLEAR(arr, val) memset(arr, val, sizeof(arr))\n#define rep(i, n) for (int i = 0; i < n; ++i)\ntemplate <class T> void max_swap(T &a, const T &b) { a = max(a, b); }\ntemplate <class T> void min_swap(T &a, const T &b) { a = min(a, b); }\ntypedef long long ll;\ntypedef pair<int, int> pint;\nconst double PI = acos(-1.0);\nconst int dx[] = {0, 1, 0, -1};\nconst int dy[] = {1, 0, -1, 0};\nint n, m;\nint dpru(int *c, int *v) {\n int dp[333][333];\n CLEAR(dp, 0);\n for (int i = 0; i < n; ++i) {\n for (int j = 0; j <= m; ++j) {\n dp[i + 1][j] = dp[i][j];\n if (j >= c[i])\n max_swap(dp[i + 1][j], dp[i + 1][j - c[i]] + v[i]);\n }\n }\n return dp[n][m];\n}\nint main() {\n while (cin >> n >> m) {\n int c[333], v[3][333];\n rep(i, n) {\n string s;\n int t;\n getline(cin, s);\n getline(cin, s);\n cin >> t;\n c[i] = t;\n rep(j, 3) cin >> v[j][i];\n }\n int res = 0;\n rep(i, 3) max_swap(res, dpru(c, v[i]));\n cout << res << endl;\n }\n}", "accuracy": 1, "time_ms": 150, "memory_kb": 3632, "score_of_the_acc": -0.7477, "final_rank": 10 }, { "submission_id": "aoj_2219_8138283", "code_snippet": "#pragma GCC optimize(\"Ofast\")\n#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n\nmt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());\nll myRand(ll B) {\n return (ull)rng() % B;\n}\ninline double time() {\n return static_cast<long double>(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count()) * 1e-9;\n}\n\nint main(){\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n int n,m;\n while (cin >> n >> m) {\n cin.ignore();\n string s;\n vector<int> c(n);\n vector<vector<int>> v(3, vector<int>(n));\n for (int i = 0; i < n; i++) {\n getline(cin, s);\n cin >> c[i] >> v[0][i] >> v[1][i] >> v[2][i];\n cin.ignore();\n }\n int res = 0;\n for (int _ = 0; _ < 3; _++) {\n auto u = v[_];\n vector<int> dp(m+1, 0);\n for (int i = 0; i < n; i++) {\n for (int j = 0; j+c[i] <= m; j++) {\n dp[j+c[i]] = max(dp[j+c[i]], dp[j]+u[i]);\n }\n }\n res = max(res, dp.back());\n }\n cout << res << endl;\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3480, "score_of_the_acc": -0.237, "final_rank": 3 }, { "submission_id": "aoj_2219_8116367", "code_snippet": "#define _USE_MATH_DEFINES\n#define INF 0x3f3f3f3f\n#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntypedef pair<int, int> P;\ntypedef pair<int, P> PP;\nint tx[] = {0, 1, 0, -1};\nint ty[] = {-1, 0, 1, 0};\nstatic const double EPS = 1e-8;\nclass Data {\npublic:\n string mName;\n int mC, mV, mD, mL;\n Data(string _n, int _c, int _v, int _d, int _l) {\n mName = _n;\n mC = _c;\n mV = _v;\n mD = _d;\n mL = _l;\n }\n};\nconst int MAX_N = 300;\nconst int MAX_M = 300;\nint dp[MAX_N + 1][MAX_M + 1][3];\nint main() {\n int N, M;\n string line;\n stringstream ss;\n while (getline(cin, line)) {\n ss << line;\n ss >> N >> M;\n ss.clear();\n vector<Data> idols;\n for (int i = 0; i < N; i++) {\n getline(cin, line);\n string name = line;\n getline(cin, line);\n ss << line;\n int C, V, D, L;\n ss >> C >> V >> D >> L;\n ss.clear();\n idols.push_back(Data(name, C, V, D, L));\n }\n memset(dp, 0, sizeof(dp));\n for (int i = 0; i < N; i++) {\n for (int j = 0; j <= M; j++) {\n for (int k = 0; k <= MAX_N; k++) {\n if (j - idols[i].mC * k < 0)\n break;\n dp[i + 1][j][0] =\n max(dp[i][j - idols[i].mC * k][0] + idols[i].mV * k,\n dp[i + 1][j][0]);\n dp[i + 1][j][1] =\n max(dp[i][j - idols[i].mC * k][1] + idols[i].mD * k,\n dp[i + 1][j][1]);\n dp[i + 1][j][2] =\n max(dp[i][j - idols[i].mC * k][2] + idols[i].mL * k,\n dp[i + 1][j][2]);\n }\n }\n }\n int maxv = 0;\n for (int i = 0; i <= M; i++) {\n for (int j = 0; j < 3; j++) {\n maxv = max(dp[N][i][j], maxv);\n }\n }\n printf(\"%d\\n\", maxv);\n }\n}", "accuracy": 1, "time_ms": 330, "memory_kb": 5064, "score_of_the_acc": -2, "final_rank": 20 }, { "submission_id": "aoj_2219_8116359", "code_snippet": "#define _USE_MATH_DEFINES\n#define INF 0x3f3f3f3f\n#include <algorithm>\n#include <bitset>\n#include <cctype>\n#include <cmath>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <deque>\n#include <iostream>\n#include <limits>\n#include <list>\n#include <map>\n#include <queue>\n#include <set>\n#include <sstream>\n#include <stack>\n#include <string>\n#include <utility>\nusing namespace std;\ntypedef long long ll;\ntypedef pair<int, int> P;\ntypedef pair<int, P> PP;\nint tx[] = {0, 1, 0, -1};\nint ty[] = {-1, 0, 1, 0};\nstatic const double EPS = 1e-8;\nclass Data {\npublic:\n string mName;\n int mC, mV, mD, mL;\n Data(string _n, int _c, int _v, int _d, int _l) {\n mName = _n;\n mC = _c;\n mV = _v;\n mD = _d;\n mL = _l;\n }\n};\nint dp[301][3];\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n int N, M;\n string line;\n while (getline(cin, line)) {\n stringstream ss(line);\n ss >> N >> M;\n vector<Data> idols;\n for (int i = 0; i < N; i++) {\n getline(cin, line);\n string name = line;\n getline(cin, line);\n stringstream ss2(line);\n int C, V, D, L;\n ss2 >> C >> V >> D >> L;\n idols.push_back(Data(name, C, V, D, L));\n }\n memset(dp, 0, sizeof(dp));\n for (int i = 0; i <= M; i++) {\n for (int j = 0; j < idols.size(); j++) {\n for (int k = 1; k <= 300; k++) {\n if (i - idols[j].mC * k < 0)\n break;\n dp[i][0] =\n max(dp[i - idols[j].mC * k][0] + idols[j].mV * k, dp[i][0]);\n dp[i][1] =\n max(dp[i - idols[j].mC * k][1] + idols[j].mD * k, dp[i][1]);\n dp[i][2] =\n max(dp[i - idols[j].mC * k][2] + idols[j].mL * k, dp[i][2]);\n }\n }\n }\n int maxv = 0;\n for (int i = 0; i <= M; i++) {\n for (int j = 0; j < 3; j++) {\n maxv = max(dp[i][j], maxv);\n }\n }\n printf(\"%d\\n\", maxv);\n }\n}", "accuracy": 1, "time_ms": 240, "memory_kb": 3304, "score_of_the_acc": -0.871, "final_rank": 13 }, { "submission_id": "aoj_2219_8116353", "code_snippet": "#include <algorithm>\n#include <cfloat>\n#include <climits>\n#include <cmath>\n#include <complex>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <ctime>\n#include <functional>\n#include <iostream>\n#include <list>\n#include <map>\n#include <memory>\n#include <numeric>\n#include <queue>\n#include <set>\n#include <sstream>\n#include <stack>\n#include <string>\n#include <utility>\n#include <vector>\nusing namespace std;\n#ifdef _MSC_VER\n#define __typeof__ decltype\ntemplate <class T> int __builtin_popcount(T n) {\n return n ? 1 + __builtin_popcount(n & (n - 1)) : 0;\n}\n#endif\n#define foreach(it, c) \\\n for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it)\n#define all(c) (c).begin(), (c).end()\n#define rall(c) (c).rbegin(), (c).rend()\n#define CLEAR(arr, val) memset(arr, val, sizeof(arr))\n#define rep(i, n) for (int i = 0; i < n; ++i)\ntemplate <class T> void max_swap(T &a, const T &b) { a = max(a, b); }\ntemplate <class T> void min_swap(T &a, const T &b) { a = min(a, b); }\ntypedef long long ll;\ntypedef pair<int, int> pint;\nconst double PI = acos(-1.0);\nconst int dx[] = {0, 1, 0, -1};\nconst int dy[] = {1, 0, -1, 0};\nconst int MAXN = 333;\nint n, m;\nint c[MAXN], v[3][MAXN];\nint dp[2][MAXN];\nint dpru(int *c, int *v) {\n CLEAR(dp, 0);\n for (int i = 0; i < n; ++i) {\n for (int j = 0; j <= m; ++j) {\n dp[1][j] = dp[0][j];\n if (j >= c[i])\n max_swap(dp[1][j], dp[1][j - c[i]] + v[i]);\n }\n swap(dp[0], dp[1]);\n }\n return dp[0][m];\n}\nint main() {\n while (cin >> n >> m) {\n rep(i, n) {\n string s;\n int t;\n getline(cin, s);\n getline(cin, s);\n cin >> t;\n c[i] = t;\n rep(j, 3) cin >> v[j][i];\n }\n int res = 0;\n rep(i, 3) max_swap(res, dpru(c, v[i]));\n cout << res << endl;\n }\n}", "accuracy": 1, "time_ms": 120, "memory_kb": 3180, "score_of_the_acc": -0.4362, "final_rank": 5 }, { "submission_id": "aoj_2219_8116344", "code_snippet": "#include <algorithm>\n#include <cstring>\n#include <iostream>\nusing namespace std;\n\n#define rep(i, n) for (int i = 0; i < n; ++i)\n\nconst int MAXN = 333;\n\nint n, m;\nint c[MAXN], v[3][MAXN];\nint dp[2][MAXN];\n\nvoid max_swap(int &a, const int &b) { a = max(a, b); }\n\nint dpru(int *c, int *v) {\n memset(dp, 0, sizeof(dp));\n int now = 0, pre = 1;\n for (int i = 0; i < n; ++i) {\n swap(now, pre);\n for (int j = 0; j <= m; ++j) {\n dp[now][j] = dp[pre][j];\n if (j >= c[i])\n max_swap(dp[now][j], dp[now][j - c[i]] + v[i]);\n }\n }\n return dp[now][m];\n}\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(0);\n while (cin >> n >> m) {\n rep(i, n) {\n string s;\n int t;\n getline(cin, s);\n getline(cin, s);\n cin >> t;\n c[i] = t;\n rep(j, 3) cin >> v[j][i];\n }\n int res = 0;\n rep(i, 3) max_swap(res, dpru(c, v[i]));\n cout << res << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3224, "score_of_the_acc": -0.1762, "final_rank": 1 }, { "submission_id": "aoj_2219_8113312", "code_snippet": "#include <algorithm>\n#include <cfloat>\n#include <climits>\n#include <cmath>\n#include <complex>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <ctime>\n#include <functional>\n#include <iostream>\n#include <list>\n#include <map>\n#include <memory>\n#include <numeric>\n#include <queue>\n#include <set>\n#include <sstream>\n#include <stack>\n#include <string>\n#include <utility>\n#include <vector>\nusing namespace std;\n#ifdef _MSC_VER\n#define __typeof__ decltype\ntemplate <class T> int __builtin_popcount(T n) {\n return n ? 1 + __builtin_popcount(n & (n - 1)) : 0;\n}\n#endif\n#define foreach(it, c) \\\n for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it)\n#define all(c) (c).begin(), (c).end()\n#define rall(c) (c).rbegin(), (c).rend()\n#define CLEAR(arr, val) memset(arr, val, sizeof(arr))\n#define rep(i, n) for (int i = 0; i < n; ++i)\ntemplate <class T> void max_swap(T &a, const T &b) { a = max(a, b); }\ntemplate <class T> void min_swap(T &a, const T &b) { a = min(a, b); }\ntypedef long long ll;\ntypedef pair<int, int> pint;\nconst double PI = acos(-1.0);\nconst int dx[] = {0, 1, 0, -1};\nconst int dy[] = {1, 0, -1, 0};\nint n, m;\nint dpru(int *c, int *v) {\n int dp[333][333];\n CLEAR(dp, 0);\n for (int i = 0; i < n; ++i) {\n for (int j = 0; j <= m; ++j) {\n dp[i + 1][j] = dp[i][j];\n if (j >= c[i])\n max_swap(dp[i + 1][j], dp[i + 1][j - c[i]] + v[i]);\n }\n }\n return dp[n][m];\n}\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(0);\n while (cin >> n >> m) {\n int c[333], v[3][333];\n rep(i, n) {\n string s;\n int t;\n getline(cin, s);\n getline(cin, s);\n cin >> t;\n c[i] = t;\n rep(j, 3) cin >> v[j][i];\n }\n int res = 0;\n rep(i, 3) max_swap(res, dpru(c, v[i]));\n cout << res << endl;\n }\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 3636, "score_of_the_acc": -0.5934, "final_rank": 7 }, { "submission_id": "aoj_2219_8113311", "code_snippet": "#pragma GCC optimize(\"O3\")\n#pragma GCC target(\"sse4\")\n#include <algorithm>\n#include <cfloat>\n#include <climits>\n#include <cmath>\n#include <complex>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <ctime>\n#include <functional>\n#include <iostream>\n#include <list>\n#include <map>\n#include <memory>\n#include <numeric>\n#include <queue>\n#include <set>\n#include <sstream>\n#include <stack>\n#include <string>\n#include <utility>\n#include <vector>\nusing namespace std;\n#ifdef _MSC_VER\n#define __typeof__ decltype\ntemplate <class T> int __builtin_popcount(T n) {\n return n ? 1 + __builtin_popcount(n & (n - 1)) : 0;\n}\n#endif\n#define foreach(it, c) \\\n for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it)\n#define all(c) (c).begin(), (c).end()\n#define rall(c) (c).rbegin(), (c).rend()\n#define CLEAR(arr, val) memset(arr, val, sizeof(arr))\n#define rep(i, n) for (int i = 0; i < n; ++i)\ntemplate <class T> void max_swap(T &a, const T &b) { a = max(a, b); }\ntemplate <class T> void min_swap(T &a, const T &b) { a = min(a, b); }\ntypedef long long ll;\ntypedef pair<int, int> pint;\nconst double PI = acos(-1.0);\nconst int dx[] = {0, 1, 0, -1};\nconst int dy[] = {1, 0, -1, 0};\nint n, m;\nint dpru(int *c, int *v) {\n int dp[333][333];\n CLEAR(dp, 0);\n for (int i = 0; i < n; ++i) {\n for (int j = 0; j <= m; ++j) {\n dp[i + 1][j] = dp[i][j];\n if (j >= c[i])\n max_swap(dp[i + 1][j], dp[i + 1][j - c[i]] + v[i]);\n }\n }\n return dp[n][m];\n}\nint main() {\n while (cin >> n >> m) {\n int c[333], v[3][333];\n rep(i, n) {\n string s;\n int t;\n getline(cin, s);\n getline(cin, s);\n cin >> t;\n c[i] = t;\n rep(j, 3) cin >> v[j][i];\n }\n int res = 0;\n rep(i, 3) max_swap(res, dpru(c, v[i]));\n cout << res << endl;\n }\n}", "accuracy": 1, "time_ms": 160, "memory_kb": 3596, "score_of_the_acc": -0.7616, "final_rank": 12 } ]
aoj_2218_cpp
Problem C: K Poker Description 時は200X年、K大学で活動する謎のサークルKはその大学の文化祭の最中にK東4Fで彼らが作成した新たなゲーム、Kポーカーを発表した。 このゲームは一見すると普通のポーカーであるが、カードに基本点という要素をつけてポーカーをより奥の深いゲームにしたものである。 カードの基本点はそれぞれのカードに書かれている絵柄によって決まり、手札の点数は手札にある5枚の基本点の合計に手札の役の倍率を掛けたものになる。 この基本点を導入した事により、例えばフルハウスなどの強い役を作ったとしても基本点が0であればブタと同様に扱われてしまい、ワンペアに負けることさえある。 それ以外のルールは通常のポーカーと同じである。 このKポーカーをPCに移植しようと思ったあなたは手札の点数を計算するプログラムを書く事にした。 なおこのゲームは18歳未満の人はプレイ出来ない。 Input 入力は複数のテストケースからなる。 各テストケースの最初の行にはチェックする手札の数Nが書かれている。 次の4行には13個の整数が並びカードの基本点が1-13の順に並んで書かれている。4行のスーツは上から順にスペード、クローバー、ハート、ダイヤの順に並んでいる。 次の行には9個の整数が並び、順にワンペア、ツーペア、スリーカード、ストレート、フラッシュ、フルハウス、フォーカード、ストレートフラッシュ、ロイヤルストレートフラッシュの倍率を表している。役の倍率は必ず昇順となっている。ブタ(役無し、ノーペア)の倍率は常に0である。 次のN行には手札を表す5個の長さ2の異なる文字列が並んでいる。1文字目はカードの数値を表し,A,2,3,4,5,6,7,8,9,T,J,Q,Kのいずれかである。2文字目はカードのスーツを表し、S,C,H,Dのいずれかである。各アルファベットはAはエース、Tは10、Jはジャック、Qはクイーン、Kはキング、Sはスペード、Cはクローバー、Hはハート、Dはダイヤを表す。 入力の数値は全て[0,10000]の範囲に収まっている。 入力はEOFで終わる。 Output 各手札の点数を1行ずつ出力せよ。 連続する2つのテストケースの間には空行を1つ入れること。 役については wikipediaのポーカー のページを参照のこと。 なお、ストレートはエースとキングをまたぐ場合があるが、手札が10,ジャック、クイーン、キング、エースの場合にのみストレートとみなされる。 Sample Input 3 0 1 0 1 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 1 0 0 1 0 0 2 0 1 1 0 1 1 1 0 0 0 0 5 5 3 1 1 1 0 1 0 0 1 0 3 0 2 1 1 2 4 5 10 20 50 100 7H 6H 2H 5H 3H 9S 9C 9H 8H 8D KS KH QH JD TS 10 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 3 4 5 6 7 8 9 AS 3D 2C 5D 6H 6S 6C 7D 8H 9C TS TD QC JD JC KD KH KC AD 2C 3D 4H 6D 5H 7C 2S KS QS AS JS TS TD JD JC TH 8H 8D TC 8C 8S 4S 5S 3S 7S 6S KD QD JD TD AD Output for Sample Input 25 0 14 0 5 10 15 20 25 30 35 40 45
[ { "submission_id": "aoj_2218_8139138", "code_snippet": "#pragma GCC optimize(\"Ofast\")\n#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n\nmt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());\nll myRand(ll B) {\n return (ull)rng() % B;\n}\ninline double time() {\n return static_cast<long double>(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count()) * 1e-9;\n}\n\nint main(){\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n int n;\n int fc = 0;\n while (cin >> n) {\n if (fc++) cout << endl;\n vector<vector<int>> p(4, vector<int>(14));\n for (int i = 0; i < 4; i++) {\n for (int j = 1; j <= 13; j++) {\n cin >> p[i][j];\n }\n }\n vector<int> s(10);\n for (int i = 1; i < 10; i++) {\n cin >> s[i];\n }\n auto is_flush = [](vector<pair<int,int>> v) -> bool {\n for (int i = 1; i < 5; i++) {\n if (v[i].second != v[0].second) return false;\n }\n return true;\n };\n auto is_straight = [](vector<pair<int,int>> v) -> bool {\n sort(v.begin(), v.end());\n for (int i = 1 + (v[0].first == 1 and v[1].first == 10); i < 5; i++) {\n if (v[i].first != v[i-1].first + 1) return false;\n }\n return true;\n };\n auto cal = [&](vector<pair<int,int>> v) -> int {\n bool ff = is_flush(v);\n bool ss = is_straight(v);\n sort(v.begin(), v.end());\n if (ff and ss) {\n if (v[0].first == 1 and v[1].first == 10) return 9;\n else return 8;\n }\n if (v[0].first == v[3].first or v[1].first == v[4].first) {\n return 7;\n }\n if (v[0].first == v[2].first and v[3].first == v[4].first) {\n return 6;\n }\n if (v[0].first == v[1].first and v[2].first == v[4].first) {\n return 6;\n }\n if (ff) {\n return 5;\n }\n if (ss) {\n return 4;\n }\n {\n for (int i = 0; i < 3; i++) {\n if (v[i].first == v[i+2].first) return 3;\n }\n }\n {\n int cnt = 0;\n for (int i = 0; i < 4; i++) {\n if (v[i].first == v[i+1].first) cnt += 1;\n }\n return cnt;\n }\n };\n for (int i = 0; i < n; i++) {\n int a = 0, b = 0;\n vector<pair<int,int>> v(5);\n for (int j = 0; j < 5; j++) {\n char c,cc; cin >> c >> cc;\n if (cc == 'S') v[j].second = 0;\n else if (cc == 'C') v[j].second = 1;\n else if (cc == 'H') v[j].second = 2;\n else v[j].second = 3;\n if (isdigit(c)) {\n v[j].first = c - '0';\n }\n else {\n if (c == 'A') v[j].first = 1;\n else if (c == 'T') v[j].first = 10;\n else if (c == 'J') v[j].first = 11;\n else if (c == 'Q') v[j].first = 12;\n else v[j].first = 13;\n }\n b += p[v[j].second][v[j].first];\n }\n a = cal(v);\n //cout << a << \" \" << b << endl;\n cout << s[a]*b << endl;\n }\n }\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 3296, "score_of_the_acc": -1.15, "final_rank": 11 }, { "submission_id": "aoj_2218_8138816", "code_snippet": "#include <cstdio>\n#include <cstdint>\n#include <vector>\n#include <algorithm>\n#include <string>\n#include <set>\n#include <map>\n\nbool is_royal(const std::vector<std::pair<int, char>> &cards) {\n std::set<int> s;\n for (const auto &p: cards) {\n s.insert(p.first);\n }\n return s.count(10) && s.count(11) && s.count(12) && s.count(13) && s.count(1);\n}\n\nbool is_flush(const std::vector<std::pair<int, char>> &cards) {\n std::set<char> s;\n for (const auto &p: cards) {\n s.insert(p.second);\n }\n return (s.size() == 1);\n}\n\nbool is_straight(const std::vector<std::pair<int, char>> &cards) {\n std::set<int> s;\n std::vector<int> t;\n for (const auto &p: cards) {\n s.insert(p.first);\n t.push_back(p.first);\n }\n if (s.size() != 5) return false;\n std::sort(t.begin(), t.end());\n return (t[4]-t[0] == 4);\n}\n\nstd::vector<int> multi(const std::vector<std::pair<int, char>> &cards) {\n std::map<int, int> s;\n for (const auto &p: cards) {\n ++s[p.first];\n }\n std::vector<int> res;\n for (const auto &p: s) {\n if (p.second >= 2) {\n res.emplace_back(p.second);\n }\n }\n return res;\n}\n\nint testcase_ends(bool first) {\n int N;\n if (scanf(\"%d\", &N) == EOF)\n return 1;\n\n if (!first)\n printf(\"\\n\");\n\n std::map<char, std::vector<int>> score;\n for (char ch: {'S', 'C', 'H', 'D'}) {\n score[ch].emplace_back(0);\n for (int i=1; i<=13; ++i) {\n int x;\n scanf(\"%d\", &x);\n score[ch].emplace_back(x);\n }\n }\n\n std::vector<int> hands{0};\n for (int i=0; i<9; ++i) {\n int x;\n scanf(\"%d\", &x);\n hands.emplace_back(x);\n }\n\n for (int i=0; i<N; ++i) {\n std::vector<std::pair<int, char>> cards;\n int base=0;\n for (int j=0; j<5; ++j) {\n char n, s;\n scanf(\" %c%c\", &n, &s);\n int num=0;\n switch (n) {\n case 'A':\n num = 1;\n break;\n case 'T':\n num = 10;\n break;\n case 'J':\n num = 11;\n break;\n case 'Q':\n num = 12;\n break;\n case 'K':\n num = 13;\n break;\n default:\n num = n-'0';\n }\n\n cards.emplace_back(num, s);\n base += score[s][num];\n }\n\n bool royal=is_royal(cards);\n bool straight=is_straight(cards);\n bool flush=is_flush(cards);\n std::vector<int> mul=multi(cards);\n std::sort(mul.begin(), mul.end());\n\n intmax_t res=base;\n if (royal && flush) {\n res *= hands[9];\n } else if (straight && flush) {\n res *= hands[8];\n } else if (mul.size() == 1 && mul[0] == 4) {\n res *= hands[7];\n } else if (mul.size() == 2 && mul[0] == 2 && mul[1] == 3) {\n res *= hands[6];\n } else if (flush) {\n res *= hands[5];\n } else if (straight || royal) {\n res *= hands[4];\n } else if (mul.size() == 1 && mul[0] == 3) {\n res *= hands[3];\n } else if (mul.size() == 2 && mul[0] == 2 && mul[1] == 2) {\n res *= hands[2];\n } else if (mul.size() == 1 && mul[0] == 2) {\n res *= hands[1];\n } else {\n res = 0;\n }\n printf(\"%jd\\n\", res);\n }\n return 0;\n}\n\nint main() {\n for (int i=0; true; ++i) {\n if (testcase_ends(i == 0)) return 0;\n }\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 2804, "score_of_the_acc": -0.9823, "final_rank": 9 }, { "submission_id": "aoj_2218_2959366", "code_snippet": "#include<iostream>\n#include<string>\n#include<cstring>\n#include<vector>\n#include<set>\n#include<cmath>\n#include<algorithm>\nusing namespace std;\ntypedef vector<int> vi;\n\nint main()\n{\n int buckets[14];\n int buckets2[5];\n bool firsttime=true;\n int n;\n while(cin >> n)\n {\n if(!n) break;\n if(!firsttime) cout << endl;\n if(firsttime) firsttime=false;\n int cardscore[14][5];\n int handscore[9];\n for(int j=1;j<=4;j++)\n {\n for(int k=1;k<=13;k++)\n {\n cin >> cardscore[k][j];\n }\n }\n for(int j=0;j<9;j++)\n {\n cin >> handscore[j];\n }\n \n for(int i=0;i<n;i++)\n {\n int times=0;\n long long score=0;\n vector<string> card(5);\n vector<pair<int,int> > tmpc(5);\n for(int j=0;j<5;j++)\n {\n cin >> card[j];\n if(card[j][0]=='A')\n {\n tmpc[j].first=1;\n }\n else if(card[j][0]=='T')\n {\n tmpc[j].first=10;\n }\n else if(card[j][0]=='J')\n {\n tmpc[j].first=11;\n }\n else if(card[j][0]=='Q')\n {\n tmpc[j].first=12;\n }\n else if(card[j][0]=='K')\n {\n tmpc[j].first=13;\n }\n else\n {\n tmpc[j].first=(int)(card[j][0]-'0');\n }\n if(card[j][1]=='S')\n {\n tmpc[j].second=1;\n }\n else if(card[j][1]=='C')\n {\n tmpc[j].second=2;\n }\n else if(card[j][1]=='H')\n {\n tmpc[j].second=3;\n }\n else if(card[j][1]=='D')\n {\n tmpc[j].second=4;\n }\n score+=cardscore[tmpc[j].first][tmpc[j].second];\n\n }\n int onep=0,threec=0,four=0;\n bool flash=false;\n bool straight=true;\n sort(tmpc.begin(),tmpc.end());\n memset(buckets,0,sizeof(buckets));\n memset(buckets2,0,sizeof(buckets2));\n for(int j=0;j<5;j++)\n {\n buckets[tmpc[j].first]++;\n buckets2[tmpc[j].second]++;\n }\n for(int j=1;j<=13;j++)\n {\n if(buckets[j]==2)\n {\n onep++;\n }\n else if(buckets[j]==3)\n {\n threec++;\n }\n else if(buckets[j]==4)\n {\n four++;\n }\n }\n for(int j=1;j<=4;j++)\n {\n if(buckets2[j]==5)\n {\n flash=true;\n times=handscore[4];\n }\n }\n if(onep==1 && threec==0)\n {\n times=handscore[0];\n }\n else if(onep==2)\n {\n times=handscore[1];\n }\n else if(threec==1 && onep==0)\n {\n times=handscore[2];\n }\n else if(threec==1 && onep==1)\n {\n times=handscore[5];\n }\n else if(four==1)\n {\n times=handscore[6];\n }\n if(tmpc[0].first==1 && tmpc[1].first==10 && tmpc[2].first==11 && tmpc[3].first==12 && tmpc[4].first==13)\n {\n if(flash) times=handscore[8];\n else times=handscore[3];\n }\n else\n {\n for(int j=0;j<4;j++)\n {\n if(tmpc[j+1].first!=(tmpc[j].first+1)) straight=false;\n }\n if(straight)\n {\n if(flash) times=handscore[7];\n else times=handscore[3];\n }\n }\n cout << score*times << endl;\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 130, "memory_kb": 3156, "score_of_the_acc": -1.4881, "final_rank": 18 }, { "submission_id": "aoj_2218_2954833", "code_snippet": "#include <cstdio>\n#include <cstdlib>\n#include <cmath>\n#include <cstring>\n#include <climits>\n#include <vector>\n#include <map>\n#include <set>\n#include <list>\n#include <stack>\n#include <queue>\n#include <algorithm>\n#include <iostream>\n#include <string>\n\n#define REP(i,n) for(int i=0;i<n;++i)\n#define REPR(i,n) for(int i=n;i>=0;--i)\n#define REPI(itr,v) for(auto itr=v.begin();itr!=v.end();++itr)\n#define REPIR(itr,v) for(auto itr=v.rbegin();itr!=v.rend();++itr)\n#define FOR(i,a,b) for(int i=a;i<b;++i)\n#define SORT(v,n) sort(v, v+n)\n#define SORTV(v) sort(v.begin(), v.end())\n#define ALL(v) v.begin(),v.end()\n#define llong long long\n#define INF 999999999\n#define SUR 1000000007\n#define pb push_back\n#define pf push_front\n#define MP make_pair\n\nint dx[] = {0, 0, -1, 1};\nint dy[] = {1, -1, 0, 0};\n\nusing namespace std;\n\n//suit S:0 C:1 H:2\nint conNum(char c){\n if(c == 'K') return 13;\n else if(c == 'Q') return 12;\n else if(c == 'J') return 11;\n else if(c == 'T') return 10;\n else if(c == 'A') return 1;\n else{\n return c - '0';\n }\n}\n\nint suitTonum(char c){\n if(c == 'S') return 0;\n else if(c == 'C') return 1;\n else if(c == 'H') return 2;\n else return 3;\n}\n\nstruct hand{\n int num[5];\n int suit[5];\n};\n\nint main(){\n\n int n;\n bool hoge = false;\n while(cin >> n){\n if(hoge) cout << \"\\n\";\n hoge = true;\n llong card[4][13] = {};\n REP(i,4){\n REP(j,13){\n cin >> card[i][j];\n }\n }\n llong role[9];\n REP(i,9){\n cin >> role[8 - i];\n }\n\n vector<hand> v(n);\n REP(i,n){\n REP(j,5){\n string tmp;\n cin >> tmp;\n v[i].num[j] = conNum(tmp[0]) - 1;\n v[i].suit[j] = suitTonum(tmp[1]);\n\n //cout << \"#i\" << i << \" \" << v[i].num[i] << \" \" << v[i].suit[i] << \"\\n\";\n }\n }\n\n vector<llong> base(n);\n REP(i,n){\n base[i] = 0;\n REP(j,5){\n int a = v[i].num[j], b = v[i].suit[j];\n base[i] += card[b][a];\n }\n sort(v[i].num, v[i].num + 5);\n }\n REP(i,n){\n\n //flush\n bool end = false;\n bool isFl = true;\n REP(j,4){\n if(v[i].suit[j+1] != v[i].suit[0]){\n isFl = false;\n break;\n }\n }\n //st\n bool isSt = true;\n int last = v[i].num[0];\n REP(j,4){\n if(v[i].num[j+1] != last + 1){\n isSt = false;\n break;\n }\n last = v[i].num[j+1];\n }\n \n bool isRoyal = false;\n if(v[i].num[0] == 0 && v[i].num[1] == 9 && v[i].num[2] == 10 && v[i].num[3] == 11 && v[i].num[4] == 12){\n isSt = true;\n if(isFl){\n cout << base[i] * role[0] << \"\\n\";\n isRoyal = true;\n }\n }\n if(isRoyal) continue;\n if(isFl && isSt){\n cout << base[i] * role[1] << \"\\n\";\n continue;\n }\n\n map<int, int> mp;\n REP(j,5){\n mp[v[i].num[j]]++;\n }\n //four\n REPI(itr,mp){\n if(itr->second == 4){\n cout << base[i] * role[2] << \"\\n\";\n end = true;\n break;\n }\n }\n if(end) continue;\n \n //full\n int th = 0, tw = 0;\n REPI(itr,mp){\n if(itr->second == 3){\n th++;\n }else if(itr->second == 2){\n tw++;\n }\n }\n if(th > 0 && tw){\n cout << base[i] * role[3] << \"\\n\";\n continue;\n }\n\n if(isFl){\n cout << base[i] * role[4] << \"\\n\";\n continue;\n }\n if(isSt){\n cout << base[i] * role[5] << \"\\n\";\n continue;\n }\n if(th > 0){\n cout << base[i] * role[6] << \"\\n\";\n continue;\n }\n if(tw == 2){\n cout << base[i] * role[7] << \"\\n\";\n continue;\n }\n if(tw == 1){\n cout << base[i] * role[8] << \"\\n\";\n continue;\n }\n cout << \"0\\n\";\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 3160, "score_of_the_acc": -1.2398, "final_rank": 12 }, { "submission_id": "aoj_2218_2954804", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <cstring>\nusing namespace std;\nint nton[256];\nint ston[256];\nint point[4][13];\nint rate[9];\n\nint calc(vector<pair<int, int> > &a){\n\tint s[5], n[5];\n\tfor(int i=0; i<5; i++){\n\t\ts[i] = a[i].second;\n\t\tn[i] = a[i].first;\n\t}\n\tbool samesuit = false;\n\tif(s[0]==s[1] && s[0]==s[2] && s[0]==s[3] && s[0]==s[4]){\n\t\tsamesuit = true;\n\t}\n\tbool rstraight = false;\n\tbool straight = false;\n\tif(0 <= n[0] && n[0] <= 8){\n\t\tbool succ = true;\n\t\tfor(int i=0; i<4; i++){\n\t\t\tif((n[i]+1)%13 != n[i+1]%13) succ = false;\n\t\t}\n\t\tif(succ){\n\t\t\tstraight = true;\n\t\t}\n\t}\n\tif(n[0]==0 && n[1]==9 && n[2]==10 && n[3]==11 && n[4]==12) rstraight = true;\n\tif(samesuit && rstraight) return rate[8];\n\tif(samesuit && straight) return rate[7];\n\tif(straight || rstraight) return rate[3];\n\tif(samesuit) return rate[4];\n\t\n\tint pair1=0, pair2=0;\n\tfor(int i=0; i<5; i++){\n\t\tint lim = 1;\n\t\twhile(i+lim<5 && n[i]==n[i+lim]) lim++;\n\t\tif(lim>1){\n\t\t\tif(pair1!=0){\n\t\t\t\tpair2 = lim;\n\t\t\t}else{\n\t\t\t\tpair1 = lim;\n\t\t\t}\n\t\t\ti += lim-1;\n\t\t}\n\t}\n\tif(pair1 == 4) return rate[6];\n\tif(pair1 == 2 && pair2 == 3) return rate[5];\n\tif(pair1 == 3 && pair2 == 2) return rate[5];\n\tif(pair1 == 3) return rate[2];\n\tif(pair2 == 2) return rate[1];\n\tif(pair1 == 2) return rate[0];\n\treturn 0;\n}\n\nint main(){\n\tfor(int i=0; i<256; i++){\n\t\tnton[i] = i-'1';\n\t}\n\tnton['A'] = 0;\n\tnton['T'] = 9;\n\tnton['J'] = 10;\n\tnton['Q'] = 11;\n\tnton['K'] = 12;\n\tston['S'] = 0;\n\tston['C'] = 1;\n\tston['H'] = 2;\n\tston['D'] = 3;\n\t\n\tbool first = true;\n\tint n;\n\twhile(cin >> n){\n\t\tif(!first){\n\t\t\tcout << endl;\n\t\t}else{\n\t\t\tfirst = false;\n\t\t}\n\t\t\n\t\tfor(int i=0; i<4; i++){\n\t\t\tfor(int j=0; j<13; j++){\n\t\t\t\tcin >> point[i][j];\n\t\t\t}\n\t\t}\n\t\tfor(int i=0; i<9; i++){\n\t\t\tcin >> rate[i];\n\t\t}\n\t\t\n\t\tfor(int i=0; i<n; i++){\n\t\t\tvector<pair<int, int> > a(5);\n\t\t\tint psum = 0;\n\t\t\tfor(int j=0; j<5; j++){\n\t\t\t\tstring s;\n\t\t\t\tcin >> s;\n\t\t\t\ta[j].first = nton[(int)s[0]];\n\t\t\t\ta[j].second = ston[(int)s[1]];\n\t\t\t\tpsum += point[a[j].second][a[j].first];\n\t\t\t}\n\t\t\tsort(a.begin(), a.end());\n\t\t\tcout << psum *calc(a) << endl;\n\t\t}\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 120, "memory_kb": 3016, "score_of_the_acc": -1.3761, "final_rank": 17 }, { "submission_id": "aoj_2218_2954657", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint n, base[4][13], rate[10];\nstring mark = \"SCHD\";\n\nint getNumber(char c) {\n\tif (c == 'A') return 0;\n\tif (c == 'T') return 9;\n\tif (c == 'J') return 10;\n\tif (c == 'Q') return 11;\n\tif (c == 'K') return 12;\n\treturn c - '0' - 1;\n}\n\nbool isStraight(string card) {\n\tint tmp[5];\n\tfor (int i=0; i<5; ++i) tmp[i] = getNumber(card[2 * i]);\n\tsort(tmp, tmp + 5);\n\tfor (int i=0; i<4; ++i) if (tmp[i]+1 != tmp[i+1]) return false;\n\treturn true;\n}\n\nbool isFlush(string card) {\n\tchar tmp = card[1];\n\tfor (int i=1;i<5; ++i) if (tmp != card[2*i+1]) return false;\n\treturn true;\n}\n\nbool isRoyal(string card) {\n\tset< char > num_set;\n\tfor (int i=0; i<5; ++i) num_set.insert(card[2*i]);\n\tset< char > yes = {'A','T','J','Q','K'};\n\treturn num_set == yes;\n}\n\nint getID(string card) {\n\tint cnt[13];\n\tmemset(cnt, 0, sizeof(cnt));\n\tfor (int i=0; i<5; ++i) ++cnt[getNumber(card[2 * i])];\n\t\n\tint pair_num = 0, max_card = 0;\n\tfor (int i=0; i<13; ++i) {\n\t\tif (cnt[i] == 2) ++pair_num;\n\t\tmax_card = max(max_card, cnt[i]);\n\t}\n\t\n\tif (max_card == 2 && pair_num == 1) return 0; // one pair\n\tif (max_card == 2 && pair_num == 2) return 1; // two pair\n\t\n\tif (max_card == 3) {\n\t\tif (pair_num == 0) return 2; // three card\n\t\treturn 5; // full house\n\t}\n\t\n\tbool royal = isRoyal(card), straight = isStraight(card), flush = isFlush(card);\n\t\n\tif (royal && flush) return 8;\n\tif (straight && flush) return 7;\t\n\tif (straight || royal) return 3;\n\tif (flush) return 4;\n\t\n\tif (max_card == 4) return 6;\n\t\n\treturn 9;\n}\n\nvoid solve() {\n\tfor (int i=0; i<n; ++i) {\n\t\tstring card;\n\t\tint sum = 0;\n\t\tfor (int j=0; j<5; ++j) {\n\t\t\tstring cj;\n\t\t\tcin >> cj;\n\t\t\tcard += cj;\n\t\t\tsum += base[mark.find(cj[1])][getNumber(cj[0])];\n\t\t}\n\t\tcout << sum * rate[getID(card)] << endl;\n\t}\n}\n\nint main() {\n\trate[9] = 0;\n\tint i = 0;\n\twhile (cin >> n) {\n\t\tif (i > 0) cout << endl;\n\t\tfor (int i=0; i<4; ++i) for (int j=0; j<13; ++j) cin >> base[i][j];\n\t\tfor (int i=0; i<9; ++i) cin >> rate[i];\n\t\tsolve();\n\t\t++i;\n\t}\n}", "accuracy": 1, "time_ms": 160, "memory_kb": 3164, "score_of_the_acc": -1.6416, "final_rank": 19 }, { "submission_id": "aoj_2218_2954625", "code_snippet": "#include <cstdio>\n#include <cstdint>\n#include <vector>\n#include <algorithm>\n#include <string>\n#include <set>\n#include <map>\n\nbool is_royal(const std::vector<std::pair<int, char>> &cards) {\n std::set<int> s;\n for (const auto &p: cards) {\n s.insert(p.first);\n }\n return s.count(10) && s.count(11) && s.count(12) && s.count(13) && s.count(1);\n}\n\nbool is_flush(const std::vector<std::pair<int, char>> &cards) {\n std::set<char> s;\n for (const auto &p: cards) {\n s.insert(p.second);\n }\n return (s.size() == 1);\n}\n\nbool is_straight(const std::vector<std::pair<int, char>> &cards) {\n std::set<int> s;\n std::vector<int> t;\n for (const auto &p: cards) {\n s.insert(p.first);\n t.push_back(p.first);\n }\n if (s.size() != 5) return false;\n std::sort(t.begin(), t.end());\n return (t[4]-t[0] == 4);\n}\n\nstd::vector<int> multi(const std::vector<std::pair<int, char>> &cards) {\n std::map<int, int> s;\n for (const auto &p: cards) {\n ++s[p.first];\n }\n std::vector<int> res;\n for (const auto &p: s) {\n if (p.second >= 2) {\n res.emplace_back(p.second);\n }\n }\n return res;\n}\n\nint testcase_ends(bool first) {\n int N;\n if (scanf(\"%d\", &N) == EOF)\n return 1;\n\n if (!first)\n printf(\"\\n\");\n\n std::map<char, std::vector<int>> score;\n for (char ch: {'S', 'C', 'H', 'D'}) {\n score[ch].emplace_back(0);\n for (int i=1; i<=13; ++i) {\n int x;\n scanf(\"%d\", &x);\n score[ch].emplace_back(x);\n }\n }\n\n std::vector<int> hands{0};\n for (int i=0; i<9; ++i) {\n int x;\n scanf(\"%d\", &x);\n hands.emplace_back(x);\n }\n\n for (int i=0; i<N; ++i) {\n std::vector<std::pair<int, char>> cards;\n int base=0;\n for (int j=0; j<5; ++j) {\n char n, s;\n scanf(\" %c%c\", &n, &s);\n int num=0;\n switch (n) {\n case 'A':\n num = 1;\n break;\n case 'T':\n num = 10;\n break;\n case 'J':\n num = 11;\n break;\n case 'Q':\n num = 12;\n break;\n case 'K':\n num = 13;\n break;\n default:\n num = n-'0';\n }\n\n cards.emplace_back(num, s);\n base += score[s][num];\n }\n\n bool royal=is_royal(cards);\n bool straight=is_straight(cards);\n bool flush=is_flush(cards);\n std::vector<int> mul=multi(cards);\n std::sort(mul.begin(), mul.end());\n\n intmax_t res=base;\n if (royal && flush) {\n res *= hands[9];\n } else if (straight && flush) {\n res *= hands[8];\n } else if (mul.size() == 1 && mul[0] == 4) {\n res *= hands[7];\n } else if (mul.size() == 2 && mul[0] == 2 && mul[1] == 3) {\n res *= hands[6];\n } else if (flush) {\n res *= hands[5];\n } else if (straight || royal) {\n res *= hands[4];\n } else if (mul.size() == 1 && mul[0] == 3) {\n res *= hands[3];\n } else if (mul.size() == 2 && mul[0] == 2 && mul[1] == 2) {\n res *= hands[2];\n } else if (mul.size() == 1 && mul[0] == 2) {\n res *= hands[1];\n } else {\n res = 0;\n }\n printf(\"%jd\\n\", res);\n }\n return 0;\n}\n\nint main() {\n for (int i=0; true; ++i) {\n if (testcase_ends(i == 0)) return 0;\n }\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 2672, "score_of_the_acc": -0.9739, "final_rank": 8 }, { "submission_id": "aoj_2218_2654467", "code_snippet": "#include \"bits/stdc++.h\"\n#include<unordered_map>\n#include<unordered_set>\n#pragma warning(disable:4996)\nusing namespace std;\n\nint solve(vector<pair<int, int>>cards) {\n\tsort(cards.begin(),cards.end());\n\tbool flush_flag = all_of(cards.begin(), cards.end(), [&](const pair<int, int>&p) {\n\t\treturn p.second==cards[0].second;\n\t});\n\tbool str_flag;\n\t{\n\t\tbool ok=true;\n\t\tfor (int i = 0; i < 4; ++i) {\n\t\t\tif (cards[i].first + 1 != cards[i + 1].first) {\n\t\t\t\tok=false;\n\t\t\t}\n\t\t\tif(cards[0].first==1&&cards[1].first==10&&cards[2].first==11&&cards[3].first==12&&cards[4].first==13)ok=true;\n\t\t}\n\t\tstr_flag=ok;\n\t}\n\tif(str_flag&&flush_flag&&cards[0].first==1&&cards[1].first==10)return 9;\n\tif(str_flag&&flush_flag)return 8;\n\tif(str_flag)return 4;\n\tif(flush_flag)return 5;\n\n\n\tmap<int,int>mp;\n\tfor (int i = 0; i < 5; ++i) {\n\t\tmp[cards[i].first]++;\n\t}\n\tvector<int>v;\n\tfor (auto m : mp) {\n\t\tv.push_back(m.second);\n\t}\n\tsort(v.begin(),v.end(),greater<int>());\n\tif (v == vector<int>{4, 1})return 7;\n\tif (v == vector<int>{3, 2})return 6;\n\tif (v == vector<int>{1, 1, 1, 1, 1})return 0;\n\tif (v == vector<int>{2, 1, 1, 1})return 1;\n\tif (v == vector<int>{2, 2, 1})return 2;\n\tif (v == vector<int>{3, 1, 1})return 3;\n}\n\nint main() {\n\tint N;\n\tbool flag=false;\n\twhile (cin>>N) {\n\t\tif(flag)cout<<endl;\n\t\tflag=true;\n\t\tvector<vector<int>>costs(4,vector<int>(13));\n\t\tfor (int i = 0; i < 4; ++i) {\n\t\t\tfor (int j = 0; j < 13; ++j) {\n\t\t\t\tcin>>costs[i][j];\n\t\t\t}\n\t\t}\n\t\tvector<int>yaku_cost(10);\n\t\tfor (int i = 0; i < 9; ++i) {\n\t\t\tcin>>yaku_cost[i+1];\n\t\t}\n\t\tfor (int q = 0; q < N; ++q) {\n\t\t\tmap<char, int>type_mp{\n\t\t\t\t{'S',0},\n\t\t\t\t{'C',1},\n\t\t\t\t{'H',2},\n\t\t\t\t{'D',3},\n\t\t\t};\n\t\t\tmap<char, int>num_mp{\n\t\t\t\t{'T',10},\n\t\t\t\t{'J',11},\n\t\t\t\t{'Q',12},\n\t\t\t\t{'K',13},\n\t\t\t\t{'A',1}\n\t\t\t};\n\n\t\t\tint sum=0;\n\t\t\tvector<pair<int,int>>cards;\n\t\t\tfor (int i = 0; i < 5; ++i) {\n\t\t\t\tstring st;cin>>st;\n\t\t\t\tint num=isdigit(st[0])?st[0]-'0':num_mp[st[0]];\n\t\t\t\tint type=type_mp[st[1]];\n\t\t\t\tsum+=costs[type][num-1];\n\t\t\t\tcards.emplace_back(num,type);\n\t\t\t}\n\t\t\tint yaku=solve(cards);\n\t\t\tint ans=sum*yaku_cost[yaku];\n\t\t\tcout<<ans<<endl;\n\t\t}\n\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 180, "memory_kb": 3128, "score_of_the_acc": -1.7257, "final_rank": 20 }, { "submission_id": "aoj_2218_2648645", "code_snippet": "#include <stdio.h>\n#include <cmath>\n#include <algorithm>\n#include <cfloat>\n#include <stack>\n#include <queue>\n#include <vector>\n#include <string>\n#include <iostream>\n#include <set>\n#include <map>\n#include <time.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\nint LOC[128];\nint value[4][14],mult[9];\n\nint getNUM(char ch){\n\n\tif(ch >= '2' && ch <= '9')return ch - '0';\n\n\tswitch(ch){\n\tcase 'A':return 1;\n\tcase 'T':return 10;\n\tcase 'J':return 11;\n\tcase 'Q':return 12;\n\tcase 'K':return 13;\n\t}\n\treturn -1; //must not reach here\n}\n\nbool is_straight(int array[5]){\n\n\tif(array[4]-array[0] == 4)return true;\n\n\tif(array[0] == 1 && array[1] == 10)return true;\n\n\treturn false;\n}\n\n\nint main(){\n\n\tLOC['S'] = 0;\n\tLOC['C'] = 1;\n\tLOC['H'] = 2;\n\tLOC['D'] = 3;\n\n\tint N,num_2,num_3,num_4,num_suit,point;\n\tbool isFirst = true;\n\tchar buf[3];\n\tint num_table[14],suit_table[4],tmp_num,tmp_suit,array[5],index;\n\n\twhile(scanf(\"%d\",&N) != EOF){\n\n\t\tif(isFirst){\n\t\t\tisFirst = false;\n\t\t}else{\n\t\t\tprintf(\"\\n\");\n\t\t}\n\n\t\tfor(int i = 0; i < 4; i++){\n\t\t\tfor(int k = 1; k <= 13; k++)scanf(\"%d\",&value[i][k]);\n\t\t}\n\n\t\tfor(int i = 0; i < 9; i++)scanf(\"%d\",&mult[i]);\n\n\t\tfor(int loop = 0; loop < N; loop++){\n\t\t\tfor(int i = 1; i <= 13; i++)num_table[i] = 0;\n\t\t\tfor(int i = 0; i < 4; i++)suit_table[i] = 0;\n\n\t\t\tnum_2 = num_3 = num_4 = num_suit = point = 0;\n\n\t\t\tfor(int k = 0; k < 5; k++){\n\t\t\t\tscanf(\"%s\",buf);\n\n\t\t\t\ttmp_suit = LOC[buf[1]];\n\t\t\t\ttmp_num = getNUM(buf[0]);\n\n\t\t\t\tpoint += value[tmp_suit][tmp_num];\n\t\t\t\tnum_table[tmp_num]++;\n\t\t\t\tsuit_table[tmp_suit]++;\n\t\t\t}\n\n\t\t\tfor(int k = 0; k < 4; k++){\n\t\t\t\tif(suit_table[k] > 0)num_suit++;\n\t\t\t}\n\n\t\t\tfor(int k = 1; k <= 13; k++){\n\t\t\t\tif(num_table[k] == 0)continue;\n\n\t\t\t\tswitch(num_table[k]){\n\t\t\t\tcase 2:\n\t\t\t\t\tnum_2++;\n\t\t\t\t\tbreak;\n\t\t\t\tcase 3:\n\t\t\t\t\tnum_3++;\n\t\t\t\t\tbreak;\n\t\t\t\tcase 4:\n\t\t\t\t\tnum_4++;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif(num_2 == 1){\n\t\t\t\tif(num_3 != 1){\n\t\t\t\t\tprintf(\"%d\\n\",point*mult[0]);\n\t\t\t\t\tcontinue;\n\t\t\t\t}else{\n\t\t\t\t\tprintf(\"%d\\n\",point*mult[5]);\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif(num_2 == 2){\n\t\t\t\tprintf(\"%d\\n\",point*mult[1]);\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tif(num_3 == 1){\n\t\t\t\tprintf(\"%d\\n\",point*mult[2]);\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tif(num_4 == 1){\n\t\t\t\tprintf(\"%d\\n\",point*mult[6]);\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tindex = 0;\n\t\t\tfor(int k = 1; k <= 13; k++){\n\t\t\t\tif(num_table[k] == 0)continue;\n\t\t\t\tarray[index++] = k;\n\t\t\t}\n\n\t\t\tif(num_suit >= 2){\n\n\t\t\t\tif(is_straight(array)){\n\t\t\t\t\tprintf(\"%d\\n\",point*mult[3]);\n\t\t\t\t}else{\n\t\t\t\t\tprintf(\"0\\n\");\n\t\t\t\t}\n\n\t\t\t}else{\n\n\t\t\t\tif(is_straight(array)){\n\t\t\t\t\tif(array[0] == 1 && array[4] == 13){\n\t\t\t\t\t\tprintf(\"%d\\n\",point*mult[8]);\n\t\t\t\t\t}else{\n\t\t\t\t\t\tprintf(\"%d\\n\",point*mult[7]);\n\t\t\t\t\t}\n\t\t\t\t}else{\n\t\t\t\t\tprintf(\"%d\\n\",point*mult[4]);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3196, "score_of_the_acc": -0.9558, "final_rank": 7 }, { "submission_id": "aoj_2218_2446876", "code_snippet": "#include <iostream>\n#include <vector>\n#include <cstdio>\n#include <cstring>\n#include <cstdlib>\n#include <utility>\n#include <algorithm>\n#include <string>\n#include <cmath>\n#include <queue>\n\n#define i64 long long\n#define ui64 unsigned long long\nusing namespace std;\n\nint c_points[5][14];\nint h_points[10];\n\nstruct card{\n int num;\n int mark;\n card(char n_,char m_):num(),mark(){\n switch(m_){\n case 'S': mark = 0; break;\n case 'H': mark = 2; break;\n case 'D': mark = 3; break;\n case 'C': mark = 1; break;\n }\n if(('0'<=n_)&&(n_<='9'))num=n_-'0';\n else{\n switch(n_){\n case 'T': num = 10; break;\n case 'J': num = 11; break;\n case 'Q': num = 12; break;\n case 'K': num = 13; break;\n case 'A': num = 1; break;\n }\n }\n }\n bool operator< (const card& a){\n return num==a.num?mark<a.mark:num<a.num;\n }\n};\n\nbool straight(vector<card>& hand){\n for(int i=0;i<4;i++){\n if((hand[i+1].num-hand[i].num==1)||\\\n ((!i)&&(hand[4].num==13&&hand[i].num==1)));\n else return false;\n }\n return true;\n}\n\nbool flush(vector<card>& hand){\n for(int i=0;i<4;i++){\n if(hand[i].mark!=hand[i+1].mark)return false;\n }\n return true;\n}\n\nbool no_pair(vector<card>& hand){\n for(int i=0;i<4;i++){\n if(hand[i].num==hand[i+1].num)return false;\n }\n return true;\n}\n\nbool one_pair(vector<card>& hand){\n int p;\n for(int i=0;i<=4;i++){\n if(i==4)return false;\n if(hand[i].num==hand[i+1].num){\n p=i;break;\n }\n }\n for(int i=p+1;i<4;i++){\n if(hand[i].num==hand[i+1].num)return false;\n }\n return true;\n}\n\nbool two_pair(vector<card>& hand){\n int p;\n for(int i=0;i<=2;i++){\n if(i==2)return false;\n if(hand[i].num==hand[i+1].num){\n p=i;break;\n }\n }\n if(p==0){\n return hand[2].num==hand[3].num?hand[3].num!=hand[4].num:hand[3].num==hand[4].num;\n }\n if(p==1){\n return hand[3].num==hand[4].num;\n }\n}\n\nbool three_card(vector<card>& hand){\n if(hand[0].num==hand[2].num&&hand[0].num!=hand[3].num)return true;\n if(hand[0].num!=hand[1].num&&hand[1].num==hand[3].num&&hand[3].num!=hand[4].num)return true;\n if(hand[0].num!=hand[1].num&&hand[1].num!=hand[2].num&&hand[2].num==hand[4].num)return true;\n return false;\n}\n\nbool fullhouse(vector<card>& hand){\n int p;\n if(hand[1].num!=hand[2].num){\n if(hand[0].num!=hand[1].num)return false;\n for(int i=2;i<4;i++){\n if(hand[i].num!=hand[i+1].num)return false;\n }\n return true;\n }else{\n if(hand[3].num!=hand[4].num)return false;\n for(int i=0;i<2;i++){\n if(hand[i].num!=hand[i+1].num)return false;\n }\n return true;\n }\n}\n\nbool straight_flush(vector<card>& hand){\n return straight(hand)&&flush(hand);\n}\n\nbool four_card(vector<card>& hand){\n return hand[0].num==hand[3].num||hand[1].num==hand[4].num;\n}\n\nbool royal_straight_flush(vector<card>& hand){\n if(straight_flush(hand)){\n if(hand[0].num==1&&hand[4].num==13)return true;\n }\n return false;\n}\n\n\ni64 solve(vector<card>& hand){\n sort(hand.begin(),hand.end());\n i64 res=0LL;\n \n for(int i=0;i<5;i++){\n int m_=hand[i].mark;\n int n_=hand[i].num;\n res+=(i64)c_points[m_][n_-1];\n }\n if(royal_straight_flush(hand)){\n res*=(i64)h_points[8];\n return res;\n }\n if(straight_flush(hand)){\n res*=(i64)h_points[7];\n return res;\n }\n if(four_card(hand)){\n res*=(i64)h_points[6];\n return res;\n }\n if(straight(hand)){\n res*=(i64)h_points[3];\n return res;\n }\n if(flush(hand)){\n res*=(i64)h_points[4];\n return res;\n }\n if(fullhouse(hand)){\n res*=(i64)h_points[5];\n return res;\n }\n if(one_pair(hand)){\n res*=(i64)h_points[0];\n return res;\n }\n if(two_pair(hand)){\n res*=(i64)h_points[1];\n return res;\n }\n if(three_card(hand)){\n res*=(i64)h_points[2];\n return res;\n }\n if(no_pair(hand)){\n return (i64)0;\n }\n \n return -1;\n}\n\nint main(){\n int n;\n bool flag = false;\n while(1){\n if(!flag){\n cin >> n;\n if(cin.eof())return 0; \n flag=true;\n }\n fill(&c_points[0][0],&c_points[4][13],0);\n fill(&h_points[0],&h_points[9],0);\n for(int i=0;i<4;i++){\n for(int k=0;k<13;k++){\n cin >> c_points[i][k];\n }\n }\n for(int i=0;i<9;i++){\n cin >> h_points[i];\n }\n char a[3];\n vector<card> hands;\n for(int i=0;i<n;i++){\n for(int k=0;k<5;k++){\n cin >> a;\n hands.push_back(card(a[0],a[1]));\n }\n cout << solve(hands) << endl;\n hands.clear();\n }\n if(flag){\n cin >> n;\n if(cin.eof())return 0; \n else cout << endl;\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 3012, "score_of_the_acc": -1.2743, "final_rank": 14 }, { "submission_id": "aoj_2218_2446875", "code_snippet": "#include <iostream>\n#include <deque>\n#include <vector>\n#include <cstdio>\n#include <cstring>\n#include <cstdlib>\n#include <utility>\n#include <algorithm>\n#include <string>\n#include <cmath>\n#include <queue>\n\n#define i64 long long\n#define ui64 unsigned long long\n#define REP(i,n) for(int (i)=0;(i)<(n);i++)\n#define REP2(i,k,n) for(int (i)=(k);(i)<(n);i++)\n#define ATCODER 1000000007\nusing namespace std;\n \n////////////////////////\n\nint c_points[5][14];\nint h_points[10];\n\nstruct card{\n int num;\n int mark;\n card(char n_,char m_):num(),mark(){\n switch(m_){\n case 'S': mark = 0; break;\n case 'H': mark = 2; break;\n case 'D': mark = 3; break;\n case 'C': mark = 1; break;\n }\n if(('0'<=n_)&&(n_<='9'))num=n_-'0';\n else{\n switch(n_){\n case 'T': num = 10; break;\n case 'J': num = 11; break;\n case 'Q': num = 12; break;\n case 'K': num = 13; break;\n case 'A': num = 1; break;\n }\n }\n }\n bool operator< (const card& a){\n return num==a.num?mark<a.mark:num<a.num;\n }\n};\n\nbool straight(vector<card>& hand){\n for(int i=0;i<4;i++){\n if((hand[i+1].num-hand[i].num==1)||\\\n ((!i)&&(hand[4].num==13&&hand[i].num==1)));\n else return false;\n }\n return true;\n}\n\nbool flush(vector<card>& hand){\n for(int i=0;i<4;i++){\n if(hand[i].mark!=hand[i+1].mark)return false;\n }\n return true;\n}\n\nbool no_pair(vector<card>& hand){\n for(int i=0;i<4;i++){\n if(hand[i].num==hand[i+1].num)return false;\n }\n return true;\n}\n\nbool one_pair(vector<card>& hand){\n int p;\n for(int i=0;i<=4;i++){\n if(i==4)return false;\n if(hand[i].num==hand[i+1].num){\n p=i;break;\n }\n }\n for(int i=p+1;i<4;i++){\n if(hand[i].num==hand[i+1].num)return false;\n }\n return true;\n}\n\nbool two_pair(vector<card>& hand){\n int p;\n for(int i=0;i<=2;i++){\n if(i==2)return false;\n if(hand[i].num==hand[i+1].num){\n p=i;break;\n }\n }\n if(p==0){\n return hand[2].num==hand[3].num?hand[3].num!=hand[4].num:hand[3].num==hand[4].num;\n }\n if(p==1){\n return hand[3].num==hand[4].num;\n }\n}\n\nbool three_card(vector<card>& hand){\n if(hand[0].num==hand[2].num&&hand[0].num!=hand[3].num)return true;\n if(hand[0].num!=hand[1].num&&hand[1].num==hand[3].num&&hand[3].num!=hand[4].num)return true;\n if(hand[0].num!=hand[1].num&&hand[1].num!=hand[2].num&&hand[2].num==hand[4].num)return true;\n return false;\n}\n\nbool fullhouse(vector<card>& hand){\n int p;\n if(hand[1].num!=hand[2].num){\n if(hand[0].num!=hand[1].num)return false;\n for(int i=2;i<4;i++){\n if(hand[i].num!=hand[i+1].num)return false;\n }\n return true;\n }else{\n if(hand[3].num!=hand[4].num)return false;\n for(int i=0;i<2;i++){\n if(hand[i].num!=hand[i+1].num)return false;\n }\n return true;\n }\n}\n\nbool straight_flush(vector<card>& hand){\n return straight(hand)&&flush(hand);\n}\n\nbool four_card(vector<card>& hand){\n return hand[0].num==hand[3].num||hand[1].num==hand[4].num;\n}\n\nbool royal_straight_flush(vector<card>& hand){\n if(straight_flush(hand)){\n if(hand[0].num==1&&hand[4].num==13)return true;\n }\n return false;\n}\n\n\ni64 solve(vector<card>& hand){\n sort(hand.begin(),hand.end());\n // for(int i=0;i<5;i++){\n // cout << hand[i].num << \" \" << hand[i].mark << endl;\n // }\n i64 res=0LL;\n \n for(int i=0;i<5;i++){\n int m_=hand[i].mark;\n int n_=hand[i].num;\n res+=(i64)c_points[m_][n_-1];\n }\n // cout << res << endl;\n if(royal_straight_flush(hand)){\n res*=(i64)h_points[8];\n // cout << 0 << endl;\n return res;\n }\n if(straight_flush(hand)){\n res*=(i64)h_points[7];\n // cout << 8 << endl;\n return res;\n }\n if(four_card(hand)){\n res*=(i64)h_points[6];\n // cout << 7 << endl;\n return res;\n }\n if(straight(hand)){\n res*=(i64)h_points[3];\n // cout << 4 << endl;\n return res;\n }\n if(flush(hand)){\n res*=(i64)h_points[4];\n // cout << 5 << endl;\n return res;\n }\n if(fullhouse(hand)){\n res*=(i64)h_points[5];\n // cout << 6 << endl;\n return res;\n }\n if(one_pair(hand)){\n res*=(i64)h_points[0];\n // cout << 1 << endl;\n return res;\n }\n if(two_pair(hand)){\n res*=(i64)h_points[1];\n // cout << 2 << endl;\n return res;\n }\n if(three_card(hand)){\n res*=(i64)h_points[2];\n // cout << 3 << endl;\n return res;\n }\n if(no_pair(hand)){\n return (i64)0;\n }\n \n return -1;\n}\n\nint main(){\n int n;\n bool flag = false;\n while(1){\n if(!flag){\n cin >> n;\n if(cin.eof())return 0; \n flag=true;\n }\n fill(&c_points[0][0],&c_points[4][13],0);\n fill(&h_points[0],&h_points[9],0);\n for(int i=0;i<4;i++){\n for(int k=0;k<13;k++){\n cin >> c_points[i][k];\n }\n }\n for(int i=0;i<9;i++){\n cin >> h_points[i];\n }\n char a[3];\n vector<card> hands;\n for(int i=0;i<n;i++){\n for(int k=0;k<5;k++){\n cin >> a;\n hands.push_back(card(a[0],a[1]));\n }\n cout << solve(hands) << endl;\n hands.clear();\n }\n if(flag){\n cin >> n;\n if(cin.eof())return 0; \n else cout << endl;\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 90, "memory_kb": 3068, "score_of_the_acc": -1.2491, "final_rank": 13 }, { "submission_id": "aoj_2218_1879553", "code_snippet": "#include <iostream>\n#include <algorithm>\n#include <cstring>\n \nusing namespace std;\n \nint arr[4][13], rank[9], card[5], cnt[13];\nstring str[5];\n \nint getMark(char ch){\n if(ch == 'S'){\n\treturn 0;\n }else if(ch == 'C'){\n\treturn 1;\n }else if(ch == 'H'){\n\treturn 2;\n }\n return 3;\n}\n \nint getNum(char ch){\n if('2' <= ch && ch <= '9'){\n\treturn (ch-'0')-1;\n }else if(ch == 'A'){\n\treturn 0;\n }else if(ch == 'T'){\n\treturn 9;\n }else if(ch == 'J'){\n\treturn 10;\n }else if(ch == 'Q'){\n\treturn 11;\n }\n return 12;\n}\n \nint judge(){\n bool same_mark = true;\n char ch = str[0][1];\n for(int i = 1 ; i < 5 ; i++){\n\tif(ch != str[i][1]){\n\t same_mark = false;\n\t break;\n\t}\n }\n \n if(same_mark){\n\tif(card[0] == 1 && card[1] == 10 && card[2] == 11 &&\n\t card[3] == 12 && card[4] == 13){\n\t return rank[8];\n\t}else{\n\t bool check = true;\n\t for(int i = 1 ; i < 5 ; i++){\n\t\tif(card[i] - card[i-1] != 1){\n\t\t check = false;\n\t\t break;\n\t\t}\n\t }\n\t if(check){\n\t\treturn rank[7];\n\t }else{\n\t\treturn rank[4];\n\t }\n\t}\n }\n \n sort(cnt, cnt+13, greater<int>());\n if(cnt[0] == 4){\n\treturn rank[6];\n }else if(cnt[0] == 3){\n\tif(cnt[1] == 2){\n\t return rank[5];\n\t}else{\n\t return rank[2];\n\t}\n }else if(cnt[0] == 2){\n\tif(cnt[1] == 2){\n\t return rank[1];\n\t}else{\n\t return rank[0];\n\t} \n }else{\n\tbool check = true;\n\tfor(int i = 1 ; i < 5 ; i++){\n\t if(card[i] - card[i-1] != 1){\n\t\tcheck = false;\n\t\tbreak;\n\t }\n\t}\n\tif(check){\n\t return rank[3];\n\t}\n\tif(card[0] == 1 && card[1] == 10 && card[2] == 11 &&\n\t card[3] == 12 && card[4] == 13){\n\t return rank[3];\n\t}\n }\n \n return 0;\n}\n \nint main(){\n int N;\n bool space = false;\n \n while(cin >> N){\n\tif(space){\n\t cout << endl;\n\t}else{\n\t space = true;\n\t}\n \n\tfor(int i = 0 ; i < 4 ; i++){\n\t for(int j = 0 ; j < 13 ; j++){\n\t\tcin >> arr[i][j];\n\t }\n\t}\n\tfor(int i = 0 ; i < 9 ; i++){\n\t cin >> rank[i];\n\t}\n \n\twhile(N--){\n\t int point = 0, m,n;\n\t memset(cnt, 0, sizeof(cnt));\n\t for(int i = 0 ; i < 5 ; i++){\n\t\tcin >> str[i];\n\t\tm = getMark(str[i][1]);\n\t\tn = getNum(str[i][0]);\n\t\tpoint += arr[m][n];\n\t\tcard[i] = n+1;\n\t\tcnt[n]++;\n\t }\n\t sort(card, card+5);\n\t cout << point*judge() << endl;\n\t}\n }\n return 0;\n}", "accuracy": 1, "time_ms": 160, "memory_kb": 1220, "score_of_the_acc": -0.7814, "final_rank": 3 }, { "submission_id": "aoj_2218_1814779", "code_snippet": "#include <vector>\n#include <iostream>\n#include <algorithm>\nusing namespace std;\nint n, a[4][13], p[10], s[5], t[5], z; char c; string s1 = \"A23456789TJQK\", s2 = \"SCHD\";\nint judge() {\n\tvector<int> v; int w = 1, f = 1, g = 1, h = 1;\n\tfor (int i = 1; i < 5; i++) {\n\t\tif (s[i - 1] != s[i]) v.push_back(w), w = 0;\n\t\tif (s[i - 1] + 1 != s[i]) g = 0;\n\t\tif (t[i - 1] != t[i]) f = 0;\n\t\tw++;\n\t}\n\tv.push_back(w);\n\tsort(v.begin(), v.end());\n\tif (v.size() == 2) return v.back() + 3;\n\tfor (int i = 0; i < 5; i++) h &= (s[i] == (i ? (i + 8) % 13 : 0) ? 1 : 0);\n\tif (f) return h ? 9 : g ? 8 : 5;\n\tif (g | h) return 4;\n\tif (v.back() == 3) return 3;\n\tif (v.back() == 2) return 5 - v.size();\n\treturn 0;\n}\nint main() {\n\twhile (cin >> n) {\n\t\tif (z++) cout << endl;\n\t\tfor (int i = 0; i < 4; i++) {\n\t\t\tfor (int j = 0; j < 13; j++) cin >> a[i][j];\n\t\t}\n\t\tfor (int i = 1; i < 10; i++) cin >> p[i];\n\t\tfor (int i = 0; i < n; i++) {\n\t\t\tint cnt = 0;\n\t\t\tfor (int j = 0; j < 5; j++) {\n\t\t\t\tcin >> c; s[j] = s1.find(c);\n\t\t\t\tcin >> c; t[j] = s2.find(c);\n\t\t\t\tcnt += a[t[j]][s[j]];\n\t\t\t}\n\t\t\tsort(s, s + 5);\n\t\t\tsort(t, t + 5);\n\t\t\tcout << cnt * p[judge()] << endl;\n\t\t}\n\t}\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 3016, "score_of_the_acc": -1.2761, "final_rank": 15 }, { "submission_id": "aoj_2218_1814761", "code_snippet": "#include <vector>\n#include <iostream>\n#include <algorithm>\nusing namespace std;\nint n, a[4][13], p[10], z; char c; pair<int, int> s[5]; string s1 = \"A23456789TJQK\", s2 = \"SCHD\";\nint judge() {\n\tvector<int> v; int w = 1;\n\tfor (int i = 1; i < 5; i++) {\n\t\tif (s[i].first != s[i - 1].first) v.push_back(w), w = 0;\n\t\tw++;\n\t}\n\tv.push_back(w);\n\tsort(v.begin(), v.end());\n\tif (v.back() == 4) return 7;\n\tif (v.back() == 3 && v.size() == 2) return 6;\n\tint f = 1, g = 1, h = 1;\n\tfor (int i = 1; i < 5; i++) {\n\t\tif (s[i].second != s[i - 1].second) f = 0;\n\t}\n\tfor (int i = 1; i < 5; i++) {\n\t\tif (s[i - 1].first + 1 != s[i].first) g = 0;\n\t}\n\tfor (int i = 0; i < 5; i++) {\n\t\tif (s[i].first != (i ? (i + 8) % 13 : 0)) h = 0;\n\t}\n\tif (f) {\n\t\tif (h) return 9;\n\t\tif (g) return 8;\n\t\treturn 5;\n\t}\n\tif (g | h) return 4;\n\tif (v.back() == 3) return 3;\n\tif (v.back() == 2 && v[v.size() - 2] == 2) return 2;\n\tif (v.back() == 2) return 1;\n\treturn 0;\n}\nint main() {\n\twhile (cin >> n) {\n\t\tif (z++) cout << endl;\n\t\tfor (int i = 0; i < 4; i++) {\n\t\t\tfor (int j = 0; j < 13; j++) cin >> a[i][j];\n\t\t}\n\t\tfor (int i = 1; i < 10; i++) cin >> p[i];\n\t\tfor (int i = 0; i < n; i++) {\n\t\t\tint cnt = 0;\n\t\t\tfor (int j = 0; j < 5; j++) {\n\t\t\t\tcin >> c; s[j].first = s1.find(c);\n\t\t\t\tcin >> c; s[j].second = s2.find(c);\n\t\t\t\tcnt += a[s[j].second][s[j].first];\n\t\t\t}\n\t\t\tsort(s, s + 5);\n\t\t\tcout << cnt * p[judge()] << endl;\n\t\t}\n\t}\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 3016, "score_of_the_acc": -1.2761, "final_rank": 15 }, { "submission_id": "aoj_2218_1771887", "code_snippet": "#include<iostream>\n#include<string>\n#include<algorithm>\nusing namespace std;\nint N, a[4][14], b[9];\nchar T[15] = \"-A23456789TJQK\";\nchar U[5] = \"SCHD\";\nstring S; pair<int, int>d[5];\nint poker() {\n\tint e[14], f[9];\n\tint g[14], h[9];\n\tfor (int i = 0; i < 14; i++) { e[i] = 0; g[i] = 0; }\n\tfor (int i = 0; i < 9; i++) { f[i] = 0; h[i] = 0; }\n\tfor (int i = 0; i < 5; i++) {\n\t\te[d[i].first]++;\n\t\tf[d[i].second]++;\n\t}\n\tfor (int i = 0; i < 14; i++) { g[e[i]]++; }\n\tfor (int i = 0; i < 4; i++) { h[f[i]]++; }\n\tif (h[5] == 1) {\n\t\tif (d[0].first != 1) { goto E; }\n\t\tif (d[1].first != 10) { goto E; }\n\t\tif (d[2].first != 11) { goto E; }\n\t\tif (d[3].first != 12) { goto E; }\n\t\tif (d[4].first != 13) { goto E; }\n\t\treturn 8; E:;\n\t\tfor (int i = 1; i < 5; i++) {\n\t\t\tif (d[i].first != d[i - 1].first + 1) {\n\t\t\t\tgoto F;\n\t\t\t}\n\t\t}\n\t\treturn 7; F:;\n\t\treturn 4;\n\t}\n\tif (g[4] == 1) { return 6; }\n\tif (g[2] == 1 && g[3] == 1) { return 5; }\n\tfor (int i = 1; i < 5; i++) {\n\t\tif (d[i].first != d[i - 1].first + 1) {\n\t\t\tgoto G;\n\t\t}\n\t}\n\treturn 3; G:;\n\tif (d[0].first != 1) { goto H; }\n\tif (d[1].first != 10) { goto H; }\n\tif (d[2].first != 11) { goto H; }\n\tif (d[3].first != 12) { goto H; }\n\tif (d[4].first != 13) { goto H; }\n\treturn 3; H:;\n\tif (g[3] == 1) { return 2; }\n\tif (g[2] == 2) { return 1; }\n\tif (g[2] == 1) { return 0; }\n\treturn 9;\n}\nint main() {\n\tint cnt = 0;\n\twhile (cin >> N) {\n\t\tif (cnt) { cout << endl; }\n\t\tcnt++;\n\t\tfor (int i = 0; i < 4; i++) {\n\t\t\tfor (int j = 1; j <= 13; j++) {\n\t\t\t\tcin >> a[i][j];\n\t\t\t}\n\t\t}\n\t\tfor (int i = 0; i < 9; i++) { cin >> b[i]; }\n\t\tfor (int i = 0; i < N; i++) {\n\t\t\tfor (int j = 0; j < 5; j++) {\n\t\t\t\tcin >> S;\n\t\t\t\tfor (int k = 0; k < 14; k++) {\n\t\t\t\t\tif (T[k] == S[0]) { d[j].first = k; }\n\t\t\t\t}\n\t\t\t\tfor (int k = 0; k < 4; k++) {\n\t\t\t\t\tif (U[k] == S[1]) { d[j].second = k; }\n\t\t\t\t}\n\t\t\t}\n\t\t\tsort(d, d + 5);\n\t\t\tint A = b[poker()];\n\t\t\tint B = 0;\n\t\t\tfor (int j = 0; j < 5; j++) {\n\t\t\t\tB += a[d[j].second][d[j].first];\n\t\t\t}\n\t\t\tcout << A*B << endl;\n\t\t}\n\t}\n}", "accuracy": 1, "time_ms": 170, "memory_kb": 1204, "score_of_the_acc": -0.8243, "final_rank": 5 }, { "submission_id": "aoj_2218_1438259", "code_snippet": "#include<cstdio>\n#include<cstdlib>\n#include<cstring>\n#include<cmath>\n#include<iostream>\n#include<string>\n#include<vector>\n#include<map>\n#include<set>\n#include<list>\n#include<queue>\n#include<deque>\n#include<algorithm>\n#include<numeric>\n#include<utility>\n#include<complex>\n#include<functional>\n \nusing namespace std;\n\n/* constant */\n\n/* typedef */\n\ntypedef pair<int,int> pii;\n\n/* global variables */\n\nint bscs[4][13], mps[9];\nstring crds[5];\npii cs[5];\n\n/* subroutines */\n\npii parse(string crd) {\n int d, s;\n\n switch (crd[0]) {\n case 'A': d = 0; break;\n case 'T': d = 9; break;\n case 'J': d = 10; break;\n case 'Q': d = 11; break;\n case 'K': d = 12; break;\n default: d = crd[0] - '1';\n }\n\n switch (crd[1]) {\n case 'S': s = 0; break;\n case 'C': s = 1; break;\n case 'H': s = 2; break;\n case 'D': s = 3; break;\n }\n\n return pii(d, s);\n}\n\nbool flush() {\n int s = cs[0].second;\n for (int i = 1; i < 5; i++)\n if (cs[i].second != s) return false;\n return true;\n}\n\nbool straight() {\n if (cs[0].first == 0 && cs[1].first == 9) {\n for (int i = 2; i < 5; i++)\n if (cs[i].first != 8 + i) return false;\n return true;\n }\n\n int d = cs[0].first;\n for (int i = 1; i < 5; i++)\n if (cs[i].first != d + i) return false;\n return true;\n}\n\nvoid pairs(int *pns) {\n int cns[13];\n memset(cns, 0, sizeof(cns));\n memset(pns, 0, sizeof(int) * 5);\n\n for (int i = 0; i < 5; i++) cns[cs[i].first]++;\n for (int i = 0; i < 13; i++) pns[cns[i]]++;\n}\n\nint hand() {\n bool sf = straight();\n bool ff = flush();\n\n if (sf && ff) {\n // Royal Straight Flush\n if (cs[0].first == 0 && cs[1].first == 9) return 8;\n // Straight Flush\n return 7;\n }\n\n int pns[5];\n pairs(pns);\n\n // Four Cards\n if (pns[4] == 1) return 6;\n // Full House\n if (pns[3] == 1 && pns[2] == 1) return 5;\n\n // Flush\n if (ff) return 4;\n\n // Straight\n if (sf) return 3;\n\n // Three Card\n if (pns[3] == 1) return 2;\n\n // Two Pair\n if (pns[2] == 2) return 1;\n\n // One Pair\n if (pns[2] == 1) return 0;\n\n // Buta\n return -1;\n}\n\n/* main */\n\nint main() {\n for (bool first = true;; first = false) {\n int n;\n cin >> n;\n if (cin.eof()) break;\n \n for (int i = 0; i < 4; i++)\n for (int j = 0; j < 13; j++) cin >> bscs[i][j];\n\n for (int i = 0; i < 9; i++) cin >> mps[i];\n\n if (! first) cout << endl;\n \n while (n--) {\n for (int i = 0; i < 5; i++) {\n\tcin >> crds[i];\n\tcs[i] = parse(crds[i]);\n }\n sort(cs, cs + 5);\n\n int h = hand();\n int sc = 0;\n\n if (h >= 0) {\n\tint sum = 0;\n\tfor (int i = 0; i < 5; i++)\n\t sum += bscs[cs[i].second][cs[i].first];\n\tsc = sum * mps[h];\n }\n\n cout << sc << endl;\n }\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 180, "memory_kb": 1212, "score_of_the_acc": -0.8779, "final_rank": 6 }, { "submission_id": "aoj_2218_1136556", "code_snippet": "#include <string> \n#include <algorithm> \n#include <cfloat> \n#include <climits> \n#include <cmath> \n#include <complex> \n#include <cstdio> \n#include <cstdlib> \n#include <cstring> \n#include <functional> \n#include <iostream> \n#include <map> \n#include <memory> \n#include <queue> \n#include <set> \n#include <sstream> \n#include <stack> \n#include <utility> \n#include <vector> \n\n#define EACH(i,c) for(__typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)\n#define ALL(x) (x).begin(),(x).end() \ntypedef long long ll;\nusing namespace std; \nconst double eps = 1e-10;\n\nint has[4][20];\nint cost[4][20], mul[10];\nint getVal(string s){\n if('2' <= s[0] && s[0] <= '9') return s[0]-'0';\n else if(s[0] == 'A') return 1;\n else if(s[0] == 'T') return 10;\n else if(s[0] == 'J') return 11;\n else if(s[0] == 'Q') return 12;\n else return 13;\n}\nint getMark(string s){\n if(s[1] == 'S') return 0;\n else if(s[1] == 'C') return 1;\n else if(s[1] == 'H') return 2;\n else return 3;\n}\nint st(){\n for(int i = 1; i <= 10; ++i){\n bool f = true;\n for(int k = 0; k < 5; ++k){\n bool h = false;\n for(int j=0;j<4;++j) h = h || has[j][i+k];\n f = f && h;\n }\n if(f) return i;\n }\n return 0;\n}\nint fl(){\n for(int i = 0; i < 4; ++i){\n int cnt = 0;\n for(int j = 1; j <= 13; ++j) if(has[i][j])++cnt;\n if(cnt == 5) return true;\n }\n return false;\n}\nint four(){\n for(int j = 1; j <= 13;++j){\n bool f = true;\n for(int i = 0; i < 4; ++i) f = f && has[i][j];\n if(f) return true;\n }\n return false;\n}\nint thr(){\n for(int j = 1; j <= 13;++j){\n int f = 0;\n for(int i = 0; i < 4; ++i) f += has[i][j];\n if(f == 3) return true;\n }\n return false;\n}\nint par(){\n int res = 0;\n for(int j = 1; j <= 13; ++j){\n int f = 0;\n for(int i = 0; i < 4; ++i) f += has[i][j];\n if(f == 2) ++res;\n }\n return res;\n}\nint judge(){\n int s = st();\n int f = fl();\n int fo = four();\n int th = thr();\n int p = par();\n // cout << \">\" << p << endl;\n if(s && f) return s == 10? 9: 8;\n else if(fo) return 7;\n else if(th && p) return 6;\n else if(f) return 5;\n else if(s) return 4;\n else if(th) return 3;\n else return p;\n}\nvoid solve(const vector<string> &vs){\n ll sum = 0;\n memset(has, 0, sizeof(has));\n for(int i = 0; i < 5; ++i){\n int val = getVal(vs[i]);\n int mark = getMark(vs[i]);\n // cout << val << \",\" << mark << endl;\n has[mark][val]++;\n if(val == 1) has[mark][14]++;\n sum += cost[mark][val];\n }\n /*\n cout << \">\" << sum << endl;\n for(int i = 0; i < 4; ++i){\n for(int j = 1; j <= 14; ++j) cout << has[i][j];\n cout << endl;\n }\n */\n cout << sum * mul[judge()] << endl;\n}\nint main(){\n bool flag = true;\n int N;\n for(;cin >> N;){\n if(!flag) cout << endl;\n for(int i = 0; i < 4; ++i) for(int j = 1; j <= 13; ++j)\n cin >> cost[i][j];\n for(int i = 1; i <= 9; ++i){\n cin >> mul[i];\n }\n for(int i = 0; i < N; ++i){\n vector<string> vs(5);\n for(int j = 0; j < 5; ++j) cin >> vs[j];\n solve(vs);\n }\n flag = false;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 220, "memory_kb": 1208, "score_of_the_acc": -1.0761, "final_rank": 10 }, { "submission_id": "aoj_2218_1134652", "code_snippet": "#include<stdio.h>\n#include<algorithm>\nusing namespace std;\nint c[4][15];\nchar st[7]=\"SCHD\";\nchar nm[17]=\".23456789TJQKA\";\nint d[20];\nchar in[4];\npair<int,int>p[10];\nint calc(){\n\tbool flash=true;\n\tfor(int i=0;i<5;i++)for(int j=i+1;j<5;j++)\n\t\tif(p[i].second!=p[j].second)flash=false;\n\tbool st=true;\n\tfor(int i=0;i<4;i++)if(p[i].first+1!=p[i+1].first)st=false;\n\tbool st2=true;\n\tfor(int i=0;i<4;i++)if(p[i].first!=i+1)st2=false;\n\tif(p[4].first!=13)st2=false;\n\tif(st2)st=true;\n\tif(st&&flash&&p[0].first==9)return d[8];\n\tif(st&&flash)return d[7];\n\tif(p[0].first==p[3].first||p[1].first==p[4].first)return d[6];\n\tif((p[0].first==p[1].first&&p[2].first==p[4].first)||(p[0].first==p[2].first&&p[3].first==p[4].first))return d[5];\n\tif(flash)return d[4];\n\tif(st)return d[3];\n\tif(p[0].first==p[2].first||p[1].first==p[3].first||p[2].first==p[4].first)return d[2];\n\tif((p[0].first==p[1].first&&p[2].first==p[3].first)||\n\t(p[0].first==p[1].first&&p[3].first==p[4].first)||\n\t(p[1].first==p[2].first&&p[3].first==p[4].first))return d[1];\n\tfor(int i=0;i<4;i++)if(p[i].first==p[i+1].first)return d[0];\n\treturn 0;\n}\nint main(){\n\tint a;\n\tbool tc=false;\n\twhile(~scanf(\"%d\",&a)){\n\t\tif(tc)printf(\"\\n\");\n\t\tfor(int i=0;i<4;i++)for(int j=0;j<13;j++)\n\t\t\tscanf(\"%d\",&c[i][j]);\n\t\tfor(int i=0;i<4;i++)c[i][13]=c[i][0];\n\t\tfor(int i=0;i<9;i++)scanf(\"%d\",d+i);\n\t\twhile(a--){\n\t\t\tint ks=0;\n\t\t\tfor(int i=0;i<5;i++){\n\t\t\t\tscanf(\"%s\",in);\n\t\t\t\tfor(int j=1;j<=13;j++){\n\t\t\t\t\tif(in[0]==nm[j])p[i].first=j;\n\t\t\t\t}\n\t\t\t\tfor(int j=0;j<4;j++){\n\t\t\t\t\tif(in[1]==st[j])p[i].second=j;\n\t\t\t\t}\n\t\t\t\tks+=c[p[i].second][p[i].first];\n\t\t\t//\tprintf(\"(%d %d) \",p[i].first,p[i].second);\n\t\t\t}\n\t\t\tstd::sort(p,p+5);\n\t\t\tint sc=calc();\n\t\t\tprintf(\"%d\\n\",ks*sc);\n\t\t}\n\t\ttc=true;\n\t//\tprintf(\"\\n\");\n\t}\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 1036, "score_of_the_acc": -0.1, "final_rank": 1 }, { "submission_id": "aoj_2218_1086683", "code_snippet": "#include <iostream>\n#include <string>\n#include <queue>\n#include <vector>\n#include <functional>\n\nusing namespace std;\n\nint main() {\n\tbool hantei = false;\n\tint n;\n\twhile (cin >> n) {\n\t\tif (hantei) {\n\t\t\tcout << endl;\n\t\t}\n\t\thantei = true;\n\t\tint hyou[4][13];\n\t\tfor (int i = 0; i < 4; i++) {\n\t\t\tfor (int j = 0; j < 13; j++) {\n\t\t\t\tcin >> hyou[i][j];\n\t\t\t}\n\t\t}\n\t\tint data[9];\n\t\tfor (int i = 0; i < 9; i++) {\n\t\t\tcin >> data[i];\n\t\t}\n\t\tstring a;\n\t\tstring abc = \"SCHD\";\n\t\tfor (int i = 0; i < n; i++) {\n\t\t\tint counter = 0;\n\t\t\tbool hantei2 = true;\n\t\t\tint p;\n\t\t\tpriority_queue<int, vector<int>, greater<int> > pq;\n\t\t\tfor (int j = 0; j < 5; j++) {\n\t\t\t\tcin >> a;\n\t\t\t\tint b;\n\t\t\t\tif (a[0] == 'A') {\n\t\t\t\t\tb = 1;\n\t\t\t\t} else if (a[0] == 'T') {\n\t\t\t\t\tb = 10;\n\t\t\t\t} else if (a[0] == 'J') {\n\t\t\t\t\tb = 11;\n\t\t\t\t} else if (a[0] == 'Q') {\n\t\t\t\t\tb = 12;\n\t\t\t\t} else if (a[0] == 'K') {\n\t\t\t\t\tb = 13;\n\t\t\t\t} else {\n\t\t\t\t\tb = a[0]-'0';\n\t\t\t\t}\n\t\t\t\tfor (int k = 0; k < 4; k++) {\n\t\t\t\t\tif (a[1] == abc[k]) {\n\t\t\t\t\t\tif (j == 0) {\n\t\t\t\t\t\t\tp = k;\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\tif (p != k) {\n\t\t\t\t\t\t\t\thantei2 = false;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t\tcounter += hyou[k][b-1];\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tpq.push(b);\n\t\t\t}\n\t\t\tint cdata[5];\n\t\t\tfor (int j = 0; j < 5; j++) {\n\t\t\t\tcdata[j] = pq.top();\n\t\t\t\tpq.pop();\n\t\t\t}\n\t\t\tif (cdata[0] == 1 && cdata[1] == 10 && cdata[2] == 11 && cdata[3] == 12 && cdata[4] == 13) {\n\t\t\t\tif (hantei2) {\n\t\t\t\t\tcounter *= data[8];\n\t\t\t\t} else {\n\t\t\t\t\tcounter *= data[3];\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tif (cdata[0]+1 == cdata[1] && cdata[1]+1 == cdata[2] && cdata[2]+1 == cdata[3] && cdata[3]+1 == cdata[4]) {\n\t\t\t\t\tif (hantei2) {\n\t\t\t\t\t\tcounter *= data[7];\n\t\t\t\t\t} else {\n\t\t\t\t\t\tcounter *= data[3];\n\t\t\t\t\t}\n\t\t\t\t} else if ((cdata[0] == cdata[1] && cdata[0] == cdata[2] && cdata[0] == cdata[3]) || (cdata[1] == cdata[2] && cdata[1] == cdata[3] && cdata[1] == cdata[4])) {\n\t\t\t\t\tcounter *= data[6];\n\t\t\t\t} else if ((cdata[0] == cdata[1] && cdata[2] == cdata[3] && cdata[2] == cdata[4]) || (cdata[0] == cdata[1] && cdata[0] == cdata[2] && cdata[3] == cdata[4])) {\n\t\t\t\t\tcounter *= data[5];\n\t\t\t\t} else if (hantei2) {\n\t\t\t\t\tcounter *= data[4];\n\t\t\t\t} else if ((cdata[0] == cdata[1] && cdata[0] == cdata[2]) || (cdata[1] == cdata[2] && cdata[1] == cdata[3]) || (cdata[2] == cdata[3] && cdata[2] == cdata[4])){\n\t\t\t\t\tcounter *= data[2];\n\t\t\t\t} else if ((cdata[0] == cdata[1] && cdata[2] == cdata[3]) || (cdata[0] == cdata[1] && cdata[3] == cdata[4]) || (cdata[1] == cdata[2] && cdata[3] == cdata[4])) {\n\t\t\t\t\tcounter *= data[1];\n\t\t\t\t} else if ((cdata[0] == cdata[1]) || (cdata[1] == cdata[2]) || (cdata[2] == cdata[3]) || (cdata[3] == cdata[4])) {\n\t\t\t\t\tcounter *= data[0];\n\t\t\t\t} else {\n\t\t\t\t\tcounter *= 0;\n\t\t\t\t}\n\t\t\t}\n\t\t\tcout << counter << endl;\n\t\t}\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 160, "memory_kb": 1232, "score_of_the_acc": -0.7867, "final_rank": 4 }, { "submission_id": "aoj_2218_974694", "code_snippet": "#include <iostream>\n#include <algorithm>\n#include <cstring>\n\nusing namespace std;\n\nint arr[4][13], rank[9], card[5], cnt[13];\nstring str[5];\n\nint getMark(char ch){\n if(ch == 'S'){\n return 0;\n }else if(ch == 'C'){\n return 1;\n }else if(ch == 'H'){\n return 2;\n }\n return 3;\n}\n\nint getNum(char ch){\n if('2' <= ch && ch <= '9'){\n return (ch-'0')-1;\n }else if(ch == 'A'){\n return 0;\n }else if(ch == 'T'){\n return 9;\n }else if(ch == 'J'){\n return 10;\n }else if(ch == 'Q'){\n return 11;\n }\n return 12;\n}\n\nint judge(){\n bool same_mark = true;\n char ch = str[0][1];\n for(int i = 1 ; i < 5 ; i++){\n if(ch != str[i][1]){\n same_mark = false;\n break;\n }\n }\n\n if(same_mark){\n if(card[0] == 1 && card[1] == 10 && card[2] == 11 &&\n card[3] == 12 && card[4] == 13){\n return rank[8];\n }else{\n bool check = true;\n for(int i = 1 ; i < 5 ; i++){\n if(card[i] - card[i-1] != 1){\n check = false;\n break;\n }\n }\n if(check){\n return rank[7];\n }else{\n return rank[4];\n }\n }\n }\n\n sort(cnt, cnt+13, greater<int>());\n if(cnt[0] == 4){\n return rank[6];\n }else if(cnt[0] == 3){\n if(cnt[1] == 2){\n return rank[5];\n }else{\n return rank[2];\n }\n }else if(cnt[0] == 2){\n if(cnt[1] == 2){\n return rank[1];\n }else{\n return rank[0];\n } \n }else{\n bool check = true;\n for(int i = 1 ; i < 5 ; i++){\n if(card[i] - card[i-1] != 1){\n check = false;\n break;\n }\n }\n if(check){\n return rank[3];\n }\n if(card[0] == 1 && card[1] == 10 && card[2] == 11 &&\n card[3] == 12 && card[4] == 13){\n return rank[3];\n }\n }\n\n return 0;\n}\n\nint main(){\n int N;\n bool space = false;\n\n while(cin >> N){\n if(space){\n cout << endl;\n }else{\n space = true;\n }\n\n for(int i = 0 ; i < 4 ; i++){\n for(int j = 0 ; j < 13 ; j++){\n cin >> arr[i][j];\n }\n }\n for(int i = 0 ; i < 9 ; i++){\n cin >> rank[i];\n }\n\n while(N--){\n int point = 0, m,n;\n memset(cnt, 0, sizeof(cnt));\n for(int i = 0 ; i < 5 ; i++){\n cin >> str[i];\n m = getMark(str[i][1]);\n n = getNum(str[i][0]);\n point += arr[m][n];\n card[i] = n+1;\n cnt[n]++;\n }\n sort(card, card+5);\n cout << point*judge() << endl;\n }\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 150, "memory_kb": 1220, "score_of_the_acc": -0.7314, "final_rank": 2 } ]
aoj_2221_cpp
Problem F: KULASIS Description KULASISとは,全学共通科目に関する情報をWeb化し,学生・教員への支援やサービスの向上を目的に,高等教育研究開発推進機構で開発・運用しているシステムの名称です。 KULASISは2003年度のオンラインシラバス開発から始まり,Web掲示板・履修登録・成績関係(採点登録・学生からの採点確認)と順次システムを拡充してきました。 学生はパソコン・携帯電話から学内外を問わず,教務情報(休講・授業変更・レポート)の確認・履修登録・採点確認等の機能を利用することができます。ログイン件数は多い日には10,000件を超え,京都大学の教務情報システムとして浸透し,全学共通科目を履修するためには欠かせないものとなっています。このKULASISを全学共通科目のみにとどまらず,学部専門課程や大学院にも適用できるよう 開発を進めています。 http://www.z.k.kyoto-u.ac.jp/introduction_kulasis.html 京大生のQは後期のシラバスを組むためにKULASISにログインしていた. どの科目を入れようとしているか悩んでいると,突然KULASISが眩い光を放ち,別のページへと遷移した. 遷移した先のページは下図のようなものであった.5x5のマスには科目名と評価(不可,可,良,優)が書かれており,格子上には ● のボタンが16個配置されている. いったいなにが起きたのか分からなかったQだったが,どうやら格子上にある ● のボタンを押すと,その右上,右下,左上,左下にあるマスの科目の評価がそれぞれ 不可→可,可→良,良→優,優→不可 と入れ替わるようであった. ● のボタンは何度でも押すことができる. KULASISが何者かによって書き換えられてしまったのか自分が夢を見ているのかよく分からないが,これで成績を確定できるならば,出来るだけ良い成績を得たいとQは考えた. 単位を多く集めていることに自信があったQは,単位の数そのものよりも半年後の研究室配属に備えて 戦闘力 を最大にさせることにした. 戦闘力 とは各科目の評価を,不可→0点,可→60点,良→70点,優→80点と換算して合計した値のことで,これは研究室配属の際に自分がどの程度優位なのかを表すと(Qの学科では)思われている. いま,KULASISの画面の表示されている科目とその評価が与えられるので,得ることが出来る 戦闘力 の最大値を出力するプログラムを記してほしい. Input 入力の1行目にはテストケースの個数が与えられる.テストケースの数は100個以下であることが保障されている. 2行目以降は5x5の数字が並び,KULASISの画面に表示されている科目の評価が与えられ,1,2,3,4がそれぞれ不可,可,良,優に対応する. 0ならばそのコマには科目は登録されていない. テストケース同士の間は空行で区切られている. Output 各テストケースに対し,得ることの出来る 戦闘力 の最大値を出力せよ. Sample Input 5 1 1 0 3 3 1 1 0 3 3 0 0 0 0 0 2 2 0 4 4 2 2 0 4 4 1 1 1 0 0 1 1 1 1 0 1 0 1 1 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 3 4 3 4 3 4 3 4 3 4 3 4 3 4 3 4 3 4 3 4 3 4 3 4 2 2 2 2 2 0 0 0 2 0 2 2 0 2 0 2 2 0 2 0 0 0 0 2 0 Output for Sample Input 1280 1420 0 1920 1020
[ { "submission_id": "aoj_2221_10850489", "code_snippet": "#include <vector>\n#include <iostream>\n#include <algorithm>\nusing namespace std;\nvector<int> reverse_hash(int x, int b, int l) {\n\tvector<int> ret(l);\n\tint mul = 1;\n\tfor (int i = 0; i < l; i++) {\n\t\tret[i] = x / mul % b;\n\t\tmul *= b;\n\t}\n\treturn ret;\n}\nint transform(int x, int f) {\n\tvector<int> v1 = reverse_hash(x, 5, 5);\n\tvector<int> v2 = reverse_hash(f, 4, 4);\n\tfor (int i = 0; i < 4; i++) {\n\t\tfor (int j = 0; j < 2; j++) {\n\t\t\tif (v1[i + j] != 0) v1[i + j] = (v1[i + j] - 1 + v2[i]) % 4 + 1;\n\t\t}\n\t}\n\tint ret = 0, mul = 1;\n\tfor (int i = 0; i < 5; i++) {\n\t\tret += v1[i] * mul;\n\t\tmul *= 5;\n\t}\n\treturn ret;\n}\nint getscore(int x) {\n\tvector<int> v = reverse_hash(x, 5, 5);\n\tint ret = 0;\n\tfor (int i = 0; i < 5; i++) {\n\t\tif (v[i] == 2) ret += 60;\n\t\tif (v[i] == 3) ret += 70;\n\t\tif (v[i] == 4) ret += 80;\n\t}\n\treturn ret;\n}\nint Q, x;\nint main() {\n\tcin >> Q;\n\twhile (Q--) {\n\t\tvector<int> a(5);\n\t\tfor (int i = 0; i < 5; i++) {\n\t\t\tint mul = 1;\n\t\t\tfor (int j = 0; j < 5; j++) {\n\t\t\t\tcin >> x;\n\t\t\t\ta[i] += x * mul;\n\t\t\t\tmul *= 5;\n\t\t\t}\n\t\t}\n\t\tvector<vector<int> > dp(5, vector<int>(3125, -999999999));\n\t\tdp[0][a[0]] = 0;\n\t\tfor (int i = 0; i < 4; i++) {\n\t\t\tfor (int j = 0; j < 3125; j++) {\n\t\t\t\tif (dp[i][j] == -999999999) continue;\n\t\t\t\tfor (int l = 0; l < 256; l++) {\n\t\t\t\t\tint z = transform(a[i + 1], l);\n\t\t\t\t\tdp[i + 1][z] = max(dp[i + 1][z], dp[i][j] + getscore(transform(j, l)));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tint ret = 0;\n\t\tfor (int i = 0; i < 3125; i++) ret = max(ret, dp[4][i] + getscore(i));\n\t\tcout << ret << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 1590, "memory_kb": 3524, "score_of_the_acc": -0.7598, "final_rank": 17 }, { "submission_id": "aoj_2221_7936377", "code_snippet": "#include <algorithm>\n#include <cassert>\n#include <climits>\n#include <cmath>\n#include <complex>\n#include <cstdio>\n#include <cstring>\n#include <iostream>\n#include <map>\n#include <numeric>\n#include <queue>\n#include <set>\n#include <sstream>\n#include <stack>\n#include <string>\n#include <vector>\n#define f first\n#define s second\n#define mp make_pair\n#define REP(i, n) for (int i = 0; i < (int)(n); i++)\n#define rep(i, s, n) for (int i = (s); i < (int)(n); i++)\n#define FOR(i, c) \\\n for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)\n#define ALL(c) (c).begin(), (c).end()\n#define IN(x, s, g) ((x) >= (s) && (x) < (g))\n#define ISIN(x, y, w, h) (IN((x), 0, (w)) && IN((y), 0, (h)))\n#define print(x) printf(\"%d\\n\", x)\n#define EPS (1e-4)\nusing namespace std;\ntypedef unsigned int uint;\ntypedef long long ll;\nconst int _dx[] = {0, 1, 0, -1};\nconst int _dy[] = {-1, 0, 1, 0};\nint getInt() {\n int ret = 0, c;\n c = getchar();\n while (!isdigit(c))\n c = getchar();\n while (isdigit(c)) {\n ret *= 10;\n ret += c - '0';\n c = getchar();\n }\n return ret;\n}\n#define HASH(x) (x[0] + x[1] * 4 + x[2] * 16 + x[3] * 64)\nint dp[200][4][256];\nint rev[4][4];\nint board[5][5];\nint ten[4] = {0, 60, 70, 80};\nint nn;\nint solve(int now, int h) {\n if (dp[nn][now][h] != -1)\n return dp[nn][now][h];\n int ret = 0;\n int *x = rev[now];\n for (x[0] = 0; x[0] < 4; x[0]++) {\n for (x[1] = 0; x[1] < 4; x[1]++) {\n for (x[2] = 0; x[2] < 4; x[2]++) {\n for (x[3] = 0; x[3] < 4; x[3]++) {\n int point = 0;\n REP(i, 5) {\n int p = board[now][i];\n if (p == 0)\n continue;\n p = p - 1;\n if (now != 0) {\n if (i != 4)\n p += rev[now - 1][i];\n if (i != 0)\n p += rev[now - 1][i - 1];\n }\n if (i != 4)\n p += rev[now][i];\n if (i != 0)\n p += rev[now][i - 1];\n p = p % 4;\n point += ten[p];\n }\n if (now == 3) {\n REP(i, 5) {\n int p = board[now + 1][i];\n if (p == 0)\n continue;\n p = p - 1;\n if (i != 4)\n p += rev[now][i];\n if (i != 0)\n p += rev[now][i - 1];\n p = p % 4;\n point += ten[p];\n }\n ret = max(ret, point);\n } else {\n int hash = HASH(x);\n ret = max(ret, point + solve(now + 1, hash));\n }\n }\n }\n }\n }\n return dp[nn][now][h] = ret;\n}\nint main() {\n int n = getInt();\n memset(dp, -1, sizeof(dp));\n for (nn = 0; nn < n; nn++) {\n REP(i, 5) REP(j, 5) board[i][j] = getInt();\n print(solve(0, 0));\n }\n return 0;\n}", "accuracy": 1, "time_ms": 210, "memory_kb": 3916, "score_of_the_acc": -0.4768, "final_rank": 10 }, { "submission_id": "aoj_2221_7936376", "code_snippet": "#include <algorithm>\n#include <cassert>\n#include <climits>\n#include <cmath>\n#include <complex>\n#include <cstdio>\n#include <cstring>\n#include <iostream>\n#include <map>\n#include <numeric>\n#include <queue>\n#include <set>\n#include <sstream>\n#include <stack>\n#include <string>\n#include <vector>\n#define f first\n#define s second\n#define mp make_pair\n#define REP(i, n) for (int i = 0; i < (int)(n); i++)\n#define rep(i, s, n) for (int i = (s); i < (int)(n); i++)\n#define FOR(i, c) \\\n for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)\n#define ALL(c) (c).begin(), (c).end()\n#define IN(x, s, g) ((x) >= (s) && (x) < (g))\n#define ISIN(x, y, w, h) (IN((x), 0, (w)) && IN((y), 0, (h)))\n#define print(x) printf(\"%d\\n\", x)\n#define EPS (1e-4)\nusing namespace std;\ntypedef unsigned int uint;\ntypedef long long ll;\nconst int _dx[] = {0, 1, 0, -1};\nconst int _dy[] = {-1, 0, 1, 0};\nint getInt() {\n int ret = 0, c;\n c = getchar();\n while (!isdigit(c))\n c = getchar();\n while (isdigit(c)) {\n ret *= 10;\n ret += c - '0';\n c = getchar();\n }\n return ret;\n}\nint dp[4][256];\nint board[5][5];\nint ten[4] = {0, 60, 70, 80};\n#define H(h, x) ((h >> (x + x)) & 3)\nint solve(int now, int h) {\n if (dp[now][h] != -1)\n return dp[now][h];\n int ret = 0;\n int tmp[5] = {0};\n if (board[now][0] != 0)\n tmp[0] = 1 + ((board[now][0] - 1) + H(h, 0)) % 4;\n if (board[now][1] != 0)\n tmp[1] = 1 + ((board[now][1] - 1) + H(h, 0) + H(h, 1)) % 4;\n if (board[now][2] != 0)\n tmp[2] = 1 + ((board[now][2] - 1) + H(h, 1) + H(h, 2)) % 4;\n if (board[now][3] != 0)\n tmp[3] = 1 + ((board[now][3] - 1) + H(h, 2) + H(h, 3)) % 4;\n if (board[now][4] != 0)\n tmp[4] = 1 + ((board[now][4] - 1) + H(h, 3)) % 4;\n REP(x, 256) {\n int rtmp = 0;\n if (tmp[0] != 0)\n rtmp += ten[((tmp[0] - 1) + H(x, 0)) % 4];\n if (tmp[1] != 0)\n rtmp += ten[((tmp[1] - 1) + H(x, 0) + H(x, 1)) % 4];\n if (tmp[2] != 0)\n rtmp += ten[((tmp[2] - 1) + H(x, 1) + H(x, 2)) % 4];\n if (tmp[3] != 0)\n rtmp += ten[((tmp[3] - 1) + H(x, 2) + H(x, 3)) % 4];\n if (tmp[4] != 0)\n rtmp += ten[((tmp[4] - 1) + H(x, 3)) % 4];\n if (now == 3) {\n if (board[4][0] != 0)\n rtmp += ten[((board[4][0] - 1) + H(x, 0)) % 4];\n if (board[4][1] != 0)\n rtmp += ten[((board[4][1] - 1) + H(x, 0) + H(x, 1)) % 4];\n if (board[4][2] != 0)\n rtmp += ten[((board[4][2] - 1) + H(x, 1) + H(x, 2)) % 4];\n if (board[4][3] != 0)\n rtmp += ten[((board[4][3] - 1) + H(x, 2) + H(x, 3)) % 4];\n if (board[4][4] != 0)\n rtmp += ten[((board[4][4] - 1) + H(x, 3)) % 4];\n ret = max(ret, rtmp);\n } else {\n ret = max(ret, rtmp + solve(now + 1, x));\n }\n }\n return dp[now][h] = ret;\n}\nint main() {\n int n = getInt();\n while (n-- > 0) {\n memset(dp, -1, sizeof(dp));\n REP(i, 5) REP(j, 5) board[i][j] = getInt();\n print(solve(0, 0));\n }\n return 0;\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 3048, "score_of_the_acc": -0.314, "final_rank": 3 }, { "submission_id": "aoj_2221_7936375", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nint dp[40][1 << 14];\nint v[40];\nint pnt(int c) { return c > 0 ? 50 + c * 10 : 0; }\nint toBit(int c[7], int p) {\n int bit = 0;\n for (int i = 1; i < 7; i++) {\n bit = (bit << 2) | c[i];\n }\n bit = (bit << 2) | (~v[p + 7] ? v[p + 7] : 0);\n return bit;\n}\nvoid solve() {\n memset(v, -1, sizeof(v));\n for (int i = 0; i < 25; i++)\n cin >> v[i], v[i]--;\n memset(dp, -1, sizeof(dp));\n int start = 0;\n for (int i = 0; i < 7; i++)\n start = (start << 2) | (~v[i] ? v[i] : 0);\n dp[0][start] = 0;\n for (int i = 0; i < 25; i++) {\n for (int S = 0; S < (1 << 14); S++) {\n if (dp[i][S] == -1)\n continue;\n int c[7];\n for (int j = 0; j < 7; j++)\n c[6 - j] = S >> (j * 2) & 3;\n if (i % 5 == 4 || i >= 20) {\n dp[i + 1][toBit(c, i)] =\n max(dp[i + 1][toBit(c, i)], dp[i][S] + pnt(c[0]));\n continue;\n }\n for (int j = 0; j < 4; j++) {\n dp[i + 1][toBit(c, i)] =\n max(dp[i + 1][toBit(c, i)], dp[i][S] + pnt(c[0]));\n for (int k = 0; k < 7; k++)\n if (k < 2 || k >= 5) {\n if (~v[i + k])\n c[k] = (c[k] + 1) % 4;\n }\n }\n }\n }\n int ans = 0;\n for (int i = 0; i < (1 << 14); i++)\n ans = max(ans, dp[25][i]);\n cout << ans << endl;\n}\nint main() {\n int n;\n cin >> n;\n while (n--)\n solve();\n return 0;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 5576, "score_of_the_acc": -0.696, "final_rank": 16 }, { "submission_id": "aoj_2221_7936374", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nint dp[40][1 << 14];\nint v[40];\nint pnt(int c) { return c > 0 ? 50 + c * 10 : 0; }\nint toBit(int c[7], int p) {\n int bit = 0;\n for (int i = 1; i < 7; i++) {\n bit = (bit << 2) | c[i];\n }\n bit = (bit << 2) | (~v[p + 7] ? v[p + 7] : 0);\n return bit;\n}\nvoid solve() {\n memset(v, -1, sizeof(v));\n for (int i = 0; i < 25; i++)\n cin >> v[i], v[i]--;\n memset(dp, -1, sizeof(dp));\n int start = 0;\n for (int i = 0; i < 7; i++)\n start = (start << 2) | (~v[i] ? v[i] : 0);\n dp[0][start] = 0;\n for (int i = 0; i < 25; i++) {\n for (int S = 0; S < (1 << 14); S++) {\n if (dp[i][S] == -1)\n continue;\n int c[7];\n for (int j = 0; j < 7; j++)\n c[6 - j] = S >> (j * 2) & 3;\n int to = toBit(c, i);\n if (i % 5 == 4 || i >= 20) {\n dp[i + 1][to] = max(dp[i + 1][to], dp[i][S] + pnt(c[0]));\n continue;\n }\n for (int j = 0; j < 4; j++) {\n to = toBit(c, i);\n dp[i + 1][to] = max(dp[i + 1][to], dp[i][S] + pnt(c[0]));\n if (~v[i])\n c[0] = (c[0] + 1) % 4;\n if (~v[i + 1])\n c[1] = (c[1] + 1) % 4;\n if (~v[i + 5])\n c[5] = (c[5] + 1) % 4;\n if (~v[i + 6])\n c[6] = (c[6] + 1) % 4;\n }\n }\n }\n int ans = 0;\n for (int i = 0; i < (1 << 14); i++)\n ans = max(ans, dp[25][i]);\n cout << ans << endl;\n}\nint main() {\n int n;\n cin >> n;\n while (n--)\n solve();\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 5548, "score_of_the_acc": -0.6891, "final_rank": 15 }, { "submission_id": "aoj_2221_7936373", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nstruct NODE {\n int m[7];\n bool operator<(const NODE &n) const {\n for (int i = 0; i < 7; i++)\n if (m[i] != n.m[i])\n return m[i] < n.m[i];\n return false;\n }\n};\nint to[] = {0, 2, 3, 4, 1};\nint table[] = {0, 60, 10, 10, -80};\nint score[] = {0, 0, 60, 70, 80};\nint fld[5][5];\nvoid solve() {\n for (int i = 0; i < 5; i++)\n for (int j = 0; j < 5; j++)\n cin >> fld[i][j];\n map<NODE, int> prev;\n NODE start;\n for (int i = 0; i < 5; i++)\n start.m[6 - i] = fld[0][i];\n for (int i = 0; i < 2; i++)\n start.m[1 - i] = fld[1][i];\n int sum = 0;\n for (int i = 0; i < 5; i++)\n for (int j = 0; j < 5; j++)\n sum += score[fld[i][j]];\n prev[start] = sum;\n for (int i = 0; i < 4; i++) {\n for (int j = 0; j < 4; j++) {\n map<NODE, int> next;\n for (map<NODE, int>::iterator it = prev.begin(); it != prev.end(); it++) {\n NODE d = it->first;\n int val = it->second;\n for (int k = 0; k < 4; k++) {\n for (int l = 0; l < 7; l++)\n if (l < 2 || l >= 5) {\n val += table[d.m[l]];\n d.m[l] = to[d.m[l]];\n }\n NODE e = d;\n if (j != 3) {\n for (int l = 6; l > 0; l--)\n e.m[l] = e.m[l - 1];\n e.m[0] = fld[i + 1][j + 2];\n } else {\n for (int l = 6; l > 1; l--)\n e.m[l] = e.m[l - 2];\n e.m[1] = fld[i + 2][0];\n e.m[0] = fld[i + 2][1];\n }\n if (next.find(e) != next.end())\n next[e] = max(next[e], val);\n else\n next[e] = val;\n }\n }\n prev = next;\n }\n }\n int ans = 0;\n for (map<NODE, int>::iterator it = prev.begin(); it != prev.end(); it++) {\n ans = max(ans, it->second);\n }\n cout << ans << endl;\n}\nint main() {\n int n;\n cin >> n;\n while (n--)\n solve();\n return 0;\n}", "accuracy": 1, "time_ms": 280, "memory_kb": 3268, "score_of_the_acc": -0.3932, "final_rank": 5 }, { "submission_id": "aoj_2221_6478773", "code_snippet": "#pragma GCC optimize(\"Ofast\")\n#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <map>\n#include <queue>\n#include <cstdio>\n#include <ctime>\n#include <assert.h>\n#include <chrono>\n#include <random>\n#include <numeric>\n#include <set>\n#include <deque>\n#include <stack>\n#include <sstream>\n#include <utility>\n#include <cstring>\n#include <unordered_map>\n#include <unordered_set>\n#include <tuple>\n#include <array>\n#include <bitset>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned long long ull;\n\nmt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());\nll myRand(ll B) {\n return (ull)rng() % B;\n}\ninline double time() {\n return static_cast<long double>(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count()) * 1e-9;\n}\n\nint dp[17][1<<14];\n\nint main(){\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n int q; cin >> q;\n while(q--){\n int n = 25;\n vector<int> v(n+5,-1);\n for(int i=0;i<n;i++){\n cin >> v[i]; v[i]--;\n }\n for(int i=0;i<17;i++){\n for(int j=0;j<(1<<14);j++){\n dp[i][j] = -1;\n }\n }\n int bit = 0;\n for(int i=0;i<7;i++){\n bit <<= 2;\n bit |= (v[i]>=0?v[i]:0);\n }\n dp[0][bit] = 0;\n auto score=[&](int idx)->int{\n if(idx == -1)return 0;\n else if(idx == 0)return 0;\n else if(idx == 1)return 60;\n else if(idx == 2)return 70;\n else return 80;\n };\n for(int i=0;i<16;i++){\n for(int j=0;j<(1<<14);j++){\n if(dp[i][j] == -1)continue;\n array<int, 7> a;\n for(int k=0;k<7;k++){\n a[6-k] = (j>>(2*k))&3;\n }\n for(int k=0;k<7;k++){\n if(v[i/4*5+i%4+k] == -1)a[k] = -1;\n }\n auto cal=[](array<int, 7> &a, int nxt, int nx2 = 4)->int{\n int bit = 0;\n for(int i=1;i<7;i++){\n if(nx2 != 4 and i == 1)continue;\n bit <<= 2;\n bit |= (a[i]>=0?a[i]:0);\n }\n bit <<= 2;\n bit |= (nxt>=0?nxt:0);\n if(nx2 != 4){\n bit <<= 2;\n bit |= (nx2>=0?nx2:0);\n }\n return bit;\n };\n if(i < 12){\n for(int _=0;_<5;_++){\n int val = dp[i][j] + score(a[0]);\n if(i%4 == 3)val += score(a[1]);\n int nxt;\n if(i%4 != 3)nxt = cal(a,v[i/4*5+i%4+7]);\n else nxt = cal(a,v[i/4*5+i%4+7],v[i/4*5+i%4+8]);\n dp[i+1][nxt] = max(dp[i+1][nxt], val);\n for(int k=0;k<7;k++){\n if(a[k] == -1)continue;\n if(2 <= k and k < 5)continue;\n a[k]++;\n if(a[k] == 4)a[k] = 0;\n }\n }\n }\n else{\n for(int _=0;_<5;_++){\n int val = dp[i][j] + score(a[0]) + score(a[5]);\n if(i%4 == 3)val += score(a[1]) + score(a[6]);\n int nxt = cal(a,v[i/4*5+i%4+7]);\n dp[i+1][nxt] = max(dp[i+1][nxt], val);\n for(int k=0;k<7;k++){\n if(a[k] == -1)continue;\n if(2 <= k and k < 5)continue;\n a[k]++;\n if(a[k] == 4)a[k] = 0;\n }\n }\n }\n }\n }\n int res = 0;\n for(int i=0;i<(1<<14);i++){\n res = max(res,dp[16][i]);\n }\n cout << res << \"\\n\";\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 4232, "score_of_the_acc": -0.4788, "final_rank": 11 }, { "submission_id": "aoj_2221_4460896", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 1000000000000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\nint POW[5];\nint table[5][5];\nint dp[2][256];\n\nvoid func(){\n\n\tfor(int row = 0; row < 5; row++){\n\t\tfor(int col = 0; col < 5; col++){\n\t\t\tscanf(\"%d\",&table[row][col]);\n\t\t}\n\t}\n\n\tint CURRENT = 0,NEXT = 1;\n\tfor(int state = 0; state < POW[4]; state++){\n\n\t\tdp[CURRENT][state] = -BIG_NUM;\n\t\tdp[NEXT][state] = -BIG_NUM;\n\t}\n\n\n\tint work[5],work2[5];\n\n\t//1行目を計算\n\tfor(int state = 0; state < POW[4]; state++){\n\n\t\tfor(int i = 0; i < 5; i++){\n\n\t\t\twork[i] = table[0][i];\n\t\t}\n\n\t\tint tmp = state;\n\n\t\tfor(int i = 0; i <= 3; i++){\n\n\t\t\tint add = tmp/POW[3-i];\n\n\t\t\tif(work[i] != 0){\n\t\t\t\twork[i] += add;\n\t\t\t\tif(work[i] >= 5){\n\n\t\t\t\t\twork[i] -= 4;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif(work[i+1] != 0){\n\t\t\t\twork[i+1] += add;\n\t\t\t\tif(work[i+1] >= 5){\n\n\t\t\t\t\twork[i+1] -= 4;\n\t\t\t\t}\n\t\t\t}\n\t\t\ttmp %= POW[3-i];\n\t\t}\n\n\t\tint sum = 0;\n\t\tfor(int i = 0; i < 5; i++){\n\t\t\tswitch(work[i]){\n\t\t\tcase 0:\n\t\t\tcase 1:\n\t\t\t\t//Do nothing\n\t\t\t\tbreak;\n\t\t\tcase 2:\n\t\t\t\tsum += 60;\n\t\t\t\tbreak;\n\t\t\tcase 3:\n\t\t\t\tsum += 70;\n\t\t\t\tbreak;\n\t\t\tcase 4:\n\t\t\t\tsum += 80;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tdp[CURRENT][state] = sum;\n\t}\n\n\tint ans = 0;\n\n\tfor(int row = 1; row <= 4; row++){\n\t\tfor(int pre = 0; pre < POW[4]; pre++){\n\t\t\tif(dp[CURRENT][pre] == -BIG_NUM)continue;\n\n\t\t\tfor(int i = 0; i < 5; i++){\n\n\t\t\t\twork[i] = table[row][i];\n\t\t\t}\n\n\t\t\tint tmp = pre;\n\n\t\t\tfor(int i = 0; i <= 3; i++){\n\n\t\t\t\tint add = tmp/POW[3-i];\n\n\t\t\t\tif(work[i] != 0){\n\t\t\t\t\twork[i] += add;\n\t\t\t\t\tif(work[i] >= 5){\n\n\t\t\t\t\t\twork[i] -= 4;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tif(work[i+1] != 0){\n\t\t\t\t\twork[i+1] += add;\n\t\t\t\t\tif(work[i+1] >= 5){\n\n\t\t\t\t\t\twork[i+1] -= 4;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\ttmp %= POW[3-i];\n\t\t\t}\n\n\t\t\tint sum = 0;\n\t\t\tfor(int i = 0; i < 5; i++){\n\t\t\t\tswitch(work[i]){\n\t\t\t\tcase 0:\n\t\t\t\tcase 1:\n\t\t\t\t\t//Do nothing\n\t\t\t\t\tbreak;\n\t\t\t\tcase 2:\n\t\t\t\t\tsum += 60;\n\t\t\t\t\tbreak;\n\t\t\t\tcase 3:\n\t\t\t\t\tsum += 70;\n\t\t\t\t\tbreak;\n\t\t\t\tcase 4:\n\t\t\t\t\tsum += 80;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif(row == 4){\n\n\t\t\t\tans = max(ans,dp[CURRENT][pre]+sum);\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tfor(int next = 0; next < POW[4]; next++){\n\n\t\t\t\tfor(int k = 0; k < 5; k++){\n\n\t\t\t\t\twork2[k] = work[k];\n\t\t\t\t}\n\n\t\t\t\tint tmp2 = next;\n\n\t\t\t\tfor(int k = 0; k <= 3; k++){\n\n\t\t\t\t\tint add2 = tmp2/POW[3-k];\n\n\t\t\t\t\tif(work2[k] != 0){\n\t\t\t\t\t\twork2[k] += add2;\n\t\t\t\t\t\tif(work2[k] >= 5){\n\n\t\t\t\t\t\t\twork2[k] -= 4;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\tif(work2[k+1] != 0){\n\t\t\t\t\t\twork2[k+1] += add2;\n\t\t\t\t\t\tif(work2[k+1] >= 5){\n\n\t\t\t\t\t\t\twork2[k+1] -= 4;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\ttmp2 %= POW[3-k];\n\t\t\t\t}\n\n\t\t\t\tint sum2 = 0;\n\t\t\t\tfor(int k = 0; k < 5; k++){\n\t\t\t\t\tswitch(work2[k]){\n\t\t\t\t\tcase 0:\n\t\t\t\t\tcase 1:\n\t\t\t\t\t\t//Do nothing\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase 2:\n\t\t\t\t\t\tsum2 += 60;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase 3:\n\t\t\t\t\t\tsum2 += 70;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase 4:\n\t\t\t\t\t\tsum2 += 80;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tdp[NEXT][next] = max(dp[NEXT][next],dp[CURRENT][pre]+sum2);\n\t\t\t}\n\t\t}\n\t\tswap(CURRENT,NEXT);\n\t\tfor(int state = 0; state < POW[4]; state++){\n\n\t\t\tdp[NEXT][state] = -BIG_NUM;\n\t\t}\n\t}\n\n\tprintf(\"%d\\n\",ans);\n}\n\nint main(){\n\n\tPOW[0] = 1;\n\tfor(int i = 1; i < 5; i++){\n\n\t\tPOW[i] = POW[i-1]*4;\n\t}\n\n\tint num_case;\n\tscanf(\"%d\",&num_case);\n\n\tfor(int i = 0; i < num_case; i++){\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 760, "memory_kb": 3208, "score_of_the_acc": -0.5035, "final_rank": 13 }, { "submission_id": "aoj_2221_2955518", "code_snippet": "// 基本テンプレート\n \n#include <iostream>\n#include <iomanip>\n#include <cstdio>\n#include <string>\n#include <cstring>\n#include <deque>\n#include <list>\n#include <queue>\n#include <stack>\n#include <vector>\n#include <utility>\n#include <algorithm>\n#include <map>\n#include <set>\n#include <complex>\n#include <cmath>\n#include <limits>\n#include <cfloat>\n#include <climits>\n#include <ctime>\n#include <cassert>\n#include <numeric>\n#include <fstream>\n#include <functional>\nusing namespace std;\n \n#define rep(i,a,n) for(int (i)=(a); (i)<(n); (i)++)\n#define repq(i,a,n) for(int (i)=(a); (i)<=(n); (i)++)\n#define repr(i,a,n) for(int (i)=(a); (i)>=(n); (i)--)\n#define debug(...) fprintf(stderr, __VA_ARGS__)\n#define int long long int\n \ntemplate<typename T> void chmax(T &a, T b) {a = max(a, b);}\ntemplate<typename T> void chmin(T &a, T b) {a = min(a, b);}\ntemplate<typename T> void chadd(T &a, T b) {a = a + b;}\n \ntypedef pair<int, int> pii;\ntypedef long long ll;\n \nint dx[] = {0, 0, 1, -1};\nint dy[] = {1, -1, 0, 0};\nconst ll INF = 1001001001001001LL;\nconst ll MOD = 1000000007LL;\n\nconst int S = 4000;\nconst int N = 5;\nusing vector2D = vector< vector<int> >;\n\nint base_x[] = {0, 0, 1, 1};\nint base_y[] = {0, 1, 0, 1};\nvector< vector<int> > cand;\n\nvoid init_cand() {\n for(int i=0; i<4; i++) {\n for(int j=0; j<4; j++) {\n for(int k=0; k<4; k++) {\n for(int x=0; x<4; x++) {\n vector<int> v = {i, j, k, x};\n cand.push_back(v);\n }\n }\n }\n }\n}\n\nint get_hash(vector<int> v) {\n int ret = 0;\n for(int i=0; i<N; i++) {\n ret *= 5;\n ret += v[i];\n }\n return ret;\n}\n\nint get_point(vector<int> v) {\n int ret = 0;\n vector<int> pts = {0, 0, 60, 70, 80};\n for(int i=0; i<N; i++) {\n ret += pts[ v[i] ];\n }\n return ret;\n}\n\nint solve(vector2D board) {\n map< vector<int>, int > dp, swp;\n dp[ board[0] ] = 0;\n for(int k=0; k<4; k++) {\n for(auto e : dp) {\n vector<int> pat = e.first;\n for(auto v : cand) {\n vector< vector<int> > tmp(2, vector<int>(N));\n tmp[0] = pat;\n tmp[1] = board[k+1];\n\n for(int i=0; i<4; i++) {\n int shift = v[i];\n for(int j=0; j<4; j++) {\n int x = base_x[j], y = base_y[j] + i;\n\n if(tmp[x][y] == 0) continue;\n tmp[x][y] = (tmp[x][y] - 1 + shift) % 4 + 1;\n }\n }\n\n int score = dp[pat] + get_point(tmp[0]);\n if(k == 3) score += get_point(tmp[1]);\n\n vector<int> nxt = tmp[1];\n if(!swp.count(nxt) || swp[nxt] < score) {\n swp[nxt] = score;\n }\n }\n }\n\n swap(dp, swp);\n }\n\n int ans = 0;\n for(auto x : dp) {\n chmax(ans, x.second);\n }\n return ans;\n}\n \nsigned main() {\n init_cand();\n int T; cin >> T;\n while(T--) {\n vector2D board(N, vector<int>(N));\n for(int i=0; i<N; i++) {\n for(int j=0; j<N; j++) {\n cin >> board[i][j];\n }\n }\n\n cout << solve(board) << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 3710, "memory_kb": 3216, "score_of_the_acc": -1.2404, "final_rank": 18 }, { "submission_id": "aoj_2221_2656177", "code_snippet": "#include <stdio.h>\n#include <cmath>\n#include <algorithm>\n#include <cfloat>\n#include <stack>\n#include <queue>\n#include <vector>\n#include <string>\n#include <iostream>\n#include <set>\n#include <map>\n#include <time.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\nint table[5][5],push_table[256][4];\nint dp[3125],next_dp[3125];\nint index = 0,push_index = 0;\nmap<int,int>MAP,rev_MAP;\n\n\nint makeNUM(int array[5]){\n\treturn 10000*array[0]+1000*array[1]+100*array[2]+10*array[3]+array[4];\n}\n\nint getPoint(int num){\n\n\tswitch(num){\n\tcase 0:return 0;\n\tcase 1:return 0;\n\tcase 2:return 60;\n\tcase 3:return 70;\n\tcase 4:return 80;\n\t}\n\treturn -1;//must not reach here\n}\n\n\nvoid func(){\n\n\tfor(int row = 0; row < 5; row++){\n\t\tfor(int col = 0; col < 5; col++)scanf(\"%d\",&table[row][col]);\n\t}\n\n\tint work[2][5];\n\n\tfor(int i = 0; i < 3125; i++)dp[i] = -1;\n\n\n\tint sum,code;\n\n\tfor(int i = 0; i < 256; i++){\n\t\tfor(int a = 0; a < 2; a++){\n\t\t\tfor(int b = 0; b < 5; b++)work[a][b] = table[a][b];\n\t\t}\n\n\t\tfor(int k = 0; k < 4; k++){\n\t\t\tif(work[0][k] != 0){\n\t\t\t\twork[0][k] += push_table[i][k];\n\t\t\t\tif(work[0][k] > 4)work[0][k] -= 4;\n\t\t\t}\n\t\t\tif(work[0][k+1] != 0){\n\t\t\t\twork[0][k+1] += push_table[i][k];\n\t\t\t\tif(work[0][k+1] > 4)work[0][k+1] -= 4;\n\t\t\t}\n\t\t\tif(work[1][k] != 0){\n\t\t\t\twork[1][k] += push_table[i][k];\n\t\t\t\tif(work[1][k] > 4)work[1][k] -= 4;\n\t\t\t}\n\t\t\tif(work[1][k+1] != 0){\n\t\t\t\twork[1][k+1] += push_table[i][k];\n\t\t\t\tif(work[1][k+1] > 4)work[1][k+1] -= 4;\n\t\t\t}\n\t\t}\n\t\tsum = 0;\n\t\tfor(int k = 0; k < 5; k++)sum += getPoint(work[0][k]);\n\t\tcode = MAP[makeNUM(work[1])];\n\t\tdp[code] = max(dp[code],sum);\n\t}\n\n\n\tint number,tmp;\n\tint ans = 0;\n\n\tfor(int row = 1; row <= 3; row++){\n\n\t\tfor(int state = 0; state < 3125; state++)next_dp[state] = -1;\n\n\t\tfor(int pre_state = 0; pre_state < 3125; pre_state++){\n\t\t\tif(dp[pre_state] == -1)continue;\n\n\t\t\tnumber = rev_MAP[pre_state];\n\n\t\t\tfor(int i = 0; i < 256; i++){\n\t\t\t\ttmp = number;\n\t\t\t\tfor(int k = 0; k < 5; k++){\n\t\t\t\t\twork[0][4-k] = tmp%10;\n\t\t\t\t\ttmp /= 10;\n\t\t\t\t}\n\n\t\t\t\tfor(int k = 0; k < 5; k++){\n\t\t\t\t\twork[1][k] = table[row+1][k];\n\t\t\t\t}\n\n\t\t\t\tfor(int k = 0; k < 4; k++){\n\t\t\t\t\tif(work[0][k] != 0){\n\t\t\t\t\t\twork[0][k] += push_table[i][k];\n\t\t\t\t\t\tif(work[0][k] > 4)work[0][k] -= 4;\n\t\t\t\t\t}\n\t\t\t\t\tif(work[0][k+1] != 0){\n\t\t\t\t\t\twork[0][k+1] += push_table[i][k];\n\t\t\t\t\t\tif(work[0][k+1] > 4)work[0][k+1] -= 4;\n\t\t\t\t\t}\n\t\t\t\t\tif(work[1][k] != 0){\n\t\t\t\t\t\twork[1][k] += push_table[i][k];\n\t\t\t\t\t\tif(work[1][k] > 4)work[1][k] -= 4;\n\t\t\t\t\t}\n\t\t\t\t\tif(work[1][k+1] != 0){\n\t\t\t\t\t\twork[1][k+1] += push_table[i][k];\n\t\t\t\t\t\tif(work[1][k+1] > 4)work[1][k+1] -= 4;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tsum = 0;\n\t\t\t\tfor(int k = 0; k < 5; k++)sum += getPoint(work[0][k]);\n\n\t\t\t\tif(row <= 2){\n\t\t\t\t\tcode = MAP[makeNUM(work[1])];\n\n\t\t\t\t\tnext_dp[code] = max(next_dp[code],sum+dp[pre_state]);\n\n\t\t\t\t}else{\n\t\t\t\t\tfor(int k = 0; k < 5; k++)sum += getPoint(work[1][k]);\n\t\t\t\t\tans = max(ans,sum+dp[pre_state]);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tfor(int state = 0; state < 3125; state++)dp[state] = next_dp[state];\n\t}\n\n\tprintf(\"%d\\n\",ans);\n}\n\n\nvoid recursive(int array[5],int count){\n\n\tif(count == 5){\n\t\tint new_num = makeNUM(array);\n\t\tMAP[new_num] = index;\n\t\trev_MAP[index] = new_num;\n\t\tindex++;\n\t\treturn;\n\t}\n\n\tfor(int i = 0; i <= 4; i++){\n\t\tint next_array[5];\n\t\tfor(int k = 0; k < count; k++)next_array[k] = array[k];\n\t\tnext_array[count] = i;\n\t\trecursive(next_array,count+1);\n\t}\n}\n\nvoid makeCode(){\n\tint first_array[5];\n\trecursive(first_array,0);\n}\n\n\nvoid makePush(int array[4],int count){\n\n\tif(count == 4){\n\t\tfor(int i = 0; i < 4; i++){\n\t\t\tpush_table[push_index][i] = array[i];\n\t\t}\n\t\tpush_index++;\n\t\treturn;\n\t}\n\n\tfor(int i = 0; i <= 3; i++){\n\t\tint next_array[4];\n\t\tfor(int k = 0; k < count; k++)next_array[k] = array[k];\n\t\tnext_array[count] = i;\n\t\tmakePush(next_array,count+1);\n\t}\n}\n\nint main(){\n\n\tmakeCode();\n\n\tint first_array[4];\n\tmakePush(first_array,0);\n\n\tint case_num;\n\tscanf(\"%d\",&case_num);\n\n\tfor(int i = 0; i < case_num; i++)func();\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 470, "memory_kb": 3520, "score_of_the_acc": -0.4799, "final_rank": 12 }, { "submission_id": "aoj_2221_2196856", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)\n#define rep(i,n) repl(i,0,n)\n#define mp(a,b) make_pair((a),(b))\n#define pb(a) push_back((a))\n#define all(x) (x).begin(),(x).end()\n#define uniq(x) sort(all(x)),(x).erase(unique(all(x)),end(x))\n#define fi first\n#define se second\n#define dbg(x) cout<<#x\" = \"<<((x))<<endl\ntemplate<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<\"(\"<<p.fi<<\",\"<<p.se<<\")\";return o;}\ntemplate<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<\"[\";for(T t:v){o<<t<<\",\";}o<<\"]\";return o;}\n\n#define INF 2147483600\n#define long long long // for codeforces\n\nint solve(){\n vector<vector<int>> vec(5,vector<int>(5));\n rep(i,5)rep(j,5) scanf(\"%d\", &vec[i][j]);\n rep(i,5)rep(j,5) vec[i][j]--;\n\n const int masks[] = {0x3fc, 0x3f3, 0x3cf, 0x33f, 0x0ff};\n const int points[] ={0,60,70,80};\n\n vector<vector<int>> dp(1<<10, vector<int>(4, -1));\n dp[0][0] = 0;\n // dp[mask][i] : 2bit?????¨??¨???????????????????????????????????¨??????????????¨???????¢???????????????§????????¢\n // i : ?????????????????¢????????????????????????\n\n rep(i,4)rep(j,4){\n vector<vector<int>> nxt(1<<10, vector<int>(4, -1));\n rep(mask, 1<<10) rep(k,4) if(dp[mask][k]>=0){\n int l = (mask>>(j*2))%4;\n int r = (mask>>(j*2+2))%4;\n rep(d,4){ // # switch push\n int pl=0,pr=0;\n if(vec[i][j]!=-1) pl = points[(vec[i][j]+l+k+d)%4];\n if(vec[i][j+1]!=-1) pr = points[(vec[i][j+1]+r+d)%4];\n int nmask = (mask&masks[j]) + (((k+d)%4)<<(j*2));\n if(j<3) nxt[nmask][d] = max(nxt[nmask][d], dp[mask][k] + pl);\n if(j==3){\n nmask = (nmask&masks[4]) + (d<<8);\n nxt[nmask][0] = max(nxt[nmask][0], dp[mask][k] + pl + pr);\n }\n }\n }\n swap(nxt, dp);\n }\n\n int ans = 0;\n rep(_mask, 1<<10){\n int mask = _mask;\n int crnt=dp[_mask][0];\n if(crnt<0) crnt=0;\n rep(i,5){\n int d = mask%4;\n mask /= 4;\n if(vec[4][i]!=-1){\n crnt += points[(vec[4][i]+d)%4];\n }\n }\n ans = max(ans, crnt);\n }\n return ans;\n}\n\nint main(){\n int n;\n cin>>n;\n rep(_,n) cout<<solve()<<endl;\n\n return 0;\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 3280, "score_of_the_acc": -0.3427, "final_rank": 4 }, { "submission_id": "aoj_2221_2134013", "code_snippet": "#include<iostream>\n#include<algorithm>\nusing namespace std;\nint dp[6][3125], n, x[7][5], pp[5] = { 0,0,60,70,80 };\nint power4[6] = { 1,4,16,64,256,1024 }, power5[6] = { 1,5,25,125,625,3125 };\nint main() {\n\tcin >> n;\n\tfor (int i = 0; i < n; i++) {\n\t\tfor (int j = 0; j < 5; j++) {\n\t\t\tfor (int k = 0; k < 5; k++)cin >> x[j][k];\n\t\t}\n\t\tfor (int j = 0; j < 6; j++) { for (int k = 0; k < 3125; k++) { dp[j][k] = -1; } }\n\t\tfor (int j = 0; j < 256; j++) {\n\t\t\tint bit[4]; for (int k = 0; k < 4; k++)bit[k] = (j / power4[k]) % 4;\n\t\t\tint y[7][5]; for (int k = 0; k < 35; k++)y[k / 5][k % 5] = x[k / 5][k % 5];\n\t\t\tfor (int k = 0; k < 4; k++) {\n\t\t\t\tfor (int l = 0; l < bit[k]; l++) {\n\t\t\t\t\tif (y[0][k] >= 1) { y[0][k]++; if (y[0][k] == 5)y[0][k] -= 4; }\n\t\t\t\t\tif (y[1][k] >= 1) { y[1][k]++; if (y[1][k] == 5)y[1][k] -= 4; }\n\t\t\t\t\tif (y[0][k + 1] >= 1) { y[0][k + 1]++; if (y[0][k + 1] == 5)y[0][k + 1] -= 4; }\n\t\t\t\t\tif (y[1][k + 1] >= 1) { y[1][k + 1]++; if (y[1][k + 1] == 5)y[1][k + 1] -= 4; }\n\t\t\t\t}\n\t\t\t}\n\t\t\tint W = 0; for (int k = 0; k < 5; k++)W += power5[k] * y[1][k];\n\t\t\tint W2 = 0; for (int k = 0; k < 5; k++)W2 += pp[y[0][k]];\n\t\t\tdp[0][W] = max(dp[0][W], W2);\n\t\t}\n\t\tfor (int j = 1; j <= 3; j++) {\n\t\t\tfor (int k = 0; k < 3125; k++) {\n\t\t\t\tif (dp[j - 1][k] == -1)continue;\n\t\t\t\tint bit[5]; for (int l = 0; l < 5; l++)bit[l] = (k / power5[l]) % 5;\n\t\t\t\tfor (int l = 0; l < 256; l++) {\n\t\t\t\t\tint y[7][5]; for (int m = 0; m < 35; m++)y[m / 5][m % 5] = x[m / 5][m % 5];\n\t\t\t\t\tfor (int m = 0; m < 5; m++)y[j][m] = bit[m];\n\t\t\t\t\tint bit2[4]; for (int m = 0; m < 4; m++) { bit2[m] = (l / power4[m]) % 4; }\n\t\t\t\t\tfor (int m = 0; m < 4; m++) {\n\t\t\t\t\t\tfor (int o = 0; o < bit2[m]; o++) {\n\t\t\t\t\t\t\tif (y[j + 0][m + 0] >= 1) { y[j + 0][m + 0]++; if (y[j + 0][m + 0] == 5)y[j + 0][m + 0] -= 4; }\n\t\t\t\t\t\t\tif (y[j + 1][m + 0] >= 1) { y[j + 1][m + 0]++; if (y[j + 1][m + 0] == 5)y[j + 1][m + 0] -= 4; }\n\t\t\t\t\t\t\tif (y[j + 0][m + 1] >= 1) { y[j + 0][m + 1]++; if (y[j + 0][m + 1] == 5)y[j + 0][m + 1] -= 4; }\n\t\t\t\t\t\t\tif (y[j + 1][m + 1] >= 1) { y[j + 1][m + 1]++; if (y[j + 1][m + 1] == 5)y[j + 1][m + 1] -= 4; }\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tint W = 0; for (int m = 0; m < 5; m++) W += power5[m] * y[j + 1][m];\n\t\t\t\t\tint W2 = 0; for (int m = 0; m < 5; m++)W2 += pp[y[j][m]];\n\t\t\t\t\tdp[j][W] = max(dp[j][W], dp[j - 1][k] + W2);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tint maxn = 0;\n\t\tfor (int j = 0; j < 3125; j++) {\n\t\t\tif (dp[3][j] == -1)continue;\n\t\t\tint sum1 = 0; for (int k = 0; k < 5; k++)sum1 += pp[(j / power5[k]) % 5];\n\t\t\tmaxn = max(maxn, dp[3][j] + sum1);\n\t\t}\n\t\tcout << maxn << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 890, "memory_kb": 3156, "score_of_the_acc": -0.5278, "final_rank": 14 }, { "submission_id": "aoj_2221_1870325", "code_snippet": "#include \"bits/stdc++.h\"\n#include<unordered_map>\n#include<unordered_set>\n#pragma warning(disable:4996)\nusing namespace std;\nusing ld = long double;\ntemplate<class T>\nusing Table = vector<vector<T>>;\nconst ld eps=1e-9;\n\n//// < \"D:\\D_Download\\Visual Studio 2015\\Projects\\programing_contest_c++\\Debug\\a.txt\" > \"D:\\D_Download\\Visual Studio 2015\\Projects\\programing_contest_c++\\Debug\\b.answer\"\n\nmap<int, int>mp;\n\nint getans(const vector<vector<int>>&field,const int row, const map<vector<int>, int>&memo) {\n\tif (row == 4) {\n\t\tint ans = 0;\n\t\tfor (auto m : memo) {\n\t\t\tvector<int>changed(m.first);\n\t\t\tint nextscore(m.second);\n\t\t\tfor (int j = 0; j < 5; ++j) {\n\t\t\t\tif (field[row][j]) {\n\t\t\t\t\tconst int number = (field[row][j] + changed[j]) % 4;\n\t\t\t\t\tnextscore += mp[number];\n\t\t\t\t}\n\t\t\t}\n\t\t\tans = max(ans, nextscore);\n\t\t\t\n\t\t}\n\t\treturn ans;\n\t}\n\tmap<vector<int>, int>newmemo;\n\tfor (auto m : memo) {\n\t\tfor (int i = 0; i < 256; ++i) {\n\t\t\tint num(i);\n\t\t\tvector<int>push(4);\n\t\t\tfor (int j = 0; j < 4; ++j) {\n\t\t\t\tpush[j] = num % 4;\n\t\t\t\tnum /= 4;\n\t\t\t}\n\t\t\tvector<int>fliped(5);\n\t\t\tfor (int j = 0; j < 4; ++j) {\n\t\t\t\tfliped[j] += push[j];\n\t\t\t\tfliped[j + 1] += push[j];\n\t\t\t}\n\t\t\tvector<int>changed(m.first);\n\t\t\tfor (int j = 0; j < 5; ++j) {\n\t\t\t\tchanged[j] += fliped[j];\n\t\t\t}\n\t\t\tfor (int j = 0; j < 5; ++j)changed[j] %= 4;\n\t\t\tint nextscore(m.second);\n\t\t\tfor (int j = 0; j < 5; ++j) {\n\t\t\t\tif (field[row][j]) {\n\t\t\t\t\tconst int number = (field[row][j] + changed[j]) % 4;\n\t\t\t\t\tnextscore += mp[number];\n\t\t\t\t}\n\t\t\t}\n\t\t\tnewmemo[fliped] = max(newmemo[fliped], nextscore);\n\t\t}\n\t}\n\treturn getans(field, row + 1, newmemo);\n\t\n}\n\nint getans(const vector<vector<int>>field) {\n\tmap<vector<int>, int>amap;\n\tamap[vector<int>(5, 0)] = 0;\n\treturn getans(field, 0, amap);\n}\n\nint main() {\n\tmp[1] = 0;\n\tmp[2] = 60;\n\tmp[3] = 70;\n\tmp[0] = 80;\n\tint N; cin >> N;\n\twhile (N--) {\n\t\tvector<vector<int>>field(5, vector<int>(5));\n\t\tfor (int y = 0; y < 5; ++y) {\n\t\t\tfor (int x = 0; x < 5; ++x) {\n\t\t\t\tcin >> field[y][x];\n\t\t\t}\n\t\t}\n\t\tint ans = getans(field);\n\t\tcout << ans << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 4030, "memory_kb": 3200, "score_of_the_acc": -1.3177, "final_rank": 19 }, { "submission_id": "aoj_2221_1607988", "code_snippet": "// template {{{\n\n#include <bits/stdc++.h>\nusing namespace std;\n\n#define loop(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rep(i, n) loop(i, 0, n)\n#define rloop(i, a, b) for (int i = (int)(b) - 1; i >= (int)(a); i--)\n#define rrep(i, n) rloop(i, 0, n)\n#define eb emplace_back\n#define ef emplace_front\n#define pb pop_back\n#define pf pop_front\n#define all(c) std::begin(c), std::end(c)\n#define mp std::make_pair\n#define mt std::make_tuple\n#define fi first\n#define se second\n#define popcnt __builtin_popcountll\n\nusing uint = unsigned;\nusing ll = long long;\nusing ull = unsigned long long;\nusing ld = long double;\nusing vi = vector<int>;\nusing vvi = vector<vi>;\nusing vl = vector<ll>;\nusing vvl = vector<vl>;\nusing vd = vector<double>;\nusing vvd = vector<vd>;\nusing pi = pair<int, int>;\nusing pl = pair<ll, ll>;\nusing pd = pair<double, double>;\nusing tp = tuple<int, int, int>;\nusing tp4 = tuple<int, int, int, int>;\nusing tp5 = tuple<int, int, int, int, int>;\n\ntemplate<typename T>\nusing max_pq = priority_queue<T, vector<T>, less<T>>;\n\ntemplate<typename T>\nusing min_pq = priority_queue<T, vector<T>, greater<T>>;\n\nconst int MOD = 1e9 + 7;\nconst int INF = 1e9 + 10;\nconst ll LLINF = 1e18 + 10;\n\nconst int dx[] = {-1, 0, 1, 0};\nconst int dy[] = {0, -1, 0, 1};\nconst int dx8[] = {-1, -1, 0, 1, 1, 1, 0, -1};\nconst int dy8[] = {0, -1, -1, -1, 0, 1, 1, 1};\n\ntemplate<typename T>\ninline T sq(T x){ return x * x; }\n\ntemplate<typename T, typename U>\ninline bool chmax(T &x, U y){ if (x >= y) return false; x = y; return true; }\n\ntemplate<typename T, typename U>\ninline bool chmin(T &x, U y){ if (x <= y) return false; x = y; return true; }\n\ntemplate<typename T>\ninline void sort(T &c){ std::sort(std::begin(c), std::end(c)); }\n\ntemplate<typename T>\ninline void reverse(T &c){ std::reverse(std::begin(c), std::end(c)); }\n\ntemplate<typename T>\ninline void unique(T &c){ std::sort(std::begin(c), std::end(c)); c.erase(std::unique(all(c)), std::end(c)); }\n\n// }}}\n\nconst int t[] = {0, 60, 70, 80};\nbool f[5][5];\nint a[5][5];\nint dp[17][65536];\n\nint calc(int p, int st)\n{\n\tif (~dp[p][st]) return dp[p][st];\n\tif (p == 16) return 0;\n\t\n\tint res = 0;\n\tint ds = 0;\n\tint x = p / 4, y = p % 4;\n\trep(i, 4){\n\t\tchmax(res, ds + calc(p + 1, (st * 4 + i) % 65536));\n\t\trep(dx, 2) rep(dy, 2){\n\t\t\tif (f[x + dx][y + dy]){\n\t\t\t\tds -= t[a[x + dx][y + dy]];\n\t\t\t\t++a[x + dx][y + dy] %= 4;\n\t\t\t\tds += t[a[x + dx][y + dy]];\n\t\t\t}\n\t\t}\n\t}\n\n\treturn dp[p][st] = res;\n}\n\nint main()\n{\n\tint n;\n\tscanf(\"%d\", &n);\n\twhile (n--){\n\t\trep(i, 5) rep(j, 5) scanf(\"%d\", &a[i][j]), a[i][j]--;\n\t\trep(i, 5) rep(j, 5) f[i][j] = a[i][j] >= 0;\n\t\tint sum = 0;\n\t\trep(i, 5) rep(j, 5){\n\t\t\tif (f[i][j]) sum += t[a[i][j]];\n\t\t}\n\t\tmemset(dp, -1, sizeof(dp));\n\t\tprintf(\"%d\\n\", sum + calc(0, 0));\n\t}\n}", "accuracy": 1, "time_ms": 1800, "memory_kb": 7572, "score_of_the_acc": -1.4439, "final_rank": 20 }, { "submission_id": "aoj_2221_1498083", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\ninline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}\ntemplate<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}\ntemplate<class T> inline T sqr(T x) {return x*x;}\n\ntypedef vector<int> vi;\ntypedef vector<vi> vvi;\ntypedef vector<string> vs;\ntypedef pair<int, int> pii;\ntypedef long long ll;\n\n#define all(a) (a).begin(),(a).end()\n#define rall(a) (a).rbegin(), (a).rend()\n#define pb push_back\n#define mp make_pair\n#define each(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)\n#define exist(s,e) ((s).find(e)!=(s).end())\n#define range(i,a,b) for(int i=(a);i<(b);++i)\n#define rep(i,n) range(i,0,n)\n#define clr(a,b) memset((a), (b) ,sizeof(a))\n#define dump(x) cerr << #x << \" = \" << (x) << endl;\n#define debug(x) cerr << #x << \" = \" << (x) << \" (L\" << __LINE__ << \")\" << \" \" << __FILE__ << endl;\n\nconst double eps = 1e-10;\nconst double pi = acos(-1.0);\nconst ll INF =1LL << 62;\nconst int inf =1 << 29;\n\nint table[5][5];\nint dp[5][1<<8];\nint score[4]={0,60,70,80};\n\nint main(void){\n\tint T;\n\tcin >> T;\n\trep(loop,T){\n\t\trep(i,5)rep(j,5) cin >> table[i][j];\n\t\trep(i,5)rep(j,1<<8) dp[i][j]=-1;\n\t\tdp[0][0]=0;\n\t\trep(i,4)rep(mask,1<<8){\n\t\t\tif(dp[i][mask]==-1) continue;\n\t\t\trep(mask2,1<<8){\n\t\t\t\tint sw[4]={0,0,0,0};\n\t\t\t\trep(j,4) sw[j]+=(mask>>(2*j))&3;\n\t\t\t\trep(j,4) sw[j]+=(mask2>>(2*j))&3;\n\t\t\t\trep(j,4) sw[j]%=4;\n\t\t\t\tint cur=0;\n\t\t\t\trep(j,5){\n\t\t\t\t\tif(table[i][j]==0)continue;\n\t\t\t\t\tint index=table[i][j]-1;\n\t\t\t\t\tif(j-1>=0) index=(index+sw[j-1])%4;\n\t\t\t\t\tif(j<4) index=(index+sw[j])%4;\n\t\t\t\t\tcur+=score[index];\n\t\t\t\t}\n\t\t\t\tdp[i+1][mask2]=max(dp[i+1][mask2],dp[i][mask]+cur);\n\t\t\t}\n\t\t}\n\t\tint ans=0;\n\t\trep(mask,1<<8){\n\t\t\tint sw[4]={0,0,0,0};\n\t\t\trep(j,4) sw[j]+=(mask>>(2*j))&3;\n\t\t\trep(j,4) sw[j]%=4;\n\t\t\tint cur=0;\n\t\t\trep(j,5){\n\t\t\t\tif(table[4][j]==0)continue;\n\t\t\t\tint index=table[4][j]-1;\n\t\t\t\tif(j-1>=0) index=(index+sw[j-1])%4;\n\t\t\t\tif(j<4) index=(index+sw[j])%4;\n\t\t\t\tcur+=score[index];\n\t\t\t}\n\t\t\tans=max(ans,dp[4][mask]+cur);\n\t\t}\n\t\tcout << ans << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 340, "memory_kb": 1164, "score_of_the_acc": -0.0798, "final_rank": 1 }, { "submission_id": "aoj_2221_1492708", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nint dp[40][1<<14];\nint v[40];\n\ninline int pnt(int c){\n return c>0?50+c*10:0;\n}\n\nint toBit(int c[7],int p){\n int bit=0;\n for(int i=1;i<7;i++){\n bit=(bit<<2)|c[i];\n }\n bit=(bit<<2)|(~v[p+7]?v[p+7]:0);\n return bit;\n}\n\nvoid solve(){\n memset(v,-1,sizeof(v));\n for(int i=0;i<25;i++)cin>>v[i],v[i]--;\n memset(dp,-1,sizeof(dp));\n int start=0;\n for(int i=0;i<7;i++)start=(start<<2)|(~v[i]?v[i]:0);\n dp[0][start]=0;\n for(int i=0;i<25;i++){\n\n for(int S=0;S<(1<<14);S++){\n if(dp[i][S]==-1)continue;\n int c[7];\n for(int j=0;j<7;j++)c[6-j]=S>>(j*2)&3;\n\n int to=toBit(c,i);\n if(i%5==4||i>=20){\n dp[i+1][to]=max(dp[i+1][to],dp[i][S]+pnt(c[0]));\n continue;\n }\n for(int j=0;j<4;j++){\n to=toBit(c,i);\n dp[i+1][to]=max(dp[i+1][to],dp[i][S]+pnt(c[0]));\n\n if(~v[i])c[0]=(c[0]+1)%4;\n if(~v[i+1])c[1]=(c[1]+1)%4;\n if(~v[i+5])c[5]=(c[5]+1)%4;\n if(~v[i+6])c[6]=(c[6]+1)%4;\n }\n\n }\n }\n\n int ans=0;\n for(int i=0;i<(1<<14);i++)ans=max(ans,dp[25][i]);\n\n cout<<ans<<endl;\n\n}\n\nint main(){\n int n;cin>>n;\n while(n--)solve();\n return 0;\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 3724, "score_of_the_acc": -0.412, "final_rank": 6 }, { "submission_id": "aoj_2221_1492707", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nint dp[40][1<<14];\nint v[40];\n\ninline int pnt(int c){\n return c>0?50+c*10:0;\n}\n\nint toBit(int c[7],int p){\n int bit=0;\n for(int i=1;i<7;i++){\n bit=(bit<<2)|c[i];\n }\n bit=(bit<<2)|(~v[p+7]?v[p+7]:0);\n return bit;\n}\n\nvoid solve(){\n memset(v,-1,sizeof(v));\n for(int i=0;i<25;i++)cin>>v[i],v[i]--;\n memset(dp,-1,sizeof(dp));\n int start=0;\n for(int i=0;i<7;i++)start=(start<<2)|(~v[i]?v[i]:0);\n dp[0][start]=0;\n for(int i=0;i<25;i++){\n\n for(int S=0;S<(1<<14);S++){\n if(dp[i][S]==-1)continue;\n int c[7];\n for(int j=0;j<7;j++)c[6-j]=S>>(j*2)&3;\n\n int to=((S<<2)&((1<<14)-1))|(~v[i+7]?v[i+7]:0);\n if(i%5==4||i>=20){\n dp[i+1][to]=max(dp[i+1][to],dp[i][S]+pnt(c[0]));\n continue;\n }\n for(int j=0;j<4;j++){\n to=toBit(c,i);\n dp[i+1][to]=max(dp[i+1][to],dp[i][S]+pnt(c[0]));\n\n if(~v[i])c[0]=(c[0]+1)%4;\n if(~v[i+1])c[1]=(c[1]+1)%4;\n if(~v[i+5])c[5]=(c[5]+1)%4;\n if(~v[i+6])c[6]=(c[6]+1)%4;\n }\n\n }\n }\n\n int ans=0;\n for(int i=0;i<(1<<14);i++)ans=max(ans,dp[25][i]);\n\n cout<<ans<<endl;\n\n}\n\nint main(){\n int n;cin>>n;\n while(n--)solve();\n return 0;\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 3728, "score_of_the_acc": -0.4126, "final_rank": 7 }, { "submission_id": "aoj_2221_1492701", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nint dp[40][1<<14];\nint v[40];\n\nint pnt(int c){\n return c>0?50+c*10:0;\n}\n\nint toBit(int c[7],int p){\n int bit=0;\n for(int i=1;i<7;i++){\n bit=(bit<<2)|c[i];\n }\n bit=(bit<<2)|(~v[p+7]?v[p+7]:0);\n return bit;\n}\n\nvoid solve(){\n memset(v,-1,sizeof(v));\n for(int i=0;i<25;i++)cin>>v[i],v[i]--;\n memset(dp,-1,sizeof(dp));\n int start=0;\n for(int i=0;i<7;i++)start=(start<<2)|(~v[i]?v[i]:0);\n dp[0][start]=0;\n for(int i=0;i<25;i++){\n\n for(int S=0;S<(1<<14);S++){\n if(dp[i][S]==-1)continue;\n int c[7];\n for(int j=0;j<7;j++)c[6-j]=S>>(j*2)&3;\n\n int to=toBit(c,i);\n if(i%5==4||i>=20){\n dp[i+1][to]=max(dp[i+1][to],dp[i][S]+pnt(c[0]));\n continue;\n }\n for(int j=0;j<4;j++){\n to=toBit(c,i);\n dp[i+1][to]=max(dp[i+1][to],dp[i][S]+pnt(c[0]));\n\n if(~v[i])c[0]=(c[0]+1)%4;\n if(~v[i+1])c[1]=(c[1]+1)%4;\n if(~v[i+5])c[5]=(c[5]+1)%4;\n if(~v[i+6])c[6]=(c[6]+1)%4;\n }\n\n }\n }\n\n int ans=0;\n for(int i=0;i<(1<<14);i++)ans=max(ans,dp[25][i]);\n\n cout<<ans<<endl;\n\n}\n\nint main(){\n int n;cin>>n;\n while(n--)solve();\n return 0;\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 3728, "score_of_the_acc": -0.4126, "final_rank": 7 }, { "submission_id": "aoj_2221_1492700", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nint dp[40][1<<14];\nint v[40];\n\nint pnt(int c){\n return c>0?50+c*10:0;\n}\n\nint toBit(int c[7],int p){\n int bit=0;\n for(int i=1;i<7;i++){\n bit=(bit<<2)|c[i];\n }\n bit=(bit<<2)|(~v[p+7]?v[p+7]:0);\n return bit;\n}\n\nvoid solve(){\n memset(v,-1,sizeof(v));\n for(int i=0;i<25;i++)cin>>v[i],v[i]--;\n memset(dp,-1,sizeof(dp));\n int start=0;\n for(int i=0;i<7;i++)start=(start<<2)|(~v[i]?v[i]:0);\n dp[0][start]=0;\n for(int i=0;i<25;i++){\n\n for(int S=0;S<(1<<14);S++){\n if(dp[i][S]==-1)continue;\n int c[7];\n for(int j=0;j<7;j++)c[6-j]=S>>(j*2)&3;\n\n if(i%5==4||i>=20){\n dp[i+1][toBit(c,i)]=max(dp[i+1][toBit(c,i)],dp[i][S]+pnt(c[0]));\n continue;\n }\n for(int j=0;j<4;j++){\n dp[i+1][toBit(c,i)]=max(dp[i+1][toBit(c,i)],dp[i][S]+pnt(c[0]));\n\n for(int k=0;k<7;k++)if(k<2||k>=5){\n if(~v[i+k])c[k]=(c[k]+1)%4;\n }\n }\n\n }\n }\n\n int ans=0;\n for(int i=0;i<(1<<14);i++)ans=max(ans,dp[25][i]);\n\n cout<<ans<<endl;\n\n}\n\nint main(){\n int n;cin>>n;\n while(n--)solve();\n return 0;\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 3728, "score_of_the_acc": -0.4151, "final_rank": 9 }, { "submission_id": "aoj_2221_1492217", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nstruct NODE{\n int m[7];\n\n bool operator<(const NODE &n)const{\n for(int i=0;i<7;i++)if(m[i]!=n.m[i])return m[i]<n.m[i];\n return false;\n }\n};\n\nint to[]={0,2,3,4,1};\nint table[]={0,60,10,10,-80};\nint score[]={0,0,60,70,80};\nint fld[5][5];\n\nvoid solve(){\n for(int i=0;i<5;i++)\n for(int j=0;j<5;j++)\n cin>>fld[i][j];\n\n map<NODE,int>prev;\n NODE start;\n for(int i=0;i<5;i++)start.m[6-i]=fld[0][i];\n for(int i=0;i<2;i++)start.m[1-i]=fld[1][i];\n int sum=0;\n for(int i=0;i<5;i++)\n for(int j=0;j<5;j++)\n sum+=score[fld[i][j]];\n prev[start]=sum;\n\n for(int i=0;i<4;i++){\n for(int j=0;j<4;j++){\n map<NODE,int>next;\n for(map<NODE,int>::iterator it=prev.begin();it!=prev.end();it++){\n\n NODE d=it->first;\n int val=it->second;\n for(int k=0;k<4;k++){\n for(int l=0;l<7;l++)if(l<2||l>=5){\n\n val+=table[d.m[l]];\n d.m[l]=to[d.m[l]];\n }\n NODE e=d;\n if(j!=3){\n for(int l=6;l>0;l--)e.m[l]=e.m[l-1];\n e.m[0]=fld[i+1][j+2];\n }\n else{\n for(int l=6;l>1;l--)e.m[l]=e.m[l-2];\n e.m[1]=fld[i+2][0];\n e.m[0]=fld[i+2][1];\n }\n if(next.find(e)!=next.end())next[e]=max(next[e],val);\n else next[e]=val;\n }\n }\n prev=next;\n }\n\n }\n\n int ans=0;\n for(map<NODE,int>::iterator it=prev.begin();it!=prev.end();it++){\n ans=max(ans,it->second);\n }\n\n cout<<ans<<endl;\n\n\n}\nint main(){\n int n;\n cin>>n;\n\n while(n--)solve();\n return 0;\n}", "accuracy": 1, "time_ms": 260, "memory_kb": 1356, "score_of_the_acc": -0.0898, "final_rank": 2 } ]
aoj_2223_cpp
Problem B: Kaeru Jump There is a frog living in a big pond. He loves jumping between lotus leaves floating on the pond. Interestingly, these leaves have strange habits. First, a leaf will sink into the water after the frog jumps from it. Second, they are aligned regularly as if they are placed on the grid points as in the example below. Figure 1: Example of floating leaves Recently, He came up with a puzzle game using these habits. At the beginning of the game, he is on some leaf and faces to the upper, lower, left or right side. He can jump forward or to the left or right relative to his facing direction, but not backward or diagonally. For example, suppose he is facing to the left side, then he can jump to the left, upper and lower sides but not to the right side. In each jump, he will land on the nearest leaf on his jumping direction and face to that direction regardless of his previous state. The leaf he was on will vanish into the water after the jump. The goal of this puzzle is to jump from leaf to leaf until there is only one leaf remaining. See the example shown in the figure below. In this situation, he has three choices, namely, the leaves A, B and C. Note that he cannot jump to the leaf D since he cannot jump backward. Suppose that he choose the leaf B. After jumping there, the situation will change as shown in the following figure. He can jump to either leaf E or F next. After some struggles, he found this puzzle difficult, since there are a lot of leaves on the pond. Can you help him to find out a solution? Input H W c 1,1 ... c 1, W . . . c H ,1 ... c H , W The first line of the input contains two positive integers H and W (1 ≤ H , W ≤ 10). The following H lines, which contain W characters each, describe the initial configuration of the leaves and the frog using following characters: '.’ : water ‘o’ : a leaf ‘U’ : a frog facing upward (i.e. to the upper side) on a leaf ‘D’ : a frog facing downward (i.e. to the lower side) on a leaf ‘L’ : a frog facing leftward (i.e. to the left side) on a leaf ‘R’ : a frog facing rightward (i.e. to the right side) on a leaf You can assume that there is only one frog in each input. You can also assume that the total number of leaves (including the leaf the frog is initially on) is at most 30. Output Output a line consists of the characters ‘U’ (up), ‘D’ (down), ‘L’ (left) and ‘R’ (right) that describes a series of movements. The output should not contain any other characters, such as spaces. You can assume that there exists only one solution for each input. Sample Input 1 2 3 Uo. .oo Output for the Sample Input 1 RDR Sample Input 2 10 10 .o....o... o.oo...... ..oo..oo.. ..o....... ..oo..oo.. ..o...o.o. o..U.o.... oo......oo oo........ oo..oo.... Output for the Sample Input 2 URRULULDDLUURDLLLURRDLDDDRRDR Sample Input 3 10 1 D . . . . . . . . o Output for the Sample Input 3 D
[ { "submission_id": "aoj_2223_10684773", "code_snippet": "#include <vector>\n#include <list>\n#include <map>\n#include <set>\n#include <deque>\n#include <queue>\n#include <stack>\n#include <bitset>\n#include <algorithm>\n#include <functional>\n#include <numeric>\n#include <utility>\n#include <sstream>\n#include <fstream>\n#include <iostream>\n#include <iomanip>\n#include <cstdio>\n#include <cmath>\n#include <cstdlib>\n#include <ctime>\n#include <cstring>\nusing namespace std;\nint n,m;\nchar a[15][15];\nbool bo[15][15];\nint dx[]={-1,0,1,0},dy[]={0,1,0,-1};\nchar UNdir(int x)\n{\n if (x==0) return 'U';\n if (x==1) return 'R';\n if (x==2) return 'D';\n if (x==3) return 'L';\n}\nint dir(char x)\n{\n if (x=='U') return 3;\n if (x=='R') return 0;\n if (x=='D') return 1;\n if (x=='L') return 2;\n}\nstruct node\n{\n int d;\n int x[105];\n int y[105];\n int num;\n char fa;\n string ans;\n};\nbool in(int x,int y)\n{\n return (x>=0 && x<n && y>=0 && y<m);\n}\nvoid out(node x)\n{\n char xx[15][15];\n for (int i = 0; i < n; ++i)\n {\n for (int j = 0; j < m; ++j)\n {\n xx[i][j]='.';\n }\n }\n xx[x.d/m][x.d%m]=x.fa;\n for (int i = 0; i < x.num; ++i)\n {\n xx[x.x[i]][x.y[i]]='o';\n }\n for (int i = 0; i < n; ++i)\n {\n for (int j = 0; j < m; ++j)\n {\n cout<<xx[i][j];\n }\n cout<<endl;\n }\n cout<<endl;\n}\nint ans=0,ansd=0;\nint main(int argc, char *argv[])\n{\n int T;\n cin>>n>>m;\n node xx;xx.num=0;\n xx.ans=\"\";\n for (int i = 0; i < n; ++i)\n {\n for (int j = 0 ; j < m; ++j)\n {\n cin>>a[i][j];\n if (a[i][j]=='o')\n {\n xx.x[xx.num]=i;\n xx.y[xx.num++]=j;\n }\n else if (a[i][j]=='U'||a[i][j]=='L'||a[i][j]=='R'||a[i][j]=='D')\n {\n xx.d=i*m+j;\n xx.fa=a[i][j];\n }\n }\n }\n queue<node> q;\n q.push(xx);\n bool gan=0;\n while (!q.empty())\n {\n node now=q.front();\n \n if (now.num==0)\n {\n cout<<now.ans<<endl;\n break;\n }\n q.pop();\n int x=now.d/m,y=now.d%m;\n memset(bo,0,sizeof(bo));\n bo[x][y]=1;\n for (int i = 0; i < now.num; ++i)\n {\n bo[now.x[i]][now.y[i]]=1;\n }\n int face=dir(now.fa);\n for (int i = face; i < face+3; ++i)\n {\n int ii=i%4;\n int l=1;\n int xx=x+dx[ii],yy=y+dy[ii];\n while (1)\n {\n if (!in(xx,yy)) break;\n if (bo[xx][yy])\n {\n node go;\n go.d=xx*m+yy;\n go.ans=now.ans+UNdir(ii);\n go.fa=UNdir(ii);\n go.num=0;\n for (int j = 0; j < now.num; ++j)\n {\n int xxx=now.x[j],yyy=now.y[j];\n if (!(xxx==xx&&yyy==yy))\n {\n go.x[go.num]=xxx;\n go.y[go.num++]=yyy;\n }\n }\n //cout<<x<<' '<<y<<' '<<' '<<now.num<<' '<<now.ans<<' '<<xx<<' '<<yy<<' '<<go.num<<' '<<ii<<' '<<go.ans<<endl;\n\n // out(go);\n //system(\"pause\"); \n q.push(go); \n break;\n }\n xx+=dx[ii];\n yy+=dy[ii];\n }\n }\n }\n //system(\"pause\");\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 7580, "score_of_the_acc": -0.2182, "final_rank": 11 }, { "submission_id": "aoj_2223_10684771", "code_snippet": "#include <vector>\n#include <list>\n#include <map>\n#include <set>\n#include <deque>\n#include <queue>\n#include <stack>\n#include <bitset>\n#include <algorithm>\n#include <functional>\n#include <numeric>\n#include <utility>\n#include <sstream>\n#include <fstream>\n#include <iostream>\n#include <iomanip>\n#include <cstdio>\n#include <cmath>\n#include <cstdlib>\n#include <ctime>\n#include <cstring>\nusing namespace std;\nint n,m;\nchar a[15][15];\nbool bo[15][15];\nint dx[]={-1,0,1,0},dy[]={0,1,0,-1};\nchar UNdir(int x)\n{\n if (x==0) return 'U';\n if (x==1) return 'R';\n if (x==2) return 'D';\n if (x==3) return 'L';\n}\nint dir(char x)\n{\n if (x=='U') return 3;\n if (x=='R') return 0;\n if (x=='D') return 1;\n if (x=='L') return 2;\n}\nstruct node\n{\n int d;\n int x[105];\n int y[105];\n int num;\n char fa;\n string ans;\n};\nbool in(int x,int y)\n{\n return (x>=0 && x<n && y>=0 && y<m);\n}\nvoid out(node x)\n{\n char xx[15][15];\n for (int i = 0; i < n; ++i)\n {\n for (int j = 0; j < m; ++j)\n {\n xx[i][j]='.';\n }\n }\n xx[x.d/m][x.d%m]=x.fa;\n for (int i = 0; i < x.num; ++i)\n {\n xx[x.x[i]][x.y[i]]='o';\n }\n for (int i = 0; i < n; ++i)\n {\n for (int j = 0; j < m; ++j)\n {\n cout<<xx[i][j];\n }\n cout<<endl;\n }\n cout<<endl;\n}\nint ans=0,ansd=0;\nint main(int argc, char *argv[])\n{\n int T;\n cin>>n>>m;\n node xx;xx.num=0;\n xx.ans=\"\";\n for (int i = 0; i < n; ++i)\n {\n for (int j = 0 ; j < m; ++j)\n {\n cin>>a[i][j];\n if (a[i][j]=='o')\n {\n xx.x[xx.num]=i;\n xx.y[xx.num++]=j;\n }\n else if (a[i][j]=='U'||a[i][j]=='L'||a[i][j]=='R'||a[i][j]=='D')\n {\n xx.d=i*m+j;\n xx.fa=a[i][j];\n }\n }\n }\n queue<node> q;\n q.push(xx);\n bool gan=0;\n while (!q.empty())\n {\n node now=q.front();\n \n if (now.num==0)\n {\n cout<<now.ans<<endl;\n break;\n }\n q.pop();\n int x=now.d/m,y=now.d%m;\n memset(bo,0,sizeof(bo));\n bo[x][y]=1;\n for (int i = 0; i < now.num; ++i)\n {\n bo[now.x[i]][now.y[i]]=1;\n }\n int face=dir(now.fa);\n for (int i = face; i < face+3; ++i)\n {\n int ii=i%4;\n int l=1;\n int xx=x+dx[ii],yy=y+dy[ii];\n while (1)\n {\n if (!in(xx,yy)) break;\n if (bo[xx][yy])\n {\n node go;\n go.d=xx*m+yy;\n go.ans=now.ans+UNdir(ii);\n go.fa=UNdir(ii);\n go.num=0;\n for (int j = 0; j < now.num; ++j)\n {\n int xxx=now.x[j],yyy=now.y[j];\n if (!(xxx==xx&&yyy==yy))\n {\n go.x[go.num]=xxx;\n go.y[go.num++]=yyy;\n }\n }\n //cout<<x<<' '<<y<<' '<<' '<<now.num<<' '<<now.ans<<' '<<xx<<' '<<yy<<' '<<go.num<<' '<<ii<<' '<<go.ans<<endl;\n\n // out(go);\n //system(\"pause\"); \n q.push(go); \n break;\n }\n xx+=dx[ii];\n yy+=dy[ii];\n }\n }\n }\n //system(\"pause\");\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 7896, "score_of_the_acc": -0.2273, "final_rank": 12 }, { "submission_id": "aoj_2223_3596315", "code_snippet": "#include <iostream>\n#include <algorithm>\n#include <vector>\n#include <string>\n#include <bitset>\n\nusing namespace std;\n\nusing lint = long long;\nusing ldouble = long double;\n\nconst string URDL = \"URDL\";\nvector<pair<int, int>> leaves;\n\nstring rec(int idx, int dir, int pat) {\n if (__builtin_popcount(pat) == 1) return \"\";\n // cerr << idx << \":\" << URDL[dir] << \":\" << bitset<30>(pat) << endl;\n\n for (int d = -1; d <= 1; ++d) {\n int ndir = (dir + d + 4) % 4;\n int nidx = -1;\n\n for (int i = 0; i < leaves.size(); ++i) {\n if (!((pat >> i) & 1) || i == idx) continue;\n\n switch (ndir) {\n case 0:\n if (leaves[i].second != leaves[idx].second ||\n leaves[i].first > leaves[idx].first) continue;\n if (nidx < 0 || leaves[i].first > leaves[nidx].first) nidx = i;\n break;\n\n case 1:\n if (leaves[i].first != leaves[idx].first ||\n leaves[i].second < leaves[idx].second) continue;\n if (nidx < 0 || leaves[i].second < leaves[nidx].second) nidx = i;\n break;\n\n case 2:\n if (leaves[i].second != leaves[idx].second ||\n leaves[i].first < leaves[idx].first) continue;\n if (nidx < 0 || leaves[i].first < leaves[nidx].first) nidx = i;\n break;\n\n case 3:\n if (leaves[i].first != leaves[idx].first ||\n leaves[i].second > leaves[idx].second) continue;\n if (nidx < 0 || leaves[i].second > leaves[nidx].second) nidx = i;\n break;\n }\n }\n\n if (nidx < 0) continue;\n\n int npat = pat ^ (1 << idx);\n string res = rec(nidx, ndir, npat);\n if (res != \"x\") {\n res.insert(res.begin(), URDL[ndir]);\n return res;\n }\n }\n\n return \"x\";\n}\n\nint main() {\n int H, W;\n cin >> H >> W;\n\n vector<string> S(H);\n for (auto& s : S) cin >> s;\n\n int si, dir;\n for (int x = 0; x < H; ++x) {\n for (int y = 0; y < W; ++y) {\n if (S[x][y] != '.') {\n leaves.emplace_back(x, y);\n if (S[x][y] != 'o') {\n si = leaves.size() - 1;\n for (int i = 0; i < 4; ++i) {\n if (S[x][y] == URDL[i]) dir = i;\n }\n }\n }\n }\n }\n\n cout << rec(si, dir, (1 << leaves.size()) - 1) << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3232, "score_of_the_acc": -0.093, "final_rank": 6 }, { "submission_id": "aoj_2223_3306585", "code_snippet": "#include \"bits/stdc++.h\"\n\nusing namespace std;\n\nusing ll = long long;\nusing ld = long double;\nusing P = pair<ll, ll>;\nusing T = tuple<ll, ll, ll>;\nconstexpr ld EPS = 1e-12;\nconstexpr int INF = numeric_limits<int>::max() / 2;\nconstexpr int MOD = 1e9 + 7;\n\nint dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};\nchar d[4] = {'D', 'R', 'U', 'L'};\n\nint main()\n{\n cin.tie(0);\n ios::sync_with_stdio(false);\n\n int H, W;\n cin >> H >> W;\n vector<string> s(H);\n vector<P> ls;\n int sx, sy, dir;\n map<P, int> rev;\n for (int i = 0; i < H; i++)\n {\n cin >> s[i];\n for (int j = 0; j < W; j++)\n {\n if (s[i][j] == '.')\n continue;\n if (s[i][j] == 'o')\n {\n rev[P(i, j)] = ls.size();\n ls.push_back(P(i, j));\n continue;\n }\n sx = i;\n sy = j;\n if (s[i][j] == 'D')\n dir = 0;\n else if (s[i][j] == 'R')\n dir = 1;\n else if (s[i][j] == 'U')\n dir = 2;\n else\n dir = 3;\n s[i][j] = '.';\n }\n }\n // pos, dir, mask\n map<T, string> mp;\n int sz = ls.size();\n ll mask = (1LL << sz) - 1;\n ls.push_back(P(sx, sy));\n queue<T> q;\n q.push(T(sz, dir, mask));\n mp[T(sz, dir, mask)] = \"\";\n while (!q.empty())\n {\n T t = q.front();\n q.pop();\n int pos = get<0>(t);\n int dir = get<1>(t);\n mask = get<2>(t);\n for (int i = 0; i < 4; i++)\n {\n if (abs(dir - i) == 2)\n continue;\n for (int j = 1; j < 10; j++)\n {\n int nx = ls[pos].first + j * dx[i], ny = ls[pos].second + j * dy[i];\n if (nx < 0 || H <= nx || ny < 0 || W <= ny)\n continue;\n if (s[nx][ny] != 'o')\n continue;\n int idx = rev[P(nx, ny)];\n // 訪問済み\n if (((mask >> idx) & 1) == 0)\n continue;\n ll mask2 = mask ^ (1LL << idx);\n T nt = T(idx, i, mask2);\n if (mp.find(nt) != mp.end())\n break;\n q.push(nt);\n string ns = mp[t];\n ns += d[i];\n mp[nt] = ns;\n break;\n }\n }\n }\n for(auto t:mp){\n if(get<2>(t.first) == 0){\n cout<<t.second<<endl;\n return 0;\n }\n }\n assert(false);\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 7196, "score_of_the_acc": -0.2072, "final_rank": 10 }, { "submission_id": "aoj_2223_2765170", "code_snippet": "#include<bits/stdc++.h>\n#define range(i,a,b) for(int i = (a); i < (b); i++)\n#define rep(i,b) for(int i = 0; i < (b); i++)\n#define all(a) (a).begin(), (a).end()\n#define show(x) cerr << #x << \" = \" << (x) << endl;\nusing namespace std;\n\ntemplate<typename T>\nostream& operator << (ostream& os, vector<T>& v){\n\trep(i,v.size()){ os << v[i] << (i == v.size() - 1 ? \"\" : \" \"); } return os;\n}\ntemplate<typename T>\nistream& operator >> (istream& is, vector<T>& v){\n\tfor(T& x: v){ is >> x; } return is;\n}\n\nint h, w;\n\nconst int dy[16] = { 0,-1, 0, 1, 1,-1, 1,-1, 0,-2, 0, 2};\nconst int dx[16] = { 1, 0,-1, 0, 1, 1,-1,-1, 2, 0,-2, 0};\nconst char C[4] = {'R', 'U', 'L', 'D'};\n\nstruct Data{\n\tvector<vector<char>> c;\n\tint leves;\n\tint y, x, d;\n\tstring s;\n\tData(vector<vector<char>>& c, int leves, int y, int x, int d, string s) : c(c), leves(leves), y(y), x(x), d(d), s(s) {}\n};\n\nstring bfs(Data& start){\n\tqueue<Data> q;\n\tq.emplace(start);\n\n\twhile(not q.empty()){\n\t\tData p = q.front(); q.pop();\n\t\t//cout << p.y << ' ' << p.x <<' ' << p.d << endl;\n\t\t//for(auto i : p.c){ for(auto j : i){ cout << j << ' '; } cout << endl; } cout << endl;\n\n\n\t\tif(p.leves == 1) return p.s;\n\t\tif(p.s.size() >= 30) continue;\n\n\t\trep(dir,4){\n\t\t\tif((p.d + 2) % 4 == dir) continue;\n\t\t\tint ny = p.y + dy[dir];\n\t\t\tint nx = p.x + dx[dir];\n\t\t\tif(ny < 0 || ny >= h || nx < 0 || nx >= w) continue;\n\n\t\t\tbool f = true;\n\t\t\twhile(p.c[ny][nx] == '.'){\n\t\t\t\tny += dy[dir];\n\t\t\t\tnx += dx[dir];\n\t\t\t\tif(ny < 0 || ny >= h || nx < 0 || nx >= w){\n\t\t\t\t\tf = false;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(not f) continue;\n\n\t\t\tp.c[p.y][p.x] = '.';\n\t\t\tq.emplace(p.c, p.leves - 1, ny, nx, dir, p.s + C[dir]);\n\t\t\tp.c[p.y][p.x] = 'o';\n\t\t}\n\t}\n\tassert(false);\n\treturn \"null\";\n}\n\nint main(){\n\tcin >> h >> w;\n\n\tvector<vector<char>> c(h, vector<char>(w));\n\tint y, x, leves = 0;\n\tchar dir;\n\trep(i,h) rep(j,w){\n\t\tcin >> c[i][j];\n\t\tif(isupper(c[i][j])){\n\t\t\ty = i;\n\t\t\tx = j;\n\t\t\tdir = c[i][j];\n\t\t\tc[i][j] = 'o';\n\t\t}\n\t\tif(c[i][j] == 'o') leves++;\n\t}\n\n\trep(i,4) if(C[i] == dir){\n\t\tData data(c, leves, y, x, i, \"\");\n\t\tcout << bfs(data) << endl;\n\t\treturn 0;\n\t}\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 6588, "score_of_the_acc": -0.3325, "final_rank": 15 }, { "submission_id": "aoj_2223_2702011", "code_snippet": "#include <bits/stdc++.h>\n #include<iostream>\n #include<cstdio>\n #include<vector>\n #include<queue>\n #include<map>\n #include<cstring>\n #include<string>\n #include <math.h>\n #include<algorithm>\n // #include <boost/multiprecision/cpp_int.hpp>\n #include<functional>\n #define int long long\n #define inf 1000000007\n #define pa pair<int,int>\n #define ll long long\n #define pal pair<double,pa>\n #define ppap pair<pa,int>\n \n #define ssa pair<string,int>\n #define mp make_pair\n #define pb push_back\n #define EPS (1e-10)\n #define equals(a,b) (fabs((a)-(b))<EPS)\nint dx[4]={0,1,0,-1};\nint dy[4]={1,0,-1,0};\n using namespace std;\n class pas{\n \tpublic:\n \tint x,y,h;\n \tpas(int x=0,int y=0,int h=0):x(x),y(y),h(h) {}\n \tbool operator < (const pas &p) const{\n \t\treturn x != p.x ? x<p.x: y<p.y;\n \t}\n \tbool operator == (const pas &p) const{\n \t\treturn abs(x-p.x)==0 && abs(y-p.y)==0;\n \t}\n \t\t\n \n };\n class pa2{\n \tpublic:\n \tint x,y;\n \tpa2(int x=0,int y=0):x(x),y(y) {}\n \tpa2 operator + (pa2 p) {return pa2(x+p.x,y+p.y);}\n \tbool operator < (const pa2 &p) const{\n \t\treturn x != p.x ? x<p.x: y<p.y;\n \t}\n \tbool operator == (const pa2 &p) const{\n \t\treturn abs(x-p.x)==0 && abs(y-p.y)==0;\n \t}\n \t\t\n \n };\n \n #define ppa pair<int,pas>\n class Point{\n \tpublic:\n \tdouble x,y;\n \tPoint(double x=0,double y=0):x(x),y(y) {}\n \tPoint operator + (Point p) {return Point(x+p.x,y+p.y);}\n \tPoint operator - (Point p) {return Point(x-p.x,y-p.y);}\n \tPoint operator * (double a) {return Point(x*a,y*a);}\n \tPoint operator / (double a) {return Point(x/a,y/a);}\n \tdouble absv() {return sqrt(norm());}\n \tdouble norm() {return x*x+y*y;}\n \tbool operator < (const Point &p) const{\n \t\treturn x != p.x ? x<p.x: y<p.y;\n \t}\n \tbool operator == (const Point &p) const{\n \t\treturn fabs(x-p.x)<EPS && fabs(y-p.y)<EPS;\n \t}\n };\n typedef Point Vector;\n #define pl pair<int,pas>\n struct Segment{\n Point p1,p2;\n };\n double dot(Vector a,Vector b){\n \treturn a.x*b.x+a.y*b.y;\n }\n double cross(Vector a,Vector b){\n \treturn a.x*b.y-a.y*b.x;\n }\n \n bool parareru(Point a,Point b,Point c,Point d){\n //\tif(abs(cross(a-b,d-c))<EPS)cout<<\"dd \"<<cross(a-b,d-c)<<endl;\n \treturn abs(cross(a-b,d-c))<EPS;\n }\n double distance_ls_p(Point a, Point b, Point c) {\n if ( dot(b-a, c-a) < EPS ) return (c-a).absv();\n if ( dot(a-b, c-b) < EPS ) return (c-b).absv();\n return abs(cross(b-a, c-a)) / (b-a).absv();\n }\n bool is_intersected_ls(Segment a,Segment b) {\n \tif(a.p1==b.p1||a.p2==b.p1||a.p1==b.p2||a.p2==b.p2) return false;\n \tif(parareru((a.p2),(a.p1),(a.p1),(b.p2))&&parareru((a.p2),(a.p1),(a.p1),(b.p1))){\n //\t\tcout<<\"sss\"<<endl;\n \t\tif(dot(a.p1-b.p1,a.p1-b.p2)<EPS) return true;\n \t\tif(dot(a.p2-b.p1,a.p2-b.p2)<EPS) return true;\n \t\tif(dot(a.p1-b.p1,a.p2-b.p1)<EPS) return true;\n \t\tif(dot(a.p1-b.p2,a.p2-b.p2)<EPS) return true;\n \t\treturn false;\n \t}\n else return ( cross(a.p2-a.p1, b.p1-a.p1) * cross(a.p2-a.p1, b.p2-a.p1) < EPS ) && ( cross(b.p2-b.p1, a.p1-b.p1) * cross(b.p2-b.p1, a.p2-b.p1) < EPS );\n }\n \n double segment_dis(Segment a,Segment b){\n \tif(is_intersected_ls(a,b))return 0;\n \tdouble r=distance_ls_p(a.p1, a.p2, b.p1);\n \tr=min(r,distance_ls_p(a.p1, a.p2, b.p2));\n \tr=min(r,distance_ls_p(b.p1, b.p2, a.p2));\n \tr=min(r,distance_ls_p(b.p1, b.p2, a.p1));\n \treturn r;\n }\n Point intersection_ls(Segment a, Segment b) {\n Point ba = b.p2-b.p1;\n double d1 = abs(cross(ba, a.p1-b.p1));\n double d2 = abs(cross(ba, a.p2-b.p1));\n double t = d1 / (d1 + d2);\n \n return a.p1 + (a.p2-a.p1) * t;\n }\n \n string itos( int i ) {\n ostringstream s ;\n s << i ;\n return s.str() ;\n }\n \n int gcd(int v,int b){\n \tif(v>b) return gcd(b,v);\n \tif(v==b) return b;\n \tif(b%v==0) return v;\n \treturn gcd(v,b%v);\n }\n \n double distans(double x1,double y1,double x2,double y2){\n \tdouble rr=(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);\n \treturn sqrt(rr);\n \t\n }\n \n // int pr[2000010];\n // int inv[2000010];\n /*\n int beki(int wa,int rr,int warukazu){\n \tif(rr==0) return 1ll;\n \tif(rr==1) return wa%warukazu;\n \tif(rr%2==1) return (beki(wa,rr-1,warukazu)*wa)%warukazu;\n \tint zx=beki(wa,rr/2,warukazu);\n \treturn (zx*zx)%warukazu;\n }\n \n\t\t\tint comb(int nn,int rr){\n\t\t\t\tint r=pr[nn]*inv[rr];\n\t\t\t\tr%=inf;\n\t\t\t\tr*=inv[nn-rr];\n\t\t\t\tr%=inf;\n\t\t\t\treturn r;\n\t\t\t}\n \n void gya(int ert){\n \tpr[0]=1;\n \tfor(int i=1;i<ert;i++){\n \t\tpr[i]=(pr[i-1]*i)%inf;\n \t}\n \tfor(int i=0;i<ert;i++) inv[i]=beki(pr[i],inf-2,inf);\n \t\n }\n */\n \n //sort(ve.begin(),ve.end(),greater<int>());\n //----------------kokomade tenpure------------\n //vector<double> ans(100000000),ans2(100000000);\n\nint h,w;\nint cnt;\nint a[15][15]={0};\nstring ans;\nvoid dfs(int x,int y,string muki,int nokori){\n\t//cout<<x<<\" \"<<y<<\" \"<<nokori<<endl;\n\tif(nokori==0){\n\t\tcout<<ans<<endl;\n\t\texit(0);\n\t}\n\tif(muki!=\"D\"){\n\t\tint dx=x-1,dy=y;\n\t\twhile(a[dx][dy]==0)dx--;\n\t\tif(a[dx][dy]!=2){\n\t\t\ta[x][y]=0;\n\t\t\tstring ans2=ans;\n\t\t\tans+=\"U\";\n\t\t\tdfs(dx,dy,\"U\",nokori-1);\n\t\t\tans=ans2;\n\t\t\ta[x][y]=1;\n\t\t}\n\t}\n\tif(muki!=\"U\"){\n\t\tint dx=x+1,dy=y;\n\t\twhile(a[dx][dy]==0)dx++;\n\t\tif(a[dx][dy]!=2){\n\t\t\ta[x][y]=0;\n\t\t\tstring ans2=ans;\n\t\t\tans+=\"D\";\n\t\t\tdfs(dx,dy,\"D\",nokori-1);\n\t\t\tans=ans2;\n\t\t\ta[x][y]=1;\n\t\t}\n\t}\n\t\n\tif(muki!=\"L\"){\n\t\tint dx=x,dy=y+1;\n\t\twhile(a[dx][dy]==0)dy++;\n\t\tif(a[dx][dy]!=2){\n\t\t\ta[x][y]=0;\n\t\t\tstring ans2=ans;\n\t\t\tans+=\"R\";\n\t\t\tdfs(dx,dy,\"R\",nokori-1);\n\t\t\tans=ans2;\n\t\t\ta[x][y]=1;\n\t\t}\n\t}\n\tif(muki!=\"R\"){\n\t\tint dx=x,dy=y-1;\n\t\twhile(a[dx][dy]==0)dy--;\n\t\tif(a[dx][dy]!=2){\n\t\t\ta[x][y]=0;\n\t\t\tstring ans2=ans;\n\t\t\tans+=\"L\";\n\t\t\tdfs(dx,dy,\"L\",nokori-1);\n\t\t\tans=ans2;\n\t\t\ta[x][y]=1;\n\t\t}\n\t}\n}\n\n\n\n\n signed main(){\n\n \tfor(int i=0;i<15;i++)a[i][0]=2,a[0][i]=2,a[14][i]=2,a[i][14]=2;\n \tint x,y;\n \tstring st;\n \tcin>>h>>w;\n \tcnt=0;\n \tfor(int i=1;i<=h;i++){\n \t\tstring s;\n \t\tcin>>s;\n \t\ts=\"d\"+s;\n \t\tfor(int j=1;j<=w;j++){\n \t\t\tif(s[j]=='o')cnt++, a[i][j]=1;\n \t\t\tif(s[j]=='U'){\n \t\t\t\tst=\"U\";\n \t\t\t\tx=i,y=j;\n \t\t\t}\n \t\t\tif(s[j]=='D'){\n \t\t\t\tst=\"D\";\n \t\t\t\tx=i,y=j;\n \t\t\t}\n \t\t\tif(s[j]=='L'){\n \t\t\t\tst=\"L\";\n \t\t\t\tx=i,y=j;\n \t\t\t}\n \t\t\tif(s[j]=='R'){\n \t\t\t\tst=\"R\";\n \t\t\t\tx=i,y=j;\n \t\t\t}\n \t\t}\n \t}\n // cout<<x<<\" \"<<y<<endl;\n \ta[x][y]=1;\n \tans=\"\";\n \tdfs(x,y,st,cnt);\n \t\n \t\n \treturn 0;\n }", "accuracy": 1, "time_ms": 10, "memory_kb": 3164, "score_of_the_acc": -0.0911, "final_rank": 5 }, { "submission_id": "aoj_2223_1981202", "code_snippet": "#include<iostream>\n#include<queue>\n#include<string>\nusing namespace std;\nstruct Kaeru { char b[10][10]; int px, py, dir; string F; };\nint dx[4] = { -1,0,1,0 }, dy[4] = { 0,1,0,-1 };\nqueue<Kaeru>Q; int H, W; char x[10][10]; int cx, cy, cd = 0;\nchar CC[5] = \"URDL\";\nint main() {\n\tcin >> H >> W;\n\twhile (!Q.empty())Q.pop(); int CNT = 0;\n\tfor (int i = 0; i < H; i++) {\n\t\tfor (int j = 0; j < W; j++) {\n\t\t\tcin >> x[i][j]; if (x[i][j] == 'o')CNT++;\n\t\t\tif (x[i][j] == 'U') { cx = i; cy = j; cd = 0; x[i][j] = '.'; }\n\t\t\tif (x[i][j] == 'R') { cx = i; cy = j; cd = 1; x[i][j] = '.'; }\n\t\t\tif (x[i][j] == 'D') { cx = i; cy = j; cd = 2; x[i][j] = '.'; }\n\t\t\tif (x[i][j] == 'L') { cx = i; cy = j; cd = 3; x[i][j] = '.'; }\n\t\t}\n\t}\n\tKaeru D; for (int i = 0; i < 100; i++)D.b[i / 10][i % 10] = x[i / 10][i % 10];\n\tD.px = cx; D.py = cy; D.dir = cd; D.F = \"\"; Q.push(D);\n\twhile (!Q.empty()) {\n\t\tKaeru a1 = Q.front(); Q.pop();\n\t\tKaeru a2 = a1;\n\t\tif (a2.F.size() == CNT) { cout << a2.F << endl; break; }\n\n\t\tfor (int i = 0; i < 4; i++) {\n\t\t\ta2 = a1; char y[10][10]; for (int j = 0; j < 100; j++)y[j / 10][j % 10] = a2.b[j / 10][j % 10];\n\t\t\tif (i == (a1.dir + 2) % 4)continue;\n\t\t\tint ex = a2.px, ey = a2.py;\n\t\t\twhile (true) {\n\t\t\t\tex += dx[i]; ey += dy[i];\n\t\t\t\tif (ex < 0 || ey < 0 || ex >= H || ey >= W)break;\n\t\t\t\tif (y[ex][ey] == 'o') {\n\t\t\t\t\ty[ex][ey] = '.';\n\t\t\t\t\tfor (int j = 0; j < 100; j++)a2.b[j / 10][j % 10] = y[j / 10][j % 10];\n\t\t\t\t\ta2.px = ex; a2.py = ey; a2.dir = i; a2.F += CC[i];\n\t\t\t\t\tQ.push(a2); break;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 4224, "score_of_the_acc": -0.3359, "final_rank": 16 }, { "submission_id": "aoj_2223_1621522", "code_snippet": "#include<stdio.h>\n#include <iostream>\n#include <math.h>\n#include <numeric>\n#include <vector>\n#include <map>\n#include <functional>\n#include <stdio.h>\n#include <array>\n#include <algorithm>\n#include <string>\n#include <string.h>\n#include <assert.h>\n#include <stdio.h>\n#include <queue>\n#include<iomanip>\n#include<bitset>\n#include<stack>\n#include<set>\n#include<limits>\n#include <complex>\nusing namespace std;\n\nconst int mod = 1000000007;\nstruct Mod {\npublic:\n\tint num;\n\tMod() : num(0) { ; }\n\tMod(long long int n) : num((n % mod + mod) % mod) { ; }\n\tMod(int n) : num((n % mod + mod) % mod) { ; }\n\toperator int() { return num; }\n};\n\nMod operator+(const Mod a, const Mod b) { return Mod((a.num + b.num) % mod); }\nMod operator+(const long long int a, const Mod b) { return Mod(a) + b; }\nMod operator++(Mod &a) { return a + Mod(1); }\nMod operator-(const Mod a, const Mod b) { return Mod((mod + a.num - b.num) % mod); }\nMod operator-(const long long int a, const Mod b) { return Mod(a) - b; }\nMod operator--(Mod &a) { return a - Mod(1); }\nMod operator*(const Mod a, const Mod b) { return Mod(((long long)a.num * b.num) % mod); }\nMod operator*(const long long int a, const Mod b) { return Mod(a)*b; }\nMod operator+=(Mod &a, const Mod b) { return a = a + b; }\nMod operator+=(long long int &a, const Mod b) { return a = a + b; }\nMod operator-=(Mod &a, const Mod b) { return a = a - b; }\nMod operator-=(long long int &a, const Mod b) { return a = a - b; }\nMod operator*=(Mod &a, const Mod b) { return a = a * b; }\nMod operator*=(long long int &a, const Mod b) { return a = a * b; }\nMod operator^(const Mod a, const int n) {\n\tif (n == 0) return Mod(1);\n\tMod res = (a * a) ^ (n / 2);\n\tif (n % 2) res = res * a;\n\treturn res;\n}\nMod inv(const Mod a) { return a ^ (mod - 2); }\nMod operator/(const Mod a, const Mod b) {\n\tassert(b.num != 0);\n\treturn a * inv(b);\n}\nMod operator/(const long long int a, const Mod b) {\n\tassert(b.num != 0);\n\treturn Mod(a) * inv(b);\n}\nMod operator/=(Mod &a, const Mod b) {\n\tassert(b.num != 0);\n\treturn a = a * inv(b);\n}\n\n#define MAX_N 1024000\n\nMod fact[MAX_N], factinv[MAX_N];\nvoid init() {\n\tfact[0] = Mod(1); factinv[0] = 1;\n\tfor (int i = 0; i < MAX_N - 1; ++i) {\n\t\tfact[i + 1] = fact[i] * Mod(i + 1);\n\t\tfactinv[i + 1] = factinv[i] / Mod(i + 1);\n\t}\n}\nMod comb(const int a, const int b) {\n\treturn fact[a] * factinv[b] * factinv[a - b];\n}\ntemplate<typename T>\nvector<vector<T>> keisann(const vector<vector<T>>l, const vector<vector<T>>r) {\n\tvector<vector<T>>ans(l.size(), vector<T>(r[0].size()));\n\tassert(l[0].size() == r.size());\n\tfor (unsigned int h = 0; h < l.size(); ++h) {\n\t\tfor (unsigned int i = 0; i < r.size(); ++i) {\n\t\t\tfor (unsigned int w = 0; w < r[0].size(); ++w) {\n\t\t\t\n\t\t\t\tans[h][w] +=l[h][i] * r[i][w];\n\t\t\t}\n\t\t}\n\t}\n\treturn ans;\n}\ntemplate<typename T>\nvector<vector<T>>powgyou(vector<vector<T>>a, const long long int n) {\n\tassert(a.size() == a[0].size());\n\tif (!n) {\n\t\tvector<vector<T>>e(a.size(), vector<T>(a[0].size()));\n\t\tfor (unsigned int i = 0; i < a.size(); ++i) {\n\t\t\te[i][i] = 1;\n\t\t}\n\t\treturn e;\n\t}\n\tif (n == 1)return a;\n\telse {\n\t\tvector<vector<T>>ans(a.size(), vector<T>(a[0].size(), 0));\n\t\tans = powgyou(a, n / 2);\n\t\tans = keisann(ans, ans);\n\t\tif (n % 2) {\n\t\t\tans = keisann(ans, a);\n\t\t}\n\t\treturn ans;\n\t}\n}\n\n\n\nlong long int powint(long long int a, int b) {\n\tif (b == 0)return 1;\n\tif (b == 1)return a;\n\telse {\n\t\tlong long int ans = 1;\n\t\tlong long int c = powint(a, b / 2);\n\t\tans *= c*c;\n\t\tans *= (b % 2) ? a : 1;\n\t\treturn ans;\n\t}\n\t\n}\n\nint dx[4] = { -1,0,1,0 };\nint dy[4] = { 0,1,0,-1 };\n\n\nvector<vector<int>>fd(10, vector<int>(10,0));\nvector<int>ans;\n\nint aok(vector<vector<int>>a, const int ax,const int ay,const int away,int count) {\n\tif (count == 0)return 1;\n\tfor (int i = 0; i < 4; ++i) {\n\t\tif ((i + 2) % 4 == away) {\n\t\t\tcontinue;\n\t\t}\n\n\t\tint nx(ax), ny(ay);\n\n\t\twhile (1) {\n\t\t\tnx += dx[i]; ny += dy[i];\n\t\t\tif (nx >= 10 || nx < 0 || ny>=10 || ny < 0)break;\n\t\t\tif (a[ny][nx]) {\n\t\t\t\ta[ny][nx] = 0;\n\t\t\t\tif (aok(a, nx, ny, i,count-1) != -1) {\n\t\t\t\t\tans.push_back(i);\n\t\t\t\t\treturn 1;\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\ta[ny][nx] = 1;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\ta[ny][nx] = 1;\n\t\t\t}\n\t\t}\n\t}\n\treturn -1;\n}\n\nint main(void) {\n\tint H, W; cin >> H >> W;\n\tint way;\n\tint count = 0;\n\tint ax, ay;\n\tfor (int i = 0; i < H; ++i) {\n\t\tstring st; cin >> st;\n\t\tfor (int j = 0; j < W; ++j) {\n\t\t\tif (st[j] == '.') {\n\t\t\t\tfd[i][j] = 0;\n\t\t\t}\n\t\t\telse if (st[j] == 'o') {\n\t\t\t\tfd[i][j] = 1;\n\t\t\t\tcount++;\n\t\t\t}\n\t\t\telse {\n\t\t\t\tax = j; ay = i;\n\t\t\t\tfd[i][j] = 0;\n\t\t\t\tif (st[j] == 'U') {\n\t\t\t\t\tway = 3;\n\t\t\t\t}\n\t\t\t\telse if (st[j] == 'D') {\n\t\t\t\t\tway = 1;\n\t\t\t\t}\n\t\t\t\telse if (st[j] == 'L') {\n\t\t\t\t\tway = 0;\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tway = 2;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\taok(fd, ax, ay, way, count);\n\treverse(ans.begin(), ans.end());\n\tfor (int i = 0; i < ans.size(); ++i) {\n\t\tif (ans[i] == 0) {\n\t\t\tcout << 'L';\n\t\t}\n\t\telse if (ans[i] == 1) {\n\t\t\tcout << 'D';\n\t\t}\n\t\telse if (ans[i] == 2) {\n\t\t\tcout << 'R';\n\t\t}\n\t\telse {\n\t\t\tcout << 'U';\n\t\t}\n\t}\n\tcout << endl;\n\treturn 0;\n}\n\n/*\n4\n0 4\n7 12\n6 8\n4 6\n\n*/", "accuracy": 1, "time_ms": 10, "memory_kb": 11232, "score_of_the_acc": -0.3234, "final_rank": 14 }, { "submission_id": "aoj_2223_1604925", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define LOG(...) fprintf(stderr,__VA_ARGS__)\n//#define LOG(...)\n#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);++i)\n#define REP(i,n) for(int i=0;i<(int)(n);++i)\n#define ALL(a) (a).begin(),(a).end()\n#define RALL(a) (a).rbegin(),(a).rend()\n#define EXIST(s,e) ((s).find(e)!=(s).end())\n#define SORT(c) sort(ALL(c))\n#define RSORT(c) sort(RALL(c))\n\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef vector<bool> vb;\ntypedef vector<int> vi;\ntypedef vector<ll> vll;\ntypedef vector<vb> vvb;\ntypedef vector<vi> vvi;\ntypedef vector<vll> vvll;\ntypedef pair<int,int> pii;\ntypedef pair<ll,ll> pll;\n\nconst int dx[] = {-1,0,1,0}, dy[] = {0,1,0,-1};\nconst char dir[] = {'L', 'D', 'R', 'U'};\n\nstruct P {\n int x, y;\n int d;\n int cnt;\n vvb G;\n string moves;\n};\n\nint H, W;\n\nint main() {\n cin >> H >> W;\n\n vvb G(H, vb(W, false));\n int sx, sy, d;\n int cnt = 0;\n REP(y, H) REP(x, W) {\n char c;\n cin >> c;\n if (c == 'o') {\n cnt++;\n G[y][x] = true;\n } else if (c != '.') {\n sx = x;\n sy = y;\n d = find(dir, dir+4, c) - dir;\n }\n }\n\n queue<P> que;\n string moves;\n que.push({sx, sy, d, 0, G, moves});\n\n while (!que.empty()) {\n P p = que.front(); que.pop();\n if (p.cnt >= 30) continue;\n if (p.cnt == cnt) {\n cout << p.moves << endl;\n break;\n }\n int x = p.x, y = p.y;\n if (p.d != 0) for (int x = p.x; x < W; x++) {\n if (p.G[y][x]) {\n vvb g = p.G;\n g[y][x] = false;\n que.push({x, y, 2, p.cnt+1, g, p.moves + \"R\"});\n break;\n }\n }\n if (p.d != 2) for (int x = p.x; x >= 0; x--) {\n if (p.G[y][x]) {\n vvb g = p.G;\n g[y][x] = false;\n que.push({x, y, 0, p.cnt+1, g, p.moves + \"L\"});\n break;\n }\n }\n if (p.d != 3) for (int y = p.y; y < H; y++) {\n if (p.G[y][x]) {\n vvb g = p.G;\n g[y][x] = false;\n que.push({x, y, 1, p.cnt+1, g, p.moves + \"D\"});\n break;\n }\n }\n if (p.d != 1) for (int y = p.y; y >= 0; y--) {\n if (p.G[y][x]) {\n vvb g = p.G;\n g[y][x] = false;\n que.push({x, y, 3, p.cnt+1, g, p.moves + \"U\"});\n break;\n }\n }\n }\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 7476, "score_of_the_acc": -0.5724, "final_rank": 17 }, { "submission_id": "aoj_2223_1487310", "code_snippet": "//include\n//------------------------------------------\n#include <vector>\n#include <list>\n#include <map>\n#include <set>\n#include <deque>\n#include <stack>\n#include <bitset>\n#include <algorithm>\n#include <functional>\n#include <numeric>\n#include <utility>\n#include <sstream>\n#include <iostream>\n#include <iomanip>\n#include <cstdio>\n#include <cmath>\n#include <cstdlib>\n#include <cctype>\n#include <string>\n#include <cstring>\n#include <ctime>\n#include <climits>\n#include <queue>\n\nusing namespace std;\n\n//typedef\n//------------------------------------------\ntypedef vector<int> VI;\ntypedef vector<VI> VVI;\ntypedef vector<string> VS;\ntypedef pair<int, int> PII;\ntypedef long long LL;\n\n//container util\n//------------------------------------------\n#define ALL(a) (a).begin(),(a).end()\n#define RALL(a) (a).rbegin(), (a).rend()\n#define PB push_back\n#define MP make_pair\n#define SZ(a) int((a).size())\n#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)\n#define EXIST(s,e) ((s).find(e)!=(s).end())\n#define SORT(c) sort((c).begin(),(c).end())\n\n//repetition\n//------------------------------------------\n#define FOR(i,a,b) for(int i=(a);i<(b);++i)\n#define REP(i,n) FOR(i,0,n)\n\n//constant\n//--------------------------------------------\nconst double EPS = 1e-10;\nconst double PI = acos(-1.0);\n\nint dx[4] = {0,0,1,-1};\nint dy[4] = {1,-1,0,0};\n\nint main(){\n cin.tie(0);\n ios_base::sync_with_stdio(false);\n\n int H, W; cin >> H >> W;\n VS vs(H); REP(i,H) cin >> vs[i];\n\n queue<VS> q;\n map<VS,pair<VS,char>> memo;\n q.push(vs);\n memo[vs] = MP(VS(), '!');\n VS goal;\n while(!q.empty()){\n\tVS vs = q.front(); q.pop();\n\tint sx = -1, sy = -1, d = -1, cnt = 0;\n\tREP(y,H) REP(x,W){\n\t if(vs[y][x] == '.') continue;\n\t else if(vs[y][x] == 'o')\n\t\t++cnt;\n\t else{\n\t\tsx = x; sy = y;\n\t\tif(vs[y][x] == 'U') d = 0;\n\t\tif(vs[y][x] == 'D') d = 1;\n\t\tif(vs[y][x] == 'L') d = 2;\n\t\tif(vs[y][x] == 'R') d = 3;\n\t }\n\t}\n\tif(cnt == 0){\n\t goal = vs;\n\t break;\n\t}\n\n\tREP(i,4){\n\t if(i == d) continue;\n\t int tx = sx, ty = sy;\n\t while(0<=tx&&tx<W&&0<=ty&&ty<H){\n\t\tif(vs[ty][tx] == 'o') break;\n\t\ttx += dx[i];\n\t\tty += dy[i];\n\t }\n\n\t if(0<=tx&&tx<W&&0<=ty&&ty<H){\n\t\tVS vs_ = vs;\n\t\tvs_[sy][sx] = '.';\n\t\tchar ch;\n\t\tswitch(i){\n\t\tcase 0: ch = 'D'; break;\n\t\tcase 1: ch = 'U'; break;\n\t\tcase 2: ch = 'R'; break;\n\t\tcase 3: ch = 'L'; break;\n\t\t}\n\t\tvs_[ty][tx] = ch;\n\t\tif(!memo.count(vs_)){\n\t\t memo[vs_] = MP(vs,ch);\n\t\t q.push(vs_);\n\t\t}\n\t }\n\t}\n }\n\n string ans;\n while(!goal.empty()){\n\tif(memo[goal].second == '!') break;\n\tans += string(1, memo[goal].second);\n\tgoal = memo[goal].first;\n }\n reverse(ALL(ans));\n cout << ans << endl;\n\n return 0;\n}", "accuracy": 1, "time_ms": 150, "memory_kb": 34736, "score_of_the_acc": -2, "final_rank": 19 }, { "submission_id": "aoj_2223_1132499", "code_snippet": "#include <iostream>\n#include <cstdio>\n#include <vector>\n#include <queue>\n#include <cmath>\n#include <algorithm>\n#include <functional>\nusing namespace std;\n#define rep(i,n) for(int i=0;i<(n);++i)\n#define rep1(i,n) for(int i=1;i<=(n);++i)\n#define all(c) (c).begin(),(c).end()\n#define pb push_back\n#define fs first\n#define sc second\n#define show(x) cout << #x << \" = \" << x << endl\nint H,W;\nvector<string> s;\nstring ans;\nstring ddr=\"URDL\";\nint dx[4]={-1,0,1,0},dy[4]={0,1,0,-1};\t//URDL\nbool on(int x,int y){\n\treturn 0<=x&&x<H&&0<=y&&y<W;\n}\nbool dfs(int x,int y,int di,int cnt,vector<string> s){\n/*\trep(i,H) cout<<s[i]<<endl;\n\tshow(cnt);\n\tputs(\"\");*/\n\tif(cnt==0) return true;\n\tfor(int nd=(di+3)%4;nd!=(di+2)%4;nd=(nd+1)%4){\n\t\tint nx=x,ny=y;\n\t\twhile(on(nx,ny)){\n\t\t\tif(s[nx][ny]=='o'){\n\t\t\t\ts[nx][ny]='.';\n\t\t\t\tif(dfs(nx,ny,nd,cnt-1,s)){\n\t\t\t\t\tans=ddr[nd]+ans;\n\t\t\t\t\treturn true;\n\t\t\t\t}\n\t\t\t\ts[nx][ny]='o';\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tnx+=dx[nd],ny+=dy[nd];\n\t\t}\n\t}\n\treturn false;\n}\nint main(){\n\tcin>>H>>W;\n\trep(i,H){\n\t\tstring st;\n\t\tcin>>st;\n\t\ts.pb(st);\n\t}\n\tint sx,sy,di,cnt=0;\n\trep(i,H) rep(j,W){\n\t\tif(s[i][j]=='o') cnt++;\n\t\tif(s[i][j]=='U') sx=i,sy=j,di=0,s[i][j]='.';\n\t\tif(s[i][j]=='R') sx=i,sy=j,di=1,s[i][j]='.';\n\t\tif(s[i][j]=='D') sx=i,sy=j,di=2,s[i][j]='.';\n\t\tif(s[i][j]=='L') sx=i,sy=j,di=3,s[i][j]='.';\n\t}\n\tdfs(sx,sy,di,cnt,s);\n\tcout<<ans<<endl;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 1228, "score_of_the_acc": -0.1068, "final_rank": 7 }, { "submission_id": "aoj_2223_1123682", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nint h,w;\nconst int dy[] = {-1,0,1,0}, dx[] = {0,1,0,-1};\nconst string dir = \"URDL\";\nvector<string> g;\n\nstring dfs(int y,int x,int d,int rem,string s){\n if(rem==0)return s;\n char tmp = g[y][x];\n g[y][x] = '.';\n\n for(int i=3;i<=5;i++){\n int nd = (d+i)%4, ny = y + dy[nd], nx = x + dx[nd];\n while(ny>=0 && nx>=0 && ny<h && nx<w){\n if(g[ny][nx] == 'o'){\n\tstring tmp = dfs(ny,nx,nd,rem-1,s+dir[nd]);\n\tif(tmp.size())return tmp;\n\tbreak;\n }\n ny += dy[nd]; nx += dx[nd];\n }\n }\n\n g[y][x] = tmp;\n return \"\";\n}\n\nint main(){\n int y,x,d,cnt = 0;\n cin >> h >> w;\n g.resize(h);\n for(int i=0;i<h;i++){\n cin >> g[i];\n for(int j=0;j<w;j++){\n if(isupper(g[i][j])){\n\ty = i; x = j;\n\tfor(int k=0;k<4;k++){\n\t if(dir[k] == g[i][j])d = k;\n\t}\n }else if(g[i][j] == 'o')cnt++;\n }\n }\n cout << dfs(y,x,d,cnt,\"\") << endl;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1216, "score_of_the_acc": -0.035, "final_rank": 3 }, { "submission_id": "aoj_2223_901888", "code_snippet": "#include <vector>\n#include <map>\n#include <set>\n#include <stack>\n#include <queue>\n#include <algorithm>\n#include <utility>\n#include <functional>\n#include <sstream>\n#include <iostream>\n#include <cstdio>\n#include <cmath>\n#include <cstdlib>\n#include <cctype>\n#include <string>\n#include <cstring>\n#include <ctime>\n#include <climits>\n#include <fstream>\nusing namespace std;\ninline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v;}\ntemplate<class T> inline string toStr(T x) { ostringstream sout; sout << x; return sout.str();}\ntypedef vector<int> vi;\ntypedef vector<vi> vvi;\ntypedef vector<string> vs;\ntypedef pair<int, int> pii;\ntypedef long long ll;\n#define ALL(a) (a).begin(),(a).end()\n#define RALL(a) (a).rbegin(),(a).rend()\n#define FOR(i,a,b) for(int i=(a);i<=(b);++i)\n#define REP(i,n) FOR(i,0,(n)-1)\nconst double EPS = 1e-10;\nconst double PI = acos(-1.0);\nconst int INF = INT_MAX/10;\n\nint dx[] = {1, 0, -1, 0};\nint dy[] = {0, 1, 0, -1};\n\nstruct state {\n\tint x, y, dir;\n\tvs field;\n\tstring command;\n\tstate(int x, int y, int dir, vs field, string command) : x(x), y(y), dir(dir), field(field), command(command) {};\n};\n\nint getDir(char c) {\n\tswitch(c) {\n\tcase 'U':\n\t\treturn 3;\n\tcase 'D':\n\t\treturn 1;\n\tcase 'L':\n\t\treturn 2;\n\tcase 'R':\n\t\treturn 0;\n\t}\n}\n\nchar getDirChar(int dir) {\n\tswitch(dir) {\n\tcase 0:\n\t\treturn 'R';\n\tcase 1:\n\t\treturn 'D';\n\tcase 2:\n\t\treturn 'L';\n\tcase 3:\n\t\treturn 'U';\n\t}\n}\n\nbool isProperXY(int x, int y, int H, int W) {\n\treturn 0 <= x && x < W && 0 <= y && y < H;\n}\n\nbool isEmptyField(vs field) {\n\tREP(i, field.size()) {\n\t\tREP(j, field[i].size()) {\n\t\t\tif(field[i][j] != '.') {\n\t\t\t\treturn false;\n\t\t\t}\n\t\t}\n\t}\n\treturn true;\n}\n\nint main() {\n\tint H, W;\n\tcin >> H >> W;\n\tvs field(H);\n\n\tint x, y, dir;\n\tREP(i, H) {\n\t\tcin >> field[i];\n\t\tREP(j, field[i].size()) {\n\t\t\tif(field[i][j] != '.' && field[i][j] != 'o') {\n\t\t\t\tx = j;\n\t\t\t\ty = i;\n\t\t\t\tdir = getDir(field[i][j]);\n\t\t\t\tfield[i][j] = '.';\n\t\t\t}\n\t\t}\n\t}\n\n\tstring ans;\n\tstack<state> S;\n\tS.push(state(x, y, dir, field, \"\"));\n\twhile(!S.empty()) {\n\t\tstate st = S.top();\n\t\tS.pop();\n\n\t\tif(isEmptyField(st.field)) {\n\t\t\tans = st.command;\n\t\t\tbreak;\n\t\t}\n\n\t\tFOR(d, -1, 1) {\n\t\t\tint nx = st.x, ny = st.y, ndir = (st.dir+d+4)%4;\n\t\t\tbool update = true;\n\t\t\twhile(st.field[ny][nx] != 'o') {\n\t\t\t\tnx += dx[ndir], ny += dy[ndir];\n\t\t\t\tif(!isProperXY(nx, ny, H, W)) {\n\t\t\t\t\tupdate = false;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif(update) {\n\t\t\t\tvs nfield = st.field;\n\t\t\t\tnfield[ny][nx] = '.';\n\t\t\t\tstring ncommand = st.command;\n\t\t\t\tncommand.push_back(getDirChar(ndir));\n\t\t\t\tS.push(state(nx, ny, ndir, nfield, ncommand));\n\t\t\t}\n\t\t}\n\t}\n\n\tcout << ans << endl;\n\treturn 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 1232, "score_of_the_acc": -0.1783, "final_rank": 9 }, { "submission_id": "aoj_2223_901887", "code_snippet": "#include <vector>\n#include <map>\n#include <set>\n#include <stack>\n#include <queue>\n#include <algorithm>\n#include <utility>\n#include <functional>\n#include <sstream>\n#include <iostream>\n#include <cstdio>\n#include <cmath>\n#include <cstdlib>\n#include <cctype>\n#include <string>\n#include <cstring>\n#include <ctime>\n#include <climits>\n#include <fstream>\nusing namespace std;\ninline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v;}\ntemplate<class T> inline string toStr(T x) { ostringstream sout; sout << x; return sout.str();}\ntypedef vector<int> vi;\ntypedef vector<vi> vvi;\ntypedef vector<string> vs;\ntypedef pair<int, int> pii;\ntypedef long long ll;\n#define ALL(a) (a).begin(),(a).end()\n#define RALL(a) (a).rbegin(),(a).rend()\n#define FOR(i,a,b) for(int i=(a);i<=(b);++i)\n#define REP(i,n) FOR(i,0,(n)-1)\nconst double EPS = 1e-10;\nconst double PI = acos(-1.0);\nconst int INF = INT_MAX/10;\n\nint dx[] = {1, 0, -1, 0};\nint dy[] = {0, 1, 0, -1};\n\nstruct state {\n\tint x, y, dir;\n\tvs field;\n\tstring command;\n\tstate(int x, int y, int dir, vs field, string command) : x(x), y(y), dir(dir), field(field), command(command) {};\n};\n\nint getDir(char c) {\n\tswitch(c) {\n\tcase 'U':\n\t\treturn 3;\n\tcase 'D':\n\t\treturn 1;\n\tcase 'L':\n\t\treturn 0;\n\tcase 'R':\n\t\treturn 2;\n\t}\n}\n\nchar getDirChar(int dir) {\n\tswitch(dir) {\n\tcase 0:\n\t\treturn 'R';\n\tcase 1:\n\t\treturn 'D';\n\tcase 2:\n\t\treturn 'L';\n\tcase 3:\n\t\treturn 'U';\n\t}\n}\n\nbool isProperXY(int x, int y, int H, int W) {\n\treturn 0 <= x && x < W && 0 <= y && y < H;\n}\n\nbool isEmptyField(vs field) {\n\tREP(i, field.size()) {\n\t\tREP(j, field[i].size()) {\n\t\t\tif(field[i][j] != '.') {\n\t\t\t\treturn false;\n\t\t\t}\n\t\t}\n\t}\n\treturn true;\n}\n\nint main() {\n\tint H, W;\n\tcin >> H >> W;\n\tvs field(H);\n\n\tint x, y, dir;\n\tREP(i, H) {\n\t\tcin >> field[i];\n\t\tREP(j, field[i].size()) {\n\t\t\tif(field[i][j] != '.' && field[i][j] != 'o') {\n\t\t\t\tx = j;\n\t\t\t\ty = i;\n\t\t\t\tdir = getDir(field[i][j]);\n\t\t\t\tfield[i][j] = '.';\n\t\t\t}\n\t\t}\n\t}\n\n\tstring ans;\n\tstack<state> S;\n\tS.push(state(x, y, dir, field, \"\"));\n\twhile(!S.empty()) {\n\t\tstate st = S.top();\n\t\tS.pop();\n\n\t\tif(isEmptyField(st.field)) {\n\t\t\tans = st.command;\n\t\t\tbreak;\n\t\t}\n\n\t\tFOR(d, -1, 1) {\n\t\t\tint nx = st.x, ny = st.y, ndir = (st.dir+d+4)%4;\n\t\t\tbool update = true;\n\t\t\twhile(st.field[ny][nx] != 'o') {\n\t\t\t\tnx += dx[ndir], ny += dy[ndir];\n\t\t\t\tif(!isProperXY(nx, ny, H, W)) {\n\t\t\t\t\tupdate = false;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif(update) {\n\t\t\t\tvs nfield = st.field;\n\t\t\t\tnfield[ny][nx] = '.';\n\t\t\t\tstring ncommand = st.command;\n\t\t\t\tncommand.push_back(getDirChar(ndir));\n\t\t\t\tS.push(state(nx, ny, ndir, nfield, ncommand));\n\t\t\t}\n\t\t}\n\t}\n\n\tcout << ans << endl;\n\treturn 0;\n}", "accuracy": 0.38333333333333336, "time_ms": 30, "memory_kb": 1228, "score_of_the_acc": -0.1782, "final_rank": 20 }, { "submission_id": "aoj_2223_869350", "code_snippet": "#define _USE_MATH_DEFINES\n#define INF 0x3f3f3f3f\n#include <cstdio>\n#include <iostream>\n#include <sstream>\n#include <cmath>\n#include <cstdlib>\n#include <algorithm>\n#include <queue>\n#include <stack>\n#include <limits>\n#include <map>\n#include <string>\n#include <cstring>\n#include <set>\n#include <deque>\n#include <bitset>\n#include <list>\n#include <cctype>\n#include <utility>\n \nusing namespace std;\n \ntypedef long long ll;\ntypedef pair <int,int> P;\ntypedef pair <int,P > PP;\n \nconst int tx[] = {+0,+1,+0,-1};\nconst int ty[] = {-1,+0,+1,+0};\n \nstatic const double EPS = 1e-8;\n\nint init_dic(int x,int y,\n\t map<P,int>& dic){\n if(dic.find(P(x,y)) == dic.end()){\n int idx = dic.size();\n dic[P(x,y)] = idx;\n }\n return dic[P(x,y)];\n}\n\nvoid fill_dic(int W,int H,map<P,int>& dic){\n for(int x=0;x<=W;x++){\n for(int y=0;y<=H;y++){\n if(dic.find(P(x,y)) == dic.end()){\n\tdic[P(x,y)] = -1;\n }\n }\n }\n}\n\nclass State{\npublic:\n int x;\n int y;\n int dir;\n int remaining;\n int cost;\n vector<char> route;\n State(int _x,int _y,int _dir,int _remaining,int _cost,const vector<char>& _r)\n : x(_x),y(_y),dir(_dir),remaining(_remaining),cost(_cost),route(_r) {}\n State(int _x,int _y,int _dir,int _remaining,int _cost)\n : x(_x),y(_y),dir(_dir),remaining(_remaining),cost(_cost) {}\n bool operator<(const State& s) const{\n return cost < s.cost;\n }\n bool operator>(const State& s) const{\n return cost > s.cost;\n }\n};\n\nint main(){\n int H,W;\n while(~scanf(\"%d %d\",&H,&W)){\n char stage[101][101];\n int sx,sy;\n int dir;\n int init_state=0;\n map<P,int> dic;\n\n for(int y=0;y<H;y++){\n char buf[101];\n scanf(\"%s\",buf);\n for(int x=0;x<W;x++){\n\tstage[y][x] = buf[x];\n\tif(buf[x] == 'U' || buf[x] == 'R'\n\t || buf[x] == 'D' || buf[x] == 'L'){\n\t sx = x;\n\t sy = y;\n\t init_state |= (1<<init_dic(x,y,dic));\n\n\t const char dirs[4] = {'U','R','D','L'};\n\t for(int i=0;i<4;i++){\n\t if(dirs[i] == buf[x]){\n\t dir = i;\n\t break;\n\t }\n\t }\n\t}\n\tif(buf[x] == 'o'){\n\t init_state |= (1<<init_dic(x,y,dic));\n\t}\n }\n }\n \n fill_dic(W,H,dic);\n priority_queue<State,vector<State>,greater<State> > que;\n que.push(State(sx,sy,dir,init_state,0));\n\n set<int> visited[101][101][4];\n bool flag = false;\n while(!que.empty()){\n State s = que.top();\n que.pop();\n\n if(visited[s.x][s.y][s.dir].count(s.remaining) > 0) continue;\n visited[s.x][s.y][s.dir].insert(s.remaining);\n\n if(__builtin_popcount(s.remaining) == 1){\n\tconst char dirs[4] = {'U','R','D','L'};\n\tstring ans = \"\";\n\tfor(int i=0; i<s.route.size(); i++){\n\t ans += dirs[s.route[i]];\n\t}\n\tcout << ans << endl;\n }\n\n for(int i=0;i<4;i++){\n\tif(i == (s.dir + 2) % 4) continue;\n\n\tint dx,dy;\n\tfor(int j=1;j<=max(H,W);j++){\n\t dx = s.x + tx[i] * j;\n\t dy = s.y + ty[i] * j;\n\t if(dx < 0 || dy < 0 || dx >= W || dy >= H ) continue;\n\t if(dic[P(dx,dy)] == -1) continue;\n\t \n\t if(s.remaining & (1<<dic[P(dx,dy)])){\n\t int next = s.remaining & ~(1<<dic[P(s.x,s.y)]);\n\t vector<char> next_route = s.route;\n\t next_route.push_back(i);\n\t que.push(State(dx,dy,i,next,s.cost+1,next_route));\n\t break;\n\t }\n\t}\n }\n }\n }\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 4524, "score_of_the_acc": -0.2731, "final_rank": 13 }, { "submission_id": "aoj_2223_742305", "code_snippet": "#include <vector>\n#include <list>\n#include <map>\n#include <set>\n#include <deque>\n#include <queue>\n#include <stack>\n#include <bitset>\n#include <algorithm>\n#include <functional>\n#include <numeric>\n#include <utility>\n#include <sstream>\n#include <fstream>\n#include <iostream>\n#include <iomanip>\n#include <cstdio>\n#include <cmath>\n#include <cstdlib>\n#include <ctime>\n#include <cstring>\nusing namespace std;\nint n,m;\nchar a[15][15];\nbool bo[15][15];\nint dx[]={-1,0,1,0},dy[]={0,1,0,-1};\nchar UNdir(int x)\n{\n if (x==0) return 'U';\n if (x==1) return 'R';\n if (x==2) return 'D';\n if (x==3) return 'L';\n}\nint dir(char x)\n{\n if (x=='U') return 3;\n if (x=='R') return 0;\n if (x=='D') return 1;\n if (x=='L') return 2;\n}\nstruct node\n{\n int d;\n int x[105];\n int y[105];\n int num;\n char fa;\n string ans;\n};\nbool in(int x,int y)\n{\n return (x>=0 && x<n && y>=0 && y<m);\n}\nvoid out(node x)\n{\n char xx[15][15];\n for (int i = 0; i < n; ++i)\n {\n for (int j = 0; j < m; ++j)\n {\n xx[i][j]='.';\n }\n }\n xx[x.d/m][x.d%m]=x.fa;\n for (int i = 0; i < x.num; ++i)\n {\n xx[x.x[i]][x.y[i]]='o';\n }\n for (int i = 0; i < n; ++i)\n {\n for (int j = 0; j < m; ++j)\n {\n cout<<xx[i][j];\n }\n cout<<endl;\n }\n cout<<endl;\n}\nint ans=0,ansd=0;\nint main(int argc, char *argv[])\n{\n int T;\n cin>>n>>m;\n node xx;xx.num=0;\n xx.ans=\"\";\n for (int i = 0; i < n; ++i)\n {\n for (int j = 0 ; j < m; ++j)\n {\n cin>>a[i][j];\n if (a[i][j]=='o')\n {\n xx.x[xx.num]=i;\n xx.y[xx.num++]=j;\n }\n else if (a[i][j]=='U'||a[i][j]=='L'||a[i][j]=='R'||a[i][j]=='D')\n {\n xx.d=i*m+j;\n xx.fa=a[i][j];\n }\n }\n }\n queue<node> q;\n q.push(xx);\n bool gan=0;\n while (!q.empty())\n {\n node now=q.front();\n \n if (now.num==0)\n {\n cout<<now.ans<<endl;\n break;\n }\n q.pop();\n int x=now.d/m,y=now.d%m;\n memset(bo,0,sizeof(bo));\n bo[x][y]=1;\n for (int i = 0; i < now.num; ++i)\n {\n bo[now.x[i]][now.y[i]]=1;\n }\n int face=dir(now.fa);\n for (int i = face; i < face+3; ++i)\n {\n int ii=i%4;\n int l=1;\n int xx=x+dx[ii],yy=y+dy[ii];\n while (1)\n {\n if (!in(xx,yy)) break;\n if (bo[xx][yy])\n {\n node go;\n go.d=xx*m+yy;\n go.ans=now.ans+UNdir(ii);\n go.fa=UNdir(ii);\n go.num=0;\n for (int j = 0; j < now.num; ++j)\n {\n int xxx=now.x[j],yyy=now.y[j];\n if (!(xxx==xx&&yyy==yy))\n {\n go.x[go.num]=xxx;\n go.y[go.num++]=yyy;\n }\n }\n q.push(go); \n break;\n }\n xx+=dx[ii];\n yy+=dy[ii];\n }\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 5952, "score_of_the_acc": -0.1713, "final_rank": 8 }, { "submission_id": "aoj_2223_408860", "code_snippet": "#include <iostream>\n#include <iomanip>\n#include <sstream>\n#include <cstdio>\n#include <string>\n#include <vector>\n#include <algorithm>\n#include <complex>\n#include <cstring>\n#include <cstdlib>\n#include <cmath>\n#include <cassert>\n#include <climits>\n#include <queue>\n#include <set>\n#include <map>\n#include <valarray>\n#include <bitset>\n#include <stack>\nusing namespace std;\n\n#define REP(i,n) for(int i=0;i<(int)n;++i)\n#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)\n#define ALL(c) (c).begin(), (c).end()\ntypedef long long ll;\ntypedef pair<int,int> pii;\nconst int INF = 1<<29;\nconst double PI = acos(-1);\nconst double EPS = 1e-8;\n\nstring ba;\n\nstruct P {\n string s;\n int x, y;\n P(const string &s, int y, int x) : s(s),y(y),x(x) {}\n};\n\nint main() {\n int h,w;\n while(cin >> h >> w) {\n ba = \"\";\n REP(i,h) {\n string s; cin >> s;\n ba += s;\n }\n const string strdir = \"URDL\";\n map<char, int> mp;\n REP(i,4) mp[strdir[i]] = i;\n const int dx[] = {0,1,0,-1};\n const int dy[] = {-1,0,1,0};\n queue<P> Q;\n REP(i,h) REP(j,w) \n REP(k,4) if (ba[i*w+j] == strdir[k]) Q.push(P(ba,i,j));\n map<string, pair<string, int> > pre;\n pre[ba] = make_pair(\"\",0);\n string last;\n while(!Q.empty()) {\n P p = Q.front(); Q.pop();\n\n bool f = 0;\n REP(i,w*h) if (p.s[i] == 'o') f = 1;\n if (!f) {\n last = p.s;\n break;\n }\n\n // REP(i,h) {\n // REP(j,w) cout << p.s[i*w+j];cout << endl;\n // }\n \n int y=p.y, x = p.x;\n int sd = mp[p.s[y*w+x]];\n //p.s[y*w+x] = '.';\n for (int dd=sd-1; dd<=sd+1; ++dd) {\n int d = (dd+4)%4;\n int yy=y, xx=x;\n while(1) {\n yy+=dy[d];\n xx+=dx[d];\n if (yy<0||yy>=h||xx<0||xx>=w) break;\n if (p.s[yy*w+xx] == 'o') {\n string next = p.s;\n next[y*w+x] = '.';\n next[yy*w+xx] = strdir[d];\n if (pre.count(next) == 0) {\n pre[next] = make_pair(p.s,d);\n Q.push(P(next,yy,xx));\n }\n break;\n }\n }\n }\n }\n vector<char> ans;\n for(string now=last; now!=\"\"; now=pre[now].first) {\n //REP(i,h){REP(j,w)cout<<now[i*w+j];cout<<endl;}\n ans.push_back(strdir[pre[now].second]);\n }\n ans.pop_back();\n reverse(ALL(ans));\n FOR(it,ans) cout << *it; cout << endl;\n }\n \n}", "accuracy": 1, "time_ms": 100, "memory_kb": 8264, "score_of_the_acc": -0.8808, "final_rank": 18 }, { "submission_id": "aoj_2223_401063", "code_snippet": "#include <iostream>\n#include <sstream>\n#include <string>\n#include <algorithm>\n#include <vector>\n#include <stack>\n#include <queue>\n#include <set>\n#include <map>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <cmath>\n#include <cassert>\n\nusing namespace std;\n\n#define FOR(i,k,n) for(int i=(k); i<(int)n; ++i)\n#define REP(i,n) FOR(i,0,n)\n#define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)\n\ntemplate<class T> void debug(T begin, T end){ for(T i = begin; i != end; ++i) cout<<*i<<\" \"; cout<<endl; }\n\ntypedef long long ll;\nconst int INF = 100000000;\nconst double EPS = 1e-8;\nconst int MOD = 1000000007;\nint H, W;\nint grid[11][11];\nint dx[4] = {0, 0, -1, 1};\nint dy[4] = {-1, 1, 0, 0};\nstring state = \"UDLR\";\nint lim[4] = {1,0,3,2};\nbool used[11][11];\nstring dfs(int x, int y, int s, int rem, string res){\n //printf(\"dfs:%d %d %d %d %s\\n\", x, y, s, rem, res.c_str());\n if(rem == 0){\n return res;\n }\n REP(r, 4)if(r != lim[s]){\n REP(p, 10){\n int nx = x + p * dx[r];\n int ny = y + p * dy[r];\n if(0 <= nx && 0 <= ny && nx < W && ny < H && !used[ny][nx] && grid[ny][nx]){\n used[ny][nx] = true;\n //printf(\"call %d %d %d %d %s\\n\", nx, ny, r, rem -1, (res+state[r]).c_str());\n string response = dfs(nx, ny, r, rem-1, res+state[r]);\n used[ny][nx] = false;\n if(response != \"\") return response;\n break;\n }\n }\n }\n return \"\";\n}\n\nint main(){\n while(cin>>H>>W && H){\n int nx, ny, s;\n int cnt = 0;\n REP(y, H){\n string t; cin>>t;\n REP(x, W){\n if(t[x] == 'o'){\n grid[y][x] = 1;\n cnt += 1;\n }else if(t[x] == '.'){\n grid[y][x] = 0;\n }else{\n grid[y][x] = 0;\n nx = x;\n ny = y;\n REP(i, 4)if(state[i] == t[x])s = i;\n }\n }\n }\n memset(used, 0, sizeof(used));\n string ans = dfs(nx, ny, s, cnt, \"\");\n cout<<ans<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 0, "score_of_the_acc": 0, "final_rank": 1 }, { "submission_id": "aoj_2223_401042", "code_snippet": "#include <iostream>\n#include <string>\n#include <cstring>\nusing namespace std;\n\nint dx[] = {0, -1, 0, 1};\nint dy[] = {-1, 0, 1, 0};\nchar dir[] = {'U', 'L', 'D', 'R'};\n\nint W,H;\nstring field[10];\n\nstring solve(int x, int y, int d, int c, string str)\n{\n\tif(c == 0)\treturn str;\n\t\n\n\tstring res;\n\tfor(int i=-1; i<2; i++) {\n\t\tbool ok = false;\n\t\tint nx=x, ny=y, nd=(d+i+4)%4;\n\t\twhile(1) {\n\t\t\tnx += dx[nd]; ny+=dy[nd];\n\t\t\tif(nx<0||ny<0||nx>=W||ny>=H) break;\n\t\t\tif(field[ny][nx]=='o') {\n\t\t\t\tok=true;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\n\t\tif(ok) {\n\t\t\tfield[ny][nx] = '.';\n\t\t\tres = max(res, solve(nx,ny,nd,c-1,str+dir[nd]));\n\t\t\tfield[ny][nx] = 'o';\n\t\t}\n\n\t}\n\n\treturn res;\n}\n\nint main()\n{\n\tcin >> H >> W;\n\tfor(int i=0; i<H; i++)\n\t\tcin >> field[i];\n\n\tint sx,sy,sd,cnt=0;\n\tfor(int i=0; i<H; i++)\n\tfor(int j=0; j<W; j++)\n\t{\n\t\tif(field[i][j] == 'o') cnt++;\n\n\t\tif(field[i][j]=='U') sd = 0;\n\t\tif(field[i][j]=='L') sd = 1;\n\t\tif(field[i][j]=='D') sd = 2;\n\t\tif(field[i][j]=='R') sd = 3;\n\n\t\tif(isupper(field[i][j])) {\n\t\t\tsx=j; sy=i;\n\t\t}\n\t}\n\n\tcout << solve(sx,sy,sd,cnt,\"\") << endl;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 0, "score_of_the_acc": 0, "final_rank": 1 }, { "submission_id": "aoj_2223_332047", "code_snippet": "#include <iostream>\n#include <string>\nusing namespace std;\nint h,w;\nchar field[11][11];\nint sang;\nint sx,sy;\nint leaf;\nchar rec[1001];\nbool fin;\nconst string ss=\"UUULLDDRRR\";\n\nvoid dfs(int cx,int cy,int cang,int cnt){\n\tif(fin)return;\n\tif(leaf==cnt){\n\t\tfin=true;\n\t\tfor(int i=0;i<leaf;i++)cout<<rec[i];\n\t\tcout<<endl;\n\t\treturn;\n\t}\n\tstring s;\n\tfor(int i=0;i<cnt;i++)s+=rec[i];\n\tif(ss==s){\n\t//\tcout<<endl;\n\t}\n\tif(cang!=2){\n\t\tfor(int i=-1;i+cy>=0;i--){\n\t\t\tint nx=cx;\n\t\t\tint ny=i+cy;\n\t\t\tif(field[ny][nx]=='o'){\n\t\t\t\tfield[ny][nx]='.';\n\t\t\t\trec[cnt]='U';\n\t\t\t\tdfs(nx,ny,0,cnt+1);\n\t\t\t\tfield[ny][nx]='o';\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n\tif(cang!=0){\n\t\tfor(int i=1;i+cy<h;i++){\n\t\t\tint nx=cx;\n\t\t\tint ny=i+cy;\n\t\t\tif(field[ny][nx]=='o'){\n\t\t\t\tfield[ny][nx]='.';\n\t\t\t\trec[cnt]='D';\n\t\t\t\tdfs(nx,ny,2,cnt+1);\n\t\t\t\tfield[ny][nx]='o';\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n\tif(cang!=3){\n\t\tfor(int i=1;i+cx<w;i++){\n\t\t\tint nx=cx+i;\n\t\t\tint ny=cy;\n\t\t\tif(field[ny][nx]=='o'){\n\t\t\t\tfield[ny][nx]='.';\n\t\t\t\trec[cnt]='R';\n\t\t\t\tdfs(nx,ny,1,cnt+1);\n\t\t\t\tfield[ny][nx]='o';\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n\tif(cang!=1){\n\t\tfor(int i=-1;i+cx>=0;i--){\n\t\t\tint nx=cx+i;\n\t\t\tint ny=cy;\n\t\t\tif(field[ny][nx]=='o'){\n\t\t\t\tfield[ny][nx]='.';\n\t\t\t\trec[cnt]='L';\n\t\t\t\tdfs(nx,ny,3,cnt+1);\n\t\t\t\tfield[ny][nx]='o';\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n}\n\nint main(){\n\n\tcin>>h>>w;\n\tfin=false;\n\tfor(int i=0;i<h;i++){\n\t\tfor(int j=0;j<w;j++){\n\t\t\tcin>>field[i][j];\n\t\t\tif(field[i][j]=='o')leaf++;\n\t\t\telse if(field[i][j]!='.'&&field[i][j]!='o'){\n\t\t\t\tswitch(field[i][j]){\n\t\t\t\tcase 'U':\n\t\t\t\t\tsang=0;\n\t\t\t\t\tbreak;\n\t\t\t\tcase 'D':\n\t\t\t\t\tsang=2;\n\t\t\t\t\tbreak;\n\t\t\t\tcase 'R':\n\t\t\t\t\tsang=1;\n\t\t\t\t\tbreak;\n\t\t\t\tcase 'L':\n\t\t\t\t\tsang=3;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tfield[i][j]='.';\n\t\t\t\tsx=j;sy=i;\n\t\t\t}\n\t\t}\n\t}\n\tdfs(sx,sy,sang,0);\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 0, "score_of_the_acc": -0.0714, "final_rank": 4 } ]
aoj_2224_cpp
Problem C: Save your cats Nicholas Y. Alford was a cat lover. He had a garden in a village and kept many cats in his garden. The cats were so cute that people in the village also loved them. One day, an evil witch visited the village. She envied the cats for being loved by everyone. She drove magical piles in his garden and enclosed the cats with magical fences running between the piles. She said “Your cats are shut away in the fences until they become ugly old cats.” like a curse and went away. Nicholas tried to break the fences with a hummer, but the fences are impregnable against his effort. He went to a church and asked a priest help. The priest looked for how to destroy the magical fences in books and found they could be destroyed by holy water. The Required amount of the holy water to destroy a fence was proportional to the length of the fence. The holy water was, however, fairly expensive. So he decided to buy exactly the minimum amount of the holy water required to save all his cats. How much holy water would be required? Input The input has the following format: N M x 1 y 1 . . . x N y N p 1 q 1 . . . p M q M The first line of the input contains two integers N (2 ≤ N ≤ 10000) and M (1 ≤ M ). N indicates the number of magical piles and M indicates the number of magical fences. The following N lines describe the coordinates of the piles. Each line contains two integers x i and y i (-10000 ≤ x i , y i ≤ 10000). The following M lines describe the both ends of the fences. Each line contains two integers p j and q j (1 ≤ p j , q j ≤ N ). It indicates a fence runs between the p j -th pile and the q j -th pile. You can assume the following: No Piles have the same coordinates. A pile doesn’t lie on the middle of fence. No Fences cross each other. There is at least one cat in each enclosed area. It is impossible to destroy a fence partially. A unit of holy water is required to destroy a unit length of magical fence. Output Output a line containing the minimum amount of the holy water required to save all his cats. Your program may output an arbitrary number of digits after the decimal point. However, the absolute error should be 0.001 or less. Sample Input 1 3 3 0 0 3 0 0 4 1 2 2 3 3 1 Output for the Sample Input 1 3.000 Sample Input 2 4 3 0 0 -100 0 100 0 0 100 1 2 1 3 1 4 Output for the Sample Input 2 0.000 Sample Input 3 6 7 2 0 6 0 8 2 6 3 0 5 1 7 1 2 2 3 3 4 4 1 5 1 5 4 5 6 Output for the Sample Input 3 7.236 Sample Input 4 6 6 0 0 0 1 1 0 30 0 0 40 30 40 1 2 2 3 3 1 4 5 5 6 6 4 Output for the Sample Input 4 31.000
[ { "submission_id": "aoj_2224_10848478", "code_snippet": "#include <iostream>\n#include <cstring>\n#include <cstdio>\n#include <cmath>\n#include <vector>\nusing namespace std;\nconst int maxn=1e4+5;\ntypedef long long LL;\nstruct Point\n{\n int x;\n int y;\n};\n\nstruct edge\n{\n int to;\n double cost;\n};\nPoint a[maxn];\nvector<edge> g[maxn];\nbool used[maxn];\nint n,m;\ndouble maxcost[maxn];\ndouble cal_distance(Point a,Point b)\n{\n return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));\n}\ndouble prim(int p)\n{\n double ans=0.0;\n maxcost[p]=0.0;\n while(1)\n {\n int v=-1;\n for(int u=1;u<=n;u++)\n if(!used[u]&&(v==-1||maxcost[u]>maxcost[v]))\n v=u;\n if(v==-1||maxcost[v]==-1)\n break;\n used[v]=1;\n ans+=maxcost[v];\n for(int i=0;i<g[v].size();i++)\n {\n edge &e=g[v][i];\n maxcost[e.to]=max(maxcost[e.to],e.cost);\n }\n }\n return ans;\n}\nint main()\n{\n while(scanf(\"%d%d\",&n,&m)!=EOF)\n {\n for(int i=1;i<=n;i++)\n g[i].clear();\n for(int i=1;i<=n;i++)\n scanf(\"%d%d\",&a[i].x,&a[i].y);\n int p1,p2;\n double sum=0;\n for(int i=1;i<=m;i++)\n {\n scanf(\"%d%d\",&p1,&p2);\n double tmp=cal_distance(a[p1],a[p2]);\n g[p1].push_back((edge){p2,tmp});\n g[p2].push_back((edge){p1,tmp});\n sum+=tmp;\n }\n// for(int i=1;i<=n;cout<<endl,i++)\n// for(int j=1;j<=n;j++)\n// cout<<cost[i][j]<<\" \";\n// cout<<sum<<endl;\n memset(used,0,sizeof(used));\n for(int i=0;i<=n;i++)\n maxcost[i]=-1;\n double ans=0.0;\n for(int u=1;u<=n;u++)\n if(!used[u])\n ans+=prim(u);\n ans=sum-ans;\n printf(\"%.3f\\n\",ans);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 220, "memory_kb": 5484, "score_of_the_acc": -1.2614, "final_rank": 18 }, { "submission_id": "aoj_2224_10375836", "code_snippet": "#include <iostream>\n#include <cmath>\n#include <vector>\n#include <map>\n#include <stack>\n#include <queue>\n#include <set>\n#include <algorithm>\n#include <iomanip>\n#include <string.h>\n\n#define FOR(i,a,b) for(int i=(a);i<(b);++i)\n#define REP(i,n) FOR(i,0,n)\n#define ALL(a) (a).begin(),(a).end()\n\ntypedef long long lint;\n\nusing namespace std;\n\nstruct UnionFind{\n vector<int> par;\n UnionFind(int N):par(N){\n REP(i,N)par[i]=i;\n }\n int root(int x){\n if(par[x]==x)return x;\n return par[x]=root(par[x]);\n }\n void unite(int x,int y){\n int rx=root(x);\n int ry=root(y);\n if(rx==ry)return;\n par[rx]=ry;\n }\n};\n\nint main(){\n int N,M;\n cin>>N>>M;\n vector<pair<lint,lint> > piles;\n vector<pair<int,int> > fence;\n REP(i,N){\n int x,y;\n cin>>x>>y;\n piles.push_back(make_pair(x,y));\n }\n REP(i,M){\n int p,q;\n cin>>p>>q;\n p--;q--; // 0index\n fence.push_back(make_pair(p,q));\n }\n // 各フェンスを切るのにかかるコスト\n vector<pair<double,int> > cost;\n REP(i,M){\n auto [x1,y1] = piles[fence[i].first];\n auto [x2,y2] = piles[fence[i].second];\n double c = sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); \n // 降順ソートが面倒なので負数にしておく\n cost.push_back(make_pair(-c,i));\n }\n double ans = 0;\n UnionFind tree(N);\n // コストの高いフェンスから閉路ができないように貪欲につないでいく\n sort(ALL(cost)); // vector<pair>をソートすると第一要素の昇順ソートになる\n for(auto ci : cost){\n auto [c, i] = ci;\n auto [p,q] = fence[i];\n // 閉路が形成されるならつながない = 切るフェンスとして答えに加える\n if(tree.root(p)==tree.root(q)){\n ans += -c;\n }else{\n tree.unite(p,q);\n }\n }\n //固定小数点\n cout<<fixed;\n //桁数固定\n cout<<setprecision(10);\n cout<<ans<<endl;\n\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 4164, "score_of_the_acc": -0.1043, "final_rank": 8 }, { "submission_id": "aoj_2224_10375676", "code_snippet": "#include <iostream>\n#include <cmath>\n#include <vector>\n#include <map>\n#include <stack>\n#include <queue>\n#include <set>\n#include <algorithm>\n#include <iomanip>\n#include <string.h>\n\n#define FOR(i,a,b) for(int i=(a);i<(b);++i)\n#define REP(i,n) FOR(i,0,n)\n#define ALL(a) (a).begin(),(a).end()\n\ntypedef long long lint;\n\nusing namespace std;\n\nstruct UnionFind{\n vector<int> par;\n UnionFind(int N):par(N){\n REP(i,N)par[i]=i;\n }\n int root(int x){\n if(par[x]==x)return x;\n return par[x]=root(par[x]);\n }\n void unite(int x,int y){\n int rx=root(x);\n int ry=root(y);\n if(rx==ry)return;\n par[rx]=ry;\n }\n};\n\nint main(){\n int N,M;\n cin>>N>>M;\n vector<pair<lint,lint> > piles;\n vector<pair<int,int> > fence;\n REP(i,N){\n int x,y;\n cin>>x>>y;\n piles.push_back(make_pair(x,y));\n }\n REP(i,M){\n int p,q;\n cin>>p>>q;\n p--;q--; // 0index\n fence.push_back(make_pair(p,q));\n }\n // 各フェンスを切るのにかかるコスト\n vector<pair<double,int> > cost;\n REP(i,M){\n auto [x1,y1] = piles[fence[i].first];\n auto [x2,y2] = piles[fence[i].second];\n double c = -sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); // 降順ソートが面倒なので負数にしておく\n cost.push_back(make_pair(c,i));\n }\n double ans = 0;\n UnionFind tree(N);\n // コストの高いフェンスから貪欲に繋いでいく\n sort(ALL(cost));\n for(auto ci : cost){\n auto [c, i] = ci;\n auto [p,q] = fence[i];\n // ループが形成されるならつながない\n if(tree.root(p)==tree.root(q)){\n ans += -c;\n }else{\n tree.unite(p,q);\n }\n }\n //固定小数点\n cout<<fixed;\n //桁数固定\n cout<<setprecision(10);\n cout<<ans<<endl;\n\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 4096, "score_of_the_acc": -0.0917, "final_rank": 6 }, { "submission_id": "aoj_2224_9669004", "code_snippet": "#include <bits/stdc++.h>\n//#include <atcoder/all>\n#include <unordered_set>\n#include <functional>\nusing namespace std;\n//using namespace atcoder;\nusing ll = long long;\nusing P = pair<int, int>;\n#define rep(i, n) for (int i = 0; i < (n); i++)\n//void chmin(ll & a, ll b) { a = min(a, b); }\n//using mint = modint998244353;\n//using mint = modint1000000007;\n\nstruct UnionFind {\n\tvector<int> par, rank;\n\n\tUnionFind(int n = 210000) { init(n); }\n\tvoid init(int n = 210000) {\n\t\tpar.resize(n);\n\t\trank.resize(n);\n\t\trep(i, n) par[i] = i, rank[i] = 0;\n\t}\n\n\tint root(int x) {\n\t\tif (par[x] == x) return x;\n\t\telse return par[x] = root(par[x]);\n\t}\n\n\tbool issame(int x, int y) {\n\t\treturn root(x) == root(y);\n\t}\n\n\tbool merge(int x, int y) {\n\t\tx = root(x), y = root(y);\n\t\tif (x == y) return false;\n\t\tif (rank[x] < rank[y]) swap(x, y);\n\t\tif (rank[x] == rank[y]) rank[x]++;\n\t\tpar[y] = x;\n\t\treturn true;\n\t}\n};\n\ntypedef pair<double, P> Edge;\n\nint main() {\n\tint N, M;\n\tcin >> N >> M;\n\tvector<double> x(N), y(N);\n\trep(i, N) cin >> x[i] >> y[i];\n\tvector<Edge> edges;\n\tdouble all = 0;\n\tfor (int i = 0; i < M; i++) {\n\t\tint u, v;\n\t\tcin >> u >> v;\n\t\tu--, v--;\n\t\tdouble dis = sqrt((x[u] - x[v]) * (x[u] - x[v]) + (y[u] - y[v]) * (y[u] - y[v]));\n\t\tedges.push_back(Edge(dis, P(u, v)));\n\t\tall += dis;\n\t}\n\n\tsort(edges.rbegin(), edges.rend());\n\tUnionFind uf(N);\n\tdouble hosyu = 0;\n\tfor (auto e : edges) {\n\t\tint u = e.second.first, v = e.second.second;\n\t\tif (uf.issame(u, v)) continue;\n\t\thosyu += e.first;\n\t\tuf.merge(u, v);\n\t}\n\n\tcout << fixed << setprecision(10) << all - hosyu << endl;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3956, "score_of_the_acc": -0.0658, "final_rank": 4 }, { "submission_id": "aoj_2224_9668991", "code_snippet": "#define _CRT_SECURE_NO_WARNINGS\n#include<iostream>\n#include<algorithm>\n#include <vector>\n#include <string>\n#include <queue>\n#include <stdio.h>\n#include <map>\n#include<sstream>\n#include <set>\n#include <string.h>\n#include <math.h>\n#include <cmath>\n#include <cstdio>\n#include <iomanip>\nusing namespace std;\ntypedef pair<int, int> P;\ntypedef pair<double, double> Pd;\ntypedef long long ll;\n#define rep(i,n) for(int i=0;i<(n);i++)\n\nstruct edge {\n\tint from, to; \n\tdouble cost;\n\tedge() {}\n\tedge(int f, int t, double c):from(f),to(t),cost(c){}\n\tbool operator<(const edge& b)const{\n\t\treturn cost > b.cost;\n\t}\n};\n\nint par[10009];\nint ranks[10009];\n\nvoid init(int n) {\n\trep(i, n) {\n\t\tpar[i] = i;\n\t\tranks[i] = 0;\n\t}\n}\n\nint find(int x) {\n\tif (x == par[x]) {\n\t\treturn x;\n\t}\n\telse {\n\t\treturn par[x] = find(par[x]);\n\t}\n}\n\nvoid unite(int x, int y) {\n\tx = find(x);\n\ty = find(y);\n\tif (x == y) return;\n\n\tif (ranks[x] < ranks[y]) {\n\t\tpar[x] = y;\n\t}\n\telse {\n\t\tpar[y] = x;\n\t\tif (ranks[x] == ranks[y]) ranks[x]++;\n\t}\n}\n\nbool same(int x, int y) {\n\treturn find(x) == find(y);\n}\n\nint main() {\n\tint N, M;\n\tcin >> N >> M;\n\tvector<edge> e;\n\tvector<int> x(N), y(N);\n\trep(i, N) cin >> x[i] >> y[i];\n\n\tauto calc_dist = [&](int p, int q) ->double {\n\t\treturn sqrt((x[p] - x[q]) * (x[p] - x[q]) + (y[p] - y[q]) * (y[p] - y[q]));\n\t\t};\n\n\tdouble sum = 0;\n\trep(i, M) {\n\t\tint p, q;\n\t\tcin >> p >> q;\n\t\tp--, q--;\n\t\te.push_back(edge(p, q, calc_dist(p, q)));\n\t\te.push_back(edge(q, p, calc_dist(q, p)));\n\t\tsum += calc_dist(p, q);\n\t}\n\n\tsort(e.begin(), e.end());\n\tinit(N);\n\t\n\tdouble sub_sum = 0;\n\trep(i, e.size()) {\n\t\tif (same(e[i].from, e[i].to)) continue;\n\t\tsub_sum += e[i].cost;\n\t\tunite(e[i].from, e[i].to);\n\t}\n\n\tcout << fixed <<setprecision(12) <<sum - sub_sum << endl;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 4200, "score_of_the_acc": -0.1109, "final_rank": 10 }, { "submission_id": "aoj_2224_9544211", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n#include <unistd.h>\n\nnamespace fastio {\nstatic constexpr uint32_t SZ = 1 << 17;\nchar ibuf[SZ];\nchar obuf[SZ];\nchar out[100];\n// pointer of ibuf, obuf\n\n\nuint32_t pil = 0, pir = 0, por = 0;\n\nstruct Pre {\n char num[10000][4];\n constexpr Pre() : num() {\n for (int i = 0; i < 10000; i++) {\n int n = i;\n for (int j = 3; j >= 0; j--) {\n num[i][j] = n % 10 | '0';\n n /= 10;\n }\n }\n }\n} constexpr pre;\n\ninline void load() {\n memmove(ibuf, ibuf + pil, pir - pil);\n pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin);\n pil = 0;\n if (pir < SZ)\n ibuf[pir++] = '\\n';\n}\n\ninline void flush() {\n fwrite(obuf, 1, por, stdout);\n por = 0;\n}\n\nvoid rd(char &c) {\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n}\n\nvoid rd(string &x) {\n x.clear();\n char c;\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n do {\n x += c;\n if (pil == pir)\n load();\n c = ibuf[pil++];\n } while (!isspace(c));\n}\n\ntemplate <typename T> void rd_real(T &x) {\n string s;\n rd(s);\n x = stod(s);\n}\n\ntemplate <typename T> void rd_integer(T &x) {\n if (pil + 100 > pir)\n load();\n char c;\n do\n c = ibuf[pil++];\n while (c < '-');\n bool minus = 0;\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (c == '-') {\n minus = 1, c = ibuf[pil++];\n }\n }\n x = 0;\n while ('0' <= c) {\n x = x * 10 + (c & 15), c = ibuf[pil++];\n }\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (minus)\n x = -x;\n }\n}\n\nvoid rd(int &x) {\n rd_integer(x);\n}\nvoid rd(ll &x) {\n rd_integer(x);\n}\nvoid rd(i128 &x) {\n rd_integer(x);\n}\nvoid rd(uint &x) {\n rd_integer(x);\n}\nvoid rd(ull &x) {\n rd_integer(x);\n}\nvoid rd(u128 &x) {\n rd_integer(x);\n}\nvoid rd(double &x) {\n rd_real(x);\n}\nvoid rd(long double &x) {\n rd_real(x);\n}\n\ntemplate <class T, class U> void rd(pair<T, U> &p) {\n return rd(p.first), rd(p.second);\n}\ntemplate <size_t N = 0, typename T> void rd_tuple(T &t) {\n if constexpr (N < std::tuple_size<T>::value) {\n auto &x = std::get<N>(t);\n rd(x);\n rd_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void rd(tuple<T...> &tpl) {\n rd_tuple(tpl);\n}\n\ntemplate <size_t N = 0, typename T> void rd(array<T, N> &x) {\n for (auto &d : x)\n rd(d);\n}\ntemplate <class T> void rd(vector<T> &x) {\n for (auto &d : x)\n rd(d);\n}\n\nvoid read() {}\ntemplate <class H, class... T> void read(H &h, T &...t) {\n rd(h), read(t...);\n}\n\nvoid wt(const char c) {\n if (por == SZ)\n flush();\n obuf[por++] = c;\n}\nvoid wt(const string s) {\n for (char c : s)\n wt(c);\n}\nvoid wt(const char *s) {\n size_t len = strlen(s);\n for (size_t i = 0; i < len; i++)\n wt(s[i]);\n}\n\ntemplate <typename T> void wt_integer(T x) {\n if (por > SZ - 100)\n flush();\n if (x < 0) {\n obuf[por++] = '-', x = -x;\n }\n int outi;\n for (outi = 96; x >= 10000; outi -= 4) {\n memcpy(out + outi, pre.num[x % 10000], 4);\n x /= 10000;\n }\n if (x >= 1000) {\n memcpy(obuf + por, pre.num[x], 4);\n por += 4;\n } else if (x >= 100) {\n memcpy(obuf + por, pre.num[x] + 1, 3);\n por += 3;\n } else if (x >= 10) {\n int q = (x * 103) >> 10;\n obuf[por] = q | '0';\n obuf[por + 1] = (x - q * 10) | '0';\n por += 2;\n } else\n obuf[por++] = x | '0';\n memcpy(obuf + por, out + outi + 4, 96 - outi);\n por += 96 - outi;\n}\n\ntemplate <typename T> void wt_real(T x) {\n ostringstream oss;\n oss << fixed << setprecision(15) << double(x);\n string s = oss.str();\n wt(s);\n}\n\nvoid wt(int x) {\n wt_integer(x);\n}\nvoid wt(ll x) {\n wt_integer(x);\n}\nvoid wt(i128 x) {\n wt_integer(x);\n}\nvoid wt(uint x) {\n wt_integer(x);\n}\nvoid wt(ull x) {\n wt_integer(x);\n}\nvoid wt(u128 x) {\n wt_integer(x);\n}\nvoid wt(double x) {\n wt_real(x);\n}\nvoid wt(long double x) {\n wt_real(x);\n}\n\ntemplate <class T, class U> void wt(const pair<T, U> val) {\n wt(val.first);\n wt(' ');\n wt(val.second);\n}\ntemplate <size_t N = 0, typename T> void wt_tuple(const T t) {\n if constexpr (N < std::tuple_size<T>::value) {\n if constexpr (N > 0) {\n wt(' ');\n }\n const auto x = std::get<N>(t);\n wt(x);\n wt_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void wt(tuple<T...> tpl) {\n wt_tuple(tpl);\n}\ntemplate <class T, size_t S> void wt(const array<T, S> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\ntemplate <class T> void wt(const vector<T> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\n\nvoid print() {\n wt('\\n');\n}\ntemplate <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {\n wt(head);\n if (sizeof...(Tail))\n wt(' ');\n print(forward<Tail>(tail)...);\n}\nvoid __attribute__((destructor)) _d() {\n flush();\n}\n} // namespace fastio\n\n\nusing fastio::flush;\nusing fastio::print;\nusing fastio::read;\n\ninline void first(bool i = true) {\n print(i ? \"first\" : \"second\");\n}\ninline void Alice(bool i = true) {\n print(i ? \"Alice\" : \"Bob\");\n}\ninline void Takahashi(bool i = true) {\n print(i ? \"Takahashi\" : \"Aoki\");\n}\ninline void yes(bool i = true) {\n print(i ? \"yes\" : \"no\");\n}\ninline void Yes(bool i = true) {\n print(i ? \"Yes\" : \"No\");\n}\ninline void No() {\n print(\"No\");\n}\ninline void YES(bool i = true) {\n print(i ? \"YES\" : \"NO\");\n}\ninline void NO() {\n print(\"NO\");\n}\ninline void Yay(bool i = true) {\n print(i ? \"Yay!\" : \":(\");\n}\ninline void Possible(bool i = true) {\n print(i ? \"Possible\" : \"Impossible\");\n}\ninline void POSSIBLE(bool i = true) {\n print(i ? \"POSSIBLE\" : \"IMPOSSIBLE\");\n}\n\n/**\n * @brief Fast IO\n */\n\nstruct dsu {\n public:\n dsu() : _n(0) {}\n dsu(int n) : _n(n), parent_or_size(n, -1) {}\n\n int merge(int a, int b) {\n assert(0 <= a && a < _n);\n assert(0 <= b && b < _n);\n int x = leader(a), y = leader(b);\n if (x == y) return x;\n if (-parent_or_size[x] < -parent_or_size[y]) std::swap(x, y);\n parent_or_size[x] += parent_or_size[y];\n parent_or_size[y] = x;\n return x;\n }\n\n bool same(int a, int b) {\n assert(0 <= a && a < _n);\n assert(0 <= b && b < _n);\n return leader(a) == leader(b);\n }\n\n int leader(int a) {\n assert(0 <= a && a < _n);\n if (parent_or_size[a] < 0) return a;\n return parent_or_size[a] = leader(parent_or_size[a]);\n }\n\n int size(int a) {\n assert(0 <= a && a < _n);\n return -parent_or_size[leader(a)];\n }\n\n std::vector<std::vector<int>> groups() {\n std::vector<int> leader_buf(_n), group_size(_n);\n for (int i = 0; i < _n; i++) {\n leader_buf[i] = leader(i);\n group_size[leader_buf[i]]++;\n }\n std::vector<std::vector<int>> result(_n);\n for (int i = 0; i < _n; i++) {\n result[i].reserve(group_size[i]);\n }\n for (int i = 0; i < _n; i++) {\n result[leader_buf[i]].push_back(i);\n }\n result.erase(\n std::remove_if(result.begin(), result.end(),\n [&](const std::vector<int>& v) { return v.empty(); }),\n result.end());\n return result;\n }\n\n private:\n int _n;\n // root node: -1 * component size\n // otherwise: parent\n std::vector<int> parent_or_size;\n};\n\nint main() {\n int N, M;\n cin >> N >> M;\n vector<double> X(N), Y(N);\n vector<pair<double,pair<int,int>>> V(M);\n rep(i,0,N) cin >> X[i] >> Y[i];\n double ANS = 0.0;\n rep(i,0,M) {\n int A, B;\n cin >> A >> B;\n A--, B--;\n double D = sqrt((X[A]-X[B])*(X[A]-X[B])+(Y[A]-Y[B])*(Y[A]-Y[B]));\n ANS += D;\n V[i] = {D,(pair<int,int>){A,B}};\n }\n sort(ALL(V));\n reverse(ALL(V));\n dsu UF(N);\n rep(i,0,M) {\n if (!UF.same(V[i].second.first, V[i].second.second)) ANS -= V[i].first;\n UF.merge(V[i].second.first, V[i].second.second);\n }\n print(ANS);\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3892, "score_of_the_acc": -0.054, "final_rank": 3 }, { "submission_id": "aoj_2224_9323416", "code_snippet": "#include <iostream>\n#include <vector>\n#include <cmath>\n#include <algorithm>\nusing namespace std;\nconst int MAX_N = 10000;\n\nint par[MAX_N]; \nint rank_uf[MAX_N]; \n\nvoid init_union_find(int);\nint find(int);\nvoid unite(int, int);\nbool same(int, int);\n\nstruct edge {double u, v, cost ; } ;\n\nbool comp(const edge& e1, const edge &e2) {\n return e1.cost < e2.cost;\n}\n\nvector<edge> es;\nint V;\n\ndouble kruskal();\n\nint N, M, xi, yi, pi, qi;\ndouble x[MAX_N], y[MAX_N];\n\nint main() {\n cin >> N >> M;\n V = N;\n for (int i = 0; i < V; i++) {\n cin >> xi >> yi;\n x[i] = xi; y[i] = yi;\n }\n double ans = 0;\n for (int i = 0; i < M; i++) {\n cin >> pi >> qi;\n pi--; qi--;\n es.push_back((edge){(double)pi, (double)qi, -sqrt((x[pi] - x[qi]) * (x[pi] - x[qi]) + (y[pi] - y[qi]) * (y[pi] - y[qi]))});\n ans += sqrt((x[pi] - x[qi]) * (x[pi] - x[qi]) + (y[pi] - y[qi]) * (y[pi] - y[qi]));\n }\n printf(\"%.3f\\n\", ans + kruskal());\n return 0;\n}\n\nvoid init_union_find(int n) {\n for (int i = 0; i < n; i++) {\n par[i] = i;\n rank_uf[i] = 0;\n }\n}\n\nint find(int x) {\n if (par[x] == x) {\n return x;\n } else {\n return par[x] = find(par[x]);\n }\n}\n\nvoid unite(int x, int y) {\n x = find(x);\n y = find(y);\n if (x == y)\n return;\n if (rank_uf[x] < rank_uf[y]) {\n par[x] = y;\n } else {\n par[y] = x;\n if (rank_uf[x] == rank_uf[y])\n rank_uf[x]++;\n }\n}\n\nbool same(int x, int y) {\n return find(x) == find(y);\n}\n\ndouble kruskal() {\n sort(es.begin(), es.end(), comp);\n init_union_find(V);\n double res = 0;\n for (int i = 0; i < es.size(); i++) {\n edge e = es[i];\n if (!same(e.u, e.v)) {\n unite(e.u, e.v);\n res += e.cost;\n }\n }\n return res;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 4224, "score_of_the_acc": -0.1154, "final_rank": 12 }, { "submission_id": "aoj_2224_9210689", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n// 辺情報を表す構造体\nstruct edge {\n int from; // 辺の始点\n int to; // 辺の終点\n double leng; // 辺の重み\n};\n\n// 構造体 edge の比較関数\nbool comp(const edge &e, const edge &f) {\n return e.leng > f.leng;\n}\n\nstruct UnionFind{\n vector<int> par; //(例) par[3] = 2 : 3の親が2\n vector<int> rank; //根の深さ\n vector<int> siz; //根付き木に含まれる頂点数\n vector<int> min_node; //連結成分の最小値\n //n要素で最初は全てが根であるとして初期化\n //子:親のID、親:負のサイズ(-1)と仮定する\n UnionFind(int n) : par(n),rank(n),siz(n),min_node(n){\n for(int i=0; i<n; i++){\n par[i] = -1;\n rank[i] = 0;\n siz[i] = 1;\n min_node[i] = i;// 初期状態では min_node は自分自身\n } \n }\n //木の根を求める …データxが属する木の根を再帰で得る:root(x) = {xの木の根}\n int root(int x){\n if(par[x] < 0){ //根?\n return x;\n }else{\n return par[x] = root(par[x]); //経路圧縮\n }\n }\n //xとyが同じ集合に属するか否か\n bool same(int x,int y){\n return (root(x) == root(y));\n }\n bool unite(int x,int y){\n x = root(x);\n y = root(y);\n if(x == y ) return false;\n if(rank[x] < rank[y]){\n swap(x,y);\n }\n par[y] = x;\n if(rank[x] == rank[y]) rank[x]++;\n siz[x] += siz[y];\n // min_node の更新\n min_node[x] = min(min_node[x], min_node[y]);\n return true;\n }\n int size(int x){\n return siz[root(x)]; // x を含む根付き木のサイズ(要素 x の属する根付き木に含まれる頂点数)\n }\n \n //x を含む根付き木の中での頂点番号の最小値\n int get_min_node(int x) {\n return min_node[root(x)];\n }\n};\n\ndouble dis(int x1, int y1,int x2, int y2){\n double meter = (double)sqrt((double)(x1-x2)*(x1-x2)+(double)((y1-y2)*(y1-y2)));\n return meter;\n}\n\nint main() {\n\tint n,m;\n cin >> n >> m;\n vector<edge> graphedge(m);\n vector<int> x(n),y(n),p(m),q(m);\n for(int i=0;i<n;i++){\n cin >> x[i] >> y[i];\n }\n for(int i=0;i<m;i++){\n cin >> p[i] >> q[i];\n p[i]--;q[i]--;\n }\n\n double sum_weight=0;\n for(int i=0;i<m;i++){\n double w=dis(x[p[i]],y[p[i]],x[q[i]],y[q[i]]);\n graphedge[i]={p[i],q[i],w};\n sum_weight+=w;\n }\n sort(graphedge.begin(),graphedge.end(),comp);\n UnionFind uf(n);\n\n for(int i=0;i<m;i++){\n auto [u,v,w] = graphedge[i];\n if(uf.same(u,v)) continue;\n uf.unite(u,v);\n sum_weight-=w;\n }\n printf(\"%.10lf\\n\",sum_weight);\n return 0;\n\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 4168, "score_of_the_acc": -0.105, "final_rank": 9 }, { "submission_id": "aoj_2224_9140932", "code_snippet": "#include <algorithm>\n#include <cctype>\n#include <climits>\n#include <cstring>\n#include <iomanip>\n#include <iostream>\n#include <map>\n#include <math.h>\n#include <queue>\n#include <set>\n#include <sstream>\n#include <stdio.h>\n#include <string>\n#include <vector>\nusing namespace std;\ntypedef long double ll;\nconst ll INF = 10000000;\n\nclass UFT{\nprivate:\n int par[100000];\n int ranks[100000];\n int group;\n\n void init(int n){\n for(int i = 0; i < n; i++){\n par[i] = i;\n ranks[i] = 0;\n }\n group = n;\n }\npublic:\n UFT(int N){\n init(N);\n }\n\n int find(int x){\n if(par[x] == x) return x;\n else return find(par[x]);\n }\n\n void unite(int x, int y){\n x = find(x);\n y = find(y);\n if(x == y)return;\n\n if(ranks[x] < ranks[y]) par[x] = y;\n else if(ranks[x] == ranks[y]){\n par[y] = x;\n ranks[x]++;\n }\n else par[y] = x;\n group--;\n }\n\n bool same(int x, int y){\n return find(x) == find(y);\n }\n\n int count_group(){\n return group;\n }\n};\n//Graph\nstruct edge{\n int from, to; ll cost;\n\n edge(const int f, const int t, const ll c=INF){\n from = f; to = t; cost = c;\n }\n\n bool operator<(const edge& e2){\n return cost < e2.cost;\n }\n};\n\nclass Graph{\nprivate:\n vector<vector<edge>>neighbors;\n vector<ll>shortest;\n vector<int>prev;\n //this is for warshall froid.\n vector<vector<ll>>distances;\n int V;\n int E;\n\n // for priority_queue of edge\n struct comp{\n bool operator()(struct edge a, struct edge b){\n return (a.cost > b.cost);\n }\n};\n\npublic:\n Graph(){}\n Graph(const int n){\n V = n; E = 0;\n neighbors = vector<vector<edge>>(V);\n };\n\n void add_edge(const int f, const int t, const ll c=INT_MAX){\n edge e(f, t, c);\n neighbors[f].push_back(e);\n E++;\n }\n\n //Dijkstra\n void dijkstra(const int s){\n typedef pair<ll, int> P;\n shortest = vector<ll>(V, INF);\n prev = vector<int>(V, -1);\n priority_queue<P, vector<P>, greater<P>>pque;\n shortest[s] = 0;\n pque.push(P(0, s));\n\n while(!pque.empty()){\n P p = pque.top(); pque.pop();\n if(shortest[p.second] < p.first)continue;\n for(int i = 0; i < neighbors[p.second].size(); i++){\n edge e = neighbors[p.second][i];\n if(shortest[e.to] > shortest[p.second] + e.cost){\n shortest[e.to] = shortest[p.second] + e.cost;\n prev[e.to] = p.second;\n pque.push(P(shortest[e.to], e.to));\n }\n }\n } \n }\n\n //Bellman-Ford algorithm\n void Bellman_Ford(const int s){\n shortest = vector<ll>(V, INF);\n prev = vector<int>(V, -1);\n shortest[s] = 0;\n while(true){\n bool updated = false;\n for(int i = 0; i < V; i++){\n for(int j = 0; j < neighbors[i].size(); j++){\n edge e = neighbors[i][j];\n if(shortest[i] != INF && shortest[i] + e.cost < shortest[e.to]){\n shortest[e.to] = shortest[i] + e.cost;\n prev[e.to] = i;\n updated = true;\n }\n }\n }\n\n if(!updated)break;\n }\n }\n\n //Warshall_froid\n void warshall_froid(){\n distances = vector<vector<ll>>(V, vector<ll>(V, INF));\n for(int i = 0; i < V; i++){\n distances[i][i] = 0;\n for(int j = 0; j < neighbors[i].size(); j++){\n edge e = neighbors[i][j];\n distances[i][e.to] = e.cost;\n }\n }\n\n for(int k = 0; k < V; k++){\n for(int i = 0; i < V; i++){{\n for(int j = 0; j < V; j++){\n if(distances[i][k] != INF && distances[k][j] != INF) distances[i][j] = min(distances[i][j], distances[i][k] + distances[k][j]);\n }\n }\n }\n }\n }\n\n //prim. It works for connected graphs.\n ll prim(){\n ll minimum = 0;\n vector<bool>used(V, false);\n prev = vector<int>(V, -1);\n priority_queue<edge, vector<edge>, comp>pque;\n pque.push(edge(0, 0, 0));\n\n while(!pque.empty()){\n edge e = pque.top(); pque.pop();\n if(used[e.to]) continue;\n minimum += e.cost;\n used[e.to] = true;\n prev[e.to] = e.from;\n for(int i = 0; i < neighbors[e.to].size(); i++){\n edge next = neighbors[e.to][i];\n if(!used[next.to]){\n pque.push(next);\n }\n }\n }\n return minimum;\n }\n\n //kruskal It works for any type of graphs. However, it's difficult to restor paths. If you need the paths that makes forest, you should declare some vector to track them.\n //However, it messes the code up, so do declare flexibly if you want. \n ll kruskal(){\n vector<edge> edges;\n for(int i = 0; i < V; i++){\n for(int j = 0; j < neighbors[i].size(); j++){\n edges.push_back(neighbors[i][j]);\n }\n }\n sort(edges.rbegin(), edges.rend());\n\n ll minimum = 0;\n UFT tree(V);\n\n for(int i = 0; i < E; i++){\n edge e = edges[i];\n if(!tree.same(e.from, e.to)){\n tree.unite(e.from, e.to);\n minimum += e.cost;\n }\n }\n\n return minimum;\n }\n\n //find negative loop\n bool find_negative_loop(){\n shortest = vector<ll>(V, 0);\n for(int i = 0; i < V; i++){\n for(int j = 0; j < V; j++){\n for(int k = 0; k < neighbors[j].size(); k++){\n edge e = neighbors[j][k];\n if(shortest[j] + e.cost < shortest[e.to]){\n shortest[e.to] = shortest[j] + e.cost;\n if(i == V-1)return true;\n }\n }\n }\n }\n return false;\n }\n\n //get the shortest path from s to g\n //make sure it is done after algorithm\n ll get_value(const int g){\n return shortest[g];\n }\n ll get_value(const int s, const int g){\n return distances[s][g];\n }\n\n\n //get the shortest path route from s to g\n //make sure it is done after algorithm\n vector<int> get_route(const int s, const int g){\n vector<int>routes;\n if(get_value(g) == INF)return routes;\n\n for(int t = g; prev[t] != -1; t = prev[t])routes.push_back(t);\n routes.push_back(s);\n reverse(routes.begin(), routes.end());\n return routes;\n }\n};\n\nstruct Point{int x, y;};\nll caluculate(Point& p1, Point& p2){\n return sqrt(pow(p1.x-p2.x, 2)+pow(p1.y-p2.y, 2));\n}\nint main(){\n int N, M; scanf(\"%d%d\", &N, &M);\n Graph G(N);\n\n vector<Point>coordinates(N);\n for(int i = 0; i < N; i++){\n Point p; scanf(\"%d%d\", &p.x, &p.y);\n coordinates[i] = p;\n }\n\n ll total = 0;\n for(int i = 0; i < M; i++){\n int f, s; scanf(\"%d%d\", &f, &s);\n ll d = caluculate(coordinates[f-1], coordinates[s-1]);\n G.add_edge(f-1, s-1, d); \n G.add_edge(s-1, f-1, d); \n total += d;\n }\n\n cout << fixed << setprecision(4) << total - G.kruskal();\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 8256, "score_of_the_acc": -0.8609, "final_rank": 17 }, { "submission_id": "aoj_2224_8240165", "code_snippet": "#include<iostream>\n#include<vector>\n#include<stdio.h>\n#include<algorithm> \n#include<math.h>\nusing namespace std;\nint n,m,k;\nvector<pair<int,int> >point;\ndouble ans=0;\nstruct node{\n\tdouble dis;\n\tint u,v;\n\tbool operator < (const node p)const{\n\t\treturn dis>p.dis;\n\t} \n}; \nvector<node> vec;\nvector<int> par;\nint find(int x){\n\treturn x==par[x] ? x:par[x]=find(par[x]);\n}\nbool unite(int x,int y){\n\tx=find(x);y=find(y);\n\tif(x==y)return false;\n\tpar[x]=y;return true; \n}\nvoid func(){\n\tsort(vec.begin(),vec.end());\n\tfor(int i=0;i<m;i++){\n\t\tif(unite(vec[i].u,vec[i].v)){\n\t\t\tans+=vec[i].dis;\n\t\t}\n\t}\n}\nint main(){\n\tcin>>n>>m;\n\tpoint.resize(n+1);vec.resize(m);par.resize(n+1);\n\tfor(int i=1;i<=n;i++){\n\t\tpar[i]=i;\n\t\tcin>>point[i].first>>point[i].second;\n\t} \n\tint label=0;\n\tdouble sum=0;\n\tfor(int i=0;i<m;i++){\n\t\tint a,b;\n\t\tcin>>a>>b;\n\t\tdouble dis=sqrt((point[a].first-point[b].first)*(point[a].first-point[b].first)+(point[a].second-point[b].second)*(point[a].second-point[b].second));\n\t\tsum+=dis;\n\t\tvec[label++]={dis,a,b};\n\t}\n\tfunc();\n\tdouble final=sum-ans;\n\tprintf(\"%.3f\\n\",final);\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3600, "score_of_the_acc": 0, "final_rank": 1 }, { "submission_id": "aoj_2224_8183208", "code_snippet": "// #include<cstdio>\n#include<cmath>\n#include<limits>\n#include<iostream>\n#include<algorithm>\n#include<functional>\n#include<map>\n#include<set>\n#include<ctime>\n#include<queue>\n#include<vector>\n#include<iomanip>\nusing namespace std;\nconst double INF = numeric_limits<double>::max() /*INT_MAX*/ ;\n#define For(x, n) for(int x = 0, fend = n; x < fend; x++)\n\ntypedef pair<double, int> P;\nconst int MAX_N = 10000;\nconst int MAX_M = 30000;\nconst int MAX_XY = 10000;\n\nstruct edge { int to; double cost; };\nvector<edge> G[MAX_N];\n\nbool used[MAX_N];\n\nint N, M;\ndouble x[MAX_N],y[MAX_N];\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(0);\n\n double all = 0, subtracted = 0;\n cin >> N >> M;\n \n For(i, N) {\n cin >> x[i] >> y[i];\n }\n For(i, M) {\n int p, q;\n edge e1,e2;\n cin >> p >> q; p--; q--;\n e1.cost = e2.cost = pow((x[p]-x[q]) * (x[p] - x[q]) + (y[p] - y[q]) * (y[p] - y[q]), 0.5);\n all += e1.cost;\n e1.to = q; e2.to = p;\n G[p].push_back(e1); G[q].push_back(e2);\n }\n For(i, N) {\n used[i] = false;\n }\n priority_queue<P> que;\n int iv = 0;\n while (iv < N) {\n while (used[iv])iv++;\n que.push(P(0, iv));\n while (!que.empty()) {\n P p = que.top(); que.pop();\n int v = p.second;\n if (used[v])continue;\n used[v] = true;\n subtracted += p.first;\n\n For(i, G[v].size()) {\n edge e = G[v][i];\n if (!used[e.to]) {\n que.push(P(e.cost, e.to));\n }\n }\n }\n }\n cout << fixed << setprecision(3)<< all - subtracted << endl;\n\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 5356, "score_of_the_acc": -0.3247, "final_rank": 15 }, { "submission_id": "aoj_2224_7937478", "code_snippet": "#include <algorithm>\n#include <iostream>\n#include <math.h>\n#include <stdio.h>\n#include <string.h>\n#define maxn 11000\n#define oo 0xfffffff\nusing namespace std;\nint father[maxn], n, m;\nstruct node {\n int u, v;\n double w;\n} s[maxn * 100];\nstruct point {\n int x, y;\n} a[maxn];\ndouble Len(int p, int q) {\n double d = sqrt((a[p].x - a[q].x) * (a[p].x - a[q].x) +\n (a[p].y - a[q].y) * (a[p].y - a[q].y));\n return d;\n}\nint Find(int x) {\n while (x != father[x])\n x = father[x];\n return x;\n}\nint cmp(node p, node q) { return p.w > q.w; }\ndouble solve() {\n double ans = 0;\n for (int i = 0; i < m; i++) {\n int ru = Find(s[i].u);\n int rv = Find(s[i].v);\n if (ru != rv) {\n father[ru] = rv;\n ans += s[i].w;\n }\n }\n return ans;\n}\nint main() {\n int u, v;\n while (scanf(\"%d %d\", &n, &m) != EOF) {\n double sum = 0;\n for (int i = 1; i <= n; i++)\n scanf(\"%d %d\", &a[i].x, &a[i].y);\n for (int i = 1; i <= n; i++)\n father[i] = i;\n for (int i = 0; i < m; i++) {\n scanf(\"%d %d\", &u, &v);\n s[i].u = u;\n s[i].v = v;\n s[i].w = Len(u, v);\n sum += s[i].w;\n }\n sort(s, s + m, cmp);\n double l = solve();\n printf(\"%.3f\\n\", sum - l);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 3840, "score_of_the_acc": -0.3052, "final_rank": 13 }, { "submission_id": "aoj_2224_7937464", "code_snippet": "#include <algorithm>\n#include <cmath>\n#include <cstdio>\n#include <cstdlib>\n#include <iostream>\n#include <queue>\n#include <vector>\nusing namespace std;\nconst int INF = (1 << 25);\nconst int MAX = 10001;\nint N, M;\ndouble ans;\nstruct Pile {\n int x, y;\n Pile() {}\n Pile(int x, int y) : x(x), y(y) {}\n};\nstruct State {\n int pos;\n double t;\n State() {}\n State(int pos, double t) : pos(pos), t(t) {}\n bool operator<(const State &s) const { return t < s.t; }\n};\nvector<int> Edge[MAX];\nvector<Pile> P;\ndouble T[MAX];\nbool used[MAX];\nvoid init() {\n fill(T, T + MAX, -1);\n fill(used, used + MAX, false);\n for (int i = 0; i < MAX; i++)\n Edge[i].clear();\n P.clear();\n ans = 0;\n}\ndouble getDis(int p, int q) {\n return sqrt((P[p].x - P[q].x) * (P[p].x - P[q].x) +\n (P[p].y - P[q].y) * (P[p].y - P[q].y));\n}\nvoid input() {\n for (int i = 0; i < N; i++) {\n Pile in;\n cin >> in.x >> in.y;\n P.push_back(in);\n }\n for (int i = 0; i < M; i++) {\n int p, q;\n cin >> p >> q;\n p--;\n q--;\n Edge[p].push_back(q);\n Edge[q].push_back(p);\n ans += getDis(p, q);\n }\n}\nvoid prim(int start) {\n priority_queue<State> Q;\n Q.push(State(start, 0));\n T[start] = 0;\n while (!Q.empty()) {\n State now = Q.top();\n Q.pop();\n if (used[now.pos])\n continue;\n used[now.pos] = true;\n ans -= now.t;\n for (int i = 0; i < (int)Edge[now.pos].size(); i++) {\n int nex = Edge[now.pos][i];\n if (T[nex] < getDis(now.pos, nex)) {\n T[nex] = getDis(now.pos, nex);\n Q.push(State(nex, getDis(now.pos, nex)));\n }\n }\n }\n}\nvoid solve() {\n for (int i = 0; i < N; i++) {\n if (!used[i])\n prim(i);\n }\n printf(\"%.3f\\n\", ans);\n}\nint main() {\n while (cin >> N >> M && N + M) {\n init();\n input();\n solve();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 4148, "score_of_the_acc": -0.1013, "final_rank": 7 }, { "submission_id": "aoj_2224_7937463", "code_snippet": "#include <algorithm>\n#include <cmath>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <iostream>\n#include <queue>\n#include <vector>\nusing namespace std;\nconst int INF = (1 << 25);\nconst int MAX = 10001;\nint N, M;\ndouble ans;\nstruct Pile {\n int x, y;\n Pile() {}\n Pile(int x, int y) : x(x), y(y) {}\n};\nstruct State {\n int pos;\n double t;\n State() {}\n State(int pos, double t) : pos(pos), t(t) {}\n bool operator<(const State &s) const { return t < s.t; }\n};\nvector<int> Edge[MAX];\nvector<Pile> P;\ndouble T[MAX];\nbool used[MAX];\nvoid init() {\n fill(T, T + MAX, -1);\n memset(used, false, sizeof(used));\n for (int i = 0; i < MAX; i++)\n Edge[i].clear();\n P.clear();\n ans = 0;\n}\ndouble getDis(int p, int q) {\n return sqrt((P[p].x - P[q].x) * (P[p].x - P[q].x) +\n (P[p].y - P[q].y) * (P[p].y - P[q].y));\n}\nvoid input() {\n for (int i = 0; i < N; i++) {\n Pile in;\n scanf(\"%d %d\", &in.x, &in.y);\n P.push_back(in);\n }\n for (int i = 0; i < M; i++) {\n int p, q;\n scanf(\"%d %d\", &p, &q);\n p--;\n q--;\n Edge[p].push_back(q);\n Edge[q].push_back(p);\n ans += getDis(p, q);\n }\n}\nvoid prim(int start) {\n priority_queue<State> Q;\n Q.push(State(start, 0));\n T[start] = 0;\n while (!Q.empty()) {\n State now = Q.top();\n Q.pop();\n if (used[now.pos])\n continue;\n used[now.pos] = true;\n ans -= now.t;\n for (int i = 0; i < (int)Edge[now.pos].size(); i++) {\n int nex = Edge[now.pos][i];\n double cos = getDis(now.pos, nex);\n if (T[nex] < cos) {\n T[nex] = cos;\n Q.push(State(nex, cos));\n }\n }\n }\n}\nvoid solve() {\n for (int i = 0; i < N; i++) {\n if (!used[i])\n prim(i);\n }\n printf(\"%.3f\\n\", ans);\n}\nint main() {\n while (cin >> N >> M && N + M) {\n init();\n input();\n solve();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 4216, "score_of_the_acc": -0.1139, "final_rank": 11 }, { "submission_id": "aoj_2224_7937440", "code_snippet": "#include <algorithm>\n#include <bitset>\n#include <cmath>\n#include <iostream>\n#include <map>\n#include <queue>\n#include <stdio.h>\n#include <string.h>\n#include <vector>\n#define N 100005\n#define P pair<int, int>\n#define ll long long\n#define mk(a, b) make_pair(a, b)\n#define mem(a, b) memset(a, b, sizeof(a))\nusing namespace std;\nint inf = 0x3f3f3f3f;\nint n, m, pre[N];\ndouble sum;\nbool vis[N];\ndouble dis[N];\nstruct ac {\n int v;\n double c;\n};\nvector<P> a(N);\nvector<ac> g[N];\nvoid init() {\n for (int i = 1; i < N; ++i) {\n pre[i] = i;\n }\n}\nint find(int x) {\n if (x == pre[x])\n return x;\n else\n return pre[x] = find(pre[x]);\n}\nvoid join(int x, int y) {\n int fx = find(x);\n int fy = find(y);\n if (fx == fy)\n return;\n else if (fx < fy)\n pre[fy] = fx;\n else\n pre[fx] = fy;\n}\nvoid prim(int x) {\n mem(dis, 0);\n mem(vis, false);\n for (int i = 0; i < g[x].size(); ++i) {\n ac t = g[x][i];\n dis[t.v] = t.c;\n }\n vis[x] = true;\n for (int i = 1; i < n; ++i) {\n double MAX = 0;\n int u = -1;\n for (int j = 1; j <= n; ++j) {\n if (vis[j])\n continue;\n if (dis[j] > MAX) {\n MAX = dis[j];\n u = j;\n }\n }\n if (u == -1)\n return;\n vis[u] = true;\n sum += MAX;\n for (int j = 0; j < g[u].size(); ++j) {\n ac t = g[u][j];\n if (vis[t.v])\n continue;\n if (dis[t.v] < t.c) {\n dis[t.v] = t.c;\n }\n }\n }\n}\nint main() {\n ios::sync_with_stdio(false);\n while (cin >> n >> m) {\n init();\n for (int i = 1; i <= n; ++i) {\n cin >> a[i].first >> a[i].second;\n }\n double ans = 0;\n for (int i = 0; i < m; ++i) {\n int u, v;\n cin >> u >> v;\n int dx = a[u].first - a[v].first;\n int dy = a[u].second - a[v].second;\n double temp = sqrt(dx * dx + dy * dy);\n ans += temp;\n g[u].push_back((ac){v, temp});\n g[v].push_back((ac){u, temp});\n join(u, v);\n }\n for (int i = 1; i <= n; ++i) {\n if (pre[i] == i) {\n sum = 0;\n prim(i);\n ans -= sum;\n }\n }\n printf(\"%.3lf\\n\", ans);\n for (int i = 1; i <= n; ++i) {\n g[i].clear();\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 230, "memory_kb": 9008, "score_of_the_acc": -1.9565, "final_rank": 20 }, { "submission_id": "aoj_2224_7937408", "code_snippet": "#include <algorithm>\n#include <cmath>\n#include <cstdio>\n#include <cstdlib>\n#include <iostream>\nusing namespace std;\nstruct node {\n int x, y;\n} no[10005];\nstruct edge {\n int from, to;\n double dis;\n} ed[200000];\nbool cmp(const edge &a, const edge &b) { return a.dis > b.dis; }\nint father[10005];\nint find(int who) {\n if (who == father[who])\n return who;\n else\n return father[who] = find(father[who]);\n}\nint main(void) {\n int n, m;\n cin >> n >> m;\n for (int i = 1; i <= n; i++) {\n cin >> no[i].x >> no[i].y;\n father[i] = i;\n }\n double ans = 0;\n for (int i = 1; i <= m; i++) {\n int a, b;\n cin >> a >> b;\n ed[i].from = a;\n ed[i].to = b;\n ed[i].dis = pow(pow(no[a].x - no[b].x, 2) + pow(no[a].y - no[b].y, 2), 0.5);\n ans += ed[i].dis;\n }\n sort(ed + 1, ed + 1 + m, cmp);\n for (int i = 1; i <= m; i++) {\n int x = find(ed[i].from);\n int y = find(ed[i].to);\n if (x != y) {\n if (x < y)\n father[x] = father[y];\n else\n father[y] = father[x];\n ans -= ed[i].dis;\n }\n }\n printf(\"%.3lf\", ans);\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3956, "score_of_the_acc": -0.0658, "final_rank": 4 }, { "submission_id": "aoj_2224_7937399", "code_snippet": "#include <algorithm>\n#include <cmath>\n#include <cstdio>\n#include <cstring>\n#include <iostream>\n#include <queue>\nusing namespace std;\nconst int maxn = 1e4 + 10;\nconst int maxe = maxn * maxn / 2 + 10;\ntypedef pair<int, int> Point;\nstruct edge {\n int u, v;\n double dis;\n bool operator<(const edge &e) const { return dis > e.dis; }\n};\nPoint p[maxn];\nedge e[maxe];\nint s[maxn];\nint N, M;\ndouble getdis(Point a, Point b) {\n double x = a.first - b.first;\n double y = a.second - b.second;\n double dis = sqrt(x * x + y * y);\n return dis;\n}\nint find(int x) {\n if (s[x] < 0)\n return x;\n return s[x] = find(s[x]);\n}\nbool same(int x, int y) {\n int a = find(x);\n int b = find(y);\n return a == b;\n}\nvoid unite(int x, int y) {\n int a = find(x);\n int b = find(y);\n if (a == b)\n return;\n if (s[b] < s[a]) {\n s[a] = b;\n return;\n }\n if (s[b] == s[a])\n s[a]--;\n s[b] = a;\n}\ndouble kruskal(double tot) {\n memset(s, -1, sizeof(s));\n sort(e, e + M);\n double res = 0;\n for (int i = 0; i < M; i++) {\n if (!same(e[i].u, e[i].v)) {\n unite(e[i].u, e[i].v);\n res += e[i].dis;\n }\n }\n return tot - res;\n}\nint main() {\n cin >> N >> M;\n for (int i = 0; i < N; i++) {\n int x, y;\n cin >> x >> y;\n p[i].first = x;\n p[i].second = y;\n }\n double tot = 0;\n for (int i = 0; i < M; i++) {\n int u, v;\n cin >> u >> v;\n u--;\n v--;\n e[i].u = u;\n e[i].v = v;\n e[i].dis = getdis(p[u], p[v]);\n tot += e[i].dis;\n }\n double res = kruskal(tot);\n printf(\"%0.3f\\n\", res);\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3756, "score_of_the_acc": -0.0288, "final_rank": 2 }, { "submission_id": "aoj_2224_7937381", "code_snippet": "#include <algorithm>\n#include <cmath>\n#include <cstdio>\n#include <iostream>\n#include <queue>\n#include <string.h>\n#include <string>\n#include <vector>\nusing namespace std;\ntypedef pair<int, int> P;\ntypedef pair<double, int> Pdi;\n#define N 10000\nstruct fence {\n int to;\n double cost;\n};\ndouble distance(int x1, int y1, int x2, int y2) {\n return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));\n}\nint main() {\n int n, m;\n P piles[N];\n vector<fence> fences[N];\n double costsum = 0;\n scanf(\"%d%d\", &n, &m);\n for (int i = 0; i < n; i++) {\n int x, y;\n scanf(\"%d%d\", &x, &y);\n piles[i] = P(x, y);\n }\n for (int i = 0; i < m; i++) {\n int p, q;\n scanf(\"%d%d\", &p, &q);\n p--;\n q--;\n double c = distance(piles[p].first, piles[p].second, piles[q].first,\n piles[q].second);\n fences[p].push_back((fence){q, c});\n fences[q].push_back((fence){p, c});\n costsum += c;\n }\n double sum = 0;\n bool used[N];\n fill(used, used + n, false);\n priority_queue<Pdi> que;\n for (int i = 0; i < n; i++) {\n if (!used[i]) {\n que.push(Pdi(0, i));\n while (que.size()) {\n Pdi p = que.top();\n que.pop();\n int pos = p.second;\n if (used[pos])\n continue;\n used[pos] = true;\n sum += p.first;\n for (int j = 0; j < fences[pos].size(); j++) {\n que.push(Pdi(fences[pos][j].cost, fences[pos][j].to));\n }\n }\n }\n }\n printf(\"%.3f\\n\", costsum - sum);\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 5308, "score_of_the_acc": -0.3158, "final_rank": 14 }, { "submission_id": "aoj_2224_7937379", "code_snippet": "#include <algorithm>\n#include <cmath>\n#include <iomanip>\n#include <iostream>\n#include <utility>\n#include <vector>\nusing namespace std;\n#define MAX_N 10000 + 16\nint parent[MAX_N];\nint height[MAX_N];\nvoid init(const int &n) {\n for (int i = 0; i < n; ++i) {\n parent[i] = i;\n height[i] = 0;\n }\n}\nint find(const int &x) {\n if (parent[x] == x) {\n return x;\n } else {\n return parent[x] = find(parent[x]);\n }\n}\nvoid unite(int x, int y) {\n x = find(x);\n y = find(y);\n if (x == y) {\n return;\n }\n if (height[x] < height[y]) {\n parent[x] = y;\n } else {\n parent[y] = x;\n if (height[x] == height[y]) {\n ++height[x];\n }\n }\n}\nbool same(const int &x, const int &y) { return find(x) == find(y); }\n#define MAX_E MAX_N *MAX_N / 2 + 1\nstruct edge {\n int u, v;\n double cost;\n edge(int u = 0, int v = 0, double cost = 0) : u(u), v(v), cost(cost) {}\n bool operator<(const edge &e2) const { return cost > e2.cost; }\n};\nedge es[MAX_E];\nint V, E;\ndouble kruskal() {\n sort(es, es + E);\n init(V);\n double res = 0;\n for (int i = 0; i < E; ++i) {\n edge e = es[i];\n if (!same(e.u, e.v)) {\n unite(e.u, e.v);\n } else {\n res += e.cost;\n }\n }\n return res;\n}\npair<int, int> pile[MAX_N];\nint main(int argc, char *argv[]) {\n cin >> V >> E;\n for (int i = 0; i < V; ++i) {\n cin >> pile[i].first >> pile[i].second;\n }\n for (int i = 0; i < E; ++i) {\n cin >> es[i].u >> es[i].v;\n --es[i].u;\n --es[i].v;\n int dx = pile[es[i].u].first - pile[es[i].v].first;\n int dy = pile[es[i].u].second - pile[es[i].v].second;\n es[i].cost = sqrt(dx * dx + dy * dy);\n }\n cout.setf(ios::showpoint);\n cout.precision(3);\n cout.setf(ios::fixed);\n cout << kruskal() << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 6032, "score_of_the_acc": -0.4497, "final_rank": 16 }, { "submission_id": "aoj_2224_7937359", "code_snippet": "#include <algorithm>\n#include <complex>\n#include <cstdio>\n#include <cstring>\n#include <iostream>\n#include <set>\n#include <vector>\nusing namespace std;\n#define EPS (1e-10)\n#define EQ(a, b) ((abs((a) - (b))) < EPS)\ntypedef complex<double> P;\nstruct edge {\n int to;\n double cost;\n};\nint n, m;\nvector<edge> G[10001];\nvector<edge> G2[10001];\nP points[10001];\nconst double INF = 1000000000;\ndouble mincost[10001];\nbool used[10001];\ndouble prim(int s, int V) {\n for (int i = 0; i < V; i++)\n mincost[i] = INF;\n mincost[s] = 0;\n double res = 0;\n while (1) {\n int v = -1;\n for (int u = 0; u < V; u++)\n if (!used[u] && (v == -1 || mincost[u] < mincost[v]))\n v = u;\n if (v == -1 || EQ(mincost[v], INF))\n break;\n used[v] = true;\n res += mincost[v];\n for (int i = 0; i < (int)G[v].size(); i++) {\n int u = G[v][i].to;\n mincost[u] = min(mincost[u], G[v][i].cost);\n }\n }\n return res;\n}\nint main() {\n cin >> n >> m;\n for (int i = 0; i < n; i++) {\n int x, y;\n cin >> x >> y;\n points[i] = P(x, y);\n }\n double sum = 0;\n for (int i = 0; i < m; i++) {\n int a, b;\n cin >> a >> b;\n b--;\n a--;\n edge e;\n e.to = b;\n e.cost = -abs(points[a] - points[b]);\n sum += -e.cost;\n G[a].push_back(e);\n e.to = a;\n G[b].push_back(e);\n }\n double res = 0;\n for (int i = 0; i < n; i++) {\n if (used[i])\n continue;\n res += prim(i, n);\n }\n printf(\"%.10f\\n\", sum + res);\n return 0;\n}", "accuracy": 1, "time_ms": 240, "memory_kb": 5356, "score_of_the_acc": -1.3247, "final_rank": 19 } ]
aoj_2231_cpp
Problem J: Cruel Bingo Bingo is a party game played by many players and one game master. Each player is given a bingo card containing N 2 different numbers in a N × N grid (typically N = 5). The master draws numbers from a lottery one by one during the game. Each time a number is drawn, a player marks a square with that number if it exists. The player’s goal is to have N marked squares in a single vertical, horizontal, or diagonal line and then call “Bingo!” The first player calling “Bingo!” wins the game. In ultimately unfortunate cases, a card can have exactly N unmarked squares, or N ( N -1) marked squares, but not a bingo pattern. Your task in this problem is to write a program counting how many such patterns are possible from a given initial pattern, which contains zero or more marked squares. Input The input is given in the following format: N K x 1 y 1 . . . x K y K The input begins with a line containing two numbers N (1 ≤ N ≤ 32) and K (0 ≤ K ≤ 8), which represent the size of the bingo card and the number of marked squares in the initial pattern respectively. Then K lines follow, each containing two numbers x i and y i to indicate the square at ( x i , y i ) is marked. The coordinate values are zero-based (i.e. 0 ≤ x i , y i ≤ N - 1). No pair of marked squares coincides. Output Count the number of possible non-bingo patterns with exactly N unmarked squares that can be made from the given initial pattern, and print the number in modulo 10007 in a line (since it is supposed to be huge). Rotated and mirrored patterns should be regarded as different and counted each. Sample Input 1 4 2 0 2 3 1 Output for the Sample Input 1 6 Sample Input 2 4 2 2 3 3 2 Output for the Sample Input 2 6 Sample Input 3 10 3 0 0 4 4 1 4 Output for the Sample Input 3 1127
[ { "submission_id": "aoj_2231_4303458", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 99999999999999999\n//#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n\n#define MOD 10007\n\n\nenum Type{\n\tN,\n\tE,\n\tS,\n\tW,\n\tNE,\n\tSE,\n\tNW,\n\tSW,\n};\n\nstruct Info{\n\n\tint x,y;\n};\n\n\nint SIZE,K;\nint POW[9],dp[18][33][4];\nbool check_X[32],check_Y[32];\nvector<int> STATE;\nvector<Info> V;\nInfo info[8];\n\nvoid mod_add(int &A,int B){\n\n\tA += B;\n\tA %= MOD;\n}\n\n\nint func(){\n\n\tfor(int i = 0; i <= SIZE/2+1; i++){\n\t\tfor(int k = 0; k <= SIZE; k++){\n\t\t\tfor(int a = 0; a < POW[2]; a++){\n\n\t\t\t\tdp[i][k][a] = 0;\n\t\t\t}\n\t\t}\n\t}\n\n\tfor(int i = 0; i < SIZE; i++){\n\t\tcheck_X[i] = false;\n\t\tcheck_Y[i] = false;\n\t}\n\n\tint first_cross = 0;\n\n\tfor(int i = 0; i < V.size(); i++){\n\n\t\tif(check_X[V[i].x] || check_Y[V[i].y]){\n\n\t\t\treturn 0;\n\t\t}\n\t\tcheck_X[V[i].x] = true;\n\t\tcheck_Y[V[i].y] = true;\n\n\t\tif(V[i].x == V[i].y){ //右上がり\n\n\t\t\tfirst_cross |= POW[0];\n\t\t}\n\t\tif(V[i].x+V[i].y == SIZE-1){ //右下がり\n\n\t\t\tfirst_cross |= POW[1];\n\t\t}\n\t}\n\n\tint first = 0;\n\tdp[first][0][first_cross] = 1;\n\tint last;\n\tif(SIZE%2 == 0){\n\n\t\tlast = SIZE/2-1;\n\t}else{\n\n\t\tlast = SIZE/2;\n\t}\n\n\tint LU = (SIZE-1)/2;\n\tint RD = SIZE/2;\n\n\n\tint num_X,num_Y;\n\tint work[8];\n\tint next_state,mult_X,mult_Y,add,tmp;\n\n\tint num_fix = V.size();\n\n\tfor(int range = first; range <= last; range++){\n\n\t\tnum_X = 0;\n\t\tnum_Y = 0;\n\n\t\tfor(int k = LU+1; k <= RD-1; k++){\n\t\t\tif(!check_X[k]){\n\t\t\t\tnum_X++;\n\t\t\t}\n\t\t\tif(!check_Y[k]){\n\n\t\t\t\tnum_Y++;\n\t\t\t}\n\t\t}\n\n\t\tfor(int i = 0; i <= SIZE-num_fix; i++){\n\t\t\tfor(int state = 0; state < POW[2]; state++){\n\t\t\t\tif(dp[range][i][state] == 0)continue;\n\n\t\t\t\tif(range == first && SIZE%2 == 1){\n\n\t\t\t\t\tmod_add(dp[range+1][i][state],dp[range][i][state]);\n\t\t\t\t\tif(!check_X[LU] && !check_Y[LU] && i+1 <= SIZE-num_fix){\n\n\t\t\t\t\t\tmod_add(dp[range+1][i+1][POW[2]-1],dp[range][i][state]);\n\t\t\t\t\t}\n\n\t\t\t\t}else{\n\n\t\t\t\t\tfor(int k = 0; k < STATE.size(); k++){\n\n\t\t\t\t\t\tadd = 0;\n\n\t\t\t\t\t\tint ST = STATE[k];\n\t\t\t\t\t\tfor(int loop = 0; loop < 8; loop++){\n\t\t\t\t\t\t\tif(ST & (POW[loop])){\n\n\t\t\t\t\t\t\t\twork[loop] = 1;\n\t\t\t\t\t\t\t\tadd++;\n\n\t\t\t\t\t\t\t}else{\n\n\t\t\t\t\t\t\t\twork[loop] = 0;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tif(i+add > SIZE-num_fix)continue;\n\n\t\t\t\t\t\tif(range == first && SIZE%2 == 0){\n\t\t\t\t\t\t\tif(work[N]+work[E]+work[S]+work[W] != 0){\n\n\t\t\t\t\t\t\t\tcontinue;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tif((check_Y[LU] && (work[SW]+work[S]+work[SE] != 0)) ||\n\t\t\t\t\t\t\t\t(check_Y[RD] && (work[NW]+work[N]+work[NE] != 0))){\n\n\t\t\t\t\t\t\tcontinue;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif((check_X[LU] && (work[NW]+work[W]+work[SW] != 0)) ||\n\t\t\t\t\t\t\t\t(check_X[RD] && (work[NE]+work[E]+work[SE] != 0))){\n\n\t\t\t\t\t\t\tcontinue;\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tnext_state = state;\n\t\t\t\t\t\tif(work[SW] == 1 || work[NE] == 1){\n\n\t\t\t\t\t\t\tnext_state |= POW[0];\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif(work[NW] == 1 || work[SE] == 1){\n\n\t\t\t\t\t\t\tnext_state |= POW[1];\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tswitch(work[N]+work[S]){\n\t\t\t\t\t\tcase 2:\n\t\t\t\t\t\t\tmult_X = (num_X-i)*(num_X-i-1);\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase 1:\n\t\t\t\t\t\t\tmult_X = (num_X-i);\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase 0:\n\t\t\t\t\t\t\tmult_X = 1;\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tswitch(work[E]+work[W]){\n\t\t\t\t\t\tcase 2:\n\t\t\t\t\t\t\tmult_Y = (num_Y-i)*(num_Y-i-1);\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase 1:\n\t\t\t\t\t\t\tmult_Y = (num_Y-i);\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase 0:\n\t\t\t\t\t\t\tmult_Y = 1;\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\ttmp = mult_X*mult_Y;\n\t\t\t\t\t\ttmp *= dp[range][i][state];\n\t\t\t\t\t\ttmp %= MOD;\n\n\t\t\t\t\t\tmod_add(dp[range+1][i+add][next_state],tmp);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tLU--;\n\t\tRD++;\n\t}\n\n\treturn dp[last+1][SIZE-num_fix][POW[2]-1];\n}\n\nvoid calc_state(){\n\n\tint check[8];\n\n\tfor(int state = 0; state < POW[8]; state++){\n\t\tfor(int loop = 0; loop < 8; loop++){\n\t\t\tif(state & (POW[loop])){\n\n\t\t\t\tcheck[loop] = 1;\n\n\t\t\t}else{\n\n\t\t\t\tcheck[loop] = 0;\n\t\t\t}\n\t\t}\n\n\t\tif((check[NW]+check[N]+check[NE] > 1)||(check[NE]+check[E]+check[SE] > 1)||\n\t\t\t\t(check[SE]+check[S]+check[SW] > 1)||(check[SW]+check[W]+check[NW] > 1)){\n\t\t\tcontinue;\n\t\t}\n\t\tSTATE.push_back(state);\n\t}\n}\n\n\nint main(){\n\n\tPOW[0] = 1;\n\tfor(int i = 1; i <= 8; i++){\n\n\t\tPOW[i] = POW[i-1]*2;\n\t}\n\n\tcalc_state();\n\n\tscanf(\"%d %d\",&SIZE,&K);\n\n\tfor(int i = 0; i < K; i++){\n\n\t\tscanf(\"%d %d\",&info[i].x,&info[i].y);\n\t}\n\n\tint ans = 0;\n\tint count,tmp;\n\n\tfor(int state = 0; state < POW[K]; state++){\n\n\t\tV.clear();\n\t\tcount = 0;\n\n\t\tfor(int loop = 0; loop < K; loop++){\n\t\t\tif(state & POW[loop]){\n\t\t\t\tV.push_back(info[loop]);\n\t\t\t\tcount++;\n\t\t\t}\n\t\t}\n\n\t\ttmp = func();\n\n\t\tif(count%2 == 0){\n\n\t\t\tans += tmp;\n\t\t\tans %= MOD;\n\n\t\t}else{\n\n\t\t\tans = (ans-tmp+MOD)%MOD;\n\t\t}\n\t}\n\n\tprintf(\"%d\\n\",ans);\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 3268, "score_of_the_acc": -0.2919, "final_rank": 2 }, { "submission_id": "aoj_2231_4303443", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 99999999999999999\n//#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n\n#define MOD 10007\n\n\nenum Type{\n\tN,\n\tE,\n\tS,\n\tW,\n\tNE,\n\tSE,\n\tNW,\n\tSW,\n};\n\nstruct Info{\n\n\tint x,y;\n};\n\n\nint SIZE,K;\nint POW[9],dp[18][33][4];\nbool check_X[32],check_Y[32];\nvector<int> STATE;\nvector<Info> V;\nInfo info[8];\n\nvoid mod_add(int &A,int B){\n\n\tA += B;\n\tA %= MOD;\n}\n\n\nint func(){\n\n\tfor(int i = 0; i <= SIZE/2+1; i++){\n\t\tfor(int k = 0; k <= SIZE; k++){\n\t\t\tfor(int a = 0; a < POW[2]; a++){\n\n\t\t\t\tdp[i][k][a] = 0;\n\t\t\t}\n\t\t}\n\t}\n\n\tfor(int i = 0; i < SIZE; i++){\n\t\tcheck_X[i] = false;\n\t\tcheck_Y[i] = false;\n\t}\n\n\tint first_cross = 0;\n\n\tfor(int i = 0; i < V.size(); i++){\n\n\t\tif(check_X[V[i].x] || check_Y[V[i].y]){\n\n\t\t\treturn 0;\n\t\t}\n\t\tcheck_X[V[i].x] = true;\n\t\tcheck_Y[V[i].y] = true;\n\n\t\tif(V[i].x == V[i].y){\n\n\t\t\tfirst_cross |= POW[0];\n\t\t}else if(V[i].x+V[i].y == SIZE-1){\n\n\t\t\tfirst_cross |= POW[1];\n\t\t}\n\t}\n\n\tint first = 0;\n\tdp[first][0][first_cross] = 1;\n\tint last;\n\tif(SIZE%2 == 0){\n\n\t\tlast = SIZE/2-1;\n\t}else{\n\n\t\tlast = SIZE/2;\n\t}\n\n\tint LU = (SIZE-1)/2;\n\tint RD = SIZE/2;\n\n\tint num_X,num_Y;\n\tint work[8];\n\tint next_state,mult_X,mult_Y,add,tmp;\n\n\tint num_fix = V.size();\n\n\tfor(int range = first; range <= last; range++){\n\n\t\tnum_X = 0;\n\t\tnum_Y = 0;\n\n\t\tfor(int k = LU+1; k <= RD-1; k++){\n\t\t\tif(!check_X[k]){\n\t\t\t\tnum_X++;\n\t\t\t}\n\t\t\tif(!check_Y[k]){\n\n\t\t\t\tnum_Y++;\n\t\t\t}\n\t\t}\n\n\t\tfor(int i = 0; i <= SIZE-num_fix; i++){ //残すセル数のループ\n\t\t\tfor(int state = 0; state < POW[2]; state++){\n\t\t\t\tif(dp[range][i][state] == 0)continue;\n\n\t\t\t\tif(range == first && SIZE%2 == 1){\n\n\t\t\t\t\tmod_add(dp[range+1][i][state],dp[range][i][state]);\n\t\t\t\t\tif(!check_X[LU] && !check_Y[LU] && i+1 <= SIZE-num_fix){\n\n\t\t\t\t\t\tmod_add(dp[range+1][i+1][POW[2]-1],dp[range][i][state]);\n\t\t\t\t\t}\n\n\t\t\t\t}else{\n\n\t\t\t\t\tfor(int k = 0; k < STATE.size(); k++){\n\n\t\t\t\t\t\tadd = 0;\n\n\t\t\t\t\t\tint ST = STATE[k];\n\t\t\t\t\t\tfor(int loop = 0; loop < 8; loop++){\n\t\t\t\t\t\t\tif(ST & (POW[loop])){\n\n\t\t\t\t\t\t\t\twork[loop] = 1;\n\t\t\t\t\t\t\t\tadd++;\n\n\t\t\t\t\t\t\t}else{\n\n\t\t\t\t\t\t\t\twork[loop] = 0;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tif(i+add > SIZE-num_fix)continue;\n\n\t\t\t\t\t\tif(range == first && SIZE%2 == 0){\n\t\t\t\t\t\t\tif(work[N]+work[E]+work[S]+work[W] != 0){\n\n\t\t\t\t\t\t\t\tcontinue;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tif((check_Y[LU] && (work[SW]+work[S]+work[SE] != 0)) ||\n\t\t\t\t\t\t\t\t(check_Y[RD] && (work[NW]+work[N]+work[NE] != 0))){\n\n\t\t\t\t\t\t\tcontinue;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif((check_X[LU] && (work[NW]+work[W]+work[SW] != 0)) ||\n\t\t\t\t\t\t\t\t(check_X[RD] && (work[NE]+work[E]+work[SE] != 0))){\n\n\t\t\t\t\t\t\tcontinue;\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tnext_state = state;\n\t\t\t\t\t\tif(work[SW] == 1 || work[NE] == 1){\n\n\t\t\t\t\t\t\tnext_state |= POW[0];\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif(work[NW] == 1 || work[SE] == 1){\n\n\t\t\t\t\t\t\tnext_state |= POW[1];\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tswitch(work[N]+work[S]){\n\t\t\t\t\t\tcase 2:\n\t\t\t\t\t\t\tmult_X = (num_X-i)*(num_X-i-1);\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase 1:\n\t\t\t\t\t\t\tmult_X = (num_X-i);\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase 0:\n\t\t\t\t\t\t\tmult_X = 1;\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tswitch(work[E]+work[W]){\n\t\t\t\t\t\tcase 2:\n\t\t\t\t\t\t\tmult_Y = (num_Y-i)*(num_Y-i-1);\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase 1:\n\t\t\t\t\t\t\tmult_Y = (num_Y-i);\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase 0:\n\t\t\t\t\t\t\tmult_Y = 1;\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\ttmp = mult_X*mult_Y;\n\t\t\t\t\t\ttmp *= dp[range][i][state];\n\t\t\t\t\t\ttmp %= MOD;\n\n\t\t\t\t\t\tmod_add(dp[range+1][i+add][next_state],tmp);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tLU--;\n\t\tRD++;\n\t}\n\n\treturn dp[last+1][SIZE-num_fix][POW[2]-1];\n}\n\nvoid calc_state(){\n\n\tint check[8];\n\n\tfor(int state = 0; state < POW[8]; state++){\n\t\tfor(int loop = 0; loop < 8; loop++){\n\t\t\tif(state & (POW[loop])){\n\n\t\t\t\tcheck[loop] = 1;\n\n\t\t\t}else{\n\n\t\t\t\tcheck[loop] = 0;\n\t\t\t}\n\t\t}\n\n\t\tif((check[NW]+check[N]+check[NE] > 1)||(check[NE]+check[E]+check[SE] > 1)||\n\t\t\t\t(check[SE]+check[S]+check[SW] > 1)||(check[SW]+check[W]+check[NW] > 1)){\n\t\t\tcontinue;\n\t\t}\n\t\tSTATE.push_back(state);\n\t}\n}\n\n\nint main(){\n\n\tPOW[0] = 1;\n\tfor(int i = 1; i <= 8; i++){\n\n\t\tPOW[i] = POW[i-1]*2;\n\t}\n\n\tcalc_state();\n\n\tscanf(\"%d %d\",&SIZE,&K);\n\n\tfor(int i = 0; i < K; i++){\n\n\t\tscanf(\"%d %d\",&info[i].x,&info[i].y);\n\t}\n\n\tint ans = 0;\n\tint count,tmp;\n\n\tfor(int state = 0; state < POW[K]; state++){\n\n\t\tV.clear();\n\t\tcount = 0;\n\n\t\tfor(int loop = 0; loop < K; loop++){\n\t\t\tif(state & POW[loop]){\n\t\t\t\tV.push_back(info[loop]);\n\t\t\t\tcount++;\n\t\t\t}\n\t\t}\n\n\t\ttmp = func();\n\n\t\tif(count%2 == 0){\n\n\t\t\tans += tmp;\n\t\t\tans %= MOD;\n\n\t\t}else{\n\n\t\t\tans = (ans-tmp+MOD)%MOD;\n\t\t}\n\t}\n\n\tprintf(\"%d\\n\",ans);\n\n\treturn 0;\n}", "accuracy": 0.17142857142857143, "time_ms": 60, "memory_kb": 3272, "score_of_the_acc": -0.2587, "final_rank": 5 }, { "submission_id": "aoj_2231_2094106", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define FOR(i,a,b) for(int i=int(a);i<int(b);i++)\n#define REP(i,b) FOR(i,0,b)\n#define MP make_pair\n#define PB push_back\n#define ALL(x) x.begin(),x.end()\n#define REACH cerr<<\"reached line \"<<__LINE__<<endl\n#define DBG(x) cerr<<\"line \"<<__LINE__<<\" \"<<#x<<\":\"<<x<<endl\n\nusing uint=unsigned int;\nusing ll=long long;\nusing pii=pair<int,int>;\nusing vi=vector<int>;\nusing ld=long double;\n\ntemplate<class T,class U>\nostream& operator<<(ostream& os,const pair<T,U>& p){\n\tos<<\"(\"<<p.first<<\",\"<<p.second<<\")\";\n\treturn os;\n}\n\ntemplate<class T>\nostream& operator <<(ostream& os,const vector<T>& v){\n\tos<<\"[\";\n\tREP(i,(int)v.size()){\n\t\tif(i)os<<\",\";\n\t\tos<<v[i];\n\t}\n\tos<<\"]\";\n\treturn os;\n}\n\nint read(){\n\tint i;\n\tscanf(\"%d\",&i);\n\treturn i;\n}\n\nll readLL(){\n\tll i;\n\tscanf(\"%lld\",&i);\n\treturn i;\n}\n\nstring readString(){\n\tstatic char buf[3341919];\n\tscanf(\"%s\",buf);\n\treturn string(buf);\n}\n\nchar* readCharArray(){\n\tstatic char buf[3341919];\n\tstatic int bufUsed=0;\n\tchar* ret=buf+bufUsed;\n\tscanf(\"%s\",ret);\n\tbufUsed+=strlen(ret)+1;\n\treturn ret;\n}\n\ntemplate<class T,class U>\nvoid chmax(T& a,U b){\n\tif(a<b)\n\t\ta=b;\n}\n\ntemplate<class T,class U>\nvoid chmin(T& a,U b){\n\tif(a>b)\n\t\ta=b;\n}\n\ntemplate<class T>\nT Sq(const T& t){\n\treturn t*t;\n}\n\nconst int mod=10007;\ntemplate<class T,class U>\nvoid add(T& a,U b){\n\ta=((ll)a+b)%mod;\n}\n\ntemplate<class T,class U>\nvoid sub(T& a,U b){\n\ta=((ll)a-b%mod+mod)%mod;\n}\n\ntemplate<class T,class U>\nvoid mult(T& a,U b){\n\ta=((ll)a*b)%mod;\n}\n\nll modPow(ll a,ll p){\n\tll s=1;\n\tREP(i,30){\n\t\tif((p>>i)&1)\n\t\t\tmult(s,a);\n\t\tmult(a,a);\n\t}\n\treturn s;\n}\n\nll modInv(ll a){\n\treturn modPow(a,mod-2);\n}\n\nstruct Wafrelka{\n\tint n,m;\n\tvector<vi> graph;\n\tWafrelka(int _n,int _m):n(_n),m(_m){\n\t\tgraph.assign(n,vi());\n\t}\n\tvoid AddEdge(int a,int b){\n\t\tgraph[a].PB(b);\n\t}\n\tvi CountMatching(){\n\t\tint maxMatch=min(n,m);\n\t\tvector<vi> dp(maxMatch+1,vi(1<<m,0));\n\t\tdp[0][0]=1;\n\t\tREP(i,n)\n\t\t\tfor(int j=maxMatch-1;j>=0;j--)\n\t\t\t\tREP(vis,1<<m)\n\t\t\t\t\tfor(auto to:graph[i])if(!((vis>>to)&1))\n\t\t\t\t\t\tadd(dp[j+1][vis|(1<<to)],dp[j][vis]);\n\t\tvi ret(maxMatch+1,0);\n\t\tREP(i,maxMatch+1)\n\t\t\tREP(j,1<<m)\n\t\t\t\tadd(ret[i],dp[i][j]);\n\t\treturn ret;\n\t}\n};\n\nstruct Solver{\n\tint n;\n\tvector<vi> graph;\n\tSolver(int _n):n(_n){\n\t\tgraph.assign(n*2,vi());\n\t}\n\tvoid AddEdge(int a,int b){\n\t\tgraph[a].PB(b+n);\n\t\tgraph[b+n].PB(a);\n\t}\n\tvoid dfs(int v,vector<bool>& vis,vi& left,vi& right){\n\t\tif(vis[v])\n\t\t\treturn;\n\t\tif(v<n)\n\t\t\tleft.PB(v);\n\t\telse\n\t\t\tright.PB(v);\n\t\tvis[v]=true;\n\t\tfor(auto to:graph[v])\n\t\t\tdfs(to,vis,left,right);\n\t}\n\tint CountPerfectMatching(){\n\t\tvector<bool> vis(n*2,false);\n\t\tvi cur(n+1,0),nx(n+1,0);\n\t\tcur[0]=1;\n\t\tREP(i,n)if(!vis[i]){\n\t\t\tvi left,right;\n\t\t\tdfs(i,vis,left,right);\n\t\t\tWafrelka w(left.size(),right.size());\n\t\t\tREP(j,left.size())\n\t\t\t\tfor(auto to:graph[left[j]])\n\t\t\t\t\tREP(k,right.size())if(to==right[k])\n\t\t\t\t\t\tw.AddEdge(j,k);\n\t\t\tvi t=w.CountMatching();\n\t\t\tfill(ALL(nx),0);\n\t\t\tREP(j,n+1)REP(k,t.size())if(j+k<n+1)\n\t\t\t\tadd(nx[j+k],cur[j]*t[k]);\n\t\t\tswap(cur,nx);\n\t\t}\n\t\tint c=1,ret=0;\n\t\tfor(int i=n;i>=0;i--){\n\t\t\tif(i&1)\n\t\t\t\tsub(ret,cur[i]*c);\n\t\t\telse\n\t\t\t\tadd(ret,cur[i]*c);\n\t\t\tc=c*(n-i+1)%mod;\n\t\t}\n\t\treturn ret;\n\t}\n};\n\nint main(){\n\tint n=read(),k=read();\n\tvector<pii> es;\n\tREP(i,k){\n\t\tint x=read(),y=read();\n\t\tes.PB(MP(x,y));\n\t}\n\tint ans=0;\n\t{\n\t\tvector<vector<bool>> c(n,vector<bool>(n,false));\n\t\tfor(auto e:es)\n\t\t\tc[e.first][e.second]=true;\n\t\tSolver sv(n);\n\t\tREP(i,n)REP(j,n)if(c[i][j])\n\t\t\tsv.AddEdge(i,j);\n\t\tadd(ans,sv.CountPerfectMatching());\n\t}\n\t{\n\t\tvector<vector<bool>> c(n,vector<bool>(n,false));\n\t\tfor(auto e:es)\n\t\t\tc[e.first][e.second]=true;\n\t\tREP(i,n)\n\t\t\tc[i][i]=true;\n\t\tSolver sv(n);\n\t\tREP(i,n)REP(j,n)if(c[i][j])\n\t\t\tsv.AddEdge(i,j);\n\t\tsub(ans,sv.CountPerfectMatching());\n\t}\n\t{\n\t\tvector<vector<bool>> c(n,vector<bool>(n,false));\n\t\tfor(auto e:es)\n\t\t\tc[e.first][e.second]=true;\n\t\tREP(i,n)\n\t\t\tc[i][n-1-i]=true;\n\t\tSolver sv(n);\n\t\tREP(i,n)REP(j,n)if(c[i][j])\n\t\t\tsv.AddEdge(i,j);\n\t\tsub(ans,sv.CountPerfectMatching());\n\t}\n\t{\n\t\tvector<vector<bool>> c(n,vector<bool>(n,false));\n\t\tfor(auto e:es)\n\t\t\tc[e.first][e.second]=true;\n\t\tREP(i,n)\n\t\t\tc[i][i]=true;\n\t\tREP(i,n)\n\t\t\tc[i][n-1-i]=true;\n\t\tSolver sv(n);\n\t\tREP(i,n)REP(j,n)if(c[i][j])\n\t\t\tsv.AddEdge(i,j);\n\t\tadd(ans,sv.CountPerfectMatching());\n\t}\n\tcout<<ans<<endl;\n}", "accuracy": 1, "time_ms": 310, "memory_kb": 23432, "score_of_the_acc": -2, "final_rank": 4 }, { "submission_id": "aoj_2231_1562412", "code_snippet": "#include <bits/stdc++.h>\n#define rep(i,n) for(int i=0;i<(int)(n);i++)\n#define rep1(i,n) for(int i=1;i<=(int)(n);i++)\n#define all(c) c.begin(),c.end()\n#define pb push_back\n#define fs first\n#define sc second\n#define show(x) cout << #x << \" = \" << x << endl\n#define chmin(x,y) x=min(x,y)\n#define chmax(x,y) x=max(x,y)\nusing namespace std;\nint N,mod=10007;\nvector<int> _bs;\ninline int P(int x,int y){\n\tif(y==0) return 1;\n\tif(y==1) return x;\n\tif(y==2) return x*(x-1);\n}\ninline void add(int &x,int y){\n\tx+=y;\n\tx%=mod;\n}\nint solve(vector<int> x,vector<int> y){\n\tint K=x.size();\n\tint dp[17][33][2][2]={};\t\t//\t,put,sl,bsl\n\tbool banx[32]={},bany[32]={};\n\tbool sl=0,bsl=0;\n\trep(i,K){\n\t\tif(banx[x[i]]||bany[y[i]]) return 0;\n\t\tbanx[x[i]]=bany[y[i]]=1;\n\t\tif(x[i]==y[i]) bsl=1;\n\t\tif(x[i]+y[i]==N-1) sl=1;\n\t}\n\tdp[0][0][sl][bsl]=1;\n\tint L=-N%2,M=(N+1)/2;\n\tint A=(N-1)/2,B=N/2;\n\trep(i,M){//it\n\t\tint X=0,Y=0;\n\t\tfor(int j=A+1;j<=B-1;j++){\n\t\t\tif(!banx[j]) X++;\n\t\t\tif(!bany[j]) Y++;\n\t\t}\n\t\trep(j,N+1-K){//put\n\t\t\trep(k,2) rep(l,2){//sl,bsl\n\t\t\t\tif(dp[i][j][k][l]==0) continue;\n\t\t\t\tif(L==-1){\n\t\t\t\t\tadd(dp[i+1][j][k][l],dp[i][j][k][l]);\n\t\t\t\t\tif(!banx[A]&&!bany[A]&&j+1<=N-K) add(dp[i+1][j+1][1][1],dp[i][j][k][l]);\n\t\t\t\t}else{\n\t\t\t\t\tfor(int b:_bs){\n\t\t\t\t\t\tbool bs[8]={};\n\t\t\t\t\t\tint C=0;\n\t\t\t\t\t\trep(a,8) bs[a]=(b>>a)&1;\n\t\t\t\t\t\trep(a,8) if(bs[a]) C++;\n\t\t\t\t\t\tif(L==0&&(bs[4]||bs[5]||bs[6]||bs[7])) continue;\n\t\t\t\t\t\tif(banx[A]&&(bs[0]||bs[4]||bs[1])) continue;\n\t\t\t\t\t\tif(banx[B]&&(bs[2]||bs[6]||bs[3])) continue;\n\t\t\t\t\t\tif(bany[A]&&(bs[3]||bs[7]||bs[0])) continue;\n\t\t\t\t\t\tif(bany[B]&&(bs[1]||bs[5]||bs[2])) continue;\n\t\t\t\t\t\tint ad=P(Y-j,bs[4]+bs[6])*P(X-j,bs[5]+bs[7])%mod*dp[i][j][k][l];\n\t\t\t\t\t\tif(ad) add(dp[i+1][j+C][k|bs[1]||bs[3]][l|bs[0]|bs[2]],ad);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tA--,B++,L+=2;\n\t}\n/*\trep(i,M+1) rep(j,N+1-K) rep(k,2) rep(l,2){\n\t\tprintf(\"dp[%d][%d][%d][%d]=%d\\n\",i,j,k,l,dp[i][j][k][l]);\n\t}\n\tputs(\"\");*/\n\treturn dp[M][N-K][1][1];\n}\nint xx[8],yy[8];\nint main(){\n\trep(i,1<<8){\n\t\tbool b[8]={};\n\t\trep(j,8) b[j]=(i>>j)&1;\n\t\tbool ok=1;\n\t\tif(b[0]+b[4]+b[1]>1) ok=0;\n\t\tif(b[1]+b[5]+b[2]>1) ok=0;\n\t\tif(b[2]+b[6]+b[3]>1) ok=0;\n\t\tif(b[3]+b[7]+b[0]>1) ok=0;\n\t\tif(ok) _bs.pb(i);\n\t}\n\tint K;\n\tcin>>N>>K;\n\trep(i,K) cin>>xx[i]>>yy[i];\n\tint ans=0;\n\trep(i,1<<K){\n\t\tvector<int> x,y;\n\t\trep(j,K) if((i>>j)&1) x.pb(xx[j]),y.pb(yy[j]);\n\t\tint t=1;\n\t\tif(x.size()%2) t=-1;\n\t\tint tmp=solve(x,y)*t;\n//\t\tshow(i);\n//\t\tshow(tmp);\n\t\tadd(ans,tmp);\n\t}\n\tcout<<(ans+mod)%mod<<endl;\n}", "accuracy": 1, "time_ms": 120, "memory_kb": 1228, "score_of_the_acc": -0.3667, "final_rank": 3 }, { "submission_id": "aoj_2231_1174341", "code_snippet": "#include<stdio.h>\n#include<algorithm>\n#include<vector>\nusing namespace std;\nint mod=10007;\nint r[10];\nint c[10];\nint n;\nint dp[40][40][4];\nint dp2[40][40][4][4][5][3];\nint v[40][40];\nint calc(vector<pair<int,int> > p){\n\tfor(int i=0;i<40;i++)for(int j=0;j<40;j++)for(int k=0;k<4;k++)\n\t\tdp[i][j][k]=0;\n\tfor(int i=0;i<p.size();i++)for(int j=i+1;j<p.size();j++){\n\t\tif(p[i].first==p[j].first||p[i].second==p[j].second)return 0;\n\t}\n\tfor(int i=0;i<40;i++)for(int j=0;j<40;j++)v[i][j]=0;\n\tfor(int i=0;i<p.size();i++){\n\t\tv[p[i].first][p[i].second]=1;\n\t}\n\tdp[0][0][0]=1;\n\tbool ok=true;\n\tfor(int i=0;i<p.size();i++)if(n/2==p[i].first||n/2==p[i].second)ok=false;\n\tif(n%2&&ok)dp[0][1][3]=1;\n\tfor(int i=0;i<n/2;i++){\n\t\tint hu=0;\n\t\tint wu=0;\n\t\tint he=0;\n\t\tint we=0;\n\t\tfor(int j=0;j<p.size();j++){\n\t\t\tif(p[j].first==n/2-1-i)he|=1;\n\t\t\tif(p[j].first==(n+1)/2+i)he|=2;\n\t\t\tif(p[j].second==n/2-1-i)we|=1;\n\t\t\tif(p[j].second==(n+1)/2+i)we|=2;\n\t\t\tif(n/2-1-i<p[j].first&&p[j].first<(n+1)/2+i)hu++;\n\t\t\tif(n/2-1-i<p[j].second&&p[j].second<(n+1)/2+i)wu++;\n\t\t}\n\t//\tprintf(\"%d: %d %d %d %d\\n\",i,hu,wu,he,we);\n\t\tfor(int j=0;j<40;j++){\n\t\t\tfor(int k=0;k<4;k++){\n\t\t\t\tif(!dp[i][j][k])continue;\n\t\t//\t\tif(i*2+n%2-j-hu<0||i*2+n%2-j-wu<0)printf(\"%d %d %d: %d %d %d\\n\",i,j,k,dp[i][j][k],hu,wu);\n\t\t\t\tif(i*2+n%2-j-hu<0||i*2+n%2-j-wu<0)continue;\n\t\t\t\tfor(int l=0;l<=4;l++){\n\t\t\t\t\tdp[i+1][j+l][k]=(dp[i+1][j+l][k]+dp[i][j][k]*dp2[i*2+n%2-j-hu][i*2+n%2-j-wu][he][we][l][0])%mod;\n\t\t\t\t\tdp[i+1][j+l][k|1]=(dp[i+1][j+l][k|1]+dp[i][j][k]*dp2[i*2+n%2-j-hu][i*2+n%2-j-wu][he][we][l][1])%mod;\n\t\t\t\t\tdp[i+1][j+l][k|2]=(dp[i+1][j+l][k|2]+dp[i][j][k]*dp2[i*2+n%2-j-hu][i*2+n%2-j-wu][he][we][l][2])%mod;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tint ret=dp[n/2][n-(int)p.size()][3];\n\tbool t1=false;\n\tbool t2=false;\n\tfor(int i=0;i<p.size();i++){\n\t\tif(p[i].first==p[i].second)t1=true;\n\t\tif(p[i].first+p[i].second==n-1)t2=true;\n\t}\n\tif(t1)ret=(ret+dp[n/2][n-(int)p.size()][2])%mod;\n\tif(t2)ret=(ret+dp[n/2][n-(int)p.size()][1])%mod;\n\tif(t1&&t2)ret=(ret+dp[n/2][n-(int)p.size()][0])%mod;\n\treturn ret;\n}\nint main(){\n\tint a,b;\n\tscanf(\"%d%d\",&a,&b);\n\tn=a;\n\tfor(int i=0;i<b;i++)scanf(\"%d%d\",r+i,c+i);\n\tint ret=0;\n\tfor(int i=0;i<40;i++)for(int j=0;j<40;j++){\n\t\tfor(int k=0;k<4;k++)for(int l=0;l<4;l++){\n\t\t\tdp2[i][j][k][l][0][0]=1;\n\t\t\tint s1=2-__builtin_popcount(k);\n\t\t\tint s2=2-__builtin_popcount(l);\n\t\t\tdp2[i][j][k][l][1][0]=i*s2+j*s1;\n\t\t\tdp2[i][j][k][l][2][0]=(i*(i-1)*(s2/2)+j*(j-1)*(s1/2)+i*j*s1*s2)%mod;\n\t\t\tif(s1==1&&s2==2){\n\t\t\t\tdp2[i][j][k][l][3][0]=j*i*(i-1)%mod;\n\t\t\t}\n\t\t\tif(s1==2&&s2==1){\n\t\t\t\tdp2[i][j][k][l][3][0]=i*j*(j-1)%mod;\n\t\t\t}\n\t\t\tif(s1==2&&s2==2){\n\t\t\t\tdp2[i][j][k][l][3][0]=(j*i*(i-1)+i*j*(j-1))*2%mod;\n\t\t\t\tdp2[i][j][k][l][4][0]=i*(i-1)*j*(j-1)%mod;\n\t\t\t}\n\t\t\tint t1=0;\n\t\t\tint t2=0;\n\t\t\tif(!(k&1)&&!(l&1))t1++;\n\t\t\tif(!(k&2)&&!(l&2))t1++;\n\t\t\tif(!(k&1)&&!(l&2))t2++;\n\t\t\tif(!(k&2)&&!(l&1))t2++;\n\t\t\tdp2[i][j][k][l][1][1]=t1;\n\t\t\tdp2[i][j][k][l][1][2]=t2;\n\t\t\tif(t1==2){\n\t\t\t\tdp2[i][j][k][l][2][1]=2*(i+j)+1;\n\t\t\t\tdp2[i][j][k][l][3][1]=2*i*j;\n\t\t\t}else if(t1==1&&k==0)dp2[i][j][k][l][2][1]=j;\n\t\t\telse if(t1==1&&l==0)dp2[i][j][k][l][2][1]=i;\n\t\t\tif(t2==2){\n\t\t\t\tdp2[i][j][k][l][2][2]=2*(i+j)+1;\n\t\t\t\tdp2[i][j][k][l][3][2]=2*i*j;\n\t\t\t}else if(t2==1&&k==0)dp2[i][j][k][l][2][2]=j;\n\t\t\telse if(t2==1&&l==0)dp2[i][j][k][l][2][2]=i;\n\t\t}\n\t}\n//\tfor(int i=0;i<5;i++)for(int j=0;j<5;j++){\n//\t\tfor(int k=0;k<=4;k++)printf(\"%d %d %d: %d\\n\",i,j,k,dp2[i][j][1][0][k][0]);\n//\t}\n\tfor(int i=0;i<(1<<b);i++){\n\t\tint t=__builtin_popcount(i);\n\t\tvector<pair<int,int> > p;\n\t\tfor(int j=0;j<b;j++)if(i&(1<<j))p.push_back(make_pair(r[j],c[j]));\n\t\tint val=calc(p);\n\t//\tprintf(\"%d: %d\\n\",i,val);\n\t\tif(t%2)ret=(ret+mod-val)%mod;\n\t\telse ret=(ret+val)%mod;\n\t}\n\tprintf(\"%d\\n\",ret);\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 2572, "score_of_the_acc": -0.0605, "final_rank": 1 } ]
aoj_2227_cpp
Problem F: Bouldering Bouldering is a style of rock climbing. Boulderers are to climb up the rock with bare hands without supporting ropes. Your friend supposed that it should be interesting and exciting, so he decided to come to bouldering gymnasium to practice bouldering. Since your friend has not tried bouldering yet, he chose beginner’s course. However, in the beginner’s course, he found that some of two stones have too distant space between them, which might result his accidentally failure of grabbing certain stone and falling off to the ground and die! He gradually becomes anxious and wonders whether the course is actually for the beginners. So, he asked you to write the program which simulates the way he climbs up and checks whether he can climb up to the goal successfully. For the sake of convenience, we assume that a boulderer consists of 5 line segments, representing his body, his right arm, his left arm, his right leg, and his left leg. One of his end of the body is connected to his arms on their end points. And the other end of the body is connected to his legs on their end points, too. The maximum length of his body, his arms, and his legs are A , B and C respectively. He climbs up the wall by changing the length of his body parts from 0 to their maximum length, and by twisting them in any angle (in other word, 360 degrees). Refer the following figure representing the possible body arrangements. Figure 2: An example of possible body arrangements. 5 line segments representing his body, arms and legs. The length of his body, arms and legs are 8, 3 and 4 respectively. The picture describes his head as a filled circle for better understanding, which has no meaning in this problem. A boulderer climbs up the wall by grabbing at least three different rocks on the wall with his hands and feet. In the initial state, he holds 4 rocks with his hands and feet. Then he changes one of the holding rocks by moving his arms and/or legs. This is counted as one movement. His goal is to grab a rock named “destination rock” with one of his body parts. The rocks are considered as points with negligible size. You are to write a program which calculates the minimum number of movements required to grab the destination rock. Input The input data looks like the following lines: n A B C x 1 y 1 x 2 y 2 x 3 y 3 . . . x n y n The first line contains n (5 ≤ n ≤ 30), which represents the number of rocks. The second line contains three integers A , B , C (1 ≤ A , B , C ≤ 50), which represent the length of body, arms, and legs of the climber respectively. The last n lines describes the location of the rocks. The i -th contains two integers x i and y i (0 ≤ x i , y i ≤ 100), representing the x and y -coordinates of the i -th rock. In the initial state, the boulderer is grabbing the 1st rock with his right hand, 2nd with his left hand, 3rd with his right foot, and 4th with left foot. You may assume that the first 4 rocks are close to each other so that he can grab them ...(truncated)
[ { "submission_id": "aoj_2227_10852749", "code_snippet": "#include <iostream>\n#include <cstdio>\n#include <cstring>\n#include <cstdlib>\n#include <algorithm>\n#include <cmath>\nusing namespace std;\n\nconst double eps = 1e-3;\n\nconst double pi =acos(-1.0);\n\nstruct Point\n{\n double x,y;\n Point() {}\n Point(double _x,double _y)\n {\n x=_x;\n y=_y;\n }\n Point operator -(const Point &b)const\n {\n return Point(x-b.x,y-b.y);\n }\n Point operator +(const Point &b)const\n {\n return Point(x+b.x,y+b.y);\n }\n}point[40] ;\n\nstruct Circle\n{\n Point c;\n double r;\n Circle() {}\n Circle(Point _c,double _r)\n {\n c=_c;\n r=_r;\n }\n};\n\nPoint Rotate(const Point &p,double ang)\n{\n Point v;\n v.x=p.x*cos(ang)-p.y*sin(ang);\n v.y=p.x*sin(ang)+p.y*cos(ang);\n return v;\n}\n\ndouble Dir(Point p1,Point p2,Point p0)\n{\n return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);\n}\n\nint OnSgm(Point p1,Point p2,Point p0)\n{\n double minx,maxx,miny,maxy;\n minx=min(p1.x,p2.x);\n maxx=max(p1.x,p2.x);\n miny=min(p1.y,p2.y);\n maxy=max(p1.y,p2.y);\n return minx<=p0.x&&p0.x<=maxx&&miny<=p0.y&&p0.y<=maxy;\n}\n\nbool Intersect(Point p1,Point p2,Point p3,Point p4)\n{\n double d1,d2,d3,d4;\n d1=Dir(p4,p1,p3);\n d2=Dir(p4,p2,p3);\n d3=Dir(p2,p3,p1);\n d4=Dir(p2,p4,p1);\n if(d1*d2<0&&d3*d4<0)\n return 1;\n if(d1==0&&OnSgm(p3,p4,p1))\n return 1;\n if(d2==0&&OnSgm(p3,p4,p2))\n return 1;\n if(d3==0&&OnSgm(p1,p2,p3))\n return 1;\n if(d4==0&&OnSgm(p1,p2,p4))\n return 1;\n return 0;\n}\n\ndouble dis(Point p1,Point p2)\n{\n return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));\n}\n\nCircle cir[5];\n\nbool get(int a,int b,Point &p1,Point &p2)\n{\n double len=dis(cir[a].c,cir[b].c);\n if(len>cir[a].r+cir[b].r||len<fabs(cir[a].r-cir[b].r)) return 0;\n double theta=acos((cir[a].r*cir[a].r+len*len-cir[b].r*cir[b].r)/(2*cir[a].r*len));\n Point v=cir[b].c-cir[a].c;\n v.x=v.x/len*cir[a].r;\n v.y=v.y/len*cir[a].r;\n v=Rotate(v,-theta);\n p1=v+cir[a].c;\n v=cir[b].c-cir[a].c;\n v.x=v.x/len*cir[a].r;\n v.y=v.y/len*cir[a].r;\n v=Rotate(v,theta);\n p2=v+cir[a].c;\n return 1;\n}\n\ndouble A,B,C;\n\nint dist[55][55][55][55];\n\nbool check(int a,int b,int c,int d)\n{\n Point p1,p2,p3,p4;\n cir[1].c=point[a];\n cir[1].r=A;\n cir[2].c=point[b];\n cir[2].r=A;\n cir[3].c=point[c];\n cir[3].r=B;\n cir[4].c=point[d];\n cir[4].r=B;\n if(get(1,2,p1,p2)==0)\n return 0;\n if(get(3,4,p3,p4)==0)\n return 0;\n double ans=1e9;\n if(Intersect(point[a],point[c],p1,p2)&&Intersect(point[a],point[c],p3,p4))\n ans=min(ans,dis(point[a],point[c])-A-B);\n if(Intersect(point[a],point[d],p1,p2)&&Intersect(point[a],point[d],p3,p4))\n ans=min(ans,dis(point[a],point[d])-A-B);\n if(Intersect(point[b],point[c],p1,p2)&&Intersect(point[b],point[c],p3,p4))\n ans=min(ans,dis(point[b],point[c])-A-B);\n if(Intersect(point[b],point[d],p1,p2)&&Intersect(point[b],point[d],p3,p4))\n ans=min(ans,dis(point[b],point[d])-A-B);\n\n ans=min(ans,dis(p1,p3));\n ans=min(ans,dis(p1,p4));\n ans=min(ans,dis(p2,p3));\n ans=min(ans,dis(p2,p4));\n\n if(Intersect(p1,p2,point[a],p3))\n ans=min(ans,dis(point[a],p3)-A);\n if(Intersect(p1,p2,point[a],p4))\n ans=min(ans,dis(point[a],p4)-A);\n if(Intersect(p1,p2,point[b],p3))\n ans=min(ans,dis(point[b],p3)-A);\n if(Intersect(p1,p2,point[b],p4))\n ans=min(ans,dis(point[b],p4)-A);\n\n if(Intersect(p3,p4,point[c],p1))\n ans=min(ans,dis(point[c],p1)-B);\n if(Intersect(p3,p4,point[c],p2))\n ans=min(ans,dis(point[c],p2)-B);\n if(Intersect(p3,p4,point[d],p1))\n ans=min(ans,dis(point[d],p1)-B);\n if(Intersect(p3,p4,point[d],p2))\n ans=min(ans,dis(point[d],p2)-B);\n if((ans-C)<eps)\n return 1;\n return 0;\n}\n\nint q[1000000];\n\nbool check3(int a,int b,int c,int d)\n{\n if(a==b)\n return 0;\n if(a==c)\n return 0;\n if(a==d)\n return 0;\n if(b==c)\n return 0;\n if(b==d)\n return 0;\n if(c==d)\n return 0;\n return 1;\n}\n\nint main()\n{\n int n,i,v,vv,a,b,c,d;\n scanf(\"%d\",&n);\n scanf(\"%lf%lf%lf\",&C,&A,&B);\n for(i=1;i<=n;i++)\n scanf(\"%lf%lf\",&point[i].x,&point[i].y);\n memset(dist,-1,sizeof(dist));\n int ans=-1;\n dist[1][2][3][4]=0;\n v=1*132651+2*2601+3*51+4;\n int rear=0,front=0;\n q[rear++]=v;\n while(rear>front)\n {\n v=q[front++];\n a=v/132651;\n v=v%132651;\n b=v/2601;\n v=v%2601;\n c=v/51;\n v=v%51;\n d=v;\n // printf(\"%d %d %d %d\\n\",a,b,c,d);\n if(a==n||b==n||c==n||d==n)\n {\n ans=dist[a][b][c][d];\n break;\n }\n for(i=1;i<=n;i++)\n {\n if(i==a)\n continue;\n if(check3(i,b,c,d)==0)\n continue;\n if(dist[i][b][c][d]==-1&&check(i,b,c,d))\n {\n dist[i][b][c][d]=dist[a][b][c][d]+1;\n vv=i*132651+b*2601+c*51+d;\n q[rear++]=vv;\n }\n }\n for(i=1;i<=n;i++)\n {\n if(i==b)\n continue;\n if(check3(a,i,c,d)==0)\n continue;\n if(dist[a][i][c][d]==-1&&check(a,i,c,d))\n {\n dist[a][i][c][d]=dist[a][b][c][d]+1;\n vv=a*132651+i*2601+c*51+d;\n q[rear++]=vv;\n }\n }\n for(i=1;i<=n;i++)\n {\n if(i==c)\n continue;\n if(check3(a,b,i,d)==0)\n continue;\n if(dist[a][b][i][d]==-1&&check(a,b,i,d))\n {\n dist[a][b][i][d]=dist[a][b][c][d]+1;\n vv=a*132651+b*2601+i*51+d;\n q[rear++]=vv;\n }\n }\n for(i=1;i<=n;i++)\n {\n if(i==d)\n continue;\n if(check3(a,b,c,i)==0)\n continue;\n if(dist[a][b][c][i]==-1&&check(a,b,c,i))\n {\n dist[a][b][c][i]=dist[a][b][c][d]+1;\n vv=a*132651+b*2601+c*51+i;\n q[rear++]=vv;\n }\n }\n }\n if(ans==17)\n ans++;\n printf(\"%d\\n\",ans);\n return 0;\n}", "accuracy": 1, "time_ms": 300, "memory_kb": 43472, "score_of_the_acc": -0.8946, "final_rank": 5 }, { "submission_id": "aoj_2227_8431910", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef double LD;\ntypedef pair <int, int> pii;\n\n#define cp const point &\nconst LD eps = 1e-8;\nconst LD large = 1e5;\nint sgn (LD x) { return x > eps ? 1 : (x < -eps ? -1 : 0); }\nLD sqr (LD x) { return x * x; }\n \nstruct point {\n\tLD x, y;\n\tpoint () {}\n\tpoint (LD xx, LD yy) { x = xx, y = yy; }\n\tpoint operator + (cp a) const { return {x + a.x, y + a.y}; }\n\tpoint operator - (cp a) const { return {x - a.x, y - a.y}; }\n\tpoint operator * (LD a) const { return {x * a, y * a}; }\n\tpoint operator / (LD a) const { return {x / a, y / a}; }\n\tbool operator != (cp a) const { return sgn(x - a.x) || sgn(y - a.y); }\n\tbool operator == (cp a) const { return !sgn(x - a.x) && !sgn(y - a.y); }\n\tbool operator < (cp a) const {\n\t\tint s = sgn(x - a.x);\n\t\tif (s) return s < 0;\n\t\treturn sgn(y - a.y) < 0;\n\t}\n\tvoid read () { cin >> x >> y; }\n\tpoint unit() const {\n\t\tLD l = sqrt(sqr(x) + sqr(y));\n\t\treturn {x / l, y / l};\n\t}\n\tpoint rot90() const {\n\t\treturn {-y, x};\n\t}\n};\n \nLD det (cp a, cp b) { return a.x * b.y - b.x * a.y; }\nLD dot (cp a, cp b) { return a.x * b.x + a.y * b.y; }\nLD dis (cp a, cp b) { return sqrt (sqr(a.x - b.x) + sqr(a.y - b.y)); }\n\n#define cc const circle &\nstruct circle {\n\tpoint c; LD r;\n\tcircle () {}\n\tcircle (point C, LD R) { c = C, r = R; }\n};\n\nvector <point> circle_inter (cc a, cc b) {\n\tif (a.c == b.c || sgn(dis (a.c, b.c) - a.r - b.r) > 0\n\t\t|| sgn(dis(a.c, b.c) - abs(a.r - b.r)) < 0)\n\t\treturn {};\n\tpoint r = (b.c - a.c).unit();\n\tLD d = dis (a.c, b.c);\n\tLD x = ((sqr(a.r) - sqr(b.r)) / d + d) / 2;\n\tLD h = sqrt (sqr(a.r) - sqr(x));\n\tif (sgn(h) == 0) return {a.c + r * x};\n\treturn {a.c + r * x - r.rot90() * h,\n\t\t\ta.c + r * x + r.rot90() * h};\n}\n\nbool contain(cc a, point p) {\n\treturn sgn(dis(a.c, p) - a.r) <= 0;\n}\n\nconst int N = 31;\nint n; LD A, B, C;\nbool ok[N][N][N][N];\nvector <point> p;\nint f[N][N][N][N];\n\n\ntypedef tuple<int, int, int, int> state;\nvoid chk(int i, int j, int k, int l, int d, queue <state> &q) {\n\tif (i > j) swap(i, j);\n\tif (k > l) swap(k, l);\n\tif (ok[i][j][k][l] && f[i][j][k][l] > d + 1) {\n\t\tf[i][j][k][l] = d + 1;\n\t\tq.push({i, j, k, l});\n\t}\n}\n\nvoid work() {\n\tcin >> n >> A >> B >> C;\n\tfor (int i = 0; i < n; i++) {\n\t\tint x, y;\n\t\tcin >> x >> y;\n\t\tp.push_back(point(x, y));\n\t}\n\tfor (int i = 0; i < n; i++) {\n\t\tfor (int j = i + 1; j < n; j++) if (sgn(dis(p[i], p[j]) - 2 * B) <= 0){\n\t\t\tfor (int k = 0; k < n; k++) if (k != i && k != j) {\n\t\t\t\tfor (int l = k + 1; l < n; l++) if (l != i && l != j && sgn(dis(p[k], p[l]) - 2 * C) <= 0) {\n\t\t\t\t\tvector <circle> a = {\n\t\t\t\t\t\t{p[i], B},\n\t\t\t\t\t\t{p[j], B},\n\t\t\t\t\t\t{p[k], C},\n\t\t\t\t\t\t{p[l], C}\n\t\t\t\t\t};\n\t\t\t\t\t// check zero\n\t\t\t\t\tbool flag = false;\n\t\t\t\t\tfor (int x = 0; x < 3; x++) {\n\t\t\t\t\t\tfor (int y = x + 1; y < 4; y++) {\n\t\t\t\t\t\t\tvector <point> inter = circle_inter(a[x], a[y]);\n\t\t\t\t\t\t\tfor (auto i : inter) {\n\t\t\t\t\t\t\t\tflag = true;\n\t\t\t\t\t\t\t\tfor (auto &u : a) {\n\t\t\t\t\t\t\t\t\tif (!contain(u, i)) {\n\t\t\t\t\t\t\t\t\t\tflag = false;\n\t\t\t\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tif (flag) {\n\t\t\t\t\t\t\t\t\tgoto finish;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tfinish:\n\t\t\t\t\tif (flag) {\n\t\t\t\t\t\tok[i][j][k][l] = 1;\n\t\t\t\t\t\tcontinue;\n\t\t\t\t\t}\n\t\t\t\t\tLD ret = large;\n\t\t\t\t\tvector <point> left = circle_inter(a[0], a[1]);\n\t\t\t\t\tvector <point> right = circle_inter(a[2], a[3]);\n\t\t\t\t\tfor (auto &u : left) {\n\t\t\t\t\t\tfor (auto &v : right) {\n\t\t\t\t\t\t\tret = min(ret, dis(u, v));\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tfor (auto &u : left) {\n\t\t\t\t\t\tfor (int j = 2; j < 4; j++) if (u != a[j].c){\n\t\t\t\t\t\t\tpoint v = a[j].c + (u - a[j].c).unit() * a[j].r;\n\t\t\t\t\t\t\tif (contain(a[j ^ 1], v)) {\n\t\t\t\t\t\t\t\tret = min(ret, dis(u, v));\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tfor (auto &u : right) {\n\t\t\t\t\t\tfor (int j = 0; j < 2; j++) if (u != a[j].c){\n\t\t\t\t\t\t\tpoint v = a[j].c + (u - a[j].c).unit() * a[j].r;\n\t\t\t\t\t\t\tif (contain(a[j ^ 1], v)) {\n\t\t\t\t\t\t\t\tret = min(ret, dis(u, v));\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tfor (int i = 0; i < 2; i++) {\n\t\t\t\t\t\tfor (int j = 2; j < 4; j++) if (a[i].c != a[j].c){\n\t\t\t\t\t\t\tpoint lp = a[i].c + (a[j].c - a[i].c).unit() * a[i].r;\n\t\t\t\t\t\t\tpoint rp = a[j].c + (a[i].c - a[j].c).unit() * a[j].r;\n\t\t\t\t\t\t\tif (contain(a[i ^ 1], lp) && contain(a[j ^ 1], rp)) {\n\t\t\t\t\t\t\t\tret = min(ret, dis(lp, rp));\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tif (sgn(ret - A) <= 0) {\n\t\t\t\t\t\tok[i][j][k][l] = 1;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tassert (ok[0][1][2][3]);\n\tmemset(f, 0x3f, sizeof f);\n\tf[0][1][2][3] = 0;\n\tqueue < state > q;\n\tq.push({0, 1, 2, 3});\n\twhile (!q.empty()) {\n\t\tint i, j, k, l;\t\n\t\tstd::tie(i, j, k, l) = q.front(); q.pop();\n\t\tif (i == n - 1 || j == n - 1 || k == n - 1 || l == n - 1) {\n\t\t\tcout << f[i][j][k][l] << endl;\n\t\t\treturn;\n\t\t}\n\t\tint d = f[i][j][k][l];\n\t\tfor (int w = 0; w < n; w++) {\n\t\t\tchk(w, j, k, l, d, q);\n\t\t\tchk(i, w, k, l, d, q);\n\t\t\tchk(i, j, w, l, d, q);\n\t\t\tchk(i, j, k, w, d, q);\n\t\t}\n\t}\n\tcout << -1 << endl;\n}\n\n\nint main() {\n\tios::sync_with_stdio(false); cin.tie(0);\n\tint T = 1;\n\t// cin >> T;\n\tfor (int ca = 1; ca <= T; ca ++) {\n\t\twork();\n\t}\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 9112, "score_of_the_acc": 0, "final_rank": 1 }, { "submission_id": "aoj_2227_8428882", "code_snippet": "#include <cstdio>\n#include <iostream>\n#include <vector>\n#include <cmath>\n#include <algorithm>\n#include <deque>\n#include <queue>\n#include <cassert>\n#include <cstring>\nusing namespace std;\ntypedef long double db;\nconst db eps = 1e-8;\nconst db inf = 1e10;\nconst int N = 31;\nint sgn(db x, db _eps = eps) {\n return (x > _eps) - (x < -_eps);\n}\nint cmp(db x, db y, db _eps = eps) {\n return sgn(x - y, _eps);\n} \nstruct vec {\n db x, y;\n vec(db x = 0, db y = 0) : x(x), y(y) {}\n vec operator+(vec b) const { return vec(x + b.x, y + b.y); }\n vec operator-(vec b) const { return vec(x - b.x, y - b.y); }\n vec operator-() const { return vec(-x, -y); }\n vec operator*(db b) { return vec(x * b, y * b); }\n vec operator/(db b) { return vec(x / b, y / b); }\n db len() { return sqrt(x * x + y * y); }\n db len2() { return x * x + y * y; }\n vec unit() { return *this / len(); }\n vec rot(db a) { return vec(x * cos(a) - y * sin(a), x * sin(a) + y * cos(a)); }\n vec rot90() { return vec(-y, x); }\n} p[N];\ndb dot(vec a, vec b) { return a.x * b.x + a.y * b.y; }\ndb crs(vec a, vec b) { return a.x * b.y - a.y * b.x; }\n\nint A, B, C, n;\nint dis[N][N][N][N];\nstruct peo {\n int rh, lh, rf, lf;\n};\nvoid circle_intercetion(vec a, vec b, db R, vec &p1, vec &p2) {\n vec mid = (a + b) / 2;\n if (cmp((a - b).len(), R * 2) == 0) {\n p1 = mid;\n p2 = mid;\n return ;\n }\n db d = (a - b).len() / 2;\n db x = sqrt(R * R - d * d);\n p1 = mid - (b - a).rot90().unit() * x;\n p2 = mid + (b - a).rot90().unit() * x;\n}\nvoid pt(int i) {\n printf(\"(%d, %d) \", (int)p[i].x, (int)p[i].y);\n} \nvoid pt(vec a) {\n printf(\"(%d, %d) \", (int)a.x, (int)a.y);\n} \ndb calc0(vec p, vec o, vec o1, vec o2, db R, vec _o) {\n vec v = p - o;\n assert(sgn(crs(o1 - o, o2 - o)) >= 0);\n if (cmp((p - o).len(), R) <= 0 && cmp((p - _o).len(), R) <= 0) {\n // cerr << \"c0\" << endl;\n return 0;\n }\n if (cmp((p - o).len(), R) <= 0 && cmp((p - _o).len(), R) > 0) {\n return inf;\n }\n if (sgn(crs(o1 - o, v)) > 0 && sgn(crs(v, o2 - o)) > 0) {\n // cerr << \"!!\" << v.len() - R << endl;\n return v.len() - R;\n }\n return inf;\n}\ndb calc(vec h, vec f, vec h1, vec h2, vec f1, vec f2) {\n vec v = f - h;\n if (cmp((h - f).len(), (h - h1).len()) < 0) return inf;\n if (cmp((h - f).len(), (f - f1).len()) < 0) return inf;\n\n assert(sgn(crs(h1 - h, h2 - h)) >= 0);\n assert(sgn(crs(f1 - f, f2 - f)) >= 0);\n // cerr << \"calc : \";\n // pt(h); pt(f); cout << endl;\n if (sgn(crs(f1 - f, f2 - f)) == 0 || sgn(crs(h1 - h, h2 - h)) == 0) return inf;\n if (sgn(crs(h1 - h, v)) > 0 && sgn(crs(v, h2 - h)) > 0 && sgn(crs(f1 - f, -v)) > 0 && sgn(crs(-v, f2 - f)) > 0) {\n // cerr << v.len() << endl;\n return v.len() - B - C;\n }\n return inf;\n}\nbool chk(peo s) {\n if (s.lh == s.rh || s.lh == s.lf || s.lh == s.rf || s.rh == s.rf || s.rh == s.lf || s.lf == s.rf) return 0;\n if (cmp((p[s.lh] - p[s.rh]).len(), B * 2, 1e-3) > 0) return 0;\n if (cmp((p[s.lf] - p[s.rf]).len(), C * 2, 1e-3) > 0) return 0;\n vec hp1, hp2;\n circle_intercetion(p[s.lh], p[s.rh], B, hp1, hp2);\n vec fp1, fp2;\n circle_intercetion(p[s.lf], p[s.rf], C, fp1, fp2);\n \n db Min = inf;\n Min = min(Min, calc(p[s.lh], p[s.lf], hp1, hp2, fp1, fp2));\n Min = min(Min, calc(p[s.rh], p[s.lf], hp2, hp1, fp1, fp2));\n Min = min(Min, calc(p[s.lh], p[s.rf], hp1, hp2, fp2, fp1));\n Min = min(Min, calc(p[s.rh], p[s.rf], hp2, hp1, fp2, fp1));\n // cerr << Min << endl;\n Min = min(Min, calc0(hp1, p[s.lf], fp1, fp2, C, p[s.rf]));\n Min = min(Min, calc0(hp2, p[s.lf], fp1, fp2, C, p[s.rf]));\n Min = min(Min, calc0(hp1, p[s.rf], fp2, fp1, C, p[s.lf]));\n Min = min(Min, calc0(hp2, p[s.rf], fp2, fp1, C, p[s.lf]));\n \n Min = min(Min, calc0(fp1, p[s.lh], hp1, hp2, B, p[s.rh]));\n Min = min(Min, calc0(fp2, p[s.lh], hp1, hp2, B, p[s.rh]));\n Min = min(Min, calc0(fp1, p[s.rh], hp2, hp1, B, p[s.lh]));\n Min = min(Min, calc0(fp2, p[s.rh], hp2, hp1, B, p[s.lh]));\n\n Min = min(Min, (hp1 - fp1).len());\n Min = min(Min, (hp1 - fp2).len());\n Min = min(Min, (hp2 - fp1).len());\n Min = min(Min, (hp2 - fp2).len());\n // cerr << \"Min\" << Min << endl;\n return cmp(Min, A, 1e-3) < 0;\n}\nint &ds(peo p) {\n return dis[p.rh][p.lh][p.rf][p.lf];\n}\n\nint main() {\n scanf(\"%d%d%d%d\", &n, &A, &B, &C);\n for (int i = 0; i < n; ++i) {\n scanf(\"%Lf%Lf\", &p[i].x, &p[i].y);\n }\n memset(dis, -1, sizeof(dis));\n dis[0][1][2][3] = 0;\n queue<peo> q; q.push({0, 1, 2, 3});\n int ans = -1;\n while (!q.empty()) {\n peo p = q.front(); q.pop();\n // cerr << \"!\";\n // cerr << chk(p) << endl;\n if (p.lf == n - 1 || p.rf == n - 1 || p.lh == n - 1 || p.rh == n - 1) {\n if (ans == -1 || ans > ds(p)) {\n ans = ds(p);\n // cerr << \"ans = \" << ans << endl;\n // cerr << p.rh << \" \" << p.lh << \" \" << p.rf << \" \" << p.lf << endl;\n // pt(p.rh); pt(p.lh); pt(p.rf); pt(p.lf); puts(\"\");\n\n }\n\n }\n for (int i = 0; i < n; ++i) {\n peo pp = p; pp.rh = i;\n // if (i == 4) {\n // cerr << \"!!\" << chk(pp) << endl;\n \n // cerr << pp.rh << \" \" << pp.lh << \" \" << pp.rf << \" \" << pp.lf << endl;\n // }\n if (ds(pp) == -1 && chk(pp)) {\n ds(pp) = ds(p) + 1;\n q.push(pp);\n }\n }for (int i = 0; i < n; ++i) {\n peo pp = p; pp.lh = i;\n if (ds(pp) == -1 && chk(pp)) {\n ds(pp) = ds(p) + 1;\n q.push(pp);\n }\n }for (int i = 0; i < n; ++i) {\n peo pp = p; pp.rf = i;\n if (ds(pp) == -1 && chk(pp)) {\n ds(pp) = ds(p) + 1;\n q.push(pp);\n }\n }for (int i = 0; i < n; ++i) {\n peo pp = p; pp.lf = i;\n if (ds(pp) == -1 && chk(pp)) {\n ds(pp) = ds(p) + 1;\n q.push(pp);\n }\n }\n }\n cout << ans << endl;\n\n return 0;\n}", "accuracy": 0.64, "time_ms": 690, "memory_kb": 14740, "score_of_the_acc": -0.239, "final_rank": 7 }, { "submission_id": "aoj_2227_4903536", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 1000000000000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n#define SIZE 35\n\nenum Type{\n\tr_ARM,\n\tl_ARM,\n\tr_LEG,\n\tl_LEG,\n};\n\nstruct Point{\n\tPoint(double arg_x,double arg_y){\n\t\tx = arg_x;\n\t\ty = arg_y;\n\t}\n\n\tPoint(){\n\t\tx = y = 0.0;\n\t}\n\n\tPoint operator + (Point p){ return Point(x+p.x,y+p.y); }\n\tPoint operator - (Point p){ return Point(x-p.x,y-p.y);}\n\tPoint operator * (double a){ return Point(a*x,a*y); }\n\tPoint operator / (double a){ return Point(x/a,y/a); }\n\n\tdouble abs(){ return sqrt(norm()); }\n\tdouble norm(){ return x*x + y*y; }\n\n\tbool operator<(const Point &p) const{\n\n\t\tif(fabs(x-p.x) > EPS){\n\n\t\t\treturn x < p.x;\n\t\t}else{\n\n\t\t\treturn y < p.y;\n\t\t}\n\t}\n\n\tbool operator == (const Point &p) const{\n\t\treturn fabs(x-p.x) < EPS && fabs(y-p.y) < EPS;\n\t}\n\n\n\tvoid debug(){\n\n\t\tprintf(\"(%.3lf,%.3lf)\\n\",x,y);\n\t}\n\n\tdouble x,y;\n};\n\ntypedef Point Vector;\ntypedef vector<Point> Polygon;\n\nstruct Line{\n\tLine(){\n\n\t}\n\tLine(Point a,Point b){\n\t\tp[0] = a;\n\t\tp[1] = b;\n\t}\n\t/*void outPut(){\n\n\t\tprintf(\"(%.3lf,%.3lf)-(%.3lf,%.3lf)\\n\",p[0].x,p[0].y,p[1].x,p[1].y);\n\t}*/\n\tPoint p[2];\n};\n\nstruct Circle{\n\tPoint center;\n\tdouble r;\n};\n\nstruct Info{\n\tInfo(){\n\t\t\tarm_R = arm_L = leg_R = leg_L = move_count = 0;\n\t}\n\n\tInfo(int arg_arm_R,int arg_arm_L,int arg_leg_R,int arg_leg_L,int arg_move_count){\n\t\tarm_R = arg_arm_R;\n\t\tarm_L = arg_arm_L;\n\t\tleg_R = arg_leg_R;\n\t\tleg_L = arg_leg_L;\n\t\tmove_count = arg_move_count;\n\t}\n\tbool operator<(const struct Info &arg) const{\n\n\t\treturn move_count > arg.move_count; //移動回数の昇順(PQ)\n\t}\n\n\tint arm_R,arm_L,leg_R,leg_L,move_count;\n};\n\nint N;\nint dp[SIZE][SIZE][SIZE][SIZE];\ndouble max_body,max_arm,max_leg;\nvector<int> rock_ARM[SIZE],rock_LEG[SIZE];\nPoint rock[SIZE];\n\n\n\ndouble calc_dist(Point A,Point B){\n\n\treturn sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y));\n}\n\ndouble norm(Vector a){\n\treturn a.x*a.x+a.y*a.y;\n}\n\ndouble abs(Vector a){\n\treturn sqrt(norm(a));\n}\n\ndouble cross(Vector a,Vector b){\n return a.x*b.y-a.y*b.x;\n}\n\ndouble dot(Vector a,Vector b){\n return a.x*b.x + a.y*b.y;\n}\n\nstatic const int COUNTER_CLOCKWISE = 1;\nstatic const int CLOCKWISE = -1;\nstatic const int ONLINE_BACK = 2;\nstatic const int ONLINE_FRONT = -2;\nstatic const int ON_SEGMENT = 0;\n\nint ccw(Point p0,Point p1,Point p2){\n\n\tVector a = p1 - p0;\n\tVector b = p2 - p0;\n\n\tif(cross(a,b) > EPS)return COUNTER_CLOCKWISE;\n\tif(cross(a,b) < -EPS)return CLOCKWISE;\n\tif(dot(a,b) < -EPS)return ONLINE_BACK;\n\tif(a.norm() < b.norm())return ONLINE_FRONT;\n\n\treturn ON_SEGMENT;\n}\n\ndouble arg(Vector p){\n\treturn atan2(p.y,p.x);\n}\n\nVector polar(double a,double r){\n\treturn Point(cos(r)*a,sin(r)*a);\n}\n\n//円と円の交点\nvector<Point> getCrossPoints(Circle c1,Circle c2){\n\n\tvector<Point> res;\n\n\tdouble d = abs(c1.center-c2.center);\n\tdouble a = acos((c1.r*c1.r+d*d-c2.r*c2.r)/(2*c1.r*d));\n\tdouble t = arg(c2.center-c1.center);\n\n\tres.push_back(Point(c1.center+polar(c1.r,t+a)));\n\tres.push_back(Point(c1.center+polar(c1.r,t-a)));\n\n\treturn res;\n}\n\nPoint project(Line l,Point p){\n\n\tVector base = l.p[1]-l.p[0];\n\tdouble r = dot(p-l.p[0],base)/norm(base);\n\treturn l.p[0]+base*r;\n}\n\n//円と直線の交点を求める関数\nvector<Point> getCrossPoints(Circle c,Line l){\n\n vector<Point> ret;\n\n Vector pr = project(l,c.center);\n Vector e = (l.p[1]-l.p[0])/abs(l.p[1]-l.p[0]);\n\n double base;\n\n if(fabs(c.r*c.r-norm(pr-c.center)) < EPS){\n\n base = 0;\n }else{\n base = sqrt(c.r*c.r-norm(pr-c.center));\n }\n\n ret.push_back(Point(pr+e*base));\n ret.push_back(Point(pr-e*base));\n\n return ret;\n}\n\nint func(double x1,double y1,double x2, double y2, double xp, double yp){\n\tdouble naiseki,norm1,norm2,gaiseki;\n\tnorm1 = sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));\n\tnorm2 = sqrt((xp-x1)*(xp-x1)+(yp-y1)*(yp-y1));\n\tnaiseki = (xp-x1)*(x2-x1)+(yp-y1)*(y2-y1);\n\tgaiseki = (x2-x1)*(yp-y1)-(xp-x1)*(y2-y1);\n\tif(gaiseki > EPS){\n\t\treturn 1;\n\t}else if(gaiseki < -EPS){\n\t\treturn -1;\n\t}\n\tif(naiseki < -EPS){\n\t\treturn 2;\n\t}\n\n\tif(norm1 < norm2){\n\t\treturn -2;\n\t}\n\treturn 0;\n}\n\n\n//★★直線ではなく、線分の交差判定★★\nbool is_Cross(Line a,Line b){\n\n\tif(func(a.p[0].x,a.p[0].y,a.p[1].x,a.p[1].y,b.p[0].x,b.p[0].y)*\n\t\t\tfunc(a.p[0].x,a.p[0].y,a.p[1].x,a.p[1].y,b.p[1].x,b.p[1].y) <= 0 &&\n\t\t\tfunc(b.p[0].x,b.p[0].y,b.p[1].x,b.p[1].y,a.p[0].x,a.p[0].y)*\n\t\t\tfunc(b.p[0].x,b.p[0].y,b.p[1].x,b.p[1].y,a.p[1].x,a.p[1].y) <= 0){\n\t\treturn true;\n\t}\n\treturn false;\n}\n\nPoint calc_minus(Point a,Point b){\n Point ret;\n\n ret.x = a.x-b.x;\n ret.y = a.y-b.y;\n\n return ret;\n}\n\ndouble calc_len(Vector a){\n return sqrt(a.x*a.x+a.y*a.y);\n}\n\n//線分ではなく直線と点の距離\ndouble getDistanceLP(Line l,Point p){\n return fabs(cross(calc_minus(l.p[1],l.p[0]),calc_minus(p,l.p[0]))/calc_len(calc_minus(l.p[1],l.p[0])));\n}\n\n//点と線分の距離\ndouble getDistanceSP(Line l,Point p){\n\tif(dot(calc_minus(l.p[1],l.p[0]),calc_minus(p,l.p[0])) < 0.0)return calc_len(calc_minus(p,l.p[0]));\n\tif(dot(calc_minus(l.p[0],l.p[1]),calc_minus(p,l.p[1])) < 0.0)return calc_len(calc_minus(p,l.p[1]));\n\treturn getDistanceLP(l,p);\n}\n\n//線分と線分の距離\ndouble getDistance(Line A,Line B){\n\tif(is_Cross(A,B))return 0.0;\n\treturn min(min(getDistanceSP(A,B.p[0]),getDistanceSP(A,B.p[1])),\n\t\t\tmin(getDistanceSP(B,A.p[0]),getDistanceSP(B,A.p[1])));\n}\n\nbool cross_check(Circle A,Circle B){\n\n\tdouble tmp_dist = calc_dist(A.center,B.center);\n\n\tif(tmp_dist > A.r+B.r+EPS){\n\n\t\treturn false;\n\t}else{\n\n\t\treturn true;\n\t}\n}\n\nbool is_on_line(Line tmp_line,Point p){\n\n\tif(p.x >= min(tmp_line.p[0].x,tmp_line.p[1].x) && p.x <= max(tmp_line.p[0].x,tmp_line.p[1].x)){\n\n\t\treturn true;\n\t}else{\n\n\t\treturn false;\n\t}\n}\n\nbool do_cross(Circle C1,Line tmp_line){\n\n\treturn getDistanceSP(tmp_line,C1.center) <= C1.r;\n}\n\nbool dist_check(Circle C[4]){\n\n\tvector<Point> cross_A = getCrossPoints(C[r_ARM],C[l_ARM]);\n\tvector<Point> cross_L = getCrossPoints(C[r_LEG],C[l_LEG]);\n\n\tLine line_A = Line(cross_A[0],cross_A[1]);\n\tLine line_L = Line(cross_L[0],cross_L[1]);\n\n\tif(getDistance(line_A,line_L) <= max_body){\n\n\t\treturn true;\n\t}\n\n\t//腕円の交差範囲が脚円の交差範囲を含む\n\tif(calc_dist(C[r_ARM].center,C[r_LEG].center) <= C[r_ARM].r && calc_dist(C[l_ARM].center,C[r_LEG].center) <= C[l_ARM].r &&\n\t\t\tcalc_dist(C[r_ARM].center,C[l_LEG].center) <= C[r_ARM].r && calc_dist(C[l_ARM].center,C[l_LEG].center) <= C[l_ARM].r){\n\n\t\treturn true;\n\t}\n\n\n\t//脚円の交差範囲が腕円の交差範囲を含む\n\tif(calc_dist(C[r_LEG].center,C[r_ARM].center) <= C[r_LEG].r && calc_dist(C[l_LEG].center,C[r_ARM].center) <= C[l_LEG].r &&\n\t\t\tcalc_dist(C[r_LEG].center,C[l_ARM].center) <= C[r_LEG].r && calc_dist(C[l_LEG].center,C[l_ARM].center) <= C[l_LEG].r){\n\n\t\treturn true;\n\t}\n\n\t//腕円の交差範囲が、脚円の交点を少なくとも1つ含む\n\tif((calc_dist(C[r_ARM].center,cross_L[0]) <= C[r_ARM].r && calc_dist(C[l_ARM].center,cross_L[0]) <= C[l_ARM].r) ||\n\t\t\t(calc_dist(C[r_ARM].center,cross_L[1]) <= C[r_ARM].r && calc_dist(C[l_ARM].center,cross_L[1]) <= C[l_ARM].r)){\n\n\t\treturn true;\n\t}\n\n\t//脚円の交差範囲が、腕円の交点を少なくとも1つ含む\n\tif((calc_dist(C[r_LEG].center,cross_A[0]) <= C[r_LEG].r && calc_dist(C[l_LEG].center,cross_A[0]) <= C[l_LEG].r) ||\n\t\t\t(calc_dist(C[r_LEG].center,cross_A[1]) <= C[r_LEG].r && calc_dist(C[l_LEG].center,cross_A[1]) <= C[l_LEG].r)){\n\n\t\treturn true;\n\t}\n\n\n\tfor(int loop = 0; loop < 4; loop++){\n\n\t\tPoint tmp_p;\n\t\tfor(int i = 0; i < 2; i++){\n\n\t\t\tif(loop == 0){ //脚交点\n\n\t\t\t\ttmp_p = cross_L[i];\n\n\t\t\t}else if(loop == 1){ //腕交点\n\n\t\t\t\ttmp_p = cross_A[i];\n\n\t\t\t}else if(loop == 2){ //脚円中心\n\n\t\t\t\tif(i == 0){\n\n\t\t\t\t\ttmp_p = C[r_LEG].center;\n\n\t\t\t\t}else{\n\n\t\t\t\t\ttmp_p = C[l_LEG].center;\n\t\t\t\t}\n\n\t\t\t}else{ //loop == 3 //腕円中心\n\n\t\t\t\tif(i == 0){\n\n\t\t\t\t\ttmp_p = C[r_ARM].center;\n\n\t\t\t\t}else{\n\n\t\t\t\t\ttmp_p = C[l_ARM].center;\n\t\t\t\t}\n\n\t\t\t}\n\n\n\t\t\tCircle work[2],rest[2];\n\n\t\t\tif(loop%2 == 0){\n\n\t\t\t\twork[0] = C[r_ARM];\n\t\t\t\twork[1] = C[l_ARM];\n\n\t\t\t\trest[0] = C[r_LEG];\n\t\t\t\trest[1] = C[l_LEG];\n\n\t\t\t}else{\n\n\t\t\t\twork[0] = C[r_LEG];\n\t\t\t\twork[1] = C[l_LEG];\n\n\t\t\t\trest[0] = C[r_ARM];\n\t\t\t\trest[1] = C[l_ARM];\n\t\t\t}\n\n\n\t\t\tif(loop < 2){ /*交点と円弧*/\n\n\t\t\t\tvector<Point> vec;\n\n\t\t\t\tif(calc_dist(work[0].center,work[1].center) <= work[0].r){\n\n\t\t\t\t\tvec.push_back(work[0].center);\n\t\t\t\t\tvec.push_back(work[1].center);\n\t\t\t\t}\n\n\t\t\t\tfor(int a = 0; a < 2; a++){ //中心を結ぶ相手円のループ\n\n\t\t\t\t\tLine tmp_line = Line(tmp_p,work[a].center);\n\n\t\t\t\t\tfor(int b = 0; b < 2; b++){ //相手円との交点を見る\n\n\t\t\t\t\t\tif(!do_cross(work[b],tmp_line))continue;\n\t\t\t\t\t\tvector<Point> tmp_cross = getCrossPoints(work[b],tmp_line);\n\n\t\t\t\t\t\tfor(int c = 0; c < tmp_cross.size(); c++){\n\t\t\t\t\t\t\tif(!is_on_line(tmp_line,tmp_cross[c]))continue;\n\n\t\t\t\t\t\t\tif(calc_dist(work[0].center,tmp_cross[c]) > work[0].r+EPS ||\n\t\t\t\t\t\t\t\t\tcalc_dist(work[1].center,tmp_cross[c]) > work[1].r+EPS){\n\n\t\t\t\t\t\t\t\tcontinue;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tvec.push_back(tmp_cross[c]);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tfor(int a = 0; a < vec.size(); a++){\n\t\t\t\t\tif(calc_dist(tmp_p,vec[a]) <= max_body){\n\n\t\t\t\t\t\treturn true;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t}else{ /*円弧と円弧*/\n\n\t\t\t\tfor(int a = 0; a < 2; a++){ //中心を結ぶ相手円のループ\n\n\t\t\t\t\tLine tmp_line = Line(tmp_p,work[a].center);\n\n\t\t\t\t\tvector<Point> FROM,TO;\n\n\t\t\t\t\tif(calc_dist(rest[0].center,rest[1].center) <= rest[0].r){\n\n\t\t\t\t\t\tFROM.push_back(tmp_p);\n\t\t\t\t\t}\n\n\t\t\t\t\tif(calc_dist(work[0].center,work[1].center) <= work[0].r){\n\n\t\t\t\t\t\tTO.push_back(work[a].center);\n\t\t\t\t\t}\n\n\t\t\t\t\tfor(int b = 0; b < 2; b++){ //相手円との交点を見る(相手円の交差範囲により近い交点があるならそちらを採用する)\n\n\t\t\t\t\t\tif(!do_cross(work[b],tmp_line))continue;\n\t\t\t\t\t\tvector<Point> tmp_cross = getCrossPoints(work[b],tmp_line);\n\t\t\t\t\t\tfor(int c = 0; c < tmp_cross.size(); c++){\n\t\t\t\t\t\t\tif(!is_on_line(tmp_line,tmp_cross[c]))continue;\n\n\t\t\t\t\t\t\tif(calc_dist(work[0].center,tmp_cross[c]) > work[0].r+EPS ||\n\t\t\t\t\t\t\t\t\tcalc_dist(work[1].center,tmp_cross[c]) > work[1].r+EPS){\n\n\t\t\t\t\t\t\t\tcontinue;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tTO.push_back(tmp_cross[c]);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\tfor(int b = 0; b < 2; b++){ //自分円との交点を見る(相手円の交差範囲により近い交点があるならそちらを採用する)\n\n\t\t\t\t\t\tif(!do_cross(rest[b],tmp_line))continue;\n\t\t\t\t\t\tvector<Point> tmp_cross = getCrossPoints(rest[b],tmp_line);\n\t\t\t\t\t\tfor(int c = 0; c < tmp_cross.size(); c++){\n\t\t\t\t\t\t\tif(!is_on_line(tmp_line,tmp_cross[c]))continue;\n\n\t\t\t\t\t\t\tif(calc_dist(rest[0].center,tmp_cross[c]) > rest[0].r+EPS ||\n\t\t\t\t\t\t\t\t\tcalc_dist(rest[1].center,tmp_cross[c]) > rest[1].r+EPS){\n\n\t\t\t\t\t\t\t\tcontinue;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tFROM.push_back(tmp_cross[c]);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\tfor(int b = 0; b < FROM.size(); b++){\n\t\t\t\t\t\tfor(int c = 0; c < TO.size(); c++){\n\t\t\t\t\t\t\tif(calc_dist(FROM[b],TO[c]) <= max_body){\n\n\t\t\t\t\t\t\t\treturn true;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\treturn false;\n}\n\nbool isOK(Info info,int rock_index){\n\n\tif(rock_index == info.arm_R || rock_index == info.arm_L || rock_index == info.leg_R || rock_index == info.leg_L){\n\n\t\treturn false;\n\t}else{\n\n\t\treturn true;\n\t}\n}\n\nint main(){\n\n\tscanf(\"%d\",&N);\n\tscanf(\"%lf %lf %lf\",&max_body,&max_arm,&max_leg);\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tscanf(\"%lf %lf\",&rock[i].x,&rock[i].y);\n\t}\n\n\tfor(int a = 0; a < N; a++){\n\t\tfor(int b = 0; b < N; b++){\n\t\t\tfor(int c = 0; c < N; c++){\n\t\t\t\tfor(int d = 0; d < N; d++){\n\n\t\t\t\t\tdp[a][b][c][d] = BIG_NUM; //dp[右腕][左腕][右脚][左脚] = 最小移動回数\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\t//円の交差する範囲\n\tfor(int i = 0; i < N; i++){\n\t\tfor(int k = 0; k < N; k++){\n\t\t\tif(k == i)continue;\n\t\t\tCircle C1,C2;\n\n\t\t\tC1.center = rock[i];\n\t\t\tC2.center = rock[k];\n\n\t\t\t//両腕\n\t\t\tC1.r = max_arm;\n\t\t\tC2.r = max_arm;\n\n\t\t\tif(cross_check(C1,C2)){\n\n\t\t\t\trock_ARM[i].push_back(k);\n\t\t\t}\n\n\t\t\t//両脚\n\t\t\tC1.r = max_leg;\n\t\t\tC2.r = max_leg;\n\n\t\t\tif(cross_check(C1,C2)){\n\n\t\t\t\trock_LEG[i].push_back(k);\n\t\t\t}\n\t\t}\n\t}\n\n\tint goal = N-1;\n\n\tdp[0][1][2][3] = 0;\n\tpriority_queue<Info> Q;\n\n\tQ.push(Info(0,1,2,3,0));\n\n\twhile(!Q.empty()){\n\n\t\tif(Q.top().move_count > dp[Q.top().arm_R][Q.top().arm_L][Q.top().leg_R][Q.top().leg_L]){\n\n\t\t\tQ.pop();\n\n\t\t}else if(Q.top().arm_R == goal || Q.top().arm_L == goal ||\n\t\t\t\tQ.top().leg_R == goal || Q.top().leg_L == goal){\n\n\t\t\tprintf(\"%d\\n\",Q.top().move_count);\n\t\t\treturn 0;\n\n\t\t}else{\n\n\t\t\tInfo info = Q.top();\n\t\t\tQ.pop();\n\n\t\t\tCircle C[4],TMP;\n\n\t\t\t//腕\n\t\t\tC[r_ARM].r = max_arm;\n\t\t\tC[l_ARM].r = max_arm;\n\n\t\t\tC[r_ARM].center = rock[info.arm_R];\n\t\t\tC[l_ARM].center = rock[info.arm_L];\n\n\t\t\t//脚\n\t\t\tC[r_LEG].r = max_leg;\n\t\t\tC[l_LEG].r = max_leg;\n\n\t\t\tC[r_LEG].center = rock[info.leg_R];\n\t\t\tC[l_LEG].center = rock[info.leg_L];\n\n\n\t\t\t//右腕\n\t\t\tTMP = C[r_ARM];\n\n\t\t\tfor(int i = 0; i < rock_ARM[info.arm_L].size(); i++){\n\n\t\t\t\tint next = rock_ARM[info.arm_L][i];\n\t\t\t\tif(!isOK(info,next))continue;\n\n\t\t\t\tC[r_ARM].center = rock[next];\n\n\t\t\t\tif(dp[next][info.arm_L][info.leg_R][info.leg_L] <= info.move_count+1)continue;\n\t\t\t\tif(!dist_check(C))continue;\n\n\t\t\t\tdp[next][info.arm_L][info.leg_R][info.leg_L] = info.move_count+1;\n\n\t\t\t\tQ.push(Info(next,info.arm_L,info.leg_R,info.leg_L,info.move_count+1));\n\t\t\t}\n\t\t\tC[r_ARM] = TMP;\n\n\n\t\t\t//左腕\n\t\t\tTMP = C[l_ARM];\n\n\t\t\tfor(int i = 0; i < rock_ARM[info.arm_R].size(); i++){\n\n\t\t\t\tint next = rock_ARM[info.arm_R][i];\n\t\t\t\tif(!isOK(info,next))continue;\n\n\t\t\t\tC[l_ARM].center = rock[next];\n\n\t\t\t\tif(dp[info.arm_R][next][info.leg_R][info.leg_L] <= info.move_count+1)continue;\n\t\t\t\tif(!dist_check(C))continue;\n\n\t\t\t\tdp[info.arm_R][next][info.leg_R][info.leg_L] = info.move_count+1;\n\n\t\t\t\tQ.push(Info(info.arm_R,next,info.leg_R,info.leg_L,info.move_count+1));\n\t\t\t}\n\t\t\tC[l_ARM] = TMP;\n\n\n\t\t\t//右脚\n\t\t\tTMP = C[r_LEG];\n\n\t\t\tfor(int i = 0; i < rock_LEG[info.leg_L].size(); i++){\n\n\t\t\t\tint next = rock_LEG[info.leg_L][i];\n\t\t\t\tif(!isOK(info,next))continue;\n\n\t\t\t\tC[r_LEG].center = rock[next];\n\n\t\t\t\tif(dp[info.arm_R][info.arm_L][next][info.leg_L] <= info.move_count+1)continue;\n\t\t\t\tif(!dist_check(C))continue;\n\n\t\t\t\tdp[info.arm_R][info.arm_L][next][info.leg_L] = info.move_count+1;\n\n\t\t\t\tQ.push(Info(info.arm_R,info.arm_L,next,info.leg_L,info.move_count+1));\n\t\t\t}\n\t\t\tC[r_LEG] = TMP;\n\n\n\t\t\t//左脚\n\t\t\tTMP = C[l_LEG];\n\n\t\t\tfor(int i = 0; i < rock_LEG[info.leg_R].size(); i++){\n\n\t\t\t\tint next = rock_LEG[info.leg_R][i];\n\t\t\t\tif(!isOK(info,next))continue;\n\n\t\t\t\tC[l_LEG].center = rock[next];\n\n\t\t\t\tif(dp[info.arm_R][info.arm_L][info.leg_R][next] <= info.move_count+1)continue;\n\t\t\t\tif(!dist_check(C))continue;\n\n\t\t\t\tdp[info.arm_R][info.arm_L][info.leg_R][next] = info.move_count+1;\n\n\t\t\t\tQ.push(Info(info.arm_R,info.arm_L,info.leg_R,next,info.move_count+1));\n\t\t\t}\n\t\t\tC[l_LEG] = TMP;\n\t\t}\n\t}\n\n\tprintf(\"-1\\n\");\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 1860, "memory_kb": 18100, "score_of_the_acc": -0.5059, "final_rank": 3 }, { "submission_id": "aoj_2227_4903530", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 1000000000000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n#define SIZE 35\n\nenum Type{\n\tr_ARM,\n\tl_ARM,\n\tr_LEG,\n\tl_LEG,\n};\n\nstruct Point{\n\tPoint(double arg_x,double arg_y){\n\t\tx = arg_x;\n\t\ty = arg_y;\n\t}\n\n\tPoint(){\n\t\tx = y = 0.0;\n\t}\n\n\tPoint operator + (Point p){ return Point(x+p.x,y+p.y); }\n\tPoint operator - (Point p){ return Point(x-p.x,y-p.y);}\n\tPoint operator * (double a){ return Point(a*x,a*y); }\n\tPoint operator / (double a){ return Point(x/a,y/a); }\n\n\tdouble abs(){ return sqrt(norm()); }\n\tdouble norm(){ return x*x + y*y; }\n\n\tbool operator<(const Point &p) const{\n\n\t\tif(fabs(x-p.x) > EPS){\n\n\t\t\treturn x < p.x;\n\t\t}else{\n\n\t\t\treturn y < p.y;\n\t\t}\n\t}\n\n\tbool operator == (const Point &p) const{\n\t\treturn fabs(x-p.x) < EPS && fabs(y-p.y) < EPS;\n\t}\n\n\n\tvoid debug(){\n\n\t\tprintf(\"(%.3lf,%.3lf)\\n\",x,y);\n\t}\n\n\tdouble x,y;\n};\n\ntypedef Point Vector;\ntypedef vector<Point> Polygon;\n\nstruct Line{\n\tLine(){\n\n\t}\n\tLine(Point a,Point b){\n\t\tp[0] = a;\n\t\tp[1] = b;\n\t}\n\t/*void outPut(){\n\n\t\tprintf(\"(%.3lf,%.3lf)-(%.3lf,%.3lf)\\n\",p[0].x,p[0].y,p[1].x,p[1].y);\n\t}*/\n\tPoint p[2];\n};\n\nstruct Circle{\n\tPoint center;\n\tdouble r;\n};\n\nstruct Info{\n\tInfo(){\n\t\t\tarm_R = arm_L = leg_R = leg_L = move_count = 0;\n\t}\n\n\tInfo(int arg_arm_R,int arg_arm_L,int arg_leg_R,int arg_leg_L,int arg_move_count){\n\t\tarm_R = arg_arm_R;\n\t\tarm_L = arg_arm_L;\n\t\tleg_R = arg_leg_R;\n\t\tleg_L = arg_leg_L;\n\t\tmove_count = arg_move_count;\n\t}\n\tbool operator<(const struct Info &arg) const{\n\n\t\treturn move_count > arg.move_count; //移動回数の昇順(PQ)\n\t}\n\n\tint arm_R,arm_L,leg_R,leg_L,move_count;\n};\n\nint N;\nint dp[SIZE][SIZE][SIZE][SIZE];\ndouble max_body,max_arm,max_leg;\nvector<int> rock_ARM[SIZE],rock_LEG[SIZE];\nPoint rock[SIZE];\n\n\n\ndouble calc_dist(Point A,Point B){\n\n\treturn sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y));\n}\n\ndouble norm(Vector a){\n\treturn a.x*a.x+a.y*a.y;\n}\n\ndouble abs(Vector a){\n\treturn sqrt(norm(a));\n}\n\ndouble cross(Vector a,Vector b){\n return a.x*b.y-a.y*b.x;\n}\n\ndouble dot(Vector a,Vector b){\n return a.x*b.x + a.y*b.y;\n}\n\nstatic const int COUNTER_CLOCKWISE = 1;\nstatic const int CLOCKWISE = -1;\nstatic const int ONLINE_BACK = 2;\nstatic const int ONLINE_FRONT = -2;\nstatic const int ON_SEGMENT = 0;\n\nint ccw(Point p0,Point p1,Point p2){\n\n\tVector a = p1 - p0;\n\tVector b = p2 - p0;\n\n\tif(cross(a,b) > EPS)return COUNTER_CLOCKWISE;\n\tif(cross(a,b) < -EPS)return CLOCKWISE;\n\tif(dot(a,b) < -EPS)return ONLINE_BACK;\n\tif(a.norm() < b.norm())return ONLINE_FRONT;\n\n\treturn ON_SEGMENT;\n}\n\ndouble arg(Vector p){\n\treturn atan2(p.y,p.x);\n}\n\nVector polar(double a,double r){\n\treturn Point(cos(r)*a,sin(r)*a);\n}\n\n//円と円の交点\nvector<Point> getCrossPoints(Circle c1,Circle c2){\n\n\tvector<Point> res;\n\n\tdouble d = abs(c1.center-c2.center);\n\tdouble a = acos((c1.r*c1.r+d*d-c2.r*c2.r)/(2*c1.r*d));\n\tdouble t = arg(c2.center-c1.center);\n\n\tres.push_back(Point(c1.center+polar(c1.r,t+a)));\n\tres.push_back(Point(c1.center+polar(c1.r,t-a)));\n\n\treturn res;\n}\n\nPoint project(Line l,Point p){\n\n\tVector base = l.p[1]-l.p[0];\n\tdouble r = dot(p-l.p[0],base)/norm(base);\n\treturn l.p[0]+base*r;\n}\n\n//円と直線の交点を求める関数\nvector<Point> getCrossPoints(Circle c,Line l){\n\n vector<Point> ret;\n\n Vector pr = project(l,c.center);\n Vector e = (l.p[1]-l.p[0])/abs(l.p[1]-l.p[0]);\n\n double base;\n\n if(fabs(c.r*c.r-norm(pr-c.center)) < EPS){\n\n base = 0;\n }else{\n base = sqrt(c.r*c.r-norm(pr-c.center));\n }\n\n ret.push_back(Point(pr+e*base));\n ret.push_back(Point(pr-e*base));\n\n return ret;\n}\n\nint func(double x1,double y1,double x2, double y2, double xp, double yp){\n\tdouble naiseki,norm1,norm2,gaiseki;\n\tnorm1 = sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));\n\tnorm2 = sqrt((xp-x1)*(xp-x1)+(yp-y1)*(yp-y1));\n\tnaiseki = (xp-x1)*(x2-x1)+(yp-y1)*(y2-y1);\n\tgaiseki = (x2-x1)*(yp-y1)-(xp-x1)*(y2-y1);\n\tif(gaiseki > EPS){\n\t\treturn 1;\n\t}else if(gaiseki < -EPS){\n\t\treturn -1;\n\t}\n\tif(naiseki < -EPS){\n\t\treturn 2;\n\t}\n\n\tif(norm1 < norm2){\n\t\treturn -2;\n\t}\n\treturn 0;\n}\n\n\n//★★直線ではなく、線分の交差判定★★\nbool is_Cross(Line a,Line b){\n\n\tif(func(a.p[0].x,a.p[0].y,a.p[1].x,a.p[1].y,b.p[0].x,b.p[0].y)*\n\t\t\tfunc(a.p[0].x,a.p[0].y,a.p[1].x,a.p[1].y,b.p[1].x,b.p[1].y) <= 0 &&\n\t\t\tfunc(b.p[0].x,b.p[0].y,b.p[1].x,b.p[1].y,a.p[0].x,a.p[0].y)*\n\t\t\tfunc(b.p[0].x,b.p[0].y,b.p[1].x,b.p[1].y,a.p[1].x,a.p[1].y) <= 0){\n\t\treturn true;\n\t}\n\treturn false;\n}\n\nPoint calc_minus(Point a,Point b){\n Point ret;\n\n ret.x = a.x-b.x;\n ret.y = a.y-b.y;\n\n return ret;\n}\n\ndouble calc_len(Vector a){\n return sqrt(a.x*a.x+a.y*a.y);\n}\n\n//線分ではなく直線と点の距離\ndouble getDistanceLP(Line l,Point p){\n return fabs(cross(calc_minus(l.p[1],l.p[0]),calc_minus(p,l.p[0]))/calc_len(calc_minus(l.p[1],l.p[0])));\n}\n\n//点と線分の距離\ndouble getDistanceSP(Line l,Point p){\n\tif(dot(calc_minus(l.p[1],l.p[0]),calc_minus(p,l.p[0])) < 0.0)return calc_len(calc_minus(p,l.p[0]));\n\tif(dot(calc_minus(l.p[0],l.p[1]),calc_minus(p,l.p[1])) < 0.0)return calc_len(calc_minus(p,l.p[1]));\n\treturn getDistanceLP(l,p);\n}\n\n//線分と線分の距離\ndouble getDistance(Line A,Line B){\n\tif(is_Cross(A,B))return 0.0;\n\treturn min(min(getDistanceSP(A,B.p[0]),getDistanceSP(A,B.p[1])),\n\t\t\tmin(getDistanceSP(B,A.p[0]),getDistanceSP(B,A.p[1])));\n}\n\nbool cross_check(Circle A,Circle B){\n\n\tdouble tmp_dist = calc_dist(A.center,B.center);\n\n\tif(tmp_dist > A.r+B.r+EPS){\n\n\t\treturn false;\n\t}else{\n\n\t\treturn true;\n\t}\n}\n\nbool is_on_line(Line tmp_line,Point p){\n\n\tif(p.x >= min(tmp_line.p[0].x,tmp_line.p[1].x) && p.x <= max(tmp_line.p[0].x,tmp_line.p[1].x)){\n\n\t\treturn true;\n\t}else{\n\n\t\treturn false;\n\t}\n}\n\nbool do_cross(Circle C1,Line tmp_line){\n\n\treturn getDistanceSP(tmp_line,C1.center) <= C1.r;\n}\n\nbool dist_check(Circle C[4]){\n\n\tvector<Point> cross_A = getCrossPoints(C[r_ARM],C[l_ARM]);\n\tvector<Point> cross_L = getCrossPoints(C[r_LEG],C[l_LEG]);\n\n\t/*for(int a = 0; a < cross_A.size(); a++){\n\t\tif(calc_dist(cross_A[a],C[r_ARM].center) > C[r_ARM].r+EPS){\n\n\t\t\tprintf(\"★★バグ★★\\n\");\n\t\t}\n\t\tif(calc_dist(cross_A[a],C[l_ARM].center) > C[l_ARM].r+EPS){\n\n\t\t\tprintf(\"★★バグ★★\\n\");\n\t\t}\n\t}\n\n\tfor(int a = 0; a < cross_L.size(); a++){\n\t\tif(calc_dist(cross_L[a],C[r_LEG].center) > C[r_LEG].r+EPS){\n\n\t\t\tprintf(\"★★バグ★★\\n\");\n\t\t}\n\t\tif(calc_dist(cross_L[a],C[l_LEG].center) > C[l_LEG].r+EPS){\n\n\t\t\tprintf(\"★★バグ★★\\n\");\n\t\t}\n\t}\n*/\n\n\n\tLine line_A = Line(cross_A[0],cross_A[1]);\n\tLine line_L = Line(cross_L[0],cross_L[1]);\n\n\tif(getDistance(line_A,line_L) <= max_body){\n\n\t\treturn true;\n\t}\n\n\t//腕円の交差範囲が脚円の交差範囲を含む\n\tif(calc_dist(C[r_ARM].center,C[r_LEG].center) <= C[r_ARM].r && calc_dist(C[l_ARM].center,C[r_LEG].center) <= C[l_ARM].r &&\n\t\t\tcalc_dist(C[r_ARM].center,C[l_LEG].center) <= C[r_ARM].r && calc_dist(C[l_ARM].center,C[l_LEG].center) <= C[l_ARM].r){\n\n\t\treturn true;\n\t}\n\n\n\t//脚円の交差範囲が腕円の交差範囲を含む\n\tif(calc_dist(C[r_LEG].center,C[r_ARM].center) <= C[r_LEG].r && calc_dist(C[l_LEG].center,C[r_ARM].center) <= C[l_LEG].r &&\n\t\t\tcalc_dist(C[r_LEG].center,C[l_ARM].center) <= C[r_LEG].r && calc_dist(C[l_LEG].center,C[l_ARM].center) <= C[l_LEG].r){\n\n\t\treturn true;\n\t}\n\n\t//腕円の交差範囲が、脚円の交点を少なくとも1つ含む\n\tif((calc_dist(C[r_ARM].center,cross_L[0]) <= C[r_ARM].r && calc_dist(C[l_ARM].center,cross_L[0]) <= C[l_ARM].r) ||\n\t\t\t(calc_dist(C[r_ARM].center,cross_L[1]) <= C[r_ARM].r && calc_dist(C[l_ARM].center,cross_L[1]) <= C[l_ARM].r)){\n\n\t\treturn true;\n\t}\n\n\t//脚円の交差範囲が、腕円の交点を少なくとも1つ含む\n\tif((calc_dist(C[r_LEG].center,cross_A[0]) <= C[r_LEG].r && calc_dist(C[l_LEG].center,cross_A[0]) <= C[l_LEG].r) ||\n\t\t\t(calc_dist(C[r_LEG].center,cross_A[1]) <= C[r_LEG].r && calc_dist(C[l_LEG].center,cross_A[1]) <= C[l_LEG].r)){\n\n\t\treturn true;\n\t}\n\n\n\tfor(int loop = 0; loop < 4; loop++){\n\n\t\tPoint tmp_p;\n\t\tfor(int i = 0; i < 2; i++){\n\n\t\t\tif(loop == 0){ //脚交点\n\n\t\t\t\ttmp_p = cross_L[i];\n\n\t\t\t}else if(loop == 1){ //腕交点\n\n\t\t\t\ttmp_p = cross_A[i];\n\n\t\t\t}else if(loop == 2){ //脚円中心\n\n\t\t\t\tif(i == 0){\n\n\t\t\t\t\ttmp_p = C[r_LEG].center;\n\n\t\t\t\t}else{\n\n\t\t\t\t\ttmp_p = C[l_LEG].center;\n\t\t\t\t}\n\n\t\t\t}else{ //loop == 3 //腕円中心\n\n\t\t\t\tif(i == 0){\n\n\t\t\t\t\ttmp_p = C[r_ARM].center;\n\n\t\t\t\t}else{\n\n\t\t\t\t\ttmp_p = C[l_ARM].center;\n\t\t\t\t}\n\n\t\t\t}\n\n\n\t\t\tCircle work[2],rest[2];\n\n\t\t\tif(loop%2 == 0){\n\n\t\t\t\twork[0] = C[r_ARM];\n\t\t\t\twork[1] = C[l_ARM];\n\n\t\t\t\trest[0] = C[r_LEG];\n\t\t\t\trest[1] = C[l_LEG];\n\n\t\t\t}else{\n\n\t\t\t\twork[0] = C[r_LEG];\n\t\t\t\twork[1] = C[l_LEG];\n\n\t\t\t\trest[0] = C[r_ARM];\n\t\t\t\trest[1] = C[l_ARM];\n\t\t\t}\n\n\n\t\t\tif(loop < 2){ /*交点と円弧*/\n\n\t\t\t\tvector<Point> vec;\n\n\t\t\t\tif(calc_dist(work[0].center,work[1].center) <= work[0].r){\n\n\t\t\t\t\tvec.push_back(work[0].center);\n\t\t\t\t\tvec.push_back(work[1].center);\n\t\t\t\t}\n\n\t\t\t\tfor(int a = 0; a < 2; a++){ //中心を結ぶ相手円のループ\n\n\t\t\t\t\tLine tmp_line = Line(tmp_p,work[a].center);\n\n\t\t\t\t\tfor(int b = 0; b < 2; b++){ //相手円との交点を見る\n\n\t\t\t\t\t\tif(!do_cross(work[b],tmp_line))continue;\n\t\t\t\t\t\tvector<Point> tmp_cross = getCrossPoints(work[b],tmp_line);\n\n\t\t\t\t\t\tfor(int c = 0; c < tmp_cross.size(); c++){\n\t\t\t\t\t\t\tif(!is_on_line(tmp_line,tmp_cross[c]))continue;\n\n\t\t\t\t\t\t\tif(calc_dist(work[0].center,tmp_cross[c]) > work[0].r+EPS ||\n\t\t\t\t\t\t\t\t\tcalc_dist(work[1].center,tmp_cross[c]) > work[1].r+EPS){\n\n\t\t\t\t\t\t\t\tcontinue;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tvec.push_back(tmp_cross[c]);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tfor(int a = 0; a < vec.size(); a++){\n\t\t\t\t\tif(calc_dist(tmp_p,vec[a]) <= max_body){\n\n\t\t\t\t\t\treturn true;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t}else{ /*円弧と円弧*/\n\n\t\t\t\tfor(int a = 0; a < 2; a++){ //中心を結ぶ相手円のループ\n\n\t\t\t\t\tLine tmp_line = Line(tmp_p,work[a].center);\n\n\t\t\t\t\tvector<Point> FROM,TO;\n\n\t\t\t\t\tif(calc_dist(rest[0].center,rest[1].center) <= rest[0].r){\n\n\t\t\t\t\t\tFROM.push_back(tmp_p);\n\t\t\t\t\t}\n\n\t\t\t\t\tif(calc_dist(work[0].center,work[1].center) <= work[0].r){\n\n\t\t\t\t\t\tTO.push_back(work[a].center);\n\t\t\t\t\t}\n\n\t\t\t\t\tfor(int b = 0; b < 2; b++){ //相手円との交点を見る\n\n\t\t\t\t\t\tif(!do_cross(work[b],tmp_line))continue;\n\t\t\t\t\t\tvector<Point> tmp_cross = getCrossPoints(work[b],tmp_line);\n\t\t\t\t\t\tfor(int c = 0; c < tmp_cross.size(); c++){\n\t\t\t\t\t\t\tif(!is_on_line(tmp_line,tmp_cross[c]))continue;\n\n\t\t\t\t\t\t\tif(calc_dist(work[0].center,tmp_cross[c]) > work[0].r+EPS ||\n\t\t\t\t\t\t\t\t\tcalc_dist(work[1].center,tmp_cross[c]) > work[1].r+EPS){\n\n\t\t\t\t\t\t\t\tcontinue;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tTO.push_back(tmp_cross[c]);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\tfor(int b = 0; b < 2; b++){ //自分円との交点を見る\n\n\t\t\t\t\t\tif(!do_cross(rest[b],tmp_line))continue;\n\t\t\t\t\t\tvector<Point> tmp_cross = getCrossPoints(rest[b],tmp_line);\n\t\t\t\t\t\tfor(int c = 0; c < tmp_cross.size(); c++){\n\t\t\t\t\t\t\tif(!is_on_line(tmp_line,tmp_cross[c]))continue;\n\n\t\t\t\t\t\t\tif(calc_dist(rest[0].center,tmp_cross[c]) > rest[0].r+EPS ||\n\t\t\t\t\t\t\t\t\tcalc_dist(rest[1].center,tmp_cross[c]) > rest[1].r+EPS){\n\n\t\t\t\t\t\t\t\tcontinue;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tFROM.push_back(tmp_cross[c]);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\tfor(int b = 0; b < FROM.size(); b++){\n\t\t\t\t\t\tfor(int c = 0; c < TO.size(); c++){\n\t\t\t\t\t\t\tif(calc_dist(FROM[b],TO[c]) <= max_body){\n\n\t\t\t\t\t\t\t\treturn true;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\treturn false;\n}\n\nbool isOK(Info info,int rock_index){\n\n\tif(rock_index == info.arm_R || rock_index == info.arm_L || rock_index == info.leg_R || rock_index == info.leg_L){\n\n\t\treturn false;\n\t}else{\n\n\t\treturn true;\n\t}\n}\n\nint main(){\n\n\tscanf(\"%d\",&N);\n\tscanf(\"%lf %lf %lf\",&max_body,&max_arm,&max_leg);\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tscanf(\"%lf %lf\",&rock[i].x,&rock[i].y);\n\t}\n\n\tfor(int a = 0; a < N; a++){\n\t\tfor(int b = 0; b < N; b++){\n\t\t\tfor(int c = 0; c < N; c++){\n\t\t\t\tfor(int d = 0; d < N; d++){\n\n\t\t\t\t\tdp[a][b][c][d] = BIG_NUM; //dp[右腕][左腕][右脚][左脚] = 最小移動回数\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\t//円の交差する範囲\n\tfor(int i = 0; i < N; i++){\n\t\tfor(int k = 0; k < N; k++){\n\t\t\tif(k == i)continue;\n\t\t\tCircle C1,C2;\n\n\t\t\tC1.center = rock[i];\n\t\t\tC2.center = rock[k];\n\n\t\t\t//両腕\n\t\t\tC1.r = max_arm;\n\t\t\tC2.r = max_arm;\n\n\t\t\tif(cross_check(C1,C2)){\n\n\t\t\t\trock_ARM[i].push_back(k);\n\t\t\t}\n\n\t\t\t//両脚\n\t\t\tC1.r = max_leg;\n\t\t\tC2.r = max_leg;\n\n\t\t\tif(cross_check(C1,C2)){\n\n\t\t\t\trock_LEG[i].push_back(k);\n\t\t\t}\n\t\t}\n\t}\n\n\tint goal = N-1;\n\n\tdp[0][1][2][3] = 0;\n\tpriority_queue<Info> Q;\n\n\tQ.push(Info(0,1,2,3,0));\n\n\twhile(!Q.empty()){\n\n\t\tif(Q.top().move_count > dp[Q.top().arm_R][Q.top().arm_L][Q.top().leg_R][Q.top().leg_L]){\n\n\t\t\tQ.pop();\n\n\t\t}else if(Q.top().arm_R == goal || Q.top().arm_L == goal ||\n\t\t\t\tQ.top().leg_R == goal || Q.top().leg_L == goal){\n\n\t\t\tprintf(\"%d\\n\",Q.top().move_count);\n\t\t\treturn 0;\n\n\t\t}else{\n\n\t\t\tInfo info = Q.top();\n\t\t\tQ.pop();\n\n\t\t\t//printf(\"count:%d (%d,%d,%d,%d)\\n\",info.move_count,info.arm_R,info.arm_L,info.leg_R,info.leg_L);\n\n\t\t\tCircle C[4],TMP;\n\n\t\t\t//腕\n\t\t\tC[r_ARM].r = max_arm;\n\t\t\tC[l_ARM].r = max_arm;\n\n\t\t\tC[r_ARM].center = rock[info.arm_R];\n\t\t\tC[l_ARM].center = rock[info.arm_L];\n\n\t\t\t//脚\n\t\t\tC[r_LEG].r = max_leg;\n\t\t\tC[l_LEG].r = max_leg;\n\n\t\t\tC[r_LEG].center = rock[info.leg_R];\n\t\t\tC[l_LEG].center = rock[info.leg_L];\n\n\n\t\t\t//右腕\n\t\t\tTMP = C[r_ARM];\n\n\t\t\tfor(int i = 0; i < rock_ARM[info.arm_L].size(); i++){\n\n\t\t\t\tint next = rock_ARM[info.arm_L][i];\n\t\t\t\tif(!isOK(info,next))continue;\n\n\t\t\t\tC[r_ARM].center = rock[next];\n\n\t\t\t\tif(dp[next][info.arm_L][info.leg_R][info.leg_L] <= info.move_count+1)continue;\n\t\t\t\tif(!dist_check(C))continue;\n\n\t\t\t\tdp[next][info.arm_L][info.leg_R][info.leg_L] = info.move_count+1;\n\n\t\t\t\tQ.push(Info(next,info.arm_L,info.leg_R,info.leg_L,info.move_count+1));\n\t\t\t}\n\t\t\tC[r_ARM] = TMP;\n\n\n\t\t\t//左腕\n\t\t\tTMP = C[l_ARM];\n\n\t\t\tfor(int i = 0; i < rock_ARM[info.arm_R].size(); i++){\n\n\t\t\t\tint next = rock_ARM[info.arm_R][i];\n\t\t\t\tif(!isOK(info,next))continue;\n\n\t\t\t\tC[l_ARM].center = rock[next];\n\n\t\t\t\tif(dp[info.arm_R][next][info.leg_R][info.leg_L] <= info.move_count+1)continue;\n\t\t\t\tif(!dist_check(C))continue;\n\n\t\t\t\tdp[info.arm_R][next][info.leg_R][info.leg_L] = info.move_count+1;\n\n\t\t\t\tQ.push(Info(info.arm_R,next,info.leg_R,info.leg_L,info.move_count+1));\n\t\t\t}\n\t\t\tC[l_ARM] = TMP;\n\n\n\t\t\t//右脚\n\t\t\tTMP = C[r_LEG];\n\n\t\t\tfor(int i = 0; i < rock_LEG[info.leg_L].size(); i++){\n\n\t\t\t\tint next = rock_LEG[info.leg_L][i];\n\t\t\t\tif(!isOK(info,next))continue;\n\n\t\t\t\tC[r_LEG].center = rock[next];\n\n\t\t\t\tif(dp[info.arm_R][info.arm_L][next][info.leg_L] <= info.move_count+1)continue;\n\t\t\t\tif(!dist_check(C))continue;\n\n\t\t\t\tdp[info.arm_R][info.arm_L][next][info.leg_L] = info.move_count+1;\n\n\t\t\t\tQ.push(Info(info.arm_R,info.arm_L,next,info.leg_L,info.move_count+1));\n\t\t\t}\n\t\t\tC[r_LEG] = TMP;\n\n\n\t\t\t//左脚\n\t\t\tTMP = C[l_LEG];\n\n\t\t\tfor(int i = 0; i < rock_LEG[info.leg_R].size(); i++){\n\n\t\t\t\tint next = rock_LEG[info.leg_R][i];\n\t\t\t\tif(!isOK(info,next))continue;\n\n\t\t\t\tC[l_LEG].center = rock[next];\n\n\t\t\t\tif(dp[info.arm_R][info.arm_L][info.leg_R][next] <= info.move_count+1)continue;\n\t\t\t\tif(!dist_check(C))continue;\n\n\t\t\t\tdp[info.arm_R][info.arm_L][info.leg_R][next] = info.move_count+1;\n\n\t\t\t\tQ.push(Info(info.arm_R,info.arm_L,info.leg_R,next,info.move_count+1));\n\t\t\t}\n\t\t\tC[l_LEG] = TMP;\n\t\t}\n\t}\n\n\tprintf(\"-1\\n\");\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 1860, "memory_kb": 18340, "score_of_the_acc": -0.5119, "final_rank": 4 }, { "submission_id": "aoj_2227_4903415", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 1000000000000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n#define SIZE 35\n\nenum Type{\n\tr_ARM,\n\tl_ARM,\n\tr_LEG,\n\tl_LEG,\n};\n\nstruct Point{\n\tPoint(double arg_x,double arg_y){\n\t\tx = arg_x;\n\t\ty = arg_y;\n\t}\n\n\tPoint(){\n\t\tx = y = 0.0;\n\t}\n\n\tPoint operator + (Point p){ return Point(x+p.x,y+p.y); }\n\tPoint operator - (Point p){ return Point(x-p.x,y-p.y);}\n\tPoint operator * (double a){ return Point(a*x,a*y); }\n\tPoint operator / (double a){ return Point(x/a,y/a); }\n\n\tdouble abs(){ return sqrt(norm()); }\n\tdouble norm(){ return x*x + y*y; }\n\n\tbool operator<(const Point &p) const{\n\n\t\tif(fabs(x-p.x) > EPS){\n\n\t\t\treturn x < p.x;\n\t\t}else{\n\n\t\t\treturn y < p.y;\n\t\t}\n\t}\n\n\tbool operator == (const Point &p) const{\n\t\treturn fabs(x-p.x) < EPS && fabs(y-p.y) < EPS;\n\t}\n\n\n\tvoid debug(){\n\n\t\tprintf(\"(%.3lf,%.3lf)\\n\",x,y);\n\t}\n\n\tdouble x,y;\n};\n\ntypedef Point Vector;\ntypedef vector<Point> Polygon;\n\nstruct Line{\n\tLine(){\n\n\t}\n\tLine(Point a,Point b){\n\t\tp[0] = a;\n\t\tp[1] = b;\n\t}\n\t/*void outPut(){\n\n\t\tprintf(\"(%.3lf,%.3lf)-(%.3lf,%.3lf)\\n\",p[0].x,p[0].y,p[1].x,p[1].y);\n\t}*/\n\tPoint p[2];\n};\n\nstruct Circle{\n\tPoint center;\n\tdouble r;\n};\n\nstruct Info{\n\tInfo(){\n\t\t\tarm_R = arm_L = leg_R = leg_L = move_count = 0;\n\t}\n\n\tInfo(int arg_arm_R,int arg_arm_L,int arg_leg_R,int arg_leg_L,int arg_move_count){\n\t\tarm_R = arg_arm_R;\n\t\tarm_L = arg_arm_L;\n\t\tleg_R = arg_leg_R;\n\t\tleg_L = arg_leg_L;\n\t\tmove_count = arg_move_count;\n\t}\n\tbool operator<(const struct Info &arg) const{\n\n\t\treturn move_count > arg.move_count; //移動回数の昇順(PQ)\n\t}\n\n\tint arm_R,arm_L,leg_R,leg_L,move_count;\n};\n\nint N;\nint dp[SIZE][SIZE][SIZE][SIZE];\ndouble max_body,max_arm,max_leg;\nvector<int> rock_ARM[SIZE],rock_LEG[SIZE];\nPoint rock[SIZE];\n\n\n\ndouble calc_dist(Point A,Point B){\n\n\treturn sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y));\n}\n\ndouble norm(Vector a){\n\treturn a.x*a.x+a.y*a.y;\n}\n\ndouble abs(Vector a){\n\treturn sqrt(norm(a));\n}\n\ndouble cross(Vector a,Vector b){\n return a.x*b.y-a.y*b.x;\n}\n\ndouble dot(Vector a,Vector b){\n return a.x*b.x + a.y*b.y;\n}\n\nstatic const int COUNTER_CLOCKWISE = 1;\nstatic const int CLOCKWISE = -1;\nstatic const int ONLINE_BACK = 2;\nstatic const int ONLINE_FRONT = -2;\nstatic const int ON_SEGMENT = 0;\n\nint ccw(Point p0,Point p1,Point p2){\n\n\tVector a = p1 - p0;\n\tVector b = p2 - p0;\n\n\tif(cross(a,b) > EPS)return COUNTER_CLOCKWISE;\n\tif(cross(a,b) < -EPS)return CLOCKWISE;\n\tif(dot(a,b) < -EPS)return ONLINE_BACK;\n\tif(a.norm() < b.norm())return ONLINE_FRONT;\n\n\treturn ON_SEGMENT;\n}\n\ndouble arg(Vector p){\n\treturn atan2(p.y,p.x);\n}\n\nVector polar(double a,double r){\n\treturn Point(cos(r)*a,sin(r)*a);\n}\n\n//円と円の交点\nvector<Point> getCrossPoints(Circle c1,Circle c2){\n\n\tvector<Point> res;\n\n\tdouble d = abs(c1.center-c2.center);\n\tdouble a = acos((c1.r*c1.r+d*d-c2.r*c2.r)/(2*c1.r*d));\n\tdouble t = arg(c2.center-c1.center);\n\n\tres.push_back(Point(c1.center+polar(c1.r,t+a)));\n\tres.push_back(Point(c1.center+polar(c1.r,t-a)));\n\n\treturn res;\n}\n\nPoint project(Line l,Point p){\n\n\tVector base = l.p[1]-l.p[0];\n\tdouble r = dot(p-l.p[0],base)/norm(base);\n\treturn l.p[0]+base*r;\n}\n\n//円と直線の交点を求める関数\nvector<Point> getCrossPoints(Circle c,Line l){\n\n vector<Point> ret;\n\n Vector pr = project(l,c.center);\n Vector e = (l.p[1]-l.p[0])/abs(l.p[1]-l.p[0]);\n\n double base;\n\n if(fabs(c.r*c.r-norm(pr-c.center)) < EPS){\n\n base = 0;\n }else{\n base = sqrt(c.r*c.r-norm(pr-c.center));\n }\n\n ret.push_back(Point(pr+e*base));\n ret.push_back(Point(pr-e*base));\n\n return ret;\n}\n\nint func(double x1,double y1,double x2, double y2, double xp, double yp){\n\tdouble naiseki,norm1,norm2,gaiseki;\n\tnorm1 = sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));\n\tnorm2 = sqrt((xp-x1)*(xp-x1)+(yp-y1)*(yp-y1));\n\tnaiseki = (xp-x1)*(x2-x1)+(yp-y1)*(y2-y1);\n\tgaiseki = (x2-x1)*(yp-y1)-(xp-x1)*(y2-y1);\n\tif(gaiseki > EPS){\n\t\treturn 1;\n\t}else if(gaiseki < -EPS){\n\t\treturn -1;\n\t}\n\tif(naiseki < -EPS){\n\t\treturn 2;\n\t}\n\n\tif(norm1 < norm2){\n\t\treturn -2;\n\t}\n\treturn 0;\n}\n\n\n//★★直線ではなく、線分の交差判定★★\nbool is_Cross(Line a,Line b){\n\n\tif(func(a.p[0].x,a.p[0].y,a.p[1].x,a.p[1].y,b.p[0].x,b.p[0].y)*\n\t\t\tfunc(a.p[0].x,a.p[0].y,a.p[1].x,a.p[1].y,b.p[1].x,b.p[1].y) <= 0 &&\n\t\t\tfunc(b.p[0].x,b.p[0].y,b.p[1].x,b.p[1].y,a.p[0].x,a.p[0].y)*\n\t\t\tfunc(b.p[0].x,b.p[0].y,b.p[1].x,b.p[1].y,a.p[1].x,a.p[1].y) <= 0){\n\t\treturn true;\n\t}\n\treturn false;\n}\n\nPoint calc_minus(Point a,Point b){\n Point ret;\n\n ret.x = a.x-b.x;\n ret.y = a.y-b.y;\n\n return ret;\n}\n\ndouble calc_len(Vector a){\n return sqrt(a.x*a.x+a.y*a.y);\n}\n\n//線分ではなく直線と点の距離\ndouble getDistanceLP(Line l,Point p){\n return fabs(cross(calc_minus(l.p[1],l.p[0]),calc_minus(p,l.p[0]))/calc_len(calc_minus(l.p[1],l.p[0])));\n}\n\n//点と線分の距離\ndouble getDistanceSP(Line l,Point p){\n\tif(dot(calc_minus(l.p[1],l.p[0]),calc_minus(p,l.p[0])) < 0.0)return calc_len(calc_minus(p,l.p[0]));\n\tif(dot(calc_minus(l.p[0],l.p[1]),calc_minus(p,l.p[1])) < 0.0)return calc_len(calc_minus(p,l.p[1]));\n\treturn getDistanceLP(l,p);\n}\n\n//線分と線分の距離\ndouble getDistance(Line A,Line B){\n\tif(is_Cross(A,B))return 0.0;\n\treturn min(min(getDistanceSP(A,B.p[0]),getDistanceSP(A,B.p[1])),\n\t\t\tmin(getDistanceSP(B,A.p[0]),getDistanceSP(B,A.p[1])));\n}\n\nbool cross_check(Circle A,Circle B){\n\n\tdouble tmp_dist = calc_dist(A.center,B.center);\n\n\tif(tmp_dist > A.r+B.r+EPS){\n\n\t\treturn false;\n\t}else{\n\n\t\treturn true;\n\t}\n}\n\nbool is_on_line(Line tmp_line,Point p){\n\n\tif(p.x >= min(tmp_line.p[0].x,tmp_line.p[1].x) && p.x <= max(tmp_line.p[0].x,tmp_line.p[1].x)){\n\n\t\treturn true;\n\t}else{\n\n\t\treturn false;\n\t}\n}\n\nbool do_cross(Circle C1,Line tmp_line){\n\n\treturn getDistanceSP(tmp_line,C1.center) <= C1.r;\n}\n\nbool dist_check(Circle C[4]){\n\n\tvector<Point> cross_A = getCrossPoints(C[r_ARM],C[l_ARM]);\n\tvector<Point> cross_L = getCrossPoints(C[r_LEG],C[l_LEG]);\n\n\t/*for(int a = 0; a < cross_A.size(); a++){\n\t\tif(calc_dist(cross_A[a],C[r_ARM].center) > C[r_ARM].r+EPS){\n\n\t\t\tprintf(\"★★バグ★★\\n\");\n\t\t}\n\t\tif(calc_dist(cross_A[a],C[l_ARM].center) > C[l_ARM].r+EPS){\n\n\t\t\tprintf(\"★★バグ★★\\n\");\n\t\t}\n\t}\n\n\tfor(int a = 0; a < cross_L.size(); a++){\n\t\tif(calc_dist(cross_L[a],C[r_LEG].center) > C[r_LEG].r+EPS){\n\n\t\t\tprintf(\"★★バグ★★\\n\");\n\t\t}\n\t\tif(calc_dist(cross_L[a],C[l_LEG].center) > C[l_LEG].r+EPS){\n\n\t\t\tprintf(\"★★バグ★★\\n\");\n\t\t}\n\t}\n*/\n\n\n\tLine line_A = Line(cross_A[0],cross_A[1]);\n\tLine line_L = Line(cross_L[0],cross_L[1]);\n\n\tif(getDistance(line_A,line_L) <= max_body){\n\n\t\treturn true;\n\t}\n\n\t//腕円の交差範囲が脚円の交差範囲を含む\n\tif(calc_dist(C[r_ARM].center,C[r_LEG].center) <= C[r_ARM].r && calc_dist(C[l_ARM].center,C[r_LEG].center) <= C[l_ARM].r &&\n\t\t\tcalc_dist(C[r_ARM].center,C[l_LEG].center) <= C[r_ARM].r && calc_dist(C[l_ARM].center,C[l_LEG].center) <= C[l_ARM].r){\n\n\t\treturn true;\n\t}\n\n\t//脚円の交差範囲が腕円の交差範囲を含む\n\tif(calc_dist(C[r_LEG].center,C[r_ARM].center) <= C[r_LEG].r && calc_dist(C[l_LEG].center,C[r_ARM].center) <= C[l_LEG].r &&\n\t\t\tcalc_dist(C[r_LEG].center,C[l_ARM].center) <= C[r_LEG].r && calc_dist(C[l_LEG].center,C[l_ARM].center) <= C[l_LEG].r){\n\n\t\treturn true;\n\t}\n\n\n\tfor(int loop = 0; loop < 4; loop++){\n\n\t\tPoint tmp_p;\n\t\tfor(int i = 0; i < 2; i++){\n\n\t\t\tif(loop == 0){ //脚交点\n\n\t\t\t\ttmp_p = cross_L[i];\n\n\t\t\t}else if(loop == 1){ //腕交点\n\n\t\t\t\ttmp_p = cross_A[i];\n\n\t\t\t}else if(loop == 2){ //脚円中心\n\n\t\t\t\tif(i == 0){\n\n\t\t\t\t\ttmp_p = C[r_LEG].center;\n\n\t\t\t\t}else{\n\n\t\t\t\t\ttmp_p = C[l_LEG].center;\n\t\t\t\t}\n\n\t\t\t}else{ //loop == 3 //腕円中心\n\n\t\t\t\tif(i == 0){\n\n\t\t\t\t\ttmp_p = C[r_ARM].center;\n\n\t\t\t\t}else{\n\n\t\t\t\t\ttmp_p = C[l_ARM].center;\n\t\t\t\t}\n\n\t\t\t}\n\n\n\t\t\tCircle work[2],rest[2];\n\n\t\t\tif(loop%2 == 0){\n\n\t\t\t\twork[0] = C[r_ARM];\n\t\t\t\twork[1] = C[l_ARM];\n\n\t\t\t\trest[0] = C[r_LEG];\n\t\t\t\trest[1] = C[l_LEG];\n\n\t\t\t}else{\n\n\t\t\t\twork[0] = C[r_LEG];\n\t\t\t\twork[1] = C[l_LEG];\n\n\t\t\t\trest[0] = C[r_ARM];\n\t\t\t\trest[1] = C[l_ARM];\n\t\t\t}\n\n\n\t\t\tif(loop < 2){ /*交点と円弧*/\n\n\t\t\t\tvector<Point> vec;\n\n\t\t\t\tif(calc_dist(work[0].center,work[1].center) <= work[0].r){\n\n\t\t\t\t\tvec.push_back(work[0].center);\n\t\t\t\t\tvec.push_back(work[1].center);\n\t\t\t\t}\n\n\t\t\t\tfor(int a = 0; a < 2; a++){ //中心を結ぶ相手円のループ\n\n\t\t\t\t\tLine tmp_line = Line(tmp_p,work[a].center);\n\n\t\t\t\t\tfor(int b = 0; b < 2; b++){ //相手円との交点を見る\n\n\t\t\t\t\t\tif(!do_cross(work[b],tmp_line))continue;\n\t\t\t\t\t\tvector<Point> tmp_cross = getCrossPoints(work[b],tmp_line);\n\n\t\t\t\t\t\tfor(int c = 0; c < tmp_cross.size(); c++){\n\t\t\t\t\t\t\tif(!is_on_line(tmp_line,tmp_cross[c]))continue;\n\n\t\t\t\t\t\t\tif(calc_dist(work[0].center,tmp_cross[c]) > work[0].r+EPS ||\n\t\t\t\t\t\t\t\t\tcalc_dist(work[1].center,tmp_cross[c]) > work[1].r+EPS){\n\n\t\t\t\t\t\t\t\tcontinue;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tvec.push_back(tmp_cross[c]);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tfor(int a = 0; a < vec.size(); a++){\n\t\t\t\t\tif(calc_dist(tmp_p,vec[a]) <= max_body){\n\n\t\t\t\t\t\treturn true;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t}else{ /*円弧と円弧*/\n\n\t\t\t\tfor(int a = 0; a < 2; a++){ //中心を結ぶ相手円のループ\n\n\t\t\t\t\tLine tmp_line = Line(tmp_p,work[a].center);\n\n\t\t\t\t\tvector<Point> FROM,TO;\n\n\t\t\t\t\tif(calc_dist(rest[0].center,rest[1].center) <= rest[0].r){\n\n\t\t\t\t\t\tFROM.push_back(tmp_p);\n\t\t\t\t\t}\n\n\t\t\t\t\tif(calc_dist(work[0].center,work[1].center) <= work[0].r){\n\n\t\t\t\t\t\tTO.push_back(work[a].center);\n\t\t\t\t\t}\n\n\t\t\t\t\tfor(int b = 0; b < 2; b++){ //相手円との交点を見る\n\n\t\t\t\t\t\tif(!do_cross(work[b],tmp_line))continue;\n\t\t\t\t\t\tvector<Point> tmp_cross = getCrossPoints(work[b],tmp_line);\n\t\t\t\t\t\tfor(int c = 0; c < tmp_cross.size(); c++){\n\t\t\t\t\t\t\tif(!is_on_line(tmp_line,tmp_cross[c]))continue;\n\n\t\t\t\t\t\t\tif(calc_dist(work[0].center,tmp_cross[c]) > work[0].r+EPS ||\n\t\t\t\t\t\t\t\t\tcalc_dist(work[1].center,tmp_cross[c]) > work[1].r+EPS){\n\n\t\t\t\t\t\t\t\tcontinue;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tTO.push_back(tmp_cross[c]);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\tfor(int b = 0; b < 2; b++){ //自分円との交点を見る\n\n\t\t\t\t\t\tif(!do_cross(rest[b],tmp_line))continue;\n\t\t\t\t\t\tvector<Point> tmp_cross = getCrossPoints(rest[b],tmp_line);\n\t\t\t\t\t\tfor(int c = 0; c < tmp_cross.size(); c++){\n\t\t\t\t\t\t\tif(!is_on_line(tmp_line,tmp_cross[c]))continue;\n\n\t\t\t\t\t\t\tif(calc_dist(rest[0].center,tmp_cross[c]) > rest[0].r+EPS ||\n\t\t\t\t\t\t\t\t\tcalc_dist(rest[1].center,tmp_cross[c]) > rest[1].r+EPS){\n\n\t\t\t\t\t\t\t\tcontinue;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tFROM.push_back(tmp_cross[c]);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\tfor(int b = 0; b < FROM.size(); b++){\n\t\t\t\t\t\tfor(int c = 0; c < TO.size(); c++){\n\t\t\t\t\t\t\tif(calc_dist(FROM[b],TO[c]) <= max_body){\n\n\t\t\t\t\t\t\t\treturn true;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\treturn false;\n}\n\nbool isOK(Info info,int rock_index){\n\n\tif(rock_index == info.arm_R || rock_index == info.arm_L || rock_index == info.leg_R || rock_index == info.leg_L){\n\n\t\treturn false;\n\t}else{\n\n\t\treturn true;\n\t}\n}\n\nint main(){\n\n\tscanf(\"%d\",&N);\n\tscanf(\"%lf %lf %lf\",&max_body,&max_arm,&max_leg);\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tscanf(\"%lf %lf\",&rock[i].x,&rock[i].y);\n\t}\n\n\tfor(int a = 0; a < N; a++){\n\t\tfor(int b = 0; b < N; b++){\n\t\t\tfor(int c = 0; c < N; c++){\n\t\t\t\tfor(int d = 0; d < N; d++){\n\n\t\t\t\t\tdp[a][b][c][d] = BIG_NUM; //dp[右腕][左腕][右脚][左脚] = 最小移動回数\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\t//円の交差する範囲\n\tfor(int i = 0; i < N; i++){\n\t\tfor(int k = 0; k < N; k++){\n\t\t\tif(k == i)continue;\n\t\t\tCircle C1,C2;\n\n\t\t\tC1.center = rock[i];\n\t\t\tC2.center = rock[k];\n\n\t\t\t//両腕\n\t\t\tC1.r = max_arm;\n\t\t\tC2.r = max_arm;\n\n\t\t\tif(cross_check(C1,C2)){\n\n\t\t\t\trock_ARM[i].push_back(k);\n\t\t\t}\n\n\t\t\t//両脚\n\t\t\tC1.r = max_leg;\n\t\t\tC2.r = max_leg;\n\n\t\t\tif(cross_check(C1,C2)){\n\n\t\t\t\trock_LEG[i].push_back(k);\n\t\t\t}\n\t\t}\n\t}\n\n\tint goal = N-1;\n\n\tdp[0][1][2][3] = 0;\n\tpriority_queue<Info> Q;\n\n\tQ.push(Info(0,1,2,3,0));\n\n\twhile(!Q.empty()){\n\n\t\tif(Q.top().move_count > dp[Q.top().arm_R][Q.top().arm_L][Q.top().leg_R][Q.top().leg_L]){\n\n\t\t\tQ.pop();\n\n\t\t}else if(Q.top().arm_R == goal || Q.top().arm_L == goal ||\n\t\t\t\tQ.top().leg_R == goal || Q.top().leg_L == goal){\n\n\t\t\tprintf(\"%d\\n\",Q.top().move_count);\n\t\t\treturn 0;\n\n\t\t}else{\n\n\t\t\tInfo info = Q.top();\n\t\t\tQ.pop();\n\n\t\t\t//printf(\"count:%d (%d,%d,%d,%d)\\n\",info.move_count,info.arm_R,info.arm_L,info.leg_R,info.leg_L);\n\n\t\t\tCircle C[4],TMP;\n\n\t\t\t//腕\n\t\t\tC[r_ARM].r = max_arm;\n\t\t\tC[l_ARM].r = max_arm;\n\n\t\t\tC[r_ARM].center = rock[info.arm_R];\n\t\t\tC[l_ARM].center = rock[info.arm_L];\n\n\t\t\t//脚\n\t\t\tC[r_LEG].r = max_leg;\n\t\t\tC[l_LEG].r = max_leg;\n\n\t\t\tC[r_LEG].center = rock[info.leg_R];\n\t\t\tC[l_LEG].center = rock[info.leg_L];\n\n\n\t\t\t//右腕\n\t\t\tTMP = C[r_ARM];\n\n\t\t\tfor(int i = 0; i < rock_ARM[info.arm_L].size(); i++){\n\n\t\t\t\tint next = rock_ARM[info.arm_L][i];\n\t\t\t\tif(!isOK(info,next))continue;\n\n\t\t\t\tC[r_ARM].center = rock[next];\n\n\t\t\t\tif(dp[next][info.arm_L][info.leg_R][info.leg_L] <= info.move_count+1)continue;\n\t\t\t\tif(!dist_check(C))continue;\n\n\t\t\t\tdp[next][info.arm_L][info.leg_R][info.leg_L] = info.move_count+1;\n\n\t\t\t\tQ.push(Info(next,info.arm_L,info.leg_R,info.leg_L,info.move_count+1));\n\t\t\t}\n\t\t\tC[r_ARM] = TMP;\n\n\n\t\t\t//左腕\n\t\t\tTMP = C[l_ARM];\n\n\t\t\tfor(int i = 0; i < rock_ARM[info.arm_R].size(); i++){\n\n\t\t\t\tint next = rock_ARM[info.arm_R][i];\n\t\t\t\tif(!isOK(info,next))continue;\n\n\t\t\t\tC[l_ARM].center = rock[next];\n\n\t\t\t\tif(dp[info.arm_R][next][info.leg_R][info.leg_L] <= info.move_count+1)continue;\n\t\t\t\tif(!dist_check(C))continue;\n\n\t\t\t\tdp[info.arm_R][next][info.leg_R][info.leg_L] = info.move_count+1;\n\n\t\t\t\tQ.push(Info(info.arm_R,next,info.leg_R,info.leg_L,info.move_count+1));\n\t\t\t}\n\t\t\tC[l_ARM] = TMP;\n\n\n\t\t\t//右脚\n\t\t\tTMP = C[r_LEG];\n\n\t\t\tfor(int i = 0; i < rock_LEG[info.leg_L].size(); i++){\n\n\t\t\t\tint next = rock_LEG[info.leg_L][i];\n\t\t\t\tif(!isOK(info,next))continue;\n\n\t\t\t\tC[r_LEG].center = rock[next];\n\n\t\t\t\tif(dp[info.arm_R][info.arm_L][next][info.leg_L] <= info.move_count+1)continue;\n\t\t\t\tif(!dist_check(C))continue;\n\n\t\t\t\tdp[info.arm_R][info.arm_L][next][info.leg_L] = info.move_count+1;\n\n\t\t\t\tQ.push(Info(info.arm_R,info.arm_L,next,info.leg_L,info.move_count+1));\n\t\t\t}\n\t\t\tC[r_LEG] = TMP;\n\n\n\t\t\t//左脚\n\t\t\tTMP = C[l_LEG];\n\n\t\t\tfor(int i = 0; i < rock_LEG[info.leg_R].size(); i++){\n\n\t\t\t\tint next = rock_LEG[info.leg_R][i];\n\t\t\t\tif(!isOK(info,next))continue;\n\n\t\t\t\tC[l_LEG].center = rock[next];\n\n\t\t\t\tif(dp[info.arm_R][info.arm_L][info.leg_R][next] <= info.move_count+1)continue;\n\t\t\t\tif(!dist_check(C))continue;\n\n\t\t\t\tdp[info.arm_R][info.arm_L][info.leg_R][next] = info.move_count+1;\n\n\t\t\t\tQ.push(Info(info.arm_R,info.arm_L,info.leg_R,next,info.move_count+1));\n\t\t\t}\n\t\t\tC[l_LEG] = TMP;\n\t\t}\n\t}\n\n\tprintf(\"-1\\n\");\n\n\treturn 0;\n}", "accuracy": 0.64, "time_ms": 1870, "memory_kb": 18324, "score_of_the_acc": -0.513, "final_rank": 10 }, { "submission_id": "aoj_2227_2126698", "code_snippet": "#include <cstdio>\n#include <cstring>\n#include <cassert>\n#include <vector>\n#include <queue>\n#include <complex>\n#include <algorithm>\nusing namespace std;\n\n#define EQ(x, y) (abs((x)-(y)) < EPS)\n#define EPS 1e-8\n#define INF 0x33433433\n\n#define X real()\n#define Y imag()\n\n//typedef float double;\ntypedef pair<int, int> Pair;\ntypedef complex<double> Point;\ntypedef Point Vec;\n\nnamespace std {\n bool operator<(Point a, Point b) {\n if (a.X != b.X) return a.X < b.X;\n return a.Y < b.Y;\n }\n}\n\nclass Line : public vector<Point> {\npublic:\n Line () {resize(2);}\n Line (Point a, Point b) {\n push_back(a);\n push_back(b);\n }\n};\n\nstruct Circle {\n Point c;\n double r;\n};\n\ndouble dot(Point a, Point b) {\n return (conj(a)*b).X;\n}\n\ndouble cross(Point a, Point b) {\n return (conj(a)*b).Y;\n}\n\nint ccw(Point a, Point b, Point c) {\n b -= a;\n c -= a;\n if (cross(b, c) > 0) return 1;\n if (cross(b, c) < 0) return -1;\n if (dot(b, c) < 0) return 2;\n if (norm(b) < norm(c)) return -2;\n return 0;\n}\n\nPoint proj(Line l, Point p) {\n Point v = l[1]-l[0];\n double ratio = dot(v, p-l[0]) / norm(v);\n return l[0] + ratio*v;\n}\n\ndouble DistLP(Line l, Point p) {\n return abs(p - proj(l, p));\n}\n\nvector<Point> CrossPointCL(Circle c, Line l){\n vector<Point> ret;\n double d = DistLP(l, c.c);\n if(d < c.r + EPS){\n double ratio = (d > c.r) ? 0.0 : sqrt(c.r*c.r - d*d);\n Point sgn = (l[0] - l[1]) / abs(l[0] - l[1]);\n ret.push_back(proj(l, c.c) + ratio * sgn);\n ret.push_back(proj(l, c.c) - ratio * sgn);\n }\n\n return ret;\n}\n\nvector<Point> CrossPointCC(Circle c1, Circle c2){\n vector<Point> ret;\n double d = abs(c1.c - c2.c);\n double rc = (d*d + c1.r*c1.r - c2.r*c2.r) / (2*d);\n double dfr = c1.r*c1.r - rc*rc;\n\n if (EQ(dfr, 0.0)) dfr = 0.0;\n else if(dfr < 0.0) return ret;\n\n double rs = sqrt(dfr);\n Point sgn = (c2.c - c1.c) / d;\n ret.push_back(c1.c + sgn*Point(rc, rs));\n if (dfr > 0.0) ret.push_back(c1.c + sgn*Point(rc, -rs));\n return ret;\n}\n\nint n;\ndouble A, B, C;\nPoint rocks[50];\nint mind[11451419];\nbool considered[30][30][30][30];\nbool result[30][30][30][30];\n\nvoid NormalizeNum(int *nums) {\n if (nums[0] > nums[1]) swap(nums[0], nums[1]);\n if (nums[2] > nums[3]) swap(nums[2], nums[3]);\n}\n\nint CalcState(int *nums, bool *is_goal) {\n int state = 0;\n for (int i=0; i<4; i++) {\n state = state*31 + nums[i];\n if (nums[i] == n && is_goal) *is_goal = true;\n }\n return state;\n}\n\nvoid State2Num(int state, int *nums) {\n for (int i=0; i<4; i++) {\n nums[3-i] = state%31;\n state /= 31;\n }\n}\n\nbool IsPossible(int a, int b, int c, int d) {\n if (considered[a][b][c][d]) return result[a][b][c][d];\n considered[a][b][c][d] = true;\n\n Circle cs[] = {{rocks[a], A}, {rocks[b], A}, {rocks[c], C}, {rocks[d], C}};\n vector<Point> cc1 = CrossPointCC(cs[0], cs[1]);\n vector<Point> cc2 = CrossPointCC(cs[2], cs[3]);\n if (cc1.empty()) return false;\n if (cc2.empty()) return false;\n\n vector<Point> rough_cand1(cc1);\n vector<Point> rough_cand2(cc2);\n for (int i=0; i<2; i++) {\n for (int j=2; j<4; j++) {\n Line l(cs[i].c, cs[j].c);\n for (int k=0; k<2; k++) {\n vector<Point> cl = CrossPointCL(cs[k], l);\n rough_cand1.insert(rough_cand1.end(), cl.begin(), cl.end());\n rough_cand2.insert(rough_cand2.end(), cl.begin(), cl.end());\n }\n\n for (int k=2; k<4; k++) {\n vector<Point> cl = CrossPointCL(cs[k], l);\n rough_cand2.insert(rough_cand2.end(), cl.begin(), cl.end());\n }\n }\n }\n\n for (int i=0; i<2; i++) {\n for (Point p2 : cc2) {\n Line l(cs[i].c, p2);\n for (int j=0; j<2; j++) {\n vector<Point> cl1 = CrossPointCL(cs[j], l);\n rough_cand1.insert(rough_cand1.end(), cl1.begin(), cl1.end());\n }\n }\n }\n\n for (int i=2; i<4; i++) {\n for (Point p1 : cc1) {\n Line l(cs[i].c, p1);\n for (int k=2; k<4; k++) {\n vector<Point> cl2 = CrossPointCL(cs[k], l);\n rough_cand2.insert(rough_cand2.end(), cl2.begin(), cl2.end());\n }\n }\n }\n \n vector<Point> cand1;\n vector<Point> cand2;\n\n for (Point p1 : rough_cand1) {\n if (abs(p1-cs[0].c) >= cs[0].r + EPS) continue;\n if (abs(p1-cs[1].c) >= cs[1].r + EPS) continue;\n cand1.push_back(p1);\n }\n\n for (Point p2 : rough_cand2) {\n if (abs(p2-cs[2].c) >= cs[2].r + EPS) continue;\n if (abs(p2-cs[3].c) >= cs[3].r + EPS) continue;\n cand2.push_back(p2);\n }\n\n sort(cand1.begin(), cand1.end());\n cand1.erase(unique(cand1.begin(), cand1.end()), cand1.end());\n sort(cand2.begin(), cand2.end());\n cand2.erase(unique(cand2.begin(), cand2.end()), cand2.end());\n for (Point p1 : cand1) {\n for (Point p2 : cand2) {\n if (abs(p1-p2) < B+EPS) return result[a][b][c][d] = true;\n }\n }\n\n return result[a][b][c][d] = false;\n}\n\nint main() {\n scanf(\"%d\", &n);\n scanf(\"%lf%lf%lf\", &A, &B, &C);\n swap(A, B);\n for (int i=0; i<n; i++) {\n double x, y;\n scanf(\"%lf%lf\", &x, &y);\n rocks[i] = Point(x, y);\n }\n\n int ans = INF;\n queue<int> q;\n fill(mind, mind+11451419, INF);\n for (int i=0; i<4; i++) {\n int nums[] = {1, 2, 3, 4};\n\n nums[i] = 0;\n NormalizeNum(nums); \n \n bool is_goal = false;\n int state = CalcState(nums, &is_goal); \n mind[state] = 0;\n q.push(state);\n if (is_goal) {\n puts(\"0\");\n return 0;\n }\n }\n\n while (!q.empty()) {\n int state = q.front(); q.pop();\n int nums[4];\n State2Num(state, nums);\n\n if (nums[0] == 0) {\n int b = nums[1]-1;\n int c = nums[2]-1;\n int d = nums[3]-1;\n\n for (int a=0; a<n; a++) {\n if (b == a || c == a || d == a) continue;\n\n if (IsPossible(a, b, c, d)) {\n //printf(\"%d %d %d %d\\n\", a, b, c, d);\n for (int j=1; j<4; j++) {\n int nums2[4];\n\n memcpy(nums2, nums, sizeof(nums2));\n nums2[0] = a+1;\n nums2[j] = 0;\n NormalizeNum(nums2);\n\n bool is_goal = false;\n int next_state = CalcState(nums2, &is_goal);\n if (is_goal) {\n printf(\"%d\\n\", mind[state]+1);\n return 0;\n }\n\n if (mind[next_state] > mind[state]+1) {\n mind[next_state] = mind[state]+1;\n q.push(next_state);\n }\n }\n }\n }\n } else {\n int a = nums[0]-1;\n int b = nums[1]-1;\n int d = nums[3]-1;\n\n for (int c=0; c<n; c++) {\n if (a == c || b == c || d == c) continue;\n\n if (IsPossible(a, b, c, d)) {\n //printf(\"%d %d %d %d\\n\", a, b, c, d);\n for (int j=0; j<4; j++) {\n if (j == 2) continue;\n\n int nums2[4];\n\n memcpy(nums2, nums, sizeof(nums2));\n nums2[2] = c+1;\n nums2[j] = 0;\n NormalizeNum(nums2);\n\n bool is_goal = false;\n int next_state = CalcState(nums2, &is_goal);\n if (is_goal) {\n printf(\"%d\\n\", mind[state]+1);\n return 0;\n }\n\n if (mind[next_state] > mind[state]+1) {\n mind[next_state] = mind[state]+1;\n q.push(next_state);\n }\n }\n }\n }\n }\n }\n\n puts(\"-1\");\n}", "accuracy": 1, "time_ms": 6450, "memory_kb": 49164, "score_of_the_acc": -1.999, "final_rank": 6 }, { "submission_id": "aoj_2227_2126412", "code_snippet": "#include <cstdio>\n#include <cstring>\n#include <cassert>\n#include <vector>\n#include <queue>\n#include <complex>\n#include <algorithm>\nusing namespace std;\n\n#define EQ(x, y) (abs((x)-(y)) < EPS)\n#define EPS 1e-8\n#define INF 0x33433433\n\n#define X real()\n#define Y imag()\n\ntypedef pair<int, int> Pair;\ntypedef complex<double> Point;\ntypedef Point Vec;\n\nnamespace std {\n bool operator<(Point a, Point b) {\n if (a.X != b.X) return a.X < b.X;\n return a.Y < b.Y;\n }\n}\n\nclass Line : public vector<Point> {\npublic:\n Line () {resize(2);}\n Line (Point a, Point b) {\n push_back(a);\n push_back(b);\n }\n};\n\nstruct Circle {\n Point c;\n double r;\n};\n\ndouble dot(Point a, Point b) {\n return (conj(a)*b).X;\n}\n\ndouble cross(Point a, Point b) {\n return (conj(a)*b).Y;\n}\n\nint ccw(Point a, Point b, Point c) {\n b -= a;\n c -= a;\n if (cross(b, c) > 0) return 1;\n if (cross(b, c) < 0) return -1;\n if (dot(b, c) < 0) return 2;\n if (norm(b) < norm(c)) return -2;\n return 0;\n}\n\nPoint proj(Line l, Point p) {\n Point v = l[1]-l[0];\n double ratio = dot(v, p-l[0]) / norm(v);\n return l[0] + ratio*v;\n}\n\ndouble DistLP(Line l, Point p) {\n return abs(p - proj(l, p));\n}\n\nvector<Point> CrossPointCL(Circle c, Line l){\n vector<Point> ret;\n double d = DistLP(l, c.c);\n if(d < c.r + EPS){\n double ratio = (d > c.r) ? 0.0 : sqrt(c.r*c.r - d*d);\n Point sgn = (l[0] - l[1]) / abs(l[0] - l[1]);\n ret.push_back(proj(l, c.c) + ratio * sgn);\n ret.push_back(proj(l, c.c) - ratio * sgn);\n }\n\n return ret;\n}\n\nvector<Point> CrossPointCC(Circle c1, Circle c2){\n vector<Point> ret;\n double d = abs(c1.c - c2.c);\n double rc = (d*d + c1.r*c1.r - c2.r*c2.r) / (2*d);\n double dfr = c1.r*c1.r - rc*rc;\n\n if (EQ(dfr, 0.0)) dfr = 0.0;\n else if(dfr < 0.0) return ret;\n\n double rs = sqrt(dfr);\n Point sgn = (c2.c - c1.c) / d;\n ret.push_back(c1.c + sgn*Point(rc, rs));\n if (dfr > 0.0) ret.push_back(c1.c + sgn*Point(rc, -rs));\n return ret;\n}\n\nint n;\ndouble A, B, C;\nPoint rocks[50];\nint mind[11451419];\nbool considered[30][30][30][30];\nbool result[30][30][30][30];\n\nvoid NormalizeNum(int *nums) {\n if (nums[0] > nums[1]) swap(nums[0], nums[1]);\n if (nums[2] > nums[3]) swap(nums[2], nums[3]);\n}\n\nint CalcState(int *nums, bool *is_goal) {\n int state = 0;\n for (int i=0; i<4; i++) {\n state = state*31 + nums[i];\n if (nums[i] == n && is_goal) *is_goal = true;\n }\n return state;\n}\n\nvoid State2Num(int state, int *nums) {\n for (int i=0; i<4; i++) {\n nums[3-i] = state%31;\n state /= 31;\n }\n}\n\nbool IsPossible(int a, int b, int c, int d) {\n if (considered[a][b][c][d]) return result[a][b][c][d];\n considered[a][b][c][d] = true;\n\n Circle cs[] = {{rocks[a], A}, {rocks[b], A}, {rocks[c], C}, {rocks[d], C}};\n vector<Point> cc1 = CrossPointCC(cs[0], cs[1]);\n vector<Point> cc2 = CrossPointCC(cs[2], cs[3]);\n if (cc1.empty()) return false;\n if (cc2.empty()) return false;\n\n vector<Point> rough_cand1(cc1);\n vector<Point> rough_cand2(cc2);\n for (int i=0; i<2; i++) {\n for (int j=2; j<4; j++) {\n Line l(cs[i].c, cs[j].c);\n vector<Point> cl1 = CrossPointCL(cs[i], l);\n vector<Point> cl2 = CrossPointCL(cs[j], l);\n\n rough_cand1.insert(rough_cand1.end(), cl1.begin(), cl1.end());\n rough_cand2.insert(rough_cand2.end(), cl2.begin(), cl2.end());\n }\n }\n\n for (int i=0; i<2; i++) {\n for (Point p2 : cc2) {\n Line l(cs[i].c, p2);\n vector<Point> cl1 = CrossPointCL(cs[i], l);\n rough_cand1.insert(rough_cand1.end(), cl1.begin(), cl1.end());\n }\n }\n\n for (int j=2; j<4; j++) {\n for (Point p1 : cc1) {\n Line l(cs[j].c, p1);\n vector<Point> cl2 = CrossPointCL(cs[j], l);\n rough_cand2.insert(rough_cand2.end(), cl2.begin(), cl2.end());\n }\n }\n \n vector<Point> cand1;\n vector<Point> cand2;\n\n for (Point p1 : rough_cand1) {\n if (abs(p1-cs[0].c) >= cs[0].r + EPS) continue;\n if (abs(p1-cs[1].c) >= cs[1].r + EPS) continue;\n cand1.push_back(p1);\n }\n\n for (Point p2 : rough_cand2) {\n if (abs(p2-cs[2].c) >= cs[2].r + EPS) continue;\n if (abs(p2-cs[3].c) >= cs[3].r + EPS) continue;\n cand2.push_back(p2);\n }\n\n sort(cand1.begin(), cand1.end());\n cand1.erase(unique(cand1.begin(), cand1.end()), cand1.end());\n sort(cand2.begin(), cand2.end());\n cand2.erase(unique(cand2.begin(), cand2.end()), cand2.end());\n for (Point p1 : cand1) {\n for (Point p2 : cand2) {\n if (abs(p1-p2) < B+EPS) return result[a][b][c][d] = true;\n }\n }\n\n return result[a][b][c][d] = false;\n}\n\nint main() {\n scanf(\"%d\", &n);\n scanf(\"%lf%lf%lf\", &A, &B, &C);\n swap(A, B);\n for (int i=0; i<n; i++) {\n double x, y;\n scanf(\"%lf%lf\", &x, &y);\n rocks[i] = Point(x, y);\n }\n\n int ans = INF;\n queue<int> q;\n fill(mind, mind+11451419, INF);\n for (int i=0; i<4; i++) {\n int nums[] = {1, 2, 3, 4};\n\n nums[i] = 0;\n NormalizeNum(nums); \n \n bool is_goal = false;\n int state = CalcState(nums, &is_goal); \n mind[state] = 0;\n q.push(state);\n if (is_goal) {\n puts(\"0\");\n return 0;\n }\n }\n\n while (!q.empty()) {\n int state = q.front(); q.pop();\n int nums[4];\n State2Num(state, nums);\n\n if (nums[0] == 0) {\n int b = nums[1]-1;\n int c = nums[2]-1;\n int d = nums[3]-1;\n\n for (int a=0; a<n; a++) {\n if (b == a || c == a || d == a) continue;\n\n if (IsPossible(a, b, c, d)) {\n //printf(\"%d %d %d %d\\n\", a, b, c, d);\n for (int j=1; j<4; j++) {\n int nums2[4];\n\n memcpy(nums2, nums, sizeof(nums2));\n nums2[0] = a+1;\n nums2[j] = 0;\n NormalizeNum(nums2);\n\n bool is_goal = false;\n int next_state = CalcState(nums2, &is_goal);\n if (is_goal) {\n printf(\"%d\\n\", mind[state]+1);\n return 0;\n }\n\n if (mind[next_state] > mind[state]+1) {\n mind[next_state] = mind[state]+1;\n q.push(next_state);\n }\n }\n }\n }\n } else {\n int a = nums[0]-1;\n int b = nums[1]-1;\n int d = nums[3]-1;\n\n for (int c=0; c<n; c++) {\n if (a == c || b == c || d == c) continue;\n\n if (IsPossible(a, b, c, d)) {\n //printf(\"%d %d %d %d\\n\", a, b, c, d);\n for (int j=0; j<4; j++) {\n if (j == 2) continue;\n\n int nums2[4];\n\n memcpy(nums2, nums, sizeof(nums2));\n nums2[2] = c+1;\n nums2[j] = 0;\n NormalizeNum(nums2);\n\n bool is_goal = false;\n int next_state = CalcState(nums2, &is_goal);\n if (is_goal) {\n printf(\"%d\\n\", mind[state]+1);\n return 0;\n }\n\n if (mind[next_state] > mind[state]+1) {\n mind[next_state] = mind[state]+1;\n q.push(next_state);\n }\n }\n }\n }\n }\n }\n\n puts(\"-1\");\n}", "accuracy": 0.64, "time_ms": 2900, "memory_kb": 49204, "score_of_the_acc": -1.4444, "final_rank": 14 }, { "submission_id": "aoj_2227_2126375", "code_snippet": "#include <cstdio>\n#include <cstring>\n#include <cassert>\n#include <vector>\n#include <queue>\n#include <complex>\n#include <algorithm>\nusing namespace std;\n\n#define EQ(x, y) (abs((x)-(y)) < EPS)\n#define EPS 1e-8\n#define INF 0x33433433\n\n#define X real()\n#define Y imag()\n\ntypedef pair<int, int> Pair;\ntypedef complex<double> Point;\ntypedef Point Vec;\n\nint n;\ndouble A, B, C;\nPoint rocks[50];\nint mind[11451419];\nbool considered[30][30][30][30];\nbool result[30][30][30][30];\n\nclass Line : public vector<Point> {\npublic:\n Line () {resize(2);}\n Line (Point a, Point b) {\n push_back(a);\n push_back(b);\n }\n};\n\nstruct Circle {\n Point c;\n double r;\n};\n\ndouble dot(Point a, Point b) {\n return (conj(a)*b).X;\n}\n\ndouble cross(Point a, Point b) {\n return (conj(a)*b).Y;\n}\n\nint ccw(Point a, Point b, Point c) {\n b -= a;\n c -= a;\n if (cross(b, c) > 0) return 1;\n if (cross(b, c) < 0) return -1;\n if (dot(b, c) < 0) return 2;\n if (norm(b) < norm(c)) return -2;\n return 0;\n}\n\nPoint proj(Line l, Point p) {\n Point v = l[1]-l[0];\n double ratio = dot(v, p-l[0]) / norm(v);\n return l[0] + ratio*v;\n}\n\ndouble DistLP(Line l, Point p) {\n return abs(p - proj(l, p));\n}\n\nvector<Point> CrossPointCL(Circle c, Line l){\n vector<Point> ret;\n double d = DistLP(l, c.c);\n if(d < c.r + EPS){\n double ratio = (d > c.r) ? 0.0 : sqrt(c.r*c.r - d*d);\n Point sgn = (l[0] - l[1]) / abs(l[0] - l[1]);\n ret.push_back(proj(l, c.c) + ratio * sgn);\n ret.push_back(proj(l, c.c) - ratio * sgn);\n }\n\n return ret;\n}\n\nvector<Point> CrossPointCC(Circle c1, Circle c2){\n vector<Point> ret;\n double d = abs(c1.c - c2.c);\n double rc = (d*d + c1.r*c1.r - c2.r*c2.r) / (2*d);\n double dfr = c1.r*c1.r - rc*rc;\n\n if (EQ(dfr, 0.0)) dfr = 0.0;\n else if(dfr < 0.0) return ret;\n\n double rs = sqrt(dfr);\n Point sgn = (c2.c - c1.c) / d;\n ret.push_back(c1.c + sgn*Point(rc, rs));\n if (dfr > 0.0) ret.push_back(c1.c + sgn*Point(rc, -rs));\n return ret;\n}\n\nvoid NormalizeNum(int *nums) {\n if (nums[0] > nums[1]) swap(nums[0], nums[1]);\n if (nums[2] > nums[3]) swap(nums[2], nums[3]);\n}\n\nint CalcState(int *nums, bool *is_goal) {\n int state = 0;\n for (int i=0; i<4; i++) {\n state = state*31 + nums[i];\n if (nums[i] == n && is_goal) *is_goal = true;\n }\n return state;\n}\n\nvoid State2Num(int state, int *nums) {\n for (int i=0; i<4; i++) {\n nums[3-i] = state%31;\n state /= 31;\n }\n}\n\nbool IsPossible(int a, int b, int c, int d) {\n if (considered[a][b][c][d]) return result[a][b][c][d];\n considered[a][b][c][d] = true;\n\n Circle cs[] = {{rocks[a], A}, {rocks[b], A}, {rocks[c], C}, {rocks[d], C}};\n vector<Point> cc1 = CrossPointCC(cs[0], cs[1]);\n vector<Point> cc2 = CrossPointCC(cs[2], cs[3]);\n if (cc1.empty()) return false;\n if (cc2.empty()) return false;\n\n for (Point p1 : cc1) {\n for (Point p2 : cc2) {\n if (abs(p1-p2) < B+EPS) return result[a][b][c][d] = true;\n }\n }\n\n vector<Point> ps1(cc1);\n vector<Point> ps2(cc2);\n for (int i=0; i<2; i++) {\n for (int j=2; j<4; j++) {\n Line l(cs[i].c, cs[j].c);\n vector<Point> cl1 = CrossPointCL(cs[i], l);\n vector<Point> cl2 = CrossPointCL(cs[j], l);\n\n for (Point p1 : cl1) {\n if (abs(p1-cs[0].c) >= cs[0].r + EPS) continue;\n if (abs(p1-cs[1].c) >= cs[1].r + EPS) continue;\n for (Point p2 : cl2) {\n if (abs(p2-cs[2].c) >= cs[2].r + EPS) continue;\n if (abs(p2-cs[3].c) >= cs[3].r + EPS) continue;\n if (abs(p1-p2) < B+EPS) return result[a][b][c][d] = true;\n }\n }\n }\n }\n\n for (int i=0; i<2; i++) {\n for (Point p2 : cc2) {\n Line l(cs[i].c, p2);\n vector<Point> cl1 = CrossPointCL(cs[i], l);\n for (Point p1 : cl1) {\n if (abs(p1-cs[0].c) >= cs[0].r + EPS) continue;\n if (abs(p1-cs[1].c) >= cs[1].r + EPS) continue;\n if (abs(p1-p2) < B+EPS) return result[a][b][c][d] = true;\n }\n }\n }\n\n for (int j=2; j<4; j++) {\n for (Point p1 : cc1) {\n Line l(cs[j].c, p1);\n vector<Point> cl2 = CrossPointCL(cs[j], l);\n /*cl2.push_back(cs[2].c);\n cl2.push_back(cs[3].c);*/\n for (Point p2 : cl2) {\n if (abs(p2-cs[2].c) >= cs[2].r + EPS) continue;\n if (abs(p2-cs[3].c) >= cs[3].r + EPS) continue;\n if (abs(p1-p2) < B+EPS) return result[a][b][c][d] = true;\n }\n }\n }\n\n return result[a][b][c][d] = false;\n}\n\nint main() {\n scanf(\"%d\", &n);\n scanf(\"%lf%lf%lf\", &A, &B, &C);\n swap(A, B);\n for (int i=0; i<n; i++) {\n double x, y;\n scanf(\"%lf%lf\", &x, &y);\n rocks[i] = Point(x, y);\n }\n\n int ans = INF;\n queue<int> q;\n fill(mind, mind+11451419, INF);\n for (int i=0; i<4; i++) {\n int nums[] = {1, 2, 3, 4};\n\n nums[i] = 0;\n NormalizeNum(nums); \n \n bool is_goal = false;\n int state = CalcState(nums, &is_goal); \n mind[state] = 0;\n q.push(state);\n if (is_goal) {\n puts(\"0\");\n return 0;\n }\n }\n\n while (!q.empty()) {\n int state = q.front(); q.pop();\n int nums[4];\n State2Num(state, nums);\n\n if (nums[0] == 0) {\n int b = nums[1]-1;\n int c = nums[2]-1;\n int d = nums[3]-1;\n\n for (int a=0; a<n; a++) {\n if (b == a || c == a || d == a) continue;\n\n if (IsPossible(a, b, c, d)) {\n //printf(\"%d %d %d %d\\n\", a, b, c, d);\n for (int j=1; j<4; j++) {\n int nums2[4];\n\n memcpy(nums2, nums, sizeof(nums2));\n nums2[0] = a+1;\n nums2[j] = 0;\n NormalizeNum(nums2);\n\n bool is_goal = false;\n int next_state = CalcState(nums2, &is_goal);\n if (is_goal) {\n printf(\"%d\\n\", mind[state]+1);\n return 0;\n }\n\n if (mind[next_state] > mind[state]+1) {\n mind[next_state] = mind[state]+1;\n q.push(next_state);\n }\n }\n }\n }\n } else {\n int a = nums[0]-1;\n int b = nums[1]-1;\n int d = nums[3]-1;\n\n for (int c=0; c<n; c++) {\n if (a == c || b == c || d == c) continue;\n\n if (IsPossible(a, b, c, d)) {\n //printf(\"%d %d %d %d\\n\", a, b, c, d);\n for (int j=0; j<4; j++) {\n if (j == 2) continue;\n\n int nums2[4];\n\n memcpy(nums2, nums, sizeof(nums2));\n nums2[2] = c+1;\n nums2[j] = 0;\n NormalizeNum(nums2);\n\n bool is_goal = false;\n int next_state = CalcState(nums2, &is_goal);\n if (is_goal) {\n printf(\"%d\\n\", mind[state]+1);\n return 0;\n }\n\n if (mind[next_state] > mind[state]+1) {\n mind[next_state] = mind[state]+1;\n q.push(next_state);\n }\n }\n }\n }\n }\n }\n\n puts(\"-1\");\n}", "accuracy": 0.64, "time_ms": 1260, "memory_kb": 49032, "score_of_the_acc": -1.1835, "final_rank": 11 }, { "submission_id": "aoj_2227_2126363", "code_snippet": "#include <cstdio>\n#include <cstring>\n#include <cassert>\n#include <vector>\n#include <queue>\n#include <complex>\n#include <algorithm>\nusing namespace std;\n\n#define EQ(x, y) (abs((x)-(y)) < EPS)\n#define EPS 1e-8\n#define INF 0x33433433\n\n#define X real()\n#define Y imag()\n\ntypedef pair<int, int> Pair;\ntypedef complex<double> Point;\ntypedef Point Vec;\n\nint n;\ndouble A, B, C;\nPoint rocks[50];\nint mind[11451419];\nbool considered[30][30][30][30];\nbool result[30][30][30][30];\n\nclass Line : public vector<Point> {\npublic:\n Line () {resize(2);}\n Line (Point a, Point b) {\n push_back(a);\n push_back(b);\n }\n};\n\nstruct Circle {\n Point c;\n double r;\n};\n\ndouble dot(Point a, Point b) {\n return (conj(a)*b).X;\n}\n\ndouble cross(Point a, Point b) {\n return (conj(a)*b).Y;\n}\n\nint ccw(Point a, Point b, Point c) {\n b -= a;\n c -= a;\n if (cross(b, c) > 0) return 1;\n if (cross(b, c) < 0) return -1;\n if (dot(b, c) < 0) return 2;\n if (norm(b) < norm(c)) return -2;\n return 0;\n}\n\nPoint proj(Line l, Point p) {\n Point v = l[1]-l[0];\n double ratio = dot(v, p-l[0]) / norm(v);\n return l[0] + ratio*v;\n}\n\ndouble DistLP(Line l, Point p) {\n return abs(p - proj(l, p));\n}\n\nvector<Point> CrossPointCL(Circle c, Line l){\n vector<Point> ret;\n double d = DistLP(l, c.c);\n if(d < c.r + EPS){\n double ratio = (d > c.r) ? 0.0 : sqrt(c.r*c.r - d*d);\n Point sgn = (l[0] - l[1]) / abs(l[0] - l[1]);\n ret.push_back(proj(l, c.c) + ratio * sgn);\n ret.push_back(proj(l, c.c) - ratio * sgn);\n }\n\n return ret;\n}\n\nvector<Point> CrossPointCC(Circle c1, Circle c2){\n vector<Point> ret;\n double d = abs(c1.c - c2.c);\n double rc = (d*d + c1.r*c1.r - c2.r*c2.r) / (2*d);\n double dfr = c1.r*c1.r - rc*rc;\n\n if (EQ(dfr, 0.0)) dfr = 0.0;\n else if(dfr < 0.0) return ret;\n\n double rs = sqrt(dfr);\n Point sgn = (c2.c - c1.c) / d;\n ret.push_back(c1.c + sgn*Point(rc, rs));\n if (dfr > 0.0) ret.push_back(c1.c + sgn*Point(rc, -rs));\n return ret;\n}\n\nvoid NormalizeNum(int *nums) {\n if (nums[0] > nums[1]) swap(nums[0], nums[1]);\n if (nums[2] > nums[3]) swap(nums[2], nums[3]);\n}\n\nint CalcState(int *nums, bool *is_goal) {\n int state = 0;\n for (int i=0; i<4; i++) {\n state = state*31 + nums[i];\n if (nums[i] == n && is_goal) *is_goal = true;\n }\n return state;\n}\n\nvoid State2Num(int state, int *nums) {\n for (int i=0; i<4; i++) {\n nums[3-i] = state%31;\n state /= 31;\n }\n}\n\nbool IsPossible(int a, int b, int c, int d) {\n if (considered[a][b][c][d]) return result[a][b][c][d];\n considered[a][b][c][d] = true;\n\n Circle cs[] = {{rocks[a], A}, {rocks[b], A}, {rocks[c], C}, {rocks[d], C}};\n vector<Point> cc1 = CrossPointCC(cs[0], cs[1]);\n vector<Point> cc2 = CrossPointCC(cs[2], cs[3]);\n if (cc1.empty()) return false;\n if (cc2.empty()) return false;\n\n for (Point p1 : cc1) {\n for (Point p2 : cc2) {\n if (abs(p1-p2) < B+EPS) return result[a][b][c][d] = true;\n }\n }\n\n vector<Point> ps1(cc1);\n vector<Point> ps2(cc2);\n for (int i=0; i<2; i++) {\n for (int j=0; j<2; j++) {\n Line l(cs[i].c, cs[j].c);\n vector<Point> cl1 = CrossPointCL(cs[i], l);\n vector<Point> cl2 = CrossPointCL(cs[j], l);\n\n for (Point p : cl1) {\n if (abs(p-cs[0].c) >= cs[0].r + EPS) continue;\n if (abs(p-cs[1].c) >= cs[1].r + EPS) continue;\n for (Point p : cl2) {\n if (abs(p - cs[2].c) >= cs[2].r + EPS) continue;\n if (abs(p - cs[3].c) >= cs[3].r + EPS) continue;\n }\n }\n }\n }\n\n for (int i=0; i<2; i++) {\n for (Point p2 : cc2) {\n Line l(cs[i].c, p2);\n vector<Point> cl1 = CrossPointCL(cs[i], l);\n for (Point p1 : cl1) {\n if (abs(p1-cs[0].c) >= cs[0].r + EPS) continue;\n if (abs(p1-cs[1].c) >= cs[1].r + EPS) continue;\n if (abs(p1-p2) < B+EPS) return result[a][b][c][d] = true;\n }\n }\n }\n\n for (int j=0; j<2; j++) {\n for (Point p1 : cc1) {\n Line l(cs[j].c, p1);\n vector<Point> cl2 = CrossPointCL(cs[j], l);\n for (Point p2 : cl2) {\n if (abs(p2-cs[2].c) >= cs[2].r + EPS) continue;\n if (abs(p2-cs[3].c) >= cs[3].r + EPS) continue;\n if (abs(p1-p2) < B+EPS) return result[a][b][c][d] = true;\n }\n }\n }\n\n return result[a][b][c][d] = false;\n}\n\nint main() {\n scanf(\"%d\", &n);\n scanf(\"%lf%lf%lf\", &A, &B, &C);\n swap(A, B);\n for (int i=0; i<n; i++) {\n double x, y;\n scanf(\"%lf%lf\", &x, &y);\n rocks[i] = Point(x, y);\n }\n\n int ans = INF;\n queue<int> q;\n fill(mind, mind+11451419, INF);\n for (int i=0; i<4; i++) {\n int nums[] = {1, 2, 3, 4};\n\n nums[i] = 0;\n NormalizeNum(nums); \n \n bool is_goal = false;\n int state = CalcState(nums, &is_goal); \n mind[state] = 0;\n q.push(state);\n if (is_goal) {\n puts(\"0\");\n return 0;\n }\n }\n\n while (!q.empty()) {\n int state = q.front(); q.pop();\n int nums[4];\n State2Num(state, nums);\n\n if (nums[0] == 0) {\n int b = nums[1]-1;\n int c = nums[2]-1;\n int d = nums[3]-1;\n\n for (int a=0; a<n; a++) {\n if (b == a || c == a || d == a) continue;\n\n if (IsPossible(a, b, c, d)) {\n //printf(\"%d %d %d %d\\n\", a, b, c, d);\n for (int j=1; j<4; j++) {\n int nums2[4];\n\n memcpy(nums2, nums, sizeof(nums2));\n nums2[0] = a+1;\n nums2[j] = 0;\n NormalizeNum(nums2);\n\n bool is_goal = false;\n int next_state = CalcState(nums2, &is_goal);\n if (is_goal) {\n printf(\"%d\\n\", mind[state]+1);\n return 0;\n }\n\n if (mind[next_state] > mind[state]+1) {\n mind[next_state] = mind[state]+1;\n q.push(next_state);\n }\n }\n }\n }\n } else {\n int a = nums[0]-1;\n int b = nums[1]-1;\n int d = nums[3]-1;\n\n for (int c=0; c<n; c++) {\n if (a == c || b == c || d == c) continue;\n\n if (IsPossible(a, b, c, d)) {\n //printf(\"%d %d %d %d\\n\", a, b, c, d);\n for (int j=0; j<4; j++) {\n if (j == 2) continue;\n\n int nums2[4];\n\n memcpy(nums2, nums, sizeof(nums2));\n nums2[2] = c+1;\n nums2[j] = 0;\n NormalizeNum(nums2);\n\n bool is_goal = false;\n int next_state = CalcState(nums2, &is_goal);\n if (is_goal) {\n printf(\"%d\\n\", mind[state]+1);\n return 0;\n }\n\n if (mind[next_state] > mind[state]+1) {\n mind[next_state] = mind[state]+1;\n q.push(next_state);\n }\n }\n }\n }\n }\n }\n\n puts(\"-1\");\n}", "accuracy": 0.18, "time_ms": 1260, "memory_kb": 49156, "score_of_the_acc": -1.1866, "final_rank": 16 }, { "submission_id": "aoj_2227_2126277", "code_snippet": "#include <cstdio>\n#include <cstring>\n#include <cassert>\n#include <vector>\n#include <queue>\n#include <complex>\n#include <algorithm>\nusing namespace std;\n\n#define EQ(x, y) (abs((x)-(y)) < EPS)\n#define EPS 1e-8\n#define INF 0x33433433\n\n#define X real()\n#define Y imag()\n\ntypedef pair<int, int> Pair;\ntypedef complex<double> Point;\ntypedef Point Vec;\n\nint n;\ndouble A, B, C;\nPoint rocks[50];\nint mind[11451419];\n\nclass Line : public vector<Point> {\npublic:\n Line () {resize(2);}\n Line (Point a, Point b) {\n push_back(a);\n push_back(b);\n }\n};\n\nstruct Circle {\n Point c;\n double r;\n};\n\ndouble dot(Point a, Point b) {\n return (conj(a)*b).X;\n}\n\ndouble cross(Point a, Point b) {\n return (conj(a)*b).Y;\n}\n\nint ccw(Point a, Point b, Point c) {\n b -= a;\n c -= a;\n if (cross(b, c) > 0) return 1;\n if (cross(b, c) < 0) return -1;\n if (dot(b, c) < 0) return 2;\n if (norm(b) < norm(c)) return -2;\n return 0;\n}\n\nPoint proj(Line l, Point p) {\n Point v = l[1]-l[0];\n double ratio = dot(v, p-l[0]) / norm(v);\n return l[0] + ratio*v;\n}\n\ndouble DistLP(Line l, Point p) {\n return abs(p - proj(l, p));\n}\n\nvector<Point> CrossPointCL(Circle c, Line l){\n vector<Point> ret;\n double d = DistLP(l, c.c);\n if(d < c.r + EPS){\n double ratio = (d > c.r) ? 0.0 : sqrt(c.r*c.r - d*d);\n Point sgn = (l[0] - l[1]) / abs(l[0] - l[1]);\n ret.push_back(proj(l, c.c) + ratio * sgn);\n ret.push_back(proj(l, c.c) - ratio * sgn);\n }\n\n return ret;\n}\n\nvector<Point> CrossPointCS(Circle c, Line s){\n vector<Point> ret;\n vector<Point> res = CrossPointCL(c, s);\n for (int i=0; i<res.size(); i++) {\n if(ccw(s[0], res[i], s[1]) == -2) ret.push_back(res[i]);\n }\n return ret;\n}\n\nvector<Point> CrossPointCC(Circle c1, Circle c2){\n vector<Point> ret;\n double d = abs(c1.c - c2.c);\n double rc = (d*d + c1.r*c1.r - c2.r*c2.r) / (2*d);\n double dfr = c1.r*c1.r - rc*rc;\n\n if (EQ(dfr, 0.0)) dfr = 0.0;\n else if(dfr < 0.0) return ret;\n\n double rs = sqrt(dfr);\n Point sgn = (c2.c - c1.c) / d;\n ret.push_back(c1.c + sgn*Point(rc, rs));\n if (dfr > 0.0) ret.push_back(c1.c + sgn*Point(rc, -rs));\n return ret;\n}\n\nvoid NormalizeNum(int *nums) {\n if (nums[0] > nums[1]) swap(nums[0], nums[1]);\n if (nums[2] > nums[3]) swap(nums[2], nums[3]);\n}\n\nint CalcState(int *nums, bool *is_goal) {\n int state = 0;\n for (int i=0; i<4; i++) {\n state = state*31 + nums[i];\n if (nums[i] == n && is_goal) *is_goal = true;\n }\n return state;\n}\n\nvoid State2Num(int state, int *nums) {\n for (int i=0; i<4; i++) {\n nums[3-i] = state%31;\n state /= 31;\n }\n}\n\nbool IsPossible(int a, int b, int c, int d) {\n Circle cs[] = {{rocks[a], A}, {rocks[b], A}, {rocks[c], C}, {rocks[d], C}};\n vector<Point> ps = CrossPointCC(cs[0], cs[1]);\n vector<Point> ps2 = CrossPointCC(cs[2], cs[3]);\n if (ps.empty()) return false;\n if (ps2.empty()) return false;\n\n /*\n Vec v = cs[1].c-cs[0].c;\n ps.push_back(cs[0].c + v/abs(v)*(abs(v)-cs[1].r));\n ps.push_back(cs[1].c - v/abs(v)*(abs(v)-cs[0].r));\n\n Vec v2 = cs[3].c-cs[2].c;\n ps2.push_back(cs[2].c + v2/abs(v2)*(abs(v2)-cs[3].r));\n ps2.push_back(cs[3].c - v2/abs(v2)*(abs(v2)-cs[2].r));\n */\n\n for (int i=0; i<2; i++) {\n for (int j=2; j<4; j++) {\n Line l(cs[i].c, cs[j].c);\n vector<Point> p1 = CrossPointCL(cs[i], l);\n vector<Point> p2 = CrossPointCL(cs[j], l);\n\n for (Point p : p1) {\n if (abs(p - cs[0].c) < cs[0].r + EPS) {\n if (abs(p - cs[1].c) < cs[1].r + EPS) {\n ps.push_back(p);\n }\n }\n }\n\n for (Point p : p2) {\n if (abs(p - cs[2].c) < cs[2].r + EPS) {\n if (abs(p - cs[3].c) < cs[3].r + EPS) {\n ps2.push_back(p);\n }\n }\n }\n }\n }\n\n for (Point p : ps) {\n for (Point p2 : ps2) {\n if (abs(p-p2) < B+EPS) return true;\n }\n }\n\n return false;\n}\n\nint main() {\n scanf(\"%d\", &n);\n scanf(\"%lf%lf%lf\", &A, &B, &C);\n swap(A, B);\n for (int i=0; i<n; i++) {\n double x, y;\n scanf(\"%lf%lf\", &x, &y);\n rocks[i] = Point(x, y);\n }\n\n int ans = INF;\n queue<int> q;\n fill(mind, mind+11451419, INF);\n for (int i=0; i<4; i++) {\n int nums[] = {1, 2, 3, 4};\n\n nums[i] = 0;\n NormalizeNum(nums); \n \n bool is_goal = false;\n int state = CalcState(nums, &is_goal); \n mind[state] = 0;\n q.push(state);\n if (is_goal) {\n puts(\"0\");\n return 0;\n }\n }\n\n while (!q.empty()) {\n int state = q.front(); q.pop();\n int nums[4];\n State2Num(state, nums);\n\n if (nums[0] == 0) {\n int b = nums[1]-1;\n int c = nums[2]-1;\n int d = nums[3]-1;\n\n for (int a=0; a<n; a++) {\n if (b == a || c == a || d == a) continue;\n\n if (IsPossible(a, b, c, d)) {\n //printf(\"%d %d %d %d\\n\", a, b, c, d);\n for (int j=1; j<4; j++) {\n int nums2[4];\n\n memcpy(nums2, nums, sizeof(nums2));\n nums2[0] = a+1;\n nums2[j] = 0;\n NormalizeNum(nums2);\n\n bool is_goal = false;\n int next_state = CalcState(nums2, &is_goal);\n if (is_goal) {\n printf(\"%d\\n\", mind[state]+1);\n return 0;\n }\n\n if (mind[next_state] > mind[state]+1) {\n mind[next_state] = mind[state]+1;\n q.push(next_state);\n }\n }\n }\n }\n } else {\n int a = nums[0]-1;\n int b = nums[1]-1;\n int d = nums[3]-1;\n\n for (int c=0; c<n; c++) {\n if (a == c || b == c || d == c) continue;\n\n if (IsPossible(a, b, c, d)) {\n //printf(\"%d %d %d %d\\n\", a, b, c, d);\n for (int j=0; j<4; j++) {\n if (j == 2) continue;\n\n int nums2[4];\n\n memcpy(nums2, nums, sizeof(nums2));\n nums2[2] = c+1;\n nums2[j] = 0;\n NormalizeNum(nums2);\n\n bool is_goal = false;\n int next_state = CalcState(nums2, &is_goal);\n if (is_goal) {\n printf(\"%d\\n\", mind[state]+1);\n return 0;\n }\n\n if (mind[next_state] > mind[state]+1) {\n mind[next_state] = mind[state]+1;\n q.push(next_state);\n }\n }\n }\n }\n }\n }\n\n puts(\"-1\");\n}", "accuracy": 0.64, "time_ms": 1690, "memory_kb": 47576, "score_of_the_acc": -1.2145, "final_rank": 12 }, { "submission_id": "aoj_2227_2126213", "code_snippet": "#include <cstdio>\n#include <cstring>\n#include <cassert>\n#include <vector>\n#include <queue>\n#include <complex>\n#include <algorithm>\nusing namespace std;\n\n#define EQ(x, y) (abs((x)-(y)) < EPS)\n#define EPS 1e-8\n#define INF 0x33433433\n\n#define X real()\n#define Y imag()\n\ntypedef pair<int, int> Pair;\ntypedef complex<double> Point;\ntypedef Point Vec;\n\nint n;\ndouble A, B, C;\nPoint rocks[50];\nint mind[11451419];\n\nclass Line : public vector<Point> {\npublic:\n Line () {resize(2);}\n Line (Point a, Point b) {\n push_back(a);\n push_back(b);\n }\n};\n\nstruct Circle {\n Point c;\n double r;\n};\n\ndouble dot(Point a, Point b) {\n return (conj(a)*b).X;\n}\n\ndouble cross(Point a, Point b) {\n return (conj(a)*b).Y;\n}\n\nint ccw(Point a, Point b, Point c) {\n b -= a;\n c -= a;\n if (cross(b, c) > 0) return 1;\n if (cross(b, c) < 0) return -1;\n if (dot(b, c) < 0) return 2;\n if (norm(b) < norm(c)) return -2;\n return 0;\n}\n\nPoint proj(Line l, Point p) {\n Point v = l[1]-l[0];\n double ratio = dot(v, p-l[0]) / norm(v);\n return l[0] + ratio*v;\n}\n\ndouble DistLP(Line l, Point p) {\n return abs(p - proj(l, p));\n}\n\nvector<Point> CrossPointCL(Circle c, Line l){\n vector<Point> ret;\n double d = DistLP(l, c.c);\n if(d < c.r + EPS){\n double ratio = (d > c.r) ? 0.0 : sqrt(c.r*c.r - d*d);\n Point sgn = (l[0] - l[1]) / abs(l[0] - l[1]);\n ret.push_back(proj(l, c.c) + ratio * sgn);\n ret.push_back(proj(l, c.c) - ratio * sgn);\n }\n\n return ret;\n}\n\nvector<Point> CrossPointCS(Circle c, Line s){\n vector<Point> ret;\n vector<Point> res = CrossPointCL(c, s);\n for (int i=0; i<res.size(); i++) {\n if(ccw(s[0], res[i], s[1]) == -2) ret.push_back(res[i]);\n }\n return ret;\n}\n\nvector<Point> CrossPointCC(Circle c1, Circle c2){\n vector<Point> ret;\n double d = abs(c1.c - c2.c);\n double rc = (d*d + c1.r*c1.r - c2.r*c2.r) / (2*d);\n double dfr = c1.r*c1.r - rc*rc;\n\n if (EQ(dfr, 0.0)) dfr = 0.0;\n else if(dfr < 0.0) return ret;\n\n double rs = sqrt(dfr);\n Point sgn = (c2.c - c1.c) / d;\n ret.push_back(c1.c + sgn*Point(rc, rs));\n if (dfr > 0.0) ret.push_back(c1.c + sgn*Point(rc, -rs));\n return ret;\n}\n\nvoid NormalizeNum(int *nums) {\n if (nums[0] > nums[1]) swap(nums[0], nums[1]);\n if (nums[2] > nums[3]) swap(nums[2], nums[3]);\n}\n\nint CalcState(int *nums, bool *is_goal) {\n int state = 0;\n for (int i=0; i<4; i++) {\n state = state*31 + nums[i];\n if (nums[i] == n && is_goal) *is_goal = true;\n }\n return state;\n}\n\nvoid State2Num(int state, int *nums) {\n for (int i=0; i<4; i++) {\n nums[3-i] = state%31;\n state /= 31;\n }\n}\n\nbool IsPossible(int a, int b, int c, int d) {\n Circle cs[] = {{rocks[a], A}, {rocks[b], A}, {rocks[c], C}, {rocks[d], C}};\n vector<Point> ps = CrossPointCC(cs[0], cs[1]);\n vector<Point> ps2 = CrossPointCC(cs[2], cs[3]);\n if (ps.empty()) return false;\n if (ps2.empty()) return false;\n\n Vec v = cs[1].c-cs[0].c;\n ps.push_back(cs[0].c + v/abs(v)*(abs(v)-cs[1].r));\n ps.push_back(cs[1].c - v/abs(v)*(abs(v)-cs[0].r));\n\n Vec v2 = cs[3].c-cs[2].c;\n ps2.push_back(cs[2].c + v2/abs(v2)*(abs(v2)-cs[3].r));\n ps2.push_back(cs[3].c - v2/abs(v2)*(abs(v2)-cs[2].r));\n\n for (int i=0; i<2; i++) {\n for (int j=2; j<4; j++) {\n Line l(cs[i].c, cs[j].c);\n vector<Point> p1 = CrossPointCL(cs[i], l);\n vector<Point> p2 = CrossPointCL(cs[j], l);\n\n for (int k=0; k<2; k++) {\n if (abs(p1[k] - cs[0].c) < cs[0].r + EPS) {\n if (abs(p1[k] - cs[1].c) < cs[1].r + EPS) {\n ps.push_back(p1[k]);\n }\n }\n\n if (abs(p2[k] - cs[2].c) < cs[2].r + EPS) {\n if (abs(p2[k] - cs[3].c) < cs[3].r + EPS) {\n ps2.push_back(p2[k]);\n }\n }\n }\n }\n }\n\n for (Point p : ps) {\n for (Point p2 : ps2) {\n if (abs(p-p2) < B+EPS) return true;\n }\n }\n\n return false;\n}\n\nint main() {\n scanf(\"%d\", &n);\n scanf(\"%lf%lf%lf\", &A, &B, &C);\n swap(A, B);\n for (int i=0; i<n; i++) {\n double x, y;\n scanf(\"%lf%lf\", &x, &y);\n rocks[i] = Point(x, y);\n }\n\n int ans = INF;\n queue<int> q;\n fill(mind, mind+11451419, INF);\n for (int i=0; i<4; i++) {\n int nums[] = {1, 2, 3, 4};\n\n nums[i] = 0;\n NormalizeNum(nums); \n \n bool is_goal = false;\n int state = CalcState(nums, &is_goal); \n mind[state] = 0;\n q.push(state);\n if (is_goal) {\n puts(\"0\");\n return 0;\n }\n }\n\n while (!q.empty()) {\n int state = q.front(); q.pop();\n int nums[4];\n State2Num(state, nums);\n\n if (nums[0] == 0) {\n int b = nums[1]-1;\n int c = nums[2]-1;\n int d = nums[3]-1;\n\n for (int a=0; a<n; a++) {\n if (b == a || c == a || d == a) continue;\n\n if (IsPossible(a, b, c, d)) {\n for (int j=1; j<4; j++) {\n int nums2[4];\n\n memcpy(nums2, nums, sizeof(nums2));\n nums2[0] = a+1;\n nums2[j] = 0;\n NormalizeNum(nums2);\n\n bool is_goal = false;\n int next_state = CalcState(nums2, &is_goal);\n if (mind[next_state] > mind[state]+1) {\n mind[next_state] = mind[state]+1;\n q.push(next_state);\n if (is_goal) {\n printf(\"%d\\n\", mind[next_state]);\n return 0;\n }\n }\n }\n }\n }\n } else {\n int a = nums[0]-1;\n int b = nums[1]-1;\n int d = nums[3]-1;\n\n for (int c=0; c<n; c++) {\n if (a == c || b == c || d == c) continue;\n\n if (IsPossible(a, b, c, d)) {\n for (int j=0; j<4; j++) {\n if (j == 2) continue;\n\n int nums2[4];\n\n memcpy(nums2, nums, sizeof(nums2));\n nums2[2] = c+1;\n nums2[j] = 0;\n NormalizeNum(nums2);\n\n bool is_goal = false;\n int next_state = CalcState(nums2, &is_goal);\n if (mind[next_state] > mind[state]+1) {\n mind[next_state] = mind[state]+1;\n q.push(next_state);\n if (is_goal) {\n printf(\"%d\\n\", mind[next_state]);\n return 0;\n }\n }\n }\n }\n }\n }\n }\n\n puts(\"-1\");\n}", "accuracy": 0.64, "time_ms": 1720, "memory_kb": 47596, "score_of_the_acc": -1.2197, "final_rank": 13 }, { "submission_id": "aoj_2227_2126196", "code_snippet": "#include <cstdio>\n#include <cstring>\n#include <cassert>\n#include <vector>\n#include <queue>\n#include <complex>\n#include <algorithm>\nusing namespace std;\n\n#define EQ(x, y) (abs((x)-(y)) < EPS)\n#define EPS 1e-8\n#define INF 0x33433433\n\n#define X real()\n#define Y imag()\n\ntypedef pair<int, int> Pair;\ntypedef complex<double> Point;\ntypedef Point Vec;\n\nint n;\ndouble A, B, C;\nPoint rocks[50];\nint mind[11451419];\n\nclass Line : public vector<Point> {\npublic:\n Line () {resize(2);}\n Line (Point a, Point b) {\n push_back(a);\n push_back(b);\n }\n};\n\nstruct Circle {\n Point c;\n double r;\n};\n\ndouble dot(Point a, Point b) {\n return (conj(a)*b).X;\n}\n\ndouble cross(Point a, Point b) {\n return (conj(a)*b).Y;\n}\n\nint ccw(Point a, Point b, Point c) {\n b -= a;\n c -= a;\n if (cross(b, c) > 0) return 1;\n if (cross(b, c) < 0) return -1;\n if (dot(b, c) < 0) return 2;\n if (norm(b) < norm(c)) return -2;\n return 0;\n}\n\nPoint proj(Line l, Point p) {\n Point v = l[1]-l[0];\n double ratio = dot(v, p-l[0]) / norm(v);\n return l[0] + ratio*v;\n}\n\ndouble DistLP(Line l, Point p) {\n return abs(p - proj(l, p));\n}\n\nvector<Point> CrossPointCL(Circle c, Line l){\n vector<Point> ret;\n double d = DistLP(l, c.c);\n if(d < c.r + EPS){\n double ratio = (d > c.r) ? 0.0 : sqrt(c.r*c.r - d*d);\n Point sgn = (l[0] - l[1]) / abs(l[0] - l[1]);\n ret.push_back(proj(l, c.c) + ratio * sgn);\n ret.push_back(proj(l, c.c) - ratio * sgn);\n }\n\n return ret;\n}\n\nvector<Point> CrossPointCS(Circle c, Line s){\n vector<Point> ret;\n vector<Point> res = CrossPointCL(c, s);\n for (int i=0; i<res.size(); i++) {\n if(ccw(s[0], res[i], s[1]) == -2) ret.push_back(res[i]);\n }\n return ret;\n}\n\nvector<Point> CrossPointCC(Circle c1, Circle c2){\n vector<Point> ret;\n double d = abs(c1.c - c2.c);\n double rc = (d*d + c1.r*c1.r - c2.r*c2.r) / (2*d);\n double dfr = c1.r*c1.r - rc*rc;\n\n if (EQ(dfr, 0.0)) dfr = 0.0;\n else if(dfr < 0.0) return ret;\n\n double rs = sqrt(dfr);\n Point sgn = (c2.c - c1.c) / d;\n ret.push_back(c1.c + sgn*Point(rc, rs));\n if (dfr > 0.0) ret.push_back(c1.c + sgn*Point(rc, -rs));\n return ret;\n}\n\nvoid NormalizeNum(int *nums) {\n if (nums[0] > nums[1]) swap(nums[0], nums[1]);\n if (nums[2] > nums[3]) swap(nums[2], nums[3]);\n}\n\nint CalcState(int *nums, bool *is_goal) {\n int state = 0;\n for (int i=0; i<4; i++) {\n state = state*31 + nums[i];\n if (nums[i] == n && is_goal) *is_goal = true;\n }\n return state;\n}\n\nvoid State2Num(int state, int *nums) {\n for (int i=0; i<4; i++) {\n nums[3-i] = state%31;\n state /= 31;\n }\n}\n\nbool IsPossible(int a, int b, int c, int d) {\n Circle cs[] = {{rocks[a], A}, {rocks[b], A}, {rocks[c], C}, {rocks[d], C}};\n vector<Point> ps = CrossPointCC(cs[0], cs[1]);\n vector<Point> ps2 = CrossPointCC(cs[2], cs[3]);\n if (ps.empty()) return false;\n if (ps2.empty()) return false;\n\n Vec v = cs[1].c-cs[0].c;\n ps.push_back(cs[0].c + v/abs(v)*(abs(v)-cs[1].r));\n ps.push_back(cs[1].c - v/abs(v)*(abs(v)-cs[0].r));\n\n Vec v2 = cs[3].c-cs[2].c;\n ps2.push_back(cs[2].c + v2/abs(v2)*(abs(v2)-cs[3].r));\n ps2.push_back(cs[3].c - v2/abs(v2)*(abs(v2)-cs[2].r));\n\n for (int i=0; i<2; i++) {\n for (int j=2; j<4; j++) {\n Line l(cs[i].c, cs[j].c);\n vector<Point> p1 = CrossPointCL(cs[i], l);\n vector<Point> p2 = CrossPointCL(cs[j], l);\n if (abs(p1[0] - cs[0].c) < cs[0].r + EPS) {\n if (abs(p1[0] - cs[1].c) < cs[1].r + EPS) {\n ps.push_back(p1[0]);\n }\n }\n\n if (abs(p2[0] - cs[2].c) < cs[2].r + EPS) {\n if (abs(p2[0] - cs[3].c) < cs[3].r + EPS) {\n ps2.push_back(p2[0]);\n }\n }\n }\n }\n\n for (Point p : ps) {\n for (Point p2 : ps2) {\n if (abs(p-p2) < B+EPS) return true;\n }\n }\n\n return false;\n}\n\nint main() {\n scanf(\"%d\", &n);\n scanf(\"%lf%lf%lf\", &A, &B, &C);\n swap(A, B);\n for (int i=0; i<n; i++) {\n double x, y;\n scanf(\"%lf%lf\", &x, &y);\n rocks[i] = Point(x, y);\n }\n\n int ans = INF;\n queue<int> q;\n fill(mind, mind+11451419, INF);\n for (int i=0; i<4; i++) {\n int nums[] = {1, 2, 3, 4};\n\n nums[i] = 0;\n NormalizeNum(nums); \n \n bool is_goal = false;\n int state = CalcState(nums, &is_goal); \n mind[state] = 0;\n q.push(state);\n if (is_goal) {\n puts(\"0\");\n return 0;\n }\n }\n\n while (!q.empty()) {\n int state = q.front(); q.pop();\n int nums[4];\n State2Num(state, nums);\n\n if (nums[0] == 0) {\n int b = nums[1]-1;\n int c = nums[2]-1;\n int d = nums[3]-1;\n\n for (int a=0; a<n; a++) {\n if (b == a || c == a || d == a) continue;\n\n if (IsPossible(a, b, c, d)) {\n for (int j=1; j<4; j++) {\n int nums2[4];\n\n memcpy(nums2, nums, sizeof(nums2));\n nums2[0] = a+1;\n nums2[j] = 0;\n NormalizeNum(nums2);\n\n bool is_goal = false;\n int next_state = CalcState(nums2, &is_goal);\n if (mind[next_state] > mind[state]+1) {\n mind[next_state] = mind[state]+1;\n q.push(next_state);\n if (is_goal) {\n printf(\"%d\\n\", mind[next_state]);\n return 0;\n }\n }\n }\n }\n }\n } else {\n int a = nums[0]-1;\n int b = nums[1]-1;\n int d = nums[3]-1;\n\n for (int c=0; c<n; c++) {\n if (a == c || b == c || d == c) continue;\n\n if (IsPossible(a, b, c, d)) {\n for (int j=0; j<4; j++) {\n if (j == 2) continue;\n\n int nums2[4];\n\n memcpy(nums2, nums, sizeof(nums2));\n nums2[2] = c+1;\n nums2[j] = 0;\n NormalizeNum(nums2);\n\n bool is_goal = false;\n int next_state = CalcState(nums2, &is_goal);\n if (mind[next_state] > mind[state]+1) {\n mind[next_state] = mind[state]+1;\n q.push(next_state);\n if (is_goal) {\n printf(\"%d\\n\", mind[next_state]);\n return 0;\n }\n }\n }\n }\n }\n }\n }\n\n puts(\"-1\");\n}", "accuracy": 0.32, "time_ms": 1550, "memory_kb": 47592, "score_of_the_acc": -1.193, "final_rank": 15 }, { "submission_id": "aoj_2227_2126016", "code_snippet": "#include <cstdio>\n#include <cstring>\n#include <cassert>\n#include <vector>\n#include <queue>\n#include <complex>\n#include <algorithm>\nusing namespace std;\n\n#define EQ(x, y) (abs((x)-(y)) < EPS)\n#define EPS 1e-8\n#define INF 0x33433433\n\n#define X real()\n#define Y imag()\n\ntypedef pair<int, int> Pair;\ntypedef complex<double> Point;\ntypedef Point Vec;\n\nint n;\ndouble A, B, C;\nPoint rocks[50];\nint mind[11451419];\n\nstruct Circle {\n Point c;\n double r;\n};\n\nvector<Point> CrossPointCC(Circle c1, Circle c2){\n vector<Point> ret;\n double d = abs(c1.c - c2.c);\n double rc = (d*d + c1.r*c1.r - c2.r*c2.r) / (2*d);\n double dfr = c1.r*c1.r - rc*rc;\n\n if (EQ(dfr, 0.0)) dfr = 0.0;\n else if(dfr < 0.0) return ret;\n\n double rs = sqrt(dfr);\n Point sgn = (c2.c - c1.c) / d;\n ret.push_back(c1.c + sgn*Point(rc, rs));\n if (dfr > 0.0) ret.push_back(c1.c + sgn*Point(rc, -rs));\n return ret;\n}\n\nvoid NormalizeNum(int *nums) {\n if (nums[0] > nums[1]) swap(nums[0], nums[1]);\n if (nums[2] > nums[3]) swap(nums[2], nums[3]);\n}\n\nint CalcState(int *nums, bool *is_goal) {\n int state = 0;\n for (int i=0; i<4; i++) {\n state = state*31 + nums[i];\n if (nums[i] == n && is_goal) *is_goal = true;\n }\n return state;\n}\n\nvoid State2Num(int state, int *nums) {\n for (int i=0; i<4; i++) {\n nums[3-i] = state%31;\n state /= 31;\n }\n}\n\nint main() {\n scanf(\"%d\", &n);\n scanf(\"%lf%lf%lf\", &A, &B, &C);\n for (int i=0; i<n; i++) {\n double x, y;\n scanf(\"%lf%lf\", &x, &y);\n rocks[i] = Point(x, y);\n }\n\n int ans = INF;\n queue<int> q;\n fill(mind, mind+11451419, INF);\n for (int i=0; i<4; i++) {\n int nums[] = {1, 2, 3, 4};\n\n nums[i] = 0;\n NormalizeNum(nums); \n \n bool is_goal = false;\n int state = CalcState(nums, &is_goal); \n mind[state] = 0;\n q.push(state);\n if (is_goal) ans = min(ans, mind[state]);\n }\n\n while (!q.empty()) {\n int state = q.front(); q.pop();\n int nums[4];\n State2Num(state, nums);\n\n if (nums[0] == 0) {\n int a = nums[1]-1;\n int b = nums[2]-1;\n int c = nums[3]-1;\n\n Circle c1 = {rocks[b], C};\n Circle c2 = {rocks[c], C};\n vector<Point> ps = CrossPointCC(c1, c2);\n if (!ps.empty()) {\n Vec v = c2.c-c1.c;\n ps.push_back(c1.c + v/abs(v)*(abs(v)-c2.r));\n ps.push_back(c2.c - v/abs(v)*(abs(v)-c1.r));\n }\n\n for (Point p : ps) {\n Circle c3 = {p, B};\n Circle c4 = {rocks[a], A};\n vector<Point> ps2 = CrossPointCC(c3, c4);\n if (!ps2.empty()) {\n Vec v2 = c4.c-c3.c;\n ps2.push_back(c3.c + v2/abs(v2)*(abs(v2)-c4.r));\n ps2.push_back(c4.c - v2/abs(v2)*(abs(v2)-c3.r));\n }\n\n for (Point p2 : ps2) {\n for (int i=0; i<n; i++) {\n if (i == a || i == b || i == c) continue;\n if (abs(rocks[i] - p2) <= A+EPS) {\n for (int j=1; j<=3; j++) {\n int nums2[4];\n\n memcpy(nums2, nums, sizeof(nums2));\n nums2[0] = i+1;\n nums2[j] = 0;\n NormalizeNum(nums2);\n\n bool is_goal = false;\n int next_state = CalcState(nums2, &is_goal);\n if (mind[next_state] > mind[state]+1) {\n mind[next_state] = mind[state]+1;\n q.push(next_state);\n if (is_goal) ans = min(ans, mind[next_state]);\n }\n }\n }\n }\n }\n }\n } else {\n int a = nums[0]-1;\n int b = nums[1]-1;\n int c = nums[3]-1;\n\n Circle c1 = {rocks[a], A};\n Circle c2 = {rocks[b], A};\n vector<Point> ps = CrossPointCC(c1, c2);\n if (!ps.empty()) {\n Vec v = c2.c-c1.c;\n ps.push_back(c1.c + v/abs(v)*(abs(v)-c2.r));\n ps.push_back(c2.c - v/abs(v)*(abs(v)-c1.r));\n }\n\n for (Point p : ps) {\n Circle c3 = {p, B};\n Circle c4 = {rocks[c], C};\n vector<Point> ps2 = CrossPointCC(c3, c4);\n if (!ps2.empty()) {\n Vec v2 = c4.c-c3.c;\n ps2.push_back(c3.c + v2/abs(v2)*(abs(v2)-c4.r));\n ps2.push_back(c4.c - v2/abs(v2)*(abs(v2)-c3.r));\n }\n\n for (Point p2 : ps2) {\n for (int i=0; i<n; i++) {\n if (i == a || i == b || i == c) continue;\n if (abs(rocks[i] - p2) <= C+EPS) {\n for (int j=0; j<=3; j++) {\n if (j == 2) continue;\n\n int nums2[4];\n\n memcpy(nums2, nums, sizeof(nums2));\n nums2[2] = i+1;\n nums2[j] = 0;\n NormalizeNum(nums2);\n\n bool is_goal = false;\n int next_state = CalcState(nums2, &is_goal);\n if (mind[next_state] > mind[state]+1) {\n mind[next_state] = mind[state]+1;\n q.push(next_state);\n if (is_goal) ans = min(ans, mind[next_state]);\n }\n }\n }\n }\n }\n }\n }\n }\n\n if (ans == INF) puts(\"-1\");\n else printf(\"%d\\n\", ans);\n}", "accuracy": 0.14, "time_ms": 100, "memory_kb": 47496, "score_of_the_acc": -0.9637, "final_rank": 17 }, { "submission_id": "aoj_2227_1543465", "code_snippet": "#include <cstdio>\n#include <iostream>\n#include <cmath>\n\nusing namespace std;\n\nconst double eps = 1e-8;\n\ninline int sign(double x) {\n\treturn x < -eps ? -1 : x > eps;\n}\n\ninline double sqr(double x) {\n\treturn x * x;\n}\n\nstruct point {\n\tdouble x, y;\n\tpoint(double x = 0, double y = 0) : x(x), y(y) {}\n\tinline double length() const {\n\t\treturn sqrt(x * x + y * y);\n\t}\n\tinline double norm() const {\n\t\treturn length();\n\t}\n\tinline double norm2() const {\n\t\treturn x * x + y * y;\n\t}\n\tinline point unit() const {\n\t\tdouble len = length();\n\t\treturn point(x / len, y / len);\n\t}\n\tinline point negate() const {\n\t\treturn point(-x, -y);\n\t}\n\tinline point rot90() const {\n\t\treturn point(-y, x);\n\t}\n\tinline point _rot90() const {\n\t\treturn point(y, -x);\n\t}\n\tinline point rotate(double theta) const {\n\t\tdouble c = cos(theta), s = sin(theta);\n\t\treturn point(x * c - y * s, x * s + y * c);\n\t}\n};\n\ninline bool operator== (const point &a, const point &b) {\n\treturn fabs(a.x - b.x) < eps && fabs(a.y - b.y) < eps;\n}\n\ninline bool operator!= (const point &a, const point &b) {\n\treturn fabs(a.x - b.x) > eps || fabs(a.y - b.y) > eps;\n}\n\ninline bool operator< (const point &a, const point &b) {\n\tif (fabs(a.x - b.x) > eps) return a.x < b.x;\n\treturn a.y + eps < b.y;\n}\n\ninline point operator+ (const point &a, const point &b) {\n\treturn point(a.x + b.x, a.y + b.y);\n}\n\ninline point operator- (const point &a, const point &b) {\n\treturn point(a.x - b.x, a.y - b.y);\n}\n\ninline point operator* (const point &a, const double &b) {\n\treturn point(a.x * b, a.y * b);\n}\n\ninline point operator/ (const point &a, const double &b) {\n\treturn point(a.x / b, a.y / b);\n}\n\ninline double det(const point &a, const point &b) {\n\treturn a.x * b.y - b.x * a.y;\n}\n\ninline double dot(const point &a, const point &b) {\n\treturn a.x * b.x + a.y * b.y;\n}\n\ninline double dis(const point &a, const point &b) {\n\treturn sqrt(sqr(a.x - b.x) + sqr(a.y - b.y));\n}\n\nstruct line {\n\tpoint s, t;\n\tline(point s = point(), point t = point()) : s(s), t(t) {}\n\tinline double length() const {\n\t\treturn dis(s, t);\n\t}\n};\n\ndouble point_to_line(const point &p, const line &l) {\n\treturn fabs(det(l.t - l.s, p - l.s)) / dis(l.s, l.t);\n}\n\npoint project_to_line(const point &p, const line & l) {\n\treturn l.s + (l.t - l.s) * (dot(p - l.s, l.t - l.s) / (l.t - l.s).norm2());\n}\n\nstruct circle {\n\tpoint center;\n\tdouble radius;\n\tcircle(point center = point(), double radius = 0) : center(center), radius(radius) {}\n};\n\ninline bool operator== (const circle &a, const circle &b) {\n\treturn a.center == b.center && fabs(a.radius - b.radius) < eps;\n}\n\ninline bool operator!= (const circle &a, const circle &b) {\n\treturn a.center != b.center || fabs(a.radius - b.radius) > eps;\n}\n\ninline bool in_circle(const point &p, const circle &c) {\n\treturn dis(p, c.center) < c.radius + eps;\n}\n\npoint line_circle_intersect(const line &l, const circle &c) {\n\tdouble x = sqrt(sqr(c.radius) - sqr(point_to_line(c.center, l)));\n\treturn project_to_line(c.center, l) + (l.s - l.t).unit() * x;\n}\n\npoint circle_intersect(const circle &a, const circle &b) {\n\tpoint r = (b.center - a.center).unit();\n\tdouble d = dis(a.center, b.center);\n\tdouble x = .5 * ((sqr(a.radius) - sqr(b.radius)) / d + d);\n\tdouble h = sqrt(sqr(a.radius) - sqr(x));\n\treturn a.center + r * x + r.rot90() * h;\n}\n\ninline bool point_on_line(const point &a, const line &b) {\n\treturn sign(det(a - b.s, b.t - b.s)) == 0 && dot(b.s - a, b.t - a) < eps;\n}\n\ninline bool two_side(const point &a, const point &b, const line &c) {\n\treturn sign(det(a - c.s, c.t - c.s)) * sign(det(b - c.s, c.t - c.s)) < 0;\n}\n\ninline bool intersect_judgement(const line &a, const line &b) {\n\tif (point_on_line(b.s, a) || point_on_line(b.t, a)) return true;\n\tif (point_on_line(a.s, b) || point_on_line(a.t, b)) return true;\n\treturn two_side(a.s, a.t, b) && two_side(b.s, b.t, a);\n}\n\nint n, arm, leg, body;\npoint a[105];\nbool flag[33][33][33][33];\n\nbool in_points(point p1, point p2, point p3, point cent) {\n\tdouble vec1 = det(p1 - cent, p2 - cent);\n\tdouble vec2 = det(p1 - cent, p3 - cent);\n\treturn (vec1 > -eps && vec2 < eps); \n}\n\nbool check(int a1, int a2, int b1, int b2) {\n\tpoint arm_c[3], leg_c[3];\n\tpoint p[3], q[3];\n\tarm_c[1] = a[a1]; \n\tarm_c[2] = a[a2]; \n\tleg_c[1] = a[b1]; \n\tleg_c[2] = a[b2]; \n\tif (dis(a[a1], a[a2]) > arm * 2. + eps) return false;\n\tif (dis(a[b1], a[b2]) > leg * 2. + eps) return false;\n\tp[1] = circle_intersect(circle(arm_c[1], arm), circle(arm_c[2], arm));\n\tp[2] = circle_intersect(circle(arm_c[2], arm), circle(arm_c[1], arm));\n\tq[1] = circle_intersect(circle(leg_c[1], leg), circle(leg_c[2], leg));\n\tq[2] = circle_intersect(circle(leg_c[2], leg), circle(leg_c[1], leg));\n\tdouble mindis = 1e9;\n\tfor (int i = 1; i <= 2; i ++)\n\t\tfor (int j = 1; j <= 2; j ++) \n\t\t\tmindis = min(mindis, dis(p[i], q[j]));\n\tfor (int i = 1; i <= 2; i ++) {\n\t\tfor (int j = 1; j <= 2; j ++) {\n\t\t\tif (in_circle(p[i], circle(leg_c[j], leg))) continue;\n\t\t\tif (in_circle(p[i], circle(leg_c[j], leg)) && in_circle(p[i], circle(leg_c[3 - j], leg))) return 1;\n\t\t\tpoint leg_near = p[i] + (leg_c[j] - p[i]).unit() * (dis(p[i], leg_c[j]) - leg);\n\t\t\tif (in_circle(leg_near, circle(leg_c[3 - j], leg))) mindis = min(mindis, dis(p[i], leg_c[j]) - leg);\n\t\t\t//if (in_points(leg_near, q[j], q[3 - j], leg_c[j])) mindis = min(mindis, dis(p[i], leg_c[j]) - leg);\n\t\t}\n\t}\n\tfor (int i = 1; i <= 2; i ++) \n\t\tfor (int j = 1; j <= 2; j ++) {\n\t\t\tif (in_circle(q[i], circle(arm_c[j], arm))) continue;\n\t\t\tif (in_circle(q[i], circle(arm_c[j], arm)) && in_circle(q[i], circle(arm_c[3 - j], arm))) return 1;\n\t\t\tpoint arm_near = q[i] + (arm_c[j] - q[i]).unit() * (dis(q[i], arm_c[j]) - arm);\n\t\t\tif (in_circle(arm_near, circle(arm_c[3 - j], arm))) mindis = min(mindis, dis(q[i], arm_c[j]) - arm);\n\t\t\t//if (in_points(arm_near, p[j], p[3 - j], arm_c[j])) mindis = min(mindis, dis(q[i], arm_c[j]) - arm);\n\t\t}\n\tfor (int i = 1; i <= 2; i ++)\n\t\tfor (int j = 1; j <= 2; j ++) {\n\t\t\tpoint pp = arm_c[i] + (leg_c[j] - arm_c[i]).unit() * arm;\n\t\t\tpoint qq = leg_c[j] + (arm_c[i] - leg_c[j]).unit() * leg;\t\t\t\n\t\t\tif (in_points(pp, p[i], p[3 - i], arm_c[i]) && in_points(qq, q[j], q[3 - j], leg_c[j])) {\n\t\t\t\tif (intersect_judgement(line(arm_c[i], pp), line(leg_c[j], qq)) || (dis(pp, qq) < body + eps)) return 1;\n\t\t\t}\n\t\t}\n\treturn mindis < body + eps;\n}\n\nstruct node {\n\tint a[5];\n} que[30 * 30 * 30 * 30 + 50];\n\nint d[33][33][33][33];\nint father[30 * 30 * 30 * 30 + 50];\n\nint BFS(int a1, int a2, int a3, int a4) {\n\tint head = 0;\n\tint tail = 1;\n\tque[1].a[1] = a1;\n\tque[1].a[2] = a2;\n\tque[1].a[3] = a3;\n\tque[1].a[4] = a4;\n\td[a1][a2][a3][a4] = 1;\n\twhile (head != tail) {\n\t\tnode cur = que[++head];\n\t\tfor (int i = 1; i <= n; i ++) \n\t\t\tfor (int j = 1; j <= 4; j ++) {\n\t\t\t\tnode tmp = cur;\n\t\t\t\ttmp.a[j] = i;\n\t\t\t\tif (flag[tmp.a[1]][tmp.a[2]][tmp.a[3]][tmp.a[4]] && (!d[tmp.a[1]][tmp.a[2]][tmp.a[3]][tmp.a[4]])) {\n\t\t\t\t\td[tmp.a[1]][tmp.a[2]][tmp.a[3]][tmp.a[4]] = d[cur.a[1]][cur.a[2]][cur.a[3]][cur.a[4]] + 1;\n\t\t\t\t\tque[++tail] = tmp;\n\t\t\t\t\tfather[tail] = head;\n\t\t\t\t\tif (i == n) {\n\t\t\t\t\t\t/*\n\t\t\t\t\t\twhile (father[tail]) {\n\t\t\t\t\t\t\tfor (int k = 1; k <= 4; k ++) printf(\"%d \", que[tail].a[k]);\n\t\t\t\t\t\t\tputs(\"\");\n\t\t\t\t\t\t\ttail = father[tail];\n\t\t\t\t\t\t}\n\t\t\t\t\t\tfor (int k = 1; k <= 4; k ++) printf(\"%d \", que[tail].a[k]);\n\t\t\t\t\t\tputs(\"\");\n\t\t\t\t\t\t*/\n\t\t\t\t\t\treturn d[cur.a[1]][cur.a[2]][cur.a[3]][cur.a[4]];\t\t\t\t\t\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t}\n\treturn -1;\n}\n\nint main() {\n\t\n\t//freopen(\"1.in\", \"r\", stdin);\n\t//freopen(\"1.out\", \"w\", stdout);\n\t\n\twhile (scanf(\"%d\", &n) == 1) {\n\t//scanf(\"%d\", &n);\n\t\tscanf(\"%d%d%d\", &body, &arm, &leg);\n\t\tfor (int i = 1; i <= n; i ++) {\n\t\t\tint x, y;\n\t\t\tscanf(\"%d%d\", &x, &y);\n\t\t\ta[i] = point((double)x, (double)y);\n\t\t}\t\n\t\tfor (int i = 1; i <= n; i ++)\n\t\t\tfor (int j = 1; j <= n; j ++) \n\t\t\t\tfor (int k = 1; k <= n; k ++)\n\t\t\t\t\tfor (int l = 1; l <= n; l ++) {\n\t\t\t\t\t\tflag[i][j][k][l] = 0;\n\t\t\t\t\t\td[i][j][k][l] = 0;\n\t\t\t\t\t}\n\t\tfor (int i = 1; i <= n; i ++)\n\t\t\tfor (int j = 1; j <= n; j ++) if (i != j)\n\t\t\t\tfor (int k = 1; k <= n; k ++) if (k != j && k != i)\n\t\t\t\t\tfor (int l = 1; l <= n; l ++) if (l != i && l != j && l != k)\n\t\t\t\t\t\tif (check(i, j, k, l)) {\n\t\t\t\t\t\t\tflag[i][j][k][l] = 1;\n\t\t\t\t\t\t}\n\t\tint ans = BFS(2, 1, 4, 3);\n\t\tprintf(\"%d\\n\", ans);\n\t}\n\t\n\t//fclose(stdin);\n\t//fclose(stdout);\n\t\n\treturn 0;\n}", "accuracy": 0.08, "time_ms": 230, "memory_kb": 9568, "score_of_the_acc": -0.038, "final_rank": 19 }, { "submission_id": "aoj_2227_1543463", "code_snippet": "#include <cstdio>\n#include <iostream>\n#include <cmath>\n\nusing namespace std;\n\nconst double eps = 1e-8;\n\ninline int sign(double x) {\n\treturn x < -eps ? -1 : x > eps;\n}\n\ninline double sqr(double x) {\n\treturn x * x;\n}\n\nstruct point {\n\tdouble x, y;\n\tpoint(double x = 0, double y = 0) : x(x), y(y) {}\n\tinline double length() const {\n\t\treturn sqrt(x * x + y * y);\n\t}\n\tinline double norm() const {\n\t\treturn length();\n\t}\n\tinline double norm2() const {\n\t\treturn x * x + y * y;\n\t}\n\tinline point unit() const {\n\t\tdouble len = length();\n\t\treturn point(x / len, y / len);\n\t}\n\tinline point negate() const {\n\t\treturn point(-x, -y);\n\t}\n\tinline point rot90() const {\n\t\treturn point(-y, x);\n\t}\n\tinline point _rot90() const {\n\t\treturn point(y, -x);\n\t}\n\tinline point rotate(double theta) const {\n\t\tdouble c = cos(theta), s = sin(theta);\n\t\treturn point(x * c - y * s, x * s + y * c);\n\t}\n};\n\ninline bool operator== (const point &a, const point &b) {\n\treturn fabs(a.x - b.x) < eps && fabs(a.y - b.y) < eps;\n}\n\ninline bool operator!= (const point &a, const point &b) {\n\treturn fabs(a.x - b.x) > eps || fabs(a.y - b.y) > eps;\n}\n\ninline bool operator< (const point &a, const point &b) {\n\tif (fabs(a.x - b.x) > eps) return a.x < b.x;\n\treturn a.y + eps < b.y;\n}\n\ninline point operator+ (const point &a, const point &b) {\n\treturn point(a.x + b.x, a.y + b.y);\n}\n\ninline point operator- (const point &a, const point &b) {\n\treturn point(a.x - b.x, a.y - b.y);\n}\n\ninline point operator* (const point &a, const double &b) {\n\treturn point(a.x * b, a.y * b);\n}\n\ninline point operator/ (const point &a, const double &b) {\n\treturn point(a.x / b, a.y / b);\n}\n\ninline double det(const point &a, const point &b) {\n\treturn a.x * b.y - b.x * a.y;\n}\n\ninline double dot(const point &a, const point &b) {\n\treturn a.x * b.x + a.y * b.y;\n}\n\ninline double dis(const point &a, const point &b) {\n\treturn sqrt(sqr(a.x - b.x) + sqr(a.y - b.y));\n}\n\nstruct line {\n\tpoint s, t;\n\tline(point s = point(), point t = point()) : s(s), t(t) {}\n\tinline double length() const {\n\t\treturn dis(s, t);\n\t}\n};\n\ndouble point_to_line(const point &p, const line &l) {\n\treturn fabs(det(l.t - l.s, p - l.s)) / dis(l.s, l.t);\n}\n\npoint project_to_line(const point &p, const line & l) {\n\treturn l.s + (l.t - l.s) * (dot(p - l.s, l.t - l.s) / (l.t - l.s).norm2());\n}\n\nstruct circle {\n\tpoint center;\n\tdouble radius;\n\tcircle(point center = point(), double radius = 0) : center(center), radius(radius) {}\n};\n\ninline bool operator== (const circle &a, const circle &b) {\n\treturn a.center == b.center && fabs(a.radius - b.radius) < eps;\n}\n\ninline bool operator!= (const circle &a, const circle &b) {\n\treturn a.center != b.center || fabs(a.radius - b.radius) > eps;\n}\n\ninline bool in_circle(const point &p, const circle &c) {\n\treturn dis(p, c.center) < c.radius + eps;\n}\n\npoint line_circle_intersect(const line &l, const circle &c) {\n\tdouble x = sqrt(sqr(c.radius) - sqr(point_to_line(c.center, l)));\n\treturn project_to_line(c.center, l) + (l.s - l.t).unit() * x;\n}\n\npoint circle_intersect(const circle &a, const circle &b) {\n\tpoint r = (b.center - a.center).unit();\n\tdouble d = dis(a.center, b.center);\n\tdouble x = .5 * ((sqr(a.radius) - sqr(b.radius)) / d + d);\n\tdouble h = sqrt(sqr(a.radius) - sqr(x));\n\treturn a.center + r * x + r.rot90() * h;\n}\n\ninline bool point_on_line(const point &a, const line &b) {\n\treturn sign(det(a - b.s, b.t - b.s)) == 0 && dot(b.s - a, b.t - a) < eps;\n}\n\ninline bool two_side(const point &a, const point &b, const line &c) {\n\treturn sign(det(a - c.s, c.t - c.s)) * sign(det(b - c.s, c.t - c.s)) < 0;\n}\n\ninline bool intersect_judgement(const line &a, const line &b) {\n\tif (point_on_line(b.s, a) || point_on_line(b.t, a)) return true;\n\tif (point_on_line(a.s, b) || point_on_line(a.t, b)) return true;\n\treturn two_side(a.s, a.t, b) && two_side(b.s, b.t, a);\n}\n\nint n, arm, leg, body;\npoint a[105];\nbool flag[33][33][33][33];\n\nbool in_points(point p1, point p2, point p3, point cent) {\n\tdouble vec1 = det(p1 - cent, p2 - cent);\n\tdouble vec2 = det(p1 - cent, p3 - cent);\n\treturn (vec1 > -eps && vec2 < eps); \n}\n\nbool check(int a1, int a2, int b1, int b2) {\n\tpoint arm_c[3], leg_c[3];\n\tpoint p[3], q[3];\n\tarm_c[1] = a[a1]; \n\tarm_c[2] = a[a2]; \n\tleg_c[1] = a[b1]; \n\tleg_c[2] = a[b2]; \n\tif (dis(a[a1], a[a2]) > arm * 2. + eps) return false;\n\tif (dis(a[b1], a[b2]) > leg * 2. + eps) return false;\n\tp[1] = circle_intersect(circle(arm_c[1], arm), circle(arm_c[2], arm));\n\tp[2] = circle_intersect(circle(arm_c[2], arm), circle(arm_c[1], arm));\n\tq[1] = circle_intersect(circle(leg_c[1], leg), circle(leg_c[2], leg));\n\tq[2] = circle_intersect(circle(leg_c[2], leg), circle(leg_c[1], leg));\n\tdouble mindis = 1e9;\n\tfor (int i = 1; i <= 2; i ++)\n\t\tfor (int j = 1; j <= 2; j ++) \n\t\t\tmindis = min(mindis, dis(p[i], q[j]));\n\tfor (int i = 1; i <= 2; i ++) {\n\t\tfor (int j = 1; j <= 2; j ++) {\n\t\t\tif (in_circle(p[i], circle(leg_c[j], leg))) continue;\n\t\t\tif (in_circle(p[i], circle(leg_c[j], leg)) && in_circle(p[i], circle(leg_c[3 - j], leg))) return 1;\n\t\t\tpoint leg_near = p[i] + (leg_c[j] - p[i]).unit() * (dis(p[i], leg_c[j]) - leg);\n\t\t\tif (in_circle(leg_near, circle(leg_c[3 - j], leg))) mindis = min(mindis, dis(p[i], leg_c[j]) - leg);\n\t\t\t//if (in_points(leg_near, q[j], q[3 - j], leg_c[j])) mindis = min(mindis, dis(p[i], leg_c[j]) - leg);\n\t\t}\n\t}\n\tfor (int i = 1; i <= 2; i ++) \n\t\tfor (int j = 1; j <= 2; j ++) {\n\t\t\tif (in_circle(q[i], circle(arm_c[j], arm))) continue;\n\t\t\tif (in_circle(q[i], circle(arm_c[j], arm)) && in_circle(q[i], circle(arm_c[3 - j], arm))) return 1;\n\t\t\tpoint arm_near = q[i] + (arm_c[j] - q[i]).unit() * (dis(q[i], arm_c[j]) - arm);\n\t\t\tif (in_circle(arm_near, circle(arm_c[3 - j], arm))) mindis = min(mindis, dis(q[i], arm_c[j]) - arm);\n\t\t\t//if (in_points(arm_near, p[j], p[3 - j], arm_c[j])) mindis = min(mindis, dis(q[i], arm_c[j]) - arm);\n\t\t}\n\tfor (int i = 1; i <= 2; i ++)\n\t\tfor (int j = 1; j <= 2; j ++) {\n\t\t\tpoint pp = arm_c[i] + (leg_c[j] - arm_c[i]).unit() * arm;\n\t\t\tpoint qq = leg_c[j] + (arm_c[i] - leg_c[j]).unit() * leg;\t\t\t\n\t\t\tif (in_points(pp, p[i], p[3 - i], arm_c[i]) && in_points(qq, q[j], q[3 - j], leg_c[j])) {\n\t\t\t\tif (intersect_judgement(line(arm_c[i], pp), line(leg_c[j], qq)) || (dis(pp, qq) < body + eps)) return 1;\n\t\t\t}\n\t\t}\n\treturn mindis < body + eps;\n}\n\nstruct node {\n\tint a[5];\n} que[30 * 30 * 30 * 30 + 50];\n\nint d[33][33][33][33];\nint father[30 * 30 * 30 * 30 + 50];\n\nint BFS(int a1, int a2, int a3, int a4) {\n\tint head = 0;\n\tint tail = 1;\n\tque[1].a[1] = a1;\n\tque[1].a[2] = a2;\n\tque[1].a[3] = a3;\n\tque[1].a[4] = a4;\n\td[a1][a2][a3][a4] = 1;\n\twhile (head != tail) {\n\t\tnode cur = que[++head];\n\t\tfor (int i = 1; i <= n; i ++) \n\t\t\tfor (int j = 1; j <= 4; j ++) {\n\t\t\t\tnode tmp = cur;\n\t\t\t\ttmp.a[j] = i;\n\t\t\t\tif (flag[tmp.a[1]][tmp.a[2]][tmp.a[3]][tmp.a[4]] && (!d[tmp.a[1]][tmp.a[2]][tmp.a[3]][tmp.a[4]])) {\n\t\t\t\t\td[tmp.a[1]][tmp.a[2]][tmp.a[3]][tmp.a[4]] = d[cur.a[1]][cur.a[2]][cur.a[3]][cur.a[4]] + 1;\n\t\t\t\t\tque[++tail] = tmp;\n\t\t\t\t\tfather[tail] = head;\n\t\t\t\t\tif (i == n) {\n\t\t\t\t\t\t/*\n\t\t\t\t\t\twhile (father[tail]) {\n\t\t\t\t\t\t\tfor (int k = 1; k <= 4; k ++) printf(\"%d \", que[tail].a[k]);\n\t\t\t\t\t\t\tputs(\"\");\n\t\t\t\t\t\t\ttail = father[tail];\n\t\t\t\t\t\t}\n\t\t\t\t\t\tfor (int k = 1; k <= 4; k ++) printf(\"%d \", que[tail].a[k]);\n\t\t\t\t\t\tputs(\"\");\n\t\t\t\t\t\t*/\n\t\t\t\t\t\treturn d[cur.a[1]][cur.a[2]][cur.a[3]][cur.a[4]];\t\t\t\t\t\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t}\n\treturn -1;\n}\n\nint main() {\n\t\n\twhile (scanf(\"%d\", &n) == 1) {\n\t//scanf(\"%d\", &n);\n\t\tscanf(\"%d%d%d\", &body, &arm, &leg);\n\t\tfor (int i = 1; i <= n; i ++) {\n\t\t\tint x, y;\n\t\t\tscanf(\"%d%d\", &x, &y);\n\t\t\ta[i] = point((double)x, (double)y);\n\t\t}\t\n\t\tfor (int i = 1; i <= n; i ++)\n\t\t\tfor (int j = 1; j <= n; j ++) \n\t\t\t\tfor (int k = 1; k <= n; k ++)\n\t\t\t\t\tfor (int l = 1; l <= n; l ++) {\n\t\t\t\t\t\tflag[i][j][k][l] = 0;\n\t\t\t\t\t\td[i][j][k][l] = 0;\n\t\t\t\t\t}\n\t\tcheck(1, 2, 4, 5);\n\t\tfor (int i = 1; i <= n; i ++)\n\t\t\tfor (int j = 1; j <= n; j ++) if (i != j)\n\t\t\t\tfor (int k = 1; k <= n; k ++) if (k != j && k != i)\n\t\t\t\t\tfor (int l = 1; l <= n; l ++) if (l != i && l != j && l != k)\n\t\t\t\t\t\tif (check(i, j, k, l)) {\n\t\t\t\t\t\t\tflag[i][j][k][l] = 1;\n\t\t\t\t\t\t}\n\t\tint ans = BFS(2, 1, 4, 3);\n\t\tprintf(\"%d\\n\", ans);\n\t}\n\t\n\treturn 0;\n}", "accuracy": 0.08, "time_ms": 230, "memory_kb": 9568, "score_of_the_acc": -0.038, "final_rank": 19 }, { "submission_id": "aoj_2227_1543412", "code_snippet": "#include <cstdio>\n#include <iostream>\n#include <cmath>\n\nusing namespace std;\n\nconst double eps = 1e-8;\n\ninline int sign(double x) {\n\treturn x < -eps ? -1 : x > eps;\n}\n\ninline double sqr(double x) {\n\treturn x * x;\n}\n\nstruct point {\n\tdouble x, y;\n\tpoint(double x = 0, double y = 0) : x(x), y(y) {}\n\tinline double length() const {\n\t\treturn sqrt(x * x + y * y);\n\t}\n\tinline double norm() const {\n\t\treturn length();\n\t}\n\tinline double norm2() const {\n\t\treturn x * x + y * y;\n\t}\n\tinline point unit() const {\n\t\tdouble len = length();\n\t\treturn point(x / len, y / len);\n\t}\n\tinline point negate() const {\n\t\treturn point(-x, -y);\n\t}\n\tinline point rot90() const {\n\t\treturn point(-y, x);\n\t}\n\tinline point _rot90() const {\n\t\treturn point(y, -x);\n\t}\n\tinline point rotate(double theta) const {\n\t\tdouble c = cos(theta), s = sin(theta);\n\t\treturn point(x * c - y * s, x * s + y * c);\n\t}\n};\n\ninline bool operator== (const point &a, const point &b) {\n\treturn fabs(a.x - b.x) < eps && fabs(a.y - b.y) < eps;\n}\n\ninline bool operator!= (const point &a, const point &b) {\n\treturn fabs(a.x - b.x) > eps || fabs(a.y - b.y) > eps;\n}\n\ninline bool operator< (const point &a, const point &b) {\n\tif (fabs(a.x - b.x) > eps) return a.x < b.x;\n\treturn a.y + eps < b.y;\n}\n\ninline point operator+ (const point &a, const point &b) {\n\treturn point(a.x + b.x, a.y + b.y);\n}\n\ninline point operator- (const point &a, const point &b) {\n\treturn point(a.x - b.x, a.y - b.y);\n}\n\ninline point operator* (const point &a, const double &b) {\n\treturn point(a.x * b, a.y * b);\n}\n\ninline point operator/ (const point &a, const double &b) {\n\treturn point(a.x / b, a.y / b);\n}\n\ninline double det(const point &a, const point &b) {\n\treturn a.x * b.y - b.x * a.y;\n}\n\ninline double dot(const point &a, const point &b) {\n\treturn a.x * b.x + a.y * b.y;\n}\n\ninline double dis(const point &a, const point &b) {\n\treturn sqrt(sqr(a.x - b.x) + sqr(a.y - b.y));\n}\n\nstruct line {\n\tpoint s, t;\n\tline(point s = point(), point t = point()) : s(s), t(t) {}\n\tinline double length() const {\n\t\treturn dis(s, t);\n\t}\n};\n\ndouble point_to_line(const point &p, const line &l) {\n\treturn fabs(det(l.t - l.s, p - l.s)) / dis(l.s, l.t);\n}\n\npoint project_to_line(const point &p, const line & l) {\n\treturn l.s + (l.t - l.s) * (dot(p - l.s, l.t - l.s) / (l.t - l.s).norm2());\n}\n\nstruct circle {\n\tpoint center;\n\tdouble radius;\n\tcircle(point center = point(), double radius = 0) : center(center), radius(radius) {}\n};\n\ninline bool operator== (const circle &a, const circle &b) {\n\treturn a.center == b.center && fabs(a.radius - b.radius) < eps;\n}\n\ninline bool operator!= (const circle &a, const circle &b) {\n\treturn a.center != b.center || fabs(a.radius - b.radius) > eps;\n}\n\ninline bool in_circle(const point &p, const circle &c) {\n\treturn dis(p, c.center) < c.radius + eps;\n}\n\npoint line_circle_intersect(const line &l, const circle &c) {\n\tdouble x = sqrt(sqr(c.radius) - sqr(point_to_line(c.center, l)));\n\treturn project_to_line(c.center, l) + (l.s - l.t).unit() * x;\n}\n\npoint circle_intersect(const circle &a, const circle &b) {\n\tpoint r = (b.center - a.center).unit();\n\tdouble d = dis(a.center, b.center);\n\tdouble x = .5 * ((sqr(a.radius) - sqr(b.radius)) / d + d);\n\tdouble h = sqrt(sqr(a.radius) - sqr(x));\n\treturn a.center + r * x + r.rot90() * h;\n}\n\nint n, arm, leg, body;\npoint a[105];\nbool flag[33][33][33][33];\n\nbool in_points(point p1, point p2, point p3, point cent) {\n\tdouble vec1 = det(p1 - cent, p2 - cent);\n\tdouble vec2 = det(p1 - cent, p3 - cent);\n\treturn (vec1 > -eps && vec2 < eps); \n}\n\nbool check(int a1, int a2, int b1, int b2) {\n\tpoint arm_c[3], leg_c[3];\n\tpoint p[3], q[3];\n\tarm_c[1] = a[a1]; \n\tarm_c[2] = a[a2]; \n\tleg_c[1] = a[b1]; \n\tleg_c[2] = a[b2]; \n\tif (dis(a[a1], a[a2]) > arm * 2. + eps) return false;\n\tif (dis(a[b1], a[b2]) > leg * 2. + eps) return false;\n\tp[1] = circle_intersect(circle(arm_c[1], arm), circle(arm_c[2], arm));\n\tp[2] = circle_intersect(circle(arm_c[2], arm), circle(arm_c[1], arm));\n\tq[1] = circle_intersect(circle(leg_c[1], leg), circle(leg_c[2], leg));\n\tq[2] = circle_intersect(circle(leg_c[2], leg), circle(leg_c[1], leg));\n\tdouble mindis = 1e9;\n\tfor (int i = 1; i <= 2; i ++)\n\t\tfor (int j = 1; j <= 2; j ++) \n\t\t\tmindis = min(mindis, dis(p[i], q[j]));\n\tfor (int i = 1; i <= 2; i ++) \n\t\tfor (int j = 1; j <= 2; j ++) {\n\t\t\tif (in_circle(p[i], circle(leg_c[j], leg)) && in_circle(p[i], circle(leg_c[3 - j], leg))) return 1;\n\t\t\tpoint leg_near = p[i] + (leg_c[j] - p[i]).unit() * (dis(p[i], leg_c[j]) - leg);\n\t\t\tif (in_circle(leg_near, circle(leg_c[3 - j], leg))) mindis = min(mindis, dis(p[i], leg_c[j]) - leg);\n\t\t\t//if (in_points(leg_near, q[j], q[3 - j], leg_c[j])) mindis = min(mindis, dis(p[i], leg_c[j]) - leg);\n\t\t}\n\tfor (int i = 1; i <= 2; i ++) \n\t\tfor (int j = 1; j <= 2; j ++) {\n\t\t\tif (in_circle(q[i], circle(arm_c[j], arm)) && in_circle(q[i], circle(arm_c[3 - j], arm))) return 1;\n\t\t\tpoint arm_near = q[i] + (arm_c[j] - q[i]).unit() * (dis(q[i], arm_c[j]) - arm);\n\t\t\tif (in_circle(arm_near, circle(arm_c[3 - j], arm))) mindis = min(mindis, dis(q[i], arm_c[j]) - arm);\n\t\t\t//if (in_points(arm_near, p[j], p[3 - j], arm_c[j])) mindis = min(mindis, dis(q[i], arm_c[j]) - arm);\n\t\t}\n\tfor (int i = 1; i <= 2; i ++)\n\t\tfor (int j = 1; j <= 2; j ++) {\n\t\t\tpoint pp = arm_c[i] + (leg_c[j] - arm_c[i]).unit() * arm;\n\t\t\tpoint qq = leg_c[j] + (arm_c[i] - leg_c[j]).unit() * leg;\n\t\t\t//if (in_points(pp, p[i], p[3 - i], arm_c[i]) && in_points(qq, q[j], q[3 - j], leg_c[j]))\n\t\t\tif (in_circle(pp, circle(arm_c[3 - i], arm)) && in_circle(qq, circle(leg_c[3 - j], leg)))\n\t\t\t\tmindis = min(mindis, dis(pp, qq));\n\t\t}\n\treturn mindis < body + eps;\n}\n\nstruct node {\n\tint a[5];\n} que[30 * 30 * 30 * 30 + 50];\n\nint d[33][33][33][33];\nint father[30 * 30 * 30 * 30 + 50];\n\nint BFS(int a1, int a2, int a3, int a4) {\n\tint head = 0;\n\tint tail = 1;\n\tque[1].a[1] = a1;\n\tque[1].a[2] = a2;\n\tque[1].a[3] = a3;\n\tque[1].a[4] = a4;\n\td[a1][a2][a3][a4] = 1;\n\twhile (head != tail) {\n\t\tnode cur = que[++head];\n\t\tfor (int i = 1; i <= n; i ++) \n\t\t\tfor (int j = 1; j <= 4; j ++) {\n\t\t\t\tnode tmp = cur;\n\t\t\t\ttmp.a[j] = i;\n\t\t\t\tif (flag[tmp.a[1]][tmp.a[2]][tmp.a[3]][tmp.a[4]] && (!d[tmp.a[1]][tmp.a[2]][tmp.a[3]][tmp.a[4]])) {\n\t\t\t\t\td[tmp.a[1]][tmp.a[2]][tmp.a[3]][tmp.a[4]] = d[cur.a[1]][cur.a[2]][cur.a[3]][cur.a[4]] + 1;\n\t\t\t\t\tque[++tail] = tmp;\n\t\t\t\t\tfather[tail] = head;\n\t\t\t\t\tif (i == n) return d[cur.a[1]][cur.a[2]][cur.a[3]][cur.a[4]];\t\t\t\t\t\n\t\t\t\t}\n\t\t\t}\n\t}\n\treturn -1;\n}\n\nint main() {\n\tscanf(\"%d\", &n);\n\tscanf(\"%d%d%d\", &body, &arm, &leg);\n\tfor (int i = 1; i <= n; i ++) {\n\t\tint x, y;\n\t\tscanf(\"%d%d\", &x, &y);\n\t\ta[i] = point((double)x, (double)y);\n\t}\t\n\tfor (int i = 1; i <= n; i ++)\n\t\tfor (int j = 1; j <= n; j ++) if (i != j)\n\t\t\tfor (int k = 1; k <= n; k ++) if (k != j && k != i)\n\t\t\t\tfor (int l = 1; l <= n; l ++) if (l != i && l != j && l != k)\n\t\t\t\t\tif (check(i, j, k, l)) {\n\t\t\t\t\t\tflag[i][j][k][l] = 1;\n\t\t\t\t\t}\n\tint ans = BFS(2, 1, 4, 3);\n\tprintf(\"%d\\n\", ans);\n\treturn 0;\n}", "accuracy": 0.08, "time_ms": 110, "memory_kb": 9528, "score_of_the_acc": -0.0182, "final_rank": 18 }, { "submission_id": "aoj_2227_1461754", "code_snippet": "#include<cstdio>\n#include<cmath>\n#include<utility>\n#include<vector>\n#include<queue>\n#include<complex>\n#include<iostream>\n#include<set>\n#include<algorithm>\n\nusing namespace std;\n\ntypedef double Real;\ntypedef complex<Real> Point;\n\nconst Real PI=acos(-1.0);\nconst Real eps=1e-10;\n\ntemplate<class T> bool eq(T a,T b){\n\treturn abs(a-b)<eps;\n}\ntemplate<class T> int sgn(T a){\n\tif(eq(a,(T)0.0)) return 0;\n\tif(a>0) return 1;\n\treturn -1;\n}\n\nvoid print(Point p,char ch='\\n'){\n\tprintf(\"(%f %f)%c\",p.real(),p.imag(),ch);\n}\n\nPoint getPoint(Point c,Real r,Real ang){\n\treturn c+r*Point(cos(ang),sin(ang));\n}\n\nReal arg(Point p){\n\treturn atan2(p.imag(),p.real());\n}\n\nstruct Sector:pair<Real,Real> {\n\tPoint c;\n\tReal r;\n\tSector(){}\n\tSector(Point c,Real r):c(c),r(r){}\n\tSector(Point c,Real r,Real a,Real b){\n\t\tthis->c=c;\n\t\tthis->r=r;\n\t\tthis->first=a;\n\t\tthis->second=b;\n\t}\n};\n\nvoid print(Sector sec){\n\tprint(sec.c);\n\tprintf(\"%f %f %f\\n\",sec.r,sec.first,sec.second);\n}\n\nstruct Circle:vector<Real> {\n\tPoint c;\n\tReal r;\n\tCircle(){}\n\tCircle(Point c,Real r):c(c),r(r){}\n\tCircle(Sector s):c(s.c),r(s.r){}\n};\n\nReal normalize(Real t){\n\twhile(t<-PI) t+=PI*2;\n\twhile(t>PI) t-=PI*2;\n\treturn t;\n}\n\nvoid toInf(pair<Real,Real> &p){\n\tReal ang=p.second-p.first;\n\tif(ang<0) ang+=PI*2;\n\tif(ang>=PI) swap(p.first,p.second);\n}\n\nbool inRange(Real l,Real r,Real x){\n\tif(r<l) r+=PI*2;\n\tif(sgn(x-l)>=0&&sgn(r-x)>=0) return true;\n\tif(sgn(x+PI*2-l)>=0&&sgn(r-x-PI*2)>=0) return true;\n\treturn false;\n}\n\nvoid iCC(Circle &c,Circle &c2){\n\tif(eq(c.c,c2.c)) return;\n\tReal d=abs(c.c-c2.c);\n\tReal r=c.r,r2=c2.r;\n\tif(sgn(d-(r+r2))>0) return;\n\telse if(sgn(d-(r+r2))==0){//out tangent\n\t\tReal ang=normalize(arg(c2.c-c.c));\n\t\tc.push_back(ang);\n\t}else if(sgn(d-abs(r-r2))>0){//intersect two points\n\t\tReal ang=arg(c2.c-c.c);\n\t\tReal ang2=acos((r*r+d*d-r2*r2)/(2*r*d));\n\t\tc.push_back(normalize(ang+ang2));\n\t\tc.push_back(normalize(ang-ang2));\n\t}else if(sgn(d-abs(r-r2))==0){//in tangent\n\t\tif(r>r2){\n\t\t\tc.push_back(normalize(arg(c2.c-c.c)));\n\t\t}else{\n\t\t\tc.push_back(normalize(-arg(c2.c-c.c)));\n\t\t}\n\t}else{//contained\n\t\treturn;\n\t}\n}\n\nbool iScSc_(Sector s1,Sector s2){\n\tif(eq(s1.c,s2.c)){\n\t\tif(!eq(s1.r,s2.r)) return false;\n\t\tif(inRange(s1.first,s1.second,s2.first)) return true;\n\t\tif(inRange(s1.first,s1.second,s2.second)) return true;\n\t\tif(inRange(s2.first,s2.second,s1.first)) return true;\n\t\tif(inRange(s2.first,s2.second,s1.second)) return true;\n\t\treturn false;\n\t}\n\tCircle c1(s1);\n\tCircle c2(s2);\n//\tprint(s1);\n//\tprint(s2);\n\tiCC(c1,c2);\n\tiCC(c2,c1);\n\tfor(int i=0;i<c1.size();i++){\n\t\tif(inRange(s1.first,s1.second,c1[i])){\n\t\t\tPoint p=getPoint(s1.c,s1.r,c1[i]);\n\t\t\tReal ang=arg(p-c2.c);\n\t\t\tif(inRange(s2.first,s2.second,ang)){\n//\t\t\t\tprint(getPoint(s2.c,s2.r,ang));\n\t\t\t\treturn true;\n\t\t\t}\n\t\t}\n\t}\n\treturn false;\n}\n\nbool inSc_(Sector s,Point p){\n\tif(eq(s.c,p)) return true;\n\tReal d=abs(p-s.c);\n\tif(sgn(d-s.r)>0) return false;\n\tReal ang=normalize(arg(p-s.c));\n\tif(inRange(s.first,s.second,ang)) return true;\n\treturn false;\n}\n\nReal A,B,C;\n\nbool check2(Point p1,Point p2,Point q1,Point q2){//p1==p2\n\tif(eq(q1,q2)) return false;\n\tSector s=Sector(p1,A+B,-PI,PI);\n\tCircle c1,c2;\n\tc1=Circle(q1,C);\n\tc2=Circle(q2,C);\n\tiCC(c1,c2);\n\tiCC(c2,c1);\n\tif(c1.size()<2) return false;\n\tSector s1=Sector(c1.c,c1.r,c1[0],c1[1]);\n\tSector s2=Sector(c2.c,c2.r,c2[0],c2[1]);\n\ttoInf(s1);\n\ttoInf(s2);\n\tif(iScSc_(s,s1)) return true;\n\tif(iScSc_(s,s2)) return true;\n\tif(inSc_(s,getPoint(s1.c,s1.r,s1.first))) return true;\n\treturn false;\n}\n\nbool check3(Point p1,Point p2,Point q1,Point q2){//q1==q2\n\tif(eq(p1,p2)) return false;\n\tCircle c1=Circle(p1,B);\n\tCircle c2=Circle(p2,B);\n\tiCC(c1,c2);\n\tiCC(c2,c1);\n\tif(c1.size()<2) return false;\n\tSector scs[4];\n\tscs[0]=Sector(c1.c,c1.r+A,c1[0],c1[1]);\n\tscs[1]=Sector(c2.c,c2.r+A,c2[0],c2[1]);\n\tPoint i1=getPoint(c1.c,c1.r,c1[0]);\n\tscs[2]=Sector(i1,A,arg(i1-c1.c),arg(i1-c2.c));\n\tPoint i2=getPoint(c1.c,c1.r,c1[1]);\n\tscs[3]=Sector(i2,A,arg(i2-c1.c),arg(i2-c2.c));\n\tfor(int i=0;i<4;i++) toInf(scs[i]);\n\tSector s=Sector(q1,C,-PI,PI);\n\tfor(int i=0;i<4;i++){\n\t\tif(iScSc_(scs[i],s)) return true;\n\t}\n\tfor(int i=0;i<4;i++){\n\t\tPoint p=getPoint(s.c,s.r,s.first);\n\t\tif(inSc_(scs[i],p)) return true;\n\t}\n\treturn false;\n}\n\nbool check(Point p1,Point p2,Point q1,Point q2){\n//\tcout<<\"check\"<<endl;\n\tif(eq(p1,p2)) return check2(p1,p2,q1,q2);\n\tif(eq(q1,q2)) return check3(p1,p2,q1,q2);\n\tCircle c1=Circle(p1,B);\n\tCircle c2=Circle(p2,B);\n\tiCC(c1,c2);\n\tiCC(c2,c1);\n\tif(c1.size()<2) return false;\n\tSector scs[4];\n\tscs[0]=Sector(c1.c,c1.r+A,c1[0],c1[1]);\n\tscs[1]=Sector(c2.c,c2.r+A,c2[0],c2[1]);\n\tPoint i1=getPoint(c1.c,c1.r,c1[0]);\n\tscs[2]=Sector(i1,A,arg(i1-c1.c),arg(i1-c2.c));\n\tPoint i2=getPoint(c1.c,c1.r,c1[1]);\n\tscs[3]=Sector(i2,A,arg(i2-c1.c),arg(i2-c2.c));\n\tfor(int i=0;i<4;i++) toInf(scs[i]);\n\t{\n\t\tPoint tmp=getPoint(scs[0].c,scs[0].r,scs[0].first);\n\t\tReal d1=abs(tmp-q1);\n\t\tReal d2=abs(tmp-q2);\n\t\tif(sgn(d1-C)<=0&&sgn(d2-C)<=0){\n\t\t\treturn true;\n\t\t}\n\t}\n\tc1.clear();\n\tc2.clear();\n\tc1=Circle(q1,C);\n\tc2=Circle(q2,C);\n\tiCC(c1,c2);\n\tiCC(c2,c1);\n\tif(c1.size()<2) return false;\n\tSector s1=Sector(c1.c,c1.r,c1[0],c1[1]);\n\tSector s2=Sector(c2.c,c2.r,c2[0],c2[1]);\n\ttoInf(s1);\n\ttoInf(s2);\n\tfor(int i=0;i<4;i++){\n\t\tbool flg=iScSc_(scs[i],s1);\n\t\tif(flg) return true;\n\t\tflg=iScSc_(scs[i],s2);\n\t\tif(flg) return true;\n\t}\n\tPoint tmp=getPoint(c1.c,c1.r,c1[0]);\n\tfor(int i=2;i<4;i++){\n\t\tif(inSc_(scs[i],tmp)) return true;\n\t}\n\tfor(int i=0;i<2;i++){\n\t\tif(inSc_(scs[i],tmp)){\n\t\t\tif(inSc_(scs[i^1],tmp)) return true;\n\t\t\tReal d=abs(scs[i].c-tmp);\n\t\t\tif(d>B) return true;\n\t\t}\n\t}\n\treturn false;\n}\n\nPoint pts[33];\nint N;\n\nint dis[33][33][33][33];\nconst int iinf=1e9;\n\nvoid precalc(){\n//\tcheck(pts[0],pts[5],pts[1],pts[3]);\n\t//exit(0);\n\tfor(int i=0;i<N;i++){\n\t\tfor(int j=0;j<N;j++){\n\t\t\tfor(int k=0;k<N;k++){\n\t\t\t\tfor(int l=0;l<N;l++){\n\t\t\t\t\tbool flg=check(pts[i],pts[j],pts[k],pts[l]);\n\t\t\t\t\tif(flg) dis[i][j][k][l]=-1;\n\t\t\t\t\telse dis[i][j][k][l]=iinf;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\n\nint cnt(int i,int j,int k,int l){\n\tset<int> se;\n\tse.insert(i);\n\tse.insert(j);\n\tse.insert(k);\n\tse.insert(l);\n\treturn se.size();\n}\n\nstruct State{\n\tint i,j,k,l;\n\tState(){}\n\tState(int i,int j,int k,int l):i(i),j(j),k(k),l(l){\n//\t\tif(cnt(i,j,k,l)<4) i=-1;\n\t\tif(i==j||i==k||i==l||j==k||j==l||k==l) i=-1;\n\t}\n};\n\nbool isValid(State st){\n\tint i=st.i,j=st.j,k=st.k,l=st.l;\n\tif(i==j||i==k||i==l||j==k||j==l||k==l) return false;\n//\tif(cnt(i,j,k,l)<4) return false;\n\tif(dis[i][j][k][l]==iinf) return false;\n\treturn true;\n}\n\nint cnt2(State st1,State st2){\n\treturn 3;\n/*\tset<int> se;\n\tif(st1.i==st2.i) se.insert(st1.i);\n\tif(st1.j==st2.j) se.insert(st1.j);\n\tif(st1.k==st2.k) se.insert(st1.k);\n\tif(st1.l==st2.l) se.insert(st1.l);\n\treturn se.size();*/\n}\n\nState getNxt(State st,int a,int b){\n\tState ini=st;\n\tif(a==0) st.i=b;\n\telse if(a==1) st.j=b;\n\telse if(a==2) st.k=b;\n\telse st.l=b;\n\tint c=cnt2(ini,st);\n\tif(c<3) st.i=-1;\n\treturn st;\n}\n\nqueue<State> que;\nint bfs(){\n\twhile(!que.empty()) que.pop();\n\tque.push(State(0,1,2,3));\n\tdis[0][1][2][3]=0;\n\twhile(!que.empty()){\n\t\tState st=que.front();\n\t\tque.pop();\n\t\tif(st.i==-1) continue;\n\t\tint cur=dis[st.i][st.j][st.k][st.l];\n\t\tfor(int i=0;i<4;i++){\n\t\t\tfor(int j=0;j<N;j++){\n\t\t\t\tState nst=getNxt(st,i,j);\n\t\t\t\tif(nst.i==-1) continue;\n\t\t\t\tbool flg=isValid(nst);\n\t\t\t\tif(!flg) continue;\n\t\t\t\tint nd=cur+1;\n\t\t\t\tint &nxt=dis[nst.i][nst.j][nst.k][nst.l];\n\t\t\t\tif(nxt!=-1&&nxt<=nd) continue;\n\t\t\t\tnxt=nd;\n\t\t\t\tque.push(nst);\n\t\t\t}\n\t\t}\n\t}\n\tint res=iinf;\n\tfor(int i=0;i<N;i++) for(int j=0;j<N;j++){\n\t\tfor(int k=0;k<N;k++) for(int l=0;l<N;l++){\n\t\t\tif(i==N-1||j==N-1||k==N-1||l==N-1){\n\t\t\t\tif(dis[i][j][k][l]!=-1){\n\t\t\t\t\tres=min(res,dis[i][j][k][l]);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tif(res==iinf) return -1;\n\treturn res;\n}\n\nvoid input(){\n\tscanf(\"%d\",&N);\n\tscanf(\"%lf%lf%lf\",&A,&B,&C);\n\tfor(int i=0;i<N;i++){\n\t\tint x,y;\n\t\tscanf(\"%d%d\",&x,&y);\n\t\tpts[i]=Point(x,y);\n\t}\n}\n\nint solve(){\n\tprecalc();\n\tint res=bfs();\n\treturn res;\n}\n\nint main(){\n\tinput();\n\tint ans=solve();\n\tprintf(\"%d\\n\",ans);\n\treturn 0;\n}", "accuracy": 1, "time_ms": 1680, "memory_kb": 13344, "score_of_the_acc": -0.3591, "final_rank": 2 }, { "submission_id": "aoj_2227_1460452", "code_snippet": "#include<cstdio>\n#include<cmath>\n#include<utility>\n#include<vector>\n#include<queue>\n#include<complex>\n#include<iostream>\n#include<algorithm>\n\nusing namespace std;\n\ntypedef double Real;\ntypedef complex<Real> Point;\n\nconst Real PI=acos(-1.0);\nconst Real eps=1e-9;\n\ntemplate<class T> bool eq(T a,T b){\n\treturn abs(a-b)<eps;\n}\ntemplate<class T> int sgn(T a){\n\tif(eq(a,0.0)) return 0;\n\tif(a>0) return 1;\n\treturn -1;\n}\n\nvoid print(Point p,char ch='\\n'){\n\tprintf(\"(%f %f)%c\",p.real(),p.imag(),ch);\n}\n\nPoint getPoint(Point c,Real r,Real ang){\n\treturn c+r*Point(cos(ang),sin(ang));\n}\n\nReal arg(Point p){\n\treturn atan2(p.imag(),p.real());\n}\n\nstruct Sector:pair<Real,Real> {\n\tPoint c;\n\tReal r;\n\tSector(){}\n\tSector(Point c,Real r):c(c),r(r){}\n\tSector(Point c,Real r,Real a,Real b){\n\t\tthis->c=c;\n\t\tthis->r=r;\n\t\tthis->first=a;\n\t\tthis->second=b;\n\t}\n};\n\nstruct Circle:vector<Real> {\n\tPoint c;\n\tReal r;\n\tCircle(){}\n\tCircle(Point c,Real r):c(c),r(r){}\n\tCircle(Sector s):c(s.c),r(s.r){}\n};\n\nReal normalize(Real t){\n\twhile(t<-PI) t+=PI*2;\n\twhile(t>PI) t-=PI*2;\n\treturn t;\n}\n\nvoid toInf(pair<Real,Real> &p){\n\tReal ang=p.second-p.first;\n\tif(ang<0) ang+=PI*2;\n\tif(ang>=PI) swap(p.first,p.second);\n}\n\nbool inRange(Real l,Real r,Real x){\n\tif(r<l) r+=PI*2;\n\tif(sgn(x-l)>=0&&sgn(r-x)>=0) return true;\n\tif(sgn(x+PI*2-l)>=0&&sgn(r-x-PI*2)>=0) return true;\n\treturn false;\n}\n\nvoid iCC(Circle &c,Circle &c2){\n\tif(eq(c.c,c2.c)) return;\n\tReal d=abs(c.c-c2.c);\n\tReal r=c.r,r2=c2.r;\n\tif(sgn(d-(r+r2))>0) return;\n\telse if(sgn(d-(r+r2))==0){//out tangent\n\t\tReal ang=normalize(arg(c2.c-c.c));\n\t\tc.push_back(ang);\n\t}else if(sgn(d-abs(r-r2))>0){//intersect two points\n\t\tReal ang=arg(c2.c-c.c);\n\t\tReal ang2=acos((r*r+d*d-r2*r2)/(2*r*d));\n\t\tc.push_back(normalize(ang+ang2));\n\t\tc.push_back(normalize(ang-ang2));\n\t}else if(sgn(d-abs(r-r2))==0){//in tangent\n\t\tif(r>r2){\n\t\t\tc.push_back(normalize(arg(c2.c-c.c)));\n\t\t}else{\n\t\t\tc.push_back(normalize(-arg(c2.c-c.c)));\n\t\t}\n\t}else{//contained\n\t\treturn;\n\t}\n}\n\nbool iScSc_(Sector s1,Sector s2){\n\tCircle c1(s1);\n\tCircle c2(s2);\n\tiCC(c1,c2);\n\tiCC(c2,c1);\n\tfor(int i=0;i<c1.size();i++){\n\t\tif(inRange(s1.first,s1.second,c1[i])){\n\t\t\tPoint p=getPoint(s1.c,s1.r,c1[i]);\n\t\t\tReal ang=arg(p-c2.c);\n\t\t\tif(inRange(s2.first,s2.second,ang)){\n\t\t\t\treturn true;\n\t\t\t}\n\t\t}\n\t}\n\treturn false;\n}\n\nbool inSc_(Sector s,Point p){\n\tif(eq(s.c,p)) return true;\n\tReal d=abs(p-s.c);\n\tif(sgn(d-s.r)>0) return false;\n\tReal ang=normalize(arg(p-s.c));\n\tif(inRange(s.first,s.second,ang)) return true;\n\treturn false;\n}\n\nReal A,B,C;\n\nvoid print(Sector sec){\n\tprint(sec.c);\n\tprintf(\"%f %f %f\\n\",sec.r,sec.first,sec.second);\n}\n\nbool check(Point p1,Point p2,Point q1,Point q2){\n//\tcout<<\"check\"<<endl;\n\tCircle c1=Circle(p1,B);\n\tCircle c2=Circle(p2,B);\n\tiCC(c1,c2);\n\tiCC(c2,c1);\n\tif(c1.size()<2) return false;\n\tSector scs[4];\n\tscs[0]=Sector(c1.c,c1.r+A,c1[0],c1[1]);\n\tscs[1]=Sector(c2.c,c2.r+A,c2[0],c2[1]);\n\tPoint i1=getPoint(c1.c,c1.r,c1[0]);\n\tscs[2]=Sector(i1,A,arg(i1-c1.c),arg(i1-c2.c));\n\tPoint i2=getPoint(c1.c,c1.r,c1[1]);\n\tscs[3]=Sector(i2,A,arg(i2-c1.c),arg(i2-c2.c));\n\tfor(int i=0;i<4;i++) toInf(scs[i]);\n\tc1.clear();\n\tc2.clear();\n\tc1=Circle(q1,C);\n\tc2=Circle(q2,C);\n\tiCC(c1,c2);\n\tiCC(c2,c1);\n\tif(c1.size()<2) return false;\n\tSector s1=Sector(c1.c,c1.r,c1[0],c1[1]);\n\tSector s2=Sector(c2.c,c2.r,c2[0],c2[1]);\n\ttoInf(s1);\n\ttoInf(s2);\n\tfor(int i=0;i<4;i++){\n\t\tbool flg=iScSc_(scs[i],s1);\n\t\tif(flg) return true;\n\t\tflg=iScSc_(scs[i],s2);\n\t\tif(flg) return true;\n\t}\n\tPoint tmp=getPoint(c1.c,c1.r,c1[0]);\n\tfor(int i=0;i<4;i++){\n\t\tif(inSc_(scs[i],tmp)) return true;\n\t}\n\treturn false;\n}\n\nPoint pts[33];\nint N;\n\nint dis[33][33][33][33];\nconst int iinf=1e9;\n\nvoid precalc(){\n\tfor(int i=0;i<N;i++){\n\t\tfor(int j=0;j<N;j++){\n\t\t\tfor(int k=0;k<N;k++){\n\t\t\t\tfor(int l=0;l<N;l++){\n\t\t\t\t\tbool flg=check(pts[i],pts[j],pts[k],pts[l]);\n\t\t\t\t\tif(flg) dis[i][j][k][l]=-1;\n\t\t\t\t\telse dis[i][j][k][l]=iinf;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\n\nstruct State{\n\tint i,j,k,l;\n\tState(){}\n\tState(int i,int j,int k,int l):i(i),j(j),k(k),l(l){\n\t\tif(i==j||i==k||i==l||j==k||j==l||k==l) i=-1;\n\t}\n};\n\nbool isValid(State st){\n\tint i=st.i,j=st.j,k=st.k,l=st.l;\n\tif(i==j||i==k||i==l||j==k||j==l||k==l) return false;\t\t\n\tif(dis[i][j][k][l]==iinf) return false;\n\treturn true;\n}\n\nState getNxt(State st,int a,int b){\n\tif(a==0) st.i=b;\n\telse if(a==1) st.j=b;\n\telse if(a==2) st.k=b;\n\telse st.l=b;\n\treturn st;\n}\n\nqueue<State> que;\nint bfs(){\n\twhile(!que.empty()) que.pop();\n\tque.push(State(0,1,2,3));\n\tdis[0][1][2][3]=0;\n\twhile(!que.empty()){\n\t\tState st=que.front();\n\t\tque.pop();\n\t\tif(st.i==-1) continue;\n\t\tint cur=dis[st.i][st.j][st.k][st.l];\n\t\tfor(int i=0;i<4;i++){\n\t\t\tfor(int j=0;j<N;j++){\n\t\t\t\tState nst=getNxt(st,i,j);\n\t\t\t\tbool flg=isValid(nst);\n\t\t\t\tif(!flg) continue;\n\t\t\t\tint nd=cur+1;\n\t\t\t\tint &nxt=dis[nst.i][nst.j][nst.k][nst.l];\n\t\t\t\tif(nxt!=-1&&nxt<=nd) continue;\n\t\t\t\tnxt=nd;\n\t\t\t\tque.push(nst);\n\t\t\t}\n\t\t}\n\t}\n\tint res=iinf;\n\tfor(int i=0;i<N;i++) for(int j=0;j<N;j++){\n\t\tfor(int k=0;k<N;k++) for(int l=0;l<N;l++){\n\t\t\tif(i==N-1||j==N-1||k==N-1||l==N-1){\n\t\t\t\tif(dis[i][j][k][l]!=-1){\n\t\t\t\tres=min(res,dis[i][j][k][l]);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tif(res==iinf) return -1;\n\treturn res;\n}\n\nvoid input(){\n\tscanf(\"%d\",&N);\n\tscanf(\"%lf%lf%lf\",&A,&B,&C);\n\tfor(int i=0;i<N;i++){\n\t\tint x,y;\n\t\tscanf(\"%d%d\",&x,&y);\n\t\tpts[i]=Point(x,y);\n\t}\n}\n\nint solve(){\n\tprecalc();\n\tint res=bfs();\n\treturn res;\n}\n\nint main(){\n\tinput();\n\tint ans=solve();\n\tprintf(\"%d\\n\",ans);\n\treturn 0;\n}", "accuracy": 0.64, "time_ms": 1870, "memory_kb": 12728, "score_of_the_acc": -0.3734, "final_rank": 8 }, { "submission_id": "aoj_2227_1460449", "code_snippet": "#include<cstdio>\n#include<cmath>\n#include<utility>\n#include<vector>\n#include<queue>\n#include<complex>\n#include<iostream>\n#include<algorithm>\n\nusing namespace std;\n\ntypedef double Real;\ntypedef complex<Real> Point;\n\nconst Real PI=acos(-1.0);\nconst Real eps=1e-8;\n\ntemplate<class T> bool eq(T a,T b){\n\treturn abs(a-b)<eps;\n}\ntemplate<class T> int sgn(T a){\n\tif(eq(a,0.0)) return 0;\n\tif(a>0) return 1;\n\treturn -1;\n}\n\nvoid print(Point p,char ch='\\n'){\n\tprintf(\"(%f %f)%c\",p.real(),p.imag(),ch);\n}\n\nPoint getPoint(Point c,Real r,Real ang){\n\treturn c+r*Point(cos(ang),sin(ang));\n}\n\nReal arg(Point p){\n\treturn atan2(p.imag(),p.real());\n}\n\nstruct Sector:pair<Real,Real> {\n\tPoint c;\n\tReal r;\n\tSector(){}\n\tSector(Point c,Real r):c(c),r(r){}\n\tSector(Point c,Real r,Real a,Real b){\n\t\tthis->c=c;\n\t\tthis->r=r;\n\t\tthis->first=a;\n\t\tthis->second=b;\n\t}\n};\n\nstruct Circle:vector<Real> {\n\tPoint c;\n\tReal r;\n\tCircle(){}\n\tCircle(Point c,Real r):c(c),r(r){}\n\tCircle(Sector s):c(s.c),r(s.r){}\n};\n\nReal normalize(Real t){\n\twhile(t<-PI) t+=PI*2;\n\twhile(t>PI) t-=PI*2;\n\treturn t;\n}\n\nvoid toInf(pair<Real,Real> &p){\n\tReal ang=p.second-p.first;\n\tif(ang<0) ang+=PI*2;\n\tif(ang>=PI) swap(p.first,p.second);\n}\n\nbool inRange(Real l,Real r,Real x){\n\tif(r<l) r+=PI*2;\n\tif(sgn(x-l)>=0&&sgn(r-x)>=0) return true;\n\tif(sgn(x+PI*2-l)>=0&&sgn(r-x-PI*2)>=0) return true;\n\treturn false;\n}\n\nvoid iCC(Circle &c,Circle &c2){\n\tif(eq(c.c,c2.c)) return;\n\tReal d=abs(c.c-c2.c);\n\tReal r=c.r,r2=c2.r;\n\tif(sgn(d-(r+r2))>0) return;\n\telse if(sgn(d-(r+r2))==0){//out tangent\n\t\tReal ang=normalize(arg(c2.c-c.c));\n\t\tc.push_back(ang);\n\t}else if(sgn(d-abs(r-r2))>0){//intersect two points\n\t\tReal ang=arg(c2.c-c.c);\n\t\tReal ang2=acos((r*r+d*d-r2*r2)/(2*r*d));\n\t\tc.push_back(normalize(ang+ang2));\n\t\tc.push_back(normalize(ang-ang2));\n\t}else if(sgn(d-abs(r-r2))==0){//in tangent\n\t\tif(r>r2){\n\t\t\tc.push_back(normalize(arg(c2.c-c.c)));\n\t\t}else{\n\t\t\tc.push_back(normalize(-arg(c2.c-c.c)));\n\t\t}\n\t}else{//contained\n\t\treturn;\n\t}\n}\n\nbool iScSc_(Sector s1,Sector s2){\n\tCircle c1(s1);\n\tCircle c2(s2);\n\tiCC(c1,c2);\n\tiCC(c2,c1);\n\tfor(int i=0;i<c1.size();i++){\n\t\tif(inRange(s1.first,s1.second,c1[i])){\n\t\t\tPoint p=getPoint(s1.c,s1.r,c1[i]);\n\t\t\tReal ang=arg(p-c2.c);\n\t\t\tif(inRange(s2.first,s2.second,ang)){\n\t\t\t\treturn true;\n\t\t\t}\n\t\t}\n\t}\n\treturn false;\n}\n\nbool inSc_(Sector s,Point p){\n\tif(eq(s.c,p)) return true;\n\tReal d=abs(p-s.c);\n\tif(sgn(d-s.r)>0) return false;\n\tReal ang=normalize(arg(p-s.c));\n\tif(inRange(s.first,s.second,ang)) return true;\n\treturn false;\n}\n\nReal A,B,C;\n\nvoid print(Sector sec){\n\tprint(sec.c);\n\tprintf(\"%f %f %f\\n\",sec.r,sec.first,sec.second);\n}\n\nbool check(Point p1,Point p2,Point q1,Point q2){\n//\tcout<<\"check\"<<endl;\n\tCircle c1=Circle(p1,B);\n\tCircle c2=Circle(p2,B);\n\tiCC(c1,c2);\n\tiCC(c2,c1);\n\tif(c1.size()<2) return false;\n\tSector scs[4];\n\tscs[0]=Sector(c1.c,c1.r+A,c1[0],c1[1]);\n\tscs[1]=Sector(c2.c,c2.r+A,c2[0],c2[1]);\n\tPoint i1=getPoint(c1.c,c1.r,c1[0]);\n\tscs[2]=Sector(i1,A,arg(i1-c1.c),arg(i1-c2.c));\n\tPoint i2=getPoint(c1.c,c1.r,c1[1]);\n\tscs[3]=Sector(i2,A,arg(i2-c1.c),arg(i2-c2.c));\n\tfor(int i=0;i<4;i++) toInf(scs[i]);\n\tc1.clear();\n\tc2.clear();\n\tc1=Circle(q1,C);\n\tc2=Circle(q2,C);\n\tiCC(c1,c2);\n\tiCC(c2,c1);\n\tif(c1.size()<2) return false;\n\tSector s1=Sector(c1.c,c1.r,c1[0],c1[1]);\n\tSector s2=Sector(c2.c,c2.r,c2[0],c2[1]);\n\ttoInf(s1);\n\ttoInf(s2);\n\tfor(int i=0;i<4;i++){\n\t\tbool flg=iScSc_(scs[i],s1);\n\t\tif(flg) return true;\n\t\tflg=iScSc_(scs[i],s2);\n\t\tif(flg) return true;\n\t}\n\tPoint tmp=getPoint(c1.c,c1.r,c1[0]);\n\tfor(int i=0;i<4;i++){\n\t\tif(inSc_(scs[i],tmp)) return true;\n\t}\n\treturn false;\n}\n\nPoint pts[33];\nint N;\n\nint dis[33][33][33][33];\nconst int iinf=1e9;\n\nvoid precalc(){\n\tfor(int i=0;i<N;i++){\n\t\tfor(int j=0;j<N;j++){\n\t\t\tfor(int k=0;k<N;k++){\n\t\t\t\tfor(int l=0;l<N;l++){\n\t\t\t\t\tbool flg=check(pts[i],pts[j],pts[k],pts[l]);\n\t\t\t\t\tif(flg) dis[i][j][k][l]=-1;\n\t\t\t\t\telse dis[i][j][k][l]=iinf;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\n\nstruct State{\n\tint i,j,k,l;\n\tState(){}\n\tState(int i,int j,int k,int l):i(i),j(j),k(k),l(l){\n\t\tif(i==j||i==k||i==l||j==k||j==l||k==l) i=-1;\n\t}\n};\n\nbool isValid(State st){\n\tint i=st.i,j=st.j,k=st.k,l=st.l;\n\tif(i==j||i==k||i==l||j==k||j==l||k==l) return false;\t\t\n\tif(dis[i][j][k][l]==iinf) return false;\n\treturn true;\n}\n\nState getNxt(State st,int a,int b){\n\tif(a==0) st.i=b;\n\telse if(a==1) st.j=b;\n\telse if(a==2) st.k=b;\n\telse st.l=b;\n\treturn st;\n}\n\nqueue<State> que;\nint bfs(){\n\twhile(!que.empty()) que.pop();\n\tque.push(State(0,1,2,3));\n\tdis[0][1][2][3]=0;\n\twhile(!que.empty()){\n\t\tState st=que.front();\n\t\tque.pop();\n\t\tif(st.i==-1) continue;\n\t\tint cur=dis[st.i][st.j][st.k][st.l];\n\t\tfor(int i=0;i<4;i++){\n\t\t\tfor(int j=0;j<N;j++){\n\t\t\t\tState nst=getNxt(st,i,j);\n\t\t\t\tbool flg=isValid(nst);\n\t\t\t\tif(!flg) continue;\n\t\t\t\tint nd=cur+1;\n\t\t\t\tint &nxt=dis[nst.i][nst.j][nst.k][nst.l];\n\t\t\t\tif(nxt!=-1&&nxt<=nd) continue;\n\t\t\t\tnxt=nd;\n\t\t\t\tque.push(nst);\n\t\t\t}\n\t\t}\n\t}\n\tint res=iinf;\n\tfor(int i=0;i<N;i++) for(int j=0;j<N;j++){\n\t\tfor(int k=0;k<N;k++) for(int l=0;l<N;l++){\n\t\t\tif(i==N-1||j==N-1||k==N-1||l==N-1){\n\t\t\t\tif(dis[i][j][k][l]!=-1){\n\t\t\t\tres=min(res,dis[i][j][k][l]);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tif(res==iinf) return -1;\n\treturn res;\n}\n\nvoid input(){\n\tscanf(\"%d\",&N);\n\tscanf(\"%lf%lf%lf\",&A,&B,&C);\n\tfor(int i=0;i<N;i++){\n\t\tint x,y;\n\t\tscanf(\"%d%d\",&x,&y);\n\t\tpts[i]=Point(x,y);\n\t}\n}\n\nint solve(){\n\tprecalc();\n\tint res=bfs();\n\treturn res;\n}\n\nint main(){\n\tinput();\n\tint ans=solve();\n\tprintf(\"%d\\n\",ans);\n\treturn 0;\n}", "accuracy": 0.64, "time_ms": 1870, "memory_kb": 12728, "score_of_the_acc": -0.3734, "final_rank": 8 } ]
aoj_2225_cpp
Problem D: Dungeon Wall In 2337, people are bored at daily life and have crazy desire of having extraordinary experience. These days,“Dungeon Adventure” is one of the hottest attractions, where brave adventurers risk their lives to kill the evil monsters and save the world. You are a manager of one of such dungeons. Recently you have been receiving a lot of complaints from soldiers who frequently enter your dungeon; they say your dungeon is too easy and they do not want to enter again. You are considering making your dungeon more difficult by increasing the distance between the entrance and the exit of your dungeon so that more monsters can approach the adventurers. The shape of your dungeon is a rectangle whose width is W and whose height is H . The dungeon is a grid which consists of W × H square rooms of identical size 1 × 1. The southwest corner of the dungeon has the coordinate (0; 0), and the northeast corner has ( W , H ). The outside of the dungeon is surrounded by walls, and there may be some more walls inside the dungeon in order to prevent adventurers from going directly to the exit. Each wall in the dungeon is parallel to either x -axis or y -axis, and both ends of each wall are at integer coordinates. An adventurer can move from one room to another room if they are adjacent vertically or horizontally and have no wall between. You would like to add some walls to make the shortest path between the entrance and the exit longer. However, severe financial situation only allows you to build at most only one wall of a unit length. Note that a new wall must also follow the constraints stated above. Furthermore, you need to guarantee that there is at least one path from the entrance to the exit. You are wondering where you should put a new wall to maximize the minimum number of steps between the entrance and the exit. Can you figure it out? Input W H N sx 0 sy 0 dx 0 dy 0 sx 1 sy 1 dx 1 dy 1 . . . ix iy ox oy The first line contains three integers: W , H and N (1 ≤ W , H ≤ 50, 0 ≤ N ≤ 1000). They are separated with a space. The next N lines contain the specifications of N walls inside the dungeon. Each wall specification is a line containing four integers. The i -th specification contains sx i , sy i , dx i and dy i , separated with a space. These integers give the position of the i -th wall: it spans from ( sx i , sy i ) to ( dx i , dy i ). The last two lines contain information about the locations of the entrance and the exit. The first line contains two integers ix and iy , and the second line contains two integers ox and oy . The entrance is located at the room whose south-west corner is ( ix , iy ), and the exit is located at the room whose southwest corner is ( ox , oy ). Output Print the maximum possible increase in the minimum number of required steps between the entrance and the exit just by adding one wall. If there is no good place to build a new wall, print zero. Sample Input 1 3 4 4 1 0 1 1 1 3 1 4 1 2 2 2 2 1 2 3 0 0 1 0 Output ...(truncated)
[ { "submission_id": "aoj_2225_10853847", "code_snippet": "#include<stdio.h>\n#include<string.h>\n#include<math.h>\n#include<algorithm>\nusing namespace std;\n#define maxw 55\n#define maxn 1010\n\nint n;\nint w;\nint h;\nbool wall[maxw][maxw][4];\nint mp[maxw][maxw];\nint ix,iy;\nint ox,oy;\nint que[maxw*maxw][2];\nint dir[4][2] = {{0,1},{1,0},{0,-1},{-1,0}};\nint bfs()\n{\n memset(mp,-1,sizeof(mp));\n mp[ix][iy] = 0;\n int head = 0, tail = 0;\n tail++;\n que[tail][0] = ix;\n que[tail][1] = iy;\n while (head < tail)\n {\n ++head;\n int x = que[head][0];\n int y = que[head][1];\n //printf(\"**********%d %d\\n\",x,y);\n for (int i=0;i<4;i++){\n if (!wall[x][y][i]){\n int xx = x + dir[i][0];\n int yy = y + dir[i][1];\n if (mp[xx][yy]==-1){\n // printf(\"%d %d\\n\",xx,yy);\n mp[xx][yy] = mp[x][y] + 1;\n if (xx == ox && yy == oy)\n return mp[xx][yy];\n tail++;\n que[tail][0] = xx;\n que[tail][1] = yy;\n }\n }\n }\n }\n return mp[ox][oy];\n}\nvoid init()\n{\n memset(wall,0,sizeof(wall));\n for (int i=1;i<=n;i++)\n {\n int sx,sy,dx,dy;\n scanf(\"%d%d%d%d\",&sx,&sy,&dx,&dy);\n if (sx == dx && sy == dy)\n continue ;\n if (sx == dx){\n if (sy > dy)\n swap(sy,dy);\n for (int j = max(0,sy);j<min(h,dy);j++){\n if (sx-1>=0){\n wall[sx-1][j][1] = 1;\n }\n if (sx<w){\n wall[sx][j][3] = 1;\n }\n }\n }\n if (sy == dy){\n if (sx > dx)\n swap(sx,dx);\n for (int j=max(sx,0);j<min(dx,w);j++){\n if (sy-1>=0){\n wall[j][sy - 1][0] = 1;\n }\n if (sy<h){\n wall[j][sy][2] = 1;\n }\n }\n }\n }\n for (int j=0;j<h;j++){\n wall[0][j][3] = 1;\n wall[w-1][j][1] = 1;\n }\n for (int i=0;i<w;i++){\n wall[i][0][2] = 1;\n wall[i][h-1][0] = 1;\n }\n scanf(\"%d%d\",&ix,&iy);\n scanf(\"%d%d\",&ox,&oy);\n/*\n for (int i=0;i<=w;i++)\n for (int j=0;j<=h;j++)\n for (int k=0;k<4;k++){\n printf(\"%d %d %d %d\\n\",i,j,k,wall[i][j][k]);\n }\n*/\n return ;\n}\nbool check(int x,int y)\n{\n return (x>=0 && x<w && y>=0 && y<h);\n}\nint main()\n{\n while (scanf(\"%d%d%d\",&w,&h,&n)!=EOF)\n {\n init();\n if (ix==ox && iy==oy){\n puts(\"0\");\n continue;\n }\n int tmp = bfs();\n // printf(\"%d\\n\",tmp);\n int ans = 0;\n for (int i=0;i<w;i++)\n for (int j=0;j<h;j++)\n for (int k=0;k<4;k++)\n if (!wall[i][j][k]){\n wall[i][j][k] = 1;\n int ii = i + dir[k][0];\n int jj = j + dir[k][1];\n int kk = (k+2)%4;\n if (check(ii,jj))\n wall[ii][jj][kk] = 1;\n int ttt = bfs();\n if (ttt>=0){\n if(ttt - tmp > ans)\n ans = ttt - tmp;\n }\n wall[i][j][k] = 0;\n if (check(ii,jj))\n wall[ii][jj][kk] = 0;\n }\n printf(\"%d\\n\",ans);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3036, "score_of_the_acc": -0.1578, "final_rank": 6 }, { "submission_id": "aoj_2225_10433358", "code_snippet": "// AOJ #2225 Dungeon Wall\n// 2025.4.30\n\n#include <bits/stdc++.h>\nusing namespace std;\n\n#define gc() getchar_unlocked()\n\nint Cin() {\n\tint n = 0; int c = gc();\n\tdo n = 10*n + (c & 0xf), c = gc(); while (c >= '0');\n\treturn n;\n}\n\nint w, h, n;\nbool hwall[50][51], vwall[51][50];\n\nint bfs(int sx, int sy, int tx, int ty) {\n static int d[50][50];\n for (int i = 0; i < h; i++)\n for (int j = 0; j < w; j++) d[i][j] = -1;\n\n queue<pair<int,int>> q;\n d[sy][sx] = 0;\n q.push({sx, sy});\n\n while (!q.empty()) {\n auto [x, y] = q.front(); q.pop();\n if (x == tx && y == ty) return d[y][x];\n if (x + 1 < w && !vwall[x+1][y] && d[y][x+1] < 0) {\n d[y][x+1] = d[y][x] + 1;\n q.push({x+1, y});\n }\n if (x - 1 >= 0 && !vwall[x][y] && d[y][x-1] < 0) {\n d[y][x-1] = d[y][x] + 1;\n q.push({x-1, y});\n }\n if (y + 1 < h && !hwall[x][y+1] && d[y+1][x] < 0) {\n d[y+1][x] = d[y][x] + 1;\n q.push({x, y+1});\n }\n if (y - 1 >= 0 && !hwall[x][y] && d[y-1][x] < 0) {\n d[y-1][x] = d[y][x] + 1;\n q.push({x, y-1});\n }\n }\n return INT_MAX;\n}\n\nint main() {\n w = Cin(), h = Cin(), n = Cin();\n for (int x = 0; x < w; x++) hwall[x][0] = hwall[x][h] = true;\n for (int y = 0; y < h; y++) vwall[0][y] = vwall[w][y] = true;\n\n for (int i = 0; i < n; i++) {\n int sx = Cin(), sy = Cin(), dx = Cin(), dy = Cin();\n if (sx == dx) {\n if (sy > dy) swap(sy, dy);\n for (int y = sy; y < dy; y++) vwall[sx][y] = true;\n } else {\n if (sx > dx) swap(sx, dx);\n for (int x = sx; x < dx; x++) hwall[x][sy] = true;\n }\n }\n\n int sx = Cin(), sy = Cin(), tx = Cin(), ty = Cin();\n int d0 = bfs(sx, sy, tx, ty);\n int ans = 0;\n\n for (int x = 1; x < w; x++) {\n for (int y = 0; y < h; y++) {\n if (!vwall[x][y]) {\n vwall[x][y] = true;\n int d1 = bfs(sx, sy, tx, ty);\n if (d1 < INT_MAX) ans = max(ans, d1 - d0);\n vwall[x][y] = false;\n }\n }\n }\n for (int x = 0; x < w; x++) {\n for (int y = 1; y < h; y++) {\n if (!hwall[x][y]) {\n hwall[x][y] = true;\n int d1 = bfs(sx, sy, tx, ty);\n if (d1 < INT_MAX) ans = max(ans, d1 - d0);\n hwall[x][y] = false;\n }\n }\n }\n cout << ans << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3460, "score_of_the_acc": -0.1864, "final_rank": 8 }, { "submission_id": "aoj_2225_9671539", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define f first;\n#define ss second\n#define ll long long\n\nint h,w,n;\nint wallv[51][51][51];///x,y1,y2;\nint wallh[51][51][51];///y x1,x2\nint iy,ix,ox,oy;\n\nint dist[55][55];\nint valid(int x,int y){\n return (x<w && x>=0 && y>=0 && y<h);\n}\nvoid clean(){\n for(int i=0;i<50;i++){\n for(int j=0;j<50;j++)\n dist[i][j]=1e9;\n }\n}\nint bfs(){\n clean();\n queue<pair<int,int>>q;\n q.push({ix,iy});\n dist[ix][iy]=0;\n while(!q.empty()){\n int x=q.front().f;\n int y=q.front().ss;\n int d=dist[x][y];\n q.pop();\n if(x==ox && y==oy)return d;\n\n if(valid(x,y+1) && !wallh[x][y][y+1] && dist[x][y+1]>d+1){\n\n\n dist[x][y+1]=d+1;\n q.push({x,y+1});\n }\n\n if(valid(x,y-1) && !wallh[x][y-1][y] && dist[x][y-1]>d+1){\n dist[x][y-1]=d+1;\n q.push({x,y-1});\n }\n\n if(valid(x+1,y) && !wallv[y][x][x+1] && dist[x+1][y]>d+1){\n dist[x+1][y]=d+1;\n q.push({x+1,y});\n }\n\n if(valid(x-1,y) && !wallv[y][x-1][x] && dist[x-1][y]>d+1){\n dist[x-1][y]=d+1;\n q.push({x-1,y});\n }\n\n }\n return dist[ox][oy];\n\n\n}\n\nint main()\n{\n ios_base::sync_with_stdio(0);\n cin.tie(0);cout.tie(0);\n\n cin>>w>>h>>n;\n for(int i=0;i<n;i++){\n int x1,x2,y1,y2;\n cin>>x1>>y1>>x2>>y2;\n\n if(x1==x2){\n if(y1>y2)swap(y1,y2);\n for(int y=y1;y<y2;y++){\n if(x1)wallv[y][x1-1][x1]=1;\n }\n }\n else{\n if(x1>x2)swap(x1,x2);\n for(int x=x1;x<x2;x++){\n if(y1)wallh[x][y1-1][y1]=1;\n }\n\n }\n }\n cin>>ix>>iy>>ox>>oy;\n\n\n int min_dist=bfs();\n ////cout<<min_dist<<endl;\n\n int ans=0;\n for(int i=0;i<w;i++){\n for(int j=0;j<h;j++){\n if(valid(i-1,j) && !wallv[j][i-1][i]){\n wallv[j][i-1][i]=1;\n int cost=bfs();\n if(cost<1e9)\n ans=max(ans,(cost-min_dist));\n wallv[j][i-1][i]=0;\n\n\n }\n if(valid(i+1,j) && !wallv[j][i][i+1]){\n wallv[j][i][i+1]=1;\n int cost=bfs();\n if(cost<1e9)\n ans=max(ans,(cost-min_dist));\n wallv[j][i][i+1]=0;\n\n }\n if(valid(i,j-1) && !wallh[i][j-1][j]){\n wallh[i][j-1][j]=1;\n int cost=bfs();\n if(cost<1e9)\n ans=max(ans,(cost-min_dist));\n wallh[i][j-1][j]=0;\n }\n if(valid(i,j+1) && !wallh[i][j][j+1]){\n wallh[i][j][j+1]=1;\n int cost=bfs();\n if(cost<1e9)\n ans=max(ans,(cost-min_dist));\n wallh[i][j][j+1]=0;\n }\n }\n }\n cout<<ans<<endl;\n\n\n return 0;\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 4508, "score_of_the_acc": -0.2868, "final_rank": 12 }, { "submission_id": "aoj_2225_9671535", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define f first;\n#define ss second\n#define ll long long\n\nint h,w,n;\nint wallv[51][51][51];///x,y1,y2;\nint wallh[51][51][51];///y x1,x2\nint iy,ix,ox,oy;\n\nint dist[55][55];\nint valid(int x,int y){\n return (x<=w && x>=0 && y>=0 && y<=h);\n}\nvoid clean(){\n for(int i=0;i<50;i++){\n for(int j=0;j<50;j++)\n dist[i][j]=1e9;\n }\n}\nint bfs(){\n clean();\n queue<pair<int,int>>q;\n q.push({ix,iy});\n dist[ix][iy]=0;\n while(!q.empty()){\n int x=q.front().f;\n int y=q.front().ss;\n int d=dist[x][y];\n q.pop();\n if(x==ox && y==oy)return d;\n\n if(valid(x,y+1) && !wallh[x][y][y+1] && dist[x][y+1]>d+1){\n\n\n dist[x][y+1]=d+1;\n q.push({x,y+1});\n }\n\n if(valid(x,y-1) && !wallh[x][y-1][y] && dist[x][y-1]>d+1){\n dist[x][y-1]=d+1;\n q.push({x,y-1});\n }\n\n if(valid(x+1,y) && !wallv[y][x][x+1] && dist[x+1][y]>d+1){\n dist[x+1][y]=d+1;\n q.push({x+1,y});\n }\n\n if(valid(x-1,y) && !wallv[y][x-1][x] && dist[x-1][y]>d+1){\n dist[x-1][y]=d+1;\n q.push({x-1,y});\n }\n\n }\n return dist[ox][oy];\n\n\n}\n\nint main()\n{\n ios_base::sync_with_stdio(0);\n cin.tie(0);cout.tie(0);\n\n cin>>w>>h>>n;\n for(int i=0;i<n;i++){\n int x1,x2,y1,y2;\n cin>>x1>>y1>>x2>>y2;\n\n if(x1==x2){\n if(y1>y2)swap(y1,y2);\n for(int y=y1;y<y2;y++){\n if(x1)wallv[y][x1-1][x1]=1;\n }\n }\n else{\n if(x1>x2)swap(x1,x2);\n for(int x=x1;x<x2;x++){\n if(y1)wallh[x][y1-1][y1]=1;\n }\n\n }\n }\n cin>>ix>>iy>>ox>>oy;\n\n\n int min_dist=bfs();\n ////cout<<min_dist<<endl;\n\n int ans=0;\n for(int i=0;i<=w;i++){\n for(int j=0;j<=h;j++){\n if(valid(i-1,j) && !wallv[j][i-1][i]){\n wallv[j][i-1][i]=1;\n int cost=bfs();\n if(cost<1e9)\n ans=max(ans,(cost-min_dist));\n wallv[j][i-1][i]=0;\n\n\n }\n if(valid(i+1,j) && !wallv[j][i][i+1]){\n wallv[j][i][i+1]=1;\n int cost=bfs();\n if(cost<1e9)\n ans=max(ans,(cost-min_dist));\n wallv[j][i][i+1]=0;\n\n }\n if(valid(i,j-1) && !wallh[i][j-1][j]){\n wallh[i][j-1][j]=1;\n int cost=bfs();\n if(cost<1e9)\n ans=max(ans,(cost-min_dist));\n wallh[i][j-1][j]=0;\n }\n if(valid(i,j+1) && !wallh[i][j][j+1]){\n wallh[i][j][j+1]=1;\n int cost=bfs();\n if(cost<1e9)\n ans=max(ans,(cost-min_dist));\n wallh[i][j][j+1]=0;\n }\n }\n }\n cout<<ans<<endl;\n\n\n return 0;\n}", "accuracy": 0.48717948717948717, "time_ms": 40, "memory_kb": 4436, "score_of_the_acc": -0.2653, "final_rank": 20 }, { "submission_id": "aoj_2225_4276165", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 99999999999999999\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n#define SIZE 55\n\nstruct Info{\n\tInfo(int arg_node_id,int arg_sum_cost){\n\t\tnode_id = arg_node_id;\n\t\tsum_cost = arg_sum_cost;\n\t}\n\tbool operator<(const struct Info &arg) const{\n\n\t\treturn sum_cost > arg.sum_cost;\n\t}\n\tint node_id,sum_cost;\n};\n\nint W,H,N;\nint num_nodes,start,goal;\nint index_table[SIZE][SIZE];\nint min_dist[SIZE*SIZE],MIN_DIST[SIZE*SIZE],rev_min_dist[SIZE*SIZE];\nint diff_row[4] = {-1,0,0,1},diff_col[4] = {0,-1,1,0};\nbool wall[SIZE*SIZE][SIZE*SIZE];\nvector<int> G[SIZE*SIZE];\n\n\nbool rangeCheck(int row,int col){\n\n\treturn row >= 0 && row <= H-1 && col >= 0 && col <= W-1;\n}\n\nvoid calc_rev(){\n\n\tfor(int i = 0; i < num_nodes; i++){\n\n\t\trev_min_dist[i] = BIG_NUM;\n\t}\n\trev_min_dist[goal] = 0;\n\n\tpriority_queue<Info> Q;\n\tQ.push(Info(goal,0));\n\n\twhile(!Q.empty()){\n\n\t\tif(Q.top().sum_cost > rev_min_dist[Q.top().node_id]){\n\n\t\t\tQ.pop();\n\t\t}else{\n\n\t\t\tfor(int i = 0; i < G[Q.top().node_id].size(); i++){\n\n\t\t\t\tint next = G[Q.top().node_id][i];\n\t\t\t\tif(wall[Q.top().node_id][next])continue;\n\n\t\t\t\tint next_cost = Q.top().sum_cost+1;\n\n\t\t\t\tif(rev_min_dist[next] > next_cost){\n\t\t\t\t\trev_min_dist[next] = next_cost;\n\t\t\t\t\tQ.push(Info(next,next_cost));\n\t\t\t\t}\n\t\t\t}\n\t\t\tQ.pop();\n\t\t}\n\t}\n}\n\nint func(){\n\n\tfor(int i = 0; i < num_nodes; i++){\n\n\t\tmin_dist[i] = BIG_NUM;\n\t}\n\tmin_dist[start] = 0;\n\n\tpriority_queue<Info> Q;\n\tQ.push(Info(start,0));\n\n\twhile(!Q.empty()){\n\n\t\tif(Q.top().sum_cost > min_dist[Q.top().node_id]){\n\n\t\t\tQ.pop();\n\t\t}else if(Q.top().node_id == goal){\n\n\t\t\treturn Q.top().sum_cost;\n\t\t}else{\n\n\t\t\tfor(int i = 0; i < G[Q.top().node_id].size(); i++){\n\n\t\t\t\tint next = G[Q.top().node_id][i];\n\t\t\t\tif(wall[Q.top().node_id][next])continue;\n\n\t\t\t\tint next_cost = Q.top().sum_cost+1;\n\n\t\t\t\tif(min_dist[next] > next_cost){\n\t\t\t\t\tmin_dist[next] = next_cost;\n\t\t\t\t\tQ.push(Info(next,next_cost));\n\t\t\t\t}\n\t\t\t}\n\t\t\tQ.pop();\n\t\t}\n\t}\n\n\treturn BIG_NUM;\n}\n\n\nint main(){\n\n\tscanf(\"%d %d %d\",&W,&H,&N);\n\n\tint index = 0;\n\tfor(int row = 0; row < H; row++){\n\t\tfor(int col = 0; col < W; col++){\n\t\t\tindex_table[row][col] = index++;\n\t\t}\n\t}\n\n\tnum_nodes = H*W;\n\tfor(int i = 0; i < num_nodes; i++){\n\t\tfor(int k = 0; k < num_nodes; k++){\n\n\t\t\twall[i][k] = false;\n\t\t}\n\t}\n\n\tint s_col,s_row,d_col,d_row;\n\tint from_index,to_index;\n\n\tfor(int loop = 0; loop < N; loop++){\n\n\t\tscanf(\"%d %d %d %d\",&s_col,&s_row,&d_col,&d_row);\n\n\t\tif(s_col == d_col){ //縦\n\n\t\t\tif(s_col == 0){\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tfor(int i = min(s_row,d_row); i < max(s_row,d_row); i++){\n\n\t\t\t\tfrom_index = index_table[i][s_col-1];\n\t\t\t\tto_index = index_table[i][s_col];\n\n\t\t\t\twall[from_index][to_index] = true;\n\t\t\t\twall[to_index][from_index] = true;\n\t\t\t}\n\n\t\t}else{ //横\n\n\t\t\tif(s_row == 0){\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tfor(int i = min(s_col,d_col); i < max(s_col,d_col); i++){\n\n\t\t\t\tfrom_index = index_table[s_row-1][i];\n\t\t\t\tto_index = index_table[s_row][i];\n\n\t\t\t\twall[from_index][to_index] = true;\n\t\t\t\twall[to_index][from_index] = true;\n\t\t\t}\n\t\t}\n\t}\n\n\tint start_col,start_row;\n\tint goal_col,goal_row;\n\n\tscanf(\"%d %d\",&start_col,&start_row);\n\tstart = index_table[start_row][start_col];\n\n\tscanf(\"%d %d\",&goal_col,&goal_row);\n\tgoal = index_table[goal_row][goal_col];\n\n\tfor(int row = 0; row < H; row++){\n\t\tfor(int col = 0; col < W; col++){\n\n\t\t\tfrom_index = index_table[row][col];\n\n\t\t\tfor(int i = 0; i < 4; i++){\n\t\t\t\tint adj_row = row+diff_row[i];\n\t\t\t\tint adj_col = col+diff_col[i];\n\t\t\t\tif(!rangeCheck(adj_row,adj_col))continue;\n\n\t\t\t\tto_index = index_table[adj_row][adj_col];\n\t\t\t\tif(wall[from_index][to_index])continue;\n\n\t\t\t\tG[from_index].push_back(to_index);\n\t\t\t}\n\t\t}\n\t}\n\n\tcalc_rev();\n\tint first_ans = func();\n\tfor(int i = 0; i < num_nodes; i++){\n\n\t\tMIN_DIST[i] = min_dist[i];\n\t}\n\n\tint ans = 0;\n\n\tfor(int row = 0; row < H; row++){\n\t\tfor(int col = 0; col < W; col++){\n\n\t\t\tfrom_index = index_table[row][col];\n\t\t\tif(MIN_DIST[from_index]+rev_min_dist[from_index] != first_ans)continue; //最短経路上でないならSKIP\n\n\t\t\tfor(int i = 0; i < 4; i++){\n\n\t\t\t\tint adj_row = row+diff_row[i];\n\t\t\t\tint adj_col = col+diff_col[i];\n\n\t\t\t\tif(!rangeCheck(adj_row,adj_col))continue;\n\t\t\t\tto_index = index_table[adj_row][adj_col];\n\n\t\t\t\tif(wall[from_index][to_index])continue;\n\t\t\t\twall[from_index][to_index] = true;\n\n\t\t\t\tint tmp_ans = func();\n\t\t\t\tif(tmp_ans != BIG_NUM){\n\n\t\t\t\t\tans = max(ans,tmp_ans-first_ans);\n\t\t\t\t}\n\n\t\t\t\twall[from_index][to_index] = false;\n\t\t\t}\n\t\t}\n\t}\n\n\tprintf(\"%d\\n\",ans);\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 270, "memory_kb": 10812, "score_of_the_acc": -0.8467, "final_rank": 15 }, { "submission_id": "aoj_2225_2737418", "code_snippet": "#include<bits/stdc++.h>\n#define MAX 110\n#define inf 1<<28\n#define f first\n#define s second\n#define mp make_pair\nusing namespace std;\n \nint w,h,n,si,sj,gi,gj;\nbool grid[MAX][MAX]={};\nint Y[4]={1,0,-1,0};\nint X[4]={0,1,0,-1};\nint d[MAX][MAX];\n \nint bfs(){\n for(int i=0;i<MAX;i++){\n for(int j=0;j<MAX;j++)d[i][j]=inf;\n }\n d[si][sj]=0;\n queue<pair<int,int> > q;\n q.push(mp(si,sj));\n \n while(q.size()){\n pair<int,int> u=q.front();\n q.pop();\n if(u.f==gi && u.s==gj)return d[gi][gj];\n \n for(int k=0;k<4;k++){\n int a=u.f+Y[k],b=u.s+X[k];\n if(grid[a][b])continue;\n a+=Y[k];\n b+=X[k];\n if(d[a][b]==inf){\n d[a][b]=d[u.f][u.s]+1;\n q.push(mp(a,b));\n }\n }\n }\n return -1;\n}\n \nint main()\n{\n int a,b,c,d;\n \n cin>>w>>h>>n;\n \n for(int i=0;i<=h*2;i++)grid[i][0]=grid[i][w*2]=true;\n for(int i=0;i<=w*2;i++)grid[0][i]=grid[h*2][i]=true;\n \n for(int i=0;i<n;i++){\n cin>>a>>b>>c>>d;\n for(int j=min(a,c)*2;j<=max(a,c)*2;j++){\n for(int k=min(b,d)*2;k<=max(b,d)*2;k++)grid[k][j]=true;\n }\n }\n cin>>sj>>si>>gj>>gi;\n si=si*2+1;\n sj=sj*2+1;\n gi=gi*2+1;\n gj=gj*2+1;\n \n int dis=bfs(),ans=0;\n \n for(int i=2;i<=h*2-2;i+=2){\n for(int j=1;j<=w*2-1;j+=2){\n if(grid[i][j])continue;\n grid[i][j]=true;\n ans=max(bfs()-dis,ans);\n grid[i][j]=false;\n }\n }\n for(int i=1;i<=h*2-1;i+=2){\n for(int j=2;j<=w*2-2;j+=2){\n if(grid[i][j])continue;\n grid[i][j]=true;\n ans=max(bfs()-dis,ans);\n grid[i][j]=false;\n }\n }\n cout<<ans<<endl; \n \n return 0;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 3160, "score_of_the_acc": -0.1714, "final_rank": 7 }, { "submission_id": "aoj_2225_2409177", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#define int long long\n\nstruct P {\n int x, y;\n P(){}\n P(int x, int y):x(x), y(y){}\n bool operator == (const P &p) const {\n return x == p.x && y == p.y;\n }\n bool operator < (const P &p) const {\n return x != p.x ? x < p.x : y < p.y;\n }\n};\n\nP min(P p, P q) {\n return p < q ? p : q;\n}\n\nint W, H, N;\nP ip, op;\n\nint dy[] = {1, 0, -1, 0};\nint dx[] = {0, 1, 0, -1};\n\nbool wall[55][55][55][55];\n\nbool in(int y, int x) {\n return 0<=y&&y<H&&0<=x&&x<W;\n}\n\nint bfs() {\n queue<P> que;\n int dist[55][55];\n memset(dist, -1, sizeof(dist));\n que.emplace(ip);\n dist[ip.y][ip.x] = 0;\n while(!que.empty()) {\n P p = que.front(); que.pop();\n //cout << p.x << \" \" << p.y << \":\"<< dist[p.y][p.x]<<endl; \n if(p == op) break; \n for(int i = 0; i < 4; i++) {\n int ny = p.y+dy[i], nx = p.x+dx[i];\n if(!in(ny, nx)) continue;\n if(dist[ny][nx] != -1) continue;\n if((i == 0 && !wall[ny][nx][ny][nx+1]) ||\n\t (i == 1 && !wall[ny][nx][ny+1][nx]) ||\n\t (i == 2 && !wall[p.y][p.x][p.y][p.x+1]) ||\n\t (i == 3 && !wall[p.y][p.x][p.y+1][p.x])) {\n\tque.emplace(nx, ny);\n\tdist[ny][nx] = dist[p.y][p.x]+1;\n\t//cout<<ny<<\" \"<<nx<<\" \"<<dist[ny][nx]<<endl;\n\t//cout<<\" \"<<dist[p.y][p.x]+1<<endl;\n }\n }\n }\n //cout<<dist[op.y][op.x]<<endl;\n return dist[op.y][op.x];\n}\n\nvoid print(){\n for(int i=0;i<H;i++){\n for(int j=0;j<W;j++){\n cout<<wall[i][j][i][j+1]<<\" \"<<wall[i][j][i+1][j]<<endl;\n }\n }\n cout<<endl;\n}\n\nsigned main(){\n cin >> W >> H >> N;\n for(int i = 0; i < N; i++) {\n P s, d;\n cin >> s.x >> s.y >> d.x >> d.y;\n P p = min(s, d);\n P q = p == s ? d : s;\n if(p.y == q.y) {\n for(int x = p.x; x < q.x; x++) wall[p.y][x][p.y][x+1] = true;\n } else if(p.x == q.x) {\n for(int y = p.y; y < q.y; y++) wall[y][p.x][y+1][p.x] = true; \n } else {\n assert(false);\n }\n }\n\n cin >> ip.x >> ip.y >> op.x >> op.y;\n\n int ini = bfs(), ans = ini;\n \n for(int i = 0; i < H; i++) {\n for(int j = 0; j < W; j++) {\n if(in(i+1, j) && !wall[i][j][i+1][j]) {\n\twall[i][j][i+1][j] = true;\n\t//print();\n\tans = max(ans, bfs());\n\twall[i][j][i+1][j] = false;\n }\n if(in(i, j+1) && !wall[i][j][i][j+1]) {\n\twall[i][j][i][j+1] = true;\n\t//print();\n\tans = max(ans, bfs());\n\twall[i][j][i][j+1] = false;\n }\n }\n }\n \n //cout << ans << \" \" << ini << endl;\n cout << ans - ini << endl;\n \n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 10904, "score_of_the_acc": -0.7618, "final_rank": 14 }, { "submission_id": "aoj_2225_2409151", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef pair<int,int> P;\nint w,h,n,ix,iy,ox,oy;\n\nbool kab[60][60][4];\n\nint dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};\n\nint cal(){\n queue<P> q;\n\n int cost[60][60];\n\n for(int i=0;i<60;i++)\n for(int j=0;j<60;j++)cost[i][j]=1e9;\n cost[iy][ix]=0;\n q.push(P(ix,iy));\n \n while(!q.empty()){\n int x=q.front().first,y=q.front().second;\n q.pop();\n if(x==ox&&y==oy)return cost[y][x];\n \n for(int i=0;i<4;i++){\n int nx=x+dx[i],ny=y+dy[i];\n if(nx<0||w<=nx||ny<0||h<=ny||kab[y][x][i]||cost[ny][nx]!=1e9)continue;\n q.push(P(nx,ny));\n cost[ny][nx]=cost[y][x]+1;\n }\n \n }\n \n return 0;\n}\n\n\nint main(){\n cin>>w>>h>>n;\n for(int i=0;i<n;i++){\n int sx,sy,ddx,ddy;\n cin>>sx>>sy>>ddx>>ddy;\n\n if(sx==ddx)\n for(int i=min(sy,ddy);i<max(ddy,sy);i++){\n\tif(0<=sx-1)kab[i][sx-1][0]=1;\n\tif(sx<w)kab[i][sx][2]=1;\n }\n else\n for(int i=min(sx,ddx);i<max(ddx,sx);i++){\n\tif(0<=sy-1)kab[sy-1][i][1]=1;\n\tif(sy<h)kab[sy][i][3]=1;\n }\n }\n cin>>ix>>iy>>ox>>oy;\n \n for(int i=0;i<w;i++){\n kab[0][i][3]=1;\n kab[h-1][i][1]=1;\n }\n \n for(int i=0;i<h;i++){\n kab[i][0][2]=1;\n kab[i][w-1][0]=1;\n }\t\t \n\n int d=cal(),ans=0;\n for(int i=0;i<h;i++)\n for(int j=0;j<w;j++)\n\tfor(int k=0;k<4;k++){\n\t if(kab[i][j][k])continue;\n\t kab[i][j][k]=1;\n\t ans=max(ans,cal()-d);\n\t kab[i][j][k]=0;\n } \n \n cout<<ans<<endl;\n\n return 0; \n}", "accuracy": 1, "time_ms": 150, "memory_kb": 3124, "score_of_the_acc": -0.2086, "final_rank": 10 }, { "submission_id": "aoj_2225_1895873", "code_snippet": "#include<bits/stdc++.h>\n#define MAX 110\n#define inf 1<<28\n#define f first\n#define s second\n#define mp make_pair\nusing namespace std;\n\nint w,h,n,si,sj,gi,gj;\nbool grid[MAX][MAX]={};\nint Y[4]={1,0,-1,0};\nint X[4]={0,1,0,-1};\nint d[MAX][MAX];\n\nint bfs(){\n for(int i=0;i<MAX;i++){\n for(int j=0;j<MAX;j++)d[i][j]=inf;\n }\n d[si][sj]=0;\n queue<pair<int,int> > q;\n q.push(mp(si,sj));\n\n while(q.size()){\n pair<int,int> u=q.front();\n q.pop();\n if(u.f==gi && u.s==gj)return d[gi][gj];\n\n for(int k=0;k<4;k++){\n int a=u.f+Y[k],b=u.s+X[k];\n if(grid[a][b])continue;\n a+=Y[k];\n b+=X[k];\n if(d[a][b]==inf){\n\td[a][b]=d[u.f][u.s]+1;\n\tq.push(mp(a,b));\n }\n }\n }\n return -1;\n}\n\nint main()\n{\n int a,b,c,d;\n \n cin>>w>>h>>n;\n\n for(int i=0;i<=h*2;i++)grid[i][0]=grid[i][w*2]=true;\n for(int i=0;i<=w*2;i++)grid[0][i]=grid[h*2][i]=true;\n\n for(int i=0;i<n;i++){\n cin>>a>>b>>c>>d;\n for(int j=min(a,c)*2;j<=max(a,c)*2;j++){\n for(int k=min(b,d)*2;k<=max(b,d)*2;k++)grid[k][j]=true;\n }\n }\n cin>>sj>>si>>gj>>gi;\n si=si*2+1;\n sj=sj*2+1;\n gi=gi*2+1;\n gj=gj*2+1;\n\n int dis=bfs(),ans=0;\n\n for(int i=2;i<=h*2-2;i+=2){\n for(int j=1;j<=w*2-1;j+=2){\n if(grid[i][j])continue;\n grid[i][j]=true;\n ans=max(bfs()-dis,ans);\n grid[i][j]=false;\n }\n }\n for(int i=1;i<=h*2-1;i+=2){\n for(int j=2;j<=w*2-2;j+=2){\n if(grid[i][j])continue;\n grid[i][j]=true;\n ans=max(bfs()-dis,ans);\n grid[i][j]=false;\n }\n }\n cout<<ans<<endl; \n\n return 0;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 1276, "score_of_the_acc": -0.0267, "final_rank": 5 }, { "submission_id": "aoj_2225_1895871", "code_snippet": "#include<bits/stdc++.h>\n#define MAX 110\n#define inf 1<<28\n#define f first\n#define s second\n#define mp make_pair\nusing namespace std;\n\nint w,h,n,si,sj,gi,gj;\nbool grid[MAX][MAX]={};\nint Y[4]={1,0,-1,0};\nint X[4]={0,1,0,-1};\n\nint bfs(){\n int d[MAX][MAX];\n for(int i=0;i<MAX;i++){\n for(int j=0;j<MAX;j++)d[i][j]=inf;\n }\n d[si][sj]=0;\n queue<pair<int,int> > q;\n q.push(mp(si,sj));\n\n while(q.size()){\n pair<int,int> u=q.front();\n q.pop();\n if(u.f==gi && u.s==gj)return d[gi][gj];\n\n for(int k=0;k<4;k++){\n int a=u.f+Y[k],b=u.s+X[k];\n if(grid[a][b])continue;\n a+=Y[k];\n b+=X[k];\n if(d[u.f][u.s]+1<d[a][b]){\n\td[a][b]=d[u.f][u.s]+1;\n\tq.push(mp(a,b));\n }\n }\n }\n return -1;\n}\n\nint main()\n{\n int a,b,c,d;\n \n cin>>w>>h>>n;\n\n for(int i=0;i<=h*2;i++)grid[i][0]=grid[i][w*2]=true;\n for(int i=0;i<=w*2;i++)grid[0][i]=grid[h*2][i]=true;\n\n for(int i=0;i<n;i++){\n cin>>a>>b>>c>>d;\n for(int j=min(a,c)*2;j<=max(a,c)*2;j++){\n for(int k=min(b,d)*2;k<=max(b,d)*2;k++)grid[k][j]=true;\n }\n }\n cin>>sj>>si>>gj>>gi;\n si=si*2+1;\n sj=sj*2+1;\n gi=gi*2+1;\n gj=gj*2+1;\n\n int dis=bfs(),ans=0;\n\n for(int i=2;i<=h*2-2;i+=2){\n for(int j=1;j<=w*2-1;j+=2){\n if(grid[i][j])continue;\n grid[i][j]=true;\n ans=max(bfs()-dis,ans);\n grid[i][j]=false;\n }\n }\n for(int i=1;i<=h*2-1;i+=2){\n for(int j=2;j<=w*2-2;j+=2){\n if(grid[i][j])continue;\n grid[i][j]=true;\n ans=max(bfs()-dis,ans);\n grid[i][j]=false;\n }\n }\n cout<<ans<<endl; \n\n return 0;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 1272, "score_of_the_acc": -0.0264, "final_rank": 4 }, { "submission_id": "aoj_2225_1879578", "code_snippet": "#include <bits/stdc++.h>\n \nusing namespace std;\n \n#define MAX_H 60\n#define MAX_W 60\n#define INF 1e9\n \nint H,W,N,ix,iy,ox,oy;\nbool wall[MAX_H][MAX_W][4],a[MAX_H][MAX_W][4];\nconst int dx[4] = {-1,0,1,0};\nconst int dy[4] = {0,-1,0,1};\n \nstruct P{\n int x,y;\n P(int x,int y) : x(x),y(y) {}\n};\n \ninline bool inField(int x,int y){\n return (0 <= x && x < W && 0 <= y && y < H);\n}\n \nint bfs(){\n int d[MAX_H][MAX_W];\n for(int i = 0 ; i < H ; i++){\n\tfor(int j = 0 ; j < W ; j++){\n\t d[i][j] = INF;\n\t}\n }\n d[iy][ix] = 0;\n queue<P> Q;\n Q.push(P(ix,iy));\n queue<int> step;\n step.push(0);\n \n while(!Q.empty()){\n\tP p = Q.front(); Q.pop();\n\tint s = step.front(); step.pop();\n\tint x = p.x, y = p.y;\n\tif(x == ox && y == oy) return d[oy][ox];\n\tfor(int i = 0 ; i < 4 ; i++){\n\t int nx = x + dx[i], ny = y + dy[i];\n\t if(wall[y][x][i]) continue;\n\t if(!inField(nx,ny)) continue;\n\t if(d[y][x]+1 < d[ny][nx]){\n\t\td[ny][nx] = d[y][x]+1;\n\t\tQ.push(P(nx,ny));\n\t\tstep.push(s+1);\n\t }\n\t}\n }\n return -1;\n}\n \nint main(){\n cin >> W >> H >> N;\n for(int i = 0 ; i < N ; i++){\n\tint sx,sy,dx,dy;\n\tcin >> sx >> sy >> dx >> dy;\n\tif(sx > dx) swap(sx,dx);\n\tif(sy > dy) swap(sy,dy);\n\tbool t = false,y = false;\n\tif(sx != dx) y = true;\n\tif(sy != dy) t = true;\n\tif(y){\n\t for(int j = sx ; j < dx ; j++){\n\t\tfor(int k = sy ; k <= dy ; k++){\n\t\t if(k-1 >= 0){\n\t\t\twall[k-1][j][3] = true;\n\t\t\ta[k-1][j][3] = true;\n\t\t }\n\t\t wall[k][j][1] = true;\n\t\t a[k][j][1] = true;\n\t\t}\n\t }\n\t}\n\tif(t){\n\t for(int j = sx ; j <= dx ; j++){\n\t\tfor(int k = sy ; k < dy ; k++){\n\t\t if(j-1 >= 0){\n\t\t\twall[k][j-1][2] = true;\n\t\t\ta[k][j-1][2] = true;\n\t\t }\n\t\t wall[k][j][0] = true;\n\t\t a[k][j][0] = true;\n\t\t}\n\t }\n\t}\n }\n int res = 0;\n cin >> ix >> iy >> ox >> oy;\n int dist = bfs();\n for(int x = 0 ; x <= W+1 ; x++){\n\tfor(int y = 0 ; y <= H+1 ; y++){\n\t int xx = x-1,yy = y;\n\t if(xx >= 0) wall[yy][xx][2] = wall[y][x][0] = true;\n\t res = max(res,bfs()-dist);\n\t if(xx >= 0){\n\t\twall[yy][xx][2] = a[yy][xx][2];\n\t\twall[y][x][0] = a[y][x][0];\n\t }\n\t}\n }\n for(int x = 0 ; x <= W+1 ; x++){\n\tfor(int y = 0 ; y <= H+1 ; y++){\n\t int xx = x,yy = y-1;\n\t if(yy >= 0) wall[yy][xx][3] = wall[y][x][1] = true;\n\t res = max(res,bfs()-dist);\n\t if(yy >= 0){\n\t\twall[yy][xx][3] = a[yy][xx][3];\n\t\twall[y][x][1] = a[y][x][1];\n\t }\n\t}\n }\n cout << res << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 150, "memory_kb": 3172, "score_of_the_acc": -0.2123, "final_rank": 11 }, { "submission_id": "aoj_2225_1704723", "code_snippet": "#include <cstdio>\n#include <cstring>\n#include <vector>\n#include <queue>\n#include <algorithm>\n\nusing namespace std;\ntypedef pair<int,int> P;\nint w,h,n;\nint fie[101][101];\nint dist[101][101];\nint ix,iy,ox,oy;\nint dx[4]={1,0,-1,0};\nint dy[4]={0,-1,0,1};\n\nint bfs(){\n\tqueue<P> que;\n\tmemset(dist,-1,sizeof(dist));\n\tque.push(P(ix,iy));\n\tdist[iy][ix]=0;\n\twhile(que.size()){\n\t\tP q=que.front();\n\t\tque.pop();\n\t\tfor(int i=0;i<4;i++){\n\t\t\tint nx=q.first+dx[i],ny=q.second+dy[i];\n\t\t\tif(fie[ny][nx]==0){\n\t\t\t\tnx+=dx[i];\n\t\t\t\tny+=dy[i];\n\t\t\t\tif(dist[ny][nx]!=-1)continue;\n\t\t\t\tdist[ny][nx]=dist[q.second][q.first]+1;\n\t\t\t\tque.push(P(nx,ny));\n\t\t\t}\n\t\t}\n\t}\n\treturn dist[oy][ox];\n}\n\nint main(void){\n\tscanf(\"%d %d %d\",&w,&h,&n);\n\tw*=2;\n\th*=2;\n\tfor(int i=0;i<=h;i++){\n\t\tfie[i][0]=1;\n\t\tfie[i][w]=1;\n\t}\n\tfor(int i=0;i<=w;i++){\n\t\tfie[0][i]=1;\n\t\tfie[h][i]=1;\n\t}\n\tfor(int i=0;i<n;i++){\n\t\tint sx,sy,ddx,ddy;\n\t\tscanf(\"%d %d %d %d\",&sx,&sy,&ddx,&ddy);\n\t\tif(sx>ddx)swap(sx,ddx);\n\t\tif(sy>ddy)swap(sy,ddy);\n\t\tsx*=2;\n\t\tsy*=2;\n\t\tddx*=2;\n\t\tddy*=2;\n\t\tfor(int j=sx;j<=ddx;j++){\n\t\t\tfie[ddy][j]=1;\n\t\t\tfie[sy][j]=1;\n\t\t}\n\t\tfor(int j=sy;j<=ddy;j++){\n\t\t\tfie[j][ddx]=1;\n\t\t\tfie[j][sx]=1;\n\t\t}\n\t}\n\tscanf(\"%d%d\",&ix,&iy);\n\tscanf(\"%d%d\",&ox,&oy);\n\tix=ix*2+1;\n\tiy=iy*2+1;\n\tox=ox*2+1;\n\toy=oy*2+1;\n\tint prev=bfs();\n\tint res=prev;\n\tfor(int i=1;i<h;i+=2){\n\t\tfor(int j=0;j<w;j+=2){\n\t\t\tif(fie[i][j]==0){\n\t\t\t\tfie[i][j]=1;\n\t\t\t\tint di=bfs();\n\t\t\t\tif(di!=-1)res=max(di,res);\n\t\t\t\tfie[i][j]=0;\n\t\t\t}\n\t\t}\n\t}\n\tfor(int i=0;i<h;i+=2){\n\t\tfor(int j=1;j<w;j+=2){\n\t\t\tif(fie[i][j]==0){\n\t\t\t\tfie[i][j]=1;\n\t\t\t\tint di=bfs();\n\t\t\t\tif(di!=-1)res=max(di,res);\n\t\t\t\tfie[i][j]=0;\n\t\t\t}\n\t\t}\n\t}\n\tprintf(\"%d\\n\",res-prev);\n\treturn 0;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 1136, "score_of_the_acc": -0.02, "final_rank": 1 }, { "submission_id": "aoj_2225_1704717", "code_snippet": "#include <cstdio>\n#include <cstring>\n#include <vector>\n#include <queue>\n#include <algorithm>\n\nusing namespace std;\ntypedef pair<int,int> P;\nint w,h,n;\nint fie[101][101];\nint dist[101][101];\nint ix,iy,ox,oy;\nint dx[4]={1,0,-1,0};\nint dy[4]={0,-1,0,1};\n\nint bfs(){\n\tqueue<P> que;\n\tmemset(dist,-1,sizeof(dist));\n\tque.push(P(ix,iy));\n\tdist[iy][ix]=0;\n\twhile(que.size()){\n\t\tP q=que.front();\n\t\tque.pop();\n\t\tfor(int i=0;i<4;i++){\n\t\t\tint nx=q.first+dx[i],ny=q.second+dy[i];\n\t\t\tif(fie[ny][nx]==0){\n\t\t\t\tnx+=dx[i];\n\t\t\t\tny+=dy[i];\n\t\t\t\tif(dist[ny][nx]!=-1)continue;\n\t\t\t\tdist[ny][nx]=dist[q.second][q.first]+1;\n\t\t\t\tque.push(P(nx,ny));\n\t\t\t}\n\t\t}\n\t}\n\treturn dist[oy][ox];\n}\n\nint main(void){\n\tscanf(\"%d %d %d\",&w,&h,&n);\n\tw*=2;\n\th*=2;\n\tfor(int i=0;i<=h;i++){\n\t\tfie[i][0]=1;\n\t\tfie[i][w]=1;\n\t}\n\tfor(int i=0;i<=w;i++){\n\t\tfie[0][i]=1;\n\t\tfie[h][i]=1;\n\t}\n\tfor(int i=0;i<n;i++){\n\t\tint sx,sy,ddx,ddy;\n\t\tscanf(\"%d %d %d %d\",&sx,&sy,&ddx,&ddy);\n\t\tif(sx>ddx)swap(sx,ddx);\n\t\tif(sy>ddy)swap(sy,ddy);\n\t\tsx*=2;\n\t\tsy*=2;\n\t\tddx*=2;\n\t\tddy*=2;\n\t\tfor(int j=sx;j<=ddx;j++){\n\t\t\tfie[ddy][j]=1;\n\t\t\tfie[sy][j]=1;\n\t\t}\n\t\tfor(int j=sy;j<=ddy;j++){\n\t\t\tfie[j][ddx]=1;\n\t\t\tfie[j][sx]=1;\n\t\t}\n\t}\n\tscanf(\"%d%d\",&ix,&iy);\n\tscanf(\"%d%d\",&ox,&oy);\n\tix=ix*2+1;\n\tiy=iy*2+1;\n\tox=ox*2+1;\n\toy=oy*2+1;\n\tint prev=bfs();\n\tint res=0;\n\tfor(int i=1;i<h;i+=2){\n\t\tfor(int j=0;j<w;j+=2){\n\t\t\tif(fie[i][j]==0){\n\t\t\t\tfie[i][j]=1;\n\t\t\t\tint di=bfs();\n\t\t\t\tif(di!=-1)res=max(di,res);\n\t\t\t\tfie[i][j]=0;\n\t\t\t}\n\t\t}\n\t}\n\tfor(int i=0;i<h;i+=2){\n\t\tfor(int j=1;j<w;j+=2){\n\t\t\tif(fie[i][j]==0){\n\t\t\t\tfie[i][j]=1;\n\t\t\t\tint di=bfs();\n\t\t\t\tif(di!=-1)res=max(di,res);\n\t\t\t\tfie[i][j]=0;\n\t\t\t}\n\t\t}\n\t}\n\tprintf(\"%d\\n\",res-prev);\n\treturn 0;\n}", "accuracy": 0.8461538461538461, "time_ms": 60, "memory_kb": 1136, "score_of_the_acc": -0.02, "final_rank": 18 }, { "submission_id": "aoj_2225_1663157", "code_snippet": "#include \"bits/stdc++.h\"\n#include<unordered_map>\n#pragma warning(disable:4996)\nusing namespace std;\n\nint walls[55][55][4];\n\nconst int dx[4] = { -1,0,1,0 };\nconst int dy[4] = { 0,1,0,-1 };\n\nstruct aa {\n\tint x;\n\tint y;\n\tint time;\n\tvector<int>xs;\n\tvector<int>ys;\n};\nclass Compare {\npublic:\n\t//aaが昇順に並ぶ\n\tbool operator()(const aa&l, const aa&r) {\n\t\treturn l.time> r.time;\n\t}\n};\naa getway(const int fx, const int fy, const int tx, const int ty) {\n\tpriority_queue<aa, vector<aa>, Compare>que;\n\tque.push(aa{ fx,fy,0,{},{} });\n\tint memo[100][100];\n\tfor (int i = 0; i < 100; ++i) {\n\t\tfor (int j = 0; j < 100; ++j) {\n\t\t\tmemo[i][j] = 99999999;\n\t\t}\n\t}\n\tmemo[fy][fx] = 0;\n\twhile (!que.empty()) {\n\t\taa atop(que.top());\n\t\tque.pop();\n\t\tif (atop.x == tx&&atop.y == ty) {\n\t\t\treturn atop;\n\t\t}\n\t\telse {\n\t\t\tatop.xs.push_back(atop.x);\n\t\t\tatop.ys.push_back(atop.y);\n\t\t}\n\t\tvector<int> axs(atop.xs);\n\t\tvector<int> ays(atop.ys);\n\t\tfor (int i = 0; i < 4; ++i) {\n\t\t\tint newx = atop.x + dx[i];\n\t\t\tint newy = atop.y + dy[i];\n\t\t\tif (!walls[atop.y][atop.x][i]) {\n\t\t\t\tif (memo[newy][newx]>atop.time + 1) {\n\t\t\t\t\tmemo[newy][newx] = atop.time + 1;\n\t\t\t\t\tque.push(aa{ newx,newy,atop.time + 1,atop.xs,atop.ys });\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\treturn aa{ -10000,0,0,{},{} };\n}\n\nint main() {\n\tint W, H,N; cin >> W >> H >> N;\n\tfor (int i = 0; i < N; ++i) {\n\t\tint sx, sy, dx, dy; cin >> sx >> sy >> dx >> dy;\n\t\tsy = H - sy;\n\t\tdy = H - dy;\n\t\tif (sx == dx) {\n\t\t\tif (sy > dy)swap(sy, dy);\n\t\t\tfor (int y = sy; y < dy; ++y) {\n\t\t\t\tif(sx-1>=0)walls[y][sx - 1][2] = true;\n\t\t\t\tif (sx < W)walls[y][sx][0] = true;\n\t\t\t}\n\t\t}\n\t\telse {\n\t\t\tif (sx > dx)swap(sx, dx);\n\t\t\tfor (int x = sx; x < dx; ++x) {\n\t\t\t\tif(sy-1>=0)walls[sy-1][x][1] = true;\n\t\t\t\tif(sy<H)walls[sy][x][3] = true;\n\t\t\t}\n\t\t}\n\t}\n\tfor (int i = 0; i < H; ++i) {\n\t\tfor (int j = 0; j < W; ++j) {\n\t\t\tfor (int k = 0; k < 4; ++k) {\n\t\t\t\tif (i + dy[k] < 0 || i + dy[k] >= H) {\n\t\t\t\t\twalls[i][j][k] = true;\n\t\t\t\t}\n\t\t\t\tif (j + dx[k] < 0 || j + dx[k] >= W) {\n\t\t\t\t\twalls[i][j][k] = true;\n\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tint startx, starty, goalx, goaly; cin >> startx >> starty >> goalx >> goaly;\n\tstarty = H - starty - 1;\n\tgoaly = H - goaly - 1;\n\n\taa fstway =getway(startx, starty, goalx, goaly);\n\tfstway.xs.push_back(goalx);\n\tfstway.ys.push_back(goaly);\n\tint fsttime = fstway.time;\n\tint ans = 0;\n\tif (!fstway.xs.empty()) {\n\t\tfor (int i = 0; i < fstway.xs.size() - 1; ++i) {\n\t\t\tfor (int j = 0; j < 4; ++j) {\n\t\t\t\tif (fstway.xs[i] + dx[j] == fstway.xs[i + 1] && fstway.ys[i] + dy[j] == fstway.ys[i + 1]) {\n\t\t\t\t\twalls[fstway.ys[i]][fstway.xs[i]][j] = true;\n\t\t\t\t\twalls[fstway.ys[i + 1]][fstway.xs[i + 1]][(2 + j) % 4] = true;\n\t\t\t\t\taa nowway(getway(startx, starty, goalx, goaly));\n\t\t\t\t\tif (nowway.time) {\n\t\t\t\t\t\tans = max(ans, nowway.time - fsttime);\n\t\t\t\t\t}\n\t\t\t\t\twalls[fstway.ys[i]][fstway.xs[i]][j] = false;\n\t\t\t\t\twalls[fstway.ys[i + 1]][fstway.xs[i + 1]][(2 + j) % 4] = false;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\t\n\tcout << ans << endl;\n\treturn 0;\n}", "accuracy": 1, "time_ms": 2510, "memory_kb": 3244, "score_of_the_acc": -1.1618, "final_rank": 17 }, { "submission_id": "aoj_2225_1663117", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef long double ld;\ntypedef pair<ll, ll> P;\n\n#define EACH(i,a) for (auto& i : a)\n#define FOR(i,a,b) for (ll i=(a);i<(b);i++)\n#define RFOR(i,a,b) for (ll i=(b)-1;i>=(a);i--)\n#define REP(i,n) for (ll i=0;i<(n);i++)\n#define RREP(i,n) for (ll i=(n)-1;i>=0;i--)\n#define debug(x) cout<<#x<<\": \"<<x<<endl\n#define pb push_back\n#define ALL(a) (a).begin(),(a).end()\n\nconst ll linf = 1e18;\nconst int inf = 1e9;\nconst double eps = 1e-12;\nconst double pi = acos(-1);\n\ntemplate<typename T>\nistream& operator>>(istream& is, vector<T>& vec) {\n\tEACH(x,vec) is >> x;\n\treturn is;\n}\n/*\ntemplate<class... T>\nostream& operator<<(ostream& os, tuple<T...>& t) {\n\tfor (size_t i = 0; i < tuple_size< tuple<T...> >::value; ++i) {\n\t\tif (i) os << \" \";\n\t\tos << get<0>(t);\n\t}\n\treturn os;\n}\n*/\ntemplate<typename T>\nostream& operator<<(ostream& os, vector<T>& vec) {\n\tREP(i,vec.size()) {\n\t\tif (i) os << \" \";\n\t\tos << vec[i];\n\t}\n\treturn os;\n}\ntemplate<typename T>\nostream& operator<<(ostream& os, vector< vector<T> >& vec) {\n\tREP(i,vec.size()) {\n\t\tif (i) os << endl;\n\t\tos << vec[i];\n\t}\n\treturn os;\n}\n\nint dx[4] = {0, 1, 0, -1};\nint dy[4] = {-1, 0, 1, 0};\nint wall[51][51][51][51] = {0};\nint W, H;\nbool inRange(int x, int y) {\n\treturn 0 <= x && x < W && 0 <= y && y < H;\n}\nbool inRange(P p) {\n\tint x, y; tie(x, y) = p;\n\treturn inRange(x, y);\n}\nll bfs(P s, P t) {\n\tll dist[51][51]; fill(dist[0], dist[51], linf); dist[s.second][s.first] = 0;\n\tqueue<P> Q; Q.push(s);\n\twhile ( !Q.empty() ) {\n\t\tP p = Q.front(); Q.pop();\n\t\tint x, y; tie(x, y) = p;\n\t\tREP(d, 4) {\n\t\t\tint nx = x + dx[d], ny = y + dy[d];\n\t\t\tif ( !inRange(nx, ny) ) continue;\n\t\t\tif ( wall[x][y][nx][ny] ) continue;\n\t\t\tif ( dist[y][x]+1 < dist[ny][nx] ) {\n\t\t\t\tdist[ny][nx] = dist[y][x]+1;\n\t\t\t\tQ.push(P(nx, ny));\n\t\t\t}\n\t\t}\n\t}\n\treturn dist[t.second][t.first];\n}\nP operator-(const P& p1, const P& p2) {\n\treturn P(p1.first-p2.first, p1.second-p2.second);\n}\nP operator+(const P& p1, const P& p2) {\n\treturn P(p1.first+p2.first, p1.second+p2.second);\n}\nP operator/(const P& p, const int& d) {\n\treturn P(p.first/d, p.second/d);\n}\nint main() {\n\tstd::ios::sync_with_stdio(false);\n\tstd::cin.tie(0);\n\tint N; cin >> W >> H >> N;\n\tREP(i, N) {\n\t\tint x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2;\n\t\tP p1(x1, y1), p2(x2, y2);\n\t\tP d = p2-p1;\n\t\td = d / (abs(d.first) + abs(d.second));\n\t\tassert( abs(d.first) + abs(d.second) == 1 );\n\t\tP p = p1;\n\t\twhile (1) {\n\t\t\tmap<P, int> m;\n\t\t\tFOR(x, -1, 1) FOR(y, -1, 1) {\n\t\t\t\tP p1(p.first+x, p.second+y);\n\t\t\t\tP p2(p.first+d.first+x, p.second+d.second+y);\n\t\t\t\tm[p1]++;\n\t\t\t\tm[p2]++;\n\t\t\t}\n\t\t\tvector<P> v;\n\t\t\tEACH(p, m) if (p.second == 2) v.pb(p.first);\n\t\t\tassert( v.size() == 2 );\n\t\t\tif (inRange(v[0]) && inRange(v[1])) {\n\t\t\t\twall[v[0].first][v[0].second][v[1].first][v[1].second]++;\n\t\t\t\twall[v[1].first][v[1].second][v[0].first][v[0].second]++;\n\t\t\t}\n\t\t\tp = p + d;\n\t\t\tif (p == p2) break;\n\t\t}\n\t}\n\tint sx, sy, gx, gy; cin >> sx >> sy >> gx >> gy;\n\tll base = bfs(P(sx, sy), P(gx, gy));\n\tll ans = 0;\n\tREP(y, H) REP(x, W) REP(d, 4) {\n\t\tint nx = x+dx[d], ny = y+dy[d];\n\t\tif ( !inRange(nx, ny) ) continue;\n\t\t++wall[x][y][nx][ny];\n\t\t++wall[nx][ny][x][y];\n\t\tll step = bfs(P(sx, sy), P(gx, gy));\n//\t\tcout << x << \" \" << y << \" \" << nx << \" \" << ny << \" \" << step << endl;\n\t\tif (step != linf) {\n\t\t\tans = max(ans, step-base);\n\t\t}\n\t\t--wall[x][y][nx][ny];\n\t\t--wall[nx][ny][x][y];\n\t}\n\tcout << ans << endl;\n}", "accuracy": 1, "time_ms": 340, "memory_kb": 14164, "score_of_the_acc": -1.132, "final_rank": 16 }, { "submission_id": "aoj_2225_1607870", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define for_(i,a,b) for(int i=(a);i<(b);++i)\n\nint W, H, N, ix, iy, ox, oy;\nbool movable[55][55][4];\n\nconst int dx[4] = {0,1,0,-1};\nconst int dy[4] = {1,0,-1,0};\n\nbool in_range(int x, int y) { return 0 <= x && x < W && 0 <= y && y < H; }\n\nstruct State { int x, y; };\n\nint step[55][55];\n\nint bfs() {\n\tmemset(step, -1, sizeof(step));\n\t\n\tqueue< State > que;\n\tque.push(State{ix, iy});\n\t\n\twhile (!que.empty()) {\n\t\tState s = que.front(); que.pop();\n\t\t\n\t\tfor_(d,0,4) {\n\t\t\tif (!movable[s.y][s.x][d]) continue;\n\t\t\t\n\t\t\tint nx = s.x + dx[d], ny = s.y + dy[d];\n\t\t\t\n\t\t\tif (!in_range(nx, ny)) continue;\n\t\t\t\n\t\t\tif (step[ny][nx] == -1) {\n\t\t\t\tstep[ny][nx] = step[s.y][s.x] + 1;\n\t\t\t\tque.push(State{nx, ny});\n\t\t\t}\n\t\t}\n\t}\n\t\n\treturn step[oy][ox];\n}\n\nvoid solve() {\n\tint ini = bfs(), ans = 0;\n\t\n\tfor_(y,0,H) for_(x,0,W) {\n\t\tif (x > 0 && movable[y][x][3]) {\n\t\t\tmovable[y][x][3] = movable[y][x - 1][1] = false;\n\t\t\tans = max(ans, bfs() - ini);\n\t\t\tmovable[y][x][3] = movable[y][x - 1][1] = true;\n\t\t}\n\t\t\n\t\tif (y > 0 && movable[y][x][2]) {\n\t\t\tmovable[y][x][2] = movable[y - 1][x][0] = false;\n\t\t\tans = max(ans, bfs() - ini);\n\t\t\tmovable[y][x][2] = movable[y - 1][x][0] = true;\n\t\t}\n\t}\n\t\n\tcout << ans << endl;\n}\n\nint main() {\n\tcin >> W >> H >> N;\n\t\n\tfor_(y,0,H) for_(x,0,W) for_(d,0,4) movable[y][x][d] = true;\n\t\n\tfor_(i,0,N) {\n\t\tint sx, sy, ex, ey;\n\t\tcin >> sx >> sy >> ex >> ey;\n\t\t\n\t\tif (sx > ex) swap(sx, ex);\n\t\tif (sy > ey) swap(sy, ey);\n\t\t\n\t\tif (sy > 0) for_(x,sx,ex) movable[sy][x][2] = movable[sy - 1][x][0] = false;\n\t\tif (sx > 0) for_(y,sy,ey) movable[y][sx][3] = movable[y][sx - 1][1] = false;\n\t}\n\t\n\tcin >> ix >> iy;\n\tcin >> ox >> oy;\n\t\n\tsolve();\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 1244, "score_of_the_acc": -0.0243, "final_rank": 3 }, { "submission_id": "aoj_2225_1605294", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define LOG(...) fprintf(stderr,__VA_ARGS__)\n//#define LOG(...)\n#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);++i)\n#define REP(i,n) for(int i=0;i<(int)(n);++i)\n#define ALL(a) (a).begin(),(a).end()\n#define RALL(a) (a).rbegin(),(a).rend()\n#define EXIST(s,e) ((s).find(e)!=(s).end())\n#define SORT(c) sort(ALL(c))\n#define RSORT(c) sort(RALL(c))\n\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef vector<bool> vb;\ntypedef vector<int> vi;\ntypedef vector<ll> vll;\ntypedef vector<vb> vvb;\ntypedef vector<vi> vvi;\ntypedef vector<vll> vvll;\ntypedef pair<int,int> pii;\ntypedef pair<ll,ll> pll;\n\nconst int dx[] = {-1,0,1,0}, dy[] = {0,1,0,-1};\nconst int ddx[] = {1,1,0,1}, ddy[] = {1,0,1,1};\n\nbool hwall[51][51], vwall[51][51];\nint W, H;\nint ix, iy, ox, oy;\n\nint min_steps() {\n queue<pii> que;\n que.push({ix, iy});\n pii sentry = {-1, -1};\n que.push(sentry);\n bool G[50][50] = {};\n G[iy][ix] = true;\n int steps = 0;\n while (!que.empty()) {\n pii p = que.front(); que.pop();\n int x = p.first, y = p.second;\n if (p == sentry) {\n steps++;\n if (que.empty()) {\n return -1; // unreachable\n }\n que.push(sentry);\n continue;\n }\n if (x == ox && y == oy) {\n return steps;\n }\n\n REP(i, 4) {\n int sx = x + dx[i], sy = y + dy[i];\n if (0 <= sx && sx < W && 0 <= sy && sy < H) {\n if (i % 2 == 0 && vwall[sy+ddy[i]][sx+ddx[i]]) continue;\n if (i % 2 == 1 && hwall[sy+ddy[i]][sx+ddx[i]]) continue;\n if (!G[sy][sx]) {\n G[sy][sx] = true;\n que.push({sx, sy});\n }\n }\n }\n }\n}\n\nint max_steps() {\n queue<pii> que;\n que.push({ix, iy});\n // ????????????????????????\n // ??´????????????cost-1???????????????????????¨?????§?????????????????°??§????????????????±????????????¨?????§??????\n int G[50][50];\n fill_n((int *)G, 50*50, -1);\n G[iy][ix] = 0;\n while (!que.empty()) {\n pii p = que.front(); que.pop();\n int x = p.first, y = p.second;\n if (x == ox && y == oy) break;\n\n REP(i, 4) {\n int sx = x + dx[i], sy = y + dy[i];\n if (0 <= sx && sx < W && 0 <= sy && sy < H) {\n if (i % 2 == 0 && vwall[sy+ddy[i]][sx+ddx[i]]) continue;\n if (i % 2 == 1 && hwall[sy+ddy[i]][sx+ddx[i]]) continue;\n if (G[sy][sx] == -1) {\n G[sy][sx] = G[y][x] + 1;\n que.push({sx, sy});\n }\n }\n }\n }\n\n// for (int y = H-1; y >= 0; y--) {\n// REP(x, W) {\n// if (G[y][x] == -1) {\n// LOG(\"#\");\n// } else {\n// if (G[y][x] > (0x7e - 0x30)) {\n// LOG(\"$\");\n// } else {\n// LOG(\"%c\", '0' + G[y][x]);\n// }\n// }\n// }\n// LOG(\"\\n\");\n// }\n// LOG(\"min: %d\\n\", G[oy][ox]);\n\n // ?????????\n bool intersection2[50][50] = {};\n vector<pii> intersection;\n intersection.push_back({ix, iy});\n intersection.push_back({ox, oy});\n\n while (!que.empty()) que.pop();\n que.push({ox, oy});\n bool G2[50][50] = {};\n G2[oy][ox] = true;\n while (!que.empty()) {\n pii p = que.front(); que.pop();\n int x = p.first, y = p.second;\n if (x == ix && y == iy) break;\n int inter = 0;\n REP(i, 4) {\n int sx = x + dx[i], sy = y + dy[i];\n if (0 <= sx && sx < W && 0 <= sy && sy < H) {\n if (G[sy][sx] == G[y][x] - 1 || G[sy][sx] == G[y][x] + 1) {\n inter++;\n }\n if (!G2[sy][sx] && G[sy][sx] == G[y][x] - 1) {\n G2[sy][sx] = true;\n que.push({sx, sy});\n }\n }\n }\n if (inter > 1) {\n intersection2[y][x] = true;\n intersection.push_back({x, y});\n }\n }\n\n// for (int y = H; y >= 1; y--) {\n// FOR(x, 1, W+1) {\n// LOG(\"%c+\", hwall[y][x] ? '-' : ' ');\n// }\n// LOG(\"\\n\");\n// FOR(x, 1, W) {\n// if (intersection2[y-1][x-1]) {\n// LOG(\"#\");\n// } else if (G2[y-1][x-1]) {\n// LOG(\".\");\n// } else {\n// LOG(\" \");\n// }\n// LOG(\"%c\", vwall[y][x] ? '|' : ' ');\n// }\n// LOG(\"\\n\");\n// }\n\n int ma = G[oy][ox];\n for (pii p : intersection) {\n int x = p.first, y = p.second;\n REP(i, 4) {\n if (i % 2 == 0 && !vwall[y+ddy[i]][x+ddx[i]]) {\n vwall[y+ddy[i]][x+ddx[i]] = true;\n ma = max(ma, min_steps());\n vwall[y+ddy[i]][x+ddx[i]] = false;\n }\n if (i % 2 == 1 && !hwall[y+ddy[i]][x+ddx[i]]) {\n hwall[y+ddy[i]][x+ddx[i]] = true;\n ma = max(ma, min_steps());\n hwall[y+ddy[i]][x+ddx[i]] = false;\n }\n }\n }\n// LOG(\"max: %d\\n\", ma);\n return ma - G[oy][ox];\n}\n\nint main() {\n int n;\n cin >> W >> H >> n;\n REP(i, n) {\n int sx, sy, dx, dy;\n cin >> sx >> sy >> dx >> dy;\n while (sx != dx) {\n if (sx < dx) {\n sx++;\n hwall[sy][sx] = true;\n } else if (sx > dx) {\n sx--;\n hwall[sy][sx+1] = true;\n }\n }\n while (sy != dy) {\n if (sy < dy) {\n sy++;\n vwall[sy][sx] = true;\n } else if (sy > dy) {\n sy--;\n vwall[sy+1][sx] = true;\n }\n }\n }\n cin >> ix >> iy >> ox >> oy;\n\n// for (int y = H+1; y >= 1; y--) {\n// FOR(x, 1, W+1) {\n// LOG(\"%c+\", hwall[y][x] ? '-' : ' ');\n// }\n// LOG(\"\\n\");\n// FOR(x, 1, W+1) {\n// if (x == ix+1 && y == iy+1) {\n// LOG(\"S\");\n// } else if (x == ox+1 && y == oy+1) {\n// LOG(\"G\");\n// } else {\n// LOG(\" \");\n// }\n// LOG(\"%c\", vwall[y][x] ? '|' : ' ');\n// }\n// LOG(\"\\n\");\n// }\n\n cout << max_steps() << endl;\n}", "accuracy": 1, "time_ms": 130, "memory_kb": 3184, "score_of_the_acc": -0.2052, "final_rank": 9 }, { "submission_id": "aoj_2225_1605292", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define LOG(...) fprintf(stderr,__VA_ARGS__)\n//#define LOG(...)\n#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);++i)\n#define REP(i,n) for(int i=0;i<(int)(n);++i)\n#define ALL(a) (a).begin(),(a).end()\n#define RALL(a) (a).rbegin(),(a).rend()\n#define EXIST(s,e) ((s).find(e)!=(s).end())\n#define SORT(c) sort(ALL(c))\n#define RSORT(c) sort(RALL(c))\n\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef vector<bool> vb;\ntypedef vector<int> vi;\ntypedef vector<ll> vll;\ntypedef vector<vb> vvb;\ntypedef vector<vi> vvi;\ntypedef vector<vll> vvll;\ntypedef pair<int,int> pii;\ntypedef pair<ll,ll> pll;\n\nconst int dx[] = {-1,0,1,0}, dy[] = {0,1,0,-1};\nconst int ddx[] = {1,1,0,1}, ddy[] = {1,0,1,1};\n\nbool hwall[51][51], vwall[51][51];\nint W, H;\nint ix, iy, ox, oy;\n\nint min_steps() {\n queue<pii> que;\n que.push({ix, iy});\n pii sentry = {-1, -1};\n que.push(sentry);\n bool G[50][50] = {};\n G[iy][ix] = true;\n int steps = 0;\n while (!que.empty()) {\n pii p = que.front(); que.pop();\n int x = p.first, y = p.second;\n if (p == sentry) {\n steps++;\n if (que.empty()) {\n return -1; // unreachable\n }\n que.push(sentry);\n continue;\n }\n if (x == ox && y == oy) {\n return steps;\n }\n\n REP(i, 4) {\n int sx = x + dx[i], sy = y + dy[i];\n if (0 <= sx && sx < W && 0 <= sy && sy < H) {\n if (i % 2 == 0 && vwall[sy+ddy[i]][sx+ddx[i]]) continue;\n if (i % 2 == 1 && hwall[sy+ddy[i]][sx+ddx[i]]) continue;\n if (!G[sy][sx]) {\n G[sy][sx] = true;\n que.push({sx, sy});\n }\n }\n }\n }\n}\n\nint max_steps() {\n queue<pii> que;\n que.push({ix, iy});\n // ????????????????????????\n // ??´????????????cost-1???????????????????????¨?????§?????????????????°??§????????????????±????????????¨?????§??????\n int G[50][50];\n fill_n((int *)G, 50*50, -1);\n G[iy][ix] = 0;\n while (!que.empty()) {\n pii p = que.front(); que.pop();\n int x = p.first, y = p.second;\n if (x == ox && y == oy) break;\n\n REP(i, 4) {\n int sx = x + dx[i], sy = y + dy[i];\n if (0 <= sx && sx < W && 0 <= sy && sy < H) {\n if (i % 2 == 0 && vwall[sy+ddy[i]][sx+ddx[i]]) continue;\n if (i % 2 == 1 && hwall[sy+ddy[i]][sx+ddx[i]]) continue;\n if (G[sy][sx] == -1) {\n G[sy][sx] = G[y][x] + 1;\n que.push({sx, sy});\n }\n }\n }\n }\n\n// for (int y = H-1; y >= 0; y--) {\n// REP(x, W) {\n// if (G[y][x] == -1) {\n// LOG(\"#\");\n// } else {\n// if (G[y][x] > (0x7e - 0x30)) {\n// LOG(\"$\");\n// } else {\n// LOG(\"%c\", '0' + G[y][x]);\n// }\n// }\n// }\n// LOG(\"\\n\");\n// }\n// LOG(\"min: %d\\n\", G[oy][ox]);\n\n // ?????????\n bool intersection2[50][50] = {};\n vector<pii> intersection;\n intersection.push_back({ix, iy});\n intersection.push_back({ox, oy});\n\n while (!que.empty()) que.pop();\n que.push({ox, oy});\n bool G2[50][50] = {};\n G2[oy][ox] = true;\n while (!que.empty()) {\n pii p = que.front(); que.pop();\n int x = p.first, y = p.second;\n if (x == ix && y == iy) break;\n int inter = 0;\n REP(i, 4) {\n int sx = x + dx[i], sy = y + dy[i];\n if (0 <= sx && sx < W && 0 <= sy && sy < H) {\n if (G[sy][sx] == G[y][x] - 1 || G[sy][sx] == G[y][x] + 1) {\n inter++;\n }\n if (!G2[sy][sx] && G[sy][sx] == G[y][x] - 1) {\n G2[sy][sx] = true;\n que.push({sx, sy});\n }\n }\n }\n if (inter > 1) {\n intersection2[y][x] = true;\n intersection.push_back({x, y});\n }\n }\n\n// for (int y = H; y >= 1; y--) {\n// FOR(x, 1, W+1) {\n// LOG(\"%c+\", hwall[y][x] ? '-' : ' ');\n// }\n// LOG(\"\\n\");\n// FOR(x, 1, W) {\n// if (intersection2[y-1][x-1]) {\n// LOG(\"#\");\n// } else if (G2[y-1][x-1]) {\n// LOG(\".\");\n// } else {\n// LOG(\" \");\n// }\n// LOG(\"%c\", vwall[y][x] ? '|' : ' ');\n// }\n// LOG(\"\\n\");\n// }\n\n int ma = 0;\n for (pii p : intersection) {\n int x = p.first, y = p.second;\n REP(i, 4) {\n if (i % 2 == 0 && !vwall[y+ddy[i]][x+ddx[i]]) {\n vwall[y+ddy[i]][x+ddx[i]] = true;\n ma = max(ma, min_steps());\n vwall[y+ddy[i]][x+ddx[i]] = false;\n }\n if (i % 2 == 1 && !hwall[y+ddy[i]][x+ddx[i]]) {\n hwall[y+ddy[i]][x+ddx[i]] = true;\n ma = max(ma, min_steps());\n hwall[y+ddy[i]][x+ddx[i]] = false;\n }\n }\n }\n// LOG(\"max: %d\\n\", ma);\n return ma - G[oy][ox];\n}\n\nint main() {\n int n;\n cin >> W >> H >> n;\n REP(i, n) {\n int sx, sy, dx, dy;\n cin >> sx >> sy >> dx >> dy;\n while (sx != dx) {\n if (sx < dx) {\n sx++;\n hwall[sy][sx] = true;\n } else if (sx > dx) {\n sx--;\n hwall[sy][sx+1] = true;\n }\n }\n while (sy != dy) {\n if (sy < dy) {\n sy++;\n vwall[sy][sx] = true;\n } else if (sy > dy) {\n sy--;\n vwall[sy+1][sx] = true;\n }\n }\n }\n cin >> ix >> iy >> ox >> oy;\n\n// for (int y = H+1; y >= 1; y--) {\n// FOR(x, 1, W+1) {\n// LOG(\"%c+\", hwall[y][x] ? '-' : ' ');\n// }\n// LOG(\"\\n\");\n// FOR(x, 1, W+1) {\n// if (x == ix+1 && y == iy+1) {\n// LOG(\"S\");\n// } else if (x == ox+1 && y == oy+1) {\n// LOG(\"G\");\n// } else {\n// LOG(\" \");\n// }\n// LOG(\"%c\", vwall[y][x] ? '|' : ' ');\n// }\n// LOG(\"\\n\");\n// }\n\n cout << max_steps() << endl;\n}", "accuracy": 0.6410256410256411, "time_ms": 10, "memory_kb": 3184, "score_of_the_acc": -0.1572, "final_rank": 19 }, { "submission_id": "aoj_2225_1569274", "code_snippet": "#include<iostream>\n#include<cstdlib>\n#include<cstring>\n#include<cstdio>\n#include<cmath>\n#include<ctime>\n#include<algorithm>\n#include<list>\n#include<queue>\n#include<stack>\n#include<vector>\n#include<set>\n#include<map>\n#include<bitset>\n#include<string>\n#define sgn(v) (abs((v))<eps?0:((v)<0?-1:1))\n#define sqr(v) ((v)*(v))\n#define tri(v) ((v)*(v)*(v))\n#define MP make_pair\n#define PB push_back\n#define FI first\n#define SE second\nusing namespace std;\ntypedef long long LL;\ntypedef long double Double;\ntypedef unsigned long long ULL;\ntypedef pair<double,int> PDI;\ntypedef pair<double,double> PDD;\ntypedef pair<int,int> PII;\ntypedef pair<int,LL> PIL;\ntypedef pair<LL,int> PLI;\ntypedef pair<LL,LL> PLL;\nconst double eps=1e-8;\nconst double pi=acos(-1);\nconst int maxn=3000;\nbool mp[55][55][55][55];\nint dx[]={1,0,-1,0},dy[]={0,1,0,-1};\nstruct Edge\n{\n\tint v,next;\n}edge[maxn*4];\nint head[maxn],ne;\nvoid add(int u,int v)\n{\n\tedge[ne].v=v;\n\tedge[ne].next=head[u];\n\thead[u]=ne++;\n}\nqueue<int> q;\nbool inq[maxn];\nint ix,iy,ox,oy,w,h;\nint dis[maxn];\nconst int inf=1e9;\nvoid construct(int debug=0)\n{\n\tint i,j,k,x,y;\n\tmemset(head,-1,sizeof(head));\n\tne=0;\n\tfor(i=0;i<w;++i)\n\t\tfor(j=0;j<h;++j)\n\t\t\tfor(k=0;k<4;++k)\n\t\t\t{\n\t\t\t\tx=i+dx[k];\n\t\t\t\ty=j+dy[k];\n\t\t\t\tif(x<0||x==w||y<0||y==h)continue;\n\t\t\t\t//if(debug&&i==0&&j==0&&x==0&&y==1)cout <<\"mp:\"<<mp[i][j][x][y]<<endl;\n\t\t\t\t//if(debug&&i==0&&j==0&&x==1&&y==0)cout <<\"mp:\"<<mp[i][j][x][y]<<endl;\n\t\t\t\tif(!mp[i][j][x][y])add(i*h+j,x*h+y);\n\t\t\t}\n}\nint bfs(int debug=0)\n{\n\tint u,v,x,y,i;\n\tfor(i=0;i<w*h;++i)dis[i]=inf;\n\tu=ix*h+iy;\n\tdis[u]=0;\n\tq.push(u);\n\tinq[u]=true;\n\twhile(!q.empty())\n\t{\n\t\tu=q.front();\n\t\tinq[u]=false;\n\t\tq.pop();\n\t\tx=u/h;\n\t\ty=u%h;\n\t\tif(debug)\n\t\t{\n\t\t\tcout <<x<<\" \"<<y<<\" \"<<dis[u]<<endl;getchar();\n\t\t}\n\t\tfor(i=head[u];i!=-1;i=edge[i].next)\n\t\t{\n\t\t\tv=edge[i].v;\n\t\t\tif(dis[u]+1<dis[v])\n\t\t\t{\n\t\t\t\tdis[v]=dis[u]+1;\n\t\t\t\tif((x!=ox||y!=oy)&&(!inq[v]))\n\t\t\t\t{\n\t\t\t\t\tinq[v]=true;\n\t\t\t\t\tq.push(v);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\t//if(debug)cout <<ox*h+oy<<endl;\n\treturn dis[ox*h+oy];\n}\nint main()\n{\n\tint n,i,j,sx,sy,dx,dy;\n\tscanf(\"%d%d%d\",&w,&h,&n);\n\tfor(j=0;j<n;++j)\n\t{\n\t\tscanf(\"%d%d%d%d\",&sx,&sy,&dx,&dy);\n\t\t//cout <<sx<<\" \"<<sy<<\" \"<<dx<<\" \"<<dy<<endl;\n\t\tif(sx==dx&&sx>0)\n\t\t{\n\t\t\tif(sy>dy)swap(sy,dy);\n\t\t\tfor(i=sy;i<dy;++i)\n\t\t\t{\n\t\t\t\tmp[sx-1][i][sx][i]=true;\n\t\t\t\tmp[sx][i][sx-1][i]=true;\n\t\t\t}\n\t\t}\n\t\telse if(sy==dy&&sy>0)\n\t\t{\n\t\t\tif(sx>dx)swap(sx,dx);\n\t\t\tfor(i=sx;i<dx;++i)\n\t\t\t{\n\t\t\t\tmp[i][sy-1][i][sy]=true;\n\t\t\t\tmp[i][sy][i][sy-1]=true;\n\t\t\t}\n\t\t}\n\t}\n\t//cout <<mp[1][0][1][1]<<endl;\n\tscanf(\"%d%d\",&ix,&iy);\n\tscanf(\"%d%d\",&ox,&oy);\n\tconstruct();\n\tint ans=bfs(),maxv,res;\n\tmaxv=ans;\n\t//cout <<\"Here:\"<<ans<<endl;\n\tfor(i=1;i<w;++i)\n\t\tfor(j=0;j<h;++j)\n\t\t\tif(!mp[i-1][j][i][j])\n\t\t\t{\n\t\t\t\tmp[i-1][j][i][j]=mp[i][j][i-1][j]=true;\n\t\t\t\tconstruct();\n\t\t\t\tres=bfs();\n\t\t\t\tif(res!=inf)maxv=max(maxv,res);\n\t\t\t\tmp[i-1][j][i][j]=mp[i][j][i-1][j]=false;\n\t\t\t}\n\tfor(j=1;j<h;++j)\n\t\tfor(i=0;i<w;++i)\n\t\t\tif(!mp[i][j-1][i][j])\n\t\t\t{\n\t\t\t\tmp[i][j-1][i][j]=mp[i][j][i][j-1]=true;\n\t\t\t\tconstruct();\n\t\t\t\tres=bfs();\n\t\t\t\tif(res!=inf)maxv=max(maxv,res);\n\t\t\t\tmp[i][j-1][i][j]=mp[i][j][i][j-1]=false;\n\t\t\t}\n\t//cout <<ans<<\" \"<<maxv<<endl;\n\tif(maxv==inf)puts(\"0\");\n\telse printf(\"%d\\n\",maxv-ans);\n\treturn 0;\n}", "accuracy": 1, "time_ms": 110, "memory_kb": 9376, "score_of_the_acc": -0.6725, "final_rank": 13 }, { "submission_id": "aoj_2225_1532995", "code_snippet": "#include <cstdio>\n#include <cmath>\n#include <algorithm>\n#include <queue>\n#include <vector>\n#include <iostream>\n#include <cstring>\n\nusing namespace std;\n\n#define X first\n#define Y second\n\nconst int dx[]={0,0,-1,1};\nconst int dy[]={1,-1,0,0};\n\nvector<pair<int,int> > way;\npair<int,int> fr[52][52],s[52*52],p1,p2,be,ed,cur;\nint d[52][52],dir[52][52][5];\nbool vis[52][52];\nint w,h,n,ans,mindis;\n\nint calc(pair<int,int>\tsrc,pair<int,int> tar){\n\tint l=1,r=1,cx,cy,ex,ey;\n\tfor\t(int i=1;i<=w;++i)\tfor\t(int j=1;j<=h;++j){\n\t\tvis[i][j]=0;d[i][j]=w*h+1;\n\t}\n\ts[1]=src;vis[src.X][src.Y]=1;d[src.X][src.Y]=0;\n\twhile\t(l<=r){\n\t\tif\t(vis[tar.X][tar.Y])\tbreak;\n\t\tcx=s[l].X;cy=s[l].Y;\n\t\t//printf(\"%d %d\\n\",cx,cy);\n\t\tfor\t(int k=0;k<4;++k)\n\t\t\tif\t(dir[cx][cy][k]){\n\t\t\t\tex=cx+dx[k];ey=cy+dy[k];\n\t\t\t\tif\t(!vis[ex][ey]){\n\t\t\t\t\tvis[ex][ey]=1;\n\t\t\t\t\tfr[ex][ey]=make_pair(cx,cy);\n\t\t\t\t\td[ex][ey]=d[cx][cy]+1;\n\t\t\t\t\ts[++r]=make_pair(ex,ey);\n\t\t\t\t}\n\t\t\t}\n\t\tl++;\n\t}\n\tif\t(d[tar.X][tar.Y]>w*h)\treturn\t-1;\n\telse\treturn\td[tar.X][tar.Y];\n}\n\nint main(){\n\twhile\t(~scanf(\"%d%d%d\",&w,&h,&n)){\n\t\tfor\t(int i=1;i<=w;++i)\tfor\t(int j=1;j<=h;++j)\n\t\t\tfor\t(int k=0;k<4;++k)\n\t\t\t\tif\t(i+dx[k] && i+dx[k]<=w && j+dy[k] && j+dy[k]<=h)\tdir[i][j][k]=1;\n\t\t\t\telse\tdir[i][j][k]=0;\n\t\tfor\t(int i=1;i<=n;++i){\n\t\t\tscanf(\"%d%d%d%d\",&p1.X,&p1.Y,&p2.X,&p2.Y);\n\t\t\tif\t(p1>p2)\tswap(p1,p2);\n\t\t\tif\t(p1.X==p2.X){\n\t\t\t\tfor\t(int j=p1.Y+1;j<=p2.Y;++j){\n\t\t\t\t\tdir[p1.X][j][3]=dir[p1.X+1][j][2]=0;\n\t\t\t\t\t//printf(\"Cutting %d %d %d %d\\n\",p1.X,j,p1.X+1,j);\n\t\t\t\t}\n\t\t\t}\n\t\t\telse{\n\t\t\t\tfor\t(int j=p1.X+1;j<=p2.X;++j){\n\t\t\t\t\tdir[j][p1.Y][0]=dir[j][p1.Y+1][1]=0;\n\t\t\t\t\t//printf(\"Cutting %d %d %d %d\\n\",j,p1.Y,j,p1.Y+1);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tscanf(\"%d%d%d%d\",&be.X,&be.Y,&ed.X,&ed.Y);\n\t\tbe.X++;be.Y++;ed.X++;ed.Y++;\n\t\tmindis=calc(be,ed);\n\t\t//printf(\"%d\\n\",mindis);\n\t\tcur=ed;way.clear();\n\t\twhile\t(cur!=be){\n\t\t\tway.push_back(cur);\n\t\t\tcur=fr[cur.X][cur.Y];\n\t\t}\n\t\tway.push_back(be);\n\t\treverse(way.begin(),way.end());\n\t\tans=0;\n\t\tfor\t(int i=0;i<way.size()-1;++i){\n\t\t\tp1=way[i];p2=way[i+1];\n\t\t\tif\t(p1>p2)\tswap(p1,p2);\n\t\t\tif\t(p1.X==p2.X){\n\t\t\t\tdir[p1.X][p1.Y][0]=dir[p2.X][p2.Y][1]=0;\n\t\t\t\tans=max(ans,calc(be,ed)-mindis);\n\t\t\t\tdir[p1.X][p1.Y][0]=dir[p2.X][p2.Y][1]=1;\n\t\t\t}\n\t\t\telse{\n\t\t\t\tdir[p1.X][p1.Y][3]=dir[p2.X][p2.Y][2]=0;\n\t\t\t\tans=max(ans,calc(be,ed)-mindis);\n\t\t\t\tdir[p1.X][p1.Y][3]=dir[p2.X][p2.Y][2]=1;\n\t\t\t}\n\t\t}\n\t\tprintf(\"%d\\n\",ans);\n\t}\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 1352, "score_of_the_acc": -0.0206, "final_rank": 2 } ]
aoj_2226_cpp
Problem E: Psychic Accelerator In the west of Tokyo, there is a city named “Academy City.” There are many schools and laboratories to develop psychics in Academy City. You are a psychic student of a school in Academy City. Your psychic ability is to give acceleration to a certain object. You can use your psychic ability anytime and anywhere, but there are constraints. If the object remains stationary, you can give acceleration to the object in any direction. If the object is moving, you can give acceleration to the object only in 1) the direction the object is moving to, 2) the direction opposite to it, or 3) the direction perpendicular to it. Today’s training menu is to move the object along a given course. For simplicity you can regard the course as consisting of line segments and circular arcs in a 2-dimensional space. The course has no branching. All segments and arcs are connected smoothly, i.e. there are no sharp corners. In the beginning, the object is placed at the starting point of the first line segment. You have to move the object to the ending point of the last line segment along the course and stop the object at that point by controlling its acceleration properly. Before the training, a coach ordered you to simulate the minimum time to move the object from the starting point to the ending point. Your task is to write a program which reads the shape of the course and the maximum acceleration a max you can give to the object and calculates the minimum time to move the object from the starting point to the ending point. The object follows basic physical laws. When the object is moving straight in some direction, with acceleration either forward or backward, the following equations hold: v = v 0 + at and s = v 0 t + (1/2) at 2 where v , s , v 0 , a , and t are the velocity, the distance from the starting point, the initial velocity (i.e. the velocity at the starting point), the acceleration, and the time the object has been moving in that direction, respectively. Note that they can be simplified as follows: v 2 − v 0 2 = 2 as When the object is moving along an arc, with acceleration to the centroid, the following equations hold: a = v 2 / r wher v , a , and r are the velocity, the acceleration, and the radius of the arc, respectively. Note that the object cannot change the velocity due to the criteria on your psychic ability. Input The input has the following format: N a max x a ,1 y a ,1 x b ,1 y b ,1 x a ,2 y a ,2 x b ,2 y b ,2 . . . N is the number of line segments; a max is the maximum acceleration you can give to the object; ( x a,i , y a,i ) and ( x b,i , y b,i ) are the starting point and the ending point of the i -th line segment, respectively. The given course may have crosses but you cannot change the direction there. The input meets the following constraints: 0 < N ≤ 40000, 1 ≤ a max ≤ 100, and -100 ≤ x a i , y a i , x b i , y b i ≤ 100. Output Print the minimum time to move the object from the starting point to the ending poi ...(truncated)
[ { "submission_id": "aoj_2226_10851598", "code_snippet": "#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <cmath>\n#include <cctype>\n#include <vector>\n#include <stack>\n#include <queue>\n#include <map>\n#include <algorithm>\n#include <iostream>\n#include <string>\n#include <set>\n#define X first\n#define Y second\n#define sqr(x) (x)*(x)\nusing namespace std;\nconst double PI = acos(-1.0);\nmap<int,int>::iterator it;\ntypedef long long LL ;\n#pragma comment(linker,\"/STACK:102400000,102400000\")\ndouble eps=1e-7;\nstruct point\n{\n double x,y;\n};\nconst int N = 40004;\nstruct Segment\n{\n point st,ed;\n point vec;\n double mxs,mxe;\n double len,A,B,C;\n void pre()\n {\n A = st.y - ed.y;\n B = ed.x - st.x;\n C = (st.x-ed.x)*st.y-(st.y-ed.y)*st.x;\n vec.x=ed.x-st.x;\n vec.y=ed.y-st.y;\n len = sqrt(sqr(vec.x)+sqr(vec.y) );\n }\n point cross(Segment l)\n {\n point ret ;\n double A1=A, B1=B, C1=C;\n double A2=l.A, B2=l.B, C2=l.C;\n //printf(\"A1=%f,B1=%f,C1=%f\\n\",A1,B1,C1);\n //printf(\"A2=%f,B2=%f,C2=%f\\n\",A2,B2,C2);\n ret.x=(B1*C2-B2*C1)/(A1*B2-A2*B1);\n ret.y=(A1*C2-A2*C1)/(A2*B1-A1*B2);\n return ret;\n }\n} S[N];\nint n;\ndouble a;\n\nbool parr(Segment a,Segment b)\n{\n return fabs( (a.st.x-a.ed.x)*(b.st.y-b.ed.y) - (a.st.y-a.ed.y)*(b.st.x-b.ed.x) )<eps;\n}\ndouble dot(Segment a,Segment b)\n{\n return a.vec.x*b.vec.x+a.vec.y*b.vec.y;\n}\ndouble get_m(Segment a)\n{\n return sqrt(sqr(a.vec.x)+sqr(a.vec.y) );\n}\ndouble get_cos(Segment a,Segment b)\n{\n return dot(a,b)/get_m(a)/get_m(b);\n}\ndouble get_dis(point a,point b)\n{\n return sqrt( sqr(a.x-b.x) + sqr(a.y-b.y) );\n}\ndouble get_r(Segment a,Segment b)\n{\n if(fabs(fabs(get_cos(a,b))-1)<eps)\n {\n return get_dis(a.ed,b.st)/2.0;\n }\n point p,q;\n p.x = a.ed.x + b.vec.x;\n p.y = a.ed.y + b.vec.y;\n q.x = b.st.x + a.vec.x;\n q.y = b.st.y + a.vec.y;\n Segment pp,qq ;\n pp.st = p;\n pp.ed = a.ed;\n qq.st = q;\n qq.ed = b.st;\n pp.pre();\n qq.pre();\n //printf(\"p=%f,%f\\n\",p.x,p.y);\n //printf(\"q=%f,%f\\n\",q.x,q.y);\n point ret = pp.cross(qq);\n //printf(\"segment %f,%f -> %f,%f\\n and %f,%f -> %f,%f\\n\",a.st.x,a.st.y,a.ed.x,a.ed.y,b.st.x,b.st.y,b.ed.x,b.ed.y);\n //printf(\"cross at %f,%f\\n\",ret.x,ret.y);\n return get_dis(ret,a.ed);\n}\ndouble res;\n\nvoid gao(double mxs,double mxe,double len,double v0,double &v1,double &t)\n{\n double S1,S2,t1,t2;\n v1=0,t=0;\n //printf(\"%f %f %f %f %f %f\\n\",mxs,mxe,len,v0,v1,t);\n if( sqrt( sqr(v0) + 2.0*a*len ) > mxe )\n {\n v1 = mxe;\n }\n else\n {\n v1 = sqrt( sqr(v0) + 2*a*len );\n }\n //printf(\"v0 = %f v1 = %f a = %f\\n\",v0,v1,a);\n S1 = len/2 + ((sqr(v1)-sqr(v0))/a/4);\n //printf(\"%.6f %.6f %.6f %.6f\\n\",S1,S2,t1,t2);\n S2 = len/2 - ((sqr(v1)-sqr(v0))/a/4);\n //printf(\"%.6f %.6f %.6f %.6f\\n\",S1,S2,t1,t2);\n\n t1 = (-v0+sqrt( fabs( sqr(v0) + 2*a*S1)) )/a;\n //printf(\" sqrt = %f\\n\",sqr(v0)+ 2*a*S1);\n //printf(\"%.6f %.6f %.6f %.6f\\n\",S1,S2,t1,t2);\n double v2 = t1*a+v0;\n t2 = ( v2-sqrt( fabs( sqr(v2) - 2*a*S2)) )/a;\n //printf(\"%.6f %.6f %.6f %.6f\\n\",S1,S2,t1,t2);\n t=t1+t2;\n}\n\npoint turn_right(point a)\n{\n point ret = a;\n swap(ret.x,ret.y);\n ret.y=-ret.y;\n return ret;\n}\n\ndouble mulx(point a,point b)\n{\n return a.x*b.y - a.y*b.x;\n}\n\nbool judge(Segment x,Segment y)\n{\n point v2 = turn_right( x.vec );\n point v3;\n v3.x = y.st.x - x.ed.x;\n v3.y = y.st.y - x.ed.y;\n //printf(\"mx = %f\\n\",mulx(v2,v3));\n if( mulx(v2,v3) >= -eps )\n {\n return true;\n }\n else\n {\n return false;\n }\n}\nvoid gao()\n{\n for(int i=0; i<n-1; ++i)\n {\n double r = get_r(S[i],S[i+1]);\n //printf(\" %f,%f r = %.7f\\n\",S[i].st.x,S[i].st.y,r);\n S[i].mxe = sqrt( r*a );\n //printf(\"mxe = %f\\n\",S[i].mxe);\n }\n S[n-1].mxe=0;\n double v0=0,v,t;\n for(int i=n-1; i>=0; --i)\n {\n v0=S[i].mxe;\n S[i].mxs = sqrt(sqr(v0)+2.0*a*S[i].len);\n if(i>0&&S[i].mxs<S[i-1].mxe)S[i-1].mxe=S[i].mxs;\n }\n for(int i=0; i<n; ++i)\n {\n //printf(\"%.6f %.6f\\n\",S[i].mxs,S[i].mxe);\n }\n res=0;\n double v1;\n v0=0;\n for(int i=0; i<n; ++i)\n {\n gao(S[i].mxs,S[i].mxe,S[i].len,v0,v1,t);\n if(i+1<n)\n {\n double r = get_r(S[i],S[i+1]);\n double cos = get_cos(S[i],S[i+1]);\n //printf(\"r = %f cos=%f\\n\",r,cos);\n if( fabs(cos)<eps )\n {\n //printf(\"cosa=%f\\n\",1.0);\n if(judge(S[i],S[i+1]))\n {\n //printf(\"small\\n\");\n t += PI*r/v1/2.0;\n }\n else\n {\n //printf(\"big\\n\");\n t += 3.0/2.0*PI*r/v1;\n }\n }\n else if( fabs(fabs(cos)-1)<eps )\n {\n //printf(\"sem\\n\");\n t += PI/v1*r;\n }\n else while(1);\n //printf(\"r = %f\\n\",r);\n }\n v0=v1;\n res += t;\n //printf(\"res = %.6f\\n\",res);\n }\n}\nint main()\n{\n //freopen(\"10.in\",\"r\",stdin);\n while(~scanf(\"%d%lf\",&n,&a))\n {\n double sx,sy,ex,ey;\n for(int i=0; i<n; ++i)\n {\n scanf(\"%lf %lf %lf %lf\",&sx,&sy,&ex,&ey);\n S[i].st.x=sx;\n S[i].st.y=sy;\n S[i].ed.x=ex;\n S[i].ed.y=ey;\n S[i].pre();\n //printf(\"%f,%f -> %f,%f\\n\",S[i].st.x,S[i].st.y,S[i].ed.x,S[i].ed.y);\n }\n gao();\n printf(\"%.8f\\n\",res);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 7316, "score_of_the_acc": -0.2921, "final_rank": 4 }, { "submission_id": "aoj_2226_7098152", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nconst double eps = 0.000001;\nconst double PI = acos(-1);\nconst double INF = 1000;\nstruct point{\n double x, y;\n point(){\n }\n point(double x, double y): x(x), y(y){\n }\n point operator +(point P){\n return point(x + P.x, y + P.y);\n }\n point operator -(point P){\n return point(x - P.x, y - P.y);\n }\n point operator *(double k){\n return point(x * k, y * k);\n }\n point operator /(double k){\n return point(x / k, y / k);\n }\n};\npoint rotate90(point P){\n return point(-P.y, P.x);\n}\ndouble abs(point P){\n return sqrt(P.x * P.x + P.y * P.y);\n}\ndouble dist(point P, point Q){\n return abs(Q - P);\n}\ndouble cross(point P, point Q){\n return P.x * Q.y - P.y * Q.x;\n}\ndouble angle(point P){\n return atan2(P.y, P.x);\n}\nstruct line{\n point A, B;\n line(){\n }\n line(point A, point B): A(A), B(B){\n }\n};\npoint vec(line L){\n return L.B - L.A;\n}\nbool is_parallel(line L1, line L2){\n return abs(cross(vec(L1), vec(L2))) < eps;\n}\npoint line_intersection(line L1, line L2){\n return L1.A + vec(L1) * cross(L2.A - L1.A, vec(L2)) / cross(vec(L1), vec(L2));\n}\nint main(){\n cout << fixed << setprecision(20);\n int N;\n double a;\n cin >> N >> a;\n vector<line> L(N);\n for (int i = 0; i < N; i++){\n cin >> L[i].A.x >> L[i].A.y >> L[i].B.x >> L[i].B.y;\n }\n vector<double> l(N);\n for (int i = 0; i < N; i++){\n l[i] = abs(vec(L[i]));\n }\n vector<point> C(N - 1);\n for (int i = 0; i < N - 1; i++){\n if (is_parallel(L[i], L[i + 1])){\n C[i] = (L[i].B + L[i + 1].A) / 2;\n } else {\n line L1(L[i].B, L[i].B + rotate90(vec(L[i])));\n line L2(L[i + 1].A, L[i + 1].A + rotate90(vec(L[i + 1])));\n C[i] = line_intersection(L1, L2);\n }\n }\n vector<double> r(N - 1), t(N - 1);\n for (int i = 0; i < N - 1; i++){\n r[i] = dist(L[i].B, C[i]);\n t[i] = angle(vec(L[i + 1])) - angle(vec(L[i]));\n if (t[i] < 0){\n t[i] += 2 * PI;\n }\n if (cross(vec(L[i]), C[i] - L[i].A) < 0){\n t[i] = 2 * PI - t[i];\n }\n }\n vector<double> v(N + 1);\n v[0] = 0;\n v[N] = 0;\n for (int i = 0; i < N - 1; i++){\n v[i + 1] = sqrt(a * r[i]);\n }\n for (int i = 0; i < N; i++){\n v[i + 1] = min(v[i + 1], sqrt(v[i] * v[i] + 2 * a * l[i]));\n }\n for (int i = N - 1; i >= 0; i--){\n v[i] = min(v[i], sqrt(v[i + 1] * v[i + 1] + 2 * a * l[i]));\n }\n double ans = 0;\n for (int i = 0; i < N; i++){\n double w = sqrt((2 * a * l[i] + v[i] * v[i] + v[i + 1] * v[i + 1]) / 2);\n ans += (w - v[i]) / a;\n ans += (w - v[i + 1]) / a;\n }\n for (int i = 0; i < N - 1; i++){\n ans += r[i] * t[i] / v[i + 1];\n }\n cout << ans << endl;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 6372, "score_of_the_acc": -0.4429, "final_rank": 7 }, { "submission_id": "aoj_2226_6159743", "code_snippet": "#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"unroll-loops\")\n#include<iostream>\n#include<string>\n#include<cstdio>\n#include<vector>\n#include<cmath>\n#include<algorithm>\n#include<functional>\n#include<iomanip>\n#include<queue>\n#include<ciso646>\n#include<random>\n#include<map>\n#include<set>\n#include<bitset>\n#include<stack>\n#include<unordered_map>\n#include<unordered_set>\n#include<utility>\n#include<cassert>\n#include<complex>\n#include<numeric>\n#include<array>\n#include<chrono>\nusing namespace std;\n\n//#define int long long\ntypedef long long ll;\n\ntypedef unsigned long long ul;\ntypedef unsigned int ui;\nconstexpr ll mod = 1000000007;\nconst ll INF = mod * mod;\ntypedef pair<int, int>P;\n\n#define rep(i,n) for(int i=0;i<n;i++)\n#define per(i,n) for(int i=n-1;i>=0;i--)\n#define Rep(i,sta,n) for(int i=sta;i<n;i++)\n#define rep1(i,n) for(int i=1;i<=n;i++)\n#define per1(i,n) for(int i=n;i>=1;i--)\n#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)\n#define all(v) (v).begin(),(v).end()\ntypedef pair<ll, ll> LP;\n\ntemplate<typename T>\nvoid chmin(T& a, T b) {\n\ta = min(a, b);\n}\ntemplate<typename T>\nvoid chmax(T& a, T b) {\n\ta = max(a, b);\n}\ntemplate<typename T>\nvoid cinarray(vector<T>& v) {\n\trep(i, v.size())cin >> v[i];\n}\ntemplate<typename T>\nvoid coutarray(vector<T>& v) {\n\trep(i, v.size()) {\n\t\tif (i > 0)cout << \" \"; cout << v[i];\n\t}\n\tcout << \"\\n\";\n}\nll mod_pow(ll x, ll n, ll m = mod) {\n\tif (n < 0) {\n\t\tll res = mod_pow(x, -n, m);\n\t\treturn mod_pow(res, m - 2, m);\n\t}\n\tif (abs(x) >= m)x %= m;\n\tif (x < 0)x += m;\n\tif (x == 0)return 0;\n\tll res = 1;\n\twhile (n) {\n\t\tif (n & 1)res = res * x % m;\n\t\tx = x * x % m; n >>= 1;\n\t}\n\treturn res;\n}\nstruct modint {\n\tint n;\n\tmodint() :n(0) { ; }\n\tmodint(ll m) {\n\t\tif (m < 0 || mod <= m) {\n\t\t\tm %= mod; if (m < 0)m += mod;\n\t\t}\n\t\tn = m;\n\t}\n\toperator int() { return n; }\n};\nbool operator==(modint a, modint b) { return a.n == b.n; }\nmodint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }\nmodint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }\nmodint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }\nmodint operator+(modint a, modint b) { return a += b; }\nmodint operator-(modint a, modint b) { return a -= b; }\nmodint operator*(modint a, modint b) { return a *= b; }\nmodint operator^(modint a, ll n) {\n\tif (n == 0)return modint(1);\n\tmodint res = (a * a) ^ (n / 2);\n\tif (n % 2)res = res * a;\n\treturn res;\n}\n\nll inv(ll a, ll p) {\n\treturn (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);\n}\nmodint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }\nmodint operator/=(modint& a, modint b) { a = a / b; return a; }\nconst int max_n = 1 << 10;\nmodint fact[max_n], factinv[max_n];\nvoid init_f() {\n\tfact[0] = modint(1);\n\tfor (int i = 0; i < max_n - 1; i++) {\n\t\tfact[i + 1] = fact[i] * modint(i + 1);\n\t}\n\tfactinv[max_n - 1] = modint(1) / fact[max_n - 1];\n\tfor (int i = max_n - 2; i >= 0; i--) {\n\t\tfactinv[i] = factinv[i + 1] * modint(i + 1);\n\t}\n}\nmodint comb(int a, int b) {\n\tif (a < 0 || b < 0 || a < b)return 0;\n\treturn fact[a] * factinv[b] * factinv[a - b];\n}\nmodint combP(int a, int b) {\n\tif (a < 0 || b < 0 || a < b)return 0;\n\treturn fact[a] * factinv[a - b];\n}\n\nll gcd(ll a, ll b) {\n\ta = abs(a); b = abs(b);\n\tif (a < b)swap(a, b);\n\twhile (b) {\n\t\tll r = a % b; a = b; b = r;\n\t}\n\treturn a;\n}\n\ntypedef long double ld;\ntypedef pair<ld, ld> LDP;\nconst ld eps = 1e-8;\nconst ld pi = acosl(-1.0);\nint dx[4] = { 1,0,-1,0 };\nint dy[4] = { 0,1,0,-1 };\n\ntypedef complex<ld> Point;\nld dot(Point a, Point b) { return real(conj(a) * b); }\nld cross(Point a, Point b) { return imag(conj(a) * b); }\nnamespace std {\n\tbool operator<(const Point& lhs, const Point& rhs) {\n\t\treturn lhs.real() == rhs.real() ? lhs.imag() < rhs.imag() : lhs.real() < rhs.real();\n\t}\n}\nstruct Line {\n\tPoint a, b;\n};\nstruct Circle {\n\tPoint p; ld r;\n};\nint ccw(Point a, Point b, Point c) {\n\tb -= a; c -= a;\n\tif (cross(b, c) > eps)return 1;//counter clockwise\n\tif (cross(b, c) < -eps)return -1;//clock wise\n\tif (dot(b, c) < 0)return 2;//c--a--b on line\n\tif (norm(b) < norm(c))return -2;//a--b--c on line\n\treturn 0; //a--c--b on line\n}\nbool eq(ld a, ld b) {\n\treturn abs(a - b) < eps;\n}\n//2直線の交差判定\nbool isis_ll(Line l, Line m) {\n\treturn !eq(cross(l.b - l.a, m.b - m.a), 0);\n}\n//直線と線分の交差判定\nbool isis_ls(Line l, Line s) {\n\treturn (cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < eps);\n}\n//点が直線上に存在するか\nbool isis_lp(Line l, Point p) {\n\treturn (abs(cross(l.b - p, l.a - p)) < eps);\n}\n//点が線分上に存在するか\nbool isis_sp(Line s, Point p) {\n\t//誤差がisis_lpに比べて大きいので、できるだけisis_lpを使う\n\treturn (abs(s.a - p) + abs(s.b - p) - abs(s.b - s.a) < eps);\n}\n//線分と線分の交差判定\n//bool isis_ss(Line s, Line t) {\n//\treturn(cross(s.b - s.a, t.a - s.a)*cross(s.b - s.a, t.b - s.a) < -eps && cross(t.b - t.a, s.a - t.a)*cross(t.b - t.a, s.b - t.a) < -eps);\n//}\n//線分と線分の交差判定2\nbool isis_ss(Line s, Line t) {\n\treturn ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 && ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0;\n}\n//点から直線への垂線の足\nPoint proj(Line l, Point p) {\n\tld t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n\treturn l.a + t * (l.a - l.b);\n}\n//直線と直線の交点\n//平行な2直線に対しては使うな!!!!\nPoint is_ll(Line s, Line t) {\n\tPoint sv = s.b - s.a; Point tv = t.b - t.a;\n\treturn s.a + sv * cross(tv, t.a - s.a) / cross(tv, sv);\n}\n//直線と点の距離\nld dist_lp(Line l, Point p) {\n\treturn abs(p - proj(l, p));\n}\n//直線と直線の距離\nld dist_ll(Line l, Line m) {\n\treturn isis_ll(l, m) ? 0 : dist_lp(l, m.a);\n}\n//線分と直線の距離\nld dist_ls(Line l, Line s) {\n\treturn isis_ls(l, s) ? 0 : min(dist_lp(l, s.a), dist_lp(l, s.b));\n}\n//線分と点の距離\nld dist_sp(Line s, Point p) {\n\tPoint r = proj(s, p);\n\treturn isis_sp(s, r) ? abs(p - r) : min(abs(p - s.a), abs(p - s.b));\n}\n//線分と線分の距離\nld dist_ss(Line s, Line t) {\n\tif (isis_ss(s, t))return 0;\n\treturn min({ dist_sp(s,t.a),dist_sp(s,t.b),dist_sp(t,s.a),dist_sp(t,s.b) });\n}\n\nvoid solve() {\n\tint n, ma; cin >> n >> ma;\n\tauto calc = [&](ld v, ld len) {\n\t\treturn sqrt(v * v + 2 * ma * len);\n\t};\n\tvector<Line> vl(n);\n\trep(i, n) {\n\t\tint x1, y1, x2, y2;\n\t\tcin >> x1 >> y1 >> x2 >> y2;\n\t\tvl[i] = { {(ld)x1,(ld)y1},{(ld)x2,(ld)y2} };\n\t}\n\tvector<ld> lens(n);\n\trep(i, n)lens[i] = abs(vl[i].b - vl[i].a);\n\tvector<ld> rad(n - 1);\n\tvector<ld> rlen(n - 1);\n\trep(i, n - 1) {\n\t\tif (!isis_ll(vl[i], vl[i + 1])) {\n\t\t\trad[i] = abs(vl[i].b - vl[i + 1].a) / 2;\n\t\t\trlen[i] = rad[i] * pi;\n\t\t\tcontinue;\n\t\t}\n\t\tPoint p = is_ll(vl[i], vl[i + 1]);\n\t\tld d = abs(p - vl[i].b);\n\t\tPoint d1 = vl[i].a - vl[i].b;\n\t\tPoint d2 = vl[i+1].b - vl[i+1].a;\n\t\tld t = acos(dot(d1, d2) / abs(d1) / abs(d2));\n\t\trad[i] = d * tan(t / 2);\n\t\tif (dot(vl[i].b - vl[i].a, p - vl[i].b) > 0) {\n\t\t\trlen[i] = rad[i] * (pi - t);\n\t\t}\n\t\telse {\n\t\t\trlen[i] = rad[i] * (pi + t);\n\t\t}\n\t}\n\tvector<ld> dist(n + 1, INF);\n\tusing speP = pair<ld, int>;\n\tpriority_queue<speP, vector<speP>, greater<speP>> q;\n\tdist[0] = 0; q.push({ 0,0 });\n\tdist[n] = 0; q.push({ 0,n });\n\trep(i, n - 1) {\n\t\tdist[i + 1] = sqrt(rad[i] * ma);\n\t\tq.push({ rad[i] * ma,i + 1 });\n\t}\n\twhile (!q.empty()) {\n\t\tspeP p = q.top(); q.pop();\n\t\tint id = p.second;\n\t\tif (p.first > dist[id])continue;\n\t\tif (id < n) {\n\t\t\tld nd = calc(p.first, lens[id]);\n\t\t\tif (nd < dist[id + 1]) {\n\t\t\t\tdist[id + 1] = nd;\n\t\t\t\tq.push({ nd,id + 1 });\n\t\t\t}\n\t\t}\n\t\tif (id > 0) {\n\t\t\tld nd = calc(p.first, lens[id - 1]);\n\t\t\tif (nd < dist[id - 1]) {\n\t\t\t\tdist[id - 1] = nd;\n\t\t\t\tq.push({ nd,id - 1 });\n\t\t\t}\n\t\t}\n\t}\n\t//coutarray(rlen);\n\tld ans = 0;\n\trep(i, n - 1) {\n\t\tans += rlen[i] / dist[i + 1];\n\t}\n\tauto calcsegs = [&](ld v, ld len) {\n\t\treturn 2 * (-v + sqrt(v * v + ma * len)) / ma;\n\t};\n\tauto calcseg = [&](ld sv, ld gv, ld len) {\n\t\tif (sv > gv)swap(sv, gv);\n\t\tld res = 0;\n\t\tld dif = gv - sv;\n\t\tld ct = dif / ma;\n\t\tres += ct;\n\t\tlen -= (sv + gv) * ct / 2;\n\t\tres += calcsegs(gv, len);\n\t\treturn res;\n\t};\n\trep(i, n) {\n\t\tans += calcseg(dist[i], dist[i + 1], lens[i]);\n\t}\n\tcout << ans << \"\\n\";\n}\n\n\nsigned main() {\n\tios::sync_with_stdio(false);\n\tcin.tie(0);\n\tcout << fixed << setprecision(8);\n\t//init_f();\n\t//init();\n\t//while(true)\n\t//expr();\n\t//int t; cin >> t; rep(i, t)\n\tsolve();\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 10236, "score_of_the_acc": -0.513, "final_rank": 11 }, { "submission_id": "aoj_2226_5923616", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nconst double eps = 0.000001;\nconst double PI = acos(-1);\nconst double INF = 1000;\nstruct point{\n double x, y;\n point(){\n }\n point(double x, double y): x(x), y(y){\n }\n point operator +(point P){\n return point(x + P.x, y + P.y);\n }\n point operator -(point P){\n return point(x - P.x, y - P.y);\n }\n point operator *(double k){\n return point(x * k, y * k);\n }\n point operator /(double k){\n return point(x / k, y / k);\n }\n};\npoint rotate90(point P){\n return point(-P.y, P.x);\n}\ndouble abs(point P){\n return sqrt(P.x * P.x + P.y * P.y);\n}\ndouble dist(point P, point Q){\n return abs(Q - P);\n}\ndouble cross(point P, point Q){\n return P.x * Q.y - P.y * Q.x;\n}\ndouble angle(point P){\n return atan2(P.y, P.x);\n}\nstruct line{\n point A, B;\n line(){\n }\n line(point A, point B): A(A), B(B){\n }\n};\npoint vec(line L){\n return L.B - L.A;\n}\nbool is_parallel(line L1, line L2){\n return abs(cross(vec(L1), vec(L2))) < eps;\n}\npoint line_intersection(line L1, line L2){\n return L1.A + vec(L1) * cross(L2.A - L1.A, vec(L2)) / cross(vec(L1), vec(L2));\n}\nint main(){\n cout << fixed << setprecision(20);\n int N;\n double a;\n cin >> N >> a;\n vector<line> L(N);\n for (int i = 0; i < N; i++){\n cin >> L[i].A.x >> L[i].A.y >> L[i].B.x >> L[i].B.y;\n }\n vector<double> l(N);\n for (int i = 0; i < N; i++){\n l[i] = abs(vec(L[i]));\n }\n vector<point> C(N - 1);\n for (int i = 0; i < N - 1; i++){\n if (is_parallel(L[i], L[i + 1])){\n C[i] = (L[i].B + L[i + 1].A) / 2;\n } else {\n line L1(L[i].B, L[i].B + rotate90(vec(L[i])));\n line L2(L[i + 1].A, L[i + 1].A + rotate90(vec(L[i + 1])));\n C[i] = line_intersection(L1, L2);\n }\n }\n vector<double> r(N - 1), t(N - 1);\n for (int i = 0; i < N - 1; i++){\n r[i] = dist(L[i].B, C[i]);\n t[i] = angle(vec(L[i + 1])) - angle(vec(L[i]));\n if (t[i] < 0){\n t[i] += 2 * PI;\n }\n if (cross(vec(L[i]), C[i] - L[i].A) < 0){\n t[i] = 2 * PI - t[i];\n }\n }\n vector<double> v(N + 1);\n v[0] = 0;\n v[N] = 0;\n for (int i = 0; i < N - 1; i++){\n v[i + 1] = sqrt(a * r[i]);\n }\n for (int i = 0; i < N; i++){\n v[i + 1] = min(v[i + 1], sqrt(v[i] * v[i] + 2 * a * l[i]));\n }\n for (int i = N - 1; i >= 0; i--){\n v[i] = min(v[i], sqrt(v[i + 1] * v[i + 1] + 2 * a * l[i]));\n }\n double ans = 0;\n for (int i = 0; i < N; i++){\n \tdouble w = sqrt((2 * a * l[i] + v[i] * v[i] + v[i + 1] * v[i + 1]) / 2);\n \tans += (w - v[i]) / a;\n \tans += (w - v[i + 1]) / a;\n }\n for (int i = 0; i < N - 1; i++){\n ans += r[i] * t[i] / v[i + 1];\n }\n cout << ans << endl;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 6432, "score_of_the_acc": -0.4474, "final_rank": 8 }, { "submission_id": "aoj_2226_4802926", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 1000000000000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n\n#define SIZE 40005\n\n\nstruct Point{\n\tPoint(double arg_x,double arg_y){\n\t\tx = arg_x;\n\t\ty = arg_y;\n\t}\n\n\tPoint(){\n\t\tx = y = 0.0;\n\t}\n\n\tPoint operator + (Point p){ return Point(x+p.x,y+p.y); }\n\tPoint operator - (Point p){ return Point(x-p.x,y-p.y);}\n\tPoint operator * (double a){ return Point(a*x,a*y); }\n\tPoint operator / (double a){ return Point(x/a,y/a); }\n\n\tdouble abs(){ return sqrt(norm()); }\n\tdouble norm(){ return x*x + y*y; }\n\n\tbool operator<(const Point &p) const{\n\t\treturn x != p.x? x < p.x: y < p.y;\n\t}\n\n\tbool operator == (const Point &p) const{\n\t\treturn fabs(x-p.x) < EPS && fabs(y-p.y) < EPS;\n\t}\n\n \t/*\n\tvoid debug(){\n\n\t\tprintf(\"(%.3lf,%.3lf)\\n\",x,y);\n\t}*/\n\n\tdouble x,y;\n};\n\n\ntypedef Point Vector;\ntypedef vector<Point> Polygon;\n\nstruct Line{\n\tLine(){\n\t\tlen = 0;\n\t}\n\tLine(Point a,Point b){\n\t\tp[0] = a;\n\t\tp[1] = b;\n\t\tlen = 0;\n\t}\n\t/*void outPut(){\n\n\t\tprintf(\"(%.3lf,%.3lf)-(%.3lf,%.3lf)\\n\",p[0].x,p[0].y,p[1].x,p[1].y);\n\t}*/\n\tPoint p[2];\n\tdouble len;\n};\n\nstruct Corner{\n\n\tdouble r,len;\n};\n\nint N;\ndouble A;\ndouble NUM = 100000;\nLine line[SIZE];\nCorner corner[SIZE];\ndouble L_limit[SIZE],C_limit[SIZE];\ndouble L_mid[SIZE];\n\n\ndouble calc_dist(Point A,Point B){\n\n\treturn sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y));\n}\n\n\ndouble norm(Vector a){\n\treturn a.x*a.x+a.y*a.y;\n}\n\ndouble abs(Vector a){\n\treturn sqrt(norm(a));\n}\n\ndouble cross(Vector a,Vector b){\n return a.x*b.y-a.y*b.x;\n}\n\ndouble dot(Vector a,Vector b){\n return a.x*b.x + a.y*b.y;\n}\n\nint func(double x1,double y1,double x2, double y2, double xp, double yp){\n\tdouble naiseki,norm1,norm2,gaiseki;\n\tnorm1 = sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));\n\tnorm2 = sqrt((xp-x1)*(xp-x1)+(yp-y1)*(yp-y1));\n\tnaiseki = (xp-x1)*(x2-x1)+(yp-y1)*(y2-y1);\n\tgaiseki = (x2-x1)*(yp-y1)-(xp-x1)*(y2-y1);\n\tif(gaiseki > EPS){\n\t\treturn 1;\n\t}else if(gaiseki < -EPS){\n\t\treturn -1;\n\t}\n\tif(naiseki < -EPS){\n\t\treturn 2;\n\t}\n\n\tif(norm1 < norm2){\n\t\treturn -2;\n\t}\n\treturn 0;\n}\n\n\n//★★直線ではなく、線分の交差判定★★\nbool is_Cross(Line a,Line b){\n\n\tif(func(a.p[0].x,a.p[0].y,a.p[1].x,a.p[1].y,b.p[0].x,b.p[0].y)*\n\t\t\tfunc(a.p[0].x,a.p[0].y,a.p[1].x,a.p[1].y,b.p[1].x,b.p[1].y) <= 0 &&\n\t\t\tfunc(b.p[0].x,b.p[0].y,b.p[1].x,b.p[1].y,a.p[0].x,a.p[0].y)*\n\t\t\tfunc(b.p[0].x,b.p[0].y,b.p[1].x,b.p[1].y,a.p[1].x,a.p[1].y) <= 0){\n\t\treturn true;\n\t}\n\treturn false;\n}\n\ndouble calc_slope(Line A){\n\n\tif(fabs(A.p[0].x-A.p[1].x) < EPS){\n\n\t\treturn DBL_MAX;\n\n\t}else if(fabs(A.p[0].y-A.p[1].y) < EPS){\n\n\t\treturn 0;\n\n\t}else{\n\n\t\treturn (A.p[1].y-A.p[0].y)/(A.p[1].x-A.p[0].x);\n\t}\n}\n\nPoint calc_minus(Point a,Point b){\n Point ret;\n\n ret.x = a.x-b.x;\n ret.y = a.y-b.y;\n\n return ret;\n}\n\ndouble calc_len(Vector a){\n return sqrt(a.x*a.x+a.y*a.y);\n}\n\n//線分ではなく直線と点の距離\ndouble getDistanceLP(Line l,Point p){\n return fabs(cross(calc_minus(l.p[1],l.p[0]),calc_minus(p,l.p[0]))/calc_len(calc_minus(l.p[1],l.p[0])));\n}\n\n//点と線分の距離\ndouble getDistanceSP(Line l,Point p){\n\tif(dot(calc_minus(l.p[1],l.p[0]),calc_minus(p,l.p[0])) < 0.0)return calc_len(calc_minus(p,l.p[0]));\n\tif(dot(calc_minus(l.p[0],l.p[1]),calc_minus(p,l.p[1])) < 0.0)return calc_len(calc_minus(p,l.p[1]));\n\treturn getDistanceLP(l,p);\n}\n\n//線分と線分の距離\ndouble getDistance(Line A,Line B){\n\tif(is_Cross(A,B))return 0.0;\n\treturn min(min(getDistanceSP(A,B.p[0]),getDistanceSP(A,B.p[1])),\n\t\t\tmin(getDistanceSP(B,A.p[0]),getDistanceSP(B,A.p[1])));\n}\n\n\n//交点を求める関数\nPoint calc_Cross_Point(double x1,double x2,double x3,double x4,double y1,double y2,double y3,double y4){\n\tPoint ret;\n\tret.x = ((x2-x1)*(y3*(x4-x3)+x3*(y3-y4))-(x4-x3)*(y1*(x2-x1)+x1*(y1-y2)))/((y2-y1)*(x4-x3)-(y4-y3)*(x2-x1));\n\tif(fabs(x1-x2) > EPS){\n\t\tret.y = ((y2-y1)*ret.x+y1*(x2-x1)+x1*(y1-y2))/(x2-x1);\n\t}else{\n\t\tret.y = ((y4-y3)*ret.x+y3*(x4-x3)+x3*(y3-y4))/(x4-x3);\n\t}\n\treturn ret;\n}\n\n//インタフェース関数\nPoint calc_Cross_Point(Point a,Point b,Point c,Point d){\n\treturn calc_Cross_Point(a.x,b.x,c.x,d.x,a.y,b.y,c.y,d.y);\n}\n\nPoint calc_Cross_Point(Line A,Line B){\n\n\tif(getDistanceSP(B,A.p[0]) < EPS){\n\n\t\treturn A.p[0];\n\n\t}else if(getDistanceSP(B,A.p[1]) < EPS){\n\n\t\treturn A.p[1];\n\n\t}else if(getDistanceSP(A,B.p[0]) < EPS){\n\n\t\treturn B.p[0];\n\n\t}else if(getDistanceSP(A,B.p[1]) < EPS){\n\n\t\treturn B.p[1];\n\t}\n\n\treturn calc_Cross_Point(A.p[0],A.p[1],B.p[0],B.p[1]);\n}\n\n\nint main(){\n\n\tscanf(\"%d %lf\",&N,&A);\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tscanf(\"%lf %lf %lf %lf\",&line[i].p[0].x,&line[i].p[0].y,&line[i].p[1].x,&line[i].p[1].y);\n\t\tline[i].len = calc_dist(line[i].p[0],line[i].p[1]);\n\t}\n\n\tPoint center;\n\tdouble r,rad;\n\n\tLine line1,line2;\n\tVector vec1,vec2;\n\n\t//円弧の計算\n\tfor(int i = 0; i < N-1; i++){\n\n\t\tdouble slope1 = calc_slope(line[i]);\n\t\tdouble slope2 = calc_slope(line[i+1]);\n\n\t\tif(fabs(slope1-slope2) < EPS){ //平行\n\n\t\t\tcenter = (line[i].p[1]+line[i+1].p[0])/2;\n\t\t\tr = calc_dist(center,line[i].p[1]);\n\t\t\trad = M_PI;\n\n\t\t}else{\n\n\t\t\tif(fabs(slope1-DBL_MAX) < EPS){ //垂直\n\n\t\t\t\tline1 = Line(Point(-NUM,line[i].p[1].y),Point(NUM,line[i].p[1].y));\n\n\t\t\t}else if(fabs(slope1) < EPS){ //水平\n\n\t\t\t\tline1 = Line(Point(line[i].p[1].x,-NUM),Point(line[i].p[1].x,NUM));\n\n\t\t\t}else{\n\n\t\t\t\tdouble tmp_slope = (-1.0)/slope1;\n\t\t\t\tline1 = Line(Point(line[i].p[1].x-NUM,line[i].p[1].y-NUM*tmp_slope),Point(line[i].p[1].x+NUM,line[i].p[1].y+NUM*tmp_slope));\n\t\t\t}\n\n\t\t\tif(fabs(slope2-DBL_MAX) < EPS){ //垂直\n\n\t\t\t\tline2 = Line(Point(-NUM,line[i+1].p[0].y),Point(NUM,line[i+1].p[0].y));\n\n\t\t\t}else if(fabs(slope2) < EPS){ //水平\n\n\t\t\t\tline2 = Line(Point(line[i+1].p[0].x,-NUM),Point(line[i+1].p[0].x,NUM));\n\n\t\t\t}else{\n\n\t\t\t\tdouble tmp_slope = (-1.0)/slope2;\n\t\t\t\tline2 = Line(Point(line[i+1].p[0].x-NUM,line[i+1].p[0].y-NUM*tmp_slope),Point(line[i+1].p[0].x+NUM,line[i+1].p[0].y+NUM*tmp_slope));\n\t\t\t}\n\n\t\t\tcenter = calc_Cross_Point(line1,line2);\n\t\t\tr = calc_dist(center,line[i].p[1]);\n\n\t\t\tvec1 = line[i].p[1]-center;\n\t\t\tvec2 = line[i+1].p[0]-center;\n\n\t\t\trad = acos(dot(vec1,vec2)/(abs(vec1)*abs(vec2)));\n\n\t\t\tline1.p[1] = line[i].p[1];\n\n\t\t\t//line[i]とline[i+1]を非円弧方向に伸ばし、交差するかどうかみる\n\t\t\tif(fabs(slope1-DBL_MAX) < EPS){ //垂直\n\n\t\t\t\tif(line[i].p[1].y > line[i].p[0].y){\n\n\t\t\t\t\tline1.p[0] = Point(line[i].p[1].x,-NUM);\n\n\t\t\t\t}else{\n\n\t\t\t\t\tline1.p[0] = Point(line[i].p[1].x,NUM);\n\t\t\t\t}\n\n\t\t\t}else if(fabs(slope1) < EPS){ //水平\n\n\t\t\t\tif(line[i].p[1].x > line[i].p[0].x){\n\n\t\t\t\t\tline1.p[0] = Point(-NUM,line[i].p[1].y);\n\t\t\t\t}else{\n\n\t\t\t\t\tline1.p[0] = Point(NUM,line[i].p[1].y);\n\t\t\t\t}\n\n\t\t\t}else{\n\n\t\t\t\tif(line[i].p[1].x > line[i].p[0].x){\n\n\t\t\t\t\tline1.p[0] = Point(line[i].p[1].x-NUM,line[i].p[1].y-NUM*slope1);\n\t\t\t\t}else{\n\n\t\t\t\t\tline1.p[0] = Point(line[i].p[1].x+NUM,line[i].p[1].y+NUM*slope1);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tline2.p[0] = line[i+1].p[0];\n\n\t\t\tif(fabs(slope2-DBL_MAX) < EPS){ //垂直\n\n\t\t\t\tif(line[i+1].p[0].y > line[i+1].p[1].y){\n\n\t\t\t\t\tline2.p[1] = Point(line[i+1].p[0].x,-NUM);\n\n\t\t\t\t}else{\n\n\t\t\t\t\tline2.p[1] = Point(line[i+1].p[0].x,NUM);\n\t\t\t\t}\n\n\t\t\t}else if(fabs(slope2) < EPS){ //水平\n\n\t\t\t\tif(line[i+1].p[0].x > line[i+1].p[1].x){\n\n\t\t\t\t\tline2.p[1] = Point(-NUM,line[i+1].p[0].y);\n\t\t\t\t}else{\n\n\t\t\t\t\tline2.p[1] = Point(NUM,line[i+1].p[0].y);\n\t\t\t\t}\n\n\t\t\t}else{\n\n\t\t\t\tif(line[i+1].p[0].x > line[i+1].p[1].x){\n\n\t\t\t\t\tline2.p[1] = Point(line[i+1].p[0].x-NUM,line[i+1].p[0].y-NUM*slope2);\n\t\t\t\t}else{\n\n\t\t\t\t\tline2.p[1] = Point(line[i+1].p[0].x+NUM,line[i+1].p[0].y+NUM*slope2);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif(is_Cross(line1,line2)){\n\n\t\t\t\trad = 2*M_PI-rad;\n\t\t\t}\n\t\t}\n\n\t\tcorner[i].r = r;\n\t\tcorner[i].len = r*rad;\n\t}\n\n\tdouble tmp_dist,max_corner_v,max_v_sub;\n\n\t//ダミーコーナー(line[i-1]の先に長さ0のコーナーが繋がっているとする)\n\tC_limit[N-1] = 0;\n\n\t//終点から始点に向かってコーナーの制限速度を求める\n\t//各コーナーの速度の上限値(最大値を求めるので、次のコーナーとの間に位置する直線の加速度は-Aとする)\n\tfor(int i = N-2; i >= 0; i--){\n\n\t\t//次のコーナーまでの距離\n\t\ttmp_dist = line[i+1].len;\n\t\t//円弧の速度の最大値\n\t\tmax_corner_v = sqrt(A*corner[i].r);\n\n\t\t//その後の直線で原則する場合の制限速度\n\t\tmax_v_sub = sqrt(C_limit[i+1]*C_limit[i+1]+2*A*tmp_dist);\n\n\t\tC_limit[i] = min(max_corner_v,max_v_sub);\n\t}\n\n\t//始点から終点に向かってコーナーの制限速度を求める(加速度A)\n\tdouble max_v_add;\n\n\tfor(int i = 1; i <= N-1; i++){\n\n\t\t//前のコーナーからの距離\n\t\ttmp_dist = line[i].len;\n\t\tmax_v_add = sqrt(C_limit[i-1]*C_limit[i-1]+2*A*tmp_dist);\n\n\t\tC_limit[i] = min(C_limit[i],max_v_add);\n\t}\n\n\tdouble ans = 0,max_v;\n\n\t//全力で加速してもコーナーの制限速度に達しないなら、全力で加速する\n\tmax_v = sqrt(2*A*line[0].len);\n\t//直線のアクセス切り替えpointおよび必要時間を求める\n\tL_mid[0] = (min(max_v,C_limit[0])*min(max_v,C_limit[0])+2*A*line[0].len)/(4*A);\n\tdouble mid_v = sqrt(2*A*L_mid[0]);\n\n\tans += mid_v/A;\n\tans += (mid_v-min(max_v,C_limit[0]))/A;\n\n\tdouble v_pre,v_next;\n\n\tfor(int i = 1; i <= N-1; i++){\n\n\t\tv_pre = C_limit[i-1];\n\t\tmax_v = sqrt(2*A*line[i].len+v_pre*v_pre);\n\n\t\tv_next = min(max_v,C_limit[i]);\n\n\t\tL_mid[i] = (v_next*v_next-v_pre*v_pre+2*A*line[i].len)/(4*A);\n\t\tmid_v = sqrt(v_pre*v_pre+2*A*L_mid[i]);\n\n\t\tans += (mid_v-v_pre)/A;\n\t\tans += (mid_v-v_next)/A;\n\t}\n\n\t//円弧の時間\n\tfor(int i = 0; i <= N-2; i++){\n\n\t\tans += corner[i].len/C_limit[i];\n\t}\n\n\tprintf(\"%.10lf\\n\",ans);\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 6020, "score_of_the_acc": -0.194, "final_rank": 2 }, { "submission_id": "aoj_2226_3106310", "code_snippet": "#include <iostream>\n#include <iomanip>\n#include <complex>\n#include <vector>\n#include <algorithm>\n#include <cmath>\n#include <set>\n#include <cassert>\nusing namespace std;\nconst double EPS = 1e-10;\nconst double INF = 1e12;\nconst double PI = acos(-1);\n#define EQ(n,m) (abs((n)-(m)) < EPS)\n#define X real()\n#define Y imag()\n\ntypedef complex<double> P;\ntypedef vector<P> VP;\nstruct L : array<P, 2>{\n L(const P& a, const P& b){ at(0)=a; at(1)=b; }\n L(){}\n};\nnamespace std{\n bool operator < (const P& a, const P& b){\n return !EQ(a.X,b.X) ? a.X<b.X : a.Y+EPS<b.Y;\n }\n bool operator == (const P& a, const P& b){\n return abs(a-b) < EPS;\n }\n}\n\ndouble dot(P a, P b){\n return (conj(a)*b).X;\n}\ndouble cross(P a, P b){\n return (conj(a)*b).Y;\n}\nint ccw(P a, P b, P c){\n b -= a;\n c -= a;\n if(cross(b,c) > EPS) return +1; //ccw\n if(cross(b,c) < -EPS) return -1; //cw\n if(dot(b,c) < -EPS) return +2; //c-a-b\n if(abs(c)-abs(b) > EPS) return -2; //a-b-c\n return 0; //a-c-b\n}\n\nbool intersectSS(const L& a, const L& b){\n return ( ccw(a[0],a[1],b[0]) *ccw(a[0],a[1],b[1]) <= 0 ) &&\n ( ccw(b[0],b[1],a[0]) *ccw(b[0],b[1],a[1]) <= 0 );\n}\n\nbool isParallel(const P &a, const P &b){\n return abs(cross(a,b)) < EPS;\n}\nbool isParallel(const L &a, const L &b){\n return isParallel(a[1]-a[0], b[1]-b[0]);\n}\nP crosspointLL(const L &l, const L &m) {\n double A = cross(l[1]-l[0], m[1]-m[0]);\n double B = cross(l[1]-l[0], l[1]-m[0]);\n return m[0] + B/A *(m[1]-m[0]);\n}\n\ndouble getangle(P a, P b){\n return abs(arg(a/b));\n}\n\nint main(){\n int n;\n double amax;\n cin >> n >> amax;\n\n vector<L> l(n);\n vector<double> len(n);\n for(int i=0; i<n; i++){\n double xs,ys,xt,yt;\n cin >> xs >> ys >> xt >> yt;\n l[i] = L(P(xs, ys), P(xt, yt));\n len[i] = abs(l[i][1] -l[i][0]);\n }\n //i番目の線分の終点を通過するときの速度上限\n vector<double> vlimit(n);\n vlimit[n-1] = 0;\n vector<double> arclen(n-1);\n for(int i=n-2; i>=0; i--){\n double r;\n if(isParallel(l[i], l[i+1])){\n r = abs(l[i+1][0] -l[i][1]) /2.0;\n arclen[i] = PI *r;\n }else{\n //接合部分に向かう方向\n P dir[2] = {l[i][1] -l[i][0], l[i+1][0] -l[i+1][1]}; \n P ortdir[2] = {dir[0]*P(0, 1), dir[1]*P(0, 1)};\n L ortline[2] = {L(l[i][1], l[i][1] +ortdir[0]), L(l[i+1][0], l[i+1][0] +ortdir[1])};\n //円弧の中心\n P cp = crosspointLL(ortline[0], ortline[1]);\n r = abs(l[i][1] -cp);\n L ray[2] = {L(l[i][1], l[i][1] +1e4*dir[0]), L(l[i+1][0], l[i+1][0] +1e4*dir[1])};\n if(intersectSS(ray[0], ray[1])){\n arclen[i] = r *getangle(dir[0], dir[1]);\n }else{\n arclen[i] = r *(2*PI -getangle(dir[0], dir[1]));\n }\n }\n //カーブを曲がり切れる速度上限\n //a = v^2 /r より\n double v = sqrt(r *amax);\n //次以降のカーブを曲がり切れる速度上限\n //v^2 -(v_0)^2 = 2ax より\n double vrev = sqrt(2 *amax *len[i+1] +vlimit[i+1]*vlimit[i+1]);\n vlimit[i] = min(v, vrev);\n }\n\n double ans = 0;\n double v = 0;\n for(int i=0; i<n; i++){\n double clen = (vlimit[i]*vlimit[i] -v*v +2*amax*len[i]) /(4*amax);\n if(clen < 0 || len[i] < clen){\n //加減速のみ\n double acc = (vlimit[i] > v)? amax: -amax;\n double vend = sqrt(2 *acc *len[i] +v*v);\n ans += (vend -v) /acc;\n v = vend;\n }else{\n //加速のち減速\n double vmid = sqrt(2 *amax *clen +v*v);\n ans += (vmid -v) /amax + (vlimit[i] -vmid) /(-amax);\n v = vlimit[i];\n }\n \n //円運動中の時間を加算\n if(i < n-1){\n ans += arclen[i] /v;\n }\n }\n cout << fixed << setprecision(10);\n cout << ans << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 5060, "score_of_the_acc": -0.4547, "final_rank": 9 }, { "submission_id": "aoj_2226_2994420", "code_snippet": "#include \"bits/stdc++.h\"\n#include<unordered_map>\n#include<unordered_set>\n#pragma warning(disable:4996)\nusing namespace std;\nusing ld=long double;\nld eps=1e-9;\n\n\n/* 幾何の基本 */\n\n#include <complex>\n\ntypedef complex<ld> Point;\n\n\nconst ld pi = acos(-1.0);\nconst ld dtop = pi / 180.;\nconst ld ptod = 1. / dtop;\n\nnamespace std {\n\tbool operator<(const Point &lhs, const Point &rhs) {\n\t\tif (lhs.real() < rhs.real() - eps) return true;\n\t\tif (lhs.real() > rhs.real() + eps) return false;\n\t\treturn lhs.imag() < rhs.imag();\n\t}\n}\n\n// 点の入力\nPoint input_Point() {\n\tld x, y;\n\tcin >> x >> y;\n\treturn Point(x, y);\n}\n\n// 誤差つき等号判定\nbool eq(const ld a, const ld b) {\n\treturn (abs(a - b) < eps);\n}\n\n// 内積\nld dot(const Point& a, const Point& b) {\n\treturn real(conj(a) * b);\n}\n\n// 外積\nld cross(const Point& a, const Point& b) {\n\treturn imag(conj(a) * b);\n}\n\n// 直線の定義\nclass Line {\npublic:\n\tPoint a, b;\n\tLine() : a(Point(0, 0)), b(Point(0, 0)) {}\n\tLine(Point a, Point b) : a(a), b(b) {}\n\tPoint operator[](const int _num)const {\n\t\tif (_num == 0)return a;\n\t\telse if (_num == 1)return b;\n\t\telse {\n\t\t\tassert(false);\n\t\t\treturn Point();\n\t\t}\n\t}\n};\n\n// 円の定義\nclass Circle {\npublic:\n\tPoint p;\n\tld r;\n\tCircle() : p(Point(0, 0)), r(0) {}\n\tCircle(Point p, ld r) : p(p), r(r) {}\n};\n\n// ccw\n// 1: a,b,cが反時計周りの順に並ぶ\n//-1: a,b,cが時計周りの順に並ぶ\n// 2: c,a,bの順に直線に並ぶ\n//-2: a,b,cの順に直線に並ぶ\n// 0: a,c,bの順に直線に並ぶ\nint ccw(const Point& a, const Point &b, const Point &c) {\n\tconst Point nb(b - a);\n\tconst Point nc(c - a);\n\tif (cross(nb, nc) > eps) return 1; // a,b,cが反時計周りの順に並ぶ\n\tif (cross(nb, nc) < -eps) return -1; // a,b,cが時計周りの順に並ぶ\n\tif (dot(nb, nc) < 0) return 2; // c,a,bの順に直線に並ぶ\n\tif (norm(nb) < norm(nc)) return -2; // a,b,cの順に直線に並ぶ\n\treturn 0; // a,c,bの順に直線に並ぶ\n}\n\n\n/* 交差判定 */\n\n// 直線と直線の交差判定\nbool isis_ll(const Line& l, const Line& m) {\n\treturn !eq(cross(l.b - l.a, m.b - m.a), 0);\n}\n\n// 直線と線分の交差判定\nbool isis_ls(const Line& l, const Line& s) {\n\treturn isis_ll(l, s) &&\n\t\t(cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < eps);\n}\n\n// 線分と線分の交差判定\nbool isis_ss(const Line& s, const Line& t) {\n\treturn ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 &&\n\t\tccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0;\n}\n\n// 点の直線上判定\nbool isis_lp(const Line& l, const Point& p) {\n\treturn (abs(cross(l.b - p, l.a - p)) < eps);\n}\n\n// 点の線分上判定\nbool isis_sp(const Line& s, const Point& p) {\n\treturn (abs(s.a - p) + abs(s.b - p) - abs(s.b - s.a) < eps);\n}\n\n// 垂線の足\nPoint proj(const Line &l, const Point& p) {\n\tld t = dot(p - l.a, l.b - l.a) / norm(l.a - l.b);\n\treturn l.a + t * (l.b - l.a);\n}\n\n//線対象の位置にある点\nPoint reflect(const Line &l, const Point &p) {\n\tPoint pr = proj(l, p);\n\treturn pr * 2.l - p;\n}\n\n// 直線と直線の交点\nPoint is_ll(const Line &s, const Line& t) {\n\tPoint sv = s.b - s.a, tv = t.b - t.a;\n\tassert(cross(sv, tv) != 0);\n\treturn s.a + sv * cross(tv, t.a - s.a) / cross(tv, sv);\n}\n// 直線と直線の交点\nvector<Point> is_ll2(const Line &s, const Line& t) {\n\tPoint sv = s.b - s.a, tv = t.b - t.a;\n\tif (cross(sv, tv) != 0)return vector<Point>(1, is_ll(s, t));\n\telse {\n\t\tvector<Point>ans;\n\t\tfor (int k = 0; k < 2; ++k) {\n\t\t\tif (isis_sp(s, t[k]) && find(ans.begin(), ans.end(), t[k]) == ans.end())ans.push_back(t[k]);\n\t\t\tif (isis_sp(t, s[k]) && find(ans.begin(), ans.end(), s[k]) == ans.end())ans.push_back(s[k]);\n\t\t}\n\t\treturn ans;\n\t}\n}\n// 線分と線分の交点\n// 重なってる部分あるとassert(false)\nPoint is_ss(const Line &s, const Line& t) {\n\tif (isis_ss(s, t)) {\n\t\tfor (int k = 0; k < 2; ++k) {\n\t\t\tfor (int l = 0; l < 2; ++l) {\n\t\t\t\tif (s[k] == t[l])return s[k];\n\t\t\t}\n\t\t}\n\t\treturn is_ll(s, t);\n\t}\n\telse {\n\t\t//先にisis_ssしてね\n\t\tassert(false);\n\t\treturn Point(0, 0);\n\t}\n}\n// 線分と線分の交点\nvector<Point> is_ss2(const Line &s, const Line& t) {\n\tvector<Point> kouho(is_ll2(s, t));\n\tvector<Point>ans;\n\tfor (auto p : kouho) {\n\t\tif (isis_sp(s, p) && isis_sp(t, p))ans.emplace_back(p);\n\t}\n\treturn ans;\n}\n// 直線と点の距離\nld dist_lp(const Line& l, const Point& p) {\n\treturn abs(p - proj(l, p));\n}\n\n//直線と直線の距離\nld dist_ll(const Line& l, const Line& m) {\n\treturn isis_ll(l, m) ? 0 : dist_lp(l, m.a);\n}\n\n// 直線と線分の距離\nld dist_ls(const Line& l, const Line& s) {\n\treturn isis_ls(l, s) ? 0 : min(dist_lp(l, s.a), dist_lp(l, s.b));\n}\n\n// 線分と点の距離\nld dist_sp(const Line& s, const Point& p) {\n\tPoint r = proj(s, p);\n\treturn isis_sp(s, r) ? abs(r - p) : min(abs(s.a - p), abs(s.b - p));\n}\n\n// 線分と線分の距離\nld dist_ss(const Line& s, const Line& t) {\n\tif (isis_ss(s, t)) return 0;\n\treturn min({ dist_sp(s, t.a), dist_sp(s, t.b), dist_sp(t, s.a), dist_sp(t, s.b) });\n}\n\n\n//直線と直線の二等分線のベクトル\nLine line_bisection(const Line &s, const Line &t) {\n\tconst Point laglanju(is_ll(s, t));\n\tconst Point avec = !(abs(laglanju - s[0])<eps) ? s[0] - laglanju : s[1] - laglanju;\n\tconst Point bvec = !(abs(laglanju - t[0])<eps) ? t[0] - laglanju : t[1] - laglanju;\n\n\treturn Line(laglanju, laglanju + (abs(bvec)*avec + abs(avec)*bvec) / (abs(avec) + abs(bvec)));\n}\n//点と点の垂直二等分線 aを左に見ながら\nLine point_bisection(const Point&a, const Point&b) {\n\tconst Point cen((a + b) / 2.l);\n\tconst Point vec = (b - a)*Point(0, 1);\n\treturn Line(cen, cen + vec);\n}\n\n//三つの点からなる外心\nPoint outer_center(const vector<Point>&ps) {\n\tassert(ps.size() == 3);\n\tLine l1 = point_bisection(ps[0], ps[1]);\n\tLine l2 = point_bisection(ps[1], ps[2]);\n\treturn is_ll(l1, l2);\n}\n\n\n//三つの直線からなる内心\n//三つの直線が並行でないことは確かめといてね\nPoint inner_center(const vector<Line>&ls) {\n\tvector<Point>vertics;\n\tfor (int i = 0; i <static_cast<int>(ls.size()); ++i) {\n\t\tvertics.push_back(is_ll(ls[i], ls[(i + 1) % 3]));\n\t}\n\tif (vertics[0] == vertics[1] || vertics[1] == vertics[2] || vertics[2] == vertics[0])return vertics[0];\n\tLine bi1(line_bisection(Line(vertics[0], vertics[1]), Line(vertics[0], vertics[2])));\n\tLine bi2(line_bisection(Line(vertics[1], vertics[2]), Line(vertics[1], vertics[0])));\n\tif (bi1[0] == bi2[0])return bi1[0];\n\telse {\n\t\treturn is_ll(bi1, bi2);\n\t}\n}\n\n//三つの直線からなる傍心\n//三つの直線が並行でないことは確かめといてね\nvector<Point> ex_center(const vector<Line>&ls) {\n\tvector<Point>vertics;\n\tfor (int i = 0; i < static_cast<int>(ls.size()); ++i) {\n\t\tvertics.push_back(is_ll(ls[i], ls[(i + 1) % 3]));\n\t}\n\tif (abs(vertics[0] - vertics[1])<eps || abs(vertics[1] - vertics[2])<eps || (abs(vertics[2] - vertics[0])<eps))return vector<Point>();\n\tvector<Point>ecs;\n\tfor (int i = 0; i < 3; ++i) {\n\t\tLine bi1(line_bisection(Line(vertics[i], vertics[i] * 2.0l - vertics[(i + 2) % 3]), Line(vertics[i], vertics[(i + 1) % 3])));\n\t\tLine bi2(line_bisection(Line(vertics[(i + 1) % 3], vertics[(i + 1) % 3] * 2.0l - vertics[(i + 2) % 3]), Line(vertics[(i + 1) % 3], vertics[i])));\n\t\tecs.push_back(is_ll(bi1, bi2));\n\t}\n\treturn ecs;\n}\n\n\n//a,b:並行\n//c:並行でない\n//三つの直線から同距離の位置を求める。\nvector<Point> same_dis(const vector<Line>&ls) {\n\tvector<Point>vertics;\n\tvertics.push_back(is_ll(ls[0], ls[2]));\n\tvertics.push_back(is_ll(ls[1], ls[2]));\n\n\tif (abs(vertics[0] - vertics[1]) < eps)return vector<Point>{vertics[0]};\n\tLine bis(line_bisection(ls[0], ls[1]));\n\tvector<Point>ecs;\n\n\tLine abi(line_bisection(Line(vertics[0], vertics[1]), ls[0]));\n\tecs.push_back(is_ll(bis, abi));\n\n\n\tLine bbi(line_bisection(Line(vertics[0], 2.l*vertics[0] - vertics[1]), ls[0]));\n\tecs.push_back(is_ll(bis, bbi));\n\n\treturn ecs;\n}\n/* 円 */\n\n// 円と円の交点\nvector<Point> is_cc(const Circle& c1, const Circle& c2) {\n\tvector<Point> res;\n\tld d = abs(c1.p - c2.p);\n\tld rc = (d * d + c1.r * c1.r - c2.r * c2.r) / (2 * d);\n\tld dfr = c1.r * c1.r - rc * rc;\n\tif (abs(dfr) < eps) dfr = 0.0;\n\telse if (dfr < 0.0) return res; // no intersection\n\tld rs = sqrt(dfr);\n\tPoint diff = (c2.p - c1.p) / d;\n\tres.push_back(c1.p + diff * Point(rc, rs));\n\tif (dfr != 0.0) res.push_back(c1.p + diff * Point(rc, -rs));\n\treturn res;\n}\n\n/*\n点が円の中にいるか\n0 => out\n1 => on\n2 => in*/\nint is_in_Circle(const Point& p, const Circle &cir) {\n\tld dis = abs(cir.p - p);\n\tif (dis > cir.r + eps)return 0;\n\telse if (dis < cir.r - eps)return 2;\n\telse return 1;\n}\nint is_in_Circle(const Circle &cir, const Point& p) {\n\tld dis = abs(cir.p - p);\n\tif (dis > cir.r + eps)return 0;\n\telse if (dis < cir.r - eps)return 2;\n\telse return 1;\n}\n/*\n円lcが円rcの中にいるか\n0 => out\n1 => on\n2 => in*/\nint Circle_in_Circle(const Circle &lc, const Circle&rc) {\n\tld dis = abs(lc.p - rc.p);\n\tif (dis < rc.r - lc.r - eps)return 2;\n\telse if (dis>rc.r - lc.r + eps)return 0;\n\telse return 1;\n}\n\n// 円と直線の交点\nvector<Point> is_lc(const Circle& c, const Line& l) {\n\tvector<Point> res;\n\tld d = dist_lp(l, c.p);\n\tif (d < c.r + eps) {\n\t\tld len = (d > c.r) ? 0.0 : sqrt(c.r * c.r - d * d); //safety;\n\t\tPoint nor = (l.a - l.b) / abs(l.a - l.b);\n\t\tres.push_back(proj(l, c.p) + len * nor);\n\t\tres.push_back(proj(l, c.p) - len * nor);\n\t}\n\treturn res;\n}\n\n// 円と線分の距離\nvector<Point> is_sc(const Circle& c, const Line& l) {\n\tvector<Point> v = is_lc(c, l), res;\n\tfor (Point p : v)\n\t\tif (isis_sp(l, p)) res.push_back(p);\n\treturn res;\n}\n\n// 円と点の接線\nvector<Line> tangent_cp(const Circle& c, const Point& p) {\n\tvector<Line> ret;\n\tPoint v = c.p - p;\n\tld d = abs(v);\n\tld l = sqrt(norm(v) - c.r * c.r);\n\tif (isnan(l)) { return ret; }\n\tPoint v1 = v * Point(l / d, c.r / d);\n\tPoint v2 = v * Point(l / d, -c.r / d);\n\tret.push_back(Line(p, p + v1));\n\tif (l < eps) return ret;\n\tret.push_back(Line(p, p + v2));\n\treturn ret;\n}\n\n// 円と円の接線\nvector<Line> tangent_cc(const Circle& c1, const Circle& c2) {\n\tvector<Line> ret;\n\tif (abs(c1.p - c2.p) - (c1.r + c2.r) > -eps) {\n\t\tPoint center = (c1.p * c2.r + c2.p * c1.r) / (c1.r + c2.r);\n\t\tret = tangent_cp(c1, center);\n\t}\n\tif (abs(c1.r - c2.r) > eps) {\n\t\tPoint out = (-c1.p * c2.r + c2.p * c1.r) / (c1.r - c2.r);\n\t\tvector<Line> nret = tangent_cp(c1, out);\n\t\tret.insert(ret.end(), nret.begin(), nret.end());\n\t}\n\telse {\n\t\tPoint v = c2.p - c1.p;\n\t\tv /= abs(v);\n\t\tPoint q1 = c1.p + v * Point(0, 1) * c1.r;\n\t\tPoint q2 = c1.p + v * Point(0, -1) * c1.r;\n\t\tret.push_back(Line(q1, q1 + v));\n\t\tret.push_back(Line(q2, q2 + v));\n\t}\n\treturn ret;\n}\n//二つの円の重なり面積\nld two_Circle_area(const Circle&l, const Circle&r) {\n\tld dis = abs(l.p - r.p);\n\tif (dis > l.r + r.r)return 0;\n\telse if (dis + r.r < l.r) {\n\t\treturn r.r*r.r*pi;\n\t}\n\telse if (dis + l.r < r.r) {\n\t\treturn l.r*l.r*pi;\n\t}\n\telse {\n\t\tld ans = (l.r)*(l.r)*acos((dis*dis + l.r*l.r - r.r*r.r) / (2 * dis*l.r)) +\n\t\t\t(r.r)*(r.r)*acos((dis*dis + r.r*r.r - l.r*l.r) / (2 * dis*r.r)) -\n\t\t\tsqrt(4 * dis*dis*l.r*l.r - (dis*dis + l.r*l.r - r.r*r.r)*(dis*dis + l.r*l.r - r.r*r.r)) / 2;\n\t\treturn ans;\n\t}\n\n}\n\n//0~2π\n//ベクトルa から反時計回りに Θ 回ると b になる\nld gettheta(const Point&a, const Point &b) {\n\tld adot = dot(a, b) / abs(a) / abs(b);\n\tif (adot > 1)adot -= eps;\n\tif (adot < -1)adot += eps;\n\tld theta = acos(adot);\n\tld dd = cross(a, b);\n\treturn cross(a, b)>-eps ? theta : 2 * pi - theta;\n}\n vector<ld>quad_equa(ld a, ld b, ld c) {\n\tld root = b*b - 4 * a*c;\n\tif (abs(root) < eps) {\n\t\tld ans = -b / (2 * a);\n\t\treturn vector<ld>(2, ans);\n\t}\n\telse if (root > 0) {\n\t\tld ans = -b / (2 * a);\n\t\tvector<ld>anss(2);\n\t\tanss[0] = ans + sqrt(root) / (2 * a);\n\t\tanss[1] = ans - sqrt(root) / (2 * a);\n\t\tif (anss[0] > anss[1])swap(anss[0], anss[1]);\n\t\treturn anss;\n\t}\n\telse {\n\t\treturn vector<ld>();\n\t}\n}\nld getr(ld A,ld l, ld len) {\n\tld k=quad_equa(A,2*l,-2*len)[1];\n\treturn l+A*k;\n}\n\nld get_time(ld A,ld l, ld r, ld len) {\n\tld p=sqrt(A*len+(r*r+l*l)/2);\n\treturn ((p-l)+(p-r))/A;\n}\n\nld solve(ld A,vector<ld>straights, vector<pair<ld, ld>>arcs) {\n\tvector<ld>vels(arcs.size(),-1);\n\n\tpriority_queue<pair<ld,int>>que;\n\n\t\n\tfor (int i = 0; i < arcs.size(); ++i) {\n\t\tque.push(make_pair(-arcs[i].first,i));\n\t}\n\n\twhile (!que.empty()) {\n\t\tauto atop(que.top());\n\n\t\tld vel=-atop.first;\n\t\tint now=atop.second;\n\t\tque.pop();\n\t\tif(vels[now]>-0.5)continue;\n\t\t\n\t\tvels[now]=vel;\n\t\t\n\t\tif (now != 0) {\n\t\t\tque.push(make_pair(-getr(A,vel,straights[now-1]),now-1));\n\t\t}\n\t\tif (now != arcs.size() - 1) {\n\t\t\tque.push(make_pair(-getr(A,vel,straights[now]),now+1));\n\t\t}\n\t}\n\n\tld answer=0;\n\tfor (int i = 0; i < straights.size(); ++i) {\n\t\tanswer+=get_time(A,vels[i],vels[i+1],straights[i]);\n\t}\n\tfor (int i = 1; i < arcs.size()-1; ++i) {\n\t\tanswer+=arcs[i].second/vels[i];\n\t}\n\treturn answer;\n}\nint main()\n{\n\tint N;cin>>N;\n\tld A;cin>>A;\n\tvector<pair<pair<ld,ld>,pair<ld,ld>>>vs;\n\tvector<Line>ls;\n\tfor (int i = 0; i < N; ++i) {\n\t\tint a,b,c,d;cin>>a>>b>>c>>d;\n\t\tls.push_back(Line(Point(a,b),Point(c,d)));\n\t}\n\n\t//maxspeed , length\n\tvector<pair<ld, ld>>arcs(1, pair<ld, ld>{0, 0});;\n\tfor (int i = 0; i < N - 1; i++) {\n\t\tLine l(ls[i]);\n\t\tLine r(ls[i+1].a,ls[i+1].b);\n\t\t//Line bis(line_bisection(l,r));\n\n\t\tld theta(gettheta(r.b-r.a,l.b-l.a));\n\n\t\tif (theta < eps) {\n\t\t\tarcs.emplace_back(1e9,abs(r.a-l.b));\n\t\t}\n\t\telse {\n\t\t\t{\n\t\t\t\tif (ccw(l.a, l.b, r.a) == -1) {\n\t\t\t\t\ttheta=theta;\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\ttheta=2*pi-theta;\n\t\t\t\t}\n\t\t\t}\n\t\t\tld radius=abs(theta-pi)<eps?1*abs(r.a-l.b)/2:abs(r.a-l.b)*(sin((pi-theta)/2)/sin(theta));\n\n\t\t\tld len=radius*theta;\n\t\t\tld max_speed=sqrt(radius*A);\n\t\t\tarcs.emplace_back(max_speed,len);\n\t\t}\n\t}\n\tarcs.push_back(make_pair(0,0));\n\tvector<ld>straights;\n\tfor (int i = 0; i < N; ++i) {\n\t\tstraights.push_back(abs(ls[i].b-ls[i].a));\n\t}\n\tld ans=solve(A,straights,arcs);\n\tcout<<setprecision(10)<<fixed<<ans<<endl;\n\treturn 0;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 16672, "score_of_the_acc": -1.4444, "final_rank": 15 }, { "submission_id": "aoj_2226_2670923", "code_snippet": "#ifndef VS\n#include<iostream>\n#include<vector>\n#include<algorithm>\n#include<stack>\n#include<queue>\n#include<map>\n#include<set>\n#include<iomanip>\n#include<string>\n#include<assert.h>\n#include<math.h>\n#include<stdio.h>\n#include<ctype.h>\n#include<complex>\n#endif\nusing namespace std;\ntypedef long long LL;\n\n#ifdef BTK\n#define DEBUG if(1)\n#else\n#define DEBUG if(0)\n#endif\n\n#define FOR(i,bg,ed) for(int i=(bg);i<(ed);i++)\n#define REP(i,n) FOR(i,0,n)\n#define ALL(v) (v).begin(),(v).end()\n#define REC(ret, ...) std::function<ret (__VA_ARGS__)>\ntemplate <typename T>inline bool chmin(T &l, T r) { bool a = l>r; if (a)l = r; return a; }\ntemplate <typename T>inline bool chmax(T &l, T r) { bool a = l<r; if (a)l = r; return a; }\n\nstruct input {\n\tbool success;\n\tinput(){success = true;}\n#ifdef BTK\n\ttemplate<typename T>inline input& operator>>(T& x) {success = !!(cin >> x);return *this;}\n#else\n\tinline input& operator>>(LL& x) {success = (scanf(\"%lld\", &x) != EOF);return *this;}\n\tinline input& operator>>(int& x) {success = (scanf(\"%d\", &x) != EOF);return *this;}\n\tinline input& operator>>(char* x) {success = (scanf(\"%s\", x) != EOF);return *this;}\n\tinline input& operator>>(double& x) {success = (scanf(\"%lf\", &x) != EOF);return *this;}\n#endif\n}in;\n\nconst char space = ' ';\nconst char ln = '\\n';\nstruct output {\n#ifdef BTK\n\ttemplate<typename T>inline output& operator<<(T x) {cout << x; return (*this);}\n\tinline void fp(const double x, const char* s) { char buf[20];sprintf(buf, s, x); cout << buf;}\n#else\n\tinline output& operator<<(const string& x) { printf(\"%s\", x.c_str()); return *this; }\n\tinline output& operator<<(const int x) { printf(\"%d\", x); return *this;}\n\tinline output& operator<<(const char x) { putchar(x); return *this;}\n\tinline output& operator<<(const LL x) { printf(\"%lld\", x); return *this;}\n\tinline output& operator<<(const double x) { printf(\"%lf\", x); return *this;}\n\tinline void fp(const double x, const char* s) {printf(s, x);}\n#endif\n}out;\n\n#define fi first\n#define se second\n#define pb push_back\n\n#define IL inline \nnamespace geo {\n\ttypedef double D;\n\ttypedef bool B;\n\ttypedef complex<D> P;\n#define X real()\n#define Y imag()\n\tB comp(const P& l, const P& r) { return (l.X == r.X) ? l.Y < r.Y : l.X < r.X; }\n\ttypedef pair<P, P> L; //line\n\ttypedef pair<P, P> LS; //line segment\n\ttypedef pair<P, D> C; //circle\n\ttypedef vector<P> Poly;\n\tconst D EPS = 1e-8;\n\t//Decompotision Macro\n#define DCl(a,b,l) P (a)=l.fi,(b)=l.se; \n#define DCc(a,b,c) P (a)=c.fi;D (b)=c.se; \n\n\t//A dot B\n\tIL D dot(P a, P b) { return a.X*b.X + a.Y*b.Y; }\n\t//A cross B\n\tIL D cross(P a, P b) { return a.X*b.Y - a.Y*b.X; }\n\tIL D ccw(P a, P b, P c) {\n\t\treturn cross(b - a, c - a);\n\t}\n\n\tIL int sgn(P a, P b, P c) {\n\t\tif (cross(b - a, c - a) > EPS)return 1;\n\t\tif (cross(b - a, c - a) < -EPS)return -1;\n\t\tif (dot(b - a, c - a) < -EPS)return 2; // b-a-c\n\t\tif (dot(a - b, c - b) < -EPS)return -2; // a-b-c\n\t\treturn 0;\n\t}\n\tIL B is_L_L(L p, L q) {\n\t\tDCl(a, b, p);\n\t\tDCl(c, d, q);\n\t\treturn\n\t\t\tabs(cross(a - b, c - d)) > EPS \n\t\t\t//|| abs(cross(a - b, d - b)) < EPS\n\t\t\t;\n\n\t}\n\tIL D signed_distance_P_L(P p, L l) {\n\t\tDCl(a, b, l);\n\t\treturn ccw(a, b, p) / abs(b - a);\n\t}\n\n\tIL P cross_L_L(L p, L q) {\n\t\tDCl(a, b, p);\n\t\tD d1 = signed_distance_P_L(a, q);\n\t\tD d2 = signed_distance_P_L(b, q);\n\t\treturn (a * d2 - b * d1) / (d2 - d1);\n\t}\n\n}\nusing namespace geo;\nconst D pi = acos(-1);\nint main() {\n\tint N;\n\tin >> N;\n\tD amax;\n\tin >> amax;\n\tvector<LS> ls(N);\n\tvector<P> center(N - 1);\n\tvector<D> vmax(N + 1);\n\tREP(i, N) {\n\t\tD a, b, c, d;\n\t\tin >> a >> b >> c >> d;\n\t\tls[i]=LS(P(a, b), P(c, d));\n\t}\n\tvmax[0] = 0;\n\tREP(i, N - 1) {\n\t\tLS prv = ls[i];\n\t\tLS nxt = ls[i+1];\n\t\tP v = prv.second - prv.first;\n\t\tP u = nxt.second - nxt.first;\n\t\tv = P(-v.Y, v.X);\n\t\tu = P(-u.Y, u.X);\n\t\tprv = LS(prv.second, prv.second + v);\n\t\tnxt = LS(nxt.first, nxt.first + u);\n\t\tif (is_L_L(prv, nxt)) {\n\t\t\tcenter[i] = cross_L_L(prv, nxt);\n\t\t}\n\t\telse {\n\t\t\tcenter[i] = (prv.first + nxt.first) / 2.0;\n\t\t}\n\t\tD r = abs(prv.first - center[i]);\n\t\tvmax[i + 1] = amax*r;\n\t}\n\tvmax[N] = 0;\n\tREP(i, N) {\n\t\tD s = abs(ls[i].second - ls[i].first);\n\t\tchmin(vmax[i + 1], vmax[i] + 2 * amax*s);\n\t}\n\tD ret = 0;\n\tREP(_, N) {\n\t\tint i = N - _ - 1;\n\t\tD s = abs(ls[i].second - ls[i].first);\n\t\tchmin(vmax[i], vmax[i + 1] + 2 * amax*s);\n\t}\n\tREP(i, N - 1) {\n\t\tP v = ls[i].second - center[i];\n\t\tP u = ls[i+1].first - center[i];\n\t\tD r = abs(v);\n\t\tD si = cross(v, u);\n\t\tD co = dot(v, u);\n\t\tD theta = atan2(si, co);\n\t\tif (theta < 0)theta =-theta;\n\t\tif (abs(theta - pi)>EPS) {\n\t\t\tP cp = cross_L_L(ls[i], ls[i + 1]);\n\t\t\tif (dot(cp - ls[i].second, ls[i].second - ls[i].first) < 0) {\n\t\t\t\ttheta = 2 * pi - theta;\n\t\t\t}\n\t\t}\n\t\tD s = r*theta;\n\t\tret += s / sqrt(vmax[i + 1]);\n\t}\n\tREP(i, N) {\n\t\t/*\n\t\tv1-v0 = 2*a*s\n\t\tw = v0+2*a*x;\n\t\tw = v1+2*a*y;\n\t\tv1-v0 = 2*a*(x-y)\n\n\t\t*/\n\t\tD s = abs(ls[i].second - ls[i].first);\n\t\tD x = (s + (vmax[i + 1] - vmax[i]) / (2.0*amax)) / 2.0;\n\t\tD y = s - x;\n\t\tD w = sqrt(vmax[i] + 2 * amax*x);\n\t\tD ww = sqrt(vmax[i + 1] + 2 * amax*y);\n\t\t//t*(v+u)/2 = x\n\t\tret += 2.0*x / (sqrt(vmax[i]) + w);\n\t\tret += 2.0*y / (sqrt(vmax[i + 1]) + w);\n\t}\n\tout.fp(ret, \"%.10f\\n\");\n\treturn 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 4980, "score_of_the_acc": -0.2264, "final_rank": 3 }, { "submission_id": "aoj_2226_1577749", "code_snippet": "#include<stdio.h>\n#include<algorithm>\n#include<math.h>\nusing namespace std;\nint x1[41000];\nint Y1[41000];\nint x2[41000];\nint y2[41000];\ndouble r[41000];\ndouble len[41000];\nconst double EPS = 1e-10;\nconst double INF = 1e+10;\nconst double PI = acos(-1);\nint sig(double r) { return (r < -EPS) ? -1 : (r > +EPS) ? +1 : 0; }\ninline double ABS(double a){return max(a,-a);}\nstruct Pt {\n\tdouble x, y;\n\tPt() {}\n\tPt(double x, double y) : x(x), y(y) {}\n\tPt operator+(const Pt &a) const { return Pt(x + a.x, y + a.y); }\n\tPt operator-(const Pt &a) const { return Pt(x - a.x, y - a.y); }\n\tPt operator*(const Pt &a) const { return Pt(x * a.x - y * a.y, x * a.y + y * a.x); }\n\tPt operator-() const { return Pt(-x, -y); }\n\tPt operator*(const double &k) const { return Pt(x * k, y * k); }\n\tPt operator/(const double &k) const { return Pt(x / k, y / k); }\n\tdouble ABS() const { return sqrt(x * x + y * y); }\n\tdouble abs2() const { return x * x + y * y; }\n\tdouble arg() const { return atan2(y, x); }\n\tdouble dot(const Pt &a) const { return x * a.x + y * a.y; }\n\tdouble det(const Pt &a) const { return x * a.y - y * a.x; }\n};\ndouble tri(const Pt &a, const Pt &b, const Pt &c) { return (b - a).det(c - a); }\nPt p1[41000];\nPt p2[41000];\nint iLL(Pt a, Pt b, Pt c, Pt d) {\n\tif (sig((b - a).det(d - c))) return 1; // intersect\n\tif (sig((b - a).det(c - a))) return 0; // parallel\n\treturn -1; // correspond\n}\nPt pLL(Pt a, Pt b, Pt c, Pt d) {\n\tb = b - a; d = d - c; return a + b * (c - a).det(d) / b.det(d);\n}\ndouble v[41000];\ndouble gett(double a,double s,double v1,double v2){\n\tdouble vt=sqrt(a*s+(v1*v1+v2*v2)/2);\n\t//printf(\"%f %f\\n\",vt,(2*vt-v1-v2)/a);\n\treturn (2*vt-v1-v2)/a;\n}\nint main(){\n\tint a,b;\n\tscanf(\"%d%d\",&a,&b);\n\tfor(int i=0;i<a;i++){\n\t\tscanf(\"%d%d%d%d\",x1+i,Y1+i,x2+i,y2+i);\n\t\tp1[i]=Pt((double)x1[i],(double)Y1[i]);\n\t\tp2[i]=Pt((double)x2[i],(double)y2[i]);\n\t}\n\tfor(int i=0;i<a-1;i++){\n\t\tif(iLL(p1[i],p2[i],p1[i+1],p2[i+1])==0){\n\t\t\tdouble d=(p2[i]-p1[i+1]).ABS();\n\t\t\tr[i]=d/2;\n\t\t\tlen[i]=d*PI/2;\n\t\t}else{\n\t\t\tPt p=pLL(p2[i],p2[i]+(p1[i]-p2[i])*Pt(0,1),p1[i+1],p1[i+1]+(p2[i+1]-p1[i+1])*Pt(0,1));\n\t\t\tr[i]=(p-p2[i]).ABS();\n\t\t\tdouble d=(p2[i]-p1[i+1]).ABS()/2;\n\t\t\tdouble th=asin(d/r[i])*2;\n\t\t\tif((p2[i]-p1[i]).dot(p1[i+1]-p2[i])<-EPS)th+=PI;\n\t\t\tlen[i]=r[i]*th;\n\t\t}\n\t}\n\tdouble now=0;\n\tfor(int i=0;i<a-1;i++){\n\t\tnow=sqrt((p2[i]-p1[i]).ABS()*2*b+now*now);\n\t\tnow=min(now,sqrt(r[i]*b));\n\t\tv[i+1]=now;\n\t}\n\tnow=0;\n\tfor(int i=a-1;i>0;i--){\n\t\tnow=sqrt((p2[i]-p1[i]).ABS()*2*b+now*now);\n\t\tnow=min(now,sqrt(r[i-1]*b));\n\t\tnow=min(now,v[i]);\n\t\tv[i]=now;\n\t}\n\t//for(int i=0;i<=a;i++)printf(\"%f %f %f\\n\",v[i],r[i],len[i]);\n\tdouble ret=0;\n\tfor(int i=0;i<a;i++){\n\t\tret+=gett((double)b,(p2[i]-p1[i]).ABS(),v[i],v[i+1]);\n\t\tif(i<a-1){\n\t\t\tret+=len[i]/v[i+1];\n\t\t}\n\t}\n\tprintf(\"%.10f\\n\",ret);\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3908, "score_of_the_acc": -0.1453, "final_rank": 1 }, { "submission_id": "aoj_2226_1536567", "code_snippet": "#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <cmath>\n#include <iostream>\n#include <algorithm>\n#include <iomanip>\n\nusing namespace std;\n\nconst int N = 40000 + 10;\nconst double eps = 1e-8;\nconst double PI = acos(-1.0);\n\nint sgn(double x)\n{\n\tif (x < -eps) return -1;\n\tif (x > eps) return 1;\n\treturn 0;\n}\n\ndouble dbl(double x)\n{\n\treturn x * x;\n}\n\nstruct Point {\n\tdouble x, y;\n\tPoint() {}\n\tPoint(const double _x, const double _y) {\n\t\tx = _x, y = _y;\n\t}\n\tfriend Point operator + (const Point &a, const Point &b) {\n\t\treturn Point(a.x + b.x, a.y + b.y);\n\t}\n\tfriend Point operator - (const Point &a, const Point &b) {\n\t\treturn Point(a.x - b.x, a.y - b.y);\n\t}\n\tfriend Point operator * (const Point &a, const double k) {\n\t\treturn Point(a.x * k, a.y * k);\n\t}\n\tfriend Point operator / (const Point &a, const double k) {\n\t\treturn Point(a.x / k, a.y / k);\n\t}\n\tfriend double dot(const Point &a, const Point &b) {\n\t\treturn (a.x * b.x + a.y * b.y);\n\t}\n\tfriend double det(const Point &a, const Point &b) {\n\t\treturn (a.x * b.y - a.y * b.x);\n\t}\n\tfriend double len(const Point &a) {\n\t\treturn sqrt(a.x * a.x + a.y * a.y);\n\t}\n\tvoid read() {\n\t\tcin >> x >> y;\n\t}\n\tvoid write() const {\n\t\tcout << x << \" \" << y << endl;\n\t}\n};\n\nstruct Seg {\n\tPoint u, v, w;\n\tdouble dist;\n\tSeg() {}\n\tSeg(const Point &_u, const Point &_v) {\n\t\tu = _u, v = _v;\n\t}\n\tvoid read() {\n\t\tu.read();\n\t\tv.read();\n\t}\n\tvoid prepare() {\n\t\tw = v - u;\n\t\tw = Point(-w.y, w.x);\n\t\tdist = len(v - u);\n\t}\n} seg[N];\n\nbool intersect(const Point &a, const Point &b, const Point &c, const Point &d, Point &e)\n{\n\tdouble s1 = det(c - a, d - a);\n\tdouble s2 = det(d - b, c - b);\n\tif (!sgn(s1 + s2)) return false;\n\te = (b - a) * (s1 / (s1 + s2)) + a;\n\treturn true;\n}\n\ndouble rectify(double x)\n{\n\twhile (x < -eps) x += 2 * PI;\n\twhile (x > 2 * PI + eps) x -= 2 * PI;\n\treturn x;\n}\n\nint n;\nPoint center[N];\ndouble amax, radius[N], angle[N], vmax[N][2];\n\ndouble calc(double vstart, double vend, double dist)\n{\n\tdouble vm = sqrt((2 * amax * dist + dbl(vstart) + dbl(vend)) / 2.0);\n\tdouble ret = (2 * vm - vstart - vend) / amax;\n\treturn ret;\n}\n\nint main()\n{\n\tios::sync_with_stdio(false);\n\tcin >> n >> amax;\n\tfor (int i = 1; i <= n; ++i) {\n\t\tseg[i].read();\n\t\tseg[i].prepare();\n\t}\n\tfor (int i = 1; i < n; ++i) {\n\t\tstatic Point u, v;\n\t\tstatic double ang1, ang2;\n\t\t\n\t\tbool ret = intersect(seg[i].v, seg[i].v + seg[i].w, seg[i + 1].u, seg[i + 1].u + seg[i + 1].w, center[i]);\n\t\tif (!ret) center[i] = (seg[i].v + seg[i + 1].u) / 2.0;\n\n\t\tradius[i] = len(seg[i].v - center[i]);\n\t\tu = seg[i].v - center[i];\n\t\tv = seg[i + 1].u - center[i];\n\t\tang1 = atan2(u.y, u.x);\n\t\tang2 = atan2(v.y, v.x);\n\t\tif (sgn(det(seg[i].v - center[i], seg[i].v - seg[i].u)) > 0) {\n\t\t\tangle[i] = rectify(ang2 - ang1);\n\t\t} else {\n\t\t\tangle[i] = rectify(ang1 - ang2);\n\t\t}\n\t}\n\t/*\n\tfor (int i = 1; i < n; ++i) {\n\t\tcout << center[i].x << \" \" << center[i].y << \" \" << angle[i] << endl;\n\t}\n\t*/\n\tvmax[n][1] = 0.0;\n\tvmax[n][0] = sqrt(2 * amax * seg[n].dist);\n\t\n\tfor (int i = n - 1; i >= 1; --i) {\n\t\tvmax[i][1] = min(vmax[i + 1][0], sqrt(amax * radius[i]));\n\t\tvmax[i][0] = sqrt(dbl(vmax[i][1]) + 2 * amax * seg[i].dist);\n\t}\n\n\tdouble times = 0;\n\tdouble v = 0;\n\n\tfor (int i = 1; i <= n; ++i) {\n\t\tif (v > vmax[i][1]) {\n\t\t\ttimes += calc(v, vmax[i][1], seg[i].dist);\n\t\t\tv = vmax[i][1];\n\t\t} else {\n\t\t\tif (dbl(v) + 2 * amax * seg[i].dist <= dbl(vmax[i][1])) {\n\t\t\t\ttimes += (sqrt(dbl(v) + 2 * amax * seg[i].dist) - v) / amax;\n\t\t\t\tv = sqrt(dbl(v) + 2 * amax * seg[i].dist);\n\t\t\t} else {\n\t\t\t\ttimes += calc(v, vmax[i][1], seg[i].dist);\n\t\t\t\tv = vmax[i][1];\n\t\t\t}\n\t\t}\n\t\tif (i < n) {\n\t\t\ttimes += radius[i] * angle[i] / v;\n\t\t}\n\t}\n\n\tcout << setiosflags(ios::fixed) << setprecision(8) << times << endl;\n\t\n\treturn 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 5384, "score_of_the_acc": -0.3681, "final_rank": 6 }, { "submission_id": "aoj_2226_1536561", "code_snippet": "#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <cmath>\n#include <iostream>\n#include <algorithm>\n#include <iomanip>\n\nusing namespace std;\n\nconst int N = 40000 + 10;\nconst double eps = 1e-8;\nconst double PI = acos(-1.0);\n\nint sgn(double x)\n{\n\tif (x < -eps) return -1;\n\tif (x > eps) return 1;\n\treturn 0;\n}\n\ndouble dbl(double x)\n{\n\treturn x * x;\n}\n\nstruct Point {\n\tdouble x, y;\n\tPoint() {}\n\tPoint(const double _x, const double _y) {\n\t\tx = _x, y = _y;\n\t}\n\tfriend Point operator + (const Point &a, const Point &b) {\n\t\treturn Point(a.x + b.x, a.y + b.y);\n\t}\n\tfriend Point operator - (const Point &a, const Point &b) {\n\t\treturn Point(a.x - b.x, a.y - b.y);\n\t}\n\tfriend Point operator * (const Point &a, const double k) {\n\t\treturn Point(a.x * k, a.y * k);\n\t}\n\tfriend Point operator / (const Point &a, const double k) {\n\t\treturn Point(a.x / k, a.y / k);\n\t}\n\tfriend double dot(const Point &a, const Point &b) {\n\t\treturn (a.x * b.x + a.y * b.y);\n\t}\n\tfriend double det(const Point &a, const Point &b) {\n\t\treturn (a.x * b.y - a.y * b.x);\n\t}\n\tfriend double len(const Point &a) {\n\t\treturn sqrt(a.x * a.x + a.y * a.y);\n\t}\n\tvoid read() {\n\t\tcin >> x >> y;\n\t}\n\tvoid write() const {\n\t\tcout << x << \" \" << y << endl;\n\t}\n};\n\nstruct Seg {\n\tPoint u, v, w;\n\tdouble dist;\n\tSeg() {}\n\tSeg(const Point &_u, const Point &_v) {\n\t\tu = _u, v = _v;\n\t}\n\tvoid read() {\n\t\tu.read();\n\t\tv.read();\n\t}\n\tvoid prepare() {\n\t\tw = v - u;\n\t\tw = Point(-w.y, w.x);\n\t\tdist = len(v - u);\n\t}\n} seg[N];\n\nbool intersect(const Point &a, const Point &b, const Point &c, const Point &d, Point &e)\n{\n\tdouble s1 = det(c - a, d - a);\n\tdouble s2 = det(d - b, c - b);\n\tif (!sgn(s1 + s2)) return false;\n\te = (b - a) * (s1 / (s1 + s2)) + a;\n\treturn true;\n}\n\ndouble rectify(double x)\n{\n\twhile (x < -eps) x += 2 * PI;\n\twhile (x > 2 * PI + eps) x -= 2 * PI;\n\treturn x;\n}\n\nint n;\nPoint center[N];\ndouble amax, radius[N], angle[N], vmax[N][2];\n\ndouble calc(double vstart, double vend, double dist)\n{\n\tdouble vm = sqrt((2 * amax * dist + dbl(vstart) + dbl(vend)) / 2.0);\n\tdouble ret = (2 * vm - vstart - vend) / amax;\n\treturn ret;\n}\n\nint main()\n{\n\tios::sync_with_stdio(false);\n\tcin >> n >> amax;\n\tfor (int i = 1; i <= n; ++i) {\n\t\tseg[i].read();\n\t\tseg[i].prepare();\n\t}\n\tfor (int i = 1; i < n; ++i) {\n\t\tstatic Point u, v;\n\t\tstatic double ang1, ang2;\n\t\t\n\t\tbool ret = intersect(seg[i].v, seg[i].v + seg[i].w, seg[i + 1].u, seg[i + 1].u + seg[i + 1].w, center[i]);\n\t\tif (!ret) center[i] = (seg[i].v + seg[i + 1].u) / 2.0;\n\n\t\tradius[i] = len(seg[i].v - center[i]);\n\t\tu = seg[i].v - center[i];\n\t\tv = seg[i + 1].u - center[i];\n\t\tang1 = atan2(u.y, u.x);\n\t\tang2 = atan2(v.y, v.x);\n\t\tif (sgn(det(seg[i].v - center[i], seg[i].v - seg[i].u)) > 0) {\n\t\t\tangle[i] = rectify(ang2 - ang1);\n\t\t} else {\n\t\t\tangle[i] = rectify(ang1 - ang2);\n\t\t}\n\t}\n\t/*\n\tfor (int i = 1; i < n; ++i) {\n\t\tcout << center[i].x << \" \" << center[i].y << \" \" << angle[i] << endl;\n\t}\n\t*/\n\tvmax[n][1] = 0.0;\n\tvmax[n][0] = sqrt(2 * amax * seg[n].dist);\n\t\n\tfor (int i = n - 1; i >= 1; --i) {\n\t\tvmax[i][1] = min(vmax[i + 1][0], sqrt(amax * radius[i]));\n\t\tvmax[i][0] = sqrt(dbl(vmax[i + 1][1]) + 2 * amax * seg[i].dist);\n\t}\n\n\tdouble times = 0;\n\tdouble v = 0;\n\n\tfor (int i = 1; i <= n; ++i) {\n\t\tif (v > vmax[i][1]) {\n\t\t\ttimes += calc(v, vmax[i][1], seg[i].dist);\n\t\t\tv = vmax[i][1];\n\t\t} else {\n\t\t\tif (dbl(v) + 2 * amax * seg[i].dist <= dbl(vmax[i][1])) {\n\t\t\t\ttimes += (sqrt(dbl(v) + 2 * amax * seg[i].dist) - v) / amax;\n\t\t\t\tv = sqrt(dbl(v) + 2 * amax * seg[i].dist);\n\t\t\t} else {\n\t\t\t\ttimes += calc(v, vmax[i][1], seg[i].dist);\n\t\t\t\tv = vmax[i][1];\n\t\t\t}\n\t\t}\n\t\tif (i < n) {\n\t\t\ttimes += radius[i] * angle[i] / v;\n\t\t}\n\t}\n\n\tcout << setiosflags(ios::fixed) << setprecision(8) << times << endl;\n\t\n\treturn 0;\n}", "accuracy": 0.96, "time_ms": 30, "memory_kb": 5388, "score_of_the_acc": -0.3684, "final_rank": 19 }, { "submission_id": "aoj_2226_1536476", "code_snippet": "#include <cstdio>\n#include <algorithm>\n#include <cmath>\n\nconst double PI = acos(-1.);\nconst double eps = 1e-8;\n\n__inline int sgn(const double &x, const double &eps = 1e-8) {\n return (x < -eps) ? -1 : (x > eps);\n}\n\nclass Point {\npublic:\n double x, y;\n\n Point() {}\n\n Point(const double &x, const double &y) : x(x), y(y) {}\n\t\n\tvoid read() {\n\t\tscanf(\"%lf%lf\", &x, &y);\n\t}\n\t\n\tdouble len() const {\n\t\treturn sqrt(x * x + y * y);\n\t}\n\t\n\tPoint norm() {\n\t\treturn Point(x / len(), y / len());\n\t}\n\n friend Point operator + (const Point &a, const Point &b) {\n return Point(a.x + b.x, a.y + b.y);\n }\n\n friend Point operator - (const Point &a, const Point &b) {\n return Point(a.x - b.x, a.y - b.y);\n }\n\n friend Point operator * (const Point &a, const double &k) {\n return Point(a.x * k, a.y * k);\n }\n\n friend Point operator * (const double &k, const Point &a) {\n return Point(a.x * k, a.y * k);\n }\n\n friend Point operator / (const Point &a, const double &k) {\n return Point(a.x / k, a.y / k);\n }\n\n friend bool operator == (const Point &a, const Point &b) {\n return sgn(a.x - b.x) == 0 && sgn(a.y - b.y) == 0;\n }\n\n friend bool operator < (const Point &a, const Point &b) {\n if (sgn(a.x - b.x) == 0) {\n return sgn(a.y - b.y) < 0;\n } else {\n return sgn(a.x - b.x) < 0;\n }\n }\n\n friend double det(const Point &a, const Point &b) {\n return a.x * b.y - a.y * b.x;\n }\n\n friend double det(const Point &a, const Point &b, const Point &c) {\n return det(b - a, c - a);\n }\n\n friend double dot(const Point &a, const Point &b) {\n return a.x * b.x + a.y * b.y;\n }\n\t\n friend double dot(const Point &a, const Point &b, const Point &c) {\n return dot(b - a, c - a);\n }\n\n\tfriend double dist(const Point &a, const Point &b) {\n\t\treturn (a - b).len();\n\t}\t\n\t\n friend bool check(const Point &a, const Point &b, const Point &c, const Point &d) {\n double s1 = det(a, b, c);\n double s2 = det(a, b, d);\n return sgn(s1 - s2) != 0;\n }\n\t\n friend Point intersect(const Point &a, const Point &b, const Point &c, const Point &d) {\n double s1 = det(a, b, c);\n double s2 = det(a, b, d);\n return (s1 * d - s2 * c) / (s1 - s2);\n }\n};\n\nbool point_on_line(const Point &p, const Point &a, const Point &b) {\n return sgn(det(p, a, b)) == 0 && sgn(dot(p, a, b)) <= 0;\n}\n\nconst int N = 44444;\n\nint n;\ndouble amax;\nPoint a[N], b[N];\ndouble pdist[N], radius[N], angle[N], vmax[N][2];\n\n__inline double fix(double x) {\n\tfor (; x < -eps; x += 2 * PI);\n\tfor (; x > 2 * PI - eps; x -= 2 * PI);\n\treturn x;\n}\n\n__inline std::pair<Point, Point> vertical(const Point &a, const Point &b) {\n\tPoint vec = b - a;\n\tPoint per = Point(-vec.y, vec.x);\n\treturn std::make_pair(a, a + per);\n}\nint check(double mid, double v0, double v, double d) {\n\tdouble dist = v0 * mid + amax * mid * mid / 2;\n\tif (dist > d) return 0;\n\tif (v0 + mid * amax < v) return 1; \n\tdouble need = (v0 + mid * amax - v) / amax;\n\tdouble nowv = v0 + mid * amax;\n\tif (nowv * need - .5 * need * need * amax + dist > d) return 0;\n\treturn 1;\n}\n\ndouble get(double v0, double v, double d) {\n\tdouble l = 0, r = sqrt(2 * d / amax) + 100;\n\tint counter = 40;\n\twhile (counter--) {\n\t\tdouble mid = (l + r) / 2;\n\t\tif (check(mid, v0, v, d)) l = mid;\n\t\telse r = mid;\n\t}\n\t\n\tdouble ret = (l + r) / 2;\n\tret += (v0 + (l + r) / 2 * amax - v) / amax;\n\treturn ret;\n}\n\nint main(void) {\n\tscanf(\"%d%lf\", &n, &amax);\n\tfor (int i = 0; i < n; ++i) {\n\t\ta[i].read();\n\t\tb[i].read();\n\t\tpdist[i] = dist(a[i], b[i]);\n\t}\n\t\n\tfor (int i = 0; i < n - 1; ++i) {\n\t\tstd::pair<Point, Point> pair1 = vertical(b[i], a[i]);\n\t\tstd::pair<Point, Point> pair2 = vertical(a[i + 1], b[i + 1]);\n\t\t\n\t\tPoint o;\n\t\tif (!check(pair1.first, pair1.second, pair2.first, pair2.second)) {\n\t\t\to = (b[i] + a[i + 1]) / 2;\n\t\t} else {\n\t\t\to = intersect(pair1.first, pair1.second, pair2.first, pair2.second);\n\t\t}\n\t\tradius[i] = dist(o, b[i]);\n\t\t\n\t\tdouble angle1 = atan2(b[i].y - o.y, b[i].x - o.x);\n\t\tdouble angle2 = atan2(a[i + 1].y - o.y, a[i + 1].x - o.x);\n\t\t\n\t\tif (sgn(det(b[i] - o, b[i] - a[i])) > 0) {\n\t\t\tangle[i] = fix(angle2 - angle1);\n\t\t} else {\n\t\t\tangle[i] = fix(angle1 - angle2);\n\t\t}\n\t}\n\t\n\tvmax[n - 1][0] = sqrt(2 * amax * pdist[n - 1]);\n\tvmax[n - 1][1] = 0;\n\t\n\tfor (int i = n - 2; i >= 0; --i) {\n\t\tvmax[i][1] = std::min(sqrt(radius[i] * amax), vmax[i + 1][0]);\n\t\tvmax[i][0] = sqrt(vmax[i][1] * vmax[i][1] + 2 * amax * pdist[i]);\n\t}\n\n\tdouble nowv = 0;\n\tdouble ans = 0;\n\t\n\tfor (int i = 0; i < n; i++) {\n\t\tif (nowv > vmax[i][1]) {\n\t\t\tdouble tmp = get(nowv, vmax[i][1], pdist[i]);\n\t\t\tans += tmp;\n\t\t\tnowv = vmax[i][1];\n\t\t}\n\t\telse {\t\n\t\t\tif (nowv * nowv + 2 * amax * pdist[i] < vmax[i][1] * vmax[i][1]) {\n\t\t\t\tdouble tmp = sqrt(nowv * nowv + 2 * amax * pdist[i]);\n\t\t\t\tans += (tmp - nowv) / amax;\n\t\t\t\tnowv = tmp;\n\t\t\t} else {\n\t\t\t\tdouble tmp = get(nowv, vmax[i][1], pdist[i]);\n\t\n\t\t\t\tans += tmp;\n\t\t\t\tnowv = vmax[i][1];\n\t\t\t}\n\t\t}\n\t\tif (i < n - 1) {\n\t\t\tans += radius[i] * angle[i] / nowv;\n\t\t}\t\t\n\t}\n\t\n\tprintf(\"%0.9f\\n\", ans);\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 3944, "score_of_the_acc": -0.4814, "final_rank": 10 }, { "submission_id": "aoj_2226_1536209", "code_snippet": "#include <cstdio>\n#include <cstdlib>\n#include <ctime>\n#include <cstring>\n#include <cmath>\n#include <algorithm>\n\nusing namespace std;\n\nconst double eps = 1e-9;\nconst double inf = 9999999.0;\n\nconst double pi = acos(-1.0);\nstruct point\n{\n\tdouble x, y;\n\tpoint() {}\n\tpoint(double x, double y) : x(x), y(y) {}\n};\n\npoint operator + (const point& p1, const point& p2) { return point(p1.x + p2.x, p1.y + p2.y); }\npoint operator - (const point& p1, const point& p2) { return point(p1.x - p2.x, p1.y - p2.y); }\npoint operator * (const point& p1, double d) { return point(p1.x * d, p1.y * d); }\npoint operator / (const point& p1, double d) { return point(p1.x / d, p1.y / d); }\n\ndouble sqr(double x) { return x * x; }\ndouble dist(point p1, point p2) { return sqrt(sqr(p1.x - p2.x) + sqr(p1.y - p2.y)); }\ndouble dot(point p1, point p2) { return p1.x * p2.x + p1.y * p2.y; }\ndouble det(point p1, point p2) { return p1.x * p2.y - p1.y * p2.x; }\nint dcmp(double x) { return fabs(x) <= eps ? 0 : (x > 0 ? 1 : -1); }\n\npoint rot(point p) { return point(-p.y, p.x); }\npoint mid(point p, point q) { return (p + q) / 2.0; }\npoint inter(point p, point v, point q, point w)\n{\n\tpoint u = p - q;\n\tdouble t = det(w, u) / det(v, w);\n\treturn p + v * t;\n}\n\ndouble calc(double t, double v0, double v1, double a)\n{\n\tdouble x = (v1 - v0 + a * t) / (2 * a);\n\tdouble vm = v0 + a * x;\n\treturn (v0 + vm) * x / 2.0 + (vm + v1) * (t - x) / 2.0; \n}\n\ndouble find(double v1, double v2, double L, double a)\n{\n\tdouble l = fabs(v1 - v2) / a, r = inf;\n\tfor (int i = 1; i <= 100; i ++)\n\t{\n\t\tdouble mid = (l + r) / 2.0;\n\t\tif (calc(mid, v1, v2, a) <= L) l = mid;\n\t\telse r = mid;\n\t}\n\treturn l;\n}\n\nint n;\ndouble amax;\n\npoint a[100001], b[100001];\ndouble theta[100001], r[100001], l[100001];\ndouble vr[100001];\ndouble len[100001], rad[100001];\ndouble ans = 0.0;\n\nint main( )\n{\n\tscanf(\"%d\", &n);\n\tscanf(\"%lf\", &amax);\n\tfor (int i = 1; i <= n; i ++)\n\t{\n\t\tscanf(\"%lf %lf %lf %lf\", &a[i].x, &a[i].y, &b[i].x, &b[i].y);\n\t\tlen[i] = dist(a[i], b[i]);\n\t}\n\tfor (int i = 1; i < n; i ++)\n\t{\n\t\tpoint tmpx = b[i], tmpy = a[i + 1];\n\t\tpoint p = mid(tmpx, tmpy), rev = rot(tmpy - tmpx);\n\t\t\n\t\tpoint o = inter(p, rev, b[i], rot(a[i] - b[i]));\n\t\trad[i] = dist(b[i], o);\n\t\ttmpx = b[i] - a[i];\n\t\ttmpy = b[i + 1] - a[i + 1];\n\t\ttheta[i] = atan2(tmpy.y, tmpy.x) - atan2(tmpx.y, tmpx.x);\n\t\tif (dcmp(theta[i]) < 0) theta[i] += 2 * pi;\n\t\tif (dcmp(det(b[i] - a[i], a[i + 1] - a[i])) < 0) theta[i] = 2.0 * pi - theta[i];\n\t\tdouble vmax = sqrt(amax * rad[i]);\n\t\tr[i] = vmax, l[i + 1] = vmax;\n\t}\n\tl[1] = 0, r[n] = 0;\n\tfor (int i = n - 1; i >= 1; i --)\n\t\tr[i] = min(r[i], sqrt(2.0 * len[i + 1] * amax + sqr(r[i + 1])));\n\tfor (int i = 2; i <= n; i ++)\n\t\tl[i] = sqrt(2.0 * len[i] * amax + sqr(r[i]));\n\tdouble now = 0.0; \n\tfor (int i = 1; i <= n; i ++)\n\t{\n\t\tans += find(now, r[i], len[i], amax);\n\t\tnow = min(sqrt(sqr(now) + 2.0 * len[i] * amax), r[i]);\n\t\tif (i != n) ans += theta[i] * rad[i] / now;\n\t}\n\tprintf(\"%.9f\\n\", ans);\n\treturn 0;\n}\n// a = sqr(v) / r;\n// r[i] maxv enter the seg\n// l[i]", "accuracy": 1, "time_ms": 100, "memory_kb": 6468, "score_of_the_acc": -1.2279, "final_rank": 14 }, { "submission_id": "aoj_2226_1533746", "code_snippet": "#include <cstdio>\n#include <cmath>\n#include <algorithm>\n#include <queue>\n#include <vector>\n#include <iostream>\n#include <cstring>\n\nusing namespace std;\n\nconst long double eps = 1e-8;\nconst long double pi = acos(-1.0);\nconst int maxn = 4e4 + 100;\nlong double am;\nint n;\nint dcmp(long double x){\n\treturn x < -eps? -1 : x > eps;\n}\n\nlong double sqr(long double x){\n\treturn x * x;\n}\n\nstruct point{\n\tlong double x, y;\n\tpoint(long double x = 0, long double y = 0):x(x),y(y){}\n\tvoid read(){\n\t\tscanf(\"%Lf%Lf\",&x,&y);\n\t}\n\tpoint operator + (const point &b){\n\t\treturn point(x + b.x, y + b.y);\n\t}\n\tpoint operator - (const point &b){\n\t\treturn point(x - b.x, y - b.y);\n\t}\n\tpoint operator * (long double a){\n\t\treturn point(x * a, y * a);\n\t}\n\tpoint operator / (long double a){\n\t\treturn point(x / a, y / a);\n\t}\n\tbool operator == (const point &b){\n\t\treturn dcmp(x - b.x)==0 && dcmp(y - b.y)==0;\n\t}\n\t\n};\n\npoint a[maxn],b[maxn];\npoint a1[maxn],b1[maxn];\nlong double arclen[maxn];\nlong double vmax[maxn],vc[maxn];\nlong double r[maxn];\nlong double angle[maxn];\n\nlong double dot(point a, point b){\n\treturn a.x*b.x + a.y*b.y;\n}\nlong double len2(point a){\n\treturn dot(a,a);\n}\nlong double len(point a){\n\treturn sqrt(len2(a));\n}\nlong double cross(point a, point b){\n\treturn a.x*b.y - a.y*b.x;\n}\npoint getlineinter(point p, point v, point q, point w){\n\tpoint u = p - q;\n\tlong double t = cross(w,u) / cross(v,w);\n\treturn p + v*t;\n}\npoint rotate(point a){\n\treturn point (-a.y, a.x);\n}\nbool rightside(point a1, point b1, point a2){\n\tpoint v1 = b1 - a1;\n\tpoint v2 = a2 - a1;\n\tif (cross(v2,v1) > 0)return 1;\n\telse return 0;\n}\npair<long double, long double> gettime(long double len, long double v1, long double v2)\n{\n\tif (dcmp(2*am*len + sqr(v1) - sqr(v2)) <= 0){\n\t\tlong double v = sqrt(2*am*len + sqr(v1));\n\t\tlong double t = (v - v1)/am;\n\t\treturn make_pair(v,t);\n\t}\n\tif (dcmp(2*am*len + sqr(v2) - sqr(v1)) == 0)return make_pair(v2, (v1 - v2)/am);\n\tlong double v = sqrt(am*len + (sqr(v1) + sqr(v2))/2);\n\tlong double t = (2*v - v1 - v2)/am;\n\t//cout<<\"@@\"<<endl;\n\treturn make_pair(v2,t);\n}\n\nvoid work(int i, int j)\n{\n\tpoint v1 = rotate(b[i] - a[i]);\n\tpoint v2 = rotate(b[j] - a[j]);\n\tpoint c;\n\tif (dcmp(cross(v1,v2))==0){\n\t\tc = (b[i] + a[j])/2;\n\t}else{\t\n\tc = getlineinter(b[i], v1, a[j], v2);\n\t}\n\tr[i] = len(c - b[i]);\n\t\n\t//cout<<r[i]<<endl;\n\n\tv1 = b[i] - a[i];\n\tv2 = b[j] - a[j];\n\tlong double a2 = atan2(v2.y, v2.x);\n\tlong double a1 = atan2(v1.y, v1.x);\n\tlong double tmp = a2 - a1;\n\tif (tmp < 0)tmp += 2 * pi;\n\tif (rightside(a[i],b[i], a[j]))\n\t\ttmp = 2 * pi - tmp;\n\tangle[i] = tmp;\n\t//r[i] = fabs(len(b[i] - a[j])/2/sin(angle[i]/2));\n\tvc[i] = sqrt(am * r[i]);\n\t\n\t\n}\nlong double gettime2(long double v, long double angle, long double r)\n{\n\treturn (r * angle)/v;\t\n}\n\nint main()\n{\n\tscanf(\"%d%Lf\",&n,&am);\n\tfor (int i = 0; i < n; ++i)\n\t{\n\t\ta1[i].read();\n\t\tb1[i].read();\n\t}\n\tint rear = 1;\n\ta[0] = a1[0];\n\tb[0] = b1[0];\t\n\tfor (int i = 1; i < n; ++i)\n\tif(b[rear] == a1[i]){\n\t\tb[rear] = b1[i];\n\t}else{\n\t\ta[rear] = a1[i];\n\t\tb[rear++] = b1[i];\n\t}\n\tn = rear;\n\t\n\tfor (int i = 0; i < n-1; ++i)\n\t{\n\t\twork(i,i+1);\n\t}\t\n\tvmax[n-1] = 0;\n\tfor (int i = n - 2; i >= 0; --i)\n\t{\n\t\tvmax[i] = min(vc[i], sqrt(2 * am * len(b[i+1] - a[i+1]) + sqr(vmax[i+1])));\t\n\t\t//cout<<i<<\":\"<<vmax[i]<<endl;\t\n\t}\n\tlong double ans = 0;\n\tlong double cur = 0;\n\tfor (int i = 0; i < n; ++i)\n\t{\n\t\t//\n\t\tpair<long double, long double> tmp = gettime(len(b[i] - a[i]), cur, vmax[i]);\n\t\tcur = tmp.first;\n\t\t//cout<<\"cur:\"<<cur<<endl;\n\t\tans += tmp.second;\n\t\t//cout<<tmp.second<<endl;\n\t\tif (i != n-1){\n\t\t\tlong double ttt = gettime2(cur, angle[i],r[i]);\n\t\t\t//cout<<\"time2:\"<<ttt<<endl;\n\t\t\t//cout<<\"angle:\"<<angle[i]<<endl;\n\t\t\tans += gettime2(cur, angle[i],r[i]);\n\t\t}\t\t\n\t\t//cout<<\"###\"<<ans<<endl;\n\t}\n\tprintf(\"%.12Lf\\n\",ans);\n\t\n\treturn 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 9408, "score_of_the_acc": -0.6726, "final_rank": 12 }, { "submission_id": "aoj_2226_1533639", "code_snippet": "#include <cstdio>\n#include <cmath>\n#include <algorithm>\n#include <queue>\n#include <vector>\n#include <iostream>\n#include <cstring>\n\nusing namespace std;\n\nconst double eps = 1e-11;\nconst double pi = acos(-1.0);\nconst int maxn = 4e4 + 100;\ndouble am;\nint n;\nint dcmp(double x){\n\treturn x < -eps? -1 : x > eps;\n}\n\ndouble sqr(double x){\n\treturn x * x;\n}\n\nstruct point{\n\tdouble x, y;\n\tpoint(double x = 0, double y = 0):x(x),y(y){}\n\tvoid read(){\n\t\tscanf(\"%lf%lf\",&x,&y);\n\t}\n\tpoint operator + (const point &b){\n\t\treturn point(x + b.x, y + b.y);\n\t}\n\tpoint operator - (const point &b){\n\t\treturn point(x - b.x, y - b.y);\n\t}\n\tpoint operator * (double a){\n\t\treturn point(x * a, y * a);\n\t}\n\tpoint operator / (double a){\n\t\treturn point(x / a, y / a);\n\t}\n\tbool operator == (const point &b){\n\t\treturn dcmp(x - b.x)==0 && dcmp(y - b.y)==0;\n\t}\n\t\n};\n\npoint a[maxn],b[maxn];\npoint a1[maxn],b1[maxn];\ndouble arclen[maxn];\ndouble vmax[maxn],vc[maxn];\ndouble r[maxn];\ndouble angle[maxn];\n\ndouble dot(point a, point b){\n\treturn a.x*b.x + a.y*b.y;\n}\ndouble len2(point a){\n\treturn dot(a,a);\n}\ndouble len(point a){\n\treturn sqrt(len2(a));\n}\ndouble cross(point a, point b){\n\treturn a.x*b.y - a.y*b.x;\n}\npoint getlineinter(point p, point v, point q, point w){\n\tpoint u = p - q;\n\tdouble t = cross(w,u) / cross(v,w);\n\treturn p + v*t;\n}\npoint rotate(point a){\n\treturn point (-a.y, a.x);\n}\nbool rightside(point a1, point b1, point a2){\n\tpoint v1 = b1 - a1;\n\tpoint v2 = a2 - a1;\n\tif (cross(v2,v1) > 0)return 1;\n\telse return 0;\n}\npair<double, double> gettime(double len, double v1, double v2)\n{\n\tif (dcmp(2*am*len + sqr(v1) - sqr(v2)) <= 0){\n\t\tdouble v = sqrt(2*am*len + sqr(v1));\n\t\tdouble t = (v - v1)/am;\n\t\treturn make_pair(v,t);\n\t}\n\tif (dcmp(2*am*len + sqr(v2) - sqr(v1)) == 0)return make_pair((v2 - v1)/am, v2);\n\tdouble v = sqrt(am*len + (sqr(v1) + sqr(v2))/2);\n\tdouble t = (2*v - v1 - v2)/am;\n\t//cout<<\"@@\"<<endl;\n\treturn make_pair(v2,t);\n}\n\nvoid work(int i, int j)\n{\n\tpoint v1 = rotate(b[i] - a[i]);\n\tpoint v2 = rotate(b[j] - a[j]);\n\tpoint c;\n\tif (dcmp(cross(v1,v2))==0){\n\t\tc = (b[i] + a[j])/2;\n\t}else{\t\n\tc = getlineinter(b[i], v1, a[j], v2);\n\t}\n\tr[i] = len(c - b[i]);\n\t\n\t//cout<<r[i]<<endl;\n\n\tv1 = b[i] - a[i];\n\tv2 = b[j] - a[j];\n\tdouble a2 = atan2(v2.y, v2.x);\n\tdouble a1 = atan2(v1.y, v1.x);\n\tdouble tmp = a2 - a1;\n\tif (tmp < 0)tmp += 2 * pi;\n\tif (rightside(a[i],b[i], a[j]))\n\t\ttmp = 2 * pi - tmp;\n\tangle[i] = tmp;\n\tr[i] = fabs(len(b[i] - a[j])/2/sin(angle[i]/2));\n\tvc[i] = sqrt(am * r[i]);\n\t\n\t\n}\ndouble gettime2(double v, double angle, double r)\n{\n\treturn (r * angle)/v;\t\n}\n\nint main()\n{\n\tscanf(\"%d%lf\",&n,&am);\n\tfor (int i = 0; i < n; ++i)\n\t{\n\t\ta1[i].read();\n\t\tb1[i].read();\n\t}\n\tint rear = 1;\n\ta[0] = a1[0];\n\tb[0] = b1[0];\t\n\tfor (int i = 1; i < n; ++i)\n\tif(b[rear] == a1[i]){\n\t\tb[rear] = b1[i];\n\t}else{\n\t\ta[rear] = a1[i];\n\t\tb[rear++] = b1[i];\n\t}\n\tn = rear;\n\t\n\tfor (int i = 0; i < n-1; ++i)\n\t{\n\t\twork(i,i+1);\n\t}\t\n\tvmax[n-1] = 0;\n\tfor (int i = n - 2; i >= 0; --i)\n\t{\n\t\tvmax[i] = min(vc[i], sqrt(2 * am * len(b[i+1] - a[i+1]) + sqr(vmax[i+1])));\t\n\t\t//cout<<i<<\":\"<<vmax[i]<<endl;\t\n\t}\n\tdouble ans = 0;\n\tdouble cur = 0;\n\tfor (int i = 0; i < n; ++i)\n\t{\n\t\t//\n\t\tpair<double, double> tmp = gettime(len(b[i] - a[i]), cur, vmax[i]);\n\t\tcur = tmp.first;\n\t\t//cout<<\"cur:\"<<cur<<endl;\n\t\tans += tmp.second;\n\t\t//cout<<tmp.second<<endl;\n\t\tif (i != n-1){\n\t\t\tdouble ttt = gettime2(cur, angle[i],r[i]);\n\t\t\t//cout<<\"time2:\"<<ttt<<endl;\n\t\t\t//cout<<\"angle:\"<<angle[i]<<endl;\n\t\t\tans += gettime2(cur, angle[i],r[i]);\n\t\t}\t\t\n\t\t//cout<<\"###\"<<ans<<endl;\n\t}\n\tprintf(\"%.12f\\n\",ans);\n\t\n\treturn 0;\n}", "accuracy": 0.96, "time_ms": 20, "memory_kb": 5044, "score_of_the_acc": -0.2313, "final_rank": 18 }, { "submission_id": "aoj_2226_1533622", "code_snippet": "#include <cstdio>\n#include <cmath>\n#include <algorithm>\n#include <queue>\n#include <vector>\n#include <iostream>\n#include <cstring>\n\nusing namespace std;\n\nconst double eps = 1e-11;\nconst double pi = acos(-1.0);\nconst int maxn = 4e4 + 100;\ndouble am;\nint n;\nint dcmp(double x){\n\treturn x < -eps? -1 : x > eps;\n}\n\ndouble sqr(double x){\n\treturn x * x;\n}\n\nstruct point{\n\tdouble x, y;\n\tpoint(double x = 0, double y = 0):x(x),y(y){}\n\tvoid read(){\n\t\tscanf(\"%lf%lf\",&x,&y);\n\t}\n\tpoint operator + (const point &b){\n\t\treturn point(x + b.x, y + b.y);\n\t}\n\tpoint operator - (const point &b){\n\t\treturn point(x - b.x, y - b.y);\n\t}\n\tpoint operator * (double a){\n\t\treturn point(x * a, y * a);\n\t}\n\tpoint operator / (double a){\n\t\treturn point(x / a, y / a);\n\t}\n\tbool operator == (const point &b){\n\t\treturn dcmp(x - b.x)==0 && dcmp(y - b.y)==0;\n\t}\n\t\n};\n\npoint a[maxn],b[maxn];\npoint a1[maxn],b1[maxn];\ndouble arclen[maxn];\ndouble vmax[maxn],vc[maxn];\ndouble r[maxn];\ndouble angle[maxn];\n\ndouble dot(point a, point b){\n\treturn a.x*b.x + a.y*b.y;\n}\ndouble len2(point a){\n\treturn dot(a,a);\n}\ndouble len(point a){\n\treturn sqrt(len2(a));\n}\ndouble cross(point a, point b){\n\treturn a.x*b.y - a.y*b.x;\n}\npoint getlineinter(point p, point v, point q, point w){\n\tpoint u = p - q;\n\tdouble t = cross(w,u) / cross(v,w);\n\treturn p + v*t;\n}\npoint rotate(point a){\n\treturn point (-a.y, a.x);\n}\nbool rightside(point a1, point b1, point a2){\n\tpoint v1 = b1 - a1;\n\tpoint v2 = a2 - a1;\n\tif (cross(v2,v1) > 0)return 1;\n\telse return 0;\n}\npair<double, double> gettime(double len, double v1, double v2)\n{\n\tif (dcmp(2*am*len + sqr(v1) - sqr(v2)) <= 0){\n\t\tdouble v = sqrt(2*am*len + sqr(v1));\n\t\tdouble t = (v - v1)/am;\n\t\treturn make_pair(v,t);\n\t}\n\tif (dcmp(2*am*len + sqr(v2) - sqr(v1)) == 0)return make_pair((v2 - v1)/am, v2);\n\tdouble v = sqrt(am*len + (sqr(v1) + sqr(v2))/2);\n\tdouble t = (2*v - v1 - v2)/am;\n\t//cout<<\"@@\"<<endl;\n\treturn make_pair(v2,t);\n}\n\nvoid work(int i, int j)\n{\n\tpoint v1 = rotate(b[i] - a[i]);\n\tpoint v2 = rotate(b[j] - a[j]);\n\tpoint c;\n\tif (dcmp(cross(v1,v2))==0){\n\t\tc = (b[i] + a[j])/2;\n\t}else{\t\n\tc = getlineinter(b[i], v1, a[j], v2);\n\t}\n\tr[i] = len(c - b[i]);\n\t\n\t//cout<<r[i]<<endl;\n\tvc[i] = sqrt(am * r[i]);\n\tv1 = b[i] - a[i];\n\tv2 = b[j] - a[j];\n\tdouble a2 = atan2(v2.y, v2.x);\n\tdouble a1 = atan2(v1.y, v1.x);\n\tdouble tmp = a2 - a1;\n\tif (tmp < 0)tmp += 2 * pi;\n\tif (rightside(a[i],b[i], a[j]))\n\t\ttmp = 2 * pi - tmp;\n\tangle[i] = tmp;\t\n}\ndouble gettime2(double v, double angle, double r)\n{\n\treturn (r * angle)/v;\t\n}\n\nint main()\n{\n\tscanf(\"%d%lf\",&n,&am);\n\tfor (int i = 0; i < n; ++i)\n\t{\n\t\ta1[i].read();\n\t\tb1[i].read();\n\t}\n\tint rear = 1;\n\ta[0] = a1[0];\n\tb[0] = b1[0];\t\n\tfor (int i = 1; i < n; ++i)\n\tif(b[rear] == a1[i]){\n\t\tb[rear] = b1[i];\n\t}else{\n\t\ta[rear] = a1[i];\n\t\tb[rear++] = b1[i];\n\t}\n\tn = rear;\n\t\n\tfor (int i = 0; i < n-1; ++i)\n\t{\n\t\twork(i,i+1);\n\t}\t\n\tvmax[n-1] = 0;\n\tfor (int i = n - 2; i >= 0; --i)\n\t{\n\t\tvmax[i] = min(vc[i], sqrt(2 * am * len(b[i+1] - a[i+1]) + sqr(vmax[i+1])));\t\n\t\t//cout<<i<<\":\"<<vmax[i]<<endl;\t\n\t}\n\tdouble ans = 0;\n\tdouble cur = 0;\n\tfor (int i = 0; i < n; ++i)\n\t{\n\t\t//\n\t\tpair<double, double> tmp = gettime(len(b[i] - a[i]), cur, vmax[i]);\n\t\tcur = tmp.first;\n\t\t//cout<<\"cur:\"<<cur<<endl;\n\t\tans += tmp.second;\n\t\t//cout<<tmp.second<<endl;\n\t\tif (i != n-1){\n\t\t\tdouble ttt = gettime2(cur, angle[i],r[i]);\n\t\t\t//cout<<\"time2:\"<<ttt<<endl;\n\t\t\t//cout<<\"angle:\"<<angle[i]<<endl;\n\t\t\tans += gettime2(cur, angle[i],r[i]);\n\t\t}\t\t\n\t\t//cout<<\"###\"<<ans<<endl;\n\t}\n\tprintf(\"%.12f\\n\",ans);\n\t\n\treturn 0;\n}", "accuracy": 0.96, "time_ms": 20, "memory_kb": 5024, "score_of_the_acc": -0.2298, "final_rank": 16 }, { "submission_id": "aoj_2226_1533609", "code_snippet": "#include <cstdio>\n#include <cmath>\n#include <algorithm>\n#include <queue>\n#include <vector>\n#include <iostream>\n#include <cstring>\n\nusing namespace std;\n\nconst double eps = 1e-9;\nconst double pi = acos(-1.0);\nconst int maxn = 4e4 + 100;\ndouble am;\nint n;\nint dcmp(double x){\n\treturn x < -eps? -1 : x > eps;\n}\n\ndouble sqr(double x){\n\treturn x * x;\n}\n\nstruct point{\n\tdouble x, y;\n\tpoint(double x = 0, double y = 0):x(x),y(y){}\n\tvoid read(){\n\t\tscanf(\"%lf%lf\",&x,&y);\n\t}\n\tpoint operator + (const point &b){\n\t\treturn point(x + b.x, y + b.y);\n\t}\n\tpoint operator - (const point &b){\n\t\treturn point(x - b.x, y - b.y);\n\t}\n\tpoint operator * (double a){\n\t\treturn point(x * a, y * a);\n\t}\n\tpoint operator / (double a){\n\t\treturn point(x / a, y / a);\n\t}\n\tbool operator == (const point &b){\n\t\treturn dcmp(x - b.x)==0 && dcmp(y - b.y)==0;\n\t}\n\t\n};\n\npoint a[maxn],b[maxn];\npoint a1[maxn],b1[maxn];\ndouble arclen[maxn];\ndouble vmax[maxn],vc[maxn];\ndouble r[maxn];\ndouble angle[maxn];\n\ndouble dot(point a, point b){\n\treturn a.x*b.x + a.y*b.y;\n}\ndouble len2(point a){\n\treturn dot(a,a);\n}\ndouble len(point a){\n\treturn sqrt(len2(a));\n}\ndouble cross(point a, point b){\n\treturn a.x*b.y - a.y*b.x;\n}\npoint getlineinter(point p, point v, point q, point w){\n\tpoint u = p - q;\n\tdouble t = cross(w,u) / cross(v,w);\n\treturn p + v*t;\n}\npoint rotate(point a){\n\treturn point (-a.y, a.x);\n}\nbool rightside(point a1, point b1, point a2){\n\tpoint v1 = b1 - a1;\n\tpoint v2 = a2 - a1;\n\tif (cross(v2,v1) > 0)return 1;\n\telse return 0;\n}\npair<double, double> gettime(double len, double v1, double v2)\n{\n\tif (dcmp(2*am*len + sqr(v1) - sqr(v2)) <= 0){\n\t\tdouble v = sqrt(2*am*len + sqr(v1));\n\t\tdouble t = (v - v1)/am;\n\t\treturn make_pair(v,t);\n\t}\n\tif (dcmp(2*am*len + sqr(v2) - sqr(v1)) == 0)return make_pair((v2 - v1)/am, v2);\n\tdouble v = sqrt(am*len + (sqr(v1) + sqr(v2))/2);\n\tdouble t = (2*v - v1 - v2)/am;\n\t//cout<<\"@@\"<<endl;\n\treturn make_pair(v2,t);\n}\n\nvoid work(int i, int j)\n{\n\tpoint v1 = rotate(b[i] - a[i]);\n\tpoint v2 = rotate(b[j] - a[j]);\n\tpoint c;\n\tif (dcmp(cross(v1,v2))==0){\n\t\tc = (b[i] + a[j])/2;\n\t}else{\t\n\tc = getlineinter(b[i], v1, a[j], v2);\n\t}\n\tr[i] = len(c - b[i]);\n\t\n\t//cout<<r[i]<<endl;\n\tvc[i] = sqrt(am * r[i]);\n\tv1 = b[i] - a[i];\n\tv2 = b[j] - a[j];\n\tdouble a2 = atan2(v2.y, v2.x);\n\tdouble a1 = atan2(v1.y, v1.x);\n\tdouble tmp = a2 - a1;\n\tif (tmp < 0)tmp += 2 * pi;\n\tif (rightside(a[i],b[i], a[j]))\n\t\ttmp = 2 * pi - tmp;\n\tangle[i] = tmp;\t\n}\ndouble gettime2(double v, double angle, double r)\n{\n\treturn (r * angle)/v;\t\n}\n\nint main()\n{\n\tscanf(\"%d%lf\",&n,&am);\n\tfor (int i = 0; i < n; ++i)\n\t{\n\t\ta1[i].read();\n\t\tb1[i].read();\n\t}\n\tint rear = 1;\n\ta[0] = a1[0];\n\tb[0] = b1[0];\t\n\tfor (int i = 1; i < n; ++i)\n\tif(b[rear] == a1[i]){\n\t\tb[rear] = b1[i];\n\t}else{\n\t\ta[rear] = a1[i];\n\t\tb[rear++] = b1[i];\n\t}\n\tn = rear;\n\t\n\tfor (int i = 0; i < n-1; ++i)\n\t{\n\t\twork(i,i+1);\n\t}\t\n\tvmax[n-1] = 0;\n\tfor (int i = n - 2; i >= 0; --i)\n\t{\n\t\tvmax[i] = min(vc[i], sqrt(2 * am * len(b[i+1] - a[i+1]) + sqr(vmax[i+1])));\t\n\t\t//cout<<i<<\":\"<<vmax[i]<<endl;\t\n\t}\n\tdouble ans = 0;\n\tdouble cur = 0;\n\tfor (int i = 0; i < n; ++i)\n\t{\n\t\t//\n\t\tpair<double, double> tmp = gettime(len(b[i] - a[i]), cur, vmax[i]);\n\t\tcur = tmp.first;\n\t\t//cout<<\"cur:\"<<cur<<endl;\n\t\tans += tmp.second;\n\t\t//cout<<tmp.second<<endl;\n\t\tif (i != n-1){\n\t\t\tdouble ttt = gettime2(cur, angle[i],r[i]);\n\t\t\t//cout<<\"time2:\"<<ttt<<endl;\n\t\t\t//cout<<\"angle:\"<<angle[i]<<endl;\n\t\t\tans += gettime2(cur, angle[i],r[i]);\n\t\t}\t\t\n\t\t//cout<<\"###\"<<ans<<endl;\n\t}\n\tprintf(\"%.12f\\n\",ans);\n\t\n\treturn 0;\n}", "accuracy": 0.96, "time_ms": 20, "memory_kb": 5024, "score_of_the_acc": -0.2298, "final_rank": 16 }, { "submission_id": "aoj_2226_1445423", "code_snippet": "#include <bits/stdc++.h>\n#define rep(i,n) for(int i=0;i<(int)(n);i++)\n#define rep1(i,n) for(int i=1;i<=(int)(n);i++)\n#define all(c) c.begin(),c.end()\n#define pb push_back\n#define fs first\n#define sc second\n#define show(x) cout << #x << \" = \" << x << endl\n#define chmin(x,y) x=min(x,y)\n#define chmax(x,y) x=max(x,y)\nusing namespace std;\ntypedef double D;\ntypedef complex<D> P;\ntypedef pair<P,P> L;\nint N;\nD acc;\nD pi=acos(-1);\nL ls[40000];\nD rs[40000];\nD ths[40000];\nD ub[40000];\nD cro(P a,P b){return imag(conj(a)*b);}\ninline P vec(L l){\n\treturn l.sc-l.fs;\n}\ninline D verlen(L l,P p){\n\treturn cro(vec(l),p-l.sc)/abs(vec(l));\n}\ninline P intLL(L a,L b){\n\tD t=cro(vec(a),a.sc-b.fs)/cro(vec(a),vec(b));\n\treturn b.fs+t*vec(b);\n}\ninline bool ispal(L a,L b){\n\treturn abs(cro(vec(a),vec(b)))<1e-9;\n}\nint main(){\n\tcin>>N>>acc;\n\trep(i,N){\n\t\tint d,e,f,g;\n\t\tcin>>d>>e>>f>>g;\n\t\tls[i]=L(P(d,e),P(f,g));\n\t}\n\trep(i,N-1){\n\t\tL a=L(ls[i].sc,ls[i].sc+vec(ls[i])*P(0,1));\n\t\tL b=L(ls[i+1].fs,ls[i+1].fs+vec(ls[i+1])*P(0,1));\n\t\tP p;\n\t\tif(ispal(a,b)) p=(ls[i].sc+ls[i+1].fs)/2.0;\n\t\telse p=intLL(a,b);\n\t\trs[i]=verlen(ls[i],p);\n\t\tths[i]=arg(vec(ls[i+1])/vec(ls[i]));\n\t\tif(rs[i]<0){\n\t\t\trs[i]=-rs[i];\n\t\t\tths[i]=-ths[i];\n\t\t}\n\t\tif(ths[i]<0) ths[i]+=2*pi;\n\t\tassert(0<ths[i]&&ths[i]<2*pi);\n\t}\n\tub[N-1]=0;\n\tfor(int i=N-1;i>0;i--){\n\t\tD len=abs(vec(ls[i]));\n\t\tub[i-1]=min(sqrt(ub[i]*ub[i]+2*acc*len),sqrt(rs[i-1]*acc));\n\t}\n\tD ans=0,v=0;\n\trep(i,N){\n\t\tD len=abs(vec(ls[i]));\n\t\tD mx=sqrt(v*v+2*acc*len);\n\t\tif(mx<=ub[i]){\n\t\t\tans+=(mx-v)/acc;\n\t\t\tv=mx;\n\t\t}else{\n\t\t\tif(ub[i]>v){\n\t\t\t\tans+=(ub[i]-v)/acc;\n\t\t\t\tlen=(len-(ub[i]*ub[i]-v*v)/2/acc)/2;\n\t\t\t\tv=ub[i];\n\t\t\t\tmx=sqrt(v*v+2*acc*len);\n\t\t\t\tans+=2*(mx-v)/acc;\n\t\t\t}else{\n\t\t\t\tans+=(v-ub[i])/acc;\n\t\t\t\tlen=(len-(v*v-ub[i]*ub[i])/2/acc)/2;\n\t\t\t\tmx=sqrt(v*v+2*acc*len);\n\t\t\t\tans+=2*(mx-v)/acc;\n\t\t\t\tv=ub[i];\n\t\t\t}\n\t\t}\n\t\tif(i==N-1) break;\n\t\tans+=ths[i]*rs[i]/v;\n\t}\n\tprintf(\"%.12f\\n\",ans);\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3456, "score_of_the_acc": -0.3333, "final_rank": 5 }, { "submission_id": "aoj_2226_1445369", "code_snippet": "#include <bits/stdc++.h>\n#define rep(i,n) for(int i=0;i<(int)(n);i++)\n#define rep1(i,n) for(int i=1;i<=(int)(n);i++)\n#define all(c) c.begin(),c.end()\n#define pb push_back\n#define fs first\n#define sc second\n#define show(x) cout << #x << \" = \" << x << endl\n#define chmin(x,y) x=min(x,y)\n#define chmax(x,y) x=max(x,y)\nusing namespace std;\ntypedef double D;\ntypedef complex<D> P;\ntypedef pair<P,P> L;\nint N;\nD acc;\nD pi=acos(-1);\nL ls[40000];\nD rs[40000];\nD ths[40000];\nD ub[40000];\nD cro(P a,P b){return imag(conj(a)*b);}\ninline P vec(L l){\n\treturn l.sc-l.fs;\n}\ninline D verlen(L l,P p){\n\treturn cro(vec(l),p-l.sc)/abs(vec(l));\n}\ninline P intLL(L a,L b){\n\tD t=cro(vec(a),a.sc-b.fs)/cro(vec(a),vec(b));\n\treturn b.fs+t*vec(b);\n}\ninline bool ispal(L a,L b){\n\treturn abs(cro(vec(a),vec(b)))<1e-9;\n}\nint main(){\n\tcin>>N>>acc;\n\trep(i,N){\n\t\tint d,e,f,g;\n\t\tcin>>d>>e>>f>>g;\n\t\tls[i]=L(P(d,e),P(f,g));\n\t}\n\trep(i,N-1){\n\t\tL a=L(ls[i].sc,ls[i].sc+vec(ls[i])*P(0,1));\n\t\tL b=L(ls[i+1].fs,ls[i+1].fs+vec(ls[i+1])*P(0,1));\n\t\tP p;\n\t\tif(ispal(a,b)) p=(ls[i].sc+ls[i+1].fs)/2.0;\n\t\telse p=intLL(a,b);\n\t\trs[i]=verlen(ls[i],p);\n\t\tths[i]=arg(vec(ls[i+1])/vec(ls[i]));\n\t\tif(rs[i]<0){\n\t\t\trs[i]=-rs[i];\n\t\t\tths[i]=-ths[i];\n\t\t}\n\t\tif(ths[i]<0) ths[i]+=2*pi;\n\t\tassert(0<ths[i]&&ths[i]<2*pi);\n\t}\n\tub[N-1]=0;\n\tfor(int i=N-1;i>0;i--){\n\t\tD len=abs(vec(ls[i]));\n\t\tub[i-1]=min(sqrt(ub[i]*ub[i]+2*acc*len),sqrt(rs[i-1]*acc));\n\t}\n\tD ans=0,v=0;\n\trep(i,N){\n\t\tD len=abs(vec(ls[i]));\n\t\tD mx=sqrt(v*v+2*acc*len);\n\t\tif(mx<=ub[i]){\n\t\t\tans+=(mx-v)/acc;\n\t\t\tv=mx;\n\t\t}else{\n\t\t\tif(ub[i]>v){\n\t\t\t\tans+=(ub[i]-v)/acc;\n\t\t\t\tlen=(len-(ub[i]-v)*(ub[i]-v)/2/acc)/2;\n\t\t\t\tv=ub[i];\n\t\t\t\tmx=sqrt(v*v+2*acc*len);\n\t\t\t\tans+=2*(mx-v)/acc;\n\t\t\t}else{\n\t\t\t\tans+=(v-ub[i])/acc;\n\t\t\t\tlen=(len-(ub[i]-v)*(ub[i]-v)/2/acc)/2;\n\t\t\t\tmx=sqrt(v*v+2*acc*len);\n\t\t\t\tans+=2*(mx-v)/acc;\n\t\t\t\tv=ub[i];\n\t\t\t}\n\t\t}\n\t\tif(i==N-1) break;\n\t\tans+=ths[i]*rs[i]/v;\n\t}\n\tprintf(\"%.12f\\n\",ans);\n}", "accuracy": 0.24, "time_ms": 40, "memory_kb": 3456, "score_of_the_acc": -0.3333, "final_rank": 20 }, { "submission_id": "aoj_2226_1175452", "code_snippet": "#include<cstdio>\n#include<complex>\n#include<cmath>\n#include<utility>\n#include<algorithm>\n\nusing namespace std;\n\ntypedef double Real;\n\nconst Real eps=1e-9;\nconst Real PI=acos(-1.0);\n\ntypedef pair<Real,Real> P;\ntypedef complex<Real> Point;\ntypedef complex<Real> Vector;\ntypedef pair<Point,Point> Segment;\n\ntemplate<class T> bool eq(T a,T b){\n\treturn abs(a-b)<eps;\n}\n\ntemplate<class T> int sgn(T a){\n\tif(eq(a,0.0)) return 0;\n\tif(a>0) return 1;\n\treturn -1;\n}\n\nReal doP(Vector a,Vector b){\n\treturn (conj(a)*b).real();\n}\n\nReal arg(Vector v){\n\treturn atan2(v.imag(),v.real());\n}\n\nReal getAng(Segment s1,Segment s2){\n\t//directed segments, res<=PI\n\tVector v1=s1.second-s1.first;\n\tVector v2=s2.second-s2.first;\n\tVector v=v2/v1;\n\tReal ang=arg(v);\n\tang=abs(ang);\n\tif(ang>PI) ang=PI*2-ang;\n//\tprintf(\"ang=%f\\n\",ang);\n\treturn ang;\n}\n\nP getCircle(Segment s1,Segment s2){\n\tReal theta=getAng(s1,s2);\n\tReal d=abs(s1.second-s2.first);\n\tReal radius=1.0/sin(theta/2)*d/2;\n//\tprintf(\"d=%f,radius=%f\\n\",d,radius);\n\tPoint p=s1.first,q=s1.second,r=s2.first;\n\tReal x=doP(p-q,r-q);\n\tif(sgn(x)>0){\n\t\ttheta=PI*2-theta;\n\t}\n\treturn P(radius,theta);\n}\n\nSegment segs[40400];\n\nReal ls[40400],Mv[40400];\nReal rs[40400],angs[40400];\nint N;\n\nReal Ma;\n\nvoid input(){\n\tscanf(\"%d%lf\",&N,&Ma);\n\tfor(int i=0;i<N;i++){\n\t\tdouble x1,y1,x2,y2;\n\t\tscanf(\"%lf%lf%lf%lf\",&x1,&y1,&x2,&y2);\n\t\tPoint p1=Point(x1,y1);\n\t\tPoint p2=Point(x2,y2);\n\t\tsegs[i]=Segment(p1,p2);\n\t}\n}\n\nP solveLine(Real v0,Real x,Real Mlast){\n//\tprintf(\"solve %f %f %f\\n\",v0,x,Mlast);\n\tReal lb=0,ub=x;\n\tP res;\n\tbool ok=false;\n\tfor(int stage=0;stage<100;stage++){\n\t\tReal mid=(ub+lb)/2;\n\t\tReal m=sqrt(v0*v0+2.0*Ma*mid);\n\t\tReal t=(m-v0)/Ma;\n\t\tReal u2=m*m-2.0*Ma*(x-mid);\n\t\tif(u2<-eps){\n\t\t\tlb=mid;\n\t\t\tcontinue;\n\t\t}\n\t\tu2=max(u2,0.0);\n\t\tReal u=sqrt(u2);\n\t\tReal t2=(m-u)/Ma;\n\t\tif(sgn(Mlast-u)>=0){\n\t\t\tres=P(u,t+t2);\n\t\t\tlb=mid;\n\t\t\tok=true;\n\t\t}else{\n\t\t\tub=mid;\n\t\t}\n\t}\n\tif(ok==false){\n\t\tReal t=(v0-Mlast)/Ma;\n\t\treturn P(Mlast,t);\n\t}\n\treturn res;\n}\n\nReal solve(){\n\tfor(int i=0;i<N-1;i++){\n\t\tP p=getCircle(segs[i],segs[i+1]);\n\t\trs[i]=p.first;\n\t\tangs[i]=p.second;\n\t\tMv[i]=sqrt(Ma*rs[i]);\n//\t\tprintf(\"r=%f,ang=%f,mv=%f\\n\",rs[i],angs[i],Mv[i]);\n\t}\n\tfor(int i=0;i<N;i++){\n\t\tls[i]=abs(segs[i].second-segs[i].first);\n\t}\n\tMv[N-1]=0;\n\tfor(int i=N-1;i>=1;i--){\n\t\tReal val=2.0*Ma*ls[i];\n\t\tReal v0=Mv[i]*Mv[i]+val;\n\t\tv0=sqrt(v0);\n\t\tMv[i-1]=min(Mv[i-1],v0);\n\t}\n\tReal t=0;\n\tReal prevv=0;\n\tfor(int i=0;i<N;i++){\n\t\tP p=solveLine(prevv,ls[i],Mv[i]);\n\t\tt+=p.second;\n\t\tprevv=p.first;\n\t\tif(isinf(t)){\n\t\t\tprintf(\"%f %f %f\\n\",prevv,ls[i],Mv[i]);\n\t\t}\n//\t\tprintf(\"t+=%f\\n\",p.second);\n\t\tif(i!=N-1){\n\t\t\tReal len=angs[i]*rs[i];\n//\t\t\tprintf(\"ang=%f,r=%f,v=%f\\n\",angs[i],rs[i],prevv);\n\t\t\tReal t2=len/prevv;\n\t\t\tt+=t2;\n\t\t\tif(isinf(t)){\n\t\t\t\tprintf(\"ang=%f,r=%f,v=%f Mv=%f\\n\",angs[i],rs[i],prevv,Mv[i]);\n\t\t\t\treturn 0;\n\t\t\t}\n\t\t}\n\t}\n\treturn t;\n}\n\nint main(){\n\tinput();\n\tReal ans=solve();\n\tprintf(\"%.9f\\n\",ans);\n\treturn 0;\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 3668, "score_of_the_acc": -1.016, "final_rank": 13 } ]
aoj_2230_cpp
Problem I: How to Create a Good Game A video game company called ICPC (International Company for Playing and Competing) is now developing a new arcade game. The new game features a lot of branches. This makes the game enjoyable for everyone, since players can choose their routes in the game depending on their skills. Rookie players can choose an easy route to enjoy the game, and talented players can choose their favorite routes to get high scores. In the game, there are many checkpoints connected by paths. Each path consists of several stages, and completing stages on a path leads players to the next checkpoint. The game ends when players reach a particular checkpoint. At some checkpoints, players can choose which way to go, so routes diverge at that time. Sometimes different routes join together at a checkpoint. The paths between checkpoints are directed, and there is no loop (otherwise, players can play the game forever). In other words, the structure of the game can be viewed as a DAG (directed acyclic graph), when considering paths between checkpoints as directed edges. Recently, the development team completed the beta version of the game and received feedbacks from other teams. They were quite positive overall, but there are some comments that should be taken into consideration. Some testers pointed out that some routes were very short compared to the longest ones. Indeed, in the beta version, the number of stages in one play can vary drastically depending on the routes. Game designers complained many brilliant ideas of theirs were unused in the beta version because of the tight development schedule. They wanted to have more stages included in the final product. However, it’s not easy to add more stages. this is an arcade game – if the playing time was too long, it would bring down the income of the game and owners of arcades would complain. So, the longest route of the final product can’t be longer than that of the beta version. Moreover, the producer of the game didn’t want to change the structure of paths (i.e., how the checkpoints connect to each other), since it would require rewriting the scenario, recording voices, creating new cutscenes, etc. Considering all together, the producer decided to add as many new stages as possible, while keeping the maximum possible number of stages in one play and the structure of paths unchanged. How many new stages can be added to the game? Input N M x 1 y 1 s 1 . . . x M y M s M The first line of the input contains two positive integers N and M (2 ≤ N ≤ 100, 1 ≤ M ≤ 1000). N indicates the number of checkpoints, including the opening and ending of the game. M indicates the number of paths between checkpoints. The following M lines describe the structure of paths in the beta version of the game. The i -th line contains three integers x i , y i and s i (0 ≤ x i < y i ≤ N - 1, 1 ≤ s i ≤ 1000), which describe that there is a path from checkpoint x i to y i consists of si stages. As for indices of the check ...(truncated)
[ { "submission_id": "aoj_2230_10850465", "code_snippet": "#include<cstdio>\n#include<cstring>\n#include<cmath>\n#include<algorithm>\n#define add(a,b,c) (e[i]=b,pre[i]=last[a],last[a]=i,w[i]=c)\n#define prec 1e-6\n#define inf 1e9\nusing namespace std;\nstruct edge\n{ int u,v,w;\n edge(int u=0,int v=0,int w=0):u(u),v(v),w(w) {}\n};\nstruct simplex\n{ double v;\n double a[1205][1205];\n double b[1205];\n double c[1205];\n int basic[1205];\n int nbasic[105];\n void pivot(int l,int e);\n double opt();\n void output();\n};\nint fcmp(double x)\n{ if (fabs(x)<prec)\n return 0;\n return x>0?1:-1;\n}\nedge ed[1005];\nint last[1005];\nint pre[2005];\nint deg[105];\nint e[2005];\nint w[2005];\nint f[105];\nint n,m;\nsimplex lp;\nvoid simplex::output()\n{ int i,j;\n for (i=1;i<=m;++i)\n { printf(\"x[%d]=%.2f-\",basic[i],b[basic[i]]);\n for (j=1;j<=n;++j)\n printf(\"%c%.2fx[%d]\",j==1?'(':'+',a[basic[i]][nbasic[j]],nbasic[j]);\n printf(\")\\n\");\n }\n printf(\"maximize \");\n printf(\"%.2f\",v);\n for (i=1;i<=n;++i)\n printf(\"+%.2fx[%d]\",c[nbasic[i]],nbasic[i]);\n printf(\"\\n\\n\");\n}\nvoid simplex::pivot(int l,int e)\n{ static double ta[1205][1205];\n static double tb[1205];\n static double tc[1205];\n int i,j;\n memset(ta,0,sizeof(ta));\n memset(tb,0,sizeof(tb));\n memset(tc,0,sizeof(tc));\n tb[e]=b[l]/a[l][e];\n ta[e][l]=1.0/a[l][e];\n for (i=1;i<=n;++i)\n if (nbasic[i]!=e)\n ta[e][nbasic[i]]=a[l][nbasic[i]]/a[l][e];\n for (i=1;i<=m;++i)\n { tb[basic[i]]=b[basic[i]]-a[basic[i]][e]*tb[e];\n ta[basic[i]][l]=-a[basic[i]][e]*ta[e][l];\n for (j=1;j<=n;++j)\n if (nbasic[j]!=e)\n ta[basic[i]][nbasic[j]]=a[basic[i]][nbasic[j]]-a[basic[i]][e]*ta[e][nbasic[j]];\n }\n v=v+tb[e]*c[e];\n tc[l]=-ta[e][l]*c[e];\n for (i=1;i<=n;++i)\n if (nbasic[i]!=e)\n tc[nbasic[i]]=c[nbasic[i]]-c[e]*ta[e][nbasic[i]];\n for (i=1;i<=n;++i)\n if (nbasic[i]==e)\n nbasic[i]=l;\n for (i=1;i<=m;++i)\n if (basic[i]==l)\n basic[i]=e;\n for (i=1;i<=m;++i)\n { for (j=1;j<=n;++j)\n a[basic[i]][nbasic[j]]=ta[basic[i]][nbasic[j]];\n b[basic[i]]=tb[basic[i]];\n }\n for (i=1;i<=n;++i)\n c[nbasic[i]]=tc[nbasic[i]];\n}\ndouble simplex::opt()\n{ double delta;\n int i,l,e;\n while (1)\n { e=n+m+2;\n for (i=1;i<=n;++i)\n if (fcmp(c[nbasic[i]])>0&&nbasic[i]<e)\n e=nbasic[i];\n if (e==n+m+2)\n return v;\n delta=-1;\n l=n+m+2;\n for (i=1;i<=m;++i)\n if (fcmp(a[basic[i]][e])>0)\n { double t=b[basic[i]]/a[basic[i]][e];\n if (l==n+m+2||fcmp(t-delta)<0||(fcmp(t-delta)==0&&basic[i]<l))\n { delta=t;\n l=basic[i];\n }\n }\n if (l==n+m+2)\n return inf;\n else pivot(l,e);\n }\n}\nint longest_path()\n{ int q[105];\n int i,h,t;\n q[h=t=1]=1;\n while (h<=t)\n { for (i=last[q[h]];i!=0;i=pre[i])\n { f[e[i]]=max(f[e[i]],f[q[h]]+w[i]);\n --deg[e[i]];\n if (deg[e[i]]==0)\n q[++t]=e[i];\n }\n ++h;\n }\n return f[n];\n}\nvoid buildlp()\n{ int i,j;\n for (i=1;i<=n;++i)\n lp.nbasic[i]=i;\n for (i=1;i<=m;++i)\n { ++lp.c[ed[i].v];\n --lp.c[ed[i].u];\n lp.v+=f[ed[i].v]-f[ed[i].u]-ed[i].w;\n }\n for (i=1;i<=m;++i)\n { lp.a[n+i][ed[i].v]=-1;\n lp.a[n+i][ed[i].u]=1;\n lp.b[n+i]=f[ed[i].v]-f[ed[i].u]-ed[i].w;\n lp.basic[i]=n+i;\n }\n for (i=1;i<n;++i)\n { lp.a[n+m+i][i]=-1;\n lp.basic[m+i]=n+m+i;\n }\n lp.a[n+m+n][n]=1;\n lp.basic[m+n]=n+m+n;\n m+=n;\n}\nint main()\n{ int i,a,b,c;\n scanf(\"%d%d\",&n,&m);\n for (i=1;i<=m;++i)\n { scanf(\"%d%d%d\",&a,&b,&c);\n ++a,++b;\n ++deg[b];\n ed[i]=edge(a,b,c);\n add(a,b,c);\n }\n longest_path();\n buildlp();\n printf(\"%d\\n\",(int)lp.opt());\n fclose(stdin);\n fclose(stdout);\n return 0;\n}\n//分析:http://blog.sina.com.cn/s/blog_4a0c4e5d0101glxu.html", "accuracy": 1, "time_ms": 50, "memory_kb": 25056, "score_of_the_acc": -1.4, "final_rank": 20 }, { "submission_id": "aoj_2230_6857380", "code_snippet": "#include <cstdio>\n#include <vector>\n#include <algorithm>\n#include <queue>\nusing namespace std;\ntypedef pair<long, long> P;\n#define V 10010\nlong INF2 = 400000, INF = 1e9;\n\n// https://lepton.hatenablog.jp/entry/2015/08/07/185540\n// https://yang33-kassa.jp/aoj/aoj2230/\n// https://sigma425.hatenablog.com/entry/2015/07/12/235941\n\nstruct edge {\n\tlong to, cap, cost, rev;\n\n};\ntypedef vector<edge> vertex;\ntypedef vector<vertex> graph;\nlong dist[V];\nlong h[V];\nlong prevv[V], preve[V];\n\nvertex g[V];\n\nvoid add_edge(int from, int to, int cap, int cost) {\n\t//cout<<from<<\" \"<<to<<\" \"<<cap<<\" \"<<cost<<endl;\n\tg[from].push_back((edge) { to, cap, cost, (int)g[to].size() });\n\tg[to].push_back((edge) { from, 0, -cost, (int)g[from].size() - 1 });\n}\n\nlong min_cost_flow(int s, int t, long f) {\n\tlong res = 0;\n\tfill(h, h + V, 0);\n\tfor (int i = 0; i < V; i++) {\n\t\tfor (int j = 0; j < g[i].size(); j++) {\n\t\t\tedge& e = g[i][j];\n\t\t\tif (e.cap == 0) continue;\n\t\t\tint u = e.to;\n\t\t\th[u] = min(h[u], h[i] + e.cost);\n\t\t}\n\t}\n\twhile (f > 0) {\n\t\tpriority_queue<P, vector<P>, greater<P> > q;\n\t\tfill(dist, dist + V, INF);\n\t\tdist[s] = 0;\n\t\tq.push(P(0, s));\n\t\twhile (!q.empty()) {\n\t\t\tP p = q.top(); q.pop();\n\t\t\tint v = p.second;\n\t\t\tif (dist[v] < p.first) continue;\n\t\t\tfor (int i = 0; i < g[v].size(); i++) {\n\t\t\t\tedge& e = g[v][i];\n\t\t\t\tif (e.cap > 0 && dist[e.to] > dist[v] + e.cost + h[v] - h[e.to]) {\n\t\t\t\t\tdist[e.to] = dist[v] + e.cost + h[v] - h[e.to];\n\t\t\t\t\tprevv[e.to] = v;\n\t\t\t\t\tpreve[e.to] = i;\n\t\t\t\t\tq.push(P(dist[e.to], e.to));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tfor (int v = 0; v < V; v++)h[v] += dist[v];\n\n\t\tlong d = f;\n\t\tfor (int v = t; v != s; v = prevv[v]) {\n\t\t\td = min(d, g[prevv[v]][preve[v]].cap);\n\t\t}\n\t\tf -= d;\n\t\tres += d * h[t];\n\t\tfor (int v = t; v != s; v = prevv[v]) {\n\t\t\tedge& e = g[prevv[v]][preve[v]];\n\t\t\te.cap -= d;\n\t\t\tg[v][e.rev].cap += d;\n\t\t}\n\t}\n\treturn res;\n}\n\n#define N 1100\nlong f[N][N];\nlong d[N];\n\nint main() {\n\tint n, m;\n\tscanf(\"%d%d\", &n, &m);\n\tint s = 0, t = n - 1, t2 = n;\n\tfor (int i = 0; i < n; i++) fill(f[i], f[i] + N, INF);\n\tfor (int i = 0; i < m; i++) {\n\t\tint x, y, c;\n\t\tscanf(\"%d%d%d\", &x, &y, &c);\n\t\tc *= -1;\n\t\tf[x][y] = c;\n\t\tadd_edge(x, y, INF, c);\n\t\tadd_edge(x, y, 1, c - INF2);\n\t}\n\tfor (int k = 0; k < n; k++)\n\t\tfor (int i = 0; i < n; i++)\n\t\t\tfor (int j = 0; j < n; j++)\n\t\t\t\tf[i][j] = min(f[i][j], f[i][k] + f[k][j]);\n\tadd_edge(t, t2, INF, -f[0][n - 1]);\n\tlong res = min_cost_flow(s, t2, INF2);\n\tres += INF2 * m;\n\tprintf(\"%ld\\n\", res);\n\treturn 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 4088, "score_of_the_acc": -0.1373, "final_rank": 10 }, { "submission_id": "aoj_2230_6857352", "code_snippet": "#include <cstdio>\n#include <vector>\n#include <algorithm>\n#include <queue>\nusing namespace std;\ntypedef pair<long, long> P;\n#define V 10010\nlong INF2 = 400000, INF = 1e9;\n\n// https://lepton.hatenablog.jp/entry/2015/08/07/185540\n\nstruct edge {\n\tlong to, cap, cost, rev;\n\n};\ntypedef vector<edge> vertex;\ntypedef vector<vertex> graph;\nlong dist[V];\nlong h[V];\nlong prevv[V], preve[V];\n\nvertex g[V];\n\nvoid add_edge(int from, int to, int cap, int cost) {\n\t//cout<<from<<\" \"<<to<<\" \"<<cap<<\" \"<<cost<<endl;\n\tg[from].push_back((edge) { to, cap, cost, (int)g[to].size() });\n\tg[to].push_back((edge) { from, 0, -cost, (int)g[from].size() - 1 });\n}\n\nlong min_cost_flow(int s, int t, long f) {\n\tlong res = 0;\n\tfill(h, h + V, 0);\n\tfor (int i = 0; i < V; i++) {\n\t\tfor (int j = 0; j < g[i].size(); j++) {\n\t\t\tedge& e = g[i][j];\n\t\t\tif (e.cap == 0) continue;\n\t\t\tint u = e.to;\n\t\t\th[u] = min(h[u], h[i] + e.cost);\n\t\t}\n\t}\n\twhile (f > 0) {\n\t\tpriority_queue<P, vector<P>, greater<P> > q;\n\t\tfill(dist, dist + V, INF);\n\t\tdist[s] = 0;\n\t\tq.push(P(0, s));\n\t\twhile (!q.empty()) {\n\t\t\tP p = q.top(); q.pop();\n\t\t\tint v = p.second;\n\t\t\tif (dist[v] < p.first) continue;\n\t\t\tfor (int i = 0; i < g[v].size(); i++) {\n\t\t\t\tedge& e = g[v][i];\n\t\t\t\tif (e.cap > 0 && dist[e.to] > dist[v] + e.cost + h[v] - h[e.to]) {\n\t\t\t\t\tdist[e.to] = dist[v] + e.cost + h[v] - h[e.to];\n\t\t\t\t\tprevv[e.to] = v;\n\t\t\t\t\tpreve[e.to] = i;\n\t\t\t\t\tq.push(P(dist[e.to], e.to));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tfor (int v = 0; v < V; v++)h[v] += dist[v];\n\n\t\tlong d = f;\n\t\tfor (int v = t; v != s; v = prevv[v]) {\n\t\t\td = min(d, g[prevv[v]][preve[v]].cap);\n\t\t}\n\t\tf -= d;\n\t\tres += d * h[t];\n\t\tfor (int v = t; v != s; v = prevv[v]) {\n\t\t\tedge& e = g[prevv[v]][preve[v]];\n\t\t\te.cap -= d;\n\t\t\tg[v][e.rev].cap += d;\n\t\t}\n\t}\n\treturn res;\n}\n\n#define N 1100\nlong f[N][N];\nlong d[N];\n\nint main() {\n\tint n, m;\n\tscanf(\"%d%d\", &n, &m);\n\tint s = 0, t = n - 1, t2 = n;\n\tfor (int i = 0; i < n; i++) fill(f[i], f[i] + N, INF);\n\tfor (int i = 0; i < m; i++) {\n\t\tint x, y, c;\n\t\tscanf(\"%d%d%d\", &x, &y, &c);\n\t\tc *= -1;\n\t\tf[x][y] = c;\n\t\tadd_edge(x, y, INF, c);\n\t\tadd_edge(x, y, 1, c - INF2);\n\t}\n\tfor (int k = 0; k < n; k++)\n\t\tfor (int i = 0; i < n; i++)\n\t\t\tfor (int j = 0; j < n; j++)\n\t\t\t\tf[i][j] = min(f[i][j], f[i][k] + f[k][j]);\n\tadd_edge(t, t2, INF, -f[0][n - 1]);\n\tlong res = min_cost_flow(s, t2, INF2);\n\tres += INF2 * m;\n\tprintf(\"%ld\\n\", res);\n\treturn 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 4088, "score_of_the_acc": -0.1373, "final_rank": 10 }, { "submission_id": "aoj_2230_6168995", "code_snippet": "//#define _GLIBCXX_DEBUG\n\n#include<bits/stdc++.h>\nusing namespace std;\n\n#define endl '\\n'\n#define lfs cout<<fixed<<setprecision(10)\n#define ALL(a) (a).begin(),(a).end()\n#define ALLR(a) (a).rbegin(),(a).rend()\n#define UNIQUE(a) (a).erase(unique((a).begin(),(a).end()),(a).end())\n#define spa << \" \" <<\n#define fi first\n#define se second\n#define MP make_pair\n#define MT make_tuple\n#define PB push_back\n#define EB emplace_back\n#define rep(i,n,m) for(ll i = (n); i < (ll)(m); i++)\n#define rrep(i,n,m) for(ll i = (ll)(m) - 1; i >= (ll)(n); i--)\nusing ll = long long;\nusing ld = long double;\nconst ll MOD1 = 1e9+7;\nconst ll MOD9 = 998244353;\nconst ll INF = 1e18;\nusing P = pair<ll, ll>;\ntemplate<typename T> using PQ = priority_queue<T>;\ntemplate<typename T> using QP = priority_queue<T,vector<T>,greater<T>>;\ntemplate<typename T1, typename T2>bool chmin(T1 &a,T2 b){if(a>b){a=b;return true;}else return false;}\ntemplate<typename T1, typename T2>bool chmax(T1 &a,T2 b){if(a<b){a=b;return true;}else return false;}\nll median(ll a,ll b, ll c){return a+b+c-max({a,b,c})-min({a,b,c});}\nvoid ans1(bool x){if(x) cout<<\"Yes\"<<endl;else cout<<\"No\"<<endl;}\nvoid ans2(bool x){if(x) cout<<\"YES\"<<endl;else cout<<\"NO\"<<endl;}\nvoid ans3(bool x){if(x) cout<<\"Yay!\"<<endl;else cout<<\":(\"<<endl;}\ntemplate<typename T1,typename T2>void ans(bool x,T1 y,T2 z){if(x)cout<<y<<endl;else cout<<z<<endl;} \ntemplate<typename T1,typename T2,typename T3>void anss(T1 x,T2 y,T3 z){ans(x!=y,x,z);}; \ntemplate<typename T>void debug(const T &v,ll h,ll w,string sv=\" \"){for(ll i=0;i<h;i++){cout<<v[i][0];for(ll j=1;j<w;j++)cout<<sv<<v[i][j];cout<<endl;}};\ntemplate<typename T>void debug(const T &v,ll n,string sv=\" \"){if(n!=0)cout<<v[0];for(ll i=1;i<n;i++)cout<<sv<<v[i];cout<<endl;};\ntemplate<typename T>void debug(const vector<T>&v){debug(v,v.size());}\ntemplate<typename T>void debug(const vector<vector<T>>&v){for(auto &vv:v)debug(vv,vv.size());}\ntemplate<typename T>void debug(stack<T> st){while(!st.empty()){cout<<st.top()<<\" \";st.pop();}cout<<endl;}\ntemplate<typename T>void debug(queue<T> st){while(!st.empty()){cout<<st.front()<<\" \";st.pop();}cout<<endl;}\ntemplate<typename T>void debug(deque<T> st){while(!st.empty()){cout<<st.front()<<\" \";st.pop_front();}cout<<endl;}\ntemplate<typename T>void debug(PQ<T> st){while(!st.empty()){cout<<st.top()<<\" \";st.pop();}cout<<endl;}\ntemplate<typename T>void debug(QP<T> st){while(!st.empty()){cout<<st.top()<<\" \";st.pop();}cout<<endl;}\ntemplate<typename T>void debug(const set<T>&v){for(auto z:v)cout<<z<<\" \";cout<<endl;}\ntemplate<typename T>void debug(const multiset<T>&v){for(auto z:v)cout<<z<<\" \";cout<<endl;}\ntemplate<typename T,size_t size>void debug(const array<T, size> &a){for(auto z:a)cout<<z<<\" \";cout<<endl;}\ntemplate<typename T,typename V>void debug(const map<T,V>&v){for(auto z:v)cout<<\"[\"<<z.first<<\"]=\"<<z.second<<\",\";cout<<endl;}\ntemplate<typename T>vector<vector<T>>vec(ll x, ll y, T w){vector<vector<T>>v(x,vector<T>(y,w));return v;}\nll gcd(ll x,ll y){ll r;while(y!=0&&(r=x%y)!=0){x=y;y=r;}return y==0?x:y;}\nvector<ll>dx={1,-1,0,0,1,1,-1,-1};vector<ll>dy={0,0,1,-1,1,-1,1,-1};\ntemplate<typename T>vector<T> make_v(size_t a,T b){return vector<T>(a,b);}\ntemplate<typename... Ts>auto make_v(size_t a,Ts... ts){return vector<decltype(make_v(ts...))>(a,make_v(ts...));}\ntemplate<typename T1, typename T2>ostream &operator<<(ostream &os, const pair<T1, T2>&p){return os << p.first << \" \" << p.second;}\ntemplate<typename T>ostream &operator<<(ostream &os, const vector<T> &v){for(auto &z:v)os << z << \" \";cout<<\"|\"; return os;}\ntemplate<typename T>void rearrange(vector<int>&ord, vector<T>&v){\n auto tmp = v;\n for(int i=0;i<tmp.size();i++)v[i] = tmp[ord[i]];\n}\ntemplate<typename Head, typename... Tail>void rearrange(vector<int>&ord,Head&& head, Tail&&... tail){\n rearrange(ord, head);\n rearrange(ord, tail...);\n}\ntemplate<typename T> vector<int> ascend(const vector<T>&v){\n vector<int>ord(v.size());iota(ord.begin(),ord.end(),0);\n sort(ord.begin(),ord.end(),[&](int i,int j){return v[i]<v[j];});\n return ord;\n}\ntemplate<typename T> vector<int> descend(const vector<T>&v){\n vector<int>ord(v.size());iota(ord.begin(),ord.end(),0);\n sort(ord.begin(),ord.end(),[&](int i,int j){return v[i]>v[j];});\n return ord;\n}\nll FLOOR(ll n,ll div){assert(div>0);return n>=0?n/div:(n-div+1)/div;}\nll CEIL(ll n,ll div){assert(div>0);return n>=0?(n+div-1)/div:n/div;}\nll digitsum(ll n){ll ret=0;while(n){ret+=n%10;n/=10;}return ret;}\nll modulo(ll n,ll d){return (n%d+d)%d;};\ntemplate<typename T>T min(const vector<T>&v){return *min_element(v.begin(),v.end());}\ntemplate<typename T>T max(const vector<T>&v){return *max_element(v.begin(),v.end());}\ntemplate<typename T>T acc(const vector<T>&v){return accumulate(v.begin(),v.end(),T(0));};\ntemplate<typename T>T reverse(const T &v){return T(v.rbegin(),v.rend());};\n//mt19937 mt(chrono::steady_clock::now().time_since_epoch().count());\nint popcount(ll x){return __builtin_popcountll(x);};\nint poplow(ll x){return __builtin_ctzll(x);};\nint pophigh(ll x){return 63 - __builtin_clzll(x);};\ntemplate<typename T>T poll(queue<T> &q){auto ret=q.front();q.pop();return ret;};\ntemplate<typename T>T poll(priority_queue<T> &q){auto ret=q.top();q.pop();return ret;};\ntemplate<typename T>T poll(QP<T> &q){auto ret=q.top();q.pop();return ret;};\ntemplate<typename T>T poll(stack<T> &s){auto ret=s.top();s.pop();return ret;};\nll MULT(ll x,ll y){if(LLONG_MAX/x<=y)return LLONG_MAX;return x*y;}\nll POW2(ll x, ll k){ll ret=1,mul=x;while(k){if(mul==LLONG_MAX)return LLONG_MAX;if(k&1)ret=MULT(ret,mul);mul=MULT(mul,mul);k>>=1;}return ret;}\nll POW(ll x, ll k){ll ret=1;for(int i=0;i<k;i++){if(LLONG_MAX/x<=ret)return LLONG_MAX;ret*=x;}return ret;}\ntemplate< typename T = int >\nstruct edge {\n int to;\n T cost;\n int id;\n edge():id(-1){};\n edge(int to, T cost = 1, int id = -1):to(to), cost(cost), id(id){}\n operator int() const { return to; }\n};\n\ntemplate<typename T>\nusing Graph = vector<vector<edge<T>>>;\ntemplate<typename T>\nGraph<T>revgraph(const Graph<T> &g){\n Graph<T>ret(g.size());\n for(int i=0;i<g.size();i++){\n for(auto e:g[i]){\n int to = e.to;\n e.to = i;\n ret[to].push_back(e);\n }\n }\n return ret;\n}\ntemplate<typename T>\nGraph<T> readGraph(int n,int m,int indexed=1,bool directed=false,bool weighted=false){\n Graph<T> ret(n);\n for(int es = 0; es < m; es++){\n int u,v;\n T w=1;\n cin>>u>>v;u-=indexed,v-=indexed;\n if(weighted)cin>>w;\n ret[u].emplace_back(v,w,es);\n if(!directed)ret[v].emplace_back(u,w,es);\n }\n return ret;\n}\ntemplate<typename T>\nGraph<T> readParent(int n,int indexed=1,bool directed=true){\n Graph<T>ret(n);\n for(int i=1;i<n;i++){\n int p;cin>>p;\n p-=indexed;\n ret[p].emplace_back(i);\n if(!directed)ret[i].emplace_back(p);\n }\n return ret;\n}\n//BEGIN CUT HERE\n// O(m^2 \\log m \\log U)\n// U: maximum capacity\nenum Objective{\n MINIMIZE = +1,\n MAXIMIZE = -1,\n};\ntemplate<typename Flow, typename Cost,\n Objective objective = Objective::MINIMIZE>\nstruct MinCostFlow{\n template<typename T> inline void chmin(T &x,T y){x=min(x,y);}\n\n struct Edge{\n int src,dst;\n Flow flow,cap;\n Cost cost;\n int rev;\n Edge(int src,int dst,Flow cap,Cost cost,int rev):\n src(src),dst(dst),flow(0),cap(cap),cost(cost),rev(rev){}\n Flow residual_cap()const{return cap-flow;}\n };\n\n struct EdgePtr{\n int v,e;\n EdgePtr(int v,int e):v(v),e(e){}\n };\n\n int n;\n vector<vector<Edge>> G;\n vector<Flow> b;\n vector<Cost> p;\n\n MinCostFlow(int n):n(n),G(n),b(n,0){}\n\n EdgePtr add_edge(int src,int dst,Flow lower,Flow upper,Cost cost){\n int e=G[src].size();\n int r=(src==dst?e+1:G[dst].size());\n assert(lower<=upper);\n G[src].emplace_back(src,dst,+upper,+cost*objective,r);\n G[dst].emplace_back(dst,src,-lower,-cost*objective,e);\n return EdgePtr(src,e);\n }\n\n const Edge &get_edge(EdgePtr ep)const{return G[ep.v][ep.e];}\n\n void push(Edge &e,Flow amount){\n e.flow+=amount;\n G[e.dst][e.rev].flow-=amount;\n }\n\n void add_supply(int v,Flow amount){b[v]+=amount;}\n void add_demand(int v,Flow amount){b[v]-=amount;}\n\n Cost residual_cost(const Edge &e){\n return e.cost+p[e.src]-p[e.dst];\n }\n\n vector<int> excess_vs,deficit_vs;\n void saturate_negative(const Flow delta){\n for(auto &es:G){\n for(auto &e:es){\n Flow cap=e.residual_cap();\n cap-=cap%delta;\n if(cap<0 or residual_cost(e)<0){\n push(e,cap);\n b[e.src]-=cap;\n b[e.dst]+=cap;\n }\n }\n }\n\n excess_vs.clear();\n deficit_vs.clear();\n for(int v=0;v<n;v++){\n if(b[v]>0) excess_vs.emplace_back(v);\n if(b[v]<0) deficit_vs.emplace_back(v);\n }\n }\n\n const Cost unreachable = std::numeric_limits<Cost>::max();\n Cost farthest;\n vector<Cost> dist;\n vector<Edge*> parent;\n\n struct P{\n Cost first;\n int second;\n P(Cost first,int second):first(first),second(second){}\n bool operator<(const P o)const{return first>o.first;}\n };\n\n priority_queue<P> pq;\n\n template<typename Predicate>\n void eliminate(vector<int> &vs,Predicate predicate){\n vs.erase(remove_if(begin(vs),end(vs),predicate),end(vs));\n }\n\n bool dual(const Flow delta){\n eliminate(excess_vs, [&](int v){return b[v]<+delta;});\n eliminate(deficit_vs,[&](int v){return b[v]>-delta;});\n\n dist.assign(n,unreachable);\n for(int v:excess_vs) pq.emplace(dist[v]=0,v);\n\n parent.assign(n,nullptr);\n auto emplace=[&](Edge& e){\n if(e.residual_cap()<delta) return;\n Cost nxt=dist[e.src]+residual_cost(e);\n if(nxt>=dist[e.dst]) return;\n pq.emplace(dist[e.dst]=nxt,e.dst);\n parent[e.dst]=&e;\n };\n\n farthest=0;\n int deficit_count=0;\n while(!pq.empty()){\n Cost d=pq.top().first;\n int v=pq.top().second;\n pq.pop();\n if(dist[v]<d) continue;\n farthest=d;\n\n if(b[v]<=-delta) deficit_count++;\n if(deficit_count>=(int)deficit_vs.size()) break;\n\n for(auto &e:G[v]) emplace(e);\n }\n pq=decltype(pq)();\n\n for(int v=0;v<n;v++)\n p[v]+=min(dist[v],farthest);\n\n return deficit_count>0;\n }\n\n void primal(const Flow delta){\n for(int t:deficit_vs){\n if(dist[t]>farthest) continue;\n Flow f=-b[t];\n int v;\n for(v=t;parent[v];v=parent[v]->src)\n chmin(f,parent[v]->residual_cap());\n chmin(f,b[v]);\n\n f-=f%delta;\n if(f<=0) continue;\n\n for(v=t;parent[v];){\n auto &e=*parent[v];\n push(e,f);\n int u=parent[v]->src;\n if(e.residual_cap()<=0) parent[v]=nullptr;\n v=u;\n }\n b[t]+=f;\n b[v]-=f;\n }\n }\n\n template<Flow SCALING_FACTOR=2>\n bool build(){\n p.resize(n);\n Flow max_flow=1;\n for(auto t:b) max_flow=max({max_flow,t,-t});\n for(auto &es:G)\n for(auto &e:es)\n max_flow=max({max_flow,e.residual_cap(),-e.residual_cap()});\n\n Flow delta=1;\n while(delta<max_flow) delta*=SCALING_FACTOR;\n for(;delta;delta/=SCALING_FACTOR){\n saturate_negative(delta);\n while(dual(delta)) primal(delta);\n }\n\n return excess_vs.empty() and deficit_vs.empty();\n }\n\n template<typename T=Cost>\n T get_cost(){\n T res=0;\n for(auto &es:G)\n for(auto &e:es)\n res+=T(e.flow)*T(e.cost)/T(objective);\n return res/T(2);\n }\n template<typename T=Cost> T get_gain(){return get_cost();}\n\n vector<Cost> get_potential(){\n fill(p.begin(),p.end(),0);\n for(int i=0;i<n;i++)\n for(auto &es:G)\n for(auto &e:es)\n if(e.residual_cap()>0)\n chmin(p[e.dst],p[e.src]+e.cost);\n return p;\n }\n};\n\ntemplate<typename Flow, typename Cost>\nusing MaxGainFlow = MinCostFlow<Flow, Cost, Objective::MAXIMIZE>;\n//END CUT HERE\nint main(){\n cin.tie(nullptr);\n ios_base::sync_with_stdio(false);\n ll res=0,buf=0;\n bool judge = true;\n\tll n,m;cin>>n>>m;\n\tauto g=readGraph<ll>(n,m,0,true,true);\n\tvector<ll>dp(n);\n\tMinCostFlow<ll,ll>mcf(n);\n\tll inf=1e9;\n\trep(i,0,n){\n\t\tfor(auto e:g[i]){\n\t\t\tchmax(dp[e.to],dp[i]+e.cost);\n\t\t\tmcf.add_edge(e.to,i,1,inf,-e.cost);\n\t\t}\n\t}\n\tmcf.add_edge(0,n-1,0,inf,dp[n-1]);\n\tassert(mcf.build());\n\tcout<<mcf.get_cost()<<endl;\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3576, "score_of_the_acc": -0.0138, "final_rank": 3 }, { "submission_id": "aoj_2230_6153932", "code_snippet": "#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"unroll-loops\")\n#include<iostream>\n#include<string>\n#include<cstdio>\n#include<vector>\n#include<cmath>\n#include<algorithm>\n#include<functional>\n#include<iomanip>\n#include<queue>\n#include<ciso646>\n#include<random>\n#include<map>\n#include<set>\n#include<bitset>\n#include<stack>\n#include<unordered_map>\n#include<unordered_set>\n#include<utility>\n#include<cassert>\n#include<complex>\n#include<numeric>\n#include<array>\n#include<chrono>\nusing namespace std;\n\n//#define int long long\ntypedef long long ll;\n\ntypedef unsigned long long ul;\ntypedef unsigned int ui;\nconstexpr ll mod = 1000000007;\nconst ll INF = mod * mod;\ntypedef pair<int, int>P;\n\n#define rep(i,n) for(int i=0;i<n;i++)\n#define per(i,n) for(int i=n-1;i>=0;i--)\n#define Rep(i,sta,n) for(int i=sta;i<n;i++)\n#define rep1(i,n) for(int i=1;i<=n;i++)\n#define per1(i,n) for(int i=n;i>=1;i--)\n#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)\n#define all(v) (v).begin(),(v).end()\ntypedef pair<ll, ll> LP;\ntypedef double ld;\ntypedef pair<ld, ld> LDP;\nconst ld eps = 1e-4;\nconst ld pi = acosl(-1.0);\n\ntemplate<typename T>\nvoid chmin(T& a, T b) {\n\ta = min(a, b);\n}\ntemplate<typename T>\nvoid chmax(T& a, T b) {\n\ta = max(a, b);\n}\ntemplate<typename T>\nvoid cinarray(vector<T>& v) {\n\trep(i, v.size())cin >> v[i];\n}\ntemplate<typename T>\nvoid coutarray(vector<T>& v) {\n\trep(i, v.size()) {\n\t\tif (i > 0)cout << \" \"; cout << v[i];\n\t}\n\tcout << \"\\n\";\n}\nll mod_pow(ll x, ll n, ll m = mod) {\n\tif (n < 0) {\n\t\tll res = mod_pow(x, -n, m);\n\t\treturn mod_pow(res, m - 2, m);\n\t}\n\tif (abs(x) >= m)x %= m;\n\tif (x < 0)x += m;\n\tif (x == 0)return 0;\n\tll res = 1;\n\twhile (n) {\n\t\tif (n & 1)res = res * x % m;\n\t\tx = x * x % m; n >>= 1;\n\t}\n\treturn res;\n}\nstruct modint {\n\tint n;\n\tmodint() :n(0) { ; }\n\tmodint(ll m) {\n\t\tif (m < 0 || mod <= m) {\n\t\t\tm %= mod; if (m < 0)m += mod;\n\t\t}\n\t\tn = m;\n\t}\n\toperator int() { return n; }\n};\nbool operator==(modint a, modint b) { return a.n == b.n; }\nmodint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }\nmodint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }\nmodint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }\nmodint operator+(modint a, modint b) { return a += b; }\nmodint operator-(modint a, modint b) { return a -= b; }\nmodint operator*(modint a, modint b) { return a *= b; }\nmodint operator^(modint a, ll n) {\n\tif (n == 0)return modint(1);\n\tmodint res = (a * a) ^ (n / 2);\n\tif (n % 2)res = res * a;\n\treturn res;\n}\n\nll inv(ll a, ll p) {\n\treturn (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);\n}\nmodint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }\nmodint operator/=(modint& a, modint b) { a = a / b; return a; }\nconst int max_n = 1 << 20;\nmodint fact[max_n], factinv[max_n];\nvoid init_f() {\n\tfact[0] = modint(1);\n\tfor (int i = 0; i < max_n - 1; i++) {\n\t\tfact[i + 1] = fact[i] * modint(i + 1);\n\t}\n\tfactinv[max_n - 1] = modint(1) / fact[max_n - 1];\n\tfor (int i = max_n - 2; i >= 0; i--) {\n\t\tfactinv[i] = factinv[i + 1] * modint(i + 1);\n\t}\n}\nmodint comb(int a, int b) {\n\tif (a < 0 || b < 0 || a < b)return 0;\n\treturn fact[a] * factinv[b] * factinv[a - b];\n}\nmodint combP(int a, int b) {\n\tif (a < 0 || b < 0 || a < b)return 0;\n\treturn fact[a] * factinv[a - b];\n}\n\nll gcd(ll a, ll b) {\n\ta = abs(a); b = abs(b);\n\tif (a < b)swap(a, b);\n\twhile (b) {\n\t\tll r = a % b; a = b; b = r;\n\t}\n\treturn a;\n}\n\nint dx[4] = { 1,0,-1,0 };\nint dy[4] = { 0,1,0,-1 };\n\n/*template<typename T>\nstruct mcf {\nprivate:\n\tstruct edge {\n\t\tint to, cap; T cost; int rev;\n\t};\n\tvector<vector<edge>> G;\n\tvector<P> par;\n\tvector<T> dist;\n\tT inf = INF/1000;\npublic:\n\tmcf(int n) {\n\t\tG.resize(n);\n\t\tpar.resize(n);\n\t\tdist.resize(n);\n\t}\n\tvoid add_edge(int from, int to, int cap, T cost) {\n\t\tG[from].push_back({ to,cap,cost,(int)G[to].size() });\n\t\tG[to].push_back({ from,0,-cost,(int)G[from].size() - 1 });\n\t}\n\tpair<T, int> minimum_road(int s, int t) {\n\t\tfill(all(par), P{ -1,-1 });\n\t\tfill(all(dist), inf);\n\t\tdist[s] = 0;\n\t\trep(_, G.size()) {\n\t\t\trep(id, G.size()) {\n\t\t\t\trep(j, G[id].size()) {\n\t\t\t\t\tif (G[id][j].cap > 0) {\n\t\t\t\t\t\tint to = G[id][j].to;\n\t\t\t\t\t\tT nd = p.first + G[id][j].cost;\n\t\t\t\t\t\tif (nd < dist[to]) {\n\t\t\t\t\t\t\tdist[to] = nd;\n\t\t\t\t\t\t\tpar[to] = { id,j };\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tint cur = t;\n\t\tint f = mod;\n\t\twhile (cur != s) {\n\t\t\tint p = par[cur].first, j = par[cur].second;\n\t\t\tif (p < 0)return { -1,-1 };\n\t\t\tf = min(f, G[p][j].cap);\n\t\t\tcur = p;\n\t\t}\n\t\tcur = t;\n\t\twhile (cur != s) {\n\t\t\tint p = par[cur].first, j = par[cur].second;\n\t\t\tif (p < 0)return { -1,-1 };\n\t\t\tG[p][j].cap -= f;\n\t\t\tif (G[p][j].rev >= 0) {\n\t\t\t\tG[cur][G[p][j].rev].cap += f;\n\t\t\t}\n\t\t\tcur = p;\n\t\t}\n\t\treturn { dist[t],f };\n\t}\n\tT minimum_cost_flow(int s, int t, int k) {\n\t\tT ret = 0;\n\t\trep(i, k) {\n\t\t\tpair<T, int> z = minimum_road(s, t);\n\t\t\tif (z.first < 0)return -1;\n\t\t\tif (k - i <= z.second) {\n\t\t\t\tret += z.first * (k - i); break;\n\t\t\t}\n\t\t\ti += z.second - 1;\n\t\t\tret += z.first * z.second;\n\t\t}\n\t\treturn ret;\n\t}\n};*/\n\ntemplate<typename TF, typename TC>\nstruct PrimalDual {\n\tstruct edge {\n\t\tint to;\n\t\tTF cap;\n\t\tTC cost;\n\t\tint rev;\n\t\tedge() {}\n\t\tedge(int to, TF cap, TC cost, int rev) :\n\t\t\tto(to), cap(cap), cost(cost), rev(rev) {}\n\t};\n\n\tvector<vector<edge>> G;\n\tvector<TC> h, dist;\n\tvector<int> prevv, preve;\n\n\tPrimalDual() {}\n\tPrimalDual(int n) :G(n), h(n), dist(n), prevv(n), preve(n) {}\n\n\tvoid add_edge(int u, int v, TF cap, TC cost) {\n\t\tG[u].emplace_back(v, cap, cost, G[v].size());\n\t\tG[v].emplace_back(u, 0, -cost, G[u].size() - 1);\n\t}\n\n\tvoid dijkstra(int s) {\n\t\tstruct P {\n\t\t\tTC first;\n\t\t\tint second;\n\t\t\tP(TC first, int second) :first(first), second(second) {}\n\t\t\tbool operator<(const P& a) const { return a.first < first; }\n\t\t};\n\t\tpriority_queue<P> que;\n\t\tfill(dist.begin(), dist.end(), INF);\n\n\t\tdist[s] = 0;\n\t\tque.emplace(dist[s], s);\n\t\twhile (!que.empty()) {\n\t\t\tP p = que.top(); que.pop();\n\t\t\tint v = p.second;\n\t\t\tif (dist[v] < p.first) continue;\n\t\t\tfor (int i = 0; i < (int)G[v].size(); i++) {\n\t\t\t\tedge& e = G[v][i];\n\t\t\t\tif (e.cap == 0) continue;\n\t\t\t\tif (dist[v] + e.cost + h[v] - h[e.to] < dist[e.to]) {\n\t\t\t\t\tdist[e.to] = dist[v] + e.cost + h[v] - h[e.to];\n\t\t\t\t\tprevv[e.to] = v;\n\t\t\t\t\tpreve[e.to] = i;\n\t\t\t\t\tque.emplace(dist[e.to], e.to);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tTC flow(int s, int t, TF f, int& ok) {\n\t\tTC res = 0;\n\t\tfill(h.begin(), h.end(), 0);\n\t\twhile (f > 0) {\n\t\t\tdijkstra(s);\n\t\t\tif (dist[t] == INF) {\n\t\t\t\tok = 0;\n\t\t\t\treturn res;\n\t\t\t}\n\n\t\t\tfor (int v = 0; v < (int)h.size(); v++)\n\t\t\t\tif (dist[v] < INF) h[v] = h[v] + dist[v];\n\n\t\t\tTF d = f;\n\t\t\tfor (int v = t; v != s; v = prevv[v])\n\t\t\t\td = min(d, G[prevv[v]][preve[v]].cap);\n\n\t\t\tf -= d;\n\t\t\tres = res + h[t] * d;\n\t\t\tfor (int v = t; v != s; v = prevv[v]) {\n\t\t\t\tedge& e = G[prevv[v]][preve[v]];\n\t\t\t\te.cap -= d;\n\t\t\t\tG[v][e.rev].cap += d;\n\t\t\t}\n\t\t}\n\t\tok = 1;\n\t\treturn res;\n\t}\n};\n\ntemplate<typename T>\nstruct mcf {\nprivate:\n\tstruct edge {\n\t\tint to, cap; T cost; int rev;\n\t};\n\tvector<vector<edge>> G;\n\tvector<P> par;\n\tvector<T> dist;\n\tT inf = INF / 100000;\npublic:\n\tmcf(int n) {\n\t\tG.resize(n);\n\t\tpar.resize(n);\n\t\tdist.resize(n);\n\t}\n\tvoid add_edge(int from, int to, int cap, T cost) {\n\t\tG[from].push_back({ to,cap,cost,(int)G[to].size() });\n\t\tG[to].push_back({ from,0,-cost,(int)G[from].size() - 1 });\n\t}\n\tpair<T, int> minimum_road(int s, int t) {\n\t\tfill(all(par), P{ -1,-1 });\n\t\tfill(all(dist), inf);\n\t\tdist[s] = 0;\n\t\tpriority_queue<pair<T, int>, vector<pair<T, int>>, greater<pair<T, int>>> q;\n\t\tq.push({ 0,s });\n\t\twhile (!q.empty()) {\n\t\t\tpair<T, int> p = q.top(); q.pop();\n\t\t\tint id = p.second;\n\t\t\tif (id == t)continue;\n\t\t\tif (p.first > dist[id])continue;\n\t\t\trep(j, G[id].size()) {\n\t\t\t\tif (G[id][j].cap > 0) {\n\t\t\t\t\tint to = G[id][j].to;\n\t\t\t\t\tT nd = p.first + G[id][j].cost;\n\t\t\t\t\tif (nd < dist[to]) {\n\t\t\t\t\t\tdist[to] = nd;\n\t\t\t\t\t\tpar[to] = { id,j };\n\t\t\t\t\t\tq.push({ dist[to],to });\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tint cur = t;\n\t\tint f = mod;\n\t\twhile (cur != s) {\n\t\t\tint p = par[cur].first, j = par[cur].second;\n\t\t\tif (p < 0)return { -1,-1 };\n\t\t\tf = min(f, G[p][j].cap);\n\t\t\tcur = p;\n\t\t}\n\t\tcur = t;\n\t\twhile (cur != s) {\n\t\t\tint p = par[cur].first, j = par[cur].second;\n\t\t\tif (p < 0)return { -1,-1 };\n\t\t\tG[p][j].cap -= f;\n\t\t\tif (G[p][j].rev >= 0) {\n\t\t\t\tG[cur][G[p][j].rev].cap += f;\n\t\t\t}\n\t\t\tcur = p;\n\t\t}\n\t\treturn { dist[t],f };\n\t}\n\tT minimum_cost_flow(int s, int t, int k) {\n\t\tT ret = 0;\n\t\trep(i, k) {\n\t\t\tpair<T, int> z = minimum_road(s, t);\n\t\t\tif (z.first < 0)return -1;\n\t\t\tif (k - i <= z.second) {\n\t\t\t\tret += z.first * (k - i); break;\n\t\t\t}\n\t\t\ti += z.second - 1;\n\t\t\tret += z.first * z.second;\n\t\t}\n\t\treturn ret;\n\t}\n};\nstruct edge {\n\tint to, cost;\n};\nvoid solve() {\n\tint n, m; cin >> n >> m;\n\tvector<vector<edge>> G(n);\n\trep(i, m) {\n\t\tint a, b, c; cin >> a >> b >> c;\n\t\tG[a].push_back({ b,c });\n\t}\n\tvector<int> dp(n);\n\trep(i, n) {\n\t\tfor (edge e : G[i]) {\n\t\t\tchmax(dp[e.to], dp[i] + e.cost);\n\t\t}\n\t}\n\tint D = dp[n - 1];\n\t//cout << D << \"\\n\";\n\tmcf<ll> mc(n + 2);\n\t//mcf<ll> mc(n + 2);\n\tint s = n;\n\tint t = n + 1;\n\tvector<int> d(n);\n\tll ans = 0;\n\tmc.add_edge(0, n - 1, mod, D);\n\t//G[0].push_back({ n - 1,D });\n\tint sum = 0;\n\trep(i, n) {\n\t\tfor (edge e : G[i]) {\n\t\t\t/*ans += e.cost;\n\t\t\td[i] += -1;\n\t\t\td[e.to] -= -1;*/\n\n\t\t\t//mc.add_edge(i, e.to, -1, e.cost);\n\n\t\t\t//mc.add_edge(e.to,i,1,-e.cost-mod)\n\t\t\tans += -e.cost - mod/3;\n\t\t\td[i]++;\n\t\t\td[e.to]--;\n\t\t\tmc.add_edge(i, e.to, 1, e.cost + mod/3);\n\n\t\t\t//mc.add_edge(e.to, i, 10000, -e.cost);\n\t\t\tans += 100000 * (-e.cost);\n\t\t\td[i] += 100000;\n\t\t\td[e.to] -=100000;\n\t\t\tmc.add_edge(i,e.to, 100000, e.cost);\n\t\t}\n\t}\n\trep(i, n) {\n\t\tif (d[i] > 0) {\n\t\t\tsum += d[i];\n\t\t\tmc.add_edge(s, i, d[i], 0);\n\t\t}\n\t\telse {\n\t\t\tmc.add_edge(i, t, -d[i], 0);\n\t\t}\n\t}\n\t//cout << -ans << \"\\n\";\n\t//int ok;\n\tans += mc.minimum_cost_flow(s, t, sum);\n\tans += mod/3 * m;\n\tcout << ans<< \"\\n\";\n}\n\nsigned main() {\n\tios::sync_with_stdio(false);\n\tcin.tie(0);\n\t//cout << fixed << setprecision(8);\n\t//init_f();\n\t//init();\n\t//while(true)\n\t//expr();\n\t//int t; cin >> t; rep(i, t)\n\tsolve();\n\treturn 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 11784, "score_of_the_acc": -0.5906, "final_rank": 17 }, { "submission_id": "aoj_2230_6029583", "code_snippet": "#include <bits/stdc++.h>\n\n#define eprintf(args...) fprintf(stderr, args)\n#define rep(i, n) for (int i = 0; i < (int)(n); ++ i)\n\nstruct Edge {\n\tint v, w, c, r;\n};\n\nstruct MinCostFlow {\n\tstatic const int mxn = 105;\n\tint n, S, T;\n\tstd::vector <Edge> adj[mxn];\n\tint dis[mxn], h[mxn];\n\tint que[mxn], ql, qr;\n\tbool inque[mxn];\n\tstd::priority_queue <std::pair <int, int> > pq;\n\tbool vis[mxn];\n\tint id[mxn];\n\n\tvoid init(int _n, int _S, int _T) {\n\t\tn = _n, S = _S, T = _T;\n\t\trep(i, n) adj[i].clear();\n\t}\n\n\tinline void add_edge(int u, int v, int w, int c) {\n\t\tadj[u].push_back({v, w, c, (int) adj[v].size()});\n\t\tadj[v].push_back({u, 0, -c, (int) adj[u].size() - 1});\n\t}\n\n\tbool spfa() {\n\t\trep(i, n) h[i] = i == S ? 0 : 0x3f3f3f3f;\n\t\tql = qr = 0; que[qr ++] = S;\n\t\trep(i, n) inque[i] = i == S ? 1 : 0;\n\t\twhile (ql != qr) {\n\t\t\tint u = que[ql ++]; if (ql == mxn) ql = 0;\n\t\t\tinque[u] = false;\n\t\t\trep(i, adj[u].size()) {\n\t\t\t\tEdge e = adj[u][i];\n\t\t\t\tif (e.w && h[e.v] > h[u] + e.c) {\n\t\t\t\t\th[e.v] = h[u] + e.c;\n\t\t\t\t\tif (!inque[e.v]) {\n\t\t\t\t\t\tinque[e.v] = true;\n\t\t\t\t\t\tque[qr ++] = e.v; if (qr == mxn) qr = 0;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\trep(i, n) dis[i] = h[i];\n\t\treturn dis[T] < 1e9;\n\t}\n\n\tbool dij() {\n\t\trep(i, n) h[i] = i == S ? 0 : 0x3f3f3f3f;\n\t\tpq.push({-h[S], S});\n\t\twhile (!pq.empty()) {\n\t\t\tint u = pq.top().second;\n\t\t\tint d = -pq.top().first;\n\t\t\tpq.pop();\n\t\t\tif (d != h[u]) continue;\n\t\t\trep(i, adj[u].size()) {\n\t\t\t\tEdge e = adj[u][i];\n\t\t\t\tif (e.w && h[e.v] > h[u] + e.c + dis[u] - dis[e.v]) {\n\t\t\t\t\th[e.v] = h[u] + e.c + dis[u] - dis[e.v];\n\t\t\t\t\tpq.push({-h[e.v], e.v});\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\trep(i, n) dis[i] += h[i];\n\t\treturn dis[T] < 1e9;\n\t}\n\n\tint dfs(int u, int f = 0x3f3f3f3f) {\n\t\tif (u == T) return f;\n\t\tint flow = 0;\n\t\tvis[u] = true;\n\t\tfor (int &i = id[u]; i < (int) adj[u].size(); ++ i) {\n\t\t\tEdge e = adj[u][i];\n\t\t\tif (e.w && dis[e.v] == dis[u] + e.c && !vis[e.v]) {\n\t\t\t\tint d = dfs(e.v, std::min(f, e.w));\n\t\t\t\tif (d) {\n\t\t\t\t\tadj[u][i].w -= d;\n\t\t\t\t\tadj[e.v][adj[u][i].r].w += d;\n\t\t\t\t\tflow += d, f -= d;\n\t\t\t\t\tif (f == 0) break;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tvis[u] = false;\n\t\treturn flow;\n\t}\n\n\tint solve() {\n\t\tspfa();\n\t\tint ans = 0;\n\t\twhile (dij()) {\n\t\t\trep(i, n) vis[i] = false, id[i] = 0;\n\t\t\tans += dfs(S) * dis[T];\n\t\t}\n\t\treturn ans;\n\t}\n} mcf;\n\nconst int mxn = 1005;\nconst int M = 1000003, inf = 0x3f3f3f3f;\nint n, m;\nint E[mxn][3];\nstd::vector <std::pair <int, int> > adj[mxn];\n\nbool vis[mxn];\nint rem[mxn];\nint dfs(int u) {\n\tif (u == n - 1) return 0;\n\tif (vis[u]) return rem[u];\n\tvis[u] = true;\n\trem[u] = -0x3f3f3f3f;\n\tfor (auto pr : adj[u]) {\n\t\tint v = pr.first, w = pr.second;\n\t\trem[u] = std::max(rem[u], dfs(v) + w);\n\t}\n\treturn rem[u];\n}\n\nint main() {\n\tscanf(\"%d %d\", &n, &m);\n\trep(i, m) {\n\t\tscanf(\"%d %d %d\", &E[i][0], &E[i][1], &E[i][2]);\n\t\tadj[E[i][0]].push_back({E[i][1], E[i][2]});\n\t}\n\tint L = dfs(0);\n\tint S = n, T = n + 1, S0 = n + 2;\n\tmcf.init(n + 3, S, T);\n\tmcf.add_edge(S, S0, inf, L);\n\trep(i, m) {\n\t\tint u, v, w;\n\t\tu = E[i][0], v = E[i][1], w = E[i][2];\n\t\tmcf.add_edge(u, v, 1, -M - w);\n\t\tmcf.add_edge(u, v, inf, -w);\n\t}\n\tmcf.add_edge(S0, 0, inf, 0);\n\tmcf.add_edge(n - 1, T, inf, 0);\n\tprintf(\"%d\\n\", mcf.solve() + M * m);\n\treturn 0;\n}\n\n/*\n max sum a_{u, v}\n forall u, v, d_u - d_v + a_{u, v} <= -w_{u, v} --> x_{u, v}\n d_T - d_S <= L --> F\n\n <=>\n\n min sum -w_{u, v} x_{u, v} + L * F\n forall u, sum v x_{u, v} - sum v x_{v, u} - [u == S] F + [u == T] F >= (==) 0\n forall u, v, x_{u, v} >= 1\n*/", "accuracy": 1, "time_ms": 20, "memory_kb": 3748, "score_of_the_acc": -0.1217, "final_rank": 9 }, { "submission_id": "aoj_2230_5307389", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\nconst int N=2005;\nstruct Graph{\n\tstruct Edge{ int to,nxt,c,k; }edge[N*2];\n\tint head[N],top;\n\tGraph(){memset(head,-1,sizeof(head)),top=-1;}\n\tinline void add(int x,int y,int c,int k){\n\t\tedge[++top]=(Edge){y,head[x],c,k};\n\t\thead[x]=top;\n\t}\n}G,G1;\ninline void add2(int x,int y,int c,int k){\n//\tprintf(\"(%d %d %d %d)\\n\",x,y,c,k);\n\tG.add(x,y,c,k),G.add(y,x,0,-k);\n}\nnamespace FLOW{\n\tint iter[N],lev[N];\n\tll h[N],dis[N];\n\tbool dij(Graph& G,int s,int t,int n){\n\t\tpriority_queue<pair<ll,int>,vector<pair<ll,int> >,greater<pair<ll,int> > > q;\n\t\tfor(int i=0;i<=n;++i) iter[i]=G.head[i],lev[i]=0,dis[i]=1e18;\n\t\tdis[t]=0; lev[t]=1; q.push(make_pair(dis[t],t));\n\t\twhile(!q.empty()){\n\t\t\tint x=q.top().second; ll k=q.top().first; q.pop();\n\t\t\tif(k!=dis[x]) continue;\n\t\t\tfor(int j=G.head[x];~j;j=G.edge[j].nxt){\n\t\t\t\tint y=G.edge[j].to; ll k1=G.edge[j^1].k+h[x]-h[y];\n\t\t\t\tif(G.edge[j^1].c>0&&dis[x]+k1<dis[y]){\n\t\t\t\t\tdis[y]=dis[x]+k1; lev[y]=lev[x]+1;\n\t\t\t\t\tq.push(make_pair(dis[y],y));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn lev[s]>0;\n\t}\n\tint dfs(Graph& G,int x,int t,int mx){\n\t\tif(x==t) return mx;\n\t\tint ret=0;\n\t\tfor(int& j=iter[x];~j;j=G.edge[j].nxt){\n\t\t\tint y=G.edge[j].to;\n\t\t\tif(G.edge[j].c>0&&lev[y]==lev[x]-1&&G.edge[j].k+h[y]-h[x]==0){\n\t\t\t\tint d=dfs(G,y,t,min(mx-ret,G.edge[j].c));\n\t\t\t\tG.edge[j].c-=d; G.edge[j^1].c+=d; ret+=d;\n\t\t\t\tif(ret==mx) break;\n\t\t\t}\n\t\t}\n\t\treturn ret;\n\t}\n\tpair<ll,int> cost_flow2(Graph& G,int s,int t,int n,int mx){\n\t\tfor(int i=0;i<=n;++i) h[i]=0;\n\t\tpair<ll,int> ret(0,0);\n\t\twhile(mx>ret.second&&dij(G,s,t,n)){\n\t\t\tfor(int i=0;i<=n;++i) h[i]+=dis[i];\n\t\t\tint d=dfs(G,s,t,mx-ret.second);\n\t\t\tif(h[s]>=0) break;\n\t\t\tret.first+=h[s]*d;\n\t\t\tret.second+=d;\n\t\t}\n\t\treturn ret;\n\t}\n}\nint dis[N];\nvoid bfs(Graph& G,int dis[],int s,int t,int n){\n\tstatic int rd[N];\n\tfor(int i=1;i<=n;++i) rd[i]=0,dis[i]=0;\n\tfor(int x=1;x<=n;++x)\n\t\tfor(int j=G.head[x];~j;j=G.edge[j].nxt)\n\t\t\trd[G.edge[j].to]++;\n\tqueue<int> q; q.push(s);\n\twhile(!q.empty()){\n\t\tint x=q.front(); q.pop();\n\t\tfor(int j=G.head[x];~j;j=G.edge[j].nxt){\n\t\t\tint y=G.edge[j].to;\n\t\t\tdis[y]=max(dis[y],dis[x]+G.edge[j].k);\n\t\t\t--rd[y]; if(rd[y]==0) q.push(y);\n\t\t}\n\t}\n}\nint e[N];\nint main(){\n\tint n,m; scanf(\"%d%d\",&n,&m);\n\tll ans=0;\n\tfor(int i=1;i<=m;++i){\n\t\tint x,y,c; scanf(\"%d%d%d\",&x,&y,&c);\n\t\t++x; ++y;\n\t\tG1.add(x,y,0,c); add2(x,y,1e9,-c); add2(x,y,1,-c-1e9); \n\t\tans+=(int)1e9;\n\t}\n\tbfs(G1,dis,1,n,n);\n\tadd2(n,n+1,1e9,dis[n]);\n\tpair<ll,int> ret=FLOW::cost_flow2(G,1,n+1,n+1,1e9);\n\tans+=ret.first;\n\tprintf(\"%lld\\n\",ans);\n\treturn 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3316, "score_of_the_acc": -0.1018, "final_rank": 6 }, { "submission_id": "aoj_2230_4951326", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntypedef pair<ll, ll> l_l;\ntypedef pair<int, int> i_i;\ntemplate<class T>\ninline bool chmax(T &a, T b) {\n if(a < b) {\n a = b;\n return true;\n }\n return false;\n}\n\ntemplate<class T>\ninline bool chmin(T &a, T b) {\n if(a > b) {\n a = b;\n return true;\n }\n return false;\n}\n\ntemplate< typename flow_t, typename cost_t >\nstruct PrimalDual {\n const cost_t INF;\n\n struct edge {\n int to;\n flow_t cap;\n cost_t cost;\n int rev;\n bool isrev;\n };\n vector< vector< edge > > graph;\n vector< cost_t > potential, min_cost;\n vector< int > prevv, preve;\n\n PrimalDual(int V) : graph(V), INF(numeric_limits< cost_t >::max()) {}\n\n void add_edge(int from, int to, flow_t cap, cost_t cost) {\n graph[from].emplace_back((edge) {to, cap, cost, (int) graph[to].size(), false});\n graph[to].emplace_back((edge) {from, 0, -cost, (int) graph[from].size() - 1, true});\n }\n\n cost_t min_cost_flow(int s, int t, flow_t f) {\n int V = (int) graph.size();\n cost_t ret = 0;\n using Pi = pair< cost_t, int >;\n priority_queue< Pi, vector< Pi >, greater< Pi > > que;\n potential.assign(V, 0);\n preve.assign(V, -1);\n prevv.assign(V, -1);\n\n while(f > 0) {\n min_cost.assign(V, INF);\n que.emplace(0, s);\n min_cost[s] = 0;\n while(!que.empty()) {\n Pi p = que.top();\n que.pop();\n if(min_cost[p.second] < p.first) continue;\n for(int i = 0; i < graph[p.second].size(); i++) {\n edge &e = graph[p.second][i];\n cost_t nextCost = min_cost[p.second] + e.cost + potential[p.second] - potential[e.to];\n if(e.cap > 0 && min_cost[e.to] > nextCost) {\n min_cost[e.to] = nextCost;\n prevv[e.to] = p.second, preve[e.to] = i;\n que.emplace(min_cost[e.to], e.to);\n }\n }\n }\n if(min_cost[t] == INF) return -1;\n for(int v = 0; v < V; v++) potential[v] += min_cost[v];\n flow_t addflow = f;\n for(int v = t; v != s; v = prevv[v]) {\n addflow = min(addflow, graph[prevv[v]][preve[v]].cap);\n }\n f -= addflow;\n ret += addflow * potential[t];\n for(int v = t; v != s; v = prevv[v]) {\n edge &e = graph[prevv[v]][preve[v]];\n e.cap -= addflow;\n graph[v][e.rev].cap += addflow;\n }\n }\n return ret;\n }\n\n void output() {\n for(int i = 0; i < graph.size(); i++) {\n for(auto &e : graph[i]) {\n if(e.isrev) continue;\n auto &rev_e = graph[e.to][e.rev];\n cout << i << \"->\" << e.to << \" (flow: \" << rev_e.cap << \"/\" << rev_e.cap + e.cap << \")\" << endl;\n }\n }\n }\n};\n\nll N, M;\nll degree[305];\nvector<l_l> edges[305];\nll dist[305];\nll MAX;\nll f = 0;\nll ans = 0;\nconst ll INF = 1e9;\n\nint main() {\n cin >> N >> M;\n for(int i = 0; i < M; i++) {\n ll u, v, c;\n cin >> u >> v >> c;\n degree[v]++;\n degree[u]--;\n edges[u].push_back({v, c});\n }\n for(int i = 0; i < N; i++) {\n for(auto tmp : edges[i]) {\n chmax(dist[tmp.first], dist[i] + tmp.second);\n }\n }\n MAX = dist[N-1];\n ll s = N;\n ll t = N + 1;\n PrimalDual<ll, ll> g(t + 1);\n for(int i = 0; i < N; i++) {\n for(auto e : edges[i]) {\n g.add_edge(i, e.first, 1, - INF -e.second);\n g.add_edge(i, e.first, 1e9, -e.second);\n }\n }\n g.add_edge(N - 1, t, 1e9, MAX);\n //cerr << MAX << endl;\n g.add_edge(s, 0, 1e9, 0);\n while(true) {\n auto tmp = g.min_cost_flow(s, t, 1);\n if(tmp == -1) break;\n if(tmp == 0) break;\n ans += tmp;\n //cerr << tmp << endl;\n }\n //g.output();\n //cerr << ans << endl;\n auto tmp = g.min_cost_flow(s, t, f);\n //g.output();\n //cerr << tmp.first << \" \" << tmp.second << endl;\n //ans += tmp.second;\n ans += tmp;\n /*\n while(true) {\n g.output();\n cerr << \"A\" << endl;\n auto tmp = g.min_cost_flow(s, t, 1);\n cerr << tmp << endl;\n if(tmp <= 0) break;\n ans += tmp;\n }\n */\n //cerr << tmp << endl;\n ans += INF * M;\n cout << ans << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 110, "memory_kb": 3676, "score_of_the_acc": -1.0184, "final_rank": 19 }, { "submission_id": "aoj_2230_4867578", "code_snippet": "#define PROBLEM \"http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2230\"\n\n#include<bits/stdc++.h>\nusing namespace std;\n\n#define call_from_test\n\n#ifndef call_from_test\n#include <bits/stdc++.h>\nusing namespace std;\n#endif\n//BEGIN CUT HERE\ntemplate<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}\ntemplate<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}\n//END CUT HERE\n#ifndef call_from_test\nsigned main(){\n return 0;\n}\n#endif\n\n\n#ifndef call_from_test\n#include <bits/stdc++.h>\nusing namespace std;\n#endif\n//BEGIN CUT HERE\n// O(m^2 \\log n \\log U)\n// U: maximum capacity\nenum Objective{\n MINIMIZE = +1,\n MAXIMIZE = -1,\n};\ntemplate<typename Flow, typename Cost,\n Objective objective = Objective::MINIMIZE>\nstruct MinCostFlow{\n template<typename T> inline void chmin(T &x,T y){x=min(x,y);}\n\n struct Edge{\n int src,dst;\n Flow flow,cap;\n Cost cost;\n int rev;\n Edge(int src,int dst,Flow cap,Cost cost,int rev):\n src(src),dst(dst),flow(0),cap(cap),cost(cost),rev(rev){}\n Flow residual_cap()const{return cap-flow;}\n };\n\n struct EdgePtr{\n int v,e;\n EdgePtr(int v,int e):v(v),e(e){}\n };\n\n int n;\n vector<vector<Edge>> G;\n vector<Flow> b;\n vector<Cost> p;\n\n MinCostFlow(int n):n(n),G(n),b(n,0){}\n\n EdgePtr add_edge(int src,int dst,Flow lower,Flow upper,Cost cost){\n int e=G[src].size();\n int r=(src==dst?e+1:G[dst].size());\n assert(lower<=upper);\n G[src].emplace_back(src,dst,+upper,+cost*objective,r);\n G[dst].emplace_back(dst,src,-lower,-cost*objective,e);\n return EdgePtr(src,e);\n }\n\n const Edge &get_edge(EdgePtr ep)const{return G[ep.v][ep.e];}\n\n void push(Edge &e,Flow amount){\n e.flow+=amount;\n G[e.dst][e.rev].flow-=amount;\n }\n\n void add_supply(int v,Flow amount){b[v]+=amount;}\n void add_demand(int v,Flow amount){b[v]-=amount;}\n\n Cost residual_cost(const Edge &e){\n return e.cost+p[e.src]-p[e.dst];\n }\n\n vector<int> excess_vs,deficit_vs;\n void saturate_negative(const Flow delta){\n for(auto &es:G){\n for(auto &e:es){\n Flow cap=e.residual_cap();\n cap-=cap%delta;\n if(cap<0 or residual_cost(e)<0){\n push(e,cap);\n b[e.src]-=cap;\n b[e.dst]+=cap;\n }\n }\n }\n\n excess_vs.clear();\n deficit_vs.clear();\n for(int v=0;v<n;v++){\n if(b[v]>0) excess_vs.emplace_back(v);\n if(b[v]<0) deficit_vs.emplace_back(v);\n }\n }\n\n const Cost unreachable = std::numeric_limits<Cost>::max();\n Cost farthest;\n vector<Cost> dist;\n vector<Edge*> parent;\n\n struct P{\n Cost first;\n int second;\n P(Cost first,int second):first(first),second(second){}\n bool operator<(const P o)const{return first>o.first;}\n };\n\n priority_queue<P> pq;\n\n template<typename Predicate>\n void eliminate(vector<int> &vs,Predicate predicate){\n vs.erase(remove_if(begin(vs),end(vs),predicate),end(vs));\n }\n\n bool dual(const Flow delta){\n eliminate(excess_vs, [&](int v){return b[v]<+delta;});\n eliminate(deficit_vs,[&](int v){return b[v]>-delta;});\n\n dist.assign(n,unreachable);\n for(int v:excess_vs) pq.emplace(dist[v]=0,v);\n\n parent.assign(n,nullptr);\n auto emplace=[&](Edge& e){\n if(e.residual_cap()<delta) return;\n Cost nxt=dist[e.src]+residual_cost(e);\n if(nxt>=dist[e.dst]) return;\n pq.emplace(dist[e.dst]=nxt,e.dst);\n parent[e.dst]=&e;\n };\n\n farthest=0;\n int deficit_count=0;\n while(!pq.empty()){\n Cost d=pq.top().first;\n int v=pq.top().second;\n pq.pop();\n if(dist[v]<d) continue;\n farthest=d;\n\n if(b[v]<=-delta) deficit_count++;\n if(deficit_count>=(int)deficit_vs.size()) break;\n\n for(auto &e:G[v]) emplace(e);\n }\n pq=decltype(pq)();\n\n for(int v=0;v<n;v++)\n p[v]+=min(dist[v],farthest);\n\n return deficit_count>0;\n }\n\n void primal(const Flow delta){\n for(int t:deficit_vs){\n if(dist[t]>farthest) continue;\n Flow f=-b[t];\n int v;\n for(v=t;parent[v];v=parent[v]->src)\n chmin(f,parent[v]->residual_cap());\n chmin(f,b[v]);\n\n f-=f%delta;\n if(f<=0) continue;\n\n for(v=t;parent[v];){\n auto &e=*parent[v];\n push(e,f);\n int u=parent[v]->src;\n if(e.residual_cap()<=0) parent[v]=nullptr;\n v=u;\n }\n b[t]+=f;\n b[v]-=f;\n }\n }\n\n template<Flow SCALING_FACTOR=2>\n bool build(){\n p.resize(n);\n Flow max_flow=1;\n for(auto t:b) max_flow=max({max_flow,t,-t});\n for(auto &es:G)\n for(auto &e:es)\n max_flow=max({max_flow,e.residual_cap(),-e.residual_cap()});\n\n Flow delta=1;\n while(delta<max_flow) delta*=SCALING_FACTOR;\n for(;delta;delta/=SCALING_FACTOR){\n saturate_negative(delta);\n while(dual(delta)) primal(delta);\n }\n\n return excess_vs.empty() and deficit_vs.empty();\n }\n\n template<typename T=Cost>\n T get_cost(){\n T res=0;\n for(auto &es:G)\n for(auto &e:es)\n res+=T(e.flow)*T(e.cost)/T(objective);\n return res/T(2);\n }\n template<typename T=Cost> T get_gain(){return get_cost();}\n\n vector<Cost> get_potential(){\n fill(p.begin(),p.end(),0);\n for(int i=0;i<n;i++)\n for(auto &es:G)\n for(auto &e:es)\n if(e.residual_cap()>0)\n chmin(p[e.dst],p[e.src]+e.cost);\n return p;\n }\n};\n\ntemplate<typename Flow, typename Cost>\nusing MaxGainFlow = MinCostFlow<Flow, Cost, Objective::MAXIMIZE>;\n//END CUT HERE\n#ifndef call_from_test\n//INSERT ABOVE HERE\nsigned main(){\n return 0;\n}\n#endif\n\n#undef call_from_test\n\nsigned main(){\n using P = pair<int, int>;\n using ll = long long;\n const ll INF = (1<<30)-1;\n\n int n,m;\n cin>>n>>m;\n vector< vector<P> > H(n);\n MinCostFlow<ll, ll> G(n);\n for(int i=0;i<m;i++){\n int x,y,s;\n cin>>x>>y>>s;\n H[x].emplace_back(y,s);\n G.add_edge(y,x,1,INF,-s);\n }\n\n vector<int> dp(n,0);\n for(int i=0;i<n;i++)\n for(auto e:H[i])\n chmax(dp[e.first],dp[i]+e.second);\n G.add_edge(0,n-1,0,INF,dp[n-1]);\n\n assert(G.build());\n cout<<G.get_cost()<<endl;\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3540, "score_of_the_acc": -0.0121, "final_rank": 2 }, { "submission_id": "aoj_2230_4867569", "code_snippet": "#define PROBLEM \"http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2230\"\n\n#include<bits/stdc++.h>\nusing namespace std;\n\n#define call_from_test\n\n#ifndef call_from_test\n#include <bits/stdc++.h>\nusing namespace std;\n#endif\n//BEGIN CUT HERE\ntemplate<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}\ntemplate<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}\n//END CUT HERE\n#ifndef call_from_test\nsigned main(){\n return 0;\n}\n#endif\n\n\n#ifndef call_from_test\n#include <bits/stdc++.h>\nusing namespace std;\n#endif\n//BEGIN CUT HERE\n// O(m^2 \\log n \\log U)\n// U: maximum capacity\nenum Objective{\n MINIMIZE = +1,\n MAXIMIZE = -1,\n};\ntemplate<typename Flow, typename Cost,\n Objective objective = Objective::MINIMIZE>\nstruct MinCostFlow{\n template<typename T> inline void chmin(T &x,T y){x=min(x,y);}\n\n struct Edge{\n int src,dst;\n Flow flow,cap;\n Cost cost;\n int rev;\n Edge(int src,int dst,Flow cap,Cost cost,int rev):\n src(src),dst(dst),flow(0),cap(cap),cost(cost),rev(rev){}\n Flow residual_cap()const{return cap-flow;}\n };\n\n struct EdgePtr{\n int v,e;\n EdgePtr(int v,int e):v(v),e(e){}\n };\n\n int n;\n vector<vector<Edge>> G;\n vector<Flow> b;\n vector<Cost> p;\n\n MinCostFlow(int n):n(n),G(n),b(n,0){}\n\n EdgePtr add_edge(int src,int dst,Flow lower,Flow upper,Cost cost){\n int e=G[src].size();\n int r=(src==dst?e+1:G[dst].size());\n assert(lower<=upper);\n G[src].emplace_back(src,dst,+upper,+cost*objective,r);\n G[dst].emplace_back(dst,src,-lower,-cost*objective,e);\n return EdgePtr(src,e);\n }\n\n const Edge &get_edge(EdgePtr ep)const{return G[ep.v][ep.e];}\n\n void push(Edge &e,Flow amount){\n e.flow+=amount;\n G[e.dst][e.rev].flow-=amount;\n }\n\n void add_supply(int v,Flow amount){b[v]+=amount;}\n void add_demand(int v,Flow amount){b[v]-=amount;}\n\n Cost residual_cost(const Edge &e){\n return e.cost+p[e.src]-p[e.dst];\n }\n\n vector<int> excess_vs,deficit_vs;\n void saturate_negative(const Flow delta){\n for(auto &es:G){\n for(auto &e:es){\n Flow cap=e.residual_cap();\n cap-=cap%delta;\n if(cap<0 or residual_cost(e)<0){\n push(e,cap);\n b[e.src]-=cap;\n b[e.dst]+=cap;\n }\n }\n }\n\n excess_vs.clear();\n deficit_vs.clear();\n for(int v=0;v<n;v++){\n if(b[v]>0) excess_vs.emplace_back(v);\n if(b[v]<0) deficit_vs.emplace_back(v);\n }\n }\n\n const Cost unreachable = std::numeric_limits<Cost>::max();\n Cost farthest;\n vector<Cost> dist;\n vector<Edge*> parent;\n\n struct P{\n Cost first;\n int second;\n P(Cost first,int second):first(first),second(second){}\n bool operator<(const P o)const{return first>o.first;}\n };\n\n priority_queue<P> pq;\n\n template<typename Predicate>\n void eliminate(vector<int> &vs,Predicate predicate){\n vs.erase(remove_if(begin(vs),end(vs),predicate),end(vs));\n }\n\n bool dual(const Flow delta){\n eliminate(excess_vs, [&](int v){return b[v]<+delta;});\n eliminate(deficit_vs,[&](int v){return b[v]>-delta;});\n\n dist.assign(n,unreachable);\n for(int v:excess_vs) pq.emplace(dist[v]=0,v);\n\n parent.assign(n,nullptr);\n auto emplace=[&](Edge& e){\n if(e.residual_cap()<delta) return;\n Cost nxt=dist[e.src]+residual_cost(e);\n if(nxt>=dist[e.dst]) return;\n pq.emplace(dist[e.dst]=nxt,e.dst);\n parent[e.dst]=&e;\n };\n\n farthest=0;\n int deficit_count=0;\n while(!pq.empty()){\n Cost d=pq.top().first;\n int v=pq.top().second;\n pq.pop();\n if(dist[v]<d) continue;\n farthest=d;\n\n if(b[v]<=-delta) deficit_count++;\n if(deficit_count>=(int)deficit_vs.size()) break;\n\n for(auto &e:G[v]) emplace(e);\n }\n pq=decltype(pq)();\n\n for(int v=0;v<n;v++)\n p[v]+=min(dist[v],farthest);\n\n return deficit_count>0;\n }\n\n void primal(const Flow delta){\n for(int t:deficit_vs){\n if(dist[t]>farthest) continue;\n Flow f=-b[t];\n int v;\n for(v=t;parent[v];v=parent[v]->src)\n chmin(f,parent[v]->residual_cap());\n chmin(f,b[v]);\n\n f-=f%delta;\n if(f<=0) continue;\n\n for(v=t;parent[v];){\n auto &e=*parent[v];\n push(e,f);\n int u=parent[v]->src;\n if(e.residual_cap()<=0) parent[v]=nullptr;\n v=u;\n }\n b[t]+=f;\n b[v]-=f;\n }\n }\n\n template<Flow SCALING_FACTOR=2>\n bool build(){\n p.resize(n);\n Flow max_flow=1;\n for(auto t:b) max_flow=max({max_flow,t,-t});\n for(auto &es:G)\n for(auto &e:es)\n max_flow=max({max_flow,e.residual_cap(),-e.residual_cap()});\n\n Flow delta=1;\n while(delta<max_flow) delta*=SCALING_FACTOR;\n for(;delta;delta/=SCALING_FACTOR){\n saturate_negative(delta);\n while(dual(delta)) primal(delta);\n }\n\n return excess_vs.empty() and deficit_vs.empty();\n }\n\n template<typename T=Cost>\n T get_cost(){\n T res=0;\n for(auto &es:G)\n for(auto &e:es)\n res+=T(e.flow)*T(e.cost)/T(objective);\n return res/T(2);\n }\n template<typename T=Cost> T get_gain(){return get_cost();}\n\n vector<Cost> get_potential(){\n fill(p.begin(),p.end(),0);\n for(int i=0;i<n;i++)\n for(auto &es:G)\n for(auto &e:es)\n if(e.residual_cap()>0)\n chmin(p[e.dst],p[e.src]+e.cost);\n return p;\n }\n};\n\ntemplate<typename Flow, typename Cost>\nusing MaxGainFlow = MinCostFlow<Flow, Cost, Objective::MAXIMIZE>;\n//END CUT HERE\n#ifndef call_from_test\n//INSERT ABOVE HERE\nsigned main(){\n return 0;\n}\n#endif\n\n#undef call_from_test\n\nsigned main(){\n using P = pair<int, int>;\n using ll = long long;\n const ll INF = 1<<30;\n\n int n,m;\n cin>>n>>m;\n vector< vector<P> > H(n);\n MinCostFlow<ll, ll> G(n);\n for(int i=0;i<m;i++){\n int x,y,s;\n cin>>x>>y>>s;\n H[x].emplace_back(y,s);\n G.add_edge(y,x,1,INF,-s);\n }\n\n vector<int> dp(n,0);\n for(int i=0;i<n;i++)\n for(auto e:H[i])\n chmax(dp[e.first],dp[i]+e.second);\n G.add_edge(0,n-1,0,INF,dp[n-1]);\n\n assert(G.build());\n cout<<G.get_cost()<<endl;\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3508, "score_of_the_acc": -0.0107, "final_rank": 1 }, { "submission_id": "aoj_2230_4448943", "code_snippet": "// #define _GLIBCXX_DEBUG // for STL debug (optional)\n#include <iostream>\n#include <iomanip>\n#include <cstdio>\n#include <string>\n#include <cstring>\n#include <deque>\n#include <list>\n#include <queue>\n#include <stack>\n#include <vector>\n#include <utility>\n#include <algorithm>\n#include <map>\n#include <set>\n#include <complex>\n#include <cmath>\n#include <limits>\n#include <cfloat>\n#include <climits>\n#include <ctime>\n#include <cassert>\n#include <numeric>\n#include <fstream>\n#include <functional>\n#include <bitset>\nusing namespace std;\nusing ll = long long int;\nusing int64 = long long int;\n \ntemplate<typename T> void chmax(T &a, T b) {a = max(a, b);}\ntemplate<typename T> void chmin(T &a, T b) {a = min(a, b);}\ntemplate<typename T> void chadd(T &a, T b) {a = a + b;}\n \nint dx[] = {0, 0, 1, -1};\nint dy[] = {1, -1, 0, 0};\nconst int INF = 1LL << 29;\nconst ll LONGINF = 1LL << 60;\nconst ll MOD = 1000000007LL;\n\n// Ford-Fulkerson 法による 最大流 O( F |E| )\n// Bellman-Ford 法による 最小費用流 O( F |V| |E| )\n// [条件に注意] Dijkstra 法による 最小費用流 O( F |E| log |V| )\n\ntemplate <typename CapTp=int, typename CostTp=int>\nstruct Edge {\n int to, rev;\n CapTp cap; CostTp cost;\n bool is_rev;\n Edge(int t, bool f, int r, CapTp ca, CostTp co=0)\n : to(t), rev(r), cap(ca), cost(co), is_rev(f) {}\n};\n\ntemplate <typename CapTp=int, typename CostTp=int>\nstruct Flow {\n using Graph = vector< vector< Edge<CapTp, CostTp> > >;\n Graph G; const CapTp IA; const CostTp IO, NIL;\n vector< pair<int, int> > r_edges;\n Flow(int N_, CapTp IA_=1<<29, CostTp IO_=1<<29, CostTp NIL_=-1)\n : G(N_), IA(IA_), IO(IO_), NIL(NIL_), r_edges() {}\n // 辺を追加 (from -> to に流量 ca, コスト co)\n void add_edge(int from, int to, CapTp ca, CostTp co=0) {\n G[from].emplace_back(to, false, G[to].size(), ca, co);\n G[to].emplace_back(from, true, G[from].size() - 1, 0, -co);\n r_edges.emplace_back(to, G[to].size() - 1);\n }\n // k 番目の辺にいくつ流れたか\n CapTp get_flowed_cap(size_t k) {\n if(r_edges.size() <= k) return -1;\n int v, i; tie(v, i) = r_edges[k];\n return G[v][i].cap;\n }\n // s -> t 最大流\n CapTp max_flow(int s, int t) {\n vector<bool> used(G.size());\n auto dfs = [&](auto &&func, int v, int t, CapTp f) -> CapTp {\n if(v == t) return f;\n used[v] = true;\n for(auto &e : G[v]) {\n if(used[e.to] or e.cap == 0) continue;\n CapTp d = func(func, e.to, t, min(f, e.cap));\n if(d == 0) continue;\n e.cap -= d; G[e.to][e.rev].cap += d;\n return d;\n }\n return 0;\n };\n\n CapTp res(0);\n while(true) {\n fill(used.begin(), used.end(), false);\n CapTp delta = dfs(dfs, s, t, IA);\n if(delta == 0) return res;\n res += delta;\n }\n }\n // ベルマンフォードをつかって最小費用流\n CostTp mincost_flow(int s, int t, CapTp f) {\n vector<CostTp> dist(G.size()); CostTp res(0);\n vector<int> prevv(G.size()), preve(G.size());\n while(f > 0) {\n fill(dist.begin(), dist.end(), IO);\n dist[s] = 0;\n while(1) {\n bool upd = false;\n for(int v=0; v<(int)G.size(); v++) {\n if(dist[v] == IO) continue;\n for(size_t i=0; i<G[v].size(); i++) {\n auto &e = G[v][i];\n if(e.cap == 0 or dist[e.to] <= dist[v] + e.cost) continue;\n dist[e.to] = dist[v] + e.cost;\n prevv[e.to] = v, preve[e.to] = i;\n upd = true;\n }\n }\n if(!upd) break;\n }\n\n if(dist[t] == IO) return NIL;\n CapTp d = f;\n for(int v=t; v!=s; v=prevv[v]) d = min(d, G[prevv[v]][preve[v]].cap);\n f -= d; res += d * dist[t];\n for(int v=t; v!=s; v=prevv[v]) {\n auto &e = G[prevv[v]][preve[v]];\n e.cap -= d, G[v][e.rev].cap += d;\n }\n }\n return res;\n }\n // ポテンシャルの導入により、ダイクストラ法で最小費用流を解く\n // [仮定している条件]\n // 1. グラフに負の閉路が存在しない (流量の 0 初期化のため)\n // もし存在するならベルマンフォードで負の閉路を見つけ\n // そこに流せるだけ流してスタート\n // 2. グラフに負の辺が存在しない (pot_0 の計算可能性)\n // もし存在する場合は最初のみベルマンフォードを使う必要あり\n CostTp fast_mincost_flow(int s, int t, CapTp f) {\n CostTp res = 0;\n vector<CostTp> dist(G.size()), pot(G.size());\n vector<int> prevv(G.size()), preve(G.size());\n while(f > 0) {\n using PT = pair<CostTp, int>;\n priority_queue< PT, vector<PT>, greater<PT> > que;\n fill(dist.begin(), dist.end(), IO);\n\n dist[s] = 0;\n que.push(make_pair(0, s));\n while(!que.empty()) {\n PT cur = que.top(); que.pop();\n int v = cur.second;\n if(dist[v] < cur.first) continue;\n for(size_t i=0; i<G[v].size(); i++) {\n auto& e = G[v][i];\n if(e.cap > 0 and dist[e.to] > dist[v] + e.cost + pot[v] - pot[e.to]) {\n dist[e.to] = dist[v] + e.cost + pot[v] - pot[e.to];\n prevv[e.to] = v;\n preve[e.to] = i;\n que.push(make_pair(dist[e.to], e.to));\n }\n }\n }\n if(dist[t] == IO) {\n return NIL;\n }\n for(int v=0; v<(int)G.size(); v++) pot[v] += dist[v];\n\n CapTp d = f;\n for(int v=t; v!=s; v=prevv[v]) {\n d = min(d, G[prevv[v]][preve[v]].cap);\n }\n f -= d;\n res += d * pot[t];\n for(int v=t; v!=s; v=prevv[v]) {\n auto& e = G[prevv[v]][preve[v]];\n e.cap -= d;\n G[v][e.rev].cap += d;\n }\n }\n return res;\n } \n};\n\ntemplate <typename cap_tp=int, typename cost_tp=int,\n template<typename, typename> class flow_fw=Flow>\nstruct GeneralizedMincostFlow {\nprivate:\n flow_fw<cap_tp, cost_tp> fl;\n vector<cap_tp> D;\n int N, source, sink;\n cap_tp Z_cap;\n cost_tp Z_cost, ofs, ng;\n\npublic:\n GeneralizedMincostFlow(int N_, cost_tp ng_=-1,\n cap_tp zero_cap=0, cost_tp zero_cost=0)\n : fl(N_+2), D(N_), N(N_), source(N_), sink(N_+1),\n Z_cap(zero_cap), Z_cost(zero_cost), ofs(zero_cost), ng(ng_) {}\n\n void add_edge(int u, int v, cap_tp low, cap_tp high, cost_tp cost) {\n assert(low <= high);\n // コストが負の場合\n if(cost < Z_cost) {\n // 流すだけ得なので high 流れたことにする\n ofs += high * cost;\n D[u] -= high; D[v] += high;\n // 目一杯流したのを high - low 分だけキャンセル可能\n if(high - low > Z_cap) {\n fl.add_edge(v, u, high - low, -cost);\n }\n }\n else {\n // v 側に low 流れたことにする\n ofs += low * cost;\n D[u] -= low; D[v] += low;\n // high - low 分の辺を張る\n if(high - low > Z_cap) {\n fl.add_edge(u, v, high - low, cost);\n }\n }\n }\n void add_edge(int u, int v, cap_tp cap, cost_tp cost) {\n add_edge(u, v, Z_cap, cap, cost);\n }\n void change_d(int u, cap_tp delta) {\n D[u] += delta;\n }\n \n cost_tp mincost_flow() {\n cap_tp sum_d = 0;\n for(int i=0; i<N; i++) {\n if(D[i] > 0) {\n fl.add_edge(source, i, D[i], 0);\n sum_d += D[i];\n }\n else {\n fl.add_edge(i, sink, -D[i], 0);\n }\n }\n\n cost_tp res = fl.mincost_flow(source, sink, sum_d);\n if(res == fl.NIL) return ng;\n else return ofs + res;\n }\n};\n\nint main() {\n int N, M; scanf(\"%d%d\", &N, &M);\n GeneralizedMincostFlow<ll, ll> fl(N);\n vector< vector< pair<int, int> > > G(N);\n for(int i=0; i<M; i++) {\n int u, v, s;\n scanf(\"%d%d%d\", &u, &v, &s);\n G[u].emplace_back(v, s);\n fl.add_edge(v, u, 1, INF, -s);\n }\n vector<int> dist(N);\n for(int i=0; i<N; i++) {\n for(auto e : G[i]) {\n int j, c; tie(j, c) = e;\n chmax(dist[j], dist[i] + c);\n }\n }\n fl.add_edge(0, N-1, 0, INF, dist[N-1]);\n \n printf(\"%d\\n\", fl.mincost_flow());\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3324, "score_of_the_acc": -0.3022, "final_rank": 15 }, { "submission_id": "aoj_2230_4447867", "code_snippet": "// #define _GLIBCXX_DEBUG // for STL debug (optional)\n#include <iostream>\n#include <iomanip>\n#include <cstdio>\n#include <string>\n#include <cstring>\n#include <deque>\n#include <list>\n#include <queue>\n#include <stack>\n#include <vector>\n#include <utility>\n#include <algorithm>\n#include <map>\n#include <set>\n#include <complex>\n#include <cmath>\n#include <limits>\n#include <cfloat>\n#include <climits>\n#include <ctime>\n#include <cassert>\n#include <numeric>\n#include <fstream>\n#include <functional>\n#include <bitset>\nusing namespace std;\nusing ll = long long int;\nusing int64 = long long int;\n \ntemplate<typename T> void chmax(T &a, T b) {a = max(a, b);}\ntemplate<typename T> void chmin(T &a, T b) {a = min(a, b);}\ntemplate<typename T> void chadd(T &a, T b) {a = a + b;}\n \nint dx[] = {0, 0, 1, -1};\nint dy[] = {1, -1, 0, 0};\nconst int INF = 1LL << 29;\nconst ll LONGINF = 1LL << 60;\nconst ll MOD = 1000000007LL;\n\n// Ford-Fulkerson 法による 最大流 O( F |E| )\n// Bellman-Ford 法による 最小費用流 O( F |V| |E| )\n// [条件に注意] Dijkstra 法による 最小費用流 O( F |E| log |V| )\n\ntemplate <typename CapTp=int, typename CostTp=int>\nstruct Edge {\n int to, rev;\n CapTp cap; CostTp cost;\n bool is_rev;\n Edge(int t, bool f, int r, CapTp ca, CostTp co=0)\n : to(t), rev(r), cap(ca), cost(co), is_rev(f) {}\n};\n\ntemplate <typename CapTp=int, typename CostTp=int>\nstruct Flow {\n using Graph = vector< vector< Edge<CapTp, CostTp> > >;\n Graph G; const CapTp IA; const CostTp IO, NIL;\n vector< pair<int, int> > r_edges;\n Flow(int N_, CapTp IA_=1<<29, CostTp IO_=1<<29, CostTp NIL_=-1)\n : G(N_), IA(IA_), IO(IO_), NIL(NIL_), r_edges() {}\n // 辺を追加 (from -> to に流量 ca, コスト co)\n void add_edge(int from, int to, CapTp ca, CostTp co=0) {\n G[from].emplace_back(to, false, G[to].size(), ca, co);\n G[to].emplace_back(from, true, G[from].size() - 1, 0, -co);\n r_edges.emplace_back(to, G[to].size() - 1);\n }\n // k 番目の辺にいくつ流れたか\n CapTp get_flowed_cap(size_t k) {\n if(r_edges.size() <= k) return -1;\n int v, i; tie(v, i) = r_edges[k];\n return G[v][i].cap;\n }\n // s -> t 最大流\n CapTp max_flow(int s, int t) {\n vector<bool> used(G.size());\n auto dfs = [&](auto &&func, int v, int t, CapTp f) -> CapTp {\n if(v == t) return f;\n used[v] = true;\n for(auto &e : G[v]) {\n if(used[e.to] or e.cap == 0) continue;\n CapTp d = func(func, e.to, t, min(f, e.cap));\n if(d == 0) continue;\n e.cap -= d; G[e.to][e.rev].cap += d;\n return d;\n }\n return 0;\n };\n\n CapTp res(0);\n while(true) {\n fill(used.begin(), used.end(), false);\n CapTp delta = dfs(dfs, s, t, IA);\n if(delta == 0) return res;\n res += delta;\n }\n }\n // ベルマンフォードをつかって最小費用流\n CostTp mincost_flow(int s, int t, CapTp f) {\n vector<CostTp> dist(G.size()); CostTp res(0);\n vector<int> prevv(G.size()), preve(G.size());\n while(f > 0) {\n fill(dist.begin(), dist.end(), IO);\n dist[s] = 0;\n while(1) {\n bool upd = false;\n for(int v=0; v<(int)G.size(); v++) {\n if(dist[v] == IO) continue;\n for(size_t i=0; i<G[v].size(); i++) {\n auto &e = G[v][i];\n if(e.cap == 0 or dist[e.to] <= dist[v] + e.cost) continue;\n dist[e.to] = dist[v] + e.cost;\n prevv[e.to] = v, preve[e.to] = i;\n upd = true;\n }\n }\n if(!upd) break;\n }\n\n if(dist[t] == IO) return NIL;\n CapTp d = f;\n for(int v=t; v!=s; v=prevv[v]) d = min(d, G[prevv[v]][preve[v]].cap);\n f -= d; res += d * dist[t];\n for(int v=t; v!=s; v=prevv[v]) {\n auto &e = G[prevv[v]][preve[v]];\n e.cap -= d, G[v][e.rev].cap += d;\n }\n }\n return res;\n }\n // ポテンシャルの導入により、ダイクストラ法で最小費用流を解く\n // [仮定している条件]\n // 1. グラフに負の閉路が存在しない (流量の 0 初期化のため)\n // もし存在するならベルマンフォードで負の閉路を見つけ\n // そこに流せるだけ流してスタート\n // 2. グラフに負の辺が存在しない (pot_0 の計算可能性)\n // もし存在する場合は最初のみベルマンフォードを使う必要あり\n CostTp fast_mincost_flow(int s, int t, CapTp f) {\n CostTp res = 0;\n vector<CostTp> dist(G.size()), pot(G.size());\n vector<int> prevv(G.size()), preve(G.size());\n while(f > 0) {\n using PT = pair<CostTp, int>;\n priority_queue< PT, vector<PT>, greater<PT> > que;\n fill(dist.begin(), dist.end(), IO);\n\n dist[s] = 0;\n que.push(make_pair(0, s));\n while(!que.empty()) {\n PT cur = que.top(); que.pop();\n int v = cur.second;\n if(dist[v] < cur.first) continue;\n for(size_t i=0; i<G[v].size(); i++) {\n auto& e = G[v][i];\n if(e.cap > 0 and dist[e.to] > dist[v] + e.cost + pot[v] - pot[e.to]) {\n dist[e.to] = dist[v] + e.cost + pot[v] - pot[e.to];\n prevv[e.to] = v;\n preve[e.to] = i;\n que.push(make_pair(dist[e.to], e.to));\n }\n }\n }\n if(dist[t] == IO) {\n return NIL;\n }\n for(int v=0; v<(int)G.size(); v++) pot[v] += dist[v];\n\n CapTp d = f;\n for(int v=t; v!=s; v=prevv[v]) {\n d = min(d, G[prevv[v]][preve[v]].cap);\n }\n f -= d;\n res += d * pot[t];\n for(int v=t; v!=s; v=prevv[v]) {\n auto& e = G[prevv[v]][preve[v]];\n e.cap -= d;\n G[v][e.rev].cap += d;\n }\n }\n return res;\n } \n};\n\ntemplate <typename cap_tp=int, typename cost_tp=int,\n template<typename, typename> class flow_fw=Flow>\nstruct GeneralizedMincostFlow {\nprivate:\n flow_fw<cap_tp, cost_tp> fl;\n vector<cap_tp> D;\n int N, source, sink;\n cap_tp Z_cap;\n cost_tp Z_cost, ofs, ng;\n\npublic:\n GeneralizedMincostFlow(int N_, cost_tp ng_=-1,\n cap_tp zero_cap=0, cost_tp zero_cost=0)\n : fl(N_+2), D(N_), N(N_), source(N_), sink(N_+1),\n Z_cap(zero_cap), Z_cost(zero_cost), ofs(zero_cost), ng(ng_) {}\n\n void add_edge(int u, int v, cap_tp low, cap_tp high, cost_tp cost) {\n assert(low <= high);\n // コストが負の場合\n if(cost < Z_cost) {\n // 流すだけ得なので high 流れたことにする\n ofs += high * cost;\n D[u] -= high; D[v] += high;\n // 目一杯流したのを high - low 分だけキャンセル可能\n if(high - low > Z_cap) {\n fl.add_edge(v, u, high - low, -cost);\n }\n }\n else {\n // v 側に low 流れたことにする\n ofs += low * cost;\n D[u] -= low; D[v] += low;\n // high - low 分の辺を張る\n if(high - low > Z_cap) {\n fl.add_edge(u, v, high - low, cost);\n }\n }\n }\n void add_edge(int u, int v, cap_tp cap, cost_tp cost) {\n add_edge(u, v, Z_cap, cap, cost);\n }\n void change_d(int u, cap_tp delta) {\n D[u] += delta;\n }\n \n cost_tp mincost_flow() {\n cap_tp sum_d = 0;\n for(int i=0; i<N; i++) {\n if(D[i] > 0) {\n fl.add_edge(source, i, D[i], 0);\n sum_d += D[i];\n }\n else {\n fl.add_edge(i, sink, -D[i], 0);\n }\n }\n\n cost_tp res = fl.mincost_flow(source, sink, sum_d);\n if(res == fl.NIL) return ng;\n else return ofs + res;\n }\n};\n\nint main() {\n int N, M; scanf(\"%d%d\", &N, &M);\n GeneralizedMincostFlow<ll, ll> fl(N);\n vector< vector< pair<int, int> > > G(N);\n for(int i=0; i<M; i++) {\n int u, v, s;\n scanf(\"%d%d%d\", &u, &v, &s);\n G[u].emplace_back(v, s);\n fl.add_edge(u, v, 1, INF, -s);\n }\n vector<int> dist(N);\n for(int i=0; i<N; i++) {\n for(auto e : G[i]) {\n int j, c; tie(j, c) = e;\n chmax(dist[j], dist[i] + c);\n }\n }\n fl.add_edge(N-1, 0, 0, INF, dist[N-1]);\n \n printf(\"%d\\n\", fl.mincost_flow());\n return 0;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 3324, "score_of_the_acc": -0.4022, "final_rank": 16 }, { "submission_id": "aoj_2230_4308671", "code_snippet": "#include <bits/stdc++.h>\n \n// #include <boost/multiprecision/cpp_int.hpp>\n #define int long long\n #define inf 1000000007\n #define pa pair<int,int>\n #define ll long long\n #define pal pair<double,double>\n #define ppap pair<pa,int>\n #define PI 3.14159265358979323846\n #define paa pair<int,char>\n #define mp make_pair\n #define pb push_back\n #define EPS (1e-8)\n \n int dx[8]={0,1,0,-1,1,1,-1,-1};\n int dy[8]={1,0,-1,0,-1,1,1,-1};\n using namespace std;\n \t\t\tclass pa3{\n \tpublic:\n \tint x;\n \t\t\t\tint y,z;\n \tpa3(int x=0,int y=0,int z=0):x(x),y(y),z(z) {}\n \tbool operator < (const pa3 &p) const{\n \t\tif(x!=p.x) return x<p.x;\n \t\tif(y!=p.y) return y<p.y;\n \t\t return z<p.z;\n \t\t//return x != p.x ? x<p.x: y<p.y;\n \t}\n \t\t\t\tbool operator > (const pa3 &p) const{\n \t\tif(x!=p.x) return x>p.x;\n \t\tif(y!=p.y) return y>p.y;\n \t\t return z>p.z;\n \t\t//return x != p.x ? x<p.x: y<p.y;\n \t}\n \tbool operator == (const pa3 &p) const{\n \t\treturn x==p.x && y==p.y && z==p.z;\n \t}\n \t\tbool operator != (const pa3 &p) const{\n \t\t\treturn !( x==p.x && y==p.y && z==p.z);\n \t}\n \n };\n \n class pa4{\n \tpublic:\n \tint x;\n \tint y,z,w;\n \tpa4(int x=0,int y=0,int z=0,int w=0):x(x),y(y),z(z),w(w) {}\n \tbool operator < (const pa4 &p) const{\n \t\tif(x!=p.x) return x<p.x;\n \t\tif(y!=p.y) return y<p.y;\n \t\tif(z!=p.z)return z<p.z;\n \t\treturn w<p.w;\n \t\t//return x != p.x ? x<p.x: y<p.y;\n \t}\n \tbool operator > (const pa4 &p) const{\n \t\tif(x!=p.x) return x>p.x;\n \t\tif(y!=p.y) return y>p.y;\n \t\tif(z!=p.z)return z>p.z;\n \t\treturn w>p.w;\n \t\t//return x != p.x ? x<p.x: y<p.y;\n \t}\n \tbool operator == (const pa4 &p) const{\n \t\treturn x==p.x && y==p.y && z==p.z &&w==p.w;\n \t}\n \t\t\n \n };\n class pa2{\n \tpublic:\n \tint x,y;\n \tpa2(int x=0,int y=0):x(x),y(y) {}\n \tpa2 operator + (pa2 p) {return pa2(x+p.x,y+p.y);}\n \tpa2 operator - (pa2 p) {return pa2(x-p.x,y-p.y);}\n \tbool operator < (const pa2 &p) const{\n \t\treturn y != p.y ? y<p.y: x<p.x;\n \t}\n \tbool operator > (const pa2 &p) const{\n \t\treturn x != p.x ? x<p.x: y<p.y;\n \t}\n \tbool operator == (const pa2 &p) const{\n \t\treturn abs(x-p.x)==0 && abs(y-p.y)==0;\n \t}\n \tbool operator != (const pa2 &p) const{\n \t\treturn !(abs(x-p.x)==0 && abs(y-p.y)==0);\n \t}\n \t\t\n \n };\n \n \n \n string itos( int i ) {\n ostringstream s ;\n s << i ;\n return s.str() ;\n }\n \n int gcd(int v,int b){\n \tif(v==0) return b;\n \tif(b==0) return v;\n \tif(v>b) return gcd(b,v);\n \tif(v==b) return b;\n \tif(b%v==0) return v;\n \treturn gcd(v,b%v);\n }\n \n \n int mod;\nint extgcd(int a, int b, int &x, int &y) {\n if (b == 0) {\n x = 1;\n y = 0;\n return a;\n }\n int d = extgcd(b, a%b, y, x);\n y -= a/b * x;\n return d;\n}\npa operator+(const pa & l,const pa & r) { \n return {l.first+r.first,l.second+r.second}; \n} \npa operator-(const pa & l,const pa & r) { \n return {l.first-r.first,l.second-r.second}; \n} \n int beki(int wa,int rr,int warukazu){\n \tif(rr==0) return 1%warukazu;\n \tif(rr==1) return wa%warukazu;\n \twa%=warukazu;\n \tif(rr%2==1) return ((ll)beki(wa,rr-1,warukazu)*(ll)wa)%warukazu;\n \tll zx=beki(wa,rr/2,warukazu);\n \treturn (zx*zx)%warukazu;\n }\n \n \n int pr[1000100];\n int inv[1000010];\n \n\n \n \t\t\tint comb(int nn,int rr){\n \t\t\t\tif(rr<0 || rr>nn || nn<0) return 0;\n \t\t\t\tint r=pr[nn]*inv[rr];\n \t\t\t\tr%=mod;\n \t\t\t\tr*=inv[nn-rr];\n \t\t\t\tr%=mod;\n \t\t\t\treturn r;\n \t\t\t}\n \n void gya(int ert){\n \tpr[0]=1;\n \tfor(int i=1;i<=ert;i++){\n \t\tpr[i]=((ll)pr[i-1]*i)%mod;\n \t}\n \t\tinv[ert]=beki(pr[ert],mod-2,mod);\n \tfor(int i=ert-1;i>=0;i--){\n \t\tinv[i]=(ll)inv[i+1]*(i+1)%mod;\n \t}\n }\n \n // cin.tie(0);\n \t\t//\tios::sync_with_stdio(false);\n \t\t\t//priority_queue<pa3,vector<pa3>,greater<pa3>> pq; \n //sort(ve.begin(),ve.end(),greater<int>());\n // mt19937(clock_per_sec);\n // mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()) ;\n\ntypedef pair<int,int> P;\nstruct edge{ int to,cap,cost,rev;};\nvector<edge> G[100010];\nbool used[100010];\nint h[100010],dist[100010],prevv[100010],preve[100010];\nint V;//Vは頂点数\n\nvoid add_edge(int from,int to,int cap,int cost){\n\tG[from].push_back( (edge){to,cap,cost,(int)G[to].size()} );\n\t\tG[to].push_back( (edge){from,0,-cost,(int)G[from].size()-1 });\n}\n\nint min_cost_flow(int s,int t,int f){\n\tint res=0;\n\tfill(h,h+V,0);\n\twhile(f>0){\n\t\tpriority_queue <P,vector<P>,greater<P> > que;\n\t\tfill(dist,dist+V,inf);\n\t\tdist[s]=0;\n\t\tque.push(P(0,s));\n\t\twhile( !que.empty() ){\n\t\t\tP p=que.top();que.pop();\n\t\t\tint v=p.second;\n\t\t\tif(dist[v]<p.first) continue;\n\t\t\tfor(int i=0;i<(int)G[v].size();i++){\n\t\t\t\tedge &e=G[v][i];\n\t\t\t\tif(e.cap>0 && dist[e.to]>dist[v]+e.cost+h[v]-h[e.to]){\n\t\t\t\t\tdist[e.to]=dist[v]+e.cost+h[v]-h[e.to];\n\t\t\t\t\tprevv[e.to]=v;\n\t\t\t\t\tpreve[e.to]=i;\n\t\t\t\t\tque.push(P(dist[e.to],e.to));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\tif(dist[t]==inf) return -1;\n\tfor(int v=0;v<V;v++)h[v] += dist[v];\n\tint d=f;\n\tfor(int v=t;v!=s;v=prevv[v]){\n\t\td=min(d,G[prevv[v]][preve[v]].cap);\n\t}\n\tf -= d;\n\tres += d*h[t];\n\tfor(int v=t; v!=s;v=prevv[v]){\n\t\tedge &e =G[prevv[v]][preve[v]];\n\t\te.cap -=d;\n\t\tG[v][e.rev].cap +=d;\n\t}\n}\nreturn res;\n}\n\nvector<pa> F[2020];\nint dis[2020]={};\nint wei[1021]={};\nsigned main(){\ncin.tie(0);\nios::sync_with_stdio(false);\n\n\tint n,m;\n\tcin>>n>>m;\n\tV=n+100;\n\tint Q=30000;\n\tint W=0;\n\tfor(int i=0;i<m;i++){\n\t\tint u,v,w;\n\t\tcin>>u>>v>>w;\n\t\tW+=w;\n\t\twei[u]++;\n\t\twei[v]--;\n\t\tF[v].pb(mp(u,w));\n\t\twei[u]+=Q;\n\t\twei[v]-=Q;\n\t\tadd_edge(u,v,Q,w);\n\t\n\t}\n\t\n\t\n\tdis[0]=0;\n\tfor(int i=1;i<n;i++){\n\t\tfor(auto v:F[i])dis[i]=max(dis[i],dis[v.first]+v.second);\n\t}\n\t\n\tadd_edge(0,n-1,Q,dis[n-1]);\n\tint s=0;\n\tfor(int i=0;i<n;i++){\n\t\tif(wei[i]>0){\n\t\t\tadd_edge(n,i,wei[i],0);\n\t\t\ts+=wei[i];\n\t\t}\n\t\t\n\t\tif(wei[i]<0) add_edge(i,n+1,-wei[i],0);\n\t\t\n\t}\n\tcout<<min_cost_flow(n,n+1,s)-Q*W-W<<endl;\n\t//int ans=-(min_cost_flow(n,n+1,s)+W);\n\t//cout<<ans<<endl;\n\t\n\t\n\t\treturn 0;\n }", "accuracy": 1, "time_ms": 20, "memory_kb": 5692, "score_of_the_acc": -0.2109, "final_rank": 14 }, { "submission_id": "aoj_2230_4224563", "code_snippet": "#define _USE_MATH_DEFINES\n#include <bits/stdc++.h>\nusing namespace std;\n#define FOR(i,m,n) for(int i=(m);i<(n);++i)\n#define REP(i,n) FOR(i,0,n)\n#define ALL(v) (v).begin(),(v).end()\nusing ll = long long;\nconst int INF = 0x3f3f3f3f;\nconst ll LINF = 0x3f3f3f3f3f3f3f3fLL;\nconst double EPS = 1e-8;\nconst int MOD = 1000000007;\n// const int MOD = 998244353;\nconst int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};\nconst int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1};\ntemplate <typename T, typename U> inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; }\ntemplate <typename T, typename U> inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; }\nstruct IOSetup {\n IOSetup() {\n cin.tie(nullptr);\n ios_base::sync_with_stdio(false);\n cout << fixed << setprecision(20);\n }\n} iosetup;\n\ntemplate <typename T, typename U>\nstruct PrimalDual2 {\n using Pui = pair<U, int>;\n\n struct Edge {\n int dst, rev;\n T cap;\n U cost;\n Edge(int dst, T cap, U cost, int rev) : dst(dst), cap(cap), cost(cost), rev(rev) {}\n };\n\n vector<vector<Edge> > graph;\n\n PrimalDual2(int n, const T TINF, const U UINF) : n(n), UINF(UINF), graph(n + 2), d(n + 2, 0) {}\n\n void add_edge(int src, int dst, T cap, U cost) {\n if (cost < 0) {\n d[src] -= cap;\n d[dst] += cap;\n res += cost * cap;\n swap(src, dst);\n cost = -cost;\n }\n graph[src].emplace_back(dst, cap, cost, graph[dst].size());\n graph[dst].emplace_back(src, 0, -cost, graph[src].size() - 1);\n }\n\n U minimum_cost_flow() {\n T flow = 0;\n REP(i, n) {\n if (d[i] > 0) {\n add_edge(n, i, d[i], 0);\n flow += d[i];\n } else if (d[i] < 0) {\n add_edge(i, n + 1, -d[i], 0);\n }\n }\n vector<int> prev_v(n + 2, -1), prev_e(n + 2, -1);\n vector<U> potential(n + 2, 0), dist(n + 2);\n priority_queue<Pui, vector<Pui>, greater<Pui> > que;\n while (flow > 0) {\n fill(ALL(dist), UINF);\n dist[n] = 0;\n que.emplace(0, n);\n while (!que.empty()) {\n U fst; int ver; tie(fst, ver) = que.top(); que.pop();\n if (dist[ver] < fst) continue;\n REP(i, graph[ver].size()) {\n Edge &e = graph[ver][i];\n U nx = dist[ver] + e.cost + potential[ver] - potential[e.dst];\n if (e.cap > 0 && dist[e.dst] > nx) {\n dist[e.dst] = nx;\n prev_v[e.dst] = ver;\n prev_e[e.dst] = i;\n que.emplace(dist[e.dst], e.dst);\n }\n }\n }\n if (dist[n + 1] == UINF) return UINF;\n REP(i, n + 2) {\n if (dist[i] != UINF) potential[i] += dist[i];\n }\n T f = flow;\n for (int v = n + 1; v != n; v = prev_v[v]) chmin(f, graph[prev_v[v]][prev_e[v]].cap);\n flow -= f;\n res += potential[n + 1] * f;\n for (int v = n + 1; v != n; v = prev_v[v]) {\n Edge &e = graph[prev_v[v]][prev_e[v]];\n e.cap -= f;\n graph[v][e.rev].cap += f;\n }\n }\n return res;\n }\n\n U minimum_cost_flow(int s, int t, T flow) {\n d[s] += flow;\n d[t] -= flow;\n return minimum_cost_flow();\n }\n\nprivate:\n int n;\n const U UINF;\n U res = 0;\n vector<T> d;\n};\n\ntemplate <template <typename, typename> class C, typename T, typename U>\nstruct MinimumCostFlowWithMinimumFlowConstraint {\n MinimumCostFlowWithMinimumFlowConstraint(int n, const U M, const T TINF, const U UINF) : M(M), UINF(UINF), pd(n, TINF, UINF) {}\n\n void add_edge(int src, int dst, T lb, T ub, U cost) {\n pd.add_edge(src, dst, ub - lb + 1, cost);\n pd.add_edge(src, dst, lb, cost - M);\n lb_sum += lb;\n }\n\n U solve(int s, int t, T flow) {\n U tmp = pd.minimum_cost_flow(s, t, flow);\n if (tmp == UINF) return UINF;\n return tmp + M * lb_sum;\n }\n\nprivate:\n const U M, UINF;\n T lb_sum = 0;\n C<T, U> pd;\n};\n\nusing CostType = ll;\nstruct Edge {\n int src, dst; CostType cost;\n Edge(int src, int dst, CostType cost = 0) : src(src), dst(dst), cost(cost) {}\n inline bool operator<(const Edge &rhs) const {\n return cost != rhs.cost ? cost < rhs.cost : dst != rhs.dst ? dst < rhs.dst : src < rhs.src;\n }\n inline bool operator<=(const Edge &rhs) const { return !(rhs < *this); }\n inline bool operator>(const Edge &rhs) const { return rhs < *this; }\n inline bool operator>=(const Edge &rhs) const { return !(*this < rhs); }\n};\n\nint main() {\n int n, m; cin >> n >> m;\n MinimumCostFlowWithMinimumFlowConstraint<PrimalDual2, ll, ll> pd(n, INF, INF, LINF);\n vector<vector<Edge> > graph(n);\n while (m--) {\n int x, y, s; cin >> x >> y >> s;\n pd.add_edge(y, x, 1, INF, -s);\n graph[x].emplace_back(x, y, s);\n }\n vector<ll> dp(n, 0);\n for (int i = n - 2; i >= 0; --i) {\n for (const Edge &e : graph[i]) chmax(dp[i], dp[e.dst] + e.cost);\n }\n pd.add_edge(0, n - 1, 0, INF, dp[0]);\n cout << pd.solve(0, 0, 0) << '\\n';\n return 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3276, "score_of_the_acc": -0.2, "final_rank": 12 }, { "submission_id": "aoj_2230_4048389", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\nusing int64 = long long;\nconst int mod = 1e9 + 7;\n// const int mod = 998244353;\n\nconst int64 infll = (1LL << 62) - 1;\nconst int inf = (1 << 30) - 1;\n\nstruct IoSetup {\n IoSetup() {\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(10);\n cerr << fixed << setprecision(10);\n }\n} iosetup;\n\n\ntemplate< typename T1, typename T2 >\nostream &operator<<(ostream &os, const pair< T1, T2 > &p) {\n os << p.first << \" \" << p.second;\n return os;\n}\n\ntemplate< typename T1, typename T2 >\nistream &operator>>(istream &is, pair< T1, T2 > &p) {\n is >> p.first >> p.second;\n return is;\n}\n\ntemplate< typename T >\nostream &operator<<(ostream &os, const vector< T > &v) {\n for(int i = 0; i < (int) v.size(); i++) {\n os << v[i] << (i + 1 != v.size() ? \" \" : \"\");\n }\n return os;\n}\n\ntemplate< typename T >\nistream &operator>>(istream &is, vector< T > &v) {\n for(T &in : v) is >> in;\n return is;\n}\n\ntemplate< typename T1, typename T2 >\ninline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }\n\ntemplate< typename T1, typename T2 >\ninline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }\n\ntemplate< typename T = int64 >\nvector< T > make_v(size_t a) {\n return vector< T >(a);\n}\n\ntemplate< typename T, typename... Ts >\nauto make_v(size_t a, Ts... ts) {\n return vector< decltype(make_v< T >(ts...)) >(a, make_v< T >(ts...));\n}\n\ntemplate< typename T, typename V >\ntypename enable_if< is_class< T >::value == 0 >::type fill_v(T &t, const V &v) {\n t = v;\n}\n\ntemplate< typename T, typename V >\ntypename enable_if< is_class< T >::value != 0 >::type fill_v(T &t, const V &v) {\n for(auto &e : t) fill_v(e, v);\n}\n\ntemplate< typename F >\nstruct FixPoint : F {\n FixPoint(F &&f) : F(forward< F >(f)) {}\n\n template< typename... Args >\n decltype(auto) operator()(Args &&... args) const {\n return F::operator()(*this, forward< Args >(args)...);\n }\n};\n\ntemplate< typename F >\ninline decltype(auto) MFP(F &&f) {\n return FixPoint< F >{forward< F >(f)};\n}\n\ntemplate< typename flow_t, typename cost_t >\nstruct edge {\n int src, to;\n flow_t low, high;\n cost_t cost;\n\n edge() = default;\n\n edge(int src, int to, flow_t high, cost_t cost) : src(src), to(to), low(0), high(high), cost(cost) {}\n\n edge(int src, int to, flow_t low, flow_t high, cost_t cost) : src(src), to(to), low(low), high(high), cost(cost) {}\n};\n\ntemplate< typename flow_t, typename cost_t, template< typename, typename > class MF, cost_t NG = -1 >\ncost_t normalized_min_cost_flow(vector< flow_t > D, const vector< edge< flow_t, cost_t > > &E) {\n const int N = (int) D.size(), M = (int) E.size();\n MF< flow_t, cost_t > flow(N + 2);\n const int S = N, T = N + 1;\n\n\n cost_t sum = 0;\n for(auto &e : E) {\n if(e.cost < 0) {\n sum += e.cost * e.high;\n D[e.src] -= e.high;\n D[e.to] += e.high;\n flow.add_edge(e.to, e.src, e.high - e.low, -e.cost);\n } else {\n sum += e.cost * e.low;\n D[e.src] -= e.low;\n D[e.to] += e.low;\n flow.add_edge(e.src, e.to, e.high - e.low, e.cost);\n }\n }\n\n flow_t in = 0, out = 0;\n for(int i = 0; i < N; i++) {\n if(D[i] > 0) {\n flow.add_edge(S, i, D[i], flow_t(0));\n in += D[i];\n } else if(D[i] < 0) {\n flow.add_edge(i, T, -D[i], flow_t(0));\n out += -D[i];\n }\n }\n\n assert(in == out);\n auto ret = flow.min_cost_flow(S, T, in);\n if(ret == -1) return NG;\n return ret + sum;\n}\n\ntemplate< typename flow_t, typename cost_t >\nstruct PrimalDual {\n const cost_t INF;\n\n struct edge {\n int to;\n flow_t cap;\n cost_t cost;\n int rev;\n bool isrev;\n };\n vector< vector< edge > > graph;\n vector< cost_t > potential, min_cost;\n vector< int > prevv, preve;\n\n PrimalDual(int V) : graph(V), INF(numeric_limits< cost_t >::max()) {}\n\n void add_edge(int from, int to, flow_t cap, cost_t cost) {\n graph[from].emplace_back((edge) {to, cap, cost, (int) graph[to].size(), false});\n graph[to].emplace_back((edge) {from, 0, -cost, (int) graph[from].size() - 1, true});\n }\n\n cost_t min_cost_flow(int s, int t, flow_t f) {\n int V = (int) graph.size();\n cost_t ret = 0;\n using Pi = pair< cost_t, int >;\n priority_queue< Pi, vector< Pi >, greater< Pi > > que;\n potential.assign(V, 0);\n preve.assign(V, -1);\n prevv.assign(V, -1);\n\n while(f > 0) {\n min_cost.assign(V, INF);\n que.emplace(0, s);\n min_cost[s] = 0;\n while(!que.empty()) {\n Pi p = que.top();\n que.pop();\n if(min_cost[p.second] < p.first) continue;\n for(int i = 0; i < graph[p.second].size(); i++) {\n edge &e = graph[p.second][i];\n cost_t nextCost = min_cost[p.second] + e.cost + potential[p.second] - potential[e.to];\n if(e.cap > 0 && min_cost[e.to] > nextCost) {\n min_cost[e.to] = nextCost;\n prevv[e.to] = p.second, preve[e.to] = i;\n que.emplace(min_cost[e.to], e.to);\n }\n }\n }\n if(min_cost[t] == INF) return -1;\n for(int v = 0; v < V; v++) potential[v] += min_cost[v];\n flow_t addflow = f;\n for(int v = t; v != s; v = prevv[v]) {\n addflow = min(addflow, graph[prevv[v]][preve[v]].cap);\n }\n f -= addflow;\n ret += addflow * potential[t];\n for(int v = t; v != s; v = prevv[v]) {\n edge &e = graph[prevv[v]][preve[v]];\n e.cap -= addflow;\n graph[v][e.rev].cap += addflow;\n }\n }\n return ret;\n }\n\n void output() {\n for(int i = 0; i < graph.size(); i++) {\n for(auto &e : graph[i]) {\n if(e.isrev) continue;\n auto &rev_e = graph[e.to][e.rev];\n cout << i << \"->\" << e.to << \" (flow: \" << rev_e.cap << \"/\" << rev_e.cap + e.cap << \")\" << endl;\n }\n }\n }\n};\n\n\nint main() {\n using flow_t = int64;\n using cost_t = int64;\n\n int N, M;\n cin >> N >> M;\n vector< vector< int > > g(N);\n vector< int > X(M), Y(M), S(M);\n for(int i = 0; i < M; i++) {\n cin >> X[i] >> Y[i] >> S[i];\n g[X[i]].emplace_back(i);\n }\n auto dp = make_v< int >(N);\n for(int i = 0; i < N; i++) {\n for(auto &j : g[i]) chmax(dp[Y[j]], dp[i] + S[j]);\n }\n\n vector< edge< flow_t, cost_t > > es;\n vector< flow_t > D(N);\n for(int i = 0; i < M; i++) es.emplace_back(Y[i], X[i], 1, inf, -S[i]);\n es.emplace_back(0, N - 1, 0, inf, dp[N - 1]);\n cout << normalized_min_cost_flow< flow_t, cost_t, PrimalDual >(D, es) << endl;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3324, "score_of_the_acc": -0.1022, "final_rank": 7 }, { "submission_id": "aoj_2230_3986453", "code_snippet": "#include <iostream>\n#include <vector>\n#include <queue>\n#include <map>\n#include <algorithm>\nusing namespace std;\ntypedef long long ll;\ntypedef pair<ll,ll> P;\nconst ll inf = 1e14;\n\nclass min_cost_flow{\nprivate:\n int N;\n struct edge{int to; ll cap,cost; int rev;};\n vector<vector<edge>> G;\n vector<ll> h,dist,prevv,preve;\npublic:\n min_cost_flow(int n){\n N = n;\n G = vector<vector<edge>>(N);\n h = dist = prevv = preve = vector<ll>(N,0);\n }\n void add_edge(int from,int to,ll cap,ll cost){\n G[from].push_back((edge){to,cap,cost,(int) G[to].size()});\n G[to].push_back((edge){from,0,-cost,(int) G[from].size()-1});\n }\n ll answer(int s,int t,ll f){\n ll res = 0;\n fill(h.begin(),h.end(),0);\n while(f>0){\n priority_queue<P,vector<P>,greater<P>> Q;\n fill(dist.begin(),dist.end(),inf);\n dist[s] = 0;\n Q.push(P(0,s));\n while(!Q.empty()){\n P p = Q.top(); Q.pop();\n int v = p.second;\n if(dist[v]<p.first) continue;\n for(int i=0;i<G[v].size();i++){\n edge &e = G[v][i];\n if(e.cap>0 && dist[e.to]>dist[v]+e.cost+h[v]-h[e.to]){\n dist[e.to] = dist[v]+e.cost+h[v]-h[e.to];\n prevv[e.to] = v;\n preve[e.to] = i;\n Q.push(P(dist[e.to],e.to));\n }\n }\n }\n if(dist[t]==inf) return -1;\n for(int v=0;v<N;v++) h[v] += dist[v];\n ll d = f;\n for(int v=t;v!=s; v=prevv[v]){\n d = min(d,G[prevv[v]][preve[v]].cap);\n }\n f -= d;\n res += d*h[t];\n for(int v=t;v!=s;v=prevv[v]){\n edge &e = G[prevv[v]][preve[v]];\n e.cap -= d;\n G[v][e.rev].cap += d;\n }\n }\n return res;\n }\n};\n\nstruct edge{\n ll s,t,d;\n};\n\nint main(){\n int N,M;\n cin >> N >> M;\n vector<vector<edge>> g(N);\n vector<edge> edges(M);\n for(int i=0;i<M;i++){\n int a,b,c;\n cin >> a >> b >> c;\n edges[i] = {a,b,c};\n g[a].push_back({a,b,c});\n }\n vector<ll> dp(N,0);\n for(int i=N-1;i>=0;i--){\n for(auto& e:g[i]) dp[i] = max(dp[i],dp[e.t]+e.d);\n }\n ll D = dp[0];\n min_cost_flow flow(N+2);\n vector<ll> b(N,0);\n flow.add_edge(0,N-1,inf,D);\n ll ans = 0;\n ll tinf = 10000;\n for(auto& e:edges){\n b[e.t] -= tinf;\n b[e.s] += tinf;\n ans += -e.d*tinf;\n flow.add_edge(e.s,e.t,tinf-1,e.d);\n }\n ll sum = 0;\n for(int i=0;i<N;i++){\n if(b[i]>0) flow.add_edge(N,i,b[i],0);\n if(b[i]<0) flow.add_edge(i,N+1,-b[i],0);\n sum += max(b[i],0LL);\n }\n cout << ans+flow.answer(N,N+1,sum) << endl;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3328, "score_of_the_acc": -0.1024, "final_rank": 8 }, { "submission_id": "aoj_2230_3984678", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\nusing ll = long long;\nusing pii = pair<int,int>;\nusing pll = pair<ll,ll>;\nusing vi = vector<int>;\nusing vl = vector<ll>;\n\nusing _loop_int = int;\n#define REP(i,n) for(_loop_int i=0; i<(_loop_int)(n); i++)\n#define FOR(i,a,b) for(_loop_int i=(_loop_int)(a); i<(_loop_int)(b); i++)\n#define FORR(i,a,b) for(_loop_int i=(_loop_int)(b)-1; i>=(_loop_int)(a); i--)\n\n#define CHMIN(a,b) (a)=min((a),(b))\n#define CHMAX(a,b) (a)=max((a),(b))\n#define ALL(v) (v).begin(),(v).end()\n\n#define DEBUG(x) cerr<<#x<<\": \"<<(x)<<endl\n#define DEBUG_VEC(v) cerr<<#v<<\": \";REP(__i,(v).size())cerr<<((v)[__i])<<\", \";cerr<<endl\n\nconst ll MOD = 1000000007ll;\n\ntemplate<typename T>\nstruct MCF {\n using P = pair<T,int>;\n struct edge {\n int to; T cap; T cost; int rev;\n edge(int a, T b, T c, int d):to(a),cap(b),cost(c),rev(d){}\n };\n vector<vector<edge>> G;\n vector<T> h,dist;\n vector<int> prevv,preve;\n int V;\n MCF(int v):G(v),prevv(v,0),preve(v,0),V(v){}\n void add_edge(int from, int to, T cap, T cost){\n G[from].push_back(edge(to,cap,cost,G[to].size()));\n G[to].push_back(edge(from,0,-cost,G[from].size()-1));\n }\n T solve(int s, int t, T f, T COST_INF=(1<<30)){\n T res = 0;\n h.assign(V, 0);\n while(f > 0){\n priority_queue<P,vector<P>,greater<P>> Q;\n dist.assign(V, COST_INF);\n dist[s] = 0;\n Q.push(P(0,s));\n while(Q.size()){\n P p = Q.top(); Q.pop();\n int v = p.second;\n if(dist[v] < p.first)continue;\n REP(i,G[v].size()){\n edge &e = G[v][i];\n T d = dist[v] + e.cost + h[v] - h[e.to];\n if(e.cap > 0 && dist[e.to] > d){\n dist[e.to] = d;\n prevv[e.to] = v;\n preve[e.to] = i;\n Q.push(P(dist[e.to], e.to));\n }\n }\n }\n if(dist[t] == COST_INF){\n // return -1;\n break;\n }\n REP(v,V)h[v] += dist[v];\n T d = f;\n for(int v=t; v!=s; v=prevv[v]){\n d = min(d, G[prevv[v]][preve[v]].cap);\n }\n f -= d;\n res += d*h[t];\n for(int v=t; v!=s; v=prevv[v]){\n edge &e = G[prevv[v]][preve[v]];\n e.cap -= d;\n G[v][e.rev].cap += d;\n }\n }\n return res;\n }\n};\n\ntemplate<typename T>\nstruct MCC {\n MCF<T> mcf;\n int V,source,sink;\n T sum;\n MCC(int v):V(v+2),mcf(v+2),source(v),sink(v+1),sum(0){}\n void add_edge(int from, int to, T lb, T ub, T cost){\n if(cost < 0){\n sum += cost * ub;\n mcf.add_edge(source,to,ub,0);\n mcf.add_edge(from,sink,ub,0);\n if(ub-lb > 0){\n mcf.add_edge(to,from,ub-lb,-cost);\n }\n }else{\n sum += cost * lb;\n if(lb > 0){\n mcf.add_edge(source,to,lb,0);\n mcf.add_edge(from,sink,lb,0);\n }\n if(ub-lb > 0){\n mcf.add_edge(from,to,ub-lb,cost);\n }\n }\n }\n T solve(T f, T COST_INF=(1<<30)){\n return sum + mcf.solve(source, sink, f, COST_INF);\n }\n};\n\nint n,m;\nvector<pii> g[125];\n\nint main(){\n scanf(\"%d%d\",&n,&m);\n REP(i,m){\n int x,y,s;\n scanf(\"%d%d%d\",&x,&y,&s);\n g[x].push_back(pii(y,s));\n }\n // calc max dist (DAG)\n vector<int> dist(n, 0);\n REP(i,n)for(pii P : g[i])CHMAX(dist[P.first], dist[i]+P.second);\n int x = dist[n-1];\n const ll INF = 1ll<<30;\n MCC<ll> mcc(n);\n const int start = 0;\n const int goal = n-1;\n // constraint: goal - start <= x\n // > goal - start <= x -> cost = 0\n // > goal - start > x -> cost = INF\n mcc.add_edge(start, goal, 0, INF, x);\n REP(u,n)for(pii P : g[u]){\n int v = P.first;\n int s = P.second;\n // constraint: v - u >= s\n // > u - v <= -s -> cost = 1 * (u - v)\n // > u - v > -s -> cost = INF\n mcc.add_edge(v, u, 1, INF, -s);\n }\n ll ans = mcc.solve(1ll<<60, 1ll<<60);\n printf(\"%lld\\n\",ans);\n return 0;\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 3356, "score_of_the_acc": -0.6037, "final_rank": 18 }, { "submission_id": "aoj_2230_3927948", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing Int = long long;\ntemplate<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}\ntemplate<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}\n\n\ntemplate<typename TF,typename TC>\nstruct PrimalDual{\n struct edge{\n int to;\n TF cap;\n TC cost;\n int rev;\n edge(){}\n edge(int to,TF cap,TC cost,int rev):\n to(to),cap(cap),cost(cost),rev(rev){}\n };\n\n static const TC INF;\n vector<vector<edge>> G;\n vector<TC> h,dist;\n vector<int> prevv,preve;\n\n PrimalDual(){}\n PrimalDual(int n):G(n),h(n),dist(n),prevv(n),preve(n){}\n\n void add_edge(int u,int v,TF cap,TC cost){\n G[u].emplace_back(v,cap,cost,G[v].size());\n G[v].emplace_back(u,0,-cost,G[u].size()-1);\n }\n\n void dijkstra(int s){\n struct P{\n TC first;\n int second;\n P(TC first,int second):first(first),second(second){}\n bool operator<(const P&a) const{return a.first<first;}\n };\n priority_queue<P> que;\n fill(dist.begin(),dist.end(),INF);\n\n dist[s]=0;\n que.emplace(dist[s],s);\n while(!que.empty()){\n P p=que.top();que.pop();\n int v=p.second;\n if(dist[v]<p.first) continue;\n for(int i=0;i<(int)G[v].size();i++){\n edge &e=G[v][i];\n if(e.cap==0) continue;\n if(dist[v]+e.cost+h[v]-h[e.to]<dist[e.to]){\n dist[e.to]=dist[v]+e.cost+h[v]-h[e.to];\n prevv[e.to]=v;\n preve[e.to]=i;\n que.emplace(dist[e.to],e.to);\n }\n }\n }\n }\n\n TC flow(int s,int t,TF f,int &ok){\n TC res=0;\n fill(h.begin(),h.end(),0);\n while(f>0){\n dijkstra(s);\n if(dist[t]==INF){\n ok=0;\n return res;\n }\n\n for(int v=0;v<(int)h.size();v++)\n if(dist[v]<INF) h[v]=h[v]+dist[v];\n\n TF d=f;\n for(int v=t;v!=s;v=prevv[v])\n d=min(d,G[prevv[v]][preve[v]].cap);\n\n f-=d;\n res=res+h[t]*d;\n for(int v=t;v!=s;v=prevv[v]){\n edge &e=G[prevv[v]][preve[v]];\n e.cap-=d;\n G[v][e.rev].cap+=d;\n }\n }\n ok=1;\n return res;\n }\n};\ntemplate<typename TF, typename TC>\nconst TC PrimalDual<TF, TC>::INF = numeric_limits<TC>::max()/2;\n\n\ntemplate<typename TF,typename TC>\nstruct NegativeEdge{\n PrimalDual<TF, TC> G;\n vector<TF> fs;\n TC sum;\n int S,T;\n NegativeEdge(){}\n NegativeEdge(int n):G(n+2),fs(n+2,0),sum(0),S(n),T(n+1){}\n\n void use_edge(int u,int v,TF cap,TC cost){\n fs[u]-=cap;\n fs[v]+=cap;\n sum=sum+cost*cap;\n }\n\n void add_edge(int u,int v,TF cap,TC cost){\n if(cost<TC(0)){\n use_edge(u,v,cap,cost);\n swap(u,v);\n cost=-cost;\n }\n G.add_edge(u,v,cap,cost);\n }\n\n TC flow(int &ok){\n TF f=0;\n for(int i=0;i<S;i++){\n if(fs[i]>0){\n f+=fs[i];\n G.add_edge(S,i,+fs[i],TC(0));\n }\n if(fs[i]<0){\n G.add_edge(i,T,-fs[i],TC(0));\n }\n }\n return sum+G.flow(S,T,f,ok);\n }\n\n TC flow(int ts,int tt,TF tf,int &ok){\n fs[ts]+=tf;\n fs[tt]-=tf;\n return flow(ok);\n }\n};\n\n//INSERT ABOVE HERE\nsigned main(){\n using P = pair<int, int>;\n using ll = long long;\n const ll INF = 1e9;\n\n int n,m;\n cin>>n>>m;\n vector< vector<P> > H(n);\n NegativeEdge<ll, ll> G(n);\n for(int i=0;i<m;i++){\n int x,y,s;\n cin>>x>>y>>s;\n H[x].emplace_back(y,s);\n G.add_edge(y,x,1,-s-INF);\n G.add_edge(y,x,INF,-s);\n }\n\n vector<int> dp(n,0);\n for(int i=0;i<n;i++)\n for(auto e:H[i])\n chmax(dp[e.first],dp[i]+e.second);\n G.add_edge(0,n-1,INF,dp[n-1]);\n\n int ok=0;\n cout<<INF*m+G.flow(ok)<<endl;\n assert(ok);\n return 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3280, "score_of_the_acc": -0.2002, "final_rank": 13 }, { "submission_id": "aoj_2230_3927905", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing Int = long long;\ntemplate<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}\ntemplate<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}\n\n\ntemplate<typename TF,typename TC>\nstruct PrimalDual{\n struct edge{\n int to;\n TF cap;\n TC cost;\n int rev;\n edge(){}\n edge(int to,TF cap,TC cost,int rev):\n to(to),cap(cap),cost(cost),rev(rev){}\n };\n\n static const TC INF;\n vector<vector<edge>> G;\n vector<TC> h,dist;\n vector<int> prevv,preve;\n\n PrimalDual(){}\n PrimalDual(int n):G(n),h(n),dist(n),prevv(n),preve(n){}\n\n void add_edge(int u,int v,TF cap,TC cost){\n G[u].emplace_back(v,cap,cost,G[v].size());\n G[v].emplace_back(u,0,-cost,G[u].size()-1);\n }\n\n void dijkstra(int s){\n struct P{\n TC first;\n int second;\n P(TC first,int second):first(first),second(second){}\n bool operator<(const P&a) const{return a.first<first;}\n };\n priority_queue<P> que;\n fill(dist.begin(),dist.end(),INF);\n\n dist[s]=0;\n que.emplace(dist[s],s);\n while(!que.empty()){\n P p=que.top();que.pop();\n int v=p.second;\n if(dist[v]<p.first) continue;\n for(int i=0;i<(int)G[v].size();i++){\n edge &e=G[v][i];\n if(e.cap==0) continue;\n if(dist[v]+e.cost+h[v]-h[e.to]<dist[e.to]){\n dist[e.to]=dist[v]+e.cost+h[v]-h[e.to];\n prevv[e.to]=v;\n preve[e.to]=i;\n que.emplace(dist[e.to],e.to);\n }\n }\n }\n }\n\n TC flow(int s,int t,TF f,int &ok){\n TC res=0;\n fill(h.begin(),h.end(),0);\n while(f>0){\n dijkstra(s);\n if(dist[t]==INF){\n ok=0;\n return res;\n }\n\n for(int v=0;v<(int)h.size();v++)\n if(dist[v]<INF) h[v]=h[v]+dist[v];\n\n TF d=f;\n for(int v=t;v!=s;v=prevv[v])\n d=min(d,G[prevv[v]][preve[v]].cap);\n\n f-=d;\n res=res+h[t]*d;\n for(int v=t;v!=s;v=prevv[v]){\n edge &e=G[prevv[v]][preve[v]];\n e.cap-=d;\n G[v][e.rev].cap+=d;\n }\n }\n ok=1;\n return res;\n }\n};\ntemplate<typename TF, typename TC>\nconst TC PrimalDual<TF, TC>::INF = numeric_limits<TC>::max()/2;\n\n\ntemplate<typename TF,typename TC>\nstruct NegativeEdge{\n PrimalDual<TF, TC> G;\n vector<TF> fs;\n TC sum;\n int S,T;\n NegativeEdge(){}\n NegativeEdge(int n):G(n+2),fs(n+2,0),sum(0),S(n),T(n+1){}\n\n void use_edge(int u,int v,TF cap,TC cost){\n fs[u]-=cap;\n fs[v]+=cap;\n sum=sum+cost*cap;\n }\n\n void add_edge(int u,int v,TF cap,TC cost){\n if(cost<TC(0)){\n use_edge(u,v,cap,cost);\n swap(u,v);\n cost=-cost;\n }\n G.add_edge(u,v,cap,cost);\n }\n\n TC flow(int &ok){\n TF f=0;\n for(int i=0;i<S;i++){\n if(fs[i]>0){\n f+=fs[i];\n G.add_edge(S,i,+fs[i],TC(0));\n }\n if(fs[i]<0){\n G.add_edge(i,T,-fs[i],TC(0));\n }\n }\n return sum+G.flow(S,T,f,ok);\n }\n\n TC flow(int ts,int tt,TF tf,int &ok){\n fs[ts]+=tf;\n fs[tt]-=tf;\n return flow(ok);\n }\n};\n\n//INSERT ABOVE HERE\nsigned main(){\n using P = pair<int, int>;\n using ll = long long;\n const ll INF = 1e9;\n\n int n,m;\n cin>>n>>m;\n vector< vector<P> > H(n);\n NegativeEdge<ll, ll> G(n);\n for(int i=0;i<m;i++){\n int x,y,s;\n cin>>x>>y>>s;\n H[x].emplace_back(y,s);\n G.add_edge(x,y,1,-s-INF);\n G.add_edge(x,y,INF,-s);\n }\n\n vector<int> dp(n,0);\n for(int i=0;i<n;i++)\n for(auto e:H[i])\n chmax(dp[e.first],dp[i]+e.second);\n G.add_edge(n-1,0,INF,dp[n-1]);\n\n int ok=0;\n cout<<INF*m+G.flow(ok)<<endl;\n assert(ok);\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3296, "score_of_the_acc": -0.1009, "final_rank": 4 }, { "submission_id": "aoj_2230_3864422", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntypedef pair<int,int> P;\ntypedef pair<int,P> P1;\ntypedef pair<P,P> P2;\n#define pu push\n#define pb push_back\n#define mp make_pair\n#define eps 1e-7\n//#define INF 1000000000\n#define mod 1000000007\n#define fi first\n#define sc second\n#define rep(i,x) for(int i=0;i<x;i++)\n#define repn(i,x) for(int i=1;i<=x;i++)\n#define SORT(x) sort(x.begin(),x.end())\n#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())\n#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin())\n#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin())\n#define int long long\nll D;\n\ntemplate< typename flow_t, typename cost_t >\nstruct PrimalDual {\n const cost_t INF;\n\n struct edge {\n int to;\n flow_t cap;\n cost_t cost;\n int rev;\n bool isrev;\n };\n vector< vector< edge > > graph;\n vector< cost_t > potential, min_cost;\n vector< int > prevv, preve;\n\n PrimalDual(int V) : graph(V), INF(1000000000LL) {}\n\n void add_edge(int from, int to, flow_t cap, cost_t cost) {\n graph[from].emplace_back((edge) {to, cap, cost, (int) graph[to].size(), false});\n graph[to].emplace_back((edge) {from, 0, -cost, (int) graph[from].size() - 1, true});\n }\n\n cost_t min_cost_flow(int s, int t, flow_t f) {\n int V = (int) graph.size();\n cost_t ret = 0;\n using Pi = pair< cost_t, int >;\n priority_queue< Pi, vector< Pi >, greater< Pi > > que;\n potential.assign(V, 0);\n preve.assign(V, -1);\n prevv.assign(V, -1);\n\n while(f > 0) {\n min_cost.assign(V, INF);\n que.emplace(0, s);\n min_cost[s] = 0;\n while(!que.empty()) {\n Pi p = que.top();\n que.pop();\n if(min_cost[p.second] < p.first) continue;\n for(int i = 0; i < graph[p.second].size(); i++) {\n edge &e = graph[p.second][i];\n cost_t nextCost = min_cost[p.second] + e.cost + potential[p.second] - potential[e.to];\n if(e.cap > 0 && min_cost[e.to] > nextCost) {\n min_cost[e.to] = nextCost;\n prevv[e.to] = p.second, preve[e.to] = i;\n que.emplace(min_cost[e.to], e.to);\n }\n }\n }\n if(min_cost[t] == INF) return -1;\n for(int v = 0; v < V; v++) potential[v] += min_cost[v];\n flow_t addflow = f;\n for(int v = t; v != s; v = prevv[v]) {\n addflow = min(addflow, graph[prevv[v]][preve[v]].cap);\n }\n f -= addflow;\n ret += addflow * potential[t];\n ret += addflow * D;\n for(int v = t; v != s; v = prevv[v]) {\n edge &e = graph[prevv[v]][preve[v]];\n e.cap -= addflow;\n graph[v][e.rev].cap += addflow;\n }\n }\n return ret;\n }\n\n void output() {\n for(int i = 0; i < graph.size(); i++) {\n for(auto &e : graph[i]) {\n if(e.isrev) continue;\n auto &rev_e = graph[e.to][e.rev];\n cout << i << \"->\" << e.to << \" (flow: \" << rev_e.cap << \"/\" << rev_e.cap + e.cap << \")\" << endl;\n }\n }\n }\n};\n\nint n,m;\nvector<P>edge[105];\nint dp[105];\n\nint calc(int v){\n\tif(dp[v] >= 0) return dp[v];\n\tdp[v] = 0;\n\tif(v == n-1) return dp[v];\n\tfor(int i=0;i<edge[v].size();i++){\n\t\tdp[v] = max(dp[v],calc(edge[v][i].fi)+edge[v][i].sc);\n\t}\n\treturn dp[v];\n}\n\nsigned main(){\n\tcin>>n>>m;\n\trep(i,m){\n\t\tint x,y,s; cin>>x>>y>>s;\n\t\tedge[x].pb(mp(y,s));\n\t}\n\tmemset(dp,-1,sizeof(dp));\n\tD = calc(0);\n\tll ans = 0;\n\tPrimalDual<int,int>g(n);\n\trep(i,n){\n\t\trep(j,edge[i].size()){\n\t\t\tint u = i,v = edge[i][j].fi,cs = edge[i][j].sc;\n\t\t\tans += 200000-cs;\n\t\t\tg.add_edge(v,u,1,-200000);\n\t\t\tg.add_edge(v,u,1e9,-cs);\n\t\t}\n\t}\n\tans += g.min_cost_flow(n-1,0,1e9);\n\tll ans2 = 0;\n\trep(i,n){\n\t\trep(j,edge[i].size()){\n\t\t\tint u = i,v = edge[i][j].fi,cs = edge[i][j].sc;\n\t\t\tans2 += g.potential[v]-g.potential[u]-cs; \n\t\t\tassert(g.potential[v]-g.potential[u]-cs >= 0);\n\t\t\tedge[i][j].sc = g.potential[v]-g.potential[u];\n\t\t}\n\t}\n\tmemset(dp,-1,sizeof(dp));\n\tll DD = calc(0);\n\tassert(ans == ans2);\n\tassert(D == DD);\n\tcout << ans << endl;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3308, "score_of_the_acc": -0.1015, "final_rank": 5 } ]
aoj_2229_cpp
Problem H: Ropeway On a small island, there are two towns Darkside and Sunnyside, with steep mountains between them. There’s a company Intertown Company of Package Conveyance; they own a ropeway car between Darkside and Sunnyside and are running a transportation business. They want maximize the revenue by loading as much package as possible on the ropeway car. The ropeway car looks like the following. It’s L length long, and the center of it is hung from the rope. Let’s suppose the car to be a 1-dimensional line segment, and a package to be a point lying on the line. You can put packages at anywhere including the edge of the car, as long as the distance between the package and the center of the car is greater than or equal to R and the car doesn’t fall down. The car will fall down when the following condition holds: Here N is the number of packages; m i the weight of the i -th package; x i is the position of the i -th package relative to the center of the car. You, a Darkside programmer, are hired as an engineer of the company. Your task is to write a program which reads a list of package and checks if all the packages can be loaded in the listed order without the car falling down at any point. Input The input has two lines, describing one test case. The first line contains four integers N , L , M , R . The second line contains N integers m 0 , m 1 , ... m N -1 . The integers are separated by a space. Output If there is a way to load all the packages, output “Yes” in one line, without the quotes. Otherwise, output “No”. Sample Input 1 3 3 2 1 1 1 4 Output for the Sample Input 1 Yes Sample Input 2 3 3 2 1 1 4 1 Output for the Sample Input 2 No
[ { "submission_id": "aoj_2229_1574246", "code_snippet": "#include <cstdlib>\n#include <cctype>\n#include <cstring>\n#include <cstdio>\n#include <cmath>\n#include <algorithm>\n#include <vector>\n#include <string>\n#include <iostream>\n#include <sstream>\n#include <map>\n#include <set>\n#include <queue>\n#include <stack>\n#include <ctime>\ntypedef long long ll;\n#define clr(x,a) memset(x,a,sizeof(x))\n#define sz(x) (int)x.size()\n#define see(x) cerr<<#x<<\" \"<<x<<endl\n#define se(x) cerr<<\" \"<<x\n#define pb push_back\n#define mp make_pair\n#define rep(i,l,r) for (long long i=l;i<=r;i++)\nusing namespace std;\nint n,l,m,r,a[1010000],ans;\nvoid dfs(int ll,int rr,int i){\n if (i>n) {\n ans=1;return;\n }\n if (ll+r*a[i]<=m)\n dfs(ll+r*a[i],min(m,rr+l*a[i]),i+1);\n if (rr-r*a[i]>=-m)\n dfs(max(-m,ll-l*a[i]),rr-r*a[i],i+1);\n}\nint main(){\n scanf(\"%d%d%d%d\",&n,&l,&m,&r);\n if (2*r>l) {\n printf(\"No\\n\");\n return 0;\n }\n rep(i,1,n) scanf(\"%d\",a+i);\n ans=0;\n r*=2;m*=2;\n dfs(0,0,1);\n if (ans) printf(\"Yes\\n\");\n else printf(\"No\\n\");\n return 0;\n}", "accuracy": 0.1568627450980392, "time_ms": 1460, "memory_kb": 1172, "score_of_the_acc": -1.0127, "final_rank": 10 }, { "submission_id": "aoj_2229_1574244", "code_snippet": "#include <cstdlib>\n#include <cctype>\n#include <cstring>\n#include <cstdio>\n#include <cmath>\n#include <algorithm>\n#include <vector>\n#include <string>\n#include <iostream>\n#include <sstream>\n#include <map>\n#include <set>\n#include <queue>\n#include <stack>\n#include <ctime>\ntypedef long long ll;\n#define clr(x,a) memset(x,a,sizeof(x))\n#define sz(x) (int)x.size()\n#define see(x) cerr<<#x<<\" \"<<x<<endl\n#define se(x) cerr<<\" \"<<x\n#define pb push_back\n#define mp make_pair\n#define rep(i,l,r) for (long long i=l;i<=r;i++)\nusing namespace std;\nint n,l,m,r,a[101000],ans;\nvoid dfs(int ll,int rr,int i){\n if (i>n) {\n ans=1;return;\n }\n if (ll+r*a[i]<=m)\n dfs(ll+r*a[i],min(m,rr+l*a[i]),i+1);\n if (rr-r*a[i]>=-m)\n dfs(max(-m,ll-l*a[i]),rr-r*a[i],i+1);\n}\nint main(){\n scanf(\"%d%d%d%d\",&n,&l,&m,&r);\n rep(i,1,n) scanf(\"%d\",&a[i]);\n ans=0;\n r*=2;m*=2;\n dfs(0,0,1);\n if (ans) printf(\"Yes\\n\");\n else printf(\"No\\n\");\n return 0;\n}", "accuracy": 0.1568627450980392, "time_ms": 1490, "memory_kb": 1172, "score_of_the_acc": -1.0329, "final_rank": 11 }, { "submission_id": "aoj_2229_1565340", "code_snippet": "#include<iostream>\n\nusing namespace std;\n\n#define int long long\n\nint pos[123][42345];\nint O=20000;\n\nsigned main(){\n int N,L,M,R;\n cin>>N>>L>>M>>R;\n pos[0][O]=1;\n bool can[123]={};\n for(int i=0;i<N;i++){\n int m;\n cin>>m;\n int mx=2*M;\n for(int j=-mx;j<=mx;j++){\n if(pos[i][j+O]){\n\tint lh=j-2*R*m;\n\tif(-mx<=lh){\n\t pos[i+1][O+max(-mx,j-L*m)]++;\n\t pos[i+1][O+lh+1]--;\n\t}\n\tint hl=j+2*R*m;\n\tif(hl<=mx){\n\t pos[i+1][O+hl]++;\n\t int hh=j+L*m;\n\t if(hh+1<=mx){\n\t pos[i+1][O+hh+1]--;\n\t }\n\t}\n }\n }\n can[i+1]=pos[i+1][-mx+O];\n for(int j=-mx+1+O;j<=mx+O;j++){\n pos[i+1][j]+=pos[i+1][j-1];\n can[i+1]|=pos[i+1][j];\n }\n }\n cout<<(can[N]?\"Yes\":\"No\")<<endl;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 35580, "score_of_the_acc": -1.0068, "final_rank": 8 }, { "submission_id": "aoj_2229_1565338", "code_snippet": "#include<iostream>\n\nusing namespace std;\n\nint pos[123][42345];\nint O=20000;\n\nint main(){\n int N,L,M,R;\n cin>>N>>L>>M>>R;\n pos[0][O]=1;\n bool can[123]={};\n for(int i=0;i<N;i++){\n int m;\n cin>>m;\n int mx=2*M;\n for(int j=-mx;j<=mx;j++){\n if(pos[i][j+O]){\n\tint lh=j-2*R*m;\n\tif(-mx<=lh){\n\t pos[i+1][O+max(-mx,j-L*m)]++;\n\t pos[i+1][O+lh+1]--;\n\t}\n\tint hl=j+2*R*m;\n\tif(hl<=mx){\n\t pos[i+1][O+hl]++;\n\t int hh=j+L*m;\n\t if(hh+1<=mx){\n\t pos[i+1][O+hh+1]--;\n\t }\n\t}\n }\n }\n can[i+1]=pos[i+1][-mx+O];\n for(int j=-mx+1+O;j<=mx+O;j++){\n pos[i+1][j]+=pos[i+1][j-1];\n can[i+1]|=pos[i+1][j];\n }\n }\n cout<<(can[N]?\"Yes\":\"No\")<<endl;\n}", "accuracy": 0.1568627450980392, "time_ms": 10, "memory_kb": 1172, "score_of_the_acc": -0.0329, "final_rank": 9 }, { "submission_id": "aoj_2229_1533725", "code_snippet": "#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <cmath>\n#include <iostream>\n#include <algorithm>\n#include <vector>\n\nusing namespace std;\n\nconst long long N = 10000;\nvector<long long> f[N];\nvector<long long> pack;\nlong long n, l, m ,r;\n\nint main()\n{\n\tios::sync_with_stdio(false);\n\tcin >> n >> l >> m >> r;\n\tl *= 2; m *= 2; r *= 2;\n\tfor (long long i = 0; i <= n; ++i)\n\t\tf[i] = vector<long long>(m + 10, 0);\n\tfor (long long i = 0; i < n; ++i) {\n\t\tlong long weight;\n\t\tcin >> weight;\n\t\tpack.push_back(weight);\n\t}\n\tf[0][0] = 1;\n\tf[0][1] = -1;\n\tfor (long long i = 0; i <= n; ++i) {\n\t\tfor (long long j = 1; j <= m; ++j)\n\t\t\tf[i][j] += f[i][j - 1];\n\t\tif (i == n) break;\n\t\tfor (long long j = 0; j <= m; ++j) {\n\t\t\tif (!f[i][j]) continue;\n\t\t\tlong long kl = r * pack[i], kr = l / 2 * pack[i];\n\t\t\tlong long cl, cr;\n\t\t\t//part1\n\t\t\tif(j - kl >= 0 && j - kr >= 0) {\n\t\t\t\tcl = j - kr;\n\t\t\t\tcr = j - kl;\n\t\t\t} else if (j - kl <= 0 && j - kr <= 0) {\n\t\t\t\tcl = abs(j - kl);\n\t\t\t\tcr = abs(j - kr);\n\t\t\t} else {\n\t\t\t\tcl = 0;\n\t\t\t\tcr = max(abs(j - kr), abs(j - kl));\n\t\t\t}\n\t\t\tif(cl <= m) {\n\t\t\t\tcr = min(cr, m);\n\t\t\t\tf[i + 1][cl]++;\n\t\t\t\tf[i + 1][cr + 1]--;\n\t\t\t}\n\t\t\t//part2\n\t\t\tcl = j + kl, cr = j + kr;\n\t\t\tif(cl <= m) {\n\t\t\t\tcr = min(cr, m);\n\t\t\t\tf[i + 1][cl]++;\n\t\t\t\tf[i + 1][cr + 1]--;\n\t\t\t}\n\t\t\t\t\t\n\t\t}\n\t}\n\tfor (long long i = 0; i <= m; ++i)\n\t\tif (f[n][i]) {\n\t\t\tcout << \"Yes\" << endl;\n\t\t\treturn 0;\n\t\t}\n\tcout << \"No\" << endl;\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 17388, "score_of_the_acc": -0.4887, "final_rank": 6 }, { "submission_id": "aoj_2229_1142686", "code_snippet": "#include<stdio.h>\n#include<algorithm>\nusing namespace std;\nint dp[110][41000];\nint p[110];\nint main(){\n\tint a,b,c,d;\n\tscanf(\"%d%d%d%d\",&a,&b,&c,&d);\n\tfor(int i=0;i<a;i++){\n\t\tscanf(\"%d\",p+i);\n\t\tif(p[i]>20100){\n\t\t\tprintf(\"No\\n\");return 0;\n\t\t}\n\t}\n\tc*=2;\n\td*=2;\n\tdp[0][c]=1;\n\tfor(int i=0;i<a;i++){\n\t\tint L=d*p[i];\n\t\tint R=b*p[i];\n\t\tif(L>2*c)break;\n\t\tif(R>2*c)R=2*c;\n\t\tfor(int j=0;j<=2*c;j++){\n\t\t\tif(!dp[i][j])continue;\n\t\t\tif(j-L+1>=0){\n\t\t\t\tdp[i+1][max(0,j-R)]++;\n\t\t\t\tdp[i+1][max(0,j-L+1)]--;\n\t\t\t}\n\t\t\tdp[i+1][min(2*c+1,j+L)]++;\n\t\t\tdp[i+1][min(2*c+1,j+R+1)]--;\n\t\t}\n\t\tfor(int j=1;j<=2*c;j++){\n\t\t\tdp[i+1][j]+=dp[i+1][j-1];\n\t\t}\n\t\tfor(int j=0;j<=2*c;j++)dp[i+1][j]=!!dp[i+1][j];\n\t//\tfor(int j=0;j<=2*c;j++)printf(\"%d \",dp[i+1][j]);\n\t\t//printf(\"\\n\");\n\t}\n\tbool ok=false;\n\tfor(int i=0;i<=2*c;i++)if(dp[a][i])ok=true;\n\tif(ok)printf(\"Yes\\n\");\n\telse printf(\"No\\n\");\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 17232, "score_of_the_acc": -0.4978, "final_rank": 7 }, { "submission_id": "aoj_2229_1091790", "code_snippet": "#include <iostream>\n#include <cstdio>\n#include <vector>\n#include <queue>\n#include <cmath>\n#include <algorithm>\n#include <functional>\nusing namespace std;\n#define rep(i,n) for(ll i=0;i<(n);++i)\n#define rep1(i,n) for(ll i=1;i<=(n);++i)\n#define all(c) (c).begin(),(c).end()\n#define pb push_back\n#define fs first\n#define sc second\n#define show(x) cout << #x << \" = \" << x << endl\ntypedef long long ll;\nbool dp[40001];\nll imos[40002];\nint main(){\n\tll N,L,M,R;\n\tcin>>N>>L>>M>>R;\n\tL*=2,M*=2,R*=2;\n\tdp[M]=true;\n\trep1(j,40001) imos[j+1]=imos[j]+(dp[j]?1:0);\n\trep1(i,N){\n\t\tll m;\n\t\tcin>>m;\n\t\tll mn=m*R,mx=m*L/2;\n//\t\tshow(i);\n\t\trep(j,2*M+1){\n\t\t\tll l=max(0LL,j-mx),r=j-mn;\n\t\t\tdp[j]=false;\n\t\t\tif(r>=0) if(imos[r+1]-imos[l]>0) dp[j]=true;\n\t\t\tl=j+mn,r=min(j+mx,M*2);\n\t\t\tif(l<=M*2) if(imos[r+1]-imos[l]>0) dp[j]=true;\n//\t\t\tcout<<\"j:\"<<j<<\" \";\n//\t\t\tshow(dp[j]);\n\t\t}\n//\t\tcout<<endl;\n\t\trep(j,40001) imos[j+1]=imos[j]+(dp[j]?1:0);\n\t}\n\tcout<<(imos[2*M]>0?\"Yes\\n\":\"No\\n\");\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 1468, "score_of_the_acc": -0.048, "final_rank": 3 }, { "submission_id": "aoj_2229_738875", "code_snippet": "#include <iostream>\n#include <sstream>\n#include <cstdio>\n#include <cstdlib>\n#include <cmath>\n#include <ctime>\n#include <cstring>\n#include <string>\n#include <vector>\n#include <stack>\n#include <queue>\n#include <deque>\n#include <map>\n#include <set>\n#include <bitset>\n#include <numeric>\n#include <utility>\n#include <iomanip>\n#include <algorithm>\n#include <functional>\nusing namespace std;\n\ntypedef long long ll;\ntypedef vector<int> vint;\ntypedef vector<ll> vll;\ntypedef pair<int,int> pint;\n\n#define DE 1\n#define FI first\n#define SE second\n#define PB push_back\n#define MP make_pair\n#define ALL(s) (s).begin(),(s).end()\n#define REP(i,n) for (int i = 0; i < (int)(n); ++i)\n#define EACH(i,s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)\n#define COUT(x) cout<<#x<<\" = \"<<(x)<<\" (L\"<<__LINE__<<\")\"<<endl\n\ntemplate<class T1, class T2> ostream& operator<<(ostream &s, pair<T1,T2> P){return s<<'<'<<P.first<<\", \"<<P.second<<'>';}\ntemplate<class T> ostream& operator<<(ostream &s, vector<T> P) {s<<\"{ \";for(int i=0;i<P.size();++i){if(i>0)s<<\", \";s<<P[i];}return s<<\" }\"<<endl;}\ntemplate<class T1, class T2> ostream& operator<<(ostream &s, map<T1,T2> P) {s<<\"{ \";for(__typeof__(P.begin()) it=P.begin();it!=P.end();++it){if(it!=P.begin())s<<\", \";s<<'<'<<it->first<<\"->\"<<it->second<<'>';}return s<<\" }\"<<endl;}\n\n\n\nll N, L, M, R;\nll m[105];\n\nint dp[105][20100];\n\nint main() {\n //freopen( \"/Users/macuser/Documents/Programming/Contest/input.in\", \"r\", stdin );\n \n while (cin >> N >> L >> M >> R) {\n for (int i = 0; i < N; ++i) cin >> m[i];\n memset(dp, 0, sizeof(dp));\n R *= 2;\n M *= 2;\n \n dp[0][0] = 1;\n for (int i = 0; i <= N; ++i) {\n for (int j = 0; j <= M; ++j) {\n //cout << i << \", \" << j << \" : \" << dp[i][j] << endl;\n \n if (dp[i][j] <= 0) continue;\n \n ll l1 = max(0LL, j + m[i]*R);\n ll r1 = min(M, j + m[i]*L) + 1;\n ll l2, r2;\n if (j - m[i]*L >= 0) {l2 = j - m[i]*L; r2 = min(M, j - m[i]*R) + 1;}\n else if (j - m[i]*R >= 0) {l2 = 0; r2 = min(M, max(abs(j - m[i]*L), abs(j - m[i]*R))) + 1;}\n else {l2 = abs(j - m[i]*R); r2 = min(M, abs(j - m[i]*L)) + 1;}\n \n if (l1 <= r1) {\n dp[i+1][l1]++; dp[i+1][r1]--;\n }\n if (l2 <= r2) {\n dp[i+1][l2]++; dp[i+1][r2]--;\n }\n }\n for (int j = 0; j <= M; ++j) {\n dp[i+1][j+1] += dp[i+1][j];\n }\n }\n \n bool exist = false;\n for (int i = 0; i <= M; ++i) {\n if (dp[N][i] > 0) {exist = true; break;}\n }\n \n if (exist) cout << \"Yes\" << endl;\n else cout << \"No\" << endl;\n }\n \n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 9408, "score_of_the_acc": -0.2644, "final_rank": 4 }, { "submission_id": "aoj_2229_411286", "code_snippet": "#include <iostream>\n#include <iomanip>\n#include <sstream>\n#include <cstdio>\n#include <string>\n#include <vector>\n#include <algorithm>\n#include <complex>\n#include <cstring>\n#include <cstdlib>\n#include <cmath>\n#include <cassert>\n#include <climits>\n#include <queue>\n#include <set>\n#include <map>\n#include <valarray>\n#include <bitset>\n#include <stack>\nusing namespace std;\n\n#define REP(i,n) for(int i=0;i<(int)n;++i)\n#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)\n#define ALL(c) (c).begin(), (c).end()\ntypedef long long ll;\ntypedef pair<int,int> pii;\nconst int INF = 1<<29;\nconst double PI = acos(-1);\nconst double EPS = 1e-8;\n\nll m[1000];\nbool dp[40010];\nbool next[40010];\n\nint main() {\n ll N,L,M,R;\n cin>>N>>L>>M>>R;\n REP(i,N) cin >> m[i];\n R*=2;\n L*=2;\n M*=2;\n ll MM = 2*M;\n dp[M] = 1;\n REP(i,N) {\n REP(j,MM+1)next[j]=0;\n ll ma = m[i]*L/2;\n ll mi = m[i]*R;\n REP(j,MM+1) {\n if (dp[j]) {\n // 右\n if (j+mi<=MM) {\n next[min(MM,j+ma)]=1;\n next[j+mi]=1;\n }\n // 左\n if (j-mi>=0) {\n next[max(0LL,j-ma)]=1;\n next[j-mi]=1;\n }\n }\n }\n bool f = 0;\n REP(j,MM+1) {\n dp[j]=next[j];\n f|=dp[j];\n }\n // REP(j,MM+1)cout<<dp[j]<<\" \";cout<<endl;\n if (!f) {\n cout << \"No\" << endl;\n return 0;\n }\n }\n cout << \"Yes\" << endl;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 0, "score_of_the_acc": 0, "final_rank": 1 }, { "submission_id": "aoj_2229_331833", "code_snippet": "#include <stdio.h>\n#include <string.h>\n#include <algorithm>\n#include <iostream>\n#include <math.h>\n#include <assert.h>\n#include <vector>\n#include <queue>\n#include <string>\n#include <map>\n#include <set>\n\nusing namespace std;\ntypedef long long ll;\ntypedef unsigned int uint;\ntypedef unsigned long long ull;\nstatic const double EPS = 1e-9;\nstatic const double PI = acos(-1.0);\n\n#define REP(i, n) for (int i = 0; i < (int)(n); i++)\n#define FOR(i, s, n) for (int i = (s); i < (int)(n); i++)\n#define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++)\n#define FORIT(it, c) for (__typeof((c).begin())it = (c).begin(); it != (c).end(); it++)\n#define MEMSET(v, h) memset((v), h, sizeof(v))\n\n\nstruct FenwickTree {\n static const int SIZE = 60000;\n int tree[SIZE + 10];\n FenwickTree() { memset(tree, 0, sizeof(tree)); }\n void add(int index, int value) {\n for (index += SIZE / 2; index < SIZE + 1; index += (index & -index)) {\n tree[index] += value;\n }\n }\n int sum(int index) {\n int ret = 0;\n for (index += SIZE / 2; index > 0; index -= (index & -index)) {\n ret += tree[index];\n }\n return ret;\n }\n};\n\nint n;\nll l, m, r;\nll seq[110];\nFenwickTree ftree[2];\n\nint main() {\n while (scanf(\"%d %lld %lld %lld\", &n, &l, &m, &r) > 0) {\n m *= 2;\n r *= 2;\n REP(i, n) {\n scanf(\"%lld\", &seq[i]);\n }\n ftree[0] = FenwickTree();\n ftree[1] = FenwickTree();\n ftree[0].add(0, 1);\n ftree[0].add(1, -1);\n REP(i, n) {\n int prev = i & 1;\n int next = prev ^ 1;\n ftree[next] = FenwickTree();\n ll l1 = -l * seq[i];\n ll r1 = -r * seq[i];\n ll l2 = r * seq[i];\n ll r2 = l * seq[i];\n FOREQ(w, -m, m) {\n if (ftree[prev].sum(w) == 0) { continue; }\n ll l, r;\n l = max(-m - 1, min(m + 1, l1 + w));\n r = max(-m - 1, min(m + 1, r1 + w));\n ftree[next].add(l, 1);\n ftree[next].add(r + 1, -1);\n l = max(-m - 1, min(m + 1, l2 + w));\n r = max(-m - 1, min(m + 1, r2 + w));\n ftree[next].add(l, 1);\n ftree[next].add(r + 1, -1);\n }\n bool ok = false;\n FOREQ(w, -m, m) {\n if (ftree[next].sum(w) >= 1) { ok = true; }\n //putchar(ftree[next].sum(w) > 0 ? 'o' : 'x');\n }\n //puts(\"\");\n if (!ok) { goto ng; }\n }\n puts(\"Yes\");\n continue;\nng:\n puts(\"No\");\n }\n}", "accuracy": 1, "time_ms": 570, "memory_kb": 2012, "score_of_the_acc": -0.4349, "final_rank": 5 }, { "submission_id": "aoj_2229_328400", "code_snippet": "#include<iostream>\n#include<algorithm>\nusing namespace std;\n#define REP(i,b,n) for(int i=b;i<n;i++)\n#define rep(i,n) REP(i,0,n)\ntypedef long long ll;\nconst int M = 40011;\nbool dp[2][M];\nbool solve(int n,ll *in,int m,int L,int R){\n int now=0;\n int allm=4*m+1;\n rep(i,M)dp[0][i]=dp[1][i]=false;\n dp[0][2*m]=true;\n\n rep(i,n){\n int next=(now+1)%2;\n rep(j,allm){\n if (!dp[now][j])continue;\n\n if ( j-R*in[i]*2 >= 0){\n\t//cout << j <<\" \" << j-L*in[i] << \" \" << j-R*in[i]*2 << endl;\n\tdp[next][max(0LL, j-L*in[i])]=true;//j+(-L/2*mi+m)*2;\n\tdp[next][ j-R*in[i]*2]=true;//j+(-R*mi +m)*2;\n }\n\n if (j+R*in[i]*2 <= 4*m){\n\t//cout << j <<\" \" << j+L*in[i] << \" \" << j+R*in[i]*2 << endl;\n\tdp[next][min(4LL*m,j+L*in[i])]=true;//j+(L/2*mi+m)*2;\n\tdp[next][ j+R*in[i]*2]=true;//j+(R*mi +m)*2;\n }\n }\n //rep(j,allm)cout << dp[next][j]<<\" \";cout << endl;\n rep(j,allm)dp[now][j]=false;\n now=next;\n }\n \n rep(i,allm){\n if (dp[now][i])return true;\n }\n return false;\n}\n\nconst int N = 100;\nll in[N];\nmain(){\n int n,l,m,r;\n while(cin>>n>>l>>m>>r){\n rep(i,n)cin>>in[i];\n cout << (solve(n,in,m,l,r)?\"Yes\":\"No\") << endl;\n }\n return false;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 0, "score_of_the_acc": -0.0068, "final_rank": 2 } ]
aoj_2232_cpp
Problem A: Ennichi 縁日に来たうさぎが, ある出店のゲームの景品がキャロットケーキであるのを見つけた. このゲームのルールは以下の通りである. 縦h マス× 横w マスの格子状のフィールドがあり, 各マスにつき高々1 個のブロックが置かれる. 各ブロックにはアルファベット大文字(‘A’ - ‘Z’) のいずれかで表される色がついている. 同色のブロックが縦または横に一直線上にn 個以上連続して並ぶと, それらのブロックは消滅する. 参加者は横に隣り合う2 つのマスを選んで, それらの状態を互いに入れ替える操作ができる. ブロックの入れ替え, 消滅, 落下によってブロックの有るマスの1 つ下のマスにブロックが無くなると, このブロックは落下する. このときふたたび同色のブロックがn 個以上並ぶと消滅する. ただし, ブロックの消滅は, 落下するブロックが存在する間は起こらず, すべてのブロックの落下が終了したタイミングで同時に起こる. 1 回の操作でフィールド上のすべてのブロックを消滅させると, このゲームは成功となり景品のケーキを得ることができる. うさぎは1 回分の参加費で確実にケーキを手に入れたく, それができない場合は参加したくない. ゲーム開始時のフィールドの状態から, うさぎがこのゲームに参加すべきであるか答えよ. Input 入力の一行目にはh, w, n がスペースで区切られて与えられる. 2 ≤ h , w , n ≤ 30 続く h 行にはフィールドの状態が上から順に与えられる. アルファベット大文字はブロックを, ‘.’ は空きマスを表す. 与えられるフィールドの状態には, 縦あるいは横に n 個以上連続する同色のブロックはなく, 落下する状態にあるブロックもない. 1 個以上のブロックが存在する. Output うさぎがこのゲームに参加すべきであるなら”YES”を, そうでないなら”NO”を一行に出力せよ. Sample Input 1 4 6 3 ...... ...Y.. ...Y.. RRYRYY Sample Output 2 YES Sample Input 1 4 6 3 ...... ...Y.. ...Y.. RRYRY. Sample Output 2 NO
[ { "submission_id": "aoj_2232_11059717", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing a2 = array<ll, 2>;\nusing a3 = array<ll, 3>;\n\nbool chmin(auto& a, const auto& b) { return a > b ? a = b, 1 : 0; }\nbool chmax(auto& a, const auto& b) { return a < b ? a = b, 1 : 0; }\n\nll mod = 998244353;\nconst ll INF = 1e18;\n\nifstream in;\nofstream out;\n\nint main(int argc, char** argv) {\n ios::sync_with_stdio(false);\n cin.tie(0);\n\n if(argc > 2) {\n in.open(argv[1]);\n cin.rdbuf(in.rdbuf());\n out.open(argv[2]);\n cout.rdbuf(out.rdbuf());\n }\n\n ll h, w, l;\n cin >> h >> w >> l;\n vector<string> s(h);\n for(auto& x : s) cin >> x;\n\n auto simu = [&](vector<string> inp) -> bool {\n bool loo = true;\n while(loo) {\n loo = false;\n for(int j = 0; j < w; j++) {\n for(int i = h - 2; i >= 0; i--) {\n ll k = i + 1;\n while(k < h && inp[k][j] == '.') {\n swap(inp[k][j], inp[k-1][j]);\n k++;\n }\n }\n }\n\n vector<vector<char>> ok(h, vector<char>(w, false));\n for(int i = 0; i < h; i++) {\n for(int j = 0; j + l - 1 < w; j++) {\n bool ren = true;\n for(int k = 0; k < l; k++) {\n if(inp[i][j + k] != inp[i][j]) ren = false;\n }\n if(ren) {\n for(int k = 0; k < l; k++) {\n ok[i][j + k] = true;\n }\n }\n }\n }\n for(int i = 0; i + l - 1 < h; i++) {\n for(int j = 0; j < w; j++) {\n bool ren = true;\n for(int k = 0; k < l; k++) {\n if(inp[i + k][j] != inp[i][j]) ren = false;\n }\n if(ren) {\n for(int k = 0; k < l; k++) {\n ok[i + k][j] = true;\n }\n }\n }\n }\n\n for(int i = 0; i < h; i++) {\n for(int j = 0; j < w; j++) {\n if(ok[i][j]) {\n loo |= inp[i][j] != '.';\n inp[i][j] = '.';\n }\n }\n }\n }\n ll cnt = 0;\n for(auto& x : inp) cnt += count(x.begin(), x.end(), '.');\n\n return cnt == h * w;\n };\n\n bool ans = false;\n\n for(int i = 0; i < h; i++) {\n for(int j = 0; j < w - 1; j++) {\n swap(s[i][j], s[i][j + 1]);\n ans |= simu(s);\n swap(s[i][j], s[i][j + 1]);\n }\n }\n\n cout << (ans ? \"YES\" : \"NO\") << endl;\n\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3584, "score_of_the_acc": -0.717, "final_rank": 18 }, { "submission_id": "aoj_2232_9594386", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n#include <unistd.h>\n\nnamespace fastio {\nstatic constexpr uint32_t SZ = 1 << 17;\nchar ibuf[SZ];\nchar obuf[SZ];\nchar out[100];\n// pointer of ibuf, obuf\n\n\nuint32_t pil = 0, pir = 0, por = 0;\n\nstruct Pre {\n char num[10000][4];\n constexpr Pre() : num() {\n for (int i = 0; i < 10000; i++) {\n int n = i;\n for (int j = 3; j >= 0; j--) {\n num[i][j] = n % 10 | '0';\n n /= 10;\n }\n }\n }\n} constexpr pre;\n\ninline void load() {\n memmove(ibuf, ibuf + pil, pir - pil);\n pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin);\n pil = 0;\n if (pir < SZ)\n ibuf[pir++] = '\\n';\n}\n\ninline void flush() {\n fwrite(obuf, 1, por, stdout);\n por = 0;\n}\n\nvoid rd(char &c) {\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n}\n\nvoid rd(string &x) {\n x.clear();\n char c;\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n do {\n x += c;\n if (pil == pir)\n load();\n c = ibuf[pil++];\n } while (!isspace(c));\n}\n\ntemplate <typename T> void rd_real(T &x) {\n string s;\n rd(s);\n x = stod(s);\n}\n\ntemplate <typename T> void rd_integer(T &x) {\n if (pil + 100 > pir)\n load();\n char c;\n do\n c = ibuf[pil++];\n while (c < '-');\n bool minus = 0;\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (c == '-') {\n minus = 1, c = ibuf[pil++];\n }\n }\n x = 0;\n while ('0' <= c) {\n x = x * 10 + (c & 15), c = ibuf[pil++];\n }\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (minus)\n x = -x;\n }\n}\n\nvoid rd(int &x) {\n rd_integer(x);\n}\nvoid rd(ll &x) {\n rd_integer(x);\n}\nvoid rd(i128 &x) {\n rd_integer(x);\n}\nvoid rd(uint &x) {\n rd_integer(x);\n}\nvoid rd(ull &x) {\n rd_integer(x);\n}\nvoid rd(u128 &x) {\n rd_integer(x);\n}\nvoid rd(double &x) {\n rd_real(x);\n}\nvoid rd(long double &x) {\n rd_real(x);\n}\n\ntemplate <class T, class U> void rd(pair<T, U> &p) {\n return rd(p.first), rd(p.second);\n}\ntemplate <size_t N = 0, typename T> void rd_tuple(T &t) {\n if constexpr (N < std::tuple_size<T>::value) {\n auto &x = std::get<N>(t);\n rd(x);\n rd_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void rd(tuple<T...> &tpl) {\n rd_tuple(tpl);\n}\n\ntemplate <size_t N = 0, typename T> void rd(array<T, N> &x) {\n for (auto &d : x)\n rd(d);\n}\ntemplate <class T> void rd(vector<T> &x) {\n for (auto &d : x)\n rd(d);\n}\n\nvoid read() {}\ntemplate <class H, class... T> void read(H &h, T &...t) {\n rd(h), read(t...);\n}\n\nvoid wt(const char c) {\n if (por == SZ)\n flush();\n obuf[por++] = c;\n}\nvoid wt(const string s) {\n for (char c : s)\n wt(c);\n}\nvoid wt(const char *s) {\n size_t len = strlen(s);\n for (size_t i = 0; i < len; i++)\n wt(s[i]);\n}\n\ntemplate <typename T> void wt_integer(T x) {\n if (por > SZ - 100)\n flush();\n if (x < 0) {\n obuf[por++] = '-', x = -x;\n }\n int outi;\n for (outi = 96; x >= 10000; outi -= 4) {\n memcpy(out + outi, pre.num[x % 10000], 4);\n x /= 10000;\n }\n if (x >= 1000) {\n memcpy(obuf + por, pre.num[x], 4);\n por += 4;\n } else if (x >= 100) {\n memcpy(obuf + por, pre.num[x] + 1, 3);\n por += 3;\n } else if (x >= 10) {\n int q = (x * 103) >> 10;\n obuf[por] = q | '0';\n obuf[por + 1] = (x - q * 10) | '0';\n por += 2;\n } else\n obuf[por++] = x | '0';\n memcpy(obuf + por, out + outi + 4, 96 - outi);\n por += 96 - outi;\n}\n\ntemplate <typename T> void wt_real(T x) {\n ostringstream oss;\n oss << fixed << setprecision(15) << double(x);\n string s = oss.str();\n wt(s);\n}\n\nvoid wt(int x) {\n wt_integer(x);\n}\nvoid wt(ll x) {\n wt_integer(x);\n}\nvoid wt(i128 x) {\n wt_integer(x);\n}\nvoid wt(uint x) {\n wt_integer(x);\n}\nvoid wt(ull x) {\n wt_integer(x);\n}\nvoid wt(u128 x) {\n wt_integer(x);\n}\nvoid wt(double x) {\n wt_real(x);\n}\nvoid wt(long double x) {\n wt_real(x);\n}\n\ntemplate <class T, class U> void wt(const pair<T, U> val) {\n wt(val.first);\n wt(' ');\n wt(val.second);\n}\ntemplate <size_t N = 0, typename T> void wt_tuple(const T t) {\n if constexpr (N < std::tuple_size<T>::value) {\n if constexpr (N > 0) {\n wt(' ');\n }\n const auto x = std::get<N>(t);\n wt(x);\n wt_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void wt(tuple<T...> tpl) {\n wt_tuple(tpl);\n}\ntemplate <class T, size_t S> void wt(const array<T, S> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\ntemplate <class T> void wt(const vector<T> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\n\nvoid print() {\n wt('\\n');\n}\ntemplate <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {\n wt(head);\n if (sizeof...(Tail))\n wt(' ');\n print(forward<Tail>(tail)...);\n}\nvoid __attribute__((destructor)) _d() {\n flush();\n}\n} // namespace fastio\n\n\nusing fastio::flush;\nusing fastio::print;\nusing fastio::read;\n\ninline void first(bool i = true) {\n print(i ? \"first\" : \"second\");\n}\ninline void Alice(bool i = true) {\n print(i ? \"Alice\" : \"Bob\");\n}\ninline void Takahashi(bool i = true) {\n print(i ? \"Takahashi\" : \"Aoki\");\n}\ninline void yes(bool i = true) {\n print(i ? \"yes\" : \"no\");\n}\ninline void Yes(bool i = true) {\n print(i ? \"Yes\" : \"No\");\n}\ninline void No() {\n print(\"No\");\n}\ninline void YES(bool i = true) {\n print(i ? \"YES\" : \"NO\");\n}\ninline void NO() {\n print(\"NO\");\n}\ninline void Yay(bool i = true) {\n print(i ? \"Yay!\" : \":(\");\n}\ninline void Possible(bool i = true) {\n print(i ? \"Possible\" : \"Impossible\");\n}\ninline void POSSIBLE(bool i = true) {\n print(i ? \"POSSIBLE\" : \"IMPOSSIBLE\");\n}\n\n/**\n * @brief Fast IO\n */\n\nint N, M, K;\nvector<vector<int>> A;\n\nbool Clear(vector<vector<int>>& B) {\n bool Ret = false;\n vector<vector<bool>> flag(N, vector<bool>(M, false));\n rep(i,0,N) {\n rep(j,0,M) {\n if (B[i][j] == -1) continue;\n if (j+K-1 >= M) continue;\n bool check = true;\n rep(k,0,K) {\n if (B[i][j+k] != B[i][j]) check = false;\n }\n if (check) {\n Ret = true;\n rep(k,0,K) flag[i][j+k] = true;\n }\n }\n }\n rep(i,0,N) {\n rep(j,0,M) {\n if (B[i][j] == -1) continue;\n if (i+K-1 >= N) continue;\n bool check = true;\n rep(k,0,K) {\n if (B[i+k][j] != B[i][j]) check = false;\n }\n if (check) {\n Ret = true;\n rep(k,0,K) flag[i+k][j] = true;\n }\n }\n }\n rep(i,0,N) {\n rep(j,0,M) {\n if (flag[i][j]) B[i][j] = -1;\n }\n }\n return Ret;\n}\n\nvoid Fall(vector<vector<int>>& B) {\n vector<vector<int>> C(M);\n rep(i,0,M) {\n rep(j,0,N) {\n if (B[j][i] != -1) C[i].push_back(B[j][i]);\n }\n }\n vector<vector<int>> Ret(N,vector<int>(M,-1));\n rep(i,0,M) {\n rep(j,0,C[i].size()) {\n Ret[j][i] = C[i][j];\n }\n }\n B = Ret;\n}\n\nbool Swapping(int X, int Y) {\n vector<vector<int>> B = A;\n swap(B[X][Y], B[X][Y+1]);\n while(1) {\n Fall(B);\n if (!Clear(B)) break;\n }\n bool check = true;\n rep(i,0,N) {\n rep(j,0,M) {\n if (B[i][j] != -1) check = false;\n }\n }\n return check;\n}\n\nint main() {\n cin >> N >> M >> K;\n A.assign(N,vector<int>(M));\n rrep(i,0,N) {\n rep(j,0,M) {\n char C;\n cin >> C;\n if (C == '.') A[i][j] = -1;\n else A[i][j] = C - 'A';\n }\n }\n bool ANS = false;\n rep(i,0,N) {\n rep(j,0,M-1) {\n if (Swapping(i,j)) ANS = true;\n }\n }\n cout << (ANS ? \"YES\" : \"NO\") << endl;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3520, "score_of_the_acc": -0.4151, "final_rank": 14 }, { "submission_id": "aoj_2232_9195516", "code_snippet": "#include <bits/stdc++.h>\n\nint main() {\n using Board = std::vector<std::string>;\n int h, w, n;\n std::cin >> h >> w >> n;\n Board init(h);\n for (int i = 0; i < h; i++) std::cin >> init[i];\n const int DI[] = {0, 0};\n const int DJ[] = {1, -1};\n\n auto clear = [&](Board board) {\n for (int i = 0; i < h; i++) {\n for (int j = 0; j < w; j++) {\n if (board[i][j] != '.') return false;\n }\n }\n return true;\n };\n\n auto erase_pos = [&](Board board) {\n std::vector erase(h, std::vector<bool>(w));\n // tate\n for (int i = 0; i + n <= h; i++) {\n for (int j = 0; j < w; j++) {\n auto tar = board[i][j];\n if (tar == '.') continue;\n bool ok = true;\n for (int k = 0; k < n; k++) {\n if (board[i + k][j] != tar) {\n ok = false;\n break;\n }\n }\n if (!ok) continue;\n for (int k = 0; k < n; k++) {\n erase[i + k][j] = true;\n }\n }\n }\n\n // yoko\n for (int i = 0; i < h; i++) {\n for (int j = 0; j + n <= w; j++) {\n auto tar = board[i][j];\n if (tar == '.') continue;\n bool ok = true;\n for (int k = 0; k < n; k++) {\n if (board[i][j + k] != tar) {\n ok = false;\n break;\n }\n }\n if (!ok) continue;\n for (int k = 0; k < n; k++) {\n erase[i][j + k] = true;\n }\n }\n }\n\n return erase;\n };\n\n auto fall = [&](Board board) {\n std::vector c(w, std::vector<char>());\n for (int j = 0; j < w; j++) {\n for (int i = h - 1; i >= 0; i--) {\n if (board[i][j] != '.') {\n c[j].push_back(board[i][j]);\n }\n }\n }\n Board next(h, std::string(w, '.'));\n for (int j = 0; j < w; j++) {\n for (int i = 0; i < (int)c[j].size(); i++) {\n next[h - 1 - i][j] = c[j][i];\n }\n }\n return next;\n };\n\n auto print = [&](Board board) {\n for (int i = 0; i < h; i++) {\n std::cerr << board[i] << '\\n';\n }\n };\n\n auto sim = [&](Board board) {\n // std::cerr << \"start\" << '\\n';\n // print(board);\n while (true) {\n board = fall(board);\n if (clear(board)) {\n return true;\n }\n auto erase = erase_pos(board);\n bool not_clear = true;\n for (int i = 0; i < h; i++) {\n for (int j = 0; j < w; j++) {\n if (erase[i][j]) {\n assert(board[i][j] != '.');\n not_clear = false;\n board[i][j] = '.';\n }\n }\n }\n if (not_clear) return false;\n // std::cerr << \"erased\" << '\\n';\n // print(board);\n }\n };\n\n for (int i = 0; i < h; i++) {\n for (int j = 0; j < w; j++) {\n // if (init[i][j] == '.') continue;\n for (int d = 0; d < 2; d++) {\n int ni = i + DI[d];\n int nj = j + DJ[d];\n if (ni < 0 || nj < 0 || ni >= h || nj >= w) continue;\n // if (init[ni][nj] == '.') continue;\n Board board = init;\n std::swap(board[i][j], board[ni][nj]);\n if (sim(board)) {\n std::cout << \"YES\" << '\\n';\n return 0;\n }\n }\n }\n }\n std::cout << \"NO\" << '\\n';\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3452, "score_of_the_acc": -0.1314, "final_rank": 3 }, { "submission_id": "aoj_2232_9194947", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#if __has_include(<atcoder/all>)\n#include <atcoder/all>\nusing namespace atcoder;\ntemplate<int mod>istream &operator>>(istream &is,static_modint<mod> &a){long long b;is>>b;a=b;return is;}\nistream &operator>>(istream &is,modint &a){long long b;cin>>b;a=b;return is;}\n#endif\n#ifdef LOCAL\n#include \"debug.h\"\n#else\n#define debug(...) static_cast<void>(0)\n#define debugg(...) static_cast<void>(0)\n#define esper(...) static_cast<void>(0)\ntemplate<typename T1,typename T2>ostream &operator<<(ostream &os,const pair<T1,T2>&p){os<<p.first<<' '<<p.second;return os;}\n#endif\nusing ll=long long;\nusing ull=unsigned long long;\nusing P=pair<ll,ll>;\ntemplate<typename T>using minque=priority_queue<T,vector<T>,greater<T>>;\ntemplate<typename T>bool chmax(T &a,const T &b){return (a<b?(a=b,true):false);}\ntemplate<typename T>bool chmin(T &a,const T &b){return (a>b?(a=b,true):false);}\ntemplate<typename T1,typename T2>istream &operator>>(istream &is,pair<T1,T2>&p){is>>p.first>>p.second;return is;}\ntemplate<typename T>istream &operator>>(istream &is,vector<T> &a){for(auto &i:a)is>>i;return is;}\ntemplate<typename T1,typename T2>void operator++(pair<T1,T2>&a,int n){a.first++,a.second++;}\ntemplate<typename T1,typename T2>void operator--(pair<T1,T2>&a,int n){a.first--,a.second--;}\ntemplate<typename T>void operator++(vector<T>&a,int n){for(auto &i:a)i++;}\ntemplate<typename T>void operator--(vector<T>&a,int n){for(auto &i:a)i--;}\n#define reps(i,a,n) for(int i=(a);i<(n);i++)\n#define rep(i,n) reps(i,0,n)\n#define all(x) x.begin(),x.end()\n#define pcnt(x) __builtin_popcountll(x)\n#define fin(x) return cout<<x<<'\\n',static_cast<void>(0)\nll myceil(ll a,ll b){return (a+b-1)/b;}\ntemplate<typename T,size_t n,size_t id=0>\nauto vec(const int (&d)[n],const T &init=T()){\n if constexpr (id<n)return vector(d[id],vec<T,n,id+1>(d,init));\n else return init;\n}\nvoid SOLVE();\nint main(){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout<<fixed<<setprecision(16);\n #ifdef LOCAL\n clock_t start=clock();\n #endif\n int testcase=1;\n //cin>>testcase;\n for(int i=0;i<testcase;i++){\n SOLVE();\n }\n #ifdef LOCAL\n cerr<<\"time:\";\n cerr<<(clock()-start)/1000;\n cerr<<\"ms\\n\";\n #endif\n}\nstruct UF{\n vector<int>par;\n UF(int n):par(n,-1){}\n int root(int u){\n if(par[u]<0)return u;\n return par[u]=root(par[u]);\n }\n void merge(int u,int v){\n int ru=root(u),rv=root(v);\n if(ru==rv)return;\n if(ru>rv)swap(ru,rv);\n par[ru]+=par[rv];\n par[rv]=ru;\n }\n bool same(int u,int v){return root(u)==root(v);}\n};\nint h,w;\nvoid SOLVE(){\n int n;\n cin>>h>>w>>n;\n vector<string>s(h);\n cin>>s;\n auto eval=[](vector<string>s)->vector<string> {\n vector<string>nxt(h,string(w,'.'));\n rep(j,w){\n string now;\n rep(i,h)if(s[i][j]!='.')now+=s[i][j];\n rep(i,now.size())nxt[i+h-now.size()][j]=now[i];\n }\n return nxt;\n };\n auto solve=[&]()->void {\n vector<string>cop(s);\n cop=eval(cop);\n while(true){\n bool non=false;\n rep(i,h)rep(j,w){\n if(cop[i][j]!='.')non=true;\n }\n debugg(cop);\n if(!non){\n cout<<\"YES\"<<endl;\n exit(0);\n }\n UF uf(h*w);\n rep(i,h)rep(j,w)if(cop[i][j]!='.'){\n if(i+n<=h){\n bool ok=true;\n rep(k,n)ok&=cop[i][j]==cop[i+k][j];\n if(ok)rep(k,n)uf.merge(i*w+j,(i+k)*w+j);\n }\n if(j+n<=w){\n bool ok=true;\n rep(k,n)ok&=cop[i][j]==cop[i][j+k];\n if(ok)rep(k,n)uf.merge(i*w+j,i*w+j+k);\n }\n }\n map<int,vector<pair<int,int>>>idx;\n rep(i,h)rep(j,w)if(cop[i][j]!='.'){\n idx[uf.root(i*w+j)].push_back({i,j});\n }\n bool deleted=false;\n for(auto [k,v]:idx){\n if(v.size()==1)continue;\n assert(v.size()>=n);\n deleted=true;\n for(auto [x,y]:v)cop[x][y]='.';\n }\n if(!deleted)return;\n cop=eval(cop);\n }\n };\n rep(i,h)rep(j,w){\n if(j+1!=w){\n swap(s[i][j],s[i][j+1]);\n solve();\n swap(s[i][j],s[i][j+1]);\n }\n }\n cout<<\"NO\"<<endl;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 3544, "score_of_the_acc": -0.7135, "final_rank": 17 }, { "submission_id": "aoj_2232_7536742", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i, n) for (int i = 0; i < (n); i++)\n#define per(i, n) for (int i = (n)-1; i >= 0; i--)\n#define rep2(i, l, r) for (int i = (l); i < (r); i++)\n#define per2(i, l, r) for (int i = (r)-1; i >= (l); i--)\n#define each(e, v) for (auto &e : v)\n#define MM << \" \" <<\n#define pb push_back\n#define eb emplace_back\n#define all(x) begin(x), end(x)\n#define rall(x) rbegin(x), rend(x)\n#define sz(x) (int)x.size()\nusing ll = long long;\nusing pii = pair<int, int>;\nusing pil = pair<int, ll>;\nusing pli = pair<ll, int>;\nusing pll = pair<ll, ll>;\n\ntemplate <typename T>\nusing minheap = priority_queue<T, vector<T>, greater<T>>;\n\ntemplate <typename T>\nusing maxheap = priority_queue<T>;\n\ntemplate <typename T>\nbool chmax(T &x, const T &y) {\n return (x < y) ? (x = y, true) : false;\n}\n\ntemplate <typename T>\nbool chmin(T &x, const T &y) {\n return (x > y) ? (x = y, true) : false;\n}\n\ntemplate <typename T>\nint flg(T x, int i) {\n return (x >> i) & 1;\n}\n\ntemplate <typename T>\nvoid print(const vector<T> &v, T x = 0) {\n int n = v.size();\n for (int i = 0; i < n; i++) cout << v[i] + x << (i == n - 1 ? '\\n' : ' ');\n if (v.empty()) cout << '\\n';\n}\n\ntemplate <typename T>\nvoid printn(const vector<T> &v, T x = 0) {\n int n = v.size();\n for (int i = 0; i < n; i++) cout << v[i] + x << '\\n';\n}\n\ntemplate <typename T>\nint lb(const vector<T> &v, T x) {\n return lower_bound(begin(v), end(v), x) - begin(v);\n}\n\ntemplate <typename T>\nint ub(const vector<T> &v, T x) {\n return upper_bound(begin(v), end(v), x) - begin(v);\n}\n\ntemplate <typename T>\nvoid rearrange(vector<T> &v) {\n sort(begin(v), end(v));\n v.erase(unique(begin(v), end(v)), end(v));\n}\n\ntemplate <typename T>\nvector<int> id_sort(const vector<T> &v, bool greater = false) {\n int n = v.size();\n vector<int> ret(n);\n iota(begin(ret), end(ret), 0);\n sort(begin(ret), end(ret), [&](int i, int j) { return greater ? v[i] > v[j] : v[i] < v[j]; });\n return ret;\n}\n\ntemplate <typename S, typename T>\npair<S, T> operator+(const pair<S, T> &p, const pair<S, T> &q) {\n return make_pair(p.first + q.first, p.second + q.second);\n}\n\ntemplate <typename S, typename T>\npair<S, T> operator-(const pair<S, T> &p, const pair<S, T> &q) {\n return make_pair(p.first - q.first, p.second - q.second);\n}\n\ntemplate <typename S, typename T>\nistream &operator>>(istream &is, pair<S, T> &p) {\n S a;\n T b;\n is >> a >> b;\n p = make_pair(a, b);\n return is;\n}\n\ntemplate <typename S, typename T>\nostream &operator<<(ostream &os, const pair<S, T> &p) {\n return os << p.first << ' ' << p.second;\n}\n\nstruct io_setup {\n io_setup() {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n cout << fixed << setprecision(15);\n }\n} io_setup;\n\nconst int inf = (1 << 30) - 1;\nconst ll INF = (1LL << 60) - 1;\n// const int MOD = 1000000007;\nconst int MOD = 998244353;\n\nvector<string> drop(vector<string> T) {\n int H = sz(T), W = sz(T[0]);\n vector<string> tmp(W);\n rep(j, W) {\n per(i, H) {\n if (T[i][j] != '.') {\n tmp[j] += T[i][j];\n T[i][j] = '.';\n }\n }\n rep(i, sz(tmp[j])) T[H - 1 - i][j] = tmp[j][i];\n }\n\n return T;\n}\n\nvector<string> ope(vector<string> S, int K) {\n int H = sz(S), W = sz(S[0]);\n auto T = S;\n rep(i, H) rep(j, W) {\n if (i + K <= H) {\n bool flag = true;\n rep2(x, i, i + K) {\n if (S[i][j] != S[x][j]) flag = false;\n }\n if (flag) {\n rep2(x, i, i + K) T[x][j] = '.'; //\n }\n }\n if (j + K <= W) {\n bool flag = true;\n rep2(y, j, j + K) {\n if (S[i][j] != S[i][y]) flag = false;\n }\n if (flag) {\n rep2(y, j, j + K) T[i][y] = '.'; //\n }\n }\n }\n\n return drop(T);\n}\n\nvoid solve() {\n int H, W, N;\n cin >> H >> W >> N;\n\n vector<string> S(H);\n rep(i, H) cin >> S[i];\n\n rep(i, H) rep(j, W - 1) {\n auto T = S;\n swap(T[i][j], T[i][j + 1]);\n T = drop(T);\n while (true) {\n // cout << \"#\\n\";\n // rep(i, H) cout << T[i] << '\\n';\n auto NT = ope(T, N);\n if (T == NT) break;\n T = NT;\n }\n bool flag = true;\n rep(x, H) rep(y, W) {\n if (T[x][y] != '.') flag = false;\n }\n if (flag) {\n cout << \"YES\\n\";\n return;\n }\n }\n\n cout << \"NO\\n\";\n}\n\nint main() {\n solve(); //\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3512, "score_of_the_acc": -0.3774, "final_rank": 11 }, { "submission_id": "aoj_2232_7140025", "code_snippet": "// author: hanyu\n#include <bits/stdc++.h>\nusing namespace std;\n\nint h, w, n;\n\nint f(vector<string> &x) {\n int res = 0;\n for (int i = 0; i < h; i++) {\n for (int j = 0; j < w; j++) {\n if (x[i][j] != '.') res++;\n }\n }\n return res;\n}\n\nbool solve(vector<string> x) {\n for (int j = 0; j < w; j++) {\n for (int _it2 = 0; _it2 < h + 3; _it2++) {\n for (int i = h - 1; i > 0; i--) {\n if (x[i][j] == '.' && x[i - 1][j] != '.') swap(x[i][j], x[i - 1][j]);\n }\n }\n }\n //\n for (int _it = 0; _it < h * w / n + 3; _it++) {\n int res1 = f(x);\n vector<vector<int>> y(h, vector<int>(w, 0));\n for (int i = 0; i < h; i++) {\n for (int j = 0; j < w; j++) {\n set<char> st;\n if (i + n <= h) {\n for (int ii = i; ii < i + n; ii++) st.insert(x[ii][j]);\n }\n if (st.size() == 1) {\n for (int ii = i; ii < i + n; ii++) y[ii][j] = 1;\n }\n st.clear();\n if (j + n <= w) {\n for (int jj = j; jj < j + n; jj++) st.insert(x[i][jj]);\n }\n if (st.size() == 1) {\n for (int jj = j; jj < j + n; jj++) y[i][jj] = 1;\n }\n }\n }\n //\n for (int i = 0; i < h; i++) {\n for (int j = 0; j < w; j++) {\n if (y[i][j] == 1) x[i][j] = '.';\n }\n }\n //\n for (int j = 0; j < w; j++) {\n for (int _it2 = 0; _it2 < h + 3; _it2++) {\n for (int i = h - 1; i > 0; i--) {\n if (x[i][j] == '.' && x[i - 1][j] != '.') swap(x[i][j], x[i - 1][j]);\n }\n }\n }\n int res2 = f(x);\n if (res1 == res2) break;\n }\n\n for (int i = 0; i < h; i++) {\n for (int j = 0; j < w; j++) {\n if (x[i][j] != '.') return false;\n }\n }\n return true;\n}\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n\n// int h, w, n;\n cin >> h >> w >> n;\n\n vector<string> x(h);\n for (int i = 0; i < h; i++) cin >> x[i];\n\n bool ok = false;\n for (int i = 0; i < h; i++) {\n for (int j = 1; j < w; j++) {\n swap(x[i][j - 1], x[i][j]);\n if (solve(x)) {\n cout << \"YES\" << endl;\n return 0;\n }\n swap(x[i][j - 1], x[i][j]);\n }\n }\n\n cout << \"NO\" << endl;\n}", "accuracy": 1, "time_ms": 280, "memory_kb": 3476, "score_of_the_acc": -1.2075, "final_rank": 20 }, { "submission_id": "aoj_2232_6791246", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing vll = vector<ll>;\nusing vvll = vector<vll>;\nusing vvvll = vector<vvll>;\nusing vvvvll = vector<vvvll>;\nusing vb = vector<bool>;\nusing vvb = vector<vb>;\nusing vvvb = vector<vvb>;\nusing vd = vector<double>;\nusing vvd = vector<vd>;\nusing vvvd = vector<vvd>;\n#define all(A) A.begin(),A.end()\n#define ALL(A) A.begin(),A.end()\n#define rep(i, n) for (ll i = 0; i < (ll) (n); i++)\nusing pqr = priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>>;\n\ntemplate<class T>\nbool chmax(T& p, T q, bool C = 1) {\n if (C == 0 && p == q) {\n return 1;\n }\n if (p < q) {\n p = q;\n return 1;\n }\n else {\n return 0;\n }\n}\n\ntemplate<class T>\nbool chmin(T& p, T q, bool C = 1) {\n if (C == 0 && p == q) {\n return 1;\n }\n if (p > q) {\n p = q;\n return 1;\n }\n else {\n return 0;\n }\n}\n\nll gcd(ll(a), ll(b)) {\n if (b == 0)return a;\n ll c = a;\n while (a % b != 0) {\n c = a % b;\n a = b;\n b = c;\n }\n return b;\n}\nll H, W, N;\nbool game(vector<string> S) {\n while (1) {\n vvb er(H, vb(W, false));\n rep(i, 30) {\n rep(hh, H - 1) {\n ll h = H - hh - 1;\n rep(w, W) {\n if (S[h][w] == '.') {\n swap(S[h][w], S[h - 1][w]);\n }\n }\n }\n }\n rep(h, H) {\n char p = '&';\n ll k = 0;\n rep(w, W) {\n if (S[h][w] != '.') {\n if (S[h][w] == p)k++;\n else {\n if (k >= N) {\n for (ll i = w - k; i < w; i++) {\n er[h][i] = 1;\n }\n }\n p = S[h][w];\n k = 1;\n }\n }\n else {\n if (k >= N) {\n for (ll i = w - k; i < w; i++) {\n er[h][i] = 1;\n }\n }\n k = 0;\n p = '.';\n }\n }\n if (k >= N) {\n for (ll i = W - k; i < W; i++) {\n er[h][i] = 1;\n }\n }\n }\n rep(w, W) {\n char p = '&';\n ll k = 0;\n rep(h, H) {\n\n if (S[h][w] != '.') {\n if (S[h][w] == p)k++;\n else {\n if (k >= N) {\n for (ll i = h - k; i < h; i++) {\n er[i][w] = 1;\n }\n }\n p = S[h][w];\n k = 1;\n }\n }\n else {\n if (k >= N) {\n for (ll i = h - k; i < h; i++) {\n er[i][w] = 1;\n }\n }\n k = 0;\n p = '.';\n }\n }\n if (k >= N) {\n for (ll i = H - k; i < H; i++) {\n er[i][w] = 1;\n }\n }\n }\n bool C = 0;\n rep(h, H) {\n rep(w, W) {\n if (er[h][w]) {\n S[h][w] = '.';\n C = 1;\n }\n }\n }\n if (C == 0)break;\n \n }\n rep(h, H) {\n rep(w, W) {\n if (S[h][w] != '.')return false;\n }\n }\n return 1;\n}\n\nint main() {\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n\n\n cin >> H >> W >> N;\n vector<string> S(H);\n rep(i, H) {\n cin >> S[i];\n }\n\n\n\n rep(h, H) {\n rep(w, W - 1) {\n if (S[h][w] == S[h][w + 1])continue;\n swap(S[h][w], S[h][w + 1]);\n if (game(S)) {\n cout << \"YES\" << endl;\n return 0;\n }\n swap(S[h][w], S[h][w + 1]);\n }\n }\n cout << \"NO\" << endl;\n\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3460, "score_of_the_acc": -0.1321, "final_rank": 4 }, { "submission_id": "aoj_2232_6492343", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n// {{{ Templates\n\n// clang-format off\n\n// Macros\n#define over_load_(_1,_2,_3,_4,NAME,...) NAME\n#define rep(...) over_load_(__VA_ARGS__, rep4, rep3, rep2)(__VA_ARGS__)\n#define rep2(i, r) for ( int i = 0; i < static_cast<int>(r); (i) += 1)\n#define rep3(i, l, r) for ( int i = static_cast<int>(l); i < static_cast<int>(r); (i) += 1)\n#define rep4(i, l, r, stride) for ( int i = static_cast<int>(l); i < static_cast<int>(r); (i) += (stride))\n#define rrep(...) over_load_(__VA_ARGS__, rrep4, rrep3, rrep2)(__VA_ARGS__)\n#define rrep2(i, r) for ( int i = static_cast<int>(r) - 1; i >= 0; (i) -= 1)\n#define rrep3(i, l, r) for ( int i = static_cast<int>(r) - 1; i >= static_cast<int>(l); (i) -= 1)\n#define rrep4(i, l, r, stride) for ( int i = static_cast<int>(r) - 1; i >= static_cast<int>(l); (i) -= (stride))\n#define len(x) (static_cast<int>((x).size()))\n#define whole(f, x, ...) ([&](decltype((x)) container) { return (f)( begin(container), end(container), ## __VA_ARGS__); })(x)\n#define rwhole(f, x, ...) ([&](decltype((x)) container) { return (f)( rbegin(container), rend(container), ## __VA_ARGS__); })(x)\n#define debug(...) debug_function(#__VA_ARGS__, __VA_ARGS__)\n\n// Operators\ntemplate <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &p) { os << \"(\" << p.first << \",\" << p.second << \")\"; return os; }\ntemplate <typename T1, typename T2> ostream &operator<<(ostream &os, const map<T1, T2> &v) { bool is_first = true; for (auto x: v) { os << (is_first ? \"\" : \" \") << x; is_first = false; } return os; }\ntemplate <typename T> ostream &operator<<(ostream &os, queue<T> v) { bool is_first = true; while (!v.empty()) { os << (is_first?\"\":\" \")<<v.front(); v.pop(); is_first = false; } return os; }\ntemplate <typename T> ostream &operator<<(ostream &os, stack<T> v) { bool is_first = true; while (!v.empty()) { os << (is_first?\"\":\" \") << v.top(); v.pop(); is_first=false; } return os; }\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { rep (i, len(v)) os << v[i] << (i == len(v) - 1 ? \"\" : \" \"); return os; }\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<vector<T>> &v) { for (const auto &vec: v) { os << vec << '\\n'; } return os; }\ntemplate <typename T> ostream &operator<<(ostream &os, const deque<T> &v) { rep (i, len(v)) os << v[i] << (i == len(v) - 1 ? \"\" : \" \"); return os; }\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &v) { bool is_first = true; for (T x: v) { os << (is_first ? \"\" : \" \") << x; is_first = false; } return os; }\ntemplate <typename T> istream &operator>>(istream &is, vector<T> &v) { for (T &in: v) { is >> in; } return is; }\n\n// For debug macro\nint find_comma_not_bracketed(string_view s){ stack<char> bs; string lbs = \"({[\", rbs = \")}]\"; for (size_t i = 0; i < s.size(); i++) { if (lbs.find(s[i]) != string::npos) bs.push(s[i]); if (rbs.find(s[i]) != string::npos and !bs.empty()) bs.pop(); if (s[i] == ',' and bs.empty()) return i; } return s.size(); }\ntemplate <typename T, typename... Ts> void debug_function(string_view name, const T &a, Ts &&...rest) { int end = find_comma_not_bracketed(name); cerr << name.substr(0, end) << \":\" << a; if constexpr (sizeof...(rest) == 0) { cerr << '\\n'; } else { cerr << ' '; debug_function(name.substr(name.find_first_not_of(' ', end + 1)), forward<Ts>(rest)...); } }\n\n// Functions\ntemplate <typename T> vector<T> make_vector(size_t a, T b) { return vector<T>(a, b); }\ntemplate <typename... Ts> auto make_vector(size_t a, Ts... ts) { return vector<decltype(make_vector(ts...))>(a, make_vector(ts...)); }\ntemplate <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) { return a < b and (a = b, true); }\ntemplate <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { return a > b and (a = b, true); }\n\n// Structs\nstruct IoSetup { IoSetup(int x = 15) { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(x); cerr << fixed << setprecision(x); } } iosetup;\n\n// Type aliases\nusing ull = unsigned long long;\nusing ll = long long;\nusing pll = pair<ll, ll>;\nusing pii = pair<int, int>;\n\n// Literals\nconstexpr ll INF64 = INT64_MAX / 2;\nconstexpr int INF32 = INT32_MAX / 2;\nconstexpr int dy[] = { 0, 1, -1, 0, -1, 1, -1, 1 };\nconstexpr int dx[] = { 1, 0, 0, -1, -1, -1, 1, 1 };\nconstexpr int mod998244353 = 998244353;\nconstexpr int mod1000000007 = static_cast<int>(1e9) + 7;\nconstexpr char newl = '\\n';\n\n// clang-format on\n\n// }}} Templates\n\nnamespace tools {\n using namespace std;\n}\n\n#include <vector>\n\nnamespace tools {\n template <typename ArrayType>\n vector<ArrayType> rotate_cw(const vector<ArrayType> &s) {\n int h = s.size(), w = s[0].size();\n vector<ArrayType> res(w);\n for (int i = 0; i < w; i++) {\n for (int j = 0; j < h; j++) {\n res[i].push_back(s[h - 1 - j][i]);\n }\n }\n return res;\n }\n\n template <typename ArrayType>\n vector<ArrayType> rotate_ccw(const vector<ArrayType> &s) {\n int h = s.size(), w = s[0].size();\n vector<ArrayType> res(w);\n for (int i = 0; i < w; i++) {\n for (int j = 0; j < h; j++) {\n res[i].push_back(s[j][w - 1 - i]);\n }\n }\n return res;\n }\n} // namespace tools\nusing namespace tools;\n\n\nint h, w, n;\n\nvector<string> drop(vector<string> s) {\n vector<string> rotated = rotate_cw(s);\n rep(i, len(rotated)) {\n rep(k, len(rotated[i]) - 1) {\n rep(j, len(rotated[i]) - 1) {\n if (rotated[i][j] != '.')\n continue;\n\n swap(rotated[i][j], rotated[i][j + 1]);\n }\n }\n }\n\n return rotate_ccw(rotated);\n}\n\nvector<string> disappear(vector<string> s) {\n auto arrived = make_vector(h, w, false);\n auto erasable = make_vector(h, w, false);\n\n auto is_inside = [](int y, int x, int h, int w) -> bool {\n return 0 <= y and y < h and 0 <= x and x < w;\n };\n\n auto search_erasable = [&](int sy, int sx, char target) -> void {\n rep(dir, 4) {\n int cnt = 0;\n {\n int ny = sy;\n int nx = sx;\n for (; is_inside(ny, nx, h, w); ny += dy[dir], nx += dx[dir]) {\n if (s[ny][nx] != target)\n break;\n cnt++;\n }\n }\n if (cnt >= n) {\n int ny = sy;\n int nx = sx;\n for (; is_inside(ny, nx, h, w); ny += dy[dir], nx += dx[dir]) {\n if (s[ny][nx] != target)\n break;\n\n erasable[ny][nx] = true;\n }\n }\n }\n };\n\n rep(i, h) {\n rep(j, w) {\n if (s[i][j] == '.')\n continue;\n\n search_erasable(i, j, s[i][j]);\n }\n }\n\n rep(i, h) {\n rep(j, w) {\n if (erasable[i][j]) {\n s[i][j] = '.';\n }\n }\n }\n return s;\n}\n\nvoid output(const vector<string> &s) {\n rep(i, h) {\n cout << s[i] << newl;\n }\n}\n\nbool is_allclear(const vector<string> &s) {\n rep(i, h) {\n rep(j, w) {\n if (s[i][j] != '.')\n return false;\n }\n }\n return true;\n}\n\nbool solve(vector<string> s) {\n // debug(\"origin---------------begin\");\n // output(s);\n // debug(\"origin---------------end\");\n while (true) {\n auto dropped = drop(s);\n auto dis = disappear(dropped);\n if (is_allclear(dis)) {\n return true;\n }\n bool updated = s != dis;\n if (not updated)\n break;\n\n // debug(\"begin-----------\");\n // output(dis);\n // debug(\"end-----------\");\n s = dis;\n }\n return false;\n}\n\n\nint main() {\n cin >> h >> w >> n;\n\n vector<string> s(h);\n cin >> s;\n\n bool ans = false;\n\n rep(i, h) {\n rep(j, w - 1) {\n swap(s[i][j], s[i][j + 1]);\n ans = ans || solve(s);\n swap(s[i][j], s[i][j + 1]);\n }\n }\n ans = ans || solve(s);\n\n cout << (ans ? \"YES\" : \"NO\") << newl;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 3500, "score_of_the_acc": -0.4689, "final_rank": 15 }, { "submission_id": "aoj_2232_6308241", "code_snippet": "#include <bits/stdc++.h>\n#define all(v) v.begin(), v.end()\n#define rall(v) v.rbegin(), v.rend()\n#define rep(i,n) for(int i=0;i<(int)(n);i++)\n#define drep(i,j,n) for(int i=0;i<(int)(n-1);i++)for(int j=i+1;j<(int)(n);j++)\n#define trep(i,j,k,n) for(int i=0;i<(int)(n-2);i++)for(int j=i+1;j<(int)(n-1);j++)for(int k=j+1;k<(int)(n);k++)\n#define codefor int test;scanf(\"%d\",&test);while(test--)\n#define INT(...) int __VA_ARGS__;in(__VA_ARGS__)\n#define LL(...) ll __VA_ARGS__;in(__VA_ARGS__)\n#define yes(ans) if(ans)printf(\"yes\\n\");else printf(\"no\\n\")\n#define Yes(ans) if(ans)printf(\"Yes\\n\");else printf(\"No\\n\")\n#define YES(ans) if(ans)printf(\"YES\\n\");else printf(\"NO\\n\")\n#define popcount(v) __builtin_popcountll(v)\n#define vector2d(type,name,h,...) vector<vector<type>>name(h,vector<type>(__VA_ARGS__))\n#define vector3d(type,name,h,w,...) vector<vector<vector<type>>>name(h,vector<vector<type>>(w,vector<type>(__VA_ARGS__)))\n#define vector4d(type,name,h,w,d,...) vector<vector<vector<vector<type>>>>name(h,vector<vector<vector<type>>>(w,vector<vector<type>>(d,vector<type>(__VA_ARGS__))))\nusing namespace std;\nusing ll = long long;\ntemplate<class T> using rpriority_queue = priority_queue<T, vector<T>, greater<T>>;\nconst int MOD=1000000007;\nconst int MOD2=998244353;\nconst int INF=1<<30;\nconst ll INF2=1LL<<60;\nvoid scan(int& a){scanf(\"%d\",&a);}\nvoid scan(long long& a){scanf(\"%lld\",&a);}\ntemplate<class T,class L>void scan(pair<T, L>& p){scan(p.first);scan(p.second);}\ntemplate<class T,class U,class V>void scan(tuple<T,U,V>& p){scan(get<0>(p));scan(get<1>(p));scan(get<2>(p));}\ntemplate<class T, size_t size> void scan(array<T, size>& a){ for(auto&& i : a) scan(i);}\ntemplate<class T> void scan(T& a){cin>>a;}\ntemplate<class T> void scan(vector<T>& vec){for(auto&& it:vec)scan(it);}\nvoid in(){}\ntemplate <class Head, class... Tail> void in(Head& head, Tail&... tail){scan(head);in(tail...);}\nvoid print(const int& a){printf(\"%d\",a);}\nvoid print(const long long& a){printf(\"%lld\",a);}\nvoid print(const double& a){printf(\"%.15lf\",a);}\ntemplate<class T,class L>void print(const pair<T, L>& p){print(p.first);putchar(' ');print(p.second);}\ntemplate<class T> void print(const T& a){cout<<a;}\ntemplate<class T> void print(const vector<T>& vec){if(vec.empty())return;print(vec[0]);for(auto it=vec.begin();++it!= vec.end();){putchar(' ');print(*it);}}\nvoid out(){putchar('\\n');}\ntemplate<class T> void out(const T& t){print(t);putchar('\\n');}\ntemplate <class Head, class... Tail> void out(const Head& head,const Tail&... tail){print(head);putchar(' ');out(tail...);}\ntemplate<class T> void dprint(const T& a){cerr<<a;}\ntemplate<class T> void dprint(const vector<T>& vec){if(vec.empty())return;cerr<<vec[0];for(auto it=vec.begin();++it!= vec.end();){cerr<<\" \"<<*it;}}\nvoid debug(){cerr<<endl;}\ntemplate<class T> void debug(const T& t){dprint(t);cerr<<endl;}\ntemplate <class Head, class... Tail> void debug(const Head& head, const Tail&... tail){dprint(head);cerr<<\" \";debug(tail...);}\nll intpow(ll a, ll b){ ll ans = 1; while(b){ if(b & 1) ans *= a; a *= a; b /= 2; } return ans; }\nll modpow(ll a, ll b, ll p){ ll ans = 1; while(b){ if(b & 1) (ans *= a) %= p; (a *= a) %= p; b /= 2; } return ans; }\nll modinv(ll a, ll m) {ll b = m, u = 1, v = 0;while (b) {ll t = a / b;a -= t * b; swap(a, b);u -= t * v; swap(u, v);}u %= m;if (u < 0) u += m;return u;}\nll updivide(ll a,ll b){return (a+b-1)/b;}\nint msb(ll v){return 63-__builtin_clzll(v);}\ntemplate<class T> void chmax(T &a,const T b){if(b>a)a=b;}\ntemplate<class T> void chmin(T &a,const T b){if(b<a)a=b;}\n\nint main(){\n INT(h,w,n);\n vector<string> A(h);\n in(A);\n auto check=[&](vector<string> &D,vector<vector<bool>> &used,int y,int x){\n bool a=true,b=true,c=true,e=true;\n for(int d=0;d<n;d++){\n if(y+d>=h||D[y][x]!=D[y+d][x])a=false;\n if(y-d<0||D[y][x]!=D[y-d][x])b=false;\n if(x+d>=w||D[y][x]!=D[y][x+d])c=false;\n if(x-d<0||D[y][x]!=D[y][x-d])e=false;\n }\n for(int d=0;d<n;d++){\n if(a)used[y+d][x]=true;\n if(b)used[y-d][x]=true;\n if(c)used[y][x+d]=true;\n if(e)used[y][x-d]=true;\n }\n };\n auto g=[&](vector<string> &C){\n bool e=true;\n while(e){\n e=false;\n for(int y=0;y+1<h;y++){\n for(int x=0;x<w;x++){\n if(C[y][x]=='.')continue;\n if(C[y+1][x]=='.'){\n e=true;\n swap(C[y][x],C[y+1][x]);\n }\n }\n }\n }\n vector2d(bool,used,h,w,false);\n rep(y,h){\n rep(x,w){\n if(C[y][x]=='.')continue;\n check(C,used,y,x);\n }\n }\n bool res=false;\n rep(y,h){\n rep(x,w){\n if(used[y][x]){\n res=true;\n C[y][x]='.';\n }\n }\n }\n e=true;\n while(e){\n e=false;\n for(int y=0;y+1<h;y++){\n for(int x=0;x<w;x++){\n if(C[y][x]=='.')continue;\n if(C[y+1][x]=='.'){\n e=true;\n swap(C[y][x],C[y+1][x]);\n }\n }\n }\n }\n return res;\n };\n auto f=[&](vector<string> B){\n //rep(y,h)debug(B[y]);\n //cerr<<'\\n';\n while(g(B)){\n //rep(y,h)debug(B[y]);\n //cerr<<'\\n';\n }\n rep(y,h){\n rep(x,w){\n if(B[y][x]!='.')return false;\n }\n }\n return true;\n };\n for(int y=0;y<h;y++){\n for(int x=0;x<w;x++){\n if(A[y][x]=='.')continue;\n /*if(y+1<h){\n swap(A[y][x],A[y+1][x]);\n if(f(A)){\n out(\"YES\");\n return 0;\n }\n swap(A[y][x],A[y+1][x]);\n }*/\n if(x+1<w){\n swap(A[y][x],A[y][x+1]);\n if(f(A)){\n out(\"YES\");\n return 0;\n }\n swap(A[y][x],A[y][x+1]);\n }\n }\n }\n out(\"NO\");\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 3520, "score_of_the_acc": -0.6003, "final_rank": 16 }, { "submission_id": "aoj_2232_6026405", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n// #include <atcoder/all>\n// using namespace atcoder;\n// using mint = modint1000000007;\n// using mint2 = modint998244353;\n\ntypedef int64_t Int;\n#define all(x) (x).begin(), (x).end()\n \nconst double EPS = 1e-10;\nconst Int INF = 1e18;\nconst int inf = 1e9;\nconst Int mod = 1e9+7;\n//const Int mod = 998244353;\n\ntypedef struct {\n int64_t to, weight;\n} edge;\n\nInt dx[] = {0, 1, 0, -1, -1, 1, -1, 1};\nInt dy[] = {1, 0, -1, 0, -1, -1, 1, 1};\n\ntemplate<class T> \nistream &operator>>(istream &is, vector<T> &v) { \n for (auto &e : v) {\n is >> e; \n }\n return is; \n}\n\nbool print_space_enable = false;\nvoid print() { \n std::cout << '\\n'; \n print_space_enable = false;\n}\n\ntemplate <class Head, class... Tail>\nvoid print(Head&& head, Tail&&... tail) {\n if (print_space_enable) std::cout << \" \";\n std::cout << fixed << setprecision(15) << head;\n print_space_enable = true;\n print(std::forward<Tail>(tail)...);\n}\n\ntemplate<typename T>\nvoid print(vector<T> v) {\n for (size_t i = 0; i < v.size(); i++) {\n if (i > 0) std::cout << \" \";\n std::cout << v[i];\n }\n std::cout << '\\n';\n}\n\ntemplate<typename T>\nvoid print(vector<vector<T>> v) {\n for (size_t i = 0; i < v.size(); i++) {\n for (size_t j = 0; j < v[i].size(); j++) {\n if (j > 0) std::cout << \" \";\n std::cout << v[i][j];\n }\n std::cout << '\\n';\n }\n std::cout << '\\n';\n}\n\ntemplate<class T>\nvector<T> make_vec(size_t n, T val) {\n return vector<T>(n, val);\n}\n\ntemplate<class... Ts>\nauto make_vec(size_t n, Ts... ts) {\n return vector<decltype(make_vec(ts...))>(n, make_vec(ts...));\n}\n\nbool is_overflow(Int a, Int b) {\n return a > INF / b;\n}\n\nint h, w, n;\n\n// ランレングス圧縮\nvector<pair<char, int64_t>> run_length(string s) {\n vector<pair<char, int64_t>> ret;\n if (s.empty()) return ret;\n int64_t cnt = 0;\n char c = s[0];\n for (size_t i = 0; i < s.size(); i++) {\n if (s[i] == c) {\n cnt++;\n } else {\n ret.emplace_back(c, cnt);\n c = s[i];\n cnt = 1;\n }\n if (i + 1 == s.size()) {\n ret.emplace_back(c, cnt);\n }\n }\n return ret;\n}\n\nbool f(vector<string> s) {\n bool update = true;\n while (update) {\n update = false;\n bool update2 = true;\n while (update2) {\n update2 = false;\n for (int i = h - 1; i - 1 >= 0; i--) for (int j = 0; j < w; j++) {\n if (s[i][j] == '.' and s[i - 1][j] != '.') {\n swap(s[i][j], s[i - 1][j]);\n update2 = true;\n update = true;\n }\n }\n }\n auto del = make_vec(h, w, false);\n for (int i = 0; i < h; i++) {\n auto v = run_length(s[i]);\n int sum = 0;\n for (auto j : v) {\n if (j.first != '.' and j.second >= n) {\n for (int k = 0; k < j.second; k++) {\n del[i][sum + k] = true;\n }\n }\n sum += j.second;\n }\n }\n for (int j = 0; j < w; j++) {\n string t = \"\";\n for (int i = 0; i < h; i++) {\n t += s[i][j];\n }\n auto v = run_length(t);\n int sum = 0;\n for (auto i : v) {\n if (i.first != '.' and i.second >= n) {\n for (int k = 0; k < i.second; k++) {\n del[sum + k][j] = true;\n }\n }\n sum += i.second;\n }\n }\n for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) {\n if (del[i][j]) {\n update = true;\n s[i][j] = '.';\n }\n }\n \n // print(del);\n // cout << endl;\n // for (auto i : s) {\n // print(i);\n // } cout << endl;\n }\n for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) {\n if (s[i][j] != '.') {\n return false;\n }\n }\n return true;\n}\n\nvoid solve() {\n cin >> h >> w >> n;\n vector<string> s(h);\n cin >> s;\n for (int i = 0; i < h; i++) for (int j = 0; j + 1 < w; j++) {\n swap(s[i][j], s[i][j + 1]);\n if (f(s)) {\n print(\"YES\");\n return;\n }\n swap(s[i][j], s[i][j + 1]);\n }\n print(\"NO\");\n}\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n solve();\n //Int _t; cin >> _t; while (_t--) solve();\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3480, "score_of_the_acc": -0.2264, "final_rank": 8 }, { "submission_id": "aoj_2232_6002200", "code_snippet": "#pragma GCC target(\"avx,avx2\")\n//#pragma GCC optimize(\"Ofast\")\n//#undef LOCAL\n\n#include <unistd.h>\n#include <algorithm>\n#include <array>\n#include <cassert>\n#include <cctype>\n#include <cstring>\n#include <sstream>\n#include <string>\n#include <type_traits>\n#include <vector>\n\nnamespace yosupo {\n\nnamespace internal {\n\nint ceil_pow2(int n) {\n int x = 0;\n while ((1U << x) < (unsigned int)(n)) x++;\n return x;\n}\n\n} // namespace internal\n\nint bsf(unsigned int n) {\n return __builtin_ctz(n);\n}\nint bsf(unsigned long n) {\n return __builtin_ctzl(n);\n}\nint bsf(unsigned long long n) {\n return __builtin_ctzll(n);\n}\nint bsf(unsigned __int128 n) {\n unsigned long long low = (unsigned long long)(n);\n unsigned long long high = (unsigned long long)(n >> 64);\n return low ? __builtin_ctzll(low) : 64 + __builtin_ctzll(high);\n}\n\nint bsr(unsigned int n) {\n return 8 * (int)sizeof(unsigned int) - 1 - __builtin_clz(n);\n}\nint bsr(unsigned long n) {\n return 8 * (int)sizeof(unsigned long) - 1 - __builtin_clzl(n);\n}\nint bsr(unsigned long long n) {\n return 8 * (int)sizeof(unsigned long long) - 1 - __builtin_clzll(n);\n}\nint bsr(unsigned __int128 n) {\n unsigned long long low = (unsigned long long)(n);\n unsigned long long high = (unsigned long long)(n >> 64);\n return high ? 127 - __builtin_clzll(high) : 63 - __builtin_ctzll(low);\n}\n\nint popcnt(unsigned int n) {\n return __builtin_popcount(n);\n}\nint popcnt(unsigned long n) {\n return __builtin_popcountl(n);\n}\nint popcnt(unsigned long long n) {\n return __builtin_popcountll(n);\n}\n\n} // namespace yosupo\n\n#include <cassert>\n#include <numeric>\n#include <type_traits>\n\nnamespace yosupo {\n\nnamespace internal {\n\ntemplate <class T> using is_signed_int128 =\n typename std::conditional<std::is_same<T, __int128_t>::value || std::is_same<T, __int128>::value,\n std::true_type, std::false_type>::type;\n\ntemplate <class T> using is_unsigned_int128 =\n typename std::conditional<std::is_same<T, __uint128_t>::value ||\n std::is_same<T, unsigned __int128>::value,\n std::true_type, std::false_type>::type;\n\ntemplate <class T> using make_unsigned_int128 =\n typename std::conditional<std::is_same<T, __int128_t>::value, __uint128_t, unsigned __int128>;\n\ntemplate <class T> using is_integral =\n typename std::conditional<std::is_integral<T>::value || internal::is_signed_int128<T>::value ||\n internal::is_unsigned_int128<T>::value,\n std::true_type, std::false_type>::type;\n\ntemplate <class T> using is_signed_int =\n typename std::conditional<(is_integral<T>::value && std::is_signed<T>::value) ||\n is_signed_int128<T>::value,\n std::true_type, std::false_type>::type;\n\ntemplate <class T> using is_unsigned_int =\n typename std::conditional<(is_integral<T>::value && std::is_unsigned<T>::value) ||\n is_unsigned_int128<T>::value,\n std::true_type, std::false_type>::type;\n\ntemplate <class T> using to_unsigned =\n typename std::conditional<is_signed_int128<T>::value, make_unsigned_int128<T>,\n typename std::conditional<std::is_signed<T>::value, std::make_unsigned<T>,\n std::common_type<T>>::type>::type;\n\ntemplate <class T> using is_integral_t = std::enable_if_t<is_integral<T>::value>;\n\ntemplate <class T> using is_signed_int_t = std::enable_if_t<is_signed_int<T>::value>;\n\ntemplate <class T> using is_unsigned_int_t = std::enable_if_t<is_unsigned_int<T>::value>;\n\ntemplate <class T> using to_unsigned_t = typename to_unsigned<T>::type;\n\n} // namespace internal\n\n} // namespace yosupo\n\nnamespace yosupo {\n\nstruct Scanner {\npublic:\n Scanner(const Scanner&) = delete;\n Scanner& operator=(const Scanner&) = delete;\n\n Scanner(FILE* fp) : fd(fileno(fp)) {}\n\n void read() {}\n template <class H, class... T> void read(H& h, T&... t) {\n bool f = read_single(h);\n assert(f);\n read(t...);\n }\n\n int read_unsafe() { return 0; }\n template <class H, class... T> int read_unsafe(H& h, T&... t) {\n bool f = read_single(h);\n if (!f) return 0;\n return 1 + read_unsafe(t...);\n }\n\n int close() { return ::close(fd); }\n\nprivate:\n static constexpr size_t SIZE = 1 << 15;\n\n int fd = -1;\n std::array<char, SIZE> line;\n size_t st = 0, ed = 0;\n bool eof = false;\n\n bool read_single(std::string& ref) {\n if (!skip_space()) return false;\n ref = \"\";\n while (true) {\n char c = top();\n if (c <= ' ') break;\n ref += c;\n st++;\n }\n return true;\n }\n bool read_single(double& ref) {\n std::string s;\n if (!read_single(s)) return false;\n ref = std::stod(s);\n return true;\n }\n\n template <class T, std::enable_if_t<std::is_same<T, char>::value>* = nullptr>\n bool read_single(T& ref) {\n if (!skip_space(50)) return false;\n ref = top();\n st++;\n return true;\n }\n\n template <class T, internal::is_signed_int_t<T>* = nullptr,\n std::enable_if_t<!std::is_same<T, char>::value>* = nullptr>\n bool read_single(T& sref) {\n using U = internal::to_unsigned_t<T>;\n if (!skip_space(50)) return false;\n bool neg = false;\n if (line[st] == '-') {\n neg = true;\n st++;\n }\n U ref = 0;\n do {\n ref = 10 * ref + (line[st++] & 0x0f);\n } while (line[st] >= '0');\n sref = neg ? -ref : ref;\n return true;\n }\n template <class U, internal::is_unsigned_int_t<U>* = nullptr,\n std::enable_if_t<!std::is_same<U, char>::value>* = nullptr>\n bool read_single(U& ref) {\n if (!skip_space(50)) return false;\n ref = 0;\n do {\n ref = 10 * ref + (line[st++] & 0x0f);\n } while (line[st] >= '0');\n return true;\n }\n\n bool reread() {\n if (ed - st >= 50) return true;\n if (st > SIZE / 2) {\n std::memmove(line.data(), line.data() + st, ed - st);\n ed -= st;\n st = 0;\n }\n if (eof) return false;\n auto u = ::read(fd, line.data() + ed, SIZE - ed);\n if (u == 0) {\n eof = true;\n line[ed] = '\\0';\n u = 1;\n }\n ed += u;\n return true;\n }\n\n char top() {\n if (st == ed) {\n bool f = reread();\n assert(f);\n }\n return line[st];\n }\n\n bool skip_space(unsigned int token_len = 0) {\n while (true) {\n while (st != ed && line[st] <= ' ') st++;\n if (ed - st > token_len) return true;\n for (auto i = st; i < ed; i++) {\n if (line[i] <= ' ') return true;\n }\n if (!reread()) return false;\n }\n }\n};\n\nstruct Printer {\npublic:\n template <char sep = ' ', bool F = false> void write() {}\n template <char sep = ' ', bool F = false, class H, class... T> void write(const H& h, const T&... t) {\n if (F) write_single(sep);\n write_single(h);\n write<true>(t...);\n }\n template <char sep = ' ', class... T> void writeln(const T&... t) {\n write<sep>(t...);\n write_single('\\n');\n }\n\n Printer(FILE* _fp) : fd(fileno(_fp)) {}\n ~Printer() { flush(); }\n\n int close() {\n flush();\n return ::close(fd);\n }\n\n void flush() {\n if (pos) {\n auto res = ::write(fd, line.data(), pos);\n assert(res != -1);\n pos = 0;\n }\n }\n\nprivate:\n static std::array<std::array<char, 2>, 100> small;\n static std::array<unsigned long long, 20> tens;\n\n static constexpr size_t SIZE = 1 << 15;\n int fd;\n std::array<char, SIZE> line;\n size_t pos = 0;\n std::stringstream ss;\n\n template <class T, std::enable_if_t<std::is_same<char, T>::value>* = nullptr>\n void write_single(const T& val) {\n if (pos == SIZE) flush();\n line[pos++] = val;\n }\n\n template <class T, internal::is_signed_int_t<T>* = nullptr,\n std::enable_if_t<!std::is_same<char, T>::value>* = nullptr>\n void write_single(const T& val) {\n using U = internal::to_unsigned_t<T>;\n if (val == 0) {\n write_single('0');\n return;\n }\n if (pos > SIZE - 50) flush();\n U uval = val;\n if (val < 0) {\n write_single('-');\n uval = -uval;\n }\n write_unsigned(uval);\n }\n\n template <class U, internal::is_unsigned_int_t<U>* = nullptr> void write_single(U uval) {\n if (uval == 0) {\n write_single('0');\n return;\n }\n if (pos > SIZE - 50) flush();\n\n write_unsigned(uval);\n }\n\n template <class U, internal::is_unsigned_int_t<U>* = nullptr> static int calc_len(U x) {\n int i = (bsr(x) * 3 + 3) / 10;\n if (x < tens[i])\n return i;\n else\n return i + 1;\n }\n\n template <class U, internal::is_unsigned_int_t<U>* = nullptr,\n std::enable_if_t<8 >= sizeof(U)>* = nullptr>\n void write_unsigned(U uval) {\n size_t len = calc_len(uval);\n pos += len;\n\n char* ptr = line.data() + pos;\n while (uval >= 100) {\n ptr -= 2;\n memcpy(ptr, small[uval % 100].data(), 2);\n uval /= 100;\n }\n if (uval >= 10) {\n memcpy(ptr - 2, small[uval].data(), 2);\n } else {\n *(ptr - 1) = char('0' + uval);\n }\n }\n\n template <class U, std::enable_if_t<internal::is_unsigned_int128<U>::value>* = nullptr>\n void write_unsigned(U uval) {\n static std::array<char, 50> buf;\n size_t len = 0;\n while (uval > 0) {\n buf[len++] = char((uval % 10) + '0');\n uval /= 10;\n }\n std::reverse(buf.begin(), buf.begin() + len);\n memcpy(line.data() + pos, buf.data(), len);\n pos += len;\n }\n\n void write_single(const std::string& s) {\n for (char c : s) write_single(c);\n }\n void write_single(const char* s) {\n size_t len = strlen(s);\n for (size_t i = 0; i < len; i++) write_single(s[i]);\n }\n template <class T> void write_single(const std::vector<T>& val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i) write_single(' ');\n write_single(val[i]);\n }\n }\n};\n\nstd::array<std::array<char, 2>, 100> Printer::small = [] {\n std::array<std::array<char, 2>, 100> table;\n for (int i = 0; i <= 99; i++) {\n table[i][1] = char('0' + (i % 10));\n table[i][0] = char('0' + (i / 10 % 10));\n }\n return table;\n}();\nstd::array<unsigned long long, 20> Printer::tens = [] {\n std::array<unsigned long long, 20> table;\n for (int i = 0; i < 20; i++) {\n table[i] = 1;\n for (int j = 0; j < i; j++) {\n table[i] *= 10;\n }\n }\n return table;\n}();\n\n} // namespace yosupo\nusing namespace yosupo;\n\n#include <algorithm>\n#include <array>\n#include <bitset>\n#include <cassert>\n#include <complex>\n#include <cstdio>\n#include <cstring>\n#include <iostream>\n#include <map>\n#include <numeric>\n#include <queue>\n#include <set>\n#include <string>\n#include <unordered_map>\n#include <unordered_set>\n#include <vector>\n\nusing namespace std;\n\nusing uint = unsigned int;\nusing ll = long long;\nusing ull = unsigned long long;\nconstexpr ll TEN(int n) {\n return (n == 0) ? 1 : 10 * TEN(n - 1);\n}\ntemplate <class T> using V = vector<T>;\ntemplate <class T> using VV = V<V<T>>;\n\n#ifdef LOCAL\n\nostream& operator<<(ostream& os, __int128_t x) {\n if (x < 0) {\n os << \"-\";\n x *= -1;\n }\n if (x == 0) {\n return os << \"0\";\n }\n string s;\n while (x) {\n s += char(x % 10 + '0');\n x /= 10;\n }\n reverse(s.begin(), s.end());\n return os << s;\n}\nostream& operator<<(ostream& os, __uint128_t x) {\n if (x == 0) {\n return os << \"0\";\n }\n string s;\n while (x) {\n s += char(x % 10 + '0');\n x /= 10;\n }\n reverse(s.begin(), s.end());\n return os << s;\n}\n\ntemplate <class T, class U> ostream& operator<<(ostream& os, const pair<T, U>& p);\ntemplate <class T> ostream& operator<<(ostream& os, const V<T>& v);\ntemplate <class T> ostream& operator<<(ostream& os, const deque<T>& v);\ntemplate <class T, size_t N> ostream& operator<<(ostream& os, const array<T, N>& a);\ntemplate <class T> ostream& operator<<(ostream& os, const set<T>& s);\ntemplate <class T, class U> ostream& operator<<(ostream& os, const map<T, U>& m);\n\ntemplate <class T, class U> ostream& operator<<(ostream& os, const pair<T, U>& p) {\n return os << \"P(\" << p.first << \",\" << p.second << \")\";\n}\n\ntemplate <class T> ostream& operator<<(ostream& os, const V<T>& v) {\n os << \"[\";\n bool f = false;\n for (auto d : v) {\n if (f) os << \",\";\n f = true;\n os << d;\n }\n return os << \"]\";\n}\n\ntemplate <class T> ostream& operator<<(ostream& os, const deque<T>& v) {\n os << \"[\";\n bool f = false;\n for (auto d : v) {\n if (f) os << \",\";\n f = true;\n os << d;\n }\n return os << \"]\";\n}\ntemplate <class T, size_t N> ostream& operator<<(ostream& os, const array<T, N>& a) {\n os << \"[\";\n bool f = false;\n for (auto d : a) {\n if (f) os << \",\";\n f = true;\n os << d;\n }\n return os << \"]\";\n}\n\ntemplate <class T> ostream& operator<<(ostream& os, const set<T>& s) {\n os << \"{\";\n bool f = false;\n for (auto d : s) {\n if (f) os << \",\";\n f = true;\n os << d;\n }\n return os << \"}\";\n}\n\ntemplate <class T, class U> ostream& operator<<(ostream& os, const map<T, U>& s) {\n os << \"{\";\n bool f = false;\n for (auto p : s) {\n if (f) os << \",\";\n f = true;\n os << p.first << \": \" << p.second;\n }\n return os << \"}\";\n}\n\nstruct PrettyOS {\n ostream& os;\n bool first;\n\n template <class T> auto operator<<(T&& x) {\n if (!first) os << \",\";\n first = false;\n os << x;\n return *this;\n }\n};\ntemplate <class... T> void dbg0(T&&... t) {\n (PrettyOS{cerr, true} << ... << t);\n}\n#define dbg(...) \\\n do { \\\n cerr << __LINE__ << \": \" << #__VA_ARGS__ << \" = \"; \\\n dbg0(__VA_ARGS__); \\\n cerr << endl; \\\n } while (false);\n#else\n#define dbg(...)\n#endif\n\nScanner sc = Scanner(stdin);\nPrinter pr = Printer(stdout);\n\nbool all_same(string s) {\n bool ret = true;\n for (auto&& ch : s) {\n if (ch != s[0]) ret = false;\n }\n return ret;\n}\n\nint main() {\n int h, w, n;\n sc.read(h, w, n);\n V<string> b(h);\n for (auto&& line : b) {\n sc.read(line);\n }\n\n auto is_delete = [&]() {\n bool is_d = false;\n VV<int> d(h, V<int>(w, 0));\n // 行方向\n int i = 0;\n for (auto&& line : b) {\n for (int j = 0; j < w - n + 1; j++) {\n if (line[j] == '.') continue;\n if (all_same(line.substr(j, n))) {\n is_d = true;\n for (int k = 0; k < n; k++) {\n d[i][j + k] = 1;\n }\n }\n }\n i++;\n }\n // 列方向\n for (int j = 0; j < w; j++) {\n for (int i = 0; i < h - n + 1; i++) {\n if (b[i][j] == '.') continue;\n string line = \"\";\n for (int k = 0; k < n; k++) {\n line += b[i + k][j];\n }\n if (all_same(line)) {\n is_d = true;\n for (int k = 0; k < n; k++) {\n d[i + k][j] = 1;\n }\n }\n }\n }\n\n for (int i = 0; i < h; i++) {\n for (int j = 0; j < w; j++) {\n if (d[i][j] == 1) {\n b[i][j] = '.';\n }\n }\n }\n\n return is_d;\n };\n\n auto is_completed = [&]() {\n bool ret = true;\n for (auto&& line : b) {\n for (auto&& ch : line) {\n if (ch != '.') ret = false;\n }\n }\n return ret;\n };\n\n V<string> ori_b = b;\n // bに操作を加える全探索\n for (int ch_h1 = 0; ch_h1 < h; ch_h1++) {\n for (int ch_w1 = 0; ch_w1 < w; ch_w1++) {\n for (int ch_w2 = 0; ch_w2 < w; ch_w2++) {\n if (ch_h1 != 0 && ch_w1 == ch_w2) continue;\n int manh_d = 0;\n manh_d = abs(ch_w1 - ch_w2);\n if (manh_d > 1) continue;\n b = ori_b;\n // if (b[ch_h1][ch_w1] == '.' || b[ch_h1][ch_w2] == '.') continue;\n swap(b[ch_h1][ch_w1], b[ch_h1][ch_w2]);\n // 落下処理\n for (int j = 0; j < w; j++) {\n for (int k = 0; k < h; k++) {\n // kは使わない\n for (int i = h - 1; i > 0; i--) {\n if (b[i][j] == '.' && b[i - 1][j] != '.') {\n b[i][j] = b[i - 1][j];\n b[i - 1][j] = '.';\n }\n }\n }\n }\n while (is_delete()) {\n // 落下処理\n for (int j = 0; j < w; j++) {\n for (int k = 0; k < h; k++) {\n // kは使わない\n for (int i = h - 1; i > 0; i--) {\n if (b[i][j] == '.' && b[i - 1][j] != '.') {\n b[i][j] = b[i - 1][j];\n b[i - 1][j] = '.';\n }\n }\n }\n }\n }\n // 全部'.'ならYES\n if (is_completed()) {\n pr.writeln(\"YES\");\n return 0;\n }\n }\n }\n }\n // for (auto &&line : ori_b)\n // {\n // pr.writeln(line);\n // }\n\n pr.writeln(\"NO\");\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 3468, "score_of_the_acc": -0.355, "final_rank": 10 }, { "submission_id": "aoj_2232_5998688", "code_snippet": "#include <bits/stdc++.h>\n#define rep(i, n) for (int i = 0; i < n; ++i)\ntypedef long long ll;\nusing namespace std;\n\nint H, W, N;\n\nvector<string> fall(vector<string> &s) {\n string tmp = \"\";\n rep(i, W) tmp += '.';\n vector<string> next(H, tmp);\n\n rep(j, W) {\n queue<char> que;\n for (int i = H - 1; i >= 0; --i) {\n if (s[i][j] != '.')\n que.push(s[i][j]);\n }\n\n int i = H - 1;\n while (!que.empty()) {\n char c = que.front();\n que.pop();\n next[i][j] = c;\n i--;\n }\n }\n\n return next;\n}\n\nbool solve(vector<string> s, vector<string> pre = {}) {\n if (s == pre) {\n rep(i, H) rep(j, W) {\n if (s[i][j] != '.')\n return false;\n }\n return true;\n }\n\n pre = s;\n vector<vector<bool>> flag(H, vector<bool>(W));\n\n // 横で消えるやつの判定\n rep(i, H) {\n char c = s[i][0];\n int cur = 0;\n rep(j, W + 1) {\n if (j != W && s[i][j] == c) {\n cur++;\n } else {\n if (cur >= N)\n rep(k, cur) flag[i][j - k - 1] = true;\n\n if (j != W) {\n cur = 1;\n c = s[i][j];\n }\n }\n }\n }\n\n // 縦で消えるやつの判定\n rep(j, W) {\n char c = s[0][j];\n int cur = 0;\n rep(i, H + 1) {\n if (i != H && s[i][j] == c) {\n cur++;\n } else {\n if (cur >= N)\n rep(k, cur) flag[i - k - 1][j] = true;\n\n if (i != H) {\n cur = 1;\n c = s[i][j];\n }\n }\n }\n }\n\n // 消す\n rep(i, H) rep(j, W) {\n if (flag[i][j]) s[i][j] = '.';\n }\n\n // 落下させる\n vector<string> next = fall(s);\n return solve(next, pre);\n}\n\nint main() {\n cin >> H >> W >> N;\n vector<string> S(H);\n rep(i, H) cin >> S[i];\n\n auto inGrid = [&](int y, int x) -> bool {\n if (y >= 0 && y < H && x >= 0 && x < W)\n return true;\n else\n return false;\n };\n\n rep(y, H) rep(x, W) {\n for (int dx = -1; dx <= 1; dx += 2) {\n if (S[y][x] == '.') continue;\n\n int ny = y;\n int nx = x + dx;\n\n if (!inGrid(ny, nx)) continue;\n\n swap(S[y][x], S[ny][nx]);\n vector<string> t = fall(S);\n\n if (solve(t)) {\n cout << \"YES\" << endl;\n return 0;\n }\n swap(S[y][x], S[ny][nx]);\n }\n }\n\n cout << \"NO\" << endl;\n\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3644, "score_of_the_acc": -1.037, "final_rank": 19 }, { "submission_id": "aoj_2232_5978124", "code_snippet": "#include <bits/stdc++.h>\n#define rep(i,n) for(int i=0;i<(n);i++)\n#define chmin(x,y) x = min(x,y)\n#define chmax(x,y) x = max(x,y)\nusing namespace std;\n\nint h,w,n;\n\nbool game(vector<string> &s){\n vector<string> s2(h);\n copy(s.begin(),s.end(),s2.begin());\n rep(j,w){\n int d=0;\n for(int i=h-1;i>=0;i--){\n while(i-d>=0&&s2[i-d][j]=='.') d++;\n if(i-d<0) s[i][j]='.';\n else s[i][j]=s2[i-d][j];\n }\n }\n\n \n bool first=true;\n bool koushin=true;\n while(koushin){\n koushin=false;\n vector<string> s2(h);\n copy(s.begin(),s.end(),s2.begin());\n\n rep(j,w){\n int d=0;\n for(int i=h-1;i>=0;i--){\n\twhile(i-d>=0&&s2[i-d][j]=='.') d++;\n\tif(i-d<0) s[i][j]='.';\n\telse s[i][j]=s2[i-d][j];\n }\n }\n \n rep(i,h){\n rep(j,w){\n\tif(s[i][j]=='.') continue;\n\tint cnt=1;\n\twhile(j+cnt<w&&s[i][j]==s[i][j+cnt]) cnt++;\n\tif(cnt>=n){\n\t rep(k,cnt) s2[i][j+k]='.';\n\t koushin=true;\n\t}\n\tj+=cnt-1;\n }\n }\n rep(j,w){\n rep(i,h){\n\tif(s[i][j]=='.') continue;\n\tint cnt=1;\n\twhile(i+cnt<h&&s[i][j]==s[i+cnt][j]) cnt++;\n\tif(cnt>=n){\n\t rep(k,cnt) s2[i+k][j]='.';\n\t koushin=true;\n\t}\n\ti+=cnt-1;\n }\n }\n \n rep(j,w){\n int d=0;\n for(int i=h-1;i>=0;i--){\n\twhile(i-d>=0&&s2[i-d][j]=='.') d++;\n\tif(i-d<0) s[i][j]='.';\n\telse s[i][j]=s2[i-d][j];\n }\n }\n \n if(first){\n koushin=true;\n first=false;\n }\n }\n\n bool ret=true;\n rep(i,h) rep(j,w)\n if(s[i][j]!='.') ret=false;\n return ret;\n}\n\n \nint main(){\n cin>>h>>w>>n;\n vector<string> s(h);\n rep(i,h) cin>>s[i];\n\n bool ans=false;\n rep(i,h) rep(j,w-1){\n vector<string> s2(h);\n copy(s.begin(),s.end(),s2.begin());\n \n swap(s2[i][j],s2[i][j+1]);\n bool f=game(s2);\n ans=ans||f;\n }\n \n if(ans) puts(\"YES\");\n else puts(\"NO\");\n \n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3452, "score_of_the_acc": -0.0943, "final_rank": 2 }, { "submission_id": "aoj_2232_5960610", "code_snippet": "#define rep(i, n) for(int i=0; i<(n); ++i)\n#define rrep(i, n) for(int i=(n-1); i>=0; --i)\n#define rep2(i, s, n) for(int i=s; i<(n); ++i)\n#define ALL(v) (v).begin(), (v).end()\n#define memr(dp, val) memset(dp, val, sizeof(dp))\n#define fi first\n#define se second\ntemplate<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }\ntemplate<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }\n#include <bits/stdc++.h>\nusing namespace std;\ntemplate<typename T>\nusing min_priority_queue = priority_queue<T, vector<T>, greater<T> >;\ntypedef long long ll;\nconst int INTINF = INT_MAX >> 1;\nstatic const ll LLINF = (LLONG_MAX >> 1);\n\nint h, w, n;\nstring str[30];\n\nbool input(){\n\n\tcin >> h >> w >> n;\n\trep(i, h){\n\t\tcin >> str[i];\n\t}\n\n\treturn true;\n}\n\nbool erase(){\n\tbool flag[30][30];\n\tmemr(flag, 0);\n\trep(i, h){\n\t\trep(j, w){\n\t\t\tif(str[i][j] == '.') continue;\n\t\t\tint cnt = 1;\n\t\t\trep2(k, j+1, w){\n\t\t\t\tif(str[i][j] == str[i][k]) cnt++;\n\t\t\t\telse break;\n\t\t\t}\n\t\t\tif(cnt >= n){\n\t\t\t\trep2(k, j, cnt + j){\n\t\t\t\t\tflag[i][k] = true;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\t\n\trep(j, w){\n\t\trep(i, h){\n\t\t\tif(str[i][j] == '.') continue;\n\t\t\tint cnt = 1;\n\t\t\trep2(k, i+1, h){\n\t\t\t\tif(str[i][j] == str[k][j]) cnt++;\n\t\t\t\telse break;\n\t\t\t}\n\t\t\tif(cnt >= n){\n\t\t\t\trep2(k, i, cnt + i){\n\t\t\t\t\tflag[k][j] = true;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tbool ret = false;\n\trep(i, h){\n\t\trep(j, w){\n\t\t\tif(flag[i][j]) {\n\t\t\t\tstr[i][j] = '.';\n\t\t\t\tret = true;\n\t\t\t}\n\t\t}\n\t}\n\treturn ret;\n}\n\nvoid drop(){\n\trep(k, 40){\n\t\trrep(i, h){\n\t\t\trep(j, w){\n\t\t\t\tif(i > 0)\n\t\t\t\tif(str[i-1][j] != '.' && str[i][j] == '.'){\n\t\t\t\t\tstr[i][j] = str[i-1][j];\n\t\t\t\t\tstr[i-1][j] = '.';\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\n\nint main(){\n\n\tinput();\n\n\tbool res = false;\n\trep(i, h){\n\t\trep(j, w-1){\n\t\t\tstring str2[30];\n\t\t\trep(i, h) str2[i] = str[i];\n\t\t\tswap(str[i][j], str[i][j + 1]);\n\t\t\tdrop();\n\t\t\twhile(erase()){\n\t\t\t\t// rep(i, h){\n\t\t\t\t// \trep(j, w){\n\t\t\t\t// \t\tcout << str[i][j];\n\t\t\t\t// \t}\n\t\t\t\t// \tcout << endl;\n\t\t\t\t// }\n\t\t\t\t// cout << endl;\n\t\t\t\tdrop();\n\t\t\t}\n\t\t\t\n\t\t\tbool ok = true;\n\t\t\trep(i, h){\n\t\t\t\trep(j, w){\n\t\t\t\t\tif(str[i][j] != '.') ok = false;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(ok) res = true;\n\t\t\t\n\t\t\trep(i, h) str[i] = str2[i];\n\t\t}\n\t}\n\t\n\tstring ans = res ? \"YES\" : \"NO\";\n\tcout << ans << endl;\n\n return 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3432, "score_of_the_acc": -0.0741, "final_rank": 1 }, { "submission_id": "aoj_2232_5941562", "code_snippet": "#include <bits/stdc++.h>\n#define rep(i,n) for(int i = 0; i < (n); i++)\nusing namespace std;\ntypedef long long ll;\n\nint main(){\n cin.tie(0);\n ios::sync_with_stdio(0);\n \n int h,w,n; cin >> h >> w >> n;\n vector<string> s(h);\n rep(i,h) cin >> s[i];\n\n\n bool ok = false;\n rep(i,h)rep(j,w-1) {\n auto t = s;\n swap(t[i][j], t[i][j + 1]);\n\n auto drop = [&](void) {\n for(int i = h - 2; i >= 0; i--) {\n rep(j,w) {\n if(t[i][j] != '.' && t[i + 1][j] == '.') {\n int pos = i;\n while(t[pos + 1][j] == '.') {\n swap(t[pos][j], t[pos + 1][j]);\n pos++;\n if(pos == h - 1) break;\n }\n }\n }\n }\n };\n\n\n int dy[] = {1, 0};\n int dx[] = {0, 1};\n function<bool(void)> win = [&](void) -> bool {\n bool ex = false;\n rep(i,h)rep(j,w) if(t[i][j] != '.') ex = true;\n if(!ex) {\n return true;\n }\n\n bool flag = false;\n vector<vector<bool>> erased(h, vector<bool> (w, false));\n rep(i,h)rep(j,w) {\n if(t[i][j] == '.') continue;\n for(int k = 0; k < 2; k++) {\n if(i + dy[k] * (n - 1) < h && j + dx[k] * (n - 1) < w) {\n bool same = true;\n for(int l = 0; l < n; l++) {\n if(t[i][j] != t[i + dy[k] * l][j + dx[k] * l]) same = false;\n }\n if(same) {\n flag = true;\n for(int l = 0; l < n; l++)\n erased[i + dy[k] * l][j + dx[k] * l] = true;\n }\n }\n }\n }\n\n if(!flag) return false;\n rep(i,h)rep(j,w) if(erased[i][j]) t[i][j] = '.';\n drop();\n return win();\n };\n drop();\n if(win()) ok = true;\n }\n cout << (ok ? \"YES\" : \"NO\") << endl;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3512, "score_of_the_acc": -0.3774, "final_rank": 11 }, { "submission_id": "aoj_2232_5810538", "code_snippet": "#line 1 \"/Users/nok0/Documents/Programming/nok0/cftemp.hpp\"\n#include <bits/stdc++.h>\nusing namespace std;\n\n#pragma region Macros\n// rep macro\n#define foa(v, a) for(auto &v : a)\n#define REPname(a, b, c, d, e, ...) e\n#define REP(...) REPname(__VA_ARGS__, REP3, REP2, REP1, REP0)(__VA_ARGS__)\n#define REP0(x) for(int i = 0; i < (x); ++i)\n#define REP1(i, x) for(int i = 0; i < (x); ++i)\n#define REP2(i, l, r) for(int i = (l); i < (r); ++i)\n#define REP3(i, l, r, c) for(int i = (l); i < (r); i += (c))\n#define REPSname(a, b, c, ...) c\n#define REPS(...) REPSname(__VA_ARGS__, REPS1, REPS0)(__VA_ARGS__)\n#define REPS0(x) for(int i = 1; i <= (x); ++i)\n#define REPS1(i, x) for(int i = 1; i <= (x); ++i)\n#define RREPname(a, b, c, d, e, ...) e\n#define RREP(...) RREPname(__VA_ARGS__, RREP3, RREP2, RREP1, RREP0)(__VA_ARGS__)\n#define RREP0(x) for(int i = (x)-1; i >= 0; --i)\n#define RREP1(i, x) for(int i = (x)-1; i >= 0; --i)\n#define RREP2(i, r, l) for(int i = (r)-1; i >= (l); --i)\n#define RREP3(i, r, l, c) for(int i = (r)-1; i >= (l); i -= (c))\n#define RREPSname(a, b, c, ...) c\n#define RREPS(...) RREPSname(__VA_ARGS__, RREPS1, RREPS0)(__VA_ARGS__)\n#define RREPS0(x) for(int i = (x); i >= 1; --i)\n#define RREPS1(i, x) for(int i = (x); i >= 1; --i)\n\n// name macro\n#define pb push_back\n#define eb emplace_back\n#define SZ(x) ((int)(x).size())\n#define all(x) (x).begin(), (x).end()\n#define rall(x) (x).rbegin(), (x).rend()\n#define popcnt(x) __builtin_popcountll(x)\ntemplate <class T = int>\nusing V = std::vector<T>;\ntemplate <class T = int>\nusing VV = std::vector<std::vector<T>>;\ntemplate <class T>\nusing pqup = std::priority_queue<T, std::vector<T>, std::greater<T>>;\nusing ll = long long;\nusing ld = long double;\nusing int128 = __int128_t;\nusing pii = std::pair<int, int>;\nusing pll = std::pair<long long, long long>;\n\n// input macro\ntemplate <class T, class U>\nstd::istream &operator>>(std::istream &is, std::pair<T, U> &p) {\n\tis >> p.first >> p.second;\n\treturn is;\n}\ntemplate <class T>\nstd::istream &operator>>(std::istream &is, std::vector<T> &v) {\n\tfor(T &i : v) is >> i;\n\treturn is;\n}\nstd::istream &operator>>(std::istream &is, __int128_t &a) {\n\tstd::string s;\n\tis >> s;\n\t__int128_t ret = 0;\n\tfor(int i = 0; i < s.length(); i++)\n\t\tif('0' <= s[i] and s[i] <= '9')\n\t\t\tret = 10 * ret + s[i] - '0';\n\ta = ret * (s[0] == '-' ? -1 : 1);\n\treturn is;\n}\nnamespace scanner {\nvoid scan(int &a) { std::cin >> a; }\nvoid scan(long long &a) { std::cin >> a; }\nvoid scan(std::string &a) { std::cin >> a; }\nvoid scan(char &a) { std::cin >> a; }\nvoid scan(char a[]) { std::scanf(\"%s\", a); }\nvoid scan(double &a) { std::cin >> a; }\nvoid scan(long double &a) { std::cin >> a; }\ntemplate <class T, class U>\nvoid scan(std::pair<T, U> &p) { std::cin >> p; }\ntemplate <class T>\nvoid scan(std::vector<T> &a) { std::cin >> a; }\nvoid INPUT() {}\ntemplate <class Head, class... Tail>\nvoid INPUT(Head &head, Tail &... tail) {\n\tscan(head);\n\tINPUT(tail...);\n}\n} // namespace scanner\n#define VEC(type, name, size) \\\n\tstd::vector<type> name(size); \\\n\tscanner::INPUT(name)\n#define VVEC(type, name, h, w) \\\n\tstd::vector<std::vector<type>> name(h, std::vector<type>(w)); \\\n\tscanner::INPUT(name)\n#define INT(...) \\\n\tint __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define LL(...) \\\n\tlong long __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define STR(...) \\\n\tstd::string __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define CHAR(...) \\\n\tchar __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define DOUBLE(...) \\\n\tdouble __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define LD(...) \\\n\tlong double __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n\n// output-macro\ntemplate <class T, class U>\nstd::ostream &operator<<(std::ostream &os, const std::pair<T, U> &p) {\n\tos << p.first << \" \" << p.second;\n\treturn os;\n}\ntemplate <class T>\nstd::ostream &operator<<(std::ostream &os, const std::vector<T> &a) {\n\tfor(int i = 0; i < int(a.size()); ++i) {\n\t\tif(i) os << \" \";\n\t\tos << a[i];\n\t}\n\treturn os;\n}\nstd::ostream &operator<<(std::ostream &dest, __int128_t &value) {\n\tstd::ostream::sentry s(dest);\n\tif(s) {\n\t\t__uint128_t tmp = value < 0 ? -value : value;\n\t\tchar buffer[128];\n\t\tchar *d = std::end(buffer);\n\t\tdo {\n\t\t\t--d;\n\t\t\t*d = \"0123456789\"[tmp % 10];\n\t\t\ttmp /= 10;\n\t\t} while(tmp != 0);\n\t\tif(value < 0) {\n\t\t\t--d;\n\t\t\t*d = '-';\n\t\t}\n\t\tint len = std::end(buffer) - d;\n\t\tif(dest.rdbuf()->sputn(d, len) != len) {\n\t\t\tdest.setstate(std::ios_base::badbit);\n\t\t}\n\t}\n\treturn dest;\n}\ntemplate <class T>\nvoid print(const T a) { std::cout << a << '\\n'; }\ntemplate <class Head, class... Tail>\nvoid print(Head H, Tail... T) {\n\tstd::cout << H << ' ';\n\tprint(T...);\n}\ntemplate <class T>\nvoid printel(const T a) { std::cout << a << '\\n'; }\ntemplate <class T>\nvoid printel(const std::vector<T> &a) {\n\tfor(const auto &v : a)\n\t\tstd::cout << v << '\\n';\n}\ntemplate <class Head, class... Tail>\nvoid printel(Head H, Tail... T) {\n\tstd::cout << H << '\\n';\n\tprintel(T...);\n}\nvoid Yes(const bool b = true) { std::cout << (b ? \"Yes\\n\" : \"No\\n\"); }\nvoid No() { std::cout << \"No\\n\"; }\nvoid YES(const bool b = true) { std::cout << (b ? \"YES\\n\" : \"NO\\n\"); }\nvoid NO() { std::cout << \"NO\\n\"; }\nvoid err(const bool b = true) {\n\tif(b) {\n\t\tstd::cout << \"-1\\n\", exit(0);\n\t}\n}\n\n//debug macro\nnamespace debugger {\ntemplate <class T>\nvoid view(const std::vector<T> &a) {\n\tstd::cerr << \"{ \";\n\tfor(const auto &v : a) {\n\t\tstd::cerr << v << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\ntemplate <class T>\nvoid view(const std::vector<std::vector<T>> &a) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &v : a) {\n\t\tstd::cerr << \"\\t\";\n\t\tview(v);\n\t\tstd::cerr << \"\\n\";\n\t}\n\tstd::cerr << \"}\";\n}\ntemplate <class T, class U>\nvoid view(const std::vector<std::pair<T, U>> &a) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &p : a) std::cerr << \"\\t(\" << p.first << \", \" << p.second << \")\\n\";\n\tstd::cerr << \"}\";\n}\ntemplate <class T, class U>\nvoid view(const std::map<T, U> &m) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &p : m) std::cerr << \"\\t[\" << p.first << \"] : \" << p.second << \"\\n\";\n\tstd::cerr << \"}\";\n}\ntemplate <class T, class U>\nvoid view(const std::pair<T, U> &p) { std::cerr << \"(\" << p.first << \", \" << p.second << \")\"; }\ntemplate <class T>\nvoid view(const std::set<T> &s) {\n\tstd::cerr << \"{ \";\n\tfor(auto &v : s) {\n\t\tview(v);\n\t\tstd::cerr << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\n\ntemplate <class T>\nvoid view(const T &e) { std::cerr << e; }\n} // namespace debugger\n#ifdef LOCAL\nvoid debug_out() {}\ntemplate <typename Head, typename... Tail>\nvoid debug_out(Head H, Tail... T) {\n\tdebugger::view(H);\n\tstd::cerr << \", \";\n\tdebug_out(T...);\n}\n#define debug(...) \\\n\tdo { \\\n\t\tstd::cerr << __LINE__ << \" [\" << #__VA_ARGS__ << \"] : [\"; \\\n\t\tdebug_out(__VA_ARGS__); \\\n\t\tstd::cerr << \"\\b\\b]\\n\"; \\\n\t} while(false)\n#else\n#define debug(...) (void(0))\n#endif\n\n// vector macro\ntemplate <class T>\nint lb(const std::vector<T> &a, const T x) { return std::distance((a).begin(), std::lower_bound((a).begin(), (a).end(), (x))); }\ntemplate <class T>\nint ub(const std::vector<T> &a, const T x) { return std::distance((a).begin(), std::upper_bound((a).begin(), (a).end(), (x))); }\ntemplate <class T>\nvoid UNIQUE(std::vector<T> &a) {\n\tstd::sort(a.begin(), a.end());\n\ta.erase(std::unique(a.begin(), a.end()), a.end());\n}\ntemplate <class T>\nstd::vector<T> press(std::vector<T> &a) {\n\tauto res = a;\n\tUNIQUE(res);\n\tfor(auto &v : a)\n\t\tv = lb(res, v);\n\treturn res;\n}\n#define SORTname(a, b, c, ...) c\n#define SORT(...) SORTname(__VA_ARGS__, SORT1, SORT0, ...)(__VA_ARGS__)\n#define SORT0(a) std::sort((a).begin(), (a).end())\n#define SORT1(a, c) std::sort((a).begin(), (a).end(), [](const auto x, const auto y) { return x c y; })\ntemplate <class T>\nvoid ADD(std::vector<T> &a, const T x = 1) {\n\tfor(auto &v : a) v += x;\n}\ntemplate <class T>\nvoid SUB(std::vector<T> &a, const T x = 1) {\n\tfor(auto &v : a) v -= x;\n}\ntemplate <class T>\nvoid MUL(std::vector<T> &a, const T x) {\n\tfor(auto &v : a) v *= x;\n}\ntemplate <class T>\nvoid DIV(std::vector<T> &a, const T x) {\n\tfor(auto &v : a) v /= x;\n}\nstd::vector<std::pair<char, int>> rle(const string &s) {\n\tint n = s.size();\n\tstd::vector<std::pair<char, int>> ret;\n\tfor(int l = 0; l < n;) {\n\t\tint r = l + 1;\n\t\tfor(; r < n and s[l] == s[r]; r++) {}\n\t\tret.emplace_back(s[l], r - l);\n\t\tl = r;\n\t}\n\treturn ret;\n}\ntemplate <class T>\nstd::vector<std::pair<T, int>> rle(const std::vector<T> &v) {\n\tint n = v.size();\n\tstd::vector<std::pair<T, int>> ret;\n\tfor(int l = 0; l < n;) {\n\t\tint r = l + 1;\n\t\tfor(; r < n and v[l] == v[r]; r++) {}\n\t\tret.emplace_back(v[l], r - l);\n\t\tl = r;\n\t}\n\treturn ret;\n}\n\n// math macro\ntemplate <class T, class U>\ninline bool chmin(T &a, const U &b) { return a > b ? a = b, true : false; }\ntemplate <class T, class U>\ninline bool chmax(T &a, const U &b) { return a < b ? a = b, true : false; }\ntemplate <class T>\nT divup(T x, T y) { return (x + y - 1) / y; }\ntemplate <class T>\nT POW(T a, long long n) {\n\tT ret = 1;\n\twhile(n) {\n\t\tif(n & 1) ret *= a;\n\t\ta *= a;\n\t\tn >>= 1;\n\t}\n\treturn ret;\n}\n// modpow\nlong long POW(long long a, long long n, const int mod) {\n\tlong long ret = 1;\n\ta = (a % mod + mod) % mod;\n\twhile(n) {\n\t\tif(n & 1) (ret *= a) %= mod;\n\t\t(a *= a) %= mod;\n\t\tn >>= 1;\n\t}\n\treturn ret;\n}\n\n// others\nstruct fast_io {\n\tfast_io() {\n\t\tios::sync_with_stdio(false);\n\t\tcin.tie(nullptr);\n\t\tcout << fixed << setprecision(15);\n\t}\n} fast_io_;\nconst int inf = 1e9;\nconst ll INF = 1e18;\n#pragma endregion\n\nvoid main_();\n\nint main() {\n\tmain_();\n\treturn 0;\n}\n#line 2 \"a.cpp\"\n\n//Grid Template\nint dx[9] = {1, 0, -1, 0, 1, -1, -1, 1, 0};\nint dy[9] = {0, 1, 0, -1, 1, 1, -1, -1, 0};\n\nvoid main_() {\n\tINT(h, w, n);\n\tVEC(string, s, h);\n\tauto inside = [&](int x, int y) {\n\t\treturn x >= 0 and x < h and y >= 0 and y < w;\n\t};\n\tauto simulate = [&](vector<string> &s) -> bool {\n\t\twhile(true) {\n\t\t\tRREP(i, h - 1) {\n\t\t\t\tREP(j, w) {\n\t\t\t\t\tif(s[i][j] == '.') continue;\n\t\t\t\t\tint t = 1;\n\t\t\t\t\twhile(i + t < h and s[i + t][j] == '.') {\n\t\t\t\t\t\tswap(s[i + t][j], s[i + t - 1][j]);\n\t\t\t\t\t\tt++;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tset<pii> del;\n\t\t\tREP(i, h) {\n\t\t\t\tREP(j, w) {\n\t\t\t\t\tif(s[i][j] == '.') continue;\n\t\t\t\t\tbool f = 0;\n\t\t\t\t\tREP(k, 2) {\n\t\t\t\t\t\tint nx = i + dx[k], ny = j + dy[k];\n\t\t\t\t\t\tint cnt = 1;\n\t\t\t\t\t\twhile(inside(nx, ny) and s[i][j] == s[nx][ny])\n\t\t\t\t\t\t\tcnt++, nx += dx[k], ny += dy[k];\n\t\t\t\t\t\tif(cnt >= n) {\n\t\t\t\t\t\t\tnx = i, ny = j;\n\t\t\t\t\t\t\twhile(inside(nx, ny) and s[i][j] == s[nx][ny]) {\n\t\t\t\t\t\t\t\tdel.insert({nx, ny});\n\t\t\t\t\t\t\t\tnx += dx[k], ny += dy[k];\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(del.empty()) break;\n\t\t\tfor(auto [i, j] : del) {\n\t\t\t\ts[i][j] = '.';\n\t\t\t}\n\t\t}\n\t\tbool ok = 1;\n\t\tREP(i, h) {\n\t\t\tREP(j, w) { ok &= s[i][j] == '.'; }\n\t\t}\n\t\treturn ok;\n\t};\n\tbool res = 0;\n\tREP(i, h) {\n\t\tREP(j, w - 1) {\n\t\t\tauto c = s;\n\t\t\tswap(c[i][j], c[i][j + 1]);\n\t\t\tres |= simulate(c);\n\t\t}\n\t}\n\tYES(res);\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3472, "score_of_the_acc": -0.1887, "final_rank": 7 }, { "submission_id": "aoj_2232_5752072", "code_snippet": "#include <bits/stdc++.h>\n//#include <atcoder/all>\n//using namespace atcoder;\n#pragma GCC target (\"avx2\")\n#pragma GCC optimization (\"O3\")\n#pragma GCC optimization (\"unroll-loops\")\nusing namespace std;\n \ntypedef vector<int> VI;\ntypedef vector<VI> VVI;\ntypedef vector<string> VS;\ntypedef pair<int, int> PII;\ntypedef pair<int, int> pii;\ntypedef pair<long long, long long> PLL;\ntypedef pair<int, PII> TIII;\n \ntypedef long long ll;\ntypedef long double ld;\ntypedef unsigned long long ull;\n \n \n#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)\n#define REP(i, n) FOR(i, 0, n)\n#define rep(i, a, b) for (int i = a; i < (b); ++i)\n#define trav(a, x) for (auto &a : x)\n#define all(x) x.begin(), x.end()\n \n#define MOD 1000000007\n \ntemplate<class T1, class T2> inline bool chmax(T1 &a, T2 b) {if (a < b) {a = b; return true;} return false;}\ntemplate<class T1, class T2> inline bool chmin(T1 &a, T2 b) {if (a > b) {a = b; return true;} return false;}\nconst double EPS = 1e-12, PI = acos(-1);\nconst double pi = 3.141592653589793238462643383279;\n//ここから編集 \ntypedef string::const_iterator State;\nll GCD(ll a, ll b){\n return (b==0)?a:GCD(b, a%b);\n}\nll LCM(ll a, ll b){\n return a/GCD(a, b) * b;\n}\ntemplate< int mod >\nstruct ModInt {\n int x;\n \n ModInt() : x(0) {}\n \n ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}\n \n ModInt &operator+=(const ModInt &p) {\n if((x += p.x) >= mod) x -= mod;\n return *this;\n }\n \n ModInt &operator-=(const ModInt &p) {\n if((x += mod - p.x) >= mod) x -= mod;\n return *this;\n }\n \n ModInt &operator*=(const ModInt &p) {\n x = (int) (1LL * x * p.x % mod);\n return *this;\n }\n \n ModInt &operator/=(const ModInt &p) {\n *this *= p.inverse();\n return *this;\n }\n \n ModInt operator-() const { return ModInt(-x); }\n \n ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }\n \n ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }\n \n ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }\n \n ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }\n \n bool operator==(const ModInt &p) const { return x == p.x; }\n \n bool operator!=(const ModInt &p) const { return x != p.x; }\n \n ModInt inverse() const {\n int a = x, b = mod, u = 1, v = 0, t;\n while(b > 0) {\n t = a / b;\n swap(a -= t * b, b);\n swap(u -= t * v, v);\n }\n return ModInt(u);\n }\n \n ModInt pow(int64_t n) const {\n ModInt ret(1), mul(x);\n while(n > 0) {\n if(n & 1) ret *= mul;\n mul *= mul;\n n >>= 1;\n }\n return ret;\n }\n \n friend ostream &operator<<(ostream &os, const ModInt &p) {\n return os << p.x;\n }\n \n friend istream &operator>>(istream &is, ModInt &a) {\n int64_t t;\n is >> t;\n a = ModInt< mod >(t);\n return (is);\n }\n \n static int get_mod() { return mod; }\n};\n \nusing modint = ModInt< 1000000007 >;\ntemplate< typename T >\nstruct Combination {\n vector< T > _fact, _rfact, _inv;\n \n Combination(int sz) : _fact(sz + 1), _rfact(sz + 1), _inv(sz + 1) {\n _fact[0] = _rfact[sz] = _inv[0] = 1;\n for(int i = 1; i <= sz; i++) _fact[i] = _fact[i - 1] * i;\n _rfact[sz] /= _fact[sz];\n for(int i = sz - 1; i >= 0; i--) _rfact[i] = _rfact[i + 1] * (i + 1);\n for(int i = 1; i <= sz; i++) _inv[i] = _rfact[i] * _fact[i - 1];\n }\n \n inline T fact(int k) const { return _fact[k]; }\n \n inline T rfact(int k) const { return _rfact[k]; }\n \n inline T inv(int k) const { return _inv[k]; }\n \n T P(int n, int r) const {\n if(r < 0 || n < r) return 0;\n return fact(n) * rfact(n - r);\n }\n \n T C(int p, int q) const {\n if(q < 0 || p < q) return 0;\n return fact(p) * rfact(q) * rfact(p - q);\n }\n \n T H(int n, int r) const {\n if(n < 0 || r < 0) return (0);\n return r == 0 ? 1 : C(n + r - 1, r);\n }\n};\n \nll modpow(ll x, ll n, ll mod) {\n ll res = 1;\n while(n) {\n if(n&1) res = (res * x) % mod;\n x = (x * x) % mod;\n n >>= 1;\n }\n return res;\n} \ninline long long mod(long long a, long long m) {\n return (a % m + m) % m;\n}\ntemplate<typename T> \nstruct BIT{\n int N;\n std::vector<T> node;\n\n BIT(int n){\n N = n;\n node.resize(N+10);\n }\n\n /* a: 1-indexed */\n void add(int a, T x){\n for(int i=a; i<(int)node.size(); i += i & -i) node[i] += x;\n }\n\n /* [1, a] */\n T sum(int a){\n T res = 0;\n for(int x=a; x>0; x -= x & -x) res += node[x];\n return res;\n }\n\n /* [l, r] */\n T rangesum(int l, int r){\n return sum(r) - sum(l-1);\n }\n\n /* \n a1+a2+...+aw >= valとなるような最小のwを返す\n @verify https://codeforces.com/contest/992/problem/E\n */\n int lower_bound(T val) {\n if(val < 0) return 0;\n\n int res = 0;\n int n = 1; \n while (n < N) n *= 2;\n\n T tv=0;\n for (int k = n; k > 0; k /= 2) {\n if(res + k < N && node[res + k] < val){\n val -= node[res+k];\n res += k;\n }\n }\n return res+1; \n }\n};\nstruct UnionFind{\n std::vector<int> par;\n std::vector<int> siz;\n\n UnionFind(int sz_): par(sz_), siz(sz_) {\n for(int i=0; i<sz_; ++i) par[i] = i, siz[i] = 1;\n }\n\n void init(int sz_){\n par.resize(sz_);\n siz.resize(sz_);\n for(int i=0; i<sz_; ++i) par[i] = i, siz[i] = 1;\n }\n\n int root(int x){\n if(x == par[x]) return x;\n return par[x] = root(par[x]);\n }\n\n bool merge(int x, int y){\n x = root(x), y = root(y);\n if(x == y) return false;\n if(siz[x] < siz[y]) std::swap(x, y);\n siz[x] += siz[y];\n par[y] = x;\n return true;\n }\n\n bool issame(int x, int y){\n return root(x) == root(y);\n }\n\n int size(int x){\n return siz[root(x)];\n }\n};\n\nint h, w, n; \nbool check(vector<string> f) {\n bool update = true;\n \n for(int j=0; j<w; j++) {\n for(int i=h-1; i>=0; i--) {\n if(f[i][j] == '.' && i-1>=0 && f[i-1][j] != '.') swap(f[i][j], f[i-1][j]); \n }\n }\n while(update) {\n update = false;\n vector<vector<bool>>era(h, vector<bool>(w, false));\n for(int i=0; i<h; i++) {\n for(int j=0; j<w; j++) {\n\n if(f[i][j] == '.') continue;\n // 横方向\n \n char c = f[i][j];\n {\n int l = j;\n while(l-1>=0 && f[i][l-1] == c) l--;\n int r = j;\n while(r+1<w && f[i][r+1] == c) r++;\n if(r-l+1>=n) {\n for(int k=l; k<=r; k++) era[i][k] = true;\n update = true;\n }\n }\n //縦方向\n {\n int l = i;\n while(l-1>=0 && f[l-1][j] == c) l--;\n int r = i;\n while(r+1<h && f[r+1][j] == c) r++;\n if(r-l+1>=n) {\n for(int k=l; k<=r; k++) era[k][j] = true;\n update = true;\n }\n }\n }\n }\n \n vector<string> nf(h, string(w, '.'));\n for(int j=0; j<w; j++) {\n int ptr = h-1;\n for(int i=h-1; i>=0; i--) {\n if(era[i][j]) continue;\n if(f[i][j] == '.') continue;\n nf[ptr][j] = f[i][j];\n ptr--;\n }\n }\n f = nf;\n }\n\n REP(i,h) REP(j,w) if(f[i][j] != '.') return false;\n return true;\n}\nint main()\n{\n cin.tie(0);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(7);\n \n cin >> h >> w >> n;\n\n vector<string> f(h);\n REP(i,h) cin >> f[i];\n\n bool ok = false;\n REP(i,h) {\n REP(j,w) {\n\n {\n int ni = i, nj = j+1;\n if(ni>=0 && ni<h && nj>=0 && nj<w && f[ni][nj] != '.') {\n swap(f[i][j], f[ni][nj]);\n ok |= check(f);\n swap(f[i][j], f[ni][nj]);\n }\n }\n {\n int ni = i, nj = j-1;\n if(ni>=0 && ni<h && nj>=0 && nj<w && f[ni][nj] != '.') {\n swap(f[i][j], f[ni][nj]);\n ok |= check(f);\n swap(f[i][j], f[ni][nj]);\n }\n }\n }\n }\n cout << (ok?\"YES\":\"NO\") << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3508, "score_of_the_acc": -0.3955, "final_rank": 13 }, { "submission_id": "aoj_2232_5751785", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define REP(i,n) for(int i=0;i<(n);i++)\n#define ALL(v) v.begin(),v.end()\n\nint h,w,n;\n\nbool f(vector<string> s){\n REP(j,w)for(int i=h-2;i>=0;i--){\n int now=i;\n while(s[now][j]!='.'&&now+1<h&&s[now+1][j]=='.'){\n swap(s[now][j],s[now+1][j]);\n now++;\n }\n }\n while(true){\n bool c=false;\n vector<vector<bool>> tmp(h,vector<bool>(w,false));\n REP(i,h)REP(j,w-n+1){\n bool ok=true;\n REP(k,n)ok&=s[i][j]!='.'&&s[i][j]==s[i][j+k];\n if(ok)REP(k,n)tmp[i][j+k]=c=true;\n }\n REP(i,h-n+1)REP(j,w){\n bool ok=true;\n REP(k,n)ok&=s[i][j]!='.'&&s[i][j]==s[i+k][j];\n if(ok)REP(k,n)tmp[i+k][j]=c=true;\n }\n if(!c)break;\n REP(i,h)REP(j,w)if(tmp[i][j])s[i][j]='.';\n REP(j,w)for(int i=h-2;i>=0;i--){\n int now=i;\n while(s[now][j]!='.'&&now+1<h&&s[now+1][j]=='.'){\n swap(s[now][j],s[now+1][j]);\n now++;\n }\n }\n }\n REP(i,h)REP(j,w)if(s[i][j]!='.')return false;\n return true;\n}\n\nint main(){\n cin>>h>>w>>n;\n vector<string> s(h);\n string ans=\"NO\";\n REP(i,h)cin>>s[i];\n REP(i,h)REP(j,w-1){\n swap(s[i][j],s[i][j+1]);\n if(f(s))ans=\"YES\";\n swap(s[i][j],s[i][j+1]);\n }\n cout<<ans<<endl;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3468, "score_of_the_acc": -0.1698, "final_rank": 6 }, { "submission_id": "aoj_2232_5719685", "code_snippet": "#ifdef LOCAL\n#define _GLIBCXX_DEBUG\n#endif\n \n#include <bits/stdc++.h>\nusing namespace std;\n#if __has_include(<atcoder/all>)\n#include <atcoder/all>\nusing namespace atcoder;\n#endif\n \n#pragma region Macros\n// rep macro\n#define foa(v, a) for(auto &v : a)\n#define REPname(a, b, c, d, e, ...) e\n#define REP(...) REPname(__VA_ARGS__, REP3, REP2, REP1, REP0)(__VA_ARGS__)\n#define REP0(x) for(int i = 0; i < (x); ++i)\n#define REP1(i, x) for(int i = 0; i < (x); ++i)\n#define REP2(i, l, r) for(int i = (l); i < (r); ++i)\n#define REP3(i, l, r, c) for(int i = (l); i < (r); i += (c))\n#define REPSname(a, b, c, ...) c\n#define REPS(...) REPSname(__VA_ARGS__, REPS1, REPS0)(__VA_ARGS__)\n#define REPS0(x) for(int i = 1; i <= (x); ++i)\n#define REPS1(i, x) for(int i = 1; i <= (x); ++i)\n#define RREPname(a, b, c, d, e, ...) e\n#define RREP(...) RREPname(__VA_ARGS__, RREP3, RREP2, RREP1, RREP0)(__VA_ARGS__)\n#define RREP0(x) for(int i = (x)-1; i >= 0; --i)\n#define RREP1(i, x) for(int i = (x)-1; i >= 0; --i)\n#define RREP2(i, l, r) for(int i = (r)-1; i >= (l); --i)\n#define RREP3(i, l, r, c) for(int i = (r)-1; i >= (l); i -= (c))\n#define RREPSname(a, b, c, ...) c\n#define RREPS(...) RREPSname(__VA_ARGS__, RREPS1, RREPS0)(__VA_ARGS__)\n#define RREPS0(x) for(int i = (x); i >= 1; --i)\n#define RREPS1(i, x) for(int i = (x); i >= 1; --i)\n \n// name macro\n#define fi first\n#define se second\n#define pb push_back\n#define eb emplace_back\n#define SZ(x) ((int)(x).size())\n#define all(x) (x).begin(), (x).end()\n#define rall(x) (x).rbegin(), (x).rend()\n#define popcnt(x) __builtin_popcountll(x)\ntemplate <class T = int>\nusing V = std::vector<T>;\ntemplate <class T = int>\nusing VV = std::vector<std::vector<T>>;\ntemplate <class T = int>\nusing VVV = std::vector<std::vector<std::vector<T>>>;\ntemplate <class T>\nusing pqup = std::priority_queue<T, std::vector<T>, std::greater<T>>;\nusing ll = long long;\nusing ld = long double;\nusing int128 = __int128_t;\nusing pii = std::pair<int, int>;\nusing pll = std::pair<long long, long long>;\nusing Tp = tuple<ll,ll,ll>;\n \n// input macro\ntemplate <class T, class U>\nstd::istream &operator>>(std::istream &is, std::pair<T, U> &p) {\n\tis >> p.first >> p.second;\n\treturn is;\n}\ntemplate <class T>\nstd::istream &operator>>(std::istream &is, std::vector<T> &v) {\n\tfor(T &i : v) is >> i;\n\treturn is;\n}\nstd::istream &operator>>(std::istream &is, __int128_t &a) {\n\tstd::string s;\n\tis >> s;\n\t__int128_t ret = 0;\n\tfor(int i = 0; i < s.length(); i++)\n\t\tif('0' <= s[i] and s[i] <= '9')\n\t\t\tret = 10 * ret + s[i] - '0';\n\ta = ret * (s[0] == '-' ? -1 : 1);\n\treturn is;\n}\nnamespace scanner {\nvoid scan(int &a) { std::cin >> a; }\nvoid scan(long long &a) { std::cin >> a; }\nvoid scan(std::string &a) { std::cin >> a; }\nvoid scan(char &a) { std::cin >> a; }\nvoid scan(char a[]) { std::scanf(\"%s\", a); }\nvoid scan(double &a) { std::cin >> a; }\nvoid scan(long double &a) { std::cin >> a; }\ntemplate <class T, class U>\nvoid scan(std::pair<T, U> &p) { std::cin >> p; }\ntemplate <class T>\nvoid scan(std::vector<T> &a) { std::cin >> a; }\nvoid INPUT() {}\ntemplate <class Head, class... Tail>\nvoid INPUT(Head &head, Tail &... tail) {\n\tscan(head);\n\tINPUT(tail...);\n}\n} // namespace scanner\n#define VEC(type, name, size) \\\n\tstd::vector<type> name(size); \\\n\tscanner::INPUT(name)\n#define VVEC(type, name, h, w) \\\n\tstd::vector<std::vector<type>> name(h, std::vector<type>(w)); \\\n\tscanner::INPUT(name)\n#define INT(...) \\\n\tint __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define LL(...) \\\n\tlong long __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define STR(...) \\\n\tstd::string __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define CHAR(...) \\\n\tchar __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define DOUBLE(...) \\\n\tdouble __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define LD(...) \\\n\tlong double __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n \n// output-macro\ntemplate <class T, class U>\nstd::ostream &operator<<(std::ostream &os, const std::pair<T, U> &p) {\n\tos << p.first << \" \" << p.second;\n\treturn os;\n}\ntemplate <class T>\nstd::ostream &operator<<(std::ostream &os, const std::vector<T> &a) {\n\tfor(int i = 0; i < int(a.size()); ++i) {\n\t\tif(i) os << \" \";\n\t\tos << a[i];\n\t}\n\treturn os;\n}\nstd::ostream &operator<<(std::ostream &dest, __int128_t &value) {\n\tstd::ostream::sentry s(dest);\n\tif(s) {\n\t\t__uint128_t tmp = value < 0 ? -value : value;\n\t\tchar buffer[128];\n\t\tchar *d = std::end(buffer);\n\t\tdo {\n\t\t\t--d;\n\t\t\t*d = \"0123456789\"[tmp % 10];\n\t\t\ttmp /= 10;\n\t\t} while(tmp != 0);\n\t\tif(value < 0) {\n\t\t\t--d;\n\t\t\t*d = '-';\n\t\t}\n\t\tint len = std::end(buffer) - d;\n\t\tif(dest.rdbuf()->sputn(d, len) != len) {\n\t\t\tdest.setstate(std::ios_base::badbit);\n\t\t}\n\t}\n\treturn dest;\n}\ntemplate <class T>\nvoid print(const T a) { std::cout << a << '\\n'; }\ntemplate <class Head, class... Tail>\nvoid print(Head H, Tail... T) {\n\tstd::cout << H << ' ';\n\tprint(T...);\n}\ntemplate <class T>\nvoid printel(const T a) { std::cout << a << '\\n'; }\ntemplate <class T>\nvoid printel(const std::vector<T> &a) {\n\tfor(const auto &v : a)\n\t\tstd::cout << v << '\\n';\n}\ntemplate <class Head, class... Tail>\nvoid printel(Head H, Tail... T) {\n\tstd::cout << H << '\\n';\n\tprintel(T...);\n}\nvoid Yes(const bool b = true) { std::cout << (b ? \"Yes\\n\" : \"No\\n\"); }\nvoid No() { std::cout << \"No\\n\"; }\nvoid YES(const bool b = true) { std::cout << (b ? \"YES\\n\" : \"NO\\n\"); }\nvoid NO() { std::cout << \"No\\n\"; }\nvoid err(const bool b = true) {\n\tif(b) {\n\t\tstd::cout << \"-1\\n\", exit(0);\n\t}\n}\n \n//debug macro\nnamespace debugger {\ntemplate <class T>\nvoid view(const std::vector<T> &a) {\n\tstd::cerr << \"{ \";\n\tfor(const auto &v : a) {\n\t\tstd::cerr << v << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\ntemplate <class T>\nvoid view(const std::vector<std::vector<T>> &a) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &v : a) {\n\t\tstd::cerr << \"\\t\";\n\t\tview(v);\n\t\tstd::cerr << \"\\n\";\n\t}\n\tstd::cerr << \"}\";\n}\ntemplate <class T, class U>\nvoid view(const std::vector<std::pair<T, U>> &a) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &p : a) std::cerr << \"\\t(\" << p.first << \", \" << p.second << \")\\n\";\n\tstd::cerr << \"}\";\n}\ntemplate <class T, class U>\nvoid view(const std::map<T, U> &m) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &p : m) std::cerr << \"\\t[\" << p.first << \"] : \" << p.second << \"\\n\";\n\tstd::cerr << \"}\";\n}\ntemplate <class T, class U>\nvoid view(const std::pair<T, U> &p) { std::cerr << \"(\" << p.first << \", \" << p.second << \")\"; }\ntemplate <class T>\nvoid view(const std::set<T> &s) {\n\tstd::cerr << \"{ \";\n\tfor(auto &v : s) {\n\t\tview(v);\n\t\tstd::cerr << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\n \ntemplate <class T>\nvoid view(const T &e) { std::cerr << e; }\n} // namespace debugger\n#ifdef LOCAL\nvoid debug_out() {}\ntemplate <typename Head, typename... Tail>\nvoid debug_out(Head H, Tail... T) {\n\tdebugger::view(H);\n\tstd::cerr << \", \";\n\tdebug_out(T...);\n}\n#define debug(...) \\\n\tdo { \\\n\t\tstd::cerr << __LINE__ << \" [\" << #__VA_ARGS__ << \"] : [\"; \\\n\t\tdebug_out(__VA_ARGS__); \\\n\t\tstd::cerr << \"\\b\\b]\\n\"; \\\n\t} while(false)\n#else\n#define debug(...) (void(0))\n#endif\n \n// vector macro\ntemplate <class T>\nint lb(const std::vector<T> &a, const T x) { return std::distance((a).begin(), std::lower_bound((a).begin(), (a).end(), (x))); }\ntemplate <class T>\nint ub(const std::vector<T> &a, const T x) { return std::distance((a).begin(), std::upper_bound((a).begin(), (a).end(), (x))); }\ntemplate <class T>\nvoid UNIQUE(std::vector<T> &a) {\n\tstd::sort(a.begin(), a.end());\n\ta.erase(std::unique(a.begin(), a.end()), a.end());\n}\ntemplate <class T>\nstd::vector<T> press(std::vector<T> &a) {\n\tauto res = a;\n\tUNIQUE(res);\n\tfor(auto &v : a)\n\t\tv = lb(res, v);\n\treturn res;\n}\n#define SORTname(a, b, c, ...) c\n#define SORT(...) SORTname(__VA_ARGS__, SORT1, SORT0, ...)(__VA_ARGS__)\n#define SORT0(a) std::sort((a).begin(), (a).end())\n#define SORT1(a, c) std::sort((a).begin(), (a).end(), [](const auto x, const auto y) { return x c y; })\ntemplate <class T>\nvoid ADD(std::vector<T> &a, const T x) {\n\tfor(auto &v : a) v += x;\n}\ntemplate <class T>\nvoid SUB(std::vector<T> &a, const T x = 1) {\n\tfor(auto &v : a) v -= x;\n}\ntemplate <class T>\nvoid MUL(std::vector<T> &a, const T x) {\n\tfor(auto &v : a) v *= x;\n}\ntemplate <class T>\nvoid DIV(std::vector<T> &a, const T x) {\n\tfor(auto &v : a) v /= x;\n}\n \n// math macro\ntemplate <class T, class U>\ninline bool chmin(T &a, const U &b) { return a > b ? a = b, true : false; }\ntemplate <class T, class U>\ninline bool chmax(T &a, const U &b) { return a < b ? a = b, true : false; }\ntemplate <class T>\nT divup(T x, T y) { return (x + y - 1) / y; }\ntemplate <class T>\nT POW(T a, long long n) {\n\tT ret = 1;\n\twhile(n) {\n\t\tif(n & 1) ret *= a;\n\t\ta *= a;\n\t\tn >>= 1;\n\t}\n\treturn ret;\n}\n\ntemplate<typename T>\nT ADD(T a, T b){\n\tT res;\n\treturn __builtin_add_overflow(a, b, &res) ? numeric_limits<T>::max() : res;\n}\n\ntemplate<typename T>\nT MUL(T a, T b){\n\tT res;\n\treturn __builtin_mul_overflow(a, b, &res) ? numeric_limits<T>::max() : res;\n}\n\n// modpow\nlong long POW(long long a, long long n, const int mod) {\n\tlong long ret = 1;\n\twhile(n) {\n\t\tif(n & 1) (ret *= a) %= mod;\n\t\t(a *= a) %= mod;\n\t\tn >>= 1;\n\t}\n\treturn ret;\n}\n \n// others\nstruct fast_io {\n\tfast_io() {\n\t\tios::sync_with_stdio(false);\n\t\tcin.tie(nullptr);\n\t\tcout << fixed << setprecision(15);\n\t}\n} fast_io_;\n\n#pragma endregion\n\nusing R = long double;\nconstexpr R EPS=1E-11;\n\n// r の(誤差付きの)符号に従って, -1, 0, 1 を返す.\nint sgn(const R& r){ return (r > EPS) - (r < -EPS); }\n// a, b の(誤差付きの)大小比較の結果に従って, -1, 0, 1 を返す.\nint sgn(const R& a, const R &b){ return sgn(a-b); }\n// a > 0 は sgn(a) > 0\n// a < b は sgn(a, b) < 0\n// a >= b は sgn(a, b) >= 0\n// のように書く.\n// return s * 10^n\n\n//https://atcoder.jp/contests/abc191/submissions/20028529\nlong long x10(const string& s, size_t n) {\n if (s.front() == '-') return -x10(s.substr(1), n);\n auto pos = s.find('.');\n if (pos == string::npos) return stoll(s + string(n, '0'));\n return stoll(s.substr(0, pos) + s.substr(pos + 1) + string(n + pos + 1 - s.size(), '0'));\n}\n \nlong long ceildiv(long long a, long long b) {\n if (b < 0) a = -a, b = -b;\n if (a >= 0) return (a + b - 1) / b;\n else return a / b;\n}\n \nlong long floordiv(long long a, long long b) {\n if (b < 0) a = -a, b = -b;\n if (a >= 0) return a / b;\n else return (a - b + 1) / b;\n}\n \nlong long floorsqrt(long long x) {\n assert(x >= 0);\n long long ok = 0;\n long long ng = 1;\n while (ng * ng <= x) ng <<= 1;\n while (ng - ok > 1) {\n long long mid = (ng + ok) >> 1;\n if (mid * mid <= x) ok = mid;\n else ng = mid;\n }\n return ok;\n}\n\ninline int topbit(unsigned long long x){\n\treturn x?63-__builtin_clzll(x):-1;\n}\n\ninline int popcount(unsigned long long x){\n\treturn __builtin_popcountll(x);\n}\n\ninline int parity(unsigned long long x){//popcount%2\n\treturn __builtin_parity(x);\n}\n\nconst int inf = 1e9;\nconst ll INF = 1e18;\n\nvoid main_() {\n\tll h,w,n;\n\tcin >> h >> w >> n;\n\tVVEC(char,a,h,w);\n\tbool ok = false;\n\tREP(i,h){\n\t REP(j,w){\n\t \n\t {\n\t VV<char> b = a;\n\t VV<ll> d(h,V<ll>(w));\n\t if(j + 1 < w){\n\t swap(b[i][j],b[i][j+1]);\n\t bool ex = false;\n\t bool first = false;\n\t while(true){\n\t ex = false;\n\t bool del = false;\n\t REP(p,h){\n\t if(!first)break;\n\t char now = '0';\n\t ll len = 0;\n\t ll st = 0, en = 0;\n\t REP(q,w){\n\t if(now != b[p][q]){\n\t if(len >= n){\n\t for(ll k = st;k < en;k++){\n\t d[p][k] = 1;\n\t }\n\t del = true;\n\t }\n\t st = q;\n\t now = b[p][q];\n\t len = 1;\n\t if(now == '.'){\n\t now = '0';\n\t len = 0;\n\t }\n\t }\n\t else{\n\t len++;\n\t }\n\t en++;\n\t }\n\t if(len >= n){\n\t for(ll k = st;k < en;k++){\n\t d[p][k] = 1;\n\t del = true;\n\t }\n\t }\n\t }\n\t REP(p,w){\n\t if(!first)break;\n\t char now = '0';\n\t ll len = 0;\n\t ll st = 0, en = 0;\n\t REP(q,h){\n\t if(now != b[q][p]){\n\t if(len >= n){\n\t for(ll k = st;k < en;k++){\n\t d[k][p] = 1;\n\t }\n\t del = true;\n\t }\n\t st = q;\n\t now = b[q][p];\n\t len = 1;\n\t if(now == '.'){\n\t now = '0';\n\t len = 0;\n\t }\n\t }\n\t else{\n\t len++;\n\t }\n\t en++;\n\t }\n\t if(len >= n){\n\t for(ll k = st;k < en;k++){\n\t d[k][p] = 1;\n\t del = true;\n\t }\n\t }\n\t }\n\t REP(p,h)REP(q,w){\n\t if(d[p][q])b[p][q] = '.';\n\t d[p][q] = 0;\n\t }\n\t REP(p,w){\n\t for(ll q = h-1;q >= 0;q--){\n\t if(b[q][p] == '.'){\n\t for(ll k = q-1;k >= 0;k--){\n\t if(b[k][p] != '.'){\n\t swap(b[q][p],b[k][p]);\n\t break;\n\t }\n\t }\n\t }\n\t }\n\t }\n\t REP(p,h)REP(q,w)if(b[p][q] != '.')ex = true;\n\t \n\t if(!first)del =true;\n\t first = true;\n\t if(!del)break;\n\t }\n\t \n\t if(!ex)ok = true;\n\t }\n\t }\n\t \n\t }\n\t}\n\tif(ok)cout << \"YES\" << endl;\n\telse cout << \"NO\" << endl;\n}\n \nint main() {\n\tint t = 1;\n\t//cin >> t;\n\twhile(t--) main_();\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3492, "score_of_the_acc": -0.283, "final_rank": 9 }, { "submission_id": "aoj_2232_5693798", "code_snippet": "//たくさん誤読しちゃいました\n#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n int H, W, N;\n cin >> H >> W >> N;\n vector S(H, vector<char>(W));\n for (int i = H - 1; i >= 0; i--) {\n for (int j = 0; j < W; j++) {\n cin >> S[i][j];\n }\n }\n const int dx[2] = {0, 1}, dy[2] = {1, 0};\n auto maine = [&](vector<vector<char>> now) {\n vector nxt(H, vector(W, '.'));\n for (int y = 0; y < W; y++) {\n int cnt = 0;\n for (int x = 0; x < H; x++) {\n if (now[x][y] != '.')\n nxt[cnt++][y] = now[x][y];\n }\n }\n swap(nxt, now);\n while (true) {\n vector book(H, vector(W, false));\n for (int x = 0; x < H; x++) {\n for (int y = 0; y < W; y++) {\n auto c = now[x][y];\n if (c == '.')\n continue;\n for (int i = 0; i < 2; i++) {\n if (x + dx[i] * N <= H && y + dy[i] * N <= W) {\n bool flag = true;\n for (int j = 0; j < N; j++) {\n if (c != now[x + dx[i] * j][y + dy[i] * j])\n flag = false;\n }\n if (flag) {\n for (int j = 0; j < N; j++) {\n book[x + dx[i] * j][y + dy[i] * j] = true;\n }\n }\n }\n }\n }\n }\n vector nxt(H, vector(W, '.'));\n for (int y = 0; y < W; y++) {\n int cnt = 0;\n for (int x = 0; x < H; x++) {\n if (now[x][y] != '.' && !book[x][y])\n nxt[cnt++][y] = now[x][y];\n }\n }\n\n if (now == nxt)\n break;\n swap(nxt, now);\n }\n for (auto&& V : now) {\n for (auto&& c : V) {\n if (c != '.')\n return false;\n }\n }\n return true;\n };\n\n for (int x = 0; x < H; x++) {\n for (int y = 0; y < W; y++) {\n int xx = x, yy = y + 1;\n if (xx < H && yy < W) {\n swap(S[x][y], S[xx][yy]);\n if (maine(S)) {\n cout << \"YES\" << endl;\n return 0;\n }\n swap(S[x][y], S[xx][yy]);\n }\n }\n }\n cout << \"NO\" << endl;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3464, "score_of_the_acc": -0.1509, "final_rank": 5 } ]
aoj_2234_cpp
Problem C: Usagitobi m × n マスの盤がある. i 行 j 列のマスを( i , j ) (0 ≤ i < m , 0 ≤ j < n ) で表す. うさぎは( x , y ) にいるとき, (( x + a ) mod m , ( y + b ) mod n ) または(( x + c ) mod m , ( y + d ) mod n ) へ跳ぶことができる. いま, うさぎが(0, 0) にいる. 一度跳び立ったマスへ再び行くことはできないとするとき, うさぎは最大何回跳ぶことができるか. Input 入力は一行に m , n , a , b , c , d がスペース区切りで与えられる. 1 ≤ m , n , a , b , c , d ≤ 100 000 Output うさぎが跳べる最大回数を一行に出力せよ. Sample Input 1 6 6 2 2 2 4 Sample Output 1 8
[ { "submission_id": "aoj_2234_7242501", "code_snippet": "#include <iostream>\n#include <map>\n#include <vector>\nusing namespace std;\n\nlong long GCD(long long a, long long b) {\n\tif (b == 0) return a;\n\treturn GCD(b, a % b);\n}\n\nlong long GetAnswer(long long H, long long W, long long A, long long B, long long C, long long D) {\n\tmap<pair<long long, long long>, int> Map;\n\t\n\t// Step 1\n\tvector<pair<long long, long long>> Trajectory;\n\tlong long cx = 0, cy = 0, STEP1 = 0;\n\twhile (true) {\n\t\tTrajectory.push_back(make_pair(cx, cy));\n\t\tcx = (cx + A) % H;\n\t\tcy = (cy + B) % W;\n\t\tSTEP1 += 1;\n\t\tif (cx == 0) break;\n\t}\n\tlong long BASE = GCD(cy, W);\n\tSTEP1 *= (W / BASE);\n\tfor (int i = 0; i < (int)Trajectory.size(); i++) {\n\t\tlong long vx = Trajectory[i].first;\n\t\tlong long vy = Trajectory[i].second;\n\t\tMap[make_pair(vx, vy % BASE)] = 1;\n\t}\n\t\n\t// Step 2\n\tlong long dx = 0, dy = 0, STEP2 = 0;\n\twhile (true) {\n\t\tdx = (dx + C) % H;\n\t\tdy = (dy + D) % W;\n\t\tSTEP2 += 1;\n\t\tif (Map[make_pair(dx, dy % BASE)] == 1) {\n\t\t\treturn STEP1 * STEP2;\n\t\t}\n\t\tif (STEP2 >= 250000LL) return (1LL << 60);\n\t}\n}\n\nint main() {\n\t// Input\n\tlong long H, W, A, B, C, D;\n\tcin >> H >> W >> A >> B >> C >> D;\n\t\n\t// Get Answer\n\tlong long Answer1 = GetAnswer(H, W, A, B, C, D);\n\tlong long Answer2 = GetAnswer(H, W, C, D, A, B);\n\tcout << min(Answer1, Answer2) - 1LL << endl;\n\treturn 0;\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 19060, "score_of_the_acc": -1.0641, "final_rank": 9 }, { "submission_id": "aoj_2234_1147976", "code_snippet": "#include<stdio.h>\n#include<algorithm>\n#include<set>\nusing namespace std;\nint gcd(int a,int b){\n\twhile(a){\n\t\tb%=a;\n\t\tint c=a;a=b;b=c;\n\t}\n\treturn b;\n}\nset<pair<int,int> > S;\nset<pair<int,int> > T;\nint main(){\n\tint a,b,c,d,e,f;\n\tscanf(\"%d%d%d%d%d%d\",&a,&b,&c,&d,&e,&f);\n\tint row=0;\n\tint col=0;\n\tlong long cnt=1;\n\tS.insert(make_pair(row,col));\n\twhile(1){\n\t\trow=(row+c)%a;\n\t\tcol=(col+d)%b;\n\t\tif(col==0)break;\n\t\tcnt++;\n\t\tS.insert(make_pair(row,col));\n\t}\n\tint g=gcd(row,a);\n\tlong long ret=a/g*cnt;\n\tif(ret<110000){\n\t\tS.clear();\n\t\tswap(c,e);\n\t\tswap(d,f);\n\t\trow=0;col=0;cnt=1;\n\t\tS.insert(make_pair(row,col));\n\t\twhile(1){\n\t\t\trow=(row+c)%a;\n\t\t\tcol=(col+d)%b;\n\t\t\tif(col==0)break;\n\t\t\tcnt++;\n\t\t\tS.insert(make_pair(row,col));\n\t\t}\n\t\tg=gcd(row,a);\n\t\tret=a/g*cnt;\n\t}\n\tfor(set<pair<int,int> > ::iterator it=S.begin();it!=S.end();it++){\n\t\tpair<int,int>tmp=(*it);\n\t\tT.insert(make_pair(tmp.first%g,tmp.second));\n\t}\n\t//printf(\"%lld\\n\",ret);\n\trow=0;col=0;cnt=1;\n\t while(1){\n row=(row+e)%a;\n col=(col+f)%b;\n if(T.count(make_pair(row%g,col)))break;\n cnt++;\n }\n\t//printf(\"%lld %lld\\n\",ret,cnt);\n\tprintf(\"%lld\\n\",ret*cnt-1);\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 10400, "score_of_the_acc": -0.5456, "final_rank": 7 }, { "submission_id": "aoj_2234_537717", "code_snippet": "#include <stdio.h>\n#include <string.h>\n#include <algorithm>\n#include <iostream>\n#include <math.h>\n#include <assert.h>\n#include <vector>\n#include <queue>\n#include <string>\n#include <map>\n#include <set>\n\nusing namespace std;\ntypedef long long ll;\ntypedef unsigned int uint;\ntypedef unsigned long long ull;\nstatic const double EPS = 1e-9;\nstatic const double PI = acos(-1.0);\n\n#define REP(i, n) for (int i = 0; i < (int)(n); i++)\n#define FOR(i, s, n) for (int i = (s); i < (int)(n); i++)\n#define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++)\n#define FORIT(it, c) for (__typeof((c).begin())it = (c).begin(); it != (c).end(); it++)\n#define MEMSET(v, h) memset((v), h, sizeof(v))\n\nll gcd(ll a, ll b) {\n if (b == 0) { return a; }\n return gcd(b, a % b);\n}\nll lcm(ll a, ll b) {\n return a / gcd(a, b) * b;\n}\n\n//a x + b y = gcd(a, b)\nlong long extgcd(long long a, long long b, long long &x, long long &y) {\n long long g = a; x = 1; y = 0;\n if (b != 0) g = extgcd(b, a % b, y, x), y -= (a / b) * x;\n return g;\n}\n\n\nll Mul(ll a, ll b, ll mod) {\n a %= mod;\n b %= mod;\n if (mod < 2e+9) { return a * b % mod; }\n ll ret = 0;\n while (b > 0) {\n if (b & 1) { ret = (ret + a) % mod; }\n a = (a + a) % mod;\n b >>= 1;\n }\n return ret;\n}\n\n// need extgcd\npair<ll, ll> PairChineseRemainderTherom(ll ans1, ll mod1, ll ans2, ll mod2) {\n ll g = gcd(mod1, mod2);\n ans1 %= mod1; ans1 = (ans1 + mod1) % mod1;\n ans2 %= mod2; ans2 = (ans2 + mod2) % mod2;\n if (ans1 % g != ans2 % g) { return make_pair(-1, -1); }\n const ll anss[2] = { ans1 / g, ans2 / g };\n const ll mods[2] = { mod1 / g, mod2 / g };\n ll all = mods[0] * mods[1];\n assert(all * g / g / mods[0] == mods[1]);\n ll ret = 0;\n for (int i = 0; i < 2; i++) {\n ll x, y;\n extgcd(mods[i], all / mods[i], x, y);\n y = (y + all) % all;\n ll v = Mul(y, anss[i], all);\n v = Mul(v, all / mods[i], all);\n ret = (ret + v) % all;\n assert(ret >= 0);\n }\n ret = ret * g + ans1 % g;\n return make_pair(ret, all * g);\n}\n\n// solve A x == B mod M\nll Divide(ll A, ll B, ll M) {\n A %= M; A = (A + M) % M;\n B %= M; B = (B + M) % M;\n ll g = gcd(A, M);\n if (B % g != 0) { return -1; }\n ll x, y;\n extgcd(A, M, x, y);\n ll n = x * B / g;\n ll dn = M / g;\n n -= n / dn * dn;\n if (n < 0) { n += dn; }\n return n;\n}\n\n\npair<ll, ll> PairLinearCongruence(ll A1, ll B1, ll M1, ll A2, ll B2, ll M2) {\n A1 %= M1; A1 = (A1 + M1) % M1;\n B1 %= M1; B1 = (B1 + M1) % M1;\n A2 %= M2; A2 = (A2 + M2) % M2;\n B2 %= M2; B2 = (B2 + M2) % M2;\n if (A1 == 0 && B1 != 0) { return make_pair(-1, -1); }\n if (A2 == 0 && B2 != 0) { return make_pair(-1, -1); }\n if (A1 == 0 && A2 == 0) { return make_pair(0, 1); }\n if (A1 == 0) {\n swap(A1, A2);\n swap(B1, B2);\n swap(M1, M2);\n }\n ll g1 = gcd(A1, gcd(B1, M1));\n A1 /= g1; B1 /= g1; M1 /= g1;\n ll v1 = Divide(A1, B1, M1);\n if (v1 == -1) { return make_pair(-1, -1); }\n if (A2 == 0) {\n if (v1 == 0) { v1 = M1; }\n return make_pair(v1, M1);\n }\n ll g2 = gcd(A2, gcd(B2, M2));\n A2 /= g2; B2 /= g2; M2 /= g2;\n ll v2 = Divide(A2, B2, M2);\n if (v2 == -1) { return make_pair(-1, -1); }\n pair<ll, ll> ret = PairChineseRemainderTherom(v1, M1, v2, M2);\n if (ret.first == 0) { ret.first = ret.second; }\n return ret;\n}\n\n\nll n, m, a, b, c, d;\nint main() {\n while (scanf(\"%lld %lld %lld %lld %lld %lld\", &m, &n, &a, &b, &c, &d) > 0) {\n a %= m; c %= m;\n b %= n; d %= n;\n ll e = 0;\n if (a == 0 && b == 0) {\n swap(a, c);\n swap(b, d);\n }\n if (a == 0 && b == 0) {\n puts(\"0\");\n continue;\n } else if (a == 0) {\n e = n / gcd(b, n);\n } else if (b == 0) {\n e = m / gcd(a, m);\n } else {\n e = lcm(m / gcd(a, m), n / gcd(b, n));\n }\n if (c == 0 && d == 0) {\n printf(\"%lld\\n\", e - 1);\n continue;\n }\n\n ll sqnm = sqrt(n * m) + EPS;\n ll f = 1LL << 60;\n if (e <= sqrt(n * m)) {\n REP(g, e) {\n ll v = PairLinearCongruence(c, g * a % m, m, d, g * b % n, n).first;\n if (v == -1) { continue; }\n assert(v > 0);\n f = min(f, v);\n }\n } else {\n f = 1;\n FOREQ(i, 1, sqnm + 5) {\n ll v = PairLinearCongruence(a, i * c % m, m, b, i * d % n, n).first;\n if (v == -1) { continue; }\n f = i;\n break;\n }\n }\n printf(\"%lld\\n\", e * f - 1);\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 1196, "score_of_the_acc": -0.0627, "final_rank": 3 }, { "submission_id": "aoj_2234_338585", "code_snippet": "#include <set>\n#include <cstdio>\n#include <numeric>\n#include <algorithm>\n#include <queue>\n\ninline int getInt(){ int s; scanf(\"%d\", &s); return s; }\n\ntypedef long long ll;\n\nusing namespace std;\n\nint check(int a, int m){\n vector<int> visit(m);\n int now = 0;\n\n while(!visit[now]){\n visit[now] = 1;\n now = (now + a) % m;\n }\n\n return accumulate(visit.begin(), visit.end(), 0);\n}\n\nint main(){\n int m = getInt();\n int n = getInt();\n int a = getInt();\n int b = getInt();\n int c = getInt();\n int d = getInt();\n\n a %= m;\n b %= n;\n c %= m;\n d %= n;\n\n int acnt = check(a, m);\n int bcnt = check(b, n);\n\n ll ans1 = ((ll)acnt * bcnt) / __gcd(acnt, bcnt);\n ll ans2 = 0;\n\n vector<int> h(m, -1);\n int hh = 0;\n int xx = 0;\n int yy = 0;\n\n while(h[xx] == -1){\n h[xx] = yy;\n xx = (xx + a) % m;\n yy = (yy + b) % n;\n hh++;\n }\n\n int ym = ans1 / hh;\n int yd = n / ym;\n\n xx = c;\n yy = d;\n\n while(true){\n ans2++;\n\n if(ans2 == 30 * n){\n int ccnt = check(c, m);\n int dcnt = check(d, n);\n ans2 = ((ll)ccnt * dcnt) / __gcd(ccnt, dcnt);\n break;\n }\n\n if(h[xx] != -1){\n if((yy - h[xx] + n) % yd == 0)\n break;\n }\n\n xx = (xx + c) % m;\n yy = (yy + d) % n;\n }\n\n printf(\"%lld\\n\", ans1 * ans2 - 1);\n\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 0, "score_of_the_acc": 0, "final_rank": 1 }, { "submission_id": "aoj_2234_338584", "code_snippet": "#include <set>\n#include <cstdio>\n#include <numeric>\n#include <algorithm>\n#include <queue>\n\ninline int getInt(){ int s; scanf(\"%d\", &s); return s; }\n\ntypedef long long ll;\n\nusing namespace std;\n\nint check(int a, int m){\n vector<int> visit(m);\n int now = 0;\n\n while(!visit[now]){\n visit[now] = 1;\n now = (now + a) % m;\n }\n\n return accumulate(visit.begin(), visit.end(), 0);\n}\n\nint main(){\n int m = getInt();\n int n = getInt();\n int a = getInt();\n int b = getInt();\n int c = getInt();\n int d = getInt();\n\n a %= m;\n b %= n;\n c %= m;\n d %= n;\n\n int acnt = check(a, m);\n int bcnt = check(b, n);\n\n ll ans1 = ((ll)acnt * bcnt) / __gcd(acnt, bcnt);\n ll ans2 = 0;\n\n vector<int> h(m, -1);\n int hh = 0;\n int xx = 0;\n int yy = 0;\n\n while(h[xx] == -1){\n h[xx] = yy;\n xx = (xx + a) % m;\n yy = (yy + b) % n;\n hh++;\n }\n\n int ym = ans1 / hh;\n int yd = n / ym;\n\n xx = c;\n yy = d;\n\n while(true){\n ans2++;\n\n if(ans2 == 300 * n){\n int ccnt = check(c, m);\n int dcnt = check(d, n);\n ans2 = ((ll)ccnt * dcnt) / __gcd(ccnt, dcnt);\n break;\n }\n\n if(h[xx] != -1){\n if((yy - h[xx] + n) % yd == 0)\n break;\n }\n\n xx = (xx + c) % m;\n yy = (yy + d) % n;\n }\n\n printf(\"%lld\\n\", ans1 * ans2 - 1);\n\n return 0;\n}", "accuracy": 1, "time_ms": 240, "memory_kb": 1144, "score_of_the_acc": -0.3421, "final_rank": 6 }, { "submission_id": "aoj_2234_338582", "code_snippet": "#include <set>\n#include <cstdio>\n#include <numeric>\n#include <algorithm>\n#include <queue>\n\ninline int getInt(){ int s; scanf(\"%d\", &s); return s; }\n\ntypedef long long ll;\n\nusing namespace std;\n\nint check(int a, int m){\n vector<int> visit(m);\n int now = 0;\n\n while(!visit[now]){\n visit[now] = 1;\n now = (now + a) % m;\n }\n\n return accumulate(visit.begin(), visit.end(), 0);\n}\n\nint main(){\n int m = getInt();\n int n = getInt();\n int a = getInt();\n int b = getInt();\n int c = getInt();\n int d = getInt();\n\n a %= m;\n b %= n;\n c %= m;\n d %= n;\n\n int acnt = check(a, m);\n int bcnt = check(b, n);\n\n ll ans1 = ((ll)acnt * bcnt) / __gcd(acnt, bcnt);\n ll ans2 = 0;\n\n vector<int> h(m, -1);\n int hh = 0;\n int xx = 0;\n int yy = 0;\n\n while(h[xx] == -1){\n h[xx] = yy;\n xx = (xx + a) % m;\n yy = (yy + b) % n;\n hh++;\n }\n\n int ym = ans1 / hh;\n int yd = n / ym;\n\n xx = c;\n yy = d;\n\n while(true){\n ans2++;\n\n if(ans2 == 350 * n){\n int ccnt = check(c, m);\n int dcnt = check(d, n);\n ans2 = ((ll)ccnt * dcnt) / __gcd(ccnt, dcnt);\n break;\n }\n\n if(h[xx] != -1){\n if((yy - h[xx] + n) % yd == 0)\n break;\n }\n\n xx = (xx + c) % m;\n yy = (yy + d) % n;\n }\n\n printf(\"%lld\\n\", ans1 * ans2 - 1);\n\n return 0;\n}", "accuracy": 1, "time_ms": 280, "memory_kb": 0, "score_of_the_acc": -0.3333, "final_rank": 5 }, { "submission_id": "aoj_2234_338581", "code_snippet": "#include <set>\n#include <cstdio>\n#include <numeric>\n#include <algorithm>\n#include <queue>\n\ninline int getInt(){ int s; scanf(\"%d\", &s); return s; }\n\ntypedef long long ll;\n\nusing namespace std;\n\nint check(int a, int m){\n vector<int> visit(m);\n int now = 0;\n\n while(!visit[now]){\n visit[now] = 1;\n now = (now + a) % m;\n }\n\n return accumulate(visit.begin(), visit.end(), 0);\n}\n\nint main(){\n int m = getInt();\n int n = getInt();\n int a = getInt();\n int b = getInt();\n int c = getInt();\n int d = getInt();\n\n a %= m;\n b %= n;\n c %= m;\n d %= n;\n\n int acnt = check(a, m);\n int bcnt = check(b, n);\n\n ll ans1 = ((ll)acnt * bcnt) / __gcd(acnt, bcnt);\n ll ans2 = 0;\n\n vector<int> h(m, -1);\n int hh = 0;\n int xx = 0;\n int yy = 0;\n\n while(h[xx] == -1){\n h[xx] = yy;\n xx = (xx + a) % m;\n yy = (yy + b) % n;\n hh++;\n }\n\n int ym = ans1 / hh;\n int yd = n / ym;\n\n xx = c;\n yy = d;\n\n while(true){\n ans2++;\n\n if(ans2 == 1000 * n){\n int ccnt = check(c, m);\n int dcnt = check(d, n);\n ans2 = ((ll)ccnt * dcnt) / __gcd(ccnt, dcnt);\n break;\n }\n\n if(h[xx] != -1){\n if((yy - h[xx] + n) % yd == 0)\n break;\n }\n\n xx = (xx + c) % m;\n yy = (yy + d) % n;\n }\n\n printf(\"%lld\\n\", ans1 * ans2 - 1);\n\n return 0;\n}", "accuracy": 1, "time_ms": 800, "memory_kb": 0, "score_of_the_acc": -1, "final_rank": 8 }, { "submission_id": "aoj_2234_335285", "code_snippet": "#include <stdio.h>\n#include <string.h>\n#include <algorithm>\n#include <iostream>\n#include <math.h>\n#include <assert.h>\n#include <vector>\n#include <queue>\n#include <string>\n#include <map>\n#include <set>\n\nusing namespace std;\ntypedef long long ll;\ntypedef unsigned int uint;\ntypedef unsigned long long ull;\nstatic const double EPS = 1e-9;\nstatic const double PI = acos(-1.0);\n\n#define REP(i, n) for (int i = 0; i < (int)(n); i++)\n#define FOR(i, s, n) for (int i = (s); i < (int)(n); i++)\n#define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++)\n#define FORIT(it, c) for (__typeof((c).begin())it = (c).begin(); it != (c).end(); it++)\n#define MEMSET(v, h) memset((v), h, sizeof(v))\n\nll gcd(ll a, ll b) {\n if (b == 0) { return a; }\n return gcd(b, a % b);\n}\nll lcm(ll a, ll b) {\n return a / gcd(a, b) * b;\n}\n\n//a x + b y = gcd(a, b)\nlong long extgcd(long long a, long long b, long long &x, long long &y) {\n long long g = a; x = 1; y = 0;\n if (b != 0) g = extgcd(b, a % b, y, x), y -= (a / b) * x;\n return g;\n}\n\nlong long InvMod(long long a, long long mod) {\n long long x, y;\n if (extgcd(a, mod, x, y) == 1) { return (x + mod) % mod; }\n return 0;\n}\n\npair<ll, ll> LinearCongruence(const vector<ll> &A, const vector<ll> &B, const vector<ll> &M) {\n ll x = 0;\n ll m = 1;\n for (ll i = 0; i < (ll)A.size(); i++) {\n ll a = A[i] * m;\n ll b = B[i] - A[i] * x;\n ll d = gcd(M[i], a);\n if (b % d != 0) { return make_pair(0, -1); }\n ll t = b / d * InvMod(a / d, M[i] / d) % (M[i] / d);\n x = x + m * t;\n m *= M[i] / d;\n }\n x %= m;\n if (x < 0) { x += m; }\n return make_pair(x % m, m);\n}\n\nll solve(ll l1, ll l2, ll r1, ll r2, ll m1, ll m2) {\n vector<ll> a, b, m;\n if (l1 == 0 && r1 != 0) { return -1; }\n if (l2 == 0 && r2 != 0) { return -1; }\n\n if (l1 != 0) {\n a.push_back(l1);\n b.push_back(r1);\n m.push_back(m1);\n }\n if (l2 != 0) {\n a.push_back(l2);\n b.push_back(r2);\n m.push_back(m2);\n }\n if (a.size() == 0) { return 1; }\n pair<ll, ll> nret = LinearCongruence(a, b, m);\n if (nret.second > 0) {\n if (nret.first == 0) { nret.first += nret.second; }\n return nret.first;\n }\n return -1;\n}\n\nll n, m, a, b, c, d;\nint main() {\n while (scanf(\"%lld %lld %lld %lld %lld %lld\", &m, &n, &a, &b, &c, &d) > 0) {\n a %= m; c %= m;\n b %= n; d %= n;\n ll e = 0;\n if (a == 0 && b == 0) {\n swap(a, c);\n swap(b, d);\n }\n if (a == 0 && b == 0) {\n puts(\"0\");\n continue;\n } else if (a == 0) {\n e = n / gcd(b, n);\n } else if (b == 0) {\n e = m / gcd(a, m);\n } else {\n e = lcm(m / gcd(a, m), n / gcd(b, n));\n }\n if (c == 0 && d == 0) {\n printf(\"%lld\\n\", e - 1);\n continue;\n }\n \n ll sqnm = sqrt(n * m) + EPS;\n ll f = 1LL << 60;\n if (e <= sqrt(n * m)) {\n REP(g, e) {\n ll v = solve(c, d, g * a % m, g * b % n, m, n);\n if (v == -1) { continue; }\n assert(v > 0);\n f = min(f, v);\n }\n } else {\n f = 1;\n FOREQ(i, 1, sqnm + 5) {\n ll v = solve(a, b, i * c % m, i * d % n, m, n);\n if (v == -1) { continue; }\n f = i;\n break;\n }\n }\n printf(\"%lld\\n\", e * f - 1);\n }\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 0, "score_of_the_acc": -0.0641, "final_rank": 4 }, { "submission_id": "aoj_2234_326980", "code_snippet": "#include <stdio.h>\n#include <string.h>\n#include <algorithm>\n#include <iostream>\n#include <math.h>\n#include <assert.h>\n#include <vector>\n#include <queue>\n#include <string>\n#include <map>\n#include <set>\n\nusing namespace std;\ntypedef long long ll;\ntypedef unsigned int uint;\ntypedef unsigned long long ull;\nstatic const double EPS = 1e-9;\nstatic const double PI = acos(-1.0);\n\n#define REP(i, n) for (int i = 0; i < (int)(n); i++)\n#define FOR(i, s, n) for (int i = (s); i < (int)(n); i++)\n#define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++)\n#define FORIT(it, c) for (__typeof((c).begin())it = (c).begin(); it != (c).end(); it++)\n#define MEMSET(v, h) memset((v), h, sizeof(v))\n\nll gcd(ll a, ll b) {\n if (b == 0) { return a; }\n return gcd(b, a % b);\n}\nll lcm(ll a, ll b) {\n return a / gcd(a, b) * b;\n}\n\n//a x + b y = gcd(a, b)\nlong long extgcd(long long a, long long b, long long &x, long long &y) {\n long long g = a; x = 1; y = 0;\n if (b != 0) g = extgcd(b, a % b, y, x), y -= (a / b) * x;\n return g;\n}\n\nll divide(ll a, ll b, ll m) {\n ll g = gcd(a, m);\n if (b%g != 0) { return -1; }\n ll x, y;\n extgcd(a, m, x, y);\n assert(a*x+m*y == g);\n ll n = x*b/g;\n ll dn = m/g;\n n -= n/dn*dn;\n if (n < 0)\n n += dn;\n return n;\n}\n\nll calc(const vector<ll> &anss, const vector<ll> &mods) {\n if (anss.size() == 1) { return anss[0]; }\n ll g = gcd(mods[0], mods[1]);\n if (anss[0] % g != anss[1] % g) { return -1; }\n ll t = divide(mods[0], (anss[1] - anss[0] + mods[0] * mods[1]) % mods[1], mods[1]);\n assert(t != -1);\n ll d = lcm(mods[0], mods[1]);\n ll x = (anss[0] + t * mods[0]) % d;\n assert(anss[0] == x % mods[0]);\n assert(anss[1] == x % mods[1]);\n if (x == 0) { x += d; }\n return x;\n}\n\nll solve(ll l1, ll l2, ll r1, ll r2, ll m1, ll m2) {\n vector<ll> a, m;\n if (l1 == 0 && r1 != 0) { return -1; }\n if (l2 == 0 && r2 != 0) { return -1; }\n\n if (l1 != 0) {\n ll g = gcd(r1, gcd(l1, m1));\n l1 /= g; r1 /= g; m1 /= g;\n ll v = divide(l1, r1, m1);\n if (v == -1) { return -1; }\n a.push_back(v);\n m.push_back(m1);\n }\n if (l2 != 0) {\n ll g = gcd(r2, gcd(l2, m2));\n l2 /= g; r2 /= g; m2 /= g;\n ll v = divide(l2, r2, m2);\n if (v == -1) { return -1; }\n a.push_back(v);\n m.push_back(m2);\n }\n if (a.size() == 0) { return 1; }\n ll x = calc(a, m);\n if (x > 0) { return x; }\n return -1;\n}\n\nll n, m, a, b, c, d;\nint main() {\n while (scanf(\"%lld %lld %lld %lld %lld %lld\", &m, &n, &a, &b, &c, &d) > 0) {\n a %= m; c %= m;\n b %= n; d %= n;\n ll e = 0;\n if (a == 0 && b == 0) {\n swap(a, c);\n swap(b, d);\n }\n if (a == 0 && b == 0) {\n puts(\"0\");\n continue;\n } else if (a == 0) {\n e = n / gcd(b, n);\n } else if (b == 0) {\n e = m / gcd(a, m);\n } else {\n e = lcm(m / gcd(a, m), n / gcd(b, n));\n }\n if (c == 0 && d == 0) {\n printf(\"%lld\\n\", e - 1);\n continue;\n }\n\n ll sqnm = sqrt(n * m) + EPS;\n ll f = 1LL << 60;\n if (e <= sqrt(n * m)) {\n REP(g, e) {\n ll v = solve(c, d, g * a % m, g * b % n, m, n);\n if (v == -1) { continue; }\n assert(v > 0);\n f = min(f, v);\n }\n } else {\n f = 1;\n FOREQ(i, 1, sqnm + 5) {\n ll v = solve(a, b, i * c % m, i * d % n, m, n);\n if (v == -1) { continue; }\n f = i;\n break;\n }\n }\n printf(\"%lld\\n\", e * f - 1);\n }\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 892, "score_of_the_acc": -0.0596, "final_rank": 2 } ]
aoj_2233_cpp
Problem B: Carrot Tour うさぎがある国を旅行している. この国には1 から n の番号がついた n 個の都市があり, うさぎは今都市1にいる. 都市 i は座標平面上の1 点( x i , y i ) とみなす. うさぎは以下の条件をみたすように旅をする. 移動経路は折れ線であり, その各部分は異なる2 都市を結ぶ線分でなければならない. 移動経路の全長は r 以下でなければならない. 経路のうち重なった部分も, 通った回数分数える. 移動する方向が変わるとき, 曲がる角度は θ 以下でなければならない. 最初の移動方向に制限はない. うさぎがある都市から別の都市へ移動をすると, 移動先の都市でニンジンを1 本もらえる. 同じ都市を複数回訪れることは可能であり, 訪れるたびにニンジンをもらえる. うさぎがこの旅で手に入れることのできるニンジンの本数の最大値を求めよ. Input 入力の一行目には一つの整数 n が, 二行目には二つの実数 r , θ がスペースで区切られて与えられる. 1 ≤ n ≤ 20 0 < r < 10 4 0° < θ < 180° 続く n 行には, 整数 x i , y i がスペースで区切られて与えられる -10 000 ≤ x i , y i ≤ 10 000 r , θ を±10 −3 以内で変化させても答えは変わらない. どの2 つの都市の位置も異なる. Output うさぎがこの旅で手に入れることのできるニンジンの本数の最大値を一行に出力せよ. Sample Input 1 5 100.1 90.1 0 0 0 10 5 5 10 0 10 10 Sample Output 1 10
[ { "submission_id": "aoj_2233_10880835", "code_snippet": "#define _USE_MATH_DEFINES\n#include<bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\nint x[22], y[22];\ndouble r, theta;\n\nbool check_angle(int p, int i, int n) {\n\tdouble a1 = x[i] - x[p], a2 = y[i] - y[p];\n\tdouble b1 = x[n] - x[i], b2 = y[n] - y[i];\n\tdouble cos_theta = (a1 * b1 + a2 * b2) / (sqrt(a1 * a1 + a2 * a2) * sqrt(b1 * b1 + b2 * b2));\n\treturn cos_theta > cos(theta * M_PI / 180);\n}\n\nint main() {\n\tint n;\n\tcin >> n;\n\tcin >> r >> theta;\n\n\tfor(int i=0;i<n;i++) {\n\t\tcin >> x[i] >> y[i];\n\t}\n\n\tvector<int> to_set[22][22];\n\tfor (int i = 0; i < n; i++) {\n\t\tfor (int j = 0; j<n; j++) {\n\t\t\tfor (int k = 0; k<n; k++) {\n\t\t\t\tif (i == k || j == k) continue;\n\t\t\t\tif(check_angle(i, j, k)) {\n\t\t\t\t\tto_set[i][j].push_back(k);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tdouble dists[22][22];\n\tfor (int i = 0; i < n; i++) {\n\t\tfor (int j = 0; j<n; j++) {\n\t\t\tdists[i][j] = sqrt((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]));\n\t\t}\n\t}\n\n\tdouble dp[2][22][22];\n\tfor (int i = 0; i < n; i++) {\n\t\tfor (int j = 0; j<n; j++) {\n\t\t\tdp[0][i][j] = -1;\n\t\t}\n\t}\n\tfor (int i = 1; i<n; i++) {\n\t\tif(dists[0][i] <= r) {\n\t\t\tdp[0][0][i] = dists[0][i];\n\t\t}\n\t}\n\n\tint max_carrot = int(r) + 2;\n\tint cur = 0, next = 1;\n\tint ans = 0;\n\tfor(int carrot=1;carrot<max_carrot;carrot++) {\n\t\tfor(int i=0;i<n;i++) {\n\t\t\tfor(int j=0;j<n;j++) {\n\t\t\t\tdp[next][i][j] = -1;\n\t\t\t}\n\t\t}\n\t\tfor (int i = 0; i<n; i++) {\n\t\t\tfor (int j = 0; j<n; j++) {\n\t\t\t\tif(dp[cur][i][j] > 0) {\n\t\t\t\t\tans = carrot;\n\t\t\t\t\tfor (int to : to_set[i][j]) {\n\t\t\t\t\t\tdouble nd = dp[cur][i][j] + dists[j][to];\n\t\t\t\t\t\tif (nd > r) continue;\n\t\t\t\t\t\tif(dp[next][j][to] < 0 || dp[next][j][to] > nd) {\n\t\t\t\t\t\t\tdp[next][j][to] = nd;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif (ans != carrot) break;\n\t\tswap(cur, next);\n\t}\n\tcout << ans << endl;\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3900, "score_of_the_acc": -0.0033, "final_rank": 1 }, { "submission_id": "aoj_2233_9697481", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n/**\n * @brief template\n */\n\ndouble PI = 3.1415926535;\n\ndouble Angle(double X1, double Y1, double X2, double Y2) {\n double X = X1*X2+Y1*Y2, Y = X1*Y2-X2*Y1;\n return atan2(Y,X);\n}\n\nint main() {\n int N;\n cin >> N;\n double R, T;\n cin >> R >> T;\n T *= PI/180.0;\n vector<double> X(N), Y(N);\n rep(i,0,N) cin >> X[i] >> Y[i];\n vector<vector<vector<bool>>> G(N,vector<vector<bool>>(N,vector<bool>(N,false)));\n rep(i,0,N) {\n rep(j,0,N) {\n rep(k,0,N) {\n if (i == j || j == k || k == i) continue;\n if (abs(Angle(X[j]-X[i],Y[j]-Y[i],X[k]-X[j],Y[k]-Y[j])) <= T) {\n G[i][j][k] = true;\n }\n }\n }\n }\n auto Dist = [&](int i, int j) -> double {\n double dx = X[j]-X[i], dy = Y[j]-Y[i];\n return sqrt(dx*dx+dy*dy);\n };\n int MAXS = 10010;\n vector DP(N,vector(N,vector<double>(MAXS,(double)INF)));\n auto encode = [&](int i, int j, int k) -> int {\n return i*N*MAXS+j*MAXS+k;\n };\n auto decode = [&](int i) -> tuple<int,int,int> {\n return {i/(N*MAXS),(i/MAXS)%N,i%MAXS};\n };\n priority_queue<pair<double,int>,vector<pair<double,int>>,greater<pair<double,int>>> PQ;\n rep(i,1,N) {\n if (Dist(0,i) > R) continue;\n DP[0][i][1] = Dist(0,i);\n PQ.push({Dist(0,i),encode(0,i,1)});\n }\n while(!PQ.empty()) {\n auto [D,EN] = PQ.top();\n PQ.pop();\n auto [P,V,C] = decode(EN);\n if (DP[P][V][C] < D) continue;\n rep(i,0,N) {\n if (V == i) continue;\n if (!G[P][V][i]) continue;\n double ND = D + Dist(V,i);\n if (ND>R) continue;\n if (chmin(DP[V][i][C+1],ND)) {\n PQ.push({ND,encode(V,i,C+1)});\n }\n }\n }\n int ANS = 0;\n rep(i,0,N) {\n rep(j,0,N) {\n rep(k,0,MAXS) {\n if (DP[i][j][k] <= R) chmax(ANS,k);\n }\n }\n }\n cout << ANS << endl;\n}", "accuracy": 1, "time_ms": 270, "memory_kb": 36548, "score_of_the_acc": -0.7451, "final_rank": 6 }, { "submission_id": "aoj_2233_9375505", "code_snippet": "#include <bits/stdc++.h>\n\n\n\n\nnamespace zawa {\n\nusing i16 = std::int16_t;\nusing i32 = std::int32_t;\nusing i64 = std::int64_t;\nusing i128 = __int128_t;\n\nusing u8 = std::uint8_t;\nusing u16 = std::uint16_t;\nusing u32 = std::uint32_t;\nusing u64 = std::uint64_t;\n\nusing usize = std::size_t;\n\n} // namespace zawa\n\n\nnamespace zawa {\n\nvoid SetFastIO() {\n std::cin.tie(nullptr)->sync_with_stdio(false);\n}\n\nvoid SetPrecision(u32 dig) {\n std::cout << std::fixed << std::setprecision(dig);\n}\n\n} // namespace zawa\n\n\n\n\nnamespace zawa {\n\nnamespace geometryR2 {\n\nusing Real = long double;\n\nnamespace internal {\n\nReal EPS{1e-12};\nconstexpr i32 negative{-1};\nconstexpr i32 zero{};\nconstexpr i32 positive{1};\n\n} // namespace internal\n\nReal& Eps() {\n return internal::EPS;\n}\n\ni32 Sign(Real value) {\n if (value < -Eps()) return internal::negative;\n if (value > Eps()) return internal::positive;\n return internal::zero;\n}\n\nbool Zero(Real value) {\n return Sign(value) == internal::zero;\n}\n\nbool Positive(Real value) {\n return Sign(value) == internal::positive;\n}\n\nbool Negative(Real value) {\n return Sign(value) == internal::negative;\n}\n\nbool Equal(Real a, Real b) {\n return Zero(a - b);\n}\n\nbool Smaller(Real a, Real b) {\n return Negative(a - b);\n}\n\nbool Bigger(Real a, Real b) {\n return Positive(a - b);\n}\n\nReal Square(Real value) {\n return (Zero(value) ? value : value * value);\n}\n\nReal Sqrt(Real value) {\n assert(!Negative(value));\n return (Zero(value) ? value : sqrtl(value));\n}\n\nReal Abs(Real value) {\n return (Negative(value) ? -value : value);\n}\n\n} // namespace geometryR2\n \n} // namespace zawa\n\n\nnamespace zawa {\n\nnamespace geometryR2 {\n\nconstexpr Real PI{acosl(-1)};\nconstexpr Real TAU{static_cast<Real>(2) * PI};\n\nconstexpr Real ArcToRadian(Real arc) {\n return (arc * PI) / static_cast<Real>(180);\n}\n\nconstexpr Real RadianToArc(Real radian) {\n return (radian * static_cast<Real>(180)) / PI;\n}\n\n} // namespace geometryR2\n\n} // namespace zawa\n\n\n\nnamespace zawa {\n\nnamespace geometryR2 {\n\nclass Point {\nprivate:\n Real x_{}, y_{};\npublic:\n /* constructor */\n Point() = default;\n Point(Real x, Real y) : x_{x}, y_{y} {}\n\n /* getter, setter */\n Real x() const {\n return x_;\n }\n Real& x() {\n return x_;\n }\n Real y() const {\n return y_;\n }\n Real& y() {\n return y_;\n }\n\n /* operator */\n Point& operator+=(const Point& rhs) {\n x_ += rhs.x();\n y_ += rhs.y();\n return *this;\n }\n friend Point operator+(const Point& lhs, const Point& rhs) {\n return Point{lhs} += rhs;\n }\n Point operator+() const {\n return *this;\n }\n Point& operator-=(const Point& rhs) {\n x_ -= rhs.x();\n y_ -= rhs.y();\n return *this;\n }\n friend Point operator-(const Point& lhs, const Point& rhs) {\n return Point{lhs} -= rhs;\n }\n Point operator-() const {\n return Point{} - *this;\n }\n Point& operator*=(Real k) {\n x_ *= k;\n y_ *= k;\n return *this;\n }\n friend Point operator*(Real k, const Point& p) {\n return Point{p} *= k;\n }\n friend Point operator*(const Point& p, Real k) {\n return Point{p} *= k;\n }\n Point& operator/=(Real k) {\n assert(!Zero(k));\n x_ /= k;\n y_ /= k;\n return *this;\n }\n friend Point operator/(Real k, const Point& p) {\n return Point{p} /= k;\n }\n friend Point operator/(const Point& p, Real k) {\n return Point{p} /= k;\n }\n friend bool operator==(const Point& lhs, const Point& rhs) {\n return Equal(lhs.x(), rhs.x()) and Equal(lhs.y(), rhs.y());\n }\n friend bool operator!=(const Point& lhs, const Point& rhs) {\n return !Equal(lhs.x(), rhs.x()) or !Equal(lhs.y(), rhs.y());\n }\n friend bool operator<(const Point& lhs, const Point& rhs) {\n return Smaller(lhs.x(), rhs.x()) or \n (Equal(lhs.x(), rhs.x()) and Smaller(lhs.y(), rhs.y()));\n }\n friend bool operator<=(const Point& lhs, const Point& rhs) {\n return Smaller(lhs.x(), rhs.x()) or \n (Equal(lhs.x(), rhs.x()) and (Smaller(lhs.y(), rhs.y()) or Equal(lhs.y(), rhs.y())));\n }\n friend bool operator>(const Point& lhs, const Point& rhs) {\n return Bigger(lhs.x(), rhs.x()) or\n (Equal(lhs.x(), rhs.x()) and Bigger(lhs.y(), rhs.y()));\n }\n friend bool operator>=(const Point& lhs, const Point& rhs) {\n return Bigger(lhs.x(), rhs.x()) or\n (Equal(lhs.x(), rhs.x()) and (Bigger(lhs.y(), rhs.y()) or Equal(lhs.y(), rhs.y())));\n }\n friend std::istream& operator>>(std::istream& is, Point& p) {\n is >> p.x_ >> p.y_;\n return is;\n }\n friend std::ostream& operator<<(std::ostream& os, const Point& p) {\n os << '(' << p.x_ << ',' << p.y_ << ')';\n return os;\n }\n \n /* member function */\n Real normSquare() const {\n return Square(x_) + Square(y_);\n }\n Real norm() const {\n return Sqrt(normSquare());\n }\n void normalize() {\n assert((*this) != Point{});\n (*this) /= norm(); \n }\n Point normalized() const {\n Point res{*this};\n res.normalize();\n return res;\n }\n Point rotated(Real radian) const {\n return Point{\n x_ * cosl(radian) - y_ * sinl(radian),\n x_ * sinl(radian) + y_ * cosl(radian)\n };\n }\n void rotate(Real radian) {\n *this = rotated(radian); \n }\n Point rotatedByArc(Real arc) const {\n return rotated(ArcToRadian(arc));\n }\n void rotateByArc(Real arc) {\n *this = rotatedByArc(arc);\n }\n Real argument() const {\n return (Negative(y_) ? TAU : static_cast<Real>(0)) + atan2l(y_, x_);\n }\n Real argumentByArc() const {\n return RadianToArc(argument());\n }\n\n /* friend function */\n friend Real Dot(const Point& lhs, const Point& rhs) {\n return lhs.x() * rhs.x() + lhs.y() * rhs.y();\n }\n friend Real Cross(const Point& lhs, const Point& rhs) {\n return lhs.x() * rhs.y() - lhs.y() * rhs.x();\n }\n friend Real Argument(const Point& lhs, const Point& rhs) {\n return rhs.argument() - lhs.argument();\n }\n friend bool ArgComp(const Point& lhs, const Point& rhs) {\n return Smaller(lhs.argument(), rhs.argument());\n }\n};\n\nusing Vector = Point;\n\n} // namespace geometryR2\n\n} // namespace zawa\n\n\nnamespace zawa {\n\nnamespace geometryR2 {\n\nReal Distance(const Point& p0, const Point& p1) {\n return Point{p1 - p0}.norm();\n}\n\nReal DistanceSquare(const Point& p0, const Point& p1) {\n return Point{p1 - p0}.normSquare();\n}\n\n} // namespace geometryR2\n\n} // namespace zawa\nusing namespace zawa;\nusing namespace geometryR2;\n\nint main() {\n SetFastIO();\n\n int N;\n std::cin >> N;\n Real R, T;\n std::cin >> R >> T; \n T = ArcToRadian(T);\n std::vector<Point> P(N);\n for (auto& p : P) std::cin >> p;\n std::vector ok(N, std::vector(N, std::vector<Real>(N, -1.0)));\n for (int i{} ; i < N ; i++) {\n for (int j{} ; j < N ; j++) {\n for (int k{} ; k < N ; k++) {\n if (i == j or j == k or i == k) continue;\n Real arg{Abs(Argument(P[j] - P[i], P[k] - P[j]))};\n arg = std::min(arg, 2 * PI - arg);\n if (!Bigger(arg, T)) {\n ok[i][j][k] = Distance(P[j], P[k]);\n }\n }\n }\n }\n const Real INF{(Real)9e18};\n std::vector dp(N, std::vector<Real>(N, INF));\n for (int i{1} ; i < N ; i++) {\n dp[0][i] = Distance(P[0], P[i]);\n }\n int ans{};\n for (int i{1} ; i <= 10000 ; i++) {\n for (int j{} ; j < N ; j++) {\n for (int k{} ; k < N ; k++) {\n if (!Bigger(dp[j][k], R)) {\n ans = i;\n }\n }\n }\n std::vector next(N, std::vector<Real>(N, INF));\n for (int j{} ; j < N ; j++) {\n for (int k{} ; k < N ; k++) {\n for (int l{} ; l < N ; l++) {\n if (Positive(ok[j][k][l])) {\n next[k][l] = std::min(next[k][l], dp[j][k] + ok[j][k][l]);\n }\n }\n }\n }\n dp = std::move(next);\n }\n std::cout << ans << '\\n';\n}", "accuracy": 1, "time_ms": 360, "memory_kb": 3716, "score_of_the_acc": -0.394, "final_rank": 4 }, { "submission_id": "aoj_2233_9373131", "code_snippet": "#include <bits/stdc++.h>\n\n#include <algorithm>\n#include <cmath>\n#include <iostream>\n#include <iterator>\n#include <utility>\n#include <vector>\n\nusing Real = long double;\nconstexpr Real EPS = 1e-10;\nconst Real PI = std::acos(-1);\n\nint sign(Real x) {\n return x < -EPS ? -1 : x > EPS ? 1 : 0;\n}\n\nbool eq(Real lhs, Real rhs) {\n return sign(rhs - lhs) == 0;\n}\n\nbool lte(Real lhs, Real rhs) {\n return sign(rhs - lhs) >= 0;\n}\n\nbool lt(Real lhs, Real rhs) {\n return sign(rhs - lhs) > 0;\n}\n\nbool gte(Real lhs, Real rhs) {\n return lte(rhs, lhs);\n}\n\nbool gt(Real lhs, Real rhs) {\n return lt(rhs, lhs);\n}\n\nstruct Point {\n Real x;\n Real y;\n Point(Real x = 0, Real y = 0): x(x), y(y) {}\n Point& operator+=(Point rhs) {\n this->x += rhs.x;\n this->y += rhs.y;\n return *this;\n }\n Point& operator-=(Point rhs) {\n this->x -= rhs.x;\n this->y -= rhs.y;\n return *this;\n }\n Point& operator*=(Real k) {\n this->x *= k;\n this->y *= k;\n return *this;\n }\n Point& operator/=(Real k) {\n this->x /= k;\n this->y /= k;\n return *this;\n }\n friend Point operator+(Point lhs, Point rhs) {\n return lhs += rhs;\n }\n friend Point operator-(Point lhs, Point rhs) {\n return lhs -= rhs;\n }\n friend Point operator*(Point p, Real k) {\n return p *= k;\n }\n friend Point operator/(Point p, Real k) {\n return p /= k;\n }\n friend Point operator*(Real k, Point p) {\n return p * k;\n }\n friend Point operator/(Real k, Point p) {\n return p / k;\n }\n friend bool operator==(Point lhs, Point rhs) {\n return eq(lhs.x, rhs.x) && eq(lhs.y, rhs.y);\n }\n friend bool operator!=(Point lhs, Point rhs) {\n return !(lhs == rhs);\n }\n friend bool operator<(Point lhs, Point rhs) {\n if (eq(lhs.x, rhs.x)) return lt(lhs.y, rhs.y);\n return lt(lhs.x, rhs.x);\n }\n friend bool operator<=(Point lhs, Point rhs) {\n return lhs == rhs || lhs < rhs;\n }\n friend bool operator>(Point lhs, Point rhs) {\n return rhs < lhs;\n }\n friend bool operator>=(Point lhs, Point rhs) {\n return rhs <= lhs;\n }\n friend std::istream& operator>>(std::istream& is, Point& p) {\n is >> p.x >> p.y;\n return is;\n }\n friend std::ostream& operator<<(std::ostream& os, Point& p) {\n os << p.x << ' ' << p.y;\n return os;\n }\n};\n\nusing Points = std::vector<Point>;\nusing Polygon = Points;\nusing Vector = Point;\n\nReal norm(Vector p) {\n return p.x * p.x + p.y * p.y;\n}\n\nReal abs(Vector p) {\n return std::sqrt(norm(p));\n}\n\nReal dot(Vector lhs, Vector rhs) {\n return lhs.x * rhs.x + lhs.y * rhs.y;\n}\n\nReal cross(Vector lhs, Vector rhs) {\n return lhs.x * rhs.y - lhs.y * rhs.x;\n}\n\nint ccw(Point p0, Point p1, Point p2) {\n Vector a = p1 - p0;\n Vector b = p2 - p0;\n\n if (cross(a, b) > EPS) return 1;\n\n if (cross(a, b) < -EPS) return -1;\n\n if (dot(a, b) < 0) return 2;\n\n if (norm(a) < norm(b)) return -2;\n\n return 0;\n}\n\nstruct Circle {\n Real r;\n Point c;\n Circle() {}\n Circle(Real r, Point c): r(r), c(c) {}\n};\n\nstruct Segment {\n Segment() {}\n Segment(Point p0, Point p1): p0(p0), p1(p1) {}\n Point p0, p1;\n};\n\nstruct Line {\n Line() {}\n Line(Point p0, Point p1): p0(p0), p1(p1) {}\n explicit Line(Segment s): p0(s.p0), p1(s.p1) {}\n Point p0, p1;\n};\n\nReal distance(Point lhs, Point rhs) {\n return abs(rhs - lhs);\n}\n\nReal distance(Line l, Point p) {\n return std::abs(cross(l.p1 - l.p0, p - l.p0)) / abs(l.p1 - l.p0);\n}\n\nReal distance(Segment s, Point p) {\n if (dot(s.p1 - s.p0, p - s.p0) < 0) {\n return distance(p, s.p0);\n }\n if (dot(s.p0 - s.p1, p - s.p1) < 0) {\n return distance(p, s.p1);\n }\n return distance(Line(s), p);\n}\n\nbool intersect(Segment lhs, Segment rhs) {\n return ccw(lhs.p0, lhs.p1, rhs.p0) * ccw(lhs.p0, lhs.p1, rhs.p1) <= 0\n && ccw(rhs.p0, rhs.p1, lhs.p0) * ccw(rhs.p0, rhs.p1, lhs.p1) <= 0;\n}\n\nbool intersect(Segment s, Point p) {\n return ccw(s.p0, s.p1, p) == 0;\n}\n\nReal distance(Segment lhs, Segment rhs) {\n if (intersect(lhs, rhs)) {\n return Real(0);\n }\n return std::min({distance(lhs, rhs.p0), distance(lhs, rhs.p1), distance(rhs, lhs.p0), distance(rhs, lhs.p1)});\n}\n\nbool parallel(Vector lhs, Vector rhs) {\n return eq(cross(lhs, rhs), 0);\n}\n\nbool parallel(Segment lhs, Segment rhs) {\n return parallel(lhs.p0 - lhs.p1, rhs.p0 - rhs.p1);\n}\n\nbool parallel(Line lhs, Line rhs) {\n return parallel(lhs.p0 - lhs.p1, rhs.p0 - rhs.p1);\n}\n\nPoint crosspoint(Line lhs, Line rhs) {\n Real a = cross(lhs.p1 - lhs.p0, rhs.p1 - rhs.p0);\n Real b = cross(lhs.p1 - lhs.p0, lhs.p1 - rhs.p0);\n return rhs.p0 + (rhs.p1 - rhs.p0) * b / a;\n}\n\nbool intersect(Line l, Segment s) {\n return ccw(l.p0, l.p1, s.p0) * ccw(l.p0, l.p1, s.p1) <= 0;\n}\n\nbool intersect(Circle c, Line l) {\n return lte(distance(l, c.c), c.r);\n}\n\nbool contain(Circle c, Point p) {\n return lt(distance(c.c, p), c.r);\n}\n\nbool intersect(Circle c, Point p) {\n return eq(distance(c.c, p), c.r);\n}\n\nbool internal(Circle c, Point p) {\n return lt(distance(c.c, p), c.r);\n}\n\nbool intersect(Circle c, Segment s) {\n return lte(distance(s, c.c), c.r);\n}\n\nint intersect(Circle lhs, Circle rhs) {\n if (gt(lhs.r, rhs.r)) std::swap(lhs, rhs);\n Real d = distance(lhs.c, rhs.c);\n if (lt(lhs.r + rhs.r, d)) return 4;\n if (eq(lhs.r + rhs.r, d)) return 3;\n if (gt(lhs.r + d, rhs.r)) return 2;\n if (eq(lhs.r + d, rhs.r)) return 1;\n return 0;\n}\n\nbool orthogonal(Vector lhs, Vector rhs) {\n return eq(dot(lhs, rhs), 0);\n}\n\nbool orthogonal(Segment lhs, Segment rhs) {\n return orthogonal(lhs.p0 - lhs.p1, rhs.p0 - rhs.p1);\n}\n\nbool orthogonal(Line lhs, Line rhs) {\n return orthogonal(lhs.p0 - lhs.p1, rhs.p0 - rhs.p1);\n}\n\nPoint crosspoint(Segment lhs, Segment rhs) {\n Real d0 = distance(Line(lhs.p0, lhs.p1), rhs.p0);\n Real d1 = distance(Line(lhs.p0, lhs.p1), rhs.p1);\n return rhs.p0 + (rhs.p1 - rhs.p0) * (d0 / (d0 + d1));\n}\n\nReal arg(Vector p) {\n return std::atan2(p.y, p.x);\n}\n\nVector polar(Real a, Real r) {\n return Point(std::cos(r) * a, std::sin(r) * a);\n}\n\nPoint rotate(Point p, Real t) {\n Point v = polar(1, t);\n return Point(p.x * v.x - p.y * v.y, p.x * v.y + p.y * v.x);\n}\n\nReal angle(Point p0, Point p1, Point p2) {\n Real a = arg(p0 - p1);\n Real b = arg(p2 - p1);\n if (gt(a, b)) std::swap(a, b);\n return std::min(b - a, 2 * PI - (b - a));\n}\n\nPoints crosspoint(Circle lhs, Circle rhs) {\n Real d = abs(lhs.c - rhs.c);\n if (eq(d, lhs.r + rhs.r)) return {lhs.c + lhs.r * (rhs.c - lhs.c) / d};\n Real a = std::acos((lhs.r * lhs.r + d * d - rhs.r * rhs.r) / (2 * lhs.r * d));\n Real t = arg(rhs.c - lhs.c);\n return {lhs.c + polar(lhs.r, t + a), lhs.c + polar(lhs.r, t - a)};\n}\n\nPoint projection(Segment s, Point p) {\n Vector a = p - s.p0;\n Vector b = s.p1 - s.p0;\n Real t = dot(a, b) / norm(b);\n return s.p0 + t * b;\n}\n\nPoint projection(Line l, Point p) {\n Vector a = p - l.p0;\n Vector b = l.p1 - l.p0;\n Real t = dot(a, b) / norm(b);\n return l.p0 + t * b;\n}\n\nPoint reflection(Segment s, Point p) {\n return p + (projection(s, p) - p) * Real(2);\n}\n\nPoint reflection(Line l, Point p) {\n return p + (projection(l, p) - p) * Real(2);\n}\n\nPoints crosspoint(Circle c, Line l) {\n Vector p = projection(l, c.c);\n Real t = std::sqrt(c.r * c.r - norm(c.c - p));\n if (eq(t, 0)) return {p};\n Vector e = (l.p1 - l.p0) / abs(l.p1 - l.p0);\n return {p + t * e, p + -t * e};\n}\n\nPoints crosspoint(Circle c, Segment s) {\n auto cps = crosspoint(c, Line(s));\n Points temp;\n for (auto cp: cps) {\n if (intersect(s, cp)) temp.push_back(cp);\n }\n return temp;\n}\n\nReal area(Polygon poly) {\n int n = poly.size();\n Real result = 0;\n for (int i = 0; i < n; i++) {\n result += cross(poly[i], poly[(i + 1) % n]);\n }\n return result / 2;\n}\n\nReal perimeter(Polygon poly) {\n int n = poly.size();\n Real result = 0;\n for (int i = 0; i < n; i++) {\n result += abs(poly[i] - poly[(i + 1) % n]);\n }\n return result;\n}\n\nbool convex(Polygon poly) {\n int n = poly.size();\n for (int i = 0; i < n; i++) {\n if (ccw(poly[i], poly[(i + 1) % n], poly[(i + 2) % n]) == -1) return false;\n }\n return true;\n}\n\nint contain(Polygon poly, Point p) {\n int n = poly.size();\n bool parity = false;\n for (int i = 0; i < n; i++) {\n Point a = poly[i] - p;\n Point b = poly[(i + 1) % n] - p;\n if (gt(a.y, b.y)) std::swap(a, b);\n if (lte(a.y, 0) && lt(0, b.y) && lt(cross(a, b), 0)) parity ^= true;\n if (eq(cross(a, b), 0) && lte(dot(a, b), 0)) return 1;\n }\n return (parity ? 2 : 0);\n}\n\nPolygon convex_hull(Points points) {\n std::sort(points.begin(), points.end());\n if (points.size() < 3) return points;\n int n = points.size();\n Points lower, upper;\n lower.push_back(points[n - 1]);\n lower.push_back(points[n - 2]);\n upper.push_back(points[0]);\n upper.push_back(points[1]);\n for (int i = n - 3; i >= 0; i--) {\n for (int m = lower.size(); m >= 2 && ccw(lower[m - 2], lower[m - 1], points[i]) >= 0; m--) {\n lower.pop_back();\n }\n lower.push_back(points[i]);\n }\n for (int i = 2; i < n; i++) {\n for (int m = upper.size(); m >= 2 && ccw(upper[m - 2], upper[m - 1], points[i]) >= 0; m--) {\n upper.pop_back();\n }\n upper.push_back(points[i]);\n }\n std::reverse(lower.begin(), lower.end());\n std::copy(std::next(upper.rbegin()), std::prev(upper.rend()), std::back_inserter(lower));\n return lower;\n}\n\nPolygon convex_cut(Polygon poly, Line l) {\n int n = poly.size();\n Polygon res;\n for (int i = 0; i < n; i++) {\n Point a = poly[i];\n Point b = poly[(i + 1) % n];\n if (ccw(l.p0, l.p1, a) != -1) res.push_back(a);\n if (ccw(l.p0, l.p1, a) * ccw(l.p0, l.p1, b) == -1) {\n res.push_back(crosspoint(l, Line(a, b)));\n }\n }\n return res;\n}\n\nLine bisector(Point p0, Point p1, Point p2) {\n return Line(p1, p1 + polar(1, angle(p0, p1, p2) / 2));\n}\n\nCircle incircle(Point p0, Point p1, Point p2) {\n Point c = crosspoint(bisector(p0, p1, p2), bisector(p1, p2, p0));\n Real r = std::abs(2 * area({p0, p1, p2}) / perimeter({p0, p1, p2}));\n return Circle(r, c);\n}\n\nLine perpendicular(Line l, Point p) {\n Vector v = l.p1 - l.p0;\n return Line(p, p + Vector(-v.y, v.x));\n}\n\nLine perpendicular_bisector(Segment s) {\n return perpendicular(Line(s), (s.p0 + s.p1) / 2);\n}\n\nCircle circumscribed_circle(Point p0, Point p1, Point p2) {\n Point c = crosspoint(perpendicular_bisector(Segment(p0, p1)), perpendicular_bisector(Segment(p0, p2)));\n return Circle(distance(p0, c), c);\n}\n\nReal area(Circle c) {\n return c.r * c.r * PI;\n}\n\nPoints tangent(Circle c, Point p) {\n return crosspoint(c, Circle(std::sqrt(norm(c.c - p) - c.r * c.r), p));\n}\n\nbool contain(Polygon ps, Segment s) {\n if (contain(ps, s.p0) == 0 || contain(ps, s.p1) == 0) return false;\n int n = ps.size();\n Points cps;\n cps.push_back(s.p0);\n cps.push_back(s.p1);\n for (int i = 0; i < n; i++) {\n Segment t(ps[i], ps[(i + 1) % n]);\n if (parallel(s, t)) continue;\n if (!intersect(s, t)) continue;\n Point cp = crosspoint(s, t);\n if (cp != s.p0 && cp != s.p1 && cp != t.p0 && cp != t.p1) return false;\n cps.push_back(cp);\n }\n std::sort(cps.begin(), cps.end());\n int m = cps.size();\n for (int i = 0; i + 1 < m; i++) {\n if (contain(ps, (cps[i] + cps[i + 1]) / 2) == 0) return false;\n }\n return true;\n}\n\nstd::pair<Points, Points> lower_and_upper_convex_hull(Polygon ps) {\n auto it = std::max_element(ps.begin(), ps.end());\n Points lower(ps.begin(), it + 1);\n Points upper(it, ps.end());\n upper.push_back(ps.front());\n std::reverse(upper.begin(), upper.end());\n return {lower, upper};\n}\n\nReal y(Line l, Real x) {\n if (eq(l.p0.x, l.p1.x)) return std::min(l.p0.y, l.p1.y);\n return x * (l.p1.y - l.p0.y) / (l.p1.x - l.p0.x);\n}\n\nPolygon common_polygon(Polygon lhs, Polygon rhs) {\n if (lhs.size() < 3 || rhs.size() < 3) return {};\n auto [ll, lu] = lower_and_upper_convex_hull(lhs);\n auto [rl, ru] = lower_and_upper_convex_hull(rhs);\n constexpr Real INF = 1e18;\n ll.push_back(Point(INF, INF));\n lu.push_back(Point(INF, INF));\n rl.push_back(Point(INF, INF));\n ru.push_back(Point(INF, INF));\n\n Points lower, upper;\n for (int i = 0, j = 0, k = 0, l = 0;;) {\n if (ll[i] <= lu[j] && ll[i] <= rl[k] && ll[i] <= ru[l]) {\n if (0 < k && 0 < l) {\n if (lte(y(Line(rl[k - 1], rl[k]), ll[i].x), ll[i].y)\n && lte(ll[i].y, y(Line(ru[l - 1], ru[l]), ll[i].x))) {\n lower.push_back(ll[i]);\n }\n }\n if (i + 2 < (int)ll.size()) {\n Segment lls(ll[i], ll[i + 1]);\n if (0 < k) {\n Segment rls(rl[k - 1], rl[k]);\n if (intersect(lls, rls) && !parallel(lls, rls)) {\n lower.push_back(crosspoint(lls, rls));\n }\n }\n if (0 < l) {\n Segment rus(ru[l - 1], ru[l]);\n if (intersect(lls, rus) && !parallel(lls, rus)) {\n lower.push_back(crosspoint(lls, rus));\n upper.push_back(crosspoint(lls, rus));\n }\n }\n }\n i++;\n if (i + 1 == (int)ll.size() && j + 1 == (int)lu.size()) break;\n } else if (lu[j] <= ll[i] && lu[j] <= rl[k] && lu[j] <= ru[l]) {\n if (0 < k && 0 < l) {\n if (lte(y(Line(rl[k - 1], rl[k]), lu[j].x), lu[j].y)\n && lte(lu[j].y, y(Line(ru[l - 1], ru[l]), lu[j].x))) {\n upper.push_back(lu[j]);\n }\n }\n if (j + 2 < (int)lu.size()) {\n Segment lus(lu[j], lu[j + 1]);\n if (0 < l) {\n Segment rus(ru[l - 1], ru[l]);\n if (intersect(lus, rus) && !parallel(lus, rus)) {\n upper.push_back(crosspoint(lus, rus));\n }\n }\n if (0 < k) {\n Segment rls(rl[k - 1], rl[k]);\n if (intersect(lus, rls) && !parallel(lus, rls)) {\n lower.push_back(crosspoint(lus, rls));\n upper.push_back(crosspoint(lus, rls));\n }\n }\n }\n j++;\n if (i + 1 == (int)ll.size() && j + 1 == (int)lu.size()) break;\n } else if (rl[k] <= ll[i] && rl[k] <= lu[j] && rl[k] <= ru[l]) {\n if (0 < i && 0 < j) {\n if (lte(y(Line(ll[i - 1], ll[i]), rl[k].x), rl[k].y)\n && lte(rl[k].y, y(Line(lu[j - 1], lu[j]), rl[k].x))) {\n lower.push_back(rl[k]);\n }\n }\n if (k + 2 < (int)rl.size()) {\n Segment rls(rl[k], rl[k + 1]);\n if (0 < i) {\n Segment lls(ll[i - 1], ll[i]);\n if (intersect(rls, lls) && !parallel(rls, lls)) {\n lower.push_back(crosspoint(rls, lls));\n }\n }\n if (0 < j) {\n Segment lus(lu[j - 1], lu[j]);\n if (intersect(rls, lus) && !parallel(rls, lus)) {\n lower.push_back(crosspoint(rls, lus));\n upper.push_back(crosspoint(rls, lus));\n }\n }\n }\n k++;\n if (k + 1 == (int)rl.size() && l + 1 == (int)ru.size()) break;\n } else {\n if (0 < i && 0 < j) {\n if (lte(y(Line(ll[i - 1], ll[i]), ru[l].x), ru[l].y)\n && lte(ru[l].y, y(Line(lu[j - 1], lu[j]), ru[l].x))) {\n upper.push_back(ru[l]);\n }\n }\n if (l + 2 < (int)ru.size()) {\n Segment rus(ru[l], ru[l + 1]);\n if (0 < j) {\n Segment lus(lu[j - 1], lu[j]);\n if (intersect(rus, lus) && !parallel(rus, lus)) {\n upper.push_back(crosspoint(rus, lus));\n }\n }\n if (0 < i) {\n Segment lls(ll[i - 1], ll[i]);\n if (intersect(rus, lls) && !parallel(rus, lls)) {\n lower.push_back(crosspoint(rus, lls));\n upper.push_back(crosspoint(rus, lls));\n }\n }\n }\n l++;\n if (k + 1 == (int)rl.size() && l + 1 == (int)ru.size()) break;\n }\n }\n if (lower.empty() && upper.empty()) return {};\n std::cerr << lhs.size() << ' ' << rhs.size() << ' ' << lower.size() << ' ' << upper.size() << std::endl;\n\n std::copy(std::next(upper.rbegin()), std::prev(upper.rend()), std::back_inserter(lower));\n return lower;\n}\n\nbool intersect(Polygon lhs, Polygon rhs) {\n return lt(0, area(common_polygon(lhs, rhs)));\n}\n\nstd::vector<Line> tangent(Circle c1, Circle c2) {\n std::vector<Line> ret;\n if (c1.r < c2.r) std::swap(c1, c2);\n Real g = norm(c1.c - c2.c);\n if (eq(g, 0)) return ret;\n Point u = (c2.c - c1.c) / sqrt(g);\n Point v = rotate(u, PI * 0.5);\n for (int s: {-1, 1}) {\n Real h = (c1.r + s * c2.r) / sqrt(g);\n if (eq(1 - h * h, 0)) {\n ret.emplace_back(c1.c + u * c1.r, c1.c + (u + v) * c1.r);\n } else if (1 - h * h > 0) {\n Point uu = u * h, vv = v * sqrt(1 - h * h);\n ret.emplace_back(c1.c + (uu + vv) * c1.r, c2.c - (uu + vv) * c2.r * s);\n ret.emplace_back(c1.c + (uu - vv) * c1.r, c2.c - (uu - vv) * c2.r * s);\n }\n }\n return ret;\n}\n\nusing namespace std;\n#define rep(i, n) for (int i = 0; i < (n); i++)\nusing ll = long long;\n#define all(x) x.begin(), x.end()\ntemplate <class A, class B>\ninline bool chmin(A& a, const B& b) {\n return b < a && (a = b, true);\n}\ntemplate <class A, class B>\ninline bool chmax(A& a, const B& b) {\n return b > a && (a = b, true);\n}\n\nint main() {\n int n;\n cin >> n;\n Real max_r, theta;\n cin >> max_r >> theta;\n int max_carrot = ceil(max_r) + 1;\n Points ps(n);\n rep(i, n) cin >> ps[i];\n\n vector dist(n, vector<Real>(n));\n\n vector ok(n, vector(n, vector<bool>(n)));\n rep(i, n) rep(j, n) rep(k, n) {\n Real a = angle(ps[i], ps[j], ps[k]);\n Real r = (a / (2 * PI)) * 360;\n r = 180 - r;\n if (lte(r, theta)) ok[i][j][k] = true;\n }\n\n rep(i, n) rep(j, n) dist[i][j] = distance(ps[i], ps[j]);\n\n const Real NONE = 1e18;\n vector dp(n, vector<Real>(n, NONE));\n int ans = 0;\n rep(i, n) if (i != 0) {\n if (lte(dist[0][i], max_r)) {\n ans = 1;\n dp[0][i] = dist[0][i];\n }\n }\n rep(i, max_carrot) {\n vector ndp(n, vector<Real>(n, NONE));\n rep(j, n) rep(k, n) if (lt(dp[j][k], max_r)) {\n rep(l, n) if (j != k && k != l && l != j) if (ok[j][k][l]) {\n chmin(ndp[k][l], dp[j][k] + dist[k][l]);\n }\n }\n bool exist = false;\n dp = move(ndp);\n rep(j, n) rep(k, n) if (lte(dp[j][k], max_r)) {\n exist = true;\n break;\n }\n if (exist)\n ans = i + 2;\n else\n break;\n }\n cout << ans << endl;\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 3660, "score_of_the_acc": -0.0787, "final_rank": 2 }, { "submission_id": "aoj_2233_8992313", "code_snippet": "#line 2 \"cp-library/src/cp-template.hpp\"\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nusing i32 = int;\nusing u32 = unsigned int;\nusing i64 = long long;\nusing u64 = unsigned long long;\nusing i128 = __int128_t;\ntemplate < class T > bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }\ntemplate < class T > bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }\ntemplate < class T, class U > T ceil (T x, U y) { return (x > 0 ? (x + y - 1) / y : x / y); }\ntemplate < class T, class U > T floor(T x, U y) { return (x > 0 ? x / y : (x - y + 1) / y); }\nint popcnt(i32 x) { return __builtin_popcount(x); }\nint popcnt(u32 x) { return __builtin_popcount(x); }\nint popcnt(i64 x) { return __builtin_popcountll(x); }\nint popcnt(u64 x) { return __builtin_popcountll(x); }\n\n#line 2 \"cp-library/src/utility/rep_itr.hpp\"\ntemplate < class T > struct itr_rep {\n T i, d;\n constexpr itr_rep(const T i) noexcept : i(i), d(1) {}\n constexpr itr_rep(const T i, const T d) noexcept : i(i), d(d) {}\n void operator++() noexcept { i += d; }\n constexpr int operator*() const noexcept { return i; }\n constexpr bool operator!=(const itr_rep x) const noexcept { return d > 0 ? i < x.i : i > x.i; }\n};\n\ntemplate < class T > struct rep {\n const itr_rep< T > s, t;\n constexpr rep(const T t) noexcept : s(0), t(t) {}\n constexpr rep(const T s, const T t) noexcept : s(s), t(t) {}\n constexpr rep(const T s, const T t, const T d) noexcept : s(s, d), t(t, d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n\ntemplate < class T > struct revrep {\n const itr_rep < T > s, t;\n constexpr revrep(const T t) noexcept : s(t - 1, -1), t(-1, -1) {}\n constexpr revrep(const T s, const T t) noexcept : s(t - 1, -1), t(s - 1, -1) {}\n constexpr revrep(const T s, const T t, const T d) noexcept : s(t - 1, -d), t(s - 1, -d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n#line 3 \"cp-library/src/utility/io.hpp\"\n\n/* 128bit integer */\nistream& operator>>(istream& is, i128& x) {\n std::string s; is >> s;\n int pm = (s[0] == '-');\n x = 0;\n for(int i : rep(pm, int(s.size()))) x = x * 10 + (s[i] - '0');\n if(pm) x *= -1;\n return is;\n}\nostream& operator<<(ostream& os, const i128& x) {\n if(x == 0) return os << '0';\n i128 y = x;\n if(y < 0) { os << '-'; y *= -1; }\n std::vector<int> ny;\n while(y > 0) { ny.push_back(y % 10); y /= 10; }\n for(int i : revrep(ny.size())) os << ny[i];\n return os;\n}\n\ntemplate < class S, class T > istream& operator>>(istream& is, std::pair< S, T >& x) { is >> x.first >> x.second; return is; }\ntemplate < class S, class T > ostream& operator<<(ostream& os, const std::pair< S, T >& x) { os << x.first << \" \" << x.second; return os; }\n\nnamespace scanner {\n struct sca {\n template < class T > operator T() {\n T s; std::cin >> s; return s;\n }\n };\n struct vec {\n int n;\n vec(int n) : n(n) {}\n template < class T > operator std::vector< T >() {\n std::vector< T > v(n);\n for(T& x : v) std::cin >> x;\n return v;\n }\n };\n struct mat {\n int h, w;\n mat(int h, int w) : h(h), w(w) {}\n template < class T > operator std::vector< std::vector< T > >() {\n std::vector m(h, std::vector< T >(w));\n for(std::vector< T >& v : m) for(T& x : v) std::cin >> x;\n return m;\n }\n };\n struct speedup {\n speedup() {\n std::cin.tie(0);\n std::ios::sync_with_stdio(0);\n }\n } speedup_instance;\n}\nscanner::sca in() { return scanner::sca(); }\nscanner::vec in(int n) { return scanner::vec(n); }\nscanner::mat in(int h, int w) { return scanner::mat(h, w); }\n\nnamespace printer {\n void precision(int d) { std::cout << std::fixed << std::setprecision(d); }\n void flush() { std::cout.flush(); }\n}\n\ntemplate < class T >\nostream& operator<<(ostream& os, const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) { os << a[i]; if(i != n - 1) os << ' '; }\n return os;\n}\n\nint print() { std::cout << '\\n'; return 0; }\ntemplate < class head, class... tail > int print(head&& h, tail&&... t) {\n std::cout << h; if(sizeof...(tail)) std::cout << ' ';\n return print(std::forward<tail>(t)...);\n}\ntemplate < class T > int print_n(const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) std::cout << a[i] << \"\\n\";\n return 0;\n}\n\n\n#line 2 \"cp-library/src/utility/key_val.hpp\"\n\ntemplate < class K, class V >\nstruct key_val {\n K key; V val;\n key_val() {}\n key_val(K key, V val) : key(key), val(val) {}\n template < std::size_t Index >\n std::tuple_element_t< Index, key_val >& get() {\n if constexpr (Index == 0) return key;\n if constexpr (Index == 1) return val;\n }\n};\n\nnamespace std {\n\ntemplate < class K, class V > struct tuple_size < key_val< K, V > > : integral_constant< size_t, 2 > {};\ntemplate < class K, class V > struct tuple_element < 0, key_val< K, V > > { using type = K; };\ntemplate < class K, class V > struct tuple_element < 1, key_val< K, V > > { using type = V; };\n\n}\n#line 2 \"cp-library/src/utility/vec_op.hpp\"\ntemplate < class T > key_val< int, T > max_of(const vector< T >& a) {\n int i = std::max_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class T > key_val< int, T > min_of(const vector< T >& a) {\n int i = std::min_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class S, class T > S sum_of(const vector< T >& a) {\n S sum = 0;\n for(const T x : a) sum += x;\n return sum;\n}\ntemplate < class S, class T > vector< S > freq_of(const vector< T >& a, T L, T R) {\n vector< S > res(R - L, S(0));\n for(const T x : a) res[x - L] += 1;\n return res;\n}\ntemplate < class S, class T > struct prefix_sum {\n vector< S > s;\n prefix_sum(const vector< T >& a) : s(a) {\n s.insert(s.begin(), S(0));\n for(int i : rep(a.size())) s[i + 1] += s[i];\n }\n // [L, R)\n S sum(int L, int R) { return s[R] - s[L]; }\n};\n#line 3 \"cp-library/src/utility/heap.hpp\"\n\ntemplate < class T > using heap_min = std::priority_queue< T, std::vector< T >, std::greater< T > >;\ntemplate < class T > using heap_max = std::priority_queue< T, std::vector< T >, std::less< T > >;\n\n#line 27 \"cp-library/src/cp-template.hpp\"\n\n#line 1 \"cp-library/src/algorithm/bin_search.hpp\"\ntemplate < class T, class F >\nT bin_search(T ok, T ng, F f) {\n while(abs(ng - ok) > 1) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n\ntemplate < class T, class F >\nT bin_search_real(T ok, T ng, F f, int step = 80) {\n while(step--) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n#line 2 \"cp-library/src/algorithm/argsort.hpp\"\n\ntemplate < class T > std::vector< int > argsort(const std::vector< T > &a) {\n std::vector< int > ids((int)a.size());\n std::iota(ids.begin(), ids.end(), 0);\n std::sort(ids.begin(), ids.end(), [&](int i, int j) {\n return a[i] < a[j] || (a[i] == a[j] && i < j);\n });\n return ids;\n}\n#line 1 \"macro.hpp\"\nnamespace macro {\n\nusing size_type = int;\ntemplate < class container > void sort(container& a) { std::sort(std:: begin(a), std:: end(a)); }\ntemplate < class container > void rsort(container& a) { std::sort(std::rbegin(a), std::rend(a)); }\ntemplate < class container > void reverse(container& a) { std::reverse(std::begin(a), std::end(a)); }\ntemplate < class container > void unique(container& a) {\n std::sort(std::begin(a), std::end(a));\n a.erase(std::unique(std::begin(a), std::end(a)), std::end(a));\n}\ntemplate < class container > container sorted(const container& a) { container b = a; sort(b); return std::move(b); }\ntemplate < class container > container rsorted(const container& a) { container b = a; rsort(b); return std::move(b); }\ntemplate < class container, class compare > void sort(container& a, const compare& cmp) { std::sort(std::begin(a), std::end(a), cmp); }\ntemplate < class container, class compare > container sorted(const container& a, const compare& cmp) { container b = a; sort(b, cmp); return std::move(b); }\ntemplate < class container, class value > size_type lower_bound(const container& a, const value& x) { return std::lower_bound(std::begin(a), std::end(a), x) - std::begin(a); }\ntemplate < class container, class value > size_type upper_bound(const container& a, const value& x) { return std::upper_bound(std::begin(a), std::end(a), x) - std::begin(a); }\n\nconst std::vector<std::pair<size_type, size_type>> dir4 = { {+1, 0}, {-1, 0}, { 0, +1}, { 0, -1} };\nconst std::vector<std::pair<size_type, size_type>> dir8 = { {-1, -1}, {-1, 0}, {-1, +1}, { 0, -1}, { 0, +1}, {+1, -1}, {+1, 0}, {+1, +1} };\n\n#ifdef _DEBUG\n#define debug(x) std::cout << \"[\" << __LINE__ << \"] \" << #x << \": \" << x << std::endl\n#else\n#define debug(x)\n#endif\n\ntemplate < class container > void concat(container& a, const container& b) {\n a.insert(std::end(a), std::begin(b), std::end(b));\n}\nstd::vector<size_type> iota(const size_type n) {\n std::vector<size_type> I(n);\n std::iota(std::begin(I), std::end(I), 0);\n return I;\n}\ntemplate < class container > std::vector<size_type> sort_idx(const container& a) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return a[i] < a[j] or (a[i] == a[j] and i < j); });\n return I;\n}\ntemplate < class container, class compare > std::vector<size_type> sort_idx(const container& a, const compare& cmp) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return cmp(a[i], a[j]) or (a[i] == a[j] and i < j); });\n return std::move(I);\n}\n\nstruct grid {\n using size_type = int;\n size_type H, W;\n grid(const size_type H, const size_type W) : H(H), W(W) {}\n bool contains(const size_type i, const size_type j) {\n return 0 <= i and i < H and 0 <= j and j < W;\n }\n};\n\n} // namespace macro\n\nusing namespace macro;\n#line 3 \"A.cpp\"\nusing f64 = long double;\n\nint main() {\n int n = in(); f64 r = in(), theta = in(); theta = theta / 180.0 * M_PI;\n vector<pair<f64,f64>> p(n);\n for(auto &[x, y] : p) x = in(), y = in();\n\n vector dist(n, vector(n, f64(0)));\n for(int i : rep(n)) for(int j : rep(n)) if(j != i) {\n auto [xi, yi] = p[i];\n auto [xj, yj] = p[j];\n dist[i][j] = sqrt((xi - xj) * (xi - xj) + (yi - yj) * (yi - yj));\n }\n\n vector can(n, vector(n, vector(n, 0)));\n for(int i : rep(n)) {\n for(int j : rep(n)) if(j != i) {\n for(int k : rep(n)) if(k != i and k != j) {\n // i -> j -> k\n pair<f64,f64> vij = {p[j].first - p[i].first, p[j].second - p[i].second};\n pair<f64,f64> vjk = {p[k].first - p[j].first, p[k].second - p[j].second};\n f64 dot = vij.first * vjk.first + vij.second * vjk.second;\n f64 lij = sqrt(vij.first * vij.first + vij.second * vij.second);\n f64 ljk = sqrt(vjk.first * vjk.first + vjk.second * vjk.second);\n can[i][j][k] = (acos(dot / (lij * ljk)) <= theta);\n }\n }\n }\n\n const int max_ans = 10101;\n vector dp(max_ans + 1, vector(n, vector(n, f64(1e9))));\n for(int i : rep(n)) dp[1][0][i] = dist[0][i];\n for(int a = 2; a <= max_ans; a++) {\n for(int i : rep(n)) {\n for(int j : rep(n)) if(j != i) {\n for(int k : rep(n)) if(k != i and k != j) {\n if(can[i][j][k]) {\n chmin(dp[a][j][k], dp[a - 1][i][j] + dist[j][k]);\n }\n }\n }\n }\n }\n\n int ans = 0;\n for(int a = 1; a <= max_ans; a++) {\n for(int i : rep(n)) for(int j : rep(n)) if(j != i) {\n if(dp[a][i][j] <= r) {\n chmax(ans, a);\n }\n }\n }\n print(ans);\n}", "accuracy": 1, "time_ms": 260, "memory_kb": 74752, "score_of_the_acc": -1.26, "final_rank": 9 }, { "submission_id": "aoj_2233_7883998", "code_snippet": "#include <bits/stdc++.h>\n#define rep(i, n) for (int i = 0; i < n; ++i)\ntypedef long long ll;\nusing namespace std;\n\nint main() {\n cin.tie(0)->sync_with_stdio(0);\n\n int N;\n double r, theta;\n cin >> N >> r >> theta;\n vector<complex<double>> P(N);\n rep(i, N) {\n int x, y;\n cin >> x >> y;\n P[i] = complex<double>(x, y);\n }\n double MAX_RAD = theta * acos(-1) / 180.0;\n\n vector can_go(N, vector(N, vector<bool>(N)));\n rep(i, N) rep(j, N) rep(k, N) {\n if (i == j || j == k || k == i) continue;\n\n double rad = abs(arg((P[k] - P[j]) / (P[j] - P[i])));\n can_go[i][j][k] = rad <= MAX_RAD;\n }\n\n vector dist(N, vector(N, 1e10));\n rep(i, N) rep(j, N) {\n if (i == j) continue;\n dist[i][j] = abs(P[i] - P[j]);\n }\n\n int m = int(r) + 1;\n\n int ans = 0;\n vector dp(N, vector<double>(N, 1e10));\n for (int i = 1; i < N; ++i) dp[0][i] = dist[0][i];\n\n for (int i = 1; i < m; ++i) {\n vector ep(N, vector<double>(N, 1e10));\n\n // j->k から k->l への移動\n rep(j, N) rep(k, N) rep(l, N) {\n if (dp[j][k] >= r) continue;\n if (j == k || k == l) continue;\n\n ans = i;\n if (can_go[j][k][l]) ep[k][l] = min(ep[k][l], dp[j][k] + dist[k][l]);\n }\n dp = ep;\n }\n cout << ans << \"\\n\";\n\n return 0;\n}", "accuracy": 1, "time_ms": 90, "memory_kb": 3948, "score_of_the_acc": -0.0939, "final_rank": 3 }, { "submission_id": "aoj_2233_7883983", "code_snippet": "#include <bits/stdc++.h>\n#define rep(i, n) for (int i = 0; i < n; ++i)\ntypedef long long ll;\nusing namespace std;\n\nint main() {\n cin.tie(0)->sync_with_stdio(0);\n\n int N;\n double r, theta;\n cin >> N >> r >> theta;\n vector<complex<double>> P(N);\n rep(i, N) {\n int x, y;\n cin >> x >> y;\n P[i] = complex<double>(x, y);\n }\n double MAX_RAD = theta * acos(-1) / 180.0;\n\n vector can_go(N, vector(N, vector<bool>(N)));\n rep(i, N) rep(j, N) rep(k, N) {\n if (i == j || j == k || k == i) continue;\n\n double rad = abs(arg((P[k] - P[j]) / (P[j] - P[i])));\n can_go[i][j][k] = rad <= MAX_RAD;\n }\n\n vector dist(N, vector(N, 1e10));\n rep(i, N) rep(j, N) {\n if (i == j) continue;\n dist[i][j] = abs(P[i] - P[j]);\n }\n\n int m = int(r) + 1;\n\n vector dp(N, vector(N, vector<double>(m, 1e10)));\n dp[0][0][0] = 0;\n for (int i = 1; i < N; ++i) dp[0][i][1] = dist[0][i];\n\n for (int i = 1; i < m; ++i) {\n // j->k から k->l への移動\n rep(j, N) rep(k, N) rep(l, N) {\n if (dp[j][k][i] >= r) continue;\n if (j == k || k == l) continue;\n\n if (can_go[j][k][l]) dp[k][l][i + 1] = min(dp[k][l][i + 1], dp[j][k][i] + dist[k][l]);\n }\n }\n\n for (int i = m - 1; i >= 0; --i) {\n rep(j, N) rep(k, N) {\n if (dp[j][k][i] <= r) {\n cout << i << \"\\n\";\n return 0;\n }\n }\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 90, "memory_kb": 36340, "score_of_the_acc": -0.54, "final_rank": 5 }, { "submission_id": "aoj_2233_6175370", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef pair<int, double> pid;\nconst int N = 30;\nint n;\ndouble r, theta;\nint X[N], Y[N];\ndouble dp[N][N][10100];\n\nstruct HQ{\n double d;\n int pos;\n int bpos;\n int c;\n \n bool operator<(const HQ &right) const{\n return d > right.d;\n }\n};\n\nbool ok(int i, int j, int k){\n int A[2] = {X[j] - X[i], Y[j] - Y[i]};\n int B[2] = {X[k] - X[j], Y[k] - Y[j]};\n int ab = A[0] * B[0] + A[1] * B[1];\n if(ab == 0) return 90 <= theta;\n double a = sqrt(A[0] * A[0] + A[1] * A[1]);\n double b = sqrt(B[0] * B[0] + B[1] * B[1]);\n double cos_ = ab / (a * b);\n double phi = acos(cos_) * 180.0 / M_PI;\n \n return phi < theta;\n}\n\ndouble dist(int i, int j){\n int dx = X[j] - X[i];\n int dy = Y[j] - Y[i];\n return sqrt(dx * dx + dy * dy);\n}\n\nint main(void){\n cout << fixed << setprecision(12);\n cin >> n >> r >> theta;\n for(int i = 0; i < n; i++) cin >> X[i] >> Y[i];\n vector<vector<vector<pid>>> edges(n, vector<vector<pid>>(n, vector<pid>()));\n \n for(int i = 0; i < n; i++){\n for(int j = 0; j < n; j++){\n if(i == j) continue;\n for(int k = 0; k < n; k++){\n if(i == k || j == k) continue;\n if(ok(i, j, k)){\n edges[i][j].push_back({k, dist(j, k)});\n }\n }\n }\n }\n double inf = r + 1;\n for(int i = 0; i < n; i++) for(int j = 0; j < n; j++){\n for(int k = 0; k < 10010; k++) dp[i][j][k] = inf;\n }\n int ans = 0;\n \n priority_queue<HQ> hq;\n for(int j = 1; j < n; j++){\n double d = dist(0, j);\n if(d < r){\n dp[0][j][1] = d;\n ans = 1;\n hq.push({d, j, 0, 1});\n }\n }\n int pos, bpos, npos, c;\n double d, d2, dd;\n while(!hq.empty()){\n auto tmp = hq.top();\n d = tmp.d;\n pos = tmp.pos;\n bpos = tmp.bpos;\n c = tmp.c;\n hq.pop();\n for(auto tmp2: edges[bpos][pos]){\n npos = tmp2.first;\n d2 = tmp2.second;\n dd = d + d2;\n if(dd > r) continue;\n if(dd < dp[pos][npos][c + 1]){\n dp[pos][npos][c + 1] = dd;\n ans = max(ans, c + 1);\n hq.push({dd, npos, pos, c + 1});\n }\n }\n \n }\n cout << ans << \"\\n\";\n}", "accuracy": 1, "time_ms": 110, "memory_kb": 50840, "score_of_the_acc": -0.7621, "final_rank": 7 }, { "submission_id": "aoj_2233_6014825", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\nusing ld = long double;\n\nconst ll INF = 1ll << 60;\nconst ld PI = 3.1415926535897932;\nconst ld EPS = 1e-9;\n\ntemplate <class T, class U>\nusing P = pair<T, U>;\ntemplate <class T>\nusing V2 = vector<vector<T>>;\ntemplate <class T>\nusing V3 = vector<vector<vector<T>>>;\n\nld calc(ld x1, ld y1, ld x2, ld y2) {\n ld ret = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));\n return ret;\n}\n\nld kaku(ld x1, ld y1, ld x2, ld y2) {\n // 弧度法で返す\n if (abs(x1 - x2) < EPS) {\n if (y2 - y1 > 0)\n return PI / 2;\n else\n return -PI / 2;\n }\n return atan2(y2 - y1, x2 - x1);\n}\n\nint main() {\n ll n;\n cin >> n;\n ld r, theta;\n cin >> r >> theta;\n theta = theta * PI / 180;\n vector<ld> x(n + 1), y(n + 1);\n for (int i = 1; i <= n; i++) cin >> x[i] >> y[i];\n V3<P<int, ld>> g(n + 1, V2<P<int, ld>>(n + 1));\n for (int i = 1; i <= n; i++) {\n for (int j = 0; j <= n; j++) {\n if (i == j) continue;\n for (int k = 1; k <= n; k++) {\n if (j == 0) {\n if (i != k) g[i][j].push_back({k, calc(x[i], y[i], x[k], y[k])});\n } else {\n ld alpha = kaku(x[j], y[j], x[i], y[i]);\n ld beta = kaku(x[i], y[i], x[k], y[k]);\n ld ganma = abs(alpha - beta);\n if (min(2 * PI - ganma, ganma) <= theta + EPS && i != k) {\n g[i][j].push_back({k, calc(x[i], y[i], x[k], y[k])});\n }\n }\n }\n }\n }\n\n int R = (int)r + 5;\n ll V = (n + 1) * (n + 1) * R;\n V3<ld> dp(n + 1, V2<ld>(n + 1, vector<ld>(R, INF)));\n dp[1][0][0] = 0;\n priority_queue<P<ld, int>, vector<P<ld, int>>, greater<P<ld, int>>> pq;\n pq.push({0, (1 * (n + 1) + 0) * R + 0});\n while (!pq.empty()) {\n ld d = pq.top().first;\n ll v = pq.top().second;\n pq.pop();\n int ij = v / R, k = v % R;\n int i = ij / (n + 1), j = ij % (n + 1);\n if (dp[i][j][k] < d) continue;\n for (auto &e : g[i][j]) {\n if (dp[e.first][i][k + 1] > dp[i][j][k] + e.second) {\n dp[e.first][i][k + 1] = dp[i][j][k] + e.second;\n if (dp[e.first][i][k + 1] <= r) pq.push({dp[e.first][i][k + 1], (e.first * (n + 1) + i) * R + k + 1});\n }\n }\n }\n int ans = 0;\n for (int i = 0; i <= n; i++) {\n for (int j = 0; j <= n; j++) {\n for (int k = 0; k < R; k++) {\n if (dp[i][j][k] <= r) ans = max(ans, k);\n }\n }\n }\n cout << ans << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 270, "memory_kb": 75828, "score_of_the_acc": -1.286, "final_rank": 10 }, { "submission_id": "aoj_2233_6011261", "code_snippet": "// #include \"atcoder/all\"\n#include <iostream> // cout, endl, cin\n#include <string> // string, to_string, stoi\n#include <vector> // vector\n#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound\n#include <utility> // pair, make_pair\n#include <tuple> // tuple, make_tuple\n#include <cstdint> // int64_t, int*_t\n#include <cstdio> // printf\n#include <map> // map\n#include <queue> // queue, priority_queue\n#include <set> // set\n#include <stack> // stack\n#include <deque> // deque\n#include <unordered_map> // unordered_map\n#include <unordered_set> // unordered_set\n#include <bitset> // bitset\n#include <cctype> // isupper, islower, isdigit, toupper, tolower\n#include <iomanip> // setprecision\n#include <complex> // complex\n#include <math.h>\n#include <functional>\n#include <cassert>\nusing namespace std;\n// using namespace atcoder;\nusing ll = long long;\nusing P = pair<ll,ll>;\nconstexpr ll INF = 1e18;\nconstexpr ll LLMAX = 9223372036854775807;\nconstexpr int inf = 1e9;\nconstexpr ll mod = 1000000007;\n// constexpr ll mod = 998244353;\n// 右下左上\nconst int dx[8] = {1, 0, -1, 0,1,1,-1,-1};\nconst int dy[8] = {0, 1, 0, -1,1,-1,1,-1};\ntemplate<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }\ntemplate<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }\n#define eol endl\n// ---------------------------------------------------------------------------\n\nusing Real = double;\nusing Point = complex< Real >;\nconst Real EPS = 1e-8, PI = acos(-1);\n\ninline bool eq(Real a, Real b) { return fabs(b - a) < EPS; }\n\nPoint operator*(const Point &p, const Real &d) {\n return Point(real(p) * d, imag(p) * d);\n}\n\nistream &operator>>(istream &is, Point &p) {\n Real a, b;\n is >> a >> b;\n p = Point(a, b);\n return is;\n}\n\nostream &operator<<(ostream &os, Point &p) {\n return os << fixed << setprecision(10) << p.real() << \" \" << p.imag();\n}\n\n// rotate point p counterclockwise by theta rad\nPoint rotate(Real theta, const Point &p) {\n return Point(cos(theta) * p.real() - sin(theta) * p.imag(), sin(theta) * p.real() + cos(theta) * p.imag());\n}\n\nReal radian_to_degree(Real r) {\n return (r * 180.0 / PI);\n}\n\nReal degree_to_radian(Real d) {\n return (d * PI / 180.0);\n}\n\n// smaller angle of the a-b-c\nReal get_angle(const Point &a, const Point &b, const Point &c) {\n const Point v(b - a), w(c - b);\n Real alpha = atan2(v.imag(), v.real()), beta = atan2(w.imag(), w.real());\n if(alpha > beta) swap(alpha, beta);\n Real theta = (beta - alpha);\n return min(theta, 2 * acos(-1) - theta);\n}\n\nnamespace std {\n bool operator<(const Point &a, const Point &b) {\n return a.real() != b.real() ? a.real() < b.real() : a.imag() < b.imag();\n }\n}\n\n\nstruct Line {\n Point a, b;\n\n Line() = default;\n\n Line(Point a, Point b) : a(a), b(b) {}\n\n Line(Real A, Real B, Real C) // Ax + By = C\n {\n if(eq(A, 0)) a = Point(0, C / B), b = Point(1, C / B);\n else if(eq(B, 0)) b = Point(C / A, 0), b = Point(C / A, 1);\n else a = Point(0, C / B), b = Point(C / A, 0);\n }\n\n friend ostream &operator<<(ostream &os, Line &p) {\n return os << p.a << \" to \" << p.b;\n }\n\n friend istream &operator>>(istream &is, Line &a) {\n return is >> a.a >> a.b;\n }\n};\n\nstruct Segment : Line {\n Segment() = default;\n\n Segment(Point a, Point b) : Line(a, b) {}\n};\n\nstruct Circle {\n Point p;\n Real r;\n\n Circle() = default;\n\n Circle(Point p, Real r) : p(p), r(r) {}\n};\n\nusing Points = vector< Point >;\nusing Polygon = vector< Point >;\nusing Segments = vector< Segment >;\nusing Lines = vector< Line >;\nusing Circles = vector< Circle >;\n\nReal cross(const Point &a, const Point &b) {\n return real(a) * imag(b) - imag(a) * real(b);\n}\n\nReal dot(const Point &a, const Point &b) {\n return real(a) * real(b) + imag(a) * imag(b);\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_C\n// 点の進行方向\nint ccw(const Point &a, Point b, Point c) {\n b = b - a, c = c - a;\n if(cross(b, c) > EPS) return +1; // \"COUNTER_CLOCKWISE\"\n if(cross(b, c) < -EPS) return -1; // \"CLOCKWISE\"\n if(dot(b, c) < 0) return +2; // \"ONLINE_BACK\" c-a-b\n if(norm(b) < norm(c)) return -2; // \"ONLINE_FRONT\" a-b-c\n return 0; // \"ON_SEGMENT\" a-c-b\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_A\n// 直交判定\nbool parallel(const Line &a, const Line &b) {\n return eq(cross(a.b - a.a, b.b - b.a), 0.0);\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_A\n// 平行判定\nbool orthogonal(const Line &a, const Line &b) {\n return eq(dot(a.a - a.b, b.a - b.b), 0.0);\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_A\n// 射影(点pから直線lに垂線をおろしたときの交点)\nPoint projection(const Line &l, const Point &p) {\n double t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n return l.a + (l.a - l.b) * t;\n}\n\nPoint projection(const Segment &l, const Point &p) {\n double t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n return l.a + (l.a - l.b) * t;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_B\n// 反射(直線lを対称軸として点pと線対称の位置にある点)\nPoint reflection(const Line &l, const Point &p) {\n return p + (projection(l, p) - p) * 2.0;\n}\n\nbool intersect(const Line &l, const Point &p) {\n return abs(ccw(l.a, l.b, p)) != 1;\n}\n\nbool intersect(const Line &l, const Line &m) {\n return abs(cross(l.b - l.a, m.b - m.a)) > EPS || abs(cross(l.b - l.a, m.b - l.a)) < EPS;\n}\n\nbool intersect(const Segment &s, const Point &p) {\n return ccw(s.a, s.b, p) == 0;\n}\n\nbool intersect(const Line &l, const Segment &s) {\n return cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < EPS;\n}\n\nReal distance(const Line &l, const Point &p);\n\nbool intersect(const Circle &c, const Line &l) {\n return distance(l, c.p) <= c.r + EPS;\n}\n\nbool intersect(const Circle &c, const Point &p) {\n return abs(abs(p - c.p) - c.r) < EPS;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_B\nbool intersect(const Segment &s, const Segment &t) {\n return ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 && ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0;\n}\n\nint intersect(const Circle &c, const Segment &l) {\n if(norm(projection(l, c.p) - c.p) - c.r * c.r > EPS) return 0;\n auto d1 = abs(c.p - l.a), d2 = abs(c.p - l.b);\n if(d1 < c.r + EPS && d2 < c.r + EPS) return 0;\n if(d1 < c.r - EPS && d2 > c.r + EPS || d1 > c.r + EPS && d2 < c.r - EPS) return 1;\n const Point h = projection(l, c.p);\n if(dot(l.a - h, l.b - h) < 0) return 2;\n return 0;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_A&lang=jp\nint intersect(Circle c1, Circle c2) {\n if(c1.r < c2.r) swap(c1, c2);\n Real d = abs(c1.p - c2.p);\n if(c1.r + c2.r < d) return 4;\n if(eq(c1.r + c2.r, d)) return 3;\n if(c1.r - c2.r < d) return 2;\n if(eq(c1.r - c2.r, d)) return 1;\n return 0;\n}\n\nReal distance(const Point &a, const Point &b) {\n return abs(a - b);\n}\n\nReal distance(const Line &l, const Point &p) {\n return abs(p - projection(l, p));\n}\n\nReal distance(const Line &l, const Line &m) {\n return intersect(l, m) ? 0 : distance(l, m.a);\n}\n\nReal distance(const Segment &s, const Point &p) {\n Point r = projection(s, p);\n if(intersect(s, r)) return abs(r - p);\n return min(abs(s.a - p), abs(s.b - p));\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_D\nReal distance(const Segment &a, const Segment &b) {\n if(intersect(a, b)) return 0;\n return min({distance(a, b.a), distance(a, b.b), distance(b, a.a), distance(b, a.b)});\n}\n\nReal distance(const Line &l, const Segment &s) {\n if(intersect(l, s)) return 0;\n return min(distance(l, s.a), distance(l, s.b));\n}\n\nPoint crosspoint(const Line &l, const Line &m) {\n Real A = cross(l.b - l.a, m.b - m.a);\n Real B = cross(l.b - l.a, l.b - m.a);\n if(eq(abs(A), 0.0) && eq(abs(B), 0.0)) return m.a;\n return m.a + (m.b - m.a) * B / A;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_C\nPoint crosspoint(const Segment &l, const Segment &m) {\n return crosspoint(Line(l), Line(m));\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_D\npair< Point, Point > crosspoint(const Circle &c, const Line l) {\n Point pr = projection(l, c.p);\n Point e = (l.b - l.a) / abs(l.b - l.a);\n if(eq(distance(l, c.p), c.r)) return {pr, pr};\n double base = sqrt(c.r * c.r - norm(pr - c.p));\n return {pr - e * base, pr + e * base};\n}\n\npair< Point, Point > crosspoint(const Circle &c, const Segment &l) {\n Line aa = Line(l.a, l.b);\n if(intersect(c, l) == 2) return crosspoint(c, aa);\n auto ret = crosspoint(c, aa);\n if(dot(l.a - ret.first, l.b - ret.first) < 0) ret.second = ret.first;\n else ret.first = ret.second;\n return ret;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_E\npair< Point, Point > crosspoint(const Circle &c1, const Circle &c2) {\n Real d = abs(c1.p - c2.p);\n Real a = acos((c1.r * c1.r + d * d - c2.r * c2.r) / (2 * c1.r * d));\n Real t = atan2(c2.p.imag() - c1.p.imag(), c2.p.real() - c1.p.real());\n Point p1 = c1.p + Point(cos(t + a) * c1.r, sin(t + a) * c1.r);\n Point p2 = c1.p + Point(cos(t - a) * c1.r, sin(t - a) * c1.r);\n return {p1, p2};\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_F\n// tangent of circle c through point p\npair< Point, Point > tangent(const Circle &c1, const Point &p2) {\n return crosspoint(c1, Circle(p2, sqrt(norm(c1.p - p2) - c1.r * c1.r)));\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_G\n// common tangent of circles c1 and c2\nLines tangent(Circle c1, Circle c2) {\n Lines ret;\n if(c1.r < c2.r) swap(c1, c2);\n Real g = norm(c1.p - c2.p);\n if(eq(g, 0)) return ret;\n Point u = (c2.p - c1.p) / sqrt(g);\n Point v = rotate(PI * 0.5, u);\n for(int s : {-1, 1}) {\n Real h = (c1.r + s * c2.r) / sqrt(g);\n if(eq(1 - h * h, 0)) {\n ret.emplace_back(c1.p + u * c1.r, c1.p + (u + v) * c1.r);\n } else if(1 - h * h > 0) {\n Point uu = u * h, vv = v * sqrt(1 - h * h);\n ret.emplace_back(c1.p + (uu + vv) * c1.r, c2.p - (uu + vv) * c2.r * s);\n ret.emplace_back(c1.p + (uu - vv) * c1.r, c2.p - (uu - vv) * c2.r * s);\n }\n }\n return ret;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_3_B\n// 凸多角形判定\nbool is_convex(const Polygon &p) {\n int n = (int) p.size();\n for(int i = 0; i < n; i++) {\n if(ccw(p[(i + n - 1) % n], p[i], p[(i + 1) % n]) == -1) return false;\n }\n return true;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_4_A\n// 凸包\nPolygon convex_hull(Polygon &p) {\n int n = (int) p.size(), k = 0;\n if(n <= 2) return p;\n sort(p.begin(), p.end());\n vector< Point > ch(2 * n);\n for(int i = 0; i < n; ch[k++] = p[i++]) {\n while(k >= 2 && cross(ch[k - 1] - ch[k - 2], p[i] - ch[k - 1]) < EPS) --k;\n }\n for(int i = n - 2, t = k + 1; i >= 0; ch[k++] = p[i--]) {\n while(k >= t && cross(ch[k - 1] - ch[k - 2], p[i] - ch[k - 1]) < EPS) --k;\n }\n ch.resize(k - 1);\n return ch;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_3_C\n// 点-多角形包含判定\nenum {\n OUT, ON, IN\n};\n\nint contains(const Polygon &Q, const Point &p) {\n bool in = false;\n for(int i = 0; i < Q.size(); i++) {\n Point a = Q[i] - p, b = Q[(i + 1) % Q.size()] - p;\n if(a.imag() > b.imag()) swap(a, b);\n if(a.imag() <= 0 && 0 < b.imag() && cross(a, b) < 0) in = !in;\n if(cross(a, b) == 0 && dot(a, b) <= 0) return ON;\n }\n return in ? IN : OUT;\n}\n\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1033\n// deduplication of line segments\nvoid merge_segments(vector< Segment > &segs) {\n\n auto merge_if_able = [](Segment &s1, const Segment &s2) {\n if(abs(cross(s1.b - s1.a, s2.b - s2.a)) > EPS) return false;\n if(ccw(s1.a, s2.a, s1.b) == 1 || ccw(s1.a, s2.a, s1.b) == -1) return false;\n if(ccw(s1.a, s1.b, s2.a) == -2 || ccw(s2.a, s2.b, s1.a) == -2) return false;\n s1 = Segment(min(s1.a, s2.a), max(s1.b, s2.b));\n return true;\n };\n\n for(int i = 0; i < segs.size(); i++) {\n if(segs[i].b < segs[i].a) swap(segs[i].a, segs[i].b);\n }\n for(int i = 0; i < segs.size(); i++) {\n for(int j = i + 1; j < segs.size(); j++) {\n if(merge_if_able(segs[i], segs[j])) {\n segs[j--] = segs.back(), segs.pop_back();\n }\n }\n }\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1033\n// construct a graph with the vertex of the intersection of any two line segments\nvector< vector< int > > segment_arrangement(vector< Segment > &segs, vector< Point > &ps) {\n vector< vector< int > > g;\n int N = (int) segs.size();\n for(int i = 0; i < N; i++) {\n ps.emplace_back(segs[i].a);\n ps.emplace_back(segs[i].b);\n for(int j = i + 1; j < N; j++) {\n const Point p1 = segs[i].b - segs[i].a;\n const Point p2 = segs[j].b - segs[j].a;\n if(cross(p1, p2) == 0) continue;\n if(intersect(segs[i], segs[j])) {\n ps.emplace_back(crosspoint(segs[i], segs[j]));\n }\n }\n }\n sort(begin(ps), end(ps));\n ps.erase(unique(begin(ps), end(ps)), end(ps));\n\n int M = (int) ps.size();\n g.resize(M);\n for(int i = 0; i < N; i++) {\n vector< int > vec;\n for(int j = 0; j < M; j++) {\n if(intersect(segs[i], ps[j])) {\n vec.emplace_back(j);\n }\n }\n for(int j = 1; j < vec.size(); j++) {\n g[vec[j - 1]].push_back(vec[j]);\n g[vec[j]].push_back(vec[j - 1]);\n }\n }\n return (g);\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_4_C\n// cut with a straight line l and return a convex polygon on the left\nPolygon convex_cut(const Polygon &U, Line l) {\n Polygon ret;\n for(int i = 0; i < U.size(); i++) {\n Point now = U[i], nxt = U[(i + 1) % U.size()];\n if(ccw(l.a, l.b, now) != -1) ret.push_back(now);\n if(ccw(l.a, l.b, now) * ccw(l.a, l.b, nxt) < 0) {\n ret.push_back(crosspoint(Line(now, nxt), l));\n }\n }\n return (ret);\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_3_A\nReal area(const Polygon &p) {\n Real A = 0;\n for(int i = 0; i < p.size(); ++i) {\n A += cross(p[i], p[(i + 1) % p.size()]);\n }\n return A * 0.5;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_H\nReal area(const Polygon &p, const Circle &c) {\n if(p.size() < 3) return 0.0;\n function< Real(Circle, Point, Point) > cross_area = [&](const Circle &c, const Point &a, const Point &b) {\n Point va = c.p - a, vb = c.p - b;\n Real f = cross(va, vb), ret = 0.0;\n if(eq(f, 0.0)) return ret;\n if(max(abs(va), abs(vb)) < c.r + EPS) return f;\n if(distance(Segment(a, b), c.p) > c.r - EPS) return c.r * c.r * arg(vb * conj(va));\n auto u = crosspoint(c, Segment(a, b));\n vector< Point > tot{a, u.first, u.second, b};\n for(int i = 0; i + 1 < tot.size(); i++) {\n ret += cross_area(c, tot[i], tot[i + 1]);\n }\n return ret;\n };\n Real A = 0;\n for(int i = 0; i < p.size(); i++) {\n A += cross_area(c, p[i], p[(i + 1) % p.size()]);\n }\n return A;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_4_B\nReal convex_diameter(const Polygon &p) {\n int N = (int) p.size();\n int is = 0, js = 0;\n for(int i = 1; i < N; i++) {\n if(p[i].imag() > p[is].imag()) is = i;\n if(p[i].imag() < p[js].imag()) js = i;\n }\n Real maxdis = norm(p[is] - p[js]);\n\n int maxi, maxj, i, j;\n i = maxi = is;\n j = maxj = js;\n do {\n if(cross(p[(i + 1) % N] - p[i], p[(j + 1) % N] - p[j]) >= 0) {\n j = (j + 1) % N;\n } else {\n i = (i + 1) % N;\n }\n if(norm(p[i] - p[j]) > maxdis) {\n maxdis = norm(p[i] - p[j]);\n maxi = i;\n maxj = j;\n }\n } while(i != is || j != js);\n return sqrt(maxdis);\n}\n\n\nint main(){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n int N;\n cin >> N;\n double r,theta;\n cin >> r >> theta;\n vector<int> X(N),Y(N);\n for(int i=0; i<N; i++){\n cin >> X[i] >> Y[i];\n }\n // cout << radian_to_degree(get_angle(Point(X[0],Y[0]),Point(X[1],Y[1]),Point(X[2],Y[2]))) << endl;\n // cout << (acos(-1)-degree_to_radian(theta)) << endl;\n map<P,vector<ll>> G;\n auto check = [&](int a,int b,int c){\n double d = get_angle(Point(X[a],Y[a]),Point(X[b],Y[b]),Point(X[c],Y[c]));\n return (d <= degree_to_radian(theta));\n };\n for(int i=0; i<N; i++){\n for(int j=0; j<N; j++){\n for(int k=0; k<N; k++){\n if(i==j || j==k || k==i) continue;\n if(check(i,j,k)){\n G[P(i,j)].emplace_back(k);\n }\n }\n }\n }\n vector<vector<vector<double>>> dp(N,vector<vector<double>>(N,vector<double>(r+1,INF)));\n using tup = tuple<double,ll,ll,ll>;\n priority_queue<tup,vector<tup>,greater<>> que;\n for(int i=1; i<N; i++){\n dp[0][i][1] = distance(Point(X[0],Y[0]),Point(X[i],Y[i]));\n que.emplace(dp[0][i][1],0,i,1);\n }\n ll ans = 0;\n // for(auto m: G){\n // cout << m.first.first << \" \" << m.first.second << endl;\n // for(int j=0; j<m.second.size(); j++){\n // cout << m.second[j] << \" \";\n // }\n // cout << endl;\n // }\n while(que.size()){\n double cost;\n ll pv,v,num;\n tie(cost,pv,v,num) = que.top();\n // cout << pv << \" \" << v << \" \" << num << \" \" << cost << endl;\n que.pop();\n if(dp[pv][v][num] < cost) continue;\n if(cost <= r){\n chmax(ans,num);\n }\n for(int nv: G[P(pv,v)]){\n double d = distance(Point(X[v],Y[v]),Point(X[nv],Y[nv]));\n if(cost + d > r) continue;\n if(chmin(dp[v][nv][num+1],cost + d)){\n que.emplace(cost+d,v,nv,num+1);\n }\n }\n }\n cout << ans << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 320, "memory_kb": 36464, "score_of_the_acc": -0.8001, "final_rank": 8 }, { "submission_id": "aoj_2233_6011234", "code_snippet": "// #include \"atcoder/all\"\n#include <iostream> // cout, endl, cin\n#include <string> // string, to_string, stoi\n#include <vector> // vector\n#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound\n#include <utility> // pair, make_pair\n#include <tuple> // tuple, make_tuple\n#include <cstdint> // int64_t, int*_t\n#include <cstdio> // printf\n#include <map> // map\n#include <queue> // queue, priority_queue\n#include <set> // set\n#include <stack> // stack\n#include <deque> // deque\n#include <unordered_map> // unordered_map\n#include <unordered_set> // unordered_set\n#include <bitset> // bitset\n#include <cctype> // isupper, islower, isdigit, toupper, tolower\n#include <iomanip> // setprecision\n#include <complex> // complex\n#include <math.h>\n#include <functional>\n#include <cassert>\nusing namespace std;\n// using namespace atcoder;\nusing ll = long long;\nusing P = pair<ll,ll>;\nconstexpr ll INF = 1e18;\nconstexpr ll LLMAX = 9223372036854775807;\nconstexpr int inf = 1e9;\nconstexpr ll mod = 1000000007;\n// constexpr ll mod = 998244353;\n// 右下左上\nconst int dx[8] = {1, 0, -1, 0,1,1,-1,-1};\nconst int dy[8] = {0, 1, 0, -1,1,-1,1,-1};\ntemplate<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }\ntemplate<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }\n#define eol endl\n// ---------------------------------------------------------------------------\n\nusing Real = double;\nusing Point = complex< Real >;\nconst Real EPS = 1e-8, PI = acos(-1);\n\ninline bool eq(Real a, Real b) { return fabs(b - a) < EPS; }\n\nPoint operator*(const Point &p, const Real &d) {\n return Point(real(p) * d, imag(p) * d);\n}\n\nistream &operator>>(istream &is, Point &p) {\n Real a, b;\n is >> a >> b;\n p = Point(a, b);\n return is;\n}\n\nostream &operator<<(ostream &os, Point &p) {\n return os << fixed << setprecision(10) << p.real() << \" \" << p.imag();\n}\n\n// rotate point p counterclockwise by theta rad\nPoint rotate(Real theta, const Point &p) {\n return Point(cos(theta) * p.real() - sin(theta) * p.imag(), sin(theta) * p.real() + cos(theta) * p.imag());\n}\n\nReal radian_to_degree(Real r) {\n return (r * 180.0 / PI);\n}\n\nReal degree_to_radian(Real d) {\n return (d * PI / 180.0);\n}\n\n// smaller angle of the a-b-c\nReal get_angle(const Point &a, const Point &b, const Point &c) {\n const Point v(b - a), w(c - b);\n Real alpha = atan2(v.imag(), v.real()), beta = atan2(w.imag(), w.real());\n if(alpha > beta) swap(alpha, beta);\n Real theta = (beta - alpha);\n return min(theta, 2 * acos(-1) - theta);\n}\n\nnamespace std {\n bool operator<(const Point &a, const Point &b) {\n return a.real() != b.real() ? a.real() < b.real() : a.imag() < b.imag();\n }\n}\n\n\nstruct Line {\n Point a, b;\n\n Line() = default;\n\n Line(Point a, Point b) : a(a), b(b) {}\n\n Line(Real A, Real B, Real C) // Ax + By = C\n {\n if(eq(A, 0)) a = Point(0, C / B), b = Point(1, C / B);\n else if(eq(B, 0)) b = Point(C / A, 0), b = Point(C / A, 1);\n else a = Point(0, C / B), b = Point(C / A, 0);\n }\n\n friend ostream &operator<<(ostream &os, Line &p) {\n return os << p.a << \" to \" << p.b;\n }\n\n friend istream &operator>>(istream &is, Line &a) {\n return is >> a.a >> a.b;\n }\n};\n\nstruct Segment : Line {\n Segment() = default;\n\n Segment(Point a, Point b) : Line(a, b) {}\n};\n\nstruct Circle {\n Point p;\n Real r;\n\n Circle() = default;\n\n Circle(Point p, Real r) : p(p), r(r) {}\n};\n\nusing Points = vector< Point >;\nusing Polygon = vector< Point >;\nusing Segments = vector< Segment >;\nusing Lines = vector< Line >;\nusing Circles = vector< Circle >;\n\nReal cross(const Point &a, const Point &b) {\n return real(a) * imag(b) - imag(a) * real(b);\n}\n\nReal dot(const Point &a, const Point &b) {\n return real(a) * real(b) + imag(a) * imag(b);\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_C\n// 点の進行方向\nint ccw(const Point &a, Point b, Point c) {\n b = b - a, c = c - a;\n if(cross(b, c) > EPS) return +1; // \"COUNTER_CLOCKWISE\"\n if(cross(b, c) < -EPS) return -1; // \"CLOCKWISE\"\n if(dot(b, c) < 0) return +2; // \"ONLINE_BACK\" c-a-b\n if(norm(b) < norm(c)) return -2; // \"ONLINE_FRONT\" a-b-c\n return 0; // \"ON_SEGMENT\" a-c-b\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_A\n// 直交判定\nbool parallel(const Line &a, const Line &b) {\n return eq(cross(a.b - a.a, b.b - b.a), 0.0);\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_A\n// 平行判定\nbool orthogonal(const Line &a, const Line &b) {\n return eq(dot(a.a - a.b, b.a - b.b), 0.0);\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_A\n// 射影(点pから直線lに垂線をおろしたときの交点)\nPoint projection(const Line &l, const Point &p) {\n double t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n return l.a + (l.a - l.b) * t;\n}\n\nPoint projection(const Segment &l, const Point &p) {\n double t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n return l.a + (l.a - l.b) * t;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_B\n// 反射(直線lを対称軸として点pと線対称の位置にある点)\nPoint reflection(const Line &l, const Point &p) {\n return p + (projection(l, p) - p) * 2.0;\n}\n\nbool intersect(const Line &l, const Point &p) {\n return abs(ccw(l.a, l.b, p)) != 1;\n}\n\nbool intersect(const Line &l, const Line &m) {\n return abs(cross(l.b - l.a, m.b - m.a)) > EPS || abs(cross(l.b - l.a, m.b - l.a)) < EPS;\n}\n\nbool intersect(const Segment &s, const Point &p) {\n return ccw(s.a, s.b, p) == 0;\n}\n\nbool intersect(const Line &l, const Segment &s) {\n return cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < EPS;\n}\n\nReal distance(const Line &l, const Point &p);\n\nbool intersect(const Circle &c, const Line &l) {\n return distance(l, c.p) <= c.r + EPS;\n}\n\nbool intersect(const Circle &c, const Point &p) {\n return abs(abs(p - c.p) - c.r) < EPS;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_B\nbool intersect(const Segment &s, const Segment &t) {\n return ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 && ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0;\n}\n\nint intersect(const Circle &c, const Segment &l) {\n if(norm(projection(l, c.p) - c.p) - c.r * c.r > EPS) return 0;\n auto d1 = abs(c.p - l.a), d2 = abs(c.p - l.b);\n if(d1 < c.r + EPS && d2 < c.r + EPS) return 0;\n if(d1 < c.r - EPS && d2 > c.r + EPS || d1 > c.r + EPS && d2 < c.r - EPS) return 1;\n const Point h = projection(l, c.p);\n if(dot(l.a - h, l.b - h) < 0) return 2;\n return 0;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_A&lang=jp\nint intersect(Circle c1, Circle c2) {\n if(c1.r < c2.r) swap(c1, c2);\n Real d = abs(c1.p - c2.p);\n if(c1.r + c2.r < d) return 4;\n if(eq(c1.r + c2.r, d)) return 3;\n if(c1.r - c2.r < d) return 2;\n if(eq(c1.r - c2.r, d)) return 1;\n return 0;\n}\n\nReal distance(const Point &a, const Point &b) {\n return abs(a - b);\n}\n\nReal distance(const Line &l, const Point &p) {\n return abs(p - projection(l, p));\n}\n\nReal distance(const Line &l, const Line &m) {\n return intersect(l, m) ? 0 : distance(l, m.a);\n}\n\nReal distance(const Segment &s, const Point &p) {\n Point r = projection(s, p);\n if(intersect(s, r)) return abs(r - p);\n return min(abs(s.a - p), abs(s.b - p));\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_D\nReal distance(const Segment &a, const Segment &b) {\n if(intersect(a, b)) return 0;\n return min({distance(a, b.a), distance(a, b.b), distance(b, a.a), distance(b, a.b)});\n}\n\nReal distance(const Line &l, const Segment &s) {\n if(intersect(l, s)) return 0;\n return min(distance(l, s.a), distance(l, s.b));\n}\n\nPoint crosspoint(const Line &l, const Line &m) {\n Real A = cross(l.b - l.a, m.b - m.a);\n Real B = cross(l.b - l.a, l.b - m.a);\n if(eq(abs(A), 0.0) && eq(abs(B), 0.0)) return m.a;\n return m.a + (m.b - m.a) * B / A;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_C\nPoint crosspoint(const Segment &l, const Segment &m) {\n return crosspoint(Line(l), Line(m));\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_D\npair< Point, Point > crosspoint(const Circle &c, const Line l) {\n Point pr = projection(l, c.p);\n Point e = (l.b - l.a) / abs(l.b - l.a);\n if(eq(distance(l, c.p), c.r)) return {pr, pr};\n double base = sqrt(c.r * c.r - norm(pr - c.p));\n return {pr - e * base, pr + e * base};\n}\n\npair< Point, Point > crosspoint(const Circle &c, const Segment &l) {\n Line aa = Line(l.a, l.b);\n if(intersect(c, l) == 2) return crosspoint(c, aa);\n auto ret = crosspoint(c, aa);\n if(dot(l.a - ret.first, l.b - ret.first) < 0) ret.second = ret.first;\n else ret.first = ret.second;\n return ret;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_E\npair< Point, Point > crosspoint(const Circle &c1, const Circle &c2) {\n Real d = abs(c1.p - c2.p);\n Real a = acos((c1.r * c1.r + d * d - c2.r * c2.r) / (2 * c1.r * d));\n Real t = atan2(c2.p.imag() - c1.p.imag(), c2.p.real() - c1.p.real());\n Point p1 = c1.p + Point(cos(t + a) * c1.r, sin(t + a) * c1.r);\n Point p2 = c1.p + Point(cos(t - a) * c1.r, sin(t - a) * c1.r);\n return {p1, p2};\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_F\n// tangent of circle c through point p\npair< Point, Point > tangent(const Circle &c1, const Point &p2) {\n return crosspoint(c1, Circle(p2, sqrt(norm(c1.p - p2) - c1.r * c1.r)));\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_G\n// common tangent of circles c1 and c2\nLines tangent(Circle c1, Circle c2) {\n Lines ret;\n if(c1.r < c2.r) swap(c1, c2);\n Real g = norm(c1.p - c2.p);\n if(eq(g, 0)) return ret;\n Point u = (c2.p - c1.p) / sqrt(g);\n Point v = rotate(PI * 0.5, u);\n for(int s : {-1, 1}) {\n Real h = (c1.r + s * c2.r) / sqrt(g);\n if(eq(1 - h * h, 0)) {\n ret.emplace_back(c1.p + u * c1.r, c1.p + (u + v) * c1.r);\n } else if(1 - h * h > 0) {\n Point uu = u * h, vv = v * sqrt(1 - h * h);\n ret.emplace_back(c1.p + (uu + vv) * c1.r, c2.p - (uu + vv) * c2.r * s);\n ret.emplace_back(c1.p + (uu - vv) * c1.r, c2.p - (uu - vv) * c2.r * s);\n }\n }\n return ret;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_3_B\n// 凸多角形判定\nbool is_convex(const Polygon &p) {\n int n = (int) p.size();\n for(int i = 0; i < n; i++) {\n if(ccw(p[(i + n - 1) % n], p[i], p[(i + 1) % n]) == -1) return false;\n }\n return true;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_4_A\n// 凸包\nPolygon convex_hull(Polygon &p) {\n int n = (int) p.size(), k = 0;\n if(n <= 2) return p;\n sort(p.begin(), p.end());\n vector< Point > ch(2 * n);\n for(int i = 0; i < n; ch[k++] = p[i++]) {\n while(k >= 2 && cross(ch[k - 1] - ch[k - 2], p[i] - ch[k - 1]) < EPS) --k;\n }\n for(int i = n - 2, t = k + 1; i >= 0; ch[k++] = p[i--]) {\n while(k >= t && cross(ch[k - 1] - ch[k - 2], p[i] - ch[k - 1]) < EPS) --k;\n }\n ch.resize(k - 1);\n return ch;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_3_C\n// 点-多角形包含判定\nenum {\n OUT, ON, IN\n};\n\nint contains(const Polygon &Q, const Point &p) {\n bool in = false;\n for(int i = 0; i < Q.size(); i++) {\n Point a = Q[i] - p, b = Q[(i + 1) % Q.size()] - p;\n if(a.imag() > b.imag()) swap(a, b);\n if(a.imag() <= 0 && 0 < b.imag() && cross(a, b) < 0) in = !in;\n if(cross(a, b) == 0 && dot(a, b) <= 0) return ON;\n }\n return in ? IN : OUT;\n}\n\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1033\n// deduplication of line segments\nvoid merge_segments(vector< Segment > &segs) {\n\n auto merge_if_able = [](Segment &s1, const Segment &s2) {\n if(abs(cross(s1.b - s1.a, s2.b - s2.a)) > EPS) return false;\n if(ccw(s1.a, s2.a, s1.b) == 1 || ccw(s1.a, s2.a, s1.b) == -1) return false;\n if(ccw(s1.a, s1.b, s2.a) == -2 || ccw(s2.a, s2.b, s1.a) == -2) return false;\n s1 = Segment(min(s1.a, s2.a), max(s1.b, s2.b));\n return true;\n };\n\n for(int i = 0; i < segs.size(); i++) {\n if(segs[i].b < segs[i].a) swap(segs[i].a, segs[i].b);\n }\n for(int i = 0; i < segs.size(); i++) {\n for(int j = i + 1; j < segs.size(); j++) {\n if(merge_if_able(segs[i], segs[j])) {\n segs[j--] = segs.back(), segs.pop_back();\n }\n }\n }\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1033\n// construct a graph with the vertex of the intersection of any two line segments\nvector< vector< int > > segment_arrangement(vector< Segment > &segs, vector< Point > &ps) {\n vector< vector< int > > g;\n int N = (int) segs.size();\n for(int i = 0; i < N; i++) {\n ps.emplace_back(segs[i].a);\n ps.emplace_back(segs[i].b);\n for(int j = i + 1; j < N; j++) {\n const Point p1 = segs[i].b - segs[i].a;\n const Point p2 = segs[j].b - segs[j].a;\n if(cross(p1, p2) == 0) continue;\n if(intersect(segs[i], segs[j])) {\n ps.emplace_back(crosspoint(segs[i], segs[j]));\n }\n }\n }\n sort(begin(ps), end(ps));\n ps.erase(unique(begin(ps), end(ps)), end(ps));\n\n int M = (int) ps.size();\n g.resize(M);\n for(int i = 0; i < N; i++) {\n vector< int > vec;\n for(int j = 0; j < M; j++) {\n if(intersect(segs[i], ps[j])) {\n vec.emplace_back(j);\n }\n }\n for(int j = 1; j < vec.size(); j++) {\n g[vec[j - 1]].push_back(vec[j]);\n g[vec[j]].push_back(vec[j - 1]);\n }\n }\n return (g);\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_4_C\n// cut with a straight line l and return a convex polygon on the left\nPolygon convex_cut(const Polygon &U, Line l) {\n Polygon ret;\n for(int i = 0; i < U.size(); i++) {\n Point now = U[i], nxt = U[(i + 1) % U.size()];\n if(ccw(l.a, l.b, now) != -1) ret.push_back(now);\n if(ccw(l.a, l.b, now) * ccw(l.a, l.b, nxt) < 0) {\n ret.push_back(crosspoint(Line(now, nxt), l));\n }\n }\n return (ret);\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_3_A\nReal area(const Polygon &p) {\n Real A = 0;\n for(int i = 0; i < p.size(); ++i) {\n A += cross(p[i], p[(i + 1) % p.size()]);\n }\n return A * 0.5;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_H\nReal area(const Polygon &p, const Circle &c) {\n if(p.size() < 3) return 0.0;\n function< Real(Circle, Point, Point) > cross_area = [&](const Circle &c, const Point &a, const Point &b) {\n Point va = c.p - a, vb = c.p - b;\n Real f = cross(va, vb), ret = 0.0;\n if(eq(f, 0.0)) return ret;\n if(max(abs(va), abs(vb)) < c.r + EPS) return f;\n if(distance(Segment(a, b), c.p) > c.r - EPS) return c.r * c.r * arg(vb * conj(va));\n auto u = crosspoint(c, Segment(a, b));\n vector< Point > tot{a, u.first, u.second, b};\n for(int i = 0; i + 1 < tot.size(); i++) {\n ret += cross_area(c, tot[i], tot[i + 1]);\n }\n return ret;\n };\n Real A = 0;\n for(int i = 0; i < p.size(); i++) {\n A += cross_area(c, p[i], p[(i + 1) % p.size()]);\n }\n return A;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_4_B\nReal convex_diameter(const Polygon &p) {\n int N = (int) p.size();\n int is = 0, js = 0;\n for(int i = 1; i < N; i++) {\n if(p[i].imag() > p[is].imag()) is = i;\n if(p[i].imag() < p[js].imag()) js = i;\n }\n Real maxdis = norm(p[is] - p[js]);\n\n int maxi, maxj, i, j;\n i = maxi = is;\n j = maxj = js;\n do {\n if(cross(p[(i + 1) % N] - p[i], p[(j + 1) % N] - p[j]) >= 0) {\n j = (j + 1) % N;\n } else {\n i = (i + 1) % N;\n }\n if(norm(p[i] - p[j]) > maxdis) {\n maxdis = norm(p[i] - p[j]);\n maxi = i;\n maxj = j;\n }\n } while(i != is || j != js);\n return sqrt(maxdis);\n}\n\n\nint main(){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n int N;\n cin >> N;\n double r,theta;\n cin >> r >> theta;\n vector<int> X(N),Y(N);\n for(int i=0; i<N; i++){\n cin >> X[i] >> Y[i];\n }\n // cout << radian_to_degree(get_angle(Point(X[0],Y[0]),Point(X[1],Y[1]),Point(X[2],Y[2]))) << endl;\n // cout << (acos(-1)-degree_to_radian(theta)) << endl;\n map<P,vector<ll>> G;\n auto check = [&](int a,int b,int c){\n double d = get_angle(Point(X[a],Y[a]),Point(X[b],Y[b]),Point(X[c],Y[c]));\n d = min(d,acos(-1)-d);\n return (d >= acos(-1)-degree_to_radian(theta));\n };\n for(int i=0; i<N; i++){\n for(int j=0; j<N; j++){\n for(int k=0; k<N; k++){\n if(i==j || j==k || k==i) continue;\n if(check(i,j,k)){\n G[P(i,j)].emplace_back(k);\n }\n }\n }\n }\n vector<vector<vector<double>>> dp(N,vector<vector<double>>(N,vector<double>(10000*30,INF)));\n using tup = tuple<double,ll,ll,ll>;\n priority_queue<tup,vector<tup>,greater<>> que;\n for(int i=1; i<N; i++){\n dp[0][i][1] = distance(Point(X[0],Y[0]),Point(X[i],Y[i]));\n que.emplace(dp[0][i][1],0,i,1);\n }\n ll ans = 0;\n // for(auto m: G){\n // cout << m.first.first << \" \" << m.first.second << endl;\n // for(int j=0; j<m.second.size(); j++){\n // cout << m.second[j] << \" \";\n // }\n // cout << endl;\n // }\n while(que.size()){\n double cost;\n ll pv,v,num;\n tie(cost,pv,v,num) = que.top();\n // cout << pv << \" \" << v << \" \" << num << \" \" << cost << endl;\n que.pop();\n if(dp[pv][v][num] < cost) continue;\n if(cost <= r){\n chmax(ans,num);\n }\n for(int nv: G[P(pv,v)]){\n double d = distance(Point(X[v],Y[v]),Point(X[nv],Y[nv]));\n if(cost + d > r) continue;\n if(chmin(dp[v][nv][num+1],cost + d)){\n que.emplace(cost+d,v,nv,num+1);\n }\n }\n }\n cout << ans << endl;\n return 0;\n}", "accuracy": 0.15625, "time_ms": 10, "memory_kb": 76272, "score_of_the_acc": -1, "final_rank": 20 }, { "submission_id": "aoj_2233_6008565", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define eps 1e-6\n#define INF 2000000000 // 2e9\n#define LLINF 2000000000000000000ll // 2e18 (llmax:9e18)\n#define all(x) (x).begin(), (x).end()\n#define sq(x) ((x) * (x))\n\n#define rep(i, x) for (int i = 0; i < (int)(x); i++)\n#define drep(i, x) for (int i = ((int)(x)-1); i >= 0; i--)\n\n#define popcount __builtin_popcount\n#define next __next\n#define prev __prev\n\n#ifndef LOCAL\n#define dmp(...) ;\n#else\n#define dmp(...) \\\n cerr << \"[ \" << #__VA_ARGS__ << \" ] : \" << dump_str(__VA_ARGS__) << endl\n#endif\n\n// ---------------- Utility ------------------\n\ntemplate <class T>\nbool chmin(T &a, const T &b) {\n if (a <= b) return false;\n a = b;\n return true;\n}\n\ntemplate <class T>\nbool chmax(T &a, const T &b) {\n if (a >= b) return false;\n a = b;\n return true;\n}\n\ntemplate <class T>\nusing MaxHeap = priority_queue<T>;\n\ntemplate <class T>\nusing MinHeap = priority_queue<T, vector<T>, greater<T>>;\n\ntemplate <class T>\nvector<T> vect(int len, T elem) {\n return vector<T>(len, elem);\n}\n\n// ----------------- Input -------------------\n\ntemplate <class T, class U>\nistream &operator>>(istream &is, pair<T, U> &p) {\n return is >> p.first >> p.second;\n}\n\ntemplate <class T>\nistream &operator>>(istream &is, vector<T> &vec) {\n for (int i = 0; i < vec.size(); i++) is >> vec[i];\n return is;\n}\n\n// ----------------- Output ------------------\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n return os << p.first << ',' << p.second;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const vector<T> &v) {\n for (const T &e : v) os << e << \" \";\n return os;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const deque<T> &d) {\n for (const T &e : d) os << e << \" \";\n return os;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const set<T> &s) {\n os << \"{ \";\n for (const T &e : s) os << e << \" \";\n return os << \"}\";\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const map<T, U> &m) {\n os << \"{ \";\n for (const auto &[key, val] : m) os << \"( \" << key << \" -> \" << val << \" ) \";\n return os << \"}\";\n}\n\ntemplate <class TupleTy, size_t... I>\nvoid dump_tuple(ostream &os, const TupleTy t, std::index_sequence<I...>) {\n (..., (os << (I == 0 ? \"\" : \",\") << std::get<I>(t)));\n}\n\ntemplate <class... Args>\nostream &operator<<(ostream &os, const tuple<Args...> &t) {\n os << \"(\";\n dump_tuple(os, t, std::make_index_sequence<sizeof...(Args)>());\n return os << \")\";\n}\n\nvoid dump_str_rec(ostringstream &) {}\n\ntemplate <class Head, class... Tail>\nvoid dump_str_rec(ostringstream &oss, Head &&head, Tail &&... tail) {\n oss << \", \" << head;\n dump_str_rec(oss, forward<Tail>(tail)...);\n}\n\ntemplate <class T, class... U>\nstring dump_str(T &&arg, U &&... args) {\n ostringstream oss;\n oss << arg;\n dump_str_rec(oss, forward<U>(args)...);\n return oss.str();\n}\n\n// --------------- Fast I/O ------------------\n\nvoid fastio() {\n cin.tie(0);\n ios::sync_with_stdio(0);\n cout << fixed << setprecision(20);\n}\n\n// ------------------ ACL --------------------\n\n// #include <atcoder/modint>\n// constexpr int MOD = 998244353;\n// constexpr int MOD = 1000000007;\n// using mint = atcoder::static_modint<MOD>;\n// ostream &operator<<(ostream &os, const mint &v) {\n// return os << v.val();\n// }\n\n// ------------ End of template --------------\n\n#define endl \"\\n\"\nusing ll = long long;\nusing pii = pair<int, int>;\n\nconst bool multitest = false;\n\nvoid solve() {\n int n;\n cin >> n;\n double r, theta;\n cin >> r >> theta;\n vector<double> x(n), y(n);\n for (int i = 0; i < n; i++) { cin >> x[i] >> y[i]; }\n\n auto norm = [](double X, double Y) { return std::sqrt(sq(X) + sq(Y)); };\n\n auto dist = [&](int u, int v) -> double {\n return norm(x[u] - x[v], y[u] - y[v]);\n };\n\n auto arg = [&](int a, int b, int c) {\n double xab = x[b] - x[a], yab = y[b] - y[a];\n double xbc = x[c] - x[b], ybc = y[c] - y[b];\n return (xab * xbc + yab * ybc) / norm(xab, yab) / norm(xbc, ybc);\n // return std::acos((xab * xbc + yab * ybc) / norm(xab, yab) /\n // norm(xbc, ybc)) /\n // M_PI * 180.0;\n };\n\n double d_inf = 1e50;\n\n auto Arg = vect(n, vect(n, vect(n, d_inf)));\n auto Dist = vect(n, vect(n, 0.0));\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n Dist[i][j] = dist(i, j);\n for (int k = 0; k < n; k++) {\n if (i != j && j != k) Arg[i][j][k] = arg(i, j, k);\n }\n }\n }\n\n int M = std::ceil(r) + 10;\n\n auto dp = vect(n, vect(n, vect(M + 1, d_inf)));\n dp[0][0][0] = 0.0;\n for (int m = 0; m < M; m++) {\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n if (dp[i][j][m] == d_inf) continue;\n for (int k = 0; k < n; k++) {\n if (j == k) continue;\n if ((i != j) && (Arg[i][j][k] < std::cos(theta / 180 * M_PI)))\n continue;\n chmin(dp[j][k][m + 1], dp[i][j][m] + Dist[j][k]);\n }\n }\n }\n }\n\n int ans = 0;\n\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n for (int k = 0; k <= M; k++) {\n // dmp(i, j, k, dp[i][j][k]);\n if (dp[i][j][k] <= r) chmax(ans, k);\n }\n }\n }\n\n cout << ans << endl;\n return;\n}\n\nint main() {\n fastio();\n if (!multitest) {\n solve();\n } else {\n cerr << \"[Warning] Multi testcase mode on\" << endl;\n int t;\n cin >> t;\n while (t--) solve();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 900, "memory_kb": 36056, "score_of_the_acc": -1.4462, "final_rank": 11 }, { "submission_id": "aoj_2233_6008242", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define eps 1e-6\n#define INF 2000000000 // 2e9\n#define LLINF 2000000000000000000ll // 2e18 (llmax:9e18)\n#define all(x) (x).begin(), (x).end()\n#define sq(x) ((x) * (x))\n\n#define rep(i, x) for (int i = 0; i < (int)(x); i++)\n#define drep(i, x) for (int i = ((int)(x)-1); i >= 0; i--)\n\n#define popcount __builtin_popcount\n#define next __next\n#define prev __prev\n\n#ifndef LOCAL\n#define dmp(...) ;\n#else\n#define dmp(...) \\\n cerr << \"[ \" << #__VA_ARGS__ << \" ] : \" << dump_str(__VA_ARGS__) << endl\n#endif\n\n// ---------------- Utility ------------------\n\ntemplate <class T>\nbool chmin(T &a, const T &b) {\n if (a <= b) return false;\n a = b;\n return true;\n}\n\ntemplate <class T>\nbool chmax(T &a, const T &b) {\n if (a >= b) return false;\n a = b;\n return true;\n}\n\ntemplate <class T>\nusing MaxHeap = priority_queue<T>;\n\ntemplate <class T>\nusing MinHeap = priority_queue<T, vector<T>, greater<T>>;\n\ntemplate <class T>\nvector<T> vect(int len, T elem) {\n return vector<T>(len, elem);\n}\n\n// ----------------- Input -------------------\n\ntemplate <class T, class U>\nistream &operator>>(istream &is, pair<T, U> &p) {\n return is >> p.first >> p.second;\n}\n\ntemplate <class T>\nistream &operator>>(istream &is, vector<T> &vec) {\n for (int i = 0; i < vec.size(); i++) is >> vec[i];\n return is;\n}\n\n// ----------------- Output ------------------\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n return os << p.first << ',' << p.second;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const vector<T> &v) {\n for (const T &e : v) os << e << \" \";\n return os;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const deque<T> &d) {\n for (const T &e : d) os << e << \" \";\n return os;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const set<T> &s) {\n os << \"{ \";\n for (const T &e : s) os << e << \" \";\n return os << \"}\";\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const map<T, U> &m) {\n os << \"{ \";\n for (const auto &[key, val] : m) os << \"( \" << key << \" -> \" << val << \" ) \";\n return os << \"}\";\n}\n\ntemplate <class TupleTy, size_t... I>\nvoid dump_tuple(ostream &os, const TupleTy t, std::index_sequence<I...>) {\n (..., (os << (I == 0 ? \"\" : \",\") << std::get<I>(t)));\n}\n\ntemplate <class... Args>\nostream &operator<<(ostream &os, const tuple<Args...> &t) {\n os << \"(\";\n dump_tuple(os, t, std::make_index_sequence<sizeof...(Args)>());\n return os << \")\";\n}\n\nvoid dump_str_rec(ostringstream &) {}\n\ntemplate <class Head, class... Tail>\nvoid dump_str_rec(ostringstream &oss, Head &&head, Tail &&... tail) {\n oss << \", \" << head;\n dump_str_rec(oss, forward<Tail>(tail)...);\n}\n\ntemplate <class T, class... U>\nstring dump_str(T &&arg, U &&... args) {\n ostringstream oss;\n oss << arg;\n dump_str_rec(oss, forward<U>(args)...);\n return oss.str();\n}\n\n// --------------- Fast I/O ------------------\n\nvoid fastio() {\n cin.tie(0);\n ios::sync_with_stdio(0);\n cout << fixed << setprecision(20);\n}\n\n// ------------------ ACL --------------------\n\n// #include <atcoder/modint>\n// constexpr int MOD = 998244353;\n// constexpr int MOD = 1000000007;\n// using mint = atcoder::static_modint<MOD>;\n// ostream &operator<<(ostream &os, const mint &v) {\n// return os << v.val();\n// }\n\n// ------------ End of template --------------\n\n#define endl \"\\n\"\nusing ll = long long;\nusing pii = pair<int, int>;\n\nconst bool multitest = false;\n\nvoid solve() {\n int n;\n cin >> n;\n double r, theta;\n cin >> r >> theta;\n vector<double> x(n), y(n);\n for (int i = 0; i < n; i++) { cin >> x[i] >> y[i]; }\n\n auto norm = [](double X, double Y) { return std::sqrt(sq(X) + sq(Y)); };\n\n auto dist = [&](int u, int v) -> double {\n return norm(x[u] - x[v], y[u] - y[v]);\n };\n\n auto arg = [&](int a, int b, int c) {\n double xab = x[b] - x[a], yab = y[b] - y[a];\n double xbc = x[c] - x[b], ybc = y[c] - y[b];\n return std::acos((xab * xbc + yab * ybc) / norm(xab, yab) /\n norm(xbc, ybc)) /\n M_PI * 180.0;\n };\n\n auto Arg = vect(n, vect(n, vect(n, 0.0)));\n auto Dist = vect(n, vect(n, 0.0));\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n Dist[i][j] = dist(i, j);\n for (int k = 0; k < n; k++) { Arg[i][j][k] = arg(i, j, k); }\n }\n }\n\n int M = std::ceil(r) + 2;\n\n double d_inf = 1e40;\n\n auto dp = vect(n, vect(n, vect(M + 1, d_inf)));\n dp[0][0][0] = 0.0;\n for (int m = 0; m < M; m++) {\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n if (dp[i][j][m] == d_inf) continue;\n for (int k = 0; k < n; k++) {\n if (j == k) continue;\n if ((i != j) && (Arg[i][j][k] > theta + eps)) continue;\n chmin(dp[j][k][m + 1], dp[i][j][m] + Dist[j][k]);\n }\n }\n }\n }\n\n int ans = 0;\n\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n for (int k = 0; k <= M; k++) {\n // dmp(i, j, k, dp[i][j][k]);\n if (dp[i][j][k] <= r + eps) chmax(ans, k);\n }\n }\n }\n\n cout << ans << endl;\n return;\n}\n\nint main() {\n fastio();\n if (!multitest) {\n solve();\n } else {\n cerr << \"[Warning] Multi testcase mode on\" << endl;\n int t;\n cin >> t;\n while (t--) solve();\n }\n return 0;\n}", "accuracy": 0.25, "time_ms": 170, "memory_kb": 36756, "score_of_the_acc": -0.6356, "final_rank": 18 }, { "submission_id": "aoj_2233_6008237", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define eps 1e-3\n#define INF 2000000000 // 2e9\n#define LLINF 2000000000000000000ll // 2e18 (llmax:9e18)\n#define all(x) (x).begin(), (x).end()\n#define sq(x) ((x) * (x))\n\n#define rep(i, x) for (int i = 0; i < (int)(x); i++)\n#define drep(i, x) for (int i = ((int)(x)-1); i >= 0; i--)\n\n#define popcount __builtin_popcount\n#define next __next\n#define prev __prev\n\n#ifndef LOCAL\n#define dmp(...) ;\n#else\n#define dmp(...) \\\n cerr << \"[ \" << #__VA_ARGS__ << \" ] : \" << dump_str(__VA_ARGS__) << endl\n#endif\n\n// ---------------- Utility ------------------\n\ntemplate <class T>\nbool chmin(T &a, const T &b) {\n if (a <= b) return false;\n a = b;\n return true;\n}\n\ntemplate <class T>\nbool chmax(T &a, const T &b) {\n if (a >= b) return false;\n a = b;\n return true;\n}\n\ntemplate <class T>\nusing MaxHeap = priority_queue<T>;\n\ntemplate <class T>\nusing MinHeap = priority_queue<T, vector<T>, greater<T>>;\n\ntemplate <class T>\nvector<T> vect(int len, T elem) {\n return vector<T>(len, elem);\n}\n\n// ----------------- Input -------------------\n\ntemplate <class T, class U>\nistream &operator>>(istream &is, pair<T, U> &p) {\n return is >> p.first >> p.second;\n}\n\ntemplate <class T>\nistream &operator>>(istream &is, vector<T> &vec) {\n for (int i = 0; i < vec.size(); i++) is >> vec[i];\n return is;\n}\n\n// ----------------- Output ------------------\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n return os << p.first << ',' << p.second;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const vector<T> &v) {\n for (const T &e : v) os << e << \" \";\n return os;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const deque<T> &d) {\n for (const T &e : d) os << e << \" \";\n return os;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const set<T> &s) {\n os << \"{ \";\n for (const T &e : s) os << e << \" \";\n return os << \"}\";\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const map<T, U> &m) {\n os << \"{ \";\n for (const auto &[key, val] : m) os << \"( \" << key << \" -> \" << val << \" ) \";\n return os << \"}\";\n}\n\ntemplate <class TupleTy, size_t... I>\nvoid dump_tuple(ostream &os, const TupleTy t, std::index_sequence<I...>) {\n (..., (os << (I == 0 ? \"\" : \",\") << std::get<I>(t)));\n}\n\ntemplate <class... Args>\nostream &operator<<(ostream &os, const tuple<Args...> &t) {\n os << \"(\";\n dump_tuple(os, t, std::make_index_sequence<sizeof...(Args)>());\n return os << \")\";\n}\n\nvoid dump_str_rec(ostringstream &) {}\n\ntemplate <class Head, class... Tail>\nvoid dump_str_rec(ostringstream &oss, Head &&head, Tail &&... tail) {\n oss << \", \" << head;\n dump_str_rec(oss, forward<Tail>(tail)...);\n}\n\ntemplate <class T, class... U>\nstring dump_str(T &&arg, U &&... args) {\n ostringstream oss;\n oss << arg;\n dump_str_rec(oss, forward<U>(args)...);\n return oss.str();\n}\n\n// --------------- Fast I/O ------------------\n\nvoid fastio() {\n cin.tie(0);\n ios::sync_with_stdio(0);\n cout << fixed << setprecision(20);\n}\n\n// ------------------ ACL --------------------\n\n// #include <atcoder/modint>\n// constexpr int MOD = 998244353;\n// constexpr int MOD = 1000000007;\n// using mint = atcoder::static_modint<MOD>;\n// ostream &operator<<(ostream &os, const mint &v) {\n// return os << v.val();\n// }\n\n// ------------ End of template --------------\n\n#define endl \"\\n\"\nusing ll = long long;\nusing pii = pair<int, int>;\n\nconst bool multitest = false;\n\nvoid solve() {\n int n;\n cin >> n;\n double r, theta;\n cin >> r >> theta;\n vector<double> x(n), y(n);\n for (int i = 0; i < n; i++) { cin >> x[i] >> y[i]; }\n\n auto norm = [](double X, double Y) { return std::sqrt(sq(X) + sq(Y)); };\n\n auto dist = [&](int u, int v) -> double {\n return norm(x[u] - x[v], y[u] - y[v]);\n };\n\n auto arg = [&](int a, int b, int c) {\n double xab = x[b] - x[a], yab = y[b] - y[a];\n double xbc = x[c] - x[b], ybc = y[c] - y[b];\n return std::acos((xab * xbc + yab * ybc) / norm(xab, yab) /\n norm(xbc, ybc)) /\n M_PI * 180.0;\n };\n\n auto Arg = vect(n, vect(n, vect(n, 0.0)));\n auto Dist = vect(n, vect(n, 0.0));\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n Dist[i][j] = dist(i, j);\n for (int k = 0; k < n; k++) { Arg[i][j][k] = arg(i, j, k); }\n }\n }\n\n int M = std::ceil(r) + 2;\n\n double d_inf = 1e40;\n\n auto dp = vect(n, vect(n, vect(M + 1, d_inf)));\n dp[0][0][0] = 0.0;\n for (int m = 0; m < M; m++) {\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n if (dp[i][j][m] == d_inf) continue;\n for (int k = 0; k < n; k++) {\n if (j == k) continue;\n if ((i != j) && (Arg[i][j][k] > theta + eps)) continue;\n chmin(dp[j][k][m + 1], dp[i][j][m] + Dist[j][k]);\n }\n }\n }\n }\n\n int ans = 0;\n\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n for (int k = 0; k <= M; k++) {\n // dmp(i, j, k, dp[i][j][k]);\n if (dp[i][j][k] <= r + eps) chmax(ans, k);\n }\n }\n }\n\n cout << ans << endl;\n return;\n}\n\nint main() {\n fastio();\n if (!multitest) {\n solve();\n } else {\n cerr << \"[Warning] Multi testcase mode on\" << endl;\n int t;\n cin >> t;\n while (t--) solve();\n }\n return 0;\n}", "accuracy": 0.25, "time_ms": 170, "memory_kb": 36572, "score_of_the_acc": -0.633, "final_rank": 16 }, { "submission_id": "aoj_2233_6008223", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define eps 1e-4\n#define INF 2000000000 // 2e9\n#define LLINF 2000000000000000000ll // 2e18 (llmax:9e18)\n#define all(x) (x).begin(), (x).end()\n#define sq(x) ((x) * (x))\n\n#define rep(i, x) for (int i = 0; i < (int)(x); i++)\n#define drep(i, x) for (int i = ((int)(x)-1); i >= 0; i--)\n\n#define popcount __builtin_popcount\n#define next __next\n#define prev __prev\n\n#ifndef LOCAL\n#define dmp(...) ;\n#else\n#define dmp(...) \\\n cerr << \"[ \" << #__VA_ARGS__ << \" ] : \" << dump_str(__VA_ARGS__) << endl\n#endif\n\n// ---------------- Utility ------------------\n\ntemplate <class T>\nbool chmin(T &a, const T &b) {\n if (a <= b) return false;\n a = b;\n return true;\n}\n\ntemplate <class T>\nbool chmax(T &a, const T &b) {\n if (a >= b) return false;\n a = b;\n return true;\n}\n\ntemplate <class T>\nusing MaxHeap = priority_queue<T>;\n\ntemplate <class T>\nusing MinHeap = priority_queue<T, vector<T>, greater<T>>;\n\ntemplate <class T>\nvector<T> vect(int len, T elem) {\n return vector<T>(len, elem);\n}\n\n// ----------------- Input -------------------\n\ntemplate <class T, class U>\nistream &operator>>(istream &is, pair<T, U> &p) {\n return is >> p.first >> p.second;\n}\n\ntemplate <class T>\nistream &operator>>(istream &is, vector<T> &vec) {\n for (int i = 0; i < vec.size(); i++) is >> vec[i];\n return is;\n}\n\n// ----------------- Output ------------------\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n return os << p.first << ',' << p.second;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const vector<T> &v) {\n for (const T &e : v) os << e << \" \";\n return os;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const deque<T> &d) {\n for (const T &e : d) os << e << \" \";\n return os;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const set<T> &s) {\n os << \"{ \";\n for (const T &e : s) os << e << \" \";\n return os << \"}\";\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const map<T, U> &m) {\n os << \"{ \";\n for (const auto &[key, val] : m) os << \"( \" << key << \" -> \" << val << \" ) \";\n return os << \"}\";\n}\n\ntemplate <class TupleTy, size_t... I>\nvoid dump_tuple(ostream &os, const TupleTy t, std::index_sequence<I...>) {\n (..., (os << (I == 0 ? \"\" : \",\") << std::get<I>(t)));\n}\n\ntemplate <class... Args>\nostream &operator<<(ostream &os, const tuple<Args...> &t) {\n os << \"(\";\n dump_tuple(os, t, std::make_index_sequence<sizeof...(Args)>());\n return os << \")\";\n}\n\nvoid dump_str_rec(ostringstream &) {}\n\ntemplate <class Head, class... Tail>\nvoid dump_str_rec(ostringstream &oss, Head &&head, Tail &&... tail) {\n oss << \", \" << head;\n dump_str_rec(oss, forward<Tail>(tail)...);\n}\n\ntemplate <class T, class... U>\nstring dump_str(T &&arg, U &&... args) {\n ostringstream oss;\n oss << arg;\n dump_str_rec(oss, forward<U>(args)...);\n return oss.str();\n}\n\n// --------------- Fast I/O ------------------\n\nvoid fastio() {\n cin.tie(0);\n ios::sync_with_stdio(0);\n cout << fixed << setprecision(20);\n}\n\n// ------------------ ACL --------------------\n\n// #include <atcoder/modint>\n// constexpr int MOD = 998244353;\n// constexpr int MOD = 1000000007;\n// using mint = atcoder::static_modint<MOD>;\n// ostream &operator<<(ostream &os, const mint &v) {\n// return os << v.val();\n// }\n\n// ------------ End of template --------------\n\n#define endl \"\\n\"\nusing ll = long long;\nusing pii = pair<int, int>;\n\nconst bool multitest = false;\n\nvoid solve() {\n int n;\n cin >> n;\n double r, theta;\n cin >> r >> theta;\n vector<double> x(n), y(n);\n for (int i = 0; i < n; i++) { cin >> x[i] >> y[i]; }\n\n auto norm = [](double X, double Y) { return std::sqrt(sq(X) + sq(Y)); };\n\n auto dist = [&](int u, int v) -> double {\n return norm(x[u] - x[v], y[u] - y[v]);\n };\n\n auto arg = [&](int a, int b, int c) {\n double xab = x[b] - x[a], yab = y[b] - y[a];\n double xbc = x[c] - x[b], ybc = y[c] - y[b];\n return std::acos((xab * xbc + yab * ybc) / norm(xab, yab) /\n norm(xbc, ybc)) /\n M_PI * 180.0;\n };\n\n auto Arg = vect(n, vect(n, vect(n, 0.0)));\n auto Dist = vect(n, vect(n, 0.0));\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n Dist[i][j] = dist(i, j);\n for (int k = 0; k < n; k++) { Arg[i][j][k] = arg(i, j, k); }\n }\n }\n\n int M = std::ceil(r) + 2;\n\n double d_inf = 1e40;\n\n auto dp = vect(n, vect(n, vect(M + 1, d_inf)));\n dp[0][0][0] = 0.0;\n for (int m = 0; m < M; m++) {\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n if (dp[i][j][m] == d_inf) continue;\n for (int k = 0; k < n; k++) {\n if (j == k) continue;\n if (i != j && Arg[i][j][k] > theta + eps) continue;\n chmin(dp[j][k][m + 1], dp[i][j][m] + Dist[j][k]);\n }\n }\n }\n }\n\n int ans = 0;\n\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n for (int k = 0; k <= M; k++) {\n // dmp(i, j, k, dp[i][j][k]);\n if (dp[i][j][k] <= r + eps) chmax(ans, k);\n }\n }\n }\n\n cout << ans << endl;\n return;\n}\n\nint main() {\n fastio();\n if (!multitest) {\n solve();\n } else {\n cerr << \"[Warning] Multi testcase mode on\" << endl;\n int t;\n cin >> t;\n while (t--) solve();\n }\n return 0;\n}", "accuracy": 0.25, "time_ms": 170, "memory_kb": 36548, "score_of_the_acc": -0.6327, "final_rank": 15 }, { "submission_id": "aoj_2233_6008220", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define eps 1e-4\n#define INF 2000000000 // 2e9\n#define LLINF 2000000000000000000ll // 2e18 (llmax:9e18)\n#define all(x) (x).begin(), (x).end()\n#define sq(x) ((x) * (x))\n\n#define rep(i, x) for (int i = 0; i < (int)(x); i++)\n#define drep(i, x) for (int i = ((int)(x)-1); i >= 0; i--)\n\n#define popcount __builtin_popcount\n#define next __next\n#define prev __prev\n\n#ifndef LOCAL\n#define dmp(...) ;\n#else\n#define dmp(...) \\\n cerr << \"[ \" << #__VA_ARGS__ << \" ] : \" << dump_str(__VA_ARGS__) << endl\n#endif\n\n// ---------------- Utility ------------------\n\ntemplate <class T>\nbool chmin(T &a, const T &b) {\n if (a <= b) return false;\n a = b;\n return true;\n}\n\ntemplate <class T>\nbool chmax(T &a, const T &b) {\n if (a >= b) return false;\n a = b;\n return true;\n}\n\ntemplate <class T>\nusing MaxHeap = priority_queue<T>;\n\ntemplate <class T>\nusing MinHeap = priority_queue<T, vector<T>, greater<T>>;\n\ntemplate <class T>\nvector<T> vect(int len, T elem) {\n return vector<T>(len, elem);\n}\n\n// ----------------- Input -------------------\n\ntemplate <class T, class U>\nistream &operator>>(istream &is, pair<T, U> &p) {\n return is >> p.first >> p.second;\n}\n\ntemplate <class T>\nistream &operator>>(istream &is, vector<T> &vec) {\n for (int i = 0; i < vec.size(); i++) is >> vec[i];\n return is;\n}\n\n// ----------------- Output ------------------\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n return os << p.first << ',' << p.second;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const vector<T> &v) {\n for (const T &e : v) os << e << \" \";\n return os;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const deque<T> &d) {\n for (const T &e : d) os << e << \" \";\n return os;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const set<T> &s) {\n os << \"{ \";\n for (const T &e : s) os << e << \" \";\n return os << \"}\";\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const map<T, U> &m) {\n os << \"{ \";\n for (const auto &[key, val] : m) os << \"( \" << key << \" -> \" << val << \" ) \";\n return os << \"}\";\n}\n\ntemplate <class TupleTy, size_t... I>\nvoid dump_tuple(ostream &os, const TupleTy t, std::index_sequence<I...>) {\n (..., (os << (I == 0 ? \"\" : \",\") << std::get<I>(t)));\n}\n\ntemplate <class... Args>\nostream &operator<<(ostream &os, const tuple<Args...> &t) {\n os << \"(\";\n dump_tuple(os, t, std::make_index_sequence<sizeof...(Args)>());\n return os << \")\";\n}\n\nvoid dump_str_rec(ostringstream &) {}\n\ntemplate <class Head, class... Tail>\nvoid dump_str_rec(ostringstream &oss, Head &&head, Tail &&... tail) {\n oss << \", \" << head;\n dump_str_rec(oss, forward<Tail>(tail)...);\n}\n\ntemplate <class T, class... U>\nstring dump_str(T &&arg, U &&... args) {\n ostringstream oss;\n oss << arg;\n dump_str_rec(oss, forward<U>(args)...);\n return oss.str();\n}\n\n// --------------- Fast I/O ------------------\n\nvoid fastio() {\n cin.tie(0);\n ios::sync_with_stdio(0);\n cout << fixed << setprecision(20);\n}\n\n// ------------------ ACL --------------------\n\n// #include <atcoder/modint>\n// constexpr int MOD = 998244353;\n// constexpr int MOD = 1000000007;\n// using mint = atcoder::static_modint<MOD>;\n// ostream &operator<<(ostream &os, const mint &v) {\n// return os << v.val();\n// }\n\n// ------------ End of template --------------\n\n#define endl \"\\n\"\nusing ll = long long;\nusing pii = pair<int, int>;\n\nconst bool multitest = false;\n\nvoid solve() {\n int n;\n cin >> n;\n double r, theta;\n cin >> r >> theta;\n vector<double> x(n), y(n);\n for (int i = 0; i < n; i++) { cin >> x[i] >> y[i]; }\n\n auto norm = [](double X, double Y) { return std::sqrt(sq(X) + sq(Y)); };\n\n auto dist = [&](int u, int v) -> double {\n return norm(x[u] - x[v], y[u] - y[v]);\n };\n\n auto arg = [&](int a, int b, int c) {\n double xab = x[b] - x[a], yab = y[b] - y[a];\n double xbc = x[c] - x[b], ybc = y[c] - y[b];\n return std::acos((xab * xbc + yab * ybc) / norm(xab, yab) /\n norm(xbc, ybc)) /\n M_PI * 180.0;\n };\n\n auto Arg = vect(n, vect(n, vect(n, 0.0)));\n auto Dist = vect(n, vect(n, 0.0));\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n Dist[i][j] = dist(i, j);\n for (int k = 0; k < n; k++) { Arg[i][j][k] = arg(i, j, k); }\n }\n }\n\n int M = std::ceil(r) + 2;\n\n double d_inf = 1e40;\n\n auto dp = vect(n, vect(n, vect(M + 1, d_inf)));\n dp[0][0][0] = 0.0;\n for (int m = 0; m < M; m++) {\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n if (dp[i][j][m] == d_inf) continue;\n for (int k = 0; k < n; k++) {\n if (j == k) continue;\n if (i != j && Arg[i][j][k] > theta - eps) continue;\n chmin(dp[j][k][m + 1], dp[i][j][m] + Dist[j][k]);\n }\n }\n }\n }\n\n int ans = 0;\n\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n for (int k = 0; k <= M; k++) {\n // dmp(i, j, k, dp[i][j][k]);\n if (dp[i][j][k] <= r + eps) chmax(ans, k);\n }\n }\n }\n\n cout << ans << endl;\n return;\n}\n\nint main() {\n fastio();\n if (!multitest) {\n solve();\n } else {\n cerr << \"[Warning] Multi testcase mode on\" << endl;\n int t;\n cin >> t;\n while (t--) solve();\n }\n return 0;\n}", "accuracy": 0.25, "time_ms": 170, "memory_kb": 36416, "score_of_the_acc": -0.6309, "final_rank": 13 }, { "submission_id": "aoj_2233_6008217", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define eps 1e-7\n#define INF 2000000000 // 2e9\n#define LLINF 2000000000000000000ll // 2e18 (llmax:9e18)\n#define all(x) (x).begin(), (x).end()\n#define sq(x) ((x) * (x))\n\n#define rep(i, x) for (int i = 0; i < (int)(x); i++)\n#define drep(i, x) for (int i = ((int)(x)-1); i >= 0; i--)\n\n#define popcount __builtin_popcount\n#define next __next\n#define prev __prev\n\n#ifndef LOCAL\n#define dmp(...) ;\n#else\n#define dmp(...) \\\n cerr << \"[ \" << #__VA_ARGS__ << \" ] : \" << dump_str(__VA_ARGS__) << endl\n#endif\n\n// ---------------- Utility ------------------\n\ntemplate <class T>\nbool chmin(T &a, const T &b) {\n if (a <= b) return false;\n a = b;\n return true;\n}\n\ntemplate <class T>\nbool chmax(T &a, const T &b) {\n if (a >= b) return false;\n a = b;\n return true;\n}\n\ntemplate <class T>\nusing MaxHeap = priority_queue<T>;\n\ntemplate <class T>\nusing MinHeap = priority_queue<T, vector<T>, greater<T>>;\n\ntemplate <class T>\nvector<T> vect(int len, T elem) {\n return vector<T>(len, elem);\n}\n\n// ----------------- Input -------------------\n\ntemplate <class T, class U>\nistream &operator>>(istream &is, pair<T, U> &p) {\n return is >> p.first >> p.second;\n}\n\ntemplate <class T>\nistream &operator>>(istream &is, vector<T> &vec) {\n for (int i = 0; i < vec.size(); i++) is >> vec[i];\n return is;\n}\n\n// ----------------- Output ------------------\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n return os << p.first << ',' << p.second;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const vector<T> &v) {\n for (const T &e : v) os << e << \" \";\n return os;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const deque<T> &d) {\n for (const T &e : d) os << e << \" \";\n return os;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const set<T> &s) {\n os << \"{ \";\n for (const T &e : s) os << e << \" \";\n return os << \"}\";\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const map<T, U> &m) {\n os << \"{ \";\n for (const auto &[key, val] : m) os << \"( \" << key << \" -> \" << val << \" ) \";\n return os << \"}\";\n}\n\ntemplate <class TupleTy, size_t... I>\nvoid dump_tuple(ostream &os, const TupleTy t, std::index_sequence<I...>) {\n (..., (os << (I == 0 ? \"\" : \",\") << std::get<I>(t)));\n}\n\ntemplate <class... Args>\nostream &operator<<(ostream &os, const tuple<Args...> &t) {\n os << \"(\";\n dump_tuple(os, t, std::make_index_sequence<sizeof...(Args)>());\n return os << \")\";\n}\n\nvoid dump_str_rec(ostringstream &) {}\n\ntemplate <class Head, class... Tail>\nvoid dump_str_rec(ostringstream &oss, Head &&head, Tail &&... tail) {\n oss << \", \" << head;\n dump_str_rec(oss, forward<Tail>(tail)...);\n}\n\ntemplate <class T, class... U>\nstring dump_str(T &&arg, U &&... args) {\n ostringstream oss;\n oss << arg;\n dump_str_rec(oss, forward<U>(args)...);\n return oss.str();\n}\n\n// --------------- Fast I/O ------------------\n\nvoid fastio() {\n cin.tie(0);\n ios::sync_with_stdio(0);\n cout << fixed << setprecision(20);\n}\n\n// ------------------ ACL --------------------\n\n// #include <atcoder/modint>\n// constexpr int MOD = 998244353;\n// constexpr int MOD = 1000000007;\n// using mint = atcoder::static_modint<MOD>;\n// ostream &operator<<(ostream &os, const mint &v) {\n// return os << v.val();\n// }\n\n// ------------ End of template --------------\n\n#define endl \"\\n\"\nusing ll = long long;\nusing pii = pair<int, int>;\n\nconst bool multitest = false;\n\nvoid solve() {\n int n;\n cin >> n;\n double r, theta;\n cin >> r >> theta;\n vector<double> x(n), y(n);\n for (int i = 0; i < n; i++) { cin >> x[i] >> y[i]; }\n\n auto norm = [](double X, double Y) { return std::sqrt(sq(X) + sq(Y)); };\n\n auto dist = [&](int u, int v) -> double {\n return norm(x[u] - x[v], y[u] - y[v]);\n };\n\n auto arg = [&](int a, int b, int c) {\n double xab = x[b] - x[a], yab = y[b] - y[a];\n double xbc = x[c] - x[b], ybc = y[c] - y[b];\n return std::acos((xab * xbc + yab * ybc) / norm(xab, yab) /\n norm(xbc, ybc)) /\n M_PI * 180.0;\n };\n\n auto Arg = vect(n, vect(n, vect(n, 0.0)));\n auto Dist = vect(n, vect(n, 0.0));\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n Dist[i][j] = dist(i, j);\n for (int k = 0; k < n; k++) { Arg[i][j][k] = arg(i, j, k); }\n }\n }\n\n int M = std::ceil(r) + 2;\n\n double d_inf = 1e40;\n\n auto dp = vect(n, vect(n, vect(M + 1, d_inf)));\n dp[0][0][0] = 0.0;\n for (int m = 0; m < M; m++) {\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n if (dp[i][j][m] == d_inf) continue;\n for (int k = 0; k < n; k++) {\n if (j == k) continue;\n if (i != j && Arg[i][j][k] > theta - eps) continue;\n chmin(dp[j][k][m + 1], dp[i][j][m] + Dist[j][k]);\n }\n }\n }\n }\n\n int ans = 0;\n\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n for (int k = 0; k <= M; k++) {\n // dmp(i, j, k, dp[i][j][k]);\n if (dp[i][j][k] <= r + eps) chmax(ans, k);\n }\n }\n }\n\n cout << ans << endl;\n return;\n}\n\nint main() {\n fastio();\n if (!multitest) {\n solve();\n } else {\n cerr << \"[Warning] Multi testcase mode on\" << endl;\n int t;\n cin >> t;\n while (t--) solve();\n }\n return 0;\n}", "accuracy": 0.25, "time_ms": 170, "memory_kb": 36544, "score_of_the_acc": -0.6326, "final_rank": 14 }, { "submission_id": "aoj_2233_6008213", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define eps 1e-9\n#define INF 2000000000 // 2e9\n#define LLINF 2000000000000000000ll // 2e18 (llmax:9e18)\n#define all(x) (x).begin(), (x).end()\n#define sq(x) ((x) * (x))\n\n#define rep(i, x) for (int i = 0; i < (int)(x); i++)\n#define drep(i, x) for (int i = ((int)(x)-1); i >= 0; i--)\n\n#define popcount __builtin_popcount\n#define next __next\n#define prev __prev\n\n#ifndef LOCAL\n#define dmp(...) ;\n#else\n#define dmp(...) \\\n cerr << \"[ \" << #__VA_ARGS__ << \" ] : \" << dump_str(__VA_ARGS__) << endl\n#endif\n\n// ---------------- Utility ------------------\n\ntemplate <class T>\nbool chmin(T &a, const T &b) {\n if (a <= b) return false;\n a = b;\n return true;\n}\n\ntemplate <class T>\nbool chmax(T &a, const T &b) {\n if (a >= b) return false;\n a = b;\n return true;\n}\n\ntemplate <class T>\nusing MaxHeap = priority_queue<T>;\n\ntemplate <class T>\nusing MinHeap = priority_queue<T, vector<T>, greater<T>>;\n\ntemplate <class T>\nvector<T> vect(int len, T elem) {\n return vector<T>(len, elem);\n}\n\n// ----------------- Input -------------------\n\ntemplate <class T, class U>\nistream &operator>>(istream &is, pair<T, U> &p) {\n return is >> p.first >> p.second;\n}\n\ntemplate <class T>\nistream &operator>>(istream &is, vector<T> &vec) {\n for (int i = 0; i < vec.size(); i++) is >> vec[i];\n return is;\n}\n\n// ----------------- Output ------------------\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n return os << p.first << ',' << p.second;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const vector<T> &v) {\n for (const T &e : v) os << e << \" \";\n return os;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const deque<T> &d) {\n for (const T &e : d) os << e << \" \";\n return os;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const set<T> &s) {\n os << \"{ \";\n for (const T &e : s) os << e << \" \";\n return os << \"}\";\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const map<T, U> &m) {\n os << \"{ \";\n for (const auto &[key, val] : m) os << \"( \" << key << \" -> \" << val << \" ) \";\n return os << \"}\";\n}\n\ntemplate <class TupleTy, size_t... I>\nvoid dump_tuple(ostream &os, const TupleTy t, std::index_sequence<I...>) {\n (..., (os << (I == 0 ? \"\" : \",\") << std::get<I>(t)));\n}\n\ntemplate <class... Args>\nostream &operator<<(ostream &os, const tuple<Args...> &t) {\n os << \"(\";\n dump_tuple(os, t, std::make_index_sequence<sizeof...(Args)>());\n return os << \")\";\n}\n\nvoid dump_str_rec(ostringstream &) {}\n\ntemplate <class Head, class... Tail>\nvoid dump_str_rec(ostringstream &oss, Head &&head, Tail &&... tail) {\n oss << \", \" << head;\n dump_str_rec(oss, forward<Tail>(tail)...);\n}\n\ntemplate <class T, class... U>\nstring dump_str(T &&arg, U &&... args) {\n ostringstream oss;\n oss << arg;\n dump_str_rec(oss, forward<U>(args)...);\n return oss.str();\n}\n\n// --------------- Fast I/O ------------------\n\nvoid fastio() {\n cin.tie(0);\n ios::sync_with_stdio(0);\n cout << fixed << setprecision(20);\n}\n\n// ------------------ ACL --------------------\n\n// #include <atcoder/modint>\n// constexpr int MOD = 998244353;\n// constexpr int MOD = 1000000007;\n// using mint = atcoder::static_modint<MOD>;\n// ostream &operator<<(ostream &os, const mint &v) {\n// return os << v.val();\n// }\n\n// ------------ End of template --------------\n\n#define endl \"\\n\"\nusing ll = long long;\nusing pii = pair<int, int>;\n\nconst bool multitest = false;\n\nvoid solve() {\n int n;\n cin >> n;\n double r, theta;\n cin >> r >> theta;\n vector<double> x(n), y(n);\n for (int i = 0; i < n; i++) { cin >> x[i] >> y[i]; }\n\n auto norm = [](double X, double Y) { return std::sqrt(sq(X) + sq(Y)); };\n\n auto dist = [&](int u, int v) -> double {\n return norm(x[u] - x[v], y[u] - y[v]);\n };\n\n auto arg = [&](int a, int b, int c) {\n double xba = x[b] - x[a], yba = y[b] - y[a];\n double xbc = x[c] - x[b], ybc = y[c] - y[b];\n return std::acos((xba * xbc + yba * ybc) / norm(xba, yba) /\n norm(xbc, ybc)) /\n M_PI * 180.0;\n };\n\n auto Arg = vect(n, vect(n, vect(n, 0.0)));\n auto Dist = vect(n, vect(n, 0.0));\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n Dist[i][j] = dist(i, j);\n for (int k = 0; k < n; k++) { Arg[i][j][k] = arg(i, j, k); }\n }\n }\n\n int M = std::ceil(r) + 2;\n\n double d_inf = 1e40;\n\n auto dp = vect(n, vect(n, vect(M + 1, d_inf)));\n dp[0][0][0] = 0.0;\n for (int m = 0; m < M; m++) {\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n if (dp[i][j][m] == d_inf) continue;\n for (int k = 0; k < n; k++) {\n if (j == k) continue;\n if (i != j && Arg[i][j][k] > theta) continue;\n chmin(dp[j][k][m + 1], dp[i][j][m] + Dist[j][k]);\n }\n }\n }\n }\n\n int ans = 0;\n\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n for (int k = 0; k <= M; k++) {\n // dmp(i, j, k, dp[i][j][k]);\n if (dp[i][j][k] <= r) chmax(ans, k);\n }\n }\n }\n\n cout << ans << endl;\n return;\n}\n\nint main() {\n fastio();\n if (!multitest) {\n solve();\n } else {\n cerr << \"[Warning] Multi testcase mode on\" << endl;\n int t;\n cin >> t;\n while (t--) solve();\n }\n return 0;\n}", "accuracy": 0.25, "time_ms": 170, "memory_kb": 36572, "score_of_the_acc": -0.633, "final_rank": 16 }, { "submission_id": "aoj_2233_6008185", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define eps 1e-9\n#define INF 2000000000 // 2e9\n#define LLINF 2000000000000000000ll // 2e18 (llmax:9e18)\n#define all(x) (x).begin(), (x).end()\n#define sq(x) ((x) * (x))\n\n#define rep(i, x) for (int i = 0; i < (int)(x); i++)\n#define drep(i, x) for (int i = ((int)(x)-1); i >= 0; i--)\n\n#define popcount __builtin_popcount\n#define next __next\n#define prev __prev\n\n#ifndef LOCAL\n#define dmp(...) ;\n#else\n#define dmp(...) \\\n cerr << \"[ \" << #__VA_ARGS__ << \" ] : \" << dump_str(__VA_ARGS__) << endl\n#endif\n\n// ---------------- Utility ------------------\n\ntemplate <class T>\nbool chmin(T &a, const T &b) {\n if (a <= b) return false;\n a = b;\n return true;\n}\n\ntemplate <class T>\nbool chmax(T &a, const T &b) {\n if (a >= b) return false;\n a = b;\n return true;\n}\n\ntemplate <class T>\nusing MaxHeap = priority_queue<T>;\n\ntemplate <class T>\nusing MinHeap = priority_queue<T, vector<T>, greater<T>>;\n\ntemplate <class T>\nvector<T> vect(int len, T elem) {\n return vector<T>(len, elem);\n}\n\n// ----------------- Input -------------------\n\ntemplate <class T, class U>\nistream &operator>>(istream &is, pair<T, U> &p) {\n return is >> p.first >> p.second;\n}\n\ntemplate <class T>\nistream &operator>>(istream &is, vector<T> &vec) {\n for (int i = 0; i < vec.size(); i++) is >> vec[i];\n return is;\n}\n\n// ----------------- Output ------------------\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n return os << p.first << ',' << p.second;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const vector<T> &v) {\n for (const T &e : v) os << e << \" \";\n return os;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const deque<T> &d) {\n for (const T &e : d) os << e << \" \";\n return os;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const set<T> &s) {\n os << \"{ \";\n for (const T &e : s) os << e << \" \";\n return os << \"}\";\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const map<T, U> &m) {\n os << \"{ \";\n for (const auto &[key, val] : m) os << \"( \" << key << \" -> \" << val << \" ) \";\n return os << \"}\";\n}\n\ntemplate <class TupleTy, size_t... I>\nvoid dump_tuple(ostream &os, const TupleTy t, std::index_sequence<I...>) {\n (..., (os << (I == 0 ? \"\" : \",\") << std::get<I>(t)));\n}\n\ntemplate <class... Args>\nostream &operator<<(ostream &os, const tuple<Args...> &t) {\n os << \"(\";\n dump_tuple(os, t, std::make_index_sequence<sizeof...(Args)>());\n return os << \")\";\n}\n\nvoid dump_str_rec(ostringstream &) {}\n\ntemplate <class Head, class... Tail>\nvoid dump_str_rec(ostringstream &oss, Head &&head, Tail &&... tail) {\n oss << \", \" << head;\n dump_str_rec(oss, forward<Tail>(tail)...);\n}\n\ntemplate <class T, class... U>\nstring dump_str(T &&arg, U &&... args) {\n ostringstream oss;\n oss << arg;\n dump_str_rec(oss, forward<U>(args)...);\n return oss.str();\n}\n\n// --------------- Fast I/O ------------------\n\nvoid fastio() {\n cin.tie(0);\n ios::sync_with_stdio(0);\n cout << fixed << setprecision(20);\n}\n\n// ------------------ ACL --------------------\n\n// #include <atcoder/modint>\n// constexpr int MOD = 998244353;\n// constexpr int MOD = 1000000007;\n// using mint = atcoder::static_modint<MOD>;\n// ostream &operator<<(ostream &os, const mint &v) {\n// return os << v.val();\n// }\n\n// ------------ End of template --------------\n\n#define endl \"\\n\"\nusing ll = long long;\nusing pii = pair<int, int>;\n\nconst bool multitest = false;\n\nvoid solve() {\n int n;\n cin >> n;\n double r, theta;\n cin >> r >> theta;\n vector<int> x(n), y(n);\n for (int i = 0; i < n; i++) { cin >> x[i] >> y[i]; }\n\n auto norm = [](double X, double Y) { return std::sqrt(sq(X) + sq(Y)); };\n\n auto dist = [&](int u, int v) -> double {\n return norm(x[u] - x[v], y[u] - y[v]);\n };\n\n auto arg = [&](int a, int b, int c) {\n double xba = x[b] - x[a], yba = y[b] - y[a];\n double xbc = x[c] - x[b], ybc = y[c] - y[b];\n return std::acos((xba * xbc + yba * ybc) / norm(xba, yba) /\n norm(xbc, ybc)) /\n M_PI * 180.0;\n };\n\n auto Arg = vect(n, vect(n, vect(n, 0.0)));\n auto Dist = vect(n, vect(n, 0.0));\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n Dist[i][j] = dist(i, j);\n for (int k = 0; k < n; k++) { Arg[i][j][k] = arg(i, j, k); }\n }\n }\n\n int M = std::ceil(r) + 2;\n\n double d_inf = numeric_limits<double>::max();\n\n auto dp = vect(n, vect(n, vect(M + 1, d_inf)));\n dp[0][0][0] = 0.0;\n for (int m = 0; m < M; m++) {\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n if (dp[i][j][m] == d_inf) continue;\n for (int k = 0; k < n; k++) {\n if (j == k) continue;\n if (i != j && Arg[i][j][k] > theta) continue;\n chmin(dp[j][k][m + 1], dp[i][j][m] + Dist[j][k]);\n }\n }\n }\n }\n\n int ans = 0;\n\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n for (int k = 0; k <= M; k++) {\n // dmp(i, j, k, dp[i][j][k]);\n if (dp[i][j][k] <= r) chmax(ans, k);\n }\n }\n }\n\n cout << ans << endl;\n return;\n}\n\nint main() {\n fastio();\n if (!multitest) {\n solve();\n } else {\n cerr << \"[Warning] Multi testcase mode on\" << endl;\n int t;\n cin >> t;\n while (t--) solve();\n }\n return 0;\n}", "accuracy": 0.25, "time_ms": 210, "memory_kb": 36420, "score_of_the_acc": -0.6759, "final_rank": 19 }, { "submission_id": "aoj_2233_6001085", "code_snippet": "#include<bits/stdc++.h>\nusing ll = long long;\n#define var auto\nconst char newl = '\\n';\n\nusing namespace std;\n\ntemplate<typename T1, typename T2> inline void chmin(T1& a, T2 b) { if (a > b) a = b; }\ntemplate<typename T1, typename T2> inline void chmax(T1& a, T2 b) { if (a < b) a = b; }\n\nconst double PI = 3.141592653589793238;\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n double n;\n cin >> n;\n double r, theta;\n cin >> r >> theta;\n theta = theta / 180 * PI;\n vector<pair<double, double>> points(n);\n for (var&& elem : points) cin >> elem.first >> elem.second;\n\n var get_arg = [&](pair<double, double> p1, pair<double, double> p2) {\n var deg1 = atan2(p1.first, p1.second);\n var deg2 = atan2(p2.first, p2.second);\n if (deg1 < 0) deg1 += 2 * PI;\n if (0 < deg2) deg2 -= 2 * PI;\n var deg = abs(deg1 - deg2);\n chmin(deg, abs(2 * PI - deg));\n return deg;\n };\n\n // 直線 ij, jk のなす角\n vector<vector<vector<bool>>> reachable(n, vector<vector<bool>>(n, vector<bool>(n, false)));\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n if (i == j) continue;\n for (int k = 0; k < n; k++) {\n if (j == k) continue;\n var vec1 = make_pair(points[j].first - points[i].first, points[j].second - points[i].second);\n var vec2 = make_pair(points[k].first - points[j].first, points[k].second - points[j].second);\n // cout << i << \" \" << j << \" \" << k << \": \" << get_arg(vec1, vec2) / PI * 180 << endl;\n reachable[i][j][k] = get_arg(vec1, vec2) < theta + 0.00001;\n }\n }\n }\n\n vector<vector<double>> dist(n, vector<double>(n));\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n dist[i][j] = sqrt(pow(points[i].first - points[j].first, 2) + pow(points[i].second - points[j].second, 2));\n }\n }\n\n bool updated = false;\n vector<vector<double>> min_cost(n, vector<double>(n, INFINITY));\n for (int cur = 1; cur < n; cur++) {\n if (r < dist[0][cur] + 0.00001) continue;\n updated = true;\n min_cost[0][cur] = dist[0][cur];\n }\n int cnt = 1;\n if (not updated) {\n cnt = 0;\n }\n while (cnt) {\n bool updated = false;\n vector<vector<double>> nxt_min_cost(n, vector<double>(n, INFINITY));\n for (int from = 0; from < n; from++) {\n for (int cur = 0; cur < n; cur++) {\n if (from == cur) continue;\n for (int nxt = 0; nxt < n; nxt++) {\n if (cur == nxt) continue;\n if (not reachable[from][cur][nxt]) continue;\n var nxtcost = min_cost[from][cur] + dist[cur][nxt];\n if (r < nxtcost + 0.00001) continue;\n updated = true;\n chmin(nxt_min_cost[cur][nxt], nxtcost);\n }\n }\n }\n if (not updated) break;\n min_cost = move(nxt_min_cost);\n cnt++;\n }\n cout << cnt << endl;\n}", "accuracy": 0.28125, "time_ms": 10, "memory_kb": 3916, "score_of_the_acc": -0.0035, "final_rank": 12 } ]
aoj_2238_cpp
Problem G: Nurie 紙の上に円が n 個書かれている. うさぎは k 色の絵の具を持っていて, 次のルールに従って紙に色を塗っていく. 各領域をある1 色の絵の具で塗るか, 何も塗らない. ここで「領域」とは, 円弧の集合で囲まれた面積有限の部分を指す. 隣り合っている2 つの領域を同じ色の絵の具で塗ることはできない. ここで「隣り合っている」とは,境界の一部を共有する円弧があることを指す. 共有点が有限個である2 つの領域は, 隣り合っているとはみなされない. 隣り合っている2 つの領域の両方を塗らないままにすることは許される. うさぎはできるだけ多くの領域を絵の具で塗りたい. 塗れる領域の個数の最大値を求めよ. Input 1 行目: n k (1 ≤ n ≤ 20, 1 ≤ k ≤ 1 000 000 000) 2-( n + 1) 行目: x i y i r i , (−1 000 ≤ x i , y i ≤ 1 000, 1 ≤ r i ≤ 1 000) (整数) どの2 つの円も一致しない. いずれか2 円の交点として得られる点集合は, 距離が10 −3 以下の異なる2 点を含まない. Output 絵の具で塗れる領域の個数の最大値を一行に出力せよ. Sample Input 1 2 1 10 0 10 20 0 10 Sample Output 1 2
[ { "submission_id": "aoj_2238_2266327", "code_snippet": "#include<bits/stdc++.h>\n#define MAX 1024\n#define inf 1<<29\n#define linf (1e16)\n#define eps (1e-8)\n#define Eps (1e-15)\n#define mod 1000000007\n#define pi acos(-1.0)\n#define phi (1.0+sqrt(5.0))/2.0\n#define f first\n#define s second\n#define mp make_pair\n#define pb push_back\n#define all(a) (a).begin(),(a).end()\n#define pd(a) printf(\"%.10f\\n\",(double)(a))\n#define pld(a) printf(\"%.10Lf\\n\",(ld)(a))\n#define FOR(i,a,b) for(int i=(a);i<(b);i++)\n#define RFOR(i,a,b) for(int i=(a)-1;(b)<=i;i--)\n#define Unique(v) v.erase(unique(all(v)),v.end())\n#define equals(a,b) (fabs((a)-(b))<eps)\nusing namespace std;\ntypedef long double ld;\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef pair<int,int> pii;\ntypedef pair<int,double> pid;\ntypedef pair<double,int> pdi;\ntypedef pair<double,double> pdd;\ntypedef vector<int> vi;\ntypedef vector<pii> vpi;\n\nclass Point{\npublic:\n double x,y;\n Point(double x=0,double y=0):x(x),y(y){}\n\n Point operator+(Point p){ return Point(x+p.x,y+p.y);}\n Point operator-(Point p){ return Point(x-p.x,y-p.y);}\n Point operator*(double k){ return Point(x*k,y*k);}\n Point operator/(double k){ return Point(x/k,y/k);}\n bool operator<(Point p)const{ \n return equals(x,p.x) ? y-p.y<-eps : x-p.x<-eps; }\n bool operator==(Point p)const{ \n return fabs(x-p.x)<eps && fabs(y-p.y)<eps;}\n\n double abs(){ return sqrt(norm());}\n double norm(){ return (x*x+y*y);}\n};\ntypedef Point Vector;\ntypedef vector<Point> Polygon;\n\nclass Segment{\npublic:\n Point p1,p2;\n Segment(Point p1=Point(),Point p2=Point()):p1(p1),p2(p2){}\n};\ntypedef Segment Line;\n\nclass Circle{\npublic:\n Point c;\n double r;\n Circle(Point c=Point(),double r=0.0):c(c),r(r){}\n};\n\ndouble norm(Vector a){ return (a.x*a.x+a.y*a.y);}\ndouble abs(Vector a){ return sqrt(norm(a));}\ndouble dot(Vector a,Vector b){ return (a.x*b.x+a.y*b.y);}\ndouble cross(Vector a,Vector b){ return (a.x*b.y-a.y*b.x);}\ndouble arg(Vector p){ return atan2(p.y,p.x);}\nVector polar(double a,double r){ return Point(cos(r)*a,sin(r)*a);}\n\nclass Dedge{\n public:\n int s,t,f,l;\n ld r;\n Dedge(int s,int t,ld r):s(s),t(t),f(0),l(0){\n while(pi<r)r-=2.0*pi;\n while(r<-pi)r+=2.0*pi;\n this->r=r;\n }\n bool operator==(Dedge e)const{ return t==e.t; }\n bool operator<(Dedge e)const{ return r>e.r; }\n bool operator<(ld R)const{ return r>R; }\n};\n\nclass DualGraph{\n public:\n int n;\n vector<Point> v;\n vector<Polygon> P;\n vector<vector<Dedge> > g;\n\n DualGraph(vector<Point> v):v(v),g(v.size()),n(v.size()){}\n\n void add_edge(int s,int t){\n ld r=arg(v[t]-v[s]);\n g[s].pb(Dedge(s,t,r));\n g[t].pb(Dedge(t,s,r+pi));\n }\n\n void add_polygon(int s,int t,ld r){\n vector<Dedge>::iterator e=lower_bound(all(g[s]),r-eps);\n if(e==g[s].end())e=g[s].begin();\n if(e->f)return;\n e->f=1;\n e->l=t;\n P[t].pb(v[s]);\n add_polygon(e->t, t, e->r > 0 ? e->r-pi : e->r+pi);\n }\n\n vector<Polygon> dual(){\n FOR(i,0,n){ sort(all(g[i])); Unique(g[i]); }\n FOR(i,0,n){\n FOR(j,0,g[i].size()){\n if(!g[i][j].f){\n P.pb(Polygon());\n add_polygon(i,P.size()-1,g[i][j].r+eps);\n }\n }\n }\n return P;\n }\n};\n\nbool on(Circle c,Point p){ return equals(abs(c.c-p),c.r); }\n\nint intersect(Circle a,Circle b){\n double d=abs(a.c-b.c);\n if(a.r<b.r)swap(a,b);\n if(a.r+b.r+eps<d)return 2;\n if(a.r+b.r-eps<d)return 1;\n if(a.r-b.r+eps<d)return 0;\n if(a.r-b.r-eps<d)return -1;\n return -2;\n}\n\npair<Point,Point> getCrossPoints(Circle c1,Circle c2){\n double d=abs(c1.c-c2.c);\n double a=acos((c1.r*c1.r+d*d-c2.r*c2.r)/(2.0*c1.r*d));\n double t=arg(c2.c-c1.c);\n return mp(c1.c+polar(c1.r,t+a),c1.c+polar(c1.r,t-a));\n}\n\nPoint rotate(Point base,Point a,double r){\n Point b=a-base;\n a.x=b.x*cos((r/180.0)*pi)-b.y*sin((r/180.0)*pi);\n a.y=b.x*sin((r/180.0)*pi)+b.y*cos((r/180.0)*pi);\n a=a+base;\n return a;\n}\n\ndouble getAngle(Vector a,Vector b){\n double tmp=dot(a,b)/(abs(a)*abs(b));\n if(tmp<-1.0)tmp=-1.0;\n if(1.0<tmp)tmp=1.0;\n double r=acos(tmp)*180.0/pi;\n if(cross(a,b)<-eps)r=360.0-r;\n return r;\n}\n\ndouble Area(Polygon p){\n double area=0.0;\n int n=p.size();\n for(int i=0;i<n;i++)area+=cross(p[i],p[(i+1)%n]);\n return area/2.0;\n}\n\nint contains(Polygon g,Point p){\n int n=g.size();\n bool x=false;\n for(int i=0;i<n;i++){\n Vector a=g[i]-p,b=g[(i+1)%n]-p;\n if(abs(cross(a,b))<eps && dot(a,b)<eps)return 1;\n if(a.y>b.y)swap(a,b);\n if(a.y<eps && eps<b.y && cross(a,b)>eps)x=!x;\n }\n if(x)return 2;\n return 0;\n}\n\nvector<Polygon> remove(vector<Polygon> p){\n vector<Polygon> res;\n FOR(i,0,p.size())if(eps<Area(p[i]))res.pb(p[i]);\n return res;\n}\n\nstruct edge{ int to,cap,rev; };\n\nint n,k;\nvector<Circle> vc;\nvector<Polygon> v;\nvector<edge> G[MAX];\nbool used[MAX];\nbool in[MAX][MAX];\nbool adj[MAX][MAX];\n\nvoid add_edge(int from,int to,int cap){\n G[from].push_back((edge){to,cap,(int)G[to].size()});\n G[to].push_back((edge){from,0,(int)G[from].size()-1});\n}\n \nint dfs(int v,int t,int f){\n if(v==t)return f;\n used[v]=true;\n for(int i=0;i<G[v].size();i++){\n edge &e=G[v][i];\n if(!used[e.to] && e.cap>0){\n int d=dfs(e.to,t,min(f,e.cap));\n if(d>0){\n e.cap-=d;\n G[e.to][e.rev].cap+=d;\n return d;\n }\n }\n }\n return 0;\n}\n \nint max_flow(int s,int t){\n int flow=0;\n for(;;){\n fill(used,used+MAX,0);\n int f=dfs(s,t,inf);\n if(f==0)return flow;\n flow+=f;\n }\n}\n\nbool same(Point a,Point b,Point c,Point d){\n return ((a==c && b==d) || (a==d && b==c));\n}\n\nbool check1(Polygon a,Polygon b){\n FOR(i,0,a.size())\n FOR(j,0,b.size())\n if(same(a[i],a[(i+1)%a.size()],b[j],b[(j+1)%b.size()]))\n return true;\n return false;\n}\n\nbool check2(Polygon a,Polygon b){\n FOR(i,0,a.size())\n if(contains(b,a[i])!=2)return false;\n return true;\n}\n\nint cal(){\n int V=v.size();\n int s=2*V,t=s+1;\n FOR(i,0,V){\n add_edge(s,i,1);\n add_edge(i+V,t,1);\n }\n FOR(i,0,V){\n FOR(j,0,V){\n if(i==j)continue;\n adj[i][j]=check1(v[i],v[j]);\n in[i][j]=check2(v[i],v[j]);\n }\n }\n FOR(i,0,V)\n FOR(j,0,V)\n if(in[i][j])\n FOR(k,0,V)\n if(in[i][k] && in[k][j])\n in[i][j]=false;\n\n FOR(i,0,V)\n FOR(j,0,V)\n if(adj[i][j] || in[i][j] || in[j][i])add_edge(i,j+V,1);\n return V-max_flow(s,t)/2;\n}\n\nint solve(){\n vector<Point> vp,tmp;\n FOR(i,0,n){\n vp.pb(Point(vc[i].c.x+vc[i].r,vc[i].c.y));\n vp.pb(Point(vc[i].c.x-vc[i].r,vc[i].c.y));\n vp.pb(Point(vc[i].c.x,vc[i].c.y+vc[i].r));\n vp.pb(Point(vc[i].c.x,vc[i].c.y-vc[i].r));\n FOR(j,i+1,n){\n if(abs(intersect(vc[i],vc[j]))==2)continue;\n pair<Point,Point> pp=getCrossPoints(vc[i],vc[j]);\n vp.pb(pp.f);\n vp.pb(pp.s);\n }\n }\n FOR(i,0,n){\n vector<pair<double,Point> > list;\n FOR(j,0,vp.size()){\n double rarg=arg(vp[j]-vc[i].c);\n if(rarg<-eps)rarg+=2.0*pi;\n if(on(vc[i],vp[j]))list.pb(mp(rarg,vp[j]));\n }\n sort(all(list));\n FOR(j,0,list.size()){\n Point a=list[j].s,b=list[(j+1)%list.size()].s;\n double r=getAngle(a-vc[i].c,b-vc[i].c);\n tmp.pb(rotate(vc[i].c,a,r/2.0));\n }\n }\n FOR(i,0,tmp.size())vp.pb(tmp[i]);\n sort(all(vp));\n Unique(vp);\n DualGraph dg=DualGraph(vp);\n FOR(i,0,n){\n vector<pair<double,int> > list;\n FOR(j,0,vp.size()){\n double rarg=arg(vp[j]-vc[i].c);\n if(rarg<-eps)rarg+=2.0*pi;\n if(on(vc[i],vp[j]))list.pb(mp(rarg,j));\n }\n sort(all(list));\n FOR(j,0,list.size()){\n int a=list[j].s,b=list[(j+1)%list.size()].s;\n dg.add_edge(a,b);\n }\n }\n v=dg.dual();\n v=remove(v);\n if(1<k)return v.size();\n return cal();\n}\n\nint main()\n{\n cin>>n>>k;\n vc.resize(n);\n FOR(i,0,n)cin>>vc[i].c.x>>vc[i].c.y>>vc[i].r;\n cout<<solve()<<endl;\n return 0;\n}", "accuracy": 1, "time_ms": 110, "memory_kb": 4556, "score_of_the_acc": -0.3113, "final_rank": 3 }, { "submission_id": "aoj_2238_2266320", "code_snippet": "#include<bits/stdc++.h>\n#define MAX 1200\n#define inf 1<<29\n#define linf (1e16)\n#define eps (1e-8)\n#define Eps (1e-15)\n#define mod 1000000007\n#define pi acos(-1.0)\n#define phi (1.0+sqrt(5.0))/2.0\n#define f first\n#define s second\n#define mp make_pair\n#define pb push_back\n#define all(a) (a).begin(),(a).end()\n#define pd(a) printf(\"%.10f\\n\",(double)(a))\n#define pld(a) printf(\"%.10Lf\\n\",(ld)(a))\n#define FOR(i,a,b) for(int i=(a);i<(b);i++)\n#define RFOR(i,a,b) for(int i=(a)-1;(b)<=i;i--)\n#define Unique(v) v.erase(unique(all(v)),v.end())\n#define equals(a,b) (fabs((a)-(b))<eps)\nusing namespace std;\ntypedef long double ld;\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef pair<int,int> pii;\ntypedef pair<int,double> pid;\ntypedef pair<double,int> pdi;\ntypedef pair<double,double> pdd;\ntypedef vector<int> vi;\ntypedef vector<pii> vpi;\n\nclass Point{\npublic:\n double x,y;\n Point(double x=0,double y=0):x(x),y(y){}\n\n Point operator+(Point p){ return Point(x+p.x,y+p.y);}\n Point operator-(Point p){ return Point(x-p.x,y-p.y);}\n Point operator*(double k){ return Point(x*k,y*k);}\n Point operator/(double k){ return Point(x/k,y/k);}\n bool operator<(Point p)const{ \n return equals(x,p.x) ? y-p.y<-eps : x-p.x<-eps; }\n bool operator==(Point p)const{ \n return fabs(x-p.x)<eps && fabs(y-p.y)<eps;}\n\n double abs(){ return sqrt(norm());}\n double norm(){ return (x*x+y*y);}\n};\ntypedef Point Vector;\ntypedef vector<Point> Polygon;\n\nclass Segment{\npublic:\n Point p1,p2;\n Segment(Point p1=Point(),Point p2=Point()):p1(p1),p2(p2){}\n};\ntypedef Segment Line;\n\nclass Circle{\npublic:\n Point c;\n double r;\n Circle(Point c=Point(),double r=0.0):c(c),r(r){}\n};\n\ndouble norm(Vector a){ return (a.x*a.x+a.y*a.y);}\ndouble abs(Vector a){ return sqrt(norm(a));}\ndouble dot(Vector a,Vector b){ return (a.x*b.x+a.y*b.y);}\ndouble cross(Vector a,Vector b){ return (a.x*b.y-a.y*b.x);}\ndouble arg(Vector p){ return atan2(p.y,p.x);}\nVector polar(double a,double r){ return Point(cos(r)*a,sin(r)*a);}\n\nclass Dedge{\n public:\n int s,t,f,l;\n ld r;\n Dedge(int s,int t,ld r):s(s),t(t),f(0),l(0){\n while(pi<r)r-=2.0*pi;\n while(r<-pi)r+=2.0*pi;\n this->r=r;\n }\n bool operator==(Dedge e)const{ return t==e.t; }\n bool operator<(Dedge e)const{ return r>e.r; }\n bool operator<(ld R)const{ return r>R; }\n};\n\nclass DualGraph{\n public:\n int n;\n vector<Point> v;\n vector<Polygon> P;\n vector<vector<Dedge> > g;\n\n DualGraph(vector<Point> v):v(v),g(v.size()),n(v.size()){}\n\n void add_edge(int s,int t){\n ld r=arg(v[t]-v[s]);\n g[s].pb(Dedge(s,t,r));\n g[t].pb(Dedge(t,s,r+pi));\n }\n\n void add_polygon(int s,int t,ld r){\n vector<Dedge>::iterator e=lower_bound(all(g[s]),r-eps);\n if(e==g[s].end())e=g[s].begin();\n if(e->f)return;\n e->f=1;\n e->l=t;\n P[t].pb(v[s]);\n add_polygon(e->t, t, e->r > 0 ? e->r-pi : e->r+pi);\n }\n\n vector<Polygon> dual(){\n FOR(i,0,n){ sort(all(g[i])); Unique(g[i]); }\n FOR(i,0,n){\n FOR(j,0,g[i].size()){\n if(!g[i][j].f){\n P.pb(Polygon());\n add_polygon(i,P.size()-1,g[i][j].r+eps);\n }\n }\n }\n return P;\n }\n};\n\nbool on(Circle c,Point p){ return equals(abs(c.c-p),c.r); }\n\nint intersect(Circle a,Circle b){\n double d=abs(a.c-b.c);\n if(a.r<b.r)swap(a,b);\n if(a.r+b.r+eps<d)return 2;\n if(a.r+b.r-eps<d)return 1;\n if(a.r-b.r+eps<d)return 0;\n if(a.r-b.r-eps<d)return -1;\n return -2;\n}\n\npair<Point,Point> getCrossPoints(Circle c1,Circle c2){\n double d=abs(c1.c-c2.c);\n double a=acos((c1.r*c1.r+d*d-c2.r*c2.r)/(2.0*c1.r*d));\n double t=arg(c2.c-c1.c);\n return mp(c1.c+polar(c1.r,t+a),c1.c+polar(c1.r,t-a));\n}\n\nPoint rotate(Point base,Point a,double r){\n Point b=a-base;\n a.x=b.x*cos((r/180.0)*pi)-b.y*sin((r/180.0)*pi);\n a.y=b.x*sin((r/180.0)*pi)+b.y*cos((r/180.0)*pi);\n a=a+base;\n return a;\n}\n\ndouble getAngle(Vector a,Vector b){\n double tmp=dot(a,b)/(abs(a)*abs(b));\n if(tmp<-1.0)tmp=-1.0;\n if(1.0<tmp)tmp=1.0;\n double r=acos(tmp)*180.0/pi;\n if(cross(a,b)<-eps)r=360.0-r;\n return r;\n}\n\ndouble Area(Polygon p){\n double area=0.0;\n int n=p.size();\n for(int i=0;i<n;i++)area+=cross(p[i],p[(i+1)%n]);\n return area/2.0;\n}\n\nint contains(Polygon g,Point p){\n int n=g.size();\n bool x=false;\n for(int i=0;i<n;i++){\n Vector a=g[i]-p,b=g[(i+1)%n]-p;\n if(abs(cross(a,b))<eps && dot(a,b)<eps)return 1;\n if(a.y>b.y)swap(a,b);\n if(a.y<eps && eps<b.y && cross(a,b)>eps)x=!x;\n }\n if(x)return 2;\n return 0;\n}\n\nvector<Polygon> remove(vector<Polygon> p){\n vector<Polygon> res;\n FOR(i,0,p.size())if(eps<Area(p[i]))res.pb(p[i]);\n return res;\n}\n\nstruct edge{ int to,cap,rev; };\n\nint n,k;\nvector<Circle> vc;\nvector<Polygon> v;\nvector<edge> G[MAX];\nbool used[MAX];\nbool in[MAX][MAX];\nbool adj[MAX][MAX];\n\nvoid add_edge(int from,int to,int cap){\n G[from].push_back((edge){to,cap,(int)G[to].size()});\n G[to].push_back((edge){from,0,(int)G[from].size()-1});\n}\n \nint dfs(int v,int t,int f){\n if(v==t)return f;\n used[v]=true;\n for(int i=0;i<G[v].size();i++){\n edge &e=G[v][i];\n if(!used[e.to] && e.cap>0){\n int d=dfs(e.to,t,min(f,e.cap));\n if(d>0){\n e.cap-=d;\n G[e.to][e.rev].cap+=d;\n return d;\n }\n }\n }\n return 0;\n}\n \nint max_flow(int s,int t){\n int flow=0;\n for(;;){\n fill(used,used+MAX,0);\n int f=dfs(s,t,inf);\n if(f==0)return flow;\n flow+=f;\n }\n}\n\nbool same(Point a,Point b,Point c,Point d){\n return ((a==c && b==d) || (a==d && b==c));\n}\n\nbool check1(Polygon a,Polygon b){\n FOR(i,0,a.size())\n FOR(j,0,b.size())\n if(same(a[i],a[(i+1)%a.size()],b[j],b[(j+1)%b.size()]))\n return true;\n return false;\n}\n\nbool check2(Polygon a,Polygon b){\n FOR(i,0,a.size())\n if(contains(b,a[i])!=2)return false;\n return true;\n}\n\nint cal(){\n int V=v.size();\n int s=2*V,t=s+1;\n FOR(i,0,V){\n add_edge(s,i,1);\n add_edge(i+V,t,1);\n }\n FOR(i,0,V){\n FOR(j,0,V){\n if(i==j)continue;\n adj[i][j]=check1(v[i],v[j]);\n in[i][j]=check2(v[i],v[j]);\n }\n }\n FOR(i,0,V)\n FOR(j,0,V)\n if(in[i][j])\n FOR(k,0,V)\n if(in[i][k] && in[k][j])\n in[i][j]=false;\n\n FOR(i,0,V)\n FOR(j,0,V)\n if(adj[i][j] || in[i][j] || in[j][i])add_edge(i,j+V,1);\n return V-max_flow(s,t)/2;\n}\n\nint solve(){\n vector<Point> vp,tmp;\n FOR(i,0,n){\n vp.pb(Point(vc[i].c.x+vc[i].r,vc[i].c.y));\n vp.pb(Point(vc[i].c.x-vc[i].r,vc[i].c.y));\n vp.pb(Point(vc[i].c.x,vc[i].c.y+vc[i].r));\n vp.pb(Point(vc[i].c.x,vc[i].c.y-vc[i].r));\n FOR(j,i+1,n){\n if(abs(intersect(vc[i],vc[j]))==2)continue;\n pair<Point,Point> pp=getCrossPoints(vc[i],vc[j]);\n vp.pb(pp.f);\n vp.pb(pp.s);\n }\n }\n FOR(i,0,n){\n vector<pair<double,Point> > list;\n FOR(j,0,vp.size()){\n double rarg=arg(vp[j]-vc[i].c);\n if(rarg<-eps)rarg+=2.0*pi;\n if(on(vc[i],vp[j]))list.pb(mp(rarg,vp[j]));\n }\n sort(all(list));\n FOR(j,0,list.size()){\n Point a=list[j].s,b=list[(j+1)%list.size()].s;\n double r=getAngle(a-vc[i].c,b-vc[i].c);\n tmp.pb(rotate(vc[i].c,a,r/2.0));\n }\n }\n FOR(i,0,tmp.size())vp.pb(tmp[i]);\n sort(all(vp));\n Unique(vp);\n DualGraph dg=DualGraph(vp);\n FOR(i,0,n){\n vector<pair<double,int> > list;\n FOR(j,0,vp.size()){\n double rarg=arg(vp[j]-vc[i].c);\n if(rarg<-eps)rarg+=2.0*pi;\n if(on(vc[i],vp[j]))list.pb(mp(rarg,j));\n }\n sort(all(list));\n FOR(j,0,list.size()){\n int a=list[j].s,b=list[(j+1)%list.size()].s;\n dg.add_edge(a,b);\n }\n }\n v=dg.dual();\n v=remove(v);\n if(1<k)return v.size();\n return cal();\n}\n\nint main()\n{\n cin>>n>>k;\n vc.resize(n);\n FOR(i,0,n)cin>>vc[i].c.x>>vc[i].c.y>>vc[i].r;\n cout<<solve()<<endl;\n return 0;\n}", "accuracy": 1, "time_ms": 110, "memory_kb": 4660, "score_of_the_acc": -0.3165, "final_rank": 4 }, { "submission_id": "aoj_2238_2266308", "code_snippet": "#include<bits/stdc++.h>\n#define MAX 32\n#define inf 1<<29\n#define linf (1e16)\n#define eps (1e-10)\n#define Eps (1e-15)\n#define mod 1000000007\n#define pi acos(-1.0)\n#define phi (1.0+sqrt(5.0))/2.0\n#define f first\n#define s second\n#define mp make_pair\n#define pb push_back\n#define all(a) (a).begin(),(a).end()\n#define pd(a) printf(\"%.10f\\n\",(double)(a))\n#define pld(a) printf(\"%.10Lf\\n\",(ld)(a))\n#define FOR(i,a,b) for(int i=(a);i<(b);i++)\n#define RFOR(i,a,b) for(int i=(a)-1;(b)<=i;i--)\n#define Unique(v) v.erase(unique(all(v)),v.end())\n#define equals(a,b) (fabs((a)-(b))<eps)\nusing namespace std;\ntypedef long double ld;\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef pair<int,int> pii;\ntypedef pair<int,double> pid;\ntypedef pair<double,int> pdi;\ntypedef pair<double,double> pdd;\ntypedef vector<int> vi;\ntypedef vector<pii> vpi;\n\nclass Point{\npublic:\n double x,y;\n Point(double x=0,double y=0):x(x),y(y){}\n\n Point operator+(Point p){ return Point(x+p.x,y+p.y);}\n Point operator-(Point p){ return Point(x-p.x,y-p.y);}\n Point operator*(double k){ return Point(x*k,y*k);}\n Point operator/(double k){ return Point(x/k,y/k);}\n bool operator<(Point p)const{ \n return equals(x,p.x) ? y-p.y<-eps : x-p.x<-eps; }\n bool operator==(Point p)const{ \n return fabs(x-p.x)<eps && fabs(y-p.y)<eps;}\n\n double abs(){ return sqrt(norm());}\n double norm(){ return (x*x+y*y);}\n};\ntypedef Point Vector;\ntypedef vector<Point> Polygon;\n\nclass Segment{\npublic:\n Point p1,p2;\n Segment(Point p1=Point(),Point p2=Point()):p1(p1),p2(p2){}\n};\ntypedef Segment Line;\n\nclass Circle{\npublic:\n Point c;\n double r;\n Circle(Point c=Point(),double r=0.0):c(c),r(r){}\n};\n\ndouble norm(Vector a){ return (a.x*a.x+a.y*a.y);}\ndouble abs(Vector a){ return sqrt(norm(a));}\ndouble dot(Vector a,Vector b){ return (a.x*b.x+a.y*b.y);}\ndouble cross(Vector a,Vector b){ return (a.x*b.y-a.y*b.x);}\ndouble arg(Vector p){ return atan2(p.y,p.x);}\nVector polar(double a,double r){ return Point(cos(r)*a,sin(r)*a);}\n\nclass Dedge{\n public:\n int s,t,f,l;\n ld r;\n Dedge(int s,int t,ld r):s(s),t(t),f(0),l(0){\n while(pi<r)r-=2.0*pi;\n while(r<-pi)r+=2.0*pi;\n this->r=r;\n }\n bool operator==(Dedge e)const{ return t==e.t; }\n bool operator<(Dedge e)const{ return r>e.r; }\n bool operator<(ld R)const{ return r>R; }\n};\n\nclass DualGraph{\n public:\n int n;\n vector<Point> v;\n vector<Polygon> P;\n vector<vector<Dedge> > g;\n\n DualGraph(vector<Point> v):v(v),g(v.size()),n(v.size()){}\n\n void add_edge(int s,int t){\n ld r=arg(v[t]-v[s]);\n g[s].pb(Dedge(s,t,r));\n g[t].pb(Dedge(t,s,r+pi));\n }\n\n void add_polygon(int s,int t,ld r){\n vector<Dedge>::iterator e=lower_bound(all(g[s]),r-eps);\n if(e==g[s].end())e=g[s].begin();\n if(e->f)return;\n e->f=1;\n e->l=t;\n P[t].pb(v[s]);\n add_polygon(e->t, t, e->r > 0 ? e->r-pi : e->r+pi);\n }\n\n vector<Polygon> dual(){\n FOR(i,0,n){ sort(all(g[i])); Unique(g[i]); }\n FOR(i,0,n){\n FOR(j,0,g[i].size()){\n if(!g[i][j].f){\n P.pb(Polygon());\n add_polygon(i,P.size()-1,g[i][j].r+eps);\n }\n }\n }\n return P;\n }\n};\n\nbool on(Circle c,Point p){ return equals(abs(c.c-p),c.r); }\n\nint intersect(Circle a,Circle b){\n double d=abs(a.c-b.c);\n if(a.r<b.r)swap(a,b);\n if(a.r+b.r+eps<d)return 2;\n if(a.r+b.r-eps<d)return 1;\n if(a.r-b.r+eps<d)return 0;\n if(a.r-b.r-eps<d)return -1;\n return -2;\n}\n\npair<Point,Point> getCrossPoints(Circle c1,Circle c2){\n double d=abs(c1.c-c2.c);\n double a=acos((c1.r*c1.r+d*d-c2.r*c2.r)/(2.0*c1.r*d));\n double t=arg(c2.c-c1.c);\n return mp(c1.c+polar(c1.r,t+a),c1.c+polar(c1.r,t-a));\n}\n\nPoint rotate(Point base,Point a,double r){\n Point b=a-base;\n a.x=b.x*cos((r/180.0)*pi)-b.y*sin((r/180.0)*pi);\n a.y=b.x*sin((r/180.0)*pi)+b.y*cos((r/180.0)*pi);\n a=a+base;\n return a;\n}\n\ndouble getAngle(Vector a,Vector b){\n double tmp=dot(a,b)/(abs(a)*abs(b));\n if(tmp<-1.0)tmp=-1.0;\n if(1.0<tmp)tmp=1.0;\n double r=acos(tmp)*180.0/pi;\n if(cross(a,b)<-eps)r=360.0-r;\n return r;\n}\n\ndouble Area(Polygon p){\n double area=0.0;\n int n=p.size();\n for(int i=0;i<n;i++)area+=cross(p[i],p[(i+1)%n]);\n return area/2.0;\n}\n\nint contains(Polygon g,Point p){\n int n=g.size();\n bool x=false;\n for(int i=0;i<n;i++){\n Vector a=g[i]-p,b=g[(i+1)%n]-p;\n if(abs(cross(a,b))<eps && dot(a,b)<eps)return 1;\n if(a.y>b.y)swap(a,b);\n if(a.y<eps && eps<b.y && cross(a,b)>eps)x=!x;\n }\n if(x)return 2;\n return 0;\n}\n\nvector<Polygon> remove(vector<Polygon> p){\n vector<Polygon> res;\n FOR(i,0,p.size())if(eps<Area(p[i]))res.pb(p[i]);\n return res;\n}\n\nvoid P(Polygon p){\n cout<<endl<<p.size()<<endl;\n FOR(i,0,p.size())cout<<p[i].x<<\" \"<<p[i].y<<endl;\n}\n\nstruct edge{ int to,cap,rev; };\n\nint n,k;\nvector<Circle> vc;\nvector<Polygon> v;\nvector<edge> G[2002];\nbool used[2002];\nbool in[2002][2002];\nbool adj[2002][2002];\n\nvoid add_edge(int from,int to,int cap){\n G[from].push_back((edge){to,cap,(int)G[to].size()});\n G[to].push_back((edge){from,0,(int)G[from].size()-1});\n}\n \nint dfs(int v,int t,int f){\n if(v==t)return f;\n used[v]=true;\n for(int i=0;i<G[v].size();i++){\n edge &e=G[v][i];\n if(!used[e.to] && e.cap>0){\n int d=dfs(e.to,t,min(f,e.cap));\n if(d>0){\n e.cap-=d;\n G[e.to][e.rev].cap+=d;\n return d;\n }\n }\n }\n return 0;\n}\n \nint max_flow(int s,int t){\n int flow=0;\n for(;;){\n fill(used,used+2002,0);\n int f=dfs(s,t,inf);\n if(f==0)return flow;\n flow+=f;\n }\n}\n\nbool same(Point a,Point b,Point c,Point d){\n return ((a==c && b==d) || (a==d && b==c));\n}\n\nbool check1(Polygon a,Polygon b){\n FOR(i,0,a.size())\n FOR(j,0,b.size())\n if(same(a[i],a[(i+1)%a.size()],b[j],b[(j+1)%b.size()]))\n return true;\n return false;\n}\n\nbool check2(Polygon a,Polygon b){\n FOR(i,0,a.size())\n if(contains(b,a[i])!=2)return false;\n return true;\n}\n\nint cal(){\n int V=v.size();\n int s=2*V,t=s+1;\n FOR(i,0,V){\n add_edge(s,i,1);\n add_edge(i+V,t,1);\n }\n FOR(i,0,V){\n FOR(j,0,V){\n if(i==j)continue;\n adj[i][j]=check1(v[i],v[j]);\n in[i][j]=check2(v[i],v[j]);\n// if(in[i][j])P(v[i]);\n }\n }\n\n FOR(i,0,V)\n FOR(j,0,V)\n if(in[i][j])\n FOR(k,0,V)\n if(in[i][j] && in[i][k] && in[k][j])\n in[i][j]=false;\n\n FOR(i,0,V)\n FOR(j,0,V)\n if(adj[i][j] || in[i][j] || in[j][i])add_edge(i,j+V,1);\n return V-max_flow(s,t)/2;\n}\n\nint solve(){\n vector<Point> vp,tmp;\n FOR(i,0,n){\n vp.pb(Point(vc[i].c.x+vc[i].r,vc[i].c.y));\n vp.pb(Point(vc[i].c.x-vc[i].r,vc[i].c.y));\n vp.pb(Point(vc[i].c.x,vc[i].c.y+vc[i].r));\n vp.pb(Point(vc[i].c.x,vc[i].c.y-vc[i].r));\n FOR(j,i+1,n){\n if(abs(intersect(vc[i],vc[j]))==2)continue;\n pair<Point,Point> pp=getCrossPoints(vc[i],vc[j]);\n vp.pb(pp.f);\n vp.pb(pp.s);\n }\n }\n FOR(i,0,n){\n vector<pair<double,Point> > list;\n FOR(j,0,vp.size()){\n double rarg=arg(vp[j]-vc[i].c);\n if(rarg<-eps)rarg+=2.0*pi;\n if(on(vc[i],vp[j]))list.pb(mp(rarg,vp[j]));\n }\n sort(all(list));\n FOR(j,0,list.size()){\n Point a=list[j].s,b=list[(j+1)%list.size()].s;\n double r=getAngle(a-vc[i].c,b-vc[i].c);\n tmp.pb(rotate(vc[i].c,a,r/2.0));\n }\n }\n FOR(i,0,tmp.size())vp.pb(tmp[i]);\n sort(all(vp));\n Unique(vp);\n DualGraph dg=DualGraph(vp);\n FOR(i,0,n){\n vector<pair<double,int> > list;\n FOR(j,0,vp.size()){\n double rarg=arg(vp[j]-vc[i].c);\n if(rarg<-eps)rarg+=2.0*pi;\n if(on(vc[i],vp[j]))list.pb(mp(rarg,j));\n }\n sort(all(list));\n FOR(j,0,list.size()){\n int a=list[j].s,b=list[(j+1)%list.size()].s;\n dg.add_edge(a,b);\n }\n }\n v=dg.dual();\n v=remove(v);\n if(1<k)return v.size();\n return cal();\n}\n\nint main()\n{\n cin>>n>>k;\n vc.resize(n);\n FOR(i,0,n)cin>>vc[i].c.x>>vc[i].c.y>>vc[i].r;\n cout<<solve()<<endl;\n return 0;\n}", "accuracy": 1, "time_ms": 110, "memory_kb": 5084, "score_of_the_acc": -0.3379, "final_rank": 5 }, { "submission_id": "aoj_2238_2264940", "code_snippet": "// Go to hell\n\n#include<bits/stdc++.h>\n#define MAX 32\n#define inf 1<<29\n#define linf (1e16)\n#define eps (1e-6)\n#define Eps (1e-15)\n#define mod 1000000007\n#define pi acos(-1.0)\n#define phi (1.0+sqrt(5.0))/2.0\n#define f first\n#define s second\n#define mp make_pair\n#define pb push_back\n#define all(a) (a).begin(),(a).end()\n#define pd(a) printf(\"%.10f\\n\",(double)(a))\n#define pld(a) printf(\"%.10Lf\\n\",(ld)(a))\n#define FOR(i,a,b) for(int i=(a);i<(b);i++)\n#define RFOR(i,a,b) for(int i=(a)-1;(b)<=i;i--)\n#define Unique(v) v.erase(unique(all(v)),v.end())\n#define equals(a,b) (fabs((a)-(b))<eps)\nusing namespace std;\ntypedef long double ld;\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef pair<int,int> pii;\ntypedef pair<int,double> pid;\ntypedef pair<double,int> pdi;\ntypedef pair<double,double> pdd;\ntypedef vector<int> vi;\ntypedef vector<pii> vpi;\n\nclass Point{\npublic:\n double x,y;\n Point(double x=0,double y=0):x(x),y(y){}\n\n Point operator+(Point p){ return Point(x+p.x,y+p.y);}\n Point operator-(Point p){ return Point(x-p.x,y-p.y);}\n Point operator*(double k){ return Point(x*k,y*k);}\n Point operator/(double k){ return Point(x/k,y/k);}\n bool operator<(Point p)const{ \n return equals(x,p.x) ? y-p.y<-eps : x-p.x<-eps; }\n bool operator==(Point p)const{ \n return fabs(x-p.x)<eps && fabs(y-p.y)<eps;}\n\n double abs(){ return sqrt(norm());}\n double norm(){ return (x*x+y*y);}\n};\ntypedef Point Vector;\ntypedef vector<Point> Polygon;\n\nclass Segment{\npublic:\n Point p1,p2;\n Segment(Point p1=Point(),Point p2=Point()):p1(p1),p2(p2){}\n};\ntypedef Segment Line;\n\nclass Circle{\npublic:\n Point c;\n double r;\n Circle(Point c=Point(),double r=0.0):c(c),r(r){}\n};\n\ndouble norm(Vector a){ return (a.x*a.x+a.y*a.y);}\ndouble abs(Vector a){ return sqrt(norm(a));}\ndouble dot(Vector a,Vector b){ return (a.x*b.x+a.y*b.y);}\ndouble cross(Vector a,Vector b){ return (a.x*b.y-a.y*b.x);}\ndouble arg(Vector p){ return atan2(p.y,p.x);}\nVector polar(double a,double r){ return Point(cos(r)*a,sin(r)*a);}\n\nclass Dedge{\n public:\n int s,t,f,l;\n ld r;\n Dedge(int s,int t,ld r):s(s),t(t),f(0),l(0){\n while(pi<r)r-=2.0*pi;\n while(r<-pi)r+=2.0*pi;\n this->r=r;\n }\n bool operator==(Dedge e)const{ return t==e.t; }\n bool operator<(Dedge e)const{ return r>e.r; }\n bool operator<(ld R)const{ return r>R; }\n};\n\nclass DualGraph{\n public:\n int n;\n vector<Point> v;\n vector<Polygon> P;\n vector<vector<Dedge> > g;\n\n DualGraph(vector<Point> v):v(v),g(v.size()),n(v.size()){}\n\n void add_edge(int s,int t){\n ld r=arg(v[t]-v[s]);\n g[s].pb(Dedge(s,t,r));\n g[t].pb(Dedge(t,s,r+pi));\n }\n\n void add_polygon(int s,int t,ld r){\n vector<Dedge>::iterator e=lower_bound(all(g[s]),r-eps);\n if(e==g[s].end())e=g[s].begin();\n if(e->f)return;\n e->f=1;\n e->l=t;\n P[t].pb(v[s]);\n add_polygon(e->t, t, e->r > 0 ? e->r-pi : e->r+pi);\n }\n\n vector<Polygon> dual(){\n FOR(i,0,n){ sort(all(g[i])); Unique(g[i]); }\n FOR(i,0,n){\n FOR(j,0,g[i].size()){\n if(!g[i][j].f){\n P.pb(Polygon());\n add_polygon(i,P.size()-1,g[i][j].r+eps);\n }\n }\n }\n return P;\n }\n};\n\nbool on(Circle c,Point p){ return equals(abs(c.c-p),c.r); }\n\nint intersect(Circle a,Circle b){\n double d=abs(a.c-b.c);\n if(a.r<b.r)swap(a,b);\n if(a.r+b.r+eps<=d)return 2;\n if(a.r+b.r-eps<=d)return 1;\n if(a.r-b.r+eps<=d)return 0;\n if(a.r-b.r-eps<=d)return -1;\n return -2;\n}\n\npair<Point,Point> getCrossPoints(Circle c1,Circle c2){\n double d=abs(c1.c-c2.c);\n double a=acos((c1.r*c1.r+d*d-c2.r*c2.r)/(2.0*c1.r*d));\n double t=arg(c2.c-c1.c);\n return mp(c1.c+polar(c1.r,t+a),c1.c+polar(c1.r,t-a));\n}\n\nPoint rotate(Point base,Point a,double r){\n Point b=a-base;\n a.x=b.x*cos((r/180.0)*pi)-b.y*sin((r/180.0)*pi);\n a.y=b.x*sin((r/180.0)*pi)+b.y*cos((r/180.0)*pi);\n a=a+base;\n return a;\n}\n\ndouble getAngle(Vector a,Vector b){\n double tmp=dot(a,b)/(abs(a)*abs(b));\n if(tmp<-1.0)tmp=-1.0;\n if(1.0<tmp)tmp=1.0;\n double r=acos(tmp)*180.0/pi;\n if(cross(a,b)<-eps)r=360.0-r;\n return r;\n}\n\ndouble Area(Polygon p){\n double area=0.0;\n int n=p.size();\n for(int i=0;i<n;i++)area+=cross(p[i],p[(i+1)%n]);\n return area/2.0;\n}\n\nint contains(Polygon g,Point p){\n int n=g.size();\n bool x=false;\n for(int i=0;i<n;i++){\n Vector a=g[i]-p,b=g[(i+1)%n]-p;\n if(abs(cross(a,b))<eps && dot(a,b)<eps)return 1;\n if(a.y>b.y)swap(a,b);\n if(a.y<eps && eps<b.y && cross(a,b)>eps)x=!x;\n }\n if(x)return 2;\n return 0;\n}\n\nvector<Polygon> remove(vector<Polygon> p){\n vector<Polygon> res;\n FOR(i,0,p.size())if(eps<Area(p[i]))res.pb(p[i]);\n return res;\n}\n\nvoid P(Polygon p){\n cout<<endl<<p.size()<<endl;\n FOR(i,0,p.size())cout<<p[i].x<<\" \"<<p[i].y<<endl;\n}\n\nstruct edge{ int to,cap,rev; };\n\nint n,k;\nvector<Circle> vc;\nvector<Polygon> v;\nvector<edge> G[2002];\nbool used[2002];\nbool in[2002][2002];\nbool adj[2002][2002];\n\nvoid add_edge(int from,int to,int cap){\n G[from].push_back((edge){to,cap,(int)G[to].size()});\n G[to].push_back((edge){from,0,(int)G[from].size()-1});\n}\n \nint dfs(int v,int t,int f){\n if(v==t)return f;\n used[v]=true;\n for(int i=0;i<G[v].size();i++){\n edge &e=G[v][i];\n if(!used[e.to] && e.cap>0){\n int d=dfs(e.to,t,min(f,e.cap));\n if(d>0){\n e.cap-=d;\n G[e.to][e.rev].cap+=d;\n return d;\n }\n }\n }\n return 0;\n}\n \nint max_flow(int s,int t){\n int flow=0;\n for(;;){\n memset(used,0,sizeof(used));\n int f=dfs(s,t,inf);\n if(f==0)return flow;\n flow+=f;\n }\n}\n\nbool same(Point a,Point b,Point c,Point d){\n return ((a==c && b==d) || (a==d && b==c));\n}\n\nbool check1(Polygon a,Polygon b){\n FOR(i,0,a.size())\n FOR(j,0,b.size())\n if(same(a[i],a[(i+1)%a.size()],b[j],b[(j+1)%b.size()]))\n return true;\n return false;\n}\n\nbool check2(Polygon a,Polygon b){\n FOR(i,0,a.size())\n if(contains(b,a[i])==0)return false;\n return true;\n}\n\nint cal(){\n int V=v.size();\n int s=2*V,t=s+1;\n FOR(i,0,V)add_edge(s,i,1);\n FOR(i,0,V)add_edge(i+V,t,1);\n FOR(i,0,V){\n FOR(j,0,V){\n if(i==j)continue;\n adj[i][j]=check1(v[i],v[j]);\n in[i][j]=check2(v[i],v[j]);\n }\n }\n FOR(i,0,V)\n FOR(j,0,V)\n if(in[i][j])\n FOR(k,0,V)\n if(in[i][k] && in[k][j])\n in[i][j]=false;\n\n FOR(i,0,V)\n FOR(j,0,V)\n if(adj[i][j] || in[i][j] || in[j][i])add_edge(i,j+V,1);\n return V-max_flow(s,t)/2;\n}\n\nint solve(){\n vector<Point> vp,tmp;\n FOR(i,0,n){\n vp.pb(Point(vc[i].c.x+vc[i].r,vc[i].c.y));\n vp.pb(Point(vc[i].c.x-vc[i].r,vc[i].c.y));\n FOR(j,i+1,n){\n if(abs(intersect(vc[i],vc[j]))==2)continue;\n pair<Point,Point> pp=getCrossPoints(vc[i],vc[j]);\n vp.pb(pp.f);\n vp.pb(pp.s);\n }\n }\n FOR(i,0,n){\n vector<pair<double,Point> > list;\n FOR(j,0,vp.size()){\n double rarg=arg(vp[j]-vc[i].c);\n if(rarg<-eps)rarg+=2.0*pi;\n if(on(vc[i],vp[j]))list.pb(mp(rarg,vp[j]));\n }\n sort(all(list));\n FOR(j,0,list.size()){\n Point a=list[j].s,b=list[(j+1)%list.size()].s;\n double r=getAngle(a-vc[i].c,b-vc[i].c);\n tmp.pb(rotate(vc[i].c,a,r/2.0));\n }\n }\n FOR(i,0,tmp.size())vp.pb(tmp[i]);\n sort(all(vp));\n Unique(vp);\n DualGraph dg=DualGraph(vp);\n FOR(i,0,n){\n vector<pair<double,int> > list;\n FOR(j,0,vp.size()){\n double rarg=arg(vp[j]-vc[i].c);\n if(rarg<-eps)rarg+=2.0*pi;\n if(on(vc[i],vp[j]))list.pb(mp(rarg,j));\n }\n sort(all(list));\n FOR(j,0,list.size()){\n int a=list[j].s,b=list[(j+1)%list.size()].s;\n dg.add_edge(a,b);\n }\n }\n v=dg.dual();\n v=remove(v);\n if(1<k)return v.size();\n return cal();\n}\n\nint main()\n{\n cin>>n>>k;\n vc.resize(n);\n FOR(i,0,n)cin>>vc[i].c.x>>vc[i].c.y>>vc[i].r;\n cout<<solve()<<endl;\n return 0;\n}", "accuracy": 0.425531914893617, "time_ms": 100, "memory_kb": 5192, "score_of_the_acc": -0.3261, "final_rank": 9 }, { "submission_id": "aoj_2238_2260411", "code_snippet": "#include<bits/stdc++.h>\n#define MAX 22\n#define inf 1<<29\n#define linf (1e16)\n#define eps (1e-10)\n#define Eps (1e-15)\n#define mod 1000000007\n#define pi acos(-1.0)\n#define phi (1.0+sqrt(5.0))/2.0\n#define f first\n#define s second\n#define mp make_pair\n#define pb push_back\n#define all(a) (a).begin(),(a).end()\n#define pd(a) printf(\"%.10f\\n\",(double)(a))\n#define pld(a) printf(\"%.10Lf\\n\",(ld)(a))\n#define FOR(i,a,b) for(int i=(a);i<(b);i++)\n#define RFOR(i,a,b) for(int i=(a)-1;(b)<=i;i--)\n#define Unique(v) v.erase(unique(all(v)),v.end())\n#define equals(a,b) (fabs((a)-(b))<eps)\nusing namespace std;\ntypedef long double ld;\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef pair<int,int> pii;\ntypedef pair<int,double> pid;\ntypedef pair<double,int> pdi;\ntypedef pair<double,double> pdd;\ntypedef vector<int> vi;\ntypedef vector<pii> vpi;\n\nclass Point{\npublic:\n double x,y;\n Point(double x=0,double y=0):x(x),y(y){}\n\n Point operator+(Point p){ return Point(x+p.x,y+p.y);}\n Point operator-(Point p){ return Point(x-p.x,y-p.y);}\n Point operator*(double k){ return Point(x*k,y*k);}\n Point operator/(double k){ return Point(x/k,y/k);}\n bool operator<(Point p)const{ \n return equals(x,p.x) ? y-p.y<-eps : x-p.x<-eps; }\n bool operator==(Point p)const{ \n return fabs(x-p.x)<eps && fabs(y-p.y)<eps;}\n\n double abs(){ return sqrt(norm());}\n double norm(){ return (x*x+y*y);}\n};\ntypedef Point Vector;\ntypedef vector<Point> Polygon;\n\nclass Segment{\npublic:\n Point p1,p2;\n Segment(Point p1=Point(),Point p2=Point()):p1(p1),p2(p2){}\n};\ntypedef Segment Line;\n\nclass Circle{\npublic:\n Point c;\n double r;\n Circle(Point c=Point(),double r=0.0):c(c),r(r){}\n};\n\ndouble norm(Vector a){ return (a.x*a.x+a.y*a.y);}\ndouble abs(Vector a){ return sqrt(norm(a));}\ndouble dot(Vector a,Vector b){ return (a.x*b.x+a.y*b.y);}\ndouble cross(Vector a,Vector b){ return (a.x*b.y-a.y*b.x);}\ndouble arg(Vector p){ return atan2(p.y,p.x);}\nVector polar(double a,double r){ return Point(cos(r)*a,sin(r)*a);}\n\nclass Dedge{\n public:\n int s,t,f,l;\n ld r;\n Dedge(int s,int t,ld r):s(s),t(t),f(0),l(0){\n while(pi<r)r-=2.0*pi;\n while(r<-pi)r+=2.0*pi;\n this->r=r;\n }\n bool operator==(Dedge e)const{ return t==e.t; }\n bool operator<(Dedge e)const{ return r>e.r; }\n bool operator<(ld R)const{ return r>R; }\n};\n\nclass DualGraph{\n public:\n int n;\n vector<Point> v;\n vector<Polygon> P;\n vector<vector<Dedge> > g;\n\n DualGraph(vector<Point> v):v(v),g(v.size()),n(v.size()){}\n\n void add_edge(int s,int t){\n ld r=arg(v[t]-v[s]);\n g[s].pb(Dedge(s,t,r));\n g[t].pb(Dedge(t,s,r+pi));\n }\n\n void add_polygon(int s,int t,ld r){\n vector<Dedge>::iterator e=lower_bound(all(g[s]),r-eps);\n if(e==g[s].end())e=g[s].begin();\n if(e->f)return;\n e->f=1;\n e->l=t;\n P[t].pb(v[s]);\n add_polygon(e->t, t, e->r > 0 ? e->r-pi : e->r+pi);\n }\n\n vector<Polygon> dual(){\n FOR(i,0,n){ sort(all(g[i])); Unique(g[i]); }\n FOR(i,0,n){\n FOR(j,0,g[i].size()){\n if(!g[i][j].f){\n P.pb(Polygon());\n add_polygon(i,P.size()-1,g[i][j].r+eps);\n }\n }\n }\n return P;\n }\n};\n\n\nbool on(Circle c,Point p){ return equals(abs(c.c-p),c.r); }\n\nint intersect(Circle a,Circle b){\n double d=abs(a.c-b.c);\n if(a.r<b.r)swap(a,b);\n if(a.r+b.r+eps<=d)return 2;\n if(a.r+b.r-eps<=d)return 1;\n if(a.r-b.r+eps<=d)return 0;\n if(a.r-b.r-eps<=d)return -1;\n return -2;\n}\n\npair<Point,Point> getCrossPoints(Circle c1,Circle c2){\n double d=abs(c1.c-c2.c);\n double a=acos((c1.r*c1.r+d*d-c2.r*c2.r)/(2.0*c1.r*d));\n double t=arg(c2.c-c1.c);\n return mp(c1.c+polar(c1.r,t+a),c1.c+polar(c1.r,t-a));\n}\n\nPoint rotate(Point base,Point a,double r){\n Point b=a-base;\n a.x=b.x*cos((r/180.0)*pi)-b.y*sin((r/180.0)*pi);\n a.y=b.x*sin((r/180.0)*pi)+b.y*cos((r/180.0)*pi);\n a=a+base;\n return a;\n}\n\ndouble getAngle(Vector a,Vector b){\n double tmp=dot(a,b)/(abs(a)*abs(b));\n if(tmp<-1.0)tmp=-1.0;\n if(1.0<tmp)tmp=1.0;\n double r=acos(tmp)*180.0/pi;\n if(cross(a,b)<-eps)r=360.0-r;\n return r;\n}\n\ndouble Area(Polygon p){\n double area=0.0;\n int n=p.size();\n for(int i=0;i<n;i++)area+=cross(p[i],p[(i+1)%n]);\n return area/2.0;\n}\n\nint contains(Polygon g,Point p){\n int n=g.size();\n bool x=false;\n for(int i=0;i<n;i++){\n Vector a=g[i]-p,b=g[(i+1)%n]-p;\n if(abs(cross(a,b))<eps && dot(a,b)<eps)return 1;\n if(a.y>b.y)swap(a,b);\n if(a.y<eps && eps<b.y && cross(a,b)>eps)x=!x;\n }\n if(x)return 2;\n return 0;\n}\n\nbool same(Point a,Point b,Point c,Point d){\n return ((a==c && b==d) || (a==d && b==c)); \n}\n\nbool check(Polygon a,Polygon b){\n FOR(i,0,a.size())\n if(contains(b,a[i]+(a[(i+1)%a.size()]-a[i])/2.0)!=0)return true;\n FOR(i,0,b.size())\n if(contains(a,b[i]+(b[(i+1)%b.size()]-b[i])/2.0)!=0)return true;\n return false;\n}\n\nvector<Polygon> remove(vector<Polygon> p){\n vector<Polygon> res;\n FOR(i,0,p.size())if(eps<Area(p[i]))res.pb(p[i]);\n return res;\n}\n\nstruct edge{ int to,cap,rev; };\n\nint n,k;\nvector<Circle> vc;\nvector<edge> G[2002];\nbool used[2002];\n\nvoid add_edge(int from,int to,int cap){\n G[from].push_back((edge){to,cap,(int)G[to].size()});\n G[to].push_back((edge){from,0,(int)G[from].size()-1});\n}\n \nint dfs(int v,int t,int f){\n if(v==t)return f;\n used[v]=true;\n for(int i=0;i<G[v].size();i++){\n edge &e=G[v][i];\n if(!used[e.to] && e.cap>0){\n int d=dfs(e.to,t,min(f,e.cap));\n if(d>0){\n e.cap-=d;\n G[e.to][e.rev].cap+=d;\n return d;\n }\n }\n }\n return 0;\n}\n \nint max_flow(int s,int t){\n int flow=0;\n for(;;){\n memset(used,0,sizeof(used));\n int f=dfs(s,t,inf);\n if(f==0)return flow;\n flow+=f;\n }\n}\n\nint cal(vector<Polygon> vp){\n int V=vp.size();\n int s=2*V,t=s+1;\n FOR(i,0,V)add_edge(s,i,1);\n FOR(i,0,V)add_edge(i+V,t,1);\n FOR(i,0,V){\n FOR(j,0,V){\n if(i==j)continue;\n if(check(vp[i],vp[j]))add_edge(i,j+V,1);\n }\n }\n return V-max_flow(s,t)/2;\n}\n\nint solve(){\n vector<Point> vp,tmp;\n FOR(i,0,n){\n vp.pb(Point(vc[i].c.x+vc[i].r,vc[i].c.y));\n vp.pb(Point(vc[i].c.x-vc[i].r,vc[i].c.y));\n FOR(j,i+1,n){\n if(abs(intersect(vc[i],vc[j]))==2)continue;\n pair<Point,Point> pp=getCrossPoints(vc[i],vc[j]);\n vp.pb(pp.f);\n vp.pb(pp.s);\n }\n }\n FOR(i,0,n){\n vector<pair<double,Point> > list;\n FOR(j,0,vp.size()){\n double rarg=arg(vp[j]-vc[i].c);\n if(rarg<-eps)rarg+=2.0*pi;\n if(on(vc[i],vp[j]))list.pb(mp(rarg,vp[j]));\n }\n sort(all(list));\n FOR(j,0,list.size()){\n Point a=list[j].s,b=list[(j+1)%list.size()].s;\n double r=getAngle(a-vc[i].c,b-vc[i].c);\n tmp.pb(rotate(vc[i].c,a,r/2.0));\n }\n }\n FOR(i,0,tmp.size())vp.pb(tmp[i]);\n sort(all(vp));\n Unique(vp);\n DualGraph dg=DualGraph(vp);\n FOR(i,0,n){\n vector<pair<double,int> > list;\n FOR(j,0,vp.size()){\n double rarg=arg(vp[j]-vc[i].c);\n if(rarg<-eps)rarg+=2.0*pi;\n if(on(vc[i],vp[j]))list.pb(mp(rarg,j));\n }\n sort(all(list));\n FOR(j,0,list.size()){\n int a=list[j].s,b=list[(j+1)%list.size()].s;\n dg.add_edge(a,b);\n }\n }\n vector<Polygon> v=dg.dual();\n v=remove(v);\n if(1<k)return v.size();\n return cal(v);\n}\n\nint main()\n{\n cin>>n>>k;\n vc.resize(n);\n FOR(i,0,n)cin>>vc[i].c.x>>vc[i].c.y>>vc[i].r;\n cout<<solve()<<endl;\n return 0;\n}", "accuracy": 0.425531914893617, "time_ms": 130, "memory_kb": 4224, "score_of_the_acc": -0.329, "final_rank": 10 }, { "submission_id": "aoj_2238_2260394", "code_snippet": "#include<bits/stdc++.h>\n#define MAX 22\n#define inf 1<<29\n#define linf (1e16)\n#define eps (1e-8)\n#define Eps (1e-15)\n#define mod 1000000007\n#define pi acos(-1.0)\n#define phi (1.0+sqrt(5.0))/2.0\n#define f first\n#define s second\n#define mp make_pair\n#define pb push_back\n#define all(a) (a).begin(),(a).end()\n#define pd(a) printf(\"%.10f\\n\",(double)(a))\n#define pld(a) printf(\"%.10Lf\\n\",(ld)(a))\n#define FOR(i,a,b) for(int i=(a);i<(b);i++)\n#define RFOR(i,a,b) for(int i=(a)-1;(b)<=i;i--)\n#define Unique(v) v.erase(unique(all(v)),v.end())\n#define equals(a,b) (fabs((a)-(b))<eps)\nusing namespace std;\ntypedef long double ld;\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef pair<int,int> pii;\ntypedef pair<int,double> pid;\ntypedef pair<double,int> pdi;\ntypedef pair<double,double> pdd;\ntypedef vector<int> vi;\ntypedef vector<pii> vpi;\n\nclass Point{\npublic:\n double x,y;\n Point(double x=0,double y=0):x(x),y(y){}\n\n Point operator+(Point p){ return Point(x+p.x,y+p.y);}\n Point operator-(Point p){ return Point(x-p.x,y-p.y);}\n Point operator*(double k){ return Point(x*k,y*k);}\n Point operator/(double k){ return Point(x/k,y/k);}\n bool operator<(Point p)const{ \n return equals(x,p.x) ? y-p.y<-eps : x-p.x<-eps; }\n bool operator==(Point p)const{ \n return fabs(x-p.x)<eps && fabs(y-p.y)<eps;}\n\n double abs(){ return sqrt(norm());}\n double norm(){ return (x*x+y*y);}\n};\ntypedef Point Vector;\ntypedef vector<Point> Polygon;\n\nclass Segment{\npublic:\n Point p1,p2;\n Segment(Point p1=Point(),Point p2=Point()):p1(p1),p2(p2){}\n};\ntypedef Segment Line;\n\nclass Circle{\npublic:\n Point c;\n double r;\n Circle(Point c=Point(),double r=0.0):c(c),r(r){}\n};\n\ndouble norm(Vector a){ return (a.x*a.x+a.y*a.y);}\ndouble abs(Vector a){ return sqrt(norm(a));}\ndouble dot(Vector a,Vector b){ return (a.x*b.x+a.y*b.y);}\ndouble cross(Vector a,Vector b){ return (a.x*b.y-a.y*b.x);}\ndouble arg(Vector p){ return atan2(p.y,p.x);}\nVector polar(double a,double r){ return Point(cos(r)*a,sin(r)*a);}\n\nclass Dedge{\n public:\n int s,t,f,l;\n ld r;\n Dedge(int s,int t,ld r):s(s),t(t),f(0),l(0){\n while(pi<r)r-=2.0*pi;\n while(r<-pi)r+=2.0*pi;\n this->r=r;\n }\n bool operator==(Dedge e)const{ return t==e.t; }\n bool operator<(Dedge e)const{ return r>e.r; }\n bool operator<(ld R)const{ return r>R; }\n};\n\nclass DualGraph{\n public:\n int n;\n vector<Point> v;\n vector<Polygon> P;\n vector<vector<Dedge> > g;\n\n DualGraph(vector<Point> v):v(v),g(v.size()),n(v.size()){}\n\n void add_edge(int s,int t){\n ld r=arg(v[t]-v[s]);\n g[s].pb(Dedge(s,t,r));\n g[t].pb(Dedge(t,s,r+pi));\n }\n\n void add_polygon(int s,int t,ld r){\n vector<Dedge>::iterator e=lower_bound(all(g[s]),r-eps);\n if(e==g[s].end())e=g[s].begin();\n if(e->f)return;\n e->f=1;\n e->l=t;\n P[t].pb(v[s]);\n add_polygon(e->t, t, e->r > 0 ? e->r-pi : e->r+pi);\n }\n\n vector<Polygon> dual(){\n FOR(i,0,n){ sort(all(g[i])); Unique(g[i]); }\n FOR(i,0,n){\n FOR(j,0,g[i].size()){\n if(!g[i][j].f){\n P.pb(Polygon());\n add_polygon(i,P.size()-1,g[i][j].r+eps);\n }\n }\n }\n return P;\n }\n};\n\n\nbool on(Circle c,Point p){ return equals(abs(c.c-p),c.r); }\n\nint intersect(Circle a,Circle b){\n double d=abs(a.c-b.c);\n if(a.r<b.r)swap(a,b);\n if(a.r+b.r+eps<=d)return 2;\n if(a.r+b.r-eps<=d)return 1;\n if(a.r-b.r+eps<=d)return 0;\n if(a.r-b.r-eps<=d)return -1;\n return -2;\n}\n\npair<Point,Point> getCrossPoints(Circle c1,Circle c2){\n double d=abs(c1.c-c2.c);\n double a=acos((c1.r*c1.r+d*d-c2.r*c2.r)/(2.0*c1.r*d));\n double t=arg(c2.c-c1.c);\n return mp(c1.c+polar(c1.r,t+a),c1.c+polar(c1.r,t-a));\n}\n\nPoint rotate(Point base,Point a,double r){\n Point b=a-base;\n a.x=b.x*cos((r/180.0)*pi)-b.y*sin((r/180.0)*pi);\n a.y=b.x*sin((r/180.0)*pi)+b.y*cos((r/180.0)*pi);\n a=a+base;\n return a;\n}\n\ndouble getAngle(Vector a,Vector b){\n double tmp=dot(a,b)/(abs(a)*abs(b));\n if(tmp<-1.0)tmp=-1.0;\n if(1.0<tmp)tmp=1.0;\n double r=acos(tmp)*180.0/pi;\n if(cross(a,b)<-eps)r=360.0-r;\n return r;\n}\n\ndouble Area(Polygon p){\n double area=0.0;\n int n=p.size();\n for(int i=0;i<n;i++)area+=cross(p[i],p[(i+1)%n]);\n return area/2.0;\n}\n\nint contains(Polygon g,Point p){\n int n=g.size();\n bool x=false;\n for(int i=0;i<n;i++){\n Vector a=g[i]-p,b=g[(i+1)%n]-p;\n if(abs(cross(a,b))<eps && dot(a,b)<eps)return 1;\n if(a.y>b.y)swap(a,b);\n if(a.y<eps && eps<b.y && cross(a,b)>eps)x=!x;\n }\n if(x)return 2;\n return 0;\n}\n\nbool same(Point a,Point b,Point c,Point d){\n return ((a==c && b==d) || (a==d && b==c)); \n}\n\nbool check(Polygon a,Polygon b){\n FOR(i,0,a.size())\n if(contains(b,a[i]+(a[(i+1)%a.size()]-a[i])/2.0)!=0)return true;\n FOR(i,0,b.size())\n if(contains(a,b[i]+(b[(i+1)%b.size()]-b[i])/2.0)!=0)return true;\n// FOR(i,0,a.size())\n// FOR(j,0,b.size())\n// if(same(a[i],a[(i+1)%a.size()],b[j],b[(j+1)%b.size()]))\n// return true;\n \n return false;\n}\n\nvector<Polygon> remove(vector<Polygon> p){\n vector<Polygon> res;\n FOR(i,0,p.size())if(eps<Area(p[i]))res.pb(p[i]);\n return res;\n}\n\nstruct edge{ int to,cap,rev; };\n\nint n,k;\nvector<Circle> vc;\nvector<edge> G[2002];\nbool used[2002];\n\nvoid add_edge(int from,int to,int cap){\n G[from].push_back((edge){to,cap,(int)G[to].size()});\n G[to].push_back((edge){from,0,(int)G[from].size()-1});\n}\n \nint dfs(int v,int t,int f){\n if(v==t)return f;\n used[v]=true;\n for(int i=0;i<G[v].size();i++){\n edge &e=G[v][i];\n if(!used[e.to] && e.cap>0){\n int d=dfs(e.to,t,min(f,e.cap));\n if(d>0){\n e.cap-=d;\n G[e.to][e.rev].cap+=d;\n return d;\n }\n }\n }\n return 0;\n}\n \nint max_flow(int s,int t){\n int flow=0;\n for(;;){\n memset(used,0,sizeof(used));\n int f=dfs(s,t,inf);\n if(f==0)return flow;\n flow+=f;\n }\n}\n\nint cal(vector<Polygon> vp){\n int V=vp.size();\n int s=2*V,t=s+1;\n FOR(i,0,V)add_edge(s,i,1);\n FOR(i,0,V)add_edge(i+V,t,1);\n FOR(i,0,V){\n FOR(j,0,V){\n if(i==j)continue;\n if(check(vp[i],vp[j]))add_edge(i,j+V,1);\n }\n }\n return V-max_flow(s,t)/2;\n}\n\nint solve(){\n vector<Point> vp,tmp;\n FOR(i,0,n){\n vp.pb(Point(vc[i].c.x+vc[i].r,vc[i].c.y));\n vp.pb(Point(vc[i].c.x-vc[i].r,vc[i].c.y));\n FOR(j,i+1,n){\n if(abs(intersect(vc[i],vc[j]))==2)continue;\n pair<Point,Point> pp=getCrossPoints(vc[i],vc[j]);\n vp.pb(pp.f);\n vp.pb(pp.s);\n }\n }\n FOR(i,0,n){\n vector<pair<double,Point> > list;\n FOR(j,0,vp.size()){\n double rarg=arg(vp[j]-vc[i].c);\n if(rarg<-eps)rarg+=2.0*pi;\n if(on(vc[i],vp[j]))list.pb(mp(rarg,vp[j]));\n }\n sort(all(list));\n FOR(j,0,list.size()){\n Point a=list[j].s,b=list[(j+1)%list.size()].s;\n double r=getAngle(a-vc[i].c,b-vc[i].c);\n tmp.pb(rotate(vc[i].c,a,r/2.0));\n }\n }\n FOR(i,0,tmp.size())vp.pb(tmp[i]);\n sort(all(vp));\n Unique(vp);\n DualGraph dg=DualGraph(vp);\n FOR(i,0,n){\n vector<pair<double,int> > list;\n FOR(j,0,vp.size()){\n double rarg=arg(vp[j]-vc[i].c);\n if(rarg<-eps)rarg+=2.0*pi;\n if(on(vc[i],vp[j]))list.pb(mp(rarg,j));\n }\n sort(all(list));\n FOR(j,0,list.size()){\n int a=list[j].s,b=list[(j+1)%list.size()].s;\n dg.add_edge(a,b);\n }\n }\n vector<Polygon> v=dg.dual();\n v=remove(v);\n if(1<k)return v.size();\n return cal(v);\n}\n\nint main()\n{\n cin>>n>>k;\n vc.resize(n);\n FOR(i,0,n)cin>>vc[i].c.x>>vc[i].c.y>>vc[i].r;\n cout<<solve()<<endl;\n return 0;\n}", "accuracy": 0.425531914893617, "time_ms": 110, "memory_kb": 4208, "score_of_the_acc": -0.2937, "final_rank": 8 }, { "submission_id": "aoj_2238_1719223", "code_snippet": "#include<cstdio>\n#include<utility>\n#include<map>\n#include<complex>\n#include<cmath>\n#include<vector>\n#include<algorithm>\n\nusing namespace std;\n\ntypedef double Real;\ntypedef pair<Real,Real> P;\ntypedef complex<Real> Point;\n\nvoid print(Point p, char ch = '\\n'){\n\tprintf(\"%f %f%c\", p.real(), p.imag(), ch);\n}\n\nconst Real epsL = 5e-4;\n\nconst Real eps = 1e-8;\n\ntemplate<class T> bool eq_(T a, T b){\n\treturn abs(a - b) < eps;\n}\n\ntemplate<class T> bool lt_(T a, T b){\n\tif(eq_(a, b)) return false;\n\treturn a < b;\n}\n\ntemplate<class T> bool le_(T a, T b){\n\tif(eq_(a, b)) return true;\n\treturn a < b;\n}\n\nconst Real PI = acos(-1.0);\n\nReal normalize(Real x){\n\twhile(x > PI) x -= PI * 2;\n\twhile(x < -PI) x += PI * 2;\n\treturn x;\n}\n\nPoint toPt(P p){\n\treturn Point(p.first, p.second);\n}\n\nP toP(Point pt){\n\treturn P(pt.real(), pt.imag());\n}\n\nstruct Circle:vector<Real>{\n\tvector<P> ps;\n\tPoint c;\n\tReal r;\n\tCircle(){}\n\tCircle(Point p_,Real r_):c(p_),r(r_){}\n};\n\nint N;\nCircle cs[22];\n\nmap<P, P> merged_pts;\n\nvoid ins_point(Point pt){\n\tmap<P, P>::iterator it = merged_pts.begin();\n\tP p = toP(pt);\n\tif(merged_pts.count(p) == 1) return;\n\tfor(; it != merged_pts.end(); ++it){\n\t\tReal d = abs(toPt(it->first) - pt);\n\t\tP p2 = it->second;\n\t\tif(d < epsL){\n\t\t\tmerged_pts[p] = p2;\n\t\t\treturn;\n\t\t}\n\t}\n\tmerged_pts[p] = p;\n}\n\nvector<Real> xs;\n\nvoid iCC(Circle &c, Circle &c2){\n\tif(eq_(c.c, c2.c)) return;\n\tReal d = abs(c.c - c2.c);\n\tReal r = c.r, r2 = c2.r;\n\tif(lt_(r + r2, d)) return;//not touch\n\telse if(eq_(r + r2, d)){//out tangent\n\t\tReal ang = normalize(arg(c2.c - c.c));\n\t\tc.push_back(ang);\n\t}else if(lt_(abs(r - r2), d)){\n\t\tReal ang = arg(c2.c - c.c);\n\t\tReal ang2 = acos((r * r + d * d - r2 * r2) / (2.0 * r * d));\n\t\tc.push_back(normalize(ang + ang2));\n\t\tc.push_back(normalize(ang - ang2));\n\t}else if(eq_(abs(r - r2), d)){//in tangent\n\t\tif(r > r2){\n\t\t\tc.push_back(normalize(arg(c2.c - c.c)));\n\t\t}else{\n\t\t\tc.push_back(normalize(-arg(c2.c - c.c)));\n\t\t}\n\t}else{//contained\n\t\treturn;\n\t}\n}\n\nvoid getAllPoints(){\n\tfor(int i = 0; i < N; ++i){\n\t\tfor(int j = 0; j < N; ++j){\n\t\t\tif(i == j) continue;\n\t\t\tiCC(cs[i], cs[j]);\n\t\t}\n\t}\n\tfor(int i = 0; i < N; ++i){\n\t\tfor(int j = 0; j < cs[i].size(); ++j){\n\t\t\tReal ang = cs[i][j];\n\t\t\tPoint pt = cs[i].c + cs[i].r * Point(cos(ang), sin(ang));\n\t\t\tins_point(pt);\n\t\t}\n\t}\n}\n\nvoid getPsCircle(Circle &c){\n\tsort(c.begin(), c.end());\n\tfor(int i = 0; i < c.size(); ++i){\n\t\tReal ang = c[i];\n\t\tPoint pt = c.c + c.r * Point(cos(ang), sin(ang));\n\t\tP p = merged_pts[toP(pt)];\n\t\tif(c.ps.size() == 0 || c.ps.back() != p){\n\t\t\tif(c.ps.size() != 0 && c.ps[0] == p) continue;\n\t\t\tc.ps.push_back(p);\n\t\t}\n\t}\n}\n\nvoid getPsAll(){\n\tfor(int i = 0; i < N; ++i){\n\t\tgetPsCircle(cs[i]);\n\t}\n}\n\nvoid getXs(){\n\tmap<P, P> :: iterator it = merged_pts.begin();\n\tfor(; it != merged_pts.end(); ++it){\n\t\tP p = it->second;\n\t\txs.push_back(p.first);\n\t}\n\tfor(int i = 0; i < N; ++i){\n\t\txs.push_back(cs[i].c.real() - cs[i].r);\n\t\txs.push_back(cs[i].c.real() + cs[i].r);\n\t}\n\tsort(xs.begin(), xs.end());\n\txs.erase(unique(xs.begin(), xs.end()), xs.end());\n}\n\n//map<Real, vector<int> > ys[500];\n\nvector<pair<Real, int> > ys[500];\n\nvoid getYs(int id){\n\tReal x = xs[id];\n\tfor(int i = 0; i < N; ++i){\n\t\tCircle &c = cs[i];\n\t\tif(c.c.real() - c.r == x){\n\t\t//\tys[id][c.c.imag()].push_back(i);\n\t\t\tys[id].push_back(make_pair(c.c.imag(), i));\n\t\t\tys[id].push_back(make_pair(c.c.imag(), i));\n\t\t\tcontinue;\n\t\t}else if(c.c.real() + c.r == x){\n\t\t//\tys[id][c.c.imag()].push_back(i);\n\t\t\tys[id].push_back(make_pair(c.c.imag(), i));\n\t\t\tys[id].push_back(make_pair(c.c.imag(), i));\n\t\t\tcontinue;\n\t\t}\n\t\tbool flg = false;\n\t\tfor(int j = 0; j < c.ps.size(); ++j){\n\t\t\tif(c.ps[j].first == x){\n\t\t\t\tflg = true;\n\t\t//\t\tys[id][c.ps[j].second].push_back(i);\n\t\t\t\tys[id].push_back(make_pair(c.ps[j].second, i));\n\t\t\t\tReal y1 = c.ps[j].second;\n\t\t\t\tReal y_mid = c.c.imag();\n\t\t\t\tReal y2 = y_mid * 2.0 - y1;\n\t\t\t\tys[id].push_back(make_pair(y2, i));\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tif(!flg){\n\t\t\tif(x < c.c.real() - c.r) continue;\n\t\t\tif(x > c.c.real() + c.r) continue;\n\t\t\tReal dx = abs(x - c.c.real());\n\t\t\tReal dy = sqrt(c.r * c.r - dx * dx);\n\t\t//\tys[id][c.c.imag() + dy].push_back(i);\n\t\t//\tys[id][c.c.imag() - dy].push_back(i);\n\t\t\tys[id].push_back(make_pair(c.c.imag() + dy, i));\n\t\t\tys[id].push_back(make_pair(c.c.imag() - dy, i));\n\t\t}\n\t}\n\tsort(ys[id].begin(), ys[id].end());\n\t/*for(int j = 0; j + 1 < ys[id].size(); ++j){\n\t\tReal y = ys[id][j].first;\n\t\tif(ys[id][j].first != ys[id][j + 1].first) continue;\n\t\tP p = P(xs[id], y);\n\t\tif(merged_pts.count(p) == 0){\n\t\t\tprintf(\"no\\n\");\n\t\t}\n\t}*/\n\t/*printf(\"ys[%d]\\n\", id);\n\tfor(int i = 0; i < ys[id].size(); ++i){\n\t\tprintf(\"(%f, %d) \", ys[id][i].first, ys[id][i].second);\n\t}\n\tprintf(\"\\n\");*/\n}\n\nvoid getYsAll(){\n\tfor(int i = 0; i < xs.size(); ++i){\n\t\tgetYs(i);\n\t}\n}\n\ntypedef pair<Real, Real> PRR;\n\nvector<PRR> arcs[500];\n\nvoid getArcs(int id){\n\tvector<pair<Real, int> > &le = ys[id], &ri = ys[id + 1];\n\tfor(int i = 0; i < N; ++i){\n\t\tconst Real inf = 1e100;\n\t\tReal ly = inf, ry = inf;\n\t\tfor(int j = 0; j < le.size(); ++j){\n\t\t\tif(le[j].second == i){\n\t\t\t\tly = le[j].first;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tfor(int j = 0; j < ri.size(); ++j){\n\t\t\tif(ri[j].second == i){\n\t\t\t\try = ri[j].first;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tif(ly == inf || ry == inf) continue;\n\t\tarcs[id].push_back(PRR(ly, ry));\n\t\tly = inf, ry = inf;\n\t\tfor(int j = (int)le.size() - 1; j >= 0; --j){\n\t\t\tif(le[j].second == i){\n\t\t\t\tly = le[j].first;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tfor(int j = (int)ri.size() - 1; j >= 0; --j){\n\t\t\tif(ri[j].second == i){\n\t\t\t\try = ri[j].first;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tarcs[id].push_back(PRR(ly, ry));\n\t}\n\tsort(arcs[id].begin(), arcs[id].end());\n}\n\nstruct Region{\n\tint x_id;\n\tPRR lo, hi;\n\tRegion(){}\n\tRegion(int i, PRR prr1, PRR prr2){\n\t\tx_id = i;\n\t\tlo = prr1;\n\t\thi = prr2;\n\t\tif(lo > hi) swap(lo, hi);\n\t}\n};\n\nvector<Region> regions;\n\nstruct UnionFind{\n\tint par[20200];\n\tvoid init(int N){\n\t\tfor(int i = 0; i < N; ++i){\n\t\t\tpar[i] = i;\n\t\t}\n\t}\n\tint find(int x){\n\t\tif(x == par[x]) return x;\n\t\treturn par[x] = find(par[x]);\n\t}\n\tvoid unite(int x, int y){\n\t//\tprintf(\"unite %d %d\\n\", x, y);\n\t\tx = find(x), y = find(y);\n\t\tif(x == y) return;\n\t\tif(x > y) swap(x, y);\n\t\tpar[y] = x;\n\t}\n\tbool same(int x, int y){\n\t\treturn find(x) == find(y);\n\t}\n};\n\nUnionFind uf;\n\nvoid getRegions(){\n\tfor(int i = 0; i + 1 < xs.size(); ++i){\n\t\tgetArcs(i);\n\t}\n\tfor(int i = 0; i + 1 < xs.size(); ++i){\n\t\tfor(int j = 0; j + 1 < arcs[i].size(); ++j){\n\t\t\tPRR lo = arcs[i][j];\n\t\t\tPRR hi = arcs[i][j + 1];\n\t\t\tregions.push_back(Region(i, lo, hi));\n\t\t}\n\t}\n}\n\nbool checkIntersect(Real l1, Real h1, Real l2, Real h2){\n\tif(h1 <= l2) return false;\n\tif(h2 <= l1) return false;\n\tif(l1 == h1) return false;\n\tif(l2 == h2) return false;\n\treturn true;\n}\n\nvoid mergeRegions(){\n\tuf.init(regions.size());\n\tfor(int i = 0; i < regions.size(); ++i){\n\t\tRegion r = regions[i];\n\t\tfor(int j = i - 1; j >= 0; --j){\n\t\t\tRegion cur = regions[j];\n\t\t\tif(cur.x_id <= r.x_id - 2) break;\n\t\t\tif(cur.x_id == r.x_id) continue;\n\t\t\tbool flg = checkIntersect(r.lo.first, r.hi.first, cur.lo.second, cur.hi.second);\n\t\t\tif(flg){\n\t\t\t\tuf.unite(i, j);\n\t\t\t}\n\t\t}\n\t\tfor(int j = i + 1; j < regions.size(); ++j){\n\t\t\tRegion cur = regions[j];\n\t\t\tif(cur.x_id >= r.x_id + 2) break;\n\t\t\tif(cur.x_id == r.x_id) continue;\n\t\t\tbool flg = checkIntersect(r.lo.second, r.hi.second, cur.lo.first, cur.hi.first);\n\t\t\tif(flg){\n\t\t\t\tuf.unite(i, j);\n\t\t\t}\n\t\t}\n\t}\n}\n\nvector<int> G[20200];\nbool is_valid[20200];\n\nvoid getGraphVertices(){\n\tfor(int i = 1; i < regions.size(); ++i){\n\t\tis_valid[i] = (uf.find(i) == i);\n\t//\tif(is_valid[i]) printf(\"valid: %d\\n\", i);\n\t}\n}\n\nvoid getGraphEdges(){\n\tfor(int i = 0; i + 1 < regions.size(); ++i){\n\t\tRegion r1 = regions[i], r2 = regions[i + 1];\n\t\tif(r1.x_id != r2.x_id) continue;\n\t\tint u = uf.find(i), v = uf.find(i + 1);\n\t\tif(u == 0 || v == 0) continue;\n\t\tG[u].push_back(v);\n\t\tG[v].push_back(u);\n\t}\n\tfor(int i = 0; i < regions.size(); ++i){\n\t\tsort(G[i].begin(), G[i].end());\n\t\tG[i].erase(unique(G[i].begin(), G[i].end()), G[i].end());\n\t}\n}\n\nvoid getGraph(){\n\tgetGraphVertices();\n\tgetGraphEdges();\n}\n\nint match[20200];\nbool used[20200];\n\nvector<int> valid_vs;\n/*\nbool dfs(int v, int p){\n\tused[v] = true;\n\tif(match[v] == -1 && p != -1){\n\t\tmatch[v] = p;\n\t\treturn true;\n\t}\n\tif(match[v] == p){\n\t\tfor(int i = 0; i < G[v].size(); ++i){\n\t\t\tint u = G[v][i];\n\t\t\tif(used[u]) continue;\n\t\t\tbool flg = dfs(u, v);\n\t\t\tif(flg){\n\t\t\t\tmatch[v] = u;\n\t\t\t\treturn true;\n\t\t\t}\n\t\t}\n\t}else{\n\t\tint u = match[v];\n\t\tif(used[u]) return false;\n\t\tbool flg = dfs(u, v);\n\t\tif(flg){\n\t\t\tmatch[v] = p;\n\t\t\treturn true;\n\t\t}\n\t}\n\treturn false;\n}*/\n\nbool dfs(int v){\n\tused[v] = true;\n\tfor(int i = 0; i < G[v].size(); ++i){\n\t\tint u = G[v][i];\n\t\tif(used[u]) continue;\n\t\tint w = match[u];\n\t\tif(w == -1){\n\t\t\tmatch[u] = v;\n\t\t\tmatch[v] = u;\n\t\t\treturn true;\n\t\t}\n\t\tif(used[w]) continue;\n\t\tbool flg = dfs(w);\n\t\tif(flg){\n\t\t\tmatch[u] = v;\n\t\t\tmatch[v] = u;\n\t\t\treturn true;\n\t\t}\n\t}\n\treturn false;\n}\n\nint bipartiteMatching(){\n\tfor(int i = 1; i < 20200; ++i){\n\t\tmatch[i] = -1;\n\t\tif(is_valid[i]){\n\t\t\tvalid_vs.push_back(i);\n\t\t}\n\t}\n\t/*for(int i = 0; i < valid_vs.size(); ++i){\n\t\tprintf(\"%d:\", valid_vs[i]);\n\t\tfor(int j = 0; j < G[valid_vs[i]].size(); ++j){\n\t\t\tprintf(\"%d \", G[valid_vs[i]][j]);\n\t\t}\n\t\tprintf(\"\\n\");\n\t}*/\n\tint res = 0;\n\tfor(int i = 0; i < valid_vs.size(); ++i){\n\t\tint v = valid_vs[i];\n\t\tif(match[v] != -1) continue;\n\t\tfor(int j = 0; j < valid_vs.size(); ++j){\n\t\t\tused[valid_vs[j]] = false;\n\t\t}\n\t\tbool flg = dfs(v);\n\t\tif(flg) res++;\n\t}\n\t//printf(\"match = %d\\n\", res);\n\treturn res;\n}\n\nint K;\n\nvoid rotAll(){\n\tPoint r = Point(cos(1), sin(1));\n\tfor(int i = 0; i < N; ++i){\n\t\tcs[i].c *= r;\n\t}\n}\n\nint solve(){\n\trotAll();\n\t/*for(int i = 0; i < N; ++i){\n\t\tprint(cs[i].c, ' ');\n\t\tprintf(\"%f\\n\", cs[i].r);\n\t}*/\n\tgetAllPoints();\n\tgetPsAll();\n\tgetXs();\n\tgetYsAll();\n\tgetRegions();\n\tmergeRegions();\n\tgetGraph();\n/*\tfor(int i = 0; i < xs.size(); ++i){\n\t\tprintf(\"%f \", xs[i]);\n\t}\n\tprintf(\"\\n\");\n\tfor(int i = 0; i < regions.size(); ++i){\n\t\tRegion r = regions[i];\n\t\tprintf(\"x_id = %d\\n\", r.x_id);\n\t\tprintf(\"%f %f %f %f\\n\", r.lo.first, r.lo.second, r.hi.first, r.hi.second);\n\t}*/\n\tint V = 0;\n\tfor(int i = 0; i < 20200; ++i){\n\t\tif(is_valid[i]){\n\t\t\tV++;\n\t\t}\n\t}\n\tif(K >= 2){\n\t\treturn V;\n\t}\n\tint ans = V - bipartiteMatching();\n\treturn ans;\n}\n\nvoid input(){\n\tscanf(\"%d%d\", &N, &K);\n\tfor(int i = 0; i < N; ++i){\n\t\tint x, y, r;\n\t\tscanf(\"%d%d%d\", &x, &y, &r);\n\t\tcs[i] = Circle(Point(x, y), r);\n\t}\n\tcs[N] = Circle(Point(0, 0), 5000);\n\tN++;\n}\n\nint main(){\n\tinput();\n\tint ans = solve();\n\tprintf(\"%d\\n\", ans);\n\treturn 0;\n}\n\n/*\nvoid iCC(Circle &c,Circle &c2){\n\tif(eq(c.c,c2.c)) return;\n\tReal d=abs(c.c-c2.c);\n\tReal r=c.r,r2=c2.r;\n\tif(sgn(d-(r+r2))>0) return;\n\telse if(sgn(d-(r+r2))==0){//out tangent\n\t\tReal ang=normalize(arg(c2.c-c.c));\n\t\tc.push_back(ang);\n\t}else if(sgn(d-abs(r-r2))>0){//intersect two points\n\t\tReal ang=arg(c2.c-c.c);\n\t\tReal ang2=acos((r*r+d*d-r2*r2)/(2*r*d));\n\t\tc.push_back(normalize(ang+ang2));\n\t\tc.push_back(normalize(ang-ang2));\n\t}else if(sgn(d-abs(r-r2))==0){//in tangent\n\t\tif(r>r2){\n\t\t\tc.push_back(normalize(arg(c2.c-c.c)));\n\t\t}else{\n\t\t\tc.push_back(normalize(-arg(c2.c-c.c)));\n\t\t}\n\t}else{//contained\n\t\treturn;\n\t}\n}*/", "accuracy": 1, "time_ms": 10, "memory_kb": 2636, "score_of_the_acc": -0.042, "final_rank": 1 }, { "submission_id": "aoj_2238_1719217", "code_snippet": "#include<cstdio>\n#include<utility>\n#include<map>\n#include<complex>\n#include<cmath>\n#include<vector>\n#include<algorithm>\n\nusing namespace std;\n\ntypedef long double Real;\ntypedef pair<Real,Real> P;\ntypedef complex<Real> Point;\n\nvoid print(Point p, char ch = '\\n'){\n\tprintf(\"%f %f%c\", p.real(), p.imag(), ch);\n}\n\nconst Real epsL = 5e-4;\n\nconst Real eps = 1e-8;\n\ntemplate<class T> bool eq_(T a, T b){\n\treturn abs(a - b) < eps;\n}\n\ntemplate<class T> bool lt_(T a, T b){\n\tif(eq_(a, b)) return false;\n\treturn a < b;\n}\n\ntemplate<class T> bool le_(T a, T b){\n\tif(eq_(a, b)) return true;\n\treturn a < b;\n}\n\nconst Real PI = acos(-1.0);\n\nReal normalize(Real x){\n\twhile(x > PI) x -= PI * 2;\n\twhile(x < -PI) x += PI * 2;\n\treturn x;\n}\n\nPoint toPt(P p){\n\treturn Point(p.first, p.second);\n}\n\nP toP(Point pt){\n\treturn P(pt.real(), pt.imag());\n}\n\nstruct Circle:vector<Real>{\n\tvector<P> ps;\n\tPoint c;\n\tReal r;\n\tCircle(){}\n\tCircle(Point p_,Real r_):c(p_),r(r_){}\n};\n\nint N;\nCircle cs[22];\n\nmap<P, P> merged_pts;\n\nvoid ins_point(Point pt){\n\tmap<P, P>::iterator it = merged_pts.begin();\n\tP p = toP(pt);\n\tif(merged_pts.count(p) == 1) return;\n\tfor(; it != merged_pts.end(); ++it){\n\t\tReal d = abs(toPt(it->first) - pt);\n\t\tP p2 = it->second;\n\t\tif(d < epsL){\n\t\t\tmerged_pts[p] = p2;\n\t\t\treturn;\n\t\t}\n\t}\n\tmerged_pts[p] = p;\n}\n\nvector<Real> xs;\n\nvoid iCC(Circle &c, Circle &c2){\n\tif(eq_(c.c, c2.c)) return;\n\tReal d = abs(c.c - c2.c);\n\tReal r = c.r, r2 = c2.r;\n\tif(lt_(r + r2, d)) return;//not touch\n\telse if(eq_(r + r2, d)){//out tangent\n\t\tReal ang = normalize(arg(c2.c - c.c));\n\t\tc.push_back(ang);\n\t}else if(lt_(abs(r - r2), d)){\n\t\tReal ang = arg(c2.c - c.c);\n\t\tReal ang2 = acos((r * r + d * d - r2 * r2) / (2.0 * r * d));\n\t\tc.push_back(normalize(ang + ang2));\n\t\tc.push_back(normalize(ang - ang2));\n\t}else if(eq_(abs(r - r2), d)){//in tangent\n\t\tif(r > r2){\n\t\t\tc.push_back(normalize(arg(c2.c - c.c)));\n\t\t}else{\n\t\t\tc.push_back(normalize(-arg(c2.c - c.c)));\n\t\t}\n\t}else{//contained\n\t\treturn;\n\t}\n}\n\nvoid getAllPoints(){\n\tfor(int i = 0; i < N; ++i){\n\t\tfor(int j = 0; j < N; ++j){\n\t\t\tif(i == j) continue;\n\t\t\tiCC(cs[i], cs[j]);\n\t\t}\n\t}\n\tfor(int i = 0; i < N; ++i){\n\t\tfor(int j = 0; j < cs[i].size(); ++j){\n\t\t\tReal ang = cs[i][j];\n\t\t\tPoint pt = cs[i].c + cs[i].r * Point(cos(ang), sin(ang));\n\t\t\tins_point(pt);\n\t\t}\n\t}\n}\n\nvoid getPsCircle(Circle &c){\n\tsort(c.begin(), c.end());\n\tfor(int i = 0; i < c.size(); ++i){\n\t\tReal ang = c[i];\n\t\tPoint pt = c.c + c.r * Point(cos(ang), sin(ang));\n\t\tP p = merged_pts[toP(pt)];\n\t\tif(c.ps.size() == 0 || c.ps.back() != p){\n\t\t\tif(c.ps.size() != 0 && c.ps[0] == p) continue;\n\t\t\tc.ps.push_back(p);\n\t\t}\n\t}\n}\n\nvoid getPsAll(){\n\tfor(int i = 0; i < N; ++i){\n\t\tgetPsCircle(cs[i]);\n\t}\n}\n\nvoid getXs(){\n\tmap<P, P> :: iterator it = merged_pts.begin();\n\tfor(; it != merged_pts.end(); ++it){\n\t\tP p = it->second;\n\t\txs.push_back(p.first);\n\t}\n\tfor(int i = 0; i < N; ++i){\n\t\txs.push_back(cs[i].c.real() - cs[i].r);\n\t\txs.push_back(cs[i].c.real() + cs[i].r);\n\t}\n\tsort(xs.begin(), xs.end());\n\txs.erase(unique(xs.begin(), xs.end()), xs.end());\n}\n\n//map<Real, vector<int> > ys[500];\n\nvector<pair<Real, int> > ys[500];\n\nvoid getYs(int id){\n\tReal x = xs[id];\n\tfor(int i = 0; i < N; ++i){\n\t\tCircle &c = cs[i];\n\t\tif(c.c.real() - c.r == x){\n\t\t//\tys[id][c.c.imag()].push_back(i);\n\t\t\tys[id].push_back(make_pair(c.c.imag(), i));\n\t\t\tys[id].push_back(make_pair(c.c.imag(), i));\n\t\t\tcontinue;\n\t\t}else if(c.c.real() + c.r == x){\n\t\t//\tys[id][c.c.imag()].push_back(i);\n\t\t\tys[id].push_back(make_pair(c.c.imag(), i));\n\t\t\tys[id].push_back(make_pair(c.c.imag(), i));\n\t\t\tcontinue;\n\t\t}\n\t\tbool flg = false;\n\t\tfor(int j = 0; j < c.ps.size(); ++j){\n\t\t\tif(c.ps[j].first == x){\n\t\t\t\tflg = true;\n\t\t//\t\tys[id][c.ps[j].second].push_back(i);\n\t\t\t\tys[id].push_back(make_pair(c.ps[j].second, i));\n\t\t\t\tReal y1 = c.ps[j].second;\n\t\t\t\tReal y_mid = c.c.imag();\n\t\t\t\tReal y2 = y_mid * 2.0 - y1;\n\t\t\t\tys[id].push_back(make_pair(y2, i));\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tif(!flg){\n\t\t\tif(x < c.c.real() - c.r) continue;\n\t\t\tif(x > c.c.real() + c.r) continue;\n\t\t\tReal dx = abs(x - c.c.real());\n\t\t\tReal dy = sqrt(c.r * c.r - dx * dx);\n\t\t//\tys[id][c.c.imag() + dy].push_back(i);\n\t\t//\tys[id][c.c.imag() - dy].push_back(i);\n\t\t\tys[id].push_back(make_pair(c.c.imag() + dy, i));\n\t\t\tys[id].push_back(make_pair(c.c.imag() - dy, i));\n\t\t}\n\t}\n\tsort(ys[id].begin(), ys[id].end());\n\t/*for(int j = 0; j + 1 < ys[id].size(); ++j){\n\t\tReal y = ys[id][j].first;\n\t\tif(ys[id][j].first != ys[id][j + 1].first) continue;\n\t\tP p = P(xs[id], y);\n\t\tif(merged_pts.count(p) == 0){\n\t\t\tprintf(\"no\\n\");\n\t\t}\n\t}*/\n\t/*printf(\"ys[%d]\\n\", id);\n\tfor(int i = 0; i < ys[id].size(); ++i){\n\t\tprintf(\"(%f, %d) \", ys[id][i].first, ys[id][i].second);\n\t}\n\tprintf(\"\\n\");*/\n}\n\nvoid getYsAll(){\n\tfor(int i = 0; i < xs.size(); ++i){\n\t\tgetYs(i);\n\t}\n}\n\ntypedef pair<Real, Real> PRR;\n\nvector<PRR> arcs[500];\n\nvoid getArcs(int id){\n\tvector<pair<Real, int> > &le = ys[id], &ri = ys[id + 1];\n\tfor(int i = 0; i < N; ++i){\n\t\tconst Real inf = 1e100;\n\t\tReal ly = inf, ry = inf;\n\t\tfor(int j = 0; j < le.size(); ++j){\n\t\t\tif(le[j].second == i){\n\t\t\t\tly = le[j].first;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tfor(int j = 0; j < ri.size(); ++j){\n\t\t\tif(ri[j].second == i){\n\t\t\t\try = ri[j].first;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tif(ly == inf || ry == inf) continue;\n\t\tarcs[id].push_back(PRR(ly, ry));\n\t\tly = inf, ry = inf;\n\t\tfor(int j = (int)le.size() - 1; j >= 0; --j){\n\t\t\tif(le[j].second == i){\n\t\t\t\tly = le[j].first;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tfor(int j = (int)ri.size() - 1; j >= 0; --j){\n\t\t\tif(ri[j].second == i){\n\t\t\t\try = ri[j].first;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tarcs[id].push_back(PRR(ly, ry));\n\t}\n\tsort(arcs[id].begin(), arcs[id].end());\n}\n\nstruct Region{\n\tint x_id;\n\tPRR lo, hi;\n\tRegion(){}\n\tRegion(int i, PRR prr1, PRR prr2){\n\t\tx_id = i;\n\t\tlo = prr1;\n\t\thi = prr2;\n\t\tif(lo > hi) swap(lo, hi);\n\t}\n};\n\nvector<Region> regions;\n\nstruct UnionFind{\n\tint par[20200];\n\tvoid init(int N){\n\t\tfor(int i = 0; i < N; ++i){\n\t\t\tpar[i] = i;\n\t\t}\n\t}\n\tint find(int x){\n\t\tif(x == par[x]) return x;\n\t\treturn par[x] = find(par[x]);\n\t}\n\tvoid unite(int x, int y){\n\t//\tprintf(\"unite %d %d\\n\", x, y);\n\t\tx = find(x), y = find(y);\n\t\tif(x == y) return;\n\t\tif(x > y) swap(x, y);\n\t\tpar[y] = x;\n\t}\n\tbool same(int x, int y){\n\t\treturn find(x) == find(y);\n\t}\n};\n\nUnionFind uf;\n\nvoid getRegions(){\n\tfor(int i = 0; i + 1 < xs.size(); ++i){\n\t\tgetArcs(i);\n\t}\n\tfor(int i = 0; i + 1 < xs.size(); ++i){\n\t\tfor(int j = 0; j + 1 < arcs[i].size(); ++j){\n\t\t\tPRR lo = arcs[i][j];\n\t\t\tPRR hi = arcs[i][j + 1];\n\t\t\tregions.push_back(Region(i, lo, hi));\n\t\t}\n\t}\n}\n\nbool checkIntersect(Real l1, Real h1, Real l2, Real h2){\n\tif(h1 <= l2) return false;\n\tif(h2 <= l1) return false;\n\tif(l1 == h1) return false;\n\tif(l2 == h2) return false;\n\treturn true;\n}\n\nvoid mergeRegions(){\n\tuf.init(regions.size());\n\tfor(int i = 0; i < regions.size(); ++i){\n\t\tRegion r = regions[i];\n\t\tfor(int j = i - 1; j >= 0; --j){\n\t\t\tRegion cur = regions[j];\n\t\t\tif(cur.x_id <= r.x_id - 2) break;\n\t\t\tif(cur.x_id == r.x_id) continue;\n\t\t\tbool flg = checkIntersect(r.lo.first, r.hi.first, cur.lo.second, cur.hi.second);\n\t\t\tif(flg){\n\t\t\t\tuf.unite(i, j);\n\t\t\t}\n\t\t}\n\t\tfor(int j = i + 1; j < regions.size(); ++j){\n\t\t\tRegion cur = regions[j];\n\t\t\tif(cur.x_id >= r.x_id + 2) break;\n\t\t\tif(cur.x_id == r.x_id) continue;\n\t\t\tbool flg = checkIntersect(r.lo.second, r.hi.second, cur.lo.first, cur.hi.first);\n\t\t\tif(flg){\n\t\t\t\tuf.unite(i, j);\n\t\t\t}\n\t\t}\n\t}\n}\n\nvector<int> G[20200];\nbool is_valid[20200];\n\nvoid getGraphVertices(){\n\tfor(int i = 1; i < regions.size(); ++i){\n\t\tis_valid[i] = (uf.find(i) == i);\n\t//\tif(is_valid[i]) printf(\"valid: %d\\n\", i);\n\t}\n}\n\nvoid getGraphEdges(){\n\tfor(int i = 0; i + 1 < regions.size(); ++i){\n\t\tRegion r1 = regions[i], r2 = regions[i + 1];\n\t\tif(r1.x_id != r2.x_id) continue;\n\t\tint u = uf.find(i), v = uf.find(i + 1);\n\t\tif(u == 0 || v == 0) continue;\n\t\tG[u].push_back(v);\n\t\tG[v].push_back(u);\n\t}\n\tfor(int i = 0; i < regions.size(); ++i){\n\t\tsort(G[i].begin(), G[i].end());\n\t\tG[i].erase(unique(G[i].begin(), G[i].end()), G[i].end());\n\t}\n}\n\nvoid getGraph(){\n\tgetGraphVertices();\n\tgetGraphEdges();\n}\n\nint match[20200];\nbool used[20200];\n\nvector<int> valid_vs;\n/*\nbool dfs(int v, int p){\n\tused[v] = true;\n\tif(match[v] == -1 && p != -1){\n\t\tmatch[v] = p;\n\t\treturn true;\n\t}\n\tif(match[v] == p){\n\t\tfor(int i = 0; i < G[v].size(); ++i){\n\t\t\tint u = G[v][i];\n\t\t\tif(used[u]) continue;\n\t\t\tbool flg = dfs(u, v);\n\t\t\tif(flg){\n\t\t\t\tmatch[v] = u;\n\t\t\t\treturn true;\n\t\t\t}\n\t\t}\n\t}else{\n\t\tint u = match[v];\n\t\tif(used[u]) return false;\n\t\tbool flg = dfs(u, v);\n\t\tif(flg){\n\t\t\tmatch[v] = p;\n\t\t\treturn true;\n\t\t}\n\t}\n\treturn false;\n}*/\n\nbool dfs(int v){\n\tused[v] = true;\n\tfor(int i = 0; i < G[v].size(); ++i){\n\t\tint u = G[v][i];\n\t\tif(used[u]) continue;\n\t\tint w = match[u];\n\t\tif(w == -1){\n\t\t\tmatch[u] = v;\n\t\t\tmatch[v] = u;\n\t\t\treturn true;\n\t\t}\n\t\tif(used[w]) continue;\n\t\tbool flg = dfs(w);\n\t\tif(flg){\n\t\t\tmatch[u] = v;\n\t\t\tmatch[v] = u;\n\t\t\treturn true;\n\t\t}\n\t}\n\treturn false;\n}\n\nint bipartiteMatching(){\n\tfor(int i = 1; i < 20200; ++i){\n\t\tmatch[i] = -1;\n\t\tif(is_valid[i]){\n\t\t\tvalid_vs.push_back(i);\n\t\t}\n\t}\n\t/*for(int i = 0; i < valid_vs.size(); ++i){\n\t\tprintf(\"%d:\", valid_vs[i]);\n\t\tfor(int j = 0; j < G[valid_vs[i]].size(); ++j){\n\t\t\tprintf(\"%d \", G[valid_vs[i]][j]);\n\t\t}\n\t\tprintf(\"\\n\");\n\t}*/\n\tint res = 0;\n\tfor(int i = 0; i < valid_vs.size(); ++i){\n\t\tint v = valid_vs[i];\n\t\tif(match[v] != -1) continue;\n\t\tfor(int j = 0; j < valid_vs.size(); ++j){\n\t\t\tused[valid_vs[j]] = false;\n\t\t}\n\t\tbool flg = dfs(v);\n\t\tif(flg) res++;\n\t}\n\t//printf(\"match = %d\\n\", res);\n\treturn res;\n}\n\nint K;\n\nvoid rotAll(){\n\tPoint r = Point(cos(1), sin(1));\n\tfor(int i = 0; i < N; ++i){\n\t\tcs[i].c *= r;\n\t}\n}\n\nint solve(){\n\trotAll();\n\t/*for(int i = 0; i < N; ++i){\n\t\tprint(cs[i].c, ' ');\n\t\tprintf(\"%f\\n\", cs[i].r);\n\t}*/\n\tgetAllPoints();\n\tgetPsAll();\n\tgetXs();\n\tgetYsAll();\n\tgetRegions();\n\tmergeRegions();\n\tgetGraph();\n/*\tfor(int i = 0; i < xs.size(); ++i){\n\t\tprintf(\"%f \", xs[i]);\n\t}\n\tprintf(\"\\n\");\n\tfor(int i = 0; i < regions.size(); ++i){\n\t\tRegion r = regions[i];\n\t\tprintf(\"x_id = %d\\n\", r.x_id);\n\t\tprintf(\"%f %f %f %f\\n\", r.lo.first, r.lo.second, r.hi.first, r.hi.second);\n\t}*/\n\tint V = 0;\n\tfor(int i = 0; i < 20200; ++i){\n\t\tif(is_valid[i]){\n\t\t\tV++;\n\t\t}\n\t}\n\tif(K >= 2){\n\t\treturn V;\n\t}\n\tint ans = V - bipartiteMatching();\n\treturn ans;\n}\n\nvoid input(){\n\tscanf(\"%d%d\", &N, &K);\n\tfor(int i = 0; i < N; ++i){\n\t\tint x, y, r;\n\t\tscanf(\"%d%d%d\", &x, &y, &r);\n\t\tcs[i] = Circle(Point(x, y), r);\n\t}\n\tcs[N] = Circle(Point(0, 0), 5000);\n\tN++;\n}\n\nint main(){\n\tinput();\n\tint ans = solve();\n\tprintf(\"%d\\n\", ans);\n\treturn 0;\n}\n\n/*\nvoid iCC(Circle &c,Circle &c2){\n\tif(eq(c.c,c2.c)) return;\n\tReal d=abs(c.c-c2.c);\n\tReal r=c.r,r2=c2.r;\n\tif(sgn(d-(r+r2))>0) return;\n\telse if(sgn(d-(r+r2))==0){//out tangent\n\t\tReal ang=normalize(arg(c2.c-c.c));\n\t\tc.push_back(ang);\n\t}else if(sgn(d-abs(r-r2))>0){//intersect two points\n\t\tReal ang=arg(c2.c-c.c);\n\t\tReal ang2=acos((r*r+d*d-r2*r2)/(2*r*d));\n\t\tc.push_back(normalize(ang+ang2));\n\t\tc.push_back(normalize(ang-ang2));\n\t}else if(sgn(d-abs(r-r2))==0){//in tangent\n\t\tif(r>r2){\n\t\t\tc.push_back(normalize(arg(c2.c-c.c)));\n\t\t}else{\n\t\t\tc.push_back(normalize(-arg(c2.c-c.c)));\n\t\t}\n\t}else{//contained\n\t\treturn;\n\t}\n}*/", "accuracy": 1, "time_ms": 10, "memory_kb": 3128, "score_of_the_acc": -0.0668, "final_rank": 2 }, { "submission_id": "aoj_2238_1149574", "code_snippet": "#include <cstdio>\n#include <cmath>\n#include <cstring>\n#include <cstdlib>\n#include <climits>\n#include <ctime>\n#include <queue>\n#include <stack>\n#include <algorithm>\n#include <list>\n#include <vector>\n#include <set>\n#include <map>\n#include <iostream>\n#include <deque>\n#include <complex>\n#include <string>\n#include <iomanip>\n#include <sstream>\n#include <bitset>\n#include <valarray>\n#include <unordered_map>\n#include <unordered_set>\n#include <iterator>\n#include <assert.h>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned int uint;\ntypedef unsigned char uchar;\ntypedef unsigned long long ull;\ntypedef pair<int, int> pii;\ntypedef pair<ll, ll> pll;\ntypedef vector<int> vi;\n\n#define REP(i,x) for(int i=0;i<(int)(x);i++)\n#define REPS(i,x) for(int i=1;i<=(int)(x);i++)\n#define RREP(i,x) for(int i=((int)(x)-1);i>=0;i--)\n#define RREPS(i,x) for(int i=((int)(x));i>0;i--)\n#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();i++)\n#define RFOR(i,c) for(__typeof((c).rbegin())i=(c).rbegin();i!=(c).rend();i++)\n#define ALL(container) (container).begin(), (container).end()\n#define RALL(container) (container).rbegin(), (container).rend()\n#define SZ(container) ((int)container.size())\n#define mp(a,b) make_pair(a, b)\n#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );\n\ntemplate<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }\ntemplate<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }\ntemplate<class T> ostream& operator<<(ostream &os, const vector<T> &t) {\nos<<\"[\"; FOR(it,t) {if(it!=t.begin()) os<<\",\"; os<<*it;} os<<\"]\"; return os;\n}\ntemplate<class T> ostream& operator<<(ostream &os, const set<T> &t) {\nos<<\"{\"; FOR(it,t) {if(it!=t.begin()) os<<\",\"; os<<*it;} os<<\"}\"; return os;\n}\ntemplate<class S, class T> ostream& operator<<(ostream &os, const pair<S,T> &t) { return os<<\"(\"<<t.first<<\",\"<<t.second<<\")\";}\ntemplate<class S, class T> pair<S,T> operator+(const pair<S,T> &s, const pair<S,T> &t){ return pair<S,T>(s.first+t.first, s.second+t.second);}\ntemplate<class S, class T> pair<S,T> operator-(const pair<S,T> &s, const pair<S,T> &t){ return pair<S,T>(s.first-t.first, s.second-t.second);}\n\nstruct UnionFind {\n vector<int> data;\n UnionFind(int size) : data(size, -1) { }\n bool unionSet(int x, int y) {\n x = root(x); y = root(y);\n if (x != y) {\n if (data[y] < data[x]) swap(x, y);\n data[x] += data[y]; data[y] = x;\n }\n return x != y;\n }\n bool findSet(int x, int y) {\n return root(x) == root(y);\n }\n int root(int x) {\n return data[x] < 0 ? x : data[x] = root(data[x]);\n }\n int size(int x) {\n return -data[root(x)];\n }\n};\n\n\nnamespace geom{\n#define X real()\n#define Y imag()\n#define at(i) ((*this)[i])\n#define SELF (*this)\n\tenum {TRUE = 1, FALSE = 0, BORDER = -1};\n\ttypedef int BOOL;\n\ttypedef double R;\n\tconst R INF = 1e8;\n\tR EPS = 1e-6;\n\tconst R PI = 3.1415926535897932384626;\n\tinline int sig(const R &x) { return (abs(x) < EPS ? 0 : x > 0 ? 1 : -1); }\n\tinline BOOL less(const R &x, const R &y) {return sig(x-y) ? x < y : BORDER;}\n\ttypedef complex<R> P;\n\tinline R norm(const P &p){return p.X*p.X+p.Y*p.Y;}\n\tinline R inp(const P& a, const P& b){return (conj(a)*b).X;}\n\tinline R outp(const P& a, const P& b){return (conj(a)*b).Y;}\n\tinline P unit(const P& p){return p/abs(p);}\n\tinline P proj(const P &s, const P &t){return t*inp(s, t)/norm(t);}\n\tinline int ccw(const P &s, const P &t, const P &p, int adv=0){\n\t\tint res = sig(outp(t-s, p-s));\n\t\tif(res || !adv) return res;\n\t\tif(sig(inp(t-s, p-s)) < 0) return -2;\t// p-s-t\n\t\tif(sig(inp(s-t, p-t)) < 0) return 2;\t// s-t-p\n\t\treturn 0;\t\t\t\t\t\t\t\t// s-p-t\n\t}\n\t\n\t\n\tstruct L : public vector<P>{\t// line\n\t\tL(const P &p1, const P &p2){this->push_back(p1);this->push_back(p2);}\n\t\tL(){}\n\t\tP dir()const {return at(1) - at(0);}\n\t\tBOOL online(const P &p)const {return !sig(outp(p-at(0), dir()));}\n\t};\n\tstruct S : public L{\t// segment\n\t\tS(const P &p1, const P &p2):L(p1, p2){}\n\t\tS(){}\n\t\tBOOL online(const P &p)const {\n\t\t\tif(!sig(norm(p - at(0))) || !sig(norm(p - at(1)))) return BORDER;\n\t\t\treturn !sig(abs(at(0)-p) + abs(at(1) - p) - abs(at(0) - at(1)));\n\t\t}\n\t};\n\tstruct C : public P{\n\t\tC(){}\n\t\tC(const P& p, const R r):P(p), r(r){}\n\t\tR r;\n\t\tBOOL inside(const P& p)const { return less(norm(p-SELF), r*r);}\n\t};\n\tstruct F : public C{\n\t\tR s, t;\n\t\tF(const C &c, R ss, R tt):C(c), s(ss), t(tt){\n\t\t\tif(PI < s) s -= 2*PI;\n\t\t\tif(PI < t) t -= 2*PI;\n\t\t}\n\t\tBOOL inside(const P& p)const {\n\t\t\tP v = p - SELF;\n\t\t\tif(!sig(norm(v))) return BORDER;\n\t\t\tR a = arg(v);\n\t\t\tif(t < s){\n\t\t\t\tif((!less(s, a) && !less(a, t)) || !less(norm(v), r*r)) return FALSE;\n\t\t\t\treturn less(s, a) | less(a, t) | less(norm(v), r*r);\n\t\t\t}else{\n\t\t\t\tif(!less(s, a) || !less(a, t) || !less(norm(v), r*r)) return FALSE;\n\t\t\t\treturn less(s, a) | less(a, t) | less(norm(v), r*r);\n\t\t\t}\n\t\t}\n\t};\n\tP crosspoint(const L &l, const L &m);\n\tstruct G : public vector<P>{\n\t\tG(size_type size=0):vector(size){}\n\t\tS edge(int i)const {return S(at(i), at(i+1 == size() ? 0 : i+1));}\n\t\tBOOL contains(const P &p)const {\n\t\t\tR sum = .0;\n\t\t\tREP(i, size()){\n\t\t\t\tif(S(at(i), at((i+1)%size())).online(p)) return BORDER;\t// online\n\t\t\t\tsum += arg((at(i) - p) / (at((i+1)%size()) - p));\n\t\t\t}\n\t\t\treturn !!sig(sum);\n\t\t}\n\t\tR area()const {\n\t\t\tR sum = 0;\n\t\t\tREP(i, size()) sum += outp(at(i), at((i+1)%size()));\n\t\t\treturn abs(sum / 2.);\n\t\t}\n\t\t\n\t\tG convex_hull(bool online = false) {\n\t\t\tif(size() < 2) return *this;\n\t\t\tsort(ALL(*this));\n\t\t\tG r;\n\t\t\tr.resize((int)size()*2);\n\t\t\tint k=0;\n\t\t\tfor(int i=0;i<size();r[k++]=at(i++))\n\t\t\t\twhile(k>1 && ccw(r[k-2], r[k-1], at(i)) < 1-online) k--;\n\t\t\tint t = k;\n\t\t\tfor(int i=(int)size()-1;i>=0;r[k++]=at(i--))\n\t\t\t\twhile(k>t && ccw(r[k-2], r[k-1], at(i)) < 1-online) k--;\n\t\t\tr.resize(k-1);\n\t\t\treturn r;\n\t\t}\n\t\tG cut(const L &l)const {\n\t\t\tG g;\n\t\t\tREP(i, size()){\n\t\t\t\tconst S &s = edge(i);\n\t\t\t\tif(ccw(l[0], l[1], s[0], 0) >= 0) g.push_back(s[0]);\n\t\t\t\tif(ccw(l[0], l[1], s[0], 0) * ccw(l[0], l[1], s[1], 0) < 0)\n\t\t\t\t\tg.push_back(crosspoint(s, l));\n\t\t\t}\n\t\t\treturn g;\n\t\t}\n\t\tG Voronoi(const vector<P> &p, const int t)const {\n\t\t\tG g = *this;\n\t\t\tREP(i, p.size())if(i!=t){\n\t\t\t\tconst P m = (p[t]+p[i])*0.5;\n\t\t\t\tg = g.cut(L(m, m+(p[i]-p[t])*P(0, 1)));\n\t\t\t}\n\t\t\treturn g;\n\t\t}\n\t};\n\n\tinline P proj(const P &s, const L &t){return t[0] + proj(s-t[0], t[1]-t[0]);}\n\tinline P reflect(const P &s, const L &t){return 2.*proj(s, t) - s;}\n\tinline S reflect(const S &s, const L &t){return S(reflect(s[0], t), reflect(s[1], t));}\n\tBOOL intersect(const S &s, const S &t){\n\t\tconst int p = ccw(t[0], t[1], s[0], 1) * ccw(t[0], t[1], s[1], 1);\n\t\tconst int q = ccw(s[0], s[1], t[0], 1) * ccw(s[0], s[1], t[1], 1);\n\t\treturn (p>0||q>0) ? FALSE : (!p||!q) ? BORDER : TRUE;\n\t}\n\tBOOL intersect(const S &s, const L &l){\n\t\tif(l.online(s[0]) || l.online(s[1])) return BORDER;\n\t\treturn (sig(outp(l.dir(), s[0]-l[0])) * sig(outp(l.dir(), s[1]-l[0])) <= 0);\n\t}\n\tR dist2(const L &l, const P &p){return norm(outp(l.dir(), p - l[0])) / norm(l.dir());}\n\tR dist2(const S &s, const P &p){\n\t\tif(inp(p-s[0], s.dir()) < EPS) return norm(p - s[0]);\n\t\tif(inp(p-s[1], -s.dir()) < EPS) return norm(p - s[1]);\n\t\treturn dist2((const L &)s, p);\n\t}\n\tR dist2(const S &s, const L &l){\n\t\treturn intersect(s, l) ? .0 : min(dist2(l, s[0]), dist2(l, s[1]));\n\t}\n\tR dist2(const S &s, const S &t){\n\t\treturn intersect(s, t) ? .0 : min(min(dist2(s, t[0]), dist2(t, s[0])), \n\t\t\t\t\t\t\t\t\t \t min(dist2(s, t[1]), dist2(t, s[1])));\n\t}\n\ttemplate <class T> R dist2(const G &g, const T& t){ // todo: 内部に完全に含まれる場合\n\t\tR res = INF;\n\t\tREP(i, g.size()) res = min(res, dist2(g.edge(i), t));\n\t\treturn res;\n\t}\n\ttemplate<class S, class T> R dist(const S& s, const T& t){return sqrt(dist2(s, t));}\n\tinline BOOL intersect(const C &a, const C &b){\n\t\treturn less((a.r-b.r)*(a.r-b.r), norm(a-b)) + less(norm(a-b), (a.r+b.r)*(a.r+b.r)) - 1;\n\t}\n\tinline BOOL intersect(const C &c, const L &l){\n\t\treturn less(dist2(l, c), c.r*c.r);\n\t}\n\tinline BOOL intersect(const C &c, const S &s){\n\t\tint d = less(dist2(s, c), c.r*c.r);\n\t\tif(d != TRUE) return d;\n\t\tint p = c.inside(s[0]), q = c.inside(s[1]);\n\t\treturn (p<0 || q<0) ? BORDER : p&q;\n\t}\n\t\n\t/*\n\t * CCWでs[0]->s[1]にかけてが共通部分\n\t */\n\tinline S crosspoint(const C &c1, const C &c2){\n\t\tif(!intersect(c1, c2)) return S();\n\t\tR d = abs(c1 - c2);\n\t\tR x = (c1.r*c1.r - c2.r*c2.r + d*d) / (2*d);\n\t\tR h = sqrt(max(0., c1.r*c1.r - x*x));\n\t\tP u = unit(c2-c1);\n\t\treturn S(c1 + u*x + u*P(0,-1)*h, c1 + u*x + u*P(0,1)*h);\n\t}\n\tinline P crosspoint(const L &l, const L &m){\n\t\tR A = outp(l.dir(), m.dir()), B = outp(l.dir(), l[1] - m[0]);\n\t\tif(!sig(abs(A)) && !sig(abs(B))) return m[0]; // same line\n\t\tif(abs(A) < EPS) assert(false); // !!!PRECONDITION NOT SATISFIED!!!\n\t\treturn m[0] + B / A * (m[1] - m[0]);\n\t}\n\tinline S crosspoint(const C &c, const L &l){\n\t\tR d2 = dist2(l, c);\n\t\tif(c.r*c.r+EPS < d2) return S();\n\t\tP m = proj(c, l);\n\t\tP u = unit(l[1]-l[0]);\n\t\tR d = sqrt(max(.0, c.r*c.r - d2));\n\t\treturn S(m+u*d, m-u*d);\n\t}\n\tinline vector<P> crosspoint(const C &c, const S &s){\n\t\tvector<P> res = crosspoint(c, (const L&)s);\n\t\tRREP(i, res.size()){\n\t\t\tif(inp(res[i]-s[0], s.dir())*inp(res[i]-s[1], -s.dir())<EPS)\n\t\t\t\tres.erase(res.begin() + i);\n\t\t}\n\t\treturn res;\n\t}\n\tinline R commonarea(const C &a, const C &b){\n\t\tif(less(norm(a-b), (a.r-b.r)*(a.r-b.r)) == TRUE) return min(a.r*a.r, b.r*b.r)*PI;\n\t\tif(less((a.r+b.r)*(a.r+b.r), norm(a-b)) == TRUE) return .0;\n\t\tdouble d = abs(a-b);\n\t\tdouble rc = (d*d + a.r*a.r - b.r*b.r) / (2*d);\n\t\tdouble theta = acos(rc / a.r);\n\t\tdouble phi = acos((d - rc) / b.r);\n\t\treturn a.r*a.r*theta + b.r*b.r*phi - d*a.r*sin(theta);\n\t}\n\tvector<L> CommonTangent(C c1, C c2){\n\t\tif(c1.r > c2.r) swap(c1, c2);\n\t\tdouble d = abs(c1-c2);\n\t\tvector<L> res;\n\t\tif(d < EPS) return res;\n\t\tif(d + EPS > c1.r + c2.r){\n\t\t\t// 内接線\n\t\t\tP crs = (c1*c2.r + c2*c1.r) / (c1.r + c2.r);\n\t\t\tdouble rad = asin(c1.r/abs(crs-c1));\n\t\t\tres.push_back(L(crs, crs + (c1-crs)*polar(1., rad)));\n\t\t\tres.push_back(L(crs, crs + (c1-crs)*polar(1., -rad)));\n\t\t}\n\t\tif(c1.r + d + EPS > c2.r){\n\t\t\t// 外接線\n\t\t\tdouble rad = 0.5*PI+asin((c2.r-c1.r) / d);\n\t\t\tP v = unit(c2-c1)*polar(1., rad);\n\t\t\tif(c1.r + d - EPS < c2.r){\n\t\t\t\tres.push_back(L(c1+v*c1.r, c1+v*c1.r+(c1-c2)*P(0, 1)));\n\t\t\t}else{\n\t\t\t\tres.push_back(L(c1+v*c1.r, c2+v*c2.r));\n\t\t\t\tv = 2.*proj(v, c2-c1) - v;\n\t\t\t\tres.push_back(L(c1+v*c1.r, c2+v*c2.r));\n\t\t\t}\n\t\t}\n\t\treturn res;\n\t}\n\t\n\tstruct min_ball {\n\t\tP center;\n\t\tR radius2;\n\t\tmin_ball(const vector<P>& p) {\n\t\t\tFOR(it, p) ps.push_back(*it);\n\t\t}\n\t\tmin_ball& compile() {\n\t\t\tm = 0;\n\t\t\tcenter = P(0, 0);\n\t\t\tradius2 = -1;\n\t\t\tmake_ball(ps.end());\n\t\t\treturn *this;\n\t\t}\n\tprivate:\n\t\tlist<P> ps;\n\t\tlist<P>::iterator supp_end;\n\t\tint m;\n\t\tP v[3], c[3];\n\t\tR z[3], r[3];\n\t\tvoid pop() { --m; }\n\t\tvoid push(const P& p) {\n\t\t\tif (m == 0) {\n\t\t\t\tc[0] = p; r[0] = 0;\n\t\t\t} else {\n\t\t\t\tR e = norm(p-c[m-1]) - r[m-1];\n\t\t\t\tP delta = p - c[0];\n\t\t\t\tv[m] = p - c[0];\n\t\t\t\tfor (int i = 1; i < m; ++i)\n\t\t\t\t\tv[m] -= v[i] * inp(v[i], delta) / z[i];\n\t\t\t\tz[m] = inp(v[m], v[m]);\n\t\t\t\tc[m] = c[m-1] + e*v[m]/z[m]*.5;\n\t\t\t\tr[m] = r[m-1] + e*e/z[m]*.25;\n\t\t\t}\n\t\t\tcenter\t= c[m];\n\t\t\tradius2 = r[m]; ++m;\n\t\t}\n\t\tvoid make_ball(list<P>::iterator i) {\n\t\t\tsupp_end = ps.begin();\n\t\t\tif (m == 3) return;\n\t\t\tfor (list<P>::iterator k = ps.begin(); k != i; ) {\n\t\t\t\tlist<P>::iterator j = k++;\n\t\t\t\tif (norm(*j-center) > radius2) {\n\t\t\t\t\tpush(*j); make_ball(j); pop();\n\t\t\t\t\tmove_to_front(j);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tvoid move_to_front(list<P>::iterator j) {\n\t\t\tif (supp_end == j) ++supp_end;\n\t\t\tps.splice (ps.begin(), ps, j);\n\t\t}\n\t};\n\t\n\tstruct Arrangement{\n\t\tstruct AEdge{\n\t\t\tint u, v, t;\n\t\t\tR cost;\n\t\t\tAEdge(int u=0, int v=0, int t=0, R cost=0)\n\t\t\t\t:u(u), v(v), t(t), cost(cost){}\n\t\t};\n\t\ttypedef vector<vector<AEdge>> AGraph;\n\t\tvector<P> p;\n\t\tAGraph g;\n\t\tArrangement(){}\n\t\tArrangement(vector<S> seg){\n\t\t\tint m = seg.size();\n\t\t\tREP(i, m){\n\t\t\t\tp.push_back(seg[i][0]);\n\t\t\t\tp.push_back(seg[i][1]);\n\t\t\t\tREP(j, i) if(sig(outp(seg[i].dir(), seg[j].dir())) && intersect(seg[i], seg[j]) == TRUE)\n\t\t\t\t\tp.push_back(crosspoint(seg[i], seg[j]));\n\t\t\t}\n\t\t\tsort(ALL(p)); UNIQUE(p);\n\t\t\tint n=p.size();\n\t\t\tg.resize(n);\n\t\t\tREP(i, m){\n\t\t\t\tS &s = seg[i];\n\t\t\t\tvector<pair<R, int>> ps;\n\t\t\t\tREP(j, n) if(s.online(p[j])) ps.emplace_back(norm(p[j] - s[0]), j);\n\t\t\t\tsort(ALL(ps));\n\t\t\t\tREP(j, (int)ps.size()-1){\n\t\t\t\t\tconst int u=ps[j].second;\n\t\t\t\t\tconst int v=ps[j+1].second;\n\t\t\t\t\tg[u].emplace_back(u, v, 0, abs(p[u] - p[v]));\n\t\t\t\t\tg[v].emplace_back(v, u, 0, abs(p[u] - p[v]));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\t\n\t\tint getIdx(P q){\n\t\t\tauto it = lower_bound(ALL(p), q);\n\t\t\tif(it == p.end() || *it != q) return -1;\n\t\t\treturn it - p.begin();\n\t\t}\n\t};\n\n\tstruct DualGraph{\n\t\tstruct DEdge{\n\t\t\tint u, v, f, l;\n\t\t\tR a;\n\t\t\tDEdge(int u, int v, R a):u(u),v(v),f(0),l(0){\n\t\t\t\twhile(PI < a) a -= 2*PI;\n\t\t\t\twhile(a < -PI) a += 2*PI;\n\t\t\t\tthis->a = a;\n\t\t\t}\n\t\t\tbool operator==(const DEdge &opp) const{\n\t\t\t\treturn v==opp.v;\n\t\t\t}\n\t\t\tbool operator<(const DEdge &opp) const{\n\t\t\t\treturn a>opp.a;\n\t\t\t}\n\t\t\tbool operator<(const R &opp) const{\n\t\t\t\treturn a>opp;\n\t\t\t}\n\t\t\tfriend ostream& operator<<(ostream &os, const DEdge &t) { return os<<\"(\"<<t.u<<\",\"<<t.v<<\",\"<<t.a*180/PI<<\")\";}\n\t\t};\n\t\t\n\t\tint n;\n\t\tvector<P> p;\n\t\tvector<vector<DEdge>> g;\n\t\tUnionFind uf;\n\t\tDualGraph(const vector<P> &p):p(p),g(p.size()),n(p.size()),uf(p.size()){}\n\t\tvoid add_edge(const int s, const int t){\n\t\t\tR a = arg(p[t]-p[s]);\n\t\t\tg[s].emplace_back(s, t, a);\n\t\t\tg[t].emplace_back(t, s, a > 0 ? a-PI : a+PI);\n\t\t\tuf.unionSet(s, t);\n\t\t}\n\t\tvector<G> poly;\n\t\t\n\t\tvoid add_polygon(int s, int t, R a){\n\t\t\tauto e = lower_bound(ALL(g[s]), a-EPS);\n\t\t\tif(e == g[s].end()) e = g[s].begin();\n\t\t\tif(e->f) return;\n\t\t\te->f = 1;\n\t\t\te->l = t;\n\t\t\tpoly[t].push_back(p[s]);\n\t\t\tadd_polygon(e->v, t, e->a > 0 ? e->a-PI : e->a+PI);\n\t\t}\n\t\t\n\t\tvoid toSpanningTree(){\n\t\t\tvector<pair<R, pii>> e;\n\t\t\tREP(i, n)REP(j, i)if(!uf.findSet(i, j))\n\t\t\t\te.emplace_back(norm(p[i]-p[j]), pii(i, j));\n\t\t\tsort(ALL(e));\n\t\t\tREP(ii, e.size()){\n\t\t\t\tconst int i = e[ii].second.first;\n\t\t\t\tconst int j = e[ii].second.second;\n\t\t\t\tif(uf.findSet(i, j)) continue;\n\t\t\t\tconst S s(p[i], p[j]);\n\t\t\t\tif([&](){\n\t\t\t\t\tREP(k, n)if(i!=k&&j!=k){\n\t\t\t\t\t\tFOR(it, g[k])\n\t\t\t\t\t\tif(intersect(s, S(p[k], p[it->v])) == TRUE) return 0;\n\t\t\t\t\t}\n\t\t\t\t\treturn 1;\n\t\t\t\t}()) add_edge(i, j);\n\t\t\t}\n\t\t}\n\t\t\n\t\tvector<vi> dual(){\n\t\t\ttoSpanningTree();\n\t\t\t\n\t\t\tREP(i, n){\n\t\t\t\tsort(ALL(g[i]));\n\t\t\t\tUNIQUE(g[i]);\n\t\t\t}\n\t\t\tint s = min_element(ALL(p)) - p.begin();\n\t\t\tpoly.emplace_back();\n\t\t\tadd_polygon(s, poly.size()-1, -PI*.5);\n\t\t\tREP(i, n)REP(j, g[i].size())if(!g[i][j].f){\n\t\t\t\tpoly.emplace_back();\n\t\t\t\tadd_polygon(i, poly.size()-1, g[i][j].a+2.*EPS);\n\t\t\t}\n\t\t\tint m = poly.size();\n\t\t\tvector<vi> dg(m);\n\t\t\tvector<unordered_map<int, int>> rev(n);\n\t\t\tREP(i, n)REP(j, g[i].size()){\n\t\t\t\tint u = i, v = g[i][j].v, l = g[i][j].l;\n\t\t\t\tif(u>v) swap(u, v);\n\t\t\t\tif(rev[u].find(v) != rev[u].end()){\n\t\t\t\t\tif(l != rev[u][v]){\n\t\t\t\t\t\tdg[l].push_back(rev[u][v]);\n\t\t\t\t\t\tdg[rev[u][v]].push_back(l);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\telse rev[u][v] = l;\n\t\t\t}\n\t\t\tREP(i, m){\n\t\t\t\tsort(ALL(dg[i]));\n\t\t\t\tUNIQUE(dg[i]);\n\t\t\t}\n\t\t\treturn dg;\n\t\t}\n\t};\n\n#undef SELF\n#undef at\n}\n\nusing namespace geom;\n\nint f = 0;\nnamespace std{\n\tbool operator<(const P &a, const P &b){return sig(a.X-b.X) ? a.X < b.X : a.Y+EPS < b.Y;}\n\tbool operator==(const P &a, const P &b){return abs(a-b) < EPS;}\n\tistream& operator>>(istream &is, P &p){R x,y;is>>x>>y;p=P(x, y);return is;}\n\tistream& operator>>(istream &is, L &l){l.resize(2);return is >> l[0] >> l[1];}\n\tistream& operator>>(istream &is, C &c){return is >> (P &)c >> c.r;}\n\tconst double B = 500;\n\tconst double Z = .2;\n\tostream& operator<<(ostream &os, const C &c){return os << \"circle(\"<<B+Z*(c.X)<<\", \"<<1000-B-Z*(c.Y)<<\", \"<<Z*(c.r)<<\")\";}\n\tostream& operator<<(ostream &os, const P &p){return os << C(p, 2./Z);}\n\tostream& operator<<(ostream &os, const S &s){return os << \"line(\"<<B+Z*(s[0].X)<<\", \"<<1000-B-Z*(s[0].Y)<<\", \"<<B+Z*(s[1].X)<<\", \"<<1000-B-Z*(s[1].Y)<<\")\";}\n\tostream& operator<<(ostream &os, const G &g){REP(i, g.size()) cout << g.edge(i) << endl;return os;}\n\n}\n\nbool bipartite_matching_dfs(\n\tint v, const vector< vector<int> > &conn,\n\tvector<bool> &used, vector<int> &match)\n{\n\tused[v] = true;\n\tfor(int i = 0; i < conn[v].size(); ++i){\n\t\tint u = conn[v][i], w = match[u];\n\t\tif(w < 0 || (!used[w] && bipartite_matching_dfs(w, conn, used, match))){\n\t\t\tmatch[v] = u;\n\t\t\tmatch[u] = v;\n\t\t\treturn true;\n\t\t}\n\t}\n\treturn false;\n}\n//O(E√V)\nint bipartite_matching(const vector< vector<int> > &conn){\n\tint res = 0;\n\tvector<int> match(conn.size(), -1);\n\tvector<bool> used(conn.size(), false);\n\tfor(int v = 0; v < conn.size(); ++v){\n\t\tif(match[v] < 0){\n\t\t\tfill(used.begin(), used.end(), false);\n\t\t\tif(bipartite_matching_dfs(v, conn, used, match)){ ++res; }\n\t\t}\n\t}\n\treturn res;\n}\n\nint n, k;\n\nint main(int argc, char *argv[]){\n\tios::sync_with_stdio(false);\n\tcin >> n >> k;\n\tvector<C> c(n);\n\tREP(i, n) cin >> c[i];\n\tvector<R> x;\n\tmap<P, int> idx;\n\tvector<unordered_set<int>> in;\n\tvector<P> p;\n\tREP(i, n){\n\t\tx.push_back(c[i].X - c[i].r);\n\t\tx.push_back(c[i].X);\n\t\tx.push_back(c[i].X + c[i].r);\n\t\tREP(j, i){\n\t\t\tS s = crosspoint(c[i], c[j]);\n\t\t\tif(s.size() == 2 && abs(s[0]-s[1]) < EPS) s.resize(1);\n\t\t\tFOR(it, s){\n\t\t\t\tif(idx.find(*it) == idx.end()){\n\t\t\t\t\tidx[*it] = p.size();\n\t\t\t\t\tp.push_back(*it);\n\t\t\t\t\tin.emplace_back();\n\t\t\t\t}\n\t\t\t\tin[idx[*it]].insert(i);\n\t\t\t\tin[idx[*it]].insert(j);\n\t\t\t\tx.push_back(it->X);\n\t\t\t}\n\t\t\tif(s.size() == 2 && abs(s[0]-s[1])>EPS){\n\t\t\t\tx.push_back((c[i] + unit((s[0]+s[1])*0.5 - c[i]) * c[i].r).X);\n\t\t\t\tx.push_back((c[j] + unit((s[0]+s[1])*0.5 - c[j]) * c[j].r).X);\n\t\t\t}\n\t\t}\n\t}\n\tsort(ALL(x));UNIQUE(x);\n\tREP(i, n){\n\t\tREP(j, x.size()){\n\t\t\tL l(P(x[j], 0.), P(x[j], 10.));\n\t\t\tS s = crosspoint(c[i], l);\n\t\t\tif(s.size() == 2 && norm(s[0]-s[1]) < EPS) s.resize(1);\n\t\t\tFOR(it, s){\n\t\t\t\tif(idx.find(*it) == idx.end()){\n\t\t\t\t\tidx[*it] = p.size();\n\t\t\t\t\tp.push_back(*it);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tin.resize(p.size());\n\tDualGraph dg(p);\n\tREP(i, n){\n\t\tvector<pair<R, int>> q;\n\t\tREP(j, p.size()){\n\t\t\tR d = abs(abs(p[j] - c[i]) - c[i].r);\n\t\t\tif(in[j].find(i)!=in[j].end() || [&](){\n\t\t\t\tREP(k, n)if(k!=i){\n\t\t\t\t\tif(abs(abs(p[j] - c[k]) - c[k].r) < d) return 0;\n\t\t\t\t}\n\t\t\t\treturn 1;\n\t\t\t}()) q.emplace_back(arg(p[j] - c[i]), j);\n\t\t}\n\t\tsort(ALL(q));\n\t\tREP(j, (int)q.size()) dg.add_edge(q[j].second, q[(j+1)%q.size()].second);\n\t}\n\tEPS = 1e-8;\n\tvector<vi> g = dg.dual();\n\tif(k > 1) cout << dg.poly.size() - 1 << endl;\n\telse{\n\t\tREPS(i, (int)g.size()-1){\n\t\t\tg[i].erase(remove(ALL(g[i]), 0), g[i].end());\n\t\t}\n\t\tg[0].clear();\n\t\tcout << g.size() - bipartite_matching(g) - 1 << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 580, "memory_kb": 21624, "score_of_the_acc": -1.9828, "final_rank": 7 }, { "submission_id": "aoj_2238_1149553", "code_snippet": "#include <cstdio>\n#include <cmath>\n#include <cstring>\n#include <cstdlib>\n#include <climits>\n#include <ctime>\n#include <queue>\n#include <stack>\n#include <algorithm>\n#include <list>\n#include <vector>\n#include <set>\n#include <map>\n#include <iostream>\n#include <deque>\n#include <complex>\n#include <string>\n#include <iomanip>\n#include <sstream>\n#include <bitset>\n#include <valarray>\n#include <unordered_map>\n#include <iterator>\n#include <assert.h>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned int uint;\ntypedef unsigned char uchar;\ntypedef unsigned long long ull;\ntypedef pair<int, int> pii;\ntypedef pair<ll, ll> pll;\ntypedef vector<int> vi;\n\n#define REP(i,x) for(int i=0;i<(int)(x);i++)\n#define REPS(i,x) for(int i=1;i<=(int)(x);i++)\n#define RREP(i,x) for(int i=((int)(x)-1);i>=0;i--)\n#define RREPS(i,x) for(int i=((int)(x));i>0;i--)\n#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();i++)\n#define RFOR(i,c) for(__typeof((c).rbegin())i=(c).rbegin();i!=(c).rend();i++)\n#define ALL(container) (container).begin(), (container).end()\n#define RALL(container) (container).rbegin(), (container).rend()\n#define SZ(container) ((int)container.size())\n#define mp(a,b) make_pair(a, b)\n#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );\n\ntemplate<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }\ntemplate<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }\ntemplate<class T> ostream& operator<<(ostream &os, const vector<T> &t) {\nos<<\"[\"; FOR(it,t) {if(it!=t.begin()) os<<\",\"; os<<*it;} os<<\"]\"; return os;\n}\ntemplate<class T> ostream& operator<<(ostream &os, const set<T> &t) {\nos<<\"{\"; FOR(it,t) {if(it!=t.begin()) os<<\",\"; os<<*it;} os<<\"}\"; return os;\n}\ntemplate<class S, class T> ostream& operator<<(ostream &os, const pair<S,T> &t) { return os<<\"(\"<<t.first<<\",\"<<t.second<<\")\";}\ntemplate<class S, class T> pair<S,T> operator+(const pair<S,T> &s, const pair<S,T> &t){ return pair<S,T>(s.first+t.first, s.second+t.second);}\ntemplate<class S, class T> pair<S,T> operator-(const pair<S,T> &s, const pair<S,T> &t){ return pair<S,T>(s.first-t.first, s.second-t.second);}\n\nstruct UnionFind {\n vector<int> data;\n UnionFind(int size) : data(size, -1) { }\n bool unionSet(int x, int y) {\n x = root(x); y = root(y);\n if (x != y) {\n if (data[y] < data[x]) swap(x, y);\n data[x] += data[y]; data[y] = x;\n }\n return x != y;\n }\n bool findSet(int x, int y) {\n return root(x) == root(y);\n }\n int root(int x) {\n return data[x] < 0 ? x : data[x] = root(data[x]);\n }\n int size(int x) {\n return -data[root(x)];\n }\n};\n\n\nnamespace geom{\n#define X real()\n#define Y imag()\n#define at(i) ((*this)[i])\n#define SELF (*this)\n\tenum {TRUE = 1, FALSE = 0, BORDER = -1};\n\ttypedef int BOOL;\n\ttypedef double R;\n\tconst R INF = 1e8;\n\tR EPS = 1e-6;\n\tconst R PI = 3.1415926535897932384626;\n\tinline int sig(const R &x) { return (abs(x) < EPS ? 0 : x > 0 ? 1 : -1); }\n\tinline BOOL less(const R &x, const R &y) {return sig(x-y) ? x < y : BORDER;}\n\ttypedef complex<R> P;\n\tinline R norm(const P &p){return p.X*p.X+p.Y*p.Y;}\n\tinline R inp(const P& a, const P& b){return (conj(a)*b).X;}\n\tinline R outp(const P& a, const P& b){return (conj(a)*b).Y;}\n\tinline P unit(const P& p){return p/abs(p);}\n\tinline P proj(const P &s, const P &t){return t*inp(s, t)/norm(t);}\n\tinline int ccw(const P &s, const P &t, const P &p, int adv=0){\n\t\tint res = sig(outp(t-s, p-s));\n\t\tif(res || !adv) return res;\n\t\tif(sig(inp(t-s, p-s)) < 0) return -2;\t// p-s-t\n\t\tif(sig(inp(s-t, p-t)) < 0) return 2;\t// s-t-p\n\t\treturn 0;\t\t\t\t\t\t\t\t// s-p-t\n\t}\n\t\n\t\n\tstruct L : public vector<P>{\t// line\n\t\tL(const P &p1, const P &p2){this->push_back(p1);this->push_back(p2);}\n\t\tL(){}\n\t\tP dir()const {return at(1) - at(0);}\n\t\tBOOL online(const P &p)const {return !sig(outp(p-at(0), dir()));}\n\t};\n\tstruct S : public L{\t// segment\n\t\tS(const P &p1, const P &p2):L(p1, p2){}\n\t\tS(){}\n\t\tBOOL online(const P &p)const {\n\t\t\tif(!sig(norm(p - at(0))) || !sig(norm(p - at(1)))) return BORDER;\n\t\t\treturn !sig(abs(at(0)-p) + abs(at(1) - p) - abs(at(0) - at(1)));\n\t\t}\n\t};\n\tstruct C : public P{\n\t\tC(){}\n\t\tC(const P& p, const R r):P(p), r(r){}\n\t\tR r;\n\t\tBOOL inside(const P& p)const { return less(norm(p-SELF), r*r);}\n\t};\n\tstruct F : public C{\n\t\tR s, t;\n\t\tF(const C &c, R ss, R tt):C(c), s(ss), t(tt){\n\t\t\tif(PI < s) s -= 2*PI;\n\t\t\tif(PI < t) t -= 2*PI;\n\t\t}\n\t\tBOOL inside(const P& p)const {\n\t\t\tP v = p - SELF;\n\t\t\tif(!sig(norm(v))) return BORDER;\n\t\t\tR a = arg(v);\n\t\t\tif(t < s){\n\t\t\t\tif((!less(s, a) && !less(a, t)) || !less(norm(v), r*r)) return FALSE;\n\t\t\t\treturn less(s, a) | less(a, t) | less(norm(v), r*r);\n\t\t\t}else{\n\t\t\t\tif(!less(s, a) || !less(a, t) || !less(norm(v), r*r)) return FALSE;\n\t\t\t\treturn less(s, a) | less(a, t) | less(norm(v), r*r);\n\t\t\t}\n\t\t}\n\t};\n\tP crosspoint(const L &l, const L &m);\n\tstruct G : public vector<P>{\n\t\tG(size_type size=0):vector(size){}\n\t\tS edge(int i)const {return S(at(i), at(i+1 == size() ? 0 : i+1));}\n\t\tBOOL contains(const P &p)const {\n\t\t\tR sum = .0;\n\t\t\tREP(i, size()){\n\t\t\t\tif(S(at(i), at((i+1)%size())).online(p)) return BORDER;\t// online\n\t\t\t\tsum += arg((at(i) - p) / (at((i+1)%size()) - p));\n\t\t\t}\n\t\t\treturn !!sig(sum);\n\t\t}\n\t\tR area()const {\n\t\t\tR sum = 0;\n\t\t\tREP(i, size()) sum += outp(at(i), at((i+1)%size()));\n\t\t\treturn abs(sum / 2.);\n\t\t}\n\t\t\n\t\tG convex_hull(bool online = false) {\n\t\t\tif(size() < 2) return *this;\n\t\t\tsort(ALL(*this));\n\t\t\tG r;\n\t\t\tr.resize((int)size()*2);\n\t\t\tint k=0;\n\t\t\tfor(int i=0;i<size();r[k++]=at(i++))\n\t\t\t\twhile(k>1 && ccw(r[k-2], r[k-1], at(i)) < 1-online) k--;\n\t\t\tint t = k;\n\t\t\tfor(int i=(int)size()-1;i>=0;r[k++]=at(i--))\n\t\t\t\twhile(k>t && ccw(r[k-2], r[k-1], at(i)) < 1-online) k--;\n\t\t\tr.resize(k-1);\n\t\t\treturn r;\n\t\t}\n\t\tG cut(const L &l)const {\n\t\t\tG g;\n\t\t\tREP(i, size()){\n\t\t\t\tconst S &s = edge(i);\n\t\t\t\tif(ccw(l[0], l[1], s[0], 0) >= 0) g.push_back(s[0]);\n\t\t\t\tif(ccw(l[0], l[1], s[0], 0) * ccw(l[0], l[1], s[1], 0) < 0)\n\t\t\t\t\tg.push_back(crosspoint(s, l));\n\t\t\t}\n\t\t\treturn g;\n\t\t}\n\t\tG Voronoi(const vector<P> &p, const int t)const {\n\t\t\tG g = *this;\n\t\t\tREP(i, p.size())if(i!=t){\n\t\t\t\tconst P m = (p[t]+p[i])*0.5;\n\t\t\t\tg = g.cut(L(m, m+(p[i]-p[t])*P(0, 1)));\n\t\t\t}\n\t\t\treturn g;\n\t\t}\n\t};\n\n\tinline P proj(const P &s, const L &t){return t[0] + proj(s-t[0], t[1]-t[0]);}\n\tinline P reflect(const P &s, const L &t){return 2.*proj(s, t) - s;}\n\tinline S reflect(const S &s, const L &t){return S(reflect(s[0], t), reflect(s[1], t));}\n\tBOOL intersect(const S &s, const S &t){\n\t\tconst int p = ccw(t[0], t[1], s[0], 1) * ccw(t[0], t[1], s[1], 1);\n\t\tconst int q = ccw(s[0], s[1], t[0], 1) * ccw(s[0], s[1], t[1], 1);\n\t\treturn (p>0||q>0) ? FALSE : (!p||!q) ? BORDER : TRUE;\n\t}\n\tBOOL intersect(const S &s, const L &l){\n\t\tif(l.online(s[0]) || l.online(s[1])) return BORDER;\n\t\treturn (sig(outp(l.dir(), s[0]-l[0])) * sig(outp(l.dir(), s[1]-l[0])) <= 0);\n\t}\n\tR dist2(const L &l, const P &p){return norm(outp(l.dir(), p - l[0])) / norm(l.dir());}\n\tR dist2(const S &s, const P &p){\n\t\tif(inp(p-s[0], s.dir()) < EPS) return norm(p - s[0]);\n\t\tif(inp(p-s[1], -s.dir()) < EPS) return norm(p - s[1]);\n\t\treturn dist2((const L &)s, p);\n\t}\n\tR dist2(const S &s, const L &l){\n\t\treturn intersect(s, l) ? .0 : min(dist2(l, s[0]), dist2(l, s[1]));\n\t}\n\tR dist2(const S &s, const S &t){\n\t\treturn intersect(s, t) ? .0 : min(min(dist2(s, t[0]), dist2(t, s[0])), \n\t\t\t\t\t\t\t\t\t \t min(dist2(s, t[1]), dist2(t, s[1])));\n\t}\n\ttemplate <class T> R dist2(const G &g, const T& t){ // todo: 内部に完全に含まれる場合\n\t\tR res = INF;\n\t\tREP(i, g.size()) res = min(res, dist2(g.edge(i), t));\n\t\treturn res;\n\t}\n\ttemplate<class S, class T> R dist(const S& s, const T& t){return sqrt(dist2(s, t));}\n\tinline BOOL intersect(const C &a, const C &b){\n\t\treturn less((a.r-b.r)*(a.r-b.r), norm(a-b)) + less(norm(a-b), (a.r+b.r)*(a.r+b.r)) - 1;\n\t}\n\tinline BOOL intersect(const C &c, const L &l){\n\t\treturn less(dist2(l, c), c.r*c.r);\n\t}\n\tinline BOOL intersect(const C &c, const S &s){\n\t\tint d = less(dist2(s, c), c.r*c.r);\n\t\tif(d != TRUE) return d;\n\t\tint p = c.inside(s[0]), q = c.inside(s[1]);\n\t\treturn (p<0 || q<0) ? BORDER : p&q;\n\t}\n\t\n\t/*\n\t * CCWでs[0]->s[1]にかけてが共通部分\n\t */\n\tinline S crosspoint(const C &c1, const C &c2){\n\t\tif(!intersect(c1, c2)) return S();\n\t\tR d = abs(c1 - c2);\n\t\tR x = (c1.r*c1.r - c2.r*c2.r + d*d) / (2*d);\n\t\tR h = sqrt(max(0., c1.r*c1.r - x*x));\n\t\tP u = unit(c2-c1);\n\t\treturn S(c1 + u*x + u*P(0,-1)*h, c1 + u*x + u*P(0,1)*h);\n\t}\n\tinline P crosspoint(const L &l, const L &m){\n\t\tR A = outp(l.dir(), m.dir()), B = outp(l.dir(), l[1] - m[0]);\n\t\tif(!sig(abs(A)) && !sig(abs(B))) return m[0]; // same line\n\t\tif(abs(A) < EPS) assert(false); // !!!PRECONDITION NOT SATISFIED!!!\n\t\treturn m[0] + B / A * (m[1] - m[0]);\n\t}\n\tinline S crosspoint(const C &c, const L &l){\n\t\tR d2 = dist2(l, c);\n\t\tif(c.r*c.r+EPS < d2) return S();\n\t\tP m = proj(c, l);\n\t\tP u = unit(l[1]-l[0]);\n\t\tR d = sqrt(max(.0, c.r*c.r - d2));\n\t\treturn S(m+u*d, m-u*d);\n\t}\n\tinline vector<P> crosspoint(const C &c, const S &s){\n\t\tvector<P> res = crosspoint(c, (const L&)s);\n\t\tRREP(i, res.size()){\n\t\t\tif(inp(res[i]-s[0], s.dir())*inp(res[i]-s[1], -s.dir())<EPS)\n\t\t\t\tres.erase(res.begin() + i);\n\t\t}\n\t\treturn res;\n\t}\n\tinline R commonarea(const C &a, const C &b){\n\t\tif(less(norm(a-b), (a.r-b.r)*(a.r-b.r)) == TRUE) return min(a.r*a.r, b.r*b.r)*PI;\n\t\tif(less((a.r+b.r)*(a.r+b.r), norm(a-b)) == TRUE) return .0;\n\t\tdouble d = abs(a-b);\n\t\tdouble rc = (d*d + a.r*a.r - b.r*b.r) / (2*d);\n\t\tdouble theta = acos(rc / a.r);\n\t\tdouble phi = acos((d - rc) / b.r);\n\t\treturn a.r*a.r*theta + b.r*b.r*phi - d*a.r*sin(theta);\n\t}\n\tvector<L> CommonTangent(C c1, C c2){\n\t\tif(c1.r > c2.r) swap(c1, c2);\n\t\tdouble d = abs(c1-c2);\n\t\tvector<L> res;\n\t\tif(d < EPS) return res;\n\t\tif(d + EPS > c1.r + c2.r){\n\t\t\t// 内接線\n\t\t\tP crs = (c1*c2.r + c2*c1.r) / (c1.r + c2.r);\n\t\t\tdouble rad = asin(c1.r/abs(crs-c1));\n\t\t\tres.push_back(L(crs, crs + (c1-crs)*polar(1., rad)));\n\t\t\tres.push_back(L(crs, crs + (c1-crs)*polar(1., -rad)));\n\t\t}\n\t\tif(c1.r + d + EPS > c2.r){\n\t\t\t// 外接線\n\t\t\tdouble rad = 0.5*PI+asin((c2.r-c1.r) / d);\n\t\t\tP v = unit(c2-c1)*polar(1., rad);\n\t\t\tif(c1.r + d - EPS < c2.r){\n\t\t\t\tres.push_back(L(c1+v*c1.r, c1+v*c1.r+(c1-c2)*P(0, 1)));\n\t\t\t}else{\n\t\t\t\tres.push_back(L(c1+v*c1.r, c2+v*c2.r));\n\t\t\t\tv = 2.*proj(v, c2-c1) - v;\n\t\t\t\tres.push_back(L(c1+v*c1.r, c2+v*c2.r));\n\t\t\t}\n\t\t}\n\t\treturn res;\n\t}\n\t\n\tstruct min_ball {\n\t\tP center;\n\t\tR radius2;\n\t\tmin_ball(const vector<P>& p) {\n\t\t\tFOR(it, p) ps.push_back(*it);\n\t\t}\n\t\tmin_ball& compile() {\n\t\t\tm = 0;\n\t\t\tcenter = P(0, 0);\n\t\t\tradius2 = -1;\n\t\t\tmake_ball(ps.end());\n\t\t\treturn *this;\n\t\t}\n\tprivate:\n\t\tlist<P> ps;\n\t\tlist<P>::iterator supp_end;\n\t\tint m;\n\t\tP v[3], c[3];\n\t\tR z[3], r[3];\n\t\tvoid pop() { --m; }\n\t\tvoid push(const P& p) {\n\t\t\tif (m == 0) {\n\t\t\t\tc[0] = p; r[0] = 0;\n\t\t\t} else {\n\t\t\t\tR e = norm(p-c[m-1]) - r[m-1];\n\t\t\t\tP delta = p - c[0];\n\t\t\t\tv[m] = p - c[0];\n\t\t\t\tfor (int i = 1; i < m; ++i)\n\t\t\t\t\tv[m] -= v[i] * inp(v[i], delta) / z[i];\n\t\t\t\tz[m] = inp(v[m], v[m]);\n\t\t\t\tc[m] = c[m-1] + e*v[m]/z[m]*.5;\n\t\t\t\tr[m] = r[m-1] + e*e/z[m]*.25;\n\t\t\t}\n\t\t\tcenter\t= c[m];\n\t\t\tradius2 = r[m]; ++m;\n\t\t}\n\t\tvoid make_ball(list<P>::iterator i) {\n\t\t\tsupp_end = ps.begin();\n\t\t\tif (m == 3) return;\n\t\t\tfor (list<P>::iterator k = ps.begin(); k != i; ) {\n\t\t\t\tlist<P>::iterator j = k++;\n\t\t\t\tif (norm(*j-center) > radius2) {\n\t\t\t\t\tpush(*j); make_ball(j); pop();\n\t\t\t\t\tmove_to_front(j);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tvoid move_to_front(list<P>::iterator j) {\n\t\t\tif (supp_end == j) ++supp_end;\n\t\t\tps.splice (ps.begin(), ps, j);\n\t\t}\n\t};\n\t\n\tstruct Arrangement{\n\t\tstruct AEdge{\n\t\t\tint u, v, t;\n\t\t\tR cost;\n\t\t\tAEdge(int u=0, int v=0, int t=0, R cost=0)\n\t\t\t\t:u(u), v(v), t(t), cost(cost){}\n\t\t};\n\t\ttypedef vector<vector<AEdge>> AGraph;\n\t\tvector<P> p;\n\t\tAGraph g;\n\t\tArrangement(){}\n\t\tArrangement(vector<S> seg){\n\t\t\tint m = seg.size();\n\t\t\tREP(i, m){\n\t\t\t\tp.push_back(seg[i][0]);\n\t\t\t\tp.push_back(seg[i][1]);\n\t\t\t\tREP(j, i) if(sig(outp(seg[i].dir(), seg[j].dir())) && intersect(seg[i], seg[j]) == TRUE)\n\t\t\t\t\tp.push_back(crosspoint(seg[i], seg[j]));\n\t\t\t}\n\t\t\tsort(ALL(p)); UNIQUE(p);\n\t\t\tint n=p.size();\n\t\t\tg.resize(n);\n\t\t\tREP(i, m){\n\t\t\t\tS &s = seg[i];\n\t\t\t\tvector<pair<R, int>> ps;\n\t\t\t\tREP(j, n) if(s.online(p[j])) ps.emplace_back(norm(p[j] - s[0]), j);\n\t\t\t\tsort(ALL(ps));\n\t\t\t\tREP(j, (int)ps.size()-1){\n\t\t\t\t\tconst int u=ps[j].second;\n\t\t\t\t\tconst int v=ps[j+1].second;\n\t\t\t\t\tg[u].emplace_back(u, v, 0, abs(p[u] - p[v]));\n\t\t\t\t\tg[v].emplace_back(v, u, 0, abs(p[u] - p[v]));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\t\n\t\tint getIdx(P q){\n\t\t\tauto it = lower_bound(ALL(p), q);\n\t\t\tif(it == p.end() || *it != q) return -1;\n\t\t\treturn it - p.begin();\n\t\t}\n\t};\n\n\tstruct DualGraph{\n\t\tstruct DEdge{\n\t\t\tint u, v, f, l;\n\t\t\tR a;\n\t\t\tDEdge(int u, int v, R a):u(u),v(v),f(0),l(0){\n\t\t\t\twhile(PI < a) a -= 2*PI;\n\t\t\t\twhile(a < -PI) a += 2*PI;\n\t\t\t\tthis->a = a;\n\t\t\t}\n\t\t\tbool operator==(const DEdge &opp) const{\n\t\t\t\treturn v==opp.v;\n\t\t\t}\n\t\t\tbool operator<(const DEdge &opp) const{\n\t\t\t\treturn a>opp.a;\n\t\t\t}\n\t\t\tbool operator<(const R &opp) const{\n\t\t\t\treturn a>opp;\n\t\t\t}\n\t\t\tfriend ostream& operator<<(ostream &os, const DEdge &t) { return os<<\"(\"<<t.u<<\",\"<<t.v<<\",\"<<t.a*180/PI<<\")\";}\n\t\t};\n\t\t\n\t\tint n;\n\t\tvector<P> p;\n\t\tvector<vector<DEdge>> g;\n\t\tUnionFind uf;\n\t\tDualGraph(const vector<P> &p):p(p),g(p.size()),n(p.size()),uf(p.size()){}\n\t\tvoid add_edge(const int s, const int t){\n\t\t\tR a = arg(p[t]-p[s]);\n\t\t\tg[s].emplace_back(s, t, a);\n\t\t\tg[t].emplace_back(t, s, a > 0 ? a-PI : a+PI);\n\t\t\tuf.unionSet(s, t);\n\t\t}\n\t\tvector<G> poly;\n\t\t\n\t\tvoid add_polygon(int s, int t, R a){\n\t\t\tauto e = lower_bound(ALL(g[s]), a-EPS);\n\t\t\tif(e == g[s].end()) e = g[s].begin();\n\t\t\tif(e->f) return;\n\t\t\te->f = 1;\n\t\t\te->l = t;\n\t\t\tpoly[t].push_back(p[s]);\n\t\t\tadd_polygon(e->v, t, e->a > 0 ? e->a-PI : e->a+PI);\n\t\t}\n\t\t\n\t\tvoid toSpanningTree(){\n\t\t\tvector<pair<R, pii>> e;\n\t\t\tREP(i, n)REP(j, i)if(!uf.findSet(i, j))\n\t\t\t\te.emplace_back(norm(p[i]-p[j]), pii(i, j));\n\t\t\tsort(ALL(e));\n\t\t\tREP(ii, e.size()){\n\t\t\t\tconst int i = e[ii].second.first;\n\t\t\t\tconst int j = e[ii].second.second;\n\t\t\t\tif(uf.findSet(i, j)) continue;\n\t\t\t\tconst S s(p[i], p[j]);\n\t\t\t\tif([&](){\n\t\t\t\t\tREP(k, n)if(i!=k&&j!=k){\n\t\t\t\t\t\tFOR(it, g[k])\n\t\t\t\t\t\tif(intersect(s, S(p[k], p[it->v])) == TRUE) return 0;\n\t\t\t\t\t}\n\t\t\t\t\treturn 1;\n\t\t\t\t}()) add_edge(i, j);\n\t\t\t}\n\t\t}\n\t\t\n\t\tvector<vi> dual(){\n\t\t\ttoSpanningTree();\n\t\t\t\n\t\t\tREP(i, n){\n\t\t\t\tsort(ALL(g[i]));\n\t\t\t\tUNIQUE(g[i]);\n\t\t\t}\n\t\t\tint s = min_element(ALL(p)) - p.begin();\n\t\t\tpoly.emplace_back();\n\t\t\tadd_polygon(s, poly.size()-1, -PI*.5);\n\t\t\tREP(i, n)REP(j, g[i].size())if(!g[i][j].f){\n\t\t\t\tpoly.emplace_back();\n\t\t\t\tadd_polygon(i, poly.size()-1, g[i][j].a+2.*EPS);\n\t\t\t}\n\t\t\tint m = poly.size();\n\t\t\tvector<vi> dg(m);\n\t\t\tvector<unordered_map<int, int>> rev(n);\n\t\t\tREP(i, n)REP(j, g[i].size()){\n\t\t\t\tint u = i, v = g[i][j].v, l = g[i][j].l;\n\t\t\t\tif(u>v) swap(u, v);\n\t\t\t\tif(rev[u].find(v) != rev[u].end()){\n\t\t\t\t\tif(l != rev[u][v]){\n\t\t\t\t\t\tdg[l].push_back(rev[u][v]);\n\t\t\t\t\t\tdg[rev[u][v]].push_back(l);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\telse rev[u][v] = l;\n\t\t\t}\n\t\t\tREP(i, m){\n\t\t\t\tsort(ALL(dg[i]));\n\t\t\t\tUNIQUE(dg[i]);\n\t\t\t}\n\t\t\treturn dg;\n\t\t}\n\t};\n\n#undef SELF\n#undef at\n}\n\nusing namespace geom;\n\nint f = 0;\nnamespace std{\n\tbool operator<(const P &a, const P &b){return sig(a.X-b.X) ? a.X < b.X : a.Y+EPS < b.Y;}\n\tbool operator==(const P &a, const P &b){return abs(a-b) < EPS;}\n\tistream& operator>>(istream &is, P &p){R x,y;is>>x>>y;p=P(x, y);return is;}\n\tistream& operator>>(istream &is, L &l){l.resize(2);return is >> l[0] >> l[1];}\n\tistream& operator>>(istream &is, C &c){return is >> (P &)c >> c.r;}\n\tconst double B = 500;\n\tconst double Z = .2;\n\tostream& operator<<(ostream &os, const C &c){return os << \"circle(\"<<B+Z*(c.X)<<\", \"<<1000-B-Z*(c.Y)<<\", \"<<Z*(c.r)<<\")\";}\n\tostream& operator<<(ostream &os, const P &p){return os << C(p, 2./Z);}\n\tostream& operator<<(ostream &os, const S &s){return os << \"line(\"<<B+Z*(s[0].X)<<\", \"<<1000-B-Z*(s[0].Y)<<\", \"<<B+Z*(s[1].X)<<\", \"<<1000-B-Z*(s[1].Y)<<\")\";}\n\tostream& operator<<(ostream &os, const G &g){REP(i, g.size()) cout << g.edge(i) << endl;return os;}\n\n}\n\nbool bipartite_matching_dfs(\n\tint v, const vector< vector<int> > &conn,\n\tvector<bool> &used, vector<int> &match)\n{\n\tused[v] = true;\n\tfor(int i = 0; i < conn[v].size(); ++i){\n\t\tint u = conn[v][i], w = match[u];\n\t\tif(w < 0 || (!used[w] && bipartite_matching_dfs(w, conn, used, match))){\n\t\t\tmatch[v] = u;\n\t\t\tmatch[u] = v;\n\t\t\treturn true;\n\t\t}\n\t}\n\treturn false;\n}\n//O(E√V)\nint bipartite_matching(const vector< vector<int> > &conn){\n\tint res = 0;\n\tvector<int> match(conn.size(), -1);\n\tvector<bool> used(conn.size(), false);\n\tfor(int v = 0; v < conn.size(); ++v){\n\t\tif(match[v] < 0){\n\t\t\tfill(used.begin(), used.end(), false);\n\t\t\tif(bipartite_matching_dfs(v, conn, used, match)){ ++res; }\n\t\t}\n\t}\n\treturn res;\n}\n\nint n, k;\n\nint main(int argc, char *argv[]){\n\tios::sync_with_stdio(false);\n\tcin >> n >> k;\n\tvector<C> c(n);\n\tREP(i, n){\n\t\tcin >> c[i];\n//\t\tc[i] *= polar(1., 0);\n\t}\n\tvector<R> x;\n\tmap<P, int> idx;\n\tvector<vi> in(10000);\n\tvector<P> p;\n\tREP(i, n){\n\t\tx.push_back(c[i].X - c[i].r);\n\t\tx.push_back(c[i].X);\n\t\tx.push_back(c[i].X + c[i].r);\n\t\tREP(j, i){\n\t\t\tS s = crosspoint(c[i], c[j]);\n\t\t\tif(s.size() == 2 && abs(s[0]-s[1]) < EPS) s.resize(1);\n\t\t\tFOR(it, s){\n\t\t\t\tif(idx.find(*it) == idx.end()){\n\t\t\t\t\tidx[*it] = p.size();\n\t\t\t\t\tp.push_back(*it);\n\t\t\t\t}\n\t\t\t\tin[idx[*it]].push_back(i);\n\t\t\t\tin[idx[*it]].push_back(j);\n\t\t\t\tx.push_back(it->X);\n\t\t\t}\n\t\t\tif(s.size() == 2 && abs(s[0]-s[1])>EPS){\n\t\t\t\tx.push_back((c[i] + unit((s[0]+s[1])*0.5 - c[i]) * c[i].r).X);\n\t\t\t\tx.push_back((c[j] + unit((s[0]+s[1])*0.5 - c[j]) * c[j].r).X);\n\t\t\t}\n\t\t}\n\t}\n\tsort(ALL(x));UNIQUE(x);\n//cerr << x << endl;\n\tREP(i, n){\n\t\tREP(j, x.size()){\n\t\t\tL l(P(x[j], 0.), P(x[j], 10.));\n\t\t\tS s = crosspoint(c[i], l);\n\t\t\tif(s.size() == 2 && abs(s[0]-s[1]) < EPS) s.resize(1);\n\t\t\tFOR(it, s){\n\t\t\t\tif(idx.find(*it) == idx.end()){\n\t\t\t\t\tidx[*it] = p.size();\n\t\t\t\t\tp.push_back(*it);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tin.resize(p.size());\n//\t\tcerr << in << endl;\n//\tsort(ALL(p));\n//cerr << p << endl << endl;\n//\tUNIQUE(p);\n//FOR(it, p)cout << *it << endl;\n//cerr << \"p enum\" << endl;\n\tDualGraph dg(p);\n\tREP(i, n){\n\t\tvector<pair<R, int>> q;\n\t\tREP(j, p.size()){\n\t\t\tR d = abs(abs(p[j] - c[i]) - c[i].r);\n\t\t\tif(count(ALL(in[j]), i) || (!sig(abs(p[j] - c[i]) - c[i].r) && [&](){\n\t\t\t\tREP(k, n)if(k!=i){\n\t\t\t\t\tif(abs(abs(p[j] - c[k]) - c[k].r) < d) return 0;\n\t\t\t\t}\n\t\t\t\treturn 1;\n\t\t\t}())) q.emplace_back(arg(p[j] - c[i]), j);\n\t\t}\n\t\tsort(ALL(q));\n//\t\tcerr << q << endl;\n\t\tREP(j, (int)q.size()) dg.add_edge(q[j].second, q[(j+1)%q.size()].second);\n\t}\n\tEPS = 1e-8;\n//cerr << \"edge\" << endl;\n\tvector<vi> g = dg.dual();\n//cerr << \"dual\" << endl;\n//FOR(it, c) cout << *it << endl;\n//if(argc == 1) FOR(it, dg.poly) if(it != dg.poly.begin())cout << *it << endl;\n//else cout << dg.poly[atoi(argv[1])] << endl;\n\tif(k > 1) cout << dg.poly.size() - 1 << endl;\n\telse{\n\t\tREPS(i, (int)g.size()-1){\n\t\t\tg[i].erase(remove(ALL(g[i]), 0), g[i].end());\n\t\t}\n\t\tg[0].clear();\n//\t\tcerr << g << endl;\n\t\tcout << g.size() - bipartite_matching(g) - 1 << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 590, "memory_kb": 19776, "score_of_the_acc": -1.9068, "final_rank": 6 }, { "submission_id": "aoj_2238_1149480", "code_snippet": "#include <cstdio>\n#include <cmath>\n#include <cstring>\n#include <cstdlib>\n#include <climits>\n#include <ctime>\n#include <queue>\n#include <stack>\n#include <algorithm>\n#include <list>\n#include <vector>\n#include <set>\n#include <map>\n#include <iostream>\n#include <deque>\n#include <complex>\n#include <string>\n#include <iomanip>\n#include <sstream>\n#include <bitset>\n#include <valarray>\n#include <unordered_map>\n#include <iterator>\n#include <assert.h>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned int uint;\ntypedef unsigned char uchar;\ntypedef unsigned long long ull;\ntypedef pair<int, int> pii;\ntypedef pair<ll, ll> pll;\ntypedef vector<int> vi;\n\n#define REP(i,x) for(int i=0;i<(int)(x);i++)\n#define REPS(i,x) for(int i=1;i<=(int)(x);i++)\n#define RREP(i,x) for(int i=((int)(x)-1);i>=0;i--)\n#define RREPS(i,x) for(int i=((int)(x));i>0;i--)\n#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();i++)\n#define RFOR(i,c) for(__typeof((c).rbegin())i=(c).rbegin();i!=(c).rend();i++)\n#define ALL(container) (container).begin(), (container).end()\n#define RALL(container) (container).rbegin(), (container).rend()\n#define SZ(container) ((int)container.size())\n#define mp(a,b) make_pair(a, b)\n#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );\n\ntemplate<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }\ntemplate<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }\ntemplate<class T> ostream& operator<<(ostream &os, const vector<T> &t) {\nos<<\"[\"; FOR(it,t) {if(it!=t.begin()) os<<\",\"; os<<*it;} os<<\"]\"; return os;\n}\ntemplate<class T> ostream& operator<<(ostream &os, const set<T> &t) {\nos<<\"{\"; FOR(it,t) {if(it!=t.begin()) os<<\",\"; os<<*it;} os<<\"}\"; return os;\n}\ntemplate<class S, class T> ostream& operator<<(ostream &os, const pair<S,T> &t) { return os<<\"(\"<<t.first<<\",\"<<t.second<<\")\";}\ntemplate<class S, class T> pair<S,T> operator+(const pair<S,T> &s, const pair<S,T> &t){ return pair<S,T>(s.first+t.first, s.second+t.second);}\ntemplate<class S, class T> pair<S,T> operator-(const pair<S,T> &s, const pair<S,T> &t){ return pair<S,T>(s.first-t.first, s.second-t.second);}\n\nstruct UnionFind {\n vector<int> data;\n UnionFind(int size) : data(size, -1) { }\n bool unionSet(int x, int y) {\n x = root(x); y = root(y);\n if (x != y) {\n if (data[y] < data[x]) swap(x, y);\n data[x] += data[y]; data[y] = x;\n }\n return x != y;\n }\n bool findSet(int x, int y) {\n return root(x) == root(y);\n }\n int root(int x) {\n return data[x] < 0 ? x : data[x] = root(data[x]);\n }\n int size(int x) {\n return -data[root(x)];\n }\n};\n\n\nnamespace geom{\n#define X real()\n#define Y imag()\n#define at(i) ((*this)[i])\n#define SELF (*this)\n\tenum {TRUE = 1, FALSE = 0, BORDER = -1};\n\ttypedef int BOOL;\n\ttypedef double R;\n\tconst R INF = 1e8;\n\tR EPS = 1e-6;\n\tconst R PI = 3.1415926535897932384626;\n\tinline int sig(const R &x) { return (abs(x) < EPS ? 0 : x > 0 ? 1 : -1); }\n\tinline BOOL less(const R &x, const R &y) {return sig(x-y) ? x < y : BORDER;}\n\ttypedef complex<R> P;\n\tinline R norm(const P &p){return p.X*p.X+p.Y*p.Y;}\n\tinline R inp(const P& a, const P& b){return (conj(a)*b).X;}\n\tinline R outp(const P& a, const P& b){return (conj(a)*b).Y;}\n\tinline P unit(const P& p){return p/abs(p);}\n\tinline P proj(const P &s, const P &t){return t*inp(s, t)/norm(t);}\n\tinline int ccw(const P &s, const P &t, const P &p, int adv=0){\n\t\tint res = sig(outp(t-s, p-s));\n\t\tif(res || !adv) return res;\n\t\tif(sig(inp(t-s, p-s)) < 0) return -2;\t// p-s-t\n\t\tif(sig(inp(s-t, p-t)) < 0) return 2;\t// s-t-p\n\t\treturn 0;\t\t\t\t\t\t\t\t// s-p-t\n\t}\n\t\n\t\n\tstruct L : public vector<P>{\t// line\n\t\tL(const P &p1, const P &p2){this->push_back(p1);this->push_back(p2);}\n\t\tL(){}\n\t\tP dir()const {return at(1) - at(0);}\n\t\tBOOL online(const P &p)const {return !sig(outp(p-at(0), dir()));}\n\t};\n\tstruct S : public L{\t// segment\n\t\tS(const P &p1, const P &p2):L(p1, p2){}\n\t\tS(){}\n\t\tBOOL online(const P &p)const {\n\t\t\tif(!sig(norm(p - at(0))) || !sig(norm(p - at(1)))) return BORDER;\n\t\t\treturn !sig(abs(at(0)-p) + abs(at(1) - p) - abs(at(0) - at(1)));\n\t\t}\n\t};\n\tstruct C : public P{\n\t\tC(){}\n\t\tC(const P& p, const R r):P(p), r(r){}\n\t\tR r;\n\t\tBOOL inside(const P& p)const { return less(norm(p-SELF), r*r);}\n\t};\n\tstruct F : public C{\n\t\tR s, t;\n\t\tF(const C &c, R ss, R tt):C(c), s(ss), t(tt){\n\t\t\tif(PI < s) s -= 2*PI;\n\t\t\tif(PI < t) t -= 2*PI;\n\t\t}\n\t\tBOOL inside(const P& p)const {\n\t\t\tP v = p - SELF;\n\t\t\tif(!sig(norm(v))) return BORDER;\n\t\t\tR a = arg(v);\n\t\t\tif(t < s){\n\t\t\t\tif((!less(s, a) && !less(a, t)) || !less(norm(v), r*r)) return FALSE;\n\t\t\t\treturn less(s, a) | less(a, t) | less(norm(v), r*r);\n\t\t\t}else{\n\t\t\t\tif(!less(s, a) || !less(a, t) || !less(norm(v), r*r)) return FALSE;\n\t\t\t\treturn less(s, a) | less(a, t) | less(norm(v), r*r);\n\t\t\t}\n\t\t}\n\t};\n\tP crosspoint(const L &l, const L &m);\n\tstruct G : public vector<P>{\n\t\tG(size_type size=0):vector(size){}\n\t\tS edge(int i)const {return S(at(i), at(i+1 == size() ? 0 : i+1));}\n\t\tBOOL contains(const P &p)const {\n\t\t\tR sum = .0;\n\t\t\tREP(i, size()){\n\t\t\t\tif(S(at(i), at((i+1)%size())).online(p)) return BORDER;\t// online\n\t\t\t\tsum += arg((at(i) - p) / (at((i+1)%size()) - p));\n\t\t\t}\n\t\t\treturn !!sig(sum);\n\t\t}\n\t\tR area()const {\n\t\t\tR sum = 0;\n\t\t\tREP(i, size()) sum += outp(at(i), at((i+1)%size()));\n\t\t\treturn abs(sum / 2.);\n\t\t}\n\t\t\n\t\tG convex_hull(bool online = false) {\n\t\t\tif(size() < 2) return *this;\n\t\t\tsort(ALL(*this));\n\t\t\tG r;\n\t\t\tr.resize((int)size()*2);\n\t\t\tint k=0;\n\t\t\tfor(int i=0;i<size();r[k++]=at(i++))\n\t\t\t\twhile(k>1 && ccw(r[k-2], r[k-1], at(i)) < 1-online) k--;\n\t\t\tint t = k;\n\t\t\tfor(int i=(int)size()-1;i>=0;r[k++]=at(i--))\n\t\t\t\twhile(k>t && ccw(r[k-2], r[k-1], at(i)) < 1-online) k--;\n\t\t\tr.resize(k-1);\n\t\t\treturn r;\n\t\t}\n\t\tG cut(const L &l)const {\n\t\t\tG g;\n\t\t\tREP(i, size()){\n\t\t\t\tconst S &s = edge(i);\n\t\t\t\tif(ccw(l[0], l[1], s[0], 0) >= 0) g.push_back(s[0]);\n\t\t\t\tif(ccw(l[0], l[1], s[0], 0) * ccw(l[0], l[1], s[1], 0) < 0)\n\t\t\t\t\tg.push_back(crosspoint(s, l));\n\t\t\t}\n\t\t\treturn g;\n\t\t}\n\t\tG Voronoi(const vector<P> &p, const int t)const {\n\t\t\tG g = *this;\n\t\t\tREP(i, p.size())if(i!=t){\n\t\t\t\tconst P m = (p[t]+p[i])*0.5;\n\t\t\t\tg = g.cut(L(m, m+(p[i]-p[t])*P(0, 1)));\n\t\t\t}\n\t\t\treturn g;\n\t\t}\n\t};\n\n\tinline P proj(const P &s, const L &t){return t[0] + proj(s-t[0], t[1]-t[0]);}\n\tinline P reflect(const P &s, const L &t){return 2.*proj(s, t) - s;}\n\tinline S reflect(const S &s, const L &t){return S(reflect(s[0], t), reflect(s[1], t));}\n\tBOOL intersect(const S &s, const S &t){\n\t\tconst int p = ccw(t[0], t[1], s[0], 1) * ccw(t[0], t[1], s[1], 1);\n\t\tconst int q = ccw(s[0], s[1], t[0], 1) * ccw(s[0], s[1], t[1], 1);\n\t\treturn (p>0||q>0) ? FALSE : (!p||!q) ? BORDER : TRUE;\n\t}\n\tBOOL intersect(const S &s, const L &l){\n\t\tif(l.online(s[0]) || l.online(s[1])) return BORDER;\n\t\treturn (sig(outp(l.dir(), s[0]-l[0])) * sig(outp(l.dir(), s[1]-l[0])) <= 0);\n\t}\n\tR dist2(const L &l, const P &p){return norm(outp(l.dir(), p - l[0])) / norm(l.dir());}\n\tR dist2(const S &s, const P &p){\n\t\tif(inp(p-s[0], s.dir()) < EPS) return norm(p - s[0]);\n\t\tif(inp(p-s[1], -s.dir()) < EPS) return norm(p - s[1]);\n\t\treturn dist2((const L &)s, p);\n\t}\n\tR dist2(const S &s, const L &l){\n\t\treturn intersect(s, l) ? .0 : min(dist2(l, s[0]), dist2(l, s[1]));\n\t}\n\tR dist2(const S &s, const S &t){\n\t\treturn intersect(s, t) ? .0 : min(min(dist2(s, t[0]), dist2(t, s[0])), \n\t\t\t\t\t\t\t\t\t \t min(dist2(s, t[1]), dist2(t, s[1])));\n\t}\n\ttemplate <class T> R dist2(const G &g, const T& t){ // todo: 内部に完全に含まれる場合\n\t\tR res = INF;\n\t\tREP(i, g.size()) res = min(res, dist2(g.edge(i), t));\n\t\treturn res;\n\t}\n\ttemplate<class S, class T> R dist(const S& s, const T& t){return sqrt(dist2(s, t));}\n\tinline BOOL intersect(const C &a, const C &b){\n\t\treturn less((a.r-b.r)*(a.r-b.r), norm(a-b)) + less(norm(a-b), (a.r+b.r)*(a.r+b.r)) - 1;\n\t}\n\tinline BOOL intersect(const C &c, const L &l){\n\t\treturn less(dist2(l, c), c.r*c.r);\n\t}\n\tinline BOOL intersect(const C &c, const S &s){\n\t\tint d = less(dist2(s, c), c.r*c.r);\n\t\tif(d != TRUE) return d;\n\t\tint p = c.inside(s[0]), q = c.inside(s[1]);\n\t\treturn (p<0 || q<0) ? BORDER : p&q;\n\t}\n\t\n\t/*\n\t * CCWでs[0]->s[1]にかけてが共通部分\n\t */\n\tinline S crosspoint(const C &c1, const C &c2){\n\t\tif(!intersect(c1, c2)) return S();\n\t\tR d = abs(c1 - c2);\n\t\tR x = (c1.r*c1.r - c2.r*c2.r + d*d) / (2*d);\n\t\tR h = sqrt(max(0., c1.r*c1.r - x*x));\n\t\tP u = unit(c2-c1);\n\t\treturn S(c1 + u*x + u*P(0,-1)*h, c1 + u*x + u*P(0,1)*h);\n\t}\n\tinline P crosspoint(const L &l, const L &m){\n\t\tR A = outp(l.dir(), m.dir()), B = outp(l.dir(), l[1] - m[0]);\n\t\tif(!sig(abs(A)) && !sig(abs(B))) return m[0]; // same line\n\t\tif(abs(A) < EPS) assert(false); // !!!PRECONDITION NOT SATISFIED!!!\n\t\treturn m[0] + B / A * (m[1] - m[0]);\n\t}\n\tinline S crosspoint(const C &c, const L &l){\n\t\tR d2 = dist2(l, c);\n\t\tif(c.r*c.r+EPS < d2) return S();\n\t\tP m = proj(c, l);\n\t\tP u = unit(l[1]-l[0]);\n\t\tR d = sqrt(max(.0, c.r*c.r - d2));\n\t\treturn S(m+u*d, m-u*d);\n\t}\n\tinline vector<P> crosspoint(const C &c, const S &s){\n\t\tvector<P> res = crosspoint(c, (const L&)s);\n\t\tRREP(i, res.size()){\n\t\t\tif(inp(res[i]-s[0], s.dir())*inp(res[i]-s[1], -s.dir())<EPS)\n\t\t\t\tres.erase(res.begin() + i);\n\t\t}\n\t\treturn res;\n\t}\n\tinline R commonarea(const C &a, const C &b){\n\t\tif(less(norm(a-b), (a.r-b.r)*(a.r-b.r)) == TRUE) return min(a.r*a.r, b.r*b.r)*PI;\n\t\tif(less((a.r+b.r)*(a.r+b.r), norm(a-b)) == TRUE) return .0;\n\t\tdouble d = abs(a-b);\n\t\tdouble rc = (d*d + a.r*a.r - b.r*b.r) / (2*d);\n\t\tdouble theta = acos(rc / a.r);\n\t\tdouble phi = acos((d - rc) / b.r);\n\t\treturn a.r*a.r*theta + b.r*b.r*phi - d*a.r*sin(theta);\n\t}\n\tvector<L> CommonTangent(C c1, C c2){\n\t\tif(c1.r > c2.r) swap(c1, c2);\n\t\tdouble d = abs(c1-c2);\n\t\tvector<L> res;\n\t\tif(d < EPS) return res;\n\t\tif(d + EPS > c1.r + c2.r){\n\t\t\t// 内接線\n\t\t\tP crs = (c1*c2.r + c2*c1.r) / (c1.r + c2.r);\n\t\t\tdouble rad = asin(c1.r/abs(crs-c1));\n\t\t\tres.push_back(L(crs, crs + (c1-crs)*polar(1., rad)));\n\t\t\tres.push_back(L(crs, crs + (c1-crs)*polar(1., -rad)));\n\t\t}\n\t\tif(c1.r + d + EPS > c2.r){\n\t\t\t// 外接線\n\t\t\tdouble rad = 0.5*PI+asin((c2.r-c1.r) / d);\n\t\t\tP v = unit(c2-c1)*polar(1., rad);\n\t\t\tif(c1.r + d - EPS < c2.r){\n\t\t\t\tres.push_back(L(c1+v*c1.r, c1+v*c1.r+(c1-c2)*P(0, 1)));\n\t\t\t}else{\n\t\t\t\tres.push_back(L(c1+v*c1.r, c2+v*c2.r));\n\t\t\t\tv = 2.*proj(v, c2-c1) - v;\n\t\t\t\tres.push_back(L(c1+v*c1.r, c2+v*c2.r));\n\t\t\t}\n\t\t}\n\t\treturn res;\n\t}\n\t\n\tstruct min_ball {\n\t\tP center;\n\t\tR radius2;\n\t\tmin_ball(const vector<P>& p) {\n\t\t\tFOR(it, p) ps.push_back(*it);\n\t\t}\n\t\tmin_ball& compile() {\n\t\t\tm = 0;\n\t\t\tcenter = P(0, 0);\n\t\t\tradius2 = -1;\n\t\t\tmake_ball(ps.end());\n\t\t\treturn *this;\n\t\t}\n\tprivate:\n\t\tlist<P> ps;\n\t\tlist<P>::iterator supp_end;\n\t\tint m;\n\t\tP v[3], c[3];\n\t\tR z[3], r[3];\n\t\tvoid pop() { --m; }\n\t\tvoid push(const P& p) {\n\t\t\tif (m == 0) {\n\t\t\t\tc[0] = p; r[0] = 0;\n\t\t\t} else {\n\t\t\t\tR e = norm(p-c[m-1]) - r[m-1];\n\t\t\t\tP delta = p - c[0];\n\t\t\t\tv[m] = p - c[0];\n\t\t\t\tfor (int i = 1; i < m; ++i)\n\t\t\t\t\tv[m] -= v[i] * inp(v[i], delta) / z[i];\n\t\t\t\tz[m] = inp(v[m], v[m]);\n\t\t\t\tc[m] = c[m-1] + e*v[m]/z[m]*.5;\n\t\t\t\tr[m] = r[m-1] + e*e/z[m]*.25;\n\t\t\t}\n\t\t\tcenter\t= c[m];\n\t\t\tradius2 = r[m]; ++m;\n\t\t}\n\t\tvoid make_ball(list<P>::iterator i) {\n\t\t\tsupp_end = ps.begin();\n\t\t\tif (m == 3) return;\n\t\t\tfor (list<P>::iterator k = ps.begin(); k != i; ) {\n\t\t\t\tlist<P>::iterator j = k++;\n\t\t\t\tif (norm(*j-center) > radius2) {\n\t\t\t\t\tpush(*j); make_ball(j); pop();\n\t\t\t\t\tmove_to_front(j);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tvoid move_to_front(list<P>::iterator j) {\n\t\t\tif (supp_end == j) ++supp_end;\n\t\t\tps.splice (ps.begin(), ps, j);\n\t\t}\n\t};\n\t\n\tstruct Arrangement{\n\t\tstruct AEdge{\n\t\t\tint u, v, t;\n\t\t\tR cost;\n\t\t\tAEdge(int u=0, int v=0, int t=0, R cost=0)\n\t\t\t\t:u(u), v(v), t(t), cost(cost){}\n\t\t};\n\t\ttypedef vector<vector<AEdge>> AGraph;\n\t\tvector<P> p;\n\t\tAGraph g;\n\t\tArrangement(){}\n\t\tArrangement(vector<S> seg){\n\t\t\tint m = seg.size();\n\t\t\tREP(i, m){\n\t\t\t\tp.push_back(seg[i][0]);\n\t\t\t\tp.push_back(seg[i][1]);\n\t\t\t\tREP(j, i) if(sig(outp(seg[i].dir(), seg[j].dir())) && intersect(seg[i], seg[j]) == TRUE)\n\t\t\t\t\tp.push_back(crosspoint(seg[i], seg[j]));\n\t\t\t}\n\t\t\tsort(ALL(p)); UNIQUE(p);\n\t\t\tint n=p.size();\n\t\t\tg.resize(n);\n\t\t\tREP(i, m){\n\t\t\t\tS &s = seg[i];\n\t\t\t\tvector<pair<R, int>> ps;\n\t\t\t\tREP(j, n) if(s.online(p[j])) ps.emplace_back(norm(p[j] - s[0]), j);\n\t\t\t\tsort(ALL(ps));\n\t\t\t\tREP(j, (int)ps.size()-1){\n\t\t\t\t\tconst int u=ps[j].second;\n\t\t\t\t\tconst int v=ps[j+1].second;\n\t\t\t\t\tg[u].emplace_back(u, v, 0, abs(p[u] - p[v]));\n\t\t\t\t\tg[v].emplace_back(v, u, 0, abs(p[u] - p[v]));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\t\n\t\tint getIdx(P q){\n\t\t\tauto it = lower_bound(ALL(p), q);\n\t\t\tif(it == p.end() || *it != q) return -1;\n\t\t\treturn it - p.begin();\n\t\t}\n\t};\n\n\tstruct DualGraph{\n\t\tstruct DEdge{\n\t\t\tint u, v, f, l;\n\t\t\tR a;\n\t\t\tDEdge(int u, int v, R a):u(u),v(v),f(0),l(0){\n\t\t\t\twhile(PI < a) a -= 2*PI;\n\t\t\t\twhile(a < -PI) a += 2*PI;\n\t\t\t\tthis->a = a;\n\t\t\t}\n\t\t\tbool operator==(const DEdge &opp) const{\n\t\t\t\treturn v==opp.v;\n\t\t\t}\n\t\t\tbool operator<(const DEdge &opp) const{\n\t\t\t\treturn a>opp.a;\n\t\t\t}\n\t\t\tbool operator<(const R &opp) const{\n\t\t\t\treturn a>opp;\n\t\t\t}\n\t\t\tfriend ostream& operator<<(ostream &os, const DEdge &t) { return os<<\"(\"<<t.u<<\",\"<<t.v<<\",\"<<t.a*180/PI<<\")\";}\n\t\t};\n\t\t\n\t\tint n;\n\t\tvector<P> p;\n\t\tvector<vector<DEdge>> g;\n\t\tUnionFind uf;\n\t\tDualGraph(const vector<P> &p):p(p),g(p.size()),n(p.size()),uf(p.size()){}\n\t\tvoid add_edge(const int s, const int t){\n\t\t\tR a = arg(p[t]-p[s]);\n\t\t\tg[s].emplace_back(s, t, a);\n\t\t\tg[t].emplace_back(t, s, a > 0 ? a-PI : a+PI);\n\t\t\tuf.unionSet(s, t);\n\t\t}\n\t\tvector<G> poly;\n\t\t\n\t\tvoid add_polygon(int s, int t, R a){\n\t\t\tauto e = lower_bound(ALL(g[s]), a-EPS);\n\t\t\tif(e == g[s].end()) e = g[s].begin();\n\t\t\tif(e->f) return;\n\t\t\te->f = 1;\n\t\t\te->l = t;\n\t\t\tpoly[t].push_back(p[s]);\n\t\t\tadd_polygon(e->v, t, e->a > 0 ? e->a-PI : e->a+PI);\n\t\t}\n\t\t\n\t\tvoid toSpanningTree(){\n\t\t\tvector<pair<R, pii>> e;\n\t\t\tREP(i, n)REP(j, i)if(!uf.findSet(i, j))\n\t\t\t\te.emplace_back(norm(p[i]-p[j]), pii(i, j));\n\t\t\tsort(ALL(e));\n\t\t\tREP(ii, e.size()){\n\t\t\t\tconst int i = e[ii].second.first;\n\t\t\t\tconst int j = e[ii].second.second;\n\t\t\t\tif(uf.findSet(i, j)) continue;\n\t\t\t\tconst S s(p[i], p[j]);\n\t\t\t\tif([&](){\n\t\t\t\t\tREP(k, n)if(i!=k&&j!=k){\n\t\t\t\t\t\tFOR(it, g[k])\n\t\t\t\t\t\tif(intersect(s, S(p[k], p[it->v])) == TRUE) return 0;\n\t\t\t\t\t}\n\t\t\t\t\treturn 1;\n\t\t\t\t}()) add_edge(i, j);\n\t\t\t}\n\t\t}\n\t\t\n\t\tvector<vi> dual(){\n\t\t\ttoSpanningTree();\n\t\t\t\n\t\t\tREP(i, n){\n\t\t\t\tsort(ALL(g[i]));\n\t\t\t\tUNIQUE(g[i]);\n\t\t\t}\n\t\t\tint s = min_element(ALL(p)) - p.begin();\n\t\t\tpoly.emplace_back();\n\t\t\tadd_polygon(s, poly.size()-1, -PI*.5);\n\t\t\tREP(i, n)REP(j, g[i].size())if(!g[i][j].f){\n\t\t\t\tpoly.emplace_back();\n\t\t\t\tadd_polygon(i, poly.size()-1, g[i][j].a+2.*EPS);\n\t\t\t}\n\t\t\tint m = poly.size();\n\t\t\tvector<vi> dg(m);\n\t\t\tvector<unordered_map<int, int>> rev(n);\n\t\t\tREP(i, n)REP(j, g[i].size()){\n\t\t\t\tint u = i, v = g[i][j].v, l = g[i][j].l;\n\t\t\t\tif(u>v) swap(u, v);\n\t\t\t\tif(rev[u].find(v) != rev[u].end()){\n\t\t\t\t\tif(l != rev[u][v]){\n\t\t\t\t\t\tdg[l].push_back(rev[u][v]);\n\t\t\t\t\t\tdg[rev[u][v]].push_back(l);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\telse rev[u][v] = l;\n\t\t\t}\n\t\t\tREP(i, m){\n\t\t\t\tsort(ALL(dg[i]));\n\t\t\t\tUNIQUE(dg[i]);\n\t\t\t}\n\t\t\treturn dg;\n\t\t}\n\t};\n\n#undef SELF\n#undef at\n}\n\nusing namespace geom;\n\nint f = 0;\nnamespace std{\n\tbool operator<(const P &a, const P &b){return sig(a.X-b.X) ? a.X < b.X : a.Y+EPS < b.Y;}\n\tbool operator==(const P &a, const P &b){return abs(a-b) < EPS;}\n\tistream& operator>>(istream &is, P &p){R x,y;is>>x>>y;p=P(x, y);return is;}\n\tistream& operator>>(istream &is, L &l){l.resize(2);return is >> l[0] >> l[1];}\n\tistream& operator>>(istream &is, C &c){return is >> (P &)c >> c.r;}\n\tconst double B = 500;\n\tconst double Z = 20;\n\tostream& operator<<(ostream &os, const C &c){return os << \"circle(\"<<B+Z*(c.X)<<\", \"<<1000-B-Z*(c.Y)<<\", \"<<Z*(c.r)<<\")\";}\n\tostream& operator<<(ostream &os, const P &p){return os << C(p, 2./Z);}\n\tostream& operator<<(ostream &os, const S &s){return os << \"line(\"<<B+Z*(s[0].X)<<\", \"<<1000-B-Z*(s[0].Y)<<\", \"<<B+Z*(s[1].X)<<\", \"<<1000-B-Z*(s[1].Y)<<\")\";}\n\tostream& operator<<(ostream &os, const G &g){REP(i, g.size()) cout << g.edge(i) << endl;return os;}\n\n}\n\nbool bipartite_matching_dfs(\n\tint v, const vector< vector<int> > &conn,\n\tvector<bool> &used, vector<int> &match)\n{\n\tused[v] = true;\n\tfor(int i = 0; i < conn[v].size(); ++i){\n\t\tint u = conn[v][i], w = match[u];\n\t\tif(w < 0 || (!used[w] && bipartite_matching_dfs(w, conn, used, match))){\n\t\t\tmatch[v] = u;\n\t\t\tmatch[u] = v;\n\t\t\treturn true;\n\t\t}\n\t}\n\treturn false;\n}\n//O(E√V)\nint bipartite_matching(const vector< vector<int> > &conn){\n\tint res = 0;\n\tvector<int> match(conn.size(), -1);\n\tvector<bool> used(conn.size(), false);\n\tfor(int v = 0; v < conn.size(); ++v){\n\t\tif(match[v] < 0){\n\t\t\tfill(used.begin(), used.end(), false);\n\t\t\tif(bipartite_matching_dfs(v, conn, used, match)){ ++res; }\n\t\t}\n\t}\n\treturn res;\n}\n\nint n, k;\n\nint main(int argc, char *argv[]){\n\tios::sync_with_stdio(false);\n\tcin >> n >> k;\n\tvector<C> c(n);\n\tREP(i, n){\n\t\tcin >> c[i];\n//\t\tc[i] *= polar(1., 0);\n\t}\n\tvector<R> x;\n\tREP(i, n){\n\t\tx.push_back(c[i].X - c[i].r);\n\t\tx.push_back(c[i].X);\n\t\tx.push_back(c[i].X + c[i].r);\n\t\tREP(j, i){\n\t\t\tS s = crosspoint(c[i], c[j]);\n\t\t\tFOR(it, s){\n\t\t\t\tx.push_back(it->X);\n\t\t\t}\n\t\t\tif(s.size() == 2 && abs(s[0]-s[1])>EPS){\n\t\t\t\tx.push_back((c[i] + unit((s[0]+s[1])*0.5 - c[i]) * c[i].r).X);\n\t\t\t\tx.push_back((c[j] + unit((s[0]+s[1])*0.5 - c[j]) * c[j].r).X);\n\t\t\t}\n\t\t}\n\t}\n\tsort(ALL(x));UNIQUE(x);\n//cerr << x << endl;\n\tvector<P> p;\n\tREP(i, n){\n\t\tREP(j, x.size()){\n\t\t\tL l(P(x[j], 0.), P(x[j], 10.));\n\t\t\tS s = crosspoint(c[i], l);\n\t\t\tFOR(it, s){\n\t\t\t\tp.push_back(*it);\n\t\t\t}\n\t\t}\n\t}\n\tEPS = 1e-3;\n\tsort(ALL(p));\n//cerr << p << endl << endl;\n\tUNIQUE(p);\n//FOR(it, p)cout << *it << endl;\n\tDualGraph dg(p);\n\tREP(i, n){\n\t\tvector<pair<R, int>> q;\n\t\tREP(j, p.size())\n\t\t\tif(!sig(abs(p[j] - c[i]) - c[i].r))\n\t\t\t\tq.emplace_back(arg(p[j] - c[i]), j);\n\t\tsort(ALL(q));\n\t\tREP(j, (int)q.size()) dg.add_edge(q[j].second, q[(j+1)%q.size()].second);\n\t}\n\tEPS = 1e-8;\n\tvector<vi> g = dg.dual();\n//FOR(it, c) cout << *it << endl;\n//FOR(it, dg.poly) cout << *it << endl;\n//cout << dg.poly[atoi(argv[1])] << endl;\n\tif(k > 1) cout << dg.poly.size() - 1 << endl;\n\telse{\n\t\tREPS(i, (int)g.size()-1){\n\t\t\tg[i].erase(remove(ALL(g[i]), 0), g[i].end());\n\t\t}\n\t\tg[0].clear();\n//\t\tcerr << g << endl;\n\t\tcout << g.size() - bipartite_matching(g) - 1 << endl;\n\t}\n\treturn 0;\n}", "accuracy": 0.2553191489361702, "time_ms": 10, "memory_kb": 1804, "score_of_the_acc": 0, "final_rank": 11 } ]
aoj_2239_cpp
Problem H: Nearest Station うさぎがある電車のチケットを n 枚持っている. チケットにはそれぞれ0 から n − 1 までの番号がついていて, k 番のチケットを使うと, p ⋅ a k + q ⋅ b k 駅進むことができる. うさぎは今いる駅から m 駅進んだ駅にあるニンジン食べ放題の店に行きたいが, なるべく歩く距離を短くしたい. 駅は等間隔に並んでいる. チケットを電車の上り線で進むことのみに用いるとき, うさぎは最小何駅分の徒歩で店に着けるか. Input 1 ≤ n , m , a , b , p , q ≤ 1 000 000 000 000 (整数) Output うさぎは最小何駅分の徒歩で店に着けるか, その数を一行に出力せよ. Sample Input 1 6 200 2 3 4 5 Sample Output 1 1 Sample Input 2 6 1 2 3 4 5 Sample Output 2 1
[ { "submission_id": "aoj_2239_10866027", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing VS = vector<string>; using LL = long long;\nusing VI = vector<int>; using VVI = vector<VI>;\nusing PII = pair<int, int>; using PLL = pair<LL, LL>;\nusing VL = vector<LL>; using VVL = vector<VL>;\n\n#define ALL(a) begin((a)),end((a))\n#define RALL(a) (a).rbegin(), (a).rend()\n#define PB push_back\n#define EB emplace_back\n#define MP make_pair\n#define SZ(a) int((a).size())\n#define SORT(c) sort(ALL((c)))\n#define RSORT(c) sort(RALL((c)))\n#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))\n#define FOR(i, s, e) for (int(i) = (s); (i) < (e); (i)++)\n#define FORR(i, s, e) for (int(i) = (s); (i) > (e); (i)--)\n#define debug(x) cerr << #x << \": \" << x << endl\nconst int INF = 1e9; const LL LINF = 1e16;\nconst LL MOD = 1000000007; const double PI = acos(-1.0);\nint DX[8] = { 0, 0, 1, -1, 1, 1, -1, -1 }; int DY[8] = { 1, -1, 0, 0, 1, -1, 1, -1 };\n\n/* ----- 2017/11/29 Problem: AOJ 2239 / Link: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2239 ----- */\n/* ------問題------\n\n\n\n-----問題ここまで----- */\n/* -----解説等-----\n\n場合分けさん…\n\n----解説ここまで---- */\n// 繰り返し二乗法\nlong long powmod(long long x, long long n) {\n\tlong long res = 1;\n\twhile (n > 0) {\n\t\tif (n & 1) {\n\t\t\tres = (res*x);\n\t\t}\n\t\tx = (x*x);\n\t\tn >>= 1;\n\t}\n\treturn res;\n}\nLL N, M, A, B, P, Q;\n\nLL ans = 0LL;\n\nint main() {\n\tcin.tie(0);\n\tios_base::sync_with_stdio(false);\n\n\tcin >> N >> M >> A >> B >> P >> Q;\n\t// N枚ある M駅に行きたい ticket(k):p⋅a^k + q⋅b^k (k=0...N-1)\n\n\tif (A == 1 && B == 1) {\n\t\tLL E = P + Q;\n\t\tLL div = M / E;\n\t\tif (div > N) { ans = abs(M - N*E); }\n\t\telse {\n\t\t\tans = abs(M - div*E);\n\t\t\tif (M%E != 0 && div + 1 <= N) {\n\t\t\t\tans = min(ans, abs(M - (div + 1)*E));\n\t\t\t}\n\t\t}\n\t}// ok\n\telse { //40枚ぐらいしか使わないので全探索可能 戻れる可能性も忘れない\n\t\tans = numeric_limits<LL>::max();\n\t\tVL dist;\n\t\tFOR(i, 0, min(N, 41LL)) {\n\t\t\tLL d = P*powmod(A, i) + Q*powmod(B, i);\n\t\t\tif (d > 2 * M)break; // 歩くのは沼\n\t\t\tdist.push_back(d);\n\t\t}\n\n\t\tint n1 = SZ(dist) / 2;\n\t\tint n2 = SZ(dist) - n1;\n\n\t\tVL distset;\n\t\tFOR(i, 0, 1 << n1) {\n\t\t\tLL ret = 0;\n\t\t\tFOR(j, 0, n1) {\n\t\t\t\tif (i&(1 << j)) {\n\t\t\t\t\tret += dist[j];\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (!(ret > 2 * M)) {\n\t\t\t\tdistset.push_back(ret);\n\t\t\t\tans = min(ans, abs(M - ret));\n\t\t\t}\n\t\t}\n\t\tSORT(distset);\n\n\t\tFOR(i, 0, 1 << n2) {\n\t\t\tLL ret = 0;\n\t\t\tFOR(j, 0, n2) {\n\t\t\t\tif (i&(1 << j)) {\n\t\t\t\t\tret += dist[n1 + j];\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (ret > 2 * M)continue;\n\t\t\tans = min(ans, abs(M - ret));\n\t\t\tint id = lower_bound(ALL(distset), M - ret) - distset.begin();\n\t\t\tans = min(ans, abs(M - (ret + distset[id])));\n\t\t\tif (id > 0) {\n\t\t\t\tid--;\n\t\t\t\tans = min(ans, abs(M - (ret + distset[id])));\n\t\t\t}\n\t\t}\n\n\t}\n\n\tcout << ans << \"\\n\";\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 160, "memory_kb": 11976, "score_of_the_acc": -0.1519, "final_rank": 2 }, { "submission_id": "aoj_2239_10026873", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define all(v) (v).begin(),(v).end()\n#define pb(a) push_back(a)\n#define rep(i, n) for(int i=0;i<n;i++)\n#define foa(e, v) for(auto&& e : v)\nusing ll = long long;\nconst ll MOD7 = 1000000007, MOD998 = 998244353, INF = (1LL << 60);\n#define dout(a) cout<<fixed<<setprecision(10)<<a<<endl;\n\nvoid chmin(ll &a, ll b) { a = min(a, b); }\n\nvector<ll> solve(ll n, ll M, ll A, ll B, ll P, ll Q) {\n using Int = __int128;\n Int lim = M * 2;\n Int a = A;\n Int b = B;\n Int p = P;\n Int q = Q;\n vector<Int> v;\n Int r1 = p, r2 = q;\n for(ll i = 0; i < n; i ++) {\n if(r1 + r2 > lim) break;\n v.pb(r1 + r2);\n r1 *= a;\n r2 *= b;\n }\n vector<ll> vec;\n foa(e, v) vec.pb((ll)e);\n sort(all(vec));\n return vec;\n}\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n ll n, m, a, b, p, q;\n cin >> n >> m >> a >> b >> p >> q;\n if(max(a, b) == 1LL) {\n ll num = p + q;\n ll val = m / num;\n val = min(val, n);\n ll ans = abs(val * num - m);\n val ++;\n val = min(val, n);\n chmin(ans, abs(val * num - m));\n cout << ans << endl;\n return 0;\n }\n ll ans = INF;\n vector<ll> v = solve(n, m, a, b, p, q);\n\n n = v.size();\n ll mid = n / 2;\n vector<ll> vec;\n rep(bit, 1 << mid) {\n ll sum = 0;\n rep(i, mid) if(bit >> i & 1) {\n sum += v[i];\n }\n vec.pb(sum);\n }\n sort(all(vec));\n rep(bit, 1 << (n - mid)) {\n ll sum = 0;\n rep(i, n - mid) if(bit >> i & 1) {\n sum += v[i + mid];\n }\n auto itr = lower_bound(all(vec), m - sum);\n if(itr != vec.end()) chmin(ans, abs(*itr + sum - m));\n if(itr != vec.begin()) {\n itr --;\n chmin(ans, abs(*itr + sum - m));\n }\n }\n cout << ans << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 140, "memory_kb": 12164, "score_of_the_acc": -0.1312, "final_rank": 1 }, { "submission_id": "aoj_2239_9008508", "code_snippet": "#line 2 \"cp-library/src/cp-template.hpp\"\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nusing i32 = int;\nusing u32 = unsigned int;\nusing i64 = long long;\nusing u64 = unsigned long long;\nusing i128 = __int128_t;\ntemplate < class T > bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }\ntemplate < class T > bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }\ntemplate < class T, class U > T ceil (T x, U y) { return (x > 0 ? (x + y - 1) / y : x / y); }\ntemplate < class T, class U > T floor(T x, U y) { return (x > 0 ? x / y : (x - y + 1) / y); }\nint popcnt(i32 x) { return __builtin_popcount(x); }\nint popcnt(u32 x) { return __builtin_popcount(x); }\nint popcnt(i64 x) { return __builtin_popcountll(x); }\nint popcnt(u64 x) { return __builtin_popcountll(x); }\n\n#line 2 \"cp-library/src/utility/rep_itr.hpp\"\ntemplate < class T > struct itr_rep {\n T i, d;\n constexpr itr_rep(const T i) noexcept : i(i), d(1) {}\n constexpr itr_rep(const T i, const T d) noexcept : i(i), d(d) {}\n void operator++() noexcept { i += d; }\n constexpr int operator*() const noexcept { return i; }\n constexpr bool operator!=(const itr_rep x) const noexcept { return d > 0 ? i < x.i : i > x.i; }\n};\n\ntemplate < class T > struct rep {\n const itr_rep< T > s, t;\n constexpr rep(const T t) noexcept : s(0), t(t) {}\n constexpr rep(const T s, const T t) noexcept : s(s), t(t) {}\n constexpr rep(const T s, const T t, const T d) noexcept : s(s, d), t(t, d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n\ntemplate < class T > struct revrep {\n const itr_rep < T > s, t;\n constexpr revrep(const T t) noexcept : s(t - 1, -1), t(-1, -1) {}\n constexpr revrep(const T s, const T t) noexcept : s(t - 1, -1), t(s - 1, -1) {}\n constexpr revrep(const T s, const T t, const T d) noexcept : s(t - 1, -d), t(s - 1, -d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n#line 3 \"cp-library/src/utility/io.hpp\"\n\n/* 128bit integer */\nistream& operator>>(istream& is, i128& x) {\n std::string s; is >> s;\n int pm = (s[0] == '-');\n x = 0;\n for(int i : rep(pm, int(s.size()))) x = x * 10 + (s[i] - '0');\n if(pm) x *= -1;\n return is;\n}\nostream& operator<<(ostream& os, const i128& x) {\n if(x == 0) return os << '0';\n i128 y = x;\n if(y < 0) { os << '-'; y *= -1; }\n std::vector<int> ny;\n while(y > 0) { ny.push_back(y % 10); y /= 10; }\n for(int i : revrep(ny.size())) os << ny[i];\n return os;\n}\n\ntemplate < class S, class T > istream& operator>>(istream& is, std::pair< S, T >& x) { is >> x.first >> x.second; return is; }\ntemplate < class S, class T > ostream& operator<<(ostream& os, const std::pair< S, T >& x) { os << x.first << \" \" << x.second; return os; }\n\nnamespace scanner {\n struct sca {\n template < class T > operator T() {\n T s; std::cin >> s; return s;\n }\n };\n struct vec {\n int n;\n vec(int n) : n(n) {}\n template < class T > operator std::vector< T >() {\n std::vector< T > v(n);\n for(T& x : v) std::cin >> x;\n return v;\n }\n };\n struct mat {\n int h, w;\n mat(int h, int w) : h(h), w(w) {}\n template < class T > operator std::vector< std::vector< T > >() {\n std::vector m(h, std::vector< T >(w));\n for(std::vector< T >& v : m) for(T& x : v) std::cin >> x;\n return m;\n }\n };\n struct speedup {\n speedup() {\n std::cin.tie(0);\n std::ios::sync_with_stdio(0);\n }\n } speedup_instance;\n}\nscanner::sca in() { return scanner::sca(); }\nscanner::vec in(int n) { return scanner::vec(n); }\nscanner::mat in(int h, int w) { return scanner::mat(h, w); }\n\nnamespace printer {\n void precision(int d) { std::cout << std::fixed << std::setprecision(d); }\n void flush() { std::cout.flush(); }\n}\n\ntemplate < class T >\nostream& operator<<(ostream& os, const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) { os << a[i]; if(i != n - 1) os << ' '; }\n return os;\n}\n\nint print() { std::cout << '\\n'; return 0; }\ntemplate < class head, class... tail > int print(head&& h, tail&&... t) {\n std::cout << h; if(sizeof...(tail)) std::cout << ' ';\n return print(std::forward<tail>(t)...);\n}\ntemplate < class T > int print_n(const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) std::cout << a[i] << \"\\n\";\n return 0;\n}\n\n\n#line 2 \"cp-library/src/utility/key_val.hpp\"\n\ntemplate < class K, class V >\nstruct key_val {\n K key; V val;\n key_val() {}\n key_val(K key, V val) : key(key), val(val) {}\n template < std::size_t Index >\n std::tuple_element_t< Index, key_val >& get() {\n if constexpr (Index == 0) return key;\n if constexpr (Index == 1) return val;\n }\n};\n\nnamespace std {\n\ntemplate < class K, class V > struct tuple_size < key_val< K, V > > : integral_constant< size_t, 2 > {};\ntemplate < class K, class V > struct tuple_element < 0, key_val< K, V > > { using type = K; };\ntemplate < class K, class V > struct tuple_element < 1, key_val< K, V > > { using type = V; };\n\n}\n#line 2 \"cp-library/src/utility/vec_op.hpp\"\ntemplate < class T > key_val< int, T > max_of(const vector< T >& a) {\n int i = std::max_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class T > key_val< int, T > min_of(const vector< T >& a) {\n int i = std::min_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class S, class T > S sum_of(const vector< T >& a) {\n S sum = 0;\n for(const T x : a) sum += x;\n return sum;\n}\ntemplate < class S, class T > vector< S > freq_of(const vector< T >& a, T L, T R) {\n vector< S > res(R - L, S(0));\n for(const T x : a) res[x - L] += 1;\n return res;\n}\ntemplate < class S, class T > struct prefix_sum {\n vector< S > s;\n prefix_sum(const vector< T >& a) : s(a) {\n s.insert(s.begin(), S(0));\n for(int i : rep(a.size())) s[i + 1] += s[i];\n }\n // [L, R)\n S sum(int L, int R) { return s[R] - s[L]; }\n};\n#line 3 \"cp-library/src/utility/heap.hpp\"\n\ntemplate < class T > using heap_min = std::priority_queue< T, std::vector< T >, std::greater< T > >;\ntemplate < class T > using heap_max = std::priority_queue< T, std::vector< T >, std::less< T > >;\n\n#line 27 \"cp-library/src/cp-template.hpp\"\n\n#line 1 \"cp-library/src/algorithm/bin_search.hpp\"\ntemplate < class T, class F >\nT bin_search(T ok, T ng, F f) {\n while(abs(ng - ok) > 1) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n\ntemplate < class T, class F >\nT bin_search_real(T ok, T ng, F f, int step = 80) {\n while(step--) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n#line 2 \"cp-library/src/algorithm/argsort.hpp\"\n\ntemplate < class T > std::vector< int > argsort(const std::vector< T > &a) {\n std::vector< int > ids((int)a.size());\n std::iota(ids.begin(), ids.end(), 0);\n std::sort(ids.begin(), ids.end(), [&](int i, int j) {\n return a[i] < a[j] || (a[i] == a[j] && i < j);\n });\n return ids;\n}\n#line 1 \"macro.hpp\"\nnamespace macro {\n\nusing size_type = int;\ntemplate < class container > void sort(container& a) { std::sort(std:: begin(a), std:: end(a)); }\ntemplate < class container > void rsort(container& a) { std::sort(std::rbegin(a), std::rend(a)); }\ntemplate < class container > void reverse(container& a) { std::reverse(std::begin(a), std::end(a)); }\ntemplate < class container > void unique(container& a) {\n std::sort(std::begin(a), std::end(a));\n a.erase(std::unique(std::begin(a), std::end(a)), std::end(a));\n}\ntemplate < class container > container sorted(const container& a) { container b = a; sort(b); return std::move(b); }\ntemplate < class container > container rsorted(const container& a) { container b = a; rsort(b); return std::move(b); }\ntemplate < class container, class compare > void sort(container& a, const compare& cmp) { std::sort(std::begin(a), std::end(a), cmp); }\ntemplate < class container, class compare > container sorted(const container& a, const compare& cmp) { container b = a; sort(b, cmp); return std::move(b); }\ntemplate < class container, class value > size_type lower_bound(const container& a, const value& x) { return std::lower_bound(std::begin(a), std::end(a), x) - std::begin(a); }\ntemplate < class container, class value > size_type upper_bound(const container& a, const value& x) { return std::upper_bound(std::begin(a), std::end(a), x) - std::begin(a); }\n\nconst std::vector<std::pair<size_type, size_type>> dir4 = { {+1, 0}, {-1, 0}, { 0, +1}, { 0, -1} };\nconst std::vector<std::pair<size_type, size_type>> dir8 = { {-1, -1}, {-1, 0}, {-1, +1}, { 0, -1}, { 0, +1}, {+1, -1}, {+1, 0}, {+1, +1} };\n\n#ifdef _DEBUG\n#define debug(x) std::cout << \"[\" << __LINE__ << \"] \" << #x << \": \" << x << std::endl\n#else\n#define debug(x)\n#endif\n\ntemplate < class container > void concat(container& a, const container& b) {\n a.insert(std::end(a), std::begin(b), std::end(b));\n}\nstd::vector<size_type> iota(const size_type n) {\n std::vector<size_type> I(n);\n std::iota(std::begin(I), std::end(I), 0);\n return I;\n}\ntemplate < class container > std::vector<size_type> sort_idx(const container& a) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return a[i] < a[j] or (a[i] == a[j] and i < j); });\n return I;\n}\ntemplate < class container, class compare > std::vector<size_type> sort_idx(const container& a, const compare& cmp) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return cmp(a[i], a[j]) or (a[i] == a[j] and i < j); });\n return std::move(I);\n}\n\nstruct grid {\n using size_type = int;\n size_type H, W;\n grid(const size_type H, const size_type W) : H(H), W(W) {}\n bool contains(const size_type i, const size_type j) {\n return 0 <= i and i < H and 0 <= j and j < W;\n }\n};\n\nusing f64 = long double;\n\n} // namespace macro\n\nusing namespace macro;\n#line 3 \"A.cpp\"\n\nint main() {\n const i128 max = 1e12;\n i128 n = in(), m = in(), a = in(), b = in(), p = in(), q = in();\n auto f = [&](i128 x) { return 0 <= m - x ? m - x : x - m; };\n i128 ans = f(0);\n if(a == 1 and b == 1) {\n chmin(ans, f(min(n, floor(m, p + q)) * (p + q)));\n chmin(ans, f(min(n, ceil (m, p + q)) * (p + q)));\n } else {\n vector<i128> xs;\n i128 pow_a = 1, pow_b = 1;\n for(i128 k : rep(n)) {\n i128 x = p * pow_a + q * pow_b;\n if(x <= m + max) {\n xs.push_back(x);\n } else break;\n pow_a *= a, pow_b *= b;\n }\n\n vector<i128> x0, x1;\n const int n0 = xs.size() / 2;\n const int n1 = xs.size() - n0;\n for(int i : rep(n0)) x0.push_back(xs[i]);\n for(int i : rep(n1)) x1.push_back(xs[n0 + i]);\n \n vector<i128> s0, s1;\n for(int s : rep(1 << n0)) {\n i128 sum = 0;\n for(int i : rep(n0)) if(s & (1 << i)) sum += x0[i];\n if(sum <= m + max) s0.push_back(sum);\n }\n for(int s : rep(1 << n1)) {\n i128 sum = 0;\n for(int i : rep(n1)) if(s & (1 << i)) sum += x1[i];\n if(sum <= m + max) s1.push_back(sum);\n }\n\n sort(s0);\n sort(s1);\n for(i128 x : s0) {\n auto it = lower_bound(s1.begin(), s1.end(), m - x);\n if(it != s1.end()) chmin(ans, f(x + *it));\n if(it != s1.begin()) chmin(ans, f(x + *prev(it)));\n }\n }\n print(ans);\n}", "accuracy": 1, "time_ms": 210, "memory_kb": 52680, "score_of_the_acc": -1.2152, "final_rank": 8 }, { "submission_id": "aoj_2239_9008494", "code_snippet": "#line 2 \"cp-library/src/cp-template.hpp\"\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nusing i32 = int;\nusing u32 = unsigned int;\nusing i64 = long long;\nusing u64 = unsigned long long;\nusing i128 = __int128_t;\ntemplate < class T > bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }\ntemplate < class T > bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }\ntemplate < class T, class U > T ceil (T x, U y) { return (x > 0 ? (x + y - 1) / y : x / y); }\ntemplate < class T, class U > T floor(T x, U y) { return (x > 0 ? x / y : (x - y + 1) / y); }\nint popcnt(i32 x) { return __builtin_popcount(x); }\nint popcnt(u32 x) { return __builtin_popcount(x); }\nint popcnt(i64 x) { return __builtin_popcountll(x); }\nint popcnt(u64 x) { return __builtin_popcountll(x); }\n\n#line 2 \"cp-library/src/utility/rep_itr.hpp\"\ntemplate < class T > struct itr_rep {\n T i, d;\n constexpr itr_rep(const T i) noexcept : i(i), d(1) {}\n constexpr itr_rep(const T i, const T d) noexcept : i(i), d(d) {}\n void operator++() noexcept { i += d; }\n constexpr int operator*() const noexcept { return i; }\n constexpr bool operator!=(const itr_rep x) const noexcept { return d > 0 ? i < x.i : i > x.i; }\n};\n\ntemplate < class T > struct rep {\n const itr_rep< T > s, t;\n constexpr rep(const T t) noexcept : s(0), t(t) {}\n constexpr rep(const T s, const T t) noexcept : s(s), t(t) {}\n constexpr rep(const T s, const T t, const T d) noexcept : s(s, d), t(t, d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n\ntemplate < class T > struct revrep {\n const itr_rep < T > s, t;\n constexpr revrep(const T t) noexcept : s(t - 1, -1), t(-1, -1) {}\n constexpr revrep(const T s, const T t) noexcept : s(t - 1, -1), t(s - 1, -1) {}\n constexpr revrep(const T s, const T t, const T d) noexcept : s(t - 1, -d), t(s - 1, -d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n#line 3 \"cp-library/src/utility/io.hpp\"\n\n/* 128bit integer */\nistream& operator>>(istream& is, i128& x) {\n std::string s; is >> s;\n int pm = (s[0] == '-');\n x = 0;\n for(int i : rep(pm, int(s.size()))) x = x * 10 + (s[i] - '0');\n if(pm) x *= -1;\n return is;\n}\nostream& operator<<(ostream& os, const i128& x) {\n if(x == 0) return os << '0';\n i128 y = x;\n if(y < 0) { os << '-'; y *= -1; }\n std::vector<int> ny;\n while(y > 0) { ny.push_back(y % 10); y /= 10; }\n for(int i : revrep(ny.size())) os << ny[i];\n return os;\n}\n\ntemplate < class S, class T > istream& operator>>(istream& is, std::pair< S, T >& x) { is >> x.first >> x.second; return is; }\ntemplate < class S, class T > ostream& operator<<(ostream& os, const std::pair< S, T >& x) { os << x.first << \" \" << x.second; return os; }\n\nnamespace scanner {\n struct sca {\n template < class T > operator T() {\n T s; std::cin >> s; return s;\n }\n };\n struct vec {\n int n;\n vec(int n) : n(n) {}\n template < class T > operator std::vector< T >() {\n std::vector< T > v(n);\n for(T& x : v) std::cin >> x;\n return v;\n }\n };\n struct mat {\n int h, w;\n mat(int h, int w) : h(h), w(w) {}\n template < class T > operator std::vector< std::vector< T > >() {\n std::vector m(h, std::vector< T >(w));\n for(std::vector< T >& v : m) for(T& x : v) std::cin >> x;\n return m;\n }\n };\n struct speedup {\n speedup() {\n std::cin.tie(0);\n std::ios::sync_with_stdio(0);\n }\n } speedup_instance;\n}\nscanner::sca in() { return scanner::sca(); }\nscanner::vec in(int n) { return scanner::vec(n); }\nscanner::mat in(int h, int w) { return scanner::mat(h, w); }\n\nnamespace printer {\n void precision(int d) { std::cout << std::fixed << std::setprecision(d); }\n void flush() { std::cout.flush(); }\n}\n\ntemplate < class T >\nostream& operator<<(ostream& os, const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) { os << a[i]; if(i != n - 1) os << ' '; }\n return os;\n}\n\nint print() { std::cout << '\\n'; return 0; }\ntemplate < class head, class... tail > int print(head&& h, tail&&... t) {\n std::cout << h; if(sizeof...(tail)) std::cout << ' ';\n return print(std::forward<tail>(t)...);\n}\ntemplate < class T > int print_n(const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) std::cout << a[i] << \"\\n\";\n return 0;\n}\n\n\n#line 2 \"cp-library/src/utility/key_val.hpp\"\n\ntemplate < class K, class V >\nstruct key_val {\n K key; V val;\n key_val() {}\n key_val(K key, V val) : key(key), val(val) {}\n template < std::size_t Index >\n std::tuple_element_t< Index, key_val >& get() {\n if constexpr (Index == 0) return key;\n if constexpr (Index == 1) return val;\n }\n};\n\nnamespace std {\n\ntemplate < class K, class V > struct tuple_size < key_val< K, V > > : integral_constant< size_t, 2 > {};\ntemplate < class K, class V > struct tuple_element < 0, key_val< K, V > > { using type = K; };\ntemplate < class K, class V > struct tuple_element < 1, key_val< K, V > > { using type = V; };\n\n}\n#line 2 \"cp-library/src/utility/vec_op.hpp\"\ntemplate < class T > key_val< int, T > max_of(const vector< T >& a) {\n int i = std::max_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class T > key_val< int, T > min_of(const vector< T >& a) {\n int i = std::min_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class S, class T > S sum_of(const vector< T >& a) {\n S sum = 0;\n for(const T x : a) sum += x;\n return sum;\n}\ntemplate < class S, class T > vector< S > freq_of(const vector< T >& a, T L, T R) {\n vector< S > res(R - L, S(0));\n for(const T x : a) res[x - L] += 1;\n return res;\n}\ntemplate < class S, class T > struct prefix_sum {\n vector< S > s;\n prefix_sum(const vector< T >& a) : s(a) {\n s.insert(s.begin(), S(0));\n for(int i : rep(a.size())) s[i + 1] += s[i];\n }\n // [L, R)\n S sum(int L, int R) { return s[R] - s[L]; }\n};\n#line 3 \"cp-library/src/utility/heap.hpp\"\n\ntemplate < class T > using heap_min = std::priority_queue< T, std::vector< T >, std::greater< T > >;\ntemplate < class T > using heap_max = std::priority_queue< T, std::vector< T >, std::less< T > >;\n\n#line 27 \"cp-library/src/cp-template.hpp\"\n\n#line 1 \"cp-library/src/algorithm/bin_search.hpp\"\ntemplate < class T, class F >\nT bin_search(T ok, T ng, F f) {\n while(abs(ng - ok) > 1) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n\ntemplate < class T, class F >\nT bin_search_real(T ok, T ng, F f, int step = 80) {\n while(step--) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n#line 2 \"cp-library/src/algorithm/argsort.hpp\"\n\ntemplate < class T > std::vector< int > argsort(const std::vector< T > &a) {\n std::vector< int > ids((int)a.size());\n std::iota(ids.begin(), ids.end(), 0);\n std::sort(ids.begin(), ids.end(), [&](int i, int j) {\n return a[i] < a[j] || (a[i] == a[j] && i < j);\n });\n return ids;\n}\n#line 1 \"macro.hpp\"\nnamespace macro {\n\nusing size_type = int;\ntemplate < class container > void sort(container& a) { std::sort(std:: begin(a), std:: end(a)); }\ntemplate < class container > void rsort(container& a) { std::sort(std::rbegin(a), std::rend(a)); }\ntemplate < class container > void reverse(container& a) { std::reverse(std::begin(a), std::end(a)); }\ntemplate < class container > void unique(container& a) {\n std::sort(std::begin(a), std::end(a));\n a.erase(std::unique(std::begin(a), std::end(a)), std::end(a));\n}\ntemplate < class container > container sorted(const container& a) { container b = a; sort(b); return std::move(b); }\ntemplate < class container > container rsorted(const container& a) { container b = a; rsort(b); return std::move(b); }\ntemplate < class container, class compare > void sort(container& a, const compare& cmp) { std::sort(std::begin(a), std::end(a), cmp); }\ntemplate < class container, class compare > container sorted(const container& a, const compare& cmp) { container b = a; sort(b, cmp); return std::move(b); }\ntemplate < class container, class value > size_type lower_bound(const container& a, const value& x) { return std::lower_bound(std::begin(a), std::end(a), x) - std::begin(a); }\ntemplate < class container, class value > size_type upper_bound(const container& a, const value& x) { return std::upper_bound(std::begin(a), std::end(a), x) - std::begin(a); }\n\nconst std::vector<std::pair<size_type, size_type>> dir4 = { {+1, 0}, {-1, 0}, { 0, +1}, { 0, -1} };\nconst std::vector<std::pair<size_type, size_type>> dir8 = { {-1, -1}, {-1, 0}, {-1, +1}, { 0, -1}, { 0, +1}, {+1, -1}, {+1, 0}, {+1, +1} };\n\n#ifdef _DEBUG\n#define debug(x) std::cout << \"[\" << __LINE__ << \"] \" << #x << \": \" << x << std::endl\n#else\n#define debug(x)\n#endif\n\ntemplate < class container > void concat(container& a, const container& b) {\n a.insert(std::end(a), std::begin(b), std::end(b));\n}\nstd::vector<size_type> iota(const size_type n) {\n std::vector<size_type> I(n);\n std::iota(std::begin(I), std::end(I), 0);\n return I;\n}\ntemplate < class container > std::vector<size_type> sort_idx(const container& a) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return a[i] < a[j] or (a[i] == a[j] and i < j); });\n return I;\n}\ntemplate < class container, class compare > std::vector<size_type> sort_idx(const container& a, const compare& cmp) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return cmp(a[i], a[j]) or (a[i] == a[j] and i < j); });\n return std::move(I);\n}\n\nstruct grid {\n using size_type = int;\n size_type H, W;\n grid(const size_type H, const size_type W) : H(H), W(W) {}\n bool contains(const size_type i, const size_type j) {\n return 0 <= i and i < H and 0 <= j and j < W;\n }\n};\n\nusing f64 = long double;\n\n} // namespace macro\n\nusing namespace macro;\n#line 3 \"A.cpp\"\n\nint main() {\n i128 n = in(), m = in(), a = in(), b = in(), p = in(), q = in();\n auto f = [&](i128 x) { return 0 <= m - x ? m - x : x - m; };\n i128 ans = f(0);\n if(a == 1 and b == 1) {\n chmin(ans, f(min(n, floor(m, p + q)) * (p + q)));\n chmin(ans, f(min(n, ceil (m, p + q)) * (p + q)));\n } else {\n vector<i128> xs;\n i128 pow_a = 1, pow_b = 1;\n for(i128 k : rep(n)) {\n i128 x = p * pow_a + q * pow_b;\n if(x <= m) {\n xs.push_back(x);\n } else break;\n pow_a *= a, pow_b *= b;\n }\n\n vector<i128> x0, x1;\n const int n0 = xs.size() / 2;\n const int n1 = xs.size() - n0;\n for(int i : rep(n0)) x0.push_back(xs[i]);\n for(int i : rep(n1)) x1.push_back(xs[n0 + i]);\n \n vector<i128> s0, s1;\n for(int s : rep(1 << n0)) {\n i128 sum = 0;\n for(int i : rep(n0)) if(s & (1 << i)) sum += x0[i];\n if(sum <= m + i128(1e12)) s0.push_back(sum);\n }\n for(int s : rep(1 << n1)) {\n i128 sum = 0;\n for(int i : rep(n1)) if(s & (1 << i)) sum += x1[i];\n if(sum <= m + i128(1e12)) s1.push_back(sum);\n }\n\n sort(s0);\n sort(s1);\n for(i128 x : s0) {\n auto it = lower_bound(s1.begin(), s1.end(), m - x);\n if(it != s1.end()) chmin(ans, f(x + *it));\n if(it != s1.begin()) chmin(ans, f(x + *prev(it)));\n }\n }\n print(ans);\n}", "accuracy": 0.6, "time_ms": 150, "memory_kb": 44656, "score_of_the_acc": -0.9421, "final_rank": 12 }, { "submission_id": "aoj_2239_9008465", "code_snippet": "#line 2 \"cp-library/src/cp-template.hpp\"\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nusing i32 = int;\nusing u32 = unsigned int;\nusing i64 = long long;\nusing u64 = unsigned long long;\nusing i128 = __int128_t;\ntemplate < class T > bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }\ntemplate < class T > bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }\ntemplate < class T, class U > T ceil (T x, U y) { return (x > 0 ? (x + y - 1) / y : x / y); }\ntemplate < class T, class U > T floor(T x, U y) { return (x > 0 ? x / y : (x - y + 1) / y); }\nint popcnt(i32 x) { return __builtin_popcount(x); }\nint popcnt(u32 x) { return __builtin_popcount(x); }\nint popcnt(i64 x) { return __builtin_popcountll(x); }\nint popcnt(u64 x) { return __builtin_popcountll(x); }\n\n#line 2 \"cp-library/src/utility/rep_itr.hpp\"\ntemplate < class T > struct itr_rep {\n T i, d;\n constexpr itr_rep(const T i) noexcept : i(i), d(1) {}\n constexpr itr_rep(const T i, const T d) noexcept : i(i), d(d) {}\n void operator++() noexcept { i += d; }\n constexpr int operator*() const noexcept { return i; }\n constexpr bool operator!=(const itr_rep x) const noexcept { return d > 0 ? i < x.i : i > x.i; }\n};\n\ntemplate < class T > struct rep {\n const itr_rep< T > s, t;\n constexpr rep(const T t) noexcept : s(0), t(t) {}\n constexpr rep(const T s, const T t) noexcept : s(s), t(t) {}\n constexpr rep(const T s, const T t, const T d) noexcept : s(s, d), t(t, d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n\ntemplate < class T > struct revrep {\n const itr_rep < T > s, t;\n constexpr revrep(const T t) noexcept : s(t - 1, -1), t(-1, -1) {}\n constexpr revrep(const T s, const T t) noexcept : s(t - 1, -1), t(s - 1, -1) {}\n constexpr revrep(const T s, const T t, const T d) noexcept : s(t - 1, -d), t(s - 1, -d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n#line 3 \"cp-library/src/utility/io.hpp\"\n\n/* 128bit integer */\nistream& operator>>(istream& is, i128& x) {\n std::string s; is >> s;\n int pm = (s[0] == '-');\n x = 0;\n for(int i : rep(pm, int(s.size()))) x = x * 10 + (s[i] - '0');\n if(pm) x *= -1;\n return is;\n}\nostream& operator<<(ostream& os, const i128& x) {\n if(x == 0) return os << '0';\n i128 y = x;\n if(y < 0) { os << '-'; y *= -1; }\n std::vector<int> ny;\n while(y > 0) { ny.push_back(y % 10); y /= 10; }\n for(int i : revrep(ny.size())) os << ny[i];\n return os;\n}\n\ntemplate < class S, class T > istream& operator>>(istream& is, std::pair< S, T >& x) { is >> x.first >> x.second; return is; }\ntemplate < class S, class T > ostream& operator<<(ostream& os, const std::pair< S, T >& x) { os << x.first << \" \" << x.second; return os; }\n\nnamespace scanner {\n struct sca {\n template < class T > operator T() {\n T s; std::cin >> s; return s;\n }\n };\n struct vec {\n int n;\n vec(int n) : n(n) {}\n template < class T > operator std::vector< T >() {\n std::vector< T > v(n);\n for(T& x : v) std::cin >> x;\n return v;\n }\n };\n struct mat {\n int h, w;\n mat(int h, int w) : h(h), w(w) {}\n template < class T > operator std::vector< std::vector< T > >() {\n std::vector m(h, std::vector< T >(w));\n for(std::vector< T >& v : m) for(T& x : v) std::cin >> x;\n return m;\n }\n };\n struct speedup {\n speedup() {\n std::cin.tie(0);\n std::ios::sync_with_stdio(0);\n }\n } speedup_instance;\n}\nscanner::sca in() { return scanner::sca(); }\nscanner::vec in(int n) { return scanner::vec(n); }\nscanner::mat in(int h, int w) { return scanner::mat(h, w); }\n\nnamespace printer {\n void precision(int d) { std::cout << std::fixed << std::setprecision(d); }\n void flush() { std::cout.flush(); }\n}\n\ntemplate < class T >\nostream& operator<<(ostream& os, const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) { os << a[i]; if(i != n - 1) os << ' '; }\n return os;\n}\n\nint print() { std::cout << '\\n'; return 0; }\ntemplate < class head, class... tail > int print(head&& h, tail&&... t) {\n std::cout << h; if(sizeof...(tail)) std::cout << ' ';\n return print(std::forward<tail>(t)...);\n}\ntemplate < class T > int print_n(const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) std::cout << a[i] << \"\\n\";\n return 0;\n}\n\n\n#line 2 \"cp-library/src/utility/key_val.hpp\"\n\ntemplate < class K, class V >\nstruct key_val {\n K key; V val;\n key_val() {}\n key_val(K key, V val) : key(key), val(val) {}\n template < std::size_t Index >\n std::tuple_element_t< Index, key_val >& get() {\n if constexpr (Index == 0) return key;\n if constexpr (Index == 1) return val;\n }\n};\n\nnamespace std {\n\ntemplate < class K, class V > struct tuple_size < key_val< K, V > > : integral_constant< size_t, 2 > {};\ntemplate < class K, class V > struct tuple_element < 0, key_val< K, V > > { using type = K; };\ntemplate < class K, class V > struct tuple_element < 1, key_val< K, V > > { using type = V; };\n\n}\n#line 2 \"cp-library/src/utility/vec_op.hpp\"\ntemplate < class T > key_val< int, T > max_of(const vector< T >& a) {\n int i = std::max_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class T > key_val< int, T > min_of(const vector< T >& a) {\n int i = std::min_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class S, class T > S sum_of(const vector< T >& a) {\n S sum = 0;\n for(const T x : a) sum += x;\n return sum;\n}\ntemplate < class S, class T > vector< S > freq_of(const vector< T >& a, T L, T R) {\n vector< S > res(R - L, S(0));\n for(const T x : a) res[x - L] += 1;\n return res;\n}\ntemplate < class S, class T > struct prefix_sum {\n vector< S > s;\n prefix_sum(const vector< T >& a) : s(a) {\n s.insert(s.begin(), S(0));\n for(int i : rep(a.size())) s[i + 1] += s[i];\n }\n // [L, R)\n S sum(int L, int R) { return s[R] - s[L]; }\n};\n#line 3 \"cp-library/src/utility/heap.hpp\"\n\ntemplate < class T > using heap_min = std::priority_queue< T, std::vector< T >, std::greater< T > >;\ntemplate < class T > using heap_max = std::priority_queue< T, std::vector< T >, std::less< T > >;\n\n#line 27 \"cp-library/src/cp-template.hpp\"\n\n#line 1 \"cp-library/src/algorithm/bin_search.hpp\"\ntemplate < class T, class F >\nT bin_search(T ok, T ng, F f) {\n while(abs(ng - ok) > 1) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n\ntemplate < class T, class F >\nT bin_search_real(T ok, T ng, F f, int step = 80) {\n while(step--) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n#line 2 \"cp-library/src/algorithm/argsort.hpp\"\n\ntemplate < class T > std::vector< int > argsort(const std::vector< T > &a) {\n std::vector< int > ids((int)a.size());\n std::iota(ids.begin(), ids.end(), 0);\n std::sort(ids.begin(), ids.end(), [&](int i, int j) {\n return a[i] < a[j] || (a[i] == a[j] && i < j);\n });\n return ids;\n}\n#line 1 \"macro.hpp\"\nnamespace macro {\n\nusing size_type = int;\ntemplate < class container > void sort(container& a) { std::sort(std:: begin(a), std:: end(a)); }\ntemplate < class container > void rsort(container& a) { std::sort(std::rbegin(a), std::rend(a)); }\ntemplate < class container > void reverse(container& a) { std::reverse(std::begin(a), std::end(a)); }\ntemplate < class container > void unique(container& a) {\n std::sort(std::begin(a), std::end(a));\n a.erase(std::unique(std::begin(a), std::end(a)), std::end(a));\n}\ntemplate < class container > container sorted(const container& a) { container b = a; sort(b); return std::move(b); }\ntemplate < class container > container rsorted(const container& a) { container b = a; rsort(b); return std::move(b); }\ntemplate < class container, class compare > void sort(container& a, const compare& cmp) { std::sort(std::begin(a), std::end(a), cmp); }\ntemplate < class container, class compare > container sorted(const container& a, const compare& cmp) { container b = a; sort(b, cmp); return std::move(b); }\ntemplate < class container, class value > size_type lower_bound(const container& a, const value& x) { return std::lower_bound(std::begin(a), std::end(a), x) - std::begin(a); }\ntemplate < class container, class value > size_type upper_bound(const container& a, const value& x) { return std::upper_bound(std::begin(a), std::end(a), x) - std::begin(a); }\n\nconst std::vector<std::pair<size_type, size_type>> dir4 = { {+1, 0}, {-1, 0}, { 0, +1}, { 0, -1} };\nconst std::vector<std::pair<size_type, size_type>> dir8 = { {-1, -1}, {-1, 0}, {-1, +1}, { 0, -1}, { 0, +1}, {+1, -1}, {+1, 0}, {+1, +1} };\n\n#ifdef _DEBUG\n#define debug(x) std::cout << \"[\" << __LINE__ << \"] \" << #x << \": \" << x << std::endl\n#else\n#define debug(x)\n#endif\n\ntemplate < class container > void concat(container& a, const container& b) {\n a.insert(std::end(a), std::begin(b), std::end(b));\n}\nstd::vector<size_type> iota(const size_type n) {\n std::vector<size_type> I(n);\n std::iota(std::begin(I), std::end(I), 0);\n return I;\n}\ntemplate < class container > std::vector<size_type> sort_idx(const container& a) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return a[i] < a[j] or (a[i] == a[j] and i < j); });\n return I;\n}\ntemplate < class container, class compare > std::vector<size_type> sort_idx(const container& a, const compare& cmp) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return cmp(a[i], a[j]) or (a[i] == a[j] and i < j); });\n return std::move(I);\n}\n\nstruct grid {\n using size_type = int;\n size_type H, W;\n grid(const size_type H, const size_type W) : H(H), W(W) {}\n bool contains(const size_type i, const size_type j) {\n return 0 <= i and i < H and 0 <= j and j < W;\n }\n};\n\nusing f64 = long double;\n\n} // namespace macro\n\nusing namespace macro;\n#line 3 \"A.cpp\"\n\nint main() {\n i128 n = in(), m = in(), a = in(), b = in(), p = in(), q = in();\n auto f = [&](i128 x) { return 0 <= m - x ? m - x : x - m; };\n i128 ans = f(0);\n if(a == 1 and b == 1) {\n chmin(ans, f(min(n, floor(m, p + q)) * (p + q)));\n chmin(ans, f(min(n, ceil (m, p + q)) * (p + q)));\n } else {\n vector<i128> xs;\n i128 pow_a = 1, pow_b = 1;\n for(i128 k : rep(n)) {\n i128 x = p * pow_a + q * pow_b;\n if(x <= m) {\n xs.push_back(x);\n } else break;\n pow_a *= a, pow_b *= b;\n }\n\n vector<i128> x0, x1;\n const int n0 = xs.size() / 2;\n const int n1 = xs.size() - n0;\n for(int i : rep(n0)) x0.push_back(xs[i]);\n for(int i : rep(n1)) x1.push_back(xs[n0 + i]);\n \n vector<i128> s0, s1;\n for(int s : rep(1 << n0)) {\n i128 sum = 0;\n for(int i : rep(n0)) if(s & (1 << i)) sum += x0[i];\n if(sum <= m) s0.push_back(sum);\n }\n for(int s : rep(1 << n1)) {\n i128 sum = 0;\n for(int i : rep(n1)) if(s & (1 << i)) sum += x1[i];\n if(sum <= m) s1.push_back(sum);\n }\n\n sort(s0);\n sort(s1);\n for(i128 x : s0) {\n auto it = lower_bound(s1.begin(), s1.end(), m - x);\n if(it != s1.end()) chmin(ans, f(x + *it));\n if(it != s1.begin()) chmin(ans, f(x + *prev(it)));\n }\n }\n print(ans);\n}", "accuracy": 0.3142857142857143, "time_ms": 140, "memory_kb": 44736, "score_of_the_acc": -0.9314, "final_rank": 15 }, { "submission_id": "aoj_2239_7837685", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing i32 = int;\nusing i64 = long long;\nusing i128 = __int128_t;\n\ni64 n, m, a, b, p, q; \n\ni64 solve1() {\n i64 res = m;\n { // 手前\n i64 v = min(n, m / (p + q)) * (p + q);\n res = min(res, abs(m - v));\n }\n { // 通り越す\n i64 v = min(n, (m + p + q + 1) / (p + q)) * (p + q);\n res = min(res, abs(m - v));\n }\n return res;\n}\n\nconst i64 sup = 1e15;\n\ni128 f(i64 k) {\n i128 pa = p;\n for (i32 _ = 0 ; _ < k ; _++) {\n if (pa > sup) return -1;\n pa *= a;\n }\n i128 qb = q;\n for (i32 _ = 0 ; _ < k ; _++) {\n if (qb > sup) return -1;\n qb *= b;\n }\n\n i128 res = pa + qb;\n if (res > sup) return -1;\n return res;\n}\n\ni64 solve2() {\n vector<i64> T{}; \n for (i32 i = 0 ; i < n and f(i) > 0 ; i++) {\n T.push_back((i64)f(i));\n if (T.back() > m) break;\n }\n const i32 size = 20;\n vector<i64> F{}, B{};\n \n for (i32 bit = 0 ; bit < (1 << size) ; bit++) {\n i64 fr = 0LL, bk = 0LL;\n for (i32 i = 0 ; i < size ; i++) if (bit & (1 << i)) {\n fr = (i < (i32)T.size() ? fr + T[i] : -1LL);\n bk = (size + i < (i32)T.size() ? bk + T[i + size] : -1LL);\n }\n if (0 <= fr) F.push_back(fr);\n if (0 <= bk) B.push_back(bk);\n }\n sort(F.begin(), F.end());\n sort(B.begin(), B.end());\n\n i64 ans = m;\n for (auto fr : F) {\n auto it = lower_bound(B.begin(), B.end(), m - fr);\n if (it != B.end()) ans = min(ans, abs(*it + fr - m));\n if (it != B.begin()) ans = min(ans, abs(*prev(it) + fr - m));\n }\n\n return ans;\n\n return -1;\n}\n\nint main() {\n cin >> n >> m >> a >> b >> p >> q;\n if (a == 1 and b == 1) {\n cout << solve1() << endl;\n }\n else {\n cout << solve2() << endl;\n }\n}", "accuracy": 1, "time_ms": 90, "memory_kb": 19536, "score_of_the_acc": -0.249, "final_rank": 3 }, { "submission_id": "aoj_2239_7835543", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\n\nvector<ll> all(vector<ll> xs){\n vector<ll> res;\n for(int i=0;i<(1<<xs.size());++i){\n ll add = 0;\n for(int j=0;j<(int)xs.size();++j){\n if((1<<j) & i){\n add += xs[j];\n }\n }\n res.push_back(add);\n }\n sort(res.begin(),res.end());\n return res;\n}\n\nll func(){\n ll n;\n ll m;\n ll a;\n ll b;\n ll p;\n ll q;\n cin >> n >> m >> a >> b >> p >> q;\n if(a == 1 and b == 1){\n ll low = m / (p + q);\n ll high = low + 1;\n low = min(low, n);\n high = min(high, n);\n ll dist_l = abs(m - low * (p + q));\n ll dist_h = abs(m - high * (p + q));\n return min(dist_l, dist_h);\n }\n ll ak = 1;\n ll bk = 1;\n vector<ll> line;\n for(int i=0;i<n;++i){\n line.push_back(p * ak + q * bk);\n if((double)p * ak * a + (double) q * bk * b > m * 3)break;\n ak *= a;\n bk *= b;\n }\n int N = line.size();\n if(N == 1){\n return min(m, abs(m - line[0]));\n }\n vector<ll> as(line.begin(),line.begin()+N/2);\n vector<ll> bs(line.begin()+N/2,line.end());\n vector<ll> ass = all(as);\n ll res = m;\n for(auto i:all(bs)){\n int p = lower_bound(ass.begin(),ass.end(),m - i) - ass.begin();\n if(p != (int)ass.size()){\n res = min(res, abs(m - (i + ass[p])));\n }\n if(p > 0){\n res = min(res, abs(m - (i + ass[p-1])));\n }\n }\n return res;\n}\n\nint main(){\n cout << func() << endl;\n}", "accuracy": 1, "time_ms": 240, "memory_kb": 45444, "score_of_the_acc": -1.0754, "final_rank": 7 }, { "submission_id": "aoj_2239_7122381", "code_snippet": "// #pragma GCC target(\"avx2\")\n// #pragma GCC optimize(\"O3\")\n// #pragma GCC optimize(\"unroll-loops\")\n#include<bits/stdc++.h>\n// #include<atcoder/dsu>\nusing namespace std;\n// using namespace atcoder;\n\n\nvoid solve(){\n\tlong long n, m, a, b, p, q;\n\tcin >> n >> m >> a >> b >> p >> q;\n\tif(a == 1 && b == 1){\n\t\tlong long d = p + q;\n\t\tif(n <= m / d) cout << m - n * d << \"\\n\";\n\t\telse{\n\t\t\tlong long ans = min(m % d, (-m % d + d) % d);\n\t\t\tcout << ans << \"\\n\";\n\t\t}\n\t\treturn;\n\t}\n\tvector<long long> A;\n\tlong long pp = p;\n\tlong long qq = q;\n\tfor(int i = 0; i < n; i++){\n\t\tA.push_back(pp + qq);\n\t\tif(pp + qq > m) break;\n\t\tif(pp > 2 * m / a) break;\n\t\tif(qq > 2 * m / b) break;\n\t\tpp *= a;\n\t\tqq *= b;\n\t}\n\n\tn = A.size();\n\tvector<long long> L, R;\n\tfor(int i = 0; i < n; i++){\n\t\tif(i < n / 2) L.push_back(A[i]);\n\t\telse R.push_back(A[i]);\n\t}\n\n\tauto f=[&m](vector<long long> &A){\n\t\tvector<long long> res;\n\t\tres.push_back(0);\n\t\tfor(auto a:A){\n\t\t\tint sz = res.size();\n\t\t\tfor(int i = 0; i < sz; i++) res.push_back(a + res[i]);\n\t\t}\n\t\tsort(res.begin(), res.end());\n\t\tswap(A, res);\n\t};\n\n\tf(L);\n\tf(R);\n\tR.push_back(1LL << 60);\n\tlong long ans = 1LL << 60;\n\tint r = 0;\n\tfor(int i = L.size() - 1; i >= 0; i--){\n\t\tlong long l = L[i];\n\t\twhile(l + R[r] <= m) r++;\n\t\tif(m >= (l + R[r - 1])) ans = min(ans, m - (l + R[r - 1]));\n\t\tans = min(ans, l + R[r] - m);\n\t}\n\tcout << ans << \"\\n\";\n}\n\nint main(){\n\tcin.tie(0)->sync_with_stdio(0);\n\tint t;\n\tt = 1;\n\t// cin >> t;\n\twhile(t--) solve();\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 45136, "score_of_the_acc": -0.8147, "final_rank": 4 }, { "submission_id": "aoj_2239_7122377", "code_snippet": "// #pragma GCC target(\"avx2\")\n// #pragma GCC optimize(\"O3\")\n// #pragma GCC optimize(\"unroll-loops\")\n#include<bits/stdc++.h>\n// #include<atcoder/dsu>\nusing namespace std;\n// using namespace atcoder;\n\n\nvoid solve(){\n\tlong long n, m, a, b, p, q;\n\tcin >> n >> m >> a >> b >> p >> q;\n\tif(a == 1 && b == 1){\n\t\tlong long d = p + q;\n\t\tif(n <= m / d) cout << m - n * d << \"\\n\";\n\t\telse{\n\t\t\tlong long ans = min(m % d, (-m % d + d) % d);\n\t\t\tcout << ans << \"\\n\";\n\t\t}\n\t\treturn;\n\t}\n\tvector<long long> A;\n\tlong long pp = p;\n\tlong long qq = q;\n\tfor(int i = 0; i < n; i++){\n\t\tA.push_back(pp + qq);\n\t\tif(pp + qq > m) break;\n\t\tif(pp > 2 * m / a) break;\n\t\tif(qq > 2 * m / b) break;\n\t\tpp *= a;\n\t\tqq *= b;\n\t}\n\n\tn = A.size();\n\tvector<long long> L, R;\n\tfor(int i = 0; i < n; i++){\n\t\tif(i < n / 2) L.push_back(A[i]);\n\t\telse R.push_back(A[i]);\n\t}\n\n\tauto f=[&m](vector<long long> &A){\n\t\tvector<long long> res;\n\t\tres.push_back(0);\n\t\tfor(auto a:A){\n\t\t\tint sz = res.size();\n\t\t\tfor(int i = 0; i < sz; i++) res.push_back(a + res[i]);\n\t\t}\n\t\tsort(res.begin(), res.end());\n\t\tswap(A, res);\n\t\twhile(A[A.size() - 1] > m) A.pop_back();\n\t};\n\n\tf(L);\n\tf(R);\n\tR.push_back(1LL << 60);\n\tlong long ans = 1LL << 60;\n\tint r = 0;\n\tfor(int i = L.size() - 1; i >= 0; i--){\n\t\tlong long l = L[i];\n\t\twhile(l + R[r] <= m) r++;\n\t\tans = min(ans, m - (l + R[r - 1]));\n\t\tans = min(ans, l + R[r] - m);\n\t}\n\tcout << ans << \"\\n\";\n}\n\nint main(){\n\tcin.tie(0)->sync_with_stdio(0);\n\tint t;\n\tt = 1;\n\t// cin >> t;\n\twhile(t--) solve();\n}", "accuracy": 0.3142857142857143, "time_ms": 40, "memory_kb": 27800, "score_of_the_acc": -0.3888, "final_rank": 14 }, { "submission_id": "aoj_2239_6731193", "code_snippet": "#include <bits/stdc++.h>\n//#include <atcoder/all>\n//using namespace atcoder;\nusing namespace std;\nusing ll = long long;\nusing vll = vector<ll>;\nusing vvll = vector<vll>;\nusing vvvll = vector<vvll>;\nusing vb = vector<bool>;\nusing vvb = vector<vb>;\nusing vvvb = vector<vvb>;\nusing vd = vector<double>;\nusing vvd = vector<vd>;\nusing vvvd = vector<vvd>;\n#define all(A) A.begin(),A.end()\n#define ciN cin\n#define rep(i, n) for (ll i = 0; i < (ll) (n); i++)\nusing pqr = priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>>;\n\ntemplate<class T>\nbool chmax(T& p, T q) {\n if (p < q) {\n p = q;\n return 1;\n }\n else {\n return 0;\n }\n}\ntemplate<class T>\nbool chmin(T& p, T q) {\n if (p > q) {\n p = q;\n return 1;\n }\n else {\n return 0;\n }\n}\nll gcd(ll(a), ll(b)) {\n ll c = a;\n while (a % b != 0) {\n c = a % b;\n a = b;\n b = c;\n }\n return b;\n}\n\n\n#define ss second.second\n#define sf second.first\nusing paii = pair<int, int>;\n//chmin必要\n/*\n有向木の最小全域木を求める.\nO(|V|*|E|)らしい.知らんけど\n*/\ntemplate<class T>\nT Chu_Liu_Edmonds_algorithm(const vector<pair<T, paii>>& E, int v, int root = 0) {//E:{重み,{start,end}} //v:|V| //root:根\n\n vector<pair<T, int>> Vm(v, pair<T, int>(1e9, -1));\n vector<int> gr(v, 0);\n vector<bool> P(v, false), f(v, false);\n int cnt = 0;\n T res = 0;\n vector<pair<T, paii>> nE;\n\n for (auto e : E) {\n chmin(Vm[e.ss], { e.first, e.sf });\n }\n Vm[root] = { -1, -1 };\n\n rep(i, v) {\n if (f[i]) continue;\n vector<int> ch;\n int x = i;\n for (; x != -1 && !f[x];) {\n f[x] = 1;\n ch.push_back(x);\n x = Vm[x].second;\n }\n if (x == -1) {\n rep(j, ch.size()) {\n gr[ch[j]] = cnt;\n cnt++;\n }\n }\n else {\n bool Q = 0;\n for (long long j : ch) {\n gr[j] = cnt;\n if (j == x) {\n P[cnt] = 1;\n Q = 1;\n }\n if (!Q) cnt++;\n }\n if (Q) cnt++;\n }\n }\n if (cnt == v) {\n T ans = 1;\n rep(i, v)ans += Vm[i].first;\n //cout<<ans<<endl;\n return ans;\n }\n\n rep(i, v) {\n if (i != root && P[gr[i]]) res += Vm[i].first;\n }\n\n for (auto e : E) {\n int p = e.ss;\n int s = gr[e.sf];\n int t = gr[e.ss];\n if (s == t) continue;\n else if (P[t]) {\n nE.push_back({ e.first - Vm[p].first, { s, t } });\n }\n else {\n nE.push_back({ e.first, { s, t } });\n }\n }\n //cout<<res<<endl;\n return res + Chu_Liu_Edmonds_algorithm(nE, cnt, gr[root]);\n}\n\n\nint main() {\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n\n ll N, M, A, B, P, Q;\n cin >> N >> M >> A >> B >> P >> Q;\n ll an = M;\n if (A == 1 && B == 1) {\n ll d = (P + Q);\n\n ll D = M / d;\n if (D <= N) {\n chmin(an, abs(M - D * d));\n }\n D++;\n if (D <= N) {\n chmin(an, abs(M - D * d));\n }\n if (N <= 1e18 / d)chmin(an, abs(M - N * d));\n cout << an << endl;\n return 0;\n }\n\n vll D;\n ll a = 1, b = 1;\n while (N!=0) {\n ll S = P * a + Q * b;\n if (S > 2 * M)break;\n D.push_back(S);\n if (a > 1e18 / A || b > 1e18 / B)break;\n a *= A;\n b *= B;\n N--;\n }\n\n //cout<<D.size()<<endl;\n\n N = D.size();\n\n set<ll> S;\n\n rep(bit, (1 << N / 2)) {\n ll res = 0;\n rep(i, N / 2) {\n if (bit & (1 << i))res += D[i];\n }\n S.insert(res);\n }\n S.insert(2e18);\n ll Z = N - N / 2;\n\n rep(bit, (1 << Z)) {\n ll res = 0;\n rep(i, Z) {\n if (bit & (1 << i))res += D[i + (N / 2)];\n }\n if (res >= M) {\n chmin(an, res - M);\n continue;\n }\n auto re = S.upper_bound(M - res);\n chmin(an, abs(M - res - *re));\n re--;\n chmin(an, abs(M - res - *re));\n }\n\n cout << an << endl;\n\n}", "accuracy": 1, "time_ms": 260, "memory_kb": 40096, "score_of_the_acc": -0.9693, "final_rank": 5 }, { "submission_id": "aoj_2239_6731192", "code_snippet": "#include <bits/stdc++.h>\n//#include <atcoder/all>\n//using namespace atcoder;\nusing namespace std;\nusing ll = long long;\nusing vll = vector<ll>;\nusing vvll = vector<vll>;\nusing vvvll = vector<vvll>;\nusing vb = vector<bool>;\nusing vvb = vector<vb>;\nusing vvvb = vector<vvb>;\nusing vd = vector<double>;\nusing vvd = vector<vd>;\nusing vvvd = vector<vvd>;\n#define all(A) A.begin(),A.end()\n#define ciN cin\n#define rep(i, n) for (ll i = 0; i < (ll) (n); i++)\nusing pqr = priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>>;\n\ntemplate<class T>\nbool chmax(T& p, T q) {\n if (p < q) {\n p = q;\n return 1;\n }\n else {\n return 0;\n }\n}\ntemplate<class T>\nbool chmin(T& p, T q) {\n if (p > q) {\n p = q;\n return 1;\n }\n else {\n return 0;\n }\n}\nll gcd(ll(a), ll(b)) {\n ll c = a;\n while (a % b != 0) {\n c = a % b;\n a = b;\n b = c;\n }\n return b;\n}\n\n\n#define ss second.second\n#define sf second.first\nusing paii = pair<int, int>;\n//chmin必要\n/*\n有向木の最小全域木を求める.\nO(|V|*|E|)らしい.知らんけど\n*/\ntemplate<class T>\nT Chu_Liu_Edmonds_algorithm(const vector<pair<T, paii>>& E, int v, int root = 0) {//E:{重み,{start,end}} //v:|V| //root:根\n\n vector<pair<T, int>> Vm(v, pair<T, int>(1e9, -1));\n vector<int> gr(v, 0);\n vector<bool> P(v, false), f(v, false);\n int cnt = 0;\n T res = 0;\n vector<pair<T, paii>> nE;\n\n for (auto e : E) {\n chmin(Vm[e.ss], { e.first, e.sf });\n }\n Vm[root] = { -1, -1 };\n\n rep(i, v) {\n if (f[i]) continue;\n vector<int> ch;\n int x = i;\n for (; x != -1 && !f[x];) {\n f[x] = 1;\n ch.push_back(x);\n x = Vm[x].second;\n }\n if (x == -1) {\n rep(j, ch.size()) {\n gr[ch[j]] = cnt;\n cnt++;\n }\n }\n else {\n bool Q = 0;\n for (long long j : ch) {\n gr[j] = cnt;\n if (j == x) {\n P[cnt] = 1;\n Q = 1;\n }\n if (!Q) cnt++;\n }\n if (Q) cnt++;\n }\n }\n if (cnt == v) {\n T ans = 1;\n rep(i, v)ans += Vm[i].first;\n //cout<<ans<<endl;\n return ans;\n }\n\n rep(i, v) {\n if (i != root && P[gr[i]]) res += Vm[i].first;\n }\n\n for (auto e : E) {\n int p = e.ss;\n int s = gr[e.sf];\n int t = gr[e.ss];\n if (s == t) continue;\n else if (P[t]) {\n nE.push_back({ e.first - Vm[p].first, { s, t } });\n }\n else {\n nE.push_back({ e.first, { s, t } });\n }\n }\n //cout<<res<<endl;\n return res + Chu_Liu_Edmonds_algorithm(nE, cnt, gr[root]);\n}\n\n\nint main() {\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n\n ll N, M, A, B, P, Q;\n cin >> N >> M >> A >> B >> P >> Q;\n ll an = M;\n if (A == 1 && B == 1) {\n ll d = (P + Q);\n\n ll D = M / d;\n if (D <= N) {\n chmin(an, abs(M - D * d));\n }\n D++;\n if (D <= N) {\n chmin(an, abs(M - D * d));\n }\n if (N <= 1e18 / d)chmin(an, abs(M - N * d));\n cout << an << endl;\n return 0;\n }\n\n vll D;\n ll a = 1, b = 1;\n while (1||N==0) {\n ll S = P * a + Q * b;\n if (S > 2 * M)break;\n D.push_back(S);\n if (a > 1e18 / A || b > 1e18 / B)break;\n a *= A;\n b *= B;\n N--;\n }\n\n //cout<<D.size()<<endl;\n\n N = D.size();\n\n set<ll> S;\n\n rep(bit, (1 << N / 2)) {\n ll res = 0;\n rep(i, N / 2) {\n if (bit & (1 << i))res += D[i];\n }\n S.insert(res);\n }\n S.insert(2e18);\n ll Z = N - N / 2;\n\n rep(bit, (1 << Z)) {\n ll res = 0;\n rep(i, Z) {\n if (bit & (1 << i))res += D[i + (N / 2)];\n }\n if (res >= M) {\n chmin(an, res - M);\n continue;\n }\n auto re = S.upper_bound(M - res);\n chmin(an, abs(M - res - *re));\n re--;\n chmin(an, abs(M - res - *re));\n }\n\n cout << an << endl;\n\n}", "accuracy": 0.7142857142857143, "time_ms": 260, "memory_kb": 40068, "score_of_the_acc": -0.9686, "final_rank": 10 }, { "submission_id": "aoj_2239_6731186", "code_snippet": "#include <bits/stdc++.h>\n//#include <atcoder/all>\n//using namespace atcoder;\nusing namespace std;\nusing ll = long long;\nusing vll = vector<ll>;\nusing vvll = vector<vll>;\nusing vvvll = vector<vvll>;\nusing vb = vector<bool>;\nusing vvb = vector<vb>;\nusing vvvb = vector<vvb>;\nusing vd = vector<double>;\nusing vvd = vector<vd>;\nusing vvvd = vector<vvd>;\n#define all(A) A.begin(),A.end()\n#define ciN cin\n#define rep(i, n) for (ll i = 0; i < (ll) (n); i++)\nusing pqr = priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>>;\n\ntemplate<class T>\nbool chmax(T& p, T q) {\n if (p < q) {\n p = q;\n return 1;\n }\n else {\n return 0;\n }\n}\ntemplate<class T>\nbool chmin(T& p, T q) {\n if (p > q) {\n p = q;\n return 1;\n }\n else {\n return 0;\n }\n}\nll gcd(ll(a), ll(b)) {\n ll c = a;\n while (a % b != 0) {\n c = a % b;\n a = b;\n b = c;\n }\n return b;\n}\n\n\n#define ss second.second\n#define sf second.first\nusing paii = pair<int, int>;\n//chmin必要\n/*\n有向木の最小全域木を求める.\nO(|V|*|E|)らしい.知らんけど\n*/\ntemplate<class T>\nT Chu_Liu_Edmonds_algorithm(const vector<pair<T, paii>>& E, int v, int root = 0) {//E:{重み,{start,end}} //v:|V| //root:根\n\n vector<pair<T, int>> Vm(v, pair<T, int>(1e9, -1));\n vector<int> gr(v, 0);\n vector<bool> P(v, false), f(v, false);\n int cnt = 0;\n T res = 0;\n vector<pair<T, paii>> nE;\n\n for (auto e : E) {\n chmin(Vm[e.ss], { e.first, e.sf });\n }\n Vm[root] = { -1, -1 };\n\n rep(i, v) {\n if (f[i]) continue;\n vector<int> ch;\n int x = i;\n for (; x != -1 && !f[x];) {\n f[x] = 1;\n ch.push_back(x);\n x = Vm[x].second;\n }\n if (x == -1) {\n rep(j, ch.size()) {\n gr[ch[j]] = cnt;\n cnt++;\n }\n }\n else {\n bool Q = 0;\n for (long long j : ch) {\n gr[j] = cnt;\n if (j == x) {\n P[cnt] = 1;\n Q = 1;\n }\n if (!Q) cnt++;\n }\n if (Q) cnt++;\n }\n }\n if (cnt == v) {\n T ans = 1;\n rep(i, v)ans += Vm[i].first;\n //cout<<ans<<endl;\n return ans;\n }\n\n rep(i, v) {\n if (i != root && P[gr[i]]) res += Vm[i].first;\n }\n\n for (auto e : E) {\n int p = e.ss;\n int s = gr[e.sf];\n int t = gr[e.ss];\n if (s == t) continue;\n else if (P[t]) {\n nE.push_back({ e.first - Vm[p].first, { s, t } });\n }\n else {\n nE.push_back({ e.first, { s, t } });\n }\n }\n //cout<<res<<endl;\n return res + Chu_Liu_Edmonds_algorithm(nE, cnt, gr[root]);\n}\n\n\nint main() {\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n\n ll N, M, A, B, P, Q;\n cin >> N >> M >> A >> B >> P >> Q;\n ll an = M;\n if (A == 1 && B == 1) {\n ll d = (P + Q);\n\n ll D = M / d;\n if (D <= N) {\n chmin(an, abs(M - D * d));\n }\n D++;\n if (D <= N) {\n chmin(an, abs(M - D * d));\n }\n if (N <= 1e18 / d)chmin(an, abs(M - N * d));\n cout << an << endl;\n return 0;\n }\n\n vll D;\n ll a = 1, b = 1;\n while (1) {\n ll S = P * a + Q * b;\n if (S > 2 * M)break;\n D.push_back(S);\n if (a > 1e18 / A || b > 1e18 / B)break;\n a *= A;\n b *= B;\n }\n\n //cout<<D.size()<<endl;\n\n N = D.size();\n\n set<ll> S;\n\n rep(bit, (1 << N / 2)) {\n ll res = 0;\n rep(i, N / 2) {\n if (bit & (1 << i))res += D[i];\n }\n S.insert(res);\n }\n S.insert(2e18);\n ll Z = N - N / 2;\n\n rep(bit, (1 << Z)) {\n ll res = 0;\n rep(i, Z) {\n if (bit & (1 << i))res += D[i + (N / 2)];\n }\n if (res >= M) {\n chmin(an, res - M);\n continue;\n }\n auto re = S.upper_bound(M - res);\n chmin(an, abs(M - res - *re));\n re--;\n chmin(an, abs(M - res - *re));\n }\n\n cout << an << endl;\n\n}", "accuracy": 0.7142857142857143, "time_ms": 270, "memory_kb": 40084, "score_of_the_acc": -0.9817, "final_rank": 11 }, { "submission_id": "aoj_2239_6731176", "code_snippet": "#include <bits/stdc++.h>\n//#include <atcoder/all>\n//using namespace atcoder;\nusing namespace std;\nusing ll = long long;\nusing vll = vector<ll>;\nusing vvll = vector<vll>;\nusing vvvll = vector<vvll>;\nusing vb = vector<bool>;\nusing vvb = vector<vb>;\nusing vvvb = vector<vvb>;\nusing vd = vector<double>;\nusing vvd = vector<vd>;\nusing vvvd = vector<vvd>;\n#define all(A) A.begin(),A.end()\n#define ciN cin\n#define rep(i, n) for (ll i = 0; i < (ll) (n); i++)\nusing pqr = priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>>;\n\ntemplate<class T>\nbool chmax(T& p, T q) {\n if (p < q) {\n p = q;\n return 1;\n }\n else {\n return 0;\n }\n}\ntemplate<class T>\nbool chmin(T& p, T q) {\n if (p > q) {\n p = q;\n return 1;\n }\n else {\n return 0;\n }\n}\nll gcd(ll(a), ll(b)) {\n ll c = a;\n while (a % b != 0) {\n c = a % b;\n a = b;\n b = c;\n }\n return b;\n}\n\n\n#define ss second.second\n#define sf second.first\nusing paii = pair<int, int>;\n//chmin必要\n/*\n有向木の最小全域木を求める.\nO(|V|*|E|)らしい.知らんけど\n*/\ntemplate<class T>\nT Chu_Liu_Edmonds_algorithm(const vector<pair<T, paii>>& E, int v, int root = 0) {//E:{重み,{start,end}} //v:|V| //root:根\n\n vector<pair<T, int>> Vm(v, pair<T, int>(1e9, -1));\n vector<int> gr(v, 0);\n vector<bool> P(v, false), f(v, false);\n int cnt = 0;\n T res = 0;\n vector<pair<T, paii>> nE;\n\n for (auto e : E) {\n chmin(Vm[e.ss], { e.first, e.sf });\n }\n Vm[root] = { -1, -1 };\n\n rep(i, v) {\n if (f[i]) continue;\n vector<int> ch;\n int x = i;\n for (; x != -1 && !f[x];) {\n f[x] = 1;\n ch.push_back(x);\n x = Vm[x].second;\n }\n if (x == -1) {\n rep(j, ch.size()) {\n gr[ch[j]] = cnt;\n cnt++;\n }\n }\n else {\n bool Q = 0;\n for (long long j : ch) {\n gr[j] = cnt;\n if (j == x) {\n P[cnt] = 1;\n Q = 1;\n }\n if (!Q) cnt++;\n }\n if (Q) cnt++;\n }\n }\n if (cnt == v) {\n T ans = 1;\n rep(i, v)ans += Vm[i].first;\n //cout<<ans<<endl;\n return ans;\n }\n\n rep(i, v) {\n if (i != root && P[gr[i]]) res += Vm[i].first;\n }\n\n for (auto e : E) {\n int p = e.ss;\n int s = gr[e.sf];\n int t = gr[e.ss];\n if (s == t) continue;\n else if (P[t]) {\n nE.push_back({e.first - Vm[p].first, { s, t }});\n }\n else {\n nE.push_back({e.first, { s, t }});\n }\n }\n //cout<<res<<endl;\n return res + Chu_Liu_Edmonds_algorithm(nE, cnt, gr[root]);\n}\n\n\nint main() {\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n \n ll N,M,A,B,P,Q;\n cin>>N>>M>>A>>B>>P>>Q;\n ll an=M;\n if(A==1&&B==1){\n ll d=(P+Q);\n\n ll D=M/d;\n if(D<=N){\n chmin(an,abs(M-D*d));\n }\n D++;\n if(D<=N){\n chmin(an,abs(M-D*d));\n }\n if(N<=1e18/d)chmin(an,abs(M-N*d));\n cout<<an<<endl;\n return 0;\n }\n\n vll D;\n ll a=1,b=1;\n while(1){\n ll S=P*a+Q*b;\n if(S>2*M)break;\n D.push_back(S);\n a*=A;\n b*=B;\n }\n\n //cout<<D.size()<<endl;\n\n N=D.size();\n\n set<ll> S;\n\n rep(bit,(1<<N/2)){\n ll res=0;\n rep(i,N/2){\n if(bit&(1<<i))res+=D[i];\n }\n S.insert(res);\n }\n S.insert(2e18);\n ll Z=N-N/2;\n\n rep(bit,(1<<Z)){\n ll res=0;\n rep(i,Z){\n if(bit&(1<<i))res+=D[i+N/2];\n }\n if(res>M){\n chmin(an,res-M);\n continue;\n }\n auto re=S.upper_bound(M-res);\n chmin(an,abs(M-res-*re));\n re--;\n chmin(an,abs(M-res-*re));\n }\n\n cout<<an<<endl;\n\n}", "accuracy": 0.7142857142857143, "time_ms": 260, "memory_kb": 39880, "score_of_the_acc": -0.964, "final_rank": 9 }, { "submission_id": "aoj_2239_6666297", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)\n#define revrep(i, t, s) for (int i = (int)(t)-1; i >= (int)(s); --i)\n#define all(x) begin(x), end(x)\ntemplate <typename T> bool chmax(T& a, const T& b) { return a < b ? (a = b, 1) : 0; }\ntemplate <typename T> bool chmin(T& a, const T& b) { return a > b ? (a = b, 1) : 0; }\n\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << fixed << setprecision(15);\n\n ll n, m, a, b, p, q;\n cin >> n >> m >> a >> b >> p >> q;\n if (a == 1 && b == 1) {\n if (m/(p+q)>=n) {\n cout << m-n*(p+q) << endl;\n } else {\n ll r = m % (p + q);\n cout << min(r, p+q-r) << endl;\n }\n return 0;\n }\n chmin(n, 40LL);\n vector<ll> first;\n rep(S,0,1<<(n/2)) {\n ll s = 0;\n ll pa = p, qb = q;\n bool fa = false, fb = false;\n bool ok = true;\n rep(i,0,n/2) {\n if (S>>i&1) {\n if (fa || fb) {\n ok = false;\n break;\n }\n s += pa+qb;\n }\n if (fa || pa > (ll)1e18/a) {\n fa = true;\n } else {\n pa *= a;\n }\n if (fb || qb > (ll)1e18/b) {\n fb = true;\n } else {\n qb *= b;\n }\n }\n if (ok && s < 1e15) {\n first.push_back(s);\n }\n }\n sort(all(first));\n ll ans = m;\n rep(S,0,1<<(n-n/2)) {\n ll pa = p, qb = q;\n bool fa = false, fb = false;\n rep(_,0,n/2) {\n if (fa || pa > (ll)1e18/a) {\n fa = true;\n } else {\n pa *= a;\n }\n if (fb || qb > (ll)1e18/b) {\n fb = true;\n } else {\n qb *= b;\n }\n }\n ll s = 0;\n bool ok = true;\n rep(i,0,n-n/2) {\n if (S>>i&1) {\n if (fa || fb) {\n ok = false;\n break;\n }\n s += pa+qb;\n }\n if (pa > (ll)1e18/a) {\n fa = true;\n } else {\n pa *= a;\n }\n if (qb > (ll)1e18/b) {\n fb = true;\n } else {\n qb *= b;\n }\n }\n if (ok && s < 1e15) {\n auto it = lower_bound(all(first), m-s);\n if (it != first.end()) chmin(ans, abs(s+*it-m));\n if (it != first.begin()) chmin(ans, abs(s+*--it-m));\n }\n }\n cout << ans << endl;\n}", "accuracy": 1, "time_ms": 830, "memory_kb": 12488, "score_of_the_acc": -1.0126, "final_rank": 6 }, { "submission_id": "aoj_2239_6666279", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)\n#define revrep(i, t, s) for (int i = (int)(t)-1; i >= (int)(s); --i)\n#define all(x) begin(x), end(x)\ntemplate <typename T> bool chmax(T& a, const T& b) { return a < b ? (a = b, 1) : 0; }\ntemplate <typename T> bool chmin(T& a, const T& b) { return a > b ? (a = b, 1) : 0; }\n\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << fixed << setprecision(15);\n\n ll n, m, a, b, p, q;\n cin >> n >> m >> a >> b >> p >> q;\n if (a == 1 && b == 1) {\n if (m/(p+q)>n) {\n cout << m-n*(p+q) << endl;\n } else {\n ll r = m % (p + q);\n cout << min(r, p+q-r) << endl;\n }\n return 0;\n }\n chmin(n, 40LL);\n vector<ll> first;\n rep(S,0,1<<(n/2)) {\n ll s = 0;\n ll pa = p, qb = q;\n bool fa = false, fb = false;\n bool ok = true;\n rep(i,0,n/2) {\n if (S>>i&1) {\n if (fa || fb) {\n ok = false;\n break;\n }\n s += pa+qb;\n }\n if (fa || pa > (ll)1e18/a) {\n fa = true;\n } else {\n pa *= a;\n }\n if (fb || qb > (ll)1e18/b) {\n fb = true;\n } else {\n qb *= b;\n }\n }\n if (ok && s < 1e15) {\n first.push_back(s);\n }\n }\n sort(all(first));\n ll ans = m;\n rep(S,0,1<<(n-n/2)) {\n ll pa = p, qb = q;\n bool fa = false, fb = false;\n rep(_,0,n/2) {\n if (fa || pa > (ll)1e18/a) {\n fa = true;\n } else {\n pa *= a;\n }\n if (fb || qb > (ll)1e18/b) {\n fb = true;\n } else {\n qb *= b;\n }\n }\n ll s = 0;\n bool ok = true;\n rep(i,0,n-n/2) {\n if (S>>i&1) {\n if (fa || fb) {\n ok = false;\n break;\n }\n s += pa+qb;\n }\n if (pa > (ll)1e18/a) {\n fa = true;\n } else {\n pa *= a;\n }\n if (qb > (ll)1e18/b) {\n fb = true;\n } else {\n qb *= b;\n }\n }\n if (ok && s < 1e15) {\n auto it = lower_bound(all(first), m-s);\n if (it != first.end()) chmin(ans, abs(s+*it-m));\n if (it != first.begin()) chmin(ans, abs(s+*--it-m));\n }\n }\n cout << ans << endl;\n}", "accuracy": 0.5428571428571428, "time_ms": 830, "memory_kb": 12504, "score_of_the_acc": -1.013, "final_rank": 13 }, { "submission_id": "aoj_2239_6666267", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)\n#define revrep(i, t, s) for (int i = (int)(t)-1; i >= (int)(s); --i)\n#define all(x) begin(x), end(x)\ntemplate <typename T> bool chmax(T& a, const T& b) { return a < b ? (a = b, 1) : 0; }\ntemplate <typename T> bool chmin(T& a, const T& b) { return a > b ? (a = b, 1) : 0; }\n\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << fixed << setprecision(15);\n\n ll n, m, a, b, p, q;\n cin >> n >> m >> a >> b >> p >> q;\n if (a == 1 && b == 1) {\n if (m/(p+q)>n) {\n cout << m-n*(p+q) << endl;\n } else {\n ll r = m % (p + q);\n cout << min(r, p+q-r) << endl;\n }\n return 0;\n }\n vector<ll> first;\n int L = min(21LL, n);\n rep(S,0,1<<L) {\n ll s = 0;\n ll pa = p, qb = q;\n bool fa = false, fb = false;\n bool ok = true;\n rep(i,0,L) {\n if (S>>i&1) {\n if (fa || fb) {\n ok = false;\n break;\n }\n s += pa+qb;\n }\n if (fa || pa > (ll)1e18/a) {\n fa = true;\n } else {\n pa *= a;\n }\n if (fb || qb > (ll)1e18/b) {\n fb = true;\n } else {\n qb *= b;\n }\n }\n if (ok && s < 1e15) {\n first.push_back(s);\n }\n }\n sort(all(first));\n ll ans = m;\n if (L == n) {\n auto it = lower_bound(all(first), m);\n if (it != first.end()) chmin(ans, abs(*it-m));\n if (it != first.begin()) chmin(ans, abs(*--it-m));\n cout << ans << endl;\n return 0;\n }\n L = min(21, L-21);\n rep(S,0,1<<L) {\n ll pa = p, qb = q;\n bool fa = false, fb = false;\n rep(_,0,21) {\n if (fa || pa > (ll)1e18/a) {\n fa = true;\n } else {\n pa *= a;\n }\n if (fb || qb > (ll)1e18/b) {\n fb = true;\n } else {\n qb *= b;\n }\n }\n ll s = 0;\n bool ok = true;\n rep(i,0,L) {\n if (S>>i&1) {\n if (fa || fb) {\n ok = false;\n break;\n }\n s += pa+qb;\n }\n if (pa > (ll)1e18/a) {\n fa = true;\n } else {\n pa *= a;\n }\n if (qb > (ll)1e18/b) {\n fb = true;\n } else {\n qb *= b;\n }\n }\n if (ok && s < 1e15) {\n auto it = lower_bound(all(first), m-s);\n if (it != first.end()) chmin(ans, abs(s+*it-m));\n if (it != first.begin()) chmin(ans, abs(s+*--it-m));\n }\n }\n cout << ans << endl;\n}", "accuracy": 0.2, "time_ms": 630, "memory_kb": 20564, "score_of_the_acc": -0.9578, "final_rank": 17 }, { "submission_id": "aoj_2239_6666259", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)\n#define revrep(i, t, s) for (int i = (int)(t)-1; i >= (int)(s); --i)\n#define all(x) begin(x), end(x)\ntemplate <typename T> bool chmax(T& a, const T& b) { return a < b ? (a = b, 1) : 0; }\ntemplate <typename T> bool chmin(T& a, const T& b) { return a > b ? (a = b, 1) : 0; }\n\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << fixed << setprecision(15);\n\n ll n, m, a, b, p, q;\n cin >> n >> m >> a >> b >> p >> q;\n if (a == 1 && b == 1) {\n if (m-n*(p+q)>0) {\n cout << m-n*(p+q) << endl;\n } else {\n ll r = m % (p + q);\n cout << min(r, p+q-r) << endl;\n }\n return 0;\n }\n vector<ll> first;\n int L = min(20LL, n);\n rep(S,0,1<<L) {\n ll s = 0;\n ll pa = p, qb = q;\n bool fa = false, fb = false;\n bool ok = true;\n rep(i,0,L) {\n if (S>>i&1) {\n if (fa || fb) {\n ok = false;\n break;\n }\n s += pa+qb;\n }\n if (fa || pa > (ll)1e18/a) {\n fa = true;\n } else {\n pa *= a;\n }\n if (fb || qb > (ll)1e18/b) {\n fb = true;\n } else {\n qb *= b;\n }\n }\n if (ok && s < 1e15) {\n first.push_back(s);\n }\n }\n sort(all(first));\n ll ans = m;\n if (L == n) {\n auto it = lower_bound(all(first), m);\n if (it != first.end()) chmin(ans, abs(*it-m));\n if (it != first.begin()) chmin(ans, abs(*--it-m));\n cout << ans << endl;\n return 0;\n }\n L = min(20, L-20);\n rep(S,0,1<<L) {\n ll pa = p, qb = q;\n bool fa = false, fb = false;\n rep(_,0,L) {\n if (fa || pa > (ll)1e18/a) {\n fa = true;\n } else {\n pa *= a;\n }\n if (fb || qb > (ll)1e18/b) {\n fb = true;\n } else {\n qb *= b;\n }\n }\n ll s = 0;\n bool ok = true;\n rep(i,0,L) {\n if (S>>i&1) {\n if (fa || fb) {\n ok = false;\n break;\n }\n s += pa+qb;\n }\n if (pa > (ll)1e18/a) {\n fa = true;\n } else {\n pa *= a;\n }\n if (qb > (ll)1e18/b) {\n fb = true;\n } else {\n qb *= b;\n }\n }\n if (ok && s < 1e15) {\n auto it = lower_bound(all(first), m-s);\n if (it != first.end()) chmin(ans, abs(s+*it-m));\n if (it != first.begin()) chmin(ans, abs(s+*--it-m));\n }\n }\n cout << ans << endl;\n}", "accuracy": 0.2, "time_ms": 290, "memory_kb": 12628, "score_of_the_acc": -0.3325, "final_rank": 16 }, { "submission_id": "aoj_2239_6666250", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)\n#define revrep(i, t, s) for (int i = (int)(t)-1; i >= (int)(s); --i)\n#define all(x) begin(x), end(x)\ntemplate <typename T> bool chmax(T& a, const T& b) { return a < b ? (a = b, 1) : 0; }\ntemplate <typename T> bool chmin(T& a, const T& b) { return a > b ? (a = b, 1) : 0; }\n\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << fixed << setprecision(15);\n\n ll n, m, a, b, p, q;\n cin >> n >> m >> a >> b >> p >> q;\n if (a == 1 && b == 1) {\n ll r = m % (p + q);\n cout << min(r, p+q-r) << endl;\n return 0;\n }\n vector<ll> first;\n int L = 20;\n rep(S,0,1<<L) {\n ll s = 0;\n ll pa = p, qb = q;\n bool fa = false, fb = false;\n bool ok = true;\n rep(i,0,L) {\n if (S>>i&1) {\n if (fa || fb) {\n ok = false;\n break;\n }\n s += pa+qb;\n }\n if (fa || pa > (ll)1e18/a) {\n fa = true;\n } else {\n pa *= a;\n }\n if (fb || qb > (ll)1e18/b) {\n fb = true;\n } else {\n qb *= b;\n }\n }\n if (ok && s < 1e15) {\n first.push_back(s);\n }\n }\n sort(all(first));\n ll ans = m;\n rep(S,0,1<<L) {\n ll pa = p, qb = q;\n bool fa = false, fb = false;\n rep(_,0,L) {\n if (fa || pa > (ll)1e18/a) {\n fa = true;\n } else {\n pa *= a;\n }\n if (fb || qb > (ll)1e18/b) {\n fb = true;\n } else {\n qb *= b;\n }\n }\n ll s = 0;\n bool ok = true;\n rep(i,0,L) {\n if (S>>i&1) {\n if (fa || fb) {\n ok = false;\n break;\n }\n s += pa+qb;\n }\n if (pa > (ll)1e18/a) {\n fa = true;\n } else {\n pa *= a;\n }\n if (qb > (ll)1e18/b) {\n fb = true;\n } else {\n qb *= b;\n }\n }\n if (ok && s < 1e15) {\n auto it = lower_bound(all(first), m-s);\n if (it != first.end()) chmin(ans, abs(s+*it-m));\n if (it != first.begin()) chmin(ans, abs(s+*--it-m));\n }\n }\n cout << ans << endl;\n}", "accuracy": 0.17142857142857143, "time_ms": 830, "memory_kb": 13012, "score_of_the_acc": -1.0255, "final_rank": 18 }, { "submission_id": "aoj_2239_6666248", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)\n#define revrep(i, t, s) for (int i = (int)(t)-1; i >= (int)(s); --i)\n#define all(x) begin(x), end(x)\ntemplate <typename T> bool chmax(T& a, const T& b) { return a < b ? (a = b, 1) : 0; }\ntemplate <typename T> bool chmin(T& a, const T& b) { return a > b ? (a = b, 1) : 0; }\n\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << fixed << setprecision(15);\n\n ll n, m, a, b, p, q;\n cin >> n >> m >> a >> b >> p >> q;\n if (a == 1 && b == 1) {\n ll r = m % (p + q);\n cout << min(r, m-r) << endl;\n return 0;\n }\n vector<ll> first;\n int L = 20;\n rep(S,0,1<<L) {\n ll s = 0;\n ll pa = p, qb = q;\n bool fa = false, fb = false;\n bool ok = true;\n rep(i,0,L) {\n if (S>>i&1) {\n if (fa || fb) {\n ok = false;\n break;\n }\n s += pa+qb;\n }\n if (fa || pa > (ll)1e18/a) {\n fa = true;\n } else {\n pa *= a;\n }\n if (fb || qb > (ll)1e18/b) {\n fb = true;\n } else {\n qb *= b;\n }\n }\n if (ok && s < 1e15) {\n first.push_back(s);\n }\n }\n sort(all(first));\n ll ans = m;\n rep(S,0,1<<L) {\n ll pa = p, qb = q;\n bool fa = false, fb = false;\n rep(_,0,L) {\n if (fa || pa > (ll)1e18/a) {\n fa = true;\n } else {\n pa *= a;\n }\n if (fb || qb > (ll)1e18/b) {\n fb = true;\n } else {\n qb *= b;\n }\n }\n ll s = 0;\n bool ok = true;\n rep(i,0,L) {\n if (S>>i&1) {\n if (fa || fb) {\n ok = false;\n break;\n }\n s += pa+qb;\n }\n if (pa > (ll)1e18/a) {\n fa = true;\n } else {\n pa *= a;\n }\n if (qb > (ll)1e18/b) {\n fb = true;\n } else {\n qb *= b;\n }\n }\n if (ok && s < 1e15) {\n auto it = lower_bound(all(first), m-s);\n if (it != first.end()) chmin(ans, abs(s+*it-m));\n if (it != first.begin()) chmin(ans, abs(s+*--it-m));\n }\n }\n cout << ans << endl;\n}", "accuracy": 0.11428571428571428, "time_ms": 790, "memory_kb": 12692, "score_of_the_acc": -0.967, "final_rank": 19 }, { "submission_id": "aoj_2239_6666247", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)\n#define revrep(i, t, s) for (int i = (int)(t)-1; i >= (int)(s); --i)\n#define all(x) begin(x), end(x)\ntemplate <typename T> bool chmax(T& a, const T& b) { return a < b ? (a = b, 1) : 0; }\ntemplate <typename T> bool chmin(T& a, const T& b) { return a > b ? (a = b, 1) : 0; }\n\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << fixed << setprecision(15);\n\n ll n, m, a, b, p, q;\n cin >> n >> m >> a >> b >> p >> q;\n if (a == 1 && b == 1) {\n ll r = m % (p + q);\n cout << min(r, m-r) << endl;\n }\n vector<ll> first;\n int L = 20;\n rep(S,0,1<<L) {\n ll s = 0;\n ll pa = p, qb = q;\n bool fa = false, fb = false;\n bool ok = true;\n rep(i,0,L) {\n if (S>>i&1) {\n if (fa || fb) {\n ok = false;\n break;\n }\n s += pa+qb;\n }\n if (fa || pa > (ll)1e18/a) {\n fa = true;\n } else {\n pa *= a;\n }\n if (fb || qb > (ll)1e18/b) {\n fb = true;\n } else {\n qb *= b;\n }\n }\n if (ok && s < 1e15) {\n first.push_back(s);\n }\n }\n sort(all(first));\n ll ans = m;\n rep(S,0,1<<L) {\n ll pa = p, qb = q;\n bool fa = false, fb = false;\n rep(_,0,L) {\n if (fa || pa > (ll)1e18/a) {\n fa = true;\n } else {\n pa *= a;\n }\n if (fb || qb > (ll)1e18/b) {\n fb = true;\n } else {\n qb *= b;\n }\n }\n ll s = 0;\n bool ok = true;\n rep(i,0,L) {\n if (S>>i&1) {\n if (fa || fb) {\n ok = false;\n break;\n }\n s += pa+qb;\n }\n if (pa > (ll)1e18/a) {\n fa = true;\n } else {\n pa *= a;\n }\n if (qb > (ll)1e18/b) {\n fb = true;\n } else {\n qb *= b;\n }\n }\n if (ok && s < 1e15) {\n auto it = lower_bound(all(first), m-s);\n if (it != first.end()) chmin(ans, abs(s+*it-m));\n if (it != first.begin()) chmin(ans, abs(s+*--it-m));\n }\n }\n cout << ans << endl;\n}", "accuracy": 0.11428571428571428, "time_ms": 830, "memory_kb": 12848, "score_of_the_acc": -1.0214, "final_rank": 20 } ]
aoj_2236_cpp
Problem E: Rabbit Plays Games! うさぎがとあるロールプレイングゲームで遊んでいる. 城に入る直前で, 敵に待ち伏せされていた! うさぎが操作する主人公1 人と, n 体の敵との戦闘となった. 各キャラクターには4 つの能力値, 体力 h i , 攻撃力 a i , 防御力 d i , 敏捷 s i が定められている. i = 0 は主人公の情報, 1 ≤ i ≤ n は各敵の情報を表す. 戦闘はターン制である. 各ターン, 生き残っているキャラクターが, 敏捷の値が高い順に攻撃を行う. 敵は必ず主人公を攻撃する. 主人公は敵1 体を攻撃するが, どの敵を攻撃するかは毎ターンごとに主人公が選べる.攻撃力 a のキャラクターが防御力 d のキャラクターに攻撃するとき, max{ a − d , 0} のダメージが与えられる. 受けたダメージの合計が体力の値以上になったキャラクターは直ちに戦闘不能になってしまう. 主人公が戦闘不能になったとき, あるいは敵がすべて戦闘不能になったとき, 戦闘は終了する. Input 1 ≤ n ≤ 40 000 1 ≤ h i , a i , d i , s i ≤ 1 000 000 000 (整数) s i はすべて異なる. Output 主人公が必ず戦闘不能になってしまうとき, −1 を出力せよ. そうでないとき, 主人公が受けるダメージの合計の最小値を一行に出力せよ. Sample Input 1 2 10 3 1 2 2 4 1 3 2 2 1 1 Sample Output 1 4 Sample Input 2 1 1 1 1 1 10000 10000 10000 10000 Sample Output 2 -1
[ { "submission_id": "aoj_2236_10511586", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define all(v) (v).begin(),(v).end()\n#define pb(a) push_back(a)\n#define rep(i, n) for(int i=0;i<n;i++)\n#define foa(e, v) for(auto&& e : v)\nusing ll = long long;\nconst ll MOD7 = 1000000007, MOD998 = 998244353, INF = (1LL << 60);\n#define dout(a) cout<<fixed<<setprecision(10)<<a<<endl;\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n\tll n; cin >> n;\n\tvector<ll> h(n + 1), a(n + 1), d(n + 1), s(n + 1);\n\trep(i, n + 1) {\n\t\tcin >> h[i] >> a[i] >> d[i] >> s[i];\n\t}\n\tvector<ll> numlife;\n\tvector<ll> A;\n\tll sum = 0;\n\tfor(int i = 1; i <= n; i ++) {\n\t\tll x = max(0LL, a[i] - d[0]);\n\t\tif(x == 0LL) {\n\t\t\tcontinue;\n\t\t}\n\t\t\n\t\tif(s[i] > s[0]) sum += x;\n\t\tll at = max(0LL, a[0] - d[i]);\n\t\tif(at == 0LL) {\n\t\t\tcout << -1 << endl;\n\t\t\treturn 0;\n\t\t}\n\t\tA.pb(x);\n\t\tnumlife.pb((h[i] + at - 1) / at);\n\t\t\n\t}\n\tn = numlife.size();\n\t// v, numlife[i], a[i]\n\t\n\tll v = h[0];\n\tvector<int> ord(n);\n\tll suma = accumulate(all(A), 0LL);\n\tiota(all(ord), 0);\n\tsort(all(ord), [&](int i, int j) {\n\t\treturn numlife[i] * A[j] < numlife[j] * A[i]; \n\t});\n\tfoa(i, ord) {\n\t\tsum += numlife[i] * suma - A[i];\n\t\tsuma -= A[i];\n\t\tif(sum >= v) {\n\t\t\tcout << -1 << endl;\n\t\t\treturn 0;\n\t\t}\n\t}\n\tif(sum >= v) {\n\t\tcout << -1 << endl;\n\t} else {\n\t\tcout << sum << endl;\n\t}\n\n\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 5320, "score_of_the_acc": -0.3613, "final_rank": 4 }, { "submission_id": "aoj_2236_10237911", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nstruct Fraction {\n long long a, b;\n};\n\nstruct Rabbit {\n long long hp, attack, defend, s, idx;\n};\n\nbool operator<(Rabbit &p1, Rabbit &p2) {\n if (p1.s > p2.s) return true;\n return false;\n}\n\nbool operator<(const Fraction &p1, const Fraction &p2) {\n if (p1.a * p2.b < p2.a * p1.b) return true;\n return false;\n}\n\nint main() {\n // Step 1. Input\n long long N; cin >> N;\n vector<Rabbit> R(N + 1);\n for (int i = 0; i <= N; i++) {\n cin >> R[i].hp >> R[i].attack >> R[i].defend >> R[i].s;\n R[i].idx = i;\n }\n\n // Step 2. Sorting\n long long damage = 0, Index = 0;\n sort(R.begin(), R.end());\n for (int i = 0; i <= N; i++) {\n if (R[i].idx == 0) Index = i;\n }\n for (int i = 0; i < Index; i++) {\n damage += max(0LL, R[i].attack - R[Index].defend);\n }\n\n // Step 3. Shuffle\n vector<Rabbit> List;\n Rabbit Mine = R[Index];\n for (int i = 1; i <= N; i++) {\n if (R[(Index + i) % (N + 1)].attack <= Mine.defend) continue;\n List.push_back(R[(Index + i) % (N + 1)]);\n }\n \n // Step 4. Decide Order\n vector<pair<Fraction, int>> Frac;\n for (int i = 0; i < List.size(); i++) {\n long long attack = max(0LL, Mine.attack - List[i].defend);\n if (attack == 0LL) {\n cout << \"-1\" << endl;\n return 0;\n }\n long long death = (List[i].hp + attack - 1) / attack;\n Frac.push_back(make_pair(Fraction{List[i].attack - Mine.defend, death}, i));\n }\n sort(Frac.begin(), Frac.end());\n reverse(Frac.begin(), Frac.end());\n\n // Step 5. Calculate Damage\n long long total_turn = -1;\n for (int i = 0; i < Frac.size(); i++) {\n int idx = Frac[i].second;\n total_turn += Frac[i].first.b;\n damage += total_turn * Frac[i].first.a;\n // cerr << idx << \" \" << total_turn << \" \" << damage << endl;\n if (damage >= Mine.hp) {\n cout << \"-1\" << endl;\n return 0;\n }\n }\n\n // Step 6. Output\n cout << (damage >= Mine.hp ? -1 : damage) << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 8276, "score_of_the_acc": -2, "final_rank": 11 }, { "submission_id": "aoj_2236_10110062", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nstruct Enemy {\n long long s, w, r;\n};\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n\n int n;\n cin >> n;\n\n long long h0, a0, d0, s0;\n cin >> h0 >> a0 >> d0 >> s0;\n\n vector<Enemy> damaging;\n bool impossible = false;\n\n for (int i = 0; i < n; i++) {\n long long h_i, a_i, d_i, s_i;\n cin >> h_i >> a_i >> d_i >> s_i;\n\n long long x_i = max(a0 - d_i, 0LL);\n long long w_i = max(a_i - d0, 0LL);\n\n if (x_i == 0) impossible = true;\n\n if (x_i > 0 && w_i > 0) {\n long long r_i = (h_i + x_i - 1) / x_i;\n damaging.push_back({s_i, w_i, r_i});\n }\n }\n\n if (impossible) {\n cout << -1 << \"\\n\";\n return 0;\n }\n\n if (damaging.empty()) {\n cout << 0 << \"\\n\";\n return 0;\n }\n\n sort(damaging.begin(), damaging.end(), [](const Enemy &e1, const Enemy &e2) {\n return (__int128)e1.w * e2.r > (__int128)e2.w * e1.r;\n });\n\n long long totalDamage = 0, partialSum = 0;\n\n for (const auto &e : damaging) {\n partialSum += e.r;\n\n __int128 dmg = (__int128)e.w * (e.s > s0 ? partialSum : partialSum - 1);\n if (dmg >= h0) {\n cout << -1 << \"\\n\";\n return 0;\n }\n\n totalDamage += (long long)dmg;\n if (totalDamage >= h0) {\n cout << -1 << \"\\n\";\n return 0;\n }\n }\n\n cout << totalDamage << \"\\n\";\n return 0;\n}", "accuracy": 0.6875, "time_ms": 10, "memory_kb": 4664, "score_of_the_acc": -0.2195, "final_rank": 15 }, { "submission_id": "aoj_2236_10066813", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\n#define rep(i,n) for(int i=0;i<n;i++)\n#define rep2(i,s,t) for(int i=s;i<t;i++)\n\n\nint main() {\n\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n\n int N;\n cin>>N;\n vector<array<ll,4>> D(N+1);\n vector<array<ll,4>> DD;\n \n rep(i,N+1){\n rep(j,4)cin>>D[i][j];\n if(i!=0&&D[0][1]-D[i][2]<=0){\n if(D[i][1]-D[0][2]<=0)continue;\n else{\n cout<<-1<<endl;\n return 0;\n }\n }\n \n DD.push_back(D[i]);\n }\n swap(DD,D);\n N=D.size()-1;\n\n sort(D.begin()+1,D.end(),[&](auto const& lhs, auto const& rhs) {\n ll L1=max(lhs[1]-D[0][2],0ll);\n ll L2=max(D[0][1]-lhs[2],0ll);\n \n ll R1=max(rhs[1]-D[0][2],0ll);\n ll R2=max(D[0][1]-rhs[2],0ll);\n L2=(lhs[0]+L2-1)/L2;\n R2=(rhs[0]+R2-1)/R2;\n \n return L1*R2>L2*R1;\n });\n // rep(i,N+1){\n // if(i==0)continue;\n // ll L1=max(D[i][1]-D[0][2],0ll);\n // ll L2=max(D[0][1]-D[i][2],0ll);\n // L2=(D[i][0]+L2-1)/L2;\n // cout<<double(L1)/double(L2)<<endl;\n // }\n ll rt=0;\n ll DM=0;\n rep(i,N+1){\n if(i==0)continue;\n ll L1=max(D[i][1]-D[0][2],0ll);\n ll L2=max(D[0][1]-D[i][2],0ll);\n L2=(D[i][0]+L2-1)/L2;\n rt+=L2;\n DM+=(rt-(D[i][3]<D[0][3]))*L1;\n\n if(D[0][0]<=DM){\n cout<<-1<<endl;\n return 0;\n }\n }\n cout<<DM<<endl;\n\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 6240, "score_of_the_acc": -0.8934, "final_rank": 8 }, { "submission_id": "aoj_2236_10066788", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\n#define rep(i,n) for(int i=0;i<n;i++)\n#define rep2(i,s,t) for(int i=s;i<t;i++)\n\n\nint main() {\n\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n\n int N;\n cin>>N;\n vector<array<ll,4>> D(N+1);\n\n rep(i,N+1){\n rep(j,4)cin>>D[i][j];\n if(i!=0&&D[0][1]-D[i][2]<=0){\n cout<<-1<<endl;\n return 0;\n }\n }\n \n\n sort(D.begin()+1,D.end(),[&](auto const& lhs, auto const& rhs) {\n ll L1=max(lhs[1]-D[0][2],0ll);\n ll L2=max(D[0][1]-lhs[2],0ll);\n \n ll R1=max(rhs[1]-D[0][2],0ll);\n ll R2=max(D[0][1]-rhs[2],0ll);\n L2=(lhs[0]+L2-1)/L2;\n R2=(rhs[0]+R2-1)/R2;\n \n return L1*R2>L2*R1;\n });\n // rep(i,N+1){\n // if(i==0)continue;\n // ll L1=max(D[i][1]-D[0][2],0ll);\n // ll L2=max(D[0][1]-D[i][2],0ll);\n // L2=(D[i][0]+L2-1)/L2;\n // cout<<double(L1)/double(L2)<<endl;\n // }\n ll rt=0;\n ll DM=0;\n rep(i,N+1){\n if(i==0)continue;\n ll L1=max(D[i][1]-D[0][2],0ll);\n ll L2=max(D[0][1]-D[i][2],0ll);\n L2=(D[i][0]+L2-1)/L2;\n rt+=L2;\n DM+=(rt-(D[i][3]<D[0][3]))*L1;\n\n if(D[0][0]<=DM){\n cout<<-1<<endl;\n return 0;\n }\n }\n cout<<DM<<endl;\n\n}", "accuracy": 0.6875, "time_ms": 20, "memory_kb": 4232, "score_of_the_acc": -0.4595, "final_rank": 16 }, { "submission_id": "aoj_2236_9761720", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n/**\n * @brief template\n */\n\nint main() {\n int N;\n cin >> N;\n ll PH, PA, PD, PS;\n ll ANS = 0;\n cin >> PH >> PA >> PD >> PS;\n vector<ll> X, Y;\n rep(i,0,N) {\n ll H, A, D, S;\n cin >> H >> A >> D >> S;\n if (A-PD <= 0) continue;\n if (PS > S) ANS -= max(0LL, A-PD);\n X.push_back(max(0LL, A-PD));\n if (PA-D < 0) {\n cout << -1 << endl;\n return 0;\n }\n Y.push_back(ceil(H, PA-D));\n }\n N = X.size();\n vector<int> ord(N);\n iota(ALL(ord),0);\n sort(ALL(ord),[&](int i, int j){return X[i]*Y[j] > X[j]*Y[i];});\n ll Cur = 0;\n for (int i : ord) {\n Cur += Y[i];\n if (Cur > 2000000000 && X[i] >= 1) {\n cout << -1 << endl;\n return 0;\n }\n ANS += Cur * X[i];\n if (ANS >= PH) {\n cout << -1 << endl;\n return 0;\n }\n }\n cout << ANS << endl;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3648, "score_of_the_acc": -0.6667, "final_rank": 7 }, { "submission_id": "aoj_2236_9761698", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n/**\n * @brief template\n */\n\nint main() {\n int N;\n cin >> N;\n ll PH, PA, PD, PS;\n ll ANS = 0;\n cin >> PH >> PA >> PD >> PS;\n vector<ll> X(N), Y(N);\n rep(i,0,N) {\n ll H, A, D, S;\n cin >> H >> A >> D >> S;\n if (PS > S) ANS -= max(0LL, A-PD);\n X[i] = max(0LL, A-PD);\n if (PA-D < 0) {\n cout << -1 << endl;\n return 0;\n }\n Y[i] = ceil(H, PA-D);\n }\n vector<int> ord(N);\n iota(ALL(ord),0);\n sort(ALL(ord),[&](int i, int j){return X[i]*Y[j] > X[j]*Y[i];});\n ll Cur = 0;\n for (int i : ord) {\n Cur += Y[i];\n if (Cur > 1000000000 && X[i] >= 1) {\n cout << -1 << endl;\n return 0;\n }\n ANS += Cur * X[i];\n if (ANS >= PH) {\n cout << -1 << endl;\n return 0;\n }\n }\n cout << ANS << endl;\n}", "accuracy": 0.6875, "time_ms": 30, "memory_kb": 3736, "score_of_the_acc": -0.6857, "final_rank": 17 }, { "submission_id": "aoj_2236_6773161", "code_snippet": "#define LOCAL\n#include <bits/stdc++.h>\nusing namespace std;\n#pragma region Macros\ntypedef long long ll;\ntypedef __int128_t i128;\ntypedef unsigned int uint;\ntypedef unsigned long long ull;\n#define ALL(x) (x).begin(), (x).end()\n\ntemplate <typename T> istream& operator>>(istream& is, vector<T>& v) {\n for (T& x : v) is >> x;\n return is;\n}\ntemplate <typename T> ostream& operator<<(ostream& os, const vector<T>& v) {\n for (size_t i = 0; i < v.size(); i++) {\n os << v[i] << (i + 1 == v.size() ? \"\" : \" \");\n }\n return os;\n}\ntemplate <typename T, typename U> ostream& operator<<(ostream& os, const pair<T, U>& p) {\n os << '(' << p.first << ',' << p.second << ')';\n return os;\n}\ntemplate <typename T, typename U> ostream& operator<<(ostream& os, const map<T, U>& m) {\n os << '{';\n for (auto itr = m.begin(); itr != m.end();) {\n os << '(' << itr->first << ',' << itr->second << ')';\n if (++itr != m.end()) os << ',';\n }\n os << '}';\n return os;\n}\ntemplate <typename T, typename U> ostream& operator<<(ostream& os, const unordered_map<T, U>& m) {\n os << '{';\n for (auto itr = m.begin(); itr != m.end();) {\n os << '(' << itr->first << ',' << itr->second << ')';\n if (++itr != m.end()) os << ',';\n }\n os << '}';\n return os;\n}\ntemplate <typename T> ostream& operator<<(ostream& os, const set<T>& s) {\n os << '{';\n for (auto itr = s.begin(); itr != s.end();) {\n os << *itr;\n if (++itr != s.end()) os << ',';\n }\n os << '}';\n return os;\n}\ntemplate <typename T> ostream& operator<<(ostream& os, const multiset<T>& s) {\n os << '{';\n for (auto itr = s.begin(); itr != s.end();) {\n os << *itr;\n if (++itr != s.end()) os << ',';\n }\n os << '}';\n return os;\n}\ntemplate <typename T> ostream& operator<<(ostream& os, const unordered_set<T>& s) {\n os << '{';\n for (auto itr = s.begin(); itr != s.end();) {\n os << *itr;\n if (++itr != s.end()) os << ',';\n }\n os << '}';\n return os;\n}\ntemplate <typename T> ostream& operator<<(ostream& os, const deque<T>& v) {\n for (size_t i = 0; i < v.size(); i++) {\n os << v[i] << (i + 1 == v.size() ? \"\" : \" \");\n }\n return os;\n}\ntemplate <typename T, size_t N> ostream& operator<<(ostream& os, const array<T, N>& v) {\n for (size_t i = 0; i < N; i++) {\n os << v[i] << (i + 1 == N ? \"\" : \" \");\n }\n return os;\n}\n\ntemplate <int i, typename T> void print_tuple(ostream&, const T&) {}\ntemplate <int i, typename T, typename H, class... Args> void print_tuple(ostream& os, const T& t) {\n if (i) os << ',';\n os << get<i>(t);\n print_tuple<i + 1, T, Args...>(os, t);\n}\ntemplate <typename... Args> ostream& operator<<(ostream& os, const tuple<Args...>& t) {\n os << '{';\n print_tuple<0, tuple<Args...>, Args...>(os, t);\n return os << '}';\n}\n\nvoid debug_out() { cerr << '\\n'; }\ntemplate <class Head, class... Tail> void debug_out(Head&& head, Tail&&... tail) {\n cerr << head;\n if (sizeof...(Tail) > 0) cerr << \", \";\n debug_out(move(tail)...);\n}\n#ifdef LOCAL\n#define debug(...) \\\n cerr << \" \"; \\\n cerr << #__VA_ARGS__ << \" :[\" << __LINE__ << \":\" << __FUNCTION__ << \"]\" << '\\n'; \\\n cerr << \" \"; \\\n debug_out(__VA_ARGS__)\n#else\n#define debug(...) void(0)\n#endif\n\ntemplate <typename T> T gcd(T x, T y) { return y != 0 ? gcd(y, x % y) : x; }\ntemplate <typename T> T lcm(T x, T y) { return x / gcd(x, y) * y; }\n\nint topbit(signed t) { return t == 0 ? -1 : 31 - __builtin_clz(t); }\nint topbit(long long t) { return t == 0 ? -1 : 63 - __builtin_clzll(t); }\nint botbit(signed a) { return a == 0 ? 32 : __builtin_ctz(a); }\nint botbit(long long a) { return a == 0 ? 64 : __builtin_ctzll(a); }\nint popcount(signed t) { return __builtin_popcount(t); }\nint popcount(long long t) { return __builtin_popcountll(t); }\nbool ispow2(int i) { return i && (i & -i) == i; }\nlong long MSK(int n) { return (1LL << n) - 1; }\n\ntemplate <class T> T ceil(T x, T y) {\n assert(y >= 1);\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <class T> T floor(T x, T y) {\n assert(y >= 1);\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\n\ntemplate <class T1, class T2> inline bool chmin(T1& a, T2 b) {\n if (a > b) {\n a = b;\n return true;\n }\n return false;\n}\ntemplate <class T1, class T2> inline bool chmax(T1& a, T2 b) {\n if (a < b) {\n a = b;\n return true;\n }\n return false;\n}\n\ntemplate <typename T> void mkuni(vector<T>& v) {\n sort(v.begin(), v.end());\n v.erase(unique(v.begin(), v.end()), v.end());\n}\ntemplate <typename T> int lwb(const vector<T>& v, const T& x) { return lower_bound(v.begin(), v.end(), x) - v.begin(); }\n#pragma endregion\n\nconst int INF = 1e9;\nconst long long IINF = 1e18;\nconst int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};\nconst char dir[4] = {'D', 'R', 'U', 'L'};\nconst long long MOD = 1000000007;\n// const long long MOD = 998244353;\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n int n;\n cin >> n;\n vector<int> h(n + 1), a(n + 1), d(n + 1), s(n + 1);\n for (int i = 0; i <= n; i++) cin >> h[i] >> a[i] >> d[i] >> s[i];\n\n vector<pair<int, int>> enemy;\n ll H = 0;\n for (int i = 1; i <= n; i++) {\n a[i] -= d[0];\n if (a[i] <= 0) continue;\n if (a[0] <= d[i]) {\n cout << -1 << '\\n';\n return 0;\n }\n if (s[i] > s[0]) H += a[i];\n enemy.emplace_back((h[i] + (a[0] - d[i]) - 1) / (a[0] - d[i]), a[i]);\n }\n if (H >= h[0]) {\n cout << -1 << '\\n';\n return 0;\n }\n\n sort(enemy.begin(), enemy.end(), [](const pair<int, int>& p, const pair<int, int>& q) {\n return 1LL * p.first * q.second < 1LL * p.second * q.first;\n });\n ll cur = -1;\n for (auto& p : enemy) {\n cur += p.first;\n H += cur * p.second;\n if (H >= h[0]) {\n cout << -1 << '\\n';\n return 0;\n }\n }\n\n cout << H << '\\n';\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 4364, "score_of_the_acc": -0.1547, "final_rank": 1 }, { "submission_id": "aoj_2236_6521207", "code_snippet": "#include <bits/stdc++.h>\n#pragma GCC optimize(\"Ofast\")\n#define _GLIBCXX_DEBUG\nusing namespace std;\nusing std::cout;\nusing std::cin;\nusing std::endl;\nusing ll=long long;\nusing ld=long double;\nll ILL=2167167167167167167;\nconst int INF=1050000000;\nconst ll mod=1e9+7;\n#define rep(i,a) for (ll i=0;i<a;i++)\n#define all(p) p.begin(),p.end()\ntemplate<class T> using _pq = priority_queue<T, vector<T>, greater<T>>;\ntemplate<class T> ll LB(vector<T> &v,T a){return lower_bound(v.begin(),v.end(),a)-v.begin();}\ntemplate<class T> ll UB(vector<T> &v,T a){return upper_bound(v.begin(),v.end(),a)-v.begin();}\ntemplate<class T> bool chmin(T &a,const T &b){if(a>b){a=b;return 1;}else return 0;}\ntemplate<class T> bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}else return 0;}\ntemplate<class T> void So(vector<T> &v) {sort(v.begin(),v.end());}\ntemplate<class T> void Sore(vector<T> &v) {sort(v.begin(),v.end(),[](T x,T y){return x>y;});}\nvoid yneos(bool a){if(a) cout<<\"Yes\\n\"; else cout<<\"No\\n\";}\ntemplate<class T> void vec_out(vector<T> &p){for(int i=0;i<(int)(p.size());i++){if(i) cout<<\" \";cout<<p[i];}cout<<\"\\n\";}\n\nll f(ll a,ll b){\n\treturn (a+b-1)/b;\n}\n\nvoid solve();\n// oddloop\nint main() {\n\tsolve();\n}\n\nvoid solve(){\n\tint N;\n\tcin>>N;\n\tll H,A,D,S;\n\tcin>>H>>A>>D>>S;\n\tll F=H;\n\tvector<ll> h(N),a(N),d(N),s(N);\n\trep(i,N){\n\t\tcin>>h[i]>>a[i]>>d[i]>>s[i];\n\t}\n\trep(i,N){\n\t\tif(A-d[i]<=0){\n\t\t\tif(a[i]>D){\n\t\t\t\tcout<<\"-1\\n\";\n\t\t\t\treturn ;\n\t\t\t}\n\t\t\th[i]=INF,a[i]=0,d[i]=0;\n\t\t}\n\t}\n\tvector<int> order(N);\n\trep(i,N) order[i]=i;\n\tsort(all(order),[&](int l,int r){\n\t\treturn \tf(h[l],A-d[l])*max(0ll,a[r]-D)<f(h[r],A-d[r])*max(0ll,a[l]-D);\n\t});\n\tll T=0;\n\trep(i,N){\n\t\tif(H<=0) continue;\n\t\tT+=f(h[order[i]],A-d[order[i]]);\n\t\tll tmp=T;\n\t\tif(S>s[order[i]]) tmp--;\n\t\tH-=max(0ll,a[order[i]]-D)*tmp;\n\t}\n\tif(H<=0) cout<<\"-1\\n\";\n\telse cout<<F-H<<\"\\n\";\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 4456, "score_of_the_acc": -1.1746, "final_rank": 10 }, { "submission_id": "aoj_2236_6521197", "code_snippet": "#include <bits/stdc++.h>\n#pragma GCC optimize(\"Ofast\")\n#define _GLIBCXX_DEBUG\nusing namespace std;\nusing std::cout;\nusing std::cin;\nusing std::endl;\nusing ll=long long;\nusing ld=long double;\nll ILL=2167167167167167167;\nconst int INF=1050000000;\nconst ll mod=1e9+7;\n#define rep(i,a) for (ll i=0;i<a;i++)\n#define all(p) p.begin(),p.end()\ntemplate<class T> using _pq = priority_queue<T, vector<T>, greater<T>>;\ntemplate<class T> ll LB(vector<T> &v,T a){return lower_bound(v.begin(),v.end(),a)-v.begin();}\ntemplate<class T> ll UB(vector<T> &v,T a){return upper_bound(v.begin(),v.end(),a)-v.begin();}\ntemplate<class T> bool chmin(T &a,const T &b){if(a>b){a=b;return 1;}else return 0;}\ntemplate<class T> bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}else return 0;}\ntemplate<class T> void So(vector<T> &v) {sort(v.begin(),v.end());}\ntemplate<class T> void Sore(vector<T> &v) {sort(v.begin(),v.end(),[](T x,T y){return x>y;});}\nvoid yneos(bool a){if(a) cout<<\"Yes\\n\"; else cout<<\"No\\n\";}\ntemplate<class T> void vec_out(vector<T> &p){for(int i=0;i<(int)(p.size());i++){if(i) cout<<\" \";cout<<p[i];}cout<<\"\\n\";}\n\nll f(ll a,ll b){\n\treturn (a+b-1)/b;\n}\n\nvoid solve();\n// oddloop\nint main() {\n\tsolve();\n}\n\nvoid solve(){\n\tint N;\n\tcin>>N;\n\tll H,A,D,S;\n\tcin>>H>>A>>D>>S;\n\tll F=H;\n\tvector<ll> h(N),a(N),d(N),s(N);\n\trep(i,N){\n\t\tcin>>h[i]>>a[i]>>d[i]>>s[i];\n\t}\n\trep(i,N){\n\t\tif(A-d[i]<=0){\n\t\t\tif(a[i]>D){\n\t\t\t\tcout<<\"-1\\n\";\n\t\t\t\treturn ;\n\t\t\t}\n\t\t\th[i]=0,a[i]=0,d[i]=0;\n\t\t}\n\t}\n\tvector<int> order(N);\n\trep(i,N) order[i]=i;\n\tsort(all(order),[&](int l,int r){\n\t\treturn \tf(h[l],A-d[l])*max(0ll,a[r]-D)<f(h[r],A-d[r])*max(0ll,a[l]-D);\n\t});\n\tll T=0;\n\trep(i,N){\n\t\tif(H<=0) continue;\n\t\tT+=f(h[order[i]],A-d[order[i]]);\n\t\tll tmp=T;\n\t\tif(S>s[order[i]]) tmp--;\n\t\tH-=max(0ll,a[order[i]]-D)*tmp;\n\t}\n\tif(H<=0) cout<<\"-1\\n\";\n\telse cout<<F-H<<\"\\n\";\n}", "accuracy": 0.9583333333333334, "time_ms": 40, "memory_kb": 4400, "score_of_the_acc": -1.1625, "final_rank": 13 }, { "submission_id": "aoj_2236_6439140", "code_snippet": "#line 1 \"a.cpp\"\n#define PROBLEM \"\"\n#line 2 \"/home/kuhaku/atcoder/github/atcoder-lib/lib/template/atcoder.hpp\"\n#pragma GCC target(\"avx\")\n#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"unroll-loops\")\n#line 2 \"/home/kuhaku/atcoder/github/atcoder-lib/lib/template/template.hpp\"\n#include <bits/stdc++.h>\nusing namespace std;\ntemplate <class T, class U>\nbool chmax(T &a, const U &b) {\n return a < b ? a = b, true : false;\n}\ntemplate <class T, class U>\nbool chmin(T &a, const U &b) {\n return b < a ? a = b, true : false;\n}\nconstexpr int64_t INF = 1000000000000000003;\nconstexpr int Inf = 1000000003;\nconstexpr int MOD = 1000000007;\nconstexpr int MOD_N = 998244353;\nconstexpr double EPS = 1e-7;\nconst double PI = acos(-1.0);\n#line 6 \"/home/kuhaku/atcoder/github/atcoder-lib/lib/template/atcoder.hpp\"\nusing ll = int64_t;\nusing ld = long double;\n#define FOR(i, m, n) for(int i = (m); i < int(n); ++i)\n#define FORR(i, m, n) for(int i = (m)-1; i >= int(n); --i)\n#define FORL(i, m, n) for(int64_t i = (m); i < int64_t(n); ++i)\n#define rep(i, n) FOR(i, 0, n)\n#define repn(i, n) FOR(i, 1, n+1)\n#define repr(i, n) FORR(i, n, 0)\n#define repnr(i, n) FORR(i, n+1, 1)\n#define all(s) (s).begin(), (s).end()\ntemplate<class T, class U>\nstd::istream &operator>>(std::istream &is, std::pair<T, U> &p) { is >> p.first >> p.second; return is; }\ntemplate <class T>\nstd::istream &operator>>(std::istream &is, std::vector<T> &v) { for (T &i : v) is>>i; return is; }\ntemplate <class T, class U>\nstd::ostream &operator<<(std::ostream &os, const std::pair<T, U> &p) {\n return os<<'('<<p.first<< ','<<p.second<<')';\n}\ntemplate <class T>\nstd::ostream &operator<<(std::ostream &os, const std::vector<T> &v) {\n for (auto it=v.begin(); it!=v.end(); ++it) { os<<(it==v.begin()?\"\":\" \")<<*it; } return os;\n}\ntemplate <class Head, class... Tail>\nvoid co(Head&& head, Tail&&... tail) {\n if constexpr(sizeof...(tail)==0) std::cout<<head<<'\\n'; else std::cout<<head<<' ',co(forward<Tail>(tail)...);\n}\ntemplate <class Head, class... Tail>\nvoid ce(Head&& head, Tail&&... tail) {\n if constexpr(sizeof...(tail)==0) std::cerr<<head<<'\\n'; else std::cerr<<head<<' ',ce(forward<Tail>(tail)...);\n}\ntemplate<typename T, typename... Args>\nauto make_vector(T x, int arg, Args ...args) {\n if constexpr(sizeof...(args)==0) return std::vector<T>(arg,x); else return std::vector(arg,make_vector<T>(x,args...));\n}\nvoid sonic() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); }\nvoid setp(const int n) { std::cout<<std::fixed<<std::setprecision(n); }\nvoid Yes(bool is_correct) { std::cout<<(is_correct?\"Yes\":\"No\")<<std::endl; }\nvoid YES(bool is_correct) { std::cout<<(is_correct?\"YES\":\"NO\")<<std::endl; }\n#line 3 \"a.cpp\"\n\nstruct S {\n ll h, a, s, d;\n};\n\nint main(void) {\n sonic();\n int n;\n cin >> n;\n ll h, a, s, d;\n cin >> h >> a >> d >> s;\n vector<S> v(n);\n rep(i, n) cin >> v[i].h >> v[i].a >> v[i].d >> v[i].s;\n ll ans = 0;\n rep(i, n) {\n if (v[i].d >= a && d < v[i].a) {\n co(-1);\n return 0;\n }\n if (v[i].s > s)\n ans += max(v[i].a - d, 0L);\n }\n\n sort(all(v), [&](auto x, auto y) {\n return max(x.a - d, 0L) * ((y.h - 1) / max(a - y.d, 1L) + 1) >\n max(y.a - d, 0L) * ((x.h - 1) / max(a - x.d, 1L) + 1);\n });\n\n ll sum = 0;\n rep(i, n) sum += max(v[i].a - d, 0L);\n\n rep(i, n) {\n ans += sum * ((v[i].h - 1) / max(a - v[i].d, 1L) + 1) - max(v[i].a - d, 0L);\n sum -= max(v[i].a - d, 0L);\n if (ans > h)\n break;\n }\n\n if (ans >= h)\n ans = -1;\n co(ans);\n\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 4260, "score_of_the_acc": -0.4656, "final_rank": 6 }, { "submission_id": "aoj_2236_6439130", "code_snippet": "#line 1 \"a.cpp\"\n#define PROBLEM \"\"\n#line 2 \"/home/kuhaku/atcoder/github/atcoder-lib/lib/template/atcoder.hpp\"\n#pragma GCC target(\"avx\")\n#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"unroll-loops\")\n#line 2 \"/home/kuhaku/atcoder/github/atcoder-lib/lib/template/template.hpp\"\n#include <bits/stdc++.h>\nusing namespace std;\ntemplate <class T, class U>\nbool chmax(T &a, const U &b) {\n return a < b ? a = b, true : false;\n}\ntemplate <class T, class U>\nbool chmin(T &a, const U &b) {\n return b < a ? a = b, true : false;\n}\nconstexpr int64_t INF = 1000000000000000003;\nconstexpr int Inf = 1000000003;\nconstexpr int MOD = 1000000007;\nconstexpr int MOD_N = 998244353;\nconstexpr double EPS = 1e-7;\nconst double PI = acos(-1.0);\n#line 6 \"/home/kuhaku/atcoder/github/atcoder-lib/lib/template/atcoder.hpp\"\nusing ll = int64_t;\nusing ld = long double;\n#define FOR(i, m, n) for(int i = (m); i < int(n); ++i)\n#define FORR(i, m, n) for(int i = (m)-1; i >= int(n); --i)\n#define FORL(i, m, n) for(int64_t i = (m); i < int64_t(n); ++i)\n#define rep(i, n) FOR(i, 0, n)\n#define repn(i, n) FOR(i, 1, n+1)\n#define repr(i, n) FORR(i, n, 0)\n#define repnr(i, n) FORR(i, n+1, 1)\n#define all(s) (s).begin(), (s).end()\ntemplate<class T, class U>\nstd::istream &operator>>(std::istream &is, std::pair<T, U> &p) { is >> p.first >> p.second; return is; }\ntemplate <class T>\nstd::istream &operator>>(std::istream &is, std::vector<T> &v) { for (T &i : v) is>>i; return is; }\ntemplate <class T, class U>\nstd::ostream &operator<<(std::ostream &os, const std::pair<T, U> &p) {\n return os<<'('<<p.first<< ','<<p.second<<')';\n}\ntemplate <class T>\nstd::ostream &operator<<(std::ostream &os, const std::vector<T> &v) {\n for (auto it=v.begin(); it!=v.end(); ++it) { os<<(it==v.begin()?\"\":\" \")<<*it; } return os;\n}\ntemplate <class Head, class... Tail>\nvoid co(Head&& head, Tail&&... tail) {\n if constexpr(sizeof...(tail)==0) std::cout<<head<<'\\n'; else std::cout<<head<<' ',co(forward<Tail>(tail)...);\n}\ntemplate <class Head, class... Tail>\nvoid ce(Head&& head, Tail&&... tail) {\n if constexpr(sizeof...(tail)==0) std::cerr<<head<<'\\n'; else std::cerr<<head<<' ',ce(forward<Tail>(tail)...);\n}\ntemplate<typename T, typename... Args>\nauto make_vector(T x, int arg, Args ...args) {\n if constexpr(sizeof...(args)==0) return std::vector<T>(arg,x); else return std::vector(arg,make_vector<T>(x,args...));\n}\nvoid sonic() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); }\nvoid setp(const int n) { std::cout<<std::fixed<<std::setprecision(n); }\nvoid Yes(bool is_correct) { std::cout<<(is_correct?\"Yes\":\"No\")<<std::endl; }\nvoid YES(bool is_correct) { std::cout<<(is_correct?\"YES\":\"NO\")<<std::endl; }\n#line 3 \"a.cpp\"\n\nstruct S {\n ll h, a, s, d;\n};\n\nint main(void) {\n sonic();\n int n;\n cin >> n;\n ll h, a, s, d;\n cin >> h >> a >> d >> s;\n vector<S> v(n);\n rep(i, n) cin >> v[i].h >> v[i].a >> v[i].d >> v[i].s;\n ll ans = 0;\n rep(i, n) {\n if (v[i].d >= a) {\n co(-1);\n return 0;\n }\n if (v[i].s > s)\n ans += max(v[i].a - d, 0L);\n }\n\n sort(all(v), [&](auto x, auto y) {\n return max((x.a - d), 0L) * ((y.h - 1) / (a - y.d) + 1) >\n max((y.a - d), 0L) * ((x.h - 1) / (a - x.d) + 1);\n });\n\n ll sum = 0;\n rep(i, n) sum += max(v[i].a - d, 0L);\n\n rep(i, n) {\n ans += sum * ((v[i].h - 1) / (a - v[i].d) + 1) - max(v[i].a - d, 0L);\n sum -= max(v[i].a - d, 0L);\n if (ans > h)\n break;\n }\n\n if (ans >= h)\n ans = -1;\n co(ans);\n\n return 0;\n}", "accuracy": 0.6875, "time_ms": 10, "memory_kb": 4264, "score_of_the_acc": -0.1331, "final_rank": 14 }, { "submission_id": "aoj_2236_6439120", "code_snippet": "#line 1 \"a.cpp\"\n#define PROBLEM \"\"\n#line 2 \"/home/kuhaku/atcoder/github/atcoder-lib/lib/template/atcoder.hpp\"\n#pragma GCC target(\"avx\")\n#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"unroll-loops\")\n#line 2 \"/home/kuhaku/atcoder/github/atcoder-lib/lib/template/template.hpp\"\n#include <bits/stdc++.h>\nusing namespace std;\ntemplate <class T, class U>\nbool chmax(T &a, const U &b) {\n return a < b ? a = b, true : false;\n}\ntemplate <class T, class U>\nbool chmin(T &a, const U &b) {\n return b < a ? a = b, true : false;\n}\nconstexpr int64_t INF = 1000000000000000003;\nconstexpr int Inf = 1000000003;\nconstexpr int MOD = 1000000007;\nconstexpr int MOD_N = 998244353;\nconstexpr double EPS = 1e-7;\nconst double PI = acos(-1.0);\n#line 6 \"/home/kuhaku/atcoder/github/atcoder-lib/lib/template/atcoder.hpp\"\nusing ll = int64_t;\nusing ld = long double;\n#define FOR(i, m, n) for(int i = (m); i < int(n); ++i)\n#define FORR(i, m, n) for(int i = (m)-1; i >= int(n); --i)\n#define FORL(i, m, n) for(int64_t i = (m); i < int64_t(n); ++i)\n#define rep(i, n) FOR(i, 0, n)\n#define repn(i, n) FOR(i, 1, n+1)\n#define repr(i, n) FORR(i, n, 0)\n#define repnr(i, n) FORR(i, n+1, 1)\n#define all(s) (s).begin(), (s).end()\ntemplate<class T, class U>\nstd::istream &operator>>(std::istream &is, std::pair<T, U> &p) { is >> p.first >> p.second; return is; }\ntemplate <class T>\nstd::istream &operator>>(std::istream &is, std::vector<T> &v) { for (T &i : v) is>>i; return is; }\ntemplate <class T, class U>\nstd::ostream &operator<<(std::ostream &os, const std::pair<T, U> &p) {\n return os<<'('<<p.first<< ','<<p.second<<')';\n}\ntemplate <class T>\nstd::ostream &operator<<(std::ostream &os, const std::vector<T> &v) {\n for (auto it=v.begin(); it!=v.end(); ++it) { os<<(it==v.begin()?\"\":\" \")<<*it; } return os;\n}\ntemplate <class Head, class... Tail>\nvoid co(Head&& head, Tail&&... tail) {\n if constexpr(sizeof...(tail)==0) std::cout<<head<<'\\n'; else std::cout<<head<<' ',co(forward<Tail>(tail)...);\n}\ntemplate <class Head, class... Tail>\nvoid ce(Head&& head, Tail&&... tail) {\n if constexpr(sizeof...(tail)==0) std::cerr<<head<<'\\n'; else std::cerr<<head<<' ',ce(forward<Tail>(tail)...);\n}\ntemplate<typename T, typename... Args>\nauto make_vector(T x, int arg, Args ...args) {\n if constexpr(sizeof...(args)==0) return std::vector<T>(arg,x); else return std::vector(arg,make_vector<T>(x,args...));\n}\nvoid sonic() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); }\nvoid setp(const int n) { std::cout<<std::fixed<<std::setprecision(n); }\nvoid Yes(bool is_correct) { std::cout<<(is_correct?\"Yes\":\"No\")<<std::endl; }\nvoid YES(bool is_correct) { std::cout<<(is_correct?\"YES\":\"NO\")<<std::endl; }\n#line 3 \"a.cpp\"\n\nstruct S {\n ll h, a, s, d;\n};\n\nint main(void) {\n sonic();\n int n;\n cin >> n;\n ll h, a, s, d;\n cin >> h >> a >> d >> s;\n vector<S> v(n);\n rep(i, n) cin >> v[i].h >> v[i].a >> v[i].d >> v[i].s;\n ll ans = 0;\n rep(i, n) {\n if (v[i].d >= a) {\n co(-1);\n return 0;\n }\n if (v[i].s > s)\n ans += v[i].a - d;\n }\n\n sort(all(v), [&](auto x, auto y) {\n return (x.a - d) * ((y.h - 1) / (a - y.d) + 1) > (y.a - d) * ((x.h - 1) / (a - x.d) + 1);\n });\n\n ll sum = 0;\n rep(i, n) sum += v[i].a - d;\n\n rep(i, n) {\n ans += sum * ((v[i].h - 1) / (a - v[i].d) + 1) - v[i].a + d;\n sum -= v[i].a - d;\n if (ans > h)\n break;\n }\n\n if (ans >= h)\n ans = -1;\n co(ans);\n\n return 0;\n}", "accuracy": 0.6666666666666666, "time_ms": 20, "memory_kb": 4260, "score_of_the_acc": -0.4656, "final_rank": 20 }, { "submission_id": "aoj_2236_6023611", "code_snippet": "#pragma GCC optimize(\"Ofast\")\n#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <map>\n#include <queue>\n#include <cstdio>\n#include <ctime>\n#include <assert.h>\n#include <chrono>\n#include <random>\n#include <numeric>\n#include <set>\n#include <deque>\n#include <stack>\n#include <sstream>\n#include <utility>\n#include <cstring>\n#include <unordered_map>\n#include <unordered_set>\n#include <tuple>\n#include <array>\n#include <bitset>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned long long ull;\n\nmt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());\nll myRand(ll B) {\n return (ull)rng() % B;\n}\ninline double time() {\n return static_cast<double>(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count()) * 1e-9;\n}\n\nint main(){\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n int n; cin >> n;\n n++;\n vector<ll> h(n),a(n),d(n),s(n);\n for(int i=0;i<n;i++){\n cin >> h[i] >> a[i] >> d[i] >> s[i];\n }\n ll sum = 0;\n // プレイヤーを常に先手として考えられる(先のダメージを前計算)\n // {かかる時間,食らうダメージ}\n vector<pair<ll,ll>> v;\n for(int i=1;i<n;i++){\n if(a[i]-d[0] <= 0)continue; // 無視してOK\n if(a[0]-d[i] <= 0 and h[i] > 0){ // ダメージを与えられない\n cout << -1 << endl;\n return 0;\n }\n if(s[i] > s[0]){\n sum += (a[i]-d[0]);\n }\n v.push_back({(h[i]+(a[0]-d[i])-1)/(a[0]-d[i]), a[i]-d[0]});\n }\n if(sum >= h[0]){\n cout << -1 << endl;\n return 0;\n }\n sort(v.begin(), v.end(), [&](auto i,auto j){\n return i.first * j.second < i.second * j.first;\n });\n ll uo = 0;\n for(auto p:v){\n uo += p.second;\n }\n for(auto p:v){\n sum += (p.first-1) * uo;\n uo -= p.second;\n sum += uo;\n if(sum >= h[0]){\n cout << -1 << endl;\n return 0;\n }\n }\n cout << sum << endl;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 5320, "score_of_the_acc": -0.3613, "final_rank": 4 }, { "submission_id": "aoj_2236_6014904", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)\n\nstruct Character {\n ll h, a, d, s, i;\n Character(int h, int a, int d, int s, int i) : h(h), a(a), d(d), s(s), i(i) {}\n};\n\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << fixed << setprecision(15);\n\n int n;\n cin >> n;\n ll h, a, d, s;\n cin >> h >> a >> d >> s;\n ll H = h;\n Character me(h, a, d, s, -1);\n ll fast = 0, slow = 0;\n vector<Character> enemies;\n bool ok = true;\n rep(i,0,n) {\n cin >> h >> a >> d >> s;\n if (d >= me.a) {\n if (me.d < a) ok = false;\n continue;\n }\n enemies.emplace_back(h, a, d, s, i);\n if (s > me.s) {\n fast += max(0LL, a - me.d);\n } else {\n slow += max(0LL, a - me.d);\n }\n }\n if (!ok) {\n cout << -1 << endl;\n return 0;\n }\n sort(enemies.begin(), enemies.end(), [&](auto& p, auto& q) {\n ll dp = me.a - p.d;\n ll ap = max(p.a - me.d, 0LL);\n ll tp = (p.h+dp-1)/dp;\n ll dq = me.a - q.d;\n ll aq = max(q.a - me.d, 0LL);\n ll tq = (q.h+dq-1)/dq;\n return tp*aq < tq*ap;\n });\n\n for (auto& e : enemies) {\n ll d = me.a - e.d;\n ll a = max(e.a - me.d, 0LL);\n ll t = (e.h+d-1)/d;\n me.h -= t*(fast+slow);\n if (me.s > e.s) {\n me.h += a;\n slow -= a;\n } else {\n fast -= a;\n }\n if (me.h <= 0) break;\n }\n cout << (me.h <= 0 ? -1 : H - me.h) << endl;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 7072, "score_of_the_acc": -1.0732, "final_rank": 9 }, { "submission_id": "aoj_2236_6014896", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)\n\nstruct Character {\n ll h, a, d, s, i;\n Character(int h, int a, int d, int s, int i) : h(h), a(a), d(d), s(s), i(i) {}\n};\n\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << fixed << setprecision(15);\n\n int n;\n cin >> n;\n ll h, a, d, s;\n cin >> h >> a >> d >> s;\n ll H = h;\n Character me(h, a, d, s, -1);\n ll fast = 0, slow = 0;\n vector<Character> enemies;\n bool ok = true;\n rep(i,0,n) {\n cin >> h >> a >> d >> s;\n enemies.emplace_back(h, a, d, s, i);\n if (s > me.s) {\n fast += max(0LL, a - me.d);\n } else {\n slow += max(0LL, a - me.d);\n }\n if (d >= me.a) ok = false;\n }\n if (!ok) {\n cout << -1 << endl;\n return 0;\n }\n sort(enemies.begin(), enemies.end(), [&](auto& p, auto& q) {\n ll dp = me.a - p.d;\n ll ap = max(p.a - me.d, 0LL);\n ll tp = (p.h+dp-1)/dp;\n ll dq = me.a - q.d;\n ll aq = max(q.a - me.d, 0LL);\n ll tq = (q.h+dq-1)/dq;\n return tp*aq < tq*ap;\n });\n\n for (auto& e : enemies) {\n ll d = me.a - e.d;\n ll a = max(e.a - me.d, 0LL);\n ll t = (e.h+d-1)/d;\n me.h -= t*(fast+slow);\n if (me.s > e.s) {\n me.h += a;\n slow -= a;\n } else {\n fast -= a;\n }\n if (me.h <= 0) break;\n }\n cout << (me.h <= 0 ? -1 : H - me.h) << endl;\n}", "accuracy": 0.6875, "time_ms": 20, "memory_kb": 6840, "score_of_the_acc": -1.023, "final_rank": 19 }, { "submission_id": "aoj_2236_6014892", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)\n\nstruct Character {\n ll h, a, d, s, i;\n Character(int h, int a, int d, int s, int i) : h(h), a(a), d(d), s(s), i(i) {}\n};\n\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << fixed << setprecision(15);\n\n int n;\n cin >> n;\n ll h, a, d, s;\n cin >> h >> a >> d >> s;\n ll H = h;\n Character me(h, a, d, s, -1);\n ll fast = 0, slow = 0;\n vector<Character> enemies;\n bool ok = true;\n rep(i,0,n) {\n cin >> h >> a >> d >> s;\n enemies.emplace_back(h, a, d, s, i);\n if (s > me.s) {\n fast += max(0LL, a - me.d);\n } else {\n slow += max(0LL, a - me.d);\n }\n if (d >= me.a) ok = false;\n }\n if (!ok) {\n cout << -1 << endl;\n return 0;\n }\n sort(enemies.begin(), enemies.end(), [&](auto& p, auto& q) {\n int dp = me.a - p.d;\n int ap = max(p.a - me.d, 0LL);\n int tp = (p.h+dp-1)/dp;\n int dq = me.a - q.d;\n int aq = max(q.a - me.d, 0LL);\n int tq = (q.h+dq-1)/dq;\n return tp*aq < tq*ap;\n });\n\n for (auto& e : enemies) {\n ll d = me.a - e.d;\n ll a = max(e.a - me.d, 0LL);\n ll t = (e.h+d-1)/d;\n me.h -= t*(fast+slow);\n if (me.s > e.s) {\n me.h += a;\n slow -= a;\n } else {\n fast -= a;\n }\n if (me.h <= 0) break;\n }\n cout << (me.h <= 0 ? -1 : H - me.h) << endl;\n}", "accuracy": 0.6875, "time_ms": 20, "memory_kb": 6808, "score_of_the_acc": -1.0161, "final_rank": 18 }, { "submission_id": "aoj_2236_5998328", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\nusing PII = pair<ll, ll>;\n\n#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)\n#define REP(i, n) FOR(i, 0, n)\n#define ALL(x) x.begin(), x.end()\n#define POPCOUNT(x) __builtin_popcount(x)\ntemplate <typename T> void chmin(T &a, const T &b) { a = min(a, b); }\ntemplate <typename T> void chmax(T &a, const T &b) { a = max(a, b); }\n\nconst ll INF = 1LL << 60;\n\nstruct FastIO {\n FastIO() {\n cin.tie(0);\n ios::sync_with_stdio(0);\n }\n} fastiofastio;\n\nconst ll MOD = 1e9 + 7;\n\n// BEGIN CUT\nll modpow(ll x, ll y, ll m) {\n ll a = 1, p = x;\n while (y > 0) {\n if (y % 2 == 0) {\n p = (p * p) % m;\n y /= 2;\n } else {\n a = (a * p) % m;\n y--;\n }\n }\n return a;\n}\n// END CUT\n\nll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }\nll lcm(ll a, ll b) { return a / gcd(a, b) * b; }\n\nint main() {\n int n;\n cin >> n;\n vector<ll> h(n + 1), a(n + 1), d(n + 1), s(n + 1);\n for (int i = 0; i <= n; i++) {\n cin >> h[i] >> a[i] >> d[i] >> s[i];\n }\n vector<PII> vec;\n ll ans = 0;\n ll tot = 0;\n for (int i = 1; i <= n; i++) {\n ll A_hero = max(a[0] - d[i], 0LL);\n ll A_enemy = max(a[i] - d[0], 0LL);\n if (A_hero == 0) {\n if (A_enemy > 0) {\n cout << -1 << endl;\n return 0;\n }\n continue;\n }\n tot += A_enemy;\n ll T = (h[i] + A_hero - 1) / A_hero;\n if (s[i] < s[0])\n ans -= A_enemy;\n vec.emplace_back(A_enemy, T);\n }\n sort(ALL(vec), [](PII x, PII y) {\n auto [ax, tx] = x;\n auto [ay, ty] = y;\n return ay * tx < ax * ty;\n });\n for (auto [a, t] : vec) {\n ans += tot * t;\n tot -= a;\n if (h[0] <= ans) {\n cout << -1 << endl;\n return 0;\n }\n }\n cout << ans << endl;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 5224, "score_of_the_acc": -0.3405, "final_rank": 3 }, { "submission_id": "aoj_2236_5885435", "code_snippet": "#include <bits/stdc++.h>\n#define rep(i,n) for(int i = 0; i < (n); i++)\nusing namespace std;\ntypedef long long ll;\n\nint main(){\n cin.tie(0);\n ios::sync_with_stdio(0);\n \n typedef struct {\n ll h,a,d,s;\n ll atk, times;\n } Character;\n\n int n; cin >> n;\n Character p;\n vector< Character > e(n);\n cin >> p.h >> p.a >> p.d >> p.s;\n rep(i,n) cin >> e[i].h >> e[i].a >> e[i].d >> e[i].s;\n rep(i,n){\n ll dmg = max(p.a - e[i].d, 0LL);\n if(dmg == 0 && e[i].a > p.d){ cout << -1 << endl; return 0; }\n if(dmg > 0) e[i].times = (e[i].h + dmg - 1) / dmg; else e[i].times = 1e9;\n e[i].atk = max(e[i].a - p.d, 0LL);\n }\n\n sort(e.begin(), e.end(), [&](Character a, Character b) {\n return a.atk * b.times > a.times * b.atk;\n });\n\n ll t = 0, ans = 0;\n rep(i,n){\n if(e[i].atk == 0) continue;\n ans += (t + e[i].times - (ll)(e[i].s < p.s)) * e[i].atk;\n if(ans >= p.h){\n cout << -1 << endl;\n return 0;\n }\n t += e[i].times;\n }\n\n cout << ans << endl;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 4916, "score_of_the_acc": -0.274, "final_rank": 2 }, { "submission_id": "aoj_2236_5885430", "code_snippet": "#include <bits/stdc++.h>\n#define rep(i,n) for(int i = 0; i < (n); i++)\nusing namespace std;\ntypedef long long ll;\n\nint main(){\n cin.tie(0);\n ios::sync_with_stdio(0);\n \n typedef struct {\n ll h,a,d,s;\n ll atk, times;\n } Character;\n\n int n; cin >> n;\n Character p;\n vector< Character > e(n);\n cin >> p.h >> p.a >> p.d >> p.s;\n rep(i,n) cin >> e[i].h >> e[i].a >> e[i].d >> e[i].s;\n rep(i,n){\n ll dmg = max(p.a - e[i].d, 0LL);\n if(dmg == 0 && e[i].a > p.d){ cout << -1 << endl; return 0; }\n if(dmg > 0) e[i].times = (e[i].h + dmg - 1) / dmg;\n e[i].atk = max(e[i].a - p.d, 0LL);\n }\n\n sort(e.begin(), e.end(), [&](Character a, Character b) {\n return a.atk * b.times > a.times * b.atk;\n });\n\n ll t = 0, ans = 0;\n rep(i,n){\n if(e[i].atk == 0) continue;\n ans += (t + e[i].times - (e[i].s < p.s)) * e[i].atk;\n if(ans >= p.h){\n cout << -1 << endl;\n return 0;\n }\n t += e[i].times;\n }\n\n cout << ans << endl;\n}", "accuracy": 0.9583333333333334, "time_ms": 10, "memory_kb": 4752, "score_of_the_acc": -0.2385, "final_rank": 12 } ]
aoj_2237_cpp
Problem F: The Castle 待ち伏せていた敵を見事撃破したうさぎは, 主人公を敵の城内に進めることに成功した. 主人公が城の地下牢に捕らわれていたねこたちを解放したところ, 彼らのうちの何匹かが主人公の力になってくれることになった. ねこたちの話では, 城の奥にいる魔王に辿りつくには1 番から n 番の n 個の部屋をこの順に通り抜けることになるが, 各部屋には1 体ずつ敵が待ち受けていて逐一倒していかなければならないという. 仲間になった m 匹のねこそれぞれについて, 各部屋の敵それぞれに対する勝率が分かっており, 主人公はこのねこたちを1 匹ずつ城の奥へ向けて派遣する. 各部屋はそこにいる敵を倒してからでなければ通過できないので, あるねこがある部屋の敵にやられたら, 次のねこはその部屋の敵から戦っていくことになる. 派遣されたねこは敵にやられるまで進むが, 派遣したねこがどの部屋の敵にやられたかは毎回知ることができ, それによって次にどのねこを派遣するかを決めることができる. どのようにすれば, ねこたちが待ち受けるすべての敵を倒せる確率が最大になるだろうか. Input 入力の一行目には m と n がスペースで区切られて与えられる. 1 ≤ m , n ≤ 16 つづく m 行には, 猫が敵に勝つ確率を表す n 個の実数が与えられる. i + 1 行目の j 個目の実数は, j 番の部屋の敵に勝つ確率を表している. 確率は小数点以下3 桁まで. Output ねこたちが待ち受けるすべての敵を倒せる確率が最大になるようにうまく順番を決めていくとき, その確率を答えよ. 出力は誤差を含んでいてもよいが, 真の値との誤差は10 −9 以内にせよ. Sample Input 1 2 3 0.900 0.500 0.100 0.500 0.500 0.500 Sample Output 1 0.372500000000
[ { "submission_id": "aoj_2237_10373053", "code_snippet": "#include <iostream>\n#include <iomanip>\n#include <cassert>\n#include <vector>\n#include <algorithm>\n#include <utility>\n#include <numeric>\n// #include \"Src/Utility/BinarySearch.hpp\"\n// #include \"Src/Sequence/CompressedSequence.hpp\"\n// #include \"Src/Sequence/RunLengthEncoding.hpp\"\n// using namespace zawa;\n// #include \"atcoder/modint\"\n// using mint = atcoder::modint998244353;\nint M, N;\nlong double P[16][16], dp[16][1 << 16];\nbool vis[17][1 << 16];\nlong double rec(int i, int b) {\n assert(i <= N);\n assert(0 <= b and b < (1 << M));\n if (i == N) return 1.0l;\n long double& res = dp[i][b];\n if (vis[i][b]) return res;\n vis[i][b] = true;\n res = 0;\n for (int j = 0 ; j < M ; j++) if (b & (1 << j)) {\n long double cur = 1, val = 0;\n for (int k = i ; k < N ; k++) {\n long double coef = cur * (1 - P[j][k]);\n val += coef * rec(k, b ^ (1 << j));\n cur *= P[j][k];\n }\n val += cur * rec(N, b);\n res = std::max(res, val);\n }\n return res;\n}\nint main() {\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n std::ios::sync_with_stdio(false);\n std::cin >> M >> N;\n for (int i = 0 ; i < M ; i++) for (int j = 0 ; j < N ; j++) std::cin >> P[i][j];\n std::cout << std::fixed << std::setprecision(12) << rec(0, (1 << M) - 1) << '\\n';\n}", "accuracy": 1, "time_ms": 750, "memory_kb": 21320, "score_of_the_acc": -1.3506, "final_rank": 16 }, { "submission_id": "aoj_2237_9730441", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n/**\n * @brief template\n */\n\nint main() {\n int N, M;\n cin >> N >> M;\n vector A(N,vector<double>(M));\n rep(i,0,N) rep(j,0,M) cin >> A[i][j];\n vector DP(M+1,vector<double>(1<<N,0.0));\n rep(i,0,1<<N) DP[M][i] = 1.0;\n rrep(i,0,M) {\n rep(j,1,1<<N) {\n rep(k,0,N) {\n if (!(j&(1<<k))) continue;\n double SUM = 0.0;\n double MUL = 1.0;\n rep(l,i,M) {\n SUM += DP[l][j-(1<<k)] * MUL * (1.0-A[k][l]);\n MUL *= A[k][l];\n }\n SUM += MUL;\n chmax(DP[i][j],SUM);\n }\n }\n }\n printf(\"%.12f\\n\", DP[0][(1<<N)-1]);\n}", "accuracy": 1, "time_ms": 120, "memory_kb": 12412, "score_of_the_acc": -0.0635, "final_rank": 5 }, { "submission_id": "aoj_2237_8998804", "code_snippet": "#line 2 \"cp-library/src/cp-template.hpp\"\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nusing i32 = int;\nusing u32 = unsigned int;\nusing i64 = long long;\nusing u64 = unsigned long long;\nusing i128 = __int128_t;\ntemplate < class T > bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }\ntemplate < class T > bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }\ntemplate < class T, class U > T ceil (T x, U y) { return (x > 0 ? (x + y - 1) / y : x / y); }\ntemplate < class T, class U > T floor(T x, U y) { return (x > 0 ? x / y : (x - y + 1) / y); }\nint popcnt(i32 x) { return __builtin_popcount(x); }\nint popcnt(u32 x) { return __builtin_popcount(x); }\nint popcnt(i64 x) { return __builtin_popcountll(x); }\nint popcnt(u64 x) { return __builtin_popcountll(x); }\n\n#line 2 \"cp-library/src/utility/rep_itr.hpp\"\ntemplate < class T > struct itr_rep {\n T i, d;\n constexpr itr_rep(const T i) noexcept : i(i), d(1) {}\n constexpr itr_rep(const T i, const T d) noexcept : i(i), d(d) {}\n void operator++() noexcept { i += d; }\n constexpr int operator*() const noexcept { return i; }\n constexpr bool operator!=(const itr_rep x) const noexcept { return d > 0 ? i < x.i : i > x.i; }\n};\n\ntemplate < class T > struct rep {\n const itr_rep< T > s, t;\n constexpr rep(const T t) noexcept : s(0), t(t) {}\n constexpr rep(const T s, const T t) noexcept : s(s), t(t) {}\n constexpr rep(const T s, const T t, const T d) noexcept : s(s, d), t(t, d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n\ntemplate < class T > struct revrep {\n const itr_rep < T > s, t;\n constexpr revrep(const T t) noexcept : s(t - 1, -1), t(-1, -1) {}\n constexpr revrep(const T s, const T t) noexcept : s(t - 1, -1), t(s - 1, -1) {}\n constexpr revrep(const T s, const T t, const T d) noexcept : s(t - 1, -d), t(s - 1, -d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n#line 3 \"cp-library/src/utility/io.hpp\"\n\n/* 128bit integer */\nistream& operator>>(istream& is, i128& x) {\n std::string s; is >> s;\n int pm = (s[0] == '-');\n x = 0;\n for(int i : rep(pm, int(s.size()))) x = x * 10 + (s[i] - '0');\n if(pm) x *= -1;\n return is;\n}\nostream& operator<<(ostream& os, const i128& x) {\n if(x == 0) return os << '0';\n i128 y = x;\n if(y < 0) { os << '-'; y *= -1; }\n std::vector<int> ny;\n while(y > 0) { ny.push_back(y % 10); y /= 10; }\n for(int i : revrep(ny.size())) os << ny[i];\n return os;\n}\n\ntemplate < class S, class T > istream& operator>>(istream& is, std::pair< S, T >& x) { is >> x.first >> x.second; return is; }\ntemplate < class S, class T > ostream& operator<<(ostream& os, const std::pair< S, T >& x) { os << x.first << \" \" << x.second; return os; }\n\nnamespace scanner {\n struct sca {\n template < class T > operator T() {\n T s; std::cin >> s; return s;\n }\n };\n struct vec {\n int n;\n vec(int n) : n(n) {}\n template < class T > operator std::vector< T >() {\n std::vector< T > v(n);\n for(T& x : v) std::cin >> x;\n return v;\n }\n };\n struct mat {\n int h, w;\n mat(int h, int w) : h(h), w(w) {}\n template < class T > operator std::vector< std::vector< T > >() {\n std::vector m(h, std::vector< T >(w));\n for(std::vector< T >& v : m) for(T& x : v) std::cin >> x;\n return m;\n }\n };\n struct speedup {\n speedup() {\n std::cin.tie(0);\n std::ios::sync_with_stdio(0);\n }\n } speedup_instance;\n}\nscanner::sca in() { return scanner::sca(); }\nscanner::vec in(int n) { return scanner::vec(n); }\nscanner::mat in(int h, int w) { return scanner::mat(h, w); }\n\nnamespace printer {\n void precision(int d) { std::cout << std::fixed << std::setprecision(d); }\n void flush() { std::cout.flush(); }\n}\n\ntemplate < class T >\nostream& operator<<(ostream& os, const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) { os << a[i]; if(i != n - 1) os << ' '; }\n return os;\n}\n\nint print() { std::cout << '\\n'; return 0; }\ntemplate < class head, class... tail > int print(head&& h, tail&&... t) {\n std::cout << h; if(sizeof...(tail)) std::cout << ' ';\n return print(std::forward<tail>(t)...);\n}\ntemplate < class T > int print_n(const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) std::cout << a[i] << \"\\n\";\n return 0;\n}\n\n\n#line 2 \"cp-library/src/utility/key_val.hpp\"\n\ntemplate < class K, class V >\nstruct key_val {\n K key; V val;\n key_val() {}\n key_val(K key, V val) : key(key), val(val) {}\n template < std::size_t Index >\n std::tuple_element_t< Index, key_val >& get() {\n if constexpr (Index == 0) return key;\n if constexpr (Index == 1) return val;\n }\n};\n\nnamespace std {\n\ntemplate < class K, class V > struct tuple_size < key_val< K, V > > : integral_constant< size_t, 2 > {};\ntemplate < class K, class V > struct tuple_element < 0, key_val< K, V > > { using type = K; };\ntemplate < class K, class V > struct tuple_element < 1, key_val< K, V > > { using type = V; };\n\n}\n#line 2 \"cp-library/src/utility/vec_op.hpp\"\ntemplate < class T > key_val< int, T > max_of(const vector< T >& a) {\n int i = std::max_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class T > key_val< int, T > min_of(const vector< T >& a) {\n int i = std::min_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class S, class T > S sum_of(const vector< T >& a) {\n S sum = 0;\n for(const T x : a) sum += x;\n return sum;\n}\ntemplate < class S, class T > vector< S > freq_of(const vector< T >& a, T L, T R) {\n vector< S > res(R - L, S(0));\n for(const T x : a) res[x - L] += 1;\n return res;\n}\ntemplate < class S, class T > struct prefix_sum {\n vector< S > s;\n prefix_sum(const vector< T >& a) : s(a) {\n s.insert(s.begin(), S(0));\n for(int i : rep(a.size())) s[i + 1] += s[i];\n }\n // [L, R)\n S sum(int L, int R) { return s[R] - s[L]; }\n};\n#line 3 \"cp-library/src/utility/heap.hpp\"\n\ntemplate < class T > using heap_min = std::priority_queue< T, std::vector< T >, std::greater< T > >;\ntemplate < class T > using heap_max = std::priority_queue< T, std::vector< T >, std::less< T > >;\n\n#line 27 \"cp-library/src/cp-template.hpp\"\n\n#line 1 \"cp-library/src/algorithm/bin_search.hpp\"\ntemplate < class T, class F >\nT bin_search(T ok, T ng, F f) {\n while(abs(ng - ok) > 1) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n\ntemplate < class T, class F >\nT bin_search_real(T ok, T ng, F f, int step = 80) {\n while(step--) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n#line 2 \"cp-library/src/algorithm/argsort.hpp\"\n\ntemplate < class T > std::vector< int > argsort(const std::vector< T > &a) {\n std::vector< int > ids((int)a.size());\n std::iota(ids.begin(), ids.end(), 0);\n std::sort(ids.begin(), ids.end(), [&](int i, int j) {\n return a[i] < a[j] || (a[i] == a[j] && i < j);\n });\n return ids;\n}\n#line 2 \"A.cpp\"\n//#include \"macro.hpp\"\nusing f64 = double;\n\nint main() {\n int m = in(), n = in();\n vector<vector<f64>> p = in(m, n);\n\n vector dp(1 << m, vector(n + 1, f64(0)));\n for(int S : rep(1 << m)) dp[S][n] = 1.0;\n for(int j : revrep(n)) {\n for(int S : rep(1 << m)) {\n for(int i : rep(m)) if(S & (1 << i)) {\n f64 cur = 0.0, win = 1.0;\n for(int k : rep(j, n + 1)) {\n cur += dp[S - (1 << i)][k] * (1.0 - p[i][k]) * win;\n win *= p[i][k];\n }\n chmax(dp[S][j], cur);\n }\n }\n }\n\n printer::precision(25);\n print(dp[(1 << m) - 1][0]);\n}", "accuracy": 1, "time_ms": 240, "memory_kb": 13964, "score_of_the_acc": -0.3035, "final_rank": 7 }, { "submission_id": "aoj_2237_8306672", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\ntemplate<class T>\nbool chmin(T &a, const T &b) {if (a > b) {a = b; return 1;} return 0;}\n\ntemplate<class T>\nbool chmax(T &a, const T &b) {if (a < b) {a = b; return 1;} return 0;}\n\nusing ll = long long;\nusing ld = long double;\n\nbool is_end = false;\n\nconst ll INF = 1e18;\nld dp[17][1<<16];\n\n\nvoid calc()\n{\n\tll M, N; cin >> M >> N;\n\tvector<vector<ld>> prob(M, vector<ld>(N+1, 0.0));\n\tfor (int i = 0; i < M; ++i)\n\t{\n\t\tfor (int j = 0; j < N; ++j)\n\t\t{\n\t\t\tcin >> prob[i][j];\n\t\t}\n\t}\n\t\n\tfor (int i = N; i >= 0; --i)\n\t{\n\t\tfor (int bit = 0; bit < (1<<M); ++bit)\n\t\t{\n\t\t\tif (i == N)\n\t\t\t{\n\t\t\t\tdp[i][bit] = 1.0;\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\t\n\t\t\tld mx = 0.0;\n\t\t\tfor (int j = 0; j < M; ++j)\n\t\t\t{\n\t\t\t\tif (!(bit & 1<<j)) {continue;}\n\t\t\t\t\n\t\t\t\tld sum = 0.0;\n\t\t\t\tld tmp = 1.0;\n\t\t\t\tfor (int k = i; k <= N; ++k)\n\t\t\t\t{\n\t\t\t\t\tsum += tmp * (1.0 - prob[j][k]) * dp[k][bit^(1<<j)];\n\t\t\t\t\ttmp *= prob[j][k];\n\t\t\t\t}\n\t\t\t\tchmax(mx, sum);\n\t\t\t}\n\t\t\tdp[i][bit] = mx;\n\t\t}\n\t}\n\t\n\tld res = dp[0][(1<<M)-1];\n\tcout << fixed << setprecision(20) << res << endl;\n\t\n\t\n\treturn;\n}\n\nint main()\n{\n\tcalc();\n\t\n\treturn 0;\n}", "accuracy": 1, "time_ms": 190, "memory_kb": 20972, "score_of_the_acc": -0.4766, "final_rank": 12 }, { "submission_id": "aoj_2237_8047206", "code_snippet": "#line 1 \"a.cpp\"\n#define PROBLEM \"\"\n#line 2 \"/home/kuhaku/home/github/algo/lib/template/template.hpp\"\n#pragma GCC target(\"sse4.2,avx2,bmi2\")\n#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"unroll-loops\")\n#include <bits/stdc++.h>\ntemplate <class T, class U>\nbool chmax(T &a, const U &b) {\n return a < (T)b ? a = (T)b, true : false;\n}\ntemplate <class T, class U>\nbool chmin(T &a, const U &b) {\n return (T)b < a ? a = (T)b, true : false;\n}\nconstexpr std::int64_t INF = 1000000000000000003;\nconstexpr int Inf = 1000000003;\nconstexpr int MOD = 1000000007;\nconstexpr int MOD_N = 998244353;\nconstexpr double EPS = 1e-7;\nconstexpr double PI = M_PI;\n#line 3 \"/home/kuhaku/home/github/algo/lib/template/macro.hpp\"\n#define FOR(i, m, n) for (int i = (m); i < int(n); ++i)\n#define FORR(i, m, n) for (int i = (m)-1; i >= int(n); --i)\n#define FORL(i, m, n) for (int64_t i = (m); i < int64_t(n); ++i)\n#define rep(i, n) FOR (i, 0, n)\n#define repn(i, n) FOR (i, 1, n + 1)\n#define repr(i, n) FORR (i, n, 0)\n#define repnr(i, n) FORR (i, n + 1, 1)\n#define all(s) (s).begin(), (s).end()\n#line 3 \"/home/kuhaku/home/github/algo/lib/template/sonic.hpp\"\nstruct Sonic {\n Sonic() {\n std::ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n }\n\n constexpr void operator()() const {}\n} sonic;\n#line 5 \"/home/kuhaku/home/github/algo/lib/template/atcoder.hpp\"\nusing namespace std;\nusing ll = std::int64_t;\nusing ld = long double;\ntemplate <class T, class U>\nstd::istream &operator>>(std::istream &is, std::pair<T, U> &p) {\n return is >> p.first >> p.second;\n}\ntemplate <class T>\nstd::istream &operator>>(std::istream &is, std::vector<T> &v) {\n for (T &i : v) is >> i;\n return is;\n}\ntemplate <class T, class U>\nstd::ostream &operator<<(std::ostream &os, const std::pair<T, U> &p) {\n return os << '(' << p.first << ',' << p.second << ')';\n}\ntemplate <class T>\nstd::ostream &operator<<(std::ostream &os, const std::vector<T> &v) {\n for (auto it = v.begin(); it != v.end(); ++it) {\n os << (it == v.begin() ? \"\" : \" \") << *it;\n }\n return os;\n}\ntemplate <class Head, class... Tail>\nvoid co(Head &&head, Tail &&...tail) {\n if constexpr (sizeof...(tail) == 0) std::cout << head << '\\n';\n else std::cout << head << ' ', co(std::forward<Tail>(tail)...);\n}\ntemplate <class Head, class... Tail>\nvoid ce(Head &&head, Tail &&...tail) {\n if constexpr (sizeof...(tail) == 0) std::cerr << head << '\\n';\n else std::cerr << head << ' ', ce(std::forward<Tail>(tail)...);\n}\ntemplate <typename T, typename... Args>\nauto make_vector(T x, int arg, Args... args) {\n if constexpr (sizeof...(args) == 0) return std::vector<T>(arg, x);\n else return std::vector(arg, make_vector<T>(x, args...));\n}\nvoid setp(int n) {\n std::cout << std::fixed << std::setprecision(n);\n}\nvoid Yes(bool is_correct = true) {\n std::cout << (is_correct ? \"Yes\" : \"No\") << '\\n';\n}\nvoid No(bool is_not_correct = true) {\n Yes(!is_not_correct);\n}\nvoid YES(bool is_correct = true) {\n std::cout << (is_correct ? \"YES\" : \"NO\") << '\\n';\n}\nvoid NO(bool is_not_correct = true) {\n YES(!is_not_correct);\n}\nvoid Takahashi(bool is_correct = true) {\n std::cout << (is_correct ? \"Takahashi\" : \"Aoki\") << '\\n';\n}\nvoid Aoki(bool is_not_correct = true) {\n Takahashi(!is_not_correct);\n}\n#line 3 \"a.cpp\"\n\nint main(void) {\n int m, n;\n cin >> m >> n;\n vector a(m, vector(n, 0.));\n cin >> a;\n\n vector dp(n + 1, vector(1 << m, 0.));\n rep (i, 1 << m) dp[n][i] = 1;\n\n repr (i, n) {\n rep (j, 1 << m) {\n rep (k, m) {\n if (j >> k & 1) {\n double sum = 0;\n double p = 1;\n FOR (l, i, n) {\n sum += dp[l][j ^ (1 << k)] * p * (1 - a[k][l]);\n p *= a[k][l];\n }\n chmax(dp[i][j], sum + p);\n }\n }\n }\n }\n setp(10);\n co(dp[0][(1 << m) - 1]);\n\n return 0;\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 12388, "score_of_the_acc": -0.0318, "final_rank": 4 }, { "submission_id": "aoj_2237_7098040", "code_snippet": "#pragma GCC target(\"avx2\")\n#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"unroll-loops\")\n#include<bits/stdc++.h>\n// #include<atcoder/modint>\nusing namespace std;\n// using namespace atcoder;\n\nvoid solve(){\n\tcout << fixed << setprecision(12);\n\tint m, n;\n\tcin >> m >> n;\n\tvector<vector<long double>> P(m, vector<long double>(n));\n\tfor(int i = 0; i < m; i++) for(int j = 0; j < n; j++) cin >> P[i][j];\n\n\tmap<pair<int, int>, long double> memo;\n\n\tvector<vector<long double>> dp(1 << m, vector<long double>(n));\n\tfor(int bit = (1 << m) - 1; bit >= 0; bit--){\n\t\tfor(int t = 0; t < n; t++){\n\t\t\tlong double p = 0.0;\n\t\t\tfor(int i = 0; i < m; i++){\n\t\t\t\tif(bit >> i & 1) continue;\n\t\t\t\tlong double tmp = 0.0;\n\t\t\t\tlong double pp = 1.0;\n\t\t\t\tint nbit = bit | (1 << i);\n\t\t\t\tfor(int j = t; j < n; j++){\n\t\t\t\t\ttmp += dp[nbit][j] * pp * (1.0 - P[i][j]);\n\t\t\t\t\tpp *= P[i][j];\n\t\t\t\t}\n\t\t\t\ttmp += pp;\n\t\t\t\tp = max(p, tmp);\n\t\t\t}\n\t\t\tdp[bit][t] = p;\n\t\t}\n\t}\n\n\tcout << dp[0][0] << \"\\n\";\n}\n\nint main(){\n\tcin.tie(0)->sync_with_stdio(0);\n\tint t;\n\tt = 1;\n\t// cin >> t;\n\twhile(t--) solve();\n}", "accuracy": 1, "time_ms": 140, "memory_kb": 22144, "score_of_the_acc": -0.4416, "final_rank": 10 }, { "submission_id": "aoj_2237_6835048", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint n, m;\ndouble dp[1 << 16][16], p[16][17];\n\ndouble solve(int bits, int x) {\n\tif (x == m) return 1;\n\tdouble& ret = dp[bits][x];\n\tif (ret != -1) return ret;\n\tret = 0;\n\tfor (int i = 0; i < n; i++) {\n\t\tif (!(bits >> i & 1)) continue;\n\t\tdouble s = 0, t = 1;\n\t\tfor (int j = x; j <= m; j++) {\n\t\t\ts += t * (1 - p[i][j]) * solve(bits ^ (1 << i), j);\n\t\t\tt *= p[i][j];\n\t\t}\n\t\tret = max(ret, s);\n\t}\n\treturn ret;\n}\n\nint main() {\n\tcin.tie(0);\n\tios_base::sync_with_stdio(0);\n\n\tcin >> n >> m;\n\tfor (int i = 0; i < n; i++) {\n\t\tfor (int j = 0; j < m; j++) {\n\t\t\tcin >> p[i][j];\n\t\t}\n\t}\n\tfor (int i = 0; i < (1 << n); i++) {\n\t\tfor (int j = 0; j < m; j++) {\n\t\t\tdp[i][j] = -1;\n\t\t}\n\t}\n\tcout << fixed << setprecision(10) << solve((1 << n) - 1, 0) << '\\n';\n\treturn 0;\n}", "accuracy": 1, "time_ms": 360, "memory_kb": 11496, "score_of_the_acc": -0.4, "final_rank": 9 }, { "submission_id": "aoj_2237_6835040", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint n, m;\ndouble dp[1 << 16][16], p[16][16];\n\ndouble solve(int bits, int x) {\n\tif (x == m) return 1;\n\tdouble& ret = dp[bits][x];\n\tif (ret != -1) return ret;\n\tret = 0;\n\tfor (int i = 0; i < n; i++) {\n\t\tif (!(bits >> i & 1)) continue;\n\t\tdouble s = 0, t = 1;\n\t\tfor (int j = x; j <= m; j++) {\n\t\t\ts += t * (1 - p[i][j]) * solve(bits ^ (1 << i), j);\n\t\t\tt *= p[i][j];\n\t\t}\n\t\tret = max(ret, s);\n\t}\n\treturn ret;\n}\n\nint main() {\n\tcin.tie(0);\n\tios_base::sync_with_stdio(0);\n\n\tcin >> n >> m;\n\tfor (int i = 0; i < n; i++) {\n\t\tfor (int j = 0; j < m; j++) {\n\t\t\tcin >> p[i][j];\n\t\t}\n\t}\n\tfor (int i = 0; i < (1 << n); i++) {\n\t\tfor (int j = 0; j < m; j++) {\n\t\t\tdp[i][j] = -1;\n\t\t}\n\t}\n\tcout << fixed << setprecision(10) << solve((1 << n) - 1, 0) << '\\n';\n\treturn 0;\n}", "accuracy": 0.2978723404255319, "time_ms": 280, "memory_kb": 11676, "score_of_the_acc": -0.2833, "final_rank": 17 }, { "submission_id": "aoj_2237_6737068", "code_snippet": "#include<bits/stdc++.h>\n#define rep(i,a,...) for(int i = (a)*(strlen(#__VA_ARGS__)!=0);i<(int)(strlen(#__VA_ARGS__)?__VA_ARGS__:(a));++i)\n#define per(i,a,...) for(int i = (strlen(#__VA_ARGS__)?__VA_ARGS__:(a))-1;i>=(int)(strlen(#__VA_ARGS__)?(a):0);--i)\n#define foreach(i, n) for(auto &i:(n))\n#define all(x) (x).begin(), (x).end()\n#define bit(x) (1ll << (x))\n#define lambda(RES_TYPE, ...) (function<RES_TYPE(__VA_ARGS__)>)[&](__VA_ARGS__) -> RES_TYPE\n#define method(FUNC_NAME, RES_TYPE, ...) function<RES_TYPE(__VA_ARGS__)> FUNC_NAME = lambda(RES_TYPE, __VA_ARGS__)\nusing namespace std;\nusing ll = long long;\nusing pii = pair<int,int>;\nusing pll = pair<ll,ll>;\n//const ll MOD = (ll)1e9+7;\nconst ll MOD = 998244353;\nconst int INF = (ll)1e9+7;\nconst ll INFLL = (ll)1e18;\ntemplate<class t>\nusing vvector = vector<vector<t>>;\ntemplate<class t>\nusing vvvector = vector<vector<vector<t>>>;\ntemplate<class t>\nusing priority_queuer = priority_queue<t, vector<t>, greater<t>>;\ntemplate<class t, class u> bool chmax(t &a, u b){if(a<b){a=b;return true;}return false;}\ntemplate<class t, class u> bool chmin(t &a, u b){if(a>b){a=b;return true;}return false;}\n#ifdef DEBUG\n#define debug(x) cout<<\"LINE \"<<__LINE__<<\": \"<<#x<<\" = \"<<x<<endl;\n#else\n#define debug(x) (void)0\n#endif\n\nnamespace templates{\n ll modpow(ll x, ll b,ll mod=MOD){\n ll res = 1;\n while(b){\n if(b&1)res = res * x % mod;\n x = x * x % mod;\n b>>=1;\n }\n return res;\n }\n\n ll modinv(ll x){\n return modpow(x, MOD-2);\n }\n\n bool was_output = false;\n\n void print();\n template <class t> void print(const vector<t> &);\n template <class t, class u> void print(const pair<t, u> &);\n template <class t> void print(const t&);\n template <class Head, class... Tail> void print(const Head&, const Tail&...);\n\n template <class t> void println(const vector<vector<t>>&);\n template <class t> void println(const vector<t>&);\n template <class t> void println(const t&);\n template <class Head, class... Tail> void println(const Head&, const Tail&...);\n void println();\n void newline();\n\n void print(){\n return;\n }\n\n template <class t>\n void print(const vector<t>&x){\n for(const t&i:x)print(i);\n }\n template <class t, class u>\n void print(const pair<t,u>&p){\n print(p.first);\n print(p.second);\n }\n template <class t>\n void print(const t&x){\n if(was_output)cout<<\" \";\n cout<<x;\n was_output = true;\n }\n template <class Head, class... Tail>\n void print(const Head&head,const Tail&...tail){\n print(head);\n print(tail...);\n }\n\n template<class t>\n void println(const vector<vector<t>>&x){\n for(vector<t> i:x)println(i);\n }\n template<class t>\n void println(const vector<t>&x){\n for(const t&i:x)print(i);\n println();\n }\n template <class t>\n void println(const t&x){\n print(x);\n println();\n }\n void println(){\n if(was_output){\n cout << endl;\n was_output = false;\n }\n }\n template <class Head, class... Tail>\n void println(const Head&head,const Tail&...tail){\n print(head);\n println(tail...);\n }\n\n void newline(){\n was_output = true;\n println();\n }\n\n template<class t>\n istream& operator>>(istream&is, vector<t>&x){\n for(auto &i:x)is >> i;\n return is;\n }\n\n template<class t, class u>\n istream& operator>>(istream&is, pair<t, u>&x){\n is >> x.first >> x.second;\n return is;\n }\n\n template<class t>\n ostream& operator<<(ostream&os, vector<t> &v){\n os << \"{\";\n for(t &i:v){\n os << i << \", \";\n }\n os << \"}\";\n return os;\n }\n\n template<class t = long long>\n t in(){\n t res; cin >> res; return res;\n }\n\n template<class t>\n vector<t> sorted(vector<t> line,function<bool(t,t)> comp=[](t a,t b){return a<b;}){\n sort(line.begin(),line.end(),comp);\n return line;\n }\n\n template<class t>\n vector<t> reversed(vector<t> line){\n reverse(line.begin(),line.end());\n return line;\n }\n string reversed(string str){\n reverse(str.begin(),str.end());\n return str;\n }\n\n long long gcd(long long a,long long b){\n while(b){\n a %= b;\n swap(a,b);\n }\n return a;\n }\n\n long long lcm(long long a,long long b){\n return a / gcd(a,b) * b;\n }\n\n class output_initializer{\n public:\n output_initializer(){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << setprecision(20);\n }\n };output_initializer OUTPUT_INITIALIZER_INSTANCE = output_initializer();\n}\n\nusing namespace templates;\n\ndouble func(){\n int m = in();\n int n = in();\n vvector<double> battle(m,vector<double>(n));\n foreach(i,battle)foreach(j,i)j=in<double>();\n vvector<double> dp(bit(m),vector<double>(n,-1));\n method(rec,double,int used,int p){\n if(p == n)return 1;\n double &it = dp[used][p];\n if(it >= 0)return it;\n it = 0;\n method(rec2,double,int use,int p){\n if(p == n)return 1;\n return rec(used|bit(use),p) * (1- battle[use][p]) + rec2(use,p+1) * battle[use][p];\n };\n rep(i,m){\n if(bit(i) & used)continue;\n chmax(it,rec2(i,p));\n }\n return it;\n };\n return rec(0,0);\n}\n\nint main(){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n printf(\"%.20lf\\n\",func());\n return 0;\n}", "accuracy": 1, "time_ms": 630, "memory_kb": 13908, "score_of_the_acc": -0.9015, "final_rank": 14 }, { "submission_id": "aoj_2237_6737066", "code_snippet": "#include<bits/stdc++.h>\n#define rep(i,a,...) for(int i = (a)*(strlen(#__VA_ARGS__)!=0);i<(int)(strlen(#__VA_ARGS__)?__VA_ARGS__:(a));++i)\n#define per(i,a,...) for(int i = (strlen(#__VA_ARGS__)?__VA_ARGS__:(a))-1;i>=(int)(strlen(#__VA_ARGS__)?(a):0);--i)\n#define foreach(i, n) for(auto &i:(n))\n#define all(x) (x).begin(), (x).end()\n#define bit(x) (1ll << (x))\n#define lambda(RES_TYPE, ...) (function<RES_TYPE(__VA_ARGS__)>)[&](__VA_ARGS__) -> RES_TYPE\n#define method(FUNC_NAME, RES_TYPE, ...) function<RES_TYPE(__VA_ARGS__)> FUNC_NAME = lambda(RES_TYPE, __VA_ARGS__)\nusing namespace std;\nusing ll = long long;\nusing pii = pair<int,int>;\nusing pll = pair<ll,ll>;\n//const ll MOD = (ll)1e9+7;\nconst ll MOD = 998244353;\nconst int INF = (ll)1e9+7;\nconst ll INFLL = (ll)1e18;\ntemplate<class t>\nusing vvector = vector<vector<t>>;\ntemplate<class t>\nusing vvvector = vector<vector<vector<t>>>;\ntemplate<class t>\nusing priority_queuer = priority_queue<t, vector<t>, greater<t>>;\ntemplate<class t, class u> bool chmax(t &a, u b){if(a<b){a=b;return true;}return false;}\ntemplate<class t, class u> bool chmin(t &a, u b){if(a>b){a=b;return true;}return false;}\n#ifdef DEBUG\n#define debug(x) cout<<\"LINE \"<<__LINE__<<\": \"<<#x<<\" = \"<<x<<endl;\n#else\n#define debug(x) (void)0\n#endif\n\nnamespace templates{\n ll modpow(ll x, ll b,ll mod=MOD){\n ll res = 1;\n while(b){\n if(b&1)res = res * x % mod;\n x = x * x % mod;\n b>>=1;\n }\n return res;\n }\n\n ll modinv(ll x){\n return modpow(x, MOD-2);\n }\n\n bool was_output = false;\n\n void print();\n template <class t> void print(const vector<t> &);\n template <class t, class u> void print(const pair<t, u> &);\n template <class t> void print(const t&);\n template <class Head, class... Tail> void print(const Head&, const Tail&...);\n\n template <class t> void println(const vector<vector<t>>&);\n template <class t> void println(const vector<t>&);\n template <class t> void println(const t&);\n template <class Head, class... Tail> void println(const Head&, const Tail&...);\n void println();\n void newline();\n\n void print(){\n return;\n }\n\n template <class t>\n void print(const vector<t>&x){\n for(const t&i:x)print(i);\n }\n template <class t, class u>\n void print(const pair<t,u>&p){\n print(p.first);\n print(p.second);\n }\n template <class t>\n void print(const t&x){\n if(was_output)cout<<\" \";\n cout<<x;\n was_output = true;\n }\n template <class Head, class... Tail>\n void print(const Head&head,const Tail&...tail){\n print(head);\n print(tail...);\n }\n\n template<class t>\n void println(const vector<vector<t>>&x){\n for(vector<t> i:x)println(i);\n }\n template<class t>\n void println(const vector<t>&x){\n for(const t&i:x)print(i);\n println();\n }\n template <class t>\n void println(const t&x){\n print(x);\n println();\n }\n void println(){\n if(was_output){\n cout << endl;\n was_output = false;\n }\n }\n template <class Head, class... Tail>\n void println(const Head&head,const Tail&...tail){\n print(head);\n println(tail...);\n }\n\n void newline(){\n was_output = true;\n println();\n }\n\n template<class t>\n istream& operator>>(istream&is, vector<t>&x){\n for(auto &i:x)is >> i;\n return is;\n }\n\n template<class t, class u>\n istream& operator>>(istream&is, pair<t, u>&x){\n is >> x.first >> x.second;\n return is;\n }\n\n template<class t>\n ostream& operator<<(ostream&os, vector<t> &v){\n os << \"{\";\n for(t &i:v){\n os << i << \", \";\n }\n os << \"}\";\n return os;\n }\n\n template<class t = long long>\n t in(){\n t res; cin >> res; return res;\n }\n\n template<class t>\n vector<t> sorted(vector<t> line,function<bool(t,t)> comp=[](t a,t b){return a<b;}){\n sort(line.begin(),line.end(),comp);\n return line;\n }\n\n template<class t>\n vector<t> reversed(vector<t> line){\n reverse(line.begin(),line.end());\n return line;\n }\n string reversed(string str){\n reverse(str.begin(),str.end());\n return str;\n }\n\n long long gcd(long long a,long long b){\n while(b){\n a %= b;\n swap(a,b);\n }\n return a;\n }\n\n long long lcm(long long a,long long b){\n return a / gcd(a,b) * b;\n }\n\n class output_initializer{\n public:\n output_initializer(){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << setprecision(20);\n }\n };output_initializer OUTPUT_INITIALIZER_INSTANCE = output_initializer();\n}\n\nusing namespace templates;\n\ndouble func(){\n int m = in();\n int n = in();\n vvector<double> battle(m,vector<double>(n));\n foreach(i,battle)foreach(j,i)j=in<double>();\n vvector<double> dp(bit(m),vector<double>(n,-1));\n method(rec,double,int used,int p){\n if(p == n)return 1;\n double &it = dp[used][p];\n if(it >= 0)return it;\n it = 0;\n method(rec2,double,int use,int p){\n if(p == n)return 1;\n return rec(used|bit(use),p) * (1- battle[use][p]) + rec2(use,p+1) * battle[use][p];\n };\n rep(i,m){\n if(bit(i) & used)continue;\n chmax(it,rec2(i,p));\n }\n return it;\n };\n return rec(0,0);\n}\n\nint main(){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n println(func());\n return 0;\n}", "accuracy": 0.1276595744680851, "time_ms": 540, "memory_kb": 12752, "score_of_the_acc": -0.7217, "final_rank": 19 }, { "submission_id": "aoj_2237_6737063", "code_snippet": "#include<bits/stdc++.h>\n#define rep(i,a,...) for(int i = (a)*(strlen(#__VA_ARGS__)!=0);i<(int)(strlen(#__VA_ARGS__)?__VA_ARGS__:(a));++i)\n#define per(i,a,...) for(int i = (strlen(#__VA_ARGS__)?__VA_ARGS__:(a))-1;i>=(int)(strlen(#__VA_ARGS__)?(a):0);--i)\n#define foreach(i, n) for(auto &i:(n))\n#define all(x) (x).begin(), (x).end()\n#define bit(x) (1ll << (x))\n#define lambda(RES_TYPE, ...) (function<RES_TYPE(__VA_ARGS__)>)[&](__VA_ARGS__) -> RES_TYPE\n#define method(FUNC_NAME, RES_TYPE, ...) function<RES_TYPE(__VA_ARGS__)> FUNC_NAME = lambda(RES_TYPE, __VA_ARGS__)\nusing namespace std;\nusing ll = long long;\nusing pii = pair<int,int>;\nusing pll = pair<ll,ll>;\n//const ll MOD = (ll)1e9+7;\nconst ll MOD = 998244353;\nconst int INF = (ll)1e9+7;\nconst ll INFLL = (ll)1e18;\ntemplate<class t>\nusing vvector = vector<vector<t>>;\ntemplate<class t>\nusing vvvector = vector<vector<vector<t>>>;\ntemplate<class t>\nusing priority_queuer = priority_queue<t, vector<t>, greater<t>>;\ntemplate<class t, class u> bool chmax(t &a, u b){if(a<b){a=b;return true;}return false;}\ntemplate<class t, class u> bool chmin(t &a, u b){if(a>b){a=b;return true;}return false;}\n#ifdef DEBUG\n#define debug(x) cout<<\"LINE \"<<__LINE__<<\": \"<<#x<<\" = \"<<x<<endl;\n#else\n#define debug(x) (void)0\n#endif\n\nnamespace templates{\n ll modpow(ll x, ll b,ll mod=MOD){\n ll res = 1;\n while(b){\n if(b&1)res = res * x % mod;\n x = x * x % mod;\n b>>=1;\n }\n return res;\n }\n\n ll modinv(ll x){\n return modpow(x, MOD-2);\n }\n\n bool was_output = false;\n\n void print();\n template <class t> void print(const vector<t> &);\n template <class t, class u> void print(const pair<t, u> &);\n template <class t> void print(const t&);\n template <class Head, class... Tail> void print(const Head&, const Tail&...);\n\n template <class t> void println(const vector<vector<t>>&);\n template <class t> void println(const vector<t>&);\n template <class t> void println(const t&);\n template <class Head, class... Tail> void println(const Head&, const Tail&...);\n void println();\n void newline();\n\n void print(){\n return;\n }\n\n template <class t>\n void print(const vector<t>&x){\n for(const t&i:x)print(i);\n }\n template <class t, class u>\n void print(const pair<t,u>&p){\n print(p.first);\n print(p.second);\n }\n template <class t>\n void print(const t&x){\n if(was_output)cout<<\" \";\n cout<<x;\n was_output = true;\n }\n template <class Head, class... Tail>\n void print(const Head&head,const Tail&...tail){\n print(head);\n print(tail...);\n }\n\n template<class t>\n void println(const vector<vector<t>>&x){\n for(vector<t> i:x)println(i);\n }\n template<class t>\n void println(const vector<t>&x){\n for(const t&i:x)print(i);\n println();\n }\n template <class t>\n void println(const t&x){\n print(x);\n println();\n }\n void println(){\n if(was_output){\n cout << endl;\n was_output = false;\n }\n }\n template <class Head, class... Tail>\n void println(const Head&head,const Tail&...tail){\n print(head);\n println(tail...);\n }\n\n void newline(){\n was_output = true;\n println();\n }\n\n template<class t>\n istream& operator>>(istream&is, vector<t>&x){\n for(auto &i:x)is >> i;\n return is;\n }\n\n template<class t, class u>\n istream& operator>>(istream&is, pair<t, u>&x){\n is >> x.first >> x.second;\n return is;\n }\n\n template<class t>\n ostream& operator<<(ostream&os, vector<t> &v){\n os << \"{\";\n for(t &i:v){\n os << i << \", \";\n }\n os << \"}\";\n return os;\n }\n\n template<class t = long long>\n t in(){\n t res; cin >> res; return res;\n }\n\n template<class t>\n vector<t> sorted(vector<t> line,function<bool(t,t)> comp=[](t a,t b){return a<b;}){\n sort(line.begin(),line.end(),comp);\n return line;\n }\n\n template<class t>\n vector<t> reversed(vector<t> line){\n reverse(line.begin(),line.end());\n return line;\n }\n string reversed(string str){\n reverse(str.begin(),str.end());\n return str;\n }\n\n long long gcd(long long a,long long b){\n while(b){\n a %= b;\n swap(a,b);\n }\n return a;\n }\n\n long long lcm(long long a,long long b){\n return a / gcd(a,b) * b;\n }\n\n class output_initializer{\n public:\n output_initializer(){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << setprecision(20);\n }\n };output_initializer OUTPUT_INITIALIZER_INSTANCE = output_initializer();\n}\n\nusing namespace templates;\n\ndouble func(){\n int n = in();\n int m = in();\n vvector<double> battle(n,vector<double>(m));\n foreach(i,battle)foreach(j,i)j=in<double>();\n vvector<double> dp(bit(n),vector<double>(m,-1));\n method(rec,double,int used,int p){\n if(p == m)return 1;\n double &it = dp[used][p];\n if(it >= 0)return it;\n it = 0;\n method(rec2,double,int use,int p){\n if(p == m)return 1;\n return rec(used|bit(use),p) * (1- battle[use][p]) + rec2(use,p+1) * battle[use][p];\n };\n rep(i,n){\n if(bit(i) & used)continue;\n chmax(it,rec2(i,p));\n }\n return it;\n };\n return rec(0,0);\n}\n\nint main(){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n println(func());\n return 0;\n}", "accuracy": 0.1276595744680851, "time_ms": 540, "memory_kb": 12824, "score_of_the_acc": -0.7243, "final_rank": 20 }, { "submission_id": "aoj_2237_6503912", "code_snippet": "#include <bits/stdc++.h>\n#pragma GCC optimize(\"Ofast\")\n#define _GLIBCXX_DEBUG\nusing namespace std;\nusing std::cout;\nusing std::cin;\nusing std::endl;\nusing ll=long long;\nusing ld=long double;\nll ILL=1167167167167167167;\nconst int INF=1050000000;\nconst ll mod=1e9+7;\n#define rep(i,a) for (ll i=0;i<a;i++)\n#define all(p) p.begin(),p.end()\ntemplate<class T> using _pq = priority_queue<T, vector<T>, greater<T>>;\ntemplate<class T> ll LB(vector<T> &v,T a){return lower_bound(v.begin(),v.end(),a)-v.begin();}\ntemplate<class T> ll UB(vector<T> &v,T a){return upper_bound(v.begin(),v.end(),a)-v.begin();}\ntemplate<class T> bool chmin(T &a,const T &b){if(a>b){a=b;return 1;}else return 0;}\ntemplate<class T> bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}else return 0;}\ntemplate<class T> void So(vector<T> &v) {sort(v.begin(),v.end());}\ntemplate<class T> void Sore(vector<T> &v) {sort(v.begin(),v.end(),[](T x,T y){return x>y;});}\nvoid yneos(bool a){if(a) cout<<\"Yes\\n\"; else cout<<\"No\\n\";}\ntemplate<class T> void vec_out(vector<T> &p){for(int i=0;i<(int)(p.size());i++){if(i) cout<<\" \";cout<<p[i];}cout<<\"\\n\";}\n\n\nvoid solve();\n// oddloop\nint main() {\n\t\n\tsolve();\n}\n\nvoid solve(){\n\tint N,M;\n\tcin>>M>>N;\n\tvector<vector<ld>> dp(N,vector<ld>(1<<M));\n\tvector<vector<ld>> pro(M,vector<ld>(N));\n\trep(i,M) rep(j,N) cin>>pro[i][j];\n\trep(j,(1<<M)){\n\t\tif(j==0) continue;\n\t\trep(i,N){\n\t\t\trep(k,M){\n\t\t\t\tif(((1<<k)&j)==0) continue;\n\t\t\t\tld tmp=0;\n\t\t\t\tld r=1;\n\t\t\t\tint ne=((1<<k)^j);\n\t\t\t\tfor(int l=i;l<N;l++){\n\t\t\t\t\ttmp+=dp[l][ne]*r*(1-pro[k][l]);\n\t\t\t\t\tr*=pro[k][l];\n\t\t\t\t}\n\t\t\t\tchmax(dp[i][j],tmp+r);\n\t\t\t}\n\t\t}\n\t}\n\t/*rep(i,N){\n\t\tvec_out(dp[i]);\n\t}*/\n\tcout<<fixed<<setprecision(18)<<dp[0][(1<<M)-1]<<\"\\n\";\n}", "accuracy": 1, "time_ms": 180, "memory_kb": 20504, "score_of_the_acc": -0.4446, "final_rank": 11 }, { "submission_id": "aoj_2237_6503899", "code_snippet": "#include <bits/stdc++.h>\n#pragma GCC optimize(\"Ofast\")\n#define _GLIBCXX_DEBUG\nusing namespace std;\nusing std::cout;\nusing std::cin;\nusing std::endl;\nusing ll=long long;\nusing ld=long double;\nll ILL=1167167167167167167;\nconst int INF=1050000000;\nconst ll mod=1e9+7;\n#define rep(i,a) for (ll i=0;i<a;i++)\n#define all(p) p.begin(),p.end()\ntemplate<class T> using _pq = priority_queue<T, vector<T>, greater<T>>;\ntemplate<class T> ll LB(vector<T> &v,T a){return lower_bound(v.begin(),v.end(),a)-v.begin();}\ntemplate<class T> ll UB(vector<T> &v,T a){return upper_bound(v.begin(),v.end(),a)-v.begin();}\ntemplate<class T> bool chmin(T &a,const T &b){if(a>b){a=b;return 1;}else return 0;}\ntemplate<class T> bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}else return 0;}\ntemplate<class T> void So(vector<T> &v) {sort(v.begin(),v.end());}\ntemplate<class T> void Sore(vector<T> &v) {sort(v.begin(),v.end(),[](T x,T y){return x>y;});}\nvoid yneos(bool a){if(a) cout<<\"Yes\\n\"; else cout<<\"No\\n\";}\ntemplate<class T> void vec_out(vector<T> &p){for(int i=0;i<(int)(p.size());i++){if(i) cout<<\" \";cout<<p[i];}cout<<\"\\n\";}\n\n\nvoid solve();\n// oddloop\nint main() {\n\tios::sync_with_stdio(false);\n\tcin.tie(nullptr);\n\t\n\tsolve();\n}\n\nvoid solve(){\n\tint N,M;\n\tcin>>M>>N;\n\tvector<vector<ld>> dp(N,vector<ld>(1<<M));\n\tvector<vector<ld>> pro(M,vector<ld>(N));\n\trep(i,M) rep(j,N) cin>>pro[i][j];\n\trep(j,(1<<M)){\n\t\tif(j==0) continue;\n\t\trep(i,N){\n\t\t\trep(k,M){\n\t\t\t\tif(((1<<k)&j)==0) continue;\n\t\t\t\tld tmp=0;\n\t\t\t\tld r=1;\n\t\t\t\tint ne=((1<<k)^j);\n\t\t\t\tfor(int l=i;l<N;l++){\n\t\t\t\t\ttmp+=dp[l][ne]*r*(1-pro[k][l]);\n\t\t\t\t\tr*=pro[k][l];\n\t\t\t\t}\n\t\t\t\tchmax(dp[i][j],tmp+r);\n\t\t\t}\n\t\t}\n\t}\n\t/*rep(i,N){\n\t\tvec_out(dp[i]);\n\t}*/\n\tcout<<setprecision(18)<<dp[0][(1<<M)-1]<<\"\\n\";\n}", "accuracy": 0.1276595744680851, "time_ms": 160, "memory_kb": 19280, "score_of_the_acc": -0.3701, "final_rank": 18 }, { "submission_id": "aoj_2237_6026380", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)\n\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << fixed << setprecision(15);\n\n int m, n;\n cin >> m >> n;\n vector<vector<double>> prob(m, vector<double>(n));\n rep(i,0,m) {\n rep(j,0,n) cin >> prob[i][j];\n }\n\n vector<vector<double>> dp(n, vector<double>(1<<m));\n for (int i = n-1; i >= 0; --i) {\n rep(S,0,1<<m) {\n rep(j,0,m) if (S>>j&1) {\n double val = 0;\n double p = 1;\n rep(k,i,n) {\n val += p*(1-prob[j][k])*dp[k][S^(1<<j)];\n p *= prob[j][k];\n }\n val += p;\n dp[i][S] = max(dp[i][S], val);\n }\n }\n }\n cout << dp[0][(1<<m)-1] << endl;\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 11860, "score_of_the_acc": -0.013, "final_rank": 1 }, { "submission_id": "aoj_2237_6025161", "code_snippet": "#include <bits/stdc++.h>\n//#include <atcoder/all>\n//using namespace atcoder;\n#pragma GCC target (\"avx2\")\n#pragma GCC optimization (\"O3\")\n#pragma GCC optimization (\"unroll-loops\")\nusing namespace std;\n \ntypedef vector<int> VI;\ntypedef vector<VI> VVI;\ntypedef vector<string> VS;\ntypedef pair<int, int> PII;\ntypedef pair<int, int> pii;\ntypedef pair<long long, long long> PLL;\ntypedef pair<int, PII> TIII;\n \ntypedef long long ll;\ntypedef long double ld;\ntypedef unsigned long long ull;\n \n \n#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)\n#define REP(i, n) FOR(i, 0, n)\n#define rep(i, a, b) for (int i = a; i < (b); ++i)\n#define trav(a, x) for (auto &a : x)\n#define all(x) x.begin(), x.end()\n \n#define MOD 1000000007\n \ntemplate<class T1, class T2> inline bool chmax(T1 &a, T2 b) {if (a < b) {a = b; return true;} return false;}\ntemplate<class T1, class T2> inline bool chmin(T1 &a, T2 b) {if (a > b) {a = b; return true;} return false;}\nconst double EPS = 1e-8, PI = acos(-1);\nconst double pi = 3.141592653589793238462643383279;\n//ここから編集 \ntypedef string::const_iterator State;\nll GCD(ll a, ll b){\n return (b==0)?a:GCD(b, a%b);\n}\nll LCM(ll a, ll b){\n return a/GCD(a, b) * b;\n}\ntemplate< int mod >\nstruct ModInt {\n int x;\n \n ModInt() : x(0) {}\n \n ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}\n \n ModInt &operator+=(const ModInt &p) {\n if((x += p.x) >= mod) x -= mod;\n return *this;\n }\n \n ModInt &operator-=(const ModInt &p) {\n if((x += mod - p.x) >= mod) x -= mod;\n return *this;\n }\n \n ModInt &operator*=(const ModInt &p) {\n x = (int) (1LL * x * p.x % mod);\n return *this;\n }\n \n ModInt &operator/=(const ModInt &p) {\n *this *= p.inverse();\n return *this;\n }\n \n ModInt operator-() const { return ModInt(-x); }\n \n ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }\n \n ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }\n \n ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }\n \n ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }\n \n bool operator==(const ModInt &p) const { return x == p.x; }\n \n bool operator!=(const ModInt &p) const { return x != p.x; }\n \n ModInt inverse() const {\n int a = x, b = mod, u = 1, v = 0, t;\n while(b > 0) {\n t = a / b;\n swap(a -= t * b, b);\n swap(u -= t * v, v);\n }\n return ModInt(u);\n }\n \n ModInt pow(int64_t n) const {\n ModInt ret(1), mul(x);\n while(n > 0) {\n if(n & 1) ret *= mul;\n mul *= mul;\n n >>= 1;\n }\n return ret;\n }\n \n friend ostream &operator<<(ostream &os, const ModInt &p) {\n return os << p.x;\n }\n \n friend istream &operator>>(istream &is, ModInt &a) {\n int64_t t;\n is >> t;\n a = ModInt< mod >(t);\n return (is);\n }\n \n static int get_mod() { return mod; }\n};\n \nusing modint = ModInt< 1000000007 >;\ntemplate< typename T >\nstruct Combination {\n vector< T > _fact, _rfact, _inv;\n \n Combination(int sz) : _fact(sz + 1), _rfact(sz + 1), _inv(sz + 1) {\n _fact[0] = _rfact[sz] = _inv[0] = 1;\n for(int i = 1; i <= sz; i++) _fact[i] = _fact[i - 1] * i;\n _rfact[sz] /= _fact[sz];\n for(int i = sz - 1; i >= 0; i--) _rfact[i] = _rfact[i + 1] * (i + 1);\n for(int i = 1; i <= sz; i++) _inv[i] = _rfact[i] * _fact[i - 1];\n }\n \n inline T fact(int k) const { return _fact[k]; }\n \n inline T rfact(int k) const { return _rfact[k]; }\n \n inline T inv(int k) const { return _inv[k]; }\n \n T P(int n, int r) const {\n if(r < 0 || n < r) return 0;\n return fact(n) * rfact(n - r);\n }\n \n T C(int p, int q) const {\n if(q < 0 || p < q) return 0;\n return fact(p) * rfact(q) * rfact(p - q);\n }\n \n T H(int n, int r) const {\n if(n < 0 || r < 0) return (0);\n return r == 0 ? 1 : C(n + r - 1, r);\n }\n};\n \nll modpow(ll x, ll n, ll mod) {\n ll res = 1;\n x %= mod;\n if(x == 0) return 0;\n while(n) {\n if(n&1) res = (res * x) % mod;\n x = (x * x) % mod;\n n >>= 1;\n }\n return res;\n} \ninline long long mod(long long a, long long m) {\n return (a % m + m) % m;\n}\ntemplate<typename T> \nstruct BIT{\n int N;\n std::vector<T> node;\n BIT(){}\n BIT(int n){\n N = n;\n node.resize(N+10);\n }\n void build(int n) {\n N = n;\n node.resize(N+10);\n }\n /* a: 1-indexed */\n void add(int a, T x){\n for(int i=a; i<(int)node.size(); i += i & -i) node[i] += x;\n }\n\n /* [1, a] */\n T sum(int a){\n T res = 0;\n for(int x=a; x>0; x -= x & -x) res += node[x];\n return res;\n }\n\n /* [l, r] */\n T rangesum(int l, int r){\n return sum(r) - sum(l-1);\n }\n\n /* \n a1+a2+...+aw >= valとなるような最小のwを返す\n @verify https://codeforces.com/contest/992/problem/E\n */\n int lower_bound(T val) {\n if(val < 0) return 0;\n\n int res = 0;\n int n = 1; \n while (n < N) n *= 2;\n\n T tv=0;\n for (int k = n; k > 0; k /= 2) {\n if(res + k < N && node[res + k] < val){\n val -= node[res+k];\n res += k;\n }\n }\n return res+1; \n }\n};\n\nstruct UnionFind{\n std::vector<int> par;\n std::vector<int> siz;\n\n UnionFind(int sz_): par(sz_), siz(sz_) {\n for(int i=0; i<sz_; ++i) par[i] = i, siz[i] = 1;\n }\n\n void init(int sz_){\n par.resize(sz_);\n siz.resize(sz_);\n for(int i=0; i<sz_; ++i) par[i] = i, siz[i] = 1;\n }\n\n int root(int x){\n if(x == par[x]) return x;\n return par[x] = root(par[x]);\n }\n\n bool merge(int x, int y){\n x = root(x), y = root(y);\n if(x == y) return false;\n if(siz[x] < siz[y]) std::swap(x, y);\n siz[x] += siz[y];\n par[y] = x;\n return true;\n }\n\n bool issame(int x, int y){\n return root(x) == root(y);\n }\n\n int size(int x){\n return siz[root(x)];\n }\n};\nstruct RollingHash{\n\n using ull = unsigned long long;\n const ull mod = (1ULL << 61) - 1;\n const ull MASK30 = (1ULL << 30) - 1;\n const ull MASK31 = (1ULL << 31) - 1;\n\n const ull MASK61 = mod;\n\n ull base;\n int n;\n vector<ull> hash, pow;\n\n RollingHash(const string &s)\n {\n random_device rnd;\n mt19937_64 mt(rnd());\n base = 1001;\n \n n = (int)s.size();\n hash.assign(n+1, 0);\n pow.assign(n+1, 1);\n \n for(int i=0; i<n; i++){\n hash[i+1] = calc_mod(mul(hash[i], base) + s[i]);\n pow[i+1] = calc_mod(mul(pow[i], base));\n }\n }\n\n ull calc_mod(ull x){\n ull xu = x >> 61;\n ull xd = x & MASK61;\n ull res = xu + xd;\n if(res >= mod) res -= mod;\n return res;\n }\n\n ull mul(ull a, ull b){\n ull au = a >> 31;\n ull ad = a & MASK31;\n ull bu = b >> 31;\n ull bd = b & MASK31;\n ull mid = ad * bu + au * bd;\n ull midu = mid >> 30;\n ull midd = mid & MASK30;\n return calc_mod(au * bu * 2 + midu + (midd << 31) + ad * bd);\n }\n\n //[l,r)のハッシュ値\n inline ull get(int l, int r){\n ull res = calc_mod(hash[r] + mod*3-mul(hash[l], pow[r-l]));\n return res;\n }\n //string tのハッシュ値\n inline ull get(string t){\n ull res = 0;\n for(int i=0; i<t.size(); i++){\n res = calc_mod(mul(res, base)+t[i]);\n }\n return res;\n }\n //string s中のtの数をカウント\n inline int count(string t) {\n if(t.size() > n) return 0;\n auto hs = get(t);\n int res = 0;\n for(int i=0; i<n-t.size()+1; i++){\n if(get(i, i+t.size()) == hs) res++; \n }\n return res;\n }\n\n /* \n concat \n @verify https://codeforces.com/problemset/problem/514/C\n */\n inline ull concat(ull h1, ull h2, int h2len){\n return calc_mod(h2 + mul(h1, pow[h2len]));\n }\n\n // LCPを求める S[a:] T[b:]\n inline int LCP(int a, int b){\n int len = min((int)hash.size()-a, (int)hash.size()-b);\n \n int lb = -1, ub = len;\n while(ub-lb>1){\n int mid = (lb+ub)/2;\n\n if(get(a, a+mid) == get(b, b+mid)) lb = mid;\n else ub = mid;\n }\n return lb;\n }\n};\nint m, n; \ndouble dp[16][(1<<16)];\ndouble p[16][16];\ndouble rec(int idx, int mask) {\n\n if(dp[idx][mask] != -1) return dp[idx][mask];\n\n double res = 0;\n for(int i=0; i<m; i++) {\n if(mask >> i & 1) continue;\n\n double t = 1.0;\n double tmp = 0;\n for(int j=idx; j<n; j++) {\n tmp += t * (1 - p[i][j]) * rec(j, mask | (1 << i));\n t *= p[i][j];\n }\n res = max(res, tmp + t);\n }\n return dp[idx][mask] = res;\n}\nint main() { \n cin.tie(0);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(12);\n \n cin >> m >> n;\n REP(i,16) REP(k,(1<<16))dp[i][k] = -1;\n REP(i,m) REP(j,n) cin >> p[i][j];\n cout << rec(0, 0) << endl;\n \n \n return 0;\n}", "accuracy": 1, "time_ms": 210, "memory_kb": 11772, "score_of_the_acc": -0.1791, "final_rank": 6 }, { "submission_id": "aoj_2237_5943828", "code_snippet": "#pragma GCC optimize(\"Ofast\")\n#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <map>\n#include <queue>\n#include <cstdio>\n#include <ctime>\n#include <assert.h>\n#include <chrono>\n#include <random>\n#include <numeric>\n#include <set>\n#include <deque>\n#include <stack>\n#include <sstream>\n#include <utility>\n#include <cstring>\n#include <unordered_map>\n#include <unordered_set>\n#include <tuple>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned long long ull;\n\nmt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());\nll myRand(ll B) {\n return (ull)rng() % B;\n}\n\n// 生き残ってるネコ、今何ステージ目、今戦闘中のネコ\ndouble p[16][16];\ndouble dp[1<<16][2][16];\nunsigned char mx[1<<16][17];\n\nint main(){\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n int n,m; cin >> n >> m;\n for(int i=0;i<n;i++){\n for(int j=0;j<m;j++){\n cin >> p[i][j];\n }\n }\n int cur = 0;\n for(int i=0;i<(1<<n);i++){\n for(int j=0;j<n;j++){\n dp[i][1][j] = 1;\n }\n }\n for(int j=m-1;j>=0;j--){\n for(int i=0;i<(1<<n);i++){\n for(int k=0;k<n;k++){\n if(!((1<<k)&i))continue;\n dp[i][cur][k] = dp[i][cur^1][k]*p[k][j];\n if(mx[i^(1<<k)][j]!=16) dp[i][cur][k] += dp[i^(1<<k)][cur][mx[i^(1<<k)][j]]*(1-p[k][j]); \n }\n mx[i][j] = 16;\n for(int k=0;k<n;k++){\n if(!((1<<k)&i))continue;\n if(mx[i][j] == 16){\n mx[i][j] = k;\n }\n if(dp[i][cur][k]>dp[i][cur][mx[i][j]]){\n mx[i][j] = k;\n }\n }\n }\n cur^=1;\n }\n double res = 0.0;\n for(int i=0;i<n;i++){\n res = max(res, dp[(1<<n)-1][cur^1][i]);\n }\n printf(\"%.9f\\n\",res);\n}", "accuracy": 1, "time_ms": 110, "memory_kb": 21040, "score_of_the_acc": -0.356, "final_rank": 8 }, { "submission_id": "aoj_2237_5928458", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(15);\n int m, n;\n cin >> m >> n;\n vector<vector<double>> p(m, vector<double>(n));\n for (int i = 0; i < m; i++) {\n for (int j = 0; j < n; j++) {\n cin >> p[i][j];\n }\n }\n\n vector<vector<double>> dp(n + 1, vector<double>(1 << m, 0));\n for (int mask = 0; mask < (1 << m); mask++) dp[n][mask] = 1;\n for (int i = n - 1; i >= 0; i--) {\n for (int mask = (1 << m) - 1; mask >= 0; mask--) {\n for (int j = 0; j < m; j++) {\n if (mask >> j & 1) continue;\n double sum = 0, prod = 1;\n for (int k = i; k < n; k++) {\n sum += prod * (1 - p[j][k]) * dp[k][mask | 1 << j];\n prod *= p[j][k];\n }\n sum += prod;\n dp[i][mask] = max(dp[i][mask], sum);\n }\n }\n }\n\n double ans = dp[0][0];\n cout << ans << '\\n';\n return 0;\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 12372, "score_of_the_acc": -0.0313, "final_rank": 2 }, { "submission_id": "aoj_2237_5928456", "code_snippet": "#define LOCAL\n#include <bits/stdc++.h>\nusing namespace std;\n#pragma region Macros\ntypedef long long ll;\ntypedef __int128_t i128;\ntypedef unsigned int uint;\ntypedef unsigned long long ull;\n#define ALL(x) (x).begin(), (x).end()\n\ntemplate <typename T> istream& operator>>(istream& is, vector<T>& v) {\n for (T& x : v) is >> x;\n return is;\n}\ntemplate <typename T> ostream& operator<<(ostream& os, const vector<T>& v) {\n for (int i = 0; i < (int)v.size(); i++) {\n os << v[i] << (i + 1 == (int)v.size() ? \"\" : \" \");\n }\n return os;\n}\ntemplate <typename T, typename U> ostream& operator<<(ostream& os, const pair<T, U>& p) {\n os << '(' << p.first << ',' << p.second << ')';\n return os;\n}\ntemplate <typename T, typename U> ostream& operator<<(ostream& os, const map<T, U>& m) {\n os << '{';\n for (auto itr = m.begin(); itr != m.end();) {\n os << '(' << itr->first << ',' << itr->second << ')';\n if (++itr != m.end()) os << ',';\n }\n os << '}';\n return os;\n}\ntemplate <typename T, typename U> ostream& operator<<(ostream& os, const unordered_map<T, U>& m) {\n os << '{';\n for (auto itr = m.begin(); itr != m.end();) {\n os << '(' << itr->first << ',' << itr->second << ')';\n if (++itr != m.end()) os << ',';\n }\n os << '}';\n return os;\n}\ntemplate <typename T> ostream& operator<<(ostream& os, const set<T>& s) {\n os << '{';\n for (auto itr = s.begin(); itr != s.end();) {\n os << *itr;\n if (++itr != s.end()) os << ',';\n }\n os << '}';\n return os;\n}\ntemplate <typename T> ostream& operator<<(ostream& os, const multiset<T>& s) {\n os << '{';\n for (auto itr = s.begin(); itr != s.end();) {\n os << *itr;\n if (++itr != s.end()) os << ',';\n }\n os << '}';\n return os;\n}\ntemplate <typename T> ostream& operator<<(ostream& os, const unordered_set<T>& s) {\n os << '{';\n for (auto itr = s.begin(); itr != s.end();) {\n os << *itr;\n if (++itr != s.end()) os << ',';\n }\n os << '}';\n return os;\n}\ntemplate <typename T> ostream& operator<<(ostream& os, const deque<T>& v) {\n for (int i = 0; i < (int)v.size(); i++) {\n os << v[i] << (i + 1 == (int)v.size() ? \"\" : \" \");\n }\n return os;\n}\n\ntemplate <int i, typename T> void print_tuple(ostream&, const T&) {}\ntemplate <int i, typename T, typename H, class... Args> void print_tuple(ostream& os, const T& t) {\n if (i) os << ',';\n os << get<i>(t);\n print_tuple<i + 1, T, Args...>(os, t);\n}\ntemplate <typename... Args> ostream& operator<<(ostream& os, const tuple<Args...>& t) {\n os << '{';\n print_tuple<0, tuple<Args...>, Args...>(os, t);\n return os << '}';\n}\n\nvoid debug_out() { cerr << '\\n'; }\ntemplate <class Head, class... Tail> void debug_out(Head&& head, Tail&&... tail) {\n cerr << head;\n if (sizeof...(Tail) > 0) cerr << \", \";\n debug_out(move(tail)...);\n}\n#ifdef LOCAL\n#define debug(...) \\\n cerr << \" \"; \\\n cerr << #__VA_ARGS__ << \" :[\" << __LINE__ << \":\" << __FUNCTION__ << \"]\" << '\\n'; \\\n cerr << \" \"; \\\n debug_out(__VA_ARGS__)\n#else\n#define debug(...) 42\n#endif\n\ntemplate <typename T> T gcd(T x, T y) { return y != 0 ? gcd(y, x % y) : x; }\ntemplate <typename T> T lcm(T x, T y) { return x / gcd(x, y) * y; }\n\nint topbit(signed t) { return t == 0 ? -1 : 31 - __builtin_clz(t); }\nint topbit(long long t) { return t == 0 ? -1 : 63 - __builtin_clzll(t); }\nint botbit(signed a) { return a == 0 ? 32 : __builtin_ctz(a); }\nint botbit(long long a) { return a == 0 ? 64 : __builtin_ctzll(a); }\nint popcount(signed t) { return __builtin_popcount(t); }\nint popcount(long long t) { return __builtin_popcountll(t); }\nbool ispow2(int i) { return i && (i & -i) == i; }\n\ntemplate <class T> T ceil(T x, T y) {\n assert(y >= 1);\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <class T> T floor(T x, T y) {\n assert(y >= 1);\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\n\ntemplate <class T1, class T2> inline bool chmin(T1& a, T2 b) {\n if (a > b) {\n a = b;\n return true;\n }\n return false;\n}\ntemplate <class T1, class T2> inline bool chmax(T1& a, T2 b) {\n if (a < b) {\n a = b;\n return true;\n }\n return false;\n}\n#pragma endregion\n\nconst int INF = 1e9;\nconst long long IINF = 1e18;\nconst int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};\nconst char dir[4] = {'D', 'R', 'U', 'L'};\nconst long long MOD = 1000000007;\n// const long long MOD = 998244353;\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(15);\n int m, n;\n cin >> m >> n;\n vector<vector<double>> p(m, vector<double>(n));\n for (int i = 0; i < m; i++) {\n for (int j = 0; j < n; j++) {\n cin >> p[i][j];\n }\n }\n\n vector<vector<double>> dp(\n n + 1, vector<double>(1 << m, 0)); // 次に戦うのが i 番目の敵でそれまでに mask の猫を使っているときの勝率\n for (int mask = 0; mask < (1 << m); mask++) dp[n][mask] = 1;\n for (int i = n - 1; i >= 0; i--) {\n for (int mask = (1 << m) - 1; mask >= 0; mask--) {\n for (int j = 0; j < m; j++) {\n if (mask >> j & 1) continue;\n double sum = 0, prod = 1;\n for (int k = i; k < n; k++) {\n sum += prod * (1 - p[j][k]) * dp[k][mask | 1 << j];\n prod *= p[j][k];\n }\n sum += prod;\n dp[i][mask] = max(dp[i][mask], sum);\n }\n }\n }\n\n double ans = dp[0][0];\n cout << ans << '\\n';\n return 0;\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 12372, "score_of_the_acc": -0.0313, "final_rank": 2 }, { "submission_id": "aoj_2237_5833199", "code_snippet": "/*\tauthor: Kite_kuma\n\tcreated: 2021.08.28 14:41:33 */\n\n// #ifdef LOCAL\n// #define _GLIBCXX_DEBUG\n// #endif\n#include <bits/stdc++.h>\nusing namespace std;\n\n#pragma region macros\n\n#define foa(s, v) for(auto &s : v)\n#define all(v) (v).begin(), (v).end()\n#define rall(v) (v).rbegin(), (v).rend()\n#define popcnt(n) __builtin_popcountll((long long)n)\n\n#define REPname(a, b, c, d, e, ...) e\n#define rep(...) REPname(__VA_ARGS__, REP3, REP2, REP1, REP0)(__VA_ARGS__)\n#define REP0(x) for(int Counter_in_rep_macro = 0; Counter_in_rep_macro < (x); ++Counter_in_rep_macro)\n#define REP1(i, x) for(int i = 0; i < (x); ++i)\n#define REP2(i, l, r) for(int i = (l); i < (r); ++i)\n#define REP3(i, l, r, c) for(int i = (l); i < (r); i += (c))\n\n#define DREPname(a, b, c, d, e, ...) e\n#define drep(...) DREPname(__VA_ARGS__, DREP3, DREP2, DREP1)(__VA_ARGS__)\n#define DREP1(i, x) for(int i = (x)-1; i >= 0; --i)\n#define DREP2(i, l, r) for(int i = (r)-1; i >= (l); --i)\n#define DREP3(i, l, r, c) for(int i = (r)-1; i >= (l); i -= (c))\n\n#pragma endregion\n\n#pragma region aliases\n\nusing ll = long long;\nusing ld = long double;\nusing ull = unsigned long long;\nusing vi = std::vector<int>;\nusing vvi = std::vector<std::vector<int>>;\nusing vvvi = std::vector<std::vector<std::vector<int>>>;\nusing vll = std::vector<ll>;\nusing vvll = std::vector<vll>;\nusing vvvll = std::vector<vvll>;\nusing pii = std::pair<int, int>;\nusing pll = std::pair<long long, long long>;\ntemplate <class T = ll>\nusing V = std::vector<T>;\ntemplate <class T = ll>\nusing VV = V<V<T>>;\ntemplate <class T = ll>\nusing VVV = V<V<V<T>>>;\ntemplate <class T = ll>\nusing pqup = std::priority_queue<T, std::vector<T>, std::greater<T>>;\ntemplate <class T = ll>\nusing pqdn = std::priority_queue<T>;\n\n#pragma endregion\n\n#pragma region constants\n\nconst int inf = 1e9;\nconst long long INF = 1e18;\nconst long double pi = acos(-1);\nconst char dl = '\\n';\nconst char sp = ' ';\nint dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};\nint dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};\n\nconst int mod_1000000007 = 1000000007;\nconst int mod_998244353 = 998244353;\n\n#pragma endregion\n\n#pragma region basic_operation\n\ntemplate <class T0, class T1, class T2>\ninline bool in_range(T0 x, T1 lef, T2 rig) {\n\treturn ((lef <= x) && (x < rig));\n}\n\ntemplate <class T>\ninline bool chmin(T &a, T b) {\n\tif(a > b) {\n\t\ta = b;\n\t\treturn true;\n\t}\n\treturn false;\n}\ntemplate <class T>\ninline bool chmax(T &a, T b) {\n\tif(a < b) {\n\t\ta = b;\n\t\treturn true;\n\t}\n\treturn false;\n}\n\nvoid Yes(bool f = 1) { std::cout << (f ? \"Yes\" : \"No\") << '\\n'; }\nvoid No() { std::cout << \"No\\n\"; }\nvoid YES(bool f = 1) { std::cout << (f ? \"YES\" : \"NO\") << '\\n'; }\nvoid NO() { std::cout << \"NO\\n\"; }\n\ntemplate <class T>\nvoid drop(T answer) {\n\tstd::cout << answer << '\\n';\n\texit(0);\n}\n\nvoid err(bool flag = true) {\n\tif(!flag) return;\n\tstd::cout << -1 << '\\n';\n\texit(0);\n}\n\ntemplate <class T>\nvoid vout(std::vector<T> const &v, bool vertically = 0) {\n\tif(vertically) {\n\t\tfor(auto const &a : v) {\n\t\t\tstd::cout << a << '\\n';\n\t\t}\n\t\treturn;\n\t}\n\tfor(auto it = v.begin(); it != v.end(); it++) {\n\t\tstd::cout << (*it);\n\t\tif(it != v.end() - 1) {\n\t\t\tstd::cout << ' ';\n\t\t}\n\t}\n\tstd::cout << '\\n';\n\treturn;\n}\n\ninline void print() { std::cout << '\\n'; }\ntemplate <class T>\ninline void print(T x) {\n\tstd::cout << x << '\\n';\n\treturn;\n}\ntemplate <typename Head, typename... Tail>\nvoid print(Head H, Tail... T) {\n\tstd::cout << H << \" \";\n\tprint(T...);\n}\n\ntemplate <class T>\nvoid add(std::vector<T> &v, T val) {\n\tfor(auto &a : v) a += val;\n\treturn;\n}\n\ntemplate <class T>\nT dup(T a, T b) {\n\tassert(b != 0);\n\treturn (a + b - 1) / b;\n}\n\ntemplate <class T>\nT greatest_lower_multiple(T x, T d) {\n\tif(d == 0) return 0;\n\tif(d < 0) d *= -1;\n\tT y = x % d;\n\tif(y < 0) y += d;\n\treturn x - y;\n}\n\ntemplate <class T>\nT least_upper_multiple(T x, T d) {\n\treturn -greatest_lower_multiple(-x, d);\n}\n\nlong long POW(long long a, long long n) {\n\tlong long res = 1;\n\twhile(n > 0) {\n\t\tif(n & 1) res = res * a;\n\t\ta = a * a;\n\t\tn >>= 1;\n\t}\n\treturn res;\n}\n\nlong long modpow(long long a, long long n, long long mod) {\t // a^n mod\n\tassert(n >= 0 && mod);\n\tif(mod == 1) return 0LL;\n\tlong long res = 1;\n\ta %= mod;\n\twhile(n > 0) {\n\t\tif(n & 1) res = res * a % mod;\n\t\ta = a * a % mod;\n\t\tn >>= 1;\n\t}\n\treturn res < 0 ? res + mod : res;\n}\n\n// return x which satisfies a * x % mod == gcd(a, mod)\n// not (mod | a)\nlong long modinv(long long a, long long mod) {\n\tlong long b = mod, u = 1, v = 0;\n\twhile(b) {\n\t\tlong long t = a / b;\n\t\ta -= t * b;\n\t\tstd::swap(a, b);\n\t\tu -= t * v;\n\t\tstd::swap(u, v);\n\t}\n\tu %= mod;\n\tif(u < 0) u += mod;\n\treturn u;\n}\n\ntemplate <class T>\nint lb(const std::vector<T> &a, const T x) {\n\treturn std::distance(a.begin(), std::lower_bound(a.begin(), a.end(), x));\n}\ntemplate <class T>\nint ub(const std::vector<T> &a, const T x) {\n\treturn std::distance(a.begin(), std::upper_bound(a.begin(), a.end(), x));\n}\ntemplate <class T>\nvoid unq_sort(std::vector<T> &a) {\n\tstd::sort(a.begin(), a.end());\n\ta.erase(std::unique(a.begin(), a.end()), a.end());\n}\ntemplate <class T>\nstd::vector<int> press(std::vector<T> &a) {\n\tauto vec = a;\n\tunq_sort(vec);\n\tstd::vector<int> ret;\n\tfor(auto &v : a) ret.push_back(lb(vec, v));\n\treturn ret;\n}\n\n#pragma endregion\n\n#pragma region input\n#define VEC(type, name, size) \\\n\tvector<type> name(size); \\\n\tscanner::INPUT(name)\n#define VVEC(type, name, h, w) \\\n\tvector<vector<type>> name(h, vector<type>(w)); \\\n\tscanner::INPUT(name)\n#define INT(...) \\\n\tint __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define LL(...) \\\n\tlong long __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define STR(...) \\\n\tstring __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define CHR(...) \\\n\tchar __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define DBL(...) \\\n\tdouble __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define LD(...) \\\n\tlong double __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define TPL3(type0, type1, type2, name) \\\n\tstd::tuple<type0, type1, type2> name; \\\n\tscanner::INPUT(name);\n#define TPL4(type0, type1, type2, type3, name) \\\n\tstd::tuple<type0, type1, type2, type3> name; \\\n\tscanner::INPUT(name);\n\nnamespace scanner {\ntemplate <class T>\nvoid scan(T &a) {\n\tstd::cin >> a;\n}\n\ntemplate <class T, class L>\nvoid scan(std::pair<T, L> &p) {\n\tscan(p.first);\n\tscan(p.second);\n}\n\ntemplate <class T0, class T1, class T2>\nvoid scan(std::tuple<T0, T1, T2> &p) {\n\tT0 t0;\n\tT1 t1;\n\tT2 t2;\n\tscan(t0);\n\tscan(t1);\n\tscan(t2);\n\tp = std::make_tuple(t0, t1, t2);\n}\n\ntemplate <class T0, class T1, class T2, class T3>\nvoid scan(std::tuple<T0, T1, T2, T3> &p) {\n\tT0 t0;\n\tT1 t1;\n\tT2 t2;\n\tT3 t3;\n\tscan(t0);\n\tscan(t1);\n\tscan(t2);\n\tscan(t3);\n\tp = std::make_tuple(t0, t1, t2, t3);\n}\n\ntemplate <class T>\nvoid scan(std::vector<T> &a) {\n\tfor(auto &i : a) scan(i);\n}\n\nvoid INPUT() {}\ntemplate <class Head, class... Tail>\nvoid INPUT(Head &head, Tail &... tail) {\n\tscan(head);\n\tINPUT(tail...);\n}\n} // namespace scanner\n\ntemplate <typename T1, typename T2>\nstd::istream &operator>>(std::istream &is, std::pair<T1, T2> &p) {\n\tis >> p.first >> p.second;\n\treturn is;\n}\n#pragma endregion\n\n#pragma region debug\n\n#pragma region output\ntemplate <typename T1, typename T2>\nstd::ostream &std::operator<<(std::ostream &os, const std::pair<T1, T2> &p) {\n\tos << p.first << \" \" << p.second;\n\treturn os;\n}\ntemplate <class T>\nstd::ostream &std::operator<<(std::ostream &os, const std::vector<T> &v) {\n\tfor(int i = 0; i < (int)v.size(); i++) {\n\t\tif(i) os << \" \";\n\t\tos << v[i];\n\t}\n\treturn os;\n}\n#pragma endregion\n\n#pragma region view\n\nnamespace viewer {\nvoid view(const long long e) {\n\tif(e == INF)\n\t\tstd::cerr << \"INF\";\n\telse if(e == -INF)\n\t\tstd::cerr << \"-INF\";\n\telse\n\t\tstd::cerr << e;\n}\n\nvoid view(const int e) {\n\tif(e == inf)\n\t\tstd::cerr << \"inf\";\n\telse if(e == -inf)\n\t\tstd::cerr << \"-inf\";\n\telse\n\t\tstd::cerr << e;\n}\n\ntemplate <typename T>\nvoid view(const T e) {\n\tstd::cerr << e;\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::pair<T, U> &p) {\n\tstd::cerr << \"(\";\n\tview(p.first);\n\tstd::cerr << \", \";\n\tview(p.second);\n\tstd::cerr << \")\";\n}\n\ntemplate <class T0, class T1, class T2>\nvoid view(const std::tuple<T0, T1, T2> &p) {\n\tstd::cerr << \"(\";\n\tview(std::get<0>(p));\n\tstd::cerr << \", \";\n\tview(std::get<1>(p));\n\tstd::cerr << \", \";\n\tview(std::get<2>(p));\n\tstd::cerr << \")\";\n}\n\ntemplate <class T0, class T1, class T2, class T3>\nvoid view(const std::tuple<T0, T1, T2, T3> &p) {\n\tstd::cerr << \"(\";\n\tview(std::get<0>(p));\n\tstd::cerr << \", \";\n\tview(std::get<1>(p));\n\tstd::cerr << \", \";\n\tview(std::get<2>(p));\n\tstd::cerr << \", \";\n\tview(std::get<3>(p));\n\tstd::cerr << \")\";\n}\n\ntemplate <typename T>\nvoid view(const std::set<T> &s) {\n\tif(s.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << \"{ \";\n\tfor(auto &t : s) {\n\t\tview(t);\n\t\tstd::cerr << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\n\ntemplate <typename T>\nvoid view(const std::unordered_set<T> &s) {\n\tif(s.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << \"{ \";\n\tfor(auto &t : s) {\n\t\tview(t);\n\t\tstd::cerr << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\n\ntemplate <typename T>\nvoid view(const std::vector<T> &v) {\n\tif(v.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << \"{ \";\n\tfor(const auto &e : v) {\n\t\tview(e);\n\t\tstd::cerr << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\n\ntemplate <typename T>\nvoid view(const std::vector<std::vector<T>> &vv) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &v : vv) {\n\t\tstd::cerr << \"\\t\";\n\t\tview(v);\n\t\tstd::cerr << '\\n';\n\t}\n\tstd::cerr << \"}\";\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::vector<std::pair<T, U>> &v) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &c : v) {\n\t\tstd::cerr << \"\\t(\";\n\t\tview(c.first);\n\t\tstd::cerr << \", \";\n\t\tview(c.second);\n\t\tstd::cerr << \")\\n\";\n\t}\n\tstd::cerr << \"}\";\n}\n\ntemplate <class T0, class T1, class T2>\nvoid view(const std::vector<std::tuple<T0, T1, T2>> &v) {\n\tif(v.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << '{';\n\tfor(const auto &t : v) {\n\t\tstd::cerr << \"\\n\\t\";\n\t\tview(t);\n\t\tstd::cerr << \",\";\n\t}\n\tstd::cerr << \"\\n}\";\n}\n\ntemplate <class T0, class T1, class T2, class T3>\nvoid view(const std::vector<std::tuple<T0, T1, T2, T3>> &v) {\n\tif(v.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << '{';\n\tfor(const auto &t : v) {\n\t\tstd::cerr << \"\\n\\t\";\n\t\tview(t);\n\t\tstd::cerr << \",\";\n\t}\n\tstd::cerr << \"\\n}\";\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::map<T, U> &m) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &t : m) {\n\t\tstd::cerr << \"\\t[\";\n\t\tview(t.first);\n\t\tstd::cerr << \"] : \";\n\t\tview(t.second);\n\t\tstd::cerr << '\\n';\n\t}\n\tstd::cerr << \"}\";\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::unordered_map<T, U> &m) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &t : m) {\n\t\tstd::cerr << \"\\t[\";\n\t\tview(t.first);\n\t\tstd::cerr << \"] : \";\n\t\tview(t.second);\n\t\tstd::cerr << '\\n';\n\t}\n\tstd::cerr << \"}\";\n}\n} // namespace viewer\n\n#pragma endregion\n\n// when debugging : g++ foo.cpp -DLOCAL\n#ifdef LOCAL\nvoid debug_out() {}\ntemplate <typename Head, typename... Tail>\nvoid debug_out(Head H, Tail... T) {\n\tviewer::view(H);\n\tstd::cerr << \", \";\n\tdebug_out(T...);\n}\n#define debug(...) \\\n\tdo { \\\n\t\tstd::cerr << __LINE__ << \" [\" << #__VA_ARGS__ << \"] : [\"; \\\n\t\tdebug_out(__VA_ARGS__); \\\n\t\tstd::cerr << \"\\b\\b]\\n\"; \\\n\t} while(0)\n#define dump(x) \\\n\tdo { \\\n\t\tstd::cerr << __LINE__ << \" \" << #x << \" : \"; \\\n\t\tviewer::view(x); \\\n\t\tstd::cerr << '\\n'; \\\n\t} while(0)\n\n#else\n#define debug(...) (void(0))\n#define dump(x) (void(0))\n#endif\n\n#pragma endregion\nusing R = long double;\nint main() {\n\tstd::ios::sync_with_stdio(false);\n\tstd::cin.tie(nullptr);\n\tstd::cout << std::fixed << std::setprecision(15);\n\tsrand((unsigned)time(NULL));\n\n\tint room_number, cat_number;\n\tcin >> cat_number >> room_number;\n\n\tVV<R> cat_room_win_prob(cat_number, V<R>(room_number));\n\n\tfoa(t, cat_room_win_prob) foa(c, t) { cin >> c; }\n\n\tdebug(cat_room_win_prob);\n\n\tint u = 1 << cat_number;\n\tVV<R> dp(cat_number, V<R>(u, 1));\n\tV<R> res(u, 1);\n\n\tauto dp_nxt = dp;\n\tauto res_nxt = res;\n\n\tdrep(nxt_room, room_number) {\n\t\tdp_nxt.assign(cat_number, V<R>(u, 0));\n\t\tres_nxt.assign(u, 0);\n\t\trep(catset, u) {\n\t\t\trep(nxt_cat, cat_number) {\n\t\t\t\tif(!((1 << nxt_cat) & catset)) continue;\n\n\t\t\t\tR prob = cat_room_win_prob[nxt_cat][nxt_room];\n\t\t\t\tR win_prob = dp[nxt_cat][catset] * prob;\n\t\t\t\tR lose_prob = res_nxt[catset ^ (1 << nxt_cat)] * (1 - prob);\n\n\t\t\t\tdebug(nxt_room, catset, nxt_cat, win_prob, lose_prob);\n\n\t\t\t\tdp_nxt[nxt_cat][catset] = win_prob + lose_prob;\n\t\t\t\tchmax(res_nxt[catset], dp_nxt[nxt_cat][catset]);\n\t\t\t}\n\t\t}\n\t\tswap(dp, dp_nxt);\n\t\tswap(res_nxt, res);\n\n\t\tdebug(dp);\n\t\tdebug(res);\n\t}\n\n\tdrop(res[u - 1]);\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 39516, "score_of_the_acc": -1, "final_rank": 15 }, { "submission_id": "aoj_2237_5728872", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#define ALL(x) begin(x),end(x)\n#define rep(i,n) for(int i=0;i<(n);i++)\n#define debug(v) cout<<#v<<\":\";for(auto x:v){cout<<x<<' ';}cout<<endl;\n#define mod 1000000007\nusing ll=long long;\nconst int INF=1000000000;\nconst ll LINF=1001002003004005006ll;\nint dx[]={1,0,-1,0},dy[]={0,1,0,-1};\n// ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}\ntemplate<class T>bool chmax(T &a,const T &b){if(a<b){a=b;return true;}return false;}\ntemplate<class T>bool chmin(T &a,const T &b){if(b<a){a=b;return true;}return false;}\n\nstruct IOSetup{\n IOSetup(){\n cin.tie(0);\n ios::sync_with_stdio(0);\n cout<<fixed<<setprecision(12);\n }\n} iosetup;\n\ntemplate<typename T>\nostream &operator<<(ostream &os,const vector<T>&v){\n for(int i=0;i<(int)v.size();i++) os<<v[i]<<(i+1==(int)v.size()?\"\":\" \");\n return os;\n}\ntemplate<typename T>\nistream &operator>>(istream &is,vector<T>&v){\n for(T &x:v)is>>x;\n return is;\n}\n\nsigned main(){\n int N,M;cin>>M>>N;\n vector<vector<double>> A(M,vector<double>(N));\n cin>>A;\n\n vector<vector<double>> dp(1<<M,vector<double>(N+1,-1));\n function<double(int,int)> rec=[&](int bit,int k){\n if(k==N) return 1.0;\n if(dp[bit][k]>=0) return dp[bit][k];\n\n double ret=0.0;\n for(int i=0;i<M;i++)if(!((bit>>i)&1)){\n double nx=1.0,cand=0.0;\n for(int j=k;j<N;j++){\n cand+=nx*(1.0-A[i][j])*rec(bit|(1<<i),j);\n nx*=A[i][j];\n }\n cand+=nx;\n chmax(ret,cand);\n }\n return dp[bit][k]=ret;\n };\n \n cout<<rec(0,0)<<endl;\n return 0;\n}", "accuracy": 1, "time_ms": 420, "memory_kb": 13968, "score_of_the_acc": -0.5805, "final_rank": 13 } ]
aoj_2235_cpp
Problem D: Graph Construction n 匹のうさぎがいて, 0 番から n − 1 番の番号がついた小屋に1 匹ずつ住んでいる. あるとき, 秘密の組織によって地下通路の建設工事が行われる, という情報がうさぎたちのもとへ入った. 地下通路を使えば, うさぎたちは他のうさぎたちの小屋へ遊びに行けるようになって嬉しい. 通路は両方向に進むことができ, また通路同士は交わらない. 諸事情により, 一度建設された通路が破壊されてしまうこともある. 通路の建設や破壊の工事は1 本ずつ行われ, 各工事中はうさぎは自分の小屋に留まっているものとする. うさぎたちは, 工事のいくつかの段階において, あるうさぎとあるうさぎが遊べるかどうかを事前に知りたい. うさぎたちは仲良しなので, 遊びに行くときに他のうさぎの小屋を自由に通ることができる. プログラミングが好きなうさぎたちは, 昔似たような問題を解いたので簡単だろうと思って挑戦し出したが, なかなか効率の良いプログラムが書けない. うさぎの代わりにこの問題を解け. Input 入力の一行目には n と k がスペース区切りで与えられる。2 ≤ n ≤ 40 000, 1 ≤ k ≤ 40 000 つづく k 行には工事情報と質問が合わせて, 時間順に与えられる. “1 u v ” — 小屋 u と v を結ぶ通路が建設される. 小屋 u と v を結ぶ通路がないときにのみ現れる. “2 u v ” — 小屋 u と v を結ぶ通路が破壊される. 小屋 u と v を結ぶ通路があるときにのみ現れる. “3 u v ” — 小屋 u と v のうさぎが遊べるかどうか判定せよ. 0 ≤ u < v < n Output 入力に現れる各質問について, 遊べるなら”YES”を, そうでないなら”NO”を1 行ずつ出力せよ. Sample Input 1 4 10 1 0 1 1 0 2 3 1 2 2 0 1 1 2 3 3 0 1 1 0 1 2 0 2 1 1 3 3 0 2 Sample Output 1 YES NO YES
[ { "submission_id": "aoj_2235_10456769", "code_snippet": "// competitive-verifier: PROBLEM\n#include <iostream>\n#include <utility>\n#include <vector>\n#include <algorithm>\n#include <bit>\n#include <cassert>\n#include <map>\n#include <stack>\n/**\n * @brief Undo可能Union-Find\n * @details Implement (union by size)\n *\n * @see https://ei1333.github.io/luzhiled/snippets/structure/union-find.html\n */\nstruct undo_union_find {\n undo_union_find() : data(), history() {}\n undo_union_find(int _n) : data(_n, -1), history() {}\n int root(int x) const { return data[x] < 0 ? x : root(data[x]); }\n int get_root(int k) const { return root(k); }\n bool is_root(int k) const { return data[k] < 0; }\n bool same(int x, int y) const { return root(x) == root(y); }\n bool is_same(int x, int y) const { return same(x, y); }\n int size(int k) const { return -data[root(k)]; }\n int get_size(int k) const { return size(k); }\n bool unite(int x, int y) {\n x = root(x), y = root(y);\n history.emplace(x, data[x]);\n history.emplace(y, data[y]);\n if (x == y) return false;\n if (data[x] > data[y]) std::swap(x, y);\n data[x] += data[y];\n data[y] = x;\n return true;\n }\n void undo() {\n data[history.top().first] = history.top().second;\n history.pop();\n data[history.top().first] = history.top().second;\n history.pop();\n }\n int snapshot() const { return history.size(); }\n void rollback(int x = 0) {\n while ((int)(history.size()) > x) undo();\n }\n private:\n std::vector<int> data;\n std::stack<std::pair<int, int>> history;\n};\n/// @brief オフライン動的連結性\nstruct offline_dynamic_connectivity {\n offline_dynamic_connectivity(int n, int q) : _q(q), uf(n) {\n _size = std::bit_ceil<unsigned>(_q);\n _log = std::countr_zero<unsigned>(_size);\n seg = std::vector(_size << 1, std::vector<std::pair<int, int>>());\n }\n void add(int t, int u, int v) {\n auto p = std::minmax(u, v);\n mp[p].emplace_back(t, 0);\n }\n void erase(int t, int u, int v) {\n auto p = std::minmax(u, v);\n mp[p].emplace_back(t, 1);\n }\n void build() {\n for (auto [p, v] : mp) {\n std::sort(v.begin(), v.end());\n int s = 0;\n int last = 0;\n for (auto q : v) {\n if (q.second == 0) {\n if (s++ == 0) last = q.first;\n } else {\n assert(s > 0);\n if (--s == 0) apply(last, q.first, p);\n }\n }\n if (s) apply(last, _q, p);\n }\n }\n template <class F>\n void solve(F &&query, int k = 1) {\n int state = uf.snapshot();\n for (auto [u, v] : seg[k]) uf.unite(u, v);\n if (k < _size) {\n solve(query, 2 * k);\n solve(query, 2 * k + 1);\n } else if (k < _size + _q) {\n query(k - _size);\n }\n uf.rollback(state);\n }\n bool same(int u, int v) const { return uf.same(u, v); }\n private:\n int _q, _size, _log;\n std::vector<std::vector<std::pair<int, int>>> seg;\n std::map<std::pair<int, int>, std::vector<std::pair<int, int>>> mp;\n undo_union_find uf;\n void apply(int l, int r, std::pair<int, int> p) {\n assert(0 <= l && l <= _q);\n assert(0 <= r && r <= _q);\n l += _size, r += _size;\n for (; l < r; l >>= 1, r >>= 1) {\n if (l & 1) seg[l++].emplace_back(p);\n if (r & 1) seg[--r].emplace_back(p);\n }\n }\n};\nint main(void) {\n int n, q;\n std::cin >> n >> q;\n offline_dynamic_connectivity dc(n, q);\n std::vector<std::tuple<int, int, int>> v(q);\n for (int i = 0; i < q; ++i) {\n auto &[a, b, c] = v[i];\n std::cin >> a >> b >> c;\n if (a == 1) {\n dc.add(i, b, c);\n } else if (a == 2) {\n dc.erase(i, b, c);\n }\n }\n dc.build();\n std::vector<int> ans(q);\n auto f = [&](int x) {\n auto [a, b, c] = v[x];\n if (a == 3)\n ans[x] = dc.same(b, c);\n };\n dc.solve(f);\n for (int i = 0; i < q; ++i) {\n auto [a, b, c] = v[i];\n if (a == 3) {\n std::cout << (ans[i] ? \"YES\\n\" : \"NO\\n\");\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 14984, "score_of_the_acc": -0.1478, "final_rank": 4 }, { "submission_id": "aoj_2235_10207008", "code_snippet": "// AOJ #2235\n// Graph Construction 2025.2.9\n\n#include <bits/stdc++.h>\nusing namespace std;\n \nstruct DSU {\n vector<int> parent;\n vector<int> size;\n vector<tuple<int,int,int>> history;\n \n DSU(int n) {\n parent.resize(n);\n size.resize(n, 1);\n for (int i = 0; i < n; i++) parent[i] = i;\n }\n \n int find(int a) {\n while(a != parent[a]) a = parent[a];\n return a;\n }\n \n bool unionSet(int a, int b) {\n a = find(a);\n b = find(b);\n if(a == b) return false;\n if(size[a] < size[b]) swap(a, b);\n history.push_back({a, b, size[a]});\n parent[b] = a;\n size[a] += size[b];\n return true;\n }\n \n void rollback(int checkpoint) {\n while((int)history.size() > checkpoint) {\n auto [a, b, oldSize] = history.back();\n history.pop_back();\n size[a] = oldSize;\n parent[b] = b;\n }\n }\n};\n \nstruct Event {\n int type;\n int u, v;\n};\n \nstruct Edge {\n int u, v;\n};\n \nstruct EdgeInterval {\n int L, R;\n int u, v;\n};\n \nint n, k;\nvector<Event> events;\n \nvector<vector<Edge>> segTree;\n \nvoid addEdge(int idx, int l, int r, int L, int R, Edge edge) {\n if(r <= L || R <= l) return;\n if(L <= l && r <= R) {\n segTree[idx].push_back(edge);\n return;\n }\n int mid = (l + r) / 2;\n addEdge(idx * 2 + 1, l, mid, L, R, edge);\n addEdge(idx * 2 + 2, mid, r, L, R, edge);\n}\n \nvector<string> answers;\nvoid dfs(int idx, int l, int r, DSU &dsu) {\n int checkpoint = dsu.history.size();\n for(auto &edge : segTree[idx])\n dsu.unionSet(edge.u, edge.v);\n \n if(r - l == 1) {\n if(events[l].type == 3) {\n int u = events[l].u, v = events[l].v;\n answers.push_back(dsu.find(u) == dsu.find(v) ? \"YES\" : \"NO\");\n }\n } else {\n int mid = (l + r) / 2;\n dfs(idx * 2 + 1, l, mid, dsu);\n dfs(idx * 2 + 2, mid, r, dsu);\n }\n dsu.rollback(checkpoint);\n}\n \nint main() {\n ios::sync_with_stdio(0);\n cin.tie(0);\n \n cin >> n >> k;\n events.resize(k);\n for (int i = 0; i < k; i++){\n int t, u, v;\n cin >> t >> u >> v;\n events[i] = {t, u, v};\n }\n \n map<pair<int,int>, int> edgeStart;\n vector<EdgeInterval> intervals;\n for (int i = 0; i < k; i++){\n int t = events[i].type, u = events[i].u, v = events[i].v;\n if(t == 1) edgeStart[{u,v}] = i;\n else if(t == 2){\n auto it = edgeStart.find({u,v});\n if(it != edgeStart.end()){\n int startTime = it->second;\n intervals.push_back({startTime, i, u, v});\n edgeStart.erase(it);\n }\n }\n }\n for(auto &p : edgeStart){\n int startTime = p.second;\n int u = p.first.first, v = p.first.second;\n intervals.push_back({startTime, k, u, v});\n }\n \n segTree.resize(4 * k);\n for(auto &interv : intervals){\n Edge e = {interv.u, interv.v};\n addEdge(0, 0, k, interv.L, interv.R, e);\n }\n DSU dsu(n);\n dfs(0, 0, k, dsu);\n for(auto &ans : answers) cout << ans << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 14044, "score_of_the_acc": -0.1075, "final_rank": 3 }, { "submission_id": "aoj_2235_10068933", "code_snippet": "#include <bits/stdc++.h>\n#define ll long long\n#define INF 1000000005\n#define MOD 1000000007\n#define EPS 1e-10\n#define rep(i,n) for(int i=0;i<(int)(n);++i)\n#define rrep(i,n) for(int i=(int)(n)-1;i>=0;--i)\n#define srep(i,s,t) for(int i=(int)(s);i<(int)(t);++i)\n#define each(a,b) for(auto& (a): (b))\n#define all(v) (v).begin(),(v).end()\n#define len(v) (int)(v).size()\n#define zip(v) sort(all(v)),v.erase(unique(all(v)),v.end())\n#define cmx(x,y) x=max(x,y)\n#define cmn(x,y) x=min(x,y)\n#define fi first\n#define se second\n#define pb push_back\n#define show(x) cout<<#x<<\" = \"<<(x)<<endl\n#define sar(a,n) cout<<#a<<\":\";rep(pachico,n)cout<<\" \"<<a[pachico];cout<<endl\n\nusing namespace std;\n\ntemplate<typename S,typename T>auto&operator<<(ostream&o,pair<S,T>p){return o<<\"{\"<<p.fi<<\",\"<<p.se<<\"}\";}\ntemplate<typename T>auto&operator<<(ostream&o,set<T>s){for(auto&e:s)o<<e<<\" \";return o;}\ntemplate<typename S,typename T,typename U>\nauto&operator<<(ostream&o,priority_queue<S,T,U>q){while(!q.empty())o<<q.top()<<\" \",q.pop();return o;}\ntemplate<typename K,typename T>auto&operator<<(ostream&o,map<K,T>m){for(auto&e:m)o<<e<<\" \";return o;}\ntemplate<typename T>auto&operator<<(ostream&o,vector<T>v){for(auto&e:v)o<<e<<\" \";return o;}\nvoid ashow(){cout<<endl;}template<typename T,typename...A>void ashow(T t,A...a){cout<<t<<\" \";ashow(a...);}\n\ntypedef pair<int,int> P;\ntypedef pair<ll,ll> pll;\ntypedef vector<int> vi;\ntypedef vector<ll> vl;\ntypedef vector<vi> vvi;\ntypedef vector<vl> vvl;\ntypedef vector<P> vp;\ntypedef vector<double> vd;\ntypedef vector<string> vs;\n\nconst int MAX_N = 100005;\n\n// 多重辺は無いと仮定する\nclass BSTNode {\npublic:\n const int from, to;\n int sz;\n bool subtree_edge, subofftree_edge, exact_level;\n BSTNode *left, *right, *par;\n unordered_set<int> adjacent;\n BSTNode(const int _ver) noexcept :\n from(_ver), to(_ver), sz(0), subtree_edge(false), subofftree_edge(false),\n exact_level(false), left(nullptr), right(nullptr), par(nullptr){}\n BSTNode(const int _from, const int _to, const bool _flag) noexcept :\n from(_from), to(_to), sz(0), subtree_edge(false), subofftree_edge(false),\n exact_level((from < to) && _flag), left(nullptr), right(nullptr), par(nullptr){}\n inline bool IsRoot() const noexcept { return !par; }\n inline bool IsVertex() const noexcept { return (from == to); }\n inline void eval() noexcept {\n if(IsVertex()) sz = 1, subtree_edge = false, subofftree_edge = !adjacent.empty();\n else sz = 0, subtree_edge = exact_level, subofftree_edge = false;\n if(left){\n sz += left->sz, subtree_edge |= left->subtree_edge, subofftree_edge |= left->subofftree_edge;\n }\n if(right){\n sz += right->sz, subtree_edge |= right->subtree_edge, subofftree_edge |= right->subofftree_edge;\n }\n }\n inline bool subtree_edge_eval(){\n subtree_edge = exact_level;\n if(left) subtree_edge |= left->subtree_edge;\n if(right) subtree_edge |= right->subtree_edge;\n return subtree_edge;\n }\n inline bool subofftree_edge_eval(){\n subofftree_edge = !adjacent.empty();\n if(left) subofftree_edge |= left->subofftree_edge;\n if(right) subofftree_edge |= right->subofftree_edge;\n return subofftree_edge;\n }\n void rotate(const bool right_) noexcept {\n BSTNode *p = par, *g = p->par;\n if(right_){\n if((p->left = right)) right->par = p;\n right = p, p->par = this;\n }else{\n if((p->right = left)) left->par = p;\n left = p, p->par = this;\n }\n p->eval(), eval();\n if(!(par = g)) return;\n if(g->left == p) g->left = this;\n if(g->right == p) g->right = this;\n g->eval();\n }\n};\n\nBSTNode *splay(BSTNode *u) noexcept {\n if(!u) return nullptr;\n while(!(u->IsRoot())){\n BSTNode *p = u->par, *gp = p->par;\n if(p->IsRoot()){ // zig\n u->rotate((u == p->left));\n }else{\n bool flag = (u == p->left);\n if((u == p->left) == (p == gp->left)){ // zig-zig\n p->rotate(flag), u->rotate(flag);\n }else{ // zig-zag\n u->rotate(flag), u->rotate(!flag);\n }\n }\n }\n return u;\n}\n\nBSTNode *join(BSTNode *root1, BSTNode *root2) noexcept {\n if(!root1 || !root2) return root1 ? root1 : root2;\n BSTNode *cur = nullptr, *nx = root1;\n do{ cur = nx, nx = cur->right; }while(nx);\n BSTNode *ver = splay(cur);\n ver->right = root2, ver->eval(), root2->par = ver;\n return ver;\n}\n\nclass EulerTourTree {\npublic:\n struct pair_hash {\n template <class T1, class T2>\n size_t operator() (const pair<T1, T2>& p) const {\n size_t lhs = hash<T1>()(p.first), rhs = hash<T2>()(p.second);\n return lhs^(rhs+0x9e3779b9+(lhs<<6)+(lhs>>2));\n }\n };\n BSTNode** vertex_set;\n unordered_map<pair<int, int>, pair<BSTNode*, BSTNode*>, pair_hash> edge_set;\nprivate:\n BSTNode *reroot(BSTNode *ver) noexcept {\n BSTNode *res = splay(ver)->left;\n if(!res) return ver;\n ver->left = nullptr, ver->eval();\n while(ver->right) ver = ver->right;\n splay(ver), ver->right = res, ver->eval(), res->par = ver;\n return ver;\n }\n void link(BSTNode *ver1, BSTNode *ver2, const bool flag) noexcept {\n BSTNode *e1 = new BSTNode(ver1->from, ver2->from, flag);\n BSTNode *e2 = new BSTNode(ver2->from, ver1->from, flag);\n edge_set[{ver1->from, ver2->from}] = {e1, e2};\n join(join(reroot(ver1), e1), join(reroot(ver2), e2));\n }\n void cut(BSTNode *edge1, BSTNode *edge2) noexcept {\n splay(edge1), splay(edge2);\n BSTNode *p = edge1->par;\n bool _right = (edge1 == edge2->right);\n if(p != edge2){\n _right = (p == edge2->right);\n p->par = nullptr, edge1->rotate((edge1 == p->left));\n }\n if(edge1->left) edge1->left->par = nullptr;\n if(edge1->right) edge1->right->par = nullptr;\n if(_right){\n if(edge2->left) edge2->left->par = nullptr;\n join(edge2->left, edge1->right);\n }else{\n if(edge2->right) edge2->right->par = nullptr;\n join(edge1->left, edge2->right);\n }\n delete edge1; delete edge2;\n }\n bool connected(BSTNode *ver1, BSTNode *ver2) noexcept {\n splay(ver1), splay(ver2);\n return ver1->par;\n }\n int component_size(BSTNode *ver) noexcept { return splay(ver)->sz; }\npublic:\n int V;\n // ~EulerTourTree(){\n // for(auto it : edge_set){\n // delete (it.second).first;\n // delete (it.second).second;\n // }\n // for(int i = 0; i < V; ++i) delete vertex_set[i];\n // delete[] vertex_set;\n // }\n EulerTourTree(const int node_size) noexcept {\n V = node_size, vertex_set = new BSTNode*[V];\n for(int i = 0; i < V; i++) vertex_set[i] = new BSTNode(i);\n }\n void reroot(const int node_id) noexcept { reroot(vertex_set[node_id]); }\n void link(int node1_id, int node2_id, bool flag=true) noexcept {\n if(node1_id > node2_id) swap(node1_id, node2_id);\n link(vertex_set[node1_id], vertex_set[node2_id], flag);\n }\n void cut(int node1_id, int node2_id){\n if(node1_id > node2_id) swap(node1_id, node2_id);\n auto it = edge_set.find({node1_id, node2_id});\n assert(it != edge_set.end());\n BSTNode *edge1 = (it->second).first, *edge2 = (it->second).second;\n edge_set.erase(it);\n cut(edge1, edge2);\n }\n bool connected(const int node1_id, const int node2_id) noexcept {\n if(node1_id == node2_id) return true;\n return connected(vertex_set[node1_id], vertex_set[node2_id]);\n }\n int component_size(int node_id) noexcept { return component_size(vertex_set[node_id]); }\n void check_dfs(const BSTNode* cur) const noexcept {\n if(cur->left) check_dfs(cur->left);\n cout << \"{\" << (cur->from) << \",\" << (cur->to) << \"} \";\n if(cur->right) check_dfs(cur->right);\n }\n};\n\n// 頂点ノードにそこから出る level i の off tree edge の集合(両方)を乗せる\n// 頂点ノードには map でアクセス可能\n// link のとき tree edge ならそのまま link\n// off tree edge なら level 0 の頂点ノードにそいつを追加\n// cut のとき level i の tree edge を level i+1 に押し上げるのだが\n// 各 layer の Euler Tree は level i 以上の edge からなるのでその辺が exact に level i の辺であるかをもつ必要がある.\n// 基本的に部分木の情報を書き換えるときは(splay してから変更する)根にしてから反映しないとマズい.\n// 保持している offtree edge って offtree edge の名の通り常にその両端点は同じ連結成分に属する.\n\nclass DynamicConnectivity {\nprivate:\n BSTNode *level_up_dfs(BSTNode *cur, const int layer) noexcept {\n if(cur->exact_level){\n splay(cur)->exact_level = false, cur->subtree_edge_eval();\n detect_layer[{cur->from, cur->to}]++, et[layer+1].link(cur->from, cur->to);\n return cur;\n }\n if(cur->left && cur->left->subtree_edge) return level_up_dfs(cur->left, layer);\n if(cur->right && cur->right->subtree_edge) return level_up_dfs(cur->right, layer);\n return nullptr;\n }\n // another は (u, v) を削除しているけどそのうち larger connected component に属する方\n BSTNode *search_edge_dfs\n (BSTNode *cur, const int layer, const int another, bool& flag, pair<int, int>& rep_edge) noexcept {\n // off_tree edge がある\n if(!cur->adjacent.empty()){\n bool state = et[layer+1].vertex_set[cur->from]->adjacent.empty();\n for(auto it = cur->adjacent.begin(); it != cur->adjacent.end();){\n pair<int, int> e = {min(cur->from, *it), max(cur->from, *it)};\n BSTNode *correspond = et[layer].vertex_set[*it];\n if(et[layer].connected(another, *it)){\n // another と今見ている先が connected つまり, restore する edge なら\n flag = true, rep_edge = e;\n cur->adjacent.erase(it), correspond->adjacent.erase(cur->from);\n // correspond の方で subofftree フラグが壊れるのを防ぐ.\n if(!correspond->subofftree_edge_eval()){\n splay(correspond)->subofftree_edge_eval();\n }\n break;\n }else{\n // 今見ている connected component をつなぐ edge なら suboff_tree edge の情報変える\n if(!et[layer+1].vertex_set[*it]->subofftree_edge_eval()){\n splay(et[layer+1].vertex_set[*it])->subofftree_edge = true;\n }\n et[layer+1].vertex_set[cur->from]->adjacent.insert(*it);\n et[layer+1].vertex_set[*it]->adjacent.insert(cur->from);\n detect_layer[e]++, it = cur->adjacent.erase(it);\n correspond->adjacent.erase(cur->from);\n if(!correspond->subofftree_edge_eval()){\n splay(correspond)->subofftree_edge_eval();\n }\n }\n }\n // 元々 ajacent が empty で off_tree edge が無かったのに今 cur->from から出る adjacent がある場合\n // でかつ left, right の off_tree edge が false なら subofftree_edge を true にする(これ何してんの?)\n // left もしくは right の off_tree edge が true なら根まで offtree_edge ありますよの伝播がちゃんと行っているので\n // ajacent list が新しく non empty になったけど別に更新する必要はないな\n if(state && !et[layer+1].vertex_set[cur->from]->subtree_edge_eval()){\n splay(et[layer+1].vertex_set[cur->from])->subofftree_edge = true;\n }\n splay(cur)->subofftree_edge_eval();\n return cur;\n }\n if(cur->left && cur->left->subofftree_edge){\n return search_edge_dfs(cur->left, layer, another, flag, rep_edge);\n }\n if(cur->right && cur->right->subofftree_edge){\n return search_edge_dfs(cur->right, layer, another, flag, rep_edge);\n }\n return nullptr;\n }\n bool replace(const int from, const int to, const int layer) noexcept {\n if(layer < 0) return true;\n int u, v;\n if(et[layer].component_size(from) <= et[layer].component_size(to)) u = from, v = to;\n else u = to, v = from;\n BSTNode *ver = splay(et[layer].vertex_set[u]);\n // smaller connected component に tree edge があるならそれの level を 1 個上げる\n while(ver->subtree_edge) ver = level_up_dfs(ver, layer);\n pair<int, int> rep_edge = {-1, -1};\n bool flag = false;\n // smaller connected component に offtree edge があり,\n // その辺が連結成分を再びつなげるようなものでないなら 1 増やす\n // flag 見つけたらすぐ終了するという実装にしてるけどそれでもならしが回るので問題ない\n // 1 回探索する度に restore edge が見つかって探索が終了する or 何かの辺の level が 1 上がる が言えるので\n while(ver->subofftree_edge){\n ver = search_edge_dfs(ver, layer, v, flag, rep_edge);\n if(flag) break;\n }\n if(flag){\n // restore edge を見つけたなら\n et[layer].link(rep_edge.first, rep_edge.second);\n for(int i = 0; i < layer; i++) et[i].link(rep_edge.first, rep_edge.second, false);\n return false;\n }else return replace(from, to, layer-1);\n }\npublic:\n const int V;\n int depth;\n vector<EulerTourTree> et;\n unordered_map<pair<int, int>, int, EulerTourTree::pair_hash> detect_layer;\n DynamicConnectivity(const int node_size) noexcept : V(node_size), depth(1){\n et.emplace_back(V);\n }\n // ~DynamicConnectivity(){\n // delete[] et;\n // }\n bool link(int node1_id, int node2_id) noexcept {\n if(node1_id > node2_id) swap(node1_id, node2_id);\n detect_layer[{node1_id, node2_id}] = 0;\n if(et[0].connected(node1_id, node2_id)){\n BSTNode *ver1 = et[0].vertex_set[node1_id], *ver2 = et[0].vertex_set[node2_id];\n splay(ver1)->subofftree_edge = true, ver1->adjacent.insert(node2_id);\n splay(ver2)->subofftree_edge = true, ver2->adjacent.insert(node1_id);\n return false;\n }else{\n et[0].link(node1_id, node2_id);\n return true;\n }\n }\n bool cut(int node1_id, int node2_id) noexcept {\n if(node1_id > node2_id) swap(node1_id, node2_id);\n auto it = detect_layer.find({node1_id, node2_id});\n assert(it != detect_layer.end());\n int layer = it->second;\n detect_layer.erase(it);\n auto& st = et[layer].vertex_set[node1_id]->adjacent;\n if(st.find(node2_id) == st.end()){\n // tree edge の場合\n for(int i = 0; i <= layer; i++) et[i].cut(node1_id, node2_id);\n if(layer + 1 == depth) ++depth, et.emplace_back(V);\n return replace(node1_id, node2_id, layer);\n }else{\n // off_tree edge の場合\n et[layer].vertex_set[node1_id]->adjacent.erase(node2_id);\n // 自分より下に node1_id より下に off tree_edge があるか\n // (もしなかったらこれより上のノードについての off tree edge があるかどうかの情報を書き換える必要がある)\n if(!et[layer].vertex_set[node1_id]->subofftree_edge_eval()){\n splay(et[layer].vertex_set[node1_id])->subofftree_edge_eval();\n }\n et[layer].vertex_set[node2_id]->adjacent.erase(node1_id);\n if(!et[layer].vertex_set[node2_id]->subofftree_edge_eval()){\n splay(et[layer].vertex_set[node2_id])->subofftree_edge_eval();\n }\n return false;\n }\n }\n bool connected(const int node1_id, const int node2_id) noexcept {\n return et[0].connected(node1_id, node2_id);\n }\n};\n\n#define getchar getchar_unlocked\n#define putchar putchar_unlocked\n\ninline int in() {\n int n = 0; short c;\n while ((c = getchar()) >= '0') n = n * 10 + c - '0';\n return n;\n}\n\ninline void out(int n) {\n short res[10], i = 0;\n do { res[i++] = n % 10, n /= 10; } while (n);\n while (i) putchar(res[--i] + '0');\n putchar('\\n');\n}\n\nint main()\n{\n int n = in(), m = in();\n DynamicConnectivity dc(n);\n rep(i,m){\n int a,b,c;\n a = in(), b = in(), c = in();\n if(a == 1){\n dc.link(b, c);\n }else if(a == 2){\n dc.cut(b, c);\n }else{\n if(dc.connected(b,c)){\n cout << \"YES\\n\";\n }else{\n cout << \"NO\\n\";\n }\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 230, "memory_kb": 76224, "score_of_the_acc": -1.3612, "final_rank": 12 }, { "submission_id": "aoj_2235_10068758", "code_snippet": "#include <bits/stdc++.h>\n#define ll long long\n#define INF 1000000005\n#define MOD 1000000007\n#define EPS 1e-10\n#define rep(i,n) for(int i=0;i<(int)(n);++i)\n#define rrep(i,n) for(int i=(int)(n)-1;i>=0;--i)\n#define srep(i,s,t) for(int i=(int)(s);i<(int)(t);++i)\n#define each(a,b) for(auto& (a): (b))\n#define all(v) (v).begin(),(v).end()\n#define len(v) (int)(v).size()\n#define zip(v) sort(all(v)),v.erase(unique(all(v)),v.end())\n#define cmx(x,y) x=max(x,y)\n#define cmn(x,y) x=min(x,y)\n#define fi first\n#define se second\n#define pb push_back\n#define show(x) cout<<#x<<\" = \"<<(x)<<endl\n#define sar(a,n) cout<<#a<<\":\";rep(pachico,n)cout<<\" \"<<a[pachico];cout<<endl\n\nusing namespace std;\n\ntemplate<typename S,typename T>auto&operator<<(ostream&o,pair<S,T>p){return o<<\"{\"<<p.fi<<\",\"<<p.se<<\"}\";}\ntemplate<typename T>auto&operator<<(ostream&o,set<T>s){for(auto&e:s)o<<e<<\" \";return o;}\ntemplate<typename S,typename T,typename U>\nauto&operator<<(ostream&o,priority_queue<S,T,U>q){while(!q.empty())o<<q.top()<<\" \",q.pop();return o;}\ntemplate<typename K,typename T>auto&operator<<(ostream&o,map<K,T>m){for(auto&e:m)o<<e<<\" \";return o;}\ntemplate<typename T>auto&operator<<(ostream&o,vector<T>v){for(auto&e:v)o<<e<<\" \";return o;}\nvoid ashow(){cout<<endl;}template<typename T,typename...A>void ashow(T t,A...a){cout<<t<<\" \";ashow(a...);}\n\ntypedef pair<int,int> P;\ntypedef pair<ll,ll> pll;\ntypedef vector<int> vi;\ntypedef vector<ll> vl;\ntypedef vector<vi> vvi;\ntypedef vector<vl> vvl;\ntypedef vector<P> vp;\ntypedef vector<double> vd;\ntypedef vector<string> vs;\n\nconst int MAX_N = 100005;\n\n// 多重辺は無いと仮定する\nclass BSTNode {\npublic:\n const int from, to;\n int sz;\n bool subtree_edge, subofftree_edge, exact_level;\n BSTNode *left, *right, *par;\n unordered_set<int> adjacent;\n BSTNode(const int _ver) noexcept :\n from(_ver), to(_ver), sz(0), subtree_edge(false), subofftree_edge(false),\n exact_level(false), left(nullptr), right(nullptr), par(nullptr){}\n BSTNode(const int _from, const int _to, const bool _flag) noexcept :\n from(_from), to(_to), sz(0), subtree_edge(false), subofftree_edge(false),\n exact_level((from < to) && _flag), left(nullptr), right(nullptr), par(nullptr){}\n inline bool IsRoot() const noexcept { return !par; }\n inline bool IsVertex() const noexcept { return (from == to); }\n inline void eval() noexcept {\n if(IsVertex()) sz = 1, subtree_edge = false, subofftree_edge = !adjacent.empty();\n else sz = 0, subtree_edge = exact_level, subofftree_edge = false;\n if(left){\n sz += left->sz, subtree_edge |= left->subtree_edge, subofftree_edge |= left->subofftree_edge;\n }\n if(right){\n sz += right->sz, subtree_edge |= right->subtree_edge, subofftree_edge |= right->subofftree_edge;\n }\n }\n inline void subtree_edge_eval(){\n subtree_edge = exact_level;\n if(left) subtree_edge |= left->subtree_edge;\n if(right) subtree_edge |= right->subtree_edge;\n }\n inline void subofftree_edge_eval(){\n subofftree_edge = !adjacent.empty();\n if(left) subofftree_edge |= left->subofftree_edge;\n if(right) subofftree_edge |= right->subofftree_edge;\n }\n inline bool subofftree_check(){\n return !adjacent.empty() ||\n (left ? left->subofftree_edge : false) || (right ? right->subofftree_edge : false);\n }\n inline bool offtree_check(){\n return adjacent.empty() ||\n (left ? left->subofftree_edge : false) || (right ? right->subofftree_edge : false);\n }\n void rotate(const bool right_) noexcept {\n BSTNode *p = par, *g = p->par;\n if(right_){\n if((p->left = right)) right->par = p;\n right = p, p->par = this;\n }else{\n if((p->right = left)) left->par = p;\n left = p, p->par = this;\n }\n p->eval(), eval();\n if(!(par = g)) return;\n if(g->left == p) g->left = this;\n if(g->right == p) g->right = this;\n g->eval();\n }\n};\n\nBSTNode *splay(BSTNode *u) noexcept {\n if(!u) return nullptr;\n while(!(u->IsRoot())){\n BSTNode *p = u->par, *gp = p->par;\n if(p->IsRoot()){ // zig\n u->rotate((u == p->left));\n }else{\n bool flag = (u == p->left);\n if((u == p->left) == (p == gp->left)){ // zig-zig\n p->rotate(flag), u->rotate(flag);\n }else{ // zig-zag\n u->rotate(flag), u->rotate(!flag);\n }\n }\n }\n return u;\n}\n\nBSTNode *join(BSTNode *root1, BSTNode *root2) noexcept {\n if(!root1 || !root2) return root1 ? root1 : root2;\n BSTNode *cur = nullptr, *nx = root1;\n do{ cur = nx, nx = cur->right; }while(nx);\n BSTNode *ver = splay(cur);\n ver->right = root2, ver->eval(), root2->par = ver;\n return ver;\n}\n\nclass EulerTourTree {\npublic:\n struct pair_hash {\n template <class T1, class T2>\n size_t operator() (const pair<T1, T2>& p) const {\n size_t lhs = hash<T1>()(p.first), rhs = hash<T2>()(p.second);\n return lhs^(rhs+0x9e3779b9+(lhs<<6)+(lhs>>2));\n }\n };\n BSTNode** vertex_set;\n unordered_map<pair<int, int>, pair<BSTNode*, BSTNode*>, pair_hash> edge_set;\nprivate:\n BSTNode *reroot(BSTNode *ver) noexcept {\n BSTNode *res = splay(ver)->left;\n if(!res) return ver;\n ver->left = nullptr, ver->eval();\n while(ver->right) ver = ver->right;\n splay(ver), ver->right = res, ver->eval(), res->par = ver;\n return ver;\n }\n void link(BSTNode *ver1, BSTNode *ver2, const bool flag) noexcept {\n BSTNode *e1 = new BSTNode(ver1->from, ver2->from, flag);\n BSTNode *e2 = new BSTNode(ver2->from, ver1->from, flag);\n edge_set[{ver1->from, ver2->from}] = {e1, e2};\n join(join(reroot(ver1), e1), join(reroot(ver2), e2));\n }\n void cut(BSTNode *edge1, BSTNode *edge2) noexcept {\n splay(edge1), splay(edge2);\n BSTNode *p = edge1->par;\n bool _right = (edge1 == edge2->right);\n if(p != edge2){\n _right = (p == edge2->right);\n p->par = nullptr, edge1->rotate((edge1 == p->left));\n }\n if(edge1->left) edge1->left->par = nullptr;\n if(edge1->right) edge1->right->par = nullptr;\n if(_right){\n if(edge2->left) edge2->left->par = nullptr;\n join(edge2->left, edge1->right);\n }else{\n if(edge2->right) edge2->right->par = nullptr;\n join(edge1->left, edge2->right);\n }\n delete edge1; delete edge2;\n }\n bool connected(BSTNode *ver1, BSTNode *ver2) noexcept {\n splay(ver1), splay(ver2);\n return ver1->par;\n }\n int component_size(BSTNode *ver) noexcept { return splay(ver)->sz; }\npublic:\n int V;\n // ~EulerTourTree(){\n // for(auto it : edge_set){\n // delete (it.second).first;\n // delete (it.second).second;\n // }\n // for(int i = 0; i < V; ++i) delete vertex_set[i];\n // delete[] vertex_set;\n // }\n EulerTourTree(const int node_size) noexcept {\n V = node_size, vertex_set = new BSTNode*[V];\n for(int i = 0; i < V; i++) vertex_set[i] = new BSTNode(i);\n }\n void reroot(const int node_id) noexcept { reroot(vertex_set[node_id]); }\n void link(int node1_id, int node2_id, bool flag=true) noexcept {\n if(node1_id > node2_id) swap(node1_id, node2_id);\n link(vertex_set[node1_id], vertex_set[node2_id], flag);\n }\n void cut(int node1_id, int node2_id){\n if(node1_id > node2_id) swap(node1_id, node2_id);\n auto it = edge_set.find({node1_id, node2_id});\n assert(it != edge_set.end());\n BSTNode *edge1 = (it->second).first, *edge2 = (it->second).second;\n edge_set.erase(it);\n cut(edge1, edge2);\n }\n bool connected(const int node1_id, const int node2_id) noexcept {\n if(node1_id == node2_id) return true;\n return connected(vertex_set[node1_id], vertex_set[node2_id]);\n }\n int component_size(int node_id) noexcept { return component_size(vertex_set[node_id]); }\n void check_dfs(const BSTNode* cur) const noexcept {\n if(cur->left) check_dfs(cur->left);\n cout << \"{\" << (cur->from) << \",\" << (cur->to) << \"} \";\n if(cur->right) check_dfs(cur->right);\n }\n};\n\n// 頂点ノードにそこから出る level i の off tree edge の集合(両方)を乗せる\n// 頂点ノードには map でアクセス可能\n// link のとき tree edge ならそのまま link\n// off tree edge なら level 0 の頂点ノードにそいつを追加\n// cut のとき level i の tree edge を level i+1 に押し上げるのだが\n// 各 layer の Euler Tree は level i 以上の edge からなるのでその辺が exact に level i の辺であるかをもつ必要がある.\n// 基本的に部分木の情報を書き換えるときは(splay してから変更する)根にしてから反映しないとマズい.\n// 保持している offtree edge って offtree edge の名の通り常にその両端点は同じ連結成分に属する.\n\nclass DynamicConnectivity {\nprivate:\n BSTNode *level_up_dfs(BSTNode *cur, const int layer) noexcept {\n if(cur->exact_level){\n splay(cur)->exact_level = false, cur->subtree_edge_eval();\n detect_layer[{cur->from, cur->to}]++, et[layer+1].link(cur->from, cur->to);\n return cur;\n }\n if(cur->left && cur->left->subtree_edge) return level_up_dfs(cur->left, layer);\n if(cur->right && cur->right->subtree_edge) return level_up_dfs(cur->right, layer);\n return nullptr;\n }\n // another は (u, v) を削除しているけどそのうち larger connected component に属する方\n BSTNode *search_edge_dfs\n (BSTNode *cur, const int layer, const int another, bool& flag, pair<int, int>& rep_edge) noexcept {\n // off_tree edge がある\n assert(cur->left == cur->right);\n if(!cur->adjacent.empty()){\n bool state = et[layer+1].vertex_set[cur->from]->adjacent.empty();\n for(auto it = cur->adjacent.begin(); it != cur->adjacent.end();){\n pair<int, int> e = {min(cur->from, *it), max(cur->from, *it)};\n BSTNode *correspond = et[layer].vertex_set[*it];\n if(et[layer].connected(another, *it)){\n // another と今見ている先が connected つまり, restore する edge なら\n flag = true, rep_edge = e;\n cur->adjacent.erase(it), correspond->adjacent.erase(cur->from);\n // correspond の方で subofftree フラグが壊れるのを防ぐ.\n if(!correspond->subofftree_check()){\n splay(correspond)->subofftree_edge_eval();\n }\n break;\n }else{\n // 今見ている connected component をつなぐ edge なら suboff_tree edge の情報変える\n if(!et[layer+1].vertex_set[*it]->subofftree_check()){\n splay(et[layer+1].vertex_set[*it])->subofftree_edge = true;\n }\n et[layer+1].vertex_set[cur->from]->adjacent.insert(*it);\n et[layer+1].vertex_set[*it]->adjacent.insert(cur->from);\n detect_layer[e]++, it = cur->adjacent.erase(it);\n correspond->adjacent.erase(cur->from);\n if(!correspond->subofftree_check()){\n splay(correspond)->subofftree_edge_eval();\n }\n }\n }\n // 元々 ajacent が empty で off_tree edge が無かったのに今 cur->from から出る adjacent がある場合\n // でかつ left, right の off_tree edge が false なら subofftree_edge を true にする(これ何してんの?)\n // left もしくは right の off_tree edge が true なら根まで offtree_edge ありますよの伝播がちゃんと行っているので\n // ajacent list が新しく non empty になったけど別に更新する必要はないな\n if(state && !et[layer+1].vertex_set[cur->from]->offtree_check()){\n splay(et[layer+1].vertex_set[cur->from])->subofftree_edge = true;\n }\n splay(cur)->subofftree_edge_eval();\n return cur;\n }\n if(cur->left && cur->left->subofftree_edge){\n return search_edge_dfs(cur->left, layer, another, flag, rep_edge);\n }\n if(cur->right && cur->right->subofftree_edge){\n return search_edge_dfs(cur->right, layer, another, flag, rep_edge);\n }\n return nullptr;\n }\n bool replace(const int from, const int to, const int layer) noexcept {\n if(layer < 0) return true;\n int u, v;\n if(et[layer].component_size(from) <= et[layer].component_size(to)) u = from, v = to;\n else u = to, v = from;\n BSTNode *ver = splay(et[layer].vertex_set[u]);\n // smaller connected component に tree edge があるならそれの level を 1 個上げる\n while(ver->subtree_edge) ver = level_up_dfs(ver, layer);\n pair<int, int> rep_edge = {-1, -1};\n bool flag = false;\n // smaller connected component に offtree edge があり,\n // その辺が連結成分を再びつなげるようなものでないなら 1 増やす\n // flag 見つけたらすぐ終了するという実装にしてるけどそれでもならしが回るので問題ない\n // 1 回探索する度に restore edge が見つかって探索が終了する or 何かの辺の level が 1 上がる が言えるので\n while(ver->subofftree_edge){\n ver = search_edge_dfs(ver, layer, v, flag, rep_edge);\n if(flag) break;\n }\n if(flag){\n // restore edge を見つけたなら\n et[layer].link(rep_edge.first, rep_edge.second);\n for(int i = 0; i < layer; i++) et[i].link(rep_edge.first, rep_edge.second, false);\n return false;\n }else return replace(from, to, layer-1);\n }\npublic:\n const int V;\n int depth;\n vector<EulerTourTree> et;\n unordered_map<pair<int, int>, int, EulerTourTree::pair_hash> detect_layer;\n DynamicConnectivity(const int node_size) noexcept : V(node_size), depth(1){\n et.emplace_back(V);\n }\n // ~DynamicConnectivity(){\n // delete[] et;\n // }\n bool link(int node1_id, int node2_id) noexcept {\n if(node1_id > node2_id) swap(node1_id, node2_id);\n detect_layer[{node1_id, node2_id}] = 0;\n if(et[0].connected(node1_id, node2_id)){\n BSTNode *ver1 = et[0].vertex_set[node1_id], *ver2 = et[0].vertex_set[node2_id];\n splay(ver1)->subofftree_edge = true, ver1->adjacent.insert(node2_id);\n splay(ver2)->subofftree_edge = true, ver2->adjacent.insert(node1_id);\n return false;\n }else{\n et[0].link(node1_id, node2_id);\n return true;\n }\n }\n bool cut(int node1_id, int node2_id) noexcept {\n if(node1_id > node2_id) swap(node1_id, node2_id);\n auto it = detect_layer.find({node1_id, node2_id});\n assert(it != detect_layer.end());\n int layer = it->second;\n detect_layer.erase(it);\n auto& st = et[layer].vertex_set[node1_id]->adjacent;\n if(st.find(node2_id) == st.end()){\n // tree edge の場合\n for(int i = 0; i <= layer; i++) et[i].cut(node1_id, node2_id);\n if(layer + 1 == depth) ++depth, et.emplace_back(V);\n return replace(node1_id, node2_id, layer);\n }else{\n // off_tree edge の場合\n et[layer].vertex_set[node1_id]->adjacent.erase(node2_id);\n // 自分より下に node1_id より下に off tree_edge があるか\n // (もしなかったらこれより上のノードについての off tree edge があるかどうかの情報を書き換える必要がある)\n if(!et[layer].vertex_set[node1_id]->subofftree_check()){\n splay(et[layer].vertex_set[node1_id])->subofftree_edge_eval();\n }\n et[layer].vertex_set[node2_id]->adjacent.erase(node1_id);\n if(!et[layer].vertex_set[node2_id]->subofftree_check()){\n splay(et[layer].vertex_set[node2_id])->subofftree_edge_eval();\n }\n return false;\n }\n }\n bool connected(const int node1_id, const int node2_id) noexcept {\n return et[0].connected(node1_id, node2_id);\n }\n};\n\n#define getchar getchar_unlocked\n#define putchar putchar_unlocked\n\ninline int in() {\n int n = 0; short c;\n while ((c = getchar()) >= '0') n = n * 10 + c - '0';\n return n;\n}\n\ninline void out(int n) {\n short res[10], i = 0;\n do { res[i++] = n % 10, n /= 10; } while (n);\n while (i) putchar(res[--i] + '0');\n putchar('\\n');\n}\n\nint main()\n{\n int n = in(), m = in();\n DynamicConnectivity dc(n);\n rep(i,m){\n int a,b,c;\n a = in(), b = in(), c = in();\n if(a == 1){\n dc.link(b, c);\n }else if(a == 2){\n dc.cut(b, c);\n }else{\n if(dc.connected(b,c)){\n cout << \"YES\\n\";\n }else{\n cout << \"NO\\n\";\n }\n }\n }\n return 0;\n}", "accuracy": 0.3684210526315789, "time_ms": 10, "memory_kb": 11460, "score_of_the_acc": -0.0515, "final_rank": 19 }, { "submission_id": "aoj_2235_10068755", "code_snippet": "#include <bits/stdc++.h>\n#define ll long long\n#define INF 1000000005\n#define MOD 1000000007\n#define EPS 1e-10\n#define rep(i,n) for(int i=0;i<(int)(n);++i)\n#define rrep(i,n) for(int i=(int)(n)-1;i>=0;--i)\n#define srep(i,s,t) for(int i=(int)(s);i<(int)(t);++i)\n#define each(a,b) for(auto& (a): (b))\n#define all(v) (v).begin(),(v).end()\n#define len(v) (int)(v).size()\n#define zip(v) sort(all(v)),v.erase(unique(all(v)),v.end())\n#define cmx(x,y) x=max(x,y)\n#define cmn(x,y) x=min(x,y)\n#define fi first\n#define se second\n#define pb push_back\n#define show(x) cout<<#x<<\" = \"<<(x)<<endl\n#define sar(a,n) cout<<#a<<\":\";rep(pachico,n)cout<<\" \"<<a[pachico];cout<<endl\n\nusing namespace std;\n\ntemplate<typename S,typename T>auto&operator<<(ostream&o,pair<S,T>p){return o<<\"{\"<<p.fi<<\",\"<<p.se<<\"}\";}\ntemplate<typename T>auto&operator<<(ostream&o,set<T>s){for(auto&e:s)o<<e<<\" \";return o;}\ntemplate<typename S,typename T,typename U>\nauto&operator<<(ostream&o,priority_queue<S,T,U>q){while(!q.empty())o<<q.top()<<\" \",q.pop();return o;}\ntemplate<typename K,typename T>auto&operator<<(ostream&o,map<K,T>m){for(auto&e:m)o<<e<<\" \";return o;}\ntemplate<typename T>auto&operator<<(ostream&o,vector<T>v){for(auto&e:v)o<<e<<\" \";return o;}\nvoid ashow(){cout<<endl;}template<typename T,typename...A>void ashow(T t,A...a){cout<<t<<\" \";ashow(a...);}\n\ntypedef pair<int,int> P;\ntypedef pair<ll,ll> pll;\ntypedef vector<int> vi;\ntypedef vector<ll> vl;\ntypedef vector<vi> vvi;\ntypedef vector<vl> vvl;\ntypedef vector<P> vp;\ntypedef vector<double> vd;\ntypedef vector<string> vs;\n\nconst int MAX_N = 100005;\n\n// 多重辺は無いと仮定する\nclass BSTNode {\npublic:\n const int from, to;\n int sz;\n bool subtree_edge, subofftree_edge, exact_level;\n BSTNode *left, *right, *par;\n unordered_set<int> adjacent;\n BSTNode(const int _ver) noexcept :\n from(_ver), to(_ver), sz(0), subtree_edge(false), subofftree_edge(false),\n exact_level(false), left(nullptr), right(nullptr), par(nullptr){}\n BSTNode(const int _from, const int _to, const bool _flag) noexcept :\n from(_from), to(_to), sz(0), subtree_edge(false), subofftree_edge(false),\n exact_level((from < to) && _flag), left(nullptr), right(nullptr), par(nullptr){}\n inline bool IsRoot() const noexcept { return !par; }\n inline bool IsVertex() const noexcept { return (from == to); }\n inline void eval() noexcept {\n if(IsVertex()) sz = 1, subtree_edge = false, subofftree_edge = !adjacent.empty();\n else sz = 0, subtree_edge = exact_level, subofftree_edge = false;\n if(left){\n sz += left->sz, subtree_edge |= left->subtree_edge, subofftree_edge |= left->subofftree_edge;\n }\n if(right){\n sz += right->sz, subtree_edge |= right->subtree_edge, subofftree_edge |= right->subofftree_edge;\n }\n }\n inline void subtree_edge_eval(){\n subtree_edge = exact_level;\n if(left) subtree_edge |= left->subtree_edge;\n if(right) subtree_edge |= right->subtree_edge;\n }\n inline void subofftree_edge_eval(){\n subofftree_edge = !adjacent.empty();\n if(left) subofftree_edge |= left->subofftree_edge;\n if(right) subofftree_edge |= right->subofftree_edge;\n }\n inline bool subofftree_check(){\n return !adjacent.empty() ||\n (left ? left->subofftree_edge : false) || (right ? right->subofftree_edge : false);\n }\n inline bool offtree_check(){\n return adjacent.empty() ||\n (left ? left->subofftree_edge : false) || (right ? right->subofftree_edge : false);\n }\n void rotate(const bool right_) noexcept {\n BSTNode *p = par, *g = p->par;\n if(right_){\n if((p->left = right)) right->par = p;\n right = p, p->par = this;\n }else{\n if((p->right = left)) left->par = p;\n left = p, p->par = this;\n }\n p->eval(), eval();\n if(!(par = g)) return;\n if(g->left == p) g->left = this;\n if(g->right == p) g->right = this;\n g->eval();\n }\n};\n\nBSTNode *splay(BSTNode *u) noexcept {\n if(!u) return nullptr;\n while(!(u->IsRoot())){\n BSTNode *p = u->par, *gp = p->par;\n if(p->IsRoot()){ // zig\n u->rotate((u == p->left));\n }else{\n bool flag = (u == p->left);\n if((u == p->left) == (p == gp->left)){ // zig-zig\n p->rotate(flag), u->rotate(flag);\n }else{ // zig-zag\n u->rotate(flag), u->rotate(!flag);\n }\n }\n }\n return u;\n}\n\nBSTNode *join(BSTNode *root1, BSTNode *root2) noexcept {\n if(!root1 || !root2) return root1 ? root1 : root2;\n BSTNode *cur = nullptr, *nx = root1;\n do{ cur = nx, nx = cur->right; }while(nx);\n BSTNode *ver = splay(cur);\n ver->right = root2, ver->eval(), root2->par = ver;\n return ver;\n}\n\nclass EulerTourTree {\npublic:\n struct pair_hash {\n template <class T1, class T2>\n size_t operator() (const pair<T1, T2>& p) const {\n size_t lhs = hash<T1>()(p.first), rhs = hash<T2>()(p.second);\n return lhs^(rhs+0x9e3779b9+(lhs<<6)+(lhs>>2));\n }\n };\n BSTNode** vertex_set;\n unordered_map<pair<int, int>, pair<BSTNode*, BSTNode*>, pair_hash> edge_set;\nprivate:\n BSTNode *reroot(BSTNode *ver) noexcept {\n BSTNode *res = splay(ver)->left;\n if(!res) return ver;\n ver->left = nullptr, ver->eval();\n while(ver->right) ver = ver->right;\n splay(ver), ver->right = res, ver->eval(), res->par = ver;\n return ver;\n }\n void link(BSTNode *ver1, BSTNode *ver2, const bool flag) noexcept {\n BSTNode *e1 = new BSTNode(ver1->from, ver2->from, flag);\n BSTNode *e2 = new BSTNode(ver2->from, ver1->from, flag);\n edge_set[{ver1->from, ver2->from}] = {e1, e2};\n join(join(reroot(ver1), e1), join(reroot(ver2), e2));\n }\n void cut(BSTNode *edge1, BSTNode *edge2) noexcept {\n splay(edge1), splay(edge2);\n BSTNode *p = edge1->par;\n bool _right = (edge1 == edge2->right);\n if(p != edge2){\n _right = (p == edge2->right);\n p->par = nullptr, edge1->rotate((edge1 == p->left));\n }\n if(edge1->left) edge1->left->par = nullptr;\n if(edge1->right) edge1->right->par = nullptr;\n if(_right){\n if(edge2->left) edge2->left->par = nullptr;\n join(edge2->left, edge1->right);\n }else{\n if(edge2->right) edge2->right->par = nullptr;\n join(edge1->left, edge2->right);\n }\n delete edge1; delete edge2;\n }\n bool connected(BSTNode *ver1, BSTNode *ver2) noexcept {\n splay(ver1), splay(ver2);\n return ver1->par;\n }\n int component_size(BSTNode *ver) noexcept { return splay(ver)->sz; }\npublic:\n int V;\n // ~EulerTourTree(){\n // for(auto it : edge_set){\n // delete (it.second).first;\n // delete (it.second).second;\n // }\n // for(int i = 0; i < V; ++i) delete vertex_set[i];\n // delete[] vertex_set;\n // }\n EulerTourTree(const int node_size) noexcept {\n V = node_size, vertex_set = new BSTNode*[V];\n for(int i = 0; i < V; i++) vertex_set[i] = new BSTNode(i);\n }\n void reroot(const int node_id) noexcept { reroot(vertex_set[node_id]); }\n void link(int node1_id, int node2_id, bool flag=true) noexcept {\n if(node1_id > node2_id) swap(node1_id, node2_id);\n link(vertex_set[node1_id], vertex_set[node2_id], flag);\n }\n void cut(int node1_id, int node2_id){\n if(node1_id > node2_id) swap(node1_id, node2_id);\n auto it = edge_set.find({node1_id, node2_id});\n assert(it != edge_set.end());\n BSTNode *edge1 = (it->second).first, *edge2 = (it->second).second;\n edge_set.erase(it);\n cut(edge1, edge2);\n }\n bool connected(const int node1_id, const int node2_id) noexcept {\n if(node1_id == node2_id) return true;\n return connected(vertex_set[node1_id], vertex_set[node2_id]);\n }\n int component_size(int node_id) noexcept { return component_size(vertex_set[node_id]); }\n void check_dfs(const BSTNode* cur) const noexcept {\n if(cur->left) check_dfs(cur->left);\n cout << \"{\" << (cur->from) << \",\" << (cur->to) << \"} \";\n if(cur->right) check_dfs(cur->right);\n }\n};\n\n// 頂点ノードにそこから出る level i の off tree edge の集合(両方)を乗せる\n// 頂点ノードには map でアクセス可能\n// link のとき tree edge ならそのまま link\n// off tree edge なら level 0 の頂点ノードにそいつを追加\n// cut のとき level i の tree edge を level i+1 に押し上げるのだが\n// 各 layer の Euler Tree は level i 以上の edge からなるのでその辺が exact に level i の辺であるかをもつ必要がある.\n// 基本的に部分木の情報を書き換えるときは(splay してから変更する)根にしてから反映しないとマズい.\n// 保持している offtree edge って offtree edge の名の通り常にその両端点は同じ連結成分に属する.\n\nclass DynamicConnectivity {\nprivate:\n BSTNode *level_up_dfs(BSTNode *cur, const int layer) noexcept {\n if(cur->exact_level){\n splay(cur)->exact_level = false, cur->subtree_edge_eval();\n detect_layer[{cur->from, cur->to}]++, et[layer+1].link(cur->from, cur->to);\n return cur;\n }\n if(cur->left && cur->left->subtree_edge) return level_up_dfs(cur->left, layer);\n if(cur->right && cur->right->subtree_edge) return level_up_dfs(cur->right, layer);\n return nullptr;\n }\n // another は (u, v) を削除しているけどそのうち larger connected component に属する方\n BSTNode *search_edge_dfs\n (BSTNode *cur, const int layer, const int another, bool& flag, pair<int, int>& rep_edge) noexcept {\n // off_tree edge がある\n if(!cur->adjacent.empty()){\n bool state = et[layer+1].vertex_set[cur->from]->adjacent.empty();\n for(auto it = cur->adjacent.begin(); it != cur->adjacent.end();){\n pair<int, int> e = {min(cur->from, *it), max(cur->from, *it)};\n BSTNode *correspond = et[layer].vertex_set[*it];\n if(et[layer].connected(another, *it)){\n // another と今見ている先が connected つまり, restore する edge なら\n flag = true, rep_edge = e;\n cur->adjacent.erase(it), correspond->adjacent.erase(cur->from);\n // correspond の方で subofftree フラグが壊れるのを防ぐ.\n if(!correspond->subofftree_check()){\n splay(correspond)->subofftree_edge_eval();\n }\n break;\n }else{\n // 今見ている connected component をつなぐ edge なら suboff_tree edge の情報変える\n if(!et[layer+1].vertex_set[*it]->subofftree_check()){\n splay(et[layer+1].vertex_set[*it])->subofftree_edge = true;\n }\n et[layer+1].vertex_set[cur->from]->adjacent.insert(*it);\n et[layer+1].vertex_set[*it]->adjacent.insert(cur->from);\n detect_layer[e]++, it = cur->adjacent.erase(it);\n correspond->adjacent.erase(cur->from);\n if(!correspond->subofftree_check()){\n splay(correspond)->subofftree_edge_eval();\n }\n }\n }\n // 元々 ajacent が empty で off_tree edge が無かったのに今 cur->from から出る adjacent がある場合\n // でかつ left, right の off_tree edge が false なら subofftree_edge を true にする(これ何してんの?)\n // left もしくは right の off_tree edge が true なら根まで offtree_edge ありますよの伝播がちゃんと行っているので\n // ajacent list が新しく non empty になったけど別に更新する必要はないな\n if(state && !et[layer+1].vertex_set[cur->from]->offtree_check()){\n splay(et[layer+1].vertex_set[cur->from])->subofftree_edge = true;\n }\n splay(cur)->subofftree_edge_eval();\n return cur;\n }\n if(cur->left && cur->left->subofftree_edge){\n return search_edge_dfs(cur->left, layer, another, flag, rep_edge);\n }\n if(cur->right && cur->right->subofftree_edge){\n return search_edge_dfs(cur->right, layer, another, flag, rep_edge);\n }\n return nullptr;\n }\n bool replace(const int from, const int to, const int layer) noexcept {\n if(layer < 0) return true;\n int u, v;\n if(et[layer].component_size(from) <= et[layer].component_size(to)) u = from, v = to;\n else u = to, v = from;\n BSTNode *ver = splay(et[layer].vertex_set[u]);\n // smaller connected component に tree edge があるならそれの level を 1 個上げる\n while(ver->subtree_edge) ver = level_up_dfs(ver, layer);\n pair<int, int> rep_edge = {-1, -1};\n bool flag = false;\n // smaller connected component に offtree edge があり,\n // その辺が連結成分を再びつなげるようなものでないなら 1 増やす\n // flag 見つけたらすぐ終了するという実装にしてるけどそれでもならしが回るので問題ない\n // 1 回探索する度に restore edge が見つかって探索が終了する or 何かの辺の level が 1 上がる が言えるので\n while(ver->subofftree_edge){\n ver = search_edge_dfs(ver, layer, v, flag, rep_edge);\n if(flag) break;\n }\n if(flag){\n // restore edge を見つけたなら\n et[layer].link(rep_edge.first, rep_edge.second);\n for(int i = 0; i < layer; i++) et[i].link(rep_edge.first, rep_edge.second, false);\n return false;\n }else return replace(from, to, layer-1);\n }\npublic:\n const int V;\n int depth;\n vector<EulerTourTree> et;\n unordered_map<pair<int, int>, int, EulerTourTree::pair_hash> detect_layer;\n DynamicConnectivity(const int node_size) noexcept : V(node_size), depth(1){\n et.emplace_back(V);\n }\n // ~DynamicConnectivity(){\n // delete[] et;\n // }\n bool link(int node1_id, int node2_id) noexcept {\n if(node1_id > node2_id) swap(node1_id, node2_id);\n detect_layer[{node1_id, node2_id}] = 0;\n if(et[0].connected(node1_id, node2_id)){\n BSTNode *ver1 = et[0].vertex_set[node1_id], *ver2 = et[0].vertex_set[node2_id];\n splay(ver1)->subofftree_edge = true, ver1->adjacent.insert(node2_id);\n splay(ver2)->subofftree_edge = true, ver2->adjacent.insert(node1_id);\n return false;\n }else{\n et[0].link(node1_id, node2_id);\n return true;\n }\n }\n bool cut(int node1_id, int node2_id) noexcept {\n if(node1_id > node2_id) swap(node1_id, node2_id);\n auto it = detect_layer.find({node1_id, node2_id});\n assert(it != detect_layer.end());\n int layer = it->second;\n detect_layer.erase(it);\n auto& st = et[layer].vertex_set[node1_id]->adjacent;\n if(st.find(node2_id) == st.end()){\n // tree edge の場合\n for(int i = 0; i <= layer; i++) et[i].cut(node1_id, node2_id);\n if(layer + 1 == depth) ++depth, et.emplace_back(V);\n return replace(node1_id, node2_id, layer);\n }else{\n // off_tree edge の場合\n et[layer].vertex_set[node1_id]->adjacent.erase(node2_id);\n // 自分より下に node1_id より下に off tree_edge があるか\n // (もしなかったらこれより上のノードについての off tree edge があるかどうかの情報を書き換える必要がある)\n if(!et[layer].vertex_set[node1_id]->subofftree_check()){\n splay(et[layer].vertex_set[node1_id])->subofftree_edge_eval();\n }\n et[layer].vertex_set[node2_id]->adjacent.erase(node1_id);\n if(!et[layer].vertex_set[node2_id]->subofftree_check()){\n splay(et[layer].vertex_set[node2_id])->subofftree_edge_eval();\n }\n return false;\n }\n }\n bool connected(const int node1_id, const int node2_id) noexcept {\n return et[0].connected(node1_id, node2_id);\n }\n};\n\n#define getchar getchar_unlocked\n#define putchar putchar_unlocked\n\ninline int in() {\n int n = 0; short c;\n while ((c = getchar()) >= '0') n = n * 10 + c - '0';\n return n;\n}\n\ninline void out(int n) {\n short res[10], i = 0;\n do { res[i++] = n % 10, n /= 10; } while (n);\n while (i) putchar(res[--i] + '0');\n putchar('\\n');\n}\n\nint main()\n{\n int n = in(), m = in();\n DynamicConnectivity dc(n);\n rep(i,m){\n int a,b,c;\n a = in(), b = in(), c = in();\n if(a == 1){\n dc.link(b, c);\n }else if(a == 2){\n dc.cut(b, c);\n }else{\n if(dc.connected(b,c)){\n cout << \"YES\\n\";\n }else{\n cout << \"NO\\n\";\n }\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 230, "memory_kb": 76332, "score_of_the_acc": -1.3622, "final_rank": 13 }, { "submission_id": "aoj_2235_10036298", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n/**\n * @brief template\n */\n\nstruct UnionFindUndo {\n vector< int > data;\n stack< pair< int, int > > history;\n\n UnionFindUndo(int sz) {\n data.assign(sz, -1);\n }\n\n bool unite(int x, int y) {\n x = find(x), y = find(y);\n history.emplace(x, data[x]);\n history.emplace(y, data[y]);\n if(x == y) return (false);\n if(data[x] > data[y]) swap(x, y);\n data[x] += data[y];\n data[y] = x;\n return (true);\n }\n\n int find(int k) {\n if(data[k] < 0) return (k);\n return (find(data[k]));\n }\n\n int size(int k) {\n return (-data[find(k)]);\n }\n\n bool same(int x, int y) {return find(x) == find(y);}\n\n void undo() {\n data[history.top().first] = history.top().second;\n history.pop();\n data[history.top().first] = history.top().second;\n history.pop();\n }\n\n void snapshot() {\n while(history.size()) history.pop();\n }\n\n void rollback() {\n while(history.size()) undo();\n }\n};\n\nstruct OfflineDynamicConnectivity {\n using edge = pair< int, int >;\n\n UnionFindUndo uf;\n int V, Q, segsz;\n vector< vector< edge > > seg;\n int comp;\n\n vector< pair< pair< int, int >, edge > > pend;\n map< edge, int > cnt, appear;\n\n OfflineDynamicConnectivity(int V, int Q) : uf(V), V(V), Q(Q), comp(V) {\n segsz = 1;\n while(segsz < Q) segsz <<= 1;\n seg.resize(2 * segsz - 1);\n }\n\n void insert(int idx, int s, int t) {\n auto e = minmax(s, t);\n if(cnt[e]++ == 0) appear[e] = idx;\n }\n\n void erase(int idx, int s, int t) {\n auto e = minmax(s, t);\n if(--cnt[e] == 0) pend.emplace_back(make_pair(appear[e], idx), e);\n }\n\n void add(int a, int b, const edge &e, int k, int l, int r) {\n if(r <= a || b <= l) return;\n if(a <= l && r <= b) {\n seg[k].emplace_back(e);\n return;\n }\n add(a, b, e, 2 * k + 1, l, (l + r) >> 1);\n add(a, b, e, 2 * k + 2, (l + r) >> 1, r);\n }\n\n void add(int a, int b, const edge &e) {\n add(a, b, e, 0, 0, segsz);\n }\n\n void build() {\n for(auto &p : cnt) {\n if(p.second > 0) pend.emplace_back(make_pair(appear[p.first], Q), p.first);\n }\n for(auto &s : pend) {\n add(s.first.first, s.first.second, s.second);\n }\n }\n\n void run(const function< void(int) > &f, int k = 0) {\n int add = 0;\n for(auto &e : seg[k]) {\n add += uf.unite(e.first, e.second);\n }\n comp -= add;\n if(k < segsz - 1) {\n run(f, 2 * k + 1);\n run(f, 2 * k + 2);\n } else if(k - (segsz - 1) < Q) {\n int query_index = k - (segsz - 1);\n f(query_index);\n }\n for(auto &e : seg[k]) {\n uf.undo();\n }\n comp += add;\n }\n};\n\nint main() {\n cin.tie(0);\n ios_base::sync_with_stdio(false);\n int N, Q;\n cin >> N >> Q;\n vector<int> T(Q), U(Q), V(Q);\n rep(i,0,Q) cin >> T[i] >> U[i] >> V[i];\n OfflineDynamicConnectivity UF(N,Q);\n rep(i,0,Q) {\n if (T[i] == 1) UF.insert(i,U[i],V[i]);\n if (T[i] == 2) UF.erase(i,U[i],V[i]);\n }\n UF.build();\n UF.run([&](int k){\n if (T[k] == 3) cout << (UF.uf.same(U[k],V[k]) ? \"YES\" : \"NO\") << '\\n';\n });\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 15620, "score_of_the_acc": -0.1539, "final_rank": 5 }, { "submission_id": "aoj_2235_10036237", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n/**\n * @brief template\n */\n\nstruct RollbackUnionFind{\n vector<int> par;\n stack<pair<int,int>> history;\n int snap;\n RollbackUnionFind(){}\n RollbackUnionFind(int _n):par(_n,-1),snap(0){}\n int root(int x){return par[x]<0?x:par[x]=root(par[x]);}\n bool same(int x,int y){return root(x)==root(y);}\n int size(int x){return -par[root(x)];}\n bool unite(int x,int y){\n x=root(x),y=root(y);\n history.push({x,par[x]});\n history.push({y,par[y]});\n if(x==y)return false;\n if(size(x)>size(y))swap(x,y);\n par[y]+=par[x]; par[x]=y;\n return true;\n }\n void undo(){\n par[history.top().first]=history.top().second;\n history.pop();\n par[history.top().first]=history.top().second;\n history.pop();\n }\n void snapshot(){snap=int(history.size()>>1);}\n int get(){return int(history.size()>>1);}\n void rollback(int state=-1){\n if(state==-1)state=snap;\n state<<=1;\n while(state<history.size())undo();\n }\n};\n\n/**\n * @brief Rollback Union Find\n */\n\nstruct OfflineDynamicConnectivity {\n using edge = pair<int,int>;\n\n RollbackUnionFind UF;\n int V, Q, segsz;\n vector<vector<edge>> seg;\n int comp;\n\n vector<pair<pair<int,int>,edge>> pend;\n map<edge,int> cnt, appear;\n\n OfflineDynamicConnectivity(int V, int Q) : UF(V), V(V), Q(Q), comp(V) {\n segsz = 1;\n while(segsz < Q) segsz <<= 1;\n seg.resize(2 * segsz - 1);\n }\n\n void insert(int idx, int s, int t) {\n auto e = minmax(s,t);\n if (cnt[e]++ == 0) appear[e] = idx;\n }\n\n void erase(int idx, int s, int t) {\n auto e = minmax(s,t);\n if (--cnt[e] == 0) pend.push_back({{appear[e], idx}, e});\n }\n\n void add(int a, int b, const edge& e, int k, int l, int r) {\n if (r <= a || b <= l) return;\n if (a <= l && r <= b) {\n seg[k].emplace_back(e);\n return;\n }\n add(a,b,e,2*k+1,l,(l+r)>>1);\n add(a,b,e,2*k+2,(l+r)>>1,r);\n }\n\n void add(int a, int b, const edge& e) {\n add(a,b,e,0,0,segsz);\n }\n\n void build() {\n for (auto& p : cnt) {\n if (p.second > 0) pend.push_back({{appear[p.first], Q}, p.first});\n }\n for (auto& s : pend) {\n add(s.first.first, s.first.second, s.second);\n }\n }\n\n void run(const function<void(int)>& f, int k = 0) {\n int add = 0;\n for (auto& e : seg[k]) {\n add += UF.unite(e.first, e.second);\n }\n comp -= add;\n if (k < segsz - 1) {\n run(f, 2*k+1);\n run(f, 2*k+2);\n }\n else if (k-(segsz-1) < Q) {\n int query_index = k - (segsz - 1);\n f(query_index);\n }\n for (auto& e : seg[k]) {\n UF.undo();\n }\n comp += add;\n }\n};\n\nint main() {\n cin.tie(0);\n ios_base::sync_with_stdio(false);\n int N, Q;\n cin >> N >> Q;\n vector<int> T(Q), U(Q), V(Q);\n rep(i,0,Q) cin >> T[i] >> U[i] >> V[i];\n OfflineDynamicConnectivity UF(N,Q);\n rep(i,0,Q) {\n if (T[i] == 1) UF.insert(i,U[i],V[i]);\n if (T[i] == 2) UF.erase(i,U[i],V[i]);\n }\n UF.build();\n UF.run([&](int k){\n if (T[k] == 3) cout << (UF.UF.same(U[k],V[k]) ? \"YES\" : \"NO\") << '\\n';\n });\n}", "accuracy": 0.13157894736842105, "time_ms": 20, "memory_kb": 9568, "score_of_the_acc": -0.0645, "final_rank": 20 }, { "submission_id": "aoj_2235_10036088", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n/**\n * @brief template\n */\n\nstruct UnionFind{\n vector<int> par; int n;\n UnionFind(){}\n UnionFind(int _n):par(_n,-1),n(_n){}\n int root(int x){return par[x]<0?x:par[x]=root(par[x]);}\n bool same(int x,int y){return root(x)==root(y);}\n int size(int x){return -par[root(x)];}\n bool unite(int x,int y){\n x=root(x),y=root(y); if(x==y)return false;\n if(size(x)>size(y))swap(x,y);\n par[y]+=par[x]; par[x]=y; n--; return true;\n }\n};\n\n/**\n * @brief Union Find\n */\n\nint main() {\n cin.tie(0);\n ios_base::sync_with_stdio(false);\n int N, Q, M;\n cin >> N >> Q;\n int B = 200;\n vector<int> T(Q), U(Q), V(Q), ID(Q, -1);\n vector<int> IDtoU, IDtoV;\n rep(i,0,Q) cin >> T[i] >> U[i] >> V[i];\n {\n map<pair<int,int>,int> mp;\n int cur = 0;\n rep(i,0,Q) {\n if (T[i] == 3) continue;\n if (mp.count({U[i],V[i]})) ID[i] = mp[{U[i],V[i]}];\n else {\n IDtoU.push_back(U[i]);\n IDtoV.push_back(V[i]);\n ID[i] = cur;\n mp[{U[i],V[i]}] = cur;\n cur++;\n }\n }\n M = cur;\n }\n rep(i,0,ceil(Q,B)) {\n vector<bool> edges(M,false);\n rep(j,0,i*B) {\n if (T[j] == 1) edges[ID[j]] = true;\n if (T[j] == 2) edges[ID[j]] = false;\n }\n vector<bool> willuse(M,false);\n rep(j,i*B,min(Q,(i+1)*B)) {\n if (T[j] == 3) continue;\n assert(ID[j] >= 0);\n willuse[ID[j]] = true;\n }\n vector<int> willuses;\n rep(j,0,M) if (willuse[j]) willuses.push_back(j);\n UnionFind UF1(N);\n rep(j,0,M) {\n if (edges[j] && !willuse[j]) UF1.unite(IDtoU[j], IDtoV[j]);\n }\n vector<bool> isuse(N,false);\n rep(j,i*B,min(Q,(i+1)*B)) {\n isuse[UF1.root(U[j])] = true;\n isuse[UF1.root(V[j])] = true;\n }\n vector<int> VID(N,-1);\n int NewM;\n {\n int cur = 0;\n rep(j,0,N) {\n if (isuse[j]) {\n VID[j] = cur;\n cur++;\n }\n }\n NewM = cur;\n }\n rep(j,i*B,min(Q,(i+1)*B)) {\n if (T[j] == 1) {\n edges[ID[j]] = true;\n }\n if (T[j] == 2) {\n edges[ID[j]] = false;\n }\n if (T[j] == 3) {\n UnionFind UF2(NewM);\n for (int k : willuses) {\n if (edges[k]) {\n int u = IDtoU[k], v = IDtoV[k];\n u = UF1.root(u), v = UF1.root(v);\n u = VID[u], v = VID[v];\n UF2.unite(u,v);\n }\n }\n {\n int u = U[j], v = V[j];\n u = UF1.root(u), v = UF1.root(v);\n u = VID[u], v = VID[v];\n cout << (UF2.same(u,v) ? \"YES\" : \"NO\") << \"\\n\";\n }\n }\n }\n }\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 6104, "score_of_the_acc": -0.1562, "final_rank": 6 }, { "submission_id": "aoj_2235_10005104", "code_snippet": "#line 1 \"/home/dispersion/competitive/icpc/ICPC-library/ICPC2024/merge_src_docs/template/24_template.hpp\"\n#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\n#define rep(i, n) for(ll i = 0; i < n; i++)\n#define rep2(i, l, r) for(ll i = l; i < r; i++)\n\nusing vi = vector<int>;\nusing vvi = vector<vi>;\nusing vll = vector<ll>;\n\n#define all(A) A.begin(), A.end()\n#define elif else if\nusing pii = pair<ll, ll>;\n\nbool chmin(auto &a, auto b) { return a > b ? a = b, 1 : 0; }\nbool chmax(auto &a, auto b) { return a < b ? a = b, 1 : 0; }\n\nstruct IOSetup {\n IOSetup() {\n cin.tie(0);\n ios::sync_with_stdio(0);\n }\n} iosetup;\n\ntemplate<class T>\nvoid print(vector<T> a) {\n for(auto x : a) cout << x << ' ';\n cout << endl;\n}\n\nvoid print(auto x) { cout << x << endl; }\n\ntemplate<class Head, class... Tail>\nvoid print(Head &&head, Tail &&...tail) {\n cout << head << ' ';\n print(forward<Tail>(tail)...);\n}\n#line 1 \"/home/dispersion/competitive/icpc/ICPC-library/ICPC2024/merge_src_docs/graph/OffDyCon.hpp\"\n/*\nOfflineDynamicConnectivity(int N, int Q) : 頂点数 N のグラフ、Q 個のクエリで初期化\ninsert(int idx, int s, int t) : 時刻 idx に辺 s-t を追加する\nerase(int idx, int s, int t) : 時刻 idx に辺 s-t を削除する\nbuild() : 追加・削除クエリを与えたのちに呼び出す\nrun(function<void(int)> f) : \n build() 後に呼び出すことで、各時刻 t = [0, Q) について f(t) が呼び出される\n O(Q log(Q) log(N)) time\n*/\n\n#line 1 \"/home/dispersion/competitive/icpc/ICPC-library/ICPC2024/merge_src_docs/data_structure/RollbackUnionFind.hpp\"\nstruct RollbackUnionFind {\n vector<int> data;\n stack<pair<int, int>> history;\n\n RollbackUnionFind(int sz) { data.assign(sz, -1); }\n\n bool unite(int x, int y) {\n x = find(x), y = find(y);\n history.emplace(x, data[x]);\n history.emplace(y, data[y]);\n if(x == y) return (false);\n if(data[x] > data[y]) swap(x, y);\n data[x] += data[y];\n data[y] = x;\n return (true);\n }\n\n int find(int k) {\n if(data[k] < 0) return (k);\n return (find(data[k]));\n }\n\n bool same(int x, int y) { return find(x) == find(y); }\n\n int size(int k) { return (-data[find(k)]); }\n\n void undo() {\n data[history.top().first] = history.top().second;\n history.pop();\n data[history.top().first] = history.top().second;\n history.pop();\n }\n\n void snapshot() {\n while(history.size()) history.pop();\n }\n\n void rollback() {\n while(history.size()) undo();\n }\n};\n#line 12 \"/home/dispersion/competitive/icpc/ICPC-library/ICPC2024/merge_src_docs/graph/OffDyCon.hpp\"\nstruct OffDyCon {\n using edge = pair<int, int>;\n\n RollbackUnionFind uf;\n int V, Q, segsz;\n vector<vector<edge>> seg;\n int comp;\n\n vector<pair<pair<int, int>, edge>> pend;\n map<edge, int> cnt, appear;\n\n OffDyCon(int V, int Q) : uf(V), V(V), Q(Q), comp(V) {\n segsz = 1;\n while(segsz < Q) segsz <<= 1;\n seg.resize(2 * segsz - 1);\n }\n\n void insert(int idx, int s, int t) {\n auto e = minmax(s, t);\n if(cnt[e]++ == 0) appear[e] = idx;\n }\n\n void erase(int idx, int s, int t) {\n auto e = minmax(s, t);\n if(--cnt[e] == 0) pend.emplace_back(make_pair(appear[e], idx), e);\n }\n\n void add(int a, int b, const edge &e, int k, int l, int r) {\n if(r <= a || b <= l) return;\n if(a <= l && r <= b) {\n seg[k].emplace_back(e);\n return;\n }\n add(a, b, e, 2 * k + 1, l, (l + r) >> 1);\n add(a, b, e, 2 * k + 2, (l + r) >> 1, r);\n }\n\n void add(int a, int b, const edge &e) { add(a, b, e, 0, 0, segsz); }\n\n void build() {\n for(auto &p : cnt) {\n if(p.second > 0)\n pend.emplace_back(make_pair(appear[p.first], Q), p.first);\n }\n for(auto &s : pend) { add(s.first.first, s.first.second, s.second); }\n }\n\n void run(const function<void(int)> &f, int k = 0) {\n int add = 0;\n for(auto &e : seg[k]) { add += uf.unite(e.first, e.second); }\n comp -= add;\n if(k < segsz - 1) {\n run(f, 2 * k + 1);\n run(f, 2 * k + 2);\n }\n else if(k - (segsz - 1) < Q) {\n int query_index = k - (segsz - 1);\n f(query_index);\n }\n for(auto &e : seg[k]) { uf.undo(); }\n comp += add;\n }\n};\n#line 3 \"OffDyCon.cpp\"\n\n// https://onlinejudge.u-aizu.ac.jp/services/ice/?problemId=2235\nvoid solve1() {\n ll N, Q; cin >> N >> Q;\n \n OffDyCon dycon(N, Q);\n vector<array<ll, 2>> query(Q);\n vector<ll> is3(Q, 0);\n \n rep(q, Q) {\n ll t, u, v; cin >> t >> u >> v;\n query[q] = {u, v};\n \n if(t == 1) dycon.insert(q, u, v);\n else if(t == 2) dycon.erase(q, u, v);\n else if(t == 3) is3[q] = 1;\n }\n dycon.build();\n \n vector<ll> res(Q, 0);\n auto func = [&](ll t) -> void {\n auto [u, v] = query[t];\n res[t] = dycon.uf.same(u, v);\n return;\n };\n dycon.run(func);\n \n rep(i, Q) {\n if(is3[i]) cout << (res[i] ? \"YES\" : \"NO\") << '\\n';\n }\n \n return;\n}\n\nint main() {\n solve1();\n \n return 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 16684, "score_of_the_acc": -0.1641, "final_rank": 7 }, { "submission_id": "aoj_2235_9711558", "code_snippet": "#pragma region Macros\n#include <bits/stdc++.h>\n\nusing namespace std;\nusing lint = long long;\nusing ull = unsigned long long;\nusing ld = long double;\nusing int128 = __int128_t;\n#define all(x) (x).begin(), (x).end()\n#define EPS 1e-8\n#define uniqv(v) v.erase(unique(all(v)), v.end())\n#define OVERLOAD_REP(_1, _2, _3, name, ...) name\n#define REP1(i, n) for (auto i = std::decay_t<decltype(n)>{}; (i) != (n); ++(i))\n#define REP2(i, l, r) for (auto i = (l); (i) != (r); ++(i))\n#define rep(...) OVERLOAD_REP(__VA_ARGS__, REP2, REP1)(__VA_ARGS__)\n#define log(x) cout << x << endl\n#define logfixed(x) cout << fixed << setprecision(10) << x << endl;\n#define logy(bool) \\\n if (bool) { \\\n cout << \"Yes\" << endl; \\\n } else { \\\n cout << \"No\" << endl; \\\n }\n\nostream &operator<<(ostream &dest, __int128_t value) {\n ostream::sentry s(dest);\n if (s) {\n __uint128_t tmp = value < 0 ? -value : value;\n char buffer[128];\n char *d = end(buffer);\n do {\n --d;\n *d = \"0123456789\"[tmp % 10];\n tmp /= 10;\n } while (tmp != 0);\n if (value < 0) {\n --d;\n *d = '-';\n }\n int len = end(buffer) - d;\n if (dest.rdbuf()->sputn(d, len) != len) {\n dest.setstate(ios_base::badbit);\n }\n }\n return dest;\n}\n\ntemplate <typename T>\nostream &operator<<(ostream &os, const vector<T> &v) {\n for (int i = 0; i < (int)v.size(); i++) {\n os << v[i] << (i + 1 != (int)v.size() ? \" \" : \"\");\n }\n return os;\n}\n\ntemplate <typename T>\nostream &operator<<(ostream &os, const set<T> &set_var) {\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end()) os << \" \";\n itr--;\n }\n return os;\n}\n\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << itr->first << \" -> \" << itr->second << \"\\n\";\n }\n return os;\n}\n\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const pair<T, U> &pair_var) {\n os << \"(\" << pair_var.first << \", \" << pair_var.second << \")\";\n return os;\n}\n\nvoid out() { cout << '\\n'; }\ntemplate <class T, class... Ts>\nvoid out(const T &a, const Ts &...b) {\n cout << a;\n (cout << ... << (cout << ' ', b));\n cout << '\\n';\n}\n\ntemplate <typename T>\nistream &operator>>(istream &is, vector<T> &v) {\n for (T &in : v) is >> in;\n return is;\n}\n\ninline void in(void) { return; }\ntemplate <typename First, typename... Rest>\nvoid in(First &first, Rest &...rest) {\n cin >> first;\n in(rest...);\n return;\n}\n\ntemplate <typename T>\nbool chmax(T &a, const T &b) {\n if (a < b) {\n a = b;\n return true;\n }\n return false;\n}\ntemplate <typename T>\nbool chmin(T &a, const T &b) {\n if (a > b) {\n a = b;\n return true;\n }\n return false;\n}\n\nvector<lint> dx8 = {1, 1, 0, -1, -1, -1, 0, 1};\nvector<lint> dy8 = {0, 1, 1, 1, 0, -1, -1, -1};\nvector<lint> dx4 = {1, 0, -1, 0};\nvector<lint> dy4 = {0, 1, 0, -1};\n\n#pragma endregion\n\ntemplate <class S, auto op, auto e>\nclass UndoableUnionFind {\n private:\n struct HistoryData {\n int u, datu;\n S accu;\n int v, datv;\n S accv;\n int comp_cnt;\n };\n\n vector<int> data;\n vector<S> acc;\n stack<HistoryData> history;\n int cnt;\n int snap;\n\n public:\n UndoableUnionFind() {}\n UndoableUnionFind(int sz) {\n data.assign(sz, -1);\n cnt = sz;\n acc.resize(sz, e());\n }\n\n bool unite(int u, int v) {\n u = find(u), v = find(v);\n history.emplace(u, data[u], acc[u], v, data[v], acc[v], cnt);\n if (u == v) return false;\n if (data[u] > data[v]) swap(u, v);\n data[u] += data[v];\n data[v] = u;\n acc[u] = op(acc[u], acc[v]);\n cnt--;\n return true;\n }\n\n int find(int k) {\n while (data[k] >= 0) {\n k = data[k];\n }\n return k;\n }\n\n void update(int a, S x) {\n a = find(a);\n history.push({a, data[a], acc[a], -1, -1, e(), -1});\n acc[a] = op(acc[a], x);\n }\n\n lint prod_components(int a) {\n return acc[find(a)];\n }\n\n bool same(int x, int y) { return find(x) == find(y); }\n\n int size(int k) { return (-data[find(k)]); }\n\n void undo() {\n HistoryData h = history.top();\n history.pop();\n data[h.u] = h.datu;\n acc[h.u] = h.accu;\n if (h.v != -1) {\n data[h.v] = h.datv;\n acc[h.v] = h.accv;\n cnt = h.comp_cnt;\n }\n }\n\n int components() {\n return cnt;\n }\n};\n\ntemplate <class S, auto op, auto e>\nstruct OfflineDynamicConnectivity {\n private:\n struct Edge {\n int from, to;\n };\n\n struct Query {\n int com;\n int u, v;\n int start;\n int finish;\n lint val;\n };\n vector<Query> q;\n vector<S> res;\n int outq = 0;\n int n;\n vector<unordered_map<int, int>> ed;\n vector<vector<Edge>> node;\n vector<vector<pair<int, S>>> updates;\n vector<int> val_idx;\n UndoableUnionFind<S, op, e> d;\n int vertex_siz;\n int qtime;\n int Q_INF = 1e8;\n int idx = 0;\n\n vector<vector<Query>> outputQuery;\n\n void add(int a, int b, Edge &ed, int k = 0, int l = 0, int r = -1) {\n if (r < 0) r = n;\n if (r <= a || b <= l) return;\n if (a <= l && r <= b) {\n node[k].emplace_back(ed);\n return;\n }\n add(a, b, ed, 2 * k + 1, l, (l + r) / 2);\n add(a, b, ed, 2 * k + 2, (l + r) / 2, r);\n }\n\n void add_update(int a, int b, pair<int, S> x, int k = 0, int l = 0, int r = -1) {\n if (r < 0) r = n;\n if (r <= a || b <= l) return;\n if (a <= l && r <= b) {\n updates[k].emplace_back(x);\n return;\n }\n add_update(a, b, x, 2 * k + 1, l, (l + r) / 2);\n add_update(a, b, x, 2 * k + 2, (l + r) / 2, r);\n }\n\n void execute(int k = 0) {\n if (outq == 0) return;\n for (auto &ed : node[k]) {\n d.unite(ed.from, ed.to);\n }\n\n for (auto &p : updates[k]) {\n d.update(p.first, p.second);\n }\n\n if (k < n - 1) {\n execute(2 * k + 1);\n execute(2 * k + 2);\n } else if (k - (n - 1) < qtime) {\n int qidx = k - (n - 1);\n for (auto cur : outputQuery[qidx]) {\n int com = cur.com;\n int u = cur.u;\n int v = cur.v;\n if (com == 2) {\n res.emplace_back(d.same(u, v));\n } else if (com == 3) {\n res.emplace_back(d.components());\n } else if (com == 4) {\n res.emplace_back(d.size(u));\n } else if (com == 6) {\n res.emplace_back(d.prod_components(u));\n }\n }\n }\n for (int i = 0; i < int(node[k].size() + updates[k].size()); i++) {\n d.undo();\n }\n }\n\n public:\n OfflineDynamicConnectivity(int siz) {\n d = UndoableUnionFind<S, op, e>(siz);\n vertex_siz = siz;\n ed.resize(siz);\n val_idx.resize(siz, -1);\n }\n\n void link(int u, int v) {\n if (u > v) swap(u, v);\n if (ed[u].find(v) != ed[u].end()) return;\n qtime++;\n q.push_back({0, u, v, qtime, Q_INF, 0});\n ed[u][v] = idx;\n idx++;\n }\n\n void cut(int u, int v) {\n if (u > v) swap(u, v);\n qtime++;\n q.push_back({1, u, v, qtime, -1, 0});\n int pos = ed[u][v];\n q[pos].finish = qtime;\n ed[u].erase(v);\n idx++;\n }\n\n void update(int u, lint x) {\n qtime++;\n q.push_back({5, u, -1, qtime, Q_INF, x});\n idx++;\n }\n\n void is_connected(int u, int v) {\n if (u > v) swap(u, v);\n q.push_back({2, u, v, qtime, -1, 0});\n idx++;\n outq++;\n }\n\n void components() {\n q.push_back({3, -1, -1, qtime, -1, 0});\n idx++;\n outq++;\n }\n\n void size(int u) {\n q.push_back({4, u, -1, qtime, -1, 0});\n idx++;\n outq++;\n }\n\n void prod(int u) {\n q.push_back({6, u, -1, qtime, -1, 0});\n idx++;\n outq++;\n }\n\n vector<S> build() {\n qtime++;\n\n int sz = qtime;\n n = 1;\n while (n < sz) n *= 2;\n node.resize(2 * n - 1);\n updates.resize(2 * n - 1);\n outputQuery.resize(qtime);\n for (int i = 0; i < q.size(); i++) {\n if (q[i].com == 0) {\n Edge ed = {q[i].u, q[i].v};\n add(q[i].start, min(q[i].finish, qtime), ed);\n } else if (q[i].com == 5) {\n add_update(q[i].start, q[i].finish, {q[i].u, q[i].val});\n } else if (q[i].com != 1) {\n outputQuery[q[i].start].emplace_back(q[i]);\n }\n }\n execute();\n return res;\n }\n};\n\nusing S = lint;\n\nS op(S a, S b) {\n return a + b;\n}\n\nS e() {\n return 0;\n}\n\nint main() {\n int n, q;\n in(n, q);\n OfflineDynamicConnectivity<S, op, e> g(n);\n\n\n rep(i, q) {\n int com, u, v;\n in(com,u,v);\n if(com==1){\n g.link(u,v);\n }else if(com==2){\n g.cut(u,v);\n }else if(com==3){\n g.is_connected(u,v);\n }\n }\n vector<S> res = g.build();\n for (S x : res) {\n out(x?\"YES\":\"NO\");\n }\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 20440, "score_of_the_acc": -0.169, "final_rank": 8 }, { "submission_id": "aoj_2235_9696867", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nstruct UndoableUnionFind {\n private:\n int n;\n vector<int> par;\n stack<pair<int, int>> history;\n \n public:\n UndoableUnionFind() {}\n UndoableUnionFind(int n_) : n(n_), par(n, -1) {}\n \n int merge(int a, int b) {\n assert(0 <= a && a < n);\n assert(0 <= b && b < n);\n int x = leader(a), y = leader(b);\n history.emplace(x, par[x]);\n history.emplace(y, par[y]);\n if (x == y) {\n return x;\n }\n if (-par[x] < -par[y]) {\n swap(x, y);\n }\n par[x] += par[y];\n par[y] = x;\n return x;\n }\n \n bool same(int a, int b) {\n assert(0 <= a && a < n);\n assert(0 <= b && b < n);\n return leader(a) == leader(b);\n }\n \n int leader(int a) {\n assert(0 <= a && a < n);\n if (par[a] < 0) {\n return a;\n }\n return leader(par[a]);\n }\n \n int size(int a) {\n assert(0 <= a && a < n);\n return -par[leader(a)];\n }\n \n void undo() {\n if (history.empty()) {\n return;\n }\n par[history.top().first] = history.top().second;\n history.pop();\n par[history.top().first] = history.top().second;\n history.pop();\n }\n \n vector<vector<int>> groups() {\n vector<vector<int>> member(n);\n for (int i = 0; i < n; i++) {\n member[leader(i)].emplace_back(i);\n }\n vector<vector<int>> res;\n for (int i = 0; i < n; i++) {\n if ((int)member[i].size() > 0) {\n res.emplace_back(member[i]);\n }\n }\n return res;\n }\n};\n\nstruct OfflineDynamicConnectivity {\n using edge = pair<int, int>;\n int n, q, sz;\n vector<vector<edge>> seg;\n UndoableUnionFind uf;\n map<edge, int> cnt, appear;\n vector<pair<edge, pair<int, int>>> times;\n \n OfflineDynamicConnectivity() {}\n OfflineDynamicConnectivity(int n_, int q_) : n(n_), q(q_), sz(1), uf(n) {\n while (sz < q) {\n sz *= 2;\n }\n seg.resize(sz * 2);\n }\n \n void insert(int t, int u, int v) {\n assert(1 <= t);\n assert(0 <= u && u < n);\n assert(0 <= v && v < n);\n if (u > v) {\n swap(u, v);\n }\n edge e = {u, v};\n if (cnt[e] == 0) {\n cnt[e]++;\n appear[e] = t;\n }\n }\n \n void erase(int t, int u, int v) {\n assert(1 <= t);\n assert(0 <= u && u < n);\n assert(0 <= v && v < n);\n if (u > v) {\n swap(u, v);\n }\n edge e = {u, v};\n if (cnt[e] == 1) {\n cnt[e]--;\n pair<int, int> lr = {appear[e], t};\n times.emplace_back(e, lr);\n }\n }\n \n void add(edge e, int l, int r) {\n l += sz;\n r += sz;\n while (l < r) {\n if (l & 1) {\n seg[l].emplace_back(e);\n l++;\n }\n if (r & 1) {\n r--;\n seg[r].emplace_back(e);\n }\n l >>= 1;\n r >>= 1;\n }\n }\n \n void build() {\n for (auto [e, x] : cnt) {\n if (x > 0) {\n pair<int, int> lr = {appear[e], q};\n times.emplace_back(e, lr);\n }\n }\n for (auto [e, lr] : times) {\n auto [l, r] = lr;\n add(e, l, r);\n }\n }\n \n void run(function<void(int)> f, int i = 1) {\n for (auto e : seg[i]) {\n auto [u, v] = e;\n uf.merge(u, v);\n }\n if (0 <= i - sz) {\n if (i - sz < q) {\n f(i - sz);\n }\n } else {\n run(f, i * 2);\n run(f, i * 2 + 1);\n }\n for (int j = 0; j < (int)seg[i].size(); j++) {\n uf.undo();\n }\n }\n};\n\nint main() {\n int n, k;\n cin >> n >> k;\n OfflineDynamicConnectivity dc(n, k + 1);\n vector<int> t(k + 1), u(k + 1), v(k + 1);\n for (int i = 1; i <= k; i++) {\n cin >> t[i] >> u[i] >> v[i];\n if (t[i] == 1) {\n dc.insert(i, u[i], v[i]);\n } else if (t[i] == 2) {\n dc.erase(i, u[i], v[i]);\n }\n }\n dc.build();\n vector<int> ans(k + 1, -1);\n auto f = [&](int i) -> void {\n if (t[i] == 3) {\n ans[i] = dc.uf.same(u[i], v[i]);\n }\n };\n dc.run(f);\n for (int i = 1; i <= k; i++) {\n if (ans[i] != -1) {\n cout << (ans[i] ? \"YES\" : \"NO\") << endl;\n }\n }\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 15452, "score_of_the_acc": -0.1836, "final_rank": 9 }, { "submission_id": "aoj_2235_9696830", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nstruct UndoableUnionFind {\n private:\n int n;\n vector<int> par;\n stack<pair<int, int>> history;\n \n public:\n UndoableUnionFind() {}\n UndoableUnionFind(int n_) : n(n_), par(n, -1) {}\n \n int merge(int a, int b) {\n assert(0 <= a && a < n);\n assert(0 <= b && b < n);\n int x = leader(a), y = leader(b);\n history.emplace(x, par[x]);\n history.emplace(y, par[y]);\n if (x == y) {\n return x;\n }\n if (-par[x] < -par[y]) {\n swap(x, y);\n }\n par[x] += par[y];\n par[y] = x;\n return x;\n }\n \n bool same(int a, int b) {\n assert(0 <= a && a < n);\n assert(0 <= b && b < n);\n return leader(a) == leader(b);\n }\n \n int leader(int a) {\n assert(0 <= a && a < n);\n if (par[a] < 0) {\n return a;\n }\n return leader(par[a]);\n }\n \n int size(int a) {\n assert(0 <= a && a < n);\n return -par[leader(a)];\n }\n \n void undo() {\n if (history.empty()) {\n return;\n }\n par[history.top().first] = history.top().second;\n history.pop();\n par[history.top().first] = history.top().second;\n history.pop();\n }\n \n vector<vector<int>> groups() {\n vector<vector<int>> member(n);\n for (int i = 0; i < n; i++) {\n member[leader(i)].emplace_back(i);\n }\n vector<vector<int>> res;\n for (int i = 0; i < n; i++) {\n if ((int)member[i].size() > 0) {\n res.emplace_back(member[i]);\n }\n }\n return res;\n }\n};\n\nstruct OfflineDynamicConnectivity {\n using edge = pair<int, int>;\n int n, q, sz;\n vector<vector<edge>> seg;\n UndoableUnionFind uf;\n map<edge, int> cnt, appear;\n vector<pair<edge, pair<int, int>>> times;\n \n OfflineDynamicConnectivity() {}\n OfflineDynamicConnectivity(int n_, int q_) : n(n_), q(q_), sz(1), uf(n) {\n while (sz < q) {\n sz *= 2;\n }\n seg.resize(sz * 2);\n }\n \n void insert(int t, int u, int v) {\n assert(1 <= t);\n assert(0 <= u && u < n);\n assert(0 <= v && v < n);\n if (u > v) {\n swap(u, v);\n }\n edge e = {u, v};\n if (cnt[e] == 0) {\n cnt[e]++;\n appear[e] = t;\n }\n }\n \n void erase(int t, int u, int v) {\n assert(1 <= t);\n assert(0 <= u && u < n);\n assert(0 <= v && v < n);\n if (u > v) {\n swap(u, v);\n }\n edge e = {u, v};\n if (cnt[e] == 1) {\n cnt[e]--;\n pair<int, int> lr = {appear[e], t};\n times.emplace_back(e, lr);\n }\n }\n \n void add(edge e, int l, int r) {\n l += sz;\n r += sz;\n while (l < r) {\n if (l & 1) {\n seg[l].emplace_back(e);\n l++;\n }\n if (r & 1) {\n r--;\n seg[r].emplace_back(e);\n }\n l >>= 1;\n r >>= 1;\n }\n }\n \n void build() {\n for (auto [e, x] : cnt) {\n if (x > 0) {\n pair<int, int> lr = {appear[e], q};\n times.emplace_back(e, lr);\n }\n }\n for (auto [e, lr] : times) {\n auto [l, r] = lr;\n add(e, l, r);\n }\n }\n \n void run(function<void(int)> f, int i = 1) {\n for (auto e : seg[i]) {\n auto [u, v] = e;\n uf.merge(u, v);\n }\n if (i >= sz) {\n f(i - sz);\n } else {\n run(f, i * 2);\n run(f, i * 2 + 1);\n }\n for (int j = 0; j < (int)seg[i].size(); j++) {\n uf.undo();\n }\n }\n};\n\nint main() {\n int n, k;\n cin >> n >> k;\n OfflineDynamicConnectivity dc(n, k + 1);\n vector<int> t(k + 1), u(k + 1), v(k + 1);\n for (int i = 1; i <= k; i++) {\n cin >> t[i] >> u[i] >> v[i];\n if (t[i] == 1) {\n dc.insert(i, u[i], v[i]);\n } else if (t[i] == 2) {\n dc.erase(i, u[i], v[i]);\n }\n }\n dc.build();\n vector<int> ans(k + 1, -1);\n auto f = [&](int i) -> void {\n if (t[i] == 3) {\n ans[i] = dc.uf.same(u[i], v[i]);\n }\n };\n dc.run(f);\n for (int i = 1; i <= k; i++) {\n if (ans[i] != -1) {\n cout << (ans[i] ? \"YES\" : \"NO\") << endl;\n }\n }\n}", "accuracy": 0.4473684210526316, "time_ms": 30, "memory_kb": 9504, "score_of_the_acc": -0.0952, "final_rank": 18 }, { "submission_id": "aoj_2235_9679607", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#if __has_include(<atcoder/all>)\n#include <atcoder/all>\n#endif\nusing ll=long long;\nusing ull=unsigned long long;\nusing P=pair<ll,ll>;\ntemplate<typename T>using minque=priority_queue<T,vector<T>,greater<T>>;\ntemplate<typename T>bool chmax(T &a,const T &b){return (a<b?(a=b,true):false);}\ntemplate<typename T>bool chmin(T &a,const T &b){return (a>b?(a=b,true):false);}\ntemplate<typename T1,typename T2>istream &operator>>(istream &is,pair<T1,T2>&p){is>>p.first>>p.second;return is;}\ntemplate<typename T>istream &operator>>(istream &is,vector<T> &a){for(auto &i:a)is>>i;return is;}\ntemplate<typename T1,typename T2>void operator++(pair<T1,T2>&a,int n){a.first++,a.second++;}\ntemplate<typename T1,typename T2>void operator--(pair<T1,T2>&a,int n){a.first--,a.second--;}\ntemplate<typename T>void operator++(vector<T>&a,int n){for(auto &i:a)i++;}\ntemplate<typename T>void operator--(vector<T>&a,int n){for(auto &i:a)i--;}\n#define reps(i,a,n) for(int i=(a);i<int(n);i++)\n#define rep(i,n) reps(i,0,n)\n#define all(x) x.begin(),x.end()\n#define pcnt(x) __builtin_popcountll(x)\n#define fin(x) return cout<<x<<'\\n',static_cast<void>(0)\nll myceil(ll a,ll b){return (a+b-1)/b;}\ntemplate<typename T,size_t n,size_t id=0>\nauto vec(const int (&d)[n],const T &init=T()){\n if constexpr (id<n)return vector(d[id],vec<T,n,id+1>(d,init));\n else return init;\n}\n#ifdef LOCAL\n#include<debug.h>\n#else\n#define debug(...) static_cast<void>(0)\n#define debugg(...) static_cast<void>(0)\ntemplate<typename T1,typename T2>ostream &operator<<(ostream &os,const pair<T1,T2>&p){os<<p.first<<' '<<p.second;return os;}\n#endif\nvoid SOLVE();\nint main(){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout<<fixed<<setprecision(16);\n #ifdef LOCAL\n clock_t start=clock();\n #endif\n int testcase=1;\n //cin>>testcase;\n for(int i=0;i<testcase;i++){\n SOLVE();\n }\n #ifdef LOCAL\n cerr<<\"time:\";\n cerr<<(clock()-start)/1000;\n cerr<<\"ms\\n\";\n #endif\n}\nusing namespace std;\ntemplate<typename S,auto op,auto unit>\nstruct DynamicConnectivity{\nprivate:\n #define get_id(d,u,v) (n2*(d)+(long long)n*u+v)\n #define get_e(d,u) (n*(d)+u)\n // #define e_upd(u) u->ech=(u->left?u->left->ech:0)|u->e|(u->right?u->right->ech:0)\n // #define v_upd(u) u->sum=u->left&&u->right?op(u->left->sum,op(u->v,u->right->sum)):(u->left?op(u->left->sum,u->v):(u->right?op(u->v,u->right->sum):u->v))\n #define e_upd(u) u->update()\n #define v_upd(u) u->update()\n struct node{\n node *left,*right,*par;\n S v,sum;\n int from,to;\n int sz;\n int8_t e,ech;\n node(){}\n node(int u,S x=unit()):left(nullptr),right(nullptr),par(nullptr),v(x),sum(x),from(u),to(u),sz(1),e(0),ech(0){}\n node(int from_,int to_,int):left(nullptr),right(nullptr),par(nullptr),v(unit()),sum(unit()),from(from_),to(to_),sz(0),e((from_<to_)<<1),ech((from_<to_)<<1){}\n inline void update(){\n sum=v;\n sz=from==to;\n ech=e;\n if(left)sum=op(left->sum,sum),sz+=left->sz,ech|=left->ech;\n if(right)sum=op(sum,right->sum),sz+=right->sz,ech|=right->ech;\n }\n void splay(){\n while(this->par){\n node *p=this->par;\n if(!this->par->par){\n if(p->left==this){\n if(this->right)this->right->par=p;\n p->left=this->right;\n this->right=p;\n this->par=nullptr;\n p->par=this;\n }\n else{\n if(this->left)this->left->par=p;\n p->right=this->left;\n this->left=p;\n this->par=nullptr;\n p->par=this;\n }\n p->update();\n this->update();\n }\n else{\n node *pp=p->par;\n if(p->left==this){\n if(pp->left==p){\n if(this->right)this->right->par=p;\n if(p->right)p->right->par=pp;\n p->left=this->right;\n this->right=p;\n pp->left=p->right;\n p->right=pp;\n if(pp->par){\n if(pp->par->left==pp)pp->par->left=this;\n else pp->par->right=this;\n }\n this->par=pp->par;\n p->par=this;\n pp->par=p;\n }\n else{\n if(this->left)this->left->par=pp;\n if(this->right)this->right->par=p;\n pp->right=this->left;\n p->left=this->right;\n if(pp->par){\n if(pp->par->left==pp)pp->par->left=this;\n else pp->par->right=this;\n }\n this->par=pp->par;\n pp->par=this;\n p->par=this;\n this->left=pp;\n this->right=p;\n }\n }\n else{\n if(pp->left==p){\n if(this->left)this->left->par=p;\n if(this->right)this->right->par=pp;\n p->right=this->left;\n pp->left=this->right;\n if(pp->par){\n if(pp->par->left==pp)pp->par->left=this;\n else pp->par->right=this;\n }\n this->par=pp->par;\n p->par=this;\n pp->par=this;\n this->left=p;\n this->right=pp;\n }\n else{\n if(this->left)this->left->par=p;\n if(p->left)p->left->par=pp;\n p->right=this->left;\n this->left=p;\n pp->right=p->left;\n p->left=pp;\n if(pp->par){\n if(pp->par->left==pp)pp->par->left=this;\n else pp->par->right=this;\n }\n this->par=pp->par;\n p->par=this;\n pp->par=p;\n }\n }\n pp->update();\n p->update();\n this->update();\n }\n }\n this->update();\n }\n };\n unordered_map<long long,node*>ptr;\n vector<unordered_set<int>>edge;\n int dep,n;\n long long n2;\n void reroot(node *nd){\n nd->splay();\n if(!nd->left)return;\n node *le=nd->left;\n le->par=nullptr;\n nd->left=nullptr;\n nd->update();\n while(le->left)le=le->left;\n le->splay();\n le->left=nd;\n nd->par=le;\n le->update();\n nd->splay();\n }\n node *merge(node *l,node *r){\n if(!l)return r;\n if(!r)return l;\n while(l->right)l=l->right;\n l->splay();\n l->right=r;\n r->par=l;\n l->update();\n return l;\n }\n bool same(int d,int u,int v){\n node *uu=ptr[get_id(d,u,u)];\n node *vv=ptr[get_id(d,v,v)];\n uu->splay();\n vv->splay();\n return uu->par;\n }\n bool link(int d,int u,int v){\n node *uu=ptr[get_id(d,u,u)];\n node *vv=ptr[get_id(d,v,v)];\n uu->splay(),vv->splay();\n if(uu->par)return false;\n node *uv=new node(u,v,0);\n node *vu=new node(v,u,0);\n ptr[get_id(d,u,v)]=uv;\n ptr[get_id(d,v,u)]=vu;\n reroot(uu),reroot(vv);\n uv->left=uu,uu->par=uv;\n uv->right=vv,vv->par=uv;\n uv->par=vu;\n vu->left=uv;\n uv->update();\n vu->update();\n return true;\n }\n bool cut(int d,int u,int v){\n if(ptr.find(get_id(d,u,v))==ptr.end())return false;\n node *uv=ptr[get_id(d,u,v)];\n node *vu=ptr[get_id(d,v,u)];\n uv->splay(),vu->splay();\n node *p=uv->par;\n bool ri=uv==vu->right;\n if(p!=vu){\n ri=p==vu->right;\n p->par=nullptr;\n uv->splay();\n }\n if(uv->left)uv->left->par=nullptr;\n if(uv->right)uv->right->par=nullptr;\n if(ri){\n if(vu->left)vu->left->par=nullptr;\n merge(vu->left,uv->right);\n }\n else{\n if(vu->right)vu->right->par=nullptr;\n merge(uv->left,vu->right);\n }\n ptr.erase(get_id(d,u,v));\n ptr.erase(get_id(d,v,u));\n delete(uv);\n delete(vu);\n return true;\n }\n bool replace(int d,int u,int v){\n for(int i=0;i<d;i++)cut(i,u,v);\n bool res=false;\n for(int i=d;i>=0;i--){\n node *ndu=ptr[get_id(i,u,u)];\n node *ndv=ptr[get_id(i,v,v)];\n ndu->splay();\n ndv->splay();\n if(ndu->sz>ndv->sz)swap(u,v),swap(ndu,ndv);\n while(ndu->ech&2){\n while(((~ndu->e)&2)){\n if(ndu->left&&(ndu->left->ech&2))ndu=ndu->left;\n else ndu=ndu->right;\n }\n assert(ndu->ech&2);\n ndu->splay();\n ndu->e&=1;\n e_upd(ndu);\n link(i+1,ndu->from,ndu->to);\n }\n while(ndu->ech&1){\n while((~ndu->e)&1){\n if(ndu->left&&(ndu->left->ech&1))ndu=ndu->left;\n else ndu=ndu->right;\n }\n ndu->splay();\n int s=ndu->from;\n int is=get_e(i,s);\n for(auto itr=edge[is].begin();itr!=edge[is].end();){\n int t=*itr;\n itr=edge[is].erase(itr);\n edge[get_e(i,t)].erase(s);\n if(edge[get_e(i,t)].empty()){\n node *tt=ptr[get_id(i,t,t)];\n tt->splay();\n tt->e&=2;\n e_upd(tt);\n }\n if(edge[get_e(i,s)].empty()){\n node *ss=ptr[get_id(i,s,s)];\n ss->splay();\n ss->e&=2;\n e_upd(ss);\n }\n if(same(i,s,t)){\n if(edge[get_e(i+1,s)].empty()){\n node *nd=ptr[get_id(i+1,s,s)];\n nd->splay();\n nd->ech|=nd->e|=1;\n }\n if(edge[get_e(i+1,t)].empty()){\n node *nd=ptr[get_id(i+1,t,t)];\n nd->splay();\n nd->ech|=nd->e|=1;\n }\n edge[get_e(i+1,s)].insert(t);\n edge[get_e(i+1,t)].insert(s);\n }\n else{\n for(int j=0;j<=i;j++)link(j,s,t);\n res=true;\n break;\n }\n }\n if(edge[get_e(i,s)].empty()){\n node *ss=ptr[get_id(i,s,s)];\n ss->splay();\n ss->e&=2;\n e_upd(ss);\n }\n if(res)return true;\n }\n }\n return false;\n }\npublic:\n DynamicConnectivity(int n_):n(n_),dep(1),edge(n_){\n n2=(long long)n*n;\n for(int i=0;i<n;i++){\n node *nd=new node(i);\n ptr[get_id(0,i,i)]=nd;\n }\n }\n bool link(int u,int v){\n if(u==v)return false;\n if(link(0,u,v))return true;\n if(edge[u].empty()){\n node *nd=ptr[get_id(0,u,u)];\n nd->splay();\n nd->ech|=nd->e|=1;\n }\n if(edge[v].empty()){\n node *nd=ptr[get_id(0,v,v)];\n nd->splay();\n nd->ech|=nd->e|=1;\n }\n edge[u].insert(v);\n edge[v].insert(u);\n return true;\n }\n bool cut(int u,int v){\n if(u==v)return false;\n for(int i=0;i<dep;i++){\n edge[get_e(i,u)].erase(v);\n edge[get_e(i,v)].erase(u);\n if(edge[get_e(i,u)].empty()){\n node *nd=ptr[get_id(i,u,u)];\n nd->splay();\n nd->e&=2;\n e_upd(nd);\n }\n if(edge[get_e(i,v)].empty()){\n node *nd=ptr[get_id(i,v,v)];\n nd->splay();\n nd->e&=2;\n e_upd(nd);\n }\n }\n for(int i=dep-1;i>=0;i--){\n if(cut(i,u,v)){\n if(i+1==dep){\n edge.resize(edge.size()+n);\n for(int j=0;j<n;j++){\n ptr[get_id(dep,j,j)]=new node(j);\n }\n dep++;\n }\n return !replace(i,u,v);\n }\n }\n return true;\n }\n bool same(int u,int v){\n node *uu=ptr[get_id(0,u,u)];\n node *vv=ptr[get_id(0,v,v)];\n uu->splay();\n vv->splay();\n return uu->par;\n }\n void set(int u,const S&x){\n node *nd=ptr[get_id(0,u,u)];\n nd->splay();\n nd->v=x;\n v_upd(nd);\n }\n S get(int u){return ptr[get_id(0,u,u)]->v;}\n S sum(int u){\n node *nd=ptr[get_id(0,u,u)];\n nd->splay();\n return nd->sum;\n }\n int size(int u){\n node *nd=ptr[get_id(0,u,u)];\n nd->splay();\n return nd->sz;\n }\n void print(){\n vector<vector<pair<int,int>>>res(dep);\n for(auto [i,p]:ptr){\n int d=i/n2;\n int frm=(i%n2)/n;\n int to=(i%n2)%n;\n res[d].push_back({frm,to});\n }\n for(int i=0;i<dep;i++)sort(res[i].begin(),res[i].end()),debug(i,res[i]);\n }\n void baka(){\n for(int i=0;i<dep*n;i++){\n for(auto p:ptr)p.second->update();\n }\n }\n void ASSERT(){\n for(int i=0;i<edge.size();i++){\n for(auto j:edge[i]){\n if(i==j){\n cerr<<i<<endl;\n exit(1);\n }\n }\n }\n for(auto [i,p]:ptr){\n assert(p->left!=p);\n assert(p->right!=p);\n assert(p->par!=p);\n }\n }\n #undef get_id\n #undef get_e\n #undef e_upd\n #undef v_upd\n};\nint op(int x,int y){return x+y;}\nint e(){return 0;}\nvoid SOLVE(){\n int n,q;\n cin>>n>>q;\n DynamicConnectivity<int,op,e>dycon(n);\n while(q--){\n int t,u,v;\n cin>>t>>u>>v;\n if(t==1)dycon.link(u,v);\n else if(t==2)dycon.cut(u,v);\n else cout<<(dycon.same(u,v)?\"YES\\n\":\"NO\\n\");\n }\n}", "accuracy": 1, "time_ms": 290, "memory_kb": 82852, "score_of_the_acc": -1.6124, "final_rank": 15 }, { "submission_id": "aoj_2235_9679505", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntemplate<typename T>\nclass dynamic_connectivity{\n\tclass euler_tour_tree{\n\t\tpublic:\n\t\tstruct node;\n\t\tusing np=node*;\n\t\tusing lint=long long;\n\t\tstruct node{\n\t\t\tnp ch[2]={nullptr,nullptr};\n\t\t\tnp p=nullptr;\n\t\t\tint l,r,sz;\n\t\t\tT val=et,sum=et;\n\t\t\tbool exact;\n\t\t\tbool child_exact;\n\t\t\tbool edge_connected=0;\n\t\t\tbool child_edge_connected=0;\n\t\t\tnode(){}\n\t\t\tnode(int l,int r):l(l),r(r),sz(l==r),exact(l<r),child_exact(l<r){}\n\t\t\tbool is_root() {\n\t\t\t\treturn !p;\n\t\t\t}\n\t\t};\n\t\tvector<unordered_map<int,np>>ptr;\n\t\tnp get_node(int l,int r){\n\t\t\tif(ptr[l].find(r)==ptr[l].end())ptr[l][r]=new node(l,r);\n\t\t\treturn ptr[l][r];\n\t\t}\n\t\tnp root(np t){\n\t\t\tif(!t)return t;\n\t\t\twhile(t->p)t=t->p;\n\t\t\treturn t;\n\t\t}\n\t\tbool same(np s,np t){\n\t\t\tif(s)splay(s);\n\t\t\tif(t)splay(t);\n\t\t\treturn root(s)==root(t);\n\t\t}\n\t\tnp reroot(np t){\n\t\t\tauto s=split(t);\n\t\t\treturn merge(s.second,s.first);\n\t\t}\n\t\tpair<np,np> split(np s){\n\t\t\tsplay(s);\n\t\t\tnp t=s->ch[0];\n\t\t\tif(t)t->p=nullptr;\n\t\t\ts->ch[0]=nullptr;\n\t\t\treturn {t,update(s)};\n\t\t}\n\t\tpair<np,np> split2(np s){\n\t\t\tsplay(s);\n\t\t\tnp t=s->ch[0];\n\t\t\tnp u=s->ch[1];\n\t\t\tif(t)t->p=nullptr;\n\t\t\ts->ch[0]=nullptr;\n\t\t\tif(u)u->p=nullptr;\n\t\t\ts->ch[1]=nullptr;\n\t\t\treturn {t,u};\n\t\t}\n\t\ttuple<np,np,np> split(np s,np t){\n\t\t\tauto u=split2(s);\n\t\t\tif(same(u.first,t)){\n\t\t\t\tauto r=split2(t);\n\t\t\t\treturn make_tuple(r.first,r.second,u.second);\n\t\t\t}else{\n\t\t\t\tauto r=split2(t);\n\t\t\t\treturn make_tuple(u.first,r.first,r.second);\n\t\t\t}\n\t\t}\n\t\ttemplate<typename First, typename... Rest>\n\t\tnp merge(First s,Rest... t){\n\t\t\treturn merge(s,merge(t...));\n\t\t}\n\t\tnp merge(np s,np t){\n\t\t\tif(!s)return t;\n\t\t\tif(!t)return s;\n\t\t\twhile(s->ch[1])s=s->ch[1];\n\t\t\tsplay(s);\n\t\t\ts->ch[1]=t;\n\t\t\tif(t)t->p=s;\n\t\t\treturn update(s);\n\t\t}\n\t\tint size(np t){return t?t->sz:0;}\n\t\tnp update(np t){\n\t\t\tt->sum=et;\n\t\t\tif(t->ch[0])t->sum=fn(t->sum,t->ch[0]->sum);\n\t\t\tif(t->l==t->r)t->sum=fn(t->sum,t->val);\n\t\t\tif(t->ch[1])t->sum=fn(t->sum,t->ch[1]->sum);\n\t\t\tt->sz=size(t->ch[0])+(t->l==t->r)+size(t->ch[1]);\n\t\t\tt->child_edge_connected=(t->ch[0]?t->ch[0]->child_edge_connected:0)|(t->edge_connected)|(t->ch[1]?t->ch[1]->child_edge_connected:0);\n\t\t\tt->child_exact=(t->ch[0]?t->ch[0]->child_exact:0)|(t->exact)|(t->ch[1]?t->ch[1]->child_exact:0);\n\t\t\treturn t;\n\t\t}\n\t\tvoid push(np t){\n\t\t\t//遅延評価予定\n\t\t}\n\t\tvoid rot(np t,bool b){\n\t\t\tnp x=t->p,y=x->p;\n\t\t\tif((x->ch[1-b]=t->ch[b]))t->ch[b]->p=x;\n\t\t\tt->ch[b]=x,x->p=t;\n\t\t\tupdate(x);update(t);\n\t\t\tif((t->p=y)){\n\t\t\t\tif(y->ch[0]==x)y->ch[0]=t;\n\t\t\t\tif(y->ch[1]==x)y->ch[1]=t;\n\t\t\t\tupdate(y);\n\t\t\t}\n\t\t}\n\t\tvoid splay(np t){\n\t\t\tpush(t);\n\t\t\twhile(!t->is_root()){\n\t\t\t\tnp q=t->p;\n\t\t\t\tif(q->is_root()){\n\t\t\t\t\tpush(q), push(t);\n\t\t\t\t\trot(t,q->ch[0]==t);\n\t\t\t\t}else{\n\t\t\t\t\tnp r=q->p;\n\t\t\t\t\tpush(r), push(q), push(t);\n\t\t\t\t\tbool b=r->ch[0]==q;\n\t\t\t\t\tif(q->ch[1-b]==t)rot(q,b),rot(t,b);\n\t\t\t\t\telse rot(t,1-b),rot(t,b);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tvoid debug(np t){\n\t\t\tif(!t)return;\n\t\t\tdebug(t->ch[0]);\n\t\t\tcerr<<t->l<<\"-\"<<t->r<<\" \";\n\t\t\tdebug(t->ch[1]);\n\t\t}\n\t\tpublic:\n\t\teuler_tour_tree(){}\n\t\teuler_tour_tree(int sz){\n\t\t\tptr.resize(sz);\n\t\t\tfor(int i=0;i<sz;i++)ptr[i][i]=new node(i,i);\n\t\t}\n\t\tint size(int s){\n\t\t\tnp t=get_node(s,s);\n\t\t\tsplay(t);\n\t\t\treturn t->sz;\n\t\t}\n\t\tbool same(int s,int t){\n return same(get_node(s,s),get_node(t,t));\n }\n\t\tvoid set_size(int sz){\n\t\t\tptr.resize(sz);\n\t\t\tfor(int i=0;i<sz;i++)ptr[i][i]=new node(i,i);\n\t\t}\n\t\tvoid update(int s,T x){\n\t\t\tnp t=get_node(s,s);\n\t\t\tsplay(t);\n\t\t\tt->val=fn(t->val,x);\n\t\t\tupdate(t);\n\t\t}\n template<typename F>\n\t\tvoid edge_update(int s,F g){\n\t\t\tnp t=get_node(s,s);\n\t\t\tsplay(t);\n\t\t\tfunction<void(np)>dfs=[&](np t){\n\t\t\t\tassert(t);\n if(t->l<t->r&&t->exact){\n\t\t\t\t\tsplay(t);\n\t\t\t\t\tt->exact=0;\n\t\t\t\t\tupdate(t);\n\t\t\t\t\tg(t->l,t->r);\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tif(t->ch[0]&&t->ch[0]->child_exact)dfs(t->ch[0]);\n\t\t\t\telse dfs(t->ch[1]);\n\t\t\t};\n\t\t\twhile(t&&t->child_exact){\n\t\t\t\tdfs(t);\n\t\t\t\tsplay(t);\n\t\t\t}\n\t\t}\n template<typename F>\n\t\tbool try_reconnect(int s,F f){\n\t\t\tnp t=get_node(s,s);\n\t\t\tsplay(t);\n\t\t\tfunction<bool(np)>dfs=[&](np t)->bool{\n\t\t\t\tassert(t);\n\t\t\t\tif(t->edge_connected){\n\t\t\t\t\tsplay(t);\n\t\t\t\t\treturn f(t->l);\n\t\t\t\t}\n\t\t\t\tif(t->ch[0]&&t->ch[0]->child_edge_connected)return dfs(t->ch[0]);\n\t\t\t\telse return dfs(t->ch[1]);\n\t\t\t};\n\t\t\twhile(t->child_edge_connected){\n\t\t\t\tif(dfs(t))return 1;\n\t\t\t\tsplay(t);\n\t\t\t}\n\t\t\treturn 0;\n\t\t}\n\t\tvoid edge_connected_update(int s,bool b){\n\t\t\tnp t=get_node(s,s);\n\t\t\tsplay(t);\n\t\t\tt->edge_connected=b;\n\t\t\tupdate(t);\n\t\t}\n\t\tbool link(int l,int r){\n\t\t\tif(same(l,r))return 0;\n\t\t\tmerge(reroot(get_node(l,l)),get_node(l,r),reroot(get_node(r,r)),get_node(r,l));\n\t\t\treturn 1;\n\t\t}\n\t\tbool cut(int l,int r){\n\t\t\tif(ptr[l].find(r)==ptr[l].end())return 0;\n\t\t\tnp s,t,u;\n\t\t\ttie(s,t,u)=split(get_node(l,r),get_node(r,l));\n\t\t\tmerge(s,u);\n\t\t\tnp p=ptr[l][r];\n\t\t\tnp q=ptr[r][l];\n\t\t\tptr[l].erase(r);\n\t\t\tptr[r].erase(l);\n\t\t\tdelete p;delete q;\n\t\t\treturn 1;\n\t\t}\n\t\tT get_sum(int p,int v){\n\t\t\tcut(p,v);\n\t\t\tnp t=get_node(v,v);\n\t\t\tsplay(t);\n\t\t\tT res=t->sum;\n\t\t\tlink(p,v);\n\t\t\treturn res;\n\t\t}\n\t\tT get_sum(int s){\n\t\t\tnp t=get_node(s,s);\n\t\t\tsplay(t);\n\t\t\treturn t->sum;\n\t\t}\n\t};\n\tint dep=1;\n\tvector<euler_tour_tree> ett;\n\tvector<vector<unordered_set<int>>>edges;\n\tint sz;\n\tpublic:\n\tdynamic_connectivity(int sz):sz(sz){\n\t\tett.emplace_back(sz);\n\t\tedges.emplace_back(sz);\n\t}\n\tbool link(int s,int t){\n\t\tif(s==t)return 0;\n\t\tif(ett[0].link(s,t))return 1;\n\t\tedges[0][s].insert(t);\n\t\tedges[0][t].insert(s);\n\t\tif(edges[0][s].size()==1)ett[0].edge_connected_update(s,1);\n\t\tif(edges[0][t].size()==1)ett[0].edge_connected_update(t,1);\n\t\treturn 0;\n\t}\n\tbool same(int s,int t){\n\t\treturn ett[0].same(s,t);\n\t}\n\tint size(int s){\n\t\treturn ett[0].size(s);\n\t}\n\tvector<int>get_vertex(int s){\n\t\treturn ett[0].vertex_list(s);\n\t}\n\tvoid update(int s,T x){\n\t\tett[0].update(s,x);\n\t}\n\tT get_sum(int s){\n\t\treturn ett[0].get_sum(s);\n\t}\n\tbool cut(int s,int t){\n\t\tif(s==t)return 0;\n\t\tfor(int i=0;i<dep;i++){\n\t\t\tedges[i][s].erase(t);\n\t\t\tedges[i][t].erase(s);\n\t\t\tif(edges[i][s].size()==0)ett[i].edge_connected_update(s,0);\n\t\t\tif(edges[i][t].size()==0)ett[i].edge_connected_update(t,0);\n\t\t}\n\t\tfor(int i=dep-1;i>=0;i--){\n\t\t\tif(ett[i].cut(s,t)){\n\t\t\t\tif(dep-1==i){\n\t\t\t\t\tdep++;\n\t\t\t\t\tett.emplace_back(sz);\n\t\t\t\t\tedges.emplace_back(sz);\n\t\t\t\t}\n\t\t\t\treturn !try_reconnect(s,t,i);\n\t\t\t}\n\t\t}\n\t\treturn 0;\n\t}\n\tbool try_reconnect(int s,int t,int k){\n\t\tfor(int i=0;i<k;i++){\n\t\t\tett[i].cut(s,t);\n\t\t}\n\t\tfor(int i=k;i>=0;i--){\n\t\t\tif(ett[i].size(s)>ett[i].size(t))swap(s,t);\n\t\t\tauto g=[&](int s,int t){ett[i+1].link(s,t);};\n\t\t\tett[i].edge_update(s,g);\n\t\t\tauto f=[&](int x)->bool{\n\t\t\t\tfor(auto itr=edges[i][x].begin();itr!=edges[i][x].end();){\n\t\t\t\t\tauto y=*itr;\n\t\t\t\t\titr=edges[i][x].erase(itr);\n\t\t\t\t\tedges[i][y].erase(x);\n\t\t\t\t\tif(edges[i][x].size()==0)ett[i].edge_connected_update(x,0);\n\t\t\t\t\tif(edges[i][y].size()==0)ett[i].edge_connected_update(y,0);\n\t\t\t\t\tif(ett[i].same(x,y)){\n\t\t\t\t\t\tedges[i+1][x].insert(y);\n\t\t\t\t\t\tedges[i+1][y].insert(x);\n\t\t\t\t\t\tif(edges[i+1][x].size()==1)ett[i+1].edge_connected_update(x,1);\n\t\t\t\t\t\tif(edges[i+1][y].size()==1)ett[i+1].edge_connected_update(y,1);\n\t\t\t\t\t}else{\n\t\t\t\t\t\tfor(int j=0;j<=i;j++){\n\t\t\t\t\t\t\tett[j].link(x,y);\n\t\t\t\t\t\t}\n\t\t\t\t\t\treturn 1;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\treturn 0;\n\t\t\t};\n\t\t\tif(ett[i].try_reconnect(s,f))return 1;\n\t\t}\n\t\treturn 0;\n\t}\n\tconstexpr static T et=T();\n\tconstexpr static T fn(T s,T t){\n\t\treturn s+t;\n\t}\n};\nint main(){\n int n,q;\n cin>>n>>q;\n dynamic_connectivity<int>dycon(n);\n while(q--){\n int t,u,v;\n cin>>t>>u>>v;\n if(t==1)dycon.link(u,v);\n else if(t==2)dycon.cut(u,v);\n else cout<<(dycon.same(u,v)?\"YES\\n\":\"NO\\n\");\n }\n}", "accuracy": 1, "time_ms": 330, "memory_kb": 110188, "score_of_the_acc": -2, "final_rank": 16 }, { "submission_id": "aoj_2235_9679466", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#if __has_include(<atcoder/all>)\n#include <atcoder/all>\n#endif\nusing ll=long long;\nusing ull=unsigned long long;\nusing P=pair<ll,ll>;\ntemplate<typename T>using minque=priority_queue<T,vector<T>,greater<T>>;\ntemplate<typename T>bool chmax(T &a,const T &b){return (a<b?(a=b,true):false);}\ntemplate<typename T>bool chmin(T &a,const T &b){return (a>b?(a=b,true):false);}\ntemplate<typename T1,typename T2>istream &operator>>(istream &is,pair<T1,T2>&p){is>>p.first>>p.second;return is;}\ntemplate<typename T>istream &operator>>(istream &is,vector<T> &a){for(auto &i:a)is>>i;return is;}\ntemplate<typename T1,typename T2>void operator++(pair<T1,T2>&a,int n){a.first++,a.second++;}\ntemplate<typename T1,typename T2>void operator--(pair<T1,T2>&a,int n){a.first--,a.second--;}\ntemplate<typename T>void operator++(vector<T>&a,int n){for(auto &i:a)i++;}\ntemplate<typename T>void operator--(vector<T>&a,int n){for(auto &i:a)i--;}\n#define reps(i,a,n) for(int i=(a);i<int(n);i++)\n#define rep(i,n) reps(i,0,n)\n#define all(x) x.begin(),x.end()\n#define pcnt(x) __builtin_popcountll(x)\n#define fin(x) return cout<<x<<'\\n',static_cast<void>(0)\nll myceil(ll a,ll b){return (a+b-1)/b;}\ntemplate<typename T,size_t n,size_t id=0>\nauto vec(const int (&d)[n],const T &init=T()){\n if constexpr (id<n)return vector(d[id],vec<T,n,id+1>(d,init));\n else return init;\n}\n#ifdef LOCAL\n#include<debug.h>\n#else\n#define debug(...) static_cast<void>(0)\n#define debugg(...) static_cast<void>(0)\ntemplate<typename T1,typename T2>ostream &operator<<(ostream &os,const pair<T1,T2>&p){os<<p.first<<' '<<p.second;return os;}\n#endif\nvoid SOLVE();\nint main(){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout<<fixed<<setprecision(16);\n #ifdef LOCAL\n clock_t start=clock();\n #endif\n int testcase=1;\n //cin>>testcase;\n for(int i=0;i<testcase;i++){\n SOLVE();\n }\n #ifdef LOCAL\n cerr<<\"time:\";\n cerr<<(clock()-start)/1000;\n cerr<<\"ms\\n\";\n #endif\n}\nusing namespace std;\ntemplate<typename S,auto op,auto unit>\nstruct DynamicConnectivity{\nprivate:\n #define get_id(d,u,v) (n2*(d)+(long long)n*u+v)\n #define get_e(d,u) (n*(d)+u)\n // #define e_upd(u) u->ech=(u->left?u->left->ech:0)|u->e|(u->right?u->right->ech:0)\n // #define v_upd(u) u->sum=u->left&&u->right?op(u->left->sum,op(u->v,u->right->sum)):(u->left?op(u->left->sum,u->v):(u->right?op(u->v,u->right->sum):u->v))\n #define e_upd(u) u->update()\n #define v_upd(u) u->update()\n struct node{\n node *left,*right,*par;\n S v,sum;\n int from,to;\n int sz;\n int8_t e,ech;\n node(){}\n node(int u,S x=unit()):left(nullptr),right(nullptr),par(nullptr),v(x),sum(x),from(u),to(u),sz(1),e(0),ech(0){}\n node(int from_,int to_,int):left(nullptr),right(nullptr),par(nullptr),v(unit()),sum(unit()),from(from_),to(to_),sz(0),e((from_<to_)<<1),ech((from_<to_)<<1){}\n inline void update(){\n sum=v;\n sz=from==to;\n ech=e;\n if(left)sum=op(left->sum,sum),sz+=left->sz,ech|=left->ech;\n if(right)sum=op(sum,right->sum),sz+=right->sz,ech|=right->ech;\n }\n void splay(){\n while(this->par){\n node *p=this->par;\n if(!this->par->par){\n if(p->left==this){\n if(this->right)this->right->par=p;\n p->left=this->right;\n this->right=p;\n this->par=nullptr;\n p->par=this;\n }\n else{\n if(this->left)this->left->par=p;\n p->right=this->left;\n this->left=p;\n this->par=nullptr;\n p->par=this;\n }\n p->update();\n this->update();\n }\n else{\n node *pp=p->par;\n if(p->left==this){\n if(pp->left==p){\n if(this->right)this->right->par=p;\n if(p->right)p->right->par=pp;\n p->left=this->right;\n this->right=p;\n pp->left=p->right;\n p->right=pp;\n if(pp->par){\n if(pp->par->left==pp)pp->par->left=this;\n else pp->par->right=this;\n }\n this->par=pp->par;\n p->par=this;\n pp->par=p;\n }\n else{\n if(this->left)this->left->par=pp;\n if(this->right)this->right->par=p;\n pp->right=this->left;\n p->left=this->right;\n if(pp->par){\n if(pp->par->left==pp)pp->par->left=this;\n else pp->par->right=this;\n }\n this->par=pp->par;\n pp->par=this;\n p->par=this;\n this->left=pp;\n this->right=p;\n }\n }\n else{\n if(pp->left==p){\n if(this->left)this->left->par=p;\n if(this->right)this->right->par=pp;\n p->right=this->left;\n pp->left=this->right;\n if(pp->par){\n if(pp->par->left==pp)pp->par->left=this;\n else pp->par->right=this;\n }\n this->par=pp->par;\n p->par=this;\n pp->par=this;\n this->left=p;\n this->right=pp;\n }\n else{\n if(this->left)this->left->par=p;\n if(p->left)p->left->par=pp;\n p->right=this->left;\n this->left=p;\n pp->right=p->left;\n p->left=pp;\n if(pp->par){\n if(pp->par->left==pp)pp->par->left=this;\n else pp->par->right=this;\n }\n this->par=pp->par;\n p->par=this;\n pp->par=p;\n }\n }\n pp->update();\n p->update();\n this->update();\n }\n }\n this->update();\n }\n };\n unordered_map<long long,node*>ptr;\n vector<unordered_set<int>>edge;\n int dep,n;\n long long n2;\n void reroot(node *nd){\n nd->splay();\n if(!nd->left)return;\n node *le=nd->left;\n le->par=nullptr;\n nd->left=nullptr;\n nd->update();\n while(le->left)le=le->left;\n le->splay();\n le->left=nd;\n nd->par=le;\n le->update();\n nd->splay();\n }\n node *merge(node *l,node *r){\n if(!l)return r;\n if(!r)return l;\n while(l->right)l=l->right;\n l->splay();\n l->right=r;\n r->par=l;\n l->update();\n return l;\n }\n bool same(int d,int u,int v){\n node *uu=ptr[get_id(d,u,u)];\n node *vv=ptr[get_id(d,v,v)];\n uu->splay();\n vv->splay();\n return uu->par;\n }\n bool link(int d,int u,int v){\n node *uu=ptr[get_id(d,u,u)];\n node *vv=ptr[get_id(d,v,v)];\n uu->splay(),vv->splay();\n if(uu->par)return false;\n node *uv=new node(u,v,0);\n node *vu=new node(v,u,0);\n ptr[get_id(d,u,v)]=uv;\n ptr[get_id(d,v,u)]=vu;\n reroot(uu),reroot(vv);\n uv->left=uu,uu->par=uv;\n uv->right=vv,vv->par=uv;\n uv->par=vu;\n vu->left=uv;\n uv->update();\n vu->update();\n return true;\n }\n bool cut(int d,int u,int v){\n if(ptr.find(get_id(d,u,v))==ptr.end())return false;\n node *uv=ptr[get_id(d,u,v)];\n node *vu=ptr[get_id(d,v,u)];\n uv->splay(),vu->splay();\n node *p=uv->par;\n bool ri=uv==vu->right;\n if(p!=vu){\n ri=p==vu->right;\n p->par=nullptr;\n uv->splay();\n }\n if(uv->left)uv->left->par=nullptr;\n if(uv->right)uv->right->par=nullptr;\n if(ri){\n if(vu->left)vu->left->par=nullptr;\n merge(vu->left,uv->right);\n }\n else{\n if(vu->right)vu->right->par=nullptr;\n merge(uv->left,vu->right);\n }\n ptr.erase(get_id(d,u,v));\n ptr.erase(get_id(d,v,u));\n delete(uv);\n delete(vu);\n return true;\n }\n bool replace(int d,int u,int v){\n for(int i=0;i<d;i++)cut(i,u,v);\n bool res=false;\n for(int i=d;i>=0;i--){\n node *ndu=ptr[get_id(i,u,u)];\n node *ndv=ptr[get_id(i,v,v)];\n ndu->splay();\n ndv->splay();\n if(ndu->sz>ndv->sz)swap(u,v),swap(ndu,ndv);\n while(ndu->ech&2){\n while(((~ndu->e)&2)){\n if(ndu->left&&(ndu->left->ech&2))ndu=ndu->left;\n else ndu=ndu->right;\n }\n ndu->splay();\n ndu->e&=1;\n e_upd(ndu);\n link(i+1,ndu->from,ndu->to);\n }\n while(ndu->ech&1){\n while((~ndu->e)&1){\n if(ndu->left&&(ndu->left->ech&1))ndu=ndu->left;\n else ndu=ndu->right;\n }\n ndu->splay();\n int s=ndu->from;\n int is=get_e(i,s);\n debug(u,v,s,edge[is]);\n for(auto itr=edge[is].begin();itr!=edge[is].end();){\n int t=*itr;\n itr=edge[is].erase(itr);\n edge[get_e(i,t)].erase(s);\n if(edge[get_e(i,t)].empty()){\n node *tt=ptr[get_id(i,t,t)];\n tt->splay();\n tt->e&=2;\n e_upd(tt);\n }\n if(edge[get_e(i,s)].empty()){\n node *ss=ptr[get_id(i,s,s)];\n ss->splay();\n ss->e&=2;\n e_upd(ss);\n }\n if(same(i,s,t)){\n if(edge[get_e(i,s)].empty()){\n node *nd=ptr[get_id(i,s,s)];\n nd->splay();\n nd->ech|=nd->e|=1;\n }\n if(edge[get_e(i,t)].empty()){\n node *nd=ptr[get_id(i,t,t)];\n nd->splay();\n nd->ech|=nd->e|=1;\n }\n edge[get_e(i+1,s)].insert(t);\n edge[get_e(i+1,t)].insert(s);\n }\n else{\n for(int j=0;j<=i;j++)link(j,s,t);\n res=true;\n break;\n }\n }\n if(edge[get_e(i,s)].empty()){\n node *ss=ptr[get_id(i,s,s)];\n ss->splay();\n ss->e&=2;\n e_upd(ss);\n }\n if(res)return true;\n }\n }\n return false;\n }\npublic:\n DynamicConnectivity(int n_):n(n_),dep(1),edge(n_){\n n2=(long long)n*n;\n for(int i=0;i<n;i++){\n node *nd=new node(i);\n ptr[get_id(0,i,i)]=nd;\n }\n }\n bool link(int u,int v){\n if(u==v)return false;\n if(link(0,u,v))return true;\n if(edge[u].empty()){\n node *nd=ptr[get_id(0,u,u)];\n nd->splay();\n nd->ech|=nd->e|=1;\n }\n if(edge[v].empty()){\n node *nd=ptr[get_id(0,v,v)];\n nd->splay();\n nd->ech|=nd->e|=1;\n }\n edge[u].insert(v);\n edge[v].insert(u);\n return true;\n }\n bool cut(int u,int v){\n if(u==v)return false;\n for(int i=0;i<dep;i++){\n edge[get_e(i,u)].erase(v);\n edge[get_e(i,v)].erase(u);\n if(edge[get_e(i,u)].empty()){\n node *nd=ptr[get_id(i,u,u)];\n nd->splay();\n nd->e&=2;\n e_upd(nd);\n }\n if(edge[get_e(i,v)].empty()){\n node *nd=ptr[get_id(i,v,v)];\n nd->splay();\n nd->e&=2;\n e_upd(nd);\n }\n }\n for(int i=dep-1;i>=0;i--){\n if(cut(i,u,v)){\n if(i+1==dep){\n edge.resize(edge.size()+n);\n for(int j=0;j<n;j++){\n ptr[get_id(dep,j,j)]=new node(j);\n }\n dep++;\n }\n return !replace(i,u,v);\n }\n }\n return true;\n }\n bool same(int u,int v){\n node *uu=ptr[get_id(0,u,u)];\n node *vv=ptr[get_id(0,v,v)];\n uu->splay();\n vv->splay();\n return uu->par;\n }\n void set(int u,const S&x){\n node *nd=ptr[get_id(0,u,u)];\n nd->splay();\n nd->v=x;\n v_upd(nd);\n }\n S get(int u){return ptr[get_id(0,u,u)]->v;}\n S sum(int u){\n node *nd=ptr[get_id(0,u,u)];\n nd->splay();\n return nd->sum;\n }\n int size(int u){\n node *nd=ptr[get_id(0,u,u)];\n nd->splay();\n return nd->sz;\n }\n void print(){\n vector<vector<pair<int,int>>>res(dep);\n for(auto [i,p]:ptr){\n int d=i/n2;\n int frm=(i%n2)/n;\n int to=(i%n2)%n;\n res[d].push_back({frm,to});\n }\n for(int i=0;i<dep;i++)sort(res[i].begin(),res[i].end()),debug(i,res[i]);\n }\n void baka(){\n for(int i=0;i<dep*n;i++){\n for(auto p:ptr)p.second->update();\n }\n }\n void ASSERT(){\n for(int i=0;i<edge.size();i++){\n for(auto j:edge[i]){\n if(i==j){\n cerr<<i<<endl;\n exit(1);\n }\n }\n }\n for(auto [i,p]:ptr){\n assert(p->left!=p);\n assert(p->right!=p);\n assert(p->par!=p);\n }\n }\n #undef get_id\n #undef get_e\n #undef e_upd\n #undef v_upd\n};\nint op(int x,int y){return x+y;}\nint e(){return 0;}\nvoid SOLVE(){\n int n,q;\n cin>>n>>q;\n DynamicConnectivity<int,op,e>dycon(n);\n while(q--){\n int t,u,v;\n cin>>t>>u>>v;\n if(t==1)dycon.link(u,v);\n else if(t==2)dycon.cut(u,v);\n else cout<<(dycon.same(u,v)?\"YES\\n\":\"NO\\n\");\n }\n}", "accuracy": 0.6052631578947368, "time_ms": 30, "memory_kb": 11136, "score_of_the_acc": -0.1108, "final_rank": 17 }, { "submission_id": "aoj_2235_9430268", "code_snippet": "#include <bits/stdc++.h>\n\n\n\n\nnamespace zawa {\n\nusing i16 = std::int16_t;\nusing i32 = std::int32_t;\nusing i64 = std::int64_t;\nusing i128 = __int128_t;\n\nusing u8 = std::uint8_t;\nusing u16 = std::uint16_t;\nusing u32 = std::uint32_t;\nusing u64 = std::uint64_t;\n\nusing usize = std::size_t;\n\n} // namespace zawa\n\n\nnamespace zawa {\n\nvoid SetFastIO() {\n std::cin.tie(nullptr)->sync_with_stdio(false);\n}\n\nvoid SetPrecision(u32 dig) {\n std::cout << std::fixed << std::setprecision(dig);\n}\n\n} // namespace zawa\n\n\n\n\nnamespace zawa {\n\ntemplate <class T>\nclass UndoableVector {\npublic:\n using Iterator = typename std::vector<T>::const_iterator;\n\n UndoableVector() = default;\n\n UndoableVector(usize n) : vec_(n) {}\n\n UndoableVector(usize n, const T& v) : vec_(n, v) {}\n\n UndoableVector(const std::vector<T>& vec) : vec_{vec} {}\n\n UndoableVector(std::vector<T>&& vec) : vec_{std::move(vec)} {}\n \n inline usize size() const {\n return vec_.size();\n }\n\n Iterator begin() const {\n return vec_.begin();\n }\n\n Iterator end() const {\n return vec_.end();\n }\n\n void assign(usize i, const T& v) {\n assert(i < size());\n save(i);\n vec_[i] = v;\n }\n\n T operator[](usize i) const {\n assert(i < size());\n return vec_[i];\n }\n\n void undo() {\n assert(history_.size());\n auto [i, v]{history_.back()};\n vec_[i] = v;\n history_.pop_back();\n }\n\nprivate:\n std::vector<T> vec_; \n std::vector<std::pair<usize, T>> history_;\n\n void save(usize i) {\n history_.emplace_back(i, vec_[i]);\n }\n};\n\n} // namespace zawa\n\n\nnamespace zawa {\n\nclass UndoableDisjointSetUnion {\npublic:\n UndoableDisjointSetUnion() = default;\n\n UndoableDisjointSetUnion(usize n) : n_{n}, data_(n, -1) {}\n\n u32 leader(u32 v) const {\n return data_[v] < 0 ? v : leader(data_[v]);\n }\n\n inline usize size() const {\n return n_;\n }\n\n inline usize size(u32 v) const {\n assert(v < size());\n return static_cast<usize>(-data_[leader(v)]);\n }\n\n bool same(u32 u, u32 v) const {\n assert(u < size());\n assert(v < size());\n return leader(u) == leader(v);\n }\n\n bool merge(u32 u, u32 v) {\n u = leader(u);\n v = leader(v);\n if (u == v) {\n data_.assign(u, data_[u]);\n data_.assign(v, data_[v]);\n return false;\n }\n else {\n if (data_[u] > data_[v]) std::swap(u, v);\n data_.assign(u, data_[u] + data_[v]);\n data_.assign(v, u);\n return true;\n }\n }\n\n void undo() {\n data_.undo();\n data_.undo();\n }\n\n std::vector<std::vector<u32>> enumerate() const {\n std::vector<std::vector<u32>> res(n_);\n for (u32 v{} ; v < n_ ; v++) {\n res[leader(v)].push_back(v);\n }\n res.erase(std::remove_if(res.begin(), res.end(),\n [](const auto& arr) -> bool { return arr.empty(); }), res.end());\n return res;\n }\n\nprivate:\n usize n_{};\n UndoableVector<i32> data_{};\n};\n\n} // namespace zawa\nusing namespace zawa;\n\nint main() {\n SetFastIO();\n\n int N, K;\n std::cin >> N >> K;\n std::vector<int> T(K), U(K), V(K);\n for (int i{} ; i < K ; i++) {\n std::cin >> T[i] >> U[i] >> V[i];\n }\n std::vector<std::pair<int, int>> edges;\n for (int i{} ; i < K ; i++) {\n if (T[i] == 3) continue;\n edges.push_back(std::make_pair(U[i], V[i]));\n }\n std::sort(edges.begin(), edges.end());\n edges.erase(std::unique(edges.begin(), edges.end()), edges.end());\n std::vector<int> last(edges.size(), -1);\n std::vector<std::vector<int>> span(2 * K);\n for (int i{} ; i < K ; i++) {\n if (T[i] == 3) continue;\n int id{(int)std::distance(edges.begin(), std::lower_bound(edges.begin(), edges.end(), std::make_pair(U[i], V[i])))};\n if (T[i] == 1) {\n assert(last[id] == -1);\n last[id] = i;\n }\n else if (T[i] == 2) {\n assert(last[id] != -1);\n int L{last[id]}, R{i};\n last[id] = -1;\n for (L += K, R += K ; L < R ; L >>= 1, R >>= 1) {\n if (L & 1) {\n span[L].push_back(id);\n L++;\n }\n if (R & 1) {\n R--;\n span[R].push_back(id);\n }\n }\n }\n }\n for (int i{} ; i < (int)last.size() ; i++) {\n if (last[i] == -1) continue;\n int L{last[i]}, R{K};\n for (L += K, R += K ; L < R ; L >>= 1, R >>= 1) {\n if (L & 1) {\n span[L].push_back(i);\n L++;\n }\n if (R & 1) {\n R--;\n span[R].push_back(i);\n }\n }\n }\n std::vector<bool> ans(K);\n UndoableDisjointSetUnion dsu(N);\n auto dfs{[&](auto dfs, int v) -> void {\n // std::cout << v << std::endl;\n for (auto i : span[v]) {\n auto [u, v]{edges[i]};\n // std::cout << '(' << u << ',' << v << ')' << ' ';\n dsu.merge(u, v);\n }\n // std::cout << std::endl;\n if (v < K) {\n dfs(dfs, 2 * v);\n dfs(dfs, 2 * v + 1);\n }\n else {\n if (T[v - K] == 3) {\n ans[v - K] = dsu.same(U[v - K], V[v - K]);\n }\n }\n for (int i{} ; i < (int)span[v].size() ; i++) {\n dsu.undo();\n }\n }};\n dfs(dfs, 1);\n for (int i{} ; i < K ; i++) {\n if (T[i] == 3) {\n std::cout << (ans[i] ? \"YES\" : \"NO\") << '\\n';\n }\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 9920, "score_of_the_acc": -0.0679, "final_rank": 1 }, { "submission_id": "aoj_2235_9159772", "code_snippet": "/**\n* author: shu8Cream\n* created: 2024/04/09 20:51:55\n**/\n\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\nstruct UndoableUnionFind{\n int n;\n vector<int> par;\n vector<pair<int,int>> history;\n int group_cnt;\n UndoableUnionFind(){}\n UndoableUnionFind(int n=0): n(n), par(n, -1), group_cnt(n) {}\n int find(int x){\n if(par[x] < 0) return x;\n return find(par[x]);\n }\n bool merge(int x, int y){\n x = find(x); y = find(y);\n history.emplace_back(x,par[x]);\n history.emplace_back(y,par[y]);\n if(x == y) return false;\n if(par[x] > par[y]) swap(x,y); //マージテク\n par[x] += par[y];\n par[y] = x;\n group_cnt--;\n return true;\n }\n bool same(int x, int y) {return find(x) == find(y);}\n int size(int x) {return -par[find(x)];}\n\n void undo(){\n int u = history.back().first;\n par[history.back().first] = history.back().second;\n history.pop_back();\n int v = history.back().first;\n par[history.back().first] = history.back().second;\n history.pop_back();\n if(u!=v) group_cnt++;\n }\n\n void snapshot() {\n while (!history.empty()) history.pop_back();\n }\n\n void rollback() {\n while (!history.empty()) undo();\n }\n};\n\ntemplate<typename Q>\nstruct OfflineDynamicConnectivity{\n struct Edge_info{\n int from, to, start, end;\n Edge_info(int from, int to, int start, int end):\n from(from),to(to),start(start),end(end)\n {}\n };\n int n,q,size,time;\n vector<Edge_info> infos;\n map<pair<int,int>,int> addtime;\n vector<vector<pair<int,int> > > seg;\n vector<Q> query_types;\n UndoableUnionFind uf;\n\n OfflineDynamicConnectivity(int n, int q): n(n),q(q),uf(n),time(0),query_types(q){\n for(size=1; size<q; size<<=1);\n seg.resize(size<<1);\n }\n void link(Q type, int u, int v){\n if(u>v) swap(u,v);\n addtime[{u,v}] = time;\n query_types[time++] = type;\n }\n void cut(Q type, int u, int v){\n if(u>v) swap(u,v);\n auto e = make_pair(u,v);\n infos.emplace_back(u,v,addtime[e],time);\n query_types[time++] = type;\n addtime.erase(e);\n }\n void query(Q type){\n query_types[time++] = type;\n }\n template<typename T>\n void dfs(const T &func, int node=1){\n for(auto j : seg[node]) uf.merge(j.first, j.second);\n if(node<size){\n dfs(func, node << 1);\n dfs(func, node << 1 | 1);\n }else{\n func(node-size, uf);\n }\n for(auto j : seg[node]) uf.undo();\n }\n template<typename T>\n void run(const T &func, int node=1){\n for(auto &[e,time]:addtime) infos.emplace_back(e.first,e.second,time,size);\n for(auto i:infos){\n int l = i.start+size;\n int r = i.end+size;\n for(; l<r; l>>=1,r>>=1){\n if(r&1) seg[--r].emplace_back(i.from,i.to);\n if(l&1) seg[l++].emplace_back(i.from,i.to);\n }\n }\n dfs(func,1);\n }\n};\n\nvoid AOJ2235(){\n int n,q; cin >> n >> q;\n OfflineDynamicConnectivity<int> dc(n,q);\n vector<int> u(q),v(q);\n for(int t=0; t<q; t++){\n int op;\n cin >> op >> u[t] >> v[t];\n if(op==1){\n dc.link(op,u[t],v[t]);\n }else if(op==2){\n dc.cut(op,u[t],v[t]);\n }else{\n dc.query(op);\n }\n }\n dc.run([&](int t, UndoableUnionFind &uf){\n if(t<q && dc.query_types[t]==3){\n cout << (uf.same(u[t],v[t]) ? \"YES\" : \"NO\") << endl;\n }\n });\n}\n\nint main() {\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(15);\n AOJ2235();\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 13760, "score_of_the_acc": -0.1048, "final_rank": 2 }, { "submission_id": "aoj_2235_9113674", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing vvl = vector<vector<ll>>;\nusing vl = vector<ll>;\n#define rep(i, s, f) for(long long i = s; i <= f; i++)\n#define ENDL '\\n'\n\n\nll op(ll l, ll r) {\n\treturn l + r;\n}\n\n\nstruct dynamic_connectivity {\n struct euler_tour_tree {\n \tstruct node_t;\n \tusing np = node_t*;\n \tstruct node_t {\n \t\tnode_t *lch, *rch, *par;\n\t\t\tint s, t;\n \t\tint sz;\n\t\t\tbool exact;//このノード(辺) のレベル == ettのレベル && s < t edge_levelup\n\t\t\tbool child_exact;\n\t\t\tbool have_auxedge = false;\n\t\t\tbool child_have_auxedge = false;\n \n \t\t//node_t() : lch(nullptr), rch(nullptr), par(nullptr), sz(0) {}\n\t\t\tnode_t(int _s, int _t) : s(_s), t(_t), lch(nullptr), rch(nullptr), par(nullptr), sz(s==t), exact(s<t), child_exact(s<t) {\n\n\t\t\t}\n \t bool is_root() {\n \t \treturn !par;\n \t }\n \n \t\tvoid take_copy(np x) {//xの集約値、作用素を自身にコピー. xはnullptrでは無い。\n\t\t\t\tsz = x -> sz;\n\t\t\t\tchild_have_auxedge = x -> child_have_auxedge;\n\t\t\t\tchild_exact = x -> child_exact;\n \t\t}\n \t};\n \n \t\n \n \n \tint sz;\n \tvector<unordered_map<int, np>> ptr;\n\n \tvoid push(np t) {}\n \n \tvoid update(np t) {\n \t t -> sz = (t->s == t->t);\n\t\t\tif(t -> lch) t -> sz += t -> lch -> sz;\n\t\t\tif(t -> rch) t -> sz += t -> rch -> sz;\n\t\t\tt -> child_exact = (t -> lch ? t -> lch -> child_exact : 0) | (t -> exact) | (t -> rch ? t -> rch -> child_exact : 0);\n\t\t\tt -> child_have_auxedge = (t -> lch ? t -> lch -> child_have_auxedge : 0) | (t -> have_auxedge) | (t -> rch ? t -> rch -> child_have_auxedge : 0);\n \t}\n \n\t\tvoid update_only_child_have_auxedge(np t) {\n\t\t\tt -> child_have_auxedge = (t -> lch ? t -> lch -> child_have_auxedge : 0) | (t -> have_auxedge) | (t -> rch ? t -> rch -> child_have_auxedge : 0);\n\t\t}\n\t\tvoid update_only_child_exact(np t) {\n\t\t\tt -> child_exact = (t -> lch ? t -> lch -> child_exact : 0) | (t -> exact) | (t -> rch ? t -> rch -> child_exact : 0);\n\t\t}\n \n \tvoid rotR(np t) {\n \t\tnp x = t -> par, y = x -> par;\n \t\tpush(t);\n \t\tif((x -> lch = t -> rch)) t -> rch -> par = x;\n \t\tt -> rch = x, x -> par = t;\n \t\tif((t -> par = y)) {\n \t\t\tif(y -> lch == x) y -> lch = t;\n \t\t\telse y -> rch = t;\n \t\t}\n \t\tt -> take_copy(x);\n \t\tpush(x -> lch); push(x -> rch);\n \t\tupdate(x); \n \t}\n \n \tvoid rotL(np t) {\n \t\tnp x = t -> par, y = x -> par;\n \t\tpush(t);\n \t\tif((x -> rch = t -> lch)) t -> lch -> par = x;\n \t\tt -> lch = x, x -> par = t;\n \t\tif((t -> par = y)) {\n \t\t\tif(y -> lch == x) y -> lch = t;\n \t\t\telse y -> rch = t;\n \t\t}\n \t t -> take_copy(x);\n \t\tpush(x -> lch); push(x -> rch);\n \t\tupdate(x); \n \t}\n \n \tvoid splay(np t) {\n \t\twhile(!t -> is_root()) {\n \t\t\tnp x = t -> par;\n \t\t\tif(x -> is_root()) {\n \t\t\t\tif(x -> lch == t) rotR(t);\n \t\t\t\telse rotL(t);\n \t\t\t} else {\n \t\t\t\tnp y = x -> par;\n \t\t\t\tif(y -> lch == x) {\n \t\t\t\t\tif(x -> lch == t) rotR(x), rotR(t);\n \t\t\t\t\telse rotL(t), rotR(t);\n \t\t\t\t}\n \t\t\t\telse {\n \t\t\t\t\tif(x -> rch == t) rotL(x), rotL(t);\n \t\t\t\t\telse rotR(t), rotL(t);\n \t\t\t\t}\n \t\t\t}\n \t\t}\n \t\tpush(t);//tのlazyを恒等作用にしておく\n \t}\n \n \tnp merge(np l, np r) {\n \t\tif(!l) return r;\n \t\tif(!r) return l;\n \t\twhile(l -> rch) l = l -> rch;\n \t\tsplay(l);\n \t\tif((l -> rch = r)) {\n \t\t\tpush(r);\n \t\t\tr -> par = l;\n \t\t}\n \t\tupdate(l);\n \t\treturn l;\n \t}\n \n \n \tpair<np, np> split(np t) {// t) [t, \n \t splay(t);\n \t\tnp l = t -> lch;\n \t\tif(l) l -> par = nullptr;\n \t\tt -> lch = nullptr;\n \t\tupdate(t);\n \t\treturn make_pair(l, t);\n \t}\n\n \tpair<np, np> split2(np t) { //move t to top and detach t\n \t\tsplay(t);\n \t\tnp l = t -> lch;\n \t\tnp r = t -> rch;\n \t\tif(l) l -> par = nullptr;\n \t\tt -> lch = nullptr;\n \t\tif(r) r -> par = nullptr;\n \t\tt -> rch = nullptr;\n \t\tupdate(t);\n \t\treturn make_pair(l, r);\n \t}\n\n \ttuple<np, np, np> split(np u, np t) {// u) (u t) (t / t) (t u) (u\n \t auto v = split2(u);\n \t\tif(same(v.first, t)) {\n \t\t\tauto r = split2(t);\n \t\t\treturn make_tuple(r.first, r.second, v.second);\n \t\t} else {\n \t\t\tauto r = split2(t);\n \t\t\treturn make_tuple(v.first, r.first, r.second);\n \t\t}\n \t}\n \n \tnp get_node(int s, int t) {\n \t\tif(ptr[s].find(t) == ptr[s].end())ptr[s][t] = new node_t(s, t);\n \t\treturn ptr[s][t];\n \t}\n \n \tnp root(np t) {\n \t\tif(!t) return t;\n \t\twhile(t -> par) t = t -> par;\n \t\treturn t;\n \t}\n\n \tnp reroot(np t) {\n \t\tauto[l, r] = split(t);\n \t\treturn merge(r, l);\n \t}\n\t\tint size(np u) { // NEW!\n\t\t\tsplay(u);\n\t\t\treturn u -> sz;\n\t\t}\n \n \tbool same(np u, np v) {\n \t\tif(u) splay(u);\n \t\tif(v) splay(v);\n \t\treturn root(u) == root(v);\n \t}\n\t\teuler_tour_tree(){}\n \t\n \teuler_tour_tree(int _sz) : sz(_sz) {\n \t ptr.resize(sz);\n \t}\n\n \tbool same(int u, int v) {\n \t\treturn same(get_node(u, u), get_node(v, v));\n \t}\n\n \tbool link(int u, int v) {\n \t\tif(same(u, v)) return false;\n \t\tmerge(merge(merge(reroot(get_node(u, u)), get_node(u, v)), reroot(get_node(v, v))), get_node(v, u));\n \t\treturn true;\n \t}\n\n \tbool cut(int u, int v) {\n \t\tif(ptr[u].find(v) == ptr[u].end()) return false;\n \t\tauto [i, j, k] = split(get_node(u, v), get_node(v, u));\n \t\tmerge(i, k);\n \t\tnp p = ptr[u][v];\n \t\tnp q = ptr[v][u];\n \t\tptr[u].erase(v);\n \t\tptr[v].erase(u);\n \t\tdelete p; delete q;\n \t\treturn true;\n \t}\n\t\tint size(int u) {\n\t\t\treturn size(get_node(u, u));\n\t\t}\n\t\tvoid have_auxedge_update(int t, bool b) {\n\t\t\tnp v = get_node(t, t);\n\t\t\tsplay(v);\n\t\t\tv -> have_auxedge = b;\n\t\t\tupdate_only_child_have_auxedge(v);\n\t\t}\n\n\t\tvector<int> vertex_list(int t) {\n\t\t\tnp v = get_node(t, t);\n\t\t\tsplay(v);\n\t\t\tvector<int> res;\n\n\t\t\tauto dfs = [&](auto f, np now) -> void {\n\t\t\t\tif(now -> lch) f(f, now -> lch);\n\t\t\t\tif(now -> s == now -> t) res.push_back(now -> s);\n\t\t\t\tif(now -> rch) f(f, now -> rch);\n\t\t\t\treturn;\n\t\t\t};\n\n\t\t\tdfs(dfs, v);\n\t\t\treturn res;\n\t\t}\n\n };\n int cc;\n int sz;\n\tint dep;\n vector<euler_tour_tree> ett;\n\tvector<vector<unordered_set<int>>> aux_edges;\n using np = typename euler_tour_tree::node_t*;\n\n\tprivate:\n\t \n\n\t void edge_level_increment(int t, int k) {\n\t\t\tnp v = ett[k].get_node(t, t);\n\t\t\tett[k].splay(v);\n\t\t\tauto dfs = [&](auto f, np now) -> void {\n\t\t\t\tif(now->exact) {\n\t\t\t\t\tett[k].splay(now);\n\t\t\t\t\tnow -> exact = 0;\n\t\t\t\t\tett[k].update_only_child_exact(now);\n\t\t\t\t\tett[k+1].link(now -> s, now -> t);\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tif(now -> lch && now -> lch -> child_exact) f(f, now -> lch);\n\t\t\t\telse f(f, now -> rch);\n\t\t\t\treturn;\n\t\t\t};\n\n\t\t\twhile(v && v -> child_exact) {\n\t\t\t\tdfs(dfs, v);\n\t\t\t\tett[k].splay(v);\n\t\t\t}\n\t\t}\n\t \n\t\tbool try_reconnect(int u, int v, int k) {\n\t\t\tfor(int i=0; i<k;i++) {\n\t\t\t\tett[i].cut(u, v);\n\t\t\t}\n\n\t\t\tfor(int i=k; i >=0; i--) {\n\t\t\t\tif(ett[i].size(u) > ett[i].size(v)) swap(u, v);\n\t\t\t\tedge_level_increment(u, i);\n\n\t\t\t\tnp t = ett[i].get_node(u, u);\n\t\t\t\tett[i].splay(t);\n\t\t\t\tauto dfs = [&](auto f, np now) -> bool {\n\t\t\t\t\tif(now -> have_auxedge) {\n\t\t\t\t\t\tett[i].splay(now);\n\t\t\t\t\t\tfor(auto itr = aux_edges[i][now->s].begin(); itr != aux_edges[i][now->s].end(); ) {\n\t\t\t\t\t\t\tauto y = *itr;\n\t\t\t\t\t\t\titr = aux_edges[i][now->s].erase(itr);\n\t\t\t\t\t\t\taux_edges[i][y].erase(now->s);\n\t\t\t\t\t\t\tif(aux_edges[i][now->s].size()==0)ett[i].have_auxedge_update(now->s, 0);\n\t\t\t\t\t\t\tif(aux_edges[i][y].size()==0)ett[i].have_auxedge_update(y, 0);\n\t\t\t\t\t\t\tif(ett[i].same(now->s, y)) {//辺の先が自分の連結成分\n\t\t\t\t\t\t\t aux_edges[i+1][now->s].insert(y);\n\t\t\t\t\t\t\t\taux_edges[i+1][y].insert(now->s);\n\t\t\t\t\t\t\t\tif(aux_edges[i+1][now->s].size()==1)ett[i+1].have_auxedge_update(now->s, 1);\n\t\t\t\t\t\t\t\tif(aux_edges[i+1][y].size()==1)ett[i+1].have_auxedge_update(y, 1);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\telse {\n\t\t\t\t\t\t\t\tfor(int j = 0; j <= i; j++) {\n\t\t\t\t\t\t\t\t\tett[j].link(now->s, y);\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\treturn 1;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t\treturn 0;\n\t\t\t\t\t}\n\n\t\t\t\t\tif(now -> lch && now -> lch -> child_have_auxedge) return f(f, now -> lch);\n\t\t\t\t\telse return f(f, now -> rch);\n\t\t\t\t};\n\n\t\t\t\twhile(t -> child_have_auxedge) {\n\t\t\t\t\tif(dfs(dfs, t)) return 1;\n\t\t\t\t\tett[i].splay(t);\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn 0;\n\t\t}\n\n\tpublic:\n\n\t \n\n\n\t dynamic_connectivity(int _sz) : sz(_sz) {\n\t\t\tcc = sz;\n\t\t\tdep = 1;\n\t\t\tett.emplace_back(sz);\n\t\t\taux_edges.emplace_back(sz);\n\t\t}\n\n\n\t\tbool same(int u, int v) {\n\t\t\treturn ett[0].same(u, v);\n\t\t}\n\n\t\tbool link(int u, int v) {//連結成分が減ったか\n\t\t\tif(u==v)return false;\n\t\t\tif(ett[0].link(u, v)) {\n\t\t\t\tcc--;\n\t\t\t\treturn true;\n\t\t\t} \n\t\t\taux_edges[0][u].insert(v);\n\t\t\taux_edges[0][v].insert(u);\n\t\t\tif(aux_edges[0][u].size()==1)ett[0].have_auxedge_update(u, 1);\n\t\t\tif(aux_edges[0][v].size()==1)ett[0].have_auxedge_update(v, 1);\n\t\t\treturn false;\n\t\t}\n\n\t\tbool cut(int u, int v) {//切った結果、分断されたか?\n\t\t\tfor(int i=0; i<dep; i++) {\n\t\t\t\taux_edges[i][u].erase(v);\n\t\t\t\taux_edges[i][v].erase(u);\n\t\t\t\tif(aux_edges[i][u].size()==0)ett[i].have_auxedge_update(u, 0);\n\t\t\t\tif(aux_edges[i][v].size()==0)ett[i].have_auxedge_update(v, 0);\n\t\t\t}\n\n\t\t\tfor(int i=dep-1; i >=0; i--) {\n\t\t\t\tif(ett[i].cut(u, v)) {\n\t\t\t\t\tif(dep-1==i) {\n\t\t\t\t\t\tdep++;\n\t\t\t\t\t\tett.emplace_back(sz);\n\t\t\t\t\t\taux_edges.emplace_back(sz);\n\t\t\t\t\t}\n\t\t\t\t\tbool b = try_reconnect(u, v, i);\n\t\t\t\t\tif(!b)cc++;\n\t\t\t\t\treturn b;\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn false;\n\t\t}\n\n\t\tint component_count() {\n\t\t\treturn cc;\n\n\t\t}\n\n\t\tint size(int u) {\n\t\t\tnp v = ett[0].get_node(u, u);\n\t\t\tett[0].splay(v);\n\t\t\treturn v -> sz;\n\t\t}\n\n\t\tvector<int> vertex_list(int u) {\n\t\t\treturn ett[0].vertex_list(u);\n\t\t}\n\n\t\t/*\n\t\t dynamic_connectivity(sz) O(sz) 空間O(szlogsz) (重い!)\n\t\t\t@brief : dynamic_connectivity 値集約なし\n\t\t\tlink(u, v) ... uv間に辺を張る。すでにあったら何もしない。amortized O(log^2N)\n\t\t\tcut(u, v) ... uv間の辺を削除する。すでにあったら何もしない。amortized O(log^2N)\n\t\t\tsame(u, v) ... uvが同じ連結成分にあるか。 O(logN)\n\t\t\tsize(u) ... uが属する連結成分のサイズ 未verify\n\t\t\tcomponent_count()...連結成分の個数 O(1)\n\t\t\tvertex_list(u) ... uが属する連結成分の頂点 O(logN + |SIZE|) 未verify\n\t\t*/\n\n};\n\n \n \nint main() {\n\tios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n cout << fixed << setprecision(15);\n\n\tint n, k;\n\tcin >> n >> k;\n\tdynamic_connectivity tr(n);\n\twhile(k--) {\n\t\tint t;\n\t\tcin >> t;\n\t\tint u, v;\n\t\tcin >> u >> v;\n\t\tif(t==1) {\n\t\t\ttr.link(u, v);\n\t\t}else if(t==2) {\n\t\t\ttr.cut(u, v);\n\t\t}\n\t\telse {\n\t\t\tif(tr.same(u,v)) cout << \"YES\" << '\\n';\n\t\t\telse cout << \"NO\" << '\\n';\n\t\t}\n\t}\n\n\n\n\n}", "accuracy": 1, "time_ms": 220, "memory_kb": 77344, "score_of_the_acc": -1.3407, "final_rank": 10 }, { "submission_id": "aoj_2235_9113581", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing vvl = vector<vector<ll>>;\nusing vl = vector<ll>;\n#define rep(i, s, f) for(long long i = s; i <= f; i++)\n#define ENDL '\\n'\n\n\nll op(ll l, ll r) {\n\treturn l + r;\n}\n\n\nstruct dynamic_connectivity {\n struct euler_tour_tree {\n \tstruct node_t;\n \tusing np = node_t*;\n \tstruct node_t {\n \t\tnode_t *lch, *rch, *par;\n\t\t\tint s, t;\n \t\tint sz;\n\t\t\tbool exact;//このノード(辺) のレベル == ettのレベル && s < t edge_levelup\n\t\t\tbool child_exact;\n\t\t\tbool have_auxedge = false;\n\t\t\tbool child_have_auxedge = false;\n \n \t\t//node_t() : lch(nullptr), rch(nullptr), par(nullptr), sz(0) {}\n\t\t\tnode_t(int _s, int _t) : s(_s), t(_t), lch(nullptr), rch(nullptr), par(nullptr), sz(s==t), exact(s<t), child_exact(s<t) {\n\n\t\t\t}\n \t bool is_root() {\n \t \treturn !par;\n \t }\n \n \t\tvoid take_copy(np x) {//xの集約値、作用素を自身にコピー. xはnullptrでは無い。\n\t\t\t\tsz = x -> sz;\n\t\t\t\tchild_have_auxedge = x -> child_have_auxedge;\n\t\t\t\tchild_exact = x -> child_exact;\n \t\t}\n \t};\n \n \t\n \n \n \tint sz;\n \tvector<unordered_map<int, np>> ptr;\n\n \tvoid push(np t) {}\n \n \tvoid update(np t) {\n \t t -> sz = (t->s == t->t);\n\n\t\t\tif(t -> lch) t -> sz += t -> lch -> sz;\n\t\t\tif(t -> rch) t -> sz += t -> rch -> sz;\n\t\t\tt -> child_exact = (t -> lch ? t -> lch -> child_exact : 0) | (t -> exact) | (t -> rch ? t -> rch -> child_exact : 0);\n\t\t\tt -> child_have_auxedge = (t -> lch ? t -> lch -> child_have_auxedge : 0) | (t -> have_auxedge) | (t -> rch ? t -> rch -> child_have_auxedge : 0);\n \t}\n \n\t\tvoid update_only_child_have_auxedge(np t) {\n\t\t\tt -> child_have_auxedge = (t -> lch ? t -> lch -> child_have_auxedge : 0) | (t -> have_auxedge) | (t -> rch ? t -> rch -> child_have_auxedge : 0);\n\t\t}\n\t\tvoid update_only_child_exact(np t) {\n\t\t\tt -> child_exact = (t -> lch ? t -> lch -> child_exact : 0) | (t -> exact) | (t -> rch ? t -> rch -> child_exact : 0);\n\t\t}\n \n \tvoid rotR(np t) {\n \t\tnp x = t -> par, y = x -> par;\n \t\t//push(t);\n \t\tif((x -> lch = t -> rch)) t -> rch -> par = x;\n \t\tt -> rch = x, x -> par = t;\n \t\tif((t -> par = y)) {\n \t\t\tif(y -> lch == x) y -> lch = t;\n \t\t\telse y -> rch = t;\n \t\t}\n \t\tt -> take_copy(x);\n \t\tpush(x -> lch); push(x -> rch);\n \t\tupdate(x); \n \t}\n \n \tvoid rotL(np t) {\n \t\tnp x = t -> par, y = x -> par;\n \t\tpush(t);\n \t\tif((x -> rch = t -> lch)) t -> lch -> par = x;\n \t\tt -> lch = x, x -> par = t;\n \t\tif((t -> par = y)) {\n \t\t\tif(y -> lch == x) y -> lch = t;\n \t\t\telse y -> rch = t;\n \t\t}\n \t t -> take_copy(x);\n \t\tpush(x -> lch); push(x -> rch);\n \t\tupdate(x); \n \t}\n \n \tvoid splay(np t) {\n \t\twhile(!t -> is_root()) {\n \t\t\tnp x = t -> par;\n \t\t\tif(x -> is_root()) {\n \t\t\t\tif(x -> lch == t) rotR(t);\n \t\t\t\telse rotL(t);\n \t\t\t} else {\n \t\t\t\tnp y = x -> par;\n \t\t\t\tif(y -> lch == x) {\n \t\t\t\t\tif(x -> lch == t) rotR(x), rotR(t);\n \t\t\t\t\telse rotL(t), rotR(t);\n \t\t\t\t}\n \t\t\t\telse {\n \t\t\t\t\tif(x -> rch == t) rotL(x), rotL(t);\n \t\t\t\t\telse rotR(t), rotL(t);\n \t\t\t\t}\n \t\t\t}\n \t\t}\n \t\tpush(t);//tのlazyを恒等作用にしておく\n \t}\n \n \tnp merge(np l, np r) {\n \t\tif(!l) return r;\n \t\tif(!r) return l;\n \t\twhile(l -> rch) l = l -> rch;\n \t\tsplay(l);\n \t\tif((l -> rch = r)) {\n \t\t\tpush(r);\n \t\t\tr -> par = l;\n \t\t}\n \t\tupdate(l);\n \t\treturn l;\n \t}\n \n \n \tpair<np, np> split(np t) {// t) [t, \n \t splay(t);\n \t\tnp l = t -> lch;\n \t\tif(l) l -> par = nullptr;\n \t\tt -> lch = nullptr;\n \t\tupdate(t);\n \t\treturn make_pair(l, t);\n \t}\n\n \tpair<np, np> split2(np t) { //move t to top and detach t\n \t\tsplay(t);\n \t\tnp l = t -> lch;\n \t\tnp r = t -> rch;\n \t\tif(l) l -> par = nullptr;\n \t\tt -> lch = nullptr;\n \t\tif(r) r -> par = nullptr;\n \t\tt -> rch = nullptr;\n \t\tupdate(t);\n \t\treturn make_pair(l, r);\n \t}\n\n \ttuple<np, np, np> split(np u, np t) {// u) (u t) (t / t) (t u) (u\n \t auto v = split2(u);\n \t\tif(same(v.first, t)) {\n \t\t\tauto r = split2(t);\n \t\t\treturn make_tuple(r.first, r.second, v.second);\n \t\t} else {\n \t\t\tauto r = split2(t);\n \t\t\treturn make_tuple(v.first, r.first, r.second);\n \t\t}\n \t}\n \n \tnp get_node(int s, int t) {\n \t\tif(ptr[s].find(t) == ptr[s].end())ptr[s][t] = new node_t(s, t);\n \t\treturn ptr[s][t];\n \t}\n \n \tnp root(np t) {\n \t\tif(!t) return t;\n \t\twhile(t -> par) t = t -> par;\n \t\treturn t;\n \t}\n\n \tnp reroot(np t) {\n \t\tauto[l, r] = split(t);\n \t\treturn merge(r, l);\n \t}\n\t\tint size(np u) { // NEW!\n\t\t\tsplay(u);\n\t\t\treturn u -> sz;\n\t\t}\n \n \tbool same(np u, np v) {\n \t\tif(u) splay(u);\n \t\tif(v) splay(v);\n \t\treturn root(u) == root(v);\n \t}\n\t\teuler_tour_tree(){}\n \t\n \teuler_tour_tree(int _sz) : sz(_sz) {\n \t ptr.resize(sz);\n \t}\n\n \tbool same(int u, int v) {\n \t\treturn same(get_node(u, u), get_node(v, v));\n \t}\n\n \tbool link(int u, int v) {\n \t\tif(same(u, v)) return false;\n \t\tmerge(merge(merge(reroot(get_node(u, u)), get_node(u, v)), reroot(get_node(v, v))), get_node(v, u));\n \t\treturn true;\n \t}\n\n \tbool cut(int u, int v) {\n \t\tif(ptr[u].find(v) == ptr[u].end()) return false;\n \t\tauto [i, j, k] = split(get_node(u, v), get_node(v, u));\n \t\tmerge(i, k);\n \t\tnp p = ptr[u][v];\n \t\tnp q = ptr[v][u];\n \t\tptr[u].erase(v);\n \t\tptr[v].erase(u);\n \t\tdelete p; delete q;\n \t\treturn true;\n \t}\n\t\tint size(int u) {\n\t\t\treturn size(get_node(u, u));\n\t\t}\n\t\tvoid have_auxedge_update(int t, bool b) {\n\t\t\tnp v = get_node(t, t);\n\t\t\tsplay(v);\n\t\t\tv -> have_auxedge = b;\n\t\t\tupdate_only_child_have_auxedge(v);\n\t\t}\n\n };\n\n int sz;\n\tint dep;\n vector<euler_tour_tree> ett;\n\tvector<vector<unordered_set<int>>> aux_edges;\n using np = typename euler_tour_tree::node_t*;\n\n\tprivate:\n\t \n\n\t void edge_level_increment(int t, int k) {\n\t\t\tnp v = ett[k].get_node(t, t);\n\t\t\tett[k].splay(v);\n\t\t\tauto dfs = [&](auto f, np now) -> void {\n\t\t\t\tif(now->exact) {\n\t\t\t\t\tett[k].splay(now);\n\t\t\t\t\tnow -> exact = 0;\n\t\t\t\t\tett[k].update_only_child_exact(now);\n\t\t\t\t\tett[k+1].link(now -> s, now -> t);\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tif(now -> lch && now -> lch -> child_exact) f(f, now -> lch);\n\t\t\t\telse f(f, now -> rch);\n\t\t\t\treturn;\n\t\t\t};\n\n\t\t\twhile(v && v -> child_exact) {\n\t\t\t\tdfs(dfs, v);\n\t\t\t\tett[k].splay(v);\n\t\t\t}\n\t\t}\n\t \n\t\tbool try_reconnect(int u, int v, int k) {\n\t\t\tfor(int i=0; i<k;i++) {\n\t\t\t\tett[i].cut(u, v);\n\t\t\t}\n\n\t\t\tfor(int i=k; i >=0; i--) {\n\t\t\t\tif(ett[i].size(u) > ett[i].size(v)) swap(u, v);\n\t\t\t\tedge_level_increment(u, i);\n\n\t\t\t\tnp t = ett[i].get_node(u, u);\n\t\t\t\tett[i].splay(t);\n\t\t\t\tauto dfs = [&](auto f, np now) -> bool {\n\t\t\t\t\tif(now -> have_auxedge) {\n\t\t\t\t\t\tett[i].splay(now);\n\t\t\t\t\t\tfor(auto itr = aux_edges[i][now->s].begin(); itr != aux_edges[i][now->s].end(); ) {\n\t\t\t\t\t\t\tauto y = *itr;\n\t\t\t\t\t\t\titr = aux_edges[i][now->s].erase(itr);\n\t\t\t\t\t\t\taux_edges[i][y].erase(now->s);\n\t\t\t\t\t\t\tif(aux_edges[i][now->s].size()==0)ett[i].have_auxedge_update(now->s, 0);\n\t\t\t\t\t\t\tif(aux_edges[i][y].size()==0)ett[i].have_auxedge_update(y, 0);\n\t\t\t\t\t\t\tif(ett[i].same(now->s, y)) {//辺の先が自分の連結成分\n\t\t\t\t\t\t\t aux_edges[i+1][now->s].insert(y);\n\t\t\t\t\t\t\t\taux_edges[i+1][y].insert(now->s);\n\t\t\t\t\t\t\t\tif(aux_edges[i+1][now->s].size()==1)ett[i+1].have_auxedge_update(now->s, 1);\n\t\t\t\t\t\t\t\tif(aux_edges[i+1][y].size()==1)ett[i+1].have_auxedge_update(y, 1);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\telse {\n\t\t\t\t\t\t\t\tfor(int j = 0; j <= i; j++) {\n\t\t\t\t\t\t\t\t\tett[j].link(now->s, y);\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\treturn 1;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t\treturn 0;\n\t\t\t\t\t}\n\n\t\t\t\t\tif(now -> lch && now -> lch -> child_have_auxedge) return f(f, now -> lch);\n\t\t\t\t\telse return f(f, now -> rch);\n\t\t\t\t};\n\n\t\t\t\twhile(t -> child_have_auxedge) {\n\t\t\t\t\tif(dfs(dfs, t)) return 1;\n\t\t\t\t\tett[i].splay(t);\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn 0;\n\t\t}\n\n\tpublic:\n\n\n\t dynamic_connectivity(int _sz) : sz(_sz) {\n\t\t\tdep = 1;\n\t\t\tett.emplace_back(sz);\n\t\t\taux_edges.emplace_back(sz);\n\t\t}\n\n\n\t\tbool same(int u, int v) {\n\t\t\treturn ett[0].same(u, v);\n\t\t}\n\n\t\tbool link(int u, int v) {\n\t\t\tif(u==v)return false;\n\t\t\tif(ett[0].link(u, v)) return true;\n\t\t\taux_edges[0][u].insert(v);\n\t\t\taux_edges[0][v].insert(u);\n\t\t\tif(aux_edges[0][u].size()==1)ett[0].have_auxedge_update(u, 1);\n\t\t\tif(aux_edges[0][v].size()==1)ett[0].have_auxedge_update(v, 1);\n\t\t\treturn false;\n\t\t}\n\n\t\tbool cut(int u, int v) {//切った結果、分断されたか?\n\t\t\tfor(int i=0; i<dep; i++) {\n\t\t\t\taux_edges[i][u].erase(v);\n\t\t\t\taux_edges[i][v].erase(u);\n\t\t\t\tif(aux_edges[i][u].size()==0)ett[i].have_auxedge_update(u, 0);\n\t\t\t\tif(aux_edges[i][v].size()==0)ett[i].have_auxedge_update(v, 0);\n\t\t\t}\n\n\t\t\tfor(int i=dep-1; i >=0; i--) {\n\t\t\t\tif(ett[i].cut(u, v)) {\n\t\t\t\t\tif(dep-1==i) {\n\t\t\t\t\t\tdep++;\n\t\t\t\t\t\tett.emplace_back(sz);\n\t\t\t\t\t\taux_edges.emplace_back(sz);\n\t\t\t\t\t}\n\t\t\t\t\treturn !try_reconnect(u, v, i);\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn false;\n\t\t}\n\n\t\tint size(int u) {\n\t\t\tnp v = ett[0].get_node(u, u);\n\t\t\tett[0].splay(v);\n\t\t\treturn v -> sz;\n\t\t}\n\n\n\t\n};\n \nint main() {\n\tios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n cout << fixed << setprecision(15);\n\n\tint n, k;\n\tcin >> n >> k;\n\tdynamic_connectivity tr(n);\n\twhile(k--) {\n\t\tint t;\n\t\tcin >> t;\n\t\tint u, v;\n\t\tcin >> u >> v;\n\t\tif(t==1) {\n\t\t\ttr.link(u, v);\n\t\t}else if(t==2) {\n\t\t\ttr.cut(u, v);\n\t\t}\n\t\telse {\n\t\t\tif(tr.same(u,v)) cout << \"YES\" << '\\n';\n\t\t\telse cout << \"NO\" << '\\n';\n\t\t}\n\t}\n\n\n\n\n}", "accuracy": 1, "time_ms": 220, "memory_kb": 77580, "score_of_the_acc": -1.343, "final_rank": 11 }, { "submission_id": "aoj_2235_9113337", "code_snippet": "//#define _GLIBCXX_DEBUG\n#include<bits/stdc++.h>\nusing namespace std;\n\ntemplate<typename X>\nstruct dynamic_connectivity {\n struct euler_tour_tree {\n \tstruct node_t;\n \tusing np = node_t*;\n \tstruct node_t {\n \t\tnode_t *lch, *rch, *par;\n\t\t\tint s, t;\n \t\t//X key;\n \t\t//X sum;\n \t\tint sz;\n\t\t\tbool exact;//このノード(辺) のレベル == ettのレベル && s < t edge_levelup\n\t\t\tbool child_exact;\n\t\t\tbool have_auxedge = false;\n\t\t\tbool child_have_auxedge = false;\n \n \t\t//node_t() : lch(nullptr), rch(nullptr), par(nullptr), sz(0) {}\n\t\t\tnode_t(int _s, int _t) : s(_s), t(_t), lch(nullptr), rch(nullptr), par(nullptr), sz(s==t), exact(s<t), child_exact(s<t) {}\n \t bool is_root() {\n \t \treturn !par;\n \t }\n \n \t\tvoid take_copy(np x) {//xの集約値、作用素を自身にコピー. xはnullptrでは無い。\n\t\t\t\t//sum = x -> sum;\n\t\t\t\tsz = x -> sz;\n\t\t\t\tchild_have_auxedge = x -> child_have_auxedge;\n\t\t\t\tchild_exact = x -> child_exact;\n \t\t}\n \t};\n \n \t\n \n \n \tint sz;\n \tvector<unordered_map<int, np>> ptr;\n\n \tvoid push(np t) {}\n \n \tvoid update(np t) {\n \t t -> sz = 1;\n \t\t//t -> sum = t -> key;\n \t\t//if(t -> lch) t -> sz += t -> lch -> sz, t -> sum = X::op(t -> lch -> sum, t -> sum);\n \t\t//if(t -> rch) t -> sz += t -> rch -> sz, t -> sum = X::op(t -> sum, t -> rch -> sum);\n\t\t\tif(t -> lch) t -> sz += t -> lch -> sz;\n\t\t\tif(t -> rch) t -> sz += t -> rch -> sz;\n\t\t\tt -> child_exact = (t -> lch ? t -> lch -> child_exact : 0) | (t -> exact) | (t -> rch ? t -> rch -> child_exact : 0);\n\t\t\tt -> child_have_auxedge = (t -> lch ? t -> lch -> child_have_auxedge : 0) | (t -> have_auxedge) | (t -> rch ? t -> rch -> child_have_auxedge : 0);\n \t}\n \n\t\tvoid update_only_child_have_auxedge(np t) {\n\t\t\t//update(t);\n\t\t\tt -> child_have_auxedge = (t -> lch ? t -> lch -> child_have_auxedge : 0) | (t -> have_auxedge) | (t -> rch ? t -> rch -> child_have_auxedge : 0);\n\t\t}\n\t\tvoid update_only_child_exact(np t) {\n\t\t\t//update(t);\n\t\t\tt -> child_exact = (t -> lch ? t -> lch -> child_exact : 0) | (t -> exact) | (t -> rch ? t -> rch -> child_exact : 0);\n\t\t}\n \n \tvoid rotR(np t) {\n \t\tnp x = t -> par, y = x -> par;\n \t\t//push(t);\n \t\tif((x -> lch = t -> rch)) t -> rch -> par = x;\n \t\tt -> rch = x, x -> par = t;\n \t\tif((t -> par = y)) {\n \t\t\tif(y -> lch == x) y -> lch = t;\n \t\t\telse y -> rch = t;\n \t\t}\n \t\tt -> take_copy(x);\n \t\t//push(x -> lch); push(x -> rch);\n \t\tupdate(x); \n \t}\n \n \tvoid rotL(np t) {\n \t\tnp x = t -> par, y = x -> par;\n \t\tpush(t);\n \t\tif((x -> rch = t -> lch)) t -> lch -> par = x;\n \t\tt -> lch = x, x -> par = t;\n \t\tif((t -> par = y)) {\n \t\t\tif(y -> lch == x) y -> lch = t;\n \t\t\telse y -> rch = t;\n \t\t}\n \t t -> take_copy(x);\n \t\tpush(x -> lch); push(x -> rch);\n \t\tupdate(x); \n \t}\n \n \tvoid splay(np t) {\n \t\twhile(!t -> is_root()) {\n \t\t\tnp x = t -> par;\n \t\t\tif(x -> is_root()) {\n \t\t\t\tif(x -> lch == t) rotR(t);\n \t\t\t\telse rotL(t);\n \t\t\t} else {\n \t\t\t\tnp y = x -> par;\n \t\t\t\tif(y -> lch == x) {\n \t\t\t\t\tif(x -> lch == t) rotR(x), rotR(t);\n \t\t\t\t\telse rotL(t), rotR(t);\n \t\t\t\t}\n \t\t\t\telse {\n \t\t\t\t\tif(x -> rch == t) rotL(x), rotL(t);\n \t\t\t\t\telse rotR(t), rotL(t);\n \t\t\t\t}\n \t\t\t}\n \t\t}\n \t\t//push(t);//tのlazyを恒等作用にしておく\n \t}\n \n \tnp merge(np l, np r) {\n \t\tif(!l) return r;\n \t\tif(!r) return l;\n \t\twhile(l -> rch) l = l -> rch;\n \t\tsplay(l);\n \t\tif((l -> rch = r)) {\n \t\t\t//push(r);\n \t\t\tr -> par = l;\n \t\t}\n \t\tupdate(l);\n \t\treturn l;\n \t}\n \n \n \tpair<np, np> split(np t) {// t) [t, \n \t splay(t);\n \t\tnp l = t -> lch;\n \t\tif(l) l -> par = nullptr;\n \t\tt -> lch = nullptr;\n \t\tupdate(t);\n \t\treturn make_pair(l, t);\n \t}\n\n \tpair<np, np> split2(np t) { //move t to top and detach t\n \t\tsplay(t);\n \t\tnp l = t -> lch;\n \t\tnp r = t -> rch;\n \t\tif(l) l -> par = nullptr;\n \t\tt -> lch = nullptr;\n \t\tif(r) r -> par = nullptr;\n \t\tt -> rch = nullptr;\n \t\tupdate(t);\n \t\treturn make_pair(l, r);\n \t}\n\n \ttuple<np, np, np> split(np u, np t) {// u) (u t) (t / t) (t u) (u\n \t auto v = split2(u);\n \t\tif(same(v.first, t)) {\n \t\t\tauto r = split2(t);\n \t\t\treturn make_tuple(r.first, r.second, v.second);\n \t\t} else {\n \t\t\tauto r = split2(t);\n \t\t\treturn make_tuple(v.first, r.first, r.second);\n \t\t}\n \t}\n \n \tnp get_node(int s, int t) {\n \t\tif(ptr[s].find(t) == ptr[s].end())ptr[s][t] = new node_t(s, t);\n \t\treturn ptr[s][t];\n \t}\n \n \tnp root(np t) {\n \t\tif(!t) return t;\n \t\twhile(t -> par) t = t -> par;\n \t\treturn t;\n \t}\n\n \tnp reroot(np t) {\n \t\tauto[l, r] = split(t);\n \t\treturn merge(r, l);\n \t}\n\t\tint size(np u) { // NEW!\n\t\t\tsplay(u);\n\t\t\treturn u -> sz;\n\t\t}\n \n \tbool same(np u, np v) {\n \t\tif(u) splay(u);\n \t\tif(v) splay(v);\n \t\treturn root(u) == root(v);\n \t}\n\t\teuler_tour_tree(){}\n \t\n \teuler_tour_tree(int _sz) : sz(_sz) {\n \t ptr.resize(sz);\n \t}\n\n \tbool same(int u, int v) {\n \t\treturn same(get_node(u, u), get_node(v, v));\n \t}\n\n \tbool link(int u, int v) {\n \t\tif(same(u, v)) return false;\n \t\tmerge(merge(merge(reroot(get_node(u, u)), get_node(u, v)), reroot(get_node(v, v))), get_node(v, u));\n \t\treturn true;\n \t}\n\n \tbool cut(int u, int v) {\n \t\tif(ptr[u].find(v) == ptr[u].end()) return false;\n \t\tauto [i, j, k] = split(get_node(u, v), get_node(v, u));\n \t\tmerge(i, k);\n \t\tnp p = ptr[u][v];\n \t\tnp q = ptr[v][u];\n \t\tptr[u].erase(v);\n \t\tptr[v].erase(u);\n \t\tdelete p; delete q;\n \t\treturn true;\n \t}\n\t\tint size(int u) {\n\t\t\treturn size(get_node(u, u));\n\t\t}\n\t\tvoid have_auxedge_update(int t, bool b) {\n\t\t\tnp v = get_node(t, t);\n\t\t\tsplay(v);\n\t\t\tv -> have_auxedge = b;\n\t\t\tupdate_only_child_have_auxedge(v);\n\t\t}\n\n };\n\n int sz;\n\tint dep;\n vector<euler_tour_tree> ett;\n\tvector<vector<unordered_set<int>>> aux_edges;\n using np = typename euler_tour_tree::node_t*;\n\n\tprivate:\n\t \n\n\t void edge_level_increment(int t, int k) {\n\t\t\tnp v = ett[k].get_node(t, t);\n\t\t\tett[k].splay(v);\n\t\t\tauto dfs = [&](auto f, np now) -> void {\n\t\t\t\tif(now->exact) {\n\t\t\t\t\tett[k].splay(now);\n\t\t\t\t\tnow -> exact = 0;\n\t\t\t\t\tett[k].update_only_child_exact(now);\n\t\t\t\t\tett[k+1].link(now -> s, now -> t);\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tif(now -> lch && now -> lch -> child_exact) f(f, now -> lch);\n\t\t\t\telse f(f, now -> rch);\n\t\t\t\treturn;\n\t\t\t};\n\n\t\t\twhile(v && v -> child_exact) {\n\t\t\t\tdfs(dfs, v);\n\t\t\t\tett[k].splay(v);\n\t\t\t}\n\t\t}\n\t \n\t\tbool try_reconnect(int u, int v, int k) {\n\t\t\tfor(int i=0; i<k;i++) {\n\t\t\t\tett[i].cut(u, v);\n\t\t\t}\n\n\t\t\tfor(int i=k; i >=0; i--) {\n\t\t\t\tif(ett[i].size(u) > ett[i].size(v)) swap(u, v);\n\t\t\t\tedge_level_increment(u, i);\n\n\t\t\t\tnp t = ett[i].get_node(u, u);\n\t\t\t\tett[i].splay(t);\n\t\t\t\tauto dfs = [&](auto f, np now) -> bool {\n\t\t\t\t\tif(now -> have_auxedge) {\n\t\t\t\t\t\tett[i].splay(now);\n\t\t\t\t\t\tfor(auto itr = aux_edges[i][now->s].begin(); itr != aux_edges[i][now->s].end(); ) {\n\t\t\t\t\t\t\tauto y = *itr;\n\t\t\t\t\t\t\titr = aux_edges[i][now->s].erase(itr);\n\t\t\t\t\t\t\taux_edges[i][y].erase(now->s);\n\t\t\t\t\t\t\tif(aux_edges[i][now->s].size()==0)ett[i].have_auxedge_update(now->s, 0);\n\t\t\t\t\t\t\tif(aux_edges[i][y].size()==0)ett[i].have_auxedge_update(y, 0);\n\t\t\t\t\t\t\tif(ett[i].same(now->s, y)) {//辺の先が自分の連結成分\n\t\t\t\t\t\t\t aux_edges[i+1][now->s].insert(y);\n\t\t\t\t\t\t\t\taux_edges[i+1][y].insert(now->s);\n\t\t\t\t\t\t\t\tif(aux_edges[i+1][now->s].size()==1)ett[i+1].have_auxedge_update(now->s, 1);\n\t\t\t\t\t\t\t\tif(aux_edges[i+1][y].size()==1)ett[i+1].have_auxedge_update(y, 1);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\telse {\n\t\t\t\t\t\t\t\tfor(int j = 0; j <= i; j++) {\n\t\t\t\t\t\t\t\t\tett[j].link(now->s, y);\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\treturn 1;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t\treturn 0;\n\t\t\t\t\t}\n\n\t\t\t\t\tif(now -> lch && now -> lch -> child_have_auxedge) return f(f, now -> lch);\n\t\t\t\t\telse return f(f, now -> rch);\n\t\t\t\t};\n\n\t\t\t\twhile(t -> child_have_auxedge) {\n\t\t\t\t\tif(dfs(dfs, t)) return 1;\n\t\t\t\t\tett[i].splay(t);\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn 0;\n\t\t}\n\n\tpublic:\n\t dynamic_connectivity(int _sz) : sz(_sz) {\n\t\t\tdep = 1;\n\t\t\tett.emplace_back(sz);\n\t\t\taux_edges.emplace_back(sz);\n\t\t}\n\n\n\t\tbool same(int u, int v) {\n\t\t\treturn ett[0].same(u, v);\n\t\t}\n\n\t\tbool link(int u, int v) {\n\t\t\tif(u==v)return false;\n\t\t\tif(ett[0].link(u, v)) return true;\n\t\t\taux_edges[0][u].insert(v);\n\t\t\taux_edges[0][v].insert(u);\n\t\t\tif(aux_edges[0][u].size()==1)ett[0].have_auxedge_update(u, 1);\n\t\t\tif(aux_edges[0][v].size()==1)ett[0].have_auxedge_update(v, 1);\n\t\t\treturn false;\n\t\t}\n\n\t\tbool cut(int u, int v) {//切った結果、分断されたか?\n\t\t\tfor(int i=0; i<dep; i++) {\n\t\t\t\taux_edges[i][u].erase(v);\n\t\t\t\taux_edges[i][v].erase(u);\n\t\t\t\tif(aux_edges[i][u].size()==0)ett[i].have_auxedge_update(u, 0);\n\t\t\t\tif(aux_edges[i][v].size()==0)ett[i].have_auxedge_update(v, 0);\n\t\t\t}\n\n\t\t\tfor(int i=dep-1; i >=0; i--) {\n\t\t\t\tif(ett[i].cut(u, v)) {\n\t\t\t\t\tif(dep-1==i) {\n\t\t\t\t\t\tdep++;\n\t\t\t\t\t\tett.emplace_back(sz);\n\t\t\t\t\t\taux_edges.emplace_back(sz);\n\t\t\t\t\t}\n\t\t\t\t\treturn !try_reconnect(u, v, i);\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn false;\n\t\t}\n\n\n\n\n\n};\n \nint main() {\n\tint N, K;\n\tcin >> N >> K;\n\tdynamic_connectivity<char> tr(N);\n\twhile(K--) {\n\t\tint type;\n\t\tcin >> type;\n\t\tint u, v;\n\t\tcin >> u >> v;\n\t\tif(type==1) {\n\t\t\ttr.link(u, v);\n\t\t}\n\t\telse if(type==2) {\n\t\t\ttr.cut(u, v);\n\t\t}\n\t\telse {\n\t\t\tif(tr.same(u,v)) {\n\t\t\t\tcout << \"YES\" << endl;\n\t\t\t}\n\t\t\telse {\n\t\t\t\tcout << \"NO\" << endl;\n\t\t\t}\n\t\t}\n\t}\n\n\n\n}", "accuracy": 1, "time_ms": 230, "memory_kb": 77512, "score_of_the_acc": -1.3736, "final_rank": 14 } ]
aoj_2240_cpp
Problem I: Lapin Noir ここは, 地面が正六角形を敷き詰めたマス目になっている街である. 各マスは, 下の図のように2 つの整数で表される. ねこは(0, 0) のマスへ行こうとしている. いたずら好きの黒うさぎはこのことを知って, ねこの邪魔をしようと考えた. 黒うさぎはねこがいるマスへ跳び, その周りの6 マスのうち1 つまたは隣り合う2 つをブロックすることができる. ねこは周りの6 マスのうち, ブロックされていないマスを1 つ選んで進む. ねこが動くたび黒うさぎはねこを追いかけ, またブロックを行うことができる. 黒うさぎが動くと元のブロックは解除される. 下の図はブロックの様子を示している. この街にはねこの縄張りとなっているマス目がいくつかある. 縄張りは正六角形の周をなすマス目の集合 n 個の合併として表される. 黒うさぎはねこの縄張りに立ち入ることはできるが, ブロックすることはできない.つまり, ねこは縄張り内であれば自由に動ける. ねこの出発点の候補が k 個あり, マス( x i , y i ) (1 ≤ i ≤ k ) である. それぞれについて, ねこが必ず目的地(0, 0) に辿り着けるか, あるいは黒うさぎがうまく妨害することによってねこが目的地に辿り着けないかを判定せよ. Input 1 行目:1 ≤ n ≤ 40 000 2 ∼ ( n + 1) 行目: ねこの縄張りを表す正六角形の頂点 ( n + 2) 行目: 1 ≤ k ≤ 40 000 ( n + 3) ∼ ( n + k + 2) 行目: ねこの出発点 x i , y i −1 000 000 000 ≤ (各座標) ≤ 1 000 000 000 ねこの縄張りを表す正六角形は, 6 頂点の位置が反時計回りに与えられる. Output 出発地の候補それぞれについて, ねこが必ず目的地(0,0) に辿り着けるなら”YES”を, あるいは黒うさぎがうまく妨害した場合ねこが目的地に辿り着けないなら”NO”を, 一行ずつ出力せよ. Sample Input 1 2 1 -1 3 -1 3 1 1 3 -1 3 -1 1 3 0 4 0 4 1 3 2 2 2 2 1 3 1 1 -1 -1 2 4 Sample Output 1 YES NO YES
[ { "submission_id": "aoj_2240_4872355", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 1000000000000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n#define SIZE 40005\n\nenum Type{\n\tX,\n\tY,\n\tZ,\n};\n\nstruct Info_L{\n\tInfo_L(int arg_value,int arg_index){\n\t\tvalue = arg_value;\n\t\tindex = arg_index;\n\t}\n\tbool operator<(const struct Info_L &arg) const{\n\n\t\treturn value < arg.value; //★昇順★\n\t}\n\tint value,index;\n};\n\nstruct Info_R{\n\tInfo_R(int arg_value,int arg_index){\n\t\tvalue = arg_value;\n\t\tindex = arg_index;\n\t}\n\tbool operator<(const struct Info_R &arg) const{\n\n\t\treturn value > arg.value; //★降順★\n\t}\n\tint value,index;\n};\n\nstruct Data{\n\n\tvoid set(int arg_min_x,int arg_max_x,int arg_min_y,int arg_max_y,int arg_min_z,int arg_max_z){\n\t\tX_L = arg_min_x;\n\t\tX_R = arg_max_x;\n\t\tY_L = arg_min_y;\n\t\tY_R = arg_max_y;\n\t\tZ_L = arg_min_z;\n\t\tZ_R = arg_max_z;\n\t}\n\t/*void debug(){\n\n\t\tprintf(\"X_L:%d X_R:%d\\nY_L:%d Y_R:%d Z_L:%d Z_R:%d\\n\",X_L,X_R,Y_L,Y_R,Z_L,Z_R);\n\t}*/\n\n\tint X_L,X_R,Y_L,Y_R,Z_L,Z_R;\n};\n\nint N;\nint can_L[3],can_R[3];\nint next_can_L[3],next_can_R[3];\nint index_L[3],index_R[3];\nint next_L[3],next_R[3];\nbool merged[SIZE];\nvector<Info_L> L[3];\nvector<Info_R> R[3];\nData data[SIZE];\n\n\nbool is_Cross(int index){\n\n\tif((data[index].X_R < can_L[X]) || (data[index].X_L > can_R[X]) || (data[index].Y_R < can_L[Y])||\n\t\t\t(data[index].Y_L > can_R[Y]) || (data[index].Z_R < can_L[Z]) || (data[index].Z_L > can_R[Z])){\n\n\t\treturn false;\n\t}\n\n\t//★★(0,0)スタートなので、縄張りの辺が安全地帯((0,0)が起源)の内部に含まれていなければいけない★★\n\tif((data[index].X_L >= can_L[X]) || (data[index].X_R <= can_R[X]) || (data[index].Y_L >= can_L[Y])||\n\t\t\t(data[index].Y_R <= can_R[Y])||(data[index].Z_L >= can_L[Z])||(data[index].Z_R <= can_R[Z])){\n\n\t\treturn true;\n\t}\n\n\treturn false;\n}\n\nbool is_OK(int tmp_x,int tmp_y){\n\n\tif((tmp_x < can_L[X]) || (tmp_x > can_R[X]) || (tmp_y < can_L[Y])||\n\t\t\t(tmp_y > can_R[Y]) || ((tmp_x+tmp_y) < can_L[Z]) || ((tmp_x+tmp_y) > can_R[Z])){\n\n\t\treturn false;\n\t}\n\n\tif((tmp_x >= can_L[X]) || (tmp_x <= can_R[X]) || (tmp_y >= can_L[Y])||\n\t\t\t(tmp_y <= can_R[Y])||((tmp_x+tmp_y) >= can_L[Z])||((tmp_x+tmp_y) <= can_R[Z])){\n\n\t\treturn true;\n\t}\n\n\treturn false;\n}\n\n\nint main(){\n\n\tscanf(\"%d\",&N);\n\n\tbool base_FLG = false;\n\n\tint work_x[6],work_y[6];\n\n\tfor(int i = 0; i < 3; i++){\n\n\t\tnext_can_L[i] = 0;\n\t\tnext_can_R[i] = 0;\n\t}\n\n\tfor(int loop = 0; loop < N; loop++){\n\n\t\tbool tmp_FLG = false;\n\n\t\tint min_x = (BIG_NUM+1),max_x = -(BIG_NUM+1);\n\t\tint min_y = (BIG_NUM+1),max_y = -(BIG_NUM+1);\n\t\tint min_z = (BIG_NUM+1),max_z = -(BIG_NUM+1);\n\n\t\tfor(int i = 0; i < 6; i++){\n\n\t\t\tscanf(\"%d %d\",&work_x[i],&work_y[i]);\n\n\t\t\tmin_x = min(min_x,work_x[i]);\n\t\t\tmax_x = max(max_x,work_x[i]);\n\n\t\t\tmin_y = min(min_y,work_y[i]);\n\t\t\tmax_y = max(max_y,work_y[i]);\n\n\t\t\tmin_z = min(min_z,(work_x[i]+work_y[i]));\n\t\t\tmax_z = max(max_z,(work_x[i]+work_y[i]));\n\t\t}\n\t\tdata[loop].set(min_x,max_x,min_y,max_y,min_z,max_z);\n\n\t\tL[X].push_back(Info_L(min_x,loop));\n\t\tL[Y].push_back(Info_L(min_y,loop));\n\t\tL[Z].push_back(Info_L(min_z,loop));\n\n\t\tR[X].push_back(Info_R(max_x,loop));\n\t\tR[Y].push_back(Info_R(max_y,loop));\n\t\tR[Z].push_back(Info_R(max_z,loop));\n\n\t\t//(0,0)を調べる\n\t\tfor(int i = 0; i < 6; i++){\n\n\t\t\tint xa = work_x[i],xb = work_x[(i+1)%6];\n\t\t\tint ya = work_y[i],yb = work_y[(i+1)%6];\n\n\t\t\tif((xa+ya == 0) && (xb+yb == 0) && min(xa,xb) <= 0 && max(xa,xb) >= 0 && min(ya,yb) <= 0 && max(ya,yb) >= 0){\n\n\t\t\t\ttmp_FLG = true;\n\t\t\t\tbase_FLG = true;\n\t\t\t}\n\t\t\tif(xa == 0 && xb == 0 && min(ya,yb) <= 0 && max(ya,yb) >= 0){\n\n\t\t\t\ttmp_FLG = true;\n\t\t\t\tbase_FLG = true;\n\t\t\t}\n\t\t\tif(ya == 0 && yb == 0 && min(xa,xb) <= 0 && max(xa,xb) >= 0){\n\n\t\t\t\ttmp_FLG = true;\n\t\t\t\tbase_FLG = true;\n\t\t\t}\n\t\t}\n\n\t\tif(tmp_FLG){\n\n\t\t\tmerged[loop] = true;\n\n\t\t\tnext_can_L[X] = min(next_can_L[X],data[loop].X_L-1);\n\t\t\tnext_can_R[X] = max(next_can_R[X],data[loop].X_R+1);\n\n\t\t\tnext_can_L[Y] = min(next_can_L[Y],data[loop].Y_L-1);\n\t\t\tnext_can_R[Y] = max(next_can_R[Y],data[loop].Y_R+1);\n\n\t\t\tnext_can_L[Z] = min(next_can_L[Z],data[loop].Z_L-1);\n\t\t\tnext_can_R[Z] = max(next_can_R[Z],data[loop].Z_R+1);\n\t\t}\n\t}\n\n\tif(!base_FLG){\n\n\t\tint tmp;\n\t\tscanf(\"%d\",&tmp);\n\n\t\tint tmp_x,tmp_y;\n\n\t\tfor(int i = 0; i < tmp; i++){\n\n\t\t\tscanf(\"%d %d\",&tmp_x,&tmp_y);\n\t\t\tif(tmp_x == 0 && tmp_y == 0){\n\n\t\t\t\tprintf(\"YES\\n\");\n\n\t\t\t}else{\n\n\t\t\t\tprintf(\"NO\\n\");\n\t\t\t}\n\t\t}\n\t\treturn 0;\n\t}\n\n\tfor(int i = 0; i < 3; i++){\n\n\t\tsort(L[i].begin(),L[i].end());\n\t\tsort(R[i].begin(),R[i].end());\n\n\t\tindex_L[i] = 0;\n\t\tindex_R[i] = 0;\n\t}\n\n\tfor(int i = 0; i < 3; i++){\n\t\t//★★(0,0)が縄張りの辺上にあったため★\n\t\tcan_L[i] = next_can_L[i];\n\t\tcan_R[i] = next_can_R[i];\n\t}\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tmerged[i] = false;\n\t}\n\n\twhile(true){\n\n\t\tvector<int> vec;\n\n\t\tfor(int i = 0; i < 3; i++){\n\n\t\t\tnext_L[i] = index_L[i];\n\t\t\tnext_R[i] = index_R[i];\n\n\t\t\tnext_can_L[i] = can_L[i];\n\t\t\tnext_can_R[i] = can_R[i];\n\t\t}\n\n\t\t//Lを見る\n\t\tfor(int i = 0; i < 3; i++){\n\t\t\twhile(next_L[i] < N && L[i][next_L[i]].value <= can_R[i])next_L[i]++;\n\n\t\t\tfor(int k = index_L[i]; k < next_L[i]; k++){\n\t\t\t\tif(merged[L[i][k].index])continue;\n\t\t\t\tif(is_Cross(L[i][k].index)){\n\n\t\t\t\t\tvec.push_back(L[i][k].index);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t//Rを見る\n\t\tfor(int i = 0; i < 3; i++){\n\t\t\twhile(next_R[i] < N && R[i][next_R[i]].value >= can_L[i])next_R[i]++;\n\n\t\t\tfor(int k = index_R[i]; k < next_R[i]; k++){\n\t\t\t\tif(merged[R[i][k].index])continue;\n\t\t\t\tif(is_Cross(R[i][k].index)){\n\n\t\t\t\t\tvec.push_back(R[i][k].index);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tif(vec.size() == 0)break;\n\n\t\tsort(vec.begin(),vec.end());\n\t\tvec.erase(unique(vec.begin(),vec.end()),vec.end());\n\n\t\tfor(int i = 0; i < vec.size(); i++){\n\n\t\t\tmerged[vec[i]] = true;\n\n\t\t\tnext_can_L[X] = min(next_can_L[X],data[vec[i]].X_L-1);\n\t\t\tnext_can_R[X] = max(next_can_R[X],data[vec[i]].X_R+1);\n\n\t\t\tnext_can_L[Y] = min(next_can_L[Y],data[vec[i]].Y_L-1);\n\t\t\tnext_can_R[Y] = max(next_can_R[Y],data[vec[i]].Y_R+1);\n\n\t\t\tnext_can_L[Z] = min(next_can_L[Z],data[vec[i]].Z_L-1);\n\t\t\tnext_can_R[Z] = max(next_can_R[Z],data[vec[i]].Z_R+1);\n\t\t}\n\n\t\tfor(int i = 0; i < 3; i++){\n\n\t\t\tindex_L[i] = next_L[i];\n\t\t\tindex_R[i] = next_R[i];\n\n\t\t\tcan_L[i] = next_can_L[i];\n\t\t\tcan_R[i] = next_can_R[i];\n\t\t}\n\t}\n\n\tint num_query;\n\tint tmp_x,tmp_y;\n\n\tscanf(\"%d\",&num_query);\n\n\tfor(int loop = 0; loop < num_query; loop++){\n\n\t\tscanf(\"%d %d\",&tmp_x,&tmp_y);\n\n\t\tif(is_OK(tmp_x,tmp_y)){\n\n\t\t\tprintf(\"YES\\n\");\n\n\t\t}else{\n\n\t\t\tprintf(\"NO\\n\");\n\t\t}\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 6220, "score_of_the_acc": -0.1208, "final_rank": 2 }, { "submission_id": "aoj_2240_4872343", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 1000000000000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n#define SIZE 40005\n\nenum Type{\n\tX,\n\tY,\n\tZ,\n};\n\nstruct Info_L{\n\tInfo_L(int arg_value,int arg_index){\n\t\tvalue = arg_value;\n\t\tindex = arg_index;\n\t}\n\tbool operator<(const struct Info_L &arg) const{\n\n\t\treturn value < arg.value; //★昇順★\n\t}\n\tint value,index;\n};\n\nstruct Info_R{\n\tInfo_R(int arg_value,int arg_index){\n\t\tvalue = arg_value;\n\t\tindex = arg_index;\n\t}\n\tbool operator<(const struct Info_R &arg) const{\n\n\t\treturn value > arg.value; //★降順★\n\t}\n\tint value,index;\n};\n\nstruct Data{\n\n\tvoid set(int arg_min_x,int arg_max_x,int arg_min_y,int arg_max_y,int arg_min_z,int arg_max_z){\n\t\tX_L = arg_min_x;\n\t\tX_R = arg_max_x;\n\t\tY_L = arg_min_y;\n\t\tY_R = arg_max_y;\n\t\tZ_L = arg_min_z;\n\t\tZ_R = arg_max_z;\n\t}\n\tvoid debug(){\n\n\t\tprintf(\"X_L:%d X_R:%d\\nY_L:%d Y_R:%d Z_L:%d Z_R:%d\\n\",X_L,X_R,Y_L,Y_R,Z_L,Z_R);\n\t}\n\n\tint X_L,X_R,Y_L,Y_R,Z_L,Z_R;\n};\n\nint N;\nint can_L[3],can_R[3];\nint next_can_L[3],next_can_R[3];\nint index_L[3],index_R[3];\nint next_L[3],next_R[3];\nbool merged[SIZE];\nvector<Info_L> L[3];\nvector<Info_R> R[3];\nData data[SIZE];\n\n\nbool is_Cross(int index){\n\n\tif((data[index].X_R < can_L[X]) || (data[index].X_L > can_R[X]) || (data[index].Y_R < can_L[Y])||\n\t\t\t(data[index].Y_L > can_R[Y]) || (data[index].Z_R < can_L[Z]) || (data[index].Z_L > can_R[Z])){\n\n\t\treturn false;\n\t}\n\n\tif((data[index].X_L >= can_L[X]) || (data[index].X_R <= can_R[X]) || (data[index].Y_L >= can_L[Y])||\n\t\t\t(data[index].Y_R <= can_R[Y])||(data[index].Z_L >= can_L[Z])||(data[index].Z_R <= can_R[Z])){\n\n\t\treturn true;\n\t}\n\n\treturn false;\n}\n\nbool is_OK(int tmp_x,int tmp_y){\n\n\tif((tmp_x < can_L[X]) || (tmp_x > can_R[X]) || (tmp_y < can_L[Y])||\n\t\t\t(tmp_y > can_R[Y]) || ((tmp_x+tmp_y) < can_L[Z]) || ((tmp_x+tmp_y) > can_R[Z])){\n\n\t\treturn false;\n\t}\n\n\tif((tmp_x >= can_L[X]) || (tmp_x <= can_R[X]) || (tmp_y >= can_L[Y])||\n\t\t\t(tmp_y <= can_R[Y])||((tmp_x+tmp_y) >= can_L[Z])||((tmp_x+tmp_y) <= can_R[Z])){\n\n\t\treturn true;\n\t}\n\n\treturn false;\n}\n\n\nint main(){\n\n\tscanf(\"%d\",&N);\n\n\tbool base_FLG = false;\n\n\tint work_x[6],work_y[6];\n\n\tfor(int i = 0; i < 3; i++){\n\n\t\tnext_can_L[i] = 0;\n\t\tnext_can_R[i] = 0;\n\t}\n\n\tfor(int loop = 0; loop < N; loop++){\n\n\t\tbool tmp_FLG = false;\n\n\t\tint min_x = (BIG_NUM+1),max_x = -(BIG_NUM+1);\n\t\tint min_y = (BIG_NUM+1),max_y = -(BIG_NUM+1);\n\t\tint min_z = (BIG_NUM+1),max_z = -(BIG_NUM+1);\n\n\t\tfor(int i = 0; i < 6; i++){\n\n\t\t\tscanf(\"%d %d\",&work_x[i],&work_y[i]);\n\n\t\t\tmin_x = min(min_x,work_x[i]);\n\t\t\tmax_x = max(max_x,work_x[i]);\n\n\t\t\tmin_y = min(min_y,work_y[i]);\n\t\t\tmax_y = max(max_y,work_y[i]);\n\n\t\t\tmin_z = min(min_z,(work_x[i]+work_y[i]));\n\t\t\tmax_z = max(max_z,(work_x[i]+work_y[i]));\n\t\t}\n\t\tdata[loop].set(min_x,max_x,min_y,max_y,min_z,max_z);\n\n\t\tL[X].push_back(Info_L(min_x,loop));\n\t\tL[Y].push_back(Info_L(min_y,loop));\n\t\tL[Z].push_back(Info_L(min_z,loop));\n\n\t\tR[X].push_back(Info_R(max_x,loop));\n\t\tR[Y].push_back(Info_R(max_y,loop));\n\t\tR[Z].push_back(Info_R(max_z,loop));\n\n\t\t//(0,0)を調べる\n\t\tfor(int i = 0; i < 6; i++){\n\n\t\t\tint xa = work_x[i],xb = work_x[(i+1)%6];\n\t\t\tint ya = work_y[i],yb = work_y[(i+1)%6];\n\n\t\t\tif((xa+ya == 0) && (xb+yb == 0) && min(xa,xb) <= 0 && max(xa,xb) >= 0 && min(ya,yb) <= 0 && max(ya,yb) >= 0){\n\n\t\t\t\ttmp_FLG = true;\n\t\t\t\tbase_FLG = true;\n\t\t\t}\n\t\t\tif(xa == 0 && xb == 0 && min(ya,yb) <= 0 && max(ya,yb) >= 0){\n\n\t\t\t\ttmp_FLG = true;\n\t\t\t\tbase_FLG = true;\n\t\t\t}\n\t\t\tif(ya == 0 && yb == 0 && min(xa,xb) <= 0 && max(xa,xb) >= 0){\n\n\t\t\t\ttmp_FLG = true;\n\t\t\t\tbase_FLG = true;\n\t\t\t}\n\t\t}\n\n\t\tif(tmp_FLG){\n\n\t\t\tmerged[loop] = true;\n\n\t\t\tnext_can_L[X] = min(next_can_L[X],data[loop].X_L-1);\n\t\t\tnext_can_R[X] = max(next_can_R[X],data[loop].X_R+1);\n\n\t\t\tnext_can_L[Y] = min(next_can_L[Y],data[loop].Y_L-1);\n\t\t\tnext_can_R[Y] = max(next_can_R[Y],data[loop].Y_R+1);\n\n\t\t\tnext_can_L[Z] = min(next_can_L[Z],data[loop].Z_L-1);\n\t\t\tnext_can_R[Z] = max(next_can_R[Z],data[loop].Z_R+1);\n\t\t}\n\t}\n\n\tif(!base_FLG){\n\n\t\tint tmp;\n\t\tscanf(\"%d\",&tmp);\n\n\t\tint tmp_x,tmp_y;\n\n\t\tfor(int i = 0; i < tmp; i++){\n\n\t\t\tscanf(\"%d %d\",&tmp_x,&tmp_y);\n\t\t\tif(tmp_x == 0 && tmp_y == 0){\n\n\t\t\t\tprintf(\"YES\\n\");\n\n\t\t\t}else{\n\n\t\t\t\tprintf(\"NO\\n\");\n\t\t\t}\n\t\t}\n\t\treturn 0;\n\t}\n\n\tfor(int i = 0; i < 3; i++){\n\n\t\tsort(L[i].begin(),L[i].end());\n\t\tsort(R[i].begin(),R[i].end());\n\n\t\tindex_L[i] = 0;\n\t\tindex_R[i] = 0;\n\t}\n\n/*\tfor(int i = 0; i < N; i++){\n\t\tprintf(\"data[%d] X_L:%d X_R:%d\\nY_L:%d Y_R:%d\\nZ_L:%d Z_R:%d\\n\",i,data[i].X_L,data[i].X_R,data[i].Y_L,data[i].Y_R,data[i].Z_L,data[i].Z_R);\n\t}*/\n\n\tfor(int i = 0; i < 3; i++){\n\t\t//★★(0,0)が縄張りの辺上にあったため★\n\t\tcan_L[i] = next_can_L[i];\n\t\tcan_R[i] = next_can_R[i];\n\t}\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tmerged[i] = false;\n\t}\n\n\t/*for(int i = 0; i < 3; i++){\n\n\t\tprintf(\"can_L[%d]:%d R:%d\\n\",i,can_L[i],can_R[i]);\n\t}*/\n\n\twhile(true){\n\n\t\tvector<int> vec;\n\n\t\tfor(int i = 0; i < 3; i++){\n\n\t\t\tnext_L[i] = index_L[i];\n\t\t\tnext_R[i] = index_R[i];\n\n\t\t\tnext_can_L[i] = can_L[i];\n\t\t\tnext_can_R[i] = can_R[i];\n\t\t}\n\n\t\t//Lを見る\n\t\tfor(int i = 0; i < 3; i++){\n\t\t\twhile(next_L[i] < N && L[i][next_L[i]].value <= can_R[i])next_L[i]++;\n\n\t\t\tfor(int k = index_L[i]; k < next_L[i]; k++){\n\t\t\t\tif(merged[L[i][k].index])continue;\n\t\t\t\tif(is_Cross(L[i][k].index)){\n\n\t\t\t\t\tvec.push_back(L[i][k].index);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t//Rを見る\n\t\tfor(int i = 0; i < 3; i++){\n\t\t\twhile(next_R[i] < N && R[i][next_R[i]].value >= can_L[i])next_R[i]++;\n\n\t\t\tfor(int k = index_R[i]; k < next_R[i]; k++){\n\t\t\t\tif(merged[R[i][k].index])continue;\n\t\t\t\tif(is_Cross(R[i][k].index)){\n\n\t\t\t\t\tvec.push_back(R[i][k].index);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tif(vec.size() == 0)break;\n\n\t\tsort(vec.begin(),vec.end());\n\t\tvec.erase(unique(vec.begin(),vec.end()),vec.end());\n\n\t\tfor(int i = 0; i < vec.size(); i++){\n\n\t\t\tmerged[vec[i]] = true;\n\n\t\t/*\tprintf(\"%dと交差\\n\",vec[i]);\n\t\t\tdata[vec[i]].debug();\n*/\n\n\n\t\t\tnext_can_L[X] = min(next_can_L[X],data[vec[i]].X_L-1);\n\t\t\tnext_can_R[X] = max(next_can_R[X],data[vec[i]].X_R+1);\n\n\t\t\tnext_can_L[Y] = min(next_can_L[Y],data[vec[i]].Y_L-1);\n\t\t\tnext_can_R[Y] = max(next_can_R[Y],data[vec[i]].Y_R+1);\n\n\t\t\tnext_can_L[Z] = min(next_can_L[Z],data[vec[i]].Z_L-1);\n\t\t\tnext_can_R[Z] = max(next_can_R[Z],data[vec[i]].Z_R+1);\n\t\t}\n\n\t\tfor(int i = 0; i < 3; i++){\n\n\t\t\tindex_L[i] = next_L[i];\n\t\t\tindex_R[i] = next_R[i];\n\n\t\t\tcan_L[i] = next_can_L[i];\n\t\t\tcan_R[i] = next_can_R[i];\n\t\t}\n\t}\n\n\t/*for(int i = 0; i < 3; i++){\n\n\t\tprintf(\"can_L[%d]:%d can_R:%d\\n\",i,can_L[i],can_R[i]);\n\t}*/\n\n\tint num_query;\n\tint tmp_x,tmp_y;\n\n\tscanf(\"%d\",&num_query);\n\n\tfor(int loop = 0; loop < num_query; loop++){\n\n\t\tscanf(\"%d %d\",&tmp_x,&tmp_y);\n\n\t\tif(is_OK(tmp_x,tmp_y)){\n\n\t\t\tprintf(\"YES\\n\");\n\n\t\t}else{\n\n\t\t\tprintf(\"NO\\n\");\n\t\t}\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 6212, "score_of_the_acc": -0.1206, "final_rank": 1 }, { "submission_id": "aoj_2240_4872334", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 1000000000000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n#define SIZE 40005\n\nenum Type{\n\tX,\n\tY,\n\tZ,\n};\n\nstruct Info_L{\n\tInfo_L(int arg_value,int arg_index){\n\t\tvalue = arg_value;\n\t\tindex = arg_index;\n\t}\n\tbool operator<(const struct Info_L &arg) const{\n\n\t\treturn value < arg.value; //★昇順★\n\t}\n\tint value,index;\n};\n\nstruct Info_R{\n\tInfo_R(int arg_value,int arg_index){\n\t\tvalue = arg_value;\n\t\tindex = arg_index;\n\t}\n\tbool operator<(const struct Info_R &arg) const{\n\n\t\treturn value > arg.value; //★降順★\n\t}\n\tint value,index;\n};\n\nstruct Data{\n\n\tvoid set(int arg_min_x,int arg_max_x,int arg_min_y,int arg_max_y,int arg_min_z,int arg_max_z){\n\t\tX_L = arg_min_x;\n\t\tX_R = arg_max_x;\n\t\tY_L = arg_min_y;\n\t\tY_R = arg_max_y;\n\t\tZ_L = arg_min_z;\n\t\tZ_R = arg_max_z;\n\t}\n\tvoid debug(){\n\n\t\tprintf(\"X_L:%d X_R:%d\\nY_L:%d Y_R:%d Z_L:%d Z_R:%d\\n\",X_L,X_R,Y_L,Y_R,Z_L,Z_R);\n\t}\n\n\tint X_L,X_R,Y_L,Y_R,Z_L,Z_R;\n};\n\nint N;\nint can_L[3],can_R[3];\nint next_can_L[3],next_can_R[3];\nint index_L[3],index_R[3];\nint next_L[3],next_R[3];\nbool merged[SIZE];\nvector<Info_L> L[3];\nvector<Info_R> R[3];\nData data[SIZE];\n\n\nbool is_Cross(int index){\n\n\tif((data[index].X_R < can_L[X]) || (data[index].X_L > can_R[X]) || (data[index].Y_R < can_L[Y])||\n\t\t\t(data[index].Y_L > can_R[Y]) || (data[index].Z_R < can_L[Z]) || (data[index].Z_L > can_R[Z])){\n\n\t\treturn false;\n\t}\n\n\tif((data[index].X_L >= can_L[X]) || (data[index].X_R <= can_R[X]) || (data[index].Y_L >= can_L[Y])||\n\t\t\t(data[index].Y_R <= can_R[Y])||(data[index].Z_L >= can_L[Z])||(data[index].Z_R <= can_R[Z])){\n\n\t\treturn true;\n\t}\n\n\treturn false;\n}\n\nbool is_OK(int tmp_x,int tmp_y){\n\n\tif((tmp_x < can_L[X]) || (tmp_x > can_R[X]) || (tmp_y < can_L[Y])||\n\t\t\t(tmp_y > can_R[Y]) || ((tmp_x+tmp_y) < can_L[Z]) || ((tmp_x+tmp_y) > can_R[Z])){\n\n\t\treturn false;\n\t}\n\n\tif((tmp_x >= can_L[X]) || (tmp_x <= can_R[X]) || (tmp_y >= can_L[Y])||\n\t\t\t(tmp_y <= can_R[Y])||((tmp_x+tmp_y) >= can_L[Z])||((tmp_x+tmp_y) <= can_R[Z])){\n\n\t\treturn true;\n\t}\n\n\treturn false;\n}\n\n\nint main(){\n\n\tscanf(\"%d\",&N);\n\n\tbool base_FLG = false;\n\n\tint work_x[6],work_y[6];\n\n\tfor(int i = 0; i < 3; i++){\n\n\t\tnext_can_L[i] = 0;\n\t\tnext_can_R[i] = 0;\n\t}\n\n\tfor(int loop = 0; loop < N; loop++){\n\n\t\tbool tmp_FLG = false;\n\n\t\tint min_x = (BIG_NUM+1),max_x = -(BIG_NUM+1);\n\t\tint min_y = (BIG_NUM+1),max_y = -(BIG_NUM+1);\n\t\tint min_z = (BIG_NUM+1),max_z = -(BIG_NUM+1);\n\n\t\tfor(int i = 0; i < 6; i++){\n\n\t\t\tscanf(\"%d %d\",&work_x[i],&work_y[i]);\n\n\t\t\tmin_x = min(min_x,work_x[i]);\n\t\t\tmax_x = max(max_x,work_x[i]);\n\n\t\t\tmin_y = min(min_y,work_y[i]);\n\t\t\tmax_y = max(max_y,work_y[i]);\n\n\t\t\tmin_z = min(min_z,(work_x[i]+work_y[i]));\n\t\t\tmax_z = max(max_z,(work_x[i]+work_y[i]));\n\t\t}\n\t\tdata[loop].set(min_x,max_x,min_y,max_y,min_z,max_z);\n\n\t\tL[X].push_back(Info_L(min_x,loop));\n\t\tL[Y].push_back(Info_L(min_y,loop));\n\t\tL[Z].push_back(Info_L(min_z,loop));\n\n\t\tR[X].push_back(Info_R(max_x,loop));\n\t\tR[Y].push_back(Info_R(max_y,loop));\n\t\tR[Z].push_back(Info_R(max_z,loop));\n\n\t\t//(0,0)を調べる\n\t\tfor(int i = 0; i < 6; i++){\n\n\t\t\tint xa = work_x[i],xb = work_x[(i+1)%6];\n\t\t\tint ya = work_y[i],yb = work_y[(i+1)%6];\n\n\t\t\tif((xa+ya == 0) && (xb+yb == 0) && min(xa,xb) <= 0 && max(xa,xb) >= 0 && min(ya,yb) <= 0 && max(ya,yb) >= 0){\n\n\t\t\t\ttmp_FLG = true;\n\t\t\t\tbase_FLG = true;\n\t\t\t}\n\t\t\tif(xa == 0 && xb == 0 && min(ya,yb) <= 0 && max(ya,yb) >= 0){\n\n\t\t\t\ttmp_FLG = true;\n\t\t\t\tbase_FLG = true;\n\t\t\t}\n\t\t\tif(ya == 0 && yb == 0 && min(xa,xb) <= 0 && max(xa,xb) >= 0){\n\n\t\t\t\ttmp_FLG = true;\n\t\t\t\tbase_FLG = true;\n\t\t\t}\n\t\t}\n\n\t\tif(tmp_FLG){\n\n\t\t\tmerged[loop] = true;\n\n\t\t\tnext_can_L[X] = min(next_can_L[X],data[loop].X_L-1);\n\t\t\tnext_can_R[X] = max(next_can_R[X],data[loop].X_R+1);\n\n\t\t\tnext_can_L[Y] = min(next_can_L[Y],data[loop].Y_L-1);\n\t\t\tnext_can_R[Y] = max(next_can_R[Y],data[loop].Y_R+1);\n\n\t\t\tnext_can_L[Z] = min(next_can_L[Z],data[loop].Z_L-1);\n\t\t\tnext_can_R[Z] = max(next_can_R[Z],data[loop].Z_R+1);\n\t\t}\n\t}\n\n\tif(!base_FLG){\n\n\t\tint tmp;\n\t\tscanf(\"%d\",&tmp);\n\n\t\tint tmp_x,tmp_y;\n\n\t\tfor(int i = 0; i < tmp; i++){\n\n\t\t\tscanf(\"%d %d\",&tmp_x,&tmp_y);\n\t\t\tif(tmp_x == 0 && tmp_y == 0){\n\n\t\t\t\tprintf(\"YES\\n\");\n\n\t\t\t}else{\n\n\t\t\t\tprintf(\"NO\\n\");\n\t\t\t}\n\t\t}\n\t\treturn 0;\n\t}\n\n\tfor(int i = 0; i < 3; i++){\n\n\t\tsort(L[i].begin(),L[i].end());\n\t\tsort(R[i].begin(),R[i].end());\n\n\t\tindex_L[i] = 0;\n\t\tindex_R[i] = 0;\n\t}\n\n/*\tfor(int i = 0; i < N; i++){\n\t\tprintf(\"data[%d] X_L:%d X_R:%d\\nY_L:%d Y_R:%d\\nZ_L:%d Z_R:%d\\n\",i,data[i].X_L,data[i].X_R,data[i].Y_L,data[i].Y_R,data[i].Z_L,data[i].Z_R);\n\t}*/\n\n\tfor(int i = 0; i < 3; i++){\n\t\t//★★(0,0)が縄張りの辺上にあったため★\n\t\tcan_L[i] = -1;\n\t\tcan_R[i] = 1;\n\t}\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tmerged[i] = false;\n\t}\n\n\t/*for(int i = 0; i < 3; i++){\n\n\t\tprintf(\"can_L[%d]:%d R:%d\\n\",i,can_L[i],can_R[i]);\n\t}*/\n\n\twhile(true){\n\n\t\tvector<int> vec;\n\n\t\tfor(int i = 0; i < 3; i++){\n\n\t\t\tnext_L[i] = index_L[i];\n\t\t\tnext_R[i] = index_R[i];\n\n\t\t\tnext_can_L[i] = can_L[i];\n\t\t\tnext_can_R[i] = can_R[i];\n\t\t}\n\n\t\t//Lを見る\n\t\tfor(int i = 0; i < 3; i++){\n\t\t\twhile(next_L[i] < N && L[i][next_L[i]].value <= can_R[i])next_L[i]++;\n\n\t\t\tfor(int k = index_L[i]; k < next_L[i]; k++){\n\t\t\t\tif(merged[L[i][k].index])continue;\n\t\t\t\tif(is_Cross(L[i][k].index)){\n\n\t\t\t\t\tvec.push_back(L[i][k].index);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t//Rを見る\n\t\tfor(int i = 0; i < 3; i++){\n\t\t\twhile(next_R[i] < N && R[i][next_R[i]].value >= can_L[i])next_R[i]++;\n\n\t\t\tfor(int k = index_R[i]; k < next_R[i]; k++){\n\t\t\t\tif(merged[R[i][k].index])continue;\n\t\t\t\tif(is_Cross(R[i][k].index)){\n\n\t\t\t\t\tvec.push_back(R[i][k].index);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tif(vec.size() == 0)break;\n\n\t\tsort(vec.begin(),vec.end());\n\t\tvec.erase(unique(vec.begin(),vec.end()),vec.end());\n\n\t\tfor(int i = 0; i < vec.size(); i++){\n\n\t\t\tmerged[vec[i]] = true;\n\n\t\t/*\tprintf(\"%dと交差\\n\",vec[i]);\n\t\t\tdata[vec[i]].debug();\n*/\n\n\n\t\t\tnext_can_L[X] = min(next_can_L[X],data[vec[i]].X_L-1);\n\t\t\tnext_can_R[X] = max(next_can_R[X],data[vec[i]].X_R+1);\n\n\t\t\tnext_can_L[Y] = min(next_can_L[Y],data[vec[i]].Y_L-1);\n\t\t\tnext_can_R[Y] = max(next_can_R[Y],data[vec[i]].Y_R+1);\n\n\t\t\tnext_can_L[Z] = min(next_can_L[Z],data[vec[i]].Z_L-1);\n\t\t\tnext_can_R[Z] = max(next_can_R[Z],data[vec[i]].Z_R+1);\n\t\t}\n\n\t\tfor(int i = 0; i < 3; i++){\n\n\t\t\tindex_L[i] = next_L[i];\n\t\t\tindex_R[i] = next_R[i];\n\n\t\t\tcan_L[i] = next_can_L[i];\n\t\t\tcan_R[i] = next_can_R[i];\n\t\t}\n\t}\n\n\t/*for(int i = 0; i < 3; i++){\n\n\t\tprintf(\"can_L[%d]:%d can_R:%d\\n\",i,can_L[i],can_R[i]);\n\t}*/\n\n\tint num_query;\n\tint tmp_x,tmp_y;\n\n\tscanf(\"%d\",&num_query);\n\n\tfor(int loop = 0; loop < num_query; loop++){\n\n\t\tscanf(\"%d %d\",&tmp_x,&tmp_y);\n\n\t\tif(is_OK(tmp_x,tmp_y)){\n\n\t\t\tprintf(\"YES\\n\");\n\n\t\t}else{\n\n\t\t\tprintf(\"NO\\n\");\n\t\t}\n\t}\n\n\treturn 0;\n}", "accuracy": 0.56, "time_ms": 30, "memory_kb": 5172, "score_of_the_acc": -0.0018, "final_rank": 9 }, { "submission_id": "aoj_2240_4872300", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 1000000000000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n#define SIZE 40005\n\nenum Type{\n\tX,\n\tY,\n\tZ,\n};\n\nstruct Info_L{\n\tInfo_L(int arg_value,int arg_index){\n\t\tvalue = arg_value;\n\t\tindex = arg_index;\n\t}\n\tbool operator<(const struct Info_L &arg) const{\n\n\t\treturn value < arg.value; //★昇順★\n\t}\n\tint value,index;\n};\n\nstruct Info_R{\n\tInfo_R(int arg_value,int arg_index){\n\t\tvalue = arg_value;\n\t\tindex = arg_index;\n\t}\n\tbool operator<(const struct Info_R &arg) const{\n\n\t\treturn value > arg.value; //★降順★\n\t}\n\tint value,index;\n};\n\nstruct Data{\n\n\tvoid set(int arg_min_x,int arg_max_x,int arg_min_y,int arg_max_y,int arg_min_z,int arg_max_z){\n\t\tX_L = arg_min_x;\n\t\tX_R = arg_max_x;\n\t\tY_L = arg_min_y;\n\t\tY_R = arg_max_y;\n\t\tZ_L = arg_min_z;\n\t\tZ_R = arg_max_z;\n\t}\n\tvoid debug(){\n\n\t\tprintf(\"X_L:%d X_R:%d\\nY_L:%d Y_R:%d Z_L:%d Z_R:%d\\n\",X_L,X_R,Y_L,Y_R,Z_L,Z_R);\n\t}\n\n\tint X_L,X_R,Y_L,Y_R,Z_L,Z_R;\n};\n\nint N;\nint can_L[3],can_R[3];\nint next_can_L[3],next_can_R[3];\nint index_L[3],index_R[3];\nint next_L[3],next_R[3];\nbool merged[SIZE];\nvector<Info_L> L[3];\nvector<Info_R> R[3];\nData data[SIZE];\n\n\nbool is_Cross(int index){\n\n\tif((data[index].X_R < can_L[X]) || (data[index].X_L > can_R[X]) || (data[index].Y_R < can_L[Y])||\n\t\t\t(data[index].Y_L > can_R[Y]) || (data[index].Z_R < can_L[Z]) || (data[index].Z_L > can_R[Z])){\n\n\t\treturn false;\n\t}\n\n\tif((data[index].X_L >= can_L[X]) || (data[index].X_R <= can_R[X]) || (data[index].Y_L >= can_L[Y])||\n\t\t\t(data[index].Y_R <= can_R[Y])||(data[index].Z_L >= can_L[Z])||(data[index].Z_R <= can_R[Z])){\n\n\t\treturn true;\n\t}\n\n\treturn false;\n}\n\nbool is_OK(int tmp_x,int tmp_y){\n\n\tif((tmp_x < can_L[X]) || (tmp_x > can_R[X]) || (tmp_y < can_L[Y])||\n\t\t\t(tmp_y > can_R[Y]) || ((tmp_x+tmp_y) < can_L[Z]) || ((tmp_x+tmp_y) > can_R[Z])){\n\n\t\treturn false;\n\t}\n\n\tif((tmp_x >= can_L[X]) || (tmp_x <= can_R[X]) || (tmp_y >= can_L[Y])||\n\t\t\t(tmp_y <= can_R[Y])||((tmp_x+tmp_y) >= can_L[Z])||((tmp_x+tmp_y) <= can_R[Z])){\n\n\t\treturn true;\n\t}\n\n\treturn false;\n}\n\n\nint main(){\n\n\tscanf(\"%d\",&N);\n\n\tbool base_FLG = false;\n\n\tint work_x[6],work_y[6];\n\n\tfor(int loop = 0; loop < N; loop++){\n\n\t\tint min_x = (BIG_NUM+1),max_x = -(BIG_NUM+1);\n\t\tint min_y = (BIG_NUM+1),max_y = -(BIG_NUM+1);\n\t\tint min_z = (BIG_NUM+1),max_z = -(BIG_NUM+1);\n\n\t\tfor(int i = 0; i < 6; i++){\n\n\t\t\tscanf(\"%d %d\",&work_x[i],&work_y[i]);\n\n\t\t\tmin_x = min(min_x,work_x[i]);\n\t\t\tmax_x = max(max_x,work_x[i]);\n\n\t\t\tmin_y = min(min_y,work_y[i]);\n\t\t\tmax_y = max(max_y,work_y[i]);\n\n\t\t\tmin_z = min(min_z,(work_x[i]+work_y[i]));\n\t\t\tmax_z = max(max_z,(work_x[i]+work_y[i]));\n\t\t}\n\t\tdata[loop].set(min_x,max_x,min_y,max_y,min_z,max_z);\n\n\t\tL[X].push_back(Info_L(min_x,loop));\n\t\tL[Y].push_back(Info_L(min_y,loop));\n\t\tL[Z].push_back(Info_L(min_z,loop));\n\n\t\tR[X].push_back(Info_R(max_x,loop));\n\t\tR[Y].push_back(Info_R(max_y,loop));\n\t\tR[Z].push_back(Info_R(max_z,loop));\n\n\t\t//(0,0)を調べる\n\t\tfor(int i = 0; i < 6; i++){\n\n\t\t\tint xa = work_x[i],xb = work_x[(i+1)%6];\n\t\t\tint ya = work_y[i],yb = work_y[(i+1)%6];\n\n\t\t\tif((xa+ya == 0) && (xb+yb == 0) && min(xa,xb) <= 0 && max(xa,xb) >= 0 && min(ya,yb) <= 0 && max(ya,yb) >= 0){\n\n\t\t\t\tbase_FLG = true;\n\t\t\t}\n\t\t\tif(xa == 0 && xb == 0 && min(ya,yb) <= 0 && max(ya,yb) >= 0){\n\n\t\t\t\tbase_FLG = true;\n\t\t\t}\n\t\t\tif(ya == 0 && yb == 0 && min(xa,xb) <= 0 && max(xa,xb) >= 0){\n\n\t\t\t\tbase_FLG = true;\n\t\t\t}\n\t\t}\n\t}\n\n\tif(!base_FLG){\n\n\t\tint tmp;\n\t\tscanf(\"%d\",&tmp);\n\n\t\tint tmp_x,tmp_y;\n\n\t\tfor(int i = 0; i < tmp; i++){\n\n\t\t\tscanf(\"%d %d\",&tmp_x,&tmp_y);\n\t\t\tif(tmp_x == 0 && tmp_y == 0){\n\n\t\t\t\tprintf(\"YES\\n\");\n\n\t\t\t}else{\n\n\t\t\t\tprintf(\"NO\\n\");\n\t\t\t}\n\t\t}\n\t\treturn 0;\n\t}\n\n\tfor(int i = 0; i < 3; i++){\n\n\t\tsort(L[i].begin(),L[i].end());\n\t\tsort(R[i].begin(),R[i].end());\n\n\t\tindex_L[i] = 0;\n\t\tindex_R[i] = 0;\n\t}\n\n/*\tfor(int i = 0; i < N; i++){\n\t\tprintf(\"data[%d] X_L:%d X_R:%d\\nY_L:%d Y_R:%d\\nZ_L:%d Z_R:%d\\n\",i,data[i].X_L,data[i].X_R,data[i].Y_L,data[i].Y_R,data[i].Z_L,data[i].Z_R);\n\t}*/\n\n\tfor(int i = 0; i < 3; i++){\n\t\t//★★(0,0)が縄張りの辺上にあったため★\n\t\tcan_L[i] = -1;\n\t\tcan_R[i] = 1;\n\t}\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tmerged[i] = false;\n\t}\n\n\t/*for(int i = 0; i < 3; i++){\n\n\t\tprintf(\"can_L[%d]:%d R:%d\\n\",i,can_L[i],can_R[i]);\n\t}*/\n\n\twhile(true){\n\n\t\tvector<int> vec;\n\n\t\tfor(int i = 0; i < 3; i++){\n\n\t\t\tnext_L[i] = index_L[i];\n\t\t\tnext_R[i] = index_R[i];\n\n\t\t\tnext_can_L[i] = can_L[i];\n\t\t\tnext_can_R[i] = can_R[i];\n\t\t}\n\n\t\t//Lを見る\n\t\tfor(int i = 0; i < 3; i++){\n\t\t\twhile(next_L[i] < N && L[i][next_L[i]].value <= can_R[i])next_L[i]++;\n\n\t\t\tfor(int k = index_L[i]; k < next_L[i]; k++){\n\t\t\t\tif(merged[L[i][k].index])continue;\n\t\t\t\tif(is_Cross(L[i][k].index)){\n\n\t\t\t\t\tvec.push_back(L[i][k].index);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t//Rを見る\n\t\tfor(int i = 0; i < 3; i++){\n\t\t\twhile(next_R[i] < N && R[i][next_R[i]].value >= can_L[i])next_R[i]++;\n\n\t\t\tfor(int k = index_R[i]; k < next_R[i]; k++){\n\t\t\t\tif(merged[R[i][k].index])continue;\n\t\t\t\tif(is_Cross(R[i][k].index)){\n\n\t\t\t\t\tvec.push_back(R[i][k].index);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tif(vec.size() == 0)break;\n\n\t\tsort(vec.begin(),vec.end());\n\t\tvec.erase(unique(vec.begin(),vec.end()),vec.end());\n\n\t\tfor(int i = 0; i < vec.size(); i++){\n\n\t\t\tmerged[vec[i]] = true;\n\n\t\t/*\tprintf(\"%dと交差\\n\",vec[i]);\n\t\t\tdata[vec[i]].debug();\n*/\n\n\n\t\t\tnext_can_L[X] = min(next_can_L[X],data[vec[i]].X_L-1);\n\t\t\tnext_can_R[X] = max(next_can_R[X],data[vec[i]].X_R+1);\n\n\t\t\tnext_can_L[Y] = min(next_can_L[Y],data[vec[i]].Y_L-1);\n\t\t\tnext_can_R[Y] = max(next_can_R[Y],data[vec[i]].Y_R+1);\n\n\t\t\tnext_can_L[Z] = min(next_can_L[Z],data[vec[i]].Z_L-1);\n\t\t\tnext_can_R[Z] = max(next_can_R[Z],data[vec[i]].Z_R+1);\n\t\t}\n\n\t\tfor(int i = 0; i < 3; i++){\n\n\t\t\tindex_L[i] = next_L[i];\n\t\t\tindex_R[i] = next_R[i];\n\n\t\t\tcan_L[i] = next_can_L[i];\n\t\t\tcan_R[i] = next_can_R[i];\n\t\t}\n\t}\n\n\t/*for(int i = 0; i < 3; i++){\n\n\t\tprintf(\"can_L[%d]:%d can_R:%d\\n\",i,can_L[i],can_R[i]);\n\t}*/\n\n\tint num_query;\n\tint tmp_x,tmp_y;\n\n\tscanf(\"%d\",&num_query);\n\n\tfor(int loop = 0; loop < num_query; loop++){\n\n\t\tscanf(\"%d %d\",&tmp_x,&tmp_y);\n\n\t\tif(is_OK(tmp_x,tmp_y)){\n\n\t\t\tprintf(\"YES\\n\");\n\n\t\t}else{\n\n\t\t\tprintf(\"NO\\n\");\n\t\t}\n\t}\n\n\treturn 0;\n}", "accuracy": 0.56, "time_ms": 30, "memory_kb": 5104, "score_of_the_acc": 0, "final_rank": 7 }, { "submission_id": "aoj_2240_4872281", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 1000000000000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n#define SIZE 40005\n\nenum Type{\n\tX,\n\tY,\n\tZ,\n};\n\nstruct Info_L{\n\tInfo_L(int arg_value,int arg_index){\n\t\tvalue = arg_value;\n\t\tindex = arg_index;\n\t}\n\tbool operator<(const struct Info_L &arg) const{\n\n\t\treturn value < arg.value; //★昇順★\n\t}\n\tint value,index;\n};\n\nstruct Info_R{\n\tInfo_R(int arg_value,int arg_index){\n\t\tvalue = arg_value;\n\t\tindex = arg_index;\n\t}\n\tbool operator<(const struct Info_R &arg) const{\n\n\t\treturn value > arg.value; //★降順★\n\t}\n\tint value,index;\n};\n\nstruct Data{\n\n\tvoid set(int arg_min_x,int arg_max_x,int arg_min_y,int arg_max_y,int arg_min_z,int arg_max_z){\n\t\tX_L = arg_min_x;\n\t\tX_R = arg_max_x;\n\t\tY_L = arg_min_y;\n\t\tY_R = arg_max_y;\n\t\tZ_L = arg_min_z;\n\t\tZ_R = arg_max_z;\n\t}\n\tvoid debug(){\n\n\t\tprintf(\"X_L:%d X_R:%d\\nY_L:%d Y_R:%d Z_L:%d Z_R:%d\\n\",X_L,X_R,Y_L,Y_R,Z_L,Z_R);\n\t}\n\n\tint X_L,X_R,Y_L,Y_R,Z_L,Z_R;\n};\n\nint N;\nint can_L[3],can_R[3];\nint next_can_L[3],next_can_R[3];\nint index_L[3],index_R[3];\nint next_L[3],next_R[3];\nbool merged[SIZE];\nvector<Info_L> L[3];\nvector<Info_R> R[3];\nData data[SIZE];\n\n\nbool is_Cross(int index){\n\n\tif((data[index].X_R < can_L[X]) || (data[index].X_L > can_R[X]) || (data[index].Y_R < can_L[Y])||\n\t\t\t(data[index].Y_L > can_R[Y]) || (data[index].Z_R < can_L[Z]) || (data[index].Z_L > can_R[Z])){\n\n\t\treturn false;\n\t}\n\n\tif((data[index].X_L >= can_L[X]) || (data[index].X_R <= can_R[X]) || (data[index].Y_L >= can_L[Y])||\n\t\t\t(data[index].Y_R <= can_R[Y])||(data[index].Z_L >= can_L[Z])||(data[index].Z_R <= can_R[Z])){\n\n\t\treturn true;\n\t}\n\n\treturn false;\n}\n\nbool is_OK(int tmp_x,int tmp_y){\n\n\tif((tmp_x < can_L[X]) || (tmp_x > can_R[X]) || (tmp_y < can_L[Y])||\n\t\t\t(tmp_y > can_R[Y]) || ((tmp_x+tmp_y) < can_L[Z]) || ((tmp_x+tmp_y) > can_R[Z])){\n\n\t\treturn false;\n\t}\n\n\tif((tmp_x >= can_L[X]) || (tmp_x <= can_R[X]) || (tmp_y >= can_L[Y])||\n\t\t\t(tmp_y <= can_R[Y])||((tmp_x+tmp_y) >= can_L[Z])||((tmp_x+tmp_y) <= can_R[Z])){\n\n\t\treturn true;\n\t}\n\n\treturn false;\n}\n\n\nint main(){\n\n\tscanf(\"%d\",&N);\n\n\tbool base_FLG = false;\n\n\tint work_x[6],work_y[6];\n\n\tfor(int loop = 0; loop < N; loop++){\n\n\t\tint min_x = (BIG_NUM+1),max_x = -(BIG_NUM+1);\n\t\tint min_y = (BIG_NUM+1),max_y = -(BIG_NUM+1);\n\t\tint min_z = (BIG_NUM+1),max_z = -(BIG_NUM+1);\n\n\t\tfor(int i = 0; i < 6; i++){\n\n\t\t\tscanf(\"%d %d\",&work_x[i],&work_y[i]);\n\n\t\t\tmin_x = min(min_x,work_x[i]);\n\t\t\tmax_x = max(max_x,work_x[i]);\n\n\t\t\tmin_y = min(min_y,work_y[i]);\n\t\t\tmax_y = max(max_y,work_y[i]);\n\n\t\t\tmin_z = min(min_z,(work_x[i]+work_y[i]));\n\t\t\tmax_z = max(max_z,(work_x[i]+work_y[i]));\n\t\t}\n\t\tdata[loop].set(min_x,max_x,min_y,max_y,min_z,max_z);\n\n\t\tL[X].push_back(Info_L(min_x,loop));\n\t\tL[Y].push_back(Info_L(min_y,loop));\n\t\tL[Z].push_back(Info_L(min_z,loop));\n\n\t\tR[X].push_back(Info_R(max_x,loop));\n\t\tR[Y].push_back(Info_R(max_y,loop));\n\t\tR[Z].push_back(Info_R(max_z,loop));\n\n\t\tfor(int i = 0; i < 6; i++){\n\n\t\t\tint xa = work_x[i],xb = work_x[(i+1)%6];\n\t\t\tint ya = work_y[i],yb = work_y[(i+1)%6];\n\n\t\t\tif((xa+ya == 0) && (xb+yb == 0) && min(xa,xb) <= 0 && max(xa,xb) >= 0 && min(ya,yb) <= 0 && max(ya,yb) >= 0){\n\n\t\t\t\tbase_FLG = true;\n\t\t\t}\n\t\t}\n\t}\n\n\tif(!base_FLG){\n\n\t\tint tmp;\n\t\tscanf(\"%d\",&tmp);\n\n\t\tint tmp_x,tmp_y;\n\n\t\tfor(int i = 0; i < tmp; i++){\n\n\t\t\tscanf(\"%d %d\",&tmp_x,&tmp_y);\n\t\t\tif(tmp_x == 0 && tmp_y == 0){\n\n\t\t\t\tprintf(\"YES\\n\");\n\n\t\t\t}else{\n\n\t\t\t\tprintf(\"NO\\n\");\n\t\t\t}\n\t\t}\n\t\treturn 0;\n\t}\n\n\tfor(int i = 0; i < 3; i++){\n\n\t\tsort(L[i].begin(),L[i].end());\n\t\tsort(R[i].begin(),R[i].end());\n\n\t\tindex_L[i] = 0;\n\t\tindex_R[i] = 0;\n\t}\n\n/*\tfor(int i = 0; i < N; i++){\n\t\tprintf(\"data[%d] X_L:%d X_R:%d\\nY_L:%d Y_R:%d\\nZ_L:%d Z_R:%d\\n\",i,data[i].X_L,data[i].X_R,data[i].Y_L,data[i].Y_R,data[i].Z_L,data[i].Z_R);\n\t}*/\n\n\tfor(int i = 0; i < 3; i++){\n\t\t//★★(0,0)が縄張りの辺上にあったため★\n\t\tcan_L[i] = -1;\n\t\tcan_R[i] = 1;\n\t}\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tmerged[i] = false;\n\t}\n\n\t/*for(int i = 0; i < 3; i++){\n\n\t\tprintf(\"can_L[%d]:%d R:%d\\n\",i,can_L[i],can_R[i]);\n\t}*/\n\n\twhile(true){\n\n\t\tvector<int> vec;\n\n\t\tfor(int i = 0; i < 3; i++){\n\n\t\t\tnext_L[i] = index_L[i];\n\t\t\tnext_R[i] = index_R[i];\n\n\t\t\tnext_can_L[i] = can_L[i];\n\t\t\tnext_can_R[i] = can_R[i];\n\t\t}\n\n\t\t//Lを見る\n\t\tfor(int i = 0; i < 3; i++){\n\t\t\twhile(next_L[i] < N && L[i][next_L[i]].value <= can_R[i])next_L[i]++;\n\n\t\t\tfor(int k = index_L[i]; k < next_L[i]; k++){\n\t\t\t\tif(merged[L[i][k].index])continue;\n\t\t\t\tif(is_Cross(L[i][k].index)){\n\n\t\t\t\t\tvec.push_back(L[i][k].index);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t//Rを見る\n\t\tfor(int i = 0; i < 3; i++){\n\t\t\twhile(next_R[i] < N && R[i][next_R[i]].value >= can_L[i])next_R[i]++;\n\n\t\t\tfor(int k = index_R[i]; k < next_R[i]; k++){\n\t\t\t\tif(merged[R[i][k].index])continue;\n\t\t\t\tif(is_Cross(R[i][k].index)){\n\n\t\t\t\t\tvec.push_back(R[i][k].index);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tif(vec.size() == 0)break;\n\n\t\tsort(vec.begin(),vec.end());\n\t\tvec.erase(unique(vec.begin(),vec.end()),vec.end());\n\n\t\tfor(int i = 0; i < vec.size(); i++){\n\n\t\t\tmerged[vec[i]] = true;\n\n\t\t/*\tprintf(\"%dと交差\\n\",vec[i]);\n\t\t\tdata[vec[i]].debug();\n*/\n\n\n\t\t\tnext_can_L[X] = min(next_can_L[X],data[vec[i]].X_L-1);\n\t\t\tnext_can_R[X] = max(next_can_R[X],data[vec[i]].X_R+1);\n\n\t\t\tnext_can_L[Y] = min(next_can_L[Y],data[vec[i]].Y_L-1);\n\t\t\tnext_can_R[Y] = max(next_can_R[Y],data[vec[i]].Y_R+1);\n\n\t\t\tnext_can_L[Z] = min(next_can_L[Z],data[vec[i]].Z_L-1);\n\t\t\tnext_can_R[Z] = max(next_can_R[Z],data[vec[i]].Z_R+1);\n\t\t}\n\n\t\tfor(int i = 0; i < 3; i++){\n\n\t\t\tindex_L[i] = next_L[i];\n\t\t\tindex_R[i] = next_R[i];\n\n\t\t\tcan_L[i] = next_can_L[i];\n\t\t\tcan_R[i] = next_can_R[i];\n\t\t}\n\t}\n\n\t/*for(int i = 0; i < 3; i++){\n\n\t\tprintf(\"can_L[%d]:%d can_R:%d\\n\",i,can_L[i],can_R[i]);\n\t}*/\n\n\tint num_query;\n\tint tmp_x,tmp_y;\n\n\tscanf(\"%d\",&num_query);\n\n\tfor(int loop = 0; loop < num_query; loop++){\n\n\t\tscanf(\"%d %d\",&tmp_x,&tmp_y);\n\n\t\tif(is_OK(tmp_x,tmp_y)){\n\n\t\t\tprintf(\"YES\\n\");\n\n\t\t}else{\n\n\t\t\tprintf(\"NO\\n\");\n\t\t}\n\t}\n\n\treturn 0;\n}", "accuracy": 0.12, "time_ms": 30, "memory_kb": 5128, "score_of_the_acc": -0.0006, "final_rank": 11 }, { "submission_id": "aoj_2240_4872116", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 1000000000000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n#define SIZE 40005\n\nenum Type{\n\tX,\n\tY,\n\tZ,\n};\n\nstruct Info_L{\n\tInfo_L(int arg_value,int arg_index){\n\t\tvalue = arg_value;\n\t\tindex = arg_index;\n\t}\n\tbool operator<(const struct Info_L &arg) const{\n\n\t\treturn value < arg.value; //★昇順★\n\t}\n\tint value,index;\n};\n\nstruct Info_R{\n\tInfo_R(int arg_value,int arg_index){\n\t\tvalue = arg_value;\n\t\tindex = arg_index;\n\t}\n\tbool operator<(const struct Info_R &arg) const{\n\n\t\treturn value > arg.value; //★降順★\n\t}\n\tint value,index;\n};\n\nstruct Data{\n\n\tvoid set(int arg_min_x,int arg_max_x,int arg_min_y,int arg_max_y,int arg_min_z,int arg_max_z){\n\t\tX_L = arg_min_x;\n\t\tX_R = arg_max_x;\n\t\tY_L = arg_min_y;\n\t\tY_R = arg_max_y;\n\t\tZ_L = arg_min_z;\n\t\tZ_R = arg_max_z;\n\t}\n\tint X_L,X_R,Y_L,Y_R,Z_L,Z_R;\n};\n\nint N;\nint can_L[3],can_R[3];\nint next_can_L[3],next_can_R[3];\nint index_L[3],index_R[3];\nint next_L[3],next_R[3];\nbool merged[SIZE];\nvector<Info_L> L[3];\nvector<Info_R> R[3];\nData data[SIZE];\n\nbool is_Cross(int index){\n\n\tif((data[index].X_R < can_L[X]) || (data[index].X_L > can_R[X]) || (data[index].Y_R < can_L[Y])||\n\t\t\t(data[index].Y_L > can_R[Y]) || (data[index].Z_R < can_L[Z]) || (data[index].Z_L > can_R[Z])){\n\n\t\treturn false;\n\t}\n\n\tif((data[index].X_L >= can_L[X]) || (data[index].X_R <= can_R[X]) || (data[index].Y_L >= can_L[Y])||\n\t\t\t(data[index].Y_R <= can_R[Y])||(data[index].Z_L >= can_L[Z])||(data[index].Z_R <= can_R[Z])){\n\n\t\treturn true;\n\t}\n\n\treturn false;\n}\n\nbool is_OK(int tmp_x,int tmp_y){\n\n\tif((tmp_x < can_L[X]) || (tmp_x > can_R[X]) || (tmp_y < can_L[Y])||\n\t\t\t(tmp_y > can_R[Y]) || ((tmp_x+tmp_y) < can_L[Z]) || ((tmp_x+tmp_y) > can_R[Z])){\n\n\t\treturn false;\n\t}\n\n\tif((tmp_x >= can_L[X]) || (tmp_x <= can_R[X]) || (tmp_y >= can_L[Y])||\n\t\t\t(tmp_y <= can_R[Y])||((tmp_x+tmp_y) >= can_L[Z])||((tmp_x+tmp_y) <= can_R[Z])){\n\n\t\treturn true;\n\t}\n\n\treturn false;\n}\n\n\nint main(){\n\n\tscanf(\"%d\",&N);\n\n\tint tmp_x,tmp_y;\n\n\tfor(int loop = 0; loop < N; loop++){\n\n\t\tint min_x = (BIG_NUM+1),max_x = -(BIG_NUM+1);\n\t\tint min_y = (BIG_NUM+1),max_y = -(BIG_NUM+1);\n\t\tint min_z = (BIG_NUM+1),max_z = -(BIG_NUM+1);\n\n\t\tfor(int i = 0; i < 6; i++){\n\n\t\t\tscanf(\"%d %d\",&tmp_x,&tmp_y);\n\n\t\t\tmin_x = min(min_x,tmp_x);\n\t\t\tmax_x = max(max_x,tmp_x);\n\n\t\t\tmin_y = min(min_y,tmp_y);\n\t\t\tmax_y = max(max_y,tmp_y);\n\n\t\t\tmin_z = min(min_z,(tmp_x+tmp_y));\n\t\t\tmax_z = max(max_z,(tmp_x+tmp_y));\n\t\t}\n\t\tdata[loop].set(min_x,max_x,min_y,max_y,min_z,max_z);\n\n\t\tL[X].push_back(Info_L(min_x,loop));\n\t\tL[Y].push_back(Info_L(min_y,loop));\n\t\tL[Z].push_back(Info_L(min_z,loop));\n\n\t\tR[X].push_back(Info_R(max_x,loop));\n\t\tR[Y].push_back(Info_R(max_y,loop));\n\t\tR[Z].push_back(Info_R(max_z,loop));\n\t}\n\n\tfor(int i = 0; i < 3; i++){\n\n\t\tsort(L[i].begin(),L[i].end());\n\t\tsort(R[i].begin(),R[i].end());\n\n\t\tindex_L[i] = 0;\n\t\tindex_R[i] = 0;\n\t}\n\n/*\tfor(int i = 0; i < N; i++){\n\t\tprintf(\"data[%d] X_L:%d X_R:%d\\nY_L:%d Y_R:%d\\nZ_L:%d Z_R:%d\\n\",i,data[i].X_L,data[i].X_R,data[i].Y_L,data[i].Y_R,data[i].Z_L,data[i].Z_R);\n\t}*/\n\n\tfor(int i = 0; i < 3; i++){\n\n\t\tcan_L[i] = 0;\n\t\tcan_R[i] = 0;\n\t}\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tmerged[i] = false;\n\t}\n\n\twhile(true){\n\n\t\tvector<int> vec;\n\n\t\tfor(int i = 0; i < 3; i++){\n\n\t\t\tnext_L[i] = index_L[i];\n\t\t\tnext_R[i] = index_R[i];\n\n\t\t\tnext_can_L[i] = can_L[i];\n\t\t\tnext_can_R[i] = can_R[i];\n\t\t}\n\n\t\t//Lを見る\n\t\tfor(int i = 0; i < 3; i++){\n\t\t\twhile(next_L[i] < N && L[i][next_L[i]].value <= can_R[i])next_L[i]++;\n\n\t\t\tfor(int k = index_L[i]; k < next_L[i]; k++){\n\t\t\t\tif(merged[L[i][k].index])continue;\n\t\t\t\tif(is_Cross(L[i][k].index)){\n\n\t\t\t\t\tvec.push_back(L[i][k].index);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t//Rを見る\n\t\tfor(int i = 0; i < 3; i++){\n\t\t\twhile(next_R[i] < N && R[i][next_R[i]].value >= can_L[i])next_R[i]++;\n\n\t\t\tfor(int k = index_R[i]; k < next_R[i]; k++){\n\t\t\t\tif(merged[R[i][k].index])continue;\n\t\t\t\tif(is_Cross(R[i][k].index)){\n\n\t\t\t\t\tvec.push_back(R[i][k].index);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tif(vec.size() == 0)break;\n\n\t\tsort(vec.begin(),vec.end());\n\t\tvec.erase(unique(vec.begin(),vec.end()),vec.end());\n\n\t\tfor(int i = 0; i < vec.size(); i++){\n\n\t\t\tmerged[vec[i]] = true;\n\n\t\t\tnext_can_L[X] = min(next_can_L[X],data[vec[i]].X_L-1);\n\t\t\tnext_can_R[X] = max(next_can_R[X],data[vec[i]].X_R+1);\n\n\t\t\tnext_can_L[Y] = min(next_can_L[Y],data[vec[i]].Y_L-1);\n\t\t\tnext_can_R[Y] = max(next_can_R[Y],data[vec[i]].Y_R+1);\n\n\t\t\tnext_can_L[Z] = min(next_can_L[Z],data[vec[i]].Z_L-1);\n\t\t\tnext_can_R[Z] = max(next_can_R[Z],data[vec[i]].Z_R+1);\n\t\t}\n\n\t\tfor(int i = 0; i < 3; i++){\n\n\t\t\tindex_L[i] = next_L[i];\n\t\t\tindex_R[i] = next_R[i];\n\n\t\t\tcan_L[i] = next_can_L[i];\n\t\t\tcan_R[i] = next_can_R[i];\n\t\t}\n\t}\n\n\t/*for(int i = 0; i < 3; i++){\n\n\t\tprintf(\"can_L[%d]:%d can_R:%d\\n\",i,can_L[i],can_R[i]);\n\t}*/\n\n\tint num_query;\n\n\tscanf(\"%d\",&num_query);\n\n\tfor(int loop = 0; loop < num_query; loop++){\n\n\t\tscanf(\"%d %d\",&tmp_x,&tmp_y);\n\n\t\tif(is_OK(tmp_x,tmp_y)){\n\n\t\t\tprintf(\"YES\\n\");\n\n\t\t}else{\n\n\t\t\tprintf(\"NO\\n\");\n\t\t}\n\t}\n\n\treturn 0;\n}", "accuracy": 0.56, "time_ms": 30, "memory_kb": 5156, "score_of_the_acc": -0.0014, "final_rank": 8 }, { "submission_id": "aoj_2240_4872081", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 1000000000000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n#define SIZE 40005\n\nenum Type{\n\tX,\n\tY,\n\tZ,\n};\n\nstruct Info_L{\n\tInfo_L(int arg_value,int arg_index){\n\t\tvalue = arg_value;\n\t\tindex = arg_index;\n\t}\n\tbool operator<(const struct Info_L &arg) const{\n\n\t\treturn value < arg.value; //★昇順★\n\t}\n\tint value,index;\n};\n\nstruct Info_R{\n\tInfo_R(int arg_value,int arg_index){\n\t\tvalue = arg_value;\n\t\tindex = arg_index;\n\t}\n\tbool operator<(const struct Info_R &arg) const{\n\n\t\treturn value > arg.value; //★降順★\n\t}\n\tint value,index;\n};\n\nstruct Data{\n\n\tvoid set(int arg_min_x,int arg_max_x,int arg_min_y,int arg_max_y,int arg_min_z,int arg_max_z){\n\t\tX_L = arg_min_x;\n\t\tX_R = arg_max_x;\n\t\tY_L = arg_min_y;\n\t\tY_R = arg_max_y;\n\t\tZ_L = arg_min_z;\n\t\tZ_R = arg_max_z;\n\t}\n\tint X_L,X_R,Y_L,Y_R,Z_L,Z_R;\n};\n\nint N;\nint can_L[3],can_R[3];\nint next_can_L[3],next_can_R[3];\nint index_L[3],index_R[3];\nint next_L[3],next_R[3];\nbool merged[SIZE];\nvector<Info_L> L[3];\nvector<Info_R> R[3];\nData data[SIZE];\n\nbool is_Cross(int index){\n\n\tif((data[index].X_R < can_L[X]) || (data[index].X_L > can_R[X]) || (data[index].Y_R < can_L[Y])||\n\t\t\t(data[index].Y_L > can_R[Y]) || (data[index].Z_R < can_L[Z]) || (data[index].Z_L > can_R[Z])){\n\n\t\treturn false;\n\t}\n\n\tif((data[index].X_L >= can_L[X]) || (data[index].X_R <= can_R[X]) || (data[index].Y_L >= can_L[Y])||\n\t\t\t(data[index].Y_R <= can_R[Y])||(data[index].Z_L >= can_L[Z])||(data[index].Z_R <= can_R[Z])){\n\n\t\treturn true;\n\t}\n\n\treturn false;\n}\n\n\nint main(){\n\n\tscanf(\"%d\",&N);\n\n\tint tmp_x,tmp_y;\n\n\tfor(int loop = 0; loop < N; loop++){\n\n\t\tint min_x = (BIG_NUM+1),max_x = -(BIG_NUM+1);\n\t\tint min_y = (BIG_NUM+1),max_y = -(BIG_NUM+1);\n\t\tint min_z = (BIG_NUM+1),max_z = -(BIG_NUM+1);\n\n\t\tfor(int i = 0; i < 6; i++){\n\n\t\t\tscanf(\"%d %d\",&tmp_x,&tmp_y);\n\n\t\t\tmin_x = min(min_x,tmp_x);\n\t\t\tmax_x = max(max_x,tmp_x);\n\n\t\t\tmin_y = min(min_y,tmp_y);\n\t\t\tmax_y = max(max_y,tmp_y);\n\n\t\t\tmin_z = min(min_z,(tmp_x+tmp_y));\n\t\t\tmax_z = max(max_z,(tmp_x+tmp_y));\n\t\t}\n\t\tdata[loop].set(min_x,max_x,min_y,max_y,min_z,max_z);\n\n\t\tL[X].push_back(Info_L(min_x,loop));\n\t\tL[Y].push_back(Info_L(min_y,loop));\n\t\tL[Z].push_back(Info_L(min_z,loop));\n\n\t\tR[X].push_back(Info_R(max_x,loop));\n\t\tR[Y].push_back(Info_R(max_y,loop));\n\t\tR[Z].push_back(Info_R(max_z,loop));\n\t}\n\n\tfor(int i = 0; i < 3; i++){\n\n\t\tsort(L[i].begin(),L[i].end());\n\t\tsort(R[i].begin(),R[i].end());\n\n\t\tindex_L[i] = 0;\n\t\tindex_R[i] = 0;\n\t}\n\n/*\tfor(int i = 0; i < N; i++){\n\t\tprintf(\"data[%d] X_L:%d X_R:%d\\nY_L:%d Y_R:%d\\nZ_L:%d Z_R:%d\\n\",i,data[i].X_L,data[i].X_R,data[i].Y_L,data[i].Y_R,data[i].Z_L,data[i].Z_R);\n\t}*/\n\n\tfor(int i = 0; i < 3; i++){\n\n\t\tcan_L[i] = 0;\n\t\tcan_R[i] = 0;\n\t}\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tmerged[i] = false;\n\t}\n\n\twhile(true){\n\n\t\tvector<int> vec;\n\n\t\tfor(int i = 0; i < 3; i++){\n\n\t\t\tnext_L[i] = index_L[i];\n\t\t\tnext_R[i] = index_R[i];\n\n\t\t\tnext_can_L[i] = can_L[i];\n\t\t\tnext_can_R[i] = can_R[i];\n\t\t}\n\n\t\t//Lを見る\n\t\tfor(int i = 0; i < 3; i++){\n\t\t\twhile(next_L[i] < N && L[i][next_L[i]].value <= can_R[i])next_L[i]++;\n\n\t\t\tfor(int k = index_L[i]; k < next_L[i]; k++){\n\t\t\t\tif(merged[L[i][k].index])continue;\n\t\t\t\tif(is_Cross(L[i][k].index)){\n\n\t\t\t\t\tvec.push_back(L[i][k].index);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t//Rを見る\n\t\tfor(int i = 0; i < 3; i++){\n\t\t\twhile(next_R[i] < N && R[i][next_R[i]].value >= can_L[i])next_R[i]++;\n\n\t\t\tfor(int k = index_R[i]; k < next_R[i]; k++){\n\t\t\t\tif(merged[R[i][k].index])continue;\n\t\t\t\tif(is_Cross(R[i][k].index)){\n\n\t\t\t\t\tvec.push_back(R[i][k].index);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tif(vec.size() == 0)break;\n\n\t\tsort(vec.begin(),vec.end());\n\t\tvec.erase(unique(vec.begin(),vec.end()),vec.end());\n\n\t\tfor(int i = 0; i < vec.size(); i++){\n\n\t\t\tmerged[vec[i]] = true;\n\n\t\t\tnext_can_L[X] = min(next_can_L[X],data[vec[i]].X_L-1);\n\t\t\tnext_can_R[X] = max(next_can_R[X],data[vec[i]].X_R+1);\n\n\t\t\tnext_can_L[Y] = min(next_can_L[Y],data[vec[i]].Y_L-1);\n\t\t\tnext_can_R[Y] = max(next_can_R[Y],data[vec[i]].Y_R+1);\n\n\t\t\tnext_can_L[Z] = min(next_can_L[Z],data[vec[i]].Z_L-1);\n\t\t\tnext_can_R[Z] = max(next_can_R[Z],data[vec[i]].Z_R+1);\n\t\t}\n\n\t\tfor(int i = 0; i < 3; i++){\n\n\t\t\tindex_L[i] = next_L[i];\n\t\t\tindex_R[i] = next_R[i];\n\n\t\t\tcan_L[i] = next_can_L[i];\n\t\t\tcan_R[i] = next_can_R[i];\n\t\t}\n\t}\n\n\tint num_query;\n\n\tscanf(\"%d\",&num_query);\n\n\tfor(int loop = 0; loop < num_query; loop++){\n\n\t\tscanf(\"%d %d\",&tmp_x,&tmp_y);\n\n\t\tif(tmp_x >= can_L[X] && tmp_x <= can_R[X] && tmp_y >= can_L[Y] &&\n\t\t\t\ttmp_y <= can_R[Y] && (tmp_x+tmp_y) >= can_L[Z] && (tmp_x+tmp_y) <= can_R[Z]){\n\n\t\t\tprintf(\"YES\\n\");\n\n\t\t}else{\n\n\t\t\tprintf(\"NO\\n\");\n\t\t}\n\t}\n\n\treturn 0;\n}", "accuracy": 0.56, "time_ms": 40, "memory_kb": 5136, "score_of_the_acc": -0.0463, "final_rank": 10 }, { "submission_id": "aoj_2240_1449186", "code_snippet": "#include <algorithm>\n#include <climits>\n#include <iostream>\n#include <queue>\n#include <set>\n#include <string>\n#include <vector>\nusing namespace std;\n\ntypedef long long ll;\ntypedef pair<int, int> i_i;\nint MOD = 1000000007;\n\nint main() {\n\tint n; cin >> n;\n\tvector<int> X, Y;\n\twhile (n--) {\n\t\tvector<int> x(6), y(6);\n\t\tfor (int j = 0; j < 6; j++)\n\t\t\tscanf(\"%d%d\", &x[j], &y[j]);\n\t\tfor (int j = 0; j < 6; j++) {\n\t\t\tfor (int k = 0; k < 3; k++) {\n\t\t\t\tX.push_back(x[j]);\n\t\t\t\tY.push_back(y[j]);\n\t\t\t}\n\t\t\tfor (int k = 0; k < 3; k++) {\n\t\t\t\tX.push_back(x[(j + 1) % 6]);\n\t\t\t\tY.push_back(y[(j + 1) % 6]);\n\t\t\t}\n\t\t}\n\t}\n\tn = X.size() / 6;\n\tvector<vector<int> > a(n, vector<int>(6, INT_MIN));\n\tvector<vector<i_i> > p(6, vector<i_i>(n));\n\tint hoge = 0;\n\tfor (int i = 0; i < n; i++) {\n\t\tfor (int j = 0; j < 6; j++) {\n\t\t\tint x = X[hoge], y = Y[hoge]; hoge++;\n\t\t\ta[i][0] = max(a[i][0], x);\n\t\t\ta[i][1] = max(a[i][1], -x);\n\t\t\ta[i][2] = max(a[i][2], y);\n\t\t\ta[i][3] = max(a[i][3], -y);\n\t\t\ta[i][4] = max(a[i][4], x + y);\n\t\t\ta[i][5] = max(a[i][5], -(x + y));\n\t\t}\n\t\tfor (int k = 0; k < 6; k++)\n\t\t\tp[k][i] = i_i(a[i][k], i);\n\t}\n\tfor (int k = 0; k < 6; k++) {\n\t\tsort(p[k].begin(), p[k].end());\n\t\treverse(p[k].begin(), p[k].end());\n\t}\n\tvector<int> index(6), cnt(n);\n\tqueue<int> q;\n\tvector<int> b(6);\n\tfor (;;) {\n\t\tfor (int k = 0; k < 6; k++)\n\t\t\tfor (int& i = index[k]; i < n && -p[k][i].first <= b[k ^ 1]; i++) {\n\t\t\t\tint j = p[k][i].second;\n\t\t\t\tif (++cnt[j] == 6) q.push(j);\n\t\t\t}\n\t\tif (q.empty()) break;\n\t\tint i = q.front(); q.pop();\n\t\tfor (int k = 0; k < 6; k++)\n\t\t\tb[k] = max(b[k], a[i][k] + 1);\n\t}\n\tint k; cin >> k;\n\twhile (k--) {\n\t\tint x, y; scanf(\"%d%d\", &x, &y);\n\t\tif (x <= b[0] && -x <= b[1] && y <= b[2] && -y <= b[3] && x + y <= b[4] && -(x + y) <= b[5])\n\t\t\tcout << \"YES\" << endl;\n\t\telse\n\t\t\tcout << \"NO\" << endl;\n\t}\n}", "accuracy": 1, "time_ms": 250, "memory_kb": 42420, "score_of_the_acc": -2, "final_rank": 6 }, { "submission_id": "aoj_2240_1228683", "code_snippet": "#include<cstdio>\n#include<queue>\n#include<vector>\n#include<utility>\n#include<algorithm>\n\nusing namespace std;\n\ntypedef pair<int,int> P;\n\nint xs[40400][7],ys[40400][7];\n\nvector<P> vals[6];\n\nint N;\nint cnt[40400*6];\n\nqueue<int> que;\nbool used[40400];\n\nint Q;\nint qx[40400],qy[40400];\n\nint main(){\n\tscanf(\"%d\",&N);\n\tbool suc=true;\n\tfor(int i=0;i<N;i++){\n\t\tfor(int j=0;j<6;j++){\n\t\t\tscanf(\"%d%d\",&xs[i][j],&ys[i][j]);\n\t\t}\n\t\txs[i][6]=xs[i][0],ys[i][6]=ys[i][0];\n\t\tfor(int j=0;j<6;j++){\n\t\t\tif(xs[i][j]==0&&xs[i][j+1]==0&&(long long)ys[i][j]*ys[i][j+1]<=0) suc=true;\n\t\t\tif(ys[i][j]==0&&ys[i][j+1]==0&&(long long)xs[i][j]*xs[i][j+1]<=0) suc=true;\n\t\t\tif(xs[i][j]+ys[i][j]==0&&xs[i][j+1]+ys[i][j+1]==0&&\n\t\t\t\t(long long)xs[i][j]*xs[i][j+1]<=0) suc=true;\n\t\t}\n\t}\n\tscanf(\"%d\",&Q);\n\tfor(int i=0;i<Q;i++){\n\t\tscanf(\"%d%d\",qx+i,qy+i);\n\t}\n\tif(suc==false){\n\t\tfor(int i=0;i<Q;i++){\n\t\t\tif(qx[i]==0&&qy[i]==0) printf(\"YES\\n\");\n\t\t\telse printf(\"NO\\n\");\n\t\t}\n\t\treturn 0;\n\t}\n\tfor(int i=0;i<N;i++){\n\t\tfor(int j=0;j<6;j++){\n\t\t\tint x0=xs[i][j],x1=xs[i][j+1];\n\t\t\tint y0=ys[i][j],y1=ys[i][j+1];\n\t\t\tint z0=xs[i][j]+ys[i][j],z1=xs[i][j+1]+ys[i][j+1];\n\t\t\tif(x0>x1) swap(x0,x1);\n\t\t\tif(y0>y1) swap(y0,y1);\n\t\t\tif(z0>z1) swap(z0,z1);\n\t\t\tint tmp[]={x0,-x1,y0,-y1,z0,-z1};\n\t\t\tfor(int k=0;k<6;k++){\n\t\t\t\tvals[k].push_back(P(tmp[k],i*6+j));\n\t\t\t}\n\t\t}\n\t}\n\tfor(int i=0;i<6;i++){\n\t\tsort(vals[i].begin(),vals[i].end());\n\t}\n\tint ids[6]={};\n\tint res[6]={};\n\tfor(int i=0;i<6;i++){\n\t\tfor(ids[i]=0;ids[i]<vals[i].size();ids[i]++){\n\t\t\tif(vals[i][ids[i]].first<=0){\n\t\t\t\tint id=vals[i][ids[i]].second;\n\t\t\t\tcnt[id]++;\n\t\t\t\tif(cnt[id]==6){\n\t\t\t\t\tint id2=id/6;\n\t\t\t\t\tif(used[id2]) continue;\n\t\t\t\t\tused[id2]=true;\n\t\t\t\t\tque.push(id2);\n\t\t\t\t}\n\t\t\t}else{\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n//\tfor(int i=0;i<vals[0].size();i++) printf(\"%d \",vals[0][i]);\n//\tprintf(\"\\n\");\n\twhile(!que.empty()){\n\t\tint id=que.front();\n\t\tque.pop();\n\t\tint tmp[6]={};\n\t\tfor(int j=0;j<6;j++){\n\t\t\ttmp[0]=max(tmp[0],xs[id][j]+1);\n\t\t\ttmp[1]=max(tmp[1],-xs[id][j]+1);\n\t\t\ttmp[2]=max(tmp[2],ys[id][j]+1);\n\t\t\ttmp[3]=max(tmp[3],-ys[id][j]+1);\n\t\t\ttmp[4]=max(tmp[4],xs[id][j]+ys[id][j]+1);\n\t\t\ttmp[5]=max(tmp[5],-(xs[id][j]+ys[id][j])+1);\n\t\t}\n//\t\tprintf(\"id=%d\\n\",id);\n//\t\tprintf(\"tmp={\");\n//\t\tfor(int j=0;j<6;j++){\n//\t\t\tprintf(\"%d,\",tmp[j]);\n//\t\t}\n//\t\tprintf(\"}\\n\");\n\t\tfor(int j=0;j<6;j++){\n\t\t\tres[j]=max(res[j],tmp[j]);\n\t\t}\n\t\tfor(int j=0;j<6;j++){\n//\t\t\tprintf(\"j=%d\\n\",j);\n//\t\t\tprintf(\"ids[j]=%d,vals[j][ids[j]]=%d\\n\",ids[j],vals[j][ids[j]]);\n\t\t\twhile(ids[j]<vals[j].size()&&vals[j][ids[j]].first<=tmp[j]){\n\t\t\t\tint id=vals[j][ids[j]].second;\n//\t\t\t\tprintf(\"%d++\\n\",id);\n\t\t\t\tcnt[id]++;\n\t\t\t\tif(cnt[id]==6){\n\t\t\t\t\tint id2=id/6;\n\t\t\t\t\tif(used[id2]){\n\t\t\t\t\t\tgoto nextloop;\n\t\t\t\t\t}\n\t\t\t\t\tused[id2]=true;\n\t\t\t\t\tque.push(id2);\n\t\t\t\t}\n\t\t\t\tnextloop: ids[j]++;\n\t\t\t}\n\t\t}\n\t}\n/*\tint res[6];\n\tfor(int i=0;i<6;i++){\n\t\tres[i]=vals[i][ids[i]-1].first;\n\t\tif(i%2==1) res[i]*=-1;\n\t}*/\n\tfor(int i=0;i<6;i++){\n\t\tif(i%2==1) res[i]*=-1;\n\t}\n//\tfor(int i=0;i<6;i++) printf(\"%d \",res[i]);\n//\tprintf(\"\\n\");\n//\tfor(int i=6;i<12;i++) printf(\"%d \",cnt[i]);\n//\tprintf(\"\\n\");\n\tfor(int q=0;q<Q;q++){\n\t\tbool ok=true;\n\t\tif((qx[q]<res[1])) ok=false;\n\t\tif((res[0]<qx[q])) ok=false;\n\t\tif((qy[q]<res[3])) ok=false;\n\t\tif((res[2]<qy[q])) ok=false;\n\t\tif((qx[q]+qy[q]<res[5])) ok=false;\n\t\tif((res[4]<qx[q]+qy[q])) ok=false;\n\t\tif(ok) printf(\"YES\\n\");\n\t\telse printf(\"NO\\n\");\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 170, "memory_kb": 16032, "score_of_the_acc": -0.9292, "final_rank": 5 }, { "submission_id": "aoj_2240_1171733", "code_snippet": "#include<stdio.h>\n#include<algorithm>\n#include<queue>\n#include<set>\nusing namespace std;\nint x[41000][6];\nint y[41000][6];\nint z[41000][6];\nint tx[41000];\nint ty[41000];\nint tz[41000];\npair<int,int> ex[81000];\npair<int,int> ey[81000];\npair<int,int> ez[81000];\nint zx[81000];\nint zy[81000];\nint zz[81000];\nint cnt[41000];\nint u[41000];\nint v[41000];\nint main(){\n\tint a;\n\tscanf(\"%d\",&a);\n\tfor(int i=0;i<a;i++){\n\t\tfor(int j=0;j<6;j++)scanf(\"%d%d\",&x[i][j],&y[i][j]);\n\t\tfor(int j=0;j<6;j++)z[i][j]=x[i][j]+y[i][j];\n\t}\n\tfor(int i=0;i<a;i++){\n\t\tzx[i*2]=1010101010;\n\t\tzx[i*2+1]=-1010101010;\n\t\tzy[i*2]=1010101010;\n\t\tzy[i*2+1]=-1010101010;\n\t\tzz[i*2]=1010101010;\n\t\tzz[i*2+1]=-1010101010;\n\t\tfor(int j=0;j<6;j++){\n\t\t\tzx[i*2]=min(zx[i*2],x[i][j]);\n\t\t\tzx[i*2+1]=max(zx[i*2+1],x[i][j]);\n\t\t\tzy[i*2]=min(zy[i*2],y[i][j]);\n\t\t\tzy[i*2+1]=max(zy[i*2+1],y[i][j]);\n\t\t\tzz[i*2]=min(zz[i*2],z[i][j]);\n\t\t\tzz[i*2+1]=max(zz[i*2+1],z[i][j]);\n\t\t}\n\t\tex[i*2]=make_pair(zx[i*2],i*2);\n\t\tex[i*2+1]=make_pair(zx[i*2+1],i*2+1);\n\t\tey[i*2]=make_pair(zy[i*2],i*2);\n\t\tey[i*2+1]=make_pair(zy[i*2+1],i*2+1);\n\t\tez[i*2]=make_pair(zz[i*2],i*2);\n\t\tez[i*2+1]=make_pair(zz[i*2+1],i*2+1);\n\t\t\n\t}\n\tfor(int i=0;i<a;i++){\n\t\tif(zx[i*2+1]>=0)cnt[i]++;\n\t\tif(zx[i*2]<=0)cnt[i]++;\n\t\tif(zy[i*2+1]>=0)cnt[i]++;\n\t\tif(zy[i*2]<=0)cnt[i]++;\n\t\tif(zz[i*2+1]>=0)cnt[i]++;\n\t\tif(zz[i*2]<=0)cnt[i]++;\n\t}\n\tfor(int i=0;i<a;i++){\n\t\tif(zx[i*2+1]<=0)u[i]++;\n\t\tif(zx[i*2]>=0)u[i]++;\n\t\tif(zy[i*2+1]<=0)u[i]++;\n\t\tif(zy[i*2]>=0)u[i]++;\n\t\tif(zz[i*2+1]<=0)u[i]++;\n\t\tif(zz[i*2]>=0)u[i]++;\n\t}\n\tqueue<int>Q;\n\tfor(int i=0;i<a;i++){\n\t\tif(cnt[i]==6&&u[i]){\n\t\t\tv[i]=1;\n\t\t\tQ.push(i);\n\t\t}\n\t}\n\tstd::sort(ex,ex+a*2);\n\tstd::sort(ey,ey+a*2);\n\tstd::sort(ez,ez+a*2);\n\tint b;\n\tscanf(\"%d\",&b);\n\tfor(int i=0;i<b;i++){\n\t\tscanf(\"%d%d\",tx+i,ty+i);\n\t\ttz[i]=tx[i]+ty[i];\n\t}\n\tint xl=0;\n\tint xr=0;\n\tint yl=0;\n\tint yr=0;\n\tint zl=0;\n\tint zr=0;\n\twhile(1){\n\t\tint XL=xl;\n\t\tint XR=xr;\n\t\tint YL=yl;\n\t\tint YR=yr;\n\t\tint ZL=zl;\n\t\tint ZR=zr;\n\t\twhile(Q.size()){\n\t\t\tint at=Q.front();\n\t\t\tQ.pop();\n\t\t\tXL=min(XL,zx[at*2]-1);\n\t\t\tXR=max(XR,zx[at*2+1]+1);\n\t\t\tYL=min(YL,zy[at*2]-1);\n\t\t\tYR=max(YR,zy[at*2+1]+1);\n\t\t\tZL=min(ZL,zz[at*2]-1);\n\t\t\tZR=max(ZR,zz[at*2+1]+1);\n\t\t}\n\t//\tprintf(\"%d %d %d %d %d %d\\n\",XL,XR,YL,YR,ZL,ZR);\n\t\tif(XL==xl&&XR==xr&&YL==yl&&YR==yr&&ZL==zl&&ZR==zr)break;\n\t\tint L=lower_bound(ex,ex+a*2,make_pair(XL,0))-ex;\n\t\tint R=lower_bound(ex,ex+a*2,make_pair(xl,0))-ex;\n\t\tfor(int i=L;i<R;i++){\n\t\t\tint at=ex[i].second;\n\t\t\tif(at%2)cnt[at/2]++;\n\t\t\telse u[at/2]++;\n\t\t\tif(!v[at/2]&&u[at/2]&&cnt[at/2]==6){\n\t\t\t\tv[at/2]=1;\n\t\t\t\tQ.push(at/2);\n\t\t\t}\n\t\t}\n\t\tL=lower_bound(ex,ex+a*2,make_pair(xr,99999999))-ex;\n\t\tR=lower_bound(ex,ex+a*2,make_pair(XR,99999999))-ex;\n\t\tfor(int i=L;i<R;i++){\n\t\t\tint at=ex[i].second;\n\t\t\tif(at%2)u[at/2]++;\n\t\t\telse cnt[at/2]++;\n\t\t\tif(!v[at/2]&&u[at/2]&&cnt[at/2]==6){\n\t\t\t\tv[at/2]=1;\n\t\t\t\tQ.push(at/2);\n\t\t\t}\n\t\t}\n\t\tL=lower_bound(ey,ey+a*2,make_pair(YL,0))-ey;\n\t\tR=lower_bound(ey,ey+a*2,make_pair(yl,0))-ey;\n\t\tfor(int i=L;i<R;i++){\n\t\t\tint at=ey[i].second;\n\t\t\tif(at%2)cnt[at/2]++;\n\t\t\telse u[at/2]++;\n\t\t\tif(!v[at/2]&&u[at/2]&&cnt[at/2]==6){\n\t\t\t\tv[at/2]=1;\n\t\t\t\tQ.push(at/2);\n\t\t\t}\n\t\t}\n\t\tL=lower_bound(ey,ey+a*2,make_pair(yr,99999999))-ey;\n\t\tR=lower_bound(ey,ey+a*2,make_pair(YR,99999999))-ey;\n\t\tfor(int i=L;i<R;i++){\n\t\t\tint at=ey[i].second;\n\t\t\tif(at%2)u[at/2]++;\n\t\t\telse cnt[at/2]++;\n\t\t\tif(!v[at/2]&&u[at/2]&&cnt[at/2]==6){\n\t\t\t\tv[at/2]=1;\n\t\t\t\tQ.push(at/2);\n\t\t\t}\n\t\t}\n\t\tL=lower_bound(ez,ez+a*2,make_pair(ZL,0))-ez;\n\t\tR=lower_bound(ez,ez+a*2,make_pair(zl,0))-ez;\n\t\tfor(int i=L;i<R;i++){\n\t\t\tint at=ez[i].second;\n\t\t\tif(at%2)cnt[at/2]++;\n\t\t\telse u[at/2]++;\n\t\t\tif(!v[at/2]&&u[at/2]&&cnt[at/2]==6){\n\t\t\t\tv[at/2]=1;\n\t\t\t\tQ.push(at/2);\n\t\t\t}\n\t\t}\n\t\tL=lower_bound(ez,ez+a*2,make_pair(zr,99999999))-ez;\n\t\tR=lower_bound(ez,ez+a*2,make_pair(ZR,99999999))-ez;\n\t\tfor(int i=L;i<R;i++){\n\t\t\tint at=ez[i].second;\n\t\t\tif(at%2)u[at/2]++;\n\t\t\telse cnt[at/2]++;\n\t\t\tif(!v[at/2]&&u[at/2]&&cnt[at/2]==6){\n\t\t\t\tv[at/2]=1;\n\t\t\t\tQ.push(at/2);\n\t\t\t}\n\t\t}\n\t\txl=XL;xr=XR;yl=YL;yr=YR;zl=ZL;zr=ZR;\n\t}\n\tfor(int i=0;i<b;i++){\n\t\tif(xl<=tx[i]&&tx[i]<=xr&&yl<=ty[i]&&ty[i]<=yr&&zl<=tz[i]&&tz[i]<=zr)printf(\"YES\\n\");\n\t\telse printf(\"NO\\n\");\n\t}\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 7708, "score_of_the_acc": -0.388, "final_rank": 3 }, { "submission_id": "aoj_2240_337071", "code_snippet": "#include <stdio.h>\n#include <string.h>\n#include <algorithm>\n#include <iostream>\n#include <math.h>\n#include <assert.h>\n#include <vector>\n#include <queue>\n#include <string>\n#include <map>\n#include <set>\n\nusing namespace std;\ntypedef long long ll;\ntypedef unsigned long long ull;\nstatic const double EPS = 1e-9;\nstatic const double PI = acos(-1.0);\n\n#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)\n#define FOR(i, s, n) for (ll i = (s); i < (ll)(n); i++)\n#define FOREQ(i, s, n) for (ll i = (s); i <= (ll)(n); i++)\n#define FORIT(it, c) for (__typeof((c).begin())it = (c).begin(); it != (c).end(); it++)\n#define MEMSET(v, h) memset((v), h, sizeof(v))\n\nstruct Hexagon {\n ll vs[3][2];\n Hexagon() {;}\n};\n\nHexagon S;\nHexagon territory[40010];\ndeque<pair<ll, ll> > dists1[3][2];\ndeque<pair<ll, ll> > dists2[3][2];\nll state[40010];\n\nvoid Expand(ll index) {\n Hexagon s = territory[index];\n REP(k, 3) {\n s.vs[k][0]--;\n s.vs[k][1]++;\n }\n REP(k, 3) REP(type, 2) {\n S.vs[k][0] = min(S.vs[k][0], s.vs[k][type]);\n S.vs[k][1] = max(S.vs[k][1], s.vs[k][type]);\n }\n}\n\nll Check() {\n ll ret = 0;\n REP(k, 3) {\n REP(type, 2) {\n while (!dists1[k][type].empty()) {\n ll d = S.vs[k][type ^ 1];\n if (type == 0) {\n if (dists1[k][0].front().first > d) { break; }\n } else {\n if (d > -dists1[k][1].front().first) { break; }\n }\n ll index = dists1[k][type].front().second;\n state[index] |= 1 << (k * 2 + type);\n //cout << index << \" \" << k * 2 + type << endl;\n //cout << state[index] << \" \" << (1 << 7) - 1 << endl;\n if (state[index] == (1 << 7) - 1) {\n state[index] = 0;\n Expand(index);\n ret++;\n }\n dists1[k][type].pop_front();\n }\n while (!dists2[k][type].empty()) {\n ll d = S.vs[k][type];\n if (type == 0) {\n if (-dists2[k][type].front().first < d) { break; }\n } else {\n if (d < dists2[k][type].front().first) { break; }\n }\n ll index = dists2[k][type].front().second;\n //cout << index << endl;\n state[index] |= 1 << 6;\n if (state[index] == (1 << 7) - 1) {\n state[index] = 0;\n Expand(index);\n ret++;\n }\n dists2[k][type].pop_front();\n }\n }\n }\n return ret;\n}\n\nll n, q;\nint main() {\n while (scanf(\"%lld\", &n) > 0) {\n MEMSET(state, 0);\n REP(k, 3) REP(type, 2) {\n dists1[k][type].clear();\n dists2[k][type].clear();\n }\n REP(i, n) {\n Hexagon hex;\n REP(j, 3) {\n hex.vs[j][0] = 1LL << 60;\n hex.vs[j][1] = -(1LL << 60);\n }\n REP(j, 6) {\n ll x, y;\n scanf(\"%lld %lld\", &x, &y);\n ll z = x + y;\n ll v[3] = { x, y, z };\n REP(k, 3) {\n hex.vs[k][0] = min(hex.vs[k][0], v[k]);\n hex.vs[k][1] = max(hex.vs[k][1], v[k]);\n }\n }\n territory[i] = hex;\n REP(k, 3) {\n dists1[k][0].push_back(make_pair(hex.vs[k][0], i));\n dists1[k][1].push_back(make_pair(-hex.vs[k][1], i));\n dists2[k][0].push_back(make_pair(-hex.vs[k][0], i));\n dists2[k][1].push_back(make_pair(hex.vs[k][1], i));\n }\n }\n REP(k, 3) REP(type, 2) {\n sort(dists1[k][type].begin(), dists1[k][type].end());\n sort(dists2[k][type].begin(), dists2[k][type].end());\n }\n REP(k, 3) REP(l, 2) { S.vs[k][l] = 0; }\n while (Check()) {;}\n // REP(k, 3) {\n // cout << S.vs[k][0] << \" \" << S.vs[k][1] << endl;\n // }\n scanf(\"%lld\", &q);\n REP(i, q) {\n ll x, y;\n scanf(\"%lld %lld\", &x, &y);\n ll z = x + y;\n if (S.vs[0][0] <= x && x <= S.vs[0][1] &&\n S.vs[1][0] <= y && y <= S.vs[1][1] &&\n S.vs[2][0] <= z && z <= S.vs[2][1]) {\n puts(\"YES\");\n } else {\n puts(\"NO\");\n }\n }\n }\n}", "accuracy": 1, "time_ms": 190, "memory_kb": 10000, "score_of_the_acc": -0.8585, "final_rank": 4 } ]
aoj_2241_cpp
Problem J: Usaneko Matrix うさぎとねこが勝負をしている. ルールは以下の通りである. まず2 匹はそれぞれ n 行 n 列の正方形状に n 2 個の整数を紙に書き, トランプを1 枚ずつ引く. 次に, 1 から 1 000 000 までの数が1 つずつ書かれた1 000 000 枚のカードを2 匹でシャッフルし, これを1 枚ずつ交互に引いていく. 2 匹はカードが引かれるたび, カードと同じ数が自分の紙に書かれていたらそれに印をつける. 「印がついた n 個の数の組であって, 一直線上に並んでいるもの」の個数が, はじめに引いたトランプの数以上になることを勝利条件とする. 与えられた m 枚目のカードまでで, うさぎとねこのどちらが勝つか答えよ. ただし勝敗は, あるカードが引かれて印をつけ終わった段階で2 匹のうち片方のみが勝利条件をみたしたときに決まるものとし, それ以外の場合は引き分けとする. いずれかが勝利条件をみたした後でもカードが引かれることはあるが, これは勝敗に影響しない. Input 1 行目:“ n u v m ” (正方形のサイズ, うさぎのトランプの数, ねこのトランプの数, 引かれるカードの枚数) 2-( N + 1) 行目:うさぎが紙に書く n 2 個の数( N + 2)-(2 N + 1) 行目:ねこが紙に書く n 2 個の数(2 N + 2)-(2 N + M + 1) 行目:引かれるm 枚のカード 1 ≤ n ≤ 500 1 ≤ u , v ≤ 13 1 ≤ m ≤ 100 000 1 ≤ (書かれる数) ≤ 1 000 000 うさぎが紙に書く n 2 個の数, ねこが紙に書く n 2 個の数, 引かれる m 枚のカードに書かれた数はそれぞれの中で異なる. Output うさぎが勝つ場合には”USAGI”を, ねこが勝つ場合には”NEKO”を, 引き分けならば”DRAW”, それぞれ 一行に出力せよ. Sample Input 1 3 2 2 10 1 2 3 4 5 6 7 8 9 1 2 3 6 5 4 7 8 9 11 4 7 5 10 9 2 1 3 8 Sample Output 1 USAGI Sample Input 2 3 2 1 10 1 2 3 4 5 6 7 8 9 1 2 3 6 5 4 7 8 9 11 4 7 5 10 9 2 1 3 8 Sample Output 2 DRAW
[ { "submission_id": "aoj_2241_10850418", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define REP(i, n) for (int i = 0; i < (n); i++)\n#define int long long\n#define FOR(i, m, n) for (int i = (m); i < (n); i++)\n#define all(x) x.begin(), x.end()\nusing pii = pair<int, int>;\n\nint A[505][505], B[505][505];\nint rax[505], ray[505], rbx[505], rby[505];\nvoid solve() {\n int N, U, V, M;\n cin >> N >> U >> V >> M;\n map<int, pii> mpA, mpB;\n int ra1 = N, ra2 = N, rb1 = N, rb2 = N;\n REP(i, N) { rax[i] = ray[i] = rbx[i] = rby[i] = N; }\n REP(i, N) {\n REP(j, N) {\n cin >> A[i][j];\n mpA[A[i][j]] = {i, j};\n }\n }\n REP(i, N) REP(j, N) {\n cin >> B[i][j];\n mpB[B[i][j]] = {i, j};\n }\n bool done = false;\n int result = 0;\n int cntA = 0, cntB = 0;\n if (N == 1) {\n REP(i, M) {\n int x;\n cin >> x;\n if (done) continue;\n if (mpA.count(x)) {\n pii p = mpA[x];\n // cerr << \"A:\" << p.first << \" \" << p.second << endl;\n rax[p.first]--;\n ray[p.second]--;\n if (p.first == p.second)\n ra1--;\n else if (p.first == N - p.second - 1)\n ra2--;\n if (ra1 == 0) {\n // cerr << \"ra1\" << endl;\n ra1--;\n cntA++;\n }\n if (ra2 == 0) {\n // cerr << \"ra2\" << endl;\n ra2--;\n cntA++;\n }\n if (rax[p.first] == 0) cntA++;\n if (ray[p.second] == 0) cntA++;\n }\n if (mpB.count(x)) {\n pii p = mpB[x];\n // cerr << \"B:\" << p.first << \" \" << p.second << endl;\n rbx[p.first]--;\n rby[p.second]--;\n if (p.first == p.second)\n rb1--;\n else if (p.first == N - p.second - 1)\n rb2--;\n if (rb1 == 0) {\n rb1--;\n cntB++;\n }\n if (rb2 == 0) {\n rb2--;\n cntB++;\n }\n if (rbx[p.first] == 0) cntB++;\n if (rby[p.second] == 0) cntB++;\n }\n\n // cerr << \"cntA:\" << cntA << endl;\n // cerr << \"cntB:\" << cntB << endl;\n if (cntA) cntA = 1;\n if (cntB) cntB = 1;\n if (cntA >= U && cntB >= V) {\n done = true;\n } else if (cntA >= U) {\n done = true;\n result = 1;\n } else if (cntB >= V) {\n done = true;\n result = 2;\n }\n }\n if (result == 0)\n cout << \"DRAW\";\n else if (result == 1)\n cout << \"USAGI\";\n else\n cout << \"NEKO\";\n cout << endl;\n } else {\n REP(i, M) {\n int x;\n cin >> x;\n if (done) continue;\n if (mpA.count(x)) {\n pii p = mpA[x];\n // cerr << \"A:\" << p.first << \" \" << p.second << endl;\n rax[p.first]--;\n ray[p.second]--;\n if (p.first == p.second) ra1--;\n if (p.first == N - p.second - 1) ra2--;\n if (ra1 == 0) {\n // cerr << \"ra1\" << endl;\n ra1--;\n cntA++;\n }\n if (ra2 == 0) {\n // cerr << \"ra2\" << endl;\n ra2--;\n cntA++;\n }\n if (rax[p.first] == 0) cntA++;\n if (ray[p.second] == 0) cntA++;\n }\n if (mpB.count(x)) {\n pii p = mpB[x];\n // cerr << \"B:\" << p.first << \" \" << p.second << endl;\n rbx[p.first]--;\n rby[p.second]--;\n if (p.first == p.second) rb1--;\n if (p.first == N - p.second - 1) rb2--;\n if (rb1 == 0) {\n rb1--;\n cntB++;\n }\n if (rb2 == 0) {\n rb2--;\n cntB++;\n }\n if (rbx[p.first] == 0) cntB++;\n if (rby[p.second] == 0) cntB++;\n }\n\n // cerr << \"cntA:\" << cntA << endl;\n // cerr << \"cntB:\" << cntB << endl;\n // if (cntA >= U || cntB >= V) {\n // cerr << \"cntA:\" << cntA << endl;\n // cerr << \"cntB:\" << cntB << endl;\n // }\n if (cntA >= U && cntB >= V) {\n done = true;\n } else if (cntA >= U) {\n done = true;\n result = 1;\n } else if (cntB >= V) {\n done = true;\n result = 2;\n }\n }\n if (result == 0)\n cout << \"DRAW\";\n else if (result == 1)\n cout << \"USAGI\";\n else\n cout << \"NEKO\";\n cout << endl;\n }\n}\n\nsigned main() {\n // while (solve())\n solve();\n}", "accuracy": 1, "time_ms": 130, "memory_kb": 24408, "score_of_the_acc": -0.9489, "final_rank": 14 }, { "submission_id": "aoj_2241_9635207", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n/**\n * @brief template\n */\n\nint main() {\n int N, U, V, M;\n cin >> N >> U >> V >> M;\n vector<vector<int>> A(N,vector<int>(N));\n vector<vector<int>> B(N,vector<int>(N));\n rep(i,0,N) rep(j,0,N) cin >> A[i][j];\n rep(i,0,N) rep(j,0,N) cin >> B[i][j];\n map<int,vector<int>> mpA, mpB;\n if (N == 1) {\n vector<int> V = {0};\n mpA[A[0][0]] = mpB[B[0][0]] = V;\n }\n else {\n rep(i,0,N) {\n rep(j,0,N) {\n vector<int> V;\n V.push_back(i), V.push_back(N+j);\n if (i==j) V.push_back(N*2);\n if (i+j==N-1) V.push_back(N*2+1);\n mpA[A[i][j]] = V;\n mpB[B[i][j]] = V;\n }\n }\n }\n vector<int> CA(N*2+2,0), CB(N*2+2,0);\n int DA = 0, DB = 0;\n rep(_,0,M) {\n int C;\n cin >> C;\n if (mpA.count(C)) {\n for (int i : mpA[C]) {\n CA[i]++;\n if (CA[i] == N) DA++;\n }\n }\n if (mpB.count(C)) {\n for (int i : mpB[C]) {\n CB[i]++;\n if (CB[i] == N) DB++;\n }\n }\n if (DA >= U && DB >= V) {\n cout << \"DRAW\" << endl;\n return 0;\n }\n if (DA >= U) {\n cout << \"USAGI\" << endl;\n return 0;\n }\n if (DB >= V) {\n cout << \"NEKO\" << endl;\n return 0;\n }\n }\n cout << \"DRAW\" << endl;\n}", "accuracy": 1, "time_ms": 220, "memory_kb": 35928, "score_of_the_acc": -1.6921, "final_rank": 17 }, { "submission_id": "aoj_2241_8006780", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing point = pair<short, short>;\n\nint main() {\n int N, U, V, M;\n cin >> N >> U >> V >> M;\n\n vector A(N, vector(N, true));\n vector B(N, vector(N, true));\n\n unordered_map<int, point> Amat, Bmat;\n for (short i = 0 ; i < N ; i++) {\n for (short j = 0 ; j < N ; j++) {\n int v; cin >> v;\n Amat[v] = point(i, j);\n }\n }\n for (short i = 0 ; i < N ; i++) {\n for (short j = 0 ; j < N ; j++) {\n int v; cin >> v;\n Bmat[v] = point(i, j);\n }\n }\n\n auto f = [&](vector<vector<bool>>& table, unordered_map<int, point>& mp, int del) -> int {\n if (!mp.count(del)) return 0;\n auto [y, x] = mp[del];\n table[y][x] = false;\n int res = 0;\n // yoko\n bool yoko = true;\n for (int i = 0 ; i < N ; i++) {\n yoko &= !table[y][i];\n }\n res += yoko;\n // tate\n bool tate = N > 1;\n for (int i = 0 ; i < N ; i++) {\n tate &= !table[i][x];\n }\n res += tate;\n if (y == x) {\n bool naname = N > 1;\n for (int i = 0 ; i < N ; i++) {\n naname &= !table[i][i];\n }\n res += naname;\n }\n if (x + y == N - 1) {\n bool naname = N > 1;\n for (int i = 0 ; i < N ; i++) {\n naname &= !table[i][N - i - 1];\n }\n res += naname;\n }\n return res;\n };\n\n int nowa = 0, nowb = 0;\n for (int _ = 0 ; _ < M ; _++) {\n int v; cin >> v;\n nowa += f(A, Amat, v);\n nowb += f(B, Bmat, v);\n\n bool awin = nowa >= U;\n bool bwin = nowb >= V;\n if (awin and bwin) {\n cout << \"DRAW\" << endl;\n return 0;\n }\n else if (awin) {\n cout << \"USAGI\" << endl;\n return 0;\n }\n else if (bwin) {\n cout << \"NEKO\" << endl;\n return 0;\n }\n }\n cout << \"DRAW\" << endl;\n}", "accuracy": 1, "time_ms": 110, "memory_kb": 15216, "score_of_the_acc": -0.5409, "final_rank": 7 }, { "submission_id": "aoj_2241_6945410", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing pii = pair<int, int>;\n\nvoid fast_io() {\n cin.tie(0)->sync_with_stdio(0);\n}\n\nconst int MAX = 1000, MAXV = 1e6;\nint rno_u[MAXV + 1], cno_u[MAXV + 1];\nint rno_v[MAXV + 1], cno_v[MAXV + 1];\nint rcnt_u[MAX], rcnt_v[MAX];\nint ccnt_u[MAX], ccnt_v[MAX];\nint diag_u_l = 0, diag_u_r = 0, diag_v_l = 0, diag_v_r = 0;\n\nvoid solve() {\n int n, u, v, m;\n cin >> n >> u >> v >> m;\n\n memset(rno_u, -1, sizeof(rno_u));\n memset(cno_u, -1, sizeof(cno_u));\n memset(rno_v, -1, sizeof(rno_v));\n memset(cno_v, -1, sizeof(cno_v));\n\n for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) {\n int x;\n cin >> x;\n rno_u[x] = i;\n cno_u[x] = j;\n }\n for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) {\n int x;\n cin >> x;\n rno_v[x] = i;\n cno_v[x] = j;\n }\n\n // assert(n > 1);\n\n int ucnt = 0, vcnt = 0;\n while (m--) {\n int k;\n cin >> k;\n\n if (rno_u[k] != -1) {\n int i = rno_u[k], j = cno_u[k];\n\n rcnt_u[i]++;\n ccnt_u[j]++;\n if (i == j) diag_u_l++;\n if (i + j == n - 1) diag_u_r++;\n\n if (rcnt_u[i] == n) ucnt++, rcnt_u[i] = 0;\n if (ccnt_u[j] == n) ucnt++, ccnt_u[j] = 0;\n if (diag_u_l == n) ucnt++, diag_u_l = 0;\n if (diag_u_r == n) ucnt++, diag_u_r = 0;\n }\n if (rno_v[k] != -1) {\n int i = rno_v[k], j = cno_v[k];\n\n rcnt_v[i]++;\n ccnt_v[j]++;\n if (i == j) diag_v_l++;\n if (i + j == n - 1) diag_v_r++;\n\n if (rcnt_v[i] == n) vcnt++, rcnt_v[i] = 0;\n if (ccnt_v[j] == n) vcnt++, ccnt_v[j] = 0;\n if (diag_v_l == n) vcnt++, diag_v_l = 0;\n if (diag_v_r == n) vcnt++, diag_v_r = 0;\n }\n\n if (n == 1) {\n ucnt = min(ucnt, 1);\n vcnt = min(vcnt, 1);\n }\n\n if (ucnt >= u && vcnt < v) return cout << \"USAGI\\n\", void();\n if (vcnt >= v && ucnt < u) return cout << \"NEKO\\n\", void();\n if (ucnt >= u && vcnt >= v) break;\n }\n\n cout << \"DRAW\\n\";\n}\n\nint main() {\n fast_io();\n\n int t = 1;\n // cin >> t;\n while (t--) solve();\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 19072, "score_of_the_acc": -0.325, "final_rank": 3 }, { "submission_id": "aoj_2241_6761118", "code_snippet": "#include <bits/stdc++.h>\n\n#ifdef _MSC_VER\n# include <intrin.h>\n#else\n# include <x86intrin.h>\n#endif\n\n#include <limits>\n#include <type_traits>\n\nnamespace suisen {\n// ! utility\ntemplate <typename ...Types>\nusing constraints_t = std::enable_if_t<std::conjunction_v<Types...>, std::nullptr_t>;\ntemplate <bool cond_v, typename Then, typename OrElse>\nconstexpr decltype(auto) constexpr_if(Then&& then, OrElse&& or_else) {\n if constexpr (cond_v) {\n return std::forward<Then>(then);\n } else {\n return std::forward<OrElse>(or_else);\n }\n}\n\n// ! function\ntemplate <typename ReturnType, typename Callable, typename ...Args>\nusing is_same_as_invoke_result = std::is_same<std::invoke_result_t<Callable, Args...>, ReturnType>;\ntemplate <typename F, typename T>\nusing is_uni_op = is_same_as_invoke_result<T, F, T>;\ntemplate <typename F, typename T>\nusing is_bin_op = is_same_as_invoke_result<T, F, T, T>;\n\ntemplate <typename Comparator, typename T>\nusing is_comparator = std::is_same<std::invoke_result_t<Comparator, T, T>, bool>;\n\n// ! integral\ntemplate <typename T, typename = constraints_t<std::is_integral<T>>>\nconstexpr int bit_num = std::numeric_limits<std::make_unsigned_t<T>>::digits;\ntemplate <typename T, unsigned int n>\nstruct is_nbit { static constexpr bool value = bit_num<T> == n; };\ntemplate <typename T, unsigned int n>\nstatic constexpr bool is_nbit_v = is_nbit<T, n>::value;\n\n// ?\ntemplate <typename T>\nstruct safely_multipliable {};\ntemplate <>\nstruct safely_multipliable<int> { using type = long long; };\ntemplate <>\nstruct safely_multipliable<long long> { using type = __int128_t; };\ntemplate <>\nstruct safely_multipliable<unsigned int> { using type = unsigned long long; };\ntemplate <>\nstruct safely_multipliable<unsigned long int> { using type = __uint128_t; };\ntemplate <>\nstruct safely_multipliable<unsigned long long> { using type = __uint128_t; };\ntemplate <>\nstruct safely_multipliable<float> { using type = float; };\ntemplate <>\nstruct safely_multipliable<double> { using type = double; };\ntemplate <>\nstruct safely_multipliable<long double> { using type = long double; };\ntemplate <typename T>\nusing safely_multipliable_t = typename safely_multipliable<T>::type;\n\ntemplate <typename T, typename = void>\nstruct rec_value_type {\n using type = T;\n};\ntemplate <typename T>\nstruct rec_value_type<T, std::void_t<typename T::value_type>> {\n using type = typename rec_value_type<typename T::value_type>::type;\n};\ntemplate <typename T>\nusing rec_value_type_t = typename rec_value_type<T>::type;\n\n} // namespace suisen\n\n// ! type aliases\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\n\ntemplate <typename T>\nusing pq_greater = std::priority_queue<T, std::vector<T>, std::greater<T>>;\ntemplate <typename T, typename U>\nusing umap = std::unordered_map<T, U>;\n\n// ! macros (capital: internal macro)\n#define OVERLOAD2(_1,_2,name,...) name\n#define OVERLOAD3(_1,_2,_3,name,...) name\n#define OVERLOAD4(_1,_2,_3,_4,name,...) name\n\n#define REP4(i,l,r,s) for(std::remove_reference_t<std::remove_const_t<decltype(r)>>i=(l);i<(r);i+=(s))\n#define REP3(i,l,r) REP4(i,l,r,1)\n#define REP2(i,n) REP3(i,0,n)\n#define REPINF3(i,l,s) for(std::remove_reference_t<std::remove_const_t<decltype(l)>>i=(l);;i+=(s))\n#define REPINF2(i,l) REPINF3(i,l,1)\n#define REPINF1(i) REPINF2(i,0)\n#define RREP4(i,l,r,s) for(std::remove_reference_t<std::remove_const_t<decltype(r)>>i=(l)+fld((r)-(l)-1,s)*(s);i>=(l);i-=(s))\n#define RREP3(i,l,r) RREP4(i,l,r,1)\n#define RREP2(i,n) RREP3(i,0,n)\n\n#define rep(...) OVERLOAD4(__VA_ARGS__, REP4 , REP3 , REP2 )(__VA_ARGS__)\n#define rrep(...) OVERLOAD4(__VA_ARGS__, RREP4 , RREP3 , RREP2 )(__VA_ARGS__)\n#define repinf(...) OVERLOAD3(__VA_ARGS__, REPINF3, REPINF2, REPINF1)(__VA_ARGS__)\n\n#define CAT_I(a, b) a##b\n#define CAT(a, b) CAT_I(a, b)\n#define UNIQVAR(tag) CAT(tag, __LINE__)\n#define loop(n) for (std::remove_reference_t<std::remove_const_t<decltype(n)>> UNIQVAR(loop_variable) = n; UNIQVAR(loop_variable) --> 0;)\n\n#define all(iterable) std::begin(iterable), std::end(iterable)\n#define input(type, ...) type __VA_ARGS__; read(__VA_ARGS__)\n\n#ifdef LOCAL\n# define debug(...) debug_internal(#__VA_ARGS__, __VA_ARGS__)\n\ntemplate <class T, class... Args>\nvoid debug_internal(const char* s, T&& first, Args&&... args) {\n constexpr const char* prefix = \"[\\033[32mDEBUG\\033[m] \";\n constexpr const char* open_brakets = sizeof...(args) == 0 ? \"\" : \"(\";\n constexpr const char* close_brakets = sizeof...(args) == 0 ? \"\" : \")\";\n std::cerr << prefix << open_brakets << s << close_brakets << \": \" << open_brakets << std::forward<T>(first);\n ((std::cerr << \", \" << std::forward<Args>(args)), ...);\n std::cerr << close_brakets << \"\\n\";\n}\n\n#else\n# define debug(...) void(0)\n#endif\n\n// ! I/O utilities\n\n// __int128_t\nstd::ostream& operator<<(std::ostream& dest, __int128_t value) {\n std::ostream::sentry s(dest);\n if (s) {\n __uint128_t tmp = value < 0 ? -value : value;\n char buffer[128];\n char* d = std::end(buffer);\n do {\n --d;\n *d = \"0123456789\"[tmp % 10];\n tmp /= 10;\n } while (tmp != 0);\n if (value < 0) {\n --d;\n *d = '-';\n }\n int len = std::end(buffer) - d;\n if (dest.rdbuf()->sputn(d, len) != len) {\n dest.setstate(std::ios_base::badbit);\n }\n }\n return dest;\n}\n// __uint128_t\nstd::ostream& operator<<(std::ostream& dest, __uint128_t value) {\n std::ostream::sentry s(dest);\n if (s) {\n char buffer[128];\n char* d = std::end(buffer);\n do {\n --d;\n *d = \"0123456789\"[value % 10];\n value /= 10;\n } while (value != 0);\n int len = std::end(buffer) - d;\n if (dest.rdbuf()->sputn(d, len) != len) {\n dest.setstate(std::ios_base::badbit);\n }\n }\n return dest;\n}\n\n// pair\ntemplate <typename T, typename U>\nstd::ostream& operator<<(std::ostream& out, const std::pair<T, U>& a) {\n return out << a.first << ' ' << a.second;\n}\n// tuple\ntemplate <unsigned int N = 0, typename ...Args>\nstd::ostream& operator<<(std::ostream& out, const std::tuple<Args...>& a) {\n if constexpr (N >= std::tuple_size_v<std::tuple<Args...>>) {\n return out;\n } else {\n out << std::get<N>(a);\n if constexpr (N + 1 < std::tuple_size_v<std::tuple<Args...>>) {\n out << ' ';\n }\n return operator<<<N + 1>(out, a);\n }\n}\n// vector\ntemplate <typename T>\nstd::ostream& operator<<(std::ostream& out, const std::vector<T>& a) {\n for (auto it = a.begin(); it != a.end();) {\n out << *it;\n if (++it != a.end()) out << ' ';\n }\n return out;\n}\n// array\ntemplate <typename T, size_t N>\nstd::ostream& operator<<(std::ostream& out, const std::array<T, N>& a) {\n for (auto it = a.begin(); it != a.end();) {\n out << *it;\n if (++it != a.end()) out << ' ';\n }\n return out;\n}\ninline void print() { std::cout << '\\n'; }\ntemplate <typename Head, typename... Tail>\ninline void print(const Head& head, const Tail &...tails) {\n std::cout << head;\n if (sizeof...(tails)) std::cout << ' ';\n print(tails...);\n}\ntemplate <typename Iterable>\nauto print_all(const Iterable& v, std::string sep = \" \", std::string end = \"\\n\") -> decltype(std::cout << *v.begin(), void()) {\n for (auto it = v.begin(); it != v.end();) {\n std::cout << *it;\n if (++it != v.end()) std::cout << sep;\n }\n std::cout << end;\n}\n\n__int128_t parse_i128(std::string& s) {\n __int128_t ret = 0;\n for (int i = 0; i < int(s.size()); i++) if ('0' <= s[i] and s[i] <= '9') ret = 10 * ret + s[i] - '0';\n if (s[0] == '-') ret = -ret;\n return ret;\n}\n__uint128_t parse_u128(std::string& s) {\n __uint128_t ret = 0;\n for (int i = 0; i < int(s.size()); i++) if ('0' <= s[i] and s[i] <= '9') ret = 10 * ret + s[i] - '0';\n return ret;\n}\n// __int128_t\nstd::istream& operator>>(std::istream& in, __int128_t& v) {\n std::string s;\n in >> s;\n v = parse_i128(s);\n return in;\n}\n// __uint128_t\nstd::istream& operator>>(std::istream& in, __uint128_t& v) {\n std::string s;\n in >> s;\n v = parse_u128(s);\n return in;\n}\n// pair\ntemplate <typename T, typename U>\nstd::istream& operator>>(std::istream& in, std::pair<T, U>& a) {\n return in >> a.first >> a.second;\n}\n// tuple\ntemplate <unsigned int N = 0, typename ...Args>\nstd::istream& operator>>(std::istream& in, std::tuple<Args...>& a) {\n if constexpr (N >= std::tuple_size_v<std::tuple<Args...>>) {\n return in;\n } else {\n return operator>><N + 1>(in >> std::get<N>(a), a);\n }\n}\n// vector\ntemplate <typename T>\nstd::istream& operator>>(std::istream& in, std::vector<T>& a) {\n for (auto it = a.begin(); it != a.end(); ++it) in >> *it;\n return in;\n}\n// array\ntemplate <typename T, size_t N>\nstd::istream& operator>>(std::istream& in, std::array<T, N>& a) {\n for (auto it = a.begin(); it != a.end(); ++it) in >> *it;\n return in;\n}\ntemplate <typename ...Args>\nvoid read(Args &...args) {\n (std::cin >> ... >> args);\n}\n\n// ! integral utilities\n\n// Returns pow(-1, n)\ntemplate <typename T>\nconstexpr inline int pow_m1(T n) {\n return -(n & 1) | 1;\n}\n// Returns pow(-1, n)\ntemplate <>\nconstexpr inline int pow_m1<bool>(bool n) {\n return -int(n) | 1;\n}\n\n// Returns floor(x / y)\ntemplate <typename T>\nconstexpr inline T fld(const T x, const T y) {\n return (x ^ y) >= 0 ? x / y : (x - (y + pow_m1(y >= 0))) / y;\n}\ntemplate <typename T>\nconstexpr inline T cld(const T x, const T y) {\n return (x ^ y) <= 0 ? x / y : (x + (y + pow_m1(y >= 0))) / y;\n}\n\ntemplate <typename T, suisen::constraints_t<suisen::is_nbit<T, 16>> = nullptr>\n__attribute__((target(\"popcnt\"))) constexpr inline int popcount(const T x) { return _mm_popcnt_u32(x); }\ntemplate <typename T, suisen::constraints_t<suisen::is_nbit<T, 32>> = nullptr>\n__attribute__((target(\"popcnt\"))) constexpr inline int popcount(const T x) { return _mm_popcnt_u32(x); }\ntemplate <typename T, suisen::constraints_t<suisen::is_nbit<T, 64>> = nullptr>\n__attribute__((target(\"popcnt\"))) constexpr inline int popcount(const T x) { return _mm_popcnt_u64(x); }\ntemplate <typename T, suisen::constraints_t<suisen::is_nbit<T, 16>> = nullptr>\nconstexpr inline int count_lz(const T x) { return x ? __builtin_clz(x) : suisen::bit_num<T>; }\ntemplate <typename T, suisen::constraints_t<suisen::is_nbit<T, 32>> = nullptr>\nconstexpr inline int count_lz(const T x) { return x ? __builtin_clz(x) : suisen::bit_num<T>; }\ntemplate <typename T, suisen::constraints_t<suisen::is_nbit<T, 64>> = nullptr>\nconstexpr inline int count_lz(const T x) { return x ? __builtin_clzll(x) : suisen::bit_num<T>; }\ntemplate <typename T, suisen::constraints_t<suisen::is_nbit<T, 16>> = nullptr>\nconstexpr inline int count_tz(const T x) { return x ? __builtin_ctz(x) : suisen::bit_num<T>; }\ntemplate <typename T, suisen::constraints_t<suisen::is_nbit<T, 32>> = nullptr>\nconstexpr inline int count_tz(const T x) { return x ? __builtin_ctz(x) : suisen::bit_num<T>; }\ntemplate <typename T, suisen::constraints_t<suisen::is_nbit<T, 64>> = nullptr>\nconstexpr inline int count_tz(const T x) { return x ? __builtin_ctzll(x) : suisen::bit_num<T>; }\ntemplate <typename T>\nconstexpr inline int floor_log2(const T x) { return suisen::bit_num<T> -1 - count_lz(x); }\ntemplate <typename T>\nconstexpr inline int ceil_log2(const T x) { return floor_log2(x) + ((x & -x) != x); }\ntemplate <typename T>\nconstexpr inline int kth_bit(const T x, const unsigned int k) { return (x >> k) & 1; }\ntemplate <typename T>\nconstexpr inline int parity(const T x) { return popcount(x) & 1; }\n\n// ! container\n\ntemplate <typename T, typename Comparator, suisen::constraints_t<suisen::is_comparator<Comparator, T>> = nullptr>\nauto priqueue_comp(const Comparator comparator) {\n return std::priority_queue<T, std::vector<T>, Comparator>(comparator);\n}\n\ntemplate <typename Iterable>\nauto isize(const Iterable& iterable) -> decltype(int(iterable.size())) {\n return iterable.size();\n}\n\ntemplate <typename T, typename Gen, suisen::constraints_t<suisen::is_same_as_invoke_result<T, Gen, int>> = nullptr>\nauto generate_vector(int n, Gen generator) {\n std::vector<T> v(n);\n for (int i = 0; i < n; ++i) v[i] = generator(i);\n return v;\n}\ntemplate <typename T>\nauto generate_range_vector(T l, T r) {\n return generate_vector(r - l, [l](int i) { return l + i; });\n}\ntemplate <typename T>\nauto generate_range_vector(T n) {\n return generate_range_vector(0, n);\n}\n\ntemplate <typename T>\nvoid sort_unique_erase(std::vector<T>& a) {\n std::sort(a.begin(), a.end());\n a.erase(std::unique(a.begin(), a.end()), a.end());\n}\n\ntemplate <typename InputIterator, typename BiConsumer>\nauto foreach_adjacent_values(InputIterator first, InputIterator last, BiConsumer f) -> decltype(f(*first++, *last), void()) {\n if (first != last) for (auto itr = first, itl = itr++; itr != last; itl = itr++) f(*itl, *itr);\n}\ntemplate <typename Container, typename BiConsumer>\nauto foreach_adjacent_values(Container c, BiConsumer f) -> decltype(c.begin(), c.end(), void()) {\n foreach_adjacent_values(c.begin(), c.end(), f);\n}\n\n// ! other utilities\n\n// x <- min(x, y). returns true iff `x` has chenged.\ntemplate <typename T>\ninline bool chmin(T& x, const T& y) {\n if (y >= x) return false;\n x = y;\n return true;\n}\n// x <- max(x, y). returns true iff `x` has chenged.\ntemplate <typename T>\ninline bool chmax(T& x, const T& y) {\n if (y <= x) return false;\n x = y;\n return true;\n}\n\ntemplate <typename T, std::enable_if_t<std::is_integral_v<T>, std::nullptr_t> = nullptr>\nstd::string bin(T val, int bit_num = -1) {\n std::string res;\n if (bit_num >= 0) {\n for (int bit = bit_num; bit-- > 0;) res += '0' + ((val >> bit) & 1);\n } else {\n for (; val; val >>= 1) res += '0' + (val & 1);\n std::reverse(res.begin(), res.end());\n }\n return res;\n}\n\ntemplate <typename T, std::enable_if_t<std::is_integral_v<T>, std::nullptr_t> = nullptr>\nstd::vector<T> digits_low_to_high(T val, T base = 10) {\n std::vector<T> res;\n for (; val; val /= base) res.push_back(val % base);\n if (res.empty()) res.push_back(T{ 0 });\n return res;\n}\ntemplate <typename T, std::enable_if_t<std::is_integral_v<T>, std::nullptr_t> = nullptr>\nstd::vector<T> digits_high_to_low(T val, T base = 10) {\n auto res = digits_low_to_high(val, base);\n std::reverse(res.begin(), res.end());\n return res;\n}\n\ntemplate <typename T>\nstd::string join(const std::vector<T>& v, const std::string& sep, const std::string& end) {\n std::ostringstream ss;\n for (auto it = v.begin(); it != v.end();) {\n ss << *it;\n if (++it != v.end()) ss << sep;\n }\n ss << end;\n return ss.str();\n}\n\nnamespace suisen {}\nusing namespace suisen;\nusing namespace std;\n\nstruct io_setup {\n io_setup(int precision = 20) {\n std::ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout << std::fixed << std::setprecision(precision);\n }\n} io_setup_{};\n\n// ! code from here\n\nconstexpr int M = 1000000;\n\nint main() {\n input(int, n, xa, xb, m);\n\n vector<vector<int>> a(n, vector<int>(n));\n vector<vector<int>> b(n, vector<int>(n));\n\n vector<int> ra(n, 0), ca(n, 0), rb(n, 0), cb(n, 0);\n int a1 = 0, a2 = 0, b1 = 0, b2 = 0;\n vector<pair<int, int>> pa(M, { -1, -1 }), pb(M, { -1, -1 });\n\n read(a, b);\n rep(i, n) rep(j, n) {\n pa[--a[i][j]] = { i, j };\n pb[--b[i][j]] = { i, j };\n }\n\n int la = 0, lb = 0;\n loop(m) {\n input(int, v);\n --v;\n auto [ia, ja] = pa[v];\n auto [ib, jb] = pb[v];\n if (ia >= 0) {\n if (++ra[ia] == n) ++la;\n if (++ca[ja] == n) ++la;\n if (ia == ja) {\n if (++a1 == n) ++la;\n }\n if (ia + ja == n - 1) {\n if (++a2 == n) ++la;\n }\n }\n if (ib >= 0) {\n if (++rb[ib] == n) ++lb;\n if (++cb[jb] == n) ++lb;\n if (ib == jb) {\n if (++b1 == n) ++lb;\n }\n if (ib + jb == n - 1) {\n if (++b2 == n) ++lb;\n }\n }\n if (n == 1) {\n chmin(la, 1);\n }\n if (n == 1) {\n chmin(lb, 1);\n }\n if (la >= xa and lb >= xb) {\n print(\"DRAW\");\n return 0;\n } else if (la >= xa) {\n print(\"USAGI\");\n return 0;\n } else if (lb >= xb) {\n print(\"NEKO\");\n return 0;\n }\n }\n print(\"DRAW\");\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 19836, "score_of_the_acc": -0.353, "final_rank": 4 }, { "submission_id": "aoj_2241_6612367", "code_snippet": "#include <bits/stdc++.h>\n//#include <atcoder/all>\n//using namespace atcoder;\nusing namespace std;\nusing ll = long long;\nusing vll = vector<ll>;\nusing vvll = vector<vll>;\nusing vvvll = vector<vvll>;\n#define all(A) A.begin(),A.end()\n#define rep(i, n) for (ll i = 0; i < (ll) (n); i++)\nusing pqr = priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>>;\n\nvector<ll> fact, factinv, inv;\nll mod = 998244353;\nvoid prenCkModp(ll n) {\n fact.resize(n + 5);\n factinv.resize(n + 5);\n inv.resize(n + 5);\n fact.at(0) = fact.at(1) = 1;\n factinv.at(0) = factinv.at(1) = 1;\n inv.at(1) = 1;\n for (ll i = 2; i < n + 5; i++) {\n fact.at(i) = (fact.at(i - 1) * i) % mod;\n inv.at(i) = mod - (inv.at(mod % i) * (mod / i)) % mod;\n factinv.at(i) = (factinv.at(i - 1) * inv.at(i)) % mod;\n }\n\n}\nll nCk(ll n, ll k) {\n if (n < k) return 0;\n return fact.at(n) * (factinv.at(k) * factinv.at(n - k) % mod) % mod;\n}\n\nbool chmax(ll& p, ll q) {\n if (p < q) {\n p = q;\n return 1;\n }\n else {\n return 0;\n }\n}\n\nbool chmin(ll& p, ll q) {\n if (p > q) {\n p = q;\n return 1;\n }\n else {\n return 0;\n }\n}\n\nint main() {\n //cin.tie(nullptr);\n //ios::sync_with_stdio(false);\n\n ll N,U,V,M;\n cin>>N>>U>>V>>M;\n map<ll,pair<ll,ll>> R,C;\n\n\n if(M==37355){\n cout<<\"USAGI\"<<endl;//落ちてる理由を探すために...良くないのだろうけども\n return 0;\n }\n rep(i,N)rep(j,N){\n ll A;\n cin>>A;\n R[A]={i,j};\n }\n rep(i,N)rep(j,N){\n ll A;\n cin>>A;\n C[A]={i,j};\n }\n\n vll RR(N,0),RC(N,0),CR(N,0),CC(N,0);\n ll RQ=0,RL=0,CQ=0,CL=0;\n ll RS=0,CS=0;\n string AN=\"MADA\";\n rep(m,M){\n ll P;\n cin>>P;\n if(R.count(P)){\n RR[R[P].first]++;\n RC[R[P].second]++;\n if(RR[R[P].first]==N){\n RS++;\n RR[R[P].first]++;\n }\n if(N!=1){\n if(RC[R[P].second]==N){\n RR[R[P].second]++;\n RS++;\n }\n }\n\n if(R[P].second==R[P].first)RQ++;\n if(R[P].second+R[P].first==N-1)RL++;\n\n if(N!=1){\n if(RQ==N){\n RS++;\n RQ++;\n }\n }\n if(N!=1){\n if(RL==N){\n RS++;\n RL++;\n }\n }\n\n\n }\n if(C.count(P)){\n CR[C[P].first]++;\n CC[C[P].second]++;\n if(N!=1){\n if(CR[C[P].first]==N){\n CS++;\n CR[C[P].first]++;\n }\n }\n if(CC[C[P].second]==N){\n CS++;\n CC[C[P].second]++;\n }\n\n if(C[P].second==C[P].first){\n CQ++;\n }\n if(C[P].second+C[P].first==N-1){\n CL++;\n }\n\n if(N!=1){\n if(CQ==N){\n CS++;\n CQ++;\n }\n }\n if(N!=1){\n if(CL==N){\n CS++;\n CL++;\n }\n }\n }\n //cout<<RS<<\" \"<<CS<<endl;\n if(AN==\"MADA\"){\n if(RS>=U&&CS>=V)AN=\"DRAW\";\n else if(RS>=U)AN=\"USAGI\";\n else if(CS>=V)AN=\"NEKO\";\n }\n }\n\n if(AN==\"MADA\")AN=\"DRAW\";\n\n cout<<AN<<endl;\n\n\n \n}", "accuracy": 1, "time_ms": 150, "memory_kb": 21332, "score_of_the_acc": -0.9077, "final_rank": 12 }, { "submission_id": "aoj_2241_6612365", "code_snippet": "#include <bits/stdc++.h>\n//#include <atcoder/all>\n//using namespace atcoder;\nusing namespace std;\nusing ll = long long;\nusing vll = vector<ll>;\nusing vvll = vector<vll>;\nusing vvvll = vector<vvll>;\n#define all(A) A.begin(),A.end()\n#define rep(i, n) for (ll i = 0; i < (ll) (n); i++)\nusing pqr = priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>>;\n\nvector<ll> fact, factinv, inv;\nll mod = 998244353;\nvoid prenCkModp(ll n) {\n fact.resize(n + 5);\n factinv.resize(n + 5);\n inv.resize(n + 5);\n fact.at(0) = fact.at(1) = 1;\n factinv.at(0) = factinv.at(1) = 1;\n inv.at(1) = 1;\n for (ll i = 2; i < n + 5; i++) {\n fact.at(i) = (fact.at(i - 1) * i) % mod;\n inv.at(i) = mod - (inv.at(mod % i) * (mod / i)) % mod;\n factinv.at(i) = (factinv.at(i - 1) * inv.at(i)) % mod;\n }\n\n}\nll nCk(ll n, ll k) {\n if (n < k) return 0;\n return fact.at(n) * (factinv.at(k) * factinv.at(n - k) % mod) % mod;\n}\n\nbool chmax(ll& p, ll q) {\n if (p < q) {\n p = q;\n return 1;\n }\n else {\n return 0;\n }\n}\n\nbool chmin(ll& p, ll q) {\n if (p > q) {\n p = q;\n return 1;\n }\n else {\n return 0;\n }\n}\n\nint main() {\n //cin.tie(nullptr);\n //ios::sync_with_stdio(false);\n\n ll N,U,V,M;\n cin>>N>>U>>V>>M;\n map<ll,pair<ll,ll>> R,C;\n rep(i,N)rep(j,N){\n ll A;\n cin>>A;\n R[A]={i,j};\n }\n rep(i,N)rep(j,N){\n ll A;\n cin>>A;\n C[A]={i,j};\n }\n\n vll RR(N,0),RC(N,0),CR(N,0),CC(N,0);\n ll RQ=0,RL=0,CQ=0,CL=0;\n ll RS=0,CS=0;\n string AN=\"MADA\";\n rep(m,M){\n ll P;\n cin>>P;\n if(R.count(P)){\n RR[R[P].first]++;\n RC[R[P].second]++;\n if(RR[R[P].first]==N){\n RS++;\n RR[R[P].first]++;\n }\n if(N!=1){\n if(RC[R[P].second]==N){\n RR[R[P].second]++;\n RS++;\n }\n }\n\n if(R[P].second==R[P].first)RQ++;\n if(R[P].second+R[P].first==N-1)RL++;\n\n if(N!=1){\n if(RQ==N){\n RS++;\n RQ++;\n }\n }\n if(N!=1){\n if(RL==N){\n RS++;\n RL++;\n }\n }\n\n\n }\n if(C.count(P)){\n CR[C[P].first]++;\n CC[C[P].second]++;\n if(N!=1){\n if(CR[C[P].first]==N){\n CS++;\n CR[C[P].first]++;\n }\n }\n if(CC[C[P].second]==N){\n CS++;\n CC[C[P].second]++;\n }\n\n if(C[P].second==C[P].first){\n CQ++;\n }\n if(C[P].second+C[P].first==N-1){\n CL++;\n }\n\n if(N!=1){\n if(CQ==N){\n CS++;\n CQ++;\n }\n }\n if(N!=1){\n if(CL==N){\n CS++;\n CL++;\n }\n }\n }\n //cout<<RS<<\" \"<<CS<<endl;\n if(AN==\"MADA\"){\n if(RS>=U&&CS>=V)AN=\"DRAW\";\n else if(RS>=U)AN=\"USAGI\";\n else if(CS>=V)AN=\"NEKO\";\n }\n }\n\n if(AN==\"MADA\")AN=\"DRAW\";\n\n cout<<AN<<endl;\n\n\n \n}", "accuracy": 0.32786885245901637, "time_ms": 150, "memory_kb": 21292, "score_of_the_acc": -0.9063, "final_rank": 20 }, { "submission_id": "aoj_2241_6612363", "code_snippet": "#include <bits/stdc++.h>\n//#include <atcoder/all>\n//using namespace atcoder;\nusing namespace std;\nusing ll = long long;\nusing vll = vector<ll>;\nusing vvll = vector<vll>;\nusing vvvll = vector<vvll>;\n#define all(A) A.begin(),A.end()\n#define rep(i, n) for (ll i = 0; i < (ll) (n); i++)\nusing pqr = priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>>;\n\nvector<ll> fact, factinv, inv;\nll mod = 998244353;\nvoid prenCkModp(ll n) {\n fact.resize(n + 5);\n factinv.resize(n + 5);\n inv.resize(n + 5);\n fact.at(0) = fact.at(1) = 1;\n factinv.at(0) = factinv.at(1) = 1;\n inv.at(1) = 1;\n for (ll i = 2; i < n + 5; i++) {\n fact.at(i) = (fact.at(i - 1) * i) % mod;\n inv.at(i) = mod - (inv.at(mod % i) * (mod / i)) % mod;\n factinv.at(i) = (factinv.at(i - 1) * inv.at(i)) % mod;\n }\n\n}\nll nCk(ll n, ll k) {\n if (n < k) return 0;\n return fact.at(n) * (factinv.at(k) * factinv.at(n - k) % mod) % mod;\n}\n\nbool chmax(ll& p, ll q) {\n if (p < q) {\n p = q;\n return 1;\n }\n else {\n return 0;\n }\n}\n\nbool chmin(ll& p, ll q) {\n if (p > q) {\n p = q;\n return 1;\n }\n else {\n return 0;\n }\n}\n\nint main() {\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n prenCkModp(100000);\n\n ll N,U,V,M;\n cin>>N>>U>>V>>M;\n ll WIN=-1;\n\n map<ll,pair<ll,ll>> R,C;\n rep(i,N)rep(j,N){\n ll A;\n cin>>A;\n R[A]={i,j};\n }\n rep(i,N)rep(j,N){\n ll A;\n cin>>A;\n C[A]={i,j};\n }\n\n vll RR(N,0),RC(N,0),CR(N,0),CC(N,0);\n ll RQ=0,RL=0,CQ=0,CL=0;\n ll RS=0,CS=0;\n string AN=\"MADA\";\n rep(m,M){\n ll P;\n cin>>P;\n if(R.count(P)){\n RR[R[P].first]++;\n RC[R[P].second]++;\n if(RR[R[P].first]==N){\n RS++;\n RR[R[P].first]++;\n }\n if(N!=1)if(RC[R[P].second]==N){\n RR[R[P].second]++;\n RS++;\n }\n\n if(R[P].second==R[P].first)RQ++;\n if(R[P].second+R[P].first==N-1)RL++;\n\n if(N!=1)if(RQ==N){\n RS++;\n RQ++;\n }\n if(N!=1)if(RL==N){\n RS++;\n RL++;\n }\n\n\n }\n if(C.count(P)){\n CR[C[P].first]++;\n CC[C[P].second]++;\n if(N!=1)if(CR[C[P].first]==N)CS++,CR[C[P].first]++;\n if(CC[C[P].second]==N)CS++,CC[C[P].second]++;\n\n if(C[P].second==C[P].first)CQ++;\n if(C[P].second+C[P].first==N-1)CL++;\n\n if(N!=1)if(CQ==N)CS++,CQ++;\n if(N!=1)if(CL==N){\n CS++;\n CL++;\n }\n }\n //cout<<RS<<\" \"<<CS<<endl;\n if(AN==\"MADA\"){\n if(RS>=U&&CS>=V)AN=\"DRAW\";\n else if(RS>=U)AN=\"USAGI\";\n else if(CS>=V)AN=\"NEKO\";\n }\n }\n\n if(AN==\"MADA\")AN=\"DRAW\";\n\n cout<<AN<<endl;\n\n\n \n}", "accuracy": 0.32786885245901637, "time_ms": 100, "memory_kb": 23312, "score_of_the_acc": -0.8017, "final_rank": 19 }, { "submission_id": "aoj_2241_6357354", "code_snippet": "#include <bits/stdc++.h>\n#define rep(i,n) for(int i = 0; i < (n); i++)\nusing namespace std;\ntypedef long long ll;\n\nint main(){\n cin.tie(0);\n ios::sync_with_stdio(0);\n \n int n,u,v,m; cin >> n >> u >> v >> m;\n vector<pair<int,int>> usagi(1010101, {-1, -1}), neko(1010101, {-1, -1});\n rep(i,n)rep(j,n) {\n int a; cin >> a;\n usagi[a] = {i, j};\n }\n rep(i,n)rep(j,n) {\n int a; cin >> a;\n neko[a] = {i, j};\n }\n\n vector<int> usagi_h(n, 0), usagi_w(n, 0);\n int usagi_d1 = 0, usagi_d2 = 0;\n vector<int> neko_h(n, 0), neko_w(n, 0);\n int neko_d1 = 0, neko_d2 = 0;\n int usagi_score = 0, neko_score = 0;\n rep(_,m) {\n int a; cin >> a;\n\n // usagi\n if(usagi[a] != pair<int,int>{-1, -1}) {\n auto [i, j] = usagi[a];\n usagi_h[i]++; if(usagi_h[i] == n) usagi_score++;\n usagi_w[j]++; if(usagi_w[j] == n) usagi_score++;\n if(i == j) {\n usagi_d1++; if(usagi_d1 == n) usagi_score++;\n }\n if(i == n - 1 - j) {\n usagi_d2++; if(usagi_d2 == n) usagi_score++;\n }\n if(n == 1) usagi_score = !!usagi_score;\n }\n\n\n // neko\n if(neko[a] != pair<int,int>{-1, -1}) {\n auto [i, j] = neko[a];\n neko_h[i]++; if(neko_h[i] == n) neko_score++;\n neko_w[j]++; if(neko_w[j] == n) neko_score++;\n if(i == j) {\n neko_d1++; if(neko_d1 == n) neko_score++;\n }\n if(i == n - 1 - j) {\n neko_d2++; if(neko_d2 == n) neko_score++;\n }\n if(n == 1) neko_score = !!neko_score;\n }\n\n if(u <= usagi_score && v <= neko_score) {\n cout << \"DRAW\" << endl; return 0;\n }\n if(u <= usagi_score) {\n cout << \"USAGI\" << endl; return 0;\n }\n if(v <= neko_score) {\n cout << \"NEKO\" << endl; return 0;\n }\n }\n\n cout << \"DRAW\" << endl;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 18840, "score_of_the_acc": -0.3165, "final_rank": 2 }, { "submission_id": "aoj_2241_6008718", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint win_turn(int n, int border, int m, vector<vector<int>> board, vector<int> cards) {\n map<int, pair<int, int>> board_index;\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n board_index[board[i][j]] = make_pair(i, j);\n }\n }\n\n int imin = 0;\n int imax = m + 1;\n\n while (imax - imin > 1) {\n int imid = (imin + imax) / 2;\n bool checked[n][n] = {};\n for (int i = 0; i < imid; i++) {\n if (!board_index.count(cards[i])) continue;\n auto p = board_index[cards[i]];\n checked[p.first][p.second] = true;\n }\n\n int cnt = 0;\n\n // 縦\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n if (!checked[i][j]) {\n break;\n }\n if (j == n - 1) {\n cnt++;\n }\n }\n }\n\n if (n != 1) {\n // 横\n for (int j = 0; j < n; j++) {\n for (int i = 0; i < n; i++) {\n if (!checked[i][j]) {\n break;\n }\n if (i == n - 1) {\n cnt++;\n }\n }\n }\n\n // 斜め1\n for (int i = 0; i < n; i++) {\n if (!checked[i][i]) {\n break;\n }\n if (i == n - 1) {\n cnt++;\n }\n }\n\n // 斜め2\n for (int i = 0; i < n; i++) {\n if (!checked[i][n - 1 - i]) {\n break;\n }\n if (i == n - 1) {\n cnt++;\n }\n }\n }\n\n if (cnt >= border) {\n imax = imid;\n } else {\n imin = imid;\n }\n }\n\n return imax;\n}\n\nint main() {\n int n, u, v, m;\n cin >> n >> u >> v >> m;\n\n vector<vector<int>> usagi(n, vector<int>(n)), neko(n, vector<int>(n));\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n cin >> usagi[i][j];\n }\n }\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n cin >> neko[i][j];\n }\n }\n\n vector<int> cards(m);\n for (int i = 0; i < m; i++) {\n cin >> cards[i];\n }\n\n auto usa = win_turn(n, u, m, usagi, cards);\n auto ne = win_turn(n, v, m, neko, cards);\n\n if (usa == ne) {\n cout << \"DRAW\" << endl;\n return 0;\n }\n cout << (usa < ne ? \"USAGI\" : \"NEKO\") << endl;\n}", "accuracy": 1, "time_ms": 290, "memory_kb": 13992, "score_of_the_acc": -1.139, "final_rank": 15 }, { "submission_id": "aoj_2241_6001620", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint M = 1000005;\nint INF = 1 << 30;\n\nint main() {\n int n, u, v, m;\n cin >> n >> u >> v >> m;\n vector<int> ux(M, -1), uy(M, -1), cx(M, -1), cy(M, -1);\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n int num;\n cin >> num;\n ux[num] = i;\n uy[num] = j;\n }\n }\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n int num;\n cin >> num;\n cx[num] = i;\n cy[num] = j;\n }\n }\n vector<int> uxc(n + 1, 0), uyc(n + 1, 0), cxc(n + 1, 0), cyc(n + 1, 0);\n int uc = 0, cc = 0;\n while (m--) {\n int num;\n cin >> num;\n if (ux[num] != -1) {\n uxc[ux[num]]++;\n uyc[uy[num]]++;\n if (ux[num] == uy[num]) uxc[n]++;\n if (ux[num] == n - 1 - uy[num]) uyc[n]++;\n if (uxc[ux[num]] == n) {\n uc++;\n uxc[ux[num]] = -INF;\n }\n if (n != 1 && uyc[uy[num]] == n) {\n uc++;\n uyc[uy[num]] = -INF;\n }\n if (n != 1 && uxc[n] == n) {\n uc++;\n uxc[n] = -INF;\n }\n if (n != 1 && uyc[n] == n) {\n uc++;\n uyc[n] = -INF;\n }\n }\n if (cx[num] != -1) {\n cxc[cx[num]]++;\n cyc[cy[num]]++;\n if (cx[num] == cy[num]) cxc[n]++;\n if (cx[num] == n - 1 - cy[num]) cyc[n]++;\n if (cxc[cx[num]] == n) {\n cc++;\n cxc[cx[num]] = -INF;\n }\n if (n != 1 && cyc[cy[num]] == n) {\n cc++;\n cyc[cy[num]] = -INF;\n }\n if (n != 1 && cxc[n] == n) {\n cc++;\n cxc[n] = -INF;\n }\n if (n != 1 && cyc[n] == n) {\n cc++;\n cyc[n] = -INF;\n }\n }\n // cout << uc << \" \" << cc << endl;\n if (uc >= u && cc >= v) {\n puts(\"DRAW\");\n return 0;\n } else if (uc >= u) {\n puts(\"USAGI\");\n return 0;\n } else if (cc >= v) {\n puts(\"NEKO\");\n return 0;\n }\n }\n puts(\"DRAW\");\n return 0;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 18748, "score_of_the_acc": -0.456, "final_rank": 6 }, { "submission_id": "aoj_2241_5992271", "code_snippet": "#include <bits/stdc++.h>\n#define rep(i,n) for(int i = 0; i < (int)(n); i++)\n#define rrep(ri,n) for(int ri = (int)(n-1); ri >= 0; ri--)\n#define rep2(i,x,n) for(int i = (int)(x); i < (int)(n); i++)\n#define rrep2(ri,x,n) for(int ri = (int)(n-1); ri >= (int)(x); ri--)\n#define repit(itr,x) for(auto itr = x.begin(); itr != x.end(); itr++)\n#define rrepit(ritr,x) for(auto ritr = x.rbegin(); ritr != x.rend(); ritr++)\n#define ALL(x) x.begin(), x.end()\nusing ll = long long;\nusing namespace std;\n\nint main() {\n int n, u, v, m;\n cin >> n >> u >> v >> m;\n vector tu(n, vector<int>(n));\n rep(i, n)rep(j, n) cin >> tu.at(i).at(j);\n vector tv(n, vector<int>(n));\n rep(i, n)rep(j, n) cin >> tv.at(i).at(j);\n map<int, pair<int, int>> mpu, mpv;\n rep(i, n)rep(j, n) mpu[tu.at(i).at(j)] = {i, j};\n rep(i, n)rep(j, n) mpv[tv.at(i).at(j)] = {i, j};\n vector bu(n, vector<bool>(n, false));\n vector bv(n, vector<bool>(n, false));\n\n vector<int> input(m);\n rep(i, m) {\n int in;\n cin >> in;\n input.at(i) = in;\n }\n auto fc = [=](vector<vector<bool>> &table, int pi, int pj) -> int {\n int res = 0;\n if(n == 1) return 1;\n bool f = false;\n rep(k, n) {\n if(!table.at(pi).at(k)) {\n f = true;\n break;\n }\n }\n if(!f) res++;\n f = false;\n rep(k, n) {\n if(!table.at(k).at(pj)) {\n f = true;\n break;\n }\n }\n if(!f) res++;\n if(pi == pj) {\n f = false;\n rep(k, n) {\n if(!table.at(k).at(k)) {\n f = true;\n break;\n }\n }\n if(!f) res++;\n }\n if(pi+pj == n-1) {\n f = false;\n rep(k, n) {\n int j = n - 1 - k;\n if(!table.at(k).at(j)) {\n f = true;\n break;\n }\n }\n if(!f) res++;\n }\n return res;\n };\n\n const string U = \"USAGI\", V = \"NEKO\", D = \"DRAW\";\n int uc = 0, vc = 0;\n for(auto now : input) {\n if(mpu.find(now) != mpu.end()) {\n auto [i, j] = mpu[now];\n bu.at(i).at(j) = true;\n uc += fc(bu, i, j);\n }\n if(mpv.find(now) != mpv.end()) {\n auto [i, j] = mpv[now];\n bv.at(i).at(j) = true;\n vc += fc(bv, i, j);\n }\n if(uc >= u) {\n if(vc >= v) {\n cout << D << endl;\n return 0;\n } else {\n cout << U << endl;\n return 0;\n }\n } else {\n if(vc >= v) {\n cout << V << endl;\n return 0;\n }\n }\n }\n cout << D << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 140, "memory_kb": 22632, "score_of_the_acc": -0.9196, "final_rank": 13 }, { "submission_id": "aoj_2241_5918114", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing P = pair<int,int>;\n\nint cnt(vector<vector<bool>> &X, int x, int y){\n if(X.size() == 1){\n return 1;\n }\n int ret = 0;\n for(int j=0; j<X.size(); j++){\n if(!X[x][j] && j != y){\n break;\n }\n if(j == X.size()-1){\n ret++;\n }\n }\n for(int i=0; i<X.size(); i++){\n if(!X[i][y] && i != x){\n break;\n }\n if(i == X.size()-1){\n ret++;\n }\n }\n if(x == y){\n for(int j=0; j<X.size(); j++){\n if(!X[j][j] && j != y){\n break;\n }\n if(j == X.size()-1){\n ret++;\n }\n }\n }\n if(x + y == X.size()-1){\n for(int j=0; j<X.size(); j++){\n if(!X[j][X.size()-1-j] && j != x){\n break;\n }\n if(j == X.size()-1){\n ret++;\n }\n }\n }\n return ret;\n}\n\nvoid solve(){\n int N, U, V, M;\n cin >> N >> U >> V >> M;\n vector<vector<bool>> A(N,vector<bool>(N));\n vector<vector<bool>> B(N,vector<bool>(N));\n map<int,P> Amp;\n map<int,P> Bmp;\n for(int i=0; i<N*N; i++){\n int x;\n cin >> x;\n Amp[x] = P(i/N, i%N);\n }\n for(int i=0; i<N*N; i++){\n int x;\n cin >> x;\n Bmp[x] = P(i/N, i%N);\n }\n \n for(int i=0; i<M; i++){\n int x;\n cin >> x;\n if(Amp.count(x)){\n auto[a,b] = Amp[x];\n U -= cnt(A,a,b);\n A[a][b] = true;\n }\n if(Bmp.count(x)){\n auto[a,b] = Bmp[x];\n V -= cnt(B,a,b);\n B[a][b] = true;\n }\n if(U <= 0 && V > 0){\n cout << \"USAGI\" << endl;\n return;\n }\n else if(U > 0 && V <= 0){\n cout << \"NEKO\" << endl;\n return;\n }\n }\n cout << \"DRAW\" << endl;\n}\n\nint main(){\n while(true){\n solve();\n break;\n }\n}", "accuracy": 1, "time_ms": 120, "memory_kb": 21344, "score_of_the_acc": -0.801, "final_rank": 11 }, { "submission_id": "aoj_2241_5877019", "code_snippet": "#pragma region Macros\n#include <bits/stdc++.h>\nusing namespace std;\ntemplate <class T> inline bool chmax(T &a, T b) {\n if(a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <class T> inline bool chmin(T &a, T b) {\n if(a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\n#ifdef DEBUG\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << '(' << p.first << ',' << p.second << ')';\n return os;\n}\ntemplate <class T> ostream &operator<<(ostream &os, const vector<T> &v) {\n os << '{';\n for(int i = 0; i < (int)v.size(); i++) {\n if(i) { os << ','; }\n os << v[i];\n }\n os << '}';\n return os;\n}\nvoid debugg() { cerr << endl; }\ntemplate <class T, class... Args>\nvoid debugg(const T &x, const Args &... args) {\n cerr << \" \" << x;\n debugg(args...);\n}\n#define debug(...) \\\n cerr << __LINE__ << \" [\" << #__VA_ARGS__ << \"]: \", debugg(__VA_ARGS__)\n#define dump(x) cerr << __LINE__ << \" \" << #x << \" = \" << (x) << endl\n#else\n#define debug(...) (void(0))\n#define dump(x) (void(0))\n#endif\n\nstruct Setup {\n Setup() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(15);\n }\n} __Setup;\n\nusing ll = long long;\n#define ALL(v) (v).begin(), (v).end()\n#define RALL(v) (v).rbegin(), (v).rend()\n#define FOR(i, a, b) for(int i = (a); i < int(b); i++)\n#define REP(i, n) FOR(i, 0, n)\nconst int INF = 1 << 30;\nconst ll LLINF = 1LL << 60;\nconstexpr int MOD = 1000000007;\nconst int dx[4] = {1, 0, -1, 0};\nconst int dy[4] = {0, 1, 0, -1};\n\nvoid Case(int i) { cout << \"Case #\" << i << \": \"; }\nint popcount(int x) { return __builtin_popcount(x); }\nll popcount(ll x) { return __builtin_popcountll(x); }\n#pragma endregion Macros\n\nconst int MAX = 1000000;\n\nint main() {\n int N, U, V, M;\n cin >> N >> U >> V >> M;\n vector<pair<int, int>> un(MAX+1, {-1, -1}), nn(MAX+1, {-1, -1});\n vector<int> cards(M);\n REP(i, N) REP(j, N) {\n int n; cin >> n;\n un[n] = {i, j};\n } \n REP(i, N) REP(j, N) {\n int n; cin >> n;\n nn[n] = {i, j};\n }\n REP(i, M) cin >> cards[i];\n\n vector usagi(N, vector<int>(N, 0)), neko(N, vector<int>(N, 0));\n int ucnt = 0, ncnt = 0;\n REP(z, M) {\n bool usagi_win = false, neko_win = false;\n // usagi\n auto [iu, ju] = un[cards[z]];\n if(iu != -1 and ju != -1) {\n usagi[iu][ju] = 1;\n bool ok = true;\n REP(j, N) if(usagi[iu][j] != 1) {\n ok = false;\n break;\n }\n ucnt += ok;\n ok = true;\n REP(i, N) if(usagi[i][ju] != 1) {\n ok = false;\n break;\n }\n ucnt += ok;\n if(iu == ju) {\n ok = true;\n REP(i, N) if(usagi[i][i] != 1) {\n ok = false;\n break;\n }\n ucnt += ok;\n }\n if(iu == N-1-ju) {\n ok = true;\n REP(i, N) if(usagi[i][N-1-i] != 1) {\n ok = false;\n break;\n }\n ucnt += ok;\n }\n if(N == 1) ucnt = 1;\n }\n if(ucnt >= U) usagi_win = true;\n // neko\n auto [in, jn] = nn[cards[z]];\n if(in != -1 and jn != -1) {\n neko[in][jn] = 1;\n bool ok = true;\n REP(j, N) if(neko[in][j] != 1) {\n ok = false;\n break;\n }\n ncnt += ok;\n ok = true;\n REP(i, N) if(neko[i][jn] != 1) {\n ok = false;\n break;\n }\n ncnt += ok;\n if(in == jn) {\n ok = true;\n REP(i, N) if(neko[i][i] != 1) {\n ok = false;\n break;\n }\n ncnt += ok;\n }\n if(in == N-1-jn) {\n ok = true;\n REP(i, N) if(neko[i][N-1-i] != 1) {\n ok = false;\n break;\n }\n ncnt += ok;\n }\n if(N == 1) ncnt = 1;\n }\n if(ncnt >= V) neko_win = true;\n\n if(usagi_win and neko_win) {\n cout << \"DRAW\\n\";\n return 0;\n } else if(usagi_win) {\n cout << \"USAGI\\n\";\n return 0;\n } else if(neko_win) {\n cout << \"NEKO\\n\";\n return 0;\n }\n }\n cout << \"DRAW\\n\";\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 19996, "score_of_the_acc": -0.3588, "final_rank": 5 }, { "submission_id": "aoj_2241_5833916", "code_snippet": "/*\tauthor: Kite_kuma\n\tcreated: 2021.08.28 18:42:55 */\n\n// #ifdef LOCAL\n// #define _GLIBCXX_DEBUG\n// #endif\n#include <bits/stdc++.h>\nusing namespace std;\n\n#pragma region macros\n\n#define foa(s, v) for(auto &s : v)\n#define all(v) (v).begin(), (v).end()\n#define rall(v) (v).rbegin(), (v).rend()\n#define popcnt(n) __builtin_popcountll((long long)n)\n\n#define REPname(a, b, c, d, e, ...) e\n#define rep(...) REPname(__VA_ARGS__, REP3, REP2, REP1, REP0)(__VA_ARGS__)\n#define REP0(x) for(int Counter_in_rep_macro = 0; Counter_in_rep_macro < (x); ++Counter_in_rep_macro)\n#define REP1(i, x) for(int i = 0; i < (x); ++i)\n#define REP2(i, l, r) for(int i = (l); i < (r); ++i)\n#define REP3(i, l, r, c) for(int i = (l); i < (r); i += (c))\n\n#define DREPname(a, b, c, d, e, ...) e\n#define drep(...) DREPname(__VA_ARGS__, DREP3, DREP2, DREP1)(__VA_ARGS__)\n#define DREP1(i, x) for(int i = (x)-1; i >= 0; --i)\n#define DREP2(i, l, r) for(int i = (r)-1; i >= (l); --i)\n#define DREP3(i, l, r, c) for(int i = (r)-1; i >= (l); i -= (c))\n\n#pragma endregion\n\n#pragma region aliases\n\nusing ll = long long;\nusing ld = long double;\nusing ull = unsigned long long;\nusing vi = std::vector<int>;\nusing vvi = std::vector<std::vector<int>>;\nusing vvvi = std::vector<std::vector<std::vector<int>>>;\nusing vll = std::vector<ll>;\nusing vvll = std::vector<vll>;\nusing vvvll = std::vector<vvll>;\nusing pii = std::pair<int, int>;\nusing pll = std::pair<long long, long long>;\ntemplate <class T = ll>\nusing V = std::vector<T>;\ntemplate <class T = ll>\nusing VV = V<V<T>>;\ntemplate <class T = ll>\nusing VVV = V<V<V<T>>>;\ntemplate <class T = ll>\nusing pqup = std::priority_queue<T, std::vector<T>, std::greater<T>>;\ntemplate <class T = ll>\nusing pqdn = std::priority_queue<T>;\n\n#pragma endregion\n\n#pragma region constants\n\nconst int inf = 1e9;\nconst long long INF = 1e18;\nconst long double pi = acos(-1);\nconst char dl = '\\n';\nconst char sp = ' ';\nint dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};\nint dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};\n\nconst int mod_1000000007 = 1000000007;\nconst int mod_998244353 = 998244353;\n\n#pragma endregion\n\n#pragma region basic_operation\n\ntemplate <class T0, class T1, class T2>\ninline bool in_range(T0 x, T1 lef, T2 rig) {\n\treturn ((lef <= x) && (x < rig));\n}\n\ntemplate <class T>\ninline bool chmin(T &a, T b) {\n\tif(a > b) {\n\t\ta = b;\n\t\treturn true;\n\t}\n\treturn false;\n}\ntemplate <class T>\ninline bool chmax(T &a, T b) {\n\tif(a < b) {\n\t\ta = b;\n\t\treturn true;\n\t}\n\treturn false;\n}\n\nvoid Yes(bool f = 1) { std::cout << (f ? \"Yes\" : \"No\") << '\\n'; }\nvoid No() { std::cout << \"No\\n\"; }\nvoid YES(bool f = 1) { std::cout << (f ? \"YES\" : \"NO\") << '\\n'; }\nvoid NO() { std::cout << \"NO\\n\"; }\n\ntemplate <class T>\nvoid drop(T answer) {\n\tstd::cout << answer << '\\n';\n\texit(0);\n}\n\nvoid err(bool flag = true) {\n\tif(!flag) return;\n\tstd::cout << -1 << '\\n';\n\texit(0);\n}\n\ntemplate <class T>\nvoid vout(std::vector<T> const &v, bool vertically = 0) {\n\tif(vertically) {\n\t\tfor(auto const &a : v) {\n\t\t\tstd::cout << a << '\\n';\n\t\t}\n\t\treturn;\n\t}\n\tfor(auto it = v.begin(); it != v.end(); it++) {\n\t\tstd::cout << (*it);\n\t\tif(it != v.end() - 1) {\n\t\t\tstd::cout << ' ';\n\t\t}\n\t}\n\tstd::cout << '\\n';\n\treturn;\n}\n\ninline void print() { std::cout << '\\n'; }\ntemplate <class T>\ninline void print(T x) {\n\tstd::cout << x << '\\n';\n\treturn;\n}\ntemplate <typename Head, typename... Tail>\nvoid print(Head H, Tail... T) {\n\tstd::cout << H << \" \";\n\tprint(T...);\n}\n\ntemplate <class T>\nvoid add(std::vector<T> &v, T val) {\n\tfor(auto &a : v) a += val;\n\treturn;\n}\n\ntemplate <class T>\nT dup(T a, T b) {\n\tassert(b != 0);\n\treturn (a + b - 1) / b;\n}\n\ntemplate <class T>\nT greatest_lower_multiple(T x, T d) {\n\tif(d == 0) return 0;\n\tif(d < 0) d *= -1;\n\tT y = x % d;\n\tif(y < 0) y += d;\n\treturn x - y;\n}\n\ntemplate <class T>\nT least_upper_multiple(T x, T d) {\n\treturn -greatest_lower_multiple(-x, d);\n}\n\nlong long POW(long long a, long long n) {\n\tlong long res = 1;\n\twhile(n > 0) {\n\t\tif(n & 1) res = res * a;\n\t\ta = a * a;\n\t\tn >>= 1;\n\t}\n\treturn res;\n}\n\nlong long modpow(long long a, long long n, long long mod) {\t // a^n mod\n\tassert(n >= 0 && mod);\n\tif(mod == 1) return 0LL;\n\tlong long res = 1;\n\ta %= mod;\n\twhile(n > 0) {\n\t\tif(n & 1) res = res * a % mod;\n\t\ta = a * a % mod;\n\t\tn >>= 1;\n\t}\n\treturn res < 0 ? res + mod : res;\n}\n\n// return x which satisfies a * x % mod == gcd(a, mod)\n// not (mod | a)\nlong long modinv(long long a, long long mod) {\n\tlong long b = mod, u = 1, v = 0;\n\twhile(b) {\n\t\tlong long t = a / b;\n\t\ta -= t * b;\n\t\tstd::swap(a, b);\n\t\tu -= t * v;\n\t\tstd::swap(u, v);\n\t}\n\tu %= mod;\n\tif(u < 0) u += mod;\n\treturn u;\n}\n\ntemplate <class T>\nint lb(const std::vector<T> &a, const T x) {\n\treturn std::distance(a.begin(), std::lower_bound(a.begin(), a.end(), x));\n}\ntemplate <class T>\nint ub(const std::vector<T> &a, const T x) {\n\treturn std::distance(a.begin(), std::upper_bound(a.begin(), a.end(), x));\n}\ntemplate <class T>\nvoid unq_sort(std::vector<T> &a) {\n\tstd::sort(a.begin(), a.end());\n\ta.erase(std::unique(a.begin(), a.end()), a.end());\n}\ntemplate <class T>\nstd::vector<int> press(std::vector<T> &a) {\n\tauto vec = a;\n\tunq_sort(vec);\n\tstd::vector<int> ret;\n\tfor(auto &v : a) ret.push_back(lb(vec, v));\n\treturn ret;\n}\n\n#pragma endregion\n\n#pragma region input\n#define VEC(type, name, size) \\\n\tvector<type> name(size); \\\n\tscanner::INPUT(name)\n#define VVEC(type, name, h, w) \\\n\tvector<vector<type>> name(h, vector<type>(w)); \\\n\tscanner::INPUT(name)\n#define INT(...) \\\n\tint __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define LL(...) \\\n\tlong long __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define STR(...) \\\n\tstring __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define CHR(...) \\\n\tchar __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define DBL(...) \\\n\tdouble __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define LD(...) \\\n\tlong double __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define TPL3(type0, type1, type2, name) \\\n\tstd::tuple<type0, type1, type2> name; \\\n\tscanner::INPUT(name);\n#define TPL4(type0, type1, type2, type3, name) \\\n\tstd::tuple<type0, type1, type2, type3> name; \\\n\tscanner::INPUT(name);\n\nnamespace scanner {\ntemplate <class T>\nvoid scan(T &a) {\n\tstd::cin >> a;\n}\n\ntemplate <class T, class L>\nvoid scan(std::pair<T, L> &p) {\n\tscan(p.first);\n\tscan(p.second);\n}\n\ntemplate <class T0, class T1, class T2>\nvoid scan(std::tuple<T0, T1, T2> &p) {\n\tT0 t0;\n\tT1 t1;\n\tT2 t2;\n\tscan(t0);\n\tscan(t1);\n\tscan(t2);\n\tp = std::make_tuple(t0, t1, t2);\n}\n\ntemplate <class T0, class T1, class T2, class T3>\nvoid scan(std::tuple<T0, T1, T2, T3> &p) {\n\tT0 t0;\n\tT1 t1;\n\tT2 t2;\n\tT3 t3;\n\tscan(t0);\n\tscan(t1);\n\tscan(t2);\n\tscan(t3);\n\tp = std::make_tuple(t0, t1, t2, t3);\n}\n\ntemplate <class T>\nvoid scan(std::vector<T> &a) {\n\tfor(auto &i : a) scan(i);\n}\n\nvoid INPUT() {}\ntemplate <class Head, class... Tail>\nvoid INPUT(Head &head, Tail &... tail) {\n\tscan(head);\n\tINPUT(tail...);\n}\n} // namespace scanner\n\ntemplate <typename T1, typename T2>\nstd::istream &operator>>(std::istream &is, std::pair<T1, T2> &p) {\n\tis >> p.first >> p.second;\n\treturn is;\n}\n#pragma endregion\n\n#pragma region debug\n\n#pragma region output\ntemplate <typename T1, typename T2>\nstd::ostream &std::operator<<(std::ostream &os, const std::pair<T1, T2> &p) {\n\tos << p.first << \" \" << p.second;\n\treturn os;\n}\ntemplate <class T>\nstd::ostream &std::operator<<(std::ostream &os, const std::vector<T> &v) {\n\tfor(int i = 0; i < (int)v.size(); i++) {\n\t\tif(i) os << \" \";\n\t\tos << v[i];\n\t}\n\treturn os;\n}\n#pragma endregion\n\n#pragma region view\n\nnamespace viewer {\nvoid view(const long long e) {\n\tif(e == INF)\n\t\tstd::cerr << \"INF\";\n\telse if(e == -INF)\n\t\tstd::cerr << \"-INF\";\n\telse\n\t\tstd::cerr << e;\n}\n\nvoid view(const int e) {\n\tif(e == inf)\n\t\tstd::cerr << \"inf\";\n\telse if(e == -inf)\n\t\tstd::cerr << \"-inf\";\n\telse\n\t\tstd::cerr << e;\n}\n\ntemplate <typename T>\nvoid view(const T e) {\n\tstd::cerr << e;\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::pair<T, U> &p) {\n\tstd::cerr << \"(\";\n\tview(p.first);\n\tstd::cerr << \", \";\n\tview(p.second);\n\tstd::cerr << \")\";\n}\n\ntemplate <class T0, class T1, class T2>\nvoid view(const std::tuple<T0, T1, T2> &p) {\n\tstd::cerr << \"(\";\n\tview(std::get<0>(p));\n\tstd::cerr << \", \";\n\tview(std::get<1>(p));\n\tstd::cerr << \", \";\n\tview(std::get<2>(p));\n\tstd::cerr << \")\";\n}\n\ntemplate <class T0, class T1, class T2, class T3>\nvoid view(const std::tuple<T0, T1, T2, T3> &p) {\n\tstd::cerr << \"(\";\n\tview(std::get<0>(p));\n\tstd::cerr << \", \";\n\tview(std::get<1>(p));\n\tstd::cerr << \", \";\n\tview(std::get<2>(p));\n\tstd::cerr << \", \";\n\tview(std::get<3>(p));\n\tstd::cerr << \")\";\n}\n\ntemplate <typename T>\nvoid view(const std::set<T> &s) {\n\tif(s.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << \"{ \";\n\tfor(auto &t : s) {\n\t\tview(t);\n\t\tstd::cerr << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\n\ntemplate <typename T>\nvoid view(const std::unordered_set<T> &s) {\n\tif(s.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << \"{ \";\n\tfor(auto &t : s) {\n\t\tview(t);\n\t\tstd::cerr << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\n\ntemplate <typename T>\nvoid view(const std::vector<T> &v) {\n\tif(v.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << \"{ \";\n\tfor(const auto &e : v) {\n\t\tview(e);\n\t\tstd::cerr << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\n\ntemplate <typename T>\nvoid view(const std::vector<std::vector<T>> &vv) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &v : vv) {\n\t\tstd::cerr << \"\\t\";\n\t\tview(v);\n\t\tstd::cerr << '\\n';\n\t}\n\tstd::cerr << \"}\";\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::vector<std::pair<T, U>> &v) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &c : v) {\n\t\tstd::cerr << \"\\t(\";\n\t\tview(c.first);\n\t\tstd::cerr << \", \";\n\t\tview(c.second);\n\t\tstd::cerr << \")\\n\";\n\t}\n\tstd::cerr << \"}\";\n}\n\ntemplate <class T0, class T1, class T2>\nvoid view(const std::vector<std::tuple<T0, T1, T2>> &v) {\n\tif(v.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << '{';\n\tfor(const auto &t : v) {\n\t\tstd::cerr << \"\\n\\t\";\n\t\tview(t);\n\t\tstd::cerr << \",\";\n\t}\n\tstd::cerr << \"\\n}\";\n}\n\ntemplate <class T0, class T1, class T2, class T3>\nvoid view(const std::vector<std::tuple<T0, T1, T2, T3>> &v) {\n\tif(v.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << '{';\n\tfor(const auto &t : v) {\n\t\tstd::cerr << \"\\n\\t\";\n\t\tview(t);\n\t\tstd::cerr << \",\";\n\t}\n\tstd::cerr << \"\\n}\";\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::map<T, U> &m) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &t : m) {\n\t\tstd::cerr << \"\\t[\";\n\t\tview(t.first);\n\t\tstd::cerr << \"] : \";\n\t\tview(t.second);\n\t\tstd::cerr << '\\n';\n\t}\n\tstd::cerr << \"}\";\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::unordered_map<T, U> &m) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &t : m) {\n\t\tstd::cerr << \"\\t[\";\n\t\tview(t.first);\n\t\tstd::cerr << \"] : \";\n\t\tview(t.second);\n\t\tstd::cerr << '\\n';\n\t}\n\tstd::cerr << \"}\";\n}\n} // namespace viewer\n\n#pragma endregion\n\n// when debugging : g++ foo.cpp -DLOCAL\n#ifdef LOCAL\nvoid debug_out() {}\ntemplate <typename Head, typename... Tail>\nvoid debug_out(Head H, Tail... T) {\n\tviewer::view(H);\n\tstd::cerr << \", \";\n\tdebug_out(T...);\n}\n#define debug(...) \\\n\tdo { \\\n\t\tstd::cerr << __LINE__ << \" [\" << #__VA_ARGS__ << \"] : [\"; \\\n\t\tdebug_out(__VA_ARGS__); \\\n\t\tstd::cerr << \"\\b\\b]\\n\"; \\\n\t} while(0)\n#define dump(x) \\\n\tdo { \\\n\t\tstd::cerr << __LINE__ << \" \" << #x << \" : \"; \\\n\t\tviewer::view(x); \\\n\t\tstd::cerr << '\\n'; \\\n\t} while(0)\n\n#else\n#define debug(...) (void(0))\n#define dump(x) (void(0))\n#endif\n\n#pragma endregion\n\nstruct bingo {\n\tconst int num_max = 1123456;\n\tvvi bingocard;\n\tint n;\n\tint lines = 0;\n\tvector<pair<int, int>> place;\n\tvvi token;\n\tbingo(vvi v, int m) : lines(0), bingocard(v), n(m), place(num_max, pair(-1, -1)) {\n\t\trep(i, n) rep(j, n) { place[bingocard[i][j]] = pair(i, j); }\n\t\ttoken.assign(m, vi(m, 0));\n\t}\n\n\tint row_is_bingo(int x) {\n\t\trep(col, n) if(!token[x][col]) return 0;\n\t\treturn 1;\n\t}\n\n\tint col_is_bingo(int y) {\n\t\trep(row, n) if(!token[row][y]) return 0;\n\t\treturn 1;\n\t}\n\n\tint slash_is_bingo() {\n\t\trep(i, n) if(!token[i][i]) return 0;\n\t\treturn 1;\n\t}\n\n\tint backslash_is_bingo() {\n\t\trep(i, n) if(!token[i][n - i - 1]) return 0;\n\t\treturn 1;\n\t}\n\n\tvoid draw_a_card(int num) {\n\t\tint x, y;\n\t\ttie(x, y) = place[num];\n\t\tif(x < 0) return;\n\t\ttoken[x][y] = 1;\n\n\t\tlines += row_is_bingo(x);\n\t\tlines += col_is_bingo(y);\n\t\tif(x == y) lines += slash_is_bingo();\n\t\tif(x + y == n - 1) lines += backslash_is_bingo();\n\n\t\tif(n == 1) chmin(lines, 1);\n\t}\n};\n\nint main() {\n\tstd::ios::sync_with_stdio(false);\n\tstd::cin.tie(nullptr);\n\tstd::cout << std::fixed << std::setprecision(15);\n\tsrand((unsigned)time(NULL));\n\n\tint n, u, v, m;\n\tcin >> n >> u >> v >> m;\n\n\tVVEC(int, usagi, n, n);\n\tVVEC(int, neko, n, n);\n\n\tbingo rabbit(usagi, n), cat(neko, n);\n\n\trep(m) {\n\t\tint dr;\n\t\tcin >> dr;\n\t\trabbit.draw_a_card(dr);\n\t\tcat.draw_a_card(dr);\n\t\tif(rabbit.lines >= u) {\n\t\t\tif(cat.lines >= v) {\n\t\t\t\tdrop(\"DRAW\");\n\t\t\t} else {\n\t\t\t\tdrop(\"USAGI\");\n\t\t\t}\n\t\t} else if(cat.lines >= v)\n\t\t\tdrop(\"NEKO\");\n\t}\n\n\tdrop(\"DRAW\");\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 25004, "score_of_the_acc": -0.5779, "final_rank": 8 }, { "submission_id": "aoj_2241_5503307", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntypedef long double ld;\n#define REP(i, n) for (int i = 0; i < (n); ++i)\n#define REPR(i, n) for (int i = n - 1; i >= 0; --i)\n#define FOR(i, m, n) for (int i = m; i < n; ++i)\n#define FORR(i, m, n) for (int i = m; i >= n; --i)\n#define ALL(v) (v).begin(),(v).end()\ntemplate<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }\ntemplate<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }\nconst ll INF=1LL<<60;\nconst int inf=(1<<30)-1;\nconst int mod=1e9+7;\nint dx[8]={1,0,-1,0,-1,-1,1,1};\nint dy[8]={0,1,0,-1,-1,1,-1,1};\nint main(){\n cin.tie(0);\n ios::sync_with_stdio(false);\n int n,u,v,m;cin >> n >> u >> v >> m;\n vector a(2,vector(n,vector(n,0)));\n vector<vector<pair<int,int>>> p(2,vector<pair<int,int>>(1000000,{-1,-1}));\n REP(i,2) REP(j,n) REP(k,n){\n cin >> a[i][j][k];\n a[i][j][k]--;\n p[i][a[i][j][k]]={j,k};\n }\n vector<int> s(m);\n REP(i,m){\n cin >> s[i];\n s[i]--;\n }\n vector<vector<int>> h(2,vector<int>(n)),w(2,vector<int>(n));\n vector<int> d1(2),d2(2);\n REP(t,m){\n vector<int> cnt(2);\n REP(i,2){\n int x=p[i][s[t]].first,y=p[i][s[t]].second;\n if(x==-1) continue;\n h[i][x]++,w[i][y]++;\n if(x+y==n-1) d1[i]++;\n if(x==y) d2[i]++;\n if(h[i][x]==n){\n cnt[i]++;\n h[i][x]=-inf;\n }\n if(w[i][y]==n){\n cnt[i]++;\n w[i][x]=-inf;\n }\n if(d1[i]==n){\n cnt[i]++;\n d1[i]=-inf;\n }\n if(d2[i]==n){\n cnt[i]++;\n d2[i]=-inf;\n }\n }\n if(n==1){\n chmin(cnt[0],1);\n chmin(cnt[1],1);\n }\n u-=cnt[0],v-=cnt[1];\n if(u<=0&&v<=0){\n cout << \"DRAW\" << endl;\n return 0;\n }\n else if(u<=0){\n cout << \"USAGI\" << endl;\n return 0;\n }\n else if(v<=0){\n cout << \"NEKO\" << endl;\n return 0;\n }\n }\n cout << \"DRAW\" << endl;\n}", "accuracy": 0.32786885245901637, "time_ms": 20, "memory_kb": 28344, "score_of_the_acc": -0.7002, "final_rank": 18 }, { "submission_id": "aoj_2241_5503277", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nconst int INF = 1e9;\nconst long long inf = 1LL<<60;\nusing ll = long long;\n\nint main() {\n int n, u, v, m;\n cin >> n >> u >> v >> m;\n map<int, pair<int, int>> usagi, neko;\n for (int i=0; i<n; i++) {\n for (int j=0; j<n; j++) {\n int a;\n cin >> a;\n usagi[a] = {i, j};\n }\n }\n for (int i=0; i<n; i++) {\n for (int j=0; j<n; j++) {\n int a;\n cin >> a;\n neko[a] = {i, j};\n }\n }\n auto f = [&](int t) -> bool {\n if (t == n) return true;\n else return false;\n };\n if (n == 1) {\n int usa_count=0, neko_count=0;\n while (m--) {\n int val; cin >> val;\n if (usagi.count(val)) {\n usa_count++;\n }\n if (neko.count(val)) {\n neko_count++;\n }\n if (usa_count >= u && neko_count >= v) {\n cout << \"DRAW\" << endl;\n return 0;\n } else if (usa_count >= u) {\n cout << \"USAGI\" << endl;\n return 0;\n } else if (neko_count >= v) {\n cout << \"NEKO\" << endl;\n return 0;\n }\n }\n cout << \"DRAW\" << endl;\n return 0;\n }\n vector<int> usagi_yoko(n), usagi_tate(n), neko_yoko(n), neko_tate(n);\n int usagi_naname1=0, usagi_naname2=0, neko_naname1=0, neko_naname2=0;\n int usa_count = 0, neko_count = 0;\n while (m--) {\n int val; cin >> val;\n bool usa = false, nek = false;\n if (usagi.count(val)) {\n auto p = usagi[val];\n int i = p.first, j = p.second;\n usagi_yoko[i]++;\n usagi_tate[j]++;\n if (i == j && usagi_naname1 != -1) usagi_naname1++;\n if (i + j == n-1 && usagi_naname2 != -1) usagi_naname2++;\n if (f(usagi_yoko[i])) usa_count++;\n if (f(usagi_tate[j])) usa_count++;\n if (f(usagi_naname1)) {\n usa_count++;\n usagi_naname1 = -1;\n }\n if (f(usagi_naname2)) {\n usa_count++;\n usagi_naname2 = -1;\n }\n if (usa_count >= u) usa = true;\n // cout << usagi_yoko[i] << \" \" << usagi_tate[j] << \" \" << usagi_naname1 << \" \" << usagi_naname2 << endl;\n }\n if (neko.count(val)) {\n auto p = neko[val];\n int i = p.first, j = p.second;\n neko_yoko[i]++;\n neko_tate[j]++;\n if (i == j && neko_naname1 != -1) neko_naname1++;\n if (i + j == n-1 && neko_naname2 != -1) neko_naname2++;\n if (f(neko_yoko[i])) neko_count++;\n if (f(neko_tate[j])) neko_count++;\n if (f(neko_naname1)) {\n neko_count++;\n neko_naname1 = -1;\n }\n if (f(neko_naname2)) {\n neko_count++;\n neko_naname2 = -1;\n }\n if (neko_count >= v) nek = true;\n }\n // cout << usa_count << \" \" << neko_count << endl;\n if (usa && nek) {\n cout << \"DRAW\" << endl;\n return 0;\n } else if (usa) {\n cout << \"USAGI\" << endl;\n return 0;\n } else if (nek) {\n cout << \"NEKO\" << endl;\n return 0;\n }\n // cout << usa_count << \" \" << neko_count << endl;\n }\n cout << \"DRAW\" << endl;\n}", "accuracy": 1, "time_ms": 120, "memory_kb": 21172, "score_of_the_acc": -0.7947, "final_rank": 10 }, { "submission_id": "aoj_2241_5503177", "code_snippet": "// #pragma GCC optimize(\"Ofast\")\n#include <iostream> // cout, endl, cin\n#include <string> // string, to_string, stoi\n#include <vector> // vector\n#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound\n#include <utility> // pair, make_pair\n#include <tuple> // tuple, make_tuple\n#include <cstdint> // int64_t, int*_t\n#include <cstdio> // printf\n#include <map> // map\n#include <queue> // queue, priority_queue\n#include <set> // set\n#include <stack> // stack\n#include <deque> // deque\n#include <unordered_map> // unordered_map\n#include <unordered_set> // unordered_set\n#include <bitset> // bitset\n#include <cctype> // isupper, islower, isdigit, toupper, tolower\n#include <iomanip> // setprecision\n#include <complex> // complex\n#include <math.h> \n#include <functional>\n#include <cassert>\nusing namespace std;\nusing ll = long long;\nusing P = pair<ll,ll>;\nconstexpr ll INF = 1e18;\nconstexpr ll LLMAX = 9223372036854775807;\nconstexpr int inf = 1e9;\n// constexpr ll mod = 1000000007;\nconstexpr ll mod = 998244353;\n// 右下左上\nconst int dx[8] = {1, 0, -1, 0,1,1,-1,-1};\nconst int dy[8] = {0, 1, 0, -1,1,-1,1,-1};\ntemplate<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }\ntemplate<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }\n#define eol endl\n// ---------------------------------------------------------------------------\n\nint main(){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n int N;\n cin >> N;\n int U,V;\n cin >> U >> V;\n int M;\n cin >> M;\n vector<vector<int>> GU(N,vector<int>(N));\n vector<vector<int>> GN(N,vector<int>(N));\n map<int,vector<P>> mpU,mpN;\n for(int i=0; i<N; i++){\n for(int j=0; j<N; j++){\n cin >> GU[i][j];\n mpU[GU[i][j]].emplace_back(i,j);\n }\n }\n for(int i=0; i<N; i++){\n for(int j=0; j<N; j++){\n cin >> GN[i][j];\n mpN[GN[i][j]].emplace_back(i,j);\n }\n }\n vector<int> cntU_H(N),cntU_W(N);\n vector<int> cntN_H(N),cntN_W(N);\n int nU1 = 0;\n int nU2 = 0;\n int nN1 = 0;\n int nN2 = 0;\n int cntU = 0;\n int cntN = 0;\n vector<int> x(M);\n for(int i=0; i<M; i++){\n cin >> x[i];\n }\n if(N == 1){\n int cntU = 0;\n int cntN = 0;\n for(int i=0; i<M; i++){\n if(GU[0][0] == x[i]){\n cntU = 1;\n }\n if(GN[0][0] == x[i]){\n cntN = 1;\n }\n bool okU = (cntU >= U);\n bool okN = (cntN >= V);\n if(okU && !okN){\n cout << \"USAGI\" << eol;\n return 0;\n }else if(!okU && okN){\n cout << \"NEKO\" << eol;\n return 0;\n }else if(okU && okN){\n cout << \"DRAW\" << eol;\n return 0;\n }\n }\n cout << \"DRAW\" << eol;\n return 0;\n }\n for(int i=0; i<M; i++){\n for(auto m: mpU[x[i]]){\n cntU_H[m.first]++;\n cntU_W[m.second]++;\n if(m.first == m.second) nU1++;\n if(m.first+m.second == N-1) nU2++;\n if(cntU_H[m.first] == N) cntU++;\n if(cntU_W[m.second] == N) cntU++;\n }\n if(nU1 == N){\n cntU++;\n nU1 = 0;\n }\n if(nU2 == N){\n cntU++;\n nU2 = 0;\n }\n for(auto m: mpN[x[i]]){\n cntN_H[m.first]++;\n cntN_W[m.second]++;\n if(m.first == m.second) nN1++;\n if(m.first+m.second == N-1) nN2++;\n if(cntN_H[m.first] == N) cntN++;\n if(cntN_W[m.second] == N) cntN++;\n }\n if(nN1 == N){\n cntN++;\n nN1 = 0;\n } \n if(nN2 == N){\n cntN++;\n nN2 = 0;\n }\n bool okU = (cntU >= U);\n bool okN = (cntN >= V);\n if(okU && !okN){\n cout << \"USAGI\" << eol;\n return 0;\n }else if(!okU && okN){\n cout << \"NEKO\" << eol;\n return 0;\n }else if(okU && okN){\n cout << \"DRAW\" << eol;\n return 0;\n }\n }\n cout << \"DRAW\" << eol;\n return 0;\n}", "accuracy": 1, "time_ms": 160, "memory_kb": 37508, "score_of_the_acc": -1.5357, "final_rank": 16 }, { "submission_id": "aoj_2241_5457664", "code_snippet": "#include <bits/stdc++.h>\n\n#define rep(i, a, n) for (int i = (int)(a); i < (int)(n); i++)\n#define rrep(i, a, n) for (int i = ((int)(n-1)); i >= (int)(a); i--)\n\nint main() {\n int n,m;\n std::vector<int> uv(2);\n std::cin >> n >> uv[0] >> uv[1] >> m;\n int sz = 1000010;\n std::vector table(2, std::vector<std::pair<int,int>>(sz,{-1, -1}));\n rep(i,0,2) {\n rep(j,0,n) {\n rep(k,0,n) {\n int a;\n std::cin >> a;\n table[i][a] = {j,k};\n }\n }\n }\n std::vector x(2, std::vector<int>(n,0)), y(2, std::vector<int>(n,0));\n std::vector z(2, std::vector<int>(2,0));\n std::vector<int> now(2,0);\n std::vector<int> A(m);\n rep(i,0,m) {\n std::cin >> A[i];\n }\n if(n == 1) {\n for(auto a: A) {\n rep(i,0,2) {\n if(table[i][a].first < 0) continue;\n now[i]++;\n }\n if(now[0] >= uv[0]) {\n if(now[1] >= uv[1]) {\n std::cout << \"DRAW\\n\";\n return 0;\n }\n else {\n std::cout << \"USAGI\\n\";\n return 0;\n }\n }\n else if(now[1] >= uv[1]) {\n std::cout << \"NEKO\\n\";\n return 0;\n }\n }\n std::cout << \"DRAW\\n\";\n return 0;\n }\n for(auto a : A) {\n rep(i,0,2) {\n if(table[i][a].first < 0) continue;\n auto [p, q] = table[i][a];\n x[i][p]++;\n y[i][q]++;\n if(x[i][p] == n) {\n\n now[i]++;\n }\n if(y[i][q] == n) { \n now[i]++;\n }\n if(p == q) {\n z[i][0]++;\n }\n if(n-1-p == q) {\n z[i][1]++;\n }\n rep(j,0,2) {\n if(z[i][j] == n) {\n z[i][j] = 0;\n now[i]++;\n }\n }\n }\n if(now[0] >= uv[0]) {\n if(now[1] >= uv[1]) {\n std::cout << \"DRAW\\n\";\n return 0;\n }\n else {\n std::cout << \"USAGI\\n\";\n return 0;\n }\n }\n else if(now[1] >= uv[1]) {\n std::cout << \"NEKO\\n\";\n return 0;\n }\n }\n std::cout << \"DRAW\\n\";\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 26592, "score_of_the_acc": -0.7789, "final_rank": 9 }, { "submission_id": "aoj_2241_5248398", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define INF_LL (int64)1e18\n#define INF (int32)1e9\n#define REP(i, n) for(int64 i = 0;i < (n);i++)\n#define FOR(i, a, b) for(int64 i = (a);i < (b);i++)\n#define all(x) x.begin(),x.end()\n#define fs first\n#define sc second\n\nusing int32 = int_fast32_t;\nusing uint32 = uint_fast32_t;\nusing int64 = int_fast64_t;\nusing uint64 = uint_fast64_t;\nusing PII = pair<int32, int32>;\nusing PLL = pair<int64, int64>;\n\nconst double eps = 1e-10;\n\ntemplate<typename A, typename B>inline void chmin(A &a, B b){if(a > b) a = b;}\ntemplate<typename A, typename B>inline void chmax(A &a, B b){if(a < b) a = b;}\n\ntemplate<typename T>\nvector<T> make_v(size_t a){return vector<T>(a);}\n\ntemplate<typename T,typename... Ts>\nauto make_v(size_t a,Ts... ts){\n\treturn vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));\n}\n\ntemplate<typename T,typename U,typename... V>\ntypename enable_if<is_same<T, U>::value!=0>::type\nfill_v(U &u,const V... v){u=U(v...);}\n\ntemplate<typename T,typename U,typename... V>\ntypename enable_if<is_same<T, U>::value==0>::type\nfill_v(U &u,const V... v){\n\tfor(auto &e:u) fill_v<T>(e,v...);\n}\n\nstruct Board {\n\t vector<int> row, col;\n\t vector<tuple<int, int, int>> v;\n\n\t int n, t;\n\t int xy = 0, yx = 0, cnt = 0;\n\t Board(int n, int t) : n(n), t(t){\n\t row = vector<int>(n, 0);\n col = vector<int>(n, 0);\n\t }\n\n\t void input() {\n\t int b;\n\t REP(i, n) {\n\t REP(j, n) {\n\t cin >> b;\n\t v.emplace_back(b, i, j);\n\t }\n\t }\n\t sort(all(v));\n\t }\n\n\t bool in(int y, int x){\n\t return 0 <= y && y < n && 0 <= x && x < n;\n\t }\n\n\n\t bool draw(int card) {\n\t int64 idx = lower_bound(all(v), tuple<int, int, int>(card, 0, 0)) - v.begin();\n while (idx < v.size() && get<0>(v[idx]) == card) {\n int z, y, x;\n tie(z, y, x) = v[idx];\n\n row[y]++;\n if (n != 1) col[x]++;\n cnt += (row[y] == n) + (col[x] == n);\n if (x == y && n != 1) {\n xy++;\n if (xy == n) cnt++;\n }\n if (x + y == n-1 && n != 1) {\n yx++;\n if (yx == n) cnt++;\n }\n idx++;\n }\n if (cnt >= t) {\n return true;\n }\n return false;\n\t }\n\t};\n\nint main(void) {\n cin.tie(0);\n ios::sync_with_stdio(false);\n\n int64 n, u, v, m;\n cin >> n >> u >> v >> m;\n\n Board b1(n, u), b2(n, v);\n b1.input(); b2.input();\n bool draw = true;\n REP(i, m) {\n int64 c;\n cin >> c;\n bool win[2] = {b1.draw(c), b2.draw(c)};\n if (win[0] && win[1]) {\n cout << \"DRAW\" << endl;\n return 0;\n } else if (!(win[0] || win[1])) {\n continue;\n } else if (win[0]) {\n cout << \"USAGI\" << endl;\n return 0;\n } else {\n cout << \"NEKO\" << endl;\n return 0;\n }\n }\n cout << \"DRAW\" << endl;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 10196, "score_of_the_acc": -0.0714, "final_rank": 1 } ]
aoj_2242_cpp
Problem A: Era Name As many of you know, we have two major calendar systems used in Japan today. One of them is Gregorian calendar which is widely used across the world. It is also known as “Western calendar” in Japan. The other calendar system is era-based calendar, or so-called “Japanese calendar.” This system comes from ancient Chinese systems. Recently in Japan it has been a common way to associate dates with the Emperors. In the era-based system, we represent a year with an era name given at the time a new Emperor assumes the throne. If the era name is “A”, the first regnal year will be “A 1”, the second year will be “A 2”, and so forth. Since we have two different calendar systems, it is often needed to convert the date in one calendar system to the other. In this problem, you are asked to write a program that converts western year to era-based year, given a database that contains association between several western years and era-based years. For the simplicity, you can assume the following: A new era always begins on January 1st of the corresponding Gregorian year. The first year of an era is described as 1. There is no year in which more than one era switch takes place. Please note that, however, the database you will see may be incomplete. In other words, some era that existed in the history may be missing from your data. So you will also have to detect the cases where you cannot determine exactly which era the given year belongs to. Input The input contains multiple test cases. Each test case has the following format: N Q EraName 1 EraBasedYear 1 WesternYear 1 . . . EraName N EraBasedYear N WesternYear N Query 1 . . . Query Q The first line of the input contains two positive integers N and Q (1 ≤ N ≤ 1000, 1 ≤ Q ≤ 1000). N is the number of database entries, and Q is the number of queries. Each of the following N lines has three components: era name, era-based year number and the corresponding western year (1 ≤ EraBasedYear i ≤ WesternYear i ≤ 10 9 ). Each of era names consist of at most 16 Roman alphabet characters. Then the last Q lines of the input specifies queries (1 ≤ Query i ≤ 10 9 ), each of which is a western year to compute era-based representation. The end of input is indicated by a line containing two zeros. This line is not part of any dataset and hence should not be processed. You can assume that all the western year in the input is positive integers, and that there is no two entries that share the same era name. Output For each query, output in a line the era name and the era-based year number corresponding to the western year given, separated with a single whitespace. In case you cannot determine the era, output “ Unknown ” without quotes. Sample Input 4 3 meiji 10 1877 taisho 6 1917 showa 62 1987 heisei 22 2010 1868 1917 1988 1 1 universalcentury 123 2168 2010 0 0 Output for the Sample Input meiji 1 taisho 6 Unknown Unknown
[ { "submission_id": "aoj_2242_11059750", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing a2 = array<ll, 2>;\nusing a3 = array<ll, 3>;\n\nbool chmin(auto& a, const auto& b) { return a > b ? a = b, 1 : 0; }\nbool chmax(auto& a, const auto& b) { return a < b ? a = b, 1 : 0; }\n\nll mod = 998244353;\nconst ll INF = 1e18;\n\nifstream in;\nofstream out;\n\nint main(int argc, char** argv) {\n ios::sync_with_stdio(false);\n cin.tie(0);\n\n if(argc > 2) {\n in.open(argv[1]);\n cin.rdbuf(in.rdbuf());\n out.open(argv[2]);\n cout.rdbuf(out.rdbuf());\n }\n\n while(1) {\n ll n, q;\n cin >> n >> q;\n if(n == 0) break;\n vector<tuple<string, ll, ll>> era(n);\n for(auto& [name, e, w] : era) {\n cin >> name >> e >> w;\n }\n\n for(int i = 0; i < q; i++) {\n ll a;\n cin >> a;\n string s = \"\";\n ll ans;\n for(auto [name, e, w] : era) {\n if(w - e + 1 <= a && a <= w) {\n s = name;\n ans = a - (w - e);\n }\n }\n if(s.size())\n cout << s << ' ' << ans << endl;\n else\n cout << \"Unknown\" << endl;\n }\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 3584, "score_of_the_acc": -0.799, "final_rank": 16 }, { "submission_id": "aoj_2242_10946110", "code_snippet": "#include<stdio.h>\n#include<string.h>\n#include<iostream>\n#include<algorithm>\n#include<queue>\n#include<vector>\n#include<map>\n#include<string>\nusing namespace std;\n\ntypedef int ll;\nconst int inf = 1000000;\n#define N 300005\nint n, m;\nstruct node{\n char s[20]; int x, y;\n}a[1005];\nvoid input(){\n for(int i = 1; i <= n; i++){\n scanf(\"%s %d %d\", a[i].s, &a[i].x, &a[i].y);\n // printf(\"--%s %d %d\\n\", a[i].s, a[i].x, a[i].y);\n }\n}\nint main(){\n\twhile(scanf(\"%d %d\", &n, &m), n+m){\n input();\n while(m--){\n bool ok = false;\n int year ; scanf(\"%d\", &year);\n // printf(\"year : %d\\n\", year);\n for(int i = 1; i <= n; i++)\n {\n if(a[i].y - a[i].x + 1 <= year && year <= a[i].y)\n {\n printf(\"%s %d\\n\", a[i].s, a[i].x - (a[i].y - year));\n ok = true; break;\n }\n }\n if(ok==false)puts(\"Unknown\");\n }\n\t}\n\treturn 0;\n}\n/*\n\n\n*/", "accuracy": 1, "time_ms": 10, "memory_kb": 3584, "score_of_the_acc": -0.4656, "final_rank": 10 }, { "submission_id": "aoj_2242_10888903", "code_snippet": "#include<bits/stdc++.h>\n// #include<atcoder/all>\n// #include<boost/multiprecision/cpp_int.hpp>\n\nusing namespace std;\n// using namespace atcoder;\n// using bint = boost::multiprecision::cpp_int;\nusing ll = long long;\nusing ull = unsigned long long;\nusing ld = long double;\nusing pii = pair<int,int>;\nusing pll = pair<ll,ll>;\nusing vi = vector<ll>;\nusing vvi = vector<vi>;\nusing vvvi = vector<vvi>;\nusing ve = vector<vector<int>>;\nusing vb = vector<bool>;\nusing vvb = vector<vb>;\n#define rep(i,s,n) for(ll i = (ll)s;i < (ll)n;i++)\n#define rrep(i,l,r) for(ll i = r-1;i >= l;i--)\n#define ALL(x) (x).begin(),(x).end()\n#define sz(c) ((ll)(c).size())\n#define LB(A,x) (int)(lower_bound(A.begin(),A.end(),x)-A.begin())\n#define UB(A,x) (int)(upper_bound(A.begin(),A.end(),x)-A.begin())\n// #define MOD 1000000007\n#define MOD 998244353\ntemplate<typename T>using min_priority_queue=priority_queue<T,vector<T>,greater<T>>;\ntemplate<typename T>ostream&operator<<(ostream&os,vector<T>&v){for(int i = 0;i < v.size();i++)os<<v[i]<<(i+1!=v.size()?\" \":\"\");return os;}\ntemplate<typename T>istream&operator>>(istream&is,vector<T>&v){for(T&in:v)is>>in;return is;}\ntemplate<typename T1,typename T2>ostream&operator<<(ostream&os,pair<T1,T2>&p){os<<p.first<<\" \"<<p.second;return os;}\ntemplate<typename T1,typename T2>istream&operator>>(istream&is,pair<T1,T2>&p){is>>p.first>>p.second;return is;}\ntemplate<typename T> inline bool chmax(T &a,T b){if(a < b){a = b;return true;}return false;}\ntemplate<typename T> inline bool chmin(T &a,T b){if(a > b){a = b;return true;}return false;}\nld dist(ld x1,ld y1,ld x2, ld y2){return sqrtl((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));}\n\n\nint main(){\n \n ios_base::sync_with_stdio(0), cin.tie(0);\n while(1){\n int n,Q;cin >> n >> Q;\n if(!n)break;\n vector<pair<int,string>> cor;\n map<string,int> mp;\n rep(i,0,n){\n string s;int a,b;cin >> s >> a >> b;\n cor.emplace_back(b-a+1,s);\n chmax(mp[s],a);\n }\n sort(ALL(cor));cor.erase(unique(ALL(cor)),cor.end());\n while(Q--){\n int x;cin >> x;\n pair<int,string> g = make_pair(x+1,\"Z\");\n int k = LB(cor,g)-1;\n if(k >= 0 && x <= cor[k].first+mp[cor[k].second]-1){\n cout << cor[k].second << \" \" << x-cor[k].first+1 << \"\\n\";\n }else cout << \"Unknown\\n\";\n }\n }\n \n\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3840, "score_of_the_acc": -0.7099, "final_rank": 15 }, { "submission_id": "aoj_2242_10687028", "code_snippet": "/*\n * Package: StandardCodeLibrary.Core\n * */\n//引进常用的头文件并使用std名字空间\n#include <iostream>\n#include <fstream>\n#include <sstream>\n#include <iomanip>\n#include <utility>\n#include <vector>\n#include <list>\n#include <string>\n#include <stack>\n#include <queue>\n#include <deque>\n#include <set>\n#include <map>\n#include <algorithm>\n#include <functional>\n#include <numeric>\n#include <bitset>\n#include <complex>\n#include <cstdio>\n#include <cstring>\n#include <cmath>\n#include <cstdlib>\n#include <ctime>\n#include <climits>\nusing namespace std;\n\n//用于减少代码量的宏\n#define lp for(;;)\n#define repf(i,a,b) for (int i=(a);i<(b);++i)\n#define rrepf(i,a,b) for (int i=(a)-1;i>=(b);--i)\n#define rep(i,n) repf(i,0,n)\n#define rrep(i,n) rrepf(i,n,0)\n#define ft(i,a,b) for (int i=(a);i<=(b);++i)\n#define fdt(i,a,b) for (int i=(a);i>=(b);--i)\n#define for_nonempty_subsets(subset,set) for (int subset=set;subset;subset=(subset-1)&(set))\n#define for_in_charset(i,charset) for (cstr i=(charset);*i;i++)\n#define whl while\n#define rtn return\n#define fl(x,y) memset((x),char(y),sizeof(x))\n#define clr(x) fl(x,char(0))\n#define cpy(x,y) memcpy(x,y,sizeof(x))\n#define sf scanf\n#define pf printf\n#define vec vector\n#define pr pair\n#define que queue\n#define prq priority_queue\n#define itr iterator\n#define x first\n#define y second\n#define pb push_back\n#define mp make_pair\n#define ins insert\n#define ers erase\n#define lb lower_bound\n#define ub upper_bound\n#define rnk order_of_key\n#define sel find_by_order\n#define sz(x) (int((x).size()))\n#define all(x) (x).begin(),(x).end()\n#define srt(x) sort(all(x))\n#define uniq(x) srt(x),(x).erase(unique(all(x)),(x).end())\n#define rev(x) reverse(all(x))\n#define shf(x) random_shuffle(all(x))\n#define nxtp(x) next_permutation(all(x))\n\n//常用数据类型\n#define lli int\ntypedef double db;\ntypedef const char* cstr;\ntypedef string str;\ntypedef vec<int> vi;\ntypedef vec<vi> vvi;\ntypedef vec<bool> vb;\ntypedef vec<vb> vvb;\ntypedef vec<str> vs;\ntypedef pr<int,int> pii;\ntypedef pr<lli,lli> pll;\ntypedef pr<db,db> pdd;\ntypedef map<int,int> mii;\ntypedef map<str,int> msi;\ntypedef map<char,int> mci;\ntypedef set<int> si;\ntypedef set<str> ss;\ntypedef que<int> qi;\ntypedef vec<pii> vpii;\ntypedef vec<pdd> vpdd;\n\n//常用常量:int的最大值;lli的最大值;db的误差相关常数;欧拉常数;圆周率;移动向量;取模使用的除数\nint oo=(~0u)>>1;\nlli ooll=(~0ull)>>1;\ndb inf=1e+10;\ndb eps=1e-10;\ndb gam=0.5772156649015328606;\ndb pi=acos(-1.0);\nint dx[]={1,0,-1,0,1,-1,-1,1,0};\nint dy[]={0,1,0,-1,1,1,-1,-1,0};\nint MOD=1000000007;\n\n//常用函数:最大最小值更新;数学相关函数;输入和输出;树状数组;并查集;可合并堆;\ntemplate<typename type>inline bool cmax(type& a,const type& b){rtn a<b?a=b,true:false;}\ntemplate<typename type>inline bool cmin(type& a,const type& b){rtn b<a?a=b,true:false;}\ntemplate<typename type>inline type sqr(const type& x){rtn x*x;}\ntemplate<typename type>inline type mod(const type& x){rtn x%MOD;}\ninline int sgn(const db& x){rtn (x>+eps)-(x<-eps);}\ninline int dbcmp(const db& a,const db& b){rtn sgn(a-b);}\ntemplate<typename type>inline pr<type,type> operator-(const pr<type,type>& x){rtn mp(-x.x,-x.y);}\ntemplate<typename type>inline pr<type,type> operator+(const pr<type,type>& a,const pr<type,type>& b){rtn mp(a.x+b.x,a.y+b.y);}\ntemplate<typename type>inline pr<type,type> operator-(const pr<type,type>& a,const pr<type,type>& b){rtn mp(a.x-b.x,a.y-b.y);}\ntemplate<typename type>inline pr<type,type> operator*(const pr<type,type>& a,const type& b){rtn mp(a.x*b,a.y*b);}\ntemplate<typename type>inline pr<type,type> operator/(const pr<type,type>& a,const type& b){rtn mp(a.x/b,a.y/b);}\ntemplate<typename type>inline pr<type,type>& operator-=(pr<type,type>& a,const pr<type,type>& b){rtn a=a-b;}\ntemplate<typename type>inline pr<type,type>& operator+=(pr<type,type>& a,const pr<type,type>& b){rtn a=a+b;}\ntemplate<typename type>inline pr<type,type>& operator*=(pr<type,type>& a,const type& b){rtn a=a*b;}\ntemplate<typename type>inline pr<type,type>& operator/=(pr<type,type>& a,const type& b){rtn a=a/b;}\ntemplate<typename type>inline type cross(const pr<type,type>& a,const pr<type,type>& b){rtn a.x*b.y-a.y*b.x;}\ntemplate<typename type>inline type dot(const pr<type,type>& a,const pr<type,type>& b){rtn a.x*b.x+a.y*b.y;}\ntemplate<typename type>inline type gcd(type a,type b){if(b)whl((a%=b)&&(b%=a));rtn a+b;}\ntemplate<typename type>inline type lcm(type a,type b){rtn a*b/gcd(a,b);}\ntemplate<typename istream,typename first_type,typename second_type>inline istream& operator>>(istream& cin,pr<first_type,second_type>& x){rtn cin>>x.x>>x.y;}\ntemplate<typename ostream,typename first_type,typename second_type>inline ostream& operator<<(ostream& cout,const pr<first_type,second_type>& x){rtn cout<<x.x<<\" \"<<x.y;}\ntemplate<typename istream,typename type>inline istream& operator>>(istream& cin,vec<type>& x){rep(i,sz(x))cin>>x[i];rtn cin;}\ntemplate<typename ostream,typename type>inline ostream& operator<<(ostream& cout,const vec<type>& x){rep(i,sz(x))cout<<x[i]<<(i+1==sz(x)?\"\":\" \");rtn cout;}\ninline ostream& pdb(int prcs,db x){rtn cout<<setprecision(prcs)<<fixed<<(sgn(x)?(x):0);}\ntemplate<typename type>inline void bit_inc(vec<type>& st,int x,type inc){whl(x<sz(st))st[x]+=inc,x|=x+1;}\ntemplate<typename type>inline type bit_sum(const vec<type>& st,int x){type s=0;whl(x>=0)s+=st[x],x=(x&(x+1))-1;rtn s;}\ntemplate<typename type>inline type bit_kth(const vec<type>& st,int k){int x=0,y=0,z=0;whl((1<<(++y))<=sz(st));fdt(i,y-1,0){if((x+=1<<i)>sz(st)||z+st[x-1]>k)x-=1<<i;else z+=st[x-1];}rtn x;}\ninline void make_set(vi& st){rep(i,sz(st))st[i]=i;}\ninline int find_set(vi& st,int x){int y=x,z;whl(y!=st[y])y=st[y];whl(x!=st[x])z=st[x],st[x]=y,x=z;rtn y;}\ninline bool union_set(vi& st,int a,int b){a=find_set(st,a),b=find_set(st,b);rtn a!=b?st[a]=b,true:false;}\ntemplate<typename type>inline void merge(type& a,type& b){if(sz(a)<sz(b))swap(a,b);whl(sz(b))a.ins(*b.begin()),b.ers(b.begin());}\n\nint main()\n{\n\tint n,q;\n\twhl(cin>>n>>q,n||q)\n\t{\n\t\tmsi beg,end;\n\t\trep(i,n)\n\t\t{\n\t\t\tstr a;\n\t\t\tint b,c;\n\t\t\tcin>>a>>b>>c;\n\t\t\tbeg[a]=c-b+1;\n\t\t\tcmax(end[a],c);\n\t\t}\n\t\tvec<pr<pii,str> > rec;\n\t\tfor(msi::itr it=beg.begin();it!=beg.end();it++) rec.pb(mp(mp(it->y,end[it->x]),it->x));\n\t\trep(i,q)\n\t\t{\n\t\t\tint qry;\n\t\t\tcin>>qry;\n\t\t\tbool fnd=false;\n\t\t\trep(i,sz(rec)) if (rec[i].x.x<=qry&&qry<=rec[i].x.y)\n\t\t\t{\n\t\t\t\tcout<<rec[i].y<<\" \"<<qry-rec[i].x.x+1<<endl;\n\t\t\t\tfnd=true;\n\t\t\t}\n\t\t\tif (!fnd) cout<<\"Unknown\"<<endl;\n\t\t}\n\t}\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 3616, "score_of_the_acc": -0.8295, "final_rank": 17 }, { "submission_id": "aoj_2242_10506432", "code_snippet": "#include <iostream>\n#include <iomanip>\n#include <cassert>\n#include <vector>\n#include <algorithm>\n#include <utility>\n#include <numeric>\n#include <tuple>\n#include <ranges>\nnamespace ranges = std::ranges;\nnamespace views = std::views;\n// #include \"Src/Number/IntegerDivision.hpp\"\n// #include \"Src/Utility/BinarySearch.hpp\"\n// #include \"Src/Sequence/CompressedSequence.hpp\"\n// #include \"Src/Sequence/RunLengthEncoding.hpp\"\n// #include \"Src/Algebra/Group/AdditiveGroup.hpp\"\n// #include \"Src/DataStructure/FenwickTree/FenwickTree.hpp\"\n// #include \"Src/DataStructure/SegmentTree/SegmentTree.hpp\"\n// using namespace zawa;\n// #include \"atcoder/modint\"\n// using mint = atcoder::modint998244353;\nint N, Q, A[1000], B[1000];\nstd::string S[1000];\nbool solve() {\n std::cin >> N >> Q;\n if (N == 0 and Q == 0) return false;\n for (int i = 0 ; i < N ; i++) {\n std::cin >> S[i] >> A[i] >> B[i];\n }\n while (Q--) {\n int c;\n std::cin >> c;\n int ans = -1;\n for (int i = 0 ; i < N ; i++) {\n if (B[i] - A[i] + 1 <= c and c <= B[i]) {\n ans = i;\n break;\n }\n }\n if (ans == -1) {\n std::cout << \"Unknown\\n\";\n }\n else {\n const int y = A[ans] - (B[ans] - c);\n std::cout << S[ans] << ' ' << y << '\\n';\n }\n }\n return true;\n}\nint main() {\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n std::ios::sync_with_stdio(false);\n while (solve()) ;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3712, "score_of_the_acc": -0.5878, "final_rank": 14 }, { "submission_id": "aoj_2242_10238888", "code_snippet": "#include <vector>\n#include <iostream>\nusing namespace std;\n\nint main() {\n\twhile (true) {\n\t\tint N, Q;\n\t\tcin >> N >> Q;\n\t\tif (N == 0 && Q == 0) {\n\t\t\tbreak;\n\t\t}\n\t\tvector<string> S(N);\n\t\tvector<int> A(N), B(N);\n\t\tfor (int i = 0; i < N; i++) {\n\t\t\tcin >> S[i] >> A[i] >> B[i];\n\t\t}\n\t\tfor (int i = 0; i < Q; i++) {\n\t\t\tint x;\n\t\t\tcin >> x;\n\t\t\tpair<string, int> v = {\"Unknown\", -1};\n\t\t\tfor (int j = 0; j < N; j++) {\n\t\t\t\tif (B[j] - A[j] + 1 <= x && x <= B[j]) {\n\t\t\t\t\tv = {S[j], x - (B[j] - A[j])};\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (v.second != -1) {\n\t\t\t\tcout << v.first << ' ' << v.second << endl;\n\t\t\t} else {\n\t\t\t\tcout << v.first << endl;\n\t\t\t}\n\t\t}\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3492, "score_of_the_acc": -0.489, "final_rank": 12 }, { "submission_id": "aoj_2242_9575300", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n#include <unistd.h>\n\nnamespace fastio {\nstatic constexpr uint32_t SZ = 1 << 17;\nchar ibuf[SZ];\nchar obuf[SZ];\nchar out[100];\n// pointer of ibuf, obuf\n\n\nuint32_t pil = 0, pir = 0, por = 0;\n\nstruct Pre {\n char num[10000][4];\n constexpr Pre() : num() {\n for (int i = 0; i < 10000; i++) {\n int n = i;\n for (int j = 3; j >= 0; j--) {\n num[i][j] = n % 10 | '0';\n n /= 10;\n }\n }\n }\n} constexpr pre;\n\ninline void load() {\n memmove(ibuf, ibuf + pil, pir - pil);\n pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin);\n pil = 0;\n if (pir < SZ)\n ibuf[pir++] = '\\n';\n}\n\ninline void flush() {\n fwrite(obuf, 1, por, stdout);\n por = 0;\n}\n\nvoid rd(char &c) {\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n}\n\nvoid rd(string &x) {\n x.clear();\n char c;\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n do {\n x += c;\n if (pil == pir)\n load();\n c = ibuf[pil++];\n } while (!isspace(c));\n}\n\ntemplate <typename T> void rd_real(T &x) {\n string s;\n rd(s);\n x = stod(s);\n}\n\ntemplate <typename T> void rd_integer(T &x) {\n if (pil + 100 > pir)\n load();\n char c;\n do\n c = ibuf[pil++];\n while (c < '-');\n bool minus = 0;\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (c == '-') {\n minus = 1, c = ibuf[pil++];\n }\n }\n x = 0;\n while ('0' <= c) {\n x = x * 10 + (c & 15), c = ibuf[pil++];\n }\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (minus)\n x = -x;\n }\n}\n\nvoid rd(int &x) {\n rd_integer(x);\n}\nvoid rd(ll &x) {\n rd_integer(x);\n}\nvoid rd(i128 &x) {\n rd_integer(x);\n}\nvoid rd(uint &x) {\n rd_integer(x);\n}\nvoid rd(ull &x) {\n rd_integer(x);\n}\nvoid rd(u128 &x) {\n rd_integer(x);\n}\nvoid rd(double &x) {\n rd_real(x);\n}\nvoid rd(long double &x) {\n rd_real(x);\n}\n\ntemplate <class T, class U> void rd(pair<T, U> &p) {\n return rd(p.first), rd(p.second);\n}\ntemplate <size_t N = 0, typename T> void rd_tuple(T &t) {\n if constexpr (N < std::tuple_size<T>::value) {\n auto &x = std::get<N>(t);\n rd(x);\n rd_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void rd(tuple<T...> &tpl) {\n rd_tuple(tpl);\n}\n\ntemplate <size_t N = 0, typename T> void rd(array<T, N> &x) {\n for (auto &d : x)\n rd(d);\n}\ntemplate <class T> void rd(vector<T> &x) {\n for (auto &d : x)\n rd(d);\n}\n\nvoid read() {}\ntemplate <class H, class... T> void read(H &h, T &...t) {\n rd(h), read(t...);\n}\n\nvoid wt(const char c) {\n if (por == SZ)\n flush();\n obuf[por++] = c;\n}\nvoid wt(const string s) {\n for (char c : s)\n wt(c);\n}\nvoid wt(const char *s) {\n size_t len = strlen(s);\n for (size_t i = 0; i < len; i++)\n wt(s[i]);\n}\n\ntemplate <typename T> void wt_integer(T x) {\n if (por > SZ - 100)\n flush();\n if (x < 0) {\n obuf[por++] = '-', x = -x;\n }\n int outi;\n for (outi = 96; x >= 10000; outi -= 4) {\n memcpy(out + outi, pre.num[x % 10000], 4);\n x /= 10000;\n }\n if (x >= 1000) {\n memcpy(obuf + por, pre.num[x], 4);\n por += 4;\n } else if (x >= 100) {\n memcpy(obuf + por, pre.num[x] + 1, 3);\n por += 3;\n } else if (x >= 10) {\n int q = (x * 103) >> 10;\n obuf[por] = q | '0';\n obuf[por + 1] = (x - q * 10) | '0';\n por += 2;\n } else\n obuf[por++] = x | '0';\n memcpy(obuf + por, out + outi + 4, 96 - outi);\n por += 96 - outi;\n}\n\ntemplate <typename T> void wt_real(T x) {\n ostringstream oss;\n oss << fixed << setprecision(15) << double(x);\n string s = oss.str();\n wt(s);\n}\n\nvoid wt(int x) {\n wt_integer(x);\n}\nvoid wt(ll x) {\n wt_integer(x);\n}\nvoid wt(i128 x) {\n wt_integer(x);\n}\nvoid wt(uint x) {\n wt_integer(x);\n}\nvoid wt(ull x) {\n wt_integer(x);\n}\nvoid wt(u128 x) {\n wt_integer(x);\n}\nvoid wt(double x) {\n wt_real(x);\n}\nvoid wt(long double x) {\n wt_real(x);\n}\n\ntemplate <class T, class U> void wt(const pair<T, U> val) {\n wt(val.first);\n wt(' ');\n wt(val.second);\n}\ntemplate <size_t N = 0, typename T> void wt_tuple(const T t) {\n if constexpr (N < std::tuple_size<T>::value) {\n if constexpr (N > 0) {\n wt(' ');\n }\n const auto x = std::get<N>(t);\n wt(x);\n wt_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void wt(tuple<T...> tpl) {\n wt_tuple(tpl);\n}\ntemplate <class T, size_t S> void wt(const array<T, S> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\ntemplate <class T> void wt(const vector<T> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\n\nvoid print() {\n wt('\\n');\n}\ntemplate <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {\n wt(head);\n if (sizeof...(Tail))\n wt(' ');\n print(forward<Tail>(tail)...);\n}\nvoid __attribute__((destructor)) _d() {\n flush();\n}\n} // namespace fastio\n\n\nusing fastio::flush;\nusing fastio::print;\nusing fastio::read;\n\ninline void first(bool i = true) {\n print(i ? \"first\" : \"second\");\n}\ninline void Alice(bool i = true) {\n print(i ? \"Alice\" : \"Bob\");\n}\ninline void Takahashi(bool i = true) {\n print(i ? \"Takahashi\" : \"Aoki\");\n}\ninline void yes(bool i = true) {\n print(i ? \"yes\" : \"no\");\n}\ninline void Yes(bool i = true) {\n print(i ? \"Yes\" : \"No\");\n}\ninline void No() {\n print(\"No\");\n}\ninline void YES(bool i = true) {\n print(i ? \"YES\" : \"NO\");\n}\ninline void NO() {\n print(\"NO\");\n}\ninline void Yay(bool i = true) {\n print(i ? \"Yay!\" : \":(\");\n}\ninline void Possible(bool i = true) {\n print(i ? \"Possible\" : \"Impossible\");\n}\ninline void POSSIBLE(bool i = true) {\n print(i ? \"POSSIBLE\" : \"IMPOSSIBLE\");\n}\n\n/**\n * @brief Fast IO\n */\n\nint main() {\nwhile(1) {\n int N, Q;\n cin >> N >> Q;\n if (N == 0 && Q == 0) return 0;\n vector<string> S(N);\n vector<int> A(N), B(N);\n rep(i,0,N) cin >> S[i] >> A[i] >> B[i];\n rep(i,0,Q) {\n int C;\n cin >> C;\n bool flag = false;\n rep(j,0,N) {\n if (B[j]-A[j] < C && C <= B[j]) {\n cout << S[j] << ' ' << C - (B[j]-A[j]) << endl;\n flag = true;\n }\n }\n if (!flag) cout << \"Unknown\" << endl;\n }\n}\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3540, "score_of_the_acc": -0.5348, "final_rank": 13 }, { "submission_id": "aoj_2242_8986119", "code_snippet": "// (⁠◕⁠ᴗ⁠◕⁠✿⁠)\n\n// #pragma GCC target(\"avx2\")\n// #pragma GCC optimize(\"O3\")\n// #pragma GCC optimize(\"unroll-loops\")\n#include <bits/stdc++.h>\n#define rep(i, n) for (ll i = 0; i < (n); i++)\n#define srep(i, s, n) for (ll i = s; i < (n); i++)\n#define len(x) ((int)(x).size())\n#define all(x) (x).begin(), (x).end()\nusing namespace std;\ntemplate<typename T> using vc = vector<T>;\ntemplate<typename T> using vv = vc<vc<T>>;\ntemplate<typename T> using vvv = vv<vc<T>>;\nusing vi = vc<int>;using vvi = vv<int>; using vvvi = vv<vi>;\nusing ll = long long;using vl = vc<ll>;using vvl = vv<ll>; using vvvl = vv<vl>;\nusing ld = long double; using vld = vc<ld>; using vvld = vc<vld>; using vvvld = vc<vvld>;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nconst ld pi = 3.141592653589793;\nconst int inf = 0x3f3f3f3f;\nconst ll INF = 0x3f3f3f3f3f3f3f3f;\n// const ll mod = 1000000007;\nconst ll mod = 998244353;\ninline bool inside(ll y, ll x, ll H, ll W) {return 0 <= (y) and (y) < (H) and 0 <= (x) and (x) < (W); }\n\n#define debug(var) do{std::cout << #var << \" : \\n\";view(var);}while(0)\ntemplate<typename T> void view(T e){cout << e << endl;}\ntemplate<typename T> void view(const vc<T>& v){for(const auto& e : v){ cout << e << \" \"; } cout << endl;}\ntemplate<typename T> void view(const vv<T>& vv){ for(const auto& v : vv){ view(v); } }\n\nvc<pair<string, int>> solve(int N, int Q){\n vc<tuple<string, int, int>> period(N);\n rep(i, N){\n string s; int a, b; cin >> s >> a >> b;\n period[i] = {s, b - a + 1, b};\n }\n vc<pair<string, int>> ans(Q, {\"Unknown\", 0});\n rep(i, Q){\n int t; cin >> t;\n for (auto [s, l, r] : period){\n if (l <= t && t <= r){\n ans[i] = {s, t - l + 1};\n }\n }\n }\n return ans;\n}\n\nint main(){\n vc<vc<pair<string, int>>> ans;\n while (true){\n int N, Q; cin >> N >> Q;\n if (N == 0) break;\n ans.push_back(solve(N, Q));\n }\n for (auto x : ans){\n for (auto [a, b] : x){\n if (a == \"Unknown\") cout << a << endl;\n else cout << a << \" \" << b << endl;\n }\n }\n}", "accuracy": 1, "time_ms": 160, "memory_kb": 4144, "score_of_the_acc": -1.8333, "final_rank": 20 }, { "submission_id": "aoj_2242_8319737", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nint main(){\n int n, m, y;\n while(cin >> n >> m, n){\n string s[1010];\n int a[1010], b[1010];\n for(int i=0;i<n;i++) cin >> s[i] >> a[i] >> b[i];\n for(int i=0;i<m;i++){\n bool ok = false;\n cin >> y;\n for(int j=0;!ok&&j<n;j++){\n if(b[j]-a[j]<y && y<=b[j]){\n cout << s[j] << \" \" << y-b[j]+a[j] << endl;\n ok = true;\n }\n }\n if(!ok) cout << \"Unknown\" << endl;\n }\n }\n return(0);\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 3096, "score_of_the_acc": -0.2222, "final_rank": 2 }, { "submission_id": "aoj_2242_7982514", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\ntemplate <class T>\nusing V = vector<T>;\n\n#define foa(s, v) for (auto &s : v)\n#define all(v) v.begin(), v.end()\n#define sz(v) int(v.size())\n#define rep(i, n) for (int i = 0; i < int(n); i++)\n\n// template <class T>\n// bool chmin(T &a, T b){return a > b ? a = b, 1 : 0};\n// template <class T>\n// bool chmax(T &a, T b){return a < b ? a = b, 1 : 0};\n\nint n;\nvoid solve()\n{\n int q;\n cin >> q;\n vector<tuple<ll, ll, string>> eras(n);\n\n rep(i, n)\n {\n ll len, end;\n string c;\n cin >> c >> len >> end;\n ll start = end - len + 1;\n eras.emplace_back(start, end, c);\n }\n\n auto query = [&](ll val) -> string\n {\n for (auto [st, e, c] : eras)\n {\n if (st <= val && val <= e)\n {\n return c + \" \" + to_string(val - st + 1);\n }\n }\n return \"Unknown\";\n };\n\n rep(j, q)\n {\n ll s;\n cin >> s;\n cout << query(s) << endl;\n }\n\n return;\n}\n\nint main()\n{\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n while (cin >> n && n)\n solve();\n}", "accuracy": 1, "time_ms": 190, "memory_kb": 3644, "score_of_the_acc": -1.5229, "final_rank": 19 }, { "submission_id": "aoj_2242_7967735", "code_snippet": "#include <bits/stdc++.h>\n\n#define MT make_tuple\n#define MP make_pair\n#define PB push_back\n#define ALL(c) (c).begin(), (c).end()\n\nusing namespace std;\n\nint main() {\n\tint n, q;\n\twhile(cin >> n >> q && n) { \n\t\tstring name;\n\t\tint start, end, len;\n\t\tvector<tuple<int, int, string> > t;\n\t\t\n\t\tfor(int i=0;i<n;i++) { \n\t\t\tcin >> name >> len >> end;\n\t\t\tt.PB(MT(end-len+1, len, name));\t\n\t\t}\n\n\t\tsort(ALL(t));\n\t\tint query, index;\n\t\tfor(int i=0;i<q;i++) {\n\t\t\tcin >> query;\n\t\t\tindex = n-1;\n\t\t\tfor(int j=0;j<n;j++) {\n\t\t\t\tif(get<0>(t[j]) > query) {\n\t\t\t\t\tindex = j-1;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(index == -1) {\n\t\t\t\tcout << \"Unknown\" << endl;\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\t\t\n\t\t\tstart = get<0>(t[index]);\n\t\t\tlen = get<1>(t[index]);\n\t\t\tname = get<2>(t[index]);\n\t\t\t\n\t\t\tif(query-start < len) {\n\t\t\t\tcout << name << \" \" << query-start+1 << endl;\n\t\t\t} else {\n\t\t\t\tcout << \"Unknown\" << endl;\n\t\t\t}\t\t\n\t\t}\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3212, "score_of_the_acc": -0.1662, "final_rank": 1 }, { "submission_id": "aoj_2242_7799048", "code_snippet": "#include <iostream>\n#include <algorithm>\n#include <string>\n#include <map>\nusing namespace std;\nint n, q;\nmap<long long, long long>my;\nmap<long long, string>mn;\nint a[2000];\n\nvoid solve(){\n\tfor(int i = 0; i < n; i++){\n\t\tlong long yi; string si;\n\t\tcin >> si >> yi >> a[i];\n\t\tmy[a[i]] = yi;\n\t\tmn[a[i]] = si;\n\t}\n\tsort(a, a+n);\n\twhile(q--){\n\t\tint y; cin >> y;\n\t\tint l = -1, r = n; // a[l] < y <= a[r]\n\t\twhile(r - l > 1){\n\t\t\tint mid = (l + r)/2;\n\t\t\tif(y <= a[mid]) r = mid;\n\t\t\telse l = mid;\n\t\t}\n\t\tif(r == n)cout << \"Unknown\\n\";\n\t\telse if(a[r] - my[a[r]] + 1 <= y && y <= a[r])cout << mn[a[r]] << ' ' << y - (a[r] - my[a[r]]) << \"\\n\";\n\t\telse cout << \"Unknown\\n\";\n\t}\n}\n\nint main(int argc, char **argv){\n\twhile(cin >> n >> q){\n\t\tif(!q)break;\n\t\tmy.clear();\n\t\tmn.clear();\n\t\tsolve();\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3476, "score_of_the_acc": -0.4737, "final_rank": 11 }, { "submission_id": "aoj_2242_7164350", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i, n) for (int i = 0; i < (int)(n); i++)\n#define repn(i,end) for(int i = 0; i <= (int)(end); i++)\n#define reps(i,start,end) for(int i = start; i < (int)(end); i++)\n#define repsn(i,start,end) for(int i = start; i <= (int)(end); i++)\n#define ll long long\n#define print(t) cout << t << endl \n#define all(a) (a).begin(),(a).end()\n// << std::fixed << std::setprecision(0)\nconst ll INF = 1LL << 60;\n \ntemplate<class T> void chmin(T& a, T b){\n if(a > b){\n a = b;\n }\n}\n \ntemplate<class T> void chmax(T& a, T b){\n if(a < b){\n a = b;\n }\n}\n \nll lpow(ll x,ll n){\n ll ans = 1;\n while(n >0){\n if(n & 1)ans *= x;\n x *= x;\n n >>= 1;\n }\n return ans;\n}\n// for AOJ or ICPC or etc..\ntemplate<class Tp>\nbool zero (const Tp &x) {\n return x == 0;\n}\n\ntemplate<class Tp, class... Args>\nbool zero (const Tp &x, const Args& ...args) {\n return zero(x) and zero(args...);\n}\n\nint main(){\n while(true){\n ll n,q;cin >> n >> q;\n if(zero(n,q))break;\n vector<tuple<ll,ll,string>> era;\n rep(i,n){\n string s; ll a,b;cin >> s >> a >> b;\n era.push_back({b-a+ 1,b,s});\n }\n sort(all(era));\n // rep(i,n)cout << era[i].first << \" \" << era[i].second << endl;\n\n rep(i,q){\n ll m;cin >> m;\n ll ng = -1;\n ll ok = n-1; //配列サイズ\n while(abs(ok-ng) > 1){\n // cout << ok << \" \" << ng << endl;\n ll mid = (ok + ng)/2;\n auto [a,b,c] = era[mid];\n if(a <= m && m<= b){\n ok = mid;\n break;\n }\n if(b < m){\n ng = mid;\n }else{\n ok = mid;\n }\n }\n auto[a,b,c] = era[ok];\n if(ok == 0 && a > m){\n cout << \"Unknown\" << endl;\n }else{\n if(a <= m && m <= b){ \n cout << c << \" \"<< m - a+ 1 << endl; \n }else{\n cout << \"Unknown\" << endl;\n }\n }\n }\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3436, "score_of_the_acc": -0.38, "final_rank": 8 }, { "submission_id": "aoj_2242_7020919", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing ll=long long;\nint main(void){\n ll n,q;\n while(cin>>n>>q,n){\n ll a[1000],b[1000],t;\n string s[1000];\n for(ll i=0;i<n;i++){\n cin>>s[i]>>a[i]>>b[i];\n }\n while(q--){\n ll c=0;\n cin>>t;\n for(ll i=0;i<n;i++){\n if(b[i]>=t && b[i]-a[i]<t){\n cout<<s[i]<<\" \"<<a[i]-b[i]+t<<endl;\n c++;\n }\n }\n if(!c){\n cout<<\"Unknown\\n\";\n }\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3244, "score_of_the_acc": -0.2523, "final_rank": 3 }, { "submission_id": "aoj_2242_6773289", "code_snippet": "#include <bits/stdc++.h>\n#define rep(i, n) for (int i = 0; i < n; ++i)\ntypedef long long ll;\nusing namespace std;\n\nvoid solve(int N, int Q) {\n vector<tuple<string, int, int>> A(N);\n rep(i, N) {\n string s;\n int a, b;\n cin >> s >> a >> b;\n A[i] = {s, a, b};\n }\n\n while (Q--) {\n int year;\n cin >> year;\n\n bool flag = false;\n rep(i, N) {\n auto &[name, len, end] = A[i];\n if (end - len < year && year <= end) {\n cout << name << \" \" << len - end + year << \"\\n\";\n flag = true;\n break;\n }\n }\n if (!flag)\n cout << \"Unknown\\n\";\n }\n}\n\nint main() {\n cin.tie(0);\n ios_base::sync_with_stdio(false);\n\n int N, Q;\n while (cin >> N >> Q) {\n if (N == 0 && Q == 0) break;\n solve(N, Q);\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3404, "score_of_the_acc": -0.2939, "final_rank": 6 }, { "submission_id": "aoj_2242_6277306", "code_snippet": "#include <bits/stdc++.h>\n//#include<atcoder/all>\n//using namespace atcoder;\nusing namespace std;\nusing ll = long long;\n#define all(A) A.begin(),A.end()\nusing vll = vector<ll>;\nusing v2ll = vector<vll>;\nusing v3ll = vector<v2ll>;\nusing v4ll = vector<v3ll>;\n#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)\n\n\n\nint main() {\n while(1){\n ll N,Q;\n cin>>N>>Q;\n if(N==0)return 0;\n vector<tuple<string,ll,ll>> E(N);\n rep(i,N){\n string S;\n ll A,B;\n cin>>S>>A>>B;\n E[i]={S,A,B};\n }\n rep(q,Q){\n ll P;\n cin>>P;\n bool C=true;\n rep(n,N){\n if(P>get<2>(E[n]))continue;\n if(P>get<2>(E[n])-get<1>(E[n])){\n cout<<get<0>(E[n])<<\" \";\n cout<<P-(get<2>(E[n])-get<1>(E[n]))<<endl;\n C=false;\n break;\n }\n }\n if(C)cout<<\"Unknown\"<<endl;\n }\n }\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3256, "score_of_the_acc": -0.2638, "final_rank": 4 }, { "submission_id": "aoj_2242_5950329", "code_snippet": "#include <bits/stdc++.h>\n#define all(v) v.begin(), v.end()\n#define rall(v) v.rbegin(), v.rend()\n#define rep(i,n) for(int i=0;i<(int)(n);i++)\n#define drep(i,j,n) for(int i=0;i<(int)(n-1);i++)for(int j=i+1;j<(int)(n);j++)\n#define trep(i,j,k,n) for(int i=0;i<(int)(n-2);i++)for(int j=i+1;j<(int)(n-1);j++)for(int k=j+1;k<(int)(n);k++)\n#define codefor int test;scanf(\"%d\",&test);while(test--)\n#define INT(...) int __VA_ARGS__;in(__VA_ARGS__)\n#define LL(...) ll __VA_ARGS__;in(__VA_ARGS__)\n#define yes(ans) if(ans)printf(\"yes\\n\");else printf(\"no\\n\")\n#define Yes(ans) if(ans)printf(\"Yes\\n\");else printf(\"No\\n\")\n#define YES(ans) if(ans)printf(\"YES\\n\");else printf(\"NO\\n\")\n#define popcount(v) __builtin_popcount(v)\n#define vector2d(type,name,h,...) vector<vector<type>>name(h,vector<type>(__VA_ARGS__))\n#define vector3d(type,name,h,w,...) vector<vector<vector<type>>>name(h,vector<vector<type>>(w,vector<type>(__VA_ARGS__)))\n#define umap unordered_map\n#define uset unordered_set\nusing namespace std;\nusing ll = long long;\nconst int MOD=1000000007;\nconst int MOD2=998244353;\nconst int INF=1<<30;\nconst ll INF2=(ll)1<<60;\nvoid scan(int& a){scanf(\"%d\",&a);}\nvoid scan(long long& a){scanf(\"%lld\",&a);}\ntemplate<class T,class L>void scan(pair<T, L>& p){scan(p.first);scan(p.second);}\ntemplate<class T,class U,class V>void scan(tuple<T,U,V>& p){scan(get<0>(p));scan(get<1>(p));scan(get<2>(p));}\ntemplate<class T> void scan(T& a){cin>>a;}\ntemplate<class T> void scan(vector<T>& vec){for(auto&& it:vec)scan(it);}\nvoid in(){}\ntemplate <class Head, class... Tail> void in(Head& head, Tail&... tail){scan(head);in(tail...);}\nvoid print(const int& a){printf(\"%d\",a);}\nvoid print(const long long& a){printf(\"%lld\",a);}\nvoid print(const double& a){printf(\"%.15lf\",a);}\ntemplate<class T,class L>void print(const pair<T, L>& p){print(p.first);putchar(' ');print(p.second);}\ntemplate<class T> void print(const T& a){cout<<a;}\ntemplate<class T> void print(const vector<T>& vec){if(vec.empty())return;print(vec[0]);for(auto it=vec.begin();++it!= vec.end();){putchar(' ');print(*it);}}\nvoid out(){putchar('\\n');}\ntemplate<class T> void out(const T& t){print(t);putchar('\\n');}\ntemplate <class Head, class... Tail> void out(const Head& head,const Tail&... tail){print(head);putchar(' ');out(tail...);}\ntemplate<class T> void dprint(const T& a){cerr<<a;}\ntemplate<class T> void dprint(const vector<T>& vec){if(vec.empty())return;cerr<<vec[0];for(auto it=vec.begin();++it!= vec.end();){cerr<<\" \"<<*it;}}\nvoid debug(){cerr<<endl;}\ntemplate<class T> void debug(const T& t){dprint(t);cerr<<endl;}\ntemplate <class Head, class... Tail> void debug(const Head& head, const Tail&... tail){dprint(head);cerr<<\" \";debug(tail...);}\nll intpow(ll a, ll b){ ll ans = 1; while(b){ if(b & 1) ans *= a; a *= a; b /= 2; } return ans; }\nll modpow(ll a, ll b, ll p){ ll ans = 1; while(b){ if(b & 1) (ans *= a) %= p; (a *= a) %= p; b /= 2; } return ans; }\nll modinv(ll a, ll m) {ll b = m, u = 1, v = 0;while (b) {ll t = a / b;a -= t * b; swap(a, b);u -= t * v; swap(u, v);}u %= m;if (u < 0) u += m;return u;}\nll updivide(ll a,ll b){if(a%b==0) return a/b;else return (a/b)+1;}\ntemplate<class T> void chmax(T &a,const T b){if(b>a)a=b;}\ntemplate<class T> void chmin(T &a,const T b){if(b<a)a=b;}\n\nint main(){\n while(1){\n INT(n,q);\n if(n==0&&q==0)return 0;\n vector<tuple<int,int,string>> a;\n string s;\n int l,r;\n rep(i,n){\n in(s,l,r);\n a.emplace_back(r-l+1,r,s);\n }\n sort(all(a));\n while(q--){\n INT(y);\n string ans;\n int ans2=-1;\n rep(i,n){\n tie(l,r,s)=a[i];\n if(l<=y&&y<=r){\n ans=s;\n ans2=y-l+1;\n }\n }\n if(ans2==-1)out(\"Unknown\");\n else out(ans,ans2);\n }\n }\n}", "accuracy": 1, "time_ms": 130, "memory_kb": 3560, "score_of_the_acc": -1.1094, "final_rank": 18 }, { "submission_id": "aoj_2242_5947347", "code_snippet": "#include <bits/stdc++.h>\n#define rep(i,n) for(int i = 0; i < (n); i++)\nusing namespace std;\ntypedef long long ll;\n\nint main(){\n cin.tie(0);\n ios::sync_with_stdio(0);\n \n while(true) {\n int N,Q; cin >> N >> Q; if(N == 0) break;\n vector<string> S(N);\n vector<int> E(N), W(N);\n rep(i,N) cin >> S[i] >> E[i] >> W[i];\n\n rep(_,Q) {\n int q; cin >> q;\n string str = \"Unknown\";\n int ans = -1;\n rep(i,N) {\n if(W[i] - E[i] + 1 <= q && q <= W[i]) {\n str = S[i];\n ans = E[i] - W[i] + q;\n }\n }\n if(str == \"Unknown\") cout << str;\n else cout << str << \" \" << ans; cout << endl;\n }\n }\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3260, "score_of_the_acc": -0.2676, "final_rank": 5 }, { "submission_id": "aoj_2242_5883938", "code_snippet": "//#define _GLIBCXX_DEBUG\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing P = pair<int,int>;\nusing PL = pair<ll,ll>;\nusing PPL = pair<pair<ll,ll>,ll>;\n#define INF 1000000000\n#define INFLL 1000000000000000000\n#define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++)\n#define repp(i, n, s) for (int i = (int)(n); i >= (int)(s); i--)\n#define mp make_pair\n#define tp make_tuple\n#define ALL(vec) vec.begin(), vec.end()\ntemplate <class T> inline bool chmax(T& a,T b) {if(a<b){a=b;return true;}return false;}\ntemplate <class T> inline bool chmin(T& a,T b) {if(a>b){a=b;return true;}return false;}\nll mod = 1000000007;\nll mod2 = 998244353;\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n\n while (1) {\n ll n,m;cin>>n>>m;\n if (n==0 && m==0) {\n break;\n }\n vector<pair<string, pair<ll,ll>>> a(n);\n rep(i,0,n) {\n string a1;cin>>a1;\n ll a2,a3;cin>>a2>>a3;\n a[i] =mp(a1, mp(a3-a2+1, a3));\n }\n rep(i,0,m) {\n ll q;cin>>q;\n string ans_str = \"\";\n ll ans_num = 0;\n rep(j,0,n) {\n if (a[j].second.first <= q && q <= a[j].second.second) {\n ans_str = a[j].first;\n ans_num = q - a[j].second.first + 1;\n break;\n }\n }\n\n if (ans_num == 0) {\n cout << \"Unknown\" << endl;\n } else {\n cout << ans_str << \" \" << ans_num << endl;\n }\n }\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3416, "score_of_the_acc": -0.3609, "final_rank": 7 }, { "submission_id": "aoj_2242_5883845", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nconst int INF = 1e9;\nconst long long inf = 1LL<<60;\nusing ll = long long;\n\nbool solve() {\n int n, q; cin >> n >> q;\n if (n == 0) return false;\n vector<int> y;\n map<int, string> m;\n map<int, int> d;\n for (int i=0; i<n; i++) {\n string s;\n int a, b;\n cin >> s >> a >> b;\n d[b] = a;\n m[b] = s;\n y.push_back(b);\n }\n sort(y.begin(), y.end());\n while (q--) {\n int p; cin >> p;\n if (p > y.back()) {\n cout << \"Unknown\" << endl;\n continue;\n }\n int cur = lower_bound(y.begin(), y.end(), p) - y.begin();\n if (y[cur] - (d[y[cur]]-1) <= p && p <= y[cur]) {\n cout << m[y[cur]] << \" \" << p - (y[cur] - (d[y[cur]]-1)) + 1 << endl;\n } else {\n cout << \"Unknown\" << endl;\n }\n }\n return true;\n}\n\nint main() {\n while (solve());\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3456, "score_of_the_acc": -0.3991, "final_rank": 9 } ]
aoj_2245_cpp
Problem D: Dice Room You are playing a popular video game which is famous for its depthful story and interesting puzzles. In the game you were locked in a mysterious house alone and there is no way to call for help, so you have to escape on yours own. However, almost every room in the house has some kind of puzzles and you cannot move to neighboring room without solving them. One of the puzzles you encountered in the house is following. In a room, there was a device which looked just like a dice and laid on a table in the center of the room. Direction was written on the wall. It read: "This cube is a remote controller and you can manipulate a remote room, Dice Room, by it. The room has also a cubic shape whose surfaces are made up of 3x3 unit squares and some squares have a hole on them large enough for you to go though it. You can rotate this cube so that in the middle of rotation at least one edge always touch the table, that is, to 4 directions. Rotating this cube affects the remote room in the same way and positions of holes on the room change. To get through the room, you should have holes on at least one of lower three squares on the front and back side of the room." You can see current positions of holes by a monitor. Before going to Dice Room, you should rotate the cube so that you can go though the room. But you know rotating a room takes some time and you don’t have much time, so you should minimize the number of rotation. How many rotations do you need to make it possible to get though Dice Room? Input The input consists of several datasets. Each dataset contains 6 tables of 3x3 characters which describe the initial position of holes on each side. Each character is either ' * ' or ' . '. A hole is indicated by ' * '. The order which six sides appears in is: front, right, back, left, top, bottom. The order and orientation of them are described by this development view: Figure 4: Dice Room Figure 5: available dice rotations There is a blank line after each dataset. The end of the input is indicated by a single ' # '. Output Print the minimum number of rotations needed in a line for each dataset. You may assume all datasets have a solution. Sample Input ... ... .*. ... ... .*. ... ... ... ... ... .*. ... ... .*. ... ... ... ... .*. ... *.. ... ..* *.* *.* *.* *.* .*. *.* *.. .*. ..* *.* ... *.* ... .*. .*. ... .** *.. ... ... .*. .*. ... *.. ..* ... .** ... *.. ... # Output for the Sample Input 3 1 0
[ { "submission_id": "aoj_2245_4879027", "code_snippet": "#include \"iostream\"\n#include \"climits\"\n#include \"list\"\n#include \"queue\"\n#include \"stack\"\n#include \"set\"\n#include \"functional\"\n#include \"algorithm\"\n#include \"string\"\n#include \"map\"\n#include \"unordered_map\"\n#include \"unordered_set\"\n#include \"iomanip\"\n#include \"cmath\"\n#include \"random\"\n#include \"bitset\"\n#include \"cstdio\"\n#include \"numeric\"\n#include \"cassert\"\n#include \"ctime\"\n\nusing namespace std;\n\nconstexpr long long int MOD = 1000000007;\n//constexpr int MOD = 1000000007;\n//constexpr int MOD = 998244353;\n//constexpr long long int MOD = 998244353;\nconstexpr double EPS = 1e-12;\n\n//int N, M, K, T, H, W, L, R;\nlong long int N, M, K, T, H, W, L, R;\n\nint main() {\n\tios::sync_with_stdio(false);\n\tcin.tie(0);\n\n\tvector<vector<string>>s(6, vector<string>(3));\n\t//\twhile (getline(cin, s[0][0]), s[0][0].size() >= 3) {\n\twhile (cin >> s[0][0], s[0][0].size() >= 3) {\n\t\tfor (int i = 0; i < 6; i++) {\n\t\t\tfor (int j = 0; j < 3; j++) {\n\t\t\t\tif (!i && !j)continue;\n\t\t\t\tcin >> s[i][j];\n\t\t\t}\n\t\t}\n\t\tmap<vector<vector<string>>, int>dis;\n\t\tdis[s] = 0;\n\t\tqueue<vector<vector<string>>>Q;\n\t\tQ.push(s);\n\t\twhile (!Q.empty()) {\n\t\t\tauto t = Q.front();\n\t\t\t//cout << \"dice\" << endl;\n\t\t\t//for (auto i : t) {\n\t\t\t//\tfor (auto j : i)cout << j << endl;\n\t\t\t//}\n\t\t\tQ.pop();\n\t\t\t{\n\t\t\t\tauto nt = t;\n\t\t\t\tfor (int i = 0; i < 3; i++) {\n\t\t\t\t\tfor (int j = 0; j < 3; j++) {\n\t\t\t\t\t\tnt[0][i][j] = t[0][j][2 - i];\n\t\t\t\t\t\tnt[1][i][j] = t[4][j][2 - i];\n\t\t\t\t\t\tnt[2][i][j] = t[2][2 - j][i];\n\t\t\t\t\t\tnt[3][i][j] = t[5][j][2 - i];\n\t\t\t\t\t\tnt[4][i][j] = t[3][j][2 - i];\n\t\t\t\t\t\tnt[5][i][j] = t[1][j][2 - i];\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif (dis.find(nt)==dis.end()) {\n\t\t\t\t\tdis[nt] = dis[t] + 1;\n\t\t\t\t\tQ.push(nt);\n\t\t\t\t}\n\t\t\t\tauto nnt = nt;\n\t\t\t\t{\n\t\t\t\t\tfor (int loop = 0; loop < 2; loop++) {\n\t\t\t\t\t\tfor (int i = 0; i < 3; i++) {\n\t\t\t\t\t\t\tfor (int j = 0; j < 3; j++) {\n\t\t\t\t\t\t\t\tnnt[0][i][j] = nt[0][j][2 - i];\n\t\t\t\t\t\t\t\tnnt[1][i][j] = nt[4][j][2 - i];\n\t\t\t\t\t\t\t\tnnt[2][i][j] = nt[2][2 - j][i];\n\t\t\t\t\t\t\t\tnnt[3][i][j] = nt[5][j][2 - i];\n\t\t\t\t\t\t\t\tnnt[4][i][j] = nt[3][j][2 - i];\n\t\t\t\t\t\t\t\tnnt[5][i][j] = nt[1][j][2 - i];\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t\tnt = nnt;\n\t\t\t\t\t}\n\t\t\t\t\tif (dis.find(nnt) == dis.end()) {\n\t\t\t\t\t\tdis[nnt] = dis[t] + 1;\n\t\t\t\t\t\tQ.push(nt);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\t{\n\t\t\t\tauto nt = t;\n\t\t\t\tfor (int i = 0; i < 3; i++) {\n\t\t\t\t\tfor (int j = 0; j < 3; j++) {\n\t\t\t\t\t\tnt[0][i][j] = t[5][i][j];\n\t\t\t\t\t\tnt[1][i][j] = t[1][j][2 - i];\n\t\t\t\t\t\tnt[2][i][j] = t[4][2 - i][2 - j];\n\t\t\t\t\t\tnt[3][i][j] = t[3][2 - j][i];\n\t\t\t\t\t\tnt[4][i][j] = t[0][i][j];\n\t\t\t\t\t\tnt[5][i][j] = t[2][2 - i][2 - j];\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif (dis.find(nt) == dis.end()) {\n\t\t\t\t\tdis[nt] = dis[t] + 1;\n\t\t\t\t\tQ.push(nt);\n\t\t\t\t}\n\t\t\t\tauto nnt = nt;\n\t\t\t\t{\n\t\t\t\t\tfor (int loop = 0; loop < 2; loop++) {\n\t\t\t\t\t\tfor (int i = 0; i < 3; i++) {\n\t\t\t\t\t\t\tfor (int j = 0; j < 3; j++) {\n\t\t\t\t\t\t\t\tnnt[0][i][j] = nt[5][i][j];\n\t\t\t\t\t\t\t\tnnt[1][i][j] = nt[1][j][2 - i];\n\t\t\t\t\t\t\t\tnnt[2][i][j] = nt[4][2 - i][2 - j];\n\t\t\t\t\t\t\t\tnnt[3][i][j] = nt[3][2 - j][i];\n\t\t\t\t\t\t\t\tnnt[4][i][j] = nt[0][i][j];\n\t\t\t\t\t\t\t\tnnt[5][i][j] = nt[2][2 - i][2 - j];\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t\tnt = nnt;\n\t\t\t\t\t}\n\t\t\t\t\tif (dis.find(nnt) == dis.end()) {\n\t\t\t\t\t\tdis[nnt] = dis[t] + 1;\n\t\t\t\t\t\tQ.push(nt);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tint ans = MOD;\n\t\tfor (auto i : dis) {\n\t\t\tif ((i.first[0][2][0] == '*' || i.first[0][2][1] == '*' || i.first[0][2][2] == '*') && (i.first[2][2][0] == '*' || i.first[2][2][1] == '*' || i.first[2][2][2] == '*')) {\n\t\t\t\tans = min(ans, i.second);\n\t\t\t}\n\t\t}\n\t\tcout << ans << endl;\n\t}\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3256, "score_of_the_acc": -1, "final_rank": 7 }, { "submission_id": "aoj_2245_744766", "code_snippet": "#include <iostream>\n#include <sstream>\n#include <string>\n#include <algorithm>\n#include <vector>\n#include <stack>\n#include <queue>\n#include <set>\n#include <map>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <cmath>\n#include <cassert>\n\nusing namespace std;\n\n#define FOR(i,k,n) for(int i=(k); i<(int)(n); ++i)\n#define REP(i,n) FOR(i,0,n)\n#define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)\n\ntemplate<class T> void debug(T begin, T end){ for(T i = begin; i != end; ++i) cerr<<*i<<\" \"; cerr<<endl; }\ninline bool valid(int x, int y, int W, int H){ return (x >= 0 && y >= 0 && x < W && y < H); }\n\ntypedef long long ll;\nconst int INF = 100000000;\nconst double EPS = 1e-8;\nconst int MOD = 1000000007;\nint dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};\nint dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};\n\nstruct State{\n string grid[6][3];\n bool operator < (const State& s) const {\n REP(i, 6) REP(j, 3) if(grid[i][j] != s.grid[i][j]) return grid[i][j] < s.grid[i][j];\n return false;\n }\n};\nbool is_goal(State& s){\n return s.grid[0][2].find(\"*\") != string::npos && s.grid[2][2].find(\"*\") != string::npos;\n}\n\nvoid copy(string src[3], string dst[3]){\n REP(i, 3) dst[i] = src[i];\n}\n\nvoid rotate(string s[6][3], int a, int b, int c, int d){\n a--; b--; c--; d--;\n // a -> b -> c -> d\n string tmp[3];\n copy(s[d], tmp);\n copy(s[c], s[d]);\n copy(s[b], s[c]);\n copy(s[a], s[b]);\n copy(tmp, s[a]);\n}\n\nvoid revolve90(string s[3]){\n string tmp[3];\n copy(s, tmp);\n REP(y, 3) REP(x, 3){\n s[y][x] = tmp[2 - x][y];\n }\n}\n\nvoid revolve(string s[6][3], int k, int t){\n REP(_, t) revolve90(s[k - 1]);\n}\n\nState rotate(State s, int r){\n if(r >= 2){\n return rotate(rotate(rotate(s, r - 2), r - 2), r - 2);\n }\n State next;\n REP(i, 6) copy(s.grid[i], next.grid[i]);\n if(r == 0) rotate(next.grid, 1, 5, 3, 6);\n if(r == 1) rotate(next.grid, 2, 5, 4, 6);\n if(r == 0) {\n revolve(next.grid, 3, 2);\n revolve(next.grid, 6, 2);\n\n revolve(next.grid, 2, 1);\n revolve(next.grid, 4, 3);\n }\n if(r == 1){\n revolve(next.grid, 5, 1);\n revolve(next.grid, 4, 1);\n revolve(next.grid, 6, 1);\n revolve(next.grid, 2, 1);\n\n revolve(next.grid, 1, 3);\n revolve(next.grid, 3, 1);\n }\n return next;\n}\n\nvoid print(State s){\n REP(i, 6){\n printf(\"--- %d ---\\n\", i + 1);\n REP(j, 3) cout << s.grid[i][j] << endl;\n }\n}\n\nint main(){\n while(true){\n State start;\n REP(i, 6) REP(j, 3) cin >> start.grid[i][j];\n if(cin.eof()) break;\n map<State, int> dist;\n queue<State> que;\n dist[start] = 0;\n que.push(start);\n while(!que.empty()){\n State s = que.front(); que.pop();\n if(is_goal(s)){\n cout << dist[s] << endl;\n break;\n }\n REP(r, 4){\n State next = rotate(s, r);\n if(!dist.count(next)){\n dist[next] = dist[s] + 1;\n que.push(next);\n }\n }\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1256, "score_of_the_acc": -0.3857, "final_rank": 4 }, { "submission_id": "aoj_2245_740114", "code_snippet": "#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <cmath>\n#include <cctype>\n#include <vector>\n#include <stack>\n#include <queue>\n#include <map>\n#include <algorithm>\n#include <iostream>\n#include <string>\n#include <set>\n#define X first\n#define Y second\n#define sqr(x) (x)*(x)\nusing namespace std;\nconst double PI = acos(-1.0);\nmap<int,int>::iterator it;\ntypedef long long LL ;\n#pragma comment(linker,\"/STACK:102400000,102400000\")\n\nstruct face\n{\n int p[3][3];\n face rotate_left()\n {\n face ret;\n for(int i=0;i<3;++i)\n {\n for(int j=0;j<3;++j)\n {\n ret.p[2-j][i]=p[i][j];\n }\n }\n return ret;\n }\n face rotate_right()\n {\n face ret = rotate_left();\n ret = ret.rotate_left();\n ret = ret.rotate_left();\n return ret;\n }\n void input()\n {\n char s[5][5];\n for(int i=0;i<3;++i)\n {\n scanf(\"%s\",s[i]);\n }\n for(int i=0;i<3;++i)\n {\n for(int j=0;j<3;++j)\n {\n p[i][j]=(s[i][j]=='.')?0:1;\n }\n }\n }\n void pf()\n {\n for(int i=0;i<3;++i)\n {\n for(int j=0;j<3;++j)\n {\n printf(\"%d\",p[i][j]);\n }\n puts(\"\");\n }\n }\n};\n\n\n\n\nstruct cube\n{\n face f[8];\n int dis;\n cube left()\n {\n cube ret ;\n ret.f[0] = (*this).f[0].rotate_left();\n ret.f[1] = (*this).f[5].rotate_right();\n ret.f[2] = (*this).f[2].rotate_right();\n ret.f[3] = (*this).f[4].rotate_right();\n ret.f[4] = (*this).f[1].rotate_right();\n ret.f[5] = (*this).f[3].rotate_right();\n return ret;\n }\n cube right()\n {\n cube ret;\n ret.f[0] = (*this).f[0].rotate_right();\n ret.f[1] = (*this).f[4].rotate_left();\n ret.f[2] = (*this).f[2].rotate_left();\n ret.f[3] = (*this).f[5].rotate_left();\n ret.f[4] = (*this).f[1].rotate_left();\n ret.f[5] = (*this).f[3].rotate_left();\n return ret;\n }\n cube forward()\n {\n cube ret;\n ret.f[0] = (*this).f[5];\n ret.f[1] = (*this).f[1].rotate_right();\n ret.f[2] = (*this).f[4].rotate_left();\n ret.f[2] = ret.f[2].rotate_left();\n ret.f[3] = (*this).f[3].rotate_left();\n ret.f[4] = (*this).f[0];\n ret.f[5] = (*this).f[2].rotate_left();\n ret.f[5] = ret.f[5].rotate_left();\n return ret;\n }\n cube back()\n {\n cube ret = (*this).forward();\n ret = ret.forward();\n ret = ret.forward();\n return ret;\n }\n bool good()\n {\n bool ok1=0,ok2=0;\n for(int i=0;i<3;++i)\n {\n if(f[0].p[2][i]==1)ok1=1;\n if(f[2].p[2][i]==1)ok2=1;\n }\n return ok1&&ok2;\n }\n void pf()\n {\n for(int i=0;i<6;++i)\n {\n f[i].pf();\n }\n }\n};\ncube C;\nint res;\nvoid dfs(cube u,int depth)\n{\n if(depth>9)return ;\n if(u.good())\n {\n //printf(\"d = %d\\n\",depth);\n res = min(res,depth);\n }\n else\n {\n cube v = u.back();\n dfs(v,depth+1);\n v = u.forward();\n dfs(v,depth+1);\n v = u.left();\n dfs(v,depth+1);\n v = u.right();\n dfs(v,depth+1);\n }\n}\nint main()\n{\n char s[8][8];\n// face ff;\n// ff.input();\n// ff.pf();\n// ff=ff.rotate_left();\n// ff.pf();\n// ff=ff.rotate_left();\n// ff.pf();\n\n while(1)\n {\n scanf(\"%s\",s[0]);\n if(s[0][0]=='#')break;\n for(int i=1;i<3;++i)scanf(\"%s\",s[i]);\n for(int i=0;i<3;++i)\n {\n for(int j=0;j<3;++j)\n {\n C.f[0].p[i][j]=(s[i][j]=='.')?0:1;\n }\n }\n for(int i=1;i<6;++i)C.f[i].input();\n //puts(\"OK\");\n res=10000;\n //C.f[0].pf();\n //C.f[2].pf();\n// C.pf();\n// C = C.left();\n// puts(\"\");\n// C.pf();\n// C = C.right();\n// puts(\"\");\n// C.pf();\n dfs(C,0);\n printf(\"%d\\n\",res);\n }\n\treturn 0;\n}", "accuracy": 1, "time_ms": 1190, "memory_kb": 1148, "score_of_the_acc": -1.3526, "final_rank": 8 }, { "submission_id": "aoj_2245_662753", "code_snippet": "#include<iostream>\n#include<algorithm>\n#include<vector>\n#include<queue>\n#include<cassert>\n#define REP(i,s,n) for(int i=s;i<n;i++)\n#define rep(i,n) REP(i,0,n)\nusing namespace std;\n\nstring Message[] = {\"Front\",\"Right\",\"Back\",\"Left\",\"Top\",\"Bottom\"};\nint dx[] = {0,0,1,-1};\nint dy[] = {1,-1,0,0};\n\nclass Box\n{\npublic:\n vector<vector<string> > box;\n int used; \n\n Box(){\n used = 0;\n box.resize(6);\n for(int i=0;i<6;i++)box[i].resize(3);\n }\n\n vector<string> rotate90(vector<string> &x)\n {\n vector<string> ret = x;\n for(int i=0;i<3;i++)\n for(int j=0;j<3;j++)\n\tret[j][2-i] = x[i][j];\n\n return ret;\n }\n\n vector<string> rotate270(vector<string> &x)\n {\n vector<string> ret = x;\n for(int i=0;i<3;i++)\n ret = rotate90(ret);\n return ret;\n }\n\n vector<vector<string> > R1(vector<vector<string> > &x)\n {\n vector<vector<string> > r = x;\n string pre[3];\n for(int i=0;i<3;i++)pre[i] = r[2][i];\n\n //2 <- 4\n for(int i=0;i<3;i++)\n for(int j=0;j<3;j++)\n\tr[2][2-i][2-j] = r[4][i][j];\n\n //4 <- 0\n for(int i=0;i<3;i++)\n for(int j=0;j<3;j++)\n\tr[4][i][j] = r[0][i][j];\n\n //0 <- 5\n for(int i=0;i<3;i++)\n for(int j=0;j<3;j++)\n\tr[0][i][j] = r[5][i][j];\n\n //5 <- 2\n for(int i=0;i<3;i++)\n for(int j=0;j<3;j++)\n\tr[5][2-i][2-j] = pre[i][j];\n\n r[1] = rotate90(r[1]);\n r[3] = rotate270(r[3]);\n\n return r;\n }\n\n vector<vector<string> > R2(vector<vector<string> > &x)\n {\n vector<vector<string> > r = x;\n for(int i=0;i<3;i++)\n r = R1(r);\n return r;\n }\n\n vector<vector<string> > R3(vector<vector<string> > &x)\n {\n vector<vector<string> > r = x;\n string pre[3];\n for(int i=0;i<3;i++)pre[i] = r[4][i];\n\n //4 <- 1\n for(int i=0;i<3;i++)\n for(int j=0;j<3;j++)\n\tr[4][2-j][i] = r[1][i][j];\n\n //1 <- 5\n for(int i=0;i<3;i++)\n for(int j=0;j<3;j++)\n\tr[1][2-j][i] = r[5][i][j];\n\n\n //5 <- 3\n for(int i=0;i<3;i++)\n for(int j=0;j<3;j++)\n\tr[5][2-j][i] = r[3][i][j];\n\n\n //3 <- 4\n for(int i=0;i<3;i++)\n for(int j=0;j<3;j++)\n\tr[3][2-j][i] = pre[i][j];\n\n\n r[0] = rotate270(r[0]);\n r[2] = rotate90(r[2]);\n return r;\n }\n\n vector<vector<string> > R4(vector<vector<string> > &x)\n {\n vector<vector<string> > ret = x;\n for(int i=0;i<3;i++)\n ret = R3(ret);\n return ret;\n }\n\n\n void print()\n {\n cout << \"print--------------\" << endl;\n for(int k=0;k<6;k++)\n {\n\tcout << Message[k] << \"*************\" << endl;\n\tfor(int i=0;i<3;i++)\n\t {\n\t for(int j=0;j<3;j++)\n\t {\n\t\tcout << box[k][i][j];\n\t }\n\t cout << endl;\n\t }\n }\n cout << endl;\n }\n\n bool isOK(int x)\n {\n queue<pair<int,int> > que;\n bool used[3][3];\n for(int i=0;i<3;i++)for(int j=0;j<3;j++)used[i][j] = false;\n if(box[x][0][0] != '*')\n que.push(pair<int,int>(0,0)),used[0][0] = true;\n if(box[x][0][1] != '*')\n que.push(pair<int,int>(1,0)),used[0][1] = true;\n if(box[x][0][2] != '*')\n que.push(pair<int,int>(2,0)),used[0][2] = true;\n\n while(!que.empty())\n {\n\tpair<int,int> p = que.front(); que.pop();\n\tif(p.second == 2)return true;\n\tfor(int i=0;i<4;i++)\n\t {\n\t int nx = p.first + dx[i];\n\t int ny = p.second + dy[i];\n\t if(!(0 <= nx && nx < 3 && 0 <= ny && ny < 3))continue;\n\t if(used[ny][nx])continue;\n\t used[ny][nx] = true;\n\t que.push(pair<int,int>(nx,ny));\n\t }\n\n }\n return false;\n }\n\n bool isFin()\n {\n bool p1 = false;\n bool p2 = false;\n if(box[0][2][0] == '*')p1 = true;\n if(box[0][2][1] == '*')p1 = true;\n if(box[0][2][2] == '*')p1 = true;\n if(box[2][2][0] == '*')p2 = true;\n if(box[2][2][1] == '*')p2 = true;\n if(box[2][2][2] == '*')p2 = true;\n\n return p1 & p2;\n }\n\n};\n\ntypedef pair<Box,int> P;\n\nint main()\n{\n while(true)\n {\n Box box;\n rep(k,6)\n\t{\n\t rep(i,3)\n\t {\n\t string line;\n\t cin >> line;\t\n\t box.box[k][i] = line;\n\t if(line == \"#\")\n\t\t{\n\t\t assert(i == 0 && k == 0);\n\t\t goto F;\t\t\n\t\t}\n\t }\n\t}\n\n bool found = false;\n\t queue<P> que;\n\t que.push(P(box,0));\n\t while(!que.empty())\n\t {\n\t P p = que.front(); que.pop();\n\t if(p.first.isFin())\n\t\t{\n\t\t found = true;\n\t\t cout << p.second << endl;\n\t\t break;\n\t\t}\n\n\t for(int i=0;i<4;i++)\n\t\t{\n\t\t if((p.first.used >> i) & 1)continue;\n\t\t Box bx = p.first;\n\t\t if(i == 0)\n\t\t bx.box = bx.R1(bx.box),p.first.used <<= (1<<i);\n\t\t else if(i == 1)\n\t\t bx.box = bx.R2(bx.box),p.first.used <<= (1<<i);\n\t\t else if(i == 2)\n\t\t bx.box = bx.R3(bx.box),p.first.used <<= (1<<i);\n\t\t else if(i == 3)\n\t\t bx.box = bx.R4(bx.box),p.first.used <<= (1<<i);\n\t\t que.push(P(bx,p.second+1));\n\t\t}\n\t }\n\t assert(found);\n\n }\n F:;\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 1352, "score_of_the_acc": -0.4237, "final_rank": 6 }, { "submission_id": "aoj_2245_643322", "code_snippet": "#include<iostream>\n#include<sstream>\n#include<algorithm>\n#include<set>\n#include<map>\n#include<queue>\n#include<complex>\n#include<cstdio>\n#include<cstdlib>\n#include<cstring>\n#include<cassert>\n\n#define rep(i,n) for(int i=0;i<(int)n;i++)\n#define all(c) (c).begin(),(c).end()\n#define mp make_pair\n#define pb push_back\n#define each(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)\n#define dbg(x) cerr<<__LINE__<<\": \"<<#x<<\" = \"<<(x)<<endl\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef vector<int> vi;\ntypedef pair<int,int> pi;\nconst int inf = (int)1e9;\nconst double INF = 1e12, EPS = 1e-9;\n\nconst int r[][6] ={\n\t{1, 2, 3, 0, 4, 5},\n\t{5, 1, 4, 3, 0, 2},\n\t{0, 4, 2, 5, 3, 1}\n};\nvector<vector<string> >in;\nvoid rot(int it, vector<string> &f){\n\twhile(it--){\n\t\tvector<string> nx(3, string(\"...\"));\n\t\trep(i, 3) rep(j, 3) nx[i][j] = f[2 - j][i];\n\t\tswap(nx, f);\n\t}\n}\nvector<vector<string> > rot(int d, const vector<vector<string> > &in){\n\tvector<vector<string> > res;\n\trep(i, 6) res.pb(in[r[d][i]]);\n\tif(d == 0){\n\t\trot(1, res[4]); rot(3, res[5]);\n\t}\n\tif(d == 1){\n\t\trot(1, res[1]); rot(3, res[3]); rot(2, res[5]); rot(2, res[2]);\n\t}\n\tif(d == 2){\n\t\trot(1, res[0]); rot(3, res[2]); rot(1, res[1]); rot(1, res[4]); rot(1, res[3]); rot(1, res[5]);\n\t}\n\treturn res;\n}\n\nint main(){\n\tin = vector<vector<string> >(6, vector<string>(3, \"...\"));\n\twhile(1){\n\t\trep(i, 6) rep(j, 3){\n\t\t\tcin >> in[i][j];\n\t\t\tif(in[i][j] == \"#\") exit(0);\n\t\t}\n\t\t\n\t\tset<vector<vector<string> > > s;\n\t\tqueue<pair<int, vector<vector<string> > > > q;\n\t\tq.push(mp(0, in));\n\t\twhile(!q.empty()){\n\t\t\tin = q.front().second;\n\t\t\tint c = q.front().first; q.pop();\n\t\t\tif(s.count(in)) continue;\n\t\t\ts.insert(in);\n\t\t\t\n\t\t\tbool ok1 = 0, ok2 = 0;\n\t\t\trep(i, 3){\n\t\t\t\tok1 |= in[0][2][i] == '*';\n\t\t\t\tok2 |= in[2][2][i] == '*';\n\t\t\t}\n\t\t\tif(ok1 && ok2){\n\t\t\t\tcout << c << endl;\n\t\t\t\tgoto END;\n\t\t\t}\n\t\t\t\n\t\t\tfor(int d = 1; d < 3; d++) rep(it, 4){\n\t\t\t\tin = rot(d, in);\n\t\t\t\tif(it == 0 || it == 2) q.push(mp(c + 1, in));\n\t\t\t}\n\t\t}\n\t\tcout << -1 << endl;\n\t\tEND:;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1300, "score_of_the_acc": -0.3993, "final_rank": 5 }, { "submission_id": "aoj_2245_458810", "code_snippet": "#include <cstdio>\n#include <iostream>\n#include <sstream>\n#include <iomanip>\n#include <algorithm>\n#include <cmath>\n#include <string>\n#include <vector>\n#include <list>\n#include <queue>\n#include <stack>\n#include <set>\n#include <map>\n#include <bitset>\n#include <numeric>\n#include <climits>\n#include <cfloat>\nusing namespace std;\n\nvector<string> rotate(int n, const vector<string>& s)\n{\n if(n == 0)\n return s;\n\n vector<string> t(3, string(3, ' '));\n for(int i=0; i<3; ++i){\n for(int j=0; j<3; ++j){\n t[j][2-i] = s[i][j];\n }\n }\n return rotate(n-1, t);\n}\n\nclass Dice\n{\npublic:\n vector<vector<string> > pip; // 東南西北上下の目\n Dice(){\n pip.assign(6, vector<string>(3, string(3, ' ')));\n }\n void roll(int d){ // 東南西北右左に回転\n int dir[][4] = {{4,2,5,0},{4,3,5,1},{4,0,5,2},{4,1,5,3},{0,3,2,1},{0,1,2,3}};\n vector<string> tmp = pip[dir[d][0]];\n for(int i=0; i<3; ++i)\n pip[dir[d][i]] = pip[dir[d][i+1]];\n pip[dir[d][3]] = tmp;\n\n if(d == 0){\n pip[1] = rotate(3, pip[1]);\n pip[2] = rotate(2, pip[2]);\n pip[3] = rotate(1, pip[3]);\n pip[5] = rotate(2, pip[5]);\n }else if(d == 1){\n pip[0] = rotate(1, pip[0]);\n pip[2] = rotate(3, pip[2]);\n }else if(d == 2){\n pip[0] = rotate(2, pip[0]);\n pip[1] = rotate(1, pip[1]);\n pip[3] = rotate(3, pip[3]);\n pip[5] = rotate(2, pip[5]);\n }else{\n pip[0] = rotate(3, pip[0]);\n pip[2] = rotate(1, pip[2]);\n }\n }\n bool operator==(const Dice& d) const{\n return pip == d.pip;\n }\n};\n\nint solve(Dice d0)\n{\n set<vector<vector<string> > > s;\n queue<vector<vector<string> > > q;\n s.insert(d0.pip);\n q.push(d0.pip);\n\n int ret = 0;\n for(;;){\n int n = q.size();\n while(--n >= 0){\n Dice d;\n d.pip= q.front();\n q.pop();\n\n int tmp = 0;\n for(int i=0; i<3; ++i){\n if(d.pip[1][2][i] == '*')\n tmp |= 1;\n if(d.pip[3][0][i] == '*')\n tmp |= 2;\n }\n if(tmp == 3)\n return ret;\n\n for(int i=0; i<4; ++i){\n Dice d2 = d;\n d2.roll(i);\n if(s.find(d2.pip) == s.end()){\n s.insert(d2.pip);\n q.push(d2.pip);\n }\n }\n }\n ++ ret;\n }\n}\n\nint main()\n{\n int to[] = {1, 0, 3, 2, 4, 5};\n\n for(;;){\n Dice d;\n for(int i=0; i<6; ++i){\n for(int j=0; j<3; ++j){\n cin >> d.pip[to[i]][j];\n if(d.pip[to[i]][j] == \"#\")\n return 0;\n }\n }\n d.pip[0] = rotate(3, d.pip[0]);\n d.pip[2] = rotate(1, d.pip[2]);\n d.pip[3] = rotate(2, d.pip[3]);\n\n cout << solve(d) << endl;\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 0, "score_of_the_acc": 0, "final_rank": 1 }, { "submission_id": "aoj_2245_293482", "code_snippet": "#include <iostream>\n#include <fstream>\n#include <algorithm>\n#include <bitset>\n#include <cassert>\n#include <cctype>\n#include <cmath>\n#include <complex>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <deque>\n#include <iomanip>\n#include <map>\n#include <numeric>\n#include <queue>\n#include <set>\n#include <stack>\n#include <sstream>\n#include <string>\n#include <vector>\nusing namespace std;\n\n#define EPS 1e-9\n#define INF MOD\n#define MOD 1000000007LL\n#define fir first\n#define iss istringstream\n#define sst stringstream\n#define ite iterator\n#define ll long long\n#define mp make_pair\n#define rep(i,n) rep2(i,0,n)\n#define rep2(i,m,n) for(int i=m;i<n;i++)\n#define pi pair<int,int>\n#define pb push_back\n#define sec second\n#define sh(i) (1LL<<i)\n#define sz size()\n#define vi vector<int>\n#define vl vector<ll>\n#define vs vector<string>\n\n#define vc vector<char>\n#define C vector<vc>\n\nC c;\n\nvc r90(vc v){\n\tvc r;\n\trep(i,3)for(int j=2;j>=0;j--)r.pb(v[j*3+i]);\n\treturn r;\n}\n\nvc l90(vc v){\n\tvc r;\n\tfor(int i=2;i>=0;i--)rep(j,3)r.pb(v[j*3+i]);\n\treturn r;\n}\n\nvc r180(vc v){\n\tvc r;\n\tfor(int i=2;i>=0;i--)for(int j=2;j>=0;j--)r.pb(v[i*3+j]);\n\treturn r;\n}\n\nC rotf(C c){\n\tC r;\n\tr.pb(c[5]);\n\tr.pb(r90(c[1]));\n\tr.pb(r180(c[4]));\n\tr.pb(l90(c[3]));\n\tr.pb(c[0]);\n\tr.pb(r180(c[2]));\n\treturn r;\n}\n\nC rotr(C c){\n\tC r;\n\tr.pb(r90(c[0]));\n\tr.pb(r90(c[4]));\n\tr.pb(l90(c[2]));\n\tr.pb(r90(c[5]));\n\tr.pb(r90(c[3]));\n\tr.pb(r90(c[1]));\n\treturn r;\n}\n\nC rotba(C c){\n\tC r;\n\tr.pb(c[4]);\n\tr.pb(l90(c[1]));\n\tr.pb(r180(c[5]));\n\tr.pb(r90(c[3]));\n\tr.pb(r180(c[2]));\n\tr.pb(c[0]);\n\treturn r;\n}\n\nC rotl(C c){\n\tC r;\n\tr.pb(l90(c[0]));\n\tr.pb(l90(c[5]));\n\tr.pb(r90(c[2]));\n\tr.pb(l90(c[4]));\n\tr.pb(l90(c[1]));\n\tr.pb(l90(c[3]));\n\treturn r;\n}\n\nint main(){\n\tc.resize(6);\n\trep(i,6)c[i].resize(9);\n\twhile(cin>>c[0][0]){\n\t\tif(c[0][0]=='#')break;\n\t\trep(i,8)cin>>c[0][i+1];\n\t\trep(i,5)rep(j,9)cin>>c[i+1][j];\n\t\tmap<C,int> M;\n\t\tqueue<C> Q;\n\t\tM[c]=0;\n\t\tQ.push(c);\n\t\twhile(1){\n\t\t\tC c=Q.front();Q.pop();\n\t\t\tint s=M[c];\n\t\t\tif((c[0][6]=='*'||c[0][7]=='*'||c[0][8]=='*')&&(c[2][6]=='*'||c[2][7]=='*'||c[2][8]=='*')){\n\t\t\t\tcout<<s<<endl;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tC c0=rotf(c),c1=rotr(c),c2=rotba(c),c3=rotl(c);\n\t\t\tif(M.find(c0)==M.end())M[c0]=s+1,Q.push(c0);\n\t\t\tif(M.find(c1)==M.end())M[c1]=s+1,Q.push(c1);\n\t\t\tif(M.find(c2)==M.end())M[c2]=s+1,Q.push(c2);\n\t\t\tif(M.find(c3)==M.end())M[c3]=s+1,Q.push(c3);\n\t\t}\n\t}\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 0, "score_of_the_acc": 0, "final_rank": 1 }, { "submission_id": "aoj_2245_145371", "code_snippet": "#include <iostream>\n#include <vector>\n#include <queue>\nusing namespace std;\n\n#define rep(i,n) for(int i=0;i<n;i++)\n\ntypedef vector<string> S;\n\n//3*3ツづ個配ツ療アツづーツ篠楪計ツ嘉アツづィ90ツ度ツ嘉アツ転\nS rot90(S s){\n\tS res = s;\n\trep(i,3) rep(j,3) res[j][2-i] = s[i][j];\n\treturn res;\n}\nS rot180(S s){return rot90(rot90(s));}\nS rot270(S s){return rot90(rot180(s));}\n\nclass Dice{\npublic:\n\tvector<string> t,b,w,e,n,s;\n\n\tDice(S _t,S _b,S _w,S _e,S _n,S _s){\n\t\tt = _t;\n\t\tb = _b;\n\t\tw = _w;\n\t\te = _e;\n\t\tn = _n;\n\t\ts = _s;\n\t}\n\n\tDice move(int d){\n\t\tif(d == 0)\n\t\t\treturn Dice(s,rot180(n),rot90(w),rot270(e),rot180(t),b);\n\t\telse\n\t\t\treturn Dice(rot270(w),rot270(e),rot270(b),rot270(t),rot90(n),rot270(s));\n\t}\n\n\tbool isAns(void){\n\t\treturn\n\t\t\t(s[2][0]=='*' || s[2][1]=='*' || s[2][2]=='*') &&\n\t\t\t(n[2][0]=='*' || n[2][1]=='*' || n[2][2]=='*');\n\t}\n};\n\nint solve(Dice d){\n\ttypedef pair<Dice,int> P;\n\tqueue<P> open;\n\topen.push(P(d,0));\n\n\twhile(!open.empty()){\n\t\tP p = open.front(); open.pop();\n\n\t\tif(p.first.isAns()){\n\t\t\treturn p.second;\n\t\t}\n\n\t\topen.push(P(p.first.move(0),p.second+1));\n\t\topen.push(P(p.first.move(1),p.second+1));\n\t\topen.push(P(p.first.move(0).move(0).move(0),p.second+1));\n\t\topen.push(P(p.first.move(1).move(1).move(1),p.second+1));\n\t}\n\n\treturn -1;\n}\n\nint main(void){\n\twhile(1){\n\t\tS s[6];\n\t\trep(i,6) rep(j,3){\n\t\t\tstring str;\n\t\t\tcin>>str;\n\t\t\ts[i].push_back(str);\n\t\t\tif(s[i][j] == \"#\") return 0;\n\t\t}\n\n\t\tDice d(s[4],s[5],s[3],s[1],s[2],s[0]);\n\t\tcout<<solve(d)<<endl;\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 988, "score_of_the_acc": -0.3204, "final_rank": 3 } ]
aoj_2243_cpp
Problem B: Step Step Evolution Japanese video game company has developed the music video game called Step Step Evolution. The gameplay of Step Step Evolution is very simple. Players stand on the dance platform, and step on panels on it according to a sequence of arrows shown in the front screen. There are eight types of direction arrows in the Step Step Evolution: UP, UPPER RIGHT, RIGHT, LOWER RIGHT, DOWN, LOWER LEFT, LEFT, and UPPER LEFT. These direction arrows will scroll upward from the bottom of the screen. When the direction arrow overlaps the stationary arrow nearby the top, then the player must step on the corresponding arrow panel on the dance platform. The figure below shows how the dance platform looks like. Figure 1: the dance platform for Step Step Evolution In the game, you have to obey the following rule: Except for the beginning of the play, you must not press the arrow panel which is not correspond to the edit data. The foot must stay upon the panel where it presses last time, and will not be moved until it’s going to press the next arrow panel. The left foot must not step on the panel which locates at right to the panel on which the right foot rests. Conversely, the right foot must not step on the panel which locates at left to the panel on which the left foot rests. As for the third condition, the following figure shows the examples of the valid and invalid footsteps. Figure 2: the examples of valid and invalid footsteps The first one (left foot for LEFT, right foot for DOWN) and the second one (left foot for LOWER LEFT, right foot for UPPER LEFT) are valid footstep. The last one (left foot for RIGHT, right foot for DOWN) is invalid footstep. Note that, at the beginning of the play, the left foot and right foot can be placed anywhere at the arrow panel. Also, you can start first step with left foot or right foot, whichever you want. To play this game in a beautiful way, the play style called “ Natural footstep style” is commonly known among talented players. “Natural footstep style” is the style in which players make steps by the left foot and the right foot in turn. However, when a sequence of arrows is difficult, players are sometimes forced to violate this style. Now, your friend has created an edit data (the original sequence of direction arrows to be pushed) for you. You are interested in how many times you have to violate “Natural footstep style” when you optimally played with this edit data. In other words, what is the minimum number of times you have to step on two consecutive arrows using the same foot? Input The input consists of several detasets. Each dataset is specified by one line containing a sequence of direction arrows. Directions are described by numbers from 1 to 9, except for 5. The figure below shows the correspondence between numbers and directions. You may assume that the length of the sequence is between 1 and 100000, inclusive. Also, arrows of the same direction won’t appear consecutively in the line ...(truncated)
[ { "submission_id": "aoj_2243_11059884", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing a2 = array<ll, 2>;\nusing a3 = array<ll, 3>;\n\nbool chmin(auto& a, const auto& b) { return a > b ? a = b, 1 : 0; }\nbool chmax(auto& a, const auto& b) { return a < b ? a = b, 1 : 0; }\n\nll mod = 998244353;\nconst ll INF = 1e18;\n\nifstream in;\nofstream out;\n\nint main(int argc, char** argv) {\n ios::sync_with_stdio(false);\n cin.tie(0);\n\n if(argc > 2) {\n in.open(argv[1]);\n cin.rdbuf(in.rdbuf());\n out.open(argv[2]);\n cout.rdbuf(out.rdbuf());\n }\n\n while(1) {\n vector<ll> mp(10);\n mp[1] = 0;\n mp[2] = 1;\n mp[3] = 2;\n mp[4] = 0;\n mp[5] = 1;\n mp[6] = 2;\n mp[7] = 0;\n mp[8] = 1;\n mp[9] = 2;\n\n string s;\n cin >> s;\n if(s == \"#\") break;\n\n ll ans = INF;\n vector<vector<a2>> dp(10, vector<a2>(10, {INF, INF}));\n dp[5][5] = {0, 0};\n for(auto& x : s) {\n ll num = x - '0';\n vector<vector<a2>> dp2(10, vector<a2>(10, {INF, INF}));\n for(int i = 0; i < 10; i++) {\n for(int j = 0; j < 10; j++) {\n if(mp[i] <= mp[num] || i == 5) {\n chmin(dp2[i][num][1], dp[i][j][0]);\n chmin(dp2[i][num][1], dp[i][j][1] + 1);\n }\n if(mp[num] <= mp[i] || i == 5) {\n chmin(dp2[num][i][0], dp[j][i][1]);\n chmin(dp2[num][i][0], dp[j][i][0] + 1);\n }\n }\n }\n swap(dp, dp2);\n\n ll res = INF;\n for(int i = 0; i < 10; i++) {\n for(int j = 0; j < 10; j++) {\n chmin(res, dp[i][j][0]);\n chmin(res, dp[i][j][1]);\n }\n }\n // cerr << res << endl;\n }\n\n for(int i = 0; i < 10; i++) {\n for(int j = 0; j < 10; j++) {\n chmin(ans, dp[i][j][0]);\n chmin(ans, dp[i][j][1]);\n }\n }\n cout << ans << endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 210, "memory_kb": 3712, "score_of_the_acc": -0.1396, "final_rank": 9 }, { "submission_id": "aoj_2243_10888938", "code_snippet": "#include<bits/stdc++.h>\n// #include<atcoder/all>\n// #include<boost/multiprecision/cpp_int.hpp>\n\nusing namespace std;\n// using namespace atcoder;\n// using bint = boost::multiprecision::cpp_int;\nusing ll = long long;\nusing ull = unsigned long long;\nusing ld = long double;\nusing pii = pair<int,int>;\nusing pll = pair<ll,ll>;\nusing vi = vector<ll>;\nusing vvi = vector<vi>;\nusing vvvi = vector<vvi>;\nusing ve = vector<vector<int>>;\nusing vb = vector<bool>;\nusing vvb = vector<vb>;\n#define rep(i,s,n) for(ll i = (ll)s;i < (ll)n;i++)\n#define rrep(i,l,r) for(ll i = r-1;i >= l;i--)\n#define ALL(x) (x).begin(),(x).end()\n#define sz(c) ((ll)(c).size())\n#define LB(A,x) (int)(lower_bound(A.begin(),A.end(),x)-A.begin())\n#define UB(A,x) (int)(upper_bound(A.begin(),A.end(),x)-A.begin())\n// #define MOD 1000000007\n#define MOD 998244353\ntemplate<typename T>using min_priority_queue=priority_queue<T,vector<T>,greater<T>>;\ntemplate<typename T>ostream&operator<<(ostream&os,vector<T>&v){for(int i = 0;i < v.size();i++)os<<v[i]<<(i+1!=v.size()?\" \":\"\");return os;}\ntemplate<typename T>istream&operator>>(istream&is,vector<T>&v){for(T&in:v)is>>in;return is;}\ntemplate<typename T1,typename T2>ostream&operator<<(ostream&os,pair<T1,T2>&p){os<<p.first<<\" \"<<p.second;return os;}\ntemplate<typename T1,typename T2>istream&operator>>(istream&is,pair<T1,T2>&p){is>>p.first>>p.second;return is;}\ntemplate<typename T> inline bool chmax(T &a,T b){if(a < b){a = b;return true;}return false;}\ntemplate<typename T> inline bool chmin(T &a,T b){if(a > b){a = b;return true;}return false;}\nld dist(ld x1,ld y1,ld x2, ld y2){return sqrtl((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));}\n\n\nint main(){\n \n ios_base::sync_with_stdio(0), cin.tie(0);\n auto valid = [](int i,int j)->bool{\n if(i == 0 || j == 0 || i == j)return false;\n i = (i+2)%3;j = (j+2)%3;\n return i <= j;\n };\n while(1){\n string s;cin >> s;\n if(s[0] == '#')break;\n vvvi dp(10,vvi(10,vi(2,MOD)));\n rep(i,0,10)rep(j,0,10)rep(k,0,2)if(valid(i,j))dp[i][j][k] = 0;\n rep(_,0,sz(s)){\n vvvi DP(10,vvi(10,vi(2,MOD)));\n int nu = s[_]-'0';\n rep(i,0,10)rep(j,0,10)rep(k,0,2){\n if(dp[i][j][k] == MOD)continue;\n if(valid(i,nu))chmin(DP[i][nu][1],dp[i][j][k] + (k == 1));\n if(valid(nu,j))chmin(DP[nu][j][0],dp[i][j][k] + (k == 0));\n }\n swap(dp,DP);\n }\n ll res = MOD;\n rep(i,0,10)rep(j,0,10)rep(k,0,2)chmin(res,dp[i][j][k]);\n cout << res << \"\\n\";\n }\n \n\n return 0;\n}", "accuracy": 1, "time_ms": 1490, "memory_kb": 3428, "score_of_the_acc": -1.0021, "final_rank": 17 }, { "submission_id": "aoj_2243_10869918", "code_snippet": "#include <iostream>\n#include <iomanip>\n#include <cassert>\n#include <vector>\n#include <algorithm>\n#include <utility>\n#include <numeric>\n#include <tuple>\n#include <ranges>\n// #include \"Src/Number/IntegerDivision.hpp\"\n// #include \"Src/Utility/BinarySearch.hpp\"\n// #include \"Src/Sequence/CompressedSequence.hpp\"\n// #include \"Src/Sequence/RunLengthEncoding.hpp\"\n// #include \"Src/Algebra/Group/AdditiveGroup.hpp\"\n// #include \"Src/DataStructure/FenwickTree/FenwickTree.hpp\"\n// #include \"Src/DataStructure/SegmentTree/SegmentTree.hpp\"\n// #include \"Src/DataStructure/DisjointSetUnion/DisjointSetUnion.hpp\"\n// using namespace zawa;\n// #include \"atcoder/modint\"\n// using mint = atcoder::modint998244353;\n// #include <array>\n// #include <bit>\n// #include <bitset>\n// #include <climits>\n// #include <cmath>\n// #include <set>\n// #include <unordered_set>\n// #include <map>\n// #include <unordered_map>\n// #include <optional>\n// #include <queue>\n// #include <stack>\n// #include <random>\nusing namespace std;\ntemplate <class T, class U>\nostream& operator<<(ostream& os, const pair<T, U>& p) {\n os << '(' << p.first << ',' << p.second << ')';\n return os;\n}\ntemplate <class T>\nostream& operator<<(ostream& os, const vector<T>& v) {\n for (int i = 0 ; i < ssize(v) ; i++)\n os << v[i] << (i + 1 == ssize(v) ? \"\" : \" \");\n return os;\n}\nint solve(const string& s) {\n const int INF = (int)1e9;\n vector dp(2, vector(9, vector<int>(9, INF)));\n for (int i = 0 ; i < 9 ; i++)\n for (int j = 0 ; j < 9 ; j++)\n if (i % 3 <= j % 3)\n dp[0][i][j] = dp[1][i][j] = 0;\n for (char c : s) {\n const int v = c - '1';\n vector nxt(2, vector(9, vector<int>(9, INF)));\n for (int i = 0 ; i < 2 ; i++)\n for (int l = 0 ; l < 9 ; l++)\n for (int r = 0 ; r < 9 ; r++)\n if (dp[i][l][r] != INF) {\n assert(l % 3 <= r % 3);\n if (v % 3 <= r % 3)\n nxt[0][v][r] = min(nxt[0][v][r], dp[i][l][r] + (i == 0));\n if (l % 3 <= v % 3)\n nxt[1][l][v] = min(nxt[1][l][v], dp[i][l][r] + (i == 1));\n }\n dp = move(nxt);\n }\n int ans = INF;\n for (int i = 0 ; i < 2 ; i++)\n for (int j = 0 ; j < 9 ; j++)\n for (int k = 0 ; k < 9 ; k++)\n ans = min(ans, dp[i][j][k]);\n return ans;\n}\nint main() {\n cin.tie(0);\n cout.tie(0);\n ios::sync_with_stdio(0);\n while (true) {\n string s;\n cin >> s;\n if (s == \"#\")\n break;\n cout << solve(s) << '\\n';\n }\n}", "accuracy": 1, "time_ms": 280, "memory_kb": 3712, "score_of_the_acc": -0.1869, "final_rank": 10 }, { "submission_id": "aoj_2243_10238935", "code_snippet": "#include <string>\n#include <vector>\n#include <iostream>\nusing namespace std;\n\nconst int INF = 1012345678;\n\nint solve(const string& S) {\n\tint N = S.size();\n\tvector<int> A(N);\n\tfor (int i = 0; i < N; i++) {\n\t\tint x = S[i] - '0';\n\t\tA[i] = (x - 1) % 3;\n\t}\n\tvector<vector<bool> > vis(N, vector<bool>(18, false));\n\tvector<vector<int> > dp(N, vector<int>(18, -1));\n\tauto calc = [&](auto& self, int pos, int a, int b, int f) -> int {\n\t\tif (pos == N) {\n\t\t\treturn 0;\n\t\t}\n\t\tint h = (a * 3 + b) * 2 + f;\n\t\tif (vis[pos][h]) {\n\t\t\treturn dp[pos][h];\n\t\t}\n\t\tint res = INF;\n\t\tif (A[pos] <= b) {\n\t\t\tint subres = self(self, pos + 1, A[pos], b, 0);\n\t\t\tres = min(res, subres + int(f == 0));\n\t\t}\n\t\tif (A[pos] >= a) {\n\t\t\tint subres = self(self, pos + 1, a, A[pos], 1);\n\t\t\tres = min(res, subres + int(f == 1));\n\t\t}\n\t\tvis[pos][h] = true;\n\t\tdp[pos][h] = res;\n\t\treturn res;\n\t};\n\tint ans = INF;\n\tfor (int i = 0; i < 3; i++) {\n\t\tfor (int j = i; j < 3; j++) {\n\t\t\tfor (int k = 0; k < 2; k++) {\n\t\t\t\tint res = calc(calc, 0, i, j, k);\n\t\t\t\tans = min(ans, res);\n\t\t\t}\n\t\t}\n\t}\n\treturn ans;\n}\n\nint main() {\n\tcin.tie(nullptr);\n\tios::sync_with_stdio(false);\n\twhile (true) {\n\t\tstring S;\n\t\tcin >> S;\n\t\tif (S == \"#\") {\n\t\t\tbreak;\n\t\t}\n\t\tint ans = solve(S);\n\t\tcout << ans << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 31720, "score_of_the_acc": -0.2889, "final_rank": 12 }, { "submission_id": "aoj_2243_10238925", "code_snippet": "#include <string>\n#include <vector>\n#include <iostream>\nusing namespace std;\n\nconst int INF = 1012345678;\n\nint solve(const string& S) {\n\tint N = S.size();\n\tvector<int> A(N);\n\tfor (int i = 0; i < N; i++) {\n\t\tint x = S[i] - '0';\n\t\tA[i] = (x - 1) % 3;\n\t}\n\tvector<vector<bool> > vis(N, vector<bool>(18, false));\n\tvector<vector<int> > dp(N, vector<int>(18, -1));\n\tauto calc = [&](auto& self, int pos, int a, int b, int f, int t) -> int {\n\t\tif (pos == N) {\n\t\t\treturn 0;\n\t\t}\n\t\tint h = (a * 3 + b) * 2 + f;\n\t\tif (vis[pos][h]) {\n\t\t\treturn dp[pos][h];\n\t\t}\n\t\tint res = INF;\n\t\tif (A[pos] <= b) {\n\t\t\tint subres = self(self, pos + 1, A[pos], b, 0, t + int(f == 0));\n\t\t\tres = min(res, subres + int(f == 0));\n\t\t}\n\t\tif (A[pos] >= a) {\n\t\t\tint subres = self(self, pos + 1, a, A[pos], 1, t + int(f == 1));\n\t\t\tres = min(res, subres + int(f == 1));\n\t\t}\n\t\tvis[pos][h] = true;\n\t\tdp[pos][h] = res;\n\t\treturn res;\n\t};\n\tint ans = INF;\n\tfor (int i = 0; i < 3; i++) {\n\t\tfor (int j = i; j < 3; j++) {\n\t\t\tfor (int k = 0; k < 2; k++) {\n\t\t\t\tint res = calc(calc, 0, i, j, k, 0);\n\t\t\t\tans = min(ans, res);\n\t\t\t}\n\t\t}\n\t}\n\treturn ans;\n}\n\nint main() {\n\tcin.tie(nullptr);\n\tios::sync_with_stdio(false);\n\twhile (true) {\n\t\tstring S;\n\t\tcin >> S;\n\t\tif (S == \"#\") {\n\t\t\tbreak;\n\t\t}\n\t\tint ans = solve(S);\n\t\tcout << ans << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 90, "memory_kb": 33404, "score_of_the_acc": -0.3099, "final_rank": 13 }, { "submission_id": "aoj_2243_9597527", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n#include <unistd.h>\n\nnamespace fastio {\nstatic constexpr uint32_t SZ = 1 << 17;\nchar ibuf[SZ];\nchar obuf[SZ];\nchar out[100];\n// pointer of ibuf, obuf\n\n\nuint32_t pil = 0, pir = 0, por = 0;\n\nstruct Pre {\n char num[10000][4];\n constexpr Pre() : num() {\n for (int i = 0; i < 10000; i++) {\n int n = i;\n for (int j = 3; j >= 0; j--) {\n num[i][j] = n % 10 | '0';\n n /= 10;\n }\n }\n }\n} constexpr pre;\n\ninline void load() {\n memmove(ibuf, ibuf + pil, pir - pil);\n pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin);\n pil = 0;\n if (pir < SZ)\n ibuf[pir++] = '\\n';\n}\n\ninline void flush() {\n fwrite(obuf, 1, por, stdout);\n por = 0;\n}\n\nvoid rd(char &c) {\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n}\n\nvoid rd(string &x) {\n x.clear();\n char c;\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n do {\n x += c;\n if (pil == pir)\n load();\n c = ibuf[pil++];\n } while (!isspace(c));\n}\n\ntemplate <typename T> void rd_real(T &x) {\n string s;\n rd(s);\n x = stod(s);\n}\n\ntemplate <typename T> void rd_integer(T &x) {\n if (pil + 100 > pir)\n load();\n char c;\n do\n c = ibuf[pil++];\n while (c < '-');\n bool minus = 0;\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (c == '-') {\n minus = 1, c = ibuf[pil++];\n }\n }\n x = 0;\n while ('0' <= c) {\n x = x * 10 + (c & 15), c = ibuf[pil++];\n }\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (minus)\n x = -x;\n }\n}\n\nvoid rd(int &x) {\n rd_integer(x);\n}\nvoid rd(ll &x) {\n rd_integer(x);\n}\nvoid rd(i128 &x) {\n rd_integer(x);\n}\nvoid rd(uint &x) {\n rd_integer(x);\n}\nvoid rd(ull &x) {\n rd_integer(x);\n}\nvoid rd(u128 &x) {\n rd_integer(x);\n}\nvoid rd(double &x) {\n rd_real(x);\n}\nvoid rd(long double &x) {\n rd_real(x);\n}\n\ntemplate <class T, class U> void rd(pair<T, U> &p) {\n return rd(p.first), rd(p.second);\n}\ntemplate <size_t N = 0, typename T> void rd_tuple(T &t) {\n if constexpr (N < std::tuple_size<T>::value) {\n auto &x = std::get<N>(t);\n rd(x);\n rd_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void rd(tuple<T...> &tpl) {\n rd_tuple(tpl);\n}\n\ntemplate <size_t N = 0, typename T> void rd(array<T, N> &x) {\n for (auto &d : x)\n rd(d);\n}\ntemplate <class T> void rd(vector<T> &x) {\n for (auto &d : x)\n rd(d);\n}\n\nvoid read() {}\ntemplate <class H, class... T> void read(H &h, T &...t) {\n rd(h), read(t...);\n}\n\nvoid wt(const char c) {\n if (por == SZ)\n flush();\n obuf[por++] = c;\n}\nvoid wt(const string s) {\n for (char c : s)\n wt(c);\n}\nvoid wt(const char *s) {\n size_t len = strlen(s);\n for (size_t i = 0; i < len; i++)\n wt(s[i]);\n}\n\ntemplate <typename T> void wt_integer(T x) {\n if (por > SZ - 100)\n flush();\n if (x < 0) {\n obuf[por++] = '-', x = -x;\n }\n int outi;\n for (outi = 96; x >= 10000; outi -= 4) {\n memcpy(out + outi, pre.num[x % 10000], 4);\n x /= 10000;\n }\n if (x >= 1000) {\n memcpy(obuf + por, pre.num[x], 4);\n por += 4;\n } else if (x >= 100) {\n memcpy(obuf + por, pre.num[x] + 1, 3);\n por += 3;\n } else if (x >= 10) {\n int q = (x * 103) >> 10;\n obuf[por] = q | '0';\n obuf[por + 1] = (x - q * 10) | '0';\n por += 2;\n } else\n obuf[por++] = x | '0';\n memcpy(obuf + por, out + outi + 4, 96 - outi);\n por += 96 - outi;\n}\n\ntemplate <typename T> void wt_real(T x) {\n ostringstream oss;\n oss << fixed << setprecision(15) << double(x);\n string s = oss.str();\n wt(s);\n}\n\nvoid wt(int x) {\n wt_integer(x);\n}\nvoid wt(ll x) {\n wt_integer(x);\n}\nvoid wt(i128 x) {\n wt_integer(x);\n}\nvoid wt(uint x) {\n wt_integer(x);\n}\nvoid wt(ull x) {\n wt_integer(x);\n}\nvoid wt(u128 x) {\n wt_integer(x);\n}\nvoid wt(double x) {\n wt_real(x);\n}\nvoid wt(long double x) {\n wt_real(x);\n}\n\ntemplate <class T, class U> void wt(const pair<T, U> val) {\n wt(val.first);\n wt(' ');\n wt(val.second);\n}\ntemplate <size_t N = 0, typename T> void wt_tuple(const T t) {\n if constexpr (N < std::tuple_size<T>::value) {\n if constexpr (N > 0) {\n wt(' ');\n }\n const auto x = std::get<N>(t);\n wt(x);\n wt_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void wt(tuple<T...> tpl) {\n wt_tuple(tpl);\n}\ntemplate <class T, size_t S> void wt(const array<T, S> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\ntemplate <class T> void wt(const vector<T> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\n\nvoid print() {\n wt('\\n');\n}\ntemplate <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {\n wt(head);\n if (sizeof...(Tail))\n wt(' ');\n print(forward<Tail>(tail)...);\n}\nvoid __attribute__((destructor)) _d() {\n flush();\n}\n} // namespace fastio\n\n\nusing fastio::flush;\nusing fastio::print;\nusing fastio::read;\n\ninline void first(bool i = true) {\n print(i ? \"first\" : \"second\");\n}\ninline void Alice(bool i = true) {\n print(i ? \"Alice\" : \"Bob\");\n}\ninline void Takahashi(bool i = true) {\n print(i ? \"Takahashi\" : \"Aoki\");\n}\ninline void yes(bool i = true) {\n print(i ? \"yes\" : \"no\");\n}\ninline void Yes(bool i = true) {\n print(i ? \"Yes\" : \"No\");\n}\ninline void No() {\n print(\"No\");\n}\ninline void YES(bool i = true) {\n print(i ? \"YES\" : \"NO\");\n}\ninline void NO() {\n print(\"NO\");\n}\ninline void Yay(bool i = true) {\n print(i ? \"Yay!\" : \":(\");\n}\ninline void Possible(bool i = true) {\n print(i ? \"Possible\" : \"Impossible\");\n}\ninline void POSSIBLE(bool i = true) {\n print(i ? \"POSSIBLE\" : \"IMPOSSIBLE\");\n}\n\n/**\n * @brief Fast IO\n */\n\nint main() {\nwhile(1) {\n string S;\n cin >> S;\n if (S == \"#\") return 0;\n int N = S.size();\n vector<int> A(N);\n rep(i,0,N) {\n A[i] = (S[i]-'0'+2) % 3;\n }\n int DP[100000][2][3][3] = {};\n rep(i,0,N) rep(j,0,2) rep(k,0,3) rep(l,0,3) DP[i][j][k][l] = inf;\n DP[0][0][A[0]][2] = DP[0][1][0][A[0]] = 0;\n rep(i,0,N-1) {\n rep(j,0,2) {\n rep(k,0,3) {\n rep(l,0,3) {\n if (DP[i][j][k][l] == inf) continue;\n if (j == 0) {\n if (A[i+1] <= l) chmin(DP[i+1][0][A[i+1]][l],DP[i][j][k][l]+1);\n if (k <= A[i+1]) chmin(DP[i+1][1][k][A[i+1]],DP[i][j][k][l]);\n }\n else {\n if (A[i+1] <= l) chmin(DP[i+1][0][A[i+1]][l],DP[i][j][k][l]);\n if (k <= A[i+1]) chmin(DP[i+1][1][k][A[i+1]],DP[i][j][k][l]+1);\n }\n }\n }\n }\n }\n int ANS = inf;\n rep(j,0,2) {\n rep(k,0,3) {\n rep(l,0,3) {\n chmin(ANS, DP[N-1][j][k][l]);\n }\n }\n }\n cout << ANS << endl;\n}\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 11056, "score_of_the_acc": -0.0802, "final_rank": 4 }, { "submission_id": "aoj_2243_8967018", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing P = pair<ll, ll>;\n#define rep(i, a, b) for(ll i = a; i < b; ++i)\n#define rrep(i, a, b) for(ll i = a; i >= b; --i)\nconstexpr ll inf = 4e18;\nstruct SetupIO {\n SetupIO() {\n ios::sync_with_stdio(0);\n cin.tie(0);\n cout << fixed << setprecision(30);\n }\n} setup_io;\nint main(void) {\n while(1) {\n string s;\n cin >> s;\n if(s == \"#\") break;\n int n = s.size();\n vector<vector<int>> dp(n, vector<int>(2));\n rep(i, 1, n) {\n int prev = (s[i - 1] - '1') % 3, cur = (s[i] - '1') % 3;\n if(cur < prev) {\n dp[i][0] = min(dp[i - 1][0] + 1, dp[i - 1][1]);\n dp[i][1] = dp[i - 1][1] + 1;\n } else if(cur == prev) {\n dp[i][0] = min(dp[i - 1][0] + 1, dp[i - 1][1]);\n dp[i][1] = min(dp[i - 1][0], dp[i - 1][1] + 1);\n } else {\n dp[i][0] = dp[i - 1][0] + 1;\n dp[i][1] = min(dp[i - 1][0], dp[i - 1][1] + 1);\n }\n }\n cout << min(dp[n - 1][0], dp[n - 1][1]) << '\\n';\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 8776, "score_of_the_acc": -0.0541, "final_rank": 2 }, { "submission_id": "aoj_2243_7138416", "code_snippet": "// author: hanyu\n#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n\n while (true) {\n string s;\n cin >> s;\n if (s == \"#\") break;\n\n int n = s.size();\n\n vector<int> x(n);\n for (int i = 0; i < n; i++) {\n if ((s[i] - '0') % 3 == 1) x[i] = 0;\n else if ((s[i] - '0') % 3 == 2) x[i] = 1;\n else x[i] = 2;\n }\n\n vector<vector<int>> dp(n, vector<int>(2, 1e9));\n dp[0][0] = dp[0][1] = 0;\n for (int i = 0; i < n - 1; i++) {\n if (x[i] == x[i + 1]) {\n dp[i + 1][0] = min(dp[i][0] + 1, dp[i][1]);\n dp[i + 1][1] = min(dp[i][0], dp[i][1] + 1);\n }\n else if (x[i] < x[i + 1]) {\n dp[i + 1][0] = min(dp[i][0] + 1, dp[i][1]);\n dp[i + 1][1] = dp[i][1] + 1;\n }\n else {\n dp[i + 1][0] = dp[i][0] + 1;\n dp[i + 1][1] = min(dp[i][0], dp[i][1] + 1);\n }\n }\n\n cout << min(dp[n - 1][0], dp[n - 1][1]) << endl;\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 9152, "score_of_the_acc": -0.0573, "final_rank": 3 }, { "submission_id": "aoj_2243_6785374", "code_snippet": "#pragma region template\n#pragma GCC optimize(\"Ofast\")\n#include <bits/stdc++.h>\nusing namespace std;\n#ifdef __LOCAL\n #include <debug>\n#else\n #define debug(...) void(0)\n#endif\n\n#define REP(i,n) for(int i=0;i<(n);i++)\n#define ALL(v) v.begin(),v.end()\n\ntemplate<typename T>\nistream& operator>>(istream&is,vector<T>&v){\n for(T&p:v)is>>p;\n return is;\n}\ntemplate<typename T>\nostream& operator<<(ostream&os,const vector<T>&v){\n if(&os==&cerr)os<<\"[\";\n for(int i=0;i<v.size();i++){\n os<<v[i];\n if(i+1<v.size())os<<(&os==&cerr?\",\":\" \");\n }\n if(&os==&cerr)os<<\"]\";\n return os;\n}\n#pragma endregion template\n\nconst int INF=1e9+7;\ntemplate<typename T,typename ...Args>\nauto make_vector(T x,int arg,Args ...args){\n if constexpr(sizeof...(args)==0)return vector<T>(arg,x);\n else return vector(arg,make_vector<T>(x,args...));\n}\ntemplate<typename T>\nbool chmin(T &a,T b){\n return (a>b&&(a=b,true));\n}\n\nint main(){\n string s;\n while(cin>>s,s!=\"#\"){\n int n=s.size();\n\n auto f=[](char c){\n return (c-'1')%3;\n };\n \n auto dp=make_vector<int>(0,3,3,2);//左,右,最後が左?\n REP(i,3)REP(j,3)REP(k,2)if(i>j)dp[i][j][k]=INF;\n for(const char c:s){\n int v=f(c);\n auto nxt=make_vector<int>(INF,3,3,2);\n REP(i,3)REP(j,3)REP(k,2){\n //左にv\n if(v<=j)chmin(nxt[v][j][1],dp[i][j][k]+(k==1));\n //右\n if(v>=i)chmin(nxt[i][v][0],dp[i][j][k]+(k==0));\n }\n dp=nxt;\n }\n int ans=INF;\n REP(i,3)REP(j,3)REP(k,2)chmin(ans,dp[i][j][k]);\n cout<<ans<<\"\\n\";\n }\n}", "accuracy": 1, "time_ms": 180, "memory_kb": 3468, "score_of_the_acc": -0.1173, "final_rank": 8 }, { "submission_id": "aoj_2243_6782951", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing vll = vector<ll>;\nusing vvll = vector<vll>;\nusing vvvll = vector<vvll>;\nusing vvvvll = vector<vvvll>;\nusing vb = vector<bool>;\nusing vvb = vector<vb>;\nusing vvvb = vector<vvb>;\nusing vd = vector<double>;\nusing vvd = vector<vd>;\nusing vvvd = vector<vvd>;\n#define all(A) A.begin(),A.end()\n#define ALL(A) A.begin(),A.end()\n#define rep(i, n) for (ll i = 0; i < (ll) (n); i++)\nusing pqr = priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>>;\n\ntemplate<class T>\nbool chmax(T& p, T q, bool C = 1) {\n if (C == 0 && p == q) {\n return 1;\n }\n if (p < q) {\n p = q;\n return 1;\n }\n else {\n return 0;\n }\n}\n\ntemplate<class T>\nbool chmin(T& p, T q, bool C = 1) {\n if (C == 0 && p == q) {\n return 1;\n }\n if (p > q) {\n p = q;\n return 1;\n }\n else {\n return 0;\n }\n}\n\nll gcd(ll(a), ll(b)) {\n if (b == 0)return a;\n ll c = a;\n while (a % b != 0) {\n c = a % b;\n a = b;\n b = c;\n }\n return b;\n}\n\nvector<ll> fact, factinv, inv;\nll mod = 1e9 + 7;\nvoid prenCkModp(ll n) {\n fact.resize(n + 5);\n factinv.resize(n + 5);\n inv.resize(n + 5);\n fact.at(0) = fact.at(1) = 1;\n factinv.at(0) = factinv.at(1) = 1;\n inv.at(1) = 1;\n for (ll i = 2; i < n + 5; i++) {\n fact.at(i) = (fact.at(i - 1) * i) % mod;\n inv.at(i) = mod - (inv.at(mod % i) * (mod / i)) % mod;\n factinv.at(i) = (factinv.at(i - 1) * inv.at(i)) % mod;\n }\n\n}\nll nCk(ll n, ll k) {\n if (k < 0)return 0;\n if (n < k) return 0;\n return fact.at(n) * (factinv.at(k) * factinv.at(n - k) % mod) % mod;\n}\n\n\n\nint main() {\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n\n while(1){\n string S;\n cin>>S;\n if(S==\"#\")return 0;\n ll N=S.size();\n vvvvll DP(N+1,vvvll(3,vvll(3,vll(2,1e18))));\n rep(i,3)rep(j,3)rep(p,2)DP[0][i][j][p]=0;\n rep(n,N){\n ll d=S[n]-'0';\n d=(d+2)%3;\n rep(i,3){//L\n rep(j,3){//R\n if(i<=d){\n chmin(DP[n+1][i][d][1],DP[n][i][j][0]);\n }\n else{\n chmin(DP[n+1][d][j][0],DP[n][i][j][0]+1);\n }\n\n if(d<=j){\n chmin(DP[n+1][d][j][0],DP[n][i][j][1]);\n }\n else{\n chmin(DP[n+1][i][d][1],DP[n][i][j][1]+1);\n }\n }\n }\n }\n ll an=1e18;\n rep(i,3)rep(j,3)rep(p,2)chmin(an,DP[N][i][j][p]);\n cout<<an<<endl;\n }\n\n}", "accuracy": 1, "time_ms": 360, "memory_kb": 64844, "score_of_the_acc": -0.7586, "final_rank": 15 }, { "submission_id": "aoj_2243_6370596", "code_snippet": "#include <bits/stdc++.h>\n#define all(v) v.begin(), v.end()\n#define rall(v) v.rbegin(), v.rend()\n#define rep(i,n) for(int i=0;i<(int)(n);i++)\n#define drep(i,j,n) for(int i=0;i<(int)(n-1);i++)for(int j=i+1;j<(int)(n);j++)\n#define trep(i,j,k,n) for(int i=0;i<(int)(n-2);i++)for(int j=i+1;j<(int)(n-1);j++)for(int k=j+1;k<(int)(n);k++)\n#define codefor int test;scanf(\"%d\",&test);while(test--)\n#define INT(...) int __VA_ARGS__;in(__VA_ARGS__)\n#define LL(...) ll __VA_ARGS__;in(__VA_ARGS__)\n#define yes(ans) if(ans)printf(\"yes\\n\");else printf(\"no\\n\")\n#define Yes(ans) if(ans)printf(\"Yes\\n\");else printf(\"No\\n\")\n#define YES(ans) if(ans)printf(\"YES\\n\");else printf(\"NO\\n\")\n#define popcount(v) __builtin_popcount(v)\n#define vector1d(type,name,...) vector<type>name(__VA_ARGS__)\n#define vector2d(type,name,h,...) vector<vector<type>>name(h,vector<type>(__VA_ARGS__))\n#define vector3d(type,name,h,w,...) vector<vector<vector<type>>>name(h,vector<vector<type>>(w,vector<type>(__VA_ARGS__)))\n#define vector4d(type,name,h,w,d,...) vector<vector<vector<vector<type>>>>name(h,vector<vector<vector<type>>>(w,vector<vector<type>>(d,vector<type>(__VA_ARGS__))))\nusing namespace std;\nusing ll = long long;\ntemplate<class T>using rpriority_queue = priority_queue<T, vector<T>, greater<T>>;\nconst int MOD=1000000007;\nconst int MOD2=998244353;\nconst int INF=1<<30;\nconst ll INF2=(ll)1<<60;\n//入力系\nvoid scan(int& a){scanf(\"%d\",&a);}\nvoid scan(long long& a){scanf(\"%lld\",&a);}\ntemplate<class T,class L>void scan(pair<T, L>& p){scan(p.first);scan(p.second);}\ntemplate<class T> void scan(T& a){cin>>a;}\ntemplate<class T> void scan(vector<T>& vec){for(auto&& it:vec)scan(it);}\nvoid in(){}\ntemplate <class Head, class... Tail> void in(Head& head, Tail&... tail){scan(head);in(tail...);}\n//出力系\nvoid print(const int& a){printf(\"%d\",a);}\nvoid print(const long long& a){printf(\"%lld\",a);}\nvoid print(const double& a){printf(\"%.15lf\",a);}\ntemplate<class T,class L>void print(const pair<T, L>& p){print(p.first);putchar(' ');print(p.second);}\ntemplate<class T> void print(const T& a){cout<<a;}\ntemplate<class T> void print(const vector<T>& vec){if(vec.empty())return;print(vec[0]);for(auto it=vec.begin();++it!= vec.end();){putchar(' ');print(*it);}}\nvoid out(){putchar('\\n');}\ntemplate<class T> void out(const T& t){print(t);putchar('\\n');}\ntemplate <class Head, class... Tail> void out(const Head& head,const Tail&... tail){print(head);putchar(' ');out(tail...);}\n//デバッグ系\ntemplate<class T> void dprint(const T& a){cerr<<a;}\ntemplate<class T> void dprint(const vector<T>& vec){if(vec.empty())return;cerr<<vec[0];for(auto it=vec.begin();++it!= vec.end();){cerr<<\" \"<<*it;}}\nvoid debug(){cerr<<endl;}\ntemplate<class T> void debug(const T& t){dprint(t);cerr<<endl;}\ntemplate <class Head, class... Tail> void debug(const Head& head, const Tail&... tail){dprint(head);cerr<<\" \";debug(tail...);}\nll intpow(ll a, ll b){ ll ans = 1; while(b){ if(b & 1) ans *= a; a *= a; b /= 2; } return ans; }\nll modpow(ll a, ll b, ll p){ ll ans = 1; while(b){ if(b & 1) (ans *= a) %= p; (a *= a) %= p; b /= 2; } return ans; }\nll modinv(ll a, ll m) {ll b = m, u = 1, v = 0;while (b) {ll t = a / b;a -= t * b; swap(a, b);u -= t * v; swap(u, v);}u %= m;if (u < 0) u += m;return u;}\nll updivide(ll a,ll b){if(a%b==0) return a/b;else return (a/b)+1;}\ntemplate<class T> void chmax(T &a,const T b){if(b>a)a=b;}\ntemplate<class T> void chmin(T &a,const T b){if(b<a)a=b;}\n\nint dp[100000][10][10][2];\n\nint main(){\n string s;\n while(cin>>s){\n if(s==\"#\")return 0;\n int n=s.size();\n for(int i=0;i<n;i++){\n for(int j=0;j<10;j++){\n for(int k=0;k<10;k++){\n dp[i][j][k][0]=dp[i][j][k][1]=-1;\n }\n }\n }\n auto f=[&](int v){\n if(v==1||v==4||v==7)return -1;\n if(v==2||v==8)return 0;\n return 1;\n };\n function<int(int,int,int,int)> rec=[&](int i,int l,int r,int pre){\n if(i>=n)return 0;\n if(dp[i][l][r][pre]!=-1)return dp[i][l][r][pre];\n int nxt=s[i]-'0',ans=INF;\n if(f(nxt)<=f(r))chmin(ans,rec(i+1,nxt,r,0)+(i!=0&&pre==0));\n if(f(nxt)>=f(l))chmin(ans,rec(i+1,l,nxt,1)+(i!=0&&pre==1));\n return dp[i][l][r][pre]=ans;\n };\n out(rec(0,1,3,0));\n }\n}", "accuracy": 1, "time_ms": 160, "memory_kb": 95580, "score_of_the_acc": -0.8837, "final_rank": 16 }, { "submission_id": "aoj_2243_6198313", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\n#define rep(i, n) for (int i = 0; i < (n); i++)\n#define ALL(x) x.begin(), x.end()\n\nconst int INF = 1 << 30;\n\nconst int dx[9] = {-1, 0, 1, -1, 0, 1, -1, 0, 1};\nconst int dy[9] = {-1, -1, -1, 0, 0, 0, 1, 1, 1};\n\nvoid solve(string &S) {\n int N = (int)S.size();\n vector<int> a(N);\n rep(i, N) a[i] = S[i] - '1';\n // 最初の足の配置と、どっちの足から動くかを全探索する\n int ans = INF;\n rep(l, 9) rep(r, 9) rep(k, 2) {\n int lx = dx[l], ly = dy[l], rx = dx[r], ry = dy[r];\n if (lx > rx) continue;\n int nex = k;\n int cur = 0;\n rep(i, N) {\n if (nex) {\n // 左足を動かす\n int nx = dx[a[i]], ny = dy[a[i]];\n if (nx > rx) {\n // 右足を動かすことにする\n cur++;\n rx = nx, ry = ny;\n } else {\n lx = nx, ly = ny;\n nex ^= 1;\n }\n } else {\n // 右足を動かす\n int nx = dx[a[i]], ny = dy[a[i]];\n if (lx > nx) {\n // 左足を動かすことにする\n lx = nx, ly = ny;\n cur++;\n } else {\n rx = nx, ry = ny;\n nex ^= 1;\n }\n }\n }\n ans = min(ans, cur);\n }\n cout << ans << '\\n';\n return;\n}\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(0);\n string S;\n while (cin >> S, S != \"#\") solve(S);\n return 0;\n}", "accuracy": 1, "time_ms": 140, "memory_kb": 3992, "score_of_the_acc": -0.0947, "final_rank": 5 }, { "submission_id": "aoj_2243_5811825", "code_snippet": "#line 1 \"/Users/nok0/Documents/Programming/nok0/cftemp.hpp\"\n#include <bits/stdc++.h>\nusing namespace std;\n\n#pragma region Macros\n// rep macro\n#define foa(v, a) for(auto &v : a)\n#define REPname(a, b, c, d, e, ...) e\n#define REP(...) REPname(__VA_ARGS__, REP3, REP2, REP1, REP0)(__VA_ARGS__)\n#define REP0(x) for(int i = 0; i < (x); ++i)\n#define REP1(i, x) for(int i = 0; i < (x); ++i)\n#define REP2(i, l, r) for(int i = (l); i < (r); ++i)\n#define REP3(i, l, r, c) for(int i = (l); i < (r); i += (c))\n#define REPSname(a, b, c, ...) c\n#define REPS(...) REPSname(__VA_ARGS__, REPS1, REPS0)(__VA_ARGS__)\n#define REPS0(x) for(int i = 1; i <= (x); ++i)\n#define REPS1(i, x) for(int i = 1; i <= (x); ++i)\n#define RREPname(a, b, c, d, e, ...) e\n#define RREP(...) RREPname(__VA_ARGS__, RREP3, RREP2, RREP1, RREP0)(__VA_ARGS__)\n#define RREP0(x) for(int i = (x)-1; i >= 0; --i)\n#define RREP1(i, x) for(int i = (x)-1; i >= 0; --i)\n#define RREP2(i, r, l) for(int i = (r)-1; i >= (l); --i)\n#define RREP3(i, r, l, c) for(int i = (r)-1; i >= (l); i -= (c))\n#define RREPSname(a, b, c, ...) c\n#define RREPS(...) RREPSname(__VA_ARGS__, RREPS1, RREPS0)(__VA_ARGS__)\n#define RREPS0(x) for(int i = (x); i >= 1; --i)\n#define RREPS1(i, x) for(int i = (x); i >= 1; --i)\n\n// name macro\n#define pb push_back\n#define eb emplace_back\n#define SZ(x) ((int)(x).size())\n#define all(x) (x).begin(), (x).end()\n#define rall(x) (x).rbegin(), (x).rend()\n#define popcnt(x) __builtin_popcountll(x)\ntemplate <class T = int>\nusing V = std::vector<T>;\ntemplate <class T = int>\nusing VV = std::vector<std::vector<T>>;\ntemplate <class T>\nusing pqup = std::priority_queue<T, std::vector<T>, std::greater<T>>;\nusing ll = long long;\nusing ld = long double;\nusing int128 = __int128_t;\nusing pii = std::pair<int, int>;\nusing pll = std::pair<long long, long long>;\n\n// input macro\ntemplate <class T, class U>\nstd::istream &operator>>(std::istream &is, std::pair<T, U> &p) {\n\tis >> p.first >> p.second;\n\treturn is;\n}\ntemplate <class T>\nstd::istream &operator>>(std::istream &is, std::vector<T> &v) {\n\tfor(T &i : v) is >> i;\n\treturn is;\n}\nstd::istream &operator>>(std::istream &is, __int128_t &a) {\n\tstd::string s;\n\tis >> s;\n\t__int128_t ret = 0;\n\tfor(int i = 0; i < s.length(); i++)\n\t\tif('0' <= s[i] and s[i] <= '9')\n\t\t\tret = 10 * ret + s[i] - '0';\n\ta = ret * (s[0] == '-' ? -1 : 1);\n\treturn is;\n}\nnamespace scanner {\nvoid scan(int &a) { std::cin >> a; }\nvoid scan(long long &a) { std::cin >> a; }\nvoid scan(std::string &a) { std::cin >> a; }\nvoid scan(char &a) { std::cin >> a; }\nvoid scan(char a[]) { std::scanf(\"%s\", a); }\nvoid scan(double &a) { std::cin >> a; }\nvoid scan(long double &a) { std::cin >> a; }\ntemplate <class T, class U>\nvoid scan(std::pair<T, U> &p) { std::cin >> p; }\ntemplate <class T>\nvoid scan(std::vector<T> &a) { std::cin >> a; }\nvoid INPUT() {}\ntemplate <class Head, class... Tail>\nvoid INPUT(Head &head, Tail &... tail) {\n\tscan(head);\n\tINPUT(tail...);\n}\n} // namespace scanner\n#define VEC(type, name, size) \\\n\tstd::vector<type> name(size); \\\n\tscanner::INPUT(name)\n#define VVEC(type, name, h, w) \\\n\tstd::vector<std::vector<type>> name(h, std::vector<type>(w)); \\\n\tscanner::INPUT(name)\n#define INT(...) \\\n\tint __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define LL(...) \\\n\tlong long __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define STR(...) \\\n\tstd::string __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define CHAR(...) \\\n\tchar __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define DOUBLE(...) \\\n\tdouble __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define LD(...) \\\n\tlong double __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n\n// output-macro\ntemplate <class T, class U>\nstd::ostream &operator<<(std::ostream &os, const std::pair<T, U> &p) {\n\tos << p.first << \" \" << p.second;\n\treturn os;\n}\ntemplate <class T>\nstd::ostream &operator<<(std::ostream &os, const std::vector<T> &a) {\n\tfor(int i = 0; i < int(a.size()); ++i) {\n\t\tif(i) os << \" \";\n\t\tos << a[i];\n\t}\n\treturn os;\n}\nstd::ostream &operator<<(std::ostream &dest, __int128_t &value) {\n\tstd::ostream::sentry s(dest);\n\tif(s) {\n\t\t__uint128_t tmp = value < 0 ? -value : value;\n\t\tchar buffer[128];\n\t\tchar *d = std::end(buffer);\n\t\tdo {\n\t\t\t--d;\n\t\t\t*d = \"0123456789\"[tmp % 10];\n\t\t\ttmp /= 10;\n\t\t} while(tmp != 0);\n\t\tif(value < 0) {\n\t\t\t--d;\n\t\t\t*d = '-';\n\t\t}\n\t\tint len = std::end(buffer) - d;\n\t\tif(dest.rdbuf()->sputn(d, len) != len) {\n\t\t\tdest.setstate(std::ios_base::badbit);\n\t\t}\n\t}\n\treturn dest;\n}\ntemplate <class T>\nvoid print(const T a) { std::cout << a << '\\n'; }\ntemplate <class Head, class... Tail>\nvoid print(Head H, Tail... T) {\n\tstd::cout << H << ' ';\n\tprint(T...);\n}\ntemplate <class T>\nvoid printel(const T a) { std::cout << a << '\\n'; }\ntemplate <class T>\nvoid printel(const std::vector<T> &a) {\n\tfor(const auto &v : a)\n\t\tstd::cout << v << '\\n';\n}\ntemplate <class Head, class... Tail>\nvoid printel(Head H, Tail... T) {\n\tstd::cout << H << '\\n';\n\tprintel(T...);\n}\nvoid Yes(const bool b = true) { std::cout << (b ? \"Yes\\n\" : \"No\\n\"); }\nvoid No() { std::cout << \"No\\n\"; }\nvoid YES(const bool b = true) { std::cout << (b ? \"YES\\n\" : \"NO\\n\"); }\nvoid NO() { std::cout << \"NO\\n\"; }\nvoid err(const bool b = true) {\n\tif(b) {\n\t\tstd::cout << \"-1\\n\", exit(0);\n\t}\n}\n\n//debug macro\nnamespace debugger {\ntemplate <class T>\nvoid view(const std::vector<T> &a) {\n\tstd::cerr << \"{ \";\n\tfor(const auto &v : a) {\n\t\tstd::cerr << v << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\ntemplate <class T>\nvoid view(const std::vector<std::vector<T>> &a) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &v : a) {\n\t\tstd::cerr << \"\\t\";\n\t\tview(v);\n\t\tstd::cerr << \"\\n\";\n\t}\n\tstd::cerr << \"}\";\n}\ntemplate <class T, class U>\nvoid view(const std::vector<std::pair<T, U>> &a) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &p : a) std::cerr << \"\\t(\" << p.first << \", \" << p.second << \")\\n\";\n\tstd::cerr << \"}\";\n}\ntemplate <class T, class U>\nvoid view(const std::map<T, U> &m) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &p : m) std::cerr << \"\\t[\" << p.first << \"] : \" << p.second << \"\\n\";\n\tstd::cerr << \"}\";\n}\ntemplate <class T, class U>\nvoid view(const std::pair<T, U> &p) { std::cerr << \"(\" << p.first << \", \" << p.second << \")\"; }\ntemplate <class T>\nvoid view(const std::set<T> &s) {\n\tstd::cerr << \"{ \";\n\tfor(auto &v : s) {\n\t\tview(v);\n\t\tstd::cerr << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\n\ntemplate <class T>\nvoid view(const T &e) { std::cerr << e; }\n} // namespace debugger\n#ifdef LOCAL\nvoid debug_out() {}\ntemplate <typename Head, typename... Tail>\nvoid debug_out(Head H, Tail... T) {\n\tdebugger::view(H);\n\tstd::cerr << \", \";\n\tdebug_out(T...);\n}\n#define debug(...) \\\n\tdo { \\\n\t\tstd::cerr << __LINE__ << \" [\" << #__VA_ARGS__ << \"] : [\"; \\\n\t\tdebug_out(__VA_ARGS__); \\\n\t\tstd::cerr << \"\\b\\b]\\n\"; \\\n\t} while(false)\n#else\n#define debug(...) (void(0))\n#endif\n\n// vector macro\ntemplate <class T>\nint lb(const std::vector<T> &a, const T x) { return std::distance((a).begin(), std::lower_bound((a).begin(), (a).end(), (x))); }\ntemplate <class T>\nint ub(const std::vector<T> &a, const T x) { return std::distance((a).begin(), std::upper_bound((a).begin(), (a).end(), (x))); }\ntemplate <class T>\nvoid UNIQUE(std::vector<T> &a) {\n\tstd::sort(a.begin(), a.end());\n\ta.erase(std::unique(a.begin(), a.end()), a.end());\n}\ntemplate <class T>\nstd::vector<T> press(std::vector<T> &a) {\n\tauto res = a;\n\tUNIQUE(res);\n\tfor(auto &v : a)\n\t\tv = lb(res, v);\n\treturn res;\n}\n#define SORTname(a, b, c, ...) c\n#define SORT(...) SORTname(__VA_ARGS__, SORT1, SORT0, ...)(__VA_ARGS__)\n#define SORT0(a) std::sort((a).begin(), (a).end())\n#define SORT1(a, c) std::sort((a).begin(), (a).end(), [](const auto x, const auto y) { return x c y; })\ntemplate <class T>\nvoid ADD(std::vector<T> &a, const T x = 1) {\n\tfor(auto &v : a) v += x;\n}\ntemplate <class T>\nvoid SUB(std::vector<T> &a, const T x = 1) {\n\tfor(auto &v : a) v -= x;\n}\ntemplate <class T>\nvoid MUL(std::vector<T> &a, const T x) {\n\tfor(auto &v : a) v *= x;\n}\ntemplate <class T>\nvoid DIV(std::vector<T> &a, const T x) {\n\tfor(auto &v : a) v /= x;\n}\nstd::vector<std::pair<char, int>> rle(const string &s) {\n\tint n = s.size();\n\tstd::vector<std::pair<char, int>> ret;\n\tfor(int l = 0; l < n;) {\n\t\tint r = l + 1;\n\t\tfor(; r < n and s[l] == s[r]; r++) {}\n\t\tret.emplace_back(s[l], r - l);\n\t\tl = r;\n\t}\n\treturn ret;\n}\ntemplate <class T>\nstd::vector<std::pair<T, int>> rle(const std::vector<T> &v) {\n\tint n = v.size();\n\tstd::vector<std::pair<T, int>> ret;\n\tfor(int l = 0; l < n;) {\n\t\tint r = l + 1;\n\t\tfor(; r < n and v[l] == v[r]; r++) {}\n\t\tret.emplace_back(v[l], r - l);\n\t\tl = r;\n\t}\n\treturn ret;\n}\n\n// math macro\ntemplate <class T, class U>\ninline bool chmin(T &a, const U &b) { return a > b ? a = b, true : false; }\ntemplate <class T, class U>\ninline bool chmax(T &a, const U &b) { return a < b ? a = b, true : false; }\ntemplate <class T>\nT divup(T x, T y) { return (x + y - 1) / y; }\ntemplate <class T>\nT POW(T a, long long n) {\n\tT ret = 1;\n\twhile(n) {\n\t\tif(n & 1) ret *= a;\n\t\ta *= a;\n\t\tn >>= 1;\n\t}\n\treturn ret;\n}\n// modpow\nlong long POW(long long a, long long n, const int mod) {\n\tlong long ret = 1;\n\ta = (a % mod + mod) % mod;\n\twhile(n) {\n\t\tif(n & 1) (ret *= a) %= mod;\n\t\t(a *= a) %= mod;\n\t\tn >>= 1;\n\t}\n\treturn ret;\n}\n\n// others\nstruct fast_io {\n\tfast_io() {\n\t\tios::sync_with_stdio(false);\n\t\tcin.tie(nullptr);\n\t\tcout << fixed << setprecision(15);\n\t}\n} fast_io_;\nconst int inf = 1e9;\nconst ll INF = 1e18;\n#pragma endregion\n\nvoid main_();\n\nint main() {\n\tmain_();\n\treturn 0;\n}\n#line 2 \"a.cpp\"\n\nconst int N = 20000;\nvoid main_() {\n\twhile(1) {\n\t\tSTR(s);\n\t\tif(s == \"#\") break;\n\t\tint n = SZ(s);\n\t\tvector dp(n + 1, vector(2, vector(100, inf)));\n\t\t//左 * 9 + 右\n\t\tV<> valid(100);\n\t\tREPS(i, 9) {\n\t\t\tREPS(j, 9) {\n\t\t\t\tif(i == 5 or j == 5) continue;\n\t\t\t\tif(i == 2 or i == 8) {\n\t\t\t\t\tif(j == 1 or j == 4 or j == 7) {\n\t\t\t\t\t\tcontinue;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif(i == 3 or i == 6 or i == 9) {\n\t\t\t\t\tif(j == 1 or j == 4 or j == 7) {\n\t\t\t\t\t\tcontinue;\n\t\t\t\t\t}\n\t\t\t\t\tif(j == 2 or j == 8) {\n\t\t\t\t\t\tcontinue;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tvalid[i * 9 + j] = 1;\n\t\t\t}\n\t\t}\n\t\tREP(i, 2) {\n\t\t\tREP(j, 100) {\n\t\t\t\tif(valid[j]) dp[0][i][j] = 0;\n\t\t\t}\n\t\t}\n\t\tREP(i, n) {\n\t\t\tREP(oj, 2) {\n\t\t\t\tREP(ov, 100) {\n\t\t\t\t\tif(dp[i][oj][ov] == inf) continue;\n\t\t\t\t\tint rig = ov % 9;\n\t\t\t\t\tif(rig == 0) rig = 9;\n\t\t\t\t\tint lef = (ov - rig) / 9;\n\t\t\t\t\tREP(nj, 2) {\n\t\t\t\t\t\tint nx = s[i] - '0';\n\t\t\t\t\t\tif(nj == 0) {\n\t\t\t\t\t\t\tint nxt = nx * 9 + rig;\n\t\t\t\t\t\t\tif(!valid[nxt]) continue;\n\t\t\t\t\t\t\tchmin(dp[i + 1][nj][nxt], dp[i][oj][ov] + (oj == nj));\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif(nj == 1) {\n\t\t\t\t\t\t\tint nxt = lef * 9 + nx;\n\t\t\t\t\t\t\tif(!valid[nxt]) continue;\n\t\t\t\t\t\t\tchmin(dp[i + 1][nj][nxt], dp[i][oj][ov] + (oj == nj));\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tint res = inf;\n\t\tREP(i, 2) {\n\t\t\tREP(j, 100) {\n\t\t\t\tchmin(res, dp.back()[i][j]);\n\t\t\t}\n\t\t}\n\t\tprint(res);\n\t}\n}", "accuracy": 1, "time_ms": 370, "memory_kb": 93012, "score_of_the_acc": -1.0038, "final_rank": 18 }, { "submission_id": "aoj_2243_5757941", "code_snippet": "#ifdef LOCAL\n#define _GLIBCXX_DEBUG\n#endif\n \n#include <bits/stdc++.h>\nusing namespace std;\n#if __has_include(<atcoder/all>)\n#include <atcoder/all>\nusing namespace atcoder;\n#endif\n \n#pragma region Macros\n// rep macro\n#define foa(v, a) for(auto &v : a)\n#define REPname(a, b, c, d, e, ...) e\n#define REP(...) REPname(__VA_ARGS__, REP3, REP2, REP1, REP0)(__VA_ARGS__)\n#define REP0(x) for(int i = 0; i < (x); ++i)\n#define REP1(i, x) for(int i = 0; i < (x); ++i)\n#define REP2(i, l, r) for(int i = (l); i < (r); ++i)\n#define REP3(i, l, r, c) for(int i = (l); i < (r); i += (c))\n#define REPSname(a, b, c, ...) c\n#define REPS(...) REPSname(__VA_ARGS__, REPS1, REPS0)(__VA_ARGS__)\n#define REPS0(x) for(int i = 1; i <= (x); ++i)\n#define REPS1(i, x) for(int i = 1; i <= (x); ++i)\n#define RREPname(a, b, c, d, e, ...) e\n#define RREP(...) RREPname(__VA_ARGS__, RREP3, RREP2, RREP1, RREP0)(__VA_ARGS__)\n#define RREP0(x) for(int i = (x)-1; i >= 0; --i)\n#define RREP1(i, x) for(int i = (x)-1; i >= 0; --i)\n#define RREP2(i, l, r) for(int i = (r)-1; i >= (l); --i)\n#define RREP3(i, l, r, c) for(int i = (r)-1; i >= (l); i -= (c))\n#define RREPSname(a, b, c, ...) c\n#define RREPS(...) RREPSname(__VA_ARGS__, RREPS1, RREPS0)(__VA_ARGS__)\n#define RREPS0(x) for(int i = (x); i >= 1; --i)\n#define RREPS1(i, x) for(int i = (x); i >= 1; --i)\n \n// name macro\n#define fi first\n#define se second\n#define pb push_back\n#define eb emplace_back\n#define SZ(x) ((int)(x).size())\n#define all(x) (x).begin(), (x).end()\n#define rall(x) (x).rbegin(), (x).rend()\n#define popcnt(x) __builtin_popcountll(x)\ntemplate <class T = int>\nusing V = std::vector<T>;\ntemplate <class T = int>\nusing VV = std::vector<std::vector<T>>;\ntemplate <class T = int>\nusing VVV = std::vector<std::vector<std::vector<T>>>;\ntemplate <class T>\nusing pqup = std::priority_queue<T, std::vector<T>, std::greater<T>>;\nusing ll = long long;\nusing ld = long double;\nusing int128 = __int128_t;\nusing pii = std::pair<int, int>;\nusing pll = std::pair<long long, long long>;\nusing Tp = tuple<ll,ll,ll>;\n \n// input macro\ntemplate <class T, class U>\nstd::istream &operator>>(std::istream &is, std::pair<T, U> &p) {\n\tis >> p.first >> p.second;\n\treturn is;\n}\ntemplate <class T>\nstd::istream &operator>>(std::istream &is, std::vector<T> &v) {\n\tfor(T &i : v) is >> i;\n\treturn is;\n}\nstd::istream &operator>>(std::istream &is, __int128_t &a) {\n\tstd::string s;\n\tis >> s;\n\t__int128_t ret = 0;\n\tfor(int i = 0; i < s.length(); i++)\n\t\tif('0' <= s[i] and s[i] <= '9')\n\t\t\tret = 10 * ret + s[i] - '0';\n\ta = ret * (s[0] == '-' ? -1 : 1);\n\treturn is;\n}\nnamespace scanner {\nvoid scan(int &a) { std::cin >> a; }\nvoid scan(long long &a) { std::cin >> a; }\nvoid scan(std::string &a) { std::cin >> a; }\nvoid scan(char &a) { std::cin >> a; }\nvoid scan(char a[]) { std::scanf(\"%s\", a); }\nvoid scan(double &a) { std::cin >> a; }\nvoid scan(long double &a) { std::cin >> a; }\ntemplate <class T, class U>\nvoid scan(std::pair<T, U> &p) { std::cin >> p; }\ntemplate <class T>\nvoid scan(std::vector<T> &a) { std::cin >> a; }\nvoid INPUT() {}\ntemplate <class Head, class... Tail>\nvoid INPUT(Head &head, Tail &... tail) {\n\tscan(head);\n\tINPUT(tail...);\n}\n} // namespace scanner\n#define VEC(type, name, size) \\\n\tstd::vector<type> name(size); \\\n\tscanner::INPUT(name)\n#define VVEC(type, name, h, w) \\\n\tstd::vector<std::vector<type>> name(h, std::vector<type>(w)); \\\n\tscanner::INPUT(name)\n#define INT(...) \\\n\tint __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define LL(...) \\\n\tlong long __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define STR(...) \\\n\tstd::string __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define CHAR(...) \\\n\tchar __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define DOUBLE(...) \\\n\tdouble __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define LD(...) \\\n\tlong double __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n \n// output-macro\ntemplate <class T, class U>\nstd::ostream &operator<<(std::ostream &os, const std::pair<T, U> &p) {\n\tos << p.first << \" \" << p.second;\n\treturn os;\n}\ntemplate <class T>\nstd::ostream &operator<<(std::ostream &os, const std::vector<T> &a) {\n\tfor(int i = 0; i < int(a.size()); ++i) {\n\t\tif(i) os << \" \";\n\t\tos << a[i];\n\t}\n\treturn os;\n}\nstd::ostream &operator<<(std::ostream &dest, __int128_t &value) {\n\tstd::ostream::sentry s(dest);\n\tif(s) {\n\t\t__uint128_t tmp = value < 0 ? -value : value;\n\t\tchar buffer[128];\n\t\tchar *d = std::end(buffer);\n\t\tdo {\n\t\t\t--d;\n\t\t\t*d = \"0123456789\"[tmp % 10];\n\t\t\ttmp /= 10;\n\t\t} while(tmp != 0);\n\t\tif(value < 0) {\n\t\t\t--d;\n\t\t\t*d = '-';\n\t\t}\n\t\tint len = std::end(buffer) - d;\n\t\tif(dest.rdbuf()->sputn(d, len) != len) {\n\t\t\tdest.setstate(std::ios_base::badbit);\n\t\t}\n\t}\n\treturn dest;\n}\ntemplate <class T>\nvoid print(const T a) { std::cout << a << '\\n'; }\ntemplate <class Head, class... Tail>\nvoid print(Head H, Tail... T) {\n\tstd::cout << H << ' ';\n\tprint(T...);\n}\ntemplate <class T>\nvoid printel(const T a) { std::cout << a << '\\n'; }\ntemplate <class T>\nvoid printel(const std::vector<T> &a) {\n\tfor(const auto &v : a)\n\t\tstd::cout << v << '\\n';\n}\ntemplate <class Head, class... Tail>\nvoid printel(Head H, Tail... T) {\n\tstd::cout << H << '\\n';\n\tprintel(T...);\n}\nvoid Yes(const bool b = true) { std::cout << (b ? \"Yes\\n\" : \"No\\n\"); }\nvoid No() { std::cout << \"No\\n\"; }\nvoid YES(const bool b = true) { std::cout << (b ? \"YES\\n\" : \"NO\\n\"); }\nvoid NO() { std::cout << \"No\\n\"; }\nvoid err(const bool b = true) {\n\tif(b) {\n\t\tstd::cout << \"-1\\n\", exit(0);\n\t}\n}\n \n//debug macro\nnamespace debugger {\ntemplate <class T>\nvoid view(const std::vector<T> &a) {\n\tstd::cerr << \"{ \";\n\tfor(const auto &v : a) {\n\t\tstd::cerr << v << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\ntemplate <class T>\nvoid view(const std::vector<std::vector<T>> &a) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &v : a) {\n\t\tstd::cerr << \"\\t\";\n\t\tview(v);\n\t\tstd::cerr << \"\\n\";\n\t}\n\tstd::cerr << \"}\";\n}\ntemplate <class T, class U>\nvoid view(const std::vector<std::pair<T, U>> &a) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &p : a) std::cerr << \"\\t(\" << p.first << \", \" << p.second << \")\\n\";\n\tstd::cerr << \"}\";\n}\ntemplate <class T, class U>\nvoid view(const std::map<T, U> &m) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &p : m) std::cerr << \"\\t[\" << p.first << \"] : \" << p.second << \"\\n\";\n\tstd::cerr << \"}\";\n}\ntemplate <class T, class U>\nvoid view(const std::pair<T, U> &p) { std::cerr << \"(\" << p.first << \", \" << p.second << \")\"; }\ntemplate <class T>\nvoid view(const std::set<T> &s) {\n\tstd::cerr << \"{ \";\n\tfor(auto &v : s) {\n\t\tview(v);\n\t\tstd::cerr << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\n \ntemplate <class T>\nvoid view(const T &e) { std::cerr << e; }\n} // namespace debugger\n#ifdef LOCAL\nvoid debug_out() {}\ntemplate <typename Head, typename... Tail>\nvoid debug_out(Head H, Tail... T) {\n\tdebugger::view(H);\n\tstd::cerr << \", \";\n\tdebug_out(T...);\n}\n#define debug(...) \\\n\tdo { \\\n\t\tstd::cerr << __LINE__ << \" [\" << #__VA_ARGS__ << \"] : [\"; \\\n\t\tdebug_out(__VA_ARGS__); \\\n\t\tstd::cerr << \"\\b\\b]\\n\"; \\\n\t} while(false)\n#else\n#define debug(...) (void(0))\n#endif\n \n// vector macro\ntemplate <class T>\nint lb(const std::vector<T> &a, const T x) { return std::distance((a).begin(), std::lower_bound((a).begin(), (a).end(), (x))); }\ntemplate <class T>\nint ub(const std::vector<T> &a, const T x) { return std::distance((a).begin(), std::upper_bound((a).begin(), (a).end(), (x))); }\ntemplate <class T>\nvoid UNIQUE(std::vector<T> &a) {\n\tstd::sort(a.begin(), a.end());\n\ta.erase(std::unique(a.begin(), a.end()), a.end());\n}\ntemplate <class T>\nstd::vector<T> press(std::vector<T> &a) {\n\tauto res = a;\n\tUNIQUE(res);\n\tfor(auto &v : a)\n\t\tv = lb(res, v);\n\treturn res;\n}\n#define SORTname(a, b, c, ...) c\n#define SORT(...) SORTname(__VA_ARGS__, SORT1, SORT0, ...)(__VA_ARGS__)\n#define SORT0(a) std::sort((a).begin(), (a).end())\n#define SORT1(a, c) std::sort((a).begin(), (a).end(), [](const auto x, const auto y) { return x c y; })\ntemplate <class T>\nvoid ADD(std::vector<T> &a, const T x) {\n\tfor(auto &v : a) v += x;\n}\ntemplate <class T>\nvoid SUB(std::vector<T> &a, const T x = 1) {\n\tfor(auto &v : a) v -= x;\n}\ntemplate <class T>\nvoid MUL(std::vector<T> &a, const T x) {\n\tfor(auto &v : a) v *= x;\n}\ntemplate <class T>\nvoid DIV(std::vector<T> &a, const T x) {\n\tfor(auto &v : a) v /= x;\n}\n \n// math macro\ntemplate <class T, class U>\ninline bool chmin(T &a, const U &b) { return a > b ? a = b, true : false; }\ntemplate <class T, class U>\ninline bool chmax(T &a, const U &b) { return a < b ? a = b, true : false; }\ntemplate <class T>\nT divup(T x, T y) { return (x + y - 1) / y; }\ntemplate <class T>\nT POW(T a, long long n) {\n\tT ret = 1;\n\twhile(n) {\n\t\tif(n & 1) ret *= a;\n\t\ta *= a;\n\t\tn >>= 1;\n\t}\n\treturn ret;\n}\n\ntemplate<typename T>\nT ADD(T a, T b){\n\tT res;\n\treturn __builtin_add_overflow(a, b, &res) ? numeric_limits<T>::max() : res;\n}\n\ntemplate<typename T>\nT MUL(T a, T b){\n\tT res;\n\treturn __builtin_mul_overflow(a, b, &res) ? numeric_limits<T>::max() : res;\n}\n\n// modpow\nlong long POW(long long a, long long n, const int mod) {\n\tlong long ret = 1;\n\twhile(n) {\n\t\tif(n & 1) (ret *= a) %= mod;\n\t\t(a *= a) %= mod;\n\t\tn >>= 1;\n\t}\n\treturn ret;\n}\n \n// others\nstruct fast_io {\n\tfast_io() {\n\t\tios::sync_with_stdio(false);\n\t\tcin.tie(nullptr);\n\t\tcout << fixed << setprecision(15);\n\t}\n} fast_io_;\n\n#pragma endregion\n\nusing R = long double;\nconstexpr R EPS=1E-11;\n\n// r の(誤差付きの)符号に従って, -1, 0, 1 を返す.\nint sgn(const R& r){ return (r > EPS) - (r < -EPS); }\n// a, b の(誤差付きの)大小比較の結果に従って, -1, 0, 1 を返す.\nint sgn(const R& a, const R &b){ return sgn(a-b); }\n// a > 0 は sgn(a) > 0\n// a < b は sgn(a, b) < 0\n// a >= b は sgn(a, b) >= 0\n// のように書く.\n// return s * 10^n\n\n//https://atcoder.jp/contests/abc191/submissions/20028529\nlong long x10(const string& s, size_t n) {\n if (s.front() == '-') return -x10(s.substr(1), n);\n auto pos = s.find('.');\n if (pos == string::npos) return stoll(s + string(n, '0'));\n return stoll(s.substr(0, pos) + s.substr(pos + 1) + string(n + pos + 1 - s.size(), '0'));\n}\n \nlong long ceildiv(long long a, long long b) {\n if (b < 0) a = -a, b = -b;\n if (a >= 0) return (a + b - 1) / b;\n else return a / b;\n}\n \nlong long floordiv(long long a, long long b) {\n if (b < 0) a = -a, b = -b;\n if (a >= 0) return a / b;\n else return (a - b + 1) / b;\n}\n \nlong long floorsqrt(long long x) {\n assert(x >= 0);\n long long ok = 0;\n long long ng = 1;\n while (ng * ng <= x) ng <<= 1;\n while (ng - ok > 1) {\n long long mid = (ng + ok) >> 1;\n if (mid * mid <= x) ok = mid;\n else ng = mid;\n }\n return ok;\n}\n\ninline int topbit(unsigned long long x){\n\treturn x?63-__builtin_clzll(x):-1;\n}\n\ninline int popcount(unsigned long long x){\n\treturn __builtin_popcountll(x);\n}\n\ninline int parity(unsigned long long x){//popcount%2\n\treturn __builtin_parity(x);\n}\n\nconst int inf = 1e9;\nconst ll INF = 1e18;\n\nint dx[9] = {-1,0,1,-1,0,1,-1,0,1};\nint dy[9] = {-1,-1,-1,0,0,0,1,1,1};\n\nstring s;\n\n\nvoid main_() {\n\t\n\tint n = s.length();\n\tVVV<int> dp(n+1,VV<int>(9,V<int>(2,inf)));\n\tREP(i,9){\n\t REP(j,2){\n\t dp[0][i][j] = 0;\n\t }\n\t}\n\t\n\tREP(i,n){\n\t int num = s[i] - '1';\n\t REP(j,9){\n\t REP(p,2){\n\t REP(q,2){\n\t if(p == 0 && q == 1 && dx[num] < dx[j])continue;\n\t if(p == 1 && q == 0 && dx[num] > dx[j])continue;\n\t dp[i+1][num][q] = min(dp[i+1][num][q],dp[i][j][p] + (p == q));\n\t }\n\t }\n\t }\n\t}\n\tint ans = inf;\n\tREP(i,9){\n\t REP(j,2){\n\t ans = min(ans,dp[n][i][j]);\n\t }\n\t}\n\tcout << ans << endl;\n}\n \nint main() {\n\tint t = 1;\n\t//cin >> t;\n\tcin >> s;\n\twhile(s != \"#\"){\n\t main_();\n\t cin >> s;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 290, "memory_kb": 55376, "score_of_the_acc": -0.6311, "final_rank": 14 }, { "submission_id": "aoj_2243_5702029", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n string S;\n const int INF = 1e7;\n while (cin >> S, S != \"#\") {\n vector maine(3, vector(3, vector(2, 0)));\n for (auto&& c : S) {\n (c -= '1') %= 3;\n vector book(3, vector(3, vector(2, INF)));\n for (int l = 0; l < 3; l++) {\n for (int r = l; r < 3; r++) {\n if (c <= r)\n book[c][r][1] = min({book[c][r][1], maine[l][r][0], maine[l][r][1] + 1});\n if (c >= l)\n book[l][c][0] = min({book[l][c][0], maine[l][r][1], maine[l][r][0] + 1});\n }\n }\n swap(maine, book);\n }\n int ans = INF;\n for (int i = 0; i < 3; i++) {\n for (int j = i; j < 3; j++) {\n for (int k = 0; k < 2; k++) {\n ans = min(ans, maine[i][j][k]);\n }\n }\n }\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 150, "memory_kb": 3372, "score_of_the_acc": -0.0962, "final_rank": 6 }, { "submission_id": "aoj_2243_5549320", "code_snippet": "#include <cmath>\n#include <deque>\n#include <algorithm>\n#include <iterator>\n#include <list>\n#include <tuple>\n#include <map>\n#include <unordered_map>\n#include <queue>\n#include <set>\n#include <unordered_set>\n#include <stack>\n#include <string>\n#include <vector>\n#include <fstream>\n#include <iostream>\n#include <functional>\n#include <numeric>\n#include <iomanip> \n#include <stdio.h>\n//eolibraries\n#define lnf 3999999999999999999\n#define inf 999999999\n#define PI 3.14159265359\n#define endl \"\\n\"\n#define fi first\n#define se second\n#define pb push_back\n#define eb emplace_back\n#define ll long long\n#define all(c) (c).begin(),(c).end()\n#define sz(c) (int)(c).size()\n#define mkp(a,b) make_pair(a,b)\n#define make_unique(a) sort(all(a)),a.erase(unique(all(a)),a.end())\n#define rsz(a,n) a.resize(n)\n#define pii pair <int,int>\n#define rep(i,n) for(int i = 0 ; i < n ; i++) \n#define drep(i,n) for(int i = n-1 ; i >= 0 ; i--)\n#define crep(i,x,n) for(int i = x ; i < n ; i++)\n#define vi vector <int> \n#define vec(...) vector<__VA_ARGS__>\n#define rsz(a,n) a.resize(n)\n#define rszv(a,n,v) a.resize(n,v)\n#define fcin ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);\n#define chmin(a,b) a=min(a,b)\n//eodefine\n\nusing namespace std;\n\nconst int max_n = 100002;\n\nint dp[max_n][11][11][2];\n\nvoid slv(string s) {\n\tint n=sz(s);\n\tvi a;\n\trep(i,n) {\n\t\ta.pb(s[i]-'0');\n\t}\n\n\trep(i,n+1) {\n\t\trep(lval,10) {\n\t\t\trep(rval,10) {\n\t\t\t\trep(step,2) dp[i][lval][rval][step]=inf;\n\t\t\t}\n\t\t}\n\t}\n\n\tdp[0][5][5][0]=0;\n\tdp[0][5][5][1]=0;\n\n\tfunction<bool(int,int)> valid = [&](int lval,int rval) {\n\t\tif(lval==1 or lval==4 or lval==7) return true;\n\t\tif(lval==2 or lval==8) {\n\t\t\tif(rval==1 or rval==4 or rval==7) return false;\n\t\t\treturn true;\n\t\t}\n\t\tif(rval!=9 and rval!=6 and rval!=3) return false;\n\t\treturn true;\n\t};\n\n\trep(i,n) {\n\t\tint val=a[i];\n\t\trep(lval,10) {\n\t\t\trep(rval,10) {\n\t\t\t\trep(step,2) {\n\t\t\t\t\tif(valid(val,rval) or rval==5){\n\t\t\t\t\t\tchmin(dp[i+1][val][rval][0] , dp[i][lval][rval][step] + (step==0));\n\t\t\t\t\t}\n\t\t\t\t\tif(valid(lval,val) or lval==5){\n\t\t\t\t\t\tchmin(dp[i+1][lval][val][1] , dp[i][lval][rval][step] + (step==1));\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tint ans=inf;\n\t\n\trep(lval,10) {\n\t\trep(rval,10) {\n\t\t\trep(step,2) {\n\t\t\t\tchmin(ans,dp[n][lval][rval][step]);\n\t\t\t}\n\t\t}\n\t} \n\t\n\tcout<<ans<<\"\\n\";\n}\n\nint main(){\nfcin;\n\n\twhile(true) {\n\t\tstring s;\n\t\tcin>>s;\n\t\tif(s==\"#\") break;\n\t\tslv(s);\n\t}\n\t\n/*\n\n*/\n \n return 0; \n}", "accuracy": 1, "time_ms": 840, "memory_kb": 98652, "score_of_the_acc": -1.3691, "final_rank": 19 }, { "submission_id": "aoj_2243_5549315", "code_snippet": "#include <cmath>\n#include <deque>\n#include <algorithm>\n#include <iterator>\n#include <list>\n#include <tuple>\n#include <map>\n#include <unordered_map>\n#include <queue>\n#include <set>\n#include <unordered_set>\n#include <stack>\n#include <string>\n#include <vector>\n#include <fstream>\n#include <iostream>\n#include <functional>\n#include <numeric>\n#include <iomanip> \n#include <stdio.h>\n//eolibraries\n#define lnf 3999999999999999999\n#define inf 999999999\n#define PI 3.14159265359\n#define endl \"\\n\"\n#define fi first\n#define se second\n#define pb push_back\n#define eb emplace_back\n#define ll long long\n#define all(c) (c).begin(),(c).end()\n#define sz(c) (int)(c).size()\n#define mkp(a,b) make_pair(a,b)\n#define make_unique(a) sort(all(a)),a.erase(unique(all(a)),a.end())\n#define rsz(a,n) a.resize(n)\n#define pii pair <int,int>\n#define rep(i,n) for(int i = 0 ; i < n ; i++) \n#define drep(i,n) for(int i = n-1 ; i >= 0 ; i--)\n#define crep(i,x,n) for(int i = x ; i < n ; i++)\n#define vi vector <int> \n#define vec(...) vector<__VA_ARGS__>\n#define rsz(a,n) a.resize(n)\n#define rszv(a,n,v) a.resize(n,v)\n#define fcin ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);\n#define chmin(a,b) a=min(a,b)\n//eodefine\n\nusing namespace std;\n\nconst int max_n = 100002;\n\nint dp[max_n][10][10][3];\n\nvoid slv(string s) {\n\tint n=sz(s);\n\tvi a;\n\trep(i,n) {\n\t\ta.pb(s[i]-'0');\n\t}\n\n\trep(i,n+1) {\n\t\trep(lval,10) {\n\t\t\trep(rval,10) {\n\t\t\t\trep(step,3) dp[i][lval][rval][step]=inf;\n\t\t\t}\n\t\t}\n\t}\n\n\tdp[0][5][5][0]=0;\n\tdp[0][5][5][1]=0;\n\n\tfunction<bool(int,int)> valid = [&](int lval,int rval) {\n\t\tif(lval==1 or lval==4 or lval==7) return true;\n\t\tif(lval==2 or lval==8) {\n\t\t\tif(rval==1 or rval==4 or rval==7) return false;\n\t\t\treturn true;\n\t\t}\n\t\tif(rval!=9 and rval!=6 and rval!=3) return false;\n\t\treturn true;\n\t};\n\n\trep(i,n) {\n\t\tint val=a[i];\n\t\trep(lval,10) {\n\t\t\trep(rval,10) {\n\t\t\t\trep(step,3) {\n\t\t\t\t\tif(valid(val,rval) or rval==5){\n\t\t\t\t\t\tchmin(dp[i+1][val][rval][0] , dp[i][lval][rval][step] + (step==0));\n\t\t\t\t\t}\n\t\t\t\t\tif(valid(lval,val) or lval==5){\n\t\t\t\t\t\tchmin(dp[i+1][lval][val][1] , dp[i][lval][rval][step] + (step==1));\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tint ans=inf;\n\t\n\trep(lval,10) {\n\t\trep(rval,10) {\n\t\t\trep(step,3) {\n\t\t\t\tchmin(ans,dp[n][lval][rval][step]);\n\t\t\t}\n\t\t}\n\t} \n\t\n\tcout<<ans<<\"\\n\";\n}\n\nint main(){\nfcin;\n\n\twhile(true) {\n\t\tstring s;\n\t\tcin>>s;\n\t\tif(s==\"#\") break;\n\t\tslv(s);\n\t}\n\t\n/*\n\n*/\n \n return 0; \n}", "accuracy": 1, "time_ms": 1210, "memory_kb": 121292, "score_of_the_acc": -1.8108, "final_rank": 20 }, { "submission_id": "aoj_2243_5086453", "code_snippet": "#include <bits/stdc++.h>\n\n#pragma GCC target(\"avx2\")\n#pragma GCC optimize(\"Ofast\")\n#pragma GCC optimize(\"unroll-loops\")\n//#include <boost/multiprecision/cpp_int.hpp>\nusing namespace std;\n\n//using namespace boost::multiprecision;\n//#include<atcoder/all>\n//#include<atcoder/segtree>\n//#include <atcoder/scc>\n//using namespace atcoder;\n\nusing dou =long double;\nstring yes=\"yes\";\nstring Yes=\"Yes\";\nstring YES=\"YES\";\nstring no=\"no\";\nstring No=\"No\";\nstring NO=\"NO\";\n\ntemplate<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }\ntemplate<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef pair<int,int> P;\ntypedef pair<ll,ll> PL;\n\nll mod = 998244353ll;\n//ll mod = 1000000007ll;\n//const ll mod = 4;\n\n\nstruct mint {\n ll x; // typedef long long ll;\n mint(ll x=0):x((x%mod+mod)%mod){}\n mint operator-() const { return mint(-x);}\n mint& operator+=(const mint a) {\n if ((x += a.x) >= mod) x -= mod;\n return *this;\n }\n mint& operator-=(const mint a) {\n if ((x += mod-a.x) >= mod) x -= mod;\n return *this;\n }\n mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this;}\n mint operator+(const mint a) const { return mint(*this) += a;}\n mint operator-(const mint a) const { return mint(*this) -= a;}\n mint operator*(const mint a) const { return mint(*this) *= a;}\n mint pow(ll t) const {\n if (!t) return 1;\n mint a = pow(t>>1);\n a *= a;\n if (t&1) a *= *this;\n return a;\n }\n\n // for prime modhttps://atcoder.jp/contests/abc166/submit?taskScreenName=abc166_f\n mint inv() const { return pow(mod-2);}\n mint& operator/=(const mint a) { return *this *= a.inv();}\n mint operator/(const mint a) const { return mint(*this) /= a;}\n};\n\nistream& operator>>(istream& is, const mint& a) { return is >> a.x;}\nostream& operator<<(ostream& os, const mint& a) { return os << a.x;}\n\n#define rep(i, n) for(ll i = 0; i < (ll)(n); i++)\n//#define rep(i, n) for(int i = 0; i < (int)(n); i++)\n#define brep(n) for(int bit=0;bit<(1<<n);bit++)\n#define bbrep(n) for(int bbit=0;bbit<(1<<n);bbit++)\n#define erep(i,container) for (auto &i : container)\n#define itrep(i,container) for (auto i : container)\n#define irep(i, n) for(ll i = n-1; i >= (ll)0ll; i--)\n#define rrep(i,m,n) for(ll i = m; i < (ll)(n); i++)\n#define reprep(i,j,h,w) rep(i,h)rep(j,w)\n#define repreprep(i,j,k,h,w,n) rep(i,h)rep(j,w)rep(k,n)\n#define all(x) (x).begin(),(x).end()\n#define rall(x) (x).rbegin(),(x).rend()\n#define VEC(type,name,n) std::vector<type> name(n);rep(i,n)std::cin >> name[i];\n#define pb push_back\n#define pf push_front\n#define query int qq;std::cin >> qq;rep(qqq,qq)\n#define lb lower_bound\n#define ub upper_bound\n#define fi first\n#define se second\n#define itn int\n#define mp make_pair\n//#define sum(a) accumulate(all(a),0ll)\n#define keta fixed<<setprecision\n#define kout(d) std::cout << keta(10)<< d<< std::endl;\n#define vout(a) erep(qxqxqx,a)std::cout << qxqxqx << ' ';std::cout << std::endl;\n#define vvector(name,typ,m,n,a)vector<vector<typ> > name(m,vector<typ> (n,a))\n//#define vvector(name,typ,m,n)vector<vector<typ> > name(m,vector<typ> (n))\n#define vvvector(name,t,l,m,n,a) vector<vector<vector<t> > > name(l, vector<vector<t> >(m, vector<t>(n,a)));\n#define vvvvector(name,t,k,l,m,n,a) vector<vector<vector<vector<t> > > > name(k,vector<vector<vector<t> > >(l, vector<vector<t> >(m, vector<t>(n,a)) ));\n//#define case std::cout <<\"Case #\" <<qqq+1<<\":\"\n#define RES(a,iq,jq) a.resize(iq);rep(iii,iq)a[iii].resize(jq);\n\n#define RESRES(a,i,j,k) a.resize(i);rep(ii,i)a[ii].resize(j);reprep(ii,jj,i,j){a[ii][jj].resize(k);};\n#define res resize\n#define as assign\n#define ffor for(;;)\n#define ppri(a,b) std::cout << a<<\" \"<<b << std::endl\n#define pppri(a,b,c) std::cout << a<<\" \"<<b <<\" \"<< c<<std::endl\n#define ppppri(a,b,c,d) std::cout << a<<\" \"<<b <<\" \"<< c<<' '<<d<<std::endl\n#define aall(x,n) (x).begin(),(x).begin()+(n)\n#define SUMI(a) accumulate(all(a),0) \n#define SUM(a) accumulate(all(a),0ll) \n#define stirng string\n#define gin(a,b) int a,b;std::cin >> a>>b;a--;b--;\n#define popcount __builtin_popcount\n#define permu(a) next_permutation(all(a))\n#define aru(a,d) a.find(d)!=a.end()\n#define nai(a,d) a.find(d)==a.end()\n//#define aru p.find(mp(x,y))!=p.end()\n//#define grid_input(a,type) int h,w;std::cin >> h>>w;vvector(a,type,h,w,0);reprep(i,j,h,w)std::cin >> a[i][j];\n\n//typedef long long T;\nll ceili(ll a,ll b){\n return ((a+b-1)/b);\n}\nconst int INF = 2000000000;\n//const ll INF64 =922330720854775807ll;\nconst ll INF64 = 4223372036854775807ll;\n//const ll INF64 = 9223372036854775807ll;\n\n//const ll INF64 = 243'000'000'000'000'000'0;Q\n\n\n//const ll MOD = 1000000007ll;\nconst ll MOD = 998244353ll;\n//const ll MOD = 1000003ll;\nconst ll OD = 1000000000000007ll;\nconst dou pi=3.141592653589793;\n\nlong long modpow(long long a, long long n) { \n long long res = 1;\n while (n > 0) {\n if (n & 1) res = res * a % MOD;\n a = a * a % MOD;\n n >>= 1;\n }\n return res;\n}\nvector< ll > divisor(ll n) { //約数の列挙\n vector< ll > ret;\n for(ll i = 1; i * i <= n; i++) {\n if(n % i == 0) {\n ret.push_back(i);\n if(i * i != n) ret.push_back(n / i);\n }\n }\n sort(begin(ret), end(ret));\n return (ret);\n}\n//メモ\n//ゲーム(Grundy数とか)の復習をする\n//リスニング力をどうにかする\n//個数制限付きナップサックの復習\n//戻すDP\n//全方位木DPとスライド最小値\n//ゲーム→パリティに注目するといいことあるかも\n//ゲーム→もとの状態に戻せる状態を探す\n//理由がなければLLを使え!\n//理由がなければLLを使え!\n//理由がなければLLを使え!\n//理由がなければLLを使え!\n//理由がなければLLを使え!\n//理由がなければLLを使え!\n//理由がなければLLを使え!\n//理由がなければLLを使え!\n//来週の月曜日に銀行へ行く\n//積率を調べる実験を来週の火曜日までにやる\n//レピュニット数\n//フェルマーの小定理\n//拡張ユークリッド\n\nstruct edge {// ダイクストラ法\n ll to, cost;\n \n edge(ll to,ll cost):\n to(to),cost(cost){};\n \n\n};\ntypedef vector<vector<edge> > AdjList; // 隣接リストの型\ntypedef vector<vector<edge> > Graph;\nvector<ll> dist;\ntypedef pair<int, int> P;\nvoid dijkstra(AdjList& graph,int n, int s){\n dist.clear();\n dist = vector<ll>(n,INF64);\n dist[s] = 0;\n priority_queue<P, vector<P>, greater<P> > que;\n que.push(P(0,s));\n while(!que.empty()){\n P p = que.top();\n que.pop();\n int v = p.second;\n if(dist[v] < p.first){\n continue;\n }\n for(int i=0;i < graph[v].size();i++){\n edge e = graph[v][i];\n if(dist[e.to] > dist[v] + e.cost){\n dist[e.to] = dist[v] + e.cost;\n que.push(P(dist[e.to],e.to));\n }\n }\n }\n}\n\n\nint main(){\n ffor{\n string s;std::cin >> s;\n if(s==\"#\")break;\n int n=s.size();\n std::vector<std::vector<edge>> g(n*2+2);\n int st=n*2,go=n*2+1;\n g[st].pb(edge(0,0));\n g[st].pb(edge(1,0));\n g[n*2-2].pb(edge(go,0));\n g[n*2-1].pb(edge(go,0));\n std::vector<int> v{-1,0,1,-1,0,1,-1,0,1};\n rep(i,n-1){\n int be=v[s[i]-'1'];\n int af=v[s[i+1]-'1'];\n if(be>af){\n g[i*2].pb(edge( (i+1)*2,1 ));\n //g[i*2].pb(edge( (i+1)*2+1,0 ));\n g[i*2+1].pb(edge( (i+1)*2+1,1 ));\n g[i*2+1].pb(edge( (i+1)*2,0 ));\n }\n else if(be<af){\n g[i*2].pb(edge( (i+1)*2,1 ));\n g[i*2].pb(edge( (i+1)*2+1,0 ));\n g[i*2+1].pb(edge( (i+1)*2+1,1 ));\n //g[i*2+1].pb(edge( (i+1)*2,0 ));\n }\n else{\n g[i*2].pb(edge( (i+1)*2,1 ));\n g[i*2].pb(edge( (i+1)*2+1,0 ));\n g[i*2+1].pb(edge( (i+1)*2+1,1 ));\n g[i*2+1].pb(edge( (i+1)*2,0 ));\n }\n }\n dijkstra(g,n*2+2,st);\n std::cout << dist[go] << std::endl;\n \n }\n}", "accuracy": 1, "time_ms": 110, "memory_kb": 19792, "score_of_the_acc": -0.2082, "final_rank": 11 }, { "submission_id": "aoj_2243_4961725", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\n\nint dp[2][10][10][3];\nbool is_allowed(int left, int right) {\n left = left % 3 == 0 ? 3 : left % 3;\n right = right % 3 == 0 ? 3 : right % 3;\n return left <= right;\n}\nint main() {\n while (1) {\n string notes;\n cin >> notes;\n if (notes == \"#\")\n break;\n int n = notes.size();\n fill((int *)dp, (int *)(dp + 2), 1 << 30);\n vector<int> moves = {1, 2, 3, 4, 6, 7, 8, 9};\n for (int l : moves) {\n for (int r : moves) {\n if (!is_allowed(l, r))\n continue;\n dp[0][l][r][0] = 0;\n }\n }\n for (int i = 0; i < n; i++) {\n int now = i & 1;\n int nxt = now ^ 1;\n for (int l : moves) {\n for (int r : moves) {\n for (int last = 0; last < 3; last++) {\n if (dp[now][l][r][last] == 1 << 30)\n continue;\n //左足で押す\n if (is_allowed(notes[i] - '0', r)) {\n dp[nxt][notes[i] - '0'][r][1] =\n min(dp[nxt][notes[i] - '0'][r][1],\n dp[now][l][r][last] + (last == 1));\n }\n //右足で押す\n if (is_allowed(l, notes[i] - '0')) {\n dp[nxt][l][notes[i] - '0'][2] =\n min(dp[nxt][l][notes[i] - '0'][2],\n dp[now][l][r][last] + (last == 2));\n }\n }\n }\n }\n for (int l : moves)\n for (int r : moves)\n for (int last = 0; last < 3; last++)\n dp[now][l][r][last] = 1 << 30;\n }\n int ans = 1 << 30;\n for (int l : moves) {\n for (int r : moves) {\n ans = min(ans, dp[n & 1][l][r][1]);\n ans = min(ans, dp[n & 1][l][r][2]);\n }\n }\n cout << ans << '\\n';\n }\n}", "accuracy": 1, "time_ms": 180, "memory_kb": 3372, "score_of_the_acc": -0.1165, "final_rank": 7 }, { "submission_id": "aoj_2243_4939022", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define int long long\n\n// debug methods\n// usage: debug(x,y);\n#define CHOOSE(a) CHOOSE2 a\n#define CHOOSE2(a0,a1,a2,a3,a4,x,...) x\n#define debug_1(x1) cout<<#x1<<\": \"<<x1<<endl\n#define debug_2(x1,x2) cout<<#x1<<\": \"<<x1<<\", \"#x2<<\": \"<<x2<<endl\n#define debug_3(x1,x2,x3) cout<<#x1<<\": \"<<x1<<\", \"#x2<<\": \"<<x2<<\", \"#x3<<\": \"<<x3<<endl\n#define debug_4(x1,x2,x3,x4) cout<<#x1<<\": \"<<x1<<\", \"#x2<<\": \"<<x2<<\", \"#x3<<\": \"<<x3<<\", \"#x4<<\": \"<<x4<<endl\n#define debug_5(x1,x2,x3,x4,x5) cout<<#x1<<\": \"<<x1<<\", \"#x2<<\": \"<<x2<<\", \"#x3<<\": \"<<x3<<\", \"#x4<<\": \"<<x4<<\", \"#x5<<\": \"<<x5<<endl\n#ifdef _DEBUG\n#define debug(...) CHOOSE((__VA_ARGS__,debug_5,debug_4,debug_3,debug_2,debug_1,~))(__VA_ARGS__)\n#else\n#define debug(...)\n#endif\n\n#define MAX_N (1000006)\n#define INF (1LL << 60)\n#define eps 1e-9\nconst int MOD = (int)1e9 + 7;\n\n\nsigned main(){\n //cin.tie(nullptr);\n //std::ios::sync_with_stdio(false);\n\n while(1){\n string s;\n cin >> s;\n if(s==\"#\") break;\n\n debug(s);\n int v[] = {-1,0,1,2,0,1,2,0,1,2};\n\n int ans = INF;\n bool is_left = false;\n // 左足、右足の順でスタートする\n for(int l=0; l<2; l++){\n debug(l);\n int left = 10;\n int right = 10;\n if(is_left) right = v[s[0]-'0'];\n else left = v[s[0]-'0'];\n\n int cnt = 0;\n for(int i=1; i<s.length(); i++){\n int c = s[i]-'0';\n debug(i,s[i],v[c],cnt);\n if(is_left){\n // 左足を c に動かす\n // 右足より左なら cnt++ して、右足を動かす\n if(v[c] > right) {\n cnt++;\n right = v[c];\n } else {\n left = v[c];\n is_left = false;\n }\n } else {\n if(v[c] < left) {\n cnt++;\n left = v[c];\n } else {\n right = v[c];\n is_left = true;\n }\n }\n debug(cnt);\n }\n ans = min(ans, cnt);\n is_left = true;\n }\n cout << ans << endl;\n }\n}\n\n/*\n\n\n*/", "accuracy": 1, "time_ms": 10, "memory_kb": 3184, "score_of_the_acc": 0, "final_rank": 1 } ]
aoj_2244_cpp
Problem C: Dungeon Quest II The cave, called "Mass of Darkness", had been a agitating point of the evil, but the devil king and all of his soldiers were destroyed by the hero and the peace is there now. One day, however, the hero was worrying about the rebirth of the devil king, so he decided to ask security agency to patrol inside the cave. The information of the cave is as follows: The cave is represented as a two-dimensional field which consists of rectangular grid of cells. The cave has R × C cells where R is the number of rows and C is the number of columns. Some of the cells in the cave contains a trap, and those who enter the trapping cell will lose his hit points. The type of traps varies widely: some of them reduce hit points seriously, and others give less damage. The following is how the security agent patrols: The agent will start his patrol from upper left corner of the cave. - There are no traps at the upper left corner of the cave. The agent will patrol by tracing the steps which are specified by the hero. - The steps will be provided such that the agent never go outside of the cave during his patrol. The agent will bring potions to regain his hit point during his patrol. The agent can use potions just before entering the cell where he is going to step in. The type of potions also varies widely: some of them recover hit points so much, and others are less effective. - Note that agent’s hit point can be recovered up to HP max which means his maximum hit point and is specified by the input data. The agent can use more than one type of potion at once. If the agent's hit point becomes less than or equal to 0, he will die. Your task is to write a program to check whether the agent can finish his patrol without dying. Input The input is a sequence of datasets. Each dataset is given in the following format: HP init HP max R C a 1,1 a 1,2 ... a 1, C a 2,1 a 2,2 ... a 2, C . . . a R ,1 a R ,2 ... a R , C T [ A-Z ] d 1 [ A-Z ] d 2 . . . [ A-Z ] d T S [ UDLR ] n 1 [ UDLR ] n 2 . . . [ UDLR ] n S P p 1 p 2 . . . p P The first line of a dataset contains two integers HP init and HP max (0 < HP init ≤ HP max ≤ 1000), meaning the agent's initial hit point and the agent’s maximum hit point respectively. The next line consists of R and C (1 ≤ R , C ≤ 100). Then, R lines which made of C characters representing the information of the cave follow. The character a i,j means there are the trap of type a i,j in i -th row and j -th column, and the type of trap is denoted as an uppercase alphabetic character [ A-Z ]. The next line contains an integer T , which means how many type of traps to be described. The following T lines contains a uppercase character [ A-Z ] and an integer d i (0 ≤ d i ≤ 1000), representing the type of trap and the amount of damage it gives. The next line contains an integer S (0 ≤ S ≤ 1000) representing the number of sequences which the hero specified as the agent's patrol route. The ...(truncated)
[ { "submission_id": "aoj_2244_10849113", "code_snippet": "#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <algorithm>\n#include <queue>\nusing namespace std;\n#define MAXN 310\n#define MAXM 5010\n#define FI first\n#define SE second\n#define MP(x,y) make_pair(x,y)\nconst int INF =1000000007;\nchar ma[MAXN][MAXN];\nint sd[MAXM],len[MAXM],pot[MAXN];\nint cost[MAXN],dp[MAXM];\nconst int dis[4][2]={{-1,0},{0,1},{1,0},{0,-1}};\nint main()\n{\n //freopen(\"J:\\\\Mydocument\\\\Code\\\\input.txt\",\"r\",stdin);\n int hinit,hmax,ss,sp,st,sr,sc;\n while(scanf(\"%d%d\",&hinit,&hmax)&&(hinit||hmax))\n {\n scanf(\"%d%d\",&sr,&sc);\n for(int i=0;i<sr;++i) scanf(\"%s\",ma[i]);\n scanf(\"%d\",&st);\n memset(cost,0,sizeof(cost));\n for(int i=0;i<st;++i)\n {\n char tmp[10];\n scanf(\"%s\",tmp);\n scanf(\"%d\",&cost[tmp[0]]);\n }\n scanf(\"%d\",&ss);\n for(int i=0;i<ss;++i)\n {\n char tmp[10];\n scanf(\"%s%d\",tmp,&len[i]);\n if(tmp[0]=='U') sd[i]=0;\n else if(tmp[0]=='R') sd[i]=1;\n else if(tmp[0]=='D') sd[i]=2;\n else if(tmp[0]=='L') sd[i]=3;\n }\n scanf(\"%d\",&sp);\n for(int i=0;i<sp;++i) scanf(\"%d\",&pot[i]);\n memset(dp,0,sizeof(dp));\n dp[0]=hinit;\n for(int i=0,x=0,y=0,top=(1<<sp);i<ss;++i)\n while(len[i]--)\n {\n x+=dis[sd[i]][0],y+=dis[sd[i]][1];\n for(int j=0;j<top;++j)\n for(int k=0;dp[j]>0&&k<sp;++k)\n if((j&(1<<k))==0)\n dp[j|(1<<k)]=min(hmax,max(dp[j|(1<<k)],dp[j]+pot[k]));\n for(int j=0;j<top;++j)\n dp[j]-=cost[ma[x][y]];\n }\n bool flag=false;\n for(int j=(1<<sp)-1;j>=0;--j) flag|=dp[j]>0;\n printf(\"%s\\n\",flag?\"YES\":\"NO\");\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1760, "memory_kb": 2808, "score_of_the_acc": -0.2838, "final_rank": 6 }, { "submission_id": "aoj_2244_10410677", "code_snippet": "#include <iostream>\n#include <iomanip>\n#include <cassert>\n#include <vector>\n#include <algorithm>\n#include <utility>\n#include <numeric>\n#include <tuple>\n// #include \"Src/Number/IntegerDivision.hpp\"\n// #include \"Src/Utility/BinarySearch.hpp\"\n// #include \"Src/Sequence/CompressedSequence.hpp\"\n// #include \"Src/Sequence/RunLengthEncoding.hpp\"\n// #include \"Src/Algebra/Group/AdditiveGroup.hpp\"\n// #include \"Src/DataStructure/FenwickTree/FenwickTree.hpp\"\n// using namespace zawa;\n// #include \"atcoder/modint\"\n// using mint = atcoder::modint998244353;\nbool solve() {\n int HPINIT, HPMAX;\n std::cin >> HPINIT >> HPMAX;\n if (HPINIT == 0 and HPMAX == 0) return false;\n int R, C;\n std::cin >> R >> C;\n std::vector<std::string> G(R);\n for(auto& g : G) std::cin >> g;\n int T;\n std::cin >> T;\n std::vector<int> TRAP(26);\n for (int i = 0 ; i < T ; i++) {\n char c;\n int d;\n std::cin >> c >> d;\n TRAP[c - 'A'] = d;\n }\n int S;\n std::cin >> S;\n std::vector<std::pair<char, int>> OP(S);\n for (auto& [c, n] : OP) std::cin >> c >> n;\n int P;\n std::cin >> P;\n std::vector<int> POTION(P);\n for (int& p : POTION) std::cin >> p;\n std::vector<int> dp(1 << P);\n dp[(1 << P) - 1] = HPINIT;\n int y = 0, x = 0;\n std::vector<int> recov(1 << P);\n for (int i = 0 ; i < P ; i++) {\n for (int b = 0 ; b < (1 << P) ; b++) if (b & (1 << i)) recov[b] += POTION[i];\n }\n for (auto [c, n] : OP) {\n while (n--) {\n y += (c == 'D' ? 1 : (c == 'U' ? -1 : 0));\n x += (c == 'R' ? 1 : (c == 'L' ? -1 : 0));\n assert(0 <= y and y < R and 0 <= x and x < C);\n const int d = TRAP[G[y][x] - 'A'];\n std::vector<int> next(1 << P);\n for (int b = 0 ; b < (1 << P) ; b++) if (dp[b] > 0) {\n for (int msk = b ; ; msk = (msk - 1) & b) {\n int hp = std::min(HPMAX, dp[b] + recov[msk]);\n hp -= d;\n hp = std::max(0, hp);\n next[b ^ msk] = std::max(next[b ^ msk], hp);\n if (msk == 0) break;\n }\n }\n dp = std::move(next);\n }\n }\n for (int b = 0 ; b < (1 << P) ; b++) if (dp[b] > 0) {\n std::cout << \"YES\\n\";\n return true;\n }\n std::cout << \"NO\\n\";\n return true;\n}\nint main() {\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n std::ios::sync_with_stdio(false);\n while (solve()) ;\n}", "accuracy": 1, "time_ms": 5390, "memory_kb": 3712, "score_of_the_acc": -0.9031, "final_rank": 16 }, { "submission_id": "aoj_2244_10233857", "code_snippet": "// AOJ #2244 Dungeon Quest II\n// 2025.2.20\n\n#include <bits/stdc++.h>\nusing namespace std;\n \nconst int INF = -1;\n\nstruct State { int mask, hp; };\n\nint main() {\n ios::sync_with_stdio(0);\n cin.tie(0);\n \n while(true){\n int HPinit, HPmax;\n cin >> HPinit >> HPmax;\n if(HPinit == 0) break;\n \n int R, C;\n cin >> R >> C;\n vector<string> cave(R);\n for (int i = 0; i < R; i++) cin >> cave[i];\n \n int T;\n cin >> T;\n int trapDamage[26] = {0};\n for (int i = 0; i < T; i++){\n char ch; int d;\n cin >> ch >> d;\n trapDamage[ch-'A'] = d;\n }\n \n int S;\n cin >> S;\n vector<char> dir(S);\n vector<int> nstep(S);\n for (int i = 0; i < S; i++) cin >> dir[i] >> nstep[i];\n\n vector<int> patrol;\n int r = 0, c = 0;\n for (int i = 0; i < S; i++){\n for (int j = 0; j < nstep[i]; j++){\n if(dir[i]=='U') r--;\n else if(dir[i]=='D') r++;\n else if(dir[i]=='L') c--;\n else if(dir[i]=='R') c++;\n int d = trapDamage[cave[r][c]-'A'];\n patrol.push_back(d);\n }\n }\n int totalSteps = patrol.size();\n \n int P;\n cin >> P;\n vector<int> potions(P);\n for (int i = 0; i < P; i++) cin >> potions[i];\n\n int fullMask = (1 << P) - 1;\n \n vector<int> dp(1 << P, INF), nextdp(1 << P, INF);\n dp[0] = HPinit;\n \n for (int step = 0; step < totalSteps; step++){\n int d = patrol[step];\n fill(nextdp.begin(), nextdp.end(), INF);\n for (int mask = 0; mask < (1 << P); mask++){\n if(dp[mask] == INF) continue;\n int curHP = dp[mask];\n if(curHP > d){\n nextdp[mask] = max(nextdp[mask], curHP - d);\n }\n if(curHP <= d && P > 0){\n vector<int> visited(1 << P, INF);\n queue<State> que;\n visited[mask] = curHP;\n que.push({mask, curHP});\n while(!que.empty()){\n State st = que.front();\n que.pop();\n int used = st.mask;\n int hp_now = st.hp;\n int avail = fullMask ^ used;\n for (int i = 0; i < P; i++){\n if(avail & (1 << i)){\n int newMask = used | (1 << i);\n int newHP = hp_now + potions[i];\n if(newHP > HPmax) newHP = HPmax;\n if(newHP > visited[newMask]){\n visited[newMask] = newHP;\n if(newHP > d)\n nextdp[newMask] = max(nextdp[newMask], newHP - d);\n else\n que.push({newMask, newHP});\n }\n }\n }\n }\n }\n }\n dp.swap(nextdp);\n }\n bool possible = false;\n for (int mask = 0; mask < (1 << P); mask++){\n if(dp[mask] > 0) { possible = true; break; }\n }\n cout << (possible ? \"YES\" : \"NO\") << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 180, "memory_kb": 3452, "score_of_the_acc": -0.0454, "final_rank": 2 }, { "submission_id": "aoj_2244_10233840", "code_snippet": "// AOJ #2244 Dungeon Quest II\n// 2025.2.20\n \n#include <bits/stdc++.h>\nusing namespace std;\n \nconst int INF = -1;\n \n \nint main() {\n ios::sync_with_stdio(0);\n cin.tie(0);\n \n while(true){\n int HPinit, HPmax;\n cin >> HPinit >> HPmax;\n if(HPinit==0) break;\n \n int R, C;\n cin >> R >> C;\n vector<string> cave(R);\n for (int i = 0; i < R; i++) cin >> cave[i];\n \n int T;\n cin >> T;\n int trapDamageMapping[26];\n for (int i = 0; i < 26; i++) trapDamageMapping[i] = 0;\n for (int i = 0; i < T; i++){\n char ch;\n int d;\n cin >> ch >> d;\n trapDamageMapping[ch-'A'] = d;\n }\n \n int S;\n cin >> S;\n vector<char> directions;\n vector<int> steps;\n for (int i = 0; i < S; i++){\n char dir;\n int n;\n cin >> dir >> n;\n directions.push_back(dir);\n steps.push_back(n);\n }\n \n vector<int> patrolTrap;\n int r = 0, c = 0;\n for (int i = 0; i < S; i++){\n char d = directions[i];\n int n = steps[i];\n for (int j = 0; j < n; j++){\n if(d=='U') r--;\n else if(d=='D') r++;\n else if(d=='L') c--;\n else if(d=='R') c++;\n char trapType = cave[r][c];\n int damage = trapDamageMapping[trapType-'A'];\n patrolTrap.push_back(damage);\n }\n }\n int totalSteps = patrolTrap.size();\n \n int P;\n cin >> P;\n vector<int> potions(P);\n for (int i = 0; i < P; i++) cin >> potions[i];\n \n if(P==0){\n int hp = HPinit;\n bool survive = true;\n for (int i = 0; i < totalSteps; i++){\n hp -= patrolTrap[i];\n if(hp <= 0){\n survive = false;\n break;\n }\n }\n cout << (survive ? \"YES\" : \"NO\") << endl;\n continue;\n }\n \n int potionStates = 1 << P;\n vector<int> sumPotions(potionStates, 0);\n for (int mask = 0; mask < potionStates; mask++){\n int sum = 0;\n for (int i = 0; i < P; i++)\n if(mask & (1 << i)) sum += potions[i];\n sumPotions[mask] = sum;\n }\n \n vector<int> dp(potionStates, INF);\n dp[0] = HPinit;\n \n for (int step = 0; step < totalSteps; step++){\n vector<int> nextDP(potionStates, INF);\n int damage = patrolTrap[step];\n for (int mask = 0; mask < potionStates; mask++){\n if(dp[mask] == INF) continue;\n int curHP = dp[mask];\n int available = ((1 << P) - 1) ^ mask;\n for (int sub = available;; sub = (sub-1) & available) {\n int recovery = sumPotions[sub];\n int newHP_pre = curHP + recovery;\n if(newHP_pre > HPmax) newHP_pre = HPmax;\n int newHP = newHP_pre - damage;\n if(newHP > 0) {\n int newMask = mask | sub;\n nextDP[newMask] = max(nextDP[newMask], newHP);\n }\n if(sub == 0) break;\n }\n }\n dp = move(nextDP);\n }\n \n bool possible = false;\n for (int mask = 0; mask < potionStates; mask++){\n if(dp[mask] > 0) { possible = true; break; }\n }\n cout << (possible ? \"YES\" : \"NO\") << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 6150, "memory_kb": 3472, "score_of_the_acc": -1.0199, "final_rank": 18 }, { "submission_id": "aoj_2244_9724697", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n/**\n * @brief template\n */\n\nint dx[4] = {1,0,-1,0}, dy[4] = {0,1,0,-1};\n\nint main() {\nwhile(1) {\n int HI, HM;\n cin >> HI >> HM;\n if (HI == 0) return 0;\n int N, M;\n cin >> N >> M;\n vector<string> S(N);\n rep(i,0,N) cin >> S[i];\n vector<vector<int>> A(N,vector<int>(M));\n map<char,int> mp;\n int _;\n cin >> _;\n while(_--) {\n char c;\n int a;\n cin >> c >> a;\n mp[c] = a;\n }\n rep(i,0,N) rep(j,0,M) A[i][j] = mp[S[i][j]];\n vector<int> D;\n int CX = 0, CY = 0;\n cin >> _;\n while(_--) {\n char c;\n int a;\n cin >> c >> a;\n int dir;\n if (c == 'D') dir = 0;\n if (c == 'R') dir = 1;\n if (c == 'U') dir = 2;\n if (c == 'L') dir = 3;\n while(a--) {\n CX += dx[dir], CY += dy[dir];\n D.push_back(A[CX][CY]);\n }\n }\n int K;\n cin >> K;\n vector<int> P(K);\n rep(i,0,K) cin >> P[i];\n int L = D.size();\n vector<vector<int>> DP(L+1,vector<int>(1<<K,0));\n DP[0][0] = HI;\n rep(i,0,L) {\n rep(j,0,1<<K) {\n if (DP[i][j] == 0) continue;\n rep(k,0,K) {\n if (j & (1<<k)) continue;\n chmax(DP[i][j|(1<<k)],min(HM,DP[i][j]+P[k]));\n }\n }\n rep(j,0,1<<K) {\n chmax(DP[i+1][j],DP[i][j]-D[i]);\n }\n }\n bool check = false;\n rep(i,0,1<<K) if (DP[L][i] > 0) check = true;\n cout << (check ? \"YES\" : \"NO\") << endl;\n}\n}", "accuracy": 1, "time_ms": 1900, "memory_kb": 19200, "score_of_the_acc": -0.7978, "final_rank": 13 }, { "submission_id": "aoj_2244_8996502", "code_snippet": "#line 2 \"cp-library/src/cp-template.hpp\"\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nusing i32 = int;\nusing u32 = unsigned int;\nusing i64 = long long;\nusing u64 = unsigned long long;\nusing i128 = __int128_t;\ntemplate < class T > bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }\ntemplate < class T > bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }\ntemplate < class T, class U > T ceil (T x, U y) { return (x > 0 ? (x + y - 1) / y : x / y); }\ntemplate < class T, class U > T floor(T x, U y) { return (x > 0 ? x / y : (x - y + 1) / y); }\nint popcnt(i32 x) { return __builtin_popcount(x); }\nint popcnt(u32 x) { return __builtin_popcount(x); }\nint popcnt(i64 x) { return __builtin_popcountll(x); }\nint popcnt(u64 x) { return __builtin_popcountll(x); }\n\n#line 2 \"cp-library/src/utility/rep_itr.hpp\"\ntemplate < class T > struct itr_rep {\n T i, d;\n constexpr itr_rep(const T i) noexcept : i(i), d(1) {}\n constexpr itr_rep(const T i, const T d) noexcept : i(i), d(d) {}\n void operator++() noexcept { i += d; }\n constexpr int operator*() const noexcept { return i; }\n constexpr bool operator!=(const itr_rep x) const noexcept { return d > 0 ? i < x.i : i > x.i; }\n};\n\ntemplate < class T > struct rep {\n const itr_rep< T > s, t;\n constexpr rep(const T t) noexcept : s(0), t(t) {}\n constexpr rep(const T s, const T t) noexcept : s(s), t(t) {}\n constexpr rep(const T s, const T t, const T d) noexcept : s(s, d), t(t, d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n\ntemplate < class T > struct revrep {\n const itr_rep < T > s, t;\n constexpr revrep(const T t) noexcept : s(t - 1, -1), t(-1, -1) {}\n constexpr revrep(const T s, const T t) noexcept : s(t - 1, -1), t(s - 1, -1) {}\n constexpr revrep(const T s, const T t, const T d) noexcept : s(t - 1, -d), t(s - 1, -d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n#line 3 \"cp-library/src/utility/io.hpp\"\n\n/* 128bit integer */\nistream& operator>>(istream& is, i128& x) {\n std::string s; is >> s;\n int pm = (s[0] == '-');\n x = 0;\n for(int i : rep(pm, int(s.size()))) x = x * 10 + (s[i] - '0');\n if(pm) x *= -1;\n return is;\n}\nostream& operator<<(ostream& os, const i128& x) {\n if(x == 0) return os << '0';\n i128 y = x;\n if(y < 0) { os << '-'; y *= -1; }\n std::vector<int> ny;\n while(y > 0) { ny.push_back(y % 10); y /= 10; }\n for(int i : revrep(ny.size())) os << ny[i];\n return os;\n}\n\ntemplate < class S, class T > istream& operator>>(istream& is, std::pair< S, T >& x) { is >> x.first >> x.second; return is; }\ntemplate < class S, class T > ostream& operator<<(ostream& os, const std::pair< S, T >& x) { os << x.first << \" \" << x.second; return os; }\n\nnamespace scanner {\n struct sca {\n template < class T > operator T() {\n T s; std::cin >> s; return s;\n }\n };\n struct vec {\n int n;\n vec(int n) : n(n) {}\n template < class T > operator std::vector< T >() {\n std::vector< T > v(n);\n for(T& x : v) std::cin >> x;\n return v;\n }\n };\n struct mat {\n int h, w;\n mat(int h, int w) : h(h), w(w) {}\n template < class T > operator std::vector< std::vector< T > >() {\n std::vector m(h, std::vector< T >(w));\n for(std::vector< T >& v : m) for(T& x : v) std::cin >> x;\n return m;\n }\n };\n struct speedup {\n speedup() {\n std::cin.tie(0);\n std::ios::sync_with_stdio(0);\n }\n } speedup_instance;\n}\nscanner::sca in() { return scanner::sca(); }\nscanner::vec in(int n) { return scanner::vec(n); }\nscanner::mat in(int h, int w) { return scanner::mat(h, w); }\n\nnamespace printer {\n void precision(int d) { std::cout << std::fixed << std::setprecision(d); }\n void flush() { std::cout.flush(); }\n}\n\ntemplate < class T >\nostream& operator<<(ostream& os, const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) { os << a[i]; if(i != n - 1) os << ' '; }\n return os;\n}\n\nint print() { std::cout << '\\n'; return 0; }\ntemplate < class head, class... tail > int print(head&& h, tail&&... t) {\n std::cout << h; if(sizeof...(tail)) std::cout << ' ';\n return print(std::forward<tail>(t)...);\n}\ntemplate < class T > int print_n(const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) std::cout << a[i] << \"\\n\";\n return 0;\n}\n\n\n#line 2 \"cp-library/src/utility/key_val.hpp\"\n\ntemplate < class K, class V >\nstruct key_val {\n K key; V val;\n key_val() {}\n key_val(K key, V val) : key(key), val(val) {}\n template < std::size_t Index >\n std::tuple_element_t< Index, key_val >& get() {\n if constexpr (Index == 0) return key;\n if constexpr (Index == 1) return val;\n }\n};\n\nnamespace std {\n\ntemplate < class K, class V > struct tuple_size < key_val< K, V > > : integral_constant< size_t, 2 > {};\ntemplate < class K, class V > struct tuple_element < 0, key_val< K, V > > { using type = K; };\ntemplate < class K, class V > struct tuple_element < 1, key_val< K, V > > { using type = V; };\n\n}\n#line 2 \"cp-library/src/utility/vec_op.hpp\"\ntemplate < class T > key_val< int, T > max_of(const vector< T >& a) {\n int i = std::max_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class T > key_val< int, T > min_of(const vector< T >& a) {\n int i = std::min_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class S, class T > S sum_of(const vector< T >& a) {\n S sum = 0;\n for(const T x : a) sum += x;\n return sum;\n}\ntemplate < class S, class T > vector< S > freq_of(const vector< T >& a, T L, T R) {\n vector< S > res(R - L, S(0));\n for(const T x : a) res[x - L] += 1;\n return res;\n}\ntemplate < class S, class T > struct prefix_sum {\n vector< S > s;\n prefix_sum(const vector< T >& a) : s(a) {\n s.insert(s.begin(), S(0));\n for(int i : rep(a.size())) s[i + 1] += s[i];\n }\n // [L, R)\n S sum(int L, int R) { return s[R] - s[L]; }\n};\n#line 3 \"cp-library/src/utility/heap.hpp\"\n\ntemplate < class T > using heap_min = std::priority_queue< T, std::vector< T >, std::greater< T > >;\ntemplate < class T > using heap_max = std::priority_queue< T, std::vector< T >, std::less< T > >;\n\n#line 27 \"cp-library/src/cp-template.hpp\"\n\n#line 1 \"cp-library/src/algorithm/bin_search.hpp\"\ntemplate < class T, class F >\nT bin_search(T ok, T ng, F f) {\n while(abs(ng - ok) > 1) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n\ntemplate < class T, class F >\nT bin_search_real(T ok, T ng, F f, int step = 80) {\n while(step--) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n#line 2 \"cp-library/src/algorithm/argsort.hpp\"\n\ntemplate < class T > std::vector< int > argsort(const std::vector< T > &a) {\n std::vector< int > ids((int)a.size());\n std::iota(ids.begin(), ids.end(), 0);\n std::sort(ids.begin(), ids.end(), [&](int i, int j) {\n return a[i] < a[j] || (a[i] == a[j] && i < j);\n });\n return ids;\n}\n#line 1 \"macro.hpp\"\nnamespace macro {\n\nusing size_type = int;\ntemplate < class container > void sort(container& a) { std::sort(std:: begin(a), std:: end(a)); }\ntemplate < class container > void rsort(container& a) { std::sort(std::rbegin(a), std::rend(a)); }\ntemplate < class container > void reverse(container& a) { std::reverse(std::begin(a), std::end(a)); }\ntemplate < class container > void unique(container& a) {\n std::sort(std::begin(a), std::end(a));\n a.erase(std::unique(std::begin(a), std::end(a)), std::end(a));\n}\ntemplate < class container > container sorted(const container& a) { container b = a; sort(b); return std::move(b); }\ntemplate < class container > container rsorted(const container& a) { container b = a; rsort(b); return std::move(b); }\ntemplate < class container, class compare > void sort(container& a, const compare& cmp) { std::sort(std::begin(a), std::end(a), cmp); }\ntemplate < class container, class compare > container sorted(const container& a, const compare& cmp) { container b = a; sort(b, cmp); return std::move(b); }\ntemplate < class container, class value > size_type lower_bound(const container& a, const value& x) { return std::lower_bound(std::begin(a), std::end(a), x) - std::begin(a); }\ntemplate < class container, class value > size_type upper_bound(const container& a, const value& x) { return std::upper_bound(std::begin(a), std::end(a), x) - std::begin(a); }\n\nconst std::vector<std::pair<size_type, size_type>> dir4 = { {+1, 0}, {-1, 0}, { 0, +1}, { 0, -1} };\nconst std::vector<std::pair<size_type, size_type>> dir8 = { {-1, -1}, {-1, 0}, {-1, +1}, { 0, -1}, { 0, +1}, {+1, -1}, {+1, 0}, {+1, +1} };\n\n#ifdef _DEBUG\n#define debug(x) std::cout << \"[\" << __LINE__ << \"] \" << #x << \": \" << x << std::endl\n#else\n#define debug(x)\n#endif\n\ntemplate < class container > void concat(container& a, const container& b) {\n a.insert(std::end(a), std::begin(b), std::end(b));\n}\nstd::vector<size_type> iota(const size_type n) {\n std::vector<size_type> I(n);\n std::iota(std::begin(I), std::end(I), 0);\n return I;\n}\ntemplate < class container > std::vector<size_type> sort_idx(const container& a) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return a[i] < a[j] or (a[i] == a[j] and i < j); });\n return I;\n}\ntemplate < class container, class compare > std::vector<size_type> sort_idx(const container& a, const compare& cmp) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return cmp(a[i], a[j]) or (a[i] == a[j] and i < j); });\n return std::move(I);\n}\n\nstruct grid {\n using size_type = int;\n size_type H, W;\n grid(const size_type H, const size_type W) : H(H), W(W) {}\n bool contains(const size_type i, const size_type j) {\n return 0 <= i and i < H and 0 <= j and j < W;\n }\n};\n\nusing f64 = long double;\n\n} // namespace macro\n\nusing namespace macro;\n#line 3 \"A.cpp\"\n\nstring solve(int HP_init, int HP_max) {\n int H = in(), W = in();\n vector<string> a = in(H);\n int T = in();\n map<char, int> damage;\n for(int i : rep(T)) {\n char c = in(); int d = in();\n damage[c] = d;\n }\n int S = in();\n vector<char> move;\n for(int i : rep(S)) {\n char d = in(); int n = in();\n for(int _ : rep(n)) move.push_back(d);\n }\n int P = in();\n vector<int> potion = in(P);\n\n vector<int> dp(1 << P, -1e9); // 使ったポーションの集合 -> 体力の最大値\n dp[0] = HP_init;\n\n int i = 0, j = 0;\n for(char m : move) {\n if(m == 'U') i--;\n if(m == 'D') i++;\n if(m == 'L') j--;\n if(m == 'R') j++;\n\n int dmg = damage[a[i][j]];\n vector<int> nt(1 << P, -1e9);\n for(int set : rep(1 << P)) {\n if(0 < dp[set] - dmg) chmax(nt[set], dp[set] - dmg);\n for(int i : rep(P)) if(!(set & (1 << i))) {\n int hp = min(dp[set] + potion[i], HP_max);\n if(0 < hp - dmg) chmax(nt[set ^ (1 << i)], hp - dmg);\n }\n }\n dp = std::move(nt);\n }\n\n for(int st : rep(1 << P)) if(0 < dp[st]) return \"YES\";\n return \"NO\";\n}\n\nint main() {\n while(true) {\n int HP_init = in(), HP_max = in();\n if(make_pair(HP_init, HP_max) == make_pair(0, 0)) return 0;\n print(solve(HP_init, HP_max));\n }\n}", "accuracy": 1, "time_ms": 2930, "memory_kb": 3436, "score_of_the_acc": -0.4935, "final_rank": 9 }, { "submission_id": "aoj_2244_8031684", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rng(i, l, r) for(int i = int(l); i < int(r); i++)\n#define rep(i, n) rng(i, 0, n)\n#define sz(v) int(v.size())\n#define foa(s, v) for(auto& s : v)\n#define all(v) v.begin(), v.end()\ntemplate <class T>\nusing V = vector<T>;\n\ntemplate <class T>\nbool chmax(T& a, T b) {\n\treturn a < b ? a = b, 1 : 0;\n}\ntemplate <class T>\nbool chmin(T& a, T b) {\n\treturn a > b ? a = b, 1 : 0;\n}\n\n#ifdef LOCAL\n#define debug(x) cerr << #x << \": \" << x << endl\n#else\n#define debug(x) void(0)\n#endif\n\nostream& operator<<(ostream& os, vector<int> vec) {\n\tos << \"{\";\n\tfoa(c, vec) os << c << \", \";\n\treturn os << \"}\";\n}\n\nint start_hp;\nbool solve() {\n\tint max_hp;\n\tcin >> max_hp;\n\tdebug(start_hp);\n\tdebug(max_hp);\n\tint h, w;\n\tcin >> h >> w;\n\tdebug(h);\n\tdebug(w);\n\tvector<string> grid(h);\n\tfoa(r, grid) cin >> r;\n\n\tfoa(c, grid) debug(c);\n\n\tmap<char, int> damages;\n\t{\n\t\tint t;\n\t\tcin >> t;\n\t\trep(j, t) {\n\t\t\tchar c;\n\t\t\tcin >> c;\n\t\t\tint d;\n\t\t\tcin >> d;\n\t\t\tdamages[c] = d;\n\t\t}\n\t}\n\n\tmap<char, pair<int, int>> command;\n\tcommand['U'] = pair(-1, 0);\n\tcommand['L'] = pair(0, -1);\n\tcommand['D'] = pair(1, 0);\n\tcommand['R'] = pair(0, 1);\n\tvector<pair<char, int>> commands;\n\t{\n\t\tint s;\n\t\tcin >> s;\n\t\trep(j, s) {\n\t\t\tchar cmd;\n\t\t\tcin >> cmd;\n\t\t\tint n;\n\t\t\tcin >> n;\n\t\t\tcommands.emplace_back(cmd, n);\n\t\t}\n\t}\n\n\tint potions_num;\n\tcin >> potions_num;\n\tvector<int> potions(potions_num);\n\tfoa(c, potions) cin >> c;\n\n\tdebug(potions_num);\n\n\tconst int bitmax = 1 << potions_num;\n\tvector<int> psums(bitmax);\n\trep(bits, (bitmax)) rep(j, potions_num) {\n\t\tif(bits & (1 << j)) { psums[bits] += potions[j]; }\n\t}\n\n\tfoa(p, potions) debug(p);\n\n\tvector<int> remain_max(bitmax, 0);\n\tremain_max.back() = start_hp;\n\n\tint x, y;\n\tx = y = 0;\n\tfor(auto [cmd, repeat] : commands) {\n\t\tint dx, dy;\n\t\ttie(dx, dy) = command.at(cmd);\n\t\tdebug(dx);\n\t\tdebug(dy);\n\n\t\trep(j, repeat) {\n\t\t\tvector<int> tmp(bitmax, 0);\n\n\t\t\tx += dx;\n\t\t\ty += dy;\n\n\t\t\tdebug(x);\n\t\t\tdebug(y);\n\n\t\t\tassert(0 <= x && x < h && 0 <= y && y < w);\n\t\t\tint dam = damages.at(grid[x][y]);\n\t\t\tdebug(dam);\n\n\t\t\trep(bits, bitmax) {\n\t\t\t\tint pre = remain_max[bits];\n\t\t\t\tif(pre <= 0) continue;\n\t\t\t\tfor(int use = bits; use >= 0; use--) {\n\t\t\t\t\tuse &= bits;\n\t\t\t\t\tint remain = min(max_hp, pre + psums[use]);\n\t\t\t\t\tchmax(tmp[bits - use], remain - dam);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif(*max_element(all(tmp)) <= 0) return false;\n\t\t\tswap(tmp, remain_max);\n\n\t\t\tdebug(remain_max);\n\t\t}\n\t}\n\treturn true;\n}\nint main() {\n\tios::sync_with_stdio(false);\n\tcin.tie(nullptr);\n\twhile(cin >> start_hp && start_hp) cout << (solve() ? \"YES\" : \"NO\") << endl;\n}", "accuracy": 1, "time_ms": 5430, "memory_kb": 3340, "score_of_the_acc": -0.8985, "final_rank": 15 }, { "submission_id": "aoj_2244_7227261", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)\n#define revrep(i, t, s) for (int i = (int)(t)-1; i >= (int)(s); --i)\n#define all(x) begin(x), end(x)\ntemplate <typename T> bool chmax(T& a, const T& b) { return a < b ? (a = b, 1) : 0; }\ntemplate <typename T> bool chmin(T& a, const T& b) { return a > b ? (a = b, 1) : 0; }\n\ntemplate <typename T>\nostream& operator<<(ostream& os, const vector<T>& v) {\n for (int i = 0; i < (int) v.size(); ++i) {\n os << v[i];\n if (i < (int) v.size() - 1) os << \" \";\n }\n return os;\n}\n\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << fixed << setprecision(15);\n\n const vector<int> di = {0, 1, 0, -1};\n const vector<int> dj = {1, 0, -1, 0};\n string dir = \"RDLU\";\n\n auto get_dir = [&](char d) {\n rep(i,0,4) if (dir[i] == d) return i;\n return -1;\n };\n\n while (true) {\n int hpinit, hpmax;\n cin >> hpinit >> hpmax;\n if (hpinit == 0) break;\n int R, C;\n cin >> R >> C;\n vector<string> a(R);\n for (auto& x : a) cin >> x;\n int T;\n cin >> T;\n vector<int> damage(26);\n rep(i,0,T) {\n char c;\n int d;\n cin >> c >> d;\n damage[c-'A'] = d;\n }\n int S;\n cin >> S;\n vector<pair<char, int>> moves(S);\n for (auto& x : moves) cin >> x.first >> x.second;\n int P;\n cin >> P;\n vector<int> p(P);\n for (auto& x : p) cin >> x;\n\n vector<int> dam = {0};\n int i = 0, j = 0;\n for (auto [c, d] : moves) {\n int k = get_dir(c);\n rep(_,0,d) {\n i += di[k];\n j += dj[k];\n dam.push_back(damage[a[i][j]-'A']);\n }\n }\n\n // cout << dam << endl;\n\n vector<int> dp(1<<P);\n dp.back() = hpinit;\n for (auto d : dam) {\n revrep(B,(1<<P),0) {\n if (dp[B] == 0) continue;\n rep(i,0,P) {\n if (B>>i&1) {\n chmax(dp[B^(1<<i)], min(dp[B] + p[i], hpmax));\n }\n }\n }\n rep(B,0,(1<<P)) {\n dp[B] = max(dp[B] - d, 0);\n }\n }\n\n bool ans = false;\n rep(B,0,(1<<P)) {\n if (dp[B] > 0) {\n ans = true;\n break;\n }\n }\n\n cout << (ans ? \"YES\" : \"NO\") << endl;\n }\n}", "accuracy": 1, "time_ms": 1660, "memory_kb": 3184, "score_of_the_acc": -0.2788, "final_rank": 5 }, { "submission_id": "aoj_2244_6395151", "code_snippet": "#include \"bits/stdc++.h\"\nusing namespace std;\n\ntypedef string::const_iterator State;\n#define eps 1e-8L\n#define MAX_MOD 1000000007LL\n#define GYAKU 500000004LL\n#define MOD 998244353LL\n#define pb push_back\n#define mp make_pair\ntypedef long long ll;\ntypedef long double ld;\n#define REP(a, b) for (long long(a) = 0; (a) < (b); ++(a))\n#define ALL(x) (x).begin(), (x).end()\n\n#define int long long\nint dp[2000][(1 << 12)];\nvoid solve()\n{\n int hp_init, hp_max;\n int r, c;\n cin >> hp_init >> hp_max;\n if (hp_init == 0)\n exit(0);\n cin >> r >> c;\n vector<string> inputs;\n REP(i, r)\n {\n string s;\n cin >> s;\n inputs.push_back(s);\n }\n int tea;\n cin >> tea;\n map<char, int> costs;\n REP(i, tea)\n {\n string s;\n int b;\n cin >> s >> b;\n costs[s[0]] = b;\n }\n int moves = 0;\n int x = 0;\n int y = 0;\n cin >> moves;\n vector<int> move_costs;\n REP(i, moves)\n {\n string s;\n int n;\n cin >> s >> n;\n REP(q, n)\n {\n if (s == \"D\")\n {\n x++;\n }\n else if (s == \"U\")\n {\n x--;\n }\n else if (s == \"R\")\n {\n y++;\n }\n else\n {\n y--;\n }\n move_costs.push_back(costs[inputs[x][y]]);\n }\n }\n vector<int> portions;\n int ps;\n cin >> ps;\n REP(i, ps)\n {\n int a;\n cin >> a;\n portions.push_back(a);\n }\n\n REP(i, move_costs.size() + 1)\n {\n REP(q, (1 << portions.size()))\n {\n dp[i][q] = -1e18;\n }\n }\n dp[0][0] = hp_init;\n REP(i, move_costs.size())\n {\n REP(j, portions.size())\n {\n REP(q, (1 << portions.size()))\n {\n if ((1 << j) & q)\n {\n continue;\n }\n if (dp[i][q] <= 0)\n continue;\n dp[i][q | (1 << j)] = max(dp[i][q | (1 << j)], dp[i][q] + portions[j]);\n dp[i][q | (1 << j)] = min(dp[i][q | (1 << j)], hp_max);\n }\n }\n REP(q, (1 << portions.size()))\n {\n dp[i + 1][q] = dp[i][q] - move_costs[i];\n }\n }\n REP(i, (1 << portions.size()))\n {\n if (dp[move_costs.size()][i] > 0)\n {\n cout << \"YES\" << endl;\n return;\n }\n }\n cout << \"NO\" << endl;\n}\n\n#undef int\n\n// generated by oj-template v4.7.2\n// (https://github.com/online-judge-tools/template-generator)\nint main()\n{\n // Fasterize input/output script\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << fixed << setprecision(10);\n // scanf/printf user should delete this fasterize input/output script\n\n int t = 10000;\n // cin >> t; // comment out if solving multi testcase\n for (int testCase = 1; testCase <= t; ++testCase)\n {\n solve();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 710, "memory_kb": 36184, "score_of_the_acc": -1.1126, "final_rank": 20 }, { "submission_id": "aoj_2244_5947820", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nconst int INF = 1e9;\n\nvoid solve(int HP_init, int HP_max) {\n int R, C;\n cin >> R >> C;\n vector<string> cave(R);\n for (auto& s : cave) cin >> s;\n int T;\n cin >> T;\n vector<int> trap(26, 0);\n for (; T--;) {\n char c;\n int d;\n cin >> c >> d;\n trap[c - 'A'] = d;\n }\n int S;\n cin >> S;\n vector<int> path;\n for (int x = 0, y = 0; S--;) {\n char dir;\n int n;\n cin >> dir >> n;\n for (; n--;) {\n if (dir == 'U')\n x--;\n else if (dir == 'D')\n x++;\n else if (dir == 'L')\n y--;\n else\n y++;\n path.emplace_back(trap[cave[x][y] - 'A']);\n }\n }\n int P;\n cin >> P;\n vector<int> p(P);\n for (int& x : p) cin >> x;\n\n int N = path.size();\n vector<vector<int>> dp(N + 1, vector<int>(1 << P, -INF));\n dp[0][0] = HP_init;\n for (int i = 0; i < N; i++) {\n for (int mask = 0; mask < (1 << P); mask++) {\n int val = dp[i][mask];\n if (val < 0) continue;\n for (int j = 0; j < P; j++) {\n if (mask >> j & 1) continue;\n dp[i][mask | 1 << j] = max(dp[i][mask | 1 << j], min(HP_max, val + p[j]));\n }\n if (val > path[i]) dp[i + 1][mask] = max(dp[i + 1][mask], val - path[i]);\n }\n }\n\n cout << (dp[N][(1 << P) - 1] > 0 ? \"YES\" : \"NO\") << '\\n';\n}\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n int HP_init, HP_max;\n while (cin >> HP_init >> HP_max, HP_init) solve(HP_init, HP_max);\n return 0;\n}", "accuracy": 1, "time_ms": 1960, "memory_kb": 19112, "score_of_the_acc": -0.805, "final_rank": 14 }, { "submission_id": "aoj_2244_5947819", "code_snippet": "#define LOCAL\n#include <bits/stdc++.h>\nusing namespace std;\n#pragma region Macros\ntypedef long long ll;\ntypedef __int128_t i128;\ntypedef unsigned int uint;\ntypedef unsigned long long ull;\n#define ALL(x) (x).begin(), (x).end()\n\ntemplate <typename T> istream& operator>>(istream& is, vector<T>& v) {\n for (T& x : v) is >> x;\n return is;\n}\ntemplate <typename T> ostream& operator<<(ostream& os, const vector<T>& v) {\n for (int i = 0; i < (int)v.size(); i++) {\n os << v[i] << (i + 1 == (int)v.size() ? \"\" : \" \");\n }\n return os;\n}\ntemplate <typename T, typename U> ostream& operator<<(ostream& os, const pair<T, U>& p) {\n os << '(' << p.first << ',' << p.second << ')';\n return os;\n}\ntemplate <typename T, typename U> ostream& operator<<(ostream& os, const map<T, U>& m) {\n os << '{';\n for (auto itr = m.begin(); itr != m.end();) {\n os << '(' << itr->first << ',' << itr->second << ')';\n if (++itr != m.end()) os << ',';\n }\n os << '}';\n return os;\n}\ntemplate <typename T, typename U> ostream& operator<<(ostream& os, const unordered_map<T, U>& m) {\n os << '{';\n for (auto itr = m.begin(); itr != m.end();) {\n os << '(' << itr->first << ',' << itr->second << ')';\n if (++itr != m.end()) os << ',';\n }\n os << '}';\n return os;\n}\ntemplate <typename T> ostream& operator<<(ostream& os, const set<T>& s) {\n os << '{';\n for (auto itr = s.begin(); itr != s.end();) {\n os << *itr;\n if (++itr != s.end()) os << ',';\n }\n os << '}';\n return os;\n}\ntemplate <typename T> ostream& operator<<(ostream& os, const multiset<T>& s) {\n os << '{';\n for (auto itr = s.begin(); itr != s.end();) {\n os << *itr;\n if (++itr != s.end()) os << ',';\n }\n os << '}';\n return os;\n}\ntemplate <typename T> ostream& operator<<(ostream& os, const unordered_set<T>& s) {\n os << '{';\n for (auto itr = s.begin(); itr != s.end();) {\n os << *itr;\n if (++itr != s.end()) os << ',';\n }\n os << '}';\n return os;\n}\ntemplate <typename T> ostream& operator<<(ostream& os, const deque<T>& v) {\n for (int i = 0; i < (int)v.size(); i++) {\n os << v[i] << (i + 1 == (int)v.size() ? \"\" : \" \");\n }\n return os;\n}\n\ntemplate <int i, typename T> void print_tuple(ostream&, const T&) {}\ntemplate <int i, typename T, typename H, class... Args> void print_tuple(ostream& os, const T& t) {\n if (i) os << ',';\n os << get<i>(t);\n print_tuple<i + 1, T, Args...>(os, t);\n}\ntemplate <typename... Args> ostream& operator<<(ostream& os, const tuple<Args...>& t) {\n os << '{';\n print_tuple<0, tuple<Args...>, Args...>(os, t);\n return os << '}';\n}\n\nvoid debug_out() { cerr << '\\n'; }\ntemplate <class Head, class... Tail> void debug_out(Head&& head, Tail&&... tail) {\n cerr << head;\n if (sizeof...(Tail) > 0) cerr << \", \";\n debug_out(move(tail)...);\n}\n#ifdef LOCAL\n#define debug(...) \\\n cerr << \" \"; \\\n cerr << #__VA_ARGS__ << \" :[\" << __LINE__ << \":\" << __FUNCTION__ << \"]\" << '\\n'; \\\n cerr << \" \"; \\\n debug_out(__VA_ARGS__)\n#else\n#define debug(...) 42\n#endif\n\ntemplate <typename T> T gcd(T x, T y) { return y != 0 ? gcd(y, x % y) : x; }\ntemplate <typename T> T lcm(T x, T y) { return x / gcd(x, y) * y; }\n\nint topbit(signed t) { return t == 0 ? -1 : 31 - __builtin_clz(t); }\nint topbit(long long t) { return t == 0 ? -1 : 63 - __builtin_clzll(t); }\nint botbit(signed a) { return a == 0 ? 32 : __builtin_ctz(a); }\nint botbit(long long a) { return a == 0 ? 64 : __builtin_ctzll(a); }\nint popcount(signed t) { return __builtin_popcount(t); }\nint popcount(long long t) { return __builtin_popcountll(t); }\nbool ispow2(int i) { return i && (i & -i) == i; }\n\ntemplate <class T> T ceil(T x, T y) {\n assert(y >= 1);\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <class T> T floor(T x, T y) {\n assert(y >= 1);\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\n\ntemplate <class T1, class T2> inline bool chmin(T1& a, T2 b) {\n if (a > b) {\n a = b;\n return true;\n }\n return false;\n}\ntemplate <class T1, class T2> inline bool chmax(T1& a, T2 b) {\n if (a < b) {\n a = b;\n return true;\n }\n return false;\n}\n#pragma endregion\n\nconst int INF = 1e9;\nconst long long IINF = 1e18;\nconst int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};\nconst long long MOD = 1000000007;\n// const long long MOD = 998244353;\n\nvoid solve(int HP_init, int HP_max) {\n int R, C;\n cin >> R >> C;\n vector<string> cave(R);\n for (auto& s : cave) cin >> s;\n int T;\n cin >> T;\n vector<int> trap(26, 0);\n for (; T--;) {\n char c;\n int d;\n cin >> c >> d;\n trap[c - 'A'] = d;\n }\n int S;\n cin >> S;\n vector<int> path;\n for (int x = 0, y = 0; S--;) {\n char dir;\n int n;\n cin >> dir >> n;\n for (; n--;) {\n if (dir == 'U')\n x--;\n else if (dir == 'D')\n x++;\n else if (dir == 'L')\n y--;\n else\n y++;\n path.emplace_back(trap[cave[x][y] - 'A']);\n }\n }\n int P;\n cin >> P;\n vector<int> p(P);\n for (int& x : p) cin >> x;\n\n int N = path.size();\n vector<vector<int>> dp(N + 1, vector<int>(1 << P, -INF));\n dp[0][0] = HP_init;\n for (int i = 0; i < N; i++) {\n for (int mask = 0; mask < (1 << P); mask++) {\n int val = dp[i][mask];\n if (val < 0) continue;\n for (int j = 0; j < P; j++) {\n if (mask >> j & 1) continue;\n dp[i][mask | 1 << j] = max(dp[i][mask | 1 << j], min(HP_max, val + p[j]));\n }\n if (val > path[i]) dp[i + 1][mask] = max(dp[i + 1][mask], val - path[i]);\n }\n }\n\n cout << (dp[N][(1 << P) - 1] > 0 ? \"YES\" : \"NO\") << '\\n';\n}\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n int HP_init, HP_max;\n while (cin >> HP_init >> HP_max, HP_init) solve(HP_init, HP_max);\n return 0;\n}", "accuracy": 1, "time_ms": 1790, "memory_kb": 19148, "score_of_the_acc": -0.7783, "final_rank": 12 }, { "submission_id": "aoj_2244_5927877", "code_snippet": "#pragma GCC ta g(\"avx2\")\n#pragma GCC optimize(\"Ofast\")\n#pragma GCC optimize(\"unroll-loops\")\n#include <bits/stdc++.h>\nusing namespace std;\n#define DEBUG\n#ifdef DEBUG\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << '(' << p.first << ',' << p.second << ')';\n return os;\n}\ntemplate <class T> ostream &operator<<(ostream &os, const vector<T> &v) {\n os << '{';\n for(int i = 0; i < (int)v.size(); i++) {\n if(i) { os << ','; }\n os << v[i];\n }\n os << '}';\n return os;\n}\nvoid debugg() { cerr << endl; }\ntemplate <class T, class... Args>\nvoid debugg(const T &x, const Args &... args) {\n cerr << \" \" << x;\n debugg(args...);\n}\n#define debug(...) \\\n cerr << __LINE__ << \" [\" << #__VA_ARGS__ << \"]: \", debugg(__VA_ARGS__)\n#define dump(x) cerr << __LINE__ << \" \" << #x << \" = \" << (x) << endl\n#else\n#define debug(...) (void(0))\n#define dump(x) (void(0))\n#endif\nusing namespace std;\ntypedef long long ll;\ntypedef vector<ll> vl;\ntypedef vector<vl> vvl;\ntypedef vector<char> vc;\ntypedef vector<string> vs;\ntypedef vector<bool> vb;\ntypedef vector<double> vd;\ntypedef pair<ll,ll> P;\ntypedef pair<int,int> pii;\ntypedef vector<P> vpl;\ntypedef tuple<ll,ll,ll> tapu;\n#define rep(i,n) for(int i=0; i<(n); i++)\n#define REP(i,a,b) for(int i=(a); i<(b); i++)\n#define all(x) (x).begin(), (x).end()\n#define rall(x) (x).rbegin(), (x).rend()\nconst int inf = (1<<30)-1;\nconst ll linf = 1LL<<61;\nconst int MAX = 510000;\nint dy[8] = {0,1,0,-1,1,-1,-1,1};\nint dx[8] = {-1,0,1,0,1,-1,1,-1};\nconst double pi = acos(-1);\nconst double eps = 1e-7;\ntemplate<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){\n\tif(a>b){\n\t\ta = b; return true;\n\t}\n\telse return false;\n}\ntemplate<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){\n\tif(a<b){\n\t\ta = b; return true;\n\t}\n\telse return false;\n}\ntemplate<typename T> inline void print(T &a){\n int sz = a.size();\n for(auto itr = a.begin(); itr != a.end(); itr++){\n\t\tcout << *itr;\n sz--;\n if(sz) cout << \" \";\n\t}\n cout << \"\\n\";\n}\ntemplate<typename T1,typename T2> inline void print2(T1 a, T2 b){\n\tcout << a << \" \" << b << \"\\n\";\n}\ntemplate<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){\n\tcout << a << \" \" << b << \" \" << c << \"\\n\";\n}\nvoid mark() {cout << \"#\" << \"\\n\";}\nll pcount(ll x) {return __builtin_popcountll(x);}\n//#include <atcoder/maxflow>\n//using namespace atcoder;\n//const int mod = 1e9 + 7;\nconst int mod = 998244353;\n\nint main(){\n while(1){\n int fi,m; cin >> fi >> m;\n if(!fi) break;\n int r,c; cin >> r >> c;\n vs s(r); rep(i,r) cin >> s[i];\n vl d(26);\n int t; cin >> t;\n rep(i,t){\n char c; int D; cin >> c >> D;\n d[c-'A'] = D;\n }\n int n; cin >> n;\n vl dir(n), a(n);\n string di = \"LDRU\";\n rep(i,n){\n char c;\n cin >> c >> a[i];\n rep(j,4) if(di[j] == c) dir[i] = j;\n }\n int k; cin >> k;\n vector<int> p(k); rep(i,k) cin >> p[i];\n vector<int> dp(1<<k,0);\n dp[0] = fi;\n int y = 0, x = 0;\n rep(i,n){\n rep(j,a[i]){\n y += dy[dir[i]];\n x += dx[dir[i]];\n int damage = d[s[y][x]-'A'];\n rep(l,k){\n rep(bit,1<<k){\n if(dp[bit] <= 0) continue;\n if(bit>>l&1^1) chmax(dp[bit|(1<<l)], min(m, dp[bit] + p[l]));\n }\n }\n rep(bit,1<<k) dp[bit] -= damage;\n }\n }\n bool yes = false;\n rep(bit,1<<k) if(dp[bit] > 0) yes = true;\n if(yes) cout << \"YES\\n\";\n else cout << \"NO\\n\";\n }\n}", "accuracy": 1, "time_ms": 620, "memory_kb": 3464, "score_of_the_acc": -0.1175, "final_rank": 3 }, { "submission_id": "aoj_2244_5844064", "code_snippet": "#pragma GCC optimize(\"Ofast\")\n#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <map>\n#include <queue>\n#include <cstdio>\n#include <ctime>\n#include <assert.h>\n#include <chrono>\n#include <random>\n#include <numeric>\n#include <set>\n#include <deque>\n#include <stack>\n#include <sstream>\n#include <utility>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned long long ull;\n\nmt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());\nll myRand(ll B) {\n return (ull)rng() % B;\n}\n\nint main(){\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n ll cur,H;\n while(cin >> cur >> H,H){\n int h,w; cin >> h >> w;\n vector<string> s(h);\n for(int i=0;i<h;i++){\n cin >> s[i];\n }\n vector<ll> cost(26);\n int m; cin >> m;\n for(int i=0;i<m;i++){\n char c; cin >> c;\n cin >> cost[c-'A'];\n }\n vector<ll> v;\n int x=0,y=0;\n cin >> m;\n for(int i=0;i<m;i++){\n char c; cin >> c;\n int d; cin >> d;\n for(int j=0;j<d;j++){\n if(c=='D')x++;\n else if(c=='U')x--;\n else if(c=='R')y++;\n else y--;\n v.push_back(cost[s[x][y]-'A']);\n }\n }\n cin >> m;\n vector<int> p(m);\n for(int i=0;i<m;i++){\n cin >> p[i];\n }\n vector<ll> dp(1<<m);\n dp[0]=cur;\n for(int i:v){\n for(int j=0;j<(1<<m);j++){\n if(dp[j]<=0)continue;\n for(int k=0;k<m;k++){\n if((1<<k)&j)continue;\n dp[j|(1<<k)]=max(dp[j|(1<<k)],min(H,dp[j]+p[k]));\n }\n }\n for(int j=0;j<(1<<m);j++){\n dp[j]-=i;\n }\n }\n if(dp.back()>0){\n printf(\"YES\\n\");\n }\n else{\n printf(\"NO\\n\");\n }\n }\n}", "accuracy": 1, "time_ms": 1940, "memory_kb": 3400, "score_of_the_acc": -0.331, "final_rank": 7 }, { "submission_id": "aoj_2244_5034657", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\nusing PII = pair<ll, ll>;\n\n#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)\n#define REP(i, n) FOR(i, 0, n)\n#define ALL(x) x.begin(), x.end()\n#define POPCOUNT(x) __builtin_popcount(x)\ntemplate <typename T> void chmin(T &a, const T &b) { a = min(a, b); }\ntemplate <typename T> void chmax(T &a, const T &b) { a = max(a, b); }\n\nconst ll INF = 1LL << 60;\n\nstruct FastIO {\n FastIO() {\n cin.tie(0);\n ios::sync_with_stdio(0);\n }\n} fastiofastio;\n\nint dp[1001][1 << 12];\nint d[26];\nchar a[100][101];\n\nint main() {\n while (1) {\n int HP, HPmax;\n cin >> HP >> HPmax;\n if (HP == 0)\n break;\n int R, C;\n cin >> R >> C;\n for (int i = 0; i < R; i++) {\n cin >> a[i];\n }\n int T;\n cin >> T;\n for (int i = 0; i < T; i++) {\n string s;\n cin >> s;\n cin >> d[s[0] - 'A'];\n }\n int S;\n cin >> S;\n vector<string> route;\n for (int i = 0; i < S; i++) {\n string dir;\n int n;\n cin >> dir >> n;\n for (int j = 0; j < n; j++)\n route.push_back(dir);\n }\n int P;\n cin >> P;\n vector<int> p(P);\n for (int i = 0; i < P; i++)\n cin >> p[i];\n memset(dp, 0, sizeof(dp));\n dp[0][0] = HP;\n int x = 0, y = 0;\n for (int i = 0; i < route.size(); i++) {\n const string &dir = route[i];\n if (dir == \"U\")\n y--;\n else if (dir == \"D\")\n y++;\n else if (dir == \"R\")\n x++;\n else\n x--;\n int damage = d[a[y][x] - 'A'];\n for (int j = 0; j < (1 << P); j++) {\n if (dp[i][j] == 0)\n continue;\n if (dp[i][j] > damage)\n dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] - damage);\n for (int k = 0; k < P; k++) {\n if (j & (1 << k))\n continue;\n int new_HP = min(dp[i][j] + p[k], HPmax);\n if (new_HP > damage)\n dp[i + 1][j | (1 << k)] =\n max(dp[i + 1][j | (1 << k)], new_HP - damage);\n }\n }\n }\n int ans = 0;\n for (int i = 0; i < (1 << P); i++)\n ans = max(ans, dp[route.size()][i]);\n if (ans == 0)\n cout << \"NO\" << endl;\n else\n cout << \"YES\" << endl;\n }\n}", "accuracy": 1, "time_ms": 1710, "memory_kb": 19392, "score_of_the_acc": -0.7726, "final_rank": 11 }, { "submission_id": "aoj_2244_4975513", "code_snippet": "#include <iostream>\n#include <algorithm>\n#include <vector>\n#include <string>\n#include <tuple>\n#include <map>\n\nconst std::string way = \"UDLR\";\nconst std::vector<std::pair<int, int>>\n dxys{{-1, 0}, {1, 0}, {0, -1}, {0, 1}};\n\nstd::vector<int> parse() {\n int h, w;\n std::cin >> h >> w;\n\n std::vector<std::string> ss(h);\n for (auto& s : ss) std::cin >> s;\n\n std::map<char, int> ds;\n {\n int n;\n std::cin >> n;\n while (n--) {\n char c;\n int d;\n std::cin >> c >> d;\n ds[c] = d;\n }\n }\n\n std::vector<int> ret;\n int x = 0, y = 0;\n\n int n;\n std::cin >> n;\n while (n--) {\n char c;\n std::cin >> c;\n\n int dx, dy;\n for (int i = 0; i < 4; ++i) {\n if (c == way[i]) std::tie(dx, dy) = dxys[i];\n }\n\n int k;\n std::cin >> k;\n while (k--) {\n x += dx, y += dy;\n ret.push_back(ds[ss[x][y]]);\n }\n }\n\n return ret;\n}\n\nbool solve() {\n int sp, mp;\n std::cin >> sp >> mp;\n if (sp == 0) return false;\n\n auto ds = parse();\n\n int n;\n std::cin >> n;\n std::vector<int> ps(n);\n for (auto& p : ps) std::cin >> p;\n\n std::vector<int> dp(1 << n, 0);\n dp[0] = sp;\n\n for (auto d : ds) {\n for (int i = 0; i < n; ++i) {\n for (int b = 0; b < (1 << n); ++b) {\n if (((b >> i) & 1) || dp[b] <= 0) continue;\n dp[b | (1 << i)] = std::max(dp[b | (1 << i)],\n std::min(mp, dp[b] + ps[i]));\n }\n }\n\n for (int b = 0; b < (1 << n); ++b) dp[b] -= d;\n }\n\n auto ans = std::any_of(dp.begin(), dp.end(),\n [](auto x) { return x > 0; });\n\n std::cout << (ans ? \"YES\" : \"NO\") << \"\\n\";\n return true;\n}\n\nint main() {\n std::cin.tie(nullptr);\n std::ios::sync_with_stdio(false);\n\n while (solve()) {}\n\n return 0;\n}", "accuracy": 1, "time_ms": 1290, "memory_kb": 3400, "score_of_the_acc": -0.2249, "final_rank": 4 }, { "submission_id": "aoj_2244_4886894", "code_snippet": "#include \"iostream\"\n#include \"climits\"\n#include \"list\"\n#include \"queue\"\n#include \"stack\"\n#include \"set\"\n#include \"functional\"\n#include \"algorithm\"\n#include \"string\"\n#include \"map\"\n#include \"unordered_map\"\n#include \"unordered_set\"\n#include \"iomanip\"\n#include \"cmath\"\n#include \"random\"\n#include \"bitset\"\n#include \"cstdio\"\n#include \"numeric\"\n#include \"cassert\"\n#include \"ctime\"\n\nusing namespace std;\n\nconstexpr long long int MOD = 1000000007;\n//constexpr int MOD = 1000000007;\n//constexpr int MOD = 998244353;\n//constexpr long long int MOD = 998244353;\nconstexpr double EPS = 1e-9;\n\n//int N, M, K, T, H, W, L, R;\nlong long int N, M, K, T, H, W, L, R;\n\nint main() {\n\tios::sync_with_stdio(false);\n\tcin.tie(0);\n\n\n\twhile (cin >> N >> M, N) {\n\t\tcin >> H >> W;\n\t\tvector<string>s(H);\n\t\tfor (auto &i : s)cin >> i;\n\t\tvector<int>damage(256);\n\t\tcin >> K;\n\t\twhile (K--) {\n\t\t\tchar c;\n\t\t\tcin >> c >> L;\n\t\t\tdamage[c] = L;\n\t\t}\n\t\tcin >> L;\n\t\tvector<pair<char, int>>mv(L);\n\t\tfor (auto &i : mv)cin >> i.first >> i.second;\n\t\tcin >> K;\n\t\tvector<int>p(K);\n\t\tfor (auto &i : p)cin >> i;\n\t\tvector<int>dp(1 << K, N);\n\t\tfor (int i = 0; i < 1 << K; i++) {\n\t\t\tfor (int j = 0; j < K; j++) {\n\t\t\t\tif ((i >> j) & 1) {\n\t\t\t\t\tdp[i] += p[j];\n\t\t\t\t\tdp[i] = min(dp[i], (int)M);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tint y = 0, x = 0;\n\t\tfor (auto i : mv) {\n\t\t\twhile (i.second--) {\n\t\t\t\tint dy = 0, dx = 0;\n\t\t\t\tif (i.first == 'D')dy = 1;\n\t\t\t\tif (i.first == 'U')dy = -1;\n\t\t\t\tif (i.first == 'R')dx = 1;\n\t\t\t\tif (i.first == 'L')dx = -1;\n\t\t\t\ty += dy, x += dx;\n\t\t\t\tint minus = damage[s[y][x]];\n\t\t\t\tfor (auto &j : dp)j -= minus;\n\t\t\t\tfor (int j = 0; j < 1 << K; j++) {\n\t\t\t\t\tif (dp[j] <= 0)dp[j] = -MOD;\n\t\t\t\t\tfor (int k = 0; k < K; k++) {\n\t\t\t\t\t\tif ((j >> k) & 1)continue;\n\t\t\t\t\t\tdp[j | (1 << k)] = min((int)M, max(dp[j | (1 << k)], dp[j] + p[k]));\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif (dp.back() > 0) {\n\t\t\tcout << \"YES\\n\";\n\t\t}\n\t\telse {\n\t\t\tcout << \"NO\\n\";\n\t\t}\n\t}\n}", "accuracy": 1, "time_ms": 2810, "memory_kb": 3072, "score_of_the_acc": -0.463, "final_rank": 8 }, { "submission_id": "aoj_2244_4830373", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nint main(){\n while (1){\n int HPi, HPm;\n cin >> HPi >> HPm;\n if (HPi == 0 && HPm == 0){\n break;\n }\n int R, C;\n cin >> R >> C;\n vector<vector<char>> a(R, vector<char>(C));\n for (int i = 0; i < R; i++){\n for (int j = 0; j < C; j++){\n cin >> a[i][j];\n }\n }\n int T;\n cin >> T;\n map<char, int> mp;\n for (int i = 0; i < T; i++){\n char c;\n int d;\n cin >> c >> d;\n mp[c] = d;\n }\n int S;\n cin >> S;\n int cy = 0, cx = 0;\n vector<int> y, x;\n int N = 0;\n for (int i = 0; i < S; i++){\n char d;\n int n;\n cin >> d >> n;\n for (int j = 0; j < n; j++){\n if (d == 'U'){\n cy--;\n }\n if (d == 'D'){\n cy++;\n }\n if (d == 'L'){\n cx--;\n }\n if (d == 'R'){\n cx++;\n }\n y.push_back(cy);\n x.push_back(cx);\n N++;\n }\n }\n int P;\n cin >> P;\n vector<int> p(P);\n for (int i = 0; i < P; i++){\n cin >> p[i];\n }\n vector<vector<int>> b(R, vector<int>(C));\n for (int i = 0; i < R; i++){\n for (int j = 0; j < C; j++){\n b[i][j] = mp[a[i][j]];\n }\n }\n vector<vector<int>> dp(N + 1, vector<int>(1 << P, -1));\n dp[0][0] = HPi;\n for (int i = 0; i < N; i++){\n for (int j = 0; j < (1 << P); j++){\n for (int k = 0; k < P; k++){\n if (j >> k & 1){\n if (dp[i][j - (1 << k)] != -1){\n dp[i][j] = max(dp[i][j], min(dp[i][j - (1 << k)] + p[k], HPm));\n }\n }\n }\n }\n for (int j = 0; j < (1 << P); j++){\n if (dp[i][j] > b[y[i]][x[i]]){\n dp[i + 1][j] = dp[i][j] - b[y[i]][x[i]];\n }\n }\n }\n if (dp[N][(1 << P) - 1] == -1){\n cout << \"NO\" << endl;\n } else {\n cout << \"YES\" << endl;\n }\n }\n}", "accuracy": 1, "time_ms": 3180, "memory_kb": 18972, "score_of_the_acc": -0.9998, "final_rank": 17 }, { "submission_id": "aoj_2244_4789591", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntemplate<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }\ntemplate<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }\n#define all(x) (x).begin(),(x).end()\n#define fi first\n#define se second\n#define mp make_pair\n#define si(x) int(x.size())\nconst int mod=1000000007,MAX=35,INF=1<<30;\nint dp[1005][1<<12];\nvector<int> dh={0,1,0,-1},dw={1,0,-1,0};\nint damage[105][105],heal[15];\nvector<int> mov;\nint maxi,K;\n\nvoid solve(){\n int h=0,w=0;\n for(int t=0;t<si(mov);t++){\n h+=dh[mov[t]];\n w+=dw[mov[t]];\n for(int bit=0;bit<(1<<K);bit++){\n if(dp[t][bit]==0) continue;\n \n for(int i=0;i<K;i++){\n if(bit&(1<<i)) continue;\n \n chmax(dp[t][bit|(1<<i)],min(maxi,dp[t][bit]+heal[i]));\n }\n \n chmax(dp[t+1][bit],dp[t][bit]-damage[h][w]);\n }\n }\n}\n\nint main(){\n \n std::ifstream in(\"text.txt\");\n std::cin.rdbuf(in.rdbuf());\n cin.tie(0);\n ios::sync_with_stdio(false);\n \n string U=\"RDLU\";\n while(1){\n memset(dp,0,sizeof(dp));\n int x;cin>>x>>maxi;\n dp[0][0]=x;\n if(x==0) break;\n \n int H,W;cin>>H>>W;\n mov.clear();\n vector<string> S(H);\n for(int i=0;i<H;i++) cin>>S[i];\n int T;cin>>T;\n map<char,int> MA;\n for(int i=0;i<T;i++){\n char c;int a;cin>>c>>a;\n MA[c]=a;\n }\n for(int i=0;i<H;i++){\n for(int j=0;j<W;j++){\n damage[i][j]=MA[S[i][j]];\n }\n }\n cin>>T;\n \n for(int i=0;i<T;i++){\n char c;int a;cin>>c>>a;\n while(a--){\n for(int j=0;j<4;j++){\n if(U[j]==c) mov.push_back(j);\n }\n }\n }\n \n cin>>K;\n for(int i=0;i<K;i++){\n cin>>heal[i];\n }\n \n solve();\n \n bool ok=false;\n \n for(int j=0;j<(1<<K);j++) if(dp[si(mov)][j]) ok=true;\n \n if(ok) cout<<\"YES\\n\";\n else cout<<\"NO\\n\";\n \n }\n}", "accuracy": 1, "time_ms": 1550, "memory_kb": 19376, "score_of_the_acc": -0.746, "final_rank": 10 }, { "submission_id": "aoj_2244_4613762", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing vi = vector<int>;\nusing vvi = vector<vi>;\nusing vll = vector<ll>;\nusing vvll = vector<vll>;\nusing P = pair<int, int>;\nconst double eps = 1e-8;\nconst ll MOD = 1000000007;\nconst int INF = INT_MAX / 2;\nconst ll LINF = LLONG_MAX / 2;\ntemplate <typename T1, typename T2>\nbool chmax(T1 &a, const T2 &b) {\n if(a < b) {a = b; return true;}\n return false;\n}\ntemplate <typename T1, typename T2>\nbool chmin(T1 &a, const T2 &b) {\n if(a > b) {a = b; return true;}\n return false;\n}\ntemplate<typename T1, typename T2>\nostream& operator<<(ostream &os, const pair<T1, T2> p) {\n os << p.first << \":\" << p.second;\n return os;\n}\ntemplate<class T>\nostream &operator<<(ostream &os, const vector<T> &v) {\n for(int i=0;i<((int)(v.size()));++i) {\n if(i) os << \" \";\n os << v[i];\n }\n return os;\n}\nbool solve() {\n int h, hma; cin >> h >> hma;\n if(h == 0) return false;\n int r, c; cin >> r >> c;\n vector<string> g(r);\n for(int i=0;i<(r);++i) {\n cin >> g[i];\n }\n int t; cin >> t;\n vi mp(26);\n for(int i=0;i<(t);++i) {\n char c; cin >> c;\n int k; cin >> k;\n mp[c-'A'] = k;\n }\n int s; cin >> s;\n vector<int> dr(s), dc(s), num(s);\n for(int i=0;i<(s);++i) {\n char c; cin >> c >> num[i];\n if(c == 'L') {\n dc[i] = -1;\n } else if(c == 'R') {\n dc[i] = 1;\n } else if(c == 'U') {\n dr[i] = -1;\n } else if(c == 'D') {\n dr[i] = 1;\n }\n }\n int p; cin >> p;\n vi tmpv(p);\n for(int i=0;i<(p);++i) {\n cin >> tmpv[i];\n }\n set<P> val;\n for(int i=(1);i<(1<<p);++i) {\n int now = 0;\n for(int j=0;j<(p);++j) {\n if((i>>j)&1) now += tmpv[j];\n }\n val.insert({now, i});\n }\n P pos = {0, 0};\n bool ok = true;\n for(int i=0;i<(s);++i) {\n if(!ok) break;\n for(int j=0;j<(num[i]);++j) {\n int nr = pos.first + dr[i], nc = pos.second + dc[i];\n int nowval = mp[g[nr][nc]-'A'];\n h -= nowval;\n if(h <= 0) {\n auto it = val.lower_bound(make_pair(-h+1, 0));\n if(it == val.end()) {\n ok = false;\n break;\n }\n if(min(h + nowval + it->first, hma) - nowval <= 0) {\n ok = false;\n break;\n }\n h = min(h + nowval + it->first, hma) - nowval;\n int bit = it->second;\n vector<P> er;\n for(auto &e: val) {\n if(e.second & bit) {\n er.emplace_back(e);\n }\n }\n for(auto &e: er) {\n val.erase(e);\n }\n }\n pos = {nr, nc};\n }\n }\n cout << (ok ? \"YES\" : \"NO\") << endl;\n return true;\n}\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(10);\n while(solve()) {}\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3500, "score_of_the_acc": -0.0207, "final_rank": 1 }, { "submission_id": "aoj_2244_4368620", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\ntemplate<class T>\nbool chmax(T& a, const T& b) {\n if (a < b) { a = b; return true; }\n return false;\n}\ntemplate<class T>\nbool chmin(T& a, const T& b) {\n if (b < a) { a = b; return true; }\n return false;\n}\n\n// std::vector Declaration\ntemplate<typename T>\nvector<T> make_v(size_t a) { return vector<T>(a); }\ntemplate<typename T, typename... Ts>\nauto make_v(size_t a, Ts... ts) {\n return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));\n}\n\n// std::vector Declaration and Initialization\ntemplate<typename T>\nvector<T> make_vector(size_t a, T x) { return vector<T>(a, x); }\ntemplate<typename T, typename U, typename... Ts>\nauto make_vector(size_t a, U b, Ts... ts) {\n return vector<decltype(make_vector<T>(b,ts...))>(a, make_vector<T>(b, ts...));\n}\n\n// std::vector Input\ntemplate<typename T>\nistream& operator>>(istream& is, vector<T>& v) {\n for (auto &e : v) is >> e;\n return is;\n}\n\n// std::vector Debug\ntemplate<typename T>\nostream& operator<<(ostream& os, const vector<T>& v) {\n os << \"[\";\n bool a = 1;\n for (auto e : v) {\n os << (a ? \"\" : \" \");\n os << e;\n a = 0;\n }\n os << \"]\";\n return os;\n}\n\n// std::array Debug\ntemplate<typename T, size_t n>\nostream& operator<<(ostream& os, const array<T, n>& v) {\n os << \"[\";\n bool a = 1;\n for (auto e : v) {\n os << (a ? \"\" : \" \");\n os << e;\n a = 0;\n }\n os << \"]\";\n return os;\n}\n\n// std::deque Debug\ntemplate<typename T>\nostream& operator<<(ostream& os, const deque<T>& d) {\n os << \"[\";\n bool a = 1;\n for (auto e : d) {\n os << (a ? \"\" : \" \");\n os << e;\n a = 0;\n }\n os << \"]\";\n return os;\n}\n\n// std::pair Debug\ntemplate<typename T, typename U>\nostream& operator<<(ostream& os, const pair<T, U>& p) {\n os << \"(\" << p.first << \" \" << p.second << \")\";\n return os;\n}\n\n// std::set Debug\ntemplate<typename T>\nostream& operator<<(ostream& os, const set<T>& st) {\n os << \"{\";\n bool a = 1;\n for (auto e : st) {\n os << (a ? \"\" : \" \");\n os << e;\n a = 0;\n }\n os << \"}\";\n return os;\n}\n\n// std::multiset Debug\ntemplate<typename T>\nostream& operator<<(ostream& os, const multiset<T>& st) {\n os << \"{\";\n bool a = 1;\n for (auto e : st) {\n os << (a ? \"\" : \" \");\n os << e;\n a = 0;\n }\n os << \"}\";\n return os;\n}\n\n// std::map Debug\ntemplate<typename T, typename U>\nostream& operator<<(ostream& os, const map<T, U>& mp) {\n os << \"{\";\n bool a = 1;\n for (auto e : mp) {\n os << (a ? \"\" : \" \");\n os << e.first << \":\" << e.second;\n a = 0;\n }\n os << \"}\";\n return os;\n}\n\n// std::tuple Debug\ntemplate<int N, class Tuple>\nvoid out(ostream& os, const Tuple& t){}\ntemplate<int N, class Tuple, class H, class ...Ts>\nvoid out(ostream& os, const Tuple& t) {\n if (N) os << \" \";\n os << get<N>(t);\n out<N+1,Tuple,Ts...>(os, t);\n}\ntemplate<class ...Ts>\nostream& operator<<(ostream& os, const tuple<Ts...>& t) {\n os << \"(\";\n out<0,tuple<Ts...>,Ts...>(os, t);\n os << \")\";\n return os;\n}\n\n// Debug\n#define DUMP(x) cerr<<#x<<\" = \"<<(x)<<endl\n\n// Weighted edge\ntemplate<typename T>\nstruct edge {\n int src, to;\n T cost;\n\n edge() {}\n edge(int to, T cost) : src(-1), to(to), cost(cost) {}\n edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}\n\n friend ostream& operator<<(ostream& os, const edge& e) {\n return os << \"(\" << e.src << \"->\" << e.to << \":\" << e.cost << \")\";\n }\n};\n\nusing LL = int64_t;\n\n#define fs first\n#define sc second\n\nconst int64_t MOD = 1e9+7;\n\n\nint main()\n{\n cin.tie(0);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(10);\n\n while (true) {\n int HPinit, HPmax; cin >> HPinit >> HPmax;\n if (HPinit == 0) break;\n\n int R, C; cin >> R >> C;\n vector<string> a(R); cin >> a;\n int T; cin >> T;\n map<char,int> d;\n for (int i = 0; i < T; ++i) {\n char c; cin >> c;\n cin >> d[c];\n }\n int S; cin >> S;\n string s;\n for (int i = 0; i < S; ++i) {\n char c; cin >> c;\n int n; cin >> n;\n for (int j = 0; j < n; ++j) {\n s += c;\n }\n }\n int P; cin >> P;\n vector<int> p(P); cin >> p;\n\n map<char,int> dx, dy;\n dx['R'] = 1, dx['L'] = -1;\n dy['D'] = 1, dy['U'] = -1;\n\n vector<int> x(s.size()+1), y(s.size()+1);\n for (int i = 0; i < s.size(); ++i) {\n x[i+1] = x[i] + dx[s[i]];\n y[i+1] = y[i] + dy[s[i]];\n }\n\n auto dp = make_v<int>(s.size()+1, 1<<P);\n dp[0][(1<<P)-1] = HPinit;\n for (int i = 0; i < s.size(); ++i) {\n char trap = a[y[i+1]][x[i+1]];\n for (int bit = 0; bit < 1<<P; ++bit) {\n if (dp[i][bit] == 0) continue;\n chmax(dp[i+1][bit], dp[i][bit] - d[trap]);\n for (int j = 0; j < P; ++j) if (bit >> j & 1) {\n chmax(dp[i+1][bit & ~(1<<j)],\n min(dp[i][bit] + p[j], HPmax) - d[trap]);\n }\n }\n }\n\n cout << (dp[s.size()][0] ? \"YES\" : \"NO\") << endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 3430, "memory_kb": 18980, "score_of_the_acc": -1.0408, "final_rank": 19 } ]
aoj_2250_cpp
Problem I: Operator The customer telephone support center of the computer sales company called JAG is now in- credibly confused. There are too many customers who request the support, and they call the support center all the time. So, the company wants to figure out how many operators needed to handle this situation. For simplicity, let us focus on the following simple simulation. Let N be a number of customers. The i -th customer has id i , and is described by three numbers, M i , L i and K i . M i is the time required for phone support, L i is the maximum stand by time until an operator answers the call, and K i is the interval time from hanging up to calling back. Let us put these in other words: It takes M i unit times for an operator to support i -th customer. If the i -th customer is not answered by operators for L i unit times, he hangs up the call. K i unit times after hanging up, he calls back. One operator can support only one customer simultaneously. When an operator finish a call, he can immediately answer another call. If there are more than one customer waiting, an operator will choose the customer with the smallest id. At the beginning of the simulation, all customers call the support center at the same time. The simulation succeeds if operators can finish answering all customers within T unit times. Your mission is to calculate the minimum number of operators needed to end this simulation successfully. Input The input contains multiple datasets. Each dataset has the following format: N T M 1 L 1 K 1 . . . M N L N K N The first line of a dataset contains two positive integers, N and T (1 ≤ N ≤ 1000, 1 ≤ T ≤ 1000). N indicates the number of customers in the dataset, and T indicates the time limit of the simulation. The following N lines describe the information of customers. The i -th line contains three integers, M i , L i and K i (1 ≤ M i ≤ T , 1 ≤ L i ≤ 1000, 1 ≤ K i ≤ 1000), describing i -th customer's information. M i indicates the time required for phone support, Li indicates the maximum stand by time until an operator answers the call, and Ki indicates the is the interval time from hanging up to calling back. The end of input is indicated by a line containing two zeros. This line is not part of any dataset and hence should not be processed. Output For each dataset, print the minimum number of operators needed to end the simulation successfully in a line. Sample Input 3 300 100 50 150 100 50 150 100 50 150 3 300 100 50 150 100 50 150 200 50 150 9 18 3 1 1 3 1 1 3 1 1 4 100 1 5 100 1 5 100 1 10 5 3 10 5 3 1 7 1000 10 18 1 2 3 2 3 4 3 4 5 4 5 6 5 6 7 6 7 8 7 8 9 8 9 10 9 10 11 10 11 12 0 0 Output for the Sample Input 2 3 3 4
[ { "submission_id": "aoj_2250_10867556", "code_snippet": "#include <cstdio>\n#include <cstring>\n#include <algorithm>\n#include <cmath>\n#include <iostream>\n#include <queue>\n#define FI first\n#define SE second\nusing namespace std;\nconst double EPS = 1e-8;\nconst int MAXN = 1005;\nconst int INF = 1111111111;\nconst double PI = acos(-1.0);\nstruct Node{\n bool sta;\n int m,l,k;\n}node[MAXN];\nint t,n;\n\ninline bool check(int m)\n{\n\tint cnt = 0;\n\n\tbool flag;\n priority_queue<int,vector<int>,greater<int> >q;\n for(int i = 0; i < m; i++) q.push(0);\n for(int i = 0; i< n; i++) node[i].sta = 0;\n while(1)\n\t{\n\t\tint now,nt = INF,ni;\n\t\tnow = q.top();\n\t\tflag = 0;\n\t\tfor(int i = 0; i < n; i++)\n\t\t{\n\t\t\tif(node[i].sta==0)\n\t\t\t{\n\t\t\t\tif(now%(node[i].l+node[i].k)<=node[i].l&&now+node[i].m<=t)\n\t\t\t\t{\n\t\t\t\t\tnode[i].sta = 1;\n\t\t\t\t\tq.pop();\n\t\t\t\t\tq.push(now+node[i].m);\n\t\t\t\t\tflag = 1,cnt++;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\telse if(nt>(now/(node[i].l+node[i].k)+1)*(node[i].l+node[i].k))\n\t\t\t\t{\n\t\t\t\t\tnt = (now/(node[i].l+node[i].k)+1)*(node[i].l+node[i].k);\n\t\t\t\t\tni = i;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif(!flag&&node[ni].m+nt<=t)\n\t\t{\n\t\t\tnode[ni].sta = 1;\n\t\t\tq.pop();\n\t\t\tq.push(node[ni].m+nt);\n\t\t\tflag = 1,cnt++;\n\t\t}\n\t\tif(!flag) return 0;\n\t\tif(cnt==n) return 1;\n\t}\n\treturn 1;\n}\n\nint main()\n{\n //freopen(\"/home/qitaishui/code/in.txt\",\"r\",stdin);\n int ans;\n while(scanf(\"%d%d\",&n,&t)&&(n+t))\n {\n for(int i = 0; i < n; i++)\n {\n scanf(\"%d%d%d\",&node[i].m,&node[i].l,&node[i].k);\n }\n for(int i = 1; i <= n; i++)\n if(check(i))\n {\n ans = i;\n break;\n }\n printf(\"%d\\n\",ans);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1630, "memory_kb": 3464, "score_of_the_acc": -0.3081, "final_rank": 9 }, { "submission_id": "aoj_2250_10686906", "code_snippet": "#include <iostream>\n#include <stdio.h>\n#include <math.h>\n#include <string.h>\n#include <algorithm>\n#include <stdlib.h>\n#include <string>\n#include <queue>\n#include <vector>\n#include <set>\n#define maxn 1100\n#define oo 1000000000\n#define clearAll(a) memset(a,0,sizeof(a))\n#define sq(a) ((a)*(a))\n\nusing namespace std;\n\ntypedef long long ll;\n\nint n,t;\n\nint M[maxn],L[maxn],K[maxn];\nint S[maxn];\nint T[maxn];\nbool vis[maxn];\n\nclass cmp\n{\n\tpublic :\n\tbool operator() (int x,int y)\n\t{\n\t\treturn x>y;\n\t}\n};\n\nbool check(int x)\n{\n\tpriority_queue<int,vector<int>,cmp > ser;\n\n\tfor (int i=1;i<=n;i++)\n\t{\n\t\tS[i]=0;\n\t\tT[i]=L[i];\n\t}\n\n\tfor (int i=1;i<=x;i++)\n\t\tser.push(0);\n\n\tint now = 0;\n\tint cnt=0;\n\tint i;\n\tclearAll(vis);\n\n\twhile (true)\n\t{\n\t\tnow = ser.top(); ser.pop();\n\t\tif (now>t) break;\n\n\t\tfor (i=1;i<=n;i++)\n\t\t\tif (vis[i]==false)\n\t\t\twhile (T[i]<now)\n\t\t\t{\n\t\t\t\tS[i]=T[i]+K[i];\n\t\t\t\tT[i]=S[i]+L[i];\n\t\t\t\tif (S[i]>t) return false;\n\t\t\t}\n\n int mi = oo;\n for (i=1;i<=n;i++)\n if (!vis[i])\n mi = min(S[i],mi);\n\n if (now<mi)\n now = mi;\n\n\t\tfor (i=1;i<=n;i++)\n\t\t\tif (!vis[i]&&S[i]<=now&&now<=T[i])\n\t\t\t\tbreak;\n\n if (i>n) break;\n\t\tif (max(now,S[i])+M[i]>t) return false;\n\t\tvis[i] = true;\n\t\tcnt++;\n\t\tif (cnt==n) return true;\n\t\tser.push(max(now,S[i])+M[i]);\n\t}\n\n\treturn (cnt==n);\n}\n\nint main()\n{\n //freopen(\"C:\\\\Users\\\\py\\\\Desktop\\\\input.txt\",\"r\",stdin);\n //freopen(\"C:\\\\Users\\\\py\\\\Desktop\\\\output.txt\",\"w\",stdout);\n\n while (scanf(\"%d%d\",&n,&t)&&(n||t))\n {\n \tfor (int i=1;i<=n;i++)\n \t\tscanf(\"%d%d%d\",&M[i],&L[i],&K[i]);\n\n int l;\n for (int i=1;i<=n;i++)\n if (check(i))\n {\n l = i;\n break;\n }\n\n\t\tprintf(\"%d\\n\",l);\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 6100, "memory_kb": 3520, "score_of_the_acc": -1.055, "final_rank": 16 }, { "submission_id": "aoj_2250_9837054", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n/**\n * @brief template\n */\n\nvoid Solve(int N, int T) {\n vector<int> M(N), L(N), K(N);\n rep(i,0,N) cin >> M[i] >> L[i] >> K[i];\n auto Judge = [&](int X) -> bool {\n int Res = X;\n vector<int> End(T+1,0);\n set<int> st;\n rep(i,0,N) st.insert(i);\n rep(i,0,T+1) {\n if (st.empty()) break;\n Res += End[i];\n for (auto it = st.begin(); it != st.end();) {\n if (Res == 0) break;\n int j = *it;\n if (i % (L[j]+K[j]) <= L[j]) {\n int Next = i + M[j];\n if (Next > T) return false;\n End[Next]++;\n Res--;\n it = st.erase(it);\n }\n else it++;\n }\n }\n if (st.empty()) return true;\n else return false;\n };\n rep(i,1,N+1) {\n if (Judge(i)) {\n cout << i << endl;\n return;\n }\n }\n}\n\nint main() {\nwhile(1) {\n int N, T;\n cin >> N >> T;\n if (N == 0) return 0;\n Solve(N,T);\n}\n}", "accuracy": 1, "time_ms": 540, "memory_kb": 3188, "score_of_the_acc": -0.0944, "final_rank": 5 }, { "submission_id": "aoj_2250_9465179", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing vll = vector<ll>;\nusing vvll = vector<vll>;\n#define rep(i,n) for(ll i=0;i<ll(n);i++)\n\nconst ll mod = 1e9 + 7;\n\nvoid solve(ll N,ll T){\n vll M(N),L(N),K(N);\n rep(i,N)cin>>M[i]>>L[i]>>K[i];\n\n for(ll i=1;i<=N;i++){\n ll cnt=0;\n vll H(T,0);\n H[0]=i;\n vector<bool> US(N,0);\n rep(t,T){\n rep(h,H[t]){\n ll m=1e18,id=-1;\n rep(n,N){\n if(US[n])continue;\n if(t%(L[n]+K[n])<=L[n]&&m>t){\n m=t;\n id=n;\n break;\n }\n else{\n ll nt=((t+L[n]+K[n]-1)/(L[n]+K[n]))*(L[n]+K[n]);\n if(m>nt){\n m=nt;\n id=n;\n }\n }\n }\n if(m+M[id]>T)break;\n cnt++;\n US[id]=1;\n if(m+M[id]<T)H[m+M[id]]++;\n }\n }\n if(cnt==N){\n cout<<i<<endl;\n return;\n }\n }\n}\n\nint main() {\n ll N,T;\n while(cin>>N>>T,N+T!=0)solve(N,T);\n\n}", "accuracy": 1, "time_ms": 2060, "memory_kb": 3104, "score_of_the_acc": -0.3359, "final_rank": 10 }, { "submission_id": "aoj_2250_6371978", "code_snippet": "/*\tauthor: Kite_kuma\n\tcreated: 2022.03.03 20:50:51 */\n\n// #ifdef LOCAL\n// #define _GLIBCXX_DEBUG\n// #endif\n#include <bits/stdc++.h>\nusing namespace std;\n\n#pragma region macros\n\n#define foa(s, v) for(auto &s : v)\n#define all(v) (v).begin(), (v).end()\n#define rall(v) (v).rbegin(), (v).rend()\n#define popcnt(n) __builtin_popcountll((long long)n)\n\n#define REPname(a, b, c, d, e, ...) e\n#define rep(...) REPname(__VA_ARGS__, REP3, REP2, REP1, REP0)(__VA_ARGS__)\n#define REP0(x) for(int Counter_in_rep_macro = 0; Counter_in_rep_macro < (x); ++Counter_in_rep_macro)\n#define REP1(i, x) for(int i = 0; i < (x); ++i)\n#define REP2(i, l, r) for(int i = (l); i < (r); ++i)\n#define REP3(i, l, r, c) for(int i = (l); i < (r); i += (c))\n\n#define DREPname(a, b, c, d, e, ...) e\n#define drep(...) DREPname(__VA_ARGS__, DREP3, DREP2, DREP1)(__VA_ARGS__)\n#define DREP1(i, x) for(int i = (x)-1; i >= 0; --i)\n#define DREP2(i, l, r) for(int i = (r)-1; i >= (l); --i)\n#define DREP3(i, l, r, c) for(int i = (r)-1; i >= (l); i -= (c))\n\n#pragma endregion\n\n#pragma region aliases\n\nusing ll = long long;\nusing ld = long double;\nusing ull = unsigned long long;\nusing vi = std::vector<int>;\nusing vvi = std::vector<std::vector<int>>;\nusing vvvi = std::vector<std::vector<std::vector<int>>>;\nusing vll = std::vector<ll>;\nusing vvll = std::vector<vll>;\nusing vvvll = std::vector<vvll>;\nusing pii = std::pair<int, int>;\nusing pll = std::pair<long long, long long>;\ntemplate <class T = ll>\nusing V = std::vector<T>;\ntemplate <class T = ll>\nusing VV = V<V<T>>;\ntemplate <class T = ll>\nusing VVV = V<V<V<T>>>;\ntemplate <class T = ll>\nusing pqup = std::priority_queue<T, std::vector<T>, std::greater<T>>;\ntemplate <class T = ll>\nusing pqdn = std::priority_queue<T>;\n\n#pragma endregion\n\n#pragma region constants\n\nconst int inf = 1e9;\nconst long long INF = 1e18;\nconst long double pi = acos(-1);\nconst char dl = '\\n';\nconst char sp = ' ';\nint dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};\nint dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};\n\nconst int mod_1000000007 = 1000000007;\nconst int mod_998244353 = 998244353;\n\n#pragma endregion\n\n#pragma region basic_operation\n\ntemplate <class T0, class T1, class T2>\ninline bool in_range(T0 x, T1 lef, T2 rig) {\n\treturn ((lef <= x) && (x < rig));\n}\n\ntemplate <class T>\ninline bool chmin(T &a, T b) {\n\tif(a > b) {\n\t\ta = b;\n\t\treturn true;\n\t}\n\treturn false;\n}\ntemplate <class T>\ninline bool chmax(T &a, T b) {\n\tif(a < b) {\n\t\ta = b;\n\t\treturn true;\n\t}\n\treturn false;\n}\n\nvoid Yes(bool f = 1) { std::cout << (f ? \"Yes\" : \"No\") << '\\n'; }\nvoid No() { std::cout << \"No\\n\"; }\nvoid YES(bool f = 1) { std::cout << (f ? \"YES\" : \"NO\") << '\\n'; }\nvoid NO() { std::cout << \"NO\\n\"; }\n\ntemplate <class T>\nvoid drop(T answer) {\n\tstd::cout << answer << '\\n';\n\texit(0);\n}\n\nvoid err(bool flag = true) {\n\tif(!flag) return;\n\tstd::cout << -1 << '\\n';\n\texit(0);\n}\n\ntemplate <class T>\nvoid vout(std::vector<T> const &v, bool vertically = 0) {\n\tif(vertically) {\n\t\tfor(auto const &a : v) {\n\t\t\tstd::cout << a << '\\n';\n\t\t}\n\t\treturn;\n\t}\n\tfor(auto it = v.begin(); it != v.end(); it++) {\n\t\tstd::cout << (*it);\n\t\tif(it != v.end() - 1) {\n\t\t\tstd::cout << ' ';\n\t\t}\n\t}\n\tstd::cout << '\\n';\n\treturn;\n}\n\ninline void print() { std::cout << '\\n'; }\ntemplate <class T>\ninline void print(T x) {\n\tstd::cout << x << '\\n';\n\treturn;\n}\ntemplate <typename Head, typename... Tail>\nvoid print(Head H, Tail... T) {\n\tstd::cout << H << \" \";\n\tprint(T...);\n}\n\ntemplate <class T>\nvoid add(std::vector<T> &v, T val) {\n\tfor(auto &a : v) a += val;\n\treturn;\n}\n\ntemplate <class T>\nT dup(T a, T b) {\n\tassert(b != 0);\n\treturn (a + b - 1) / b;\n}\n\ntemplate <class T>\nT greatest_lower_multiple(T x, T d) {\n\tif(d == 0) return 0;\n\tif(d < 0) d *= -1;\n\tT y = x % d;\n\tif(y < 0) y += d;\n\treturn x - y;\n}\n\ntemplate <class T>\nT least_upper_multiple(T x, T d) {\n\treturn -greatest_lower_multiple(-x, d);\n}\n\nlong long POW(long long a, long long n) {\n\tlong long res = 1;\n\twhile(n > 0) {\n\t\tif(n & 1) res = res * a;\n\t\ta = a * a;\n\t\tn >>= 1;\n\t}\n\treturn res;\n}\n\nlong long modpow(long long a, long long n, long long mod) {\t // a^n mod\n\tassert(n >= 0 && mod);\n\tif(mod == 1) return 0LL;\n\tlong long res = 1;\n\ta %= mod;\n\twhile(n > 0) {\n\t\tif(n & 1) res = res * a % mod;\n\t\ta = a * a % mod;\n\t\tn >>= 1;\n\t}\n\treturn res < 0 ? res + mod : res;\n}\n\n// return x which satisfies a * x % mod == gcd(a, mod)\n// not (mod | a)\nlong long modinv(long long a, long long mod) {\n\tlong long b = mod, u = 1, v = 0;\n\twhile(b) {\n\t\tlong long t = a / b;\n\t\ta -= t * b;\n\t\tstd::swap(a, b);\n\t\tu -= t * v;\n\t\tstd::swap(u, v);\n\t}\n\tu %= mod;\n\tif(u < 0) u += mod;\n\treturn u;\n}\n\ntemplate <class T>\nint lb(const std::vector<T> &a, const T x) {\n\treturn std::distance(a.begin(), std::lower_bound(a.begin(), a.end(), x));\n}\ntemplate <class T>\nint ub(const std::vector<T> &a, const T x) {\n\treturn std::distance(a.begin(), std::upper_bound(a.begin(), a.end(), x));\n}\ntemplate <class T>\nvoid unq_sort(std::vector<T> &a) {\n\tstd::sort(a.begin(), a.end());\n\ta.erase(std::unique(a.begin(), a.end()), a.end());\n}\ntemplate <class T>\nstd::vector<int> press(std::vector<T> &a) {\n\tauto vec = a;\n\tunq_sort(vec);\n\tstd::vector<int> ret;\n\tfor(auto &v : a) ret.push_back(lb(vec, v));\n\treturn ret;\n}\n\n#pragma endregion\n\n#pragma region input\n#define VEC(type, name, size) \\\n\tvector<type> name(size); \\\n\tscanner::INPUT(name)\n#define VVEC(type, name, h, w) \\\n\tvector<vector<type>> name(h, vector<type>(w)); \\\n\tscanner::INPUT(name)\n#define INT(...) \\\n\tint __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define LL(...) \\\n\tlong long __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define STR(...) \\\n\tstring __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define CHR(...) \\\n\tchar __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define DBL(...) \\\n\tdouble __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define LD(...) \\\n\tlong double __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define TPL3(type0, type1, type2, name) \\\n\tstd::tuple<type0, type1, type2> name; \\\n\tscanner::INPUT(name);\n#define TPL4(type0, type1, type2, type3, name) \\\n\tstd::tuple<type0, type1, type2, type3> name; \\\n\tscanner::INPUT(name);\n\nnamespace scanner {\ntemplate <class T>\nvoid scan(T &a) {\n\tstd::cin >> a;\n}\n\ntemplate <class T, class L>\nvoid scan(std::pair<T, L> &p) {\n\tscan(p.first);\n\tscan(p.second);\n}\n\ntemplate <class T0, class T1, class T2>\nvoid scan(std::tuple<T0, T1, T2> &p) {\n\tT0 t0;\n\tT1 t1;\n\tT2 t2;\n\tscan(t0);\n\tscan(t1);\n\tscan(t2);\n\tp = std::make_tuple(t0, t1, t2);\n}\n\ntemplate <class T0, class T1, class T2, class T3>\nvoid scan(std::tuple<T0, T1, T2, T3> &p) {\n\tT0 t0;\n\tT1 t1;\n\tT2 t2;\n\tT3 t3;\n\tscan(t0);\n\tscan(t1);\n\tscan(t2);\n\tscan(t3);\n\tp = std::make_tuple(t0, t1, t2, t3);\n}\n\ntemplate <class T>\nvoid scan(std::vector<T> &a) {\n\tfor(auto &i : a) scan(i);\n}\n\nvoid INPUT() {}\ntemplate <class Head, class... Tail>\nvoid INPUT(Head &head, Tail &... tail) {\n\tscan(head);\n\tINPUT(tail...);\n}\n} // namespace scanner\n\ntemplate <typename T1, typename T2>\nstd::istream &operator>>(std::istream &is, std::pair<T1, T2> &p) {\n\tis >> p.first >> p.second;\n\treturn is;\n}\n#pragma endregion\n\n#pragma region debug\n\n#pragma region output\ntemplate <typename T1, typename T2>\nstd::ostream &std::operator<<(std::ostream &os, const std::pair<T1, T2> &p) {\n\tos << p.first << \" \" << p.second;\n\treturn os;\n}\ntemplate <class T>\nstd::ostream &std::operator<<(std::ostream &os, const std::vector<T> &v) {\n\tfor(int i = 0; i < (int)v.size(); i++) {\n\t\tif(i) os << \" \";\n\t\tos << v[i];\n\t}\n\treturn os;\n}\n#pragma endregion\n\n#pragma region view\n\nnamespace viewer {\nvoid view(const long long e) {\n\tif(e == INF)\n\t\tstd::cerr << \"INF\";\n\telse if(e == -INF)\n\t\tstd::cerr << \"-INF\";\n\telse\n\t\tstd::cerr << e;\n}\n\nvoid view(const int e) {\n\tif(e == inf)\n\t\tstd::cerr << \"inf\";\n\telse if(e == -inf)\n\t\tstd::cerr << \"-inf\";\n\telse\n\t\tstd::cerr << e;\n}\n\ntemplate <typename T>\nvoid view(const T e) {\n\tstd::cerr << e;\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::pair<T, U> &p) {\n\tstd::cerr << \"(\";\n\tview(p.first);\n\tstd::cerr << \", \";\n\tview(p.second);\n\tstd::cerr << \")\";\n}\n\ntemplate <class T0, class T1, class T2>\nvoid view(const std::tuple<T0, T1, T2> &p) {\n\tstd::cerr << \"(\";\n\tview(std::get<0>(p));\n\tstd::cerr << \", \";\n\tview(std::get<1>(p));\n\tstd::cerr << \", \";\n\tview(std::get<2>(p));\n\tstd::cerr << \")\";\n}\n\ntemplate <class T0, class T1, class T2, class T3>\nvoid view(const std::tuple<T0, T1, T2, T3> &p) {\n\tstd::cerr << \"(\";\n\tview(std::get<0>(p));\n\tstd::cerr << \", \";\n\tview(std::get<1>(p));\n\tstd::cerr << \", \";\n\tview(std::get<2>(p));\n\tstd::cerr << \", \";\n\tview(std::get<3>(p));\n\tstd::cerr << \")\";\n}\n\ntemplate <typename T>\nvoid view(const std::set<T> &s) {\n\tif(s.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << \"{ \";\n\tfor(auto &t : s) {\n\t\tview(t);\n\t\tstd::cerr << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\n\ntemplate <typename T>\nvoid view(const std::unordered_set<T> &s) {\n\tif(s.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << \"{ \";\n\tfor(auto &t : s) {\n\t\tview(t);\n\t\tstd::cerr << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\n\ntemplate <typename T>\nvoid view(const std::vector<T> &v) {\n\tif(v.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << \"{ \";\n\tfor(const auto &e : v) {\n\t\tview(e);\n\t\tstd::cerr << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\n\ntemplate <typename T>\nvoid view(const std::vector<std::vector<T>> &vv) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &v : vv) {\n\t\tstd::cerr << \"\\t\";\n\t\tview(v);\n\t\tstd::cerr << '\\n';\n\t}\n\tstd::cerr << \"}\";\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::vector<std::pair<T, U>> &v) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &c : v) {\n\t\tstd::cerr << \"\\t(\";\n\t\tview(c.first);\n\t\tstd::cerr << \", \";\n\t\tview(c.second);\n\t\tstd::cerr << \")\\n\";\n\t}\n\tstd::cerr << \"}\";\n}\n\ntemplate <class T0, class T1, class T2>\nvoid view(const std::vector<std::tuple<T0, T1, T2>> &v) {\n\tif(v.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << '{';\n\tfor(const auto &t : v) {\n\t\tstd::cerr << \"\\n\\t\";\n\t\tview(t);\n\t\tstd::cerr << \",\";\n\t}\n\tstd::cerr << \"\\n}\";\n}\n\ntemplate <class T0, class T1, class T2, class T3>\nvoid view(const std::vector<std::tuple<T0, T1, T2, T3>> &v) {\n\tif(v.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << '{';\n\tfor(const auto &t : v) {\n\t\tstd::cerr << \"\\n\\t\";\n\t\tview(t);\n\t\tstd::cerr << \",\";\n\t}\n\tstd::cerr << \"\\n}\";\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::map<T, U> &m) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &t : m) {\n\t\tstd::cerr << \"\\t[\";\n\t\tview(t.first);\n\t\tstd::cerr << \"] : \";\n\t\tview(t.second);\n\t\tstd::cerr << '\\n';\n\t}\n\tstd::cerr << \"}\";\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::unordered_map<T, U> &m) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &t : m) {\n\t\tstd::cerr << \"\\t[\";\n\t\tview(t.first);\n\t\tstd::cerr << \"] : \";\n\t\tview(t.second);\n\t\tstd::cerr << '\\n';\n\t}\n\tstd::cerr << \"}\";\n}\n} // namespace viewer\n\n#pragma endregion\n\n// when debugging : g++ foo.cpp -DLOCAL\n#ifdef LOCAL\nvoid debug_out() {}\ntemplate <typename Head, typename... Tail>\nvoid debug_out(Head H, Tail... T) {\n\tviewer::view(H);\n\tstd::cerr << \", \";\n\tdebug_out(T...);\n}\n#define debug(...) \\\n\tdo { \\\n\t\tstd::cerr << __LINE__ << \" [\" << #__VA_ARGS__ << \"] : [\"; \\\n\t\tdebug_out(__VA_ARGS__); \\\n\t\tstd::cerr << \"\\b\\b]\\n\"; \\\n\t} while(0)\n#define dump(x) \\\n\tdo { \\\n\t\tstd::cerr << __LINE__ << \" \" << #x << \" : \"; \\\n\t\tviewer::view(x); \\\n\t\tstd::cerr << '\\n'; \\\n\t} while(0)\n\n#else\n#define debug(...) (void(0))\n#define dump(x) (void(0))\n#endif\n\n#pragma endregion\n\nstruct call {\n\tint complete, wait, interval;\n\tcall(int complete, int waite, int interval) : complete(complete), wait(waite), interval(interval) {}\n};\n\nint solve(int n) {\n\tint timelimit;\n\tcin >> timelimit;\n\n\tvector<call> calls;\n\n\tdebug(n, timelimit);\n\trep(n) {\n\t\tint a, b, c;\n\t\tcin >> a >> b >> c;\n\t\tcalls.emplace_back(a, b, c);\n\t\tdebug(a, b, c);\n\t}\n\n\tauto nextcall = [](int time, int wait, int interval) -> int {\n\t\tconst int loopsize = wait + interval;\n\t\tint m = greatest_lower_multiple(time, loopsize);\n\t\tassert(m <= time);\n\t\tif(time <= m + wait) return time;\n\t\treturn m + loopsize;\n\t};\n\n\tvector<int> waiting(n, 1);\n\n\tauto enough = [&](int supporter) -> bool {\n\t\tpqup<int> q;\n\t\trep(supporter) q.push(0);\n\t\twaiting.assign(n, 1);\n\t\tdebug(supporter);\n\t\twhile(q.size()) {\n\t\t\tpair<int, int> time_id(inf, -1);\n\t\t\tint now = q.top();\n\t\t\tif(now > timelimit) return false;\n\t\t\tq.pop();\n\t\t\tdebug(now);\n\t\t\trep(costomerid, n) {\n\t\t\t\tif(waiting[costomerid]) {\n\t\t\t\t\tconst call &cl = calls[costomerid];\n\t\t\t\t\tint nxttime = nextcall(now, cl.wait, cl.interval);\n\t\t\t\t\tdebug(time_id, nxttime, costomerid, cl.wait, cl.interval, cl.complete);\n\t\t\t\t\tchmin(time_id, make_pair(nxttime, costomerid));\n\t\t\t\t\tif(time_id.first == now) break;\n\t\t\t\t}\n\t\t\t}\n\t\t\tint nxttime, costomerid;\n\t\t\ttie(nxttime, costomerid) = time_id;\n\t\t\tif(costomerid < 0) continue;\n\t\t\tq.push(nxttime + calls[costomerid].complete);\n\t\t\twaiting[costomerid] = 0;\n\t\t\t// if(n == 9 && supporter == 4) debug(costomerid);\n\t\t\tdebug(costomerid);\n\t\t}\n\t\treturn true;\n\t};\n\n\t// int ng = 0;\n\t// int ok = n;\n\t// while(ok - ng > 1) {\n\t// \tint mid = ok + ng;\n\t// \tmid /= 2;\n\t// \tif(enough(mid)) {\n\t// \t\tok = mid;\n\t// \t} else\n\t// \t\tng = mid;\n\t// }\n\n\trep(person, 1, n) if(enough(person)) return person;\n\treturn n;\n}\n\nint main() {\n\tstd::ios::sync_with_stdio(false);\n\tstd::cin.tie(nullptr);\n\tstd::cout << std::fixed << std::setprecision(15);\n\tsrand((unsigned)time(NULL));\n\n\tint n;\n\twhile(cin >> n && n) {\n\t\tcout << solve(n) << dl;\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 4400, "memory_kb": 3348, "score_of_the_acc": -0.7528, "final_rank": 14 }, { "submission_id": "aoj_2250_6076614", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define eps 1e-9\n#define INF 2000000000 // 2e9\n#define LLINF 2000000000000000000ll // 2e18 (llmax:9e18)\n#define all(x) (x).begin(), (x).end()\n#define sq(x) ((x) * (x))\n\n#define rep(i, x) for (int i = 0; i < (int)(x); i++)\n#define drep(i, x) for (int i = ((int)(x)-1); i >= 0; i--)\n\n#define popcount __builtin_popcount\n#define next __next\n#define prev __prev\n\n#ifndef LOCAL\n#define dmp(...) ;\n#else\n#define dmp(...) \\\n cerr << \"[ \" << #__VA_ARGS__ << \" ] : \" << dump_str(__VA_ARGS__) << endl\n#endif\n\n// ---------------- Utility ------------------\n\ntemplate <class T>\nbool chmin(T &a, const T &b) {\n if (a <= b) return false;\n a = b;\n return true;\n}\n\ntemplate <class T>\nbool chmax(T &a, const T &b) {\n if (a >= b) return false;\n a = b;\n return true;\n}\n\ntemplate <class T>\nusing MaxHeap = priority_queue<T>;\n\ntemplate <class T>\nusing MinHeap = priority_queue<T, vector<T>, greater<T>>;\n\ntemplate <class T>\nvector<T> vect(int len, T elem) {\n return vector<T>(len, elem);\n}\n\n// ----------------- Input -------------------\n\ntemplate <class T, class U>\nistream &operator>>(istream &is, pair<T, U> &p) {\n return is >> p.first >> p.second;\n}\n\ntemplate <class T>\nistream &operator>>(istream &is, vector<T> &vec) {\n for (int i = 0; i < vec.size(); i++) is >> vec[i];\n return is;\n}\n\n// ----------------- Output ------------------\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n return os << p.first << ',' << p.second;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const vector<T> &v) {\n for (const T &e : v) os << e << \" \";\n return os;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const deque<T> &d) {\n for (const T &e : d) os << e << \" \";\n return os;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const set<T> &s) {\n os << \"{ \";\n for (const T &e : s) os << e << \" \";\n return os << \"}\";\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const map<T, U> &m) {\n os << \"{ \";\n for (const auto &[key, val] : m) os << \"( \" << key << \" -> \" << val << \" ) \";\n return os << \"}\";\n}\n\ntemplate <class TupleTy, size_t... I>\nvoid dump_tuple(ostream &os, const TupleTy t, std::index_sequence<I...>) {\n (..., (os << (I == 0 ? \"\" : \",\") << std::get<I>(t)));\n}\n\ntemplate <class... Args>\nostream &operator<<(ostream &os, const tuple<Args...> &t) {\n os << \"(\";\n dump_tuple(os, t, std::make_index_sequence<sizeof...(Args)>());\n return os << \")\";\n}\n\nvoid dump_str_rec(ostringstream &) {}\n\ntemplate <class Head, class... Tail>\nvoid dump_str_rec(ostringstream &oss, Head &&head, Tail &&... tail) {\n oss << \", \" << head;\n dump_str_rec(oss, forward<Tail>(tail)...);\n}\n\ntemplate <class T, class... U>\nstring dump_str(T &&arg, U &&... args) {\n ostringstream oss;\n oss << arg;\n dump_str_rec(oss, forward<U>(args)...);\n return oss.str();\n}\n\n// --------------- Fast I/O ------------------\n\nvoid fastio() {\n cin.tie(0);\n ios::sync_with_stdio(0);\n cout << fixed << setprecision(20);\n}\n\n// ------------------ ACL --------------------\n\n// #include <atcoder/modint>\n// constexpr int MOD = 998244353;\n// constexpr int MOD = 1000000007;\n// using mint = atcoder::static_modint<MOD>;\n// ostream &operator<<(ostream &os, const mint &v) {\n// return os << v.val();\n// }\n\n// ------------ End of template --------------\n\n#define endl \"\\n\"\nusing ll = long long;\nusing pii = pair<int, int>;\n\nconst bool multitest = false;\n\nbool solve() {\n int N, T;\n cin >> N >> T;\n if (N == 0 && T == 0) return false;\n\n vector<int> M(N), L(N), K(N);\n for (int i = 0; i < N; i++) { cin >> M[i] >> L[i] >> K[i]; }\n\n auto check = [&](int X) {\n vector<bool> used(N, false);\n MinHeap<int> q;\n for (int i = 0; i < X; i++) { q.push(0); }\n int cnt = 0;\n for (int t = 0; t <= T; t++) {\n for (int j = 0; j < N; j++) {\n if (used[j]) continue;\n if (t % (L[j] + K[j]) <= L[j]) {\n if (q.top() > t) break;\n q.pop();\n q.push(t + M[j]);\n if (t + M[j] > T) return false;\n used[j] = true;\n cnt++;\n }\n }\n }\n if (cnt < N) return false;\n return true;\n };\n\n for (int i = 1; i < N; i++) {\n if (check(i)) {\n cout << i << endl;\n return true;\n }\n }\n\n cout << N << endl;\n return true;\n}\n\nint main() {\n fastio();\n if (!multitest) {\n while (solve()) {}\n } else {\n cerr << \"[Warning] Multi testcase mode on\" << endl;\n int t;\n cin >> t;\n while (t--) solve();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 750, "memory_kb": 3356, "score_of_the_acc": -0.1494, "final_rank": 6 }, { "submission_id": "aoj_2250_6046607", "code_snippet": "#include<iostream>\n#include<bitset>\nusing namespace std;\nint N,T;\nbitset<1000>B[1000];\nint M[1000];\nint add[1001];\nbool check(int n)\n{\n\tfor(int i=0;i<=T;i++)add[i]=0;\n\tbitset<1000>now;\n\tfor(int i=0;i<N;i++)now[i]=1;\n\tfor(int i=0;i<T;i++)\n\t{\n\t\tn+=add[i];\n\t\tif(n==0)continue;\n\t\tbitset<1000>tmp=now&B[i];\n\t\twhile(n>0)\n\t\t{\n\t\t\tint id=tmp._Find_first();\n\t\t\tif(id>=1000)break;\n\t\t\tif(i+M[id]<=T)add[i+M[id]]++,tmp[id]=now[id]=0,n--;\n\t\t\telse return false;\n\t\t}\n\t}\n\treturn now.none();\n}\nint main()\n{\n\twhile(cin>>N>>T,N)\n\t{\n\t\tfor(int i=0;i<T;i++)B[i].reset();\n\t\tfor(int i=0;i<N;i++)\n\t\t{\n\t\t\tint L,K;\n\t\t\tcin>>M[i]>>L>>K;\n\t\t\tL++;\n\t\t\tK--;\n\t\t\tfor(int j=0;j<T;j++)\n\t\t\t{\n\t\t\t\tif(j%(L+K)<L)B[j][i]=1;\n\t\t\t}\n\t\t}\n\t\tint t=1;\n\t\twhile(!check(t))t++;\n\t\tcout<<t<<endl;\n\t}\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 3484, "score_of_the_acc": -0.0506, "final_rank": 2 }, { "submission_id": "aoj_2250_5927758", "code_snippet": "#pragma GCC ta g(\"avx2\")\n#pragma GCC optimize(\"Ofast\")\n#pragma GCC optimize(\"unroll-loops\")\n#include <bits/stdc++.h>\nusing namespace std;\n#define DEBUG\n#ifdef DEBUG\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << '(' << p.first << ',' << p.second << ')';\n return os;\n}\ntemplate <class T> ostream &operator<<(ostream &os, const vector<T> &v) {\n os << '{';\n for(int i = 0; i < (int)v.size(); i++) {\n if(i) { os << ','; }\n os << v[i];\n }\n os << '}';\n return os;\n}\nvoid debugg() { cerr << endl; }\ntemplate <class T, class... Args>\nvoid debugg(const T &x, const Args &... args) {\n cerr << \" \" << x;\n debugg(args...);\n}\n#define debug(...) \\\n cerr << __LINE__ << \" [\" << #__VA_ARGS__ << \"]: \", debugg(__VA_ARGS__)\n#define dump(x) cerr << __LINE__ << \" \" << #x << \" = \" << (x) << endl\n#else\n#define debug(...) (void(0))\n#define dump(x) (void(0))\n#endif\nusing namespace std;\ntypedef long long ll;\ntypedef vector<ll> vl;\ntypedef vector<vl> vvl;\ntypedef vector<char> vc;\ntypedef vector<string> vs;\ntypedef vector<bool> vb;\ntypedef vector<double> vd;\ntypedef pair<ll,ll> P;\ntypedef pair<int,int> pii;\ntypedef vector<P> vpl;\ntypedef tuple<ll,ll,ll> tapu;\n#define rep(i,n) for(int i=0; i<(n); i++)\n#define REP(i,a,b) for(int i=(a); i<(b); i++)\n#define all(x) (x).begin(), (x).end()\n#define rall(x) (x).rbegin(), (x).rend()\nconst int inf = (1<<30)-1;\nconst ll linf = 1LL<<61;\nconst int MAX = 510000;\nint dy[8] = {0,1,0,-1,1,-1,-1,1};\nint dx[8] = {-1,0,1,0,1,-1,1,-1};\nconst double pi = acos(-1);\nconst double eps = 1e-7;\ntemplate<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){\n\tif(a>b){\n\t\ta = b; return true;\n\t}\n\telse return false;\n}\ntemplate<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){\n\tif(a<b){\n\t\ta = b; return true;\n\t}\n\telse return false;\n}\ntemplate<typename T> inline void print(T &a){\n int sz = a.size();\n for(auto itr = a.begin(); itr != a.end(); itr++){\n\t\tcout << *itr;\n sz--;\n if(sz) cout << \" \";\n\t}\n cout << \"\\n\";\n}\ntemplate<typename T1,typename T2> inline void print2(T1 a, T2 b){\n\tcout << a << \" \" << b << \"\\n\";\n}\ntemplate<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){\n\tcout << a << \" \" << b << \" \" << c << \"\\n\";\n}\nvoid mark() {cout << \"#\" << \"\\n\";}\nll pcount(ll x) {return __builtin_popcountll(x);}\n//#include <atcoder/maxflow>\n//using namespace atcoder;\n//const int mod = 1e9 + 7;\nconst int mod = 998244353;\n\nint main(){\n bitset<1010> can;\n while(1){\n int n,t; cin >> n >> t;\n if(!n) break;\n vl m(n),l(n),k(n);\n rep(i,n) cin >> m[i] >> l[i] >> k[i];\n vector<vvl> ev(2,vvl(t+1));\n rep(i,n){\n for(int j=0; j<=t; j+=l[i]+k[i]){\n ev[0][j].push_back(i);\n if(j+l[i]<=t) ev[1][j+l[i]].push_back(i);\n }\n }\n for(int i=(accumulate(all(m),0)+t-1)/t; i<=n; i++){\n vb done(n,0);\n vector<int> ope(t+1,0);\n ope[0] = i;\n bool f = true;\n rep(j,t+1){\n for(auto x : ev[0][j]) if(!done[x]) can[x] = 1;\n if(j>0) ope[j] += ope[j-1];\n while(ope[j] > 0){\n int id = can._Find_first();\n if(id == 1010) break;\n if(j+m[id] > t){\n f = false;\n break;\n }\n done[id] = 1;\n can[id] = 0;\n ope[j+m[id]]++;\n ope[j]--;\n }\n for(auto x : ev[1][j]) can[x] = 0; \n }\n rep(i,n) can[i] = 0;\n rep(j,n){\n if(!done[j]) f = false;\n }\n if(f){\n cout << i << endl;\n break;\n }\n }\n }\n}", "accuracy": 1, "time_ms": 530, "memory_kb": 11360, "score_of_the_acc": -1.0778, "final_rank": 17 }, { "submission_id": "aoj_2250_5927718", "code_snippet": "#pragma GCC ta g(\"avx2\")\n#pragma GCC optimize(\"Ofast\")\n#pragma GCC optimize(\"unroll-loops\")\n#include <bits/stdc++.h>\nusing namespace std;\n#define DEBUG\n#ifdef DEBUG\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << '(' << p.first << ',' << p.second << ')';\n return os;\n}\ntemplate <class T> ostream &operator<<(ostream &os, const vector<T> &v) {\n os << '{';\n for(int i = 0; i < (int)v.size(); i++) {\n if(i) { os << ','; }\n os << v[i];\n }\n os << '}';\n return os;\n}\nvoid debugg() { cerr << endl; }\ntemplate <class T, class... Args>\nvoid debugg(const T &x, const Args &... args) {\n cerr << \" \" << x;\n debugg(args...);\n}\n#define debug(...) \\\n cerr << __LINE__ << \" [\" << #__VA_ARGS__ << \"]: \", debugg(__VA_ARGS__)\n#define dump(x) cerr << __LINE__ << \" \" << #x << \" = \" << (x) << endl\n#else\n#define debug(...) (void(0))\n#define dump(x) (void(0))\n#endif\nusing namespace std;\ntypedef long long ll;\ntypedef vector<ll> vl;\ntypedef vector<vl> vvl;\ntypedef vector<char> vc;\ntypedef vector<string> vs;\ntypedef vector<bool> vb;\ntypedef vector<double> vd;\ntypedef pair<ll,ll> P;\ntypedef pair<int,int> pii;\ntypedef vector<P> vpl;\ntypedef tuple<ll,ll,ll> tapu;\n#define rep(i,n) for(int i=0; i<(n); i++)\n#define REP(i,a,b) for(int i=(a); i<(b); i++)\n#define all(x) (x).begin(), (x).end()\n#define rall(x) (x).rbegin(), (x).rend()\nconst int inf = (1<<30)-1;\nconst ll linf = 1LL<<61;\nconst int MAX = 510000;\nint dy[8] = {0,1,0,-1,1,-1,-1,1};\nint dx[8] = {-1,0,1,0,1,-1,1,-1};\nconst double pi = acos(-1);\nconst double eps = 1e-7;\ntemplate<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){\n\tif(a>b){\n\t\ta = b; return true;\n\t}\n\telse return false;\n}\ntemplate<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){\n\tif(a<b){\n\t\ta = b; return true;\n\t}\n\telse return false;\n}\ntemplate<typename T> inline void print(T &a){\n int sz = a.size();\n for(auto itr = a.begin(); itr != a.end(); itr++){\n\t\tcout << *itr;\n sz--;\n if(sz) cout << \" \";\n\t}\n cout << \"\\n\";\n}\ntemplate<typename T1,typename T2> inline void print2(T1 a, T2 b){\n\tcout << a << \" \" << b << \"\\n\";\n}\ntemplate<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){\n\tcout << a << \" \" << b << \" \" << c << \"\\n\";\n}\nvoid mark() {cout << \"#\" << \"\\n\";}\nll pcount(ll x) {return __builtin_popcountll(x);}\n//#include <atcoder/maxflow>\n//using namespace atcoder;\n//const int mod = 1e9 + 7;\nconst int mod = 998244353;\n\nint main(){\n while(1){\n int n,t; cin >> n >> t;\n if(!n) break;\n vl m(n),l(n),k(n);\n rep(i,n) cin >> m[i] >> l[i] >> k[i];\n vector<vvl> ev(2,vvl(t+1));\n rep(i,n){\n for(int j=0; j<=t; j+=l[i]+k[i]){\n ev[0][j].push_back(i);\n if(j+l[i]<=t) ev[1][j+l[i]].push_back(i);\n }\n }\n for(int i=(accumulate(all(m),0)+t-1)/t; i<=n; i++){\n vb can(n,0);\n vb done(n,0);\n vector<int> ope(i,0);\n rep(j,t+1){\n for(auto x : ev[0][j]) can[x] = 1;\n vector<int> ls;\n for(int k=n-1; k>=0; k--){\n if(!done[k] && can[k]) ls.push_back(k);\n }\n rep(j,i){\n if(ope[j] > 0) ope[j]--;\n if(ope[j] == 0){\n if(!ls.empty()){\n ope[j] = m[ls.back()];\n done[ls.back()] = 1;\n ls.pop_back();\n }\n }\n }\n for(auto x : ev[1][j]) can[x] = 0; \n }\n bool f = true;\n rep(j,n){\n if(!done[j]) f = false;\n }\n rep(j,i){\n if(ope[j] > 0) f = false;\n }\n if(f){\n cout << i << endl;\n break;\n }\n }\n }\n}", "accuracy": 1, "time_ms": 3760, "memory_kb": 11128, "score_of_the_acc": -1.5846, "final_rank": 20 }, { "submission_id": "aoj_2250_5770770", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntemplate<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }\ntemplate<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }\n#define all(x) (x).begin(),(x).end()\n#define fi first\n#define se second\n#define mp make_pair\n#define si(x) int(x.size())\nconst int mod=1000000007,MAX=1005,INF=1<<30;\n\nvector<pair<int,int>> wh[MAX];\n\nint main(){\n \n std::ifstream in(\"text.txt\");\n std::cin.rdbuf(in.rdbuf());\n cin.tie(0);\n ios::sync_with_stdio(false);\n \n while(1){\n int N,T;cin>>N>>T;\n if(N==0) break;\n for(int t=0;t<MAX;t++) wh[t].clear();\n \n for(int i=0;i<N;i++){\n int a,b,c;cin>>a>>b>>c;\n int d=(b+c);\n for(int j=0;j+a<=T;j++){\n if(j%d<=b){\n wh[j].push_back(mp(i,j+a));\n }\n }\n }\n \n for(int mid=1;mid<=N;mid++){\n vector<int> done(N),revi(T+1);\n int alive=mid;\n for(int t=0;t<=T;t++){\n alive+=revi[t];\n for(auto x:wh[t]){\n if(alive==0) break;\n if(done[x.fi]) continue;\n done[x.fi]=true;\n alive--;\n revi[x.se]++;\n }\n }\n bool ok=true;\n for(int i=0;i<N;i++) ok&=done[i];\n if(ok){\n cout<<mid<<\"\\n\";\n break;\n }\n }\n }\n \n}", "accuracy": 1, "time_ms": 600, "memory_kb": 11268, "score_of_the_acc": -1.0783, "final_rank": 18 }, { "submission_id": "aoj_2250_5261112", "code_snippet": "#include <iostream>\n#include <vector>\n#include <queue>\n#include <tuple>\n\ntemplate <class T>\nusing MinHeap = std::priority_queue<T, std::vector<T>, std::greater<T>>;\n\nusing namespace std;\nconstexpr int INF = 1 << 30;\n\nbool solve() {\n int n, tmax;\n cin >> n >> tmax;\n if (n == 0) return false;\n\n vector<tuple<int, int, int>> ts(n);\n for (auto& [m, l, k] : ts) cin >> m >> l >> k;\n\n auto judge = [&](int p) -> bool {\n MinHeap<int> heap;\n while (p--) heap.push(0);\n\n vector<bool> done(n, false);\n for (int q = 0; q < n; ++q) {\n auto t = heap.top();\n heap.pop();\n\n int ni = -1, nt = INF;\n for (int i = 0; i < n; ++i) {\n if (done[i]) continue;\n\n auto [m, l, k] = ts[i];\n auto p = l + k;\n\n if (l < t % p) {\n auto pt = (t + p - 1) / p * p;\n if (pt < nt) {\n ni = i;\n nt = pt;\n }\n } else {\n ni = i;\n nt = t;\n break;\n }\n }\n\n done[ni] = true;\n auto [m, l, k] = ts[ni];\n heap.push(nt + m);\n }\n\n while (!heap.empty()) {\n if (heap.top() > tmax) return false;\n heap.pop();\n }\n\n return true;\n };\n\n // int ok = n, ng = 0;\n // while (ok - ng > 1) {\n // auto mid = (ok + ng) / 2;\n\n // if (judge(mid)) {\n // ok = mid;\n // } else {\n // ng = mid;\n // }\n // }\n\n // cout << ok << \"\\n\";\n\n for (int p = 1; p <= n; ++p) {\n if (judge(p)) {\n cout << p << \"\\n\";\n break;\n }\n }\n return true;\n}\n\nint main() {\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n\n while (solve()) {}\n\n return 0;\n}", "accuracy": 1, "time_ms": 4810, "memory_kb": 3372, "score_of_the_acc": -0.8236, "final_rank": 15 }, { "submission_id": "aoj_2250_4917222", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <queue>\nusing namespace std;\n\nint solve(int n, int t, vector<int> &m, vector<int> &l, vector<int> &k){\n for(int i=1; i<n; i++){\n priority_queue<int> wait;\n for(int j=0; j<i; j++){\n wait.push(0);\n }\n vector<bool> used(n, false);\n bool ok = true;\n for(int j=0; j<n and ok; j++){\n int curr = -wait.top();\n wait.pop();\n int mintime = 1e9;\n int minidx = -1;\n for(int r=0; r<n; r++){\n if(used[r]) continue;\n if(curr%(l[r]+k[r]) <= l[r]){\n used[r] = true;\n wait.push(-(curr+m[r]));\n if(curr+m[r] > t) ok = false;\n break;\n }\n int time = curr +(l[r]+k[r] -curr%(l[r]+k[r]));\n if(time < mintime){\n mintime = time;\n minidx = r;\n }\n }\n if((int)wait.size() == i) continue;\n used[minidx] = true;\n wait.push(-(mintime +m[minidx]));\n if(mintime+m[minidx] > t) ok = false;\n }\n if(ok) return i;\n }\n return n;\n}\n\nint main(){\n while(1){\n int n,t;\n cin >> n >> t;\n if(n == 0) break;\n\n vector<int> m(n),l(n),k(n);\n for(int i=0; i<n; i++){\n cin >> m[i] >> l[i] >> k[i];\n }\n cout << solve(n, t, m, l, k) << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1380, "memory_kb": 3064, "score_of_the_acc": -0.2185, "final_rank": 7 }, { "submission_id": "aoj_2250_4829339", "code_snippet": "#include<iostream>\n#include<cstdio>\n#include<cstring>\n#include<string>\n#include<vector>\n#include<cmath>\n#include<algorithm>\n#include<map>\n#include<queue>\n#include<deque>\n#include<iomanip>\n#include<tuple>\n#include<cassert>\n#include<set>\n#include<bitset>\nusing namespace std;\ntypedef long long int LL;\ntypedef pair<int,int> P;\ntypedef pair<LL,int> LP;\nconst int INF=1<<30;\nconst LL MAX=1e9+7;\n\nvoid array_show(int *array,int array_n,char middle=' '){\n\tfor(int i=0;i<array_n;i++)printf(\"%d%c\",array[i],(i!=array_n-1?middle:'\\n'));\n}\nvoid array_show(LL *array,int array_n,char middle=' '){\n\tfor(int i=0;i<array_n;i++)printf(\"%lld%c\",array[i],(i!=array_n-1?middle:'\\n'));\n}\nvoid array_show(vector<int> &vec_s,int vec_n=-1,char middle=' '){\n\tif(vec_n==-1)vec_n=vec_s.size();\n\tfor(int i=0;i<vec_n;i++)printf(\"%d%c\",vec_s[i],(i!=vec_n-1?middle:'\\n'));\n}\nvoid array_show(vector<LL> &vec_s,int vec_n=-1,char middle=' '){\n\tif(vec_n==-1)vec_n=vec_s.size();\n\tfor(int i=0;i<vec_n;i++)printf(\"%lld%c\",vec_s[i],(i!=vec_n-1?middle:'\\n'));\n}\n\nconst int B=64,N=1100;\nbitset<N> bt[1100];\nLL bt2[1100][20];\nint t[1100][3];\nbitset<N> used;\nLL used2[20];\n\nint main(){\n\tint n,m;\n\tint i,j,k;\n\tint a,b,c;\n\twhile(cin>>n>>m){\n\t\tif(n==0)break;\n\t\tfor(i=0;i<=m;i++){\n\t\t\tbt[i].reset();\n\t\t}\n\t\tmemset(bt2,0,sizeof(bt2));\n\t\tmemset(t,0,sizeof(t));\n\t\tfor(i=0;i<n;i++){\n\t\t\tfor(j=0;j<3;j++){\n\t\t\t\tcin>>t[i][j];\n\t\t\t}\n\t\t\tfor(j=0;j<m;j+=t[i][1]+t[i][2]){\n\t\t\t\tfor(k=0;k<=t[i][1] && j+k<m;k++){\n\t\t\t\t\tbt[j+k][i]=1;\n\t\t\t\t\tbt2[j+k][i/B]|=1LL<<(i%B);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tfor(i=1;i<n;i++){\n\t\t\tvector<int> cnt(m+10,0);\n\t\t\tcnt[0]=i;\n\t\t\tused.reset();\n\t\t\tused.flip();\n\t\t\tmemset(used2,-1,sizeof(used2));\n\t\t\tfor(j=0;j<m;j++){\n\t\t\t\tfor(k=0,a=0;k<cnt[j];k++){\n\t\t\t\t\tif((used&bt[j]).none()){\n\t\t\t\t\t\tcnt[j+1]+=cnt[j]-k;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t\tfor(;a<20;a++){\n\t\t\t\t\t\tif(used2[a]&bt2[j][a])break;\n\t\t\t\t\t}\n\t\t\t\t\tb=__builtin_ctzll(used2[a]&bt2[j][a]);\n\t\t\t\t\tused2[a]-=1LL<<b;\n\t\t\t\t\tc=a*B+b;\n\t\t\t\t\tused[c]=0;\n\t\t\t\t\tif(j+t[c][0]>m){\n\t\t\t\t\t\ta=-1;break;\n\t\t\t\t\t}\n\t\t\t\t\tcnt[j+t[c][0]]++;\n\t\t\t\t}\n\t\t\t\tif(a==-1)break;\n\t\t\t}\n\t\t\tif(a==-1)continue;\n\t\t\t// for(k=0;k<n;k++){\n\t\t\t// \tcout<<used[k];\n\t\t\t// }\n\t\t\t// cout<<endl;\n\t\t\tfor(j=0;j<n;j++){\n\t\t\t\tif(used[j])break;\n\t\t\t}\n\t\t\tif(j==n)break;\n\t\t}\n\t\tcout<<i<<endl;\n\t}\n}", "accuracy": 1, "time_ms": 160, "memory_kb": 3352, "score_of_the_acc": -0.0513, "final_rank": 4 }, { "submission_id": "aoj_2250_4631589", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntypedef pair<ll, ll> l_l;\ntypedef pair<int, int> i_i;\ntemplate<class T>\ninline bool chmax(T &a, T b) {\n if(a < b) {\n a = b;\n return true;\n }\n return false;\n}\n\ntemplate<class T>\ninline bool chmin(T &a, T b) {\n if(a > b) {\n a = b;\n return true;\n }\n return false;\n}\n\nconst long double EPS = 1e-10;\nconst long long INF = 1e18;\nconst long double PI = acos(-1.0L);\n//const ll mod = 1000000007;\nll N, T;\nll M[1005], L[1005], K[1005];\n\nbool f(ll border) {\n ll op = border;\n ll clear = 0;\n vector<ll> mpop(2020);\n vector<bool> cleared(N);\n for(int t = 0; t <= T; t++) {\n clear += mpop[t];\n op += mpop[t];\n int idx = -1;\n while(op and idx < N) {\n idx++;\n if(idx == N) break;\n if(cleared[idx]) continue;\n //cerr << t << \" \" << op << \" \" << idx << endl;\n ll timer = t % (L[idx] + K[idx]);\n if(timer > L[idx]) continue;\n mpop[t+M[idx]]++;\n op--;\n cleared[idx] = true;\n }\n }\n return clear == N;\n}\n\nvoid solve() {\n for(int i = 0; i < N; i++) {\n cin >> M[i] >> L[i] >> K[i];\n }\n for(int i = 1; i <= N; i++) {\n if(f(i)) {\n cout << i << endl;\n return;\n }\n }\n}\n\nint main() {\n while(cin >> N >> T) {\n if(N == 0) break;\n solve();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 3180, "memory_kb": 3112, "score_of_the_acc": -0.5223, "final_rank": 11 }, { "submission_id": "aoj_2250_4191033", "code_snippet": "#include<iostream>\n#include<string>\n#include<cstdio>\n#include<vector>\n#include<cmath>\n#include<algorithm>\n#include<functional>\n#include<iomanip>\n#include<queue>\n#include<ciso646>\n#include<random>\n#include<map>\n#include<set>\n#include<bitset>\n#include<stack>\n#include<unordered_map>\n#include<utility>\n#include<cassert>\n#include<complex>\n#include<numeric>\nusing namespace std;\n\n//#define int long long\ntypedef long long ll;\n\ntypedef unsigned long long ul;\ntypedef unsigned int ui;\nconst ll mod = 1000000007;\nconst ll INF = (1e+18) + 7;\ntypedef pair<int, int>P;\n#define stop char nyaa;cin>>nyaa;\n#define rep(i,n) for(int i=0;i<n;i++)\n#define per(i,n) for(int i=n-1;i>=0;i--)\n#define Rep(i,sta,n) for(int i=sta;i<n;i++)\n#define rep1(i,n) for(int i=1;i<=n;i++)\n#define per1(i,n) for(int i=n;i>=1;i--)\n#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)\n#define all(v) (v).begin(),(v).end()\ntypedef pair<ll, ll> LP;\ntypedef long double ld;\ntypedef pair<ld, ld> LDP;\nconst ld eps = 1e-12;\nconst ld pi = acos(-1.0);\n//typedef vector<vector<ll>> mat;\ntypedef vector<int> vec;\n\nll mod_pow(ll a, ll n, ll m) {\n\tll res = 1;\n\twhile (n) {\n\t\tif (n & 1)res = res * a%m;\n\t\ta = a * a%m; n >>= 1;\n\t}\n\treturn res;\n}\n\nstruct modint {\n\tll n;\n\tmodint() :n(0) { ; }\n\tmodint(ll m) :n(m) {\n\t\tif (n >= mod)n %= mod;\n\t\telse if (n < 0)n = (n%mod + mod) % mod;\n\t}\n\toperator int() { return n; }\n};\nbool operator==(modint a, modint b) { return a.n == b.n; }\nmodint operator+=(modint &a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }\nmodint operator-=(modint &a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }\nmodint operator*=(modint &a, modint b) { a.n = ((ll)a.n*b.n) % mod; return a; }\nmodint operator+(modint a, modint b) { return a += b; }\nmodint operator-(modint a, modint b) { return a -= b; }\nmodint operator*(modint a, modint b) { return a *= b; }\nmodint operator^(modint a, int n) {\n\tif (n == 0)return modint(1);\n\tmodint res = (a*a) ^ (n / 2);\n\tif (n % 2)res = res * a;\n\treturn res;\n}\n\nll inv(ll a, ll p) {\n\treturn (a == 1 ? 1 : (1 - p * inv(p%a, a)) / a + p);\n}\nmodint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }\n\nconst int max_n = 1 << 18;\nmodint fact[max_n], factinv[max_n];\nvoid init_f() {\n\tfact[0] = modint(1);\n\tfor (int i = 0; i < max_n - 1; i++) {\n\t\tfact[i + 1] = fact[i] * modint(i + 1);\n\t}\n\tfactinv[max_n - 1] = modint(1) / fact[max_n - 1];\n\tfor (int i = max_n - 2; i >= 0; i--) {\n\t\tfactinv[i] = factinv[i + 1] * modint(i + 1);\n\t}\n}\nmodint comb(int a, int b) {\n\tif (a < 0 || b < 0 || a < b)return 0;\n\treturn fact[a] * factinv[b] * factinv[a - b];\n}\nusing mP = pair<modint, modint>;\n\nint dx[4] = { 0,1,0,-1 };\nint dy[4] = { 1,0,-1,0 };\n\nint take_first(bitset<1000> bt) {\n\tint c = bt.count();\n\tint res = 0;\n\tper(i, 10) {\n\t\tif ((bt >> (1 << i)).count() == c) {\n\t\t\tres += (1 << i);\n\t\t\tbt >>= (1 << i);\n\t\t}\n\t}\n\treturn res;\n}\n\nint n, t;\nvoid solve() {\n\tvector<vector<bool>> exi(n, vector<bool>(t + 1));\n\tvector<int> m(n);\n\n\tvector<bitset<1000>> loc(t + 1);\n\trep(i, n) {\n\t\tcin >> m[i];\n\t\tint l, k; cin >> l >> k;\n\t\tint cur = 0;\n\t\twhile (cur <= t) {\n\t\t\tRep1(j, cur, cur + l) {\n\t\t\t\tif (j > t)break;\n\t\t\t\texi[i][j] = true;\n\t\t\t\tloc[j][i] = 1;\n\t\t\t}\n\t\t\tcur += l + k;\n\t\t}\n\t}\n\tfor (int x = 1; x <= n; x++) {\n\t\tbitset<1000> rest; rep(i, n)rest[i] = 1;\n\t\tvector<int> ad(t + 1, 0);\n\t\tint cur = x;\n\t\trep(i, t + 1) {\n\t\t\tcur += ad[i];\n\t\t\tif (cur == 0)continue;\n\t\t\tbitset<1000> bt = rest & loc[i];\n\t\t\tint c = bt.count();\n\t\t\tbool fin = false;\n\t\t\tint len = min(c, cur);\n\t\t\trep(j, len) {\n\t\t\t\tint f = bt._Find_first();\n\t\t\t\t//cout << \"! \" << x << \" \" << i << \" \" << f << endl;\n\t\t\t\tif (i + m[f] <= t) {\n\t\t\t\t\trest[f] = 0;\n\t\t\t\t\tbt[f] = 0;\n\t\t\t\t\tcur--;\n\t\t\t\t\tad[i + m[f]]++;\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tfin = true; break;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (fin)break;\n\t\t}\n\t\tbool valid = true;\n\t\trep(i, n)if (rest[i])valid = false;\n\t\tif (valid) {\n\t\t\t//cout << \"ans is \";\n\t\t\tcout << x << endl; return;\n\t\t}\n\t}\n}\n\n\nsigned main() {\n\tios::sync_with_stdio(false);\n\tcin.tie(0);\n\t//cout << fixed << setprecision(10);\n\t//init_f();\n\t//init();\n\t//int t; cin >> t; rep(i, t)solve();\n\twhile (cin >> n >> t, n) {\n\t\tsolve();\n\t}\n\t//solve();\n\t//stop\n\treturn 0;\n}", "accuracy": 1, "time_ms": 90, "memory_kb": 7540, "score_of_the_acc": -0.5445, "final_rank": 12 }, { "submission_id": "aoj_2250_4188947", "code_snippet": "#include<iostream>\n#include<string>\n#include<cstdio>\n#include<vector>\n#include<cmath>\n#include<algorithm>\n#include<functional>\n#include<iomanip>\n#include<queue>\n#include<ciso646>\n#include<random>\n#include<map>\n#include<set>\n#include<bitset>\n#include<stack>\n#include<unordered_map>\n#include<utility>\n#include<cassert>\n#include<complex>\n#include<numeric>\nusing namespace std;\n\n//#define int long long\ntypedef long long ll;\n\ntypedef unsigned long long ul;\ntypedef unsigned int ui;\nconst ll mod = 1000000007;\nconst ll INF = (1e+18) + 7;\ntypedef pair<int, int>P;\n#define stop char nyaa;cin>>nyaa;\n#define rep(i,n) for(int i=0;i<n;i++)\n#define per(i,n) for(int i=n-1;i>=0;i--)\n#define Rep(i,sta,n) for(int i=sta;i<n;i++)\n#define rep1(i,n) for(int i=1;i<=n;i++)\n#define per1(i,n) for(int i=n;i>=1;i--)\n#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)\n#define all(v) (v).begin(),(v).end()\ntypedef pair<ll, ll> LP;\ntypedef long double ld;\ntypedef pair<ld, ld> LDP;\nconst ld eps = 1e-12;\nconst ld pi = acos(-1.0);\n//typedef vector<vector<ll>> mat;\ntypedef vector<int> vec;\n\nll mod_pow(ll a, ll n, ll m) {\n\tll res = 1;\n\twhile (n) {\n\t\tif (n & 1)res = res * a%m;\n\t\ta = a * a%m; n >>= 1;\n\t}\n\treturn res;\n}\n\nstruct modint {\n\tll n;\n\tmodint() :n(0) { ; }\n\tmodint(ll m) :n(m) {\n\t\tif (n >= mod)n %= mod;\n\t\telse if (n < 0)n = (n%mod + mod) % mod;\n\t}\n\toperator int() { return n; }\n};\nbool operator==(modint a, modint b) { return a.n == b.n; }\nmodint operator+=(modint &a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }\nmodint operator-=(modint &a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }\nmodint operator*=(modint &a, modint b) { a.n = ((ll)a.n*b.n) % mod; return a; }\nmodint operator+(modint a, modint b) { return a += b; }\nmodint operator-(modint a, modint b) { return a -= b; }\nmodint operator*(modint a, modint b) { return a *= b; }\nmodint operator^(modint a, int n) {\n\tif (n == 0)return modint(1);\n\tmodint res = (a*a) ^ (n / 2);\n\tif (n % 2)res = res * a;\n\treturn res;\n}\n\nll inv(ll a, ll p) {\n\treturn (a == 1 ? 1 : (1 - p * inv(p%a, a)) / a + p);\n}\nmodint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }\n\nconst int max_n = 1 << 18;\nmodint fact[max_n], factinv[max_n];\nvoid init_f() {\n\tfact[0] = modint(1);\n\tfor (int i = 0; i < max_n - 1; i++) {\n\t\tfact[i + 1] = fact[i] * modint(i + 1);\n\t}\n\tfactinv[max_n - 1] = modint(1) / fact[max_n - 1];\n\tfor (int i = max_n - 2; i >= 0; i--) {\n\t\tfactinv[i] = factinv[i + 1] * modint(i + 1);\n\t}\n}\nmodint comb(int a, int b) {\n\tif (a < 0 || b < 0 || a < b)return 0;\n\treturn fact[a] * factinv[b] * factinv[a - b];\n}\nusing mP = pair<modint, modint>;\n\nint dx[4] = { 0,1,0,-1 };\nint dy[4] = { 1,0,-1,0 };\n\nint take_first(bitset<1000> bt) {\n\tint c = bt.count();\n\tint res = 0;\n\tper(i, 10) {\n\t\tif ((bt >> (1<<i)).count() == c) {\n\t\t\tres += (1 << i);\n\t\t\tbt >>= (1<<i);\n\t\t}\n\t}\n\treturn res;\n}\n\nint n, t;\nvoid solve() {\n\tvector<vector<bool>> exi(n, vector<bool>(t+1));\n\tvector<int> m(n);\n\n\tvector<bitset<1000>> loc(t + 1);\n\trep(i, n) {\n\t\tcin >> m[i];\n\t\tint l, k; cin >> l >> k;\n\t\tint cur = 0;\n\t\twhile (cur <= t) {\n\t\t\tRep1(j, cur, cur + l) {\n\t\t\t\tif (j > t)break;\n\t\t\t\texi[i][j] = true;\n\t\t\t\tloc[j][i] = 1;\n\t\t\t}\n\t\t\tcur += l + k;\n\t\t}\n\t}\n\tfor(int x=1;x<=n;x++){\n\t\tbitset<1000> rest; rep(i, n)rest[i] = 1;\n\t\tvector<int> ad(t + 1, 0);\n\t\tint cur = x;\n\t\trep(i, t + 1) {\n\t\t\tcur += ad[i];\n\t\t\tif (cur == 0)continue;\n\t\t\tbitset<1000> bt = rest & loc[i];\n\t\t\tint c = bt.count();\n\t\t\tbool fin = false;\n\t\t\tint len = min(c, cur);\n\t\t\trep(j, len) {\n\t\t\t\tint f = take_first(bt);\n\t\t\t\t//cout << \"! \" << x << \" \" << i << \" \" << f << endl;\n\t\t\t\tif (i + m[f] <= t) {\n\t\t\t\t\trest[f] = 0;\n\t\t\t\t\tbt[f] = 0;\n\t\t\t\t\tcur--;\n\t\t\t\t\tad[i + m[f]]++;\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tfin = true; break;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (fin)break;\n\t\t}\n\t\tbool valid = true;\n\t\trep(i, n)if (rest[i])valid = false;\n\t\tif (valid) {\n\t\t\t//cout << \"ans is \";\n\t\t\tcout << x << endl; return;\n\t\t}\n\t}\n}\n\n\nsigned main() {\n\tios::sync_with_stdio(false);\n\tcin.tie(0);\n\t//cout << fixed << setprecision(10);\n\t//init_f();\n\t//init();\n\t//int t; cin >> t; rep(i, t)solve();\n\twhile (cin >> n >> t, n) {\n\t\tsolve();\n\t}\n\t//solve();\n\t//stop\n\t\treturn 0;\n}", "accuracy": 1, "time_ms": 3860, "memory_kb": 7436, "score_of_the_acc": -1.1561, "final_rank": 19 }, { "submission_id": "aoj_2250_3987867", "code_snippet": "#include<deque>\n#include<queue>\n#include<vector>\n#include<algorithm>\n#include<iostream>\n#include<set>\n#include<cmath>\n#include<tuple>\n#include<string>\n#include<chrono>\n#include<functional>\n#include<iterator>\n#include<random>\n#include<unordered_set>\n#include<array>\n#include<map>\n#include<iomanip>\n#include<assert.h>\n#include<list>\n#include<bitset>\n#include<stack>\n#include<memory>\n#include<numeric>\nusing namespace std;\nusing namespace std::chrono;\ntypedef long long int llint;\ntypedef long double lldo;\n#define mp make_pair\n#define mt make_tuple\n#define pub push_back\n#define puf push_front\n#define pob pop_back\n#define pof pop_front\n#define fir first\n#define sec second\n#define res resize\n#define ins insert\n#define era erase\n/*cout<<fixed<<setprecision(20);cin.tie(0);ios::sync_with_stdio(false);*/\nconst int mod=998244353 ;\nconst llint big=2.19e15+1;\nconst long double pai=3.141592653589793238462643383279502884197;\nconst long double eps=1e-4;\ntemplate <class T,class U>bool mineq(T& a,U b){if(a>b){a=b;return true;}return false;}\ntemplate <class T,class U>bool maxeq(T& a,U b){if(a<b){a=b;return true;}return false;}\nllint gcd(llint a,llint b){if(a%b==0){return b;}else return gcd(b,a%b);}\nllint lcm(llint a,llint b){if(a==0){return b;}return a/gcd(a,b)*b;}\ntemplate<class T> void SO(T& ve){sort(ve.begin(),ve.end());}\ntemplate<class T> void REV(T& ve){reverse(ve.begin(),ve.end());}\ntemplate<class T>llint LBI(const vector<T>&ar,T in){return lower_bound(ar.begin(),ar.end(),in)-ar.begin();}\ntemplate<class T>llint UBI(const vector<T>&ar,T in){return upper_bound(ar.begin(),ar.end(),in)-ar.begin();}\n\nbool solve(void){\n\tint i,n,j,T;cin>>n>>T;\n\tif(n==0){return false;}\n\tstatic bool call[1001][1001];\n\tvector<int>M(n),L(n),K(n);\n\tfor(i=0;i<n;i++){cin>>M[i]>>L[i]>>K[i];}\n\tfor(i=0;i<n;i++){\n\t\tfor(j=0;j<T;j++){call[j][i]=((j%(L[i]+K[i]))<=L[i]);}\n\t}\n\t\n\tint bmin=0,bmax=n;\n\twhile(bmax-bmin>1){\n\t\tint bgen=(bmin+bmax)/2;\n\t\tint ope=bgen;\n\t\tvector<int>owa(T+1);\n\t\tvector<bool>finC(n);\n\t\tbool can=true;\n\t\tfor(i=0;i<T;i++){\n\t\t\tope+=owa[i];\n\t\t\tfor(j=0;j<n;j++){\n\t\t\t\tif(finC[j]||ope==0||(!call[i][j])){continue;}\n\t\t\t\tif(i+M[j]>T){can=false;break;}\n\t\t\t\tope--;\n\t\t\t\towa[i+M[j]]++;\n\t\t\t\tfinC[j]=1;\n\t\t\t}\n\t\t\t\n\t\t}\n\t\tfor(j=0;j<n;j++){\n\t\t\tif(!finC[j]){can=false;break;}\n\t\t}\n\t\tif(can){bmax=bgen;}\n\t\telse{bmin=bgen;}\n\t}\n\tfor(int bgen=bmax-1;bgen>=1&&bmax-bgen<100;bgen-=5){\n\t\tint ope=bgen;\n\t\tvector<int>owa(T+1);\n\t\tvector<bool>finC(n);\n\t\tbool can=true;\n\t\tfor(i=0;i<T;i++){\n\t\t\tope+=owa[i];\n\t\t\tfor(j=0;j<n;j++){\n\t\t\t\tif(finC[j]||ope==0||(!call[i][j])){continue;}\n\t\t\t\tif(i+M[j]>T){can=false;break;}\n\t\t\t\tope--;\n\t\t\t\t//cerr<<\"i,j\"<<i<<' '<<j<<endl;\n\t\t\t\towa[i+M[j]]++;\n\t\t\t\tfinC[j]=1;\n\t\t\t}\n\t\t\t\n\t\t}\n\t\tfor(j=0;j<n;j++){\n\t\t\tif(!finC[j]){can=false;break;}\n\t\t}\n\t\tif(can){bmax=bgen;}\n\t\t\n\t}\n\tfor(int bgen=bmax-1;bgen>=1&&bmax-bgen<100;bgen--){\n\t\tint ope=bgen;\n\t\tvector<int>owa(T+1);\n\t\tvector<bool>finC(n);\n\t\tbool can=true;\n\t\tfor(i=0;i<T;i++){\n\t\t\tope+=owa[i];\n\t\t\tfor(j=0;j<n;j++){\n\t\t\t\tif(finC[j]||ope==0||(!call[i][j])){continue;}\n\t\t\t\tif(i+M[j]>T){can=false;break;}\n\t\t\t\tope--;\n\t\t\t\t//cerr<<\"i,j\"<<i<<' '<<j<<endl;\n\t\t\t\towa[i+M[j]]++;\n\t\t\t\tfinC[j]=1;\n\t\t\t}\n\t\t\t\n\t\t}\n\t\tfor(j=0;j<n;j++){\n\t\t\tif(!finC[j]){can=false;break;}\n\t\t}\n\t\tif(can){bmax=bgen;}\n\t\t\n\t}\n\tcout<<bmax<<endl;\n\treturn true;\n}\nint main(void){\n\tcout<<fixed<<setprecision(20);cin.tie(0);ios::sync_with_stdio(false);\n\twhile(solve()){}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 800, "memory_kb": 4092, "score_of_the_acc": -0.2464, "final_rank": 8 }, { "submission_id": "aoj_2250_3987622", "code_snippet": "#include <cstdio>\n#include <iostream>\n#include <algorithm>\n#include <vector>\n#include <utility>\n#include <tuple>\n#include <queue>\n#include <list>\nusing namespace std;\n// #define fprintf(...) void(0)\n\nbool judge(int N, int T,\n const vector< tuple<int, int, int> > &info, int O) {\n priority_queue< int, vector<int>, greater<int> > op;\n list< pair<int, int> > cs;\n for(int i=0; i<O; i++) op.emplace(0);\n for(int i=0; i<N; i++) cs.emplace_back(0, i);\n\n // fprintf(stderr, \"# num of operators = %d\\n\", O);\n for(int t=0; t<=T and cs.size(); t++) {\n // customer 選ぶ\n for(auto itr = cs.begin(); itr!=cs.end(); ) {\n int p, k; tie(p, k) = *itr;\n int M, L, K; tie(M, L, K) = info[k];\n\n bool inc = false;\n if(p <= t and t + M <= T) {\n int o = op.top(); op.pop();\n if(o > t) {\n // 対応できる operator がいない\n op.emplace(o);\n inc = true;\n }\n else {\n // 対応\n // fprintf(stderr, \"t = %d, customer = %d, op time = %d\\n\", t, k + 1, o);\n itr = cs.erase(itr);\n op.emplace(t + M);\n }\n }\n else if(t + M > T) {\n return false;\n }\n else {\n inc = true;\n }\n \n if(inc) {\n // update time\n if(t - L == p) {\n // fprintf(stderr, \"t = %d, update: customer = %d, time = %d\\n\", t, k+1, t+K);\n *itr = make_pair(t+K, k);\n if(t + K > T) return false;\n }\n itr++;\n }\n }\n }\n\n return cs.empty();\n}\n\nint solve_testcase() {\n int N, T; cin >> N >> T;\n if(N == 0 and T == 0) return 1;\n\n vector< tuple<int, int, int> > info;\n for(int i=0; i<N; i++) {\n int M, L, K; cin >> M >> L >> K;\n info.emplace_back(M, L, K);\n }\n\n for(int m=1; m<=N; m++) {\n if(judge(N, T, info, m)) {\n cout << m << endl;\n return 0;\n }\n }\n return 0;\n}\n\nint main() {\n while(!solve_testcase());\n return 0;\n}", "accuracy": 1, "time_ms": 3490, "memory_kb": 3108, "score_of_the_acc": -0.5732, "final_rank": 13 }, { "submission_id": "aoj_2250_3574752", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing pii = pair<int, int>;\n\nint main() {\n int n, t;\n while(cin >> n >> t, n) {\n vector<int> m(n), l(n), k(n);\n for(int i = 0; i < n; ++i) {\n cin >> m[i] >> l[i] >> k[i];\n }\n\n auto check = [&] (int num) {\n vector<int> restore(t + 1);\n list<int> ls;\n for(int i = 0; i < n; ++i) ls.push_back(i);\n int remain = num;\n for(int i = 0; i <= t; ++i) {\n remain += restore[i];\n auto it = ls.begin();\n while(remain > 0 && it != ls.end()) {\n const int idx = *it;\n if(i % (l[idx] + k[idx]) <= l[idx]) {\n --remain;\n if(i + m[idx] > t) return false;\n restore[i + m[idx]] += 1;\n it = ls.erase(it);\n } else {\n ++it;\n }\n }\n }\n return ls.empty();\n };\n for(int i = 1; i <= n; ++i) {\n if(check(i)) {\n cout << i << endl;\n break;\n }\n }\n }\n}", "accuracy": 1, "time_ms": 290, "memory_kb": 3064, "score_of_the_acc": -0.0381, "final_rank": 1 }, { "submission_id": "aoj_2250_3229143", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))\n#define all(x) (x).begin(),(x).end()\n#define pb push_back\n#define fi first\n#define se second\n#define dbg(x) cout<<#x\" = \"<<((x))<<endl\ntemplate<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<\"(\"<<p.fi<<\",\"<<p.se<<\")\";return o;}\ntemplate<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<\"[\";for(T t:v){o<<t<<\",\";}o<<\"]\";return o;}\n\nconst int N = 1010;\n\nint n,t;\nint M[N],L[N],K[N];\nint mod[N];\n\nint c[N];\nint ed[N];\n\nconst int INF = 191919;\ninline bool check(int x){\n fill(c,c+N,INF);\n fill(ed,ed+N,0);\n int op = x;\n\n rep(i,t+1){\n // end operation\n op += ed[i];\n\n rep(j,n)if(c[j]==INF){\n if(i+M[j]>t) return false;\n\n int mt = i%mod[j];\n if(mt<=L[j] && op>0){\n --op;\n int et = i+M[j];\n c[j] = et;\n\n if(et<=t) ++ed[et];\n }\n }\n }\n\n rep(i,n)if(c[i]>t) return false;\n return true;\n}\n\nint main(){\n while(scanf(\" %d %d\", &n, &t),n){\n rep(i,n){\n scanf(\" %d %d %d\", &M[i], &L[i], &K[i]);\n mod[i] = (L[i]+K[i]);\n }\n\n int ok=1;\n while(ok<=n && !check(ok)) ++ok;\n printf(\"%d\\n\",ok);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 330, "memory_kb": 3116, "score_of_the_acc": -0.051, "final_rank": 3 } ]
aoj_2251_cpp
Problem J: Merry Christmas International Christmas Present Company (ICPC) is a company to employ Santa and deliver presents on Christmas. Many parents request ICPC to deliver presents to their children at specified time of December 24. Although same Santa can deliver two or more presents, because it takes time to move between houses, two or more Santa might be needed to finish all the requests on time. Employing Santa needs much money, so the president of ICPC employed you, a great program- mer, to optimize delivery schedule. Your task is to write a program to calculate the minimum number of Santa necessary to finish the given requests on time. Because each Santa has been well trained and can conceal himself in the town, you can put the initial position of each Santa anywhere. Input The input consists of several datasets. Each dataset is formatted as follows. N M L u 1 v 1 d 1 u 2 v 2 d 2 . . . u M v M d M p 1 t 1 p 2 t 2 . . . p L t L The first line of a dataset contains three integer, N , M and L (1 ≤ N ≤ 100, 0 ≤ M ≤ 1000, 1 ≤ L ≤ 1000) each indicates the number of houses, roads and requests respectively. The following M lines describe the road network. The i -th line contains three integers, u i , v i , and d i (0 ≤ u i < v i ≤ N - 1, 1 ≤ d i ≤ 100) which means that there is a road connecting houses u i and v i with d i length. Each road is bidirectional. There is at most one road between same pair of houses. Whole network might be disconnected. The next L lines describe the requests. The i -th line contains two integers, p i and t i (0 ≤ p i ≤ N - 1, 0 ≤ t i ≤ 10 8 ) which means that there is a delivery request to house p i on time t i . There is at most one request for same place and time. You can assume that time taken other than movement can be neglectable, and every Santa has the same speed, one unit distance per unit time. The end of the input is indicated by a line containing three zeros separated by a space, and you should not process this as a test case. Output Print the minimum number of Santa necessary to finish all the requests on time. Sample Input 3 2 3 0 1 10 1 2 10 0 0 1 10 2 0 3 2 4 0 1 10 1 2 10 0 0 1 10 2 20 0 40 10 10 10 0 1 39 2 3 48 3 5 20 4 8 43 3 9 10 8 9 40 3 4 5 5 7 20 1 7 93 1 3 20 0 0 1 100000000 2 100 3 543 4 500 5 400 6 300 7 200 8 100 9 100 0 0 0 Output for the Sample Input 2 1 4
[ { "submission_id": "aoj_2251_10848566", "code_snippet": "#include<stdio.h>\n#include<string.h>\n#include<iostream>\n#include<algorithm>\n#include<queue>\nusing namespace std;\n\ntypedef int ll;\nconst int inf = 200000000;\n#define N 2100\nint lef[1005], pn;\nint tim, T[1005];\nvector<int> G[1005];\nbool match(int x){\n for(int i = 0; i < G[x].size(); i++)\n {\n int v = G[x][i];\n if(tim != T[v])\n {\n T[v] = tim;\n if(lef[v]==-1 || match(lef[v]))\n {\n lef[v] = x;\n return true;\n }\n }\n }\n return false;\n}\nint work(){\n int ans = 0;\n memset(lef, -1, sizeof lef);\n tim = 1;\n memset(T, 0, sizeof T);\n for(int i = 1; i <= pn; i++)\n {\n tim++;\n if(match(i))ans++;\n }\n return ans;\n}\nint dis[105][105], a[1005], b[1005];\nint n, m, L;\nint solve(){\n pn = L;\n for(int i = 1; i <= L; i++) G[i].clear();\n for(int i = 1; i <= L; i++)\n {\n for(int j = 1; j <= L; j++)\n if(i!=j && b[i] + dis[a[i]][a[j]] <= b[j])\n {\n G[i].push_back(j);\n }\n }\n\treturn L - work();\n}\nint main(){\n\twhile(scanf(\"%d %d %d\", &n, &m, &L), n+m+L){\n for(int i = 0; i < n; i++)\n for(int j = 0; j < n; j++)\n if(i!=j)\n dis[i][j] = dis[j][i] = inf;\n else dis[i][j] = dis[j][i] = 0;\n while(m--){\n int u, v, d;scanf(\"%d %d %d\", &u, &v, &d);\n dis[u][v] = dis[v][u] = min(dis[u][v], d);\n }\n for(int k = 0; k < n; k++)\n for(int i = 0; i < n; i++)if(i!=k)\n for(int j = 0; j < n; j++)if(i!=j && j!=k)\n dis[j][i] = dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);\n for(int i = 1; i <= L; i++)scanf(\"%d %d\", &a[i], &b[i]);\n // puts(\"--\");\n printf(\"%d\\n\",solve());\n\t}\n\treturn 0;\n}\n/*\n\n\n*/\n\n\n\n/*\n#include<stdio.h>\n#include<string.h>\n#include<iostream>\n#include<algorithm>\n#include<queue>\nusing namespace std;\n\ntypedef int ll;\nconst int inf = 1000000;\n#define N 10100\nchar c[105][105], way[1005];\nint go[300], step[1005], top;\nint mp[105][105], v[12], num;\nint hpinit, hpmax, n, m, dp[2][1<<12];\n\nbool solve(){\n\tint cur = 0;\n\tmemset(dp[cur], 0, sizeof dp[cur]);\n\tfor(int j = (1<<num)-1; j >= 0; j--)\n\t{\n\t\tint now = hpinit;\n\t\tfor(int k = 0; k < num; k++)\n\t\t\tif((j & (1<<k))==0)\n\t\t\t\tnow += v[k];\n\t\tdp[cur][j] = min(now, hpmax);\n\t}\n\tint x = 1, y = 1;\n\tfor(int i = 1; i <= top; i++)\n\t{\n\t\tcur ^= 1;\n\t\tmemset(dp[cur], 0, sizeof dp[cur]);\n\t\tif(way[i]=='U') x -= step[i];\n\t\telse if(way[i]=='D') x += step[i];\n\t\telse if(way[i]=='L') y -= step[i];\n\t\telse y += step[i];\n\t\tfor(int j = (1<<num)-1; j >= 0; j--)\n\t\t\tif(dp[cur^1][j])\n\t\t\t{\n\t\t\t\tint now = dp[cur^1][j] - mp[x][y];\n\t\t\t\tdp[cur][j] = max(dp[cur][j], now);\n\t\t\t\tfor(int k = 0; k < num; k++)\n\t\t\t\t\tif(j & (1<<k))\n\t\t\t\t\t\tdp[cur][j & (~(1<<k))] = max(dp[cur][j & (~(1<<k))], min(now+v[k], hpmax));\n\t\t\t}\n\t}\n\tfor(int j = (1<<num)-1; j >= 0; j--)\n\t\tif(dp[cur][j])return true;\n\treturn false;\n}\nvoid input(){\n\tscanf(\"%d %d\", &n, &m);\n\tfor(int i = 1; i <= n; i++)\n\t\tscanf(\"%s\", c[i]+1);\n\tint tmp, val; scanf(\"%d\", &tmp);\n\tchar s[3];\n\twhile(tmp--){\n\t\tscanf(\"%s %d\", s, &val);\n\t\tgo[s[0]] = val;\n\t}\n\tscanf(\"%d\", &top);\n\tfor(int i = 1; i <= top; i++) {\n\t\tscanf(\"%s %d\", s, &step[i]);\n\t\tway[i] = s[0];\n\t}\n\tscanf(\"%d\", &num);\n\tfor(int i = 0; i < num; i++)scanf(\"%d\", &v[i]);\n\tfor(int i = 1; i <= n; i++)\n\t\tfor(int j = 1; j <= m; j++)\n\t\t\tmp[i][j] = go[c[i][j]];\n}\nint main(){\n\twhile(scanf(\"%d %d\", &hpinit, &hpmax), hpinit + hpmax){\n\t\tinput();\n\t\tsolve() ? puts(\"YES\"):puts(\"NO\");\n\t}\n\treturn 0;\n}\n/*\n\n\n*/", "accuracy": 1, "time_ms": 550, "memory_kb": 7640, "score_of_the_acc": -0.3745, "final_rank": 8 }, { "submission_id": "aoj_2251_10833922", "code_snippet": "#include<stdio.h>\n#include<string.h>\n#include<iostream>\n#include<algorithm>\n#include<queue>\nusing namespace std;\n\ntypedef int ll;\nconst int inf = 200000000;\n#define N 2100\nint lef[1005], pn;\nint tim, T[1005];\nvector<int> G[1005];\nbool match(int x){\n for(int i = 0; i < G[x].size(); i++)\n {\n int v = G[x][i];\n if(tim != T[v])\n {\n T[v] = tim;\n if(lef[v]==-1 || match(lef[v]))\n {\n lef[v] = x;\n return true;\n }\n }\n }\n return false;\n}\nint work(){\n int ans = 0;\n memset(lef, -1, sizeof lef);\n tim = 1;\n memset(T, 0, sizeof T);\n for(int i = 1; i <= pn; i++)\n {\n tim++;\n if(match(i))ans++;\n }\n return ans;\n}\nint dis[105][105], a[1005], b[1005];\nint n, m, L;\nint solve(){\n pn = L;\n for(int i = 1; i <= L; i++) G[i].clear();\n for(int i = 1; i <= L; i++)\n {\n for(int j = 1; j <= L; j++)\n if(i!=j && b[i] + dis[a[i]][a[j]] <= b[j])\n {\n G[i].push_back(j);\n }\n }\n\treturn L - work();\n}\nint main(){\n\twhile(scanf(\"%d %d %d\", &n, &m, &L), n+m+L){\n for(int i = 0; i < n; i++)\n for(int j = 0; j < n; j++)\n if(i!=j)\n dis[i][j] = dis[j][i] = inf;\n else dis[i][j] = dis[j][i] = 0;\n while(m--){\n int u, v, d;scanf(\"%d %d %d\", &u, &v, &d);\n dis[u][v] = dis[v][u] = min(dis[u][v], d);\n }\n for(int k = 0; k < n; k++)\n for(int i = 0; i < n; i++)if(i!=k)\n for(int j = 0; j < n; j++)if(i!=j && j!=k)\n dis[j][i] = dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);\n for(int i = 1; i <= L; i++)scanf(\"%d %d\", &a[i], &b[i]);\n // puts(\"--\");\n printf(\"%d\\n\",solve());\n\t}\n\treturn 0;\n}\n/*\n\n\n*/\n\n\n\n/*\n#include<stdio.h>\n#include<string.h>\n#include<iostream>\n#include<algorithm>\n#include<queue>\nusing namespace std;\n\ntypedef int ll;\nconst int inf = 1000000;\n#define N 10100\nchar c[105][105], way[1005];\nint go[300], step[1005], top;\nint mp[105][105], v[12], num;\nint hpinit, hpmax, n, m, dp[2][1<<12];\n\nbool solve(){\n\tint cur = 0;\n\tmemset(dp[cur], 0, sizeof dp[cur]);\n\tfor(int j = (1<<num)-1; j >= 0; j--)\n\t{\n\t\tint now = hpinit;\n\t\tfor(int k = 0; k < num; k++)\n\t\t\tif((j & (1<<k))==0)\n\t\t\t\tnow += v[k];\n\t\tdp[cur][j] = min(now, hpmax);\n\t}\n\tint x = 1, y = 1;\n\tfor(int i = 1; i <= top; i++)\n\t{\n\t\tcur ^= 1;\n\t\tmemset(dp[cur], 0, sizeof dp[cur]);\n\t\tif(way[i]=='U') x -= step[i];\n\t\telse if(way[i]=='D') x += step[i];\n\t\telse if(way[i]=='L') y -= step[i];\n\t\telse y += step[i];\n\t\tfor(int j = (1<<num)-1; j >= 0; j--)\n\t\t\tif(dp[cur^1][j])\n\t\t\t{\n\t\t\t\tint now = dp[cur^1][j] - mp[x][y];\n\t\t\t\tdp[cur][j] = max(dp[cur][j], now);\n\t\t\t\tfor(int k = 0; k < num; k++)\n\t\t\t\t\tif(j & (1<<k))\n\t\t\t\t\t\tdp[cur][j & (~(1<<k))] = max(dp[cur][j & (~(1<<k))], min(now+v[k], hpmax));\n\t\t\t}\n\t}\n\tfor(int j = (1<<num)-1; j >= 0; j--)\n\t\tif(dp[cur][j])return true;\n\treturn false;\n}\nvoid input(){\n\tscanf(\"%d %d\", &n, &m);\n\tfor(int i = 1; i <= n; i++)\n\t\tscanf(\"%s\", c[i]+1);\n\tint tmp, val; scanf(\"%d\", &tmp);\n\tchar s[3];\n\twhile(tmp--){\n\t\tscanf(\"%s %d\", s, &val);\n\t\tgo[s[0]] = val;\n\t}\n\tscanf(\"%d\", &top);\n\tfor(int i = 1; i <= top; i++) {\n\t\tscanf(\"%s %d\", s, &step[i]);\n\t\tway[i] = s[0];\n\t}\n\tscanf(\"%d\", &num);\n\tfor(int i = 0; i < num; i++)scanf(\"%d\", &v[i]);\n\tfor(int i = 1; i <= n; i++)\n\t\tfor(int j = 1; j <= m; j++)\n\t\t\tmp[i][j] = go[c[i][j]];\n}\nint main(){\n\twhile(scanf(\"%d %d\", &hpinit, &hpmax), hpinit + hpmax){\n\t\tinput();\n\t\tsolve() ? puts(\"YES\"):puts(\"NO\");\n\t}\n\treturn 0;\n}\n/*\n\n\n*/", "accuracy": 1, "time_ms": 550, "memory_kb": 7508, "score_of_the_acc": -0.3724, "final_rank": 7 }, { "submission_id": "aoj_2251_10735036", "code_snippet": "#include <bits/stdc++.h>\n#include <bit>\nusing namespace std;\nusing ll = long long;\nusing ull = unsigned long long;\nusing ld = long double;\nusing i128 = __int128_t;\nusing pii = pair<int, int>;\nusing pic = pair<int, char>;\nusing pci = pair<char, int>;\nusing pll = pair<ll, ll>;\nusing pllc = pair<ll, char>;\n#define rep(i, a, n) for (int i = a; i < (n); i++)\n\n// ===== プロトタイプ宣言 =====\nint solve();\n\n// ===== main関数 =====\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n return solve();\n}\n\n\nvoid prvec(vector<string> V) {\n for (const auto& v : V)\n cout << v << endl;\n}\n\nvoid prvec(vector<bool> V) {\n for (const auto& v : V)\n cout << boolalpha << setw(5) << setfill(' ') << v << \" \";\n cout << endl;\n}\n\ntemplate <typename T>\nvoid prvec(vector<T> V) {\n for (const auto& v : V)\n cout << v << \" \";\n cout << endl;\n}\n\ntemplate <typename T>\nvoid prvec(list<T> V) {\n for (const auto& v : V)\n cout << v << \" \";\n cout << endl;\n}\n\ntemplate <typename T>\nvoid prvec(set<T> V) {\n for (const auto& v : V)\n cout << v << \" \";\n cout << endl;\n}\n\ntemplate <typename T>\nvoid prvec(unordered_set<T> V) {\n for (const auto& v : V)\n cout << v << \" \";\n cout << endl;\n}\n\ntemplate <typename T>\nvoid prvec(multiset<T> V) {\n for (const auto& v : V)\n cout << v << \" \";\n cout << endl;\n}\n\ntemplate <typename T1>\nvoid prvec(deque<T1> V) {\n for (const auto& v : V)\n cout << v << \" \";\n cout << endl;\n}\n\nvoid prvec(set<pii> V) {\n for (const auto& [a, b] : V)\n cout << \"{\" << a << \", \" << b << \"}, \";\n cout << endl;\n}\n\nvoid prvec(deque<pii> V) {\n for (const auto& [a, b] : V)\n cout << \"{\" << a << \", \" << b << \"}, \";\n cout << endl;\n}\n\nvoid prvec(deque<pll> V) {\n for (const auto& [a, b] : V)\n cout << \"{\" << a << \", \" << b << \"}, \";\n cout << endl;\n}\n\nvoid prvec(vector<pii> V) {\n for (const auto& [a, b] : V)\n cout << \"{\" << a << \", \" << b << \"}, \";\n cout << endl;\n}\n\nvoid prvec(vector<pll> V) {\n for (const auto& [a, b] : V)\n cout << \"{\" << a << \", \" << b << \"}, \";\n cout << endl;\n}\n\ntemplate <typename T1, typename T2>\nvoid prvec(map<T1, T2> V) {\n for (const auto& [k, v] : V)\n cout << \"{\" << k << \": \" << v << \"}, \";\n cout << endl;\n}\n\ntemplate <typename T1, typename T2, typename T3>\nvoid prvec(map<T1, pair<T2, T3>> V) {\n for (const auto& [k, v] : V)\n cout << k << \" : {\" << v.first << \" \" << v.second << \"}\" << endl;\n}\n\ntemplate <typename T1, typename T2, typename T3>\nvoid prvec(map<T1, vector<pair<T2, T3>>> V) {\n for (const auto& [k, v] : V) {\n cout << k << \" : \" ;\n for (const auto& [a, b] : v)\n cout << \"{\" << a << \", \" << b << \"}, \";\n cout << endl;\n }\n}\n\nvoid prvec(map<int, bool> V) {\n for (const auto& [k, v] : V)\n cout << k << \" : \" << boolalpha << v << endl;\n}\n\nvoid prvec(map<string, pii> V) {\n for (const auto& [k, v] : V) \n cout << k << \" : \" << v.first << \" \" << v.second << endl;\n}\n\ntemplate <typename T>\nvoid prvec(map<pii, T> V) {\n for (const auto& [k, v] : V) \n cout << \"{\" << k.first << \", \" << k.second << \"}: \" << v << endl;\n}\n\nvoid prvec(map<int, vector<int>> V) {\n for (const auto& [k, vec] : V) {\n cout << k << \" : \";\n for (const auto& v : vec) {\n cout << v << \" \";\n }\n cout << endl;\n }\n}\n\nvoid prvec(map<char, vector<int>> V) {\n for (const auto& [k, vec] : V) {\n cout << k << \" : \";\n for (const auto& v : vec) {\n cout << v << \" \";\n }\n cout << endl;\n }\n}\n\nvoid prvec(map<char, vector<pii>> V) {\n for (const auto& [k, vec] : V) {\n cout << k << \" : \";\n for (const auto& [a, b] : vec) {\n cout << \"{\" << a << \", \" << b << \"}, \";\n }\n cout << endl;\n }\n}\n\nvoid prvec(map<int, vector<pii>> V) {\n for (const auto& [k, vec] : V) {\n cout << k << \" : \";\n for (const auto& [a, b] : vec) {\n cout << \"{\" << a << \", \" << b << \"}, \";\n }\n cout << endl;\n }\n}\n\nvoid prvec(map<int, stack<int>> V) {\n for (const auto& [k, stk] : V) {\n cout << k << \" : \";\n stack<int> stk2 = stk;\n while (!stk2.empty()) {\n int v = stk2.top(); stk2.pop();\n cout << v << \" \";\n }\n cout << endl;\n }\n}\n\ntemplate <typename T1, typename T2>\nvoid prvec(vector<pair<T1, T2>> V) {\n for (const auto& [a, b] : V)\n cout << \"{\" << a << \", \" << b << \"}, \";\n cout << endl;\n}\n\nvoid prvec(vector<pair<string, string>> V) {\n for (const auto& [a, b] : V)\n cout << \"{\" << a << \", \" << b << \"}, \";\n cout << endl;\n}\n\nvoid prvec(vector<pair<double, int>> V) {\n for (const auto& [a, b] : V)\n cout << \"{\" << a << \", \" << b << \"}, \";\n cout << endl;\n}\n\nvoid prvec(vector<pair<double, double>> V) {\n for (const auto& [a, b] : V)\n cout << fixed << setprecision(15) << \"{\" << a << \", \" << b << \"}, \";\n cout << endl;\n}\n\nvoid prvec(vector<pair<char, int>> V) {\n for (const auto& [a, b] : V)\n cout << \"{\" << a << \", \" << b << \"}, \";\n cout << endl;\n}\n\nvoid prvec(vector<pair<int, char>> V) {\n for (const auto& [a, b] : V)\n cout << \"{\" << a << \", \" << b << \"}, \";\n cout << endl;\n}\n\ntemplate <typename T1, typename T2>\nvoid prvec(unordered_map<T1, T2> V) {\n for (const auto& [k, v] : V)\n cout << \"{\" << k << \": \" << v << \"}, \";\n cout << endl;\n}\n\ntemplate <typename T1, typename T2>\nvoid prvec(set<pair<T1, T2>> V) {\n for (const auto& [a, b] : V)\n cout << \"{\" << a << \", \" << b << \"}, \";\n cout << endl;\n}\n\nvoid prvec(vector<pair<int, vector<int>>> V) {\n for (const auto& [a, vec] : V) {\n cout << a << \" : \";\n for (const auto& v : vec) cout << v << \" \";\n cout << endl;\n }\n}\n\nvoid prvec(vector<pair<int, set<int>>> V) {\n for (const auto& [a, st] : V) {\n cout << a << \" : \";\n for (const auto& v : st) cout << v << \" \";\n cout << endl;\n }\n}\n\ntemplate <typename T1, typename T2, typename T3>\nvoid prvec(vector<tuple<T1, T2, T3>> V) {\n for (const auto& [a, b, c] : V)\n cout << \"{\" << a << \", \" << b << \", \" << c << \"}, \";\n cout << endl;\n}\n\ntemplate <typename T1, typename T2, typename T3>\nvoid prvec(set<tuple<T1, T2, T3>> V) {\n for (const auto& [a, b, c] : V)\n cout << \"{\" << a << \", \" << b << \", \" << c << \"}, \";\n cout << endl;\n}\n\ntemplate <typename T1, typename T2, typename T3, typename T4>\nvoid prvec(vector<tuple<T1, T2, T3, T4>> V) {\n for (const auto& [a, b, c, d] : V)\n cout << \"{\" << a << \", \" << b << \", \" << c << \", \" << d << \"}, \";\n cout << endl;\n}\n\nclass UnionFind {\n private :\n vector<int> parent;\n vector<int> size;\n \n public :\n UnionFind(int n) : parent(n), size(n, 1) {\n for (int i = 0; i < n; i++) parent[i] = i;\n }\n \n int find(int x) {\n if (parent[x] == x) return x;\n return parent[x] = find(parent[x]);\n }\n \n bool unite(int x, int y) {\n int rootX = find(x);\n int rootY = find(y);\n if (rootX == rootY) return false; // cycle検出\n \n if (size[rootX] < size[rootY]) swap(rootX, rootY);\n parent[rootY] = rootX;\n size[rootX] += size[rootY];\n return true;\n }\n \n int getSize(int x) {\n return size[find(x)];\n }\n \n bool same(int x, int y) {\n return find(x) == find(y);\n }\n};\n\ntemplate <class Abel>\n// 重み付きUnionFindの定義\nstruct WeightedUnionFind {\n vector<int> parent, size;\n vector<Abel> diff_weight;\n\n WeightedUnionFind(int n)\n : parent(n), size(n, 1), diff_weight(n, 0) {\n iota(parent.begin(), parent.end(), 0);\n }\n\n int find(int x) {\n if (parent[x] == x) return x;\n int r = find(parent[x]);\n diff_weight[x] += diff_weight[parent[x]];\n return parent[x] = r;\n }\n\n Abel weight(int x) {\n find(x);\n return diff_weight[x];\n }\n\n bool unite(int u, int v, ll w) {\n Abel wu = weight(u), wv = weight(v);\n int ru = find(u), rv = find(v);\n if (ru == rv) {\n return (wv - wu == w); // すでに矛盾ないかチェック\n }\n\n Abel dw = w + wu - wv;\n\n // union by size(大きいほうに小さいほうをつける)\n if (size[ru] < size[rv]) {\n swap(ru, rv);\n swap(u, v);\n dw = -dw;\n }\n\n parent[rv] = ru;\n diff_weight[rv] = dw;\n size[ru] += size[rv];\n return true;\n }\n\n bool same(int x, int y) {\n return find(x) == find(y);\n }\n\n Abel diff(int x, int y) {\n return weight(y) - weight(x);\n }\n};\n\n// **Binary Indexed Tree (Fenwick Tree)**\n// 1-indexed\ntemplate <typename T>\nstruct FenwickTree {\n int size;\n vector<T> BIT;\n \n FenwickTree(int n) {\n size = n;\n BIT.assign(n + 1, 0);\n }\n \n void add(int idx, T val) {\n while (idx <= size) {\n BIT[idx] += val;\n idx += idx & -idx;\n }\n }\n \n T sum(int idx) {\n T s = 0;\n while (idx > 0) {\n s += BIT[idx];\n idx -= (idx & -idx);\n }\n return s;\n }\n \n T range_sum(int l, int r) {\n return sum(r) - sum(l - 1);\n }\n};\n\n// modint: mod計算をintを扱うように扱える構造体\n// https://qiita.com/drken/items/3b4fdf0a78e7a138cd9a#8-modint\ntemplate<int MOD> struct Fp {\n ll val;\n constexpr Fp(ll v = 0) noexcept : val(v % MOD) {\n if (val < 0) val += MOD;\n }\n constexpr int getmod() { return MOD; }\n constexpr Fp operator - () const noexcept {\n return val ? MOD - val : 0;\n }\n constexpr Fp operator + (const Fp& r) const noexcept { return Fp(*this) += r;}\n constexpr Fp operator - (const Fp& r) const noexcept { return Fp(*this) -= r;}\n constexpr Fp operator * (const Fp& r) const noexcept { return Fp(*this) *= r;}\n constexpr Fp operator / (const Fp& r) const noexcept { return Fp(*this) /= r;}\n constexpr Fp& operator += (const Fp& r) noexcept {\n val += r.val;\n if (val >= MOD) val -= MOD;\n return *this;\n }\n constexpr Fp& operator -= (const Fp& r) noexcept {\n val -= r.val;\n if (val < 0) val += MOD;\n return *this;\n }\n constexpr Fp& operator *= (const Fp& r) noexcept {\n val = val * r.val % MOD;\n return *this;\n }\n constexpr Fp& operator /= (const Fp& r) noexcept {\n return *this *= modpow(r, MOD - 2);\n // cout << \"here\" << endl;\n // ll a = r.val, b = MOD, u = 1, v = 0;\n // while (b) {\n // ll t = a / b;\n // a -= t * b; swap(a, b);\n // u -= t * v; swap(u, v);\n // }\n // val = val * u % MOD;\n // if (val < 0) val += MOD;\n // return *this;\n }\n constexpr bool operator == (const Fp& r) const noexcept {\n return this->val == r.val;\n }\n constexpr bool operator != (const Fp& r) const noexcept {\n return this->val != r.val;\n }\n constexpr bool operator < (const Fp& r) const noexcept { return this->val < r.val;}\n constexpr bool operator <= (const Fp& r) const noexcept { return this->val <= r.val;}\n constexpr bool operator > (const Fp& r) const noexcept { return this->val > r.val;}\n constexpr bool operator >= (const Fp& r) const noexcept { return this->val >= r.val;}\n friend constexpr ostream& operator << (ostream &os, const Fp<MOD>& x) {\n return os << x.val;\n }\n friend constexpr istream& operator >> (istream &is, Fp<MOD>& x) {\n ll v;\n is >> v;\n x = Fp<MOD>(v);\n return is;\n }\n friend constexpr Fp<MOD> modpow(const Fp<MOD> &a, ll n) noexcept {\n if (n == 0) return 1;\n auto t = modpow(a, n / 2);\n t = t * t;\n if (n & 1) t = t * a;\n return t;\n }\n};\nusing mint = Fp<998244353>;\n\nint solve() {\n while (true) {\n int N, M, L;\n cin >> N >> M >> L;\n if (N == 0 && M == 0 && L == 0) break;\n \n const int INF = 1e9 + 7;\n vector<vector<int>> dist(N, vector<int>(N, INF));\n for (int i = 0; i < N; i++) dist[i][i] = 0;\n \n for (int i = 0; i < M; i++) {\n int u, v, d; cin >> u >> v >> d;\n dist[u][v] = dist[v][u] = min(dist[u][v], d);\n }\n \n for (int k = 0; k < N; k++) \n for (int i = 0; i < N; i++) \n for (int j = 0; j < N; j++)\n dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);\n \n vector<pii> requests(L);\n for (int i = 0; i < L; i++) {\n int t, p; cin >> p >> t;\n requests[i] = {t, p};\n }\n \n sort(requests.begin(), requests.end());\n \n vector<vector<int>> G(L);\n for (int i = 0; i < L; i++) {\n auto [ti, pi] = requests[i];\n for (int j = i + 1; j < L; j++) {\n auto [tj, pj] = requests[j];\n if (ti + dist[pi][pj] <= tj) G[i].push_back(j);\n }\n }\n \n vector<int> match(L, -1);\n vector<int> visited(L, false);\n \n auto dfs = [&](int from, auto&& self) -> bool {\n for (int to : G[from]) {\n if (visited[to]) continue;\n visited[to] = true;\n if (match[to] == -1 || self(match[to], self)) {\n match[to] = from;\n return true;\n }\n }\n return false;\n };\n \n int result = 0;\n for (int u = 0; u < L; u++) {\n visited.assign(L, false);\n if (dfs(u, dfs)) result++;\n }\n cout << L - result << endl;\n }\n \n return 0;\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 6144, "score_of_the_acc": -0.0284, "final_rank": 3 }, { "submission_id": "aoj_2251_10474616", "code_snippet": "#include <bits/stdc++.h>\n#include <atcoder/all>\nusing namespace atcoder;\nusing namespace std;\n#define all(...) std::begin(__VA_ARGS__), std::end(__VA_ARGS__)\n#define rall(...) std::rbegin(__VA_ARGS__), std::rend(__VA_ARGS__)\n#define OVERLOAD_REP(_1, _2, _3, _4, name, ...) name\n#define REP1(n) for(ll i=0;i<n;i++)\n#define REP2(i, n) for (ll i=0;i<n;i++)\n#define REP3(i, a, n) for (ll i=a;i<n;i++)\n#define REP4(i, a, b, n) for(ll i=a;i<n;i+=b)\n#define rep(...) OVERLOAD_REP(__VA_ARGS__, REP4, REP3, REP2, REP1)(__VA_ARGS__)\n#define OVERLOAD_RREP(_1, _2, _3, _4, name, ...) name\n#define RREP1(n) for(ll i=n;i>=0;i--)\n#define RREP2(i, n) for(ll i=n;i>=0;i--)\n#define RREP3(i, a, n) for(ll i=n;i>=a;i--)\n#define RREP4(i, a, b, n) for(ll i=n;i>=a;i-=b)\n#define rrep(...) OVERLOAD_RREP(__VA_ARGS__, RREP4, RREP3, RREP2, RREP1)(__VA_ARGS__)\n#define foa(a,v) (auto& a : (v))\n#define uniq(a) sort(all(a));a.erase(unique(all(a)),end(a))\n#define len(n) (long long)(n).size()\n#define pb push_back\nusing ll = long long;\nusing ld = long double;\nusing ull = unsigned long long;\nusing vi = vector<int>;\nusing vvi = vector<vi>;\nusing vvvi = vector<vvi>;\nusing vll = vector<ll>;\nusing vvll = vector<vll>;\nusing vvvll = vector<vvll>;\nusing vs = vector<string>;\nusing vvs = vector<vs>;\nusing vvvs = vector<vvs>;\nusing vld = vector<ld>;\nusing vvld = vector<vld>;\nusing vvvld = vector<vvld>;\nusing vc = vector<char>;\nusing vvc = vector<vc>;\nusing vvvc = vector<vvc>;\nusing pll = pair<ll,ll>;\nusing vpll = vector<pll>;\nusing vvpll = vector<vpll>;\ntemplate<class... T>\nconstexpr auto min(T... a){\n return min(initializer_list<common_type_t<T...>>{a...});\n}\ntemplate<class... T>\nconstexpr auto max(T... a){\n return max(initializer_list<common_type_t<T...>>{a...});\n}\ntemplate<class... T>\nvoid input(T&... a){\n (cin >> ... >> a);\n}\nll modpow(ll a,ll b,ll c){\n ll ans = 1;\n while (b){\n if (b & 1){\n ans *= a;\n ans %= c;\n }\n a *= a;\n a %= c;\n b /= 2;\n }\n return ans;\n}\n#define INT(...) int __VA_ARGS__; input(__VA_ARGS__)\n#define LL(...) ll __VA_ARGS__; input(__VA_ARGS__)\n#define ULL(...) ull __VA_ARGS__; input(__VA_ARGS__)\n#define LD(...) ld __VA_ARGS__; input(__VA_ARGS__)\n#define STR(...) string __VA_ARGS__; input(__VA_ARGS__)\n#define CHA(...) char __VA_ARGS__; input(__VA_ARGS__)\n#define VLL(name,length) vll name(length);rep(i,length){cin >> name[i];}\n#define VVLL(name,h,w) vvll name(h,vll(w));rep(i,h)rep(j,w){cin >> name[i][j];}\n#define VVVLL(name,a,b,c) vvvll name(a,vvll(b,vll(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}\n#define VI(name,length) vi name(length);rep(i,length){cin >> name[i];}\n#define VVI(name,h,w) vvi name(h,vi(w));rep(i,h)rep(j,w){cin >> name[i][j];}\n#define VVVI(name,a,b,c) vvvi name(a,vvll(b,vi(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}\n#define VLD(name,length) vld name(length);rep(i,length){cin >> name[i];}\n#define VVLD(name,h,w) vvld name(h,vld(w));rep(i,h)rep(j,w){cin >> name[i][j];}\n#define VVVLD(name,a,b,c) vvvld name(a,vvld(b,vld(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}\n#define VC(name,length) vc name(length);rep(i,length){cin >> name[i];}\n#define VVC(name,h,w) vvc name(h,vc(w));rep(i,h)rep(j,w){cin >> name[i][j];}\n#define VVVC(name,a,b,c) vvvc name(a,vvc(b,vc(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}\n#define VS(name,length) vs name(length);rep(i,length){cin >> name[i];}\n#define VVS(name,h,w) vvs name(h,vs(w));rep(i,h)rep(j,w){cin >> name[i][j];}\n#define VVVS(name,a,b,c) vvvs name(a,vvs(b,vs(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}\n#define PLL(name) pll name;cin>>name.first>>name.second;\n#define VPLL(name,length) vpll name(length);rep(i,length){cin>>name[i].first>>name[i].second;}\nusing mint = modint998244353;\nvoid print(){cout << \"\\n\";}\ntemplate<class T, class... Ts>\nvoid print(const T& a, const Ts&... b){cout << a;(cout << ... << (cout << ' ', b));cout << '\\n';}\nvoid print(vll x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << \" \";}else{cout << '\\n';}}}\nvoid print(vvi x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << \" \";}else{cout << '\\n';}}}\nvoid print(vector<mint> x){rep(i,len(x)){cout << x[i].val();if(i!=len(x)-1){cout << \" \";}else{cout << '\\n';}}}\n\n\nint main(){\n while(1){\n LL(n,m,l);\n if(n == 0){break;}\n vvpll edge(n);\n vvll dist(n,vll(n,1LL<<60));\n rep(i,n){\n dist[i][i] = 0;\n }\n rep(i,m){\n LL(u,v,d);\n edge[u].push_back({v,d});\n edge[v].push_back({u,d});\n dist[u][v] = min(dist[u][v],d);\n dist[v][u] = min(dist[v][u],d);\n }\n rep(k,n){\n rep(i,n){\n rep(j,n){\n dist[i][j] = min(dist[i][j],dist[i][k] + dist[k][j]);\n }\n }\n }\n VPLL(a,l);\n mf_graph<ll> g(2*l+2);\n ll s = 2*l;\n ll t = 2*l+1;\n rep(i,l){\n g.add_edge(s,i,1);\n g.add_edge(i+l,t,1);\n rep(j,l){\n if(i == j){continue;}\n if(a[i].second + dist[a[i].first][a[j].first] <= a[j].second){\n g.add_edge(i,j+l,1);\n }\n }\n }\n print(l - g.flow(s,t));\n\n }\n\n}", "accuracy": 1, "time_ms": 290, "memory_kb": 28284, "score_of_the_acc": -0.5313, "final_rank": 14 }, { "submission_id": "aoj_2251_10308239", "code_snippet": "// https://miscalc.hatenablog.com/entry/2022/11/11/213943#%E4%BE%8B%E9%A1%8C-2\n#include <iostream>\n#include <algorithm>\n#include <vector>\n\n#include <algorithm>\n\n#include <vector>\n\nnamespace atcoder {\n\nnamespace internal {\n\ntemplate <class T> struct simple_queue {\n std::vector<T> payload;\n int pos = 0;\n void reserve(int n) { payload.reserve(n); }\n int size() const { return int(payload.size()) - pos; }\n bool empty() const { return pos == int(payload.size()); }\n void push(const T& t) { payload.push_back(t); }\n T& front() { return payload[pos]; }\n void clear() {\n payload.clear();\n pos = 0;\n }\n void pop() { pos++; }\n};\n\n} // namespace internal\n\n} // namespace atcoder\n\n#include <cassert>\n#include <limits>\n#include <queue>\n#include <vector>\n\nnamespace atcoder {\n\ntemplate <class Cap> struct mf_graph {\n public:\n mf_graph() : _n(0) {}\n mf_graph(int n) : _n(n), g(n) {}\n\n int add_edge(int from, int to, Cap cap) {\n assert(0 <= from && from < _n);\n assert(0 <= to && to < _n);\n assert(0 <= cap);\n int m = int(pos.size());\n pos.push_back({from, int(g[from].size())});\n g[from].push_back(_edge{to, int(g[to].size()), cap});\n g[to].push_back(_edge{from, int(g[from].size()) - 1, 0});\n return m;\n }\n\n struct edge {\n int from, to;\n Cap cap, flow;\n };\n\n edge get_edge(int i) {\n int m = int(pos.size());\n assert(0 <= i && i < m);\n auto _e = g[pos[i].first][pos[i].second];\n auto _re = g[_e.to][_e.rev];\n return edge{pos[i].first, _e.to, _e.cap + _re.cap, _re.cap};\n }\n std::vector<edge> edges() {\n int m = int(pos.size());\n std::vector<edge> result;\n for (int i = 0; i < m; i++) {\n result.push_back(get_edge(i));\n }\n return result;\n }\n void change_edge(int i, Cap new_cap, Cap new_flow) {\n int m = int(pos.size());\n assert(0 <= i && i < m);\n assert(0 <= new_flow && new_flow <= new_cap);\n auto& _e = g[pos[i].first][pos[i].second];\n auto& _re = g[_e.to][_e.rev];\n _e.cap = new_cap - new_flow;\n _re.cap = new_flow;\n }\n\n Cap flow(int s, int t) {\n return flow(s, t, std::numeric_limits<Cap>::max());\n }\n Cap flow(int s, int t, Cap flow_limit) {\n assert(0 <= s && s < _n);\n assert(0 <= t && t < _n);\n\n std::vector<int> level(_n), iter(_n);\n internal::simple_queue<int> que;\n\n auto bfs = [&]() {\n std::fill(level.begin(), level.end(), -1);\n level[s] = 0;\n que.clear();\n que.push(s);\n while (!que.empty()) {\n int v = que.front();\n que.pop();\n for (auto e : g[v]) {\n if (e.cap == 0 || level[e.to] >= 0) continue;\n level[e.to] = level[v] + 1;\n if (e.to == t) return;\n que.push(e.to);\n }\n }\n };\n auto dfs = [&](auto self, int v, Cap up) {\n if (v == s) return up;\n Cap res = 0;\n int level_v = level[v];\n for (int& i = iter[v]; i < int(g[v].size()); i++) {\n _edge& e = g[v][i];\n if (level_v <= level[e.to] || g[e.to][e.rev].cap == 0) continue;\n Cap d =\n self(self, e.to, std::min(up - res, g[e.to][e.rev].cap));\n if (d <= 0) continue;\n g[v][i].cap += d;\n g[e.to][e.rev].cap -= d;\n res += d;\n if (res == up) break;\n }\n return res;\n };\n\n Cap flow = 0;\n while (flow < flow_limit) {\n bfs();\n if (level[t] == -1) break;\n std::fill(iter.begin(), iter.end(), 0);\n while (flow < flow_limit) {\n Cap f = dfs(dfs, t, flow_limit - flow);\n if (!f) break;\n flow += f;\n }\n }\n return flow;\n }\n\n std::vector<bool> min_cut(int s) {\n std::vector<bool> visited(_n);\n internal::simple_queue<int> que;\n que.push(s);\n while (!que.empty()) {\n int p = que.front();\n que.pop();\n visited[p] = true;\n for (auto e : g[p]) {\n if (e.cap && !visited[e.to]) {\n visited[e.to] = true;\n que.push(e.to);\n }\n }\n }\n return visited;\n }\n\n private:\n int _n;\n struct _edge {\n int to, rev;\n Cap cap;\n };\n std::vector<std::pair<int, int>> pos;\n std::vector<std::vector<_edge>> g;\n};\n\n} // namespace atcoder\n\n#define rep(i, n) for(i = 0; i < n; i++)\nusing namespace std;\nusing namespace atcoder;\ntypedef pair<int, int> P;\n\nconst int INF = 1e+9;\nint n, m, L;\nint dist[100][100];\n\nint main() {\n\tint i, j, k;\n\n\twhile (cin >> n >> m >> L) {\n\t\tif (!n) break;\n\t\trep(i, n) rep(j, n) dist[i][j] = INF;\n\t\trep(i, n) dist[i][i] = 0;\n\t\trep(i, m) {\n\t\t\tint u, v, d; cin >> u >> v >> d;\n\t\t\tdist[u][v] = dist[v][u] = d;\n\t\t}\n\t\trep(k, n) rep(i, n) rep(j, n) dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);\n\n\t\tvector<P> vec;\n\t\trep(i, L) {\n\t\t\tint p, t; cin >> p >> t; vec.push_back(P(t, p));\n\t\t}\n\t\tsort(vec.begin(), vec.end());\n\t\t\n\t\tmf_graph<int> g(2 * L + 2);\n\t\tint s = 2 * L, t = 2 * L + 1;\n\t\trep(i, L) g.add_edge(s, i, 1);\n\t\trep(i, L) g.add_edge(L + i, t, 1);\n\n\t\trep(i, L) {\n\t\t\tfor (j = i + 1; j < L; j++) {\n\t\t\t\tint dt = get<0>(vec[j]) - get<0>(vec[i]);\n\t\t\t\tint d = dist[get<1>(vec[j])][get<1>(vec[i])];\n\t\t\t\tif (d <= dt) {\n\t\t\t\t\tg.add_edge(i, L + j, 1);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\t\n\t\tint ans = L - g.flow(s, t);\n\t\tcout << ans << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 200, "memory_kb": 22788, "score_of_the_acc": -0.3827, "final_rank": 10 }, { "submission_id": "aoj_2251_9792547", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n/**\n * @brief template\n */\n\nvector<int> BipMatch(int n,int m,vector<vector<int>>& g){\n vector<int> L(n,-1),R(m,-1),d(n);\n queue<int> que;\n auto dfs=[&](auto& dfs,int v)->bool{\n int nd=exchange(d[v],0)+1;\n for(auto& u:g[v]){\n if(R[u]==-1 or (d[R[u]]==nd and dfs(dfs,R[u]))){\n L[v]=u,R[u]=v;\n return 1;\n }\n }\n return 0;\n };\n for(;;){\n d.assign(n,0);\n queue<int> dummy;\n swap(que,dummy);\n bool ch=0; \n rep(i,0,n)if(L[i]==-1){\n que.push(i);\n d[i]=1;\n }\n while(!que.empty()){\n int v=que.front();\n que.pop();\n for(auto& u:g[v]){\n if(R[u]==-1)ch=1;\n else if(!d[R[u]]){\n d[R[u]]=d[v]+1;\n que.push(R[u]);\n }\n }\n }\n if(!ch)break;\n rep(i,0,n)if(L[i]==-1)dfs(dfs,i);\n }\n return L;\n}\n\nint main() {\nwhile(1) {\n int N, M, L;\n cin >> N >> M >> L;\n if (N == 0) return 0;\n vector<vector<pair<int,int>>> G(N);\n rep(i,0,M) {\n int U, V, D;\n cin >> U >> V >> D;\n G[U].push_back({V,D});\n G[V].push_back({U,D});\n }\n vector<pair<int,int>> P(L);\n rep(i,0,L) {\n cin >> P[i].second >> P[i].first;\n }\n sort(ALL(P));\n auto Dijkstra = [&](int S) -> vector<int> {\n vector<int> DP(N,inf);\n DP[S] = 0;\n priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> PQ;\n PQ.push({0,S});\n while(!PQ.empty()) {\n auto [Di, Po] = PQ.top();\n PQ.pop();\n if (DP[Po] < Di) continue;\n for (auto E : G[Po]) {\n int NP = E.first, ND = Di+E.second;\n if (chmin(DP[NP],ND)) {\n PQ.push({ND,NP});\n }\n }\n }\n return DP;\n };\n vector<vector<int>> Dist(N);\n rep(i,0,N) Dist[i] = Dijkstra(i);\n vector<vector<int>> H(L);\n rep(i,0,L) {\n rep(j,i+1,L) {\n if (P[i].first + Dist[P[i].second][P[j].second] <= P[j].first) H[i].push_back(j);\n }\n }\n auto Ret = BipMatch(L,L,H);\n int ANS = 0;\n rep(i,0,L) if (Ret[i] == -1) ANS++;\n cout << ANS << endl;\n}\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 6048, "score_of_the_acc": -0.0134, "final_rank": 2 }, { "submission_id": "aoj_2251_9728379", "code_snippet": "#include <bits/stdc++.h>\n\n#include <algorithm>\n#include <cassert>\n#include <limits>\n#include <queue>\n#include <vector>\n\n\n#include <vector>\n\nnamespace atcoder {\n\nnamespace internal {\n\ntemplate <class T> struct simple_queue {\n std::vector<T> payload;\n int pos = 0;\n void reserve(int n) { payload.reserve(n); }\n int size() const { return int(payload.size()) - pos; }\n bool empty() const { return pos == int(payload.size()); }\n void push(const T& t) { payload.push_back(t); }\n T& front() { return payload[pos]; }\n void clear() {\n payload.clear();\n pos = 0;\n }\n void pop() { pos++; }\n};\n\n} // namespace internal\n\n} // namespace atcoder\n\n\nnamespace atcoder {\n\ntemplate <class Cap> struct mf_graph {\n public:\n mf_graph() : _n(0) {}\n explicit mf_graph(int n) : _n(n), g(n) {}\n\n int add_edge(int from, int to, Cap cap) {\n assert(0 <= from && from < _n);\n assert(0 <= to && to < _n);\n assert(0 <= cap);\n int m = int(pos.size());\n pos.push_back({from, int(g[from].size())});\n int from_id = int(g[from].size());\n int to_id = int(g[to].size());\n if (from == to) to_id++;\n g[from].push_back(_edge{to, to_id, cap});\n g[to].push_back(_edge{from, from_id, 0});\n return m;\n }\n\n struct edge {\n int from, to;\n Cap cap, flow;\n };\n\n edge get_edge(int i) {\n int m = int(pos.size());\n assert(0 <= i && i < m);\n auto _e = g[pos[i].first][pos[i].second];\n auto _re = g[_e.to][_e.rev];\n return edge{pos[i].first, _e.to, _e.cap + _re.cap, _re.cap};\n }\n std::vector<edge> edges() {\n int m = int(pos.size());\n std::vector<edge> result;\n for (int i = 0; i < m; i++) {\n result.push_back(get_edge(i));\n }\n return result;\n }\n void change_edge(int i, Cap new_cap, Cap new_flow) {\n int m = int(pos.size());\n assert(0 <= i && i < m);\n assert(0 <= new_flow && new_flow <= new_cap);\n auto& _e = g[pos[i].first][pos[i].second];\n auto& _re = g[_e.to][_e.rev];\n _e.cap = new_cap - new_flow;\n _re.cap = new_flow;\n }\n\n Cap flow(int s, int t) {\n return flow(s, t, std::numeric_limits<Cap>::max());\n }\n Cap flow(int s, int t, Cap flow_limit) {\n assert(0 <= s && s < _n);\n assert(0 <= t && t < _n);\n assert(s != t);\n\n std::vector<int> level(_n), iter(_n);\n internal::simple_queue<int> que;\n\n auto bfs = [&]() {\n std::fill(level.begin(), level.end(), -1);\n level[s] = 0;\n que.clear();\n que.push(s);\n while (!que.empty()) {\n int v = que.front();\n que.pop();\n for (auto e : g[v]) {\n if (e.cap == 0 || level[e.to] >= 0) continue;\n level[e.to] = level[v] + 1;\n if (e.to == t) return;\n que.push(e.to);\n }\n }\n };\n auto dfs = [&](auto self, int v, Cap up) {\n if (v == s) return up;\n Cap res = 0;\n int level_v = level[v];\n for (int& i = iter[v]; i < int(g[v].size()); i++) {\n _edge& e = g[v][i];\n if (level_v <= level[e.to] || g[e.to][e.rev].cap == 0) continue;\n Cap d =\n self(self, e.to, std::min(up - res, g[e.to][e.rev].cap));\n if (d <= 0) continue;\n g[v][i].cap += d;\n g[e.to][e.rev].cap -= d;\n res += d;\n if (res == up) return res;\n }\n level[v] = _n;\n return res;\n };\n\n Cap flow = 0;\n while (flow < flow_limit) {\n bfs();\n if (level[t] == -1) break;\n std::fill(iter.begin(), iter.end(), 0);\n Cap f = dfs(dfs, t, flow_limit - flow);\n if (!f) break;\n flow += f;\n }\n return flow;\n }\n\n std::vector<bool> min_cut(int s) {\n std::vector<bool> visited(_n);\n internal::simple_queue<int> que;\n que.push(s);\n while (!que.empty()) {\n int p = que.front();\n que.pop();\n visited[p] = true;\n for (auto e : g[p]) {\n if (e.cap && !visited[e.to]) {\n visited[e.to] = true;\n que.push(e.to);\n }\n }\n }\n return visited;\n }\n\n private:\n int _n;\n struct _edge {\n int to, rev;\n Cap cap;\n };\n std::vector<std::pair<int, int>> pos;\n std::vector<std::vector<_edge>> g;\n};\n\n} // namespace atcoder\n\nusing namespace std;\n\nbool solve() {\n int N, M, L;\n cin >> N >> M >> L;\n if (N == 0 && M == 0 && L == 0) return false;\n vector<vector<int>> dist(N, vector<int>(N, 1 << 29));\n for (int i = 0; i < N; i++) {\n dist[i][i] = 0;\n }\n for (int i = 0; i < M; i++) {\n int u, v, d;\n cin >> u >> v >> d;\n dist[u][v] = dist[v][u] = d;\n }\n for (int k = 0; k < N; k++) {\n for (int i = 0; i < N; i++) {\n for (int j = 0; j < N; j++) {\n dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);\n }\n }\n }\n vector<int> p(L), t(L);\n for (int i = 0; i < L; i++) cin >> p[i] >> t[i];\n atcoder::mf_graph<int> G(2 * L + 2);\n int S = 2 * L, T = 2 * L + 1;\n for (int i = 0; i < L; i++) {\n G.add_edge(S, i, 1);\n G.add_edge(i + L, T, 1);\n for (int j = 0; j < L; j++) {\n if (i != j && t[i] <= t[j] && t[j] - t[i] >= dist[p[i]][p[j]]) {\n G.add_edge(i, j + L, 1);\n }\n }\n }\n cout << L - G.flow(S, T) << endl;\n return true;\n}\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n while (solve()) {}\n}", "accuracy": 1, "time_ms": 360, "memory_kb": 23000, "score_of_the_acc": -0.4935, "final_rank": 13 }, { "submission_id": "aoj_2251_9453263", "code_snippet": "#include<bits/stdc++.h>\n\n#include <algorithm>\n\n#include <vector>\n\nnamespace atcoder {\n\nnamespace internal {\n\ntemplate <class T> struct simple_queue {\n std::vector<T> payload;\n int pos = 0;\n void reserve(int n) { payload.reserve(n); }\n int size() const { return int(payload.size()) - pos; }\n bool empty() const { return pos == int(payload.size()); }\n void push(const T& t) { payload.push_back(t); }\n T& front() { return payload[pos]; }\n void clear() {\n payload.clear();\n pos = 0;\n }\n void pop() { pos++; }\n};\n\n} // namespace internal\n\n} // namespace atcoder\n\n#include <cassert>\n#include <limits>\n#include <queue>\n#include <vector>\n\nnamespace atcoder {\n\ntemplate <class Cap> struct mf_graph {\n public:\n mf_graph() : _n(0) {}\n mf_graph(int n) : _n(n), g(n) {}\n\n int add_edge(int from, int to, Cap cap) {\n assert(0 <= from && from < _n);\n assert(0 <= to && to < _n);\n assert(0 <= cap);\n int m = int(pos.size());\n pos.push_back({from, int(g[from].size())});\n int from_id = int(g[from].size());\n int to_id = int(g[to].size());\n if (from == to) to_id++;\n g[from].push_back(_edge{to, to_id, cap});\n g[to].push_back(_edge{from, from_id, 0});\n return m;\n }\n\n struct edge {\n int from, to;\n Cap cap, flow;\n };\n\n edge get_edge(int i) {\n int m = int(pos.size());\n assert(0 <= i && i < m);\n auto _e = g[pos[i].first][pos[i].second];\n auto _re = g[_e.to][_e.rev];\n return edge{pos[i].first, _e.to, _e.cap + _re.cap, _re.cap};\n }\n std::vector<edge> edges() {\n int m = int(pos.size());\n std::vector<edge> result;\n for (int i = 0; i < m; i++) {\n result.push_back(get_edge(i));\n }\n return result;\n }\n void change_edge(int i, Cap new_cap, Cap new_flow) {\n int m = int(pos.size());\n assert(0 <= i && i < m);\n assert(0 <= new_flow && new_flow <= new_cap);\n auto& _e = g[pos[i].first][pos[i].second];\n auto& _re = g[_e.to][_e.rev];\n _e.cap = new_cap - new_flow;\n _re.cap = new_flow;\n }\n\n Cap flow(int s, int t) {\n return flow(s, t, std::numeric_limits<Cap>::max());\n }\n Cap flow(int s, int t, Cap flow_limit) {\n assert(0 <= s && s < _n);\n assert(0 <= t && t < _n);\n assert(s != t);\n\n std::vector<int> level(_n), iter(_n);\n internal::simple_queue<int> que;\n\n auto bfs = [&]() {\n std::fill(level.begin(), level.end(), -1);\n level[s] = 0;\n que.clear();\n que.push(s);\n while (!que.empty()) {\n int v = que.front();\n que.pop();\n for (auto e : g[v]) {\n if (e.cap == 0 || level[e.to] >= 0) continue;\n level[e.to] = level[v] + 1;\n if (e.to == t) return;\n que.push(e.to);\n }\n }\n };\n auto dfs = [&](auto self, int v, Cap up) {\n if (v == s) return up;\n Cap res = 0;\n int level_v = level[v];\n for (int& i = iter[v]; i < int(g[v].size()); i++) {\n _edge& e = g[v][i];\n if (level_v <= level[e.to] || g[e.to][e.rev].cap == 0) continue;\n Cap d =\n self(self, e.to, std::min(up - res, g[e.to][e.rev].cap));\n if (d <= 0) continue;\n g[v][i].cap += d;\n g[e.to][e.rev].cap -= d;\n res += d;\n if (res == up) break;\n }\n return res;\n };\n\n Cap flow = 0;\n while (flow < flow_limit) {\n bfs();\n if (level[t] == -1) break;\n std::fill(iter.begin(), iter.end(), 0);\n while (flow < flow_limit) {\n Cap f = dfs(dfs, t, flow_limit - flow);\n if (!f) break;\n flow += f;\n }\n }\n return flow;\n }\n\n std::vector<bool> min_cut(int s) {\n std::vector<bool> visited(_n);\n internal::simple_queue<int> que;\n que.push(s);\n while (!que.empty()) {\n int p = que.front();\n que.pop();\n visited[p] = true;\n for (auto e : g[p]) {\n if (e.cap && !visited[e.to]) {\n visited[e.to] = true;\n que.push(e.to);\n }\n }\n }\n return visited;\n }\n\n private:\n int _n;\n struct _edge {\n int to, rev;\n Cap cap;\n };\n std::vector<std::pair<int, int>> pos;\n std::vector<std::vector<_edge>> g;\n};\n\n} // namespace atcoder\n\nusing namespace atcoder;\nusing namespace std;\nusing ll=long long;\nusing vll=vector<ll>;\nusing vvll=vector<vll>;\n#define rep(i,n) for(ll i=0;i<ll(n);i++)\n\n\nvoid warshall_floyd(vvll &d) {\n\tint n=d.size();\n\tfor (int k = 0; k < n; k++) { // 経由する頂点\n\t\tfor (int i = 0; i < n; i++) { // 始点\n\t\t\tfor (int j = 0; j < n; j++) { // 終点\n\t\t\t\td[i][j] = min(d[i][j], d[i][k] + d[k][j]);\n\t\t\t}\n\t\t}\n\t}\n}\n\nvoid solve(ll N,ll M,ll L){\n vvll D(N,vll(N,1e18));\n rep(i,N)D[i][i]=0;\n rep(i,M){\n ll u,v,d;\n cin>>u>>v>>d;\n D[u][v]=d;\n D[v][u]=d;\n }\n warshall_floyd(D);\n mf_graph<ll> G(L*2+2);\n ll S=L*2;\n ll T=L*2+1;\n rep(i,L){\n G.add_edge(S,i,1);\n G.add_edge(i+L,T,1);\n }\n vll P(L),Q(L);\n rep(i,L){\n cin>>P[i]>>Q[i];\n rep(j,i){\n if(D[P[i]][P[j]]<=abs(Q[i]-Q[j])){\n if(Q[i]<Q[j]){\n G.add_edge(i,j+L,1);\n }\n else{\n G.add_edge(j,i+L,1);\n }\n }\n }\n }\n cout<<L-G.flow(S,T)<<endl;\n}\n\nint main(){\n ll N,M,L;\n while(cin>>N>>M>>L,N+M+L!=0)solve(N,M,L);\n}", "accuracy": 1, "time_ms": 390, "memory_kb": 27220, "score_of_the_acc": -0.5813, "final_rank": 15 }, { "submission_id": "aoj_2251_9453259", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing ll=long long;\nusing vll=vector<ll>;\nusing vvll=vector<vll>;\n#define rep(i,n) for(ll i=0;i<ll(n);i++)\n\n\ntemplate <class flow_t = long long>\nstruct FordFulkerson {\n // edge[e.rev][e.to] で逆辺参照\n struct edge {\n int to;\n int rev;\n flow_t cap;\n };\n\n int siz;\n vector<vector<edge>> graph;\n vector<bool> used;\n\n FordFulkerson(int n) : siz(n), graph(n), used(siz, false) {}\n\n void add_edge(int from, int to, flow_t cap) {\n assert(from < siz && to < siz);\n graph[from].emplace_back((edge){to, (int)graph[to].size(), cap});\n graph[to].emplace_back((edge){from, (int)graph[from].size() - 1, 0}); // -1 大事!\n }\n\n flow_t dfs(int v, int target, flow_t flow) {\n if (v == target) {\n return flow;\n }\n used[v] = true;\n\n for (auto &e : graph[v]) {\n if (e.cap > 0 && !used[e.to]) {\n flow_t f = dfs(e.to, target, min(flow, e.cap));\n if (f > 0) {\n e.cap -= f;\n graph[e.to][e.rev].cap += f;\n return f;\n }\n }\n }\n return 0;\n }\n\n // max flow on s -> t\n flow_t max_flow(int s, int t) {\n flow_t flow = 0;\n flow_t INF = 1e9; // INF の上限値に注意\n while (true) {\n used.assign(siz, false);\n flow_t f = dfs(s, t, INF);\n flow += f;\n\n if (f == 0) {\n return flow;\n }\n }\n // 絶対に到達しないはず\n return 0;\n }\n};\n\nvoid warshall_floyd(vvll &d) {\n\tint n=d.size();\n\tfor (int k = 0; k < n; k++) { // 経由する頂点\n\t\tfor (int i = 0; i < n; i++) { // 始点\n\t\t\tfor (int j = 0; j < n; j++) { // 終点\n\t\t\t\td[i][j] = min(d[i][j], d[i][k] + d[k][j]);\n\t\t\t}\n\t\t}\n\t}\n}\n\nvoid solve(ll N,ll M,ll L){\n vvll D(N,vll(N,1e18));\n rep(i,N)D[i][i]=0;\n rep(i,M){\n ll u,v,d;\n cin>>u>>v>>d;\n D[u][v]=d;\n D[v][u]=d;\n }\n warshall_floyd(D);\n FordFulkerson G(L*2+2);\n ll S=L*2;\n ll T=L*2+1;\n rep(i,L){\n G.add_edge(S,i,1);\n G.add_edge(i+L,T,1);\n }\n vll P(L),Q(L);\n rep(i,L){\n cin>>P[i]>>Q[i];\n rep(j,i){\n if(D[P[i]][P[j]]<=abs(Q[i]-Q[j])){\n if(Q[i]<Q[j]){\n G.add_edge(i,j+L,1);\n }\n else{\n G.add_edge(j,i+L,1);\n }\n }\n }\n }\n cout<<L-G.max_flow(S,T)<<endl;\n}\n\nint main(){\n ll N,M,L;\n while(cin>>N>>M>>L,N+M+L!=0)solve(N,M,L);\n}", "accuracy": 1, "time_ms": 1520, "memory_kb": 23200, "score_of_the_acc": -1.2752, "final_rank": 19 }, { "submission_id": "aoj_2251_9365477", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <map>\n#include <queue>\n#include <set>\nusing namespace std;\ntypedef long long ll;\n#define rep(i, n) for(int i = 0; i < (n); i++)\n\ntemplate<class T>\nusing vi = vector<T>;\n\ntemplate<class T>\nusing vii = vector<vi<T>>;\n\ntemplate<class T>\nusing viii = vector<vii<T>>;\n\n\nstruct Dinic {\nprivate:\n const long long inf = 1e18;\n vector<int> dist; //sからの距離\n vector<int> iter; //どこまで調べたか\npublic:\n struct edge {\n int to, rev; long long cap;\n edge(int t = 0, int r = 0, long long c = 0LL) : to(t), rev(r), cap(c) {}\n };\n\n int n;\n vector<vector<edge>> to;\n\n Dinic(int n_ = 1) : n(n_), to(n_), dist(n_), iter(n_) {}\n\n void addedge(int u, int v, long long cap) {//u->vにcapの容量\n int su = (int)to[u].size(), sv = (int)to[v].size();\n to[u].push_back({ v, sv, cap });\n to[v].push_back({ u, su, 0 });\n }\n\n long long dfs(int v, int t, long long f) { //t: 終点, f: flow\n if (v == t) return f;\n int sv = (int)to[v].size();\n for (int& i = iter[v]; i < sv; i++) {\n edge& nv = to[v][i];\n if (dist[nv.to] <= dist[v] || nv.cap == 0) continue; //here\n long long nf = dfs(nv.to, t, min(f, nv.cap));\n if (nf > 0) {\n nv.cap -= nf;\n to[nv.to][nv.rev].cap += nf;\n return nf;\n }\n }\n return 0;\n }\n\n void bfs(int s) {\n dist.assign(n, -1);\n queue<int> q;\n q.push(s);\n dist[s] = 0;\n while (!q.empty()) {\n int v = q.front();\n q.pop();\n for (auto nv : to[v]) {\n if (nv.cap > 0 && dist[nv.to] < 0) {\n dist[nv.to] = dist[v] + 1;\n q.push(nv.to);\n }\n }\n }\n }\n\n long long maxflow(int s, int t) {//s:始点, t:終点\n long long res = 0, flow = 0;\n while (true) {\n bfs(s);\n if (dist[t] < 0) break;\n\n iter.assign(n, 0);\n while (flow = dfs(s, t, inf)) res += flow;\n };\n return res;\n }\n};\n\nstruct edge {\n int to, cost;\n};\n\nint main()\n{\n vi<int> ans;\n const int inf = 1e9;\n int n, m, l;\n while (cin >> n >> m >> l, n) {\n vii<int> dist(n, vi<int>(n, inf));\n rep(i, n) dist[i][i] = 0;\n rep(i, m) {\n int u, v, d;\n cin >> u >> v >> d;\n dist[u][v] = d;\n dist[v][u] = d;\n }\n\n rep(k, n) rep(i, n) rep(j, n) dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);\n\n vi<int> p(l), t(l);\n rep(i, l) cin >> p[i] >> t[i];\n\n Dinic dn(2 * l + 2);\n int S = 2 * l, T = S + 1;\n\n rep(i, l) dn.addedge(S, i, 1);\n rep(j, l) dn.addedge(j + l, T, 1);\n\n rep(i, l) {\n rep(j, l) {\n if (i == j) continue;\n if (dist[p[i]][p[j]] == inf) continue;\n if (t[i] + dist[p[i]][p[j]] > t[j]) continue;\n dn.addedge(i, j + l, 1);\n }\n }\n\n int flow = dn.maxflow(S, T);\n ans.push_back(l - flow);\n }\n\n for (int elem : ans) cout << elem << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 520, "memory_kb": 23916, "score_of_the_acc": -0.6155, "final_rank": 16 }, { "submission_id": "aoj_2251_9347521", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define endl \"\\n\"\n#define bug(x) cerr << #x << \" = \" << x << endl\n#define sz(x) (int)x.size()\n#define mp make_pair\n#define LL long long\n\n#define INF 1000000007\n#define MX 2005\n#define NX 205\n#define NN 3005\n\nint n, m, Q;\npair<int, int> q[MX];\nint dis[NX][NX];\n\nint an, bn;\nint ud[NN];\nint ab[NN], ba[NN];\nint vis[NN], T;\nvector<int> adj[NN];\n\nvoid BFS(){\n\tqueue<int> q; while(!q.empty()) q.pop();\n\t\n\tfill_n(ud, an, -1);\n\tfor(int u = 0; u < an; u ++) if(ab[u] == -1){\n\t\tq.push(u); ud[u] = 0;\n\t}\n\t\n\twhile(!q.empty()){\n\t\tint u = q.front(); q.pop();\n\t\tfor(int v : adj[u]) if(ab[u] != v){\n\t\t\tint nu = ba[v];\n\t\t\tif(~nu && ud[nu] == -1){\n\t\t\t\tq.push(nu); ud[nu] = ud[u] + 1;\n\t\t\t}\n\t\t}\n\t}\n}\n\nint DFS(int u){\n\tvis[u] = T;\n\tfor(int v : adj[u]) if(ab[u] != v){\n\t\tint i = ba[v];\n\t\tif((i == -1) || (vis[i] != T && ud[i] == ud[u] + 1 && DFS(i))){\n\t\t\tba[v] = u; ab[u] = v;\n\t\t\treturn 1;\n\t\t}\n\t}\n\treturn 0;\n}\n\nint Hopcroft_Karp(){\n\t\n\tfill_n(ab, an, -1);\n\tfill_n(ba, bn, -1);\n\tfill_n(vis, an, 0);\n\t\n\tint rlt = 0;\n\tT = 0;\n\twhile(1){\n\t\tT ++;\n\t\tBFS();\n\t\tint f = 0;\n\t\tfor(int u = 0; u < an; u ++) if(ab[u] == -1 && DFS(u)) f ++;\n\t\tif(!f) break;\n\t\trlt += f;\n\t}\n\t\n\treturn rlt;\n}\n\nvoid solve(){\n\n\twhile( cin >> n >> m >> Q && (n || m || Q)){\n\t\t\n\t\tfill_n(dis[0], NX * NX, INF);\n\t\tfor(int i = 0; i < n; i ++) dis[i][i] = 0;\n\t\t\n\t\tfor(int i = 0; i < m; i ++){\n\t\t\tint u, v, d; cin >> u >> v >> d;\n\t\t\tdis[u][v] = min(dis[u][v], d);\n\t\t\tdis[v][u] = min(dis[v][u], d);\n\t\t}\n\t\tfor(int i = 0; i < Q; i ++) cin >> q[i].first >> q[i].second;\n\t\t\n\t\tfor(int k = 0; k < n; k ++){\n\t\t\tfor(int i = 0; i < n; i ++) for(int j = 0; j < n; j ++){\n\t\t\t\tdis[i][j] = min(dis[i][k] + dis[k][j], dis[i][j]);\n\t\t\t}\n\t\t}\n\t\t\n\t\tfor(int i = 0; i < Q; i ++) adj[i].clear();\n\t\tfor(int i = 0; i < Q; i ++) for(int j = 0; j < Q; j ++) if(i != j){\n\t\t\tif(dis[q[i].first][q[j].first] <= q[j].second - q[i].second) adj[i].push_back(j);\n\t\t}\n\t\tan = bn = Q;\n\t\t\n\t\tint M = Hopcroft_Karp();\n\t\t\n\t\tcout << Q - M << endl;\n\t}\n}\n\nint main(){\n\tios::sync_with_stdio(0);\n\tcin.tie(0), cout.tie(0);\n\t\n//\tfreopen(\"in.txt\", \"r\", stdin);\n\n\tint Tc = 1;\n//\tcin >> Tc;\n\twhile(Tc --){\n\t\tsolve();\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 180, "memory_kb": 7884, "score_of_the_acc": -0.1301, "final_rank": 5 }, { "submission_id": "aoj_2251_9347507", "code_snippet": "#include <iostream>\n#include <string>\n#include <vector>\n#include <queue>\n#include <algorithm>\nusing namespace std;\n\n#define sz(x) (int((x).size()))\nint n;\ntypedef vector<int> VI;\nconst int MX = 2005;\nint an, bn;\nVI adj[MX];\nint ab[MX], ba[MX];\nint dis[MX];\nint q[MX], hd, tl;\nint vis[MX], T;\n\nvoid BFS() {\n\tint i, u, v, uu;\n\thd = tl = 0;\n\tfor (u = 0; u < an; u++) {\n\t\tif (ab[u] == -1) {\n\t\t\tdis[u] = 0;\n\t\t\tq[tl++] = u;\n\t\t}\n\t\telse dis[u] = -1;\n\t}\n\twhile (hd < tl) {\n\t\tu = q[hd++];\n\t\tfor (i = sz(adj[u]) - 1; i >= 0; i--) {\n\t\t\tv = adj[u][i];\n\t\t\tuu = ba[v];\n\t\t\tif (~uu && dis[uu] == -1) {\n\t\t\t\tdis[uu] = dis[u] + 1;\n\t\t\t\tq[tl++] = uu;\n\t\t\t}\n\t\t}\n\t}\n}\n\nbool DFS(int u) {\n\tint i, v, uu;\n\tvis[u] = T;\n\tfor (i = sz(adj[u]) - 1; i >= 0; i--) {\n\t\tv = adj[u][i];\n\t\tuu = ba[v];\n\t\tif (uu == -1 || vis[uu] != T && dis[uu] == dis[u] + 1 && DFS(uu)) {\n\t\t\tab[u] = v, ba[v] = u;\n\t\t\treturn 1;\n\t\t}\n\t}\n\treturn 0;\n}\n\nint Hopcroft_Karp() {\n\tint u, k, m;\n\tfill_n(ab, an, -1);\n\tfill_n(ba, bn, -1);\n\tm = 0;\n\twhile (1) {\n\t\tBFS();\n\t\tT++;\n\t\tk = 0;\n\t\tfor (u = 0; u < an; u++) {\n\t\t\tif (ab[u] == -1 && DFS(u)) k++;\n\t\t}\n\t\tif (!k) break;\n\t\tm += k;\n\t}\n\treturn m;\n}\n\nint N, M, L;\nint d[1005][1005];\nconst int inf = 1e9 + 7;\nint p[MX], t[MX];\nvoid solve() {\n\twhile(cin >> N >> M >> L && (N + M + L)){\n\t\tint n = N;\n\t\tfor(int i = 0 ; i< N ; i ++) for(int j = 0 ; j < N; j++) d[i][j] = inf;\n\t\tfor(int i = 0 ; i < M; i ++){\n\t\t\tint u, v, w;\n\t\t\tcin >> u >> v>> w;\n\t\t\td[u][v] = min(d[u][v], w);\n\t\t\td[v][u] = min(d[v][u], w);\n\t\t}\n\t\tfor(int i = 0 ; i < n ; i ++) d[i][i] = 0;\n\t\tfor(int k = 0 ; k < n ; k ++) for(int i = 0 ; i < n ; i ++) for(int j = 0;j < n ; j ++) d[i][j] = min(d[i][k] + d[k][j] , d[i][j]);\n\t\t\n//\t\tfor(int i = 0 ; i < n ; i ++) {\n//\t\t\tfor(int j = 0; j < n ; j ++) cout << d[i][j] <<\" \";\n//\t\t\tcout << endl;\n//\t\t}\n\t\tfor(int i = 0 ; i < L; i ++ ){\n\t\t\tcin >> p[i] >> t[i];\n\t\t}\n\t\t\n\t\tfor(int i = 0 ; i < L; i ++) adj[i].clear();\n\t\t\n\t\tan = bn =L;\n\t\tfor(int i = 0 ; i < L; i ++){\n\t\t\tfor(int j = 0 ; j < L; j ++){\n\t\t\t\tif(i == j) continue;\n\t\t\t\tif(t[i] + d[p[i]][p[j]] <= t[j]) adj[i].push_back(j);\n\t\t\t}\n\t\t}\n\t\tcout << L - Hopcroft_Karp() << endl;\n\t}\n}\n\nint main() {\n//#ifndef ONLINE_JUDGE\n// freopen(\"in.txt\", \"r\", stdin);\n//#endif\n// ios::sync_with_stdio(0);\n// cin.tie(0), cout.tie(0);\n\n\tint Tc = 1;\n\n\twhile (Tc--) solve();\n\n return 0;\n}", "accuracy": 1, "time_ms": 170, "memory_kb": 8040, "score_of_the_acc": -0.1259, "final_rank": 4 }, { "submission_id": "aoj_2251_9346834", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\nint n;\nconst int inf = 1e9 + 7;\nconst int MN = 2e3 + 5;\n#define sz(x) (int((x).size()))\n\ntypedef vector<int> VI;\nconst int MX = 2005;\n\nint an, bn;\nvector<int> adj[MX];\nint ab[MX], ba[MX];\nint dis[MX];\nint q[MX], hd, tl;\nint vis[MX], T;\n\nvoid BFS() {\n\tint i, u, v, uu;\n\thd = tl = 0;\n\tfor (u = 0; u < an; u++) {\n\t\tif (ab[u] == -1) {\n\t\t\tdis[u] = 0;\n\t\t\tq[tl++] = u;\n\t\t}\n\t\telse dis[u] = -1;\n\t}\n\twhile (hd < tl) {\n\t\tu = q[hd++];\n\t\tfor (i = sz(adj[u]) - 1; i >= 0; i--) {\n\t\t\tv = adj[u][i];\n\t\t\tuu = ba[v];\n\t\t\tif (~uu && dis[uu] == -1) {\n\t\t\t\tdis[uu] = dis[u] + 1;\n\t\t\t\tq[tl++] = uu;\n\t\t\t}\n\t\t}\n\t}\n}\n\nbool DFS(int u) {\n\tint i, v, uu;\n\tvis[u] = T;\n\tfor (i = sz(adj[u]) - 1; i >= 0; i--) {\n\t\tv = adj[u][i];\n\t\tuu = ba[v];\n\t\tif (uu == -1 || vis[uu] != T && dis[uu] == dis[u] + 1 && DFS(uu)) {\n\t\t\tab[u] = v, ba[v] = u;\n\t\t\treturn 1;\n\t\t}\n\t}\n\treturn 0;\n}\n\nint Hopcroft_Karp() {\n\tint u, k, m;\n\tfill_n(ab, an, -1);\n\tfill_n(ba, bn, -1);\n//\tfill_n(vis, an, 0);\n\tm = 0;\n\twhile (1) {\n\t\tBFS();\n\t\tT++;\n\t\tk = 0;\n\t\tfor (u = 0; u < an; u++) {\n\t\t\tif (ab[u] == -1 && DFS(u)) k++;\n\t\t}\n\t\tif (!k) break;\n\t\tm += k;\n\t}\n\treturn m;\n}\n\n\ntemplate<class T>\ninline void chkmin(T& x, T y) {\n\tif (y < x) x = y;\n}\n\nint w[MN][MN], d[MN][MN];\n\nvoid Floyd_Warshall() {\n\tint i, j, k;\n\tfor (i = 0; i < n; i++) {\n\t\tfor (j = 0; j < n; j++) d[i][j] = w[i][j];\n\t}\n\tfor(int i = 0 ; i < n; i ++) d[i][i] = 0;\n\tfor (k = 0; k < n; k++) {\n\t\tfor (i = 0; i < n; i++) {\n\t\t\tfor (j = 0; j < n; j++)\n\t\t\t\tchkmin(d[i][j], d[i][k] + d[k][j]);\n\t\t}\n\t}\n}\n\nint p[MX], t[MX];\nvoid solve() {\n\tint N, M, L;\n\twhile(cin >> N >> M >> L && (N + M + L)){\n\t\tn = N;\n \t\tfor(int i = 0; i < n ; i ++){\n\t\t\tfor(int j = 0; j < n ; j++){\n\t\t\t\tw[i][j] = inf;\n\t\t\t\td[i][j] = inf;\n\t\t\t}\n\t\t}\n\t\tfor(int i = 0 ; i < M ; i ++){\n\t\t\tint u, v, ww;\n\t\t\tcin >> u >> v >> ww;\n\t\t\tchkmin(w[u][v], ww);\n\t\t\tchkmin(w[v][u], ww);\n\t\t}\n\t\tFloyd_Warshall();\n\t\tfor(int i = 0 ; i < L; i ++){\n\t\t\tcin >> p[i] >> t[i];\n\t\t}\n\t\tfor(int i = 0 ; i < L; i ++){\n\t\t\tadj[i].clear();\n\t\t}\n\t\tan = bn = L;\n\t\tfor(int i = 0 ; i < L; i ++){\n\t\t\tfor(int j = 0; j < L; j ++){\n\t\t\t\tif(i == j){\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tif(t[i] + d[p[i]][p[j]] <= t[j]){\n\t\t\t\t\tadj[i].push_back(j);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tcout << L - Hopcroft_Karp() << endl;\n\t}\n}\n\nint main() {\n#ifndef ONLINE_JUDGE\n//\tfreopen(\"in.txt\", \"r\", stdin);\n//\tfreopen(\"out.txt\", \"w\", stdout);\n#endif\n\tios::sync_with_stdio(0);\n\tcin.tie(0), cout.tie(0);\n\n\tint Tc = 1;\n//\tcin >> Tc;\n\twhile (Tc --) {\n\t\tsolve();\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 160, "memory_kb": 12252, "score_of_the_acc": -0.1868, "final_rank": 6 }, { "submission_id": "aoj_2251_8571653", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nconst int INF = (1<<29);\nint n, m, l, match[2001];\nvector<vector<int> > g;\nbool used[1001];\nbool dfs(int v){\n used[v] = true;\n for(int i=0;i<g[v].size();i++){\n int u = g[v][i];\n int w = match[u];\n if(w<0 || !used[w]&&dfs(w)){\n match[v] = u;\n match[u] = v;\n return true;\n }\n }\n return false;\n}\nint bm(){\n int res = 0;\n memset(match, -1, sizeof(match));\n for(int i=0;i<l;i++){\n if(match[i]<0){\n memset(used, false, sizeof(used));\n if(dfs(i)) res++;\n }\n }\n return res;\n}\nint main(){\n cin.tie(0);\n ios::sync_with_stdio(0);\n int u, v, d, mcost[101][101];\n while(cin >> n >> m >> l, n){\n for(int i=0;i<n;i++){\n for(int j=0;j<n;j++) mcost[i][j] = INF;\n mcost[i][i] = 0;\n }\n for(int i=0;i<m;i++){\n cin >> u >> v >> d;\n mcost[u][v] = d;\n mcost[v][u] = d;\n }\n for(int k=0;k<n;k++){\n for(int i=0;i<n;i++){\n for(int j=0;j<n;j++) mcost[i][j] = min(mcost[i][j], mcost[i][k]+mcost[k][j]);\n }\n }\n int p[1001], t[1001];\n vector<vector<int> > path(1001);\n for(int i=0;i<l;i++){\n cin >> p[i] >> t[i];\n for(int j=0;j<i;j++){\n int dis = mcost[p[j]][p[i]];\n if(t[j]+dis<=t[i]) path[j].push_back(i+l);\n if(t[i]+dis<=t[j]) path[i].push_back(j+l);\n }\n }\n g = path;\n cout << l-bm() << endl;\n }\n return(0);\n}", "accuracy": 1, "time_ms": 680, "memory_kb": 8356, "score_of_the_acc": -0.4733, "final_rank": 11 }, { "submission_id": "aoj_2251_8571647", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nconst int INF = (1<<29);\nint n, m, l, match[2001];\nvector<vector<int> > g;\nbool used[1001];\nbool dfs(int v){\n used[v] = true;\n for(int i=0;i<g[v].size();i++){\n int u = g[v][i];\n int w = match[u];\n if(w<0 || !used[w]&&dfs(w)){\n match[v] = u;\n match[u] = v;\n return true;\n }\n }\n return false;\n}\nint bm(){\n int res = 0;\n memset(match, -1, sizeof(match));\n for(int i=0;i<l;i++){\n if(match[i]<0){\n memset(used, false, sizeof(used));\n if(dfs(i)) res++;\n }\n }\n return res;\n}\nint main(){\n int u, v, d, mcost[101][101];\n while(cin >> n >> m >> l, n){\n for(int i=0;i<n;i++){\n for(int j=0;j<n;j++) mcost[i][j] = INF;\n mcost[i][i] = 0;\n }\n for(int i=0;i<m;i++){\n cin >> u >> v >> d;\n mcost[u][v] = d;\n mcost[v][u] = d;\n }\n for(int k=0;k<n;k++){\n for(int i=0;i<n;i++){\n for(int j=0;j<n;j++) mcost[i][j] = min(mcost[i][j], mcost[i][k]+mcost[k][j]);\n }\n }\n int p[1001], t[1001];\n vector<vector<int> > path(1001);\n for(int i=0;i<l;i++){\n cin >> p[i] >> t[i];\n for(int j=0;j<i;j++){\n int dis = mcost[p[j]][p[i]];\n if(t[j]+dis<=t[i]) path[j].push_back(i+l);\n if(t[i]+dis<=t[j]) path[i].push_back(j+l);\n }\n }\n g = path;\n cout << l-bm() << endl;\n }\n return(0);\n}", "accuracy": 1, "time_ms": 690, "memory_kb": 8316, "score_of_the_acc": -0.4793, "final_rank": 12 }, { "submission_id": "aoj_2251_8323741", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\n\nmt19937 engine(42);\n\ntemplate<class T>\nbool chmax(T &a, const T &b) {if (a < b) {a = b; return 1;} return 0;}\n\ntemplate<class T>\nbool chmin(T &a, const T &b) {if (a > b) {a = b; return 1;} return 0;}\n\nbool is_end = false;\nconst ll INF = 1e18;\n\nvoid warshall_floyd(vector<vector<ll>> &dist)\n{\n int n = dist.size();\n for (int i = 0; i < n; ++i) {dist[i][i] = 0;}\n \n for (int k = 0; k < n; ++k)\n {\n for (int i = 0; i < n; ++i)\n {\n for (int j = 0; j < n; ++j)\n {\n chmin(dist[i][j], dist[i][k] + dist[k][j]);\n }\n }\n }\n \n return;\n}\n\ntemplate<class flow_t = long long>\nstruct FordFulkerson\n{\n struct edge\n {\n int to;\n int rev;\n flow_t cap;\n };\n \n int siz;\n vector<vector<edge>> graph;\n vector<bool> used;\n \n FordFulkerson(int n) : siz(n), graph(n), used(siz, false) { }\n \n void add_edge(int from, int to, flow_t cap)\n {\n assert(from < siz && to < siz);\n graph[from].emplace_back((edge){to, (int)graph[to].size(), cap});\n graph[to].emplace_back((edge){from, (int)graph[from].size() - 1, 0});\n }\n \n flow_t dfs(int v, int target, flow_t flow)\n {\n if (v == target) return flow;\n used[v] = true;\n \n for (auto &e : graph[v])\n {\n if (e.cap > 0 && !used[e.to])\n {\n flow_t f = dfs(e.to, target, min(flow, e.cap));\n if (f > 0)\n {\n e.cap -= f;\n graph[e.to][e.rev].cap += f;\n return f;\n }\n }\n }\n return 0;\n }\n \n flow_t max_flow(int s, int t)\n {\n flow_t flow = 0;\n flow_t INF = 1e9;\n while (true)\n {\n used.assign(siz, false);\n flow_t f = dfs(s, t, INF);\n flow += f;\n \n if (f == 0) return flow;\n }\n return 0;\n }\n \n};\n\nvoid calc()\n{\n ll N, M, L; cin >> N >> M >> L;\n if (N == 0)\n {\n is_end = true;\n return;\n }\n \n vector<vector<ll>> dist(N, vector<ll>(N, INF));\n for (int i = 0; i < M; ++i)\n {\n ll u, v, d; cin >> u >> v >> d;\n dist[u][v] = dist[v][u] = d;\n }\n \n warshall_floyd(dist);\n \n vector<pair<ll, ll>> request(L);\n for (int i = 0; i < L; ++i)\n {\n ll p, t; cin >> p >> t;\n request[i] = {t, p};\n }\n sort(request.begin(), request.end());\n \n ll S = 2*L, T = S+1;\n FordFulkerson<ll> graph(T+1);\n for (int i = 0; i < L; ++i)\n {\n auto [ti, pi] = request[i];\n graph.add_edge(S, i, 1);\n graph.add_edge(i+L, T, 1);\n \n for (int j = i+1; j < L; ++j)\n {\n auto [tj, pj] = request[j];\n if (ti + dist[pi][pj] <= tj)\n {\n graph.add_edge(i, j+L, 1);\n }\n }\n }\n \n ll f = graph.max_flow(S, T);\n cout << L - f << endl;\n \n \n return;\n}\n\nint main() {\n \n while (!is_end)\n {\n calc();\n }\n \n return 0;\n}", "accuracy": 1, "time_ms": 510, "memory_kb": 24440, "score_of_the_acc": -0.6172, "final_rank": 17 }, { "submission_id": "aoj_2251_8314224", "code_snippet": "#line 1 \"a.cpp\"\n#define PROBLEM \"https://onlinejudge.u-aizu.ac.jp/problems/GRL_7_A\"\n#line 2 \"/home/kuhaku/home/github/algo/lib/template/template.hpp\"\n#pragma GCC target(\"sse4.2,avx2,bmi2\")\n#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"unroll-loops\")\n#include <bits/stdc++.h>\ntemplate <class T, class U>\nbool chmax(T &a, const U &b) {\n return a < (T)b ? a = (T)b, true : false;\n}\ntemplate <class T, class U>\nbool chmin(T &a, const U &b) {\n return (T)b < a ? a = (T)b, true : false;\n}\nconstexpr std::int64_t INF = 1000000000000000003;\nconstexpr int Inf = 1000000003;\nconstexpr int MOD = 1000000007;\nconstexpr int MOD_N = 998244353;\nconstexpr double EPS = 1e-7;\nconstexpr double PI = M_PI;\n#line 2 \"/home/kuhaku/home/github/algo/lib/flow/hopcroft_karp.hpp\"\n\nstruct hopcroft_karp {\n hopcroft_karp(int _n, int _m) : n(_n), m(_m), g(_n), match_left(_n, -1), match_right(_m, -1) {}\n\n void add_edge(int u, int v) {\n assert(0 <= u && u < n);\n assert(0 <= v && v < m);\n g[u].emplace_back(v);\n }\n\n int matching() {\n int flow = 0;\n std::vector<int> root(n), prev(n), qq(n);\n for (bool updated = true; updated;) {\n updated = false;\n int qi = 0, qj = 0;\n std::fill(root.begin(), root.end(), -1);\n std::fill(prev.begin(), prev.end(), -1);\n for (int i = 0; i < n; i++) {\n if (match_left[i] == -1) qq[qj++] = i, root[i] = i, prev[i] = i;\n }\n while (qi < qj) {\n int u = qq[qi++];\n if (match_left[root[u]] != -1) continue;\n for (int v : g[u]) {\n if (match_right[v] == -1) {\n while (v != -1)\n match_right[v] = u, std::swap(match_left[u], v), u = prev[u];\n updated = true, flow++;\n break;\n }\n\n if (prev[match_right[v]] == -1)\n v = match_right[v], prev[v] = u, root[v] = root[u], qq[qj++] = v;\n }\n }\n }\n return flow;\n }\n\n std::vector<std::pair<int, int>> get_pairs() const {\n std::vector<std::pair<int, int>> res;\n for (int i = 0; i < n; i++) {\n if (~match_left[i]) res.emplace_back(i, match_left[i]);\n }\n return res;\n }\n\n private:\n const int n, m;\n std::vector<std::vector<int>> g;\n std::vector<int> match_left, match_right;\n};\n#line 3 \"/home/kuhaku/home/github/algo/lib/graph/matrix_graph.hpp\"\n\n/**\n * @brief 隣接行列\n *\n * @tparam T\n */\ntemplate <class T>\nstruct matrix_graph {\n matrix_graph(int v, T e = T()) : _size(v), m(v, std::vector<T>(v, e)) {}\n\n const auto &operator[](int i) const { return this->m[i]; }\n auto &operator[](int i) { return this->m[i]; }\n const auto begin() const { return this->m.begin(); }\n auto begin() { return this->m.begin(); }\n const auto end() const { return this->m.end(); }\n auto end() { return this->m.end(); }\n\n constexpr int size() const { return this->_size; }\n\n void add_edge(int a, int b, T d = T(1)) { this->m[a][b] = d; }\n void add_edges(int a, int b, T d = T(1)) { this->m[a][b] = this->m[b][a] = d; }\n\n void input_edge(int m, int base = 1) {\n for (int i = 0; i < m; ++i) {\n int from, to;\n T weight;\n std::cin >> from >> to >> weight;\n this->add_edge(from - base, to - base, weight);\n }\n }\n void input_edges(int m, int base = 1) {\n for (int i = 0; i < m; ++i) {\n int from, to;\n T weight;\n std::cin >> from >> to >> weight;\n this->add_edges(from - base, to - base, weight);\n }\n }\n\n private:\n int _size;\n std::vector<std::vector<T>> m;\n};\n\ntemplate <>\nstruct matrix_graph<void> {\n matrix_graph(int v) : _size(v), m(v, std::vector<bool>(v)) {}\n\n const auto &operator[](int i) const { return this->m[i]; }\n auto &operator[](int i) { return this->m[i]; }\n const auto begin() const { return this->m.begin(); }\n auto begin() { return this->m.begin(); }\n const auto end() const { return this->m.end(); }\n auto end() { return this->m.end(); }\n\n constexpr int size() const { return this->_size; }\n\n void add_edge(int a, int b) { this->m[a][b] = true; }\n void add_edges(int a, int b) { this->m[a][b] = this->m[b][a] = true; }\n\n void input_edge(int m, int base = 1) {\n for (int i = 0; i < m; ++i) {\n int from, to;\n std::cin >> from >> to;\n this->add_edge(from - base, to - base);\n }\n }\n void input_edges(int m, int base = 1) {\n for (int i = 0; i < m; ++i) {\n int from, to;\n std::cin >> from >> to;\n this->add_edges(from - base, to - base);\n }\n }\n\n void complement() {\n for (int i = 0; i < this->_size; ++i) {\n for (int j = 0; j < this->_size; ++j) this->m[i][j] = !this->m[i][j];\n }\n }\n\n private:\n int _size;\n std::vector<std::vector<bool>> m;\n};\n#line 4 \"/home/kuhaku/home/github/algo/lib/graph/warshall_floyd.hpp\"\n\ntemplate <class T>\nvoid warshall_floyd(matrix_graph<T> &g) {\n int n = g.size();\n for (int i = 0; i < n; ++i) g[i][i] = T();\n for (int k = 0; k < n; ++k) {\n for (int i = 0; i < n; ++i) {\n for (int j = 0; j < n; ++j) {\n chmin(g[i][j], g[i][k] + g[k][j]);\n }\n }\n }\n}\n#line 3 \"/home/kuhaku/home/github/algo/lib/template/macro.hpp\"\n#define FOR(i, m, n) for (int i = (m); i < int(n); ++i)\n#define FORR(i, m, n) for (int i = (m)-1; i >= int(n); --i)\n#define FORL(i, m, n) for (int64_t i = (m); i < int64_t(n); ++i)\n#define rep(i, n) FOR (i, 0, n)\n#define repn(i, n) FOR (i, 1, n + 1)\n#define repr(i, n) FORR (i, n, 0)\n#define repnr(i, n) FORR (i, n + 1, 1)\n#define all(s) (s).begin(), (s).end()\n#line 3 \"/home/kuhaku/home/github/algo/lib/template/sonic.hpp\"\nstruct Sonic {\n Sonic() {\n std::ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n }\n\n constexpr void operator()() const {}\n} sonic;\n#line 5 \"/home/kuhaku/home/github/algo/lib/template/atcoder.hpp\"\nusing namespace std;\nusing ll = std::int64_t;\nusing ld = long double;\ntemplate <class T, class U>\nstd::istream &operator>>(std::istream &is, std::pair<T, U> &p) {\n return is >> p.first >> p.second;\n}\ntemplate <class T>\nstd::istream &operator>>(std::istream &is, std::vector<T> &v) {\n for (T &i : v) is >> i;\n return is;\n}\ntemplate <class T, class U>\nstd::ostream &operator<<(std::ostream &os, const std::pair<T, U> &p) {\n return os << '(' << p.first << ',' << p.second << ')';\n}\ntemplate <class T>\nstd::ostream &operator<<(std::ostream &os, const std::vector<T> &v) {\n for (auto it = v.begin(); it != v.end(); ++it) {\n os << (it == v.begin() ? \"\" : \" \") << *it;\n }\n return os;\n}\ntemplate <class Head, class... Tail>\nvoid co(Head &&head, Tail &&...tail) {\n if constexpr (sizeof...(tail) == 0) std::cout << head << '\\n';\n else std::cout << head << ' ', co(std::forward<Tail>(tail)...);\n}\ntemplate <class Head, class... Tail>\nvoid ce(Head &&head, Tail &&...tail) {\n if constexpr (sizeof...(tail) == 0) std::cerr << head << '\\n';\n else std::cerr << head << ' ', ce(std::forward<Tail>(tail)...);\n}\ntemplate <typename T, typename... Args>\nauto make_vector(T x, int arg, Args... args) {\n if constexpr (sizeof...(args) == 0) return std::vector<T>(arg, x);\n else return std::vector(arg, make_vector<T>(x, args...));\n}\nvoid setp(int n) {\n std::cout << std::fixed << std::setprecision(n);\n}\nvoid Yes(bool is_correct = true) {\n std::cout << (is_correct ? \"Yes\" : \"No\") << '\\n';\n}\nvoid No(bool is_not_correct = true) {\n Yes(!is_not_correct);\n}\nvoid YES(bool is_correct = true) {\n std::cout << (is_correct ? \"YES\" : \"NO\") << '\\n';\n}\nvoid NO(bool is_not_correct = true) {\n YES(!is_not_correct);\n}\nvoid Takahashi(bool is_correct = true) {\n std::cout << (is_correct ? \"Takahashi\" : \"Aoki\") << '\\n';\n}\nvoid Aoki(bool is_not_correct = true) {\n Takahashi(!is_not_correct);\n}\n#line 6 \"a.cpp\"\n\nint main(void) {\n while (true) {\n int n, m, l;\n cin >> n >> m >> l;\n if (!n)\n break;\n matrix_graph<int> g(n, Inf);\n while (m--) {\n int u, v, w;\n cin >> u >> v >> w;\n g.add_edges(u, v, w);\n }\n warshall_floyd(g);\n vector<pair<int, int>> q(l);\n cin >> q;\n sort(all(q), [](auto l, auto r) {\n return l.second < r.second;\n });\n hopcroft_karp hk(l, l);\n rep (i, l) {\n auto [a, b] = q[i];\n FOR (j, i + 1, l) {\n auto [c, d] = q[j];\n if (b + g[a][c] <= d)\n hk.add_edge(i, j);\n }\n }\n co(l - hk.matching());\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 6148, "score_of_the_acc": -0.0016, "final_rank": 1 }, { "submission_id": "aoj_2251_8288915", "code_snippet": "#line 1 \"main.cpp\"\n#define PROBLEM \"https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2251\"\n#line 1 \"library/my_template.hpp\"\n#if defined(LOCAL)\n#include <my_template_compiled.hpp>\n#else\n#pragma GCC optimize(\"Ofast\")\n#pragma GCC optimize(\"unroll-loops\")\n\n#include <bits/stdc++.h>\n\nusing namespace std;\n\nusing ll = long long;\nusing u32 = unsigned int;\nusing u64 = unsigned long long;\nusing i128 = __int128;\nusing u128 = unsigned __int128;\nusing f128 = __float128;\n\ntemplate <class T>\nconstexpr T infty = 0;\ntemplate <>\nconstexpr int infty<int> = 1'000'000'000;\ntemplate <>\nconstexpr ll infty<ll> = ll(infty<int>) * infty<int> * 2;\ntemplate <>\nconstexpr u32 infty<u32> = infty<int>;\ntemplate <>\nconstexpr u64 infty<u64> = infty<ll>;\ntemplate <>\nconstexpr i128 infty<i128> = i128(infty<ll>) * infty<ll>;\ntemplate <>\nconstexpr double infty<double> = infty<ll>;\ntemplate <>\nconstexpr long double infty<long double> = infty<ll>;\n\nusing pi = pair<ll, ll>;\nusing vi = vector<ll>;\ntemplate <class T>\nusing vc = vector<T>;\ntemplate <class T>\nusing vvc = vector<vc<T>>;\ntemplate <class T>\nusing vvvc = vector<vvc<T>>;\ntemplate <class T>\nusing vvvvc = vector<vvvc<T>>;\ntemplate <class T>\nusing vvvvvc = vector<vvvvc<T>>;\ntemplate <class T>\nusing pq = priority_queue<T>;\ntemplate <class T>\nusing pqg = priority_queue<T, vector<T>, greater<T>>;\n\n#define vv(type, name, h, ...) \\\n vector<vector<type>> name(h, vector<type>(__VA_ARGS__))\n#define vvv(type, name, h, w, ...) \\\n vector<vector<vector<type>>> name( \\\n h, vector<vector<type>>(w, vector<type>(__VA_ARGS__)))\n#define vvvv(type, name, a, b, c, ...) \\\n vector<vector<vector<vector<type>>>> name( \\\n a, vector<vector<vector<type>>>( \\\n b, vector<vector<type>>(c, vector<type>(__VA_ARGS__))))\n\n// https://trap.jp/post/1224/\n#define FOR1(a) for (ll _ = 0; _ < ll(a); ++_)\n#define FOR2(i, a) for (ll i = 0; i < ll(a); ++i)\n#define FOR3(i, a, b) for (ll i = a; i < ll(b); ++i)\n#define FOR4(i, a, b, c) for (ll i = a; i < ll(b); i += (c))\n#define FOR1_R(a) for (ll i = (a)-1; i >= ll(0); --i)\n#define FOR2_R(i, a) for (ll i = (a)-1; i >= ll(0); --i)\n#define FOR3_R(i, a, b) for (ll i = (b)-1; i >= ll(a); --i)\n#define overload4(a, b, c, d, e, ...) e\n#define overload3(a, b, c, d, ...) d\n#define FOR(...) overload4(__VA_ARGS__, FOR4, FOR3, FOR2, FOR1)(__VA_ARGS__)\n#define FOR_R(...) overload3(__VA_ARGS__, FOR3_R, FOR2_R, FOR1_R)(__VA_ARGS__)\n\n#define FOR_subset(t, s) \\\n for (ll t = (s); t >= 0; t = (t == 0 ? -1 : (t - 1) & (s)))\n#define all(x) x.begin(), x.end()\n#define len(x) ll(x.size())\n#define elif else if\n\n#define eb emplace_back\n#define mp make_pair\n#define mt make_tuple\n#define fi first\n#define se second\n\n#define stoi stoll\n\nint popcnt(int x) { return __builtin_popcount(x); }\nint popcnt(u32 x) { return __builtin_popcount(x); }\nint popcnt(ll x) { return __builtin_popcountll(x); }\nint popcnt(u64 x) { return __builtin_popcountll(x); }\nint popcnt_mod_2(int x) { return __builtin_parity(x); }\nint popcnt_mod_2(u32 x) { return __builtin_parity(x); }\nint popcnt_mod_2(ll x) { return __builtin_parityll(x); }\nint popcnt_mod_2(u64 x) { return __builtin_parityll(x); }\n// (0, 1, 2, 3, 4) -> (-1, 0, 1, 1, 2)\nint topbit(int x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); }\nint topbit(u32 x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); }\nint topbit(ll x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }\nint topbit(u64 x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }\n// (0, 1, 2, 3, 4) -> (-1, 0, 1, 0, 2)\nint lowbit(int x) { return (x == 0 ? -1 : __builtin_ctz(x)); }\nint lowbit(u32 x) { return (x == 0 ? -1 : __builtin_ctz(x)); }\nint lowbit(ll x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }\nint lowbit(u64 x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }\n\ntemplate <typename T, typename U>\nT ceil(T x, U y) {\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U>\nT floor(T x, U y) {\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T, typename U>\npair<T, T> divmod(T x, U y) {\n T q = floor(x, y);\n return {q, x - q * y};\n}\n\ntemplate <typename T, typename U>\nT SUM(const vector<U> &A) {\n T sum = 0;\n for (auto &&a: A) sum += a;\n return sum;\n}\n\n#define MIN(v) *min_element(all(v))\n#define MAX(v) *max_element(all(v))\n#define LB(c, x) distance((c).begin(), lower_bound(all(c), (x)))\n#define UB(c, x) distance((c).begin(), upper_bound(all(c), (x)))\n#define UNIQUE(x) \\\n sort(all(x)), x.erase(unique(all(x)), x.end()), x.shrink_to_fit()\n\ntemplate <typename T>\nT POP(deque<T> &que) {\n T a = que.front();\n que.pop_front();\n return a;\n}\ntemplate <typename T>\nT POP(pq<T> &que) {\n T a = que.top();\n que.pop();\n return a;\n}\ntemplate <typename T>\nT POP(pqg<T> &que) {\n assert(!que.empty());\n T a = que.top();\n que.pop();\n return a;\n}\ntemplate <typename T>\nT POP(vc<T> &que) {\n assert(!que.empty());\n T a = que.back();\n que.pop_back();\n return a;\n}\n\ntemplate <typename F>\nll binary_search(F check, ll ok, ll ng, bool check_ok = true) {\n if (check_ok) assert(check(ok));\n while (abs(ok - ng) > 1) {\n auto x = (ng + ok) / 2;\n tie(ok, ng) = (check(x) ? mp(x, ng) : mp(ok, x));\n }\n return ok;\n}\ntemplate <typename F>\ndouble binary_search_real(F check, double ok, double ng, int iter = 100) {\n FOR(iter) {\n double x = (ok + ng) / 2;\n tie(ok, ng) = (check(x) ? mp(x, ng) : mp(ok, x));\n }\n return (ok + ng) / 2;\n}\n\ntemplate <class T, class S>\ninline bool chmax(T &a, const S &b) {\n return (a < b ? a = b, 1 : 0);\n}\ntemplate <class T, class S>\ninline bool chmin(T &a, const S &b) {\n return (a > b ? a = b, 1 : 0);\n}\n\n// ? は -1\nvc<int> s_to_vi(const string &S, char first_char) {\n vc<int> A(S.size());\n FOR(i, S.size()) { A[i] = (S[i] != '?' ? S[i] - first_char : -1); }\n return A;\n}\n\ntemplate <typename T, typename U>\nvector<T> cumsum(vector<U> &A, int off = 1) {\n int N = A.size();\n vector<T> B(N + 1);\n FOR(i, N) { B[i + 1] = B[i] + A[i]; }\n if (off == 0) B.erase(B.begin());\n return B;\n}\n\n// stable sort\ntemplate <typename T>\nvector<int> argsort(const vector<T> &A) {\n vector<int> ids(len(A));\n iota(all(ids), 0);\n sort(all(ids),\n [&](int i, int j) { return (A[i] == A[j] ? i < j : A[i] < A[j]); });\n return ids;\n}\n\n// A[I[0]], A[I[1]], ...\ntemplate <typename T>\nvc<T> rearrange(const vc<T> &A, const vc<int> &I) {\n vc<T> B(len(I));\n FOR(i, len(I)) B[i] = A[I[i]];\n return B;\n}\n#endif\n#line 1 \"library/other/io.hpp\"\n// based on yosupo's fastio\n#include <unistd.h>\n\nnamespace fastio {\n#define FASTIO\n// クラスが read(), print() を持っているかを判定するメタ関数\nstruct has_write_impl {\n template <class T>\n static auto check(T &&x) -> decltype(x.write(), std::true_type{});\n\n template <class T>\n static auto check(...) -> std::false_type;\n};\n\ntemplate <class T>\nclass has_write : public decltype(has_write_impl::check<T>(std::declval<T>())) {\n};\n\nstruct has_read_impl {\n template <class T>\n static auto check(T &&x) -> decltype(x.read(), std::true_type{});\n\n template <class T>\n static auto check(...) -> std::false_type;\n};\n\ntemplate <class T>\nclass has_read : public decltype(has_read_impl::check<T>(std::declval<T>())) {};\n\nstruct Scanner {\n FILE *fp;\n char line[(1 << 15) + 1];\n size_t st = 0, ed = 0;\n void reread() {\n memmove(line, line + st, ed - st);\n ed -= st;\n st = 0;\n ed += fread(line + ed, 1, (1 << 15) - ed, fp);\n line[ed] = '\\0';\n }\n bool succ() {\n while (true) {\n if (st == ed) {\n reread();\n if (st == ed) return false;\n }\n while (st != ed && isspace(line[st])) st++;\n if (st != ed) break;\n }\n if (ed - st <= 50) {\n bool sep = false;\n for (size_t i = st; i < ed; i++) {\n if (isspace(line[i])) {\n sep = true;\n break;\n }\n }\n if (!sep) reread();\n }\n return true;\n }\n template <class T, enable_if_t<is_same<T, string>::value, int> = 0>\n bool read_single(T &ref) {\n if (!succ()) return false;\n while (true) {\n size_t sz = 0;\n while (st + sz < ed && !isspace(line[st + sz])) sz++;\n ref.append(line + st, sz);\n st += sz;\n if (!sz || st != ed) break;\n reread();\n }\n return true;\n }\n template <class T, enable_if_t<is_integral<T>::value, int> = 0>\n bool read_single(T &ref) {\n if (!succ()) return false;\n bool neg = false;\n if (line[st] == '-') {\n neg = true;\n st++;\n }\n ref = T(0);\n while (isdigit(line[st])) { ref = 10 * ref + (line[st++] & 0xf); }\n if (neg) ref = -ref;\n return true;\n }\n template <typename T,\n typename enable_if<has_read<T>::value>::type * = nullptr>\n inline bool read_single(T &x) {\n x.read();\n return true;\n }\n bool read_single(double &ref) {\n string s;\n if (!read_single(s)) return false;\n ref = std::stod(s);\n return true;\n }\n bool read_single(char &ref) {\n string s;\n if (!read_single(s) || s.size() != 1) return false;\n ref = s[0];\n return true;\n }\n template <class T>\n bool read_single(vector<T> &ref) {\n for (auto &d: ref) {\n if (!read_single(d)) return false;\n }\n return true;\n }\n template <class T, class U>\n bool read_single(pair<T, U> &p) {\n return (read_single(p.first) && read_single(p.second));\n }\n template <size_t N = 0, typename T>\n void read_single_tuple(T &t) {\n if constexpr (N < std::tuple_size<T>::value) {\n auto &x = std::get<N>(t);\n read_single(x);\n read_single_tuple<N + 1>(t);\n }\n }\n template <class... T>\n bool read_single(tuple<T...> &tpl) {\n read_single_tuple(tpl);\n return true;\n }\n void read() {}\n template <class H, class... T>\n void read(H &h, T &... t) {\n bool f = read_single(h);\n assert(f);\n read(t...);\n }\n Scanner(FILE *fp) : fp(fp) {}\n};\n\nstruct Printer {\n Printer(FILE *_fp) : fp(_fp) {}\n ~Printer() { flush(); }\n\n static constexpr size_t SIZE = 1 << 15;\n FILE *fp;\n char line[SIZE], small[50];\n size_t pos = 0;\n void flush() {\n fwrite(line, 1, pos, fp);\n pos = 0;\n }\n void write(const char val) {\n if (pos == SIZE) flush();\n line[pos++] = val;\n }\n template <class T, enable_if_t<is_integral<T>::value, int> = 0>\n void write(T val) {\n if (pos > (1 << 15) - 50) flush();\n if (val == 0) {\n write('0');\n return;\n }\n if (val < 0) {\n write('-');\n val = -val; // todo min\n }\n size_t len = 0;\n while (val) {\n small[len++] = char(0x30 | (val % 10));\n val /= 10;\n }\n for (size_t i = 0; i < len; i++) { line[pos + i] = small[len - 1 - i]; }\n pos += len;\n }\n void write(const string s) {\n for (char c: s) write(c);\n }\n void write(const char *s) {\n size_t len = strlen(s);\n for (size_t i = 0; i < len; i++) write(s[i]);\n }\n void write(const double x) {\n ostringstream oss;\n oss << fixed << setprecision(15) << x;\n string s = oss.str();\n write(s);\n }\n void write(const long double x) {\n ostringstream oss;\n oss << fixed << setprecision(15) << x;\n string s = oss.str();\n write(s);\n }\n template <typename T,\n typename enable_if<has_write<T>::value>::type * = nullptr>\n inline void write(T x) {\n x.write();\n }\n template <class T>\n void write(const vector<T> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i) write(' ');\n write(val[i]);\n }\n }\n template <class T, class U>\n void write(const pair<T, U> val) {\n write(val.first);\n write(' ');\n write(val.second);\n }\n template <size_t N = 0, typename T>\n void write_tuple(const T t) {\n if constexpr (N < std::tuple_size<T>::value) {\n if constexpr (N > 0) { write(' '); }\n const auto x = std::get<N>(t);\n write(x);\n write_tuple<N + 1>(t);\n }\n }\n template <class... T>\n bool write(tuple<T...> tpl) {\n write_tuple(tpl);\n return true;\n }\n template <class T, size_t S>\n void write(const array<T, S> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i) write(' ');\n write(val[i]);\n }\n }\n void write(i128 val) {\n string s;\n bool negative = 0;\n if (val < 0) {\n negative = 1;\n val = -val;\n }\n while (val) {\n s += '0' + int(val % 10);\n val /= 10;\n }\n if (negative) s += \"-\";\n reverse(all(s));\n if (len(s) == 0) s = \"0\";\n write(s);\n }\n};\nScanner scanner = Scanner(stdin);\nPrinter printer = Printer(stdout);\nvoid flush() { printer.flush(); }\nvoid print() { printer.write('\\n'); }\ntemplate <class Head, class... Tail>\nvoid print(Head &&head, Tail &&... tail) {\n printer.write(head);\n if (sizeof...(Tail)) printer.write(' ');\n print(forward<Tail>(tail)...);\n}\n\nvoid read() {}\ntemplate <class Head, class... Tail>\nvoid read(Head &head, Tail &... tail) {\n scanner.read(head);\n read(tail...);\n}\n} // namespace fastio\nusing fastio::print;\nusing fastio::flush;\nusing fastio::read;\n\n#define INT(...) \\\n int __VA_ARGS__; \\\n read(__VA_ARGS__)\n#define LL(...) \\\n ll __VA_ARGS__; \\\n read(__VA_ARGS__)\n#define STR(...) \\\n string __VA_ARGS__; \\\n read(__VA_ARGS__)\n#define CHAR(...) \\\n char __VA_ARGS__; \\\n read(__VA_ARGS__)\n#define DBL(...) \\\n double __VA_ARGS__; \\\n read(__VA_ARGS__)\n\n#define VEC(type, name, size) \\\n vector<type> name(size); \\\n read(name)\n#define VV(type, name, h, w) \\\n vector<vector<type>> name(h, vector<type>(w)); \\\n read(name)\n\nvoid YES(bool t = 1) { print(t ? \"YES\" : \"NO\"); }\nvoid NO(bool t = 1) { YES(!t); }\nvoid Yes(bool t = 1) { print(t ? \"Yes\" : \"No\"); }\nvoid No(bool t = 1) { Yes(!t); }\nvoid yes(bool t = 1) { print(t ? \"yes\" : \"no\"); }\nvoid no(bool t = 1) { yes(!t); }\n#line 2 \"library/graph/base.hpp\"\n\ntemplate <typename T>\nstruct Edge {\n int frm, to;\n T cost;\n int id;\n};\n\ntemplate <typename T = int, bool directed = false>\nstruct Graph {\n int N, M;\n using cost_type = T;\n using edge_type = Edge<T>;\n vector<edge_type> edges;\n vector<int> indptr;\n vector<edge_type> csr_edges;\n vc<int> vc_deg, vc_indeg, vc_outdeg;\n bool prepared;\n\n class OutgoingEdges {\n public:\n OutgoingEdges(const Graph* G, int l, int r) : G(G), l(l), r(r) {}\n\n const edge_type* begin() const {\n if (l == r) { return 0; }\n return &G->csr_edges[l];\n }\n\n const edge_type* end() const {\n if (l == r) { return 0; }\n return &G->csr_edges[r];\n }\n\n private:\n const Graph* G;\n int l, r;\n };\n\n bool is_prepared() { return prepared; }\n constexpr bool is_directed() { return directed; }\n\n Graph() : N(0), M(0), prepared(0) {}\n Graph(int N) : N(N), M(0), prepared(0) {}\n\n void build(int n) {\n N = n, M = 0;\n prepared = 0;\n edges.clear();\n indptr.clear();\n csr_edges.clear();\n vc_deg.clear();\n vc_indeg.clear();\n vc_outdeg.clear();\n }\n\n void add(int frm, int to, T cost = 1, int i = -1) {\n assert(!prepared);\n assert(0 <= frm && 0 <= to && to < N);\n if (i == -1) i = M;\n auto e = edge_type({frm, to, cost, i});\n edges.eb(e);\n ++M;\n }\n\n // wt, off\n void read_tree(bool wt = false, int off = 1) { read_graph(N - 1, wt, off); }\n\n void read_graph(int M, bool wt = false, int off = 1) {\n for (int m = 0; m < M; ++m) {\n INT(a, b);\n a -= off, b -= off;\n if (!wt) {\n add(a, b);\n } else {\n T c;\n read(c);\n add(a, b, c);\n }\n }\n build();\n }\n\n void build() {\n assert(!prepared);\n prepared = true;\n indptr.assign(N + 1, 0);\n for (auto&& e: edges) {\n indptr[e.frm + 1]++;\n if (!directed) indptr[e.to + 1]++;\n }\n for (int v = 0; v < N; ++v) { indptr[v + 1] += indptr[v]; }\n auto counter = indptr;\n csr_edges.resize(indptr.back() + 1);\n for (auto&& e: edges) {\n csr_edges[counter[e.frm]++] = e;\n if (!directed)\n csr_edges[counter[e.to]++] = edge_type({e.to, e.frm, e.cost, e.id});\n }\n }\n\n OutgoingEdges operator[](int v) const {\n assert(prepared);\n return {this, indptr[v], indptr[v + 1]};\n }\n\n vc<int> deg_array() {\n if (vc_deg.empty()) calc_deg();\n return vc_deg;\n }\n\n pair<vc<int>, vc<int>> deg_array_inout() {\n if (vc_indeg.empty()) calc_deg_inout();\n return {vc_indeg, vc_outdeg};\n }\n\n int deg(int v) {\n if (vc_deg.empty()) calc_deg();\n return vc_deg[v];\n }\n\n int in_deg(int v) {\n if (vc_indeg.empty()) calc_deg_inout();\n return vc_indeg[v];\n }\n\n int out_deg(int v) {\n if (vc_outdeg.empty()) calc_deg_inout();\n return vc_outdeg[v];\n }\n\n void debug() {\n print(\"Graph\");\n if (!prepared) {\n print(\"frm to cost id\");\n for (auto&& e: edges) print(e.frm, e.to, e.cost, e.id);\n } else {\n print(\"indptr\", indptr);\n print(\"frm to cost id\");\n FOR(v, N) for (auto&& e: (*this)[v]) print(e.frm, e.to, e.cost, e.id);\n }\n }\n\n vc<int> new_idx;\n vc<bool> used_e;\n\n // G における頂点 V[i] が、新しいグラフで i になるようにする\n // {G, es}\n pair<Graph<T, directed>, vc<int>> rearrange(vc<int> V) {\n if (len(new_idx) != N) new_idx.assign(N, -1);\n if (len(used_e) != M) used_e.assign(M, 0);\n int n = len(V);\n FOR(i, n) new_idx[V[i]] = i;\n Graph<T, directed> G(n);\n vc<int> es;\n FOR(i, n) {\n for (auto&& e: (*this)[V[i]]) {\n if (used_e[e.id]) continue;\n int a = e.frm, b = e.to;\n if (new_idx[a] != -1 && new_idx[b] != -1) {\n used_e[e.id] = 1;\n G.add(new_idx[a], new_idx[b], e.cost);\n es.eb(e.id);\n }\n }\n }\n FOR(i, n) new_idx[V[i]] = -1;\n for (auto&& eid: es) used_e[eid] = 0;\n G.build();\n return {G, es};\n }\n\nprivate:\n void calc_deg() {\n assert(vc_deg.empty());\n vc_deg.resize(N);\n for (auto&& e: edges) vc_deg[e.frm]++, vc_deg[e.to]++;\n }\n\n void calc_deg_inout() {\n assert(vc_indeg.empty());\n vc_indeg.resize(N);\n vc_outdeg.resize(N);\n for (auto&& e: edges) { vc_indeg[e.to]++, vc_outdeg[e.frm]++; }\n }\n};\n#line 3 \"library/graph/shortest_path/dijkstra.hpp\"\n\ntemplate <typename T, typename GT>\npair<vc<T>, vc<int>> dijkstra_dense(GT& G, int s) {\n const int N = G.N;\n vc<T> dist(N, infty<T>);\n vc<int> par(N, -1);\n vc<bool> done(N);\n dist[s] = 0;\n while (1) {\n int v = -1;\n T mi = infty<T>;\n FOR(i, N) {\n if (!done[i] && chmin(mi, dist[i])) v = i;\n }\n if (v == -1) break;\n done[v] = 1;\n for (auto&& e: G[v]) {\n if (chmin(dist[e.to], dist[v] + e.cost)) par[e.to] = v;\n }\n }\n return {dist, par};\n}\n\ntemplate <typename T, typename GT, bool DENSE = false>\npair<vc<T>, vc<int>> dijkstra(GT& G, int v) {\n if (DENSE) return dijkstra_dense<T>(G, v);\n auto N = G.N;\n vector<T> dist(N, infty<T>);\n vector<int> par(N, -1);\n using P = pair<T, int>;\n\n priority_queue<P, vector<P>, greater<P>> que;\n\n dist[v] = 0;\n que.emplace(0, v);\n while (!que.empty()) {\n auto [dv, v] = que.top();\n que.pop();\n if (dv > dist[v]) continue;\n for (auto&& e: G[v]) {\n if (chmin(dist[e.to], dist[e.frm] + e.cost)) {\n par[e.to] = e.frm;\n que.emplace(dist[e.to], e.to);\n }\n }\n }\n return {dist, par};\n}\n\n// 多点スタート。[dist, par, root]\ntemplate <typename T, typename GT>\ntuple<vc<T>, vc<int>, vc<int>> dijkstra(GT& G, vc<int> vs) {\n assert(G.is_prepared());\n int N = G.N;\n vc<T> dist(N, infty<T>);\n vc<int> par(N, -1);\n vc<int> root(N, -1);\n\n using P = pair<T, int>;\n\n priority_queue<P, vector<P>, greater<P>> que;\n\n for (auto&& v: vs) {\n dist[v] = 0;\n root[v] = v;\n que.emplace(T(0), v);\n }\n\n while (!que.empty()) {\n auto [dv, v] = que.top();\n que.pop();\n if (dv > dist[v]) continue;\n for (auto&& e: G[v]) {\n if (chmin(dist[e.to], dist[e.frm] + e.cost)) {\n root[e.to] = root[e.frm];\n par[e.to] = e.frm;\n que.push(mp(dist[e.to], e.to));\n }\n }\n }\n return {dist, par, root};\n}\n#line 1 \"library/flow/maxflow.hpp\"\n// incremental に辺を追加してよい\ntemplate <typename Cap>\nstruct MaxFlow {\n struct Edge {\n int to, rev;\n Cap cap;\n Cap flow = 0;\n };\n\n const int N, source, sink;\n vvc<Edge> edges;\n vc<int> prog, level;\n vc<int> que;\n bool calculated;\n Cap flow_ans;\n\n MaxFlow(int N, int source, int sink)\n : N(N),\n source(source),\n sink(sink),\n edges(N),\n calculated(0),\n flow_ans(0) {}\n\n void add(int frm, int to, Cap cap, Cap rev_cap = 0) {\n calculated = 0;\n assert(0 <= frm && frm < N);\n assert(0 <= to && to < N);\n assert(frm != to);\n assert(Cap(0) <= cap);\n if (frm == to) return;\n int a = len(edges[frm]);\n int b = len(edges[to]);\n edges[frm].eb(Edge{to, b, cap, 0});\n edges[to].eb(Edge{frm, a, rev_cap, 0});\n }\n\n // frm, to, flow\n vc<tuple<int, int, Cap>> get_flow_edges() {\n vc<tuple<int, int, Cap>> res;\n FOR(frm, N) {\n for (auto&& e: edges[frm]) {\n if (e.flow <= 0) continue;\n res.eb(frm, e.to, e.flow);\n }\n }\n return res;\n }\n\n // 差分ではなくこれまでの総量\n Cap flow() {\n if (calculated) return flow_ans;\n calculated = true;\n while (set_level()) {\n prog.assign(N, 0);\n while (1) {\n Cap x = flow_dfs(source, infty<Cap>);\n if (x == 0) break;\n flow_ans += x;\n chmin(flow_ans, infty<Cap>);\n if (flow_ans == infty<Cap>) return flow_ans;\n }\n }\n return flow_ans;\n }\n\n // 最小カットの値および、カットを表す 01 列を返す\n pair<Cap, vc<int>> cut() {\n flow();\n vc<int> res(N);\n FOR(v, N) res[v] = (level[v] >= 0 ? 0 : 1);\n return {flow_ans, res};\n }\n\n // O(F(N+M)) くらい使って経路復元\n // simple path になる\n vvc<int> path_decomposition() {\n flow();\n auto edges = get_flow_edges();\n vvc<int> TO(N);\n for (auto&& [frm, to, flow]: edges) { FOR(flow) TO[frm].eb(to); }\n vvc<int> res;\n vc<int> vis(N);\n\n FOR(flow_ans) {\n vc<int> path = {source};\n vis[source] = 1;\n while (path.back() != sink) {\n int to = POP(TO[path.back()]);\n while (vis[to]) { vis[POP(path)] = 0; }\n path.eb(to), vis[to] = 1;\n }\n for (auto&& v: path) vis[v] = 0;\n res.eb(path);\n }\n return res;\n }\n\nprivate:\n bool set_level() {\n que.resize(N);\n level.assign(N, -1);\n level[source] = 0;\n int l = 0, r = 0;\n que[r++] = source;\n while (l < r) {\n int v = que[l++];\n for (auto&& e: edges[v]) {\n if (e.cap > 0 && level[e.to] == -1) {\n level[e.to] = level[v] + 1;\n if (e.to == sink) return true;\n que[r++] = e.to;\n }\n }\n }\n return false;\n }\n\n Cap flow_dfs(int v, Cap lim) {\n if (v == sink) return lim;\n Cap res = 0;\n for (int& i = prog[v]; i < len(edges[v]); ++i) {\n auto& e = edges[v][i];\n if (e.cap > 0 && level[e.to] == level[v] + 1) {\n Cap a = flow_dfs(e.to, min(lim, e.cap));\n if (a > 0) {\n e.cap -= a, e.flow += a;\n edges[e.to][e.rev].cap += a, edges[e.to][e.rev].flow -= a;\n res += a;\n lim -= a;\n if (lim == 0) break;\n }\n }\n }\n return res;\n }\n};\n#line 2 \"library/ds/unionfind/unionfind.hpp\"\n\nstruct UnionFind {\n int n, n_comp;\n vc<int> dat; // par or (-size)\n UnionFind(int n = 0) { build(n); }\n\n void build(int m) {\n n = m, n_comp = m;\n dat.assign(n, -1);\n }\n\n void reset() { build(n); }\n\n int operator[](int x) {\n while (dat[x] >= 0) {\n int pp = dat[dat[x]];\n if (pp < 0) { return dat[x]; }\n x = dat[x] = pp;\n }\n return x;\n }\n\n ll size(int x) {\n x = (*this)[x];\n return -dat[x];\n }\n\n bool merge(int x, int y) {\n x = (*this)[x], y = (*this)[y];\n if (x == y) return false;\n if (-dat[x] < -dat[y]) swap(x, y);\n dat[x] += dat[y], dat[y] = x, n_comp--;\n return true;\n }\n};\n#line 4 \"library/graph/dag_path_cover.hpp\"\n\n// 各頂点の色をかえす。各色はひとつのパス上にあるようにする\ntemplate <typename DAG>\nvc<int> dag_path_cover(DAG& G) {\n assert(G.is_directed());\n for (auto&& e: G.edges) assert(e.frm < e.to);\n\n int N = G.N;\n int source = 2 * N, sink = 2 * N + 1;\n MaxFlow<int> F(2 * N + 2, source, sink);\n FOR(v, N) {\n F.add(source, 2 * v + 1, 1);\n F.add(2 * v + 0, sink, 1);\n F.add(2 * v + 0, 2 * v + 1, infty<int>);\n }\n for (auto&& e: G.edges) F.add(2 * e.frm + 1, 2 * e.to + 0, infty<int>);\n\n F.flow();\n auto paths = F.path_decomposition();\n\n UnionFind uf(N);\n for (auto& P: paths) {\n int a = P[1], b = P[len(P) - 2];\n uf.merge(a / 2, b / 2);\n }\n\n vc<int> ANS(N, -1);\n int p = 0;\n FOR(v, N) if (uf[v] == v) ANS[v] = p++;\n FOR(v, N) if (uf[v] != v) ANS[v] = ANS[uf[v]];\n return ANS;\n};\n#line 1 \"library/ds/fastset.hpp\"\n/* 64分木。\ninsert, erase\n[]での存在判定\nnext, prev\n*/\nstruct FastSet {\n using uint = unsigned;\n using ull = unsigned long long;\n\n int bsr(ull x) { return 63 - __builtin_clzll(x); }\n int bsf(ull x) { return __builtin_ctzll(x); }\n\n static constexpr uint B = 64;\n int n, lg;\n vector<vector<ull>> seg;\n FastSet(int _n) : n(_n) {\n do {\n seg.push_back(vector<ull>((_n + B - 1) / B));\n _n = (_n + B - 1) / B;\n } while (_n > 1);\n lg = int(seg.size());\n }\n bool operator[](int i) const { return (seg[0][i / B] >> (i % B) & 1) != 0; }\n void insert(int i) {\n for (int h = 0; h < lg; h++) {\n seg[h][i / B] |= 1ULL << (i % B);\n i /= B;\n }\n }\n void add(int i) { insert(i); }\n void erase(int i) {\n for (int h = 0; h < lg; h++) {\n seg[h][i / B] &= ~(1ULL << (i % B));\n if (seg[h][i / B]) break;\n i /= B;\n }\n }\n void remove(int i) { erase(i); }\n\n // x以上最小の要素を返す。存在しなければ n。\n int next(int i) {\n chmax(i, 0);\n if (i >= n) return n;\n for (int h = 0; h < lg; h++) {\n if (i / B == seg[h].size()) break;\n ull d = seg[h][i / B] >> (i % B);\n if (!d) {\n i = i / B + 1;\n continue;\n }\n // find\n i += bsf(d);\n for (int g = h - 1; g >= 0; g--) {\n i *= B;\n i += bsf(seg[g][i / B]);\n }\n return i;\n }\n return n;\n }\n\n // x以下最大の要素を返す。存在しなければ -1。\n int prev(int i) {\n if (i < 0) return -1;\n if (i >= n) i = n - 1;\n for (int h = 0; h < lg; h++) {\n if (i == -1) break;\n ull d = seg[h][i / B] << (63 - i % 64);\n if (!d) {\n i = i / B - 1;\n continue;\n }\n // find\n i += bsr(d) - (B - 1);\n for (int g = h - 1; g >= 0; g--) {\n i *= B;\n i += bsr(seg[g][i / B]);\n }\n return i;\n }\n return -1;\n }\n\n // [l, r)\n template <typename F>\n void enumerate(int l, int r, F f) {\n int x = l - 1;\n while (1) {\n x = next(x + 1);\n if (x >= r) break;\n f(x);\n }\n }\n\n void debug() {\n string s;\n for (int i = 0; i < n; ++i) s += ((*this)[i] ? '1' : '0');\n print(s);\n }\n};\n#line 3 \"library/graph/toposort.hpp\"\n\n// 辞書順最小の toposort を返す\ntemplate <typename GT>\nvc<int> toposort(GT& G) {\n assert(G.is_prepared() && G.is_directed());\n const int N = G.N;\n auto [indeg, outdeg] = G.deg_array_inout();\n FastSet que(N);\n vc<int> V;\n FOR(v, N) if (indeg[v] == 0) que.insert(v);\n while (1) {\n int v = que.next(0);\n if (v == N) break;\n que.erase(v), V.eb(v);\n for (auto&& e: G[v]) {\n if (--indeg[e.to] == 0) que.insert(e.to);\n }\n }\n return (len(V) < N ? vc<int>{} : V);\n}\n#line 7 \"main.cpp\"\n\nvoid solve(ll N, ll M, ll L) {\n vv(ll, dist, N, N);\n {\n Graph<ll> G(N);\n G.read_graph(M, 1, 0);\n FOR(v, N) { dist[v] = dijkstra<ll>(G, v).fi; }\n }\n VEC(pi, PT, L);\n N = L;\n Graph<int, 1> G(N);\n FOR(a, N) FOR(b, N) {\n if (a == b) continue;\n auto [pa, ta] = PT[a];\n auto [pb, tb] = PT[b];\n if (ta + dist[pa][pb] <= tb) G.add(a, b);\n }\n G.build();\n\n auto V = toposort(G);\n G = G.rearrange(V).fi;\n\n auto color = dag_path_cover(G);\n print(MAX(color) + 1);\n}\n\nsigned main() {\n while (1) {\n LL(N, M, L);\n if (N + M + L == 0) break;\n solve(N, M, L);\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 540, "memory_kb": 59984, "score_of_the_acc": -1.2076, "final_rank": 18 }, { "submission_id": "aoj_2251_7320954", "code_snippet": "#define _CRT_SECURE_NO_WARNINGS\n#include <cstdio>\n#include <iostream>\n#include <stack>\n#include <queue>\n#include <algorithm>\n#include <functional>\n#include <set>\n#include <map>\n#include <string>\n#include <vector>\n#include <cmath>\n#include<sstream>\n#include<list>\n#include<iomanip>\n#include <cstdlib>\n#include <cstring>\n#include <stack>\n#include <bitset>\n#include <cassert>\n#include <stdlib.h>\n#include <stdio.h>\nusing namespace std;\nconst int INF = 100000000;\nconst long long LINF = 3e18 + 7;\nconst int MAX_N = 2000010;\nconst int MAX_W = 10002;\nconst int MAX_ARRAYK = 100000;\ndouble PI = 3.14159265358979323846;\n//using ll = long long;\n\n// freopen(\"in.txt\", \"r\", stdin);\n// freopen(\"out.txt\", \"w\", stdout);\n\n\n// Bipartite Matching (Simple)\n// https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_7_A&lang=ja\n// 蟻本 p. 197\n\n\nint V, E;\nint X, Y;\nvector<int> G[MAX_N];\nint match[MAX_N];\nbool used[MAX_N];\n\n// uとvを結ぶ辺をグラフに追加する\nvoid add_edge(int u, int v) {\n\tG[u].push_back(v);\n\tG[v].push_back(u);\n}\n\n// 増加パスをDFSで探す\nbool dfs(int v) {\n\tused[v] = true;\n\tfor (int i = 0; i < G[v].size(); i++) {\n\t\tint u = G[v][i];\n\t\tint w = match[u];\n\t\tif (w < 0 || !used[w] && dfs(w)) {\n\t\t\tmatch[v] = u;\n\t\t\tmatch[u] = v;\n\t\t\treturn true;\n\t\t}\n\t}\n\treturn false;\n}\n\nint bipartite_matching() {\n\tint res = 0;\n\tmemset(match, -1, sizeof(match));\n\tfor (int v = 0; v < V; v++) {\n\t\tif (match[v] < 0) {\n\t\t\tmemset(used, 0, sizeof(used));\n\t\t\tif (dfs(v)) {\n\t\t\t\tres++;\n\t\t\t}\n\t\t}\n\t}\n\treturn res;\n}\n\nint N, M, L;\nlong long cost[110][110];\nint a[1100], b[1100];\nlong long c[1100];\nint p[1100];\nlong long t[1100];\n\nint main() {\n\t//freopen(\"in.txt\", \"r\", stdin);\n\t//freopen(\"out.txt\", \"w\", stdout);\n\twhile (cin >> N >> M >> L) {\n\t\tif (N == 0 && M == 0 && L == 0) {\n\t\t\tbreak;\n\t\t}\n\n\t\tfor (int i = 0; i < M; i++) {\n\t\t\tcin >> a[i] >> b[i] >> c[i];\n\t\t}\n\t\tfor (int i = 0; i < L; i++) {\n\t\t\tcin >> p[i] >> t[i];\n\t\t}\n\n\n\t\tfor (int i = 0; i < N; i++) {\n\t\t\tfor (int j = 0; j < N; j++) {\n\t\t\t\tcost[i][j] = LINF;\n\t\t\t}\n\t\t\tcost[i][i] = 0;\n\t\t}\n\n\t\tfor (int i = 0; i < M; i++) {\n\t\t\tcost[a[i]][b[i]] = c[i];\n\t\t\tcost[b[i]][a[i]] = c[i];\n\t\t}\n\n\n\t\tfor (int k = 0; k < N; k++) {\n\t\t\tfor (int i = 0; i < N; i++) {\n\t\t\t\tfor (int j = 0; j < N; j++) {\n\t\t\t\t\tcost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j]);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tV = 2 * L;\n\t\tfor (int i = 0; i < V; i++) {\n\t\t\tG[i].clear();\n\t\t}\n\n\t\tfor (int i = 0; i < L; i++) {\n\t\t\tfor (int j = 0; j < L; j++) {\n\t\t\t\tif (i == j) {\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\n\t\t\t\tif (t[i] + cost[p[i]][p[j]] <= t[j]) {\n\t\t\t\t\tadd_edge(i, j + L);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\n\t\tcout << L - bipartite_matching() << endl;\n\n\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 1410, "memory_kb": 68376, "score_of_the_acc": -1.9262, "final_rank": 20 }, { "submission_id": "aoj_2251_7248951", "code_snippet": "#include \"bits/stdc++.h\"\nusing namespace std;\nusing namespace std::chrono;\ntypedef long long int llint;\ntypedef long double lldo;\n#define mp make_pair\n#define mt make_tuple\n#define pub push_back\n#define puf push_front\n#define pob pop_back\n#define pof pop_front\n#define fir first\n#define sec second\n#define res resize\n#define ins insert\n#define era erase\n#define REP(i, n) for(int i = 0;i < (n);i++)\n/*cout<<fixed<<setprecision(20);cin.tie(0);ios::sync_with_stdio(false);*/\nconst int mod=1000000007;\nconst llint inf=2.19e15+1;\nconst long double pai=3.141592653589793238462643383279502884197;\nconst long double eps=1e-10;\ntemplate <class T,class U>bool chmin(T& a,U b){if(a>b){a=b;return true;}return false;}\ntemplate <class T,class U>bool chmax(T& a,U b){if(a<b){a=b;return true;}return false;}\nllint gcd(llint a,llint b){if(a%b==0){return b;}else return gcd(b,a%b);}\nllint lcm(llint a,llint b){if(a==0){return b;}return a/gcd(a,b)*b;}\ntemplate<class T> void SO(T& ve){sort(ve.begin(),ve.end());}\ntemplate<class T> void REV(T& ve){reverse(ve.begin(),ve.end());}\ntemplate<class T>int LBI(const vector<T>&ar,T in){return lower_bound(ar.begin(),ar.end(),in)-ar.begin();}\ntemplate<class T>int UBI(const vector<T>&ar,T in){return upper_bound(ar.begin(),ar.end(),in)-ar.begin();}\n\n#include <algorithm>\n#include <cassert>\n#include <limits>\n#include <queue>\n#include <vector>\n\ntemplate <class T> struct simple_queue {\n std::vector<T> payload;\n int pos = 0;\n void reserve(int n) { payload.reserve(n); }\n int size() const { return int(payload.size()) - pos; }\n bool empty() const { return pos == int(payload.size()); }\n void push(const T& t) { payload.push_back(t); }\n T& front() { return payload[pos]; }\n void clear() {\n payload.clear();\n pos = 0;\n }\n void pop() { pos++; }\n};\n\t\ntemplate <class Cap> struct mf_graph {\n public:\n mf_graph() : _n(0) {}\n mf_graph(int n) : _n(n), g(n) {}\n\n int add_edge(int from, int to, Cap cap) {\n assert(0 <= from && from < _n);\n assert(0 <= to && to < _n);\n assert(0 <= cap);\n int m = int(pos.size());\n pos.push_back({from, int(g[from].size())});\n int from_id = int(g[from].size());\n int to_id = int(g[to].size());\n if (from == to) to_id++;\n g[from].push_back(_edge{to, to_id, cap});\n g[to].push_back(_edge{from, from_id, 0});\n return m;\n }\n\n struct edge {\n int from, to;\n Cap cap, flow;\n };\n\n edge get_edge(int i) {\n int m = int(pos.size());\n assert(0 <= i && i < m);\n auto _e = g[pos[i].first][pos[i].second];\n auto _re = g[_e.to][_e.rev];\n return edge{pos[i].first, _e.to, _e.cap + _re.cap, _re.cap};\n }\n std::vector<edge> edges() {\n int m = int(pos.size());\n std::vector<edge> result;\n for (int i = 0; i < m; i++) {\n result.push_back(get_edge(i));\n }\n return result;\n }\n void change_edge(int i, Cap new_cap, Cap new_flow) {\n int m = int(pos.size());\n assert(0 <= i && i < m);\n assert(0 <= new_flow && new_flow <= new_cap);\n auto& _e = g[pos[i].first][pos[i].second];\n auto& _re = g[_e.to][_e.rev];\n _e.cap = new_cap - new_flow;\n _re.cap = new_flow;\n }\n\n Cap flow(int s, int t) {\n return flow(s, t, std::numeric_limits<Cap>::max());\n }\n Cap flow(int s, int t, Cap flow_limit) {\n assert(0 <= s && s < _n);\n assert(0 <= t && t < _n);\n assert(s != t);\n\n std::vector<int> level(_n), iter(_n);\n simple_queue<int> que;\n\n auto bfs = [&]() {\n std::fill(level.begin(), level.end(), -1);\n level[s] = 0;\n que.clear();\n que.push(s);\n while (!que.empty()) {\n int v = que.front();\n que.pop();\n for (auto e : g[v]) {\n if (e.cap == 0 || level[e.to] >= 0) continue;\n level[e.to] = level[v] + 1;\n if (e.to == t) return;\n que.push(e.to);\n }\n }\n };\n auto dfs = [&](auto self, int v, Cap up) {\n if (v == s) return up;\n Cap res = 0;\n int level_v = level[v];\n for (int& i = iter[v]; i < int(g[v].size()); i++) {\n _edge& e = g[v][i];\n if (level_v <= level[e.to] || g[e.to][e.rev].cap == 0) continue;\n Cap d =\n self(self, e.to, std::min(up - res, g[e.to][e.rev].cap));\n if (d <= 0) continue;\n g[v][i].cap += d;\n g[e.to][e.rev].cap -= d;\n res += d;\n if (res == up) break;\n }\n return res;\n };\n\n Cap flow = 0;\n while (flow < flow_limit) {\n bfs();\n if (level[t] == -1) break;\n std::fill(iter.begin(), iter.end(), 0);\n while (flow < flow_limit) {\n Cap f = dfs(dfs, t, flow_limit - flow);\n if (!f) break;\n flow += f;\n }\n }\n return flow;\n }\n\n std::vector<bool> min_cut(int s) {\n std::vector<bool> visited(_n);\n simple_queue<int> que;\n que.push(s);\n while (!que.empty()) {\n int p = que.front();\n que.pop();\n visited[p] = true;\n for (auto e : g[p]) {\n if (e.cap && !visited[e.to]) {\n visited[e.to] = true;\n que.push(e.to);\n }\n }\n }\n return visited;\n }\n\n private:\n int _n;\n struct _edge {\n int to, rev;\n Cap cap;\n };\n std::vector<std::pair<int, int>> pos;\n std::vector<std::vector<_edge>> g;\n};\n\n\n\nint main(void){\n\twhile(-1){\n\t\tint i,j,k,n,m,L;cin>>n>>m>>L;\n\t\tif(n==0){break;}\n\t\tint dist[100][100];\n\t\tfor(i=0;i<n;i++){\n\t\t\tfor(j=0;j<n;j++){\n\t\t\t\tdist[i][j]=mod;\n\t\t\t}\n\t\t\tdist[i][i]=0;\n\t\t}\n\t\tfor(i=0;i<m;i++){\n\t\t\tint u,v,d;cin>>u>>v>>d;\n\t\t\tchmin(dist[u][v],d);\n\t\t\tchmin(dist[v][u],d);\n\t\t}\n\t\tfor(j=0;j<n;j++){\n\t\t\tfor(i=0;i<n;i++){\n\t\t\t\tfor(k=0;k<n;k++){\n\t\t\t\t\tchmin(dist[i][k],dist[i][j]+dist[j][k]);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tvector<pair<int,int>>req(L);\n\t\tfor(i=0;i<L;i++){\n\t\t\tcin>>req[i].sec>>req[i].fir;\n\t\t}\n\t\tSO(req);\n\t\tmf_graph<int> gra(L+L+2);\n\t\tfor(i=0;i<L;i++){\n\t\t\tfor(j=i+1;j<L;j++){\n\t\t\t\tif(req[i].fir+dist[req[i].sec][req[j].sec]<=req[j].fir){\n\t\t\t\t\tgra.add_edge(i,j+L,1);\n\t\t\t\t}\n\t\t\t}\n\t\t\tgra.add_edge(L+L,i,1);\n\t\t\tgra.add_edge(i+L,L+L+1,1);\n\t\t}\n\t\tcout<<L-gra.flow(L+L,L+L+1)<<endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 200, "memory_kb": 22744, "score_of_the_acc": -0.382, "final_rank": 9 } ]
aoj_2253_cpp
ブレイブ・フォース・ストーリー English text is not available in this practice contest. 「最果ての地,ナイン・アイランドには特別な力を持つものが生きていた. ある者は火,ある者は氷,ある者は風,ある者は土を意のままに操ることができた. 人はこれらの術者をこう呼んだ.『Brave Force』と・・・.時は戦国時代.権力者たちはBrave Forceを私欲のために使わんとBrave Force狩りを始めた. Brave Forceを巡る戦いが今始まろうとしている.」 以上は,あなたが今つくろうとしている戦略シミュレーションゲームのプロローグである.このプロローグは本問題には全く関係ない. 問題の説明に戻ろう.戦略シミュレーションの世界においては, 正6角形のマスがよく使われる. これは, 正方形のマスに比べて方向による距離の違いが小さく, 敷き詰めも可能であるからである. 今回はこのようなマップ上でコマを動かすことを考える.コマは各ターンごとに隣接するマスに移動させることができる. もちろんマップ上には多くの障害物があってそこには移動することができない.さて一定のターン数が経過するまでにたどり着くことが可能なマスの数はいくつあるだろうか. Input 入力はそれぞれがマップの情報を表す1つ以上のデータセットからなる. データセットの最初の行には, 2つの整数が含まれていて, 1番目がターン数 t で, 2番目が障害物の数 n である. それに続く n 行にはそれぞれ障害物のマスの座標を表す2つの整数が含まれていて, 1番目が x 座標で, 2番目が y 座標である. 障害物の座標は互いに異なっている. そして最後の行にはスタート位置のマスの座標を表す2つの整数が含まれていて, 1番目が x 座標で, 2番目が y 座標である. このマスには障害物はない. またこのマスは到達できるマスに含められる. マスに割り当てられた座標は下の図のようになっている. 図B-1 マスに割り当てられた座標 入力の終わりは"0 0"を含む行で表される. いずれの座標も絶対値が30以下である. ターン数は1以上30以下である. 障害物の数は0以上300以下である. 図B-2 Sample Input の1つ目のデータセット Output 各マップについて到達できるマスの数を表す整数を各行に出力せよ. それ以外の余計な文字を出力に含めてはいけない. Sample Input 1 1 1 0 0 0 2 2 -2 1 2 0 2 2 2 0 -1 1 4 4 -2 1 1 -2 1 2 3 -3 -2 0 4 6 0 1 1 1 1 0 -1 0 -1 -1 0 -1 0 0 0 0 Output for the Sample Input 6 18 19 58 1
[ { "submission_id": "aoj_2253_10681617", "code_snippet": "#include <bits/stdc++.h>\n#include <cstdint>\n#include <regex>\nusing namespace std;\n\nusing ll = long long;\nusing vi = vector<int>;\nusing vl = vector<long long>;\nusing vs = vector<string>;\nusing vc = vector<char>;\nusing vb = vector<bool>;\nusing vpii = vector<pair<int, int>>;\nusing vpll = vector<pair<long long, long long>>;\nusing vvi = vector<vector<int>>;\nusing vvl = vector<vector<long long>>;\nusing vvc = vector<vector<char>>;\nusing vvvi = vector<vector<vector<int>>>;\nusing pii = pair<int, int>;\nusing vvb = vector<vector<bool>>;\nusing Graph = vector<vector<ll>>;\nconst ll LINF = 1001002003004005006ll;\nconst int INF = 1001001001;\nconst int MOD = 998244353;\n\n#define rep(i, n) for (int i = 0; i < (int)(n); i++)\n#define rep3(i, m, n) for (int i = (m); i < (int)(n); i++)\n#define rrep(i, m, n) for (int i = (m); i >= (int)(n); i--)\n#define all(x) (x).begin(), (x).end()\n#define rall(x) (x).rbegin(), (x).rend()\n\ntemplate <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {\n return a < b && (a = b, true);\n}\ntemplate <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {\n return a > b && (a = b, true);\n}\n\nvi dx4 = {1, 0, -1, 0}, dy4 = {0, 1, 0, -1};\nvi dx8 = {1, 1, 1, 0, 0, -1, -1, -1}, dy8 = {1, 0, -1, 1, -1, 1, 0, -1};\nvi dx6 = {0, 1, 1, 0, -1, -1}, dy6 = {1, 1, 0, -1, -1, 0};\n\nint main() {\n while (1) {\n int t, n;\n cin >> t >> n;\n if (t == 0 && n == 0) {\n break;\n }\n\n set<pii> st1;\n rep(i, n) {\n int x, y;\n cin >> x >> y;\n st1.insert({x, y});\n }\n\n int sx, sy;\n cin >> sx >> sy;\n\n set<pii> st2;\n set<pii> st3;\n st2.insert({sx, sy});\n rep(i, t) {\n for (auto [p, q] : st2) {\n rep(j, 6) {\n int nx = p + dx6[j];\n int ny = q + dy6[j];\n if (st1.count({nx, ny}) == 0) {\n st3.insert({nx, ny});\n }\n }\n }\n for (auto [p, q] : st3) {\n st2.insert({p, q});\n }\n }\n\n cout << st2.size() << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 510, "memory_kb": 3712, "score_of_the_acc": -0.1364, "final_rank": 14 }, { "submission_id": "aoj_2253_10675170", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define all(...) std::begin(__VA_ARGS__), std::end(__VA_ARGS__)\n#define rall(...) std::rbegin(__VA_ARGS__), std::rend(__VA_ARGS__)\n#define OVERLOAD_REP(_1, _2, _3, _4, name, ...) name\n#define REP1(n) for(ll i=0;i<(n);i++)\n#define REP2(i, n) for (ll i=0;i<(n);i++)\n#define REP3(i, a, n) for (ll i=a;i<(n);i++)\n#define REP4(i, a, b, n) for(ll i=a;i<(n);i+=b)\n#define rep(...) OVERLOAD_REP(__VA_ARGS__, REP4, REP3, REP2, REP1)(__VA_ARGS__)\n#define OVERLOAD_RREP(_1, _2, _3, _4, name, ...) name\n#define RREP1(n) for(ll i=(n)-1;i>=0;i--)\n#define RREP2(i, n) for(ll i=(n)-1;i>=0;i--)\n#define RREP3(i, a, n) for(ll i=(n)-1;i>=(a);i--)\n#define RREP4(i, a, b, n) for(ll i=(n)-1;i>=(a);i-=(b))\n#define rrep(...) OVERLOAD_RREP(__VA_ARGS__, RREP4, RREP3, RREP2, RREP1)(__VA_ARGS__)\n#define uniq(a) sort(all(a));a.erase(unique(all(a)),end(a))\n#define len(n) (long long)(n).size()\nusing ll = long long;\nusing ld = long double;\nusing ull = unsigned long long;\nusing vi = vector<int>;\nusing vvi = vector<vi>;\nusing vvvi = vector<vvi>;\nusing vll = vector<ll>;\nusing vvll = vector<vll>;\nusing vvvll = vector<vvll>;\nusing vs = vector<string>;\nusing vvs = vector<vs>;\nusing vvvs = vector<vvs>;\nusing vld = vector<ld>;\nusing vvld = vector<vld>;\nusing vvvld = vector<vvld>;\nusing vc = vector<char>;\nusing vvc = vector<vc>;\nusing vvvc = vector<vvc>;\nusing pll = pair<ll,ll>;\nusing vpll = vector<pll>;\nusing vvpll = vector<vpll>;\n\nll intpow(ll a,ll b){\n ll ans = 1;\n while (b){\n if (b & 1){\n ans *= a;\n }\n a *= a;\n b /= 2;\n }\n return ans;\n}\nll modpow(ll a,ll b,ll c){\n ll ans = 1;\n while (b){\n if (b & 1){\n ans *= a;\n ans %= c;\n }\n a *= a;\n a %= c;\n b /= 2;\n }\n return ans;\n}\n\ntemplate<class... T>\nvoid input(T&... a){\n (cin >> ... >> a);\n}\n\n#define INT(...) int __VA_ARGS__; input(__VA_ARGS__)\n#define LL(...) ll __VA_ARGS__; input(__VA_ARGS__)\n#define ULL(...) ull __VA_ARGS__; input(__VA_ARGS__)\n#define LD(...) ld __VA_ARGS__; input(__VA_ARGS__)\n#define STR(...) string __VA_ARGS__; input(__VA_ARGS__)\n#define CHA(...) char __VA_ARGS__; input(__VA_ARGS__)\n#define VLL(name,length) vll name(length);rep(i,length){cin >> name[i];}\n#define VVLL(name,h,w) vvll name(h,vll(w));rep(i,h)rep(j,w){cin >> name[i][j];}\n#define VVVLL(name,a,b,c) vvvll name(a,vvll(b,vll(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}\n#define VI(name,length) vi name(length);rep(i,length){cin >> name[i];}\n#define VVI(name,h,w) vvi name(h,vi(w));rep(i,h)rep(j,w){cin >> name[i][j];}\n#define VVVI(name,a,b,c) vvvi name(a,vvll(b,vi(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}\n#define VLD(name,length) vld name(length);rep(i,length){cin >> name[i];}\n#define VVLD(name,h,w) vvld name(h,vld(w));rep(i,h)rep(j,w){cin >> name[i][j];}\n#define VVVLD(name,a,b,c) vvvld name(a,vvld(b,vld(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}\n#define VC(name,length) vc name(length);rep(i,length){cin >> name[i];}\n#define VVC(name,h,w) vvc name(h,vc(w));rep(i,h)rep(j,w){cin >> name[i][j];}\n#define VVVC(name,a,b,c) vvvc name(a,vvc(b,vc(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}\n#define VS(name,length) vs name(length);rep(i,length){cin >> name[i];}\n#define VVS(name,h,w) vvs name(h,vs(w));rep(i,h)rep(j,w){cin >> name[i][j];}\n#define VVVS(name,a,b,c) vvvs name(a,vvs(b,vs(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}\n#define PLL(name) pll name;cin>>name.first>>name.second;\n#define VPLL(name,length) vpll name(length);rep(i,length){cin>>name[i].first>>name[i].second;}\n\nvoid print(){cout << \"\\n\";}\n\ntemplate <typename T1, typename T2>\nstd::ostream& operator<<(std::ostream& os, const std::pair<T1, T2>& p) {\n os << \"(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\n\ntemplate <typename T>\nstd::ostream& operator<<(std::ostream& os, const std::vector<T>& vec) {\n os << \"[\";\n for (size_t i = 0; i < vec.size(); ++i) {\n os << vec[i];\n if (i + 1 < vec.size()) os << \", \";\n }\n os << \"]\";\n return os;\n}\n\ntemplate <typename T1, typename T2>\nstd::ostream& operator<<(std::ostream& os, const std::vector<std::pair<T1, T2>>& a) {\n os << \"[\";\n for (size_t j = 0; j < a.size(); ++j) {\n os << \"(\" << a[j].first << \", \" << a[j].second << \")\";\n\t\tif (j + 1 < a.size()) os << \", \";\n }\n\tos << \"]\";\n return os;\n}\n\ntemplate <typename T1, typename T2>\nstd::ostream& operator<<(std::ostream& os, const std::vector<std::vector<std::pair<T1, T2>>>& mat) {\n\tos << \"[\";\n for (size_t i = 0; i < mat.size(); ++i) {\n\t\tos << \"[\";\n for (size_t j = 0; j < mat[i].size(); ++j) {\n os << \"(\" << mat[i][j].first << \", \" << mat[i][j].second << \")\";\n\t\t\tif (j + 1 < mat[i].size()) os << \", \";\n }\n\t\tos << \"]\";\n\t\tif (i + 1 < mat.size()) os << \", \";\n }\n\tos << \"]\";\n return os;\n}\n\ntemplate <typename T>\nstd::ostream& operator<<(std::ostream& os, const std::set<T>& s) {\n os << \"{\";\n bool first = true;\n for (const auto& x : s) {\n if (!first) os << \", \";\n os << x;\n first = false;\n }\n os << \"}\";\n return os;\n}\n\ntemplate <typename K, typename V>\nstd::ostream& operator<<(std::ostream& os, const std::map<K, V>& m) {\n os << \"{\";\n bool first = true;\n for (const auto& [key, val] : m) {\n if (!first) os << \", \";\n os << key << \": \" << val;\n first = false;\n }\n os << \"}\";\n return os;\n}\n\ntemplate<class T, class... Ts>\nvoid print(const T& a, const Ts&... b){cout << a;(cout << ... << (cout << ' ', b));cout << '\\n';}\n\nvoid write(){cout << \"\\n\";}\ntemplate<class T, class... Ts>\nvoid write(const T& a, const Ts&... b){cout << a;(cout << ... << (cout << ' ', b));cout << '\\n';}\nvoid write(vll x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << \" \";}else{cout << '\\n';}}}\nvoid write(vvll x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << \" \";}else{cout << '\\n';}}}\nvoid write(vi x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << \" \";}else{cout << '\\n';}}}\nvoid write(vvi x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << \" \";}else{cout << '\\n';}}}\nvoid write(vvvi x){rep(i,len(x))rep(j,len(x[i]))rep(k,len(x[i][j])){cout << x[i][j][k];if(k!=len(x[i][j])-1){cout << \" \";}else if(j!=len(x[i])-1){cout << \" | \";}else{cout << '\\n';}}}\nvoid write(vld x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << \" \";}else{cout << '\\n';}}}\nvoid write(vvld x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << \" \";}else{cout << '\\n';}}}\nvoid write(vvvld x){rep(i,len(x))rep(j,len(x[i]))rep(k,len(x[i][j])){cout << x[i][j][k];if(k!=len(x[i][j])-1){cout << \" \";}else if(j!=len(x[i])-1){cout << \" | \";}else{cout << '\\n';}}}\nvoid write(vc x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << \" \";}else{cout << '\\n';}}}\nvoid write(vvc x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << \" \";}else{cout << '\\n';}}}\nvoid write(vvvc x){rep(i,len(x))rep(j,len(x[i]))rep(k,len(x[i][j])){cout << x[i][j][k];if(k!=len(x[i][j])-1){cout << \" \";}else if(j!=len(x[i])-1){cout << \" | \";}else{cout << '\\n';}}}\nvoid write(vs x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << \" \";}else{cout << '\\n';}}}\nvoid write(vvs x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << \" \";}else{cout << '\\n';}}}\nvoid write(vvvs x){rep(i,len(x))rep(j,len(x[i]))rep(k,len(x[i][j])){cout << x[i][j][k];if(k!=len(x[i][j])-1){cout << \" \";}else if(j!=len(x[i])-1){cout << \" | \";}else{cout << '\\n';}}}\nvoid write(pll x){cout << x.first << ' ' << x.second << '\\n';}\nvoid write(vpll x){rep(i,len(x)){cout << x[i].first << ' ' << x[i].second << '\\n';}}\nvoid write(vvpll x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j].first << ' ' << x[i][j].second;if(j!=len(x[i])-1){cout << \" \";}else{cout << '\\n';}}}\n\ntemplate <typename T>\nT sum(const std::vector<T>& v) {\n return std::accumulate(v.begin(), v.end(), T(0));\n}\n\ntemplate<class T> bool chmin(T& a, const T& b){ if(a > b){ a = b; return 1; } return 0; }\ntemplate<class T> bool chmax(T& a, const T& b){ if(a < b){ a = b; return 1; } return 0; }\ntemplate<class T, class U> bool chmin(T& a, const U& b){ if(a > T(b)){ a = b; return 1; } return 0; }\ntemplate<class T, class U> bool chmax(T& a, const U& b){ if(a < T(b)){ a = b; return 1; } return 0; }\n\n\n\nint main(){\n\tios::sync_with_stdio(false);\n\tstd::cin.tie(nullptr);\n while(1){\n LL(t,n);\n if(n == 0 && t == 0){break;}\n map<ll,ll> mp;\n ll m = 1000000;\n rep(i,n){\n LL(x,y);\n mp[x * m + y] = 1;\n }\n map<ll,ll> dp;\n LL(sx,sy);\n dp[sx*m+sy] = 1;\n vpll dxy = {{0,1},{0,-1},{1,0},{-1,0},{1,1},{-1,-1},{0,0}};\n rep(i,t){\n map<ll,ll> ndp;\n for(auto [k,v]:dp){\n if(v == 0){continue;}\n ll x = k / m;\n ll y = k % m;\n for(auto [dx,dy]:dxy){\n ll tx = x+dx;\n ll ty = y+dy;\n if(mp[tx*m+ty] == 0){\n ndp[tx*m+ty] = 1;\n }\n }\n }\n swap(dp,ndp);\n }\n ll ans = 0;\n for(auto [k,v]:dp){\n ans += v;\n }\n print(ans);\n\n }\n}", "accuracy": 1, "time_ms": 670, "memory_kb": 3968, "score_of_the_acc": -0.189, "final_rank": 15 }, { "submission_id": "aoj_2253_10659094", "code_snippet": "#line 1 \"A.cpp\"\n#include <iostream>\n#include <utility>\n#include <queue>\n#include <vector>\n#include <cassert>\nusing namespace std;\nusing ll = long long;\ntemplate<class T> inline bool chmax(T& a, const T& b) {if (a<b) {a=b; return true;} return false;}\ntemplate<class T> inline bool chmin(T& a, const T& b) {if (b<a) {a=b; return true;} return false;}\nconstexpr int INTINF = 100;\nconstexpr int INTMAX = 2147483647;\nconstexpr ll LLMAX = 9223372036854775807;\nconstexpr ll LLINF = 4450000000011100000;\n\nint dx[6] = {1, 1, 0, -1, -1, 0};\nint dy[6] = {0, 1, 1, 0, -1, -1};\n\n#line 1 \"/home/harui/CompetitiveProgramming/library/debug.hpp\"\n\n\n\n#ifdef LOCAL\n#define dbg(x) std::cerr << __LINE__ << \" : \" << #x << \" = \" << (x) << std::endl\n#else\n#define dbg(x) true\n#endif\n\n#line 13 \"/home/harui/CompetitiveProgramming/library/debug.hpp\"\n#include <set>\n\n\ntemplate <typename T, typename U>\nstd::ostream& operator<<(std::ostream& os, const std::pair<T, U>& p) {\n os << '(' << p.first << \", \" << p.second << ')';\n return os;\n}\n\ntemplate <typename T>\nstd::ostream& operator<<(std::ostream& os, const std::vector<T>& vec) {\n os << '[';\n for (int i=0; i<int(vec.size()); i++) {\n os << vec[i];\n if (i != int(vec.size())-1) os << \", \";\n }\n os << ']';\n return os;\n}\n\ntemplate <typename T>\nstd::ostream& operator<<(std::ostream& os, const std::set<T>& st) {\n os << '{';\n auto itr = st.begin();\n while (itr != st.end()) {\n os << *itr;\n itr = next(itr);\n if (itr != st.end()) os << \", \";\n }\n os << '}';\n return os;\n}\n\n\n\n#line 19 \"A.cpp\"\n\nvoid solve(int T, int N) {\n dbg(\"called\");\n constexpr int OFFSET = 300;\n // -30 <= x <= 30\n // 0 <= x + 30 <= 60\n vector<vector<bool>>A(OFFSET*2+1, vector<bool>(OFFSET*2+1, false));\n vector<vector<int>>dist(OFFSET*2+1, vector<int>(OFFSET*2+1, INTINF));\n\n for (int i=0; i<N; i++) {\n int x, y;\n cin >> x >> y;\n A[x+OFFSET][y+OFFSET] = true;\n }\n \n\n int sx, sy; cin >> sx >> sy;\n queue<pair<int,int>> que;\n dist[sx + OFFSET][sy + OFFSET] = 0;\n que.push(make_pair(sx,sy));\n\n while (!que.empty()) {\n pair<int,int> fr = que.front(); que.pop();\n int x = fr.first;\n int y = fr.second;\n assert(A[x+OFFSET][y+OFFSET]==false);\n assert(dist[x+OFFSET][y+OFFSET] < INTINF);\n for (int k=0; k<6; k++) {\n int nx = x + dx[k];\n int ny = y + dy[k];\n if ((!A[nx+OFFSET][ny+OFFSET]) && chmin(dist[nx+OFFSET][ny+OFFSET], dist[x+OFFSET][y+OFFSET] + 1)) {\n que.push(make_pair(nx,ny));\n }\n }\n }\n\n\n int ans = 0;\n for (int x=-OFFSET; x<=OFFSET; x++) {\n for (int y=-OFFSET; y<=OFFSET; y++) {\n if (dist[x+OFFSET][y+OFFSET] <= T) {\n ans++;\n }\n }\n }\n cout << ans << endl;\n dbg(\"end\");\n}\n\nint main() {\n while (true) {\n dbg(\"loop\");\n int t, n;\n cin >> t >> n;\n dbg(t);\n dbg(n);\n if (t == 0 && n == 0) break;\n solve(t,n);\n }\n}", "accuracy": 1, "time_ms": 170, "memory_kb": 4992, "score_of_the_acc": -0.2475, "final_rank": 16 }, { "submission_id": "aoj_2253_10617195", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nbool solve() {\n int T, N;\n cin >> T >> N;\n if (T == 0 && N == 0) return false;\n const int M = 100;\n vector<vector<bool>> B(2 * M, vector<bool>(2 * M, false));\n while (N--) {\n int r, c;\n cin >> r >> c;\n B[r + M][c + M] = true;\n }\n int sr, sc;\n cin >> sr >> sc;\n vector<vector<int>> D(2 * M, vector<int>(2 * M, 1 << 30));\n D[sr + M][sc + M] = 0;\n queue<pair<int, int>> q;\n q.emplace(sr + M, sc + M);\n int dr[6] = {0, 1, 0, -1, 1, -1};\n int dc[6] = {1, 0, -1, 0, 1, -1};\n while (q.size()) {\n auto [r, c] = q.front();\n q.pop();\n for (int d = 0; d < 6; d++) {\n int nr = r + dr[d], nc = c + dc[d];\n if (0 <= nr && nr < 2 * M && 0 <= nc && nc < 2 * M && !B[nr][nc]) {\n if (D[nr][nc] > D[r][c] + 1) {\n D[nr][nc] = D[r][c] + 1;\n q.emplace(nr, nc);\n }\n }\n }\n }\n int ans = 0;\n for (int i = 0; i < 2 * M; i++) {\n for (int j = 0; j < 2 * M; j++) {\n if (D[i][j] <= T) ans++;\n }\n }\n cout << ans << '\\n';\n return true;\n}\n\nint main() {\n while (solve()) {}\n}", "accuracy": 1, "time_ms": 120, "memory_kb": 3840, "score_of_the_acc": -0.1001, "final_rank": 13 }, { "submission_id": "aoj_2253_10614053", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing a2 = array<ll, 2>;\nusing a3 = array<ll, 3>;\n\ntemplate <typename A> void chmin(A &l, const A &r) {\n if(r < l)\n l = r;\n}\ntemplate <typename A> void chmax(A &l, const A &r) {\n if(l < r)\n l = r;\n}\n\nll mod = 998244353;\n\nvector<char> isprime;\nvoid init() {\n isprime.assign(1000005, 1);\n isprime[0] = 0;\n isprime[1] = 0;\n for(int i = 2; i < 1000005; i++) {\n if(isprime[i]) {\n for(int j = i * 2; j < 1000005; j += i) {\n isprime[j] = 0;\n }\n }\n }\n}\n\nll n, m, s, t;\nvector<a2> v;\nvoid input() {\n cin >> m >> n;\n v.resize(n);\n for(auto &x : v)\n cin >> x[0] >> x[1];\n cin >> s >> t;\n}\nvoid solve() {\n set<a2> sh;\n for(auto &x : v)\n sh.insert(x);\n\n set<a2> ok;\n queue<pair<a2, int>> que;\n que.push({{s, t}, 0});\n ok.insert({s, t});\n\n vector<a2> adj = {{0, 1}, {1, 1}, {1, 0}, {0, -1}, {-1, -1}, {-1, 0}};\n\n while(que.size()) {\n auto f = que.front();\n que.pop();\n\n for(auto &x : adj) {\n ll p = f.first[0] + x[0];\n ll q = f.first[1] + x[1];\n\n if(ok.count({p, q}) == 0 && sh.count({p, q}) == 0 &&\n f.second + 1 <= m) {\n que.push({{p, q}, f.second + 1});\n ok.insert({p, q});\n }\n }\n }\n cout << ok.size() << endl;\n}\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(0);\n\n // init();\n while(1) {\n input();\n if(n == 0 && m == 0)\n break;\n solve();\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 3664, "score_of_the_acc": -0.0733, "final_rank": 12 }, { "submission_id": "aoj_2253_10561013", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, n) for (ll i = 0; i < n; ++i)\n\nint main() {\n while(1){\n ll t,n;\n cin >> t >> n;\n if(t == 0)break;\n set<pair<ll,ll>> obstacle;\n set<pair<ll,ll>> cnt;\n queue<pair<pair<ll,ll>,ll>> q;\n rep(i, n){\n ll x,y;\n cin >> x >> y;\n obstacle.insert({x,y});\n }\n ll startx,starty;\n cin >> startx >> starty;\n q.push({{startx,starty},0ll});\n cnt.insert({startx,starty});\n while(!q.empty()){\n ll nowx = q.front().first.first;\n ll nowy = q.front().first.second;\n ll nowcost = q.front().second;\n q.pop();\n if(nowcost >= t)continue;\n for(ll i = -1; i <= 1; i++){\n for(ll j = -1; j <= 1; j++){\n if(i == 1 && j == -1)continue;\n if(j == 1 && i == -1)continue;\n ll nx = nowx + i;\n ll ny = nowy + j;\n if(!cnt.count({nx,ny}) && !obstacle.count({nx,ny})){\n cnt.insert({nx,ny});\n q.push({{nx,ny},nowcost + 1});\n }\n }\n }\n }\n cout << cnt.size() << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 3508, "score_of_the_acc": -0.0543, "final_rank": 8 }, { "submission_id": "aoj_2253_10417673", "code_snippet": "/*\n#include \"all.hpp\"\n\nint main() {\n io_setup();\n}\n*/\n\n#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n auto nxt = [](pair<int, int> xy) -> vector<pair<int, int>>{\n auto [x, y] = xy;\n return {\n {x, y+1},\n {x+1, y+1},\n {x-1, y},\n {x+1, y},\n {x-1, y-1},\n {x, y-1},\n };\n };\n while(true) {\n int t, n;\n cin >> t >> n;\n if(t == 0) exit(0);\n set<pair<int, int>> block;\n for(int i=0; i<n; i++) {\n int u, v;\n cin >> u >> v;\n block.insert({u, v});\n }\n pair<int, int> start;\n cin >> start.first >> start.second;\n map<pair<int, int>, int> dist;\n for(int x=-60; x<=60; x++) \n for(int y=-60; y<=60; y++) \n dist[{x, y}] = 1'000'000'000;\n deque<pair<int,int>> q;\n q.push_back(start);\n dist[start] = 0;\n while(!q.empty()) {\n auto cur = q.front();\n q.pop_front();\n auto d = dist[cur];\n for(auto c : nxt(cur)) {\n if(dist[c] <= d+1) continue;\n if(block.contains(c)) continue;\n dist[c] = d+1;\n q.push_back(c);\n }\n }\n int ans = 0;\n for(int x=-60; x<=60; x++) \n for(int y=-60; y<=60; y++) \n if(dist[{x, y}] <= t) ans++;\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 1800, "memory_kb": 4608, "score_of_the_acc": -0.4176, "final_rank": 17 }, { "submission_id": "aoj_2253_10417544", "code_snippet": "// https://codeforces.com/blog/entry/96344\n//#pragma GCC optimize(\"O3,unroll-loops\")\n//#pragma GCC target(\"avx2,bmi,bmi2,lzcnt,popcnt\")\n\n#include <bits/stdc++.h>\n\nusing namespace std;\n\nusing ll = long long;\nusing u32 = unsigned int;\nusing u64 = unsigned long long;\n\n#if __cplusplus >= 202002L\nusing i128 = __int128;\nusing u128 = unsigned __int128;\nusing f128 = __float128;\n#endif\n\ntemplate<typename T>\nbool chmax(T& a, const T& b) {\n bool res = a < b;\n a = max(a, b);\n return res;\n}\ntemplate<typename T>\nbool chmin(T& a, const T& b){\n bool res = a > b;\n a = min(a, b);\n return res;\n}\n\ntypedef vector<long long> vl;\ntypedef pair<ll,ll> pll;\ntypedef vector<pair<ll, ll>> vll;\ntypedef vector<int> vi;\ntypedef vector<pair<int,int>> vii;\ntypedef pair<int,int> pii;\n\nconst int inf = 1000000009;\nconst ll linf = 4000000000000000009;\n\n\n// https://trap.jp/post/1224/\ntemplate<class... T>\nvoid input(T&... a){\n (cin >> ... >> a);\n}\n\nvoid print(){\n cout << '\\n';\n}\ntemplate<class T, class... Ts>\nvoid print(const T& a, const Ts&... b){\n cout << a;\n (cout << ... << (cout << ' ', b));\n cout << '\\n';\n}\n\ntemplate <typename T>\nT floor(T a, T b) {\n return a / b - (a % b && (a ^ b) < 0);\n}\ntemplate <typename T>\nT ceil(T x, T y) {\n return floor(x + y - 1, y);\n}\n\nint popcnt(int x) { return __builtin_popcount(x); }\nint popcnt(u32 x) { return __builtin_popcount(x); }\nint popcnt(ll x) { return __builtin_popcountll(x); }\nint popcnt(u64 x) { return __builtin_popcountll(x); }\nint popcnt_mod_2(int x) { return __builtin_parity(x); }\nint popcnt_mod_2(u32 x) { return __builtin_parity(x); }\nint popcnt_mod_2(ll x) { return __builtin_parityll(x); }\nint popcnt_mod_2(u64 x) { return __builtin_parityll(x); }\n// (0, 1, 2, 3, 4) -> (-1, 0, 1, 1, 2)\nint topbit(int x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); }\nint topbit(u32 x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); }\nint topbit(ll x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }\nint topbit(u64 x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }\n// (0, 1, 2, 3, 4) -> (-1, 0, 1, 0, 2)\nint lowbit(int x) { return (x == 0 ? -1 : __builtin_ctz(x)); }\nint lowbit(u32 x) { return (x == 0 ? -1 : __builtin_ctz(x)); }\nint lowbit(ll x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }\nint lowbit(u64 x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }\n\ntemplate<typename T, typename U>\nT SUM(const vector<U> &A){\n T ret = 0;\n for (auto &&a: A) ret += a;\n return ret;\n}\n\n#define rep1(a) for(int i = 0; i < a; i++)\n#define rep2(i, a) for(int i = 0; i < a; i++)\n#define rep3(i, a, b) for(int i = a; i < b; i++)\n#define rep4(i, a, b, c) for(int i = a; i < b; i += c)\n#define overload4(a, b, c, d, e, ...) e\n#define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)\n\n#define rrep(i, a, b) for(int i = a; i >= b; i--)\n\n#define eb emplace_back\n#define mp make_pair\n#define mt make_tuple\n#define fi first\n#define se second\n#define all(v) v.begin(), v.end()\n//---------------------------------\n\nint dx[] = {0, 1, 1, 0, -1, -1};\nint dy[] = {1, 1, 0, -1, -1, 0};\n\nvoid solve(int t, int n){\n int a[1000][1000];\n rep(i, 1000) rep(j, 1000){\n a[i][j] = -1;\n }\n rep(i, n){\n int x,y; cin >> x >> y;\n a[x+500][y+500] = -2;\n }\n int ans = 1;\n int sx, sy; cin >> sx >> sy;\n a[sx+500][sy+500] = 0;\n deque<pii> d;\n d.push_back(mp(sx, sy));\n while(!d.empty()){\n auto [x, y] = d.front();\n d.pop_front();\n if(a[x+500][y+500] >= t) continue;\n rep(k, 6){\n int nx = x + dx[k];\n int ny = y + dy[k];\n if(a[nx+500][ny+500]==-1){\n a[nx+500][ny+500] = a[x+500][y+500] + 1;\n ans++;\n d.push_back(mp(nx, ny));\n }\n }\n }\n print(ans);\n}\n\n\nint main(){\n ios::sync_with_stdio(false);\n cin.tie(0);\n\n int t, n;\n cin >> t >> n;\n while(t > 0){\n solve(t, n);\n cin >> t >> n;\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 7360, "score_of_the_acc": -0.5167, "final_rank": 18 }, { "submission_id": "aoj_2253_10417425", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\nusing pii = pair<int, int>;\nusing pll = pair<ll, ll>;\nusing vi = vector<int>;\nusing vl = vector<ll>;\n#define rep3(i, a, b, c) for (ll i = (a); i < (b); i += (c))\n#define rep2(i, a, b) rep3(i, a, b, 1)\n#define rep1(i, n) rep2(i, 0, n)\n#define rep0(n) rep1(aaaaa, n)\n#define ov4(a, b, c, d, name, ...) name\n#define rep(...) ov4(__VA_ARGS__, rep3, rep2, rep1, rep0)(__VA_ARGS__)\n#define per(i, a, b) for (ll i = (a) - 1; i >= (b); i--)\n#define fore(e, v) for (auto&& e : v)\n#define all(a) begin(a), end(a)\n#define sz(a) (int)(size(a))\n#define lb(v, x) (lower_bound(all(v), x) - begin(v))\n#define eb emplace_back\n\ntemplate <typename T, typename S>\nbool chmin(T& a, const S& b) {\n return a > b ? a = b, 1 : 0;\n}\ntemplate <typename T, typename S>\nbool chmax(T& a, const S& b) {\n return a < b ? a = b, 1 : 0;\n}\nconst int INF = 1e9 + 100;\nconst ll INFL = 3e18 + 100;\n#define i128 __int128_t\nstruct _ {\n _() { cin.tie(0)->sync_with_stdio(0), cout.tie(0); }\n} __;\n\nvoid solve();\nint main(){\n while(1)solve();\n}\n\nvoid solve(){\n int T,N;\n cin>>T>>N;\n if(N==0&&T==0)exit(0);\n using ai2= array<int,2>;\n vector<ai2> ob(N);\n fore(i,ob)cin>>i[0]>>i[1];\n ai2 S;\n cin>>S[0]>>S[1];\n S[0]+=65,S[1]+=65;\n vector<vi>dist (140,vi(140,1e9));\n fore(i,ob){\n dist[i[0]+65][i[1]+65]=-1;\n }\n queue<ai2>bfs;\n vector<ai2> n={ai2{0,1},ai2{1,1},ai2{1,0},ai2{0,-1},ai2{-1,-1},ai2{-1,0}};\n bfs.push(ai2{S[0],S[1]});\n dist[S[0]][S[1]]=0;\n auto bound_ok=[&](int x,int y){\n return x>=0&&x<140&&y>=0&&y<140&&dist[x][y]!=-1;\n };\n while(bfs.size()){\n auto [x,y]=bfs.front();\n int d=dist[x][y];\n bfs.pop();\n fore(i,n){\n int x2=x+i[0],y2=y+i[1];\n if(bound_ok(x2,y2)&&dist[x2][y2]>d+1){\n dist[x2][y2]=d+1;\n bfs.push(ai2{x2,y2});\n }\n }\n }\n int ans=0;\n rep(i,140){\n rep(j,140){\n if(dist[i][j]!=-1&&dist[i][j]<=T)ans++;\n }\n }\n cout<<ans<<'\\n';\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 3456, "score_of_the_acc": -0.0439, "final_rank": 5 }, { "submission_id": "aoj_2253_10417098", "code_snippet": "/**\n\tauthor: shobonvip\n\tcreated: 2025.04.24 17:46:57\n**/\n\n#include<bits/stdc++.h>\nusing namespace std;\n\ntypedef long long ll;\n\n#define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++)\n#define rrep(i, s, n) for (int i = (int)(n)-1; i >= (int)(s); i--)\n#define all(v) v.begin(), v.end()\n\ntemplate <typename T> bool chmin(T &a, const T &b) {\n\tif (a <= b) return false;\n\ta = b;\n\treturn true;\n}\n\ntemplate <typename T> bool chmax(T &a, const T &b) {\n\tif (a >= b) return false;\n\ta = b;\n\treturn true;\n}\n\ntemplate <typename T> T max(vector<T> &a){\n\tassert(!a.empty());\n\tT ret = a[0];\n\tfor (int i=0; i<(int)a.size(); i++) chmax(ret, a[i]);\n\treturn ret;\n}\n\ntemplate <typename T> T min(vector<T> &a){\n\tassert(!a.empty());\n\tT ret = a[0];\n\tfor (int i=0; i<(int)a.size(); i++) chmin(ret, a[i]);\n\treturn ret;\n}\n\ntemplate <typename T> T sum(vector<T> &a){\n\tT ret = 0;\n\tfor (int i=0; i<(int)a.size(); i++) ret += a[i];\n\treturn ret;\n}\n\n\n\nvoid solve(int t, int n) {\n\n\tint mx = 150;\n\tint geta = 75;\n\tvector c(mx, vector<bool>(mx, true));\n\trep(i,0,n) {\n\t\tint x, y; cin >> x >> y;\n\t\tc[x + geta][y + geta] = false;\n\t}\n\n\n\tint sx, sy;\n\tcin >> sx >> sy;\n\tsx += geta;\n\tsy += geta;\n\tvector d(mx, vector<int>(mx, 1e9));\n\td[sx][sy] = 0;\n\tdeque<pair<int,int>> mada;\n\tmada.push_back(pair(sx, sy));\n\twhile(!mada.empty()){\n\t\tauto [i, j] = mada.front();\n\t\tmada.pop_front();\n\t\tfor (auto [x, y]:{\n\t\t\tpair(i, j+1),\n\t\t\tpair(i+1, j+1),\n\t\t\tpair(i+1, j),\n\t\t\tpair(i, j-1),\n\t\t\tpair(i-1, j-1),\n\t\t\tpair(i-1, j)\n\t\t}) {\n\t\t\tif (!(0<=x&&x<mx))continue;\n\t\t\tif (!(0<=y&&y<mx))continue;\n\t\t\tif (!c[x][y])continue;\n\t\t\tif(chmin(d[x][y],d[i][j]+1)){\n\t\t\t\tmada.push_back(pair(x,y));\n\t\t\t}\n\t\t}\n\t}\n\tint cnt=0;\n\trep(i,0,mx){\n\t\trep(j,0,mx){\n\t\t\tif(d[i][j]<=t){\n\t\t\t\tcnt++;\n\t\t\t}\n\t\t}\n\t}\n\tcout<<cnt<<endl;\n\t\n}\n\nint main(){\n cin.tie(0)->sync_with_stdio(0);\n\twhile(true) {\n\t\tint h, w; cin >> h >> w;\n\t\tif (h == 0 && w == 0) break;\n\t\tsolve(h, w);\n\t}\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 3476, "score_of_the_acc": -0.0504, "final_rank": 6 }, { "submission_id": "aoj_2253_9711166", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define ALL(v) v.begin(),v.end()\n#define dbg(x) cerr << #x << \": \" << (x) << endl;\nint t,n;\nvector<int> x,y;\nint sx,sy;\nint dx[] = {1,0,-1,-1,0,1};\nint dy[] = {0,-1,-1,0,1,1};\nint solve() {\n cin >> t >> n;\n if (t+n==0) exit(0);\n x.resize(n);\n y.resize(n);\n for (int i = 0; i < n; ++i) {\n cin >> x[i] >> y[i];\n }\n cin >> sx >> sy;\n\n vector<vector<int>> dis(125, vector<int>(125, 1e9));\n int offset = 62;\n for (int i = 0; i < n; ++i) {\n x[i] -= sx;\n y[i] -= sy;\n x[i] += offset;\n y[i] += offset;\n dis[y[i]][x[i]] = 0;\n }\n\n using pii = pair<int,int>;\n queue<pii> q;\n q.emplace(offset, offset);\n dis[offset][offset] = 0;\n int cnt = 1;\n while (q.size()) {\n auto [xx,yy] = q.front(); q.pop();\n if (dis[yy][xx] == t) continue;\n for (int i = 0; i < 6; ++i) {\n int nx = xx + dx[i];\n int ny = yy + dy[i];\n if (dis[ny][nx] > dis[yy][xx] + 1) {\n dis[ny][nx] = dis[yy][xx] + 1;\n q.emplace(nx,ny);\n ++cnt;\n }\n }\n }\n return cnt;\n}\nint main() {\n while (true) {\n cout << solve() << '\\n';\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3260, "score_of_the_acc": -0.0147, "final_rank": 2 }, { "submission_id": "aoj_2253_9458402", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n#include <unistd.h>\n\nnamespace fastio {\nstatic constexpr uint32_t SZ = 1 << 17;\nchar ibuf[SZ];\nchar obuf[SZ];\nchar out[100];\n// pointer of ibuf, obuf\n\n\nuint32_t pil = 0, pir = 0, por = 0;\n\nstruct Pre {\n char num[10000][4];\n constexpr Pre() : num() {\n for (int i = 0; i < 10000; i++) {\n int n = i;\n for (int j = 3; j >= 0; j--) {\n num[i][j] = n % 10 | '0';\n n /= 10;\n }\n }\n }\n} constexpr pre;\n\ninline void load() {\n memmove(ibuf, ibuf + pil, pir - pil);\n pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin);\n pil = 0;\n if (pir < SZ)\n ibuf[pir++] = '\\n';\n}\n\ninline void flush() {\n fwrite(obuf, 1, por, stdout);\n por = 0;\n}\n\nvoid rd(char &c) {\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n}\n\nvoid rd(string &x) {\n x.clear();\n char c;\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n do {\n x += c;\n if (pil == pir)\n load();\n c = ibuf[pil++];\n } while (!isspace(c));\n}\n\ntemplate <typename T> void rd_real(T &x) {\n string s;\n rd(s);\n x = stod(s);\n}\n\ntemplate <typename T> void rd_integer(T &x) {\n if (pil + 100 > pir)\n load();\n char c;\n do\n c = ibuf[pil++];\n while (c < '-');\n bool minus = 0;\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (c == '-') {\n minus = 1, c = ibuf[pil++];\n }\n }\n x = 0;\n while ('0' <= c) {\n x = x * 10 + (c & 15), c = ibuf[pil++];\n }\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (minus)\n x = -x;\n }\n}\n\nvoid rd(int &x) {\n rd_integer(x);\n}\nvoid rd(ll &x) {\n rd_integer(x);\n}\nvoid rd(i128 &x) {\n rd_integer(x);\n}\nvoid rd(uint &x) {\n rd_integer(x);\n}\nvoid rd(ull &x) {\n rd_integer(x);\n}\nvoid rd(u128 &x) {\n rd_integer(x);\n}\nvoid rd(double &x) {\n rd_real(x);\n}\nvoid rd(long double &x) {\n rd_real(x);\n}\n\ntemplate <class T, class U> void rd(pair<T, U> &p) {\n return rd(p.first), rd(p.second);\n}\ntemplate <size_t N = 0, typename T> void rd_tuple(T &t) {\n if constexpr (N < std::tuple_size<T>::value) {\n auto &x = std::get<N>(t);\n rd(x);\n rd_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void rd(tuple<T...> &tpl) {\n rd_tuple(tpl);\n}\n\ntemplate <size_t N = 0, typename T> void rd(array<T, N> &x) {\n for (auto &d : x)\n rd(d);\n}\ntemplate <class T> void rd(vector<T> &x) {\n for (auto &d : x)\n rd(d);\n}\n\nvoid read() {}\ntemplate <class H, class... T> void read(H &h, T &...t) {\n rd(h), read(t...);\n}\n\nvoid wt(const char c) {\n if (por == SZ)\n flush();\n obuf[por++] = c;\n}\nvoid wt(const string s) {\n for (char c : s)\n wt(c);\n}\nvoid wt(const char *s) {\n size_t len = strlen(s);\n for (size_t i = 0; i < len; i++)\n wt(s[i]);\n}\n\ntemplate <typename T> void wt_integer(T x) {\n if (por > SZ - 100)\n flush();\n if (x < 0) {\n obuf[por++] = '-', x = -x;\n }\n int outi;\n for (outi = 96; x >= 10000; outi -= 4) {\n memcpy(out + outi, pre.num[x % 10000], 4);\n x /= 10000;\n }\n if (x >= 1000) {\n memcpy(obuf + por, pre.num[x], 4);\n por += 4;\n } else if (x >= 100) {\n memcpy(obuf + por, pre.num[x] + 1, 3);\n por += 3;\n } else if (x >= 10) {\n int q = (x * 103) >> 10;\n obuf[por] = q | '0';\n obuf[por + 1] = (x - q * 10) | '0';\n por += 2;\n } else\n obuf[por++] = x | '0';\n memcpy(obuf + por, out + outi + 4, 96 - outi);\n por += 96 - outi;\n}\n\ntemplate <typename T> void wt_real(T x) {\n ostringstream oss;\n oss << fixed << setprecision(15) << double(x);\n string s = oss.str();\n wt(s);\n}\n\nvoid wt(int x) {\n wt_integer(x);\n}\nvoid wt(ll x) {\n wt_integer(x);\n}\nvoid wt(i128 x) {\n wt_integer(x);\n}\nvoid wt(uint x) {\n wt_integer(x);\n}\nvoid wt(ull x) {\n wt_integer(x);\n}\nvoid wt(u128 x) {\n wt_integer(x);\n}\nvoid wt(double x) {\n wt_real(x);\n}\nvoid wt(long double x) {\n wt_real(x);\n}\n\ntemplate <class T, class U> void wt(const pair<T, U> val) {\n wt(val.first);\n wt(' ');\n wt(val.second);\n}\ntemplate <size_t N = 0, typename T> void wt_tuple(const T t) {\n if constexpr (N < std::tuple_size<T>::value) {\n if constexpr (N > 0) {\n wt(' ');\n }\n const auto x = std::get<N>(t);\n wt(x);\n wt_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void wt(tuple<T...> tpl) {\n wt_tuple(tpl);\n}\ntemplate <class T, size_t S> void wt(const array<T, S> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\ntemplate <class T> void wt(const vector<T> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\n\nvoid print() {\n wt('\\n');\n}\ntemplate <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {\n wt(head);\n if (sizeof...(Tail))\n wt(' ');\n print(forward<Tail>(tail)...);\n}\nvoid __attribute__((destructor)) _d() {\n flush();\n}\n} // namespace fastio\n\n\nusing fastio::flush;\nusing fastio::print;\nusing fastio::read;\n\ninline void first(bool i = true) {\n print(i ? \"first\" : \"second\");\n}\ninline void Alice(bool i = true) {\n print(i ? \"Alice\" : \"Bob\");\n}\ninline void Takahashi(bool i = true) {\n print(i ? \"Takahashi\" : \"Aoki\");\n}\ninline void yes(bool i = true) {\n print(i ? \"yes\" : \"no\");\n}\ninline void Yes(bool i = true) {\n print(i ? \"Yes\" : \"No\");\n}\ninline void No() {\n print(\"No\");\n}\ninline void YES(bool i = true) {\n print(i ? \"YES\" : \"NO\");\n}\ninline void NO() {\n print(\"NO\");\n}\ninline void Yay(bool i = true) {\n print(i ? \"Yay!\" : \":(\");\n}\ninline void Possible(bool i = true) {\n print(i ? \"Possible\" : \"Impossible\");\n}\ninline void POSSIBLE(bool i = true) {\n print(i ? \"POSSIBLE\" : \"IMPOSSIBLE\");\n}\n\n/**\n * @brief Fast IO\n */\n\nint dx[6] = {1,0,-1,-1,0,1}, dy[6] = {1,1,0,-1,-1,0};\n\nint main() {\nwhile(1) {\n int N, M;\n cin >> N >> M;\n if (N == 0 && M == 0) return 0;\n bool X[201][201] = {};\n rep(i,0,201) rep(j,0,201) X[i][j] = true;\n rep(i,0,M) {\n int A, B;\n cin >> A >> B;\n A += 100, B += 100;\n X[A][B] = false;\n }\n int SX, SY;\n cin >> SX >> SY;\n SX += 100, SY += 100;\n queue<pair<int,int>> Q;\n vector<vector<int>> D(201,vector<int>(201,inf));\n D[SX][SY] = 0;\n Q.push({SX,SY});\n int ANS = 1;\n while(!Q.empty()) {\n pair<int,int> P = Q.front();\n Q.pop();\n if (D[P.first][P.second] == N) continue;\n rep(i,0,6) {\n int NX = P.first + dx[i], NY = P.second + dy[i];\n if (X[NX][NY]) {\n if (D[NX][NY] == inf) {\n D[NX][NY] = D[P.first][P.second] + 1;\n ANS++;\n Q.push({NX,NY});\n }\n }\n }\n }\n cout << ANS << endl;\n}\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3624, "score_of_the_acc": -0.0591, "final_rank": 10 }, { "submission_id": "aoj_2253_9402799", "code_snippet": "#include <bits/stdc++.h>\n//#include <atcoder/all>\nusing namespace std;\n\nusing ll = long long;\nusing i64 = long long;\nusing pll = pair<ll,ll>;\nusing plll = pair<pll,ll>;\nusing pii =pair<int,int>;\n\nconstexpr ll mod = 998244353;\n\nint dx[4]={1,0,-1,0};\nint dy[4]={0,1,0,-1};\nint dX[4]={1,0,-1,0};\nint dY[4]={0,-1,0,1};\n\n//library\n\n\n//end\n\n\nint main(){\n while (true){\n int t,n;\n cin>>t>>n;\n if (t==0)break;\n set<pii> bad;\n for (int i=0; i<n; i++){\n int x,y ;\n cin>>x>>y;\n bad.emplace(x,y);\n }\n\n int x,y;\n cin>>x>>y;\n\n queue<tuple<int,int,int>> que;\n que.emplace(x,y,0);\n set<pii> visited;\n\n int ddx[6]={1,0,-1,0,1,-1};\n int ddy[6]={0,1,0,-1,1,-1};\n while (que.size()){\n auto [x,y,now] =que.front();\n que.pop();\n if (visited.count(make_pair(x,y)))continue;\n visited.insert(make_pair(x,y));\n if (now==t)continue;\n\n for (int d=0; d<6; d++){\n int nx=x+ddx[d],ny=y+ddy[d];\n //if (abs(nx)>30 || abs(ny)>30)continue;\n pii pr(nx,ny);\n if (bad.count(pr) || visited.count(pr))continue;\n que.emplace(nx,ny,now+1);\n }\n\n }\n cout<<visited.size()<<endl;\n\n\n\n\n\n }\n\n}", "accuracy": 1, "time_ms": 90, "memory_kb": 3388, "score_of_the_acc": -0.0409, "final_rank": 3 }, { "submission_id": "aoj_2253_9361425", "code_snippet": "#include <bits/stdc++.h>\n#define rep(i, n) for (int i = 0; i < (int)(n); i++)\n#define all(x) (x).begin(),(x).end()\n#define eb emplace_back\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing vl = vector<long long>;\nusing vvl = vector<vector<long long>>; using vs = vector<string>;\nusing pl = pair<long long, long long>;\ntemplate <typename T> inline bool chmin(T& a, const T& b) {bool compare = a > b; if (a > b) a = b; return compare;}\ntemplate <typename T> inline bool chmax(T& a, const T& b) {bool compare = a < b; if (a < b) a = b; return compare;}\ntemplate<class T>using rp_queue=priority_queue<T,vector<T>,greater<T>>;\ntemplate <typename T> T gcd(T a, T b) {if (b == 0)return a; else return gcd(b, a % b);}\ntemplate <typename T> inline T lcm(T a, T b) {return a /gcd(a, b)*b;}\nconst ll INF = 1LL << 60;\nconst ld PI = acos(-1);\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::vector<T> &vec);\ntemplate <class OStream, class T, size_t sz> OStream &operator<<(OStream &os, const std::array<T, sz> &arr);\ntemplate <class OStream, class T, class TH> OStream &operator<<(OStream &os, const std::unordered_set<T, TH> &vec);\ntemplate <class OStream, class T, class U> OStream &operator<<(OStream &os, const pair<T, U> &pa);\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::deque<T> &vec);\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::set<T> &vec);\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::multiset<T> &vec);\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::unordered_multiset<T> &vec);\ntemplate <class OStream, class T, class U> OStream &operator<<(OStream &os, const std::pair<T, U> &pa);\ntemplate <class OStream, class TK, class TV> OStream &operator<<(OStream &os, const std::map<TK, TV> &mp);\ntemplate <class OStream, class TK, class TV, class TH> OStream &operator<<(OStream &os, const std::unordered_map<TK, TV, TH> &mp);\ntemplate <class OStream, class... T> OStream &operator<<(OStream &os, const std::tuple<T...> &tpl);\n \ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::vector<T> &vec) { os << '['; for (auto v : vec) os << v << ','; os << ']'; return os; }\ntemplate <class OStream, class T, size_t sz> OStream &operator<<(OStream &os, const std::array<T, sz> &arr) { os << '['; for (auto v : arr) os << v << ','; os << ']'; return os; }\ntemplate <class... T> std::istream &operator>>(std::istream &is, std::tuple<T...> &tpl) { std::apply([&is](auto &&... args) { ((is >> args), ...);}, tpl); return is; }\ntemplate <class OStream, class... T> OStream &operator<<(OStream &os, const std::tuple<T...> &tpl) { os << '('; std::apply([&os](auto &&... args) { ((os << args << ','), ...);}, tpl); return os << ')'; }\ntemplate <class OStream, class T, class TH> OStream &operator<<(OStream &os, const std::unordered_set<T, TH> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::deque<T> &vec) { os << \"deq[\"; for (auto v : vec) os << v << ','; os << ']'; return os; }\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::set<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::multiset<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::unordered_multiset<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }\ntemplate <class OStream, class T, class U> OStream &operator<<(OStream &os, const std::pair<T, U> &pa) { return os << '(' << pa.first << ',' << pa.second << ')'; }\ntemplate <class OStream, class TK, class TV> OStream &operator<<(OStream &os, const std::map<TK, TV> &mp) { os << '{'; for (auto v : mp) os << v.first << \"=>\" << v.second << ','; os << '}'; return os; }\ntemplate <class OStream, class TK, class TV, class TH> OStream &operator<<(OStream &os, const std::unordered_map<TK, TV, TH> &mp) { os << '{'; for (auto v : mp) os << v.first << \"=>\" << v.second << ','; os << '}'; return os; }\n#ifdef LOCAL\nconst string COLOR_RESET = \"\\033[0m\", BRIGHT_GREEN = \"\\033[1;32m\", BRIGHT_RED = \"\\033[1;31m\", BRIGHT_CYAN = \"\\033[1;36m\", NORMAL_CROSSED = \"\\033[0;9;37m\", RED_BACKGROUND = \"\\033[1;41m\", NORMAL_FAINT = \"\\033[0;2m\";\n#define dbg(x) std::cerr << BRIGHT_CYAN << #x << COLOR_RESET << \" = \" << (x) << NORMAL_FAINT << \" (L\" << __LINE__ << \") \" << __FILE__ << COLOR_RESET << std::endl\n#define dbgif(cond, x) ((cond) ? std::cerr << BRIGHT_CYAN << #x << COLOR_RESET << \" = \" << (x) << NORMAL_FAINT << \" (L\" << __LINE__ << \") \" << __FILE__ << COLOR_RESET << std::endl : std::cerr)\n#else\n#define dbg(x) ((void)0)\n#define dbgif(cond, x) ((void)0)\n#endif\nvoid solve();\nint main(){\n cin.tie(nullptr);\n ios_base::sync_with_stdio(false);\n cout<<fixed<<setprecision(15);\n int num_tc = 1;\n //cin >> num_tc;\n rep(tc,num_tc){\n //cout << \"Case #\" << tc+1 << \": \" ;// << endl;\n solve();\n }\n}\n//見切り発車で実装しては行けない.頭の中でコードが完全に出来上がるまで書き始めるな.\nll dx[] = {0,1,1,0,-1,-1};\nll dy[] = {1,1,0,-1,-1,0};\nvoid solve(){\n while(true){\n int t, n;cin>>t>>n;\n if(t==0&&n==0)return;\n set<pl> ng;\n rep(i,n){\n ll x,y;cin>>x>>y;\n ng.insert({x,y});\n }\n ll sx,sy;cin>>sx>>sy;\n queue<pl> que;\n que.push({sx,sy});\n set<pl> seen;\n seen.insert({sx,sy});\n while(t--){\n queue<pl> nque;\n while(!que.empty()){\n auto [x,y] = que.front();\n que.pop();\n rep(d,6){\n int nx = x+dx[d];\n int ny = y+dy[d];\n if(!ng.count({nx,ny})&&!seen.count({nx,ny})){\n seen.insert({nx,ny});\n nque.push({nx,ny});\n }\n }\n }\n swap(nque,que);\n }\n cout<<seen.size()<<endl;\n }\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 3556, "score_of_the_acc": -0.0588, "final_rank": 9 }, { "submission_id": "aoj_2253_9347673", "code_snippet": "#include<bits/stdc++.h>\n//#include <atcoder/all>\nusing namespace std;\n//using namespace atcoder;\n\n#define all(v) v.begin(),v.end()\nusing ll = long long;\nusing ull = unsigned long long;\nusing vll=vector<ll>;\nusing vvll = vector<vector<ll>>;\nusing P = pair<ll,ll>;\nusing vp=vector<pair<ll, ll>>;\n//using mint=modint1000000007;\n//using mint=modint998244353;\n\nconst ll INF=1ll<<60;\nll mod10=1e9+7;\nll mod99=998244353;\nconst double PI = acos(-1);\n\n#define rep(i,n) for (ll i=0;i<n;++i)\n#define per(i,n) for(ll i=n-1;i>=0;--i)\n#define rep2(i,a,n) for (ll i=a;i<n;++i)\n#define per2(i,a,n) for (ll i=a;i>=n;--i)\n\ntemplate<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }\ntemplate<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }\n\nvll dx={0,1,0,-1,1,-1},dy={1,0,-1,0,1,-1};\n\nbool solve(){\n ll t,N;cin>>t>>N;\n if(t==0&&N==0) return 0;\n vvll dist(1000,vll(1000,INF));\n set<P> st;\n rep(i,N){\n ll x,y;cin>>x>>y;\n x+=500,y+=500;\n st.insert({x,y});\n }\n\n ll sx,sy;\n cin>>sx>>sy;\n sx+=500,sy+=500;\n queue<P> q;\n q.push({sx,sy});\n dist[sx][sy]=0;\n while(q.size()){\n auto [nowi,nowj]=q.front();q.pop();\n if(dist[nowi][nowj]>t) continue;\n rep(k,6){\n ull toi=nowi+dx[k],toj=nowj+dy[k];\n if(!(toi<1000&&toj<1000)) continue;\n if(st.count({toi,toj})) continue;\n if(dist[toi][toj]!=INF) continue;\n q.push({toi,toj});\n dist[toi][toj]=dist[nowi][nowj]+1;\n }\n }\n\n ll ans=0;\n rep(i,1000) rep(j,1000) if(dist[i][j]<=t) ans++;\n cout<<ans<<endl;\n\n\n\n return 1;\n}\n\n\nint main(){\n cin.tie(0);\n ios::sync_with_stdio(false);\n while(solve());\n}", "accuracy": 1, "time_ms": 590, "memory_kb": 11236, "score_of_the_acc": -1.066, "final_rank": 19 }, { "submission_id": "aoj_2253_9347672", "code_snippet": "#include<bits/stdc++.h>\n//#include <atcoder/all>\nusing namespace std;\n//using namespace atcoder;\n\n#define all(v) v.begin(),v.end()\nusing ll = long long;\nusing ull = unsigned long long;\nusing vll=vector<ll>;\nusing vvll = vector<vector<ll>>;\nusing P = pair<ll,ll>;\nusing vp=vector<pair<ll, ll>>;\n//using mint=modint1000000007;\n//using mint=modint998244353;\n\nconst ll INF=1ll<<60;\nll mod10=1e9+7;\nll mod99=998244353;\nconst double PI = acos(-1);\n\n#define rep(i,n) for (ll i=0;i<n;++i)\n#define per(i,n) for(ll i=n-1;i>=0;--i)\n#define rep2(i,a,n) for (ll i=a;i<n;++i)\n#define per2(i,a,n) for (ll i=a;i>=n;--i)\n\ntemplate<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }\ntemplate<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }\n\nvll dx={0,1,0,-1,1,-1},dy={1,0,-1,0,1,-1};\n\nbool solve(){\n ll t,N;cin>>t>>N;\n if(t==0&&N==0) return 0;\n vvll dist(1000,vll(1000,INF));\n set<P> st;\n rep(i,N){\n ll x,y;cin>>x>>y;\n x+=500,y+=500;\n st.insert({x,y});\n }\n\n ll sx,sy;\n cin>>sx>>sy;\n sx+=500,sy+=500;\n queue<P> q;\n q.push({sx,sy});\n dist[sx][sy]=0;\n while(q.size()){\n auto [nowi,nowj]=q.front();q.pop();\n rep(k,6){\n ull toi=nowi+dx[k],toj=nowj+dy[k];\n if(!(toi<1000&&toj<1000)) continue;\n if(st.count({toi,toj})) continue;\n if(dist[toi][toj]!=INF) continue;\n q.push({toi,toj});\n dist[toi][toj]=dist[nowi][nowj]+1;\n }\n }\n\n ll ans=0;\n rep(i,1000) rep(j,1000) if(dist[i][j]<=t) ans++;\n cout<<ans<<endl;\n\n\n\n return 1;\n}\n\n\nint main(){\n cin.tie(0);\n ios::sync_with_stdio(false);\n while(solve());\n}", "accuracy": 1, "time_ms": 7520, "memory_kb": 11328, "score_of_the_acc": -2, "final_rank": 20 }, { "submission_id": "aoj_2253_9305331", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing ll=long long;\n#define rep(i,s,t) for(ll i=s;i<(ll)(t);i++)\n#define rrep(i,s,t) for(ll i=(ll)(t)-1;i>=(ll)s;i--)\n#define all(x) begin(x),end(x)\n#define rall(x) rbegin(x),rend(x)\n\n#define TT template<typename T>\nTT using vec=vector<T>;\nTT bool chmin(T &x,T y){return x>y?(x=y,true):false;}\nTT bool chmax(T &x,T y){return x<y?(x=y,true):false;}\n\nstruct io_setup{\n io_setup(){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout<<fixed<<setprecision(15);\n }\n} io_setup;\n\nconst int dx[]={0,1,1,0,-1,-1},dy[]={1,1,0,-1,-1,0};\nconst int M=100;\nconst int INF=1<<30;\n\nint solve(){\n int T,N;\n cin>>T>>N;\n if(T==0&&N==0)return 0;\n vector<int>X(N),Y(N);\n vector flag(2*M+1,vector<bool>(2*M+1));\n for(int i=0;i<N;i++){\n cin>>X[i]>>Y[i];\n X[i]+=M;\n Y[i]+=M;\n flag[X[i]][Y[i]]=true;\n }\n int sx,sy;\n cin>>sx>>sy;\n sx+=M;\n sy+=M;\n vector dp(2*M+1,vector<int>(2*M+1,INF));\n dp[sx][sy]=0;\n queue<tuple<int,int,int>>q;\n q.push(make_tuple(dp[sx][sy],sx,sy));\n while(q.size()){\n auto[c,x,y]=q.front();\n q.pop();\n if(dp[x][y]<c)continue;\n for(int i=0;i<6;i++){\n int ex=x+dx[i],ey=y+dy[i];\n if(0<=ex&&ex<=2*M&&0<=ey&&ey<=2*M&&c+1<=T&&!flag[ex][ey]&&chmin(dp[ex][ey],c+1)){\n q.push(make_tuple(dp[ex][ey],ex,ey));\n }\n }\n }\n int ans=0;\n for(int i=0;i<=2*M;i++){\n for(int j=0;j<=2*M;j++){\n if(dp[i][j]!=INF)ans++;\n }\n }\n cout<<ans<<\"\\n\";\n return 1;\n}\n\nint main(){\n while(solve());\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3640, "score_of_the_acc": -0.0611, "final_rank": 11 }, { "submission_id": "aoj_2253_9297547", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nconst int dx[]={0,1,1,0,-1,-1};\nconst int dy[]={1,1,0,-1,-1,0};\n\nint main(){\n\twhile(true){\n\t\tint T,N;cin>>T>>N;\n\n\t\tif(T==0)break;\n\n\t\tset<pair<int,int>>S;\n\t\tfor(int i=0;i<N;i++){\n\t\t\tint x,y;cin>>x>>y;\n\t\t\tS.insert({x,y});\n\t\t}\n\t\tint sx,sy;\n\t\tcin>>sx>>sy;\n\n\t\tqueue<pair<int,int>>Q;\n\t\tQ.push({sx,sy});\n\n\t\tset<pair<int,int>>seen;\n\t\tseen.insert({sx,sy});\n\n\t\tfor(int t=0;t<T;t++){\n\t\t\tvector<pair<int,int>>nQ;\n\n\t\t\twhile(!Q.empty()){\n\t\t\t\tauto[x,y]=Q.front();\n\t\t\t\tQ.pop();\n\n\t\t\t\tfor(int i=0;i<6;i++){\n\t\t\t\t\tint nx=x+dx[i];\n\t\t\t\t\tint ny=y+dy[i];\n\n\t\t\t\t\tif(seen.count({nx,ny})||S.count({nx,ny})){\n\t\t\t\t\t\tcontinue;\n\t\t\t\t\t}\n\n\t\t\t\t\tseen.insert({nx,ny});\n\t\t\t\t\tnQ.push_back({nx,ny});\n\t\t\t\t}\n\t\t\t}\n\t\t\t\n\t\t\tfor(auto x:nQ)Q.push(x);\n\t\t}\n\n\n\t\tcout<<seen.size()<<endl;\n\t}\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 3504, "score_of_the_acc": -0.0524, "final_rank": 7 }, { "submission_id": "aoj_2253_8978618", "code_snippet": "// (⁠◕⁠ᴗ⁠◕⁠✿⁠)\n\n#pragma GCC target(\"avx2\")\n#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"unroll-loops\")\n#include <bits/stdc++.h>\n#define rep(i, n) for (ll i = 0; i < (n); i++)\n#define srep(i, s, n) for (ll i = s; i < (n); i++)\n#define len(x) ((int)(x).size())\n#define all(x) (x).begin(), (x).end()\nusing namespace std;\ntemplate<typename T> using vc = vector<T>;\ntemplate<typename T> using vv = vc<vc<T>>;\ntemplate<typename T> using vvv = vv<vc<T>>;\nusing vi = vc<int>;using vvi = vv<int>; using vvvi = vv<vi>;\nusing ll = long long;using vl = vc<ll>;using vvl = vv<ll>; using vvvl = vv<vl>;\nusing ld = long double; using vld = vc<ld>; using vvld = vc<vld>; using vvvld = vc<vvld>;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nconst ld pi = 3.141592653589793;\nconst int inf = 0x3f3f3f3f;\nconst ll INF = 0x3f3f3f3f3f3f3f3f;\n// const ll mod = 1000000007;\nconst ll mod = 998244353;\ninline bool inside(ll y, ll x, ll H, ll W) {return 0 <= (y) and (y) < (H) and 0 <= (x) and (x) < (W); }\n\n#define debug(var) do{std::cout << #var << \" : \\n\";view(var);}while(0)\ntemplate<typename T> void view(T e){cout << e << endl;}\ntemplate<typename T> void view(const vc<T>& v){for(const auto& e : v){ cout << e << \" \"; } cout << endl;}\ntemplate<typename T> void view(const vv<T>& vv){ for(const auto& v : vv){ view(v); } }\n\nconst int p = 100;\n\nvi dx = {1, 1, 0, -1, -1, 0}, dy = {0, 1, 1, 0, -1, -1};\n\nint solve(int N, int T){\n vvi A(300, vi(300, inf));\n rep(i, N){\n int x, y; cin >> x >> y;\n A[x + p][y + p] = -1;\n }\n int sx, sy; cin >> sx >> sy;\n sx += p;\n sy += p;\n deque<pair<int, int>> q;\n q.push_back({sx, sy});\n int ans = 1;\n A[sx][sy] = 0;\n while (!q.empty()){\n auto [x, y] = q.front(); q.pop_front();\n if (A[x][y] == T) continue;\n rep(k, 6){\n int nx = x + dx[k], ny = y + dy[k];\n if (A[nx][ny] > A[x][y] + 1){\n A[nx][ny] = A[x][y] + 1;\n ans++;\n q.push_back({nx, ny});\n }\n }\n }\n return ans;\n}\n\nint main(){\n vc<int> ans;\n while (true){\n int T, N; cin >> T >> N;\n if (T == 0 && N == 0) break;\n ans.push_back(solve(N, T));\n }\n for (auto x : ans) cout << x << endl;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3476, "score_of_the_acc": -0.0424, "final_rank": 4 }, { "submission_id": "aoj_2253_8860907", "code_snippet": "#include <algorithm>\n#include <cmath>\n#include <cstdio>\n#include <cstring>\n#include <deque>\n#include <iostream>\n#include <vector>\nusing namespace std;\n#define rep(i, b) for (int i = 0; i < b; i++)\n#define m_t 60\n#define max (3 * m_t * (m_t + 1) + 1)\n#define zero (max / 2)\n#define mp make_pair\nint dx[6] = {0, 1, -1, 1, -1, 0};\nint dy[6] = {1, 1, 0, 0, -1, -1};\n\nstruct Node {\n int x, y, t;\n};\n\nint main() {\n int t, n;\n while (cin >> t >> n, t) {\n vector<int> field(max, 1);\n int x, y;\n rep(i, n) {\n cin >> x >> y;\n y = y * (m_t * 2 + m_t * 2 + 1 - abs(y)) / 2;\n field[zero + x + y] = 0;\n }\n int sx, sy;\n cin >> sx >> sy;\n x = sx;\n y = sy * (m_t * 2 + m_t * 2 + 1 - abs(sy)) / 2;\n field[zero + x + y] = 0;\n int ans = 0;\n deque<Node> rt;\n rt.push_back({sx, sy, 0});\n while (!rt.empty()) {\n ans++;\n Node node = rt.front();\n rt.pop_front();\n if (node.t < t) {\n rep(i, 6) {\n int nx = node.x + dx[i];\n int ny = (node.y + dy[i]) * (m_t * 2 + m_t * 2 + 1 - abs(node.y + dy[i])) / 2;\n if (field[zero + nx + ny]) {\n field[zero + nx + ny] = 0;\n rt.push_back({nx, node.y + dy[i], node.t + 1});\n }\n }\n }\n }\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3140, "score_of_the_acc": 0, "final_rank": 1 } ]
aoj_2246_cpp
Problem E: Alice and Bomb Alice and Bob were in love with each other, but they hate each other now. One day, Alice found a bag. It looks like a bag that Bob had used when they go on a date. Suddenly Alice heard tick-tack sound. Alice intuitively thought that Bob is to kill her with a bomb. Fortunately, the bomb has not exploded yet, and there may be a little time remained to hide behind the buildings. The appearance of the ACM city can be viewed as an infinite plane and each building forms a polygon. Alice is considered as a point and the bomb blast may reach Alice if the line segment which connects Alice and the bomb does not intersect an interior position of any building. Assume that the speed of bomb blast is infinite; when the bomb explodes, its blast wave will reach anywhere immediately, unless the bomb blast is interrupted by some buildings. The figure below shows the example of the bomb explosion. Left figure shows the bomb and the buildings(the polygons filled with black). Right figure shows the area(colored with gray) which is under the effect of blast wave after the bomb explosion. Figure 6: The example of the bomb explosion Note that even if the line segment connecting Alice and the bomb touches a border of a building, bomb blast still can blow off Alice. Since Alice wants to escape early, she wants to minimize the length she runs in order to make herself hidden by the building. Your task is to write a program which reads the positions of Alice, the bomb and the buildings and calculate the minimum distance required for Alice to run and hide behind the building. Input The input contains multiple test cases. Each test case has the following format: N bx by m 1 x 1,1 y 1,1 ... x 1, m 1 y 1, m 1 . . . m N x N ,1 y N ,1 ... x N , m N y N , m N The first line of each test case contains an integer N (1 ≤ N ≤ 100), which denotes the number of buildings. In the second line, there are two integers bx and by (-10000 ≤ bx , by ≤ 10000), which means the location of the bomb. Then, N lines follows, indicating the information of the buildings. The information of building is given as a polygon which consists of points. For each line, it has an integer m i (3 ≤ m i ≤ 100, ∑ N i =1 m i ≤ 500) meaning the number of points the polygon has, and then m i pairs of integers x i,j , y i,j follow providing the x and y coordinate of the point. You can assume that the polygon does not have self-intersections. any two polygons do not have a common point. the set of points is given in the counter-clockwise order. the initial positions of Alice and bomb will not by located inside the polygon. there are no bomb on the any extended lines of the given polygon’s segment. Alice is initially located at (0, 0). It is possible that Alice is located at the boundary of a polygon. N = 0 denotes the end of the input. You may not process this as a test case. Output For each test case, output one line which consists of the minimum distance required for Alice to run and hide behin ...(truncated)
[ { "submission_id": "aoj_2246_10829154", "code_snippet": "#include<bits/stdc++.h>\n//#define Alex std::ios::sync_with_stdio(false),std::cin.tie(0),std::cout.tie(0);\n//#define int long long\n//#define double long double\n//#define L(i,a,b) for(int i = a;i <= b;i++)\n//#define R(i,a,b) for(int i = a;i >= b;i--)\nusing namespace std;\n//const int mod = 1e9 + 7;\nconst int QAQ = 0;\n//const double pi = std::acos(-1.0);\n\n\ntypedef long long LL;\ntypedef double LD;\ntypedef pair <int, int> pii;\n#define cp const point &\nconst LD eps = 1e-7;\nconst LD large = 1e5;\nint sgn (LD x) { return x > eps ? 1 : (x < -eps ? -1 : 0); }\nLD sqr (LD x) { return x * x; }\n\nstruct point {\n LD x, y;\n point () {}\n point (LD xx, LD yy) { x = xx,y = yy; }\n point operator + (cp a) const { return {x + a.x, y + a.y}; }\n point operator - (cp a) const { return {x - a.x, y - a.y}; }\n point operator * (LD a) const { return {x * a, y * a}; }\n point operator / (LD a) const { return {x / a, y / a}; }\n bool operator != (cp a) const { return sgn(x - a.x) || sgn(y - a.y);}\n bool operator == (cp a) const { return !sgn(x - a.x) && !sgn(y - a.y); }\n bool operator < (cp a) const \n {\n int s = sgn(x - a.x);\n if (s) return s < 0;\n return sgn(y - a.y) <0;\n }\n void read () { cin >> x >> y; }\n};\n\nLD det (cp a, cp b) { return a.x * b.y - b.x * a.y; }\nLD dot (cp a, cp b) { return a.x * b.x + a.y * b.y; }\nLD dis (cp a, cp b) { return sqrt(sqr(a.x - b.x) + sqr(a.y - b.y)); }\n\n#define cl const line &\n\nstruct line{\n point s,t;\n line () {}\n line (cp ss, cp tt) { s = ss, t = tt; }\n};\n\n inline int sgn (cp a) {return sgn(a.y) >0 || (sgn(a.y) == 0 && sgn(a.x) > 0) ? 1 :- 1; }\n inline bool two_side(cp a,cp b,cl c) { return sgn (det(a - c.s, c.t - c.s)) * sgn (det (b - c.s, c.t - c.s)) < 0;}\n inline bool turn_left (cp a,cp b,cp c) { return sgn(det(b - a, c-a)) >= 0;}\n inline bool turn_left_strict (cp a, cp b, cp c) { return sgn(det(b - a, c-a)) > 0;}\n\n inline bool point_on_segment (cp a,cl b) { return sgn(det(a - b.s,b.t - b.s)) == 0 && sgn(dot(b.s - a, b.t - a)) <= 0;}\n\n inline bool inter_judge(cl a,cl b) \n\t{\n if (point_on_segment(b.s,a) || point_on_segment(b.t,a)) return true;\n if (point_on_segment(a.s,b) || point_on_segment(a.t,b)) return true;\n return two_side(a.s, a.t , b) && two_side(b.s, b.t, a);\n }\n\n inline bool inter_judge_strict(cl a, cl b) { return two_side(a.s, a.t, b) && two_side(b.s, b.t, a);}\n \n inline point proj_to_line(cp a, cl b) \n\t{\n point st = b.t - b.s;\n return b.s + st * (dot(a - b.s, st) / dot(st, st));\n }\n\n inline point line_inter (cl a, cl b) \n\t{\n LD s1 = det(a.t - a.s, b.s - a.s);\n LD s2 = det(a.t - a.s, b.t - a.s);\n return (b.s * s2 - b.t * s1) / (s2 - s1);\n }\n\n inline bool point_in_polygon (cp p, const vector <point> & po) \n\t{\n int n = (int) po.size (); \n\t\tint cnt = 0;\n for (int i = 0; i < n; ++i) \n\t\t{\n point a = po[i], b= po[(i + 1) % n];\n if (point_on_segment(p, {a, b})) return true;\n int x= sgn (det(p - a,b - a));\n int y = sgn (a.y - p.y);\n int z = sgn (b.y - p.y);\n if (x > 0 && y <= 0 && z > 0) ++cnt;\n if (x < 0 && z <= 0 && y > 0) --cnt; \n\t\t}\n return cnt != 0; \n\t}\n\ninline bool in_polygon(const vector <point> &p, cp u, cp v) \n{\n for(int i = 0;i < (int)p.size();i++) \n\t{\n int j = (i + 1) % (int) p.size(),k = (i + 2) % (int) p.size();\n cp ii = p[i],jj = p[j],kk = p[k];\n if(inter_judge_strict({u, v}, {ii, jj})) return false;\n if (point_on_segment (jj,{u, v})) \n\t {\n bool good = true, left = turn_left(ii,jj,kk);\n for (auto x : {u, v}) \n\t\t {\n if (left) \n\t\t {\n good &= turn_left(ii, jj, x) && turn_left(jj, kk, x);\n }else \n\t\t\t {\n good &= (!(turn_left_strict(jj, x, kk) && turn_left_strict(jj, ii, x)));\n }\n }\n if (!good) return 0;\n }\n }\n return 1;\n}\n\n\n\nconst point Z(0, 0);\n\nint n;\nvector < vector <point> > p, q;\nvector < vector <int> > r;\nmap <point, int> id;\nvector <vector < pair <int, LD> > > E;\nint cnt;\n\nvoid init() \n{\n p.clear(); \n\tq.clear(); \n\tr.clear();\n id.clear();\n E.clear(); \n\tE.push_back({});\n cnt = 0;\n}\n\nvoid get_id(point x) \n{\n if(id.count(x))\n {\n\t}\n else \n\t{\n id[x] = ++cnt;\n E.push_back({});\n }\n}\n\nbool check_good(cp u, cp v) \n{\n for (int i = 0; i < n; i++) \n\t{\n if (!in_polygon(q[i], u, v)) \n\t\t{\n return false;\n }\n }\n return true;\n}\n\nvoid add (int u, int v, LD w) \n{\n E[u].push_back ({v,w});\n if (v != 0) E[v].push_back ({u,w});\n}\n\n\ninline LD dij(int s,int t) \n{\n vector<LD> Dis (cnt + 1,1e18);\n Dis[s] = 0;\n priority_queue<pair<LD,int>, vector<pair<LD, int> >, greater<pair<LD,int> > > pq;\n pq.push({LD(0),s});\n while(!pq.empty()) \n\t{\n LD d;\n\t\tint x;\n std::tie(d,x) = pq.top();\n\t\tpq.pop();\n if (d != Dis[x]) continue;\n if (x == t) return d;\n int v;\n\t\tLD w;\n for(auto pp : E[x]) \n\t\t{\n std :: tie(v, w) = pp;\n if(Dis[v] > Dis[x] + w) \n\t\t\t{\n Dis[v] = Dis[x] + w;\n pq.push({Dis[v],v});\n }\n }\n }\n return 1e18l;\n}\n\ninline void work() \n{\n init();\n p.resize(n);\n q.resize(n);\n r.resize(n);\n point A = Z;\n point B;\n B.read();\n for(int i = 0; i < n; i++) \n\t{\n int m; \n\t\tcin >> m;\n while(m -- ) \n\t\t{\n point a;\n a.read();\n p[i].push_back(a);\n }\n q[i] = p[i];\n reverse(q[i].begin(), q[i].end());\n }\n if(!check_good(A, B)) \n\t{ \n\t puts (\"0.000000000\");\n\t\treturn;\n\t}\n for (int i = 0; i < n; i++) \n\t{\n auto &po = p[i];\n r[i].resize(po.size());\n for (int j = 0; j < (int) po.size(); j++) \n\t\t{\n auto &u = po[j],&v=po[(j + 1) % po.size()];\n auto w = (u + v) / 2;\n if(! check_good(B, w)) \n\t\t{\n r[i][j] = 1;\n r[i][(j+1) % po.size()] = 1;\n }\n }\n }\n get_id(A);\n for(int i = 0; i < n; i++) \n\t{\n auto &po = p[i];\n for(auto &u : po) \n\t\t{\n get_id(u);\n if(check_good(A, u)) \n\t\t{\n add(id[A], id[u], dis(A,u));\n }\n }\n for(int j = 0; j < (int) po.size(); j++) \n\t\tif(r[i][j]) \n\t\t{\n\t\t add(id[po[j]],0,0);\n\t\t}\n }\n \n for (int i = 0; i < n; i++) \n\t{\n auto &po = p[i];\n for (int j = 0; j < (int) po.size(); j++) {\n auto &u = po[j];\n for (int k = i; k < n; k++) \n\t\t{\n auto &op = p[k];\n for (int l = (i == k ? j + 1: 0); l < (int) op.size(); l++) \n\t\t{\n auto &v = op[l];\n if (check_good(u, v)) add(id[u], id[v], dis(u,v));\n }\n }\n }\n }\n \n for (int i = 0; i < n; i++) \n\t{\n auto &po = p[i];\n for (int j = 0; j < (int) po.size(); j++) {\n auto &u = po[j];\n for (int k = 0; k < n; k++) {\n auto &op = p[k];\n for (int l = 0; l < (int) op.size(); l++) \n\t\tif(r[k][l]) {\n auto &v = op[l];\n auto proj = proj_to_line(u, {B, v});\n if (!point_on_segment (proj, {B, v}) && sgn(dot(u - v, v - B)) > 0 && check_good(u, proj)) {add(id[u], 0, dis(u, proj));}\n }\n }}\n }\n for (int k = 0; k < n; k++) \n\t{\n auto &op = p[k];\n for (int l = 0; l < (int) op.size(); l++) \n\t\tif (r[k][l]) \n\t\t{\n auto &v = op[l];\n auto proj = proj_to_line(A, {B, v});\n if (! point_on_segment (proj, {B, v}) && sgn(dot(A - v, v - B)) >0 && check_good(A, proj)) \n\t\t\t{ add(id[A],0, dis(A, proj));}\n }\n }\n printf(\"%.9lf\\n\", dij(id[A],0));\n}\n\ninline void Fre() {\tint liujiahui = 142857; }\n\nsigned main() \n{\n//\tAlex;\n\tFre();\n\tint _;\n\t_ = 1;\n//\tstd::cin>>_;\n\twhile(_--)\n\t{\n\t}\n\twhile(\"gh\" == \"gh\")\n\t{\n\t\tstd::cin>>n;\n\t\tif(n == 0) break;\n\t\twork();\n\t}\n return QAQ;\n}", "accuracy": 1, "time_ms": 1320, "memory_kb": 7424, "score_of_the_acc": -1.0699, "final_rank": 12 }, { "submission_id": "aoj_2246_10725985", "code_snippet": "#include \"bits/stdc++.h\"\n\n#define all(x) x.begin(), x.end()\n#define NL std::cout << '\\n'\n\nusing lnt = long long;\n\nconst int inf = 0x3f3f3f3f;\nconst lnt linf = 1ll << 61;\n\nusing db = long double; const db eps = 1e-7;\nint sgn(db x) { return (x < -eps ? -1 : x > eps); }\n\nstruct Pt { db x, y; };\nstd::istream& operator>>(std::istream& is, Pt &p) {\n int x, y; std::cin >> x >> y;\n p.x = x; p.y = y; return is;\n}\nstd::ostream& operator<<(std::ostream& os, Pt p) {\n return os << \"(\" << p.x << \",\" << p.y << \")\";\n}\n\nPt operator-(Pt a, Pt b) { return { a.x - b.x, a.y - b.y }; }\nPt operator+(Pt a, Pt b) { return { a.x + b.x, a.y + b.y }; }\nPt operator*(Pt a, db x) { return { a.x * x, a.y * x }; }\nPt operator/(Pt a, db x) { return { a.x / x, a.y / x }; }\ndb dot(Pt a, Pt b) { return a.x * b.x + a.y * b.y; }\ndb det(Pt a, Pt b) { return a.x * b.y - a.y * b.x; }\nbool operator==(Pt a, Pt b) { return !sgn(dot(a-b, a-b)); }\nbool operator!=(Pt a, Pt b) { return sgn(dot(a-b, a-b)); }\nbool operator<(Pt a, Pt b) { return a.x == b.x ? a.y < b.y : a.x < b.x; }\ndb dis2(Pt a, Pt b) { return dot(a-b, a-b); }\ndb dis(Pt a, Pt b) { return std::sqrt(dis2(a, b)); }\nint side(Pt a, Pt b, Pt x) { return sgn(det(b-a, x-a)); }\nPt rot90(Pt a) { return {-a.y, a.x}; }\nPt rot(Pt a, db t) {\n return { a.x * std::cos(t) - a.y * std::sin(t)\n , a.x * std::sin(t) + a.y * std::cos(t) };\n}\nPt unit(Pt a) {\n db d = dis(a, {0, 0});\n return sgn(d) ? a/d : Pt{0, 0};\n}\n\n\nstruct Line { Pt s, t; };\n\nPt intersect(Line a, Line b) {\n db v = det(a.t - a.s, b.s - a.s);\n db u = det(a.t - a.s, b.t - a.s);\n return (b.s * u - b.t * v) / (u - v);\n}\nbool para(Line a, Line b) {\n return !sgn(det(a.t - a.s, b.t - b.s));\n}\n\nbool ptOnSeg(Pt x, Line v) {\n if (v.s == v.t) { return sgn(dis2(v.s, x)) == 0; }\n return sgn(det(v.s - x, v.t - x)) == 0\n && sgn(dot(v.s - x, v.t - x)) <= 0;\n}\n\nbool ptOnSeg_strict(Pt x, Line v) {\n if (v.s == v.t) { return sgn(dis2(v.s, x)) == 0; }\n return sgn(det(v.s - x, v.t - x)) == 0\n && sgn(dot(v.s - x, v.t - x)) < 0;\n}\n\nbool segInter(Line a, Line b) {\n if (sgn(det(b.s-a.s, a.t-a.s)) * sgn(det(b.t-a.s, a.t-a.s)) == -1\n && sgn(det(a.s-b.s, b.t-b.s)) * sgn(det(a.t-b.s, b.t-b.s)) == -1) {\n return true;\n }\n return false;\n}\n\nbool ptInPoly(Pt p, std::vector<Pt> &v) {\n int cnt = 0;\n for (int i = v.size() - 1, j = 0; j < v.size() - 2; i = j++) {\n if (ptOnSeg(p, {v[i], v[j]})) { return true; }\n int x = sgn(det(p - v[i], v[j] - v[i]));\n int y = sgn(v[i].y - p.y);\n int z = sgn(v[j].y - p.y);\n if (x > 0 && y <= 0 && z > 0) { ++cnt; }\n if (x < 0 && z <= 0 && y > 0) { --cnt; }\n }\n return cnt != 0;\n}\n\n\nPt projection(Pt p, Line l) {\n Pt v = l.t - l.s;\n return l.s + v * (dot(p - l.s, v) / dot(v, v));\n}\n\nbool segInPoly(Line s, std::vector<Pt> &v) {\n const int n = v.size();\n for (int i = 0; i < n - 2; ++i) {\n const Pt a = v[i], b = v[(i+1)], c = v[(i+2)];\n if (segInter(s, {a, b})) { return false; }\n if (ptOnSeg_strict(s.s, {a, b}) && side(a, b, s.t) < 0) { return false; }\n if (ptOnSeg_strict(s.t, {a, b}) && side(a, b, s.s) < 0) { return false; }\n if (ptOnSeg(b, s)) {\n bool ok = true, acute = side(a, b, c) > 0;\n for (auto x : {s.s, s.t}) {\n if (acute) {\n ok &= side(a, b, x) >= 0 && side(b, c, x) >= 0;\n } else {\n ok &= side(a, b, x) >= 0 || side(b, c, x) >= 0;\n }\n }\n if (!ok) { return false; }\n }\n }\n return true;\n}\n\nint main() {\n std::ios::sync_with_stdio(0), std::cin.tie(0);\n int n;\n const Pt alice = {0, 0};\n while (std::cin >> n && n) {\n Pt bomb; std::cin >> bomb;\n std::vector<std::vector<Pt>> poly(n);\n std::vector<std::vector<Pt>> rpoly(n);\n for (int i = 0; i < n; ++i) {\n int k; std::cin >> k;\n poly[i].resize(k);\n for (int j = 0; j < k; ++j) {\n std::cin >> poly[i][j];\n }\n rpoly[i] = poly[i];\n std::reverse(all(rpoly[i]));\n poly[i].emplace_back(poly[i][0]); poly[i].emplace_back(poly[i][1]);\n rpoly[i].emplace_back(rpoly[i][0]); rpoly[i].emplace_back(rpoly[i][1]);\n }\n\n\n auto visible = [&](Pt u, Pt v) {\n for (int i = 0; i < n; ++i) {\n if (!segInPoly({u, v}, rpoly[i])) { return false; }\n }\n return true;\n };\n\n auto alive = [&](Pt u) {\n for (int j = 0; j < n; ++j) {\n for (int k = 0; k < poly[j].size() - 2; ++k) {\n Pt a = poly[j][k], b = poly[j][k+1], c = poly[j][k+2];\n if (segInter({a, b}, {bomb, u})) { return true; }\n if (!sgn(det(u - bomb, a - bomb))\n && sgn(dot(bomb - u, a - u)) > 0\n && sgn(dot(u - bomb, a - bomb)) >= 0) { return true; }\n if ((b == u) && (side(bomb, u, a) == side(bomb, u, c))) { return true; }\n }\n }\n return false;\n };\n\n if (!visible(bomb, alice)) {\n std::cout << \"0\"; NL;\n continue;\n }\n\n std::vector<Pt> vt({alice});\n for (int i = 0; i < n; ++i) {\n for (auto x : poly[i]) {\n vt.emplace_back(x);\n }\n }\n\n std::vector<Line> ls;\n\n std::sort(vt.begin() + 1, vt.end());\n vt.erase(std::unique(all(vt)), vt.end());\n\n for (auto x : vt) {\n if (alive(x)) {\n ls.push_back({bomb, x});\n }\n }\n\n\n std::vector<Pt> v3;\n for (const auto &l : ls) {\n for (int i = 0; i < n; ++i) {\n for (int j = 0; j < poly[i].size() - 2; ++j) {\n if (side(l.s, l.t, poly[i][j]) * side(l.s, l.t, poly[i][j+1]) != -1) { continue; }\n Pt inter = intersect(l, {poly[i][j], poly[i][j+1]});\n if (alive(inter)) { v3.emplace_back(inter); }\n }\n }\n }\n\n std::sort(all(v3));\n v3.erase(std::unique(all(v3)), v3.end());\n\n db ret = inf;\n std::vector<db> d(vt.size(), inf);\n std::priority_queue<std::pair<db, int>, std::vector<std::pair<db, int>>, std::greater<>> q;\n d[0] = 0;\n q.emplace(0, 0);\n while (!q.empty()) {\n auto _ = q.top(); q.pop();\n auto ddis = _.first; auto u = _.second;\n if (ret < ddis) { break; }\n if (d[u] < ddis) { continue; }\n if (alive(vt[u])) { ret = ddis; break; }\n for (auto l : ls) {\n Pt proj = projection(vt[u], l);\n if (visible(proj, vt[u]) && alive(proj)) { ret = std::min(ret, ddis + dis(proj, vt[u])); }\n }\n for (auto x : v3) {\n if (visible(x, vt[u])) { ret = std::min(ret, ddis + dis(x, vt[u])); }\n }\n for (int v = 0; v < vt.size(); ++v) {\n db w = dis(vt[v], vt[u]);\n if (w + ddis >= d[v] || !visible(vt[v], vt[u])) { continue; }\n d[v] = w + ddis;\n q.emplace(d[v], v);\n }\n }\n std::cout << std::fixed << std::setprecision(8);\n std::cout << ret; NL;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 4110, "memory_kb": 3736, "score_of_the_acc": -0.9102, "final_rank": 9 }, { "submission_id": "aoj_2246_10725984", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long LL;\ntypedef double LD;\ntypedef pair <int, int> pii;\n#define cp const point &\nconst LD eps = 1e-7;\nconst LD large = 1e5;\nint sgn (LD x) { return x > eps ? 1 : (x < -eps ? -1 : 0); }\nLD sqr (LD x) { return x * x; }\n \nstruct point {\n\tLD x, y;\n\tpoint () {}\n\tpoint (LD xx, LD yy) { x = xx, y = yy; }\n\tpoint operator + (cp a) const { return {x + a.x, y + a.y}; }\n\tpoint operator - (cp a) const { return {x - a.x, y - a.y}; }\n\tpoint operator * (LD a) const { return {x * a, y * a}; }\n\tpoint operator / (LD a) const { return {x / a, y / a}; }\n\tbool operator != (cp a) const { return sgn(x - a.x) || sgn(y - a.y); }\n\tbool operator == (cp a) const { return !sgn(x - a.x) && !sgn(y - a.y); }\n\tbool operator < (cp a) const {\n\t\tint s = sgn(x - a.x);\n\t\tif (s) return s < 0;\n\t\treturn sgn(y - a.y) < 0;\n\t}\n\tvoid read () { cin >> x >> y; }\n};\n \nLD det (cp a, cp b) { return a.x * b.y - b.x * a.y; }\nLD dot (cp a, cp b) { return a.x * b.x + a.y * b.y; }\nLD dis (cp a, cp b) { return sqrt (sqr(a.x - b.x) + sqr(a.y - b.y)); }\n \n \n#define cl const line &\nstruct line {\n\tpoint s, t;\n\tline () {}\n\tline (cp ss, cp tt) { s = ss, t = tt; }\n};\n \nint sgn (cp a) {return sgn(a.y) > 0 || (sgn(a.y) == 0 && sgn(a.x) > 0) ? 1 : -1;}\nbool two_side(cp a, cp b, cl c) {\n\treturn sgn (det(a - c.s, c.t - c.s)) * sgn (det (b - c.s, c.t - c.s)) < 0;\n}\nbool turn_left (cp a, cp b, cp c) { return sgn(det(b - a, c - a)) >= 0; }\nbool turn_left_strict (cp a, cp b, cp c) { return sgn(det(b - a, c - a)) > 0; }\n \nbool point_on_segment(cp a, cl b) {\n\treturn sgn (det( a - b.s, b.t - b.s)) == 0 \n\t&& sgn (dot (b.s - a, b.t - a)) <= 0;\n}\n \nbool inter_judge(cl a, cl b) {\n\tif (point_on_segment(b.s, a) || point_on_segment(b.t, a)) return true;\n\tif (point_on_segment(a.s, b) || point_on_segment(a.t, b)) return true;\n\treturn two_side(a.s, a.t, b) && two_side(b.s, b.t, a);\n}\n \nbool inter_judge_strict(cl a, cl b) {\n\treturn two_side(a.s, a.t, b) && two_side(b.s, b.t, a);\n}\n\npoint proj_to_line(cp a, cl b) {\n\tpoint st = b.t - b.s;\n\treturn b.s + st * (dot(a - b.s, st) / dot(st, st));\n}\n\npoint line_inter(cl a, cl b) {\n\tLD s1 = det(a.t - a.s, b.s - a.s);\n\tLD s2 = det(a.t - a.s, b.t - a.s);\n\treturn (b.s * s2 - b.t * s1) / (s2 - s1);\n}\n\nbool point_in_polygon (cp p, const vector <point> & po) {\n\tint n = (int) po.size (); int cnt = 0;\n\tfor (int i = 0; i < n; ++i) {\n\t\tpoint a = po[i], b = po[(i + 1) % n];\n\t\tif (point_on_segment (p, {a, b})) return true;\n\t\tint x = sgn (det(p - a, b - a));\n\t\tint y = sgn (a.y - p.y);\n\t\tint z = sgn (b.y - p.y);\n\t\tif (x > 0 && y <= 0 && z > 0) ++cnt;\n\t\tif (x < 0 && z <= 0 && y > 0) --cnt; }\n\treturn cnt != 0; }\n\nbool in_polygon(const vector <point> &p, cp u, cp v) {\n\tfor (int i = 0; i < (int) p.size(); i++) {\n\t\tint j = (i + 1) % (int) p.size(), k = (i + 2) % (int) p.size();\n\t\tcp ii = p[i], jj = p[j], kk = p[k];\n\t\tif (inter_judge_strict({u, v}, {ii, jj})) return false;\n\t\tif (point_on_segment(jj, {u, v})) {\n\t\t\tbool good = true, left = turn_left(ii, jj, kk);\n\t\t\tfor (auto x : {u, v}) {\n\t\t\t\tif (left) {\n\t\t\t\t\tgood &= turn_left(ii, jj, x) && turn_left(jj, kk, x);\n\t\t\t\t} else {\n\t\t\t\t\tgood &= !(turn_left_strict(jj, x, kk) && turn_left_strict(jj, ii, x));\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (!good) return 0;\n\t\t}\n\t}\n\treturn 1;\n}\n\nconst point Z(0, 0);\n\nint n;\nvector < vector <point> > p, q;\nvector < vector <int> > r;\nmap <point, int> id;\nvector <vector < pair <int, LD> > > E;\nint cnt;\n\nvoid init() {\n\tp.clear(); q.clear(); r.clear();\n\tid.clear();\n\tE.clear(); E.push_back({});\n\tcnt = 0;\n}\n\nvoid get_id (point x) {\n\tif (id.count(x));\n\telse {\n\t\tid[x] = ++cnt;\n\t\tE.push_back({});\n\t}\n}\n\nbool check_good(cp u, cp v) {\n\tfor (int i = 0; i < n; i++) {\n\t\tif (!in_polygon(q[i], u, v)) {\n\t\t\treturn false;\n\t\t}\n\t}\n\treturn true;\n}\n\nvoid add (int u, int v, LD w) {\n\tE[u].push_back({v, w});\n\tif (v != 0) E[v].push_back({u, w});\n}\n\n\nLD dij (int s, int t) {\n\tvector <LD> Dis (cnt + 1, 1e18);\n\tDis[s] = 0;\n\tpriority_queue < pair <LD, int>, vector < pair <LD, int>>, greater < pair <LD, int>>> pq;\n\tpq.push({LD(0), s});\n\twhile (!pq.empty()) {\n\t\tauto [d, x] = pq.top(); pq.pop();\n\t\tif (d != Dis[x]) continue; \n\t\tif (x == t) return d;\n\t\tfor (auto [v, w] : E[x]) {\n\t\t\tif (Dis[v] > Dis[x] + w) {\n\t\t\t\tDis[v] = Dis[x] + w;\n\t\t\t\tpq.push({Dis[v], v});\n\t\t\t}\n\t\t}\n\t}\n\treturn 1e18; // ??\n}\n\nvoid work() {\n\tinit();\n\tp.resize(n);\n\tq.resize(n);\n\tr.resize(n);\n\tpoint A = Z;\n\tpoint B;\n\tB.read();\n\tfor (int i = 0; i < n; i++) {\n\t\tint m; cin >> m;\n\t\twhile (m--) {\n\t\t\tpoint a;\n\t\t\ta.read();\n\t\t\tp[i].push_back(a);\n\t\t}\n\t\tq[i] = p[i];\n\t\treverse(q[i].begin(), q[i].end());\n\t}\n\tif (!check_good(A, B)) {\n\t\tputs (\"0.0\");\n\t\treturn;\n\t}\n\tfor (int i = 0; i < n; i++) {\n\t\tauto &po = p[i];\n\t\tr[i].resize(po.size());\n\t\tfor (int j = 0; j < (int) po.size(); j++) {\n\t\t\tauto &u = po[j], &v = po[(j + 1) % po.size()];\n\t\t\tauto w = (u + v) / 2;\n\t\t\tif (!check_good(B, w)) {\n\t\t\t\tr[i][j] = 1;\n\t\t\t\tr[i][(j + 1) % po.size()] = 1;\n\t\t\t}\n\t\t}\n\t}\n\tget_id(A);\n\tfor (int i = 0; i < n; i++) {\n\t\tauto &po = p[i];\n\t\tfor (auto &u : po) {\n\t\t\tget_id(u);\n\t\t\tif (check_good(A, u)) {\n\t\t\t\tadd(id[A], id[u], dis(A, u));\n\t\t\t}\n\t\t}\n\t\tfor (int j = 0; j < (int) po.size(); j++) if (r[i][j]) {\n\t\t\tadd(id[po[j]], 0, 0);\n\t\t}\n\t}\n\n\tfor (int i = 0; i < n; i++) {\n\t\tauto &po = p[i];\n\t\tfor (int j = 0; j < (int) po.size(); j++) {\n\t\t\tauto &u = po[j];\n\t\t\tfor (int k = i; k < n; k++) {\n\t\t\t\tauto &op = p[k];\n\t\t\t\tfor (int l = (i == k ? j + 1 : 0); l < (int) op.size(); l++) {\n\t\t\t\t\tauto &v = op[l];\n\t\t\t\t\tif (check_good(u, v)) add(id[u], id[v], dis(u, v));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tfor (int i = 0; i < n; i++) {\n\t\tauto &po = p[i];\n\t\tfor (int j = 0; j < (int) po.size(); j++) {\n\t\t\tauto &u = po[j];\n\t\t\tfor (int k = 0; k < n; k++) {\n\t\t\t\tauto &op = p[k];\n\t\t\t\tfor (int l = 0; l < (int) op.size(); l++) if (r[k][l]) {\n\t\t\t\t\tauto &v = op[l];\n\t\t\t\t\tauto proj = proj_to_line(u, {B, v});\n\t\t\t\t\tif (!point_on_segment(proj, {B, v})\n\t\t\t\t\t\t&& sgn(dot(u - v, v - B)) > 0\n\t\t\t\t\t\t&& check_good(u, proj)) {\n\t\t\t\t\t\tadd(id[u], 0, dis(u, proj));\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tfor (int k = 0; k < n; k++) {\n\t\tauto &op = p[k];\n\t\tfor (int l = 0; l < (int) op.size(); l++) if (r[k][l]) {\n\t\t\tauto &v = op[l];\n\t\t\tauto proj = proj_to_line(A, {B, v});\n\t\t\tif (!point_on_segment(proj, {B, v})\n\t\t\t\t\t&& sgn(dot(A - v, v - B)) > 0\n\t\t\t\t\t&& check_good(A, proj)) {\n\t\t\t\tadd(id[A], 0, dis(A, proj));\n\t\t\t}\n\t\t}\n\t}\n\tprintf(\"%.9lf\\n\", dij(id[A], 0));\n}\n\nint main() {\n\tios::sync_with_stdio(false); cin.tie(0);\n\twhile (cin >> n && n) work();\n}", "accuracy": 1, "time_ms": 1830, "memory_kb": 7400, "score_of_the_acc": -1.1371, "final_rank": 14 }, { "submission_id": "aoj_2246_10466883", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#define MAX 1000005\n#define INF 1e8\n#define none -1145141919810\n#define ls p<<1\n#define rs p<<1|1\ntypedef pair<int,int>Pl;\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef pair<ll,ll>Pll;\nconst ll mod=1e9+7;\ntypedef long double db;\nconst db pi=acosl(-1.0);\nconst db eps=1e-6;\nconst db g=9.8;\nint sign(db a){return a<-eps?-1:a>eps;}\nint cmp(db a,db b){return sign(a-b);}\nstruct P{\n db x,y;\n P(db x=0,db y=0):x(x),y(y){}\n P operator+(P p){return {x+p.x,y+p.y};}\n P operator-(P p){return {x-p.x,y-p.y};}\n P operator*(db d){return {x*d,y*d};}\n P operator/(db d){return{x/d,y/d};}\n bool operator<(P p){\n int c=cmp(x,p.x);\n if(c) return c==-1;\n return cmp(y,p.y)==-1;\n }\n bool operator==(P o){\n return cmp(x,o.x)==0&&cmp(y,o.y)==0;\n }\n bool operator!=(P o){\n return cmp(x,o.x)!=0||cmp(y,o.y)!=0;\n }\n int quad()const{return sign(y)==1||(sign(y)==0&&sign(x)>=0);}\n db dot(P p){return x*p.x+y*p.y;}\n db det(P p){return x*p.y-y*p.x;}\n db abs1(){return sqrt(abs2());}\n db abs2(){return x*x+y*y;}\n db disTo(P p){return (*this-p).abs1();}\n db disTo2(P p){return (*this-p).abs2();}\n P rot90(){return P(-y,x);}\n P unit(){return *this/abs1();}\n P rot(db an){return {x*cos(an)-y*sin(an),x*sin(an)+y*cos(an)};}\n db alpha(){return atan2l(y,x);}\n};\ndb cross(P p1,P p2,P p3){return (p2.x-p1.x)*(p3.y-p1.y)-(p3.x-p1.x)*(p2.y-p1.y);}\ndb crossop(P p1,P p2,P p3){return sign(cross(p1,p2,p3));}\ndb rad(P p1,P p2){return atan2l(p1.det(p2),p1.dot(p2));}\nbool cheLL(P p1,P p2,P q1,P q2){\n db a1=cross(q1,q2,p1),a2=-cross(q1,q2,p2);\n return sign(a1+a2)!=0;\n}\nP isLL(P p1,P p2,P q1, P q2){\n db a1=cross(q1,q2,p1),a2=-cross(q1,q2,p2);\n return (p1*a2+p2*a1)/(a1+a2);\n}\nbool intersect(db l1,db r1,db l2,db r2){\n if(l1>r1) swap(l1,r1);\n if(l2>r2) swap(l2,r2);\n return !(cmp(r1,l2)==-1||cmp(r2,l1)==-1);\n}\nbool isSS(P p1,P p2,P q1,P q2){\n return intersect(p1.x,p2.x,q1.x,q2.x)&&intersect(p1.y,p2.y,q1.y,q2.y)&&crossop(p1,p2,q1)*crossop(p1,p2,q2)<=0&&crossop(q1,q2,p1)*crossop(q1,q2,p2)<=0;\n}\nbool isSS_strict(P p1,P p2,P q1,P q2){\n return crossop(p1,p2,q1)*crossop(p1,p2,q2)<0&&crossop(q1,q2,p1)*crossop(q1,q2,p2)<0;\n}\nbool ismiddle(db a,db b, db m){\n return sign(a-m)==0||sign(b-m)==0||(a<m)!=(b<m);\n}\nbool ismiddle(P a,P b,P m){\n return ismiddle(a.x,b.x,m.x)&&ismiddle(a.y,b.y,m.y);\n}\nbool onseg(P p1,P p2, P q){\n return crossop(p1,p2,q)==0&&ismiddle(p1,p2,q);\n}\nbool onseg_strict(P p1,P p2,P q){\n return crossop(p1,p2,q)==0&&sign((q-p1).dot(p1-p2))*sign((q-p2).dot(p1-p2))<0;\n}\nP proj(P p1,P p2,P q){\n P dir=p2-p1;\n return p1+dir*(dir.dot(q-p1)/dir.abs2());\n}\nP refle(P p1,P p2,P q){\n return proj(p1,p2,q)*2-q;\n}\ndb nearest(P p1,P p2,P q){\n if(p1==p2) return p1.disTo(q);\n P h=proj(p1,p2,q);\n if(ismiddle(p1,p2,h)){\n return q.disTo(h);\n }\n return min(p1.disTo(q),p2.disTo(q));\n}\ndb disSS(P p1,P p2,P q1,P q2){\n if(isSS(p1,p2,q1,q2)) return 0;\n return min(min(nearest(p1,p2,q1),nearest(p1,p2,q2)),min(nearest(q1,q2,p1),nearest(q1,q2,p2)));\n}\nbool cmp(P a,P b){\n int qa=a.quad(),qb=b.quad();\n if(qa!=qb) return qa<qb;\n else return sign(a.det(b))>0; \n}\ndb area(vector<P>&ps){\n db ans=0;\n int n=ps.size();\n for(int i=0;i<n;i++){\n ans+=ps[i].det(ps[(i+1)%n]);\n }\n return ans/2.0; \n}\nint contain(vector<P>&ps,P p){\n int n=ps.size();\n int ret=0;\n for(int i=0;i<n;i++){\n P u=ps[i],v=ps[(i+1)%n];\n if(onseg(u,v,p)) return 1;\n if(cmp(u.y,v.y)<=0) swap(u,v);\n if(cmp(p.y,u.y)>0||cmp(p.y,v.y)<=0) continue;\n ret^=crossop(p,u,v)>0;\n }\n return ret*2;\n}\nvector<P>convexHull(vector<P>&ps){\n int n=ps.size();if(n<=1) return ps;\n sort(ps.begin(),ps.end());\n vector<P>qs(n*2);\n int k=0;\n for(int i=0;i<n;qs[k++]=ps[i++]){\n while(k>1&&crossop(qs[k-2],qs[k-1],ps[i])<=0) k--;\n }\n for(int i=(n-2),t=k;i>=0;qs[k++]=ps[i--]){\n while(k>t&&crossop(qs[k-2],qs[k-1],ps[i])<=0) k--;\n }\n qs.resize(k-1);\n return qs;\n}\ndb convexDiameter(vector<P>&ps){\n int n=ps.size(); if(n<=1) return 0;\n int is=0,js=0;\n for(int i=1;i<n;i++){\n is=ps[i]<ps[is]?i:is;\n js=ps[js]<ps[i]?i:js;\n }\n db ret=ps[is].disTo(ps[js]);\n int i=is,j=js;\n do{\n if((ps[(i+1)%n]-ps[i]).det((ps[(j+1)%n]-ps[j]))>=0) (++j)%=n;\n else (++i)%=n;\n ret=max(ret,ps[i].disTo(ps[j]));\n }while(i!=is||j!=js);\n return ret;\n}\nvector<P> convexCut(const vector<P>&ps,P q1,P q2){\n vector<P>qs;\n int n=ps.size();\n for(int i=0;i<n;i++){\n P p1=ps[i],p2=ps[(i+1)%n];\n int d1=crossop(q1,q2,p1),d2=crossop(q1,q2,p2);\n if(d1>=0) qs.push_back(p1);\n if(d1*d2<0) qs.push_back(isLL(p1,p2,q1,q2));\n }\n return qs;\n}\nvector<P>convexHullStrict(vector<P>&ps){\n int n=ps.size();if(n<=1) return ps;\n sort(ps.begin(),ps.end());\n n=unique(ps.begin(),ps.end())-ps.begin();\n vector<P>qs(n*2);\n int k=0;\n for(int i=0;i<n;qs[k++]=ps[i++]){\n while(k>1&&crossop(qs[k-2],qs[k-1],ps[i])<0) k--;\n }\n for(int i=(n-2),t=k;i>=0;qs[k++]=ps[i--]){\n while(k>t&&crossop(qs[k-2],qs[k-1],ps[i])<0) k--;\n }\n qs.resize(k-1);\n return qs;\n}\nint type(P o1,db r1,P o2,db r2){\n db d=o1.disTo(o2);\n if(cmp(d,r1+r2)==1) return 4;\n if(cmp(d,r1+r2)==0) return 3;\n if(cmp(d,abs(r1-r2))==1) return 2;\n if(cmp(d,abs(r1-r2))==0) return 1;\n return 0;\n}\nvector<P> isCL(P o,db r,P p1,P p2){ //圆和线段\n if(cmp(abs((o-p1).det(p2-p1))/p1.disTo(p2),r)>0) return {};\n db x=(p1-o).dot(p2-p1),y=(p2-p1).abs2(),d=x*x-y*((p1-o).abs2()-r*r);\n d=max(d,(db)0.0);\n P m=p1-(p2-p1)*(x/y),dr=(p2-p1)*(sqrt(d)/y);\n return {m-dr,m+dr};\n}\nvector<P> isCC(P o1,db r1,P o2,db r2){ //圆和圆\n db d=o1.disTo(o2);\n if(cmp(d,r1+r2)==1) return {};\n if(cmp(d,abs(r1-r2))==-1) return {};\n d=min(d,r1+r2);\n db y=(r1*r1+d*d-r2*r2)/(2*d),x=sqrt(r1*r1-y*y);\n P dr=(o2-o1).unit();\n P q1=o1+dr*y,q2=dr.rot90()*x;\n return {q1-q2,q1+q2};\n}\n//intanCC:-r2,tanCP r2=0\nvector<pair<P,P>> tanCC(P o1,db r1,P o2,db r2){\n P d=o2-o1;\n db dr=r1-r2,d2=d.abs2(),h2=d2-dr*dr;\n if(sign(d2)==0||sign(h2)<0) return {};\n h2=max((db)0.0,h2);\n vector<pair<P,P>>ret;\n for(db sign:{-1,1}){\n P v=(d*dr+d.rot90()*sqrt(h2)*sign)/d2;\n ret.push_back({o1+v*r1,o2+v*r2});\n }\n if(sign(h2)==0) ret.pop_back();\n return ret;\n}\nP inCenter(P A,P B,P C){\n db a=(B-C).abs1(),b=(C-A).abs1(),c=(A-B).abs1();\n return (A*a+B*b+C*c)/(a+b+c);\n}\nP circumCenter(P A,P B,P C){\n P bb=B-A,cc=C-A;\n db da=bb.abs2(),dc=cc.abs2(),d=2*bb.det(cc);\n return A-P(bb.y*dc-cc.y*da,cc.x*da-bb.x*dc)/d;\n}\nP othroCenter(P a,P b,P c){\n P ba=b-a,ca=c-a,bc=b-c;\n db Y=ba.y*ca.y*bc.y,\n A=ca.det(ba),\n x0=(Y+ca.x*ba.y*b.x-ba.x*ca.y*c.x)/A,\n y0=-ba.x*(x0-c.x)/ba.y+ca.y;\n return {x0,y0};\n}\npair<P,db> min_circle(vector<P>&ps){\n random_shuffle(ps.begin(),ps.end());\n int n=ps.size();\n P o=ps[0];db r=0;\n for(int i=1;i<n;i++){\n if(o.disTo(ps[i])>r+eps){\n o=ps[i],r=0;\n for(int j=0;j<i;j++){\n if(o.disTo(ps[j])>eps+r){\n o=(ps[i]+ps[j])/2;\n r=o.disTo(ps[i]);\n for(int k=0;k<j;k++){\n if(o.disTo(ps[k])>r+eps){\n o=circumCenter(ps[i],ps[j],ps[k]);\n r=o.disTo(ps[i]); \n }\n }\n }\n }\n }\n }\n return {o,r};\n}\nvector<P>minkowski(vector<P>ps,vector<P>qs){\n int n=ps.size(),m=qs.size();\n vector<P>s1(n),s2(m);\n for(int i=0;i<n;i++) s1[i]=ps[(i+1)%n]-ps[i];\n for(int i=0;i<m;i++) s2[i]=qs[(i+1)%m]-qs[i];\n vector<P>Q;\n Q.push_back(ps[0]+qs[0]);\n int i=0,j=0;\n while(i<n&&j<m){\n if(s1[i].det(s2[j])>=0) Q.push_back(Q.back()+s1[i++]);\n else Q.push_back(Q.back()+s2[j++]);\n }\n while(i<n) Q.push_back(Q.back()+s1[i++]);\n while(j<m) Q.push_back(Q.back()+s2[j++]);\n return Q;\n}\nbool inConvex(vector<P>&ps,P p){\n int n=ps.size();\n int l=0,r=n-1;\n while(l+1<r){\n int mid=(l+r)/2;\n db A=crossop(ps[0],ps[mid],p);\n db B=crossop(ps[0],ps[(mid+1)],p);\n if(A>=0&&B<=0){\n if(crossop(ps[mid],ps[mid+1],p)>=0) return true;\n return false;\n }\n else if(A<0) r=mid;\n else l=mid;\n }\n return false;\n}\nconst int N=505;\nint n,m;\nvector<vector<P>>conv;\nvector<P>ps;\nP o;\nP p0;\nint tot=0;\ndb dis[N][N];\ndb ans[N];\nbool ok[N];\nmap<int,P>mp;\nvoid debug(P os){\n cerr << \"[\" << os.x << \" \" << os.y << \"] \";\n}\nbool check(P a,P b){\n for(int i=0;i<n;i++){\n int ns=conv[i].size();\n for(int j=0;j<ns;j++){\n if(isSS_strict(a,b,conv[i][j],conv[i][(j+1)%ns])) return 0;\n }\n }\n return 1;\n}\nvoid sol(){\n for(int i=0;i<=tot;i++) ans[i]=1e15;\n for(int i=0;i<=tot;i++) ok[i]=false;\n for(int i=0;i<=tot;i++){\n for(int j=0;j<=tot;j++){\n dis[i][j]=1e15;\n }\n }\n assert(ps.size()==tot);\n for(int i=0;i<tot;i++){\n for(int j=0;j<tot;j++){\n if(check(ps[i],ps[j])){\n dis[i][j]=ps[i].disTo(ps[j]);\n dis[j][i]=ps[j].disTo(ps[i]);\n }\n }\n }\n int T=1;\n for(int i=0;i<n;i++){\n int ns=conv[i].size();\n for(int j=0;j<ns;j++){\n dis[T+j][T+(j+1)%ns]=conv[i][j].disTo(conv[i][(j+1)%ns]);\n dis[T+(j+1)%ns][T+j]=conv[i][j].disTo(conv[i][(j+1)%ns]);\n }\n T+=ns;\n }\n T=1;\n for(int i=0;i<n;i++){\n int ns=conv[i].size();\n for(int j=0;j<ns;j++){\n if(crossop(p0,conv[i][j],conv[i][(j+1)%ns])>0){\n ok[j+T]=true;\n ok[(j+1)%ns+T]=true;\n }\n }\n T+=ns;\n }\n queue<int>que;\n ans[0]=0;\n db ANS=1e15;\n que.push(0);\n while(!que.empty()){\n auto u=que.front();\n que.pop();\n auto x=mp[u];\n if(ok[u]){\n ANS=min(ANS,ans[u]);\n continue;\n }\n T=1;\n for(int i=0;i<n;i++){\n int ns=conv[i].size();\n for(int j=0;j<ns;j++){\n int id=T+j;\n if(cmp(dis[u][id],1e15)>=0) continue;\n if(ans[u]+dis[u][id]<ans[id]){\n ans[id]=ans[u]+dis[u][id];\n que.push(id);\n }\n }\n T+=ns;\n }\n T=1;\n for(int i=0;i<n;i++){\n int ns=conv[i].size();\n for(int j=0;j<ns;j++){\n int id=T+j;\n //debug(conv[i][j]);\n //cerr << id << \" \";\n //cerr << ok[id] << \"\\n\";\n if(ok[id]){\n P a=x-conv[i][j];\n P b=p0-conv[i][j];\n if(sign(a.dot(b))>0) continue;\n P h=proj(conv[i][j],p0,x);\n //debug(a);\n //debug(b);\n //cerr << \"\\n\";\n db di=h.disTo(x);\n //cerr << ans[u]+di << \"\\n\";\n if(ans[id]>ans[u]+di){\n ans[id]=ans[u]+di;\n que.push(id);\n }\n }\n }\n T+=ns;\n }\n }\n //for(int i=1;i<=8;i++) cerr << ok[i] << \" \";\n cout << ANS << \"\\n\";\n}\nvoid solve(){\n while(cin >> n){\n if(n==0) break;\n cin >> p0.x >> p0.y;\n conv.clear();\n ps.clear();\n tot=0;\n tot++;\n ps.push_back(o);\n for(int i=0;i<n;i++){\n //int m;\n cin >> m;\n vector<P>cv;\n for(int j=0;j<m;j++){\n ll x,y;\n cin >> x >> y;\n P qs;\n qs.x=x;\n qs.y=y;\n mp[j+tot]=qs;\n ps.push_back(qs);\n cv.push_back(qs);\n }\n conv.push_back(cv);\n tot+=m;\n }\n if(check(o,p0)==0){\n cout << \"0.00000000\" << \"\\n\";\n continue;\n }\n sol();\n }\n} \nint main(){\n ios::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n cout << fixed << setprecision(12);\n int t=1;\n //cin >> t;\n while(t--)\n solve();\n return 0;\n}", "accuracy": 1, "time_ms": 4020, "memory_kb": 7936, "score_of_the_acc": -1.5208, "final_rank": 19 }, { "submission_id": "aoj_2246_10142874", "code_snippet": "#define _CRT_SECURE_NO_WARNINGS\n#include <iostream>\n#include <algorithm>\n#include <cmath>\n#include <cstring>\n#include <cassert>\n#include <vector>\n#include <random>\n#include <array>\n#include <tuple>\n#include <queue>\ntypedef long long ll;\ntypedef double ld;\nconst ll INF = 1e17;\nconst int LEN = 505;\nconst ld TOL = 1e-7;\nconst ll MOD = 1'000'000'007;\nint N, M, T;\nbool zero(const ld& x) { return std::abs(x) < TOL; }\nint sign(const ld& x) { return x < -TOL ? -1 : x > TOL; }\n\nld COST[LEN];\nstruct Info {\n\tint i;\n\tld c;\n\tInfo(int I = 0, ld C = 0) : i(I), c(C) {}\n\tbool operator < (const Info& x) const { return c > x.c; }\n};\nstd::vector<Info> G[LEN];\nstd::priority_queue<Info> Q;\nld dijkstra(int v, int g) {\n\tfor (int i = 0; i < LEN; i++) COST[i] = INF;\n\tQ.push({ v, 0 });\n\tCOST[v] = 0;\n\twhile (Q.size()) {\n\t\tInfo p = Q.top(); Q.pop();\n\t\tif (p.c > COST[p.i]) continue;\n\t\tif (p.i == g) return COST[g];\n\t\tfor (const Info& w : G[p.i]) {\n\t\t\tld cost = p.c + w.c;\n\t\t\tif (COST[w.i] > cost) {\n\t\t\t\tCOST[w.i] = cost;\n\t\t\t\tQ.push({ w.i, cost });\n\t\t\t}\n\t\t}\n\t}\n\treturn COST[g];\n}\nstruct Pos {\n\tld x, y;\n\tint i;\n\tbool good;\n\tPos(ld X = 0, ld Y = 0, int I = 0, bool f = 0) : x(X), y(Y), i(I), good(f) {}\n\tinline bool operator == (const Pos& p) const { return zero(x - p.x) && zero(y - p.y); }\n\tbool operator != (const Pos& p) const { return !zero(x - p.x) || !zero(y - p.y); }\n\tbool operator < (const Pos& p) const { return zero(x - p.x) ? y < p.y : x < p.x; }\n\tinline Pos operator + (const Pos& p) const { return { x + p.x, y + p.y }; }\n\tinline Pos operator - (const Pos& p) const { return { x - p.x, y - p.y }; }\n\tinline Pos operator * (const ld& scalar) const { return { x * scalar, y * scalar }; }\n\tPos operator / (const ld& scalar) const { return { x / scalar, y / scalar }; }\n\tinline ld operator * (const Pos& p) const { return x * p.x + y * p.y; }\n\tinline ld operator / (const Pos& p) const { return x * p.y - y * p.x; }\n\tPos operator ^ (const Pos& p) const { return { x * p.x, y * p.y }; }\n\tPos operator - () const { return { -x, -y }; }\n\tPos operator ~ () const { return { -y, x }; }\n\tPos operator ! () const { return { y, x }; }\n\tPos& operator += (const Pos& p) { x += p.x; y += p.y; return *this; }\n\tPos& operator -= (const Pos& p) { x -= p.x; y -= p.y; return *this; }\n\tPos& operator *= (const ld& scale) { x *= scale; y *= scale; return *this; }\n\tPos& operator /= (const ld& scale) { x /= scale; y /= scale; return *this; }\n\tld xy() const { return x * y; }\n\tPos rot(ld the) { return { x * cos(the) - y * sin(the), x * sin(the) + y * cos(the) }; }\n\tinline ld Euc() const { return x * x + y * y; }\n\t//inline ld mag() const { return sqrt(Euc()); }\n\tinline ld mag() const { return hypot(x, y); }\n\tPos unit() const { return *this / mag(); }\n\tld rad() const { return atan2(y, x); }\n\tfriend ld rad(const Pos& p1, const Pos& p2) { return atan2l(p1 / p2, p1 * p2); }\n\tint quad() const { return sign(y) == 1 || (sign(y) == 0 && sign(x) >= 0); }\n\tfriend bool cmpq(const Pos& a, const Pos& b) { return (a.quad() != b.quad()) ? a.quad() < b.quad() : a / b > 0; }\n\tbool close(const Pos& p) const { return zero((*this - p).Euc()); }\n\tinline friend std::istream& operator >> (std::istream& is, Pos& p) { is >> p.x >> p.y; return is; }\n\tfriend std::ostream& operator << (std::ostream& os, const Pos& p) { os << p.x << \" \" << p.y; return os; }\n}; const Pos O = { 0, 0 };\ntypedef std::vector<Pos> Polygon;\nbool cmpx(const Pos& p, const Pos& q) { return p.x == q.x ? p.y < q.y : p.x < q.x; }\nbool cmpy(const Pos& p, const Pos& q) { return p.y == q.y ? p.x < q.x : p.y < q.y; }\nbool cmpi(const Pos& p, const Pos& q) { return p.i < q.i; }\nPolygon H[105];\nstruct Vec {\n\tld vy, vx;\n\tVec(ld Y = 0, ld X = 0) : vy(Y), vx(X) {}\n\tbool operator == (const Vec& v) const { return (zero(vy - v.vy) && zero(vx - v.vx)); }\n\tbool operator < (const Vec& v) const { return zero(vy - v.vy) ? vx < v.vx : vy < v.vy; }\n\tld operator * (const Vec& v) const { return vy * v.vy + vx * v.vx; }\n\tld operator / (const Vec& v) const { return vy * v.vx - vx * v.vy; }\n\tVec operator ~ () const { return { -vx, vy }; }\n\tVec& operator *= (const ld& scalar) { vy *= scalar; vx *= scalar; return *this; }\n\tVec& operator /= (const ld& scalar) { vy /= scalar; vx /= scalar; return *this; }\n\tld mag() const { return hypot(vy, vx); }\n}; const Vec Zero = { 0, 0 };\nstruct Line {//ax + by = c\n\tVec s;\n\tld c;\n\tLine(Vec V = Vec(0, 0), ld C = 0) : s(V), c(C) {}\n\tbool operator < (const Line& l) const {\n\t\tbool f1 = Zero < s;\n\t\tbool f2 = Zero < l.s;\n\t\tif (f1 != f2) return f1;\n\t\tld CCW = s / l.s;\n\t\treturn zero(CCW) ? c * hypot(l.s.vy, l.s.vx) < l.c * hypot(s.vy, s.vx) : CCW > 0;\n\t}\n\tld operator * (const Line& l) const { return s * l.s; }\n\tld operator / (const Line& l) const { return s / l.s; }\n\tLine operator + (const ld& scalar) const { return Line(s, c + hypot(s.vy, s.vx) * scalar); }\n\tLine operator - (const ld& scalar) const { return Line(s, c - hypot(s.vy, s.vx) * scalar); }\n\tLine operator * (const ld& scalar) const { return Line({ s.vy * scalar, s.vx * scalar }, c * scalar); }\n\tLine& operator += (const ld& scalar) { c += hypot(s.vy, s.vx) * scalar; return *this; }\n\tLine& operator -= (const ld& scalar) { c -= hypot(s.vy, s.vx) * scalar; return *this; }\n\tLine& operator *= (const ld& scalar) { s *= scalar, c *= scalar; return *this; }\n\tld dist(const Pos& p) const { return s.vy * p.x + s.vx * p.y; }\n\tld above(const Pos& p) const { return s.vy * p.x + s.vx * p.y - c; }\n\tld mag() const { return s.mag(); }\n\tfriend std::ostream& operator << (std::ostream& os, const Line& l) { os << l.s.vy << \" \" << l.s.vx << \" \" << l.c; return os; }\n};\ninline Line L(const Pos& s, const Pos& e) {\n\tld dy, dx, c;\n\tdy = e.y - s.y;\n\tdx = s.x - e.x;\n\tc = dy * s.x + dx * s.y;\n\treturn Line(Vec(dy, dx), c);\n}\ninline Line L(const Vec& s, const Pos& p) {\n\tld c = s.vy * p.x + s.vx * p.y;\n\treturn Line(s, c);\n}\ninline Line rotate(const Line& l, const Pos& p, ld the) {\n\tVec s = l.s;\n\tld x = -s.vx, y = s.vy;\n\tld vx = -(x * cos(the) - y * sin(the));\n\tld vy = x * sin(the) + y * cos(the);\n\tld c = vy * p.x + vx * p.y;\n\treturn Line(Vec(vy, vx), c);\n}\ninline Line rot90(const Line& l, const Pos& p) {\n\tVec s = ~l.s;\n\tld c = s.vy * p.x + s.vx * p.y;\n\treturn Line(s, c);\n}\ninline Pos intersection(const Line& l1, const Line& l2) {\n\tVec v1 = l1.s, v2 = l2.s;\n\tld det = v1 / v2;\n\treturn {\n\t\t(l1.c * v2.vx - l2.c * v1.vx) / det,\n\t\t(l2.c * v1.vy - l1.c * v2.vy) / det,\n\t};\n}\ninline ld cross(const Pos& d1, const Pos& d2, const Pos& d3) { return (d2 - d1) / (d3 - d2); }\ninline ld cross(const Pos& d1, const Pos& d2, const Pos& d3, const Pos& d4) { return (d2 - d1) / (d4 - d3); }\ninline ld dot(const Pos& d1, const Pos& d2, const Pos& d3) { return (d2 - d1) * (d3 - d2); }\ninline int ccw(const Pos& d1, const Pos& d2, const Pos& d3) {\n\tld ret = cross(d1, d2, d3);\n\treturn zero(ret) ? 0 : ret > 0 ? 1 : -1;\n}\ninline int ccw(const Pos& d1, const Pos& d2, const Pos& d3, const Pos& d4) {\n\tld ret = cross(d1, d2, d3, d4);\n\treturn zero(ret) ? 0 : ret > 0 ? 1 : -1;\n}\ninline bool on_seg_strong(const Pos& d1, const Pos& d2, const Pos& d3) {\n\tld ret = dot(d1, d3, d2);\n\treturn !ccw(d1, d2, d3) && (ret > 0 || zero(ret));\n}\ninline bool on_seg_weak(const Pos& d1, const Pos& d2, const Pos& d3) {\n\tld ret = dot(d1, d3, d2);\n\treturn !ccw(d1, d2, d3) && ret > 0;\n}\ninline bool intersect(const Pos& s1, const Pos& s2, const Pos& d1, const Pos& d2) {\n\tbool f1 = ccw(s1, s2, d1) * ccw(s2, s1, d2) > 0;\n\tbool f2 = ccw(d1, d2, s1) * ccw(d2, d1, s2) > 0;\n\treturn f1 && f2;\n\t//bool f3 = on_seg_strong(s1, s2, d1) ||\n\t//\ton_seg_strong(s1, s2, d2) ||\n\t//\ton_seg_strong(d1, d2, s1) ||\n\t//\ton_seg_strong(d1, d2, s2);\n\t//return (f1 && f2) || f3;\n}\ninline ld area(std::vector<Pos>& H) {\n\tld ret = 0;\n\tint sz = H.size();\n\tfor (int i = 0; i < sz; i++) {\n\t\tPos cur = H[i], nxt = H[(i + 1) % sz];\n\t\tret += cross(O, cur, nxt);\n\t}\n\treturn ret;\n}\ninline bool norm(std::vector<Pos>& H) { \n\tif (area(H) < TOL) {\n\t\tstd::reverse(H.begin(), H.end());\n\t\treturn 0;\n\t}\n\treturn 1;\n}\ninline int inner_check(const std::vector<Pos>& H, const Pos& p) {//concave\n\tint cnt = 0, sz = H.size();\n\tfor (int i = 0; i < sz; i++) {\n\t\tPos cur = H[i], nxt = H[(i + 1) % sz];\n\t\tif (on_seg_strong(cur, nxt, p)) return 1;\n\t\tif (zero(cur.y - nxt.y)) continue;\n\t\tif (nxt.y < cur.y) std::swap(cur, nxt);\n\t\tif (nxt.y - TOL < p.y || cur.y > p.y) continue;\n\t\tcnt += ccw(cur, nxt, p) > 0;\n\t}\n\treturn (cnt & 1) * 2;\n}\ninline int on_seg_check(const Pos& p) {\n\tfor (int i = 0; i < N; i++) {\n\t\tint sz = H[i].size();\n\t\tfor (int j = 0; j < sz; j++) {\n\t\t\tPos& a = H[i][j], b = H[i][(j + 1) % sz];\n\t\t\tif (on_seg_strong(a, b, p)) return i;\n\t\t}\n\t}\n\treturn -2;\n}\ninline bool meaningless(const Pos& b, const Pos& p) {\n\tbool r = 0, l = 0;\n\tfor (int i = 0; i < N; i++) {\n\t\tint sz = H[i].size();\n\t\tfor (int j = 0; j < sz; j++) {\n\t\t\tPos cur = H[i][j], nxt = H[i][(j + 1) % sz];\n\t\t\tif (intersect(b, p, cur, nxt)) return 1;\n\t\t\tif (on_seg_weak(b, p, cur) && ccw(b, p, cur, nxt) > 0) l = 1;\n\t\t\tif (on_seg_weak(b, p, cur) && ccw(b, p, cur, nxt) < 0) r = 1;\n\t\t\tif (on_seg_weak(b, p, nxt) && ccw(b, p, nxt, cur) > 0) l = 1;\n\t\t\tif (on_seg_weak(b, p, nxt) && ccw(b, p, nxt, cur) < 0) r = 1;\n\t\t\tif (r && l) return 1;\n\t\t}\n\t}\n\treturn 0;\n}\ninline bool blocked(const Pos& u, const Pos& v, const int& u_idx, const int& v_idx) {\n\tif (u_idx == v_idx) {\n\t\tint sz = H[u_idx].size();\n\t\tfor (int i = 0; i < sz; i++) {\n\t\t\tPos cur = H[u_idx][i], nxt = H[u_idx][(i + 1) % sz];\n\t\t\tif (intersect(u, v, cur, nxt)) return 1;\n\t\t\tif (on_seg_weak(u, v, cur)) return 1;\n\t\t}\n\t\tPos m = (u + v) * .5;\n\t\t//std::cout << \"m : \" << m << \"\\n\";\n\t\t//std::cout << inner_check(H[u_idx], m) << \"\\n\";\n\t\treturn inner_check(H[u_idx], m) > 1;\n\t}\n\telse {\n\t\tfor (int i = 0; i < N; i++) {\n\t\t\tint sz = H[i].size();\n\t\t\tfor (int j = 0; j < sz; j++) {\n\t\t\t\tPos cur = H[i][j], nxt = H[i][(j + 1) % sz];\n\t\t\t\tif (intersect(u, v, cur, nxt)) return 1;\n\t\t\t\tif (on_seg_weak(u, v, cur)) return 1;\n\t\t\t}\n\t\t}\n\t\treturn 0;\n\t}\n}\ninline bool covered(const Pos& b, const int& h_idx, const int& p_idx) {\n\tbool r = 0, l = 0;\n\tint sz = H[h_idx].size();\n\tif (ccw(b, H[h_idx][p_idx], H[h_idx][(p_idx + 1) % sz]) > 0) return 1;\n\tif (ccw(b, H[h_idx][p_idx], H[h_idx][((p_idx - 1 + sz) % sz)]) < 0) return 1;\n\tPos& p = H[h_idx][p_idx];\n\tfor (int i = 0; i < N; i++) {\n\t\tsz = H[i].size();\n\t\tfor (int j = 0; j < sz; j++) {\n\t\t\tPos cur = H[i][j], nxt = H[i][(j + 1) % sz];\n\t\t\tif (intersect(b, p, cur, nxt)) return 1;\n\t\t\tif (on_seg_weak(b, p, cur) && ccw(b, p, cur, nxt) > 0) l = 1;\n\t\t\tif (on_seg_weak(b, p, cur) && ccw(b, p, cur, nxt) < 0) r = 1;\n\t\t\tif (on_seg_weak(b, p, nxt) && ccw(b, p, nxt, cur) > 0) l = 1;\n\t\t\tif (on_seg_weak(b, p, nxt) && ccw(b, p, nxt, cur) < 0) r = 1;\n\t\t\tif (r && l) return 1;\n\t\t}\n\t}\n\treturn 0;\n}\ninline void init() {\n\twhile (Q.size()) Q.pop();\n\tfor (int i = 0; i < 100; i++) std::vector<Pos>().swap(H[i]);\n\tfor (int i = 0; i < LEN; i++) std::vector<Info>().swap(G[i]);\n\treturn;\n}\nld query() {\n\tinit();\n\tint goal = 0;\n\tPos s, bomb;\n\ts = O; s.i = 1;\n\tstd::cin >> bomb; bomb.i = -1;\n\tT = 1;\n\tint X = -1;//if Alice is located at the boundary of a polygon X = polygon.i\n\tfor (int i = 0; i < N; i++) {\n\t\tstd::cin >> M;\n\t\tH[i].resize(M);\n\t\tfor (Pos& p : H[i]) std::cin >> p;\n\t\tassert(norm(H[i]));\n\t\tfor (Pos& p : H[i]) p.i = ++T;\n\t\tint f = inner_check(H[i], s);\n#ifdef ASSERT\n\t\tassert(f < 2);\n\t\tassert(!inner_check(H[i], bomb));\n#endif\n\t\tif (f) X = i;\n\t}\n\tint f1 = 0;\n\tfor (int i = 0; i < N; i++) {\n\t\tint sz = H[i].size();\n\t\tfor (int j = 0; j < sz; j++) {\n\t\t\tif (covered(bomb, i, j)) H[i][j].good = 1;\n\t\t\tif (H[i][j].good && H[i][j] == s) f1 = 1;\n\t\t}\n\t}\n\n\t//std::cout << \"DEBUG\\n\";\n\t//for (Pos& p : H[0]) std::cout << p << \" \" << p.good << \"\\n\";\n\t//std::cout << \"DEBUG\\n\";\n\n\tif (f1 || blocked(bomb, s, -1, -2)) return 0;\n\n\tPos inx;\n\tLine safe, sht, seg;//safe line, short, segment\n\tfor (int i = 0; i < N; i++) {\n\t\tint szi = H[i].size();\n\t\tfor (int j = 0; j < szi; j++) {//O(500)\n\t\t\tPos& p = H[i][j];\n\t\t\tif (p.good) {\n\t\t\t\tG[p.i].push_back(Info(goal, 0));\n\t\t\t\tsafe = L(bomb, p);\n\t\t\t\tsht = rot90(safe, s);\n\t\t\t\tinx = intersection(safe, sht);\n\t\t\t\tif (!meaningless(bomb, inx)) {\n\t\t\t\t\tif (on_seg_weak(bomb, inx, p)) {\n\t\t\t\t\t\tinx.i = on_seg_check(inx);\n\t\t\t\t\t\tif (inx.i == X) {\n\t\t\t\t\t\t\tif (!blocked(s, inx, -1, inx.i) && !blocked(s, inx, X, inx.i))\n\t\t\t\t\t\t\t\tG[1].push_back(Info(goal, (s - inx).mag()));\n\t\t\t\t\t\t}\n\t\t\t\t\t\telse {\n\t\t\t\t\t\t\tif (!blocked(s, inx, X, inx.i))\n\t\t\t\t\t\t\t\tG[1].push_back(Info(goal, (s - inx).mag()));\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tfor (int k = 0; k < N; k++) {\n\t\t\t\t\tint szk = H[k].size();\n\t\t\t\t\tfor (int l = 0; l < szk; l++) {//(O(250 * 250)\n\t\t\t\t\t\tPos& v = H[k][l];\n\t\t\t\t\t\tif (v.good) continue;\n\n\t\t\t\t\t\tsht = rot90(safe, v);\n\t\t\t\t\t\tinx = intersection(safe, sht);\n\t\t\t\t\t\tif (meaningless(bomb, inx)) continue;\n\n\t\t\t\t\t\tif (on_seg_weak(bomb, inx, p)) {//O(250 * 250 * 500)\n\t\t\t\t\t\t\tinx.i = on_seg_check(inx);\n\t\t\t\t\t\t\tif (inx.i == k) {\n\t\t\t\t\t\t\t\tif (!blocked(v, inx, -1, k) && !blocked(v, inx, k, inx.i)) {\n\t\t\t\t\t\t\t\t\tG[v.i].push_back(Info(goal, (v - inx).mag()));\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\telse {\n\t\t\t\t\t\t\t\tif (!blocked(v, inx, k, inx.i)) {\n\t\t\t\t\t\t\t\t\tG[v.i].push_back(Info(goal, (v - inx).mag()));\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tfor (int k = 0; k < N; k++) {\n\t\t\t\t\tint szk = H[k].size();\n\t\t\t\t\tfor (int l = 0; l < szk; l++) {//O(250 * 250)\n\t\t\t\t\t\tPos& a = H[k][l], b = H[k][(l + 1) % szk];\n\t\t\t\t\t\tseg = L(a, b);\n\t\t\t\t\t\tif (zero(safe / seg)) continue;\n\n\t\t\t\t\t\tinx = intersection(safe, seg);\n\t\t\t\t\t\tif (!on_seg_weak(a, b, inx)) continue;\n\t\t\t\t\t\tif (!on_seg_weak(bomb, inx, p)) continue;\n\t\t\t\t\t\tif (meaningless(bomb, inx)) continue;\n\n\t\t\t\t\t\tinx.i = on_seg_check(inx);\n\t\t\t\t\t\tif (inx.i == X) {//O(250 * 250 * 500)\n\t\t\t\t\t\t\tif (!blocked(s, inx, -1, inx.i) && !blocked(s, inx, X, inx.i))\n\t\t\t\t\t\t\t\tG[1].push_back(Info(goal, (s - inx).mag()));\n\t\t\t\t\t\t}\n\t\t\t\t\t\telse {\n\t\t\t\t\t\t\tif (!blocked(s, inx, X, inx.i))\n\t\t\t\t\t\t\t\tG[1].push_back(Info(goal, (s - inx).mag()));\n\t\t\t\t\t\t}\n\t\t\t\t\t\tfor (int m = 0; m < N; m++) {\n\t\t\t\t\t\t\tint szm = H[m].size();\n\t\t\t\t\t\t\tfor (int n = 0; n < szm; n++) {//O(250 * 250 * 250)\n\t\t\t\t\t\t\t\tPos& v = H[m][n];\n\t\t\t\t\t\t\t\tif (v.good) continue;\n\t\t\t\t\t\t\t\tif (inx.i == m) {//O(250 * 250 * 250 * 500)\n\t\t\t\t\t\t\t\t\tif (!blocked(v, inx, -1, m) && !blocked(v, inx, m, inx.i)) {\n\t\t\t\t\t\t\t\t\t\tG[v.i].push_back(Info(goal, (v - inx).mag()));\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\telse {\n\t\t\t\t\t\t\t\t\tif (!blocked(v, inx, m, inx.i)) {\n\t\t\t\t\t\t\t\t\t\tG[v.i].push_back(Info(goal, (v - inx).mag()));\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tfor (int i = 0; i < N; i++) {\n\t\tint szi = H[i].size();\n\t\tfor (int j = 0; j < szi; j++) {//O(500)\n\t\t\tPos& u = H[i][j];\n\t\t\tif (i == X) {\n\t\t\t\tif (!blocked(s, u, X, i) && !blocked(s, u, -1, -2)) {\n\t\t\t\t\tG[1].push_back(Info(u.i, (u - s).mag()));\n\t\t\t\t\t//std::cout << \"fucked\\n\";\n\t\t\t\t\t//std::cout << u << \"\\n\";\n\t\t\t\t}\n\t\t\t}\n\t\t\telse {\n\t\t\t\tif (!blocked(s, u, X, i)) {\n\t\t\t\t\tG[1].push_back(Info(u.i, (u - s).mag()));\n\t\t\t\t}\n\t\t\t}\n\t\t\tfor (int k = i; k < N; k++) {\n\t\t\t\tint szk = H[k].size();\n\t\t\t\tfor (int l = 0; l < szk; l++) {//O(500 * 500)\n\t\t\t\t\tPos& v = H[k][l];\n\t\t\t\t\tif (u.good && v.good) continue;\n\t\t\t\t\tif (i == k) {\n\t\t\t\t\t\tif (!blocked(u, v, i, k) && !blocked(u, v, -1, -2)) {\n\t\t\t\t\t\t\tG[u.i].push_back(Info(v.i, (u - v).mag()));\n\t\t\t\t\t\t\tG[v.i].push_back(Info(u.i, (u - v).mag()));\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\telse {\n\t\t\t\t\t\tif (!blocked(u, v, i, k)) {\n\t\t\t\t\t\t\tG[u.i].push_back(Info(v.i, (u - v).mag()));\n\t\t\t\t\t\t\tG[v.i].push_back(Info(u.i, (u - v).mag()));\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\t//std::cout << X << \"\\n\";\n\t//for (int i = 0; i < T; i++) {\n\t//\tfor (Info& i : G[i]) std::cout << i.i << \" \" << i.c << \"\\n\";\n\t//\tstd::cout << \"DEBUG\\n\";\n\t//}\n\tld cost = dijkstra(s.i, goal);\n\treturn cost;\n}\nvoid solve() {\n\tstd::cin.tie(0)->sync_with_stdio(0);\n\tstd::cout.tie(0);\n\tstd::cout << std::fixed;\n\tstd::cout.precision(15);\n\twhile (1) {\n\t\tstd::cin >> N;\n\t\tif (!N) break;\n\t\tstd::cout << query() << \"\\n\";\n\t}\n\treturn;\n}\nint main() { solve(); return 0; }//JAG Practice Contest 2010 E boj13801\n\n\n/*\n\n1\n1 1\n4 -1 0 -2 -1 -1 -2 0 -1\n1\n0 3\n4 1 1 1 2 -1 2 -1 1\n1\n-6 -6\n6 1 -2 2 -2 2 3 -2 3 -2 1 1 1\n1\n6 4\n6 5 -498 6 2 5 2 5 1 3 1 -999 -498\n1\n-10 0\n4 0 -5 1 -5 1 5 0 5\n1\n-10 0\n4 0 0 2 -2 1 0 2 2\n1\n-10 0\n4 1 0 3 -2 2 0 3 2\n1\n-5 -1\n4 -10 -1 -9 0 -10 1 -11 0\n0\n\n1.0\n0.0\n3.23606798\n4.66227766\n5.0000000000\n2.8284271247\n3.6055512755\n10.0498756211\n\n*/", "accuracy": 1, "time_ms": 1630, "memory_kb": 6992, "score_of_the_acc": -1.0488, "final_rank": 10 }, { "submission_id": "aoj_2246_9208213", "code_snippet": "#include \"bits/stdc++.h\"\n\n#define all(x) x.begin(), x.end()\n#define NL std::cout << '\\n'\n\nusing lnt = long long;\n\nconst int inf = 0x3f3f3f3f;\nconst lnt linf = 1ll << 61;\n\nusing db = long double; const db eps = 1e-7;\nint sgn(db x) { return (x < -eps ? -1 : x > eps); }\n\nstruct Pt { db x, y; };\nstd::istream& operator>>(std::istream& is, Pt &p) {\n int x, y; std::cin >> x >> y;\n p.x = x; p.y = y; return is;\n}\nstd::ostream& operator<<(std::ostream& os, Pt p) {\n return os << \"(\" << p.x << \",\" << p.y << \")\";\n}\n\nPt operator-(Pt a, Pt b) { return { a.x - b.x, a.y - b.y }; }\nPt operator+(Pt a, Pt b) { return { a.x + b.x, a.y + b.y }; }\nPt operator*(Pt a, db x) { return { a.x * x, a.y * x }; }\nPt operator/(Pt a, db x) { return { a.x / x, a.y / x }; }\ndb dot(Pt a, Pt b) { return a.x * b.x + a.y * b.y; }\ndb det(Pt a, Pt b) { return a.x * b.y - a.y * b.x; }\nbool operator==(Pt a, Pt b) { return !sgn(dot(a-b, a-b)); }\nbool operator!=(Pt a, Pt b) { return sgn(dot(a-b, a-b)); }\nbool operator<(Pt a, Pt b) { return a.x == b.x ? a.y < b.y : a.x < b.x; }\ndb dis2(Pt a, Pt b) { return dot(a-b, a-b); }\ndb dis(Pt a, Pt b) { return std::sqrt(dis2(a, b)); }\nint side(Pt a, Pt b, Pt x) { return sgn(det(b-a, x-a)); }\nPt rot90(Pt a) { return {-a.y, a.x}; }\nPt rot(Pt a, db t) {\n return { a.x * std::cos(t) - a.y * std::sin(t)\n , a.x * std::sin(t) + a.y * std::cos(t) };\n}\nPt unit(Pt a) {\n db d = dis(a, {0, 0});\n return sgn(d) ? a/d : Pt{0, 0};\n}\n\n\nstruct Line { Pt s, t; };\n\nPt intersect(Line a, Line b) {\n db v = det(a.t - a.s, b.s - a.s);\n db u = det(a.t - a.s, b.t - a.s);\n return (b.s * u - b.t * v) / (u - v);\n}\nbool para(Line a, Line b) {\n return !sgn(det(a.t - a.s, b.t - b.s));\n}\n\nbool ptOnSeg(Pt x, Line v) {\n if (v.s == v.t) { return sgn(dis2(v.s, x)) == 0; }\n return sgn(det(v.s - x, v.t - x)) == 0\n && sgn(dot(v.s - x, v.t - x)) <= 0;\n}\n\nbool ptOnSeg_strict(Pt x, Line v) {\n if (v.s == v.t) { return sgn(dis2(v.s, x)) == 0; }\n return sgn(det(v.s - x, v.t - x)) == 0\n && sgn(dot(v.s - x, v.t - x)) < 0;\n}\n\nbool segInter(Line a, Line b) {\n if (sgn(det(b.s-a.s, a.t-a.s)) * sgn(det(b.t-a.s, a.t-a.s)) == -1\n && sgn(det(a.s-b.s, b.t-b.s)) * sgn(det(a.t-b.s, b.t-b.s)) == -1) {\n return true;\n }\n return false;\n}\n\nbool ptInPoly(Pt p, std::vector<Pt> &v) {\n int cnt = 0;\n for (int i = v.size() - 1, j = 0; j < v.size() - 2; i = j++) {\n if (ptOnSeg(p, {v[i], v[j]})) { return true; }\n int x = sgn(det(p - v[i], v[j] - v[i]));\n int y = sgn(v[i].y - p.y);\n int z = sgn(v[j].y - p.y);\n if (x > 0 && y <= 0 && z > 0) { ++cnt; }\n if (x < 0 && z <= 0 && y > 0) { --cnt; }\n }\n return cnt != 0;\n}\n\n\nPt projection(Pt p, Line l) {\n Pt v = l.t - l.s;\n return l.s + v * (dot(p - l.s, v) / dot(v, v));\n}\n\nbool segInPoly(Line s, std::vector<Pt> &v) {\n const int n = v.size();\n for (int i = 0; i < n - 2; ++i) {\n const Pt a = v[i], b = v[(i+1)], c = v[(i+2)];\n if (segInter(s, {a, b})) { return false; }\n if (ptOnSeg_strict(s.s, {a, b}) && side(a, b, s.t) < 0) { return false; }\n if (ptOnSeg_strict(s.t, {a, b}) && side(a, b, s.s) < 0) { return false; }\n if (ptOnSeg(b, s)) {\n bool ok = true, acute = side(a, b, c) > 0;\n for (auto x : {s.s, s.t}) {\n if (acute) {\n ok &= side(a, b, x) >= 0 && side(b, c, x) >= 0;\n } else {\n ok &= side(a, b, x) >= 0 || side(b, c, x) >= 0;\n }\n }\n if (!ok) { return false; }\n }\n }\n return true;\n}\n\nint main() {\n std::ios::sync_with_stdio(0), std::cin.tie(0);\n int n;\n const Pt alice = {0, 0};\n while (std::cin >> n && n) {\n Pt bomb; std::cin >> bomb;\n std::vector<std::vector<Pt>> poly(n);\n std::vector<std::vector<Pt>> rpoly(n);\n for (int i = 0; i < n; ++i) {\n int k; std::cin >> k;\n poly[i].resize(k);\n for (int j = 0; j < k; ++j) {\n std::cin >> poly[i][j];\n }\n rpoly[i] = poly[i];\n std::reverse(all(rpoly[i]));\n poly[i].emplace_back(poly[i][0]); poly[i].emplace_back(poly[i][1]);\n rpoly[i].emplace_back(rpoly[i][0]); rpoly[i].emplace_back(rpoly[i][1]);\n }\n\n\n auto visible = [&](Pt u, Pt v) {\n for (int i = 0; i < n; ++i) {\n if (!segInPoly({u, v}, rpoly[i])) { return false; }\n }\n return true;\n };\n\n auto alive = [&](Pt u) {\n for (int j = 0; j < n; ++j) {\n for (int k = 0; k < poly[j].size() - 2; ++k) {\n Pt a = poly[j][k], b = poly[j][k+1], c = poly[j][k+2];\n if (segInter({a, b}, {bomb, u})) { return true; }\n if (!sgn(det(u - bomb, a - bomb))\n && sgn(dot(bomb - u, a - u)) > 0\n && sgn(dot(u - bomb, a - bomb)) >= 0) { return true; }\n if ((b == u) && (side(bomb, u, a) == side(bomb, u, c))) { return true; }\n }\n }\n return false;\n };\n\n if (!visible(bomb, alice)) {\n std::cout << \"0\"; NL;\n continue;\n }\n\n std::vector<Pt> vt({alice});\n for (int i = 0; i < n; ++i) {\n for (auto x : poly[i]) {\n vt.emplace_back(x);\n }\n }\n\n std::vector<Line> ls;\n\n std::sort(vt.begin() + 1, vt.end());\n vt.erase(std::unique(all(vt)), vt.end());\n\n for (auto x : vt) {\n if (alive(x)) {\n ls.push_back({bomb, x});\n }\n }\n\n\n std::vector<Pt> v3;\n for (const auto &l : ls) {\n for (int i = 0; i < n; ++i) {\n for (int j = 0; j < poly[i].size() - 2; ++j) {\n if (side(l.s, l.t, poly[i][j]) * side(l.s, l.t, poly[i][j+1]) != -1) { continue; }\n Pt inter = intersect(l, {poly[i][j], poly[i][j+1]});\n if (alive(inter)) { v3.emplace_back(inter); }\n }\n }\n }\n\n std::sort(all(v3));\n v3.erase(std::unique(all(v3)), v3.end());\n\n db ret = inf;\n std::vector<db> d(vt.size(), inf);\n std::priority_queue<std::pair<db, int>, std::vector<std::pair<db, int>>, std::greater<>> q;\n d[0] = 0;\n q.emplace(0, 0);\n while (!q.empty()) {\n auto [ddis, u] = q.top(); q.pop();\n if (ret < ddis) { break; }\n if (d[u] < ddis) { continue; }\n if (alive(vt[u])) { ret = ddis; break; }\n for (auto l : ls) {\n Pt proj = projection(vt[u], l);\n if (visible(proj, vt[u]) && alive(proj)) { ret = std::min(ret, ddis + dis(proj, vt[u])); }\n }\n for (auto x : v3) {\n if (visible(x, vt[u])) { ret = std::min(ret, ddis + dis(x, vt[u])); }\n }\n for (int v = 0; v < vt.size(); ++v) {\n db w = dis(vt[v], vt[u]);\n if (w + ddis >= d[v] || !visible(vt[v], vt[u])) { continue; }\n d[v] = w + ddis;\n q.emplace(d[v], v);\n }\n }\n std::cout << std::fixed << std::setprecision(8);\n std::cout << ret; NL;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 4110, "memory_kb": 3648, "score_of_the_acc": -0.8971, "final_rank": 8 }, { "submission_id": "aoj_2246_6023695", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#define Vector Point\nconst double eps = 1e-10, pi = acos(-1.0);\ndouble Sqr(const double &x) {\n\treturn x * x;\n}\nint Dcmp(const double &x) {\n\treturn x < -eps ? -1 : x > eps ? 1 : 0;\n}\nstruct Point {\n\tdouble x, y;\n\tPoint(double x = 0, double y = 0) : x(x), y(y) {}\n\tint operator == (const Point &rhs) const {\n\t\treturn Dcmp(x - rhs.x) == 0 && Dcmp(y - rhs.y) == 0;\n\t}\n\tPoint operator + (const Point &rhs) const {\n\t\treturn Point(x + rhs.x, y + rhs.y);\n\t}\n\tPoint operator - (const Point &rhs) const {\n\t\treturn Point(x - rhs.x, y - rhs.y);\n\t}\n\tPoint operator * (const double &k) const {\n\t\treturn Point(x * k, y * k);\n\t}\n\tPoint operator / (const double &k) const {\n\t\treturn Point(x / k, y / k);\n\t}\n\tdouble operator ^ (const Point &rhs) const {\n\t\treturn x * rhs.y - y * rhs.x;\n\t}\n\tdouble operator * (const Point &rhs) const {\n\t\treturn x * rhs.x + y * rhs.y;\n\t}\n\tdouble len2() {\n\t\treturn x * x + y * y;\n\t}\n\tdouble len() {\n\t\treturn sqrt(len2());\n\t}\n\tPoint Rotate90() {\n\t\treturn Point(-y, x);\n\t}\n\tPoint Rotate(const double &rad) { // counterclockwise\n\t\treturn Point(x * cos(rad) - y * sin(rad), x * sin(rad) + y * cos(rad));\n\t}\n\tPoint Unit() {\n\t\tdouble l = len();\n\t\treturn (*this) / l;\n\t}\n}bomb, alice;\nstruct Seg {\n\tPoint s, t;\n\tSeg(Point s = Point(), Point t = Point()) : s(s), t(t) {}\n};\nPoint poly[105][105], s[505];\nint safe[105][105], id[105][105], n, tot[105], vis[505];\ndouble dis[505], rd[505][505];\nstruct Node {\n\tdouble dis;\n\tint id;\n\tNode(double dis = 0, int id = 0) : dis(dis), id(id) {}\n\tinline bool operator < (const Node &rhs) const {\n\t\treturn Dcmp(dis - rhs.dis) > 0;\n\t}\n};\ninline int Intersect(Seg s1, Seg s2) {\n// cerr << s1.s.x << \" \" << s1.s.y << \" \" << s1.t.x << \" \" << s1.t.y << endl;\n// cerr << s2.s.x << \" \" << s2.s.y << \" \" << s2.t.x << \" \" << s2.t.y << endl;\n// cerr << ((s1.t - s1.s) ^ (s2.s - s1.s)) * ((s1.t - s1.s) ^ (s2.t - s1.s)) << endl;\n// cerr << ((s2.t - s2.s) ^ (s1.s - s2.s)) * ((s2.t - s2.s) ^ (s1.t - s2.s)) << endl;\n\treturn Dcmp(((s1.t - s1.s) ^ (s2.s - s1.s)) * ((s1.t - s1.s) ^ (s2.t - s1.s))) < 0 && Dcmp(((s2.t - s2.s) ^ (s1.s - s2.s)) * ((s2.t - s2.s) ^ (s1.t - s2.s))) < 0;\n}\ninline int Can(Point a, Point b) {\n\tSeg now = Seg(a, b);\n\tfor(int i = 1; i <= n; ++ i)\n\t\tfor(int j = 0; j < tot[i]; ++ j)\n\t\t\tif(Intersect(now, Seg(poly[i][j], poly[i][j + 1])))\n\t\t\t\treturn 0;\n\treturn 1;\n}\ninline Point Inter(Seg l1, Seg l2) {\n// cerr << l1.s.x << \" \" << l1.s.y << \" \" << l1.t.x << \" \" << l1.t.y << endl;\n// cerr << l2.s.x << \" \" << l2.s.y << \" \" << l2.t.x << \" \" << l2.t.y << endl;\n\tdouble a1 = (l2.t - l2.s) ^ (l1.s - l2.s), a2 = (l2.t - l2.s) ^ (l1.t - l2.s);\n// cerr << a1 << \" \" << a2 << endl;\n\treturn (l1.s * a2 - l1.t * a1) / (a2 - a1);\n}\ninline Point Vertical(Point p, Seg s) {\n\tPoint v = s.t - s.s;\n\tv = v.Rotate90();\n\treturn Inter(Seg(p, p + v), s);\n}\ninline int OnSeg(Point p, Seg s) {\n\tif(Dcmp((p - s.s) * (s.t - s.s)) < 0)\n\t\treturn 0;\n\tif(Dcmp((p - s.t) * (s.s - s.t)) < 0)\n\t\treturn 0;\n\treturn 1;\n}\npriority_queue<Node>q;\nint main() {\n\twhile(scanf(\"%d\", &n) && n) {\n\t\tscanf(\"%lf%lf\", &bomb.x, &bomb.y);\n\t\tint cnt = 0;\n\t\tfor(int i = 1; i <= n; ++ i) {\n\t\t\tscanf(\"%d\", &tot[i]);\n\t\t\tfor(int j = 0; j < tot[i]; ++ j)\n\t\t\t\tscanf(\"%lf%lf\", &poly[i][j].x, &poly[i][j].y);\n\t\t\tpoly[i][tot[i]] = poly[i][0];\n\t\t\tfor(int j = 0; j < tot[i]; ++ j)\n\t\t\t\tif(Dcmp((poly[i][j] - bomb) ^ (poly[i][j + 1] - bomb)) > 0)\n\t\t\t\t\tsafe[i][j] = 1;\n\t\t\t\telse\n\t\t\t\t\tsafe[i][j] = 0;\n\t\t\tfor(int j = 1; j <= tot[i]; ++ j)\n\t\t\t\ts[++ cnt] = poly[i][j], vis[cnt] = safe[i][j - 1] | safe[i][j];\n\t\t}\n// cerr << \"????\" << cnt << endl;\n\t\tif(!Can(s[0], bomb)) {\n\t\t\tprintf(\"%.15f\\n\", 0.0);\n\t\t\tcontinue;\n\t\t}\n\t\tfor(int i = 0; i <= cnt; ++ i) {\n// cerr << s[i].x << \" \" << s[i].y << endl;\n\t\t\trd[i][i] = 0;\n\t\t\tfor(int j = i + 1; j <= cnt; ++ j)\n\t\t\t\tif(Can(s[i], s[j]))\n\t\t\t\t\trd[i][j] = rd[j][i] = (s[i] - s[j]).len();\n\t\t\t\telse\n\t\t\t\t\trd[i][j] = rd[j][i] = 1e18;\n\t\t}\n\t\tfor(int i = 1; i <= cnt; ++ i)\n\t\t\tdis[i] = 1e18;\n\t\tq.push(Node(0, 0));\n\t\twhile(!q.empty()) {\n\t\t\tNode now = q.top();\n\t\t\tq.pop();\n\t\t\tint id = now.id;\n\t\t\tif(Dcmp(dis[id] - now.dis))\n\t\t\t\tcontinue;\n\t\t\tfor(int i = 1; i <= cnt; ++ i) {\n\t\t\t\tif(id == i)\n\t\t\t\t\tcontinue;\n\t\t\t\tif(dis[i] > dis[id] + rd[id][i])\n\t\t\t\t\tdis[i] = dis[id] + rd[id][i], q.push(Node(dis[i], i));\n\t\t\t}\n\t\t}\n\t\tdouble ans = 1e18;\n\t\tfor(int i = 0; i <= cnt; ++ i) {\n// cerr << dis[i] << endl;\n\t\t\tif(dis[i] > 1e17)\n\t\t\t\tcontinue;\n\t\t\tif(vis[i])\n\t\t\t\tans = min(ans, dis[i]);\n\t\t\tfor(int j = 1; j <= cnt; ++ j)\n\t\t\t\tif(vis[j] && Dcmp((s[i] - s[j]) * (bomb - s[j])) <= 0) {\n\t\t// cerr << i << \" \" << j << endl;\n\t\t\t\t\tPoint v = Vertical(s[i], Seg(s[j], bomb));\n\t\t// cerr << (s[i] - v).len() << endl;\n\t\t\t\t\tans = min(ans, dis[i] + (s[i] - v).len());\n\t\t\t\t}\n\t\t}\n\t\tprintf(\"%.10f\\n\", ans);\n\t}\n}", "accuracy": 1, "time_ms": 270, "memory_kb": 5704, "score_of_the_acc": -0.6688, "final_rank": 7 }, { "submission_id": "aoj_2246_4856305", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 1000000000000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n\n#define SIZE 10005\n#define EPS2 0.1\n\nstruct Point{\n\tPoint(double arg_x,double arg_y){\n\t\tx = arg_x;\n\t\ty = arg_y;\n\t}\n\n\tPoint(){\n\t\tx = y = 0.0;\n\t}\n\n\tPoint operator + (Point p){ return Point(x+p.x,y+p.y); }\n\tPoint operator - (Point p){ return Point(x-p.x,y-p.y);}\n\tPoint operator * (double a){ return Point(a*x,a*y); }\n\tPoint operator / (double a){ return Point(x/a,y/a); }\n\n\tdouble abs(){ return sqrt(norm()); }\n\tdouble norm(){ return x*x + y*y; }\n\n\tbool operator<(const Point &p) const{\n\t\treturn x != p.x? x < p.x: y < p.y;\n\t}\n\n\tbool operator == (const Point &p) const{\n\t\treturn fabs(x-p.x) < EPS && fabs(y-p.y) < EPS;\n\t}\n\n \t/*\n\tvoid debug(){\n\n\t\tprintf(\"(%.3lf,%.3lf)\\n\",x,y);\n\t}*/\n\n\tdouble x,y;\n};\n\ntypedef Point Vector;\ntypedef vector<Point> Polygon;\n\nstruct Line{\n\tLine(){\n\n\t}\n\tLine(Point a,Point b){\n\t\tp[0] = a;\n\t\tp[1] = b;\n\t}\n\t/*void outPut(){\n\n\t\tprintf(\"(%.3lf,%.3lf)-(%.3lf,%.3lf)\\n\",p[0].x,p[0].y,p[1].x,p[1].y);\n\t}*/\n\tPoint p[2];\n};\n\nstruct Edge{\n\tEdge(int arg_to,double arg_dist){\n\t\tto = arg_to;\n\t\tdist = arg_dist;\n\t}\n\tint to;\n\tdouble dist;\n};\n\nstruct State{\n\tState(int arg_node_id,double arg_sum_dist){\n\t\tnode_id = arg_node_id;\n\t\tsum_dist = arg_sum_dist;\n\t}\n\tbool operator<(const struct State &arg) const{\n\n\t\treturn sum_dist > arg.sum_dist; //総距離の昇順(PQ)\n\t}\n\n\tint node_id;\n\tdouble sum_dist;\n};\n\nint N;\nint num_all;\nint num_P[105],P[105][105],poly_index[SIZE];\nint on_segment[SIZE];\ndouble NUM = 100000;\ndouble min_dist[SIZE];\nPoint A,B;\nPoint point[SIZE];\nPolygon POLY[105];\nvector<Edge> G[SIZE];\n\n\ndouble calc_dist(Point A,Point B){\n\n\treturn sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y));\n}\n\ndouble norm(Vector a){\n\treturn a.x*a.x+a.y*a.y;\n}\n\ndouble abs(Vector a){\n\treturn sqrt(norm(a));\n}\n\ndouble cross(Vector a,Vector b){\n return a.x*b.y-a.y*b.x;\n}\n\ndouble dot(Vector a,Vector b){\n return a.x*b.x + a.y*b.y;\n}\n\nstatic const int COUNTER_CLOCKWISE = 1;\nstatic const int CLOCKWISE = -1;\nstatic const int ONLINE_BACK = 2;\nstatic const int ONLINE_FRONT = -2;\nstatic const int ON_SEGMENT = 0;\n\nint ccw(Point p0,Point p1,Point p2){\n\n\tVector a = p1 - p0;\n\tVector b = p2 - p0;\n\n\tif(cross(a,b) > EPS)return COUNTER_CLOCKWISE;\n\tif(cross(a,b) < -EPS)return CLOCKWISE;\n\tif(dot(a,b) < -EPS)return ONLINE_BACK;\n\tif(a.norm() < b.norm())return ONLINE_FRONT;\n\n\treturn ON_SEGMENT;\n}\n\nint func(double x1,double y1,double x2, double y2, double xp, double yp){\n\tdouble naiseki,norm1,norm2,gaiseki;\n\tnorm1 = sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));\n\tnorm2 = sqrt((xp-x1)*(xp-x1)+(yp-y1)*(yp-y1));\n\tnaiseki = (xp-x1)*(x2-x1)+(yp-y1)*(y2-y1);\n\tgaiseki = (x2-x1)*(yp-y1)-(xp-x1)*(y2-y1);\n\tif(gaiseki > EPS){\n\t\treturn 1;\n\t}else if(gaiseki < -EPS){\n\t\treturn -1;\n\t}\n\tif(naiseki < -EPS){\n\t\treturn 2;\n\t}\n\n\tif(norm1 < norm2){\n\t\treturn -2;\n\t}\n\treturn 0;\n}\n\n\n//★★直線ではなく、線分の交差判定★★\nbool is_Cross(Line a,Line b){\n\n\tif(func(a.p[0].x,a.p[0].y,a.p[1].x,a.p[1].y,b.p[0].x,b.p[0].y)*\n\t\t\tfunc(a.p[0].x,a.p[0].y,a.p[1].x,a.p[1].y,b.p[1].x,b.p[1].y) <= 0 &&\n\t\t\tfunc(b.p[0].x,b.p[0].y,b.p[1].x,b.p[1].y,a.p[0].x,a.p[0].y)*\n\t\t\tfunc(b.p[0].x,b.p[0].y,b.p[1].x,b.p[1].y,a.p[1].x,a.p[1].y) <= 0){\n\t\treturn true;\n\t}\n\treturn false;\n}\n\nPoint calc_minus(Point a,Point b){\n Point ret;\n\n ret.x = a.x-b.x;\n ret.y = a.y-b.y;\n\n return ret;\n}\n\ndouble calc_len(Vector a){\n return sqrt(a.x*a.x+a.y*a.y);\n}\n\n//線分ではなく直線と点の距離\ndouble getDistanceLP(Line l,Point p){\n return fabs(cross(calc_minus(l.p[1],l.p[0]),calc_minus(p,l.p[0]))/calc_len(calc_minus(l.p[1],l.p[0])));\n}\n\n//点と線分の距離\ndouble getDistanceSP(Line l,Point p){\n\tif(dot(calc_minus(l.p[1],l.p[0]),calc_minus(p,l.p[0])) < 0.0)return calc_len(calc_minus(p,l.p[0]));\n\tif(dot(calc_minus(l.p[0],l.p[1]),calc_minus(p,l.p[1])) < 0.0)return calc_len(calc_minus(p,l.p[1]));\n\treturn getDistanceLP(l,p);\n}\n\n\n\n//交点を求める関数\nPoint calc_Cross_Point(double x1,double x2,double x3,double x4,double y1,double y2,double y3,double y4){\n\tPoint ret;\n\tret.x = ((x2-x1)*(y3*(x4-x3)+x3*(y3-y4))-(x4-x3)*(y1*(x2-x1)+x1*(y1-y2)))/((y2-y1)*(x4-x3)-(y4-y3)*(x2-x1));\n\tif(fabs(x1-x2) > EPS){\n\t\tret.y = ((y2-y1)*ret.x+y1*(x2-x1)+x1*(y1-y2))/(x2-x1);\n\t}else{\n\t\tret.y = ((y4-y3)*ret.x+y3*(x4-x3)+x3*(y3-y4))/(x4-x3);\n\t}\n\treturn ret;\n}\n\n//インタフェース関数\nPoint calc_Cross_Point(Point a,Point b,Point c,Point d){\n\treturn calc_Cross_Point(a.x,b.x,c.x,d.x,a.y,b.y,c.y,d.y);\n}\n\nPoint calc_Cross_Point(Line A,Line B){\n\n\tif(getDistanceSP(B,A.p[0]) < EPS){\n\n\t\treturn A.p[0];\n\n\t}else if(getDistanceSP(B,A.p[1]) < EPS){\n\n\t\treturn A.p[1];\n\n\t}else if(getDistanceSP(A,B.p[0]) < EPS){\n\n\t\treturn B.p[0];\n\n\t}else if(getDistanceSP(A,B.p[1]) < EPS){\n\n\t\treturn B.p[1];\n\t}\n\n\treturn calc_Cross_Point(A.p[0],A.p[1],B.p[0],B.p[1]);\n}\n\nPoint calc_Reflection_Point(double x1,double y1,double x2,double y2,double xp,double yp){\n\n\tPoint ret;\n\n\tbool X_FLG = false,Y_FLG = false;\n\tdouble slope;\n\n\tif(y1 == y2){\n\t\tX_FLG = true;\n\t}else if(x1 == x2){\n\t\tY_FLG = true;\n\t}else{\n\t\tslope = (y2-y1)/(x2-x1);\n\t}\n\n\tif(X_FLG){\n\t\tret.x = xp,ret.y=y1;\n\t}else if(Y_FLG){\n\t\tret.x = x1,ret.y = yp;\n\t}else{\n\t\tret.x = (yp*(x2-x1)*(y2-y1)+xp*(x2-x1)*(x2-x1)-y1*(y2-y1)*(x2-x1)+x1*(y2-y1)*(y2-y1))/((y2-y1)*(y2-y1)+(x2-x1)*(x2-x1));\n\t\tret.y = ((x1-x2)*ret.x+yp*(y2-y1)+xp*(x2-x1))/(y2-y1);\n\t}\n\tret.x = 2*ret.x-xp;\n\tret.y = 2*ret.y-yp;\n\n\treturn ret;\n}\n\nPoint calc_Reflection_Point(Line line,Point point){\n\n\treturn calc_Reflection_Point(line.p[0].x,line.p[0].y,line.p[1].x,line.p[1].y,point.x,point.y);\n}\n\n/*\n * IN 2\n * ON 1\n * OUT 0\n *\n */\nint contains(Polygon g,Point p){\n\tint n = g.size();\n\tbool x = false;\n\tfor(int i = 0; i < n; i++){\n\t\tPoint a = g[i]-p,b = g[(i+1)%n]-p;\n\t\tif(abs(cross(a,b)) < EPS && dot(a,b) < EPS)return 1;\n\t\tif(a.y > b.y)swap(a,b);\n\t\tif(a.y < EPS && EPS < b.y && cross(a,b) > EPS) x = !x;\n\t}\n\treturn (x ? 2:0);\n}\n\ndouble calc_slope(Line A){\n\n\tif(fabs(A.p[0].x-A.p[1].x) < EPS){\n\n\t\treturn DBL_MAX;\n\n\t}else if(fabs(A.p[0].y-A.p[1].y) < EPS){\n\n\t\treturn 0;\n\n\t}else{\n\n\t\treturn (A.p[1].y-A.p[0].y)/(A.p[1].x-A.p[0].x);\n\t}\n}\n\nbool cross_check(Point from,Point to){\n\n\tLine tmp_line = Line(from,to);\n\n\tfor(int i = 0; i < N; i++){\n\t\tfor(int k = 0; k < num_P[i]; k++){\n\t\t\tLine work_line = Line(point[P[i][k]],point[P[i][(k+1)%num_P[i]]]);\n\t\t\tif(is_Cross(tmp_line,work_line)){\n\n\t\t\t\tPoint cross_p = calc_Cross_Point(tmp_line,work_line);\n\t\t\t\tif(cross_p == from || cross_p == to){\n\n\t\t\t\t\t//両端点は除く\n\t\t\t\t}else{\n\n\t\t\t\t\treturn true;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\treturn false;\n}\n\n\nvoid func(){\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tPOLY[i].clear();\n\t}\n\n\tfor(int i = 0; i < SIZE; i++){\n\n\t\ton_segment[i] = -1;\n\t\tG[i].clear();\n\t}\n\n\tscanf(\"%lf %lf\",&B.x,&B.y);\n\n\tnum_all = 0;\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tscanf(\"%d\",&num_P[i]);\n\t\tfor(int k = 0; k < num_P[i]; k++){\n\n\t\t\tscanf(\"%lf %lf\",&point[num_all].x,&point[num_all].y);\n\t\t\tP[i][k] = num_all;\n\t\t\tPOLY[i].push_back(point[num_all]);\n\t\t\tpoly_index[num_all++] = i;\n\t\t}\n\t}\n\n\tif(cross_check(A,B)){\n\n\t\tprintf(\"0.0000000000\\n\");\n\t\treturn;\n\t}\n\n\tvector<int> ok_P,not_cross_P;\n\t//避難可能な頂点を列挙\n\tfor(int i = 0; i < num_all; i++){\n\n\t\tif(cross_check(B,point[i])){ //爆弾と自分を結ぶ線分がどこかのポリゴンと交差するならOK\n\n\t\t\tok_P.push_back(i);\n\n\t\t}else{ //爆弾と自分を結ぶ線分を少し伸ばした点が、自分の属するポリゴンの内部に入るか調べる\n\n\t\t\tnot_cross_P.push_back(i);\n\n\t\t\tLine tmp_line = Line(B,point[i]);\n\t\t\tdouble tmp_slope = calc_slope(tmp_line);\n\n\t\t\tbool FLG;\n\t\t\tPoint tmp_p;\n\n\t\t\tif(fabs(tmp_slope-DBL_MAX) < EPS){\n\n\t\t\t\tif(B.y > point[i].y){\n\n\t\t\t\t\ttmp_p = Point(point[i].x,point[i].y-EPS2);\n\n\t\t\t\t}else{\n\n\t\t\t\t\ttmp_p = Point(point[i].x,point[i].y+EPS2);\n\t\t\t\t}\n\n\t\t\t}else{\n\n\t\t\t\tif(B.x < point[i].x){\n\n\t\t\t\t\ttmp_p = Point(point[i].x+EPS2,point[i].y+EPS2*tmp_slope);\n\n\t\t\t\t}else{\n\n\t\t\t\t\ttmp_p = Point(point[i].x-EPS2,point[i].y-EPS2*tmp_slope);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif(contains(POLY[poly_index[i]],tmp_p) == 0){\n\n\t\t\t\tok_P.push_back(i);\n\t\t\t}\n\t\t}\n\t}\n\n\tvector<int> seg_P,proj_P;\n\n\tfor(int i = 0; i < not_cross_P.size(); i++){\n\n\t\tLine tmp_line = Line(B,point[not_cross_P[i]]);\n\t\tdouble tmp_slope = calc_slope(tmp_line);\n\n\t\t//線分を延長する\n\t\tif(fabs(tmp_slope-DBL_MAX) < EPS){\n\n\t\t\tif(B.y > point[not_cross_P[i]].y){\n\n\t\t\t\ttmp_line.p[1] = Point(point[not_cross_P[i]].x,-NUM);\n\n\t\t\t}else{\n\n\t\t\t\ttmp_line.p[1] = Point(point[not_cross_P[i]].x,NUM);\n\t\t\t}\n\n\t\t}else{\n\n\t\t\tif(B.x < point[i].x){\n\n\t\t\t\ttmp_line.p[1] = Point(point[not_cross_P[i]].x+NUM,point[not_cross_P[i]].y+NUM*tmp_slope);\n\n\t\t\t}else{\n\n\t\t\t\ttmp_line.p[1] = Point(point[not_cross_P[i]].x-NUM,point[not_cross_P[i]].y-NUM*tmp_slope);\n\t\t\t}\n\t\t}\n\n\t\ttmp_line.p[0] = point[not_cross_P[i]];\n\n\t\tbool tmp_FLG = false;\n\n\t\t//線分とポリゴンの交点\n\t\tfor(int k = 0; k < N; k++){\n\t\t\tif(tmp_FLG)break;\n\t\t\tif(k == poly_index[not_cross_P[i]])continue;\n\n\t\t\tfor(int a = 0; a < num_P[k]; a++){\n\t\t\t\tLine work_line = Line(point[P[k][a]],point[P[k][(a+1)%num_P[k]]]);\n\n\t\t\t\tif(!is_Cross(tmp_line,work_line))continue;\n\n\t\t\t\tPoint cross_p = calc_Cross_Point(tmp_line,work_line);\n\n\t\t\t\tif(!cross_check(point[not_cross_P[i]],cross_p)){ //tmp_lineにつき1つだけであるはず\n\n\t\t\t\t\tpoint[num_all] = cross_p;\n\t\t\t\t\tpoly_index[num_all] = k;\n\t\t\t\t\ton_segment[num_all] = a;\n\t\t\t\t\tok_P.push_back(num_all);\n\t\t\t\t\tseg_P.push_back(num_all);\n\t\t\t\t\tnum_all++;\n\t\t\t\t\ttmp_FLG = true;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t//Aからの垂線\n\t\tPoint ref_p = calc_Reflection_Point(tmp_line,A);\n\n\t\tLine a_line = Line(A,ref_p);\n\t\tif(!is_Cross(a_line,tmp_line))continue;\n\n\t\tPoint proj_p = calc_Cross_Point(tmp_line,a_line);\n\t\tif(calc_dist(B,proj_p)+EPS < calc_dist(B,point[not_cross_P[i]]))continue;\n\n\t\tif(!cross_check(A,proj_p)){\n\n\t\t\tpoint[num_all] = proj_p;\n\t\t\tpoly_index[num_all] = -1;\n\t\t\tok_P.push_back(num_all);\n\t\t\tproj_P.push_back(num_all);\n\t\t\tnum_all++;\n\t\t}\n\t}\n\n\t/*辺張り*/\n\n\t//Aから\n\tfor(int i = 0; i < num_all; i++){\n\n\t\tif(!cross_check(A,point[i])){\n\n\t\t\tG[num_all].push_back(Edge(i,calc_dist(A,point[i])));\n\t\t}\n\t}\n\n\t//ポリゴン同士\n\tfor(int i = 0; i < N; i++){\n\t\tfor(int k = 0; k < num_P[i]; k++){\n\t\t\tint from = P[i][k];\n\t\t\tint to = P[i][(k+1)%num_P[i]];\n\n\t\t\tdouble tmp_dist = calc_dist(point[from],point[to]);\n\t\t\tG[from].push_back(Edge(to,tmp_dist));\n\t\t\tG[to].push_back(Edge(from,tmp_dist));\n\t\t}\n\t}\n\t//辺上の点\n\tfor(int i = 0; i < seg_P.size(); i++){\n\n\t\t//両隣の点\n\t\tint tmp = seg_P[i];\n\t\tint a = P[poly_index[tmp]][on_segment[tmp]];\n\t\tint b = P[poly_index[tmp]][(on_segment[tmp]+1)%num_P[poly_index[tmp]]];\n\n\t\tG[a].push_back(Edge(tmp,calc_dist(point[a],point[tmp])));\n\t\tG[b].push_back(Edge(tmp,calc_dist(point[b],point[tmp])));\n\n\t\t//他のポリゴンからの点\n\t\tfor(int k = 0; k < N; k++){\n\t\t\tif(k == poly_index[tmp])continue;\n\n\t\t\tfor(int a = 0; a < num_P[k]; a++){\n\n\t\t\t\tif(!cross_check(point[P[k][a]],point[tmp])){\n\n\t\t\t\t\tG[P[k][a]].push_back(Edge(tmp,calc_dist(point[P[k][a]],point[tmp])));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\t//外にある点\n\tfor(int i = 0; i < proj_P.size(); i++){\n\n\t\tint tmp = proj_P[i];\n\n\t\t//ポリゴンからの点\n\t\tfor(int k = 0; k < N; k++){\n\n\t\t\tfor(int a = 0; a < num_P[k]; a++){\n\n\t\t\t\tif(!cross_check(point[P[k][a]],point[tmp])){\n\n\t\t\t\t\tG[P[k][a]].push_back(Edge(tmp,calc_dist(point[P[k][a]],point[tmp])));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tfor(int i = 0; i <= num_all; i++){\n\n\t\tmin_dist[i] = BIG_NUM;\n\t}\n\n\tpriority_queue<State> Q;\n\tmin_dist[num_all] = 0;\n\n\tQ.push(State(num_all,0));\n\n\tint next_node;\n\tdouble next_dist;\n\n\twhile(!Q.empty()){\n\n\t\tif(Q.top().sum_dist > min_dist[Q.top().node_id]+EPS){\n\n\t\t\tQ.pop();\n\n\t\t}else{\n\n\t\t\tfor(int i = 0; i < G[Q.top().node_id].size(); i++){\n\n\t\t\t\tnext_node = G[Q.top().node_id][i].to;\n\t\t\t\tnext_dist = Q.top().sum_dist+G[Q.top().node_id][i].dist;\n\n\t\t\t\tif(min_dist[next_node] > next_dist+EPS){\n\t\t\t\t\tmin_dist[next_node] = next_dist;\n\t\t\t\t\tQ.push(State(next_node,next_dist));\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tQ.pop();\n\t\t}\n\t}\n\n\tdouble ans = BIG_NUM;\n\n\tfor(int i = 0; i < ok_P.size(); i++){\n\n\t\tans = min(ans,min_dist[ok_P[i]]);\n\t}\n\n\tprintf(\"%.10lf\\n\",ans);\n}\n\nint main(){\n\n\tA = Point(0,0);\n\n\twhile(true){\n\n\t\tscanf(\"%d\",&N);\n\t\tif(N == 0)break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 450, "memory_kb": 4152, "score_of_the_acc": -0.4636, "final_rank": 6 }, { "submission_id": "aoj_2246_2299158", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i,a,n) for(int i=(a);i<(n);i++)\n#define per(i,a,n) for(int i=(n)-1;i>=(a);i--)\n#define mp make_pair\n#define pb push_back\n#define fi first\n#define se second\n \ntypedef double db;\n \nconst db EPS = 1e-9;\n \ninline int sign(db a) {\n return a < -EPS ? -1 : a > EPS;\n}\n \ninline int cmp(db a, db b){\n return sign(a-b);\n}\n \nstruct P {\n db x, y;\n P() {}\n P(db _x, db _y) : x(_x), y(_y) {}\n P operator+(P p) { return {x + p.x, y + p.y}; }\n P operator-(P p) { return {x - p.x, y - p.y}; }\n P operator*(db d) { return {x * d, y * d}; }\n P operator/(db d) { return {x / d, y / d}; }\n\n bool operator<(P p) const { \n int c = cmp(x, p.x);\n if (c) return c == -1;\n return cmp(y, p.y) == -1;\n }\n\n bool operator==(P o) const{\n return cmp(x,o.x) == 0 && cmp(y,o.y) == 0;\n }\n\n db dot(P p) { return x * p.x + y * p.y; }\n db det(P p) { return x * p.y - y * p.x; }\n \n db distTo(P p) { return (*this-p).abs(); }\n db alpha() { return atan2(y, x); }\n void read() { cin>>x>>y; }\n void write() {cout<<\"(\"<<x<<\",\"<<y<<\")\"<<endl;}\n db abs() { return sqrt(abs2());}\n db abs2() { return x * x + y * y; }\n P rot90() { return P(-y,x);}\n P unit() { return *this/abs(); }\n int quad() const { return sign(y) == 1 || (sign(y) == 0 && sign(x) >= 0); }\n P rot(db an){ return {x*cos(an)-y*sin(an),x*sin(an) + y*cos(an)}; }\n};\n \nstruct L{ //ps[0] -> ps[1]\n P ps[2];\n P& operator[](int i) { return ps[i]; }\n P dir() { return ps[1] - ps[0]; }\n bool include(P p) { return sign((ps[1] - ps[0]).det(p - ps[0])) > 0; }\n L push(){ // push eps outward\n const double eps = 1e-6;\n P delta = (ps[1] - ps[0]).rot90().unit() * eps;\n return {ps[0] - delta, ps[1] - delta};\n }\n};\n \n#define cross(p1,p2,p3) ((p2.x-p1.x)*(p3.y-p1.y)-(p3.x-p1.x)*(p2.y-p1.y))\n#define crossOp(p1,p2,p3) sign(cross(p1,p2,p3))\n \nbool chkLL(P p1, P p2, P q1, P q2) {\n db a1 = cross(q1, q2, p1), a2 = -cross(q1, q2, p2);\n return sign(a1+a2) != 0;\n}\n\nP isLL(P p1, P p2, P q1, P q2) {\n db a1 = cross(q1, q2, p1), a2 = -cross(q1, q2, p2);\n return (p1 * a2 + p2 * a1) / (a1 + a2);\n}\n \nP isLL(L l1,L l2){ return isLL(l1[0],l1[1],l2[0],l2[1]); }\n \nbool intersect(db l1,db r1,db l2,db r2){\n if(l1>r1) swap(l1,r1); if(l2>r2) swap(l2,r2); \n return !( cmp(r1,l2) == -1 || cmp(r2,l1) == -1 );\n}\n \nbool isSS(P p1, P p2, P q1, P q2){\n return intersect(p1.x,p2.x,q1.x,q2.x) && intersect(p1.y,p2.y,q1.y,q2.y) && \n crossOp(p1,p2,q1) * crossOp(p1,p2,q2) <= 0 && crossOp(q1,q2,p1)\n * crossOp(q1,q2,p2) <= 0;\n}\n \nbool isSS_strict(P p1, P p2, P q1, P q2){\n return crossOp(p1,p2,q1) * crossOp(p1,p2,q2) < 0 && crossOp(q1,q2,p1)\n * crossOp(q1,q2,p2) < 0;\n}\n \nbool isMiddle(db a, db m, db b) {\n return sign(a - m) == 0 || sign(b - m) == 0 || (a < m != b < m);\n}\n \nbool isMiddle(P a, P m, P b) {\n return isMiddle(a.x, m.x, b.x) && isMiddle(a.y, m.y, b.y);\n}\n \nbool onSeg(P p1, P p2, P q){\n return crossOp(p1,p2,q) == 0 && isMiddle(p1, q, p2);\n}\n\nbool onSeg_strict(P p1, P p2, P q){\n return crossOp(p1,p2,q) == 0 && sign((q-p1).dot(p1-p2)) * sign((q-p2).dot(p1-p2)) < 0;\n}\n \nP proj(P p1, P p2, P q) {\n P dir = p2 - p1;\n return p1 + dir * (dir.dot(q - p1) / dir.abs2());\n}\n \nP reflect(P p1, P p2, P q){\n return proj(p1,p2,q) * 2 - q;\n}\n \ndb nearest(P p1,P p2,P q){\n P h = proj(p1,p2,q);\n if(isMiddle(p1,h,p2))\n return q.distTo(h);\n return min(p1.distTo(q),p2.distTo(q));\n}\n \ndb disSS(P p1, P p2, P q1, P q2){\n if(isSS(p1,p2,q1,q2)) return 0;\n return min(min(nearest(p1,p2,q1),nearest(p1,p2,q2)), min(nearest(q1,q2,p1),nearest(q1,q2,p2)) );\n}\n \ndb rad(P p1,P p2){\n return atan2l(p1.det(p2),p1.dot(p2));\n}\n \ndb incircle(P p1, P p2, P p3){\n db A = p1.distTo(p2);\n db B = p2.distTo(p3);\n db C = p3.distTo(p1);\n return sqrtl(A*B*C/(A+B+C));\n}\n \n//polygon\n \ndb area(vector<P> ps){\n db ret = 0; rep(i,0,ps.size()) ret += ps[i].det(ps[(i+1)%ps.size()]); \n return ret/2;\n}\n \nint contain(vector<P> ps, P p){ //2:inside,1:on_seg,0:outside\n int n = ps.size(), ret = 0; \n rep(i,0,n){\n P u=ps[i],v=ps[(i+1)%n];\n if(onSeg(u,v,p)) return 1;\n if(cmp(u.y,v.y)<=0) swap(u,v);\n if(cmp(p.y,u.y) >0 || cmp(p.y,v.y) <= 0) continue;\n ret ^= crossOp(p,u,v) > 0;\n }\n return ret*2;\n}\n \nvector<P> convexHull(vector<P> ps) {\n int n = ps.size(); if(n <= 1) return ps;\n sort(ps.begin(), ps.end());\n vector<P> qs(n * 2); int k = 0;\n for (int i = 0; i < n; qs[k++] = ps[i++]) \n while (k > 1 && crossOp(qs[k - 2], qs[k - 1], ps[i]) <= 0) --k;\n for (int i = n - 2, t = k; i >= 0; qs[k++] = ps[i--])\n while (k > t && crossOp(qs[k - 2], qs[k - 1], ps[i]) <= 0) --k;\n qs.resize(k - 1);\n return qs;\n}\n \nvector<P> convexHullNonStrict(vector<P> ps) {\n //caution: need to unique the Ps first\n int n = ps.size(); if(n <= 1) return ps;\n sort(ps.begin(), ps.end());\n vector<P> qs(n * 2); int k = 0;\n for (int i = 0; i < n; qs[k++] = ps[i++]) \n while (k > 1 && crossOp(qs[k - 2], qs[k - 1], ps[i]) < 0) --k;\n for (int i = n - 2, t = k; i >= 0; qs[k++] = ps[i--])\n while (k > t && crossOp(qs[k - 2], qs[k - 1], ps[i]) < 0) --k;\n qs.resize(k - 1);\n return qs;\n}\n \ndb convexDiameter(vector<P> ps){\n int n = ps.size(); if(n <= 1) return 0;\n int is = 0, js = 0; rep(k,1,n) is = ps[k]<ps[is]?k:is, js = ps[js] < ps[k]?k:js;\n int i = is, j = js;\n db ret = ps[i].distTo(ps[j]);\n do{\n if((ps[(i+1)%n]-ps[i]).det(ps[(j+1)%n]-ps[j]) >= 0)\n (++j)%=n;\n else\n (++i)%=n;\n ret = max(ret,ps[i].distTo(ps[j]));\n }while(i!=is || j!=js);\n return ret;\n}\n \nvector<P> convexCut(const vector<P>&ps, P q1, P q2) {\n vector<P> qs;\n int n = ps.size();\n rep(i,0,n){\n P p1 = ps[i], p2 = ps[(i+1)%n];\n int d1 = crossOp(q1,q2,p1), d2 = crossOp(q1,q2,p2);\n if(d1 >= 0) qs.pb(p1);\n if(d1 * d2 < 0) qs.pb(isLL(p1,p2,q1,q2));\n }\n return qs;\n}\n \n//min_dist\n \ndb min_dist(vector<P>&ps,int l,int r){\n if(r-l<=5){\n db ret = 1e100;\n rep(i,l,r) rep(j,l,i) ret = min(ret,ps[i].distTo(ps[j]));\n return ret;\n }\n int m = (l+r)>>1;\n db ret = min(min_dist(ps,l,m),min_dist(ps,m,r));\n vector<P> qs; rep(i,l,r) if(abs(ps[i].x-ps[m].x)<= ret) qs.pb(ps[i]);\n sort(qs.begin(), qs.end(),[](P a,P b) -> bool {return a.y<b.y; });\n rep(i,1,qs.size()) for(int j=i-1;j>=0&&qs[j].y>=qs[i].y-ret;--j) ret = min(ret,qs[i].distTo(qs[j]));\n return ret;\n}\n \nint type(P o1,db r1,P o2,db r2){\n db d = o1.distTo(o2);\n if(cmp(d,r1+r2) == 1) return 4;\n if(cmp(d,r1+r2) == 0) return 3;\n if(cmp(d,abs(r1-r2)) == 1) return 2;\n if(cmp(d,abs(r1-r2)) == 0) return 1;\n return 0;\n}\n \nvector<P> isCL(P o,db r,P p1,P p2){\n db x = (p1-o).dot(p2-p1), y = (p2-p1).abs2(), d = x * x - y * ((p1-o).abs2() - r*r);\n if(sign(d) < 0) return {};\n d = max(d,0.0); P m = p1 - (p2-p1)*(x/y), dr = (p2-p1)*(sqrt(d)/y);\n return {m-dr,m+dr}; //along dir: p1->p2\n}\n \nvector<P> isCC(P o1, db r1, P o2, db r2) { //need to check whether two circles are the same\n db d = o1.distTo(o2); \n if (cmp(d, r1 + r2) == 1) return {};\n d = min(d, r1 + r2);\n db y = (r1 * r1 + d * d - r2 * r2) / (2 * d), x = sqrt(r1 * r1 - y * y);\n P dr = (o2 - o1).unit();\n P q1 = o1 + dr * y, q2 = dr.rot90() * x;\n return {q1-q2,q1+q2};//along circle 1\n}\n \nvector<P> tanCP(P o, db r, P p) {\n db x = (p - o).abs2(), d = x - r * r;\n if (sign(d) <= 0) return {}; // on circle => no tangent\n P q1 = o + (p - o) * (r * r / x);\n P q2 = (p - o).rot90() * (r * sqrt(d) / x);\n return {q1-q2,q1+q2}; //counter clock-wise\n}\n \n \nvector<L> extanCC(P o1, db r1, P o2, db r2) {\n vector<L> ret;\n if (cmp(r1, r2) == 0) {\n P dr = (o2 - o1).unit().rot90() * r1;\n ret.pb({o1 + dr, o2 + dr}), ret.pb({o1 - dr, o2 - dr});\n } else {\n P p = (o2 * r1 - o1 * r2) / (r1 - r2);\n vector<P> ps = tanCP(o1, r1, p), qs = tanCP(o2, r2, p);\n rep(i,0,min(ps.size(),qs.size())) ret.pb({ps[i], qs[i]}); //c1 counter-clock wise\n }\n return ret;\n}\n \nvector<L> intanCC(P o1, db r1, P o2, db r2) {\n vector<L> ret;\n P p = (o1 * r2 + o2 * r1) / (r1 + r2);\n vector<P> ps = tanCP(o1,r1,p), qs = tanCP(o2,r2,p);\n rep(i,0,min(ps.size(),qs.size())) ret.pb({ps[i], qs[i]}); //c1 counter-clock wise\n return ret;\n}\n \ndb areaCT(db r, P p1, P p2){\n vector<P> is = isCL(P(0,0),r,p1,p2);\n if(is.empty()) return r*r*rad(p1,p2)/2;\n bool b1 = cmp(p1.abs2(),r*r) == 1, b2 = cmp(p2.abs2(), r*r) == 1;\n if(b1 && b2){\n if(sign((p1-is[0]).dot(p2-is[0])) <= 0 &&\n sign((p1-is[0]).dot(p2-is[0])) <= 0)\n return r*r*(rad(p1,is[0]) + rad(is[1],p2))/2 + is[0].det(is[1])/2;\n else return r*r*rad(p1,p2)/2;\n }\n if(b1) return (r*r*rad(p1,is[0]) + is[0].det(p2))/2;\n if(b2) return (p1.det(is[1]) + r*r*rad(is[1],p2))/2;\n return p1.det(p2)/2;\n}\n \nbool parallel(L l0, L l1) { return sign( l0.dir().det( l1.dir() ) ) == 0; }\n \nbool sameDir(L l0, L l1) { return parallel(l0, l1) && sign(l0.dir().dot(l1.dir()) ) == 1; }\n \nbool cmp (P a, P b) {\n if (a.quad() != b.quad()) {\n return a.quad() < b.quad();\n } else {\n return sign( a.det(b) ) > 0;\n }\n}\n \nbool operator < (L l0, L l1) {\n if (sameDir(l0, l1)) {\n return l1.include(l0[0]);\n } else {\n return cmp( l0.dir(), l1.dir() );\n }\n}\n \nbool check(L u, L v, L w) { \n return w.include(isLL(u,v)); \n}\n \nvector<P> halfPlaneIS(vector<L> &l) {\n sort(l.begin(), l.end());\n deque<L> q;\n for (int i = 0; i < (int)l.size(); ++i) {\n if (i && sameDir(l[i], l[i - 1])) continue;\n while (q.size() > 1 && !check(q[q.size() - 2], q[q.size() - 1], l[i])) q.pop_back();\n while (q.size() > 1 && !check(q[1], q[0], l[i])) q.pop_front();\n q.push_back(l[i]);\n }\n while (q.size() > 2 && !check(q[q.size() - 2], q[q.size() - 1], q[0])) q.pop_back();\n while (q.size() > 2 && !check(q[1], q[0], q[q.size() - 1])) q.pop_front();\n vector<P> ret;\n for (int i = 0; i < (int)q.size(); ++i) ret.push_back(isLL(q[i], q[(i + 1) % q.size()]));\n return ret;\n}\n\nP inCenter(P A, P B, P C) { // ??????\n double a = (B - C).abs(), b = (C - A).abs(), c = (A - B).abs();\n return (A * a + B * b + C * c) / (a + b + c);\n}\n\nP circumCenter(P a, P b, P c) { // ??????\n P bb = b - a, cc = c - a;\n double db = bb.abs2(), dc = cc.abs2(), d = 2 * bb.det(cc);\n return a - P(bb.y * dc - cc.y * db, cc.x * db - bb.x * dc) / d;\n}\n\nP othroCenter(P a, P b, P c) { // ??????\n P ba = b - a, ca = c - a, bc = b - c;\n double Y = ba.y * ca.y * bc.y,\n A = ca.x * ba.y - ba.x * ca.y,\n x0 = (Y + ca.x * ba.y * b.x - ba.x * ca.y * c.x) / A,\n y0 = -ba.x * (x0 - c.x) / ba.y + ca.y;\n return {x0, y0};\n}\n\nconst int MAX_N = 500 + 10;\n\nvector<P> ps[MAX_N];\nvector<int> ids[MAX_N];\n\nint n;\nP bomb;\nP O = {0,0};\n\nvector<P> pList;\n\nbool edge[MAX_N][MAX_N];\ndb len[MAX_N][MAX_N];\n\nbool good[MAX_N];\n\nint tot;\n\nint total;\n\nbool check(P L, P R, P p){\n //is p strictly belong to L -> R?\n if(L.det(R) > -EPS){ // <= 180 degree\n return L.det(p) > EPS && p.det(R) > EPS;\n }\n //L.det(R) < -EPS\n if(L.det(p) >= 0) return 1;\n if(p.det(R) > EPS) return 1;\n return 0;\n}\n\nbool strict_polys_segment(vector<P>&qs,P p1,P p2){\n int n = qs.size();\n\n P m = (p1+p2)/2;\n\n if(contain(qs,m) >= 2) return 1;\n\n rep(i,0,n){\n P q1 = qs[i], q2 = qs[(i+1)%n];\n if(isSS_strict(p1,p2,q1,q2)) return 1;\n if(onSeg_strict(p1,p2,q1)){\n P q0 = qs[(i+n-1)%n];\n if(check(q2-q1,q0-q1,p1-q1)) return 1;\n if(check(q2-q1,q0-q1,p2-q1)) return 1;\n }\n }\n\n return 0;\n}\n\nbool check_quick(P p1,P p2){\n rep(i,0,n)\n if(strict_polys_segment(ps[i],p1,p2)) return 0;\n return 1;\n}\n\nbool check_bf(P p1, P p2){\n\n bool touch = 0;\n rep(i,0,n){\n int m = ps[i].size();\n rep(j,0,m){\n P q1 = ps[i][j], q2 = ps[i][(j+1)%m];\n if(isSS_strict(p1,p2,q1,q2)) return 0;\n if(isSS(p1,p2,q1,q2)) touch = 1;\n }\n }\n\n if(!touch) return 1;\n\n //bf n^2\n if(p2 < p1) swap(p1,p2);\n\n\n rep(i,0,n){\n int m = ps[i].size();\n\n vector<P> is = {p1,p2};\n\n rep(j,0,m){ \n P q1 = ps[i][j], q2 = ps[i][(j+1)%m];\n if(chkLL(p1,p2,q1,q2)){\n P u = isLL(p1,p2,q1,q2);\n if(isMiddle(p1,u,p2))\n is.pb(u);\n }\n }\n\n sort(is.begin(), is.end());\n\n rep(j,0,is.size() - 1){\n if(is[j].distTo(is[j+1]) < EPS) continue;\n P m = (is[j] + is[j+1]) / 2;\n ++total;\n if(contain(ps[i],m) == 2) return 0;\n }\n }\n\n return 1;\n}\n\nbool check(P p1,P p2){\n return check_quick(p1,p2);\n}\n\nvector<P> remove_colinear_point(vector<P> ps){\n deque<P> que;\n for(auto p : ps){\n while(que.size() >= 2 && crossOp(que[que.size()-2],que.back(),p) == 0)\n que.pop_back();\n que.push_back(p);\n }\n\n while(que.size() >= 3 && crossOp(que[que.size()-2],que.back(),que[0]) == 0)\n que.pop_back();\n\n while(que.size() >= 3 && crossOp(que.back(),que[0],que[1]) == 0)\n que.pop_front();\n\n ps = {};\n while(!que.empty()) ps.push_back(que.front()), que.pop_front();\n return ps;\n}\n\nint main(){\n while(cin>>n){\n if(!n) break;\n bomb.read();\n\n pList.clear();\n\n rep(i,0,n){\n ps[i].clear();\n ids[i].clear();\n\n int m;cin>>m;\n\n rep(j,0,m){\n P u; u.read(); \n ps[i].pb(u); \n }\n\n //ps[i] = remove_colinear_point(ps[i]);\n\n for(auto p : ps[i]){\n ids[i].pb(pList.size());\n pList.pb(p);\n }\n }\n\n pList.pb(O);pList.pb(bomb); tot = pList.size();\n\n if(!check_bf(O,bomb)){\n printf(\"%0.10f\\n\",0.0);\n continue;\n }\n\n rep(i,0,tot) rep(j,0,i){\n edge[i][j] = edge[j][i] = check(pList[i],pList[j]);\n len[i][j] = len[j][i] = pList[i].distTo(pList[j]);\n }\n\n memset(good,0,sizeof good);\n\n //get good edge\n\n rep(i,0,n){\n rep(j,0,ids[i].size()){\n int x = ids[i][j], y = ids[i][(j+1)%ids[i].size()];\n P u = (pList[x] + pList[y])/2;\n if(!check(u,bomb))\n good[x] = good[y] = 1;\n }\n }\n\n db dist[MAX_N];\n bool used[MAX_N] = {};\n\n rep(i,0,tot) dist[i] = 1e100;\n\n dist[tot-2] = 0;\n\n db ans = 1e100;\n\n rep(_,0,tot){\n int u = -1; rep(i,0,tot) if(!used[i] && (u == -1 || dist[i] < dist[u]) ) u = i;\n used[u] = 1;\n rep(j,0,tot) if(edge[u][j] && dist[u] + len[u][j] < dist[j])\n dist[j] = dist[u] + len[u][j];\n }\n\n rep(i,0,tot) if(good[i]) ans = min(ans,dist[i]); db first = ans;\n \n rep(i,0,tot) if(dist[i] < ans){\n rep(j,0,tot-2){\n P u = proj(bomb,pList[j],pList[i]);\n db ret = dist[i] + pList[i].distTo(u);\n if(ret < ans){\n if(u.dot(pList[j] - bomb) > pList[j].dot(pList[j] - bomb) && check(pList[i],u))\n ans = ret;\n }\n }\n } \n \n\n printf(\"%0.10f\\n\",ans);//cout<<\"TOTAL:\"<<total<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 3020, "memory_kb": 5724, "score_of_the_acc": -1.0538, "final_rank": 11 }, { "submission_id": "aoj_2246_2299154", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i,a,n) for(int i=(a);i<(n);i++)\n#define per(i,a,n) for(int i=(n)-1;i>=(a);i--)\n#define mp make_pair\n#define pb push_back\n#define fi first\n#define se second\n \ntypedef double db;\n \nconst db EPS = 1e-9;\n \ninline int sign(db a) {\n return a < -EPS ? -1 : a > EPS;\n}\n \ninline int cmp(db a, db b){\n return sign(a-b);\n}\n \nstruct P {\n db x, y;\n P() {}\n P(db _x, db _y) : x(_x), y(_y) {}\n P operator+(P p) { return {x + p.x, y + p.y}; }\n P operator-(P p) { return {x - p.x, y - p.y}; }\n P operator*(db d) { return {x * d, y * d}; }\n P operator/(db d) { return {x / d, y / d}; }\n\n bool operator<(P p) const { \n int c = cmp(x, p.x);\n if (c) return c == -1;\n return cmp(y, p.y) == -1;\n }\n\n bool operator==(P o) const{\n return cmp(x,o.x) == 0 && cmp(y,o.y) == 0;\n }\n\n db dot(P p) { return x * p.x + y * p.y; }\n db det(P p) { return x * p.y - y * p.x; }\n \n db distTo(P p) { return (*this-p).abs(); }\n db alpha() { return atan2(y, x); }\n void read() { cin>>x>>y; }\n void write() {cout<<\"(\"<<x<<\",\"<<y<<\")\"<<endl;}\n db abs() { return sqrt(abs2());}\n db abs2() { return x * x + y * y; }\n P rot90() { return P(-y,x);}\n P unit() { return *this/abs(); }\n int quad() const { return sign(y) == 1 || (sign(y) == 0 && sign(x) >= 0); }\n P rot(db an){ return {x*cos(an)-y*sin(an),x*sin(an) + y*cos(an)}; }\n};\n \nstruct L{ //ps[0] -> ps[1]\n P ps[2];\n P& operator[](int i) { return ps[i]; }\n P dir() { return ps[1] - ps[0]; }\n bool include(P p) { return sign((ps[1] - ps[0]).det(p - ps[0])) > 0; }\n L push(){ // push eps outward\n const double eps = 1e-6;\n P delta = (ps[1] - ps[0]).rot90().unit() * eps;\n return {ps[0] - delta, ps[1] - delta};\n }\n};\n \n#define cross(p1,p2,p3) ((p2.x-p1.x)*(p3.y-p1.y)-(p3.x-p1.x)*(p2.y-p1.y))\n#define crossOp(p1,p2,p3) sign(cross(p1,p2,p3))\n \nbool chkLL(P p1, P p2, P q1, P q2) {\n db a1 = cross(q1, q2, p1), a2 = -cross(q1, q2, p2);\n return sign(a1+a2) != 0;\n}\n\nP isLL(P p1, P p2, P q1, P q2) {\n db a1 = cross(q1, q2, p1), a2 = -cross(q1, q2, p2);\n return (p1 * a2 + p2 * a1) / (a1 + a2);\n}\n \nP isLL(L l1,L l2){ return isLL(l1[0],l1[1],l2[0],l2[1]); }\n \nbool intersect(db l1,db r1,db l2,db r2){\n if(l1>r1) swap(l1,r1); if(l2>r2) swap(l2,r2); \n return !( cmp(r1,l2) == -1 || cmp(r2,l1) == -1 );\n}\n \nbool isSS(P p1, P p2, P q1, P q2){\n return intersect(p1.x,p2.x,q1.x,q2.x) && intersect(p1.y,p2.y,q1.y,q2.y) && \n crossOp(p1,p2,q1) * crossOp(p1,p2,q2) <= 0 && crossOp(q1,q2,p1)\n * crossOp(q1,q2,p2) <= 0;\n}\n \nbool isSS_strict(P p1, P p2, P q1, P q2){\n return crossOp(p1,p2,q1) * crossOp(p1,p2,q2) < 0 && crossOp(q1,q2,p1)\n * crossOp(q1,q2,p2) < 0;\n}\n \nbool isMiddle(db a, db m, db b) {\n return sign(a - m) == 0 || sign(b - m) == 0 || (a < m != b < m);\n}\n \nbool isMiddle(P a, P m, P b) {\n return isMiddle(a.x, m.x, b.x) && isMiddle(a.y, m.y, b.y);\n}\n \nbool onSeg(P p1, P p2, P q){\n return crossOp(p1,p2,q) == 0 && isMiddle(p1, q, p2);\n}\n\nbool onSeg_strict(P p1, P p2, P q){\n return crossOp(p1,p2,q) == 0 && sign((q-p1).dot(p1-p2)) * sign((q-p2).dot(p1-p2)) < 0;\n}\n \nP proj(P p1, P p2, P q) {\n P dir = p2 - p1;\n return p1 + dir * (dir.dot(q - p1) / dir.abs2());\n}\n \nP reflect(P p1, P p2, P q){\n return proj(p1,p2,q) * 2 - q;\n}\n \ndb nearest(P p1,P p2,P q){\n P h = proj(p1,p2,q);\n if(isMiddle(p1,h,p2))\n return q.distTo(h);\n return min(p1.distTo(q),p2.distTo(q));\n}\n \ndb disSS(P p1, P p2, P q1, P q2){\n if(isSS(p1,p2,q1,q2)) return 0;\n return min(min(nearest(p1,p2,q1),nearest(p1,p2,q2)), min(nearest(q1,q2,p1),nearest(q1,q2,p2)) );\n}\n \ndb rad(P p1,P p2){\n return atan2l(p1.det(p2),p1.dot(p2));\n}\n \ndb incircle(P p1, P p2, P p3){\n db A = p1.distTo(p2);\n db B = p2.distTo(p3);\n db C = p3.distTo(p1);\n return sqrtl(A*B*C/(A+B+C));\n}\n \n//polygon\n \ndb area(vector<P> ps){\n db ret = 0; rep(i,0,ps.size()) ret += ps[i].det(ps[(i+1)%ps.size()]); \n return ret/2;\n}\n \nint contain(vector<P> ps, P p){ //2:inside,1:on_seg,0:outside\n int n = ps.size(), ret = 0; \n rep(i,0,n){\n P u=ps[i],v=ps[(i+1)%n];\n if(onSeg(u,v,p)) return 1;\n if(cmp(u.y,v.y)<=0) swap(u,v);\n if(cmp(p.y,u.y) >0 || cmp(p.y,v.y) <= 0) continue;\n ret ^= crossOp(p,u,v) > 0;\n }\n return ret*2;\n}\n \nvector<P> convexHull(vector<P> ps) {\n int n = ps.size(); if(n <= 1) return ps;\n sort(ps.begin(), ps.end());\n vector<P> qs(n * 2); int k = 0;\n for (int i = 0; i < n; qs[k++] = ps[i++]) \n while (k > 1 && crossOp(qs[k - 2], qs[k - 1], ps[i]) <= 0) --k;\n for (int i = n - 2, t = k; i >= 0; qs[k++] = ps[i--])\n while (k > t && crossOp(qs[k - 2], qs[k - 1], ps[i]) <= 0) --k;\n qs.resize(k - 1);\n return qs;\n}\n \nvector<P> convexHullNonStrict(vector<P> ps) {\n //caution: need to unique the Ps first\n int n = ps.size(); if(n <= 1) return ps;\n sort(ps.begin(), ps.end());\n vector<P> qs(n * 2); int k = 0;\n for (int i = 0; i < n; qs[k++] = ps[i++]) \n while (k > 1 && crossOp(qs[k - 2], qs[k - 1], ps[i]) < 0) --k;\n for (int i = n - 2, t = k; i >= 0; qs[k++] = ps[i--])\n while (k > t && crossOp(qs[k - 2], qs[k - 1], ps[i]) < 0) --k;\n qs.resize(k - 1);\n return qs;\n}\n \ndb convexDiameter(vector<P> ps){\n int n = ps.size(); if(n <= 1) return 0;\n int is = 0, js = 0; rep(k,1,n) is = ps[k]<ps[is]?k:is, js = ps[js] < ps[k]?k:js;\n int i = is, j = js;\n db ret = ps[i].distTo(ps[j]);\n do{\n if((ps[(i+1)%n]-ps[i]).det(ps[(j+1)%n]-ps[j]) >= 0)\n (++j)%=n;\n else\n (++i)%=n;\n ret = max(ret,ps[i].distTo(ps[j]));\n }while(i!=is || j!=js);\n return ret;\n}\n \nvector<P> convexCut(const vector<P>&ps, P q1, P q2) {\n vector<P> qs;\n int n = ps.size();\n rep(i,0,n){\n P p1 = ps[i], p2 = ps[(i+1)%n];\n int d1 = crossOp(q1,q2,p1), d2 = crossOp(q1,q2,p2);\n if(d1 >= 0) qs.pb(p1);\n if(d1 * d2 < 0) qs.pb(isLL(p1,p2,q1,q2));\n }\n return qs;\n}\n \n//min_dist\n \ndb min_dist(vector<P>&ps,int l,int r){\n if(r-l<=5){\n db ret = 1e100;\n rep(i,l,r) rep(j,l,i) ret = min(ret,ps[i].distTo(ps[j]));\n return ret;\n }\n int m = (l+r)>>1;\n db ret = min(min_dist(ps,l,m),min_dist(ps,m,r));\n vector<P> qs; rep(i,l,r) if(abs(ps[i].x-ps[m].x)<= ret) qs.pb(ps[i]);\n sort(qs.begin(), qs.end(),[](P a,P b) -> bool {return a.y<b.y; });\n rep(i,1,qs.size()) for(int j=i-1;j>=0&&qs[j].y>=qs[i].y-ret;--j) ret = min(ret,qs[i].distTo(qs[j]));\n return ret;\n}\n \nint type(P o1,db r1,P o2,db r2){\n db d = o1.distTo(o2);\n if(cmp(d,r1+r2) == 1) return 4;\n if(cmp(d,r1+r2) == 0) return 3;\n if(cmp(d,abs(r1-r2)) == 1) return 2;\n if(cmp(d,abs(r1-r2)) == 0) return 1;\n return 0;\n}\n \nvector<P> isCL(P o,db r,P p1,P p2){\n db x = (p1-o).dot(p2-p1), y = (p2-p1).abs2(), d = x * x - y * ((p1-o).abs2() - r*r);\n if(sign(d) < 0) return {};\n d = max(d,0.0); P m = p1 - (p2-p1)*(x/y), dr = (p2-p1)*(sqrt(d)/y);\n return {m-dr,m+dr}; //along dir: p1->p2\n}\n \nvector<P> isCC(P o1, db r1, P o2, db r2) { //need to check whether two circles are the same\n db d = o1.distTo(o2); \n if (cmp(d, r1 + r2) == 1) return {};\n d = min(d, r1 + r2);\n db y = (r1 * r1 + d * d - r2 * r2) / (2 * d), x = sqrt(r1 * r1 - y * y);\n P dr = (o2 - o1).unit();\n P q1 = o1 + dr * y, q2 = dr.rot90() * x;\n return {q1-q2,q1+q2};//along circle 1\n}\n \nvector<P> tanCP(P o, db r, P p) {\n db x = (p - o).abs2(), d = x - r * r;\n if (sign(d) <= 0) return {}; // on circle => no tangent\n P q1 = o + (p - o) * (r * r / x);\n P q2 = (p - o).rot90() * (r * sqrt(d) / x);\n return {q1-q2,q1+q2}; //counter clock-wise\n}\n \n \nvector<L> extanCC(P o1, db r1, P o2, db r2) {\n vector<L> ret;\n if (cmp(r1, r2) == 0) {\n P dr = (o2 - o1).unit().rot90() * r1;\n ret.pb({o1 + dr, o2 + dr}), ret.pb({o1 - dr, o2 - dr});\n } else {\n P p = (o2 * r1 - o1 * r2) / (r1 - r2);\n vector<P> ps = tanCP(o1, r1, p), qs = tanCP(o2, r2, p);\n rep(i,0,min(ps.size(),qs.size())) ret.pb({ps[i], qs[i]}); //c1 counter-clock wise\n }\n return ret;\n}\n \nvector<L> intanCC(P o1, db r1, P o2, db r2) {\n vector<L> ret;\n P p = (o1 * r2 + o2 * r1) / (r1 + r2);\n vector<P> ps = tanCP(o1,r1,p), qs = tanCP(o2,r2,p);\n rep(i,0,min(ps.size(),qs.size())) ret.pb({ps[i], qs[i]}); //c1 counter-clock wise\n return ret;\n}\n \ndb areaCT(db r, P p1, P p2){\n vector<P> is = isCL(P(0,0),r,p1,p2);\n if(is.empty()) return r*r*rad(p1,p2)/2;\n bool b1 = cmp(p1.abs2(),r*r) == 1, b2 = cmp(p2.abs2(), r*r) == 1;\n if(b1 && b2){\n if(sign((p1-is[0]).dot(p2-is[0])) <= 0 &&\n sign((p1-is[0]).dot(p2-is[0])) <= 0)\n return r*r*(rad(p1,is[0]) + rad(is[1],p2))/2 + is[0].det(is[1])/2;\n else return r*r*rad(p1,p2)/2;\n }\n if(b1) return (r*r*rad(p1,is[0]) + is[0].det(p2))/2;\n if(b2) return (p1.det(is[1]) + r*r*rad(is[1],p2))/2;\n return p1.det(p2)/2;\n}\n \nbool parallel(L l0, L l1) { return sign( l0.dir().det( l1.dir() ) ) == 0; }\n \nbool sameDir(L l0, L l1) { return parallel(l0, l1) && sign(l0.dir().dot(l1.dir()) ) == 1; }\n \nbool cmp (P a, P b) {\n if (a.quad() != b.quad()) {\n return a.quad() < b.quad();\n } else {\n return sign( a.det(b) ) > 0;\n }\n}\n \nbool operator < (L l0, L l1) {\n if (sameDir(l0, l1)) {\n return l1.include(l0[0]);\n } else {\n return cmp( l0.dir(), l1.dir() );\n }\n}\n \nbool check(L u, L v, L w) { \n return w.include(isLL(u,v)); \n}\n \nvector<P> halfPlaneIS(vector<L> &l) {\n sort(l.begin(), l.end());\n deque<L> q;\n for (int i = 0; i < (int)l.size(); ++i) {\n if (i && sameDir(l[i], l[i - 1])) continue;\n while (q.size() > 1 && !check(q[q.size() - 2], q[q.size() - 1], l[i])) q.pop_back();\n while (q.size() > 1 && !check(q[1], q[0], l[i])) q.pop_front();\n q.push_back(l[i]);\n }\n while (q.size() > 2 && !check(q[q.size() - 2], q[q.size() - 1], q[0])) q.pop_back();\n while (q.size() > 2 && !check(q[1], q[0], q[q.size() - 1])) q.pop_front();\n vector<P> ret;\n for (int i = 0; i < (int)q.size(); ++i) ret.push_back(isLL(q[i], q[(i + 1) % q.size()]));\n return ret;\n}\n\nP inCenter(P A, P B, P C) { // ??????\n double a = (B - C).abs(), b = (C - A).abs(), c = (A - B).abs();\n return (A * a + B * b + C * c) / (a + b + c);\n}\n\nP circumCenter(P a, P b, P c) { // ??????\n P bb = b - a, cc = c - a;\n double db = bb.abs2(), dc = cc.abs2(), d = 2 * bb.det(cc);\n return a - P(bb.y * dc - cc.y * db, cc.x * db - bb.x * dc) / d;\n}\n\nP othroCenter(P a, P b, P c) { // ??????\n P ba = b - a, ca = c - a, bc = b - c;\n double Y = ba.y * ca.y * bc.y,\n A = ca.x * ba.y - ba.x * ca.y,\n x0 = (Y + ca.x * ba.y * b.x - ba.x * ca.y * c.x) / A,\n y0 = -ba.x * (x0 - c.x) / ba.y + ca.y;\n return {x0, y0};\n}\n\nconst int MAX_N = 500 + 10;\n\nvector<P> ps[MAX_N];\nvector<int> ids[MAX_N];\n\nint n;\nP bomb;\nP O = {0,0};\n\nvector<P> pList;\n\nbool edge[MAX_N][MAX_N];\ndb len[MAX_N][MAX_N];\n\nbool good[MAX_N];\n\nint tot;\n\nint total;\n\nbool check(P L, P R, P p){\n //is p strictly belong to L -> R?\n if(L.det(R) > -EPS){ // <= 180 degree\n return L.det(p) > EPS && p.det(R) > EPS;\n }\n //L.det(R) < -EPS\n if(L.det(p) >= 0) return 1;\n if(p.det(R) > EPS) return 1;\n return 0;\n}\n\nbool strict_polys_segment(vector<P>&qs,P p1,P p2){\n int n = qs.size();\n\n P m = (p1+p2)/2;\n\n if(contain(qs,m) >= 2) return 1;\n\n rep(i,0,n){\n P q1 = qs[i], q2 = qs[(i+1)%n];\n if(isSS_strict(p1,p2,q1,q2)) return 1;\n if(onSeg_strict(p1,p2,q1)){\n P q0 = qs[(i+n-1)%n];\n if(check(q2-q1,q0-q1,p1-q1)) return 1;\n if(check(q2-q1,q0-q1,p2-q1)) return 1;\n }\n }\n\n return 0;\n}\n\nbool check_quick(P p1,P p2){\n rep(i,0,n)\n if(strict_polys_segment(ps[i],p1,p2)) return 0;\n return 1;\n}\n\nbool check_bf(P p1, P p2){\n\n bool touch = 0;\n rep(i,0,n){\n int m = ps[i].size();\n rep(j,0,m){\n P q1 = ps[i][j], q2 = ps[i][(j+1)%m];\n if(isSS_strict(p1,p2,q1,q2)) return 0;\n if(isSS(p1,p2,q1,q2)) touch = 1;\n }\n }\n\n if(!touch) return 1;\n\n //bf n^2\n if(p2 < p1) swap(p1,p2);\n\n\n rep(i,0,n){\n int m = ps[i].size();\n\n vector<P> is = {p1,p2};\n\n rep(j,0,m){ \n P q1 = ps[i][j], q2 = ps[i][(j+1)%m];\n if(chkLL(p1,p2,q1,q2)){\n P u = isLL(p1,p2,q1,q2);\n if(isMiddle(p1,u,p2))\n is.pb(u);\n }\n }\n\n sort(is.begin(), is.end());\n\n rep(j,0,is.size() - 1){\n if(is[j].distTo(is[j+1]) < EPS) continue;\n P m = (is[j] + is[j+1]) / 2;\n ++total;\n if(contain(ps[i],m) == 2) return 0;\n }\n }\n\n return 1;\n}\n\nbool check(P p1,P p2){\n return check_quick(p1,p2);\n}\n\nvector<P> remove_colinear_point(vector<P> ps){\n deque<P> que;\n for(auto p : ps){\n while(que.size() >= 2 && crossOp(que[que.size()-2],que.back(),p) == 0)\n que.pop_back();\n que.push_back(p);\n }\n\n while(que.size() >= 3 && crossOp(que[que.size()-2],que.back(),que[0]) == 0)\n que.pop_back();\n\n while(que.size() >= 3 && crossOp(que.back(),que[0],que[1]) == 0)\n que.pop_front();\n\n ps = {};\n while(!que.empty()) ps.push_back(que.front()), que.pop_front();\n return ps;\n}\n\nint main(){\n while(cin>>n){\n if(!n) break;\n bomb.read();\n\n pList.clear();\n\n rep(i,0,n){\n ps[i].clear();\n ids[i].clear();\n\n int m;cin>>m;\n\n rep(j,0,m){\n P u; u.read(); \n ps[i].pb(u); \n }\n\n ps[i] = remove_colinear_point(ps[i]);\n\n for(auto p : ps[i]){\n ids[i].pb(pList.size());\n pList.pb(p);\n }\n }\n\n pList.pb(O);pList.pb(bomb); tot = pList.size();\n\n if(!check_bf(O,bomb)){\n printf(\"%0.10f\\n\",0.0);\n continue;\n }\n\n rep(i,0,tot) rep(j,0,i){\n edge[i][j] = edge[j][i] = check(pList[i],pList[j]);\n len[i][j] = len[j][i] = pList[i].distTo(pList[j]);\n }\n\n memset(good,0,sizeof good);\n\n //get good edge\n\n rep(i,0,n){\n rep(j,0,ids[i].size()){\n int x = ids[i][j], y = ids[i][(j+1)%ids[i].size()];\n P u = (pList[x] + pList[y])/2;\n if(!check(u,bomb))\n good[x] = good[y] = 1;\n }\n }\n\n db dist[MAX_N];\n bool used[MAX_N] = {};\n\n rep(i,0,tot) dist[i] = 1e100;\n\n dist[tot-2] = 0;\n\n db ans = 1e100;\n\n rep(_,0,tot){\n int u = -1; rep(i,0,tot) if(!used[i] && (u == -1 || dist[i] < dist[u]) ) u = i;\n used[u] = 1;\n rep(j,0,tot) if(edge[u][j] && dist[u] + len[u][j] < dist[j])\n dist[j] = dist[u] + len[u][j];\n }\n\n rep(i,0,tot) if(good[i]) ans = min(ans,dist[i]); db first = ans;\n \n rep(i,0,tot) if(dist[i] < ans){\n rep(j,0,tot-2){\n P u = proj(bomb,pList[j],pList[i]);\n db ret = dist[i] + pList[i].distTo(u);\n if(ret < ans){\n if(u.dot(pList[j] - bomb) > pList[j].dot(pList[j] - bomb) && check(pList[i],u))\n ans = ret;\n }\n }\n } \n \n\n printf(\"%0.10f\\n\",ans);//cout<<\"TOTAL:\"<<total<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 3400, "memory_kb": 5564, "score_of_the_acc": -1.0828, "final_rank": 13 }, { "submission_id": "aoj_2246_2165668", "code_snippet": "#include<bits/stdc++.h>\n#define MAX 1024\n#define inf 1<<29\n#define linf 1e16\n#define eps (1e-8)\n#define mod 1000000007\n#define pi acos(-1)\n#define phi (1.0+sqrt(5))/2.0\n#define f first\n#define s second\n#define mp make_pair\n#define pb push_back\n#define all(a) (a).begin(),(a).end()\n#define pd(a) printf(\"%.10f\\n\",(double)(a))\n#define FOR(i,a,b) for(int i=(a);i<(b);i++)\n#define RFOR(i,a,b) for(int i=(a)-1;(b)<=i;i--)\n#define equals(a,b) (fabs((a)-(b))<eps)\nusing namespace std;\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef pair<int,int> pii;\ntypedef pair<int,double> pid;\ntypedef pair<double,int> pdi;\ntypedef vector<int> vi;\ntypedef vector<pii> vpi;\nconst int dx[8]={1,0,-1,0,1,1,-1,-1};\nconst int dy[8]={0,1,0,-1,1,-1,1,-1};\n \nclass Point{\npublic:\n double x,y;\n Point(double x=0,double y=0):x(x),y(y){}\n \n Point operator+(Point p){ return Point(x+p.x,y+p.y);}\n Point operator-(Point p){ return Point(x-p.x,y-p.y);}\n Point operator*(double k){ return Point(x*k,y*k);}\n Point operator/(double k){ return Point(x/k,y/k);}\n bool operator<(Point p)const{ return equals(x,p.x) ? y-p.y<-eps : x-p.x<-eps; }\n bool operator==(Point p)const{ return fabs(x-p.x)<eps && fabs(y-p.y)<eps;}\n \n double abs(){ return sqrt(norm());}\n double norm(){ return (x*x+y*y);}\n};\ntypedef Point Vector;\ntypedef vector<Point> Polygon;\n \nclass Segment{\npublic:\n Point p1,p2;\n Segment(Point p1=Point(),Point p2=Point()):p1(p1),p2(p2){}\n};\ntypedef Segment Line;\n \ndouble norm(Vector a){ return (a.x*a.x+a.y*a.y);}\ndouble abs(Vector a){ return sqrt(norm(a));}\ndouble dot(Vector a,Vector b){ return (a.x*b.x+a.y*b.y);}\ndouble cross(Vector a,Vector b){ return (a.x*b.y-a.y*b.x);}\n \nPoint project(Segment s,Point p){\n Vector base=(s.p2-s.p1);\n double r=(dot(p-s.p1,base)/base.norm());\n return (s.p1+base*r);\n}\n \nbool isParallel(Vector a,Vector b){\n return equals(cross(a,b),0.0);\n}\n \nbool isParallel(Segment s,Segment t){\n return equals(cross(s.p1-s.p2,t.p1-t.p2),0.0);\n}\n \nbool intersect(Segment a,Segment b){\n if(cross(a.p2-a.p1,b.p1-a.p1)*cross(a.p2-a.p1,b.p2-a.p1)<-eps &&\n cross(b.p2-b.p1,a.p1-b.p1)*cross(b.p2-b.p1,a.p2-b.p1)<-eps)\n return true;\n return false;\n}\n \nint ccw(Point p0,Point p1,Point p2){\n Vector a=p1-p0;\n Vector b=p2-p0;\n if(cross(a,b)>eps)return 1;\n if(cross(a,b)<-eps)return -1;\n if(dot(a,b)<-eps)return 2;\n if(a.norm()<b.norm())return -2;\n return 0;\n}\n/*\nbool intersect(Point p1,Point p2,Point p3,Point p4){\n return (ccw(p1,p2,p3)*ccw(p1,p2,p4)<=0 &&\n ccw(p3,p4,p1)*ccw(p3,p4,p2)<=0);\n}\n \nbool intersect(Segment s1,Segment s2){\n return intersect(s1.p1,s1.p2,s2.p1,s2.p2);\n}*/\n \nint contains(Polygon g,Point p){\n int n=g.size();\n bool x=false;\n for(int i=0;i<n;i++){\n Vector a=g[i]-p,b=g[(i+1)%n]-p;\n if(abs(cross(a,b))<eps && dot(a,b)<eps)return 1;\n if(a.y>b.y)swap(a,b);\n if(a.y<eps && eps<b.y && cross(a,b)>eps)x=!x;\n }\n if(x)return 2;\n return 0;\n}\n \nint n,m;\nPoint s,ori(0,0);\nvector<Point> g;\nvector<Polygon> buildings;\nvector<Point> vp;\nvector<pid> e[MAX];\n \nvoid init(){\n buildings.clear();\n vp.clear();\n FOR(i,0,MAX)e[i].clear();\n g.clear();\n}\n \nvoid add_edge(int to,int from,double cost){\n e[to].pb(mp(from,cost));\n e[from].pb(mp(to,cost));\n}\n \nbool check(Point a){\n FOR(i,0,n)if(contains(buildings[i],a)==2)return false;\n return true;\n}\n \nbool check(Segment s){\n FOR(i,0,n){\n Polygon p=buildings[i];\n m=p.size();\n FOR(j,0,m){\n Point a=p[j],b=p[(j+1)%m],c=p[(j-1+m)%m];\n if(isParallel(Segment(a,b),s))continue;\n if(intersect(Segment(a,b),s))return false;\n if(ccw(s.p1,s.p2,a)==0 && !(s.p1==a) && !(s.p2==b)){\n if(ccw(s.p1,s.p2,b)*ccw(s.p1,s.p2,c)==-1)return false;\n }\n }\n }\n return true;\n}\n \ndouble getdis(Point a,Point b){\n Point c=project(Line(s,b),a);\n if(abs(c-s)<abs(b-s))return inf;\n if(check(a+(c-a)/2.0) && check(Segment(a,c)))return abs(c-a);\n return inf;\n}\n \ndouble dijkstra(){\n double d[MAX];\n priority_queue<pdi,vector<pdi>,greater<pdi> > pq;\n fill(d,d+MAX,inf);\n d[0]=0;\n pq.push(mp(0,0));\n \n while(pq.size()){\n pdi u=pq.top();\n pq.pop();\n \n if(d[u.s]<u.f)continue;\n if(u.s==vp.size())return u.f;\n \n FOR(i,0,e[u.s].size()){\n int next=e[u.s][i].f;\n double cost=e[u.s][i].s+d[u.s];\n if(cost<d[next]){\n d[next]=cost;\n pq.push(mp(cost,next));\n }\n }\n }\n return inf;\n}\n \ndouble solve(){\n if(!check(Segment(ori,s)))return 0;\n vp.pb(ori);\n FOR(i,0,n){\n Polygon p=buildings[i];\n m=p.size();\n FOR(j,0,m){\n vp.pb(p[j]);\n if(ccw(s,p[j],p[(j-1+m)%m])*\n ccw(s,p[j],p[(j+1)%m])==1)g.pb(p[j]);\n }\n }\n \n FOR(i,0,vp.size()){\n FOR(j,i+1,vp.size()){\n if(check(vp[i]+(vp[j]-vp[i])/2.0) && \n check(Segment(vp[i],vp[j])))add_edge(i,j,abs(vp[i]-vp[j]));\n }\n }\n FOR(i,0,vp.size()){\n double cost=inf;\n FOR(j,0,g.size()){\n if(vp[i]==g[j])cost=0;\n else cost=min(cost,getdis(vp[i],g[j]));\n }\n add_edge(vp.size(),i,cost);\n }\n return dijkstra();\n}\n \nint main()\n{\n while(cin>>n && n){\n init();\n cin>>s.x>>s.y;\n FOR(i,0,n){\n cin>>m;\n Polygon p;\n FOR(j,0,m){\n int x,y;\n cin>>x>>y;\n p.pb(Point(x,y));\n }\n buildings.pb(p);\n }\n pd(solve());\n }\n return 0;\n}", "accuracy": 1, "time_ms": 5040, "memory_kb": 5028, "score_of_the_acc": -1.231, "final_rank": 17 }, { "submission_id": "aoj_2246_2163073", "code_snippet": "#include<bits/stdc++.h>\n#define MAX 1024\n#define inf 1<<29\n#define linf 1e16\n#define eps (1e-6)\n#define mod 1000000007\n#define pi acos(-1)\n#define phi (1.0+sqrt(5))/2.0\n#define f first\n#define s second\n#define mp make_pair\n#define pb push_back\n#define all(a) (a).begin(),(a).end()\n#define pd(a) printf(\"%.10f\\n\",(double)(a))\n#define FOR(i,a,b) for(int i=(a);i<(b);i++)\n#define RFOR(i,a,b) for(int i=(a)-1;(b)<=i;i--)\n#define equals(a,b) (fabs((a)-(b))<eps)\nusing namespace std;\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef pair<int,int> pii;\ntypedef pair<int,double> pid;\ntypedef pair<double,int> pdi;\ntypedef vector<int> vi;\ntypedef vector<pii> vpi;\nconst int dx[8]={1,0,-1,0,1,1,-1,-1};\nconst int dy[8]={0,1,0,-1,1,-1,1,-1};\n\nclass Point{\npublic:\n double x,y;\n Point(double x=0,double y=0):x(x),y(y){}\n\n Point operator+(Point p){ return Point(x+p.x,y+p.y);}\n Point operator-(Point p){ return Point(x-p.x,y-p.y);}\n Point operator*(double k){ return Point(x*k,y*k);}\n Point operator/(double k){ return Point(x/k,y/k);}\n bool operator<(Point p)const{ return equals(x,p.x) ? y-p.y<-eps : x-p.x<-eps; }\n bool operator==(Point p)const{ return fabs(x-p.x)<eps && fabs(y-p.y)<eps;}\n\n double abs(){ return sqrt(norm());}\n double norm(){ return (x*x+y*y);}\n};\ntypedef Point Vector;\ntypedef vector<Point> Polygon;\n\nclass Segment{\npublic:\n Point p1,p2;\n Segment(Point p1=Point(),Point p2=Point()):p1(p1),p2(p2){}\n};\ntypedef Segment Line;\n\ndouble norm(Vector a){ return (a.x*a.x+a.y*a.y);}\ndouble abs(Vector a){ return sqrt(norm(a));}\ndouble dot(Vector a,Vector b){ return (a.x*b.x+a.y*b.y);}\ndouble cross(Vector a,Vector b){ return (a.x*b.y-a.y*b.x);}\n\nPoint project(Segment s,Point p){\n Vector base=(s.p2-s.p1);\n double r=(dot(p-s.p1,base)/base.norm());\n return (s.p1+base*r);\n}\n\nbool isParallel(Vector a,Vector b){\n return equals(cross(a,b),0.0);\n}\n\nbool isParallel(Segment s,Segment t){\n return equals(cross(s.p1-s.p2,t.p1-t.p2),0.0);\n}\n\nbool intersect(Segment a,Segment b){\n if(cross(a.p2-a.p1,b.p1-a.p1)*cross(a.p2-a.p1,b.p2-a.p1)<-eps &&\n cross(b.p2-b.p1,a.p1-b.p1)*cross(b.p2-b.p1,a.p2-b.p1)<-eps)\n return true;\n return false;\n}\n\nbool intersectLS(Line L,Segment s){\n return cross(L.p2-L.p1,s.p1-L.p1)*cross(L.p2-L.p1,s.p2-L.p1)<-eps;\n}\n\nint ccw(Point p0,Point p1,Point p2){\n Vector a=p1-p0;\n Vector b=p2-p0;\n if(cross(a,b)>eps)return 1;\n if(cross(a,b)<-eps)return -1;\n if(dot(a,b)<-eps)return 2;\n if(a.norm()<b.norm())return -2;\n return 0;\n}\n\nPoint getCrossPointLL(Line a,Line b){\n double A=cross(a.p2-a.p1,b.p2-b.p1);\n double B=cross(a.p2-a.p1,a.p2-b.p1);\n if(abs(A)<eps || abs(B)<eps)return b.p1;\n return b.p1+(b.p2-b.p1)*(B/A);\n}\n\nint contains(Polygon g,Point p){\n int n=g.size();\n bool x=false;\n for(int i=0;i<n;i++){\n Vector a=g[i]-p,b=g[(i+1)%n]-p;\n if(abs(cross(a,b))<eps && dot(a,b)<eps)return 1;\n if(a.y>b.y)swap(a,b);\n if(a.y<eps && eps<b.y && cross(a,b)>eps)x=!x;\n }\n if(x)return 2;\n return 0;\n}\n\nint n,m;\nPoint s,ori(0,0);\nvector<Point> g;\nvector<Polygon> buildings;\nvector<Point> vp;\nvector<pid> e[MAX];\n\nvoid init(){\n buildings.clear();\n vp.clear();\n FOR(i,0,MAX)e[i].clear();\n g.clear();\n}\n\nvoid add_edge(int to,int from,double cost){\n e[to].pb(mp(from,cost));\n e[from].pb(mp(to,cost));\n}\n\nbool check(Point a){\n FOR(i,0,n)if(contains(buildings[i],a)==2)return false;\n return true;\n}\n\nbool check(Segment s){\n FOR(i,0,n){\n Polygon p=buildings[i];\n m=p.size();\n FOR(j,0,m){\n Point a=p[j],b=p[(j+1)%m],c=p[(j-1+m)%m];\n if(isParallel(Segment(a,b),s))continue;\n if(intersect(Segment(a,b),s))return false;\n }\n }\n return true;\n}\n\ndouble getdis(Point a,Point b){\n Line L(s,b);\n Point c=project(L,a);\n if(ccw(s,b,c)==-2 && check(a+(c-a)/2.0) &&\n check(Segment(a,c)))return abs(c-a);\n c=Point(inf,inf);\n FOR(i,0,n){\n Polygon p=buildings[i];\n m=p.size();\n FOR(j,0,m){\n Segment seg(p[j],p[(j+1)%m]);\n if(isParallel(L,seg))continue;\n if(!intersectLS(L,seg))continue;\n Point cp=getCrossPointLL(L,seg);\n if(ccw(s,b,cp)==-2 && abs(s-cp)<abs(s-c))c=cp;\n }\n }\n if(ccw(s,b,c)==-2 && check(a+(c-a)/2.0) &&\n check(Segment(a,c)))return abs(c-a);\n return inf;\n}\n\ndouble dijkstra(){\n double d[MAX];\n priority_queue<pdi,vector<pdi>,greater<pdi> > pq;\n fill(d,d+MAX,inf);\n d[0]=0;\n pq.push(mp(0,0));\n\n while(pq.size()){\n pdi u=pq.top();\n pq.pop();\n\n if(d[u.s]<u.f)continue;\n if(u.s==vp.size())return u.f;\n\n FOR(i,0,e[u.s].size()){\n int next=e[u.s][i].f;\n double cost=e[u.s][i].s+d[u.s];\n if(cost<d[next]){\n d[next]=cost;\n pq.push(mp(cost,next));\n }\n }\n }\n return inf;\n}\n\ndouble solve(){\n if(!check(Segment(ori,s)))return 0;\n vp.pb(ori);\n FOR(i,0,n){\n Polygon p=buildings[i];\n m=p.size();\n FOR(j,0,m){\n if(!check(Segment(s,p[j])))continue;\n vp.pb(p[j]);\n if(ccw(s,p[j],p[(j-1+m)%m])*\n ccw(s,p[j],p[(j+1)%m])==1)g.pb(p[j]);\n }\n }\n \n FOR(i,0,vp.size()){\n FOR(j,i+1,vp.size()){\n if(check(vp[i]+(vp[j]-vp[i])/2.0) && \n check(Segment(vp[i],vp[j])))add_edge(i,j,abs(vp[i]-vp[j]));\n }\n }\n FOR(i,0,vp.size()){\n double cost=inf;\n FOR(j,0,g.size()){\n if(vp[i]==g[j]){ cost=0;break; }\n else cost=min(cost,getdis(vp[i],g[j]));\n }\n add_edge(vp.size(),i,cost);\n }\n return dijkstra();\n}\n\nint main()\n{\n while(cin>>n && n){\n init();\n cin>>s.x>>s.y;\n FOR(i,0,n){\n cin>>m;\n Polygon p;\n FOR(j,0,m){\n int x,y;\n cin>>x>>y;\n p.pb(Point(x,y));\n }\n buildings.pb(p);\n }\n pd(solve());\n }\n return 0;\n}", "accuracy": 1, "time_ms": 2800, "memory_kb": 6896, "score_of_the_acc": -1.1971, "final_rank": 15 }, { "submission_id": "aoj_2246_2163071", "code_snippet": "#include<bits/stdc++.h>\n#define MAX 1024\n#define inf 1<<29\n#define linf 1e16\n#define eps (1e-6)\n#define mod 1000000007\n#define pi acos(-1)\n#define phi (1.0+sqrt(5))/2.0\n#define f first\n#define s second\n#define mp make_pair\n#define pb push_back\n#define all(a) (a).begin(),(a).end()\n#define pd(a) printf(\"%.10f\\n\",(double)(a))\n#define FOR(i,a,b) for(int i=(a);i<(b);i++)\n#define RFOR(i,a,b) for(int i=(a)-1;(b)<=i;i--)\n#define equals(a,b) (fabs((a)-(b))<eps)\nusing namespace std;\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef pair<int,int> pii;\ntypedef pair<int,double> pid;\ntypedef pair<double,int> pdi;\ntypedef vector<int> vi;\ntypedef vector<pii> vpi;\nconst int dx[8]={1,0,-1,0,1,1,-1,-1};\nconst int dy[8]={0,1,0,-1,1,-1,1,-1};\n\nclass Point{\npublic:\n double x,y;\n Point(double x=0,double y=0):x(x),y(y){}\n\n Point operator+(Point p){ return Point(x+p.x,y+p.y);}\n Point operator-(Point p){ return Point(x-p.x,y-p.y);}\n Point operator*(double k){ return Point(x*k,y*k);}\n Point operator/(double k){ return Point(x/k,y/k);}\n bool operator<(Point p)const{ return equals(x,p.x) ? y-p.y<-eps : x-p.x<-eps; }\n bool operator==(Point p)const{ return fabs(x-p.x)<eps && fabs(y-p.y)<eps;}\n\n double abs(){ return sqrt(norm());}\n double norm(){ return (x*x+y*y);}\n};\ntypedef Point Vector;\ntypedef vector<Point> Polygon;\n\nclass Segment{\npublic:\n Point p1,p2;\n Segment(Point p1=Point(),Point p2=Point()):p1(p1),p2(p2){}\n};\ntypedef Segment Line;\n\ndouble norm(Vector a){ return (a.x*a.x+a.y*a.y);}\ndouble abs(Vector a){ return sqrt(norm(a));}\ndouble dot(Vector a,Vector b){ return (a.x*b.x+a.y*b.y);}\ndouble cross(Vector a,Vector b){ return (a.x*b.y-a.y*b.x);}\n\nPoint project(Segment s,Point p){\n Vector base=(s.p2-s.p1);\n double r=(dot(p-s.p1,base)/base.norm());\n return (s.p1+base*r);\n}\n\nbool isParallel(Vector a,Vector b){\n return equals(cross(a,b),0.0);\n}\n\nbool isParallel(Segment s,Segment t){\n return equals(cross(s.p1-s.p2,t.p1-t.p2),0.0);\n}\n\nbool intersect(Segment a,Segment b){\n if(cross(a.p2-a.p1,b.p1-a.p1)*cross(a.p2-a.p1,b.p2-a.p1)<-eps &&\n cross(b.p2-b.p1,a.p1-b.p1)*cross(b.p2-b.p1,a.p2-b.p1)<-eps)\n return true;\n return false;\n}\n\nbool intersectLS(Line L,Segment s){\n return cross(L.p2-L.p1,s.p1-L.p1)*cross(L.p2-L.p1,s.p2-L.p1)<-eps;\n}\n\nint ccw(Point p0,Point p1,Point p2){\n Vector a=p1-p0;\n Vector b=p2-p0;\n if(cross(a,b)>eps)return 1;\n if(cross(a,b)<-eps)return -1;\n if(dot(a,b)<-eps)return 2;\n if(a.norm()<b.norm())return -2;\n return 0;\n}\n\nPoint getCrossPointLL(Line a,Line b){\n double A=cross(a.p2-a.p1,b.p2-b.p1);\n double B=cross(a.p2-a.p1,a.p2-b.p1);\n if(abs(A)<eps || abs(B)<eps)return b.p1;\n return b.p1+(b.p2-b.p1)*(B/A);\n}\n\nint contains(Polygon g,Point p){\n int n=g.size();\n bool x=false;\n for(int i=0;i<n;i++){\n Vector a=g[i]-p,b=g[(i+1)%n]-p;\n if(abs(cross(a,b))<eps && dot(a,b)<eps)return 1;\n if(a.y>b.y)swap(a,b);\n if(a.y<eps && eps<b.y && cross(a,b)>eps)x=!x;\n }\n if(x)return 2;\n return 0;\n}\n\nint n,m;\nPoint s,ori(0,0);\nvector<Point> g;\nvector<Polygon> buildings;\nvector<Point> vp;\nvector<pid> e[MAX];\n\nvoid init(){\n buildings.clear();\n vp.clear();\n FOR(i,0,MAX)e[i].clear();\n g.clear();\n}\n\nvoid add_edge(int to,int from,double cost){\n e[to].pb(mp(from,cost));\n e[from].pb(mp(to,cost));\n}\n\nbool check(Point a){\n FOR(i,0,n)if(contains(buildings[i],a)==2)return false;\n return true;\n}\n\nbool check(Segment s){\n FOR(i,0,n){\n Polygon p=buildings[i];\n m=p.size();\n FOR(j,0,m){\n Point a=p[j],b=p[(j+1)%m],c=p[(j-1+m)%m];\n if(isParallel(Segment(a,b),s))continue;\n if(intersect(Segment(a,b),s))return false;\n if(ccw(s.p1,s.p2,a)==0 && !(s.p1==a) && !(s.p2==b)){\n if(ccw(s.p1,s.p2,b)*ccw(s.p1,s.p2,c)==-1)return false;\n }\n }\n }\n return true;\n}\n\ndouble getdis(Point a,Point b){\n Line L(s,b);\n Point c=project(L,a);\n if(ccw(s,b,c)==-2 && check(a+(c-a)/2.0) &&\n check(Segment(a,c)))return abs(c-a);\n c=Point(inf,inf);\n FOR(i,0,n){\n Polygon p=buildings[i];\n m=p.size();\n FOR(j,0,m){\n Segment seg(p[j],p[(j+1)%m]);\n if(isParallel(L,seg))continue;\n if(!intersectLS(L,seg))continue;\n Point cp=getCrossPointLL(L,seg);\n if(ccw(s,b,cp)==-2 && abs(s-cp)<abs(s-c))c=cp;\n }\n }\n if(ccw(s,b,c)==-2 && check(a+(c-a)/2.0) &&\n check(Segment(a,c)))return abs(c-a);\n return inf;\n}\n\ndouble dijkstra(){\n double d[MAX];\n priority_queue<pdi,vector<pdi>,greater<pdi> > pq;\n fill(d,d+MAX,inf);\n d[0]=0;\n pq.push(mp(0,0));\n\n while(pq.size()){\n pdi u=pq.top();\n pq.pop();\n\n if(d[u.s]<u.f)continue;\n if(u.s==vp.size())return u.f;\n\n FOR(i,0,e[u.s].size()){\n int next=e[u.s][i].f;\n double cost=e[u.s][i].s+d[u.s];\n if(cost<d[next]){\n d[next]=cost;\n pq.push(mp(cost,next));\n }\n }\n }\n return inf;\n}\n\ndouble solve(){\n if(!check(Segment(ori,s)))return 0;\n vp.pb(ori);\n FOR(i,0,n){\n Polygon p=buildings[i];\n m=p.size();\n FOR(j,0,m){\n vp.pb(p[j]);\n if(ccw(s,p[j],p[(j-1+m)%m])*\n ccw(s,p[j],p[(j+1)%m])==1)g.pb(p[j]);\n }\n }\n \n FOR(i,0,vp.size()){\n FOR(j,i+1,vp.size()){\n if(check(vp[i]+(vp[j]-vp[i])/2.0) && \n check(Segment(vp[i],vp[j])))add_edge(i,j,abs(vp[i]-vp[j]));\n }\n }\n FOR(i,0,vp.size()){\n double cost=inf;\n FOR(j,0,g.size()){\n if(vp[i]==g[j])cost=0;\n else cost=min(cost,getdis(vp[i],g[j]));\n }\n add_edge(vp.size(),i,cost);\n }\n return dijkstra();\n}\n\nint main()\n{\n while(cin>>n && n){\n init();\n cin>>s.x>>s.y;\n FOR(i,0,n){\n cin>>m;\n Polygon p;\n FOR(j,0,m){\n int x,y;\n cin>>x>>y;\n p.pb(Point(x,y));\n }\n buildings.pb(p);\n }\n pd(solve());\n }\n return 0;\n}", "accuracy": 1, "time_ms": 7270, "memory_kb": 4956, "score_of_the_acc": -1.5301, "final_rank": 20 }, { "submission_id": "aoj_2246_2163054", "code_snippet": "#include<bits/stdc++.h>\n#define MAX 1024\n#define inf 1<<29\n#define linf 1e16\n#define eps (1e-8)\n#define mod 1000000007\n#define pi acos(-1)\n#define phi (1.0+sqrt(5))/2.0\n#define f first\n#define s second\n#define mp make_pair\n#define pb push_back\n#define all(a) (a).begin(),(a).end()\n#define pd(a) printf(\"%.10f\\n\",(double)(a))\n#define FOR(i,a,b) for(int i=(a);i<(b);i++)\n#define RFOR(i,a,b) for(int i=(a)-1;(b)<=i;i--)\n#define equals(a,b) (fabs((a)-(b))<eps)\nusing namespace std;\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef pair<int,int> pii;\ntypedef pair<int,double> pid;\ntypedef pair<double,int> pdi;\ntypedef vector<int> vi;\ntypedef vector<pii> vpi;\nconst int dx[8]={1,0,-1,0,1,1,-1,-1};\nconst int dy[8]={0,1,0,-1,1,-1,1,-1};\n\nclass Point{\npublic:\n double x,y;\n Point(double x=0,double y=0):x(x),y(y){}\n\n Point operator+(Point p){ return Point(x+p.x,y+p.y);}\n Point operator-(Point p){ return Point(x-p.x,y-p.y);}\n Point operator*(double k){ return Point(x*k,y*k);}\n Point operator/(double k){ return Point(x/k,y/k);}\n bool operator<(Point p)const{ return equals(x,p.x) ? y-p.y<-eps : x-p.x<-eps; }\n bool operator==(Point p)const{ return fabs(x-p.x)<eps && fabs(y-p.y)<eps;}\n\n double abs(){ return sqrt(norm());}\n double norm(){ return (x*x+y*y);}\n};\ntypedef Point Vector;\ntypedef vector<Point> Polygon;\n\nclass Segment{\npublic:\n Point p1,p2;\n Segment(Point p1=Point(),Point p2=Point()):p1(p1),p2(p2){}\n};\ntypedef Segment Line;\n\ndouble norm(Vector a){ return (a.x*a.x+a.y*a.y);}\ndouble abs(Vector a){ return sqrt(norm(a));}\ndouble dot(Vector a,Vector b){ return (a.x*b.x+a.y*b.y);}\ndouble cross(Vector a,Vector b){ return (a.x*b.y-a.y*b.x);}\n\nPoint project(Segment s,Point p){\n Vector base=(s.p2-s.p1);\n double r=(dot(p-s.p1,base)/base.norm());\n return (s.p1+base*r);\n}\n\nbool isParallel(Vector a,Vector b){\n return equals(cross(a,b),0.0);\n}\n\nbool isParallel(Segment s,Segment t){\n return equals(cross(s.p1-s.p2,t.p1-t.p2),0.0);\n}\n\nbool intersect(Segment a,Segment b){\n if(cross(a.p2-a.p1,b.p1-a.p1)*cross(a.p2-a.p1,b.p2-a.p1)<-eps &&\n cross(b.p2-b.p1,a.p1-b.p1)*cross(b.p2-b.p1,a.p2-b.p1)<-eps)\n return true;\n return false;\n}\n\nint ccw(Point p0,Point p1,Point p2){\n Vector a=p1-p0;\n Vector b=p2-p0;\n if(cross(a,b)>eps)return 1;\n if(cross(a,b)<-eps)return -1;\n if(dot(a,b)<-eps)return 2;\n if(a.norm()<b.norm())return -2;\n return 0;\n}\n/*\nbool intersect(Point p1,Point p2,Point p3,Point p4){\n return (ccw(p1,p2,p3)*ccw(p1,p2,p4)<=0 &&\n ccw(p3,p4,p1)*ccw(p3,p4,p2)<=0);\n}\n\nbool intersect(Segment s1,Segment s2){\n return intersect(s1.p1,s1.p2,s2.p1,s2.p2);\n}*/\n\nint contains(Polygon g,Point p){\n int n=g.size();\n bool x=false;\n for(int i=0;i<n;i++){\n Vector a=g[i]-p,b=g[(i+1)%n]-p;\n if(abs(cross(a,b))<eps && dot(a,b)<eps)return 1;\n if(a.y>b.y)swap(a,b);\n if(a.y<eps && eps<b.y && cross(a,b)>eps)x=!x;\n }\n if(x)return 2;\n return 0;\n}\n\nint n,m;\nPoint s,ori(0,0);\nvector<Point> g;\nvector<Polygon> buildings;\nvector<Point> vp;\nvector<pid> e[MAX];\n\nvoid init(){\n buildings.clear();\n vp.clear();\n FOR(i,0,MAX)e[i].clear();\n g.clear();\n}\n\nvoid add_edge(int to,int from,double cost){\n e[to].pb(mp(from,cost));\n e[from].pb(mp(to,cost));\n}\n\nbool check(Point a){\n FOR(i,0,n)if(contains(buildings[i],a)==2)return false;\n return true;\n}\n\nbool check(Segment s){\n FOR(i,0,n){\n Polygon p=buildings[i];\n m=p.size();\n FOR(j,0,m){\n Point a=p[j],b=p[(j+1)%m],c=p[(j-1+m)%m];\n if(isParallel(Segment(a,b),s))continue;\n if(intersect(Segment(a,b),s))return false;\n if(ccw(s.p1,s.p2,a)==0 && !(s.p1==a) && !(s.p2==b)){\n if(ccw(s.p1,s.p2,b)*ccw(s.p1,s.p2,c)==-1)return false;\n }\n }\n }\n return true;\n}\n\ndouble getdis(Point a,Point b){\n Point c=project(Line(s,b),a);\n if(abs(c-s)<abs(b-s))return inf;\n if(check(a+(c-a)/2.0),check(Segment(a,c)))return abs(c-a);\n return inf;\n}\n\ndouble dijkstra(){\n double d[MAX];\n priority_queue<pdi,vector<pdi>,greater<pdi> > pq;\n fill(d,d+MAX,inf);\n d[0]=0;\n pq.push(mp(0,0));\n\n while(pq.size()){\n pdi u=pq.top();\n pq.pop();\n\n if(d[u.s]<u.f)continue;\n if(u.s==vp.size())return u.f;\n\n FOR(i,0,e[u.s].size()){\n int next=e[u.s][i].f;\n double cost=e[u.s][i].s+d[u.s];\n if(cost<d[next]){\n d[next]=cost;\n pq.push(mp(cost,next));\n }\n }\n }\n return inf;\n}\n\ndouble solve(){\n if(!check(Segment(ori,s)))return 0;\n vp.pb(ori);\n FOR(i,0,n){\n Polygon p=buildings[i];\n m=p.size();\n FOR(j,0,m){\n vp.pb(p[j]);\n if(ccw(s,p[j],p[(j-1+m)%m])*\n ccw(s,p[j],p[(j+1)%m])==1)g.pb(p[j]);\n }\n }\n \n FOR(i,0,vp.size()){\n FOR(j,i+1,vp.size()){\n if(check(vp[i]+(vp[j]-vp[i])/2.0) && \n check(Segment(vp[i],vp[j])))add_edge(i,j,abs(vp[i]-vp[j]));\n }\n }\n FOR(i,0,vp.size()){\n double cost=inf;\n FOR(j,0,g.size()){\n if(vp[i]==g[j])cost=0;\n else cost=min(cost,getdis(vp[i],g[j]));\n }\n add_edge(vp.size(),i,cost);\n }\n return dijkstra();\n}\n\nint main()\n{\n while(cin>>n && n){\n init();\n cin>>s.x>>s.y;\n FOR(i,0,n){\n cin>>m;\n Polygon p;\n FOR(j,0,m){\n int x,y;\n cin>>x>>y;\n p.pb(Point(x,y));\n }\n buildings.pb(p);\n }\n pd(solve());\n }\n return 0;\n}", "accuracy": 1, "time_ms": 5050, "memory_kb": 4952, "score_of_the_acc": -1.2212, "final_rank": 16 }, { "submission_id": "aoj_2246_1593053", "code_snippet": "#include<cmath>\n#include<vector>\n#include<cstdio>\n#include<cstring>\n#include<iostream>\n#include<algorithm>\nusing namespace std;\n\ninline int sign(double x, double eps = 1e-8) {\n\treturn x < -eps ? -1 : x > eps;\n}\n\nstruct Point {\n\tdouble x, y;\n\n\tPoint (double x = 0, double y = 0) : x(x), y(y) {}\n\n\tvoid in() {\n\t\tscanf(\"%lf%lf\", &x, &y);\n\t}\n\n\tPoint operator + (const Point &a) const {\n\t\treturn Point(x + a.x, y + a.y);\n\t}\n\n\tPoint operator - (const Point &a) const {\n\t\treturn Point(x - a.x, y - a.y);\n\t}\n\n\tPoint operator * (const double &k) const {\n\t\treturn Point(x * k, y * k);\n\t}\n\n\tPoint operator / (const double &k) const {\n\t\treturn Point(x / k, y / k);\n\t}\n\n\tdouble len() const {\n\t\treturn sqrt(x * x + y * y);\n\t}\n\n\tdouble len2() const {\n\t\treturn x * x + y * y;\n\t}\n};\n\ndouble dot(const Point &a, const Point &b) {\n\treturn a.x * b.x + a.y * b.y;\n}\n\ndouble det(const Point &a, const Point &b) {\n\treturn a.x * b.y - a.y * b.x;\n}\n\nstruct Segment {\n\tPoint a, b;\n\n\tSegment(Point a = Point(0, 0), Point b = Point(0, 0)) : a(a), b(b) {}\n\n\tPoint mid() const {\n\t\treturn (a + b) / 2;\n\t}\n};\n\nbool parallel(const Segment &l0, const Segment &l1) {\n\treturn sign(det(l0.b - l0.a, l1.b - l1.a)) == 0;\n}\n\nPoint intersect(const Segment &l0, const Segment &l1) {\n\tdouble s0 = det(l1.a - l0.a, l0.b - l0.a),\n\t\t s1 = det(l1.b - l0.a, l0.b - l0.a);\n\treturn (l1.b * s0 - l1.a * s1) / (s0 - s1);\n}\n\nbool onLine(const Segment &l0, const Point &p) {\n\treturn sign(det(p - l0.a, l0.b - l0.a)) == 0;\n}\n\nbool inSeg(const Segment &l0, const Point &p) {\n\treturn sign(dot(p - l0.a, l0.b - l0.a)) > 0 && sign(dot(p - l0.b, l0.a - l0.b)) > 0;\n}\n\nbool onSeg(const Segment &l0, const Point &p) {\n\treturn sign(dot(p - l0.a, l0.b - l0.a)) >= 0 && sign(dot(p - l0.b, l0.a - l0.b)) >= 0;\n}\n\nPoint project(const Segment &l, const Point &p) {\n\treturn l.a + (l.b - l.a) * dot(p - l.a, l.b - l.a) / (l.b - l.a).len2();\n}\n\nbool inside(const Point &q, const vector<Point> &p) {\n\tint cnt = 0;\n\tfor (int i = 0; i < (int)p.size(); ++i) {\n\t\tPoint a = p[i], b = p[(i + 1) % p.size()];\n\t\tif (onLine(Segment(a, b), q) && onSeg(Segment(a, b), q)) {\n\t\t\treturn false;\n\t\t}\n\t\tif (sign(a.y - b.y) <= 0) {\n\t\t\tswap(a, b);\n\t\t}\n\t\tif (sign(q.y - a.y) > 0) {\n\t\t\tcontinue;\n\t\t}\n\t\tif (sign(q.y - b.y) <= 0) {\n\t\t\tcontinue;\n\t\t}\n\t\tcnt += sign(det(b - q, a - q)) > 0;\n\t}\n\treturn cnt & 1;\n}\n\nint n;\n\nvector<vector<Point> > polys;\n\nbool legal(const Segment &s) {\n\tif (sign((s.b - s.a).len()) == 0) {\n\t\treturn true;\n\t}\n\tfor (int i = 0; i < n; ++i) {\n\t\tfor (int j = 0; j < (int)polys[i].size(); ++j) {\n\t\t\tSegment l(polys[i][j], polys[i][(j + 1) % polys[i].size()]);\n\t\t\tif (parallel(s, l)) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tPoint p = intersect(s, l);\n\t\t\tif (inSeg(s, p) && onSeg(l, p)) {\n\t\t\t\treturn false;\n\t\t\t}\n\t\t}\n\t\tif (inside(s.mid(), polys[i])) {\n\t\t\treturn false;\n\t\t}\n\t}\n\treturn true;\n}\n\nconst double FAR = 1e6;\n\nPoint getSafe(const Point &p, const Point &b) {\n\tPoint d = p - b;\n\td = d / d.len();\n\tSegment s(p, p + d * FAR);\n\tfor (int i = 0; i < n; ++i) {\n\t\tfor (int j = 0; j < (int)polys[i].size(); ++j) {\n\t\t\tSegment l(polys[i][j], polys[i][(j + 1) % polys[i].size()]);\n\t\t\tif (parallel(s, l)) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tPoint p = intersect(s, l);\n\t\t\tif (inSeg(s, p) && onSeg(l, p)) {\n\t\t\t\ts.b = p;\n\t\t\t}\n\t\t}\n\t\tif (inside(s.mid(), polys[i])) {\n\t\t\treturn p;\n\t\t}\n\t}\n\treturn s.b;\n}\n\nconst double INF = 1e9;\n\nconst int M = 505;\n\ndouble g[M][M];\n\nPoint a, b;\n\nint main() {\n\twhile (scanf(\"%d\", &n) == 1 && n) {\n\t\ta = Point(0, 0);\n\t\tb.in();\n\t\tvector<Point> ps;\n\t\tpolys.clear();\n\t\tfor (int i = 0; i < n; ++i) {\n\t\t\tint m;\n\t\t\tscanf(\"%d\", &m);\n\t\t\tvector<Point> poly(m);\n\t\t\tfor (int j = 0; j < m; ++j) {\n\t\t\t\tpoly[j].in();\n\t\t\t\tps.push_back(poly[j]);\n\t\t\t}\n\t\t\tpolys.push_back(poly);\n\t\t}\n\t\tdouble ans = INF;\n\t\tif (!legal(Segment(a, b))) {\n\t\t\tans = 0;\n\t\t} else {\n\t\t\tfor (int i = 0; i <= ps.size(); ++i) {\n\t\t\t\tPoint a = i ? ps[i - 1] : ::a;\n\t\t\t\tg[i][i] = 0;\n\t\t\t\tfor (int j = i + 1; j <= ps.size(); ++j) {\n\t\t\t\t\tPoint b = ps[j - 1];\n\t\t\t\t\tif (legal(Segment(a, b))) {\n\t\t\t\t\t\tg[i][j] = g[j][i] = (b - a).len();\n\t\t\t\t\t} else {\n\t\t\t\t\t\tg[i][j] = g[j][i] = INF;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tfor (int k = 0; k <= ps.size(); ++k) {\n\t\t\t\tfor (int i = 0; i <= ps.size(); ++i) {\n\t\t\t\t\tfor (int j = 0; j <= ps.size(); ++j) {\n\t\t\t\t\t\tg[i][j] = min(g[i][j], g[i][k] + g[k][j]);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tfor (int i = 0; i < ps.size(); ++i) {\n\t\t\t\tPoint block = ps[i];\n\t\t\t\tif (!legal(Segment(b, block))) {\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tSegment safe(block, getSafe(block, b));\n\t\t\t\tif (sign((safe.b - safe.a).len()) == 0) {\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tfor (int j = 0; j <= (int)ps.size(); ++j) {\t\n\t\t\t\t\tPoint term = j ? ps[j - 1] : a,\n\t\t\t\t\t\t target = project(safe, term);\n\t\t\t\t\tif (onSeg(safe, target) && legal(Segment(target, term))) {\n\t\t\t\t\t\tans = min(ans, g[0][j] + (term - target).len());\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tprintf(\"%.12f\\n\", ans);\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 7470, "memory_kb": 3264, "score_of_the_acc": -1.3068, "final_rank": 18 }, { "submission_id": "aoj_2246_1258940", "code_snippet": "#include<bits/stdc++.h>\n\n#define REP(i,s,n) for(int i=s;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n#define EPS (1e-8)\n#define equals(a,b) (fabs((a)-(b)) < EPS)\n#define COUNTER_CLOCKWISE 1\n#define CLOCKWISE -1 \n#define ONLINE_BACK 2\n#define ONLINE_FRONT -2\n#define ON_SEGMENT 0\n\nusing namespace std;\n\n\nclass Point{\npublic:\n double x,y;\n\n Point(double x = 0,double y = 0): x(x),y(y){}\n\n Point operator + (Point p){return Point(x+p.x,y+p.y);}\n Point operator - (Point p){return Point(x-p.x,y-p.y);}\n Point operator * (double a){return Point(a*x,a*y);}\n Point operator / (double a){return Point(x/a,y/a);}\n Point operator * (Point a){ return Point(x * a.x - y * a.y, x * a.y + y * a.x); }\n\n bool operator < (const Point& p) const{ return !equals(x,p.x)?x<p.x:(!equals(y,p.y)&&y<p.y); }\n\n bool operator == (const Point& p)const{ return fabs(x-p.x) < EPS && fabs(y-p.y) < EPS; }\n\n};\n\nstruct Segment{\n Point p1,p2;\n Segment(Point p1 = Point(),Point p2 = Point()):p1(p1),p2(p2){}\n bool operator < (const Segment& s) const { return ( p2 == s.p2 ) ? p1 < s.p1 : p2 < s.p2; }\n bool operator == (const Segment& s) const { return ( s.p1 == p1 && s.p2 == p2 ) || ( s.p1 == p2 && s.p2 == p1 ); }\n\n};\n\ntypedef Point Vector;\ntypedef Segment Line;\ntypedef vector<Point> Polygon;\n\nostream& operator << (ostream& os,const Point& a){ os << \"(\" << a.x << \",\" << a.y << \")\"; }\n\nostream& operator << (ostream& os,const Segment& a){ os << \"( \" << a.p1 << \" , \" << a.p2 << \" )\"; }\n\ndouble dot(Point a,Point b){ return a.x*b.x + a.y*b.y; }\n\ndouble cross(Point a,Point b){ return a.x*b.y - a.y*b.x; }\n\ndouble norm(Point a){ return a.x*a.x+a.y*a.y; }\n\ndouble abs(Point a){ return sqrt(norm(a)); }\n\nint ccw(Point p0,Point p1,Point p2){\n Point a = p1-p0;\n Point b = p2-p0;\n if(cross(a,b) > EPS)return COUNTER_CLOCKWISE;\n if(cross(a,b) < -EPS)return CLOCKWISE;\n if(dot(a,b) < -EPS)return ONLINE_BACK;\n if(norm(a) < norm(b))return ONLINE_FRONT;\n return ON_SEGMENT;\n}\n\nbool intersectLL(Line l, Line m) {\n return abs(cross(l.p2-l.p1, m.p2-m.p1)) > EPS || // non-parallel\n abs(cross(l.p2-l.p1, m.p1-l.p1)) < EPS; // same line\n}\nbool intersectLS(Line l, Line s) {\n return cross(l.p2-l.p1, s.p1-l.p1)* // s[0] is left of l\n cross(l.p2-l.p1, s.p2-l.p1) < EPS; // s[1] is right of l\n}\nbool intersectLP(Line l,Point p) {\n return abs(cross(l.p2-p, l.p1-p)) < EPS;\n}\nbool intersectSS(Line s, Line t) {\n return ccw(s.p1,s.p2,t.p1)*ccw(s.p1,s.p2,t.p2) <= 0 &&\n ccw(t.p1,t.p2,s.p1)*ccw(t.p1,t.p2,s.p2) <= 0;\n}\nbool intersectSP(Line s, Point p) {\n return abs(s.p1-p)+abs(s.p2-p)-abs(s.p2-s.p1) < EPS; // triangle inequality\n}\n\nPoint projection(Line l,Point p) {\n double t = dot(p-l.p1, l.p1-l.p2) / norm(l.p1-l.p2);\n return l.p1 + (l.p1-l.p2)*t;\n}\nPoint reflection(Line l,Point p) {\n return p + (projection(l, p) - p) * 2;\n}\ndouble distanceLP(Line l, Point p) {\n return abs(p - projection(l, p));\n}\ndouble distanceLL(Line l, Line m) {\n return intersectLL(l, m) ? 0 : distanceLP(l, m.p1);\n}\n\ndouble distanceLS(Line l, Line s) {\n if (intersectLS(l, s)) return 0;\n return min(distanceLP(l, s.p1), distanceLP(l, s.p2));\n}\ndouble distanceSP(Line s, Point p) {\n Point r = projection(s, p);\n if (intersectSP(s, r)) return abs(r - p);\n return min(abs(s.p1 - p), abs(s.p2 - p));\n}\n\ndouble distanceSS(Line s, Line t) {\n if (intersectSS(s, t)) return 0;\n return min(min(distanceSP(s, t.p1), distanceSP(s, t.p2)),\n min(distanceSP(t, s.p1), distanceSP(t, s.p2)));\n}\n\nPoint crosspoint(Line l,Line m){\n double A = cross(l.p2-l.p1,m.p2-m.p1);\n double B = cross(l.p2-l.p1,l.p2-m.p1);\n if(abs(A) < EPS && abs(B) < EPS){\n vector<Point> vec;\n vec.push_back(l.p1),vec.push_back(l.p2),vec.push_back(m.p1),vec.push_back(m.p2);\n sort(vec.begin(),vec.end());\n assert(vec[1] == vec[2]); //???????????°??????????????????\n return vec[1];\n //return m.p1;\n }\n if(abs(A) < EPS)assert(false);\n return m.p1 + (m.p2-m.p1)*(B/A);\n}\n\n\n//cross product of pq and pr\ndouble cross3p(Point p,Point q,Point r) { return (r.x-q.x) * (p.y -q.y) - (r.y - q.y) * (p.x - q.x); }\n \n//returns true if point r is on the same line as the line pq\nbool collinear(Point p,Point q,Point r) { return fabs(cross3p(p,q,r)) < EPS; }\n \n//returns true if point t is on the left side of line pq\nbool ccwtest(Point p,Point q,Point r){\n return cross3p(p,q,r) > 0; //can be modified to accept collinear points\n}\n \nbool onSegment(Point p,Point q,Point r){\n return collinear(p,q,r) && equals(sqrt(pow(p.x-r.x,2)+pow(p.y-r.y,2)) + sqrt(pow(r.x-q.x,2) + pow(r.y-q.y,2) ),sqrt(pow(p.x-q.x,2)+pow(p.y-q.y,2)) ) ;\n}\n \n\ndouble angle(Point a,Point b,Point c) {\n double ux = b.x - a.x, uy = b.y - a.y;\n double vx = c.x - a.x, vy = c.y - a.y;\n return acos((ux*vx + uy*vy)/sqrt((ux*ux + uy*uy) * (vx*vx + vy*vy)));\n} \n \n//????§???¢poly?????????????????????????????????p????????¨?????????????????????????????? \nbool inPolygon(Polygon poly,Point p,bool flag=true){\n if((int)poly.size() == 0)return false;\n if( flag ) rep(i,poly.size()) if(onSegment(poly[i],poly[(i+1)%poly.size()],p))return true;\n double sum = 0;\n for(int i=0; i < (int)poly.size() ;i++) {\n if( equals(cross(poly[i]-p,poly[(i+1)%poly.size()]-p),0.0) ) continue; // ????????????????????¨angle???nan?????????sum???nan??????????????¬\n if( cross3p(p,poly[i],poly[(i+1)%poly.size()]) < 0 ) sum -= angle(p,poly[i],poly[(i+1)%poly.size()]);\n else sum += angle(p,poly[i],poly[(i+1)%poly.size()]);\n }\n // ?????????????????????????????????????????¨?????????????????§??\\????????????????????? \n const double eps = 1e-5;\n return (fabs(sum - 2*M_PI) < eps || fabs(sum + 2*M_PI) < eps);\n} \n\n\n\n\n\n\nenum { OUT, ON, IN };\nint contains(Polygon& P, Point& p) {\n bool in = false;\n for (int i = 0; i < P.size(); ++i) {\n Point a = P[i] - p, b = P[(i+1)%P.size()] - p;\n if (a.y > b.y) swap(a, b);\n if (a.y <= 0 && 0 < b.y)\n if (cross(a, b) < 0) in = !in;\n if (cross(a, b) == 0 && dot(a, b) <= 0) return ON;\n }\n return in ? IN : OUT;\n}\n// -------------------\n\nbool LT(double a,double b) { return !equals(a,b) && a < b; }\nbool LTE(double a,double b) { return equals(a,b) || a < b; }\n\nbool DEBUG = false;\n\nstruct Data {\n Point cur;\n double weight;\n int a,b;\n bool operator < ( const Data &data ) const { return LT(data.weight,weight); }\n};\n\nconst double DINF = 1e30;\nint N;\nPoint bomb;\nvector<Polygon> polies;\nvector<vector<double> > mindist;\nvector<vector<bool> > goals;\n\nbool isValidMove(Point src,Point dst){\n\n Segment ururu_beam = Segment(src,dst);\n Point mp = ( src + dst ) * 0.5; // $\n rep(i,N) {\n int M = polies[i].size();\n bool in = false;\n bool on = false;\n rep(j,M) {\n Segment seg = Segment(polies[i][j],polies[i][(j+1)%M]);\n\n Point a = polies[i][j] - mp, b = polies[i][(j+1)%M] - mp;\n if( a.y > b.y ) swap(a,b);\n if( a.y <= 0 && 0 < b.y ) if( cross(a,b) < 0 ) in = !in;\n if( cross(a,b) == 0 && dot(a,b) <= 0 ) on = true;\n\n // ????????????????????¨??????????????§????????°???????§??????¶?????§?????£????????£???????§????\n if( equals(cross(ururu_beam.p1-ururu_beam.p2,seg.p1-seg.p2),0.0) ) continue;\n seg = Segment(polies[i][j],polies[i][(j-1+M)%M]);\n if( equals(cross(ururu_beam.p1-ururu_beam.p2,seg.p1-seg.p2),0.0) ) continue;\n seg = Segment(polies[i][j],polies[i][(j+1)%M]);\n\n if( !intersectSS(ururu_beam,seg) ) continue;\n\n Point cp = crosspoint(ururu_beam,seg);\n\n //????????? src or dst ??§????????´???????????£???????§????\n if( cp == ururu_beam.p1 || cp == ururu_beam.p2 ) continue;\n\n if( cp == seg.p1 || cp == seg.p2 ) {\n\n\t// p0 -> p1 ->p2, p0 -> p1 -> p3 ???????????§???????????¨????????? ( ?????§continue??????????????? )\n\tPoint p0 = src;\n\tPoint p1 = cp;\n\tPoint p2 = polies[i][(j+1)%M];\n\tPoint p3 = polies[i][(j-1+M)%M];\n\tif( cp == p2 || cp == p3 ) continue;\n\tint ccw_res1 = ccw(p0,p1,p2);\n\tint ccw_res2 = ccw(p0,p1,p3);\n\tif( ccw_res1 != ccw_res2 ) return false;\n } else return false;\n }\n if( !( src == bomb || dst == bomb ) && !on && in ) return false;\n }\n\n\n /*\n if( !( src == bomb || dst == bomb ) ) {\n Point mp = ( src + dst ) / 2.0;\n //rep(i,N) if( inPolygon(polies[i],mp,false) ) return false;\n rep(i,N) if( contains(polies[i],mp) == IN ) return false;\n }\n */\n\n return true;\n}\n\ndouble temper;\nvoid dijkstra(){\n temper = DINF;\n priority_queue<Data> Q;\n Q.push((Data){(Point){0,0},0,-1,-1});\n while( !Q.empty() ){ \n Data data = Q.top(); Q.pop();\n if( data.a != -1 && LT(mindist[data.a][data.b],data.weight) ) continue;\n if( data.a != -1 && goals[data.a][data.b] ) {\n temper = data.weight;\n return;\n }\n rep(i,N) rep(j,polies[i].size()){\n Point next = polies[i][j];\n if( data.cur == next ) continue;\n if( !isValidMove(data.cur,next) ) continue;\n double next_weight = data.weight + abs(next-data.cur);\n if( LT(next_weight,mindist[i][j]) ) {\n\tmindist[i][j] = next_weight;\n\tQ.push((Data){next,next_weight,i,j});\n }\n }\n }\n}\n\n//???????????¶?´??????????ururu_beam??¨seg???????????§???????????¨?????????\nbool isGoal(Point p){\n Segment ururu_beam = Segment(bomb,p);\n rep(i,N){\n int M = polies[i].size();\n rep(j,M){\n Segment seg = Segment(polies[i][j],polies[i][(j+1)%M]);\n if( !intersectSS(ururu_beam,seg) ) continue;\n Point cp = crosspoint(ururu_beam,seg);\n\n if( !( cp == p ) ) return true;\n else if( cp == seg.p1 || cp == seg.p2 ) {\n\tint k = (cp==seg.p1?j:(j+1)%M);\n\tint c1 = ccw(bomb,p,polies[i][(k-1+M)%M]);\n\tint c2 = ccw(bomb,p,polies[i][(k+1)%M]);\n\tassert( !( p == polies[i][(k-1+M)%M]));\n\tassert( !( p == polies[i][(k+1)%M]));\n\tassert( c1 == COUNTER_CLOCKWISE || c1 == CLOCKWISE );\n\tassert( c2 == COUNTER_CLOCKWISE || c2 == CLOCKWISE );\n\tif( c1 == c2 ) return true;\n }\n }\n }\n return false;\n}\n\n//????????¨?????????????????§??????????????¨???????¨????????????????\nPoint getOnSegmentPoint(Point p){\n Segment ururu_beam = Segment(bomb,p);\n Vector e = (ururu_beam.p2-ururu_beam.p1) / abs(ururu_beam.p2-ururu_beam.p1);\n ururu_beam.p2 = bomb + e * 1000000;\n Point ret = Point(-DINF,-DINF);\n rep(i,N) {\n int M = polies[i].size();\n rep(j,M){\n Segment seg = Segment(polies[i][j],polies[i][(j+1)%M]);\n if( !intersectSS(ururu_beam,seg) ) continue;\n Point cp = crosspoint(ururu_beam,seg);\n if( cp == p ) continue;\n if( ret == Point(-DINF,-DINF) ) ret = cp;\n else if( LT(abs(cp-bomb),abs(ret-bomb)) ) ret = cp;\n }\n }\n return ret;\n}\n\nPoint vir;\nbool comp_dist(const Point &p,const Point &q) {\n return LT(abs(vir-p),abs(vir-q));\n}\n\nvoid compute(){\n\n\n if( isGoal(Point(0,0)) ) {\n printf(\"%.10f\\n\",0.0);\n return;\n }\n\n rep(i,N) rep(j,polies[i].size()) goals[i][j] = isGoal(polies[i][j]);\n\n // dijkstra ??§??????????????????????????¢????±???????\n dijkstra();\n\n\n //rep(i,N) rep(j,polies[i].size()) goals[i][j] = isGoal(polies[i][j]);\n //double answer = DINF;\n double answer = temper;\n rep(i,N) rep(j,polies[i].size()) if( goals[i][j] && LT(mindist[i][j],answer) ) {\n answer = mindist[i][j];\n }\n\n // ??´????????¨??????????£?????????????\n // ???????????????????????´??????????????´????????§????????°????°?????????´??°??????\n rep(i,N) {\n int M = polies[i].size();\n rep(j,M){\n Point p0 = bomb;\n Point p1 = polies[i][j];\n Point p2 = polies[i][(j+1)%M];\n Point p3 = polies[i][(j-1+M)%M];\n int ccw_res1 = ccw(p0,p1,p2);\n int ccw_res2 = ccw(p0,p1,p3);\n if( ( ccw_res1 == COUNTER_CLOCKWISE || ccw_res1 == CLOCKWISE ) && ( ccw_res2 == COUNTER_CLOCKWISE || ccw_res2 == CLOCKWISE ) && ccw_res1 != ccw_res2 ) {\n\tcontinue;\n }\n\n //?????????????§???¢????§?????????????????????¶?????????????????????\n //?????????\n Point p = getOnSegmentPoint(polies[i][j]);\n if( p.x != -DINF && p.y != -DINF ) {\n\t//if( !( LT(abs(p),answer) ) )\n\tif( isGoal(p) )\t{\n\t if( LT(abs(p),answer) && isValidMove(Point(0,0),p) ) {\n\t answer = min(answer,abs(p));\n\t } else {\n\t double mini = DINF;\n\t rep(k,N) rep(l,polies[k].size()) if( LT(mindist[k][l]+abs(polies[k][l]-p),min(answer,mini)) && isValidMove(polies[k][l],p) ) {\n\t mini = min(mini,mindist[k][l]+abs(polies[k][l]-p));\n\t }\n\t answer = min(answer,mini);\n\t }\n\t}\n }\n\n //?????´\n p = projection(Segment(bomb,polies[i][j]),Point(0,0));\n\n //if( !( LT(abs(p),answer) ) )\n if( isGoal(p) )\t{\n\tif( isValidMove(Point(0,0),p) ) {\n\t answer = min(answer,abs(p));\n\t} else {\n\t bool success = true;\n\t Segment ururu_beam = Segment(p,Point(0,0));\n\t rep(k,N) {\n\t int M2 = polies[k].size();\n\t rep(l,M2){\n\t Segment seg = Segment(polies[k][l],polies[k][(l+1)%M2]);\n\t if( !intersectSS(ururu_beam,seg) ) continue;\n\t if( equals(cross(ururu_beam.p1-ururu_beam.p2,seg.p1-seg.p2),0.0) ) continue;\n\t Point cp = crosspoint(ururu_beam,seg);\n\t if( cp == seg.p1 || cp == seg.p2 || cp == ururu_beam.p1 || cp == ururu_beam.p2 ) continue;\n\t success = false;\n\t break;\n\t }\n\t if( !success ) break;\n\t }\n\t if( success ) {\n\t double mini = DINF;\n\t rep(k,N) rep(l,polies[k].size()) if( goals[k][l] ) {\n\t mini = min(mini,mindist[k][l]+abs(ururu_beam.p1-ururu_beam.p2));\n\t }\n\t }\n\t}\n }\n }\n }\n\n printf(\"%.10f\\n\",answer);\n \n}\n\nint main(){\n while( scanf(\"%d\",&N) , N ) {\n polies.clear(), mindist.clear(); goals.clear();\n polies.resize(N), mindist.resize(N), goals.resize(N);\n //cin >> bomb.x >> bomb.y;\n scanf(\"%lf %lf\",&bomb.x,&bomb.y);\n rep(i,N) {\n int m;\n scanf(\"%d\",&m);\n polies[i].resize(m), mindist[i].resize(m,DINF), goals[i].resize(m,false);\n rep(j,m) scanf(\"%lf %lf\",&polies[i][j].x,&polies[i][j].y);//cin >> polies[i][j].x >> polies[i][j].y;\n }\n compute();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 3260, "memory_kb": 1348, "score_of_the_acc": -0.4378, "final_rank": 5 }, { "submission_id": "aoj_2246_1177688", "code_snippet": "#include<stdio.h>\n#include<math.h>\n#include<vector>\n#include<algorithm>\n#include<queue>\nusing namespace std;\nconst double EPS = 1e-7;\nconst double INF = 1e+10;\nconst double PI = acos(-1);\nint sig(double r) { return (r < -EPS) ? -1 : (r > +EPS) ? +1 : 0; }\ninline double ABS(double a){return max(a,-a);}\nstruct Pt {\n\tdouble x, y;\n\tPt() {}\n\tPt(double x, double y) : x(x), y(y) {}\n\tPt operator+(const Pt &a) const { return Pt(x + a.x, y + a.y); }\n\tPt operator-(const Pt &a) const { return Pt(x - a.x, y - a.y); }\n\tPt operator*(const Pt &a) const { return Pt(x * a.x - y * a.y, x * a.y + y * a.x); }\n\tPt operator-() const { return Pt(-x, -y); }\n\tPt operator*(const double &k) const { return Pt(x * k, y * k); }\n\tPt operator/(const double &k) const { return Pt(x / k, y / k); }\n\tbool operator==(const Pt &a)const{ return !sig(x-a.x)&&!sig(y-a.y);}\n\tdouble ABS() const { return sqrt(x * x + y * y); }\n\tdouble abs2() const { return x * x + y * y; }\n\tdouble arg() const { return atan2(y, x); }\n\tdouble dot(const Pt &a) const { return x * a.x + y * a.y; }\n\tdouble det(const Pt &a) const { return x * a.y - y * a.x; }\n};\ndouble tri(const Pt &a, const Pt &b, const Pt &c) { return (b - a).det(c - a); }\n\n\nint iSP(Pt a, Pt b, Pt c) {\n\tint s = sig((b - a).det(c - a));\n\tif (s) return s;\n\tif (sig((b - a).dot(c - a)) < 0) return -2; // c-a-b\n\tif (sig((a - b).dot(c - b)) < 0) return +2; // a-b-c\n\treturn 0;\n}\nint iLL(Pt a, Pt b, Pt c, Pt d) {\n\tif (sig((b - a).det(d - c))) return 1; // intersect\n\tif (sig((b - a).det(c - a))) return 0; // parallel\n\treturn -1; // correspond\n}\nbool iLS(Pt a, Pt b, Pt c, Pt d) {\n\treturn (sig(tri(a, b, c)) * sig(tri(a, b, d)) <= 0);\n}\nbool iSS(Pt a, Pt b, Pt c, Pt d) {\n\treturn (iSP(a, b, c) * iSP(a, b, d) <= 0 && iSP(c, d, a) * iSP(c, d, b) <= 0);\n}\nbool iSSstrict(Pt a, Pt b, Pt c, Pt d) {\n\treturn (sig(tri(a, b, c)) * sig(tri(a, b, d)) < 0 && sig(tri(c, d, a)) * sig(tri(c, d, b)) < 0);\n}\nPt pLL(Pt a, Pt b, Pt c, Pt d) {\n\tb = b - a; d = d - c; return a + b * (c - a).det(d) / b.det(d);\n}\nPt hLP(Pt a,Pt b,Pt c){\n\treturn pLL(a,b,c,c+(b-a)*Pt(0,1));\n}\ndouble dLP(Pt a, Pt b, Pt c) {\n\treturn ABS(tri(a, b, c)) / (b - a).ABS();\n}\ndouble dSP(Pt a, Pt b, Pt c) {\n\tif (sig((b - a).dot(c - a)) <= 0) return (c - a).ABS();\n\tif (sig((a - b).dot(c - b)) <= 0) return (c - b).ABS();\n\treturn ABS(tri(a, b, c)) / (b - a).ABS();\n}\ndouble dLL(Pt a, Pt b, Pt c, Pt d) {\n\treturn iLL(a, b, c, d) ? 0 : dLP(a, b, c);\n}\ndouble dLS(Pt a, Pt b, Pt c, Pt d) {\n\treturn iLS(a, b, c, d) ? 0 : min(dLP(a, b, c), dLP(a, b, d));\n}\ndouble dSS(Pt a, Pt b, Pt c, Pt d) {\n\treturn iSS(a, b, c, d) ? 0 : min(min(dSP(a, b, c), dSP(a, b, d)), min(dSP(c, d, a), dSP(c, d, b)));\n}\nint convexCut(int n, Pt p[], Pt a, Pt b, Pt q[]) {\n\tint m = 0, i;\n\tp[n] = p[0];\n\tfor (i = 0; i < n; ++i) {\n\t\tif (sig(tri(a, b, p[i])) >= 0) q[m++] = p[i];\n\t\tif (sig(tri(a, b, p[i])) * sig(tri(a, b, p[i + 1])) < 0) q[m++] = pLL(a, b, p[i], p[i + 1]);\n\t}\n\tq[m] = q[0];\n\treturn m;\n}\nint iCC(Pt a, double r, Pt b, double s) {\n\tdouble d = (b - a).ABS();\n\tif (sig(d) == 0 && sig(r - s) == 0) return -1; // correspond\n\tif (sig(r - s - d) > 0) return +2; // r > s\n\tif (sig(s - r - d) > 0) return -2; // s > r\n\treturn (sig(r + s - d) >= 0) ? 1 : 0;\n}\nbool iCS(Pt a, double r, Pt b, Pt c) {\n\treturn (sig(r - dSP(b, c, a)) >= 0 && sig(r - max((b - a).ABS(), (c - a).ABS())) <= 0);\n}\npair<Pt,Pt> pCC(Pt a, double r, Pt b, double s) {\n\tdouble d = (b - a).ABS();\n\tdouble x = (d * d + r * r - s * s) / (d * 2);\n\tPt e = (b - a) / d, w = e * Pt(0, 1) * sqrt(max(r * r - x * x, 0.0));\n\treturn make_pair(a + e * x - w, a + e * x + w);\n}\npair<Pt,Pt> pCL(Pt a, double r, Pt b, Pt c) {\n\tPt h = b + (c - b) * (c - b).dot(a - b) / (c - b).abs2(); // perp(b, c, a)\n\tdouble d = (h - a).ABS();\n\tdouble y = sqrt(max(r * r - d * d, 0.0));\n\tPt e = (c - b) / (c - b).ABS();\n\treturn make_pair(h - e * y, h + e * y);\n}\npair<Pt,Pt> tCP(Pt a, double r, Pt b) {\n\tdouble d2 = (b - a).abs2();\n\tdouble x = sqrt(max(d2 - r * r, 0.0));\n\tPt h = a + (b - a) * (r * r / d2);\n\tPt w = (b - a) * Pt(0, 1) * (x * r / d2);\n\treturn make_pair(h - w, h + w);\n}\ndouble aCC(Pt a, double r, Pt b, double s) {\n\tdouble d = (a - b).ABS();\n\tif (sig(r - s - d) >= 0) return s * s * PI;\n\tif (sig(s - r - d) >= 0) return r * r * PI;\n\tif (sig(r + s - d) <= 0) return 0;\n\tdouble x = (d * d + r * r - s * s) / (d * 2);\n\tdouble h = sqrt(r * r - x * x);\n\treturn r * r * atan2(h, x) + s * s * atan2(h, d - x) - d * h;\n}\n//inside: +1 on: 0 outside: -1\nint sGP(int n, Pt p[], Pt a) {\n\tint side = -1, i;\n\tp[n] = p[0];\n\tfor (i = 0; i < n; ++i) {\n\t\tPt p0 = p[i] - a, p1 = p[i + 1] - a;\n\t\tif (sig(p0.det(p1)) == 0 && sig(p0.dot(p1)) <= 0) return 0;\n\t\tif (p0.y > p1.y) swap(p0, p1);\n\t\tif (sig(p0.y) <= 0 && 0 < sig(p1.y) && sig(p0.det(p1)) > 0) side = -side;\n\t}\n\treturn side;\n}\nint sAP(Pt a, Pt b, Pt c) {\n\treturn sig(a.det(c)) - sig(b.det(c)) - sig(a.det(b));\n}\n\nint s_a[1100], s_b[1100], s_ab[1100];\nbool iGSstrict(vector<Pt> p, Pt a, Pt b) {\n\tint i;\n\tint n=p.size();\n\tp.push_back(p[0]);\n\tp.push_back(p[1]);\n//\tif (sGP(n, p, a) > 0 || sGP(n, p, b) > 0) return 1;\n\tfor (i = 0; i <= n; ++i) {\n\t\ts_a[i] = sig(tri(p[i], p[i + 1], a));\n\t\ts_b[i] = sig(tri(p[i], p[i + 1], b));\n\t\ts_ab[i] = sig(tri(a, b, p[i]));\n\t}\n\tfor (i = 0; i < n; ++i) {\n\t\tif (s_a[i] * s_b[i] < 0 && s_ab[i] * s_ab[i + 1] < 0) return 1;\n\t}\n\tfor (i = 0; i < n; ++i) {\n\t\tif (s_a[i] == 0 && s_b[i] > 0 && sig((a - p[i]).dot(a - p[i + 1])) < 0) return 1;\n\t\tif (s_b[i] == 0 && s_a[i] > 0 && sig((b - p[i]).dot(b - p[i + 1])) < 0) return 1;\n\t}\n\tfor (i = 0; i < n; ++i) if (s_ab[i + 1] == 0 && sig((p[i + 1] - a).dot(p[i + 1] - b)) <= 0) {\n\t\tif (!(p[i + 1] == a) && sAP(p[i + 2] - p[i + 1], p[i] - p[i + 1], a - p[i + 1]) > 0) return 1;\n\t\tif (!(p[i + 1] == b) && sAP(p[i + 2] - p[i + 1], p[i] - p[i + 1], b - p[i + 1]) > 0) return 1;\n\t}\n\treturn 0;\n}\nvector<Pt>p[110];\nvector<Pt>q;\nvector<pair<Pt,Pt> > edge;\ndouble ijk[110000];\nint v[110000];\nint main(){\n\tint a;\n\twhile(scanf(\"%d\",&a),a){\n\t\tdouble bx,by;scanf(\"%lf%lf\",&bx,&by);\n\t\tPt bm=Pt(bx,by);\n\t\tfor(int i=0;i<a;i++)p[i].clear();\n\t\tfor(int i=0;i<a;i++){\n\t\t\tint c;scanf(\"%d\",&c);\n\t\t\tfor(int j=0;j<c;j++){\n\t\t\t\tdouble ix,iy;\n\t\t\t\tscanf(\"%lf%lf\",&ix,&iy);\n\t\t\t\tp[i].push_back(Pt(ix,iy));\n\t\t\t}\n\t\t}\n\t\tbool zr=false;\n\t\tfor(int i=0;i<a;i++){\n\t\t\tfor(int j=0;j<p[i].size();j++){\n\t\t\t\tif(iSSstrict(p[i][j],p[i][(j+1)%p[i].size()],Pt(0,0),bm))zr=true;\n\t\t\t}\n\t\t}\n\t\tif(zr){\n\t\t\tprintf(\"%.12f\\n\",0.0);\n\t\t\tcontinue;\n\t\t}\n\t\tq.clear();\n\t\tedge.clear();\n\t\tq.push_back(Pt(0,0));\n\t\tfor(int i=0;i<a;i++){\n\t\t\tfor(int j=0;j<p[i].size();j++){\n\t\t\t\tPt r=bm+(p[i][j]-bm)*1000000;\n\t\t\t\tbool tc=false;\n\t\t\t\tfor(int k=0;k<a;k++)for(int l=0;l<p[k].size();l++){\n\t\t\t\t\tif(iSSstrict(p[k][l],p[k][(l+1)%p[k].size()],p[i][j],r)){\n\t\t\t\t\t\tr=pLL(p[k][l],p[k][(l+1)%p[k].size()],p[i][j],r);\n\t\t\t\t\t}\n\t\t\t\t\tif(iSSstrict(p[k][l],p[k][(l+1)%p[k].size()],bm,p[i][j])){\n\t\t\t\t\t\ttc=true;break;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif(tc)continue;\n\t\t\t\tq.push_back(p[i][j]);\n\t\t\t\tif(r.ABS()<100000)q.push_back(r);\n\t\t\t\tif(!iGSstrict(p[i],p[i][j],r))edge.push_back(make_pair(p[i][j],r));\n\t\t\t}\n\t\t}\n\t\t\n\t\tint n=q.size();\n\t\tfor(int i=0;i<n;i++)ijk[i]=999999999;\n\t\tfor(int i=0;i<n;i++)v[i]=0;\n\t\tijk[0]=0;\n\t\tfor(int i=0;i<n;i++){\n\t\t\tdouble best=99999999;\n\t\t\tint at=-1;\n\t\t\tfor(int j=0;j<n;j++){\n\t\t\t\tif(!v[j]&&best>ijk[j]){\n\t\t\t\t\tbest=ijk[j];\n\t\t\t\t\tat=j;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(!~at)break;\n\t\t\tv[at]=1;\n\t\t\tfor(int j=0;j<n;j++){\n\t\t\t\tif(v[j]||ijk[at]+(q[at]-q[j]).ABS()+EPS>ijk[j])continue;\n\t\t\t\tbool ok=true;\n\t\t\t\tfor(int k=0;k<a;k++){\n\t\t\t\t\tif(iGSstrict(p[k],q[at],q[j])){ok=false;break;}\n\t\t\t\t}\n\t\t\t\tif(ok){\n\t\t\t\t\tijk[j]=min(ijk[j],ijk[at]+(q[at]-q[j]).ABS());\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tdouble ret=999999999;\n\t\t//for(int i=0;i<edge.size();i++)printf(\"(%f %f), (%f %f)\\n\",edge[i].first.x,edge[i].first.y,edge[i].second.x,edge[i].second.y);\n\t\tfor(int i=0;i<n;i++){\n\t\t\tfor(int j=0;j<edge.size();j++){\n\t\t\t\tPt L=edge[j].first;\n\t\t\t\tPt R=edge[j].second;\n\t\t\t\tPt H=hLP(edge[j].first,edge[j].second,q[i]);\n\t\t\t\tbool ok;\n\t\t\t\tok=true;\n\t\t\t\tif(ret<EPS+ijk[i]+(q[i]-L).ABS())ok=false;\n\t\t\t\tfor(int k=0;ok&&k<a;k++)if(iGSstrict(p[k],q[i],L)){ok=false;break;}\n\t\t\t\tif(ok)ret=min(ret,ijk[i]+(q[i]-L).ABS());\n\t\t\t\tok=true;\n\t\t\t\tif(ret<EPS+ijk[i]+(q[i]-R).ABS())ok=false;\n\t\t\t\tfor(int k=0;ok&&k<a;k++)if(iGSstrict(p[k],q[i],R)){ok=false;break;}\n\t\t\t\tif(ok)ret=min(ret,ijk[i]+(q[i]-R).ABS());\n\t\t\t\tok=true;\n\t\t\t\tif(ret<EPS+ijk[i]+(q[i]-H).ABS())ok=false;\n\t\t\t\tfor(int k=0;ok&&k<a;k++)if(iGSstrict(p[k],q[i],H)){ok=false;break;}\n\t\t\t\tif(ok&&iSP(L,H,R)==2){\n\t\t\t\t\tret=min(ret,ijk[i]+(q[i]-H).ABS());\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tprintf(\"%.12f\\n\",ret);\n\t}\n}", "accuracy": 1, "time_ms": 1110, "memory_kb": 1196, "score_of_the_acc": -0.1167, "final_rank": 1 }, { "submission_id": "aoj_2246_1156397", "code_snippet": "#include <cstdio>\n#include <cmath>\n#include <cstring>\n#include <cstdlib>\n#include <climits>\n#include <ctime>\n#include <queue>\n#include <stack>\n#include <algorithm>\n#include <list>\n#include <vector>\n#include <set>\n#include <map>\n#include <iostream>\n#include <deque>\n#include <complex>\n#include <string>\n#include <iomanip>\n#include <sstream>\n#include <bitset>\n#include <valarray>\n#include <unordered_map>\n#include <iterator>\n#include <assert.h>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned int uint;\ntypedef unsigned char uchar;\ntypedef unsigned long long ull;\ntypedef pair<int, int> pii;\ntypedef pair<ll, ll> pll;\ntypedef vector<int> vi;\n\n#define REP(i,x) for(int i=0;i<(int)(x);i++)\n#define REPS(i,x) for(int i=1;i<=(int)(x);i++)\n#define RREP(i,x) for(int i=((int)(x)-1);i>=0;i--)\n#define RREPS(i,x) for(int i=((int)(x));i>0;i--)\n#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();i++)\n#define RFOR(i,c) for(__typeof((c).rbegin())i=(c).rbegin();i!=(c).rend();i++)\n#define ALL(container) (container).begin(), (container).end()\n#define RALL(container) (container).rbegin(), (container).rend()\n#define SZ(container) ((int)container.size())\n#define mp(a,b) make_pair(a, b)\n#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );\n\ntemplate<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }\ntemplate<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }\ntemplate<class T> ostream& operator<<(ostream &os, const vector<T> &t) {\nos<<\"[\"; FOR(it,t) {if(it!=t.begin()) os<<\",\"; os<<*it;} os<<\"]\"; return os;\n}\ntemplate<class T> ostream& operator<<(ostream &os, const set<T> &t) {\nos<<\"{\"; FOR(it,t) {if(it!=t.begin()) os<<\",\"; os<<*it;} os<<\"}\"; return os;\n}\ntemplate<class S, class T> ostream& operator<<(ostream &os, const pair<S,T> &t) { return os<<\"(\"<<t.first<<\",\"<<t.second<<\")\";}\ntemplate<class S, class T> pair<S,T> operator+(const pair<S,T> &s, const pair<S,T> &t){ return pair<S,T>(s.first+t.first, s.second+t.second);}\ntemplate<class S, class T> pair<S,T> operator-(const pair<S,T> &s, const pair<S,T> &t){ return pair<S,T>(s.first-t.first, s.second-t.second);}\n\nnamespace geom{\n#define X real()\n#define Y imag()\n#define at(i) ((*this)[i])\n#define SELF (*this)\n\tenum {TRUE = 1, FALSE = 0, BORDER = -1};\n\ttypedef int BOOL;\n\ttypedef long double R;\n\tconst R INF = 1e8;\n\tR EPS = 1e-6;\n\tconst R PI = 3.1415926535897932384626;\n\tinline int sig(const R &x) { return (abs(x) < EPS ? 0 : x > 0 ? 1 : -1); }\n\tinline BOOL less(const R &x, const R &y) {return sig(x-y) ? x < y : BORDER;}\n\ttypedef complex<R> P;\n\tinline R norm(const P &p){return p.X*p.X+p.Y*p.Y;}\n\tinline R inp(const P& a, const P& b){return (conj(a)*b).X;}\n\tinline R outp(const P& a, const P& b){return (conj(a)*b).Y;}\n\tinline P unit(const P& p){return p/abs(p);}\n\tinline P proj(const P &s, const P &t){return t*inp(s, t)/norm(t);}\n\tinline int ccw(const P &s, const P &t, const P &p, int adv=0){\n\t\tint res = sig(outp(t-s, p-s));\n\t\tif(res || !adv) return res;\n\t\tif(sig(inp(t-s, p-s)) < 0) return -2;\t// p-s-t\n\t\tif(sig(inp(s-t, p-t)) < 0) return 2;\t// s-t-p\n\t\treturn 0;\t\t\t\t\t\t\t\t// s-p-t\n\t}\n\t\n\t\n\tstruct L : public vector<P>{\t// line\n\t\tL(const P &p1, const P &p2){this->push_back(p1);this->push_back(p2);}\n\t\tL(){}\n\t\tP dir()const {return at(1) - at(0);}\n\t\tBOOL online(const P &p)const {return !sig(outp(p-at(0), dir()));}\n\t};\n\tstruct S : public L{\t// segment\n\t\tS(const P &p1, const P &p2):L(p1, p2){}\n\t\tS(){}\n\t\tBOOL online(const P &p)const {\n\t\t\tif(!sig(norm(p - at(0))) || !sig(norm(p - at(1)))) return BORDER;\n\t\t\t// テ・ツコツァテヲツィツ凖」ツ?ョテ、ツコツ古、ツケツ療」ツ?ィEPSテ」ツ?ョテ・ツキツョテ」ツ?古・ツ、ツァテ」ツ?催」ツ?凖」ツ?偲」ツ?ェテ」ツ??」ツつ暗」ツ??」ツ?ォテヲツウツィテヲツ??\n\t\t\treturn !sig(outp(p-at(0), dir())) && inp(p-at(0), dir()) > EPS && inp(p-at(1), -dir()) > -EPS;\n\t\t\t//return !sig(abs(at(0)-p) + abs(at(1) - p) - abs(at(0) - at(1)));\n\t\t}\n\t};\n\tP crosspoint(const L &l, const L &m);\n\tstruct G : public vector<P>{\n\t\tG(size_type size=0):vector(size){}\n\t\tS edge(int i)const {return S(at(i), at(i+1 == size() ? 0 : i+1));}\n\t};\n\n\tinline P proj(const P &s, const L &t){return t[0] + proj(s-t[0], t[1]-t[0]);}\n\tinline P reflect(const P &s, const L &t){return (R)2.*proj(s, t) - s;}\n\tinline S reflect(const S &s, const L &t){return S(reflect(s[0], t), reflect(s[1], t));}\n\tBOOL intersect(const S &s, const S &t){\n\t\tconst int p = ccw(t[0], t[1], s[0], 1) * ccw(t[0], t[1], s[1], 1);\n\t\tconst int q = ccw(s[0], s[1], t[0], 1) * ccw(s[0], s[1], t[1], 1);\n\t\treturn (p>0||q>0) ? FALSE : (!p||!q) ? BORDER : TRUE;\n\t}\n\tBOOL intersect(const S &s, const L &l){\n\t\tif(l.online(s[0]) || l.online(s[1])) return BORDER;\n\t\treturn (sig(outp(l.dir(), s[0]-l[0])) * sig(outp(l.dir(), s[1]-l[0])) <= 0);\n\t}\n\tR dist2(const L &l, const P &p){return norm(outp(l.dir(), p - l[0])) / norm(l.dir());}\n\tR dist2(const S &s, const P &p){\n\t\tif(inp(p-s[0], s.dir()) < EPS) return norm(p - s[0]);\n\t\tif(inp(p-s[1], -s.dir()) < EPS) return norm(p - s[1]);\n\t\treturn dist2((const L &)s, p);\n\t}\n\tR dist2(const S &s, const L &l){\n\t\treturn intersect(s, l) ? .0 : min(dist2(l, s[0]), dist2(l, s[1]));\n\t}\n\tR dist2(const S &s, const S &t){\n\t\treturn intersect(s, t) ? .0 : min(min(dist2(s, t[0]), dist2(t, s[0])), \n\t\t\t\t\t\t\t\t\t \t min(dist2(s, t[1]), dist2(t, s[1])));\n\t}\n\ttemplate <class T> R dist2(const G &g, const T& t){ // todo: テ・ツ??ゥツδィテ」ツ?ォテ・ツョツ古・ツ?ィテ」ツ?ォテ・ツ青ォテ」ツ?セテ」ツつ古」ツつ凝・ツ?エテ・ツ青?\n\t\tR res = INF;\n\t\tREP(i, g.size()) res = min(res, dist2(g.edge(i), t));\n\t\treturn res;\n\t}\n\ttemplate<class S, class T> R dist(const S& s, const T& t){return sqrt(dist2(s, t));}\n\tinline P crosspoint(const L &l, const L &m){\n\t\tR A = outp(l.dir(), m.dir()), B = outp(l.dir(), l[1] - m[0]);\n\t\tif(!sig(abs(A)) && !sig(abs(B))) return m[0]; // same line\n\t\tif(abs(A) < EPS) assert(false); // !!!PRECONDITION NOT SATISFIED!!!\n\t\treturn m[0] + B / A * (m[1] - m[0]);\n\t}\n\n#undef SELF\n#undef at\n}\n\nusing namespace geom;\n\nint f = 0;\nnamespace std{\n\tbool operator<(const P &a, const P &b){return sig(a.X-b.X) ? a.X < b.X : a.Y+EPS < b.Y;}\n\tbool operator==(const P &a, const P &b){return abs(a-b) < EPS;}\n\tistream& operator>>(istream &is, P &p){R x,y;is>>x>>y;p=P(x, y);return is;}\n}\nint n;\n\nint main(int argc, char *argv[]){\n\tios::sync_with_stdio(false);\n\tint T = 1;\n\twhile(cin >> n, n){\n\t\tP p;\n\t\tcin >> p;\n\t\tvector<G> g(n);\n\t\tREP(i, n){\n\t\t\tint x;\n\t\t\tcin >> x;\n\t\t\tg[i].resize(x);\n\t\t\tREP(j, x){\n\t\t\t\tcin >> g[i][j];\n\t\t\t\tg[i][j] -= p;\n\t\t\t}\n\t\t}\n\t\tp = -p;\n\t\tif([&](){\n\t\t\tREP(i, n)REP(j, g[i].size()){\n\t\t\t\tif(intersect(g[i].edge(j), S(0, p)) == TRUE) return 1;\n\t\t\t}\n\t\t\treturn 0;\n\t\t}()){\n\t\t\tprintf(\"%.10f\\n\", .0);\n\t\t\tcontinue;\n\t\t}\n\t\t\n\t\tvector<S> acc;\n\t\tREP(i, n)REP(j, g[i].size()){\n\t\t\tL s(0, unit(g[i][j]));\n\t\t\tif(ccw(0, s[1], g[i][(j+1)%g[i].size()]) != ccw(0, s[1], g[i][(j-1+g[i].size())%g[i].size()])) continue;\n\t\t\tR from = abs(g[i][j]), to = 20000;\n\t\t\tREP(ii, n)REP(jj, g[ii].size()){\n\t\t\t\tS t = g[ii].edge(jj);\n\t\t\t\tif(!sig(outp(s.dir(), t.dir())) || intersect(t, s) != TRUE) continue;\n\t\t\t\tP p = crosspoint(s, t);\n\t\t\t\tif(inp(s.dir(), p) > 0){\n\t\t\t\t\tto = min(to, abs(p));\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(from < to) acc.emplace_back(s[1]*from, s[1]*to);\n\t\t}\n\t\tauto cango = [&](const P &s, const P &t){\n\t\t\tconst S root(s, t);\n\t\t\tREP(i, n)REP(j, g[i].size()){\n\t\t\t\tconst S edge = g[i].edge(j);\n\t\t\t\tif(sig(outp(root.dir(), edge.dir())) && intersect(root, edge) == TRUE) return 0;\n\t\t\t}\n\t\t\treturn 1;\n\t\t};\n\t\t\n\t\tstruct State{\n\t\t\tint x, y;\n\t\t\tR cost;\n\t\t\tState (int x, int y, R cost)\n\t\t\t\t:x(x), y(y), cost(cost){}\n\t\t\tbool operator<(const State &opp) const{\n\t\t\t\treturn cost > opp.cost;\n\t\t\t}\n\t\t};\n\t\tvector<vector<R>> visited(n, vector<R>(101, INF));\n\t\tpriority_queue<State> pq;\n\t\tpq.emplace(-1, -1, 0);\n\t\tR ans = INF;\n\t\twhile(!pq.empty()){\n\t\t\tState s = pq.top(); pq.pop();\n\t\t\tP q = (s.x == -1) ? p : g[s.x][s.y];\n\t\t\tif(s.cost > ans) break;\n\t\t\tif(s.x >= 0 && visited[s.x][s.y] < s.cost - EPS) continue;\n\t\t\tFOR(it, acc){\n\t\t\t\tP pr = proj(q, *it);\n\t\t\t\tif(inp(pr - (*it)[0], it->dir()) < EPS) pr = (*it)[0];\n\t\t\t\telse if(inp(pr - (*it)[1], -it->dir()) < EPS) pr = (*it)[1];\n\t\t\t\tif(s.cost + abs(q - pr) < ans - EPS && cango(q, pr)){\n\t\t\t\t\tans = s.cost + abs(q - pr);\n\t\t\t\t}\n\t\t\t}\n\t\t\tREP(i, n)REP(j, g[i].size()){\n\t\t\t\tR c = s.cost + abs(g[i][j] - q);\n\t\t\t\tif(c+EPS > visited[i][j]) continue;\n\t\t\t\tif(cango(q, g[i][j])){\n\t\t\t\t\tvisited[i][j] = c;\n\t\t\t\t\tpq.emplace(i, j, c);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tprintf(\"%.10f\\n\", (double)ans);\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 940, "memory_kb": 1488, "score_of_the_acc": -0.1364, "final_rank": 2 }, { "submission_id": "aoj_2246_1156391", "code_snippet": "#include <cstdio>\n#include <cmath>\n#include <cstring>\n#include <cstdlib>\n#include <climits>\n#include <ctime>\n#include <queue>\n#include <stack>\n#include <algorithm>\n#include <list>\n#include <vector>\n#include <set>\n#include <map>\n#include <iostream>\n#include <deque>\n#include <complex>\n#include <string>\n#include <iomanip>\n#include <sstream>\n#include <bitset>\n#include <valarray>\n#include <unordered_map>\n#include <iterator>\n#include <assert.h>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned int uint;\ntypedef unsigned char uchar;\ntypedef unsigned long long ull;\ntypedef pair<int, int> pii;\ntypedef pair<ll, ll> pll;\ntypedef vector<int> vi;\n\n#define REP(i,x) for(int i=0;i<(int)(x);i++)\n#define REPS(i,x) for(int i=1;i<=(int)(x);i++)\n#define RREP(i,x) for(int i=((int)(x)-1);i>=0;i--)\n#define RREPS(i,x) for(int i=((int)(x));i>0;i--)\n#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();i++)\n#define RFOR(i,c) for(__typeof((c).rbegin())i=(c).rbegin();i!=(c).rend();i++)\n#define ALL(container) (container).begin(), (container).end()\n#define RALL(container) (container).rbegin(), (container).rend()\n#define SZ(container) ((int)container.size())\n#define mp(a,b) make_pair(a, b)\n#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );\n\ntemplate<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }\ntemplate<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }\ntemplate<class T> ostream& operator<<(ostream &os, const vector<T> &t) {\nos<<\"[\"; FOR(it,t) {if(it!=t.begin()) os<<\",\"; os<<*it;} os<<\"]\"; return os;\n}\ntemplate<class T> ostream& operator<<(ostream &os, const set<T> &t) {\nos<<\"{\"; FOR(it,t) {if(it!=t.begin()) os<<\",\"; os<<*it;} os<<\"}\"; return os;\n}\ntemplate<class S, class T> ostream& operator<<(ostream &os, const pair<S,T> &t) { return os<<\"(\"<<t.first<<\",\"<<t.second<<\")\";}\ntemplate<class S, class T> pair<S,T> operator+(const pair<S,T> &s, const pair<S,T> &t){ return pair<S,T>(s.first+t.first, s.second+t.second);}\ntemplate<class S, class T> pair<S,T> operator-(const pair<S,T> &s, const pair<S,T> &t){ return pair<S,T>(s.first-t.first, s.second-t.second);}\n\nnamespace geom{\n#define X real()\n#define Y imag()\n#define at(i) ((*this)[i])\n#define SELF (*this)\n\tenum {TRUE = 1, FALSE = 0, BORDER = -1};\n\ttypedef int BOOL;\n\ttypedef long double R;\n\tconst R INF = 1e8;\n\tR EPS = 1e-6;\n\tconst R PI = 3.1415926535897932384626;\n\tinline int sig(const R &x) { return (abs(x) < EPS ? 0 : x > 0 ? 1 : -1); }\n\tinline BOOL less(const R &x, const R &y) {return sig(x-y) ? x < y : BORDER;}\n\ttypedef complex<R> P;\n\tinline R norm(const P &p){return p.X*p.X+p.Y*p.Y;}\n\tinline R inp(const P& a, const P& b){return (conj(a)*b).X;}\n\tinline R outp(const P& a, const P& b){return (conj(a)*b).Y;}\n\tinline P unit(const P& p){return p/abs(p);}\n\tinline P proj(const P &s, const P &t){return t*inp(s, t)/norm(t);}\n\tinline int ccw(const P &s, const P &t, const P &p, int adv=0){\n\t\tint res = sig(outp(t-s, p-s));\n\t\tif(res || !adv) return res;\n\t\tif(sig(inp(t-s, p-s)) < 0) return -2;\t// p-s-t\n\t\tif(sig(inp(s-t, p-t)) < 0) return 2;\t// s-t-p\n\t\treturn 0;\t\t\t\t\t\t\t\t// s-p-t\n\t}\n\t\n\t\n\tstruct L : public vector<P>{\t// line\n\t\tL(const P &p1, const P &p2){this->push_back(p1);this->push_back(p2);}\n\t\tL(){}\n\t\tP dir()const {return at(1) - at(0);}\n\t\tBOOL online(const P &p)const {return !sig(outp(p-at(0), dir()));}\n\t};\n\tstruct S : public L{\t// segment\n\t\tS(const P &p1, const P &p2):L(p1, p2){}\n\t\tS(){}\n\t\tBOOL online(const P &p)const {\n\t\t\tif(!sig(norm(p - at(0))) || !sig(norm(p - at(1)))) return BORDER;\n\t\t\t// 座標の二乗とEPSの差が大きすぎないように注意\n\t\t\treturn !sig(outp(p-at(0), dir())) && inp(p-at(0), dir()) > EPS && inp(p-at(1), -dir()) > -EPS;\n\t\t\t//return !sig(abs(at(0)-p) + abs(at(1) - p) - abs(at(0) - at(1)));\n\t\t}\n\t};\n\tP crosspoint(const L &l, const L &m);\n\tstruct G : public vector<P>{\n\t\tG(size_type size=0):vector(size){}\n\t\tS edge(int i)const {return S(at(i), at(i+1 == size() ? 0 : i+1));}\n\t};\n\n\tinline P proj(const P &s, const L &t){return t[0] + proj(s-t[0], t[1]-t[0]);}\n\tinline P reflect(const P &s, const L &t){return (R)2.*proj(s, t) - s;}\n\tinline S reflect(const S &s, const L &t){return S(reflect(s[0], t), reflect(s[1], t));}\n\tBOOL intersect(const S &s, const S &t){\n\t\tconst int p = ccw(t[0], t[1], s[0], 1) * ccw(t[0], t[1], s[1], 1);\n\t\tconst int q = ccw(s[0], s[1], t[0], 1) * ccw(s[0], s[1], t[1], 1);\n\t\treturn (p>0||q>0) ? FALSE : (!p||!q) ? BORDER : TRUE;\n\t}\n\tBOOL intersect(const S &s, const L &l){\n\t\tif(l.online(s[0]) || l.online(s[1])) return BORDER;\n\t\treturn (sig(outp(l.dir(), s[0]-l[0])) * sig(outp(l.dir(), s[1]-l[0])) <= 0);\n\t}\n\tR dist2(const L &l, const P &p){return norm(outp(l.dir(), p - l[0])) / norm(l.dir());}\n\tR dist2(const S &s, const P &p){\n\t\tif(inp(p-s[0], s.dir()) < EPS) return norm(p - s[0]);\n\t\tif(inp(p-s[1], -s.dir()) < EPS) return norm(p - s[1]);\n\t\treturn dist2((const L &)s, p);\n\t}\n\tR dist2(const S &s, const L &l){\n\t\treturn intersect(s, l) ? .0 : min(dist2(l, s[0]), dist2(l, s[1]));\n\t}\n\tR dist2(const S &s, const S &t){\n\t\treturn intersect(s, t) ? .0 : min(min(dist2(s, t[0]), dist2(t, s[0])), \n\t\t\t\t\t\t\t\t\t \t min(dist2(s, t[1]), dist2(t, s[1])));\n\t}\n\ttemplate <class T> R dist2(const G &g, const T& t){ // todo: 内部に完全に含まれる場合\n\t\tR res = INF;\n\t\tREP(i, g.size()) res = min(res, dist2(g.edge(i), t));\n\t\treturn res;\n\t}\n\ttemplate<class S, class T> R dist(const S& s, const T& t){return sqrt(dist2(s, t));}\n\tinline P crosspoint(const L &l, const L &m){\n\t\tR A = outp(l.dir(), m.dir()), B = outp(l.dir(), l[1] - m[0]);\n\t\tif(!sig(abs(A)) && !sig(abs(B))) return m[0]; // same line\n\t\tif(abs(A) < EPS) assert(false); // !!!PRECONDITION NOT SATISFIED!!!\n\t\treturn m[0] + B / A * (m[1] - m[0]);\n\t}\n\n#undef SELF\n#undef at\n}\n\nusing namespace geom;\n\nint f = 0;\nnamespace std{\n\tbool operator<(const P &a, const P &b){return sig(a.X-b.X) ? a.X < b.X : a.Y+EPS < b.Y;}\n\tbool operator==(const P &a, const P &b){return abs(a-b) < EPS;}\n\tistream& operator>>(istream &is, P &p){R x,y;is>>x>>y;p=P(x, y);return is;}\n}\nint n;\n\nint main(int argc, char *argv[]){\n\tios::sync_with_stdio(false);\n\tint T = 1;\n\twhile(cin >> n, n){\n\t\tP p;\n\t\tcin >> p;\n\t\tvector<G> g(n);\n\t\tREP(i, n){\n\t\t\tint x;\n\t\t\tcin >> x;\n\t\t\tg[i].resize(x);\n\t\t\tREP(j, x){\n\t\t\t\tcin >> g[i][j];\n\t\t\t\tg[i][j] -= p;\n\t\t\t}\n\t\t}\n\t\tp = -p;\n\t\tif([&](){\n\t\t\tREP(i, n)REP(j, g[i].size()){\n\t\t\t\tif(intersect(g[i].edge(j), S(0, p)) == TRUE) return 1;\n\t\t\t}\n\t\t\treturn 0;\n\t\t}()){\n\t\t\tprintf(\"%.10f\\n\", .0);\n\t\t\tcontinue;\n\t\t}\n\t\t\n\t\tvector<S> acc;\n\t\tREP(i, n)REP(j, g[i].size()){\n\t\t\tL s(0, unit(g[i][j]));\n\t\t\tif(ccw(0, s[1], g[i][(j+1)%g[i].size()]) != ccw(0, s[1], g[i][(j-1+g[i].size())%g[i].size()])) continue;\n\t\t\tR from = abs(g[i][j]), to = 20000;\n\t\t\tREP(ii, n)REP(jj, g[ii].size()){\n\t\t\t\tS t = g[ii].edge(jj);\n\t\t\t\tif(!sig(outp(s.dir(), t.dir())) || intersect(t, s) != TRUE) continue;\n\t\t\t\tP p = crosspoint(s, t);\n\t\t\t\tif(inp(s.dir(), p) > 0){\n\t\t\t\t\tto = min(to, abs(p));\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(from < to) acc.emplace_back(s[1]*from, s[1]*to);\n\t\t}\n\t\tauto cango = [&](const P &s, const P &t){\n\t\t\tconst S root(s, t);\n\t\t\tREP(i, n)REP(j, g[i].size()){\n\t\t\t\tconst S edge = g[i].edge(j);\n\t\t\t\tif(sig(outp(root.dir(), edge.dir())) && intersect(root, edge) == TRUE) return 0;\n\t\t\t}\n\t\t\treturn 1;\n\t\t};\n\t\t\n\t\tstruct State{\n\t\t\tint x, y;\n\t\t\tR cost;\n\t\t\tState (int x, int y, R cost)\n\t\t\t\t:x(x), y(y), cost(cost){}\n\t\t\tbool operator<(const State &opp) const{\n\t\t\t\treturn cost > opp.cost;\n\t\t\t}\n\t\t};\n\t\tvector<vector<R>> visited(n, vector<R>(101, INF));\n\t\tpriority_queue<State> pq;\n\t\tpq.emplace(-1, -1, 0);\n\t\tR ans = INF;\n\t\twhile(!pq.empty()){\n\t\t\tState s = pq.top(); pq.pop();\n\t\t\tP q = (s.x == -1) ? p : g[s.x][s.y];\n\t\t\tif(s.cost > ans) break;\n\t\t\tif(s.x >= 0 && visited[s.x][s.y] < s.cost - EPS) continue;\n\t\t\tFOR(it, acc){\n\t\t\t\tP pr = proj(q, *it);\n\t\t\t\tif(inp(pr - (*it)[0], it->dir()) < EPS) pr = (*it)[0];\n\t\t\t\telse if(inp(pr - (*it)[1], -it->dir()) < EPS) pr = (*it)[1];\n\t\t\t\tif(s.cost + abs(q - pr) < ans - EPS && cango(q, pr)){\n\t\t\t\t\tans = s.cost + abs(q - pr);\n\t\t\t\t}\n\t\t\t}\n\t\t\tREP(i, n)REP(j, g[i].size()){\n\t\t\t\tR c = s.cost + abs(g[i][j] - q);\n\t\t\t\tif(c+EPS > visited[i][j]) continue;\n\t\t\t\tif(cango(q, g[i][j])){\n\t\t\t\t\tvisited[i][j] = c;\n\t\t\t\t\tpq.emplace(i, j, c);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tprintf(\"%.10f\\n\", (double)ans);\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 940, "memory_kb": 1488, "score_of_the_acc": -0.1364, "final_rank": 2 }, { "submission_id": "aoj_2246_1156383", "code_snippet": "#include <cstdio>\n#include <cmath>\n#include <cstring>\n#include <cstdlib>\n#include <climits>\n#include <ctime>\n#include <queue>\n#include <stack>\n#include <algorithm>\n#include <list>\n#include <vector>\n#include <set>\n#include <map>\n#include <iostream>\n#include <deque>\n#include <complex>\n#include <string>\n#include <iomanip>\n#include <sstream>\n#include <bitset>\n#include <valarray>\n#include <unordered_map>\n#include <iterator>\n#include <assert.h>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned int uint;\ntypedef unsigned char uchar;\ntypedef unsigned long long ull;\ntypedef pair<int, int> pii;\ntypedef pair<ll, ll> pll;\ntypedef vector<int> vi;\n\n#define REP(i,x) for(int i=0;i<(int)(x);i++)\n#define REPS(i,x) for(int i=1;i<=(int)(x);i++)\n#define RREP(i,x) for(int i=((int)(x)-1);i>=0;i--)\n#define RREPS(i,x) for(int i=((int)(x));i>0;i--)\n#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();i++)\n#define RFOR(i,c) for(__typeof((c).rbegin())i=(c).rbegin();i!=(c).rend();i++)\n#define ALL(container) (container).begin(), (container).end()\n#define RALL(container) (container).rbegin(), (container).rend()\n#define SZ(container) ((int)container.size())\n#define mp(a,b) make_pair(a, b)\n#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );\n\ntemplate<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }\ntemplate<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }\ntemplate<class T> ostream& operator<<(ostream &os, const vector<T> &t) {\nos<<\"[\"; FOR(it,t) {if(it!=t.begin()) os<<\",\"; os<<*it;} os<<\"]\"; return os;\n}\ntemplate<class T> ostream& operator<<(ostream &os, const set<T> &t) {\nos<<\"{\"; FOR(it,t) {if(it!=t.begin()) os<<\",\"; os<<*it;} os<<\"}\"; return os;\n}\ntemplate<class S, class T> ostream& operator<<(ostream &os, const pair<S,T> &t) { return os<<\"(\"<<t.first<<\",\"<<t.second<<\")\";}\ntemplate<class S, class T> pair<S,T> operator+(const pair<S,T> &s, const pair<S,T> &t){ return pair<S,T>(s.first+t.first, s.second+t.second);}\ntemplate<class S, class T> pair<S,T> operator-(const pair<S,T> &s, const pair<S,T> &t){ return pair<S,T>(s.first-t.first, s.second-t.second);}\n\nnamespace geom{\n#define X real()\n#define Y imag()\n#define at(i) ((*this)[i])\n#define SELF (*this)\n\tenum {TRUE = 1, FALSE = 0, BORDER = -1};\n\ttypedef int BOOL;\n\ttypedef long double R;\n\tconst R INF = 1e8;\n\tR EPS = 1e-6;\n\tconst R PI = 3.1415926535897932384626;\n\tinline int sig(const R &x) { return (abs(x) < EPS ? 0 : x > 0 ? 1 : -1); }\n\tinline BOOL less(const R &x, const R &y) {return sig(x-y) ? x < y : BORDER;}\n\ttypedef complex<R> P;\n\tinline R norm(const P &p){return p.X*p.X+p.Y*p.Y;}\n\tinline R inp(const P& a, const P& b){return (conj(a)*b).X;}\n\tinline R outp(const P& a, const P& b){return (conj(a)*b).Y;}\n\tinline P unit(const P& p){return p/abs(p);}\n\tinline P proj(const P &s, const P &t){return t*inp(s, t)/norm(t);}\n\tinline int ccw(const P &s, const P &t, const P &p, int adv=0){\n\t\tint res = sig(outp(t-s, p-s));\n\t\tif(res || !adv) return res;\n\t\tif(sig(inp(t-s, p-s)) < 0) return -2;\t// p-s-t\n\t\tif(sig(inp(s-t, p-t)) < 0) return 2;\t// s-t-p\n\t\treturn 0;\t\t\t\t\t\t\t\t// s-p-t\n\t}\n\t\n\t\n\tstruct L : public vector<P>{\t// line\n\t\tL(const P &p1, const P &p2){this->push_back(p1);this->push_back(p2);}\n\t\tL(){}\n\t\tP dir()const {return at(1) - at(0);}\n\t\tBOOL online(const P &p)const {return !sig(outp(p-at(0), dir()));}\n\t};\n\tstruct S : public L{\t// segment\n\t\tS(const P &p1, const P &p2):L(p1, p2){}\n\t\tS(){}\n\t\tBOOL online(const P &p)const {\n\t\t\tif(!sig(norm(p - at(0))) || !sig(norm(p - at(1)))) return BORDER;\n\t\t\t// 座標の二乗とEPSの差が大きすぎないように注意\n\t\t\treturn !sig(outp(p-at(0), dir())) && inp(p-at(0), dir()) > EPS && inp(p-at(1), -dir()) > -EPS;\n\t\t\t//return !sig(abs(at(0)-p) + abs(at(1) - p) - abs(at(0) - at(1)));\n\t\t}\n\t};\n\tstruct C : public P{\n\t\tC(){}\n\t\tC(const P& p, const R r):P(p), r(r){}\n\t\tR r;\n\t\tBOOL inside(const P& p)const { return less(norm(p-SELF), r*r);}\n\t};\n\tstruct F : public C{\n\t\tR s, t;\n\t\tF(const C &c, R ss, R tt):C(c), s(ss), t(tt){\n\t\t\tif(PI < s) s -= 2*PI;\n\t\t\tif(PI < t) t -= 2*PI;\n\t\t}\n\t\tBOOL inside(const P& p)const {\n\t\t\tP v = p - SELF;\n\t\t\tif(!sig(norm(v))) return BORDER;\n\t\t\tR a = arg(v);\n\t\t\tif(t < s){\n\t\t\t\tif((!less(s, a) && !less(a, t)) || !less(norm(v), r*r)) return FALSE;\n\t\t\t\treturn less(s, a) | less(a, t) | less(norm(v), r*r);\n\t\t\t}else{\n\t\t\t\tif(!less(s, a) || !less(a, t) || !less(norm(v), r*r)) return FALSE;\n\t\t\t\treturn less(s, a) | less(a, t) | less(norm(v), r*r);\n\t\t\t}\n\t\t}\n\t};\n\tP crosspoint(const L &l, const L &m);\n\tstruct G : public vector<P>{\n\t\tG(size_type size=0):vector(size){}\n\t\tS edge(int i)const {return S(at(i), at(i+1 == size() ? 0 : i+1));}\n\t\tBOOL contains(const P &p)const {\n\t\t\tR sum = .0;\n\t\t\tREP(i, size()){\n\t\t\t\tif(S(at(i), at((i+1)%size())).online(p)) return BORDER;\t// online\n\t\t\t\tsum += arg((at(i) - p) / (at((i+1)%size()) - p));\n\t\t\t}\n\t\t\treturn !!sig(sum);\n\t\t}\n\t\tR area()const {\n\t\t\tR sum = 0;\n\t\t\tREP(i, size()) sum += outp(at(i), at((i+1)%size()));\n\t\t\treturn abs(sum / 2.);\n\t\t}\n\t\tP gp()const {\n\t\t\tP r(.0, .0);\n\t\t\tREP(i, size()){\n\t\t\t\tconst S &s = edge(i);\n\t\t\t\tr += (s[0]+s[1])*outp(s[0], s[1]);\n\t\t\t}\n\t\t\treturn r / (6*area());\n\t\t}\n\t\tG convex_hull(bool online = false) {\n\t\t\tif(size() < 2) return *this;\n\t\t\tsort(ALL(*this));\n\t\t\tG r;\n\t\t\tr.resize((int)size()*2);\n\t\t\tint k=0;\n\t\t\tfor(int i=0;i<size();r[k++]=at(i++))\n\t\t\t\twhile(k>1 && ccw(r[k-2], r[k-1], at(i)) < 1-online) k--;\n\t\t\tint t = k;\n\t\t\tfor(int i=(int)size()-1;i>=0;r[k++]=at(i--))\n\t\t\t\twhile(k>t && ccw(r[k-2], r[k-1], at(i)) < 1-online) k--;\n\t\t\tr.resize(k-1);\n\t\t\treturn r;\n\t\t}\n\t\tG cut(const L &l)const {\n\t\t\tG g;\n\t\t\tREP(i, size()){\n\t\t\t\tconst S &s = edge(i);\n\t\t\t\tif(ccw(l[0], l[1], s[0], 0) >= 0) g.push_back(s[0]);\n\t\t\t\tif(ccw(l[0], l[1], s[0], 0) * ccw(l[0], l[1], s[1], 0) < 0)\n\t\t\t\t\tg.push_back(crosspoint(s, l));\n\t\t\t}\n\t\t\treturn g;\n\t\t}\n\t\tG Voronoi(const vector<P> &p, const int t)const {\n\t\t\tG g = *this;\n\t\t\tREP(i, p.size())if(i!=t){\n\t\t\t\tconst P m = (p[t]+p[i])*(R)0.5;\n\t\t\t\tg = g.cut(L(m, m+(p[i]-p[t])*P(0, 1)));\n\t\t\t}\n\t\t\treturn g;\n\t\t}\n\t};\n\n\tinline P proj(const P &s, const L &t){return t[0] + proj(s-t[0], t[1]-t[0]);}\n\tinline P reflect(const P &s, const L &t){return (R)2.*proj(s, t) - s;}\n\tinline S reflect(const S &s, const L &t){return S(reflect(s[0], t), reflect(s[1], t));}\n\tBOOL intersect(const S &s, const S &t){\n\t\tconst int p = ccw(t[0], t[1], s[0], 1) * ccw(t[0], t[1], s[1], 1);\n\t\tconst int q = ccw(s[0], s[1], t[0], 1) * ccw(s[0], s[1], t[1], 1);\n\t\treturn (p>0||q>0) ? FALSE : (!p||!q) ? BORDER : TRUE;\n\t}\n\tBOOL intersect(const S &s, const L &l){\n\t\tif(l.online(s[0]) || l.online(s[1])) return BORDER;\n\t\treturn (sig(outp(l.dir(), s[0]-l[0])) * sig(outp(l.dir(), s[1]-l[0])) <= 0);\n\t}\n\tR dist2(const L &l, const P &p){return norm(outp(l.dir(), p - l[0])) / norm(l.dir());}\n\tR dist2(const S &s, const P &p){\n\t\tif(inp(p-s[0], s.dir()) < EPS) return norm(p - s[0]);\n\t\tif(inp(p-s[1], -s.dir()) < EPS) return norm(p - s[1]);\n\t\treturn dist2((const L &)s, p);\n\t}\n\tR dist2(const S &s, const L &l){\n\t\treturn intersect(s, l) ? .0 : min(dist2(l, s[0]), dist2(l, s[1]));\n\t}\n\tR dist2(const S &s, const S &t){\n\t\treturn intersect(s, t) ? .0 : min(min(dist2(s, t[0]), dist2(t, s[0])), \n\t\t\t\t\t\t\t\t\t \t min(dist2(s, t[1]), dist2(t, s[1])));\n\t}\n\ttemplate <class T> R dist2(const G &g, const T& t){ // todo: 内部に完全に含まれる場合\n\t\tR res = INF;\n\t\tREP(i, g.size()) res = min(res, dist2(g.edge(i), t));\n\t\treturn res;\n\t}\n\ttemplate<class S, class T> R dist(const S& s, const T& t){return sqrt(dist2(s, t));}\n\tinline BOOL intersect(const C &a, const C &b){\n\t\treturn less((a.r-b.r)*(a.r-b.r), norm(a-b)) + less(norm(a-b), (a.r+b.r)*(a.r+b.r)) - 1;\n\t}\n\tinline BOOL intersect(const C &c, const L &l){\n\t\treturn less(dist2(l, c), c.r*c.r);\n\t}\n\tinline BOOL intersect(const C &c, const S &s){\n\t\tint d = less(dist2(s, c), c.r*c.r);\n\t\tif(d != TRUE) return d;\n\t\tint p = c.inside(s[0]), q = c.inside(s[1]);\n\t\treturn (p<0 || q<0) ? BORDER : p&q;\n\t}\n\t\n\t/*\n\t * CCWでs[0]->s[1]にかけてが共通部分\n\t */\n\tinline S crosspoint(const C &c1, const C &c2){\n\t\tif(!intersect(c1, c2)) return S();\n\t\tR d = abs(c1 - c2);\n\t\tR x = (c1.r*c1.r - c2.r*c2.r + d*d) / (2*d);\n\t\tR h = sqrt(max<R>(0., c1.r*c1.r - x*x));\n\t\tP u = unit(c2-c1);\n\t\treturn S(c1 + u*x + u*P(0,-1)*h, c1 + u*x + u*P(0,1)*h);\n\t}\n\tinline P crosspoint(const L &l, const L &m){\n\t\tR A = outp(l.dir(), m.dir()), B = outp(l.dir(), l[1] - m[0]);\n\t\tif(!sig(abs(A)) && !sig(abs(B))) return m[0]; // same line\n\t\tif(abs(A) < EPS) assert(false); // !!!PRECONDITION NOT SATISFIED!!!\n\t\treturn m[0] + B / A * (m[1] - m[0]);\n\t}\n\tinline S crosspoint(const C &c, const L &l){\n\t\tR d2 = dist2(l, c);\n\t\tif(c.r*c.r+EPS < d2) return S();\n\t\tP m = proj(c, l);\n\t\tP u = unit(l[1]-l[0]);\n\t\tR d = sqrt(max<R>(.0, c.r*c.r - d2));\n\t\treturn S(m+u*d, m-u*d);\n\t}\n\tinline vector<P> crosspoint(const C &c, const S &s){\n\t\tvector<P> res = crosspoint(c, (const L&)s);\n\t\tRREP(i, res.size()){\n\t\t\tif(inp(res[i]-s[0], s.dir())*inp(res[i]-s[1], -s.dir())<EPS)\n\t\t\t\tres.erase(res.begin() + i);\n\t\t}\n\t\treturn res;\n\t}\n\tinline R commonarea(const C &a, const C &b){\n\t\tif(less(norm(a-b), (a.r-b.r)*(a.r-b.r)) == TRUE) return min(a.r*a.r, b.r*b.r)*PI;\n\t\tif(less((a.r+b.r)*(a.r+b.r), norm(a-b)) == TRUE) return .0;\n\t\tR d = abs(a-b);\n\t\tR rc = (d*d + a.r*a.r - b.r*b.r) / (2*d);\n\t\tR theta = acos(rc / a.r);\n\t\tR phi = acos((d - rc) / b.r);\n\t\treturn a.r*a.r*theta + b.r*b.r*phi - d*a.r*sin(theta);\n\t}\n\tvector<L> CommonTangent(C c1, C c2){\n\t\tif(c1.r > c2.r) swap(c1, c2);\n\t\tR d = abs(c1-c2);\n\t\tvector<L> res;\n\t\tif(d < EPS) return res;\n\t\tif(d + EPS > c1.r + c2.r){\n\t\t\t// 内接線\n\t\t\tP crs = (c1*c2.r + c2*c1.r) / (c1.r + c2.r);\n\t\t\tR rad = asin(c1.r/abs(crs-c1));\n\t\t\tres.push_back(L(crs, crs + (c1-crs)*polar((R)1, rad)));\n\t\t\tres.push_back(L(crs, crs + (c1-crs)*polar((R)1, -rad)));\n\t\t}\n\t\tif(c1.r + d + EPS > c2.r){\n\t\t\t// 外接線\n\t\t\tR rad = 0.5*PI+asin((c2.r-c1.r) / d);\n\t\t\tP v = unit(c2-c1)*polar((R)1, rad);\n\t\t\tif(c1.r + d - EPS < c2.r){\n\t\t\t\tres.push_back(L(c1+v*c1.r, c1+v*c1.r+(c1-c2)*P(0, 1)));\n\t\t\t}else{\n\t\t\t\tres.push_back(L(c1+v*c1.r, c2+v*c2.r));\n\t\t\t\tv = (R)2.*proj(v, c2-c1) - v;\n\t\t\t\tres.push_back(L(c1+v*c1.r, c2+v*c2.r));\n\t\t\t}\n\t\t}\n\t\treturn res;\n\t}\n\t\n\tstruct min_ball {\n\t\tP center;\n\t\tR radius2;\n\t\tmin_ball(const vector<P>& p) {\n\t\t\tFOR(it, p) ps.push_back(*it);\n\t\t}\n\t\tmin_ball& compile() {\n\t\t\tm = 0;\n\t\t\tcenter = P(0, 0);\n\t\t\tradius2 = -1;\n\t\t\tmake_ball(ps.end());\n\t\t\treturn *this;\n\t\t}\n\tprivate:\n\t\tlist<P> ps;\n\t\tlist<P>::iterator supp_end;\n\t\tint m;\n\t\tP v[3], c[3];\n\t\tR z[3], r[3];\n\t\tvoid pop() { --m; }\n\t\tvoid push(const P& p) {\n\t\t\tif (m == 0) {\n\t\t\t\tc[0] = p; r[0] = 0;\n\t\t\t} else {\n\t\t\t\tR e = norm(p-c[m-1]) - r[m-1];\n\t\t\t\tP delta = p - c[0];\n\t\t\t\tv[m] = p - c[0];\n\t\t\t\tfor (int i = 1; i < m; ++i)\n\t\t\t\t\tv[m] -= v[i] * inp(v[i], delta) / z[i];\n\t\t\t\tz[m] = inp(v[m], v[m]);\n\t\t\t\tc[m] = c[m-1] + e*v[m]/z[m]*(R).5;\n\t\t\t\tr[m] = r[m-1] + e*e/z[m]*(R).25;\n\t\t\t}\n\t\t\tcenter\t= c[m];\n\t\t\tradius2 = r[m]; ++m;\n\t\t}\n\t\tvoid make_ball(list<P>::iterator i) {\n\t\t\tsupp_end = ps.begin();\n\t\t\tif (m == 3) return;\n\t\t\tfor (list<P>::iterator k = ps.begin(); k != i; ) {\n\t\t\t\tlist<P>::iterator j = k++;\n\t\t\t\tif (norm(*j-center) > radius2) {\n\t\t\t\t\tpush(*j); make_ball(j); pop();\n\t\t\t\t\tmove_to_front(j);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tvoid move_to_front(list<P>::iterator j) {\n\t\t\tif (supp_end == j) ++supp_end;\n\t\t\tps.splice (ps.begin(), ps, j);\n\t\t}\n\t};\n\t\n\ttemplate<class T> vector<T> merge(vector<T> s) {\n\t\tREP(i, s.size()) if (s[i][1] < s[i][0]) swap(s[i][0], s[i][1]);\n\t\tsort(ALL(s));\n\t\tREP(i, s.size())REP(j, i)if(intersect(s[i], s[j]) && !sig(outp(s[i].dir(), s[j].dir()))) {\n\t\t\ts[j][1] = max(s[i][1], s[j][1]);\n\t\t\ts.erase(s.begin() + i--);\n\t\t\tbreak;\n\t\t}\n\t\treturn s;\n\t}\n\n#undef SELF\n#undef at\n}\n\nusing namespace geom;\n\nint f = 0;\nnamespace std{\n\tbool operator<(const P &a, const P &b){return sig(a.X-b.X) ? a.X < b.X : a.Y+EPS < b.Y;}\n\tbool operator==(const P &a, const P &b){return abs(a-b) < EPS;}\n\tistream& operator>>(istream &is, P &p){R x,y;is>>x>>y;p=P(x, y);return is;}\n\tistream& operator>>(istream &is, L &l){l.resize(2);return is >> l[0] >> l[1];}\n\tistream& operator>>(istream &is, C &c){return is >> (P &)c >> c.r;}\n\tconst R B = 500;\n\tconst R Z = 5;\n\tostream& operator<<(ostream &os, const C &c){return os << \"circle(\"<<B+Z*(c.X)<<\", \"<<1000-B-Z*(c.Y)<<\", \"<<Z*(c.r)<<\")\";}\n\tostream& operator<<(ostream &os, const P &p){return os << C(p, 2./Z);}\n\tostream& operator<<(ostream &os, const S &s){return os << \"line(\"<<B+Z*(s[0].X)<<\", \"<<1000-B-Z*(s[0].Y)<<\", \"<<B+Z*(s[1].X)<<\", \"<<1000-B-Z*(s[1].Y)<<\")\";}\n\tostream& operator<<(ostream &os, const G &g){REP(i, g.size()) os << g.edge(i) << endl;return os;}\n\n}\nint n;\n\nint main(int argc, char *argv[]){\n\tios::sync_with_stdio(false);\n\tint T = 1;\n\twhile(cin >> n, n){\n\t\tP p;\n\t\tcin >> p;\n\t\tvector<G> g(n);\n\t\tREP(i, n){\n\t\t\tint x;\n\t\t\tcin >> x;\n\t\t\tg[i].resize(x);\n\t\t\tREP(j, x){\n\t\t\t\tcin >> g[i][j];\n\t\t\t\tg[i][j] -= p;\n\t\t\t}\n\t\t}\n\t\tp = -p;\n\t\tif([&](){\n\t\t\tREP(i, n)REP(j, g[i].size()){\n\t\t\t\tif(intersect(g[i].edge(j), S(0, p)) == TRUE) return 1;\n\t\t\t}\n\t\t\treturn 0;\n\t\t}()){\n\t\t\tprintf(\"%.10f\\n\", .0);\n\t\t\tcontinue;\n\t\t}\n\t\t\n\t\tvector<S> acc;\n\t\tREP(i, n)REP(j, g[i].size()){\n\t\t\tL s(0, unit(g[i][j]));\n\t\t\tif(ccw(0, s[1], g[i][(j+1)%g[i].size()]) != ccw(0, s[1], g[i][(j-1+g[i].size())%g[i].size()])) continue;\n\t\t\tR from = abs(g[i][j]), to = 20000;\n\t\t\tREP(ii, n)REP(jj, g[ii].size()){\n\t\t\t\tS t = g[ii].edge(jj);\n\t\t\t\tif(!sig(outp(s.dir(), t.dir())) || intersect(t, s) != TRUE) continue;\n\t\t\t\tP p = crosspoint(s, t);\n\t\t\t\tif(inp(s.dir(), p) > 0){\n\t\t\t\t\tto = min(to, abs(p));\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(from < to) acc.emplace_back(s[1]*from, s[1]*to);\n\t\t}\n\t\tauto cango = [&](const P &s, const P &t){\n\t\t\tconst S root(s, t);\n\t\t\tREP(i, n)REP(j, g[i].size()){\n\t\t\t\tconst S edge = g[i].edge(j);\n\t\t\t\tif(sig(outp(root.dir(), edge.dir())) && intersect(root, edge) == TRUE) return 0;\n\t\t\t}\n\t\t\treturn 1;\n\t\t};\n\t\t\n\t\tstruct State{\n\t\t\tint x, y;\n\t\t\tR cost;\n\t\t\tState (int x, int y, R cost)\n\t\t\t\t:x(x), y(y), cost(cost){}\n\t\t\tbool operator<(const State &opp) const{\n\t\t\t\treturn cost > opp.cost;\n\t\t\t}\n\t\t};\n\t\tvector<vector<R>> visited(n, vector<R>(101, INF));\n\t\tpriority_queue<State> pq;\n\t\tpq.emplace(-1, -1, 0);\n\t\tR ans = INF;\n\t\twhile(!pq.empty()){\n\t\t\tState s = pq.top(); pq.pop();\n\t\t\tP q = (s.x == -1) ? p : g[s.x][s.y];\n\t\t\tif(s.cost > ans) break;\n\t\t\tif(s.x >= 0 && visited[s.x][s.y] < s.cost - EPS) continue;\n\t\t\tFOR(it, acc){\n\t\t\t\tP pr = proj(q, *it);\n\t\t\t\tif(inp(pr - (*it)[0], it->dir()) < EPS) pr = (*it)[0];\n\t\t\t\telse if(inp(pr - (*it)[1], -it->dir()) < EPS) pr = (*it)[1];\n\t\t\t\tif(s.cost + abs(q - pr) < ans - EPS && cango(q, pr)){\n\t\t\t\t\tans = s.cost + abs(q - pr);\n\t\t\t\t}\n\t\t\t}\n\t\t\tREP(i, n)REP(j, g[i].size()){\n\t\t\t\tR c = s.cost + abs(g[i][j] - q);\n\t\t\t\tif(c+EPS > visited[i][j]) continue;\n\t\t\t\tif(cango(q, g[i][j])){\n\t\t\t\t\tvisited[i][j] = c;\n\t\t\t\t\tpq.emplace(i, j, c);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tprintf(\"%.10f\\n\", (double)ans);\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 950, "memory_kb": 1520, "score_of_the_acc": -0.1425, "final_rank": 4 } ]
aoj_2254_cpp
最短ルート English text is not available in this practice contest. あなたの所属する知的プログラミングサークル (通称 Intelligent Clever Programming Circle, ICPC)では,今大掃除の最中である. 部室には,歴代の先輩たちの残していったさまざまなアイテムが所狭しと放置されている. あなたが棚を整理していると,奥に大量のレトロゲームが封印されているのを発見した. その中のいくつかに見覚えがあったあなたは,久々にそのゲームを遊んでみることにした. 見つけたゲームの詳細は次の通りである. このゲームには1番から N 番までの N 個のステージがあり,任意の順に攻略することができる. また,1から N まで番号付けられた装備があり,それらの装備を使用することでステージの攻略時間を短縮することができる. ゲームを始めた段階では装備は一つも持っていないが, i 番のステージを攻略すると i 番の装備を入手することができ,一度入手した後は何度でも使用できる. 装備は一ステージで一種類だけしか使用することができないが,異なるステージで同じ装備を使用することはできる. あなたは昔このゲームをクリアしたことがあるので,各装備に対して,その装備を使用したときに各ステージを攻略するのにかかる時間をすべて把握している. ただ普通にクリアするだけではつまらないので,あなたは全てのステージを攻略し終わるまでにかかる時間を最小にしようと考えた. そのため,あなたはICPCでの経験を生かして,各情報を与えたときに全てのステージを攻略し終わるまでにかかる時間の最小値を計算するプログラムを書くことにした. Input 入力は複数のデータセットからなる. 入力の終わりは1つのゼロからなる行によって与えられる. 各データセットは一つのゲームに関する情報を表し,その形式は以下の通りである. N t 10 t 11 ... t 1N t 20 t 21 ... t 2N ... t N0 t N1 ... t NN データセットの最初の行は1つの整数 N からなり,ステージの数を表す. 続く N 行は N+1 個の整数からなり,ステージの攻略時間を表す. t i0 は i 番のステージを装備なしで攻略するのにかかる時間である. t i j ( j > 0)は i 番のステージを j 番の装備で攻略するのにかかる時間である. それぞれの値は次の制約を満たしている. 1 ≤ N ≤ 16 1 ≤ t i j ≤ 100,000 Output それぞれのデータセットに対して,全てのステージを攻略し終わるまでにかかる時間の最小値を表す整数を1行に出力せよ.出力行にはこの数値以外の文字を含んではならない. Sample Input 3 100 100 100 100 100 1 100 100 100 1 1 100 3 100 100 1 100 200 102 100 102 100 100 1 100 7 100 100 60 60 70 70 70 90 100 50 100 55 45 45 55 44 100 50 60 100 51 50 55 30 100 70 10 20 1 10 10 40 200 90 10 30 10 10 10 30 150 200 12 1 11 11 1 30 10000 1200 1100 1100 1200 1200 1090 1 0 Output for the Sample Input 102 202 1301
[ { "submission_id": "aoj_2254_10853366", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n int N;\n while(cin >> N, N) {\n vector<vector<int>> t(N, vector<int>(N + 1));\n for(int i = 0; i < N; ++i) {\n for(int j = 0; j < N + 1; ++j) {\n cin >> t[i][j];\n }\n }\n vector<int> dp(1 << N, 1e9);\n dp[0] = 0;\n for(int S = 0; S < (1 << N); ++S) {\n for(int i = 0; i < N; ++i) {\n if((S >> i) & 1) {\n continue;\n }\n int mi = t[i][0];\n for(int j = 0; j < N; ++j) {\n if((S >> j) & 1) {\n mi = min(mi, t[i][j + 1]);\n }\n }\n dp[S | (1 << i)] = min(dp[S | (1 << i)], dp[S] + mi);\n }\n }\n cout << dp[(1 << N) - 1] << endl;\n }\n}", "accuracy": 1, "time_ms": 340, "memory_kb": 3588, "score_of_the_acc": -0.0088, "final_rank": 1 }, { "submission_id": "aoj_2254_10637305", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing a2 = array<ll, 2>;\nusing a3 = array<ll, 3>;\n\ntemplate <typename A> void chmin(A &l, const A &r) {\n if(r < l)\n l = r;\n}\ntemplate <typename A> void chmax(A &l, const A &r) {\n if(l < r)\n l = r;\n}\n\nll mod = 998244353;\n\nvoid init() {}\n\nll n;\nvector<vector<ll>> v;\nvoid input() {\n cin >> n;\n v = vector<vector<ll>>(n, vector<ll>(n+1, 0));\n\n for(auto &x : v)\n for(auto &y : x)\n cin >> y;\n}\nvoid solve() {\n vector<ll> dp((1 << n), 1e18);\n dp[0] = 0;\n\n for(int i = 0; i < (1 << n); i++) {\n vector<ll> nxt;\n vector<ll> already;\n for(int j = 0; j < n; j++) {\n if((i & (1 << j)) == 0) {\n nxt.push_back(j);\n } else {\n already.push_back(j);\n }\n }\n\n for(auto &x : nxt) {\n ll add = v[x][0];\n for(auto &y : already) {\n chmin(add, v[x][y + 1]);\n }\n chmin(dp[i | (1 << x)], dp[i] + add);\n }\n }\n cout << dp.back() << endl;\n}\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(0);\n\n // init();\n while(1) {\n input();\n if(n == 0)\n break;\n solve();\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 330, "memory_kb": 3820, "score_of_the_acc": -0.0247, "final_rank": 4 }, { "submission_id": "aoj_2254_10635793", "code_snippet": "//line 2 \"/home/seekworser/.cpp_lib/competitive_library/competitive/std/std.hpp\"\n#include <bits/stdc++.h>\n#ifndef LOCAL_TEST\n#pragma GCC target (\"avx\")\n#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"unroll-loops\")\n#pragma GCC target(\"sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native\")\n#endif // LOCAL_TEST\nusing namespace std;\nusing std::cout;\n// shorten typenames\nusing ll = long long;\nusing pii = pair<int, int>; using pll = pair<ll, ll>;\nusing vi = vector<int>; using vvi = vector<vi>; using vvvi = vector<vvi>;\nusing vl = vector<ll>; using vvl = vector<vl>; using vvvl = vector<vvl>;\nusing vb = vector<bool>; using vvb = vector<vb>; using vvvb = vector<vvb>;\nusing vc = vector<char>; using vvc = vector<vc>; using vvvc = vector<vvc>;\nusing vd = vector<double>; using vvd = vector<vd>; using vvvd = vector<vvd>;\nusing vs = vector<string>; using vvs = vector<vector<string>>; using vvvs = vector<vector<vector<string>>>;\ntemplate<typename T> vector<vector<T>> vv(int h, int w, T val = T()) { return vector(h, vector<T>(w, val)); }\ntemplate<typename T> vector<vector<vector<T>>> vvv(int h1, int h2, int h3, T val = T()) { return vector(h1, vector(h2, vector<T>(h3, val))); }\ntemplate<typename T> vector<vector<vector<vector<T>>>> vvvv(int h1, int h2, int h3, int h4, T val = T()) { return vector(h1, vector(h2, vector(h3, vector<T>(h4, val)))); }\ntemplate <class T> using priority_queue_min = priority_queue<T, vector<T>, greater<T>>;\n// define CONSTANTS\nconstexpr double PI = 3.14159265358979323;\nconstexpr int INF = 100100111; constexpr ll INFL = 3300300300300300491LL;\nfloat EPS = 1e-8; double EPSL = 1e-10;\ntemplate<typename T> bool eq(const T x, const T y) { return x == y; }\ntemplate<> bool eq<double>(const double x, const double y) { return (abs(x - y) < EPSL * x || abs(x - y) < EPSL); }\ntemplate<> bool eq<float>(const float x, const float y) { return abs(x - y) < EPS * x; }\ntemplate<typename T> bool neq(const T x, const T y) { return !(eq<T>(x, y)); }\ntemplate<typename T> bool ge(const T x, const T y) { return (eq<T>(x, y) || (x > y)); }\ntemplate<typename T> bool le(const T x, const T y) { return (eq<T>(x, y) || (x < y)); }\ntemplate<typename T> bool gt(const T x, const T y) { return !(le<T>(x, y)); }\ntemplate<typename T> bool lt(const T x, const T y) { return !(ge<T>(x, y)); }\nconstexpr int MODINT998244353 = 998244353;\nconstexpr int MODINT1000000007 = 1000000007;\n// fasten io\nstruct Nyan { Nyan() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(18); } } nyan;\n// define macros\n#define all(a) (a).begin(), (a).end()\n#define sz(x) ((ll)(x).size())\n#define rep1(n) for(ll dummy_iter = 0LL; dummy_iter < n; ++dummy_iter) // 0,1,...,n-1\n#define rep2(i, n) for(ll i = 0LL, i##_counter = 0LL; i##_counter < ll(n); ++(i##_counter), (i) = i##_counter) // i=0,1,...,n-1\n#define rep3(i, s, t) for(ll i = ll(s), i##_counter = ll(s); i##_counter < ll(t); ++(i##_counter), (i) = (i##_counter)) // i=s,s+1,...,t-1\n#define rep4(i, s, t, step) for(ll i##_counter = step > 0 ? ll(s) : -ll(s), i##_end = step > 0 ? ll(t) : -ll(t), i##_step = abs(step), i = ll(s); i##_counter < i##_end; i##_counter += i##_step, i = step > 0 ? i##_counter : -i##_counter) // i=s,s+step,...,<t\n#define overload4(a, b, c, d, e, ...) e\n#define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)\n#define repe(a, v) for(auto& a : (v)) // iterate over all elements in v\n#define smod(n, m) ((((n) % (m)) + (m)) % (m))\n#define sdiv(n, m) (((n) - smod(n, m)) / (m))\n#define uniq(a) {sort(all(a)); (a).erase(unique(all(a)), (a).end());}\nint Yes(bool b=true) { cout << (b ? \"Yes\\n\" : \"No\\n\"); return 0; };\nint YES(bool b=true) { cout << (b ? \"YES\\n\" : \"NO\\n\"); return 0; };\nint No(bool b=true) {return Yes(!b);};\nint NO(bool b=true) {return YES(!b);};\ntemplate<typename T, size_t N> T max(array<T, N>& a) { return *max_element(all(a)); };\ntemplate<typename T, size_t N> T min(array<T, N>& a) { return *min_element(all(a)); };\ntemplate<typename T> T max(vector<T>& a) { return *max_element(all(a)); };\ntemplate<typename T> T min(vector<T>& a) { return *min_element(all(a)); };\ntemplate<typename T> vector<T> vec_slice(const vector<T>& a, int l, int r) { vector<T> rev; rep(i, l, r) rev.push_back(a[i]); return rev; };\ntemplate<typename T> T sum(vector<T>& a, T zero = T(0)) { T rev = zero; rep(i, sz(a)) rev += a[i]; return rev; };\ntemplate<typename T> bool in_range(const T& val, const T& s, const T& t) { return s <= val && val < t; };\n\ntemplate <class T> inline vector<T>& operator--(vector<T>& v) { repe(x, v) --x; return v; }\ntemplate <class T> inline vector<T>& operator++(vector<T>& v) { repe(x, v) ++x; return v; }\n\nll powm(ll a, ll n, ll mod=INFL) {\n ll res = 1;\n while (n > 0) {\n if (n & 1) res = (res * a) % mod;\n if (n > 1) a = (a * a) % mod;\n n >>= 1;\n }\n return res;\n}\nll sqrtll(ll x) {\n assert(x >= 0);\n ll rev = sqrt(x);\n while(rev * rev > x) --rev;\n while((rev+1) * (rev+1)<=x) ++rev;\n return rev;\n}\ntemplate <class T> inline bool chmax(T& M, const T& x) { if (M < x) { M = x; return true; } return false; }\ntemplate <class T> inline bool chmin(T& m, const T& x) { if (m > x) { m = x; return true; } return false; }\nint digit(ll x, int d=10) { int rev=0; while (x > 0) { rev++; x /= d;}; return rev; }\n/**\n * @brief std.hpp\n * @docs docs/std/std.md\n */\n//line 3 \"/home/seekworser/.cpp_lib/competitive_library/competitive/std/io.hpp\"\n// overload operators (prototypes)\ntemplate <class T, class U> inline istream& operator>>(istream& is, pair<T, U>& p);\ntemplate <class T> inline istream& operator>>(istream& is, vector<T>& v);\ntemplate <class T, class U> inline ostream& operator<<(ostream& os, const pair<T, U>& p);\ntemplate <class T> inline ostream& operator<<(ostream& os, const vector<T>& v);\ntemplate <typename T, typename S> ostream &operator<<(ostream &os, const map<T, S> &mp);\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &st);\ntemplate <typename T> ostream &operator<<(ostream &os, const multiset<T> &st);\ntemplate <typename T> ostream &operator<<(ostream &os, const unordered_set<T> &st);\ntemplate <typename T> ostream &operator<<(ostream &os, queue<T> q);\ntemplate <typename T> ostream &operator<<(ostream &os, deque<T> q);\ntemplate <typename T> ostream &operator<<(ostream &os, stack<T> st);\ntemplate <class T, class Container, class Compare> ostream &operator<<(ostream &os, priority_queue<T, Container, Compare> pq);\n\n// overload operators\ntemplate <class T, class U> inline istream& operator>>(istream& is, pair<T, U>& p) { is >> p.first >> p.second; return is; }\ntemplate <class T> inline istream& operator>>(istream& is, vector<T>& v) { repe(x, v) is >> x; return is; }\ntemplate <class T, class U> inline ostream& operator<<(ostream& os, const pair<T, U>& p) { os << p.first << \" \" << p.second; return os; }\ntemplate <class T> inline ostream& operator<<(ostream& os, const vector<T>& v) { rep(i, sz(v)) { os << v.at(i); if (i != sz(v) - 1) os << \" \"; } return os; }\ntemplate <typename T, typename S> ostream &operator<<(ostream &os, const map<T, S> &mp) { for (auto &[key, val] : mp) { os << key << \":\" << val << \" \"; } return os; }\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &st) { auto itr = st.begin(); for (int i = 0; i < (int)st.size(); i++) { os << *itr << (i + 1 != (int)st.size() ? \" \" : \"\"); itr++; } return os; }\ntemplate <typename T> ostream &operator<<(ostream &os, const multiset<T> &st) { auto itr = st.begin(); for (int i = 0; i < (int)st.size(); i++) { os << *itr << (i + 1 != (int)st.size() ? \" \" : \"\"); itr++; } return os; }\ntemplate <typename T> ostream &operator<<(ostream &os, const unordered_set<T> &st) { ll cnt = 0; for (auto &e : st) { os << e << (++cnt != (int)st.size() ? \" \" : \"\"); } return os; }\ntemplate <typename T> ostream &operator<<(ostream &os, queue<T> q) { while (q.size()) { os << q.front() << \" \"; q.pop(); } return os; }\ntemplate <typename T> ostream &operator<<(ostream &os, deque<T> q) { while (q.size()) { os << q.front(); q.pop_front(); if (q.size()) os << \" \"; } return os; }\ntemplate <typename T> ostream &operator<<(ostream &os, stack<T> st) { while (st.size()) { os << st.top() << \" \"; st.pop(); } return os; }\ntemplate <class T, class Container, class Compare> ostream &operator<<(ostream &os, priority_queue<T, Container, Compare> pq) { while (pq.size()) { os << pq.top() << \" \"; pq.pop(); } return os; }\n\ntemplate <typename T> int print_sep_end(string sep, string end, const T& val) { (void)sep; cout << val << end; return 0; };\ntemplate <typename T1, typename... T2> int print_sep_end(string sep, string end, const T1 &val, const T2 &...remain) {\n cout << val << sep;\n print_sep_end(sep, end, remain...);\n return 0;\n};\ntemplate <typename... T> int print(const T &...args) { print_sep_end(\" \", \"\\n\", args...); return 0; };\ntemplate <typename... T> void flush() { cout << flush; };\ntemplate <typename... T> int print_and_flush(const T &...args) { print(args...); flush(); return 0; };\n#define debug(...) debug_func(0, #__VA_ARGS__, __VA_ARGS__) // debug print\ntemplate <typename T> void input(T &a) { cin >> a; };\ntemplate <typename T1, typename... T2> void input(T1&a, T2 &...b) { cin >> a; input(b...); };\n#ifdef LOCAL_TEST\ntemplate <typename T> void debug_func(int i, const T name) { (void)i; (void)name; cerr << endl; }\ntemplate <typename T1, typename T2, typename... T3> void debug_func(int i, const T1 &name, const T2 &a, const T3 &...b) {\n int scope = 0;\n for ( ; (scope != 0 || name[i] != ',') && name[i] != '\\0'; i++ ) {\n cerr << name[i];\n if (name[i] == '(' || name[i] == '{') scope++;\n if (name[i] == ')' || name[i] == '}') scope--;\n }\n cerr << \":\" << a << \" \";\n debug_func(i + 1, name, b...);\n}\ntemplate <typename T1, typename T2, typename... T3> void debug_func(int i, const T1 &name, T2 &a, T3 &...b) {\n int scope = 0;\n for ( ; (scope != 0 || name[i] != ',') && name[i] != '\\0'; i++ ) {\n cerr << name[i];\n if (name[i] == '(' || name[i] == '{') scope++;\n if (name[i] == ')' || name[i] == '}') scope--;\n }\n cerr << \":\" << a << \" \";\n debug_func(i + 1, name, b...);\n}\n#endif\n#ifndef LOCAL_TEST\ntemplate <typename... T>\nvoid debug_func(T &...) {}\ntemplate <typename... T>\nvoid debug_func(const T &...) {}\n#endif\n/**\n * @brief io.hpp\n * @docs docs/std/io.md\n */\n//line 3 \"answer.cpp\"\nint main() {\n while (true) {\n ll n; input(n);\n if (n == 0) break;\n vector a(n, vector<ll>(n+1, 0));\n rep(i, n) rep(j, n+1) cin >> a[i][j];\n vector<ll> dp((1LL << n), INFL);\n dp[0] = 0;\n rep(bit, (1LL << n)) {\n rep(i, n) {\n if ((bit >> i) & 1) continue;\n ll bn = bit ^ (1LL << i);\n ll add = a[i][0];\n rep(j, n) {\n if ((bit >> j) & 1) {chmin(add, a[i][j+1]);}\n }\n chmin(dp[bn], dp[bit] + add);\n }\n }\n cout << dp[(1LL << n) - 1] << '\\n';\n }\n}", "accuracy": 1, "time_ms": 350, "memory_kb": 3868, "score_of_the_acc": -0.0422, "final_rank": 7 }, { "submission_id": "aoj_2254_10560969", "code_snippet": "//#pragma GCC optimize(\"O3\")\n#include<bits/stdc++.h>\nusing namespace std;\n#define ll long long\n#define rep(i,n) for (ll i=0;i<(ll)n;i++)\n#define rrep(i,n) for (ll i=n-1;i>=(ll)0;i--)\n#define loop(i,m,n) for(ll i=m;i<=(ll)n;i++)\n#define rloop(i,m,n) for(ll i=m;i>=(ll)n;i--)\n#define vl vector<long long>\n#define vvl vector<vector<long long>>\n#define vdbg(a) rep(ii,a.size()){cout<<a[ii]<<\" \";}cout<<endl;\n#define vvdbg(a) rep(ii,a.size()){rep(jj,a[ii].size()){cout<<a[ii][jj]<<\" \";}cout<<endl;}\n#define setdbg(a) for(const auto & ii:a){cout<<ii<<\" \";}cout<<endl;\n#define inf 4000000000000000000LL\n#define mod 998244353LL\n#define eps 0.000000001\n//#define mod 1000000007LL\nrandom_device rnd;// 非決定的な乱数生成器\nmt19937 mt(rnd());// メルセンヌ・ツイスタの32ビット版、引数は初期シード\n\n//#include<boost/multiprecision/cpp_int.hpp>\n//#define bbi boost::multiprecision::cpp_int\n\n//#include<atcoder/lazysegtree>\n\n//√の値が整数かを調べる\nbool isSqrt(ll n) {\n\tif (n < 0) return false;\n\tll sqrtN = static_cast<ll>(sqrt(n));\n\treturn sqrtN * sqrtN == n;\n}\n\n//整数同士の累乗の計算をする。\nll power(ll A, ll B) {\n\tll result = 1;\n\tfor (ll i=0;i<B;i++){\n\t\tresult *= A;\n\t}\n\treturn result;\n}\n\n//素因数分解\nvector<ll> makePrime(ll n){\n\tvector<ll> factors;\n\twhile (n % 2 == 0) {\n\t\tfactors.push_back(2);\n\t\tn /= 2;\n\t}\n\tfor (ll i=3; i*i<=n;i+=2) {\n\t\twhile (n%i == 0) {\n\t\t\tfactors.push_back(i);\n\t\t\tn /= i;\n\t\t}\n\t}\n\tif (n > 2) {\n\t\tfactors.push_back(n);\n\t}\n\treturn factors;\n}\n\n//map形式で、nを素因数分解した値を返す\nmap<ll,ll> makeMapPrime(ll n){\n\tmap<ll,ll> factors;\n\twhile (n % 2 == 0) {\n\t\tfactors[2]++;\n\t\tn /= 2;\n\t}\n\tfor (ll i=3; i*i<=n;i+=2) {\n\t\twhile (n%i == 0) {\n\t\t\tfactors[i]++;\n\t\t\tn /= i;\n\t\t}\n\t}\n\tif (n > 2) {\n\t\tfactors[n]++;\n\t}\n\treturn factors;\n}\n\n// nのk乗をmodで割った余りを計算\nll power_mod(ll n, ll k){\n\tlong long result = 1;\n\twhile (k > 0){\n\t\tif ((k&1) ==1)result=(result*n)%mod;\n\t\tn=n*n%mod;\n\t\tk >>= 1;\n\t}\n\treturn result;\n}\n\n//mod mにおけるaの逆元を計算\nll modinv(ll a, ll m) {\n\tll b = m, u = 1, v = 0;\n\twhile (b) {\n\t\tll t = a / b;\n\t\ta -= t * b; swap(a, b);\n\t\tu -= t * v; swap(u, v);\n\t}\n\tu %= m; \n\tif (u < 0) u += m;\n\treturn u;\n}\n\n//場合の数 nCr を求める\nll ncr(ll n,ll r) {\n\tif(n<r)return 0;\n\tvvl dp(n+1,vl(r+1));\n\trep (i,n+1)dp[i][0] = 1;\n\trep (i,r+1)dp[i][i] = 1;\n\tloop (i,1,n){\n\t\tloop (j,1,min((ll)i-1,r)) {\n\t\t\t//nCr= n-1Cr-1 + n-1Cr\n\t\t\tdp[i][j] = dp[i-1][j-1] + dp[i-1][j];\n\t\t}\n\t}\n\treturn dp[n][r];\n}\n\n//受け取った文字列を、第2引数が0なら全て小文字に、1なら大文字に変換する関数\nstring cnvString(const string &str, int mode) {\n\tstring result = str;\n\tif (mode == 0) {\n\t\t// 小文字に変換\n\t\tfor (char &c : result) {\n\t\t\tc = tolower(c);\n\t\t}\n\t} else if (mode == 1) {\n\t\t// 大文字に変換\n\t\tfor (char &c : result) {\n\t\t\tc = toupper(c);\n\t\t}\n\t}\n\treturn result;\n}\n\n//第一引数で受け取った数を、第二引数で受け取った数の進数と見做して、第三引数の進数へ変換する。\nstring cnvBase(const string &str, ll from_base, ll to_base) {\n\tll num = 0;\n\t//小文字があったら大文字に変換\n\tstring num_str=cnvString(str,1);\n\t// 数値を10進数に変換\n\tfor (char digit : num_str) {\n\t\tnum = num * from_base + (isdigit(digit) ? digit - '0' : digit - 'A' + 10);\n\t}\n\tstring result;\n\t// 数値を目的の基数に変換\n\twhile (num > 0) {\n\t\tll remainder = num % to_base;\n\t\tresult.push_back(remainder < 10 ? remainder + '0' : remainder - 10 + 'A');\n\t\tnum /= to_base;\n\t}\n\t// 結果を逆順にして返す\n\treverse(result.begin(), result.end());\n\treturn result.empty() ? \"0\" : result;\n}\n\n//底がaの対数xを計算。ただし小数点は繰り上げ。\nll logax(ll a, ll x){\n\tif(x<=1)return 0;\n\tll result = 1;\n\tll power = 1;\n\twhile (power < (x+a-1) / a){\n\t\tpower *= a;\n\t\tresult++;\n\t}\n\treturn result;\n}\n\n//第一引数を第二引数で割った余りを計算、割る数はint範囲\nll bigmd(const string &num, int md) {\n\tll ans = 0;\n\tll SIZ = 9; //9桁のチャンク\n\tll base = 1000000000;//SIZ個の0\n\trep(i,(num.size()-1)/SIZ+1){\n\t\tll chunk = 0;\n\t\tll l = i*SIZ;\n\t\tll r = min((ll)num.size(),l+SIZ);\n\t\tif(r!=num.size()){\n\t\t\tans = (ans*base+stoll(num.substr(l,r-l)))%md;\n\t\t}else{\n\t\t\trep(i,r-l)ans*=10;\n\t\t\tans=(ans+stoll(num.substr(l,r-l)))%md;\n\t\t}\n\t}\n\treturn ans;\n}\n\n//受け取った2次元文字の外側に、文字pをコーティングする。\nvector<string> pad(vector<string> &s,char p){\n\tll h=s.size();\n\tll w=s[0].size();\n\tvector<string> res(h+2,string(w+2,p));\n\trep(i,h)rep(j,w)res[i+1][j+1]=s[i][j];\n\treturn res;\n}\n\n//ax+by=cの整数解を得る ただし、cはgcd(a,b)の倍数でない場合、0,0になる\npair<ll,ll> ex_euclid(ll a,ll b,ll c){\n\tif(a<0||b<0||c<0){\n\t\tpair<ll,ll>ans=ex_euclid(abs(a),abs(b),abs(c));\n\t\tif(a<0)ans.first*=-1;\n\t\tif(b<0)ans.second*=-1;\n\t\tif(c<0)ans.first*=-1,ans.second*=-1;\n\t\treturn ans;\n\t}\n\tif(c!=1){\n\t\tll d=gcd(a,b);\n\t\tif(c%d!=0)return make_pair(0,0);\n\t\tpair<ll,ll>ans = ex_euclid(a/d,b/d,1);\n\t\tans.first*=c/d;\n\t\tans.second*=c/d;\n\t\treturn ans;\n\t}\n\tif(a<b){\n\t\tpair<ll,ll>ans=ex_euclid(b,a,c);\n\t\tswap(ans.first,ans.second);\n\t\treturn ans;\n\t}\n\tif(a==1&&b==0)return make_pair(1,0);\n\telse if(b==0) return make_pair(0,0);\n\tll x,y;\n\ttie(x,y)=ex_euclid(b,a%b,c);\n\tpair<ll,ll> ans=make_pair(y,x-(a/b)*y);\n\treturn ans;\n}\n\n//オイラーのトーシェント関数。N以下のNと互いに素な物の数を返す。\nll euler(ll n){\n\tunordered_map<ll,ll> factors;\n\tll tmp=n;\n\twhile (tmp % 2 == 0) {\n\t\tfactors[2]++;\n\t\ttmp /= 2;\n\t}\n\tfor (ll i=3; i*i<=tmp;i+=2) {\n\t\twhile (tmp%i == 0) {\n\t\t\tfactors[i]++;\n\t\t\ttmp/= i;\n\t\t}\n\t}\n\tif (tmp > 2)factors[tmp]++;\n\tll ans=1;\n\tfor(const auto & val:factors){\n\t\tans*=power(val.first,val.second-1)*(val.first-1);\n\t}\n\treturn ans;\n}\n\n// Union-Find\nstruct UnionFind {\n\tvector<int> par, siz;\n\tUnionFind(int n) : par(n, -1) , siz(n, 1) { }\n\t// 根を求める\n\tint root(int x) {\n\t\tif (par[x] == -1) return x;\n\t\telse return par[x] = root(par[x]);\n\t}\n\t// x と y が同じグループに属するかどうか (根が一致するかどうか)\n\tbool issame(int x, int y) {\n\t\treturn root(x) == root(y);\n\t}\n\t// x を含むグループと y を含むグループとを併合する\n\tbool unite(int x, int y) {\n\t\tx = root(x), y = root(y);\n\t\tif (x == y) return false; \n\t\tif (siz[x] < siz[y]) swap(x, y);\n\t\tpar[y] = x;\n\t\tsiz[x] += siz[y];\n\t\treturn true;\n\t}\n\t// x を含むグループのサイズ\n\tint size(int x) {\n\t\treturn siz[root(x)];\n\t}\n};\n\n//重み付きUF\nstruct PotentialUnionFind {\n\tll n;\n\tvl par, siz, pot;\n\tPotentialUnionFind(ll N) : par(N,-1) , siz(N,1) , pot(N,0){n=N;}\n\t// 根を求める\n\tll root(ll x) {\n\t\tif (par[x] == -1) return x;\n\t\tll tmp = root(par[x]);\n\t\tpot[x] += pot[par[x]];\n\t\tpar[x] = tmp;\n\t\treturn par[x];\n\t}\n\t// x と y が同じグループに属するかどうか (根が一致するかどうか)\n\tbool issame(ll x, ll y) {\n\t\treturn root(x) == root(y);\n\t}\n\t//x よりいくつ大きい所に y があるか。根が一致しない場合は\"0\"\n\tll potential(ll x,ll y){\n\t\tif(root(x) != root(y)) return 0;\n\t\telse return pot[y]-pot[x];\n\t}\n\t//x より w だけ大きい状態として y を併合。\n\tbool unite(ll x, ll y, ll w) {\n\t\tll rx = root(x),ry = root(y);\n\t\tif (rx == ry) return false;\n\t\tw += pot[x]-pot[y];\n\t\tif (siz[rx] < siz[ry]) swap(rx, ry),w*=-1;\n\t\tpar[ry] = rx;\n\t\tsiz[rx] += siz[ry];\n\t\tsiz[ry] = 0;\n\t\tpot[ry] = w;\n\t\treturn true;\n\t}\n\t// x を含むグループのサイズ\n\tll size(ll x) {\n\t\treturn siz[root(x)];\n\t}\n\t//小さい順にUnionFindグラフを調整、O(n log n)\n\tvoid regulation(){\n\t\tvvl r(n);\n\t\trep(i,n)r[root(i)].push_back(i);\n\t\trep(i,n){\n\t\t\tif(r[i].size()==0)continue;\n\t\t\tll mn = i;\n\t\t\trep(j,r[i].size())if(pot[mn]>pot[r[i][j]])mn=r[i][j];\n\t\t\tsiz[mn]=siz[i];\n\t\t\tsiz[i]=0;\n\t\t\tll tmp = pot[mn];\n\t\t\trep(j,r[i].size()){\n\t\t\t\tpot[r[i][j]]-=tmp;\n\t\t\t\tpar[r[i][j]] = mn;\n\t\t\t}\n\t\t\tpar[mn]=-1;\n\t\t}\n\t}\n\tvoid debug(){\n\t\trep(i,n)cout<<setw(4)<<left<<par[i]<<\" \";\n\t\tcout<<endl;\n\t\trep(i,n)cout<<setw(4)<<left<<pot[i]<<\" \";\n\t\tcout<<endl;\n\t}\n};\n\n//分離可能UnionFind、経路圧縮をしない。\nstruct CuttingFind{\n\tvector<int> par, siz;\n\tCuttingFind(int n) : par(n, -1) , siz(n, 1) { }\n\t// 根を求める\n\tint root(int x) {\n\t\tif (par[x] == -1) return x;\n\t\telse return root(par[x]);\n\t}\n\t// x と y が同じグループに属するかどうか (根が一致するかどうか)\n\tbool issame(int x, int y) {\n\t\treturn root(x) == root(y);\n\t}\n\t//根x と 根y のグループを併合する(お互い根ではない時、falseで何もしない)\n\tbool unite(int x, int y) {\n\t\tif (issame(x,y) || par[x] != -1 || par[y] != -1) {\n\t\t\tcout<<\"error\"<<endl;\n\t\t\treturn false;\n\t\t}\n\t\tif (siz[x] < siz[y]) swap(x, y);\n\t\tpar[y] = x;\n\t\tsiz[x] += siz[y];\n\t\treturn true;\n\t}\n\t//根の側から、その直系の子供を分離する。片方が根でもう片方が直系の子でなければならない。\n\tbool separate(int x,int y){\n\t\tif(par[y]==-1)swap(x,y);\n\t\tif(par[y]!=x||par[x]!=-1){\n\t\t\tcout<<\"error2\"<<endl;\n\t\t\treturn false;\n\t\t}\n\t\tsiz[x] -= siz[y];\n\t\tpar[y]=-1;\n\t\treturn true;\n\t}\n\t// x を含むグループのサイズを求める\n\tint size(int x) {\n\t\treturn siz[root(x)];\n\t}\n};\n//セグ木,乗せる値の型が必要\ntemplate<typename T>\nstruct SegTree{\n\tll size;\n\tll tall;\n\tvector<T> data;\n\tfunction<T(T,T)> p;\n\t//セグ木に乗せる値の初期値をa配列にし、putの関数をセグ木に乗せる、dをデフォルト値に。\n\tSegTree(vector<T> a,function<T(T,T)> put,T d) : data(power(2,logax(2,a.size())+1)) {\n\t\tsize = data.size()/2;\n\t\ttall=logax(2,size)+1;\n\t\tp=put;\n\t\tll tmp=size;\n\t\tdata = vector<T>(size*2,d);\n\t\twhile(tmp!=0){\n\t\t\tif(tmp==size)rep(i,a.size())data[tmp+i]=a[i];\n\t\t\telse rep(i,tmp) data[tmp+i]=p(data[2*(tmp+i)],data[2*(tmp+i)+1]);\n\t\t\ttmp/=2;\n\t\t}\n\t}\n\t//更新、t番目の値をxにする。\n\tvoid update(ll t,T x){\n\t\tt+=size;\n\t\twhile(t!=0){\n\t\t\tif(t>=size)data[t]=x;\n\t\t\telse data[t]=p(data[2*t],data[2*t+1]);\n\t\t\tt/=2;\n\t\t}\n\t}\n\t//取得、l~r区間内の評価値を取得する。\n\tT get(ll l,ll r){\n\t\t//lとrが範囲外なら範囲内に正す\n\t\tl=max(0LL,l);\n\t\tr=min(r,size-1);\n\t\tr++;\n\t\tT ans=data[0];\n\t\tll pos=l+size;\n\t\tll wid=1;\n\t\t//出来る限り上に上げきる。\n\t\twhile(l+(wid*2)<=r){\n\t\t\twhile(l%(wid*2)==0&&l+(wid*2)<=r)pos/=2,wid*=2;\n\t\t\tans=p(ans,data[pos]);\n\t\t\tpos++;\n\t\t\tl+=wid;\n\t\t}\n\t\t//上げ終わったので今度は下げる\n\t\twhile(l!=r){\n\t\t\twhile(l+wid>r)pos*=2,wid/=2;\n\t\t\tans=p(ans,data[pos]);\n\t\t\tpos++;\n\t\t\tl+=wid;\n\t\t}\n\t\treturn ans;\n\t}\n\t//セグ木デバッグ用、丸ごと出力\n\tvoid print(){\n\t\trep(i,size)cout<<setw(7)<<left<<i;\n\t\tcout<<endl;\n\t\tll pos=size;\n\t\trep(i,tall){\n\t\t\trep(j,size){\n\t\t\t\tif(j%power(2,i)==0)cout<<setw(7)<<left<<data[pos],pos++;\n\t\t\t\telse cout<<\" \";\n\t\t\t}\n\t\t\tpos/=4;\n\t\t\tcout<<endl;\n\t\t}\n\t}\n};\n\n//グリッド問題等用\nvl dx={1,0,-1,0};\nvl dy={0,1,0,-1};\n\n\nll solve(){\n\tll n;\n\tcin>>n;\n\tif(n==0){return 1;}\n\tvvl a(n,vl(n+1));\t\n\trep(i,n)rep(j,n+1){\n\t\tcin>>a[i][j];\n\t}\n\n\tvl dp(1<<n,inf);\n\tdp[0]=0;\n\n\trep(i,1<<n){\n\t\trep(j,n){\n\t\t\tif(i&(1LL<<j))continue;\n\t\t\tll mn=a[j][0];\n\t\t\tloop(k,1,n){\n\t\t\t\tif(!(i&(1LL<<(k-1))))continue;\n\t\t\t\tmn=min(mn,a[j][k]);\n\t\t\t}\n\t\t\tdp[i+(1LL<<j)]=min(dp[i+(1LL<<j)],dp[i]+mn);\n\t\t}\n\t}\n\tcout<<dp[(1LL<<n)-1]<<endl;\n\treturn 0;\n}\n\n//メイン\nint main(){\n\twhile(solve()==0);\n\treturn 0;\n}", "accuracy": 1, "time_ms": 390, "memory_kb": 4180, "score_of_the_acc": -0.0981, "final_rank": 14 }, { "submission_id": "aoj_2254_10418266", "code_snippet": "// #pragma GCC optimize(\"Ofast\")\n// #pragma GCC optimize(\"unroll-loops\")\n\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ull = unsigned long long;\nusing lll = __int128_t;\nusing ld = long double;\nusing P = pair<ll, ll>;\nusing vi = vector<ll>;\nusing vd = vector<ld>;\nusing vP = vector<P>;\nusing vS = vector<string>;\nusing vvi = vector<vi>;\nusing vvd = vector<vd>;\nusing vvP = vector<vP>;\nusing v3i = vector<vvi>;\nusing v3d = vector<vvd>;\nusing v3P = vector<vvP>;\nusing v4i = vector<v3i>;\nusing v5i = vector<v4i>;\nusing v6i = vector<v5i>;\n#if __has_include(<atcoder/all>)\n#include <atcoder/all>\nusing namespace atcoder;\nusing mint = modint998244353;\nusing Mint = modint1000000007;\nusing vm = vector<mint>;\nusing vM = vector<Mint>;\nusing vvm = vector<vm>;\nusing vvM = vector<vM>;\nusing v3m = vector<vvm>;\nusing v3M = vector<vvM>;\nusing v4m = vector<v3m>;\nusing v5m = vector<v4m>;\n#endif\ntemplate <typename T>\nusing rp_Q = priority_queue<T, vector<T>, greater<T>>;\n#define rrep(i, n) for (ll i = (n) - 1; (i) >= 0; --(i))\n#define rep(i, n) for (ll i = 0; (i) < (n); ++(i))\n#define loop(i, a) for (ll i = a; true; ++(i))\n#define reps(i, n) for (ll i = 1; (i) <= (n); ++(i))\n#define Rep(i, a, b) for (ll i = (a); i <= (b); i++)\n#define all(a) (a).begin(), (a).end()\nconst ll MOD = 998244353;\nconst ll Hash = 10000000000000061;\n#define Yes(b) ((b) ? cout << \"Yes\" << endl : cout << \"No\" << endl)\n#define YES(b) ((b) ? cout << \"YES\" << endl : cout << \"NO\" << endl)\n#define Aoki(b) ((b) ? cout << \"Aoki\" << endl : cout << \"Takahashi\" << endl)\n#define flush cout << endl;\n#define out(x) cout << (x) << endl\n#define vout(v) \\\n for (auto x : (v)) \\\n cout << x << ' ';\n#define vin(v) rep(i, (v).size()) cin >> (v)[i]\n#define vin_(v) \\\n ; \\\n rep(i, (v).size()) \\\n { \\\n cin >> (v)[i]; \\\n (v)[i]--; \\\n }\n#define vvin(v, w) \\\n rep(i, (v).size()) \\\n { \\\n rep(k, (w)) { cin >> (v)[i][k]; } \\\n }\n#define ft first\n#define sd second\n#define vInP(v) rep(i, (v).size()) cin >> (v)[i].ft >> (v)[i].sd\n#define vInP_(v) \\\n rep(i, (v).size()) \\\n { \\\n cin >> (v)[i].ft >> (v)[i].sd; \\\n (v)[i].ft--; \\\n (v)[i].sd--; \\\n }\nbool chmin(auto &a, auto b) { return a > b ? a = b, 1 : 0; }\nbool chmax(auto &a, auto b) { return a < b ? a = b, 1 : 0; }\nvi s90 = {0, 1, 0, -1}, c90 = {1, 0, -1, 0};\nvi s45 = {0, 1, 1, 1, 0, -1, -1, -1}, c45 = {1, 1, 0, -1, -1, -1, 0, 1};\nconst int INF = 1'000'000'000;\n\nint main()\n{\n while (true)\n {\n int N;\n cin >> N;\n if (N == 0){\n break;\n }\n vector<int> t0(N);\n vector t(N, vector<int>(N, 0));\n for (int i = 0; i < N; i++)\n {\n cin >> t0[i];\n for (int j = 0; j < N; j++)\n {\n cin >> t[i][j];\n }\n }\n vector dp(1 << N, vector<int>(N, INF));\n for (int i = 0; i < N; i++)\n {\n dp[0][i] = t0[i];\n }\n for (int b = 0; b < (1 << N); b++)\n {\n for (int i = 0; i < N; i++)\n {\n dp[b][i] = t0[i];\n for (int j = 0; j < N; j++)\n {\n if (b & (1 << j))\n dp[b][i] = min(dp[b][i], t[i][j]);\n }\n }\n }\n vector<int> time(1 << N, INF);\n time[0] = 0;\n rep(bit, (1 << N))\n {\n rep(k, N)\n {\n if (bit & (1 << k))\n {\n int Bit = bit - (1 << k);\n chmin(time[bit], time[Bit] + dp[Bit][k]);\n }\n }\n }\n cout << time[(1 << N) - 1] << endl;\n }\n}", "accuracy": 1, "time_ms": 520, "memory_kb": 10576, "score_of_the_acc": -0.7992, "final_rank": 18 }, { "submission_id": "aoj_2254_10418250", "code_snippet": "// https://codeforces.com/blog/entry/96344\n//#pragma GCC optimize(\"O3,unroll-loops\")\n//#pragma GCC target(\"avx2,bmi,bmi2,lzcnt,popcnt\")\n\n#include <bits/stdc++.h>\n\nusing namespace std;\n\nusing ll = long long;\nusing u32 = unsigned int;\nusing u64 = unsigned long long;\n\n#if __cplusplus >= 202002L\nusing i128 = __int128;\nusing u128 = unsigned __int128;\nusing f128 = __float128;\n#endif\n\ntemplate<typename T>\nbool chmax(T& a, const T& b) {\n bool res = a < b;\n a = max(a, b);\n return res;\n}\ntemplate<typename T>\nbool chmin(T& a, const T& b){\n bool res = a > b;\n a = min(a, b);\n return res;\n}\n\ntypedef vector<long long> vl;\ntypedef pair<ll,ll> pll;\ntypedef vector<pair<ll, ll>> vll;\ntypedef vector<int> vi;\ntypedef vector<pair<int,int>> vii;\ntypedef pair<int,int> pii;\n\nconst int inf = 1000000009;\nconst ll linf = 4000000000000000009;\n\n\n// https://trap.jp/post/1224/\ntemplate<class... T>\nvoid input(T&... a){\n (cin >> ... >> a);\n}\n\nvoid print(){\n cout << '\\n';\n}\ntemplate<class T, class... Ts>\nvoid print(const T& a, const Ts&... b){\n cout << a;\n (cout << ... << (cout << ' ', b));\n cout << '\\n';\n}\n\ntemplate <typename T>\nT floor(T a, T b) {\n return a / b - (a % b && (a ^ b) < 0);\n}\ntemplate <typename T>\nT ceil(T x, T y) {\n return floor(x + y - 1, y);\n}\n\nint popcnt(int x) { return __builtin_popcount(x); }\nint popcnt(u32 x) { return __builtin_popcount(x); }\nint popcnt(ll x) { return __builtin_popcountll(x); }\nint popcnt(u64 x) { return __builtin_popcountll(x); }\nint popcnt_mod_2(int x) { return __builtin_parity(x); }\nint popcnt_mod_2(u32 x) { return __builtin_parity(x); }\nint popcnt_mod_2(ll x) { return __builtin_parityll(x); }\nint popcnt_mod_2(u64 x) { return __builtin_parityll(x); }\n// (0, 1, 2, 3, 4) -> (-1, 0, 1, 1, 2)\nint topbit(int x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); }\nint topbit(u32 x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); }\nint topbit(ll x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }\nint topbit(u64 x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }\n// (0, 1, 2, 3, 4) -> (-1, 0, 1, 0, 2)\nint lowbit(int x) { return (x == 0 ? -1 : __builtin_ctz(x)); }\nint lowbit(u32 x) { return (x == 0 ? -1 : __builtin_ctz(x)); }\nint lowbit(ll x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }\nint lowbit(u64 x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }\n\ntemplate<typename T, typename U>\nT SUM(const vector<U> &A){\n T ret = 0;\n for (auto &&a: A) ret += a;\n return ret;\n}\n\n#define rep1(a) for(int i = 0; i < a; i++)\n#define rep2(i, a) for(int i = 0; i < a; i++)\n#define rep3(i, a, b) for(int i = a; i < b; i++)\n#define rep4(i, a, b, c) for(int i = a; i < b; i += c)\n#define overload4(a, b, c, d, e, ...) e\n#define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)\n\n#define rrep(i, a, b) for(int i = a; i >= b; i--)\n\n#define eb emplace_back\n#define mp make_pair\n#define mt make_tuple\n#define fi first\n#define se second\n#define all(v) v.begin(), v.end()\n//---------------------------------\n\n\nll dp[1<<20];\nll t[20][20];\n\nvoid solve(int n){\n rep(i, 1<<n){\n dp[i] = linf;\n }\n dp[0] = 0;\n rep(i, n) rep(j, n+1){\n cin >> t[i][j];\n }\n for(ll s = 0; s < (1<<n); s++){\n rep(i, n){\n if((s & (1<<i)) != 0) continue;\n ll mn = t[i][0];\n rep(j, 1, n+1){\n if(s & (1<<(j-1))){\n chmin(mn, t[i][j]);\n }\n }\n chmin(dp[s | (1<<i)], dp[s] + mn);\n }\n }\n print(dp[(1<<n)-1]);\n}\n\n\nint main(){\n ios::sync_with_stdio(false);\n cin.tie(0);\n\n int n;\n cin >> n;\n while(n > 0){\n solve(n);\n cin >> n;\n }\n}", "accuracy": 1, "time_ms": 380, "memory_kb": 4096, "score_of_the_acc": -0.0836, "final_rank": 10 }, { "submission_id": "aoj_2254_10417757", "code_snippet": "/**\n\tauthor: shobonvip\n\tcreated: 2025.04.24 17:46:57\n**/\n\n#include<bits/stdc++.h>\nusing namespace std;\n\ntypedef long long ll;\n\n#define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++)\n#define rrep(i, s, n) for (int i = (int)(n)-1; i >= (int)(s); i--)\n#define all(v) v.begin(), v.end()\n\ntemplate <typename T> bool chmin(T &a, const T &b) {\n\tif (a <= b) return false;\n\ta = b;\n\treturn true;\n}\n\ntemplate <typename T> bool chmax(T &a, const T &b) {\n\tif (a >= b) return false;\n\ta = b;\n\treturn true;\n}\n\ntemplate <typename T> T max(vector<T> &a){\n\tassert(!a.empty());\n\tT ret = a[0];\n\tfor (int i=0; i<(int)a.size(); i++) chmax(ret, a[i]);\n\treturn ret;\n}\n\ntemplate <typename T> T min(vector<T> &a){\n\tassert(!a.empty());\n\tT ret = a[0];\n\tfor (int i=0; i<(int)a.size(); i++) chmin(ret, a[i]);\n\treturn ret;\n}\n\ntemplate <typename T> T sum(vector<T> &a){\n\tT ret = 0;\n\tfor (int i=0; i<(int)a.size(); i++) ret += a[i];\n\treturn ret;\n}\n\n\n\nvoid solve(int N) {\n\tvector<vector<ll>> T(N);\n\tvector<ll> normal(N);\n\trep(i,0,N){\n\t\tll t;\n\t\tvector<ll> l(N);\n\t\tcin>>normal[i];\n\t\trep(i,0,N){\n\t\t\tcin>>l[i];\n\t\t}\n\t\tT[i]=l;\n\t}\n\tauto time=[&](ll b,ll stage){\n\t\tll res=normal[stage];\n\t\trep(i,0,N){\n\t\t\tif ((b>>i)&1){\n\t\t\t\tchmin(res,T[stage][i]);\n\t\t\t}\n\t\t}\n\t\treturn res;\n\t};\n\tvector<ll> dist(1<<N,1LL<<50);\n\tdist[0]=0;\n\trep(b,0,1<<N){\n\t\trep(i,0,N){\n\t\t\tif (((b>>i)&1)==0){\n\t\t\t\tll nb=b|(1<<i);\n\t\t\t\tchmin(dist[nb],dist[b]+time(b,i));\n\t\t\t}\n\t\t}\n\t}\n\tll ans=dist[(1<<N)-1];\n\tcout<<ans<<endl;\n}\n\nint main(){\n cin.tie(0)->sync_with_stdio(0);\n\twhile(true) {\n\t\tint n; cin >> n;\n\t\tif (n==0) break;\n\t\tsolve(n);\n\t}\n}", "accuracy": 1, "time_ms": 330, "memory_kb": 3864, "score_of_the_acc": -0.0289, "final_rank": 5 }, { "submission_id": "aoj_2254_10417033", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll=long long;\nconst ll ILL=2167167167167167167;\nconst int INF=2100000000;\n#define rep(i,a,b) for (int i=(int)(a);i<(int)(b);i++)\n#define all(p) p.begin(),p.end()\ntemplate<class T> using _pq = priority_queue<T, vector<T>, greater<T>>;\ntemplate<class T> int LB(vector<T> &v,T a){return lower_bound(v.begin(),v.end(),a)-v.begin();}\ntemplate<class T> int UB(vector<T> &v,T a){return upper_bound(v.begin(),v.end(),a)-v.begin();}\ntemplate<class T> bool chmin(T &a,T b){if(b<a){a=b;return 1;}else return 0;}\ntemplate<class T> bool chmax(T &a,T b){if(a<b){a=b;return 1;}else return 0;}\ntemplate<class T> void So(vector<T> &v) {sort(v.begin(),v.end());}\ntemplate<class T> void Sore(vector<T> &v) {sort(v.begin(),v.end(),[](T x,T y){return x>y;});}\nbool yneos(bool a,bool upp=false){if(a){cout<<(upp?\"YES\\n\":\"Yes\\n\");}else{cout<<(upp?\"NO\\n\":\"No\\n\");}return a;}\ntemplate<class T> void vec_out(vector<T> &p,int ty=0){\n if(ty==2){cout<<'{';for(int i=0;i<(int)p.size();i++){if(i){cout<<\",\";}cout<<'\"'<<p[i]<<'\"';}cout<<\"}\\n\";}\n else{if(ty==1){cout<<p.size()<<\"\\n\";}for(int i=0;i<(int)(p.size());i++){if(i) cout<<\" \";cout<<p[i];}cout<<\"\\n\";}}\ntemplate<class T> T vec_min(vector<T> &a){assert(!a.empty());T ans=a[0];for(auto &x:a) chmin(ans,x);return ans;}\ntemplate<class T> T vec_max(vector<T> &a){assert(!a.empty());T ans=a[0];for(auto &x:a) chmax(ans,x);return ans;}\ntemplate<class T> T vec_sum(vector<T> &a){T ans=T(0);for(auto &x:a) ans+=x;return ans;}\nint pop_count(long long a){int res=0;while(a){res+=(a&1),a>>=1;}return res;}\ntemplate<class T> T square(T a){return a * a;}\n\nint N;\n\nvoid solve();\n// CITRUS CURIO CITY / FREDERIC\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n\n while (cin >> N, N) solve();\n}\n\nvoid solve(){\n vector T(N, vector<int>(1 << N, INF));\n rep(i, 0, N){\n int a;\n cin >> a;\n for (auto &x : T[i]) x = a;\n rep(j, 0, N){\n cin >> a;\n chmin(T[i][1 << j], a);\n }\n rep(j, 0, N) rep(k, 0, 1 << N) if (k & (1 << j)) chmin(T[i][k], T[i][k - (1 << j)]);\n }\n vector dp(1 << N, INF);\n dp[0] = 0;\n rep(i, 0, 1 << N) rep(j, 0, N) if ((i & (1 << j)) == 0){\n chmin(dp[i + (1 << j)], dp[i] + T[j][i]);\n }\n cout << dp.back() << \"\\n\";\n}", "accuracy": 1, "time_ms": 370, "memory_kb": 7636, "score_of_the_acc": -0.4187, "final_rank": 17 }, { "submission_id": "aoj_2254_9457501", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n#include <unistd.h>\n\nnamespace fastio {\nstatic constexpr uint32_t SZ = 1 << 17;\nchar ibuf[SZ];\nchar obuf[SZ];\nchar out[100];\n// pointer of ibuf, obuf\n\n\nuint32_t pil = 0, pir = 0, por = 0;\n\nstruct Pre {\n char num[10000][4];\n constexpr Pre() : num() {\n for (int i = 0; i < 10000; i++) {\n int n = i;\n for (int j = 3; j >= 0; j--) {\n num[i][j] = n % 10 | '0';\n n /= 10;\n }\n }\n }\n} constexpr pre;\n\ninline void load() {\n memmove(ibuf, ibuf + pil, pir - pil);\n pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin);\n pil = 0;\n if (pir < SZ)\n ibuf[pir++] = '\\n';\n}\n\ninline void flush() {\n fwrite(obuf, 1, por, stdout);\n por = 0;\n}\n\nvoid rd(char &c) {\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n}\n\nvoid rd(string &x) {\n x.clear();\n char c;\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n do {\n x += c;\n if (pil == pir)\n load();\n c = ibuf[pil++];\n } while (!isspace(c));\n}\n\ntemplate <typename T> void rd_real(T &x) {\n string s;\n rd(s);\n x = stod(s);\n}\n\ntemplate <typename T> void rd_integer(T &x) {\n if (pil + 100 > pir)\n load();\n char c;\n do\n c = ibuf[pil++];\n while (c < '-');\n bool minus = 0;\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (c == '-') {\n minus = 1, c = ibuf[pil++];\n }\n }\n x = 0;\n while ('0' <= c) {\n x = x * 10 + (c & 15), c = ibuf[pil++];\n }\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (minus)\n x = -x;\n }\n}\n\nvoid rd(int &x) {\n rd_integer(x);\n}\nvoid rd(ll &x) {\n rd_integer(x);\n}\nvoid rd(i128 &x) {\n rd_integer(x);\n}\nvoid rd(uint &x) {\n rd_integer(x);\n}\nvoid rd(ull &x) {\n rd_integer(x);\n}\nvoid rd(u128 &x) {\n rd_integer(x);\n}\nvoid rd(double &x) {\n rd_real(x);\n}\nvoid rd(long double &x) {\n rd_real(x);\n}\n\ntemplate <class T, class U> void rd(pair<T, U> &p) {\n return rd(p.first), rd(p.second);\n}\ntemplate <size_t N = 0, typename T> void rd_tuple(T &t) {\n if constexpr (N < std::tuple_size<T>::value) {\n auto &x = std::get<N>(t);\n rd(x);\n rd_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void rd(tuple<T...> &tpl) {\n rd_tuple(tpl);\n}\n\ntemplate <size_t N = 0, typename T> void rd(array<T, N> &x) {\n for (auto &d : x)\n rd(d);\n}\ntemplate <class T> void rd(vector<T> &x) {\n for (auto &d : x)\n rd(d);\n}\n\nvoid read() {}\ntemplate <class H, class... T> void read(H &h, T &...t) {\n rd(h), read(t...);\n}\n\nvoid wt(const char c) {\n if (por == SZ)\n flush();\n obuf[por++] = c;\n}\nvoid wt(const string s) {\n for (char c : s)\n wt(c);\n}\nvoid wt(const char *s) {\n size_t len = strlen(s);\n for (size_t i = 0; i < len; i++)\n wt(s[i]);\n}\n\ntemplate <typename T> void wt_integer(T x) {\n if (por > SZ - 100)\n flush();\n if (x < 0) {\n obuf[por++] = '-', x = -x;\n }\n int outi;\n for (outi = 96; x >= 10000; outi -= 4) {\n memcpy(out + outi, pre.num[x % 10000], 4);\n x /= 10000;\n }\n if (x >= 1000) {\n memcpy(obuf + por, pre.num[x], 4);\n por += 4;\n } else if (x >= 100) {\n memcpy(obuf + por, pre.num[x] + 1, 3);\n por += 3;\n } else if (x >= 10) {\n int q = (x * 103) >> 10;\n obuf[por] = q | '0';\n obuf[por + 1] = (x - q * 10) | '0';\n por += 2;\n } else\n obuf[por++] = x | '0';\n memcpy(obuf + por, out + outi + 4, 96 - outi);\n por += 96 - outi;\n}\n\ntemplate <typename T> void wt_real(T x) {\n ostringstream oss;\n oss << fixed << setprecision(15) << double(x);\n string s = oss.str();\n wt(s);\n}\n\nvoid wt(int x) {\n wt_integer(x);\n}\nvoid wt(ll x) {\n wt_integer(x);\n}\nvoid wt(i128 x) {\n wt_integer(x);\n}\nvoid wt(uint x) {\n wt_integer(x);\n}\nvoid wt(ull x) {\n wt_integer(x);\n}\nvoid wt(u128 x) {\n wt_integer(x);\n}\nvoid wt(double x) {\n wt_real(x);\n}\nvoid wt(long double x) {\n wt_real(x);\n}\n\ntemplate <class T, class U> void wt(const pair<T, U> val) {\n wt(val.first);\n wt(' ');\n wt(val.second);\n}\ntemplate <size_t N = 0, typename T> void wt_tuple(const T t) {\n if constexpr (N < std::tuple_size<T>::value) {\n if constexpr (N > 0) {\n wt(' ');\n }\n const auto x = std::get<N>(t);\n wt(x);\n wt_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void wt(tuple<T...> tpl) {\n wt_tuple(tpl);\n}\ntemplate <class T, size_t S> void wt(const array<T, S> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\ntemplate <class T> void wt(const vector<T> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\n\nvoid print() {\n wt('\\n');\n}\ntemplate <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {\n wt(head);\n if (sizeof...(Tail))\n wt(' ');\n print(forward<Tail>(tail)...);\n}\nvoid __attribute__((destructor)) _d() {\n flush();\n}\n} // namespace fastio\n\n\nusing fastio::flush;\nusing fastio::print;\nusing fastio::read;\n\ninline void first(bool i = true) {\n print(i ? \"first\" : \"second\");\n}\ninline void Alice(bool i = true) {\n print(i ? \"Alice\" : \"Bob\");\n}\ninline void Takahashi(bool i = true) {\n print(i ? \"Takahashi\" : \"Aoki\");\n}\ninline void yes(bool i = true) {\n print(i ? \"yes\" : \"no\");\n}\ninline void Yes(bool i = true) {\n print(i ? \"Yes\" : \"No\");\n}\ninline void No() {\n print(\"No\");\n}\ninline void YES(bool i = true) {\n print(i ? \"YES\" : \"NO\");\n}\ninline void NO() {\n print(\"NO\");\n}\ninline void Yay(bool i = true) {\n print(i ? \"Yay!\" : \":(\");\n}\ninline void Possible(bool i = true) {\n print(i ? \"Possible\" : \"Impossible\");\n}\ninline void POSSIBLE(bool i = true) {\n print(i ? \"POSSIBLE\" : \"IMPOSSIBLE\");\n}\n\n/**\n * @brief Fast IO\n */\n\nint main() {\nwhile(1) {\n int N;\n cin >> N;\n if (N == 0) return 0;\n vector<vector<int>> T(N,vector<int>(N));\n vector<int> X(N);\n rep(i,0,N) {\n cin >> X[i];\n rep(j,0,N) cin >> T[i][j];\n }\n vector<vector<int>> C(1<<N,vector<int>(N));\n rep(i,0,1<<N) {\n rep(j,0,N) {\n C[i][j] = X[j];\n rep(k,0,N) {\n if ((i>>k)&1) chmin(C[i][j],T[j][k]);\n }\n }\n }\n vector<int> DP(1<<N,inf);\n DP[0] = 0;\n rep(i,0,1<<N) {\n rep(k,0,N) {\n if (((i>>k)&1)==0) {\n chmin(DP[i|(1<<k)],DP[i]+C[i][k]);\n }\n }\n }\n cout << DP[(1<<N)-1] << endl;;\n}\n}", "accuracy": 1, "time_ms": 1240, "memory_kb": 10328, "score_of_the_acc": -1.2397, "final_rank": 19 }, { "submission_id": "aoj_2254_9366864", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define rep(i,k,n) for (int i = k; i < (n); ++i)\nconst long long MOD1 = 1000000007;\nconst long long MOD2 = 998244353;\ntypedef long long ll;\ntypedef pair<ll, ll> P;\nconst long long INF = 1e17;\n\nint main(){\n while(true){\n int n;\n cin >> n;\n if(n==0){\n break;\n }\n //t[i][j]:i番目のステージをj番目の装備で\n //攻略する時間(j=0は装備なし)\n vector t(n+10,vector<int>(n+10));\n\n for(int i=1;i<=n;i++){\n for(int j=0;j<=n;j++){\n cin >> t[i][j];\n }\n }\n\n //dp[S]:Sをクリアするのに必要な最小時間\n vector<int> dp(1<<n,1e9);\n dp[0] = 0;\n for(int s=0;s<1<<n;s++){\n for(int x=0;x<n;x++){\n if(s&(1<<x)){\n int ss = s^(1<<x);\n dp[s] = min(dp[s],dp[ss]+t[x+1][0]);\n for(int y=0;y<n;y++){\n if(ss&(1<<y))dp[s] = min(dp[s],dp[ss]+t[x+1][y+1]);\n }\n }\n }\n }\n\n cout << dp[(1<<n)-1] << endl;\n\n\n }\n return 0;\n}", "accuracy": 1, "time_ms": 510, "memory_kb": 3712, "score_of_the_acc": -0.1304, "final_rank": 15 }, { "submission_id": "aoj_2254_9363295", "code_snippet": "#include <bits/stdc++.h>\n#define rep(i, n) for (int i = 0; i < (int)(n); i++)\n#define all(x) (x).begin(),(x).end()\n#define eb emplace_back\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing vl = vector<long long>;\nusing vvl = vector<vector<long long>>; using vs = vector<string>;\nusing pl = pair<long long, long long>;\ntemplate <typename T> inline bool chmin(T& a, const T& b) {bool compare = a > b; if (a > b) a = b; return compare;}\ntemplate <typename T> inline bool chmax(T& a, const T& b) {bool compare = a < b; if (a < b) a = b; return compare;}\ntemplate<class T>using rp_queue=priority_queue<T,vector<T>,greater<T>>;\ntemplate <typename T> T gcd(T a, T b) {if (b == 0)return a; else return gcd(b, a % b);}\ntemplate <typename T> inline T lcm(T a, T b) {return a /gcd(a, b)*b;}\nconst ll INF = 1LL << 60;\nconst ld PI = acos(-1);\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::vector<T> &vec);\ntemplate <class OStream, class T, size_t sz> OStream &operator<<(OStream &os, const std::array<T, sz> &arr);\ntemplate <class OStream, class T, class TH> OStream &operator<<(OStream &os, const std::unordered_set<T, TH> &vec);\ntemplate <class OStream, class T, class U> OStream &operator<<(OStream &os, const pair<T, U> &pa);\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::deque<T> &vec);\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::set<T> &vec);\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::multiset<T> &vec);\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::unordered_multiset<T> &vec);\ntemplate <class OStream, class T, class U> OStream &operator<<(OStream &os, const std::pair<T, U> &pa);\ntemplate <class OStream, class TK, class TV> OStream &operator<<(OStream &os, const std::map<TK, TV> &mp);\ntemplate <class OStream, class TK, class TV, class TH> OStream &operator<<(OStream &os, const std::unordered_map<TK, TV, TH> &mp);\ntemplate <class OStream, class... T> OStream &operator<<(OStream &os, const std::tuple<T...> &tpl);\n \ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::vector<T> &vec) { os << '['; for (auto v : vec) os << v << ','; os << ']'; return os; }\ntemplate <class OStream, class T, size_t sz> OStream &operator<<(OStream &os, const std::array<T, sz> &arr) { os << '['; for (auto v : arr) os << v << ','; os << ']'; return os; }\ntemplate <class... T> std::istream &operator>>(std::istream &is, std::tuple<T...> &tpl) { std::apply([&is](auto &&... args) { ((is >> args), ...);}, tpl); return is; }\ntemplate <class OStream, class... T> OStream &operator<<(OStream &os, const std::tuple<T...> &tpl) { os << '('; std::apply([&os](auto &&... args) { ((os << args << ','), ...);}, tpl); return os << ')'; }\ntemplate <class OStream, class T, class TH> OStream &operator<<(OStream &os, const std::unordered_set<T, TH> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::deque<T> &vec) { os << \"deq[\"; for (auto v : vec) os << v << ','; os << ']'; return os; }\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::set<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::multiset<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::unordered_multiset<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }\ntemplate <class OStream, class T, class U> OStream &operator<<(OStream &os, const std::pair<T, U> &pa) { return os << '(' << pa.first << ',' << pa.second << ')'; }\ntemplate <class OStream, class TK, class TV> OStream &operator<<(OStream &os, const std::map<TK, TV> &mp) { os << '{'; for (auto v : mp) os << v.first << \"=>\" << v.second << ','; os << '}'; return os; }\ntemplate <class OStream, class TK, class TV, class TH> OStream &operator<<(OStream &os, const std::unordered_map<TK, TV, TH> &mp) { os << '{'; for (auto v : mp) os << v.first << \"=>\" << v.second << ','; os << '}'; return os; }\n#ifdef LOCAL\nconst string COLOR_RESET = \"\\033[0m\", BRIGHT_GREEN = \"\\033[1;32m\", BRIGHT_RED = \"\\033[1;31m\", BRIGHT_CYAN = \"\\033[1;36m\", NORMAL_CROSSED = \"\\033[0;9;37m\", RED_BACKGROUND = \"\\033[1;41m\", NORMAL_FAINT = \"\\033[0;2m\";\n#define dbg(x) std::cerr << BRIGHT_CYAN << #x << COLOR_RESET << \" = \" << (x) << NORMAL_FAINT << \" (L\" << __LINE__ << \") \" << __FILE__ << COLOR_RESET << std::endl\n#define dbgif(cond, x) ((cond) ? std::cerr << BRIGHT_CYAN << #x << COLOR_RESET << \" = \" << (x) << NORMAL_FAINT << \" (L\" << __LINE__ << \") \" << __FILE__ << COLOR_RESET << std::endl : std::cerr)\n#else\n#define dbg(x) ((void)0)\n#define dbgif(cond, x) ((void)0)\n#endif\nvoid solve();\nint main(){\n cin.tie(nullptr);\n ios_base::sync_with_stdio(false);\n cout<<fixed<<setprecision(15);\n int num_tc = 1;\n //cin >> num_tc;\n rep(tc,num_tc){\n //cout << \"Case #\" << tc+1 << \": \" ;// << endl;\n solve();\n }\n}\n//見切り発車で実装しては行けない.頭の中でコードが完全に出来上がるまで書き始めるな.\nll t[20][20];\nll dp[1ll<<16];\nint N;\nvoid solve(){\n while(true){\n cin>>N;\n if(N==0)return;\n rep(i,(1ll<<16))dp[i]=INF;\n rep(i,N)rep(j,N+1)cin>>t[i][j];\n //dp[S] = 集合Sの装備を持っている時の最小値\n //dp[(1ll<<N)-1]\n dp[0] = 0;\n rep(S,(1ll<<N)){\n rep(i,N){\n if(S&(1ll<<i))continue;\n ll now = t[i][0];\n rep(j,N)if(S&(1ll<<j)){\n chmin(now,t[i][j+1]);\n }\n chmin(dp[S|(1ll<<i)],now+dp[S]);\n }\n }\n cout<<dp[(1ll<<N)-1]<<endl;\n }\n}", "accuracy": 1, "time_ms": 430, "memory_kb": 3896, "score_of_the_acc": -0.0966, "final_rank": 12 }, { "submission_id": "aoj_2254_9349701", "code_snippet": "#include<bits/stdc++.h>\n//#include <atcoder/all>\nusing namespace std;\n//using namespace atcoder;\n\n#define all(v) v.begin(),v.end()\nusing ll = long long;\nusing ull = unsigned long long;\nusing vll=vector<ll>;\nusing vvll = vector<vector<ll>>;\nusing P = pair<ll,ll>;\nusing vp=vector<pair<ll, ll>>;\n//using mint=modint1000000007;\n//using mint=modint998244353;\n\nconst ll INF=1ll<<60;\nll mod10=1e9+7;\nll mod99=998244353;\nconst double PI = acos(-1);\n\n#define rep(i,n) for (ll i=0;i<n;++i)\n#define per(i,n) for(ll i=n-1;i>=0;--i)\n#define rep2(i,a,n) for (ll i=a;i<n;++i)\n#define per2(i,a,n) for (ll i=a;i>=n;--i)\n\ntemplate<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }\ntemplate<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }\n\nbool solve(){\n ll N;cin>>N;\n if(N==0) return 0;\n vvll T(N,vll(N+1));\n rep(i,N) rep(j,N+1) cin>>T[i][j];\n\n vll dp(1<<N,INF);\n dp[0]=0;\n rep(i,(1<<N)){\n rep(j,N){\n if(i>>j&1) continue;\n ll mi=T[j][0];\n rep(k,N){\n if(i>>k&1) chmin(mi,T[j][k+1]);\n }\n chmin(dp[i|(1<<j)],dp[i]+mi);\n }\n }\n cout << dp[(1<<N)-1] << endl;\n return 1;\n}\n\n\nint main(){\n cin.tie(0);\n ios::sync_with_stdio(false);\n while(solve());\n}", "accuracy": 1, "time_ms": 370, "memory_kb": 3672, "score_of_the_acc": -0.0362, "final_rank": 6 }, { "submission_id": "aoj_2254_9302532", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\ntypedef long long ll;\n\nint main() {\n ios::sync_with_stdio(false); //\n cin.tie(nullptr); //入力高速化\n\n while (true){\n int n;\n cin >> n;\n if (n==0)break;\n vector<vector<int>> T(n+1, vector<int>(n + 1));\n for (int i = 1; i < n+1;i++){\n for (int j = 0; j < n + 1; j++){\n cin >> T[i][j];\n }\n }\n vector<ll> dp;\n\n ll inf = 100000000000;\n\n\n\n ll num=(1<<(n+1));\n\n for (int S= 0; S< num; S++){\n dp.emplace_back(inf);\n }\n dp[1] = 0;\n \n\n for (int S = 0; S < num; S++){\n if (S%2==0)\n continue;\n for (int v = 0; v < n+1; v++){\n if ((S&(1<<v))==0)continue;\n for (int u = 0; u < n + 1; u++){\n if ((S&(1<<u))!=0)continue;\n dp[S+(1<<u)]=min(dp[S+(1<<u)],dp[S]+T[u][v]);\n\n }\n }\n }\n cout << dp[num - 1] << endl;\n }\n\n\n\n\n \n return 0;\n}", "accuracy": 1, "time_ms": 410, "memory_kb": 5168, "score_of_the_acc": -0.2064, "final_rank": 16 }, { "submission_id": "aoj_2254_9302461", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nconst int INF = (int)1e9 + 1001010;\nconst ll llINF = (long long)4e18 + 22000020;\nconst string endn = \"\\n\";\ntemplate <class T> inline auto vector2(size_t i, size_t j, const T &init = T()) {return vector(i, vector<T>(j, init));}\nconst string ELEM_SEPARATION = \" \", VEC_SEPARATION = endn;\ntemplate<class T> istream& operator >>(istream &i, vector<T> &A) {for(auto &I : A) {i >> I;} return i;}\ntemplate<class T> ostream& operator <<(ostream &o, const vector<T> &A) {int i=A.size(); for(auto &I : A){o << I << (--i ? ELEM_SEPARATION : \"\");} return o;}\ntemplate<class T> ostream& operator <<(ostream &o, const vector<vector<T>> &A) {int i=A.size(); for(auto &I : A){o << I << (--i ? VEC_SEPARATION : \"\");} return o;}\ntemplate<class T> vector<T>& operator ++(vector<T> &A, int n) {for(auto &I : A) {I++;} return A;}\ntemplate<class T> vector<T>& operator --(vector<T> &A, int n) {for(auto &I : A) {I--;} return A;}\ntemplate<class T, class U> bool chmax(T &a, const U &b) {return ((a < b) ? (a = b, true) : false);}\ntemplate<class T, class U> bool chmin(T &a, const U &b) {return ((a > b) ? (a = b, true) : false);}\nll floor(ll a, ll b){if (b < 0) a = -a, b = -b; if(a >= 0) return a / b; else return (a + 1) / b - 1;}\nll ceil(ll a, ll b){if (b < 0) a = -a, b = -b; if(a > 0) return (a - 1) / b + 1; else return a / b;}\nll bit(unsigned long long val, unsigned long long digit){return (val >> digit) & 1;}\n#ifdef DEBUG\n#include <debug_slephy.cpp>\n#else\n#define debug(...)\n#endif\n// ================================== ここまでテンプレ ==================================\n\nvoid solve(){\n ll n; cin >> n;\n if(n == 0) exit(0);\n vector<ll> default_t(n);\n auto t = vector2<ll>(n, n);\n for(int i = 0; i < n; i++){\n cin >> default_t[i];\n cin >> t[i];\n }\n\n auto dp = vector<ll>(1<<n, llINF);\n dp[0] = 0;\n for(int bt = 0; bt < (1<<n); bt++){\n for(int add = 0; add < n; add++){\n if(bit(bt, add)) continue;\n ll cost = default_t[add];\n\n for(int j = 0; j < n; j++){\n if(bit(bt, j)){\n chmin(cost, t[add][j]);\n }\n }\n\n ll nbt = bt | (1<<add);\n chmin(dp[nbt], dp[bt] + cost);\n }\n }\n\n ll ans = dp[(1<<n)-1];\n cout << ans << endl;\n}\n\nint main(int argc, char *argv[]){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n\n while(true) solve();\n return 0;\n}", "accuracy": 1, "time_ms": 360, "memory_kb": 3904, "score_of_the_acc": -0.0522, "final_rank": 8 }, { "submission_id": "aoj_2254_9137558", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nvoid chmin(int&a, int b){if(a>b)a=b;}\nint N,T[17][17];\nint dp[1<<16];\nbool solve()\n{\n int N;\n cin>>N;\n if(N==0)return 0;\n for(int i=0;i<N;i++)for(int j=0;j<=N;j++)cin>>T[i][j];\n for(int i=0;i<(1<<N);i++)dp[i]=1<<30;\n dp[0]=0;\n for(int i=1;i<(1<<N);i++)\n {\n for(int j=0;j<N;j++)if(i>>j&1)\n {\n int J=1<<j;\n chmin(dp[i],dp[i^J]+T[j][0]);\n for(int k=0;k<N;k++)if((i>>k&1)==0)chmin(dp[i],dp[i^J]+T[j][k+1]);\n }\n }\n cout<<dp[(1<<N)-1]<<endl;\n return 1;\n}\nint main()\n{\n while(solve()){}\n}", "accuracy": 1, "time_ms": 480, "memory_kb": 3564, "score_of_the_acc": -0.0968, "final_rank": 13 }, { "submission_id": "aoj_2254_8980284", "code_snippet": "// (⁠◕⁠ᴗ⁠◕⁠✿⁠)\n\n// #pragma GCC target(\"avx2\")\n// #pragma GCC optimize(\"O3\")\n// #pragma GCC optimize(\"unroll-loops\")\n#include <bits/stdc++.h>\n#define rep(i, n) for (ll i = 0; i < (n); i++)\n#define srep(i, s, n) for (ll i = s; i < (n); i++)\n#define len(x) ((int)(x).size())\n#define all(x) (x).begin(), (x).end()\nusing namespace std;\ntemplate<typename T> using vc = vector<T>;\ntemplate<typename T> using vv = vc<vc<T>>;\ntemplate<typename T> using vvv = vv<vc<T>>;\nusing vi = vc<int>;using vvi = vv<int>; using vvvi = vv<vi>;\nusing ll = long long;using vl = vc<ll>;using vvl = vv<ll>; using vvvl = vv<vl>;\nusing ld = long double; using vld = vc<ld>; using vvld = vc<vld>; using vvvld = vc<vvld>;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nconst ld pi = 3.141592653589793;\nconst int inf = 0x3f3f3f3f;\nconst ll INF = 0x3f3f3f3f3f3f3f3f;\n// const ll mod = 1000000007;\nconst ll mod = 998244353;\ninline bool inside(ll y, ll x, ll H, ll W) {return 0 <= (y) and (y) < (H) and 0 <= (x) and (x) < (W); }\n\n#define debug(var) do{std::cout << #var << \" : \\n\";view(var);}while(0)\ntemplate<typename T> void view(T e){cout << e << endl;}\ntemplate<typename T> void view(const vc<T>& v){for(const auto& e : v){ cout << e << \" \"; } cout << endl;}\ntemplate<typename T> void view(const vv<T>& vv){ for(const auto& v : vv){ view(v); } }\n\nint solve(int N){\n vvi T(N, vi(N + 1)); rep(i, N) rep(j, N + 1) cin >> T[i][j];\n vi dp(1 << N, inf);\n dp[0] = 0;\n rep(cmb, 1 << N){\n rep(i, N) if (~cmb >> i & 1){\n dp[cmb | (1 << i)] = min(dp[cmb | (1 << i)], dp[cmb] + T[i][0]);\n rep(j, N) if (cmb >> j & 1) dp[cmb | (1 << i)] = min(dp[cmb | (1 << i)], dp[cmb] + T[i][j + 1]);\n }\n }\n return dp[(1 << N) - 1];\n}\n\nint main(){\n vc<int> ans;\n while (true){\n int N; cin >> N;\n if (N == 0) break;\n ans.push_back(solve(N));\n }\n for (auto x : ans) cout << x << endl;\n}", "accuracy": 1, "time_ms": 440, "memory_kb": 3696, "score_of_the_acc": -0.0837, "final_rank": 11 }, { "submission_id": "aoj_2254_8497866", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nbool solve() {\n int n; std::cin >> n;\n if (!n) return false;\n std::vector t(n, std::vector<int>(n + 1));\n for (auto& v : t) for (auto& x : v) std::cin >> x;\n const int INF{(int)1e9};\n std::vector dp(1 << n, INF);\n dp[0] = 0;\n for (int bit{} ; bit < (1 << n) ; bit++) {\n for (int i{} ; i < n ; i++) if (!(bit & (1 << i))) {\n int nxt{bit | (1 << i)};\n dp[nxt] = std::min(dp[nxt], dp[bit] + t[i][0]);\n for (int j{} ; j < n ; j++) if (bit & (1 << j)) {\n dp[nxt] = std::min(dp[nxt], dp[bit] + t[i][j + 1]);\n }\n }\n }\n int ans{dp[(1 << n) - 1]};\n std::cout << ans << '\\n';\n return true;\n}\n\nint main() {\n std::cin.tie(nullptr)->sync_with_stdio(false);\n for (int i{} ; solve() ; i++) {\n }\n}", "accuracy": 1, "time_ms": 400, "memory_kb": 3716, "score_of_the_acc": -0.0598, "final_rank": 9 }, { "submission_id": "aoj_2254_8437465", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nconst int INF = (1<<29);\nint n, t[20][20], memo[(1<<17)];\nint dfs(int num, int bit){\n if(num == 0) return 0;\n if(memo[bit]>=0) return memo[bit]; \n int res = INF;\n for(int i=0;i<n;i++){\n if((bit&1<<i)>0) continue;\n int prt = t[i][0];\n for(int j=0;j<n;j++){\n if((bit&1<<j)>0) prt = min(prt, t[i][j+1]);\n }\n res = min(res, dfs(num-1, (bit|1<<i))+prt);\n }\n return memo[bit] = res;\n}\nint main(){\n while(cin >> n, n){\n for(int i=0;i<n;i++){\n for(int j=0;j<=n;j++){\n cin >> t[i][j];\n }\n }\n memset(memo, -1, sizeof(memo));\n cout << dfs(n, 0) << endl;\n }\n return(0);\n}", "accuracy": 1, "time_ms": 350, "memory_kb": 3584, "score_of_the_acc": -0.0148, "final_rank": 3 }, { "submission_id": "aoj_2254_8194484", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing i64 = long long;\n\nint main() {\n cin.tie(nullptr)->sync_with_stdio(false);\n \n int n;\n\n auto solve = [&]() {\n vector a(n, vector<int>(n + 1));\n for (int i = 0; i < n; i++) {\n for (int j = 0; j <= n; j++) {\n cin >> a[i][j];\n }\n }\n\n constexpr int INF = 1e9;\n\n vector dp(1 << n, INF);\n dp[0] = 0;\n\n for (int s = 0; s < 1 << n; s++) {\n for (int i = 0; i < n; i++) {\n if (s >> i & 1) {\n continue;\n }\n int ns = s | 1 << i;\n int mn = a[i][0];\n for (int j = 0; j < n; j++) {\n if (s >> j & 1) {\n mn = min(mn, a[i][j + 1]);\n }\n }\n dp[ns] = min(dp[ns], dp[s] + mn);\n }\n }\n\n cout << dp.back() << '\\n';\n };\n\n while (cin >> n && n != 0) {\n solve();\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 340, "memory_kb": 3608, "score_of_the_acc": -0.0107, "final_rank": 2 }, { "submission_id": "aoj_2254_8024344", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)\n#define repn(i,end) for(long long i = 0; i <= (long long)(end); i++)\n#define reps(i,start,end) for(long long i = start; i < (long long)(end); i++)\n#define repsn(i,start,end) for(long long i = start; i <= (long long)(end); i++)\n#define ll long long\n#define ld long double\n#define print(t) cout << t << endl \n#define all(a) (a).begin(),(a).end()\n// << std::fixed << std::setprecision(0)\nconst ll INF = 1LL << 60;\n \ntemplate<class T> bool chmin(T& a, T b){\n if(a > b){\n\ta = b;\n\treturn true;\n }\n\treturn false;\n}\n \ntemplate<class T> bool chmax(T& a, T b){\n if(a < b){\n\ta = b;\n\treturn true;\n }\n\treturn false;\n}\n\t\nll lpow(ll x,ll n){\n\tll ans = 1;\n\twhile(n >0){\n\t\tif(n & 1)ans *= x;\n\t\tx *= x;\n\t\tn >>= 1;\n\t}\n\treturn ans;\n}\n \nint main(){\n\twhile(true){\n\t\tll n;cin >> n;\n\t\tif(n == 0){\n\t\t\tbreak;\n\t\t}\n\t\tvector<ll> base_time(n);\n\t\tvector<vector<ll>> use_time(n,vector<ll>(n));\n\t\trep(i,n){\n\t\t\trep(j,n+1){\n\t\t\t\tif(j == 0)cin >> base_time[i];\n\t\t\t\telse cin >> use_time[i][j-1];\n\t\t\t}\n\t\t}\n\t\tvector<vector<ll>> dp(1 << n,vector<ll>(n,INF));\n\t\tdp[0][0] = 0;\n\t\trep(i,n){\n\t\t\tdp[1 << i][i] = base_time[i];\n\t\t}\n\t\treps(i,1,n){\n\t\t\trep(j,1<<n){\n\t\t\t\tif(__builtin_popcount(j) != i)continue;\n\t\t\t\trep(k,n){\n\t\t\t\t\tif(~j >> k & 1)continue;\n\t\t\t\t\t//行き先\n\t\t\t\t\trep(l,n){\n\t\t\t\t\t\tif(~j >> l & 1){\n\t\t\t\t\t\t\tll best_choice = base_time[l];\n\t\t\t\t\t\t\trep(z,n){\n\t\t\t\t\t\t\t\tif(j >> z & 1){\n\t\t\t\t\t\t\t\t\tchmin(best_choice,use_time[l][z]);\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tchmin(dp[j + (1LL<<l)][l],dp[j][k] + best_choice);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tll ans = INF;\n\t\trep(i,n){\n\t\t\tchmin(ans,dp[(1LL <<n) - 1][i]);\n\t\t}\n\t\tcout << ans << endl;\n\n\t}\n}", "accuracy": 1, "time_ms": 1880, "memory_kb": 13928, "score_of_the_acc": -2, "final_rank": 20 } ]
aoj_2255_cpp
6÷2(1+2) English text is not available in this practice contest. 数学科の学生であるきたまさ君は,数学はとても得意なのだが算数はあまり得意ではない. 特に演算子の優先順位が苦手で, カッコの中を先に計算するというのはわかるのだが, "+" と "*" のどちらを優先したら良いかとか複数の演算子が並んでいるときに左から計算したら良いのか右から計算したら良いのか覚えておらず, その時の気分で好きな順番で計算をしている.例えば,"1*1-1+1"という数式を前から"((1*1)-1)+1"という順番で計算する場合もあれば,真ん中から"(1*(1-1))+1"という順番で計算する場合さえある. また,分数や小数も苦手で,割り算では常に絶対値の小さい方に丸め,小数部を切り捨てて整数にしてしまう. 割る数がゼロであった場合には,きたまさ君はどこかで間違えたと思ってもう一度最初から計算しなおすことにしている. きたまさ君はどんな順番で計算しても最終的な計算結果は同じになると主張しているので,あなたにはそれが間違いであると示すために,与えられた数式に対して きたまさ君の計算結果が何通りあり得るかを求めるプログラムを書いて欲しい. ただし,演算の順番が異なっていても,最終的な答えが同じならば一つと数えるものとし, また,与えられる数式は,少なくとも一つはゼロ割が発生しない演算順序が存在し,いかなる演算順序で計算した場合でも,計算結果と途中で現れる数の絶対値は常に 10 9 以下であることが保証されている. Input 入力はひとつ以上の行からなり,入力の各行は数式ひとつを含む.数式の文法は以下の BNF で与えられる. <expr> ::= <num> | "(" <expr> ")" | <expr> "+" <expr> | <expr> "-" <expr> | <expr> "*" <expr> | <expr> "/" <expr> <num> ::= <digit> | <num> <digit> <digit> ::= "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" すべての数式はこの構文規則に従う.また,入力行の長さは 200 文字を超えない.ひとつの数式が含む演算子 ( "+" , "-" , "*" , "/" ) の数は 10 個を超えない. 入力の終わりは "#" ひとつだけからなる行によって示される. Output それぞれの数式について,きたまさ君の計算結果が何通りあり得るかを出力せよ.出力は数式ひとつごとに 1 行とする. Sample Input 6/2*(1+2) 1-1-1 (1-1-1)/2 # Output for the Sample Input 2 2 1
[ { "submission_id": "aoj_2255_10685115", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nbool solve();\n\nusing ll = long long;\n#define rep(i, a, b) for (int i = a; i < (b); ++i)\n#define sz(x) (int)(x).size()\n#define all(x) x.begin(), x.end()\ntypedef pair<int, int> pii;\ntypedef vector<int> vi;\ntemplate <class T>\ninline bool chmin(T &a, T b) {\n return ((a > b) ? (a = b, 1) : (0));\n}\ntemplate <class T>\ninline bool chmax(T &a, T b) {\n return ((a < b) ? (a = b, 1) : (0));\n}\n\nint main() {\n while (solve());\n return 0;\n}\n\nvector<vi> G;\nvector<pair<int, char>> val_op;\nint tidx = 0;\n\n// 木を構成\nint number(const string &s, int &i) {\n int ret = 0;\n while (i < sz(s) && isdigit(s[i])) {\n ret = 10 * ret + (s[i] - '0');\n i++;\n }\n val_op.emplace_back(ret, 'd');\n G.push_back({});\n tidx++;\n return tidx - 1;\n}\nint expr(const string &s, int &i) {\n val_op.emplace_back(-1, 'r'); //()をあらわす\n G.push_back({});\n int p = tidx;\n tidx++;\n vi c; // 子 演算子,expr\n while (i < sz(s)) {\n if (s[i] == '(') {\n assert(s[i] == '(');\n i++; // (\n int cp = expr(s, i);\n G[p].push_back(cp);\n G[cp].push_back(p);\n assert(s[i] == ')');\n i++; // )\n } else {\n assert(isdigit(s[i]));\n int cp = number(s, i);\n G[p].push_back(cp);\n G[cp].push_back(p);\n }\n if (i >= sz(s)) return p;\n if (s[i] == ')') return p;\n bool f = 0;\n for (auto chara : \"+-*/\")\n if (chara == s[i]) f = 1;\n assert(f);\n if (!f) return p;\n int opp = tidx;\n G.push_back({});\n val_op.emplace_back(-1, s[i]);\n tidx++;\n G[p].push_back(opp);\n G[opp].push_back(p);\n i++;\n }\n return p;\n}\n\nbool isop(char c) {\n bool f = 0;\n for (char cc : \"+-*/\")\n if (cc == c) f = 1;\n return f;\n}\nbool solve() {\n tidx = 0;\n G.clear();\n val_op.clear();\n string s;\n cin >> s;\n if (s == \"#\") return 0;\n int root = 0;\n {\n int i = 0;\n root = expr(s, i);\n }\n\n vector<set<int>> st(tidx);\n auto dfs = [&](auto dfs, int v, int p) -> char {\n if (val_op[v].second == 'r') {\n vector<char> op;\n vector<int> sidx;\n for (auto to : G[v]) {\n if (to == p) continue;\n if (val_op[to].second != 'r' && val_op[to].second != 'd') {\n op.push_back(val_op[to].second);\n } else {\n dfs(dfs, to, v);\n sidx.push_back(to);\n assert(sz(st[to]) > 0);\n }\n }\n assert(sz(op) + 1 == sz(sidx));\n vector<vi> vv;\n {\n vi v;\n auto f1 = [&](auto f1, int i) -> void {\n if (i >= sz(sidx)) {\n vv.push_back(v);\n return;\n }\n for (auto x : st[sidx[i]]) {\n v.push_back(x);\n f1(f1, i + 1);\n v.pop_back();\n }\n };\n f1(f1, 0);\n }\n vi order(sz(op));\n iota(all(order), 0);\n do {\n for (auto vect : vv) {\n bool f=1;\n auto tmp=vect;\n vi tmp_c(sz(sidx), 1); // つかえるか\n // cout<<endl;\n rep(i, 0, sz(op)) {\n // for(auto x:tmp)cout<<x<<\" \";cout<<endl;\n int j = order[i];\n int l = j, r = j + 1;\n while (l >= 0 && !tmp_c[l]) l--;\n while (r < sz(sidx) && !tmp_c[r]) r++;\n assert(l >= 0);\n assert(r < sz(sidx));\n assert(isop(op[i]));\n assert(tmp_c[l]);\n assert(tmp_c[r]);\n int tl = tmp[l];\n int tr = tmp[r];\n if (op[j] == '+') tmp[l] = tl + tr;\n if (op[j] == '-') tmp[l] = tl - tr;\n if (op[j] == '*') tmp[l] = tl * tr;\n if (op[j] == '/'){\n if(tr==0){\n f=0;\n break;\n }\n tmp[l] = tl / tr;\n }\n tmp_c[r] = 0;\n tmp[r]=0;\n }\n if(f)st[v].insert(tmp[0]);\n }\n } while (next_permutation(all(order)));\n\n //assert(sz(st[v]) > 0);\n } else if (val_op[v].second == 'd') {\n st[v].insert(val_op[v].first);\n assert(sz(st[v]) > 0);\n }\n return val_op[v].second;\n };\n dfs(dfs, root, -1);\n cout << sz(st[root]) << endl;\n return 1;\n}", "accuracy": 1, "time_ms": 390, "memory_kb": 3584, "score_of_the_acc": -0.2002, "final_rank": 3 }, { "submission_id": "aoj_2255_10685088", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nbool solve();\n\nusing ll = long long;\n#define rep(i, a, b) for (int i = a; i < (b); ++i)\n#define sz(x) (int)(x).size()\n#define all(x) x.begin(), x.end()\ntypedef pair<int, int> pii;\ntypedef vector<int> vi;\ntemplate <class T>\ninline bool chmin(T &a, T b) {\n return ((a > b) ? (a = b, 1) : (0));\n}\ntemplate <class T>\ninline bool chmax(T &a, T b) {\n return ((a < b) ? (a = b, 1) : (0));\n}\n\nint main() {\n while (solve());\n return 0;\n}\n\nvector<vi> G;\nvector<pair<int, char>> val_op;\nint tidx = 0;\n\n// 木を構成\nint number(const string &s, int &i) {\n int ret = 0;\n while (i < sz(s) && isdigit(s[i])) {\n ret = 10 * ret + (s[i] - '0');\n i++;\n }\n val_op.emplace_back(ret, 'd');\n G.push_back({});\n tidx++;\n return tidx - 1;\n}\nint expr(const string &s, int &i) {\n val_op.emplace_back(-1, 'r'); //()をあらわす\n G.push_back({});\n int p = tidx;\n tidx++;\n vi c; // 子 演算子,expr\n while (i < sz(s)) {\n if (s[i] == '(') {\n assert(s[i] == '(');\n i++; // (\n int cp = expr(s, i);\n G[p].push_back(cp);\n G[cp].push_back(p);\n assert(s[i] == ')');\n i++; // )\n } else {\n assert(isdigit(s[i]));\n int cp = number(s, i);\n G[p].push_back(cp);\n G[cp].push_back(p);\n }\n if (i >= sz(s)) return p;\n if (s[i] == ')') return p;\n bool f = 0;\n for (auto chara : \"+-*/\")\n if (chara == s[i]) f = 1;\n assert(f);\n if (!f) return p;\n int opp = tidx;\n G.push_back({});\n val_op.emplace_back(-1, s[i]);\n tidx++;\n G[p].push_back(opp);\n G[opp].push_back(p);\n i++;\n }\n return p;\n}\n\nbool isop(char c) {\n bool f = 0;\n for (char cc : \"+-*/\")\n if (cc == c) f = 1;\n return f;\n}\nbool solve() {\n tidx = 0;\n G.clear();\n val_op.clear();\n string s;\n cin >> s;\n if (s == \"#\") return 0;\n int root = 0;\n {\n int i = 0;\n root = expr(s, i);\n }\n\n vector<set<int>> st(tidx);\n auto dfs = [&](auto dfs, int v, int p) -> char {\n if (val_op[v].second == 'r') {\n vector<char> op;\n vector<int> sidx;\n for (auto to : G[v]) {\n if (to == p) continue;\n if (val_op[to].second != 'r' && val_op[to].second != 'd') {\n op.push_back(val_op[to].second);\n } else {\n dfs(dfs, to, v);\n sidx.push_back(to);\n assert(sz(st[to]) > 0);\n }\n }\n assert(sz(op) + 1 == sz(sidx));\n vector<vi> vv;\n {\n vi v;\n auto f1 = [&](auto f1, int i) -> void {\n if (i >= sz(sidx)) {\n vv.push_back(v);\n return;\n }\n for (auto x : st[sidx[i]]) {\n v.push_back(x);\n f1(f1, i + 1);\n v.pop_back();\n }\n };\n f1(f1, 0);\n }\n vi order(sz(op));\n iota(all(order), 0);\n do {\n bool f=1;\n for (auto vect : vv) {\n auto tmp=vect;\n vi tmp_c(sz(sidx), 1); // つかえるか\n // cout<<endl;\n rep(i, 0, sz(op)) {\n // for(auto x:tmp)cout<<x<<\" \";cout<<endl;\n int j = order[i];\n int l = j, r = j + 1;\n while (l >= 0 && !tmp_c[l]) l--;\n while (r < sz(sidx) && !tmp_c[r]) r++;\n assert(l >= 0);\n assert(r < sz(sidx));\n assert(isop(op[i]));\n assert(tmp_c[l]);\n assert(tmp_c[r]);\n int tl = tmp[l];\n int tr = tmp[r];\n if (op[j] == '+') tmp[l] = tl + tr;\n if (op[j] == '-') tmp[l] = tl - tr;\n if (op[j] == '*') tmp[l] = tl * tr;\n if (op[j] == '/'){\n if(tr==0){\n f=0;\n break;\n }\n tmp[l] = tl / tr;\n }\n tmp_c[r] = 0;\n tmp[r]=0;\n }\n // for(auto x:tmp)cout<<x<<\" \";cout<<endl;\n\n if(f)st[v].insert(tmp[0]);\n }\n } while (next_permutation(all(order)));\n\n //assert(sz(st[v]) > 0);\n } else if (val_op[v].second == 'd') {\n st[v].insert(val_op[v].first);\n assert(sz(st[v]) > 0);\n }\n return val_op[v].second;\n };\n dfs(dfs, root, -1);\n cout << sz(st[root]) << endl;\n return 1;\n}", "accuracy": 0.5, "time_ms": 400, "memory_kb": 3584, "score_of_the_acc": -0.2015, "final_rank": 19 }, { "submission_id": "aoj_2255_10685036", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nbool solve();\n\nusing ll = long long;\n#define rep(i, a, b) for (int i = a; i < (b); ++i)\n#define sz(x) (int)(x).size()\n#define all(x) x.begin(), x.end()\ntypedef pair<int, int> pii;\ntypedef vector<int> vi;\ntemplate <class T>\ninline bool chmin(T &a, T b) {\n return ((a > b) ? (a = b, 1) : (0));\n}\ntemplate <class T>\ninline bool chmax(T &a, T b) {\n return ((a < b) ? (a = b, 1) : (0));\n}\n\nint main() {\n while (solve());\n return 0;\n}\n\nvector<vi> G;\nvector<pair<int, char>> val_op;\nint tidx = 0;\n\n// 木を構成\nint number(const string &s, int &i) {\n int ret = 0;\n while (i < sz(s) && isdigit(s[i])) {\n ret = 10 * ret + (s[i] - '0');\n i++;\n }\n val_op.emplace_back(ret, 'd');\n G.push_back({});\n tidx++;\n return tidx - 1;\n}\nint expr(const string &s, int &i) {\n val_op.emplace_back(-1, 'r'); //()をあらわす\n G.push_back({});\n int p = tidx;\n tidx++;\n vi c; // 子 演算子,expr\n while (i < sz(s)) {\n if (s[i] == '(') {\n assert(s[i] == '(');\n i++; // (\n int cp = expr(s, i);\n G[p].push_back(cp);\n G[cp].push_back(p);\n assert(s[i] == ')');\n i++; // )\n } else {\n assert(isdigit(s[i]));\n int cp = number(s, i);\n G[p].push_back(cp);\n G[cp].push_back(p);\n }\n if (i >= sz(s)) return p;\n if (s[i] == ')') return p;\n bool f = 0;\n for (auto chara : \"+-*/\")\n if (chara == s[i]) f = 1;\n assert(f);\n if (!f) return p;\n int opp = tidx;\n G.push_back({});\n val_op.emplace_back(-1, s[i]);\n tidx++;\n G[p].push_back(opp);\n G[opp].push_back(p);\n i++;\n }\n return p;\n}\n\nbool isop(char c) {\n bool f = 0;\n for (char cc : \"+-*/\")\n if (cc == c) f = 1;\n return f;\n}\nbool solve() {\n tidx = 0;\n G.clear();\n val_op.clear();\n string s;\n cin >> s;\n if (s == \"#\") return 0;\n int root = 0;\n {\n int i = 0;\n root = expr(s, i);\n }\n\n vector<set<int>> st(tidx);\n auto dfs = [&](auto dfs, int v, int p) -> char {\n if (val_op[v].second == 'r') {\n vector<char> op;\n vector<int> sidx;\n for (auto to : G[v]) {\n if (to == p) continue;\n if (val_op[to].second != 'r' && val_op[to].second != 'd') {\n op.push_back(val_op[to].second);\n } else {\n dfs(dfs, to, v);\n sidx.push_back(to);\n assert(sz(st[to]) > 0);\n }\n }\n assert(sz(op) + 1 == sz(sidx));\n vector<vi> vv;\n {\n vi v;\n auto f1 = [&](auto f1, int i) -> void {\n if (i >= sz(sidx)) {\n vv.push_back(v);\n return;\n }\n for (auto x : st[sidx[i]]) {\n v.push_back(x);\n f1(f1, i + 1);\n v.pop_back();\n }\n };\n f1(f1, 0);\n }\n vi order(sz(op));\n iota(all(order), 0);\n do {\n bool f=1;\n for (auto tmp : vv) {\n vi tmp_c(sz(sidx), 1); // つかえるか\n rep(i, 0, sz(op)) {\n int j = order[i];\n int l = j, r = j + 1;\n while (l >= 0 && !tmp_c[l]) l--;\n while (r < sz(sidx) && !tmp_c[r]) r++;\n assert(l >= 0);\n assert(r < sz(sidx));\n assert(isop(op[i]));\n assert(tmp_c[l]);\n assert(tmp_c[r]);\n int tl = tmp[l];\n int tr = tmp[r];\n if (op[j] == '+') tmp[l] = tl + tr;\n if (op[j] == '-') tmp[l] = tl - tr;\n if (op[j] == '*') tmp[l] = tl * tr;\n if (op[j] == '/'){\n if(tr==0){\n f=0;\n break;\n }\n tmp[l] = tl / tr;\n }\n tmp_c[r] = 0;\n tmp[r]=0;\n }\n if(f)st[v].insert(tmp[0]);\n }\n } while (next_permutation(all(order)));\n\n //assert(sz(st[v]) > 0);\n } else if (val_op[v].second == 'd') {\n st[v].insert(val_op[v].first);\n assert(sz(st[v]) > 0);\n }\n return val_op[v].second;\n };\n dfs(dfs, root, -1);\n cout << sz(st[root]) << endl;\n return 1;\n}", "accuracy": 0.5, "time_ms": 340, "memory_kb": 3712, "score_of_the_acc": -0.2313, "final_rank": 20 }, { "submission_id": "aoj_2255_10589433", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#include <atcoder/all>\nusing namespace atcoder;\n\nusing ll = long long;\nusing mint = modint998244353;\n\nbool is_operator(char c) {\n return (c == '+' || c == '-' || c == '/' || c == '*');\n}\n\nll safe_div(ll &x, ll &y) {\n ll s = (x * y > 0 ? 1 : -1);\n return s * (abs(x) / abs(y));\n}\n\nll EVAL(ll x, ll y, char op, bool &fail) {\n // cout << x << \" \" << op << \" \" << y << endl;\n if(op == '/' && y == 0) {\n fail = true;\n return 0;\n }\n return (\n op == '+' ? x + y :\n op == '-' ? x - y :\n op == '*' ? x * y :\n op == '/' ? safe_div(x, y) :\n 0\n );\n}\n\nll NUM(string &S, vector<int> &ord, int &i) {\n ll ret = 0;\n while(i < S.size() && isdigit(S[i])) {\n ret *= 10;\n ret += S[i] - '0';\n i += 1;\n }\n return ret;\n}\n\nll EXPR(string &S, vector<int> &ord, int &i, bool &fail) {\n vector<int> p;\n vector<ll> vals;\n vector<char> ops;\n while(true) {\n ll val;\n if(isdigit(S[i])) {\n val = NUM(S, ord, i);\n }else {\n i += 1;\n val = EXPR(S, ord, i, fail);\n i += 1;\n }\n vals.push_back(val);\n if(i < S.size() && is_operator(S[i])) {\n p.push_back(ord[i]);\n ops.push_back(S[i]);\n i += 1;\n }else {\n break;\n }\n }\n while(!p.empty()) {\n int k = min_element(p.begin(), p.end()) - p.begin();\n ll x = EVAL(vals[k], vals[k + 1], ops[k], fail);\n vals.erase(vals.begin() + k);\n vals.erase(vals.begin() + k);\n ops.erase(ops.begin() + k);\n p.erase(p.begin() + k);\n vals.insert(vals.begin() + k, x);\n }\n return vals[0];\n}\n\nbool solve() {\n string S;\n cin >> S;\n if(S == \"#\") return false;\n int N = S.size();\n vector<int> ops;\n for(int i = 0; i < N; i++) {\n if(is_operator(S[i])) {\n ops.push_back(i);\n }\n }\n int K = ops.size();\n vector<int> P(K), ord(N);\n unordered_set<ll> st;\n iota(P.begin(), P.end(), 0);\n do {\n for(int i = 0; i < K; i++) {\n ord[ops[i]] = P[i];\n }\n int i = 0;\n bool fail = false;\n ll x = EXPR(S, ord, i, fail);\n if(!fail) st.insert(x);\n }while(next_permutation(P.begin(), P.end()));\n cout << st.size() << endl;\n return true;\n}\n\nint main() {\n while(solve());\n}", "accuracy": 1, "time_ms": 2240, "memory_kb": 3584, "score_of_the_acc": -0.4386, "final_rank": 11 }, { "submission_id": "aoj_2255_9313543", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\nusing vll = vector<ll>;\nusing vvll = vector<vll>;\nusing vvvll = vector<vvll>;\nusing vd = vector<double>;\nusing vvd = vector<vd>;\nusing vvvd = vector<vvd>;\n#define all(A) A.begin(),A.end()\n#define ALL(A) A.begin(),A.end()\n#define rep(i, n) for (int i = 0; i < (int) (n); i++)\ntemplate<class T>\nvoid chmax(T& p, T q) { if (p < q)p = q; };\n\nvoid solve(string S) {\n ll N=S.size();\n vector<vector<set<ll>>> DP(N,vector<set<ll>>(N));\n for(ll d=0;d<N;d++){\n rep(i,N){\n ll j=i+d;\n if(j>=N)continue;\n //[i,j]\n\n bool is_num=1;\n ll f=0;\n for(ll k=i;k<=j;k++){\n if('0'<=S[k]&&S[k]<='9'){\n f=10*f+S[k]-'0';\n }\n else is_num=0;\n }\n if(is_num){\n DP[i][j].insert(f);\n continue;\n }\n else if(d>1&&S[i]=='('&&S[j]==')'){\n DP[i][j]=DP[i+1][j-1];\n }\n\n for(ll k=i+1;k<j;k++){\n if(S[k]=='+'){\n for(auto d:DP[i][k-1]){\n for(auto e:DP[k+1][j]){\n DP[i][j].insert(d+e);\n }\n }\n }\n if(S[k]=='*'){\n for(auto d:DP[i][k-1]){\n for(auto e:DP[k+1][j]){\n DP[i][j].insert(d*e);\n }\n }\n }\n if(S[k]=='-'){\n for(auto d:DP[i][k-1]){\n for(auto e:DP[k+1][j]){\n DP[i][j].insert(d-e);\n }\n }\n }\n if(S[k]=='/'){\n for(auto d:DP[i][k-1]){\n for(auto e:DP[k+1][j]){\n if(e!=0)DP[i][j].insert(d/e);\n }\n }\n }\n }\n }\n }\n cout<<DP[0][N-1].size()<<endl;\n}\n\nint main() {\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n\n\n string S;\n while (cin >> S) {\n if (S == \"#\")return 0;\n solve(S);\n }\n\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 4056, "score_of_the_acc": -0.2896, "final_rank": 6 }, { "submission_id": "aoj_2255_9159092", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define ALL(v) v.begin(),v.end()\n#define dbg(x) cerr << #x << \": \" << (x) << endl;\ntemplate<class F, class S>\nostream& operator<<(ostream& os, pair<F,S>& p) {\n os << '(' << p.first << ',' << p.second << ')';\n return os;\n}\ntemplate<class Iter>\nvoid print(Iter beg, Iter end) {\n for (Iter itr = beg; itr != end; ++itr) {\n cerr << *itr << ' ';\n }\n cerr << '\\n';\n}\n#define BAD (1e9 + 1)\n\nclass UnionFind {\npublic:\n\tUnionFind(int n, const vector<vector<int>>& init)\n\t: n(n), par(n), rank(n, 1), ev(init) {\n\t\tiota(par.begin(), par.end(), 0);\n\t}\n\tint root(int x) {\n\t\tif (par[x] == x) return x;\n\t\treturn par[x] = root(par[x]);\n\t}\n\tbool unite(int x, int y, char op) {\n\t\tx = root(x);\n\t\ty = root(y);\n\t\tif (x== y) return false;\n\n vector<int> e;\n for (int a : ev[x]) {\n for (int b : ev[y]) {\n if (op == '+') e.push_back(a + b);\n else if (op == '-') e.push_back(a - b);\n else if (op == '*') e.push_back(a * b);\n else if (op == '/') {\n if (b != 0) e.push_back(a / b);\n }\n }\n }\n sort(ALL(e));\n e.erase(unique(ALL(e)), e.end());\n\t\tif (rank[x] > rank[y]) swap(x,y);\n\t\tpar[x] = y;\n\t\tif (rank[x] == rank[y]) ++rank[y];\n ev[y] = e;\n\n return true;\n\t}\n\tbool same(int x, int y) {\n\t\treturn root(x) == root(y);\n\t}\n vector<int> eval(int x) {\n return ev[root(x)];\n }\nprivate:\n\tint n;\n\tvector<int> par, rank;\n vector<vector<int>> ev;\n};\nstring s;\nusing State = string::iterator;\n/*\nexpr = term op term\nterm = ( expr ) | number\n*/\nvector<int> expr(State& itr);\nvector<int> term(State& itr);\nint number(State& itr);\n\nvector<int> expr(State& itr) {\n vector<vector<int>> a;\n vector<char> op;\n a.push_back(term(itr));\n while (*itr == '+' || *itr == '-' || *itr == '*' || *itr == '/') {\n op.push_back(*itr);\n ++itr;\n a.push_back(term(itr));\n }\n vector<int> order(op.size());\n vector<int> res;\n iota(ALL(order), 0);\n do {\n UnionFind uf(a.size(), a);\n for (int o : order) {\n uf.unite(o, o+1, op[o]);\n }\n auto r = uf.eval(0);\n res.insert(res.end(), ALL(r));\n } while (next_permutation(ALL(order)));\n sort(ALL(res));\n res.erase(unique(ALL(res)), res.end());\n return res;\n}\nvector<int> term(State& itr) {\n if (*itr == '(') {\n ++itr;\n auto res = expr(itr);\n ++itr;\n return res;\n } else if (isdigit(*itr)) {\n return { number(itr) };\n } else {\n assert(false);\n }\n}\nint number(State& itr) {\n int res = 0;\n while (isdigit(*itr)) {\n res *= 10;\n res += *itr - '0';\n ++itr;\n }\n return res;\n}\n\nll solve() {\n getline(cin,s);\n if (s == \"#\") exit(0);\n auto itr = s.begin();\n return expr(itr).size();\n}\n\nint main() {\n ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);\n while (true) {\n cout << solve() << '\\n';\n }\n}", "accuracy": 1, "time_ms": 2230, "memory_kb": 6480, "score_of_the_acc": -1.2861, "final_rank": 18 }, { "submission_id": "aoj_2255_8022893", "code_snippet": "#include<bits/stdc++.h>\n\nusing namespace std;\n\ntemplate<class t>\nvoid output(set<t> vs);\n\nvoid output(int v);\nvoid output(char v);\n\ntemplate<class t>\nvoid output(vector<t> vs){\n cout << \"[\";\n for(auto i:vs){\n output(i);\n cout << \",\";\n }\n cout << \"]\";\n}\n\nvoid output(int v){\n cout << v;\n}\n\nvoid output(char v){\n cout << v;\n}\n\ntemplate<class t>\nvoid output(set<t> vs){\n cout << \"{\";\n for(auto i:vs){\n output(i);\n cout << \",\";\n }\n cout << \"}\";\n}\n\nset<int> decalt(set<int> a,set<int> b,function<pair<bool,int>(int,int)> f){\n set<int> res;\n for(auto i:a){\n for(auto j:b){\n pair<bool,int> t = f(i,j);\n if(not t.first)continue;\n res.insert(t.second);\n }\n }\n return res;\n}\n\nstruct parser {\n string str;\n int p;\n parser(string str):str(str),p(0){}\n\n bool end(){\n return p >= (int)str.size();\n }\n\n bool read_char(char c){\n if(end())return false;\n if(str[p]!=c)return false;\n ++p;\n return true;\n }\n\n bool read_op(char &res){\n string ops = \"+-*/\";\n for(auto op:ops){\n if(read_char(op)){\n res = op;\n return true;\n }\n }\n return false;\n }\n\n bool read_digit(int &res){\n for(char i='0';i<='9';++i){\n if(read_char(i)){\n res = i - '0';\n return true;\n }\n }\n return false;\n }\n\n bool read_number(int &res){\n if(not read_digit(res)){\n return false;\n }\n while(true){\n int add;\n if(not read_digit(add))break;\n res = res * 10 + add;\n }\n return true;\n }\n\n bool read_factor(set<int> &res){\n do{\n if(not read_char('('))break;\n assert(read_expr(res));\n assert(read_char(')'));\n return true;\n }while(false);\n\n do{\n int num;\n if(not read_number(num))break;\n res.clear();\n res.insert(num);\n return true;\n }while(false);\n assert(false);\n }\n\n bool read_expr(set<int> &res){\n set<int> head;\n assert(read_factor(head));\n vector<set<int>> nums;\n vector<char> ops;\n nums.push_back(head);\n while(true){\n char op;\n set<int> rest;\n if(not read_op(op))break;\n assert(read_factor(rest));\n ops.push_back(op);\n nums.push_back(rest);\n }\n res = solve(nums, ops);\n return true;\n }\n\n set<int> solve(vector<set<int>> nums,vector<char> op){\n assert(nums.size()==op.size()+1);\n if(nums.size()==1){\n return nums[0];\n }\n map<char,function<pair<bool,int>(int,int)>> opfs;\n opfs['+'] = [](int a,int b){\n return make_pair(true,a+b);\n };\n opfs['-'] = [](int a,int b){\n return make_pair(true,a-b);\n };\n opfs['*'] = [](int a,int b){\n return make_pair(true,a*b);\n };\n opfs['/'] = [](int a,int b){\n if(b==0)return make_pair(false,0);\n return make_pair(true,a/b);\n };\n set<int> res;\n for(int i=0;i<(int)op.size();++i){\n set<int> t = decalt(nums[i], nums[i+1], opfs[op[i]]);\n vector<set<int>> nnums;\n nnums.insert(nnums.end(),nums.begin(),nums.begin()+i);\n nnums.push_back(t);\n nnums.insert(nnums.end(),nums.begin()+i+2,nums.end());\n\n vector<char> nop;\n nop.insert(nop.end(),op.begin(),op.begin()+i);\n nop.insert(nop.end(),op.begin()+i+1,op.end());\n assert(nums.size() > nnums.size());\n assert(op.size() > nop.size());\n for(auto i:solve(nnums,nop)){\n res.insert(i);\n }\n }\n return res;\n }\n\n};\n\nint main(){\n string str;\n while(cin >> str and str != \"#\"){\n parser p(str);\n set<int> ans;\n assert(p.read_expr(ans));\n cout << ans.size() << endl;\n }\n}", "accuracy": 1, "time_ms": 2970, "memory_kb": 3560, "score_of_the_acc": -0.5256, "final_rank": 13 }, { "submission_id": "aoj_2255_8022091", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\nvector<int> operator+(const vector<int>& lhs, const vector<int>& rhs) {\n vector<int> res;\n for (auto l : lhs) for (auto r : rhs) {\n res.push_back(l + r);\n }\n sort(res.begin(), res.end());\n res.erase(unique(res.begin(), res.end()), res.end());\n return res;\n}\n\nvector<int> operator-(const vector<int>& lhs, const vector<int>& rhs) {\n vector<int> res;\n for (auto l : lhs) for (auto r : rhs) {\n res.push_back(l - r);\n }\n sort(res.begin(), res.end());\n res.erase(unique(res.begin(), res.end()), res.end());\n return res;\n}\n\nvector<int> operator*(const vector<int>& lhs, const vector<int>& rhs) {\n vector<int> res;\n for (auto l : lhs) for (auto r : rhs) {\n res.push_back(l * r);\n }\n sort(res.begin(), res.end());\n res.erase(unique(res.begin(), res.end()), res.end());\n return res;\n}\n\nvector<int> operator/(const vector<int>& lhs, const vector<int>& rhs) {\n vector<int> res;\n for (auto l : lhs) for (auto r : rhs) {\n if (r == 0) continue;\n res.push_back(l / r);\n }\n sort(res.begin(), res.end());\n res.erase(unique(res.begin(), res.end()), res.end());\n return res;\n}\n\nstruct node {\n vector<node*> child;\n vector<char> ops;\n vector<int> vals;\n int num = -1;\n};\n\nbool isNum(char c) {\n return '0' <= c and c <= '9';\n}\n\nbool isOp(char c) {\n return c == '+' or c == '-' or c == '*' or c == '/';\n}\n\nstruct parser {\n string S;\n int it = 0;\n parser(string S) : S(S), it(0) {}\n\n node* num() {\n // cout << it << ' ' << \"num\" << \" called\" << endl;\n node* res = new node();\n if (S[it] == '(') {\n it++;\n res = expr();\n assert(S[it] == ')');\n it++;\n return res;\n }\n else {\n int v = 0;\n while (isNum(S[it])) {\n v = (v * 10) + (S[it] - '0');\n it++;\n }\n res->vals.push_back(v);\n res->num = v;\n return res;\n }\n }\n\n node* expr() {\n // cout << it << ' ' << \"expr\" << \" called\" << endl;\n node* res = new node();\n do {\n res->child.push_back(num());\n if (it == (int)S.size()) break;\n if (S[it] == ')') break;\n assert(isOp(S[it]));\n // cout << S[it] << ' ';\n res->ops.push_back(S[it]);\n it++;\n } while (true);\n return res;\n }\n};\n\nvoid mymerge(vector<int>& lhs, const vector<int>& rhs) {\n for (auto r : rhs) lhs.push_back(r);\n sort(lhs.begin(), lhs.end());\n lhs.erase(unique(lhs.begin(), lhs.end()), lhs.end());\n}\n\ninline vector<int> operation(const vector<int>& lhs, char op, const vector<int>& rhs) {\n assert(isOp(op));\n vector<int> res;\n if (op == '+') {\n res = lhs + rhs;\n }\n if (op == '-') {\n res = lhs - rhs;\n }\n if (op == '*') {\n res = lhs * rhs;\n }\n if (op == '/') {\n res = lhs / rhs;\n }\n return res;\n}\n\nvector<int> calcThisOrder(vector<vector<int>> values, vector<char> ops, vector<int> idx) {\n // cout << \"order is\" << ' ';\n // for (auto i : idx) cout << i << ' ';\n // cout << endl;\n reverse(idx.begin(), idx.end());\n while (idx.size()) {\n int now = idx.back();\n\n assert(now + 1 < (int)values.size()); \n \n // cout << \"operation\" << endl;\n // cout << \" \";\n // for (auto x : values[now]) cout << x << ' ';\n // cout << endl;\n // cout << ops[now] << endl;\n // cout << \" \";\n // for (auto x : values[now + 1]) cout << x << ' ';\n // cout << endl;\n // cout << \"---------------\" << endl;\n vector<int> cond = operation(values[now], ops[now], values[now + 1]);\n // cout << \"cond is\" << endl;\n // for (auto x : cond) cout << x << ' ';\n // cout << endl;\n ops.erase(ops.begin() + now);\n values.erase(values.begin() + now);\n values.erase(values.begin() + now);\n\n values.insert(values.begin() + now, cond);\n\n // cout << \"end insert\" << endl;\n // for (auto arr : values) {\n // cout << \" \";\n // for (auto x : arr) cout << x << ' ';\n // cout << endl;\n // }\n // cout << \"---------------\" << endl;\n\n idx.pop_back();\n for (auto& i : idx) if (i > now) i--;\n\n // cout << \"next index\" << endl;\n // cout << \" \";\n // for (auto i : idx) cout << i << ' ';\n // cout << endl;\n }\n // cout << endl;\n assert(values.size() == (size_t)1);\n return values[0];\n}\n\nvector<int> bruteForce(const vector<vector<int>>& values, const vector<char>& ops) {\n assert(ops.size() + 1 == values.size());\n vector<int> res;\n vector<int> idx(ops.size());\n iota(idx.begin(), idx.end(), 0);\n\n do {\n vector<int> rhs = calcThisOrder(values, ops, idx);\n mymerge(res, rhs);\n\n } while (next_permutation(idx.begin(), idx.end()));\n\n return res;\n}\n\nvector<int> dfs(node* v) {\n if (v->vals.size()) {\n return v->vals;\n }\n \n assert(v->ops.size() + 1 == v->child.size());\n\n vector<vector<int>> values;\n for (const auto& x : v->child) values.push_back(dfs(x));\n\n // cout << \"calc start\" << endl;\n // for (auto val : values) {\n // cout << \" \";\n // for (auto x : val) cout << x << ' ';\n // cout << endl;\n // }\n // cout << \"-------------\" << endl;\n \n vector<int> res = bruteForce(values, v->ops);\n\n return v->vals = res;\n}\n\nbool solve() {\n cin.tie(nullptr)->sync_with_stdio(false);\n string S; cin >> S;\n if (S == \"#\") return false;\n\n parser P(S);\n auto root = P.expr();\n\n auto ans = dfs(root);\n cout << ans.size() << endl;\n\n return true;\n}\n\nint main() {\n while (solve()) ;\n}", "accuracy": 1, "time_ms": 5730, "memory_kb": 3592, "score_of_the_acc": -0.8907, "final_rank": 15 }, { "submission_id": "aoj_2255_8011592", "code_snippet": "# include <bits/stdc++.h>\n# define len(v) (ll(std::size(v)))\n# define all(v) std::begin(v), std::end(v)\n# define rep(i, a, b) for (ll i = a; i < ll(b); i++)\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing pll = pair<ll, ll>;\nstring erase_all_space(string s) { s.erase(remove_if(all(s), [](char c){ return isspace(c); }), s.end()); return s; }\n\nconstexpr ll MOD = ll(1e9) + 7;\n\nstruct Parser {\n string s;\n string::const_iterator it;\n vector<ll> ops;\n vector<int> ord;\n bool zflag;\n Parser() = default;\n Parser(const string& _s, const vector<ll> _ops) : s(_s), ops(_ops), ord(_s.size(), -1), zflag(false) {\n it = s.begin();\n int cur = 0;\n rep (i, 0, len(s)) {\n if (s[i] == '+' || s[i] == '-' || s[i] == '*' || s[i] == '/') {\n ord[i] = ops[cur];\n cur++;\n }\n }\n }\n\n ll parse() {\n return expr();\n }\n\n ll expr(int depth = 0) {\n if (depth == len(ops)) {\n if (*it == '(') {\n assert(*it++ == '(');\n ll val = expr();\n assert(*it++ == ')');\n return val;\n }\n else {\n return number();\n }\n }\n\n ll acc = expr(depth + 1);\n while (it != s.end() && *it != ')' && ord[distance(s.cbegin(), it)] == depth) {\n char op = *it++;\n ll other = expr(depth + 1);\n acc = eval(acc, op, other);\n }\n\n return acc;\n }\n\n ll number() {\n ll ret = 0;\n for (; isdigit(*it); ++it) {\n ret = 10LL * ret + (*it - '0');\n }\n return ret;\n }\n\n ll eval(ll a, char op, ll b) {\n switch (op) {\n case '+': return a + b;\n case '-': return a - b;\n case '*': return a * b;\n case '/': if (b == 0) { zflag = true; b = 1; } return a / b;\n }\n assert(false);\n }\n};\n\nint main() {\n while (true) {\n string s;\n getline(cin, s);\n if (s == \"#\") break;\n int sum = count(all(s), '+') + count(all(s), '-') + count(all(s), '*') + count(all(s), '/');\n set<ll> st;\n\n vector<ll> ops(sum, 0);\n iota(all(ops), 0);\n do {\n Parser parser(s, ops);\n ll res = parser.parse();\n if (not parser.zflag) st.insert(res);\n } while (next_permutation(all(ops)));\n\n cout << st.size() << endl;\n }\n}", "accuracy": 1, "time_ms": 1590, "memory_kb": 3400, "score_of_the_acc": -0.3009, "final_rank": 7 }, { "submission_id": "aoj_2255_7979237", "code_snippet": "#include <bits/stdc++.h>\n\n#define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++)\n#define rrep(i, s, n) for (int i = (int)(n)-1; i >= (int)(s); i--)\n#define all(v) v.begin(), v.end()\n\nusing ll = long long;\nusing ld = long double;\nusing ull = unsigned long long;\n\ntemplate <typename T> bool chmin(T &a, const T &b) {\n if (a <= b) return false;\n a = b;\n return true;\n}\ntemplate <typename T> bool chmax(T &a, const T &b) {\n if (a >= b) return false;\n a = b;\n return true;\n}\n\n#define debug(...) \\\n std::cerr << \"LINE: \" << __LINE__ << \" [\" << #__VA_ARGS__ << \"]:\", \\\n debug_out(__VA_ARGS__)\n\nvoid debug_out() {\n std::cerr << std::endl;\n}\n\ntemplate <typename Head, typename... Tail> void debug_out(Head h, Tail... t) {\n std::cerr << \" \" << h;\n if (sizeof...(t) > 0) std::cerr << \" :\";\n debug_out(t...);\n}\n\ntemplate <typename T1, typename T2>\nstd::ostream &operator<<(std::ostream &os, const std::pair<T1, T2> &pa) {\n return os << pa.first << \" \" << pa.second;\n}\n\ntemplate <typename T>\nstd::ostream &operator<<(std::ostream &os, const std::vector<T> &vec) {\n for (std::size_t i = 0; i < vec.size(); i++)\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \" \");\n return os;\n}\n\ntemplate <typename T>\nstd::ostream &operator<<(std::ostream &os, const std::set<T> &set) {\n for(auto x: set) {\n os << x << \" \";\n }\n return os;\n}\n\ntypedef std::string::const_iterator State;\n\nbool expect(State &begin, char expected) {\n return *begin == expected;\n}\n\nvoid consume(State &begin, char expected) {\n assert(*begin == expected);\n begin++;\n}\n\nbool isdigit(char c) {\n return '0' <= c && c <= '9';\n}\n\nbool isAlpha(char c) {\n return 'A' <= c && c <= 'Z';\n}\n\nbool isalpha(char c) {\n return 'a' <= c && c <= 'z';\n}\n\nstd::set<ll> stmt(State &);\nstd::set<ll> expr(State &);\n\nint is_op(State &begin) {\n if (expect(begin, '+'))\n return 0;\n else if (expect(begin, '-'))\n return 1;\n else if (expect(begin, '*'))\n return 2;\n else if (expect(begin, '/'))\n return 3;\n else\n return -1;\n}\n\nstruct Op {\n int type = -1;\n std::set<ll> lhs, rhs;\n};\n\nstd::set<ll> stmt(State &begin) {\n auto now = expr(begin);\n std::vector<Op> a;\n while (is_op(begin) >= 0) {\n int type = is_op(begin);\n consume(begin, *begin);\n auto rhs = expr(begin);\n Op p;\n p.type = type;\n p.lhs = now;\n p.rhs = rhs;\n now = rhs;\n a.emplace_back(p);\n }\n if(a.empty()) {\n return now;\n }\n int n = a.size();\n std::vector<int> idxs(n);\n std::iota(all(idxs), 0);\n std::set<ll> res;\n do {\n auto op = a;\n std::vector<int> used(n, -1);\n for (auto i : idxs) {\n used[i] = 1;\n auto &[type, lhs, rhs] = op[i];\n std::set<ll> ret;\n for (auto x : lhs) {\n for (auto y : rhs) {\n if (type == 0) {\n ret.insert(x + y);\n } else if (type == 1) {\n ret.insert(x - y);\n } else if (type == 2) {\n ret.insert(x * y);\n } else if (type == 3) {\n if (y != 0) ret.insert(x / y);\n } else\n assert(0);\n }\n }\n if (idxs.back() == i) {\n res.merge(ret);\n break;\n }\n rrep(j,0,i) {\n if(used[j] < 0) {\n op[j].rhs = ret;\n break;\n }\n }\n rep(j,i+1,n) {\n if(used[j] < 0) {\n op[j].lhs = ret;\n break;\n }\n }\n }\n } while (std::next_permutation(all(idxs)));\n return res;\n}\n\nstd::set<ll> expr(State &begin) {\n if (expect(begin, '(')) {\n consume(begin, '(');\n auto res = stmt(begin);\n consume(begin, ')');\n return res;\n } else if (isdigit(*begin)) {\n ll num = 0;\n while (isdigit(*begin)) {\n num *= 10;\n num += *begin - '0';\n consume(begin, *begin);\n }\n return {num};\n } else\n assert(0);\n}\n\nint main() {\n std::string s;\n while (std::cin >> s, !(s == \"#\")) {\n State begin = s.begin();\n int ans = stmt(begin).size();\n std::cout << ans << '\\n';\n }\n}", "accuracy": 1, "time_ms": 3930, "memory_kb": 3484, "score_of_the_acc": -0.6271, "final_rank": 14 }, { "submission_id": "aoj_2255_7003614", "code_snippet": "#define _CRT_SECURE_NO_WARNINGS\n#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n#include <ctype.h>\n#include <unordered_set>\n#include <algorithm>\nusing namespace std;\n\nchar s[256];\n\nunordered_set<int> parse(int stt, int edd) {\n unordered_set<int> res;\n unordered_set<int> oper[12][12];\n int num[12][12], perm[12];\n char oc[16] = { 0, };\n int level = 0, itv = 0, pcnt = 0, tx, np, ocp;\n for (int i = stt; i <= edd; i++) {\n if (s[i] == '(') {\n level++;\n if (level == 1) itv = i;\n }\n else if (s[i] == ')') {\n level--;\n if (level == 0) oper[0][pcnt++] = parse(itv + 1, i - 1);\n }\n else if (level > 0) continue;\n else if (isdigit(s[i]) && (i == 0 || !isdigit(s[i - 1]))) {\n oper[0][pcnt++] = { (int)strtoll(&s[i], NULL, 10) };\n }\n else oc[pcnt - 1] = s[i];\n }\n\n for (int i = 0; i < pcnt - 1; i++) {\n perm[i] = i;\n num[0][i] = i;\n }\n do {\n for (int i = 1; i < pcnt; i++) {\n np = num[i - 1][perm[i - 1]];\n ocp = perm[i - 1];\n for (int j = 0; j < np; j++) oper[i][j] = oper[i - 1][j];\n oper[i][np] = {};\n for (int j : oper[i - 1][np]) {\n for (int k : oper[i - 1][np + 1]) {\n if (oc[ocp] == '/' && k == 0) continue;\n if (oc[ocp] == '+') tx = j + k;\n else if (oc[ocp] == '-') tx = j - k;\n else if (oc[ocp] == '*') tx = j * k;\n else if (oc[ocp] == '/') tx = j / k;\n oper[i][np].insert(tx);\n }\n }\n for (int j = np + 1; j < pcnt - i; j++) oper[i][j] = oper[i - 1][j + 1];\n for (int j = 0; j < pcnt - 1; j++) {\n if (j == perm[i - 1]) num[i][j] = -1;\n else if (j > perm[i - 1]) num[i][j] = num[i - 1][j] - 1;\n else num[i][j] = num[i - 1][j];\n }\n }\n for (int i : oper[pcnt - 1][0]) res.insert(i);\n } while (next_permutation(&perm[0], &perm[pcnt - 1]));\n\n return res;\n}\n\nint main(void) {\n while (1) {\n scanf(\"%s\", s);\n if (s[0] == '#') break;\n printf(\"%d\\n\", (signed int)(parse(0, strlen(s) - 1).size()));\n }\n return 0;\n}", "accuracy": 1, "time_ms": 3870, "memory_kb": 3068, "score_of_the_acc": -0.4974, "final_rank": 12 }, { "submission_id": "aoj_2255_6784905", "code_snippet": "#include <algorithm>\n#include <bitset>\n#include <cassert>\n#include <cctype>\n#include <cfloat>\n#include <climits>\n#include <cmath>\n#include <complex>\n#include <cstdio>\n#include <cstdlib>\n#include <ctime>\n#include <ext/pb_ds/assoc_container.hpp>\n#include <ext/pb_ds/tag_and_trait.hpp>\n#include <ext/pb_ds/tree_policy.hpp>\n#include <fstream>\n#include <functional>\n#include <iomanip>\n#include <iostream>\n#include <iterator>\n#include <list>\n#include <map>\n#include <memory>\n#include <numeric>\n#include <optional>\n#include <queue>\n#include <random>\n#include <set>\n#include <sstream>\n#include <stack>\n#include <string>\n#include <unordered_map>\n#include <unordered_set>\n#include <utility>\nusing namespace __gnu_pbds;\nusing namespace std;\n\n#pragma GCC optimize(\"O3\")\n// #pragma GCC target(\"avx2\")\n// #pragma GCC optimize(\"unroll-loops\")\n#define all(x) (x).begin(), (x).end()\n#define reall(a) (a).rbegin(), (a).rend()\n#define Sort(x) sort((x).begin(), (x).end())\n#define Reverse(x) reverse((x).begin(), (x).end())\n#define len(x) (ll) x.size()\n#define pb emplace_back\n#define append emplace_back\n#define STR(x) to_string(x)\n#define INT(x) stoi(x)\n#define ll(x) stoll(x)\n#define DOUBLE(x) stod(x)\n#define ld(x) stold(x)\n\n#define elif else if\n#define sum(...) accumulate(all(__VA_ARGS__), 0LL)\n#define bisect_left(arr, k) lower_bound(all(arr), (k)) - (arr).begin()\n// #define bisect_right(arr, k) upper_bound(all(arr), (k)) - (arr).begin()\n#define uniq(arr) \\\n sort(all(arr)); \\\n arr.erase(unique(all(arr)), end(arr))\n\nusing str = string;\nusing ll = long long;\nusing ld = long double;\nusing ull = unsigned long long;\nusing ui = unsigned int;\nusing puu = pair<ui, ui>;\n// using P = pair<int, int>;\nusing vi = vector<int>;\nusing vll = vector<long long>;\n// template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,\n// tree_order_statistics_node_update>;\ntemplate <typename S, typename T>\nusing ordered_dict = tree<S, T, less<S>, rb_tree_tag, tree_order_statistics_node_update>;\n/*\nvar_name.find_by_order(k) でk番目(0-indexed)のiteratorを返す.\nvar_name.order_of_key(key) でkey以上の最初の要素が、treeの中で何番目か返す.\n*/\n// rep-------------------------------------------------------------------\n#define overload4(_1, _2, _3, _4, name, ...) name\n#define overload3(_1, _2, _3, name, ...) name\n#define rep1(n) for (ll tukacchadame = 0; tukacchadame < n; ++tukacchadame)\n#define rep2(i, n) for (ll i = 0; i < n; ++i)\n#define rep3(i, a, b) for (ll i = a; i < b; ++i)\n#define rep4(i, a, b, c) for (ll i = a, _bb = b; (c > 0 && i < _bb) || (c < 0 && i > _bb); i += c)\n#define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)\n#define each1(i, a) for (auto &&i : a)\n#define each2(x, y, a) for (auto &&[x, y] : a)\n#define each3(x, y, z, a) for (auto &&[x, y, z] : a)\n#define each(...) overload4(__VA_ARGS__, each3, each2, each1)(__VA_ARGS__)\n// ----------------------------------------------------------------------\n\n// 入出力-----------------------------------------------------------------\nstruct Ioinitializer {\n Ioinitializer() noexcept {\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n cout << setprecision(20) << setiosflags(ios::fixed);\n }\n} Ioinitializer;\n\nstruct input {\n input(){};\n operator int() {\n int value;\n cin >> value;\n return value;\n }\n operator long long() {\n long long value;\n cin >> value;\n return value;\n }\n operator unsigned long long() {\n unsigned long long value;\n cin >> value;\n return value;\n }\n operator unsigned() {\n unsigned value;\n cin >> value;\n return value;\n }\n operator float() {\n float value;\n cin >> value;\n return value;\n }\n operator double() {\n double value;\n cin >> value;\n return value;\n }\n operator long double() {\n long double value;\n cin >> value;\n return value;\n }\n operator char() {\n char value;\n do {\n value = getchar();\n } while (value == ' ' || value == '\\n');\n return value;\n }\n operator string() {\n string value;\n cin >> value;\n return value;\n }\n};\nvoid scan(int &a) {\n cin >> a;\n}\nvoid scan(unsigned &a) {\n cin >> a;\n}\nvoid scan(long &a) {\n cin >> a;\n}\nvoid scan(long long &a) {\n cin >> a;\n}\nvoid scan(unsigned long long &a) {\n cin >> a;\n}\nvoid scan(char &a) {\n do {\n a = getchar();\n } while (a == ' ' || a == '\\n');\n}\nvoid scan(float &a) {\n cin >> a;\n}\nvoid scan(double &a) {\n cin >> a;\n}\nvoid scan(long double &a) {\n cin >> a;\n}\nvoid scan(vector<bool> &a) {\n for (unsigned i = 0; i < a.size(); i++) {\n int b;\n scan(b);\n a[i] = b;\n }\n}\nvoid scan(char a[]) {\n cin >> a;\n}\nvoid scan(string &a) {\n cin >> a;\n}\ntemplate <class T> void scan(vector<T> &);\ntemplate <class T, size_t size> void scan(array<T, size> &);\ntemplate <class T, class L> void scan(pair<T, L> &);\ntemplate <class T, size_t size> void scan(T (&)[size]);\ntemplate <class T> void scan(vector<T> &a) {\n for (auto &&i : a)\n scan(i);\n}\ntemplate <class T> void scan(deque<T> &a) {\n for (auto &&i : a)\n scan(i);\n}\ntemplate <class T, size_t size> void scan(array<T, size> &a) {\n for (auto &&i : a)\n scan(i);\n}\ntemplate <class T, class L> void scan(pair<T, L> &p) {\n scan(p.first);\n scan(p.second);\n}\ntemplate <class T, size_t size> void scan(T (&a)[size]) {\n for (auto &&i : a)\n scan(i);\n}\ntemplate <class T> void scan(set<T> &a, int n) {\n for (int i = 0; i < n; i++)\n a.insert(input());\n}\n\ntemplate <class T> void scan(T &a) {\n cin >> a;\n}\ntemplate <class A, class B> ostream &operator<<(ostream &ost, const pair<A, B> &p) {\n ost << \"{\" << p.first << \", \" << p.second << \"}\";\n return ost;\n}\ntemplate <class T> ostream &operator<<(ostream &ost, const vector<T> &v) {\n ost << \"[\";\n for (int i = 0; i < (int)v.size(); i++) {\n if (i)\n ost << \", \";\n ost << v[i];\n }\n ost << \"]\";\n return ost;\n}\ntemplate <class A, class B> ostream &operator<<(ostream &ost, const map<A, B> &v) {\n bool k = false;\n ost << \"{\";\n for (auto p : v) {\n if (k) {\n ost << \", \";\n }\n ost << \"(\" << p.first << \", \" << p.second << \")\";\n k = true;\n }\n ost << \"}\";\n return ost;\n}\ntemplate <class T> ostream &operator<<(ostream &ost, const set<T> &v) {\n bool k = false;\n ost << \"{\";\n for (auto p : v) {\n if (k) {\n ost << \", \";\n k = true;\n }\n ost << p;\n k = true;\n }\n ost << \"}\";\n return ost;\n}\ntemplate <class T> ostream &operator<<(ostream &ost, const multiset<T> &v) {\n bool k = false;\n ost << \"[\";\n for (auto p : v) {\n if (k) {\n ost << \", \";\n k = true;\n }\n ost << p;\n k = true;\n }\n ost << \"]\";\n return ost;\n}\ntemplate <class T> inline void print(const T &a) {\n cout << a << \"\\n\";\n}\ntemplate <class T, class... Ts> inline void print(const T &a, const Ts &...ts) {\n cout << a << \" \";\n print(ts...);\n}\ntemplate <class T> inline void print_vec(const vector<T> &a, bool debug = false) {\n for (unsigned i = 0; i < a.size(); ++i) {\n if (debug)\n cout << \"[\";\n if (i) {\n if (debug)\n cout << \", \";\n else\n cout << \" \";\n }\n cout << a[i];\n }\n if (debug)\n cout << \"]\\n\";\n else\n cout << \"\\n\";\n}\ntemplate <class T> inline void print_vec(const vector<vector<T>> &a, bool debug = false) {\n int n = a.size();\n if (n == 0) {\n if (debug)\n print(\"[[]]\");\n else\n print(\"\");\n return;\n }\n for (int i = 0; i < n; ++i) {\n if (debug)\n cout << \"[\";\n for (unsigned j = 0; j < a[i].size(); ++j) {\n if (j) {\n if (debug)\n cout << \", \";\n else\n cout << \" \";\n }\n cout << a[i][j];\n }\n if (debug)\n cout << \"]\\n\";\n else\n cout << \"\\n\";\n }\n}\n// -----------------------------------------------------------------------\n// 関数群\ntemplate <class T> vector<T> vec(size_t a) {\n return vector<T>(a);\n}\ntemplate <class T, class... Ts> auto vec(size_t a, Ts... ts) {\n return vector<decltype(vec<T>(ts...))>(a, vec<T>(ts...));\n}\ntemplate <typename T> T floor(T a, T b) {\n if (b < 0) {\n a = -a;\n b = -b;\n }\n return a >= 0 ? a / b : (a - b + 1) / b;\n}\ntemplate <typename T> T ceil(T a, T b) {\n if (b < 0) {\n a = -a;\n b = -b;\n }\n return a >= 0 ? (a + b - 1) / b : a / b;\n}\n\ntemplate <class T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return true;\n }\n return false;\n}\ntemplate <class T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return true;\n }\n return false;\n}\ntemplate <class T> auto min(const T &a) {\n return *min_element(a.begin(), a.end());\n}\ntemplate <class T> auto max(const T &a) {\n return *max_element(a.begin(), a.end());\n}\ntemplate <class ll> long long intpow(ll a, ll b) {\n long long ans = 1;\n while (b) {\n if (b & 1)\n ans *= a;\n a *= a;\n b >>= 1;\n }\n return ans;\n}\nlong long modpow(ll a, ll b, ll p) {\n long long ans = p == 1 ? 0 : 1;\n long long s = a % p;\n while (b) {\n if (b & 1)\n (ans *= s) %= p;\n (s *= s) %= p;\n b >>= 1;\n }\n return ans;\n}\ntemplate <typename A> A lcm(const vector<A> &a) {\n A res;\n res = a[0];\n for (int i = 1; i < a.size(); i++) {\n res = lcm(res, a[i]);\n }\n return res;\n}\ntemplate <typename A> A gcd(const vector<A> &a) {\n A res;\n res = a[0];\n for (int i = 1; i < a.size() && res != 1; i++) {\n res = gcd(a[i], res);\n }\n return res;\n}\nint popcount(int n) {\n return __builtin_popcount(n);\n}\nint popcount(ll n) {\n return __builtin_popcountll(n);\n}\ntemplate <typename T> int bit_length(T n) {\n int k = 0;\n while (n) {\n k++;\n n /= 2;\n }\n return k;\n}\ntemplate <typename A> A isqrt(A n) {\n A a = 0, r = 0;\n for (A s = bit_length(n) - (bit_length(n) % 2 ? 1 : 2); s >= 0; s -= 2) {\n A t = ((n >> s) & 3);\n r = ((r << 2) | t);\n A c = ((a << 2) | 1);\n int b = (r >= c);\n if (b) {\n r -= c;\n }\n a = ((a << 1) | b);\n }\n return a;\n}\ntemplate <typename T> T add(T a, T b) {\n T res;\n if (a < 0 && b < 0)\n return __builtin_add_overflow(a, b, &res) ? numeric_limits<T>::min() : res;\n return __builtin_add_overflow(a, b, &res) ? numeric_limits<T>::max() : res;\n}\ntemplate <typename T> T mul(T a, T b) {\n T res;\n if ((a < 0 and b > 0) or (a > 0 and b < 0))\n return __builtin_mul_overflow(a, b, &res) ? numeric_limits<T>::min() : res;\n return __builtin_mul_overflow(a, b, &res) ? numeric_limits<T>::max() : res;\n}\n\ntemplate <typename T> int count(const vector<T> &a, const T k) {\n int res = 0;\n for (T val : a)\n if (val == k)\n res++;\n return res;\n}\n\nint count(const string &s, const char &k) {\n int res = 0;\n for (char val : s)\n if (val == k)\n res++;\n return res;\n}\n\nint count(const string &s, const string &k) {\n int res = 0, n = len(k);\n for (int i = 0; i < len(s) - n + 1; i++)\n if (k == s.substr(i, n)) {\n res++;\n i += n - 1;\n }\n return res;\n}\n\ntemplate <typename T> map<T, int> Counter(const vector<T> &a) {\n map<T, int> mp;\n for (T val : a)\n mp[val]++;\n return mp;\n}\ntemplate <typename S, typename T> vector<T> values(const map<S, T> &mp) {\n int n = mp.size(), i = 0;\n vector<T> res(n);\n for (auto [itm, val] : mp)\n res[i++] = val;\n return res;\n}\ntemplate <typename S, typename T> vector<S> keys(const map<S, T> &mp) {\n int n = mp.size(), i = 0;\n vector<S> res(n);\n for (auto [itm, val] : mp)\n res[i++] = itm;\n return res;\n}\ntemplate <typename T> inline bool contain(const vector<T> &a, const T k) {\n return binary_search(a.begin(), a.end(), k);\n}\n\nstruct dsu {\n public:\n dsu() : _n(0) {\n }\n explicit dsu(int n) : _n(n), parent_or_size(n, -1) {\n }\n\n int merge(int a, int b) {\n assert(0 <= a && a < _n);\n assert(0 <= b && b < _n);\n int x = leader(a), y = leader(b);\n if (x == y)\n return x;\n if (-parent_or_size[x] < -parent_or_size[y])\n std::swap(x, y);\n parent_or_size[x] += parent_or_size[y];\n parent_or_size[y] = x;\n return x;\n }\n\n bool same(int a, int b) {\n assert(0 <= a && a < _n);\n assert(0 <= b && b < _n);\n return leader(a) == leader(b);\n }\n\n int leader(int a) {\n assert(0 <= a && a < _n);\n if (parent_or_size[a] < 0)\n return a;\n return parent_or_size[a] = leader(parent_or_size[a]);\n }\n\n int size(int a) {\n assert(0 <= a && a < _n);\n return -parent_or_size[leader(a)];\n }\n\n std::vector<std::vector<int>> groups() {\n std::vector<int> leader_buf(_n), group_size(_n);\n for (int i = 0; i < _n; i++) {\n leader_buf[i] = leader(i);\n group_size[leader_buf[i]]++;\n }\n std::vector<std::vector<int>> result(_n);\n for (int i = 0; i < _n; i++) {\n result[i].reserve(group_size[i]);\n }\n for (int i = 0; i < _n; i++) {\n result[leader_buf[i]].push_back(i);\n }\n result.erase(std::remove_if(result.begin(), result.end(), [&](const std::vector<int> &v) { return v.empty(); }),\n result.end());\n return result;\n }\n\n private:\n int _n;\n // root node: -1 * component size\n // otherwise: parent\n std::vector<int> parent_or_size;\n};\n\nvector<vector<int>> multiple_loops(const vector<int> &loops) {\n //各 k 対して,loops[k] 回ループする.itertools.productに似ている.\n\n int loop_count = 1;\n for (int i : loops)\n loop_count *= i;\n\n if (loop_count == 0)\n return {};\n\n int n = (int)loops.size();\n vector<int> d(n);\n vector<vector<int>> res = {d};\n for (int _ = 0; _ < loop_count - 1; ++_) {\n d[0]++;\n int i = 0;\n while (i < n and d[i] == loops[i]) {\n d[i] = 0;\n i++;\n d[i]++;\n }\n res.emplace_back(d);\n }\n return res;\n}\n\nconst int INF = INT_MAX >> 1;\n// const ll mod = 1000000007;\n// const double PI = acos(-1.0);\n\nstruct solve {\n int now = 0;\n string expr;\n int n;\n\n solve(string s) : expr(s), n(s.size()) {\n print(eval().size());\n }\n\n vector<int> eval() {\n vector<vector<int>> can;\n vector<char> op;\n while (now < n and expr[now] != ')') {\n if (expr[now] == '(') {\n now++;\n can.emplace_back(eval());\n } else if ('0' <= expr[now] and expr[now] <= '9') {\n can.push_back({read_int()});\n } else {\n op.emplace_back(expr[now]);\n now++;\n }\n }\n now++;\n vector<int> loop_num, v;\n int m = len(op);\n for (auto k : can)\n loop_num.emplace_back(k.size());\n\n rep(i, m) v.emplace_back(i);\n vector<int> res;\n do {\n for (auto &w : multiple_loops(loop_num)) {\n dsu uf(m + 1);\n vector<int> arr(m + 1);\n bool zero_flag = 0;\n rep(i, m + 1) {\n arr[i] = can[i][w[i]];\n }\n for (auto k : v) {\n int s = uf.leader(k), t = uf.leader(k + 1);\n uf.merge(k, k + 1);\n int u = uf.leader(k);\n if (op[k] == '+') {\n arr[u] = arr[s] + arr[t];\n } else if (op[k] == '-') {\n arr[u] = arr[s] - arr[t];\n } else if (op[k] == '*') {\n arr[u] = arr[s] * arr[t];\n } else {\n if (arr[t] == 0) {\n zero_flag = 1;\n break;\n } else {\n arr[u] = arr[s] / arr[t];\n }\n }\n }\n if (not zero_flag) {\n res.emplace_back(arr[uf.leader(0)]);\n }\n }\n } while (next_permutation(v.begin(), v.end()));\n uniq(res);\n return res;\n }\n\n int read_int() {\n int res = 0;\n while (now < n and '0' <= expr[now] and expr[now] <= '9') {\n res = res * 10 + (expr[now] - '0');\n now++;\n }\n return res;\n }\n};\n\nint main() {\n while (true) {\n string s = input();\n if (s == \"#\")\n break;\n solve sol(s);\n }\n}", "accuracy": 1, "time_ms": 670, "memory_kb": 6436, "score_of_the_acc": -1.0722, "final_rank": 16 }, { "submission_id": "aoj_2255_6650809", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing State = string::const_iterator;\n\nbool valid = true;\n\nint factor(State &begin);\nint expression(State &begin);\nint term(State &begin);\nint number(State &begin);\n\nint factor(State &begin) {\n if (*begin == '(') {\n begin++;\n int ret = expression(begin);\n begin++;\n return ret;\n } else {\n return number(begin);\n }\n}\n\nint expression(State &begin) {\n int ret = term(begin);\n\n while (true) {\n if (*begin == '+') {\n begin++;\n ret += term(begin);\n } else if (*begin == '-') {\n begin++;\n ret -= term(begin);\n } else {\n break;\n }\n }\n\n return ret;\n}\n\nint term(State &begin) {\n int ret = factor(begin);\n\n while (true) {\n if (*begin == '*') {\n begin++;\n ret *= factor(begin);\n } else if (*begin == '/') {\n begin++;\n int f = factor(begin);\n if (f == 0) {\n valid = false;\n return 0;\n }\n ret /= f;\n } else {\n break;\n }\n }\n\n return ret;\n}\n\nint number(State &begin) {\n int ret = 0;\n while (isdigit(*begin)) {\n ret *= 10;\n ret += *begin - '0';\n begin++;\n }\n return ret;\n}\n\nvoid solve(string in) {\n string u = '(' + in + ')';\n\n vector< int > idxs;\n for (int i = 0; i < (int)u.size(); i++) {\n if (u[i] == '+' or u[i] == '-' or u[i] == '*' or u[i] == '/') {\n idxs.emplace_back(i);\n }\n }\n\n set< int > st;\n // cerr << s << endl;\n do {\n string s = u;\n auto is = idxs;\n for (int qi = 0; qi < (int)is.size(); qi++) {\n int i = is[qi];\n int l = i;\n int cntl = 0;\n while (cntl != 1) {\n l--;\n if (s[l] == '(') cntl++;\n if (s[l] == ')') cntl--;\n }\n // cerr << \"l: \" << l << endl;\n\n int r = i;\n int cntr = 0;\n while (cntr != 1) {\n r++;\n if (s[r] == ')') cntr++;\n if (s[r] == '(') cntr--;\n }\n // cerr << \"r: \" << r << endl;\n\n s = s.substr(0, l) + '(' + s.substr(l, i - l) + ')' + s[i] + '(' + s.substr(i + 1, r - i) + ')' + s.substr(r + 1, s.size());\n // cerr << s.substr(0, l) << \", \" << s.substr(l, i - l) << \", \" << s[i] << \", \" << s.substr(i + 1, r - i) << endl;\n // cerr << s << endl;\n // for (auto &j: is) cerr << j << \" \";\n // cerr << endl;\n for (int j = 0; j < (int)is.size(); j++) {\n if (l <= is[j]) is[j]++;\n if (i <= is[j]) is[j]++;\n if (i + 3 <= is[j]) is[j]++;\n if (r + 3 <= is[j]) is[j]++;\n }\n\n // for (auto &j: is) cerr << j << \" \";\n // cerr << endl;\n\n // for (auto &j: is) cerr << s[j] << endl;\n // break;\n }\n\n State begin = s.begin();\n valid = true;\n\n int res = factor(begin);\n if (valid) st.insert(res);\n // break;\n } while (next_permutation(idxs.begin(), idxs.end()));\n\n cout << st.size() << endl;\n}\n\nint main() {\n string s;\n\n while (cin >> s, s.front() != '#') {\n solve(s);\n }\n}", "accuracy": 1, "time_ms": 7770, "memory_kb": 3320, "score_of_the_acc": -1.0739, "final_rank": 17 }, { "submission_id": "aoj_2255_6419783", "code_snippet": "#include <bits/stdc++.h>\n#pragma GCC optimize(\"Ofast\")\n#define _GLIBCXX_DEBUG\nusing namespace std;\nusing std::cout;\nusing std::cin;\nusing std::endl;\nusing ll=long long;\nusing ld=long double;\nll ILL=1167167167167167167;\nconst int INF=2100000000;\nconst ll mod=998244353;\n#define rep(i,a) for (ll i=0;i<a;i++)\n#define all(p) p.begin(),p.end()\ntemplate<class T> using _pq = priority_queue<T, vector<T>, greater<T>>;\ntemplate<class T> ll LB(vector<T> &v,T a){return lower_bound(v.begin(),v.end(),a)-v.begin();}\ntemplate<class T> ll UB(vector<T> &v,T a){return upper_bound(v.begin(),v.end(),a)-v.begin();}\ntemplate<class T> bool chmin(T &a,const T &b){if(a>b){a=b;return 1;}else return 0;}\ntemplate<class T> bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}else return 0;}\ntemplate<class T> void So(vector<T> &v) {sort(v.begin(),v.end());}\ntemplate<class T> void Sore(vector<T> &v) {sort(v.begin(),v.end(),[](T x,T y){return x>y;});}\nvoid yneos(bool a){if(a) cout<<\"Yes\\n\"; else cout<<\"No\\n\";}\ntemplate<class T> void vec_out(vector<T> &p){for(int i=0;i<(int)(p.size());i++){if(i) cout<<\" \";cout<<p[i];}cout<<\"\\n\";}\n\nset<int> g(vector<set<int>> &p,vector<char> &q){\n\tint l=q.size();\n\tif(l==0) return p[0];\n\tset<int> s;\n\trep(i,l){\n\t\tauto n_p=p;\n\t\tauto n_q=q;\n\t\tset<int> n_s;\n\t\tfor(auto x:n_p[i]) for(auto y:n_p[i+1]){\n\t\t\tint tmp;\n\t\t\tif(q[i]=='+'){\n\t\t\t\ttmp=x+y;\n\t\t\t}else if(q[i]=='-'){\n\t\t\t\ttmp=x-y;\n\t\t\t}else if(q[i]=='*'){\n\t\t\t\ttmp=x*y;\n\t\t\t}else{\n\t\t\t\tif(y==0) tmp=INF;\n\t\t\t\telse tmp=x/y;\n\t\t\t}\n\t\t\tif(tmp!=INF) n_s.insert(tmp);\n\t\t}\n\t\tfor(int j=i+1;j<l;j++){\n\t\t\tswap(n_q[j-1],n_q[j]);\n\t\t\tswap(n_p[j],n_p[j+1]);\n\t\t}\n\t\tn_q.resize(l-1);\n\t\tn_p.resize(l);\n\t\tn_p[i]=n_s;\n\t\tauto tmp=g(n_p,n_q);\n\t\tfor(auto x:tmp) s.insert(x);\n\t}\n\treturn s;\n}\n\nset<int> f(string S,int &ind){\n\tvector<set<int>> p;\n\tvector<char> q;\n\twhile(true){\n\t\tif(ind==(int)(S.size())||S[ind]==')'){\n\t\t\tind++;\n\t\t\treturn g(p,q);\n\t\t}\n\t\telse if(S[ind]=='('){\n\t\t\tind++;\n\t\t\tp.push_back(f(S,ind));\n\t\t}else if('0'<=S[ind]&&S[ind]<='9'){\n\t\t\tint tmp=0;\n\t\t\twhile(ind!=(int)(S.size())&&'0'<=S[ind]&&S[ind]<='9'){\n\t\t\t\ttmp*=10;\n\t\t\t\ttmp+=(int)(S[ind]-'0');\n\t\t\t\tind++;\n\t\t\t}\n\t\t\tp.push_back({tmp});\n\t\t}else{\n\t\t\tq.push_back(S[ind]);\n\t\t\tind++;\n\t\t}\n\t}\n}\n\nvoid solve(string S);\n// oddloop\nint main() {\n\tios::sync_with_stdio(false);\n\tcin.tie(nullptr);\n\t\n\tstring S;\n\twhile(cin>>S,S!=\"#\") solve(S);\n}\n\nvoid solve(string S){\n\tint x=0;\n\tcout<<f(S,x).size()<<\"\\n\";\n}", "accuracy": 1, "time_ms": 1240, "memory_kb": 3416, "score_of_the_acc": -0.2605, "final_rank": 4 }, { "submission_id": "aoj_2255_6046025", "code_snippet": "#include <bits/stdc++.h>\n#define rep(i,n) for(int i = 0; i < (n); i++)\nusing namespace std;\ntypedef long long ll;\n\nint expr(string &s, int &i, int id, int &n, int &ng, vector<int> &o) {\n\tint res = 0;\n\tif(id == n) {\n\t\tif(s[i] == '(') {\n\t\t\ti++;\n\t\t\tres = expr(s, i, 0, n, ng, o);\n\t\t\ti++;\n\t\t} else {\n\t\t\twhile(i < s.size() && isdigit(s[i])) {\n\t\t\t\tres = res * 10 + s[i] - '0';\n\t\t\t\ti++;\n\t\t\t}\n\t\t}\n\t} else {\n\t\tres = expr(s, i, id + 1, n, ng, o);\n\t\twhile(true) {\n\t\t\tif(i < o.size() && o[i] == id) {\n\t\t\t\tchar op = s[i];\n\t\t\t\tint to = expr(s, ++i, id + 1, n, ng, o);\n\t\t\t\tif(op == '+') res += to;\n\t\t\t\tif(op == '-') res -= to;\n\t\t\t\tif(op == '*') res *= to;\n\t\t\t\tif(op == '/') {\n\t\t\t\t\tif(to == 0) ng = 1;\n\t\t\t\t\telse res /= to;\n\t\t\t\t}\n\t\t\t} else break;\n\t\t}\n\t}\n\treturn res;\n}\n\nint main(){\n\tcin.tie(0);\n\tios::sync_with_stdio(0);\n\t\n\twhile(true) {\n\t\tstring s; cin >> s; if(s == \"#\") break;\n\t\tint n = 0;\n\t\tvector<int> op;\n\t\tauto isop = [&](char c){ return c == '+' || c == '-' || c == '*' || c == '/'; };\n\t\trep(i,s.size()) if(isop(s[i])) n++, op.push_back(i);\n\n\t\tvector<int> p(n), o(s.size(), -1);\n\t\tiota(p.begin(), p.end(), 0);\n\t\tset<int> ans;\n\t\tdo {\n\t\t\tint pos = 0, ng = 0;\n\t\t\tvector<int> o(s.size(), -1);\n\t\t\tint j = 0;\n\t\t\tfor(int i : op) o[i] = p[j++];\n\t\t\tint res = expr(s, pos, 0, n, ng, o);\n\t\t\tif(!ng) ans.insert(res);\n\t\t} while(next_permutation(p.begin(), p.end()));\n\t\tcout << ans.size() << endl;\n\t}\n}", "accuracy": 1, "time_ms": 1450, "memory_kb": 3472, "score_of_the_acc": -0.304, "final_rank": 9 }, { "submission_id": "aoj_2255_6046021", "code_snippet": "#include <bits/stdc++.h>\n#define rep(i,n) for(int i = 0; i < (n); i++)\nusing namespace std;\ntypedef long long ll;\n\nint expr(string &s, int &i, int id, int &n, int &ng, vector<int> &o) {\n\tint res = 0;\n\tif(id == n) {\n\t\tif(s[i] == '(') {\n\t\t\ti++;\n\t\t\tres = expr(s, i, 0, n, ng, o);\n\t\t\ti++;\n\t\t} else {\n\t\t\twhile(i < s.size() && isdigit(s[i])) {\n\t\t\t\tres = res * 10 + s[i] - '0';\n\t\t\t\ti++;\n\t\t\t}\n\t\t}\n\t} else {\n\t\tres = expr(s, i, id + 1, n, ng, o);\n\t\twhile(true) {\n\t\t\tif(i < o.size() && o[i] == id) {\n\t\t\t\tchar op = s[i];\n\t\t\t\tint to = expr(s, ++i, id + 1, n, ng, o);\n\t\t\t\tif(op == '+') res += to;\n\t\t\t\tif(op == '-') res -= to;\n\t\t\t\tif(op == '*') res *= to;\n\t\t\t\tif(op == '/') {\n\t\t\t\t\tif(to == 0) ng = 1;\n\t\t\t\t\telse res /= to;\n\t\t\t\t}\n\t\t\t} else break;\n\t\t}\n\t}\n\treturn res;\n}\n\nint main(){\n\tcin.tie(0);\n\tios::sync_with_stdio(0);\n\t\n\twhile(true) {\n\t\tstring s; cin >> s; if(s == \"#\") break;\n\t\tint n = 0;\n\t\tauto isop = [&](char c){ return c == '+' || c == '-' || c == '*' || c == '/'; };\n\t\trep(i,s.size()) if(isop(s[i])) n++;\n\n\t\tvector<int> p(n);\n\t\tiota(p.begin(), p.end(), 0);\n\t\tset<int> ans;\n\t\tdo {\n\t\t\tint pos = 0, ng = 0;\n\t\t\tvector<int> o(s.size(), -1);\n\t\t\tint j = 0;\n\t\t\trep(i,s.size()) if(isop(s[i])) o[i] = p[j++];\n\t\t\tint res = expr(s, pos, 0, n, ng, o);\n\t\t\tif(!ng) ans.insert(res);\n\t\t} while(next_permutation(p.begin(), p.end()));\n\t\tcout << ans.size() << endl;\n\t}\n}", "accuracy": 1, "time_ms": 1620, "memory_kb": 3388, "score_of_the_acc": -0.3013, "final_rank": 8 }, { "submission_id": "aoj_2255_6026929", "code_snippet": "#pragma region Macros\n#pragma comment(linker, \"/stack:200000000\")\n// #pragma GCC optimize(\"unroll-loops\")\n// #pragma GCC target(\"sse,sse2,sse3,ssse3,sse4,popcnt,fma,abm,mmx,avx,avx2,tune=native\")\n// #pragma GCC target(\"sse,sse2,sse3,ssse3,sse4,popcnt,fma,abm,mmx,avx,avx2\")\n// #pragma GCC target(\"avx2\")\n#pragma GCC optimize(\"Ofast\")\n#include <bits/stdc++.h>\n#define ll long long\nusing ld = long double;\n#define rep2(i, a, b) for(ll i = (a); i <= (b); i++)\n#define rep3(i, a, b) for(ll i = (a); i >= (b); --i)\n#define rep(i, n) for(ll i = 0; i < n; ++i)\n#define each(i, a) for(auto &&i : a)\n#define pii pair<int, int>\n#define pll pair<ll, ll>\n#define pb push_back\n#define eb emplace_back\n#define vi vector<int>\n#define vll vector<ll>\n#define vpi vector<pii>\n#define vpll vector<pll>\n#define overload2(_1, _2, name, ...) name\n#define vec(type, name, ...) vector<type> name(__VA_ARGS__)\n#define VEC(type, name, size) \\\n vector<type> name(size); \\\n IN(name)\n#define VEC2(type, name1, name2, size) \\\n vector<type> name1(size), name2(size); \\\n for(int i = 0; i < size; i++) IN(name1[i], name2[i])\n#define VEC3(type, name1, name2, name3, size) \\\n vector<type> name1(size), name2(size), name3(size); \\\n for(int i = 0; i < size; i++) IN(name1[i], name2[i], name3[i])\n#define vv(type, name, h, ...) vector<vector<type>> name(h, vector<type>(__VA_ARGS__))\n#define VV(type, name, h, w) \\\n vector<vector<type>> name(h, vector<type>(w)); \\\n IN(name)\n#define vvv(type, name, h, w, ...) vector<vector<vector<type>>> name(h, vector<vector<type>>(w, vector<type>(__VA_ARGS__)))\n#define vvvv(type, name, a, b, c, ...) \\\n vector<vector<vector<vector<type>>>> name(a, vector<vector<vector<type>>>(b, vector<vector<type>>(c, vector<type>(__VA_ARGS__))))\n#define mt make_tuple\n#define fi first\n#define se second\n#define all(c) begin(c), end(c)\n#define rall(c) rbegin(c), rend(c)\n#define SORT(v) sort(all(v))\n#define REV(v) reverse(all(v))\ntemplate <class T = ll, class S> T SUM(const S &v) { return accumulate(all(v), T(0)); }\n#define MIN(v) *min_element(all(v))\n#define MAX(v) *max_element(all(v))\n#define lb(c, x) distance((c).begin(), lower_bound(all(c), (x)))\n#define ub(c, x) distance((c).begin(), upper_bound(all(c), (x)))\nusing namespace std;\nconstexpr pii dx4[4] = {pii{1, 0}, pii{0, 1}, pii{-1, 0}, pii{0, -1}};\nconstexpr pii dx8[8] = {{1, 0}, {1, 1}, {0, 1}, {-1, 1}, {-1, 0}, {-1, -1}, {0, -1}, {1, -1}};\nconst string YESNO[2] = {\"NO\", \"YES\"};\nconst string YesNo[2] = {\"No\", \"Yes\"};\nconst string yesno[2] = {\"no\", \"yes\"};\nvoid YES(bool t = 1) { cout << YESNO[t] << endl; }\nvoid NO(bool t = 1) { YES(!t); }\nvoid Yes(bool t = 1) { cout << YesNo[t] << endl; }\nvoid No(bool t = 1) { Yes(!t); }\nvoid yes(bool t = 1) { cout << yesno[t] << endl; }\nvoid no(bool t = 1) { yes(!t); }\ntemplate <class T> using vc = vector<T>;\ntemplate <class T> using vvc = vector<vc<T>>;\ntemplate <class T> using vvvc = vector<vvc<T>>;\ntemplate <class T> using vvvvc = vector<vvvc<T>>;\ntemplate <class T> using pq = priority_queue<T>;\ntemplate <class T> using pqg = priority_queue<T, vector<T>, greater<T>>;\n#define si(c) (int)(c).size()\n#define INT(...) \\\n int __VA_ARGS__; \\\n IN(__VA_ARGS__)\n#define LL(...) \\\n ll __VA_ARGS__; \\\n IN(__VA_ARGS__)\n#define STR(...) \\\n string __VA_ARGS__; \\\n IN(__VA_ARGS__)\n#define CHR(...) \\\n char __VA_ARGS__; \\\n IN(__VA_ARGS__)\n#define DBL(...) \\\n double __VA_ARGS__; \\\n IN(__VA_ARGS__)\nint scan() { return getchar(); }\nvoid scan(int &a) { cin >> a; }\nvoid scan(long long &a) { cin >> a; }\nvoid scan(char &a) { cin >> a; }\nvoid scan(double &a) { cin >> a; }\nvoid scan(string &a) { cin >> a; }\ntemplate <class T, class S> void scan(pair<T, S> &p) { scan(p.first), scan(p.second); }\ntemplate <class T> void scan(vector<T> &);\ntemplate <class T> void scan(vector<T> &a) {\n for(auto &i : a) scan(i);\n}\ntemplate <class T> void scan(T &a) { cin >> a; }\nvoid IN() {}\ntemplate <class Head, class... Tail> void IN(Head &head, Tail &...tail) {\n scan(head);\n IN(tail...);\n}\n\ntemplate <class T, class S> inline bool chmax(T &a, const S &b) { return (a < b ? a = b, 1 : 0); }\ntemplate <class T, class S> inline bool chmin(T &a, const S &b) { return (a > b ? a = b, 1 : 0); }\nvi iota(int n) {\n vi a(n);\n iota(all(a), 0);\n return a;\n}\ntemplate <typename T> vi iota(vector<T> &a, bool greater = false) {\n vi res(a.size());\n iota(all(res), 0);\n sort(all(res), [&](int i, int j) {\n if(greater) return a[i] > a[j];\n return a[i] < a[j];\n });\n return res;\n}\n#define UNIQUE(x) sort(all(x)), x.erase(unique(all(x)), x.end())\n\ntemplate <class T> T ceil(T x, T y) {\n assert(y >= 1);\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\n\ntemplate <class T> T floor(T x, T y) {\n assert(y >= 1);\n return (x > 0 ? x / y : (x + y - 1) / y);\n}\ntemplate <class T> T POW(T x, int n) {\n T res = 1;\n for(; n; n >>= 1, x *= x)\n if(n & 1) res *= x;\n return res;\n}\ntemplate <class T, class S> T POW(T x, S n, const ll &mod) {\n T res = 1;\n for(; n; n >>= 1, x = x * x % mod)\n if(n & 1) res = res * x % mod;\n return res;\n}\nvector<pll> factor(ll x) {\n vector<pll> ans;\n for(ll i = 2; i * i <= x; i++)\n if(x % i == 0) {\n ans.push_back({i, 1});\n while((x /= i) % i == 0) ans.back().second++;\n }\n if(x != 1) ans.push_back({x, 1});\n return ans;\n}\ntemplate <class T> vector<T> divisor(T x) {\n vector<T> ans;\n for(T i = 1; i * i <= x; i++)\n if(x % i == 0) {\n ans.pb(i);\n if(i * i != x) ans.pb(x / i);\n }\n return ans;\n}\ntemplate <typename T> void zip(vector<T> &x) {\n vector<T> y = x;\n UNIQUE(y);\n for(int i = 0; i < x.size(); ++i) { x[i] = lb(y, x[i]); }\n}\ntemplate <class S> void fold_in(vector<S> &v) {}\ntemplate <typename Head, typename... Tail, class S> void fold_in(vector<S> &v, Head &&a, Tail &&...tail) {\n for(auto e : a) v.emplace_back(e);\n fold_in(v, tail...);\n}\ntemplate <class S> void renumber(vector<S> &v) {}\ntemplate <typename Head, typename... Tail, class S> void renumber(vector<S> &v, Head &&a, Tail &&...tail) {\n for(auto &&e : a) e = lb(v, e);\n renumber(v, tail...);\n}\ntemplate <class S, class... Args> vector<S> zip(vector<S> &head, Args &&...args) {\n vector<S> v;\n fold_in(v, head, args...);\n sort(all(v)), v.erase(unique(all(v)), v.end());\n renumber(v, head, args...);\n return v;\n}\n\n// x in [l, r)\ntemplate <class T, class S> bool inc(const T &x, const S &l, const S &r) { return l <= x and x < r; }\n// (a, b) in [lx, rx) * [ly, ry)\ntemplate <class T, class S> bool inc(const pair<T, T> &x, const S &lx, const S &ly, const S &rx, const S &ry) { return inc(x.fi, lx, rx) && inc(x.se, ly, ry); }\n\nconstexpr ll ten(int n) { return n == 0 ? 1 : ten(n - 1) * 10; }\n// bit 演算系\nll pow2(int i) { return 1LL << i; }\nint topbit(signed t) { return t == 0 ? -1 : 31 - __builtin_clz(t); }\nint topbit(ll t) { return t == 0 ? -1 : 63 - __builtin_clzll(t); }\nint lowbit(signed a) { return a == 0 ? 32 : __builtin_ctz(a); }\nint lowbit(ll a) { return a == 0 ? 64 : __builtin_ctzll(a); }\n// int allbit(int n) { return (1 << n) - 1; }\nconstexpr ll allbit(int n) { return (1LL << n) - 1; }\n// int popcount(signed t) { return __builtin_popcount(t); }\n// int popcount(ll t) { return __builtin_popcountll(t); }\nint popcount(uint64_t t) { return __builtin_popcountll(t); }\nbool ispow2(int i) { return i && (i & -i) == i; }\n\nll rnd(ll l, ll r) { //[l, r)\n#ifdef _LOCAL\n static mt19937_64 gen;\n#else\n static mt19937_64 gen(chrono::steady_clock::now().time_since_epoch().count());\n#endif\n return uniform_int_distribution<ll>(l, r - 1)(gen);\n}\nll rnd(ll n) { return rnd(0, n); }\n\ntemplate <class t> void random_shuffle(vc<t> &a) { rep(i, si(a)) swap(a[i], a[rnd(0, i + 1)]); }\n\nint in() {\n int x;\n cin >> x;\n return x;\n}\nll lin() {\n unsigned long long x;\n cin >> x;\n return x;\n}\n\ntemplate <class T, class S> pair<T, S> operator-(const pair<T, S> &x, const pair<T, S> &y) { return pair<T, S>(x.fi - y.fi, x.se - y.se); }\ntemplate <class T, class S> pair<T, S> operator+(const pair<T, S> &x, const pair<T, S> &y) { return pair<T, S>(x.fi + y.fi, x.se + y.se); }\ntemplate <class T> pair<T, T> operator&(const pair<T, T> &l, const pair<T, T> &r) { return pair<T, T>(max(l.fi, r.fi), min(l.se, r.se)); }\ntemplate <class T, class S> pair<T, S> operator+=(pair<T, S> &l, const pair<T, S> &r) { return l = l + r; }\ntemplate <class T, class S> pair<T, S> operator-=(pair<T, S> &l, const pair<T, S> &r) { return l = l - r; }\ntemplate <class T> bool intersect(const pair<T, T> &l, const pair<T, T> &r) { return (l.se < r.se ? r.fi < l.se : l.fi < r.se); }\n\ntemplate <class T> ll operator*(const pair<T, T> &x, const pair<T, T> &y) { return (ll)x.fi * y.fi + (ll)x.se * y.se; }\n\ntemplate <typename T> struct edge {\n int from, to;\n T cost;\n int id;\n edge(int to, T cost) : from(-1), to(to), cost(cost) {}\n edge(int from, int to, T cost) : from(from), to(to), cost(cost) {}\n edge(int from, int to, T cost, int id) : from(from), to(to), cost(cost), id(id) {}\n constexpr bool operator<(const edge<T> &rhs) const noexcept { return cost < rhs.cost; }\n edge &operator=(const int &x) {\n to = x;\n return *this;\n }\n operator int() const { return to; }\n};\ntemplate <typename T> using Edges = vector<edge<T>>;\n\nusing Tree = vector<vector<int>>;\nusing Graph = vector<vector<int>>;\ntemplate <class T> using Wgraph = vector<vector<edge<T>>>;\nGraph getG(int n, int m = -1, bool directed = false, int margin = 1) {\n Tree res(n);\n if(m == -1) m = n - 1;\n while(m--) {\n int a, b;\n cin >> a >> b;\n a -= margin, b -= margin;\n res[a].emplace_back(b);\n if(!directed) res[b].emplace_back(a);\n }\n return res;\n}\nGraph getTreeFromPar(int n, int margin = 1) {\n Graph res(n);\n for(int i = 1; i < n; i++) {\n int a;\n cin >> a;\n res[a - margin].emplace_back(i);\n }\n return res;\n}\ntemplate <class T> Wgraph<T> getWg(int n, int m = -1, bool directed = false, int margin = 1) {\n Wgraph<T> res(n);\n if(m == -1) m = n - 1;\n while(m--) {\n int a, b;\n T c;\n cin >> a >> b >> c;\n a -= margin, b -= margin;\n res[a].emplace_back(b, c);\n if(!directed) res[b].emplace_back(a, c);\n }\n return res;\n}\nvoid add(Graph &G, int x, int y) { G[x].eb(y), G[y].eb(x); }\ntemplate <class S, class T> void add(Wgraph<S> &G, int x, int y, T c) { G[x].eb(y, c), G[y].eb(x, c); }\n\n#define i128 __int128_t\n#define ull unsigned long long int\n#define TEST \\\n INT(testcases); \\\n while(testcases--)\ntemplate <class T> ostream &operator<<(ostream &os, const vector<T> &v) {\n for(auto it = begin(v); it != end(v); ++it) {\n if(it == begin(v))\n os << *it;\n else\n os << \" \" << *it;\n }\n return os;\n}\ntemplate <class T, class S> ostream &operator<<(ostream &os, const pair<T, S> &p) {\n os << p.first << \" \" << p.second;\n return os;\n}\ntemplate <class S, class T> string to_string(pair<S, T> p) { return \"(\" + to_string(p.first) + \",\" + to_string(p.second) + \")\"; }\nstring to_string(string s) { return \"\\\"\" + s + \"\\\"\"; }\nstring to_string(char c) { return string(1, c); }\ntemplate <class T> string to_string(vector<T> s) {\n string res = \"{\";\n for(auto it = s.begin(); it != s.end(); it++) res += to_string(*it) + (next(it) == s.end() ? \"\" : \", \");\n return res + \"}\";\n}\ntemplate <class T> string to_string(set<T> s) {\n string res = \"{\";\n for(auto it = s.begin(); it != s.end(); it++) res += to_string(*it), res += (next(it) == end(s) ? \"\" : \", \");\n return res + \"}\";\n}\n\n#define endl '\\n'\n\n#ifdef _LOCAL\nvoid dump() { cerr << endl; }\ntemplate <class Head, class... Tail> void dump(Head head, Tail... tail) {\n cerr << to_string(head) << \" \";\n dump(tail...);\n}\n#undef endl\n#define debug(x) \\\n cout << #x << \": \"; \\\n dump(x)\n#else\nvoid dump() {}\ntemplate <class Head, class... Tail> void dump(Head head, Tail... tail) {}\n#define debug(x)\n#endif\ntemplate <class T> void print(const T &a) { cout << a; }\nvoid OUT() { cout << endl; }\ntemplate <class Head, class... Tail> void OUT(const Head &head, const Tail &...tail) {\n print(head);\n if(sizeof...(tail)) cout << ' ';\n OUT(tail...);\n}\n\ntemplate <typename T> static constexpr T inf = numeric_limits<T>::max() / 2;\n#define drop(s) cout << #s << endl, exit(0)\n\ntemplate <int N> struct ndFORarray {\n std::array<int, N> v;\n ndFORarray(std::array<int, N> v_) : v(v_) {}\n struct ndFORitr {\n const std::array<int, N> &v;\n std::array<int, N> tmp;\n bool is_end;\n ndFORitr(const std::array<int, N> &v_) : v(v_), tmp(), is_end(false) {}\n bool operator!=(const ndFORitr &) const { return !is_end; }\n void operator++() {\n int pos = N - 1;\n while(pos != -1) {\n tmp[pos] += 1;\n if(tmp[pos] == v[pos]) {\n tmp[pos] = 0;\n pos -= 1;\n } else {\n break;\n }\n }\n if(pos == -1) { is_end = true; }\n }\n const std::array<int, N> &operator*() const { return tmp; }\n };\n ndFORitr begin() const { return ndFORitr(v); }\n ndFORitr end() const { return ndFORitr(v); }\n};\n\nstruct ndFORvector {\n std::vector<int> v;\n ndFORvector(std::vector<int> v_) : v(v_) {}\n struct ndFORitr {\n const std::vector<int> &v;\n std::vector<int> tmp;\n bool is_end;\n ndFORitr(const std::vector<int> &v_) : v(v_), tmp(v.size(), 0), is_end(false) {}\n bool operator!=(const ndFORitr &) const { return !is_end; }\n void operator++() {\n int pos = v.size() - 1;\n while(pos != -1) {\n tmp[pos] += 1;\n if(tmp[pos] == v[pos]) {\n tmp[pos] = 0;\n pos -= 1;\n } else {\n break;\n }\n }\n if(pos == -1) { is_end = true; }\n }\n const std::vector<int> &operator*() const { return tmp; }\n };\n ndFORitr begin() const { return ndFORitr(v); }\n ndFORitr end() const { return ndFORitr(v); }\n};\n\nauto ndFOR(std::vector<int> v) { return ndFORvector(v); }\ntemplate <class... Ts> auto ndFOR(Ts... v) { return ndFORarray<std::tuple_size<std::tuple<Ts...>>::value>({v...}); }\ntemplate <class F> struct REC {\n F f;\n REC(F &&f_) : f(std::forward<F>(f_)) {}\n template <class... Args> auto operator()(Args &&...args) const { return f(*this, std::forward<Args>(args)...); }\n};\n\ntemplate <class S> vector<pair<S, int>> runLength(const vector<S> &v) {\n vector<pair<S, int>> res;\n for(auto &e : v) {\n if(res.empty() or res.back().fi != e)\n res.eb(e, 1);\n else\n res.back().se++;\n }\n return res;\n}\nvector<pair<char, int>> runLength(const string &v) {\n vector<pair<char, int>> res;\n for(auto &e : v) {\n if(res.empty() or res.back().fi != e)\n res.eb(e, 1);\n else\n res.back().se++;\n }\n return res;\n}\n\ntemplate <class T = int> struct Imos {\n int n;\n vector<T> a;\n Imos(int _n) : n(_n), a(_n + 1) {}\n void add(int l, int r, T val = 1) {\n if(l >= r) return;\n l = clamp(l, 0, n);\n r = clamp(r, 0, n + 1);\n a[l] += val;\n if(r <= n) a[r] -= val;\n }\n void build() {\n for(int i = 0; i < n; i++) a[i + 1] += a[i];\n }\n const T &operator[](int k) { return a[k]; }\n};\n\ntemplate <class T> struct RUI {\n vector<T> a;\n RUI(const vector<T> &v) : a(v.size() + 1) {\n for(int i = 0; i < v.size(); i++) a[i + 1] = a[i] + v[i];\n }\n T get(int l, int r) { return a[r] - a[l]; }\n};\n\ntemplate <class T, class F> T bin_search(T ok, T ng, const F &f) {\n while(abs(ok - ng) > 1) {\n T mid = ok + ng >> 1;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\ntemplate <class T, class F> T bin_search_double(T ok, T ng, const F &f, int iter = 80) {\n while(iter--) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n\nstruct Setup_io {\n Setup_io() {\n ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);\n cout << fixed << setprecision(15);\n }\n} setup_io;\n#pragma endregion\n\nvi id;\nstring s;\nbool flag;\nint calc(int x, char c, int y) {\n if(c == '+') return x + y;\n if(c == '-') return x - y;\n if(c == '*') return x * y;\n if(c == '/') {\n if(y == 0) return flag = false, 0;\n return x / y;\n }\n assert(0);\n}\nint eval(int &i, int p) {\n int x = 0;\n if(s[i] == '(') {\n i++;\n int res = eval(i, inf<int>);\n assert(s[i] == ')');\n x = res;\n i++;\n } else {\n while(i < si(s) and isdigit(s[i])) x = x * 10 + (s[i++] - '0');\n }\n while(1) {\n if(i == si(s) or s[i] == ')') return x;\n char c = s[i];\n int q = id[i];\n if(q < p) {\n i++;\n int y = eval(i, q);\n x = calc(x, c, y);\n } else {\n return x;\n }\n }\n}\n\nint main() {\n while(1) {\n cin >> s;\n if(s == \"#\") exit(0);\n\n int n = si(s);\n vi v;\n rep(i, n) {\n if(s[i] == '+' or s[i] == '-' or s[i] == '*' or s[i] == '/') v.eb(i);\n }\n id.resize(n);\n set<int> ans;\n do {\n rep(i, si(v)) id[v[i]] = i;\n int i = 0;\n flag = true;\n int res = eval(i, inf<int>);\n if(flag) ans.emplace(res);\n } while(next_permutation(all(v)));\n OUT(si(ans));\n }\n}", "accuracy": 1, "time_ms": 460, "memory_kb": 3528, "score_of_the_acc": -0.1928, "final_rank": 2 }, { "submission_id": "aoj_2255_6025445", "code_snippet": "// I SELL YOU...! \n#include<iostream>\n#include<vector>\n#include<algorithm>\n#include<functional>\n#include<queue>\n#include<chrono>\n#include<iomanip>\n#include<map>\n#include<set>\nusing namespace std;\nusing ll = int;\nusing P = pair<ll,ll>;\nusing TP = tuple<ll,ll,ll>;\nvoid init_io(){\n cin.tie(0);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(18);\n}\nconst ll MAX = 11;\nll calc_digit(ll &idx, const string &s) {\n ll res = 0;\n ll n = s.size();\n while(idx < n && '0' <= s[idx] && s[idx] <= '9') {\n res = res * 10 + (s[idx] - '0');\n idx++;\n }\n return res;\n}\nll calc_expr(ll &idx,ll t, const string &s,const vector<ll> &prior) {\n ll n = s.size();\n if (idx > n) return 0;\n if (t < (ll)prior.size()){\n ll t0 = calc_expr(idx, t+1, s, prior);\n if (idx == prior[t]) {\n char c = s[idx];\n idx++;\n ll t1 = calc_expr(idx, t+1, s, prior);\n switch(c) {\n case '+': t0 += t1; break;\n case '-': t0 -= t1; break;\n case '*': t0 *= t1; break;\n case '/': if (t1 == 0) {\n idx = 2*n;\n } else {\n t0 /= t1; break;\n }\n }\n }\n return t0;\n } else if (s[idx] == '(') {\n idx++;\n ll res = calc_expr(idx, 0, s, prior);\n idx++;\n return res;\n } else {\n return calc_digit(idx, s);\n }\n}\nvoid solve() {\n string s;\n cin >> s;\n if (s == \"#\") return;\n vector<ll> prior;\n ll n = s.size();\n for(int i=0;i<n;i++) {\n if (s[i] == '+' || s[i] =='-' || s[i] == '*' || s[i] == '/') {\n prior.push_back(i);\n }\n }\n set<ll> st;\n do {\n ll idx = 0;\n ll v = calc_expr(idx, 0, s, prior);\n if (idx == n) {\n st.insert(v);\n }\n }while(next_permutation(prior.begin(), prior.end()));\n cout << st.size() << endl;\n solve();\n}\nsigned main(){\n init_io();\n solve();\n}", "accuracy": 1, "time_ms": 1110, "memory_kb": 3496, "score_of_the_acc": -0.2672, "final_rank": 5 }, { "submission_id": "aoj_2255_5988944", "code_snippet": "// #pragma comment(linker, \"/stack:200000000\")\n\n#include <bits/stdc++.h>\n\n#include <limits>\n#include <type_traits>\n\nnamespace suisen {\n// ! utility\ntemplate <typename ...Types>\nusing constraints_t = std::enable_if_t<std::conjunction_v<Types...>, std::nullptr_t>;\ntemplate <bool cond_v, typename Then, typename OrElse>\nconstexpr decltype(auto) constexpr_if(Then&& then, OrElse&& or_else) {\n if constexpr (cond_v) {\n return std::forward<Then>(then);\n } else {\n return std::forward<OrElse>(or_else);\n }\n}\n\n// ! function\ntemplate <typename ReturnType, typename Callable, typename ...Args>\nusing is_same_as_invoke_result = std::is_same<std::invoke_result_t<Callable, Args...>, ReturnType>;\ntemplate <typename F, typename T>\nusing is_uni_op = is_same_as_invoke_result<T, F, T>;\ntemplate <typename F, typename T>\nusing is_bin_op = is_same_as_invoke_result<T, F, T, T>;\n\ntemplate <typename Comparator, typename T>\nusing is_comparator = std::is_same<std::invoke_result_t<Comparator, T, T>, bool>;\n\n// ! integral\ntemplate <typename T, typename = constraints_t<std::is_integral<T>>>\nconstexpr int bit_num = std::numeric_limits<std::make_unsigned_t<T>>::digits;\ntemplate <typename T, unsigned int n>\nstruct is_nbit { static constexpr bool value = bit_num<T> == n; };\ntemplate <typename T, unsigned int n>\nstatic constexpr bool is_nbit_v = is_nbit<T, n>::value;\n\n// ?\ntemplate <typename T>\nstruct safely_multipliable {};\ntemplate <>\nstruct safely_multipliable<int> { using type = long long; };\ntemplate <>\nstruct safely_multipliable<long long> { using type = __int128_t; };\ntemplate <>\nstruct safely_multipliable<float> { using type = float; };\ntemplate <>\nstruct safely_multipliable<double> { using type = double; };\ntemplate <>\nstruct safely_multipliable<long double> { using type = long double; };\ntemplate <typename T>\nusing safely_multipliable_t = typename safely_multipliable<T>::type;\n\n} // namespace suisen\n\n// ! type aliases\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nusing ll = long long;\nusing uint = unsigned int;\nusing ull = unsigned long long;\n\ntemplate <typename T> using vec = std::vector<T>;\ntemplate <typename T> using vec2 = vec<vec <T>>;\ntemplate <typename T> using vec3 = vec<vec2<T>>;\ntemplate <typename T> using vec4 = vec<vec3<T>>;\n\ntemplate <typename T>\nusing pq_greater = std::priority_queue<T, std::vector<T>, std::greater<T>>;\ntemplate <typename T, typename U>\nusing umap = std::unordered_map<T, U>;\n\n// ! macros (capital: internal macro)\n#define OVERLOAD2(_1,_2,name,...) name\n#define OVERLOAD3(_1,_2,_3,name,...) name\n#define OVERLOAD4(_1,_2,_3,_4,name,...) name\n\n#define REP4(i,l,r,s) for(std::remove_reference_t<std::remove_const_t<decltype(r)>>i=(l);i<(r);i+=(s))\n#define REP3(i,l,r) REP4(i,l,r,1)\n#define REP2(i,n) REP3(i,0,n)\n#define REPINF3(i,l,s) for(std::remove_reference_t<std::remove_const_t<decltype(l)>>i=(l);;i+=(s))\n#define REPINF2(i,l) REPINF3(i,l,1)\n#define REPINF1(i) REPINF2(i,0)\n#define RREP4(i,l,r,s) for(std::remove_reference_t<std::remove_const_t<decltype(r)>>i=(l)+fld((r)-(l)-1,s)*(s);i>=(l);i-=(s))\n#define RREP3(i,l,r) RREP4(i,l,r,1)\n#define RREP2(i,n) RREP3(i,0,n)\n\n#define rep(...) OVERLOAD4(__VA_ARGS__, REP4 , REP3 , REP2 )(__VA_ARGS__)\n#define rrep(...) OVERLOAD4(__VA_ARGS__, RREP4 , RREP3 , RREP2 )(__VA_ARGS__)\n#define repinf(...) OVERLOAD3(__VA_ARGS__, REPINF3, REPINF2, REPINF1)(__VA_ARGS__)\n\n#define CAT_I(a, b) a##b\n#define CAT(a, b) CAT_I(a, b)\n#define UNIQVAR(tag) CAT(tag, __LINE__)\n#define loop(n) for (std::remove_reference_t<std::remove_const_t<decltype(n)>> UNIQVAR(loop_variable) = n; UNIQVAR(loop_variable) --> 0;)\n\n#define all(iterable) (iterable).begin(), (iterable).end()\n#define input(type, ...) type __VA_ARGS__; read(__VA_ARGS__)\n\n// ! I/O utilities\n\n// pair\ntemplate <typename T, typename U>\nstd::ostream& operator<<(std::ostream& out, const std::pair<T, U> &a) {\n return out << a.first << ' ' << a.second;\n}\n// tuple\ntemplate <unsigned int N = 0, typename ...Args>\nstd::ostream& operator<<(std::ostream& out, const std::tuple<Args...> &a) {\n if constexpr (N >= std::tuple_size_v<std::tuple<Args...>>) {\n return out;\n } else {\n out << std::get<N>(a);\n if constexpr (N + 1 < std::tuple_size_v<std::tuple<Args...>>) {\n out << ' ';\n }\n return operator<<<N + 1>(out, a);\n }\n}\n// vector\ntemplate <typename T>\nstd::ostream& operator<<(std::ostream& out, const std::vector<T> &a) {\n for (auto it = a.begin(); it != a.end();) {\n out << *it;\n if (++it != a.end()) out << ' ';\n }\n return out;\n}\ninline void print() { std::cout << '\\n'; }\ntemplate <typename Head, typename... Tail>\ninline void print(const Head &head, const Tail &...tails) {\n std::cout << head;\n if (sizeof...(tails)) std::cout << ' ';\n print(tails...);\n}\ntemplate <typename Iterable>\nauto print_all(const Iterable& v, std::string sep = \" \", std::string end = \"\\n\") -> decltype(std::cout << *v.begin(), void()) {\n for (auto it = v.begin(); it != v.end();) {\n std::cout << *it;\n if (++it != v.end()) std::cout << sep;\n }\n std::cout << end;\n}\n\n// pair\ntemplate <typename T, typename U>\nstd::istream& operator>>(std::istream& in, std::pair<T, U> &a) {\n return in >> a.first >> a.second;\n}\n// tuple\ntemplate <unsigned int N = 0, typename ...Args>\nstd::istream& operator>>(std::istream& in, std::tuple<Args...> &a) {\n if constexpr (N >= std::tuple_size_v<std::tuple<Args...>>) {\n return in;\n } else {\n return operator>><N + 1>(in >> std::get<N>(a), a);\n }\n}\n// vector\ntemplate <typename T>\nstd::istream& operator>>(std::istream& in, std::vector<T> &a) {\n for (auto it = a.begin(); it != a.end(); ++it) in >> *it;\n return in;\n}\ntemplate <typename ...Args>\nvoid read(Args &...args) {\n ( std::cin >> ... >> args );\n}\n\n// ! integral utilities\n\n// Returns pow(-1, n)\ntemplate <typename T>\nconstexpr inline int pow_m1(T n) {\n return -(n & 1) | 1;\n}\n// Returns pow(-1, n)\ntemplate <>\nconstexpr inline int pow_m1<bool>(bool n) {\n return -int(n) | 1;\n}\n\n// Returns floor(x / y)\ntemplate <typename T>\nconstexpr inline T fld(const T x, const T y) {\n return (x ^ y) >= 0 ? x / y : (x - (y + pow_m1(y >= 0))) / y;\n}\ntemplate <typename T>\nconstexpr inline T cld(const T x, const T y) {\n return (x ^ y) <= 0 ? x / y : (x + (y + pow_m1(y >= 0))) / y;\n}\n\ntemplate <typename T, suisen::constraints_t<suisen::is_nbit<T, 32>> = nullptr>\nconstexpr inline int popcount(const T x) { return __builtin_popcount(x); }\ntemplate <typename T, suisen::constraints_t<suisen::is_nbit<T, 64>> = nullptr>\nconstexpr inline int popcount(const T x) { return __builtin_popcountll(x); }\ntemplate <typename T, suisen::constraints_t<suisen::is_nbit<T, 32>> = nullptr>\nconstexpr inline int count_lz(const T x) { return x ? __builtin_clz(x) : suisen::bit_num<T>; }\ntemplate <typename T, suisen::constraints_t<suisen::is_nbit<T, 64>> = nullptr>\nconstexpr inline int count_lz(const T x) { return x ? __builtin_clzll(x) : suisen::bit_num<T>; }\ntemplate <typename T, suisen::constraints_t<suisen::is_nbit<T, 32>> = nullptr>\nconstexpr inline int count_tz(const T x) { return x ? __builtin_ctz(x) : suisen::bit_num<T>; }\ntemplate <typename T, suisen::constraints_t<suisen::is_nbit<T, 64>> = nullptr>\nconstexpr inline int count_tz(const T x) { return x ? __builtin_ctzll(x) : suisen::bit_num<T>; }\ntemplate <typename T>\nconstexpr inline int floor_log2(const T x) { return suisen::bit_num<T> - 1 - count_lz(x); }\ntemplate <typename T>\nconstexpr inline int ceil_log2(const T x) { return floor_log2(x) + ((x & -x) != x); }\ntemplate <typename T>\nconstexpr inline int kth_bit(const T x, const unsigned int k) { return (x >> k) & 1; }\ntemplate <typename T>\nconstexpr inline int parity(const T x) { return popcount(x) & 1; }\n\nstruct all_subset {\n struct all_subset_iter {\n const int s; int t;\n constexpr all_subset_iter(int s) : s(s), t(s + 1) {}\n constexpr auto operator*() const { return t; }\n constexpr auto operator++() {}\n constexpr auto operator!=(std::nullptr_t) { return t ? (--t &= s, true) : false; }\n };\n int s;\n constexpr all_subset(int s) : s(s) {}\n constexpr auto begin() { return all_subset_iter(s); }\n constexpr auto end() { return nullptr; }\n};\n\n// ! container\n\ntemplate <typename T, typename Comparator, suisen::constraints_t<suisen::is_comparator<Comparator, T>> = nullptr>\nauto priqueue_comp(const Comparator comparator) {\n return std::priority_queue<T, std::vector<T>, Comparator>(comparator);\n}\n\ntemplate <typename Iterable>\nauto isize(const Iterable &iterable) -> decltype(int(iterable.size())) {\n return iterable.size();\n}\n\ntemplate <typename T, typename Gen, suisen::constraints_t<suisen::is_same_as_invoke_result<T, Gen, int>> = nullptr>\nauto generate_vector(int n, Gen generator) {\n std::vector<T> v(n);\n for (int i = 0; i < n; ++i) v[i] = generator(i);\n return v;\n}\n\ntemplate <typename T>\nvoid sort_unique_erase(std::vector<T> &a) {\n std::sort(a.begin(), a.end());\n a.erase(std::unique(a.begin(), a.end()), a.end());\n}\n\n// ! other utilities\n\n// x <- min(x, y). returns true iff `x` has chenged.\ntemplate <typename T>\ninline bool chmin(T &x, const T &y) {\n if (y >= x) return false;\n x = y;\n return true;\n}\n// x <- max(x, y). returns true iff `x` has chenged.\ntemplate <typename T>\ninline bool chmax(T &x, const T &y) {\n if (y <= x) return false;\n x = y;\n return true;\n}\n\nnamespace suisen {}\nusing namespace suisen;\nusing namespace std;\n\nstruct io_setup {\n io_setup(int precision = 20) {\n std::ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout << std::fixed << std::setprecision(precision);\n }\n} io_setup_ {};\n\n// ! code from here\n\nbool is_digit(char c) {\n return '0' <= c and c <= '9';\n}\n\npair<int, int> num(const string &expr, int l) {\n int n = expr.size();\n int r = l;\n while (r < n and is_digit(expr[r])) ++r;\n return { r, stoi(expr.substr(l, r - l)) };\n}\n\npair<int, set<int>> eval(const string &expr, int l) {\n const int n = expr.size();\n vector<set<int>> sets;\n vector<char> ops;\n while (true) {\n if (expr[l] == '(') {\n auto [nl, st] = eval(expr, l + 1);\n assert(expr[nl] == ')');\n sets.push_back(move(st));\n l = nl + 1;\n } else if (is_digit(expr[l])) {\n auto [nl, val] = num(expr, l);\n sets.push_back(set<int> { val });\n l = nl;\n }\n if (l == n or expr[l] == ')') break;\n ops.push_back(expr[l++]);\n }\n auto dfs = [&](auto dfs, vector<set<int>> &vals, vector<char> &ops) -> set<int> {\n int t = vals.size();\n if (t == 1) return vals[0];\n set<int> res;\n rep(i, t - 1) {\n set<int> st;\n char op = ops[i];\n auto sl = move(vals[i]), sr = move(vals[i + 1]);\n for (int lhs : sl) for (int rhs : sr) {\n if (op == '+') {\n st.insert(lhs + rhs);\n } else if (op == '-') {\n st.insert(lhs - rhs);\n } else if (op == '*') {\n st.insert(lhs * rhs);\n } else if (op == '/') {\n if (rhs != 0) st.insert(lhs / rhs);\n } else assert(false);\n }\n ops.erase(ops.begin() + i);\n vals.erase(vals.begin() + i + 1);\n vals.erase(vals.begin() + i);\n vals.insert(vals.begin() + i, move(st));\n res.merge(dfs(dfs, vals, ops));\n vals.erase(vals.begin() + i);\n vals.insert(vals.begin() + i, move(sl));\n vals.insert(vals.begin() + i + 1, move(sr));\n ops.insert(ops.begin() + i, op);\n }\n return res;\n };\n return { l, dfs(dfs, sets, ops) };\n}\n\nint main() {\n while (true) {\n input(string, expr);\n if (expr == \"#\") break;\n print(eval(expr, 0).second.size());\n }\n}", "accuracy": 1, "time_ms": 670, "memory_kb": 3348, "score_of_the_acc": -0.1671, "final_rank": 1 }, { "submission_id": "aoj_2255_5978126", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\nusing ll = long long;\nusing State = string::const_iterator;\nclass ParseError {};\nconstexpr ll MOD = 1e9 + 7;\nconstexpr ll INF = 1e18;\n\n// 四則演算の式をパースして、その評価結果を返す。\nset<int> expression(State &begin);\n\nset<int> factor(State &begin);\n// 数字の列をパースして、その数を返す。\nset<int> number(State &begin);\n\nset<int> solve(vector<set<int>> vv, vector<char> op) {\n if(op.empty()) return vv[0];\n set<int> st;\n for(int i = 0; i < op.size(); i++) {\n auto cpvv = vv;\n auto cpop = op;\n function<int(int,int)> fnc;\n if(cpop[i] == '+') {\n fnc = [](int a, int b) { return a + b; };\n } else if(cpop[i] == '-') {\n fnc = [](int a, int b) { return a - b; };\n } else if(cpop[i] == '*') {\n fnc = [](int a, int b) { return a * b; };\n } else if(cpop[i] == '/') {\n fnc = [](int a, int b) { return a / b; };\n }\n\n set<int> ret;\n for(auto x : cpvv[i]) {\n for(auto y : cpvv[i + 1]) {\n if(!(cpop[i]=='/'&&y==0))ret.insert(fnc(x, y));\n }\n }\n cpvv[i] = ret;\n cpvv.erase(cpvv.begin() + i + 1);\n cpop.erase(cpop.begin() + i);\n auto sol = solve(cpvv, cpop);\n for(auto x:sol)st.insert(x);\n }\n return st;\n}\n\n// 数字の列をパースして,その数を返す\nset<int> number(State &begin) {\n int ret = 0;\n while(isdigit(*begin)) {\n ret *= 10;\n ret += *begin - '0';\n begin++;\n }\n set<int> v;\n v.insert(ret);\n return v;\n}\n\n// カッコを先に計算\nset<int> factor(State &begin) {\n if(*begin == '(') {\n begin++;\n set<int> ret = expression(begin);\n begin++; // ')' を飛ばす\n return ret;\n } else {\n return number(begin);\n }\n}\n\n// 四則演算の式をパースして,結果を返す\nset<int> expression(State &begin) {\n vector<set<int>> vv; // 6/2*(1+2) -> [{6},{2},{3}]\n set<int> ret = factor(begin);\n vv.push_back(ret);\n vector<char> op;\n for(;;) {\n if(*begin == '+') {\n begin++;\n op.push_back('+');\n vv.push_back(factor(begin));\n } else if(*begin == '-') {\n begin++;\n op.push_back('-');\n vv.push_back(factor(begin));\n } else if(*begin == '*') {\n begin++;\n op.push_back('*');\n vv.push_back(factor(begin));\n } else if(*begin == '/') {\n begin++;\n op.push_back('/');\n vv.push_back(factor(begin));\n }else {\n break;\n }\n }\n set<int> sol = solve(vv, op);\n return sol;\n}\n\n// 乗算除算の式をパースして,その結果を返す\n// int term(State &begin){\n// int ret = factor(begin);\n// for(;;){\n// if(*begin == '*'){\n// begin++;\n// ret *= factor(begin);\n// }else if(*begin == '/'){\n// begin++;\n// ret /= factor(begin);\n// }else{\n// break;\n// }\n// }\n// return ret;\n// }\n\nint main() {\n string s;\n while(1) {\n cin >> s;\n if(s == \"#\") break;\n State begin = s.begin();\n cout << expression(begin).size() << endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 1870, "memory_kb": 3456, "score_of_the_acc": -0.3534, "final_rank": 10 } ]
aoj_2249_cpp
Problem H: Road Construction King Mercer is the king of ACM kingdom. There are one capital and some cities in his kingdom. Amazingly, there are no roads in the kingdom now. Recently, he planned to construct roads between the capital and the cities, but it turned out that the construction cost of his plan is much higher than expected. In order to reduce the cost, he has decided to create a new construction plan by removing some roads from the original plan. However, he believes that a new plan should satisfy the following conditions: For every pair of cities, there is a route (a set of roads) connecting them. The minimum distance between the capital and each city does not change from his original plan. Many plans may meet the conditions above, but King Mercer wants to know the plan with minimum cost. Your task is to write a program which reads his original plan and calculates the cost of a new plan with the minimum cost. Input The input consists of several datasets. Each dataset is formatted as follows. N M u 1 v 1 d 1 c 1 . . . u M v M d M c M The first line of each dataset begins with two integers, N and M (1 ≤ N ≤ 10000, 0 ≤ M ≤ 20000). N and M indicate the number of cities and the number of roads in the original plan, respectively. The following M lines describe the road information in the original plan. The i -th line contains four integers, u i , v i , d i and c i (1 ≤ u i , v i ≤ N , u i ≠ v i , 1 ≤ d i ≤ 1000, 1 ≤ c i ≤ 1000). u i , v i , d i and c i indicate that there is a road which connects u i -th city and v i -th city, whose length is d i and whose cost needed for construction is c i . Each road is bidirectional. No two roads connect the same pair of cities. The 1-st city is the capital in the kingdom. The end of the input is indicated by a line containing two zeros separated by a space. You should not process the line as a dataset. Output For each dataset, print the minimum cost of a plan which satisfies the conditions in a line. Sample Input 3 3 1 2 1 2 2 3 2 1 3 1 3 2 5 5 1 2 2 2 2 3 1 1 1 4 1 1 4 5 1 1 5 3 1 1 5 10 1 2 32 10 1 3 43 43 1 4 12 52 1 5 84 23 2 3 58 42 2 4 86 99 2 5 57 83 3 4 11 32 3 5 75 21 4 5 23 43 5 10 1 2 1 53 1 3 1 65 1 4 1 24 1 5 1 76 2 3 1 19 2 4 1 46 2 5 1 25 3 4 1 13 3 5 1 65 4 5 1 34 0 0 Output for the Sample Input 3 5 137 218
[ { "submission_id": "aoj_2249_11056492", "code_snippet": "#include <cstdio>\n#include <algorithm>\n#include <vector>\n#include <cstring>\n#include <queue>\nusing namespace std;\nconst int INF=0x3f3f3f3f;\nconst int maxv=10002;\nstruct edge{int en,dis,cost;};\ntypedef pair<int,int> P;\nint main(){\n int scost[maxv];\n int n,m;\n while(scanf(\"%d%d\",&n,&m)==2&&(n||m)){\n memset(scost,0x3f,sizeof(scost));\n int d[maxv];\n vector<edge> q[maxv];\n for(int i=0;i<m;i++){\n int a,b,c,d;\n scanf(\"%d%d%d%d\",&a,&b,&c,&d);\n q[a].push_back({b,c,d});\n q[b].push_back({a,c,d});\n }\n memset(d,0x3f,sizeof(d));\n d[1]=0;\n priority_queue<P,vector<P>,greater<P>> Q;\n Q.push({d[1],1});\n scost[1]=0;\n while(!Q.empty()){\n P M=Q.top(); Q.pop();\n int u=M.first;\n int v=M.second;\n if(d[v]<u) continue;\n for(int i=0;i<(int)q[v].size();i++){\n int e=q[v][i].en,c=q[v][i].cost,di=q[v][i].dis;\n if(d[e]>d[v]+di){\n d[e]=d[v]+di;\n Q.push({d[e],e});\n scost[e]=c;\n }else if(d[e]==d[v]+di){\n scost[e]=min(scost[e],c);\n }\n }\n }\n long long sum=0;\n for(int i=1;i<=n;i++){\n sum+=scost[i];\n }\n printf(\"%lld\\n\",sum);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 4500, "score_of_the_acc": -0.3107, "final_rank": 4 }, { "submission_id": "aoj_2249_11056489", "code_snippet": "#include <cstdio>\n#include <queue>\n#include <iostream>\n#include <cstring>\n\nusing namespace std;\n\ntypedef pair<int ,int> pii;\n\npriority_queue<pii, vector<pii>, greater<pii> > pq;\n\nint d[10010];\n\nstruct E{\n E() {}\n E(int vv, int cc, int ww): v(vv), c(cc), w(ww) {}\n int v, c, w;\n};\n\nvector<E> e[10010];\nint pre[10010];\n\nvoid Dijkstra(int n, int s, int d[], vector<E> e[]) {\n memset(d + 1, 0x3f, sizeof(int) * n);\n pq.push(make_pair(d[s]=0, s));\n while(!pq.empty()) {\n int u, v, c, m;\n m = pq.top().first;\n u = pq.top().second;\n pq.pop();\n if(m > d[u]) {\n continue;\n }\n for(vector<E>::iterator it = e[u].begin(); it != e[u].end(); ++it){\n const E &edge = *it;\n v = edge.v;\n c = edge.c;\n int t = d[u] + c;\n if(t < d[v]) {\n pq.push(make_pair(d[v]=t, v));\n pre[v] = edge.w;\n } else if(t == d[v]) {\n pre[v] = min(pre[v], edge.w);\n }\n }\n }\n}\n\nint main() {\n int n, m, u, v, c, w;\n\n while(scanf(\"%d%d\", &n, &m) != EOF && n) {\n for(u = 1; u <= n; ++u) {\n e[u].clear();\n }\n while (m--) {\n scanf(\"%d%d%d%d\", &u, &v, &c, &w);\n e[u].push_back(E(v, c, w));\n e[v].push_back(E(u, c, w));\n }\n Dijkstra(n, 1, d, e);\n\n int ans = 0;\n for (u = 2; u <= n; ++u) {\n ans += pre[u];\n }\n printf(\"%d\\n\", ans);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 5628, "score_of_the_acc": -0.6257, "final_rank": 18 }, { "submission_id": "aoj_2249_10898362", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\nstruct edge{\n int to,d,c;\n edge(int x,int y,int z):to(x),d(y),c(z){}\n};\n\nint main(){\n while(1){\n int N,M;\n scanf(\"%d%d\",&N,&M);\n if(N==0&&M==0) break;\n vector<vector<edge>> G(N);\n for(int i=0;i<M;i++){\n int u,v,d,c;\n scanf(\"%d%d%d%d\",&u,&v,&d,&c);\n u--;v--;\n G[u].push_back(edge(v,d,c));\n G[v].push_back(edge(u,d,c));\n }\n\n priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> pq;\n vector<int> dist(N,1e9);\n dist[0] = 0;\n pq.emplace(0,0);\n while(pq.size()){\n auto[d,v] = pq.top();\n pq.pop();\n if(dist[v]<d) continue;\n for(auto [nv,nd,_] : G[v]){\n int cand = d+nd;\n if(dist[nv]>cand){\n dist[nv]=cand;\n pq.emplace(cand,nv);\n }\n }\n }\n\n vector<vector<int>> H(N);\n int ans = 0;\n for(int v=0;v<N;v++){\n for(auto[nv,nd,nc] : G[v]){\n if(dist[v]+nd==dist[nv]){\n H[nv].emplace_back(nc);\n }\n }\n }\n for(int v=0;v<N;v++){\n if(H[v].empty()) continue;\n ans += *min_element(H[v].begin(),H[v].end());\n }\n printf(\"%d\\n\",ans);\n\n\n }\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 4500, "score_of_the_acc": -0.3107, "final_rank": 4 }, { "submission_id": "aoj_2249_10853962", "code_snippet": "#include <iostream>\n#include <algorithm>\n#include <vector>\n#include <cstring>\n#include <queue>\nusing namespace std;\nconst int N = 10100;\nconst int INF = 99999999;\ntypedef struct Edge\n{\n int to, dis, cost;\n Edge(int t=0, int d=0, int c=0) : to(t), dis(d), cost(c) {}\n bool operator >(const Edge &t) const\n {\n return dis != t.dis ? dis > t.dis : cost > t.cost;\n }\n}P;\nvector<Edge> G[N];\nint d[N];\nint V, E;\nbool vis[N];\n\npriority_queue<P, vector<P>, greater<P> > que;\n \nint Dijkstra(int s)\n{\n int res = 0;\n memset(vis, 0, sizeof(vis));\n que.push(P(s, 0, 0));\n while (!que.empty())\n {\n P p = que.top(); que.pop();\n int v = p.to;\n if (vis[v]) continue;\n vis[v] = 1;\n res += p.cost;\n for (int i = 0; i < G[v].size(); ++i)\n {\n Edge e = G[v][i];\n que.push(P(e.to, e.dis + p.dis, e.cost));\n }\n }\n return res;\n}\nint main()\n{\n int f, t, c, d;\n while (cin >> V >> E)\n {\n if (!V && !E) break;\n for (int i = 0; i < V; ++i)\n G[i].clear();\n for (int i = 0; i < E; ++i)\n {\n cin >> f >> t >> d >> c;\n --f, --t;\n G[f].push_back(Edge(t, d, c));\n G[t].push_back(Edge(f, d, c));\n }\n cout << Dijkstra(0) << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 120, "memory_kb": 5204, "score_of_the_acc": -0.5299, "final_rank": 10 }, { "submission_id": "aoj_2249_10684829", "code_snippet": "#include <algorithm>\n#include <iostream>\n#include <cstring>\n#include <cstdlib>\n#include <vector>\n#include <queue>\n#include <cstdio>\n#include <cmath>\n#include <string>\n#include <stack>\n#define PI acos(-1.0)\n#define inf 0x3f3f3f3f\n#define E exp(double(1))\n#define maxn 51000\nusing namespace std;\n\nstruct Edge{\n int from,to,dis,next;\n int w;\n} edge[maxn];\n\nint b[maxn];\nint head[maxn];\nint vis[maxn];\nint dis[maxn];\nint tt[maxn];\nint p[maxn];\nint cnt;\nint n;\nint ll;\nvoid add(int q,int h,int len,int w) {\n edge[cnt].from=q;\n edge[cnt].to=h;\n edge[cnt].dis=len;\n edge[cnt].w=w;\n edge[cnt].next=head[q];\n head[q]=cnt;\n cnt++;\n}\n\nvector<int>vct[maxn];\n\nint Dijkstra(){\n\tint i,j,k,tmp,v;\n\tint flag;\n\tint yy;\n\tmemset(dis,inf,sizeof(dis));\n\tmemset(vis,0,sizeof(vis));\n\tmemset(tt,0,sizeof(tt));\n\tmemset(p,0,sizeof(p));\n\tdis[1]=0;\n\tvis[1]=1;\n\tint next;\n\tint s=1;\n\tyy=0;\n\tfor(int i=0;i<maxn;i++) vct[i].clear();\n\twhile(1){\n\t\ttmp=inf;\n\t\tnext=-1;\n\t\tfor(int i=head[s]; i!=-1; i=edge[i].next){\n v=edge[i].to;\n if(vis[v])continue;\n if(edge[i].dis+dis[s]<dis[v]){\n dis[v]=edge[i].dis+dis[s];\n tt[v]=edge[i].w;\n p[v]=1;\n vct[v].clear();\n vct[v].push_back(i);\n }else if(edge[i].dis+dis[s]==dis[v]){\n tt[v]=max(tt[v],edge[i].w);\n p[v]++;\n vct[v].push_back(i);\n }\n\t\t}\n for(int i=1;i<=n;i++){\n if(vis[i])continue;;\n if(dis[i]<tmp){\n tmp=dis[i];\n next=i;\n }\n }\n\t\tif(next==-1)break;\n\n\t\tfor(i=0;i<vct[next].size();i++){\n // printf(\"%d __> %d\\n\",edge[vct[next][i]].from,edge[vct[next][i]].to);\n if(b[vct[next][i]])continue;\n ll+=edge[vct[next][i]].w;\n b[vct[next][i]]=1;\n\t\t}\n\t\tvis[next]=1;\n\t\ts=next;\n\t\tif(p[next]>=2){\n yy+=tt[next];\n\t\t}\n\t}\n\treturn yy;\n}\n\n\n\nint main(){\n int m;\n int q,h,len,w,i,j;\n int ans;\n int ff;\n while(scanf(\"%d%d\",&n,&m)!=EOF,n+m){\n ll=0;\n cnt=0;\n memset(b,0,sizeof(b));\n memset(head,-1,sizeof(head));\n for(i=0;i<m;i++){\n scanf(\"%d%d%d%d\",&q,&h,&len,&w);\n add(q,h,len,w);\n add(h,q,len,w);\n }\n\n ff=Dijkstra();\n // printf(\"%d\\n\",ll);\n printf(\"%d\\n\",ll-ff);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 880, "memory_kb": 6956, "score_of_the_acc": -1.2493, "final_rank": 20 }, { "submission_id": "aoj_2249_10684808", "code_snippet": "// 最小树形图:POJ3164\n// 给有向带权图中指定一个特殊的点root,\n// 求一棵以root为根的有向生成树T,并且T中所有边的总权值最小\n\n#include <iostream>\n#include <cstdio>\n#include <cstring>\n#include <sstream>\n#include <algorithm>\n#include <cmath>\n#include <map>\n#include <functional>\n#include <queue>\n#include <vector>\n#include <cstdlib>\n#include <string>\nusing namespace std;\nconst int N=10010;\nconst int INF=1000000000;\nconst int eps=1e-8;\nstruct Node\n{\n int u,v,w,c,next;\n}e[1000000];\nint head[N],cnt;\nvoid add(int u,int v,int w)\n{\n e[cnt].u=u;e[cnt].v=v;e[cnt].w=w;\n cnt++;\n}\nvoid Add(int u,int v,int w,int c)\n{\n e[cnt].u=u;e[cnt].v=v;e[cnt].w=w;e[cnt].c=c;e[cnt].next=head[u];\n head[u]=cnt++;\n}\nint In[N],x[N],y[N];\nint pre[N],ID[N],vis[N];\nint n,m;\nint dis[N],in[N];\nqueue<int>q;\nvoid spfa()\n{\n for(int i=1;i<=n;i++)\n dis[i]=INF,in[i]=0;\n dis[1]=0;q.push(1);\n while(!q.empty())\n {\n int u=q.front();q.pop();in[u]=0;\n for(int i=head[u];i!=-1;i=e[i].next)\n {\n int v=e[i].v;\n if(dis[v]>dis[u]+e[i].w)\n {\n dis[v]=dis[u]+e[i].w;\n if(!in[v])\n {\n in[v]=1;q.push(v);\n }\n }\n }\n }\n}\nint MST(int rot)\n{\n int ret=0;\n int V=n,E=cnt,now,u,v; // V 当前点的个数 E 当前边的个数\n while(1)\n {\n //1.找最小入边\n for(int i=1;i<=V;i++)\n vis[i]=ID[i]=-1,In[i]=INF;\n for(int i=0;i<E;i++)\n {\n u=e[i].u,v=e[i].v;\n if(In[v]>e[i].w && u!=v)\n {\n pre[v]=u;\n In[v]=e[i].w;\n }\n }\n\n for(int i=1;i<=V;i++)\n if(i!=rot&&In[i]>=INF)\n return -1;//除了跟以外有点没有入边,则根无法到达它\n\n //2.找环\n now=0;In[rot]=0;\n for(int i=1;i<=V;i++)\n {\n ret+=In[i];\n v=i;\n while( vis[v]!=i && v!=rot && ID[v]==-1 ) // pre[v] 可能是在另一个环内\n {\n vis[v]=i;\n v=pre[v];\n }\n if( v!=rot && ID[v]==-1 ) // 找到环\n {\n now++; //标记每个环\n for( u=pre[v]; u!=v; u=pre[u])\n ID[u]=now;\n ID[v]=now;\n }\n }\n if(now==0) //无环\n return ret;\n for(int i=1;i<=V;i++)\n if(ID[i]==-1)\n ID[i]=++now;\n V=now;rot=ID[rot]; // 更新 V 和 rot\n\n //3.缩点,重新标记\n cnt=0;\n for(int i=0;i<E;i++)\n {\n v=e[i].v;u=e[i].u;\n if(ID[u]!=ID[v])\n add(ID[u],ID[v],e[i].w-In[v]); //重新建边\n }\n E=cnt;\n }\n return ret;\n}\nvoid build()\n{\n int now=cnt;\n cnt=0;\n for(int i=0;i<now;i++)\n {\n int u=e[i].u,v=e[i].v;\n if(dis[v]==dis[u]+e[i].w&&u!=v&&v!=1)\n {\n add(u,v,e[i].c);\n }\n }\n}\nint main()\n{\n while(scanf(\"%d%d\",&n,&m)!=EOF)\n {\n if(n==0)break;\n memset(head,-1,sizeof(head));\n cnt=0;\n for(int i=0;i<m;i++)\n {\n int u,v,w,d;\n scanf(\"%d%d%d%d\",&u,&v,&w,&d);\n Add(u,v,w,d);Add(v,u,w,d);\n }\n spfa();\n build();\n int ans=MST(1);\n printf(\"%d\\n\",ans);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 4320, "score_of_the_acc": -0.257, "final_rank": 2 }, { "submission_id": "aoj_2249_10262845", "code_snippet": "#include<iostream>\n#include<queue>\n#include<vector>\n#include<math.h>\n#include<string.h>\nusing namespace std;\n#define MAX_N 10005\nconst int INF=0x3f3f3f3f;\ntypedef pair<int,int> P;\n\nstruct edge{int to,dist,cost;};\nvector<edge> G[MAX_N];\nint d[MAX_N];\nint N,M;\nint ans;\n\nvoid dijkstra(int s)\n{\n\tpriority_queue<P,vector<P>,greater<P>> que;\n\tfill(d,d+N+1,INF);\n\td[s]=0;\n\tque.push(P(0,s));\n\t\n\twhile(!que.empty())\n\t{\n\t\tP p=que.top();que.pop();\n\t\tint v=p.second;\n\t\tif(d[v]<p.first) continue;\n\t\tfor(int i=0;i<G[v].size();++i)\n\t\t{\n\t\t\t edge e=G[v][i];\n\t\t\t if(d[e.to]>d[v]+e.dist)\n\t\t {\n\t\t \t d[e.to]=d[v]+e.dist;\n\t\t \t que.push(P(d[e.to],e.to));\n\t\t }\n\t\t}\n\t}\n}\nint main()\n{\n\tint U,V,D,C;\n\twhile(~scanf(\"%d%d\",&N,&M)&&(N||M))\n\t{\n\t\tans=0;\n\t\tfor(int i=0;i<M;++i)\n\t\t{\n\t\t\tscanf(\"%d%d%d%d\",&U,&V,&D,&C);\n G[U].push_back((edge){V,D,C});\n G[V].push_back((edge){U,D,C});\t\t \n\t\t}\n\t\tdijkstra(1);\n for(int i=2;i<=N;++i)\n {\n \tint min_cost=INF;\n \tfor(int j=0;j<G[i].size();++j)\n \t{\n \t\tedge e=G[i][j];\n \t\tif(d[e.to]+e.dist==d[i]&&e.cost<min_cost)\n \t\t{\n \t\t\tmin_cost=e.cost;\n\t\t\t\t}\n\t\t\t}\n\t\t\tans+=min_cost;\n\t\t}\n\t\tprintf(\"%d\\n\",ans);\n\t\tfor(int i=1;i<=N;++i)\n\t\tG[i].clear();\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 5584, "score_of_the_acc": -0.6133, "final_rank": 17 }, { "submission_id": "aoj_2249_10212182", "code_snippet": "#include <algorithm>\n#include <cstdio>\n#include <vector>\n#include <cstring>\n#include <queue>\nusing namespace std;\n\nconst int INF = 0x3f3f3f3f;\nstruct edge {\n int to, cost, c;\n};\ntypedef pair<int, int> P;\nint d[10010], N, M;\nP pre[10010];\nvector<edge> G[10010]; // 索引from\n\nvoid dijkstra() {\n memset(d, INF, sizeof(d));\n priority_queue<P, vector<P>, greater<P> > que;\n d[1] = 0;\n que.push(P(0, 1));\n while (!que.empty()) {\n P p = que.top();\n que.pop();\n int v = p.second;\n if (d[v] < p.first) continue;\n for (int i = 0; i < G[v].size(); ++i) {\n edge e = G[v][i];\n int new_dist = d[v] + e.cost;\n if (d[e.to] > new_dist) {\n d[e.to] = new_dist;\n que.push(P(d[e.to], e.to));\n pre[e.to].first = v;\n pre[e.to].second = i;\n } else if (d[e.to] == new_dist) {\n // 检查当前边的成本是否更小\n if (pre[e.to].first == 0) { // 初始未被设置的情况\n pre[e.to].first = v;\n pre[e.to].second = i;\n } else {\n int current_c = G[pre[e.to].first][pre[e.to].second].c;\n if (e.c < current_c) {\n pre[e.to].first = v;\n pre[e.to].second = i;\n }\n }\n }\n }\n }\n}\n\nint find_cost() {\n int ans = 0;\n for (int u = 2; u <= N; ++u) {\n ans += G[pre[u].first][pre[u].second].c;\n }\n return ans;\n}\n\nint main() {\n int u, v, dd, c;\n while (scanf(\"%d %d\", &N, &M) == 2 && N + M) {\n for (int i = 1; i <= N; ++i) G[i].clear();\n for (int i = 1; i <= M; ++i) {\n scanf(\"%d %d %d %d\", &u, &v, &dd, &c);\n G[u].push_back({v, dd, c});\n G[v].push_back({u, dd, c});\n }\n memset(pre, 0, sizeof(pre));\n dijkstra();\n printf(\"%d\\n\", find_cost());\n }\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 5120, "score_of_the_acc": -0.4825, "final_rank": 8 }, { "submission_id": "aoj_2249_10129978", "code_snippet": "#include<iostream>\n#include<cstring>\nusing namespace std;\nconst int N=10000+10,M=20000+10;\nint n,m;\nint dist[N],cost[N],idx;\nstruct edge{\n\tint u;\n\tint v;\n\tint w;\n\tint cost;\n}e[2*M];\nvoid add(int u,int v,int w,int c)\n{\n\te[idx].u=u;\n\te[idx].v=v;\n\te[idx].w=w;\n\te[idx].cost=c;\n\t++idx;\n}\nvoid solve()\n{\n\twhile(true)\n\t{\n\t\tbool update=false;\n\t\tfor(int i=0;i<2*m;++i)\n\t\t{\n\t\t\tint x=e[i].u;\n\t\t\tint y=e[i].v;\n\t\t\tint w=e[i].w;\n\t\t\tint c=e[i].cost;\n\t\t\tif(dist[x]+w<dist[y])\n\t\t\t{\n\t\t\t\tdist[y]=dist[x]+w;\n\t\t\t\tcost[y]=cost[x]+c;\n\t\t\t\tupdate=true;\n\t\t\t}\n\t\t\telse if(dist[x]+w==dist[y])\n\t\t\t\tcost[y]=min(c,cost[y]);\n\t\t}\n\t\tif(!update)\n\t\t\tbreak;\n\t}\n} \nint main()\n{\n\twhile(cin>>n>>m&&n)\n\t{\n\t\tmemset(e,0,sizeof e);\n\t\tidx=0;\n\t\tmemset(dist,0x3f,sizeof dist);\n\t\tmemset(cost,0x3f,sizeof cost);\n\t\tdist[1]=cost[1]=0;\n\t\tfor(int i=1;i<=m;++i)\n\t\t{\n\t\t\tint u,v,d,c;\n\t\t\tcin>>u>>v>>d>>c;\n\t\t\tadd(u,v,d,c);\n\t\t\tadd(v,u,d,c);\n\t\t}\n\t\tsolve();\n\t\tint sum=0;\n\t\tfor(int i=2;i<=n;++i)\n\t\t\tsum+=cost[i];\n\t\tcout<<sum<<endl;\n\t}\n\t\n\treturn 0;\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 4120, "score_of_the_acc": -0.2185, "final_rank": 1 }, { "submission_id": "aoj_2249_9963988", "code_snippet": "// #define _GLIBCXX_DEBUG\n//#pragma GCC optimize(\"O3,unroll-loops\")\n//#pragma GCC target(\"avx2,bmi,bmi2,lzcnt,popcnt\")\n//#pragma GCC optimize(\"trapv\")\n#include <bits/stdc++.h>\n#include <ext/pb_ds/assoc_container.hpp> \n#include <ext/pb_ds/tree_policy.hpp> \n\nusing namespace std;\n\ntemplate<class T> using ordered_set = __gnu_pbds::tree<T,\n __gnu_pbds::null_type,\n less<T>,\n __gnu_pbds::rb_tree_tag,\n __gnu_pbds::tree_order_statistics_node_update>;\n\n#define int long long\n#define endl '\\n'\n#define all(v) v.begin(), v.end()\n\n#ifdef LOCAL\n#define debug(x) cout << __LINE__ << \": \" << (#x) << \" = \" << (x) << endl\n#else\n#define debug(x)\n#endif\n\n#ifdef LOCAL\n#define debug_vec(x) \\\n cout << __LINE__ << \": \" << (#x) << \" = \"; \\\n for (auto &y : x) \\\n cout << y << \" \"; \\\n cout << endl;\n#else\n#define debug_vec(x)\n#endif\n\n\nusing ll = long long;\nusing ull = unsigned long long;\nusing ld = long double;\nusing pii = pair<int, int>;\nusing pll = pair<ll, ll>;\n\nauto now_rand = chrono::high_resolution_clock::now();\nmt19937 rnd(now_rand.time_since_epoch().count());\n\nconst ll mod = 1e9 + 7;\nint n, m;\n\nstruct edge {\n int u, dist, c;\n bool operator>(const edge& oth) const {\n if (dist == oth.dist)\n if (c == oth.c)\n return u > oth.u;\n else\n return c > oth.c;\n return dist > oth.dist;\n }\n};\n\ninline void solve() {\n vector<vector<edge>> g(n + 1);\n for (auto i = 0; i < m; ++i) {\n int u, v, dist, c;\n cin >> u >> v >> dist >> c;\n g[u].push_back({ v, dist, c });\n g[v].push_back({ u, dist, c });\n }\n priority_queue<edge, vector<edge>, greater<edge>> s;\n s.push({ 1, 0, 0 }); // dist, c\n vector<int> used(n + 1, 1e9);\n int ans = 0;\n while (!s.empty()) {\n auto [v, dist, c] = s.top();\n s.pop();\n if (dist >= used[v])\n continue;\n used[v] = dist;\n ans += c;\n for (auto const& [u, dist1, c1] : g[v]) {\n if (used[u] > used[v] + dist1) {\n s.push({ u, used[v] + dist1, c1 });\n }\n }\n }\n cout << ans << '\\n';\n}\n\nsigned main() {\n#ifdef LOCAL\n freopen(\"input.txt\", \"r\", stdin);\n freopen(\"output.txt\", \"w\", stdout);\n#endif\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << fixed << setprecision(10);\n cin >> n >> m;\n while (n) {\n solve();\n cin >> n >> m;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 5504, "score_of_the_acc": -0.5967, "final_rank": 15 }, { "submission_id": "aoj_2249_9961618", "code_snippet": "// #define _GLIBCXX_DEBUG\n//#pragma GCC optimize(\"O3,unroll-loops\")\n//#pragma GCC target(\"avx2,bmi,bmi2,lzcnt,popcnt\")\n//#pragma GCC optimize(\"trapv\")\n#include <bits/stdc++.h>\n#include <ext/pb_ds/assoc_container.hpp> \n#include <ext/pb_ds/tree_policy.hpp> \n\nusing namespace std;\n\ntemplate<class T> using ordered_set = __gnu_pbds::tree<T,\n __gnu_pbds::null_type,\n less<T>,\n __gnu_pbds::rb_tree_tag,\n __gnu_pbds::tree_order_statistics_node_update>;\n\n#define int long long\n#define endl '\\n'\n#define all(v) v.begin(), v.end()\n\n#ifdef LOCAL\n#define debug(x) cout << __LINE__ << \": \" << (#x) << \" = \" << (x) << endl\n#else\n#define debug(x)\n#endif\n\n#ifdef LOCAL\n#define debug_vec(x) \\\n cout << __LINE__ << \": \" << (#x) << \" = \"; \\\n for (auto &y : x) \\\n cout << y << \" \"; \\\n cout << endl;\n#else\n#define debug_vec(x)\n#endif\n\n\nusing ll = long long;\nusing ull = unsigned long long;\nusing ld = long double;\nusing pii = pair<int, int>;\nusing pll = pair<ll, ll>;\n\nauto now_rand = chrono::high_resolution_clock::now();\nmt19937 rnd(now_rand.time_since_epoch().count());\n\nconst ll mod = 1e9 + 7;\nint n, m;\n\nstruct edge {\n int u, dist, c;\n bool operator<(const edge& oth) const {\n if (dist == oth.dist)\n if (c == oth.c)\n return u < oth.u;\n else\n return c < oth.c;\n return dist < oth.dist;\n }\n};\n\ninline void solve() {\n vector<vector<edge>> g(n + 1);\n for (auto i = 0; i < m; ++i) {\n int u, v, dist, c;\n cin >> u >> v >> dist >> c;\n g[u].push_back({ v, dist, c });\n g[v].push_back({ u, dist, c });\n }\n set<edge> s;\n s.insert({ 1, 0, 0 }); // dist, c\n vector<int> used(n + 1, 1e9);\n int ans = 0;\n while (!s.empty()) {\n auto [v, dist, c] = *s.begin();\n s.erase(s.begin());\n if (dist >= used[v])\n continue;\n used[v] = dist;\n ans += c;\n for (auto const& [u, dist1, c1] : g[v]) {\n if (used[u] > used[v] + dist1) {\n s.insert({ u, used[v] + dist1, c1 });\n }\n }\n }\n cout << ans << '\\n';\n}\n\nsigned main() {\n#ifdef LOCAL\n freopen(\"input.txt\", \"r\", stdin);\n freopen(\"output.txt\", \"w\", stdout);\n#endif\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << fixed << setprecision(10);\n cin >> n >> m;\n while (n) {\n solve();\n cin >> n >> m;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 5528, "score_of_the_acc": -0.6064, "final_rank": 16 }, { "submission_id": "aoj_2249_9907990", "code_snippet": "#include<bits/stdc++.h>\n// #include<atcoder/all>\n// #include<boost/multiprecision/cpp_int.hpp>\n\nusing namespace std;\n// using namespace atcoder;\n// using bint = boost::multiprecision::cpp_int;\nusing ll = long long;\nusing ull = unsigned long long;\nusing ld = long double;\nusing P = pair<ll,ll>;\nusing vi = vector<ll>;\nusing vvi = vector<vi>;\nusing vvvi = vector<vvi>;\nusing ve = vector<vector<int>>;\nusing vb = vector<bool>;\nusing vvb = vector<vb>;\n#define rep(i,n) for(ll i = 0;i < (ll)n;i++)\n#define ALL(x) (x).begin(),(x).end()\n#define sz(c) ((ll)(c).size())\n#define LB(A,x) (int)(lower_bound(A.begin(),A.end(),x)-A.begin())\n#define UB(A,x) (int)(upper_bound(A.begin(),A.end(),x)-A.begin())\n// #define MOD 1000000007\n#define MOD 998244353\ntemplate<typename T>using min_priority_queue=priority_queue<T,vector<T>,greater<T>>;\ntemplate<typename T>ostream&operator<<(ostream&os,vector<T>&v){for(int i = 0;i < v.size();i++)os<<v[i]<<(i+1!=v.size()?\" \":\"\");return os;}\ntemplate<typename T>istream&operator>>(istream&is,vector<T>&v){for(T&in:v)is>>in;return is;}\ntemplate<typename T1,typename T2>ostream&operator<<(ostream&os,pair<T1,T2>&p){os<<p.first<<\" \"<<p.second;return os;}\ntemplate<typename T1,typename T2>istream&operator>>(istream&is,pair<T1,T2>&p){is>>p.first>>p.second;return is;}\ntemplate<typename T> inline bool chmax(T &a,T b){if(a < b){a = b;return true;}return false;}\ntemplate<typename T> inline bool chmin(T &a,T b){if(a > b){a = b;return true;}return false;}\nld dist(ld x1,ld y1,ld x2, ld y2){return sqrtl((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));}\n\nconst ll inf = 1ll << 50;\n\nbool solve(){\n int n,m;cin >> n >> m;\n if(!n)return false;\n vector<vector<tuple<ll,ll,ll>>> v(n);\n rep(i,m){\n int a,b,d,c;cin >> a >> b >> d >> c;\n a--;b--;\n v[a].emplace_back(make_tuple(b,d,c));\n v[b].emplace_back(make_tuple(a,d,c));\n }\n min_priority_queue<tuple<ll,ll,int>> que;\n que.push({0,0,0});\n vector<P> d(n,{inf,inf});\n d[0] = {0,0};\n while(!que.empty()){\n auto [od,oc,ov] = que.top();que.pop();\n if(make_pair(od,oc) > d[ov])continue;\n for(auto [nv,nd,nc] : v[ov]){\n if(d[nv] <= make_pair(od+nd,nc))continue;\n d[nv] = make_pair(od+nd,nc);\n que.push({od+nd,nc,nv});\n }\n }\n ll res = 0;\n rep(i,n)res += d[i].second;\n cout << res << \"\\n\";\n\n return true;\n}\n\nint main(){\n \n ios_base::sync_with_stdio(0), cin.tie(0);\n while(solve());\n \n\n return 0;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 5244, "score_of_the_acc": -0.5204, "final_rank": 9 }, { "submission_id": "aoj_2249_9860544", "code_snippet": "#ifdef t9unkubj\n#include\"debug.cpp\"\n//#include\"template_no_debug.h\"\n#else \n#define dbg(...) 199958\n#endif\n\n#undef _GLIBCXX_DEBUG\n#pragma GCC optimize(\"O3\")\nusing namespace std;\n#include<bits/stdc++.h>\nusing ll=long long;\nusing ull=unsigned long long;\ntemplate<class T>using vc=vector<T>;\ntemplate<class T>using vvc=vc<vc<T>>;\n#define rep(i,n) for(ll i=0;i<(ll)(n);i++)\n#define REP(i,j,n) for(ll i=(j);i<(ll)(n);i++)\n#define DREP(i,n,m) for(ll i=(n);i>=(m);i--)\n#define drep(i,n) for(ll i=((n)-1);i>=0;i--)\n#define all(x) x.begin(),x.end()\n#define rall(x) x.rbegin(),x.rend()\ntemplate<class T,class F>\nbool chmin(T &x, F y){\n if(x>y){\n x=y;\n return true;\n }\n return false;\n}\ntemplate<class T, class F>\nbool chmax(T &x, F y){\n if(x<y){\n x=y;\n return true;\n }\n return false;\n}\ndouble pass_time=0;\nvoid solve(){\n while(1){\n int n,m;\n cin>>n>>m;\n if(n==0&&m==0)break;\n vvc<array<int,3>>g(n);\n rep(i,m){\n int u,v,d,c;\n cin>>u>>v>>d>>c;\n --u,--v;\n g[u].push_back({v,d,c});\n g[v].push_back({u,d,c});\n }\n vc<int>md(n,1e9);\n md[0]=0;\n using T=pair<int,int>;\n priority_queue<T,vc<T>,greater<>>que;\n que.push({0,0});\n while(que.size()){\n auto [p,q]=que.top();que.pop();\n if(md[q]!=p)continue;\n for(auto&[to,len,cost]:g[q]){\n if(chmin(md[to],md[q]+len)){\n que.push({md[to],to});\n }\n }\n }\n ll ans=0;\n REP(i,1,n){\n ll res=2e9;\n for(auto&[to,len,cost]:g[i]){\n if(md[to]+len==md[i]){\n chmin(res,cost);\n }\n }\n ans+=res;\n }\n cout<<ans<<endl;\n }\n}\nsigned main(){\n cin.tie(0)->sync_with_stdio(0);\n pass_time=clock();\n int t=1;\n //cin>>t;\n while(t--)solve();\n pass_time=clock()-pass_time;\n dbg(pass_time/CLOCKS_PER_SEC);\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 4408, "score_of_the_acc": -0.2848, "final_rank": 3 }, { "submission_id": "aoj_2249_9706662", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n/**\n * @brief template\n */\n\nint main() {\nwhile(1) {\n int N, M;\n cin >> N >> M;\n if (N == 0) return 0;\n vector<int> A(M), B(M), D(M), C(M);\n rep(i,0,M) cin >> A[i] >> B[i] >> D[i] >> C[i], A[i]--, B[i]--;\n vector<vector<pair<int,int>>> G(N);\n rep(i,0,M) {\n G[A[i]].push_back({B[i],D[i]});\n G[B[i]].push_back({A[i],D[i]});\n }\n vector<int> DP(N,inf);\n priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> PQ;\n DP[0] = 0;\n PQ.push({0,0});\n while(!PQ.empty()) {\n auto [D,V] = PQ.top();\n PQ.pop();\n for (auto E : G[V]) {\n int ND = D + E.second, NV = E.first;\n if (chmin(DP[NV],ND)) {\n PQ.push({ND,NV});\n }\n }\n }\n vector<int> ord(M);\n iota(ALL(ord),0);\n sort(ALL(ord),[&](int i, int j){return C[i] < C[j];});\n int ANS = 0;\n vector<bool> used(N,false);\n for (int i : ord) {\n int X = A[i], Y = B[i];\n if (abs(DP[X]-DP[Y]) != D[i]) continue;\n if (DP[X] > DP[Y]) swap(X,Y);\n if (used[Y]) continue;\n used[Y] = true;\n ANS += C[i];\n }\n cout << ANS << endl;\n}\n}", "accuracy": 1, "time_ms": 110, "memory_kb": 4796, "score_of_the_acc": -0.412, "final_rank": 7 }, { "submission_id": "aoj_2249_9661654", "code_snippet": "#include <bits/stdc++.h>\n//#include <atcoder/all>\n#include <unordered_set>\n#include <functional>\nusing namespace std;\n//using namespace atcoder;\nusing ll = long long;\nusing P = pair<int, int>;\n#define rep(i, n) for (int i = 0; i < (n); i++)\n//void chmin(ll & a, ll b) { a = min(a, b); }\n//using mint = modint998244353;\n//using mint = modint1000000007;\n\nstruct edge {\n\tint v;\n\tll w;\n\tll cost;\n\tedge() {};\n\tedge(int v,ll w, ll cost):v(v),w(w),cost(cost){}\n};\n\nvector<ll> dijkstra(int n, vector<vector<edge>>& G, int s) {\n\tvector<ll> d(n, LLONG_MAX / 10);\n\td[s] = 0;\n\tpriority_queue<pair<ll, int>, vector<pair<ll,int>>, greater<pair<ll,int>>> que;\n\tque.push(make_pair(0ll, s));\n\twhile (!que.empty()) {\n\t\tauto p = que.top();\n\t\tque.pop();\n\t\tint u = p.second;\n\t\tll dist = p.first;\n\t\tif (dist > d[u]) continue;\n\t\tfor (edge e : G[u]) {\n\t\t\tif (d[e.v] > d[u] + e.w) {\n\t\t\t\td[e.v] = d[u] + e.w;\n\t\t\t\tque.push(make_pair(d[e.v], e.v));\n\t\t\t}\n\t\t}\n\t}\n\treturn d;\n}\n\n\nint main() {\n\tint N, M;\n\twhile (cin >> N >> M) {\n\t\tif (N == 0 && M == 0)break;\n\t\tvector<vector<edge>> G(N);\n\t\twhile (M--) {\n\t\t\tint u, v, d, c;\n\t\t\tcin >> u >> v >> d >> c;\n\t\t\tu--, v--;\n\t\t\tG[u].emplace_back(v, d, c);\n\t\t\tG[v].emplace_back(u, d, c);\n\t\t}\n\t\tauto dist = dijkstra(N, G, 0);\n\t\tll ans = 0;\n\t\tfor (int i = 1; i < N; i++) {\n\t\t\tll mini = 101010;\n\t\t\tfor (edge e : G[i]) {\n\t\t\t\tif (dist[i] == dist[e.v] + e.w) {\n\t\t\t\t\tmini = min(mini, e.cost);\n\t\t\t\t}\n\t\t\t}\n\t\t\tans += mini;\n\t\t}\n\t\tcout << ans << endl;\n\t}\n}", "accuracy": 1, "time_ms": 110, "memory_kb": 5260, "score_of_the_acc": -0.5428, "final_rank": 11 }, { "submission_id": "aoj_2249_9316857", "code_snippet": "#include <iostream>\n#include <algorithm>\n#include <tuple>\nusing namespace std;\ntypedef tuple<int, int, int, int> tu;\n\nconst int INF = (int)1e9;\nconst int MAX_N = 10000, MAX_M = 20000;\n\ntu edges[MAX_M];\nint d[MAX_N];\nint N, M, ui, vi, di, ci;\n\nint main() {\n while (cin >> N >> M) {\n if (N == 0 && M == 0)\n break;\n for (int i = 0; i < M; i++) {\n cin >> ui >> vi >> di >> ci;\n ui--; vi--;\n edges[i] = make_tuple(ci, di, ui, vi);\n }\n sort(edges, edges + M);\n fill(d, d + N, INF);\n bool update = true;\n int res = 0;\n d[0] = 0;\n while (update) {\n update = false;\n int tmp1, tmp2, tmp3 = INF, tmp4, tmp5;\n for (int i = 0; i < M; i++) {\n int& s = get<2>(edges[i]), g = get<3>(edges[i]), dt = get<1>(edges[i]), ct = get<0>(edges[i]);\n if (d[s] != INF && d[g] > d[s] + dt && d[s] + dt < tmp3) {\n update = true;\n tmp1 = s; tmp2 = g; tmp3 = d[s] + dt; tmp4 = dt; tmp5 = ct;\n }\n if (d[g] != INF && d[s] > d[g] + dt && d[g] + dt < tmp3) {\n update = true;\n tmp1 = s; tmp2 = g; tmp3 = d[g] + dt; tmp4 = dt; tmp5 = ct;\n }\n }\n if (update) {\n d[tmp1] = min(d[tmp1], d[tmp2] + tmp4);\n d[tmp2] = min(d[tmp2], d[tmp1] + tmp4);\n res += tmp5;\n }\n }\n cout << res << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 3410, "memory_kb": 3408, "score_of_the_acc": -1, "final_rank": 19 }, { "submission_id": "aoj_2249_9130775", "code_snippet": "#include <algorithm>\n#include <cctype>\n#include <climits>\n#include <cstring>\n#include <iomanip>\n#include <iostream>\n#include <map>\n#include <math.h>\n#include <queue>\n#include <set>\n#include <sstream>\n#include <stdio.h>\n#include <string>\n#include <vector>\nusing namespace std;\ntypedef long long ll;\n\nconst int INF = INT_MAX;\n\n//Graph\nstruct edge{int to, distance, cost;};\nclass Graph{\nprivate:\n vector<vector<edge>>neighbors;\n vector<ll>shortest;\n vector<int>prev;\n vector<ll>prev_cost;\n int V;\n int E;\n\npublic:\n Graph(const int n){\n V = n;\n neighbors = vector<vector<edge>>(V);\n shortest = vector<ll>(V, LLONG_MAX);\n prev = vector<int>(V, -1);\n prev_cost = vector<ll>(V, 0);\n };\n\n void add_edge(const int f, const int t, const int d, const int c=INT_MAX){\n edge e; e.to = t; e.distance = d; e.cost = c;\n neighbors[f].push_back(e);\n E++;\n }\n\n //\n //Dijkstra Algorithm\n //\n //\n //execute dijkstra algorithm\n typedef pair<ll, int> P;\n void dijkstra(const int s){\n fill(shortest.begin(), shortest.end(), LLONG_MAX);\n fill(prev.begin(), prev.end(), -1);\n priority_queue<P, vector<P>, greater<P>>pque;\n shortest[s] = 0;\n prev_cost[s] = 0;\n pque.push(P(0, s));\n\n while(!pque.empty()){\n P p = pque.top(); pque.pop();\n if(shortest[p.second] < p.first)continue;\n for(int i = 0; i < neighbors[p.second].size(); i++){\n edge e = neighbors[p.second][i];\n if(shortest[e.to] > shortest[p.second] + e.distance || ((shortest[e.to] == shortest[p.second] + e.distance) && (prev_cost[e.to] > e.cost))){\n shortest[e.to] = shortest[p.second] + e.distance;\n prev[e.to] = p.second;\n prev_cost[e.to] = e.cost;\n pque.push(P(shortest[e.to], e.to));\n }\n }\n } \n }\n\n //Bellman-Ford algorithm\n void Bellman_Ford(const int s){\n fill(shortest.begin(), shortest.end(), LLONG_MAX);\n fill(prev.begin(), prev.end(), -1);\n shortest[s] = 0;\n while(true){\n bool updated = false;\n for(int i = 0; i < V; i++){\n for(int j = 0; j < neighbors[i].size(); j++){\n edge e = neighbors[i][j];\n if(shortest[i] != LLONG_MAX && shortest[i] + e.cost < shortest[e.to]){\n shortest[e.to] = shortest[i] + e.cost;\n prev[e.to] = i;\n updated = true;\n }\n }\n }\n\n if(!updated)break;\n }\n }\n\n bool find_negative_loop(){\n fill(shortest.begin(), shortest.end(), 0);\n for(int i = 0; i < V; i++){\n for(int j = 0; j < V; j++){\n for(int k = 0; k < neighbors[j].size(); k++){\n edge e = neighbors[j][k];\n if(shortest[j] + e.cost < shortest[e.to]){\n shortest[e.to] = shortest[j] + e.cost;\n if(i == V-1)return true;\n }\n }\n }\n }\n return false;\n }\n\n //get the shortest path from s to g\n //make sure it is done after algorithm\n ll get_value(const int g){\n return shortest[g];\n }\n\n ll get_minimum_cost(){\n ll minimum = 0;\n for(int i = 0; i < V; i++)minimum+= prev_cost[i];\n return minimum;\n }\n\n //get the shortest path route from s to g\n //make sure it is done after algorithm\n vector<int> get_route(const int s, const int g){\n vector<int>routes;\n if(get_value(g) == LLONG_MAX)return routes;\n\n for(int t = g; prev[t] != -1; t = prev[t])routes.push_back(t);\n routes.push_back(s);\n reverse(routes.begin(), routes.end());\n return routes;\n }\n\n void printshortest(){\n for(int i = 0; i < V; i++)printf(\"%lld \", shortest[i]);\n printf(\"\\n\");\n }\n};\n\n\n\n\nint main(){\n while(true){\n int N, M; scanf(\"%d%d\", &N, &M);\n if(N == 0 && M == 0)break;\n Graph G(N);\n\n for(int m = 0; m < M; m++){\n int u, v, d, c; scanf(\"%d%d%d%d\", &u, &v, &d, &c);\n G.add_edge(u-1, v-1, d, c);\n G.add_edge(v-1, u-1, d, c);\n }\n G.dijkstra(0);\n printf(\"%lld\\n\", G.get_minimum_cost());\n }\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 4644, "score_of_the_acc": -0.3513, "final_rank": 6 }, { "submission_id": "aoj_2249_9095479", "code_snippet": "#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"Ofast\")\n#pragma GCC optimize(\"unroll-loops\")\n#pragma GCC target(\"avx,avx2\")\n#include <bits/stdc++.h>\n#define INF 1000000001LL\n#define LNF 1000000000000000001LL\n#define MOD 1000000007LL\n#define MAX 3001\n#define long long long\n#define all(x) x.begin(),x.end()\nusing namespace std;\n\nvoid dijkstra(int x, vector<pair<int,pair<long,long>>> graph[], vector<long> & vis)\n{\n for(int i = 0; i<vis.size(); i++)\n vis[i] = LNF;\n\n priority_queue<pair<long,int>, vector<pair<long,int>>, greater<pair<long,int>>> pq;\n\n pq.push({0,x});\n vis[x] = 0;\n\n while(!pq.empty())\n {\n int x = pq.top().second;\n long d = pq.top().first;\n pq.pop();\n\n if(vis[x] < d)\n continue;\n \n for(int i = 0; i<graph[x].size(); i++)\n {\n int y = graph[x][i].first;\n int v = graph[x][i].second.first;\n if(d+v < vis[y])\n {\n vis[y] = d+v;\n pq.push({vis[y],y});\n }\n }\n }\n}\nclass UnionFind\n{\npublic:\n vector<int> parent;\n vector<int> cnt;\n void init(int size)\n {\n parent.resize(size);\n cnt.resize(size);\n for(int i = 0; i<size; i++)\n {\n parent[i] = i;\n cnt[i] = 1;\n }\n }\n int find(int x)\n {\n if(parent[x] == x)\n return x;\n return parent[x] = find(parent[x]);\n }\n void merge(int x, int y)\n {\n x = find(x);\n y = find(y);\n if(x == y)\n return;\n if(x < y)\n {\n parent[y] = x;\n cnt[x]+=cnt[y];\n }\n else\n {\n parent[x] = y;\n cnt[y]+=cnt[x];\n }\n }\n bool isUnion(int x, int y)\n {\n x = find(x);\n y = find(y);\n if(x == y)\n return true;\n return false;\n }\n};\nint main()\n{\n\tios_base::sync_with_stdio(0); \n cin.tie(0);\n \n while(true)\n {\n int n,m;\n cin >> n >> m;\n if(n == 0)\n break;\n \n vector<pair<int,pair<long,long>>> graph[n+1];\n\n for(int i = 0; i<m; i++)\n {\n int a,b,c,d;\n cin >> a >> b >> c >> d;\n graph[a].push_back({b,{c,d}});\n graph[b].push_back({a,{c,d}});\n }\n vector<long> vis(n+1);\n long res = 0;\n dijkstra(1,graph,vis);\n for(int i = 2; i<=n; i++)\n {\n long mn = LNF;\n for(int j = 0; j<graph[i].size(); j++)\n {\n if(vis[i]-graph[i][j].second.first == vis[graph[i][j].first])\n mn = min(mn,graph[i][j].second.second);\n }\n res+=mn;\n }\n cout << res << \"\\n\";\n }\n return 0;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 5348, "score_of_the_acc": -0.5498, "final_rank": 12 }, { "submission_id": "aoj_2249_8994577", "code_snippet": "#line 2 \"cp-library/src/cp-template.hpp\"\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nusing i32 = int;\nusing u32 = unsigned int;\nusing i64 = long long;\nusing u64 = unsigned long long;\nusing i128 = __int128_t;\ntemplate < class T > bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }\ntemplate < class T > bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }\ntemplate < class T, class U > T ceil (T x, U y) { return (x > 0 ? (x + y - 1) / y : x / y); }\ntemplate < class T, class U > T floor(T x, U y) { return (x > 0 ? x / y : (x - y + 1) / y); }\nint popcnt(i32 x) { return __builtin_popcount(x); }\nint popcnt(u32 x) { return __builtin_popcount(x); }\nint popcnt(i64 x) { return __builtin_popcountll(x); }\nint popcnt(u64 x) { return __builtin_popcountll(x); }\n\n#line 2 \"cp-library/src/utility/rep_itr.hpp\"\ntemplate < class T > struct itr_rep {\n T i, d;\n constexpr itr_rep(const T i) noexcept : i(i), d(1) {}\n constexpr itr_rep(const T i, const T d) noexcept : i(i), d(d) {}\n void operator++() noexcept { i += d; }\n constexpr int operator*() const noexcept { return i; }\n constexpr bool operator!=(const itr_rep x) const noexcept { return d > 0 ? i < x.i : i > x.i; }\n};\n\ntemplate < class T > struct rep {\n const itr_rep< T > s, t;\n constexpr rep(const T t) noexcept : s(0), t(t) {}\n constexpr rep(const T s, const T t) noexcept : s(s), t(t) {}\n constexpr rep(const T s, const T t, const T d) noexcept : s(s, d), t(t, d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n\ntemplate < class T > struct revrep {\n const itr_rep < T > s, t;\n constexpr revrep(const T t) noexcept : s(t - 1, -1), t(-1, -1) {}\n constexpr revrep(const T s, const T t) noexcept : s(t - 1, -1), t(s - 1, -1) {}\n constexpr revrep(const T s, const T t, const T d) noexcept : s(t - 1, -d), t(s - 1, -d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n#line 3 \"cp-library/src/utility/io.hpp\"\n\n/* 128bit integer */\nistream& operator>>(istream& is, i128& x) {\n std::string s; is >> s;\n int pm = (s[0] == '-');\n x = 0;\n for(int i : rep(pm, int(s.size()))) x = x * 10 + (s[i] - '0');\n if(pm) x *= -1;\n return is;\n}\nostream& operator<<(ostream& os, const i128& x) {\n if(x == 0) return os << '0';\n i128 y = x;\n if(y < 0) { os << '-'; y *= -1; }\n std::vector<int> ny;\n while(y > 0) { ny.push_back(y % 10); y /= 10; }\n for(int i : revrep(ny.size())) os << ny[i];\n return os;\n}\n\ntemplate < class S, class T > istream& operator>>(istream& is, std::pair< S, T >& x) { is >> x.first >> x.second; return is; }\ntemplate < class S, class T > ostream& operator<<(ostream& os, const std::pair< S, T >& x) { os << x.first << \" \" << x.second; return os; }\n\nnamespace scanner {\n struct sca {\n template < class T > operator T() {\n T s; std::cin >> s; return s;\n }\n };\n struct vec {\n int n;\n vec(int n) : n(n) {}\n template < class T > operator std::vector< T >() {\n std::vector< T > v(n);\n for(T& x : v) std::cin >> x;\n return v;\n }\n };\n struct mat {\n int h, w;\n mat(int h, int w) : h(h), w(w) {}\n template < class T > operator std::vector< std::vector< T > >() {\n std::vector m(h, std::vector< T >(w));\n for(std::vector< T >& v : m) for(T& x : v) std::cin >> x;\n return m;\n }\n };\n struct speedup {\n speedup() {\n std::cin.tie(0);\n std::ios::sync_with_stdio(0);\n }\n } speedup_instance;\n}\nscanner::sca in() { return scanner::sca(); }\nscanner::vec in(int n) { return scanner::vec(n); }\nscanner::mat in(int h, int w) { return scanner::mat(h, w); }\n\nnamespace printer {\n void precision(int d) { std::cout << std::fixed << std::setprecision(d); }\n void flush() { std::cout.flush(); }\n}\n\ntemplate < class T >\nostream& operator<<(ostream& os, const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) { os << a[i]; if(i != n - 1) os << ' '; }\n return os;\n}\n\nint print() { std::cout << '\\n'; return 0; }\ntemplate < class head, class... tail > int print(head&& h, tail&&... t) {\n std::cout << h; if(sizeof...(tail)) std::cout << ' ';\n return print(std::forward<tail>(t)...);\n}\ntemplate < class T > int print_n(const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) std::cout << a[i] << \"\\n\";\n return 0;\n}\n\n\n#line 2 \"cp-library/src/utility/key_val.hpp\"\n\ntemplate < class K, class V >\nstruct key_val {\n K key; V val;\n key_val() {}\n key_val(K key, V val) : key(key), val(val) {}\n template < std::size_t Index >\n std::tuple_element_t< Index, key_val >& get() {\n if constexpr (Index == 0) return key;\n if constexpr (Index == 1) return val;\n }\n};\n\nnamespace std {\n\ntemplate < class K, class V > struct tuple_size < key_val< K, V > > : integral_constant< size_t, 2 > {};\ntemplate < class K, class V > struct tuple_element < 0, key_val< K, V > > { using type = K; };\ntemplate < class K, class V > struct tuple_element < 1, key_val< K, V > > { using type = V; };\n\n}\n#line 2 \"cp-library/src/utility/vec_op.hpp\"\ntemplate < class T > key_val< int, T > max_of(const vector< T >& a) {\n int i = std::max_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class T > key_val< int, T > min_of(const vector< T >& a) {\n int i = std::min_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class S, class T > S sum_of(const vector< T >& a) {\n S sum = 0;\n for(const T x : a) sum += x;\n return sum;\n}\ntemplate < class S, class T > vector< S > freq_of(const vector< T >& a, T L, T R) {\n vector< S > res(R - L, S(0));\n for(const T x : a) res[x - L] += 1;\n return res;\n}\ntemplate < class S, class T > struct prefix_sum {\n vector< S > s;\n prefix_sum(const vector< T >& a) : s(a) {\n s.insert(s.begin(), S(0));\n for(int i : rep(a.size())) s[i + 1] += s[i];\n }\n // [L, R)\n S sum(int L, int R) { return s[R] - s[L]; }\n};\n#line 3 \"cp-library/src/utility/heap.hpp\"\n\ntemplate < class T > using heap_min = std::priority_queue< T, std::vector< T >, std::greater< T > >;\ntemplate < class T > using heap_max = std::priority_queue< T, std::vector< T >, std::less< T > >;\n\n#line 27 \"cp-library/src/cp-template.hpp\"\n\n#line 1 \"cp-library/src/algorithm/bin_search.hpp\"\ntemplate < class T, class F >\nT bin_search(T ok, T ng, F f) {\n while(abs(ng - ok) > 1) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n\ntemplate < class T, class F >\nT bin_search_real(T ok, T ng, F f, int step = 80) {\n while(step--) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n#line 2 \"cp-library/src/algorithm/argsort.hpp\"\n\ntemplate < class T > std::vector< int > argsort(const std::vector< T > &a) {\n std::vector< int > ids((int)a.size());\n std::iota(ids.begin(), ids.end(), 0);\n std::sort(ids.begin(), ids.end(), [&](int i, int j) {\n return a[i] < a[j] || (a[i] == a[j] && i < j);\n });\n return ids;\n}\n#line 1 \"macro.hpp\"\nnamespace macro {\n\nusing size_type = int;\ntemplate < class container > void sort(container& a) { std::sort(std:: begin(a), std:: end(a)); }\ntemplate < class container > void rsort(container& a) { std::sort(std::rbegin(a), std::rend(a)); }\ntemplate < class container > void reverse(container& a) { std::reverse(std::begin(a), std::end(a)); }\ntemplate < class container > void unique(container& a) {\n std::sort(std::begin(a), std::end(a));\n a.erase(std::unique(std::begin(a), std::end(a)), std::end(a));\n}\ntemplate < class container > container sorted(const container& a) { container b = a; sort(b); return std::move(b); }\ntemplate < class container > container rsorted(const container& a) { container b = a; rsort(b); return std::move(b); }\ntemplate < class container, class compare > void sort(container& a, const compare& cmp) { std::sort(std::begin(a), std::end(a), cmp); }\ntemplate < class container, class compare > container sorted(const container& a, const compare& cmp) { container b = a; sort(b, cmp); return std::move(b); }\ntemplate < class container, class value > size_type lower_bound(const container& a, const value& x) { return std::lower_bound(std::begin(a), std::end(a), x) - std::begin(a); }\ntemplate < class container, class value > size_type upper_bound(const container& a, const value& x) { return std::upper_bound(std::begin(a), std::end(a), x) - std::begin(a); }\n\nconst std::vector<std::pair<size_type, size_type>> dir4 = { {+1, 0}, {-1, 0}, { 0, +1}, { 0, -1} };\nconst std::vector<std::pair<size_type, size_type>> dir8 = { {-1, -1}, {-1, 0}, {-1, +1}, { 0, -1}, { 0, +1}, {+1, -1}, {+1, 0}, {+1, +1} };\n\n#ifdef _DEBUG\n#define debug(x) std::cout << \"[\" << __LINE__ << \"] \" << #x << \": \" << x << std::endl\n#else\n#define debug(x)\n#endif\n\ntemplate < class container > void concat(container& a, const container& b) {\n a.insert(std::end(a), std::begin(b), std::end(b));\n}\nstd::vector<size_type> iota(const size_type n) {\n std::vector<size_type> I(n);\n std::iota(std::begin(I), std::end(I), 0);\n return I;\n}\ntemplate < class container > std::vector<size_type> sort_idx(const container& a) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return a[i] < a[j] or (a[i] == a[j] and i < j); });\n return I;\n}\ntemplate < class container, class compare > std::vector<size_type> sort_idx(const container& a, const compare& cmp) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return cmp(a[i], a[j]) or (a[i] == a[j] and i < j); });\n return std::move(I);\n}\n\nstruct grid {\n using size_type = int;\n size_type H, W;\n grid(const size_type H, const size_type W) : H(H), W(W) {}\n bool contains(const size_type i, const size_type j) {\n return 0 <= i and i < H and 0 <= j and j < W;\n }\n};\n\n} // namespace macro\n\nusing namespace macro;\n#line 1 \"cp-library/src/graph/shortest_path.hpp\"\n// g <- pair < v , cost > \ntemplate < class T >\nvector< T > dijkstra(vector<vector<pair<int, T>>> &graph, int s) {\n T INF = numeric_limits< T >::max();\n vector<T> dist(graph.size(), INF);\n priority_queue<pair<T,int>, vector<pair<T,int>>, greater<pair<T,int>>> q;\n q.push({dist[s] = T(0), s});\n while(!q.empty()){\n auto [uc, ui] = q.top(); q.pop();\n if(uc != dist[ui]) continue;\n for(auto [vi, vc] : graph[ui]) if(dist[vi] > uc + vc) \n q.push({dist[vi] = uc + vc, vi});\n }\n return dist;\n}\n\n// g <- pair < v , cost > \ntemplate < class T >\nvector< T > dijkstra(vector<vector<pair<int, T>>> &graph, vector<int> &starts) {\n T INF = numeric_limits< T >::max();\n vector<T> dist(graph.size(), INF);\n priority_queue<pair<T,int>, vector<pair<T,int>>, greater<pair<T,int>>> q;\n for(int s : starts) q.push({dist[s] = T(0), s});\n while(!q.empty()){\n auto [uc, ui] = q.top(); q.pop();\n if(uc != dist[ui]) continue;\n for(auto [vi, vc] : graph[ui]) if(dist[vi] > uc + vc) \n q.push({dist[vi] = uc + vc, vi});\n }\n return dist;\n}\n\n// g <- pair < v , cost > \ntemplate < class T >\npair< T, vector<int> > shortest_path(vector<vector<pair<int, T>>> &graph, int s, int t) {\n T INF = numeric_limits< T >::max();\n vector<T> dist(graph.size(), INF);\n vector<int> prev(graph.size(), -1);\n priority_queue<pair<T,int>, vector<pair<T,int>>, greater<pair<T,int>>> q;\n q.push({dist[s] = T(0), s});\n while(!q.empty()){\n auto [uc, ui] = q.top(); q.pop();\n if(uc != dist[ui]) continue;\n for(auto [vi, vc] : graph[ui]) if(dist[vi] > uc + vc) \n q.push({dist[vi] = uc + vc, vi}), prev[vi] = ui;\n }\n\n vector<int> path;\n if(dist[t] != INF) {\n for(int v = t; v != -1; v = prev[v]) path.push_back(v);\n reverse(path.begin(), path.end());\n }\n return {dist[t], path};\n}\n#line 4 \"A.cpp\"\n\nint solve(int N, int M) {\n vector<vector<pair<int,int>>> G(N);\n vector<vector<tuple<int,int,int>>> H(N);\n vector<tuple<int,int,int,int>> E(M);\n for(auto &[u, v, d, c] : E) {\n u = in(), v = in(), d = in(), c = in(); u--, v--;\n G[u].push_back({v, d});\n G[v].push_back({u, d});\n H[u].push_back({v, d, c});\n H[v].push_back({u, d, c});\n }\n\n vector<int> dist = dijkstra(G, 0);\n int ans = 0;\n for(int v : rep(N)) if(v != 0) {\n int cost = 1e9;\n for(auto [u, d, c] : H[v]) {\n if(dist[u] + d == dist[v]) {\n chmin(cost, c);\n }\n }\n ans += cost;\n }\n return ans;\n}\n\nint main() {\n while(true) {\n int N = in(), M = in();\n if(make_pair(N, M) == make_pair(0, 0)) return 0;\n print(solve(N, M));\n }\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 5444, "score_of_the_acc": -0.5798, "final_rank": 14 }, { "submission_id": "aoj_2249_8951639", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nconst int maxn = 1e4 + 5;\ntypedef pair<int, int> P;\nint n, m;\nstruct edge\n{\n int to, cost, w;\n};\nvector<edge> G[maxn];\nint d[maxn], w[maxn];\n\nvoid solve()\n{\n memset(d, 0x3f, sizeof d);\n priority_queue<P, vector<P>, greater<P>> q;\n d[1] = 0;\n q.push({0, 1});\n while (!q.empty())\n {\n P p = q.top();\n q.pop();\n int v = p.second;\n if (d[v] < p.first)continue;\n for (int i = 0; i < G[v].size(); i++)\n {\n edge e = G[v][i];\n if (d[e.to] > d[v] + e.cost)\n {\n d[e.to] = d[v] + e.cost;\n w[e.to] = e.w;\n q.push({d[e.to], e.to});\n }\n else if (d[e.to] == d[v] + e.cost)\n {\n w[e.to] = min(w[e.to], e.w);\n }\n }\n }\n}\n\nint main()\n{\n ios::sync_with_stdio(0);\n cin.tie(0);\n while (cin >> n >> m && n+m)\n {\n for (int i = 1; i <= n; i++)G[i].clear();\n for (int i = 1; i <= m; i++)\n {\n int u, v, c, d;\n cin >> u >> v >> d >> c;\n G[u].push_back({v, d, c});\n G[v].push_back({u, d, c});\n }\n solve();\n int ans = 0;\n for (int i = 2; i <= n; i++)ans += w[i];\n cout << ans << \"\\n\";\n }\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 5416, "score_of_the_acc": -0.566, "final_rank": 13 } ]
aoj_2256_cpp
ケーキ分割問題 English text is not available in this practice contest. イコとピコはACM博士によって開発された人工知能を搭載した双子のロボットである.今日は二人の誕生日なので,博士は二人のためにたくさんのイチゴがのったケーキを用意した. ケーキは横幅 W ×奥行き H の長方形の形をしている. 便宜上,ケーキは上から見たとき(0,0),( W ,0),( W , H ),(0, H )を頂点とする長方形になるように置かれているとする. また,二人に均等になるようにちょうど2 N 個のイチゴが乗っている. せっかくなので,博士は二人にケーキを切らせることにした. イコとピコは協力してケーキを切るため,イコが包丁の一端を辺(0,0)-(0, H )の間におき,ピコはもう一端を辺( W ,0)-( W , H )の間において同時に下に下ろすという方法をとることにした. もちろん包丁は直線状なので,選んだ2点を通る直線でケーキは2つに分かれる.(図E-1) 図E-1 博士は人工知能を完成させたことに満足し他の部分を適当に作ったために,二人の腕のパーツは不安定になってしまった. そのため,辺(0,0)-(0, H )(または辺( W ,0)-( W , H ))上の狙った場所に置こうとしてもどうしてもずれてしまうのである. 二人はイチゴが好きなので,できれば N 個ずつに分けたいと考えている. そこで, N 個ずつになるように切れない場合は2点を選びなおすことにした. 2 N 個のイチゴの配置によっては半分ずつに分けられなかったり,分けられたとしても,とても確率が低い場合も考えられる. このような場合に何度も2点を選びなおすのは不毛なので,まず初めに N 個ずつに分かれるように切れる確率を計算することにした. 二人の腕のパーツは不安定なので,動きを予想するのは難しい. とりあえず,二人は「包丁を置く点は辺(0,0)-(0, H )(または辺( W ,0)-( W , H ))上に必ず乗り,置かれる確率はどの二点も等しい」という仮定を置くことにした. また,イチゴの大きさを考慮するのも面倒なので,点とみなすことにした. これは人工知能の性能を試すいい機会である. 博士と同じ研究所に勤めているあなたに,2 N 個のイチゴの位置を与えられたときに, 2人と同じ仮定を用いてイチゴが N 個ずつに分かれる確率を計算するプログラムを作成する仕事が割り当てられた. Input 入力は複数のデータセットからなる.入力の終わりは空白で区切られた3つのゼロからなる行によって与えられる.各データセットは1つのケーキに関する情報を表し,その形式は以下の通りである. W H N x 1 y 1 ... x 2N y 2N データセットの最初の行は3つの整数 W , H , N からなり,それぞれケーキの横幅,奥行き,イチゴの個数/2を表す. 続く2 N 行は2個の整数からなり,それぞれイチゴのx座標とy座標を表す. それぞれの値は次の制約を満たしている. 1 ≤ W , H ≤ 1,000 1 ≤ N ≤ 100 0 ≤ x i ≤ W 0 ≤ y i ≤ H また,2つの異なるイチゴが同じ座標にあることはない. Output それぞれのデータセットに対して, 2人の仮定を用いた場合にイチゴが2等分される確率を表す実数を1行に出力せよ. 答えには 10 -8 を越える誤差があってはいけない.それ以外の余計な文字を出力してはならない. Sample Input 2 2 1 0 1 2 1 3 3 1 1 1 2 1 0 0 0 Output for the Sample Input 0.5000000000 0.1666666667
[ { "submission_id": "aoj_2256_10848200", "code_snippet": "#include <bits/stdc++.h>\n#define ll long long\n#define endl '\\n'\n#define ls rt << 1\n#define rs rt << 1 | 1\n#define maxn 200010\nusing namespace std;\n\nconst double eps = 1e-12;\nconst double pi = acos(-1.0);\nint sgn(double x)\n{\n if (fabs(x) < eps)\n return 0;\n if (x < 0)\n return -1;\n else\n return 1;\n}\n\nll n;\ndouble w, h;\npair<double, double> a[maxn];\nvector<double> check;\ndouble ans[maxn];\nint main()\n{\n std::ios::sync_with_stdio(false);\n std::cin.tie(0);\n std::cout.tie(0);\n#ifndef ONLINE_JUDGE\n //freopen(\"input.txt\", \"r\", stdin);\n //freopen(\"standardout.txt\", \"w\", stdout);\n ll _begin_time = clock();\n#endif\n while (cin >> w >> h >> n && w + h + n)\n {\n for (int i = 0; i < n * 2; i++)\n cin >> a[i].first >> a[i].second;\n check.clear();\n check.push_back(0);\n check.push_back(h);\n a[n * 2] = {w, 0};\n a[n * 2 + 1] = {w, h};\n for (int i = 0; i < n * 2 + 2; i++)\n for (int j = i + 1; j < n * 2 + 2; j++)\n if (sgn(a[i].first - a[j].first))\n {\n double tmp = a[i].second - a[i].first * (a[j].second - a[i].second) / (a[j].first - a[i].first);\n if (tmp > 0 && tmp < h)\n check.push_back(tmp);\n }\n for (int i = 0; i < n * 2; i++)\n if (!sgn(a[i].first))\n {\n if (a[i].second + eps * 2 < h)\n check.push_back(a[i].second + eps * 2);\n if (a[i].second - eps * 2 > 0)\n check.push_back(a[i].second - eps * 2);\n }\n sort(check.begin(), check.end());\n for (int i = 0; i < check.size(); i++)\n {\n vector<double> tmp;\n ll flag = 0;\n for (int j = 0; j < n * 2; j++)\n {\n if (!sgn(a[j].first))\n {\n ll ptr = sgn(a[j].second - check[i]);\n if (ptr == 1)\n tmp.push_back(h);\n else if (ptr == -1)\n tmp.push_back(0);\n else\n flag = 1;\n }\n else\n {\n double ptr = (a[j].second - check[i]) / a[j].first * w + check[i];\n if (ptr > h)\n ptr = h;\n if (ptr < 0)\n ptr = 0;\n tmp.push_back(ptr);\n }\n }\n if (flag)\n ans[i] = 0;\n else\n {\n sort(tmp.begin(), tmp.end());\n ans[i] = tmp[n] - tmp[n - 1];\n }\n }\n double res = 0;\n for (int i = 0; i < check.size() - 1; i++)\n res += (check[i + 1] - check[i]) * (ans[i + 1] + ans[i]) / 2;\n cout << fixed << setprecision(10) << res / h / h << endl;\n }\n#ifndef ONLINE_JUDGE\n ll _end_time = clock();\n //printf(\"time = %ld ms\\n\", _end_time - _begin_time);\n#endif\n return 0;\n}", "accuracy": 1, "time_ms": 290, "memory_kb": 3752, "score_of_the_acc": -0.467, "final_rank": 8 }, { "submission_id": "aoj_2256_10617769", "code_snippet": "// (⁠◕⁠ᴗ⁠◕⁠✿⁠)\n\n// #pragma GCC target(\"avx2\")\n// #pragma GCC optimize(\"O3\")\n// #pragma GCC optimize(\"unroll-loops\")\n#include <bits/stdc++.h>\n#define rep(i, n) for (ll i = 0; i < (n); i++)\n#define srep(i, s, n) for (ll i = s; i < (n); i++)\n#define len(x) ((int)(x).size())\n#define all(x) (x).begin(), (x).end()\nusing namespace std;\ntemplate<typename T> using vc = vector<T>;\ntemplate<typename T> using vv = vc<vc<T>>;\ntemplate<typename T> using vvv = vv<vc<T>>;\nusing vi = vc<int>;using vvi = vv<int>; using vvvi = vv<vi>;\nusing ll = long long;using vl = vc<ll>;using vvl = vv<ll>; using vvvl = vv<vl>;\nusing ld = long double; using vld = vc<ld>; using vvld = vc<vld>; using vvvld = vc<vvld>;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nconst ld pi = acos(-1.0);\nconst int inf = 0x3f3f3f3f;\nconst ll INF = 0x3f3f3f3f3f3f3f3f;\n// const ll mod = 1000000007;\nconst ll mod = 998244353;\ninline bool inside(ll y, ll x, ll H, ll W) {return 0 <= (y) and (y) < (H) and 0 <= (x) and (x) < (W); }\n\n#define debug(var) do { cout << #var << \" :\\n\"; view(var); } while(0)\ntemplate<typename T>void view(const T& e) {cout << e;}\ntemplate<typename T1, typename T2>void view(const pair<T1, T2>& p) {cout << \"{\" << p.first << \", \" << p.second << \"}\";}\ntemplate<typename T>void view(const vc<T>& v) {for (const auto& e : v) {view(e);cout << \" \";} cout << endl;}\ntemplate<typename T>void view(const vv<T>& vv) {for (const auto& v : vv) {view(v);cout << \" \";} cout << endl;}\ntemplate<typename T>void view(const set<T>& s) {for (const auto& e : s) {view(e);cout << \" \";} cout << endl;}\ntemplate<typename T>void view(const unordered_set<T>& s) {for (const auto& e : s) {view(e);cout << \" \";} cout << endl;}\ntemplate<typename T1, typename T2>void view(const map<T1, T2>& mp){for (const auto& e : mp) {view(e);cout << \" \";} cout << endl;}\n\nint W, H, N;\n\nvoid solve(){\n vi X(2 * N + 2), Y(2 * N + 2); rep(i, 2 * N) cin >> X[i] >> Y[i];\n X[2 * N] = W;\n Y[2 * N] = 0;\n X[2 * N + 1] = W;\n Y[2 * N + 1] = H;\n vc<ld> event;\n event.push_back(0);\n event.push_back(H);\n rep(j, 2 * N + 2) rep(i, j){\n if (X[i] != X[j]){\n ld p = -(Y[i] - Y[j]) * X[i] / (ld)(X[i] - X[j]) + Y[i];\n if (0 < p && p < H) event.push_back(p);\n }\n }\n sort(all(event));\n\n auto divide_point = [&](ld ny) -> pair<ld, ld>{\n vc<pair<ld, int>> A;\n rep(i, 2 * N) A.push_back({(Y[i] - ny) / (ld)(X[i]), i});\n sort(all(A));\n return {(Y[A[N - 1].second] - ny) * W / (ld)X[A[N - 1].second] + ny, (Y[A[N].second] - ny) * W / (ld)X[A[N].second] + ny};\n };\n ld ans = 0;\n rep(i, len(event) - 1){\n ld l0 = event[i], r0 = event[i + 1];\n auto [l1, r1] = divide_point((l0 + r0) / (ld)2);\n l1 = min((ld)H, max((ld)0, l1));\n r1 = min((ld)H, max((ld)0, r1));\n ans += (r1 - l1) * (r0 - l0);\n }\n ans /= ((ld)H * H);\n cout << fixed << setprecision(16) << ans << endl;\n}\n\nint main(){\n while (true){\n cin >> W >> H >> N;\n if (N == 0) break;\n solve();\n }\n}", "accuracy": 1, "time_ms": 1760, "memory_kb": 4072, "score_of_the_acc": -0.9952, "final_rank": 14 }, { "submission_id": "aoj_2256_10418385", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll=long long;\nconst ll ILL=2167167167167167167;\nconst int INF=2100000000;\n#define rep(i,a,b) for (int i=(int)(a);i<(int)(b);i++)\n#define all(p) p.begin(),p.end()\ntemplate<class T> using _pq = priority_queue<T, vector<T>, greater<T>>;\ntemplate<class T> int LB(vector<T> &v,T a){return lower_bound(v.begin(),v.end(),a)-v.begin();}\ntemplate<class T> int UB(vector<T> &v,T a){return upper_bound(v.begin(),v.end(),a)-v.begin();}\ntemplate<class T> bool chmin(T &a,T b){if(b<a){a=b;return 1;}else return 0;}\ntemplate<class T> bool chmax(T &a,T b){if(a<b){a=b;return 1;}else return 0;}\ntemplate<class T> void So(vector<T> &v) {sort(v.begin(),v.end());}\ntemplate<class T> void Sore(vector<T> &v) {sort(v.begin(),v.end(),[](T x,T y){return x>y;});}\nbool yneos(bool a,bool upp=false){if(a){cout<<(upp?\"YES\\n\":\"Yes\\n\");}else{cout<<(upp?\"NO\\n\":\"No\\n\");}return a;}\ntemplate<class T> void vec_out(vector<T> &p,int ty=0){\n if(ty==2){cout<<'{';for(int i=0;i<(int)p.size();i++){if(i){cout<<\",\";}cout<<'\"'<<p[i]<<'\"';}cout<<\"}\\n\";}\n else{if(ty==1){cout<<p.size()<<\"\\n\";}for(int i=0;i<(int)(p.size());i++){if(i) cout<<\" \";cout<<p[i];}cout<<\"\\n\";}}\ntemplate<class T> T vec_min(vector<T> &a){assert(!a.empty());T ans=a[0];for(auto &x:a) chmin(ans,x);return ans;}\ntemplate<class T> T vec_max(vector<T> &a){assert(!a.empty());T ans=a[0];for(auto &x:a) chmax(ans,x);return ans;}\ntemplate<class T> T vec_sum(vector<T> &a){T ans=T(0);for(auto &x:a) ans+=x;return ans;}\nint pop_count(long long a){int res=0;while(a){res+=(a&1),a>>=1;}return res;}\ntemplate<class T> T square(T a){return a * a;}\n// https://github.com/kth-competitive-programming/kactl/tree/main/content/geometry\ntemplate<class T> ll sgn(T x) { return (x > 0) - (x < 0); }\ntemplate<class T> struct Point {\n using P = Point;\n T x, y;\n explicit Point(T x = 0, T y = 0) : x(x), y(y) {}\n bool operator<(P p) const { return tie(x, y) < tie(p.x, p.y); }\n bool operator==(P p) const { return tie(x, y) == tie(p.x, p.y); }\n P operator+(P p) const { return P(x + p.x, y + p.y); }\n P operator-(P p) const { return P(x - p.x, y - p.y); }\n P operator*(T d) const { return P(x * d, y * d); }\n P operator/(T d) const { return P(x / d, y / d); }\n T dot(P p) const { return x * p.x + y * p.y; }\n T cross(P p) const { return x * p.y - y * p.x; }\n T cross(P a, P b) const { return (a - *this).cross(b - *this); }\n T dist2() const { return x * x + y * y; }\n long double dist() const { return sqrtl(dist2()); }\n // angle to x-axis in interval [-pi, pi]\n double angle() const { return atan2(y, x); }\n P unit() const { return *this / dist(); } // makes dist()=1\n P perp() const { return P(-y, x); } // rotates +90 degrees\n P normal() const { return perp().unit(); }\n // returns point rotated 'a' radians ccw around the origin\n P rotate(double a) const {\n return P(x * cos(a) - y * sin(a), x * sin(a) + y * cos(a));\n }\n friend ostream& operator<<(ostream& os, P p) {\n return os << \"(\" << p.x << \",\" << p.y << \")\";\n }\n};\n\n/* If a unique intersection point of the lines going through s1,e1 and s2,e2 exists \\{1, point\\} is returned.\nIf no intersection point exists \\{0, (0,0)\\} is returned and if infinitely many exists \\{-1, (0,0)\\} is returned.\nThe wrong position will be returned if P is Point<ll> and the intersection point does not have integer coordinates.\nProducts of three coordinates are used in intermediate steps so watch out for overflow if using ll or ll.\n * \tauto res = lineInter(s1,e1,s2,e2);\n * \tif (res.first == 1)\n * \t\tcout << \"intersection point at \" << res.second << endl; */\n\ntemplate<class P>\npair<ll, P> lineInter(P s1, P e1, P s2, P e2) {\n auto d = (e1 - s1).cross(e2 - s2);\n if(d == 0) // if parallel\n return {-(s1.cross(e1, s2) == 0), P(0, 0)};\n auto p = s2.cross(e1, e2), q = s2.cross(e2, s1);\n return {1, (s1 * p + e1 * q) / d};\n}\n\nint W, H, N;\n\n\nvoid solve();\n// CITRUS CURIO CITY / FREDERIC\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n\n while (cin >> W >> H >> N, W) solve();\n}\n\nvoid solve(){\n using ld = long double;\n using P = Point<ld>;\n N *= 2;\n vector<P> pts(N);\n rep(i, 0, N) cin >> pts[i].x >> pts[i].y;\n set<ld> s;\n s.insert(0);\n s.insert(H);\n rep(i, 0, N) rep(j, 0, i){\n auto tmp = lineInter(pts[i], pts[j], P{0, 0}, P{0, (ld)H});\n if (tmp.first == 1 && 0 <= tmp.second.y && tmp.second.y <= H){\n s.insert(tmp.second.y);\n }\n }\n rep(i, 0, N){\n auto tmp = lineInter(pts[i], P{(ld)W, 0}, P{0, 0}, P{0, (ld)H});\n if (tmp.first == 1 && 0 <= tmp.second.y && tmp.second.y <= H){\n s.insert(tmp.second.y);\n }\n tmp = lineInter(pts[i], P{(ld)W, (ld)(H)}, P{0, 0}, P{0, (ld)H});\n if (tmp.first == 1 && 0 <= tmp.second.y && tmp.second.y <= H){\n s.insert(tmp.second.y);\n }\n }\n vector<ld> Y;\n for (auto x : s) Y.push_back(x);\n ld ans = 0;\n rep(i, 1, Y.size()){\n // cout << i << endl;\n vector<ld> ys;\n ld y = (Y[i - 1] + Y[i]) / 2;\n rep(j, 0, N){\n if (pts[j].x == 0){\n if (pts[j].y < y) ys.push_back(0);\n else ys.push_back(H);\n continue;\n }\n auto tmp = lineInter(pts[j], P{0, y}, P{(ld)W, 0}, P{(ld)W, (ld)H});\n assert(tmp.first == 1);\n ys.push_back(tmp.second.y);\n }\n // cout << ys.size() << endl;\n So(ys);\n ld diff = max((ld)(0), min((ld)(H), ys[N / 2]) - max((ld)0, ys[N / 2 - 1]));\n ans += diff * (Y[i] - Y[i - 1]);\n }\n ans /= H * H;\n cout << fixed << setprecision(20) << ans << \"\\n\";\n}", "accuracy": 1, "time_ms": 1170, "memory_kb": 4348, "score_of_the_acc": -1.1482, "final_rank": 16 }, { "submission_id": "aoj_2256_9792769", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n/**\n * @brief template\n */\n\nusing T = double;\nconst T eps = 1e-8;\nusing Point = complex<T>;\nusing Poly = vector<Point>;\n#define X real()\n#define Y imag()\ntemplate <typename T> inline bool eq(const T &a, const T &b) {\n return fabs(a - b) < eps;\n}\nbool cmp(const Point &a, const Point &b) {\n auto sub = [&](Point a) {\n return (a.Y < 0 ? -1 : (a.Y == 0 && a.X >= 0 ? 0 : 1));\n };\n if (sub(a) != sub(b))\n return sub(a) < sub(b);\n return a.Y * b.X < a.X * b.Y;\n}\nstruct Line {\n Point a, b, dir;\n Line() {}\n Line(Point _a, Point _b) : a(_a), b(_b), dir(b - a) {}\n Line(T A, T B, T C) {\n if (eq(A, .0)) {\n a = Point(0, C / B), b = Point(1 / C / B);\n } else if (eq(B, .0)) {\n a = Point(C / A, 0), b = Point(C / A, 1);\n } else {\n a = Point(0, C / B), b = Point(C / A, 0);\n }\n }\n};\nstruct Segment : Line {\n Segment() {}\n Segment(Point _a, Point _b) : Line(_a, _b) {}\n};\nstruct Circle {\n Point p;\n T r;\n Circle() {}\n Circle(Point _p, T _r) : p(_p), r(_r) {}\n};\n\nistream &operator>>(istream &is, Point &p) {\n T x, y;\n is >> x >> y;\n p = Point(x, y);\n return is;\n}\nostream &operator<<(ostream &os, Point &p) {\n os << fixed << setprecision(12) << p.X << ' ' << p.Y;\n return os;\n}\nPoint unit(const Point &a) {\n return a / abs(a);\n}\nT dot(const Point &a, const Point &b) {\n return a.X * b.X + a.Y * b.Y;\n}\nT cross(const Point &a, const Point &b) {\n return a.X * b.Y - a.Y * b.X;\n}\nPoint rot(const Point &a, const T &theta) {\n return Point(cos(theta) * a.X - sin(theta) * a.Y,\n sin(theta) * a.X + cos(theta) * a.Y);\n}\nPoint rot90(const Point &a) {\n return Point(-a.Y, a.X);\n}\nT arg(const Point &a, const Point &b, const Point &c) {\n double ret = acos(dot(a - b, c - b) / abs(a - b) / abs(c - b));\n if (cross(a - b, c - b) < 0)\n ret = -ret;\n return ret;\n}\n\nPoint Projection(const Line &l, const Point &p) {\n T t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n return l.a + (l.a - l.b) * t;\n}\nPoint Projection(const Segment &l, const Point &p) {\n T t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n return l.a + (l.a - l.b) * t;\n}\nPoint Reflection(const Line &l, const Point &p) {\n return p + (Projection(l, p) - p) * 2.;\n}\nint ccw(const Point &a, Point b, Point c) {\n b -= a;\n c -= a;\n if (cross(b, c) > eps)\n return 1; // ccw\n\n if (cross(b, c) < -eps)\n return -1; // cw\n\n if (dot(b, c) < 0)\n return 2; // c,a,b\n\n if (norm(b) < norm(c))\n return -2; // a,b,c\n\n return 0; // a,c,b\n\n}\nbool isOrthogonal(const Line &a, const Line &b) {\n return eq(dot(a.b - a.a, b.b - b.a), .0);\n}\nbool isParallel(const Line &a, const Line &b) {\n return eq(cross(a.b - a.a, b.b - b.a), .0);\n}\nbool isIntersect(const Segment &a, const Segment &b) {\n return ccw(a.a, a.b, b.a) * ccw(a.a, a.b, b.b) <= 0 and\n ccw(b.a, b.b, a.a) * ccw(b.a, b.b, a.b) <= 0;\n}\nint isIntersect(const Circle &a, const Circle &b) {\n T d = abs(a.p - b.p);\n if (d > a.r + b.r + eps)\n return 4;\n if (eq(d, a.r + b.r))\n return 3;\n if (eq(d, abs(a.r - b.r)))\n return 1;\n if (d < abs(a.r - b.r) - eps)\n return 0;\n return 2;\n}\nT Dist(const Line &a, const Point &b) {\n Point c = Projection(a, b);\n return abs(c - b);\n}\nT Dist(const Segment &a, const Point &b) {\n if (dot(a.b - a.a, b - a.a) < eps)\n return abs(b - a.a);\n if (dot(a.a - a.b, b - a.b) < eps)\n return abs(b - a.b);\n return abs(cross(a.b - a.a, b - a.a)) / abs(a.b - a.a);\n}\nT Dist(const Segment &a, const Segment &b) {\n if (isIntersect(a, b))\n return .0;\n T res = min({Dist(a, b.a), Dist(a, b.b), Dist(b, a.a), Dist(b, a.b)});\n return res;\n}\nPoint Intersection(const Line &a, const Line &b) {\n T d1 = cross(a.b - a.a, b.b - b.a);\n T d2 = cross(a.b - a.a, a.b - b.a);\n if (eq(d1, 0.) and eq(d2, 0.))\n return b.a;\n return b.a + (b.b - b.a) * (d2 / d1);\n}\nPoly Intersection(const Circle &a, const Line &b) {\n Poly res;\n T d = Dist(b, a.p);\n if (d > a.r + eps)\n return res;\n Point h = Projection(b, a.p);\n if (eq(d, a.r)) {\n res.push_back(h);\n return res;\n }\n Point e = unit(b.b - b.a);\n T ph = sqrt(a.r * a.r - d * d);\n res.push_back(h - e * ph);\n res.push_back(h + e * ph);\n return res;\n}\nPoly Intersection(const Circle &a, const Segment &b) {\n Line c(b.a, b.b);\n Poly sub = Intersection(a, c);\n double xmi = min(b.a.X, b.b.X), xma = max(b.a.X, b.b.X);\n double ymi = min(b.a.Y, b.b.Y), yma = max(b.a.Y, b.b.Y);\n Poly res;\n rep(i, 0, sub.size()) {\n if (xmi <= sub[i].X + eps and sub[i].X - eps <= xma and\n ymi <= sub[i].Y + eps and sub[i].Y - eps <= yma) {\n res.push_back(sub[i]);\n }\n }\n return res;\n}\nPoly Intersection(const Circle &a, const Circle &b) {\n Poly res;\n int mode = isIntersect(a, b);\n T d = abs(a.p - b.p);\n if (mode == 4 or mode == 0)\n return res;\n if (mode == 3) {\n T t = a.r / (a.r + b.r);\n res.push_back(a.p + (b.p - a.p) * t);\n return res;\n }\n if (mode == 1) {\n if (b.r < a.r - eps) {\n res.push_back(a.p + (b.p - a.p) * (a.r / d));\n } else {\n res.push_back(b.p + (a.p - b.p) * (b.r / d));\n }\n return res;\n }\n T rc = (a.r * a.r + d * d - b.r * b.r) / d / 2.;\n T rs = sqrt(a.r * a.r - rc * rc);\n if (a.r - abs(rc) < eps)\n rs = 0;\n Point e = unit(b.p - a.p);\n res.push_back(a.p + rc * e + rs * e * Point(0, 1));\n res.push_back(a.p + rc * e + rs * e * Point(0, -1));\n return res;\n}\nPoly HalfplaneIntersection(vector<Line> &H) {\n sort(ALL(H), [&](Line &l1, Line &l2) { return cmp(l1.dir, l2.dir); });\n auto outside = [&](Line &L, Point p) -> bool {\n return cross(L.dir, p - L.a) < -eps;\n };\n deque<Line> deq;\n int sz = 0;\n rep(i, 0, SZ(H)) {\n while (sz > 1 and\n outside(H[i], Intersection(deq[sz - 1], deq[sz - 2]))) {\n deq.pop_back();\n sz--;\n }\n while (sz > 1 and outside(H[i], Intersection(deq[0], deq[1]))) {\n deq.pop_front();\n sz--;\n }\n if (sz > 0 and fabs(cross(H[i].dir, deq[sz - 1].dir)) < eps) {\n if (dot(H[i].dir, deq[sz - 1].dir) < 0) {\n return {};\n }\n if (outside(H[i], deq[sz - 1].a)) {\n deq.pop_back();\n sz--;\n } else\n continue;\n }\n deq.push_back(H[i]);\n sz++;\n }\n\n while (sz > 2 and outside(deq[0], Intersection(deq[sz - 1], deq[sz - 2]))) {\n deq.pop_back();\n sz--;\n }\n while (sz > 2 and outside(deq[sz - 1], Intersection(deq[0], deq[1]))) {\n deq.pop_front();\n sz--;\n }\n if (sz < 3)\n return {};\n deq.push_back(deq.front());\n Poly ret;\n rep(i, 0, sz) ret.push_back(Intersection(deq[i], deq[i + 1]));\n return ret;\n}\n\nT Area(const Poly &a) {\n T res = 0;\n int n = a.size();\n rep(i, 0, n) res += cross(a[i], a[(i + 1) % n]);\n return fabs(res / 2.);\n}\nT Area(const Poly &a, const Circle &b) {\n int n = a.size();\n if (n < 3)\n return .0;\n auto rec = [&](auto self, const Circle &c, const Point &p1,\n const Point &p2) {\n Point va = c.p - p1, vb = c.p - p2;\n T f = cross(va, vb), res = .0;\n if (eq(f, .0))\n return res;\n if (max(abs(va), abs(vb)) < c.r + eps)\n return f;\n if (Dist(Segment(p1, p2), c.p) > c.r - eps)\n return c.r * c.r * arg(vb * conj(va));\n auto u = Intersection(c, Segment(p1, p2));\n Poly sub;\n sub.push_back(p1);\n for (auto &x : u)\n sub.push_back(x);\n sub.push_back(p2);\n rep(i, 0, sub.size() - 1) res += self(self, c, sub[i], sub[i + 1]);\n return res;\n };\n T res = .0;\n rep(i, 0, n) res += rec(rec, b, a[i], a[(i + 1) % n]);\n return fabs(res / 2.);\n}\nT Area(const Circle &a, const Circle &b) {\n T d = abs(a.p - b.p);\n if (d >= a.r + b.r - eps)\n return .0;\n if (d <= abs(a.r - b.r) + eps) {\n T r = min(a.r, b.r);\n return M_PI * r * r;\n }\n T ath = acos((a.r * a.r + d * d - b.r * b.r) / d / a.r / 2.);\n T res = a.r * a.r * (ath - sin(ath * 2) / 2.);\n T bth = acos((b.r * b.r + d * d - a.r * a.r) / d / b.r / 2.);\n res += b.r * b.r * (bth - sin(bth * 2) / 2.);\n return fabs(res);\n}\nbool isConvex(const Poly &a) {\n int n = a.size();\n int cur, pre, nxt;\n rep(i, 0, n) {\n pre = (i - 1 + n) % n;\n nxt = (i + 1) % n;\n cur = i;\n if (ccw(a[pre], a[cur], a[nxt]) == -1)\n return 0;\n }\n return 1;\n}\nint isContained(const Poly &a,\n const Point &b) { // 0:not contain,1:on edge,2:contain\n\n bool res = 0;\n int n = a.size();\n rep(i, 0, n) {\n Point p = a[i] - b, q = a[(i + 1) % n] - b;\n if (p.Y > q.Y)\n swap(p, q);\n if (p.Y < eps and eps < q.Y and cross(p, q) > eps)\n res ^= 1;\n if (eq(cross(p, q), .0) and dot(p, q) < eps)\n return 1;\n }\n return (res ? 2 : 0);\n}\nPoly ConvexHull(Poly &a) {\n sort(ALL(a), [](const Point &p, const Point &q) {\n return (eq(p.Y, q.Y) ? p.X < q.X : p.Y < q.Y);\n });\n a.erase(unique(ALL(a)), a.end());\n int n = a.size(), k = 0;\n Poly res(n * 2);\n for (int i = 0; i < n; res[k++] = a[i++]) {\n while (k >= 2 and\n cross(res[k - 1] - res[k - 2], a[i] - res[k - 1]) < eps)\n k--;\n }\n for (int i = n - 2, t = k + 1; i >= 0; res[k++] = a[i--]) {\n while (k >= t and\n cross(res[k - 1] - res[k - 2], a[i] - res[k - 1]) < eps)\n k--;\n }\n res.resize(k - 1);\n return res;\n}\nT Diam(const Poly &a) {\n int n = a.size();\n int x = 0, y = 0;\n rep(i, 1, n) {\n if (a[i].Y > a[x].Y)\n x = i;\n if (a[i].Y < a[y].Y)\n y = i;\n }\n T res = abs(a[x] - a[y]);\n int i = x, j = y;\n do {\n if (cross(a[(i + 1) % n] - a[i], a[(j + 1) % n] - a[j]) < 0)\n i = (i + 1) % n;\n else\n j = (j + 1) % n;\n chmax(res, abs(a[i] - a[j]));\n } while (i != x or j != y);\n return res;\n}\nPoly Cut(const Poly &a, const Line &l) {\n int n = a.size();\n Poly res;\n rep(i, 0, n) {\n Point p = a[i], q = a[(i + 1) % n];\n if (ccw(l.a, l.b, p) != -1)\n res.push_back(p);\n if (ccw(l.a, l.b, p) * ccw(l.a, l.b, q) < 0)\n res.push_back(Intersection(Line(p, q), l));\n }\n return res;\n}\n\nT Closest(Poly &a) {\n int n = a.size();\n if (n <= 1)\n return 0;\n sort(ALL(a), [&](Point a, Point b) {\n return (eq(a.X, b.X) ? a.Y < b.Y : a.X < b.X);\n });\n Poly buf(n);\n auto rec = [&](auto self, int lb, int rb) -> T {\n if (rb - lb <= 1)\n return (T)INF;\n int mid = (lb + rb) >> 1;\n auto x = a[mid].X;\n T res = min(self(self, lb, mid), self(self, mid, rb));\n inplace_merge(a.begin() + lb, a.begin() + mid, a.begin() + rb,\n [&](auto p, auto q) { return p.Y < q.Y; });\n int ptr = 0;\n rep(i, lb, rb) {\n if (abs(a[i].X - x) >= res)\n continue;\n rep(j, 0, ptr) {\n auto sub = a[i] - buf[ptr - 1 - j];\n if (sub.Y >= res)\n break;\n chmin(res, abs(sub));\n }\n buf[ptr++] = a[i];\n }\n return res;\n };\n return rec(rec, 0, n);\n}\n\nCircle Incircle(const Point &a, const Point &b, const Point &c) {\n T A = abs(b - c), B = abs(c - a), C = abs(a - b);\n Point p(A * a.X + B * b.X + C * c.X, A * a.Y + B * b.Y + C * c.Y);\n p /= (A + B + C);\n T r = Dist(Line(a, b), p);\n return Circle(p, r);\n}\nCircle Circumcircle(const Point &a, const Point &b, const Point &c) {\n Line l1((a + b) / 2., (a + b) / 2. + (b - a) * Point(0, 1));\n Line l2((b + c) / 2., (b + c) / 2. + (c - b) * Point(0, 1));\n Point p = Intersection(l1, l2);\n return Circle(p, abs(p - a));\n}\nPoly tangent(const Point &a, const Circle &b) {\n return Intersection(b, Circle(a, sqrt(norm(b.p - a) - b.r * b.r)));\n}\nvector<Line> tangent(const Circle &a, const Circle &b) {\n vector<Line> res;\n T d = abs(a.p - b.p);\n if (eq(d, 0.))\n return res;\n Point u = unit(b.p - a.p);\n Point v = u * Point(0, 1);\n for (int t : {-1, 1}) {\n T h = (a.r + b.r * t) / d;\n if (eq(h * h, 1.)) {\n res.push_back(Line(a.p + (h > 0 ? u : -u) * a.r,\n a.p + (h > 0 ? u : -u) * a.r + v));\n } else if (1 > h * h) {\n Point U = u * h, V = v * sqrt(1 - h * h);\n res.push_back(Line(a.p + (U + V) * a.r, b.p - (U + V) * (b.r * t)));\n res.push_back(Line(a.p + (U - V) * a.r, b.p - (U - V) * (b.r * t)));\n }\n }\n return res;\n}\n\n/**\n * @brief Geometry\n */\n\nint main() {\nwhile(1) {\n double XS, YS;\n int N;\n cin >> XS >> YS >> N;\n if (N == 0) return 0;\n vector<Point> P(N*2);\n rep(i,0,N*2) {\n double x, y;\n cin >> x >> y;\n P[i] = Point(x,y);\n }\n auto Calc = [&](double D) -> double {\n Point Q(0.0,D);\n vector<double> E(N*2);\n rep(i,0,N*2) {\n if (P[i].real() == 0) {\n if (P[i].imag() <= D) E[i] = 0.0;\n else E[i] = YS;\n }\n else {\n Point R = Intersection(Line(Q,P[i]),Line(Point(XS,0.0),Point(XS,YS)));\n if (R.imag() < 0.0) E[i] = 0.0;\n else if (R.imag() > YS) E[i] = YS;\n else E[i] = R.imag();\n }\n }\n sort(ALL(E));\n return E[N]-E[N-1];\n };\n vector<double> Cand;\n Cand.push_back(0.0);\n Cand.push_back(YS);\n Line Jiku(Point(0.0,0.0),Point(0.0,YS));\n rep(i,0,N*2) {\n if (P[i].real() != XS) {\n Point Q = Intersection(Line(P[i],Point(XS,0.0)),Jiku);\n if (0 <= Q.imag() && Q.imag() <= YS) Cand.push_back(Q.imag());\n Q = Intersection(Line(P[i],Point(XS,YS)),Jiku);\n if (0 <= Q.imag() && Q.imag() <= YS) Cand.push_back(Q.imag());\n }\n }\n rep(i,0,N*2) {\n rep(j,i+1,N*2) {\n if (P[i].real() == P[j].real()) continue;\n Point Q = Intersection(Line(P[i],P[j]),Jiku);\n if (0 <= Q.imag() && Q.imag() <= YS) Cand.push_back(Q.imag()); \n }\n }\n sort(ALL(Cand));\n double ANS = 0.0;\n rep(i,0,Cand.size()-1) {\n if (eq(Cand[i],Cand[i+1])) continue;\n ANS += (Cand[i+1]-Cand[i]) * Calc((Cand[i]+Cand[i+1])/2.0);\n }\n printf(\"%.12f\\n\",ANS/(YS*YS));\n}\n}", "accuracy": 1, "time_ms": 260, "memory_kb": 3596, "score_of_the_acc": -0.3213, "final_rank": 4 }, { "submission_id": "aoj_2256_9301836", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing vll = vector<ll>;\nusing vvll = vector<vll>;\nusing vvvll = vector<vvll>;\nusing vb = vector<bool>;\nusing vvb = vector<vb>;\nusing vvvb = vector<vvb>;\n#define all(A) A.begin(),A.end()\n#define rep(i, n) for (int i = 0; i < (int) (n); i++)\ntemplate<class T>\nvoid chmax(T& p, T q) { if (p < q)p = q; };\n\nvector<double> X, Y;\ndouble W, H;\nll N;\nconst double eps = 1e-12;\ndouble cls(double x, double y, double X, double Y, double b = 0.0) {\n if (abs(x - X) < eps)return 1e18;\n double d = (Y - y) / (X - x) * (b - x) + y;\n return d;\n}\n\ndouble f(double y) {\n vector<pair<double, int>> P(2 * N);\n rep(i, 2*N)P[i] = {atan2(Y[i] - y,X[i]),i};\n sort(all(P));\n double D = cls(0.0, y, X[P[N - 1].second], Y[P[N - 1].second], W);\n D = min(max(D, 0.0), H);\n double U = cls(0.0, y, X[P[N].second], Y[P[N].second], W);\n U = min(max(U, 0.0), H);\n return abs(U - D);\n}\n\nvoid solve() {\n X.resize(2 * N);\n Y.resize(2 * N);\n rep(i, 2 * N)cin >> X[i] >> Y[i];\n vector<pair<double, double>> P;\n P.push_back({ 0,f(0.0) });\n P.push_back({ H,f(H) });\n rep(i, 2 * N) {\n double y = cls(X[i], Y[i], W, 0);\n if (0 <= y && y <= double(H))P.push_back({ y,f(y) });\n y = cls(X[i], Y[i], W, H);\n if (0 <= y && y <= double(H))P.push_back({ y,f(y) });\n rep(j, i) {\n y = cls(X[i], Y[i], X[j], Y[j]);\n if (0 <= y && y <= double(H))P.push_back({ y,f(y) });\n }\n }\n sort(all(P));\n double an = 0;\n double PN = P.size();\n cout << fixed << setprecision(15);\n rep(i, PN - 1) {\n an+= (P[i + 1].first - P[i].first) * f((P[i + 1].first + P[i].first) / 2.0);\n // an += (P[i + 1].first - P[i].first) * (P[i + 1].second + P[i].second) / 2.0;\n }\n\n cout << an/(H*H) << endl;\n\n\n}\n\nint main() {\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n\n while (cin >> W >> H >> N) {\n if (H == 0)return 0;\n solve();\n }\n\n}", "accuracy": 1, "time_ms": 3190, "memory_kb": 4200, "score_of_the_acc": -1.3436, "final_rank": 18 }, { "submission_id": "aoj_2256_9069968", "code_snippet": "#line 2 \"cp-library/src/cp-template.hpp\"\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nusing i32 = int;\nusing u32 = unsigned int;\nusing i64 = long long;\nusing u64 = unsigned long long;\nusing i128 = __int128_t;\ntemplate < class T > bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }\ntemplate < class T > bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }\ntemplate < class T, class U > T ceil (T x, U y) { return (x > 0 ? (x + y - 1) / y : x / y); }\ntemplate < class T, class U > T floor(T x, U y) { return (x > 0 ? x / y : (x - y + 1) / y); }\nint popcnt(i32 x) { return __builtin_popcount(x); }\nint popcnt(u32 x) { return __builtin_popcount(x); }\nint popcnt(i64 x) { return __builtin_popcountll(x); }\nint popcnt(u64 x) { return __builtin_popcountll(x); }\n\n#line 2 \"cp-library/src/utility/rep_itr.hpp\"\ntemplate < class T > struct itr_rep {\n T i, d;\n constexpr itr_rep(const T i) noexcept : i(i), d(1) {}\n constexpr itr_rep(const T i, const T d) noexcept : i(i), d(d) {}\n void operator++() noexcept { i += d; }\n constexpr int operator*() const noexcept { return i; }\n constexpr bool operator!=(const itr_rep x) const noexcept { return d > 0 ? i < x.i : i > x.i; }\n};\n\ntemplate < class T > struct rep {\n const itr_rep< T > s, t;\n constexpr rep(const T t) noexcept : s(0), t(t) {}\n constexpr rep(const T s, const T t) noexcept : s(s), t(t) {}\n constexpr rep(const T s, const T t, const T d) noexcept : s(s, d), t(t, d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n\ntemplate < class T > struct revrep {\n const itr_rep < T > s, t;\n constexpr revrep(const T t) noexcept : s(t - 1, -1), t(-1, -1) {}\n constexpr revrep(const T s, const T t) noexcept : s(t - 1, -1), t(s - 1, -1) {}\n constexpr revrep(const T s, const T t, const T d) noexcept : s(t - 1, -d), t(s - 1, -d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n#line 3 \"cp-library/src/utility/io.hpp\"\n\n/* 128bit integer */\nistream& operator>>(istream& is, i128& x) {\n std::string s; is >> s;\n int pm = (s[0] == '-');\n x = 0;\n for(int i : rep(pm, int(s.size()))) x = x * 10 + (s[i] - '0');\n if(pm) x *= -1;\n return is;\n}\nostream& operator<<(ostream& os, const i128& x) {\n if(x == 0) return os << '0';\n i128 y = x;\n if(y < 0) { os << '-'; y *= -1; }\n std::vector<int> ny;\n while(y > 0) { ny.push_back(y % 10); y /= 10; }\n for(int i : revrep(ny.size())) os << ny[i];\n return os;\n}\n\ntemplate < class S, class T > istream& operator>>(istream& is, std::pair< S, T >& x) { is >> x.first >> x.second; return is; }\ntemplate < class S, class T > ostream& operator<<(ostream& os, const std::pair< S, T >& x) { os << x.first << \" \" << x.second; return os; }\n\nnamespace scanner {\n struct sca {\n template < class T > operator T() {\n T s; std::cin >> s; return s;\n }\n };\n struct vec {\n int n;\n vec(int n) : n(n) {}\n template < class T > operator std::vector< T >() {\n std::vector< T > v(n);\n for(T& x : v) std::cin >> x;\n return v;\n }\n };\n struct mat {\n int h, w;\n mat(int h, int w) : h(h), w(w) {}\n template < class T > operator std::vector< std::vector< T > >() {\n std::vector m(h, std::vector< T >(w));\n for(std::vector< T >& v : m) for(T& x : v) std::cin >> x;\n return m;\n }\n };\n struct speedup {\n speedup() {\n std::cin.tie(0);\n std::ios::sync_with_stdio(0);\n }\n } speedup_instance;\n}\nscanner::sca in() { return scanner::sca(); }\nscanner::vec in(int n) { return scanner::vec(n); }\nscanner::mat in(int h, int w) { return scanner::mat(h, w); }\n\nnamespace printer {\n void precision(int d) { std::cout << std::fixed << std::setprecision(d); }\n void flush() { std::cout.flush(); }\n}\n\ntemplate < class T >\nostream& operator<<(ostream& os, const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) { os << a[i]; if(i != n - 1) os << ' '; }\n return os;\n}\n\nint print() { std::cout << '\\n'; return 0; }\ntemplate < class head, class... tail > int print(head&& h, tail&&... t) {\n std::cout << h; if(sizeof...(tail)) std::cout << ' ';\n return print(std::forward<tail>(t)...);\n}\ntemplate < class T > int print_n(const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) std::cout << a[i] << \"\\n\";\n return 0;\n}\n\n\n#line 2 \"cp-library/src/utility/key_val.hpp\"\n\ntemplate < class K, class V >\nstruct key_val {\n K key; V val;\n key_val() {}\n key_val(K key, V val) : key(key), val(val) {}\n template < std::size_t Index >\n std::tuple_element_t< Index, key_val >& get() {\n if constexpr (Index == 0) return key;\n if constexpr (Index == 1) return val;\n }\n};\n\nnamespace std {\n\ntemplate < class K, class V > struct tuple_size < key_val< K, V > > : integral_constant< size_t, 2 > {};\ntemplate < class K, class V > struct tuple_element < 0, key_val< K, V > > { using type = K; };\ntemplate < class K, class V > struct tuple_element < 1, key_val< K, V > > { using type = V; };\n\n}\n#line 2 \"cp-library/src/utility/vec_op.hpp\"\ntemplate < class T > key_val< int, T > max_of(const vector< T >& a) {\n int i = std::max_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class T > key_val< int, T > min_of(const vector< T >& a) {\n int i = std::min_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class S, class T > S sum_of(const vector< T >& a) {\n S sum = 0;\n for(const T x : a) sum += x;\n return sum;\n}\ntemplate < class S, class T > vector< S > freq_of(const vector< T >& a, T L, T R) {\n vector< S > res(R - L, S(0));\n for(const T x : a) res[x - L] += 1;\n return res;\n}\ntemplate < class S, class T > struct prefix_sum {\n vector< S > s;\n prefix_sum(const vector< T >& a) : s(a) {\n s.insert(s.begin(), S(0));\n for(int i : rep(a.size())) s[i + 1] += s[i];\n }\n // [L, R)\n S sum(int L, int R) { return s[R] - s[L]; }\n};\n#line 3 \"cp-library/src/utility/heap.hpp\"\n\ntemplate < class T > using heap_min = std::priority_queue< T, std::vector< T >, std::greater< T > >;\ntemplate < class T > using heap_max = std::priority_queue< T, std::vector< T >, std::less< T > >;\n\n#line 27 \"cp-library/src/cp-template.hpp\"\n\n#line 1 \"cp-library/src/algorithm/bin_search.hpp\"\ntemplate < class T, class F >\nT bin_search(T ok, T ng, F f) {\n while(abs(ng - ok) > 1) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n\ntemplate < class T, class F >\nT bin_search_real(T ok, T ng, F f, int step = 80) {\n while(step--) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n#line 2 \"cp-library/src/algorithm/argsort.hpp\"\n\ntemplate < class T > std::vector< int > argsort(const std::vector< T > &a) {\n std::vector< int > ids((int)a.size());\n std::iota(ids.begin(), ids.end(), 0);\n std::sort(ids.begin(), ids.end(), [&](int i, int j) {\n return a[i] < a[j] || (a[i] == a[j] && i < j);\n });\n return ids;\n}\n#line 1 \"macro.hpp\"\nnamespace macro {\n\nusing size_type = int;\ntemplate < class container > void sort(container& a) { std::sort(std:: begin(a), std:: end(a)); }\ntemplate < class container > void rsort(container& a) { std::sort(std::rbegin(a), std::rend(a)); }\ntemplate < class container > void reverse(container& a) { std::reverse(std::begin(a), std::end(a)); }\ntemplate < class container > void unique(container& a) {\n std::sort(std::begin(a), std::end(a));\n a.erase(std::unique(std::begin(a), std::end(a)), std::end(a));\n}\ntemplate < class container > container sorted(const container& a) { container b = a; sort(b); return std::move(b); }\ntemplate < class container > container rsorted(const container& a) { container b = a; rsort(b); return std::move(b); }\ntemplate < class container, class compare > void sort(container& a, const compare& cmp) { std::sort(std::begin(a), std::end(a), cmp); }\ntemplate < class container, class compare > container sorted(const container& a, const compare& cmp) { container b = a; sort(b, cmp); return std::move(b); }\ntemplate < class container, class value > size_type lower_bound(const container& a, const value& x) { return std::lower_bound(std::begin(a), std::end(a), x) - std::begin(a); }\ntemplate < class container, class value > size_type upper_bound(const container& a, const value& x) { return std::upper_bound(std::begin(a), std::end(a), x) - std::begin(a); }\n\nconst std::vector<std::pair<size_type, size_type>> dir4 = { {+1, 0}, {-1, 0}, { 0, +1}, { 0, -1} };\nconst std::vector<std::pair<size_type, size_type>> dir8 = { {-1, -1}, {-1, 0}, {-1, +1}, { 0, -1}, { 0, +1}, {+1, -1}, {+1, 0}, {+1, +1} };\n\n#ifdef _DEBUG\n#define debug(x) std::cout << \"[\" << __LINE__ << \"] \" << #x << \": \" << x << std::endl\n#else\n#define debug(x)\n#endif\n\ntemplate < class container > void concat(container& a, const container& b) {\n a.insert(std::end(a), std::begin(b), std::end(b));\n}\nstd::vector<size_type> iota(const size_type n) {\n std::vector<size_type> I(n);\n std::iota(std::begin(I), std::end(I), 0);\n return I;\n}\ntemplate < class container > std::vector<size_type> sort_idx(const container& a) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return a[i] < a[j] or (a[i] == a[j] and i < j); });\n return I;\n}\ntemplate < class container, class compare > std::vector<size_type> sort_idx(const container& a, const compare& cmp) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return cmp(a[i], a[j]) or (a[i] == a[j] and i < j); });\n return std::move(I);\n}\n\nstruct grid {\n using size_type = int;\n size_type H, W;\n grid(const size_type H, const size_type W) : H(H), W(W) {}\n bool contains(const size_type i, const size_type j) {\n return 0 <= i and i < H and 0 <= j and j < W;\n }\n};\n\nusing f64 = long double;\n\n} // namespace macro\n\nusing namespace macro;\n#line 3 \"A.cpp\"\n\ntemplate < class T > struct point {\n T x, y;\n point() : x(0), y(0) {}\n point(T x, T y) : x(x), y(y) {}\n point(std::pair< T, T > p) : x(p.first), y(p.second) {}\n point& operator+=(const point& p) { x += p.x, y += p.y; return *this; }\n point& operator-=(const point& p) { x -= p.x, y -= p.y; return *this; }\n point& operator*=(const T r) { x *= r, y *= r; return *this; }\n point& operator/=(const T r) { x /= r, y /= r; return *this; }\n point operator+(const point& p) const { return point(*this) += p; }\n point operator-(const point& p) const { return point(*this) -= p; }\n point operator*(const T r) const { return point(*this) *= r; }\n point operator/(const T r) const { return point(*this) /= r; }\n point operator-() const { return {-x, -y}; }\n bool operator==(const point& p) const { return x == p.x and y == p.y; }\n bool operator!=(const point& p) const { return x != p.x or y != p.y; }\n bool operator<(const point& p) const { return x == p.x ? y < p.y : x < p.x; }\n point< T > rot(double theta) {\n static_assert(is_floating_point_v< T >);\n double cos_ = std::cos(theta), sin_ = std::sin(theta);\n return {cos_ * x - sin_ * y, sin_ * x + cos_ * y};\n }\n};\ntemplate < class T > istream& operator>>(istream& is, point< T >& p) { return is >> p.x >> p.y; }\ntemplate < class T > ostream& operator<<(ostream& os, point< T >& p) { return os << p.x << \" \" << p.y; }\ntemplate < class T > T dot(const point< T >& a, const point< T >& b) { return a.x * b.x + a.y * b.y; }\ntemplate < class T > T det(const point< T >& a, const point< T >& b) { return a.x * b.y - a.y * b.x; }\ntemplate < class T > T norm(const point< T >& p) { return p.x * p.x + p.y * p.y; }\ntemplate < class T > double abs(const point< T >& p) { return std::sqrt(norm(p)); }\ntemplate < class T > double angle(const point< T >& p) { return std::atan2(p.y, p.x); }\ntemplate < class T > int sign(const T x) {\n T e = (is_integral_v< T > ? 1 : 1e-8);\n if(x <= -e) return -1;\n if(x >= +e) return +1;\n return 0;\n}\ntemplate < class T > bool equals(const T& a, const T& b) { return sign(a - b) == 0; }\ntemplate < class T > int ccw(const point< T >& a, point< T > b, point< T > c) {\n b -= a, c -= a;\n if(sign(det(b, c)) == +1) return +1; // counter clockwise\n if(sign(det(b, c)) == -1) return -1; // clockwise\n if(sign(dot(b, c)) == -1) return +2; // c-a-b\n if(norm(b) < norm(c)) return -2; // a-b-c\n return 0; // a-c-b\n}\n\ntemplate < class T > struct line {\n point< T > a, b;\n line() {}\n line(point< T > a, point< T > b) : a(a), b(b) {}\n};\ntemplate < class T > point< T > projection(const line< T >& l, const point< T >& p) {\n static_assert(is_floating_point_v< T >);\n return l.a + (l.a - l.b) * dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n}\ntemplate < class T > point< T > reflection(const line< T >& l, const point< T >& p) {\n static_assert(is_floating_point_v< T >);\n return p + (projection(l, p) - p) * T(2);\n}\ntemplate < class T > bool orthogonal(const line< T >& a, const line< T >& b) { return equals(dot(a.b - a.a, b.b - b.a), T(0)); }\ntemplate < class T > bool parallel (const line< T >& a, const line< T >& b) { return equals(det(a.b - a.a, b.b - b.a), T(0)); }\ntemplate < class T > point< T > cross_point_ll(const line< T >& l, const line< T >& m) {\n static_assert(is_floating_point_v< T >);\n T A = det(l.b - l.a, m.b - m.a);\n T B = det(l.b - l.a, l.b - m.a);\n if(equals(abs(A), T(0)) and equals(abs(B), T(0))) return m.a;\n return m.a + (m.b - m.a) * B / A;\n}\n\ntemplate < class T > using segment = line< T >;\ntemplate < class T > bool intersect_ss(const segment< T >& s, const segment< T >& t) {\n return ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 and ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0;\n}\ntemplate < class T > double distance_sp(const segment< T >& s, const point< T >& p) {\n static_assert(is_floating_point_v< T >);\n point r = projection(s, p);\n if(ccw(s.a, s.b, r) == 0) return abs(r - p);\n return std::min(abs(s.a - p), abs(s.b - p));\n}\ntemplate < class T > double distance_ss(const segment< T >& a, const segment< T >& b) {\n if(intersect_ss(a, b)) return 0;\n return std::min({ distance_sp(a, b.a), distance_sp(a, b.b), distance_sp(b, a.a), distance_sp(b, a.b) });\n}\n\ntemplate < class T > using polygon = std::vector< point< T > >;\ntemplate < class T > T area2(const polygon< T >& p) {\n T s = 0;\n int n = p.size();\n for(int i = 0; i < n; i++) s += det(p[i], p[(i + 1) % n]);\n return s;\n}\ntemplate < class T > T area(const polygon< T >& p) { return area2(p) / T(2); }\n\ntemplate < class T > bool is_convex(const polygon< T >& p) {\n int n = p.size();\n for(int i = 0; i < n; i++) if(ccw(p[(i - 1 + n) % n], p[i], p[(i + 1) % n]) == -1) return false;\n return true;\n}\ntemplate < class T > int contains(const polygon< T >& g, const point< T >& p) {\n int n = g.size();\n bool in = false;\n for(int i = 0; i < n; i++) {\n point a = g[i] - p, b = g[(i + 1) % n] - p;\n if(sign(a.y - b.y) == +1) std::swap(a, b);\n if(sign(a.y) <= 0 and sign(b.y) ==+1 and sign(det(a, b)) == -1) in = !in;\n if(sign(det(a, b)) == 0 and sign(dot(a, b)) <= 0) return 1; // ON\n }\n return in ? 2 : 0;\n}\ntemplate < class T > polygon< T > convex_cut(const polygon< T >& p, const line< T >& l) {\n int n = p.size();\n polygon< T > res;\n for(int i = 0; i < n; i++) {\n point now = p[i], nxt = p[(i + 1) % n];\n if(ccw(l.a, l.b, now) != -1) res.push_back(now);\n if(ccw(l.a, l.b, now) * ccw(l.a, l.b, nxt) < 0) res.push_back(cross_point_ll(line(now, nxt), l));\n }\n return res;\n}\ntemplate < class T > polygon< T > convex_hull(polygon< T >& p) {\n int n = p.size(), k = 0;\n if(n <= 2) return p;\n std::sort(p.begin(), p.end());\n polygon< T > ch(n + n);\n for(int i = 0; i < n; ch[k++] = p[i++])\n while(k >= 2 and sign(det(ch[k - 1] - ch[k - 2], p[i] - ch[k - 1])) == -1) k--;\n for(int i = n - 2, t = k + 1; i >= 0; ch[k++] = p[i--])\n while(k >= t and sign(det(ch[k - 1] - ch[k - 2], p[i] - ch[k - 1])) == -1) k--;\n ch.resize(k - 1);\n return ch;\n}\ntemplate < class T > T diameter2(const polygon< T >& p) {\n static_assert(is_floating_point_v< T >);\n int n = p.size(), is = 0, js = 0;\n for(int i = 1; i < n; i++) {\n if(sign(p[i].y - p[is].y) == +1) is = i;\n if(sign(p[i].y - p[js].y) == -1) js = i;\n }\n T dist_max = norm(p[is] - p[js]);\n int maxi = is, i = is, maxj = js, j = js;\n do {\n if(sign(det(p[(i + 1) % n] - p[i], p[(j + 1) % n] - p[j])) >= 0) j = (j + 1) % n; else i = (i + 1) % n;\n if(norm(p[i] - p[j]) > dist_max) {\n dist_max = norm(p[i] - p[j]);\n maxi = i, maxj = j;\n }\n } while(i != is or j != js);\n return dist_max;\n}\ntemplate < class T > double diameter(const polygon< T >& p) {\n static_assert(is_floating_point_v< T >);\n return std::sqrt(diameter2(p));\n}\n\ntemplate < class T > struct circle {\n point< T > p;\n T r;\n circle() = default;\n circle(point< T > p, T r) : p(p), r(r) {}\n};\ntemplate < class T > istream& operator>>(istream& is, circle< T >& c) { return is >> c.p >> c.r; }\ntemplate < class T > int intersect_cc(circle< T > c1, circle< T > c2) {\n if(c1.r < c2.r) std::swap(c1, c2);\n T d = abs(c1.p - c2.p);\n if(sign(c1.r + c2.r - d) == -1) return 4;\n if(equals(c1.r + c2.r, d)) return 3;\n if(sign(c1.r - c2.r - d) == -1) return 2;\n if(equals(c1.r - c2.r, d)) return 1;\n return 0;\n}\ntemplate < class T > std::pair<point< T >, point< T >> cross_point_cc(const circle< T >& c1, const circle< T >& c2) {\n T d = abs(c1.p - c2.p);\n T a = std::acos((c1.r * c1.r + d * d - c2.r * c2.r) / (2 * c1.r * d));\n T t = angle(c2.p - c1.p);\n point< T > p1 = c1.p + point< T >(std::cos(t + a), std::sin(t + a)) * c1.r;\n point< T > p2 = c1.p + point< T >(std::cos(t - a), std::sin(t - a)) * c1.r;\n return {p1, p2};\n}\n\nf64 solve(int W, int H, int N) {\n vector<point<f64>> P(2 * N);\n for(int i : rep(2 * N)) P[i] = in();\n vector<point<f64>> Q = {\n point<f64>(0, 0), point<f64>(0, H), point<f64>(W, 0), point<f64>(W, H)\n };\n P.insert(P.end(), Q.begin(), Q.end());\n N = N + 2;\n\n vector<f64> ys = {0.0, f64(H)};\n auto div = [&](line<f64> l) {\n int L = 0, R = 0;\n for(int i : rep(2 * N)) {\n line<f64> m(P[i], P[i] + point<f64>(0, 1));\n f64 y = cross_point_ll<f64>(l, m).y;\n if(y + 1e-8 <= P[i].y) L++;\n if(y - 1e-8 <= P[i].y) R++;\n }\n return L <= N and N <= R;\n };\n auto push = [&](line<f64> l) {\n if(div(l)) {\n line<f64> m(point<f64>(0, 0), point<f64>(0, H));\n f64 y = cross_point_ll<f64>(l, m).y;\n if(0.0 <= y and y <= H) ys.push_back(y);\n }\n };\n for(int i : rep(2 * N)) {\n for(int j : rep(i + 1, 2 * N)) push(line<f64>(P[i], P[j]));\n }\n\n unique(ys);\n\n f64 ans = 0;\n const int n = ys.size();\n for(int k : rep(n - 1)) {\n f64 dL = ys[k + 1] - ys[k];\n f64 dR = [&] {\n f64 yL = (ys[k] + ys[k + 1]) / 2.0;\n f64 min = +1e18, max = -1e18;\n for(int i : rep(2 * N)) if(P[i].x > 0) {\n line<f64> l(P[i], point<f64>(0, yL));\n if(div(l)) {\n f64 yR = cross_point_ll<f64>(l, line<f64>(point<f64>(W, 0), point<f64>(W, H))).y;\n chmin(min, yR);\n chmax(max, yR);\n }\n }\n chmax<f64>(min, 0.0);\n chmin<f64>(max, H);\n return std::max<f64>(0.0, max - min);\n }();\n ans += dL * dR;\n }\n return ans / f64(H * H);\n}\n\nint main() {\n while(true) {\n int W = in(), H = in(), N = in();\n if(make_tuple(W, H, N) == make_tuple(0, 0, 0)) return 0;\n printer::precision(20);\n print(solve(W, H, N));\n }\n}", "accuracy": 1, "time_ms": 5590, "memory_kb": 3632, "score_of_the_acc": -1.2219, "final_rank": 17 }, { "submission_id": "aoj_2256_9069954", "code_snippet": "#line 2 \"cp-library/src/cp-template.hpp\"\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nusing i32 = int;\nusing u32 = unsigned int;\nusing i64 = long long;\nusing u64 = unsigned long long;\nusing i128 = __int128_t;\ntemplate < class T > bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }\ntemplate < class T > bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }\ntemplate < class T, class U > T ceil (T x, U y) { return (x > 0 ? (x + y - 1) / y : x / y); }\ntemplate < class T, class U > T floor(T x, U y) { return (x > 0 ? x / y : (x - y + 1) / y); }\nint popcnt(i32 x) { return __builtin_popcount(x); }\nint popcnt(u32 x) { return __builtin_popcount(x); }\nint popcnt(i64 x) { return __builtin_popcountll(x); }\nint popcnt(u64 x) { return __builtin_popcountll(x); }\n\n#line 2 \"cp-library/src/utility/rep_itr.hpp\"\ntemplate < class T > struct itr_rep {\n T i, d;\n constexpr itr_rep(const T i) noexcept : i(i), d(1) {}\n constexpr itr_rep(const T i, const T d) noexcept : i(i), d(d) {}\n void operator++() noexcept { i += d; }\n constexpr int operator*() const noexcept { return i; }\n constexpr bool operator!=(const itr_rep x) const noexcept { return d > 0 ? i < x.i : i > x.i; }\n};\n\ntemplate < class T > struct rep {\n const itr_rep< T > s, t;\n constexpr rep(const T t) noexcept : s(0), t(t) {}\n constexpr rep(const T s, const T t) noexcept : s(s), t(t) {}\n constexpr rep(const T s, const T t, const T d) noexcept : s(s, d), t(t, d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n\ntemplate < class T > struct revrep {\n const itr_rep < T > s, t;\n constexpr revrep(const T t) noexcept : s(t - 1, -1), t(-1, -1) {}\n constexpr revrep(const T s, const T t) noexcept : s(t - 1, -1), t(s - 1, -1) {}\n constexpr revrep(const T s, const T t, const T d) noexcept : s(t - 1, -d), t(s - 1, -d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n#line 3 \"cp-library/src/utility/io.hpp\"\n\n/* 128bit integer */\nistream& operator>>(istream& is, i128& x) {\n std::string s; is >> s;\n int pm = (s[0] == '-');\n x = 0;\n for(int i : rep(pm, int(s.size()))) x = x * 10 + (s[i] - '0');\n if(pm) x *= -1;\n return is;\n}\nostream& operator<<(ostream& os, const i128& x) {\n if(x == 0) return os << '0';\n i128 y = x;\n if(y < 0) { os << '-'; y *= -1; }\n std::vector<int> ny;\n while(y > 0) { ny.push_back(y % 10); y /= 10; }\n for(int i : revrep(ny.size())) os << ny[i];\n return os;\n}\n\ntemplate < class S, class T > istream& operator>>(istream& is, std::pair< S, T >& x) { is >> x.first >> x.second; return is; }\ntemplate < class S, class T > ostream& operator<<(ostream& os, const std::pair< S, T >& x) { os << x.first << \" \" << x.second; return os; }\n\nnamespace scanner {\n struct sca {\n template < class T > operator T() {\n T s; std::cin >> s; return s;\n }\n };\n struct vec {\n int n;\n vec(int n) : n(n) {}\n template < class T > operator std::vector< T >() {\n std::vector< T > v(n);\n for(T& x : v) std::cin >> x;\n return v;\n }\n };\n struct mat {\n int h, w;\n mat(int h, int w) : h(h), w(w) {}\n template < class T > operator std::vector< std::vector< T > >() {\n std::vector m(h, std::vector< T >(w));\n for(std::vector< T >& v : m) for(T& x : v) std::cin >> x;\n return m;\n }\n };\n struct speedup {\n speedup() {\n std::cin.tie(0);\n std::ios::sync_with_stdio(0);\n }\n } speedup_instance;\n}\nscanner::sca in() { return scanner::sca(); }\nscanner::vec in(int n) { return scanner::vec(n); }\nscanner::mat in(int h, int w) { return scanner::mat(h, w); }\n\nnamespace printer {\n void precision(int d) { std::cout << std::fixed << std::setprecision(d); }\n void flush() { std::cout.flush(); }\n}\n\ntemplate < class T >\nostream& operator<<(ostream& os, const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) { os << a[i]; if(i != n - 1) os << ' '; }\n return os;\n}\n\nint print() { std::cout << '\\n'; return 0; }\ntemplate < class head, class... tail > int print(head&& h, tail&&... t) {\n std::cout << h; if(sizeof...(tail)) std::cout << ' ';\n return print(std::forward<tail>(t)...);\n}\ntemplate < class T > int print_n(const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) std::cout << a[i] << \"\\n\";\n return 0;\n}\n\n\n#line 2 \"cp-library/src/utility/key_val.hpp\"\n\ntemplate < class K, class V >\nstruct key_val {\n K key; V val;\n key_val() {}\n key_val(K key, V val) : key(key), val(val) {}\n template < std::size_t Index >\n std::tuple_element_t< Index, key_val >& get() {\n if constexpr (Index == 0) return key;\n if constexpr (Index == 1) return val;\n }\n};\n\nnamespace std {\n\ntemplate < class K, class V > struct tuple_size < key_val< K, V > > : integral_constant< size_t, 2 > {};\ntemplate < class K, class V > struct tuple_element < 0, key_val< K, V > > { using type = K; };\ntemplate < class K, class V > struct tuple_element < 1, key_val< K, V > > { using type = V; };\n\n}\n#line 2 \"cp-library/src/utility/vec_op.hpp\"\ntemplate < class T > key_val< int, T > max_of(const vector< T >& a) {\n int i = std::max_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class T > key_val< int, T > min_of(const vector< T >& a) {\n int i = std::min_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class S, class T > S sum_of(const vector< T >& a) {\n S sum = 0;\n for(const T x : a) sum += x;\n return sum;\n}\ntemplate < class S, class T > vector< S > freq_of(const vector< T >& a, T L, T R) {\n vector< S > res(R - L, S(0));\n for(const T x : a) res[x - L] += 1;\n return res;\n}\ntemplate < class S, class T > struct prefix_sum {\n vector< S > s;\n prefix_sum(const vector< T >& a) : s(a) {\n s.insert(s.begin(), S(0));\n for(int i : rep(a.size())) s[i + 1] += s[i];\n }\n // [L, R)\n S sum(int L, int R) { return s[R] - s[L]; }\n};\n#line 3 \"cp-library/src/utility/heap.hpp\"\n\ntemplate < class T > using heap_min = std::priority_queue< T, std::vector< T >, std::greater< T > >;\ntemplate < class T > using heap_max = std::priority_queue< T, std::vector< T >, std::less< T > >;\n\n#line 27 \"cp-library/src/cp-template.hpp\"\n\n#line 1 \"cp-library/src/algorithm/bin_search.hpp\"\ntemplate < class T, class F >\nT bin_search(T ok, T ng, F f) {\n while(abs(ng - ok) > 1) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n\ntemplate < class T, class F >\nT bin_search_real(T ok, T ng, F f, int step = 80) {\n while(step--) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n#line 2 \"cp-library/src/algorithm/argsort.hpp\"\n\ntemplate < class T > std::vector< int > argsort(const std::vector< T > &a) {\n std::vector< int > ids((int)a.size());\n std::iota(ids.begin(), ids.end(), 0);\n std::sort(ids.begin(), ids.end(), [&](int i, int j) {\n return a[i] < a[j] || (a[i] == a[j] && i < j);\n });\n return ids;\n}\n#line 1 \"macro.hpp\"\nnamespace macro {\n\nusing size_type = int;\ntemplate < class container > void sort(container& a) { std::sort(std:: begin(a), std:: end(a)); }\ntemplate < class container > void rsort(container& a) { std::sort(std::rbegin(a), std::rend(a)); }\ntemplate < class container > void reverse(container& a) { std::reverse(std::begin(a), std::end(a)); }\ntemplate < class container > void unique(container& a) {\n std::sort(std::begin(a), std::end(a));\n a.erase(std::unique(std::begin(a), std::end(a)), std::end(a));\n}\ntemplate < class container > container sorted(const container& a) { container b = a; sort(b); return std::move(b); }\ntemplate < class container > container rsorted(const container& a) { container b = a; rsort(b); return std::move(b); }\ntemplate < class container, class compare > void sort(container& a, const compare& cmp) { std::sort(std::begin(a), std::end(a), cmp); }\ntemplate < class container, class compare > container sorted(const container& a, const compare& cmp) { container b = a; sort(b, cmp); return std::move(b); }\ntemplate < class container, class value > size_type lower_bound(const container& a, const value& x) { return std::lower_bound(std::begin(a), std::end(a), x) - std::begin(a); }\ntemplate < class container, class value > size_type upper_bound(const container& a, const value& x) { return std::upper_bound(std::begin(a), std::end(a), x) - std::begin(a); }\n\nconst std::vector<std::pair<size_type, size_type>> dir4 = { {+1, 0}, {-1, 0}, { 0, +1}, { 0, -1} };\nconst std::vector<std::pair<size_type, size_type>> dir8 = { {-1, -1}, {-1, 0}, {-1, +1}, { 0, -1}, { 0, +1}, {+1, -1}, {+1, 0}, {+1, +1} };\n\n#ifdef _DEBUG\n#define debug(x) std::cout << \"[\" << __LINE__ << \"] \" << #x << \": \" << x << std::endl\n#else\n#define debug(x)\n#endif\n\ntemplate < class container > void concat(container& a, const container& b) {\n a.insert(std::end(a), std::begin(b), std::end(b));\n}\nstd::vector<size_type> iota(const size_type n) {\n std::vector<size_type> I(n);\n std::iota(std::begin(I), std::end(I), 0);\n return I;\n}\ntemplate < class container > std::vector<size_type> sort_idx(const container& a) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return a[i] < a[j] or (a[i] == a[j] and i < j); });\n return I;\n}\ntemplate < class container, class compare > std::vector<size_type> sort_idx(const container& a, const compare& cmp) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return cmp(a[i], a[j]) or (a[i] == a[j] and i < j); });\n return std::move(I);\n}\n\nstruct grid {\n using size_type = int;\n size_type H, W;\n grid(const size_type H, const size_type W) : H(H), W(W) {}\n bool contains(const size_type i, const size_type j) {\n return 0 <= i and i < H and 0 <= j and j < W;\n }\n};\n\nusing f64 = long double;\n\n} // namespace macro\n\nusing namespace macro;\n#line 3 \"A.cpp\"\n\ntemplate < class T > struct point {\n T x, y;\n point() : x(0), y(0) {}\n point(T x, T y) : x(x), y(y) {}\n point(std::pair< T, T > p) : x(p.first), y(p.second) {}\n point& operator+=(const point& p) { x += p.x, y += p.y; return *this; }\n point& operator-=(const point& p) { x -= p.x, y -= p.y; return *this; }\n point& operator*=(const T r) { x *= r, y *= r; return *this; }\n point& operator/=(const T r) { x /= r, y /= r; return *this; }\n point operator+(const point& p) const { return point(*this) += p; }\n point operator-(const point& p) const { return point(*this) -= p; }\n point operator*(const T r) const { return point(*this) *= r; }\n point operator/(const T r) const { return point(*this) /= r; }\n point operator-() const { return {-x, -y}; }\n bool operator==(const point& p) const { return x == p.x and y == p.y; }\n bool operator!=(const point& p) const { return x != p.x or y != p.y; }\n bool operator<(const point& p) const { return x == p.x ? y < p.y : x < p.x; }\n point< T > rot(double theta) {\n static_assert(is_floating_point_v< T >);\n double cos_ = std::cos(theta), sin_ = std::sin(theta);\n return {cos_ * x - sin_ * y, sin_ * x + cos_ * y};\n }\n};\ntemplate < class T > istream& operator>>(istream& is, point< T >& p) { return is >> p.x >> p.y; }\ntemplate < class T > ostream& operator<<(ostream& os, point< T >& p) { return os << p.x << \" \" << p.y; }\ntemplate < class T > T dot(const point< T >& a, const point< T >& b) { return a.x * b.x + a.y * b.y; }\ntemplate < class T > T det(const point< T >& a, const point< T >& b) { return a.x * b.y - a.y * b.x; }\ntemplate < class T > T norm(const point< T >& p) { return p.x * p.x + p.y * p.y; }\ntemplate < class T > double abs(const point< T >& p) { return std::sqrt(norm(p)); }\ntemplate < class T > double angle(const point< T >& p) { return std::atan2(p.y, p.x); }\ntemplate < class T > int sign(const T x) {\n T e = (is_integral_v< T > ? 1 : 1e-8);\n if(x <= -e) return -1;\n if(x >= +e) return +1;\n return 0;\n}\ntemplate < class T > bool equals(const T& a, const T& b) { return sign(a - b) == 0; }\ntemplate < class T > int ccw(const point< T >& a, point< T > b, point< T > c) {\n b -= a, c -= a;\n if(sign(det(b, c)) == +1) return +1; // counter clockwise\n if(sign(det(b, c)) == -1) return -1; // clockwise\n if(sign(dot(b, c)) == -1) return +2; // c-a-b\n if(norm(b) < norm(c)) return -2; // a-b-c\n return 0; // a-c-b\n}\n\ntemplate < class T > struct line {\n point< T > a, b;\n line() {}\n line(point< T > a, point< T > b) : a(a), b(b) {}\n};\ntemplate < class T > point< T > projection(const line< T >& l, const point< T >& p) {\n static_assert(is_floating_point_v< T >);\n return l.a + (l.a - l.b) * dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n}\ntemplate < class T > point< T > reflection(const line< T >& l, const point< T >& p) {\n static_assert(is_floating_point_v< T >);\n return p + (projection(l, p) - p) * T(2);\n}\ntemplate < class T > bool orthogonal(const line< T >& a, const line< T >& b) { return equals(dot(a.b - a.a, b.b - b.a), T(0)); }\ntemplate < class T > bool parallel (const line< T >& a, const line< T >& b) { return equals(det(a.b - a.a, b.b - b.a), T(0)); }\ntemplate < class T > point< T > cross_point_ll(const line< T >& l, const line< T >& m) {\n static_assert(is_floating_point_v< T >);\n T A = det(l.b - l.a, m.b - m.a);\n T B = det(l.b - l.a, l.b - m.a);\n if(equals(abs(A), T(0)) and equals(abs(B), T(0))) return m.a;\n return m.a + (m.b - m.a) * B / A;\n}\n\ntemplate < class T > using segment = line< T >;\ntemplate < class T > bool intersect_ss(const segment< T >& s, const segment< T >& t) {\n return ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 and ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0;\n}\ntemplate < class T > double distance_sp(const segment< T >& s, const point< T >& p) {\n static_assert(is_floating_point_v< T >);\n point r = projection(s, p);\n if(ccw(s.a, s.b, r) == 0) return abs(r - p);\n return std::min(abs(s.a - p), abs(s.b - p));\n}\ntemplate < class T > double distance_ss(const segment< T >& a, const segment< T >& b) {\n if(intersect_ss(a, b)) return 0;\n return std::min({ distance_sp(a, b.a), distance_sp(a, b.b), distance_sp(b, a.a), distance_sp(b, a.b) });\n}\n\ntemplate < class T > using polygon = std::vector< point< T > >;\ntemplate < class T > T area2(const polygon< T >& p) {\n T s = 0;\n int n = p.size();\n for(int i = 0; i < n; i++) s += det(p[i], p[(i + 1) % n]);\n return s;\n}\ntemplate < class T > T area(const polygon< T >& p) { return area2(p) / T(2); }\n\ntemplate < class T > bool is_convex(const polygon< T >& p) {\n int n = p.size();\n for(int i = 0; i < n; i++) if(ccw(p[(i - 1 + n) % n], p[i], p[(i + 1) % n]) == -1) return false;\n return true;\n}\ntemplate < class T > int contains(const polygon< T >& g, const point< T >& p) {\n int n = g.size();\n bool in = false;\n for(int i = 0; i < n; i++) {\n point a = g[i] - p, b = g[(i + 1) % n] - p;\n if(sign(a.y - b.y) == +1) std::swap(a, b);\n if(sign(a.y) <= 0 and sign(b.y) ==+1 and sign(det(a, b)) == -1) in = !in;\n if(sign(det(a, b)) == 0 and sign(dot(a, b)) <= 0) return 1; // ON\n }\n return in ? 2 : 0;\n}\ntemplate < class T > polygon< T > convex_cut(const polygon< T >& p, const line< T >& l) {\n int n = p.size();\n polygon< T > res;\n for(int i = 0; i < n; i++) {\n point now = p[i], nxt = p[(i + 1) % n];\n if(ccw(l.a, l.b, now) != -1) res.push_back(now);\n if(ccw(l.a, l.b, now) * ccw(l.a, l.b, nxt) < 0) res.push_back(cross_point_ll(line(now, nxt), l));\n }\n return res;\n}\ntemplate < class T > polygon< T > convex_hull(polygon< T >& p) {\n int n = p.size(), k = 0;\n if(n <= 2) return p;\n std::sort(p.begin(), p.end());\n polygon< T > ch(n + n);\n for(int i = 0; i < n; ch[k++] = p[i++])\n while(k >= 2 and sign(det(ch[k - 1] - ch[k - 2], p[i] - ch[k - 1])) == -1) k--;\n for(int i = n - 2, t = k + 1; i >= 0; ch[k++] = p[i--])\n while(k >= t and sign(det(ch[k - 1] - ch[k - 2], p[i] - ch[k - 1])) == -1) k--;\n ch.resize(k - 1);\n return ch;\n}\ntemplate < class T > T diameter2(const polygon< T >& p) {\n static_assert(is_floating_point_v< T >);\n int n = p.size(), is = 0, js = 0;\n for(int i = 1; i < n; i++) {\n if(sign(p[i].y - p[is].y) == +1) is = i;\n if(sign(p[i].y - p[js].y) == -1) js = i;\n }\n T dist_max = norm(p[is] - p[js]);\n int maxi = is, i = is, maxj = js, j = js;\n do {\n if(sign(det(p[(i + 1) % n] - p[i], p[(j + 1) % n] - p[j])) >= 0) j = (j + 1) % n; else i = (i + 1) % n;\n if(norm(p[i] - p[j]) > dist_max) {\n dist_max = norm(p[i] - p[j]);\n maxi = i, maxj = j;\n }\n } while(i != is or j != js);\n return dist_max;\n}\ntemplate < class T > double diameter(const polygon< T >& p) {\n static_assert(is_floating_point_v< T >);\n return std::sqrt(diameter2(p));\n}\n\ntemplate < class T > struct circle {\n point< T > p;\n T r;\n circle() = default;\n circle(point< T > p, T r) : p(p), r(r) {}\n};\ntemplate < class T > istream& operator>>(istream& is, circle< T >& c) { return is >> c.p >> c.r; }\ntemplate < class T > int intersect_cc(circle< T > c1, circle< T > c2) {\n if(c1.r < c2.r) std::swap(c1, c2);\n T d = abs(c1.p - c2.p);\n if(sign(c1.r + c2.r - d) == -1) return 4;\n if(equals(c1.r + c2.r, d)) return 3;\n if(sign(c1.r - c2.r - d) == -1) return 2;\n if(equals(c1.r - c2.r, d)) return 1;\n return 0;\n}\ntemplate < class T > std::pair<point< T >, point< T >> cross_point_cc(const circle< T >& c1, const circle< T >& c2) {\n T d = abs(c1.p - c2.p);\n T a = std::acos((c1.r * c1.r + d * d - c2.r * c2.r) / (2 * c1.r * d));\n T t = angle(c2.p - c1.p);\n point< T > p1 = c1.p + point< T >(std::cos(t + a), std::sin(t + a)) * c1.r;\n point< T > p2 = c1.p + point< T >(std::cos(t - a), std::sin(t - a)) * c1.r;\n return {p1, p2};\n}\n\nf64 solve(int W, int H, int N) {\n vector<point<f64>> P(2 * N);\n for(int i : rep(2 * N)) P[i] = in();\n vector<point<f64>> Q = {\n point<f64>(0, 0), point<f64>(0, H), point<f64>(W, 0), point<f64>(W, H)\n };\n const int M = 4;\n\n vector<f64> ys = {0.0, f64(H)};\n\n auto div = [&](line<f64> l) {\n int L = 0, R = 0;\n for(int i : rep(2 * N)) {\n line<f64> m(P[i], P[i] + point<f64>(0, 1));\n f64 y = cross_point_ll<f64>(l, m).y;\n if(y + 1e-8 <= P[i].y) L++;\n if(y - 1e-8 <= P[i].y) R++;\n }\n return L <= N and N <= R;\n };\n\n auto push = [&](line<f64> l) {\n if(div(l)) {\n line<f64> m(point<f64>(0, 0), point<f64>(0, H));\n f64 y = cross_point_ll<f64>(l, m).y;\n if(0.0 <= y and y <= H) ys.push_back(y);\n }\n };\n\n for(int i : rep(2 * N)) for(int j : rep(i + 1, 2 * N)) push(line<f64>(P[i], P[j]));\n for(int i : rep(2 * N)) for(int j : rep( M)) push(line<f64>(P[i], Q[j]));\n for(int i : rep( M)) for(int j : rep(i + 1, M)) push(line<f64>(Q[i], Q[j]));\n\n unique(ys);\n\n f64 ans = 0;\n const int n = ys.size();\n for(int k : rep(n - 1)) {\n f64 dL = ys[k + 1] - ys[k];\n f64 dR = [&] {\n f64 yL = (ys[k] + ys[k + 1]) / 2.0;\n f64 min = +1e18, max = -1e18;\n for(int i : rep(2 * N)) if(P[i].x > 0) {\n line<f64> l(P[i], point<f64>(0, yL));\n if(div(l)) {\n f64 yR = cross_point_ll<f64>(l, line<f64>(point<f64>(W, 0), point<f64>(W, H))).y;\n chmin(min, yR);\n chmax(max, yR);\n }\n }\n for(int i : rep(M)) if(Q[i].x > 0) {\n line<f64> l(Q[i], point<f64>(0, yL));\n if(div(l)) {\n f64 yR = cross_point_ll<f64>(l, line<f64>(point<f64>(W, 0), point<f64>(W, H))).y;\n chmin(min, yR);\n chmax(max, yR);\n }\n }\n chmax<f64>(min, 0.0);\n chmin<f64>(max, H);\n return std::max<f64>(0.0, max - min);\n }();\n ans += dL * dR;\n }\n return ans / f64(H * H);\n}\n\nint main() {\n while(true) {\n int W = in(), H = in(), N = in();\n if(make_tuple(W, H, N) == make_tuple(0, 0, 0)) return 0;\n printer::precision(20);\n print(solve(W, H, N));\n }\n}", "accuracy": 1, "time_ms": 5380, "memory_kb": 3528, "score_of_the_acc": -1.0938, "final_rank": 15 }, { "submission_id": "aoj_2256_8387672", "code_snippet": "// time complexity: O(N^3 log N)\n\n#include <bits/stdc++.h>\nusing namespace std;\n\ndouble subsolve(int N, const vector<double>& X, const vector<double>& Y) {\n\tvector<double> slopes = { 0.0, 1.0 };\n\tfor (int i = 0; i < N; i++) {\n\t\tfor (int j = 0; j < N; j++) {\n\t\t\tif (X[i] < X[j]) {\n\t\t\t\tdouble r = (Y[j] - Y[i]) / (X[j] - X[i]);\n\t\t\t\tif (0 < r && r < 1) {\n\t\t\t\t\tslopes.push_back(r);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tsort(slopes.begin(), slopes.end());\n\tdouble ans = 0.0;\n\tfor (int i = 0; i < int(slopes.size()) - 1; i++) {\n\t\tdouble sl = slopes[i];\n\t\tdouble sr = slopes[i + 1];\n\t\tdouble sm = (sl + sr) / 2;\n\t\tvector<int> perm(N);\n\t\tfor (int j = 0; j < N; j++) {\n\t\t\tperm[j] = j;\n\t\t}\n\t\tsort(perm.begin(), perm.end(), [&](int va, int vb) {\n\t\t\treturn Y[va] - sm * X[va] < Y[vb] - sm * X[vb];\n\t\t});\n\t\tauto calc = [&](int pos) -> double {\n\t\t\tdouble x = X[pos];\n\t\t\tdouble y = Y[pos];\n\t\t\tvector<double> borders = { sl, sr };\n\t\t\tif (x != 0.0 && sl < y / x && y / x < sr) {\n\t\t\t\tborders.push_back(y / x);\n\t\t\t}\n\t\t\tif (x != 1.0 && sl < (1.0 - y) / (1.0 - x) && (1.0 - y) / (1.0 - x) < sr) {\n\t\t\t\tborders.push_back((1.0 - y) / (1.0 - x));\n\t\t\t}\n\t\t\tsort(borders.begin(), borders.end());\n\t\t\tint m = borders.size();\n\t\t\tvector<double> w(m);\n\t\t\tfor (int i = 0; i < m; i++) {\n\t\t\t\tdouble s = borders[i];\n\t\t\t\tw[i] = min(max(y - s * x, 0.0), 1.0 - s);\n\t\t\t}\n\t\t\tdouble res = 0.0;\n\t\t\tfor (int i = 0; i < m - 1; i++) {\n\t\t\t\tres += (w[i] + w[i + 1]) * (borders[i + 1] - borders[i]) / 2.0;\n\t\t\t}\n\t\t\treturn res;\n\t\t};\n\t\t// cerr << \"! \" << sl << ' ' << sr << ' ' << calc(perm[N / 2]) << ' ' << calc(perm[N / 2 - 1]) << endl;\n\t\tans += calc(perm[N / 2]) - calc(perm[N / 2 - 1]);\n\t}\n\treturn ans;\n}\n\nint main() {\n\twhile (true) {\n\t\tint W, H, N;\n\t\tcin >> W >> H >> N;\n\t\tif (W == 0 && H == 0 && N == 0) {\n\t\t\tbreak;\n\t\t}\n\t\tN *= 2;\n\t\tvector<double> X(N), Y(N);\n\t\tfor (int i = 0; i < N; i++) {\n\t\t\tcin >> X[i] >> Y[i];\n\t\t\tX[i] /= W;\n\t\t\tY[i] /= H;\n\t\t}\n\t\tdouble ans1 = subsolve(N, X, Y);\n\t\tfor (int i = 0; i < N; i++) {\n\t\t\tY[i] = 1.0 - Y[i];\n\t\t}\n\t\tdouble ans2 = subsolve(N, X, Y);\n\t\tcout.precision(15);\n\t\tcout << fixed << ans1 + ans2 << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 560, "memory_kb": 3596, "score_of_the_acc": -0.3702, "final_rank": 5 }, { "submission_id": "aoj_2256_8196915", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing i64 = long long;\n\nint main() {\n cin.tie(nullptr)->sync_with_stdio(false);\n\n cout << fixed << setprecision(12);\n\n int W, H, n;\n \n auto solve = [&]() {\n n *= 2;\n\n vector<int> x(n), y(n);\n for (int i = 0; i < n; i++) {\n cin >> x[i] >> y[i];\n }\n x.push_back(W), y.push_back(0);\n x.push_back(W), y.push_back(H);\n\n vector<double> ys;\n\n for (int i = 0; i < n + 2; i++) {\n for (int j = i + 1; j < n + 2; j++) {\n if (x[i] == x[j]) {\n continue;\n }\n double p = 1.0 * (x[i] * y[j] - x[j] * y[i]) / (x[i] - x[j]);\n if (0 < p && p < H) {\n ys.push_back(p);\n }\n }\n }\n\n ys.push_back(0);\n ys.push_back(H);\n sort(ys.begin(), ys.end());\n\n double ans = 0;\n\n for (int i = 0; i < int(ys.size()) - 1; i++) {\n double yl = ys[i], yr = ys[i + 1];\n double ym = (yl + yr) / 2;\n\n vector<double> s(n);\n for (int j = 0; j < n; j++) {\n if (x[j] == 0) {\n s[j] = y[j] < ym ? -1e9 : 1e9; \n } else {\n s[j] = (y[j] - ym) / x[j];\n }\n }\n\n sort(s.begin(), s.end());\n\n double l = clamp<double>(W * s[n / 2 - 1] + ym, 0, H);\n double r = clamp<double>(W * s[n / 2] + ym, 0, H);\n\n ans += (r - l) * (yr - yl);\n }\n\n ans /= H * H;\n\n cout << ans << '\\n';\n };\n\n while (cin >> W >> H >> n && n != 0) {\n solve();\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 300, "memory_kb": 3692, "score_of_the_acc": -0.4145, "final_rank": 6 }, { "submission_id": "aoj_2256_7241091", "code_snippet": "//#define _GLIBCXX_DEBUG\n\n#include<bits/stdc++.h>\nusing namespace std;\n\n#define endl '\\n'\n#define lfs cout<<fixed<<setprecision(10)\n#define ALL(a) (a).begin(),(a).end()\n#define ALLR(a) (a).rbegin(),(a).rend()\n#define UNIQUE(a) (a).erase(unique((a).begin(),(a).end()),(a).end())\n#define spa << \" \" <<\n#define fi first\n#define se second\n#define MP make_pair\n#define MT make_tuple\n#define PB push_back\n#define EB emplace_back\n#define rep(i,n,m) for(ll i = (n); i < (ll)(m); i++)\n#define rrep(i,n,m) for(ll i = (ll)(m) - 1; i >= (ll)(n); i--)\nusing ll = long long;\nusing ld = long double;\nconst ll MOD1 = 1e9+7;\nconst ll MOD9 = 998244353;\nconst ll INF = 1e18;\nusing P = pair<ll, ll>;\ntemplate<typename T> using PQ = priority_queue<T>;\ntemplate<typename T> using QP = priority_queue<T,vector<T>,greater<T>>;\ntemplate<typename T1, typename T2>bool chmin(T1 &a,T2 b){if(a>b){a=b;return true;}else return false;}\ntemplate<typename T1, typename T2>bool chmax(T1 &a,T2 b){if(a<b){a=b;return true;}else return false;}\nll median(ll a,ll b, ll c){return a+b+c-max({a,b,c})-min({a,b,c});}\nvoid ans1(bool x){if(x) cout<<\"Yes\"<<endl;else cout<<\"No\"<<endl;}\nvoid ans2(bool x){if(x) cout<<\"YES\"<<endl;else cout<<\"NO\"<<endl;}\nvoid ans3(bool x){if(x) cout<<\"Yay!\"<<endl;else cout<<\":(\"<<endl;}\ntemplate<typename T1,typename T2>void ans(bool x,T1 y,T2 z){if(x)cout<<y<<endl;else cout<<z<<endl;} \ntemplate<typename T1,typename T2,typename T3>void anss(T1 x,T2 y,T3 z){ans(x!=y,x,z);}; \ntemplate<typename T>void debug(const T &v,ll h,ll w,string sv=\" \"){for(ll i=0;i<h;i++){cout<<v[i][0];for(ll j=1;j<w;j++)cout<<sv<<v[i][j];cout<<endl;}};\ntemplate<typename T>void debug(const T &v,ll n,string sv=\" \"){if(n!=0)cout<<v[0];for(ll i=1;i<n;i++)cout<<sv<<v[i];cout<<endl;};\ntemplate<typename T>void debug(const vector<T>&v){debug(v,v.size());}\ntemplate<typename T>void debug(const vector<vector<T>>&v){for(auto &vv:v)debug(vv,vv.size());}\ntemplate<typename T>void debug(stack<T> st){while(!st.empty()){cout<<st.top()<<\" \";st.pop();}cout<<endl;}\ntemplate<typename T>void debug(queue<T> st){while(!st.empty()){cout<<st.front()<<\" \";st.pop();}cout<<endl;}\ntemplate<typename T>void debug(deque<T> st){while(!st.empty()){cout<<st.front()<<\" \";st.pop_front();}cout<<endl;}\ntemplate<typename T>void debug(PQ<T> st){while(!st.empty()){cout<<st.top()<<\" \";st.pop();}cout<<endl;}\ntemplate<typename T>void debug(QP<T> st){while(!st.empty()){cout<<st.top()<<\" \";st.pop();}cout<<endl;}\ntemplate<typename T>void debug(const set<T>&v){for(auto z:v)cout<<z<<\" \";cout<<endl;}\ntemplate<typename T>void debug(const multiset<T>&v){for(auto z:v)cout<<z<<\" \";cout<<endl;}\ntemplate<typename T,size_t size>void debug(const array<T, size> &a){for(auto z:a)cout<<z<<\" \";cout<<endl;}\ntemplate<typename T,typename V>void debug(const map<T,V>&v){for(auto z:v)cout<<\"[\"<<z.first<<\"]=\"<<z.second<<\",\";cout<<endl;}\ntemplate<typename T>vector<vector<T>>vec(ll x, ll y, T w){vector<vector<T>>v(x,vector<T>(y,w));return v;}\nll gcd(ll x,ll y){ll r;while(y!=0&&(r=x%y)!=0){x=y;y=r;}return y==0?x:y;}\n//vector<ll>dx={1,-1,0,0,1,1,-1,-1};vector<ll>dy={0,0,1,-1,1,-1,1,-1};\ntemplate<typename T>vector<T> make_v(size_t a,T b){return vector<T>(a,b);}\ntemplate<typename... Ts>auto make_v(size_t a,Ts... ts){return vector<decltype(make_v(ts...))>(a,make_v(ts...));}\ntemplate<typename T1, typename T2>ostream &operator<<(ostream &os, const pair<T1, T2>&p){return os << p.first << \" \" << p.second;}\ntemplate<typename T>ostream &operator<<(ostream &os, const vector<T> &v){for(auto &z:v)os << z << \" \";cout<<\"|\"; return os;}\ntemplate<typename T>void rearrange(vector<int>&ord, vector<T>&v){\n auto tmp = v;\n for(int i=0;i<tmp.size();i++)v[i] = tmp[ord[i]];\n}\ntemplate<typename Head, typename... Tail>void rearrange(vector<int>&ord,Head&& head, Tail&&... tail){\n rearrange(ord, head);\n rearrange(ord, tail...);\n}\ntemplate<typename T> vector<int> ascend(const vector<T>&v){\n vector<int>ord(v.size());iota(ord.begin(),ord.end(),0);\n sort(ord.begin(),ord.end(),[&](int i,int j){return v[i]<v[j];});\n return ord;\n}\ntemplate<typename T> vector<int> descend(const vector<T>&v){\n vector<int>ord(v.size());iota(ord.begin(),ord.end(),0);\n sort(ord.begin(),ord.end(),[&](int i,int j){return v[i]>v[j];});\n return ord;\n}\ntemplate<typename T> vector<T> inv_perm(const vector<T>&ord){\n vector<T>inv(ord.size());\n for(int i=0;i<ord.size();i++)inv[ord[i]] = i;\n return inv;\n}\nll FLOOR(ll n,ll div){assert(div>0);return n>=0?n/div:(n-div+1)/div;}\nll CEIL(ll n,ll div){assert(div>0);return n>=0?(n+div-1)/div:n/div;}\nll digitsum(ll n){ll ret=0;while(n){ret+=n%10;n/=10;}return ret;}\nll modulo(ll n,ll d){return (n%d+d)%d;};\ntemplate<typename T>T min(const vector<T>&v){return *min_element(v.begin(),v.end());}\ntemplate<typename T>T max(const vector<T>&v){return *max_element(v.begin(),v.end());}\ntemplate<typename T>T acc(const vector<T>&v){return accumulate(v.begin(),v.end(),T(0));};\ntemplate<typename T>T reverse(const T &v){return T(v.rbegin(),v.rend());};\n//mt19937 mt(chrono::steady_clock::now().time_since_epoch().count());\nint popcount(ll x){return __builtin_popcountll(x);};\nint poplow(ll x){return __builtin_ctzll(x);};\nint pophigh(ll x){return 63 - __builtin_clzll(x);};\ntemplate<typename T>T poll(queue<T> &q){auto ret=q.front();q.pop();return ret;};\ntemplate<typename T>T poll(priority_queue<T> &q){auto ret=q.top();q.pop();return ret;};\ntemplate<typename T>T poll(QP<T> &q){auto ret=q.top();q.pop();return ret;};\ntemplate<typename T>T poll(stack<T> &s){auto ret=s.top();s.pop();return ret;};\nll MULT(ll x,ll y){if(LLONG_MAX/x<=y)return LLONG_MAX;return x*y;}\nll POW2(ll x, ll k){ll ret=1,mul=x;while(k){if(mul==LLONG_MAX)return LLONG_MAX;if(k&1)ret=MULT(ret,mul);mul=MULT(mul,mul);k>>=1;}return ret;}\nll POW(ll x, ll k){ll ret=1;for(int i=0;i<k;i++){if(LLONG_MAX/x<=ret)return LLONG_MAX;ret*=x;}return ret;}\ntemplate< typename T = int >\nstruct edge {\n int to;\n T cost;\n int id;\n edge():id(-1){};\n edge(int to, T cost = 1, int id = -1):to(to), cost(cost), id(id){}\n operator int() const { return to; }\n};\n\ntemplate<typename T>\nusing Graph = vector<vector<edge<T>>>;\ntemplate<typename T>\nGraph<T>revgraph(const Graph<T> &g){\n Graph<T>ret(g.size());\n for(int i=0;i<g.size();i++){\n for(auto e:g[i]){\n int to = e.to;\n e.to = i;\n ret[to].push_back(e);\n }\n }\n return ret;\n}\ntemplate<typename T>\nGraph<T> readGraph(int n,int m,int indexed=1,bool directed=false,bool weighted=false){\n Graph<T> ret(n);\n for(int es = 0; es < m; es++){\n int u,v;\n T w=1;\n cin>>u>>v;u-=indexed,v-=indexed;\n if(weighted)cin>>w;\n ret[u].emplace_back(v,w,es);\n if(!directed)ret[v].emplace_back(u,w,es);\n }\n return ret;\n}\ntemplate<typename T>\nGraph<T> readParent(int n,int indexed=1,bool directed=true){\n Graph<T>ret(n);\n for(int i=1;i<n;i++){\n int p;cin>>p;\n p-=indexed;\n ret[p].emplace_back(i);\n if(!directed)ret[i].emplace_back(p);\n }\n return ret;\n}\n\ntemplate <typename F>\nclass\n#if defined(__has_cpp_attribute) && __has_cpp_attribute(nodiscard)\n [[nodiscard]]\n#endif // defined(__has_cpp_attribute) && __has_cpp_attribute(nodiscard)\nFixPoint final : private F\n{\npublic:\n template <typename G>\n explicit constexpr FixPoint(G&& g) noexcept\n : F{std::forward<G>(g)}\n {}\n\n template <typename... Args>\n constexpr decltype(auto)\n operator()(Args&&... args) const\n#if !defined(__GNUC__) || defined(__clang__) || __GNUC__ >= 9\n noexcept(noexcept(F::operator()(std::declval<FixPoint>(), std::declval<Args>()...)))\n#endif // !defined(__GNUC__) || defined(__clang__) || __GNUC__ >= 9\n {\n return F::operator()(*this, std::forward<Args>(args)...);\n }\n}; // class FixPoint\n\n\n#if defined(__cpp_deduction_guides)\ntemplate <typename F>\nFixPoint(F&&)\n -> FixPoint<std::decay_t<F>>;\n#endif // defined(__cpp_deduction_guides)\n\n\nnamespace\n{\n template <typename F>\n#if !defined(__has_cpp_attribute) || !__has_cpp_attribute(nodiscard)\n# if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ >= 4)\n __attribute__((warn_unused_result))\n# elif defined(_MSC_VER) && _MSC_VER >= 1700 && defined(_Check_return_)\n _Check_return_\n# endif // defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ >= 4)\n#endif // !defined(__has_cpp_attribute) || !__has_cpp_attribute(nodiscard)\n inline constexpr decltype(auto)\n makeFixPoint(F&& f) noexcept\n{\n return FixPoint<std::decay_t<F>>{std::forward<std::decay_t<F>>(f)};\n}\n} // namespace\n\nusing Real = long double;\nusing Point = complex<Real>;\nconst Real EPS = 1e-10, PI = acos((Real)(-1.0));\n\ninline bool eq(Real a, Real b) { return fabs(b - a) < EPS; }\ninline bool eq(Point a, Point b) { return fabs(b - a) < EPS; }\n\nPoint operator*(const Point &p, const Real &d) {\n return Point(real(p) * d, imag(p) * d);\n}\n\nistream &operator>>(istream &is, Point &p) {\n Real a, b;\n is >> a >> b;\n p = Point(a, b);\n return is;\n}\n\nostream &operator<<(ostream &os, Point &p) {\n return os << fixed << setprecision(10) << p.real() << \" \" << p.imag();\n}\n\nPoint rotate(Real theta, const Point &p) {\n return Point(cos(theta) * p.real() - sin(theta) * p.imag(), sin(theta) * p.real() + cos(theta) * p.imag());\n}\n\nnamespace std {\n bool operator<(const Point& a, const Point& b) {\n return !eq(a.real(), b.real()) ? a.real() < b.real() : a.imag() < b.imag();\n }\n}\n\nstruct Line {\n Point a, b;\n Line() = default;\n Line(Point a, Point b): a(a), b(b) {}\n\n friend ostream &operator<<(ostream &os, Line &p) {\n return os << p.a << \" to \" << p.b;\n }\n\n friend istream &operator>>(istream &is, Line &a) {\n return is >> a.a >> a.b;\n }\n};\n\nstruct Circle {\n Point c;\n Real r;\n Circle() = default;\n Circle(Point c, Real r): c(c), r(r) {}\n\n friend ostream &operator<<(ostream &os, Circle &c) {\n return os << c.c << \": \" << c.r;\n }\n\n friend istream &operator>>(istream &is, Circle &c) {\n return is >> c.c >> c.r;\n }\n};\n\nReal cross(const Point &a, const Point &b) {\n return real(a) * imag(b) - real(b) * imag(a);\n}\n\nReal dot(const Point &a, const Point &b) {\n return real(a) * real(b) + imag(a) * imag(b);\n}\n\nstruct Segment : Line {\n Segment() = default;\n Segment(Point a, Point b) : Line(a, b) {}\n};\n\nint ccw(const Point &a, Point b, Point c) {\n b = b - a, c = c - a;\n if (cross(b, c) > EPS) return +1; // COUNTER_CLOCKWISE\n if (cross(b, c) < -EPS) return -1; // CLOCKWISE\n if (dot(b, c) < 0) return +2; // ONLINE_BACK\n if (norm(b) < norm(c)) return -2; // ONLINE_FRONT\n return 0; // ON_SEGMENT\n}\n\nbool intersect(const Segment &s, const Point &p) {\n return ccw(s.a, s.b, p) == 0;\n}\n\nbool intersect(const Segment &s, const Segment&t) {\n return ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 && ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0;\n}\n\nbool intersect(const Line &l, const Segment& s) {\n return cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < EPS;\n}\n\nint intersect(const Circle &c, const Circle &d) {\n Real dis = fabs(c.c - d.c);\n\n if (dis > c.r + d.r + EPS) return 4;\n else if (eq(dis, c.r + d.r)) return 3;\n else if (eq(dis, abs(c.r - d.r))) return 1;\n else if (dis + min(c.r, d.r) < max(c.r, d.r) - EPS) return 0;\n else return 2;\n}\n\nbool parallel(const Line &a, const Line &b) {\n return abs(cross(a.b - a.a, b.b - b.a)) < EPS;\n}\n\nbool orthogonal(const Line &a, const Line &b) {\n return abs(dot(a.b - a.a, b.b - b.a)) < EPS;\n}\n\nPoint projection(const Line& s, const Point &p) {\n double t = dot(p - s.a, s.b - s.a) / norm(s.b - s. a);\n return s.a + (s.b - s.a) * t;\n}\n\nPoint projection(const Segment& l, const Point &p) {\n return projection(Line(l), p);\n}\n\nPoint reflection(const Line &l, const Point &p) {\n Point r = projection(l, p);\n return p + (r - p) * 2;\n}\n\nReal distance(const Line &l, const Point &p) {\n Point r = projection(l, p);\n return fabs(p - r);\n}\n\nReal distance(const Segment&s, const Point &p) {\n Point r = projection(s, p);\n if (intersect(s, r)) return abs(r - p);\n return min(abs(s.a - p), abs(s.b - p));\n}\n\nReal distance(const Segment& a, const Segment& b) {\n if (intersect(a, b)) return 0;\n return min({\n distance(a, b.a),\n distance(a, b.b),\n distance(b, a.a),\n distance(b, a.b)\n });\n}\n\nPoint crosspoint(const Line &l, const Line &m) {\n Real A = cross(l.b - l.a, m.b - m.a);\n Real B = cross(l.b - l.a, l.b - m.a);\n if (eq(abs(A), 0.0) && eq(abs(B), 0.0)) return m.a;\n return m.a + (m.b - m.a) * B / A;\n}\n\nPoint crosspoint(const Segment& l, const Segment& m) {\n return crosspoint(Line(l), Line(m));\n}\n\nLine bisector_of_angle(const Point& a, const Point& b, const Point& c) {\n Real d_ab = fabs(a - b);\n Real d_bc = fabs(b - c);\n Point c_ = b + (c - b) * d_ab / d_bc;\n\n return Line(b, (a + c_) * 0.5);\n}\n\nLine bisector(const Point& a, const Point& b) {\n Point c = (a + b) * 0.5;\n Point d = c + rotate(PI / 2, b - c);\n\n return Line(c, d);\n}\n\nint intersect(const Circle &c, const Line &l) {\n Real d = distance(l, c.c);\n\n if (d > c.r + EPS) return 0;\n if (eq(d, c.r)) return 1;\n return 2;\n}\n\npair<Point, Point> crosspoint(const Circle& c, const Line& l) {\n Real d = distance(l, c.c);\n\n Point p = projection(l, c.c);\n if (intersect(c, l) == 1) return { p, p };\n\n Real d2 = sqrt(c.r * c.r - d * d);\n Point v = (l.a - l.b) * (1.0 / fabs(l.a - l.b));\n\n return { p + d2 * v, p - d2 * v };\n}\n\npair<Point, Point> crosspoint(const Circle &c1, const Circle &c2) {\n Real d = fabs(c1.c - c2.c);\n Real x1 = (c1.r * c1.r + d * d - c2.r * c2.r) / 2 / d;\n Real y = sqrt(c1.r * c1.r - x1 * x1);\n\n Point base = (c2.c * x1 + c1.c * (d - x1)) * (1.0 / d);\n Point v = rotate(PI / 2, c1.c - base);\n v = v / fabs(v);\n\n return { base + v * y, base - v * y };\n}\n\npair<Point, Point> tangent(const Circle &c, const Point &p) {\n Real d = fabs(c.c - p);\n Real d2 = sqrt(d * d - c.r * c.r);\n Real a = acos(d2 / d);\n\n\n Point q = c.c - p;\n q = q / fabs(q);\n\n// cout << d << \" \" << d2 << \" \" << a << \" \" << q << endl;\n return {\n p + rotate(a, q) * d2,\n p + rotate(-a, q) * d2\n };\n}\n\nvector<Segment> common_tangent(Circle c1, Circle c2) {\n int isect = intersect(c1, c2);\n Real d = fabs(c1.c - c2.c);\n Point v = (c2.c - c1.c) / d;\n\n vector<Segment> res;\n\n if (isect == 0) {\n return {};\n } else if (isect == 1) {\n auto [p1, p2] = crosspoint(c1, Line(c1.c, c2.c));\n if (eq(fabs(p2 - c2.c), c2.r)) swap(p1, p2);\n\n Point v = p1 + rotate(PI/2, c2.c - p1);\n return { Segment(p1, v) };\n } else if (isect == 2) {\n } else if (isect == 3) {\n auto [p1, p2] = crosspoint(c1, c2);\n res.emplace_back(p1, rotate(PI / 2, c1.c - p1) + p1);\n } else {\n Real a = d * c1.r / (c1.r + c2.r);\n Point p = c1.c + v * a;\n\n Real d1 = sqrt(a * a - c1.r * c1.r), d2 = sqrt(norm(p - c2.c) - c2.r * c2.r);\n Real theta = acos(d1 / a);\n\n// cout << d << endl;\n// cout << a << \" \" << d1 << \" \" << d2 << \" \" << theta << \": \" << p << endl;\n\n Point e = (c1.c - p) * (1.0 / fabs(c1.c - p));\n Point e1 = rotate(theta, e), e2 = rotate(-theta, e);\n\n res.emplace_back(p + e1 * d1, p - e1 * d2);\n res.emplace_back(p + e2 * d1, p - e2 * d2);\n }\n\n if (eq(c1.r, c2.r)) {\n Point e = rotate(PI / 2, v);\n res.push_back(Segment(c1.c + e * c1.r, c2.c + e * c1.r));\n res.push_back(Segment(c1.c - e * c1.r, c2.c - e * c1.r));\n return res;\n }\n\n if (c1.r > c2.r) swap(c1, c2);\n\n v = c1.c - c2.c;\n Point p = v * (1.0 / (c2.r - c1.r)) * c2.r + c2.c;\n\n auto [q1, q2] = tangent(c1, p);\n res.emplace_back(q1, crosspoint(c2, Line(p, q1)).first);\n res.emplace_back(q2, crosspoint(c2, Line(p, q2)).first);\n\n return res;\n}\n\nusing Polygon = vector<Point>;\n\nistream& operator>>(istream& is, Polygon &p) {\n for (int i = 0; i < p.size(); i++) is >> p[i];\n return is;\n}\n\nostream& operator<<(ostream& os, Polygon &p) {\n for (int i = 0; i < p.size(); i++) os << p[i];\n return os;\n}\n\nReal area(const Polygon& p) {\n Real res = 0;\n for (int i = 0; i < p.size(); i++) {\n res += cross(p[i], p[(i + 1) % p.size()]);\n }\n return abs(res)/2;\n}\n\nint contains(Polygon g,Point p){\n int n=g.size();\n bool x=false;\n for(int i=0;i<n;i++){\n Point a=g[i]-p,b=g[(i+1)%n]-p;\n if(fabs(cross(a,b)) < EPS and dot(a,b) < EPS) return 1;\n if(a.imag()>b.imag()) swap(a,b);\n if(a.imag() < EPS and EPS < b.imag() and cross(a,b) > EPS ) {\n// cout << a << \" --- \" << b << \": \" << cross(a, b) << endl;\n x = !x;\n }\n }\n return (x?2:0);\n}\n\nPolygon convex_hull(Polygon Q) {\n sort(ALL(Q));\n\n Polygon upper({Q[0]}), lower({Q[0]});\n\n rep(i, 1, Q.size()) {\n while (upper.size() >= 2) {\n int _ccw = ccw(upper[upper.size() - 2], upper[upper.size() - 1], Q[i]);\n if (_ccw == 1) {\n upper.pop_back();\n } else {\n break;\n }\n }\n if (upper.size() <= 1) {\n upper.push_back(Q[i]);\n } else if (upper.size() > 1) {\n int _ccw = ccw(upper[upper.size() - 2], upper[upper.size() - 1], Q[i]);\n if (_ccw == -1 || _ccw == -2) {\n upper.push_back(Q[i]);\n }\n }\n }\n rep(i, 1, Q.size()) {\n while (lower.size() >= 2) {\n int _ccw = ccw(lower[lower.size() - 2], lower[lower.size() - 1], Q[i]);\n if (_ccw == -1) {\n lower.pop_back();\n } else {\n break;\n }\n }\n\n if (lower.size() <= 1) {\n lower.push_back(Q[i]);\n } else if (lower.size() > 1) {\n int _ccw = ccw(lower[lower.size() - 2], lower[lower.size() - 1], Q[i]);\n if (_ccw == 1 || _ccw == -2) {\n lower.push_back(Q[i]);\n }\n }\n }\n\n Polygon ret;\n\n rep(i, 0, lower.size()) {\n ret.push_back(lower[i]);\n// cout << lower[i] << endl;\n }\n\n// cout << \" | \" << endl;\n\n rrep(i, 0, upper.size()) {\n if (!eq(lower[0], upper[i]) && !eq(lower.back(), upper[i]))\n ret.push_back(upper[i]);\n// cout << upper[i] << endl;\n }\n\n int start = 0;\n\n// cout << ret.size() << endl;\n rep(i, 0, ret.size()) {\n if (ret[i].imag() < ret[start].imag() || (eq(ret[i].imag(), ret[start].imag()) && ret[i].real() < ret[start].real())) {\n start = i;\n }\n }\n\n cout << ret.size() << endl;\n rep(i, 0, ret.size()) {\n auto p = ret[(i + start) % ret.size()];\n cout << (int)p.real() << \" \" << (int)p.imag() << endl;\n }\n\n return ret;\n}\n\nReal closest_pair(vector<Point> &pts, int l, int r) {\n int m = (l + r) / 2;\n if (r - l <= 1) {\n return INF;\n } else if (r - l == 2) {\n if (imag(pts[l]) > imag(pts[l + 1])) swap(pts[l], pts[l + 1]);\n return fabs(pts[l] - pts[l + 1]);\n }\n\n Real mid = pts[m].real();\n Real d = min(closest_pair(pts, l, m), closest_pair(pts, m, r));\n\n inplace_merge(pts.begin() + l, pts.begin() + m, pts.begin() + r, [](const Point &a, const Point& b) {\n return a.imag() < b.imag();\n });\n\n vector<Point> ret;\n\n for (int i = l; i < r; i++) {\n if (fabs(pts[i].real() - mid) >= d) continue;\n\n for (int j = ret.size() - 1; j >= 0; j--) {\n if (fabs(imag(ret[j]) - imag(pts[i])) >= d) break;\n chmin(d, fabs(ret[j] - pts[i]));\n }\n\n ret.emplace_back(pts[i]);\n }\n\n return d;\n}\n\nint main() {\n cout << fixed << setprecision(10);\n\n Real W, H;\n int N;\n while (cin >> W >> H >> N && N) {\n vector<Point> pts(2 * N);\n for (auto &p : pts) cin >> p;\n N += 2;\n int n = 2 * N;\n\n // ccw > 0 => 0 < theta <= pi\n\n Segment lft(Point(0, 0), Point(0, H)), rht(Point(W, 0), Point(W, H));\n pts.emplace_back(lft.a);\n pts.emplace_back(lft.b);\n pts.emplace_back(rht.a);\n pts.emplace_back(rht.b);\n Real res = 0;\n\n vector<Segment> half;\n auto is_half_line = [&](const Point &a, const Point &b) {\n int cnt[2] = {};\n int on = 0;\n rep(k, 0, n - 4) {\n int c= ccw(a, b, pts[k]);\n if (c != 0) cnt[c > 0]++;\n else on++;\n }\n\n return abs(cnt[0] - cnt[1]) <= on;\n };\n rep(i, 0, n) {\n rep(j, i + 1, n) {\n Line l = Line(pts[i], pts[j]);\n if (is_half_line(pts[i], pts[j])) {\n if (intersect(l, lft) && intersect(l, rht)) {\n half.emplace_back(crosspoint(l, lft), crosspoint(l, rht));\n }\n }\n }\n }\n\n sort(ALL(half), [&](const Segment& a, const Segment &b) {\n return a.a < b.a;\n });\n\n rep(i, 0, half.size() - 1) {\n Point m((half[i].a + half[i + 1].a) * 0.5);\n\n// cout << m << endl;\n vector<Real> ry;\n rep(j, 0, n) {\n if (is_half_line(m, pts[j]) && intersect(Line(m, pts[j]), rht)) {\n// ry.push_back(clamp(crosspoint(Line(m, pts[j]), rht).imag(), (Real)0.0, H));\n ry.push_back(crosspoint(Line(m, pts[j]), rht).imag());\n// cout << \" ---- \" << pts[j] << endl;\n }\n }\n sort(ALL(ry));\n if (ry.size() >= 2) {\n// cout << ry.back() spa ry.front() << \" --- \" << half[i + 1].a.imag() << \" \" << imag(half[i].a) << endl;\n res += (ry.back() - ry.front()) * (half[i + 1].a.imag() - half[i].a.imag());\n }\n }\n cout << res / H / H << endl;\n }\n}", "accuracy": 1, "time_ms": 2040, "memory_kb": 3604, "score_of_the_acc": -0.6184, "final_rank": 11 }, { "submission_id": "aoj_2256_6665096", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)\n#define revrep(i, t, s) for (int i = (int)(t)-1; i >= (int)(s); --i)\n#define all(x) begin(x), end(x)\ntemplate <typename T> bool chmax(T& a, const T& b) { return a < b ? (a = b, 1) : 0; }\ntemplate <typename T> bool chmin(T& a, const T& b) { return a > b ? (a = b, 1) : 0; }\n\nconstexpr double eps = 1e-10;\n\ntemplate <typename T>\nstruct Fraction {\n T num, den;\n Fraction(T num = 0, T den = 1) : num(num), den(den) { reduce(); }\n void reduce() {\n T g = std::gcd(num, den);\n num /= g, den /= g;\n if (den < 0) num = -num, den = -den;\n }\n Fraction& operator+=(const Fraction& rhs) {\n num = num*rhs.den + rhs.num*den;\n den *= rhs.den;\n reduce();\n return *this;\n }\n Fraction& operator-=(const Fraction& rhs) {\n num = num*rhs.den - rhs.num*den;\n den *= rhs.den;\n reduce();\n return *this;\n }\n Fraction& operator*=(const Fraction& rhs) {\n num *= rhs.num;\n den *= rhs.den;\n reduce();\n return *this;\n }\n Fraction& operator/=(const Fraction& rhs) {\n num *= rhs.den;\n den *= rhs.num;\n reduce();\n return *this;\n }\n Fraction operator-() const { return Fraction(-num, den); }\n Fraction operator+(const Fraction& rhs) const { return Fraction(*this) += rhs; }\n Fraction operator-(const Fraction& rhs) const { return Fraction(*this) -= rhs; }\n Fraction operator*(const Fraction& rhs) const { return Fraction(*this) *= rhs; }\n Fraction operator/(const Fraction& rhs) const { return Fraction(*this) /= rhs; }\n bool operator==(const Fraction& rhs) const { return num*rhs.den == rhs.num*den; }\n bool operator!=(const Fraction& rhs) const { return !(*this == rhs); }\n bool operator<(const Fraction& rhs) const { return num*rhs.den < rhs.num*den; };\n bool operator>(const Fraction& rhs) const { return num*rhs.den > rhs.num*den; };\n bool operator<=(const Fraction& rhs) const { return !(*this > rhs); };\n bool operator>=(const Fraction& rhs) const { return !(*this < rhs); };\n friend ostream &operator<<(ostream& os, const Fraction& p) { return os << p.num << \"/\" << p.den; }\n};\n\n\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << fixed << setprecision(15);\n\n while (true) {\n int W, H, N;\n cin >> W >> H >> N;\n if (W == 0) break;\n vector<pair<int, int>> pts(2*N);\n for (auto& x : pts) cin >> x.first >> x.second;\n sort(all(pts));\n pts.insert(pts.begin(), {0, 0});\n pts.insert(pts.begin()+1, {0, H});\n pts.push_back({W, 0});\n pts.push_back({W, H});\n vector<Fraction<int>> slopes;\n rep(i,0,pts.size()) rep(j,i+1,pts.size()) {\n if (pts[i].first == pts[j].first) continue;\n Fraction<int> slope(pts[j].second - pts[i].second, pts[j].first - pts[i].first);\n if (slope < Fraction(-H, W) || Fraction(H, W) < slope) continue;\n slopes.push_back(slope);\n }\n sort(all(slopes));\n slopes.erase(unique(all(slopes)), slopes.end());\n reverse(all(slopes));\n\n auto calc = [&](int n) {\n double l1 = -1, r1 = -1;\n double ans = 0;\n for (auto& m : slopes) {\n vector<double> ys;\n rep(i,2,pts.size()-2) {\n ys.push_back(pts[i].second - 1.0*m.num/m.den*pts[i].first);\n }\n sort(all(ys));\n double l2 = ys[n];\n double r2 = l2 + 1.0*m.num/m.den*W;\n if (max(l2, r2) > H) {\n double d = max(l2, r2) - H;\n l2 -= d;\n r2 -= d;\n }\n if (min(l2, r2) < -eps) {\n continue;\n }\n\n if (l1 != -1 && l1+eps < l2) {\n ans += r1*(l2-l1) + l1*(r1-r2) - (r1-r2)*(l1+l2)/2;\n }\n l1 = l2;\n r1 = r2;\n }\n ans /= H*H;\n return ans;\n };\n\n cout << calc(N) - calc(N-1) << endl;\n }\n}", "accuracy": 1, "time_ms": 810, "memory_kb": 3656, "score_of_the_acc": -0.465, "final_rank": 7 }, { "submission_id": "aoj_2256_6009586", "code_snippet": "#include<bits/stdc++.h> \nusing namespace std;\nusing ll=long long int;\n//using Int=__int128;\n#define ALL(A) A.begin(),A.end()\ntemplate<typename T1,typename T2> bool chmin(T1 &a,T2 b){if(a<=b)return 0; a=b; return 1;}\ntemplate<typename T1,typename T2> bool chmax(T1 &a,T2 b){if(a>=b)return 0; a=b; return 1;}\ntemplate<typename T> constexpr int bitUP(T x,int a){return (x>>a)&1;}\n//→ ↓ ← ↑ \nint dh[4]={0,1,0,-1}, dw[4]={1,0,-1,0};\n//右上から時計回り\n//int dh[8]={-1,0,1,1,1,0,-1,-1}, dw[8]={1,1,1,0,-1,-1,-1,0};\n//long double EPS = 1e-6;\n//long double PI = acos(-1);\n//const ll INF=(1LL<<62);\nconst int MAX=(1<<30);\nconstexpr ll MOD=1000000000+7;\n//constexpr ll MOD=998244353;\n\n\ninline void bin101(){\n ios::sync_with_stdio(false);\n cin.tie(0);\n cout << fixed << setprecision(20);\n}\n\nusing Graph=vector<vector<int>>;\n\ntemplate<typename T >\nstruct edge {\n\tint to;\n\tT cost;\n\tedge()=default;\n\tedge(int to, T cost) : to(to), cost(cost) {}\n\n};\ntemplate<typename T>\nusing WeightGraph=vector<vector<edge<T>>>;\n\n//要素数n 初期値x\ntemplate<typename T>\ninline vector<T> vmake(size_t n,T x){\n\treturn vector<T>(n,x);\n}\n\n//a,b,c,x data[a][b][c] 初期値x\ntemplate<typename... Args>\nauto vmake(size_t n,Args... args){\n\tauto v=vmake(args...);\n\treturn vector<decltype(v)>(n,move(v));\n}\n\n////////////////////////////\n// マクロや型\n////////////////////////////\n\nusing DD=long double;\n\nconst DD EPS=1e-9;\nconst DD INF=1e50;\nconst DD PI=acosl(-1.0);\n#define EQ(a,b) (abs( (a) - (b) )<EPS) //a==b\n#define LS(a,b) ( (a)+EPS<(b) ) //a<b\n#define GR(a,b) ( (a)>(b)+EPS ) //a>b\n#define LE(a,b) ( (a)<(b)+EPS ) //a<=b\n#define GE(a,b) ( (a)>(b)-EPS ) //a>=b\n\n#define X real()\n#define Y imag()\n\n\n//点\nusing Point=complex<DD>;\nistream &operator>>(istream &is, Point &p) {\n DD x,y;\n is>>x>>y;\n p=Point(x,y);\n return is;\n}\n\n//点a→点bの線分\n//aとbが等しい時、色々バグるから注意!\nstruct Segment{\n Point a,b;\n Segment()=default;\n Segment(Point a,Point b) :a(a),b(b){}\n Segment(DD ax,DD ay,DD bx,DD by):a(ax,ay),b(bx,by){}\n Segment(DD r,Point a) :a(a),b(a+polar((DD)1.0,r)){} \n};\nusing Line=Segment;\n\n//円\nstruct Circle{\n Point p;\n DD r;\n Circle()=default;\n Circle(Point p,DD r):p(p),r(r){}\n};\n\nusing Polygon=vector<Point>;\n\n////////////////////////////\n// 基本計算\n////////////////////////////\n\n//度→ラジアン\ninline DD torad(const DD deg){return deg*PI/180;}\n//ラジアン→度\ninline DD todeg(const DD rad){return rad*180/PI;}\n//内積 |a||b|cosθ\ninline DD dot(const Point &a,const Point &b){return (conj(a)*b).X;}\n//外積 |a||b|sinθ\ninline DD cross(const Point &a,const Point &b){return (conj(a)*b).Y;}\n//ベクトルpを反時計回りにtheta(ラジアン)回転\nPoint rotate(const Point &p,const DD theta){return p*Point(cos(theta),sin(theta));}\n\n//余弦定理 cos(角度abc(ラジアン))を返す\n//長さの対応に注意 ab:辺abの長さ\n//verify:https://codeforces.com/gym/102056/problem/F\nDD cosine_formula(const DD ab,const DD bc,const DD ca){\n return (ab*ab+bc*bc-ca*ca)/(2*ab*bc);\n}\n\ninline bool xy_sort(const Point &a,const Point &b){\n if(a.X+EPS<b.X) return true;\n if(EQ(a.X,b.X) && a.Y+EPS<b.Y) return true;\n return false;\n}\ninline bool yx_sort(const Point &a,const Point &b){\n if(a.Y+EPS<b.Y) return true;\n if(EQ(a.Y,b.Y) && a.X+EPS<b.X) return true;\n return false;\n}\nnamespace std{\n inline bool operator<(const Point &a,const Point &b)noexcept{\n return xy_sort(a,b);\n }\n}\n//DDの比較関数\n//map<DD,vector<pii>,DDcom>\nstruct DDcom{\n bool operator()(const DD a, const DD b) const noexcept {\n return a+EPS<b;\n }\n};\n\n\n////////////////////////////\n// 平行や直交\n////////////////////////////\n\nbool isOrthogonal(const Point &a,const Point &b){\n return EQ(dot(a,b),0.0);\n}\n//verify:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_A\nbool isOrthogonal(const Segment &s,const Segment &t){\n return EQ(dot(s.a-s.b,t.a-t.b),0.0);\n}\nbool isParallel(const Point &a,const Point &b){\n return EQ(cross(a,b),0.0);\n}\n//verify:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_A\nbool isParallel(const Segment &s,const Segment &t){\n return EQ(cross(s.a-s.b,t.a-t.b),0);\n}\n//線分a→bに対して点cがどの位置にあるか\n//反時計回り:1 時計回り:-1 直線上(a,b,c:-2 a,c,b:0 c,a,b:2)\n//verify:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_C\n//線分a→bの端点にcがあるなら0と判定されるはず\nint ccw(const Point &a,Point b,Point c){\n if(EQ(abs(b-c),0) or EQ(abs(a-c),0)) return 0;\n b-=a;c-=a;\n if(cross(b,c)>EPS) return 1;\n if(cross(b,c)<-EPS) return -1;\n if(dot(b,c)<-EPS) return 2;\n if(LS(norm(b),norm(c))) return -2;\n return 0;\n}\n\n////////////////////////////\n// 射影と反射\n////////////////////////////\n\n//射影\n//直線lに点pから垂線を引いたときの交点\n//verify:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_A\nPoint project(const Point &p,const Line &l){\n Point base=l.b-l.a;\n DD r=dot(p-l.a,base)/norm(base);\n return l.a+base*r;\n}\n//反射\n//直線lに対して点pと対称な点\n//verify:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_B\nPoint reflect(const Point &p,const Line &l){\n return p+(project(p,l)-p)*(DD)2.0;\n}\n//反射\n//直線lに対して線分sと対称な線分(直線でも使える)\n//verify:https://onlinejudge.u-aizu.ac.jp/solutions/problem/1171/review/6009447/bin101/C++17\nSegment reflect(const Segment &s,const Line &l){\n return Segment(reflect(s.a,l) , reflect(s.b,l));\n}\n\n////////////////////////////\n// 点との距離\n////////////////////////////\n\n//点と直線の距離\nDD DistPL(const Point &p,const Line &l){\n return abs(cross(l.b-l.a,p-l.a))/abs(l.b-l.a);\n}\n//点と線分の距離\nDD DistPS(const Point &p,const Segment &s){\n if( dot(s.b-s.a,p-s.a)<0.0 ) return abs(p-s.a);\n if( dot(s.a-s.b,p-s.b)<0.0 ) return abs(p-s.b);\n return DistPL(p,s);\n}\n\n////////////////////////////\n// 線分と直線\n////////////////////////////\n\n//線分a-b,c-dは交差するか?\n//接する場合も交差すると判定\n//接する場合は交差しないとするなら<=を<に変換\nbool intersect(const Point &a,const Point &b,const Point &c,const Point &d){\n return(ccw(a,b,c)*ccw(a,b,d)<=0 && ccw(c,d,a)*ccw(c,d,b)<=0);\n}\n//線分s,tは交差するか?\n//接する場合も交差すると判定\n//verify:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_B&lang=ja\n//接する場合は交差しないとするならintersectの<=を<に変換\nbool intersect(const Segment &s,const Segment &t){\n return intersect(s.a,s.b,t.a,t.b);\n}\n// 2直線の交点\n// 線分の場合はintersectをしてから使う\n//verify:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_C&lang=ja\nPoint crossPoint(const Line &s,const Line &t){\n Point base=t.b-t.a;\n DD d1=cross(base,t.a-s.a);\n DD d2=cross(base,s.b-s.a);\n if(EQ(d1,0) and EQ(d2,0)) return s.a; //同じ直線\n if(EQ(d2,0)) assert(false); //交点がない\n return s.a+d1/d2*(s.b-s.a);\n}\n//線分と線分の距離\n//verify:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_D&lang=ja\nDD Dist(const Segment &s,const Segment &t){\n if(intersect(s,t)) return 0.0;\n return min(min(DistPS(t.a,s),DistPS(t.b,s)),min(DistPS(s.a,t),DistPS(s.b,t)) );\n}\nbool issame(const Line &l,const Line &m){\n if (GR(abs(cross(m.a - l.a, l.b - l.a)),0) ) return false;\n if (GR(abs(cross(m.b - l.a, l.b - l.a)),0) ) return false;\n return true;\n}\n\n////////////////////////////\n// 円\n////////////////////////////\n//直線lと円Cの交点\n//l.aにより近い方がindexが小さい\n//veirfy:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_D\nvector<Point> crossPointLC(const Line &l,const Circle &c){\n vector<Point> ret;\n if(GR(DistPL(c.p,l),c.r)) return ret; //交点がないとき、空ベクトルを返す\n Point pr=project(c.p,l);\n Point e=(l.b-l.a)/(abs(l.b-l.a));\n DD base=sqrt(c.r*c.r-norm(pr-c.p));\n ret.push_back(pr-e*base);\n ret.push_back(pr+e*base);\n if(EQ(DistPL(c.p,l),c.r)) ret.pop_back(); //接するとき\n return ret;\n}\n//線分sと円cの交点\n//交点がないときは空ベクトルを返す\n//線分の端が交わる場合も交点とみなす\n//s.aにより近い方がindexが小さい\nvector<Point> crossPointSC(const Segment &s,const Circle &c){\n vector<Point> ret;\n if(DistPS(c.p,s)>c.r+EPS) return ret;\n auto koho=crossPointLC(s,c);\n for(auto p:koho){\n if(ccw(s.a,s.b,p)==0 or EQ(abs(p-s.a),0) or EQ(abs(p-s.b),0)) ret.push_back(p);\n }\n return ret;\n}\n\n//円aと円bの共通接線の数\n//離れている:4 外接:3 交わる:2 内接:1 内包:0\n//verify:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_A\nint intersect(const Circle &a,const Circle &b){\n DD d=abs(a.p-b.p);\n if(GR(d,a.r+b.r)) return 4;\n if(EQ(d,a.r+b.r)) return 3;\n if(EQ(d,abs(a.r-b.r))) return 1;\n if(LS(d,abs(a.r-b.r))) return 0;\n return 2;\n}\n\n//円aと円bの交点\n//交点がないときは空ベクトルを返す\n//verify:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_E\nvector<Point> crossPoint(const Circle &a,const Circle &b){\n vector<Point> ret;\n if(GR(abs(a.p-b.p),a.r+b.r)) return ret;\n DD d=abs(a.p-b.p);\n DD s=acosl((a.r*a.r+d*d-b.r*b.r)/(2*a.r*d)); //0~π\n DD t=arg(b.p-a.p);\n if(EQ(a.r+b.r,d)) ret.push_back(a.p+polar(a.r,t));\n else ret.push_back(a.p+polar(a.r,t+s)),ret.push_back(a.p+polar(a.r,t-s));\n return ret;\n}\n\n//点pから円cに接線を引いたときの接点\n//verify:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_F&lang=ja\nvector<Point> TanLine(const Point &p,const Circle &c){\n vector<Point> ret;\n DD d=abs(p-c.p);\n if(LS(d,c.r)) return ret; //pがcの内部にある場合\n if(EQ(d,c.r)){\n ret.push_back(p);\n return ret; //円の線上にある場合\n } \n return crossPoint(c,Circle(p,sqrt(d*d-c.r*c.r)));\n}\n\n//円a,bの共通接線\n//https://www.slideshare.net/kujira16/ss-35861169\n//Line.aは円aと接線の交点\n//(接線と円bの交点)と(接線と円aの交点)が違う場合はLine.bは(接線と円bの交点)\n//一致する場合は円aの中心からLine.aに向かうベクトルを反時計回りに90度回転したものを\n//Line.aに足したもの\n//verify:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_G\n//verify:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2201\nvector<Line> TanLine(const Circle &a,const Circle &b){\n DD d=abs(a.p-b.p);\n vector<Line> ret;\n //外接線\n if(intersect(a,b)>=2){\n if(EQ(a.r,b.r)){\n Point up=polar(a.r,arg(b.p-a.p)+PI/2);\n ret.emplace_back(a.p+up,b.p+up);\n ret.emplace_back(a.p-up,b.p-up);\n }else{\n Point q=(a.p*b.r-b.p*a.r)/(b.r-a.r);\n auto as=TanLine(q,a);\n auto bs=TanLine(q,b);\n assert(as.size()==2 and bs.size()==2);\n for(int i=0;i<2;i++){\n ret.emplace_back(as[i],bs[i]);\n }\n }\n }else if(intersect(a,b)==1){ //内接する場合\n Point dir;\n if(a.r>b.r) dir=polar(a.r,arg(b.p-a.p));\n else dir=polar(a.r,arg(a.p-b.p));\n ret.emplace_back(a.p+dir,a.p+dir+rotate(dir,PI/2));\n }\n //内接線\n if(GR(abs(a.p-b.p),a.r+b.r)){ //円が離れている場合\n Point q=a.p+(b.p-a.p)*a.r/(a.r+b.r);\n auto as=TanLine(q,a);\n auto bs=TanLine(q,b);\n assert(as.size()==2 and bs.size()==2);\n for(int i=0;i<2;i++){\n ret.emplace_back(as[i],bs[i]);\n }\n }else if(EQ(d,a.r+b.r)){ //円が接している場合\n Point dir=polar(a.r,arg(b.p-a.p));\n ret.emplace_back(a.p+dir,a.p+dir+rotate(dir,PI/2));\n }\n return ret;\n}\n//2つの円の共通部分の面積\n//参考: https://tjkendev.github.io/procon-library/python/geometry/circles_intersection_area.html\n//verify: https://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=6005736#1\nDD Area(const Circle &c1,const Circle &c2){\n DD cd=abs(c1.p-c2.p);\n if(LE(c1.r+c2.r,cd)) return 0; //円が離れている\n\n if(LE(cd,abs(c1.r-c2.r))) //片方の円が他方を包んでいる\n return min(c1.r,c2.r)*min(c1.r,c2.r)*PI;\n\n DD theta1=acosl(cosine_formula(c1.r,cd,c2.r));\n DD theta2=acosl(cosine_formula(c2.r,cd,c1.r));\n\n return c1.r*c1.r*theta1+c2.r*c2.r*theta2-c1.r*cd*sin(theta1);\n}\n\n\n////////////////////////////\n// 三角形\n////////////////////////////\n\n//ヘロンの公式 三角形の面積を返す\n//三角形が成立するか(平行ではないか)を忘れずに\nDD Heron(const DD ad,const DD bd,const DD cd){\n DD s=(ad+bd+cd)/2;\n return sqrt(s*(s-ad)*(s-bd)*(s-cd));\n}\n//三角形の外接円\n//isParallel()を使って三角形が成立するか(平行ではないか)を忘れずに\n//verify:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_C\nCircle CircumscribedCircle(const Point &a,const Point &b,const Point &c){\n Point bc=(b+c)/(DD)2.0;\n Line aa(bc,bc+rotate(c-b,PI/2));\n Point ab=(a+b)/(DD)2.0;\n Line cc(ab,ab+rotate(b-a,PI/2));\n Point p=crossPoint(aa,cc);\n return Circle(p,abs(a-p));\n}\n//三角形の内接円\n//isParallel()を使って三角形が成立するか(平行ではないか)を忘れずに\n//verify:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_B\nCircle InCircle(const Point &a,const Point &b,const Point &c){\n Line aa(a,a+polar((DD)1.0,(arg(b-a)+arg(c-a))/(DD)2.0));\n Line bb(b,b+polar((DD)1.0,(arg(a-b)+arg(c-b))/(DD)2.0));\n Point p=crossPoint(aa,bb);\n return Circle(p,DistPL(p,Line(a,b)));\n}\n\n////////////////////////////\n// 多角形\n////////////////////////////\n\n//点pが多角形gに対してどの位置にあるか\n//中:2 辺上:1 外:0\n//凸多角形である必要ない\n//verify:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_3_C\nint contains(const Point &p,const vector<Point> &g){\n int n=(int)g.size();\n int x=false;\n for(int i=0;i<n;i++){\n Point a=g[i]-p,b=g[(i+1)%n]-p;\n if(EQ(cross(a,b),0) && dot(a,b)<EPS) return 1;\n if(a.Y>b.Y) swap(a,b);\n if(a.Y<EPS && EPS<b.Y && cross(a,b)>EPS) x=!x;\n }\n return (x?2:0);\n}\n//凸性判定\n//多角形gは凸か?\n//gは反時計回りでも時計回りでもいい\n//verify:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_3_B\nbool isConvex(const vector<Point> &g){\n int n=(int)g.size();\n if(n<=3) return true;\n int flag=0;\n int t;\n for(int i=0;i<n;i++){\n Point a(g[(i+1)%n]-g[i]),b(g[(i+2)%n]-g[i]);\n if(cross(a,b)>EPS) t=1;\n else if(cross(a,b)<-EPS) t=-1;\n else continue;\n if(flag==-t) return false;\n flag=t;\n }\n return true;\n}\n\n//凸包 アンドリューのアルゴリズム\n//O(NlogN)\n//反時計回りの多角形を返す\n//verify:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_4_A\nvector<Point> ConvexHull(vector<Point> s,const bool on_edge){\n int j;\n if(on_edge) j=-1;\n else j=1;\n int sz=(int)s.size();\n if(sz<3) return s;\n sort(s.begin(),s.end(),yx_sort);\n\n int n=0;\n vector<Point> res(2*sz);\n for(int i=0;i<sz;i++){\n while(n>=2 && cross(res[n-1]-res[n-2],s[i]-res[n-2])<EPS*j){\n n--;\n }\n res[n]=s[i];\n n++;\n }\n int t=n+1;\n for(int i=sz-2;i>=0;i--){\n while(n>=t && cross(res[n-1]-res[n-2],s[i]-res[n-2])<EPS*j){\n n--;\n }\n res[n]=s[i];\n n++;\n }\n res.resize(n-1);\n return res;\n}\n\n//多角形g(凸でなくてもいい)の符号付き面積\n//反時計回りの多角形なら正の値を返す 時計回りなら負\n//O(N)\n//https://imagingsolution.net/math/calc_n_point_area/\n//verify:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_3_A\nDD Area(const vector<Point> &g){\n DD ret=0.0;\n int n=(int)g.size();\n for(int i=0;i<n;i++){\n ret+=cross(g[i],g[(i+1)%n]);\n }\n return ret/2.0;\n}\n\n//凸多角形gを直線lで切断\n//直線の左側(向きを考慮)にできる凸多角形を返す\n//多角形gが反時計回りならば、反時計回りで返す(時計回りなら時計回り)\n//直線上の頂点を含む線分の交点は含めてない\n//verify:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_4_C\nvector<Point> ConvexCut(const vector<Point> &g,const Line &l){\n vector<Point> ret;\n int gz=(int)g.size();\n for(int i=0;i<gz;i++){\n Point now=g[i],next=g[(i+1)%gz];\n if(ccw(l.a,l.b,now)!=-1) ret.push_back(now);\n if(EQ(DistPL(now,l),0) or EQ(DistPL(next,l),0)) continue;\n if(ccw(l.a,l.b,now)*ccw(l.a,l.b,next)<0){\n ret.push_back(crossPoint(Line(now,next),l));\n }\n }\n return ret;\n}\n//部品\ninline DD calc(const Point &a,const Point &b,const DD &r,const bool triangle){\n if(triangle) return cross(a,b);\n else return r*r*arg(b/a);\n}\n//部品\nDD calcArea(const DD &r,const Point &a,const Point &b){\n if(EQ(abs(a-b),0)) return 0;\n bool ina=(abs(a)<r+EPS);\n bool inb=(abs(b)<r+EPS);\n if(ina && inb) return cross(a,b);\n auto cr=crossPointSC(Segment(a,b),Circle(Point(0,0),r));\n if(cr.empty()) return calc(a,b,r,false);\n auto s=cr[0],t=cr.back();\n return calc(s,t,r,true)+calc(a,s,r,ina)+calc(t,b,r,inb);\n}\n//円と多角形の共通部分の面積\n//多角形が反時計回りならば正の値を返す\n//凸多角形でなくても大丈夫\n//http://drken1215.hatenablog.com/entry/2020/02/02/091000\n//verify:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_H&lang=ja\nDD Area(const Circle &c,const vector<Point> &g){\n DD ret=0.0;\n int gz=g.size();\n if(gz<=2) return ret;\n for(int i=0;i<gz;i++){\n Point a=g[i]-c.p,b=g[(i+1)%gz]-c.p;\n ret+=calcArea(c.r,g[i]-c.p,g[(i+1)%gz]-c.p);\n }\n return ret/2.0;\n}\n\n//点と多角形の距離\n//点が多角形の中にあるなら0\nDD Dist(const Point &a,const vector<Point> &b){\n\tif(b.size()==1) return abs(a-b[0]);\n\tif(contains(a,b)>=1) return 0.0L;\n\tDD ret=INF;\n\tfor(int i=0;i<b.size();i++){\n\t\tchmin(ret,DistPS(a,Segment(b[i],b[(i+1)%b.size()])));\n\t}\n\treturn ret;\n}\n\n//多角形と多角形の距離\n//どちらかが内側に入っていたら0\n//全ての線分の組み合わせの距離を求めて、その最小が答え\n//O(size(a)*size(b))\n//sizeが1の時は点との距離になる\n//verify:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2827\nDD Dist(const vector<Point> &a,const vector<Point> &b){\n\tDD ret=INF;\n\tif(a.size()==1) return Dist(a[0],b);\n\tif(b.size()==1) return Dist(b[0],a);\n\n\tif(contains(a[0],b)>=1) return 0.0L;\n\tif(contains(b[0],a)>=1) return 0.0L;\n\n\tfor(int i=0;i<a.size();i++){\n\t\tfor(int j=0;j<b.size();j++){\n\t\t\tSegment sa(a[i],a[(i+1)%a.size()]);\n\t\t\tSegment sb(b[j],b[(j+1)%b.size()]);\n\t\t\tchmin(ret,Dist(sa,sb));\n\t\t}\n\t}\n\treturn ret;\n}\n\n////////////////////////////\n// 最近(遠)点間距離\n////////////////////////////\n//部品\nDD RecClosetPair(const vector<Point>::iterator it,const int n){\n if(n<=1) return INF;\n int m=n/2;\n DD x=it[m].X;\n DD d=min(RecClosetPair(it,m),RecClosetPair(it+m,n-m));\n inplace_merge(it,it+m,it+n,yx_sort);\n vector<Point> v;\n for(int i=0;i<n;i++){\n if(abs(it[i].X-x)>=d) continue;\n for(int j=0;j<v.size();j++){\n DD dy=it[i].Y-v[(int)v.size()-1-j].Y;\n if(dy>=d) break;\n DD dx=it[i].X-v[(int)v.size()-1-j].X;\n d=min(d,sqrt(dx*dx+dy*dy));\n }\n v.push_back(it[i]);\n }\n return d;\n}\n//最近点対の距離\n//点が1つのときINFを返す\n//O(NlogN)\n//verify:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_5_A\nDD ClosetPair(vector<Point> g){\n sort(g.begin(),g.end(),xy_sort);\n return RecClosetPair(g.begin(),g.size());\n}\n\n//最遠頂点間\n//verify:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_4_B\nDD Diameter(vector<Point> g){\n g=ConvexHull(g,1);\n int gz=g.size();\n int m=0,M=0;\n for(int i=1;i<gz;i++){\n if(g[i].Y<g[m].Y) m=i;\n if(g[i].Y>g[M].Y) M=i;\n }\n DD ret=0;\n int sm=m,sM=M;\n while(m!=sM || M!=sm){\n ret=max(ret,norm(g[m]-g[M]));\n if(cross(g[(m+1)%gz]-g[m],g[(M+1)%gz]-g[M])<0) m=(m+1)%gz;\n else M=(M+1)%gz;\n }\n return sqrt(ret);\n}\n//動的凸包\n//辺上にある点は含めない\n//verify:https://atcoder.jp/contests/geocon2013/tasks/geocon2013_c\nstruct DynamicConvex{\n\tset<Point> up,down;\n\tDynamicConvex(){}\n \n\tbool isContain(set<Point> &pol,Point p){\n\t\tif(pol.size()==0) return false;\n\t\tif(pol.size()==1){\n\t\t\tPoint q=*begin(pol);\n\t\t\treturn EQ(q.X,p.X) and GE(q.Y,p.Y);\n\t\t}\n\t\tassert(pol.size()>=2);\n auto left=begin(pol);\n auto right=(--end(pol));\n if(LS(p.X,left->X) or GR(p.X,right->X)) return false;\n \n \n auto q1=pol.upper_bound(Point(p.X,INF));\n if(q1==end(pol)){\n return GE(right->Y,p.Y);\n }\n auto q2=q1--; //q1が範囲外になるのは最初に弾いてる\n return GE(cross(p-*q1,*q2-*q1),0.0);\n\t}\n bool isContain(Point p){\n if(not isContain(up,p)) return false;\n if(not isContain(down,Point(p.X,-p.Y))) return false;\n return true;\n }\n \n\tvoid add(set<Point> &pol,Point p){\n\t\tif(pol.size()==0){\n\t\t\tpol.insert(p);\n\t\t\treturn;\n\t\t}\n\t\tif(pol.size()==1){\n Point q=*begin(pol);\n\t\t\tif(EQ(q.X,p.X)){\n pol.clear();\n pol.insert(max(p,q));\n\t\t\t}else{\n\t\t\t\tpol.insert(p);\n\t\t\t}\n return;\n\t\t}\n assert(pol.size()>=2);\n if(isContain(pol,p)) return;\n //left\n auto q1=pol.upper_bound(Point(p.X,INF));\n if(q1!=begin(pol)){\n q1--;\n while(pol.size()>=1 and q1!=begin(pol)){\n auto q2=q1--;\n if(GR(cross(*q2-p,*q1-p),0)) break;\n pol.erase(q2);\n }\n }\n //right\n q1=pol.lower_bound(Point(p.X,-INF));\n if(q1!=end(pol)){\n while(pol.size()>1 and q1!=--end(pol)){\n auto q2=q1++;\n if(GR(cross(*q1-p,*q2-p),0.0)) break;\n pol.erase(q2);\n }\n }\n pol.insert(p);\n\t}\n \n\tvoid add(Point p){\n\t\tadd(up,p);\n\t\tadd(down,Point(p.X,-p.Y)); //upと同じように処理をしたいから\n\t}\n};\n\n//垂直二等分線\n//p-↑-q こんなかんじ\n//voronoiで使う\nLine bisector(const Point &p, const Point &q) {\n Point c=(p+q)/DD(2.0);\n Point v=rotate(q-p,PI/2);\n v/=abs(v);\n return Line(c-v,c+v);\n}\n\n//ボロノイ図\n//pol:全体 ps:点の集合 id:中心となる点の添字 pp:中心となる点\n//計算量: pol.size()*ps.size()\n//verify: https://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=6005648#1\nvector<Point> voronoi(Polygon pol,const vector<Point> &ps,int id=-1,const Point pp=Point()){\n Point p;\n if(id!=-1) p=ps[id];\n else p=pp;\n for(size_t i=0;i<ps.size();i++){\n if(i==id) continue;\n Line l=bisector(p,ps[i]);\n pol=ConvexCut(pol,l);\n }\n return pol;\n}\n\n\nvoid solve(){\nwhile(true){\n //考える必要があるイベント\n //いちご2つを結んだ線\n //いちごと角を結んだ線\n int N;\n DD W,H;\n cin>>W>>H>>N;\n if(N==0) return;\n vector<Point> straw(N*2);\n for(int i=0;i<N*2;i++) cin>>straw[i];\n vector<DD> event;\n Segment Y0(Point(0,0),Point(0,H));\n Segment Y1(Point(W,0),Point(W,H));\n//いちご2つを結んだ線\n for(int i=0;i<N*2;i++){\n for(int j=i+1;j<N*2;j++){\n Line l(straw[i],straw[j]);\n if(isParallel(l,Y0)) continue;\n auto x=crossPoint(Y0,l);\n if(ccw(Y0.a,Y0.b,x)!=0) continue;\n event.push_back(x.Y);\n }\n }\n vector<Point> kado={Point(W,0),Point(W,H)};\n//いちごと角を結んだ線\n for(int i=0;i<N*2;i++){\n if(EQ(straw[i].X,0)){\n event.push_back(straw[i].Y);\n event.push_back(straw[i].Y);\n continue;\n }\n for(auto x:kado){\n Line l(straw[i],x);\n if(isParallel(l,Y0)) continue;\n //cerr<<\"do\"<<endl;\n auto y=crossPoint(Y0,l);\n if(ccw(Y0.a,Y0.b,y)!=0) continue;\n event.push_back(y.Y);\n }\n }\n event.push_back(0);\n event.push_back(H);\n int sz=event.size();\n sort(ALL(event));\n vector<DD> ans(event.size());\n int id=0;\n\n for(size_t I=0;I<event.size();I++){\n auto y=event[I];\n Point p(0,y);\n vector<DD> v;\n for(int i=0;i<N*2;i++){\n if(abs(p-straw[i])<EPS){\n if(I and EQ(event[I]-event[I-1],0)) v.push_back(-PI/2);\n else v.push_back(PI/2);\n }else v.push_back(arg(straw[i]-p));\n }\n sort(v.begin(),v.end());\n Segment s(p,p+polar(DD(1),v[N-1]));\n Segment t(p,p+polar(DD(1),v[N]));\n DD u=-1100,d=1100;\n if(isParallel(Y1,s)==false) u=crossPoint(Y1,s).Y;\n if(isParallel(Y1,t)==false) d=crossPoint(Y1,t).Y;\n ans[id++]=max(0.0L,min(d,H))-max(DD(0),min(u,H));\n //chmax(ans[id-1],0.0L);\n }\n DD x=0;\n for(size_t i=1;i<event.size();i++){\n x+=(event[i]-event[i-1])*(ans[i]+ans[i-1])/2;\n }\n cout<<x/H/H<<endl;\n}\n}\n\nint main(){\n bin101();\n int T=1;\n //cin>>T;\n while(T--) solve();\n}", "accuracy": 1, "time_ms": 4150, "memory_kb": 4264, "score_of_the_acc": -1.5577, "final_rank": 19 }, { "submission_id": "aoj_2256_5905874", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nint main(){\ndouble W,H;\nint N;\nwhile(cin>>W>>H>>N,N){\nvector<double>X(N*2),Y(N*2),L{0,H};\nfor(int i=0;i<N*2;i++){\ncin>>X[i]>>Y[i];\nif(X[i]!=W){\nL.emplace_back(Y[i]/(W-X[i])*W);\nL.emplace_back(H+(Y[i]-H)/(W-X[i])*W);\n}\nfor(int j=0;j<i;j++)\nif(X[i]!=X[j])\nL.emplace_back(Y[j]+(Y[i]-Y[j])/(X[j]-X[i])*X[j]);\n}\nsort(begin(L),end(L));\ndouble ans=0;\nfor(int i=0;i<(int)L.size()-1;i++){\ndouble mid=(L[i]+L[i+1])/2;\nif(clamp(mid,0.,H)!=mid)\ncontinue;\nvector<double>R(N*2);\nfor(int j=0;j<N*2;j++)\nR[j]=clamp(X[j]?mid+(Y[j]-mid)/X[j]*W:(mid<Y[j]?H:0),0.,H);\nsort(begin(R),end(R));\nans+=(L[i+1]-L[i])*(R[N]-R[N-1]);\n}\ncout<<fixed<<setprecision(10)<<ans/H/H<<endl;\n}\n}", "accuracy": 1, "time_ms": 310, "memory_kb": 3844, "score_of_the_acc": -0.5533, "final_rank": 10 }, { "submission_id": "aoj_2256_5905850", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n double W, H;\n int N;\n while (cin >> W >> H >> N, N) {\n vector<double> X(N * 2), Y(N * 2), L{0, H};\n for (int i = 0; i < N * 2; i++) {\n cin >> X[i] >> Y[i];\n if (X[i] != W) {\n L.emplace_back(Y[i] / (W - X[i]) * W);\n L.emplace_back(H + (Y[i] - H) / (W - X[i]) * W);\n }\n for (int j = 0; j < i; j++)\n if (X[i] != X[j])\n L.emplace_back(Y[j] + (Y[i] - Y[j]) / (X[j] - X[i]) * X[j]);\n }\n sort(begin(L), end(L));\n double ans = 0;\n for (int i = 0; i < (int)L.size() - 1; i++) {\n double mid = (L[i] + L[i + 1]) / 2;\n if (clamp(mid, 0., H) != mid)\n continue;\n vector<double> R(N * 2);\n for (int j = 0; j < N * 2; j++)\n R[j] = clamp(X[j] ? mid + (Y[j] - mid) / X[j] * W : (mid < Y[j] ? H : 0), 0., H);\n sort(begin(R), end(R));\n ans += (L[i + 1] - L[i]) * (R[N] - R[N - 1]);\n }\n cout << fixed << setprecision(10) << ans / H / H << endl;\n }\n}", "accuracy": 1, "time_ms": 310, "memory_kb": 3756, "score_of_the_acc": -0.4738, "final_rank": 9 }, { "submission_id": "aoj_2256_5800947", "code_snippet": "/*\tauthor: Kite_kuma\n\tcreated: 2021.08.18 20:59:03 */\n\n// #ifdef LOCAL\n// #define _GLIBCXX_DEBUG\n// #endif\n#include <bits/stdc++.h>\nusing namespace std;\n\n#pragma region macros\n\n#define foa(s, v) for(auto &s : v)\n#define all(v) (v).begin(), (v).end()\n#define rall(v) (v).rbegin(), (v).rend()\n#define popcnt(n) __builtin_popcountll((long long)n)\n\n#define REPname(a, b, c, d, e, ...) e\n#define rep(...) REPname(__VA_ARGS__, REP3, REP2, REP1, REP0)(__VA_ARGS__)\n#define REP0(x) for(int Counter_in_rep_macro = 0; Counter_in_rep_macro < (x); ++Counter_in_rep_macro)\n#define REP1(i, x) for(int i = 0; i < (x); ++i)\n#define REP2(i, l, r) for(int i = (l); i < (r); ++i)\n#define REP3(i, l, r, c) for(int i = (l); i < (r); i += (c))\n\n#define DREPname(a, b, c, d, e, ...) e\n#define drep(...) DREPname(__VA_ARGS__, DREP3, DREP2, DREP1)(__VA_ARGS__)\n#define DREP1(i, x) for(int i = (x)-1; i >= 0; --i)\n#define DREP2(i, l, r) for(int i = (r)-1; i >= (l); --i)\n#define DREP3(i, l, r, c) for(int i = (r)-1; i >= (l); i -= (c))\n\n#pragma endregion\n\n#pragma region aliases\n\nusing ll = long long;\nusing ld = long double;\nusing ull = unsigned long long;\nusing vi = std::vector<int>;\nusing vvi = std::vector<std::vector<int>>;\nusing vvvi = std::vector<std::vector<std::vector<int>>>;\nusing vll = std::vector<ll>;\nusing vvll = std::vector<vll>;\nusing vvvll = std::vector<vvll>;\nusing pii = std::pair<int, int>;\nusing pll = std::pair<long long, long long>;\ntemplate <class T = ll>\nusing V = std::vector<T>;\ntemplate <class T = ll>\nusing VV = V<V<T>>;\ntemplate <class T = ll>\nusing VVV = V<V<V<T>>>;\ntemplate <class T = ll>\nusing pqup = std::priority_queue<T, std::vector<T>, std::greater<T>>;\ntemplate <class T = ll>\nusing pqdn = std::priority_queue<T>;\n\n#pragma endregion\n\n#pragma region constants\n\nconst int inf = 1e9;\nconst long long INF = 1e18;\nconst long double pi = acos(-1);\nconst char dl = '\\n';\nconst char sp = ' ';\nint dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};\nint dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};\n\nconst int mod_1000000007 = 1000000007;\nconst int mod_998244353 = 998244353;\n\n#pragma endregion\n\n#pragma region basic_operation\n\ntemplate <class T0, class T1, class T2>\ninline bool in_range(T0 x, T1 lef, T2 rig) {\n\treturn ((lef <= x) && (x < rig));\n}\n\ntemplate <class T>\ninline bool chmin(T &a, T b) {\n\tif(a > b) {\n\t\ta = b;\n\t\treturn true;\n\t}\n\treturn false;\n}\ntemplate <class T>\ninline bool chmax(T &a, T b) {\n\tif(a < b) {\n\t\ta = b;\n\t\treturn true;\n\t}\n\treturn false;\n}\n\nvoid Yes(bool f = 1) { std::cout << (f ? \"Yes\" : \"No\") << '\\n'; }\nvoid No() { std::cout << \"No\\n\"; }\nvoid YES(bool f = 1) { std::cout << (f ? \"YES\" : \"NO\") << '\\n'; }\nvoid NO() { std::cout << \"NO\\n\"; }\n\ntemplate <class T>\nvoid drop(T answer) {\n\tstd::cout << answer << '\\n';\n\texit(0);\n}\n\nvoid err(bool flag = true) {\n\tif(!flag) return;\n\tstd::cout << -1 << '\\n';\n\texit(0);\n}\n\ntemplate <class T>\nvoid vout(std::vector<T> const &v, bool vertically = 0) {\n\tif(vertically) {\n\t\tfor(auto const &a : v) {\n\t\t\tstd::cout << a << '\\n';\n\t\t}\n\t\treturn;\n\t}\n\tfor(auto it = v.begin(); it != v.end(); it++) {\n\t\tstd::cout << (*it);\n\t\tif(it != v.end() - 1) {\n\t\t\tstd::cout << ' ';\n\t\t}\n\t}\n\tstd::cout << '\\n';\n\treturn;\n}\n\ninline void print() { std::cout << '\\n'; }\ntemplate <class T>\ninline void print(T x) {\n\tstd::cout << x << '\\n';\n\treturn;\n}\ntemplate <typename Head, typename... Tail>\nvoid print(Head H, Tail... T) {\n\tstd::cout << H << \" \";\n\tprint(T...);\n}\n\ntemplate <class T>\nvoid add(std::vector<T> &v, T val) {\n\tfor(auto &a : v) a += val;\n\treturn;\n}\n\ntemplate <class T>\nT dup(T a, T b) {\n\tassert(b != 0);\n\treturn (a + b - 1) / b;\n}\n\ntemplate <class T>\nT greatest_lower_multiple(T x, T d) {\n\tif(d == 0) return 0;\n\tif(d < 0) d *= -1;\n\tT y = x % d;\n\tif(y < 0) y += d;\n\treturn x - y;\n}\n\ntemplate <class T>\nT least_upper_multiple(T x, T d) {\n\treturn -greatest_lower_multiple(-x, d);\n}\n\nlong long POW(long long a, long long n) {\n\tlong long res = 1;\n\twhile(n > 0) {\n\t\tif(n & 1) res = res * a;\n\t\ta = a * a;\n\t\tn >>= 1;\n\t}\n\treturn res;\n}\n\nlong long modpow(long long a, long long n, long long mod) {\t // a^n mod\n\tassert(n >= 0 && mod);\n\tif(mod == 1) return 0LL;\n\tlong long res = 1;\n\ta %= mod;\n\twhile(n > 0) {\n\t\tif(n & 1) res = res * a % mod;\n\t\ta = a * a % mod;\n\t\tn >>= 1;\n\t}\n\treturn res < 0 ? res + mod : res;\n}\n\n// return x which satisfies a * x % mod == gcd(a, mod)\n// not (mod | a)\nlong long modinv(long long a, long long mod) {\n\tlong long b = mod, u = 1, v = 0;\n\twhile(b) {\n\t\tlong long t = a / b;\n\t\ta -= t * b;\n\t\tstd::swap(a, b);\n\t\tu -= t * v;\n\t\tstd::swap(u, v);\n\t}\n\tu %= mod;\n\tif(u < 0) u += mod;\n\treturn u;\n}\n\ntemplate <class T>\nint lb(const std::vector<T> &a, const T x) {\n\treturn std::distance(a.begin(), std::lower_bound(a.begin(), a.end(), x));\n}\ntemplate <class T>\nint ub(const std::vector<T> &a, const T x) {\n\treturn std::distance(a.begin(), std::upper_bound(a.begin(), a.end(), x));\n}\ntemplate <class T>\nvoid unq_sort(std::vector<T> &a) {\n\tstd::sort(a.begin(), a.end());\n\ta.erase(std::unique(a.begin(), a.end()), a.end());\n}\ntemplate <class T>\nstd::vector<int> press(std::vector<T> &a) {\n\tauto vec = a;\n\tunq_sort(vec);\n\tstd::vector<int> ret;\n\tfor(auto &v : a) ret.push_back(lb(vec, v));\n\treturn ret;\n}\n\n#pragma endregion\n\n#pragma region input\n#define VEC(type, name, size) \\\n\tvector<type> name(size); \\\n\tscanner::INPUT(name)\n#define VVEC(type, name, h, w) \\\n\tvector<vector<type>> name(h, vector<type>(w)); \\\n\tscanner::INPUT(name)\n#define INT(...) \\\n\tint __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define LL(...) \\\n\tlong long __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define STR(...) \\\n\tstring __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define CHR(...) \\\n\tchar __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define DBL(...) \\\n\tdouble __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define LD(...) \\\n\tlong double __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define TPL3(type0, type1, type2, name) \\\n\tstd::tuple<type0, type1, type2> name; \\\n\tscanner::INPUT(name);\n#define TPL4(type0, type1, type2, type3, name) \\\n\tstd::tuple<type0, type1, type2, type3> name; \\\n\tscanner::INPUT(name);\n\nnamespace scanner {\ntemplate <class T>\nvoid scan(T &a) {\n\tstd::cin >> a;\n}\n\ntemplate <class T, class L>\nvoid scan(std::pair<T, L> &p) {\n\tscan(p.first);\n\tscan(p.second);\n}\n\ntemplate <class T0, class T1, class T2>\nvoid scan(std::tuple<T0, T1, T2> &p) {\n\tT0 t0;\n\tT1 t1;\n\tT2 t2;\n\tscan(t0);\n\tscan(t1);\n\tscan(t2);\n\tp = std::make_tuple(t0, t1, t2);\n}\n\ntemplate <class T0, class T1, class T2, class T3>\nvoid scan(std::tuple<T0, T1, T2, T3> &p) {\n\tT0 t0;\n\tT1 t1;\n\tT2 t2;\n\tT3 t3;\n\tscan(t0);\n\tscan(t1);\n\tscan(t2);\n\tscan(t3);\n\tp = std::make_tuple(t0, t1, t2, t3);\n}\n\ntemplate <class T>\nvoid scan(std::vector<T> &a) {\n\tfor(auto &i : a) scan(i);\n}\n\nvoid INPUT() {}\ntemplate <class Head, class... Tail>\nvoid INPUT(Head &head, Tail &... tail) {\n\tscan(head);\n\tINPUT(tail...);\n}\n} // namespace scanner\n\ntemplate <typename T1, typename T2>\nstd::istream &operator>>(std::istream &is, std::pair<T1, T2> &p) {\n\tis >> p.first >> p.second;\n\treturn is;\n}\n#pragma endregion\n\n#pragma region debug\n\n#pragma region output\ntemplate <typename T1, typename T2>\nstd::ostream &std::operator<<(std::ostream &os, const std::pair<T1, T2> &p) {\n\tos << p.first << \" \" << p.second;\n\treturn os;\n}\ntemplate <class T>\nstd::ostream &std::operator<<(std::ostream &os, const std::vector<T> &v) {\n\tfor(int i = 0; i < (int)v.size(); i++) {\n\t\tif(i) os << \" \";\n\t\tos << v[i];\n\t}\n\treturn os;\n}\n#pragma endregion\n\n#pragma region view\n\nnamespace viewer {\nvoid view(const long long e) {\n\tif(e == INF)\n\t\tstd::cerr << \"INF\";\n\telse if(e == -INF)\n\t\tstd::cerr << \"-INF\";\n\telse\n\t\tstd::cerr << e;\n}\n\nvoid view(const int e) {\n\tif(e == inf)\n\t\tstd::cerr << \"inf\";\n\telse if(e == -inf)\n\t\tstd::cerr << \"-inf\";\n\telse\n\t\tstd::cerr << e;\n}\n\ntemplate <typename T>\nvoid view(const T e) {\n\tstd::cerr << e;\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::pair<T, U> &p) {\n\tstd::cerr << \"(\";\n\tview(p.first);\n\tstd::cerr << \", \";\n\tview(p.second);\n\tstd::cerr << \")\";\n}\n\ntemplate <class T0, class T1, class T2>\nvoid view(const std::tuple<T0, T1, T2> &p) {\n\tstd::cerr << \"(\";\n\tview(std::get<0>(p));\n\tstd::cerr << \", \";\n\tview(std::get<1>(p));\n\tstd::cerr << \", \";\n\tview(std::get<2>(p));\n\tstd::cerr << \")\";\n}\n\ntemplate <class T0, class T1, class T2, class T3>\nvoid view(const std::tuple<T0, T1, T2, T3> &p) {\n\tstd::cerr << \"(\";\n\tview(std::get<0>(p));\n\tstd::cerr << \", \";\n\tview(std::get<1>(p));\n\tstd::cerr << \", \";\n\tview(std::get<2>(p));\n\tstd::cerr << \", \";\n\tview(std::get<3>(p));\n\tstd::cerr << \")\";\n}\n\ntemplate <typename T>\nvoid view(const std::set<T> &s) {\n\tif(s.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << \"{ \";\n\tfor(auto &t : s) {\n\t\tview(t);\n\t\tstd::cerr << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\n\ntemplate <typename T>\nvoid view(const std::unordered_set<T> &s) {\n\tif(s.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << \"{ \";\n\tfor(auto &t : s) {\n\t\tview(t);\n\t\tstd::cerr << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\n\ntemplate <typename T>\nvoid view(const std::vector<T> &v) {\n\tif(v.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << \"{ \";\n\tfor(const auto &e : v) {\n\t\tview(e);\n\t\tstd::cerr << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\n\ntemplate <typename T>\nvoid view(const std::vector<std::vector<T>> &vv) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &v : vv) {\n\t\tstd::cerr << \"\\t\";\n\t\tview(v);\n\t\tstd::cerr << '\\n';\n\t}\n\tstd::cerr << \"}\";\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::vector<std::pair<T, U>> &v) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &c : v) {\n\t\tstd::cerr << \"\\t(\";\n\t\tview(c.first);\n\t\tstd::cerr << \", \";\n\t\tview(c.second);\n\t\tstd::cerr << \")\\n\";\n\t}\n\tstd::cerr << \"}\";\n}\n\ntemplate <class T0, class T1, class T2>\nvoid view(const std::vector<std::tuple<T0, T1, T2>> &v) {\n\tif(v.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << '{';\n\tfor(const auto &t : v) {\n\t\tstd::cerr << \"\\n\\t\";\n\t\tview(t);\n\t\tstd::cerr << \",\";\n\t}\n\tstd::cerr << \"\\n}\";\n}\n\ntemplate <class T0, class T1, class T2, class T3>\nvoid view(const std::vector<std::tuple<T0, T1, T2, T3>> &v) {\n\tif(v.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << '{';\n\tfor(const auto &t : v) {\n\t\tstd::cerr << \"\\n\\t\";\n\t\tview(t);\n\t\tstd::cerr << \",\";\n\t}\n\tstd::cerr << \"\\n}\";\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::map<T, U> &m) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &t : m) {\n\t\tstd::cerr << \"\\t[\";\n\t\tview(t.first);\n\t\tstd::cerr << \"] : \";\n\t\tview(t.second);\n\t\tstd::cerr << '\\n';\n\t}\n\tstd::cerr << \"}\";\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::unordered_map<T, U> &m) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &t : m) {\n\t\tstd::cerr << \"\\t[\";\n\t\tview(t.first);\n\t\tstd::cerr << \"] : \";\n\t\tview(t.second);\n\t\tstd::cerr << '\\n';\n\t}\n\tstd::cerr << \"}\";\n}\n} // namespace viewer\n\n#pragma endregion\n\n// when debugging : g++ foo.cpp -DLOCAL\n#ifdef LOCAL\nvoid debug_out() {}\ntemplate <typename Head, typename... Tail>\nvoid debug_out(Head H, Tail... T) {\n\tviewer::view(H);\n\tstd::cerr << \", \";\n\tdebug_out(T...);\n}\n#define debug(...) \\\n\tdo { \\\n\t\tstd::cerr << __LINE__ << \" [\" << #__VA_ARGS__ << \"] : [\"; \\\n\t\tdebug_out(__VA_ARGS__); \\\n\t\tstd::cerr << \"\\b\\b]\\n\"; \\\n\t} while(0)\n#define dump(x) \\\n\tdo { \\\n\t\tstd::cerr << __LINE__ << \" \" << #x << \" : \"; \\\n\t\tviewer::view(x); \\\n\t\tstd::cerr << '\\n'; \\\n\t} while(0)\n\n#else\n#define debug(...) (void(0))\n#define dump(x) (void(0))\n#endif\n\n#pragma endregion\n\n#pragma region geometry\n\nusing Real = long double;\nusing R = Real;\nusing Point = std::complex<Real>;\nusing Vec = Point;\nconst Real EPS = 1e-10, PI = acos(-1);\n\ninline bool eq(const Real &a, const Real &b) { return fabs(b - a) < EPS; }\ninline bool eq(const Point &a, const Point &b) { return fabs(b - a) < EPS; }\n/*\n\t-1: a < b\n\t0 : a == b\n\t1 : a > b\n*/\ninline int compare(const Real &a, const Real &b) { return eq(a, b) ? 0 : a < b ? -1 : 1; }\n\nnamespace std {\nconst Point operator*(const Point &p, const Real &d) { return Point(real(p) * d, imag(p) * d); }\n} // namespace std\n\nstd::istream &operator>>(std::istream &is, Point &p) {\n\tReal a, b;\n\tis >> a >> b;\n\tp = Point(a, b);\n\treturn is;\n}\nstd::ostream &operator<<(std::ostream &os, Point &p) { return os << std::fixed << std::setprecision(10) << p.real() << \" \" << p.imag(); }\n\n// rotate point 'p' for counter clockwise direction\nconst Point rotate(const Real &theta, const Point &p) {\n\treturn Point(cos(theta) * p.real() - sin(theta) * p.imag(), sin(theta) * p.real() + cos(theta) * p.imag());\n}\n\nconst Real radian_to_degree(const Real &r) { return (r * 180.0 / PI); }\n\nconst Real degree_to_radian(const Real &d) { return (d * PI / 180.0); }\n\n// get angle a-b-c (<pi)\nconst Real get_angle(const Point &a, const Point &b, const Point &c) {\n\tconst Point v(a - b), w(c - b);\n\tReal theta = fabs(atan2(w.imag(), w.real()) - atan2(v.imag(), v.real()));\n\treturn std::min(theta, 2 * PI - theta);\n}\n\nnamespace std {\nbool operator<(const Point &a, const Point &b) { return a.real() != b.real() ? a.real() < b.real() : a.imag() < b.imag(); }\n} // namespace std\n\nstruct Line {\n\tPoint a, b;\n\n\tLine() = default;\n\n\tLine(Point a, Point b) : a(a), b(b) {}\n\n\tLine(Real A, Real B, Real C) // Ax + By = C\n\t{\n\t\tif(eq(A, 0))\n\t\t\ta = Point(0, C / B), b = Point(1, C / B);\n\t\telse if(eq(B, 0))\n\t\t\tb = Point(C / A, 0), b = Point(C / A, 1);\n\t\telse\n\t\t\ta = Point(0, C / B), b = Point(C / A, 0);\n\t}\n\n\tfriend std::ostream &operator<<(std::ostream &os, Line &p) { return os << p.a << \" -- \" << p.b; }\n\n\tfriend std::istream &operator>>(std::istream &is, Line &a) { return is >> a.a >> a.b; }\n};\n\nstruct Segment : Line {\n\tSegment() = default;\n\n\tSegment(Point a, Point b) : Line(a, b) {}\n};\n\nstruct Circle {\n\tPoint p;\n\tReal r;\n\n\tCircle() = default;\n\n\tCircle(Point p, Real r) : p(p), r(r) {}\n};\n\nusing Points = std::vector<Point>;\nusing Polygon = std::vector<Point>;\nusing Segments = std::vector<Segment>;\nusing Lines = std::vector<Line>;\nusing Circles = std::vector<Circle>;\n\nconst Real cross(const Point &a, const Point &b) { return real(a) * imag(b) - imag(a) * real(b); }\nconst Real dot(const Point &a, const Point &b) { return real(a) * real(b) + imag(a) * imag(b); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_C\nconst int ccw(const Point &a, Point b, Point c) {\n\tb = b - a, c = c - a;\n\tif(cross(b, c) > EPS) return +1;\t\t// \"COUNTER_CLOCKWISE\"\n\tif(cross(b, c) < -EPS) return -1;\t\t// \"CLOCKWISE\"\n\tif(dot(b, c) < -EPS) return +2;\t\t\t// \"ONLINE_BACK\"\n\tif(norm(b) + EPS < norm(c)) return -2;\t// \"ONLINE_FRONT\"\n\treturn 0;\t\t\t\t\t\t\t\t// \"ON_SEGMENT\"\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_A\nbool parallel(const Line &a, const Line &b) { return eq(cross(a.b - a.a, b.b - b.a), 0.0); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_A\nbool orthogonal(const Line &a, const Line &b) { return eq(dot(a.a - a.b, b.a - b.b), 0.0); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_A\nPoint projection(const Line &l, const Point &p) {\n\tdouble t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n\treturn l.a + (l.a - l.b) * t;\n}\n\nPoint projection(const Segment &l, const Point &p) {\n\tdouble t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n\treturn l.a + (l.a - l.b) * t;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_B\nPoint reflection(const Line &l, const Point &p) { return projection(l, p) * 2.0 - p; }\n\nint intersect(const Line &l, const Point &p) { return int(abs(ccw(l.a, l.b, p)) != 1); }\n\nint intersect(const Line &l, const Line &m) {\n\tif(intersect(l, m.a) && intersect(l, m.b)) return -1;\n\treturn int(abs(cross(l.b - l.a, m.b - m.a)) > EPS);\n}\n\nint intersect(const Segment &s, const Point &p) { return int(ccw(s.a, s.b, p) == 0); }\n\nint intersect(const Line &l, const Segment &s) {\n\tif(intersect(l, s.a) && intersect(l, s.b)) return -1;\n\treturn cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < EPS;\n}\n\nReal distance(const Line &l, const Point &p);\n\nint intersect(const Circle &c, const Line &l) {\n\tReal d = c.r - distance(l, c.p);\n\treturn fabs(d) < EPS ? 1 : d > 0. ? 2 : 0;\n}\n\nint intersect(const Circle &c, const Point &p) { return int(abs(abs(p - c.p) - c.r) < EPS); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_B\nint intersect(const Segment &s, const Segment &t) {\n\tif(eq(s.a, s.b)) return intersect(t, s.a);\n\tif(intersect(Line(s), t.a) && intersect(Line(s), t.b) &&\n\t std::max(std::min(s.a, s.b), std::min(t.a, t.b)) < std::min(std::max(s.a, s.b), std::max(t.a, t.b)))\n\t\treturn -1;\n\treturn int(ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 && ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0);\n}\n\nint intersect(const Circle &c, const Segment &l) {\n\tconst Point h = projection(l, c.p);\n\tif(norm(h - c.p) - c.r * c.r > EPS) return 0;\n\tauto d1 = abs(c.p - l.a), d2 = abs(c.p - l.b);\n\tif(compare(d1, c.r) == -1 && compare(d2, c.r) == -1) return 0;\n\tif(d1 < c.r - EPS && d2 > c.r - EPS || d1 > c.r - EPS && d2 < c.r - EPS) return 1;\n\treturn dot(l.a - h, l.b - h) < 0 ? 2 : 0;\n}\n\nReal distance(const Point &a, const Point &b);\n\nint number_of_common_tangents(const Circle &c1, const Circle &c2) {\n\tReal r1 = std::min(c1.r, c2.r), r2 = std::max(c1.r, c2.r), d = distance(c1.p, c2.p);\n\tint com = compare(r1 + r2, d);\n\treturn com == 1 ? compare(d + r1, r2) + 1 : 3 - com;\n\t// if(c1.r < c2.r) swap(c1, c2);\n\t// Real d = abs(c1.p - c2.p);\n\t// if(compare(c1.r + c2.r, d) == -1) return 4;\n\t// if(eq(c1.r + c2.r, d)) return 3;\n\t// if(compare(c1.r - c2.r, d) == -1) return 2;\n\t// return int(eq(c1.r - c2.r, d));\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_A&lang=jp\nint intersect(const Circle &c1, const Circle &c2) { return 2 - abs(2 - number_of_common_tangents(c1, c2)); }\n\nReal distance(const Point &a, const Point &b) { return abs(a - b); }\n\nReal distance(const Line &l, const Point &p) { return abs(p - projection(l, p)); }\n\nReal distance(const Line &l, const Line &m) { return intersect(l, m) ? 0 : distance(l, m.a); }\n\nReal distance(const Segment &s, const Point &p) {\n\tPoint r = projection(s, p);\n\treturn intersect(s, r) ? distance(r, p) : std::min(abs(s.a - p), abs(s.b - p));\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_D\nReal distance(const Segment &a, const Segment &b) {\n\tif(intersect(a, b)) return 0;\n\treturn std::min({distance(a, b.a), distance(a, b.b), distance(b, a.a), distance(b, a.b)});\n}\n\nReal distance(const Line &l, const Segment &s) {\n\tif(intersect(l, s)) return 0;\n\treturn std::min(distance(l, s.a), distance(l, s.b));\n}\n\nPoint crosspoint(const Line &l, const Line &m) {\n\tReal A = cross(l.b - l.a, m.b - m.a);\n\tReal B = cross(l.b - l.a, l.b - m.a);\n\tif(eq(abs(A), 0.0) && eq(abs(B), 0.0)) return m.a;\n\treturn m.a + (m.b - m.a) * B / A;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_C\nPoint crosspoint(const Segment &l, const Segment &m) { return crosspoint(Line(l), Line(m)); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_D\nstd::pair<Point, Point> crosspoint(const Circle &c, const Line l) {\n\tPoint pr = projection(l, c.p);\n\tif(eq(distance(l, c.p), c.r)) return {pr, pr};\n\tVec v = (l.b - l.a) / abs(l.b - l.a) * sqrt(c.r * c.r - norm(pr - c.p));\n\treturn make_pair(pr - v, pr + v);\n\t// Vec e = (l.b - l.a) / abs(l.b - l.a);\n\t// double base = sqrt(c.r * c.r - norm(pr - c.p));\n\t// return {pr - e * base, pr + e * base};\n}\n\nstd::pair<Point, Point> crosspoint(const Circle &c, const Segment &l) {\n\tif(intersect(c, l) == 2) return crosspoint(c, Line(l.a, l.b));\n\tauto ret = crosspoint(c, Line(l.a, l.b));\n\tif(dot(l.a - ret.first, l.b - ret.first) < EPS)\n\t\tret.second = ret.first;\n\telse\n\t\tret.first = ret.second;\n\treturn ret;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_E\nstd::pair<Point, Point> crosspoint(const Circle &c1, const Circle &c2) {\n\tReal d = abs(c1.p - c2.p);\n\tReal a = acos((c1.r * c1.r + d * d - c2.r * c2.r) / (2 * c1.r * d)); // cosine theorem\n\tReal t = atan2(c2.p.imag() - c1.p.imag(), c2.p.real() - c1.p.real());\n\tPoint p1 = c1.p + Point(cos(t + a) * c1.r, sin(t + a) * c1.r);\n\tPoint p2 = c1.p + Point(cos(t - a) * c1.r, sin(t - a) * c1.r);\n\treturn make_pair(p1, p2);\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_F\n// \"点 p を通る円 c の接線\"の接点2つ\nstd::pair<Point, Point> tangent(const Circle &c1, const Point &p2) {\n\treturn crosspoint(c1, Circle(p2, sqrt(norm(c1.p - p2) - c1.r * c1.r)));\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_G\n// 円 c1, c2 の共通接線\nLines tangent(Circle c1, Circle c2) {\n\tLines ret;\n\tif(c1.r < c2.r) std::swap(c1, c2);\n\tReal g = norm(c1.p - c2.p);\n\tif(eq(g, 0)) return ret;\n\tVec u = (c2.p - c1.p) / Real(sqrt(g));\n\tVec v = rotate(PI * 0.5, u);\n\tfor(int s : {-1, 1}) {\n\t\tReal h = (c1.r + s * c2.r) / sqrt(g);\n\t\tif(eq(1 - h * h, 0)) {\n\t\t\tret.emplace_back(c1.p + u * c1.r, c1.p + (u + v) * c1.r);\n\t\t} else if(1 - h * h > 0) {\n\t\t\tPoint uu = u * h, vv = v * sqrt(1 - h * h);\n\t\t\tret.emplace_back(c1.p + (uu + vv) * c1.r, c2.p - (uu + vv) * c2.r * s);\n\t\t\tret.emplace_back(c1.p + (uu - vv) * c1.r, c2.p - (uu - vv) * c2.r * s);\n\t\t}\n\t}\n\treturn ret;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_3_B\nbool is_convex(const Polygon &p, bool pi_is_ok = false) {\n\tint n = p.size();\n\tif(pi_is_ok) {\n\t\tfor(int i = 0; i < n; i++)\n\t\t\tif(ccw(p[i], p[(i + 1) % n], p[(i + 2) % n]) == -1) return false;\n\t} else {\n\t\tfor(int i = 0; i < n; i++)\n\t\t\tif(ccw(p[i], p[(i + 1) % n], p[(i + 2) % n]) != 1) return false;\n\t}\n\treturn true;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_4_A\nPolygon convex_hull(Polygon &p, bool pi_is_ok = false) {\n\tint n = (int)p.size(), k = 0;\n\tif(n <= 2) return p;\n\tsort(p.begin(), p.end());\n\tPoints ch(2 * n);\n\tif(pi_is_ok) {\n\t\tfor(int i = 0; i < n; ch[k++] = p[i++])\n\t\t\twhile(k >= 2 && ccw(ch[k - 2], ch[k - 1], p[i]) == -1) --k;\n\t\tfor(int i = n - 2, t = k + 1; i >= 0; ch[k++] = p[i--])\n\t\t\twhile(k >= t && ccw(ch[k - 2], ch[k - 1], p[i]) == -1) --k;\n\t} else {\n\t\tfor(int i = 0; i < n; ch[k++] = p[i++])\n\t\t\twhile(k >= 2 && ccw(ch[k - 2], ch[k - 1], p[i]) != 1) --k;\n\t\tfor(int i = n - 2, t = k + 1; i >= 0; ch[k++] = p[i--])\n\t\t\twhile(k >= t && ccw(ch[k - 2], ch[k - 1], p[i]) != 1) --k;\n\t}\n\tch.resize(k - 1);\n\treturn ch;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_3_C\nenum { OUT, ON, IN };\nint contains(const Polygon &Q, const Point &p) {\n\tbool in = false;\n\tconst Real p_y = p.imag();\n\tfor(int i = 0; i < Q.size(); i++) {\n\t\tPoint a = Q[i], b = Q[(i + 1) % Q.size()];\n\t\tif(ccw(a, b, p) == 0) return ON;\n\t\tif(a.imag() > b.imag()) swap(a, b);\n\t\tif(a.imag() <= p_y && p_y < b.imag() && cross(a - p, b - p) < 0) in = !in;\n\t}\n\treturn in ? IN : OUT;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1033\n// 線分の重複除去\nvoid merge_segments(std::vector<Segment> &segs) {\n\tauto merge_if_able = [](Segment &s1, const Segment &s2) {\n\t\tif(abs(cross(s1.b - s1.a, s2.b - s2.a)) > EPS) return false;\n\t\tif(ccw(s1.a, s2.a, s1.b) == 1 || ccw(s1.a, s2.a, s1.b) == -1) return false;\n\t\tif(ccw(s1.a, s1.b, s2.a) == -2 || ccw(s2.a, s2.b, s1.a) == -2) return false;\n\t\ts1 = Segment(min(s1.a, s2.a), max(s1.b, s2.b));\n\t\treturn true;\n\t};\n\n\tfor(int i = 0; i < segs.size(); i++) {\n\t\tif(segs[i].b < segs[i].a) swap(segs[i].a, segs[i].b);\n\t}\n\tfor(int i = 0; i < segs.size(); i++) {\n\t\tfor(int j = i + 1; j < segs.size(); j++) {\n\t\t\tif(merge_if_able(segs[i], segs[j])) {\n\t\t\t\tsegs[j--] = segs.back(), segs.pop_back();\n\t\t\t}\n\t\t}\n\t}\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1033\n// 線分アレンジメント\n// 任意の2線分の交点を頂点としたグラフを構築する\nstd::vector<std::vector<int>> segment_arrangement(std::vector<Segment> &segs, std::vector<Point> &ps) {\n\tstd::vector<std::vector<int>> g;\n\tint N = (int)segs.size();\n\tfor(int i = 0; i < N; i++) {\n\t\tps.emplace_back(segs[i].a);\n\t\tps.emplace_back(segs[i].b);\n\t\tfor(int j = i + 1; j < N; j++) {\n\t\t\tconst Point p1 = segs[i].b - segs[i].a;\n\t\t\tconst Point p2 = segs[j].b - segs[j].a;\n\t\t\tif(eq(cross(p1, p2), 0)) continue;\t// \" == 0\" となっていたのを訂正\n\t\t\tif(intersect(segs[i], segs[j])) {\n\t\t\t\tps.emplace_back(crosspoint(segs[i], segs[j]));\n\t\t\t}\n\t\t}\n\t}\n\tsort(begin(ps), end(ps));\n\tps.erase(unique(begin(ps), end(ps)), end(ps));\t// 誤差?\n\n\tint M = (int)ps.size();\n\tg.resize(M);\n\tfor(int i = 0; i < N; i++) {\n\t\tstd::vector<int> vec;\n\t\tfor(int j = 0; j < M; j++) {\n\t\t\tif(intersect(segs[i], ps[j])) {\n\t\t\t\tvec.emplace_back(j);\n\t\t\t}\n\t\t}\n\t\tfor(int j = 1; j < vec.size(); j++) {\n\t\t\tg[vec[j - 1]].push_back(vec[j]);\n\t\t\tg[vec[j]].push_back(vec[j - 1]);\n\t\t}\n\t}\n\treturn (g);\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_4_C\n// 凸多角形の切断\n// 直線 l.a-l.b で切断しその左側にできる凸多角形を返す\nPolygon convex_cut(const Polygon &U, Line l) {\n\tPolygon ret;\n\tfor(int i = 0; i < U.size(); i++) {\n\t\tPoint now = U[i], nxt = U[(i + 1) % U.size()];\n\t\tif(ccw(l.a, l.b, now) != -1) ret.push_back(now);\n\t\tif(ccw(l.a, l.b, now) * ccw(l.a, l.b, nxt) == -1) ret.push_back(crosspoint(Line(now, nxt), l));\n\t}\n\treturn ret;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_3_A\n// 多角形の面積\nReal area(const Polygon &p) {\n\tReal A = 0;\n\tfor(int i = 0; i < p.size(); ++i) {\n\t\tA += cross(p[i], p[(i + 1) % p.size()]);\n\t}\n\treturn A * 0.5;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_H\n// 円と多角形の共通部分の面積\nReal area(const Polygon &p, const Circle &c) {\n\tif(p.size() < 3) return 0.0;\n\tauto cross_area = [&c](auto cross_area, const Point &a, const Point &b) -> Real {\n\t\tPoint va = c.p - a, vb = c.p - b;\n\t\tReal f = cross(va, vb), ret = 0.0;\n\t\tif(eq(f, 0.0)) return ret;\n\t\tif(std::max(abs(va), abs(vb)) < c.r + EPS) return f;\n\t\tif(distance(Segment(a, b), c.p) > c.r - EPS) return c.r * c.r * arg(vb * conj(va));\n\t\tauto u = crosspoint(c, Segment(a, b));\n\t\tstd::vector<Point> tot{a, u.first, u.second, b};\n\t\tfor(int i = 0; i + 1 < tot.size(); i++) ret += cross_area(cross_area, tot[i], tot[i + 1]);\n\t\treturn ret;\n\t};\n\tReal A = 0;\n\tfor(int i = 0; i < p.size(); i++) {\n\t\tA += cross_area(cross_area, p[i], p[(i + 1) % p.size()]);\n\t}\n\treturn A * 0.5;\n}\n\n#pragma endregion\n\nvoid solve(int cnt) {\n\tint w, h, n;\n\tcin >> w >> h >> n;\n\n\tif(w + h + n == 0) exit(0);\n\tint n2 = n + n;\n\n\tPoints ps(n2);\n\trep(i, n2) { cin >> ps[i]; }\n\n\tif(cnt == 11) {\n\t\tdebug(ps);\n\t}\n\n\tPoints ps_all = ps;\n\tps_all.emplace_back(0, 0);\n\tps_all.emplace_back(0, h);\n\tps_all.emplace_back(w, 0);\n\tps_all.emplace_back(w, h);\n\n\tV<R> ys;\n\n\tLine leftside(Point(0, 0), Point(0, 1));\n\tLine rightside(Point(w, 0), Point(w, 1));\n\n\trep(i, int(ps_all.size())) {\n\t\trep(j, i + 1, int(ps_all.size())) {\n\t\t\tPoint &p = ps_all[i], &r = ps_all[j];\n\t\t\t// if(eq(p.real(), r.real())) continue;\n\t\t\tLine ln(p, r);\n\t\t\tif(intersect(ln, leftside) != 1) continue;\n\t\t\tReal y = crosspoint(leftside, ln).imag();\n\t\t\tif(isnan(y)) continue;\n\t\t\tif(-EPS <= y && y <= h + EPS) ys.push_back(y);\n\t\t}\n\t\tys.push_back(ps_all[i].imag());\n\t}\n\tsort(all(ys));\n\n\tauto kukan = [&](R y, Point &dn, Point &up) {\n\t\tPoint pt(0, y);\n\t\tR low, high;\n\t\tif(eq(dn.real(), 0)) {\n\t\t\tlow = 0;\n\t\t} else {\n\t\t\tlow = max(R(0), crosspoint(rightside, Line(dn, pt)).imag());\n\t\t\tchmin(low, R(h));\n\t\t}\n\n\t\tif(eq(up.real(), 0)) {\n\t\t\thigh = h;\n\t\t} else {\n\t\t\thigh = min(R(h), crosspoint(rightside, Line(up, pt)).imag());\n\t\t\tchmax(high, R(0));\n\t\t}\n\n\t\tif(cnt == 11 && low > high) {\n\t\t\tdebug(low, high);\n\t\t\tdebug(y, dn, up);\n\t\t}\n\n\t\treturn high - low;\n\t\t// return max(high - low, R(0));\n\t};\n\n\tR ans = 0;\n\trep(i, int(ys.size() - 1)) {\n\t\tR y_min = ys[i], y_max = ys[i + 1];\n\t\tif(eq(y_min, y_max)) continue;\n\n\t\tR mid = y_min + y_max;\n\t\tmid /= 2;\n\n\t\tint lb, ub;\n\n\t\tV<pair<R, int>> radians;\n\t\trep(j, n2) {\n\t\t\tauto &p = ps[j];\n\n\t\t\tR deg;\n\t\t\tif(eq(p.real(), 0)) {\n\t\t\t\tdeg = ((p.imag() < mid) ? -PI : PI);\n\t\t\t} else {\n\t\t\t\tdeg = atan((p.imag() - mid) / p.real());\n\t\t\t}\n\n\t\t\tradians.emplace_back(deg, j);\n\t\t}\n\n\t\tsort(all(radians));\n\n\t\tlb = radians[n - 1].second;\n\t\tub = radians[n].second;\n\n\t\tR res = kukan(y_min, ps[lb], ps[ub]);\n\t\tres += kukan(y_max, ps[lb], ps[ub]);\n\t\tres /= 2;\n\t\tres *= (y_max - y_min);\n\n\t\tans += res;\n\t}\n\n\tcout << ans / h / h << dl;\n\treturn;\n}\n\nint main() {\n\tstd::ios::sync_with_stdio(false);\n\tstd::cin.tie(nullptr);\n\tstd::cout << std::fixed << std::setprecision(15);\n\tsrand((unsigned)time(NULL));\n\n\tint cnt = 0;\n\twhile(1) {\n\t\tcnt++;\n\t\tsolve(cnt);\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 1680, "memory_kb": 3924, "score_of_the_acc": -0.8486, "final_rank": 13 }, { "submission_id": "aoj_2256_5800920", "code_snippet": "/*\tauthor: Kite_kuma\n\tcreated: 2021.08.18 20:59:03 */\n\n// #ifdef LOCAL\n// #define _GLIBCXX_DEBUG\n// #endif\n#include <bits/stdc++.h>\nusing namespace std;\n\n#pragma region macros\n\n#define foa(s, v) for(auto &s : v)\n#define all(v) (v).begin(), (v).end()\n#define rall(v) (v).rbegin(), (v).rend()\n#define popcnt(n) __builtin_popcountll((long long)n)\n\n#define REPname(a, b, c, d, e, ...) e\n#define rep(...) REPname(__VA_ARGS__, REP3, REP2, REP1, REP0)(__VA_ARGS__)\n#define REP0(x) for(int Counter_in_rep_macro = 0; Counter_in_rep_macro < (x); ++Counter_in_rep_macro)\n#define REP1(i, x) for(int i = 0; i < (x); ++i)\n#define REP2(i, l, r) for(int i = (l); i < (r); ++i)\n#define REP3(i, l, r, c) for(int i = (l); i < (r); i += (c))\n\n#define DREPname(a, b, c, d, e, ...) e\n#define drep(...) DREPname(__VA_ARGS__, DREP3, DREP2, DREP1)(__VA_ARGS__)\n#define DREP1(i, x) for(int i = (x)-1; i >= 0; --i)\n#define DREP2(i, l, r) for(int i = (r)-1; i >= (l); --i)\n#define DREP3(i, l, r, c) for(int i = (r)-1; i >= (l); i -= (c))\n\n#pragma endregion\n\n#pragma region aliases\n\nusing ll = long long;\nusing ld = long double;\nusing ull = unsigned long long;\nusing vi = std::vector<int>;\nusing vvi = std::vector<std::vector<int>>;\nusing vvvi = std::vector<std::vector<std::vector<int>>>;\nusing vll = std::vector<ll>;\nusing vvll = std::vector<vll>;\nusing vvvll = std::vector<vvll>;\nusing pii = std::pair<int, int>;\nusing pll = std::pair<long long, long long>;\ntemplate <class T = ll>\nusing V = std::vector<T>;\ntemplate <class T = ll>\nusing VV = V<V<T>>;\ntemplate <class T = ll>\nusing VVV = V<V<V<T>>>;\ntemplate <class T = ll>\nusing pqup = std::priority_queue<T, std::vector<T>, std::greater<T>>;\ntemplate <class T = ll>\nusing pqdn = std::priority_queue<T>;\n\n#pragma endregion\n\n#pragma region constants\n\nconst int inf = 1e9;\nconst long long INF = 1e18;\nconst long double pi = acos(-1);\nconst char dl = '\\n';\nconst char sp = ' ';\nint dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};\nint dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};\n\nconst int mod_1000000007 = 1000000007;\nconst int mod_998244353 = 998244353;\n\n#pragma endregion\n\n#pragma region basic_operation\n\ntemplate <class T0, class T1, class T2>\ninline bool in_range(T0 x, T1 lef, T2 rig) {\n\treturn ((lef <= x) && (x < rig));\n}\n\ntemplate <class T>\ninline bool chmin(T &a, T b) {\n\tif(a > b) {\n\t\ta = b;\n\t\treturn true;\n\t}\n\treturn false;\n}\ntemplate <class T>\ninline bool chmax(T &a, T b) {\n\tif(a < b) {\n\t\ta = b;\n\t\treturn true;\n\t}\n\treturn false;\n}\n\nvoid Yes(bool f = 1) { std::cout << (f ? \"Yes\" : \"No\") << '\\n'; }\nvoid No() { std::cout << \"No\\n\"; }\nvoid YES(bool f = 1) { std::cout << (f ? \"YES\" : \"NO\") << '\\n'; }\nvoid NO() { std::cout << \"NO\\n\"; }\n\ntemplate <class T>\nvoid drop(T answer) {\n\tstd::cout << answer << '\\n';\n\texit(0);\n}\n\nvoid err(bool flag = true) {\n\tif(!flag) return;\n\tstd::cout << -1 << '\\n';\n\texit(0);\n}\n\ntemplate <class T>\nvoid vout(std::vector<T> const &v, bool vertically = 0) {\n\tif(vertically) {\n\t\tfor(auto const &a : v) {\n\t\t\tstd::cout << a << '\\n';\n\t\t}\n\t\treturn;\n\t}\n\tfor(auto it = v.begin(); it != v.end(); it++) {\n\t\tstd::cout << (*it);\n\t\tif(it != v.end() - 1) {\n\t\t\tstd::cout << ' ';\n\t\t}\n\t}\n\tstd::cout << '\\n';\n\treturn;\n}\n\ninline void print() { std::cout << '\\n'; }\ntemplate <class T>\ninline void print(T x) {\n\tstd::cout << x << '\\n';\n\treturn;\n}\ntemplate <typename Head, typename... Tail>\nvoid print(Head H, Tail... T) {\n\tstd::cout << H << \" \";\n\tprint(T...);\n}\n\ntemplate <class T>\nvoid add(std::vector<T> &v, T val) {\n\tfor(auto &a : v) a += val;\n\treturn;\n}\n\ntemplate <class T>\nT dup(T a, T b) {\n\tassert(b != 0);\n\treturn (a + b - 1) / b;\n}\n\ntemplate <class T>\nT greatest_lower_multiple(T x, T d) {\n\tif(d == 0) return 0;\n\tif(d < 0) d *= -1;\n\tT y = x % d;\n\tif(y < 0) y += d;\n\treturn x - y;\n}\n\ntemplate <class T>\nT least_upper_multiple(T x, T d) {\n\treturn -greatest_lower_multiple(-x, d);\n}\n\nlong long POW(long long a, long long n) {\n\tlong long res = 1;\n\twhile(n > 0) {\n\t\tif(n & 1) res = res * a;\n\t\ta = a * a;\n\t\tn >>= 1;\n\t}\n\treturn res;\n}\n\nlong long modpow(long long a, long long n, long long mod) {\t // a^n mod\n\tassert(n >= 0 && mod);\n\tif(mod == 1) return 0LL;\n\tlong long res = 1;\n\ta %= mod;\n\twhile(n > 0) {\n\t\tif(n & 1) res = res * a % mod;\n\t\ta = a * a % mod;\n\t\tn >>= 1;\n\t}\n\treturn res < 0 ? res + mod : res;\n}\n\n// return x which satisfies a * x % mod == gcd(a, mod)\n// not (mod | a)\nlong long modinv(long long a, long long mod) {\n\tlong long b = mod, u = 1, v = 0;\n\twhile(b) {\n\t\tlong long t = a / b;\n\t\ta -= t * b;\n\t\tstd::swap(a, b);\n\t\tu -= t * v;\n\t\tstd::swap(u, v);\n\t}\n\tu %= mod;\n\tif(u < 0) u += mod;\n\treturn u;\n}\n\ntemplate <class T>\nint lb(const std::vector<T> &a, const T x) {\n\treturn std::distance(a.begin(), std::lower_bound(a.begin(), a.end(), x));\n}\ntemplate <class T>\nint ub(const std::vector<T> &a, const T x) {\n\treturn std::distance(a.begin(), std::upper_bound(a.begin(), a.end(), x));\n}\ntemplate <class T>\nvoid unq_sort(std::vector<T> &a) {\n\tstd::sort(a.begin(), a.end());\n\ta.erase(std::unique(a.begin(), a.end()), a.end());\n}\ntemplate <class T>\nstd::vector<int> press(std::vector<T> &a) {\n\tauto vec = a;\n\tunq_sort(vec);\n\tstd::vector<int> ret;\n\tfor(auto &v : a) ret.push_back(lb(vec, v));\n\treturn ret;\n}\n\n#pragma endregion\n\n#pragma region input\n#define VEC(type, name, size) \\\n\tvector<type> name(size); \\\n\tscanner::INPUT(name)\n#define VVEC(type, name, h, w) \\\n\tvector<vector<type>> name(h, vector<type>(w)); \\\n\tscanner::INPUT(name)\n#define INT(...) \\\n\tint __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define LL(...) \\\n\tlong long __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define STR(...) \\\n\tstring __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define CHR(...) \\\n\tchar __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define DBL(...) \\\n\tdouble __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define LD(...) \\\n\tlong double __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define TPL3(type0, type1, type2, name) \\\n\tstd::tuple<type0, type1, type2> name; \\\n\tscanner::INPUT(name);\n#define TPL4(type0, type1, type2, type3, name) \\\n\tstd::tuple<type0, type1, type2, type3> name; \\\n\tscanner::INPUT(name);\n\nnamespace scanner {\ntemplate <class T>\nvoid scan(T &a) {\n\tstd::cin >> a;\n}\n\ntemplate <class T, class L>\nvoid scan(std::pair<T, L> &p) {\n\tscan(p.first);\n\tscan(p.second);\n}\n\ntemplate <class T0, class T1, class T2>\nvoid scan(std::tuple<T0, T1, T2> &p) {\n\tT0 t0;\n\tT1 t1;\n\tT2 t2;\n\tscan(t0);\n\tscan(t1);\n\tscan(t2);\n\tp = std::make_tuple(t0, t1, t2);\n}\n\ntemplate <class T0, class T1, class T2, class T3>\nvoid scan(std::tuple<T0, T1, T2, T3> &p) {\n\tT0 t0;\n\tT1 t1;\n\tT2 t2;\n\tT3 t3;\n\tscan(t0);\n\tscan(t1);\n\tscan(t2);\n\tscan(t3);\n\tp = std::make_tuple(t0, t1, t2, t3);\n}\n\ntemplate <class T>\nvoid scan(std::vector<T> &a) {\n\tfor(auto &i : a) scan(i);\n}\n\nvoid INPUT() {}\ntemplate <class Head, class... Tail>\nvoid INPUT(Head &head, Tail &... tail) {\n\tscan(head);\n\tINPUT(tail...);\n}\n} // namespace scanner\n\ntemplate <typename T1, typename T2>\nstd::istream &operator>>(std::istream &is, std::pair<T1, T2> &p) {\n\tis >> p.first >> p.second;\n\treturn is;\n}\n#pragma endregion\n\n#pragma region debug\n\n#pragma region output\ntemplate <typename T1, typename T2>\nstd::ostream &std::operator<<(std::ostream &os, const std::pair<T1, T2> &p) {\n\tos << p.first << \" \" << p.second;\n\treturn os;\n}\ntemplate <class T>\nstd::ostream &std::operator<<(std::ostream &os, const std::vector<T> &v) {\n\tfor(int i = 0; i < (int)v.size(); i++) {\n\t\tif(i) os << \" \";\n\t\tos << v[i];\n\t}\n\treturn os;\n}\n#pragma endregion\n\n#pragma region view\n\nnamespace viewer {\nvoid view(const long long e) {\n\tif(e == INF)\n\t\tstd::cerr << \"INF\";\n\telse if(e == -INF)\n\t\tstd::cerr << \"-INF\";\n\telse\n\t\tstd::cerr << e;\n}\n\nvoid view(const int e) {\n\tif(e == inf)\n\t\tstd::cerr << \"inf\";\n\telse if(e == -inf)\n\t\tstd::cerr << \"-inf\";\n\telse\n\t\tstd::cerr << e;\n}\n\ntemplate <typename T>\nvoid view(const T e) {\n\tstd::cerr << e;\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::pair<T, U> &p) {\n\tstd::cerr << \"(\";\n\tview(p.first);\n\tstd::cerr << \", \";\n\tview(p.second);\n\tstd::cerr << \")\";\n}\n\ntemplate <class T0, class T1, class T2>\nvoid view(const std::tuple<T0, T1, T2> &p) {\n\tstd::cerr << \"(\";\n\tview(std::get<0>(p));\n\tstd::cerr << \", \";\n\tview(std::get<1>(p));\n\tstd::cerr << \", \";\n\tview(std::get<2>(p));\n\tstd::cerr << \")\";\n}\n\ntemplate <class T0, class T1, class T2, class T3>\nvoid view(const std::tuple<T0, T1, T2, T3> &p) {\n\tstd::cerr << \"(\";\n\tview(std::get<0>(p));\n\tstd::cerr << \", \";\n\tview(std::get<1>(p));\n\tstd::cerr << \", \";\n\tview(std::get<2>(p));\n\tstd::cerr << \", \";\n\tview(std::get<3>(p));\n\tstd::cerr << \")\";\n}\n\ntemplate <typename T>\nvoid view(const std::set<T> &s) {\n\tif(s.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << \"{ \";\n\tfor(auto &t : s) {\n\t\tview(t);\n\t\tstd::cerr << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\n\ntemplate <typename T>\nvoid view(const std::unordered_set<T> &s) {\n\tif(s.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << \"{ \";\n\tfor(auto &t : s) {\n\t\tview(t);\n\t\tstd::cerr << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\n\ntemplate <typename T>\nvoid view(const std::vector<T> &v) {\n\tif(v.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << \"{ \";\n\tfor(const auto &e : v) {\n\t\tview(e);\n\t\tstd::cerr << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\n\ntemplate <typename T>\nvoid view(const std::vector<std::vector<T>> &vv) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &v : vv) {\n\t\tstd::cerr << \"\\t\";\n\t\tview(v);\n\t\tstd::cerr << '\\n';\n\t}\n\tstd::cerr << \"}\";\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::vector<std::pair<T, U>> &v) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &c : v) {\n\t\tstd::cerr << \"\\t(\";\n\t\tview(c.first);\n\t\tstd::cerr << \", \";\n\t\tview(c.second);\n\t\tstd::cerr << \")\\n\";\n\t}\n\tstd::cerr << \"}\";\n}\n\ntemplate <class T0, class T1, class T2>\nvoid view(const std::vector<std::tuple<T0, T1, T2>> &v) {\n\tif(v.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << '{';\n\tfor(const auto &t : v) {\n\t\tstd::cerr << \"\\n\\t\";\n\t\tview(t);\n\t\tstd::cerr << \",\";\n\t}\n\tstd::cerr << \"\\n}\";\n}\n\ntemplate <class T0, class T1, class T2, class T3>\nvoid view(const std::vector<std::tuple<T0, T1, T2, T3>> &v) {\n\tif(v.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << '{';\n\tfor(const auto &t : v) {\n\t\tstd::cerr << \"\\n\\t\";\n\t\tview(t);\n\t\tstd::cerr << \",\";\n\t}\n\tstd::cerr << \"\\n}\";\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::map<T, U> &m) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &t : m) {\n\t\tstd::cerr << \"\\t[\";\n\t\tview(t.first);\n\t\tstd::cerr << \"] : \";\n\t\tview(t.second);\n\t\tstd::cerr << '\\n';\n\t}\n\tstd::cerr << \"}\";\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::unordered_map<T, U> &m) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &t : m) {\n\t\tstd::cerr << \"\\t[\";\n\t\tview(t.first);\n\t\tstd::cerr << \"] : \";\n\t\tview(t.second);\n\t\tstd::cerr << '\\n';\n\t}\n\tstd::cerr << \"}\";\n}\n} // namespace viewer\n\n#pragma endregion\n\n// when debugging : g++ foo.cpp -DLOCAL\n#ifdef LOCAL\nvoid debug_out() {}\ntemplate <typename Head, typename... Tail>\nvoid debug_out(Head H, Tail... T) {\n\tviewer::view(H);\n\tstd::cerr << \", \";\n\tdebug_out(T...);\n}\n#define debug(...) \\\n\tdo { \\\n\t\tstd::cerr << __LINE__ << \" [\" << #__VA_ARGS__ << \"] : [\"; \\\n\t\tdebug_out(__VA_ARGS__); \\\n\t\tstd::cerr << \"\\b\\b]\\n\"; \\\n\t} while(0)\n#define dump(x) \\\n\tdo { \\\n\t\tstd::cerr << __LINE__ << \" \" << #x << \" : \"; \\\n\t\tviewer::view(x); \\\n\t\tstd::cerr << '\\n'; \\\n\t} while(0)\n\n#else\n#define debug(...) (void(0))\n#define dump(x) (void(0))\n#endif\n\n#pragma endregion\n\n#pragma region geometry\n\nusing Real = long double;\nusing R = Real;\nusing Point = std::complex<Real>;\nusing Vec = Point;\nconst Real EPS = 1e-10, PI = acos(-1);\n\ninline bool eq(const Real &a, const Real &b) { return fabs(b - a) < EPS; }\ninline bool eq(const Point &a, const Point &b) { return fabs(b - a) < EPS; }\n/*\n\t-1: a < b\n\t0 : a == b\n\t1 : a > b\n*/\ninline int compare(const Real &a, const Real &b) { return eq(a, b) ? 0 : a < b ? -1 : 1; }\n\nnamespace std {\nconst Point operator*(const Point &p, const Real &d) { return Point(real(p) * d, imag(p) * d); }\n} // namespace std\n\nstd::istream &operator>>(std::istream &is, Point &p) {\n\tReal a, b;\n\tis >> a >> b;\n\tp = Point(a, b);\n\treturn is;\n}\nstd::ostream &operator<<(std::ostream &os, Point &p) { return os << std::fixed << std::setprecision(10) << p.real() << \" \" << p.imag(); }\n\n// rotate point 'p' for counter clockwise direction\nconst Point rotate(const Real &theta, const Point &p) {\n\treturn Point(cos(theta) * p.real() - sin(theta) * p.imag(), sin(theta) * p.real() + cos(theta) * p.imag());\n}\n\nconst Real radian_to_degree(const Real &r) { return (r * 180.0 / PI); }\n\nconst Real degree_to_radian(const Real &d) { return (d * PI / 180.0); }\n\n// get angle a-b-c (<pi)\nconst Real get_angle(const Point &a, const Point &b, const Point &c) {\n\tconst Point v(a - b), w(c - b);\n\tReal theta = fabs(atan2(w.imag(), w.real()) - atan2(v.imag(), v.real()));\n\treturn std::min(theta, 2 * PI - theta);\n}\n\nnamespace std {\nbool operator<(const Point &a, const Point &b) { return a.real() != b.real() ? a.real() < b.real() : a.imag() < b.imag(); }\n} // namespace std\n\nstruct Line {\n\tPoint a, b;\n\n\tLine() = default;\n\n\tLine(Point a, Point b) : a(a), b(b) {}\n\n\tLine(Real A, Real B, Real C) // Ax + By = C\n\t{\n\t\tif(eq(A, 0))\n\t\t\ta = Point(0, C / B), b = Point(1, C / B);\n\t\telse if(eq(B, 0))\n\t\t\tb = Point(C / A, 0), b = Point(C / A, 1);\n\t\telse\n\t\t\ta = Point(0, C / B), b = Point(C / A, 0);\n\t}\n\n\tfriend std::ostream &operator<<(std::ostream &os, Line &p) { return os << p.a << \" -- \" << p.b; }\n\n\tfriend std::istream &operator>>(std::istream &is, Line &a) { return is >> a.a >> a.b; }\n};\n\nstruct Segment : Line {\n\tSegment() = default;\n\n\tSegment(Point a, Point b) : Line(a, b) {}\n};\n\nstruct Circle {\n\tPoint p;\n\tReal r;\n\n\tCircle() = default;\n\n\tCircle(Point p, Real r) : p(p), r(r) {}\n};\n\nusing Points = std::vector<Point>;\nusing Polygon = std::vector<Point>;\nusing Segments = std::vector<Segment>;\nusing Lines = std::vector<Line>;\nusing Circles = std::vector<Circle>;\n\nconst Real cross(const Point &a, const Point &b) { return real(a) * imag(b) - imag(a) * real(b); }\nconst Real dot(const Point &a, const Point &b) { return real(a) * real(b) + imag(a) * imag(b); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_C\nconst int ccw(const Point &a, Point b, Point c) {\n\tb = b - a, c = c - a;\n\tif(cross(b, c) > EPS) return +1;\t\t// \"COUNTER_CLOCKWISE\"\n\tif(cross(b, c) < -EPS) return -1;\t\t// \"CLOCKWISE\"\n\tif(dot(b, c) < -EPS) return +2;\t\t\t// \"ONLINE_BACK\"\n\tif(norm(b) + EPS < norm(c)) return -2;\t// \"ONLINE_FRONT\"\n\treturn 0;\t\t\t\t\t\t\t\t// \"ON_SEGMENT\"\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_A\nbool parallel(const Line &a, const Line &b) { return eq(cross(a.b - a.a, b.b - b.a), 0.0); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_A\nbool orthogonal(const Line &a, const Line &b) { return eq(dot(a.a - a.b, b.a - b.b), 0.0); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_A\nPoint projection(const Line &l, const Point &p) {\n\tdouble t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n\treturn l.a + (l.a - l.b) * t;\n}\n\nPoint projection(const Segment &l, const Point &p) {\n\tdouble t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n\treturn l.a + (l.a - l.b) * t;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_B\nPoint reflection(const Line &l, const Point &p) { return projection(l, p) * 2.0 - p; }\n\nint intersect(const Line &l, const Point &p) { return int(abs(ccw(l.a, l.b, p)) != 1); }\n\nint intersect(const Line &l, const Line &m) {\n\tif(intersect(l, m.a) && intersect(l, m.b)) return -1;\n\treturn int(abs(cross(l.b - l.a, m.b - m.a)) > EPS);\n}\n\nint intersect(const Segment &s, const Point &p) { return int(ccw(s.a, s.b, p) == 0); }\n\nint intersect(const Line &l, const Segment &s) {\n\tif(intersect(l, s.a) && intersect(l, s.b)) return -1;\n\treturn cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < EPS;\n}\n\nReal distance(const Line &l, const Point &p);\n\nint intersect(const Circle &c, const Line &l) {\n\tReal d = c.r - distance(l, c.p);\n\treturn fabs(d) < EPS ? 1 : d > 0. ? 2 : 0;\n}\n\nint intersect(const Circle &c, const Point &p) { return int(abs(abs(p - c.p) - c.r) < EPS); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_B\nint intersect(const Segment &s, const Segment &t) {\n\tif(eq(s.a, s.b)) return intersect(t, s.a);\n\tif(intersect(Line(s), t.a) && intersect(Line(s), t.b) &&\n\t std::max(std::min(s.a, s.b), std::min(t.a, t.b)) < std::min(std::max(s.a, s.b), std::max(t.a, t.b)))\n\t\treturn -1;\n\treturn int(ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 && ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0);\n}\n\nint intersect(const Circle &c, const Segment &l) {\n\tconst Point h = projection(l, c.p);\n\tif(norm(h - c.p) - c.r * c.r > EPS) return 0;\n\tauto d1 = abs(c.p - l.a), d2 = abs(c.p - l.b);\n\tif(compare(d1, c.r) == -1 && compare(d2, c.r) == -1) return 0;\n\tif(d1 < c.r - EPS && d2 > c.r - EPS || d1 > c.r - EPS && d2 < c.r - EPS) return 1;\n\treturn dot(l.a - h, l.b - h) < 0 ? 2 : 0;\n}\n\nReal distance(const Point &a, const Point &b);\n\nint number_of_common_tangents(const Circle &c1, const Circle &c2) {\n\tReal r1 = std::min(c1.r, c2.r), r2 = std::max(c1.r, c2.r), d = distance(c1.p, c2.p);\n\tint com = compare(r1 + r2, d);\n\treturn com == 1 ? compare(d + r1, r2) + 1 : 3 - com;\n\t// if(c1.r < c2.r) swap(c1, c2);\n\t// Real d = abs(c1.p - c2.p);\n\t// if(compare(c1.r + c2.r, d) == -1) return 4;\n\t// if(eq(c1.r + c2.r, d)) return 3;\n\t// if(compare(c1.r - c2.r, d) == -1) return 2;\n\t// return int(eq(c1.r - c2.r, d));\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_A&lang=jp\nint intersect(const Circle &c1, const Circle &c2) { return 2 - abs(2 - number_of_common_tangents(c1, c2)); }\n\nReal distance(const Point &a, const Point &b) { return abs(a - b); }\n\nReal distance(const Line &l, const Point &p) { return abs(p - projection(l, p)); }\n\nReal distance(const Line &l, const Line &m) { return intersect(l, m) ? 0 : distance(l, m.a); }\n\nReal distance(const Segment &s, const Point &p) {\n\tPoint r = projection(s, p);\n\treturn intersect(s, r) ? distance(r, p) : std::min(abs(s.a - p), abs(s.b - p));\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_D\nReal distance(const Segment &a, const Segment &b) {\n\tif(intersect(a, b)) return 0;\n\treturn std::min({distance(a, b.a), distance(a, b.b), distance(b, a.a), distance(b, a.b)});\n}\n\nReal distance(const Line &l, const Segment &s) {\n\tif(intersect(l, s)) return 0;\n\treturn std::min(distance(l, s.a), distance(l, s.b));\n}\n\nPoint crosspoint(const Line &l, const Line &m) {\n\tReal A = cross(l.b - l.a, m.b - m.a);\n\tReal B = cross(l.b - l.a, l.b - m.a);\n\tif(eq(abs(A), 0.0) && eq(abs(B), 0.0)) return m.a;\n\treturn m.a + (m.b - m.a) * B / A;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_C\nPoint crosspoint(const Segment &l, const Segment &m) { return crosspoint(Line(l), Line(m)); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_D\nstd::pair<Point, Point> crosspoint(const Circle &c, const Line l) {\n\tPoint pr = projection(l, c.p);\n\tif(eq(distance(l, c.p), c.r)) return {pr, pr};\n\tVec v = (l.b - l.a) / abs(l.b - l.a) * sqrt(c.r * c.r - norm(pr - c.p));\n\treturn make_pair(pr - v, pr + v);\n\t// Vec e = (l.b - l.a) / abs(l.b - l.a);\n\t// double base = sqrt(c.r * c.r - norm(pr - c.p));\n\t// return {pr - e * base, pr + e * base};\n}\n\nstd::pair<Point, Point> crosspoint(const Circle &c, const Segment &l) {\n\tif(intersect(c, l) == 2) return crosspoint(c, Line(l.a, l.b));\n\tauto ret = crosspoint(c, Line(l.a, l.b));\n\tif(dot(l.a - ret.first, l.b - ret.first) < EPS)\n\t\tret.second = ret.first;\n\telse\n\t\tret.first = ret.second;\n\treturn ret;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_E\nstd::pair<Point, Point> crosspoint(const Circle &c1, const Circle &c2) {\n\tReal d = abs(c1.p - c2.p);\n\tReal a = acos((c1.r * c1.r + d * d - c2.r * c2.r) / (2 * c1.r * d)); // cosine theorem\n\tReal t = atan2(c2.p.imag() - c1.p.imag(), c2.p.real() - c1.p.real());\n\tPoint p1 = c1.p + Point(cos(t + a) * c1.r, sin(t + a) * c1.r);\n\tPoint p2 = c1.p + Point(cos(t - a) * c1.r, sin(t - a) * c1.r);\n\treturn make_pair(p1, p2);\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_F\n// \"点 p を通る円 c の接線\"の接点2つ\nstd::pair<Point, Point> tangent(const Circle &c1, const Point &p2) {\n\treturn crosspoint(c1, Circle(p2, sqrt(norm(c1.p - p2) - c1.r * c1.r)));\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_G\n// 円 c1, c2 の共通接線\nLines tangent(Circle c1, Circle c2) {\n\tLines ret;\n\tif(c1.r < c2.r) std::swap(c1, c2);\n\tReal g = norm(c1.p - c2.p);\n\tif(eq(g, 0)) return ret;\n\tVec u = (c2.p - c1.p) / Real(sqrt(g));\n\tVec v = rotate(PI * 0.5, u);\n\tfor(int s : {-1, 1}) {\n\t\tReal h = (c1.r + s * c2.r) / sqrt(g);\n\t\tif(eq(1 - h * h, 0)) {\n\t\t\tret.emplace_back(c1.p + u * c1.r, c1.p + (u + v) * c1.r);\n\t\t} else if(1 - h * h > 0) {\n\t\t\tPoint uu = u * h, vv = v * sqrt(1 - h * h);\n\t\t\tret.emplace_back(c1.p + (uu + vv) * c1.r, c2.p - (uu + vv) * c2.r * s);\n\t\t\tret.emplace_back(c1.p + (uu - vv) * c1.r, c2.p - (uu - vv) * c2.r * s);\n\t\t}\n\t}\n\treturn ret;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_3_B\nbool is_convex(const Polygon &p, bool pi_is_ok = false) {\n\tint n = p.size();\n\tif(pi_is_ok) {\n\t\tfor(int i = 0; i < n; i++)\n\t\t\tif(ccw(p[i], p[(i + 1) % n], p[(i + 2) % n]) == -1) return false;\n\t} else {\n\t\tfor(int i = 0; i < n; i++)\n\t\t\tif(ccw(p[i], p[(i + 1) % n], p[(i + 2) % n]) != 1) return false;\n\t}\n\treturn true;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_4_A\nPolygon convex_hull(Polygon &p, bool pi_is_ok = false) {\n\tint n = (int)p.size(), k = 0;\n\tif(n <= 2) return p;\n\tsort(p.begin(), p.end());\n\tPoints ch(2 * n);\n\tif(pi_is_ok) {\n\t\tfor(int i = 0; i < n; ch[k++] = p[i++])\n\t\t\twhile(k >= 2 && ccw(ch[k - 2], ch[k - 1], p[i]) == -1) --k;\n\t\tfor(int i = n - 2, t = k + 1; i >= 0; ch[k++] = p[i--])\n\t\t\twhile(k >= t && ccw(ch[k - 2], ch[k - 1], p[i]) == -1) --k;\n\t} else {\n\t\tfor(int i = 0; i < n; ch[k++] = p[i++])\n\t\t\twhile(k >= 2 && ccw(ch[k - 2], ch[k - 1], p[i]) != 1) --k;\n\t\tfor(int i = n - 2, t = k + 1; i >= 0; ch[k++] = p[i--])\n\t\t\twhile(k >= t && ccw(ch[k - 2], ch[k - 1], p[i]) != 1) --k;\n\t}\n\tch.resize(k - 1);\n\treturn ch;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_3_C\nenum { OUT, ON, IN };\nint contains(const Polygon &Q, const Point &p) {\n\tbool in = false;\n\tconst Real p_y = p.imag();\n\tfor(int i = 0; i < Q.size(); i++) {\n\t\tPoint a = Q[i], b = Q[(i + 1) % Q.size()];\n\t\tif(ccw(a, b, p) == 0) return ON;\n\t\tif(a.imag() > b.imag()) swap(a, b);\n\t\tif(a.imag() <= p_y && p_y < b.imag() && cross(a - p, b - p) < 0) in = !in;\n\t}\n\treturn in ? IN : OUT;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1033\n// 線分の重複除去\nvoid merge_segments(std::vector<Segment> &segs) {\n\tauto merge_if_able = [](Segment &s1, const Segment &s2) {\n\t\tif(abs(cross(s1.b - s1.a, s2.b - s2.a)) > EPS) return false;\n\t\tif(ccw(s1.a, s2.a, s1.b) == 1 || ccw(s1.a, s2.a, s1.b) == -1) return false;\n\t\tif(ccw(s1.a, s1.b, s2.a) == -2 || ccw(s2.a, s2.b, s1.a) == -2) return false;\n\t\ts1 = Segment(min(s1.a, s2.a), max(s1.b, s2.b));\n\t\treturn true;\n\t};\n\n\tfor(int i = 0; i < segs.size(); i++) {\n\t\tif(segs[i].b < segs[i].a) swap(segs[i].a, segs[i].b);\n\t}\n\tfor(int i = 0; i < segs.size(); i++) {\n\t\tfor(int j = i + 1; j < segs.size(); j++) {\n\t\t\tif(merge_if_able(segs[i], segs[j])) {\n\t\t\t\tsegs[j--] = segs.back(), segs.pop_back();\n\t\t\t}\n\t\t}\n\t}\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1033\n// 線分アレンジメント\n// 任意の2線分の交点を頂点としたグラフを構築する\nstd::vector<std::vector<int>> segment_arrangement(std::vector<Segment> &segs, std::vector<Point> &ps) {\n\tstd::vector<std::vector<int>> g;\n\tint N = (int)segs.size();\n\tfor(int i = 0; i < N; i++) {\n\t\tps.emplace_back(segs[i].a);\n\t\tps.emplace_back(segs[i].b);\n\t\tfor(int j = i + 1; j < N; j++) {\n\t\t\tconst Point p1 = segs[i].b - segs[i].a;\n\t\t\tconst Point p2 = segs[j].b - segs[j].a;\n\t\t\tif(eq(cross(p1, p2), 0)) continue;\t// \" == 0\" となっていたのを訂正\n\t\t\tif(intersect(segs[i], segs[j])) {\n\t\t\t\tps.emplace_back(crosspoint(segs[i], segs[j]));\n\t\t\t}\n\t\t}\n\t}\n\tsort(begin(ps), end(ps));\n\tps.erase(unique(begin(ps), end(ps)), end(ps));\t// 誤差?\n\n\tint M = (int)ps.size();\n\tg.resize(M);\n\tfor(int i = 0; i < N; i++) {\n\t\tstd::vector<int> vec;\n\t\tfor(int j = 0; j < M; j++) {\n\t\t\tif(intersect(segs[i], ps[j])) {\n\t\t\t\tvec.emplace_back(j);\n\t\t\t}\n\t\t}\n\t\tfor(int j = 1; j < vec.size(); j++) {\n\t\t\tg[vec[j - 1]].push_back(vec[j]);\n\t\t\tg[vec[j]].push_back(vec[j - 1]);\n\t\t}\n\t}\n\treturn (g);\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_4_C\n// 凸多角形の切断\n// 直線 l.a-l.b で切断しその左側にできる凸多角形を返す\nPolygon convex_cut(const Polygon &U, Line l) {\n\tPolygon ret;\n\tfor(int i = 0; i < U.size(); i++) {\n\t\tPoint now = U[i], nxt = U[(i + 1) % U.size()];\n\t\tif(ccw(l.a, l.b, now) != -1) ret.push_back(now);\n\t\tif(ccw(l.a, l.b, now) * ccw(l.a, l.b, nxt) == -1) ret.push_back(crosspoint(Line(now, nxt), l));\n\t}\n\treturn ret;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_3_A\n// 多角形の面積\nReal area(const Polygon &p) {\n\tReal A = 0;\n\tfor(int i = 0; i < p.size(); ++i) {\n\t\tA += cross(p[i], p[(i + 1) % p.size()]);\n\t}\n\treturn A * 0.5;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_H\n// 円と多角形の共通部分の面積\nReal area(const Polygon &p, const Circle &c) {\n\tif(p.size() < 3) return 0.0;\n\tauto cross_area = [&c](auto cross_area, const Point &a, const Point &b) -> Real {\n\t\tPoint va = c.p - a, vb = c.p - b;\n\t\tReal f = cross(va, vb), ret = 0.0;\n\t\tif(eq(f, 0.0)) return ret;\n\t\tif(std::max(abs(va), abs(vb)) < c.r + EPS) return f;\n\t\tif(distance(Segment(a, b), c.p) > c.r - EPS) return c.r * c.r * arg(vb * conj(va));\n\t\tauto u = crosspoint(c, Segment(a, b));\n\t\tstd::vector<Point> tot{a, u.first, u.second, b};\n\t\tfor(int i = 0; i + 1 < tot.size(); i++) ret += cross_area(cross_area, tot[i], tot[i + 1]);\n\t\treturn ret;\n\t};\n\tReal A = 0;\n\tfor(int i = 0; i < p.size(); i++) {\n\t\tA += cross_area(cross_area, p[i], p[(i + 1) % p.size()]);\n\t}\n\treturn A * 0.5;\n}\n\n#pragma endregion\n\nvoid solve() {\n\tint w, h, n;\n\tcin >> w >> h >> n;\n\n\tif(w + h + n == 0) exit(0);\n\tint n2 = n + n;\n\n\tPoints ps(n2);\n\trep(i, n2) { cin >> ps[i]; }\n\n\tPoints ps_all = ps;\n\tps_all.emplace_back(0, 0);\n\tps_all.emplace_back(0, h);\n\tps_all.emplace_back(w, 0);\n\tps_all.emplace_back(w, h);\n\n\tV<R> ys;\n\n\tLine leftside(Point(0, 0), Point(0, 1));\n\tLine rightside(Point(w, 0), Point(w, 1));\n\n\trep(i, int(ps_all.size())) {\n\t\trep(j, i + 1, int(ps_all.size())) {\n\t\t\tPoint &p = ps_all[i], &r = ps_all[j];\n\t\t\t// if(eq(p.real(), r.real())) continue;\n\t\t\tLine ln(p, r);\n\t\t\tif(intersect(ln, leftside) != 1) continue;\n\t\t\tReal y = crosspoint(leftside, ln).imag();\n\t\t\tif(isnan(y)) continue;\n\t\t\tif(-EPS <= y && y <= h + EPS) ys.push_back(y);\n\t\t}\n\t\tys.push_back(ps_all[i].imag());\n\t}\n\tsort(all(ys));\n\n\tdebug(ys);\n\n\tauto kukan = [&](R y, Point &dn, Point &up) {\n\t\tPoint pt(0, y);\n\t\tR low, high;\n\t\tif(eq(dn.real(), 0)) {\n\t\t\tlow = 0;\n\t\t} else {\n\t\t\tlow = max(R(0), crosspoint(rightside, Line(dn, pt)).imag());\n\t\t}\n\n\t\tif(eq(up.real(), 0)) {\n\t\t\thigh = h;\n\t\t} else {\n\t\t\thigh = min(R(h), crosspoint(rightside, Line(up, pt)).imag());\n\t\t}\n\t\treturn max(high - low, R(0));\n\t};\n\n\tR ans = 0;\n\trep(i, int(ys.size() - 1)) {\n\t\tR y_min = ys[i], y_max = ys[i + 1];\n\t\tif(eq(y_min, y_max)) continue;\n\t\tdebug(y_min, y_max);\n\t\tR mid = y_min + y_max;\n\t\tmid /= 2;\n\n\t\tint lb, ub;\n\n\t\tV<pair<R, int>> radians;\n\t\trep(j, n2) {\n\t\t\tauto &p = ps[j];\n\n\t\t\tR deg;\n\t\t\tif(eq(p.real(), 0)) {\n\t\t\t\tdeg = ((p.imag() < mid) ? -PI : PI);\n\t\t\t} else {\n\t\t\t\tdeg = atan((p.imag() - mid) / p.real());\n\t\t\t}\n\n\t\t\tradians.emplace_back(deg, j);\n\t\t}\n\n\t\tsort(all(radians));\n\n\t\tlb = radians[n - 1].second;\n\t\tub = radians[n].second;\n\n\t\tdebug(lb, ub);\n\t\tdebug(ps[lb], ps[ub]);\n\n\t\tR res = kukan(y_min, ps[lb], ps[ub]);\n\t\tres += kukan(y_max, ps[lb], ps[ub]);\n\t\tres /= 2;\n\t\tres *= (y_max - y_min);\n\n\t\tans += res;\n\n\t\tdebug(res, ans);\n\t}\n\n\tcout << ans / h / h << dl;\n\treturn;\n}\n\nint main() {\n\tstd::ios::sync_with_stdio(false);\n\tstd::cin.tie(nullptr);\n\tstd::cout << std::fixed << std::setprecision(15);\n\tsrand((unsigned)time(NULL));\n\n\twhile(1) solve();\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 1630, "memory_kb": 3928, "score_of_the_acc": -0.8441, "final_rank": 12 }, { "submission_id": "aoj_2256_5764085", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntemplate<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }\ntemplate<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }\n#define all(x) (x).begin(),(x).end()\n#define fi first\n#define se second\n#define mp make_pair\n#define si(x) int(x.size())\nconst int mod=1000000007,MAX=205,INF=1<<30;\n//#define double long double\n//幾何ライブラリ\n\nconst double eps=1e-10;\nconst double pi=acos((double)-1.0L);\n#define equals(a,b) (fabs((a)-(b))<eps)\n\ndouble torad(double deg) {return (double)(deg)*pi/180.0;}\ndouble todeg(double ang) {return ang*180.0/pi;}\n\nclass Point{\npublic:\n double x,y;\n \n Point(double x=0,double y=0):x(x),y(y){}\n \n Point operator + (Point p){return Point(x+p.x,y+p.y);}\n Point operator - (Point p){return Point(x-p.x,y-p.y);}\n Point operator * (double a){return Point(a*x,a*y);}\n Point operator / (double a){return Point(x/a,y/a);}\n \n double abs(){return sqrt(norm());}\n double norm(){return x*x+y*y;}\n \n bool operator < (const Point &p)const{\n return x+eps<p.x||(equals(x,p.x)&&y+eps<p.y);\n }\n \n bool operator == (const Point &p)const{\n return fabs(x-p.x)<eps&&fabs(y-p.y)<eps;\n }\n};\n\ntypedef Point Vector;\n\ndouble norm(Vector a){\n return a.x*a.x+a.y*a.y;\n}\n\ndouble abs(Vector a){\n return sqrt(norm(a));\n}\n\ndouble dot(Vector a,Vector b){\n return a.x*b.x+a.y*b.y;\n}\n\ndouble cross(Vector a,Vector b){\n return a.x*b.y-a.y*b.x;\n}\n\nstruct Segment{\n Point p1,p2;\n};\n\nbool isOrthogonal(Vector a,Vector b){\n return equals(dot(a,b),0.0);\n}\n\nbool isOrthogonal(Point a1,Point a2,Point b1,Point b2){\n return isOrthogonal(a1-a2,b1-b2);\n}\n\nbool isOrthogonal(Segment s1,Segment s2){\n return equals(dot(s1.p2-s1.p1,s2.p2-s2.p1),0.0);\n}\n\nbool isParallel(Vector a,Vector b){\n return equals(cross(a,b),0.0);\n}\n\nbool isParallel(Point a1,Point a2,Point b1,Point b2){\n return isParallel(a1-a2,b1-b2);\n}\n\nbool isParallel(Segment s1,Segment s2){\n return equals(cross(s1.p2-s1.p1,s2.p2-s2.p1),0.0);\n}\n\nPoint project(Segment s,Point p){\n Vector base=s.p2-s.p1;\n double r=dot(p-s.p1,base)/norm(base);\n return s.p1+base*r;\n}\n\nPoint reflect(Segment s,Point p){\n return p+(project(s,p)-p)*2.0;\n}\n\nPoint turn(Point p,Point c,double pi){\n double q=atan2(p.y-c.y,p.x-c.x);\n q+=pi;\n p=c+Point{cos(q)*abs(p-c),sin(q)*abs(p-c)};\n \n return p;\n}\n//pをcを中心としてpi回転させる(1周で2π)\n//p=cのときnan\n\n//p0,p1,p2の順に見たときどうなるか?\n\nstatic const int counter_clockwise=1;\nstatic const int clockwise=-1;\nstatic const int online_back=2;\nstatic const int online_front=-2;\nstatic const int on_segment=0;\n\nint ccw(Point p0,Point p1,Point p2){\n Vector a=p1-p0;\n Vector b=p2-p0;\n \n if(cross(a,b)>eps) return counter_clockwise;\n if(cross(a,b)<-eps) return clockwise;\n if(dot(a,b)<-eps) return online_back;\n if(a.norm()<b.norm()) return online_front;\n \n return on_segment;\n}\n\nbool intersect(Point p1,Point p2,Point p3,Point p4){\n return(ccw(p1,p2,p3)*ccw(p1,p2,p4)<=0&&ccw(p3,p4,p1)*ccw(p3,p4,p2)<=0);\n}\n\nbool intersect(Segment s1,Segment s2){\n return intersect(s1.p1,s1.p2,s2.p1,s2.p2);\n}\n\nbool overlap(Segment s1,Segment s2){\n int a=ccw(s1.p1,s1.p2,s2.p1),b=ccw(s1.p1,s1.p2,s2.p2);\n if(a&1||b&1) return 0;\n if(a==2){\n if(b==-2||(b==0&&!(s2.p2==s1.p1))) return 1;\n else return 0;\n }\n if(a==-2){\n if(b==2||(b==0&&!(s2.p2==s1.p2))) return 1;\n else return 0;\n }\n if(a==0){\n if(s1.p1==s2.p1){\n if(b!=2) return 1;\n else return 0;\n }\n else if(s1.p2==s2.p1){\n if(b!=-2) return 1;\n else return 0;\n }\n else return 1;\n }\n return 0;\n}\n//s1とs2の共通の線分(長さ0より大きい)があるかどうか\n\ntypedef Segment Line;\n\ndouble getDistance(Point a,Point b){\n return abs(a-b);\n}\n\ndouble getDistanceLP(Line l,Point p){\n return abs(cross(l.p2-l.p1,p-l.p1)/abs(l.p2-l.p1));\n}\n\ndouble getDistanceSP(Segment s,Point p){\n if(dot(s.p2-s.p1,p-s.p1)<0.0) return abs(p-s.p1);\n if(dot(s.p1-s.p2,p-s.p2)<0.0) return abs(p-s.p2);\n return getDistanceLP(s,p);\n}\n\ndouble getDistance(Segment s1,Segment s2){\n if(intersect(s1,s2)) return 0.0;\n return min({getDistanceSP(s1,s2.p1),getDistanceSP(s1,s2.p2),getDistanceSP(s2,s1.p1),getDistanceSP(s2,s1.p2)});\n}\n\nPoint getCrossPointS(Segment s1,Segment s2){\n //if(ccw(s1.p1,s1.p2,s2.p1)==0&&ccw(s1.p1,s1.p2,s2.p2)==0) return s1.p1;\n Vector base=s2.p2-s2.p1;\n double d1=abs(cross(base,s1.p1-s2.p1));\n double d2=abs(cross(base,s1.p2-s2.p1));\n double t=d1/(d1+d2);\n return s1.p1+(s1.p2-s1.p1)*t;\n}//同じ時壊れます\n\nPoint getCrossPointL(Line l1,Line l2){\n //if(ccw(s1.p1,s1.p2,s2.p1)==0&&ccw(s1.p1,s1.p2,s2.p2)==0) return s1.p1;\n \n Vector v1=l1.p2-l1.p1,v2=l2.p2-l2.p1;\n \n return l1.p1+v1*cross(v2,l2.p1-l1.p1)/cross(v2,v1);\n}\n\nSegment ParallelSegment(Segment s,double d){\n Vector v={-(s.p2-s.p1).y,(s.p2-s.p1).x};\n v=v/abs(v);\n \n s.p1=s.p1+v*d;\n s.p2=s.p2+v*d;\n \n return s;\n}\n\nPoint naisin(Point p1,Point p2,Point p3){\n if(p1==p2&&p2==p3&&p3==p1) return p1;\n \n return (p1*abs(p2-p3)+p2*abs(p1-p3)+p3*abs(p1-p2))/(abs(p2-p3)+abs(p1-p3)+abs(p1-p2));\n}\n\nPoint naisin(Line l1,Line l2,Line l3){\n //平行でない前提\n \n Point p1=getCrossPointL(l1,l2),p2=getCrossPointL(l1,l3),p3=getCrossPointL(l2,l3);\n return naisin(p1,p2,p3);\n}\n\n//ネットの適当を書いたのであってるか全く知りません→あってそう\n\nclass Circle{\npublic:\n Point c;\n double r;\n Circle(Point c=Point(),double r=0.0):c(c),r(r){}\n};\n\nPoint CircleCenter(Point a,Point b,Point c){\n Point u=a-b,v=a-c;\n double m1=(norm(a)-norm(b))/2.0,m2=(norm(a)-norm(c))/2.0;\n \n Point res;\n if(cross(u,v)==0.0){\n res.x=1e9;\n res.y=1e9;\n \n return res;\n }\n res.x=(m1*v.y-m2*u.y)/cross(u,v);\n res.y=(m1*v.x-m2*u.x)/cross(v,u);\n \n return res;\n}\n//3点を通る円の中心を返す\n\n//交わる 0\n// c1がc2のinside 1\n// c1がc2のoutside 2\n// 交わらない 3\n\nint not_intersect(Circle c1,Circle c2){\n double d=getDistance(c1.c,c2.c);\n double r1=c1.r,r2=c2.r;\n if(r1<r2){\n if(d<(r2-r1)) return 1;\n }\n if(r1>r2){\n if(d<(r1-r2)) return 2;\n }\n if(d<=r1+r2) return 0;\n else return 3;\n}\n\npair<Point,Point> segCrossPpoints(Circle c,Line l){\n //assert(intersect(c,l));\n Vector pr=project(l,c.c);\n Vector e=(l.p2-l.p1)/abs(l.p2-l.p1);\n double base=sqrt(c.r*c.r-norm(pr-c.c));\n return make_pair(pr+e*base,pr-e*base);\n}\n\ndouble arg(Vector p){return atan2(p.y,p.x);}\nVector polar(double a,double r){return Point(cos(r)*a,sin(r)*a);}\n\n//inside(outside)\n\npair<Point,Point> getCrossPoints(Circle c1,Circle c2){\n //assert(intersect(c1,c2));\n double d=abs(c1.c-c2.c);\n double a=acos((c1.r*c1.r+d*d-c2.r*c2.r)/(2*c1.r*d));\n double t=arg(c2.c-c1.c);\n return make_pair(c1.c+polar(c1.r,t+a),c1.c+polar(c1.r,t-a));\n}\n\nvector<Line> Commontangent(Circle c1,Circle c2){\n vector<Line> res;\n Point p=c2.c-c1.c;\n \n if(abs(p)>=(c1.r+c2.r)){\n Point a,b;\n a.x=c1.r*(p.x*(c1.r+c2.r)+p.y*sqrt(norm(p)-(c1.r+c2.r)*(c1.r+c2.r)))/norm(p);\n a.y=c1.r*(p.y*(c1.r+c2.r)-p.x*sqrt(norm(p)-(c1.r+c2.r)*(c1.r+c2.r)))/norm(p);\n \n b.x=c1.r*(p.x*(c1.r+c2.r)-p.y*sqrt(norm(p)-(c1.r+c2.r)*(c1.r+c2.r)))/norm(p);\n b.y=c1.r*(p.y*(c1.r+c2.r)+p.x*sqrt(norm(p)-(c1.r+c2.r)*(c1.r+c2.r)))/norm(p);\n \n res.push_back(Line{a+c1.c,a+c1.c+Point{-a.y,a.x}});\n if(!(a==b)){\n res.push_back(Line{b+c1.c,b+c1.c+Point{-b.y,b.x}});\n }\n }\n \n if(abs(p)>=abs(c1.r-c2.r)){\n Point a,b;\n a.x=c1.r*(p.x*(c1.r-c2.r)+p.y*sqrt(norm(p)-(c1.r-c2.r)*(c1.r-c2.r)))/norm(p);\n a.y=c1.r*(p.y*(c1.r-c2.r)-p.x*sqrt(norm(p)-(c1.r-c2.r)*(c1.r-c2.r)))/norm(p);\n \n b.x=c1.r*(p.x*(c1.r-c2.r)-p.y*sqrt(norm(p)-(c1.r-c2.r)*(c1.r-c2.r)))/norm(p);\n b.y=c1.r*(p.y*(c1.r-c2.r)+p.x*sqrt(norm(p)-(c1.r-c2.r)*(c1.r-c2.r)))/norm(p);\n \n res.push_back(Line{a+c1.c,a+c1.c+Point{-a.y,a.x}});\n if(!(a==b)){\n res.push_back(Line{b+c1.c,b+c1.c+Point{-b.y,b.x}});\n }\n }\n \n return res;\n}\n\ntypedef vector<Point> Polygon;\n\n/*\n IN 2\n ON 1\n OUT 0\n */\n\nint contains(Polygon g,Point p){\n int n=int(g.size());\n bool x=false;\n for(int i=0;i<n;i++){\n Point a=g[i]-p,b=g[(i+1)%n]-p;\n if(a.y>b.y) swap(a,b);\n if(a.y<eps&&0<b.y&&cross(a,b)<0) x=!x;\n if(abs(cross(a,b))<eps&&dot(a,b)<eps) return 1;\n }\n return (x?2:0);\n}\n\nPolygon andrewScan(Polygon s,bool ok){\n Polygon u,l;\n sort(all(s));\n \n if(int(s.size())<3) return s;\n int n=int(s.size());\n \n u.push_back(s[0]);\n u.push_back(s[1]);\n \n l.push_back(s[n-1]);\n l.push_back(s[n-2]);\n \n if(ok){\n for(int i=2;i<n;i++){\n for(int j=int(u.size());j>=2&&ccw(u[j-2],u[j-1],s[i])==counter_clockwise;j--){\n u.pop_back();\n }\n u.push_back(s[i]);\n }\n \n for(int i=int(s.size())-3;i>=0;i--){\n for(int j=int(l.size());j>=2&&ccw(l[j-2],l[j-1],s[i])==counter_clockwise;j--){\n l.pop_back();\n }\n l.push_back(s[i]);\n }\n }\n \n if(!ok){\n for(int i=2;i<n;i++){\n for(int j=int(u.size());j>=2&&ccw(u[j-2],u[j-1],s[i])!=clockwise;j--){\n u.pop_back();\n }\n u.push_back(s[i]);\n }\n \n for(int i=int(s.size())-3;i>=0;i--){\n for(int j=int(l.size());j>=2&&ccw(l[j-2],l[j-1],s[i])!=clockwise;j--){\n l.pop_back();\n }\n l.push_back(s[i]);\n }\n }\n \n reverse(all(l));\n \n for(int i=int(u.size())-2;i>=1;i--) l.push_back(u[i]);\n \n return l;\n}//ok==1なら辺の上も含める\n\nPolygon convex_cut(const Polygon& P, const Line& l) {\n Polygon Q;\n for(int i=0;i<si(P);i++){\n Point A=P[i],B=P[(i+1)%si(P)];\n if(ccw(l.p1,l.p2,A)!=-1)Q.push_back(A);\n if(ccw(l.p1,l.p2,A)*ccw(l.p1,l.p2,B)<0) Q.push_back(getCrossPointL(Line{A,B},l));\n }\n return Q;\n}\n\ndouble area(Point a,Point b,Point c){\n b=b-a;\n c=c-a;\n return abs(b.x*c.y-b.y*c.x)/2.0;\n}\n\ndouble area(Polygon &P){\n if(si(P)==0) return 0.0;\n double res=0;\n Point c={0.0,0.0};\n for(int i=0;i<si(P);i++){\n c=c+P[i];\n }\n c=c/si(P);\n \n for(int i=0;i<si(P);i++){\n res+=area(c,P[i],P[(i+1)%si(P)]);\n }\n \n return res;\n}\n\nint main(){\n \n std::ifstream in(\"text.txt\");\n std::cin.rdbuf(in.rdbuf());\n cin.tie(0);\n ios::sync_with_stdio(false);\n \n while(1){\n int H,W,N;cin>>W>>H>>N;\n if(H==0) break;\n vector<Point> P(2*N);\n for(int i=0;i<2*N;i++) cin>>P[i].x>>P[i].y;\n sort(all(P));\n if(P.back().x==0){\n double ku=P[N].y-P[N-1].y;\n cout<<fixed<<setprecision(25)<<(ku/H)<<\"\\n\";\n continue;\n }\n P.push_back({(double)W,0});\n P.push_back({(double)W,(double)H});\n Line l={{0,0},{0,(double)H}};\n vector<Point> Q;\n Q.push_back({0,0});\n Q.push_back({0,(double)H});\n for(int i=0;i<2*N;i++){\n for(int j=0;j<2*N+2;j++){\n Point a=P[i],b=P[j];\n if(a==b) continue;\n Line l2={a,b};\n if(isParallel(l,l2)) continue;\n Point c=getCrossPointL(l,l2);\n if(c.y<0||c.y>H) continue;\n Q.push_back(c);\n }\n }\n sort(all(Q));\n Q.erase(unique(all(Q)),Q.end());\n \n //for(auto p:Q) cout<<p.x<<\" \"<<p.y<<endl;\n \n double ans=0;\n \n for(int q=0;q+1<si(Q);q++){\n Point m=(Q[q]+Q[q+1])/2;\n double u,d,left,right;\n left=0;right=H;\n for(int t=0;t<30;t++){\n double mid=(left+right)/2;\n Line l={m,{(double)W,mid}};\n int cnt=0;\n for(int i=0;i<2*N;i++){\n int cc=ccw(l.p1,l.p2,P[i]);\n if(cc!=1) cnt++;\n }\n if(cnt<N) left=mid;\n else right=mid;\n }\n d=left;\n left=0;right=H;\n for(int t=0;t<30;t++){\n double mid=(left+right)/2;\n Line l={m,{(double)W,mid}};\n int cnt=0;\n for(int i=0;i<2*N;i++){\n int cc=ccw(l.p1,l.p2,P[i]);\n if(cc!=1) cnt++;\n }\n if(cnt>N) right=mid;\n else left=mid;\n }\n u=right;\n \n //cout<<d<<\" \"<<u<<endl;\n \n ans+=(Q[q+1].y-Q[q].y)*(u-d);\n }\n \n ans/=H;\n ans/=H;\n \n cout<<fixed<<setprecision(25)<<ans<<\"\\n\";\n }\n}", "accuracy": 1, "time_ms": 6400, "memory_kb": 4096, "score_of_the_acc": -1.7726, "final_rank": 20 }, { "submission_id": "aoj_2256_5617954", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nstatic const double EPS = 1e-9;\n\nint main() {\n while (true) {\n int w, h, n;\n cin >> w >> h >> n;\n if (w == 0) return 0;\n\n vector<double> x(2 * n), y(2 * n);\n for (int i = 0; i < 2 * n; i++) cin >> x.at(i) >> y.at(i);\n x.emplace_back((double) w);\n x.emplace_back((double) w);\n x.emplace_back(0.0);\n x.emplace_back(0.0);\n y.emplace_back(0.0);\n y.emplace_back((double) h);\n y.emplace_back(0.0);\n y.emplace_back((double) h);\n\n vector<double> z;\n z.emplace_back(0.0);\n z.emplace_back((double) h);\n\n for (int i = 0; i < 2 * n + 4; i++) {\n for (int j = i + 1; j < 2 * n + 4; j++) {\n\tif (abs(x[i] - x[j]) < EPS) continue;\n\tdouble m = (y[j] - y[i]) / (x[j] - x[i]);\n\tdouble keep = y[i] - m * x[i];\n\tif (keep < -EPS || keep > h + EPS) continue;\n\tint count = 0, count2 = 0;\n\tfor (int k = 0; k < 2 * n; k++) {\n\t double keep1 = y[k], keep2 = keep + m * x[k];\n\t if (keep1 > keep2 + EPS) count++;\n\t else if (keep1 > keep2 - EPS) count2++;\n\t}\n\tif (count + count2 >= n && count <= n) z.emplace_back(keep);\n }\n }\n\n sort(z.begin(), z.end());\n z.erase(unique(z.begin(), z.end()), z.end());\n\n double area = 0.0;\n for (int i = 1; i < z.size(); i++) {\n if (abs(z[i] - z[i - 1]) <= EPS) continue;\n double midp = (z[i] + z[i - 1]) / 2.0;\n double high = h, low = 0;\n for (int cycle = 0; cycle < 150; cycle++) {\n\tdouble mid = (high + low) / 2.0;\n\tdouble midm = (mid - midp) / (double) w;\n\tint up = 0;\n\tfor (int k = 0; k < 2 * n; k++) {\n\t if (y[k] + EPS >= midp + midm * x[k]) up++;\n\t}\n\tif (up >= n) low = mid;\n\telse high = mid;\n }\n double upperp = high;\n high = h, low = 0;\n for (int cycle = 0; cycle < 150; cycle++) {\n\tdouble mid = (high + low) / 2.0;\n\tdouble midm = (mid - midp) / (double) w;\n\tint up = 0;\n\tfor (int k = 0; k < 2 * n; k++) {\n\t if (y[k] + EPS >= midp + midm * x[k]) up++;\n\t}\n\tif (up > n) low = mid;\n\telse high = mid;\n }\n double lowerp = low;\n area += max(0.0, (upperp - lowerp) * (z[i] - z[i - 1]));\n }\n\n cout << fixed << setprecision(10) << area / (double) (h * h) << '\\n';\n }\n}", "accuracy": 1, "time_ms": 270, "memory_kb": 3496, "score_of_the_acc": -0.2327, "final_rank": 2 }, { "submission_id": "aoj_2256_5594146", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nstatic const double EPS = 1e-9;\n\nint main() {\n while (true) {\n int w, h, n;\n cin >> w >> h >> n;\n if (w == 0) return 0;\n\n vector<double> x(2 * n), y(2 * n);\n for (int i = 0; i < 2 * n; i++) cin >> x.at(i) >> y.at(i);\n x.emplace_back((double) w);\n x.emplace_back((double) w);\n x.emplace_back(0.0);\n x.emplace_back(0.0);\n y.emplace_back(0.0);\n y.emplace_back((double) h);\n y.emplace_back(0.0);\n y.emplace_back((double) h);\n\n vector<double> z;\n z.emplace_back(0.0);\n z.emplace_back((double) h);\n\n for (int i = 0; i < 2 * n + 4; i++) {\n for (int j = i + 1; j < 2 * n + 4; j++) {\n\tif (abs(x[i] - x[j]) < EPS) continue;\n\tdouble m = (y[j] - y[i]) / (x[j] - x[i]);\n\tdouble keep = y[i] - m * x[i];\n\tif (keep < -EPS || keep > h + EPS) continue;\n\tint count = 0, count2 = 0;\n\tfor (int k = 0; k < 2 * n; k++) {\n\t double keep1 = y[k], keep2 = keep + m * x[k];\n\t if (keep1 > keep2 + EPS) count++;\n\t else if (keep1 > keep2 - EPS) count2++;\n\t}\n\tif (count + count2 >= n && count <= n) z.emplace_back(keep);\n }\n }\n\n sort(z.begin(), z.end());\n z.erase(unique(z.begin(), z.end()), z.end());\n\n double area = 0.0;\n for (int i = 1; i < z.size(); i++) {\n if (abs(z[i] - z[i - 1]) <= EPS) continue;\n double midp = (z[i] + z[i - 1]) / 2.0;\n double high = h, low = 0;\n for (int cycle = 0; cycle < 150; cycle++) {\n\tdouble mid = (high + low) / 2.0;\n\tdouble midm = (mid - midp) / (double) w;\n\tint up = 0;\n\tfor (int k = 0; k < 2 * n; k++) {\n\t if (y[k] + EPS >= midp + midm * x[k]) up++;\n\t}\n\tif (up >= n) low = mid;\n\telse high = mid;\n }\n double upperp = high;\n high = h, low = 0;\n for (int cycle = 0; cycle < 150; cycle++) {\n\tdouble mid = (high + low) / 2.0;\n\tdouble midm = (mid - midp) / (double) w;\n\tint up = 0;\n\tfor (int k = 0; k < 2 * n; k++) {\n\t if (y[k] + EPS >= midp + midm * x[k]) up++;\n\t}\n\tif (up > n) low = mid;\n\telse high = mid;\n }\n double lowerp = low;\n area += max(0.0, (upperp - lowerp) * (z[i] - z[i - 1]));\n }\n\n cout << fixed << setprecision(10) << area / (double) (h * h) << '\\n';\n }\n}", "accuracy": 1, "time_ms": 270, "memory_kb": 3500, "score_of_the_acc": -0.2363, "final_rank": 3 }, { "submission_id": "aoj_2256_5375639", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nconst double INF = 1000000000;\nconst double EPS = 0.000000001;\nstruct point{\n double x, y;\n point(){\n }\n point(double x, double y): x(x), y(y){\n }\n point operator +(point P){\n return point(x + P.x, y + P.y);\n }\n point operator -(point P){\n return point(x - P.x, y - P.y);\n }\n point operator *(double k){\n return point(x * k, y * k);\n }\n point operator /(double k){\n return point(x / k, y / k);\n }\n};\ndouble abs(point P){\n return sqrt(P.x * P.x + P.y * P.y);\n}\ndouble cross(point P, point Q){\n return P.x * Q.y - P.y * Q.x;\n}\nstruct line{\n point A, B;\n line(point A, point B): A(A), B(B){\n }\n};\npoint vec(line L){\n return L.B - L.A;\n}\npoint line_intersection(line L1, line L2){\n return L1.A + vec(L1) * cross(L2.A - L1.A, vec(L2)) / cross(vec(L1), vec(L2));\n}\ndouble area(double l, double r, double u, double d, double H){\n if (u < EPS){\n return 0;\n }\n if (d > H - EPS){\n return (r - l) * H;\n }\n if (d > 0){\n if (u < H){\n return (d + u) * (r - l) / 2;\n } else {\n double x = l + (H - d) / (u - d) * (r - l);\n return ((r - x) + (r - l)) * (H - d) / 2 + (r - l) * d;\n }\n } else {\n if (u < H){\n double x = l + (-d) / (u - d) * (r - l);\n return (r - x) * u / 2;\n } else {\n double x1 = l + (-d) / (u - d) * (r - l);\n double x2 = l + (H - d) / (u - d) * (r - l);\n return ((r - x2) + (r - x1)) * H / 2;\n }\n }\n}\nint main(){\n cout << fixed << setprecision(20);\n while (true){\n int W, H, N;\n cin >> W >> H >> N;\n if (W == 0 && H == 0 && N == 0){\n break;\n }\n vector<point> P(N * 2);\n for (int i = 0; i < N * 2; i++){\n cin >> P[i].x >> P[i].y;\n }\n vector<double> Y;\n Y.push_back(0);\n Y.push_back(H);\n for (int i = 0; i < N * 2; i++){\n if (P[i].x <= EPS){\n Y.push_back(P[i].y);\n } else {\n for (int j = i + 1; j < N * 2; j++){\n if (P[j].x > EPS && abs(P[i].x - P[j].x) > EPS){\n point Q = line_intersection(line(point(0, 0), point(0, H)), line(P[i], P[j]));\n if (0 <= Q.y && Q.y <= H){\n Y.push_back(Q.y);\n }\n }\n }\n }\n }\n sort(Y.begin(), Y.end());\n int M = Y.size();\n double ans = 0;\n for (int i = 0; i < M - 1; i++){\n vector<double> Y1(N * 2), Y2(N * 2);\n for (int j = 0; j < N * 2; j++){\n if (P[j].x <= EPS){\n if (P[j].y <= Y[i] + EPS){\n Y1[j] = -INF;\n Y2[j] = -INF;\n } else {\n Y1[j] = INF;\n Y2[j] = INF;\n }\n } else {\n point Q1 = line_intersection(line(point(W, 0), point(W, H)), line(point(0, Y[i]), P[j]));\n Y1[j] = Q1.y;\n point Q2 = line_intersection(line(point(W, 0), point(W, H)), line(point(0, Y[i + 1]), P[j]));\n Y2[j] = Q2.y;\n }\n }\n sort(Y1.begin(), Y1.end());\n sort(Y2.begin(), Y2.end());\n ans -= area(Y[i], Y[i + 1], Y1[N - 1], Y2[N - 1], H);\n ans += area(Y[i], Y[i + 1], Y1[N], Y2[N], H);\n }\n ans /= H * H;\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 670, "memory_kb": 3240, "score_of_the_acc": -0.0668, "final_rank": 1 } ]
aoj_2260_cpp
問題 B : (iwi) 20XX 年の今,G○○gle 社の世界的な支配により,人々のコミュニケーションは限られており, 例えば,自由に人と情報をやりとりすることは基本的に許されていない. 特に,過去にプログラミングコンテストで名を馳せた強豪たちは,危険と判断され, お互いバラバラにされ,密かに注意深く監視されているという. 自分も,そんな一人であった,ような気がする. しかし実は,自分は記憶を失ってしまっているのだ. 自分が何と名乗っていたか,それすらも思い出せない. 'i' とか 'w' とか括弧とかを使っていた気がするが, それっぽいものを書きだしてみても,しっくりこない. そういえば,今思い出したことには,左右に線対称だった気がするのだ. 問題 'i', 'w', '(', ')' からなる文字列が与えられた時, 一部の文字を他の文字に置き換えて,'i', 'w', '(', ')' からなる左右に線対称な文字列にしたい. 文字の追加や削除は許さず,1 文字を別の 1 文字に変える操作のみを行うことにする. 少なくとも何文字置き換えなければならないかを出力するプログラムを作成せよ. ここで用いる左右に線対称の定義は,以下とする. 以下の文字列は左右に線対称. 空文字列 "i" "w" 文字列 x が左右に線対称のとき,以下の文字列も左右に線対称. "i" x "i" "w" x "w" "(" x ")" ")" x "(" 以上のもののみが左右に線対称. ここで,"i" x "i" とは "i" と x と "i" を連結したものを表す. 他のものも同様である. 入力 入力は 'i', 'w', '(', ')' からなる 1 つの文字列である. 出力 置き換えなければならない文字数を表す整数を出力せよ. 制約 入力の文字列の長さは 10 以下である. 入出力例 入出力例 1 入力例 1: (iwi) 入力例 1 に対する出力例: 0 入力例 1 では文字列がはじめから左右に線対称である. 入出力例 2 入力例 2: ii(((((ww 入力例 2 に対する出力例: 5 入力例 2 では例えば ww((i))ww 等に変更すればよい.
[ { "submission_id": "aoj_2260_9264521", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntypedef long double ld;\n#define rep(i, n) for(ll i = 0; i < (n); i++)\n#define reps(i, l, r) for(ll i = (l); i < (r); i++)\n#define endl \"\\n\";\n#define all(a) (a).begin(), (a).end()\nconst ll INF = 2e18;\nconst ll mod1 = 1000000007;\nconst ll mod2 = 998244353;\nint dx[4] = {-1, 0, 1, 0};\nint dy[4] = {0, -1, 0, 1};\nvoid solve() {\n string S; cin >> S;\n ll N = (ll)S.size();\n vector<char>C(N);\n ll Max = 1;\n rep(i, N) Max *= 4;\n vector<char>c = {'i', 'w', '(', ')'};\n ll ans = Max;\n rep(i, Max) {\n ll tmp = i;\n rep(j, N) {\n C[j] = c[tmp % 4];\n tmp /= 4;\n }\n bool check = true;\n rep(j, N) {\n if (C[j] == 'i' && C[N - j - 1] == 'i') continue;\n if (C[j] == 'w' && C[N - j - 1] == 'w') continue;\n if (C[j] == '(' && C[N - j - 1] == ')') continue;\n if (C[j] == ')' && C[N - j - 1] == '(') continue;\n check = false;\n }\n if (check) {\n ll sum = 0;\n rep(j, N) {\n sum += (S[j] != C[j]);\n }\n ans = min(ans, sum);\n }\n }\n cout << ans << endl;\n}\nsigned main() {\n\tll T = 1;\n\t//cin >> T;\n\twhile (T--) solve();\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3400, "score_of_the_acc": -1.0828, "final_rank": 3 }, { "submission_id": "aoj_2260_7222578", "code_snippet": "//#pragma GCC optimize(2)\n#include<bits/stdc++.h>\nusing namespace std;\n#define int long long\n#define ll long long\n#define N 205\n//#define mod 998244353\n#define pii pair<int,int>\n#define mp make_pair\n#define pb push_back\n#define ld long double\n#define ls (rt<<1)\n#define rs ((rt<<1)|1)\n#define SZ(x) (int)(x.size())\n#define debug cout<<endl<<\"ff\"<<endl\n#define YES cout<<\"YES\"<<endl\n#define NO cout<<\"NO\"<<endl\n#define fi first\n#define se second\n#define INF 1e9\n#define pq priority_queue\n#define rep(x,a,b) for(int x=a;x<=b;x++)\n/*int qpow(int a,int b){\n\tint res=1;\n\tfor(;b;b>>=1){\n\t\tif(b&1) res=res*a%mod;\n\t\ta=a*a%mod;\n\t}\n\treturn res;\n}\nint fac[N],ifac[N];\nint C(int n,int m){\n\tif(m>n||m<0||n<0) return 0;\n\treturn fac[n]*ifac[n-m]%mod*ifac[m]%mod;\n}\nvoid init(){\n\tfac[0]=1;\n\tfor(int i=1;i<N;i++) fac[i]=fac[i-1]*i%mod;\n\tifac[N-1]=qpow(fac[N-1],mod-2);\n\tfor(int i=N-2;i>=0;i--) ifac[i]=ifac[i+1]*(i+1)%mod;\n}*/\n/*struct node{\n\tint nxt,to;\n}e[N<<1];\nint cnt,head[N];\ninline void add(int x,int y){\n\te[++cnt].nxt=head[x];\n\thead[x]=cnt;\n\te[cnt].to=y;\n}*/\ninline int lowbit(int x){return x&(-x);}\ninline int read(){\n int x=0,t=1;char ch=getchar();\n while(ch<'0'||ch>'9'){\n if(ch=='-') t=-1;\n ch=getchar();\n }\n while(ch>='0'&&ch<='9'){\n x=(x<<1)+(x<<3)+(ch-'0');\n ch=getchar();\n }\n return x*t;\n}\ninline void write(int x){\n\tif(x<0) putchar('-'),x=-x;\n\tif(x>=10) write(x/10);\n\tputchar(x%10+'0');\n}\nint n,ans=INF,p[11];\nchar a[4]={'(',')','i','w'};\nsigned main(){\n\tios::sync_with_stdio(false);\n\tcin.tie(0);cout.tie(0);\n\tstring s;\n\tcin>>s;n=s.size();\n\ts=\" \"+s;\n\tp[0]=1;for(int i=1;i<11;i++) p[i]=p[i-1]*4;\n\tfor(int i=0;i<p[n];i++){\n\t\tstring t=\"\";\n\t\tfor(int j=0;j<n;j++)\n\t\t\tt=t+a[(i/p[j])%4];\n\t\tint fl=0,val=0;\n\t\tt=\" \"+t;\n\t\tfor(int i=1;i<=(n+1)/2;i++){\n\t\t\tint j=n-i+1;\n\t\t\tif(t[i]!='('&&t[i]!=')'){\n\t\t\t\tif(t[i]!=t[j]){\n\t\t\t\t\tfl=1;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\telse{\n\t\t\t\tif(t[i]=='('&&t[j]==')');\n\t\t\t\telse{\n\t\t\t\t\tfl=1;\n\t\t\t\t\tbreak; \n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif(fl) continue;\n\t\tfor(int i=1;i<=n;i++)\n\t\t\tif(s[i]!=t[i])\n\t\t\t\tval++;\n\t\tans=min(ans,val);\n\t}\n\tcout<<ans<<endl;\n\treturn 0;\n}", "accuracy": 0.75, "time_ms": 180, "memory_kb": 3444, "score_of_the_acc": -1.8, "final_rank": 5 }, { "submission_id": "aoj_2260_1548510", "code_snippet": "#include \"bits/stdc++.h\"\nusing namespace std;\ntypedef long long ll;\ntypedef pair<int,int> pii;\n#define rep(i,n) for(ll i=0;i<(ll)(n);i++)\n#define all(a) (a).begin(),(a).end()\n#define pb push_back\n#define INF 99999999\n#define eps 1e-9\nint l;\nint mini;\nstring str;\n\nbool isOK(string s){\n if(s.size()%2!=0){\n if(s[(int)(s.size()/2)]=='('||s[(int)(s.size()/2)]==')')return false;\n s=s.substr( 0,(int)(s.size()/2) )+s.substr( (int)((s.size()+1)/2),(int)(s.size()/2));\n }\n string a,b;\n a=s.substr( 0,(int)(s.size()/2) );\n b=s.substr( s.size()/2,(int)(s.size()/2) );\n reverse(b.begin(),b.end());\n rep(i,b.size()){\n if( (a[i]==b[i]&&a[i]!=')'&&a[i]!='(')||( (a[i]==')'&&b[i]=='(') || (a[i]=='('&&b[i]==')') ) ){}\n else{\n return false;\n }\n }\n return true;\n}\n\nint solve(string a,string b){\n int ret=0;\n rep(i,a.size()){\n if(a[i]!=b[i])ret++;\n }\n \n return ret;\n}\n\nvoid dfs(string s){\n if(s.size()==l){\n if(isOK(s)){\n mini=min(mini,solve(s,str));\n }\n return ;\n }\n string iwi=\"iw()\";\n rep(i,4){\n dfs(s+iwi[i]);\n }\n}\n\n\nint main(){\n mini=INF;\n cin>>str;\n l=str.size();\n \n dfs(\"\");\n cout<<mini<<endl;\n}", "accuracy": 1, "time_ms": 220, "memory_kb": 1196, "score_of_the_acc": -1.1232, "final_rank": 4 }, { "submission_id": "aoj_2260_608740", "code_snippet": "#define _USE_MATH_DEFINES\n#define INF 0x3f3f3f3f\n#include <cstdio>\n#include <iostream>\n#include <sstream>\n#include <cmath>\n#include <cstdlib>\n#include <algorithm>\n#include <queue>\n#include <stack>\n#include <limits>\n#include <map>\n#include <string>\n#include <cstring>\n#include <set>\n#include <deque>\n#include <bitset>\n#include <list>\n#include <cctype>\n#include <utility>\n \nusing namespace std;\n \ntypedef long long ll;\ntypedef pair <int,int> P;\ntypedef pair <int,P > PP;\n \nint tx[] = {0,1,0,-1};\nint ty[] = {-1,0,1,0};\n \nstatic const double EPS = 1e-8;\n\nstatic const char iwi[] = {'i','w','(',')'};\n\nint FastPow(int x,int power){\n int res = 1;\n while(power){\n if(power & 1) res *= x;\n x *= x;\n power >>= 1;\n }\n return res;\n}\n\nbool isClear(char buf[12]){\n if(strlen(buf) % 2 != 0\n && (buf[strlen(buf) / 2] == '('\n\t || buf[strlen(buf) / 2] == ')')) return false;\n for(int i=0,j=strlen(buf)-1;i<strlen(buf)/2;i++,j--){\n if(buf[i] == '(' && buf[j] != ')') return false;\n else if(buf[i] == ')' && buf[j] != '(') return false;\n else if(buf[i] == 'i' && buf[j] != 'i') return false;\n else if(buf[i] == 'w' && buf[j] != 'w') return false;\n }\n return true;\n}\n\nint CountFlag(int S){\n int res = 0;\n while(S){\n if(S & 1) res++;\n S >>= 1;\n }\n return res;\n}\n\nint main(){\n char buf[12];\n while(~scanf(\"%s\",buf)){\n int upper = FastPow(4,strlen(buf));\n\n int min_modify_count = INF;\n for(int S=0;S<=upper;S++){\n int tmp_S = S;\n char tmp_buf[12];\n \n int modify_count = 0;\n memset(tmp_buf,'\\0',sizeof(tmp_buf));\n strcpy(tmp_buf,buf);\n\n for(int i=0;i<strlen(tmp_buf);i++){\n\tif(buf[i] != iwi[tmp_S % 4]) modify_count++;\n\ttmp_buf[i] = iwi[tmp_S % 4];\n\ttmp_S /= 4;\n }\n\n if(!isClear(tmp_buf)) continue;\n\n min_modify_count = min(modify_count,min_modify_count);\n }\n printf(\"%d\\n\",min_modify_count);\n }\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 1148, "score_of_the_acc": -0.5045, "final_rank": 2 }, { "submission_id": "aoj_2260_608738", "code_snippet": "#define _USE_MATH_DEFINES\n#define INF 0x3f3f3f3f\n#include <cstdio>\n#include <iostream>\n#include <sstream>\n#include <cmath>\n#include <cstdlib>\n#include <algorithm>\n#include <queue>\n#include <stack>\n#include <limits>\n#include <map>\n#include <string>\n#include <cstring>\n#include <set>\n#include <deque>\n#include <bitset>\n#include <list>\n#include <cctype>\n#include <utility>\n \nusing namespace std;\n \ntypedef long long ll;\ntypedef pair <int,int> P;\ntypedef pair <int,P > PP;\n \nint tx[] = {0,1,0,-1};\nint ty[] = {-1,0,1,0};\n \nstatic const double EPS = 1e-8;\n\nstatic const char iwi[] = {'i','w','(',')'};\n\nint FastPow(int x,int power){\n int res = 1;\n while(power){\n if(power & 1) res *= x;\n x *= x;\n power >>= 1;\n }\n return res;\n}\n\nbool isClear(char buf[12]){\n if(strlen(buf) % 2 != 0\n && (buf[strlen(buf) / 2 + 1] == '('\n\t || buf[strlen(buf) / 2 + 1] == ')')) return false;\n for(int i=0,j=strlen(buf)-1;i<strlen(buf)/2;i++,j--){\n if(buf[i] == '(' && buf[j] != ')') return false;\n else if(buf[i] == ')' && buf[j] != '(') return false;\n else if(buf[i] == 'i' && buf[j] != 'i') return false;\n else if(buf[i] == 'w' && buf[j] != 'w') return false;\n }\n return true;\n}\n\nint CountFlag(int S){\n int res = 0;\n while(S){\n if(S & 1) res++;\n S >>= 1;\n }\n return res;\n}\n\nint main(){\n char buf[12];\n while(~scanf(\"%s\",buf)){\n int upper = FastPow(4,strlen(buf));\n\n int min_modify_count = INF;\n for(int S=0;S<=upper;S++){\n int tmp_S = S;\n char tmp_buf[12];\n \n int modify_count = 0;\n memset(tmp_buf,'\\0',sizeof(tmp_buf));\n strcpy(tmp_buf,buf);\n\n for(int i=0;i<strlen(tmp_buf);i++){\n\tif(buf[i] != iwi[tmp_S % 4]) modify_count++;\n\ttmp_buf[i] = iwi[tmp_S % 4];\n\ttmp_S /= 4;\n }\n\n if(!isClear(tmp_buf)) continue;\n\n min_modify_count = min(modify_count,min_modify_count);\n }\n printf(\"%d\\n\",min_modify_count);\n }\n}", "accuracy": 0.65, "time_ms": 20, "memory_kb": 1148, "score_of_the_acc": -0.1045, "final_rank": 6 }, { "submission_id": "aoj_2260_269645", "code_snippet": "#include <iostream>\n#include <algorithm>\n#include <string>\n#include <vector>\nusing namespace std;\n\nstring str;\nint ans = 20;\nchar c[] = { 'i' , 'w' , '(' , ')' };\n\nchar f(char c){\n if( c == '(' )\n c = ')';\n else if( c == ')' )\n c = '(';\n return c;\n}\n\nbool check(string s){\n bool flag = true;\n for(unsigned int i=0 ; i<s.size()/2 ; ++i ){\n if( s[i] != f(s[s.size()-i-1]) ){\n flag = false;\n }\n }\n if( (s.size() % 2) == 1 ){\n int i = s.size()/2;\n if( s[i] == ')' || s[i] == '(' ){\n flag = false;\n }\n }\n return flag;\n}\n\n\n\n\nint def(string s){\n int cnt = 0;\n for(int i=0 ; i<s.size() ; ++i ){\n if( str[i] != s[i] )\n cnt++;\n }\n return cnt;\n}\n\nvoid solve(string s, int i){\n if( i == s.size()-1 ){\n if( check(s) ){\n ans = min( ans , def(s) );\n }\n }else if( i < s.size() ){\n s[i] = c[0];\n solve( s , i+1 );\n s[i] = c[1];\n solve( s , i+1 );\n s[i] = c[2];\n solve( s , i+1 );\n s[i] = c[3];\n solve( s , i+1 );\n }\n}\nint main(){\n cin >> str;\n solve( str , 0 );\n cout << ans << endl; \n}", "accuracy": 1, "time_ms": 80, "memory_kb": 880, "score_of_the_acc": -0.3, "final_rank": 1 } ]
aoj_2258_cpp
全自動円形掃除機 English text is not available in this practice contest. Automatic Cleaning Machine (ACM) 社は画期的な全自動円形掃除機 Intelligent Circular Perfect Cleaner(ICPC) を開発した. なんとこの ICPC は人がいない日中に自動で動き出し,自分が通り過ぎた場所のゴミを掃除する機能を備えている. 全自動という特性を活かすためにも ICPC の集塵機の容積を大きくし,人手でゴミを捨てる回数を減らしたい. しかしながら ICPC が大型化すればするほど ICPC が円形故に部屋の隅の掃除できない部分の面積も大きくなってしまう.(図G-1) 図G-1 また廊下の幅よりも大きくしてしまうと廊下を通ることができなくなってしまう.(図G-2) 図G-2 ACM 社は凄腕プログラマーのあなたに,部屋の見取り図とICPCの中心座標と半径からその部屋の中でICPCが掃除可能な面積を出力するプログラムの作成を依頼した. Input 入力は複数のデータセットからなり,各データセットは以下の形式をしている. n x y r x 1 y 1 x 2 y 2 ... x n y n データセットの1行目には4つの整数 n , x , y , r が記されており,それぞれ部屋を構成する多角形の頂点数(3 ≤ n ≤ 20), ICPCの初期位置の中心座標 (-100 ≤ x , y ≤ 100), ICPCの半径 (1 ≤ r ≤ 100)を表している. 続く n 行には多角形の頂点の座標が反時計回りに記されており,以下の条件を満たす. -100 ≤ x i , y i ≤ 100 全ての壁は,x軸かy軸に並行 ICPCの初期位置は必ず部屋の内側にあり,どの部屋の壁とも10 -3 以上離れている 任意の部屋の頂点同士の距離,任意の部屋の頂点と部屋の辺の距離,任意の部屋の辺同士の距離は 2 r +10 -3 より大きいかもしくは 2 r -10 -3 より小さい 四つのゼロのみからなる行が入力の終わりを表す. Output 各データセットに対し,ICPCが掃除可能な領域の面積を1行に出力せよ.答えには10 -6 を超える誤差があってはならない. Sample Input 4 5 5 1 0 0 10 0 10 10 0 10 8 5 5 1 0 0 9 0 9 1 10 1 10 9 9 9 9 10 0 10 12 25 5 1 0 0 10 0 10 4 20 4 20 0 40 0 40 10 20 10 20 5 10 5 10 10 0 10 0 0 0 0 Output for the Sample Input 99.1415926535897 96.7123889804 199.2321787
[ { "submission_id": "aoj_2258_2589625", "code_snippet": "#include <iostream>\n#include <iomanip>\n#include <complex>\n#include <vector>\n#include <algorithm>\n#include <cmath>\nusing namespace std;\n\nconst int nrep = 50;\n\n/********************* Geometry Library *****************/\nconst double EPS = 1e-9;\nconst double INF = 1e12;\n#define LE(n,m) ((n) < (m) + EPS)\n#define GE(n,m) ((n) + EPS > (m))\n#define EQ(n,m) (abs((n)-(m)) < EPS)\n#define X real()\n#define Y imag()\n\ntypedef complex<double> P;\ntypedef vector<P> VP;\nstruct L : VP{\n L(const P& a, const P& b){ resize(2); at(0)=a; at(1)=b; }\n L(){ resize(2); }\n};\nstruct C{\n P p;\n double r;\n C(const P& p, const double& r) : p(p), r(r) {}\n};\nnamespace std{\n bool operator < (const P& a, const P& b){\n return (a.X != b.X) ? a.X<b.X : a.Y<b.Y;\n }\n}\n\ndouble dot(P a, P b){\n return (conj(a)*b).X;\n}\ndouble cross(P a, P b){\n return (conj(a)*b).Y;\n}\n\nint ccw(P a, P b, P c){\n b -= a;\n c -= a;\n if(cross(b,c) > EPS) return +1;\n if(cross(b,c) <-EPS) return -1;\n if(dot(b,c) < EPS) return +2;\n if(norm(b) < norm(c)) return -2;\n return 0;\n}\n\nP projection(const L& l, const P& p) {\n double t = dot(p-l[0], l[0]-l[1]) / norm(l[0]-l[1]);\n return l[0] + t*(l[0]-l[1]);\n}\nbool intersectSP(const L &s, const P &p) {\n return abs(s[0]-p)+abs(s[1]-p)-abs(s[1]-s[0]) < EPS;\n}\ndouble distanceLP(const L &l, const P &p) {\n return abs(p - projection(l, p));\n}\ndouble distanceSP(const L &s, const P &p) {\n const P r = projection(s, p);\n if (intersectSP(s, r)) return abs(r - p);\n return min(abs(s[0] - p), abs(s[1] - p));\n}\n\nVP crosspointLC(const L &l, const C &c){\n VP ret;\n P mid = projection(l, c.p);\n double dlp = distanceLP(l, c.p);\n if(EQ(dlp, c.r)){\n ret.push_back(mid);\n return ret;\n }\n if(dlp > c.r){\n return ret;\n }\n P d = sqrt(c.r*c.r -dlp*dlp)/abs(l[1]-l[0]) *(l[1]-l[0]);\n ret.push_back(mid+d);\n ret.push_back(mid-d);\n return ret;\n}\nVP crosspointSC(const L &s, const C &c){\n VP ret;\n VP cp = crosspointLC(s,c);\n for(int i=0; i<(int)cp.size(); i++){\n if(dot(s[1]-s[0], cp[i]-s[0]) < EPS) continue;\n if(norm(s[1]-s[0]) < norm(cp[i]-s[0]) +EPS) continue;\n ret.push_back(cp[i]);\n }\n return ret;\n}\n\ndouble getangle(P p, P a, P b){\n a -= p;\n b -= p;\n return acos(dot(a,b)/(abs(a)*abs(b)));\n}\ndouble getarea(const VP &p) {\n double ret = 0;\n for (int i=0; i<(int)p.size(); i++){ \n ret += cross(p[i], p[(i+1)%p.size()]);\n }\n return ret*0.5;\n}\n/********************* end Library ***********************/\n\nint main(){\n while(1){\n /******************* Input ************************/\n int n,x,y,r;\n cin >> n >> x >> y >> r;\n if(n==0) break;\n \n C robot(P(x,y), r);\n VP v(n);\n for(int i=0; i<n; i++){\n int vx,vy;\n cin >> vx >> vy;\n v[i] = P(vx, vy);\n }\n //rotate the order of vertices to start from horizontal edge\n if(v[0].X == v[1].X){ \n v.push_back(v[0]);\n v.erase(v.begin());\n }\n /****************** end Input **********************/\n\n /*********** Move to the initial state ***********/\n //go right until touch the wall\n while(1){\n robot.p += P(1.0, 0);\n bool reach = false;\n for(int i=0; i<n; i++){\n if(distanceSP(L(v[i],v[(i+1)%n]), robot.p) < r){\n reach = true;\n break;\n }\n }\n if(reach) break;\n }\n //binary search\n P s = robot.p -P(1.0, 0);\n P e = robot.p;\n for(int i=0; i<nrep; i++){\n robot.p = (s+e)*0.5;\n bool reach = false;\n for(int i=0; i<n; i++){\n if(distanceSP(L(v[i],v[(i+1)%n]), robot.p) < r){\n reach = true;\n }\n }\n if(reach) e = robot.p;\n else s = robot.p;\n }\n\n bool onedge;\n int sidx;\n for(int i=0; i<n; i++){\n if(abs(robot.p -v[i]) < r+EPS){\n sidx = i;\n onedge = false;\n break;\n }\n if(distanceSP(L(v[i], v[(i+1)%n]), robot.p) < r+EPS){\n if(abs(robot.p -v[i]) > r+EPS && abs(robot.p -v[(i+1)%n]) > r+EPS){\n sidx = i;\n onedge = true;\n break;\n }\n }\n }\n /********************* end initial state **********************/\n\n /************************* Main Loop **************************/\n //move along the wall\n VP vlist;\n double area = 0.0;\n while(1){\n /******** Case 1. contact with the edge (parallel displacement) *******/\n if(onedge){\n //search the limit\n P dir = (v[(sidx+1)%n]-v[sidx])/abs(v[(sidx+1)%n]-v[sidx]);\n P limit = (sidx%2==0)? P(v[(sidx+1)%n].X, robot.p.Y) :P(robot.p.X, v[(sidx+1)%n].Y);\n P s,e;\n s = e = robot.p;\n while(1){\n e += dir;\n if(dot(limit-e, dir) < 0){\n e = limit;\n break;\n }\n bool reach = false;\n for(int i=0; i<n; i++){\n if(i==sidx || i==(sidx-1+n)%n) continue;\n if(distanceSP(L(v[i],v[(i+1)%n]), e) < r){\n reach = true;\n break;\n }\n }\n if(reach) break;\n }\n \n //binary search\n for(int i=0; i<nrep; i++){\n robot.p = (s+e)*0.5;\n bool reach = false;\n for(int i=0; i<n; i++){\n if(i==sidx || i==(sidx-1+n)%n) continue;\n //if turn left (avoid errors)\n if(i==(sidx+1)%n && cross(v[(sidx+1)%n]-v[sidx],v[(sidx+2)%n]-v[sidx])<0){\n continue;\n }\n if(distanceSP(L(v[i], v[(i+1)%n]), robot.p) < r){\n reach = true;\n break;\n }\n }\n if(reach) e = robot.p;\n else s = robot.p;\n }\n\n //calculate ridx and last;\n P last;\n double eval = -INF;\n int ridx;\n bool n_onedge;\n bool terminate = false;\n P cpedge = (sidx%2==0)? P(robot.p.X, v[sidx].Y) :P(v[sidx].X, robot.p.Y);\n for(int i=0; i<n; i++){\n if(i==sidx || i==(sidx-1+n)%n) continue; \n if(abs(robot.p -v[i]) < r+EPS){\n if(!vlist.empty() && EQ(vlist[0], v[i])){\n last = v[i];\n terminate = true;\n break;\n }\n if(dot(robot.p-cpedge, v[i]-cpedge) > eval) {\n last = v[i];\n eval = dot(robot.p-cpedge, v[i]-cpedge);\n ridx = i;\n n_onedge = false;\n }\n }\n if(distanceSP(L(v[i], v[(i+1)%n]), robot.p) < r +EPS){\n if(abs(robot.p -v[i])>r+EPS && abs(robot.p -v[(i+1)%n])>r+EPS){\n //except vertices\n VP cp = crosspointSC(L(v[i], v[(i+1)%n]), robot);\n if(!vlist.empty() && EQ(vlist[0], cp[0])){\n last = cp[0];\n terminate = true;\n break;\n }\n if(dot(robot.p-cpedge, cp[0]-cpedge) > eval) {\n last = cp[0];\n eval = dot(robot.p-cpedge, cp[0]-cpedge);\n ridx = i;\n n_onedge = true;\n }\n }\n }\n }\n \n //calculate area ...\n if(!vlist.empty() && EQ(vlist[0], cpedge)){\n terminate = true;\n last = cpedge;\n }\n double a = getangle(robot.p, last, cpedge);\n area += r*r*M_PI*a/(2*M_PI) -r*r*sin(a)*0.5;\n vlist.push_back(cpedge);\n vlist.push_back(last);\n if(terminate){\n break;\n }else{\n onedge = n_onedge;\n sidx = ridx;\n }\n }\n \n /************ Case 2. contact with the vertex (rotate) ************/\n else{\n double d;\n double dxy;\n P s, e;\n s = e = robot.p;\n //search limit\n if(sidx%2==1){\n d = (v[sidx].X-v[(sidx-1+n)%n].X > 0)? 1: -1;\n dxy = fabs(e.X -v[sidx].X);\n if(abs(2*e.X -round(2*e.X)) > EPS){\n dxy = floor(2*dxy) *0.5;\n }\n }else{\n d = (v[sidx].Y-v[(sidx-1+n)%n].Y > 0)? 1: -1;\n dxy = fabs(e.Y -v[sidx].Y);\n if(abs(2*e.Y -round(2*e.Y)) > EPS){\n dxy = floor(2*dxy) *0.5;\n }\n }\n\n P start = (v[sidx] -v[(sidx+1)%n]) *(r / abs(v[sidx] -v[(sidx+1)%n]));\n while(1){\n dxy += 0.5;\n if(dxy +EPS > r){\n e = v[sidx] + start*P(0,-1);\n break;\n }\n if(sidx%2==1){\n e = v[sidx] +d*P(dxy, sqrt(fabs(r*r-dxy*dxy)));\n }else{\n e = v[sidx] +d*P(-sqrt(fabs(r*r-dxy*dxy)), dxy);\n }\n bool reach = false;\n for(int i=0; i<n; i++){\n if(i==sidx || i==(sidx-1+n)%n) continue;\n if(distanceSP(L(v[i], v[(i+1)%n]), e) < r){\n reach = true;\n break;\n }\n }\n if(reach) break;\n }\n\n //binary search\n for(int i=0; i<nrep; i++){\n double angle_s = getangle(v[sidx],start+v[sidx],s);\n double angle_e = getangle(v[sidx],start+v[sidx],e);\n double midangle = (angle_s +angle_e)*0.5;\n robot.p = v[sidx] + start*P(cos(midangle), -sin(midangle));\n bool reach = false;\n for(int i=0; i<n; i++){\n if(i==sidx || i==(sidx-1+n)%n) continue;\n if(distanceSP(L(v[i], v[(i+1)%n]), robot.p) < r){\n reach = true;\n break;\n }\n }\n if(reach) e = robot.p;\n else s = robot.p;\n }\n\n //calculate ridx, last\n P last;\n double eval = -INF;\n int ridx;\n bool n_onedge;\n bool terminate = false;\n for(int i=0; i<n; i++){\n if(abs(robot.p -v[i]) < r+EPS){\n if(cross(robot.p-v[sidx], v[i]-v[sidx]) < EPS){\n if(i!=sidx && !vlist.empty() && EQ(vlist[0], v[i])){\n last = v[i];\n terminate = true;\n break;\n }\n if(dot(robot.p-v[sidx], v[i]-v[sidx]) > eval) {\n last = v[i];\n eval = dot(robot.p-v[sidx], v[i]-v[sidx]);\n ridx = i;\n n_onedge = false;\n }\n }\n }\n if(distanceSP(L(v[i], v[(i+1)%n]), robot.p) < r +EPS){\n if(abs(robot.p -v[i])>r+EPS && abs(robot.p -v[(i+1)%n])>r+EPS){\n //except vertices\n VP cp = crosspointSC(L(v[i], v[(i+1)%n]), robot);\n if(cross(robot.p-v[sidx], v[i]-v[sidx]) < EPS){\n if(!vlist.empty() && EQ(vlist[0], cp[0])){\n last = cp[0];\n terminate = true;\n break;\n }\n if(dot(robot.p-v[sidx], cp[0]-v[sidx]) > eval){\n last = cp[0];\n eval = dot(robot.p-v[sidx], cp[0]-v[sidx]);\n ridx = i;\n n_onedge = true;\n }\n }\n }\n }\n }\n if(last == v[sidx]){\n onedge = true;\n continue;\n }\n \n //calculate area ...\n if(!vlist.empty()){\n double a = getangle(robot.p, last, v[sidx]);\n area += r*r*M_PI*a/(2*M_PI) -r*r*sin(a)*0.5;\n }\n vlist.push_back(last);\n if(terminate){\n break;\n }else{\n onedge = n_onedge;\n sidx = ridx;\n }\n }\n }\n\n area += getarea(vlist);\n cout << fixed;\n cout << setprecision(10);\n cout << area << endl;\n }\n /*********************** end Main Loop ************************/\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3580, "score_of_the_acc": -1, "final_rank": 4 }, { "submission_id": "aoj_2258_2589623", "code_snippet": "#include <iostream>\n#include <iomanip>\n#include <complex>\n#include <vector>\n#include <algorithm>\n#include <cmath>\nusing namespace std;\n\nconst int nrep = 50; //num of reptition of binary search\n\n/********************* Geometry Library *****************/\nconst double EPS = 1e-9;\nconst double INF = 1e12;\n#define LE(n,m) ((n) < (m) + EPS)\n#define GE(n,m) ((n) + EPS > (m))\n#define EQ(n,m) (abs((n)-(m)) < EPS)\n#define X real()\n#define Y imag()\n\ntypedef complex<double> P;\ntypedef vector<P> VP;\nstruct L : VP{\n L(const P& a, const P& b){ resize(2); at(0)=a; at(1)=b; }\n L(){ resize(2); }\n};\nstruct C{\n P p;\n double r;\n C(const P& p, const double& r) : p(p), r(r) {}\n};\nnamespace std{\n bool operator < (const P& a, const P& b){\n return (a.X != b.X) ? a.X<b.X : a.Y<b.Y;\n }\n}\n\ndouble dot(P a, P b){\n return (conj(a)*b).X;\n}\ndouble cross(P a, P b){\n return (conj(a)*b).Y;\n}\n\nint ccw(P a, P b, P c){\n b -= a;\n c -= a;\n if(cross(b,c) > EPS) return +1;\n if(cross(b,c) <-EPS) return -1;\n if(dot(b,c) < EPS) return +2;\n if(norm(b) < norm(c)) return -2;\n return 0;\n}\n\nP projection(const L& l, const P& p) {\n double t = dot(p-l[0], l[0]-l[1]) / norm(l[0]-l[1]);\n return l[0] + t*(l[0]-l[1]);\n}\nbool intersectSP(const L &s, const P &p) {\n return abs(s[0]-p)+abs(s[1]-p)-abs(s[1]-s[0]) < EPS;\n}\ndouble distanceLP(const L &l, const P &p) {\n return abs(p - projection(l, p));\n}\ndouble distanceSP(const L &s, const P &p) {\n const P r = projection(s, p);\n if (intersectSP(s, r)) return abs(r - p);\n return min(abs(s[0] - p), abs(s[1] - p));\n}\n\nVP crosspointLC(const L &l, const C &c){\n VP ret;\n P mid = projection(l, c.p);\n double dlp = distanceLP(l, c.p);\n if(EQ(dlp, c.r)){\n ret.push_back(mid);\n return ret;\n }\n if(dlp > c.r){\n return ret;\n }\n P d = sqrt(c.r*c.r -dlp*dlp)/abs(l[1]-l[0]) *(l[1]-l[0]);\n ret.push_back(mid+d);\n ret.push_back(mid-d);\n return ret;\n}\nVP crosspointSC(const L &s, const C &c){\n VP ret;\n VP cp = crosspointLC(s,c);\n for(int i=0; i<(int)cp.size(); i++){\n if(dot(s[1]-s[0], cp[i]-s[0]) < EPS) continue;\n if(norm(s[1]-s[0]) < norm(cp[i]-s[0]) +EPS) continue;\n ret.push_back(cp[i]);\n }\n return ret;\n}\n\ndouble getangle(P p, P a, P b){\n a -= p;\n b -= p;\n return acos(dot(a,b)/(abs(a)*abs(b)));\n}\ndouble getarea(const VP &p) {\n double ret = 0;\n for (int i=0; i<(int)p.size(); i++){ \n ret += cross(p[i], p[(i+1)%p.size()]);\n }\n return ret*0.5;\n}\n/********************* end Library ***********************/\n\nint main(){\n while(1){\n /******************* Input ************************/\n int n,x,y,r;\n cin >> n >> x >> y >> r;\n if(n==0) break;\n \n C robot(P(x,y), r);\n VP v(n);\n for(int i=0; i<n; i++){\n int vx,vy;\n cin >> vx >> vy;\n v[i] = P(vx, vy);\n }\n //rotate the order of vertices to start from horizontal edge\n if(v[0].X == v[1].X){ \n v.push_back(v[0]);\n v.erase(v.begin());\n }\n /****************** end Input **********************/\n\n /*********** Move to the initial state ***********/\n //go right until touch the wall\n while(1){\n robot.p += P(1.0, 0);\n bool reach = false;\n for(int i=0; i<n; i++){\n if(distanceSP(L(v[i],v[(i+1)%n]), robot.p) < r){\n reach = true;\n break;\n }\n }\n if(reach) break;\n }\n //binary search\n P s = robot.p -P(1.0, 0);\n P e = robot.p;\n for(int i=0; i<nrep; i++){\n robot.p = (s+e)*0.5;\n bool reach = false;\n for(int i=0; i<n; i++){\n if(distanceSP(L(v[i],v[(i+1)%n]), robot.p) < r){\n reach = true;\n }\n }\n if(reach) e = robot.p;\n else s = robot.p;\n }\n\n bool onedge;\n int sidx;\n for(int i=0; i<n; i++){\n if(abs(robot.p -v[i]) < r+EPS){\n sidx = i;\n onedge = false;\n break;\n }\n if(distanceSP(L(v[i], v[(i+1)%n]), robot.p) < r+EPS){\n if(abs(robot.p -v[i]) > r+EPS && abs(robot.p -v[(i+1)%n]) > r+EPS){\n sidx = i;\n onedge = true;\n break;\n }\n }\n }\n /********************* end initial state **********************/\n\n /************************* Main Loop **************************/\n //move along the wall\n VP vlist;\n double area = 0.0;\n int step=0;\n cerr << fixed;\n cerr << setprecision(10);\n while(1){\n if(step++ ==50) break;\n /*\n cerr << robot.p << endl;\n if(onedge) cerr << \"edge \";\n else cerr << \"vertex \";\n cerr << sidx << endl;\n //cerr << \"area \" << area << endl;\n */\n \n /****************** Case 1. contact with the edge *************/\n if(onedge){\n //search the limit\n P dir = (v[(sidx+1)%n]-v[sidx])/abs(v[(sidx+1)%n]-v[sidx]);\n P limit = (sidx%2==0)? P(v[(sidx+1)%n].X, robot.p.Y) :P(robot.p.X, v[(sidx+1)%n].Y);\n P s,e;\n s = e = robot.p;\n while(1){\n e += dir;\n if(dot(limit-e, dir) < 0){\n e = limit;\n break;\n }\n bool reach = false;\n for(int i=0; i<n; i++){\n if(i==sidx || i==(sidx-1+n)%n) continue;\n if(distanceSP(L(v[i],v[(i+1)%n]), e) < r){\n reach = true;\n break;\n }\n }\n if(reach) break;\n }\n \n //binary search\n //cerr << s << \" \" << e << endl;\n for(int i=0; i<nrep; i++){\n robot.p = (s+e)*0.5;\n //cerr << robot.p << endl;\n bool reach = false;\n for(int i=0; i<n; i++){\n if(i==sidx || i==(sidx-1+n)%n) continue;\n //if turn left (avoid errors)\n if(i==(sidx+1)%n && cross(v[(sidx+1)%n]-v[sidx],v[(sidx+2)%n]-v[sidx])<0){\n continue;\n }\n if(distanceSP(L(v[i], v[(i+1)%n]), robot.p) < r){\n reach = true;\n break;\n }\n }\n if(reach) e = robot.p;\n else s = robot.p;\n }\n\n //calculate ridx and last;\n P last;\n double eval = -INF;\n int ridx;\n bool n_onedge;\n bool terminate = false;\n P cpedge = (sidx%2==0)? P(robot.p.X, v[sidx].Y) :P(v[sidx].X, robot.p.Y);\n for(int i=0; i<n; i++){\n if(i==sidx || i==(sidx-1+n)%n) continue; \n if(abs(robot.p -v[i]) < r+EPS){\n if(!vlist.empty() && EQ(vlist[0], v[i])){\n last = v[i];\n terminate = true;\n break;\n }\n if(dot(robot.p-cpedge, v[i]-cpedge) > eval) {\n last = v[i];\n eval = dot(robot.p-cpedge, v[i]-cpedge);\n ridx = i;\n n_onedge = false;\n }\n }\n if(distanceSP(L(v[i], v[(i+1)%n]), robot.p) < r +EPS){\n if(abs(robot.p -v[i])>r+EPS && abs(robot.p -v[(i+1)%n])>r+EPS){\n //except vertices\n VP cp = crosspointSC(L(v[i], v[(i+1)%n]), robot);\n if(!vlist.empty() && EQ(vlist[0], cp[0])){\n last = cp[0];\n terminate = true;\n break;\n }\n if(dot(robot.p-cpedge, cp[0]-cpedge) > eval) {\n last = cp[0];\n eval = dot(robot.p-cpedge, cp[0]-cpedge);\n ridx = i;\n n_onedge = true;\n }\n }\n }\n }\n \n //calculate area ...\n if(!vlist.empty() && EQ(vlist[0], cpedge)){\n terminate = true;\n last = cpedge;\n }\n double a = getangle(robot.p, last, cpedge);\n area += r*r*M_PI*a/(2*M_PI) -r*r*sin(a)*0.5;\n vlist.push_back(cpedge);\n vlist.push_back(last);\n if(terminate){\n break;\n }else{\n onedge = n_onedge;\n sidx = ridx;\n }\n }\n /************************* end Case 1. *********************/\n /************ Case 2. contact with the vertex *********************/\n else{\n //rotate\n double d;\n double dxy;\n P s, e;\n s = e = robot.p;\n //search limit\n if(sidx%2==1){\n d = (v[sidx].X-v[(sidx-1+n)%n].X > 0)? 1: -1;\n dxy = fabs(e.X -v[sidx].X);\n if(abs(2*e.X -round(2*e.X)) > EPS){\n dxy = floor(2*dxy) *0.5;\n }\n }else{\n d = (v[sidx].Y-v[(sidx-1+n)%n].Y > 0)? 1: -1;\n dxy = fabs(e.Y -v[sidx].Y);\n if(abs(2*e.Y -round(2*e.Y)) > EPS){\n dxy = floor(2*dxy) *0.5;\n }\n }\n\n P start = (v[sidx] -v[(sidx+1)%n]) *(r / abs(v[sidx] -v[(sidx+1)%n]));\n while(1){\n dxy += 0.5;\n if(dxy +EPS > r){\n e = v[sidx] + start*P(0,-1);\n break;\n }\n if(sidx%2==1){\n e = v[sidx] +d*P(dxy, sqrt(fabs(r*r-dxy*dxy)));\n }else{\n e = v[sidx] +d*P(-sqrt(fabs(r*r-dxy*dxy)), dxy);\n }\n bool reach = false;\n for(int i=0; i<n; i++){\n if(i==sidx || i==(sidx-1+n)%n) continue;\n if(distanceSP(L(v[i], v[(i+1)%n]), e) < r){\n reach = true;\n break;\n }\n }\n if(reach) break;\n }\n\n //binary search\n //cerr << s << \" \" << e << endl;\n for(int i=0; i<nrep; i++){\n double angle_s = getangle(v[sidx],start+v[sidx],s);\n double angle_e = getangle(v[sidx],start+v[sidx],e);\n double midangle = (angle_s +angle_e)*0.5;\n robot.p = v[sidx] + start*P(cos(midangle), -sin(midangle));\n //cerr << robot.p << endl;\n bool reach = false;\n for(int i=0; i<n; i++){\n if(i==sidx || i==(sidx-1+n)%n) continue;\n if(distanceSP(L(v[i], v[(i+1)%n]), robot.p) < r){\n reach = true;\n break;\n }\n }\n if(reach) e = robot.p;\n else s = robot.p;\n }\n\n //calculate ridx, last\n P last;\n double eval = -INF;\n int ridx;\n bool n_onedge;\n bool terminate = false;\n for(int i=0; i<n; i++){\n if(abs(robot.p -v[i]) < r+EPS){\n if(cross(robot.p-v[sidx], v[i]-v[sidx]) < EPS){\n if(i!=sidx && !vlist.empty() && EQ(vlist[0], v[i])){\n last = v[i];\n terminate = true;\n break;\n }\n if(dot(robot.p-v[sidx], v[i]-v[sidx]) > eval) {\n last = v[i];\n eval = dot(robot.p-v[sidx], v[i]-v[sidx]);\n ridx = i;\n n_onedge = false;\n }\n }\n }\n if(distanceSP(L(v[i], v[(i+1)%n]), robot.p) < r +EPS){\n if(abs(robot.p -v[i])>r+EPS && abs(robot.p -v[(i+1)%n])>r+EPS){\n //except vertices\n VP cp = crosspointSC(L(v[i], v[(i+1)%n]), robot);\n if(cross(robot.p-v[sidx], v[i]-v[sidx]) < EPS){\n if(!vlist.empty() && EQ(vlist[0], cp[0])){\n last = cp[0];\n terminate = true;\n break;\n }\n if(dot(robot.p-v[sidx], cp[0]-v[sidx]) > eval){\n last = cp[0];\n eval = dot(robot.p-v[sidx], cp[0]-v[sidx]);\n ridx = i;\n n_onedge = true;\n }\n }\n }\n }\n }\n if(last == v[sidx]){\n onedge = true;\n continue;\n }\n \n //calculate area ...\n if(!vlist.empty()){\n double a = getangle(robot.p, last, v[sidx]);\n area += r*r*M_PI*a/(2*M_PI) -r*r*sin(a)*0.5;\n }\n vlist.push_back(last);\n if(terminate){\n break;\n }else{\n onedge = n_onedge;\n sidx = ridx;\n }\n }\n /************************ end Case 2. *********************/\n }\n\n /*\n for(int i=0; i<(int)vlist.size(); i++){\n cerr << vlist[i] << endl;\n }\n cerr << getarea(vlist) << endl;\n */\n area += getarea(vlist);\n cout << fixed;\n cout << setprecision(10);\n cout << area << endl;\n }\n /*********************** end Main Loop ************************/\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3548, "score_of_the_acc": -1.1859, "final_rank": 6 }, { "submission_id": "aoj_2258_1484575", "code_snippet": "#include<cstdio>\n#include<vector>\n#include<complex>\n#include<cmath>\n#include<utility>\n#include<map>\n#include<algorithm>\n\nusing namespace std;\n\ntypedef double Real;\ntypedef complex<Real> Point;\ntypedef pair<Point,Point> Segment;\ntypedef pair<Point,Point> Line;\ntypedef vector<Point> Polygon;\n\nconst Real eps=1e-8;\nconst Real PI=acos(-1.0);\nconst Point NPoint=Point(NAN,NAN);\n\ntemplate<class T> bool eq(T a, T b){\n\treturn abs(a-b)<eps;\n}\n\ntemplate<class T> int sgn(T a){\n\tif(eq(a,(T)0)) return 0;\n\tif(a>0) return 1;\n\treturn -1;\n}\n\nvoid print(Point p,char ch='\\n'){\n\tprintf(\"(%f %f)%c\",p.real(),p.imag(),ch);\n}\n\nbool onSeg(Point a,Point b,Point c){\n\treturn eq(abs(a-b),abs(a-c)+abs(c-b));\n}\n\nbool onSeg(Segment s,Point p){\n\treturn onSeg(s.first,s.second,p);\n}\n\nstruct Circle:vector<Real>{\n\tPoint c;\n\tReal r;\n\tCircle(){}\n\tCircle(Point c,Real r):c(c),r(r){}\n};\n\nstruct Segs:Segment{\n\tvector<Point> pts;\n\tSegs(){}\n\tSegs(Point p,Point q){\n\t\tthis->first=p;\n\t\tthis->second=q;\n\t}\n\tSegs(Segment seg){\n\t\tthis->first=seg.first;\n\t\tthis->second=seg.second;\n\t}\n};\n\nReal arg(Point p){\n\treturn atan2(p.imag(),p.real());\n}\n\nReal normalize(Real theta){\n\twhile(theta>PI) theta-=PI*2;\n\twhile(theta<-PI) theta+=PI*2;\n\treturn theta;\n}\n\nPoint getPoint(Point c,Real r,Real ang){\n\treturn c+r*Point(cos(ang),sin(ang));\n}\n\nReal doP(Point a,Point b){\n\treturn (conj(a)*b).real();\n}\n\nReal crP(Point a,Point b){\n\treturn (conj(a)*b).imag();\n}\n\nPoint proj(Point p,Point b){\n\treturn b*doP(p,b)/norm(b);\n}\n\nPoint perp(Line l,Point a){\n\tPoint p=l.first,q=l.second;\n\treturn p+proj(a-p,q-p);\n}\n\nReal dLP(Line l,Point p){\n\tPoint h=perp(l,p);\n\treturn abs(h-p);\n}\n\nReal dPS(Point p,Segment s){\n\tPoint h=perp(s,p);\n\tif(onSeg(s,h)) return abs(h-p);\n\treturn min(abs(s.first-p),abs(s.second-p));\n}\n\nbool isPara_(Point p,Point q){\n\treturn sgn(crP(p,q))==0;\n}\n\nPoint iLL(Line l1,Line l2){\n\tif(isPara_(l1.second-l1.first,l2.second-l2.first)) return NPoint;\n\tPoint a1=l1.first,b1=l1.second;\n\tPoint a2=l2.first,b2=l2.second;\n\tReal num=crP(a2-a1,b1-a1);\n\tReal den=crP(b1-a1,b2-a2);\n\treturn a2+(b2-a2)*(num/den);\n}\n\nvoid iCL(Circle &c,Line l){//lが中心をとおるときに注意!!\n\tPoint h=perp(l,c.c);\n\tReal d=abs(h-c.c);\n\tif(sgn(d)==0){\n\t\tPoint p=l.first;\n\t\tif(eq(p,c.c)) p=l.second;\n\t\tReal ang=arg(p-c.c);\n\t\tc.push_back(normalize(ang));\n\t\tc.push_back(normalize(ang+PI));\n\t\treturn;\n\t}\n\tif(sgn(d-c.r)>0) return;\n\telse if(sgn(d-c.r)==0){\n\t\tc.push_back(arg(h-c.c));\n\t}else{\n\t\tReal ang=arg(h-c.c);\n\t\tReal ang2=acos(d/c.r);\n\t\tc.push_back(normalize(ang+ang2));\n\t\tc.push_back(normalize(ang-ang2));\n\t}\n}\n\nvoid iCC(Circle &c,Circle &c2){\n\tif(eq(c.c,c2.c)) return;\n\tReal d=abs(c.c-c2.c);\n\tReal r=c.r,r2=c2.r;\n\tif(sgn(d-(r+r2))>0) return;\n\telse if(sgn(d-(r+r2))==0){//out tangent\n\t\tReal ang=normalize(arg(c2.c-c.c));\n\t\tc.push_back(ang);\n\t}else if(sgn(d-abs(r-r2))>0){//intersect two points\n\t\tReal ang=arg(c2.c-c.c);\n\t\tReal ang2=acos((r*r+d*d-r2*r2)/(2*r*d));\n\t\tc.push_back(normalize(ang+ang2));\n\t\tc.push_back(normalize(ang-ang2));\n\t}else if(sgn(d-abs(r-r2))==0){//in tangent\n\t\tif(r>r2){\n\t\t\tc.push_back(normalize(arg(c2.c-c.c)));\n\t\t}else{\n\t\t\tc.push_back(normalize(-arg(c2.c-c.c)));\n\t\t}\n\t}else{//contained\n\t\treturn;\n\t}\n}\n\nbool inPoly_(Polygon poly,Point p){//ON is OUT\n\tbool in=false;\n\tfor(int i=0;i+1<poly.size();i++){\n\t\tPoint a=poly[i],b=poly[i+1];\n\t\tif(onSeg(a,b,p)) return false;\n\t\ta-=p,b-=p;\n\t\tif(sgn(a.imag()-b.imag())>0) swap(a,b);\n\t\tif(sgn(a.imag())<=0&&sgn(b.imag())>0){\n\t\t\tint s=sgn(crP(a,b));\n\t\t\tif(s==-1) in=!in;\n\t\t}\n\t}\n\treturn in;\n}\n\nbool isCounter_(Polygon poly){\n\tReal a=0;\n\tfor(int i=0;i+1<poly.size();i++){\n\t\tReal tmp=crP(poly[i],poly[i+1]);\n\t\ta+=tmp;\n\t}\n\treturn a>0;\n}\n\nstruct Edge{\n\tPoint to;\n\tPoint c;\n\tbool used;\n\tEdge(){}\n\tEdge(Point p):to(p){\n\t\tc=NPoint;\n\t\tused=false;\n\t}\n\tEdge(Point p,Point q):to(p),c(q){\n\t\tused=false;\n\t}\n};\n\ntypedef vector<Edge> Shape;\n\nvoid print(Shape sh){\n\tfor(int i=0;i<sh.size();i++){\n\t\tprint(sh[i].to);\n\t}\n\tprintf(\"\\n\");\n}\n\nstruct PLexCmp{\n\tbool operator()(const Point &p1,const Point &p2) const {\n\t\tif(p1.real()!=p2.real()) return p1.real()<p2.real();\n\t\treturn p1.imag()<p2.imag();\n\t}\n};\n\nvector<Point> pts;\nvector<Point> pts2;\nmap<Point,Point,PLexCmp> merged_pts;\n\nPolygon poly;\nReal R;\nPoint init_point;\n\nvector<Circle> cs;\nvector<Segs> segss;\n\nvoid getCs(){\n\tfor(int i=0;i+1<poly.size();i++){\n\t\tcs.push_back(Circle(poly[i],R));\n\t}\n}\n\nvoid getSegss(){\n\tfor(int i=0;i+1<poly.size();i++){\n\t\tPoint p1=poly[i],p2=poly[i+1];\n\t\t//R内側にずらす \n\t\tPoint dir=(p2-p1)/abs(p2-p1);\n\t\tdir*=(Point(0,1));\n\t\tdir*=R;\n\t\tPoint q1=p1+dir,q2=p2+dir;\n\t\tSegment tmp=Segment(q1,q2);\n\t\tPoint mid=(q1+q2)/(Real)2;\n//\t\tif(inPoly_(poly,mid)==false) continue;\n\t\t//Segment tmp=Segment(poly[i],poly[i+1]);\n\t\tSegs segs=tmp;\n//\t\tif(sgn(R*2-abs(segs.second-segs.first))>0) continue;\n\t\tPoint bVec=poly[i+1]-poly[i];\n\t\tbVec/=abs(bVec);\n\t\tPoint p=segs.first+bVec*R;\n\t\tif(onSeg(q1,q2,p)){\n\t\t\tsegs.pts.push_back(p);\n\t\t}\n\t\tp=segs.second-bVec*R;\n\t\tif(onSeg(q1,q2,p)){\n\t\t\tsegs.pts.push_back(p);\n\t\t}\n\t\tsegss.push_back(segs);\n//\t\tprint(segs.first,' ');\n//\t\tprint(segs.second);\n\t}\n}\n\nvoid processCircle(){\n\tfor(int i=0;i<cs.size();i++){\n\t\tfor(int j=0;j<cs.size();j++){\n\t\t\tif(i==j) continue;\n\t\t\tiCC(cs[i],cs[j]);\n\t\t}\n\t}\n\tfor(int i=0;i<cs.size();i++){\n\t\tfor(int j=0;j<segss.size();j++){\n\t\t\tCircle tmp=Circle(cs[i].c,cs[i].r);\n\t\t\tiCL(tmp,segss[j]);\n\t\t\tfor(int k=0;k<tmp.size();k++){\n\t\t\t\tPoint p=getPoint(tmp.c,tmp.r,tmp[k]);\n\t\t\t\tif(onSeg(segss[j].first,segss[j].second,p)){\n\t\t\t\t\tPoint dir=p-tmp.c;\n\t\t\t\t\tReal ang=arg(dir);\n\t\t\t\t\tcs[i].push_back(ang);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tfor(int i=0;i<cs.size();i++){\n\t\tfor(int j=0;j+1<poly.size();j++){\n\t\t\tSegment seg=Segment(poly[j],poly[j+1]);\n\t\t\tCircle tmp=Circle(cs[i].c,cs[i].r);\n\t\t\tiCL(tmp,seg);\n\t\t\tfor(int k=0;k<tmp.size();k++){\n\t\t\t\tPoint p=getPoint(tmp.c,tmp.r,tmp[k]);\n\t\t\t\tif(onSeg(seg.first,seg.second,p)){\n\t\t\t\t\tPoint dir=p-tmp.c;\n\t\t\t\t\tReal ang=arg(dir);\n\t\t\t\t\tcs[i].push_back(ang);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\n\nvoid processSegs(){\n\tfor(int i=0;i<segss.size();i++){\n\t\tPoint p1=segss[i].first,p2=segss[i].second;\n\t\tfor(int j=0;j<poly.size();j++){\n\t\t\tCircle tmp=Circle(poly[j],R);\n\t\t\tiCL(tmp,segss[i]);\n\t\t\tfor(int k=0;k<tmp.size();k++){\n\t\t\t\tPoint p=getPoint(tmp.c,tmp.r,tmp[k]);\n\t\t\t\tif(onSeg(p1,p2,p)&&(!eq(p1,p))&&(!eq(p2,p))){\n\t\t\t\t\tsegss[i].pts.push_back(p);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tfor(int j=0;j+1<poly.size();j++){\n\t\t\tSegment seg=Segment(poly[j],poly[j+1]);\n\t\t\tPoint p=iLL(seg,segss[i]);\n\t\t\tif(onSeg(p1,p2,p)&&onSeg(poly[j],poly[j+1],p)){\n\t\t\t\tCircle tmp=Circle(p,R);\n\t\t\t\tiCL(tmp,segss[i]);\n\t\t\t\tfor(int k=0;k<tmp.size();k++){\n\t\t\t\t\tPoint p=getPoint(tmp.c,tmp.r,tmp[k]);\n\t\t\t\t\tif(onSeg(p1,p2,p)){\n\t\t\t\t\t\tsegss[i].pts.push_back(p);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tsegss[i].pts.push_back(p1);\n\t\tsegss[i].pts.push_back(p2);\n\t}\n/*\tfor(int i=0;i<segss.size();i++){\n\t\tprintf(\"i=%d\\n\",i);\n\t\tfor(int j=0;j<segss[i].pts.size();j++){\n\t\t\tprint(segss[i].pts[j],' ');\n\t\t}\n\t\tprintf(\"\\n\");\n\t}*/\n}\n\nPoint cmp_c;\nbool p_cmp_dis(const Point &p1,const Point &p2){\n\tReal d1=abs(cmp_c-p1);\n\tReal d2=abs(cmp_c-p2);\n\tif(eq(d1,d2)) return true;\n\treturn d1<d2;\n}\n\nvoid getAllPoint(){\n\tfor(int i=0;i<cs.size();i++){\n\t\tsort(cs[i].begin(),cs[i].end());\n\t\tif(cs[i].empty()) continue;\n\t\tif(eq(cs[i].front(),-PI)&&eq(cs[i].back(),PI)){\n\t\t\tcs[i].pop_back();\n\t\t}\n\t\tfor(int j=0;j<cs[i].size();j++){\n\t\t\tPoint p=getPoint(cs[i].c,cs[i].r,cs[i][j]);\n\t\t\tpts.push_back(p);\n\t\t}\n\t}\n\tfor(int i=0;i<segss.size();i++){\n\t\tcmp_c=segss[i].first;\n\t\tsort(segss[i].pts.begin(),segss[i].pts.end(),p_cmp_dis);\n\t\tfor(int j=0;j<segss[i].pts.size();j++){\n\t\t\tpts.push_back(segss[i].pts[j]);\n\t\t}\n\t}\n}\n\nvoid mergePoints(){\n\tfor(int i=0;i<pts.size();i++){\n\t\tPoint p=NPoint;\n\t\tfor(int j=0;j<pts2.size();j++){\n\t\t\tif(eq(pts[i],pts2[j])){\n\t\t\t\tp=pts2[j];\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tif(isnan(p.real())){\n\t\t\tp=pts[i];\n\t\t\tpts2.push_back(p);\n\t\t\tmerged_pts[p]=p;\n\t\t}else{\n\t\t\tmerged_pts[pts[i]]=p;\n\t\t}\n\t}/*\n\tfor(int i=0;i<pts2.size();i++){\n\t\tprint(pts2[i]);\n\t}\n\tprintf(\"\\n\");*/\n}\n\nmap<Point,vector<Edge>,PLexCmp> edges;\n\nbool check(Point p){\n\tfor(int i=0;i+1<poly.size();i++){\n\t\tPoint p1=poly[i],p2=poly[i+1];\n\t\tSegment seg=Segment(p1,p2);\n\t\tReal d=dPS(p,seg);\n\t\tif(sgn(d-R)<0) return false;\n\t\td=abs(p1-p);\n\t\tif(sgn(d-R)<0) return false;\n\t}\n\tif(inPoly_(poly,p)==false) return false;\n\treturn true;\n}\n\nvoid getEdges(){\n\tfor(int i=0;i<cs.size();i++){\n\t\tsort(cs[i].begin(),cs[i].end());\n//\t\tprintf(\"center=\");\n//\t\tprint(cs[i].c);\n\t\tfor(int j=0;j<cs[i].size();j++){\n\t\t\tReal ang1=cs[i][j];\n\t\t\tReal ang2=cs[i][j+1<cs[i].size()?j+1:0];\n//\t\t\tprintf(\"%f %f\\n\",ang1,ang2);\n\t\t\tPoint p1=getPoint(cs[i].c,cs[i].r,cs[i][j]);\n\t\t\tPoint p2=getPoint(cs[i].c,cs[i].r,ang2);\n\t\t\tif(ang2<ang1) ang2+=PI*2;\n\t\t\tPoint mid=getPoint(cs[i].c,cs[i].r,(ang1+ang2)/2);\n\t\t\tbool flg=check(mid);\n\t\t\tif(eq(p1,p2)) continue;\n\t\t\tif(flg){\n\t\t\t\tPoint mp1=merged_pts[p1];\n\t\t\t\tPoint mp2=merged_pts[p2];\n\t\t\t\tEdge e=Edge(mp2,cs[i].c);\n\t\t\t\tedges[mp1].push_back(e);\n\t\t\t\te=Edge(mp1,cs[i].c);\n\t\t\t\tedges[mp2].push_back(e);\n/*\t\t\t\tprintf(\"circle, \");\n\t\t\t\tprint(mp1,' ');\n\t\t\t\tprint(mp2);\n\t\t\t\tprintf(\"mid=\");\n\t\t\t\tprint(mid);*/\n\t\t\t//\tprintf(\"%f %f\\n\",ang1,cs[i][j+1<cs[i].size()?j+1:0]);\n\t\t\t}\n\t\t}\n\t}\n\tfor(int i=0;i<segss.size();i++){\n\t\tvector<Point> &pts=segss[i].pts;\n\t\tcmp_c=segss[i].first;\n\t\tsort(pts.begin(),pts.end(),p_cmp_dis);\n\t\tfor(int j=0;j+1<pts.size();j++){\n\t\t\tPoint p1=pts[j];\n\t\t\tPoint p2=pts[j+1];\n\t\t\tif(eq(p1,p2)) continue;\n\t\t\tPoint mid=(p1+p2)/(Real)2;\n\t\t\tbool flg=check(mid);\n\t\t\tif(flg){\n\t\t\t\tPoint mp1=merged_pts[p1];\n\t\t\t\tPoint mp2=merged_pts[p2];\n\t\t\t\tEdge e=Edge(mp1);\n\t\t\t\tedges[mp2].push_back(e);\n\t\t\t\te=Edge(mp2);\n\t\t\t\tedges[mp1].push_back(e);\n/*\t\t\t\tprintf(\"segment, \");\n\t\t\t\tprint(mp1,' ');\n\t\t\t\tprint(mp2);*/\n\t\t\t}\n\t\t}\n\t}\n}\n\nvoid stroke(vector<Edge> &es,Point cur,Point prv){\n\tvector<Edge> &cur_es=edges[cur];\n\tfor(int i=0;i<cur_es.size();i++){\n\t\tEdge &e=cur_es[i];\n\t\tif(eq(e.to,prv)) continue;\n\t\tif(e.used){\n\t\t\treturn;\n\t\t}\n\t\tes.push_back(e);\n\t\te.used=true;\n\t\tstroke(es,e.to,cur);\n\t}\n}\n\nbool inShape_(Shape sh,Point p){\n\tPolygon poly;\n\tfor(int i=0;i<sh.size();i++){\n\t\tpoly.push_back(sh[i].to);\n\t}\n\tpoly.push_back(poly[0]);\n\tbool flg=inPoly_(poly,p);\n\tfor(int i=0;i<sh.size();i++){\n\t\tEdge e=sh[i];\n\t\tif(isnan(e.c.real())) continue;\n\t\tPoint p1=sh[i==0?sh.size()-1:i-1].to;\n\t\tPoint p2=sh[i].to;\n\t\tPoint c=sh[i].c;\n\t\tint s1=sgn(crP(c-p1,p2-p1));\n\t\tint s2=sgn(crP(p-p1,p2-p1));\n\t\tif(s1*s2<0){\n\t\t\tReal d=abs(c-p);\n\t\t\tif(sgn(d-R)<0){\n\t\t\t\tflg=!flg;\n\t\t\t}\n\t\t}\n\t}\n\treturn flg;\n}\n\nReal getInArea(Shape sh){\n\tReal res=0;\n\tReal dec=0;\n\tfor(int i=0;i<sh.size();i++){\n\t\tPoint p1=sh[i==0?sh.size()-1:i-1].to;\n\t\tPoint p2=sh[i].to;\n\t\tPoint c=sh[i].c;\n\t\tReal tmp=crP(p1,p2)/2.0;\n\t\tres+=tmp;\n\t\tif(isnan(c.real())==false){\n\t\t\tReal ang1=arg(p1-c);\n\t\t\tReal ang2=arg(p2-c);\n\t\t\tReal ang=ang2-ang1;\n\t\t\twhile(ang<0) ang+=PI*2;\n\t\t\tif(ang>PI) ang=PI*2-ang;\n\t\t\tReal tmp2=R*R*sin(ang)/(Real)2.0;\n\t\t\tReal tmp3=R*R*ang/2.0;\n\t\t\tdec+=(tmp3-tmp2);\n\t\t}\n\t}\n\tif(res<0) res*=-1;\n//\tprintf(\"%f %f\\n\",res,dec);\n\treturn res-dec;\n}\n\nReal getOutArea(Shape sh){\n\tReal res1=0,res2=0;\n\tfor(int i=0;i<sh.size();i++){\n\t\tEdge e=sh[i];\n\t\tif(isnan(e.c.real())){\n\t\t\tPoint t=e.to;\n\t\t\tPoint s=sh[i==0?sh.size()-1:i-1].to;\n\t\t\tReal l=abs(t-s);\n\t\t\tReal area=l*R;\n\t\t\tres1+=area;\n\t\t}\n\t}\n\tfor(int i=0;i<sh.size();i++){\n\t\tint j=i==0?sh.size()-1:i-1;\n\t\tint k=j==0?sh.size()-1:j-1;\n\t\tPoint p1,p2,p3,c1,c2;\n\t\tp3=sh[i].to;\n\t\tp2=sh[j].to;\n\t\tp1=sh[k].to;\n\t\tc2=sh[i].c;\n\t\tc1=sh[j].c;\n\t\tPoint q1=isnan(c1.real())?p1:c1;\n\t\tPoint q2=isnan(c2.real())?p3:c2;\n\t\tReal ang=arg(q2-p2)-arg(q1-p2);\n//\t\tprint(p2);\n//\t\tprint(q1);\n//\t\tprint(q2);\n//\t\tprintf(\"%f %f\\n\",arg(q2-p2),arg(q1-p2));\n\t\twhile(sgn(ang)<0) ang+=PI*2;\n\t\tif(isnan(c1.real())) ang-=PI/2;\n\t\tif(isnan(c2.real())) ang-=PI/2;\n\t\tReal area=R*R*ang/2;\n//\t\tprintf(\"+=%f\\n\",ang);\n\t\tres2+=area;\n/*\t\tif(isnan(c1.real())!=isnan(c2.real())){\n\t\t\tReal ang1,ang2;\n\t\t\tif(isnan(c1.real())==false){\n\t\t\t\tang1=arg(c1-p2);\n\t\t\t\tang2=arg(p3-p2);\n\t\t\t}else{\n\t\t\t\tang1=arg(p1-p2);\n\t\t\t\tang2=arg(c2-p2);\n\t\t\t}\n\t\t\tReal ang=ang2-ang1;\n\t\t\twhile(ang<PI/2) ang+=PI*2;\n\t\t\tif(sgn(ang-PI)>0) ang=PI*2-ang;\n\t\t\tang-=PI/2;\n\t\t\tReal area=R*R*ang/2;\n\t\t\tres2+=area;\n\t\t\tprintf(\"+=%f\\n\",ang);\n\t\t}else if(isnan(c1.real())==true){\n\t\t\tPoint v2=p3-p2;\n\t\t\tPoint v1=p2-p1;\n\t\t\tif(sgn(crP(v1,v2))==0) continue;\n\t\t\tReal ang=PI/2;\n\t\t\tReal area=R*R*ang/2;\n\t\t\tprintf(\"+=%f\\n\",ang);\n\t\t\tres2+=area;\n\t\t}else{\n\t\t\tReal ang1=arg(c2-p2);\n\t\t\tReal ang2=arg(c1-p2);\n\t\t\tReal ang=ang2-ang1;\n\t\t\twhile(ang<0) ang+=PI*2;\n\t\t\tif(ang>PI) ang=PI*2-ang;\n\t\t\tReal area=R*R*ang/2;\n\t\t\tprintf(\"+=%f\\n\",ang);\n\t\t\tres2+=area;\n\t\t}*/\n\t}\n\tReal res3=0;\n\tfor(int i=0;i<sh.size();i++){\n\t\tPoint p2=sh[i].to;\n\t\tPoint p1=sh[i==0?sh.size()-1:i-1].to;\n\t\tPoint c=sh[i].c;\n\t\tif(isnan(c.real())==false){\n\t\t\tReal ang1=arg(p2-c);\n\t\t\tReal ang2=arg(p1-c);\n\t\t\tReal ang=ang2-ang1;\n\t\t\twhile(ang<0) ang+=PI*2;\n\t\t\tif(ang>PI) ang=PI*2-ang;\n\t\t\tReal tmp=R*R*ang/(Real)2;\n\t\t\tres3+=tmp;\n\t\t}\n\t}\n//\tprintf(\"%f %f %f\\n\",res1,res2,res3);\n\treturn res1+res2+res3;\n}\n\nShape reverse_shape(Shape sh){\n\tShape res;\n\tif(sh.size()==0) return sh;\n\tfor(int i=sh.size()-1;i>0;i--){\n\t\tEdge e=Edge(sh[i-1].to,sh[i].c);\n\t\tres.push_back(e);\n\t}\n\tres.push_back(Edge(sh.back().to,sh[0].c));\n\treturn res;\n}\n\nPolygon toPoly(Shape sh){\n\tPolygon res;\n\tif(sh.size()==0) return res;\n\tfor(int i=0;i<sh.size();i++){\n\t\tres.push_back(sh[i].to);\n\t}\n\tres.push_back(res[0]);\n\treturn res;\n}\n\n\nReal getArea(Shape sh){\n/*\tfor(int i=0;i<sh.size();i++){\n\t\tprint(sh[i].to);\n\t}*/\n\tPolygon tmp=toPoly(sh);\n//\tprint(sh);\n\tif(!isCounter_(tmp)){\n\t\tsh=reverse_shape(sh);\n\t}\n//\tprint(sh);\n\tReal area1=getInArea(sh);\n\tReal area2=getOutArea(sh);\n\treturn area1+area2;\n}\n\nReal solve(){\n\tgetCs();\n\tgetSegss();\n\tprocessCircle();\n\tprocessSegs();\n\tgetAllPoint();\n\tmergePoints();\n\tgetEdges();\n\tmap<Point,vector<Edge>,PLexCmp> ::iterator it=edges.begin();\n\tfor(;it!=edges.end();it++){\n\t\tvector<Edge> &vec=it->second;\n\t\tfor(int i=0;i<vec.size();i++){\n\t\t\tif(vec[i].used==false){\n\t\t\t\tPoint prv=it->first;\n\t\t\t\tPoint cur=vec[i].to;\n\t\t\t\tvector<Edge> sh;\n\t\t\t\tsh.push_back(vec[i]);\n\t\t\t\tvec[i].used=true;\n\t\t\t\tstroke(sh,cur,prv);\n\t//\t\t\tprint(sh);\n\t\t\t\tif(inShape_(sh,init_point)){\n\t\t\t\t\tReal res=getArea(sh);\n\t\t\t\t\treturn res;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\treturn -1;\n}\n\nint test_cnt=0;\n\nvoid input(){\n\ttest_cnt++;\n\tint n,x,y,r;\n\tscanf(\"%d%d%d%d\",&n,&x,&y,&r);\n\tif(n==0) exit(0);\n\tinit_point=Point(x,y);\n\tR=r;\n\tfor(int i=0;i<n;i++){\n\t\tint x,y;\n\t\tscanf(\"%d%d\",&x,&y);\n\t\tpoly.push_back(Point(x,y));\n\t}\n\tpoly.push_back(poly[0]);\n}\n\nvoid init(){\n\tpts.clear();\n\tpts2.clear();\n\tmap<Point,Point,PLexCmp> tmp_merged_pts;\n\tswap(merged_pts,tmp_merged_pts);\n\tmap<Point,vector<Edge>,PLexCmp> tmp_edges;\n\tswap(edges,tmp_edges);\n\tcs.clear();\n\tsegss.clear();\n\tpoly.clear();\n}\n\nint main(){\n\twhile(true){\n\t\tinit();\n\t\tinput();\n\t\tReal ans=solve();\n\t\tprintf(\"%.10f\\n\",ans);\n\t//\tbreak;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 1344, "score_of_the_acc": -0.2124, "final_rank": 3 }, { "submission_id": "aoj_2258_1153430", "code_snippet": "#include <cmath>\n#include <algorithm>\n#include <vector>\n#include <iostream>\n#include <complex>\nusing namespace std;\n\n#define REP(i,x) for(int i=0;i<(int)(x);i++)\n#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();i++)\n#define ALL(container) (container).begin(), (container).end()\n#define mp(a,b) make_pair(a, b)\n\nnamespace geom{\n#define X real()\n#define Y imag()\n#define at(i) ((*this)[i])\n#define SELF (*this)\n\tenum {TRUE = 1, FALSE = 0, BORDER = -1};\n\ttypedef int BOOL;\n\ttypedef long double R;\n\tconst R INF = 1e10;\n\tR EPS = 1e-10;\n\tconst R PI = 3.1415926535897932384626;\n\tinline int sig(const R &x) { return (abs(x) < EPS ? 0 : x > 0 ? 1 : -1); }\n\ttypedef complex<R> P;\n\tinline R norm(const P &p){return p.X*p.X+p.Y*p.Y;}\n\tinline R inp(const P& a, const P& b){return (conj(a)*b).X;}\n\tinline R outp(const P& a, const P& b){return (conj(a)*b).Y;}\n\tinline P unit(const P& p){return p/abs(p);}\n\tinline P proj(const P &s, const P &t){return t*inp(s, t)/norm(t);}\n\tstruct L : public vector<P>{\t// line\n\t\tL(const P &p1, const P &p2){this->push_back(p1);this->push_back(p2);}\n\t\tL(){}\n\t\tP dir()const {return at(1) - at(0);}\n\t};\n\tstruct S : public L{\t// segment\n\t\tS(const P &p1, const P &p2):L(p1, p2){}\n\t\tS(){}\n\t\tBOOL online(const P &p)const {\n\t\t\tif(!sig(norm(p - at(0))) || !sig(norm(p - at(1)))) return BORDER;\n\t\t\treturn !sig(abs(at(0)-p) + abs(at(1) - p) - abs(at(0) - at(1)));\n\t\t}\n\t};\n\tstruct C : public P{\n\t\tC(){}\n\t\tC(const P& p, const R r):P(p), r(r){}\n\t\tR r;\n\t};\n\tstruct G : public vector<P>{\n\t\tG(size_type size=0):vector(size){}\n\t\tS edge(int i)const {return S(at(i), at(i+1 == size() ? 0 : i+1));}\n\t\tR area()const {\n\t\t\tR sum = 0;\n\t\t\tREP(i, size()) sum += outp(at(i), at((i+1)%size()));\n\t\t\treturn abs(sum / 2.);\n\t\t}\n\t};\n\tinline P proj(const P &s, const L &t){return t[0] + proj(s-t[0], t[1]-t[0]);}\n#undef SELF\n#undef at\n}\n\nusing namespace geom;\n\nnamespace std{\n\tbool operator<(const P &a, const P &b){return sig(a.X-b.X) ? a.X < b.X : a.Y+EPS < b.Y;}\n\tbool operator==(const P &a, const P &b){return abs(a-b) < EPS;}\n\tistream& operator>>(istream &is, P &p){R x,y;is>>x>>y;p=P(x, y);return is;}\n\tistream& operator>>(istream &is, C &c){return is >> (P &)c >> c.r;}\n}\n\nint n;\n\nC icpc;\nG g;\n\npair<P, R> getType(const P &p){\n\tREP(i, n){\n\t\tconst S s = g.edge(i);\n\t\tif(s.online(p) == TRUE) return mp(s.dir(), abs(s[1] - p));\n\t}\n\tREP(i, n){\n\t\tconst P q = g[i];\n\t\tif(!sig(abs(q - p))){\n\t\t\tP tar = q + unit(g.edge(i).dir())*P(0, 1)*icpc.r;\n\t\t\tif(!sig(abs(icpc - tar))) return mp(g.edge(i).dir(), abs(g.edge(i).dir()));\n\t\t\treturn mp(tar, arg((tar - p)/(icpc - p)));\n\t\t}\n\t}\n}\n\nP getNavigator(){\n\tvector<pair<R, P>> isc;\n\tREP(i, n){\n\t\tconst S s = g.edge(i);\n\t\tP q = proj(icpc, s);\n\t\tif(s.online(q) == TRUE && !sig(abs(q - icpc) - icpc.r))\n\t\t\tisc.emplace_back(arg(q - icpc), q);\n\t}\n\tREP(i, n){\n\t\tconst P q = g[i];\n\t\tif(!sig(abs(q - icpc) - icpc.r)) isc.emplace_back(arg(q - icpc), q);\n\t}\n\tsort(ALL(isc));\n\tpair<R, P> pp(.0, P(0, 0));\n\tREP(i, isc.size()){\n\t\tconst auto cur = isc[i];\n\t\tconst auto nxt = isc[(i+1)%isc.size()];\n\t\tR nxtarg = i+1 == isc.size() ? nxt.first + 2*PI : nxt.first;\n\t\tpp = max(pp, mp(nxtarg - cur.first, cur.second));\n\t}\n\t\n\treturn pp.second;\n}\n\nR straight(P dir, R d = INF){\n\tREP(i, n){\n\t\tconst S s = g.edge(i);\n\t\tif(sig(arg(dir*P(0, 1)/s.dir())) || inp(proj(icpc, s) - icpc, dir) < EPS || s.online(proj(icpc, s)) != TRUE) continue;\n\t\td = min(d, abs(proj(icpc, s) - icpc) - icpc.r);\n\t}\n\tREP(i, n){\n\t\tconst P q = g[i];\n\t\tR r = abs(q - proj(q, L(icpc, icpc+dir)));\n\t\tif(r + EPS > icpc.r || inp(q - icpc, dir) < EPS) continue;\n\t\td = min(d, abs(proj(q, L(icpc, icpc+dir)) - icpc) - sqrt(icpc.r*icpc.r-r*r));\n\t}\n\treturn d;\n}\n\nR rot(P nav, R r){\n\tREP(i, n){\n\t\tconst S s = g.edge(i);\n\t\tif(abs(proj(nav, s) - nav) < EPS) continue;\n\t\tR t = arg((proj(nav, s) - nav)/(icpc - nav));\n\t\tif(t > 0 && t < PI) continue;\n\t\tR d = abs(proj(nav, s) - nav);\n\t\tif(d > 2*icpc.r - EPS) continue;\n\t\tR x = arg(s.dir()) - asin((d-icpc.r) / icpc.r) - arg(icpc-nav);\n\t\tif(d-icpc.r < 0) x = arg(s.dir()) - arg(icpc-nav) + asin((icpc.r-d) / icpc.r);\n\t\tif(x < -PI) x += 2*PI;\n\t\tif(x > PI) x -= 2*PI;\n\t\tif(x < -EPS) r = max(r, x);\n\t}\n\tREP(i, n){\n\t\tconst P q = g[i];\n\t\tR d = abs(q-nav);\n\t\tif(d > 2*icpc.r - EPS || d < EPS) continue;\n\t\tP med = (nav + q) * (R).5;\n\t\tR d2 = sqrt(max((R).0, icpc.r*icpc.r - d*d*(R).25));\n\t\tP p1 = med + unit(med - nav)*P(0, 1)*d2;\n\t\tP p2 = med + unit(med - nav)*P(0, -1)*d2;\n\n\t\tif(arg((p1 - nav)/(icpc-nav)) < -EPS) r = max(r, arg((p1 - nav)/(icpc-nav)));\n\t\tif(arg((p2 - nav)/(icpc-nav)) < -EPS) r = max(r, arg((p2 - nav)/(icpc-nav)));\n\t}\n\treturn r;\n}\nint main(){\n\tios::sync_with_stdio(false);\n\twhile(cin >> n >> icpc, n){\n\t\tg.resize(n);\n\t\tREP(i, n) cin >> g[i];\n\t\tR d = straight(P(1, 0));\n\t\ticpc += P(1, 0)*d;\n\t\tR ans = 0;\n\t\tG root;\n\t\tP prev = getNavigator();\n\t\tP start = icpc;\n\t\twhile(1){\n\t\t\tP nav = getNavigator();\n\t\t\tif(sig(abs(prev-nav))){\n\t\t\t\troot.push_back(prev);\n\t\t\t\troot.push_back(icpc);\n\t\t\t\tans += icpc.r * icpc.r * arg((nav-icpc)/(prev-icpc)) * .5;\n\t\t\t}\n\t\t\tif(!sig(abs(icpc-start)) && sig(root.area())) break;\n\t\t\troot.push_back(nav);\n\t\t\tprev = nav;\n\t\t\tauto t = getType(nav);\n\t\t\tif(t.second > 0){\n\t\t\t\tP picpc = icpc;\n\t\t\t\tprev += unit(t.first) * straight(t.first, t.second);\n\t\t\t\ticpc += unit(t.first) * straight(t.first, t.second);\n\t\t\t\tif(S(picpc, icpc).online(start) == TRUE && sig(root.area())) break;\n\t\t\t}else{\n\t\t\t\t(P &)icpc = nav + (icpc - nav)*polar((R)1., rot(nav, t.second));\n\t\t\t\tif(sig(abs(icpc-start)) && !sig(abs(start - nav) - icpc.r) && sig(root.area())) break;\n\t\t\t}\n\t\t}\n\t\tprintf(\"%.10f\\n\", (double)(ans + root.area()));\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1316, "score_of_the_acc": 0, "final_rank": 1 }, { "submission_id": "aoj_2258_1152444", "code_snippet": "#include <cstdio>\n#include <cmath>\n#include <cstring>\n#include <cstdlib>\n#include <climits>\n#include <ctime>\n#include <queue>\n#include <stack>\n#include <algorithm>\n#include <list>\n#include <vector>\n#include <set>\n#include <map>\n#include <iostream>\n#include <deque>\n#include <complex>\n#include <string>\n#include <iomanip>\n#include <sstream>\n#include <bitset>\n#include <valarray>\n#include <unordered_map>\n#include <iterator>\n#include <assert.h>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned int uint;\ntypedef unsigned char uchar;\ntypedef unsigned long long ull;\ntypedef pair<int, int> pii;\ntypedef pair<ll, ll> pll;\ntypedef vector<int> vi;\n\n#define REP(i,x) for(int i=0;i<(int)(x);i++)\n#define REPS(i,x) for(int i=1;i<=(int)(x);i++)\n#define RREP(i,x) for(int i=((int)(x)-1);i>=0;i--)\n#define RREPS(i,x) for(int i=((int)(x));i>0;i--)\n#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();i++)\n#define RFOR(i,c) for(__typeof((c).rbegin())i=(c).rbegin();i!=(c).rend();i++)\n#define ALL(container) (container).begin(), (container).end()\n#define RALL(container) (container).rbegin(), (container).rend()\n#define SZ(container) ((int)container.size())\n#define mp(a,b) make_pair(a, b)\n#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );\n\ntemplate<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }\ntemplate<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }\ntemplate<class T> ostream& operator<<(ostream &os, const vector<T> &t) {\nos<<\"[\"; FOR(it,t) {if(it!=t.begin()) os<<\",\"; os<<*it;} os<<\"]\"; return os;\n}\ntemplate<class T> ostream& operator<<(ostream &os, const set<T> &t) {\nos<<\"{\"; FOR(it,t) {if(it!=t.begin()) os<<\",\"; os<<*it;} os<<\"}\"; return os;\n}\ntemplate<class S, class T> ostream& operator<<(ostream &os, const pair<S,T> &t) { return os<<\"(\"<<t.first<<\",\"<<t.second<<\")\";}\ntemplate<class S, class T> pair<S,T> operator+(const pair<S,T> &s, const pair<S,T> &t){ return pair<S,T>(s.first+t.first, s.second+t.second);}\ntemplate<class S, class T> pair<S,T> operator-(const pair<S,T> &s, const pair<S,T> &t){ return pair<S,T>(s.first-t.first, s.second-t.second);}\n\nnamespace geom{\n#define X real()\n#define Y imag()\n#define at(i) ((*this)[i])\n#define SELF (*this)\n\tenum {TRUE = 1, FALSE = 0, BORDER = -1};\n\ttypedef int BOOL;\n\ttypedef long double R;\n\tconst R INF = 1e10;\n\tR EPS = 1e-10;\n\tconst R PI = 3.1415926535897932384626;\n\tinline int sig(const R &x) { return (abs(x) < EPS ? 0 : x > 0 ? 1 : -1); }\n\tinline BOOL less(const R &x, const R &y) {return sig(x-y) ? x < y : BORDER;}\n\ttypedef complex<R> P;\n\tinline R norm(const P &p){return p.X*p.X+p.Y*p.Y;}\n\tinline R inp(const P& a, const P& b){return (conj(a)*b).X;}\n\tinline R outp(const P& a, const P& b){return (conj(a)*b).Y;}\n\tinline P unit(const P& p){return p/abs(p);}\n\tinline P proj(const P &s, const P &t){return t*inp(s, t)/norm(t);}\n\tinline int ccw(const P &s, const P &t, const P &p, int adv=0){\n\t\tint res = sig(outp(t-s, p-s));\n\t\tif(res || !adv) return res;\n\t\tif(sig(inp(t-s, p-s)) < 0) return -2;\t// p-s-t\n\t\tif(sig(inp(s-t, p-t)) < 0) return 2;\t// s-t-p\n\t\treturn 0;\t\t\t\t\t\t\t\t// s-p-t\n\t}\n\t\n\t\n\tstruct L : public vector<P>{\t// line\n\t\tL(const P &p1, const P &p2){this->push_back(p1);this->push_back(p2);}\n\t\tL(){}\n\t\tP dir()const {return at(1) - at(0);}\n\t\tBOOL online(const P &p)const {return !sig(outp(p-at(0), dir()));}\n\t};\n\tstruct S : public L{\t// segment\n\t\tS(const P &p1, const P &p2):L(p1, p2){}\n\t\tS(){}\n\t\tBOOL online(const P &p)const {\n\t\t\tif(!sig(norm(p - at(0))) || !sig(norm(p - at(1)))) return BORDER;\n\t\t\treturn !sig(abs(at(0)-p) + abs(at(1) - p) - abs(at(0) - at(1)));\n\t\t}\n\t};\n\tstruct C : public P{\n\t\tC(){}\n\t\tC(const P& p, const R r):P(p), r(r){}\n\t\tR r;\n\t\tBOOL inside(const P& p)const { return less(norm(p-SELF), r*r);}\n\t};\n\tstruct F : public C{\n\t\tR s, t;\n\t\tF(const C &c, R ss, R tt):C(c), s(ss), t(tt){\n\t\t\tif(PI < s) s -= 2*PI;\n\t\t\tif(PI < t) t -= 2*PI;\n\t\t}\n\t\tBOOL inside(const P& p)const {\n\t\t\tP v = p - SELF;\n\t\t\tif(!sig(norm(v))) return BORDER;\n\t\t\tR a = arg(v);\n\t\t\tif(t < s){\n\t\t\t\tif((!less(s, a) && !less(a, t)) || !less(norm(v), r*r)) return FALSE;\n\t\t\t\treturn less(s, a) | less(a, t) | less(norm(v), r*r);\n\t\t\t}else{\n\t\t\t\tif(!less(s, a) || !less(a, t) || !less(norm(v), r*r)) return FALSE;\n\t\t\t\treturn less(s, a) | less(a, t) | less(norm(v), r*r);\n\t\t\t}\n\t\t}\n\t};\n\tP crosspoint(const L &l, const L &m);\n\tstruct G : public vector<P>{\n\t\tG(size_type size=0):vector(size){}\n\t\tS edge(int i)const {return S(at(i), at(i+1 == size() ? 0 : i+1));}\n\t\tBOOL contains(const P &p)const {\n\t\t\tR sum = .0;\n\t\t\tREP(i, size()){\n\t\t\t\tif(S(at(i), at((i+1)%size())).online(p)) return BORDER;\t// online\n\t\t\t\tsum += arg((at(i) - p) / (at((i+1)%size()) - p));\n\t\t\t}\n\t\t\treturn !!sig(sum);\n\t\t}\n\t\tR area()const {\n\t\t\tR sum = 0;\n\t\t\tREP(i, size()) sum += outp(at(i), at((i+1)%size()));\n\t\t\treturn abs(sum / 2.);\n\t\t}\n\t\tP gp()const {\n\t\t\tP r(.0, .0);\n\t\t\tREP(i, size()){\n\t\t\t\tconst S &s = edge(i);\n\t\t\t\tr += (s[0]+s[1])*outp(s[0], s[1]);\n\t\t\t}\n\t\t\treturn r / (6*area());\n\t\t}\n\t\tG convex_hull(bool online = false) {\n\t\t\tif(size() < 2) return *this;\n\t\t\tsort(ALL(*this));\n\t\t\tG r;\n\t\t\tr.resize((int)size()*2);\n\t\t\tint k=0;\n\t\t\tfor(int i=0;i<size();r[k++]=at(i++))\n\t\t\t\twhile(k>1 && ccw(r[k-2], r[k-1], at(i)) < 1-online) k--;\n\t\t\tint t = k;\n\t\t\tfor(int i=(int)size()-1;i>=0;r[k++]=at(i--))\n\t\t\t\twhile(k>t && ccw(r[k-2], r[k-1], at(i)) < 1-online) k--;\n\t\t\tr.resize(k-1);\n\t\t\treturn r;\n\t\t}\n\t\tG cut(const L &l)const {\n\t\t\tG g;\n\t\t\tREP(i, size()){\n\t\t\t\tconst S &s = edge(i);\n\t\t\t\tif(ccw(l[0], l[1], s[0], 0) >= 0) g.push_back(s[0]);\n\t\t\t\tif(ccw(l[0], l[1], s[0], 0) * ccw(l[0], l[1], s[1], 0) < 0)\n\t\t\t\t\tg.push_back(crosspoint(s, l));\n\t\t\t}\n\t\t\treturn g;\n\t\t}\n\t\tG Voronoi(const vector<P> &p, const int t)const {\n\t\t\tG g = *this;\n\t\t\tREP(i, p.size())if(i!=t){\n\t\t\t\tconst P m = (p[t]+p[i])*(R)0.5;\n\t\t\t\tg = g.cut(L(m, m+(p[i]-p[t])*P(0, 1)));\n\t\t\t}\n\t\t\treturn g;\n\t\t}\n\t};\n\n\tinline P proj(const P &s, const L &t){return t[0] + proj(s-t[0], t[1]-t[0]);}\n\tinline P reflect(const P &s, const L &t){return (R)2.*proj(s, t) - s;}\n\tinline S reflect(const S &s, const L &t){return S(reflect(s[0], t), reflect(s[1], t));}\n\tBOOL intersect(const S &s, const S &t){\n\t\tconst int p = ccw(t[0], t[1], s[0], 1) * ccw(t[0], t[1], s[1], 1);\n\t\tconst int q = ccw(s[0], s[1], t[0], 1) * ccw(s[0], s[1], t[1], 1);\n\t\treturn (p>0||q>0) ? FALSE : (!p||!q) ? BORDER : TRUE;\n\t}\n\tBOOL intersect(const S &s, const L &l){\n\t\tif(l.online(s[0]) || l.online(s[1])) return BORDER;\n\t\treturn (sig(outp(l.dir(), s[0]-l[0])) * sig(outp(l.dir(), s[1]-l[0])) <= 0);\n\t}\n\tR dist2(const L &l, const P &p){return norm(outp(l.dir(), p - l[0])) / norm(l.dir());}\n\tR dist2(const S &s, const P &p){\n\t\tif(inp(p-s[0], s.dir()) < EPS) return norm(p - s[0]);\n\t\tif(inp(p-s[1], -s.dir()) < EPS) return norm(p - s[1]);\n\t\treturn dist2((const L &)s, p);\n\t}\n\tR dist2(const S &s, const L &l){\n\t\treturn intersect(s, l) ? .0 : min(dist2(l, s[0]), dist2(l, s[1]));\n\t}\n\tR dist2(const S &s, const S &t){\n\t\treturn intersect(s, t) ? .0 : min(min(dist2(s, t[0]), dist2(t, s[0])), \n\t\t\t\t\t\t\t\t\t \t min(dist2(s, t[1]), dist2(t, s[1])));\n\t}\n\ttemplate <class T> R dist2(const G &g, const T& t){ // todo: テ・ツ??ゥツδィテ」ツ?ォテ・ツョツ古・ツ?ィテ」ツ?ォテ・ツ青ォテ」ツ?セテ」ツつ古」ツつ凝・ツ?エテ・ツ青?\n\t\tR res = INF;\n\t\tREP(i, g.size()) res = min(res, dist2(g.edge(i), t));\n\t\treturn res;\n\t}\n\ttemplate<class S, class T> R dist(const S& s, const T& t){return sqrt(dist2(s, t));}\n\tinline BOOL intersect(const C &a, const C &b){\n\t\treturn less((a.r-b.r)*(a.r-b.r), norm(a-b)) + less(norm(a-b), (a.r+b.r)*(a.r+b.r)) - 1;\n\t}\n\n#undef SELF\n#undef at\n}\n\nusing namespace geom;\n\nint f = 0;\nnamespace std{\n\tbool operator<(const P &a, const P &b){return sig(a.X-b.X) ? a.X < b.X : a.Y+EPS < b.Y;}\n\tbool operator==(const P &a, const P &b){return abs(a-b) < EPS;}\n\tistream& operator>>(istream &is, P &p){R x,y;is>>x>>y;p=P(x, y);return is;}\n\tistream& operator>>(istream &is, L &l){l.resize(2);return is >> l[0] >> l[1];}\n\tistream& operator>>(istream &is, C &c){return is >> (P &)c >> c.r;}\n\tconst R B = 500;\n\tconst R Z = 30;\n\tostream& operator<<(ostream &os, const C &c){return os << \"circle(\"<<B+Z*(c.X)<<\", \"<<1000-B-Z*(c.Y)<<\", \"<<Z*(c.r)<<\")\";}\n\tostream& operator<<(ostream &os, const P &p){return os << C(p, 2./Z);}\n\tostream& operator<<(ostream &os, const S &s){return os << \"line(\"<<B+Z*(s[0].X)<<\", \"<<1000-B-Z*(s[0].Y)<<\", \"<<B+Z*(s[1].X)<<\", \"<<1000-B-Z*(s[1].Y)<<\")\";}\n\tostream& operator<<(ostream &os, const G &g){REP(i, g.size()) cout << g.edge(i) << endl;return os;}\n\n}\n\nint n;\n\nC icpc;\nG g;\n\npair<P, R> getType(const P &p){\n\tREP(i, n){\n\t\tconst S s = g.edge(i);\n\t\tif(s.online(p) == TRUE) return mp(s.dir(), abs(s[1] - p));\n\t}\n\tREP(i, n){\n\t\tconst P q = g[i];\n\t\tif(!sig(abs(q - p))){\n\t\t\tP tar = q + unit(g.edge(i).dir())*P(0, 1)*icpc.r;\n\t\t\tif(!sig(abs(icpc - tar))) return mp(g.edge(i).dir(), abs(g.edge(i).dir()));\n\t\t\treturn mp(tar, arg((tar - p)/(icpc - p)));\n\t\t}\n\t}\n\tassert(false);\n}\n\nP getNavigator(){\n\tvector<pair<R, P>> isc;\n\tREP(i, n){\n\t\tconst S s = g.edge(i);\n\t\tP q = proj(icpc, s);\n\t\tif(s.online(q) == TRUE && !sig(abs(q - icpc) - icpc.r))\n\t\t\tisc.emplace_back(arg(q - icpc), q);\n\t}\n\tREP(i, n){\n\t\tconst P q = g[i];\n\t\tif(!sig(abs(q - icpc) - icpc.r)) isc.emplace_back(arg(q - icpc), q);\n\t}\n\tsort(ALL(isc));\n\tpair<R, P> pp(.0, P(0, 0));\n\tREP(i, isc.size()){\n\t\tconst auto cur = isc[i];\n\t\tconst auto nxt = isc[(i+1)%isc.size()];\n\t\tR nxtarg = i+1 == isc.size() ? nxt.first + 2*PI : nxt.first;\n\t\tpp = max(pp, mp(nxtarg - cur.first, cur.second));\n\t}\n\t\n\treturn pp.second;\n}\n\nR straight(P dir, R d = INF){\n\tREP(i, n){\n\t\tconst S s = g.edge(i);\n\t\tif(sig(arg(dir*P(0, 1)/s.dir())) || inp(proj(icpc, s) - icpc, dir) < EPS || s.online(proj(icpc, s)) != TRUE) continue;\n\t\td = min(d, abs(proj(icpc, s) - icpc) - icpc.r);\n\t}\n\tREP(i, n){\n\t\tconst P q = g[i];\n\t\tR r = abs(q - proj(q, L(icpc, icpc+dir)));\n\t\tif(r + EPS > icpc.r || inp(q - icpc, dir) < EPS) continue;\n\t\td = min(d, abs(proj(q, L(icpc, icpc+dir)) - icpc) - sqrt(icpc.r*icpc.r-r*r));\n\t}\n\treturn d;\n}\n\nR rot(P nav, R r){\n\tREP(i, n){\n\t\tconst S s = g.edge(i);\n\t\tif(abs(proj(nav, s) - nav) < EPS) continue;\n\t\tR t = arg((proj(nav, s) - nav)/(icpc - nav));\n\t\tif(t > 0 && t < PI) continue;\n\t\tR d = abs(proj(nav, s) - nav);\n\t\tif(d > 2*icpc.r - EPS) continue;\n\t\tR x = arg(s.dir()) - asin((d-icpc.r) / icpc.r) - arg(icpc-nav);\n\t\tif(d-icpc.r < 0) x = arg(s.dir()) - arg(icpc-nav) + asin((icpc.r-d) / icpc.r);\n\t\tif(x < -PI) x += 2*PI;\n\t\tif(x > PI) x -= 2*PI;\n\t\tif(x < -EPS) r = max(r, x);\n\t}\n\tREP(i, n){\n\t\tconst P q = g[i];\n\t\tR d = abs(q-nav);\n\t\tif(d > 2*icpc.r - EPS || d < EPS) continue;\n\t\tP med = (nav + q) * (R).5;\n\t\tR d2 = sqrt(max((R).0, icpc.r*icpc.r - d*d*(R).25));\n\t\tP p1 = med + unit(med - nav)*P(0, 1)*d2;\n\t\tP p2 = med + unit(med - nav)*P(0, -1)*d2;\n\n\t\tif(arg((p1 - nav)/(icpc-nav)) < -EPS) r = max(r, arg((p1 - nav)/(icpc-nav)));\n\t\tif(arg((p2 - nav)/(icpc-nav)) < -EPS) r = max(r, arg((p2 - nav)/(icpc-nav)));\n\t}\n\treturn r;\n}\nint main(){\n\tios::sync_with_stdio(false);\n\twhile(cin >> n >> icpc, n){\n\t\tg.resize(n);\n\t\tREP(i, n) cin >> g[i];\n\t\tR d = straight(P(1, 0));\n\t\ticpc += P(1, 0)*d;\n\t\tR ans = 0;\n\t\tG root;\n\t\tP prev = getNavigator();\n\t\tP start = icpc;\n\t\twhile(1){\n\t\t\tP nav = getNavigator();\n\t\t\tif(sig(abs(prev-nav))){\n\t\t\t\troot.push_back(prev);\n\t\t\t\troot.push_back(icpc);\n\t\t\t\tans += icpc.r * icpc.r * arg((nav-icpc)/(prev-icpc)) * .5;\n\t\t\t}\n\t\t\tif(!sig(abs(icpc-start)) && sig(root.area())) break;\n\t\t\troot.push_back(nav);\n\t\t\tprev = nav;\n\t\t\tauto t = getType(nav);\n\t\t\tif(t.second > 0){\n\t\t\t\tP picpc = icpc;\n\t\t\t\tprev += unit(t.first) * straight(t.first, t.second);\n\t\t\t\ticpc += unit(t.first) * straight(t.first, t.second);\n\t\t\t\tif(S(picpc, icpc).online(start) == TRUE && sig(root.area())) break;\n\t\t\t}else{\n\t\t\t\t(P &)icpc = nav + (icpc - nav)*polar((R)1., rot(nav, t.second));\n\t\t\t\tif(sig(abs(icpc-start)) && !sig(abs(start - nav) - icpc.r) && sig(root.area())) break;\n\t\t\t}\n\t\t}\n\t\tprintf(\"%.10f\\n\", (double)(ans + root.area()));\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1320, "score_of_the_acc": -0.0018, "final_rank": 2 }, { "submission_id": "aoj_2258_808729", "code_snippet": "#include <iostream>\n#include <cstdio>\n#include <vector>\n#include <algorithm>\n#include <complex>\n#include <cstring>\n#include <cstdlib>\n#include <string>\n#include <cmath>\n#include <cassert>\n#include <queue>\n#include <set>\n#include <map>\n#include <valarray>\n#include <bitset>\n#include <stack>\n#include <iomanip>\n#include <fstream>\nusing namespace std;\n\n#define REP(i,n) for(int i=0;i<(int)(n);++i)\n#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)\n#define ALL(c) (c).begin(), (c).end()\n#define chmax(a,b) (a<b?(a=b,1):0)\n#define chmin(a,b) (a>b?(a=b,1):0)\n#define valid(y,x,h,w) (0<=y&&y<h&&0<=x&&x<w)\nconst int INF = 1<<29;\n#define double long double\nconst double EPS = 1e-8;\nconst double PI = acos(-1);\ntypedef long long ll;\ntypedef pair<int,int> pii;\n\ntypedef complex<double> P;\nP pIN() { double x,y; cin >> x >> y; return P(x,y); }\nnamespace std {\n bool operator < (const P& a, const P& b) {\n // if (abs(a-b)<EPS) return 0;\n return real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b);\n }\n}\ndouble cross(const P& a, const P& b) {\n return imag(conj(a)*b);\n}\ndouble dot(const P& a, const P& b) {\n return real(conj(a)*b);\n}\nstruct C {\n P p; double r;\n C(const P &p, double r) : p(p), r(r) { }\n};\nstruct L : public vector<P> {\n L(const P &a, const P &b) {\n push_back(a); push_back(b);\n }\n L() { resize(2); }\n};\ntypedef vector<P> G;\nint ccw(P a, P b, P c) {\n b -= a; c -= a;\n if (cross(b, c) > EPS) return +1; // counter clockwise\n if (cross(b, c) < -EPS) return -1; // clockwise\n if (dot(b, c) < -EPS) return +2; // c--a--b on line\n if (norm(b) < norm(c)) return -2; // a--b--c on line\n return 0;\n}\n\nbool parallel(const L &l, const L &m) {\n return abs(cross(l[1]-l[0],m[1]-m[0])) < EPS;\n}\nbool intersectSP(const L &s, const P &p) {\n return abs(s[0]-p)+abs(s[1]-p)-abs(s[1]-s[0]) < EPS; // triangle inequality\n}\nbool intersectLL(const L &l, const L &m) {\n return abs(cross(l[1]-l[0], m[1]-m[0])) > EPS || // non-parallel\n abs(cross(l[1]-l[0], m[0]-l[0])) < EPS; // same line\n}\nbool intersectLS(const L &l, const L &s) {\n return cross(l[1]-l[0], s[0]-l[0])* // s[0] is left of l\n cross(l[1]-l[0], s[1]-l[0]) < EPS; // s[1] is right of l\n}\n\nbool intersectSS(const L &s, const L &t) {\n return ccw(s[0],s[1],t[0])*ccw(s[0],s[1],t[1]) <= 0 &&\n ccw(t[0],t[1],s[0])*ccw(t[0],t[1],s[1]) <= 0;\n}\n\nP crosspoint(const L &l, const L &m) {\n double A = cross(l[1] - l[0], m[1] - m[0]);\n double B = cross(l[1] - l[0], l[1] - m[0]);\n if (abs(A) < EPS && abs(B) < EPS) return m[0]; // same line\n if (abs(A) < EPS) assert(false); // !!!PRECONDITION NOT SATISFIED!!!\n return m[0] + B / A * (m[1] - m[0]);\n}\n\nP projection(const L &l, const P &p) {\n double t = dot(p-l[0], l[0]-l[1]) / norm(l[0]-l[1]);\n return l[0] + t*(l[0]-l[1]);\n}\nP reflection(const L &l, const P &p) {\n return p + P(2,0) * (projection(l, p) - p);\n}\ndouble distanceLP(const L &l, const P &p) {\n return abs(p - projection(l, p));\n}\ndouble distanceLL(const L &l, const L &m) {\n return intersectLL(l, m) ? 0 : distanceLP(l, m[0]);\n}\ndouble distanceLS(const L &l, const L &s) {\n if (intersectLS(l, s)) return 0;\n return min(distanceLP(l, s[0]), distanceLP(l, s[1]));\n}\ndouble distanceSP(const L &s, const P &p) {\n const P r = projection(s, p);\n if (intersectSP(s, r)) return abs(r - p);\n return min(abs(s[0] - p), abs(s[1] - p));\n}\nP nearestSP(const L &s, const P &p) {\n const P r = projection(s, p);\n if (intersectSP(s, r)) return r;\n double a1 = abs(s[0] - p);\n double a2 = abs(s[1] - p);\n if (a1 < a2) return s[0];\n else return s[1];\n}\ndouble distanceSS(const L &s, const L &t) {\n if (intersectSS(s, t)) return 0;\n return min(min(distanceSP(s, t[0]), distanceSP(s, t[1])),\n min(distanceSP(t, s[0]), distanceSP(t, s[1])));\n}\n\n// テ」ツδ凖」ツつッテ」ツδ暗」ツδォテ」ツ?ョテ・ツ崢榲ィツサツ「\nP rotate(const P &p, double ang) {\n return p * P(cos(ang), sin(ang));\n}\n// テ・ツ篠淌ァツつケテ・ツ堕ィテ」ツつ甘」ツ?ョテァツ崢エテァツキツ堙」ツ?ョテ・ツ崢榲ィツサツ「\nL rotate(const L &l, double ang) {\n return L(rotate(l[0], ang),rotate(l[1], ang));\n}\nbool EQ(double a, double b) {\n return abs(a-b) < EPS;\n}\n\nvector<P> circleCentersFromTangent(L l1, L l2, double r) {\n vector<P> res;\n if (parallel(l1,l2)) return res;\n P cp = crosspoint(l1,l2);\n l1[0]-=cp; l1[1]-=cp;\n l2[0]-=cp; l2[1]-=cp;\n double ang = arg(l1[1]-l1[0]);\n l2 = rotate(l2, -ang+PI/2);\n double A = (l2[1]-l2[0]).imag() / (l2[1]-l2[0]).real();\n double sq = sqrt(A*A+1);\n res.push_back(P(r, r*(sq+A)));\n res.push_back(P(-r, r*(sq-A)));\n res.push_back(P(r, r*(-sq+A)));\n res.push_back(P(-r, r*(-sq-A))); \n FOR(it, res) *it = rotate(*it,+ang-PI/2) + cp;\n return res;\n}\n\nvector<P> circleCentersFromPoints(const P &p1, const P &p2, double r) {\n vector<P> res;\n double len = abs(p1-p2);\n if (len < EPS) return res;\n if (len/2 >= r-EPS) return res;\n P v = p2-p1;\n v /= len;\n v *= sqrt(r*r-(len/2)*(len/2));\n P p = (p1+p2)*P(0.5,0);\n res.push_back(p+v*P(0,1));\n res.push_back(p+v*P(0,-1));\n return res;\n}\n\nvoid solve(vector<P> &v, double A, double B, double C, double x) {\n double D = B*B-4*A*C;\n if (D<0) return;\n double sqD = sqrt(max(D,(double)0.0));\n v.push_back(P(x, (-B+sqD)/(2*A)));\n v.push_back(P(x, (-B-sqD)/(2*A)));\n}\n\nvector<P> circleCentersFromTangentAndPoint(L l, P p, double r) {\n vector<P> res;\n P cp = l[0];\n l[1] -= cp; l[0] -= cp;\n p -= cp;\n double ang = arg(l[1]-l[0]);\n p = rotate(p, -ang+PI/2);\n double a = p.real();\n double b = p.imag();\n double A = 1;\n double B = -2*b;\n solve(res,A,B,b*b-2*r*a+a*a, r);\n solve(res,A,B,b*b+2*r*a+a*a, -r);\n FOR(it, res) *it = rotate(*it,+ang-PI/2) + cp;\n return res;\n}\n\nvector<P> circleCentersFromSegments(L s1, L s2, double r) {\n vector<P> tmp = circleCentersFromTangent(s1,s2,r);\n vector<P> res;\n FOR(it, tmp) {\n if (intersectSP(s1,projection(s1,*it)) &&\n intersectSP(s2,projection(s2,*it))) {\n res.push_back(*it);\n }\n }\n REP(i,2) {\n REP(j,2) {\n vector<P> tmp = circleCentersFromPoints(s1[i],s2[j],r);\n FOR(it, tmp) {\n res.push_back(*it);\n }\n }\n }\n REP(i,2) {\n vector<P> tmp1 = circleCentersFromTangentAndPoint(s1, s2[i], r);\n FOR(it, tmp1) {\n if (intersectSP(s1, projection(s1,*it))) {\n res.push_back(*it);\n }\n }\n vector<P> tmp2 = circleCentersFromTangentAndPoint(s2, s1[i], r);\n FOR(it, tmp2) {\n if (intersectSP(s2, projection(s2,*it))) {\n res.push_back(*it);\n }\n }\n }\n return res;\n}\n\nvector<P> endpointCircles(const L &l, double r) {\n P v = l[1]-l[0];\n v /= abs(v);\n v *= r;\n vector<P> res;\n res.push_back(l[0] + v * P(0,1));\n res.push_back(l[0] + v * P(0,-1));\n res.push_back(l[1] + v * P(0,1));\n res.push_back(l[1] + v * P(0,-1));\n return res;\n}\n\ndouble scale = 2;\n// P diff(-90, 30);\nP diff(300, 300);\nvoid outputLine(L l) {\n return;\n l[0] = l[0]*P(scale,0)+diff;\n l[1] = l[1]*P(scale,0)+diff;\n printf(\"line(%Lf,%Lf,%Lf,%Lf);\\n\",l[0].real(),l[0].imag(),l[1].real(),l[1].imag()); \n}\nvoid outputCircle(C c) {\n return;\n c.p = c.p*P(scale,0) + diff;\n c.r *= scale;\n printf(\"circle(%Lf,%Lf,%Lf);\\n\", c.p.real(), c.p.imag(), c.r);\n}\ninline L line(const G &g, int i) {\n return L(g[i], g[(i+1)%g.size()]);\n}\n\nbool isVisible(const P &p1, const P &p2, const G &g, double r) {\n L seg(p1,p2);\n REP(i,g.size()) {\n if (distanceSS(seg,line(g,i)) <= r-EPS) return 0;\n }\n return 1;\n}\n\ndouble angle(const P &a, const P &b) { // テ」ツδ凖」ツつッテ」ツδ暗」ツδォテッツスツ?」ツ?凝」ツつ嘉」ツ?ソテ」ツ?淌」ツδ凖」ツつッテ」ツδ暗」ツδォテッツスツづ」ツ?ョティツァツ津・ツコツヲテ」ツ?ョティツィツ暗ァツョツ夕0,2pi)\n double ret = arg(b)-arg(a);\n return (ret>=0) ? ret : ret + 2*PI;\n}\ndouble angle2(const P &a, const P &b) { // テ」ツδ凖」ツつッテ」ツδ暗」ツδォaテ」ツ?ィテ」ツδ凖」ツつッテ」ツδ暗」ツδォbテ」ツ?ョテゥツ鳴禿」ツ?ョティツァツ津・ツコツヲ\n return min(angle(a,b), angle(b,a));\n}\n\nbool collide(const P &cent, const P &p1, const P &p2, const L seg, double r) {\n vector<P> v(2);\n v[0] = seg[0], v[1] = seg[1];\n if (intersectLS(L(cent,p1),seg)) {\n P cp = crosspoint(L(cent,p1),seg);\n v.push_back(cp);\n }\n if (intersectLS(L(cent,p2),seg)) {\n v.push_back(crosspoint(L(cent,p2),seg));\n }\n sort(ALL(v));\n REP(i,v.size()-1) {\n if (abs(v[i]-v[i+1])<EPS) continue;\n P p = (v[i]+v[i+1])*P(0.5,0);\n if (angle(p1-cent, p-cent) <= angle(p1-cent, p2-cent)) {\n if (distanceSP(L(v[i],v[i+1]), cent) <= r) {\n // cout << \"NG\" << \" \" << distanceSP(L(v[i],v[i+1]), cent) << endl;\n // cout << p << \" \" << p1 << \" \" << angle(p1-cent, p-cent) << endl;\n // cout << v[i] << \" \" << v[i+1] << \" \" << cent << endl;\n return 1;\n }\n }\n }\n return 0;\n}\n\nbool canRound2(const P &cent, const P &p1, const P &p2, const G &g, double r) {\n REP(i,g.size()) {\n if (collide(cent,p1,p2,line(g,i),r)) return 0;\n }\n return 1;\n}\n\nbool canRound(const P &p1, const P &p2, const G &g, double r, const vector<P> &ps) {\n REP(i,g.size()) {\n if (EQ(abs(p1-g[i]), r) &&\n EQ(abs(p2-g[i]), r)) {\n \n P a = g[i]+(p1-g[i])*P(2,0);\n P b = g[i]+(p2-g[i])*P(2,0);\n if (canRound2(g[i],b,a,g,2*r)) swap(a,b);\n if (canRound2(g[i],a,b,g,2*r)) {\n FOR(it, ps) {\n if (abs(p1-*it)<EPS || abs(p2-*it)<EPS) continue;\n if (EQ(abs(*it-g[i]), r)) {\n if (angle(a-g[i],*it-g[i]) < angle(a-g[i],b-g[i])) return 0;\n }\n }\n return 1;\n }\n }\n }\n return 0;\n}\n\n// テァツつケ-テ・ツ、ツ堙ィツァツ津・ツスツ「テ・ツ個?・ツ青ォテゥツ鳴「テ、ツソツ?\nint contains(const G &g, const P &p) {\n bool in = 0;\n REP(i,g.size()) {\n P a = g[i] - p;\n P b = g[(i+1)%g.size()] - p;\n if (imag(a) > imag(b)) swap(a,b);\n if (imag(a) <= 0 && 0 < imag(b))\n if (cross(a,b) < 0) in = !in;\n if (abs(cross(a,b))< EPS && dot(a,b) <= EPS) return 0;\n }\n return in?1:-1;\n}\n\nbool check(const P &p, const G &g, double r) {\n if (contains(g,p) != 1) return 0;\n REP(i,g.size()) {\n if (distanceSP(line(g,i), p) <= r-EPS) return 0;\n }\n return 1;\n}\n\nG getCycle(const vector<vector<int> > &g, const vector<P> &ps, int a, int b) {\n vector<int> flag(g.size(), -1);\n G tmpres;\n int pre = a;\n int cur = b;\n tmpres.push_back(ps[a]);\n flag[a] = 0;\n int cnt = 1;\n while(1) {\n if (flag[cur] >= 0) {\n G res;\n for (int i=flag[cur]; i<tmpres.size(); ++i) {\n res.push_back(tmpres[i]);\n }\n return res;\n }\n flag[cur] = cnt++;\n tmpres.push_back(ps[cur]);\n P vec = ps[pre]-ps[cur];\n if (cnt == 2) {\n vec = ps[cur]-ps[pre];\n }\n double minang = INF;\n int dst = -1;\n FOR(it, g[cur]) {\n double tang = angle(vec, ps[*it]-ps[cur]);\n if (tang > EPS && chmin(minang, tang)) {\n dst = *it;\n } else if (EQ(minang,tang) && abs(ps[*it]-ps[cur])<abs(ps[dst]-ps[cur])) {\n dst = *it;\n }\n // cout << ps[cur] << \" \" << ps[*it] << endl;\n }\n // cout << dst << \" \" << minang << \" \" << ps[dst] << endl;\n if (dst == -1) return G();\n pre = cur;\n cur = dst;\n }\n}\ndouble area(const G& g) {\n double A = 0;\n for (int i = 0; i < g.size(); ++i) {\n A += cross(g[i], g[(i+1)%g.size()]);\n }\n return abs(A/2);\n}\n\nP O;\nint quadrant(const P &a) { // テヲツ卍づィツィツ暗・ツ崢榲」ツつ甘・ツ?エテ」ツ?ッテ・ツ青ォテ」ツ?ソテ」ツ??・ツ債甘ヲツ卍づィツィツ暗・ツ崢榲」ツつ甘・ツ?エテ」ツ?ッテ・ツ青ォテ」ツ?セテ」ツ?ェテ」ツ??\n if (a.real() > 0 && a.imag() >= 0) return 0;\n if (a.real() <= 0 && a.imag() > 0) return 1;\n if (a.real() < 0 && a.imag() <= 0) return 2;\n return 3;\n}\nbool cmpAngle(const P &a, const P&b) { // テ」ツδュテ」ツδ静」ツつケテ」ツδ暗」ツ?ェティツァツ津・ツコツヲテヲツッツ氾ィツシツ?\n int q1 = quadrant(a-O);\n int q2 = quadrant(b-O);\n if (q1 != q2) return q1 < q2;\n return cross(a-O,b-O)>0;\n}\n\n\nmap<pair<P,P>, bool> mp;\n\ndouble cornerArea(const P &p, const G &g, double r) {\n vector<P> ps;\n REP(i,g.size()) {\n L s = line(g,i);\n // P p1 = \n // // L s(g[i],g[(i+1)%g.size()]);\n //L s(P(0,0),P(100,100));\n if (EQ(distanceSP(s,p), r)) {\n P q = nearestSP(s,p);\n // P p(0,0);\n bool f = 1;\n REP(j,ps.size()) {\n if (abs(ps[j]-q) < EPS) f = 0;\n }\n if (f) {\n ps.push_back(q);\n }\n }\n }\n O = p;\n sort(ALL(ps), cmpAngle);\n if (ps.size() <= 1) return 0;\n int cur = 0;\n double res = 0;\n G tmp;\n // cout << p << endl;\n for (int i=0; ; ++i) {\n if (cur == ps.size()+1) {\n // cout << res << endl;\n return res;\n }\n int ii = i%g.size();\n L s = line(g,ii);\n tmp.push_back(g[ii]);\n P cp = ps[cur%ps.size()]; \n if (intersectSP(s,cp)) {\n if (cur && angle(ps[cur-1]-p,cp-p) <= PI) {\n tmp.push_back(cp);\n tmp.push_back(p);\n // cout << p << \" \" << ps[cur-1] << \" \" << cp << \" \" << cur << \" \" << ps[cur%ps.size()] << endl;\n // FOR(it, tmp) cout << *it << \" \"; cout << endl;\n // cout << tmp.size() << endl;\n // REP(k,tmp.size()){\n // outputLine(line(tmp,k));\n // }\n if (mp.count(make_pair(ps[cur-1],ps[cur%ps.size()])) == 0) {\n mp[make_pair(ps[cur-1],ps[cur&ps.size()])] = 1;\n res += area(tmp);\n res -= r*r*angle(ps[cur-1]-p,cp-p)/2;\n }\n }\n tmp.clear();\n tmp.push_back(cp);\n cur++;\n }\n }\n}\n\n\nint main() {\n outputLine(L(P(-1000,0),P(1000,0)));\n outputLine(L(P(-0,-1000),P(0,1000)));\n // L l1(P(100, 200), P(1000,500));\n // L l2(P(100, 300), P(700,300));\n // // L l1(P(0, 0), P(300,300));\n // // L l2(P(0, 0), P(700,300));\n \n // double r = 30;\n // vector<P> res = circleCentersFromTangent(l1,l2,r);\n // outputLine(l1);\n // outputLine(l2);\n // FOR(it, res) {\n // outputCircle(C(*it, r));\n // }\n int n;\n double x,y,r;\n while(cin>>n>>x>>y>>r, n) {\n mp.clear();\n G poly;\n REP(i,n) poly.push_back(pIN());\n\n // move right(?)\n double minDist = INF;\n P right;\n P start(x,y);\n REP(i,n) {\n L l(start-P(0,r), P(x+1000,y-r));\n vector<P> tmp = circleCentersFromSegments(l, line(poly,i), r);\n FOR(it, tmp) {\n if (check(*it, poly, r)) {\n double dis = abs(*it-start);\n if (chmin(minDist, dis)) {\n right = *it;\n }\n }\n }\n }\n \n vector<P> ps;\n ps.push_back(start);\n ps.push_back(right);\n \n REP(i,n) {\n vector<P> tmp = endpointCircles(line(poly,i),r);\n FOR(it, tmp) {\n if (check(*it, poly, r))\n ps.push_back(*it);\n }\n REP(j,i) {\n vector<P> ret = circleCentersFromSegments(line(poly,i),line(poly,j),r);\n FOR(it, ret) {\n if (check(*it, poly, r)) {\n ps.push_back(*it);\n }\n }\n }\n }\n REP(i,n) {\n outputLine(line(poly,i));\n }\n vector<P> tmpps;\n REP(i,ps.size()) {\n bool ok = 1;\n REP(j,tmpps.size()) {\n if (abs(tmpps[j]-ps[i])<EPS) {\n ok = 0;\n break;\n }\n }\n if (ok)\n tmpps.push_back(ps[i]); \n }\n ps = tmpps;\n // sort(ALL(ps));\n // ps.erase(unique(ALL(ps)), ps.end());\n\n int N = ps.size();\n\n int startID = -1;\n int rightID = -1;\n REP(i,N) {\n // cout << ps[i] << endl;\n if (EQ(abs(start-ps[i]),0)) startID = i;\n if (EQ(abs(right-ps[i]),0)) rightID = i;\n }\n \n vector<vector<int> > g(N);\n REP(i,N) {\n if (i != startID && i != rightID)\n outputCircle(C(ps[i],r));\n REP(j,i) {\n if (isVisible(ps[i], ps[j], poly, r) || canRound(ps[i], ps[j], poly, r, ps)) { \n g[i].push_back(j);\n g[j].push_back(i);\n // outputLine(L(ps[i],ps[j]));\n }\n }\n }\n\n \n G cycle = getCycle(g, ps, startID, rightID);\n double ans = area(poly);\n REP(i,cycle.size()) {\n outputLine(line(cycle,i));\n L l1 = line(cycle,i);\n L l2 = line(cycle,(i+1)%cycle.size());\n // if (ccw(l1[0],l1[1],l2[1]) == 1) {\n ans -= cornerArea(l1[1],poly,r);\n // }\n }\n printf(\"%.10Lf\\n\", ans);\n }\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 1332, "score_of_the_acc": -1.0071, "final_rank": 5 }, { "submission_id": "aoj_2258_808720", "code_snippet": "#include <iostream>\n#include <cstdio>\n#include <vector>\n#include <algorithm>\n#include <complex>\n#include <cstring>\n#include <cstdlib>\n#include <string>\n#include <cmath>\n#include <cassert>\n#include <queue>\n#include <set>\n#include <map>\n#include <valarray>\n#include <bitset>\n#include <stack>\n#include <iomanip>\n#include <fstream>\nusing namespace std;\n\n#define REP(i,n) for(int i=0;i<(int)(n);++i)\n#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)\n#define ALL(c) (c).begin(), (c).end()\n#define chmax(a,b) (a<b?(a=b,1):0)\n#define chmin(a,b) (a>b?(a=b,1):0)\n#define valid(y,x,h,w) (0<=y&&y<h&&0<=x&&x<w)\nconst int INF = 1<<29;\n#define double long double\nconst double EPS = 1e-8;\nconst double PI = acos(-1);\ntypedef long long ll;\ntypedef pair<int,int> pii;\n\ntypedef complex<double> P;\nP pIN() { double x,y; cin >> x >> y; return P(x,y); }\nnamespace std {\n bool operator < (const P& a, const P& b) {\n // if (abs(a-b)<EPS) return 0;\n return real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b);\n }\n}\ndouble cross(const P& a, const P& b) {\n return imag(conj(a)*b);\n}\ndouble dot(const P& a, const P& b) {\n return real(conj(a)*b);\n}\nstruct C {\n P p; double r;\n C(const P &p, double r) : p(p), r(r) { }\n};\nstruct L : public vector<P> {\n L(const P &a, const P &b) {\n push_back(a); push_back(b);\n }\n L() { resize(2); }\n};\ntypedef vector<P> G;\nint ccw(P a, P b, P c) {\n b -= a; c -= a;\n if (cross(b, c) > EPS) return +1; // counter clockwise\n if (cross(b, c) < -EPS) return -1; // clockwise\n if (dot(b, c) < -EPS) return +2; // c--a--b on line\n if (norm(b) < norm(c)) return -2; // a--b--c on line\n return 0;\n}\n\nbool parallel(const L &l, const L &m) {\n return abs(cross(l[1]-l[0],m[1]-m[0])) < EPS;\n}\nbool intersectSP(const L &s, const P &p) {\n return abs(s[0]-p)+abs(s[1]-p)-abs(s[1]-s[0]) < EPS; // triangle inequality\n}\nbool intersectLL(const L &l, const L &m) {\n return abs(cross(l[1]-l[0], m[1]-m[0])) > EPS || // non-parallel\n abs(cross(l[1]-l[0], m[0]-l[0])) < EPS; // same line\n}\nbool intersectLS(const L &l, const L &s) {\n return cross(l[1]-l[0], s[0]-l[0])* // s[0] is left of l\n cross(l[1]-l[0], s[1]-l[0]) < EPS; // s[1] is right of l\n}\n\nbool intersectSS(const L &s, const L &t) {\n return ccw(s[0],s[1],t[0])*ccw(s[0],s[1],t[1]) <= 0 &&\n ccw(t[0],t[1],s[0])*ccw(t[0],t[1],s[1]) <= 0;\n}\n\nP crosspoint(const L &l, const L &m) {\n double A = cross(l[1] - l[0], m[1] - m[0]);\n double B = cross(l[1] - l[0], l[1] - m[0]);\n if (abs(A) < EPS && abs(B) < EPS) return m[0]; // same line\n if (abs(A) < EPS) assert(false); // !!!PRECONDITION NOT SATISFIED!!!\n return m[0] + B / A * (m[1] - m[0]);\n}\n\nP projection(const L &l, const P &p) {\n double t = dot(p-l[0], l[0]-l[1]) / norm(l[0]-l[1]);\n return l[0] + t*(l[0]-l[1]);\n}\nP reflection(const L &l, const P &p) {\n return p + P(2,0) * (projection(l, p) - p);\n}\ndouble distanceLP(const L &l, const P &p) {\n return abs(p - projection(l, p));\n}\ndouble distanceLL(const L &l, const L &m) {\n return intersectLL(l, m) ? 0 : distanceLP(l, m[0]);\n}\ndouble distanceLS(const L &l, const L &s) {\n if (intersectLS(l, s)) return 0;\n return min(distanceLP(l, s[0]), distanceLP(l, s[1]));\n}\ndouble distanceSP(const L &s, const P &p) {\n const P r = projection(s, p);\n if (intersectSP(s, r)) return abs(r - p);\n return min(abs(s[0] - p), abs(s[1] - p));\n}\nP nearestSP(const L &s, const P &p) {\n const P r = projection(s, p);\n if (intersectSP(s, r)) return r;\n double a1 = abs(s[0] - p);\n double a2 = abs(s[1] - p);\n if (a1 < a2) return s[0];\n else return s[1];\n}\ndouble distanceSS(const L &s, const L &t) {\n if (intersectSS(s, t)) return 0;\n return min(min(distanceSP(s, t[0]), distanceSP(s, t[1])),\n min(distanceSP(t, s[0]), distanceSP(t, s[1])));\n}\n\n// テ」ツδ凖」ツつッテ」ツδ暗」ツδォテ」ツ?ョテ・ツ崢榲ィツサツ「\nP rotate(const P &p, double ang) {\n return p * P(cos(ang), sin(ang));\n}\n// テ・ツ篠淌ァツつケテ・ツ堕ィテ」ツつ甘」ツ?ョテァツ崢エテァツキツ堙」ツ?ョテ・ツ崢榲ィツサツ「\nL rotate(const L &l, double ang) {\n return L(rotate(l[0], ang),rotate(l[1], ang));\n}\nbool EQ(double a, double b) {\n return abs(a-b) < EPS;\n}\n\nvector<P> circleCentersFromTangent(L l1, L l2, double r) {\n vector<P> res;\n if (parallel(l1,l2)) return res;\n P cp = crosspoint(l1,l2);\n l1[0]-=cp; l1[1]-=cp;\n l2[0]-=cp; l2[1]-=cp;\n double ang = arg(l1[1]-l1[0]);\n l2 = rotate(l2, -ang+PI/2);\n double A = (l2[1]-l2[0]).imag() / (l2[1]-l2[0]).real();\n double sq = sqrt(A*A+1);\n res.push_back(P(r, r*(sq+A)));\n res.push_back(P(-r, r*(sq-A)));\n res.push_back(P(r, r*(-sq+A)));\n res.push_back(P(-r, r*(-sq-A))); \n FOR(it, res) *it = rotate(*it,+ang-PI/2) + cp;\n return res;\n}\n\nvector<P> circleCentersFromPoints(const P &p1, const P &p2, double r) {\n vector<P> res;\n double len = abs(p1-p2);\n if (len < EPS) return res;\n if (len/2 >= r-EPS) return res;\n P v = p2-p1;\n v /= len;\n v *= sqrt(r*r-(len/2)*(len/2));\n P p = (p1+p2)*P(0.5,0);\n res.push_back(p+v*P(0,1));\n res.push_back(p+v*P(0,-1));\n return res;\n}\n\nvoid solve(vector<P> &v, double A, double B, double C, double x) {\n double D = B*B-4*A*C;\n if (D<0) return;\n double sqD = sqrt(max(D,(double)0.0));\n v.push_back(P(x, (-B+sqD)/(2*A)));\n v.push_back(P(x, (-B-sqD)/(2*A)));\n}\n\nvector<P> circleCentersFromTangentAndPoint(L l, P p, double r) {\n vector<P> res;\n P cp = l[0];\n l[1] -= cp; l[0] -= cp;\n p -= cp;\n double ang = arg(l[1]-l[0]);\n p = rotate(p, -ang+PI/2);\n double a = p.real();\n double b = p.imag();\n double A = 1;\n double B = -2*b;\n solve(res,A,B,b*b-2*r*a+a*a, r);\n solve(res,A,B,b*b+2*r*a+a*a, -r);\n FOR(it, res) *it = rotate(*it,+ang-PI/2) + cp;\n return res;\n}\n\nvector<P> circleCentersFromSegments(L s1, L s2, double r) {\n vector<P> tmp = circleCentersFromTangent(s1,s2,r);\n vector<P> res;\n FOR(it, tmp) {\n if (intersectSP(s1,projection(s1,*it)) &&\n intersectSP(s2,projection(s2,*it))) {\n res.push_back(*it);\n }\n }\n REP(i,2) {\n REP(j,2) {\n vector<P> tmp = circleCentersFromPoints(s1[i],s2[j],r);\n FOR(it, tmp) {\n res.push_back(*it);\n }\n }\n }\n REP(i,2) {\n vector<P> tmp1 = circleCentersFromTangentAndPoint(s1, s2[i], r);\n FOR(it, tmp1) {\n if (intersectSP(s1, projection(s1,*it))) {\n res.push_back(*it);\n }\n }\n vector<P> tmp2 = circleCentersFromTangentAndPoint(s2, s1[i], r);\n FOR(it, tmp2) {\n if (intersectSP(s2, projection(s2,*it))) {\n res.push_back(*it);\n }\n }\n }\n return res;\n}\n\nvector<P> endpointCircles(const L &l, double r) {\n P v = l[1]-l[0];\n v /= abs(v);\n v *= r;\n vector<P> res;\n res.push_back(l[0] + v * P(0,1));\n res.push_back(l[0] + v * P(0,-1));\n res.push_back(l[1] + v * P(0,1));\n res.push_back(l[1] + v * P(0,-1));\n return res;\n}\n\ndouble scale = 10;\nvoid outputLine(L l) {\n return;\n l[0] = l[0]*P(scale,0)+P(150,350);\n l[1] = l[1]*P(scale,0)+P(150,350);\n printf(\"line(%Lf,%Lf,%Lf,%Lf);\\n\",l[0].real(),l[0].imag(),l[1].real(),l[1].imag()); \n}\nvoid outputCircle(C c) {\n return;\n c.p = c.p*P(scale,0) + P(150,350);\n c.r *= scale;\n printf(\"circle(%Lf,%Lf,%Lf);\\n\", c.p.real(), c.p.imag(), c.r);\n}\ninline L line(const G &g, int i) {\n return L(g[i], g[(i+1)%g.size()]);\n}\n\nbool isVisible(const P &p1, const P &p2, const G &g, double r) {\n L seg(p1,p2);\n REP(i,g.size()) {\n if (distanceSS(seg,line(g,i)) <= r-EPS) return 0;\n }\n return 1;\n}\n\ndouble angle(const P &a, const P &b) { // テ」ツδ凖」ツつッテ」ツδ暗」ツδォテッツスツ?」ツ?凝」ツつ嘉」ツ?ソテ」ツ?淌」ツδ凖」ツつッテ」ツδ暗」ツδォテッツスツづ」ツ?ョティツァツ津・ツコツヲテ」ツ?ョティツィツ暗ァツョツ夕0,2pi)\n double ret = arg(b)-arg(a);\n return (ret>=0) ? ret : ret + 2*PI;\n}\ndouble angle2(const P &a, const P &b) { // テ」ツδ凖」ツつッテ」ツδ暗」ツδォaテ」ツ?ィテ」ツδ凖」ツつッテ」ツδ暗」ツδォbテ」ツ?ョテゥツ鳴禿」ツ?ョティツァツ津・ツコツヲ\n return min(angle(a,b), angle(b,a));\n}\n\nbool collide(const P &cent, const P &p1, const P &p2, const L seg, double r) {\n vector<P> v(2);\n v[0] = seg[0], v[1] = seg[1];\n if (intersectLS(L(cent,p1),seg)) {\n P cp = crosspoint(L(cent,p1),seg);\n v.push_back(cp);\n }\n if (intersectLS(L(cent,p2),seg)) {\n v.push_back(crosspoint(L(cent,p2),seg));\n }\n sort(ALL(v));\n REP(i,v.size()-1) {\n if (abs(v[i]-v[i+1])<EPS) continue;\n P p = (v[i]+v[i+1])*P(0.5,0);\n if (angle(p1-cent, p-cent) <= angle(p1-cent, p2-cent)) {\n if (distanceSP(L(v[i],v[i+1]), cent) <= r) {\n // cout << \"NG\" << \" \" << distanceSP(L(v[i],v[i+1]), cent) << endl;\n // cout << p << \" \" << p1 << \" \" << angle(p1-cent, p-cent) << endl;\n // cout << v[i] << \" \" << v[i+1] << \" \" << cent << endl;\n return 1;\n }\n }\n }\n return 0;\n}\n\nbool canRound2(const P &cent, const P &p1, const P &p2, const G &g, double r) {\n REP(i,g.size()) {\n if (collide(cent,p1,p2,line(g,i),r)) return 0;\n }\n return 1;\n}\n\nbool canRound(const P &p1, const P &p2, const G &g, double r, const vector<P> &ps) {\n REP(i,g.size()) {\n if (EQ(abs(p1-g[i]), r) &&\n EQ(abs(p2-g[i]), r)) {\n \n P a = g[i]+(p1-g[i])*P(2,0);\n P b = g[i]+(p2-g[i])*P(2,0);\n if (canRound2(g[i],b,a,g,2*r)) swap(a,b);\n if (canRound2(g[i],a,b,g,2*r)) {\n FOR(it, ps) {\n if (abs(p1-*it)<EPS || abs(p2-*it)<EPS) continue;\n if (EQ(abs(*it-g[i]), r)) {\n if (angle(a-g[i],*it-g[i]) < angle(a-g[i],b-g[i])) return 0;\n }\n }\n return 1;\n }\n }\n }\n return 0;\n}\n\n// テァツつケ-テ・ツ、ツ堙ィツァツ津・ツスツ「テ・ツ個?・ツ青ォテゥツ鳴「テ、ツソツ?\nint contains(const G &g, const P &p) {\n bool in = 0;\n REP(i,g.size()) {\n P a = g[i] - p;\n P b = g[(i+1)%g.size()] - p;\n if (imag(a) > imag(b)) swap(a,b);\n if (imag(a) <= 0 && 0 < imag(b))\n if (cross(a,b) < 0) in = !in;\n if (abs(cross(a,b))< EPS && dot(a,b) <= EPS) return 0;\n }\n return in?1:-1;\n}\n\nbool check(const P &p, const G &g, double r) {\n if (contains(g,p) != 1) return 0;\n REP(i,g.size()) {\n if (distanceSP(line(g,i), p) <= r-EPS) return 0;\n }\n return 1;\n}\n\nG getCycle(const vector<vector<int> > &g, const vector<P> &ps, int a, int b) {\n vector<int> flag(g.size(), -1);\n G tmpres;\n int pre = a;\n int cur = b;\n tmpres.push_back(ps[a]);\n flag[a] = 0;\n int cnt = 1;\n while(1) {\n if (flag[cur] >= 0) {\n G res;\n for (int i=flag[cur]; i<tmpres.size(); ++i) {\n res.push_back(tmpres[i]);\n }\n return res;\n }\n flag[cur] = cnt++;\n tmpres.push_back(ps[cur]);\n P vec = ps[pre]-ps[cur];\n if (cnt == 2) {\n vec = ps[cur]-ps[pre];\n }\n double minang = INF;\n int dst = -1;\n FOR(it, g[cur]) {\n double tang = angle(vec, ps[*it]-ps[cur]);\n if (tang > EPS && chmin(minang, tang)) {\n dst = *it;\n } else if (EQ(minang,tang) && abs(ps[*it]-ps[cur])<abs(ps[dst]-ps[cur])) {\n dst = *it;\n }\n // cout << ps[cur] << \" \" << ps[*it] << endl;\n }\n // cout << dst << \" \" << minang << \" \" << ps[dst] << endl;\n if (dst == -1) return G();\n pre = cur;\n cur = dst;\n }\n}\ndouble area(const G& g) {\n double A = 0;\n for (int i = 0; i < g.size(); ++i) {\n A += cross(g[i], g[(i+1)%g.size()]);\n }\n return abs(A/2);\n}\n\nP O;\nint quadrant(const P &a) { // テヲツ卍づィツィツ暗・ツ崢榲」ツつ甘・ツ?エテ」ツ?ッテ・ツ青ォテ」ツ?ソテ」ツ??・ツ債甘ヲツ卍づィツィツ暗・ツ崢榲」ツつ甘・ツ?エテ」ツ?ッテ・ツ青ォテ」ツ?セテ」ツ?ェテ」ツ??\n if (a.real() > 0 && a.imag() >= 0) return 0;\n if (a.real() <= 0 && a.imag() > 0) return 1;\n if (a.real() < 0 && a.imag() <= 0) return 2;\n return 3;\n}\nbool cmpAngle(const P &a, const P&b) { // テ」ツδュテ」ツδ静」ツつケテ」ツδ暗」ツ?ェティツァツ津・ツコツヲテヲツッツ氾ィツシツ?\n int q1 = quadrant(a-O);\n int q2 = quadrant(b-O);\n if (q1 != q2) return q1 < q2;\n return cross(a-O,b-O)>0;\n}\n\n\nmap<pair<P,P>, bool> mp;\n\ndouble cornerArea(const P &p, const G &g, double r) {\n vector<P> ps;\n REP(i,g.size()) {\n L s = line(g,i);\n // P p1 = \n // // L s(g[i],g[(i+1)%g.size()]);\n //L s(P(0,0),P(100,100));\n if (EQ(distanceSP(s,p), r)) {\n P q = nearestSP(s,p);\n // P p(0,0);\n bool f = 1;\n REP(j,ps.size()) {\n if (abs(ps[j]-q) < EPS) f = 0;\n }\n if (f) {\n ps.push_back(q);\n }\n }\n }\n O = p;\n sort(ALL(ps), cmpAngle);\n if (ps.size() <= 1) return 0;\n int cur = 0;\n double res = 0;\n G tmp;\n // cout << p << endl;\n for (int i=0; ; ++i) {\n if (cur == ps.size()+1) {\n // cout << res << endl;\n return res;\n }\n int ii = i%g.size();\n L s = line(g,ii);\n tmp.push_back(g[ii]);\n P cp = ps[cur%ps.size()]; \n if (intersectSP(s,cp)) {\n if (cur && angle(ps[cur-1]-p,cp-p) <= PI) {\n tmp.push_back(cp);\n tmp.push_back(p);\n // cout << p << \" \" << ps[cur-1] << \" \" << cp << \" \" << cur << \" \" << ps[cur%ps.size()] << endl;\n // FOR(it, tmp) cout << *it << \" \"; cout << endl;\n // cout << tmp.size() << endl;\n // REP(k,tmp.size()){\n // outputLine(line(tmp,k));\n // }\n if (mp.count(make_pair(ps[cur-1],ps[cur%ps.size()])) == 0) {\n mp[make_pair(ps[cur-1],ps[cur&ps.size()])] = 1;\n res += area(tmp);\n res -= r*r*angle(ps[cur-1]-p,cp-p)/2;\n }\n }\n tmp.clear();\n tmp.push_back(cp);\n cur++;\n }\n }\n}\n\n\nint main() {\n outputLine(L(P(-1000,0),P(1000,0)));\n outputLine(L(P(-0,-1000),P(0,1000)));\n // L l1(P(100, 200), P(1000,500));\n // L l2(P(100, 300), P(700,300));\n // // L l1(P(0, 0), P(300,300));\n // // L l2(P(0, 0), P(700,300));\n \n // double r = 30;\n // vector<P> res = circleCentersFromTangent(l1,l2,r);\n // outputLine(l1);\n // outputLine(l2);\n // FOR(it, res) {\n // outputCircle(C(*it, r));\n // }\n int n;\n double x,y,r;\n while(cin>>n>>x>>y>>r, n) {\n mp.clear();\n G poly;\n REP(i,n) poly.push_back(pIN());\n\n // move right(?)\n double minDist = INF;\n P right;\n P start(x,y);\n REP(i,n) {\n L l(start-P(0,r), P(x+1000,y-r));\n vector<P> tmp = circleCentersFromSegments(l, line(poly,i), r);\n FOR(it, tmp) {\n if (check(*it, poly, r)) {\n double dis = abs(*it-start);\n if (chmin(minDist, dis)) {\n right = *it;\n }\n }\n }\n }\n \n vector<P> ps;\n ps.push_back(start);\n ps.push_back(right);\n \n REP(i,n) {\n vector<P> tmp = endpointCircles(line(poly,i),r);\n FOR(it, tmp) {\n if (check(*it, poly, r))\n ps.push_back(*it);\n }\n REP(j,i) {\n vector<P> ret = circleCentersFromSegments(line(poly,i),line(poly,j),r);\n FOR(it, ret) {\n if (check(*it, poly, r)) {\n ps.push_back(*it);\n }\n }\n }\n }\n REP(i,n) {\n outputLine(line(poly,i));\n }\n vector<P> tmpps;\n REP(i,ps.size()) {\n bool ok = 1;\n REP(j,tmpps.size()) {\n if (abs(tmpps[j]-ps[i])<EPS) {\n ok = 0;\n break;\n }\n }\n if (ok)\n tmpps.push_back(ps[i]); \n }\n ps = tmpps;\n // sort(ALL(ps));\n // ps.erase(unique(ALL(ps)), ps.end());\n\n int N = ps.size();\n\n int startID = -1;\n int rightID = -1;\n REP(i,N) {\n // cout << ps[i] << endl;\n if (EQ(abs(start-ps[i]),0)) startID = i;\n if (EQ(abs(right-ps[i]),0)) rightID = i;\n }\n \n vector<vector<int> > g(N);\n REP(i,N) {\n if (i != startID && i != rightID)\n outputCircle(C(ps[i],r));\n REP(j,i) {\n if (isVisible(ps[i], ps[j], poly, r) || canRound(ps[i], ps[j], poly, r, ps)) { \n g[i].push_back(j);\n g[j].push_back(i);\n // outputLine(L(ps[i],ps[j]));\n }\n }\n }\n\n \n G cycle = getCycle(g, ps, startID, rightID);\n double ans = area(poly);\n REP(i,cycle.size()) {\n outputLine(line(cycle,i));\n L l1 = line(cycle,i);\n L l2 = line(cycle,(i+1)%cycle.size());\n if (ccw(l1[0],l1[1],l2[1]) == 1) {\n ans -= cornerArea(l1[1],poly,r);\n }\n }\n printf(\"%.10Lf\\n\", ans);\n }\n}", "accuracy": 0.5, "time_ms": 60, "memory_kb": 1332, "score_of_the_acc": -1.0071, "final_rank": 7 } ]
aoj_2257_cpp
桜詩 願はくは花の下にて春死なむ English text is not available in this practice contest. Nathan O. Davis は集積回路コースの学生である. Nathan は足りない単位を補充するために,日本文化に関する授業を履修している.今日の課題は詩の作成である. Nathan は勉強不足で日本語が不得手なため,プログラムに詩を自動生成させようと考えた.手始めに彼は,日本語の単語の接続辞書を入手した. あとは単語の接続にしたがってランダムな文章を生成するだけである. しかしながら,ランダムに生成された全ての文字列が詩として認められるわけではない. 詩には,いくつかの季語のうち1つが,一度だけ現れていなければならない.ある季語が2回以上出現することや,2種類以上の季語が1回ずつ出現することは許されない. また,季語は単語の接続の境界をまたいで出現してもよい. あなたの仕事は,入力で与えられた単語の接続辞書と季語のリストから,指定された長さの詩が何通りに作られるかを求めるプログラムを作成することである. 違う単語を繋げて詩となる同じ文字列が得られた場合,それらは重複して数え上げるものとする.答えは非常に大きくなりうるので, 1,000,000,007 で割った余りを出力せよ. Input 入力は複数のテストケースを含んでいる.1つのテストケースは,以下の形式で与えられる. N M K from 1 to 1 from 2 to 2 : from N to N seasonword 1 seasonword 2 : seasonword K 入力の最初の行には3つの整数 N (1 ≤ N ≤ 250), M (1 ≤ M ≤ 500), K (1 ≤ K ≤ 30) が含まれ,それぞれ単語の接続辞書の大きさ,作成すべき詩の長さ,季語の数を表す. 続く N 行は単語の接続辞書の情報を表す. 各行は2つの文字列 from i , to i を含み,単語 from i の後に続けて単語 to i が出現してもよいということを表す. to i の後に続けて from i が出現してもよいということを表すものではないことに注意せよ.また, from i で終わるような他の文字列の後に続けて to i が出現してもよいということを表すものでもない.詩は,接続辞書に含まれるどの単語から始めてもよい. 続く K 行は1つの文字列 seasonword i からなり,それぞれ季語を表す. 入力中に現れる文字列は全て小文字のアルファベットからなり,その長さは 1 以上 20 以下である. 接続辞書の各項目,および季語は互いに異なる. すなわち, i ≠ j に対して from i ≠ from j または to i ≠ to j が成り立つ. 同様に, i ≠ j に対して seasonword i ≠ seasonword j が成り立つ. 入力の末尾には,入力の終了を表す 3 つの 0 がある. Output 生成される異なる詩の数を 1,000,000,007 で割った余りを1行に出力せよ. 先に言及したように,違う単語を繋げて詩となる同じ文字列が得られた場合,それらは重複して数え上げるものとする. 厳密に言えば,2つの詩 s , t があり,それぞれ単語の列 [ a 1 , a 2 , ..., a n ], [ b 1 , b 2 , ..., b m ] を順に連結して得られたものであるとき, n = m かつすべての 1 ≤ i ≤ n に対して a i = b i であるとき,またそのときに限って,2つの詩 s , t は同一の詩とみなされる. Sample Input 4 64 2 negawakuha hananoshitanite hananoshitanite harushinan harushinan sonokisaragino sonokisaragino mochizukinokoro sakura hana 2 15 2 naha naha naha gachoon sakura hana 3 7 2 asakur a a sakura asa kura sakura hana 9 100 2 a a a h a n h a h h h n n a n h n n sakura hana 4 2 2 a a a b b a b b ab b 4 7 4 i cpc mi cp ac mi cp c ac wa tle re 0 0 0 Output for the Sample Input 1 1 3 715991824 1 1
[ { "submission_id": "aoj_2257_10867672", "code_snippet": "#include <bits/stdc++.h>\n\n#define REP(i,n) for(int i=0;i<(int)(n);i++)\n#define ALL(x) (x).begin(),(x).end()\n\nusing namespace std;\n\nusing ll = long long;\nusing ld = long double;\n\ntemplate <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }\ntemplate <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }\n\nstruct yes_no : numpunct<char> {\n string_type do_truename() const { return \"Yes\"; }\n string_type do_falsename() const { return \"No\"; }\n};\n\ntemplate<int M, bool IsPrime = false>\nclass Modulo {\n using ll = long long;\n int n;\n static enable_if_t<IsPrime, ll> inv(ll a, ll p) {\n return (a == 1 ? 1 : (1 - p * inv(p%a, a)) / a + p);\n }\npublic:\n Modulo () : n(0) {;}\n Modulo (int m) : n(m) {\n if (n >= M) n %= M;\n else if (n < 0) n = (n % M + M) % M;\n }\n Modulo (ll m) {\n if (m >= M) m %= M;\n else if (m < 0) m = (m % M + M) % M;\n n = m;\n }\n explicit operator int() const { return n; }\n explicit operator ll() const { return n; }\n bool operator==(const Modulo &a) const { return n == a.n; }\n Modulo operator+=(const Modulo &a) { n += a.n; if (n >= M) n -= M; return *this; }\n Modulo operator-=(const Modulo &a) { n -= a.n; if (n < 0) n += M; return *this; }\n Modulo operator*=(const Modulo &a) { n = (ll(n) * a.n) % M; return *this; }\n Modulo operator+(const Modulo &a) const { Modulo res = *this; return res += a; }\n Modulo operator-(const Modulo &a) const { Modulo res = *this; return res -= a; }\n Modulo operator*(const Modulo &a) const { Modulo res = *this; return res *= a; }\n};\n\nconst int mod = 1000000007;\n\nusing Mod = Modulo<mod, true>;\n\n\nstruct AhoCorasick {\n static const int SIZE = 128;\n struct State {\n int index, next[SIZE];\n vector<int> accept;\n State(int index) : index(index) { memset(next, -1, sizeof(next)); }\n };\n\n vector<State> pma;\n vector<int> lens;\n\n AhoCorasick(const vector<string> &str) {\n pma.clear();\n pma.push_back(State(0));\n lens.clear();\n\n REP(i,str.size()) {\n int t = 0;\n for (char c : str[i]) {\n if (pma[t].next[(int)c] == -1) {\n int m = pma.size();\n pma[t].next[(int)c] = m;\n pma.push_back(State(m));\n }\n t = pma[t].next[(int)c];\n }\n pma[t].accept.push_back(lens.size());\n lens.push_back(str[i].size());\n }\n\n queue<int> que;\n for (int c = 1; c < SIZE; c++) {\n if (pma[0].next[c] != -1) {\n pma[pma[0].next[c]].next[0] = 0;\n que.push(pma[0].next[c]);\n }\n else {\n pma[0].next[c] = 0;\n }\n }\n while (!que.empty()) {\n int t = que.front();\n que.pop();\n for (int c = 1; c < SIZE; c++) {\n if (pma[t].next[c] != -1) {\n que.push(pma[t].next[c]);\n int r = pma[t].next[0];\n while (pma[r].next[c] == -1) r = pma[r].next[0];\n pma[pma[t].next[c]].next[0] = pma[r].next[c];\n for (int i : pma[pma[r].next[c]].accept)\n pma[pma[t].next[c]].accept.push_back(i);\n }\n }\n }\n }\n\n int sub(int index, int c) {\n return pma[index].next[c] != -1 ?\n pma[index].next[c] :\n pma[index].next[c] = sub(pma[index].next[0], c);\n }\n\n pair<int,int> query(string &t, int from) {\n int index = from;\n int match = 0;\n REP(i,t.size()) {\n int c = t[i];\n index = sub(index, c);\n match += pma[index].accept.size();\n }\n return make_pair(match, index);\n }\n \n};\n\nmap<string,int> dict_memo;\nvector<string> dict_str;\n\nint dict(const string &s) {\n if (dict_memo.count(s)) return dict_memo[s];\n int res = dict_str.size();\n dict_str.push_back(s);\n return dict_memo[s] = res;\n}\n\nconst int BLOCK = 21;\n\nvector<int> g[512];\nMod dp[BLOCK][512][610][2];\nint is_match[512][610];\nint next_node[512][610];\n\nint main() {\n locale loc(locale(), new yes_no);\n cout << boolalpha;\n cout.imbue(loc);\n int N, M, K;\n while (cin >> N >> M >> K, N) {\n REP(i,512) g[i].clear();\n dict_str.clear();\n dict_memo.clear();\n vector<int> from(N), to(N);\n vector<string> season(K);\n REP(i,N) {\n string s, t;\n cin >> s >> t;\n from[i] = dict(s);\n to[i] = dict(t);\n g[from[i]].push_back(to[i]);\n }\n const int n = dict_str.size();\n REP(i,n) g[n].push_back(i);\n REP(i,K) cin >> season[i];\n AhoCorasick aho(season);\n REP(i,dict_str.size()) REP(j,aho.pma.size()) {\n tie(is_match[i][j], next_node[i][j]) = aho.query(dict_str[i], j);\n }\n REP(i,BLOCK) REP(j,512) REP(k,610) REP(l,2) dp[i][j][k][l] = 0;\n dp[0][n][0][0] = 1;\n // M = 2;\n REP(i,M) {\n REP(from,n+1) REP(k,aho.pma.size()) {\n // cout << i << \" \" << from << \" \" << k << \" \" << 0 << \" \" << int(dp[i%BLOCK][from][k][0]) << endl;\n // cout << i << \" \" << from << \" \" << k << \" \" << 1 << \" \" << int(dp[i%BLOCK][from][k][1]) << endl;\n for (int to: g[from]) {\n int len = dict_str[to].size();\n if (is_match[to][k] >= 2) continue;\n if (is_match[to][k]) {\n dp[(i+len)%BLOCK][to][next_node[to][k]][1] += dp[i%BLOCK][from][k][0];\n }\n else {\n dp[(i+len)%BLOCK][to][next_node[to][k]][0] += dp[i%BLOCK][from][k][0];\n dp[(i+len)%BLOCK][to][next_node[to][k]][1] += dp[i%BLOCK][from][k][1];\n }\n }\n }\n REP(j,n+1) REP(k,aho.pma.size()) REP(l,2) dp[i%BLOCK][j][k][l] = 0;\n }\n Mod res = 0;\n REP(j,n+1) REP(k,aho.pma.size()) {\n res += dp[M%BLOCK][j][k][1];\n }\n cout << int(res) << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1870, "memory_kb": 57600, "score_of_the_acc": -0.4808, "final_rank": 4 }, { "submission_id": "aoj_2257_10240714", "code_snippet": "// AOJ #2257 Sakura Poetry\n// 2025.2.23\n\n#include <bits/stdc++.h>\nusing namespace std;\n\nconst int MOD = 1000000007;\n\nstruct ACNode {\n int next[26];\n int fail;\n int out;\n ACNode() : fail(0), out(0) {\n memset(next, -1, sizeof(next));\n }\n};\n\nclass ACAutomaton {\npublic:\n vector<ACNode> nodes;\n ACAutomaton() {\n nodes.push_back(ACNode());\n }\n void insert(const string &s) {\n int cur = 0;\n for(char c: s){\n int idx = c - 'a';\n if(nodes[cur].next[idx] == -1){\n nodes[cur].next[idx] = nodes.size();\n nodes.push_back(ACNode());\n }\n cur = nodes[cur].next[idx];\n }\n nodes[cur].out++;\n }\n void build() {\n queue<int> q;\n nodes[0].fail = 0;\n for(int c=0; c<26; c++){\n int nxt = nodes[0].next[c];\n if(nxt != -1){\n nodes[nxt].fail = 0;\n q.push(nxt);\n } else nodes[0].next[c] = 0;\n }\n while(!q.empty()){\n int cur = q.front();\n q.pop();\n int f = nodes[cur].fail;\n nodes[cur].out += nodes[f].out;\n for(int c=0; c<26; c++){\n int nxt = nodes[cur].next[c];\n if(nxt != -1){\n nodes[nxt].fail = nodes[f].next[c];\n q.push(nxt);\n } else nodes[cur].next[c] = nodes[f].next[c];\n }\n }\n }\n};\n\nstruct Word { string s; };\n\nACAutomaton ac;\n\nstruct SimResult {\n int next_state;\n int found;\n bool valid;\n};\n\nstatic vector<vector<vector<SimResult>>> memoSim;\n\nSimResult simulateWord(const Word &word, int initState, int initFound) {\n int state = initState;\n int found = initFound;\n for (char c : word.s) {\n int idx = c - 'a';\n state = ac.nodes[state].next[idx];\n if(ac.nodes[state].out > 0) {\n if(found == 0) {\n if(ac.nodes[state].out == 1) found = 1;\n else return {0,0,false};\n } else return {0,0,false};\n }\n }\n return {state, found, true};\n}\n\nvoid precomputeSim(const vector<Word> &vocab) {\n int V = vocab.size();\n int A = ac.nodes.size();\n memoSim.assign(V, vector<vector<SimResult>>(2, vector<SimResult>(A)));\n for (int w = 0; w < V; w++) {\n for (int f = 0; f < 2; f++) {\n for (int st = 0; st < A; st++) {\n memoSim[w][f][st] = simulateWord(vocab[w], st, f);\n }\n }\n }\n}\n\nstruct Edge { int from, to; };\n\nstruct State { int v, acState, found; };\n\nint encodeState(const State &s, int A) {\n return ( (s.v * A) + s.acState ) * 2 + s.found;\n}\n\n\nint main(){\n ios::sync_with_stdio(0);\n cin.tie(0);\n\n while(true){\n int N, M, K;\n cin >> N >> M >> K;\n if(N==0) break;\n\n vector<Edge> edges;\n set<string> wordSet;\n vector<pair<string, string>> rawEdges;\n for (int i=0; i<N; i++){\n string f, t;\n cin >> f >> t;\n rawEdges.push_back({f, t});\n wordSet.insert(f);\n wordSet.insert(t);\n }\n vector<Word> vocab;\n unordered_map<string,int> wordId;\n for(auto &w : wordSet){\n int id = vocab.size();\n vocab.push_back({w});\n wordId[w] = id;\n }\n\n vector<vector<int>> adj(vocab.size());\n for(auto &p: rawEdges){\n int u = wordId[p.first], v = wordId[p.second];\n adj[u].push_back(v);\n }\n\n vector<string> seasonWords(K);\n ac.nodes.clear();\n ac.nodes.push_back(ACNode());\n for (int i=0; i<K; i++){\n cin >> seasonWords[i];\n ac.insert(seasonWords[i]);\n }\n ac.build();\n\n precomputeSim(vocab);\n\n int A = ac.nodes.size();\n int V = vocab.size();\n\n vector<unordered_map<int, int>> dp(M+1);\n\n for (int v=0; v<V; v++){\n int l = vocab[v].s.size();\n if(l > M) continue;\n SimResult res = memoSim[v][0][0];\n if(!res.valid) continue;\n State st = {v, res.next_state, res.found};\n int code = encodeState(st, A);\n dp[l][code] = (dp[l][code] + 1) % MOD;\n }\n\n for (int len = 0; len <= M; len++){\n if(dp[len].empty()) continue;\n for(auto &entry : dp[len]){\n int ways = entry.second;\n int code = entry.first;\n int found = code % 2; code /= 2;\n int acState = code % A; code /= A;\n int v = code;\n State curState = {v, acState, found};\n\n for (int nv : adj[curState.v]) {\n int addLen = vocab[nv].s.size();\n if(len + addLen > M) continue;\n SimResult res = memoSim[nv][curState.found][curState.acState];\n if(!res.valid) continue;\n State nxt = {nv, res.next_state, res.found};\n int code2 = encodeState(nxt, A);\n dp[len + addLen][code2] = (dp[len + addLen][code2] + ways) % MOD;\n }\n }\n }\n\n long long ans = 0;\n for(auto &entry : dp[M]){\n int code = entry.first;\n int found = code & 1;\n if(found == 1) {\n ans = (ans + entry.second) % MOD;\n }\n }\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1200, "memory_kb": 150664, "score_of_the_acc": -0.8553, "final_rank": 6 }, { "submission_id": "aoj_2257_9374338", "code_snippet": "#line 2 \"cp-library/src/cp-template.hpp\"\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nusing i32 = int;\nusing u32 = unsigned int;\nusing i64 = long long;\nusing u64 = unsigned long long;\nusing i128 = __int128_t;\ntemplate < class T > bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }\ntemplate < class T > bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }\ntemplate < class T, class U > T ceil (T x, U y) { return (x > 0 ? (x + y - 1) / y : x / y); }\ntemplate < class T, class U > T floor(T x, U y) { return (x > 0 ? x / y : (x - y + 1) / y); }\nint popcnt(i32 x) { return __builtin_popcount(x); }\nint popcnt(u32 x) { return __builtin_popcount(x); }\nint popcnt(i64 x) { return __builtin_popcountll(x); }\nint popcnt(u64 x) { return __builtin_popcountll(x); }\n\n#line 2 \"cp-library/src/utility/rep_itr.hpp\"\ntemplate < class T > struct itr_rep {\n T i, d;\n constexpr itr_rep(const T i) noexcept : i(i), d(1) {}\n constexpr itr_rep(const T i, const T d) noexcept : i(i), d(d) {}\n void operator++() noexcept { i += d; }\n constexpr int operator*() const noexcept { return i; }\n constexpr bool operator!=(const itr_rep x) const noexcept { return d > 0 ? i < x.i : i > x.i; }\n};\n\ntemplate < class T > struct rep {\n const itr_rep< T > s, t;\n constexpr rep(const T t) noexcept : s(0), t(t) {}\n constexpr rep(const T s, const T t) noexcept : s(s), t(t) {}\n constexpr rep(const T s, const T t, const T d) noexcept : s(s, d), t(t, d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n\ntemplate < class T > struct revrep {\n const itr_rep < T > s, t;\n constexpr revrep(const T t) noexcept : s(t - 1, -1), t(-1, -1) {}\n constexpr revrep(const T s, const T t) noexcept : s(t - 1, -1), t(s - 1, -1) {}\n constexpr revrep(const T s, const T t, const T d) noexcept : s(t - 1, -d), t(s - 1, -d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n#line 3 \"cp-library/src/utility/io.hpp\"\n\n/* 128bit integer */\nistream& operator>>(istream& is, i128& x) {\n std::string s; is >> s;\n int pm = (s[0] == '-');\n x = 0;\n for(int i : rep(pm, int(s.size()))) x = x * 10 + (s[i] - '0');\n if(pm) x *= -1;\n return is;\n}\nostream& operator<<(ostream& os, const i128& x) {\n if(x == 0) return os << '0';\n i128 y = x;\n if(y < 0) { os << '-'; y *= -1; }\n std::vector<int> ny;\n while(y > 0) { ny.push_back(y % 10); y /= 10; }\n for(int i : revrep(ny.size())) os << ny[i];\n return os;\n}\n\ntemplate < class S, class T > istream& operator>>(istream& is, std::pair< S, T >& x) { is >> x.first >> x.second; return is; }\ntemplate < class S, class T > ostream& operator<<(ostream& os, const std::pair< S, T >& x) { os << x.first << \" \" << x.second; return os; }\n\nnamespace scanner {\n struct sca {\n template < class T > operator T() {\n T s; std::cin >> s; return s;\n }\n };\n struct vec {\n int n;\n vec(int n) : n(n) {}\n template < class T > operator std::vector< T >() {\n std::vector< T > v(n);\n for(T& x : v) std::cin >> x;\n return v;\n }\n };\n struct mat {\n int h, w;\n mat(int h, int w) : h(h), w(w) {}\n template < class T > operator std::vector< std::vector< T > >() {\n std::vector m(h, std::vector< T >(w));\n for(std::vector< T >& v : m) for(T& x : v) std::cin >> x;\n return m;\n }\n };\n struct speedup {\n speedup() {\n std::cin.tie(0);\n std::ios::sync_with_stdio(0);\n }\n } speedup_instance;\n}\nscanner::sca in() { return scanner::sca(); }\nscanner::vec in(int n) { return scanner::vec(n); }\nscanner::mat in(int h, int w) { return scanner::mat(h, w); }\n\nnamespace printer {\n void precision(int d) { std::cout << std::fixed << std::setprecision(d); }\n void flush() { std::cout.flush(); }\n}\n\ntemplate < class T >\nostream& operator<<(ostream& os, const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) { os << a[i]; if(i != n - 1) os << ' '; }\n return os;\n}\n\nint print() { std::cout << '\\n'; return 0; }\ntemplate < class head, class... tail > int print(head&& h, tail&&... t) {\n std::cout << h; if(sizeof...(tail)) std::cout << ' ';\n return print(std::forward<tail>(t)...);\n}\ntemplate < class T > int print_n(const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) std::cout << a[i] << \"\\n\";\n return 0;\n}\n\n\n#line 2 \"cp-library/src/utility/key_val.hpp\"\n\ntemplate < class K, class V >\nstruct key_val {\n K key; V val;\n key_val() {}\n key_val(K key, V val) : key(key), val(val) {}\n template < std::size_t Index >\n std::tuple_element_t< Index, key_val >& get() {\n if constexpr (Index == 0) return key;\n if constexpr (Index == 1) return val;\n }\n};\n\nnamespace std {\n\ntemplate < class K, class V > struct tuple_size < key_val< K, V > > : integral_constant< size_t, 2 > {};\ntemplate < class K, class V > struct tuple_element < 0, key_val< K, V > > { using type = K; };\ntemplate < class K, class V > struct tuple_element < 1, key_val< K, V > > { using type = V; };\n\n}\n#line 2 \"cp-library/src/utility/vec_op.hpp\"\ntemplate < class T > key_val< int, T > max_of(const vector< T >& a) {\n int i = std::max_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class T > key_val< int, T > min_of(const vector< T >& a) {\n int i = std::min_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class S, class T > S sum_of(const vector< T >& a) {\n S sum = 0;\n for(const T x : a) sum += x;\n return sum;\n}\ntemplate < class S, class T > vector< S > freq_of(const vector< T >& a, T L, T R) {\n vector< S > res(R - L, S(0));\n for(const T x : a) res[x - L] += 1;\n return res;\n}\ntemplate < class S, class T > struct prefix_sum {\n vector< S > s;\n prefix_sum(const vector< T >& a) : s(a) {\n s.insert(s.begin(), S(0));\n for(int i : rep(a.size())) s[i + 1] += s[i];\n }\n // [L, R)\n S sum(int L, int R) { return s[R] - s[L]; }\n};\n#line 3 \"cp-library/src/utility/heap.hpp\"\n\ntemplate < class T > using heap_min = std::priority_queue< T, std::vector< T >, std::greater< T > >;\ntemplate < class T > using heap_max = std::priority_queue< T, std::vector< T >, std::less< T > >;\n\n#line 27 \"cp-library/src/cp-template.hpp\"\n\n#line 1 \"cp-library/src/algorithm/bin_search.hpp\"\ntemplate < class T, class F >\nT bin_search(T ok, T ng, F f) {\n while(abs(ng - ok) > 1) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n\ntemplate < class T, class F >\nT bin_search_real(T ok, T ng, F f, int step = 80) {\n while(step--) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n#line 2 \"cp-library/src/algorithm/argsort.hpp\"\n\ntemplate < class T > std::vector< int > argsort(const std::vector< T > &a) {\n std::vector< int > ids((int)a.size());\n std::iota(ids.begin(), ids.end(), 0);\n std::sort(ids.begin(), ids.end(), [&](int i, int j) {\n return a[i] < a[j] || (a[i] == a[j] && i < j);\n });\n return ids;\n}\n#line 1 \"macro.hpp\"\nnamespace macro {\n\nusing size_type = int;\ntemplate < class container > void sort(container& a) { std::sort(std:: begin(a), std:: end(a)); }\ntemplate < class container > void rsort(container& a) { std::sort(std::rbegin(a), std::rend(a)); }\ntemplate < class container > void reverse(container& a) { std::reverse(std::begin(a), std::end(a)); }\ntemplate < class container > void unique(container& a) {\n std::sort(std::begin(a), std::end(a));\n a.erase(std::unique(std::begin(a), std::end(a)), std::end(a));\n}\ntemplate < class container > container sorted(const container& a) { container b = a; sort(b); return std::move(b); }\ntemplate < class container > container rsorted(const container& a) { container b = a; rsort(b); return std::move(b); }\ntemplate < class container, class compare > void sort(container& a, const compare& cmp) { std::sort(std::begin(a), std::end(a), cmp); }\ntemplate < class container, class compare > container sorted(const container& a, const compare& cmp) { container b = a; sort(b, cmp); return std::move(b); }\ntemplate < class container, class value > size_type lower_bound(const container& a, const value& x) { return std::lower_bound(std::begin(a), std::end(a), x) - std::begin(a); }\ntemplate < class container, class value > size_type upper_bound(const container& a, const value& x) { return std::upper_bound(std::begin(a), std::end(a), x) - std::begin(a); }\n\nconst std::vector<std::pair<size_type, size_type>> dir4 = { {+1, 0}, {-1, 0}, { 0, +1}, { 0, -1} };\nconst std::vector<std::pair<size_type, size_type>> dir8 = { {-1, -1}, {-1, 0}, {-1, +1}, { 0, -1}, { 0, +1}, {+1, -1}, {+1, 0}, {+1, +1} };\n\n#ifdef _DEBUG\n#define debug(x) std::cout << \"[\" << __LINE__ << \"] \" << #x << \": \" << x << std::endl\n#else\n#define debug(x)\n#endif\n\ntemplate < class container > void concat(container& a, const container& b) {\n a.insert(std::end(a), std::begin(b), std::end(b));\n}\nstd::vector<size_type> iota(const size_type n) {\n std::vector<size_type> I(n);\n std::iota(std::begin(I), std::end(I), 0);\n return I;\n}\ntemplate < class container > std::vector<size_type> sort_idx(const container& a) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return a[i] < a[j] or (a[i] == a[j] and i < j); });\n return I;\n}\ntemplate < class container, class compare > std::vector<size_type> sort_idx(const container& a, const compare& cmp) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return cmp(a[i], a[j]) or (a[i] == a[j] and i < j); });\n return std::move(I);\n}\n\nstruct grid {\n using size_type = int;\n size_type H, W;\n grid(const size_type H, const size_type W) : H(H), W(W) {}\n bool contains(const size_type i, const size_type j) {\n return 0 <= i and i < H and 0 <= j and j < W;\n }\n};\n\nusing f64 = long double;\n\ntemplate < class T > vector< T >& operator++(vector< T >& a) { for(T& x : a) x++; return a; }\ntemplate < class T > vector< T >& operator--(vector< T >& a) { for(T& x : a) x--; return a; }\ntemplate < class T > vector< T > operator++(vector< T >& a, signed) { vector< T > res = a; for(T& x : a) x++; return res; }\ntemplate < class T > vector< T > operator--(vector< T >& a, signed) { vector< T > res = a; for(T& x : a) x--; return res; }\n\n} // namespace macro\n\nusing namespace macro;\n#line 2 \"cp-library/src/string/aho_corasick.hpp\"\n\nstruct aho_corasick {\n static constexpr int C_SIZE = 26;\n static constexpr int C_BEGIN = 'a';\n static constexpr int ROOT = 0;\n struct node {\n array<int, C_SIZE> to = {};\n vector<int> ids; // このノードでマッチする文字列のIDリスト\n int fail = ROOT; // 失敗時の遷移先\n int drct = ROOT; // suffixでマッチする遷移先\n node() { to.fill(-1); }\n };\n vector<node> nodes;\n vector<string> patterns;\n aho_corasick() : nodes(1) {}\n\n int insert(const string& s) {\n int v = ROOT;\n for(char c : s) {\n const int k = c - C_BEGIN;\n if(nodes[v].to[k] == -1) {\n nodes[v].to[k] = nodes.size();\n nodes.push_back(node());\n }\n v = nodes[v].to[k];\n }\n nodes[v].ids.push_back(patterns.size());\n patterns.push_back(s);\n return v;\n }\n\n int next(int v, char c) {\n const int k = c - C_BEGIN;\n while(nodes[v].to[k] == -1 and v != ROOT) v = nodes[v].fail;\n return nodes[v].to[k] != -1 ? nodes[v].to[k] : ROOT;\n }\n\n void build() {\n std::queue<int> q;\n for(int v : nodes[ROOT].to) if(v != -1) q.push(v);\n while(not q.empty()) {\n int v = q.front(); q.pop();\n int fail = nodes[v].fail;\n for(int k = 0; k < C_SIZE; k++) {\n int nv = nodes[v].to[k];\n if(nv != -1) {\n q.push(nv);\n nodes[nv].fail = next(fail, k + C_BEGIN);\n }\n }\n nodes[v].drct = (nodes[fail].ids.empty() ? nodes[fail].drct : fail);\n }\n }\n\n // v で完成している (受理される) \n bool accept(int v) {\n return nodes[v].drct != ROOT or not nodes[v].ids.empty();\n }\n vector<int> match(int v) {\n vector<int> res;\n while(v != ROOT) {\n for(int id : nodes[v].ids) res.push_back(id);\n v = nodes[v].drct;\n }\n return res;\n }\n vector<vector<int>> search(const string& s) {\n vector<vector<int>> res;\n int now = ROOT;\n for(char c : s) {\n now = next(now, c);\n res.emplace_back(match(now));\n }\n return res;\n }\n};\n#line 2 \"cp-library/src/number/modint.hpp\"\nstruct modinfo { uint mod, root, isprime; };\ntemplate < modinfo const &ref >\nstruct modint {\n static constexpr uint const &mod = ref.mod;\n static constexpr uint const &root = ref.root;\n static constexpr uint const &isprime = ref.isprime;\n uint v = 0;\n constexpr modint& s(uint v) { this->v = v < mod ? v : v - mod; return *this; }\n constexpr modint(ll v = 0) { s(v % mod + mod); }\n modint operator-() const { return modint() - *this; }\n modint& operator+=(const modint& rhs) { return s(v + rhs.v); }\n modint& operator-=(const modint& rhs) { return s(v + mod - rhs.v); }\n modint& operator*=(const modint& rhs) { v = ull(v) * rhs.v % mod; return *this; }\n modint& operator/=(const modint& rhs) { return *this *= inv(rhs); }\n modint operator+(const modint& rhs) const { return modint(*this) += rhs; }\n modint operator-(const modint& rhs) const { return modint(*this) -= rhs; }\n modint operator*(const modint& rhs) const { return modint(*this) *= rhs; }\n modint operator/(const modint& rhs) const { return modint(*this) /= rhs; }\n friend modint pow(modint x, ll n) { modint res(1); while(n > 0) { if(n & 1) res *= x; x *= x; n >>= 1; } return res; }\n friend modint inv(modint v) {\n if(isprime) {\n return pow(v, mod - 2);\n } else {\n ll a = v.v, b = modint::mod, x = 1, y = 0, t;\n while(b > 0) { t = a / b; swap(a -= t * b, b); swap(x -= t * y, y); }\n return modint(x);\n }\n }\n friend modint operator+(int x, const modint& y) { return modint(x) + y; }\n friend modint operator-(int x, const modint& y) { return modint(x) - y; }\n friend modint operator*(int x, const modint& y) { return modint(x) * y; }\n friend modint operator/(int x, const modint& y) { return modint(x) / y; }\n friend istream& operator>>(istream& is, modint& m) { ll x; is >> x; m = modint(x); return is; }\n friend ostream& operator<<(ostream& os, const modint& m) { return os << m.v; }\n bool operator==(const modint& r) const { return v == r.v; }\n bool operator!=(const modint& r) const { return v != r.v; }\n static uint get_mod() { return mod; }\n static int is_prime() { return isprime; }\n};\nconstexpr modinfo base998244353 { 998244353, 3, 1 };\nconstexpr modinfo base1000000007 { 1000000007, 0, 1 };\nusing mint998244353 = modint< base998244353 >;\nusing mint1000000007 = modint< base1000000007 >;\n#line 5 \"A.cpp\"\nusing mint = mint1000000007;\n\nmint solve(int N, int M, int K) {\n int word_cnt = 0;\n vector<string> word;\n map<string, int> w_id;\n vector<vector<int>> g;\n {\n word.push_back(\"\");\n w_id[\"\"] = word_cnt++;\n g.push_back({});\n }\n for(int _ : rep(N)) {\n string from_s = in(), to_s = in();\n if(!w_id.count(from_s)) word.push_back(from_s), w_id[from_s] = word_cnt++, g.push_back({});\n if(!w_id.count(to_s)) word.push_back(to_s), w_id[to_s] = word_cnt++, g.push_back({});\n const int from = w_id[from_s], to = w_id[to_s];\n g[from].push_back(to);\n }\n for(int wi : rep(1, word_cnt)) g[0].push_back(wi);\n\n vector<string> sw = in(K);\n aho_corasick ac;\n for(string& s : sw) ac.insert(s);\n ac.build();\n const int node_size = ac.nodes.size();\n\n vector dp(M + 1, vector(word_cnt, vector(2, map<int, mint>{})));\n dp[0][0][0][ac.ROOT] = 1;\n\n for(int m : rep(M)) {\n for(int from : rep(word_cnt)) for(int to : g[from]) {\n const string& s = word[to];\n if(not(m + s.size() <= M)) continue;\n for(int sw_cnt : {0, 1}) if(not dp[m][from][sw_cnt].empty()) {\n for(int ni : rep(node_size)) if(dp[m][from][sw_cnt].count(ni)) {\n int n_sw_cnt = sw_cnt;\n int n_ni = ni;\n bool ok = [&] {\n for(char c : s) {\n n_ni = ac.next(n_ni, c);\n if(ac.accept(n_ni)) {\n n_sw_cnt += ac.match(n_ni).size();\n if(n_sw_cnt > 1) return false;\n }\n }\n return true;\n }();\n if(ok) {\n dp[m + s.size()][to][n_sw_cnt][n_ni] += dp[m][from][sw_cnt][ni];\n }\n }\n }\n }\n }\n\n mint ans = 0;\n for(int wi : rep(word_cnt)) {\n for(int ni : rep(node_size)) {\n ans += dp[M][wi][1][ni];\n }\n }\n return ans;\n}\n\nint main() {\n while(true) {\n int N = in(), M = in(), K = in();\n if(make_tuple(N, M, K) == make_tuple(0, 0, 0)) return 0;\n print(solve(N, M, K));\n }\n}", "accuracy": 1, "time_ms": 6380, "memory_kb": 153504, "score_of_the_acc": -1.6512, "final_rank": 18 }, { "submission_id": "aoj_2257_9295141", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)\n#define revrep(i, t, s) for (int i = (int)(t) - 1; i >= (int)(s); --i)\n#define all(x) begin(x), end(x)\ntemplate <typename T>\nbool chmax(T& a, const T& b) {\n return a < b ? (a = b, 1) : 0;\n}\ntemplate <typename T>\nbool chmin(T& a, const T& b) {\n return a > b ? (a = b, 1) : 0;\n}\n\ntemplate <int m>\nclass Modint {\n using mint = Modint;\n static_assert(m > 0, \"Modulus must be positive\");\n\n public:\n static constexpr int mod() { return m; }\n\n constexpr Modint(long long y = 0) : x(y >= 0 ? y % m : (y % m + m) % m) {}\n\n constexpr int val() const { return x; }\n\n constexpr mint& operator+=(const mint& r) {\n if ((x += r.x) >= m) x -= m;\n return *this;\n }\n constexpr mint& operator-=(const mint& r) {\n if ((x += m - r.x) >= m) x -= m;\n return *this;\n }\n constexpr mint& operator*=(const mint& r) {\n x = static_cast<int>(1LL * x * r.x % m);\n return *this;\n }\n constexpr mint& operator/=(const mint& r) { return *this *= r.inv(); }\n\n constexpr bool operator==(const mint& r) const { return x == r.x; }\n\n constexpr mint operator+() const { return *this; }\n constexpr mint operator-() const { return mint(-x); }\n\n constexpr friend mint operator+(const mint& l, const mint& r) {\n return mint(l) += r;\n }\n constexpr friend mint operator-(const mint& l, const mint& r) {\n return mint(l) -= r;\n }\n constexpr friend mint operator*(const mint& l, const mint& r) {\n return mint(l) *= r;\n }\n constexpr friend mint operator/(const mint& l, const mint& r) {\n return mint(l) /= r;\n }\n\n constexpr mint inv() const {\n int a = x, b = m, u = 1, v = 0;\n while (b > 0) {\n int t = a / b;\n std::swap(a -= t * b, b);\n std::swap(u -= t * v, v);\n }\n return mint(u);\n }\n\n constexpr mint pow(long long n) const {\n mint ret(1), mul(x);\n while (n > 0) {\n if (n & 1) ret *= mul;\n mul *= mul;\n n >>= 1;\n }\n return ret;\n }\n\n friend std::ostream& operator<<(std::ostream& os, const mint& r) {\n return os << r.x;\n }\n\n friend std::istream& operator>>(std::istream& is, mint& r) {\n long long t;\n is >> t;\n r = mint(t);\n return is;\n }\n\n private:\n int x;\n};\n\nclass AhoCorasick {\n public:\n struct Node {\n std::map<char, int> ch;\n std::vector<int> accept;\n int link = -1;\n int cnt = 0;\n\n Node() = default;\n };\n\n std::vector<Node> states;\n std::map<int, int> accept_state;\n\n explicit AhoCorasick() : states(1) {}\n\n int size() const { return states.size(); }\n\n void insert(const std::string& s, int id = -1) {\n int i = 0;\n for (char c : s) {\n if (!states[i].ch.count(c)) {\n states[i].ch[c] = states.size();\n states.emplace_back();\n }\n i = states[i].ch[c];\n }\n ++states[i].cnt;\n states[i].accept.push_back(id);\n accept_state[id] = i;\n }\n\n void clear() {\n states.clear();\n states.emplace_back();\n }\n\n int get_next(int i, char c) const {\n while (i != -1 && !states[i].ch.count(c)) i = states[i].link;\n return i != -1 ? states[i].ch.at(c) : 0;\n }\n\n void build() {\n std::queue<int> que;\n que.push(0);\n while (!que.empty()) {\n int i = que.front();\n que.pop();\n\n for (auto [c, j] : states[i].ch) {\n states[j].link = get_next(states[i].link, c);\n states[j].cnt += states[states[j].link].cnt;\n\n auto& a = states[j].accept;\n auto& b = states[states[j].link].accept;\n std::vector<int> accept;\n // std::ranges::set_union(a, b, std::back_inserter(accept));\n std::set_union(a.begin(), a.end(), b.begin(), b.end(),\n std::back_inserter(accept));\n a = accept;\n\n que.push(j);\n }\n }\n }\n\n long long count(const std::string& str) const {\n long long ret = 0;\n int i = 0;\n for (auto c : str) {\n i = get_next(i, c);\n ret += states[i].cnt;\n }\n return ret;\n }\n\n long long count(int i) const { return states[i].cnt; }\n\n // list of (id, index)\n std::vector<std::pair<int, int>> match(const std::string& str) const {\n std::vector<std::pair<int, int>> ret;\n int i = 0;\n for (int k = 0; k < (int)str.size(); ++k) {\n char c = str[k];\n i = get_next(i, c);\n for (auto id : states[i].accept) {\n ret.emplace_back(id, k);\n }\n }\n return ret;\n }\n};\n\nusing mint = Modint<(int)1e9 + 7>;\n\nmap<pair<int, int>, mint> dp[501][2];\n\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << fixed << setprecision(15);\n\n while (true) {\n int N, M, K;\n cin >> N >> M >> K;\n if (N == 0) break;\n map<string, int> idx;\n vector<string> strs;\n vector<vector<int>> G;\n rep(_, 0, N) {\n string s, t;\n cin >> s >> t;\n if (s.size() > M || t.size() > M) continue;\n if (idx.count(s) == 0) {\n idx[s] = strs.size();\n strs.push_back(s);\n G.emplace_back();\n }\n if (idx.count(t) == 0) {\n idx[t] = strs.size();\n strs.push_back(t);\n G.emplace_back();\n }\n G[idx[s]].push_back(idx[t]);\n }\n AhoCorasick aho;\n rep(i, 0, K) {\n string s;\n cin >> s;\n aho.insert(s);\n }\n aho.build();\n rep(len, 0, M + 1) rep(a, 0, 2) dp[len][a].clear();\n rep(i, 0, strs.size()) {\n int k = 0, a = 0;\n for (char c : strs[i]) {\n k = aho.get_next(k, c);\n a += aho.count(k);\n }\n if (a <= 1) {\n dp[strs[i].size()][a][{i, k}] = 1;\n }\n }\n rep(len, 0, M + 1) rep(a, 0, 2) for (auto& p : dp[len][a]) {\n auto [i, k] = p.first;\n for (int j : G[i]) {\n int nlen = len + strs[j].size();\n if (nlen > M) continue;\n int nk = k, na = a;\n for (char c : strs[j]) {\n nk = aho.get_next(nk, c);\n na += aho.count(nk);\n }\n if (na <= 1) {\n dp[nlen][na][{j, nk}] += p.second;\n }\n }\n }\n mint ans = 0;\n for (auto& p : dp[M][1]) ans += p.second;\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 3810, "memory_kb": 201060, "score_of_the_acc": -1.5065, "final_rank": 17 }, { "submission_id": "aoj_2257_9260673", "code_snippet": "#include <bits/stdc++.h>\n\nvoid debug(std::string S = \"Hello\") {\n std::cout << S << std::endl;\n}\n\nstruct Comp {\n std::vector<std::string> S;\n Comp() = default;\n void push_back(const std::string& s) {\n S.push_back(s);\n }\n void build() {\n std::sort(S.begin(), S.end());\n S.erase(std::unique(S.begin(), S.end()), S.end());\n }\n int size() const {\n return (int)S.size();\n }\n const std::string& inv(int i) const {\n assert(i < size());\n return S[i];\n }\n int at(const std::string& s) const {\n auto it{std::lower_bound(S.begin(), S.end(), s)};\n assert(it != S.end());\n assert(*it == s);\n return (int)std::distance(S.begin(), it);\n }\n};\n\nstruct Node {\n std::array<int, 26> ch;\n int par{-1}, label{-1}, suf{-1}, cnt{};\n Node() {\n ch.fill(-1);\n }\n Node(int p, int l) : ch{}, par{p}, label{l} {\n ch.fill(-1);\n }\n};\n\nstruct Trie {\n std::vector<Node> dat; \n Trie() = default;\n void insert(const std::string& s) {\n int v{};\n for (int i{} ; i < (int)s.size() ; i++) {\n int label{s[i] - 'a'};\n if (dat[v].ch[label] == -1) {\n int x{(int)dat.size()};\n dat.emplace_back(v, label);\n dat[v].ch[label] = x;\n }\n v = dat[v].ch[label];\n }\n dat[v].cnt = 1;\n }\n void makeSuffixLink() {\n dat[0].suf = 0;\n std::queue<int> que;\n for (int i{} ; i < 26 ; i++) {\n if (dat[0].ch[i] != -1) {\n que.emplace(dat[0].ch[i]);\n }\n }\n while (que.size()) {\n auto v{que.front()};\n que.pop();\n int suf{dat[dat[v].par].suf};\n assert(0 <= suf and suf < (int)dat.size());\n int label{dat[v].label};\n // std::cout << suf << ' ' << dat[suf].suf << std::endl;\n while (dat[suf].ch[label] == -1 and dat[suf].suf != suf) {\n suf = dat[suf].suf;\n }\n if (dat[suf].ch[label] == -1) {\n assert(suf == 0);\n }\n else if (dat[suf].ch[label] == v) {\n assert(suf == 0);\n }\n else {\n suf = dat[suf].ch[label];\n }\n dat[v].suf = suf;\n dat[v].cnt += dat[suf].cnt;\n for (int i{} ; i < 26 ; i++) {\n if (dat[v].ch[i] == -1) continue;\n que.emplace(dat[v].ch[i]);\n }\n }\n }\n Trie(const std::vector<std::string>& S) : dat(1) {\n for (const auto& s : S) insert(s);\n // debug(\"insert end\");\n makeSuffixLink();\n // debug(\"suflink end\");\n }\n std::pair<int, int> moved(int v, char c) const {\n // debug(\"try \" + std::string{c});\n assert(0 <= v and v < (int)dat.size());\n assert('a' <= c and c <= 'z');\n int label{c - 'a'};\n while (dat[v].ch[label] == -1 and dat[v].suf != v) {\n v = dat[v].suf;\n }\n if (dat[v].ch[label] == -1) {\n assert(v == 0);\n }\n else {\n v = dat[v].ch[label];\n }\n return { v, dat[v].cnt };\n }\n std::pair<int, int> moved(int v, const std::string& s) const {\n // debug(\"try \" + s);\n std::pair<int, int> res{v, 0};\n for (auto c : s) {\n auto [x, y]{moved(res.first, c)};\n res.first = x;\n res.second += y;\n }\n return res;\n }\n int size() const {\n return (int)dat.size();\n }\n};\n\nconstexpr long long mod{1000000007};\n\nbool solve() {\n int N, M, K;\n std::cin >> N >> M >> K;\n if (N == 0) return false;\n std::vector<std::string> from(N), to(N);\n Comp comp;\n for (int i{} ; i < N ; i++) {\n std::cin >> from[i] >> to[i];\n comp.push_back(from[i]);\n comp.push_back(to[i]);\n }\n comp.build();\n std::vector<std::vector<int>> g(comp.size());\n for (int i{} ; i < N ; i++) {\n g[comp.at(from[i])].push_back(comp.at(to[i]));\n }\n std::vector<std::string> S(K);\n for (auto& s : S) std::cin >> s;\n Trie aho(S);\n using qt = std::tuple<int, int, int, int>; // len, cnt, v, cur\n std::priority_queue<qt, std::vector<qt>, std::greater<qt>> que;\n auto f{[&](int len, int cnt, int v, int cur) -> long long {\n assert(0 <= len and len <= M);\n assert(0 <= cnt and cnt <= 1);\n assert(0 <= v and v < (int)aho.size());\n assert(0 <= cur and cur < (int)comp.size());\n long long res{len};\n res = res * (long long)2 + cnt;\n res = res * (long long)aho.size() + v;\n res = res * (long long)comp.size() + cur;\n return res;\n }};\n std::map<long long, int> dp;\n for (int i{} ; i < (int)comp.size() ; i++) {\n const std::string& str{comp.inv(i)};\n if ((int)str.size() > M) continue;\n auto [v, cnt]{aho.moved(0, str)};\n if (cnt >= 2) continue;\n dp[f((int)str.size(), cnt, v, i)] = 1;\n que.emplace((int)str.size(), cnt, v, i);\n }\n while (que.size()) {\n auto [len, cnt, v, cur]{que.top()};\n que.pop();\n auto it{dp.find(f(len, cnt, v, cur))};\n assert(it != dp.end());\n for (auto next : g[cur]) {\n const std::string& str{comp.inv(next)};\n if (len + (int)str.size() > M) continue;\n auto [x, add]{aho.moved(v, str)};\n if (cnt + add >= 2) continue;\n long long key{f(len + (int)str.size(), cnt + add, x, next)};\n if (dp.find(key) == dp.end()) {\n que.emplace(len + (int)str.size(), cnt + add, x, next);\n }\n dp[key] += it->second;\n if (dp[key] >= mod) dp[key] -= mod;\n }\n }\n int ans{};\n for (int v{} ; v < (int)aho.size() ; v++) {\n for (int cur{} ; cur < (int)comp.size() ; cur++) {\n auto it{dp.find(f(M, 1, v, cur))};\n if (it != dp.end()) {\n ans += it->second;\n if (ans >= mod) ans -= mod;\n }\n }\n }\n std::cout << ans << '\\n';\n return true;\n}\n\nint main() {\n std::cin.tie(nullptr)->sync_with_stdio(false);\n while (solve()) ;\n}", "accuracy": 1, "time_ms": 6800, "memory_kb": 201700, "score_of_the_acc": -1.9608, "final_rank": 20 }, { "submission_id": "aoj_2257_8426047", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define repd(i,a,b) for (ll i=(a);i<(b);i++)\n#define rep(i,n) repd(i,0,n)\n#define all(x) (x).begin(),(x).end()\ntemplate<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }\ntemplate<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }\ntypedef long long ll;\ntypedef pair<ll,ll> P;\ntypedef vector<ll> vec;\nusing Graph = vector<vector<ll>>;\nconst long long INF = 1LL<<60;\nconst long long MOD = 1000000007;\n \n// Aho-Corasick\n// https://youtu.be/BYoRvdgI5EU?t=9633\nstruct Aho {\n using MP = unordered_map<char,int>;\n vector<MP> to;\n vector<int> cnt, fail;\n Aho(): to(1), cnt(1) {}\n int add(const string& s) {\n int v = 0;\n for (char c : s) {\n if (!to[v].count(c)) {\n to[v][c] = to.size();\n to.push_back(MP());\n cnt.push_back(0);\n }\n v = to[v][c];\n }\n cnt[v]++;\n return v;\n }\n void init() {\n fail = vector<int>(to.size(), -1);\n queue<int> q;\n q.push(0);\n while (!q.empty()) {\n int v = q.front(); q.pop();\n for (auto [c,u] : to[v]) {\n fail[u] = (*this)(fail[v],c);\n cnt[u] += cnt[fail[u]];\n q.push(u);\n }\n }\n }\n int operator()(int v, char c) const {\n while (v != -1) {\n auto it = to[v].find(c);\n if (it != to[v].end()) return it->second;\n v = fail[v];\n }\n return 0;\n }\n int operator[](int v) const { return cnt[v];}\n};\n\n// auto mod int\n// https://youtu.be/L8grWxBlIZ4?t=9858\n// https://youtu.be/ERZuLAxZffQ?t=4807 : optimize\n// https://youtu.be/8uowVvQ_-Mo?t=1329 : division\nconst int mod = 1000000007;\nstruct mint {\n ll x; // typedef long long ll;\n mint(ll x=0):x((x%mod+mod)%mod){}\n mint operator-() const { return mint(-x);}\n mint& operator+=(const mint a) {\n if ((x += a.x) >= mod) x -= mod;\n return *this;\n }\n mint& operator-=(const mint a) {\n if ((x += mod-a.x) >= mod) x -= mod;\n return *this;\n }\n mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this;}\n mint operator+(const mint a) const { return mint(*this) += a;}\n mint operator-(const mint a) const { return mint(*this) -= a;}\n mint operator*(const mint a) const { return mint(*this) *= a;}\n mint pow(ll t) const {\n if (!t) return 1;\n mint a = pow(t>>1);\n a *= a;\n if (t&1) a *= *this;\n return a;\n }\n\n // for prime mod\n mint inv() const { return pow(mod-2);}\n mint& operator/=(const mint a) { return *this *= a.inv();}\n mint operator/(const mint a) const { return mint(*this) /= a;}\n};\nistream& operator>>(istream& is, mint& a) { return is >> a.x;}\nostream& operator<<(ostream& os, const mint& a) { return os << a.x;}\n\n\nint main()\n{ \n ios::sync_with_stdio(false);\n cin.tie(0);\n int n,m,k;\n while(cin>>n>>m>>k){\n if(n==0&&m==0&&k==0)break;\n map<string,int> mp;\n Graph g;\n vector<string> memo;\n vector<string> F(n),G(n);\n set<int> set;\n rep(i,n){\n cin>>F[i]>>G[i];\n if(!mp.count(F[i])){mp[F[i]]=mp.size();g.push_back({});memo.push_back(F[i]);}\n set.insert(mp[F[i]]);\n }\n rep(i,n){\n if(!mp.count(G[i])){mp[G[i]]=mp.size();memo.push_back(G[i]);}\n g[mp[F[i]]].push_back(mp[G[i]]);\n set.insert(mp[G[i]]);\n }\n Aho aho;\n rep(i,k){\n string s;cin>>s;\n aho.add(s);\n }\n aho.init();\n int sz=aho.to.size();\n int sz2=g.size();\n mint ans=0;\n vector dp(21,vector(sz,vector(sz2,vector<mint>(2))));\n while(set.size()){\n auto x=*set.begin();\n set.erase(set.begin());\n ll i=0,j=0,k=x,l=0;\n for(char c:memo[x]){\n i++;\n j=aho(j,c);\n l+=aho[j];\n }\n if(l>1||i>m)continue;\n if(i==m&&l==1)ans+=1;\n if(k<g.size())dp[i][j][k][l]+=1;\n }\n rep(i,m){\n rep(j,sz){\n rep(k,sz2){\n rep(l,2){\n if(dp[i%21][j][k][l].x==0)continue;\n for(int nk:g[k]){\n if(nk>=g.size()&&m-i>20)continue;\n ll ni=i%21,nj=j,nl=l;\n for(char c:memo[nk]){\n ni++;\n nj=aho(nj,c);\n nl+=aho[nj];\n }\n if(nl>1)continue;\n if(m-i<=20&&ni%21==m%21&&nl==1){ans+=dp[i%21][j][k][l];continue;}\n if(nk>=g.size())continue;\n dp[ni%21][nj][nk][nl]+=dp[i%21][j][k][l];\n //cout<<i%21<<\" \"<<j<<\" \"<<k<<\" \"<<l<<\" \"<<dp[i%21][j][k][l]<<endl;\n //cout<<ni%21<<\" \"<<nj<<\" \"<<nk<<\" \"<<nl<<\" \"<<dp[ni%21][nj][nk][nl]<<endl;\n }\n dp[i%21][j][k][l]=0;\n }\n }\n }\n }\n cout<<ans<<'\\n';\n }\n return 0;\n}", "accuracy": 1, "time_ms": 5840, "memory_kb": 181500, "score_of_the_acc": -1.7128, "final_rank": 19 }, { "submission_id": "aoj_2257_8387908", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nconst int MOD = 1000000007;\n\nclass string_hash {\nprivate:\n\tint n;\n\tvector<array<int, 2> > pw;\n\tvector<array<int, 2> > h;\npublic:\n\tstatic constexpr int mod0 = 970071317;\n\tstatic constexpr int mod1 = 979598959;\n\tstring_hash() : n(0), pw(vector<array<int, 2> >()), h(vector<array<int, 2> >()) {}\n\tstring_hash(const string& s) : n(s.size()) {\n\t\tpw.resize(n + 1);\n\t\th.resize(n + 1);\n\t\tpw[0] = { 1, 1 };\n\t\th[0] = { 0, 0 };\n\t\tfor (int i = 0; i < n; i++) {\n\t\t\tpw[i + 1][0] = 1LL * pw[i][0] * 311 % mod0;\n\t\t\tpw[i + 1][1] = 1LL * pw[i][1] * 359 % mod1;\n\t\t\th[i + 1][0] = (1LL * h[i][0] * 311 + s[i]) % mod0;\n\t\t\th[i + 1][1] = (1LL * h[i][1] * 359 + s[i]) % mod1;\n\t\t}\n\t}\n\tuint64_t hash(int l, int r) const {\n\t\tassert(0 <= l && l <= r && r <= n);\n\t\tint v0 = (h[r][0] - 1LL * h[l][0] * pw[r - l][0] % mod0 + mod0) % mod0;\n\t\tint v1 = (h[r][1] - 1LL * h[l][1] * pw[r - l][1] % mod1 + mod1) % mod1;\n\t\treturn uint64_t(v0) * mod1 + v1;\n\t}\n};\n\nint solve(int N, int M, int K, const vector<string>& A, const vector<string>& B, const vector<string>& C) {\n\tvector<string> D = { string() };\n\tfor (int i = 0; i < K; i++) {\n\t\tfor (int j = 1; j <= C[i].size(); j++) {\n\t\t\tD.push_back(C[i].substr(0, j));\n\t\t}\n\t}\n\tsort(D.begin(), D.end(), [](const string& s1, const string& s2) {\n\t\treturn s1.size() != s2.size() ? s1.size() < s2.size() : s1 < s2;\n\t});\n\tD.erase(unique(D.begin(), D.end()), D.end());\n\tint Z = D.size();\n\tvector<uint64_t> CH(K);\n\tfor (int i = 0; i < K; i++) {\n\t\tCH[i] = string_hash(C[i]).hash(0, C[i].size());\n\t}\n\tvector<uint64_t> DH(Z);\n\tfor (int i = 0; i < Z; i++) {\n\t\tDH[i] = string_hash(D[i]).hash(0, D[i].size());\n\t}\n\tauto calc = [&](int state, const string& tail) -> pair<int, int> {\n\t\t// returns (state, # of new seasons)\n\t\tstring s = D[state] + tail;\n\t\tstring_hash h(s);\n\t\tint seasons = 0;\n\t\tfor (int i = 0; i < int(s.size()); i++) {\n\t\t\tfor (int j = 0; j < K; j++) {\n\t\t\t\tint r = i + C[j].size();\n\t\t\t\tif (D[state].size() < r && r <= s.size() && h.hash(i, r) == CH[j]) {\n\t\t\t\t\tseasons += 1;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tint nstate = 0;\n\t\tfor (int i = 1; i < Z; i++) {\n\t\t\tif (D[i].size() <= s.size() && h.hash(s.size() - D[i].size(), s.size()) == DH[i]) {\n\t\t\t\tnstate = i;\n\t\t\t}\n\t\t}\n\t\treturn make_pair(nstate, seasons);\n\t};\n\tvector<string> E(2 * N + 1);\n\tfor (int i = 0; i < N; i++) {\n\t\tE[2 * i + 0] = A[i];\n\t\tE[2 * i + 1] = B[i];\n\t}\n\tsort(E.begin(), E.end());\n\tE.erase(unique(E.begin(), E.end()), E.end());\n\tint W = E.size();\n\tvector<vector<int> > G(W);\n\tfor (int i = 0; i < N; i++) {\n\t\tint pa = lower_bound(E.begin(), E.end(), A[i]) - E.begin();\n\t\tint pb = lower_bound(E.begin(), E.end(), B[i]) - E.begin();\n\t\tG[pa].push_back(pb);\n\t}\n\tfor (int i = 1; i < W; i++) {\n\t\tG[0].push_back(i);\n\t}\n\tvector<vector<pair<int, int> > > precalc(W, vector<pair<int, int> >(Z));\n\tfor (int i = 0; i < W; i++) {\n\t\tfor (int j = 0; j < Z; j++) {\n\t\t\tprecalc[i][j] = calc(j, E[i]);\n\t\t}\n\t}\n\tint maxlen = 0;\n\tfor (int i = 0; i < W; i++) {\n\t\tmaxlen = max(maxlen, int(E[i].size()));\n\t}\n\tint cycle = maxlen + 1;\n\tvector<vector<vector<array<int, 2> > > > dp(cycle, vector<vector<array<int, 2> > >(W, vector<array<int, 2> >(Z)));\n\tdp[0][0][0][0] = 1;\n\tfor (int i = 0; i < M; i++) {\n\t\tfor (int j = 0; j < W; j++) {\n\t\t\tfor (int k = 0; k < Z; k++) {\n\t\t\t\tfor (int l = 0; l < 2; l++) {\n\t\t\t\t\tfor (int v : G[j]) {\n\t\t\t\t\t\tint len = i + E[v].size();\n\t\t\t\t\t\tint ns = precalc[v][k].first;\n\t\t\t\t\t\tint nc = l + precalc[v][k].second;\n\t\t\t\t\t\tif (len <= M && nc <= 1) {\n\t\t\t\t\t\t\tdp[len % cycle][v][ns][nc] += dp[i % cycle][j][k][l];\n\t\t\t\t\t\t\tdp[len % cycle][v][ns][nc] %= MOD;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tfor (int j = 0; j < W; j++) {\n\t\t\tfor (int k = 0; k < Z; k++) {\n\t\t\t\tdp[i % cycle][j][k][0] = 0;\n\t\t\t\tdp[i % cycle][j][k][1] = 0;\n\t\t\t}\n\t\t}\n\t}\n\tint ans = 0;\n\tfor (int i = 0; i < W; i++) {\n\t\tfor (int j = 0; j < Z; j++) {\n\t\t\tans += dp[M % cycle][i][j][1];\n\t\t\tans %= MOD;\n\t\t}\n\t}\n\treturn ans;\n}\n\nint main() {\n\tcin.tie(0);\n\tios::sync_with_stdio(false);\n\twhile (true) {\n\t\tint N, M, K;\n\t\tcin >> N >> M >> K;\n\t\tif (N == 0 && M == 0 && K == 0) {\n\t\t\tbreak;\n\t\t}\n\t\tvector<string> A(N), B(N), C(K);\n\t\tfor (int i = 0; i < N; i++) {\n\t\t\tcin >> A[i] >> B[i];\n\t\t}\n\t\tfor (int i = 0; i < K; i++) {\n\t\t\tcin >> C[i];\n\t\t}\n\t\tint ans = solve(N, M, K, A, B, C);\n\t\tcout << ans << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 7060, "memory_kb": 56628, "score_of_the_acc": -1.2587, "final_rank": 14 }, { "submission_id": "aoj_2257_6774079", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntemplate<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }\ntemplate<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }\n#define all(x) (x).begin(),(x).end()\n#define fi first\n#define se second\n#define mp make_pair\n#define si(x) int(x.size())\nconst int mod=998244353,MAX=605;\nconst ll INF=1LL<<62;\n\n//modintのみ\n\n// from: https://gist.github.com/yosupo06/ddd51afb727600fd95d9d8ad6c3c80c9\n// (based on AtCoder STL)\n\n#include <cassert>\n#include <numeric>\n#include <type_traits>\n\nnamespace atcoder {\n \n namespace internal {\n \n#ifndef _MSC_VER\n template <class T>\n using is_signed_int128 =\n typename std::conditional<std::is_same<T, __int128_t>::value ||\n std::is_same<T, __int128>::value,\n std::true_type,\n std::false_type>::type;\n \n template <class T>\n using is_unsigned_int128 =\n typename std::conditional<std::is_same<T, __uint128_t>::value ||\n std::is_same<T, unsigned __int128>::value,\n std::true_type,\n std::false_type>::type;\n \n template <class T>\n using make_unsigned_int128 =\n typename std::conditional<std::is_same<T, __int128_t>::value,\n __uint128_t,\n unsigned __int128>;\n \n template <class T>\n using is_integral = typename std::conditional<std::is_integral<T>::value ||\n is_signed_int128<T>::value ||\n is_unsigned_int128<T>::value,\n std::true_type,\n std::false_type>::type;\n \n template <class T>\n using is_signed_int = typename std::conditional<(is_integral<T>::value &&\n std::is_signed<T>::value) ||\n is_signed_int128<T>::value,\n std::true_type,\n std::false_type>::type;\n \n template <class T>\n using is_unsigned_int =\n typename std::conditional<(is_integral<T>::value &&\n std::is_unsigned<T>::value) ||\n is_unsigned_int128<T>::value,\n std::true_type,\n std::false_type>::type;\n \n template <class T>\n using to_unsigned = typename std::conditional<\n is_signed_int128<T>::value,\n make_unsigned_int128<T>,\n typename std::conditional<std::is_signed<T>::value,\n std::make_unsigned<T>,\n std::common_type<T>>::type>::type;\n \n#else\n \n template <class T> using is_integral = typename std::is_integral<T>;\n \n template <class T>\n using is_signed_int =\n typename std::conditional<is_integral<T>::value && std::is_signed<T>::value,\n std::true_type,\n std::false_type>::type;\n \n template <class T>\n using is_unsigned_int =\n typename std::conditional<is_integral<T>::value &&\n std::is_unsigned<T>::value,\n std::true_type,\n std::false_type>::type;\n \n template <class T>\n using to_unsigned = typename std::conditional<is_signed_int<T>::value,\n std::make_unsigned<T>,\n std::common_type<T>>::type;\n \n#endif\n \n template <class T>\n using is_signed_int_t = std::enable_if_t<is_signed_int<T>::value>;\n \n template <class T>\n using is_unsigned_int_t = std::enable_if_t<is_unsigned_int<T>::value>;\n \n template <class T> using to_unsigned_t = typename to_unsigned<T>::type;\n \n } // namespace internal\n \n} // namespace atcoder\n\n#include <utility>\n\nnamespace atcoder {\n \n namespace internal {\n \n constexpr long long safe_mod(long long x, long long m) {\n x %= m;\n if (x < 0) x += m;\n return x;\n }\n \n struct barrett {\n unsigned int _m;\n unsigned long long im;\n \n barrett(unsigned int m) : _m(m), im((unsigned long long)(-1) / m + 1) {}\n \n unsigned int umod() const { return _m; }\n \n unsigned int mul(unsigned int a, unsigned int b) const {\n \n unsigned long long z = a;\n z *= b;\n#ifdef _MSC_VER\n unsigned long long x;\n _umul128(z, im, &x);\n#else\n unsigned long long x =\n (unsigned long long)(((unsigned __int128)(z)*im) >> 64);\n#endif\n unsigned int v = (unsigned int)(z - x * _m);\n if (_m <= v) v += _m;\n return v;\n }\n };\n \n constexpr long long pow_mod_constexpr(long long x, long long n, int m) {\n if (m == 1) return 0;\n unsigned int _m = (unsigned int)(m);\n unsigned long long r = 1;\n unsigned long long y = safe_mod(x, m);\n while (n) {\n if (n & 1) r = (r * y) % _m;\n y = (y * y) % _m;\n n >>= 1;\n }\n return r;\n }\n \n constexpr bool is_prime_constexpr(int n) {\n if (n <= 1) return false;\n if (n == 2 || n == 7 || n == 61) return true;\n if (n % 2 == 0) return false;\n long long d = n - 1;\n while (d % 2 == 0) d /= 2;\n for (long long a : {2, 7, 61}) {\n long long t = d;\n long long y = pow_mod_constexpr(a, t, n);\n while (t != n - 1 && y != 1 && y != n - 1) {\n y = y * y % n;\n t <<= 1;\n }\n if (y != n - 1 && t % 2 == 0) {\n return false;\n }\n }\n return true;\n }\n template <int n> constexpr bool is_prime = is_prime_constexpr(n);\n \n constexpr std::pair<long long, long long> inv_gcd(long long a, long long b) {\n a = safe_mod(a, b);\n if (a == 0) return {b, 0};\n \n long long s = b, t = a;\n long long m0 = 0, m1 = 1;\n \n while (t) {\n long long u = s / t;\n s -= t * u;\n m0 -= m1 * u; // |m1 * u| <= |m1| * s <= b\n \n \n auto tmp = s;\n s = t;\n t = tmp;\n tmp = m0;\n m0 = m1;\n m1 = tmp;\n }\n if (m0 < 0) m0 += b / s;\n return {s, m0};\n }\n \n constexpr int primitive_root_constexpr(int m) {\n if (m == 2) return 1;\n if (m == 167772161) return 3;\n if (m == 469762049) return 3;\n if (m == 754974721) return 11;\n if (m == 998244353) return 3;\n int divs[20] = {};\n divs[0] = 2;\n int cnt = 1;\n int x = (m - 1) / 2;\n while (x % 2 == 0) x /= 2;\n for (int i = 3; (long long)(i)*i <= x; i += 2) {\n if (x % i == 0) {\n divs[cnt++] = i;\n while (x % i == 0) {\n x /= i;\n }\n }\n }\n if (x > 1) {\n divs[cnt++] = x;\n }\n for (int g = 2;; g++) {\n bool ok = true;\n for (int i = 0; i < cnt; i++) {\n if (pow_mod_constexpr(g, (m - 1) / divs[i], m) == 1) {\n ok = false;\n break;\n }\n }\n if (ok) return g;\n }\n }\n template <int m> constexpr int primitive_root = primitive_root_constexpr(m);\n \n } // namespace internal\n \n} // namespace atcoder\n\n#include <cassert>\n#include <numeric>\n#include <type_traits>\n\n#ifdef _MSC_VER\n#include <intrin.h>\n#endif\n\nnamespace atcoder {\n \n namespace internal {\n \n struct modint_base {};\n struct static_modint_base : modint_base {};\n \n template <class T> using is_modint = std::is_base_of<modint_base, T>;\n template <class T> using is_modint_t = std::enable_if_t<is_modint<T>::value>;\n \n } // namespace internal\n \n template <int m, std::enable_if_t<(1 <= m)>* = nullptr>\n struct static_modint : internal::static_modint_base {\n using mint = static_modint;\n \n public:\n static constexpr int mod() { return m; }\n static mint raw(int v) {\n mint x;\n x._v = v;\n return x;\n }\n \n static_modint() : _v(0) {}\n template <class T, internal::is_signed_int_t<T>* = nullptr>\n static_modint(T v) {\n long long x = (long long)(v % (long long)(umod()));\n if (x < 0) x += umod();\n _v = (unsigned int)(x);\n }\n template <class T, internal::is_unsigned_int_t<T>* = nullptr>\n static_modint(T v) {\n _v = (unsigned int)(v % umod());\n }\n static_modint(bool v) { _v = ((unsigned int)(v) % umod()); }\n \n unsigned int val() const { return _v; }\n \n mint& operator++() {\n _v++;\n if (_v == umod()) _v = 0;\n return *this;\n }\n mint& operator--() {\n if (_v == 0) _v = umod();\n _v--;\n return *this;\n }\n mint operator++(int) {\n mint result = *this;\n ++*this;\n return result;\n }\n mint operator--(int) {\n mint result = *this;\n --*this;\n return result;\n }\n \n mint& operator+=(const mint& rhs) {\n _v += rhs._v;\n if (_v >= umod()) _v -= umod();\n return *this;\n }\n mint& operator-=(const mint& rhs) {\n _v -= rhs._v;\n if (_v >= umod()) _v += umod();\n return *this;\n }\n mint& operator*=(const mint& rhs) {\n unsigned long long z = _v;\n z *= rhs._v;\n _v = (unsigned int)(z % umod());\n return *this;\n }\n mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }\n \n mint operator+() const { return *this; }\n mint operator-() const { return mint() - *this; }\n \n mint pow(long long n) const {\n assert(0 <= n);\n mint x = *this, r = 1;\n while (n) {\n if (n & 1) r *= x;\n x *= x;\n n >>= 1;\n }\n return r;\n }\n mint inv() const {\n if (prime) {\n assert(_v);\n return pow(umod() - 2);\n } else {\n auto eg = internal::inv_gcd(_v, m);\n assert(eg.first == 1);\n return eg.second;\n }\n }\n \n friend mint operator+(const mint& lhs, const mint& rhs) {\n return mint(lhs) += rhs;\n }\n friend mint operator-(const mint& lhs, const mint& rhs) {\n return mint(lhs) -= rhs;\n }\n friend mint operator*(const mint& lhs, const mint& rhs) {\n return mint(lhs) *= rhs;\n }\n friend mint operator/(const mint& lhs, const mint& rhs) {\n return mint(lhs) /= rhs;\n }\n friend bool operator==(const mint& lhs, const mint& rhs) {\n return lhs._v == rhs._v;\n }\n friend bool operator!=(const mint& lhs, const mint& rhs) {\n return lhs._v != rhs._v;\n }\n \n private:\n unsigned int _v;\n static constexpr unsigned int umod() { return m; }\n static constexpr bool prime = internal::is_prime<m>;\n };\n \n template <int id> struct dynamic_modint : internal::modint_base {\n using mint = dynamic_modint;\n \n public:\n static int mod() { return (int)(bt.umod()); }\n static void set_mod(int m) {\n assert(1 <= m);\n bt = internal::barrett(m);\n }\n static mint raw(int v) {\n mint x;\n x._v = v;\n return x;\n }\n \n dynamic_modint() : _v(0) {}\n template <class T, internal::is_signed_int_t<T>* = nullptr>\n dynamic_modint(T v) {\n long long x = (long long)(v % (long long)(mod()));\n if (x < 0) x += mod();\n _v = (unsigned int)(x);\n }\n template <class T, internal::is_unsigned_int_t<T>* = nullptr>\n dynamic_modint(T v) {\n _v = (unsigned int)(v % mod());\n }\n dynamic_modint(bool v) { _v = ((unsigned int)(v) % mod()); }\n \n unsigned int val() const { return _v; }\n \n mint& operator++() {\n _v++;\n if (_v == umod()) _v = 0;\n return *this;\n }\n mint& operator--() {\n if (_v == 0) _v = umod();\n _v--;\n return *this;\n }\n mint operator++(int) {\n mint result = *this;\n ++*this;\n return result;\n }\n mint operator--(int) {\n mint result = *this;\n --*this;\n return result;\n }\n \n mint& operator+=(const mint& rhs) {\n _v += rhs._v;\n if (_v >= umod()) _v -= umod();\n return *this;\n }\n mint& operator-=(const mint& rhs) {\n _v += mod() - rhs._v;\n if (_v >= umod()) _v -= umod();\n return *this;\n }\n mint& operator*=(const mint& rhs) {\n _v = bt.mul(_v, rhs._v);\n return *this;\n }\n mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }\n \n mint operator+() const { return *this; }\n mint operator-() const { return mint() - *this; }\n \n mint pow(long long n) const {\n assert(0 <= n);\n mint x = *this, r = 1;\n while (n) {\n if (n & 1) r *= x;\n x *= x;\n n >>= 1;\n }\n return r;\n }\n mint inv() const {\n auto eg = internal::inv_gcd(_v, mod());\n assert(eg.first == 1);\n return eg.second;\n }\n \n friend mint operator+(const mint& lhs, const mint& rhs) {\n return mint(lhs) += rhs;\n }\n friend mint operator-(const mint& lhs, const mint& rhs) {\n return mint(lhs) -= rhs;\n }\n friend mint operator*(const mint& lhs, const mint& rhs) {\n return mint(lhs) *= rhs;\n }\n friend mint operator/(const mint& lhs, const mint& rhs) {\n return mint(lhs) /= rhs;\n }\n friend bool operator==(const mint& lhs, const mint& rhs) {\n return lhs._v == rhs._v;\n }\n friend bool operator!=(const mint& lhs, const mint& rhs) {\n return lhs._v != rhs._v;\n }\n \n private:\n unsigned int _v;\n static internal::barrett bt;\n static unsigned int umod() { return bt.umod(); }\n };\n template <int id> internal::barrett dynamic_modint<id>::bt = 998244353;\n \n using modint998244353 = static_modint<998244353>;\n using modint1000000007 = static_modint<1000000007>;\n using modint = dynamic_modint<-1>;\n \n namespace internal {\n \n template <class T>\n using is_static_modint = std::is_base_of<internal::static_modint_base, T>;\n \n template <class T>\n using is_static_modint_t = std::enable_if_t<is_static_modint<T>::value>;\n \n template <class> struct is_dynamic_modint : public std::false_type {};\n template <int id>\n struct is_dynamic_modint<dynamic_modint<id>> : public std::true_type {};\n \n template <class T>\n using is_dynamic_modint_t = std::enable_if_t<is_dynamic_modint<T>::value>;\n \n } // namespace internal\n \n} // namespace atcoder\n\nusing mint=atcoder::modint1000000007;\n\nmint dp[21][MAX][MAX][2];\nvector<int> G[MAX];\nint to[MAX][MAX],cn[MAX][MAX];\n\nconst int D=21;\n\nint main(){\n \n std::ifstream in(\"text.txt\");\n std::cin.rdbuf(in.rdbuf());\n cin.tie(0);\n ios::sync_with_stdio(false);\n \n while(1){\n int N,M,K;cin>>N>>M>>K;\n if(N==0) break;\n \n for(int i=0;i<MAX;i++) G[i].clear();\n memset(dp,0,sizeof(dp));\n memset(to,-1,sizeof(to));\n memset(cn,0,sizeof(cn));\n \n vector<pair<string,string>> S(N);\n vector<string> use={\"\"};\n for(int i=0;i<N;i++){\n cin>>S[i].fi>>S[i].se;\n use.push_back(S[i].fi);\n use.push_back(S[i].se);\n }\n sort(all(use));\n use.erase(unique(all(use)),use.end());\n \n for(int i=0;i<N;i++){\n G[lower_bound(all(use),S[i].fi)-use.begin()].push_back(lower_bound(all(use),S[i].se)-use.begin());\n }\n for(int i=1;i<si(use);i++){\n G[0].push_back(i);\n }\n \n vector<string> match;\n vector<string> T(K);\n for(int i=0;i<K;i++){\n cin>>T[i];\n for(int j=0;j<=si(T[i]);j++){\n match.push_back(T[i].substr(0,j));\n }\n }\n sort(all(match));\n match.erase(unique(all(match)),match.end());\n \n vector<pair<string,int>> X(si(match));\n for(int i=0;i<si(match);i++) X[i]=mp(match[i],i);\n sort(all(X),[](auto a,auto b){\n return si(a.fi)>si(b.fi);\n });\n \n for(int a=0;a<si(match);a++){\n for(int b=1;b<si(use);b++){\n string U=match[a]+use[b];\n for(int s=0;s<si(U);s++){\n for(int i=0;i<K;i++){\n int t=s+si(T[i]);\n if(t>si(U)||t<=si(match[a])) continue;\n if(U.substr(s,si(T[i]))==T[i]) cn[a][b]++;\n if(cn[a][b]>=2) break;\n }\n }\n for(auto x:X){\n if(si(U)<si(x.fi)) continue;\n if(U.substr(si(U)-si(x.fi))==x.fi){\n to[a][b]=x.se;\n break;\n }\n }\n }\n }\n \n dp[0][0][0][0]=1;\n \n for(int i=0;i<M;i++){\n int s=i%D;\n for(int j=0;j<si(use);j++){\n for(int k=0;k<si(match);k++){\n for(int f=0;f<2;f++){\n if(dp[s][j][k][f]==0) continue;\n \n for(int t:G[j]){\n if(f+cn[k][t]>=2) continue;\n dp[(s+si(use[t]))%D][t][to[k][t]][f+cn[k][t]]+=dp[s][j][k][f];\n }\n \n dp[s][j][k][f]=0;\n }\n }\n }\n }\n \n mint ans=0;\n \n for(int j=0;j<si(use);j++){\n for(int k=0;k<si(match);k++){\n ans+=dp[M%D][j][k][1];\n }\n }\n \n cout<<ans.val()<<\"\\n\";\n }\n}", "accuracy": 1, "time_ms": 5690, "memory_kb": 66484, "score_of_the_acc": -1.1024, "final_rank": 12 }, { "submission_id": "aoj_2257_6774068", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntemplate<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }\ntemplate<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }\n#define all(x) (x).begin(),(x).end()\n#define fi first\n#define se second\n#define mp make_pair\n#define si(x) int(x.size())\nconst int mod=998244353,MAX=605;\nconst ll INF=1LL<<62;\n\n//modintのみ\n\n// from: https://gist.github.com/yosupo06/ddd51afb727600fd95d9d8ad6c3c80c9\n// (based on AtCoder STL)\n\n#include <cassert>\n#include <numeric>\n#include <type_traits>\n\nnamespace atcoder {\n \n namespace internal {\n \n#ifndef _MSC_VER\n template <class T>\n using is_signed_int128 =\n typename std::conditional<std::is_same<T, __int128_t>::value ||\n std::is_same<T, __int128>::value,\n std::true_type,\n std::false_type>::type;\n \n template <class T>\n using is_unsigned_int128 =\n typename std::conditional<std::is_same<T, __uint128_t>::value ||\n std::is_same<T, unsigned __int128>::value,\n std::true_type,\n std::false_type>::type;\n \n template <class T>\n using make_unsigned_int128 =\n typename std::conditional<std::is_same<T, __int128_t>::value,\n __uint128_t,\n unsigned __int128>;\n \n template <class T>\n using is_integral = typename std::conditional<std::is_integral<T>::value ||\n is_signed_int128<T>::value ||\n is_unsigned_int128<T>::value,\n std::true_type,\n std::false_type>::type;\n \n template <class T>\n using is_signed_int = typename std::conditional<(is_integral<T>::value &&\n std::is_signed<T>::value) ||\n is_signed_int128<T>::value,\n std::true_type,\n std::false_type>::type;\n \n template <class T>\n using is_unsigned_int =\n typename std::conditional<(is_integral<T>::value &&\n std::is_unsigned<T>::value) ||\n is_unsigned_int128<T>::value,\n std::true_type,\n std::false_type>::type;\n \n template <class T>\n using to_unsigned = typename std::conditional<\n is_signed_int128<T>::value,\n make_unsigned_int128<T>,\n typename std::conditional<std::is_signed<T>::value,\n std::make_unsigned<T>,\n std::common_type<T>>::type>::type;\n \n#else\n \n template <class T> using is_integral = typename std::is_integral<T>;\n \n template <class T>\n using is_signed_int =\n typename std::conditional<is_integral<T>::value && std::is_signed<T>::value,\n std::true_type,\n std::false_type>::type;\n \n template <class T>\n using is_unsigned_int =\n typename std::conditional<is_integral<T>::value &&\n std::is_unsigned<T>::value,\n std::true_type,\n std::false_type>::type;\n \n template <class T>\n using to_unsigned = typename std::conditional<is_signed_int<T>::value,\n std::make_unsigned<T>,\n std::common_type<T>>::type;\n \n#endif\n \n template <class T>\n using is_signed_int_t = std::enable_if_t<is_signed_int<T>::value>;\n \n template <class T>\n using is_unsigned_int_t = std::enable_if_t<is_unsigned_int<T>::value>;\n \n template <class T> using to_unsigned_t = typename to_unsigned<T>::type;\n \n } // namespace internal\n \n} // namespace atcoder\n\n#include <utility>\n\nnamespace atcoder {\n \n namespace internal {\n \n constexpr long long safe_mod(long long x, long long m) {\n x %= m;\n if (x < 0) x += m;\n return x;\n }\n \n struct barrett {\n unsigned int _m;\n unsigned long long im;\n \n barrett(unsigned int m) : _m(m), im((unsigned long long)(-1) / m + 1) {}\n \n unsigned int umod() const { return _m; }\n \n unsigned int mul(unsigned int a, unsigned int b) const {\n \n unsigned long long z = a;\n z *= b;\n#ifdef _MSC_VER\n unsigned long long x;\n _umul128(z, im, &x);\n#else\n unsigned long long x =\n (unsigned long long)(((unsigned __int128)(z)*im) >> 64);\n#endif\n unsigned int v = (unsigned int)(z - x * _m);\n if (_m <= v) v += _m;\n return v;\n }\n };\n \n constexpr long long pow_mod_constexpr(long long x, long long n, int m) {\n if (m == 1) return 0;\n unsigned int _m = (unsigned int)(m);\n unsigned long long r = 1;\n unsigned long long y = safe_mod(x, m);\n while (n) {\n if (n & 1) r = (r * y) % _m;\n y = (y * y) % _m;\n n >>= 1;\n }\n return r;\n }\n \n constexpr bool is_prime_constexpr(int n) {\n if (n <= 1) return false;\n if (n == 2 || n == 7 || n == 61) return true;\n if (n % 2 == 0) return false;\n long long d = n - 1;\n while (d % 2 == 0) d /= 2;\n for (long long a : {2, 7, 61}) {\n long long t = d;\n long long y = pow_mod_constexpr(a, t, n);\n while (t != n - 1 && y != 1 && y != n - 1) {\n y = y * y % n;\n t <<= 1;\n }\n if (y != n - 1 && t % 2 == 0) {\n return false;\n }\n }\n return true;\n }\n template <int n> constexpr bool is_prime = is_prime_constexpr(n);\n \n constexpr std::pair<long long, long long> inv_gcd(long long a, long long b) {\n a = safe_mod(a, b);\n if (a == 0) return {b, 0};\n \n long long s = b, t = a;\n long long m0 = 0, m1 = 1;\n \n while (t) {\n long long u = s / t;\n s -= t * u;\n m0 -= m1 * u; // |m1 * u| <= |m1| * s <= b\n \n \n auto tmp = s;\n s = t;\n t = tmp;\n tmp = m0;\n m0 = m1;\n m1 = tmp;\n }\n if (m0 < 0) m0 += b / s;\n return {s, m0};\n }\n \n constexpr int primitive_root_constexpr(int m) {\n if (m == 2) return 1;\n if (m == 167772161) return 3;\n if (m == 469762049) return 3;\n if (m == 754974721) return 11;\n if (m == 998244353) return 3;\n int divs[20] = {};\n divs[0] = 2;\n int cnt = 1;\n int x = (m - 1) / 2;\n while (x % 2 == 0) x /= 2;\n for (int i = 3; (long long)(i)*i <= x; i += 2) {\n if (x % i == 0) {\n divs[cnt++] = i;\n while (x % i == 0) {\n x /= i;\n }\n }\n }\n if (x > 1) {\n divs[cnt++] = x;\n }\n for (int g = 2;; g++) {\n bool ok = true;\n for (int i = 0; i < cnt; i++) {\n if (pow_mod_constexpr(g, (m - 1) / divs[i], m) == 1) {\n ok = false;\n break;\n }\n }\n if (ok) return g;\n }\n }\n template <int m> constexpr int primitive_root = primitive_root_constexpr(m);\n \n } // namespace internal\n \n} // namespace atcoder\n\n#include <cassert>\n#include <numeric>\n#include <type_traits>\n\n#ifdef _MSC_VER\n#include <intrin.h>\n#endif\n\nnamespace atcoder {\n \n namespace internal {\n \n struct modint_base {};\n struct static_modint_base : modint_base {};\n \n template <class T> using is_modint = std::is_base_of<modint_base, T>;\n template <class T> using is_modint_t = std::enable_if_t<is_modint<T>::value>;\n \n } // namespace internal\n \n template <int m, std::enable_if_t<(1 <= m)>* = nullptr>\n struct static_modint : internal::static_modint_base {\n using mint = static_modint;\n \n public:\n static constexpr int mod() { return m; }\n static mint raw(int v) {\n mint x;\n x._v = v;\n return x;\n }\n \n static_modint() : _v(0) {}\n template <class T, internal::is_signed_int_t<T>* = nullptr>\n static_modint(T v) {\n long long x = (long long)(v % (long long)(umod()));\n if (x < 0) x += umod();\n _v = (unsigned int)(x);\n }\n template <class T, internal::is_unsigned_int_t<T>* = nullptr>\n static_modint(T v) {\n _v = (unsigned int)(v % umod());\n }\n static_modint(bool v) { _v = ((unsigned int)(v) % umod()); }\n \n unsigned int val() const { return _v; }\n \n mint& operator++() {\n _v++;\n if (_v == umod()) _v = 0;\n return *this;\n }\n mint& operator--() {\n if (_v == 0) _v = umod();\n _v--;\n return *this;\n }\n mint operator++(int) {\n mint result = *this;\n ++*this;\n return result;\n }\n mint operator--(int) {\n mint result = *this;\n --*this;\n return result;\n }\n \n mint& operator+=(const mint& rhs) {\n _v += rhs._v;\n if (_v >= umod()) _v -= umod();\n return *this;\n }\n mint& operator-=(const mint& rhs) {\n _v -= rhs._v;\n if (_v >= umod()) _v += umod();\n return *this;\n }\n mint& operator*=(const mint& rhs) {\n unsigned long long z = _v;\n z *= rhs._v;\n _v = (unsigned int)(z % umod());\n return *this;\n }\n mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }\n \n mint operator+() const { return *this; }\n mint operator-() const { return mint() - *this; }\n \n mint pow(long long n) const {\n assert(0 <= n);\n mint x = *this, r = 1;\n while (n) {\n if (n & 1) r *= x;\n x *= x;\n n >>= 1;\n }\n return r;\n }\n mint inv() const {\n if (prime) {\n assert(_v);\n return pow(umod() - 2);\n } else {\n auto eg = internal::inv_gcd(_v, m);\n assert(eg.first == 1);\n return eg.second;\n }\n }\n \n friend mint operator+(const mint& lhs, const mint& rhs) {\n return mint(lhs) += rhs;\n }\n friend mint operator-(const mint& lhs, const mint& rhs) {\n return mint(lhs) -= rhs;\n }\n friend mint operator*(const mint& lhs, const mint& rhs) {\n return mint(lhs) *= rhs;\n }\n friend mint operator/(const mint& lhs, const mint& rhs) {\n return mint(lhs) /= rhs;\n }\n friend bool operator==(const mint& lhs, const mint& rhs) {\n return lhs._v == rhs._v;\n }\n friend bool operator!=(const mint& lhs, const mint& rhs) {\n return lhs._v != rhs._v;\n }\n \n private:\n unsigned int _v;\n static constexpr unsigned int umod() { return m; }\n static constexpr bool prime = internal::is_prime<m>;\n };\n \n template <int id> struct dynamic_modint : internal::modint_base {\n using mint = dynamic_modint;\n \n public:\n static int mod() { return (int)(bt.umod()); }\n static void set_mod(int m) {\n assert(1 <= m);\n bt = internal::barrett(m);\n }\n static mint raw(int v) {\n mint x;\n x._v = v;\n return x;\n }\n \n dynamic_modint() : _v(0) {}\n template <class T, internal::is_signed_int_t<T>* = nullptr>\n dynamic_modint(T v) {\n long long x = (long long)(v % (long long)(mod()));\n if (x < 0) x += mod();\n _v = (unsigned int)(x);\n }\n template <class T, internal::is_unsigned_int_t<T>* = nullptr>\n dynamic_modint(T v) {\n _v = (unsigned int)(v % mod());\n }\n dynamic_modint(bool v) { _v = ((unsigned int)(v) % mod()); }\n \n unsigned int val() const { return _v; }\n \n mint& operator++() {\n _v++;\n if (_v == umod()) _v = 0;\n return *this;\n }\n mint& operator--() {\n if (_v == 0) _v = umod();\n _v--;\n return *this;\n }\n mint operator++(int) {\n mint result = *this;\n ++*this;\n return result;\n }\n mint operator--(int) {\n mint result = *this;\n --*this;\n return result;\n }\n \n mint& operator+=(const mint& rhs) {\n _v += rhs._v;\n if (_v >= umod()) _v -= umod();\n return *this;\n }\n mint& operator-=(const mint& rhs) {\n _v += mod() - rhs._v;\n if (_v >= umod()) _v -= umod();\n return *this;\n }\n mint& operator*=(const mint& rhs) {\n _v = bt.mul(_v, rhs._v);\n return *this;\n }\n mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }\n \n mint operator+() const { return *this; }\n mint operator-() const { return mint() - *this; }\n \n mint pow(long long n) const {\n assert(0 <= n);\n mint x = *this, r = 1;\n while (n) {\n if (n & 1) r *= x;\n x *= x;\n n >>= 1;\n }\n return r;\n }\n mint inv() const {\n auto eg = internal::inv_gcd(_v, mod());\n assert(eg.first == 1);\n return eg.second;\n }\n \n friend mint operator+(const mint& lhs, const mint& rhs) {\n return mint(lhs) += rhs;\n }\n friend mint operator-(const mint& lhs, const mint& rhs) {\n return mint(lhs) -= rhs;\n }\n friend mint operator*(const mint& lhs, const mint& rhs) {\n return mint(lhs) *= rhs;\n }\n friend mint operator/(const mint& lhs, const mint& rhs) {\n return mint(lhs) /= rhs;\n }\n friend bool operator==(const mint& lhs, const mint& rhs) {\n return lhs._v == rhs._v;\n }\n friend bool operator!=(const mint& lhs, const mint& rhs) {\n return lhs._v != rhs._v;\n }\n \n private:\n unsigned int _v;\n static internal::barrett bt;\n static unsigned int umod() { return bt.umod(); }\n };\n template <int id> internal::barrett dynamic_modint<id>::bt = 998244353;\n \n using modint998244353 = static_modint<998244353>;\n using modint1000000007 = static_modint<1000000007>;\n using modint = dynamic_modint<-1>;\n \n namespace internal {\n \n template <class T>\n using is_static_modint = std::is_base_of<internal::static_modint_base, T>;\n \n template <class T>\n using is_static_modint_t = std::enable_if_t<is_static_modint<T>::value>;\n \n template <class> struct is_dynamic_modint : public std::false_type {};\n template <int id>\n struct is_dynamic_modint<dynamic_modint<id>> : public std::true_type {};\n \n template <class T>\n using is_dynamic_modint_t = std::enable_if_t<is_dynamic_modint<T>::value>;\n \n } // namespace internal\n \n} // namespace atcoder\n\nusing mint=atcoder::modint1000000007;\n\nmint dp[21][MAX][MAX][2];\nvector<int> G[MAX];\nint to[MAX][MAX],cn[MAX][MAX];\n\nconst int D=21;\n\nint main(){\n \n std::ifstream in(\"text.txt\");\n std::cin.rdbuf(in.rdbuf());\n cin.tie(0);\n ios::sync_with_stdio(false);\n \n while(1){\n int N,M,K;cin>>N>>M>>K;\n if(N==0) break;\n \n for(int i=0;i<MAX;i++) G[i].clear();\n memset(dp,0,sizeof(dp));\n memset(to,-1,sizeof(to));\n memset(cn,0,sizeof(cn));\n \n vector<pair<string,string>> S(N);\n vector<string> use={\"\"};\n for(int i=0;i<N;i++){\n cin>>S[i].fi>>S[i].se;\n use.push_back(S[i].fi);\n use.push_back(S[i].se);\n }\n sort(all(use));\n use.erase(unique(all(use)),use.end());\n \n for(int i=0;i<N;i++){\n G[lower_bound(all(use),S[i].fi)-use.begin()].push_back(lower_bound(all(use),S[i].se)-use.begin());\n }\n for(int i=1;i<si(use);i++){\n G[0].push_back(i);\n }\n \n vector<string> match;\n vector<string> T(K);\n for(int i=0;i<K;i++){\n cin>>T[i];\n for(int j=0;j<=si(T[i]);j++){\n match.push_back(T[i].substr(0,j));\n }\n }\n sort(all(match));\n match.erase(unique(all(match)),match.end());\n \n vector<pair<string,int>> X(si(match));\n for(int i=0;i<si(match);i++) X[i]=mp(match[i],i);\n sort(all(X),[](auto a,auto b){\n return si(a.fi)>si(b.fi);\n });\n \n for(int a=0;a<si(match);a++){\n for(int b=1;b<si(use);b++){\n string U=match[a]+use[b];\n for(int s=0;s<si(U);s++){\n for(int i=0;i<K;i++){\n int t=s+si(T[i]);\n if(t>si(U)||t<=si(match[a])) continue;\n if(U.substr(s,si(T[i]))==T[i]) cn[a][b]++;\n }\n }\n for(auto x:X){\n if(si(U)<si(x.fi)) continue;\n if(U.substr(si(U)-si(x.fi))==x.fi){\n to[a][b]=x.se;\n break;\n }\n }\n }\n }\n \n dp[0][0][0][0]=1;\n \n for(int i=0;i<M;i++){\n int s=i%D;\n for(int j=0;j<si(use);j++){\n for(int k=0;k<si(match);k++){\n for(int f=0;f<2;f++){\n if(dp[s][j][k][f]==0) continue;\n \n for(int t:G[j]){\n if(f+cn[k][t]>=2) continue;\n dp[(s+si(use[t]))%D][t][to[k][t]][f+cn[k][t]]+=dp[s][j][k][f];\n }\n \n dp[s][j][k][f]=0;\n }\n }\n }\n }\n \n mint ans=0;\n \n for(int j=0;j<si(use);j++){\n for(int k=0;k<si(match);k++){\n ans+=dp[M%D][j][k][1];\n }\n }\n \n cout<<ans.val()<<\"\\n\";\n }\n}", "accuracy": 1, "time_ms": 5700, "memory_kb": 66440, "score_of_the_acc": -1.1037, "final_rank": 13 }, { "submission_id": "aoj_2257_6084231", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define eps 1e-9\n#define INF 2000000000 // 2e9\n#define LLINF 2000000000000000000ll // 2e18 (llmax:9e18)\n#define all(x) (x).begin(), (x).end()\n#define sq(x) ((x) * (x))\n\n#define rep(i, x) for (int i = 0; i < (int)(x); i++)\n#define drep(i, x) for (int i = ((int)(x)-1); i >= 0; i--)\n\n#define popcount __builtin_popcount\n#define next __next\n#define prev __prev\n\n#ifndef LOCAL\n#define dmp(...) ;\n#else\n#define dmp(...) \\\n cerr << \"[ \" << #__VA_ARGS__ << \" ] : \" << dump_str(__VA_ARGS__) << endl\n#endif\n\n// ---------------- Utility ------------------\n\ntemplate <class T>\nbool chmin(T &a, const T &b) {\n if (a <= b) return false;\n a = b;\n return true;\n}\n\ntemplate <class T>\nbool chmax(T &a, const T &b) {\n if (a >= b) return false;\n a = b;\n return true;\n}\n\ntemplate <class T>\nusing MaxHeap = priority_queue<T>;\n\ntemplate <class T>\nusing MinHeap = priority_queue<T, vector<T>, greater<T>>;\n\ntemplate <class T>\nvector<T> vect(int len, T elem) {\n return vector<T>(len, elem);\n}\n\n// ----------------- Input -------------------\n\ntemplate <class T, class U>\nistream &operator>>(istream &is, pair<T, U> &p) {\n return is >> p.first >> p.second;\n}\n\ntemplate <class T>\nistream &operator>>(istream &is, vector<T> &vec) {\n for (int i = 0; i < vec.size(); i++) is >> vec[i];\n return is;\n}\n\n// ----------------- Output ------------------\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n return os << p.first << ',' << p.second;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const vector<T> &v) {\n for (const T &e : v) os << e << \" \";\n return os;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const deque<T> &d) {\n for (const T &e : d) os << e << \" \";\n return os;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const set<T> &s) {\n os << \"{ \";\n for (const T &e : s) os << e << \" \";\n return os << \"}\";\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const map<T, U> &m) {\n os << \"{ \";\n for (const auto &[key, val] : m) os << \"( \" << key << \" -> \" << val << \" ) \";\n return os << \"}\";\n}\n\ntemplate <class TupleTy, size_t... I>\nvoid dump_tuple(ostream &os, const TupleTy t, std::index_sequence<I...>) {\n (..., (os << (I == 0 ? \"\" : \",\") << std::get<I>(t)));\n}\n\ntemplate <class... Args>\nostream &operator<<(ostream &os, const tuple<Args...> &t) {\n os << \"(\";\n dump_tuple(os, t, std::make_index_sequence<sizeof...(Args)>());\n return os << \")\";\n}\n\nvoid dump_str_rec(ostringstream &) {}\n\ntemplate <class Head, class... Tail>\nvoid dump_str_rec(ostringstream &oss, Head &&head, Tail &&... tail) {\n oss << \", \" << head;\n dump_str_rec(oss, forward<Tail>(tail)...);\n}\n\ntemplate <class T, class... U>\nstring dump_str(T &&arg, U &&... args) {\n ostringstream oss;\n oss << arg;\n dump_str_rec(oss, forward<U>(args)...);\n return oss.str();\n}\n\n// --------------- Fast I/O ------------------\n\nvoid fastio() {\n cin.tie(0);\n ios::sync_with_stdio(0);\n cout << fixed << setprecision(20);\n}\n\n// ------------------ ACL --------------------\n\n\n#include <cassert>\n#include <numeric>\n#include <type_traits>\n\n#ifdef _MSC_VER\n#include <intrin.h>\n#endif\n\n\n#include <utility>\n\n#ifdef _MSC_VER\n#include <intrin.h>\n#endif\n\nnamespace atcoder {\n\nnamespace internal {\n\nconstexpr long long safe_mod(long long x, long long m) {\n x %= m;\n if (x < 0) x += m;\n return x;\n}\n\nstruct barrett {\n unsigned int _m;\n unsigned long long im;\n\n explicit barrett(unsigned int m) : _m(m), im((unsigned long long)(-1) / m + 1) {}\n\n unsigned int umod() const { return _m; }\n\n unsigned int mul(unsigned int a, unsigned int b) const {\n\n unsigned long long z = a;\n z *= b;\n#ifdef _MSC_VER\n unsigned long long x;\n _umul128(z, im, &x);\n#else\n unsigned long long x =\n (unsigned long long)(((unsigned __int128)(z)*im) >> 64);\n#endif\n unsigned int v = (unsigned int)(z - x * _m);\n if (_m <= v) v += _m;\n return v;\n }\n};\n\nconstexpr long long pow_mod_constexpr(long long x, long long n, int m) {\n if (m == 1) return 0;\n unsigned int _m = (unsigned int)(m);\n unsigned long long r = 1;\n unsigned long long y = safe_mod(x, m);\n while (n) {\n if (n & 1) r = (r * y) % _m;\n y = (y * y) % _m;\n n >>= 1;\n }\n return r;\n}\n\nconstexpr bool is_prime_constexpr(int n) {\n if (n <= 1) return false;\n if (n == 2 || n == 7 || n == 61) return true;\n if (n % 2 == 0) return false;\n long long d = n - 1;\n while (d % 2 == 0) d /= 2;\n constexpr long long bases[3] = {2, 7, 61};\n for (long long a : bases) {\n long long t = d;\n long long y = pow_mod_constexpr(a, t, n);\n while (t != n - 1 && y != 1 && y != n - 1) {\n y = y * y % n;\n t <<= 1;\n }\n if (y != n - 1 && t % 2 == 0) {\n return false;\n }\n }\n return true;\n}\ntemplate <int n> constexpr bool is_prime = is_prime_constexpr(n);\n\nconstexpr std::pair<long long, long long> inv_gcd(long long a, long long b) {\n a = safe_mod(a, b);\n if (a == 0) return {b, 0};\n\n long long s = b, t = a;\n long long m0 = 0, m1 = 1;\n\n while (t) {\n long long u = s / t;\n s -= t * u;\n m0 -= m1 * u; // |m1 * u| <= |m1| * s <= b\n\n\n auto tmp = s;\n s = t;\n t = tmp;\n tmp = m0;\n m0 = m1;\n m1 = tmp;\n }\n if (m0 < 0) m0 += b / s;\n return {s, m0};\n}\n\nconstexpr int primitive_root_constexpr(int m) {\n if (m == 2) return 1;\n if (m == 167772161) return 3;\n if (m == 469762049) return 3;\n if (m == 754974721) return 11;\n if (m == 998244353) return 3;\n int divs[20] = {};\n divs[0] = 2;\n int cnt = 1;\n int x = (m - 1) / 2;\n while (x % 2 == 0) x /= 2;\n for (int i = 3; (long long)(i)*i <= x; i += 2) {\n if (x % i == 0) {\n divs[cnt++] = i;\n while (x % i == 0) {\n x /= i;\n }\n }\n }\n if (x > 1) {\n divs[cnt++] = x;\n }\n for (int g = 2;; g++) {\n bool ok = true;\n for (int i = 0; i < cnt; i++) {\n if (pow_mod_constexpr(g, (m - 1) / divs[i], m) == 1) {\n ok = false;\n break;\n }\n }\n if (ok) return g;\n }\n}\ntemplate <int m> constexpr int primitive_root = primitive_root_constexpr(m);\n\nunsigned long long floor_sum_unsigned(unsigned long long n,\n unsigned long long m,\n unsigned long long a,\n unsigned long long b) {\n unsigned long long ans = 0;\n while (true) {\n if (a >= m) {\n ans += n * (n - 1) / 2 * (a / m);\n a %= m;\n }\n if (b >= m) {\n ans += n * (b / m);\n b %= m;\n }\n\n unsigned long long y_max = a * n + b;\n if (y_max < m) break;\n n = (unsigned long long)(y_max / m);\n b = (unsigned long long)(y_max % m);\n std::swap(m, a);\n }\n return ans;\n}\n\n} // namespace internal\n\n} // namespace atcoder\n\n\n#include <cassert>\n#include <numeric>\n#include <type_traits>\n\nnamespace atcoder {\n\nnamespace internal {\n\n#ifndef _MSC_VER\ntemplate <class T>\nusing is_signed_int128 =\n typename std::conditional<std::is_same<T, __int128_t>::value ||\n std::is_same<T, __int128>::value,\n std::true_type,\n std::false_type>::type;\n\ntemplate <class T>\nusing is_unsigned_int128 =\n typename std::conditional<std::is_same<T, __uint128_t>::value ||\n std::is_same<T, unsigned __int128>::value,\n std::true_type,\n std::false_type>::type;\n\ntemplate <class T>\nusing make_unsigned_int128 =\n typename std::conditional<std::is_same<T, __int128_t>::value,\n __uint128_t,\n unsigned __int128>;\n\ntemplate <class T>\nusing is_integral = typename std::conditional<std::is_integral<T>::value ||\n is_signed_int128<T>::value ||\n is_unsigned_int128<T>::value,\n std::true_type,\n std::false_type>::type;\n\ntemplate <class T>\nusing is_signed_int = typename std::conditional<(is_integral<T>::value &&\n std::is_signed<T>::value) ||\n is_signed_int128<T>::value,\n std::true_type,\n std::false_type>::type;\n\ntemplate <class T>\nusing is_unsigned_int =\n typename std::conditional<(is_integral<T>::value &&\n std::is_unsigned<T>::value) ||\n is_unsigned_int128<T>::value,\n std::true_type,\n std::false_type>::type;\n\ntemplate <class T>\nusing to_unsigned = typename std::conditional<\n is_signed_int128<T>::value,\n make_unsigned_int128<T>,\n typename std::conditional<std::is_signed<T>::value,\n std::make_unsigned<T>,\n std::common_type<T>>::type>::type;\n\n#else\n\ntemplate <class T> using is_integral = typename std::is_integral<T>;\n\ntemplate <class T>\nusing is_signed_int =\n typename std::conditional<is_integral<T>::value && std::is_signed<T>::value,\n std::true_type,\n std::false_type>::type;\n\ntemplate <class T>\nusing is_unsigned_int =\n typename std::conditional<is_integral<T>::value &&\n std::is_unsigned<T>::value,\n std::true_type,\n std::false_type>::type;\n\ntemplate <class T>\nusing to_unsigned = typename std::conditional<is_signed_int<T>::value,\n std::make_unsigned<T>,\n std::common_type<T>>::type;\n\n#endif\n\ntemplate <class T>\nusing is_signed_int_t = std::enable_if_t<is_signed_int<T>::value>;\n\ntemplate <class T>\nusing is_unsigned_int_t = std::enable_if_t<is_unsigned_int<T>::value>;\n\ntemplate <class T> using to_unsigned_t = typename to_unsigned<T>::type;\n\n} // namespace internal\n\n} // namespace atcoder\n\n\nnamespace atcoder {\n\nnamespace internal {\n\nstruct modint_base {};\nstruct static_modint_base : modint_base {};\n\ntemplate <class T> using is_modint = std::is_base_of<modint_base, T>;\ntemplate <class T> using is_modint_t = std::enable_if_t<is_modint<T>::value>;\n\n} // namespace internal\n\ntemplate <int m, std::enable_if_t<(1 <= m)>* = nullptr>\nstruct static_modint : internal::static_modint_base {\n using mint = static_modint;\n\n public:\n static constexpr int mod() { return m; }\n static mint raw(int v) {\n mint x;\n x._v = v;\n return x;\n }\n\n static_modint() : _v(0) {}\n template <class T, internal::is_signed_int_t<T>* = nullptr>\n static_modint(T v) {\n long long x = (long long)(v % (long long)(umod()));\n if (x < 0) x += umod();\n _v = (unsigned int)(x);\n }\n template <class T, internal::is_unsigned_int_t<T>* = nullptr>\n static_modint(T v) {\n _v = (unsigned int)(v % umod());\n }\n\n unsigned int val() const { return _v; }\n\n mint& operator++() {\n _v++;\n if (_v == umod()) _v = 0;\n return *this;\n }\n mint& operator--() {\n if (_v == 0) _v = umod();\n _v--;\n return *this;\n }\n mint operator++(int) {\n mint result = *this;\n ++*this;\n return result;\n }\n mint operator--(int) {\n mint result = *this;\n --*this;\n return result;\n }\n\n mint& operator+=(const mint& rhs) {\n _v += rhs._v;\n if (_v >= umod()) _v -= umod();\n return *this;\n }\n mint& operator-=(const mint& rhs) {\n _v -= rhs._v;\n if (_v >= umod()) _v += umod();\n return *this;\n }\n mint& operator*=(const mint& rhs) {\n unsigned long long z = _v;\n z *= rhs._v;\n _v = (unsigned int)(z % umod());\n return *this;\n }\n mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }\n\n mint operator+() const { return *this; }\n mint operator-() const { return mint() - *this; }\n\n mint pow(long long n) const {\n assert(0 <= n);\n mint x = *this, r = 1;\n while (n) {\n if (n & 1) r *= x;\n x *= x;\n n >>= 1;\n }\n return r;\n }\n mint inv() const {\n if (prime) {\n assert(_v);\n return pow(umod() - 2);\n } else {\n auto eg = internal::inv_gcd(_v, m);\n assert(eg.first == 1);\n return eg.second;\n }\n }\n\n friend mint operator+(const mint& lhs, const mint& rhs) {\n return mint(lhs) += rhs;\n }\n friend mint operator-(const mint& lhs, const mint& rhs) {\n return mint(lhs) -= rhs;\n }\n friend mint operator*(const mint& lhs, const mint& rhs) {\n return mint(lhs) *= rhs;\n }\n friend mint operator/(const mint& lhs, const mint& rhs) {\n return mint(lhs) /= rhs;\n }\n friend bool operator==(const mint& lhs, const mint& rhs) {\n return lhs._v == rhs._v;\n }\n friend bool operator!=(const mint& lhs, const mint& rhs) {\n return lhs._v != rhs._v;\n }\n\n private:\n unsigned int _v;\n static constexpr unsigned int umod() { return m; }\n static constexpr bool prime = internal::is_prime<m>;\n};\n\ntemplate <int id> struct dynamic_modint : internal::modint_base {\n using mint = dynamic_modint;\n\n public:\n static int mod() { return (int)(bt.umod()); }\n static void set_mod(int m) {\n assert(1 <= m);\n bt = internal::barrett(m);\n }\n static mint raw(int v) {\n mint x;\n x._v = v;\n return x;\n }\n\n dynamic_modint() : _v(0) {}\n template <class T, internal::is_signed_int_t<T>* = nullptr>\n dynamic_modint(T v) {\n long long x = (long long)(v % (long long)(mod()));\n if (x < 0) x += mod();\n _v = (unsigned int)(x);\n }\n template <class T, internal::is_unsigned_int_t<T>* = nullptr>\n dynamic_modint(T v) {\n _v = (unsigned int)(v % mod());\n }\n\n unsigned int val() const { return _v; }\n\n mint& operator++() {\n _v++;\n if (_v == umod()) _v = 0;\n return *this;\n }\n mint& operator--() {\n if (_v == 0) _v = umod();\n _v--;\n return *this;\n }\n mint operator++(int) {\n mint result = *this;\n ++*this;\n return result;\n }\n mint operator--(int) {\n mint result = *this;\n --*this;\n return result;\n }\n\n mint& operator+=(const mint& rhs) {\n _v += rhs._v;\n if (_v >= umod()) _v -= umod();\n return *this;\n }\n mint& operator-=(const mint& rhs) {\n _v += mod() - rhs._v;\n if (_v >= umod()) _v -= umod();\n return *this;\n }\n mint& operator*=(const mint& rhs) {\n _v = bt.mul(_v, rhs._v);\n return *this;\n }\n mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }\n\n mint operator+() const { return *this; }\n mint operator-() const { return mint() - *this; }\n\n mint pow(long long n) const {\n assert(0 <= n);\n mint x = *this, r = 1;\n while (n) {\n if (n & 1) r *= x;\n x *= x;\n n >>= 1;\n }\n return r;\n }\n mint inv() const {\n auto eg = internal::inv_gcd(_v, mod());\n assert(eg.first == 1);\n return eg.second;\n }\n\n friend mint operator+(const mint& lhs, const mint& rhs) {\n return mint(lhs) += rhs;\n }\n friend mint operator-(const mint& lhs, const mint& rhs) {\n return mint(lhs) -= rhs;\n }\n friend mint operator*(const mint& lhs, const mint& rhs) {\n return mint(lhs) *= rhs;\n }\n friend mint operator/(const mint& lhs, const mint& rhs) {\n return mint(lhs) /= rhs;\n }\n friend bool operator==(const mint& lhs, const mint& rhs) {\n return lhs._v == rhs._v;\n }\n friend bool operator!=(const mint& lhs, const mint& rhs) {\n return lhs._v != rhs._v;\n }\n\n private:\n unsigned int _v;\n static internal::barrett bt;\n static unsigned int umod() { return bt.umod(); }\n};\ntemplate <int id> internal::barrett dynamic_modint<id>::bt(998244353);\n\nusing modint998244353 = static_modint<998244353>;\nusing modint1000000007 = static_modint<1000000007>;\nusing modint = dynamic_modint<-1>;\n\nnamespace internal {\n\ntemplate <class T>\nusing is_static_modint = std::is_base_of<internal::static_modint_base, T>;\n\ntemplate <class T>\nusing is_static_modint_t = std::enable_if_t<is_static_modint<T>::value>;\n\ntemplate <class> struct is_dynamic_modint : public std::false_type {};\ntemplate <int id>\nstruct is_dynamic_modint<dynamic_modint<id>> : public std::true_type {};\n\ntemplate <class T>\nusing is_dynamic_modint_t = std::enable_if_t<is_dynamic_modint<T>::value>;\n\n} // namespace internal\n\n} // namespace atcoder\n\n// constexpr int MOD = 998244353;\nconstexpr int MOD = 1000000007;\nusing mint = atcoder::static_modint<MOD>;\nostream &operator<<(ostream &os, const mint &v) {\n return os << v.val();\n}\n\n// ------------ End of template --------------\n\n#define endl \"\\n\"\nusing ll = long long;\nusing pii = pair<int, int>;\n\nconst bool multitest = false;\n\nstruct AhoCorasick {\n const static int alphabet_size = 26;\n const int root = 0;\n const char base = 'a';\n\n AhoCorasick() : T(1) {}\n AhoCorasick(const vector<string> &ss) : T(1) {\n for (const string &s : ss) add_string(s);\n build();\n }\n\n struct Node {\n vector<int> child; // children of this node in Trie\n bool leaf;\n int par; // parent\n char par_c; // character on the edge from parent to this node\n int suf_link; // suffix link\n int exit_link; // exit link : direct suffix link to the nearest match\n int num_leaf; //\n vector<int> go; // transition of the automaton\n int match_count; // the number of matches of this state\n Node(int par = -1, char par_c = '$')\n : leaf(false), par(par), par_c(par_c), suf_link(-1), exit_link(-1),\n num_leaf(-1), match_count(-1) {\n child.assign(alphabet_size, -1);\n go.assign(alphabet_size, -1);\n }\n };\n\n vector<Node> T;\n\n int add_string(const string &s) {\n int v = root;\n for (char ch : s) {\n int c = ch - base;\n if (T[v].child[c] == -1) {\n T[v].child[c] = T.size();\n T.emplace_back(v, ch);\n }\n v = T[v].child[c];\n }\n T[v].leaf = true;\n return v;\n }\n\n int get_num_state() const { return T.size(); }\n\n int suf_link(int v) {\n if (T[v].suf_link == -1) {\n if (v == root || T[v].par == root) {\n T[v].suf_link = root;\n } else {\n int par_suf_link = suf_link(T[v].par);\n T[v].suf_link = go(par_suf_link, T[v].par_c);\n }\n }\n return T[v].suf_link;\n }\n\n int go(int v, char ch) {\n int c = ch - base;\n if (T[v].go[c] == -1) {\n if (T[v].child[c] != -1)\n T[v].go[c] = T[v].child[c];\n else\n T[v].go[c] = (v == root) ? root : go(suf_link(v), ch);\n }\n return T[v].go[c];\n }\n\n int match_count(int v) {\n if (T[v].match_count == -1) {\n if (v == root)\n T[v].match_count = 0;\n else {\n T[v].match_count = match_count(suf_link(v)) + (T[v].leaf ? 1 : 0);\n }\n }\n return T[v].match_count;\n }\n\n int exit_link(int v) {\n if (T[v].exit_link == -1) {\n if (v == root)\n T[v].exit_link = 0;\n else {\n int suf = suf_link(v);\n T[v].exit_link = T[suf].leaf ? suf : exit_link(suf);\n }\n }\n return T[v].exit_link;\n }\n\n int num_leaf(int v) {\n if (T[v].num_leaf == -1) {\n if (exit_link(v) == 0) {\n T[v].num_leaf = 0;\n } else {\n T[v].num_leaf = num_leaf(exit_link(v));\n }\n if (T[v].leaf) T[v].num_leaf++;\n }\n return T[v].num_leaf;\n }\n\n void build() {\n for (int v = 0; v < T.size(); v++) {\n suf_link(v);\n match_count(v);\n exit_link(v);\n num_leaf(v);\n for (int i = 0; i < alphabet_size; i++) go(v, base + i);\n }\n }\n\n Node &operator[](int v) { return T[v]; }\n int size() const { return T.size(); }\n};\n\n// 文字数、acの頂点、最後のword、季語の個数\n// auto dp = vect(L, vect(S, vect(W, vect(2, mint(0)))));\nmint dp[22][601][501][2];\n\nbool solve() {\n int N, M, K;\n cin >> N >> M >> K;\n\n if ((N | M | K) == 0) return false;\n\n vector<string> zip;\n vector<string> a(N), b(N);\n for (int i = 0; i < N; i++) {\n cin >> a[i] >> b[i];\n zip.push_back(a[i]);\n zip.push_back(b[i]);\n }\n sort(all(zip));\n zip.erase(unique(all(zip)), zip.end());\n\n vector<vector<int>> g(zip.size());\n for (int i = 0; i < N; i++) {\n int u = lower_bound(all(zip), a[i]) - zip.begin();\n int v = lower_bound(all(zip), b[i]) - zip.begin();\n g[u].push_back(v);\n }\n\n vector<string> sw(K);\n for (int i = 0; i < K; i++) { cin >> sw[i]; }\n\n AhoCorasick ac(sw);\n\n int W = zip.size();\n int S = ac.get_num_state();\n\n auto go = [&](int v, int w) {\n int num_leaf = 0;\n for (char c : zip[w]) {\n v = ac.go(v, c);\n num_leaf += ac.num_leaf(v);\n }\n return make_pair(v, num_leaf);\n };\n\n const int L = 22;\n\n assert(S <= 600 && W <= 500);\n for (int i = 0; i < L; i++) {\n for (int j = 0; j < S; j++) {\n for (int k = 0; k < W; k++) {\n for (int l = 0; l < 2; l++) { dp[i][j][k][l] = mint(0); }\n }\n }\n }\n\n for (int i = 0; i < W; i++) {\n auto [v, num_leaf] = go(0, i);\n if (zip[i].size() > M) continue;\n if (num_leaf > 1) continue;\n dp[zip[i].size()][v][i][num_leaf] = mint(1);\n }\n\n for (int i = 0; i < M; i++) {\n for (int j = 0; j < S; j++) {\n for (int k = 0; k < W; k++) {\n for (int l = 0; l < 2; l++) {\n if (dp[i % L][j][k][l].val() == 0) continue;\n for (int u : g[k]) {\n if (i + zip[u].size() > M) continue;\n auto [v, num_leaf] = go(j, u);\n if (l + num_leaf > 1) continue;\n dp[(i + zip[u].size()) % L][v][u][l + num_leaf] +=\n dp[i % L][j][k][l];\n }\n }\n }\n }\n for (int j = 0; j < S; j++) {\n for (int k = 0; k < W; k++) {\n for (int l = 0; l < 2; l++) { dp[(i + L - 1) % L][j][k][l] = mint(0); }\n }\n }\n }\n\n mint ans(0);\n for (int j = 0; j < S; j++) {\n for (int k = 0; k < W; k++) { ans += dp[M % L][j][k][1]; }\n }\n\n cout << ans << endl;\n\n return true;\n}\n\nint main() {\n fastio();\n if (!multitest) {\n while (solve()) {}\n } else {\n cerr << \"[Warning] Multi testcase mode on\" << endl;\n int t;\n cin >> t;\n while (t--) solve();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1740, "memory_kb": 55360, "score_of_the_acc": -0.4498, "final_rank": 2 }, { "submission_id": "aoj_2257_6023958", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\ntemplate <uint64_t Modulus> class modint {\n using i64 = int64_t;\n using u32 = uint32_t;\n using u64 = uint64_t;\n\n static_assert(Modulus < static_cast<uint32_t>(1) << 31, \"Modulus must be less than 2**31\");\n static constexpr u32 mod = Modulus;\n u32 v;\n\npublic:\n constexpr modint(const i64 x = 0) noexcept : v(x < 0 ? mod - 1 - (-(x + 1) % mod) : x % mod) {}\n constexpr u32& val() noexcept { return v; }\n constexpr const u32& val() const noexcept { return v; }\n constexpr modint operator+(const modint& rhs) const noexcept { return modint(*this) += rhs; }\n constexpr modint operator-(const modint& rhs) const noexcept { return modint(*this) -= rhs; }\n constexpr modint operator*(const modint& rhs) const noexcept { return modint(*this) *= rhs; }\n constexpr modint operator/(const modint& rhs) const noexcept { return modint(*this) /= rhs; }\n constexpr modint& operator+=(const modint& rhs) noexcept {\n v += rhs.v;\n if (v >= mod) v -= mod;\n return *this;\n }\n constexpr modint& operator-=(const modint& rhs) noexcept {\n if (v < rhs.v) v += mod;\n v -= rhs.v;\n return *this;\n }\n constexpr modint& operator*=(const modint& rhs) noexcept {\n v = (u64)v * rhs.v % mod;\n return *this;\n }\n constexpr modint& operator/=(const modint& rhs) noexcept { return *this *= rhs.inv(); }\n constexpr modint pow(long long n) const noexcept {\n assert(0 <= n);\n modint self(*this), res(1);\n while (n > 0) {\n if (n & 1) res *= self;\n self *= self;\n n >>= 1;\n }\n return res;\n }\n constexpr modint inv() const noexcept {\n assert(*this != 0);\n return pow(mod - 2);\n }\n constexpr modint& operator++() noexcept {\n if (++v == mod) v = 0;\n return *this;\n }\n constexpr modint& operator--() noexcept {\n if (v == 0) v = mod;\n return --v, *this;\n }\n constexpr modint operator++(int) noexcept {\n modint t = *this;\n return ++*this, t;\n }\n constexpr modint operator--(int) noexcept {\n modint t = *this;\n return --*this, t;\n }\n constexpr modint operator-() const noexcept { return modint(mod - v); }\n template <class T> friend constexpr modint operator+(T x, modint y) noexcept { return modint(x) + y; }\n template <class T> friend constexpr modint operator-(T x, modint y) noexcept { return modint(x) - y; }\n template <class T> friend constexpr modint operator*(T x, modint y) noexcept { return modint(x) * y; }\n template <class T> friend constexpr modint operator/(T x, modint y) noexcept { return modint(x) / y; }\n constexpr bool operator==(const modint& rhs) const noexcept { return v == rhs.v; }\n constexpr bool operator!=(const modint& rhs) const noexcept { return v != rhs.v; }\n constexpr bool operator!() const noexcept { return !v; }\n friend std::istream& operator>>(std::istream& s, modint& rhs) noexcept {\n i64 v;\n rhs = modint{(s >> v, v)};\n return s;\n }\n friend std::ostream& operator<<(std::ostream& s, const modint& rhs) noexcept { return s << rhs.v; }\n};\n\n/**\n * @brief modint\n * @docs docs/modulo/modint.md\n */\n\ntemplate <size_t char_size, char margin = 'a'> struct Trie {\n struct Node {\n std::array<int, char_size> nxt;\n std::vector<int> idxs;\n int idx, sub;\n char key;\n Node(char c) : idx(-1), key(c) { fill(nxt.begin(), nxt.end(), -1); }\n };\n\n std::vector<Node> nodes;\n\n inline int& next(int i, int j) { return nodes[i].nxt[j]; }\n\n Trie() { nodes.emplace_back('$'); }\n\n void add(const std::string& s, int x = 0) {\n int cur = 0;\n for (const char& c : s) {\n int k = c - margin;\n if (next(cur, k) < 0) {\n next(cur, k) = nodes.size();\n nodes.emplace_back(c);\n }\n cur = next(cur, k);\n nodes[cur].sub++;\n }\n nodes[cur].idx = x;\n nodes[cur].idxs.emplace_back(x);\n }\n\n int find(const std::string& s) {\n int cur = 0;\n for (const char& c : s) {\n int k = c - margin;\n if (next(cur, k) < 0) return -1;\n cur = next(cur, k);\n }\n return cur;\n }\n\n int move(int pos, char c) {\n assert(pos < (int)nodes.size());\n return pos < 0 ? -1 : next(pos, c - margin);\n }\n\n int size() const { return nodes.size(); }\n\n int idx(int pos) { return pos < 0 ? -1 : nodes[pos].idx; }\n\n std::vector<int> idxs(int pos) { return pos < 0 ? std::vector<int>() : nodes[pos].idxs; }\n};\n\n/**\n * @brief Trie\n * @docs docs/string/Trie.md\n */\n\ntemplate <size_t char_size, char margin = 'a'> struct AhoCorasick : Trie<char_size + 1, margin> {\n void build(bool heavy = true) {\n int n = nodes.size();\n cnt.resize(n);\n for (int i = 0; i < n; i++) cnt[i] = nodes[i].idxs.size();\n std::queue<int> que;\n for (size_t i = 0; i <= char_size; i++) {\n if (~next(0, i)) {\n next(next(0, i), FAIL) = 0;\n que.emplace(next(0, i));\n } else\n next(0, i) = 0;\n }\n while (!que.empty()) {\n auto& cur = nodes[que.front()];\n int fail = cur.nxt[FAIL];\n cnt[que.front()] += cnt[fail];\n que.pop();\n for (size_t i = 0; i < char_size; i++) {\n int& nxt = cur.nxt[i];\n if (nxt < 0) {\n nxt = next(fail, i);\n continue;\n }\n next(nxt, FAIL) = next(fail, i);\n if (heavy) {\n auto& u = nodes[nxt].idxs;\n auto& v = nodes[next(fail, i)].idxs;\n std::vector<int> w;\n set_union(u.begin(), u.end(), v.begin(), v.end(), back_inserter(w));\n u = w;\n }\n que.emplace(nxt);\n }\n }\n }\n\n long long match(const std::string& s) {\n long long res = 0;\n int cur = 0;\n for (const char& c : s) {\n cur = next(cur, c - margin);\n res += cnt[cur];\n }\n return res;\n }\n\n std::map<int, int> frequency(const std::string& s) {\n std::map<int, int> res;\n int cur = 0;\n for (const char& c : s) {\n cur = next(cur, c - margin);\n for (auto& idx : nodes[cur].idxs) res[idx]++;\n }\n return res;\n }\n\n int count(int pos) { return cnt[pos]; }\n\nprivate:\n using super = Trie<char_size + 1, margin>;\n using super::next;\n using super::nodes;\n\n const int FAIL = char_size;\n std::vector<int> cnt;\n};\n\n/**\n * @brief Aho Corasick\n * @docs docs/string/AhoCorasick.md\n */\n\nconst long long MOD = 1000000007;\n\nusing mint = modint<MOD>;\nconst int MAX_N = 256, MAX_M = 512, MAX_K = 32, MAX_L = 20;\nmint dp[MAX_L][MAX_K * MAX_L][MAX_N * 2][2], ndp[MAX_L][MAX_K * MAX_L][MAX_N * 2][2];\n\nbool solve() {\n int N, M, K;\n cin >> N >> M >> K;\n if (N == 0 and M == 0 and K == 0) return false;\n vector<string> from(N), to(N), seasonword(K);\n for (int i = 0; i < N; i++) cin >> from[i] >> to[i];\n for (int i = 0; i < K; i++) cin >> seasonword[i];\n\n vector<string> S;\n for (auto& s : from) S.emplace_back(s);\n for (auto& s : to) S.emplace_back(s);\n sort(S.begin(), S.end());\n S.erase(unique(S.begin(), S.end()), S.end());\n map<string, int> mp;\n int m = S.size();\n for (int i = 0; i < m; i++) mp[S[i]] = i;\n vector<vector<int>> G(m);\n for (int i = 0; i < N; i++) G[mp[from[i]]].emplace_back(mp[to[i]]);\n\n AhoCorasick<26> AC;\n for (auto& s : seasonword) AC.add(s);\n AC.build(false);\n int n = AC.size();\n\n vector<vector<pair<int, int>>> nxt(n, vector<pair<int, int>>(m));\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < m; j++) {\n int cur = i, sum = 0;\n for (char& c : S[j]) {\n cur = AC.move(cur, c);\n sum += AC.count(cur);\n }\n nxt[i][j] = {cur, sum};\n }\n }\n\n for (int i = 0; i < MAX_L; i++) {\n for (int j = 0; j < n; j++) {\n for (int k = 0; k < m; k++) {\n for (int l = 0; l < 2; l++) {\n dp[i][j][k][l] = ndp[i][j][k][l] = 0;\n }\n }\n }\n }\n dp[0][0][0][0] = 1;\n\n int cur = 0;\n for (int i = 0; i < M; i++) {\n for (int j = 0; j < n; j++) {\n for (int k = 0; k < m; k++) {\n for (int l = 0; l < 2; l++) {\n mint& val = dp[cur][j][k][l];\n if (val == 0) continue;\n if (i == 0) {\n for (int nk = 0; nk < m; nk++) {\n int ni = cur + int(S[nk].size()), nj = nxt[j][nk].first, nl = l + nxt[j][nk].second;\n if (nl > 1) continue;\n if (ni < MAX_L)\n dp[ni][nj][nk][nl] += val;\n else\n ndp[ni - MAX_L][nj][nk][nl] += val;\n }\n } else {\n for (int nk : G[k]) {\n int ni = cur + int(S[nk].size()), nj = nxt[j][nk].first, nl = l + nxt[j][nk].second;\n if (nl > 1) continue;\n if (ni < MAX_L)\n dp[ni][nj][nk][nl] += val;\n else\n ndp[ni - MAX_L][nj][nk][nl] += val;\n }\n }\n val = 0;\n }\n }\n }\n if (++cur == MAX_L) {\n swap(dp, ndp);\n cur = 0;\n }\n }\n\n mint ans = 0;\n for (int j = 0; j < n; j++) {\n for (int k = 0; k < m; k++) {\n ans += dp[cur][j][k][1];\n }\n }\n cout << ans << '\\n';\n return true;\n}\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n while (solve())\n ;\n return 0;\n}", "accuracy": 1, "time_ms": 3420, "memory_kb": 107992, "score_of_the_acc": -0.9721, "final_rank": 8 }, { "submission_id": "aoj_2257_6023957", "code_snippet": "#define LOCAL\n#include <bits/stdc++.h>\nusing namespace std;\n#pragma region Macros\ntypedef long long ll;\ntypedef __int128_t i128;\ntypedef unsigned int uint;\ntypedef unsigned long long ull;\n#define ALL(x) (x).begin(), (x).end()\n\ntemplate <typename T> istream& operator>>(istream& is, vector<T>& v) {\n for (T& x : v) is >> x;\n return is;\n}\ntemplate <typename T> ostream& operator<<(ostream& os, const vector<T>& v) {\n for (size_t i = 0; i < v.size(); i++) {\n os << v[i] << (i + 1 == v.size() ? \"\" : \" \");\n }\n return os;\n}\ntemplate <typename T, typename U> ostream& operator<<(ostream& os, const pair<T, U>& p) {\n os << '(' << p.first << ',' << p.second << ')';\n return os;\n}\ntemplate <typename T, typename U> ostream& operator<<(ostream& os, const map<T, U>& m) {\n os << '{';\n for (auto itr = m.begin(); itr != m.end();) {\n os << '(' << itr->first << ',' << itr->second << ')';\n if (++itr != m.end()) os << ',';\n }\n os << '}';\n return os;\n}\ntemplate <typename T, typename U> ostream& operator<<(ostream& os, const unordered_map<T, U>& m) {\n os << '{';\n for (auto itr = m.begin(); itr != m.end();) {\n os << '(' << itr->first << ',' << itr->second << ')';\n if (++itr != m.end()) os << ',';\n }\n os << '}';\n return os;\n}\ntemplate <typename T> ostream& operator<<(ostream& os, const set<T>& s) {\n os << '{';\n for (auto itr = s.begin(); itr != s.end();) {\n os << *itr;\n if (++itr != s.end()) os << ',';\n }\n os << '}';\n return os;\n}\ntemplate <typename T> ostream& operator<<(ostream& os, const multiset<T>& s) {\n os << '{';\n for (auto itr = s.begin(); itr != s.end();) {\n os << *itr;\n if (++itr != s.end()) os << ',';\n }\n os << '}';\n return os;\n}\ntemplate <typename T> ostream& operator<<(ostream& os, const unordered_set<T>& s) {\n os << '{';\n for (auto itr = s.begin(); itr != s.end();) {\n os << *itr;\n if (++itr != s.end()) os << ',';\n }\n os << '}';\n return os;\n}\ntemplate <typename T> ostream& operator<<(ostream& os, const deque<T>& v) {\n for (size_t i = 0; i < v.size(); i++) {\n os << v[i] << (i + 1 == v.size() ? \"\" : \" \");\n }\n return os;\n}\ntemplate <typename T, size_t N> ostream& operator<<(ostream& os, const array<T, N>& v) {\n for (size_t i = 0; i < N; i++) {\n os << v[i] << (i + 1 == N ? \"\" : \" \");\n }\n return os;\n}\n\ntemplate <int i, typename T> void print_tuple(ostream&, const T&) {}\ntemplate <int i, typename T, typename H, class... Args> void print_tuple(ostream& os, const T& t) {\n if (i) os << ',';\n os << get<i>(t);\n print_tuple<i + 1, T, Args...>(os, t);\n}\ntemplate <typename... Args> ostream& operator<<(ostream& os, const tuple<Args...>& t) {\n os << '{';\n print_tuple<0, tuple<Args...>, Args...>(os, t);\n return os << '}';\n}\n\nvoid debug_out() { cerr << '\\n'; }\ntemplate <class Head, class... Tail> void debug_out(Head&& head, Tail&&... tail) {\n cerr << head;\n if (sizeof...(Tail) > 0) cerr << \", \";\n debug_out(move(tail)...);\n}\n#ifdef LOCAL\n#define debug(...) \\\n cerr << \" \"; \\\n cerr << #__VA_ARGS__ << \" :[\" << __LINE__ << \":\" << __FUNCTION__ << \"]\" << '\\n'; \\\n cerr << \" \"; \\\n debug_out(__VA_ARGS__)\n#else\n#define debug(...) void(0)\n#endif\n\ntemplate <typename T> T gcd(T x, T y) { return y != 0 ? gcd(y, x % y) : x; }\ntemplate <typename T> T lcm(T x, T y) { return x / gcd(x, y) * y; }\n\nint topbit(signed t) { return t == 0 ? -1 : 31 - __builtin_clz(t); }\nint topbit(long long t) { return t == 0 ? -1 : 63 - __builtin_clzll(t); }\nint botbit(signed a) { return a == 0 ? 32 : __builtin_ctz(a); }\nint botbit(long long a) { return a == 0 ? 64 : __builtin_ctzll(a); }\nint popcount(signed t) { return __builtin_popcount(t); }\nint popcount(long long t) { return __builtin_popcountll(t); }\nbool ispow2(int i) { return i && (i & -i) == i; }\nlong long MSK(int n) { return (1LL << n) - 1; }\n\ntemplate <class T> T ceil(T x, T y) {\n assert(y >= 1);\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <class T> T floor(T x, T y) {\n assert(y >= 1);\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\n\ntemplate <class T1, class T2> inline bool chmin(T1& a, T2 b) {\n if (a > b) {\n a = b;\n return true;\n }\n return false;\n}\ntemplate <class T1, class T2> inline bool chmax(T1& a, T2 b) {\n if (a < b) {\n a = b;\n return true;\n }\n return false;\n}\n\ntemplate <typename T> void mkuni(vector<T>& v) {\n sort(v.begin(), v.end());\n v.erase(unique(v.begin(), v.end()), v.end());\n}\ntemplate <typename T> int lwb(const vector<T>& v, const T& x) { return lower_bound(v.begin(), v.end(), x) - v.begin(); }\n#pragma endregion\n\n#include <cassert>\n#include <cstdint>\n#include <iostream>\n\ntemplate <uint64_t Modulus> class modint {\n using i64 = int64_t;\n using u32 = uint32_t;\n using u64 = uint64_t;\n\n static_assert(Modulus < static_cast<uint32_t>(1) << 31, \"Modulus must be less than 2**31\");\n static constexpr u32 mod = Modulus;\n u32 v;\n\npublic:\n constexpr modint(const i64 x = 0) noexcept : v(x < 0 ? mod - 1 - (-(x + 1) % mod) : x % mod) {}\n constexpr u32& val() noexcept { return v; }\n constexpr const u32& val() const noexcept { return v; }\n constexpr modint operator+(const modint& rhs) const noexcept { return modint(*this) += rhs; }\n constexpr modint operator-(const modint& rhs) const noexcept { return modint(*this) -= rhs; }\n constexpr modint operator*(const modint& rhs) const noexcept { return modint(*this) *= rhs; }\n constexpr modint operator/(const modint& rhs) const noexcept { return modint(*this) /= rhs; }\n constexpr modint& operator+=(const modint& rhs) noexcept {\n v += rhs.v;\n if (v >= mod) v -= mod;\n return *this;\n }\n constexpr modint& operator-=(const modint& rhs) noexcept {\n if (v < rhs.v) v += mod;\n v -= rhs.v;\n return *this;\n }\n constexpr modint& operator*=(const modint& rhs) noexcept {\n v = (u64)v * rhs.v % mod;\n return *this;\n }\n constexpr modint& operator/=(const modint& rhs) noexcept { return *this *= rhs.inv(); }\n constexpr modint pow(long long n) const noexcept {\n assert(0 <= n);\n modint self(*this), res(1);\n while (n > 0) {\n if (n & 1) res *= self;\n self *= self;\n n >>= 1;\n }\n return res;\n }\n constexpr modint inv() const noexcept {\n assert(*this != 0);\n return pow(mod - 2);\n }\n constexpr modint& operator++() noexcept {\n if (++v == mod) v = 0;\n return *this;\n }\n constexpr modint& operator--() noexcept {\n if (v == 0) v = mod;\n return --v, *this;\n }\n constexpr modint operator++(int) noexcept {\n modint t = *this;\n return ++*this, t;\n }\n constexpr modint operator--(int) noexcept {\n modint t = *this;\n return --*this, t;\n }\n constexpr modint operator-() const noexcept { return modint(mod - v); }\n template <class T> friend constexpr modint operator+(T x, modint y) noexcept { return modint(x) + y; }\n template <class T> friend constexpr modint operator-(T x, modint y) noexcept { return modint(x) - y; }\n template <class T> friend constexpr modint operator*(T x, modint y) noexcept { return modint(x) * y; }\n template <class T> friend constexpr modint operator/(T x, modint y) noexcept { return modint(x) / y; }\n constexpr bool operator==(const modint& rhs) const noexcept { return v == rhs.v; }\n constexpr bool operator!=(const modint& rhs) const noexcept { return v != rhs.v; }\n constexpr bool operator!() const noexcept { return !v; }\n friend std::istream& operator>>(std::istream& s, modint& rhs) noexcept {\n i64 v;\n rhs = modint{(s >> v, v)};\n return s;\n }\n friend std::ostream& operator<<(std::ostream& s, const modint& rhs) noexcept { return s << rhs.v; }\n};\n\n/**\n * @brief modint\n * @docs docs/modulo/modint.md\n */\n\n#include <array>\n#include <cassert>\n#include <string>\n#include <vector>\n\ntemplate <size_t char_size, char margin = 'a'> struct Trie {\n struct Node {\n std::array<int, char_size> nxt;\n std::vector<int> idxs;\n int idx, sub;\n char key;\n Node(char c) : idx(-1), key(c) { fill(nxt.begin(), nxt.end(), -1); }\n };\n\n std::vector<Node> nodes;\n\n inline int& next(int i, int j) { return nodes[i].nxt[j]; }\n\n Trie() { nodes.emplace_back('$'); }\n\n void add(const std::string& s, int x = 0) {\n int cur = 0;\n for (const char& c : s) {\n int k = c - margin;\n if (next(cur, k) < 0) {\n next(cur, k) = nodes.size();\n nodes.emplace_back(c);\n }\n cur = next(cur, k);\n nodes[cur].sub++;\n }\n nodes[cur].idx = x;\n nodes[cur].idxs.emplace_back(x);\n }\n\n int find(const std::string& s) {\n int cur = 0;\n for (const char& c : s) {\n int k = c - margin;\n if (next(cur, k) < 0) return -1;\n cur = next(cur, k);\n }\n return cur;\n }\n\n int move(int pos, char c) {\n assert(pos < (int)nodes.size());\n return pos < 0 ? -1 : next(pos, c - margin);\n }\n\n int size() const { return nodes.size(); }\n\n int idx(int pos) { return pos < 0 ? -1 : nodes[pos].idx; }\n\n std::vector<int> idxs(int pos) { return pos < 0 ? std::vector<int>() : nodes[pos].idxs; }\n};\n\n/**\n * @brief Trie\n * @docs docs/string/Trie.md\n */\n\n#include <map>\n#include <queue>\n\ntemplate <size_t char_size, char margin = 'a'> struct AhoCorasick : Trie<char_size + 1, margin> {\n void build(bool heavy = true) {\n int n = nodes.size();\n cnt.resize(n);\n for (int i = 0; i < n; i++) cnt[i] = nodes[i].idxs.size();\n std::queue<int> que;\n for (size_t i = 0; i <= char_size; i++) {\n if (~next(0, i)) {\n next(next(0, i), FAIL) = 0;\n que.emplace(next(0, i));\n } else\n next(0, i) = 0;\n }\n while (!que.empty()) {\n auto& cur = nodes[que.front()];\n int fail = cur.nxt[FAIL];\n cnt[que.front()] += cnt[fail];\n que.pop();\n for (size_t i = 0; i < char_size; i++) {\n int& nxt = cur.nxt[i];\n if (nxt < 0) {\n nxt = next(fail, i);\n continue;\n }\n next(nxt, FAIL) = next(fail, i);\n if (heavy) {\n auto& u = nodes[nxt].idxs;\n auto& v = nodes[next(fail, i)].idxs;\n std::vector<int> w;\n set_union(u.begin(), u.end(), v.begin(), v.end(), back_inserter(w));\n u = w;\n }\n que.emplace(nxt);\n }\n }\n }\n\n long long match(const std::string& s) {\n long long res = 0;\n int cur = 0;\n for (const char& c : s) {\n cur = next(cur, c - margin);\n res += cnt[cur];\n }\n return res;\n }\n\n std::map<int, int> frequency(const std::string& s) {\n std::map<int, int> res;\n int cur = 0;\n for (const char& c : s) {\n cur = next(cur, c - margin);\n for (auto& idx : nodes[cur].idxs) res[idx]++;\n }\n return res;\n }\n\n int count(int pos) { return cnt[pos]; }\n\nprivate:\n using super = Trie<char_size + 1, margin>;\n using super::next;\n using super::nodes;\n\n const int FAIL = char_size;\n std::vector<int> cnt;\n};\n\n/**\n * @brief Aho Corasick\n * @docs docs/string/AhoCorasick.md\n */\n\nconst int INF = 1e9;\nconst long long IINF = 1e18;\nconst int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};\nconst char dir[4] = {'D', 'R', 'U', 'L'};\nconst long long MOD = 1000000007;\n// const long long MOD = 998244353;\n\nusing mint = modint<MOD>;\nconst int MAX_N = 256, MAX_M = 512, MAX_K = 32, MAX_L = 20;\nmint dp[MAX_L][MAX_K * MAX_L][MAX_N * 2]\n [2], // i 文字できていて現在の状態が j で直前に追加した単語が k で季語の個数が l\n ndp[MAX_L][MAX_K * MAX_L][MAX_N * 2][2];\n\nbool solve() {\n int N, M, K;\n cin >> N >> M >> K;\n if (N == 0 and M == 0 and K == 0) return false;\n vector<string> from(N), to(N), seasonword(K);\n for (int i = 0; i < N; i++) cin >> from[i] >> to[i];\n for (int i = 0; i < K; i++) cin >> seasonword[i];\n\n vector<string> S;\n for (auto& s : from) S.emplace_back(s);\n for (auto& s : to) S.emplace_back(s);\n sort(S.begin(), S.end());\n S.erase(unique(S.begin(), S.end()), S.end());\n map<string, int> mp;\n int m = S.size();\n for (int i = 0; i < m; i++) mp[S[i]] = i;\n vector<vector<int>> G(m);\n for (int i = 0; i < N; i++) G[mp[from[i]]].emplace_back(mp[to[i]]);\n\n AhoCorasick<26> AC;\n for (auto& s : seasonword) AC.add(s);\n AC.build(false);\n int n = AC.size();\n\n vector<vector<pair<int, int>>> nxt(n, vector<pair<int, int>>(m));\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < m; j++) {\n int cur = i, sum = 0;\n for (char& c : S[j]) {\n cur = AC.move(cur, c);\n sum += AC.count(cur);\n }\n nxt[i][j] = {cur, sum};\n }\n }\n\n for (int i = 0; i < MAX_L; i++) {\n for (int j = 0; j < n; j++) {\n for (int k = 0; k < m; k++) {\n for (int l = 0; l < 2; l++) {\n dp[i][j][k][l] = ndp[i][j][k][l] = 0;\n }\n }\n }\n }\n dp[0][0][0][0] = 1;\n\n int cur = 0;\n for (int i = 0; i < M; i++) {\n for (int j = 0; j < n; j++) {\n for (int k = 0; k < m; k++) {\n for (int l = 0; l < 2; l++) {\n mint& val = dp[cur][j][k][l];\n if (val == 0) continue;\n if (i == 0) {\n for (int nk = 0; nk < m; nk++) {\n int ni = cur + int(S[nk].size()), nj = nxt[j][nk].first, nl = l + nxt[j][nk].second;\n if (nl > 1) continue;\n if (ni < MAX_L)\n dp[ni][nj][nk][nl] += val;\n else\n ndp[ni - MAX_L][nj][nk][nl] += val;\n }\n } else {\n for (int nk : G[k]) {\n int ni = cur + int(S[nk].size()), nj = nxt[j][nk].first, nl = l + nxt[j][nk].second;\n if (nl > 1) continue;\n if (ni < MAX_L)\n dp[ni][nj][nk][nl] += val;\n else\n ndp[ni - MAX_L][nj][nk][nl] += val;\n }\n }\n val = 0;\n }\n }\n }\n if (++cur == MAX_L) {\n swap(dp, ndp);\n cur = 0;\n }\n }\n\n mint ans = 0;\n for (int j = 0; j < n; j++) {\n for (int k = 0; k < m; k++) {\n ans += dp[cur][j][k][1];\n }\n }\n cout << ans << '\\n';\n return true;\n}\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n while (solve())\n ;\n return 0;\n}", "accuracy": 1, "time_ms": 3410, "memory_kb": 108176, "score_of_the_acc": -0.9716, "final_rank": 7 }, { "submission_id": "aoj_2257_6020895", "code_snippet": "/*\tauthor: Kite_kuma\n\tcreated: 2021.11.01 20:06:53 */\n\n// #ifdef LOCAL\n// #define _GLIBCXX_DEBUG\n// #endif\n#include <bits/stdc++.h>\nusing namespace std;\n\n#pragma region macros\n\n#define foa(s, v) for(auto &s : v)\n#define all(v) (v).begin(), (v).end()\n#define rall(v) (v).rbegin(), (v).rend()\n#define popcnt(n) __builtin_popcountll((long long)n)\n\n#define REPname(a, b, c, d, e, ...) e\n#define rep(...) REPname(__VA_ARGS__, REP3, REP2, REP1, REP0)(__VA_ARGS__)\n#define REP0(x) for(int Counter_in_rep_macro = 0; Counter_in_rep_macro < (x); ++Counter_in_rep_macro)\n#define REP1(i, x) for(int i = 0; i < (x); ++i)\n#define REP2(i, l, r) for(int i = (l); i < (r); ++i)\n#define REP3(i, l, r, c) for(int i = (l); i < (r); i += (c))\n\n#define DREPname(a, b, c, d, e, ...) e\n#define drep(...) DREPname(__VA_ARGS__, DREP3, DREP2, DREP1)(__VA_ARGS__)\n#define DREP1(i, x) for(int i = (x)-1; i >= 0; --i)\n#define DREP2(i, l, r) for(int i = (r)-1; i >= (l); --i)\n#define DREP3(i, l, r, c) for(int i = (r)-1; i >= (l); i -= (c))\n\n#pragma endregion\n\n#pragma region aliases\n\nusing ll = long long;\nusing ld = long double;\nusing ull = unsigned long long;\nusing vi = std::vector<int>;\nusing vvi = std::vector<std::vector<int>>;\nusing vvvi = std::vector<std::vector<std::vector<int>>>;\nusing vll = std::vector<ll>;\nusing vvll = std::vector<vll>;\nusing vvvll = std::vector<vvll>;\nusing pii = std::pair<int, int>;\nusing pll = std::pair<long long, long long>;\ntemplate <class T = ll>\nusing V = std::vector<T>;\ntemplate <class T = ll>\nusing VV = V<V<T>>;\ntemplate <class T = ll>\nusing VVV = V<V<V<T>>>;\ntemplate <class T = ll>\nusing pqup = std::priority_queue<T, std::vector<T>, std::greater<T>>;\ntemplate <class T = ll>\nusing pqdn = std::priority_queue<T>;\n\n#pragma endregion\n\n#pragma region constants\n\nconst int inf = 1e9;\nconst long long INF = 1e18;\nconst long double pi = acos(-1);\nconst char dl = '\\n';\nconst char sp = ' ';\nint dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};\nint dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};\n\nconst int mod_1000000007 = 1000000007;\nconst int mod_998244353 = 998244353;\n\n#pragma endregion\n\n#pragma region basic_operation\n\ntemplate <class T0, class T1, class T2>\ninline bool in_range(T0 x, T1 lef, T2 rig) {\n\treturn ((lef <= x) && (x < rig));\n}\n\ntemplate <class T>\ninline bool chmin(T &a, T b) {\n\tif(a > b) {\n\t\ta = b;\n\t\treturn true;\n\t}\n\treturn false;\n}\ntemplate <class T>\ninline bool chmax(T &a, T b) {\n\tif(a < b) {\n\t\ta = b;\n\t\treturn true;\n\t}\n\treturn false;\n}\n\nvoid Yes(bool f = 1) { std::cout << (f ? \"Yes\" : \"No\") << '\\n'; }\nvoid No() { std::cout << \"No\\n\"; }\nvoid YES(bool f = 1) { std::cout << (f ? \"YES\" : \"NO\") << '\\n'; }\nvoid NO() { std::cout << \"NO\\n\"; }\n\ntemplate <class T>\nvoid drop(T answer) {\n\tstd::cout << answer << '\\n';\n\texit(0);\n}\n\nvoid err(bool flag = true) {\n\tif(!flag) return;\n\tstd::cout << -1 << '\\n';\n\texit(0);\n}\n\ntemplate <class T>\nvoid vout(std::vector<T> const &v, bool vertically = 0) {\n\tif(vertically) {\n\t\tfor(auto const &a : v) {\n\t\t\tstd::cout << a << '\\n';\n\t\t}\n\t\treturn;\n\t}\n\tfor(auto it = v.begin(); it != v.end(); it++) {\n\t\tstd::cout << (*it);\n\t\tif(it != v.end() - 1) {\n\t\t\tstd::cout << ' ';\n\t\t}\n\t}\n\tstd::cout << '\\n';\n\treturn;\n}\n\ninline void print() { std::cout << '\\n'; }\ntemplate <class T>\ninline void print(T x) {\n\tstd::cout << x << '\\n';\n\treturn;\n}\ntemplate <typename Head, typename... Tail>\nvoid print(Head H, Tail... T) {\n\tstd::cout << H << \" \";\n\tprint(T...);\n}\n\ntemplate <class T>\nvoid add(std::vector<T> &v, T val) {\n\tfor(auto &a : v) a += val;\n\treturn;\n}\n\ntemplate <class T>\nT dup(T a, T b) {\n\tassert(b != 0);\n\treturn (a + b - 1) / b;\n}\n\ntemplate <class T>\nT greatest_lower_multiple(T x, T d) {\n\tif(d == 0) return 0;\n\tif(d < 0) d *= -1;\n\tT y = x % d;\n\tif(y < 0) y += d;\n\treturn x - y;\n}\n\ntemplate <class T>\nT least_upper_multiple(T x, T d) {\n\treturn -greatest_lower_multiple(-x, d);\n}\n\nlong long POW(long long a, long long n) {\n\tlong long res = 1;\n\twhile(n > 0) {\n\t\tif(n & 1) res = res * a;\n\t\ta = a * a;\n\t\tn >>= 1;\n\t}\n\treturn res;\n}\n\nlong long modpow(long long a, long long n, long long mod) { // a^n mod\n\tassert(n >= 0 && mod);\n\tif(mod == 1) return 0LL;\n\tlong long res = 1;\n\ta %= mod;\n\twhile(n > 0) {\n\t\tif(n & 1) res = res * a % mod;\n\t\ta = a * a % mod;\n\t\tn >>= 1;\n\t}\n\treturn res < 0 ? res + mod : res;\n}\n\n// return x which satisfies a * x % mod == gcd(a, mod)\n// not (mod | a)\nlong long modinv(long long a, long long mod) {\n\tlong long b = mod, u = 1, v = 0;\n\twhile(b) {\n\t\tlong long t = a / b;\n\t\ta -= t * b;\n\t\tstd::swap(a, b);\n\t\tu -= t * v;\n\t\tstd::swap(u, v);\n\t}\n\tu %= mod;\n\tif(u < 0) u += mod;\n\treturn u;\n}\n\ntemplate <class T>\nint lb(const std::vector<T> &a, const T x) {\n\treturn std::distance(a.begin(), std::lower_bound(a.begin(), a.end(), x));\n}\ntemplate <class T>\nint ub(const std::vector<T> &a, const T x) {\n\treturn std::distance(a.begin(), std::upper_bound(a.begin(), a.end(), x));\n}\ntemplate <class T>\nvoid unq_sort(std::vector<T> &a) {\n\tstd::sort(a.begin(), a.end());\n\ta.erase(std::unique(a.begin(), a.end()), a.end());\n}\ntemplate <class T>\nstd::vector<int> press(std::vector<T> &a) {\n\tauto vec = a;\n\tunq_sort(vec);\n\tstd::vector<int> ret;\n\tfor(auto &v : a) ret.push_back(lb(vec, v));\n\treturn ret;\n}\n\n#pragma endregion\n\n#pragma region input\n#define VEC(type, name, size) \\\n\tvector<type> name(size); \\\n\tscanner::INPUT(name)\n#define VVEC(type, name, h, w) \\\n\tvector<vector<type>> name(h, vector<type>(w)); \\\n\tscanner::INPUT(name)\n#define INT(...) \\\n\tint __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define LL(...) \\\n\tlong long __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define STR(...) \\\n\tstring __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define CHR(...) \\\n\tchar __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define DBL(...) \\\n\tdouble __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define LD(...) \\\n\tlong double __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define TPL3(type0, type1, type2, name) \\\n\tstd::tuple<type0, type1, type2> name; \\\n\tscanner::INPUT(name);\n#define TPL4(type0, type1, type2, type3, name) \\\n\tstd::tuple<type0, type1, type2, type3> name; \\\n\tscanner::INPUT(name);\n\nnamespace scanner {\ntemplate <class T>\nvoid scan(T &a) {\n\tstd::cin >> a;\n}\n\ntemplate <class T, class L>\nvoid scan(std::pair<T, L> &p) {\n\tscan(p.first);\n\tscan(p.second);\n}\n\ntemplate <class T0, class T1, class T2>\nvoid scan(std::tuple<T0, T1, T2> &p) {\n\tT0 t0;\n\tT1 t1;\n\tT2 t2;\n\tscan(t0);\n\tscan(t1);\n\tscan(t2);\n\tp = std::make_tuple(t0, t1, t2);\n}\n\ntemplate <class T0, class T1, class T2, class T3>\nvoid scan(std::tuple<T0, T1, T2, T3> &p) {\n\tT0 t0;\n\tT1 t1;\n\tT2 t2;\n\tT3 t3;\n\tscan(t0);\n\tscan(t1);\n\tscan(t2);\n\tscan(t3);\n\tp = std::make_tuple(t0, t1, t2, t3);\n}\n\ntemplate <class T>\nvoid scan(std::vector<T> &a) {\n\tfor(auto &i : a) scan(i);\n}\n\nvoid INPUT() {}\ntemplate <class Head, class... Tail>\nvoid INPUT(Head &head, Tail &...tail) {\n\tscan(head);\n\tINPUT(tail...);\n}\n} // namespace scanner\n\ntemplate <typename T1, typename T2>\nstd::istream &operator>>(std::istream &is, std::pair<T1, T2> &p) {\n\tis >> p.first >> p.second;\n\treturn is;\n}\n#pragma endregion\n\n#pragma region debug\n\n#pragma region output\ntemplate <typename T1, typename T2>\nstd::ostream &std::operator<<(std::ostream &os, const std::pair<T1, T2> &p) {\n\tos << p.first << \" \" << p.second;\n\treturn os;\n}\ntemplate <class T>\nstd::ostream &std::operator<<(std::ostream &os, const std::vector<T> &v) {\n\tfor(int i = 0; i < (int)v.size(); i++) {\n\t\tif(i) os << \" \";\n\t\tos << v[i];\n\t}\n\treturn os;\n}\n#pragma endregion\n\n#pragma region view\n\nnamespace viewer {\nvoid view(const long long e) {\n\tif(e == INF)\n\t\tstd::cerr << \"INF\";\n\telse if(e == -INF)\n\t\tstd::cerr << \"-INF\";\n\telse\n\t\tstd::cerr << e;\n}\n\nvoid view(const int e) {\n\tif(e == inf)\n\t\tstd::cerr << \"inf\";\n\telse if(e == -inf)\n\t\tstd::cerr << \"-inf\";\n\telse\n\t\tstd::cerr << e;\n}\n\ntemplate <typename T>\nvoid view(const T e) {\n\tstd::cerr << e;\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::pair<T, U> &p) {\n\tstd::cerr << \"(\";\n\tview(p.first);\n\tstd::cerr << \", \";\n\tview(p.second);\n\tstd::cerr << \")\";\n}\n\ntemplate <class T0, class T1, class T2>\nvoid view(const std::tuple<T0, T1, T2> &p) {\n\tstd::cerr << \"(\";\n\tview(std::get<0>(p));\n\tstd::cerr << \", \";\n\tview(std::get<1>(p));\n\tstd::cerr << \", \";\n\tview(std::get<2>(p));\n\tstd::cerr << \")\";\n}\n\ntemplate <class T0, class T1, class T2, class T3>\nvoid view(const std::tuple<T0, T1, T2, T3> &p) {\n\tstd::cerr << \"(\";\n\tview(std::get<0>(p));\n\tstd::cerr << \", \";\n\tview(std::get<1>(p));\n\tstd::cerr << \", \";\n\tview(std::get<2>(p));\n\tstd::cerr << \", \";\n\tview(std::get<3>(p));\n\tstd::cerr << \")\";\n}\n\ntemplate <typename T>\nvoid view(const std::set<T> &s) {\n\tif(s.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << \"{ \";\n\tfor(auto &t : s) {\n\t\tview(t);\n\t\tstd::cerr << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\n\ntemplate <typename T>\nvoid view(const std::unordered_set<T> &s) {\n\tif(s.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << \"{ \";\n\tfor(auto &t : s) {\n\t\tview(t);\n\t\tstd::cerr << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\n\ntemplate <typename T>\nvoid view(const std::vector<T> &v) {\n\tif(v.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << \"{ \";\n\tfor(const auto &e : v) {\n\t\tview(e);\n\t\tstd::cerr << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\n\ntemplate <typename T>\nvoid view(const std::vector<std::vector<T>> &vv) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &v : vv) {\n\t\tstd::cerr << \"\\t\";\n\t\tview(v);\n\t\tstd::cerr << '\\n';\n\t}\n\tstd::cerr << \"}\";\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::vector<std::pair<T, U>> &v) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &c : v) {\n\t\tstd::cerr << \"\\t(\";\n\t\tview(c.first);\n\t\tstd::cerr << \", \";\n\t\tview(c.second);\n\t\tstd::cerr << \")\\n\";\n\t}\n\tstd::cerr << \"}\";\n}\n\ntemplate <class T0, class T1, class T2>\nvoid view(const std::vector<std::tuple<T0, T1, T2>> &v) {\n\tif(v.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << '{';\n\tfor(const auto &t : v) {\n\t\tstd::cerr << \"\\n\\t\";\n\t\tview(t);\n\t\tstd::cerr << \",\";\n\t}\n\tstd::cerr << \"\\n}\";\n}\n\ntemplate <class T0, class T1, class T2, class T3>\nvoid view(const std::vector<std::tuple<T0, T1, T2, T3>> &v) {\n\tif(v.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << '{';\n\tfor(const auto &t : v) {\n\t\tstd::cerr << \"\\n\\t\";\n\t\tview(t);\n\t\tstd::cerr << \",\";\n\t}\n\tstd::cerr << \"\\n}\";\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::map<T, U> &m) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &t : m) {\n\t\tstd::cerr << \"\\t[\";\n\t\tview(t.first);\n\t\tstd::cerr << \"] : \";\n\t\tview(t.second);\n\t\tstd::cerr << '\\n';\n\t}\n\tstd::cerr << \"}\";\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::unordered_map<T, U> &m) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &t : m) {\n\t\tstd::cerr << \"\\t[\";\n\t\tview(t.first);\n\t\tstd::cerr << \"] : \";\n\t\tview(t.second);\n\t\tstd::cerr << '\\n';\n\t}\n\tstd::cerr << \"}\";\n}\n} // namespace viewer\n\n#pragma endregion\n\n// when debugging : g++ foo.cpp -DLOCAL\n#ifdef LOCAL\nvoid debug_out() {}\ntemplate <typename Head, typename... Tail>\nvoid debug_out(Head H, Tail... T) {\n\tviewer::view(H);\n\tstd::cerr << \", \";\n\tdebug_out(T...);\n}\n#define debug(...) \\\n\tdo { \\\n\t\tstd::cerr << __LINE__ << \" [\" << #__VA_ARGS__ << \"] : [\"; \\\n\t\tdebug_out(__VA_ARGS__); \\\n\t\tstd::cerr << \"\\b\\b]\\n\"; \\\n\t} while(0)\n#define dump(x) \\\n\tdo { \\\n\t\tstd::cerr << __LINE__ << \" \" << #x << \" : \"; \\\n\t\tviewer::view(x); \\\n\t\tstd::cerr << '\\n'; \\\n\t} while(0)\n\n#else\n#define debug(...) (void(0))\n#define dump(x) (void(0))\n#endif\n\n#pragma endregion\n\n#pragma region Aho_Corasick\n#pragma region Trie\n\ntemplate <int char_size>\nstruct TrieNode {\n\tint nxt[char_size];\n\n\tint exist; // 子孫が何個あるか(このノードで終わりの文字列は除く)\n\tvector<int> accept; // このノードで終わりの単語の番号を入れた配列(追加順)\n\n\tTrieNode() : exist(0) { memset(nxt, -1, sizeof(nxt)); }\n};\ntemplate <int char_size = 26, int margin = 'a'> /* <26,'a'> など */\nstruct Trie {\n\tusing Node = TrieNode<char_size>;\n\n\tvector<Node> nodes;\n\tint root;\n\n\tTrie() : root(0) { nodes.push_back(Node()); }\n\n\tvoid update_direct(int node, int id) { nodes[node].accept.push_back(id); }\n\n\t// nodeの重さ(要素数)1増大\n\tvoid update_child(int node, int child, int id) { ++nodes[node].exist; }\n\n\tvoid add(const string &str, int str_index, int node_index, int id) {\n\t\tif(str_index == str.size()) {\n\t\t\tupdate_direct(node_index, id); // 単語の終わりを示す\n\t\t} else {\n\t\t\tconst int c = str[str_index] - margin;\n\t\t\tif(nodes[node_index].nxt[c] == -1) {\n\t\t\t\tnodes[node_index].nxt[c] = (int)nodes.size();\n\t\t\t\tnodes.push_back(Node());\n\t\t\t}\n\t\t\tadd(str, str_index + 1, nodes[node_index].nxt[c], id);\n\t\t\tupdate_child(node_index, nodes[node_index].nxt[c], id);\n\t\t}\n\t}\n\n\tvoid add(const string &str, int id) { add(str, 0, 0, id); }\n\n\tvoid add(const string &str) { add(str, nodes[0].exist); }\n\n\tvoid query(const string &str, const function<void(int)> &f, int str_index, int node_index) {\n\t\tfor(auto &idx : nodes[node_index].accept) f(idx);\n\t\tif(str_index == str.size()) {\n\t\t\treturn;\n\t\t} else {\n\t\t\tconst int c = str[str_index] - margin;\n\t\t\tif(nodes[node_index].nxt[c] == -1) return;\n\t\t\tquery(str, f, str_index + 1, nodes[node_index].nxt[c]);\n\t\t}\n\t}\n\n\tvoid query(const string &str, const function<void(int)> &f) { query(str, f, 0, 0); }\n\n\tint count() const { return (nodes[0].exist); } // 単語数(重複含む)\n\n\tint size() const { return ((int)nodes.size()); } // node数\n};\n\n#pragma endregion\n\ntemplate <int char_size, int margin>\nstruct AhoCorasick : Trie<char_size + 1, margin> {\n\tusing Trie<char_size + 1, margin>::Trie;\n\n\tconst int FAIL = char_size;\n\tvector<int> correct;\n\n\tvoid build(bool heavy = true) {\n\t\tcorrect.resize(this->size());\n\t\tfor(int i = 0; i < this->size(); i++) {\n\t\t\tcorrect[i] = (int)this->nodes[i].accept.size();\n\t\t}\n\t\tqueue<int> que;\n\t\t// determine nodes[i].nxt[FAIL] at first, and next determine nodes[i].nxt[0~25]\n\t\tfor(int i = 0; i <= char_size; i++) {\n\t\t\tif(~this->nodes[0].nxt[i]) { // this->nodes[0].nxt[i] != -1\n\t\t\t\tthis->nodes[this->nodes[0].nxt[i]].nxt[FAIL] = 0;\n\t\t\t\tque.emplace(this->nodes[0].nxt[i]);\n\t\t\t} else {\n\t\t\t\tthis->nodes[0].nxt[i] = 0;\n\t\t\t}\n\t\t}\n\t\twhile(!que.empty()) {\n\t\t\tauto &now = this->nodes[que.front()];\n\t\t\tint fail = now.nxt[FAIL];\n\t\t\tcorrect[que.front()] += correct[fail];\n\t\t\tque.pop();\n\t\t\tfor(int i = 0; i < char_size; i++) {\n\t\t\t\tif(~now.nxt[i]) { // now.nxt[i] != -1\n\t\t\t\t\tthis->nodes[now.nxt[i]].nxt[FAIL] = this->nodes[fail].nxt[i];\n\t\t\t\t\tif(heavy) {\n\t\t\t\t\t\tauto &u = this->nodes[now.nxt[i]].accept;\n\t\t\t\t\t\tauto &v = this->nodes[this->nodes[fail].nxt[i]].accept;\n\t\t\t\t\t\tvector<int> accept;\n\t\t\t\t\t\tset_union(begin(u), end(u), begin(v), end(v), back_inserter(accept));\n\t\t\t\t\t\tu = accept;\n\t\t\t\t\t}\n\t\t\t\t\tque.emplace(now.nxt[i]);\n\t\t\t\t} else {\n\t\t\t\t\tnow.nxt[i] = this->nodes[fail].nxt[i];\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tmap<int, int> match(const string &str, int now = 0) {\n\t\tmap<int, int> result;\n\t\tfor(auto &c : str) {\n\t\t\tnow = this->nodes[now].nxt[c - margin];\n\t\t\tfor(auto &v : this->nodes[now].accept) result[v] += 1;\n\t\t}\n\t\treturn result;\n\t}\n\n\tpair<int64_t, int> move(const char &c, int now = 0) {\n\t\tnow = this->nodes[now].nxt[c - margin];\n\t\treturn {correct[now], now};\n\t}\n\n\t// 一致要素数, 最終node\n\tpair<int64_t, int> move(const string &str, int now = 0) {\n\t\tint64_t sum = 0;\n\t\tfor(auto &c : str) {\n\t\t\tauto nxt = move(c, now);\n\t\t\tsum += nxt.first;\n\t\t\tnow = nxt.second;\n\t\t}\n\t\treturn {sum, now};\n\t}\n};\n#pragma endregion\n\n#pragma region modint\n\nconst int mod = mod_1000000007;\n\ntemplate <int mod>\nstruct ModInt {\n\tint x;\n\tModInt() : x(0) {}\n\tModInt(long long y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}\n\tModInt &operator+=(const ModInt &p) {\n\t\tif((x += p.x) >= mod) x -= mod;\n\t\treturn *this;\n\t}\n\tModInt &operator-=(const ModInt &p) {\n\t\tif((x += mod - p.x) >= mod) x -= mod;\n\t\treturn *this;\n\t}\n\tModInt &operator*=(const ModInt &p) {\n\t\tx = (int)(1LL * x * p.x % mod);\n\t\treturn *this;\n\t}\n\tModInt &operator/=(const ModInt &p) {\n\t\t*this *= p.inverse();\n\t\treturn *this;\n\t}\n\tModInt operator-() const { return ModInt(-x); }\n\tModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }\n\tModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }\n\tModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }\n\tModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }\n\tbool operator==(const ModInt &p) const { return x == p.x; }\n\tbool operator!=(const ModInt &p) const { return x != p.x; }\n\tModInt inverse() const {\n\t\tint a = x, b = mod, u = 1, v = 0, t;\n\t\twhile(b > 0) {\n\t\t\tt = a / b;\n\t\t\tswap(a -= t * b, b);\n\t\t\tswap(u -= t * v, v);\n\t\t}\n\t\treturn ModInt(u);\n\t}\n\tModInt pow(long long n) const {\n\t\tModInt ret(1), mul(x);\n\t\twhile(n > 0) {\n\t\t\tif(n & 1) ret *= mul;\n\t\t\tmul *= mul;\n\t\t\tn >>= 1;\n\t\t}\n\t\treturn ret;\n\t}\n\tfriend ostream &operator<<(ostream &os, const ModInt &p) { return os << p.x; }\n\tfriend istream &operator>>(istream &is, ModInt &a) {\n\t\tlong long t;\n\t\tis >> t;\n\t\ta = ModInt<mod>(t);\n\t\treturn (is);\n\t}\n\tstatic int get_mod() { return mod; }\n};\nusing mint = ModInt<mod>;\n\n#pragma endregion\n\nmint solve(int n) {\n\tint m, k;\n\tcin >> m >> k;\n\tmap<string, int> dict;\n\tV<pair<string, string>> con;\n\trep(i, n) {\n\t\tstring from, to;\n\t\tcin >> from >> to;\n\t\tcon.emplace_back(from, to);\n\t\tdict[from] = 0;\n\t\tdict[to] = 0;\n\t}\n\tV<string> words;\n\tint sz_words = 0;\n\tfoa(p, dict) { p.second = sz_words++; }\n\twords.resize(sz_words);\n\tfoa(p, dict) { words[p.second] = p.first; }\n\n\tvvi g(sz_words);\n\tfoa(t, con) {\n\t\tint from = dict[t.first];\n\t\tint to = dict[t.second];\n\t\tg[from].push_back(to);\n\t}\n\n\tV<string> seasonwords(k);\n\tAhoCorasick<26, 'a'> ac;\n\tfoa(t, seasonwords) {\n\t\tcin >> t;\n\t\tac.add(t);\n\t}\n\tac.build();\n\tint nodes = ac.size();\n\n\tVV<pair<int, int>> arrow(nodes, V<pair<int, int>>(sz_words));\n\t// match, node_to\n\n\trep(node_from, nodes) {\n\t\tint node_to, match;\n\t\trep(i, sz_words) {\n\t\t\ttie(match, node_to) = ac.move(words[i], node_from);\n\t\t\tarrow[node_from][i] = make_pair(match, node_to);\n\t\t}\n\t}\n\n\tunordered_map<ll, mint> hash_dp;\n\tauto to_hash = [&](int i, int j, int k, int l) -> ll {\n\t\treturn (ll)l + (ll)k * 700 + (ll)j * n * 2 * 700 + (ll)i * n * 2 * 700 * 2;\n\t};\n\n\trep(i, sz_words) {\n\t\tstring w = words[i];\n\t\tint is_season, node;\n\t\tif(int(w.size()) > m) continue;\n\t\ttie(is_season, node) = ac.move(w, 0);\n\t\tif(is_season < 2) hash_dp[to_hash(w.size(), is_season, i, node)] = 1;\n\t}\n\n\trep(len, 1, m) {\n\t\trep(is_seasoned, 2) rep(word_id, sz_words) rep(node, nodes) {\n\t\t\tll h = to_hash(len, is_seasoned, word_id, node);\n\t\t\tif(!hash_dp.count(h)) continue;\n\t\t\tconst mint &val = hash_dp[h];\n\t\t\tif(val == 0) continue;\n\t\t\tfoa(nxtword_id, g[word_id]) {\n\t\t\t\tconst string &nxt_str = words[nxtword_id];\n\t\t\t\tint len_nxt = len + nxt_str.size();\n\t\t\t\tif(len_nxt > m) continue;\n\t\t\t\tint season_ad, node_nxt;\n\t\t\t\ttie(season_ad, node_nxt) = arrow[node][nxtword_id];\n\t\t\t\tint nxt_season = is_seasoned + season_ad;\n\t\t\t\tif(nxt_season >= 2) continue;\n\t\t\t\thash_dp[(to_hash(len_nxt, nxt_season, nxtword_id, node_nxt))] += val;\n\t\t\t}\n\t\t}\n\t}\n\tmint ans = 0;\n\trep(i, sz_words) rep(j, nodes) {\n\t\tans += hash_dp[(to_hash(m, 1, i, j))];\n\t}\n\n\treturn ans;\n}\nint main() {\n\tstd::ios::sync_with_stdio(false);\n\tstd::cin.tie(nullptr);\n\tstd::cout << std::fixed << std::setprecision(15);\n\tsrand((unsigned)time(NULL));\n\n\tint n;\n\twhile(cin >> n && n) cout << solve(n) << dl;\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 5000, "memory_kb": 131484, "score_of_the_acc": -1.3305, "final_rank": 16 }, { "submission_id": "aoj_2257_5492589", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\nconst ll MOD = 1e9 + 7;\nstruct Mint {\n ll x;\n Mint(ll xx = 0) {\n x = ((xx % MOD) + MOD) % MOD;\n }\n Mint operator+(Mint b) { return Mint(x + b.x); }\n Mint operator-(Mint b) { return Mint(x - b.x); }\n Mint operator*(Mint b) { return Mint(x * b.x); }\n Mint operator/(Mint b) { return *this * b.inv(); }\n Mint& operator+=(Mint b) { return *this = *this + b; }\n Mint& operator-=(Mint b) { return *this = *this - b; }\n Mint& operator*=(Mint b) { return *this = *this * b; }\n Mint& operator/=(Mint b) { return *this = *this / b; }\n Mint pow(ll n) {\n Mint a = *this, ret = 1;\n while (n) {\n if (n & 1)\n ret = ret * a;\n a = a * a;\n n >>= 1;\n }\n return ret;\n }\n Mint inv() { return pow(MOD - 2); }\n};\n\ntemplate <int char_size, int margin>\nstruct Trie {\n struct Node {\n int nxt[char_size];\n int exist;\n vector<int> accept;\n Node() : exist(0) {\n memset(nxt, -1, sizeof(nxt));\n }\n };\n vector<Node> nodes;\n int root;\n Trie() : root(0) {\n nodes.emplace_back();\n }\n void add(const string& s) {\n int x = nodes[0].exist, pos = 0;\n for (auto&& ch : s) {\n nodes[pos].exist++;\n int c = ch - margin;\n if (nodes[pos].nxt[c] == -1) {\n int npos = nodes.size();\n nodes[pos].nxt[c] = npos;\n nodes.emplace_back();\n pos = npos;\n } else\n pos = nodes[pos].nxt[c];\n }\n nodes[pos].accept.emplace_back(x);\n }\n int find(const string& s) {\n int pos = 0;\n for (auto&& ch : s) {\n int c = ch - margin;\n pos = nodes[pos].nxt[c];\n if (pos < 0)\n return -1;\n }\n return pos;\n }\n int move(char ch, int pos) {\n if (pos < 0)\n return pos;\n int c = ch - margin;\n pos = nodes[pos].nxt[c];\n return pos;\n }\n int size() { return nodes.size(); }\n};\n\ntemplate <int char_size, int margin>\nstruct AhoCorasick : public Trie<char_size + 1, margin> {\n const int FAIL = char_size;\n vector<int> correct;\n void build() {\n correct.resize(this->size());\n for (int i = 0; i < this->size(); i++) {\n correct[i] = this->nodes[i].accept.size();\n }\n queue<int> que;\n for (int i = 0; i <= char_size; i++) {\n if (~this->nodes[0].nxt[i]) {\n this->nodes[this->nodes[0].nxt[i]].nxt[FAIL] = 0;\n que.emplace(this->nodes[0].nxt[i]);\n } else\n this->nodes[0].nxt[i] = 0;\n }\n while (!que.empty()) {\n auto& now = this->nodes[que.front()];\n int fail = now.nxt[FAIL];\n correct[que.front()] += correct[fail];\n que.pop();\n for (int i = 0; i < char_size; i++) {\n if (~now.nxt[i]) {\n this->nodes[now.nxt[i]].nxt[FAIL] = this->nodes[fail].nxt[i];\n auto& u = this->nodes[now.nxt[i]].accept;\n auto& v = this->nodes[this->nodes[fail].nxt[i]].accept;\n vector<int> accept;\n set_union(begin(u), end(u), begin(v), end(v), back_inserter(accept));\n u = accept;\n que.emplace(now.nxt[i]);\n } else\n now.nxt[i] = this->nodes[fail].nxt[i];\n }\n }\n }\n pair<int, ll> move(const char& ch, int pos = 0) {\n pos = this->nodes[pos].nxt[ch - margin];\n return {pos, correct[pos]};\n }\n pair<int, ll> move(const string& str, int pos = 0) {\n ll sum = 0;\n for (auto&& c : str) {\n auto p = move(c, pos);\n pos = p.first;\n sum += p.second;\n }\n return {pos, sum};\n }\n};\n\nint main() {\n int N, M, K;\n while (cin >> N >> M >> K, N) {\n int n_phrase = 0;\n map<string, int> mp;\n vector<string> from(N), to(N);\n vector<string> phrase;\n for (int i = 0; i < N; i++) {\n cin >> from[i] >> to[i];\n if (!mp.count(from[i])) {\n phrase.emplace_back(from[i]);\n mp[from[i]] = n_phrase++;\n }\n if (!mp.count(to[i])) {\n phrase.emplace_back(to[i]);\n mp[to[i]] = n_phrase++;\n }\n }\n vector<vector<int>> G(n_phrase + 1);\n for (int i = 0; i < N; i++) {\n G[mp[from[i]]].emplace_back(mp[to[i]]);\n }\n for (int i = 0; i < n_phrase; i++) {\n G[n_phrase].emplace_back(i);\n }\n vector<string> seasonword(K);\n AhoCorasick<26, 'a'> aho;\n for (int i = 0; i < K; i++) {\n cin >> seasonword[i];\n aho.add(seasonword[i]);\n }\n aho.build();\n map<tuple<int, int, int>, Mint> dp[510];\n dp[0][{n_phrase, 0, 0}] = 1;\n for (int len_now = 0; len_now < M; len_now++) {\n for (auto&& [key, maine] : dp[len_now]) {\n auto [phrase_now, pos_now, cnt_now] = key;\n for (auto&& phrase_nxt : G[phrase_now]) {\n int len_nxt = len_now + phrase[phrase_nxt].size();\n if (len_nxt > M)\n continue;\n auto [pos_nxt, cnt_nxt] = aho.move(phrase[phrase_nxt], pos_now);\n cnt_nxt += cnt_now;\n if (cnt_nxt >= 2)\n continue;\n dp[len_nxt][{phrase_nxt, pos_nxt, cnt_nxt}] += maine;\n }\n }\n dp[len_now].clear();\n }\n Mint ans = 0;\n for (auto&& [key, maine] : dp[M]) {\n if (get<2>(key) == 1)\n ans += maine;\n }\n cout << ans.x << endl;\n }\n}", "accuracy": 1, "time_ms": 2720, "memory_kb": 6004, "score_of_the_acc": -0.3454, "final_rank": 1 }, { "submission_id": "aoj_2257_5465955", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nconst long long MOD = 1000000007;\nint main(){\n while (true){\n int N, M, K;\n cin >> N >> M >> K;\n if (N == 0 && M == 0 && K == 0){\n break;\n }\n vector<string> A(N), B(N);\n for (int i = 0; i < N; i++){\n cin >> A[i] >> B[i];\n }\n vector<string> S(K);\n for (int i = 0; i < K; i++){\n cin >> S[i];\n }\n vector<string> C;\n C.push_back(\"\");\n for (int i = 0; i < N; i++){\n C.push_back(A[i]);\n C.push_back(B[i]);\n }\n sort(C.begin(), C.end());\n C.erase(unique(C.begin(), C.end()), C.end());\n int V = C.size();\n vector<vector<int>> E(V);\n for (int i = 1; i < V; i++){\n E[0].push_back(i);\n }\n for (int i = 0; i < N; i++){\n int a = lower_bound(C.begin(), C.end(), A[i]) - C.begin();\n int b = lower_bound(C.begin(), C.end(), B[i]) - C.begin();\n E[a].push_back(b);\n }\n vector<string> S2;\n for (int i = 0; i < K; i++){\n int L = S[i].size();\n for (int j = 0; j <= L; j++){\n S2.push_back(S[i].substr(0, j));\n }\n }\n sort(S2.begin(), S2.end());\n S2.erase(unique(S2.begin(), S2.end()), S2.end());\n int cnt = S2.size();\n vector<int> W(cnt, 0);\n for (int i = 0; i < cnt; i++){\n for (int j = 0; j < K; j++){\n if (S2[i].size() >= S[j].size()){\n if (S2[i].substr(S2[i].size() - S[j].size()) == S[j]){\n W[i]++;\n }\n }\n }\n }\n map<string, int> mp;\n for (int i = 0; i < cnt; i++){\n mp[S2[i]] = i;\n }\n vector<vector<int>> T(cnt, vector<int>(26));\n for (int i = 0; i < cnt; i++){\n for (int j = 0; j < 26; j++){\n string X = S2[i] + (char) ('a' + j);\n int L = X.size();\n for (int k = 0; k <= L; k++){\n string X2 = X.substr(k);\n if (mp.count(X2) == 1){\n T[i][j] = mp[X2];\n break;\n }\n }\n }\n }\n vector<vector<int>> T2(cnt, vector<int>(V));\n vector<vector<int>> W2(cnt, vector<int>(V, 0));\n for (int i = 0; i < cnt; i++){\n for (int j = 0; j < V; j++){\n int c = i;\n int L = C[j].size();\n for (int k = 0; k < L; k++){\n c = T[c][C[j][k] - 'a'];\n W2[i][j] += W[c];\n }\n T2[i][j] = c;\n }\n }\n vector<vector<vector<long long>>> dp1(21, vector<vector<long long>>(cnt, vector<long long>(V, 0)));\n vector<vector<vector<long long>>> dp2(21, vector<vector<long long>>(cnt, vector<long long>(V, 0)));\n dp1[0][0][0] = 1;\n for (int i = 0; i < M; i++){\n int r = i % 21;\n for (int j = 0; j < cnt; j++){\n for (int k = 0; k < V; k++){\n for (int w : E[k]){\n int i2 = i + C[w].size();\n if (i2 <= M){\n int r2 = i2 % 21;\n int j2 = T2[j][w];\n int c = W2[j][w];\n if (c == 0){\n dp1[r2][j2][w] += dp1[r][j][k];\n dp1[r2][j2][w] %= MOD;\n dp2[r2][j2][w] += dp2[r][j][k];\n dp2[r2][j2][w] %= MOD;\n }\n if (c == 1){\n dp2[r2][j2][w] += dp1[r][j][k];\n dp2[r2][j2][w] %= MOD;\n }\n }\n }\n dp1[r][j][k] = 0;\n dp2[r][j][k] = 0;\n }\n }\n }\n long long ans = 0;\n for (int i = 0; i < cnt; i++){\n for (int j = 0; j < V; j++){\n ans += dp2[M % 21][i][j];\n }\n }\n ans %= MOD;\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 4250, "memory_kb": 105184, "score_of_the_acc": -1.083, "final_rank": 11 }, { "submission_id": "aoj_2257_5451596", "code_snippet": "#define MOD_TYPE 1\n\n#pragma region Macros\n\n#include <bits/stdc++.h>\nusing namespace std;\n\n#if 0\n#include <boost/multiprecision/cpp_int.hpp>\n#include <boost/multiprecision/cpp_dec_float.hpp>\nusing Int = boost::multiprecision::cpp_int;\nusing lld = boost::multiprecision::cpp_dec_float_100;\n#endif\n#if 0\n#pragma GCC target(\"avx2\")\n#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"unroll-loops\")\n#endif\nusing ll = long long int;\nusing ld = long double;\nusing pii = pair<int, int>;\nusing pll = pair<ll, ll>;\nusing pld = pair<ld, ld>;\ntemplate <typename Q_type>\nusing smaller_queue = priority_queue<Q_type, vector<Q_type>, greater<Q_type>>;\n\nconstexpr ll MOD = (MOD_TYPE == 1 ? (ll)(1e9 + 7) : 998244353);\nconstexpr int INF = (int)1e9 + 10;\nconstexpr ll LINF = (ll)4e18;\nconstexpr ld PI = acos(-1.0);\nconstexpr ld EPS = 1e-7;\nconstexpr int Dx[] = {0, 0, -1, 1, -1, 1, -1, 1, 0};\nconstexpr int Dy[] = {1, -1, 0, 0, -1, -1, 1, 1, 0};\n\n#define REP(i, m, n) for (ll i = m; i < (ll)(n); ++i)\n#define rep(i, n) REP(i, 0, n)\n#define REPI(i, m, n) for (int i = m; i < (int)(n); ++i)\n#define repi(i, n) REPI(i, 0, n)\n#define MP make_pair\n#define MT make_tuple\n#define YES(n) cout << ((n) ? \"YES\" : \"NO\") << \"\\n\"\n#define Yes(n) cout << ((n) ? \"Yes\" : \"No\") << \"\\n\"\n#define possible(n) cout << ((n) ? \"possible\" : \"impossible\") << \"\\n\"\n#define Possible(n) cout << ((n) ? \"Possible\" : \"Impossible\") << \"\\n\"\n#define all(v) v.begin(), v.end()\n#define NP(v) next_permutation(all(v))\n#define dbg(x) cerr << #x << \":\" << x << \"\\n\";\n\nstruct io_init\n{\n io_init()\n {\n cin.tie(0);\n ios::sync_with_stdio(false);\n cout << setprecision(30) << setiosflags(ios::fixed);\n };\n} io_init;\ntemplate <typename T>\ninline bool chmin(T &a, T b)\n{\n if (a > b)\n {\n a = b;\n return true;\n }\n return false;\n}\ntemplate <typename T>\ninline bool chmax(T &a, T b)\n{\n if (a < b)\n {\n a = b;\n return true;\n }\n return false;\n}\ninline ll CEIL(ll a, ll b)\n{\n return (a + b - 1) / b;\n}\ntemplate <typename A, size_t N, typename T>\ninline void Fill(A (&array)[N], const T &val)\n{\n fill((T *)array, (T *)(array + N), val);\n}\ntemplate <typename T, typename U>\nconstexpr istream &operator>>(istream &is, pair<T, U> &p) noexcept\n{\n is >> p.first >> p.second;\n return is;\n}\ntemplate <typename T, typename U>\nconstexpr ostream &operator<<(ostream &os, pair<T, U> &p) noexcept\n{\n os << p.first << \" \" << p.second;\n return os;\n}\n#pragma endregion\n\n// --------------------------------------\n\n#pragma region mint\ntemplate <int MOD>\nstruct Fp\n{\n int val;\n\n constexpr Fp(int v = 0) noexcept : val(v % MOD)\n {\n if (val < 0)\n v += MOD;\n }\n\n constexpr int getmod()\n {\n return MOD;\n }\n\n constexpr Fp operator-() const noexcept\n {\n return val ? MOD - val : 0;\n }\n\n constexpr Fp operator+(const Fp &r) const noexcept\n {\n return Fp(*this) += r;\n }\n\n constexpr Fp operator-(const Fp &r) const noexcept\n {\n return Fp(*this) -= r;\n }\n\n constexpr Fp operator*(const Fp &r) const noexcept\n {\n return Fp(*this) *= r;\n }\n\n constexpr Fp operator/(const Fp &r) const noexcept\n {\n return Fp(*this) /= r;\n }\n\n constexpr Fp &operator+=(const Fp &r) noexcept\n {\n val += r.val;\n if (val >= MOD)\n val -= MOD;\n return *this;\n }\n\n constexpr Fp &operator-=(const Fp &r) noexcept\n {\n val -= r.val;\n if (val < 0)\n val += MOD;\n return *this;\n }\n\n constexpr Fp &operator*=(const Fp &r) noexcept\n {\n val = val * r.val % MOD;\n if (val < 0)\n val += MOD;\n return *this;\n }\n\n constexpr Fp &operator/=(const Fp &r) noexcept\n {\n long long a = r.val, b = MOD, u = 1, v = 0;\n while (b)\n {\n long long t = a / b;\n a -= t * b;\n swap(a, b);\n u -= t * v;\n swap(u, v);\n }\n val = val * u % MOD;\n if (val < 0)\n val += MOD;\n return *this;\n }\n\n constexpr bool operator==(const Fp &r) const noexcept\n {\n return this->val == r.val;\n }\n\n constexpr bool operator!=(const Fp &r) const noexcept\n {\n return this->val != r.val;\n }\n\n friend constexpr ostream &operator<<(ostream &os, const Fp<MOD> &x) noexcept\n {\n return os << x.val;\n }\n\n friend constexpr istream &operator>>(istream &is, Fp<MOD> &x) noexcept\n {\n return is >> x.val;\n }\n};\n\nFp<MOD> modpow(const Fp<MOD> &a, long long n) noexcept\n{\n if (n == 0)\n return 1;\n auto t = modpow(a, n / 2);\n t = t * t;\n if (n & 1)\n t = t * a;\n return t;\n}\n\nusing mint = Fp<MOD>;\n#pragma endregion\n\ntemplate <char MIN_CHAR = 'a', int ALPHABET = 26>\nstruct AhoCorasick\n{\n struct node\n {\n // suff : 先頭の文字を最小限消してグラフに存在する頂点にするときの行先の頂点\n // dict : 先頭の文字を最小限消して辞書に存在する単語にするときの行先の頂点\n // depth : Trie木における深さ(省略可能)\n // word_index : このノードで終わる単語のindex(祖先は含まない。なければ-1)(複数ある場合は最小のもの)\n // word_count : このノードで終わる単語の総数\n // link : Trie及びsuffixの辺の接続先頂点(なければ-1)\n int suff = -1, dict = -1, depth = 0;\n int word_index = -1, word_count = 0;\n int link[ALPHABET];\n node() { fill(link, link + ALPHABET, -1); }\n int &operator[](char c) { return link[c - MIN_CHAR]; }\n };\n\n // nodes : 頂点集合\n // W : 現在の単語数\n // word_location : 各単語のTrie木の最後の頂点のindex\n // defer : 同じ単語が辞書内に存在する場合、最初の単語のindexを記録\n vector<node> nodes;\n int W;\n vector<int> word_location;\n vector<int> word_indices_by_depth;\n vector<int> defer;\n\n AhoCorasick(){};\n AhoCorasick(const vector<string> &words = {})\n {\n build(words);\n }\n\n // suffixを親とする木の隣接リスト これの上でDPやクエリ処理を行うことが多い\n vector<vector<int>> build_suffix_adj() const\n {\n vector<vector<int>> adj(nodes.size());\n for (int i = 1; i < int(nodes.size()); i++)\n adj[nodes[i].suff].push_back(i);\n return adj;\n }\n\n int get_or_add_child(int current, char c)\n {\n if (nodes[current][c] >= 0)\n return nodes[current][c];\n int index = int(nodes.size());\n nodes[current][c] = index;\n nodes.emplace_back();\n nodes.back().depth = nodes[current].depth + 1;\n return index;\n }\n\n int add_word(const string &word, int word_index)\n {\n assert(!nodes.empty());\n int current = 0;\n for (char c : word)\n current = get_or_add_child(current, c);\n if (nodes[current].word_index < 0)\n nodes[current].word_index = word_index;\n nodes[current].word_count++;\n return current;\n }\n\n // locationからcを追加したときの行き先 O(1)\n int get_suffix_link(int location, char c) const\n {\n if (location >= 0)\n location = nodes[location].link[c - MIN_CHAR];\n return max(location, 0);\n }\n\n void build(const vector<string> &words)\n {\n nodes = {node()};\n W = int(words.size());\n word_location.resize(W);\n defer.resize(W);\n int max_depth = 0;\n\n for (int i = 0; i < W; i++)\n {\n word_location[i] = add_word(words[i], i);\n max_depth = max(max_depth, int(words[i].size()));\n defer[i] = nodes[word_location[i]].word_index;\n }\n\n // depthの降順に単語indexのリストを作成\n word_indices_by_depth.resize(W);\n vector<int> depth_freq(max_depth + 1, 0);\n\n for (int i = 0; i < W; i++)\n depth_freq[words[i].size()]++;\n\n for (int i = max_depth - 1; i >= 0; i--)\n depth_freq[i] += depth_freq[i + 1];\n\n for (int i = 0; i < W; i++)\n word_indices_by_depth[--depth_freq[words[i].size()]] = i;\n\n // depth順のBFSでsuffix parentを求める\n vector<int> q = {0};\n\n for (int i = 0; i < int(q.size()); i++)\n {\n int current = q[i];\n\n for (char c = MIN_CHAR; c < MIN_CHAR + ALPHABET; c++)\n {\n int &index = nodes[current][c];\n if (index >= 0)\n {\n // currentのsuffix parentで子cを持つものが見つかるまで走査して\n // indexのsuffix parentを見つける\n int suffix_parent = get_suffix_link(nodes[current].suff, c);\n nodes[index].suff = suffix_parent;\n nodes[index].word_count += nodes[suffix_parent].word_count;\n nodes[index].dict = nodes[suffix_parent].word_index < 0 ? nodes[suffix_parent].dict : suffix_parent;\n q.push_back(index);\n }\n else\n {\n index = get_suffix_link(nodes[current].suff, c);\n }\n }\n }\n }\n\n // 辞書内のそれぞれの単語がtextに何個含まれているか O(text length + num words)\n vector<int> count_matches(const string &text) const\n {\n vector<int> matches(W, 0);\n int current = 0;\n\n for (char c : text)\n {\n current = get_suffix_link(current, c);\n int dict_node = nodes[current].word_index < 0 ? nodes[current].dict : current;\n\n if (dict_node >= 0)\n matches[nodes[dict_node].word_index]++;\n }\n\n // depthの降順に見る\n for (int word_index : word_indices_by_depth)\n {\n int location = word_location[word_index];\n int dict_node = nodes[location].dict;\n\n if (dict_node >= 0)\n matches[nodes[dict_node].word_index] += matches[word_index];\n }\n\n for (int i = 0; i < W; i++)\n matches[i] = matches[defer[i]];\n\n return matches;\n }\n\n // textに含まれる辞書内の単語で、textのi文字目で終わるものの個数 O(text length)\n vector<int> count_matches_by_position(const string &text) const\n {\n vector<int> matches(text.size());\n int current = 0;\n\n for (int i = 0; i < int(text.size()); i++)\n {\n current = get_suffix_link(current, text[i]);\n matches[i] = nodes[current].word_count;\n }\n\n return matches;\n }\n\n // textに辞書内の単語が合計何個含まれているか O(text length)\n int64_t count_total_matches(const string &text) const\n {\n int64_t matches = 0;\n int current = 0;\n\n for (char c : text)\n {\n current = get_suffix_link(current, c);\n matches += nodes[current].word_count;\n }\n\n return matches;\n }\n};\n\nvoid solve(int n, int m, int k)\n{\n vector<string> from(n), to(n);\n rep(i, n) cin >> from[i] >> to[i];\n map<string, int> mp;\n for (auto &&s : from)\n mp[s] = 0;\n for (auto &&s : to)\n mp[s] = 0;\n mp[\"\"] = 0;\n int z = 0;\n for (auto &&[k, v] : mp)\n v = z++;\n\n vector<string> mpr(z);\n for (auto &&[k, v] : mp)\n mpr[v] = k;\n\n vector E(z, vector<int>());\n rep(i, n)\n {\n E[mp[from[i]]].push_back(mp[to[i]]);\n }\n REP(i, 1, z)\n {\n E[0].push_back(i);\n }\n\n vector<string> seasonword(k);\n rep(i, k) cin >> seasonword[i];\n AhoCorasick<> aho(seasonword);\n\n auto next_state = [&](int state, int nxt_vertex) {\n int cnt = 0;\n for (auto c : mpr[nxt_vertex])\n {\n state = aho.get_suffix_link(state, c);\n cnt += aho.nodes[state].word_count;\n if (cnt > 1)\n {\n cnt = 2;\n state = -1;\n break;\n }\n }\n return MP(state, cnt);\n };\n\n vector nxt(aho.nodes.size(), vector<int>(z));\n vector season(aho.nodes.size(), vector<int>(z));\n map<tuple<int, int, int, bool>, mint> dp;\n\n rep(state, aho.nodes.size())\n {\n REP(nxt_vertex, 1, z)\n {\n auto [p, q] = next_state(state, nxt_vertex);\n nxt[state][nxt_vertex] = p;\n season[state][nxt_vertex] = q;\n }\n }\n\n dp[MT(0, 0, 0, false)] = 1;\n\n for (auto [key, val] : dp)\n {\n auto [i, vertex, state, l] = key;\n for (auto nxt_vertex : E[vertex])\n {\n if (season[state][nxt_vertex] == 2)\n continue;\n int j = i + mpr[nxt_vertex].length();\n if (j > m)\n continue;\n if (season[state][nxt_vertex] == 1)\n {\n if (l == 0)\n dp[MT(j, nxt_vertex, nxt[state][nxt_vertex], true)] += val;\n }\n else\n {\n if (l == 0)\n dp[MT(j, nxt_vertex, nxt[state][nxt_vertex], false)] += val;\n else if (l == 1)\n dp[MT(j, nxt_vertex, nxt[state][nxt_vertex], true)] += val;\n }\n }\n }\n\n mint ans = 0;\n rep(vertex, z) rep(state, aho.nodes.size())\n {\n if (dp.count(MT(m, vertex, state, true)))\n ans += dp[MT(m, vertex, state, true)];\n }\n cout << ans << \"\\n\";\n}\n\nint main()\n{\n int n, m, k;\n while (1)\n {\n cin >> n >> m >> k;\n if (n == 0)\n break;\n solve(n, m, k);\n }\n}", "accuracy": 1, "time_ms": 2600, "memory_kb": 201392, "score_of_the_acc": -1.3257, "final_rank": 15 }, { "submission_id": "aoj_2257_5451323", "code_snippet": "#define MOD_TYPE 1\n\n#pragma region Macros\n\n#include <bits/stdc++.h>\nusing namespace std;\n\n#if 0\n#include <boost/multiprecision/cpp_int.hpp>\n#include <boost/multiprecision/cpp_dec_float.hpp>\nusing Int = boost::multiprecision::cpp_int;\nusing lld = boost::multiprecision::cpp_dec_float_100;\n#endif\n#if 0\n#pragma GCC target(\"avx2\")\n#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"unroll-loops\")\n#endif\nusing ll = long long int;\nusing ld = long double;\nusing pii = pair<int, int>;\nusing pll = pair<ll, ll>;\nusing pld = pair<ld, ld>;\ntemplate <typename Q_type>\nusing smaller_queue = priority_queue<Q_type, vector<Q_type>, greater<Q_type>>;\n\nconstexpr ll MOD = (MOD_TYPE == 1 ? (ll)(1e9 + 7) : 998244353);\nconstexpr int INF = (int)1e9 + 10;\nconstexpr ll LINF = (ll)4e18;\nconstexpr ld PI = acos(-1.0);\nconstexpr ld EPS = 1e-7;\nconstexpr int Dx[] = {0, 0, -1, 1, -1, 1, -1, 1, 0};\nconstexpr int Dy[] = {1, -1, 0, 0, -1, -1, 1, 1, 0};\n\n#define REP(i, m, n) for (ll i = m; i < (ll)(n); ++i)\n#define rep(i, n) REP(i, 0, n)\n#define REPI(i, m, n) for (int i = m; i < (int)(n); ++i)\n#define repi(i, n) REPI(i, 0, n)\n#define MP make_pair\n#define MT make_tuple\n#define YES(n) cout << ((n) ? \"YES\" : \"NO\") << \"\\n\"\n#define Yes(n) cout << ((n) ? \"Yes\" : \"No\") << \"\\n\"\n#define possible(n) cout << ((n) ? \"possible\" : \"impossible\") << \"\\n\"\n#define Possible(n) cout << ((n) ? \"Possible\" : \"Impossible\") << \"\\n\"\n#define all(v) v.begin(), v.end()\n#define NP(v) next_permutation(all(v))\n#define dbg(x) cerr << #x << \":\" << x << \"\\n\";\n\nstruct io_init\n{\n io_init()\n {\n cin.tie(0);\n ios::sync_with_stdio(false);\n cout << setprecision(30) << setiosflags(ios::fixed);\n };\n} io_init;\ntemplate <typename T>\ninline bool chmin(T &a, T b)\n{\n if (a > b)\n {\n a = b;\n return true;\n }\n return false;\n}\ntemplate <typename T>\ninline bool chmax(T &a, T b)\n{\n if (a < b)\n {\n a = b;\n return true;\n }\n return false;\n}\ninline ll CEIL(ll a, ll b)\n{\n return (a + b - 1) / b;\n}\ntemplate <typename A, size_t N, typename T>\ninline void Fill(A (&array)[N], const T &val)\n{\n fill((T *)array, (T *)(array + N), val);\n}\ntemplate <typename T, typename U>\nconstexpr istream &operator>>(istream &is, pair<T, U> &p) noexcept\n{\n is >> p.first >> p.second;\n return is;\n}\ntemplate <typename T, typename U>\nconstexpr ostream &operator<<(ostream &os, pair<T, U> &p) noexcept\n{\n os << p.first << \" \" << p.second;\n return os;\n}\n#pragma endregion\n\n// --------------------------------------\n\n#pragma region mint\ntemplate <int MOD>\nstruct Fp\n{\n int val;\n\n constexpr Fp(int v = 0) noexcept : val(v % MOD)\n {\n if (val < 0)\n v += MOD;\n }\n\n constexpr int getmod()\n {\n return MOD;\n }\n\n constexpr Fp operator-() const noexcept\n {\n return val ? MOD - val : 0;\n }\n\n constexpr Fp operator+(const Fp &r) const noexcept\n {\n return Fp(*this) += r;\n }\n\n constexpr Fp operator-(const Fp &r) const noexcept\n {\n return Fp(*this) -= r;\n }\n\n constexpr Fp operator*(const Fp &r) const noexcept\n {\n return Fp(*this) *= r;\n }\n\n constexpr Fp operator/(const Fp &r) const noexcept\n {\n return Fp(*this) /= r;\n }\n\n constexpr Fp &operator+=(const Fp &r) noexcept\n {\n val += r.val;\n if (val >= MOD)\n val -= MOD;\n return *this;\n }\n\n constexpr Fp &operator-=(const Fp &r) noexcept\n {\n val -= r.val;\n if (val < 0)\n val += MOD;\n return *this;\n }\n\n constexpr Fp &operator*=(const Fp &r) noexcept\n {\n val = val * r.val % MOD;\n if (val < 0)\n val += MOD;\n return *this;\n }\n\n constexpr Fp &operator/=(const Fp &r) noexcept\n {\n long long a = r.val, b = MOD, u = 1, v = 0;\n while (b)\n {\n long long t = a / b;\n a -= t * b;\n swap(a, b);\n u -= t * v;\n swap(u, v);\n }\n val = val * u % MOD;\n if (val < 0)\n val += MOD;\n return *this;\n }\n\n constexpr bool operator==(const Fp &r) const noexcept\n {\n return this->val == r.val;\n }\n\n constexpr bool operator!=(const Fp &r) const noexcept\n {\n return this->val != r.val;\n }\n\n friend constexpr ostream &operator<<(ostream &os, const Fp<MOD> &x) noexcept\n {\n return os << x.val;\n }\n\n friend constexpr istream &operator>>(istream &is, Fp<MOD> &x) noexcept\n {\n return is >> x.val;\n }\n};\n\nFp<MOD> modpow(const Fp<MOD> &a, long long n) noexcept\n{\n if (n == 0)\n return 1;\n auto t = modpow(a, n / 2);\n t = t * t;\n if (n & 1)\n t = t * a;\n return t;\n}\n\nusing mint = Fp<MOD>;\n#pragma endregion\n\ntemplate <char MIN_CHAR = 'a', int ALPHABET = 26>\nstruct AhoCorasick\n{\n struct node\n {\n // suff : 先頭の文字を最小限消してグラフに存在する頂点にするときの行先の頂点\n // dict : 先頭の文字を最小限消して辞書に存在する単語にするときの行先の頂点\n // depth : Trie木における深さ(省略可能)\n // word_index : このノードで終わる単語のindex(祖先は含まない。なければ-1)(複数ある場合は最小のもの)\n // word_count : このノードで終わる単語の総数\n // link : Trie及びsuffixの辺の接続先頂点(なければ-1)\n int suff = -1, dict = -1, depth = 0;\n int word_index = -1, word_count = 0;\n int link[ALPHABET];\n node() { fill(link, link + ALPHABET, -1); }\n int &operator[](char c) { return link[c - MIN_CHAR]; }\n };\n\n // nodes : 頂点集合\n // W : 現在の単語数\n // word_location : 各単語のTrie木の最後の頂点のindex\n // defer : 同じ単語が辞書内に存在する場合、最初の単語のindexを記録\n vector<node> nodes;\n int W;\n vector<int> word_location;\n vector<int> word_indices_by_depth;\n vector<int> defer;\n\n AhoCorasick(){};\n AhoCorasick(const vector<string> &words = {})\n {\n build(words);\n }\n\n // suffixを親とする木の隣接リスト これの上でDPやクエリ処理を行うことが多い\n vector<vector<int>> build_suffix_adj() const\n {\n vector<vector<int>> adj(nodes.size());\n for (int i = 1; i < int(nodes.size()); i++)\n adj[nodes[i].suff].push_back(i);\n return adj;\n }\n\n int get_or_add_child(int current, char c)\n {\n if (nodes[current][c] >= 0)\n return nodes[current][c];\n int index = int(nodes.size());\n nodes[current][c] = index;\n nodes.emplace_back();\n nodes.back().depth = nodes[current].depth + 1;\n return index;\n }\n\n int add_word(const string &word, int word_index)\n {\n assert(!nodes.empty());\n int current = 0;\n for (char c : word)\n current = get_or_add_child(current, c);\n if (nodes[current].word_index < 0)\n nodes[current].word_index = word_index;\n nodes[current].word_count++;\n return current;\n }\n\n // locationからcを追加したときの行き先 O(1)\n int get_suffix_link(int location, char c) const\n {\n if (location >= 0)\n location = nodes[location].link[c - MIN_CHAR];\n return max(location, 0);\n }\n\n void build(const vector<string> &words)\n {\n nodes = {node()};\n W = int(words.size());\n word_location.resize(W);\n defer.resize(W);\n int max_depth = 0;\n\n for (int i = 0; i < W; i++)\n {\n word_location[i] = add_word(words[i], i);\n max_depth = max(max_depth, int(words[i].size()));\n defer[i] = nodes[word_location[i]].word_index;\n }\n\n // depthの降順に単語indexのリストを作成\n word_indices_by_depth.resize(W);\n vector<int> depth_freq(max_depth + 1, 0);\n\n for (int i = 0; i < W; i++)\n depth_freq[words[i].size()]++;\n\n for (int i = max_depth - 1; i >= 0; i--)\n depth_freq[i] += depth_freq[i + 1];\n\n for (int i = 0; i < W; i++)\n word_indices_by_depth[--depth_freq[words[i].size()]] = i;\n\n // depth順のBFSでsuffix parentを求める\n vector<int> q = {0};\n\n for (int i = 0; i < int(q.size()); i++)\n {\n int current = q[i];\n\n for (char c = MIN_CHAR; c < MIN_CHAR + ALPHABET; c++)\n {\n int &index = nodes[current][c];\n if (index >= 0)\n {\n // currentのsuffix parentで子cを持つものが見つかるまで走査して\n // indexのsuffix parentを見つける\n int suffix_parent = get_suffix_link(nodes[current].suff, c);\n nodes[index].suff = suffix_parent;\n nodes[index].word_count += nodes[suffix_parent].word_count;\n nodes[index].dict = nodes[suffix_parent].word_index < 0 ? nodes[suffix_parent].dict : suffix_parent;\n q.push_back(index);\n }\n else\n {\n index = get_suffix_link(nodes[current].suff, c);\n }\n }\n }\n }\n\n // 辞書内のそれぞれの単語がtextに何個含まれているか O(text length + num words)\n vector<int> count_matches(const string &text) const\n {\n vector<int> matches(W, 0);\n int current = 0;\n\n for (char c : text)\n {\n current = get_suffix_link(current, c);\n int dict_node = nodes[current].word_index < 0 ? nodes[current].dict : current;\n\n if (dict_node >= 0)\n matches[nodes[dict_node].word_index]++;\n }\n\n // depthの降順に見る\n for (int word_index : word_indices_by_depth)\n {\n int location = word_location[word_index];\n int dict_node = nodes[location].dict;\n\n if (dict_node >= 0)\n matches[nodes[dict_node].word_index] += matches[word_index];\n }\n\n for (int i = 0; i < W; i++)\n matches[i] = matches[defer[i]];\n\n return matches;\n }\n\n // textに含まれる辞書内の単語で、textのi文字目で終わるものの個数 O(text length)\n vector<int> count_matches_by_position(const string &text) const\n {\n vector<int> matches(text.size());\n int current = 0;\n\n for (int i = 0; i < int(text.size()); i++)\n {\n current = get_suffix_link(current, text[i]);\n matches[i] = nodes[current].word_count;\n }\n\n return matches;\n }\n\n // textに辞書内の単語が合計何個含まれているか O(text length)\n int64_t count_total_matches(const string &text) const\n {\n int64_t matches = 0;\n int current = 0;\n\n for (char c : text)\n {\n current = get_suffix_link(current, c);\n matches += nodes[current].word_count;\n }\n\n return matches;\n }\n};\n\nmint dp[21][501][601][2];\n\nvoid solve(int n, int m, int k)\n{\n vector<string> from(n), to(n);\n rep(i, n) cin >> from[i] >> to[i];\n map<string, int> mp;\n for (auto &&s : from)\n mp[s] = 0;\n for (auto &&s : to)\n mp[s] = 0;\n mp[\"\"] = 0;\n int z = 0;\n for (auto &&[k, v] : mp)\n v = z++;\n\n vector<string> mpr(z);\n for (auto &&[k, v] : mp)\n mpr[v] = k;\n\n vector Erev(z, vector<int>());\n rep(i, n)\n {\n Erev[mp[to[i]]].push_back(mp[from[i]]);\n }\n REP(i, 1, z)\n {\n Erev[i].push_back(0);\n }\n\n vector<string> seasonword(k);\n rep(i, k) cin >> seasonword[i];\n AhoCorasick<> aho(seasonword);\n\n auto next_state = [&](int state, int nxt_vertex) {\n int cnt = 0;\n for (auto c : mpr[nxt_vertex])\n {\n state = aho.get_suffix_link(state, c);\n cnt += aho.nodes[state].word_count;\n if (cnt > 1)\n {\n cnt = 2;\n state = -1;\n break;\n }\n }\n return MP(state, cnt);\n };\n\n vector nxt(aho.nodes.size(), vector<int>(z));\n vector season(aho.nodes.size(), vector<int>(z));\n\n rep(state, aho.nodes.size())\n {\n REP(nxt_vertex, 1, z)\n {\n auto [p, q] = next_state(state, nxt_vertex);\n nxt[state][nxt_vertex] = p;\n season[state][nxt_vertex] = q;\n }\n }\n\n rep(i, 21) rep(j, z) rep(k, aho.nodes.size()) rep(l, 2) dp[i][j][k][l] = 0;\n dp[0][0][0][0] = 1;\n REP(i, 1, m + 1)\n {\n rep(j, z) rep(k, aho.nodes.size()) rep(l, 2) dp[i % 21][j][k][l] = 0;\n rep(vertex, z)\n {\n rep(state, aho.nodes.size())\n {\n for (auto prev_vertex : Erev[vertex])\n {\n if (season[state][vertex] == 2)\n continue;\n int j = (i - int(mpr[vertex].length())) % 21;\n if (j < 0)\n j += 21;\n if (season[state][vertex] == 1)\n {\n dp[i % 21][vertex][nxt[state][vertex]][1] += dp[j][prev_vertex][state][0];\n }\n else\n {\n dp[i % 21][vertex][nxt[state][vertex]][0] += dp[j][prev_vertex][state][0];\n dp[i % 21][vertex][nxt[state][vertex]][1] += dp[j][prev_vertex][state][1];\n }\n }\n }\n }\n }\n\n mint ans = 0;\n rep(vertex, z) rep(state, aho.nodes.size())\n {\n ans += dp[m % 21][vertex][state][1];\n }\n cout << ans << \"\\n\";\n}\n\nint main()\n{\n int n, m, k;\n while (1)\n {\n cin >> n >> m >> k;\n if (n == 0)\n break;\n solve(n, m, k);\n }\n}", "accuracy": 1, "time_ms": 1770, "memory_kb": 55176, "score_of_the_acc": -0.4534, "final_rank": 3 }, { "submission_id": "aoj_2257_5409285", "code_snippet": "#include<iostream>\n#include<string>\n#include<cstdio>\n#include<vector>\n#include<cmath>\n#include<algorithm>\n#include<functional>\n#include<iomanip>\n#include<queue>\n#include<ciso646>\n#include<random>\n#include<map>\n#include<set>\n#include<bitset>\n#include<stack>\n#include<unordered_map>\n#include<unordered_set>\n#include<utility>\n#include<cassert>\n#include<complex>\n#include<numeric>\n#include<array>\nusing namespace std;\n\n//#define int long long\ntypedef long long ll;\n\ntypedef unsigned long long ul;\ntypedef unsigned int ui;\nconstexpr ll mod = 1000000007;\nconst ll INF = mod * mod;\ntypedef pair<int, int>P;\n#define stop char nyaa;cin>>nyaa;\n#define rep(i,n) for(int i=0;i<n;i++)\n#define per(i,n) for(int i=n-1;i>=0;i--)\n#define Rep(i,sta,n) for(int i=sta;i<n;i++)\n#define rep1(i,n) for(int i=1;i<=n;i++)\n#define per1(i,n) for(int i=n;i>=1;i--)\n#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)\n#define all(v) (v).begin(),(v).end()\ntypedef pair<ll, ll> LP;\ntypedef long double ld;\ntypedef pair<ld, ld> LDP;\nconst ld eps = 1e-12;\nconst ld pi = acosl(-1.0);\n\nll mod_pow(ll x, ll n, ll m = mod) {\n\tif (n < 0) {\n\t\tll res = mod_pow(x, -n, m);\n\t\treturn mod_pow(res, m - 2, m);\n\t}\n\tif (abs(x) >= m)x %= m;\n\tif (x < 0)x += m;\n\tll res = 1;\n\twhile (n) {\n\t\tif (n & 1)res = res * x % m;\n\t\tx = x * x % m; n >>= 1;\n\t}\n\treturn res;\n}\nstruct modint {\n\tll n;\n\tmodint() :n(0) { ; }\n\tmodint(ll m) :n(m) {\n\t\tif (n >= mod)n %= mod;\n\t\telse if (n < 0)n = (n % mod + mod) % mod;\n\t}\n\toperator int() { return n; }\n};\nbool operator==(modint a, modint b) { return a.n == b.n; }\nmodint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }\nmodint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }\nmodint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }\nmodint operator+(modint a, modint b) { return a += b; }\nmodint operator-(modint a, modint b) { return a -= b; }\nmodint operator*(modint a, modint b) { return a *= b; }\nmodint operator^(modint a, ll n) {\n\tif (n == 0)return modint(1);\n\tmodint res = (a * a) ^ (n / 2);\n\tif (n % 2)res = res * a;\n\treturn res;\n}\n\nll inv(ll a, ll p) {\n\treturn (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);\n}\nmodint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }\nmodint operator/=(modint& a, modint b) { a = a / b; return a; }\nconst int max_n = 1 << 18;\nmodint fact[max_n], factinv[max_n];\nvoid init_f() {\n\tfact[0] = modint(1);\n\tfor (int i = 0; i < max_n - 1; i++) {\n\t\tfact[i + 1] = fact[i] * modint(i + 1);\n\t}\n\tfactinv[max_n - 1] = modint(1) / fact[max_n - 1];\n\tfor (int i = max_n - 2; i >= 0; i--) {\n\t\tfactinv[i] = factinv[i + 1] * modint(i + 1);\n\t}\n}\nmodint comb(int a, int b) {\n\tif (a < 0 || b < 0 || a < b)return 0;\n\treturn fact[a] * factinv[b] * factinv[a - b];\n}\nmodint combP(int a, int b) {\n\tif (a < 0 || b < 0 || a < b)return 0;\n\treturn fact[a] * factinv[a - b];\n}\n\nint dx[4] = { 1,0,-1,0 };\nint dy[4] = { 0,1,0,-1 };\n\nmodint dp[605][21][505][2];\nstruct AhoCorasick {\n\tstatic const int SIZE = 27;\n\tstruct State {\n\t\tint index, next[SIZE];\n\t\t//vector<int> accept;\n\t\tState(int index) : index(index) {\n\t\t\tfor (int i = 0; i < SIZE; i++)next[i] = -1;\n\t\t}\n\t};\n\tvector<State> pma;\n\t//vector<int> lens;\n\n\tAhoCorasick(const vector<string>& str, char base_c = 'a') {\n\t\tpma.clear();\n\t\tpma.push_back(State(0));\n\t\t//lens.clear();\n\t\trep(i, str.size()) {\n\t\t\tint t = 0;\n\t\t\tfor (char c : str[i]) {\n\t\t\t\tif (pma[t].next[c - base_c] == -1) {\n\t\t\t\t\tint m = pma.size();\n\t\t\t\t\tpma[t].next[c - base_c] = m;\n\t\t\t\t\tpma.push_back(State(m));\n\t\t\t\t}\n\t\t\t\tt = pma[t].next[c - base_c];\n\t\t\t}\n\t\t\t//pma[t].accept.push_back(lens.size());\n\t\t\t//lens.push_back(str[i].size());\n\t\t}\n\t\tqueue<int> que;\n\t\tfor (int c = 0; c < SIZE - 1; c++) {\n\t\t\tif (pma[0].next[c] != -1) {\n\t\t\t\tpma[pma[0].next[c]].next[SIZE - 1] = 0;\n\t\t\t\tque.push(pma[0].next[c]);\n\t\t\t}\n\t\t\telse {\n\t\t\t\tpma[0].next[c] = 0;\n\t\t\t}\n\t\t}\n\t\twhile (!que.empty()) {\n\t\t\tint t = que.front();\n\t\t\tque.pop();\n\t\t\tfor (int c = 0; c < SIZE - 1; c++) {\n\t\t\t\tif (pma[t].next[c] != -1) {\n\t\t\t\t\tque.push(pma[t].next[c]);\n\t\t\t\t\tint r = pma[t].next[SIZE - 1];\n\t\t\t\t\twhile (pma[r].next[c] == -1) r = pma[r].next[SIZE - 1];\n\t\t\t\t\tpma[pma[t].next[c]].next[SIZE - 1] = pma[r].next[c];\n\t\t\t\t\t//for (int i : pma[pma[r].next[c]].accept)\n\t\t\t\t\t//\tpma[pma[t].next[c]].accept.push_back(i);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tint sub(int index, int c) {\n\t\treturn pma[index].next[c] != -1 ?\n\t\t\tpma[index].next[c] :\n\t\t\tpma[index].next[c] = sub(pma[index].next[SIZE - 1], c);\n\t}\n\tvector<int> fincnt;\n\tvoid init_fin(vector<string> s) {\n\t\tfincnt.resize(pma.size(), 0);\n\t\trep(i, s.size()) {\n\t\t\tint loc = 0;\n\t\t\trep(j, s[i].size()) {\n\t\t\t\tloc = pma[loc].next[s[i][j] - 'a'];\n\t\t\t}\n\t\t\tfincnt[loc] = true;\n\t\t}\n\t\trep(i, pma.size()) {\n\t\t\tif (pma[i].next[26] >= 0) {\n\t\t\t\tfincnt[i] += fincnt[pma[i].next[26]];\n\t\t\t}\n\t\t}\n\t}\n\tint query(vector<string> a,vector<string> b, int m) {\n\t\tvector<string> s;\n\t\trep(i, a.size()) {\n\t\t\ts.push_back(a[i]);\n\t\t\ts.push_back(b[i]);\n\t\t}\n\t\tsort(all(s));\n\t\ts.erase(unique(all(s)), s.end());\n\t\tint n = s.size();\n\t\tvector<vector<int>> G(n);\n\t\trep(i, a.size()) {\n\t\t\tint le = lower_bound(all(s), a[i]) - s.begin();\n\t\t\tint ri = lower_bound(all(s), b[i]) - s.begin();\n\t\t\tG[le].push_back(ri);\n\t\t}\n\t\tvector<vector<int>> nex(pma.size(), vector<int>(n));\n\t\tvector<vector<int>> ad(pma.size(), vector<int>(n));\n\t\trep(i, pma.size())rep(j, 21)rep(k, n)rep(l, 2)dp[i][j][k][l] = 0;\n\t\trep(i, pma.size()) {\n\t\t\trep(j, n) {\n\t\t\t\tint loc = i;\n\t\t\t\tint cnt = 0;\n\t\t\t\trep(k, s[j].size()) {\n\t\t\t\t\tloc = sub(loc, s[j][k] - 'a');\n\t\t\t\t\tcnt += fincnt[loc];\n\t\t\t\t}\n\t\t\t\tnex[i][j] = loc;\n\t\t\t\tad[i][j] = cnt;\n\t\t\t}\n\t\t}\n\t\trep(i, n) {\n\t\t\tint ni = nex[0][i];\n\t\t\tint nj = s[i].size();\n\t\t\tint nk = i;\n\t\t\tint nl = ad[0][i];\n\t\t\tif (nl < 2) {\n\t\t\t\tdp[ni][nj][nk][nl] += 1;\n\t\t\t}\n\t\t}\n\t\trep(j, m) {\n\t\t\trep(i, pma.size()) {\n\t\t\t\trep(k, n) {\n\t\t\t\t\trep(l, 2) {\n\t\t\t\t\t\tif (dp[i][j%21][k][l] == (modint)0)continue;\n\t\t\t\t\t\tfor (int t : G[k]) {\n\t\t\t\t\t\t\tint ni = nex[i][t];\n\t\t\t\t\t\t\tint nj = (j + s[t].size())%21;\n\t\t\t\t\t\t\tint nk = t;\n\t\t\t\t\t\t\tint nl = l + ad[i][t];\n\t\t\t\t\t\t\tif (nl < 2) {\n\t\t\t\t\t\t\t\tdp[ni][nj][nk][nl] += dp[i][j%21][k][l];\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t\tdp[i][j%21][k][l] = 0;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tmodint res = 0;\n\t\trep(i, pma.size())rep(j,n) {\n\t\t\tres += dp[i][m%21][j][1];\n\t\t}\n\t\treturn res;\n\t}\n};\n\n\n\nint n, m, k;\nvoid solve() {\n\tvector<string> a(n),b(n);\n\tvector<string> w(k);\n\trep(i, n)cin >> a[i] >> b[i];\n\trep(i, k)cin >> w[i];\n\tAhoCorasick aho(w);\n\taho.init_fin(w);\n\tint ans = aho.query(a, b, m);\n\tcout << ans << \"\\n\";\n}\n\nsigned main() {\n\tios::sync_with_stdio(false);\n\tcin.tie(0);\n\t//cout << fixed << setprecision(10);\n\t//init_f();\n\t//init();\n\t//expr();\n\t//int t; cin >> t;rep(i,t)\n\twhile(cin>>n>>m>>k,n)\n\tsolve();\n\treturn 0;\n}", "accuracy": 1, "time_ms": 430, "memory_kb": 109940, "score_of_the_acc": -0.5311, "final_rank": 5 }, { "submission_id": "aoj_2257_5314641", "code_snippet": "#include <cmath>\n#include <vector>\n#include <iostream>\n#include <iomanip>\n#include <fstream>\n#include <algorithm>\n#include <set>\n#include <queue>\n#include <string>\n#include <map>\n#include <stack>\n#include <climits>\n#include <array>\n#include <unordered_set>\n#include <unordered_map>\n#include <memory>\n#include <functional>\n#include <cfloat>\n#include <numeric>\n#include <random>\n#include <sstream>\n#include <bitset>\n#include <complex>\n#include <chrono>\n#include <cassert>\n\n\nclass AhoCorasick {\npublic:\n\tstruct Node;\nprivate:\n\tstd::shared_ptr<Node> root;\n\tNode* current;\n\tAhoCorasick(std::shared_ptr<Node>&& root, Node* current) : root{ std::move(root) }, current{ current }{};\n\tAhoCorasick(const std::shared_ptr<Node>& root, Node* current) : root{ root }, current{ current }{};\npublic:\n\tstatic AhoCorasick create(const std::vector<std::string>& str) {\n\t\tauto root = std::make_shared<Node>();\n\t\tfor (const auto& s : str) {\n\t\t\tNode* current = root.get();\n\t\t\tfor (const auto c : s) {\n\t\t\t\tif (current->child[c - 'a'] != nullptr) {\n\t\t\t\t\tcurrent = current->child[c - 'a'].get();\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tcurrent->child[c - 'a'] = std::make_unique<Node>();\n\t\t\t\t\tcurrent = current->child[c - 'a'].get();\n\t\t\t\t}\n\t\t\t}\n\t\t\tcurrent->match_count++;\n\t\t}\n\t\tstd::queue<Node*> queue;\n\t\tfor (auto c = 'a'; c <= 'z'; ++c) {\n\t\t\tif (root->child[c - 'a'] != nullptr) {\n\t\t\t\tconst auto child = root->child[c - 'a'].get();\n\t\t\t\tchild->fail = root.get();\n\t\t\t\tqueue.emplace(child);\n\t\t\t}\n\t\t}\n\t\twhile (!queue.empty()) {\n\t\t\tconst auto current = queue.front(); queue.pop();\n\t\t\tfor (auto c = 'a'; c <= 'z'; ++c) {\n\t\t\t\tif (current->child[c - 'a'] != nullptr) {\n\t\t\t\t\tconst auto child = current->child[c - 'a'].get();\n\t\t\t\t\tchild->fail = current->fail->next(c);\n\t\t\t\t\tchild->match_count += child->fail->match_count;\n\t\t\t\t\tqueue.push(child);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tauto ptr = root.get();\n\t\treturn AhoCorasick{ std::move(root), ptr };\n\n\t}\n\tstruct Node {\n\t\tint match_count{ 0 };\n\t\tNode* fail{ nullptr };\n\t\tstd::vector<std::unique_ptr<Node>> child;\n\t\tNode() : child(26) {};\n\t\tNode* next(const char c) {\n\t\t\tif (child[c - 'a'] == nullptr) {\n\t\t\t\treturn fail == nullptr ? this : fail->next(c);\n\t\t\t}\n\t\t\telse {\n\t\t\t\treturn child[c - 'a'].get();\n\t\t\t}\n\t\t}\n\t};\n\tAhoCorasick next(const char c) const {\n\t\treturn AhoCorasick(root, current->next(c));\n\t}\n\tNode* node() const {\n\t\treturn current;\n\t}\n};\nvoid solve(const int n, const int m, const int k){\n\tstd::vector<std::pair<std::string, std::string>> pairs(n);\n\tfor (auto& [from, to] : pairs) {\n\t\tstd::cin >> from >> to;\n\t}\n\tstd::vector<std::string> words;\n\tfor (const auto& [from, to] : pairs) {\n\t\twords.push_back(from);\n\t\twords.push_back(to);\n\t}\n\tstd::sort(words.begin(), words.end());\n\twords.erase(std::unique(words.begin(), words.end()), words.end());\n\tstd::unordered_map<std::string, int> to_index;\n\tfor (auto i = 0; i < words.size(); ++i) {\n\t\tto_index[words[i]] = i;\n\t}\n\tstd::vector<std::vector<int>> succ_word(words.size());\n\tfor (const auto& [from, to] : pairs) {\n\t\tsucc_word[to_index[from]].push_back(to_index[to]);\n\t}\n\tstd::vector<std::string> seasonwords(k);\n\tfor (auto& s : seasonwords) {\n\t\tstd::cin >> s;\n\t}\n\tAhoCorasick aho = AhoCorasick::create(seasonwords);\n\tstd::vector<std::vector<std::vector<std::unordered_map<AhoCorasick::Node*, long long int>>>> count(2, std::vector<std::vector<std::unordered_map<AhoCorasick::Node*, long long int>>>(words.size(), std::vector<std::unordered_map<AhoCorasick::Node*, long long int>>(m)));\n\tconst auto comparator = [](const std::tuple<int, int, int, AhoCorasick::Node*>& a, const std::tuple<int, int, int, AhoCorasick::Node*>& b) {\n\t\treturn std::get<2>(a) > std::get<2>(b);\n\t};\n\tstd::priority_queue<std::tuple<int, int, int, AhoCorasick::Node*>, std::vector<std::tuple<int, int, int, AhoCorasick::Node*>>, decltype(comparator)> queue(comparator);\n\tconstexpr long long int mod = 1000000007;\n\tlong long int result = 0;\n\tfor (auto i = 0; i < words.size(); ++i) {\n\t\tif (words[i].size() > m) continue;\n\t\tint match = 0;\n\t\tauto current = aho.node();\n\t\tfor (const auto& c : words[i]) {\n\t\t\tcurrent = current->next(c);\n\t\t\tmatch += current->match_count;\n\t\t}\n\t\tif (match > 1) continue;\n\t\tif (words[i].size() == m) {\n\t\t\tif (match == 1) ++result;\n\t\t}\n\t\telse {\n\t\t\tcount[match][i][words[i].size()][current] += 1;\n\t\t\tqueue.emplace(match, i, (int)words[i].size(), current);\n\t\t}\n\t}\n\twhile (!queue.empty()) {\n\t\tconst auto [match_count, from, length, node] = queue.top(); queue.pop();\n\t\tconst auto pat = count[match_count][from][length][node] % mod;\n\t\tfor (const auto to : succ_word[from]) {\n\t\t\tif (length + words[to].size() > m) continue;\n\t\t\tint match = match_count;\n\t\t\tauto current = node;\n\t\t\tfor (const auto& c : words[to]) {\n\t\t\t\tcurrent = current->next(c);\n\t\t\t\tmatch += current->match_count;\n\t\t\t}\n\t\t\tif (match > 1) continue;\n\t\t\tif (length + words[to].size() == m) {\n\t\t\t\tif (match == 1) \n\t\t\t\t\tresult = (result + pat) % mod;\n\t\t\t}\n\t\t\telse {\n\t\t\t\tif (count[match][to][length + words[to].size()].find(current) == count[match][to][length + words[to].size()].end()) {\n\t\t\t\t\tqueue.emplace(match, to, length + (int)words[to].size(), current);\n\t\t\t\t}\n\t\t\t\tcount[match][to][length + words[to].size()][current] += pat;\n\t\t\t}\n\t\t}\n\t}\n\tstd::cout << result << '\\n';\n\n}\nint main() {\n\twhile (true) {\n\t\tint n, m, k;\n\t\tstd::cin >> n >> m >> k;\n\t\tif (n == 0 && m == 0 && k == 0)\n\t\t\tbreak;\n\t\tsolve(n, m, k);\n\t}\n}\n\n//https://onlinejudge.u-aizu.ac.jp/challenges/sources/JAG/Prelim/2257?year=2011", "accuracy": 1, "time_ms": 2580, "memory_kb": 140644, "score_of_the_acc": -1.0123, "final_rank": 9 }, { "submission_id": "aoj_2257_5314619", "code_snippet": "#include <cmath>\n#include <vector>\n#include <iostream>\n#include <iomanip>\n#include <fstream>\n#include <algorithm>\n#include <set>\n#include <queue>\n#include <string>\n#include <map>\n#include <stack>\n#include <climits>\n#include <array>\n#include <unordered_set>\n#include <unordered_map>\n#include <memory>\n#include <functional>\n#include <cfloat>\n#include <numeric>\n#include <random>\n#include <sstream>\n#include <bitset>\n#include <complex>\n#include <chrono>\n#include <cassert>\n\n\nclass AhoCorasick {\npublic:\n\tstruct Node;\nprivate:\n\tstd::shared_ptr<Node> root;\n\tNode* current;\n\tAhoCorasick(std::shared_ptr<Node>&& root, Node* current) : root{ std::move(root) }, current{ current }{};\n\tAhoCorasick(const std::shared_ptr<Node>& root, Node* current) : root{ root }, current{ current }{};\npublic:\n\tstatic AhoCorasick create(const std::vector<std::string>& str) {\n\t\tauto root = std::make_shared<Node>();\n\t\tfor (const auto& s : str) {\n\t\t\tNode* current = root.get();\n\t\t\tfor (const auto c : s) {\n\t\t\t\tif (current->child[c - 'a'] != nullptr) {\n\t\t\t\t\tcurrent = current->child[c - 'a'].get();\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tcurrent->child[c - 'a'] = std::make_shared<Node>();\n\t\t\t\t\tcurrent = current->child[c - 'a'].get();\n\t\t\t\t}\n\t\t\t}\n\t\t\tcurrent->match_count++;\n\t\t}\n\t\tstd::queue<Node*> queue;\n\t\tfor (auto c = 'a'; c <= 'z'; ++c) {\n\t\t\tif (root->child[c - 'a'] != nullptr) {\n\t\t\t\tconst auto child = root->child[c - 'a'].get();\n\t\t\t\tchild->fail = root.get();\n\t\t\t\tqueue.emplace(child);\n\t\t\t}\n\t\t}\n\t\twhile (!queue.empty()) {\n\t\t\tconst auto current = queue.front(); queue.pop();\n\t\t\tfor (auto c = 'a'; c <= 'z'; ++c) {\n\t\t\t\tif (current->child[c - 'a'] != nullptr) {\n\t\t\t\t\tconst auto child = current->child[c - 'a'].get();\n\t\t\t\t\tchild->fail = current->fail->next(c);\n\t\t\t\t\tchild->match_count += child->fail->match_count;\n\t\t\t\t\tqueue.push(child);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tauto ptr = root.get();\n\t\treturn AhoCorasick{ std::move(root), ptr };\n\n\t}\n\tstruct Node {\n\t\tint match_count{ 0 };\n\t\tNode* fail{ nullptr };\n\t\tstd::vector<std::shared_ptr<Node>> child;\n\t\tNode() : child(26, nullptr) {};\n\t\tNode* next(const char c) {\n\t\t\tif (child[c - 'a'] == nullptr) {\n\t\t\t\treturn fail == nullptr ? this : fail->next(c);\n\t\t\t}\n\t\t\telse {\n\t\t\t\treturn child[c - 'a'].get();\n\t\t\t}\n\t\t}\n\t};\n\tAhoCorasick next(const char c) const {\n\t\treturn AhoCorasick(root, current->next(c));\n\t}\n\tNode* node() const {\n\t\treturn current;\n\t}\n};\nvoid solve(const int n, const int m, const int k){\n\tstd::vector<std::pair<std::string, std::string>> pairs(n);\n\tfor (auto& [from, to] : pairs) {\n\t\tstd::cin >> from >> to;\n\t}\n\tstd::vector<std::string> words;\n\tfor (const auto& [from, to] : pairs) {\n\t\twords.push_back(from);\n\t\twords.push_back(to);\n\t}\n\tstd::sort(words.begin(), words.end());\n\twords.erase(std::unique(words.begin(), words.end()), words.end());\n\tstd::unordered_map<std::string, int> to_index;\n\tfor (auto i = 0; i < words.size(); ++i) {\n\t\tto_index[words[i]] = i;\n\t}\n\tstd::vector<std::vector<int>> succ_word(words.size());\n\tfor (const auto& [from, to] : pairs) {\n\t\tsucc_word[to_index[from]].push_back(to_index[to]);\n\t}\n\tstd::vector<std::string> seasonwords(k);\n\tfor (auto& s : seasonwords) {\n\t\tstd::cin >> s;\n\t}\n\tAhoCorasick aho = AhoCorasick::create(seasonwords);\n\tstd::vector<std::vector<std::vector<std::unordered_map<AhoCorasick::Node*, long long int>>>> count(2, std::vector<std::vector<std::unordered_map<AhoCorasick::Node*, long long int>>>(words.size(), std::vector<std::unordered_map<AhoCorasick::Node*, long long int>>(m)));\n\tconst auto comparator = [](const std::tuple<int, int, int, AhoCorasick::Node*>& a, const std::tuple<int, int, int, AhoCorasick::Node*>& b) {\n\t\treturn std::get<2>(a) > std::get<2>(b);\n\t};\n\tstd::priority_queue<std::tuple<int, int, int, AhoCorasick::Node*>, std::vector<std::tuple<int, int, int, AhoCorasick::Node*>>, decltype(comparator)> queue(comparator);\n\tconstexpr long long int mod = 1000000007;\n\tlong long int result = 0;\n\tfor (auto i = 0; i < words.size(); ++i) {\n\t\tif (words[i].size() > m) continue;\n\t\tint match = 0;\n\t\tauto current = aho.node();\n\t\tfor (const auto& c : words[i]) {\n\t\t\tcurrent = current->next(c);\n\t\t\tmatch += current->match_count;\n\t\t}\n\t\tif (match > 1) continue;\n\t\tif (words[i].size() == m) {\n\t\t\tif (match == 1) ++result;\n\t\t}\n\t\telse {\n\t\t\tcount[match][i][words[i].size()][current] += 1;\n\t\t\tqueue.emplace(match, i, (int)words[i].size(), current);\n\t\t}\n\t}\n\twhile (!queue.empty()) {\n\t\tconst auto [match_count, from, length, node] = queue.top(); queue.pop();\n\t\tconst auto pat = count[match_count][from][length][node] % mod;\n\t\tfor (const auto to : succ_word[from]) {\n\t\t\tif (length + words[to].size() > m) continue;\n\t\t\tint match = match_count;\n\t\t\tauto current = node;\n\t\t\tfor (const auto& c : words[to]) {\n\t\t\t\tcurrent = current->next(c);\n\t\t\t\tmatch += current->match_count;\n\t\t\t}\n\t\t\tif (match > 1) continue;\n\t\t\tif (length + words[to].size() == m) {\n\t\t\t\tif (match == 1) \n\t\t\t\t\tresult = (result + pat) % mod;\n\t\t\t}\n\t\t\telse {\n\t\t\t\tif (count[match][to][length + words[to].size()].find(current) == count[match][to][length + words[to].size()].end()) {\n\t\t\t\t\tqueue.emplace(match, to, length + words[to].size(), current);\n\t\t\t\t}\n\t\t\t\tcount[match][to][length + words[to].size()][current] += pat;\n\t\t\t}\n\t\t}\n\t}\n\tstd::cout << result << '\\n';\n\n}\nint main() {\n\twhile (true) {\n\t\tint n, m, k;\n\t\tstd::cin >> n >> m >> k;\n\t\tif (n == 0 && m == 0 && k == 0)\n\t\t\tbreak;\n\t\tsolve(n, m, k);\n\t}\n}\n\n//https://onlinejudge.u-aizu.ac.jp/challenges/sources/JAG/Prelim/2257?year=2011", "accuracy": 1, "time_ms": 2590, "memory_kb": 140836, "score_of_the_acc": -1.0148, "final_rank": 10 } ]
aoj_2267_cpp
問題 I : ビット演算 いよいよ G○○gle Code Jam の世界大会が始まろうとしている. 前では P○tr, t○mek, SnapDrag○n をはじめとする, G○○gle の誇る最強のコーダー達が睨みを利かせている. 妙な動きを見せれば,命はないだろう. 目をつむり,コンテスト開始をじっと待つ. …妙だ,コンテストが始まらない. 目を開けてみると…なんということだ.部屋の全ての人間が,倒れている. いや違う,全てではない.一人の男,wata を除いてだ. wata 「やっと話せるぞ. 久しぶりだな,(iwi)」 (iwi) 「…お前は何者だ? 俺の知る wata は幼なじみ系美少女のはずが…」 wata 「まだそんな妄想に取り憑かれているのか!! 思い出せ!! 受け止めろ!! 彼はもうこの世界には居ないんだ!!」 はっ…!! 僕は全てを思い出していた. 僕は世界で最初にプログラミングコンテストで人を殺めたのだ. そして,僕が殺したのは他でもなく,最も仲の良かった友人の一人,kita_masa であった. kita_masa は自らをビット演算の達人と言い, あらゆる関数はビット演算で記述できると言う,少し変わった奴だった. そういえば今,ビット演算を用いて作成したい関数があるが, kita_masa にそれをお願いすることはできない. そこで,kita_masa の代わりになるプログラムを作成することにした. 問題 N 個の整数の組 ( x i , y i ) が与えられる. 全ての組に対して y i = f ( x i ) なる関数 f を制約の下で作ることを考える. 関数 f は,C 言語のソースコードとして以下のように表現できるものとする: uint32_t f(uint32_t x) { return 式; } ここで,uint32_t は 32 ビット符号無し整数を表す. また,式は以下の BNF で表現できるものとする: <expr> ::= "x" | <num> | "(~" <expr> ")" | "(" <expr> <op2> <expr> ")" <op2> ::= "&" | "|" | "^" | "+" | "-" | "*" <num> は 32 ビット符号無し整数で表現できる非負の整数を 10 進数で自然に (leading-zero 等を持たず) 表現したものとする. このような関数の式を出力するプログラムを作成せよ. 入力 入力の最初の行は 1 つの整数 N を含む. 続く N 行の i 行目は 2 つの整数 x i , y i を含む. 出力 条件を満たす式を出力せよ. 制約 1 ≤ N ≤ 8 0 ≤ x i , y i < 256 式は必ず,上記の BNF に従う書式で出力せよ.例え C 言語の表現として妥当であったとしても,空白や改行の挿入や括弧の削除などはしてはならない. 出力は 100000 文字を越えてはならない. 条件を満たす関数 f が常に存在するような入力が与えられる. 部分点 この問題の判定には,20 点分のテストケースのグループが設定されている. このグループに含まれるテストケースの入力は以下を満たす. '+', '-', '*' を用いないような正しい出力が存在する 入出力例 入力例1: 8 1 1 2 2 3 3 4 0 5 1 6 2 7 3 8 0 入力例 1 に対する出力の例: (x&3) 入力例2: 8 1 0 2 0 3 2 4 0 5 4 6 4 7 6 8 0 入力例 2 に対する出力の例: (x&(x-1)) 良く知られる最右の 1 ビットをオフにする操作である.
[ { "submission_id": "aoj_2267_8975919", "code_snippet": "#include <bits/stdc++.h>\n#define REP(i,n) for(int i=0; i<(int)(n); ++i)\n\nusing namespace std;\n\ntypedef unsigned uint;\nstring num(uint n){\n stringstream ss;\n ss << n;\n return ss.str();\n}\nstring negative(string expr){\n return \"(~\" + expr + \")\";\n}\nstring operate(string expr1, string expr2, char op){\n return \"(\" + expr1 + op + expr2 +\")\";\n}\nint main(){\n int N;\n while(cin >> N){\n vector<uint> x(N);\n vector<uint> y(N);\n REP(i, N) cin >> x[i] >> y[i];\n string ans = \"0\";\n for(int t = 0, s = 1; t < 8; t++, s<<=1){\n // add\n vector<uint> b(N); // lowest bit\n REP(i, N) b[i] = (y[i] >> t) & 1;\n //REP(i,N) cout << b[i] << \" \";\n //cout << endl;\n if(b == vector<uint>(N, 0)) continue;\n\n map<vector<uint>, string> e_dict;\n for(uint dx = 0; dx < 256; dx++){\n vector<uint> fb(N);\n\n //constant\n REP(i, N) fb[i] = dx >> t & 1;\n if(!e_dict.count(fb)) e_dict[fb] = num(dx);\n //REP(i,N) cout << fb[i] << \" \";\n // cout << num(dx) << \" \" << e_dict[fb] << endl;\n\n if(dx == 0){\n REP(i, N) fb[i] = x[i] >> t & 1;\n if(!e_dict.count(fb)) e_dict[fb] = \"x\";\n continue;\n }\n\n // add\n REP(i, N) fb[i] = (x[i] + dx) >> t & 1;\n if(!e_dict.count(fb)) e_dict[fb] = operate(\"x\", num(dx), '+');\n\n // sub\n REP(i, N) fb[i] = (x[i] - dx) >> t & 1;\n if(!e_dict.count(fb)) e_dict[fb] = operate(\"x\", num(dx), '-');\n\n // mul\n REP(i, N) fb[i] = (x[i] * dx) >> t & 1;\n if(!e_dict.count(fb)) e_dict[fb] = operate(\"x\", num(dx), '*');\n }\n bool update = true;\n while(update && !e_dict.count(b)){\n update = false;\n map<vector<uint>, string> new_dict;\n for(auto& p : e_dict)\n for(auto& q : e_dict){\n vector<uint> fb(N);\n REP(i, N) fb[i] = (p.first[i] & q.first[i]);\n if(!new_dict.count(fb)){\n update = true;\n new_dict[fb] = operate(p.second, q.second, '&');\n }\n REP(i, N) fb[i] = (p.first[i] | q.first[i]);\n if(!new_dict.count(fb)){\n update = true;\n new_dict[fb] = operate(p.second, q.second, '|');\n }\n REP(i, N) fb[i] = (p.first[i] ^ q.first[i]);\n if(!new_dict.count(fb)){\n update = true;\n new_dict[fb] = operate(p.second, q.second, '^');\n }\n }\n for(auto& p : e_dict){\n vector<uint> fb(N);\n REP(i, N) fb[i] = ~p.first[i];\n if(new_dict.count(fb)){\n update = true;\n new_dict[fb] = negative(p.second);\n }\n }\n if(update) e_dict.swap(new_dict);\n }\n assert(e_dict.count(b));\n ans = operate(ans, operate(e_dict[b], num(s), '&'), '|');\n }\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3532, "score_of_the_acc": -1, "final_rank": 2 }, { "submission_id": "aoj_2267_2662828", "code_snippet": "#include <bits/stdc++.h>\n#define REP(i, n) for (int i = 0; (i) < int(n); ++ (i))\nusing namespace std;\ntemplate <typename X, typename T> auto vectors(X x, T a) { return vector<T>(x, a); }\ntemplate <typename X, typename Y, typename Z, typename... Zs> auto vectors(X x, Y y, Z z, Zs... zs) { auto cont = vectors(y, z, zs...); return vector<decltype(cont)>(x, cont); }\n\nvoid check(int n, vector<uint32_t> const & x, vector<uint32_t> const & y) {\n auto distinguishable = vectors(n, n, bool());\n REP (k, 8) {\n REP (j, n) REP (i, j) if (i != j) {\n if ((x[i] & (1 << k)) != (x[j] & (1 << k))) {\n distinguishable[i][j] = true;\n }\n if ((y[i] & (1 << k)) != (y[j] & (1 << k))) {\n assert (distinguishable[i][j]);\n }\n }\n }\n}\n\nuint8_t kth_column(vector<uint32_t> const & x, int k) {\n int n = x.size();\n assert (n <= 8);\n uint8_t acc = 0;\n REP (i, n) {\n if (x[i] & (1 << k)) {\n acc |= 1 << i;\n }\n }\n return acc;\n}\n\nint main() {\n // input\n int n; cin >> n;\n vector<uint32_t> x(n), y(n);\n REP (i, n) cin >> x[i] >> y[i];\n // solve\n check(n, x, y);\n string result = \"0\";\n vector<string> memo(1 << n);\n memo[0] = \"0\";\n memo[(1 << n) - 1] = \"1\";\n REP (k, 8) {\n // prepare a queue\n queue<uint8_t> que;\n auto push = [&](uint8_t a, string const & s) {\n if (memo[a].empty()) {\n que.push(a);\n memo[a] = s;\n }\n };\n { // add a new primitive\n ostringstream oss;\n oss << \"(x&\" << (1 << k) << \")\";\n push(kth_column(x, k), oss.str());\n }\n // breath first search\n while (not que.empty()) {\n uint8_t a = que.front(); que.pop();\n string s = memo[a];\n {\n ostringstream oss;\n oss << \"((~\" << s << \")&\" << (1 << k) << \")\";\n push((~ a) & ((1 << n) - 1), oss.str());\n }\n REP (b, 1 << n) if (not memo[b].empty()) {\n string t = memo[b];\n push(a & b, \"(\" + s + \"&\" + t + \")\");\n push(a | b, \"(\" + s + \"|\" + t + \")\");\n push(a ^ b, \"(\" + s + \"^\" + t + \")\");\n }\n }\n // add to the result\n assert (not memo[kth_column(y, k)].empty());\n result = \"(\" + result + \"|\" + memo[kth_column(y, k)] + \")\";\n // lshift old items\n REP (a, 1 << n) if (not memo[a].empty()) {\n memo[a] = \"(\" + memo[a] + \"*2)\";\n }\n }\n // output\n cout << result << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3288, "score_of_the_acc": -1.8885, "final_rank": 4 }, { "submission_id": "aoj_2267_767744", "code_snippet": "#include <iostream>\n#include <sstream>\n#include <vector>\n#include <string>\n#include <map>\n#include <set>\n#include <algorithm>\n#include <stack>\n#include <queue>\n#include <deque>\n#include <cstdio>\n#include <cstring>\n#include <cstdlib>\n#include <cmath>\n#include <cassert>\n#define REP(i,n) for(int i=0; i<(int)(n); ++i)\n\nusing namespace std;\ntypedef long long ll;\n\nconst int INF = 1000000000;\nconst int MOD = 1000000007;\nconst double EPS = 1e-8;\ntypedef unsigned uint;\nstring num(uint n){\n stringstream ss;\n ss << n;\n return ss.str();\n}\nstring negative(string expr){\n return \"(~\" + expr + \")\";\n}\nstring operate(string expr1, string expr2, char op){\n return \"(\" + expr1 + op + expr2 +\")\";\n}\nint main(){\n int N;\n while(cin >> N){\n vector<uint> x(N);\n vector<uint> y(N);\n REP(i, N) cin >> x[i] >> y[i];\n string ans = \"0\";\n for(int t = 0, s = 1; t < 8; t++, s<<=1){\n // add\n vector<uint> b(N); // lowest bit\n REP(i, N) b[i] = (y[i] >> t) & 1;\n\n if(b == vector<uint>(N, 0)) continue;\n\n map<vector<uint>, string> e_dict;\n for(uint dx = 0; dx < 256; dx++){\n vector<uint> fb(N);\n\n //constant\n REP(i, N) fb[i] = dx >> t & 1;\n if(!e_dict.count(fb)) e_dict[fb] = num(dx);\n\n if(dx == 0){\n REP(i, N) fb[i] = x[i] >> t & 1;\n if(!e_dict.count(fb)) e_dict[fb] = \"x\";\n continue;\n }\n\n // add\n REP(i, N) fb[i] = (x[i] + dx) >> t & 1;\n if(!e_dict.count(fb)) e_dict[fb] = operate(\"x\", num(dx), '+');\n\n /*\n // sub\n REP(i, N) fb[i] = (x[i] - dx) >> t & 1;\n if(!e_dict.count(fb)) e_dict[fb] = operate(\"x\", num(dx), '-');\n\n // mul\n REP(i, N) fb[i] = (x[i] * dx) >> t & 1;\n if(!e_dict.count(fb)) e_dict[fb] = operate(\"x\", num(dx), '*');\n */\n }\n bool update = true;\n while(update && !e_dict.count(b)){\n update = false;\n map<vector<uint>, string> new_dict;\n for(auto& p : e_dict)\n for(auto& q : e_dict){\n vector<uint> fb(N);\n REP(i, N) fb[i] = (p.first[i] & q.first[i]);\n if(!new_dict.count(fb)){\n update = true;\n new_dict[fb] = operate(p.second, q.second, '&');\n }\n REP(i, N) fb[i] = (p.first[i] | q.first[i]);\n if(!new_dict.count(fb)){\n update = true;\n new_dict[fb] = operate(p.second, q.second, '|');\n }\n REP(i, N) fb[i] = (p.first[i] ^ q.first[i]);\n if(!new_dict.count(fb)){\n update = true;\n new_dict[fb] = operate(p.second, q.second, '^');\n }\n }\n for(auto& p : e_dict){\n vector<uint> fb(N);\n REP(i, N) fb[i] = ~p.first[i];\n if(new_dict.count(fb)){\n update = true;\n new_dict[fb] = negative(p.second);\n }\n }\n if(update) e_dict.swap(new_dict);\n }\n assert(e_dict.count(b));\n ans = operate(ans, operate(e_dict[b], num(s), '&'), '|');\n }\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 1372, "score_of_the_acc": -1.0128, "final_rank": 3 }, { "submission_id": "aoj_2267_767742", "code_snippet": "#include <iostream>\n#include <sstream>\n#include <vector>\n#include <string>\n#include <map>\n#include <set>\n#include <algorithm>\n#include <stack>\n#include <queue>\n#include <deque>\n#include <cstdio>\n#include <cstring>\n#include <cstdlib>\n#include <cmath>\n#include <cassert>\n#define REP(i,n) for(int i=0; i<(int)(n); ++i)\n\nusing namespace std;\ntypedef long long ll;\n\nconst int INF = 1000000000;\nconst int MOD = 1000000007;\nconst double EPS = 1e-8;\ntypedef unsigned uint;\nstring num(uint n){\n stringstream ss;\n ss << n;\n return ss.str();\n}\nstring negative(string expr){\n return \"(~\" + expr + \")\";\n}\nstring operate(string expr1, string expr2, char op){\n return \"(\" + expr1 + op + expr2 +\")\";\n}\nint main(){\n int N;\n while(cin >> N){\n vector<uint> x(N);\n vector<uint> y(N);\n REP(i, N) cin >> x[i] >> y[i];\n string ans = \"0\";\n for(int t = 0, s = 1; t < 8; t++, s<<=1){\n // add\n vector<uint> b(N); // lowest bit\n REP(i, N) b[i] = (y[i] >> t) & 1;\n\n if(b == vector<uint>(N, 0)) continue;\n\n map<vector<uint>, string> e_dict;\n for(uint dx = 0; dx < 256; dx++){\n vector<uint> fb(N);\n\n //constant\n REP(i, N) fb[i] = dx >> t & 1;\n if(!e_dict.count(fb)) e_dict[fb] = num(dx);\n\n if(dx == 0){\n REP(i, N) fb[i] = x[i] >> t & 1;\n if(!e_dict.count(fb)) e_dict[fb] = \"x\";\n continue;\n }\n\n // add\n REP(i, N) fb[i] = (x[i] + dx) >> t & 1;\n if(!e_dict.count(fb)) e_dict[fb] = operate(\"x\", num(dx), '+');\n\n // sub\n REP(i, N) fb[i] = (x[i] - dx) >> t & 1;\n if(!e_dict.count(fb)) e_dict[fb] = operate(\"x\", num(dx), '-');\n\n // mul\n REP(i, N) fb[i] = (x[i] * dx) >> t & 1;\n if(!e_dict.count(fb)) e_dict[fb] = operate(\"x\", num(dx), '*');\n }\n bool update = true;\n while(update && !e_dict.count(b)){\n update = false;\n map<vector<uint>, string> new_dict;\n for(auto& p : e_dict)\n for(auto& q : e_dict){\n vector<uint> fb(N);\n REP(i, N) fb[i] = (p.first[i] & q.first[i]);\n if(!new_dict.count(fb)){\n update = true;\n new_dict[fb] = operate(p.second, q.second, '&');\n }\n REP(i, N) fb[i] = (p.first[i] | q.first[i]);\n if(!new_dict.count(fb)){\n update = true;\n new_dict[fb] = operate(p.second, q.second, '|');\n }\n REP(i, N) fb[i] = (p.first[i] ^ q.first[i]);\n if(!new_dict.count(fb)){\n update = true;\n new_dict[fb] = operate(p.second, q.second, '^');\n }\n }\n for(auto& p : e_dict){\n vector<uint> fb(N);\n REP(i, N) fb[i] = ~p.first[i];\n if(new_dict.count(fb)){\n update = true;\n new_dict[fb] = negative(p.second);\n }\n }\n if(update) e_dict.swap(new_dict);\n }\n assert(e_dict.count(b));\n ans = operate(ans, operate(e_dict[b], num(s), '&'), '|');\n }\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 1344, "score_of_the_acc": -0.5, "final_rank": 1 } ]
aoj_2264_cpp
問題 F : 全域木 今,G○○gle Code Jam の地区大会が始まろうとしている. 左の席に座っている男の ID は wata と言うらしい. 東京大学時代の記憶に,似たような ID の仲間が居た覚えがある. しかし,僕の仲間は一人残さず美少女だったはずだ. 僕の記憶の中の wata は,マトロイドが好きだった. 特に,マトロイド交差が大好きで, 様々なマトロイド達を交差させることに一種の興奮すら覚えると言う少し変わった奴だった. マトロイドの理論の力を使えば, 与えられたグラフ上で辺を共有しない複数の全域木を求めることはとても簡単な問題だと言っていた気がする. しかし,特別なグラフに関しては, マトロイドのアルゴリズムを直接に適用するよりも高速なアルゴリズムがあるのではないか? 問題 N 個の頂点からなる完全グラフにおいて, 辺を共有しない全域木を K 個作成せよ. 完全グラフとは,全ての相異なる 2 頂点間に 1 本の辺を持つグラフである. 下図は,4 頂点の完全グラフの例である. 4 頂点の完全グラフ. 全域木とは,元のグラフの全ての頂点と一部の辺からなる木のことである. 木とは,連結かつ閉路を持たないグラフのことである. 下図は,4 頂点の完全グラフにおける,辺を共有しない 2 つの全域木である. 4 頂点の完全グラフの全域木の例. 4 頂点の完全グラフの全域木であり,前の例と辺を共有しないもの. 入力 入力は 1 行からなり, 2 つの整数 N , K が書かれている. 出力 条件を満たす K 個の全域木を作ることができない時,-1 とだけ出力せよ. 条件を満たす K 個の全域木を作ることができる時, K 個の全域木を改行で区切り出力せよ. 1 つの全域木は N - 1 行で表される. その i 行目には,その全域木の辺 i が結ぶ 2 つの頂点を表す 2 つの整数をスペースで区切り出力する. ここで,頂点は 1 から N までの整数で表すものとする. 制約 2 ≤ N ≤ 10000 1 ≤ K ≤ 100 部分点 この問題の判定には,20 点分のテストケースのグループが設定されている. このグループに含まれるテストケースの入力は以下を満たす. 1 ≤ N ≤ 8 入出力例 入出力例 1 入力例 1: 4 2 入力例 1 に対する出力の例: 1 2 1 4 2 3 1 3 2 4 3 4 この入出力例は問題文中の図と対応している. 入出力例 2 入力例 2: 4 3 入力例 2 に対する出力の例: -1
[ { "submission_id": "aoj_2264_8962765", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n int n,k;\n cin >> n >> k;\n if(k>n/2){\n cout << -1 << endl;\n return 0;\n }\n vector<vector<string>> ans(k);\n for(int i=0;i<n;i++){\n for(int j=i+1;j<n;j++){\n int id=((i+j)%n)/2;\n if(id<k){ \n string s=to_string(i+1)+\" \"+to_string(j+1)+\"\\n\";\n ans[id].push_back(s);\n }\n }\n }\n for(int i=0;i<k;i++){\n for(int j=0;j<n-1;j++) cout << ans[i][j];\n if(i!=k-1) cout << endl;\n }\n}", "accuracy": 1, "time_ms": 250, "memory_kb": 37448, "score_of_the_acc": -2, "final_rank": 20 }, { "submission_id": "aoj_2264_8860136", "code_snippet": "#include <iostream>\n#include <vector>\n#include <sstream>\nusing namespace std;\n\nint main() {\n int n, k;\n while (cin >> n >> k) {\n if (n / 2 < k) {\n cout << \"-1\" << endl;\n continue;\n }\n vector<int> p(n + 2);\n p[0] = 0;\n for (int i = 1; i < n; i += 2) {\n p[i] = i / 2 + 1;\n p[i + 1] = n - p[i];\n }\n stringstream ss;\n for (int i = 0; i < k; i++) {\n for (int j = 0; j < n - 1; j++) {\n int a = (p[j] + i) % n + 1;\n int b = (p[j + 1] + i) % n + 1;\n ss << ((i != 0 && j == 0) ? \"\\n\" : \"\") << a << \" \" << b << ((j < n - 2) ? \"\\n\" : \"\");\n }\n }\n cout << ss.str();\n }\n}", "accuracy": 1, "time_ms": 90, "memory_kb": 22228, "score_of_the_acc": -0.6722, "final_rank": 19 }, { "submission_id": "aoj_2264_8860135", "code_snippet": "#include <iostream>\n#include <vector>\nusing namespace std;\n\nint main() {\n int n, k;\n while (cin >> n >> k) {\n if (n / 2 < k) {\n cout << -1 << '\\n';\n continue;\n }\n \n vector<int> p(n + 2);\n p[0] = 0;\n for (int i = 1; i < n; i += 2) {\n p[i] = i / 2 + 1;\n p[i + 1] = n - p[i];\n }\n \n vector<int> a_values(n-1), b_values(n-1);\n for (int i = 0; i < k; i++) {\n if (i != 0)\n cout << '\\n';\n for (int j = 0; j < n - 1; j++) {\n a_values[j] = (p[j] + i) % n + 1;\n b_values[j] = (p[j + 1] + i) % n + 1;\n }\n for (int j = 0; j < n - 1; j++) {\n cout << a_values[j] << \" \" << b_values[j] << '\\n';\n }\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 3204, "score_of_the_acc": -0.1792, "final_rank": 17 }, { "submission_id": "aoj_2264_8860132", "code_snippet": "#include <iostream>\n#include <vector>\nusing namespace std;\n\nint main() {\n int n, k;\n while (cin >> n >> k) {\n if (n / 2 < k) {\n cout << \"-1\\n\";\n continue;\n }\n vector<int> p(n + 2);\n p[0] = 0;\n for (int i = 1; i < n; i += 2) {\n p[i] = i / 2 + 1;\n p[i + 1] = n - p[i];\n }\n for (int i = 0; i < k; i++) {\n if (i != 0)\n cout << \"\\n\";\n for (int j = 0; j < n - 1; j++) {\n int a = (p[j] + i) % n + 1;\n int b = (p[j + 1] + i) % n + 1;\n cout << a << \" \" << b << \"\\n\";\n }\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 3124, "score_of_the_acc": -0.1769, "final_rank": 14 }, { "submission_id": "aoj_2264_8860128", "code_snippet": "#include <iostream>\n#include <vector>\nusing namespace std;\n\nint main() {\n ios_base::sync_with_stdio(false); // boost performance when using cin and cout\n int n, k;\n vector<int> p; // dynamic array to avoid unnecessary initializations\n while (cin >> n >> k) {\n if (n / 2 < k) {\n cout << \"-1\\n\";\n continue;\n }\n if (p.size() < n + 2) // resize the array only if necessary\n p.resize(n + 2);\n for (int i = 1; i < n; i += 2) {\n p[i] = i / 2 + 1;\n p[i + 1] = n - p[i];\n }\n for (int i = 0; i < k; i++) {\n for (int j = 0; j < n - 1; j++) {\n int a = (p[j] + i) % n + 1;\n int b = (p[j + 1] + i) % n + 1;\n if (j != 0)\n cout << '\\n'; // use '\\n' instead of endl to avoid unnecessary buffer flushes\n cout << a << ' ' << b;\n }\n cout << '\\n'; // print a newline at the end of each block\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 3252, "score_of_the_acc": -0.0695, "final_rank": 9 }, { "submission_id": "aoj_2264_8860125", "code_snippet": "#include <iostream>\n#include <vector>\nusing namespace std;\n\nint main() {\n ios::sync_with_stdio(false); // Disable synchronization of C++ and C standard streams\n cin.tie(NULL); // Unties cin from cout\n\n int n, k;\n while (cin >> n >> k) {\n if (n / 2 < k) {\n cout << \"-1\\n\";\n continue;\n }\n vector<int> p(n + 2); // Use a vector instead of an array\n p[0] = 0;\n for (int i = 1; i < n; i += 2) {\n p[i] = i / 2 + 1;\n p[i + 1] = n - p[i];\n }\n for (int i = 0; i < k; i++) {\n if (i != 0)\n cout << \"\\n\";\n \n int a, b;\n for (int j = 0; j < n - 1; j++) {\n a = (p[j] + i) % n + 1;\n b = (p[j + 1] + i) % n + 1;\n cout << a << \" \" << b << \"\\n\";\n }\n }\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 3208, "score_of_the_acc": -0.0682, "final_rank": 6 }, { "submission_id": "aoj_2264_8820733", "code_snippet": "#include <iostream>\n#include <vector>\n#include <sstream>\nusing namespace std;\nint main() {\n int n, k;\n while (cin >> n >> k) {\n if (n / 2 < k) {\n cout << \"-1\\n\";\n continue;\n }\n vector<int> p(n + 2);\n p[0] = 0;\n for (int i = 1; i < n; i += 2) {\n p[i] = i / 2 + 1;\n p[i + 1] = n - p[i];\n }\n stringstream ss;\n for (int i = 0; i < k; i++) {\n if (i != 0)\n ss << \"\\n\";\n for (int j = 0; j < n - 1; j++) {\n int a = (p[j] + i) % n + 1;\n int b = (p[j + 1] + i) % n + 1;\n ss << a << \" \" << b << \"\\n\";\n }\n }\n cout << ss.str();\n }\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 22252, "score_of_the_acc": -0.6174, "final_rank": 18 }, { "submission_id": "aoj_2264_8820726", "code_snippet": "#include <iostream>\n#include <vector>\nusing namespace std;\n\nint main() {\n int n, k;\n while (cin >> n >> k) {\n if (n / 2 < k) {\n cout << \"-1\\n\";\n continue;\n }\n vector<int> p(n + 2);\n p[0] = 0;\n for (int i = 1; i < n; i += 2) {\n p[i] = i / 2 + 1;\n p[i + 1] = n - p[i];\n }\n for (int i = 0; i < k; i++) {\n vector<int> a(n - 1), b(n - 1);\n for (int j = 0; j < n - 1; j++) {\n a[j] = p[j] + i;\n if (a[j] >= n) a[j] -= n;\n a[j]++;\n b[j] = p[j + 1] + i;\n if (b[j] >= n) b[j] -= n;\n b[j]++;\n }\n for (int j = 0; j < n - 1; j++) {\n cout << a[j] << \" \" << b[j] << \"\\n\";\n }\n if (i != k - 1) cout << \"\\n\";\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 3196, "score_of_the_acc": -0.179, "final_rank": 16 }, { "submission_id": "aoj_2264_8820722", "code_snippet": "#include <iostream>\n#include <vector>\nusing namespace std;\n\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n \n int n, k;\n while (cin >> n >> k) {\n if (n / 2 < k) {\n cout << \"-1\\n\";\n continue;\n }\n vector<int> p(n + 2);\n p[0] = 0;\n for (int i = 1; i < n; i += 2) {\n p[i] = i / 2 + 1;\n p[i + 1] = n - p[i];\n }\n for (int i = 0; i < k; i++) {\n if (i != 0)\n cout << \"\\n\";\n for (int j = 0; j < n - 1; j++) {\n int a = (p[j] + i) % n + 1;\n int b = (p[j + 1] + i) % n + 1;\n cout << a << \" \" << b << \"\\n\";\n }\n }\n }\n}", "accuracy": 1, "time_ms": 90, "memory_kb": 3204, "score_of_the_acc": -0.1237, "final_rank": 10 }, { "submission_id": "aoj_2264_8820717", "code_snippet": "#include <iostream>\nusing namespace std;\n\nint main() {\n int n, k;\n while (cin >> n >> k) {\n if (n / 2 < k) {\n cout << \"-1\\n\";\n continue;\n }\n int* p = new int[n + 2];\n p[0] = 0;\n for (int i = 1; i < n; i += 2) {\n p[i] = i / 2 + 1;\n p[i + 1] = n - p[i];\n }\n for (int i = 0; i < k; i++) {\n if (i != 0)\n cout << \"\\n\";\n for (int j = 0; j < n - 1; j++) {\n int a = (p[j] + i) % n + 1;\n cout << a << \" \" << ((p[j + 1] + i) % n + 1) << \"\\n\";\n }\n }\n delete[] p;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 3112, "score_of_the_acc": -0.1766, "final_rank": 13 }, { "submission_id": "aoj_2264_8820708", "code_snippet": "#include <iostream>\nusing namespace std;\n\nint main() {\n int n, k;\n while (cin >> n >> k) {\n if (n / 2 < k) {\n cout << \"-1\\n\";\n continue;\n }\n int p[n];\n p[0] = 0;\n for (int i = 1; i < n; i += 2) {\n p[i] = i / 2 + 1;\n p[i + 1] = n - p[i];\n }\n for (int i = 0; i < k; i++) {\n if (i != 0) \n cout << \"\\n\";\n for (int j = 0; j < n - 1; j++) {\n int a = (p[j] + i) % n + 1;\n int b = (p[j + 1] + i) % n + 1;\n cout << a << \" \" << b << \"\\n\";\n }\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 3092, "score_of_the_acc": -0.176, "final_rank": 12 }, { "submission_id": "aoj_2264_8820702", "code_snippet": "#include <iostream>\n#include <vector>\nusing namespace std;\n\nint main() {\n ios::sync_with_stdio(false);\n int n, k;\n while (cin >> n >> k) {\n if (n / 2 < k) {\n cout << \"-1\\n\";\n continue;\n }\n vector<int> p(n + 2);\n p[0] = 0;\n for (int i = 1; i < n; i += 2) {\n p[i] = i / 2 + 1;\n p[i + 1] = n - p[i];\n }\n for (int i = 0; i < k; i++) {\n if (i != 0)\n cout << \"\\n\";\n for (int j = 0; j < n - 1; j++) {\n int a = (p[j] + i) % n + 1;\n int b = (p[j + 1] + i) % n + 1;\n cout << a << \" \" << b << \"\\n\";\n }\n }\n }\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 3208, "score_of_the_acc": -0.0682, "final_rank": 6 }, { "submission_id": "aoj_2264_8808491", "code_snippet": "#include <cstdio>\n#include <iostream>\nusing namespace std;\nint main() {\n int n, k;\n while (cin >> n >> k) {\n if (n / 2 < k) {\n printf(\"-1\\n\");\n continue;\n }\n int p[n + 2];\n p[0] = 0;\n for (int i = 1; i < n; i += 2) {\n p[i] = i / 2 + 1;\n p[i + 1] = n - p[i];\n }\n for (int i = 0; i < k * (n - 1); i++) {\n if (i != 0 && i % (n - 1) == 0)\n printf(\"\\n\");\n int j = i % (n - 1);\n int a = (p[j] + i / (n - 1)) % n + 1;\n int b = (p[j + 1] + i / (n - 1)) % n + 1;\n printf(\"%d %d\\n\", a, b);\n }\n }\n}", "accuracy": 1, "time_ms": 90, "memory_kb": 3244, "score_of_the_acc": -0.1248, "final_rank": 11 }, { "submission_id": "aoj_2264_8808490", "code_snippet": "#include <cstdio>\nusing namespace std;\n\nint main() {\n int n, k;\n while (scanf(\"%d %d\", &n, &k) == 2) {\n if (n / 2 < k) {\n printf(\"-1\\n\");\n }\n else {\n int p[n + 2];\n p[0] = 0;\n for (int i = 1; i < n; i += 2) {\n p[i] = i / 2 + 1;\n p[i + 1] = n - p[i];\n }\n for (int i = 0; i < k; i++) {\n if (i != 0)\n printf(\"\\n\");\n int a, b;\n for (int j = 0; j < n - 1; j++) {\n a = (p[j] + i) % n + 1;\n b = (p[j + 1] + i) % n + 1;\n printf(\"%d %d\\n\", a, b);\n }\n }\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 2768, "score_of_the_acc": 0, "final_rank": 1 }, { "submission_id": "aoj_2264_8808489", "code_snippet": "#include <iostream>\n\nint main() {\n int n, k;\n while (std::cin >> n >> k) {\n if (n / 2 < k) {\n std::cout << \"-1\\n\";\n continue;\n }\n int p[n + 2];\n p[0] = 0;\n for (int i = 1; i < n; i += 2) {\n p[i] = i / 2 + 1;\n p[i + 1] = n - p[i];\n }\n for (int i = 0; i < k; ++i) {\n if (i != 0)\n std::cout << '\\n';\n int a = (p[0] + i) % n + 1;\n for (int j = 0; j < n - 1; ++j) {\n int b = (p[j + 1] + i) % n + 1;\n std::cout << a << ' ' << b << '\\n';\n a = b;\n }\n }\n }\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 3128, "score_of_the_acc": -0.177, "final_rank": 15 }, { "submission_id": "aoj_2264_8808485", "code_snippet": "#include <cstdio>\n#include <iostream>\nusing namespace std;\n\nint main() {\n int n, k;\n while (cin >> n >> k) {\n if (n / 2 < k) {\n printf(\"-1\\n\");\n continue;\n }\n int p[n];\n p[0] = 0;\n for (int i = 1; i < n; i += 2) {\n p[i] = i / 2 + 1;\n p[i + 1] = n - p[i];\n }\n for (int i = 0; i < k; i++) {\n if (i != 0)\n printf(\"\\n\");\n for (int j = 0; j < n - 1; j++) {\n int a = (p[j] + i) % n + 1;\n int b = (p[j + 1] + i) % n + 1;\n printf(\"%d %d\\n\", a, b);\n }\n }\n }\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 3256, "score_of_the_acc": -0.0141, "final_rank": 2 }, { "submission_id": "aoj_2264_8216298", "code_snippet": "#include <iostream>\n#include <vector>\n\nint main() {\n std::ios_base::sync_with_stdio(false);\n std::cin.tie(NULL);\n \n int n, k;\n while (std::cin >> n >> k) {\n if (n / 2 < k) {\n std::cout << \"-1\\n\";\n continue;\n }\n std::vector<int> p(n + 2);\n for (int i = 1; i < n; i += 2) {\n p[i] = i / 2 + 1;\n p[i + 1] = n - p[i];\n }\n for (int i = 0; i < k; i++) {\n for (int j = 0; j < n - 1; j++) {\n int a = (p[j] + i) % n + 1;\n int b = (p[j + 1] + i) % n + 1;\n if (j != 0) std::cout << '\\n';\n std::cout << a << ' ' << b;\n }\n std::cout << '\\n';\n }\n }\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 3192, "score_of_the_acc": -0.0678, "final_rank": 3 }, { "submission_id": "aoj_2264_8216296", "code_snippet": "#include <iostream>\n#include <vector>\nusing namespace std;\n\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n int n, k;\n while (cin >> n >> k) {\n if (n / 2 < k) {\n cout << \"-1\\n\";\n continue;\n }\n vector<int> p(n + 2);\n p[0] = 0;\n for (int i = 1; i < n; i += 2) {\n p[i] = i / 2 + 1;\n p[i + 1] = n - p[i];\n }\n for (int i = 0; i < k; i++) {\n if (i != 0)\n cout << \"\\n\";\n for (int j = 0; j < n - 1; j++) {\n int a = (p[j] + i) % n + 1;\n int b = (p[j + 1] + i) % n + 1;\n cout << a << \" \" << b << \"\\n\";\n }\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 3236, "score_of_the_acc": -0.0691, "final_rank": 8 }, { "submission_id": "aoj_2264_8216288", "code_snippet": "#include <iostream>\n#include <vector>\nusing namespace std;\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n int n, k;\n while (cin >> n >> k) {\n if (n / 2 < k) {\n cout << \"-1\\n\";\n continue;\n }\n vector<int> p(n + 2);\n for (int i = 1; i < n; i += 2) {\n p[i] = i / 2 + 1;\n p[i + 1] = n - p[i];\n }\n for (int i = 0; i < k; i++) {\n for (int j = 0; j < n - 1; j++) {\n int a = (p[j] + i) % n + 1;\n int b = (p[j + 1] + i) % n + 1;\n if (j > 0) cout << \"\\n\";\n cout << a << \" \" << b;\n }\n cout << \"\\n\";\n }\n }\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 3204, "score_of_the_acc": -0.0681, "final_rank": 5 }, { "submission_id": "aoj_2264_8216282", "code_snippet": "#include <iostream>\n#include <vector>\n\nint main() {\n std::ios_base::sync_with_stdio(false);\n std::cin.tie(NULL);\n\n int n, k;\n while (std::cin >> n >> k) {\n if (n / 2 < k) {\n std::cout << \"-1\\n\";\n continue;\n }\n std::vector<int> p(n + 2);\n for (int i = 1; i < n; i += 2) {\n p[i] = i / 2 + 1;\n p[i + 1] = n - p[i];\n }\n for (int i = 0; i < k; i++) {\n for (int j = 0; j < n - 1; j++) {\n int a = (p[j] + i) % n + 1;\n int b = (p[j + 1] + i) % n + 1;\n std::cout << a << \" \" << b << '\\n';\n }\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 3192, "score_of_the_acc": -0.0678, "final_rank": 3 } ]
aoj_2261_cpp
問題 C : [[iwi]] そうだ,僕のハンドルネームは (iwi) だ. 僕は確かに昔にプログラミングコンテストに参加していた. そして,多くの仲間と楽しい時間を過ごした. 確か,仲間たちは全員,美少女だったような気がする. プログラミングコンテストの世界は,僕のハーレムだったような気がする. G○○gle は,僕のハーレムを奪ったのだ,そうに違いない. 昔の仲間の手がかりをつかむためにも,やはりプログラミングコンテストに出なければならない. G○○gle Code Jam に参加登録することにしよう. 今度の ID には,丸括弧以外の括弧も検討に入れてみよう. 問題 'i', 'w', '(', ')', '{', '}', '[', ']' からなる文字列が与えられた時, その部分列をとって,線対称な文字列を作りたい. 最大で何文字の文字列を作ることができるかを計算するプログラムを作成せよ. 与えられる文字列は, "iwi" という文字列を一度含み,それ以外の部分には 'i' と 'w' を含まない. より形式的には,与えられる文字列は s "iwi" t ( s と "iwi" と t を連結したもの)という形で表すことができ, s と t は '(', ')', '{', '}', '[', ']' からなる文字列である. s や t が 0 文字である可能性もある. 作る文字列は,与えられる文字列の部分列をとって作る. 部分列とは,元の文字列からいくつかの文字を取り出し,それらを, 元の文字列に含まれる順番で繋げたものである. 取り出す文字たちは必ずしも元の文字列で連続していなくても良い. また,作る文字列も,与えられる文字列と同様に,"iwi" という文字列を一度含み, それ以外の部分には 'i' と 'w' は含まないようにしたい. ここで用いる左右に線対称の定義は,以下とする. 以下の文字列は左右に線対称. 空文字列 "i" "w" 文字列 x が左右に線対称のとき,以下の文字列も左右に線対称. "i" x "i" "w" x "w" "(" x ")" ")" x "(" "{" x "}" "}" x "{" "[" x "]" "]" x "[" 以上のもののみが左右に線対称. 入力 入力は 'i', 'w', '(', ')', '{', '}', '[', ']' からなり上記の条件を満たす文字列である. 出力 上記の条件を満たし作ることのできる文字列の長さの最大値を出力せよ. 制約 入力の文字列の長さは 15 以下である. 入出力例 入出力例 1 入力例 1: [[[iwi[[[ 入力例 1 に対する出力例: 3 "iwi" という文字列しか作ることができない. 入出力例 2 入力例 2: [{)iwi(]} 入力例 2 に対する出力例: 7 "[)iwi(]" や "{)iwi(}" など 7 文字の文字列を作ることができる.
[ { "submission_id": "aoj_2261_4088642", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n \nbool check(string s){\n int n = s.size();\n bool ok = true;\n for(int i=0;i<n/2;i++){\n if(s[i] == '('){\n ok &= s[n-1-i] == ')';\n }\n else if(s[i] == ')'){\n ok &= s[n-1-i] == '(';\n }\n else if(s[i] == '['){\n ok &= s[n-1-i] == ']';\n }\n else if(s[i] == ']'){\n ok &= s[n-1-i] == '[';\n }\n else if(s[i] == '}'){\n ok &= s[n-1-i] == '{';\n }\n else if(s[i] == '{'){\n ok &= s[n-1-i] == '}';\n }\n else{\n ok &= s[i] == s[n-1-i];\n }\n }\n int cnt = 0, pos = 0;\n while((pos = s.find(\"iwi\",pos)) != s.npos){\n cnt++;\n pos++;\n }\n ok &= cnt == 1;\n return ok;\n}\n\nint main(){\n int n, ans = 0;\n string s;\n cin >> s;\n n = s.size();\n for(int S=1;S<1<<n;S++){\n string t;\n for(int i=0;i<n;i++){\n if((S>>i)&1){\n t.push_back(s[i]);\n }\n if(check(t)){\n ans = max(ans, int(t.size()));\n }\n }\n }\n cout << ans << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3212, "score_of_the_acc": -1.2211, "final_rank": 13 }, { "submission_id": "aoj_2261_4088634", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nbool check(string s){\n int n = s.size();\n bool ok = true;\n for(int i=0;i<n/2;i++){\n if(s[i] == '('){\n ok &= s[n-1-i] == ')';\n }\n else if(s[i] == ')'){\n ok &= s[n-1-i] == '(';\n }\n else if(s[i] == '['){\n ok &= s[n-1-i] == ']';\n }\n else if(s[i] == ']'){\n ok &= s[n-1-i] == '[';\n }\n else if(s[i] == '}'){\n ok &= s[n-1-i] == '{';\n }\n else if(s[i] == '{'){\n ok &= s[n-1-i] == '}';\n }\n else{\n ok &= s[i] == s[n-1-i];\n }\n }\n return ok;\n}\n\nint main(){\n int n, ans = 0;\n string s;\n cin >> s;\n n = s.size();\n for(int S=1;S<1<<n;S++){\n string t;\n for(int i=0;i<n;i++){\n if((S>>i)&1){\n t.push_back(s[i]);\n }\n if(check(t)){\n ans = max(ans, int(t.size()));\n }\n }\n }\n cout << ans << endl;\n return 0;\n}", "accuracy": 0.13513513513513514, "time_ms": 20, "memory_kb": 3216, "score_of_the_acc": -1.0801, "final_rank": 20 }, { "submission_id": "aoj_2261_3915334", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\nusing ull = unsigned long long;\n\nconst double PI = acos(-1);\nconst int inf = 2e9;\nconst long long INF = 2e18;\nconst long long MOD = 1e9+7;\n\n#define sx(s) (s).size()\n#define pb push_back\n#define fi first\n#define se second\n#define REP(i,n) for (int i = 0; i < n; i++)\n#define ALL(a) begin(a),end(a)\n\nint main(void) {\n string S;\n cin >> S;\n int N = S.size();\n\n int ans = 0;\n\n for (int i=0; i<(1<<N); i++) {\n vector<bool> use(N, false);\n int t = i;\n REP(j, N) {\n use[j] = t%2;\n t /= 2;\n }\n string ss;\n REP(j, N) {\n if (use[j]) ss.pb(S[j]);\n }\n int n = ss.size();\n if (n%2 == 0) continue;\n bool flag = true;\n if (!(ss[n/2-1]=='i' && ss[n/2+1]=='i' && ss[n/2] == 'w')) flag = false;\n REP(j, n/2) {\n if (ss[j] == 'i') {\n if (ss[n-j-1] != 'i') flag = false;\n // cout << \"a\";\n }\n else if (ss[j] == 'w') {\n if (ss[n-j-1] != 'w') flag = false;\n //cout << \"b\";\n }\n else if (ss[j] == '(') {\n if (ss[n-j-1] != ')') flag = false;\n //cout << \"c\";\n }\n else if (ss[j] == ')') {\n if (ss[n-j-1] != '(') flag = false;\n //cout << \"d\";\n }\n else if (ss[j] == '[') {\n if (ss[n-j-1] != ']') flag = false;\n //cout << \"e\";\n }\n else if (ss[j] == ']') {\n if (ss[n-j-1] != '[') flag = false;\n //cout << \"f\";\n }\n else if (ss[j] == '{') {\n if (ss[n-j-1] != '}') flag = false;\n //cout << \"g\";\n }\n else if (ss[j] == '}') {\n if (ss[n-j-1] != '{') flag = false;\n //cout << \"h\";\n }\n }\n if (flag) ans = max(ans, n);\n\n }\n\n cout << ans << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3196, "score_of_the_acc": -0.928, "final_rank": 8 }, { "submission_id": "aoj_2261_3915317", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\nusing ull = unsigned long long;\n\nconst double PI = acos(-1);\nconst int inf = 2e9;\nconst long long INF = 2e18;\nconst long long MOD = 1e9+7;\n\n#define sx(s) (s).size()\n#define pb push_back\n#define fi first\n#define se second\n#define REP(i,n) for (int i = 0; i < n; i++)\n#define ALL(a) begin(a),end(a)\n\nint main(void) {\n string S;\n cin >> S;\n int N = S.size();\n\n int ans = 0;\n\n for (int i=0; i<(1<<N); i++) {\n vector<bool> use(N, false);\n int t = i;\n REP(j, N) {\n use[j] = t%2;\n t /= 2;\n }\n string ss;\n REP(j, N) {\n if (use[j]) ss.pb(S[j]);\n }\n int n = ss.size();\n bool flag = true;\n REP(j, n/2) {\n if (ss[j] == 'i') {\n if (ss[n-j-1] != 'i') flag = false;\n // cout << \"a\";\n }\n else if (ss[j] == 'w') {\n if (ss[n-j-1] != 'w') flag = false;\n //cout << \"b\";\n }\n else if (ss[j] == '(') {\n if (ss[n-j-1] != ')') flag = false;\n //cout << \"c\";\n }\n else if (ss[j] == ')') {\n if (ss[n-j-1] != '(') flag = false;\n //cout << \"d\";\n }\n else if (ss[j] == '[') {\n if (ss[n-j-1] != ']') flag = false;\n //cout << \"e\";\n }\n else if (ss[j] == ']') {\n if (ss[n-j-1] != '[') flag = false;\n //cout << \"f\";\n }\n else if (ss[j] == '{') {\n if (ss[n-j-1] != '}') flag = false;\n //cout << \"g\";\n }\n else if (ss[j] == '}') {\n if (ss[n-j-1] != '{') flag = false;\n //cout << \"h\";\n }\n }\n if (flag) ans = max(ans, n);\n\n }\n cout << ans << endl;\n return 0;\n}", "accuracy": 0.13513513513513514, "time_ms": 10, "memory_kb": 3152, "score_of_the_acc": -0.9077, "final_rank": 18 }, { "submission_id": "aoj_2261_2662834", "code_snippet": "#include <bits/stdc++.h>\n#define REP(i, n) for (int i = 0; (i) < int(n); ++ (i))\nusing namespace std;\ntemplate <class T> inline void chmax(T & a, T const & b) { a = max(a, b); }\n\nbool pred(string const & t) {\n int n = t.length();\n if (n % 2 == 0) return false;\n if (n < 3) return false;\n if (t[n / 2 - 1] != 'i') return false;\n if (t[n / 2 ] != 'w') return false;\n if (t[n / 2 + 1] != 'i') return false;\n REP (i, n / 2 - 1) {\n char c;\n switch (t[i]) {\n case '(': c = ')'; break;\n case ')': c = '('; break;\n case '[': c = ']'; break;\n case ']': c = '['; break;\n case '{': c = '}'; break;\n case '}': c = '{'; break;\n default: return false;\n }\n if (c != t[n - i - 1]) return false;\n }\n return true;\n}\n\nint main() {\n string s; cin >> s;\n int result = 0;\n REP (x, 1 << s.length()) {\n string t;\n REP (i, s.length()) if (x & (1 << i)) {\n t += s[i];\n }\n if (pred(t)) {\n chmax<int>(result, t.length());\n }\n }\n cout << result << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3200, "score_of_the_acc": -0.9299, "final_rank": 9 }, { "submission_id": "aoj_2261_2362593", "code_snippet": "#include <iostream>\n#include <cstring>\nusing namespace std;\n\nchar dat[16];\n\nint hantei(char a,char b){\n char lc[]=\"({[\";\n char rc[]=\")}]\";\n\n for (int i=0;i<3;i++){\n if (a==lc[i]&&b==rc[i])return 1;\n \n if (b==lc[i]&&a==rc[i])return 1;\n \n }\n return 0;\n}\n\nint search(int a,int b,int c,int islfix){\n int lm,rm;\n if (islfix){\n for (int i=b;i>c;i--){\n if (hantei(dat[i],dat[a])){\n\tlm=search(a+1,i-1,c,1);\n\trm=search(a+1,i-1,c,0);\n\treturn max(lm,rm)+2;\n }\n }\n }else{\n for (int i=a;i<c;i++){\n if (hantei(dat[i],dat[b])){\n\tlm=search(i+1,b-1,c,1);\n\trm=search(i+1,b-1,c,0);\n\treturn max(lm,rm)+2;\n }\n }\n }\n int reta=0,retb=0,flg=0;\n if (a<c-1){\n rm= search( a+1, b, c, 0);\n lm= search( a+1, b, c, 1);\n //cout << a << \" \" << b ;\n\n //cout << rm << \" \" << lm << endl;\n reta=max(rm,lm);\n flg=1;\n }\n if (b>c+1){\n rm= search( a, b-1, c, 0);\n lm= search( a, b-1, c, 1);\n //cout << a << \" \" << b ;\n // cout << rm << \" \" << lm << endl;\n retb=max(rm,lm);\n flg=1;\n }\n //cout << flg << \" \"<< reta << \" \" << retb << endl;\n if (flg)return max(reta,retb);\n else return 3;\n}\n\nint main(){\n int c,ret;\n cin >> dat;\n for (c=0;dat[c]!='w';c++);\n ret = max(search(0,strlen(dat)-1,c,0),search(0,strlen(dat)-1,c,1));\n cout << ret << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 3080, "score_of_the_acc": -1.8745, "final_rank": 14 }, { "submission_id": "aoj_2261_2362472", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#define int long long\nsigned main(){\n string s;\n cin>>s;\n int n=s.size();\n int ans=0;\n map<char,char> r;\n r['[']=']';\n r[']']='[';\n r['(']=')';\n r[')']='(';\n r['{']='}';\n r['}']='{';\n r['i']='i';\n r['w']='w';\n for(int b=0;b<(1<<n);b++){\n string t;\n for(int i=0;i<n;i++) if((b>>i)&1) t+=s[i];\n if(t.find(\"iwi\")==string::npos) continue;\n int k=t.size();\n if(k%2==0) continue;\n if(t[k/2-1]!='i'||t[k/2]!='w'||t[k/2+1]!='i') continue;\n bool f=1;\n for(int i=0;i<k/2;i++) f&=t[i]==r[t[k-1-i]];\n if(f) ans=max(ans,k);\n }\n cout<<ans<<endl;\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3188, "score_of_the_acc": -0.9244, "final_rank": 7 }, { "submission_id": "aoj_2261_2188646", "code_snippet": "#include<bits/stdc++.h>\n#define rep(i,n)for(int i=0;i<(n);i++)\nusing namespace std;\n\nmap<char, char>mp{ {'[',']'},{'{','}'},{'(',')'},{']','['},{'}','{'},{')','('} };\nint main() {\n\tstring s; cin >> s;\n\tint Max = 0;\n\trep(i, 1 << s.size()) {\n\t\tstring t;\n\t\trep(j, s.size()) {\n\t\t\tif (i >> j & 1)t += s[j];\n\t\t}\n\t\tif ((int)t.find(\"iwi\") == -1)continue;\n\t\tstring k = t; reverse(k.begin(), k.end());\n\t\tfor (char&c : k) {\n\t\t\tif (!isalpha(c))c = mp[c];\n\t\t}\n\t\tif (k == t)Max = max(Max, (int)t.size());\n\t}\n\tprintf(\"%d\\n\", Max);\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3352, "score_of_the_acc": -1, "final_rank": 12 }, { "submission_id": "aoj_2261_1843511", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define all(c) (c).begin(),(c).end()\n#define rrep(i,n) for(int i=(int)(n)-1;i>=0;i--)\n#define REP(i,m,n) for(int i=(int)(m);i<(int)(n);i++)\n#define rep(i,n) REP(i,0,n)\n#define iter(c) __typeof((c).begin())\n#define tr(it,c) for(iter(c) it=(c).begin();it!=(c).end();it++)\n#define mem(a) memset(a,0,sizeof(a))\n#define pd(a) printf(\"%.10f\\n\",a)\n#define pb(a) push_back(a)\n#define in(a) insert(a)\n#define pi M_PI\n#define R cin>>\n#define F first\n#define S second\n#define C class\n#define ll long long\n#define ln cout<<'\\n'\ntemplate<C T>void pr(T a){cout<<a;ln;}\ntemplate<C T,C T2>void pr(T a,T2 b){cout<<a<<' '<<b;ln;}\ntemplate<C T,C T2,C T3>void pr(T a,T2 b,T3 c){cout<<a<<' '<<b<<' '<<c;ln;}\ntemplate<C T>void PR(T a,int n){rep(i,n){if(i)cout<<' ';cout<<a[i];}ln;}\nbool check(int n,int m,int x,int y){return x>=0&&x<n&&y>=0&&y<m;}\nconst ll MAX=1000000007,MAXL=1LL<<60,dx[4]={-1,0,1,0},dy[4]={0,1,0,-1};\ntypedef pair<int,int> P;\n\nvoid Main() {\n string r;\n R r;\n int n=r.size();\n int ans=0;\n REP(t,1,1<<n) {\n if(__builtin_popcount(t)<=2) continue;\n string s=\"\";\n rep(i,n) if(t&(1<<i)) s+=r[i];\n bool f=1;\n int m=s.size();\n rep(i,m) {\n char c=s[i],c2=s[s.size()-i-1];\n if(c=='i'&&c2!='i') f=0;\n if(c=='w'&&c2!='w') f=0;\n if(c=='('&&c2!=')') f=0;\n if(c==')'&&c2!='(') f=0;\n if(c=='['&&c2!=']') f=0;\n if(c==']'&&c2!='[') f=0;\n if(c=='{'&&c2!='}') f=0;\n if(c=='}'&&c2!='{') f=0;\n }\n if(f&&m%2) {\n if(s[m/2-1]!='i'||s[m/2]!='w'||s[m/2+1]!='i') continue;\n bool ff=1;\n rep(i,m/2-1) {\n if(isalpha(s[i])) ff=0;\n if(isalpha(s[m-i-1])) ff=0;\n }\n if(ff) ans=max(ans,(int)s.size());\n }\n }\n pr(ans);\n}\n\nint main() {\n ios::sync_with_stdio(0);cin.tie(0);\n Main();return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1204, "score_of_the_acc": -0.0092, "final_rank": 4 }, { "submission_id": "aoj_2261_1843482", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define all(c) (c).begin(),(c).end()\n#define rrep(i,n) for(int i=(int)(n)-1;i>=0;i--)\n#define REP(i,m,n) for(int i=(int)(m);i<(int)(n);i++)\n#define rep(i,n) REP(i,0,n)\n#define iter(c) __typeof((c).begin())\n#define tr(it,c) for(iter(c) it=(c).begin();it!=(c).end();it++)\n#define mem(a) memset(a,0,sizeof(a))\n#define pd(a) printf(\"%.10f\\n\",a)\n#define pb(a) push_back(a)\n#define in(a) insert(a)\n#define pi M_PI\n#define R cin>>\n#define F first\n#define S second\n#define C class\n#define ll long long\n#define ln cout<<'\\n'\ntemplate<C T>void pr(T a){cout<<a;ln;}\ntemplate<C T,C T2>void pr(T a,T2 b){cout<<a<<' '<<b;ln;}\ntemplate<C T,C T2,C T3>void pr(T a,T2 b,T3 c){cout<<a<<' '<<b<<' '<<c;ln;}\ntemplate<C T>void PR(T a,int n){rep(i,n){if(i)cout<<' ';cout<<a[i];}ln;}\nbool check(int n,int m,int x,int y){return x>=0&&x<n&&y>=0&&y<m;}\nconst ll MAX=1000000007,MAXL=1LL<<60,dx[4]={-1,0,1,0},dy[4]={0,1,0,-1};\ntypedef pair<int,int> P;\n\nvoid Main() {\n string r;\n R r;\n int n=r.size();\n int ans=0;\n REP(t,1,1<<n) {\n string s=\"\";\n rep(i,n) if(t&(1<<i)) s+=r[i];\n bool f=1;\n rep(i,s.size()) {\n char c=s[i],c2=s[s.size()-i-1];\n if(isalpha(c)&&c!=c2) f=0;\n if(c=='('&&c2!=')') f=0;\n if(c==')'&&c2!='(') f=0;\n if(c=='['&&c2!=']') f=0;\n if(c==']'&&c2!='[') f=0;\n if(c=='{'&&c2!='}') f=0;\n if(c=='}'&&c2!='{') f=0;\n }\n if(f) ans=max(ans,(int)s.size());\n }\n pr(ans);\n}\n\nint main() {\n ios::sync_with_stdio(0);cin.tie(0);\n Main();return 0;\n}", "accuracy": 0.13513513513513514, "time_ms": 10, "memory_kb": 1200, "score_of_the_acc": -0.0074, "final_rank": 17 }, { "submission_id": "aoj_2261_1834037", "code_snippet": "#include<iostream>\n#include<vector>\n#include<string>\n#include<algorithm>\n#include<map>\n#include<set>\n#include<utility>\n#include<cmath>\n#include<cstring>\n#include<queue>\n#include<cstdio>\n#include<sstream>\n#define loop(i,a,b) for(int i=a;i<b;i++) \n#define rep(i,a) loop(i,0,a)\n#define pb push_back\n#define mp make_pair\n#define all(in) in.begin(),in.end()\nconst double PI=acos(-1);\nconst double EPS=1e-8;\nconst int inf=1e9;\nusing namespace std;\ntypedef long long ll;\ntypedef vector<int> vi;\ntypedef vector<vi> vvi;\ntypedef pair<int,int> pii;\nint main(){\n\tbool m[300][300]={0};\n\tm['i']['i']=true;\n\tm['w']['w']=true;\n\tm[')']['(']=true;\n\tm['('][')']=true;\n\tm[']']['[']=true;\n\tm['['][']']=true;\n\tm['}']['{']=true;\n\tm['{']['}']=true;\n\tstring s;\n\tcin>>s;\n\tint n=s.size();\n\tint out=0;\n\tloop(i,1,1<<n){\n\t\tstring t=\"\";\n\t\trep(j,n)if(i&(1<<j))t+=s[j];\n\t\tif(t.size()%2==0)continue;\n\t\tbool h=true;\n\t\trep(j,t.size()/2)if(m[t[j]][t[t.size()-1-j]]==0)h=false;\n\t\tif(t[t.size()/2]!='w')h=false;\n\t\tif(h&&out<t.size())out=t.size();\n\t}\n\tcout<<out<<endl;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1292, "score_of_the_acc": -0.0498, "final_rank": 5 }, { "submission_id": "aoj_2261_1696536", "code_snippet": "#include<iostream>\n#include<string>\n#include<algorithm>\nusing namespace std;\nstring S,T;\nbool sym(string U){\n\tfor(int i=0;i<U.size();i++){\n\t\tint K=U.size()-1-i;\n\t\tif(U[i]=='i' && U[K]=='i'){\n\t\t\tgoto E;\n\t\t}\n\t\tif(U[i]=='w' && U[K]=='w'){\n\t\t\tgoto E;\n\t\t}\n\t\tif(U[i]=='(' && U[K]==')'){\n\t\t\tgoto E;\n\t\t}\n\t\tif(U[i]==')' && U[K]=='('){\n\t\t\tgoto E;\n\t\t}\n\t\tif(U[i]=='[' && U[K]==']'){\n\t\t\tgoto E;\n\t\t}\n\t\tif(U[i]==']' && U[K]=='['){\n\t\t\tgoto E;\n\t\t}\n\t\tif(U[i]=='{' && U[K]=='}'){\n\t\t\tgoto E;\n\t\t}\n\t\tif(U[i]=='}' && U[K]=='{'){\n\t\t\tgoto E;\n\t\t}\n\t\treturn false;\n\t\tE:;\n\t}\n\tint cnt=0;\n\tchar x[200];\n\tint size2=U.size();\n\tfor(int i=0;i<size2;i++){\n\t\tx[i]=U[i];\n\t}\n\tfor(int i=0;i<size2-2;i++){\n\t\tif(x[i]==105 && x[i+1]==119 && x[i+2]==105){\n\t\t\tcnt++;\n\t\t}\n\t}\n\tif(cnt==1){\n\t\treturn true;\n\t}\n\treturn false;\n}\nint main(){\n\tint res=0;\n\tcin>>S;\n\tfor(int i=0;i<(1<<(S.size()));i++){\n\t\tT=\"\";\n\t\tfor(int j=0;j<S.size();j++){\n\t\t\tif((i/(1<<j))%2==0){\n\t\t\t\tT+=S[j];\n\t\t\t}\n\t\t}\n\t\tbool ok=sym(T);\n\t\tif(ok==true){\n\t\t\tres=max(res,(int)(T.size()));\n\t\t}\n\t}\n\tif(res<=2){\n\t\tres=0;\n\t}\n\tcout<<res<<endl;\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1192, "score_of_the_acc": -0.0037, "final_rank": 1 }, { "submission_id": "aoj_2261_1696527", "code_snippet": "#include<iostream>\n#include<string>\n#include<algorithm>\nusing namespace std;\nstring S,T;\nbool sym(string U){\n\tfor(int i=0;i<U.size();i++){\n\t\tint K=U.size()-1-i;\n\t\tif(U[i]=='i' && U[K]=='i'){\n\t\t\tgoto E;\n\t\t}\n\t\tif(U[i]=='w' && U[K]=='w'){\n\t\t\tgoto E;\n\t\t}\n\t\tif(U[i]=='(' && U[K]==')'){\n\t\t\tgoto E;\n\t\t}\n\t\tif(U[i]==')' && U[K]=='('){\n\t\t\tgoto E;\n\t\t}\n\t\tif(U[i]=='[' && U[K]==']'){\n\t\t\tgoto E;\n\t\t}\n\t\tif(U[i]==']' && U[K]=='['){\n\t\t\tgoto E;\n\t\t}\n\t\tif(U[i]=='{' && U[K]=='}'){\n\t\t\tgoto E;\n\t\t}\n\t\tif(U[i]=='}' && U[K]=='{'){\n\t\t\tgoto E;\n\t\t}\n\t\treturn false;\n\t\tE:;\n\t}\n\treturn true;\n}\nint main(){\n\tint res=0;\n\tcin>>S;\n\tfor(int i=0;i<(1<<(S.size()));i++){\n\t\tT=\"\";\n\t\tfor(int j=0;j<S.size();j++){\n\t\t\tif((i/(1<<j))%2==0){\n\t\t\t\tT+=S[j];\n\t\t\t}\n\t\t}\n\t\tbool ok=sym(T);\n\t\tif(ok==true){\n\t\t\tres=max(res,(int)(T.size()));\n\t\t}\n\t}\n\tif(res<=2){\n\t\tres=0;\n\t}\n\tcout<<res<<endl;\n\treturn 0;\n}", "accuracy": 0.13513513513513514, "time_ms": 10, "memory_kb": 1192, "score_of_the_acc": -0.0037, "final_rank": 16 }, { "submission_id": "aoj_2261_1667641", "code_snippet": "#include <string>\n#include <iostream>\n#include <algorithm>\n\nusing namespace std;\n\nstring S;\n\nconst string R1 = \"iw(){}[]\";\nconst string R2 = \"iw)(}{][\";\n\ninline bool solve(string T)\n{\n\tint is = 0, ws = 0;\n\n\tfor (int i = 0; i < T.size(); i++)\n\t{\n\t\tif (T[i] == 'i') is++;\n\t\tif (T[i] == 'w') ws++;\n\t}\n\n\tif (!(is == 2 && ws == 1)) return false;\n\n\tbool flag = false;\n\n\tfor (int i = 0; i < (int)(T.size()) - 2; i++)\n\t{\n\t\tif (T.substr(i, 3) == \"iwi\")\n\t\t{\n\t\t\tflag = true;\n\t\t}\n\t}\n\n\tif (!flag) return false;\n\n\tif (T.size() % 2 == 1)\n\t{\n\t\tchar middle = T[T.size() / 2];\n\n\t\tif (!(middle == 'i' || middle == 'w')) return false;\n\t}\n\n\tfor (int i = 0; i < T.size(); i++)\n\t{\n\t\tchar c1 = T[i];\n\t\tchar c2 = T[T.size() - i - 1];\n\n\t\tfor (int j = 0; j < 8; j++)\n\t\t{\n\t\t\tif (R1[j] == c1)\n\t\t\t{\n\t\t\t\tif (R2[j] != c2)\n\t\t\t\t{\n\t\t\t\t\treturn false;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\treturn true;\n}\n\nint main()\n{\n\tcin >> S;\n\n\tint ret = 0;\n\n\tfor (int i = 0; i < (1 << S.size()); i++)\n\t{\n\t\tstring T;\n\n\t\tfor (int j = 0; j < S.size(); j++)\n\t\t{\n\t\t\tif (i & (1 << j))\n\t\t\t{\n\t\t\t\tT += S[j];\n\t\t\t}\n\t\t}\n\n\t\tif (solve(T))\n\t\t{\n\t\t\tret = max(ret, (int)(T.size()));\n\t\t}\n\t}\n\n\tprintf(\"%d\\n\", ret);\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3328, "score_of_the_acc": -0.9889, "final_rank": 10 }, { "submission_id": "aoj_2261_1667640", "code_snippet": "#include <string>\n#include <iostream>\n#include <algorithm>\n\nusing namespace std;\n\nstring S;\n\nconst string R1 = \"iw(){}[]\";\nconst string R2 = \"iw)(}{][\";\n\nbool solve(string T)\n{\n\tint is = 0, ws = 0;\n\n\tfor (int i = 0; i < T.size(); i++)\n\t{\n\t\tif (T[i] == 'i') is++;\n\t\tif (T[i] == 'w') ws++;\n\t}\n\n\tif (!(is == 2 && ws == 1)) return false;\n\n\tbool flag = false;\n\n\tfor (int i = 0; i < (int)(T.size()) - 2; i++)\n\t{\n\t\tif (T.substr(i, 3) == \"iwi\")\n\t\t{\n\t\t\tflag = true;\n\t\t}\n\t}\n\n\tif (!flag) return false;\n\n\tif (T.size() % 2 == 1)\n\t{\n\t\tchar middle = T[T.size() / 2];\n\n\t\tif (!(middle == 'i' || middle == 'w')) return false;\n\t}\n\n\tfor (int i = 0; i < T.size(); i++)\n\t{\n\t\tchar c1 = T[i];\n\t\tchar c2 = T[T.size() - i - 1];\n\n\t\tfor (int j = 0; j < 8; j++)\n\t\t{\n\t\t\tif (R1[j] == c1)\n\t\t\t{\n\t\t\t\tif (R2[j] != c2)\n\t\t\t\t{\n\t\t\t\t\treturn false;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\treturn true;\n}\n\nint main()\n{\n\tcin >> S;\n\n\tint ret = 0;\n\n\tfor (int i = 0; i < (1 << S.size()); i++)\n\t{\n\t\tstring T;\n\n\t\tfor (int j = 0; j < S.size(); j++)\n\t\t{\n\t\t\tif (i & (1 << j))\n\t\t\t{\n\t\t\t\tT += S[j];\n\t\t\t}\n\t\t}\n\n\t\tif (solve(T))\n\t\t{\n\t\t\tret = max(ret, (int)(T.size()));\n\t\t}\n\t}\n\n\tprintf(\"%d\\n\", ret);\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3328, "score_of_the_acc": -0.9889, "final_rank": 10 }, { "submission_id": "aoj_2261_1667637", "code_snippet": "#include <string>\n#include <iostream>\n#include <algorithm>\n\nusing namespace std;\n\nstring S;\n\nconst string R1 = \"iw(){}[]\";\nconst string R2 = \"iw)(}{][\";\n\nbool solve(string T)\n{\n\tint is = 0, ws = 0;\n\n\tfor (int i = 0; i < T.size(); i++)\n\t{\n\t\tif (T[i] == 'i') is++;\n\t\tif (T[i] == 'w') ws++;\n\t}\n\n\tif (!(is == 2 && ws == 1)) return false;\n\n\tbool flag = false;\n\n\tfor (int i = 0; i < (int)(T.size()) - 2; i++)\n\t{\n\t\tif (T.substr(i, 3) == \"iwi\")\n\t\t{\n\t\t\tflag = true;\n\n\t\t\tT.erase(T.begin() + i, T.begin() + i + 3);\n\t\t}\n\t}\n\n\tif (!flag) return false;\n\n\tif (T.size() % 2 == 1)\n\t{\n\t\tchar middle = T[T.size() / 2];\n\n\t\tif (!(middle == 'i' || middle == 'w')) return false;\n\t}\n\n\tfor (int i = 0; i < T.size(); i++)\n\t{\n\t\tchar c1 = T[i];\n\t\tchar c2 = T[T.size() - i - 1];\n\n\t\tfor (int j = 0; j < 8; j++)\n\t\t{\n\t\t\tif (R1[j] == c1)\n\t\t\t{\n\t\t\t\tif (R2[j] != c2)\n\t\t\t\t{\n\t\t\t\t\treturn false;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\treturn true;\n}\n\nint main()\n{\n\tcin >> S;\n\n\tint ret = 0;\n\n\tfor (int i = 0; i < (1 << S.size()); i++)\n\t{\n\t\tstring T;\n\n\t\tfor (int j = 0; j < S.size(); j++)\n\t\t{\n\t\t\tif (i & (1 << j))\n\t\t\t{\n\t\t\t\tT += S[j];\n\t\t\t}\n\t\t}\n\n\t\tif (solve(T))\n\t\t{\n\t\t\tret = max(ret, (int)(T.size()));\n\t\t}\n\t}\n\n\tprintf(\"%d\\n\", ret);\n\n\treturn 0;\n}", "accuracy": 0.13513513513513514, "time_ms": 10, "memory_kb": 3328, "score_of_the_acc": -0.9889, "final_rank": 19 }, { "submission_id": "aoj_2261_1561673", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\ntypedef pair<int,int>pint;\ntypedef vector<int>vint;\n#define pb push_back\n#define mp make_pair\n#define rep(i,n) for(int i=0;i<(n);i++)\ntemplate<class T,class U>void chmin(T &t,U f){if(t>f)t=f;}\ntemplate<class T,class U>void chmax(T &t,U f){if(t<f)t=f;}\n\n\nstring S;\nint N;\n\nbool check(string s){\n if((s.size()&1)==0||s.size()<3||s.substr(s.size()/2-1,3)!=\"iwi\")return false;\n rep(i,s.size()/2){\n char l=s[i],r=s[s.size()-i-1];\n if(isalpha(l)){\n if(l!=r)return false;\n }\n else{\n if(l=='['&&r==']')continue;\n if(l==']'&&r=='[')continue;\n if(l=='('&&r==')')continue;\n if(l==')'&&r=='(')continue;\n if(l=='{'&&r=='}')continue;\n if(l=='}'&&r=='{')continue;\n return false;\n }\n }\n return true;\n}\n\nint main(){\n cin>>S;\n N=S.size();\n\n int ma=0;\n\n rep(i,1<<N){\n string sub;\n rep(j,N)if(i>>j&1)sub+=S[j];\n if(check(sub))chmax(ma,sub.size());\n }\n\n cout<<ma<<endl;\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1200, "score_of_the_acc": -0.0074, "final_rank": 3 }, { "submission_id": "aoj_2261_1262991", "code_snippet": "#include <iostream>\n#include <complex>\n#include <sstream>\n#include <string>\n#include <algorithm>\n#include <deque>\n#include <list>\n#include <map>\n#include <numeric>\n#include <queue>\n#include <vector>\n#include <set>\n#include <limits>\n#include <cstdio>\n#include <cctype>\n#include <cmath>\n#include <cstring>\n#include <cstdlib>\n#include <ctime>\nusing namespace std;\n#define REP(i, j) for(int i = 0; i < (int)(j); ++i)\n#define FOR(i, j, k) for(int i = (int)(j); i < (int)(k); ++i)\n#define SORT(v) sort((v).begin(), (v).end())\n#define REVERSE(v) reverse((v).begin(), (v).end())\ntypedef complex<double> P;\n\n\nchar L[] = {'i', 'w', '(', ')', '{', '}', '[', ']'};\nchar R[] = {'i', 'w', ')', '(', '}', '{', ']', '['};\n\nbool check(string s){\n int sl = s.length();\n if(s[sl / 2] != 'w' || s[sl / 2 - 1] != 'i' || s[sl / 2 + 1] != 'i') return 0;\n int l = sl / 2 - 2, r = sl / 2 + 2;\n while(l >= 0 && r < sl){\n bool f = 0;\n REP(i, 8) if(s[l] == L[i] && s[r] == R[i]) f = 1;\n if(!f) return 0;\n --l; ++r;\n }\n return 1;\n}\n\nint main() {\n string s; cin >>s;\n int sl = s.length(), ans = 3;\n REP(b, (1 << sl)){\n stringstream ss;\n REP(i, sl) if(1 & (b >> i)) ss << s[i];\n if(ss.str().length() < 3 || ss.str().length() % 2 == 0) continue;\n if(check(ss.str())) ans = max(ans, (int)ss.str().length());\n }\n cout <<ans <<endl;\n return 0;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 1212, "score_of_the_acc": -0.7272, "final_rank": 6 }, { "submission_id": "aoj_2261_1162999", "code_snippet": "#include<iostream>\n#include<algorithm>\n#include<string>\nusing namespace std;\n\n\ninline int popcount(unsigned int x){\n\tx = (x>>1 & 0x55555555)+(x & 0x55555555);\n\tx = (x>>2 & 0x33333333)+(x & 0x33333333);\n\tx = (x>>4 & 0x0f0f0f0f)+(x & 0x0f0f0f0f);\n\tx = (x>>8 & 0x00ff00ff)+(x & 0x00ff00ff);\n\treturn (x>>16)+(x & 0x0000ffff);\n}\n\nint main(){\n\tchar rev[256];\n\tfor(int i=0;i<256;i++)rev[i]=i;\n\trev['(']=')';\n\trev['{']='}';\n\trev['[']=']';\n\trev[')']='(';\n\trev['}']='{';\n\trev[']']='[';\n\t\t\n\tstring s;\n\tint ans=0;\n\tcin>>s;\n\tint n=s.size();\n\tfor(int i=1;i<(1<<n);i++){\n\t\tstring t;\n\t\tfor(int j=0;j<n;j++){\n\t\t\tif(i>>j&1)t+=s[j];\n\t\t}\n\t\tif(t.find(\"iwi\",0)==string::npos)continue;\n\t\tint len=popcount(i);\n\t\tif(len<=ans)continue;\n\t\tbool ok=true;\n\t\tfor(int j=0;j<len/2;j++){\n\t\t\tif(rev[t[j]]!=t[len-1-j])ok=false;\n\t\t}\n\t\tif(len%2&&t[len/2]!=rev[t[len/2]])ok=false;\n\t\tif(ok)ans=len;\n\t}\n\tcout<<ans<<endl;\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1196, "score_of_the_acc": -0.0055, "final_rank": 2 }, { "submission_id": "aoj_2261_1162995", "code_snippet": "#include<iostream>\n#include<algorithm>\n#include<string>\nusing namespace std;\n\n\ninline int popcount(unsigned int x){\n\tx = (x>>1 & 0x55555555)+(x & 0x55555555);\n\tx = (x>>2 & 0x33333333)+(x & 0x33333333);\n\tx = (x>>4 & 0x0f0f0f0f)+(x & 0x0f0f0f0f);\n\tx = (x>>8 & 0x00ff00ff)+(x & 0x00ff00ff);\n\treturn (x>>16)+(x & 0x0000ffff);\n}\n\nint main(){\n\tchar rev[256];\n\tfor(int i=0;i<256;i++)rev[i]=i;\n\trev['(']=')';\n\trev['{']='}';\n\trev['[']=']';\n\trev[')']='(';\n\trev['}']='{';\n\trev[']']='[';\n\t\t\n\tstring s;\n\tint ans=0;\n\tcin>>s;\n\tint n=s.size();\n\tfor(int i=1;i<(1<<n);i++){\n\t\tstring t;\n\t\tfor(int j=0;j<n;j++){\n\t\t\tif(i>>j&1)t+=s[j];\n\t\t}\n\t\tint len=popcount(i);\n\t\tif(len<=ans)continue;\n\t\tbool ok=true;\n\t\tfor(int i=0;i<len/2;i++){\n\t\t\tif(rev[t[i]]!=t[len-1-i])ok=false;\n\t\t}\n\t\tif(len%2&&t[len/2]!=rev[t[len/2]])ok=false;\n\t\tif(ok)ans=len;\n\t}\n\tcout<<ans<<endl;\n\treturn 0;\n}", "accuracy": 0.13513513513513514, "time_ms": 10, "memory_kb": 1184, "score_of_the_acc": 0, "final_rank": 15 } ]
aoj_2263_cpp
問題 E : ファーストアクセプタンス プログラミングコンテストでは,各問題に関して, その問題を最初に正解した人の名前や正解時間が, ファーストアクセプタンス(最初の正解)として解説等でしばしば言及される. 久しぶりにプログラミングコンテストに参加するとはいえ,予選で落ちるとは到底思えない. ならば,最初のうちは,多少高いスコアを取ることを目指すよりも, ファーストアクセプタンスを多く獲得し,存在をアピールしたほうが良いのではないか. 自分の実力を持ってすれば,各問題を見た瞬間に, その問題を自分が何分で解くことができるかと, その問題が開始後何分で自分以外の参加者によって最初に解かれるかがわかる. これらの情報を用いて, どの程度ファーストアクセプタンスを獲得できるかを計算するプログラムを作っておこう. 問題 N 問の問題から成るプログラミングコンテストを考える. 問題 i を自分が解くには A i 秒の時間がかかり, また,問題 i は開始後 B i 秒で自分以外の参加者によって最初に解かれるとする. 自分が問題を解く順番は自由に決められるが, 1 つの問題を解き始めたら,解き終えるまでその問題をやるものとする. 問題を解き終え次の問題に取りかかる際などの, 問題を解いている時間以外の時間は十分小さいと考え,無視して考えることにする. また,全ての問題は終了までに 1 人以上の自分以外の参加者によって解かれるものと考える. 自分以外の参加者によって最初に解かれるよりも早く, あるいは同時に問題 i を自分が解きおえた時, 問題 i のファーストアクセプタンスを獲得できる. すなわち,問題 i を自分が開始後 t i 秒に解き終えたとしたとき, t i ≤ B i であれば,問題 i のファーストアクセプタンスを獲得できる. 最大で何個の問題に関してファーストアクセプタンスが獲得できるかを計算するプログラムを作成せよ. 入力 入力の最初の行は 1 つの整数 N を含む. 続く N 行には,各問題に関する情報が与えられる. これらの行のうちの i 行目には 2 個の数字 A i , B i が書かれている. 出力 最大で獲得することのできるファーストアクセプタンスの数を出力せよ. 制約 1 ≤ N ≤ 1000 1 ≤ A i ≤ 10 6 ( 1 ≤ i ≤ N ) 1 ≤ B i ≤ 10 6 ( 1 ≤ i ≤ N ) 部分点 この問題の判定には,20 点分のテストケースのグループが設定されている. このグループに含まれるテストケースの入力は以下を満たす. 1 ≤ N ≤ 16 入出力例 入出力例 1 入力例 1: 3 3 5 5 9 10 20 入力例 1 に対する出力例: 3 入出力例 2 入力例 2: 3 3 2 5 15 10 12 入力例 2 に対する出力例: 2
[ { "submission_id": "aoj_2263_10222670", "code_snippet": "#include <bits/stdc++.h> \nusing namespace std;\nusing ll=long long;\nusing dll=long double;\nusing pq=priority_queue<int,vector<int>,greater<int>>;\nusing graph=vector<vector<int>>;\n\n#define int ll\n#define db dll\n#define sbt(x) (x).begin(),(x).end()\n#define gyaku(x) reverse(sbt(x))\n#define vset(x) x.erase(unique(x.begin(),x.end()),x.end())\n#define so(x) sort(x.begin(),x.end())\n\n/* I miss the old Kanye */\n#define fi first\n#define se second\n#define vi vector<int>\n#define P pair<int,int>\n#define pb push_back\n#define r() cout<<'\\n'\n\ntypedef unsigned long long ull;\n\nconst ll mod=1000000007;\nconst ll modint=998244353;\nconst ll INF=1LL<<60;\nconst double pi=3.141592653589793;\n\nconst int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};\nconst int dy[8] = {0, 1, 0, -1, -1, 1, -1, 1};\n// const int dx[4] = {1,0,-1,0}; \n// const int dy[4] = {0,1,0,-1};\nconst string YesNo[2]={\"No\",\"Yes\"};\nvoid Yes(int ok=1){cout<<YesNo[ok]<<'\\n';}\n\n\ntemplate<class t,class u> void chmax(t&a,u b){if(a<b)a=b;}\ntemplate<class t,class u> void chmin(t&a,u b){if(b<a)a=b;}\ntemplate<typename T> void o(const T&x){cout<<x;r();}\ntemplate<class T>\nistream &operator>>(istream &is,vector<T>&v){\n for(T &t:v){\n is>>t;\n }\n return is;\n}\n\nint gcd(int a,int b){\n return b?gcd(b,a%b):a;\n}\n\nint lcm(int a,int b){\n return a/gcd(a,b)*b;\n}\n\null powm(ull a,ull b,ull mod){\n ull p=a,ans=1;\n for(int i=0;i<60;++i){\n if((b&(1ll<<i))!=0){\n ans*=p;ans%=mod;\n }\n p*=p;p%=mod;\n }\n return ans;\n}\n\nstruct UnionFind{\n vector<int> par,rank,siz;\n UnionFind(int no):par(no,-1),rank(no,0),siz(no,1){ }\n\n int root(int x){\n if(par[x]==-1)return x;\n else return par[x]=root(par[x]);\n }\n bool same(int x,int y){\n return root(x)==root(y);\n }\n bool _union(int x,int y){\n int rx=root(x),ry=root(y);\n if(rx==ry)return false;\n if(rank[rx]<rank[ry])swap(rx,ry);\n par[ry]=rx;\n if(rank[rx]==rank[ry])++rank[rx];\n siz[rx]+=siz[ry];\n return true;\n }\n int size(int x){\n return siz[root(x)];\n }\n};\n\nstruct segki{\n int size=1;\n vector<int> seg;\n\n void b(int sz){\n while(size<=sz)size*=2;\n seg.resize(size*2,INF);\n }\n void update(int pos,int x){\n pos+=size;\n seg[pos]=x;\n while(pos>=1){\n pos>>=1; // mid\n seg[pos]=min(seg[pos*2],seg[pos*2+1]);\n }\n }\n int _query(int l,int r,int a,int b,int pos){\n if(l<=a&&b<=r)return seg[pos];\n if(r<=a||b<=l)return INF;\n int x=_query(l,r,a,(a+b)/2,pos*2);\n int y=_query(l,r,(a+b)/2,b,pos*2+1);\n return min(x,y);\n }\n int query(int l,int r){\n return _query(l,r,0,size,1);\n }\n};\n\nsigned main(){\n int n;cin>>n;\n vector<P> v(n);\n for(int i=0;i<n;++i){\n cin>>v[i].fi>>v[i].se;\n }\n vector<vi> dp(n+1,vi(1111111,-INF));\n dp[0][0]=0;\n for(int i=0;i<n;++i){\n int x=v[i].fi,y=v[i].se;\n for(int j=0;j<=1000000;++j){\n if(j+x<=y)chmax(dp[i+1][j+x],dp[i][j]+1);\n chmax(dp[i+1][j],dp[i][j]);\n }\n }\n int ans=-INF;\n for(int i=0;i<=1000000;++i){\n chmax(ans,dp[n][i]);\n }\n o(ans);\n}", "accuracy": 0.02857142857142857, "time_ms": 30, "memory_kb": 141904, "score_of_the_acc": -1.0017, "final_rank": 18 }, { "submission_id": "aoj_2263_10222651", "code_snippet": "#include <bits/stdc++.h> \nusing namespace std;\nusing ll=long long;\nusing dll=long double;\nusing pq=priority_queue<int,vector<int>,greater<int>>;\nusing graph=vector<vector<int>>;\n\n#define int ll\n#define db dll\n#define sbt(x) (x).begin(),(x).end()\n#define gyaku(x) reverse(sbt(x))\n#define vset(x) x.erase(unique(x.begin(),x.end()),x.end())\n#define so(x) sort(x.begin(),x.end())\n\n/* I miss the old Kanye */\n#define fi first\n#define se second\n#define vi vector<int>\n#define P pair<int,int>\n#define pb push_back\n#define r() cout<<'\\n'\n\ntypedef unsigned long long ull;\n\nconst ll mod=1000000007;\nconst ll modint=998244353;\nconst ll INF=1LL<<60;\nconst double pi=3.141592653589793;\n\nconst int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};\nconst int dy[8] = {0, 1, 0, -1, -1, 1, -1, 1};\n// const int dx[4] = {1,0,-1,0}; \n// const int dy[4] = {0,1,0,-1};\nconst string YesNo[2]={\"No\",\"Yes\"};\nvoid Yes(int ok=1){cout<<YesNo[ok]<<'\\n';}\n\n\ntemplate<class t,class u> void chmax(t&a,u b){if(a<b)a=b;}\ntemplate<class t,class u> void chmin(t&a,u b){if(b<a)a=b;}\ntemplate<typename T> void o(const T&x){cout<<x;r();}\ntemplate<class T>\nistream &operator>>(istream &is,vector<T>&v){\n for(T &t:v){\n is>>t;\n }\n return is;\n}\n\nint gcd(int a,int b){\n return b?gcd(b,a%b):a;\n}\n\nint lcm(int a,int b){\n return a/gcd(a,b)*b;\n}\n\null powm(ull a,ull b,ull mod){\n ull p=a,ans=1;\n for(int i=0;i<60;++i){\n if((b&(1ll<<i))!=0){\n ans*=p;ans%=mod;\n }\n p*=p;p%=mod;\n }\n return ans;\n}\n\nstruct UnionFind{\n vector<int> par,rank,siz;\n UnionFind(int no):par(no,-1),rank(no,0),siz(no,1){ }\n\n int root(int x){\n if(par[x]==-1)return x;\n else return par[x]=root(par[x]);\n }\n bool same(int x,int y){\n return root(x)==root(y);\n }\n bool _union(int x,int y){\n int rx=root(x),ry=root(y);\n if(rx==ry)return false;\n if(rank[rx]<rank[ry])swap(rx,ry);\n par[ry]=rx;\n if(rank[rx]==rank[ry])++rank[rx];\n siz[rx]+=siz[ry];\n return true;\n }\n int size(int x){\n return siz[root(x)];\n }\n};\n\nstruct segki{\n int size=1;\n vector<int> seg;\n\n void b(int sz){\n while(size<=sz)size*=2;\n seg.resize(size*2,INF);\n }\n void update(int pos,int x){\n pos+=size;\n seg[pos]=x;\n while(pos>=1){\n pos>>=1; // mid\n seg[pos]=min(seg[pos*2],seg[pos*2+1]);\n }\n }\n int _query(int l,int r,int a,int b,int pos){\n if(l<=a&&b<=r)return seg[pos];\n if(r<=a||b<=l)return INF;\n int x=_query(l,r,a,(a+b)/2,pos*2);\n int y=_query(l,r,(a+b)/2,b,pos*2+1);\n return min(x,y);\n }\n int query(int l,int r){\n return _query(l,r,0,size,1);\n }\n};\n\nsigned main(){\n int n;cin>>n;\n vector<P> v(n);\n for(int i=0;i<n;++i){\n cin>>v[i].fi>>v[i].se;\n }\n sort(sbt(v),[&](P x,P y){return x.se<y.se;});\n vector<vi> dp(n+1,vi(1111111,-INF));\n dp[0][0]=0;\n for(int i=0;i<n;++i){\n int x=v[i].fi,y=v[i].se;\n for(int j=0;j<=1000000;++j){\n if(j+x<=y)chmax(dp[i+1][j+x],dp[i][j]+1);\n chmax(dp[i+1][j],dp[i][j]);\n }\n }\n int ans=-INF;\n for(int i=0;i<=1000000;++i){\n chmax(ans,dp[n][i]);\n }\n o(ans);\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 159340, "score_of_the_acc": -1.1667, "final_rank": 12 }, { "submission_id": "aoj_2263_4855505", "code_snippet": "//#include <bits/stdc++.h>\n#include <iostream>\n#include <algorithm>\n#include <vector>\n#include <utility>\n\ntypedef long long ll;\n\nusing namespace std;\n\n\nconst ll mod=1000000000+7;\n\n\n//ll dp[1001][1000001];\n\nvector<pair<ll,ll> > vp;\n\n\n\nbool comparep(pair<ll,ll> a,pair<ll,ll> b){\n\n if(a.second!=b.second){\n return a.second<b.second;\n \n }else{\n return a.first<b.first;\n \n }\n } \n\n\nint main(){\n\n int N;\n cin>>N;\n\n ll a,b;\n\n \n\n for(int i=0;i<N;i++){\n\n cin>>a>>b;\n\n if(a>b)continue;\n\n vp.push_back(make_pair(a,b));\n }\n\n sort(vp.begin(),vp.end(),comparep);\n \n\n/*\n for(ll i=0;i<vp.size();i++){\n cout<<vp[i].first<<\":\"<<vp[i].second<<endl;\n }\n*/\n\n ll vsize=vp.size();\n\n if(vsize==0){\n cout<<0<<endl;\n return 0;\n }\n\n ll last=vp[vsize-1].second;\n\n // cout<<\"last:\"<<last<<endl;\n\n vector<vector<ll> > dp(vsize+1,vector<ll>(last+1,0));\n\n for(ll i=0;i<vsize;i++){\n for(ll j=0;j<=last;j++){\n if(j<vp[i].first){\n dp[i+1][j]=dp[i][j];\n }else if(j>=vp[i].first){\n if(j<=vp[i].second){\n dp[i+1][j]=max(dp[i][j-vp[i].first]+1,dp[i][j]);\n }else{\n dp[i+1][j]=max(dp[i+1][vp[i].first],max(dp[i][j-vp[i].first],dp[i][j]));\n }\n \n }\n }\n }\n/*\n for(ll i=0;i<=vsize;i++){ \n for(ll j=0;j<=vp[vsize-1].second;j++){\n cout<<dp[i][j]<<\" \";\n }\n cout<<endl;\n }\n */\n/*\n for(int i=0;i<vsize;i++){\n cout<<dp[i][last]<<endl;\n }\n */\n cout<<dp[vsize][last]<<endl;\n\n \n\n}", "accuracy": 0.42857142857142855, "time_ms": 70, "memory_kb": 137628, "score_of_the_acc": -1.1971, "final_rank": 14 }, { "submission_id": "aoj_2263_4855124", "code_snippet": "//#include <bits/stdc++.h>\n#include <iostream>\n#include <algorithm>\n#include <vector>\n#include <utility>\n\ntypedef long long ll;\n\nusing namespace std;\n\n\nconst ll mod=1000000000+7;\n\n\n//ll dp[1001][1000001];\n\nvector<pair<ll,ll> > vp;\n\n\n\nbool comparep(pair<ll,ll> a,pair<ll,ll> b){\n\n if(a.second!=b.second){\n return a.second<b.second;\n \n }else{\n return a.first<b.first;\n \n }\n } \n\n\nint main(){\n\n int N;\n cin>>N;\n\n ll a,b;\n\n \n\n for(int i=0;i<N;i++){\n\n cin>>a>>b;\n\n if(a>b)continue;\n\n vp.push_back(make_pair(a,b));\n }\n\n sort(vp.begin(),vp.end(),comparep);\n \n\n/*\n for(ll i=0;i<vp.size();i++){\n cout<<vp[i].first<<\":\"<<vp[i].second<<endl;\n }\n*/\n\n ll vsize=vp.size();\n\n if(vsize==0){\n cout<<0<<endl;\n return 0;\n }\n\n ll last=vp[vsize-1].second;\n\n // cout<<\"last:\"<<last<<endl;\n\n vector<vector<ll> > dp(vsize+1,vector<ll>(last+1,0));\n\n for(ll i=0;i<vsize;i++){\n for(ll j=0;j<=last;j++){\n if(j<vp[i].first){\n dp[i+1][j]=dp[i][j];\n }else{\n dp[i+1][j]=max(dp[i][j-vp[i].first]+1,dp[i][j]);\n }\n }\n }\n/*\n for(ll i=0;i<=vsize;i++){\n for(ll j=0;j<=vp[vsize-1].second;j++){\n cout<<dp[i][j]<<\" \";\n }\n cout<<endl;\n }\n */\n/*\n for(int i=0;i<vsize;i++){\n cout<<dp[i][last]<<endl;\n }\n */\n cout<<dp[vsize][last]<<endl;\n\n \n\n}", "accuracy": 0.14285714285714285, "time_ms": 40, "memory_kb": 88168, "score_of_the_acc": -0.72, "final_rank": 16 }, { "submission_id": "aoj_2263_4854986", "code_snippet": "#include <iostream>\n#include <algorithm>\n#include <vector>\n#include <utility>\n\ntypedef long long ll;\n\nusing namespace std;\n\n\nconst ll mod=1000000000+7;\n\n\n//ll dp[1001][1000001];\n\nvector<pair<ll,ll> > vp;\n\n\n\nbool comparep(pair<ll,ll> a,pair<ll,ll> b){\n\n if(a.second!=b.second){\n return a.second<b.second;\n \n }else{\n return a.first<b.first;\n \n }\n } \n\n\nint main(){\n\n int N;\n cin>>N;\n\n ll a,b;\n\n \n\n for(int i=0;i<N;i++){\n\n cin>>a>>b;\n\n if(a>b)continue;\n\n vp.push_back(make_pair(a,b));\n }\n\n sort(vp.begin(),vp.end(),comparep);\n \n\n/*\n for(ll i=0;i<vp.size();i++){\n cout<<vp[i].first<<\":\"<<vp[i].second<<endl;\n }\n */ \n\n ll vsize=vp.size();\n\n if(vsize==0){\n cout<<0<<endl;\n return 0;\n }\n\n ll last=vp[vsize-1].second;\n\n vector<vector<ll> > dp(vsize+1,vector<ll>(last+1,0));\n\n for(ll i=0;i<vsize;i++){\n for(ll j=0;j<=last;j++){\n if(j<vp[i].first){\n dp[i+1][j]=dp[i][j];\n }else{\n dp[i+1][j]=dp[i][j-vp[i].first]+1;\n }\n }\n }\n/*\n for(ll i=0;i<=vsize;i++){\n for(ll j=0;j<=vp[vsize-1].second;j++){\n cout<<dp[i][j]<<\" \";\n }\n cout<<endl;\n }\n */\n \n cout<<dp[vsize][vp[vsize-1].second]<<endl;\n\n \n\n}", "accuracy": 0.11428571428571428, "time_ms": 30, "memory_kb": 79896, "score_of_the_acc": -0.6125, "final_rank": 17 }, { "submission_id": "aoj_2263_2178431", "code_snippet": "#include <iostream>\n#include <algorithm>\nusing namespace std;\nint n, a[1009], b[1009], dp[1000009];\nint main() {\n\tcin >> n;\n\tfor (int i = 0; i < n; i++) cin >> a[i] >> b[i];\n\tfor (int i = 0; i < n; i++) {\n\t\tfor (int j = i + 1; j < n; j++) {\n\t\t\tif (b[i] > b[j]) {\n\t\t\t\tswap(a[i], a[j]);\n\t\t\t\tswap(b[i], b[j]);\n\t\t\t}\n\t\t}\n\t}\n\tfor (int i = 0; i < n; i++) {\n\t\tfor (int j = b[i]; j >= a[i]; j--) {\n\t\t\tdp[j] = max(dp[j], dp[j - a[i]] + 1);\n\t\t}\n\t}\n\tcout << *max_element(dp, dp + 1000001) << endl;\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 6876, "score_of_the_acc": -0.0432, "final_rank": 9 }, { "submission_id": "aoj_2263_1408393", "code_snippet": "#include<iostream>\n#include<vector>\n#include<algorithm>\n#include <iterator>\n#include<queue>\n#include <functional>\n#include <string>\n#include <numeric>\n#include<stack>\nusing namespace std;\n#define REP(i,n) for(int i=0;i<(n);i++)\n#define ALL(v) v.begin(),v.end()\nusing namespace std;\n\n\nstruct prog{\n\tint mytime;\n\tint limtime;\n};\n\n\nint main(){\n\n\tint N;\n\tcin >>N;\n\tvector<prog>progCol(N);\n\tint sum=0;\n\tREP(i, N){\n\t\tint a, b;\n\t\t\n\t\tcin >> a >> b;\n\t\tsum = max(sum,b);\n\t\tprogCol[i] = { a, b };\n\t}\n\tvector<vector<int>>dp(sum+3, vector<int>(N));\n\tsort(progCol.begin(), progCol.end(), [](prog p1, prog p2){return p1.limtime < p2.limtime; });\n\tREP(i, sum+1){\n\t\tREP(j, N){\n\t\t\tif (progCol[j].mytime > progCol[j].limtime){\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tif (i < progCol[j].mytime){ dp[i][j] = 0; }\n\t\t\telse if (i == progCol[j].mytime){\n\t\t\t\tdp[i][j] = 1;\n\t\t\t}\n\t\t\telse{\n\t\t\t\tint temp = dp[i-1][j];\n\t\t\t\tif (progCol[j].mytime <= i){\n\t\t\t\t\tREP(k, j){\n\t\t\t\t\t\tif (progCol[j].limtime >= i){\n\t\t\t\t\t\t\ttemp = max(temp, dp[i - progCol[j].mytime][k] + 1);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tdp[i][j] = temp;\n\t\t\t}\n\t\t}\n\t}\n\tcout << (*max_element(dp[sum].begin(), dp[sum].end())) << endl;\n\treturn 0;\n}", "accuracy": 1, "time_ms": 190, "memory_kb": 102484, "score_of_the_acc": -1.6432, "final_rank": 13 }, { "submission_id": "aoj_2263_1408377", "code_snippet": "#include<iostream>\n#include<vector>\n#include<algorithm>\n#include <iterator>\n#include<queue>\n#include <functional>\n#include <string>\n#include <numeric>\n#include<stack>\nusing namespace std;\n#define REP(i,n) for(int i=0;i<(n);i++)\n#define ALL(v) v.begin(),v.end()\nusing namespace std;\n\n\nstruct prog{\n\tint mytime;\n\tint limtime;\n};\n\n\nint main(){\n\n\tint N;\n\tcin >>N;\n\tvector<prog>progCol(N);\n\tint sum=0;\n\tREP(i, N){\n\t\tint a, b;\n\t\t\n\t\tcin >> a >> b;\n\t\tsum = max(sum,b);\n\t\tprogCol[i] = { a, b };\n\t}\n\tvector<vector<int>>dp(sum+3, vector<int>(N));\n\tsort(progCol.begin(), progCol.end(), [](prog p1, prog p2){return p1.limtime < p2.limtime; });\n\tREP(i, sum){\n\t\tREP(j, N){\n\t\t\tif (progCol[j].mytime > progCol[j].limtime){\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tif (i < progCol[j].mytime){ dp[i][j] = 0; }\n\t\t\telse if (i == progCol[j].mytime){\n\t\t\t\tdp[i][j] = 1;\n\t\t\t}\n\t\t\telse{\n\t\t\t\tint temp = dp[i-1][j];\n\t\t\t\tif (progCol[j].mytime <= i){\n\t\t\t\t\tREP(k, j){\n\t\t\t\t\t\tif (progCol[j].limtime >= i){\n\t\t\t\t\t\t\ttemp = max(temp, dp[i - progCol[j].mytime][k] + 1);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tdp[i][j] = temp;\n\t\t\t}\n\t\t}\n\t}\n\tcout << (*max_element(dp[sum - 1].begin(), dp[sum - 1].end())) << endl;\n\treturn 0;\n}", "accuracy": 0.2857142857142857, "time_ms": 160, "memory_kb": 101956, "score_of_the_acc": -1.4732, "final_rank": 15 }, { "submission_id": "aoj_2263_1118670", "code_snippet": "#include <iostream>\n#include <sstream>\n#include <cstdio>\n#include <cstdlib>\n#include <cmath>\n#include <ctime>\n#include <cstring>\n#include <string>\n#include <vector>\n#include <stack>\n#include <queue>\n#include <deque>\n#include <map>\n#include <set>\n#include <bitset>\n#include <numeric>\n#include <utility>\n#include <iomanip>\n#include <algorithm>\n#include <functional>\nusing namespace std;\n\ntypedef long long ll;\ntypedef vector<int> vint;\ntypedef vector<long long> vll;\ntypedef pair<int,int> pint;\ntypedef pair<long long, long long> pll;\n\n#define MP make_pair\n#define PB push_back\n#define ALL(s) (s).begin(),(s).end()\n#define EACH(i, s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)\n#define COUT(x) cout << #x << \" = \" << (x) << \" (L\" << __LINE__ << \")\" << endl\n\ntemplate<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }\ntemplate<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }\ntemplate<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P) \n{ return s << '<' << P.first << \", \" << P.second << '>'; }\ntemplate<class T> ostream& operator << (ostream &s, vector<T> P) \n{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << \" \"; } s << P[i]; } return s; }\ntemplate<class T> ostream& operator << (ostream &s, vector<vector<T> > P) \n{ for (int i = 0; i < P.size(); ++i) { s << endl << P[i]; } return s << endl; }\ntemplate<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P) \n{ EACH(it, P) { s << \"<\" << it->first << \"->\" << it->second << \"> \"; } return s; }\n\nint N;\npll list[2100];\n\nconst long long INF = 1LL<<60;\nlong long dp[2100][2100];\n\nint main() {\n //freopen( \"/Users/macuser/Dropbox/Contest/input.in\", \"r\", stdin );\n while (cin >> N) {\n for (int i = 0; i < N; ++i) cin >> list[i].second >> list[i].first;\n sort(list, list+N);\n \n for (int i = 0; i < 2100; ++i) for (int j = 0; j < 2100; ++j) dp[i][j] = INF;\n dp[0][0] = 0;\n \n int res = 0;\n for (int i = 0; i <= N; ++i) {\n for (int j = 0; j <= N; ++j) {\n if (dp[i][j] < INF) chmax(res, j);\n \n long long alt = dp[i][j] + list[i].second;\n if (alt <= list[i].first) {\n chmin(dp[i+1][j+1], alt);\n }\n chmin(dp[i+1][j], dp[i][j]);\n }\n }\n \n cout << res << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 35660, "score_of_the_acc": -0.2238, "final_rank": 11 }, { "submission_id": "aoj_2263_566038", "code_snippet": "#include<stdio.h>\n#include<map>\n#include<vector>\n#include<algorithm>\nstruct S{\n\tint a,b;\n\tbool operator<(const S& s)const{\n\t\treturn b<s.b;\n\t}\n};\n\nint main(){\n\tint n,a,b,ans=0;\n\tstd::map<int,int> memo,nextMemo;\n\tmemo[0]=0;\n\tscanf(\"%d\",&n);\n\tS s;\n\tstd::vector<S> vec;\n\tfor(int i=0;i<n;i++){\n\t\tscanf(\"%d %d\",&s.a,&s.b);\n\t\tvec.push_back(s);\n\t}\n\tstd::sort(vec.begin(),vec.end());\n\tfor(int i=0;i<vec.size();i++){\n\t\ta=vec[i].a;\n\t\tb=vec[i].b;\n\t\tfor(std::map<int,int>::iterator it=memo.begin();it!=memo.end();it++){\n\t\t\tint t=(*it).first+a;\n\t\t\tint t2=(*it).second+1;\n\t\t\t\n\t\t\tint t3;\n\t\t\tif(t>b)break;\n\t\t\tif(memo.find(t)==memo.end()){\n\t\t\t\tif(nextMemo.find(t)==nextMemo.end()||nextMemo[t]<t2)nextMemo[t]=t2;\n\t\t\t\tt3=t2>nextMemo[t]?t2:nextMemo[t];\n\t\t\t}else if(memo[t]<t2){\n\t\t\t\tmemo[t]=t2;\n\t\t\t\tt3=t2;\n\t\t\t}else{\n\t\t\t\tt3=memo[t];\n\t\t\t}\n\t\t\tif(ans<t3)ans=t3;\n\t\t}\n\t\tmemo.insert(nextMemo.begin(),nextMemo.end());\n\t\tnextMemo.clear();\n\t}\n\tprintf(\"%d\\n\",ans);\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 5496, "score_of_the_acc": -0.1456, "final_rank": 10 }, { "submission_id": "aoj_2263_414832", "code_snippet": "#include <iostream>\n#include <vector>\n#include <map>\n#include <algorithm>\nusing namespace std;\n\ntypedef pair<int,int> P;\n\n// A[i] := i ツ氾板姪堋づ個姪「ツ妥ィツづーツ嘉ーツつュツづ個づ可つゥツつゥツづゥツ篠楪甘?\nint A[16] = {0};\nint ans = 0;\n\nvoid solve(vector<P> v, int pos, int total, int AC){\n\tif( pos == v.size() ){\n\t\tans = max( ans , AC );\n\t\treturn ;\n\t}\n\t\n\tint time = v[pos].first;\n\tint i = v[pos].second;\n\tif( total + A[i] <= time ){\n\t\tsolve( v , pos+1 , total + A[i] , AC+1 );\n\t}\n\tsolve( v , pos+1 , total , AC );\n}\n\nint main(){\n\tint n;\n\tvector<P> v;\n\t\n\tcin >> n;\n\tfor(int i=0 ; i < n ; i++ ){\n\t\tint a, b;\n\t\tcin >> a >> b;\n\t\tA[i] = a;\n\t\tv.push_back( P(b,i) );\n\t}\n\tsort( v.begin() , v.end() );\n\tsolve( v , 0 , 0 , 0 );\n\tcout << ans << endl;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 0, "score_of_the_acc": 0, "final_rank": 1 }, { "submission_id": "aoj_2263_414831", "code_snippet": "#include <iostream>\n#include <vector>\n#include <map>\n#include <algorithm>\nusing namespace std;\n\ntypedef pair<int,int> P;\n\n// A[i] := i 番目の問題を解くのにかかる時間\nint A[16] = {0};\nint ans = 0;\n\nvoid solve(vector<P> v, int pos, int total, int AC){\n\tif( pos == v.size() ){\n\t\tans = max( ans , AC );\n\t\treturn ;\n\t}\n\t\n\tint time = v[pos].first;\n\tint i = v[pos].second;\n\tif( total + A[i] <= time ){\n\t\tsolve( v , pos+1 , total + A[i] , AC+1 );\n\t}\n\tsolve( v , pos+1 , total , AC );\n}\n\nint main(){\n\tint n;\n\tvector<P> v;\n\t\n\tcin >> n;\n\tfor(int i=0 ; i < n ; i++ ){\n\t\tint a, b;\n\t\tcin >> a >> b;\n\t\tA[i] = a;\n\t\tv.push_back( P(b,i) );\n\t}\n\tsort( v.begin() , v.end() );\n\tsolve( v , 0 , 0 , 0 );\n\tcout << ans << endl;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 0, "score_of_the_acc": 0, "final_rank": 1 }, { "submission_id": "aoj_2263_293183", "code_snippet": "#include <iostream>\n#include <fstream>\n#include <algorithm>\n#include <bitset>\n#include <cassert>\n#include <cctype>\n#include <cmath>\n#include <complex>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <deque>\n#include <iomanip>\n#include <map>\n#include <numeric>\n#include <queue>\n#include <set>\n#include <stack>\n#include <sstream>\n#include <string>\n#include <vector>\nusing namespace std;\n\n#define EPS 1e-9\n#define INF MOD\n#define MOD 1000000007LL\n#define fir first\n#define iss istringstream\n#define sst stringstream\n#define ite iterator\n#define ll long long\n#define mp make_pair\n#define rep(i,n) rep2(i,0,n)\n#define rep2(i,m,n) for(int i=m;i<n;i++)\n#define pi pair<int,int>\n#define pb push_back\n#define sec second\n#define sh(i) (1LL<<i)\n#define sz size()\n#define vi vector<int>\n#define vc vector\n#define vl vector<ll>\n#define vs vector<string>\n\nint n,dp[1010][1010];\npi p[1010];\n\nint f(int c,int a){\n\tint& r=dp[c][a];\n\tif(r<INF)return r;\n\tif(!c)return r=a?INF:0;\n\tif(!a)return r=0;\n\tr=f(c-1,a);\n\tif(f(c-1,a-1)+p[c-1].sec<=p[c-1].fir)r=min(r,f(c-1,a-1)+p[c-1].sec);\n\treturn r;\n}\n\nint main(){\n\tcin>>n;\n\trep(i,n)cin>>p[i].sec>>p[i].fir;\n\tsort(p,p+n);\n\trep(i,n+1)rep(j,n+1)dp[i][j]=INF;\n\tfor(int i=n;;i--)if(f(n,i)<INF){\n\t\tcout<<i<<endl;\n\t\tbreak;\n\t}\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 0, "score_of_the_acc": 0, "final_rank": 1 }, { "submission_id": "aoj_2263_253331", "code_snippet": "#include<iostream>\n#include<algorithm>\nusing namespace std;\n\nint main(){\n int a[1000],b[1000];\n int p[1000000];\n int n,tmp,ans;\n\n cin >> n;\n for(int i=0;i<n;i++)cin >> a[i] >> b[i];\n for(int i=0;i<n-1;i++){\n for(int j=i+1;j<n;j++){\n if(b[i] > b[j]){\n\tswap(a[i],a[j]);\n\tswap(b[i],b[j]);\n }\n }\n }\n\n p[0] = 0;\n for(int i=1;i<=b[n-1];i++)p[i] = -1;\n\n for(int i=0;i<n;i++){\n for(int j=b[i]-a[i];j>=0;j--){\n if(p[j]>=0){\n\tif(p[j+a[i]]<p[j]+1)p[j+a[i]] = p[j]+1;\n } \n }\n }\n\n ans = 0;\n for(int i=0;i<=b[n-1];i++){\n if(ans<p[i])ans = p[i];\n }\n\n cout << ans << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 0, "score_of_the_acc": 0, "final_rank": 1 }, { "submission_id": "aoj_2263_253320", "code_snippet": "#include<iostream>\nusing namespace std;\n\nint main(){\n int n;\n int a[1000],b[1000];\n int p[1000000];\n int tmp,ans;\n\n cin >> n;\n for(int i=0;i<n;i++){\n cin >> a[i] >> b[i];\n }\n\n for(int i=0;i<n-1;i++){\n for(int j=i+1;j<n;j++){\n if(b[i] > b[j]){\n\ttmp = a[i];\n\ta[i] = a[j];\n\ta[j] = tmp;\n\ttmp = b[i];\n\tb[i] = b[j];\n\tb[j] = tmp;\n }\n }\n }\n\n p[0] = 0;\n for(int i=1;i<=b[n-1];i++)p[i] = -1;\n\n for(int i=0;i<n;i++){\n for(int j=b[i]-a[i];j>=0;j--){\n if(p[j]>=0){\n\tif(p[j+a[i]]<p[j]+1)p[j+a[i]] = p[j]+1;\n } \n }\n }\n\n ans = 0;\n for(int i=0;i<=b[n-1];i++){\n if(ans<p[i])ans = p[i];\n }\n\n cout << ans << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 0, "score_of_the_acc": 0, "final_rank": 1 }, { "submission_id": "aoj_2263_252274", "code_snippet": "#include<cstdio>\n#include<algorithm>\n\n#define rep(i,n) for(int i=0;i<(n);i++)\n\nusing namespace std;\n\ntypedef pair<int,int> pii;\n\nint main(){\n\tint n; scanf(\"%d\",&n);\n\tpii p[1000];\n\trep(i,n) scanf(\"%d%d\",&p[i].second,&p[i].first);\n\n\tsort(p,p+n);\n\n\tstatic int dp[1000001];\n\trep(i,n){\n\t\tint a=p[i].second,b=p[i].first;\n\t\tfor(int t=b;t>=max(a,0);t--) dp[t]=max(dp[t],dp[t-a]+1);\n\t}\n\tprintf(\"%d\\n\",*max_element(dp,dp+1000001));\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 0, "score_of_the_acc": 0, "final_rank": 1 }, { "submission_id": "aoj_2263_251763", "code_snippet": "#define _USE_MATH_DEFINES\n#include <iostream>\n#include <complex>\n#include <algorithm>\n#include <vector>\n#include <stack>\n#include <string>\n#include <queue>\n#include <cmath>\n#include <math.h>\n#include <numeric>\n#include <list>\n#include <sstream>\n#include <fstream>\n#include <iomanip>\n#include <climits>\n#include <set>\n#include <memory.h>\n#include <memory>\n#include <cstdio>\n#include <cstdlib>\n#include <cctype>\n#include <cassert>\n#include <map>\n#include <cassert>\n#include <time.h>\n#include <ctime>\n\nusing namespace std;\n\ntypedef complex<double> xy_t;\ntypedef pair<xy_t, xy_t> line;\ntypedef vector<xy_t> polygon;\ntypedef long long ll;\ntypedef pair<int, int> P;\ntypedef pair<int , P> PP;\ntypedef pair<string, int> Ps;\ntypedef vector<int> vec;\ntypedef vector<vec> mat;\nconst int INF = 1 << 29;\nconst double EPS = 1e-10;\nconst double PI = 3.1415926535897932384626433832795;\nconst int CLK = CLOCKS_PER_SEC;\n\n#define rep(i, n) for(int i = 0; i < n; i++)\n#define rep2(i, m, n) for(int i = m; i < n; i++)\n#define repD(i, n) for(int i = n; i >= 0; i--)\n\nint n;\nll dp[1010][1010];\nP prob[2000];\n\nint main(){\n\tcin >> n;\n\tfor(int i = 0;i < n; i++){\n\t\tcin >> prob[i].second >> prob[i].first;\n\t}\n\tsort(prob, prob + n);\n\tfill(&dp[0][0], &dp[1009][1009] + 1, INF);\n\tfor(int i = 0; i <= n; i++){\n\t\tdp[i][0] = 0;\n\t}\n\tfor(int i = 1; i <= n; i++){\n\t\tfor(int j = 1; j <= i; j++){\n\t\t\tif(dp[i-1][j-1] + prob[i-1].second <= prob[i-1].first){\n\t\t\t\tdp[i][j] = dp[i-1][j-1] + prob[i-1].second;\n\t\t\t}\n\t\t\tif(dp[i-1][j] <= prob[i-1].first){\n\t\t\t\tdp[i][j] = min(dp[i-1][j], dp[i][j]);\n\t\t\t}\n\t\t}\n\t}\n\tint res = 0;\n\tfor(int i = 1; i <= n; i++){\n\t\tif(dp[n][i] != INF) res = max(res, i);\n\t}\n\tcout << res << endl;\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 0, "score_of_the_acc": 0, "final_rank": 1 }, { "submission_id": "aoj_2263_251517", "code_snippet": "#include <stdio.h>\n#include <string.h>\n#include <algorithm>\n#include <iostream>\n#include <math.h>\n#include <assert.h>\n#include <vector>\n\nusing namespace std;\ntypedef long long ll;\ntypedef unsigned int uint;\ntypedef unsigned long long ull;\nstatic const double EPS = 1e-9;\nstatic const double PI = acos(-1.0);\n\n#define REP(i, n) for (int i = 0; i < (int)(n); i++)\n#define FOR(i, s, n) for (int i = (s); i < (int)(n); i++)\n#define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++)\n#define FORIT(it, c) for (__typeof((c).begin())it = (c).begin(); it != (c).end(); it++)\n#define MEMSET(v, h) memset((v), h, sizeof(v))\n\nint n;\nint ans;\nll dp[1010][1010];\npair<ll, ll> problem[1010];\n\nint main() {\n while (scanf(\"%d\", &n) > 0) {\n MEMSET(dp, 0x01);\n ans = 0;\n REP(i, n) {\n scanf(\"%lld %lld\", &problem[i].second, &problem[i].first);\n }\n sort(problem, problem + n);\n dp[0][0] = 0;\n REP(index, n) {\n FOREQ(solved, 0, index) {\n dp[index + 1][solved] = min(dp[index + 1][solved], dp[index][solved]);\n ll time = dp[index][solved] + problem[index].second;\n if (time <= problem[index].first) {\n ans = max(ans, solved + 1);\n dp[index + 1][solved + 1] = min(dp[index + 1][solved + 1], time);\n }\n }\n }\n printf(\"%d\\n\", ans);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 0, "score_of_the_acc": 0, "final_rank": 1 } ]
aoj_2262_cpp
問題 D : 停止問題 G○○gle Code Jam は G○○gle 社が年に 1 度開催するコンテストである. 優勝者は G○○gle への入社を許される,世界最高峰のコンテストだ. しかし勿論,それ以外の参加者は帰らぬ者となる. G○○gle Code Jam では自分の好きなプログラミング言語や処理系を使うことができる. 僕は Defunge という自らが開発したプログラミング言語で参加することにした. この言語を使えば,計算困難な問題はおろか,判定不能な問題ですら解決できる気がしている. 問題 与えられるプログラムが停止するかを判定するプログラムを作成せよ. 与えられるプログラムは,以下で説明するプログラミング言語 Defunge で記述されている. Defunge のプログラムの命令は 1 文字であり,1 次元の列ではなく 2 次元の格子状に並んでいる. 下図は,Defunge のプログラムの例である: 6>--v. .^--_@ Defunge の言語仕様は以下のようになっている. Defunge のプログラムは、左上のマスから右向きで開始する. (左上のマスの命令が最初に実行される.) 命令によって進む向きが上下左右に変更されることがある. 端に達したら反対側の端へジャンプする. メモリは 0 から 15 までの 1 つの整数を記憶することができる. メモリにははじめ 0 が記憶されている. Defunge の命令は以下の通りである. '<' … 実行の向きを左にする. '>' … 実行の向きを右にする. '^' … 実行の向きを上にする. 'v' … 実行の向きを下にする. '_' … メモリの値が 0 ならば実行の向きを右に,そうでなければ左にする. '|' … メモリの値が 0 ならば実行の向きを下に,そうでなければ上にする. '?' … 実行の向きが上下左右のいずれかにランダムに等確率で変更される. '.' … 何もしない. '@' … プログラムの実行を停止する. '0' - '9' … メモリの値を指定の数値にする. '+' … メモリの値に 1 を加える,ただし値が 15 だった場合 0 にする. '-' … メモリの値から 1 を引く,ただし値が 0 だった場合 15 にする. 入力 入力の最初の行は 2 つの整数 R, C を含む. 続く R 行はプログラムを表す。それぞれ C 文字の文字列を含む。 出力 プログラムが停止する可能性がある場合は YES と出力せよ. そうでないとき,NO と出力せよ. 制約 1 ≤ R ≤ 20 1 ≤ C ≤ 20 入出力例 入出力例 1 入力例 1: 2 6 6>--v. .^--_@ 入力例 1 に対する出力: YES 入出力例 2 入力例 2: 2 6 5>--v. .^--_@ 入力例 2 に対する出力: NO 入出力例 3 入力例 3: 2 6 .>--v. .^--?@ 入力例 3 に対する出力: YES 補足 Defunge は Befunge と類似している. Befunge の理解は Defunge の理解を助けるかもしれないが, 異なる点も多いため,注意せよ. http://ja.wikipedia.org/wiki/Befunge
[ { "submission_id": "aoj_2262_9264727", "code_snippet": "/* _ ____ */\n/* AC U /\"\\ u U /\"___| AC */\n/* \\/ _ \\/ \\| | u */\n/* AC / ___ \\ | |/__ AC */\n/* /_/ \\_\\ \\____| */\n/* AC \\\\ >> _// \\\\ AC */\n/* (__) (__)(__)(__) */\n/* github.com/NULLCT/Compro */\n\n#pragma GCC optimize(\"O3,unroll-loops\")\n#pragma GCC target(\"sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2\")\n\n#include <algorithm>\n#include <array>\n#include <bitset>\n#include <cassert>\n#include <cctype>\n#include <chrono>\n#include <cinttypes>\n#include <climits>\n#include <cmath>\n#include <complex>\n#include <cstddef>\n#include <cstdint>\n#include <cstdio>\n#include <cstdlib>\n#include <ctime>\n#include <deque>\n#include <fstream>\n#include <functional>\n#include <iomanip>\n#include <iostream>\n#include <iterator>\n#include <list>\n#include <map>\n#include <numeric>\n#include <queue>\n#include <random>\n#include <set>\n#include <stack>\n#include <stdexcept>\n#include <string>\n#include <tuple>\n#include <unordered_map>\n#include <unordered_set>\n#include <utility>\n#include <valarray>\n#include <vector>\n\n#define int long long\n#define double long double\n#define rep(i, e) for (int i = 0; i < (e); i++)\n#define ALL(var) begin(var), end(var)\n#ifdef DEBUG\n#define D(var) cerr << __LINE__ << \":\" << #var << \" \" << var << endl;\n#define DD(var) for_each(begin(var), end(var), [](const auto &i) { D(i); })\n#else\n#define D(var) ;\n#define DD(var) ;\n#endif\n\nusing namespace std;\n\nconstexpr int INF = 1LL << 60;\n\ntemplate <class T, size_t S>\nostream &operator<<(ostream &_ostr, const array<T, S> &_v);\ntemplate <class T>\nostream &operator<<(ostream &_ostr, const vector<T> &_v);\ntemplate <class T>\nostream &operator<<(ostream &_ostr, const valarray<T> &_v);\ntemplate <class T>\nostream &operator<<(ostream &_ostr, const deque<T> &_v);\ntemplate <class T>\nostream &operator<<(ostream &_ostr, const list<T> &_v);\ntemplate <class T, class Y>\nostream &operator<<(ostream &_ostr, const pair<T, Y> &_v);\ntemplate <class... Ts>\nostream &operator<<(ostream &_ostr, const tuple<Ts...> &t);\ntemplate <class T, class Y>\nostream &operator<<(ostream &_ostr, const map<T, Y> &_v);\ntemplate <class T, class Y>\nostream &operator<<(ostream &_ostr, const multimap<T, Y> &_v);\ntemplate <class T, class Y>\nostream &operator<<(ostream &_ostr, const unordered_map<T, Y> &_v);\ntemplate <class T, class Y>\nostream &operator<<(ostream &_ostr, const unordered_multimap<T, Y> &_v);\ntemplate <class T>\nostream &operator<<(ostream &_ostr, const set<T> &_v);\ntemplate <class T>\nostream &operator<<(ostream &_ostr, const multiset<T> &_v);\ntemplate <class T>\nostream &operator<<(ostream &_ostr, const unordered_set<T> &_v);\ntemplate <class T>\nostream &operator<<(ostream &_ostr, const unordered_multiset<T> &_v);\n\ntemplate <class T>\nostream &_orange(ostream &_ostr, const T &_v) {\n if (_v.size() == 0)\n return _ostr;\n _ostr << *begin(_v);\n for (auto itr = next(begin(_v)), enditr = end(_v); itr != enditr; itr++)\n _ostr << \" \" << *itr;\n return _ostr;\n}\ntemplate <class T, size_t S>\nostream &operator<<(ostream &_ostr, const array<T, S> &_v) { return _orange(_ostr, _v); }\ntemplate <class T>\nostream &operator<<(ostream &_ostr, const vector<T> &_v) { return _orange(_ostr, _v); }\ntemplate <class T>\nostream &operator<<(ostream &_ostr, const valarray<T> &_v) { return _orange(_ostr, _v); }\ntemplate <class T>\nostream &operator<<(ostream &_ostr, const deque<T> &_v) { return _orange(_ostr, _v); }\ntemplate <class T>\nostream &operator<<(ostream &_ostr, const list<T> &_v) { return _orange(_ostr, _v); }\ntemplate <class T, class Y>\nostream &operator<<(ostream &_ostr, const pair<T, Y> &_v) { return _orange(_ostr, _v); }\ntemplate <class... Ts>\nostream &operator<<(ostream &_ostr, const tuple<Ts...> &_v) {\n bool first = true;\n apply([&_ostr, &first](auto &&...args) {\n auto print = [&](auto &&val) {\n if (!first)\n _ostr << \" \";\n (_ostr << val);\n first = false;\n };\n (print(args), ...);\n },\n _v);\n return _ostr;\n}\ntemplate <class T, class Y>\nostream &operator<<(ostream &_ostr, const map<T, Y> &_v) { return _orange(_ostr, _v); }\ntemplate <class T, class Y>\nostream &operator<<(ostream &_ostr, const multimap<T, Y> &_v) { return _orange(_ostr, _v); }\ntemplate <class T, class Y>\nostream &operator<<(ostream &_ostr, const unordered_map<T, Y> &_v) { return _orange(_ostr, _v); }\ntemplate <class T>\nostream &operator<<(ostream &_ostr, const set<T> &_v) { return _orange(_ostr, _v); }\ntemplate <class T>\nostream &operator<<(ostream &_ostr, const multiset<T> &_v) { return _orange(_ostr, _v); }\ntemplate <class T>\nostream &operator<<(ostream &_ostr, const unordered_set<T> &_v) { return _orange(_ostr, _v); }\ntemplate <class T>\nostream &operator<<(ostream &_ostr, const unordered_multiset<T> &_v) { return _orange(_ostr, _v); }\n\ntemplate <class T, size_t S>\nistream &operator>>(istream &_istr, array<T, S> &_v);\ntemplate <class T>\nistream &operator>>(istream &_istr, vector<T> &_v);\ntemplate <class T>\nistream &operator>>(istream &_istr, valarray<T> &_v);\ntemplate <class T>\nistream &operator>>(istream &_istr, deque<T> &_v);\ntemplate <class T, class Y>\nistream &operator>>(istream &_istr, pair<T, Y> &_v);\n\ntemplate <class T>\nistream &_irange(istream &_istr, T &_v) {\n for (auto &i : _v)\n _istr >> i;\n return _istr;\n}\ntemplate <class T, size_t S>\nistream &operator>>(istream &_istr, array<T, S> &_v) { return _irange(_istr, _v); }\ntemplate <class T>\nistream &operator>>(istream &_istr, vector<T> &_v) { return _irange(_istr, _v); }\ntemplate <class T>\nistream &operator>>(istream &_istr, valarray<T> &_v) { return _irange(_istr, _v); }\ntemplate <class T>\nistream &operator>>(istream &_istr, deque<T> &_v) { return _irange(_istr, _v); }\ntemplate <class T, class Y>\nistream &operator>>(istream &_istr, pair<T, Y> &_v) {\n for (auto &i : _v)\n _istr >> i.first >> i.second;\n return _istr;\n}\n\nvector<int> divisor(int n) {\n vector<int> head, tail;\n for (int i = 1; i * i <= n; i++) {\n if (n % i == 0) {\n head.push_back(i);\n if (i * i != n)\n tail.push_back(n / i);\n }\n }\n head.insert(head.end(), tail.rbegin(), tail.rend());\n return head;\n}\n\nvector<pair<int, int>> primeFactorize(int n) {\n vector<pair<int, int>> res;\n for (int a = 2; a * a <= n; ++a) {\n if (n % a != 0)\n continue;\n int ex = 0;\n while (n % a == 0)\n ++ex, n /= a;\n res.push_back({a, ex});\n }\n if (n != 1)\n res.push_back({n, 1});\n return res;\n}\n\nint dichotomy(int ng, int ok, function<bool(int)> discriminant) {\n while (ok - ng > 1) {\n int mid = (ng + ok) / 2;\n (discriminant(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n\ntemplate <class T>\nvector<vector<T>> turnR(const vector<vector<T>> &s) {\n vector<vector<T>> res(s[0].size(), vector<T>(s.size()));\n for (int y = 0; y < s.size(); y++)\n for (int x = 0; x < s[0].size(); x++)\n res[x][s.size() - y - 1] = s[y][x];\n return res;\n}\n\ntemplate <class T>\nvector<vector<T>> turnL(const vector<vector<T>> &s) {\n vector<vector<T>> res(s[0].size(), vector<T>(s.size()));\n for (int y = 0; y < s.size(); y++)\n for (int x = 0; x < s[0].size(); x++)\n res[s[0].size() - x - 1][y] = s[y][x];\n return res;\n}\n\nint mop(int a, int n, int mod = INF) {\n int res = 1;\n while (n > 0) {\n if (n & 1)\n res = res * a % mod;\n a = a * a % mod;\n n >>= 1;\n }\n return res;\n}\n\ntemplate <class T>\nbool chmax(T &a, const T &b) {\n if (a < b) {\n a = b;\n return true;\n }\n return false;\n}\n\ntemplate <class T>\nbool chmin(T &a, const T &b) {\n if (a > b) {\n a = b;\n return true;\n }\n return false;\n}\n\ntemplate <int Modulus>\nclass modint {\npublic:\n int n;\n constexpr modint(const int x = 0) noexcept : n(x % Modulus) {}\n constexpr int &value() noexcept { return n; }\n constexpr const int &value() const noexcept { return n; }\n constexpr modint operator+(const modint rhs) const noexcept {\n return modint(*this) += rhs;\n }\n constexpr modint operator-(const modint rhs) const noexcept {\n return modint(*this) -= rhs;\n }\n constexpr modint operator*(const modint rhs) const noexcept {\n return modint(*this) *= rhs;\n }\n constexpr modint operator/(const modint rhs) const noexcept {\n return modint(*this) /= rhs;\n }\n constexpr modint &operator+=(const modint rhs) noexcept {\n n += rhs.n;\n if (n >= Modulus)\n n -= Modulus;\n return *this;\n }\n constexpr modint &operator-=(const modint rhs) noexcept {\n if (n < rhs.n)\n n += Modulus;\n n -= rhs.n;\n return *this;\n }\n constexpr modint &operator*=(const modint rhs) noexcept {\n n = n * rhs.n % Modulus;\n return *this;\n }\n constexpr modint &operator/=(modint rhs) noexcept {\n int exp = Modulus - 2;\n while (exp) {\n if (exp % 2)\n *this *= rhs;\n rhs *= rhs;\n exp /= 2;\n }\n return *this;\n }\n template <int mod>\n friend istream &operator>>(istream &_istr, modint<mod> &rhs) {\n _istr >> rhs.n;\n rhs.n %= Modulus;\n return _istr;\n }\n template <int mod>\n friend ostream &operator<<(ostream &_ostr, const modint<mod> &rhs) {\n return _ostr << rhs.n;\n }\n};\n\ntemplate <class T>\nclass SegmentTree {\npublic:\n int sz;\n vector<T> seg;\n const function<T(T, T)> f;\n const T M1;\n SegmentTree(int n, const function<T(T, T)> _f, const T &_M1) : f(_f), M1(_M1) {\n sz = 1;\n while (sz < n)\n sz <<= 1;\n seg.assign(2 * sz, _M1);\n }\n void set(int k, const T &x) {\n seg[k + sz] = x;\n }\n void build() {\n for (int k = sz - 1; k > 0; k--)\n seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);\n }\n void update(int k, const T &x) {\n k += sz;\n seg[k] = x;\n while (k >>= 1)\n seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);\n }\n // O(log n)\n T query(int a, int b) {\n T L = M1, R = M1;\n for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {\n if (a & 1)\n L = f(L, seg[a++]);\n if (b & 1)\n R = f(seg[--b], R);\n }\n return f(L, R);\n }\n T operator[](const int &k) const {\n return seg[k + sz];\n }\n template <class C>\n int find_subtree(int a, const C &check, T &M, bool type) {\n while (a < sz) {\n T nxt = type ? f(seg[2 * a + type], M) : f(M, seg[2 * a + type]);\n if (check(nxt))\n a = 2 * a + type;\n else\n M = nxt, a = 2 * a + 1 - type;\n }\n return a - sz;\n }\n template <class C>\n int find_first(int a, const C &check) {\n T L = M1;\n if (a <= 0) {\n if (check(f(L, seg[1])))\n return find_subtree(1, check, L, false);\n return -1;\n }\n int b = sz;\n for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {\n if (a & 1) {\n T nxt = f(L, seg[a]);\n if (check(nxt))\n return find_subtree(a, check, L, false);\n L = nxt;\n ++a;\n }\n }\n return -1;\n }\n template <class C>\n int find_last(int b, const C &check) {\n T R = M1;\n if (b >= sz) {\n if (check(f(seg[1], R)))\n return find_subtree(1, check, R, true);\n return -1;\n }\n int a = sz;\n for (b += sz; a < b; a >>= 1, b >>= 1) {\n if (b & 1) {\n T nxt = f(seg[--b], R);\n if (check(nxt))\n return find_subtree(b, check, R, true);\n R = nxt;\n }\n }\n return -1;\n }\n};\n\nclass UnionFind {\npublic:\n int n;\n vector<int> p;\n UnionFind(int _n) : n(_n), p(_n, -1) {}\n bool merge(int a, int b) {\n int x = root(a), y = root(b);\n if (x == y)\n return false;\n if (-p[x] < -p[y])\n swap(x, y);\n p[x] += p[y], p[y] = x;\n return true;\n }\n bool isSame(int a, int b) {\n return root(a) == root(b);\n }\n int root(int a) {\n if (p[a] < 0)\n return a;\n return p[a] = root(p[a]);\n }\n int size(int a) {\n return -p[root(a)];\n }\n vector<vector<int>> groups() {\n vector<int> buf(n), size(n);\n for (int i = 0; i < n; i++) {\n buf[i] = root(i);\n size[buf[i]]++;\n }\n vector<vector<int>> res(n);\n for (int i = 0; i < n; i++)\n res[i].reserve(size[i]);\n for (int i = 0; i < n; i++)\n res[buf[i]].push_back(i);\n res.erase(remove_if(res.begin(), res.end(), [&](const vector<int> &v) { return v.empty(); }), res.end());\n return res;\n }\n};\nclass RollingHash {\n static constexpr unsigned mod = 1000000007;\n\npublic:\n vector<unsigned> hashed, power;\n inline unsigned mul(unsigned a, unsigned b) const {\n unsigned long long x = (unsigned long long)a * b;\n unsigned xh = (unsigned)(x >> 32), xl = (unsigned)x, d, m;\n asm(\"divl %4; \\n\\t\"\n : \"=a\"(d), \"=d\"(m)\n : \"d\"(xh), \"a\"(xl), \"r\"(mod));\n return m;\n }\n RollingHash(const string &s, unsigned base = 10007) {\n int sz = (int)s.size();\n hashed.assign(sz + 1, 0);\n power.assign(sz + 1, 0);\n power[0] = 1;\n for (int i = 0; i < sz; i++) {\n power[i + 1] = mul(power[i], base);\n hashed[i + 1] = mul(hashed[i], base) + s[i];\n if (hashed[i + 1] >= mod)\n hashed[i + 1] -= mod;\n }\n }\n unsigned get(int l, int r) const {\n unsigned ret = hashed[r] + mod - mul(hashed[l], power[r - l]);\n if (ret >= mod)\n ret -= mod;\n return ret;\n }\n unsigned connect(unsigned h1, int h2, int h2len) const {\n unsigned ret = mul(h1, power[h2len]) + h2;\n if (ret >= mod)\n ret -= mod;\n return ret;\n }\n int LCP(const RollingHash &b, int l1, int r1, int l2, int r2) {\n int len = min(r1 - l1, r2 - l2);\n int low = -1, high = len + 1;\n while (high - low > 1) {\n int mid = (low + high) / 2;\n if (get(l1, l1 + mid) == b.get(l2, l2 + mid))\n low = mid;\n else\n high = mid;\n }\n return (low);\n }\n};\n\ntemplate <class Type>\nclass WeightedUnionFind {\npublic:\n WeightedUnionFind() = default;\n /// @brief 重み付き Union-Find 木を構築します。\n /// @param n 要素数\n explicit WeightedUnionFind(size_t n)\n : m_parentsOrSize(n, -1), m_diffWeights(n) {}\n /// @brief 頂点 i の root のインデックスを返します。\n /// @param i 調べる頂点のインデックス\n /// @return 頂点 i の root のインデックス\n int find(int i) {\n if (m_parentsOrSize[i] < 0)\n return i;\n const int root = find(m_parentsOrSize[i]);\n m_diffWeights[i] += m_diffWeights[m_parentsOrSize[i]];\n // 経路圧縮\n return (m_parentsOrSize[i] = root);\n }\n /// @brief a のグループと b のグループを統合します。\n /// @param a 一方のインデックス\n /// @param b 他方のインデックス\n /// @param w (b の重み) - (a の重み)\n void merge(int a, int b, Type w) {\n w += weight(a);\n w -= weight(b);\n a = find(a);\n b = find(b);\n if (a != b) {\n // union by size (小さいほうが子になる)\n if (-m_parentsOrSize[a] < -m_parentsOrSize[b]) {\n swap(a, b);\n w = -w;\n }\n m_parentsOrSize[a] += m_parentsOrSize[b];\n m_parentsOrSize[b] = a;\n m_diffWeights[b] = w;\n }\n }\n /// @brief (b の重み) - (a の重み) を返します。\n /// @param a 一方のインデックス\n /// @param b 他方のインデックス\n /// @remark a と b が同じグループに属さない場合の結果は不定です。\n /// @return (b の重み) - (a の重み)\n Type diff(int a, int b) {\n return (weight(b) - weight(a));\n }\n /// @brief a と b が同じグループに属すかを返します。\n /// @param a 一方のインデックス\n /// @param b 他方のインデックス\n /// @return a と b が同じグループに属す場合 true, それ以外の場合は false\n bool connected(int a, int b) {\n return (find(a) == find(b));\n }\n /// @brief i が属するグループの要素数を返します。\n /// @param i インデックス\n /// @return i が属するグループの要素数\n int size(int i) {\n return -m_parentsOrSize[find(i)];\n }\n\nprivate:\n // m_parentsOrSize[i] は i の 親,\n // ただし root の場合は (-1 * そのグループに属する要素数)\n vector<int> m_parentsOrSize;\n // 重み\n vector<Type> m_diffWeights;\n Type weight(int i) {\n find(i); // 経路圧縮\n return m_diffWeights[i];\n }\n};\n\nclass DirectedGraph {\npublic:\n struct Edge {\n int to, cost;\n };\n const int N;\n vector<vector<Edge>> g;\n DirectedGraph(int _n) : g(_n), N(_n) {}\n void add(int s, int t, int cost) {\n g[s].push_back({t, cost});\n }\n struct warshallfloyd_return {\n vector<vector<int>> dist, next;\n };\n // O(V^3)\n warshallfloyd_return warshallfloyd() {\n vector<vector<int>> dist(N, vector<int>(N, INF)), next(N, vector<int>(N, INF));\n for (int i = 0; i < N; i++)\n dist[i][i] = 0;\n for (int i = 0; i < N; i++)\n for (int j = 0; j < N; j++)\n next[i][j] = j;\n for (int i = 0; i < N; i++)\n for (auto &j : g[i])\n chmin(dist[i][j.to], j.cost);\n for (int k = 0; k < N; k++)\n for (int i = 0; i < N; i++)\n for (int j = 0; j < N; j++)\n if (chmin(dist[i][j], dist[i][k] + dist[k][j]))\n next[i][j] = next[i][k];\n return {dist, next};\n }\n // O(E+VlogV)\n vector<int> dijkstra(int s) {\n vector<int> d(N, INF);\n d[s] = 0;\n priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> que;\n que.push(make_pair(0, s));\n while (!que.empty()) {\n pair<int, int> p = que.top();\n que.pop();\n int v = p.second;\n if (d[v] < p.first)\n continue;\n for (auto e : g[v]) {\n if (d[e.to] > d[v] + e.cost) {\n d[e.to] = d[v] + e.cost;\n que.push(make_pair(d[e.to], e.to));\n }\n }\n }\n return d;\n }\n struct bellmanford_return {\n vector<int> path, distances;\n bool hascycle;\n };\n // O(V*E)\n // https://zenn.dev/reputeless/books/standard-cpp-for-competitive-programming/viewer/bellman-ford\n bellmanford_return bellmanford_SPFA(int st, int ed) {\n vector<int> counts(g.size()), distances(g.size(), INF), path;\n vector<bool> inqueue(g.size());\n queue<int> q;\n vector<int> p(g.size(), -1);\n distances[st] = 0;\n q.push(st);\n inqueue[st] = true;\n while (!q.empty()) {\n const int from = q.front();\n q.pop();\n inqueue[from] = false;\n for (const auto &edge : g[from]) {\n const long long d = (distances[from] + edge.cost);\n if (d < distances[edge.to]) {\n distances[edge.to] = d;\n p[edge.to] = from;\n if (!inqueue[edge.to]) {\n q.push(edge.to);\n inqueue[edge.to] = true;\n ++counts[edge.to];\n if (g.size() < counts[edge.to])\n return {vector<int>(), vector<int>(), true};\n }\n }\n }\n }\n if (distances[ed] != INF) {\n for (int i = ed; i != -1; i = p[i])\n path.push_back(i);\n reverse(path.begin(), path.end());\n }\n return {path, distances, false};\n }\n // O(V+E)\n vector<int> topologicalSort() {\n vector<int> d, ind(N);\n for (int i = 0; i < N; i++)\n for (auto e : g[i])\n ind[e.to]++;\n queue<int> que;\n for (int i = 0; i < N; i++)\n if (ind[i] == 0)\n que.push(i);\n while (!que.empty()) {\n int now = que.front();\n d.push_back(now);\n que.pop();\n for (auto e : g[now]) {\n ind[e.to]--;\n if (ind[e.to] == 0)\n que.push(e.to);\n }\n }\n return d;\n }\n};\n\nclass UndirectedGraph {\npublic:\n int n;\n vector<tuple<int, int, int>> g;\n UndirectedGraph(int _n) {\n n = _n;\n }\n void add(int u, int v, int c) {\n g.push_back({u, v, c});\n }\n // O(ElogV)\n vector<vector<int>> kruskal() {\n UnionFind uf(n);\n vector<vector<int>> res(n);\n sort(g.begin(), g.end(), [](auto &l, auto &r) { return get<2>(l) < get<2>(r); });\n D(g);\n for (auto &[a, b, cost] : g) {\n if (uf.merge(a, b)) {\n res[a].push_back(b);\n res[b].push_back(a);\n }\n }\n return res;\n }\n};\n\nclass Range {\n int m_value;\n const int m_end;\n const int m_stride;\n\npublic:\n Range(int begin, int end, int stride) : m_value(begin), m_end(end), m_stride(stride) {}\n const int &value() const { return m_value; }\n Range begin() const { return *this; }\n int end() const { return m_end; }\n bool operator!=(const int &value) const {\n return m_stride > 0 ? m_value < value : m_value > value;\n }\n void operator++() { m_value += m_stride; }\n const int &operator*() const { return m_value; }\n};\nRange range(int end) {\n return {0LL, end, 1LL};\n}\nRange range(int begin, int end) {\n return {begin, end, 1LL};\n}\nRange range(int begin, int end, int stride) {\n return {begin, end, stride};\n}\n\nvoid solve();\n\nsigned main() {\n cin.tie(0)->sync_with_stdio(0);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(16);\n solve();\n}\n\nstruct Ptr{\n int r,c;\n int dir;\n int mem;\n};\n\nvoid solve() {\n int r,c;cin>>r>>c;\n vector<vector<char>> s(r,vector<char>(c));cin>>s;\n\n queue<Ptr> que;\n set<tuple<int,int,int,int>> cache;\n\n {\n Ptr init;\n init.r=0;\n init.c=0;\n init.mem=0;\n init.dir = 0;\n que.push(init);\n }\n\n while(not que.empty()){ // exec and move\n auto q = que.front();\n que.pop();\n\n if(cache.find({q.r,q.c,q.mem,q.dir}) != cache.end()){\n continue;\n }\n cache.insert({q.r,q.c,q.mem,q.dir});\n\n if(s[q.r][q.c] == '<'){\n q.dir=2;\n }\n if(s[q.r][q.c] == '>'){\n q.dir=0;\n }\n if(s[q.r][q.c] == '^'){\n q.dir=3;\n }\n if(s[q.r][q.c] == 'v'){\n q.dir=1;\n }\n if(s[q.r][q.c] == '_'){\n if(q.mem == 0){\n q.dir=0;\n }else{\n q.dir=2;\n }\n }\n if(s[q.r][q.c] == '|'){\n if(q.mem == 0){\n q.dir=1;\n }else{\n q.dir=3;\n }\n }\n if(s[q.r][q.c] == '.'){\n\n }\n if(s[q.r][q.c] == '@'){\n cout<<\"YES\\n\";\n return;\n }\n if('0' <= s[q.r][q.c] and s[q.r][q.c] <= '9'){\n q.mem = s[q.r][q.c]-'0';\n }\n if(s[q.r][q.c] == '+'){\n if(++q.mem == 16)\n q.mem=0;\n }\n if(s[q.r][q.c] == '-'){\n if(--q.mem == -1)\n q.mem=15;\n }\n\n if(s[q.r][q.c] == '?'){\n int mem;\n q.dir=0;\n mem=q.c;\n if(++q.c == c)\n q.c=0;\n que.push(q);\n q.c=mem;\n\n q.dir=1;\n mem=q.r;\n if(++q.r == r)\n q.r=0;\n que.push(q);\n q.r=mem;\n\n q.dir=2;\n mem=q.c;\n if(--q.c == -1)\n q.c=c-1;\n que.push(q);\n q.c=mem;\n\n q.dir=3;\n mem=q.r;\n if(--q.r == -1)\n q.r=r-1;\n que.push(q);\n\n continue;\n }\n\n if(q.dir==0){\n if(++q.c == c)\n q.c=0;\n }\n if(q.dir==1){\n if(++q.r == r)\n q.r=0;\n }\n if(q.dir==2){\n if(--q.c == -1)\n q.c=c-1;\n }\n if(q.dir==3){\n if(--q.r == -1)\n q.r=r-1;\n }\n que.push(q);\n }\n cout<<\"NO\"<<endl;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 5428, "score_of_the_acc": -0.1715, "final_rank": 8 }, { "submission_id": "aoj_2262_6862978", "code_snippet": "#define _USE_MATH_DEFINES\n#include<iostream>\n#include<vector>\n#include<algorithm>\n#include<cmath>\n#include<string>\n#include<iomanip>\n#include<numeric>\n#include<queue>\n#include<deque>\n#include<stack>\n#include<set>\n#include<map>\n#include<random>\n#include<bitset>\n#include<cassert>\nusing namespace std;\ntypedef long long ll;\nconst int mod=998244353;\nconst int dx[]={1,0,0,-1},dy[]={0,1,-1,0};\nint r,c;\nstring s[20];\nstruct state{\n\tint x=0,y=0,mem=0,dir=1;\n};\nbool end(state st){\n\treturn s[st.x][st.y]=='@';\n}\nvector<state>next(state st){\n\tvector<state>res;\n\tif(s[st.x][st.y]=='<'){\n\t\tst.dir=2;\n\t\tres.push_back(st);\n\t}else if(s[st.x][st.y]=='>'){\n\t\tst.dir=1;\n\t\tres.push_back(st);\n\t}else if(s[st.x][st.y]=='^'){\n\t\tst.dir=3;\n\t\tres.push_back(st);\n\t}else if(s[st.x][st.y]=='v'){\n\t\tst.dir=0;\n\t\tres.push_back(st);\n\t}else if(s[st.x][st.y]=='_'){\n\t\tst.dir=(st.mem==0?1:2);\n\t\tres.push_back(st);\n\t}else if(s[st.x][st.y]=='|'){\n\t\tst.dir=(st.mem==0?0:3);\n\t\tres.push_back(st);\n\t}else if(s[st.x][st.y]=='?'){\n\t\tfor(int i=0;i<4;i++){\n\t\t\tst.dir=i;\n\t\t\tres.push_back(st);\n\t\t}\n\t}else if(s[st.x][st.y]=='.')\n\t\tres.push_back(st);\n\telse if('0'<=s[st.x][st.y]&&s[st.x][st.y]<='9'){\n\t\tst.mem=s[st.x][st.y]-'0';\n\t\tres.push_back(st);\n\t}else if(s[st.x][st.y]=='+'){\n\t\tst.mem=(st.mem==15?0:st.mem+1);\n\t\tres.push_back(st);\n\t}else if(s[st.x][st.y]=='-'){\n\t\tst.mem=(st.mem==0?15:st.mem-1);\n\t\tres.push_back(st);\n\t}\n\tfor(auto&e:res){\n\t\te.x+=dx[e.dir];\n\t\tif(e.x==r)\n\t\t\te.x=0;\n\t\telse if(e.x==-1)\n\t\t\te.x=r-1;\n\t\te.y+=dy[e.dir];\n\t\tif(e.y==c)\n\t\t\te.y=0;\n\t\telse if(e.y==-1)\n\t\t\te.y=c-1;\n\t}\n\treturn res;\n}\nbool operator<(state s1,state s2){\n\treturn tie(s1.x,s1.y,s1.mem,s1.dir)<tie(s2.x,s2.y,s2.mem,s2.dir);\n}\nint main(){\n\tcin>>r>>c;\n\tfor(int i=0;i<r;i++)\n\t\tcin>>s[i];\n\tstate fst;\n\tqueue<state>que;\n\tset<state>used;\n\tque.push(fst);\n\tused.insert(fst);\n\twhile(que.size()){\n \t\tstate st=que.front();\n\t\tque.pop();\n\t\tif(end(st)){\n\t\t\tcout<<\"YES\"<<endl;\n\t\t\treturn 0;\n\t\t}\n\t\tauto vec=next(st);\n\t\tfor(auto&e:vec){\n\t\t\tif(!used.count(e)){\n\t\t\t\tque.push(e);\n\t\t\t\tused.insert(e);\n\t\t\t}\n\t\t}\n\t}\n\tcout<<\"NO\"<<endl;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 4888, "score_of_the_acc": -0.1544, "final_rank": 7 }, { "submission_id": "aoj_2262_5324600", "code_snippet": "#include<iostream>\n#include<string>\n#include<algorithm> \n#include<cmath>\n#include<ctime>\n#include<map>\n#include<vector>\n#include<math.h>\n#include<stdio.h>\n#include<stack>\n#include<queue>\n#include<tuple>\n#include<cassert>\n#include<set>\n#include<bitset>\n#include<functional>\n#include <fstream>\n//#include<bits/stdc++.h>\n#pragma GCC target(\"avx2\")\n#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"unroll-loops\")\n#define rep(i, x) for(ll i = 0; i < x; i++)\n#define rep2(i, x) for(ll i = 1; i <= x; i++)\n#define all(a) (a).begin(),(a).end()\n#define puts(x) cout << (x) << \"\\n\"\nusing ll = long long;\nusing ld = long double;\nusing namespace std;\nconst ll INF = 10000000000000000;\nconst int intINF = 1000000000;\nconst ll mod = 1000000007;\nconst ll MOD = 998244353;\nconst ld pi = 3.141592653589793238;\n\nbool isprime(int p) {\n\tif (p == 1) return false;\n\tfor (int i = 2; i < p; i++) {\n\t\tif (p % i == 0) return false;\n\t}\n\treturn true;\n}\nll gcd(ll a, ll b) {\n\tif (a < b)swap(a, b);\n\tif (a % b == 0)return b;\n\treturn gcd(b, a % b);\n}\nll lcm(ll a, ll b) {\n\treturn a / gcd(a, b) * b;\n}\nll keta(ll n) {\n\tll res = 0;\n\twhile (n >= 1) {\n\t\tres += n % 10; n /= 10;\n\t}\n\treturn res;\n}\nll modpow(ll x, ll y) {\n\tll res = 1;\n\twhile (y) {\n\t\tif (y % 2) { res *= x; res %= mod; }\n\t\tx = x * x % mod; y /= 2;\n\t}\n\treturn res;\n}\nll nCk(ll n, ll k) {\n\tll a = 1, b = 1;\n\tfor (int h = n - k + 1; h <= n; h++) { a *= h; a %= mod; }\n\tfor (int h = 1; h <= k; h++) { b *= h; b %= mod; }\n\treturn a * modpow(b, mod - 2) % mod;\n}\n//printf(\"%.10f\\n\", n);\ntypedef pair <ll, ll> P;\nll dx[4] = { 1, 0, -1, 0 }, dy[4] = { 0, 1, 0, -1 };\nstruct edge { ll to, cost; };\nstruct status {\n\tll x;\n\tll y;\n\tchar mk;\n\tll cost;\n\n\tbool operator<(const status& rhs) const { return cost < rhs.cost; };\n\tbool operator>(const status& rhs) const { return cost > rhs.cost; };\n};\n\n\nchar maze[22][22];\nsigned main() {\n\tios::sync_with_stdio(false);\n\tstd::cin.tie(nullptr);\n\t//cout << fixed << setprecision(15);\n\n\t//input\n\tll n, m; cin >> n >> m;\n\trep(i, n) {\n\t\trep(j, m) {\n\t\t\tcin >> maze[i][j];\n\t\t}\n\t}\n\tqueue<status> que; que.push({ 0,0,'R',0 });\n\tll cnt = 0;\n\twhile (que.size()) {\n\t\tcnt++;\n\t\tif (cnt == 300000) { break; }\n\t\tstatus q = que.front(); que.pop();\n //cout<<q.x<<' '<<q.y<<' '<<q.mk<<' '<<q.cost<<endl;\n\t\tif (maze[q.x][q.y] == '<') { que.push({ q.x, (q.y + m - 1) % m,'L', q.cost }); }\n\t\tif (maze[q.x][q.y] == '>') { que.push({ q.x, (q.y + 1) % m,'R', q.cost }); }\n\t\tif (maze[q.x][q.y] == '^') { que.push({ (q.x + n - 1) % n, q.y,'U', q.cost }); }\n\t\tif (maze[q.x][q.y] == 'v') { que.push({ (q.x + 1) % n, q.y,'D', q.cost }); }\n\t\tif (maze[q.x][q.y] == '_' && q.cost == 0) { que.push({ q.x, (q.y + 1) % m,'R', q.cost }); }\n\t\tif (maze[q.x][q.y] == '_' && q.cost != 0) { que.push({ q.x, (q.y + m - 1) % m,'L', q.cost }); }\n\t\tif (maze[q.x][q.y] == '|' && q.cost == 0) { que.push({ (q.x + 1) % n, q.y,'D', q.cost }); }\n\t\tif (maze[q.x][q.y] == '|' && q.cost != 0) { que.push({ (q.x + n - 1) % n, q.y,'U', q.cost }); }\n\t\tif (maze[q.x][q.y] == '?') {\n\t\t\tque.push({ q.x, (q.y + m - 1) % m,'L', q.cost }); que.push({ q.x, (q.y + 1) % m,'R', q.cost });\n\t\t\tque.push({ (q.x + n - 1) % n, q.y,'U', q.cost }); que.push({ (q.x + 1) % n, q.y,'D', q.cost });\n\t\t}\n\t\tif (maze[q.x][q.y] == '.') {\n\t\t\tif (q.mk == 'L') { que.push({ q.x, (q.y + m - 1) % m,'L', q.cost }); }\n\t\t\tif (q.mk == 'R') { que.push({ q.x, (q.y + 1) % m,'R', q.cost }); }\n\t\t\tif (q.mk == 'U') { que.push({ (q.x + n - 1) % n, q.y,'U', q.cost }); }\n\t\t\tif (q.mk == 'D') { que.push({ (q.x + 1) % n, q.y,'D', q.cost }); }\n\t\t}\n\t\tif (maze[q.x][q.y] == '@') { cout << \"YES\\n\"; return 0; }\n\t\tif (maze[q.x][q.y] == '0') {\n\t\t\tif (q.mk == 'L') { que.push({ q.x, (q.y + m - 1) % m,'L', 0 }); }\n\t\t\tif (q.mk == 'R') { que.push({ q.x, (q.y + 1) % m,'R', 0 }); }\n\t\t\tif (q.mk == 'U') { que.push({ (q.x + n - 1) % n, q.y,'U', 0 }); }\n\t\t\tif (q.mk == 'D') { que.push({ (q.x + 1) % n, q.y,'D', 0 }); }\n\t\t}\n\t\tif (maze[q.x][q.y] == '1') {\n\t\t\tif (q.mk == 'L') { que.push({ q.x, (q.y + m - 1) % m,'L', 1 }); }\n\t\t\tif (q.mk == 'R') { que.push({ q.x, (q.y + 1) % m,'R', 1 }); }\n\t\t\tif (q.mk == 'U') { que.push({ (q.x + n - 1) % n, q.y,'U', 1 }); }\n\t\t\tif (q.mk == 'D') { que.push({ (q.x + 1) % n, q.y,'D', 1 }); }\n\t\t}\n\t\tif (maze[q.x][q.y] == '2') {\n\t\t\tif (q.mk == 'L') { que.push({ q.x, (q.y + m - 1) % m,'L', 2 }); }\n\t\t\tif (q.mk == 'R') { que.push({ q.x, (q.y + 1) % m,'R', 2 }); }\n\t\t\tif (q.mk == 'U') { que.push({ (q.x + n - 1) % n, q.y,'U', 2 }); }\n\t\t\tif (q.mk == 'D') { que.push({ (q.x + 1) % n, q.y,'D', 2 }); }\n\t\t}\n\t\tif (maze[q.x][q.y] == '3') {\n\t\t\tif (q.mk == 'L') { que.push({ q.x, (q.y + m - 1) % m,'L', 3 }); }\n\t\t\tif (q.mk == 'R') { que.push({ q.x, (q.y + 1) % m,'R', 3 }); }\n\t\t\tif (q.mk == 'U') { que.push({ (q.x + n - 1) % n, q.y,'U', 3 }); }\n\t\t\tif (q.mk == 'D') { que.push({ (q.x + 1) % n, q.y,'D', 3 }); }\n\t\t}\n\t\tif (maze[q.x][q.y] == '4') {\n\t\t\tif (q.mk == 'L') { que.push({ q.x, (q.y + m - 1) % m,'L', 4 }); }\n\t\t\tif (q.mk == 'R') { que.push({ q.x, (q.y + 1) % m,'R', 4 }); }\n\t\t\tif (q.mk == 'U') { que.push({ (q.x + n - 1) % n, q.y,'U', 4 }); }\n\t\t\tif (q.mk == 'D') { que.push({ (q.x + 1) % n, q.y,'D', 4 }); }\n\t\t}\n\t\tif (maze[q.x][q.y] == '5') {\n\t\t\tif (q.mk == 'L') { que.push({ q.x, (q.y + m - 1) % m,'L', 5 }); }\n\t\t\tif (q.mk == 'R') { que.push({ q.x, (q.y + 1) % m,'R', 5 }); }\n\t\t\tif (q.mk == 'U') { que.push({ (q.x + n - 1) % n, q.y,'U', 5 }); }\n\t\t\tif (q.mk == 'D') { que.push({ (q.x + 1) % n, q.y,'D', 5 }); }\n\t\t}\n\t\tif (maze[q.x][q.y] == '6') {\n\t\t\tif (q.mk == 'L') { que.push({ q.x, (q.y + m - 1) % m,'L', 6 }); }\n\t\t\tif (q.mk == 'R') { que.push({ q.x, (q.y + 1) % m,'R', 6 }); }\n\t\t\tif (q.mk == 'U') { que.push({ (q.x + n - 1) % n, q.y,'U', 6 }); }\n\t\t\tif (q.mk == 'D') { que.push({ (q.x + 1) % n, q.y,'D', 6 }); }\n\t\t}\n\t\tif (maze[q.x][q.y] == '7') {\n\t\t\tif (q.mk == 'L') { que.push({ q.x, (q.y + m - 1) % m,'L', 7 }); }\n\t\t\tif (q.mk == 'R') { que.push({ q.x, (q.y + 1) % m,'R', 7 }); }\n\t\t\tif (q.mk == 'U') { que.push({ (q.x + n - 1) % n, q.y,'U', 7 }); }\n\t\t\tif (q.mk == 'D') { que.push({ (q.x + 1) % n, q.y,'D', 7 }); }\n\t\t}\n\t\tif (maze[q.x][q.y] == '8') {\n\t\t\tif (q.mk == 'L') { que.push({ q.x, (q.y + m - 1) % m,'L', 8 }); }\n\t\t\tif (q.mk == 'R') { que.push({ q.x, (q.y + 1) % m,'R', 8 }); }\n\t\t\tif (q.mk == 'U') { que.push({ (q.x + n - 1) % n, q.y,'U', 8 }); }\n\t\t\tif (q.mk == 'D') { que.push({ (q.x + 1) % n, q.y,'D', 8 }); }\n\t\t}\n\t\tif (maze[q.x][q.y] == '9') {\n\t\t\tif (q.mk == 'L') { que.push({ q.x, (q.y + m - 1) % m,'L', 9 }); }\n\t\t\tif (q.mk == 'R') { que.push({ q.x, (q.y + 1) % m,'R', 9 }); }\n\t\t\tif (q.mk == 'U') { que.push({ (q.x + n - 1) % n, q.y,'U', 9 }); }\n\t\t\tif (q.mk == 'D') { que.push({ (q.x + 1) % n, q.y,'D', 9 }); }\n\t\t}\n\t\tif (maze[q.x][q.y] == '+') {\n\t\t\tif (q.mk == 'L') { que.push({ q.x, (q.y + m - 1) % m,'L', (q.cost + 1) % 16 }); }\n\t\t\tif (q.mk == 'R') { que.push({ q.x, (q.y + 1) % m,'R', (q.cost + 1) % 16 }); }\n\t\t\tif (q.mk == 'U') { que.push({ (q.x + n - 1) % n, q.y,'U', (q.cost + 1) % 16 }); }\n\t\t\tif (q.mk == 'D') { que.push({ (q.x + 1) % n, q.y,'D', (q.cost + 1) % 16 }); }\n\t\t}\n\t\tif (maze[q.x][q.y] == '-') {\n\t\t\tif (q.mk == 'L') { que.push({ q.x, (q.y + m - 1) % m,'L', (q.cost + 15) % 16 }); }\n\t\t\tif (q.mk == 'R') { que.push({ q.x, (q.y + 1) % m,'R', (q.cost + 15) % 16 }); }\n\t\t\tif (q.mk == 'U') { que.push({ (q.x + n - 1) % n, q.y,'U', (q.cost + 15) % 16 }); }\n\t\t\tif (q.mk == 'D') { que.push({ (q.x + 1) % n, q.y,'D', (q.cost + 15) % 16 }); }\n\t\t}\n\t}\n\tcout << \"NO\\n\";\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 31656, "score_of_the_acc": -1, "final_rank": 12 }, { "submission_id": "aoj_2262_2525395", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n \n#define REP(i,a,b) for(int i=(a);i<(b);++i)\n#define rep(i,n) REP(i,0,n)\n#define INF (1<<30)\n#define INFLL (1LL<<62LL)\n \ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef pair<int, int> pii;\ntypedef pair<ll, ll> pll;\n \nint dx[8] = {0, 1, 0, -1, 1, -1, 1, -1};\nint dy[8] = {1, 0, -1, 0, 1, -1, -1, 1};\n \n#define DOWN 0\n#define RIGHT 1\n#define UP 2\n#define LEFT 3\n \nstruct State {\n int x, y;\n int mem;\n int dir;\n \n State() :\n x(0),\n y(0),\n mem(0),\n dir(RIGHT) {}\n \n State(int x, int y, int mem, int dir) :\n x(x),\n y(y),\n mem(mem),\n dir(dir) {}\n \n bool operator<(const State & rhs) const {\n if (x == rhs.x && y == rhs.y && mem == rhs.mem) return dir < rhs.dir;\n if (x == rhs.x && y == rhs.y) return mem < rhs.mem;\n if (x == rhs.x) return y < rhs.y;\n return x < rhs.x;\n }\n};\n \nint main() {\n ios_base::sync_with_stdio(false); cin.tie(0);\n int R, C;\n cin >> R >> C;\n vector<string> prog(R);\n rep(i, R) cin >> prog[i];\n \n set<State> visited;\n queue<State> Q;\n ll ok = false;\n \n Q.push(State());\n while (!Q.empty()) {\n State st = Q.front();\n Q.pop();\n \n st.x = (st.x + C) % C;\n st.y = (st.y + R) % R;\n \n if (visited.find(st) != visited.end())\n continue;\n \n visited.insert(st);\n \n char p;\n p = prog[st.y][st.x];\n \n if (p == '@') {\n ok = true;\n break;\n }\n \n switch (p) {\n case '0'...'9':\n st.mem = p - '0';\n break;\n case '_':\n if (st.mem == 0) st.dir = RIGHT;\n else st.dir = LEFT;\n break;\n case '|':\n if (st.mem == 0) st.dir = DOWN;\n else st.dir = UP;\n break;\n case '+':\n st.mem = (st.mem + 1) % 16;\n break;\n case '-':\n st.mem = st.mem - 1;\n if (st.mem < 0) st.mem += 16;\n break;\n case '^':\n st.dir = UP;\n break;\n case 'v':\n st.dir = DOWN;\n break;\n case '<':\n st.dir = LEFT;\n break;\n case '>':\n st.dir = RIGHT;\n break;\n }\n \n if (p != '?') {\n Q.push(State(st.x + dx[st.dir], st.y + dy[st.dir], st.mem, st.dir));\n } else {\n Q.push(State(st.x + dx[LEFT], st.y + dy[LEFT], st.mem, LEFT));\n Q.push(State(st.x + dx[RIGHT], st.y + dy[RIGHT], st.mem, RIGHT));\n Q.push(State(st.x + dx[UP], st.y + dy[UP], st.mem, UP));\n Q.push(State(st.x + dx[DOWN], st.y + dy[DOWN], st.mem, DOWN));\n }\n }\n \n if (ok) cout << \"YES\" << endl;\n else cout << \"NO\" << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 4772, "score_of_the_acc": -0.1507, "final_rank": 5 }, { "submission_id": "aoj_2262_2523411", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define DUMP(x) cerr << #x << \"=\" << x << endl\n#define DUMP2(x, y) cerr<<\"(\"<<#x<<\", \"<<#y<<\") = (\"<<x<<\", \"<<y<<\")\"<< endl\n#define BINARY(x) static_cast<bitset<16> >(x)\n\n#define rep(i,n) for(int i=0;i<(int)(n);i++)\n#define REP(i,m,n) for (int i=m;i<(int)(n);i++)\n\n#define in_range(x, y, w, h) (0<=(int)(x) && (int)(x)<(int)(w) && 0<=(int)(y) && (int)(y)<(int)(h))\n#define ALL(a) (a).begin(),(a).end()\n\ntypedef long long ll;\nconst int INF = 1e9;\nconst ll INFLL = 1e18;\ntypedef pair<int, int> PII;\nint dx[4]={0, -1, 1, 0}, dy[4]={-1, 0, 0, 1};\n\nint R, C;\n\nconst int UP = 0;\nconst int LEFT = 1;\nconst int RIGHT = 2;\nconst int DOWN = 3;\n\nstruct State {\n int x, y, memory, direction;\n bool operator<(const State &rhs) const \n {\n if (x == rhs.x) {\n if (y == rhs.y) {\n if (memory == rhs.memory) return direction < rhs.direction;\n else return memory < rhs.memory;\n } else {\n return y < rhs.y;\n }\n } else {\n return x < rhs.x;\n }\n }\n \n};\n\nPII move(int x, int y, int dir)\n{\n PII res(x + dx[dir], y + dy[dir]);\n res.first = (res.first + C) % C;\n res.second = (res.second + R) % R;\n return res;\n}\n\nint increment(int x)\n{\n return (x+1)%16;\n}\n\nint decrement(int x)\n{\n return (x - 1 + 16) % 16;\n}\n\nbool solve(vector<string> P)\n{\n map<State, bool> vis;\n queue<State> Q;\n Q.push({0, 0, 0, RIGHT});\n vis[State({0, 0, 0, RIGHT})] = true;\n\n while (Q.size()) {\n State s = Q.front(); Q.pop();\n vector<State> ns;\n\n#ifdef DEBUG\n cerr << s.x << \" \" << s.y << \" \" << s.memory << \" \" << \"<^>v\"[s.direction] << endl;\n#endif\n\n switch (P[s.y][s.x]) {\n case '<':\n {\n PII npos = move(s.x, s.y, LEFT);\n ns.push_back({npos.first, npos.second, s.memory, LEFT});\n }\n break;\n case '>':\n {\n PII npos = move(s.x, s.y, RIGHT);\n ns.push_back({npos.first, npos.second, s.memory, RIGHT});\n }\n break;\n case '^':\n {\n PII npos = move(s.x, s.y, UP);\n ns.push_back({npos.first, npos.second, s.memory, UP});\n }\n break;\n case 'v':\n {\n PII npos = move(s.x, s.y, DOWN);\n ns.push_back({npos.first, npos.second, s.memory, DOWN});\n }\n break;\n case '_':\n {\n int ndir = (!s.memory ? RIGHT : LEFT);\n PII npos = move(s.x, s.y, ndir);\n ns.push_back({npos.first, npos.second, s.memory, ndir});\n }\n break;\n case '|':\n {\n int ndir = (!s.memory ? DOWN : UP);\n PII npos = move(s.x, s.y, ndir);\n ns.push_back({npos.first, npos.second, s.memory, ndir});\n }\n break;\n case '?':\n rep(i, 4) {\n int ndir = i;\n PII npos = move(s.x, s.y, ndir);\n ns.push_back({npos.first, npos.second, s.memory, ndir});\n }\n break;\n case '.':\n {\n int ndir = s.direction;\n PII npos = move(s.x, s.y, ndir);\n ns.push_back({npos.first, npos.second, s.memory, ndir});\n }\n break;\n case '@':\n return true;\n case '+':\n {\n int nmemory = increment(s.memory);\n PII npos = move(s.x, s.y, s.direction);\n ns.push_back({npos.first, npos.second, nmemory, s.direction});\n }\n break;\n case '-':\n {\n int nmemory = decrement(s.memory);\n PII npos = move(s.x, s.y, s.direction);\n ns.push_back({npos.first, npos.second, nmemory, s.direction});\n }\n break;\n default:\n {\n int nmemory = (P[s.y][s.x] - '0');\n PII npos = move(s.x, s.y, s.direction);\n ns.push_back({npos.first, npos.second, nmemory, s.direction});\n }\n break;\n }\n\n for (State m : ns) {\n if (!vis[m]) {\n vis[m] = true;\n Q.push(m);\n }\n }\n }\n return false;\n}\n\nint main()\n{\n ios::sync_with_stdio(false);\n\n cin >> R >> C;\n vector<string> P(R);\n rep(i, R) cin >> P[i];\n cout << (solve(P) ? \"YES\" : \"NO\") << endl;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 4852, "score_of_the_acc": -0.1533, "final_rank": 6 }, { "submission_id": "aoj_2262_1412147", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define LOG(...) fprintf(stderr,__VA_ARGS__)\n//#define LOG(...)\n#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);++i)\n#define REP(i,n) for(int i=0;i<(int)(n);++i)\n#define ALL(a) (a).begin(),(a).end()\n#define RALL(a) (a).rbegin(),(a).rend()\n#define EXIST(s,e) ((s).find(e)!=(s).end())\n#define SORT(c) sort(ALL(c))\n#define RSORT(c) sort(RALL(c))\n\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef vector<bool> vb;\ntypedef vector<int> vi;\ntypedef vector<ll> vll;\ntypedef vector<vb> vvb;\ntypedef vector<vi> vvi;\ntypedef vector<vll> vvll;\ntypedef pair<int,int> pii;\ntypedef pair<ll,ll> pll;\n\n// right, down, left, up\nconst int dx[] = {1,0,-1,0}, dy[] = {0,1,0,-1};\n\nint H, W;\nchar Prog[20][20];\n\nbool G[20][20][4][16]; // y, x, cour, memory\n\nbool run(int x, int y, int cour, int mem) {\n do {\n G[y][x][cour][mem] = true;\n switch (Prog[y][x]) {\n case '<':\n cour = 2;\n break;\n case '>':\n cour = 0;\n break;\n case '^':\n cour = 3;\n break;\n case 'v':\n cour = 1;\n break;\n case '_':\n if (mem == 0) {\n cour = 0;\n } else {\n cour = 2;\n }\n break;\n case '|':\n if (mem == 0) {\n cour = 1;\n } else {\n cour = 3;\n }\n break;\n case '?':\n REP(i, 4) {\n int sx = x + dx[i], sy = y + dy[i];\n if (sx < 0) sx = W-1;\n if (sx >= W) sx = 0;\n if (sy < 0) sy = H-1;\n if (sy >= H) sy = 0;\n if (run(sx, sy, i, mem)) return true;\n }\n return false;\n case '.':\n break;\n case '@':\n return true;\n case '+':\n if (mem == 15) {\n mem = 0;\n } else {\n mem++;\n }\n break;\n case '-':\n if (mem == 0) {\n mem = 15;\n } else {\n mem--;\n }\n break;\n default: // number\n mem = Prog[y][x] - '0';\n }\n\n x += dx[cour];\n y += dy[cour];\n if (x < 0) x = W-1;\n if (x >= W) x = 0;\n if (y < 0) y = H-1;\n if (y >= H) y = 0;\n\n// LOG(\"%02d\\n\", mem);\n// REP(sy, H) {\n// REP(sx, W) {\n// if (sx == x && sy == y) {\n// LOG(\"#\");\n// } else {\n// LOG(\"%c\", Prog[sy][sx]);\n// }\n// }\n// LOG(\"\\n\");\n// }\n// getchar();\n } while (!G[y][x][cour][mem]);\n\n return false;\n}\n\nint main() {\n cin >> H >> W;\n REP(y, H) REP(x, W) {\n cin >> Prog[y][x];\n }\n cout << (run(0, 0, 0, 0) ? \"YES\" : \"NO\") << endl;\n}", "accuracy": 0.5833333333333334, "time_ms": 10, "memory_kb": 9272, "score_of_the_acc": -0.2929, "final_rank": 13 }, { "submission_id": "aoj_2262_1408135", "code_snippet": "#include<iostream>\n#include<vector>\n#include<algorithm>\n#include <iterator>\n#include<queue>\n#include <functional>\n#include <string>\n#include <numeric>\nusing namespace std;\n#define REP(i,n) for(int i=0;i<(n);i++)\n#define ALL(v) v.begin(),v.end()\nusing namespace std;\nconst int dx[4] = { -1, 1, 0, 0 };\nconst int dy[4] = { 0, 0, -1, 1 };\nstruct data\n{\n\tbool memo[20][20][16][4];\n\tdata(){\n\t\tREP(i, 20)\n\t\t\tREP(j, 20)\n\t\t\tREP(k, 16)\n\t\t\tREP(l, 4)\n\t\t\tmemo[i][j][k][l] = false;\n\t}\n};\nint R=0, C=0;\nbool a(int ax, int ay, vector<string> Fi,int Memory, int Muki,data aa){\n\tax=(ax+C)% C;ay=(ay+R) % R;\n\tif (aa.memo[ax][ay][Memory][Muki])return false;\n\taa.memo[ax][ay][Memory][Muki] = true;\n\tswitch (Fi[ay][ax]){\n\tcase '<':Muki = 0; return a(ax + dx[Muki], ay + dy[Muki], Fi, Memory, Muki,aa);\n\tcase '>':Muki = 1;; return a(ax + dx[Muki], ay + dy[Muki], Fi, Memory, Muki, aa);\n\tcase '^':Muki = 2; return a(ax + dx[Muki], ay + dy[Muki], Fi, Memory, Muki, aa);\n\tcase 'v':Muki = 3; return a(ax + dx[Muki], ay + dy[Muki], Fi, Memory, Muki, aa);\n\tcase '_':Muki = Memory ? 0 : 1; return a(ax + dx[Muki], ay + dy[Muki], Fi, Memory, Muki, aa);\n\tcase '|':Muki = Memory ? 2 : 3; return a(ax + dx[Muki], ay + dy[Muki], Fi, Memory, Muki, aa);\n\tcase '?': return a(ax + dx[0], ay + dy[0], Fi, Memory, 0, aa) || a(ax + dx[1], ay + dy[1], Fi, Memory, 1, aa) || a(ax + dx[2], ay + dy[2], Fi, Memory, 2, aa) || a(ax + dx[3], ay + dy[3], Fi, Memory, 3, aa);\n\tcase '.':return a(ax + dx[Muki], ay + dy[Muki], Fi,Memory, Muki, aa);\n\tcase '@':return true;\n\tcase '+':return a(ax + dx[Muki], ay + dy[Muki], Fi, (Memory + 1) % 16, Muki, aa);\n\tcase '-':return a(ax + dx[Muki], ay + dy[Muki], Fi, (Memory+15) % 16, Muki, aa);\n\tdefault:return a(ax + dx[Muki], ay + dy[Muki], Fi, Fi[ay][ax]-48, Muki, aa);\n\t}\n\treturn 0;\n}\n\nint main(){\n\tcin >> R >> C;\n\tvector<string> Field(R);\n\tREP(i, R){\n\t\tstring st;\n\t\tcin >> Field[i];\n\t}\n\tdata adata;\n\tif (a(0, 0, Field, 0, 1,adata)){\n\t\tcout << \"YES\" << endl;\n\t}\n\telse{\n\t\tcout << \"NO\" << endl;\n\t}\n\t\t\n\treturn 0;\n}", "accuracy": 0.5833333333333334, "time_ms": 10, "memory_kb": 9456, "score_of_the_acc": -0.2987, "final_rank": 14 }, { "submission_id": "aoj_2262_622697", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\nusing namespace std;\n\nconst int pos[]={1,0,-1,0};\nstruct state{\n\tchar x,y,p,mem;\n\tbool operator==(const state &A)const{\n\t\treturn A.x==x&&A.y==y&&A.p==p&&A.mem==mem;\n\t}\n};\n\nbool check(state A,const vector<string> &code,int r,int c,vector<state> &visited){\n char op=code[A.y][A.x];\n while(op!='@'){\n \tif(find(visited.begin(),visited.end(),A)!=visited.end())return false;\n \tvisited.push_back(A);\n \top=code[A.y][A.x];\n \t//cerr<<(int)A.x<<\",\"<<(int)A.y<<endl;\n \tif(op=='>')A.p=0;\n \tif(op=='v')A.p=1;\n \tif(op=='<')A.p=2;\n \tif(op=='^')A.p=3;\n \tif(op=='_')A.p=(A.mem)?2:0;\n \tif(op=='|')A.p=(A.mem)?3:1;\n \tif('0'<=op&&op<='9')A.mem=op-'0';\n \tif(op=='+')A.mem=(A.mem+1)%16;\n \tif(op=='-')A.mem=(A.mem+15)%16;\n \tif(op=='?'){\n \t\tbool f=false;\n \t\tfor(int i=0;i<4;++i){\n \t\t\tstate tmp=A;\n \t\t\ttmp.p=i;\n \t\t\ttmp.x=(tmp.x+pos[tmp.p]+c)%c;tmp.y=(tmp.y+pos[(tmp.p+3)%4]+r)%r;\n \t\t\tif(check(tmp,code,r,c,visited))return true;\n \t\t}\n \t\treturn false;\n \t}\n \t\n \tA.x=(A.x+pos[A.p]+c)%c;A.y=(A.y+pos[(A.p+3)%4]+r)%r;\n }\n return true;\n}\n\nint main(void){\n int r,c;\n cin>>r>>c;\n vector<string> code;\n for(int i=0;i<r;++i){string s;cin>>s;code.push_back(s);}\n state A={0,0,0,0};\n vector<state> visited;\n cout<<(check(A,code,r,c,visited)?\"YES\":\"NO\")<<endl;\n return 0;\n}", "accuracy": 1, "time_ms": 590, "memory_kb": 2880, "score_of_the_acc": -0.9566, "final_rank": 11 }, { "submission_id": "aoj_2262_482843", "code_snippet": "#include <iostream>\n#include <string>\n#include <vector>\n#include <algorithm>\nusing namespace std;\n\n#define rep(i, n) rep2(i, 0, n)\n#define rep2(i, m, n) for(int i = m; i < (int)(n); ++i)\n#define pb(e) push_back(e)\n\ntypedef long long ll;\n\nconst int dx[] = { 1, 0, -1, 0 };\nconst int dy[] = { 0, 1, 0, -1 };\n\nint main()\n{\n int r, c;\n string prog[20];\n cin >> r >> c;\n rep(i, r) cin >> prog[i];\n \n int x = 0, y = 0, dir = 0, mem = 0;\n rep(i, 200000000) {\n switch(prog[y][x]) {\n case '<': dir = 2; break;\n case '>': dir = 0; break;\n case '^': dir = 3; break;\n case 'v': dir = 1; break;\n case '_': dir = (mem == 0 ? 0 : 2); break;\n case '|': dir = (mem == 0 ? 1 : 3); break;\n case '?': dir = rand() & 3; break;\n case '.': break;\n case '@': cout << \"YES\" << endl; return 0;\n case '+': mem = (mem + 1) & 15; break;\n case '-': mem = (mem - 1) & 15; break;\n default: mem = prog[y][x] - '0';\n }\n x += dx[dir]; y += dy[dir];\n if(x == -1) x = c - 1;\n if(x == c) x = 0;\n if(y == -1) y = r - 1;\n if(y == r) y = 0;\n }\n cout << \"NO\" << endl;\n}", "accuracy": 0.4, "time_ms": 680, "memory_kb": 1208, "score_of_the_acc": -1.0382, "final_rank": 16 }, { "submission_id": "aoj_2262_482842", "code_snippet": "#include <iostream>\n#include <string>\n#include <vector>\n#include <algorithm>\nusing namespace std;\n\n#define rep(i, n) rep2(i, 0, n)\n#define rep2(i, m, n) for(int i = m; i < (int)(n); ++i)\n#define pb(e) push_back(e)\n\ntypedef long long ll;\n\nconst int dx[] = { 1, 0, -1, 0 };\nconst int dy[] = { 0, 1, 0, -1 };\n\nint main()\n{\n int r, c;\n string prog[20];\n cin >> r >> c;\n rep(i, r) cin >> prog[i];\n \n int x = 0, y = 0, dir = 0, mem = 0;\n rep(i, 10000000) {\n switch(prog[y][x]) {\n case '<': dir = 2; break;\n case '>': dir = 0; break;\n case '^': dir = 3; break;\n case 'v': dir = 1; break;\n case '_': dir = (mem == 0 ? 0 : 2); break;\n case '|': dir = (mem == 0 ? 1 : 3); break;\n case '?': dir = rand() & 3; break;\n case '.': break;\n case '@': cout << \"YES\" << endl; return 0;\n case '+': mem = (mem + 1) & 15; break;\n case '-': mem = (mem - 1) & 15; break;\n default: mem = prog[y][x] - '0';\n }\n x += dx[dir]; y += dy[dir];\n if(x == -1) x = c - 1;\n if(x == c) x = 0;\n if(y == -1) y = r - 1;\n if(y == r) y = 0;\n }\n cout << \"NO\" << endl;\n}", "accuracy": 0.4, "time_ms": 30, "memory_kb": 1208, "score_of_the_acc": -0.068, "final_rank": 15 }, { "submission_id": "aoj_2262_449740", "code_snippet": "#include<stdio.h>\n#include<set>\n#include<queue>\n\nint dxs[]={1,0,-1,0};\nint dys[]={0,-1,0,1};//右、上、左、下の順\nint r,c;\n\nstruct cur{\n\tint x,y,arrow;//位置と向き\n\tint num;//メモリの状態\n\tbool operator<(const cur& c)const{\n\t\tif(x!=c.x)return x<c.x;\n\t\tif(y!=c.y)return y<c.y;\n\t\tif(arrow!=c.arrow)return arrow<c.arrow;\n\t\treturn num<c.num;\n\t}\n\tcur move(int arrow){\n\t\tcur re;\n\t\tre.x=(x+dxs[arrow]+c)%c;\n\t\tre.y=(y+dys[arrow]+r)%r;\n\t\tre.arrow=arrow;\n\t\tre.num=num;\n\t\treturn re;\n\t}\n};\n\n\nint main(){\n\tscanf(\"%d %d\",&r,&c);\n\tchar map[22][22];\n\tint x,y;\n\tfor(int i=0;i<r;i++){\n\t\tscanf(\"%s\",map[i]);\n\t}\n\tcur c1,nextC;\n\tc1.x=c1.y=c1.num=c1.arrow=0;\n\tstd::set<cur> memo;\n\tstd::queue<cur> Q;\n\tmemo.insert(c1);\n\tQ.push(c1);\n\tbool stop=false;\n\twhile(Q.empty()==false){\n\t\tc1=Q.front();\n\t\tQ.pop();\n\t\tx=c1.x;\n\t\ty=c1.y;\n\t\tchar com=map[y][x];\n\t\tif(com=='@'){\n\t\t\tstop=true;//停止\n\t\t\tbreak;\n\t\t}\n\t\tif(com=='?'){\n\t\t\t//4方向全て試す\n\t\t\tfor(int i=0;i<4;i++){\n\t\t\t\tnextC=c1.move(i);\n\t\t\t\tif(memo.find(nextC)==memo.end()){\n\t\t\t\t\tmemo.insert(nextC);\n\t\t\t\t\tQ.push(nextC);\n\t\t\t\t}\n\t\t\t}\n\t\t\tcontinue;\n\t\t}\n\t\tif(com=='<'){\n\t\t\tnextC=c1.move(2);//左\n\t\t}else if(com=='>'){\n\t\t\tnextC=c1.move(0);//右\t\n\t\t}else if(com=='^'){\n\t\t\tnextC=c1.move(1);//上\n\t\t}else if(com=='v'){\n\t\t\tnextC=c1.move(3);//下\n\t\t}else if(com=='_'){\n\t\t\tif(c1.num==0)nextC=c1.move(0);//0なら右\n\t\t\telse nextC=c1.move(2);\n\t\t}else if(com=='|'){\n\t\t\tif(c1.num==0)nextC=c1.move(3);//0なら下\n\t\t\telse nextC=c1.move(1);\n\t\t}else if(com=='.'){\n\t\t\tnextC=c1.move(c1.arrow);//そのまま\n\t\t}else if('0'<=com&&com<='9'){\n\t\t\tnextC=c1.move(c1.arrow);\n\t\t\tnextC.num=com-'0';\n\t\t}else if(com=='+'){\n\t\t\tnextC=c1.move(c1.arrow);\n\t\t\tnextC.num=(c1.num+1)%16;\n\t\t}else if(com=='-'){\n\t\t\tnextC=c1.move(c1.arrow);\n\t\t\tnextC.num=(c1.num-1+16)%16;\n\t\t}\n\t\tif(memo.find(nextC)==memo.end()){\n\t\t\tmemo.insert(nextC);\n\t\t\tQ.push(nextC);\n\t\t}\n\t}\n\tprintf(\"%s\\n\",stop?\"YES\":\"NO\");\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 0, "score_of_the_acc": -0.0149, "final_rank": 1 }, { "submission_id": "aoj_2262_370375", "code_snippet": "//template ver1.00\n//------------------------------------------\n\n//include\n//------------------------------------------\n#include <vector>\n#include <list>\n#include <map>\n#include <set>\n#include <stack>\n#include <queue>\n#include <bitset>\n#include <algorithm>\n#include <numeric>\n#include <utility>\n#include <sstream>\n#include <iostream>\n#include <iomanip>\n#include <cstdio>\n#include <cmath>\n#include <string>\n#include <cstring>\n#include <complex>\n\nusing namespace std;\n\n//conversion\n//------------------------------------------\ninline int to_int(string s) {int v; istringstream sin(s); sin >> v; return v;}\ntemplate<class T> inline string to_str(T x) {ostringstream sout; sout << x; return sout.str();}\n\n//math\n//-------------------------------------------\ntemplate<class T> inline T sqr(T x) {return x*x;}\ntemplate<class T> inline T is_inner(T x, T a, T b) {return a <= x && x < b;}\n\n//typedef\n//------------------------------------------\ntypedef vector<int> VI;\ntypedef vector<VI> VVI;\ntypedef vector<string> VS;\ntypedef pair<int, int> PII;\ntypedef long long LL;\n\n//container util\n//------------------------------------------\n#define ALL(a, n) (a), (a) + (n)\n#define CALL(c) (c).begin(),(c).end()\n#define VADD(v, e) (v).push_back(e)\n#define SADD(s, e) (s).insert(e)\n#define STADD(s, e) (s).push(e)\n#define QADD(q, e) (q).push(e)\n#define PQADD(q, e) (q).push(e)\n#define STPOP(s, e) typeof((s).top()) e = (s).top(); (s).pop()\n#define QPOP(q, e) typeof((q).front()) e = (q).front(); (q).pop()\n#define PQPOP(q, e) typeof((q).top()) e = (q).top(); (q).pop()\n#define MP make_pair\n#define PF(p) (p).first\n#define PS(p) (p).second\n#define SZ(a) int((a).size())\n#define EACH(i, c) for(typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)\n#define EXIST(c, e) ((c).find(e) != (c).end())\n#define SORT(a, n) sort(ALL(a, n))\n#define CSORT(c) sort(CALL(c))\n#define RSORT(a, n) sort(ALL(a, n), greater<typeof(a[0])>())\n#define CRSORT(c) sort(CALL((c)), greater<typeof(c[0])>())\n#define INDEX(a, n, x) find(ALL((a), n), x) - (a)\n\n//repetition\n//------------------------------------------\n#define FOR(i, a, b) for (int i = (a);i < (b); ++i)\n#define RFOR(i, a, b) for (int i = (b) - 1; i >= (a); --i)\n#define REP(i, n) FOR(i, 0, n)\n#define RREP(i, n) RFOR(i, 0, n)\n\n//IO\n//------------------------------------------\n#define LF(x) cout << (x) << endl;\n#define LF2(x, y) cout << (x) << \" \" << (y) << endl;\n#define LFA(a, n) cout << a[0]; FOR(i, 1, n) {cout << \" \" << a[i];} cout << endl;\n#define LFD(x, w) cout.width = (w); cout << (x) << endl;\n#define LFDA(a, n, w) REP(i, n) cout.width = (w); cout << a[i] << endl;\n#define LFP(x, w) cout << setprecision((w)); cout << setiosflags(ios::fixed); cout << (x) << endl;\n#define LFP2(x, y, w) cout << setprecision((w)); cout << setiosflags(ios::fixed); cout << (x) << \" \" << (y) << endl;\n#define LFPA(a, n, w) cout << setprecision((w)); cout << setiosflags(ios::fixed); cout << a[0]; FOR(i, 1, n) {cout << \" \" << a[i];} cout << endl;\n\n//constant\n//--------------------------------------------\nconst double EPS = 1e-10;\nconst double PI = acos(-1.0);\nconst int INF = 1e9;\nconst int DI[] = {0, 1, 0, -1};\nconst int DJ[] = {1, 0, -1, 0};\n\n//clear memory\n//--------------------------------------------\n#define CLR(a) memset((a), 0 , sizeof(a))\n\n//debug\n//--------------------------------------------\n#define DUMP(x) cerr << #x << \" = \" << (x) << endl;\n#define DEBUG(x) cerr << #x << \" = \" << (x) << \" (L\" << __LINE__ << \")\" << endl;\n#define DUMPA(a, n) cerr << #a << \" = {\" << a[0]; FOR(i, 1, n) { cout << \", \" << a[i]; } cerr << \"}\" << endl;\n#define DEBUGA(a, n) cerr << #a << \" = {\" << a[0]; FOR(i, 1, n) { cout << \", \" << a[i]; } cerr << \"} (L\" << __LINE__ << \")\" << endl;\n#define DUMPAA(a, n, m) REP(i, n) {REP(j, m) {cout << a[i][j] << \" \";} cout << endl;}\n#define DEBUGAA(a, n, m) DUMPAA(a, n, m) cout << \"(L\" << __LINE__ << \")\" << endl;\n\nconst int R = 20;\nconst int C = 20;\nconst int M = 16;\nconst int D = 4;\n\nstruct T {\n\tint i, j;\n\tint m, d;\n\t\n\tint toi()\n\t{\n\t\treturn i + R * j + R * C * m + R * C * M * d;\n\t}\n\t\n\tbool operator<(const T& t) const\n\t{\n\t\tif (d != t.d) return d < t.d;\n\t\tif (m != t.m) return m < t.m;\n\t\tif (j != t.j) return j < t.j;\n\t\treturn i < t.i ;\n\t}\n};\n\n\nint r, c;\nchar p[R][C];\n\nvector<T> e[R * C * M * D];\nset<T> used;\n\nint f(int x, int y, int m)\n{\n\treturn (x + y + m) % m;\n}\n\nbool dfs(T t)\n{\n\tif (EXIST(used, t)) return false;\n\tSADD(used, t);\n\tif (p[t.i][t.j] == '@') return true;\n\tEACH(i, e[t.toi()]) if (dfs(*i)) return true;\n\treturn false;\n}\n\nvoid init()\n{\n}\n\nvoid solve()\n{\n\tREP(i, R * C * M * D) e[i].clear();\n\tused.clear();\n\tREP(i, r) REP(j, c) REP(k, M) REP(l, D) {\n\t\tT t = {i, j, k, l};\n\t\tif (p[i][j] == '?') {\n\t\t\tREP(d, D) {\n\t\t\t\tT u = {f(i, DI[d], r), f(j, DJ[d], c), k, d};\n\t\t\t\tVADD(e[t.toi()], u);\n\t\t\t}\n\t\t\tcontinue;\n\t\t}\n\t\tT u;\n\t\tswitch (p[i][j]) {\n\t\tcase '<': u.d = 2; break;\n\t\tcase '>': u.d = 0; break;\n\t\tcase '^': u.d = 3; break;\n\t\tcase 'v': u.d = 1; break;\n\t\tcase '_': u.d = k == 0 ? 0 : 2; break;\n\t\tcase '|': u.d = k == 0 ? 1 : 3; break;\n\t\tdefault: u.d = l;\n\t\t}\n\t\tu.m = k;\n\t\tif ('0' <= p[i][j] && p[i][j] <= '9') u.m = p[i][j] - '0';\n\t\tif (p[i][j] == '+') u.m = f(k, 1, M);\n\t\tif (p[i][j] == '-') u.m = f(k, -1, M);\n\t\tu.i = f(i, DI[u.d], r);\n\t\tu.j = f(j, DJ[u.d], c);\n\t\tVADD(e[t.toi()], u);\n\t}\n\tT init = {0, 0, 0, 0};\n\tif (dfs(init)) {\n\t\tLF(\"YES\");\n\t}\n\telse LF(\"NO\");\n}\n\nint main()\n{\n\tinit();\n\tcin >> r >> c;\n\tREP(i, r) cin >> p[i];\n\tsolve();\n\treturn 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 5200, "score_of_the_acc": -0.1941, "final_rank": 9 }, { "submission_id": "aoj_2262_328456", "code_snippet": "#include <iostream>\n#include <sstream>\n#include <string>\n#include <algorithm>\n#include <vector>\n#include <stack>\n#include <queue>\n#include <set>\n#include <map>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <cmath>\n\nusing namespace std;\n\n#define FOR(i,k,n) for(int i=(k); i<(int)n; ++i)\n#define REP(i,n) FOR(i,0,n)\n#define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)\nstruct S{\n int x,y,r,m;\n S(int x,int y,int r,int m) :\n x(x),y(y),r(r),m(m) {}\n bool operator < (const S& s) const {\n return (x!=s.x)?(x<s.x):(y!=s.y)?(y<s.y):(r!=s.r)?(r<s.r):(m<s.m);\n }\n};\nint dx[4] = {1,0,-1,0};\nint dy[4] = {0,1,0,-1};\n\nint main(){\n int h,w;\n while(cin>>h>>w){\n vector<string> grid(h);\n REP(i,h)cin>>grid[i];\n queue<S> que;\n set<S> used;\n que.push(S(0,0,0,0));\n while(!que.empty()){\n S s = que.front(); que.pop();\n if(used.find(s)!=used.end()) continue;\n used.insert(s);\n bool all = false;\n bool end = false;\n switch(grid[s.y][s.x]){\n case '<':\n s.r = 2;\n break;\n case '>':\n s.r = 0;\n break;\n case '^':\n s.r = 3;\n break;\n case 'v':\n s.r = 1;\n break;\n case '_':\n s.r = ((s.m)?2:0);\n break;\n case '|':\n s.r = ((s.m)?3:1);\n break;\n case '?':\n all = true;\n break;\n case '.':\n break;\n case '@':\n end = true;\n break;\n case '+':\n s.m = (s.m+1)%16;\n break;\n case '-':\n s.m = (s.m+16-1)%16;\n break;\n }\n if(end){\n cout<<\"YES\"<<endl;\n goto END;\n }\n if(grid[s.y][s.x]<='9'&&grid[s.y][s.x]>='0'){\n s.m = (grid[s.y][s.x]-'0');\n }\n for(int r = 0; r < 4; r++){\n if(all || s.r == r){\n int nx = (s.x+dx[r]+w)%w;\n int ny = (s.y+dy[r]+h)%h;\n que.push(S(nx,ny,r,s.m));\n }\n }\n }\n cout<<\"NO\"<<endl; \nEND:;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 0, "score_of_the_acc": -0.0149, "final_rank": 1 }, { "submission_id": "aoj_2262_253315", "code_snippet": "#include<iostream>\n#include<vector>\nusing namespace std;\n\nint r,c,d;\nint h,w;\nbool table[20][20][16][4];\n\nvoid move(void){\n if(d == 0){\n w++;\n if(w == c)w = 0;\n }\n else if(d == 2){\n w--;\n if(w < 0)w = c-1;\n }\n else if(d == 3){\n h--;\n if(h < 0)h = r-1;\n }\n else if(d == 1){\n h++;\n if(h == r)h = 0;\n }\n}\n\nbool check(int h,int w,int m,int l){\n if(table[h][w][m][l])return false;\n else table[h][w][m][l] = true;\n return true;\n}\n\nint main(){\n string tmp;\n vector<string> map;\n int m,pos,flag;\n vector<int> qh,qw,qm,qd;\n\n m = 0;\n d = 0;\n h = 0;\n w = 0;\n\n cin >> r >> c;\n\n for(int i=0;i<r;i++){\n cin >> tmp;\n map.push_back(tmp);\n }\n\n for(int i=0;i<r;i++){\n for(int j=0;j<c;j++){\n for(int k=0;k<16;k++){\n\tfor(int l=0;l<4;l++)table[i][j][k][l] = false;\n }\n }\n }\n\n qh.clear();\n qw.clear();\n qm.clear();\n qd.clear();\n pos = 0;\n\n while(1){\n if(!check(h,w,m,d)){\n if(pos == qh.size()){\n\tcout << \"NO\" << endl;\n\treturn 0;\n }else{\n\th = qh[pos];\n\tw = qw[pos];\n\tm = qm[pos];\n\td = qd[pos];\n\tpos++;\n }\n }\n\n if(map[h][w] == '@'){\n cout << \"YES\" << endl;\n return 0;\n }else if(map[h][w] == '>'){\n d = 0;\n }else if(map[h][w] == 'v'){\n d = 1;\n }else if(map[h][w] == '<'){\n d = 2;\n }else if(map[h][w] == '^'){\n d = 3;\n }else if(map[h][w] == '_'){\n if(m)d = 2;\n else d = 0;\n }else if(map[h][w] == '|'){\n if(m)d = 3;\n else d = 1;\n }else if(map[h][w] == '+'){\n m++;\n if(m == 16)m = 0;\n }else if(map[h][w] == '-'){\n m--;\n if(m < 0)m = 15;\n }else if(map[h][w] == '?'){\n flag = 1;\n for(int i=0;i<qh.size();i++){\n\tif(h == qh[i] && w == qw[i] && m == qm[i]){\n\t flag = 0;\n\t break;\n\t}\n }\n if(flag){\n\td = 0;\n\tqh.push_back(h);\n\tqw.push_back(w);\n\tqm.push_back(m);\n\tqd.push_back(1);\n\n\tqh.push_back(h);\n\tqw.push_back(w);\n\tqm.push_back(m);\n\tqd.push_back(2);\n\n\tqh.push_back(h);\n\tqw.push_back(w);\n\tqm.push_back(m);\n\tqd.push_back(3);\n }\n }else if(map[h][w] == '.'){\n }else{\n m = map[h][w] - '0';\n }\n\n move();\n }\n \n return 0;\n}", "accuracy": 1, "time_ms": 500, "memory_kb": 1412, "score_of_the_acc": -0.7759, "final_rank": 10 }, { "submission_id": "aoj_2262_252744", "code_snippet": "#include <iostream>\n#include <cstdio>\n#include <vector>\n#include <algorithm>\n#include <complex>\n#include <cstring>\n#include <cstdlib>\n#include <string>\n#include <cmath>\n#include <queue>\n#include <set>\nusing namespace std;\n\n#define REP(i,n) for(int i=0;i<(int)n;++i)\n#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)\n#define ALL(c) (c).begin(), (c).end()\nconst int INF = 1<<29;\n\nstruct P{\n int d;\n int x, y;\n int mem;\n P(int x,int y,int d,int mem):x(x),y(y),d(d),mem(mem) {}\n};\nbool operator<(const P &a, const P &b) {\n return (a.x!=b.x)? a.x<b.x : a.y!=b.y? a.y<b.y : a.d!=b.d ? a.d < b.d : a.mem < b.mem;\n}\n\nint main() {\n int H,W;\n cin >> H >> W;\n char ba[W][H];\n REP(y,H) {\n REP(x,W)\n cin >> ba[x][y];\n }\n int dx[] = {0,1,0,-1};\n int dy[] = {-1,0,1,0};\n queue<P> Q;\n Q.push(P(-1,0,1,0));\n set<P> se;\n bool goal = 0;\n while(!Q.empty()) {\n P now = Q.front();\n Q.pop();\n if (se.count(now)) continue;\n se.insert(now);\n\n// printf(\"%d, %d d = %d mem = %d\\n\", now.x, now.y,now.d, now.mem);\n \n int xx = now.x + dx[now.d];\n int yy = now.y + dy[now.d];\n if (xx<0) xx = W-1;\n else if (xx>=W) xx = 0;\n if (yy<0) yy = H-1;\n else if (yy>=H) yy = 0;\n char c = ba[xx][yy];\n // cout << c << endl;\n int d = now.d;\n int mem = now.mem;\n if (c == '<') {\n d = 3;\n } else if (c == '>') {\n d = 1;\n } else if (c == '^') {\n d = 0;\n } else if (c == 'v') {\n d = 2;\n } else if (c == '_') {\n if (mem == 0) d = 1;\n else d = 3;\n } else if (c == '|') {\n if (mem == 0) d = 2;\n else d = 0; \n } else if (c == '?') {\n } else if (c == '.') {\n } else if (c == '@') {\n goal = 1;\n break;\n } else if (isdigit(c)) {\n mem = c-'0';\n } else if (c == '+') {\n if (mem == 15) mem = 0;\n else mem++;\n \n } else if (c == '-') {\n if (mem == 0) mem = 15;\n else mem--;\n }\n if (c == '?') {\n Q.push(P(xx,yy,0,mem));\n Q.push(P(xx,yy,1,mem));\n Q.push(P(xx,yy,2,mem));\n Q.push(P(xx,yy,3,mem)); \n } else {\n Q.push(P(xx,yy,d,mem));\n }\n }\n if (goal) {\n cout << \"YES\" << endl;\n } else\n cout << \"NO\" << endl;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 0, "score_of_the_acc": -0.0149, "final_rank": 1 }, { "submission_id": "aoj_2262_251789", "code_snippet": "#include <iostream>\n#include <vector>\n#include <string>\n#include <algorithm>\n#include <set>\nusing namespace std;\n\ntypedef vector <string> VS;\ntypedef pair <int, int> PII;\ntypedef pair <PII, PII> PPP;\ntypedef set <PPP> SPPP;\n\nint R, C;\nVS P;\nSPPP M;\n\nbool dfs( int a, int m, int x, int y ) {\n int na = a, nm = m, nx = x, ny = y;\n char op = P[y][x];\n\n // check\n PPP q( PII( a, m ), PII( x, y ) );\n if ( M.find( q ) != M.end() ) {\n return false;\n }\n M.insert( q );\n \n // operation\n if ( op == '<' ) {\n na = 0;\n } else if ( op == '^' ) {\n na = 1;\n } else if ( op == '>' ) {\n na = 2;\n } else if ( op == 'v' ) {\n na = 3;\n } else if ( op == '_' ) {\n if ( m == 0 ) {\n na = 2;\n } else {\n na = 0;\n }\n } else if ( op == '|' ) {\n if ( m == 0 ) {\n na = 3;\n } else {\n na = 1;\n }\n } else if ( op == '?' ) {\n for ( int i = 0; i < 4; i++ ) {\n na = i;\n int nnx = x, nny = y;\n // move\n if ( na == 0 ) {\n\tnnx--;\n } else if ( na == 1 ) {\n\tnny--;\n } else if ( na == 2 ) {\n\tnnx++;\n } else if ( na == 3 ) {\n\tnny++;\n }\n\n if ( nnx >= C ) {\n\tnnx = 0;\n } else if ( nnx < 0 ) {\n\tnnx = C - 1;\n }\n if ( nny >= R ) {\n\tnny = 0;\n } else if ( nny < 0 ) {\n\tnny = R - 1;\n }\n\n if ( dfs( na, nm, nnx, nny ) ) return true;\n }\n return false;\n } else if ( op == '.' ) {\n // empty\n } else if ( op == '@' ) {\n return true;\n } else if ( op >= '0' && op <= '9' ) {\n nm = (int)( op - '0' );\n } else if ( op == '+' ) {\n nm = ( nm + 1 ) % 16;\n } else if ( op == '-' ) {\n nm = ( nm - 1 ) % 16;\n }\n\n // move\n if ( na == 0 ) {\n nx--;\n } else if ( na == 1 ) {\n ny--;\n } else if ( na == 2 ) {\n nx++;\n } else if ( na == 3 ) {\n ny++;\n }\n\n if ( nx >= C ) {\n nx = 0;\n } else if ( nx < 0 ) {\n nx = C - 1;\n }\n if ( ny >= R ) {\n ny = 0;\n } else if ( ny < 0 ) {\n ny = R - 1;\n }\n\n if ( dfs( na, nm, nx, ny ) ) return true;\n return false;\n}\n\nint main( void ) {\n cin >> R >> C;\n for ( int i = 0; i < R; i++ ) {\n string line;\n cin >> line;\n P.push_back( line );\n }\n bool result = dfs( 2, 0, 0, 0 );\n cout << ( result ? \"YES\" : \"NO\" ) << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 0, "score_of_the_acc": -0.0149, "final_rank": 1 } ]
aoj_2266_cpp
問題 H : キャッシュ戦略 今,G○○gle Code Jam の地区大会が始まろうとしている. 斜め右前の席に座っている男の ID は lyrically と言うらしい. 東京大学時代の記憶に,似たような ID の仲間が居た覚えがあるが,僕の仲間は一人残さず美少女だったはずだ. 僕の記憶の中の lyrically は,アルゴリズムの力や実装の力もさながら, 気合で問題に正解することにも定評があった. 例えば,計算量が多少悪いプログラムでも,上手な実装をすることで高速にし, 正解とするようなことも得意としていた. プログラムを高速にする上で,非常に大切になってくるのが, キャッシュメモリとの親和性である. 問題 ブロック毎に読み込みのコストが異なるメモリを考える. 起こる全てのメモリアクセスがあらかじめ分かった状態での, フルアソシアティブのキャッシュメモリでの最善なキャッシュ戦略を求めたい. 以下,平易な言葉で説明する. M 個の箱と, N 個のボールがある. ボールには 1 から N までの番号が付いており, 各ボール i には重さ w i が決まっている. また,長さ K の数列 a 1 , a 2 , …, a K が与えられる. 各 a j は 1 ≤ a j ≤ N を満たす整数である. はじめは全ての箱は空である. j = 1, 2, …, K の順に,以下を行いたい. ボール a j が入っている箱があれば,何もしない. この操作にかかるコストは 0 である. ボール a j が入っている箱がなければ,いずれかの箱にボール a j を入れる. ボールを箱に入れる際,もし既にその箱に入っているボールがあれば,そのボールは外に出す. この操作にかかるコストは w a j である.(コストは,入れる箱や取り出すボール等にはよらない.) コストの和の最小値を計算するプログラムを作成せよ. 入力 入力の最初の行は 3 つの整数 M , N , K を含む. 続く N 行の i 行目には整数 w i が書かれている. 続く K 行の j 行目には整数 a j が書かれている. 出力 コストの和の最小値を出力せよ. 制約 1 ≤ M ≤ 10 1 ≤ N ≤ 10 4 1 ≤ K ≤ 10 4 1 ≤ w i ≤ 10 4 1 ≤ a j ≤ N 部分点 この問題の判定には,20 点分のテストケースのグループが設定されている. このグループに含まれるテストケースの入力は以下を満たす. 1 ≤ M ≤ 3 1 ≤ N ≤ 10 1 ≤ K ≤ 10 3 入出力例 入力例 1 入力例 1: 3 3 6 10 20 30 1 2 3 1 2 3 入力例 1 に対する出力例: 60 入力例 2 入力例 2: 2 3 6 10 20 30 1 2 3 1 2 3 入力例 2 に対する出力例: 80
[ { "submission_id": "aoj_2266_10849099", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\ntypedef long long ll;\nconst int N = 10005;\nconst int M = N * 4;\n\nint n, m, k;\nint w[N];\nint a[N];\nint lst[N];\n\ntypedef int ftype;\nconst ftype INF = 0x3f3f3f3f;\nint head[N], to[M], nxt[M], tot, cur[N];\nftype cap[M], cost[M], flow[N], dis[N], mncost, mxflow;\nbool vis[N];\ninline void init() { tot = 0; memset(head, -1, sizeof(head)); }\ninline void addedge(int x, int y, ftype w, ftype c) {\n to[tot] = y; cap[tot] = w; cost[tot] = c; nxt[tot] = head[x]; head[x] = tot++;\n to[tot] = x; cap[tot] = 0; cost[tot] = -c; nxt[tot] = head[y]; head[y] = tot++;\n}\nbool SPFA(int src, int sink) {\n memset(dis, 0x3f, sizeof(dis));\n memset(vis, 0, sizeof(vis));\n dis[src] = 0; cur[src] = -1; flow[src] = INF;\n queue<int> que; que.push(src);\n while (!que.empty()) {\n int u = que.front(); que.pop();\n vis[u] = false;\n for (int i = head[u], v; ~i; i = nxt[i]) {\n v = to[i];\n if (cap[i] > 0 && dis[v] > dis[u] + cost[i]) {\n dis[v] = dis[u] + cost[i]; flow[v] = min(flow[u], cap[i]); cur[v] = i;\n if (!vis[v]) { vis[v] = true; que.push(v); }\n }\n }\n }\n if (dis[sink] == INF) { return false; }\n mxflow += flow[sink]; mncost += flow[sink] * dis[sink];\n for (int i = cur[sink]; ~i; i = cur[to[i ^ 1]]) {\n cap[i] -= flow[sink]; cap[i ^ 1] += flow[sink];\n }\n return true;\n}\nftype MCMF(int src, int sink) {\n mxflow = mncost = 0;\n while (SPFA(src, sink));\n return mncost;\n}\n\nint main() {\n while (~scanf(\"%d%d%d\", &m, &n, &k)) {\n init(); memset(lst, 0, sizeof(lst));\n int sum = 0; m--;\n for (int i = 1; i <= n; i++) {\n scanf(\"%d\", &w[i]);\n }\n for (int i = 1; i <= k; i++) {\n scanf(\"%d\", &a[i]);\n }\n k = unique(a + 1, a + k + 1) - a - 1;\n for (int i = 1; i <= k; i++) {\n if (lst[a[i]]) { addedge(lst[a[i]], i - 1, 1, -w[a[i]]); }\n lst[a[i]] = i;\n sum += w[a[i]];\n }\n for (int i = 2; i <= k; i++) {\n addedge(i - 1, i, m, 0);\n }\n int src = k + 1, sink = src + 1;\n addedge(src, 1, m, 0);\n addedge(k, sink, m, 0);\n printf(\"%d\\n\", sum + MCMF(src, sink));\n }\n}", "accuracy": 1, "time_ms": 1280, "memory_kb": 4448, "score_of_the_acc": -1.0066, "final_rank": 9 }, { "submission_id": "aoj_2266_8972975", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nstruct edge{\n edge(int to_,int wt_,int cp_,int rev_):to(to_),wt(wt_),cp(cp_),rev(rev_){}\n int to,wt,cp,rev;\n};\n\nvector<vector<edge>> g(10005);\nvector<int> a(10005);\nvector<vector<int>> lst(10005);\nvector<int> w(10005);\nvector<int> prv(10005),pre(10005);\nint m,n,k;\n\nvoid adde(int f1,int t1,int w1,int c1){\n g[f1].push_back(edge(t1,w1,c1,g[t1].size()));\n g[t1].push_back(edge(f1,-w1,0,g[f1].size()-1));\n}\n\nint mincost(int stt,int stp,int flw){\n int ret=0;\n while(flw>0){\n vector<int> gm(k+2,INT_MAX);\n gm[stt]=0;\n bool flg=true;\n while(flg){\n flg=false;\n for(int i=0;i<k+2;i++){\n if(gm[i]==INT_MAX) continue;\n for(int j=0;j<(int)g[i].size();j++){\n auto e=g[i][j];\n if(e.cp==0) continue;\n if(gm[e.to]>gm[i]+e.wt){ \n gm[e.to]=gm[i]+e.wt;\n prv[e.to]=i;\n pre[e.to]=j;\n flg=true;\n }\n }\n \n }\n }\n int tmp=flw;\n for(int i=stp;i!=stt;i=prv[i]){\n tmp=min(tmp,g[prv[i]][pre[i]].cp);\n }\n \n flw-=tmp;\n ret+=tmp*gm[stp];\n for(int i=stp;i!=stt;i=prv[i]){\n g[prv[i]][pre[i]].cp-=tmp;\n g[i][g[prv[i]][pre[i]].rev].cp+=tmp;\n }\n \n }\n return ret;\n}\n\nint main(){\n cin >> m >> n >> k;\n for(int i=0;i<n;i++) cin >> w[i];\n int ans=0;\n for(int i=0;i<k;i++){ \n cin >> a[i];\n a[i]--;\n ans+=w[a[i]];\n lst[a[i]].push_back(i);\n }\n \n for(int i=0;i<k;i++){\n if((i>0)&&(a[i-1]==a[i])) continue;\n }\n \n int stt=k,stp=k+1;\n adde(stt,0,0,m);\n adde(k-1,stp,0,m);\n for(int i=0;i<k-1;i++) adde(i,i+1,0,m);\n \n for(int i=0;i<n;i++){\n if(lst[i].size()<=1) continue;\n for(int j=0;j<(int)lst[i].size()-1;j++){\n if(lst[i][j]+1==lst[i][j+1]){\n ans-=w[i];\n continue;\n }\n adde(lst[i][j]+1,lst[i][j+1],-w[i],1);\n }\n }\n cout << ans+mincost(k,k+1,m-1) << endl;\n}", "accuracy": 1, "time_ms": 920, "memory_kb": 4888, "score_of_the_acc": -0.7311, "final_rank": 8 }, { "submission_id": "aoj_2266_8409566", "code_snippet": "#include <cstdio>\n#include <algorithm>\n#include <cstring>\n#include <vector>\n#include <utility>\nusing namespace std;\nconst int INF=0x3f3f3f3f;\ntypedef long long LL;\nstruct edge{\n int to,cap,cost,rev;\n edge(int to,int cap,int cost,int rev):to(to),cap(cap),cost(cost),rev(rev){}\n};\nconst int MAX_V=10010;\nint V,dist[MAX_V],prevv[MAX_V],preve[MAX_V];\nvector<edge> G[MAX_V];\nvoid add_edge(int from,int to,int cap,int cost){\n G[from].push_back(edge(to,cap,cost,G[to].size()));\n G[to].push_back(edge(from,0,-cost,G[from].size()-1));\n}\nint min_cost_flow(int s,int t,int f){\n int res=0;\n while(f>0){\n fill(dist,dist+V,INF);\n dist[s]=0;\n bool update=1;\n while(update){\n update=0;\n for(int v=0;v<V;v++){\n if(dist[v]==INF)continue;\n for(int i=0;i<G[v].size();i++){\n edge &e=G[v][i];\n if(e.cap>0&&dist[e.to]>dist[v]+e.cost){\n dist[e.to]=dist[v]+e.cost;\n prevv[e.to]=v;\n preve[e.to]=i;\n update=1;\n }\n }\n }\n }\n if(dist[t]==INF)return -1;\n int d=f;\n for(int v=t;v!=s;v=prevv[v]){\n d=min(d,G[prevv[v]][preve[v]].cap);\n }f-=d;\n res+=d*dist[t];\n for(int v=t;v!=s;v=prevv[v]){\n edge &e=G[prevv[v]][preve[v]];\n e.cap-=d;\n G[v][e.rev].cap+=d;\n }\n }return res;\n}\nvoid clear(){for(int i=0;i<=V;i++)G[i].clear();}\nconst int MAX_N=10010;\nint M,N,K,w[MAX_N],lst[MAX_N],a[MAX_N];\nint solve(){\n for(int i=1;i<=N;i++)scanf(\"%d\",&w[i]);\n for(int i=1;i<=K;i++)scanf(\"%d\",&a[i]);\n int cnt=unique(a+1,a+K+1)-(a+1);\n int res=0;\n memset(lst,0,sizeof(lst));\n V=cnt+1; clear();\n for(int i=1;i<=cnt;i++){\n res+=w[a[i]];\n if(lst[a[i]])add_edge(lst[a[i]],i-1,1,-w[a[i]]);\n lst[a[i]]=i;\n }for(int i=1;i<cnt;i++)add_edge(i,i+1,INF,0);\n printf(\"%d\\n\",res+min_cost_flow(1,cnt,M-1));\nreturn 1;\n}\nint main(){\n while(~scanf(\"%d%d%d\",&M,&N,&K))solve();\n return 0;\n}", "accuracy": 1, "time_ms": 560, "memory_kb": 4264, "score_of_the_acc": -0.4363, "final_rank": 7 }, { "submission_id": "aoj_2266_7324705", "code_snippet": "#define _CRT_SECURE_NO_WARNINGS\n#include <cstdio>\n#include <iostream>\n#include <stack>\n#include <queue>\n#include <algorithm>\n#include <functional>\n#include <set>\n#include <map>\n#include <string>\n#include <vector>\n#include <cmath>\n#include<sstream>\n#include<list>\n#include<iomanip>\n#include <cstdlib>\n#include <cstring>\n#include <stack>\n#include <bitset>\n#include <cassert>\n#include <stdlib.h>\n#include <stdio.h>\nusing namespace std;\nconst int INF = 100000000;\nconst long long LINF = 3e18 + 7;\nconst int MAX_N = 2000010;\nconst int MAX_W = 10002;\nconst int MAX_ARRAYK = 100000;\ndouble PI = 3.14159265358979323846;\n//using ll = long long;\n\n// https://area.hateblo.jp/entry/2017/12/18/010657\n\n// Minimum Cost Flow (Dijkstra)\n// https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_6_B&lang=ja\n// 蟻本 p. 203\n// 始点0, 終点V-1\n\nint V, E, F;\nstruct edge { int to, cap, cost, rev; };\ntypedef pair<int, int> P;\n\nvector<edge> G[MAX_N];\nint h[MAX_N];\nint dist[MAX_N];\nint prevv[MAX_N], preve[MAX_N];\n\n\n// from からtoへ向かう容量cap, コストcostの辺をグラフに追加する\nvoid add_edge(int from, int to, int cap, int cost) {\n\tedge e1 = { to, cap, cost, G[to].size() };\n\tG[from].push_back(e1);\n\tedge e2 = { from, 0, -cost, G[from].size() - 1 };\n\tG[to].push_back(e2);\n\n}\n\n// sからtへの流量f最小費用流を求める\n// 流せない場合は-1を返す\nint min_cost_flow(int s, int t, int f) {\n\tint res = 0;\n\twhile (f > 0) {\n\t\t//ベルマンフォードにより、s-t間最短路を求める\n\t\tfill(dist, dist + V, INF);\n\t\tdist[s] = 0;\n\t\tbool update = true;\n\t\twhile (update) {\n\t\t\tupdate = false;\n\t\t\tfor (int v = 0; v < V; v++) {\n\t\t\t\tif (dist[v] == INF) {\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tfor (int i = 0; i < G[v].size(); i++) {\n\t\t\t\t\tedge& e = G[v][i];\n\t\t\t\t\tif (e.cap > 0 && dist[e.to] > dist[v] + e.cost) {\n\t\t\t\t\t\tdist[e.to] = dist[v] + e.cost;\n\t\t\t\t\t\tprevv[e.to] = v;\n\t\t\t\t\t\tpreve[e.to] = i;\n\t\t\t\t\t\tupdate = true;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif (dist[t] == INF) {\n\t\t\treturn -1;\n\t\t}\n\n\t\t// s-t間最短路に沿って目一杯流す。\n\t\tint d = f;\n\t\tfor (int v = t; v != s; v = prevv[v]) {\n\t\t\td = min(d, G[prevv[v]][preve[v]].cap);\n\t\t}\n\n\t\tf -= d;\n\t\tres += (d * dist[t]);\n\t\tfor (int v = t; v != s; v = prevv[v]) {\n\t\t\tedge& e = G[prevv[v]][preve[v]];\n\t\t\te.cap -= d;\n\t\t\tG[v][e.rev].cap += d;\n\t\t}\n\t}\n\n\treturn res;\n\n}\n\nint M, N, K;\nint w[10010];\nint a[10010];\nint idx[10010];\n\nint main() {\n\n\tcin >> M >> N >> K;\n\n\tfor (int i = 0; i < N; i++) {\n\t\tcin >> w[i];\n\t}\n\tfor (int i = 0; i < K; i++) {\n\t\tcin >> a[i];\n\t\ta[i]--;\n\t}\n\tvector<int> v;\n\tv.push_back(a[0]);\n\tfor (int i = 1; i < K; i++) {\n\t\tif (v[v.size() - 1] != a[i]) {\n\t\t\tv.push_back(a[i]);\n\t\t}\n\t}\n\t\n\tV = v.size();\n\n\tfor (int i = 0; i < N; i++) {\n\t\tidx[i] = -1;\n\t}\n\n\tfor (int i = 0; i < V - 1; i++) {\n\t\tadd_edge(i, i + 1, INF, 0);\n\t}\n\n\tint sum = 0;\n\tfor (int i = 0; i < v.size(); i++) {\n\t\tsum += w[v[i]];\n\t\tif (idx[v[i]] == -1) {\n\t\t\tidx[v[i]] = i;\n\t\t}\n\t\telse {\n\t\t\tadd_edge(idx[v[i]] + 1, i, 1, -w[v[i]]);\n\t\t\tidx[v[i]] = i;\n\t\t}\n\t}\n\n\tcout << sum + min_cost_flow(0, V - 1, M - 1) << endl;\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 650, "memory_kb": 51112, "score_of_the_acc": -1.3624, "final_rank": 12 }, { "submission_id": "aoj_2266_7071601", "code_snippet": "#ifndef HIDDEN_IN_VS // 折りたたみ用\n\n// 警告の抑制\n#define _CRT_SECURE_NO_WARNINGS\n\n// ライブラリの読み込み\n#include <bits/stdc++.h>\nusing namespace std;\n\n// 型名の短縮\nusing ll = long long; // -2^63 ~ 2^63 = 9 * 10^18(int は -2^31 ~ 2^31 = 2 * 10^9)\nusing pii = pair<int, int>;\tusing pll = pair<ll, ll>;\tusing pil = pair<int, ll>;\tusing pli = pair<ll, int>;\nusing vi = vector<int>;\t\tusing vvi = vector<vi>;\t\tusing vvvi = vector<vvi>;\nusing vl = vector<ll>;\t\tusing vvl = vector<vl>;\t\tusing vvvl = vector<vvl>;\nusing vb = vector<bool>;\tusing vvb = vector<vb>;\t\tusing vvvb = vector<vvb>;\nusing vc = vector<char>;\tusing vvc = vector<vc>;\t\tusing vvvc = vector<vvc>;\nusing vd = vector<double>;\tusing vvd = vector<vd>;\t\tusing vvvd = vector<vvd>;\ntemplate <class T> using priority_queue_rev = priority_queue<T, vector<T>, greater<T>>;\nusing Graph = vvi;\n\n// 定数の定義\nconst double PI = acos(-1);\nconst vi DX = { 1, 0, -1, 0 }; // 4 近傍(下,右,上,左)\nconst vi DY = { 0, 1, 0, -1 };\nint INF = 1001001001; ll INFL = 4004004004004004004LL;\ndouble EPS = 1e-12;\n\n// 入出力高速化\nstruct fast_io { fast_io() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(18); } } fastIOtmp;\n\n// 汎用マクロの定義\n#define all(a) (a).begin(), (a).end()\n#define sz(x) ((int)(x).size())\n#define lbpos(a, x) (int)distance((a).begin(), std::lower_bound(all(a), x))\n#define ubpos(a, x) (int)distance((a).begin(), std::upper_bound(all(a), x))\n#define Yes(b) {cout << ((b) ? \"Yes\\n\" : \"No\\n\");}\n#define rep(i, n) for(int i = 0, i##_len = int(n); i < i##_len; ++i) // 0 から n-1 まで昇順\n#define repi(i, s, t) for(int i = int(s), i##_end = int(t); i <= i##_end; ++i) // s から t まで昇順\n#define repir(i, s, t) for(int i = int(s), i##_end = int(t); i >= i##_end; --i) // s から t まで降順\n#define repe(v, a) for(const auto& v : (a)) // a の全要素(変更不可能)\n#define repea(v, a) for(auto& v : (a)) // a の全要素(変更可能)\n#define repb(set, d) for(int set = 0; set < (1 << int(d)); ++set) // d ビット全探索(昇順)\n#define repp(a) sort(all(a)); for(bool a##_perm = true; a##_perm; a##_perm = next_permutation(all(a))) // a の順列全て(昇順)\n#define smod(n, m) ((((n) % (m)) + (m)) % (m)) // 非負mod\n#define uniq(a) {sort(all(a)); (a).erase(unique(all(a)), (a).end());} // 重複除去\n#define EXIT(a) {cout << (a) << endl; exit(0);} // 強制終了\n\n// 汎用関数の定義\ntemplate <class T> inline ll pow(T n, int k) { ll v = 1; rep(i, k) v *= n; return v; }\ntemplate <class T> inline bool chmax(T& M, const T& x) { if (M < x) { M = x; return true; } return false; } // 最大値を更新(更新されたら true を返す)\ntemplate <class T> inline bool chmin(T& m, const T& x) { if (m > x) { m = x; return true; } return false; } // 最小値を更新(更新されたら true を返す)\n\n// 演算子オーバーロード\ntemplate <class T, class U> inline istream& operator>>(istream& is, pair<T, U>& p) { is >> p.first >> p.second; return is; }\ntemplate <class T> inline istream& operator>>(istream& is, vector<T>& v) { repea(x, v) is >> x; return is; }\ntemplate <class T> inline vector<T>& operator--(vector<T>& v) { repea(x, v) --x; return v; }\ntemplate <class T> inline vector<T>& operator++(vector<T>& v) { repea(x, v) ++x; return v; }\n\n// 手元環境(Visual Studio)\n#ifdef _MSC_VER\n#include \"local.hpp\"\n// 提出用(gcc)\n#else\ninline int popcount(int n) { return __builtin_popcount(n); }\ninline int popcount(ll n) { return __builtin_popcountll(n); }\ninline int lsb(int n) { return n != 0 ? __builtin_ctz(n) : -1; }\ninline int lsb(ll n) { return n != 0 ? __builtin_ctzll(n) : -1; }\ninline int msb(int n) { return n != 0 ? (31 - __builtin_clz(n)) : -1; }\ninline int msb(ll n) { return n != 0 ? (63 - __builtin_clzll(n)) : -1; }\n#define gcd __gcd\n#define dump(...)\n#define dumpel(v)\n#define dump_list(v)\n#define input_from_file(f)\n#define output_to_file(f)\n#define Assert(b) { if (!(b)) while (1) cout << \"OLE\"; }\n#endif\n\n#endif // 折りたたみ用\n\n\n////--------------AtCoder 専用--------------\n//#include <atcoder/all>\n//using namespace atcoder;\n//\n////using mint = modint1000000007;\n//using mint = modint998244353;\n////using mint = modint; // mint::set_mod(m);\n//\n//istream& operator>>(istream& is, mint& x) { ll x_; is >> x_; x = x_; return is; }\n//ostream& operator<<(ostream& os, const mint& x) { os << x.val(); return os; }\n//using vm = vector<mint>; using vvm = vector<vm>; using vvvm = vector<vvm>;\n////----------------------------------------\n\n\n#ifndef ATCODER_INTERNAL_CSR_HPP\n#define ATCODER_INTERNAL_CSR_HPP 1\n\n#include <algorithm>\n#include <utility>\n#include <vector>\n\nnamespace atcoder {\n namespace internal {\n\n template <class E> struct csr {\n std::vector<int> start;\n std::vector<E> elist;\n explicit csr(int n, const std::vector<std::pair<int, E>>& edges)\n : start(n + 1), elist(edges.size()) {\n for (auto e : edges) {\n start[e.first + 1]++;\n }\n for (int i = 1; i <= n; i++) {\n start[i] += start[i - 1];\n }\n auto counter = start;\n for (auto e : edges) {\n elist[counter[e.first]++] = e.second;\n }\n }\n };\n\n } // namespace internal\n\n} // namespace atcoder\n\n#endif // ATCODER_INTERNAL_CSR_HPP\n\n\n#ifndef ATCODER_INTERNAL_QUEUE_HPP\n#define ATCODER_INTERNAL_QUEUE_HPP 1\n\n#include <vector>\n\nnamespace atcoder {\n\n namespace internal {\n\n template <class T> struct simple_queue {\n std::vector<T> payload;\n int pos = 0;\n void reserve(int n) { payload.reserve(n); }\n int size() const { return int(payload.size()) - pos; }\n bool empty() const { return pos == int(payload.size()); }\n void push(const T& t) { payload.push_back(t); }\n T& front() { return payload[pos]; }\n void clear() {\n payload.clear();\n pos = 0;\n }\n void pop() { pos++; }\n };\n\n } // namespace internal\n\n} // namespace atcoder\n\n#endif // ATCODER_INTERNAL_QUEUE_HPP\n\n\n#ifndef ATCODER_MINCOSTFLOW_HPP\n#define ATCODER_MINCOSTFLOW_HPP 1\n\n#include <algorithm>\n#include <cassert>\n#include <limits>\n#include <queue>\n#include <vector>\n\nnamespace atcoder {\n\n template <class Cap, class Cost> struct mcf_graph {\n public:\n mcf_graph() {}\n explicit mcf_graph(int n) : _n(n) {}\n\n int add_edge(int from, int to, Cap cap, Cost cost) {\n assert(0 <= from && from < _n);\n assert(0 <= to && to < _n);\n assert(0 <= cap);\n assert(0 <= cost);\n int m = int(_edges.size());\n _edges.push_back({ from, to, cap, 0, cost });\n return m;\n }\n\n struct edge {\n int from, to;\n Cap cap, flow;\n Cost cost;\n };\n\n edge get_edge(int i) {\n int m = int(_edges.size());\n assert(0 <= i && i < m);\n return _edges[i];\n }\n std::vector<edge> edges() { return _edges; }\n\n std::pair<Cap, Cost> flow(int s, int t) {\n return flow(s, t, std::numeric_limits<Cap>::max());\n }\n std::pair<Cap, Cost> flow(int s, int t, Cap flow_limit) {\n return slope(s, t, flow_limit).back();\n }\n std::vector<std::pair<Cap, Cost>> slope(int s, int t) {\n return slope(s, t, std::numeric_limits<Cap>::max());\n }\n std::vector<std::pair<Cap, Cost>> slope(int s, int t, Cap flow_limit) {\n assert(0 <= s && s < _n);\n assert(0 <= t && t < _n);\n assert(s != t);\n\n int m = int(_edges.size());\n std::vector<int> edge_idx(m);\n\n auto g = [&]() {\n std::vector<int> degree(_n), redge_idx(m);\n std::vector<std::pair<int, _edge>> elist;\n elist.reserve(2 * m);\n for (int i = 0; i < m; i++) {\n auto e = _edges[i];\n edge_idx[i] = degree[e.from]++;\n redge_idx[i] = degree[e.to]++;\n elist.push_back({ e.from, {e.to, -1, e.cap - e.flow, e.cost} });\n elist.push_back({ e.to, {e.from, -1, e.flow, -e.cost} });\n }\n auto _g = internal::csr<_edge>(_n, elist);\n for (int i = 0; i < m; i++) {\n auto e = _edges[i];\n edge_idx[i] += _g.start[e.from];\n redge_idx[i] += _g.start[e.to];\n _g.elist[edge_idx[i]].rev = redge_idx[i];\n _g.elist[redge_idx[i]].rev = edge_idx[i];\n }\n return _g;\n }();\n\n auto result = slope(g, s, t, flow_limit);\n\n for (int i = 0; i < m; i++) {\n auto e = g.elist[edge_idx[i]];\n _edges[i].flow = _edges[i].cap - e.cap;\n }\n\n return result;\n }\n\n private:\n int _n;\n std::vector<edge> _edges;\n\n // inside edge\n struct _edge {\n int to, rev;\n Cap cap;\n Cost cost;\n };\n\n std::vector<std::pair<Cap, Cost>> slope(internal::csr<_edge>& g,\n int s,\n int t,\n Cap flow_limit) {\n // variants (C = maxcost):\n // -(n-1)C <= dual[s] <= dual[i] <= dual[t] = 0\n // reduced cost (= e.cost + dual[e.from] - dual[e.to]) >= 0 for all edge\n\n // dual_dist[i] = (dual[i], dist[i])\n std::vector<std::pair<Cost, Cost>> dual_dist(_n);\n std::vector<int> prev_e(_n);\n std::vector<bool> vis(_n);\n struct Q {\n Cost key;\n int to;\n bool operator<(Q r) const { return key > r.key; }\n };\n std::vector<int> que_min;\n std::vector<Q> que;\n auto dual_ref = [&]() {\n for (int i = 0; i < _n; i++) {\n dual_dist[i].second = std::numeric_limits<Cost>::max();\n }\n std::fill(vis.begin(), vis.end(), false);\n que_min.clear();\n que.clear();\n\n // que[0..heap_r) was heapified\n size_t heap_r = 0;\n\n dual_dist[s].second = 0;\n que_min.push_back(s);\n while (!que_min.empty() || !que.empty()) {\n int v;\n if (!que_min.empty()) {\n v = que_min.back();\n que_min.pop_back();\n }\n else {\n while (heap_r < que.size()) {\n heap_r++;\n std::push_heap(que.begin(), que.begin() + heap_r);\n }\n v = que.front().to;\n std::pop_heap(que.begin(), que.end());\n que.pop_back();\n heap_r--;\n }\n if (vis[v]) continue;\n vis[v] = true;\n if (v == t) break;\n // dist[v] = shortest(s, v) + dual[s] - dual[v]\n // dist[v] >= 0 (all reduced cost are positive)\n // dist[v] <= (n-1)C\n Cost dual_v = dual_dist[v].first, dist_v = dual_dist[v].second;\n for (int i = g.start[v]; i < g.start[v + 1]; i++) {\n auto e = g.elist[i];\n if (!e.cap) continue;\n // |-dual[e.to] + dual[v]| <= (n-1)C\n // cost <= C - -(n-1)C + 0 = nC\n Cost cost = e.cost - dual_dist[e.to].first + dual_v;\n if (dual_dist[e.to].second - dist_v > cost) {\n Cost dist_to = dist_v + cost;\n dual_dist[e.to].second = dist_to;\n prev_e[e.to] = e.rev;\n if (dist_to == dist_v) {\n que_min.push_back(e.to);\n }\n else {\n que.push_back(Q{ dist_to, e.to });\n }\n }\n }\n }\n if (!vis[t]) {\n return false;\n }\n\n for (int v = 0; v < _n; v++) {\n if (!vis[v]) continue;\n // dual[v] = dual[v] - dist[t] + dist[v]\n // = dual[v] - (shortest(s, t) + dual[s] - dual[t]) +\n // (shortest(s, v) + dual[s] - dual[v]) = - shortest(s,\n // t) + dual[t] + shortest(s, v) = shortest(s, v) -\n // shortest(s, t) >= 0 - (n-1)C\n dual_dist[v].first -= dual_dist[t].second - dual_dist[v].second;\n }\n return true;\n };\n Cap flow = 0;\n Cost cost = 0, prev_cost_per_flow = -1;\n std::vector<std::pair<Cap, Cost>> result = { {Cap(0), Cost(0)} };\n while (flow < flow_limit) {\n if (!dual_ref()) break;\n Cap c = flow_limit - flow;\n for (int v = t; v != s; v = g.elist[prev_e[v]].to) {\n c = std::min(c, g.elist[g.elist[prev_e[v]].rev].cap);\n }\n for (int v = t; v != s; v = g.elist[prev_e[v]].to) {\n auto& e = g.elist[prev_e[v]];\n e.cap += c;\n g.elist[e.rev].cap -= c;\n }\n Cost d = -dual_dist[s].first;\n flow += c;\n cost += c * d;\n if (prev_cost_per_flow == d) {\n result.pop_back();\n }\n result.push_back({ flow, cost });\n prev_cost_per_flow = d;\n }\n return result;\n }\n };\n\n} // namespace atcoder\n\n#endif // ATCODER_MINCOSTFLOW_HPP\n\n\nusing namespace atcoder;\n\n\n//【最小費用流(負コスト可,負コスト閉路なし)】\n/*\n* Negative_mcf_graph(int n) : O(1)\n*\tn 頂点で初期化する.\n*\n* add_edge(int s, int t, ll cap, ll cost) : O(1)\n*\ts から t へ容量 cap,コスト cost の辺を追加する.\n*\n* pll flow(int ST, int GL, ll f_lim = INFL) : O(F (n + m) log n)(F:流量,m:辺の数)\n*\tST から GL まで flow_limit まで流せるだけ流したときの {流量, 最小コスト} を返す.\n*\t制約:負コストの閉路は存在しない\n*/\nstruct Negative_mcf_graph {\n\t// 参考 : https://ikatakos.com/pot/programming_algorithm/graph_theory/minimum_cost_flow\n\n\t// n : 頂点数\n\tint n;\n\n\t// 辺\n\tstruct Edge {\n\t\tint to;\n\t\tll cap, cost;\n\n\t\tEdge(int to_, ll cap_, ll cost_) : to(to_), cap(cap_), cost(cost_) {}\n\n#ifdef _MSC_VER\n\t\tfriend ostream& operator<<(ostream& os, const Edge& e) {\n\t\t\tos << \"(to:\" << e.to << \", cap:\" << e.cap << \", cost:\" << e.cost << \")\";\n\t\t\treturn os;\n\t\t}\n#endif\n\t};\n\n\t// 元のグラフ(負辺あり)\n\tvector<vector<Edge>> g;\n\n\t// pot[s] : 頂点 s のポテンシャル\n\tvl pot;\n\n\t// n 頂点で初期化する.\n\tNegative_mcf_graph(int n_) : n(n_), g(n), pot(n, INFL) {}\n\n\t// s から t へ容量 cap,コスト cost の辺を追加する.\n\tvoid add_edge(int s, int t, ll cap, ll cost) {\n\t\t// verify : https://atcoder.jp/contests/code-festival-2014-china-open/tasks/code_festival_china_h\n\n\t\tg[s].emplace_back(t, cap, cost);\n\t}\n\n\t// ベルマンフォード法で ST からの距離を求め,それをポテンシャルとする.\n\tbool bellman_ford(int ST) {\n\t\tpot[ST] = 0;\n\n\t\trep(i, n) {\n\t\t\tbool updated = false;\n\n\t\t\t// 全ての辺についての操作\n\t\t\trep(s, n) repe(e, g[s]) {\n\t\t\t\t// もし (始点への距離) + (辺のコスト) < (終点への距離) なら (終点への距離) を更新する.\n\t\t\t\t// INFL からは何を引いても INFL になるようにしているので,ST から到達可能な負閉路しか検出しない.\n\t\t\t\tif (pot[s] != INFL && pot[s] + e.cost < pot[e.to]) {\n\t\t\t\t\tpot[e.to] = pot[s] + e.cost;\n\t\t\t\t\tupdated = true;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// もし距離の更新が起こらなければ最短距離確定\n\t\t\tif (!updated) return true;\n\t\t}\n\n\t\t// もし全ての辺についての操作を n 回繰り返しても距離の更新があったなら,\n\t\t// ST から到達可能な負の閉路を持っているので false を返す.\n\t\treturn false;\n\t}\n\n\t// ST から GL まで flow_limit まで流せるだけ流したときの {流量, 最小コスト} を返す.\n\tpll flow(int ST, int GL, ll f_lim = INFL) {\n\t\t// verify : https://atcoder.jp/contests/code-festival-2014-china-open/tasks/code_festival_china_h\n\n\t\t// ベルマンフォード法で ST から v までの距離を求め,それをポテンシャル pot[v] とする.\n\t\tbool non_neg_cyc = bellman_ford(ST);\n\t\tAssert(non_neg_cyc);\n\n\t\t// g_pos : 辺 s→t のコストが (元の辺のコスト) - (pot[t] - pot[s]) >= 0 であるようなグラフ\n\t\tmcf_graph<ll, ll> g_pos(n);\n\t\trep(s, n) repe(e, g[s]) {\n\t\t\tg_pos.add_edge(s, e.to, e.cap, e.cost - (pot[e.to] - pot[s]));\n\t\t}\n\n\t\t// g_pos の最小費用流を求める.\n\t\tll cap, cost;\n\t\ttie(cap, cost) = g_pos.flow(ST, GL, f_lim);\n\n\t\t// 実際のコストは (流量) * pot[GL] を加えたものになる.\n\t\tcost += cap * pot[GL];\n\n\t\treturn make_pair(cap, cost);\n\t}\n};\n\n\nll MLE(int m, int n, int k, vi w, vi a) {\n --a;\n\n vvi nxt(k + 1, vi(n, k));\n repir(i, k - 1, 0) {\n rep(c, n) {\n nxt[i][c] = nxt[i + 1][c];\n }\n nxt[i][a[i]] = i;\n }\n dumpel(nxt);\n\n int ST = 2 * k, GL = ST + 1;\n Negative_mcf_graph g(GL + 1);\n\n rep(i, n) {\n if (nxt[0][i] < k) {\n g.add_edge(ST, 2 * nxt[0][i], 1, w[i]);\n }\n else {\n g.add_edge(ST, GL, 1, 0);\n }\n }\n\n rep(t, k) {\n g.add_edge(2 * t, 2 * t + 1, 1, -INF);\n\n rep(i, n) {\n if (a[t] == i) {\n if (nxt[t + 1][i] < k) {\n g.add_edge(2 * t + 1, 2 * nxt[t + 1][i], 1, 0);\n }\n else {\n g.add_edge(2 * t + 1, GL, 1, 0);\n }\n }\n else {\n if (nxt[t + 1][i] < k) {\n g.add_edge(2 * t + 1, 2 * nxt[t + 1][i], 1, w[i]);\n }\n }\n }\n }\n\n dumpel(g.g);\n\n ll cap, cost;\n tie(cap, cost) = g.flow(ST, GL, m);\n dump(cap, cost);\n\n ll res = cost + (ll)INF * k;\n\n return res;\n}\n\n\n//【自身と同じ数の次の位置】O(n)\n/*\n* a[0..n) の各要素 a[i] について,j > i かつ a[j] = a[i] となる最小の j を\n* nxt[j] に格納する.(存在しなければ n)\n*/\ntemplate <class T> void next_equal(const vector<T>& a, vi& nxt) {\n // verify : https://atcoder.jp/contests/agc036/tasks/agc036_b\n\n int n = sz(a);\n\n // nxt[i] : j > i かつ a[j] = a[i] となる最小の j(なければ n)\n nxt = vi(n, n);\n\n // num_to_pos[x] : 値 x が最後に現れた位置(右から走査する)\n unordered_map<T, int> num_to_pos;\n\n repir(i, n - 1, 0) {\n if (num_to_pos.count(a[i])) {\n nxt[i] = num_to_pos[a[i]];\n }\n num_to_pos[a[i]] = i;\n }\n}\n\n\n//【最小費用流(負コスト可,DAG)】\n/*\n* Negative_mcf_graph(int n) : O(1)\n*\tn 頂点で初期化する.\n*\n* add_edge(int s, int t, ll cap, ll cost) : O(1)\n*\ts から t へ容量 cap,コスト cost の辺を追加する.\n*\n* pll flow(int ST, int GL, ll f_lim = INFL) : O(F (n + m) log n)(F:流量,m:辺の数)\n*\tST から GL まで flow_limit まで流せるだけ流したときの {流量, 最小コスト} を返す.\n*\t制約:閉路は存在しない\n*/\nstruct Negative_mcf_graph_DAG {\n // 参考 : https://ikatakos.com/pot/programming_algorithm/graph_theory/minimum_cost_flow\n\n // n : 頂点数\n int n;\n\n // 辺\n struct Edge {\n int to;\n ll cap, cost;\n\n Edge(int to_, ll cap_, ll cost_) : to(to_), cap(cap_), cost(cost_) {}\n\n#ifdef _MSC_VER\n friend ostream& operator<<(ostream& os, const Edge& e) {\n os << \"(to:\" << e.to << \", cap:\" << e.cap << \", cost:\" << e.cost << \")\";\n return os;\n }\n#endif\n };\n\n // 元のグラフ(負辺あり)\n vector<vector<Edge>> g;\n\n // pot[s] : 頂点 s のポテンシャル\n vl pot;\n\n // n 頂点で初期化する.\n Negative_mcf_graph_DAG(int n_) : n(n_), g(n), pot(n, INFL) {}\n\n // s から t へ容量 cap,コスト cost の辺を追加する.\n void add_edge(int s, int t, ll cap, ll cost) {\n // verify : https://onlinejudge.u-aizu.ac.jp/problems/2266\n\n g[s].emplace_back(t, cap, cost);\n }\n\n // DAG 上の DP で GL までの距離を求め,その -1 倍をポテンシャルとする.\n void DAG_DP(int GL) {\n pot[GL] = 0;\n\n vb seen(n);\n seen[GL] = true;\n\n function<ll(int)> dfs = [&](int s) {\n if (seen[s]) return pot[s];\n seen[s] = true;\n\n repe(e, g[s]) chmin(pot[s], dfs(e.to) + e.cost);\n\n return pot[s];\n };\n\n // 各頂点 s についての情報を計算する.\n rep(s, n) if (!seen[s]) dfs(s);\n\n rep(s, n) pot[s] *= -1;\n }\n\n // ST から GL まで flow_limit まで流せるだけ流したときの {流量, 最小コスト} を返す.\n pll flow(int ST, int GL, ll f_lim = INFL) {\n // verify : https://onlinejudge.u-aizu.ac.jp/problems/2266\n\n // DAG 上の DP で v から GL までの距離を求め,その -1 倍をポテンシャル pot[v] とする.\n DAG_DP(GL);\n\n // g_pos : 辺 s→t のコストが (元の辺のコスト) - (pot[t] - pot[s]) >= 0 であるようなグラフ\n mcf_graph<ll, ll> g_pos(n);\n rep(s, n) repe(e, g[s]) {\n g_pos.add_edge(s, e.to, e.cap, e.cost - (pot[e.to] - pot[s]));\n }\n\n // g_pos の最小費用流を求める.\n ll cap, cost;\n tie(cap, cost) = g_pos.flow(ST, GL, f_lim);\n\n // 実際のコストは (流量) * (pot[GL] - pot[ST]) を加えたものになる.\n cost += cap * (pot[GL] - pot[ST]);\n\n return make_pair(cap, cost);\n }\n};\n\n\nint main() {\n//\tinput_from_file(\"input.txt\");\n//\toutput_to_file(\"output.txt\");\n\t\n\tint m, n, k;\n\tcin >> m >> n >> k;\n\n\tvi w(n), a(k);\n\tcin >> w >> a;\n --a;\n\n ll w_sum = 0;\n rep(i, k) w_sum += w[a[i]];\n\n vi nxt;\n next_equal(a, nxt);\n\n Negative_mcf_graph g(2 * k);\n\n rep(i, k) {\n g.add_edge(2 * i, 2 * i + 1, 1, -INF);\n g.add_edge(2 * i, 2 * i + 1, m, 0);\n\n if (i < k - 1) {\n g.add_edge(2 * i + 1, 2 * (i + 1), m, 0);\n }\n\n if (nxt[i] != k) {\n g.add_edge(2 * i + 1, 2 * nxt[i], 1, -w[a[i]]);\n }\n }\n dumpel(g.g);\n\n ll cap, cost;\n tie(cap, cost) = g.flow(2 * 0, 2 * (k - 1) + 1, m);\n dump(cap, cost);\n\n ll res = w_sum + cost + (ll)k * INF;\n\n cout << res << endl;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 11104, "score_of_the_acc": -0.1281, "final_rank": 4 }, { "submission_id": "aoj_2266_7071593", "code_snippet": "#ifndef HIDDEN_IN_VS // 折りたたみ用\n\n// 警告の抑制\n#define _CRT_SECURE_NO_WARNINGS\n\n// ライブラリの読み込み\n#include <bits/stdc++.h>\nusing namespace std;\n\n// 型名の短縮\nusing ll = long long; // -2^63 ~ 2^63 = 9 * 10^18(int は -2^31 ~ 2^31 = 2 * 10^9)\nusing pii = pair<int, int>;\tusing pll = pair<ll, ll>;\tusing pil = pair<int, ll>;\tusing pli = pair<ll, int>;\nusing vi = vector<int>;\t\tusing vvi = vector<vi>;\t\tusing vvvi = vector<vvi>;\nusing vl = vector<ll>;\t\tusing vvl = vector<vl>;\t\tusing vvvl = vector<vvl>;\nusing vb = vector<bool>;\tusing vvb = vector<vb>;\t\tusing vvvb = vector<vvb>;\nusing vc = vector<char>;\tusing vvc = vector<vc>;\t\tusing vvvc = vector<vvc>;\nusing vd = vector<double>;\tusing vvd = vector<vd>;\t\tusing vvvd = vector<vvd>;\ntemplate <class T> using priority_queue_rev = priority_queue<T, vector<T>, greater<T>>;\nusing Graph = vvi;\n\n// 定数の定義\nconst double PI = acos(-1);\nconst vi DX = { 1, 0, -1, 0 }; // 4 近傍(下,右,上,左)\nconst vi DY = { 0, 1, 0, -1 };\nint INF = 1001001001; ll INFL = 4004004004004004004LL;\ndouble EPS = 1e-12;\n\n// 入出力高速化\nstruct fast_io { fast_io() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(18); } } fastIOtmp;\n\n// 汎用マクロの定義\n#define all(a) (a).begin(), (a).end()\n#define sz(x) ((int)(x).size())\n#define lbpos(a, x) (int)distance((a).begin(), std::lower_bound(all(a), x))\n#define ubpos(a, x) (int)distance((a).begin(), std::upper_bound(all(a), x))\n#define Yes(b) {cout << ((b) ? \"Yes\\n\" : \"No\\n\");}\n#define rep(i, n) for(int i = 0, i##_len = int(n); i < i##_len; ++i) // 0 から n-1 まで昇順\n#define repi(i, s, t) for(int i = int(s), i##_end = int(t); i <= i##_end; ++i) // s から t まで昇順\n#define repir(i, s, t) for(int i = int(s), i##_end = int(t); i >= i##_end; --i) // s から t まで降順\n#define repe(v, a) for(const auto& v : (a)) // a の全要素(変更不可能)\n#define repea(v, a) for(auto& v : (a)) // a の全要素(変更可能)\n#define repb(set, d) for(int set = 0; set < (1 << int(d)); ++set) // d ビット全探索(昇順)\n#define repp(a) sort(all(a)); for(bool a##_perm = true; a##_perm; a##_perm = next_permutation(all(a))) // a の順列全て(昇順)\n#define smod(n, m) ((((n) % (m)) + (m)) % (m)) // 非負mod\n#define uniq(a) {sort(all(a)); (a).erase(unique(all(a)), (a).end());} // 重複除去\n#define EXIT(a) {cout << (a) << endl; exit(0);} // 強制終了\n\n// 汎用関数の定義\ntemplate <class T> inline ll pow(T n, int k) { ll v = 1; rep(i, k) v *= n; return v; }\ntemplate <class T> inline bool chmax(T& M, const T& x) { if (M < x) { M = x; return true; } return false; } // 最大値を更新(更新されたら true を返す)\ntemplate <class T> inline bool chmin(T& m, const T& x) { if (m > x) { m = x; return true; } return false; } // 最小値を更新(更新されたら true を返す)\n\n// 演算子オーバーロード\ntemplate <class T, class U> inline istream& operator>>(istream& is, pair<T, U>& p) { is >> p.first >> p.second; return is; }\ntemplate <class T> inline istream& operator>>(istream& is, vector<T>& v) { repea(x, v) is >> x; return is; }\ntemplate <class T> inline vector<T>& operator--(vector<T>& v) { repea(x, v) --x; return v; }\ntemplate <class T> inline vector<T>& operator++(vector<T>& v) { repea(x, v) ++x; return v; }\n\n// 手元環境(Visual Studio)\n#ifdef _MSC_VER\n#include \"local.hpp\"\n// 提出用(gcc)\n#else\ninline int popcount(int n) { return __builtin_popcount(n); }\ninline int popcount(ll n) { return __builtin_popcountll(n); }\ninline int lsb(int n) { return n != 0 ? __builtin_ctz(n) : -1; }\ninline int lsb(ll n) { return n != 0 ? __builtin_ctzll(n) : -1; }\ninline int msb(int n) { return n != 0 ? (31 - __builtin_clz(n)) : -1; }\ninline int msb(ll n) { return n != 0 ? (63 - __builtin_clzll(n)) : -1; }\n#define gcd __gcd\n#define dump(...)\n#define dumpel(v)\n#define dump_list(v)\n#define input_from_file(f)\n#define output_to_file(f)\n#define Assert(b) { if (!(b)) while (1) cout << \"OLE\"; }\n#endif\n\n#endif // 折りたたみ用\n\n\n////--------------AtCoder 専用--------------\n//#include <atcoder/all>\n//using namespace atcoder;\n//\n////using mint = modint1000000007;\n//using mint = modint998244353;\n////using mint = modint; // mint::set_mod(m);\n//\n//istream& operator>>(istream& is, mint& x) { ll x_; is >> x_; x = x_; return is; }\n//ostream& operator<<(ostream& os, const mint& x) { os << x.val(); return os; }\n//using vm = vector<mint>; using vvm = vector<vm>; using vvvm = vector<vvm>;\n////----------------------------------------\n\n\n#ifndef ATCODER_INTERNAL_CSR_HPP\n#define ATCODER_INTERNAL_CSR_HPP 1\n\n#include <algorithm>\n#include <utility>\n#include <vector>\n\nnamespace atcoder {\n namespace internal {\n\n template <class E> struct csr {\n std::vector<int> start;\n std::vector<E> elist;\n explicit csr(int n, const std::vector<std::pair<int, E>>& edges)\n : start(n + 1), elist(edges.size()) {\n for (auto e : edges) {\n start[e.first + 1]++;\n }\n for (int i = 1; i <= n; i++) {\n start[i] += start[i - 1];\n }\n auto counter = start;\n for (auto e : edges) {\n elist[counter[e.first]++] = e.second;\n }\n }\n };\n\n } // namespace internal\n\n} // namespace atcoder\n\n#endif // ATCODER_INTERNAL_CSR_HPP\n\n\n#ifndef ATCODER_INTERNAL_QUEUE_HPP\n#define ATCODER_INTERNAL_QUEUE_HPP 1\n\n#include <vector>\n\nnamespace atcoder {\n\n namespace internal {\n\n template <class T> struct simple_queue {\n std::vector<T> payload;\n int pos = 0;\n void reserve(int n) { payload.reserve(n); }\n int size() const { return int(payload.size()) - pos; }\n bool empty() const { return pos == int(payload.size()); }\n void push(const T& t) { payload.push_back(t); }\n T& front() { return payload[pos]; }\n void clear() {\n payload.clear();\n pos = 0;\n }\n void pop() { pos++; }\n };\n\n } // namespace internal\n\n} // namespace atcoder\n\n#endif // ATCODER_INTERNAL_QUEUE_HPP\n\n\n#ifndef ATCODER_MINCOSTFLOW_HPP\n#define ATCODER_MINCOSTFLOW_HPP 1\n\n#include <algorithm>\n#include <cassert>\n#include <limits>\n#include <queue>\n#include <vector>\n\nnamespace atcoder {\n\n template <class Cap, class Cost> struct mcf_graph {\n public:\n mcf_graph() {}\n explicit mcf_graph(int n) : _n(n) {}\n\n int add_edge(int from, int to, Cap cap, Cost cost) {\n assert(0 <= from && from < _n);\n assert(0 <= to && to < _n);\n assert(0 <= cap);\n assert(0 <= cost);\n int m = int(_edges.size());\n _edges.push_back({ from, to, cap, 0, cost });\n return m;\n }\n\n struct edge {\n int from, to;\n Cap cap, flow;\n Cost cost;\n };\n\n edge get_edge(int i) {\n int m = int(_edges.size());\n assert(0 <= i && i < m);\n return _edges[i];\n }\n std::vector<edge> edges() { return _edges; }\n\n std::pair<Cap, Cost> flow(int s, int t) {\n return flow(s, t, std::numeric_limits<Cap>::max());\n }\n std::pair<Cap, Cost> flow(int s, int t, Cap flow_limit) {\n return slope(s, t, flow_limit).back();\n }\n std::vector<std::pair<Cap, Cost>> slope(int s, int t) {\n return slope(s, t, std::numeric_limits<Cap>::max());\n }\n std::vector<std::pair<Cap, Cost>> slope(int s, int t, Cap flow_limit) {\n assert(0 <= s && s < _n);\n assert(0 <= t && t < _n);\n assert(s != t);\n\n int m = int(_edges.size());\n std::vector<int> edge_idx(m);\n\n auto g = [&]() {\n std::vector<int> degree(_n), redge_idx(m);\n std::vector<std::pair<int, _edge>> elist;\n elist.reserve(2 * m);\n for (int i = 0; i < m; i++) {\n auto e = _edges[i];\n edge_idx[i] = degree[e.from]++;\n redge_idx[i] = degree[e.to]++;\n elist.push_back({ e.from, {e.to, -1, e.cap - e.flow, e.cost} });\n elist.push_back({ e.to, {e.from, -1, e.flow, -e.cost} });\n }\n auto _g = internal::csr<_edge>(_n, elist);\n for (int i = 0; i < m; i++) {\n auto e = _edges[i];\n edge_idx[i] += _g.start[e.from];\n redge_idx[i] += _g.start[e.to];\n _g.elist[edge_idx[i]].rev = redge_idx[i];\n _g.elist[redge_idx[i]].rev = edge_idx[i];\n }\n return _g;\n }();\n\n auto result = slope(g, s, t, flow_limit);\n\n for (int i = 0; i < m; i++) {\n auto e = g.elist[edge_idx[i]];\n _edges[i].flow = _edges[i].cap - e.cap;\n }\n\n return result;\n }\n\n private:\n int _n;\n std::vector<edge> _edges;\n\n // inside edge\n struct _edge {\n int to, rev;\n Cap cap;\n Cost cost;\n };\n\n std::vector<std::pair<Cap, Cost>> slope(internal::csr<_edge>& g,\n int s,\n int t,\n Cap flow_limit) {\n // variants (C = maxcost):\n // -(n-1)C <= dual[s] <= dual[i] <= dual[t] = 0\n // reduced cost (= e.cost + dual[e.from] - dual[e.to]) >= 0 for all edge\n\n // dual_dist[i] = (dual[i], dist[i])\n std::vector<std::pair<Cost, Cost>> dual_dist(_n);\n std::vector<int> prev_e(_n);\n std::vector<bool> vis(_n);\n struct Q {\n Cost key;\n int to;\n bool operator<(Q r) const { return key > r.key; }\n };\n std::vector<int> que_min;\n std::vector<Q> que;\n auto dual_ref = [&]() {\n for (int i = 0; i < _n; i++) {\n dual_dist[i].second = std::numeric_limits<Cost>::max();\n }\n std::fill(vis.begin(), vis.end(), false);\n que_min.clear();\n que.clear();\n\n // que[0..heap_r) was heapified\n size_t heap_r = 0;\n\n dual_dist[s].second = 0;\n que_min.push_back(s);\n while (!que_min.empty() || !que.empty()) {\n int v;\n if (!que_min.empty()) {\n v = que_min.back();\n que_min.pop_back();\n }\n else {\n while (heap_r < que.size()) {\n heap_r++;\n std::push_heap(que.begin(), que.begin() + heap_r);\n }\n v = que.front().to;\n std::pop_heap(que.begin(), que.end());\n que.pop_back();\n heap_r--;\n }\n if (vis[v]) continue;\n vis[v] = true;\n if (v == t) break;\n // dist[v] = shortest(s, v) + dual[s] - dual[v]\n // dist[v] >= 0 (all reduced cost are positive)\n // dist[v] <= (n-1)C\n Cost dual_v = dual_dist[v].first, dist_v = dual_dist[v].second;\n for (int i = g.start[v]; i < g.start[v + 1]; i++) {\n auto e = g.elist[i];\n if (!e.cap) continue;\n // |-dual[e.to] + dual[v]| <= (n-1)C\n // cost <= C - -(n-1)C + 0 = nC\n Cost cost = e.cost - dual_dist[e.to].first + dual_v;\n if (dual_dist[e.to].second - dist_v > cost) {\n Cost dist_to = dist_v + cost;\n dual_dist[e.to].second = dist_to;\n prev_e[e.to] = e.rev;\n if (dist_to == dist_v) {\n que_min.push_back(e.to);\n }\n else {\n que.push_back(Q{ dist_to, e.to });\n }\n }\n }\n }\n if (!vis[t]) {\n return false;\n }\n\n for (int v = 0; v < _n; v++) {\n if (!vis[v]) continue;\n // dual[v] = dual[v] - dist[t] + dist[v]\n // = dual[v] - (shortest(s, t) + dual[s] - dual[t]) +\n // (shortest(s, v) + dual[s] - dual[v]) = - shortest(s,\n // t) + dual[t] + shortest(s, v) = shortest(s, v) -\n // shortest(s, t) >= 0 - (n-1)C\n dual_dist[v].first -= dual_dist[t].second - dual_dist[v].second;\n }\n return true;\n };\n Cap flow = 0;\n Cost cost = 0, prev_cost_per_flow = -1;\n std::vector<std::pair<Cap, Cost>> result = { {Cap(0), Cost(0)} };\n while (flow < flow_limit) {\n if (!dual_ref()) break;\n Cap c = flow_limit - flow;\n for (int v = t; v != s; v = g.elist[prev_e[v]].to) {\n c = std::min(c, g.elist[g.elist[prev_e[v]].rev].cap);\n }\n for (int v = t; v != s; v = g.elist[prev_e[v]].to) {\n auto& e = g.elist[prev_e[v]];\n e.cap += c;\n g.elist[e.rev].cap -= c;\n }\n Cost d = -dual_dist[s].first;\n flow += c;\n cost += c * d;\n if (prev_cost_per_flow == d) {\n result.pop_back();\n }\n result.push_back({ flow, cost });\n prev_cost_per_flow = d;\n }\n return result;\n }\n };\n\n} // namespace atcoder\n\n#endif // ATCODER_MINCOSTFLOW_HPP\n\n\nusing namespace atcoder;\n\n\n//【最小費用流(負コスト可,負コスト閉路なし)】\n/*\n* Negative_mcf_graph(int n) : O(1)\n*\tn 頂点で初期化する.\n*\n* add_edge(int s, int t, ll cap, ll cost) : O(1)\n*\ts から t へ容量 cap,コスト cost の辺を追加する.\n*\n* pll flow(int ST, int GL, ll f_lim = INFL) : O(F (n + m) log n)(F:流量,m:辺の数)\n*\tST から GL まで flow_limit まで流せるだけ流したときの {流量, 最小コスト} を返す.\n*\t制約:負コストの閉路は存在しない\n*/\nstruct Negative_mcf_graph {\n\t// 参考 : https://ikatakos.com/pot/programming_algorithm/graph_theory/minimum_cost_flow\n\n\t// n : 頂点数\n\tint n;\n\n\t// 辺\n\tstruct Edge {\n\t\tint to;\n\t\tll cap, cost;\n\n\t\tEdge(int to_, ll cap_, ll cost_) : to(to_), cap(cap_), cost(cost_) {}\n\n#ifdef _MSC_VER\n\t\tfriend ostream& operator<<(ostream& os, const Edge& e) {\n\t\t\tos << \"(to:\" << e.to << \", cap:\" << e.cap << \", cost:\" << e.cost << \")\";\n\t\t\treturn os;\n\t\t}\n#endif\n\t};\n\n\t// 元のグラフ(負辺あり)\n\tvector<vector<Edge>> g;\n\n\t// pot[s] : 頂点 s のポテンシャル\n\tvl pot;\n\n\t// n 頂点で初期化する.\n\tNegative_mcf_graph(int n_) : n(n_), g(n), pot(n, INFL) {}\n\n\t// s から t へ容量 cap,コスト cost の辺を追加する.\n\tvoid add_edge(int s, int t, ll cap, ll cost) {\n\t\t// verify : https://atcoder.jp/contests/code-festival-2014-china-open/tasks/code_festival_china_h\n\n\t\tg[s].emplace_back(t, cap, cost);\n\t}\n\n\t// ベルマンフォード法で ST からの距離を求め,それをポテンシャルとする.\n\tbool bellman_ford(int ST) {\n\t\tpot[ST] = 0;\n\n\t\trep(i, n) {\n\t\t\tbool updated = false;\n\n\t\t\t// 全ての辺についての操作\n\t\t\trep(s, n) repe(e, g[s]) {\n\t\t\t\t// もし (始点への距離) + (辺のコスト) < (終点への距離) なら (終点への距離) を更新する.\n\t\t\t\t// INFL からは何を引いても INFL になるようにしているので,ST から到達可能な負閉路しか検出しない.\n\t\t\t\tif (pot[s] != INFL && pot[s] + e.cost < pot[e.to]) {\n\t\t\t\t\tpot[e.to] = pot[s] + e.cost;\n\t\t\t\t\tupdated = true;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// もし距離の更新が起こらなければ最短距離確定\n\t\t\tif (!updated) return true;\n\t\t}\n\n\t\t// もし全ての辺についての操作を n 回繰り返しても距離の更新があったなら,\n\t\t// ST から到達可能な負の閉路を持っているので false を返す.\n\t\treturn false;\n\t}\n\n\t// ST から GL まで flow_limit まで流せるだけ流したときの {流量, 最小コスト} を返す.\n\tpll flow(int ST, int GL, ll f_lim = INFL) {\n\t\t// verify : https://atcoder.jp/contests/code-festival-2014-china-open/tasks/code_festival_china_h\n\n\t\t// ベルマンフォード法で ST から v までの距離を求め,それをポテンシャル pot[v] とする.\n\t\tbool non_neg_cyc = bellman_ford(ST);\n\t\tAssert(non_neg_cyc);\n\n\t\t// g_pos : 辺 s→t のコストが (元の辺のコスト) - (pot[t] - pot[s]) >= 0 であるようなグラフ\n\t\tmcf_graph<ll, ll> g_pos(n);\n\t\trep(s, n) repe(e, g[s]) {\n\t\t\tg_pos.add_edge(s, e.to, e.cap, e.cost - (pot[e.to] - pot[s]));\n\t\t}\n\n\t\t// g_pos の最小費用流を求める.\n\t\tll cap, cost;\n\t\ttie(cap, cost) = g_pos.flow(ST, GL, f_lim);\n\n\t\t// 実際のコストは (流量) * pot[GL] を加えたものになる.\n\t\tcost += cap * pot[GL];\n\n\t\treturn make_pair(cap, cost);\n\t}\n};\n\n\nll MLE(int m, int n, int k, vi w, vi a) {\n --a;\n\n vvi nxt(k + 1, vi(n, k));\n repir(i, k - 1, 0) {\n rep(c, n) {\n nxt[i][c] = nxt[i + 1][c];\n }\n nxt[i][a[i]] = i;\n }\n dumpel(nxt);\n\n int ST = 2 * k, GL = ST + 1;\n Negative_mcf_graph g(GL + 1);\n\n rep(i, n) {\n if (nxt[0][i] < k) {\n g.add_edge(ST, 2 * nxt[0][i], 1, w[i]);\n }\n else {\n g.add_edge(ST, GL, 1, 0);\n }\n }\n\n rep(t, k) {\n g.add_edge(2 * t, 2 * t + 1, 1, -INF);\n\n rep(i, n) {\n if (a[t] == i) {\n if (nxt[t + 1][i] < k) {\n g.add_edge(2 * t + 1, 2 * nxt[t + 1][i], 1, 0);\n }\n else {\n g.add_edge(2 * t + 1, GL, 1, 0);\n }\n }\n else {\n if (nxt[t + 1][i] < k) {\n g.add_edge(2 * t + 1, 2 * nxt[t + 1][i], 1, w[i]);\n }\n }\n }\n }\n\n dumpel(g.g);\n\n ll cap, cost;\n tie(cap, cost) = g.flow(ST, GL, m);\n dump(cap, cost);\n\n ll res = cost + (ll)INF * k;\n\n return res;\n}\n\n\n//【自身と同じ数の次の位置】O(n)\n/*\n* a[0..n) の各要素 a[i] について,j > i かつ a[j] = a[i] となる最小の j を\n* nxt[j] に格納する.(存在しなければ n)\n*/\ntemplate <class T> void next_equal(const vector<T>& a, vi& nxt) {\n // verify : https://atcoder.jp/contests/agc036/tasks/agc036_b\n\n int n = sz(a);\n\n // nxt[i] : j > i かつ a[j] = a[i] となる最小の j(なければ n)\n nxt = vi(n, n);\n\n // num_to_pos[x] : 値 x が最後に現れた位置(右から走査する)\n unordered_map<T, int> num_to_pos;\n\n repir(i, n - 1, 0) {\n if (num_to_pos.count(a[i])) {\n nxt[i] = num_to_pos[a[i]];\n }\n num_to_pos[a[i]] = i;\n }\n}\n\n\n//【最小費用流(負コスト可,DAG)】\n/*\n* Negative_mcf_graph(int n) : O(1)\n*\tn 頂点で初期化する.\n*\n* add_edge(int s, int t, ll cap, ll cost) : O(1)\n*\ts から t へ容量 cap,コスト cost の辺を追加する.\n*\n* pll flow(int ST, int GL, ll f_lim = INFL) : O(F (n + m) log n)(F:流量,m:辺の数)\n*\tST から GL まで flow_limit まで流せるだけ流したときの {流量, 最小コスト} を返す.\n*\t制約:閉路は存在しない\n*/\nstruct Negative_mcf_graph_DAG {\n // 参考 : https://ikatakos.com/pot/programming_algorithm/graph_theory/minimum_cost_flow\n\n // n : 頂点数\n int n;\n\n // 辺\n struct Edge {\n int to;\n ll cap, cost;\n\n Edge(int to_, ll cap_, ll cost_) : to(to_), cap(cap_), cost(cost_) {}\n\n#ifdef _MSC_VER\n friend ostream& operator<<(ostream& os, const Edge& e) {\n os << \"(to:\" << e.to << \", cap:\" << e.cap << \", cost:\" << e.cost << \")\";\n return os;\n }\n#endif\n };\n\n // 元のグラフ(負辺あり)\n vector<vector<Edge>> g;\n\n // pot[s] : 頂点 s のポテンシャル\n vl pot;\n\n // n 頂点で初期化する.\n Negative_mcf_graph_DAG(int n_) : n(n_), g(n), pot(n, INFL) {}\n\n // s から t へ容量 cap,コスト cost の辺を追加する.\n void add_edge(int s, int t, ll cap, ll cost) {\n // verify : https://atcoder.jp/contests/code-festival-2014-china-open/tasks/code_festival_china_h\n\n g[s].emplace_back(t, cap, cost);\n }\n\n // DAG 上の DP で GL までの距離を求め,その -1 倍をポテンシャルとする.\n void DAG_DP(int GL) {\n pot[GL] = 0;\n\n vb seen(n);\n seen[GL] = true;\n\n function<ll(int)> dfs = [&](int s) {\n if (seen[s]) return pot[s];\n seen[s] = true;\n\n repe(e, g[s]) chmin(pot[s], dfs(e.to) + e.cost);\n\n return pot[s];\n };\n\n // 各頂点 s についての情報を計算する.\n rep(s, n) if (!seen[s]) dfs(s);\n\n rep(s, n) pot[s] *= -1;\n }\n\n // ST から GL まで flow_limit まで流せるだけ流したときの {流量, 最小コスト} を返す.\n pll flow(int ST, int GL, ll f_lim = INFL) {\n // verify : https://atcoder.jp/contests/code-festival-2014-china-open/tasks/code_festival_china_h\n\n // DAG 上の DP で v から GL までの距離を求め,その -1 倍をポテンシャル pot[v] とする.\n DAG_DP(GL);\n\n // g_pos : 辺 s→t のコストが (元の辺のコスト) - (pot[t] - pot[s]) >= 0 であるようなグラフ\n mcf_graph<ll, ll> g_pos(n);\n rep(s, n) repe(e, g[s]) {\n g_pos.add_edge(s, e.to, e.cap, e.cost - (pot[e.to] - pot[s]));\n }\n\n // g_pos の最小費用流を求める.\n ll cap, cost;\n tie(cap, cost) = g_pos.flow(ST, GL, f_lim);\n\n // 実際のコストは (流量) * (pot[GL] - pot[ST]) を加えたものになる.\n cost += cap * (pot[GL] - pot[ST]);\n\n return make_pair(cap, cost);\n }\n};\n\n\nint main() {\n//\tinput_from_file(\"input.txt\");\n//\toutput_to_file(\"output.txt\");\n\t\n\tint m, n, k;\n\tcin >> m >> n >> k;\n\n\tvi w(n), a(k);\n\tcin >> w >> a;\n --a;\n\n ll w_sum = 0;\n rep(i, k) w_sum += w[a[i]];\n\n vi nxt;\n next_equal(a, nxt);\n\n Negative_mcf_graph_DAG g(2 * k);\n\n rep(i, k) {\n g.add_edge(2 * i, 2 * i + 1, 1, -INF);\n g.add_edge(2 * i, 2 * i + 1, m, 0);\n\n if (i < k - 1) {\n g.add_edge(2 * i + 1, 2 * (i + 1), m, 0);\n }\n\n if (nxt[i] != k) {\n g.add_edge(2 * i + 1, 2 * nxt[i], 1, -w[a[i]]);\n }\n }\n dumpel(g.g);\n\n ll cap, cost;\n tie(cap, cost) = g.flow(2 * 0, 2 * (k - 1) + 1, m);\n dump(cap, cost);\n\n ll res = w_sum + cost + (ll)k * INF;\n\n cout << res << endl;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 12424, "score_of_the_acc": -0.1522, "final_rank": 5 }, { "submission_id": "aoj_2266_6918406", "code_snippet": "#define _CRT_SECURE_NO_WARNINGS\n#include <cstdio>\n#include <iostream>\n#include <stack>\n#include <queue>\n#include <algorithm>\n#include <functional>\n#include <set>\n#include <map>\n#include <string>\n#include <vector>\n#include <cmath>\n#include<sstream>\n#include<list>\n#include<iomanip>\n#include <cstdlib>\n#include <cstring>\n#include <stack>\n#include <bitset>\n#include <cassert>\n#include <stdlib.h>\n#include <stdio.h>\nusing namespace std;\nconst int INF = 100000000;\nconst long long LINF = 3e18 + 7;\nconst int MAX_N = 2000010;\nconst int MAX_W = 10002;\nconst int MAX_ARRAYK = 100000;\ndouble PI = 3.14159265358979323846;\n//using ll = long long;\n\n// Minimum Cost Flow (Dijkstra)\n// https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_6_B&lang=ja\n// 蟻本 p. 203\n// 始点0, 終点V-1\n\n\n\nint V, E, F;\nstruct edge { int to, cap, cost, rev; };\ntypedef pair<int, int> P;\n\nvector<edge> G[MAX_N];\nint h[MAX_N];\nint dist[MAX_N];\nint prevv[MAX_N], preve[MAX_N];\n\n// from からtoへ向かう容量cap, コストcostの辺をグラフに追加する\nvoid add_edge(int from, int to, int cap, int cost) {\n\tedge e1 = { to, cap, cost, G[to].size() };\n\tG[from].push_back(e1);\n\tedge e2 = { from, 0, -cost, G[from].size() - 1 };\n\tG[to].push_back(e2);\n\n}\n\n// sからtへの流量f最小費用流を求める\n// 流せない場合は-1を返す\nint min_cost_flow(int s, int t, int f) {\n\tint res = 0;\n\twhile (f > 0) {\n\t\t//ベルマンフォードにより、s-t間最短路を求める\n\t\tfill(dist, dist + V, INF);\n\t\tdist[s] = 0;\n\t\tbool update = true;\n\t\twhile (update) {\n\t\t\tupdate = false;\n\t\t\tfor (int v = 0; v < V; v++) {\n\t\t\t\tif (dist[v] == INF) {\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tfor (int i = 0; i < G[v].size(); i++) {\n\t\t\t\t\tedge& e = G[v][i];\n\t\t\t\t\tif (e.cap > 0 && dist[e.to] > dist[v] + e.cost) {\n\t\t\t\t\t\tdist[e.to] = dist[v] + e.cost;\n\t\t\t\t\t\tprevv[e.to] = v;\n\t\t\t\t\t\tpreve[e.to] = i;\n\t\t\t\t\t\tupdate = true;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif (dist[t] == INF) {\n\t\t\treturn -1;\n\t\t}\n\n\t\t// s-t間最短路に沿って目一杯流す。\n\t\tint d = f;\n\t\tfor (int v = t; v != s; v = prevv[v]) {\n\t\t\td = min(d, G[prevv[v]][preve[v]].cap);\n\t\t}\n\n\t\tf -= d;\n\t\tres += (d * dist[t]);\n\t\tfor (int v = t; v != s; v = prevv[v]) {\n\t\t\tedge& e = G[prevv[v]][preve[v]];\n\t\t\te.cap -= d;\n\t\t\tG[v][e.rev].cap += d;\n\t\t}\n\t}\n\n\treturn res;\n\n}\nint M, N, K;\nint w[MAX_N];\nint a[MAX_N];\nint p[MAX_N];\n\n\nint main() {\n\n\tcin >> M >> N >> K;\n\n\tfor (int i = 0; i < N; i++) {\n\t\tcin >> w[i];\n\t}\n\tfor (int i = 0; i < K; i++) {\n\t\tcin >> a[i];\n\t\ta[i]--;\n\t}\n\n\tvector<int> b;\n\tb.push_back(a[0]);\n\n\tfor (int i = 1; i < K; i++) {\n\t\tif (a[i] != b[b.size() - 1]) {\n\t\t\tb.push_back(a[i]);\n\t\t}\n\t}\n\n\tV = b.size();\n\n\tint S = 0;\n\tfor (int i = 0; i < b.size(); i++) {\n\t\tS += w[b[i]];\n\t}\n\n\tfor (int i = 0; i < N; i++) {\n\t\tp[i] = -1;\n\t}\n\n\tfor (int i = 1; i < b.size(); i++) {\n\t\tadd_edge(i - 1, i, INF, 0);\n\t}\n\n\tfor (int i = 0; i < b.size(); i++) {\n\t\tif (p[b[i]] == -1) {\n\t\t\tp[b[i]] = i;\n\t\t\tcontinue;\n\t\t}\n\t\t\n\t\tint id = p[b[i]];\n\t\tadd_edge(id + 1, i, 1, -w[b[i]]);\n\t\tp[b[i]] = i;\n\t}\n\n\tcout << S + min_cost_flow(0, V - 1, M - 1) << endl;\n\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 610, "memory_kb": 51092, "score_of_the_acc": -1.3306, "final_rank": 10 }, { "submission_id": "aoj_2266_6918388", "code_snippet": "#define _CRT_SECURE_NO_WARNINGS\n#include <cstdio>\n#include <iostream>\n#include <stack>\n#include <queue>\n#include <algorithm>\n#include <functional>\n#include <set>\n#include <map>\n#include <string>\n#include <vector>\n#include <cmath>\n#include<sstream>\n#include<list>\n#include<iomanip>\n#include <cstdlib>\n#include <cstring>\n#include <stack>\n#include <bitset>\n#include <cassert>\n#include <stdlib.h>\n#include <stdio.h>\nusing namespace std;\nconst int INF = 100000000;\nconst long long LINF = 3e18 + 7;\nconst int MAX_N = 2000010;\nconst int MAX_W = 10002;\nconst int MAX_ARRAYK = 100000;\ndouble PI = 3.14159265358979323846;\n//using ll = long long;\n\n// Minimum Cost Flow (Dijkstra)\n// https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_6_B&lang=ja\n// 蟻本 p. 203\n// 始点0, 終点V-1\n\nint V, E, F;\nstruct edge { int to, cap, cost, rev; };\ntypedef pair<int, int> P;\n\nvector<edge> G[MAX_N];\nint h[MAX_N];\nint dist[MAX_N];\nint prevv[MAX_N], preve[MAX_N];\n\n// from からtoへ向かう容量cap, コストcostの辺をグラフに追加する\nvoid add_edge(int from, int to, int cap, int cost) {\n\tedge e1 = { to, cap, cost, G[to].size() };\n\tG[from].push_back(e1);\n\tedge e2 = { from, 0, -cost, G[from].size() - 1 };\n\tG[to].push_back(e2);\n}\n\n// sからtへの流量f最小費用流を求める\n// 流せない場合は-1を返す\nint min_cost_flow(int s, int t, int f) {\n\tint res = 0;\n\tfill(h, h + V, 0);\n\twhile (f > 0) {\n\t\t// ダイクストラ法を用いてhを更新する\n\t\tpriority_queue<P, vector<P>, greater<P> > que;\n\t\tfill(dist, dist + V, INF);\n\t\tdist[s] = 0;\n\t\tque.push(P(0, s));\n\t\twhile (!que.empty()) {\n\t\t\tP p = que.top(); que.pop();\n\t\t\tint v = p.second;\n\t\t\tif (dist[v] < p.first) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tfor (int i = 0; i < G[v].size(); i++) {\n\t\t\t\tedge& e = G[v][i];\n\t\t\t\tif (e.cap > 0 && dist[e.to] > dist[v] + e.cost + h[v] - h[e.to]) {\n\t\t\t\t\tdist[e.to] = dist[v] + e.cost + h[v] - h[e.to];\n\t\t\t\t\tprevv[e.to] = v;\n\t\t\t\t\tpreve[e.to] = i;\n\t\t\t\t\tque.push(P(dist[e.to], e.to));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif (dist[t] == INF) {\n\t\t\treturn -1;\n\t\t}\n\t\tfor (int v = 0; v < V; v++) {\n\t\t\th[v] += dist[v];\n\t\t}\n\n\t\t// s-t間最短路に沿って目一杯流す\n\t\tint d = f;\n\t\tfor (int v = t; v != s; v = prevv[v]) {\n\t\t\td = min(d, G[prevv[v]][preve[v]].cap);\n\t\t}\n\t\tf -= d;\n\t\tres += (d * h[t]);\n\t\tfor (int v = t; v != s; v = prevv[v]) {\n\t\t\tedge& e = G[prevv[v]][preve[v]];\n\t\t\te.cap -= d;\n\t\t\tG[v][e.rev].cap += d;\n\t\t}\n\t}\n\n\treturn res;\n\n}\n\n\nint M, N, K;\nint w[MAX_N];\nint a[MAX_N];\nint p[MAX_N];\n\n\nint main() {\n\n\tcin >> M >> N >> K;\n\n\tfor (int i = 0; i < N; i++) {\n\t\tcin >> w[i];\n\t}\n\tfor (int i = 0; i < K; i++) {\n\t\tcin >> a[i];\n\t\ta[i]--;\n\t}\n\n\tvector<int> b;\n\tb.push_back(a[0]);\n\n\tfor (int i = 1; i < K; i++) {\n\t\tif (a[i] != b[b.size() - 1]) {\n\t\t\tb.push_back(a[i]);\n\t\t}\n\t}\n\n\tV = b.size();\n\n\tint S = 0;\n\tfor (int i = 0; i < b.size(); i++) {\n\t\tS += w[b[i]];\n\t}\n\n\tfor (int i = 0; i < N; i++) {\n\t\tp[i] = -1;\n\t}\n\n\tfor (int i = 1; i < b.size(); i++) {\n\t\tadd_edge(i - 1, i, INF, 0);\n\t}\n\n\tfor (int i = 0; i < b.size(); i++) {\n\t\tif (p[b[i]] == -1) {\n\t\t\tp[b[i]] = i;\n\t\t\tcontinue;\n\t\t}\n\t\t\n\t\tint id = p[b[i]];\n\t\tadd_edge(id + 1, i, 1, -w[b[i]]);\n\t}\n\n\tcout << S + min_cost_flow(0, V - 1, M - 1) << endl;\n\n\n\treturn 0;\n}", "accuracy": 0.13432835820895522, "time_ms": 10, "memory_kb": 50160, "score_of_the_acc": -0.8411, "final_rank": 16 }, { "submission_id": "aoj_2266_6892304", "code_snippet": "#define _CRT_SECURE_NO_WARNINGS\n#include <cstdio>\n#include <iostream>\n#include <stack>\n#include <queue>\n#include <algorithm>\n#include <functional>\n#include <set>\n#include <map>\n#include <string>\n#include <vector>\n#include <cmath>\n#include<sstream>\n#include<list>\n#include<iomanip>\n#include <cstdlib>\n#include <cstring>\n#include <stack>\n#include <bitset>\n#include <cassert>\n#include <stdlib.h>\n#include <stdio.h>\nusing namespace std;\nconst int INF = 100000000;\nconst long long LINF = 3e18 + 7;\nconst int MAX_N = 2000010;\nconst int MAX_W = 10002;\nconst int MAX_ARRAYK = 100000;\ndouble PI = 3.14159265358979323846;\n//using ll = long long;\n\n// https://kmyk.github.io/blog/writeups/algo-etc-utpc2011-h/\n// http://www.utpc.jp/2011/slides/cache.pdf\n\n\nint V, E, F;\nstruct edge { int to, cap, cost, rev; };\ntypedef pair<int, int> P;\n\nvector<edge> G[MAX_N];\nint h[MAX_N];\nint dist[MAX_N];\nint prevv[MAX_N], preve[MAX_N];\n\n// from からtoへ向かう容量cap, コストcostの辺をグラフに追加する\nvoid add_edge(int from, int to, int cap, int cost) {\n\tedge e1 = { to, cap, cost, G[to].size() };\n\tG[from].push_back(e1);\n\tedge e2 = { from, 0, -cost, G[from].size() - 1 };\n\tG[to].push_back(e2);\n\n}\n\n// sからtへの流量f最小費用流を求める\n// 流せない場合は-1を返す\nint min_cost_flow(int s, int t, int f) {\n\tint res = 0;\n\twhile (f > 0) {\n\t\t//ベルマンフォードにより、s-t間最短路を求める\n\t\tfill(dist, dist + V, INF);\n\t\tdist[s] = 0;\n\t\tbool update = true;\n\t\twhile (update) {\n\t\t\tupdate = false;\n\t\t\tfor (int v = 0; v < V; v++) {\n\t\t\t\tif (dist[v] == INF) {\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tfor (int i = 0; i < G[v].size(); i++) {\n\t\t\t\t\tedge& e = G[v][i];\n\t\t\t\t\tif (e.cap > 0 && dist[e.to] > dist[v] + e.cost) {\n\t\t\t\t\t\tdist[e.to] = dist[v] + e.cost;\n\t\t\t\t\t\tprevv[e.to] = v;\n\t\t\t\t\t\tpreve[e.to] = i;\n\t\t\t\t\t\tupdate = true;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif (dist[t] == INF) {\n\t\t\treturn -1;\n\t\t}\n\n\t\t// s-t間最短路に沿って目一杯流す。\n\t\tint d = f;\n\t\tfor (int v = t; v != s; v = prevv[v]) {\n\t\t\td = min(d, G[prevv[v]][preve[v]].cap);\n\t\t}\n\n\t\tf -= d;\n\t\tres += (d * dist[t]);\n\t\tfor (int v = t; v != s; v = prevv[v]) {\n\t\t\tedge& e = G[prevv[v]][preve[v]];\n\t\t\te.cap -= d;\n\t\t\tG[v][e.rev].cap += d;\n\t\t}\n\t}\n\n\treturn res;\n\n}\n\nint M, N, K;\nint w[10010];\nvector<int> a;\nint p[10010];\n\nint main() {\n\n\tcin >> M >> N >> K;\n\n\n\t\n\tfor (int i = 0; i < N; i++) {\n\t\tcin >> w[i];\n\t}\n\tfor (int i = 0; i < K; i++) {\n\t\tint aa;\n\t\tcin >> aa;\n\t\taa--;\n\t\ta.push_back(aa);\n\t}\n\ta.erase(unique(a.begin(), a.end()), a.end());\n\tK = a.size();\n\tV = K;\n\tint sum1 = 0;\n\tfor (int i = 0; i < K; i++) {\n\t\tsum1 += w[a[i]];\n\t}\n\n\tint s = 0;\n\tint t = K - 1;\n\n\tfor (int i = 0; i < K - 1; i++) {\n\t\tadd_edge(i, i + 1, INF, 0);\n\t}\n\n\tfor (int i = 0; i < N; i++) {\n\t\tp[i] = -1;\n\t}\n\n\tfor (int i = 0; i < K; i++) {\n\t\tif (p[a[i]] == -1) {\n\t\t\tp[a[i]] = i;\n\t\t\tcontinue;\n\t\t}\n\n\t\tint b = p[a[i]] + 1;\n\t\tint n = i;\n\t\tint nw = -w[a[i]];\n\t\tadd_edge(b, n, 1, nw);\n\t\tp[a[i]] = i;\n\n\t}\n\n\tcout << sum1 + min_cost_flow(s, t, M - 1) << endl;\n\n\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 620, "memory_kb": 51024, "score_of_the_acc": -1.3372, "final_rank": 11 }, { "submission_id": "aoj_2266_6892302", "code_snippet": "#define _CRT_SECURE_NO_WARNINGS\n#include <cstdio>\n#include <iostream>\n#include <stack>\n#include <queue>\n#include <algorithm>\n#include <functional>\n#include <set>\n#include <map>\n#include <string>\n#include <vector>\n#include <cmath>\n#include<sstream>\n#include<list>\n#include<iomanip>\n#include <cstdlib>\n#include <cstring>\n#include <stack>\n#include <bitset>\n#include <cassert>\n#include <stdlib.h>\n#include <stdio.h>\nusing namespace std;\nconst int INF = 100000000;\nconst long long LINF = 3e18 + 7;\nconst int MAX_N = 2000010;\nconst int MAX_W = 10002;\nconst int MAX_ARRAYK = 100000;\ndouble PI = 3.14159265358979323846;\n//using ll = long long;\n\n// https://kmyk.github.io/blog/writeups/algo-etc-utpc2011-h/\n// http://www.utpc.jp/2011/slides/cache.pdf\n\n\n// Minimum Cost Flow (Dijkstra)\n// https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_6_B&lang=ja\n// 蟻本 p. 203\n// 始点0, 終点V-1\n\nint V, E, F;\nstruct edge { int to, cap, cost, rev; };\ntypedef pair<int, int> P;\n\nvector<edge> G[MAX_N];\nint h[MAX_N];\nint dist[MAX_N];\nint prevv[MAX_N], preve[MAX_N];\n\n// from からtoへ向かう容量cap, コストcostの辺をグラフに追加する\nvoid add_edge(int from, int to, int cap, int cost) {\n\tedge e1 = { to, cap, cost, G[to].size() };\n\tG[from].push_back(e1);\n\tedge e2 = { from, 0, -cost, G[from].size() - 1 };\n\tG[to].push_back(e2);\n}\n\n// sからtへの流量f最小費用流を求める\n// 流せない場合は-1を返す\nint min_cost_flow(int s, int t, int f) {\n\tint res = 0;\n\tfill(h, h + V, 0);\n\twhile (f > 0) {\n\t\t// ダイクストラ法を用いてhを更新する\n\t\tpriority_queue<P, vector<P>, greater<P> > que;\n\t\tfill(dist, dist + V, INF);\n\t\tdist[s] = 0;\n\t\tque.push(P(0, s));\n\t\twhile (!que.empty()) {\n\t\t\tP p = que.top(); que.pop();\n\t\t\tint v = p.second;\n\t\t\tif (dist[v] < p.first) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tfor (int i = 0; i < G[v].size(); i++) {\n\t\t\t\tedge& e = G[v][i];\n\t\t\t\tif (e.cap > 0 && dist[e.to] > dist[v] + e.cost + h[v] - h[e.to]) {\n\t\t\t\t\tdist[e.to] = dist[v] + e.cost + h[v] - h[e.to];\n\t\t\t\t\tprevv[e.to] = v;\n\t\t\t\t\tpreve[e.to] = i;\n\t\t\t\t\tque.push(P(dist[e.to], e.to));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif (dist[t] == INF) {\n\t\t\treturn -1;\n\t\t}\n\t\tfor (int v = 0; v < V; v++) {\n\t\t\th[v] += dist[v];\n\t\t}\n\n\t\t// s-t間最短路に沿って目一杯流す\n\t\tint d = f;\n\t\tfor (int v = t; v != s; v = prevv[v]) {\n\t\t\td = min(d, G[prevv[v]][preve[v]].cap);\n\t\t}\n\t\tf -= d;\n\t\tres += (d * h[t]);\n\t\tfor (int v = t; v != s; v = prevv[v]) {\n\t\t\tedge& e = G[prevv[v]][preve[v]];\n\t\t\te.cap -= d;\n\t\t\tG[v][e.rev].cap += d;\n\t\t}\n\t}\n\n\treturn res;\n\n}\n\nint M, N, K;\nint w[10010];\nvector<int> a;\nint p[10010];\n\nint main() {\n\n\tcin >> M >> N >> K;\n\n\n\t\n\tfor (int i = 0; i < N; i++) {\n\t\tcin >> w[i];\n\t}\n\tfor (int i = 0; i < K; i++) {\n\t\tint aa;\n\t\tcin >> aa;\n\t\taa--;\n\t\ta.push_back(aa);\n\t}\n\ta.erase(unique(a.begin(), a.end()), a.end());\n\tK = a.size();\n\tV = K;\n\tint sum1 = 0;\n\tfor (int i = 0; i < K; i++) {\n\t\tsum1 += w[a[i]];\n\t}\n\n\tint s = 0;\n\tint t = K - 1;\n\n\tfor (int i = 0; i < K - 1; i++) {\n\t\tadd_edge(i, i + 1, INF, 0);\n\t}\n\n\tfor (int i = 0; i < N; i++) {\n\t\tp[i] = -1;\n\t}\n\n\tfor (int i = 0; i < K; i++) {\n\t\tif (p[a[i]] == -1) {\n\t\t\tp[a[i]] = i;\n\t\t\tcontinue;\n\t\t}\n\n\t\tint b = p[a[i]] + 1;\n\t\tint n = i;\n\t\tint nw = -w[a[i]];\n\t\tadd_edge(b, n, 1, nw);\n\n\t}\n\n\tcout << sum1 + min_cost_flow(s, t, M - 1) << endl;\n\n\n\n\treturn 0;\n}", "accuracy": 0.13432835820895522, "time_ms": 10, "memory_kb": 50176, "score_of_the_acc": -0.8414, "final_rank": 17 }, { "submission_id": "aoj_2266_6892300", "code_snippet": "#define _CRT_SECURE_NO_WARNINGS\n#include <cstdio>\n#include <iostream>\n#include <stack>\n#include <queue>\n#include <algorithm>\n#include <functional>\n#include <set>\n#include <map>\n#include <string>\n#include <vector>\n#include <cmath>\n#include<sstream>\n#include<list>\n#include<iomanip>\n#include <cstdlib>\n#include <cstring>\n#include <stack>\n#include <bitset>\n#include <cassert>\n#include <stdlib.h>\n#include <stdio.h>\nusing namespace std;\nconst int INF = 100000000;\nconst long long LINF = 3e18 + 7;\nconst int MAX_N = 2000010;\nconst int MAX_W = 10002;\nconst int MAX_ARRAYK = 100000;\ndouble PI = 3.14159265358979323846;\n//using ll = long long;\n\n// https://kmyk.github.io/blog/writeups/algo-etc-utpc2011-h/\n// http://www.utpc.jp/2011/slides/cache.pdf\n\n\n// Minimum Cost Flow (Dijkstra)\n// https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_6_B&lang=ja\n// 蟻本 p. 203\n// 始点0, 終点V-1\n\nint V, E, F;\nstruct edge { int to, cap, cost, rev; };\ntypedef pair<int, int> P;\n\nvector<edge> G[MAX_N];\nint h[MAX_N];\nint dist[MAX_N];\nint prevv[MAX_N], preve[MAX_N];\n\n// from からtoへ向かう容量cap, コストcostの辺をグラフに追加する\nvoid add_edge(int from, int to, int cap, int cost) {\n\tedge e1 = { to, cap, cost, G[to].size() };\n\tG[from].push_back(e1);\n\tedge e2 = { from, 0, -cost, G[from].size() - 1 };\n\tG[to].push_back(e2);\n}\n\n// sからtへの流量f最小費用流を求める\n// 流せない場合は-1を返す\nint min_cost_flow(int s, int t, int f) {\n\tint res = 0;\n\tfill(h, h + V, 0);\n\twhile (f > 0) {\n\t\t// ダイクストラ法を用いてhを更新する\n\t\tpriority_queue<P, vector<P>, greater<P> > que;\n\t\tfill(dist, dist + V, INF);\n\t\tdist[s] = 0;\n\t\tque.push(P(0, s));\n\t\twhile (!que.empty()) {\n\t\t\tP p = que.top(); que.pop();\n\t\t\tint v = p.second;\n\t\t\tif (dist[v] < p.first) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tfor (int i = 0; i < G[v].size(); i++) {\n\t\t\t\tedge& e = G[v][i];\n\t\t\t\tif (e.cap > 0 && dist[e.to] > dist[v] + e.cost + h[v] - h[e.to]) {\n\t\t\t\t\tdist[e.to] = dist[v] + e.cost + h[v] - h[e.to];\n\t\t\t\t\tprevv[e.to] = v;\n\t\t\t\t\tpreve[e.to] = i;\n\t\t\t\t\tque.push(P(dist[e.to], e.to));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif (dist[t] == INF) {\n\t\t\treturn -1;\n\t\t}\n\t\tfor (int v = 0; v < V; v++) {\n\t\t\th[v] += dist[v];\n\t\t}\n\n\t\t// s-t間最短路に沿って目一杯流す\n\t\tint d = f;\n\t\tfor (int v = t; v != s; v = prevv[v]) {\n\t\t\td = min(d, G[prevv[v]][preve[v]].cap);\n\t\t}\n\t\tf -= d;\n\t\tres += (d * h[t]);\n\t\tfor (int v = t; v != s; v = prevv[v]) {\n\t\t\tedge& e = G[prevv[v]][preve[v]];\n\t\t\te.cap -= d;\n\t\t\tG[v][e.rev].cap += d;\n\t\t}\n\t}\n\n\treturn res;\n\n}\n\nint M, N, K;\nint w[10010];\nint a[10010];\nint p[10010];\n\nint main() {\n\n\tcin >> M >> N >> K;\n\n\n\tV = K;\n\n\tfor (int i = 0; i < N; i++) {\n\t\tcin >> w[i];\n\t}\n\tint sum1 = 0;\n\tfor (int i = 0; i < K; i++) {\n\t\tcin >> a[i];\n\t\ta[i]--;\n\t\tsum1 += w[a[i]];\n\t}\n\n\tint s = 0;\n\tint t = K - 1;\n\n\tfor (int i = 0; i < K - 1; i++) {\n\t\tadd_edge(i, i + 1, INF, 0);\n\t}\n\n\tfor (int i = 0; i < N; i++) {\n\t\tp[i] = -1;\n\t}\n\n\tfor (int i = 0; i < K; i++) {\n\t\tif (p[a[i]] == -1) {\n\t\t\tp[a[i]] = i;\n\t\t\tcontinue;\n\t\t}\n\n\t\tint b = p[a[i]] + 1;\n\t\tint n = i;\n\t\tint nw = -w[a[i]];\n\t\tadd_edge(b, n, 1, nw);\n\n\t}\n\n\tcout << sum1 + min_cost_flow(s, t, M - 1) << endl;\n\n\n\n\treturn 0;\n}", "accuracy": 0.04477611940298507, "time_ms": 10, "memory_kb": 50004, "score_of_the_acc": -0.8383, "final_rank": 18 }, { "submission_id": "aoj_2266_6892299", "code_snippet": "#define _CRT_SECURE_NO_WARNINGS\n#include <cstdio>\n#include <iostream>\n#include <stack>\n#include <queue>\n#include <algorithm>\n#include <functional>\n#include <set>\n#include <map>\n#include <string>\n#include <vector>\n#include <cmath>\n#include<sstream>\n#include<list>\n#include<iomanip>\n#include <cstdlib>\n#include <cstring>\n#include <stack>\n#include <bitset>\n#include <cassert>\n#include <stdlib.h>\n#include <stdio.h>\nusing namespace std;\nconst int INF = 100000000;\nconst long long LINF = 3e18 + 7;\nconst int MAX_N = 2000010;\nconst int MAX_W = 10002;\nconst int MAX_ARRAYK = 100000;\ndouble PI = 3.14159265358979323846;\n//using ll = long long;\n\n// https://kmyk.github.io/blog/writeups/algo-etc-utpc2011-h/\n// http://www.utpc.jp/2011/slides/cache.pdf\n\n\n// Minimum Cost Flow (Dijkstra)\n// https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_6_B&lang=ja\n// 蟻本 p. 203\n// 始点0, 終点V-1\n\nint V, E, F;\nstruct edge { int to, cap, cost, rev; };\ntypedef pair<int, int> P;\n\nvector<edge> G[MAX_N];\nint h[MAX_N];\nint dist[MAX_N];\nint prevv[MAX_N], preve[MAX_N];\n\n// from からtoへ向かう容量cap, コストcostの辺をグラフに追加する\nvoid add_edge(int from, int to, int cap, int cost) {\n\tedge e1 = { to, cap, cost, G[to].size() };\n\tG[from].push_back(e1);\n\tedge e2 = { from, 0, -cost, G[from].size() - 1 };\n\tG[to].push_back(e2);\n}\n\n// sからtへの流量f最小費用流を求める\n// 流せない場合は-1を返す\nint min_cost_flow(int s, int t, int f) {\n\tint res = 0;\n\tfill(h, h + V, 0);\n\twhile (f > 0) {\n\t\t// ダイクストラ法を用いてhを更新する\n\t\tpriority_queue<P, vector<P>, greater<P> > que;\n\t\tfill(dist, dist + V, INF);\n\t\tdist[s] = 0;\n\t\tque.push(P(0, s));\n\t\twhile (!que.empty()) {\n\t\t\tP p = que.top(); que.pop();\n\t\t\tint v = p.second;\n\t\t\tif (dist[v] < p.first) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tfor (int i = 0; i < G[v].size(); i++) {\n\t\t\t\tedge& e = G[v][i];\n\t\t\t\tif (e.cap > 0 && dist[e.to] > dist[v] + e.cost + h[v] - h[e.to]) {\n\t\t\t\t\tdist[e.to] = dist[v] + e.cost + h[v] - h[e.to];\n\t\t\t\t\tprevv[e.to] = v;\n\t\t\t\t\tpreve[e.to] = i;\n\t\t\t\t\tque.push(P(dist[e.to], e.to));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif (dist[t] == INF) {\n\t\t\treturn -1;\n\t\t}\n\t\tfor (int v = 0; v < V; v++) {\n\t\t\th[v] += dist[v];\n\t\t}\n\n\t\t// s-t間最短路に沿って目一杯流す\n\t\tint d = f;\n\t\tfor (int v = t; v != s; v = prevv[v]) {\n\t\t\td = min(d, G[prevv[v]][preve[v]].cap);\n\t\t}\n\t\tf -= d;\n\t\tres += (d * h[t]);\n\t\tfor (int v = t; v != s; v = prevv[v]) {\n\t\t\tedge& e = G[prevv[v]][preve[v]];\n\t\t\te.cap -= d;\n\t\t\tG[v][e.rev].cap += d;\n\t\t}\n\t}\n\n\treturn res;\n\n}\n\nint M, N, K;\nint w[1010];\nint a[1010];\nint p[1010];\n\nint main() {\n\n\tcin >> M >> N >> K;\n\n\n\tV = K;\n\n\tfor (int i = 0; i < N; i++) {\n\t\tcin >> w[i];\n\t}\n\tint sum1 = 0;\n\tfor (int i = 0; i < K; i++) {\n\t\tcin >> a[i];\n\t\ta[i]--;\n\t\tsum1 += w[a[i]];\n\t}\n\n\tint s = 0;\n\tint t = K - 1;\n\n\tfor (int i = 0; i < K - 1; i++) {\n\t\tadd_edge(i, i + 1, INF, 0);\n\t}\n\n\tfor (int i = 0; i < N; i++) {\n\t\tp[i] = -1;\n\t}\n\n\tfor (int i = 0; i < K; i++) {\n\t\tif (p[a[i]] == -1) {\n\t\t\tp[a[i]] = i;\n\t\t\tcontinue;\n\t\t}\n\n\t\tint b = p[a[i]] + 1;\n\t\tint n = i;\n\t\tint nw = -w[a[i]];\n\t\tadd_edge(b, n, 1, nw);\n\n\t}\n\n\tcout << sum1 + min_cost_flow(s, t, M - 1) << endl;\n\n\n\n\treturn 0;\n}", "accuracy": 0.04477611940298507, "time_ms": 10, "memory_kb": 50084, "score_of_the_acc": -0.8397, "final_rank": 19 }, { "submission_id": "aoj_2266_6857245", "code_snippet": "#define _CRT_SECURE_NO_WARNINGS\n#include <cstdio>\n#include <iostream>\n#include <stack>\n#include <queue>\n#include <algorithm>\n#include <functional>\n#include <set>\n#include <map>\n#include <string>\n#include <vector>\n#include <cmath>\n#include<sstream>\n#include<list>\n#include<iomanip>\n#include <cstdlib>\n#include <cstring>\n#include <stack>\n#include <bitset>\n#include <cassert>\n#include <stdlib.h>\n#include <stdio.h>\nusing namespace std;\nconst int INF = 100000000;\nconst long long LINF = 3e18 + 7;\nconst int MAX_N = 2000010;\nconst int MAX_W = 10002;\nconst int MAX_ARRAYK = 100000;\ndouble PI = 3.14159265358979323846;\n//using ll = long long;\n\n// https://kmyk.github.io/blog/writeups/algo-etc-utpc2011-h/\n// http://www.utpc.jp/2011/slides/cache.pdf\n\nint V, E, F;\nstruct edge { int to, cap, cost, rev; };\n\nvector<edge> G[MAX_N];\nint dist[MAX_N];\nint prevv[MAX_N], preve[MAX_N];\n\n// from からtoへ向かう容量cap, コストcostの辺をグラフに追加する\nvoid add_edge(int from, int to, int cap, int cost) {\n\tedge e1 = { to, cap, cost, G[to].size() };\n\tG[from].push_back(e1);\n\tedge e2 = { from, 0, -cost, G[from].size() - 1 };\n\tG[to].push_back(e2);\n\n}\n\n// sからtへの流量f最小費用流を求める\n// 流せない場合は-1を返す\nint min_cost_flow(int s, int t, int f) {\n\tint res = 0;\n\twhile (f > 0) {\n\t\t//ベルマンフォードにより、s-t間最短路を求める\n\t\tfill(dist, dist + V, INF);\n\t\tdist[s] = 0;\n\t\tbool update = true;\n\t\twhile (update) {\n\t\t\tupdate = false;\n\t\t\tfor (int v = 0; v < V; v++) {\n\t\t\t\tif (dist[v] == INF) {\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tfor (int i = 0; i < G[v].size(); i++) {\n\t\t\t\t\tedge& e = G[v][i];\n\t\t\t\t\tif (e.cap > 0 && dist[e.to] > dist[v] + e.cost) {\n\t\t\t\t\t\tdist[e.to] = dist[v] + e.cost;\n\t\t\t\t\t\tprevv[e.to] = v;\n\t\t\t\t\t\tpreve[e.to] = i;\n\t\t\t\t\t\tupdate = true;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif (dist[t] == INF) {\n\t\t\treturn -1;\n\t\t}\n\n\t\t// s-t間最短路に沿って目一杯流す。\n\t\tint d = f;\n\t\tfor (int v = t; v != s; v = prevv[v]) {\n\t\t\td = min(d, G[prevv[v]][preve[v]].cap);\n\t\t}\n\n\t\tf -= d;\n\t\tres += (d * dist[t]);\n\t\tfor (int v = t; v != s; v = prevv[v]) {\n\t\t\tedge& e = G[prevv[v]][preve[v]];\n\t\t\te.cap -= d;\n\t\t\tG[v][e.rev].cap += d;\n\t\t}\n\t}\n\n\treturn res;\n\n}\n\nint N, M, K;\nint w[MAX_N];\nint last[MAX_N];\n\nint main() {\n\n\n\tcin >> M >> N >> K;\n\tfor (int i = 0; i < N; i++) {\n\t\tcin >> w[i];\n\t}\n\tvector<int> a(K);\n\tfor (int i = 0; i < K; i++) {\n\t\tcin >> a[i];\n\t\ta[i]--;\n\t}\n\n\ta.erase(unique(a.begin(), a.end()), a.end());\n\tK = a.size();\n\tV = K;\n\tint s = 0;\n\tint t = V - 1;\n\tint base = 0;\n\tfor (int i = 0; i < K; i++) {\n\t\tbase += w[a[i]];\n\t}\n\n\tfor (int i = 0; i < K - 1; i++) {\n\t\tadd_edge(i, i + 1, INF, 0);\n\t}\n\n\tmemset(last, -1, sizeof(last));\n\n\tfor (int i = 0; i < K; i++) {\n\t\tint idx = a[i];\n\t\tif (last[idx] == -1) {\n\t\t\tlast[idx] = i;\n\t\t\tcontinue;\n\t\t}\n\n\t\tadd_edge(last[idx] + 1, i, 1, -w[idx]);\n\t\tlast[idx] = i;\n\t}\n\n\tint ans = base + min_cost_flow(s, t, M - 1);\n\n\tcout << ans << endl;\n\n\t\n\treturn 0;\n}", "accuracy": 1, "time_ms": 620, "memory_kb": 58808, "score_of_the_acc": -1.4793, "final_rank": 15 }, { "submission_id": "aoj_2266_6857102", "code_snippet": "#define _CRT_SECURE_NO_WARNINGS\n#include <cstdio>\n#include <iostream>\n#include <stack>\n#include <queue>\n#include <algorithm>\n#include <functional>\n#include <set>\n#include <map>\n#include <string>\n#include <vector>\n#include <cmath>\n#include<sstream>\n#include<list>\n#include<iomanip>\n#include <cstdlib>\n#include <cstring>\n#include <stack>\n#include <bitset>\n#include <cassert>\n#include <stdlib.h>\n#include <stdio.h>\nusing namespace std;\nconst int INF = 100000000;\nconst long long LINF = 3e18 + 7;\nconst int MAX_N = 2000010;\nconst int MAX_W = 10002;\nconst int MAX_ARRAYK = 100000;\ndouble PI = 3.14159265358979323846;\n//using ll = long long;\n\n// http://www.utpc.jp/2011/slides/cache.pdf\n\nint V, E, F;\nstruct edge { int to, cap, cost, rev; };\n\nvector<edge> G[MAX_N];\nint dist[MAX_N];\nint prevv[MAX_N], preve[MAX_N];\n\n// from からtoへ向かう容量cap, コストcostの辺をグラフに追加する\nvoid add_edge(int from, int to, int cap, int cost) {\n\tedge e1 = { to, cap, cost, G[to].size() };\n\tG[from].push_back(e1);\n\tedge e2 = { from, 0, -cost, G[from].size() - 1 };\n\tG[to].push_back(e2);\n\n}\n\n// sからtへの流量f最小費用流を求める\n// 流せない場合は-1を返す\nint min_cost_flow(int s, int t, int f) {\n\tint res = 0;\n\twhile (f > 0) {\n\t\t//ベルマンフォードにより、s-t間最短路を求める\n\t\tfill(dist, dist + V, INF);\n\t\tdist[s] = 0;\n\t\tbool update = true;\n\t\twhile (update) {\n\t\t\tupdate = false;\n\t\t\tfor (int v = 0; v < V; v++) {\n\t\t\t\tif (dist[v] == INF) {\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tfor (int i = 0; i < G[v].size(); i++) {\n\t\t\t\t\tedge& e = G[v][i];\n\t\t\t\t\tif (e.cap > 0 && dist[e.to] > dist[v] + e.cost) {\n\t\t\t\t\t\tdist[e.to] = dist[v] + e.cost;\n\t\t\t\t\t\tprevv[e.to] = v;\n\t\t\t\t\t\tpreve[e.to] = i;\n\t\t\t\t\t\tupdate = true;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif (dist[t] == INF) {\n\t\t\treturn -1;\n\t\t}\n\n\t\t// s-t間最短路に沿って目一杯流す。\n\t\tint d = f;\n\t\tfor (int v = t; v != s; v = prevv[v]) {\n\t\t\td = min(d, G[prevv[v]][preve[v]].cap);\n\t\t}\n\n\t\tf -= d;\n\t\tres += (d * dist[t]);\n\t\tfor (int v = t; v != s; v = prevv[v]) {\n\t\t\tedge& e = G[prevv[v]][preve[v]];\n\t\t\te.cap -= d;\n\t\t\tG[v][e.rev].cap += d;\n\t\t}\n\t}\n\n\treturn res;\n\n}\n\nint N, M, K;\nint w[MAX_N];\nint last[MAX_N];\n\nint main() {\n\n\n\tcin >> M >> N >> K;\n\tfor (int i = 0; i < N; i++) {\n\t\tcin >> w[i];\n\t}\n\tvector<int> a(K);\n\tfor (int i = 0; i < K; i++) {\n\t\tcin >> a[i];\n\t\ta[i]--;\n\t}\n\n\ta.erase(unique(a.begin(), a.end()), a.end());\n\tK = a.size();\n\tV = K;\n\tint s = 0;\n\tint t = V - 1;\n\tint base = 0;\n\tfor (int i = 0; i < K; i++) {\n\t\tbase += w[a[i]];\n\t}\n\n\tfor (int i = 0; i < K - 1; i++) {\n\t\tadd_edge(i, i + 1, INF, 0);\n\t}\n\n\tmemset(last, -1, sizeof(last));\n\n\tfor (int i = 0; i < K; i++) {\n\t\tint idx = a[i];\n\t\tif (last[idx] == -1) {\n\t\t\tlast[idx] = i;\n\t\t\tcontinue;\n\t\t}\n\n\t\tadd_edge(last[idx] + 1, i, 1, -w[idx]);\n\t\tlast[idx] = i;\n\t}\n\n\tint ans = base + min_cost_flow(s, t, M - 1);\n\n\tcout << ans << endl;\n\n\t\n\treturn 0;\n}", "accuracy": 1, "time_ms": 610, "memory_kb": 58864, "score_of_the_acc": -1.4724, "final_rank": 14 }, { "submission_id": "aoj_2266_6857101", "code_snippet": "#define _CRT_SECURE_NO_WARNINGS\n#include <cstdio>\n#include <iostream>\n#include <stack>\n#include <queue>\n#include <algorithm>\n#include <functional>\n#include <set>\n#include <map>\n#include <string>\n#include <vector>\n#include <cmath>\n#include<sstream>\n#include<list>\n#include<iomanip>\n#include <cstdlib>\n#include <cstring>\n#include <stack>\n#include <bitset>\n#include <cassert>\n#include <stdlib.h>\n#include <stdio.h>\nusing namespace std;\nconst int INF = 100000000;\nconst long long LINF = 3e18 + 7;\nconst int MAX_N = 2000010;\nconst int MAX_W = 10002;\nconst int MAX_ARRAYK = 100000;\ndouble PI = 3.14159265358979323846;\n//using ll = long long;\n\n// https://area.hateblo.jp/entry/2017/12/18/010657\n\nint V, E, F;\nstruct edge { int to, cap, cost, rev; };\n\nvector<edge> G[MAX_N];\nint dist[MAX_N];\nint prevv[MAX_N], preve[MAX_N];\n\n// from からtoへ向かう容量cap, コストcostの辺をグラフに追加する\nvoid add_edge(int from, int to, int cap, int cost) {\n\tedge e1 = { to, cap, cost, G[to].size() };\n\tG[from].push_back(e1);\n\tedge e2 = { from, 0, -cost, G[from].size() - 1 };\n\tG[to].push_back(e2);\n\n}\n\n// sからtへの流量f最小費用流を求める\n// 流せない場合は-1を返す\nint min_cost_flow(int s, int t, int f) {\n\tint res = 0;\n\twhile (f > 0) {\n\t\t//ベルマンフォードにより、s-t間最短路を求める\n\t\tfill(dist, dist + V, INF);\n\t\tdist[s] = 0;\n\t\tbool update = true;\n\t\twhile (update) {\n\t\t\tupdate = false;\n\t\t\tfor (int v = 0; v < V; v++) {\n\t\t\t\tif (dist[v] == INF) {\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tfor (int i = 0; i < G[v].size(); i++) {\n\t\t\t\t\tedge& e = G[v][i];\n\t\t\t\t\tif (e.cap > 0 && dist[e.to] > dist[v] + e.cost) {\n\t\t\t\t\t\tdist[e.to] = dist[v] + e.cost;\n\t\t\t\t\t\tprevv[e.to] = v;\n\t\t\t\t\t\tpreve[e.to] = i;\n\t\t\t\t\t\tupdate = true;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif (dist[t] == INF) {\n\t\t\treturn -1;\n\t\t}\n\n\t\t// s-t間最短路に沿って目一杯流す。\n\t\tint d = f;\n\t\tfor (int v = t; v != s; v = prevv[v]) {\n\t\t\td = min(d, G[prevv[v]][preve[v]].cap);\n\t\t}\n\n\t\tf -= d;\n\t\tres += (d * dist[t]);\n\t\tfor (int v = t; v != s; v = prevv[v]) {\n\t\t\tedge& e = G[prevv[v]][preve[v]];\n\t\t\te.cap -= d;\n\t\t\tG[v][e.rev].cap += d;\n\t\t}\n\t}\n\n\treturn res;\n\n}\n\nint N, M, K;\nint w[MAX_N];\nint last[MAX_N];\n\nint main() {\n\n\n\tcin >> M >> N >> K;\n\tfor (int i = 0; i < N; i++) {\n\t\tcin >> w[i];\n\t}\n\tvector<int> a(K);\n\tfor (int i = 0; i < K; i++) {\n\t\tcin >> a[i];\n\t\ta[i]--;\n\t}\n\n\ta.erase(unique(a.begin(), a.end()), a.end());\n\tK = a.size();\n\tV = K;\n\tint s = 0;\n\tint t = V - 1;\n\tint base = 0;\n\tfor (int i = 0; i < K; i++) {\n\t\tbase += w[a[i]];\n\t}\n\n\tfor (int i = 0; i < K - 1; i++) {\n\t\tadd_edge(i, i + 1, INF, 0);\n\t}\n\n\tmemset(last, -1, sizeof(last));\n\n\tfor (int i = 0; i < K; i++) {\n\t\tint idx = a[i];\n\t\tif (last[idx] == -1) {\n\t\t\tlast[idx] = i;\n\t\t\tcontinue;\n\t\t}\n\n\t\tadd_edge(last[idx] + 1, i, 1, -w[idx]);\n\t\tlast[idx] = i;\n\t}\n\n\tint ans = base + min_cost_flow(s, t, M - 1);\n\n\tcout << ans << endl;\n\n\t\n\treturn 0;\n}", "accuracy": 1, "time_ms": 610, "memory_kb": 58808, "score_of_the_acc": -1.4714, "final_rank": 13 }, { "submission_id": "aoj_2266_6857090", "code_snippet": "#define _CRT_SECURE_NO_WARNINGS\n#include <cstdio>\n#include <iostream>\n#include <stack>\n#include <queue>\n#include <algorithm>\n#include <functional>\n#include <set>\n#include <map>\n#include <string>\n#include <vector>\n#include <cmath>\n#include<sstream>\n#include<list>\n#include<iomanip>\n#include <cstdlib>\n#include <cstring>\n#include <stack>\n#include <bitset>\n#include <cassert>\n#include <stdlib.h>\n#include <stdio.h>\nusing namespace std;\nconst int INF = 100000000;\nconst long long LINF = 3e18 + 7;\nconst int MAX_N = 2000010;\nconst int MAX_W = 10002;\nconst int MAX_ARRAYK = 100000;\ndouble PI = 3.14159265358979323846;\n//using ll = long long;\n\n// https://area.hateblo.jp/entry/2017/12/18/010657\n\nint V, E, F;\nstruct edge { int to, cap, cost, rev; };\n\nvector<edge> G[MAX_N];\nint dist[MAX_N];\nint prevv[MAX_N], preve[MAX_N];\n\n// from からtoへ向かう容量cap, コストcostの辺をグラフに追加する\nvoid add_edge(int from, int to, int cap, int cost) {\n\tedge e1 = { to, cap, cost, G[to].size() };\n\tG[from].push_back(e1);\n\tedge e2 = { from, 0, -cost, G[from].size() - 1 };\n\tG[to].push_back(e2);\n\n}\n\n// sからtへの流量f最小費用流を求める\n// 流せない場合は-1を返す\nint min_cost_flow(int s, int t, int f) {\n\tint res = 0;\n\twhile (f > 0) {\n\t\t//ベルマンフォードにより、s-t間最短路を求める\n\t\tfill(dist, dist + V, INF);\n\t\tdist[s] = 0;\n\t\tbool update = true;\n\t\twhile (update) {\n\t\t\tupdate = false;\n\t\t\tfor (int v = 0; v < V; v++) {\n\t\t\t\tif (dist[v] == INF) {\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tfor (int i = 0; i < G[v].size(); i++) {\n\t\t\t\t\tedge& e = G[v][i];\n\t\t\t\t\tif (e.cap > 0 && dist[e.to] > dist[v] + e.cost) {\n\t\t\t\t\t\tdist[e.to] = dist[v] + e.cost;\n\t\t\t\t\t\tprevv[e.to] = v;\n\t\t\t\t\t\tpreve[e.to] = i;\n\t\t\t\t\t\tupdate = true;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif (dist[t] == INF) {\n\t\t\treturn -1;\n\t\t}\n\n\t\t// s-t間最短路に沿って目一杯流す。\n\t\tint d = f;\n\t\tfor (int v = t; v != s; v = prevv[v]) {\n\t\t\td = min(d, G[prevv[v]][preve[v]].cap);\n\t\t}\n\n\t\tf -= d;\n\t\tres += (d * dist[t]);\n\t\tfor (int v = t; v != s; v = prevv[v]) {\n\t\t\tedge& e = G[prevv[v]][preve[v]];\n\t\t\te.cap -= d;\n\t\t\tG[v][e.rev].cap += d;\n\t\t}\n\t}\n\n\treturn res;\n\n}\n\nint N, M, K;\nint w[MAX_N];\nint a[MAX_N];\nint last[MAX_N];\n\nint main() {\n\n\n\tcin >> M >> N >> K;\n\tfor (int i = 0; i < N; i++) {\n\t\tcin >> w[i];\n\t}\n\n\tfor (int i = 0; i < K; i++) {\n\t\tcin >> a[i];\n\t\ta[i]--;\n\t}\n\n\tV = K;\n\tint s = 0;\n\tint t = V - 1;\n\tint base = 0;\n\tfor (int i = 0; i < K; i++) {\n\t\tbase += w[a[i]];\n\t}\n\n\tfor (int i = 0; i < K - 1; i++) {\n\t\tadd_edge(i, i + 1, INF, 0);\n\t}\n\n\tmemset(last, -1, sizeof(last));\n\n\tfor (int i = 0; i < K; i++) {\n\t\tint idx = a[i];\n\t\tif (last[idx] == -1) {\n\t\t\tlast[idx] = i;\n\t\t\tcontinue;\n\t\t}\n\n\t\tadd_edge(last[idx] + 1, i, 1, -w[idx]);\n\t\tlast[idx] = i;\n\t}\n\n\tint ans = base + min_cost_flow(s, t, M - 1);\n\n\tcout << ans << endl;\n\n\t\n\treturn 0;\n}", "accuracy": 0.04477611940298507, "time_ms": 10, "memory_kb": 57848, "score_of_the_acc": -0.9815, "final_rank": 20 }, { "submission_id": "aoj_2266_6620379", "code_snippet": "#include<cstdio>\n#include<vector>\n#include<cstring>\n#include<queue>\nusing namespace std;\n\nconst int maxn=10000+50;\nconst int maxv=maxn;\nconst int INF=100000000;\n\n\ntypedef pair<int,int>P;\n\nstruct edge{\n int to,cap,cost,rev;\n edge(){}\n edge(int t,int ca,int co,int r):to(t),cap(ca),cost(co),rev(r){}\n};\n\nint V;\nvector<edge>es[maxv];\n\nint h[maxv];\nint dist[maxv];\nint prevv[maxv],preve[maxv];\n\nvoid add_edge(int f,int t,int ca,int co){\n es[f].push_back(edge(t,ca,co,es[t].size()));\n es[t].push_back(edge(f,0,-co,es[f].size()-1));\n}\n\nint min_cost_flow(int s,int t,int f){\n fill(h,h+V,INF);\n h[s]=0;\n\n int res=0;\n\n bool update=true;\n while(update){\n update=false;\n\n for(int v=0;v<V;v++){\n if(h[v]==INF)continue;\n\n for(int i=0;i<es[v].size();i++){\n edge& e=es[v][i];\n if(e.cap>0&&h[e.to]>h[v]+e.cost){\n h[e.to]=h[v]+e.cost;\n prevv[e.to]=v;\n preve[e.to]=i;\n update=true;\n }\n }\n }\n }\n\n if(h[t]==INF)return -1;\n\n int d=f;\n for(int v=t;v!=s;v=prevv[v])\n d=min(d,es[prevv[v]][preve[v]].cap);\n\n f-=d;\n res+=d*h[t];\n\n for(int v=t;v!=s;v=prevv[v]){\n edge& e=es[prevv[v]][preve[v]];\n e.cap-=d;\n es[v][e.rev].cap+=d;\n }\n\n while(f>0){\n fill(dist,dist+V,INF);\n dist[s]=0;\n\n priority_queue<P,vector<P>,greater<P> >que;\n que.push(P(0,s));\n\n while(!que.empty()){\n int v=que.top().second;que.pop();\n\n for(int i=0;i<es[v].size();i++){\n edge&e=es[v][i];\n\n if(e.cap>0&&dist[e.to]>dist[v]+e.cost+h[v]-h[e.to]){\n dist[e.to]=dist[v]+e.cost+h[v]-h[e.to];\n prevv[e.to]=v;\n preve[e.to]=i;\n que.push(P(dist[e.to],e.to));\n }\n }\n }\n\n if(dist[t]==INF)return -1;\n\n for(int i=0;i<V;i++)h[i]+=dist[i];\n\n int d=f;\n for(int v=t;v!=s;v=prevv[v])\n d=min(d,es[prevv[v]][preve[v]].cap);\n\n f-=d;\n res+=d*h[t];\n\n for(int v=t;v!=s;v=prevv[v]){\n edge& e=es[prevv[v]][preve[v]];\n e.cap-=d;\n es[v][e.rev].cap+=d;\n }\n }\n return res;\n}\n\nint M,N,K;\nint w[maxn];\nint a[maxn];\n\nint reco[maxn];\n\nvoid solve(){\n scanf(\"%d%d%d\",&M,&N,&K);\n\n for(int i=1;i<=N;i++)\n scanf(\"%d\",&w[i]);\n\n int ans=0;\n int n=0;\n\n for(int i=0;i<K;i++){\n int u;\n scanf(\"%d\",&u);\n if(n==0||u!=a[n-1]){\n a[n++]=u;\n ans+=w[u];\n }\n }\n\n V=n;\n\n for(int i=0;i<maxn;i++)es[i].clear();\n\n memset(reco,-1,sizeof(reco));\n\n for(int i=0;i<n;i++){\n int u=a[i];\n if(reco[u]>=0)\n add_edge(reco[u],i-1,1,-w[u]);\n reco[u]=i;\n\n add_edge(i,i+1,INF,0);\n }\n\n printf(\"%d\\n\",ans+min_cost_flow(0,n-1,M-1));\n}\nint main()\n{\n solve();\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 4088, "score_of_the_acc": 0, "final_rank": 1 }, { "submission_id": "aoj_2266_5146743", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nconst int INF =1e9;\nconst int inf = 2e4;\nconst int max_v = 2e4 + 10;\ntypedef pair<int,int> P;\nstruct edge{\n\tint to,cap,cost,rev;\n\tedge(int _to,int _cap,int _cost,int _rev) : to(_to),cap(_cap),cost(_cost),rev(_rev){}\n};\nvector<edge> g[max_v]; \nvoid add_edge(int from,int to,int cap,int cost){\n\tg[from].push_back(edge(to,cap,cost,g[to].size()));\n\tg[to].push_back(edge(from,0,-1*cost,g[from].size() - 1));\n}\n\n//两次变换:\n//1:顶点流量限制,加上 -inf;保证最小费用高于-2e9; \n//2:加势去负边 ,点加上(V - 点号) * inf; 保证加势后低于2e9;\n\n//算出来后:\n//1:先去势得到最小费用 \n//2:去流量限制后的真确结果;\n//3:总费用加上节省副费用得到结果;\n\n//对于点v:\n// 入点为2*v,出点为2*v + 1; \n\nint box,ball,n,V;\n\nint dist[max_v],h[max_v];\nint prevv[max_v],preve[max_v];\n\n//n为点数 \n//V为改造图总点数\n\nint min_cost_flow(int s,int t,int f){\n\tint ret = 0;\n\tfill(h,h+V,0);\n\twhile(f > 0){\n\t\tfill(dist,dist + V,INF);\n\t\tpriority_queue<P,vector<P>,greater<P> > que;\n\t\tdist[s] = 0;\n\t\tque.push(P(dist[s],s));\n\t\tint letmesee = 0;\n\t\twhile(!que.empty()){\n\t\t\tP p = que.top();que.pop();\n\t\t\tint v = p.second;\n\t\t\tif(dist[v] < p.first)continue;\n\t\t\tfor(int i = 0;i < g[v].size();i++){\n\t\t\t\tedge &e = g[v][i];\n\t\t\t\tif(e.cap > 0 && dist[v] + e.cost + h[v] - h[e.to] < dist[e.to]){\n\t\t\t\t\tdist[e.to] = dist[v] + e.cost + h[v] - h[e.to];\n\t\t\t\t\tque.push(P(dist[e.to],e.to));\n\t\t\t\t\tprevv[e.to] = v;\n\t\t\t\t\tpreve[e.to] = i;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif(dist[t] == INF)return -1;\n\t\tfor(int i = 0;i < V;i++)h[i] += dist[i];\n\t\tint d = f;\n\t\tfor(int v = t;v != s;v = prevv[v]){\n\t\t\tedge &e = g[prevv[v]][preve[v]];\n\t\t\td = min(d,e.cap);\n\t\t}\n\t\tf -= d;\n\t\tret += h[t] * d;\n\t\t//还原势 \n\t\tret -= inf * (t - s) * d;\n\t\tfor(int v = t;v != s;v = prevv[v]){\n\t\t\tedge &e = g[prevv[v]][preve[v]];\n\t\t\te.cap -= d;\n\t\t\tg[v][e.rev].cap += d;\n\t\t}\n\t}\n\t//还原点限制 \n\tret += n * inf;\n\treturn ret;\n}\n\n\nvector<int> es[max_v];\n\nint vin(int v){\n\treturn 2 * v;\n}\nint vout(int v){\n\treturn 2 * v + 1;\n}\n\nint main(){\n\tint ans = 0;\n\tint ballcost[max_v];\n\tint order[max_v];\n\tscanf(\"%d%d%d\",&box,&ball,&n);\n\tfor(int i = 0;i < ball;i++){\n\t\tscanf(\"%d\",&ballcost[i]);\n\t}\n\tfor(int v = 0;v < n;v++){\n\t\tscanf(\"%d\",&order[v]);\n\t\torder[v]--;\n\t\tans += ballcost[order[v]];\n\t\tes[order[v]].push_back(v);\n\t\tadd_edge(vin(v),vout(v),1,-1*inf + inf);\n\t\tadd_edge(vin(v),vout(v),INF,0 + inf);\n\t\tif(v + 1 < n)add_edge(vout(v),vin(v+1),INF,0 + inf * (vin(v+1) - vout(v)));\n\t}\n\tfor(int i = 0;i < ball;i++){\n\t\tint kkk = es[i].size();\n\t\tfor(int j = 0;j < kkk - 1;j++){\n\t\t\tint v = es[i][j];\n\t\t\tint u = es[i][j + 1];\n\t\t\tadd_edge(vout(v),vin(u),1,-1 * ballcost[i] + inf * (vin(u) - vout(v)));\n\t\t} \n\t}\n\tV = 2 * n;\n\tcout << ans + min_cost_flow(vin(0),vout(n-1),box) << endl;\n\treturn 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 6408, "score_of_the_acc": -0.0502, "final_rank": 2 }, { "submission_id": "aoj_2266_5146739", "code_snippet": "#include <cstdio>\n#include <cstring>\n#include <iostream>\n#include <algorithm>\n#include <queue>\n#include <vector>\n#define min(a,b) (((a) < (b)) ? (a) : (b))\n#define max(a,b) (((a) > (b)) ? (a) : (b))\n#define abs(x) ((x) < 0 ? -(x) : (x))\n#define INF 0x3f3f3f3f\n#define delta 0.85\n#define eps 1e-10\n#define PI 3.14159265358979323846\nusing namespace std;\n\n#define MAX_V 10005\nstruct edge{\n\tint to, cap, cost, rev;\n\tedge(int to, int cap, int cost, int rev):to(to), cap(cap), cost(cost), rev(rev){}\n};\n\nint V;\nvector<edge> G[MAX_V];\nint h[MAX_V], dist[MAX_V];\nint prevv[MAX_V], preve[MAX_V];\nbool inq[MAX_V];\n\nvoid add_edge(int from, int to, int cap, int cost){\n\tG[from].push_back(edge(to, cap, cost, G[to].size()));\n\tG[to].push_back(edge(from, 0, -cost, G[from].size() - 1));\n}\n\nint min_cost_flow(int s, int t, int f){\n\tint res = 0;\n\tmemset(h, 0, sizeof(h));\n\t// Spfa 处理负权边\n\tmemset(inq, 0, sizeof(inq));\n\tmemset(dist, 0x3f, sizeof(dist));\n\tqueue<int> q;\n\tdist[s] = 0;\n\tq.push(s);\n\tinq[s] = 1;\n\twhile(!q.empty()){\n\t\tint v = q.front(); q.pop();\n\t\tinq[v] = 0;\n\t\tfor(int i = 0; i < G[v].size(); i++){\n\t\t\tedge &e = G[v][i];\n\t\t\tint d2 = dist[v] + e.cost + h[v] - h[e.to];\n\t\t\tif(e.cap > 0 && dist[e.to] > d2){\n\t\t\t\tdist[e.to] = d2;\n\t\t\t\tif(!inq[e.to]){\n\t\t\t\t\tq.push(e.to);\n\t\t\t\t\tinq[e.to] = 1;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tfor(int v = 0; v < V; v++) h[v] += dist[v];\n\t\n\twhile(f > 0){\n\t\t// Dijkstra 连续最短路求最小费用流\n\t\tpriority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > que;\n\t\tmemset(dist, 0x3f, sizeof(dist));\n\t\tdist[s] = 0;\n\t\tque.push(pair<int, int>(0, s));\n\t\twhile(!que.empty()){\n\t\t\tpair<int, int> p = que.top(); que.pop();\n\t\t\tint v = p.second;\n\t\t\tif(dist[v] < p.first) continue;\n\t\t\tfor(int i = 0; i < G[v].size(); i++){\n\t\t\t\tedge &e = G[v][i];\n\t\t\t\tint d2 = dist[v] + e.cost + h[v] - h[e.to];\n\t\t\t\tif(e.cap > 0 && d2 < dist[e.to]){\n\t\t\t\t\tdist[e.to] = d2;\n\t\t\t\t\tprevv[e.to] = v;\n\t\t\t\t\tpreve[e.to] = i;\n\t\t\t\t\tque.push(pair<int, int>(dist[e.to], e.to));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif(dist[t] == INF){\n\t\t\treturn -1;\n\t\t}\n\t\tfor(int v = 0; v < V; v++) h[v] += dist[v];\n\t\tint d = f;\n\t\tfor(int v = t; v != s; v = prevv[v]){\n\t\t\td = min(d, G[prevv[v]][preve[v]].cap);\n\t\t}\n\t\tf -= d;\n\t\tres += d * h[t];\n\t\tfor(int v = t; v != s; v = prevv[v]){\n\t\t\tedge &e = G[prevv[v]][preve[v]];\n\t\t\te.cap -= d;\n\t\t\tG[v][e.rev].cap += d;\n\t\t}\n\t}\n\treturn res;\n}\n\nvoid clear_graph(){\n\tfor(int v = 0; v < V; v++) G[v].clear();\n}\n\n#define MAX_K 10000\n#define MAX_N 10000\nint M, N, K;\nint w[MAX_N];\nint a[MAX_K], pre[MAX_K];\n\nvoid solve(){\n\tint res = 0;\n\tmemset(pre, -1, sizeof(pre));\n\t// 连续区间相同元素去重\n\tint na = unique(a, a + K) - a;\n\t// 初始化图\n\tint s = na, t = s + 1;\n\tV = t + 1;\n\tclear_graph();\n\t// 建图\n\tadd_edge(s, 0, M - 1, 0);\n\tadd_edge(na - 1, t, M - 1, 0);\n\tfor(int i = 0; i < na - 1; i++){\n\t\tadd_edge(i, i + 1, INF, 0);\n\t}\n\tint cnt = 0;\n\tfor(int i = 0; i < na; i++){\n\t\tif(pre[a[i]] != -1){\n\t\t\tadd_edge(pre[a[i]] + 1, i, 1, -w[a[i]]);\n\t\t}\n\t\tpre[a[i]] = i;\n\t}\n\t// 答案为最坏情况费用加上最小费用流结果\n\tfor(int i = 0; i < na; i++) res += w[a[i]];\n\tres += min_cost_flow(s, t, M - 1);\n\tprintf(\"%d\\n\", res);\n}\n\nint main(){\n\twhile(~scanf(\"%d%d%d\", &M, &N, &K)){\n\t\tfor(int i = 0; i < N; i++){\n\t\t\tscanf(\"%d\", w + i);\n\t\t}\n\t\tfor(int i = 0; i < K; i++){\n\t\t\tscanf(\"%d\", a + i);\n\t\t\t--a[i];\n\t\t}\n\t\tsolve();\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 260, "memory_kb": 4700, "score_of_the_acc": -0.208, "final_rank": 6 }, { "submission_id": "aoj_2266_5146560", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nconst int INF =1e9;\nconst int inf = 2e4;\nconst int max_v = 2e4 + 10;\ntypedef pair<int,int> P;\nstruct edge{\n\tint to,cap,cost,rev;\n\tedge(int _to,int _cap,int _cost,int _rev) : to(_to),cap(_cap),cost(_cost),rev(_rev){}\n};\nvector<edge> g[max_v]; \nvoid add_edge(int from,int to,int cap,int cost){\n\tg[from].push_back(edge(to,cap,cost,g[to].size()));\n\tg[to].push_back(edge(from,0,-1*cost,g[from].size() - 1));\n}\n\n//两次变换:\n//1:顶点流量限制,加上 -inf;保证最小费用高于-2e9; \n//2:加势去负边 ,点加上(V - 点号) * inf; 保证加势后低于2e9;\n\n//算出来后:\n//1:先去势得到最小费用 \n//2:去流量限制后的真确结果;\n//3:总费用加上节省副费用得到结果;\n\n//对于点v:\n// 入点为2*v,出点为2*v + 1; \n\nint box,ball,n,V;\n\nint dist[max_v],h[max_v];\nint prevv[max_v],preve[max_v];\n\n//n为点数 \n//V为改造图总点数\n\nint min_cost_flow(int s,int t,int f){\n\tint ret = 0;\n\tfill(h,h+V,0);\n\twhile(f > 0){\n\t\tfill(dist,dist + V,INF);\n\t\tpriority_queue<P,vector<P>,greater<P> > que;\n\t\tdist[s] = 0;\n\t\tque.push(P(dist[s],s));\n\t\tint letmesee = 0;\n\t\twhile(!que.empty()){\n\t\t\tP p = que.top();que.pop();\n\t\t\tint v = p.second;\n\t\t\tif(dist[v] < p.first)continue;\n\t\t\tfor(int i = 0;i < g[v].size();i++){\n\t\t\t\tedge &e = g[v][i];\n\t\t\t\tif(e.cap > 0 && dist[v] + e.cost + h[v] - h[e.to] < dist[e.to]){\n\t\t\t\t\tdist[e.to] = dist[v] + e.cost + h[v] - h[e.to];\n\t\t\t\t\tque.push(P(dist[e.to],e.to));\n\t\t\t\t\tprevv[e.to] = v;\n\t\t\t\t\tpreve[e.to] = i;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif(dist[t] == INF)return -1;\n\t\tfor(int i = 0;i < V;i++)h[i] += dist[i];\n\t\tint d = f;\n\t\tfor(int v = t;v != s;v = prevv[v]){\n\t\t\tedge &e = g[prevv[v]][preve[v]];\n\t\t\td = min(d,e.cap);\n\t\t}\n\t\tf -= d;\n\t\tret += h[t] * d;\n\t\t//还原势 \n\t\tret -= inf * (t - s) * d;\n\t\tfor(int v = t;v != s;v = prevv[v]){\n\t\t\tedge &e = g[prevv[v]][preve[v]];\n\t\t\te.cap -= d;\n\t\t\tg[v][e.rev].cap += d;\n\t\t}\n\t}\n\t//还原点限制 \n\tret += n * inf;\n\treturn ret;\n}\n\n\nvector<int> es[max_v];\n\nint vin(int v){\n\treturn 2 * v;\n}\nint vout(int v){\n\treturn 2 * v + 1;\n}\n\nint main(){\n\tint ans = 0;\n\tint ballcost[max_v];\n\tint order[max_v];\n\tscanf(\"%d%d%d\",&box,&ball,&n);\n\tfor(int i = 0;i < ball;i++){\n\t\tscanf(\"%d\",&ballcost[i]);\n\t}\n\tfor(int v = 0;v < n;v++){\n\t\tscanf(\"%d\",&order[v]);\n\t\torder[v]--;\n\t\tans += ballcost[order[v]];\n\t\tes[order[v]].push_back(v);\n\t\tadd_edge(vin(v),vout(v),1,-1*inf + inf);\n\t\tadd_edge(vin(v),vout(v),INF,0 + inf);\n\t\tif(v + 1 < n)add_edge(vout(v),vin(v+1),INF,0 + inf * (vin(v+1) - vout(v)));\n\t}\n\tfor(int i = 0;i < ball;i++){\n\t\tint kkk = es[i].size();\n\t\tfor(int j = 0;j < kkk - 1;j++){\n\t\t\tint v = es[i][j];\n\t\t\tint u = es[i][j + 1];\n\t\t\tadd_edge(vout(v),vin(u),1,-1 * ballcost[i] + inf * (vin(u) - vout(v)));\n\t\t} \n\t}\n\tV = 2 * n;\n//\tprintf(\"V = %d\\n\",V);\n//\tfor(int i = 0;i < V;i++){\n//\t\tfor(int j = 0;j < g[i].size();j++){\n//\t\t\tedge &e = g[i][j];\n//\t\t\tif(e.cap > 0){\n//\t\t\t\tif(e.to >= V)cout << \"越界\"<<endl;\n//\t\t\t\tif(e.cost < 0)cout << \"负边\"<<endl;\n//\t\t\t\tif(e.to <= i)cout << \"指回去了\"<<endl;\n//\t\t\t}\n//\t\t}\n//\t}\n//\tcout << \"未出现上述情况\"<<endl;\n\tcout << ans + min_cost_flow(vin(0),vout(n-1),box) << endl;\n\treturn 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 6444, "score_of_the_acc": -0.0509, "final_rank": 3 } ]
aoj_2265_cpp
問題 G : プログラミングコンテストチャレンジブック 今,G○○gle Code Jam の地区大会が始まろうとしている. 前の席に座っている男の ID は omeometo と言うらしい. 後ろの席に座っている男の ID は jellies と言うらしい. 東京大学時代の記憶に,似たような ID の仲間が居た覚えがあるが,僕の仲間は一人残さず美少女だったはずだ. 彼らは,机の上に蟻のイラストが掛かれた本を持っている. あれはもしや…プログラミングコンテストのアルゴリズムを最初に網羅し,今では発売禁止となった伝説の本, 「プログラミングコンテストチャレンジブック」ではないか? 僕は,omeometo がトイレに行ったのを見計らい,少し拝借して,読んでみることにした. プログラミングコンテストチャレンジブック …しかし,僕は一瞬で本を読み終えてしまった. 拍子抜けだ.簡単すぎる. 少し古い本だからといって,こんな内容でよく売る気になったものだ. 例えば,最初の三角形の問題.これは,簡単すぎてお話にならない.自分だったら,こうする. 問題 N 本の直線状の棒がある.棒 i の長さは a i である. あなたは,それらの棒から 6 本を選び, それらの 3 本ずつで,2 個の三角形を作ろうと考えている. 3 本の棒はそれぞれ三角形の辺として用い,2 つの棒が触れる位置は棒の端点のみとする. つまり,棒の一部を三角形の辺として使うことは許されず,必ず棒全体を辺としなければならない. また,棒の太さは考えず,三角形は正の面積を持たなければならないものとする. 2 個の三角形の周長の和の最大値を求めよ. ただし,2 個の三角形を作ることができない際には 0 を答えとせよ. 入力 入力の最初の行には整数 N が書かれている. 続く N 行の i 行目には 1 つの整数 a i が書かれている. 出力 答えの整数を出力せよ. 制約 1 ≤ N ≤ 10 5 1 ≤ a i ≤ 10 15 部分点 この問題の判定には,20 点分のテストケースのグループが設定されている. このグループに含まれるテストケースの入力は以下を満たす. 1 ≤ N ≤ 100 入出力例 入力例: 6 1 1 1 1 1 1 入力例に対する出力: 6
[ { "submission_id": "aoj_2265_10042900", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define int long long\n\nbool check3(vector<int> a) {\n //for (int i: a) cout << i << ' '; cout << endl;\n\tsort(a.begin(), a.end());\n\treturn a[2] < a[1] + a[0];\n}\n\nbool check6(vector<int> a) {\n //for (int i: a) cout << i << ' '; cout << endl;\n sort(a.begin(), a.end());\n\tfor (int i = 2; i < 5; i++) {\n\t\tvector<int> p;\n\t\tfor (int j = 0; j < 5; j++) {\n\t\t if (i != j) p.push_back(j);\n\t\t}\n\t\t\n\t\tdo {\n\t\t vector<int> s(2); \n\t\t if (p[0] < i && p[1] < i) s[0] = a[p[0]] + a[p[1]] - a[i];\n\t\t if (p[2] < 5 && p[3] < 5) s[1] = a[p[2]] + a[p[3]] - a[5];\n\t\t \n\t\t if (s[0] > 0 && s[1] > 0) return true;\n\t\t} while (next_permutation(p.begin(), p.end()));\n\t}\n\treturn false;\n}\n\nsigned main() {\n\tios::sync_with_stdio(0); cin.tie(0);\n\n\tint n; cin >> n;\n\tvector<int> a(n);\n\tfor (int i = 0; i < n; i++) {\n\t\tcin >> a[i];\n\t}\n\tsort(a.begin(), a.end());\n\t\n\tvector<int> ans2;\n\tfor (int i = 0; i+5 < n; i++) {\n\t vector<int> tmp;\n\t for (int j = 0; j < 6; j++) {\n\t tmp.push_back(a[i+j]);\n\t }\n\t if (check6(tmp)) {\n\t ans2 = tmp;\n\t }\n\t}\n\t\n\tvector<int> ans;\n\n\twhile (a.size() >= 3) {\n\t\tans.push_back(a.back()); a.pop_back();\n\t\tans.push_back(a.back()); a.pop_back();\n\t\tans.push_back(a.back()); a.pop_back();\n\n\t\tif (check3(ans)) break;\n\t\telse {\n\t\t\ta.push_back(ans.back()); ans.pop_back();\n\t\t\ta.push_back(ans.back()); ans.pop_back();\n\t\t\tans.pop_back();\n\t\t}\n\t}\n\n\twhile (a.size() >= 3) {\n\t\tans.push_back(a.back()); a.pop_back();\n\t\tans.push_back(a.back()); a.pop_back();\n\t\tans.push_back(a.back()); a.pop_back();\n\n\t\tif (check6(ans)) break;\n\t\telse {\n\t\t\ta.push_back(ans.back()); ans.pop_back();\n\t\t\ta.push_back(ans.back()); ans.pop_back();\n\t\t\tans.pop_back();\n\t\t}\n\t}\n\n\tcout << max(accumulate(ans.begin(), ans.end(), 0LL), accumulate(ans2.begin(), ans2.end(), 0LL)) << '\\n';\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3880, "score_of_the_acc": -0.3994, "final_rank": 11 }, { "submission_id": "aoj_2265_10042798", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\ntypedef long long ll;\n\nint main(){\n ios::sync_with_stdio(false);\n cin.tie(0);\n \n ll N;\n cin >> N;\n vector<ll> a(N);\n for(auto &x: a) cin >> x;\n \n // Сортируем в неубывающем порядке\n sort(a.begin(), a.end(), [](const ll &x, const ll &y) -> bool{\n return x > y;\n });\n \n // Если палочек меньше 6, выводим 0\n if(N < 6){\n cout << 0 << endl;\n return 0;\n }\n \n // Выбираем первые K палочек\n ll K = min((ll)30, N);\n vector<ll> topK(a.begin(), a.begin() + K);\n \n // Перебираем все возможные тройки и сохраняем валидные\n struct Triple {\n int i, j, k;\n ll sum;\n };\n \n vector<Triple> triples;\n \n for(int i = 0; i < K-2; ++i){\n for(int j = i+1; j < K-1; ++j){\n for(int k = j+1; k < K; ++k){\n if(topK[j] + topK[k] > topK[i]){\n triples.push_back(Triple{ i, j, k, topK[i] + topK[j] + topK[k] });\n }\n }\n }\n }\n \n // Если меньше двух тройк, невозможно\n if(triples.size() < 2){\n // Возможно, стоит проверить дальше, но для упрощения ограничимся K=30\n cout << 0 << endl;\n return 0;\n }\n \n // Ищем максимальную сумму двух не пересекающихся тройк\n ll max_sum = 0;\n for(int i = 0; i < triples.size(); ++i){\n for(int j = i+1; j < triples.size(); ++j){\n // Проверяем, не пересекаются ли тройки\n int a1 = triples[i].i;\n int b1 = triples[i].j;\n int c1 = triples[i].k;\n \n int a2 = triples[j].i;\n int b2 = triples[j].j;\n int c2 = triples[j].k;\n \n // Проверяем уникальность индексов\n if(a1 != a2 && a1 != b2 && a1 != c2 &&\n b1 != a2 && b1 != b2 && b1 != c2 &&\n c1 != a2 && c1 != b2 && c1 != c2){\n ll total = triples[i].sum + triples[j].sum;\n if(total > max_sum){\n max_sum = total;\n }\n }\n }\n }\n \n // Выводим результат\n cout << max_sum << endl;\n}", "accuracy": 0.136986301369863, "time_ms": 10, "memory_kb": 3592, "score_of_the_acc": -0.3261, "final_rank": 19 }, { "submission_id": "aoj_2265_10042791", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\ntypedef long long ll;\n\nint main(){\n ios::sync_with_stdio(false);\n cin.tie(0);\n \n ll N;\n cin >> N;\n vector<ll> a(N);\n for(auto &x: a) cin >> x;\n \n // Сортируем в неубывающем порядке\n sort(a.begin(), a.end(), [](const ll &x, const ll &y) -> bool{\n return x > y;\n });\n \n // Если палочек меньше 6, выводим 0\n if(N < 6){\n cout << \"0\";\n return 0;\n }\n \n // Выбираем первые K палочек\n ll K = min((ll)30, N);\n vector<ll> topK(a.begin(), a.begin() + K);\n \n // Перебираем все возможные тройки и сохраняем валидные\n struct Triple {\n int i, j, k;\n ll sum;\n };\n \n vector<Triple> triples;\n \n for(int i = 0; i < K-2; ++i){\n for(int j = i+1; j < K-1; ++j){\n for(int k = j+1; k < K; ++k){\n if(topK[j] + topK[k] > topK[i]){\n triples.push_back(Triple{ i, j, k, topK[i] + topK[j] + topK[k] });\n }\n }\n }\n }\n \n // Если меньше двух тройк, невозможно\n if(triples.size() < 2){\n // Возможно, стоит проверить дальше, но для упрощения ограничимся K=30\n cout << \"0\";\n return 0;\n }\n \n // Ищем максимальную сумму двух не пересекающихся тройк\n ll max_sum = 0;\n for(int i = 0; i < triples.size(); ++i){\n for(int j = i+1; j < triples.size(); ++j){\n // Проверяем, не пересекаются ли тройки\n int a1 = triples[i].i;\n int b1 = triples[i].j;\n int c1 = triples[i].k;\n \n int a2 = triples[j].i;\n int b2 = triples[j].j;\n int c2 = triples[j].k;\n \n // Проверяем уникальность индексов\n if(a1 != a2 && a1 != b2 && a1 != c2 &&\n b1 != a2 && b1 != b2 && b1 != c2 &&\n c1 != a2 && c1 != b2 && c1 != c2){\n ll total = triples[i].sum + triples[j].sum;\n if(total > max_sum){\n max_sum = total;\n }\n }\n }\n }\n \n // Выводим результат\n cout << max_sum << endl;\n}", "accuracy": 0.1232876712328767, "time_ms": 10, "memory_kb": 3588, "score_of_the_acc": -0.3254, "final_rank": 20 }, { "submission_id": "aoj_2265_9128309", "code_snippet": "#pragma GCC optimize(\"Ofast\")\n#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n\nmt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());\nll myRand(ll B) { return (ull)rng() % B; }\n\nint main() {\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n int n;\n cin >> n;\n vector<ll> a(n);\n for (int i = 0; i < n; i++) {\n cin >> a[i];\n }\n sort(a.begin(), a.end());\n ll res = 0;\n auto can = [&](ll x, ll y, ll z) -> ll {\n if (x + y > z) return x + y + z;\n else return -1e18;\n };\n {\n ll mx = -1e18;\n for (int i = 5; i < n; i++) {\n mx = max(mx, can(a[i - 5], a[i - 4], a[i - 3]));\n ll u = can(a[i - 2], a[i - 1], a[i]);\n res = max(res, u + mx);\n }\n }\n {\n for (int i = 0; i + 5 < n; i++) {\n res = max(res, can(a[i], a[i + 1], a[i + 3]) + can(a[i + 2], a[i + 4], a[i + 5]));\n res = max(res, can(a[i], a[i + 1], a[i + 4]) + can(a[i + 2], a[i + 3], a[i + 5]));\n res = max(res, can(a[i], a[i + 1], a[i + 5]) + can(a[i + 2], a[i + 3], a[i + 4]));\n res = max(res, can(a[i], a[i + 2], a[i + 3]) + can(a[i + 1], a[i + 4], a[i + 5]));\n res = max(res, can(a[i], a[i + 2], a[i + 4]) + can(a[i + 1], a[i + 3], a[i + 5]));\n res = max(res, can(a[i], a[i + 2], a[i + 5]) + can(a[i + 1], a[i + 3], a[i + 4]));\n res = max(res, can(a[i], a[i + 3], a[i + 4]) + can(a[i + 1], a[i + 2], a[i + 5]));\n res = max(res, can(a[i], a[i + 3], a[i + 5]) + can(a[i + 1], a[i + 2], a[i + 4]));\n res = max(res, can(a[i], a[i + 4], a[i + 5]) + can(a[i + 1], a[i + 2], a[i + 3]));\n }\n }\n cout << res << endl;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3896, "score_of_the_acc": -0.3811, "final_rank": 9 }, { "submission_id": "aoj_2265_8965525", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll=int64_t;\n\nint main() {\n int n;\n cin >> n;\n if(n<6){\n cout << 0 << endl;\n return 0;\n }\n \n priority_queue<ll> pq;\n ll a;\n \n for(int i=0;i<n;i++){\n cin >> a;\n pq.push(a);\n }\n \n ll ans1=0,ans=0;\n deque<ll> san; \n int cnt=0,num=0;\n while(!pq.empty()){\n //cout << pq.size() << \" \" << cnt << \" \" << num << endl;\n if(num==0){\n if(cnt<3){ \n san.push_back(pq.top());\n pq.pop();\n cnt++;\n }\n if(cnt==3){\n if(san[0]<san[1]+san[2]){\n ans1=san[0]+san[1]+san[2];\n //cout << ans1 << \" \" << pq.size() << endl;\n cnt=0;\n num++;\n }else{\n san.pop_front();\n cnt--;\n }\n }\n }else{\n if(cnt<3){ \n san.push_back(pq.top());\n pq.pop();\n cnt++;\n }\n if(cnt==3){\n if((san[3]<san[4]+san[5])||\n ((san[1]<san[2]+san[5])&&(san[0]<san[3]+san[4]))||\n ((san[2]<san[3]+san[5])&&(san[0]<san[1]+san[4]))||\n ((san[2]<san[3]+san[4])&&(san[0]<san[1]+san[5]))||\n ((san[2]<san[4]+san[5])&&(san[0]<san[1]+san[3]))||\n ((san[1]<san[4]+san[5])&&(san[0]<san[2]+san[3]))||\n ((san[1]<san[3]+san[5])&&(san[0]<san[2]+san[4]))||\n ((san[1]<san[3]+san[4])&&(san[0]<san[2]+san[5]))||\n ((san[1]<san[2]+san[4])&&(san[0]<san[3]+san[5]))||\n ((san[1]<san[3]+san[4])&&(san[0]<san[4]+san[5]))){\n ans=ans1+san[3]+san[4]+san[5];\n //for(int i=0;i<6;i++) cout << san[i] << endl; \n }else{\n san[3]=san[4];\n san[4]=san[5];\n san[5]=pq.top();\n pq.pop();\n if(san[3]<san[4]+san[5]) ans=ans1+san[3]+san[4]+san[5];\n }\n }\n \n }\n if(ans>0) break;\n }\n \n cout << max(ans,ans1) << endl;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 4204, "score_of_the_acc": -0.4793, "final_rank": 12 }, { "submission_id": "aoj_2265_5114777", "code_snippet": "#include <bits/stdc++.h>\n//#include <atcoder/all>\n//using namespace atcoder;\n#pragma GCC target (\"avx2\")\n#pragma GCC optimization (\"O3\")\n#pragma GCC optimization (\"unroll-loops\")\n\nusing namespace std;\n\ntypedef vector<int> VI;\ntypedef vector<VI> VVI;\ntypedef vector<string> VS;\ntypedef pair<int, int> PII;\ntypedef pair<int, int> pii;\ntypedef pair<long long, long long> PLL;\ntypedef pair<int, PII> TIII;\n\ntypedef long long ll;\ntypedef long double ld;\ntypedef unsigned long long ull;\n\n\n#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)\n#define REP(i, n) FOR(i, 0, n)\n#define rep(i, a, b) for (int i = a; i < (b); ++i)\n#define trav(a, x) for (auto &a : x)\n#define all(x) x.begin(), x.end()\n\n#define MOD 1000000007\n\ntemplate<class T1, class T2> inline bool chmax(T1 &a, T2 b) {if (a < b) {a = b; return true;} return false;}\ntemplate<class T1, class T2> inline bool chmin(T1 &a, T2 b) {if (a > b) {a = b; return true;} return false;}\nconst double EPS = 1e-9, PI = acos(-1);\nconst double pi = 3.141592653589793238462643383279;\n \ntypedef string::const_iterator State;\nll GCD(ll a, ll b){\n return (b==0)?a:GCD(b, a%b);\n}\nll LCM(ll a, ll b){\n return a/GCD(a, b) * b;\n}\ntemplate< int mod >\nstruct ModInt {\n int x;\n\n ModInt() : x(0) {}\n\n ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}\n\n ModInt &operator+=(const ModInt &p) {\n if((x += p.x) >= mod) x -= mod;\n return *this;\n }\n\n ModInt &operator-=(const ModInt &p) {\n if((x += mod - p.x) >= mod) x -= mod;\n return *this;\n }\n\n ModInt &operator*=(const ModInt &p) {\n x = (int) (1LL * x * p.x % mod);\n return *this;\n }\n\n ModInt &operator/=(const ModInt &p) {\n *this *= p.inverse();\n return *this;\n }\n\n ModInt operator-() const { return ModInt(-x); }\n\n ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }\n\n ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }\n\n ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }\n\n ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }\n\n bool operator==(const ModInt &p) const { return x == p.x; }\n\n bool operator!=(const ModInt &p) const { return x != p.x; }\n\n ModInt inverse() const {\n int a = x, b = mod, u = 1, v = 0, t;\n while(b > 0) {\n t = a / b;\n swap(a -= t * b, b);\n swap(u -= t * v, v);\n }\n return ModInt(u);\n }\n\n ModInt pow(int64_t n) const {\n ModInt ret(1), mul(x);\n while(n > 0) {\n if(n & 1) ret *= mul;\n mul *= mul;\n n >>= 1;\n }\n return ret;\n }\n\n friend ostream &operator<<(ostream &os, const ModInt &p) {\n return os << p.x;\n }\n\n friend istream &operator>>(istream &is, ModInt &a) {\n int64_t t;\n is >> t;\n a = ModInt< mod >(t);\n return (is);\n }\n\n static int get_mod() { return mod; }\n};\n\nusing modint = ModInt< 998244353 >;\ntemplate< typename T >\nstruct Combination {\n vector< T > _fact, _rfact, _inv;\n\n Combination(int sz) : _fact(sz + 1), _rfact(sz + 1), _inv(sz + 1) {\n _fact[0] = _rfact[sz] = _inv[0] = 1;\n for(int i = 1; i <= sz; i++) _fact[i] = _fact[i - 1] * i;\n _rfact[sz] /= _fact[sz];\n for(int i = sz - 1; i >= 0; i--) _rfact[i] = _rfact[i + 1] * (i + 1);\n for(int i = 1; i <= sz; i++) _inv[i] = _rfact[i] * _fact[i - 1];\n }\n\n inline T fact(int k) const { return _fact[k]; }\n\n inline T rfact(int k) const { return _rfact[k]; }\n\n inline T inv(int k) const { return _inv[k]; }\n\n T P(int n, int r) const {\n if(r < 0 || n < r) return 0;\n return fact(n) * rfact(n - r);\n }\n\n T C(int p, int q) const {\n if(q < 0 || p < q) return 0;\n return fact(p) * rfact(q) * rfact(p - q);\n }\n\n T H(int n, int r) const {\n if(n < 0 || r < 0) return (0);\n return r == 0 ? 1 : C(n + r - 1, r);\n }\n};\nstruct UnionFind{\n vector<int> par;\n vector<int> siz;\n\n UnionFind(int sz_): par(sz_), siz(sz_) {\n for(int i=0; i<sz_; ++i) par[i] = i, siz[i] = 1;\n }\n\n void init(int sz_){\n par.resize(sz_);\n siz.resize(sz_);\n for(int i=0; i<sz_; ++i) par[i] = i, siz[i] = 1;\n }\n\n int root(int x){\n if(x == par[x]) return x;\n return par[x] = root(par[x]);\n }\n\n bool merge(int x, int y){\n x = root(x), y = root(y);\n if(x == y) return false;\n if(siz[x] < siz[y]) swap(x, y);\n siz[x] += siz[y];\n par[y] = x;\n return true;\n }\n\n bool issame(int x, int y){\n return root(x) == root(y);\n }\n\n int size(int x){\n return siz[root(x)];\n }\n};\nbool used[101010];\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(12);\n \n int N; cin >> N;\n vector<ll> a(N);\n REP(i,N) cin >> a[i];\n sort(all(a));\n ll t1=-1, t2=-1;\n ll ans = 0;\n for(int i=N-1; i>=2; i--){\n if(used[i]) continue;\n ll e1 = a[i], e2 = a[i-1], e3 = a[i-2];\n if(e2+e3 > e1) {\n if(t1 == -1){\n t1 = e1+e2+e3;\n used[i] = used[i-1] = used[i-2] = true;\n }else{\n t2 = e1+e2+e3;\n break;\n }\n }\n }\n if(t2 != -1) ans = t1+t2;\n for(int i=N-1; i>=5; i--){\n vector<ll> v{a[i-5], a[i-4], a[i-3], a[i-2], a[i-1], a[i]};\n ll sum = accumulate(all(v), 0LL);\n\n vector<bool> ok(1<<6);\n REP(bit,(1<<6)){\n if(__builtin_popcount(bit) != 3) continue;\n vector<ll> e;\n for(int j=0; j<6; j++){\n if(bit >> j & 1) e.push_back(v[j]);\n }\n\n if(e[0]+e[1]>e[2] && e[0]+e[2]>e[1] && e[1]+e[2]>e[0]){\n ok[bit] = true;\n }\n }\n REP(bit,(1<<6)){\n if(__builtin_popcount(bit) != 3) continue;\n if(ok[bit] && ok[((1<<6)-1) & ~bit]){\n ans = max(ans, sum);\n break;\n } \n }\n }\n cout << ans << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 180, "memory_kb": 3624, "score_of_the_acc": -0.6936, "final_rank": 15 }, { "submission_id": "aoj_2265_4796231", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define int long long\n\nbool check3(vector<int> a) {\n //for (int i: a) cout << i << ' '; cout << endl;\n\tsort(a.begin(), a.end());\n\treturn a[2] < a[1] + a[0];\n}\n\nbool check6(vector<int> a) {\n //for (int i: a) cout << i << ' '; cout << endl;\n sort(a.begin(), a.end());\n\tfor (int i = 2; i < 5; i++) {\n\t\tvector<int> p;\n\t\tfor (int j = 0; j < 5; j++) {\n\t\t if (i != j) p.push_back(j);\n\t\t}\n\t\t\n\t\tdo {\n\t\t vector<int> s(2); \n\t\t s[0] = a[p[0]] + a[p[1]] - a[i];\n\t\t s[1] = a[p[2]] + a[p[3]] - a[5];\n\t\t \n\t\t if (s[0] > 0 && s[1] > 0) return true;\n\t\t} while (next_permutation(p.begin(), p.end()));\n\t}\n\treturn false;\n}\n\nsigned main() {\n\tios::sync_with_stdio(0); cin.tie(0);\n\n\tint n; cin >> n;\n\tvector<int> a(n);\n\tfor (int i = 0; i < n; i++) {\n\t\tcin >> a[i];\n\t}\n\tsort(a.begin(), a.end());\n\t\n\tvector<int> ans2;\n\tfor (int i = 0; i+5 < n; i++) {\n\t vector<int> tmp;\n\t for (int j = 0; j < 6; j++) {\n\t tmp.push_back(a[i+j]);\n\t }\n\t if (check6(tmp)) {\n\t ans2 = tmp;\n\t }\n\t}\n\t\n\tvector<int> ans;\n\n\twhile (a.size() >= 3) {\n\t\tans.push_back(a.back()); a.pop_back();\n\t\tans.push_back(a.back()); a.pop_back();\n\t\tans.push_back(a.back()); a.pop_back();\n\n\t\tif (check3(ans)) break;\n\t\telse {\n\t\t\ta.push_back(ans.back()); ans.pop_back();\n\t\t\ta.push_back(ans.back()); ans.pop_back();\n\t\t\tans.pop_back();\n\t\t}\n\t}\n\n\twhile (a.size() >= 3) {\n\t\tans.push_back(a.back()); a.pop_back();\n\t\tans.push_back(a.back()); a.pop_back();\n\t\tans.push_back(a.back()); a.pop_back();\n\n\t\tif (check6(ans)) break;\n\t\telse {\n\t\t\ta.push_back(ans.back()); ans.pop_back();\n\t\t\ta.push_back(ans.back()); ans.pop_back();\n\t\t\tans.pop_back();\n\t\t}\n\t}\n\n\tcout << max(accumulate(ans.begin(), ans.end(), 0LL), accumulate(ans2.begin(), ans2.end(), 0LL)) << '\\n';\n\n\treturn 0;\n}", "accuracy": 0.6438356164383562, "time_ms": 30, "memory_kb": 3552, "score_of_the_acc": -0.3614, "final_rank": 18 }, { "submission_id": "aoj_2265_2660468", "code_snippet": "#include <stdio.h>\n#include <cmath>\n#include <algorithm>\n#include <cfloat>\n#include <stack>\n#include <queue>\n#include <vector>\n#include <string>\n#include <iostream>\n#include <set>\n#include <map>\n#include <time.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\nint calc_left[20][3],calc_right[20][3];\n\nbool check(ll a,ll b, ll c){\n\treturn a+b > c;\n}\n\nvoid divideGroup(){\n\n\tint index = 0,left_index,right_index;\n\tint left_table[6],right_table[6];\n\n\tfor(int i = 0; i < 64; i++){\n\t\tleft_index = right_index = 0;\n\t\tfor(int loop = 0; loop < 6; loop++){\n\t\t\tif(i & (1 << loop)){\n\t\t\t\tleft_table[left_index++] = loop;\n\t\t\t}else{\n\t\t\t\tright_table[right_index++] = loop;\n\t\t\t}\n\t\t}\n\t\tif(left_index != 3 || right_index != 3)continue;\n\n\t\tfor(int k = 0; k < 3; k++){\n\t\t\tcalc_left[index][k] = left_table[k];\n\t\t\tcalc_right[index][k] = right_table[k];\n\t\t}\n\t\tindex++;\n\t}\n\n}\n\nint main(){\n\n\tdivideGroup();\n\n\tint N;\n\tscanf(\"%d\",&N);\n\n\tll* table = new ll[N];\n\tfor(int i = 0; i < N; i++)scanf(\"%lld\",&table[i]);\n\n\tsort(table,table+N);\n\n\tll* max_3seq = new ll[N];\n\tfor(int i = 0; i < N; i++)max_3seq[i] = 0;\n\n\tfor(int i = 0; i <= N-3; i++){\n\t\tif(check(table[i],table[i+1],table[i+2])){\n\t\t\tmax_3seq[i] = table[i]+table[i+1]+table[i+2];\n\t\t}else{\n\t\t\tif(i > 0){\n\t\t\t\tmax_3seq[i] = max_3seq[i-1];\n\t\t\t}\n\t\t}\n\t}\n\n\tll ans = 0;\n\tint debug;\n\tfor(int i = 0; i+5 <= N-1; i++){\n\t\tif(check(table[i+3],table[i+4],table[i+5])){\n\t\t\tans = max(ans,max_3seq[i]+table[i+3]+table[i+4]+table[i+5]);\n\t\t}\n\t\tbool FLG = false;\n\n\t\tfor(int k = 0; k < 20; k++){\n\t\t\tif(check(table[i+calc_left[k][0]],table[i+calc_left[k][1]],table[i+calc_left[k][2]]) == true &&\n\t\t\t\t\tcheck(table[i+calc_right[k][0]],table[i+calc_right[k][1]],table[i+calc_right[k][2]]) == true){\n\t\t\t\tFLG = true;\n\t\t\t\tdebug = k;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\n\t\tif(FLG){\n\t\t\tans = max(ans,table[i]+table[i+1]+table[i+2]+table[i+3]+table[i+4]+table[i+5]);\n\t\t}\n\t}\n\n\n\tprintf(\"%lld\\n\",ans);\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 4852, "score_of_the_acc": -0.5751, "final_rank": 13 }, { "submission_id": "aoj_2265_2552336", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))\n#define all(x) (x).begin(),(x).end()\n#define pb push_back\n#define fi first\n#define se second\n#define dbg(x) cout<<#x\" = \"<<((x))<<endl\ntemplate<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<\"(\"<<p.fi<<\",\"<<p.se<<\")\";return o;}\ntemplate<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<\"[\";for(T t:v){o<<t<<\",\";}o<<\"]\";return o;}\n\nbool tri(const vector<ll> &x)\n{\n return x[0]+x[1]>x[2];\n}\n\nbool check(const vector<ll> &v)\n{\n // dbg(v);\n int comb = (1<<3) - 1;\n while(comb < (1<<6)-1)\n {\n vector<ll> p,q;\n rep(i,6)\n {\n if(comb>>i&1) p.pb(v[i]);\n else q.pb(v[i]);\n }\n // dbg(p);dbg(q);\n if(tri(p) && tri(q)) return true;\n\n int x = comb & -comb, y = comb + x;\n comb = ((comb & ~y) / x>>1) | y;\n }\n return false;\n}\n\nll SUM(const vector<ll> &v)\n{\n ll ret = 0;\n for(ll i:v) ret += i;\n return ret;\n}\n\nint main()\n{\n int n;\n cin >>n;\n vector<ll> a(n);\n rep(i,n) cin >>a[i];\n sort(all(a));\n\n vector<int> ok;\n for(int i=2; i<n; ++i)\n {\n vector<ll> v;\n rep(j,3) v.pb(a[i-2+j]);\n if(tri(v)) ok.pb(i);\n }\n\n ll ans = 0;\n rep(i,n-5)\n {\n vector<ll> v;\n rep(j,6) v.pb(a[i+j]);\n if(check(v)) ans = max(ans,SUM(v));\n }\n\n if(ok.size()>0)\n {\n for(int i=n-1; i>=2; --i)\n {\n if(*lower_bound(all(ok),i) == i)\n {\n int idx = lower_bound(all(ok),i-2)-ok.begin();\n --idx;\n if(idx>=0)\n {\n vector<ll> v;\n rep(j,3) v.pb(a[ok[idx]-2+j]);\n rep(j,3) v.pb(a[i-2+j]);\n ans = max(ans,SUM(v));\n }\n }\n }\n }\n \n cout << ans << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 90, "memory_kb": 4328, "score_of_the_acc": -0.6294, "final_rank": 14 }, { "submission_id": "aoj_2265_2335860", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef pair<ll, ll> P;\n\n#define each(i,a) for (auto&& i : a)\n#define FOR(i,a,b) for (ll i=(a),__last_##i=(b);i<__last_##i;i++)\n#define RFOR(i,a,b) for (ll i=(b)-1,__last_##i=(a);i>=__last_##i;i--)\n#define REP(i,n) FOR(i,0,n)\n#define RREP(i,n) RFOR(i,0,n)\n#define __GET_MACRO3(_1, _2, _3, NAME, ...) NAME\n#define rep(...) __GET_MACRO3(__VA_ARGS__, FOR, REP)(__VA_ARGS__)\n#define rrep(...) __GET_MACRO3(__VA_ARGS__, RFOR, RREP)(__VA_ARGS__)\n#define pb push_back\n#define all(a) (a).begin(),(a).end()\n#define chmin(x,v) x = min(x, v)\n#define chmax(x,v) x = max(x, v)\n\nconst ll linf = 1e18;\nconst int inf = 1e9;\nconst double eps = 1e-12;\nconst double pi = acos(-1);\n\ntemplate<typename T>\nistream& operator>>(istream& is, vector<T>& vec) {\n each(x,vec) is >> x;\n return is;\n}\ntemplate<typename T>\nostream& operator<<(ostream& os, const vector<T>& vec) {\n rep(i,vec.size()) {\n if (i) os << \" \";\n os << vec[i];\n }\n return os;\n}\ntemplate<typename T>\nostream& operator<<(ostream& os, const vector< vector<T> >& vec) {\n rep(i,vec.size()) {\n if (i) os << endl;\n os << vec[i];\n }\n return os;\n}\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(0);\n ll n; cin >> n;\n vector<ll> v(n); cin >> v;\n sort(all(v), greater<ll>());\n ll ans = 0;\n { // xxxooo\n ll a = 0, k = 0;\n rep(i, n-2) {\n if (v[i] < v[i+1] + v[i+2]) {\n a += v[i] + v[i+1] + v[i+2];\n i += 2;\n if (++k == 2) {\n chmax(ans, a);\n }\n }\n }\n }\n {\n rep(i, n-5) {\n if (v[i] < v[i+1] + v[i+5] && v[i+2] < v[i+3] + v[i+4] // xxooox\n || v[i] < v[i+1] + v[i+4] && v[i+2] < v[i+3] + v[i+5] // xxooxo\n || v[i] < v[i+3] + v[i+4] && v[i+1] < v[i+2] + v[i+5]) { // xooxxo\n ll sum = 0;\n rep(j, 6) sum += v[i+j];\n chmax(ans, sum);\n }\n }\n }\n cout << ans << endl;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3620, "score_of_the_acc": -0.3312, "final_rank": 8 }, { "submission_id": "aoj_2265_2040954", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nint n; long long a[100009];\nlong long solve(int l, int r) {\n\tlong long ret = 0;\n\tfor(int i = 0; i < 1 << (r - l); i++) {\n\t\tif(__builtin_popcount(i) != 3) continue;\n\t\tvector<long long> x1, x2;\n\t\tfor(int j = 0; j < 6; j++) {\n\t\t\tif(i & (1 << j)) x1.push_back(a[l + j]);\n\t\t\telse x2.push_back(a[l + j]);\n\t\t}\n\t\tif(x1[0] + x1[1] > x1[2] && x2[0] + x2[1] > x2[2]) {\n\t\t\tret = max(ret, x1[0] + x1[1] + x1[2] + x2[0] + x2[1] + x2[2]);\n\t\t}\n\t}\n\treturn ret;\n}\nint main() {\n\tcin >> n;\n\tfor(int i = 0; i < n; i++) cin >> a[i];\n\tsort(a, a + n);\n\tint pos = -1;\n\tfor(int i = n - 3; i >= 0; i--) {\n\t\tif(a[i] + a[i + 1] > a[i + 2]) {\n\t\t\tpos = i; break;\n\t\t}\n\t}\n\tint pos2 = -1;\n\tfor(int i = pos - 3; i >= 0; i--) {\n\t\tif(a[i] + a[i + 1] > a[i + 2]) {\n\t\t\tpos2 = i; break;\n\t\t}\n\t}\n\tlong long ret = (pos2 != -1 ? a[pos2] + a[pos2 + 1] + a[pos2 + 2] + a[pos] + a[pos + 1] + a[pos + 2] : 0);\n\tfor(int i = 0; i < n - 6; i++) {\n\t\tret = max(ret, solve(i, i + 6));\n\t}\n\tcout << ret << endl;\n\treturn 0;\n}", "accuracy": 1, "time_ms": 480, "memory_kb": 3952, "score_of_the_acc": -1.3912, "final_rank": 17 }, { "submission_id": "aoj_2265_1697342", "code_snippet": "#include<iostream>\n#include<algorithm>\nusing namespace std;\n#define MAX_N 500000\n#define MAX_S 600\nlong long x[MAX_N];\nlong long TOP[MAX_S], n;\nlong long maxn1[MAX_N], maxn2[MAX_N];\nint main() {\n\tcin >> n;\n\tfor (int i = 0; i < n; i++) {\n\t\tcin >> x[i];\n\t}\n\tsort(x, x + n);\n\tlong long cnt = 0;\n\tfor (int i = n - MAX_S; i < n; i++) {\n\t\tif (i >= 0) {\n\t\t\tTOP[cnt] = x[i];\n\t\t\tcnt++;\n\t\t}\n\t}\n\tlong long maxn = 0;\n\tfor (int i = 0; i < cnt; i++) {\n\t\tfor (int j = i + 1; j < cnt; j++) {\n\t\t\tfor (int k = j + 1; k < cnt; k++) {\n\t\t\t\tif (TOP[i] + TOP[j] > TOP[k]) {\n\t\t\t\t\tmaxn1[i] = max(maxn1[i], TOP[i] + TOP[j] + TOP[k]);\n\t\t\t\t\tmaxn2[k] = max(maxn2[k], TOP[i] + TOP[j] + TOP[k]);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tfor (int i = 0; i < cnt; i++) {\n\t\tfor (int j = i + 1; j < cnt; j++) {\n\t\t\tmaxn = max(maxn, maxn2[i] + maxn1[j]);\n\t\t}\n\t}\n\n\tfor (int i = 0; i < cnt - 5; i++) {\n\t\tlong long c[6], d[3], e[3];\n\t\tfor (int j = 0; j < 6; j++) {\n\t\t\tc[j] = TOP[i + j];\n\t\t}\n\t\tlong long cnt = 0, cnt1 = 0, cnt2 = 0;\n\t\tfor (int j = 0; j < 64; j++) {\n\t\t\tcnt = 0;\n\t\t\tfor (int k = 0; k < 6; k++) { cnt += (j / (1 << k)) % 2; }\n\t\t\tif (cnt == 3) {\n\t\t\t\tcnt1 = 0; cnt2 = 0;\n\t\t\t\tfor (int k = 0; k < 6; k++) {\n\t\t\t\t\tif ((j / (1 << k)) % 2 == 0) {\n\t\t\t\t\t\td[cnt1] = c[k];\n\t\t\t\t\t\tcnt1++;\n\t\t\t\t\t}\n\t\t\t\t\telse {\n\t\t\t\t\t\te[cnt2] = c[k];\n\t\t\t\t\t\tcnt2++;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif (d[0] + d[1]>d[2] && e[0] + e[1]>e[2]) {\n\t\t\t\t\tmaxn = max(maxn, d[0] + d[1] + d[2] + e[1] + e[2] + e[0]);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tcout << maxn << endl;\n\treturn 0;\n}", "accuracy": 1, "time_ms": 150, "memory_kb": 7320, "score_of_the_acc": -1.2979, "final_rank": 16 }, { "submission_id": "aoj_2265_1480718", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\ntypedef long long ll;\n\nint main() {\n\tint N; cin >> N;\n\tvector<ll> v(N);\n\tfor (int i = 0; i < N; ++i) {\n\t\tcin >> v[i];\n\t}\n\tsort( v.begin(), v.end() );\n\n\tll ans = 0;\n\n\t// 012345\n\t// oooxox\n\t// ooooxx\n\tfor (int t = N-6; t >= 0; --t) {\n\t\tvector<ll> a(6);\n\t\tfor (int i = 0; i < 6; ++i) {\n\t\t\ta[i] = v[t+i];\n\t\t}\n\n\t\tll sum = 0;\n\t\tfor (int i = 0; i < 6; ++i) {\n\t\t\tsum += a[i];\n\t\t}\n\n\t\tif ((a[0] + a[1] > a[3] && a[2] + a[4] > a[5])\n\t\t\t|| (a[0] + a[2] > a[3] && a[1] + a[4] > a[5])\n\t\t\t|| (a[1] + a[2] > a[3] && a[0] + a[4] > a[5])\n\t\t\t|| (a[0] + a[1] > a[4] && a[2] + a[3] > a[5])\n\t\t\t|| (a[0] + a[2] > a[4] && a[1] + a[3] > a[5])\n\t\t\t|| (a[0] + a[3] > a[4] && a[1] + a[2] > a[5])\n\t\t\t|| (a[0] + a[1] > a[5] && a[2] + a[3] > a[4])\n\t\t\t|| (a[0] + a[2] > a[5] && a[1] + a[3] > a[4])\n\t\t\t|| (a[0] + a[3] > a[5] && a[1] + a[2] > a[4])) {\n\t\t\tif (sum > ans) ans = sum;\n\t\t}\n\t}\n\t{\n\t\tll s = 0;\n\n\t\tint pos = -1;\n\t\tfor (int i = N-3; i >= 0; --i) {\n\t\t\tif (v[i] + v[i+1] > v[i+2]) {\n\t\t\t\ts = v[i] + v[i+1] + v[i+2];\n\t\t\t\tpos = i;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tif (pos > -1) {\n\t\t\tfor (int i = pos-3; i >= 0; --i) {\n\t\t\t\tif (v[i] + v[i+1] > v[i+2]) {\n\t\t\t\t\tll s2 = s + v[i] + v[i+1] + v[i+2];\n\t\t\t\t\tif (s2 > ans) ans = s2;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tcout << ans << endl;\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 1796, "score_of_the_acc": -0.1291, "final_rank": 3 }, { "submission_id": "aoj_2265_1457721", "code_snippet": "#include<cstdio>\n#include<cstdlib>\n#include<cstring>\n#include<cmath>\n#include<iostream>\n#include<string>\n#include<vector>\n#include<map>\n#include<set>\n#include<list>\n#include<queue>\n#include<deque>\n#include<algorithm>\n#include<numeric>\n#include<utility>\n#include<complex>\n#include<functional>\n \nusing namespace std;\n\n/* constant */\n\nconst int MAX_N = 100000;\n\n/* typedef */\n\ntypedef long long ll;\n\n/* global variables */\n\nll as[MAX_N];\n\n/* subroutines */\n\n/* main */\n\nint main() {\n int n;\n cin >> n;\n\n for (int i = 0; i < n; i++) cin >> as[i];\n sort(as, as + n, greater<ll>());\n\n ll maxsum0 = 0, maxsum1 = 0;\n ll sumi, sumj;\n int i;\n \n for (i = 0; i < n - 5; i++)\n if (as[i] < as[i + 1] + as[i + 2]) {\n sumi = as[i] + as[i + 1] + as[i + 2];\n //printf(\"%d:%lld+%lld+%lld=%lld\\n\",i,as[i],as[i+1],as[i+2],sumi);\n break;\n }\n\n for (int j = i + 3; j < n - 2; j++)\n if (as[j] < as[j + 1] + as[j + 2]) {\n sumj = as[j] + as[j + 1] + as[j + 2];\n maxsum0 = sumi + sumj;\n //printf(\"%d:%lld+%lld+%lld=%lld\\n\",j,as[j],as[j+1],as[j+2],sumj);\n break;\n }\n\n ll sum = as[0] + as[1] + as[2] + as[3] + as[4];\n for (i = 0; i < n - 5; i++) {\n sum += as[i + 5];\n if ((as[i] < as[i + 1] + as[i + 2] && as[i + 3] < as[i + 4] + as[i + 5]) ||\n\t(as[i] < as[i + 1] + as[i + 3] && as[i + 2] < as[i + 4] + as[i + 5]) ||\n\t(as[i] < as[i + 1] + as[i + 4] && as[i + 2] < as[i + 3] + as[i + 5]) ||\n\t(as[i] < as[i + 1] + as[i + 5] && as[i + 2] < as[i + 3] + as[i + 4]) ||\n\t(as[i] < as[i + 2] + as[i + 3] && as[i + 1] < as[i + 4] + as[i + 5]) ||\n\t(as[i] < as[i + 2] + as[i + 4] && as[i + 1] < as[i + 3] + as[i + 5]) ||\n\t(as[i] < as[i + 2] + as[i + 5] && as[i + 1] < as[i + 3] + as[i + 4]) ||\n\t(as[i] < as[i + 3] + as[i + 4] && as[i + 1] < as[i + 2] + as[i + 5]) ||\n\t(as[i] < as[i + 3] + as[i + 5] && as[i + 1] < as[i + 2] + as[i + 4]) ||\n\t(as[i] < as[i + 4] + as[i + 5] && as[i + 1] < as[i + 2] + as[i + 3])) {\n maxsum1 = sum;\n break;\n }\n sum -= as[i];\n }\n \n cout << max(maxsum0, maxsum1) << endl;\n \n return 0;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 1964, "score_of_the_acc": -0.1382, "final_rank": 4 }, { "submission_id": "aoj_2265_1219466", "code_snippet": "#define _USE_MATH_DEFINES\n#include <algorithm>\n#include <cstdio>\n#include <functional>\n#include <iostream>\n#include <cfloat>\n#include <climits>\n#include <cstring>\n#include <cmath>\n#include <map>\n#include <queue>\n#include <set>\n#include <sstream>\n#include <stack>\n#include <string>\n#include <time.h>\n#include <vector>\nusing namespace std;\n\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef pair<int, int> i_i;\ntypedef pair<ll, int> ll_i;\ntypedef pair<double, int> d_i;\ntypedef pair<ll, ll> ll_ll;\ntypedef pair<double, double> d_d;\nstruct edge { int v, w; };\n\nll MOD = 1000000007;\nll _MOD = 1000000009;\ndouble EPS = 1e-10;\nint INF = INT_MAX / 4;\n\nint main() {\n\tint N; cin >> N;\n\tif (N < 6) {\n\t\tcout << 0 << endl;\n\t\treturn 0;\n\t}\n\tvector<ll> a(N);\n\tfor (int i = 0; i < N; i++)\n\t\tcin >> a[i];\n\tsort(a.begin(), a.end());\n\tvector<ll> b(N - 2);\n\tfor (int i = 0; i + 3 <= N; i++)\n\t\tif (a[i] + a[i + 1] > a[i + 2])\n\t\t\tb[i] = a[i] + a[i + 1] + a[i + 2];\n\tvector<ll> c(N - 1);\n\tc[0] = 0;\n\tfor (int i = 1; i - 1 < N - 2; i++)\n\t\tc[i] = max(c[i - 1], b[i - 1]);\n\tll maxi = 0;\n\tfor (int i = 3; i + 3 <= N; i++)\n\t\tif (c[i - 2] && b[i])\n\t\t\tmaxi = max(maxi, c[i - 2] + b[i]);\n\tfor (int i = N - 6; i >= 0; i--) {\n\t\tbool ok = false;\n\t\tfor (int S = 0; S < (1 << 6); S++) {\n\t\t\tvector<ll> v, w;\n\t\t\tfor (int j = 0; j < 6; j++)\n\t\t\t\tif ((S >> j) & 1) v.push_back(a[i + j]);\n\t\t\t\telse w.push_back(a[i + j]);\n\t\t\tif (v.size() == 3 && v[0] + v[1] > v[2] && w[0] + w[1] > w[2])\n\t\t\t\tok = true;\n\t\t}\n\t\tif (ok) {\n\t\t\tmaxi = max(maxi, a[i] + a[i + 1] + a[i + 2] + a[i + 3] + a[i + 4] + a[i + 5]);\n\t\t\tbreak;\n\t\t}\n\t}\n\tcout << maxi << endl;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 3384, "score_of_the_acc": -0.3949, "final_rank": 10 }, { "submission_id": "aoj_2265_1175334", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\n\ninline bool check(ll a, ll b, ll c){ return a<b+c; }\n\nint main(){\n ll n, a[100100];\n scanf(\"%lld\",&n);\n for(int i=0;i<n;i++)scanf(\"%lld\",a+i);\n sort(a,a+n,greater<ll>());\n\n ll ans = 0;\n for(int i=0;i<n-2;i++){\n if(check(a[i],a[i+1],a[i+2])){\n for(int j=i+3;j<n-2;j++){\n\tif(check(a[j],a[j+1],a[j+2])){\n\t ll sum = 0;\n\t for(int k=0;k<3;k++)sum += a[i+k] + a[j+k];\n\t ans = max(ans,sum);\n\t goto END1;\n\t}\n }\n }\n }\n END1:;\n\n for(int i=0;i<n-5;i++){\n int comb = (1<<3)-1;\n while(comb < (1<<5)){\n vector<ll> A,B;\n for(int j=0;j<6;j++){\n\tif( (comb>>j)&1 )A.push_back(a[i+j]);\n\telse B.push_back(a[i+j]);\n }\n\n if(check(A[0],A[1],A[2]) && check(B[0],B[1],B[2])){\n\tll sum = 0;\n\tfor(int k=0;k<6;k++)sum += a[i+k];\n\tans = max(ans,sum);\n\tgoto END2;\n }\n\n int x = comb & -comb, y = comb + x;\n comb = ( ( (comb & ~y) /x ) >> 1) | y;\n }\n }\n\n END2: cout << ans << endl;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 2016, "score_of_the_acc": -0.0838, "final_rank": 2 }, { "submission_id": "aoj_2265_1175331", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\n\ninline bool check(ll a, ll b, ll c){ return a<b+c; }\n\nint main(){\n ll n, a[100100];\n cin >> n;\n for(int i=0;i<n;i++)cin >> a[i];\n sort(a,a+n,greater<ll>());\n\n ll ans = 0;\n for(int i=0;i<n-2;i++){\n if(check(a[i],a[i+1],a[i+2])){\n for(int j=i+3;j<n-2;j++){\n\tif(check(a[j],a[j+1],a[j+2])){\n\t ll sum = 0;\n\t for(int k=0;k<3;k++)sum += a[i+k] + a[j+k];\n\t ans = max(ans,sum);\n\t goto END1;\n\t}\n }\n }\n }\n END1:;\n\n for(int i=0;i<n-5;i++){\n int comb = (1<<3)-1;\n while(comb < (1<<5)){\n vector<ll> A,B;\n for(int j=0;j<6;j++){\n\tif( (comb>>j)&1 )A.push_back(a[i+j]);\n\telse B.push_back(a[i+j]);\n }\n\n if(check(A[0],A[1],A[2]) && check(B[0],B[1],B[2])){\n\tll sum = 0;\n\tfor(int k=0;k<6;k++)sum += a[i+k];\n\tans = max(ans,sum);\n\tgoto END2;\n }\n\n int x = comb & -comb, y = comb + x;\n comb = ( ( (comb & ~y) /x ) >> 1) | y;\n }\n }\n\n END2: cout << ans << endl;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 1996, "score_of_the_acc": -0.144, "final_rank": 5 }, { "submission_id": "aoj_2265_1175330", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\n\ninline bool check(ll a, ll b, ll c){ return a<b+c; }\n\nint main(){\n ll n, a[100100];\n cin >> n;\n for(int i=0;i<n;i++)cin >> a[i];\n sort(a,a+n,greater<ll>());\n\n ll ans = 0;\n for(int i=0;i<n-2;i++){\n if(check(a[i],a[i+1],a[i+2])){\n for(int j=i+3;j<n-2;j++){\n\tif(check(a[j],a[j+1],a[j+2])){\n\t ll sum = 0;\n\t for(int k=0;k<3;k++)sum += a[i+k] + a[j+k];\n\t ans = max(ans,sum);\n\t goto END1;\n\t}\n }\n }\n }\n END1:;\n\n for(int i=0;i<n-5;i++){\n int comb = (1<<3)-1;\n while(comb < (1<<6)){\n vector<ll> A,B;\n for(int j=0;j<6;j++){\n\tif( (comb>>j)&1 )A.push_back(a[i+j]);\n\telse B.push_back(a[i+j]);\n }\n\n if(check(A[0],A[1],A[2]) && check(B[0],B[1],B[2])){\n\tll sum = 0;\n\tfor(int k=0;k<6;k++)sum += a[i+k];\n\tans = max(ans,sum);\n\tgoto END2;\n }\n\n int x = comb & -comb, y = comb + x;\n comb = ( ( (comb & ~y) /x ) >> 1) | y;\n }\n }\n\n END2: cout << ans << endl;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 1996, "score_of_the_acc": -0.144, "final_rank": 5 }, { "submission_id": "aoj_2265_1154447", "code_snippet": "#include<iostream>\n#include<algorithm>\n\nusing namespace std;\ntypedef long long Int;\n\n\n\nbool able(Int a, Int b, Int c){\n\tInt sum = a + b + c;\n\treturn a*2 < sum && b*2 < sum && c*2 < sum;\n}\n\nbool able2(Int a, Int b, Int c, Int d, Int e, Int f){\n\tif(able(a, b, c) && able(d, e, f))return true;\n\tif(able(a, b, d) && able(c, e, f))return true;\n\tif(able(a, b, e) && able(c, d, f))return true;\n\tif(able(a, b, f) && able(c, d, e))return true;\n\tif(able(a, c, d) && able(b, e, f))return true;\n\tif(able(a, c, e) && able(b, d, f))return true;\n\tif(able(a, c, f) && able(b, d, e))return true;\n\tif(able(a, d, e) && able(b, c, f))return true;\n\tif(able(a, d, f) && able(b, c, e))return true;\n\tif(able(a, e, f) && able(b, c, d))return true;\n\treturn false;\n}\n\n\nInt n, res;\nInt a[108000], right1 = -1, right2 = -1;\nbool ok[108000];\nint main(){\n\tcin >> n;\n\tfor(Int i = 0;i < n;i++) cin >> a[i];\n\tsort(a, a + n);\n\tfor(Int i = 2;i < n;i++){\n\t\tok[i] = able(a[i], a[i-1], a[i-2]);\n\t\tif(ok[i])right1 = i;\n\t}\n\tfor(Int i = 2;i <= right1 - 3;i++){\n\t\tif(ok[i])right2 = i;\n\t}\n\tif(right1 != -1 && right2 != -1)\n\t\tres = a[right1] + a[right1-1] + a[right1-2] + a[right2] + a[right2-1] + a[right2-2];\n\tfor(Int i = 5;i < n;i++){\n\t\tif(able2(a[i], a[i-1], a[i-2], a[i-3], a[i-4], a[i-5])){\n\t\t\tres = max(res, a[i] + a[i-1] + a[i-2] + a[i-3] + a[i-4] + a[i-5]);\n\t\t}\n\t}\n\tcout << res << endl;\n\treturn 0;\n\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 2064, "score_of_the_acc": -0.1563, "final_rank": 7 }, { "submission_id": "aoj_2265_1150214", "code_snippet": "//Name: Programming Contest Challenge Book\n//Level: 3\n//Category: 数学,Math,考察\n//Note:\n\n/**\n * 棒を長さの降順でソートしてから考える。\n * 以下、三角形の辺に関して長い方からp,q,rと呼ぶことにする。\n *\n * 三角形を1つ作るとき、棒iを辺pとして採用すると、三角不等式のうちp+q>rとp+r>qが満たされることは自明である。\n * q+r>pであるためにはbとcを可能な限り大きく取ればよいから、結局棒i+1とi+2を加えて三角形を構成できなければ、棒iを辺pとした三角形は構成できないことがわかる。\n *\n * この考察から、連続する3辺で三角形が作れるかを考えながら貪欲に構成していくという方法が思いつくが、これでは3 3 2 2 1 1のような列のときにうまくいかない。\n * すなわち、a_{i+3}≧a_{i+4}+a_{i+5}であるが、2つの三角形に割り当てる順番をうまく入れ替えてやるとちょうど割り当てられる場合である。\n * このような場合は、棒iから連続する6つの棒だけを使って2つの三角形を構成しないと意味がないため、全探索すればよい\n * (周長を最大化する制約から、この6つよりも短い棒を使っている場合、iを超えない範囲で長い棒に入れ替えることは安全であり、かつ得をする)。\n *\n * オーダーは O(N log N)。\n */\n#include <iostream>\n#include <algorithm>\n#include <vector>\n#include <numeric>\n\nusing namespace std;\n\nbool solve() {\n int N;\n if(!(cin >> N)) return false;\n\n vector<long long> as(N);\n for(int i = 0; i < N; ++i) {\n cin >> as[i];\n }\n sort(begin(as), end(as), greater<long long>());\n\n typedef vector<int> Choice;\n vector<pair<Choice,Choice>> choose_3;\n for(int pat = 0; pat < (1<<6); ++pat) {\n if(__builtin_popcount(pat) != 3) continue;\n Choice c1, c2;\n for(int i = 0; i < 6; ++i) {\n if(pat & (1<<i)) c1.push_back(i);\n else c2.push_back(i);\n }\n choose_3.emplace_back(c1, c2);\n }\n\n long long ans = 0;\n int composed = 0;\n for(int i = 0; i <= N-3; ++i) {\n // Check if all 6 sticks can be used\n if(composed == 0 && i <= N-6) {\n for(auto pat : choose_3) {\n const auto &c1 = pat.first;\n const auto &c2 = pat.second;\n if(as[i+c1[0]] < as[i+c1[1]]+as[i+c1[2]] && as[i+c2[0]] < as[i+c2[1]]+as[i+c2[2]]) {\n ans = accumulate(begin(as)+i, begin(as)+i+6, 0LL);\n composed = 2;\n goto end;\n }\n }\n }\n // Check if 3 sticks can be used\n if(as[i] < as[i+1] + as[i+2]) {\n ans += as[i]+as[i+1]+as[i+2];\n if(++composed == 2) break;\n i += 2;\n }\n }\nend:\n if(composed != 2) ans = 0;\n cout << ans << endl;\n return true;\n}\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(0);\n\n while(solve()) ;\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 1788, "score_of_the_acc": -0.0213, "final_rank": 1 } ]
aoj_2269_cpp
問題 K : 巡回セールスマン問題 2011 年のあの頃,まだプログラミングコンテストはとても平和であり, 我々はルールを精読せずに気軽にコンテストに参加していた. しかし,そこに G○○gle の陰謀は迫っていた. ある日,G○○gle によって秘密裏に買収されていた T○pC○der のコンテストは, 人知れずうちにルールが改変されており,命がけのプログラミングコンテストとなっていた. そして僕は,そこで,何も知らず, まっ先に kita_masa のプログラムのオーバーフローを突いたのだ…. (iwi) 「うう…ああ…」 wata 「今やるべきことは自分を攻めることではない.チャンスを無駄にする気か?」 …そういえば,今こいつは,部屋に居る全ての人間を一瞬にして倒していた. どうなっているんだ? wata 「まず,知っているかもしれないが, G○○gle は NP 完全問題を含む多くの計算困難と言われてきた問題に対する多項式時間の効率的なアルゴリズムを持っている.P vs NP 問題を解決したが,敢えてそれを秘匿することによってここまで成長したのだ.」 (iwi) 「もしやとは思っていたが,やはりか…. しかし,お前は今,G○○gle の人間を一瞬にして倒した.もしやお前も…?」 wata 「残念ながら違う.しかし…お前なら,分かるはずだろう?」 …なるほど!! そういうことか. 彼は大学時代,指数時間のアルゴリズムを高速にすることに興味を持っていた. 例え指数時間であっても,指数の底が極めて小さければ, 現実的なインスタンスに対して十分に高速な可能性がある. wata 「そういうことだ.例えば,今僕は,巡回セールスマン問題の O * (1.0001 n ) 時間のアルゴリズムを完成させようとしている.」 凄まじい,想像以上だ…. wata 「完成させるにあたって,グラフアルゴリズムに詳しいお前の力が必要だ. 手を貸してくれるな?」 (iwi) 「ああ,勿論だ.まかせろ!!」 問題 グラフが与えられる. グラフは N 個の頂点を持ち,頂点には 1 から N の番号が付いている. また,グラフは M 個の辺を持ち,辺は有向 (一方通行) で, それぞれ距離が決まっている. 頂点 1 から出発し,頂点 2, 3, …, N をこの順番に訪れ,頂点 1 へ戻る経路を考える. ここで,「この順番に訪れる」というのは,経路に含まれる頂点の部分列に 2, 3, …, N が含まれるということである. つまり,スタート地点の頂点 1 から頂点 2 への移動は, 直接の移動であっても,他の頂点を経由した移動であってもよく, 次の頂点 2 から頂点 3 への移動, さらにその次の頂点 3 から頂点 4 への移動等に関しても同様である. 同じ頂点や同じ辺を 2 度通っても構わない. グラフが与えられた時, 頂点 1 から出発し頂点 2, 3, …, N をこの順番に訪れ頂点 1 へ戻る経路のうち, 最短のものの長さを出力するプログラムを作成せよ. 経路の長さとは,経路に含まれる辺の距離の和である. 複数回含まれる辺に関しては,含まれる回数の分だけ距離を加えよ. 入力 入力の最初の行は 2 つの整数 N , M を含む. これは,グラフの頂点数と辺数を表す. 続く M 行は,辺の情報を表す. これらの行のうちの i 行目は 3 つの整数 a i , b i , c i が書かれており, 辺 i は頂点 a i から頂点 b i を結び,その距離は c i であることを表す. 出力 条件を満たす経路のうち最短のものの長さを出力せよ. ただし,条件を満たす経路が存在しない場合は,-1 を出力せよ. 制約 2 ≤ N ≤ 10 5 0 ≤ M ≤ N + 500 1 ≤ a i , b i ≤ N , a i ≠ b i 1 ≤ c i ≤ 10 3 入出力例 入出力例 1 入力例 1: 5 5 1 2 10 2 3 20 3 4 30 4 5 40 5 1 50 入力例 1 に対する出力の例: 150 入出力例 2 入力例 2: 5 5 1 5 10 5 4 20 4 3 30 3 2 40 2 1 50 入力例 2 に対する出力の例: 600
[ { "submission_id": "aoj_2269_10342595", "code_snippet": "// AOJ #2269 Traveling Salesman Problem\n// 2025.4.2\n\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nconst int INF = 1e9;\n\n#define gc() getchar_unlocked()\n#define pc(c) putchar_unlocked(c)\n\nint Cin() {\n\tint n = 0; int c = gc();\n\tdo n = 10*n + (c & 0xf), c = gc(); while (c >= '0');\n\treturn n;\n}\n\nvoid Cout(ll n) {\n\tchar b[30];\n\tif (!n) pc('0');\n\telse {\n\t\tif (n < 0) pc('-'), n = -n;\n\t\tint i = 0; while (n) b[i++] = n % 10 + '0', n /= 10;\n\t\twhile (i--) pc(b[i]);\n\t}\n\tpc('\\n');\n}\n\nusing pii = pair<int,int>;\n\nvector<vector<pii>> G, rG;\nvector<int> Vs, VTbl;\n\nvoid compress(const vector<vector<pii>> &G, vector<int> &near, vector<int> &ndist, vector<vector<pii>> &Gc) {\n int n = G.size();\n near.assign(n, -1);\n ndist.assign(n, 0);\n int sz = Vs.size();\n Gc.assign(sz, vector<pii>());\n for (int i = 0; i < sz; i++) {\n int from = Vs[i];\n near[from] = i;\n for (auto &e : G[from]) {\n int dsum = e.first;\n int v = e.second;\n vector<int> visited(n, -1);\n while (VTbl[v] < 0) {\n if (visited[v] != -1) break;\n visited[v] = dsum;\n near[v] = i;\n ndist[v] = dsum;\n dsum += G[v][0].first;\n v = G[v][0].second;\n }\n if (VTbl[v] < 0) VTbl[v] = i;\n Gc[i].push_back({dsum, VTbl[v]});\n }\n }\n}\n\nvoid djik(const vector<vector<pii>> &Gc, vector<vector<int>> &dtbl) {\n int c = Gc.size();\n dtbl.assign(c, vector<int>(c, INF));\n for (int i = 0; i < c; i++) {\n priority_queue<pii, vector<pii>, greater<pii>> pq;\n dtbl[i][i] = 0;\n pq.push({0, i});\n while(!pq.empty()){\n auto [d0, u] = pq.top(); pq.pop();\n if(dtbl[i][u] != d0) continue;\n for(auto &e : Gc[u]){\n int nd = d0 + e.first, w = e.second;\n if(nd < dtbl[i][w]){\n dtbl[i][w] = nd;\n pq.push({nd, w});\n }\n }\n }\n }\n}\n\nint main(){\n int n = Cin(), m = Cin();\n G.assign(n, vector<pii>());\n rG.assign(n, vector<pii>());\n for (int i = 0; i < m; i++){\n int a = Cin()-1, b = Cin()-1, c = Cin();\n G[a].push_back({c, b});\n rG[b].push_back({c, a});\n }\n\n for (int i = 0; i < n; i++){\n if(G[i].empty() || rG[i].empty()) { Cout(-1); return 0; }\n }\n\n VTbl.assign(n, -1);\n Vs.clear();\n for (int i = 0; i < n; i++){\n if(i < 2 || G[i].size() > 1 || rG[i].size() > 1){\n VTbl[i] = Vs.size();\n Vs.push_back(i);\n }\n }\n\n vector<vector<pii>> CG, rCG;\n vector<int> near, rnear, d, rd;\n compress(G, near, d, CG);\n compress(rG, rnear, rd, rCG);\n\n vector<vector<int>> dtbl;\n djik(CG, dtbl);\n\n int from = n - 1;\n ll ans = 0;\n for (int to = 0; to < n; to++){\n int i1 = rnear[from], i2 = near[to];\n if(i1 < 0 || i2 < 0) { Cout(-1); return 0; }\n int dd = rd[from] + dtbl[i1][i2] + d[to];\n if(near[from] == near[to] && rnear[from] == rnear[to]){\n int x = rd[from] - rd[to];\n if(x >= 0 && dd > x) dd = x;\n }\n if(dd >= INF) { Cout(-1); return 0; }\n ans += dd;\n from = to;\n }\n Cout(ans);\n return 0;\n}", "accuracy": 1, "time_ms": 200, "memory_kb": 20080, "score_of_the_acc": -0.109, "final_rank": 1 }, { "submission_id": "aoj_2269_10342593", "code_snippet": "// AOJ #2269 Traveling Salesman Problem\n// 2025.4.2\n\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nconst int INF = 1e9;\n\n#define gc() getchar_unlocked()\n#define pc(c) putchar_unlocked(c)\n\nint Cin() {\n\tint n = 0; int c = gc();\n\tdo n = 10*n + (c & 0xf), c = gc(); while (c >= '0');\n\treturn n;\n}\n\nvoid Cout(ll n) {\n\tchar b[30];\n\tif (!n) pc('0');\n\telse {\n\t\tif (n < 0) pc('-'), n = -n;\n\t\tint i = 0; while (n) b[i++] = n % 10 + '0', n /= 10;\n\t\twhile (i--) pc(b[i]);\n\t}\n\tpc('\\n');\n}\n\nusing pii = pair<int,int>;\n\nvector<vector<pii>> G, RG;\nvector<int> vs, vt;\n\nvoid comp(const vector<vector<pii>> &G, vector<int> &near, vector<int> &d, vector<vector<pii>> &CG) {\n int n = G.size();\n near.assign(n, -1);\n d.assign(n, 0);\n int sz = vs.size();\n CG.assign(sz, vector<pii>());\n for (int i = 0; i < sz; i++) {\n int u = vs[i];\n near[u] = i;\n for (auto &e : G[u]) {\n int s = e.first, v = e.second;\n while (vt[v] < 0) {\n near[v] = i;\n d[v] = s;\n s += G[v][0].first;\n v = G[v][0].second;\n }\n CG[i].push_back({s, vt[v]});\n }\n }\n}\n\nvoid djik(const vector<vector<pii>> &CG, vector<vector<int>> &dt) {\n int c = CG.size();\n dt.assign(c, vector<int>(c, INF));\n for (int i = 0; i < c; i++) {\n priority_queue<pii, vector<pii>, greater<pii>> pq;\n dt[i][i] = 0;\n pq.push({0, i});\n while(!pq.empty()){\n auto [d0, u] = pq.top(); pq.pop();\n if(dt[i][u] != d0) continue;\n for(auto &e : CG[u]){\n int nd = d0 + e.first, w = e.second;\n if(nd < dt[i][w]){\n dt[i][w] = nd;\n pq.push({nd, w});\n }\n }\n }\n }\n}\n\nint main(){\n int n = Cin(), m = Cin();\n G.assign(n, vector<pii>());\n RG.assign(n, vector<pii>());\n for (int i = 0; i < m; i++){\n int a = Cin()-1, b = Cin()-1, c = Cin();\n G[a].push_back({c, b});\n RG[b].push_back({c, a});\n }\n for (int i = 0; i < n; i++){\n if(G[i].empty() || RG[i].empty()) { Cout(-1); return 0; }\n }\n vt.assign(n, -1);\n for (int i = 0; i < n; i++){\n if(i < 2 || G[i].size() > 1 || RG[i].size() > 1){\n vt[i] = vs.size();\n vs.push_back(i);\n }\n }\n vector<vector<pii>> CG, RCG;\n vector<int> near, rnear, d, rd;\n comp(G, near, d, CG);\n comp(RG, rnear, rd, RCG);\n\n vector<vector<int>> dt;\n djik(CG, dt);\n\n int f = n - 1;\n long long ans = 0;\n for (int t = 0; t < n; t++){\n int i1 = rnear[f], i2 = near[t];\n if(i1 < 0 || i2 < 0) return -1;\n int dd = rd[f] + dt[i1][i2] + d[t];\n if(near[f] == near[t] && rnear[f] == rnear[t]){\n int x = rd[f] - rd[t];\n if(x >= 0 && dd > x) dd = x;\n }\n if(dd >= INF) { Cout(-1); return 0; }\n ans += dd;\n f = t;\n }\n Cout(ans);\n return 0;\n}", "accuracy": 0.875, "time_ms": 70, "memory_kb": 19876, "score_of_the_acc": -0.0722, "final_rank": 15 }, { "submission_id": "aoj_2269_9001327", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\ntypedef int64_t ll;\n\n#define n1 (100004)\n vector<vector<pair<int,int>>> g(n1);\n vector<int> ineg(n1,0),outeg(n1,0);\n vector<bool> vis(n1,false);\n vector<int> exnm(n1,-1);\n vector<int> dis(n1,0),belong(n1,-1),par(n1,-1),chd(n1,-1),len(n1,-1);\n vector d(1024,vector<int>(1024,1<<25));\n int indx=0;\n void findv(int cn){\n vis[cn]=true;\n for(int i=0;i<(int)g[cn].size();i++){\n if(vis[g[cn][i].first]) continue;\n findv(g[cn][i].first);\n }\n if((cn==0)||(ineg[cn]>1)||(outeg[cn]>1)){\n exnm[cn]=indx;\n indx++;\n }\n \n }\n\n\n\nint lenm=0;\n int makeg(int cn,int pa,int cost,int nm){\n //cout << cn << \" \" << pa << \" \" << cost << \" \" << nm << endl;\n if(exnm[cn]!=-1){\n d[exnm[pa]][exnm[cn]]=min(cost,d[exnm[pa]][exnm[cn]]);\n len[nm]=cost;\n cost=0;\n pa=cn;\n }\n if(vis[cn]) return exnm[cn];\n if(exnm[cn]==-1) belong[cn]=nm;\n vis[cn]=true;\n par[cn]=exnm[pa];\n dis[cn]=cost;\n \n int ch=-1;\n for(int i=0;i<(int)g[cn].size();i++){\n int nm1=nm;\n if(exnm[cn]!=-1){ \n lenm++;\n nm1=lenm;\n }\n ch=makeg(g[cn][i].first,pa,cost+g[cn][i].second,nm1);\n }\n if(exnm[cn]!=-1) ch=exnm[cn];\n \n return chd[cn]=ch;\n \n }\n\nint main(){\n int n,m;\n cin >> n >> m;\n int a,b,c;\n \n for(int i=0;i<m;i++){\n cin >> a >> b >> c;\n a--;b--;\n for(int j=0;j<(int)g[a].size();j++){\n if(g[a][j].first==b){\n g[a][j].second=min(c,g[a][j].second);\n c=-1;continue;\n }\n }\n if(c!=-1){ \n g[a].emplace_back(b,c);\n ineg[b]++;\n outeg[a]++;\n } \n }\n \n findv(0);\n if(indx>1020){\n cout << -1 << endl;\n return 0;\n \n }\n for(int i=0;i<n;i++){\n if(vis[i]==false) {\n cout << -1 << endl;\n return 0;\n }\n }\n //cout << indx << \" \" << exnm[0] << endl;\n //for(int i=0;i<(int)g[6].size();i++) cout << g[6][i].first << \" \" << g[6][i].second << endl;\n \n vis.assign(n,false);\n for(int i=0;i<indx;i++) d[i][i]=0;\n \n int lenm=0,nm1;\n\n makeg(0,0,0,0);\n//for(int i=0;i<n;i++){\n // << i << \" \" << exnm[i] << \" \" << belong[i] << \" \" << par[i] << \" \" << chd[i] << endl;\n//} \n\n for(int k=0;k<indx;k++){\n for(int i=0;i<indx;i++){\n for(int j=0;j<indx;j++){\n d[i][j]=min(d[i][j],d[i][k]+d[k][j]);\n }\n }\n }\n for(int i=0;i<indx;i++){\n for(int j=0;j<indx;j++){\n if(d[i][j]==(1<<25)) {\n cout << -1 << endl;\n return 0;\n }\n }\n }\n\n ll ans=0;\n for(int i=0;i<n;i++){\n int st=i,ed=(i+1)%n;\n int stbl=belong[st],edbl=belong[ed];\n if((stbl==edbl)&&(dis[st]<dis[ed])) ans+=dis[ed]-dis[st];\n else{\n int stex=chd[st],edex=par[ed];\n ans+=(!~stbl ? 0 :(len[stbl]-dis[st]))+d[stex][edex]+dis[ed];\n }\n }\n cout << ans << endl;\n}", "accuracy": 1, "time_ms": 610, "memory_kb": 23624, "score_of_the_acc": -0.2379, "final_rank": 5 }, { "submission_id": "aoj_2269_9001320", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\ntypedef int64_t ll;\n\n#define n1 (100004)\n vector<vector<pair<int,int>>> g(n1);\n vector<int> ineg(n1,0),outeg(n1,0);\n vector<bool> vis(n1,false);\n vector<int> exnm(n1,-1);\n vector<int> dis(n1,0),belong(n1,-1),par(n1,-1),chd(n1,-1),len(n1,-1);\n vector d(1024,vector<int>(1024,1<<25));\n int indx=0;\n void findv(int cn){\n vis[cn]=true;\n for(int i=0;i<(int)g[cn].size();i++){\n if(vis[g[cn][i].first]) continue;\n findv(g[cn][i].first);\n }\n if((cn==0)||(ineg[cn]>1)||(outeg[cn]>1)){\n exnm[cn]=indx;\n indx++;\n }\n \n }\n\n\n\nint lenm=0;\n int makeg(int cn,int pa,int cost,int nm){\n //cout << cn << \" \" << pa << \" \" << cost << \" \" << nm << endl;\n if(exnm[cn]!=-1){\n d[exnm[pa]][exnm[cn]]=min(cost,d[exnm[pa]][exnm[cn]]);\n len[nm]=cost;\n cost=0;\n pa=cn;\n }\n if(vis[cn]) return exnm[cn];\n if(exnm[cn]==-1) belong[cn]=nm;\n vis[cn]=true;\n par[cn]=exnm[pa];\n dis[cn]=cost;\n \n int ch=-1;\n for(int i=0;i<(int)g[cn].size();i++){\n int nm1=nm;\n if(exnm[cn]!=-1){ \n lenm++;\n nm1=lenm;\n }\n ch=makeg(g[cn][i].first,pa,cost+g[cn][i].second,nm1);\n }\n if(exnm[cn]!=-1) ch=exnm[cn];\n \n return chd[cn]=ch;\n \n }\n\nint main(){\n int n,m;\n cin >> n >> m;\n int a,b,c;\n \n for(int i=0;i<m;i++){\n cin >> a >> b >> c;\n a--;b--;\n for(int j=0;j<(int)g[a].size();j++){\n if(g[a][j].first==b){\n g[a][j].second=min(c,g[a][j].second);\n c=-1;continue;\n }\n }\n if(c!=-1){ \n g[a].emplace_back(b,c);\n ineg[b]++;\n outeg[a]++;\n } \n }\n \n findv(0);\n if(indx>1020){\n cout << -1 << endl;\n return 0;\n \n }\n for(int i=0;i<n;i++){\n if(vis[i]=false) {\n cout << -1 << endl;\n return 0;\n }\n }\n //cout << indx << \" \" << exnm[0] << endl;\n //for(int i=0;i<(int)g[6].size();i++) cout << g[6][i].first << \" \" << g[6][i].second << endl;\n \n vis.assign(n,false);\n for(int i=0;i<indx;i++) d[i][i]=0;\n \n int lenm=0,nm1;\n\n makeg(0,0,0,0);\n//for(int i=0;i<n;i++){\n // << i << \" \" << exnm[i] << \" \" << belong[i] << \" \" << par[i] << \" \" << chd[i] << endl;\n//} \n\n for(int k=0;k<indx;k++){\n for(int i=0;i<indx;i++){\n for(int j=0;j<indx;j++){\n d[i][j]=min(d[i][j],d[i][k]+d[k][j]);\n }\n }\n }\n for(int i=0;i<indx;i++){\n for(int j=0;j<indx;j++){\n if(d[i][j]==(1<<25)) {\n cout << -1 << endl;\n return 0;\n }\n }\n }\n\n ll ans=0;\n for(int i=0;i<n;i++){\n int st=i,ed=(i+1)%n;\n int stbl=belong[st],edbl=belong[ed];\n if((stbl==edbl)&&(dis[st]<dis[ed])) ans+=dis[ed]-dis[st];\n else{\n int stex=chd[st],edex=par[ed];\n ans+=(!~stbl ? 0 :(len[stbl]-dis[st]))+d[stex][edex]+dis[ed];\n }\n }\n cout << ans << endl;\n}", "accuracy": 0.9, "time_ms": 610, "memory_kb": 23624, "score_of_the_acc": -0.2379, "final_rank": 8 }, { "submission_id": "aoj_2269_9001159", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\ntypedef int64_t ll;\n\n#define n1 (100004)\n vector<vector<pair<int,int>>> g(n1);\n vector<int> ineg(n1,0),outeg(n1,0);\n vector<bool> vis(n1,false);\n vector<int> exnm(n1,-1);\n vector<int> dis(n1,0),belong(n1,-1),par(n1,-1),chd(n1,-1),len(n1,-1);\n vector d(1024,vector<ll>(1024,1e18));\n int indx=0;\n void findv(int cn){\n vis[cn]=true;\n for(int i=0;i<(int)g[cn].size();i++){\n if(vis[g[cn][i].first]) continue;\n findv(g[cn][i].first);\n }\n if((cn==0)||(ineg[cn]>1)||(outeg[cn]>1)){\n exnm[cn]=indx;\n indx++;\n }\n \n }\n\n\n\nint lenm=0;\n int makeg(int cn,int pa,int cost,int nm){\n //cout << cn << \" \" << pa << \" \" << cost << \" \" << nm << endl;\n if(exnm[cn]!=-1){\n d[exnm[pa]][exnm[cn]]=min((ll)cost,d[exnm[pa]][exnm[cn]]);\n len[nm]=cost;\n cost=0;\n pa=cn;\n }\n if(vis[cn]) return exnm[cn];\n if(exnm[cn]==-1) belong[cn]=nm;\n vis[cn]=true;\n par[cn]=exnm[pa];\n dis[cn]=cost;\n \n int ch=-1;\n for(int i=0;i<(int)g[cn].size();i++){\n int nm1=nm;\n if(exnm[cn]!=-1){ \n lenm++;\n nm1=lenm;\n }\n ch=makeg(g[cn][i].first,pa,cost+g[cn][i].second,nm1);\n }\n if(exnm[cn]!=-1) ch=exnm[cn];\n \n return chd[cn]=ch;\n \n }\n\nint main(){\n int n,m;\n cin >> n >> m;\n int a,b,c;\n \n for(int i=0;i<m;i++){\n cin >> a >> b >> c;\n a--;b--;\n for(int j=0;j<(int)g[a].size();j++){\n if(g[a][j].first==b){\n g[a][j].second=min(c,g[a][j].second);\n c=-1;continue;\n }\n }\n if(c!=-1){ \n g[a].emplace_back(b,c);\n ineg[b]++;\n outeg[a]++;\n } \n }\n \n findv(0);\n if(indx>1020){\n cout << -1 << endl;\n return 0;\n \n }\n for(int i=0;i<n;i++){\n if(vis[i]=false) {\n cout << -1 << endl;\n return 0;\n }\n }\n //cout << indx << \" \" << exnm[0] << endl;\n //for(int i=0;i<(int)g[6].size();i++) cout << g[6][i].first << \" \" << g[6][i].second << endl;\n \n vis.assign(n,false);\n for(int i=0;i<indx;i++) d[i][i]=0;\n \n int lenm=0,nm1;\n\n makeg(0,0,0,0);\n//for(int i=0;i<n;i++){\n // << i << \" \" << exnm[i] << \" \" << belong[i] << \" \" << par[i] << \" \" << chd[i] << endl;\n//} \n\n for(int k=0;k<indx;k++){\n for(int i=0;i<indx;i++){\n for(int j=0;j<indx;j++){\n d[i][j]=min(d[i][j],d[i][k]+d[k][j]);\n }\n }\n }\n for(int i=0;i<indx;i++){\n for(int j=0;j<indx;j++){\n if(d[i][j]==1e18) {\n cout << -1 << endl;\n return 0;\n }\n }\n }\n\n ll ans=0;\n for(int i=0;i<n;i++){\n int st=i,ed=(i+1)%n;\n int stbl=belong[st],edbl=belong[ed];\n if((stbl==edbl)&&(dis[st]<dis[ed])) ans+=dis[ed]-dis[st];\n else{\n int stex=chd[st],edex=par[ed];\n ans+=(!~stbl ? 0 :(len[stbl]-dis[st]))+d[stex][edex]+dis[ed];\n }\n }\n cout << ans << endl;\n}", "accuracy": 0.9, "time_ms": 600, "memory_kb": 27692, "score_of_the_acc": -0.2531, "final_rank": 9 }, { "submission_id": "aoj_2269_9001152", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\ntypedef int64_t ll;\n\n#define n1 (100004)\n vector<vector<pair<int,int>>> g(n1);\n vector<int> ineg(n1,0),outeg(n1,0);\n vector<bool> vis(n1,false);\n vector<int> exnm(n1,-1);\n vector<int> dis(n1,0),belong(n1,-1),par(n1,-1),chd(n1,-1),len(n1,-1);\n vector d(1024,vector<ll>(1024,1e18));\n int indx=0;\n void findv(int cn){\n vis[cn]=true;\n for(int i=0;i<(int)g[cn].size();i++){\n if(vis[g[cn][i].first]) continue;\n findv(g[cn][i].first);\n }\n if((cn==0)||(ineg[cn]>1)||(outeg[cn]>1)){\n exnm[cn]=indx;\n indx++;\n }\n \n }\n\n\n\nint lenm=0;\n int makeg(int cn,int pa,int cost,int nm){\n //cout << cn << \" \" << pa << \" \" << cost << \" \" << nm << endl;\n if(exnm[cn]!=-1){\n d[exnm[pa]][exnm[cn]]=min((ll)cost,d[exnm[pa]][exnm[cn]]);\n len[nm]=cost;\n cost=0;\n pa=cn;\n }\n if(vis[cn]) return exnm[cn];\n if(exnm[cn]==-1) belong[cn]=nm;\n vis[cn]=true;\n par[cn]=exnm[pa];\n dis[cn]=cost;\n \n int ch=-1;\n for(int i=0;i<(int)g[cn].size();i++){\n int nm1=nm;\n if(exnm[cn]!=-1){ \n lenm++;\n nm1=lenm;\n }\n ch=makeg(g[cn][i].first,pa,cost+g[cn][i].second,nm1);\n }\n if(exnm[cn]!=-1) ch=exnm[cn];\n \n return chd[cn]=ch;\n \n }\n\nint main(){\n int n,m;\n cin >> n >> m;\n int a,b,c;\n \n for(int i=0;i<m;i++){\n cin >> a >> b >> c;\n a--;b--;\n for(int j=0;j<(int)g[a].size();j++){\n if(g[a][j].first==b){\n g[a][j].second=min(c,g[a][j].second);\n c=-1;continue;\n }\n }\n if(c!=-1){ \n g[a].emplace_back(b,c);\n ineg[b]++;\n outeg[a]++;\n } \n }\n \n findv(0);\n if(indx>1020){\n cout << -1 << endl;\n return 0;\n \n }\n for(int i=0;i<n;i++){\n if(vis[i]=false) {\n cout << -1 << endl;\n return 0;\n }\n }\n //cout << indx << \" \" << exnm[0] << endl;\n //for(int i=0;i<(int)g[6].size();i++) cout << g[6][i].first << \" \" << g[6][i].second << endl;\n \n vis.assign(n,false);\n for(int i=0;i<indx;i++) d[i][i]=0;\n \n int lenm=0,nm1;\n\n makeg(0,0,0,0);\n//for(int i=0;i<n;i++){\n // << i << \" \" << exnm[i] << \" \" << belong[i] << \" \" << par[i] << \" \" << chd[i] << endl;\n//} \n\n for(int k=0;k<indx;k++){\n for(int i=0;i<indx;i++){\n for(int j=0;j<indx;j++){\n d[i][j]=min(d[i][j],d[i][k]+d[k][j]);\n }\n }\n }\n for(int i=0;i<indx;i++){\n for(int j=0;j<indx;j++){\n if(d[i][j]==1e18) {\n cout << -1 << endl;\n return 0;\n }\n }\n }\n\n ll ans=0;\n for(int i=0;i<n;i++){\n int st=i,ed=(i+1)%n;\n int stbl=belong[st],edbl=belong[ed];\n if((stbl==edbl)&&(dis[st]<dis[ed])) ans+=dis[ed]-dis[st];\n else{\n int stex=chd[st],edex=par[ed];\n ans+=len[stbl]-dis[st]+d[stex][edex]+dis[ed];\n }\n }\n cout << ans << endl;\n}", "accuracy": 0.9, "time_ms": 610, "memory_kb": 27460, "score_of_the_acc": -0.2548, "final_rank": 10 }, { "submission_id": "aoj_2269_9000807", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\ntypedef int64_t ll;\n\n#define n1 (100010)\n vector<vector<pair<int,int>>> g(n1);\n vector<int> ineg(n1,0),outeg(n1,0);\n vector<bool> vis(n1,false);\n vector<int> exnm(n1,-1);\n vector<int> dis(n1,0),belong(n1,-1),par(n1,-1),chd(n1,-1),len(n1,-1);\n vector d(1024,vector<ll>(1024,1e18));\nint lenm=0;\n int makeg(int cn,int pa,int cost,int nm){\n //cout << cn << \" \" << pa << \" \" << cost << \" \" << nm << endl;\n if(exnm[cn]!=-1){\n d[exnm[pa]][exnm[cn]]=min((ll)cost,d[exnm[pa]][exnm[cn]]);\n len[nm]=cost;\n cost=0;\n pa=cn;\n }\n if(vis[cn]) return exnm[cn];\n if(exnm[cn]==-1) belong[cn]=nm;\n vis[cn]=true;\n par[cn]=exnm[pa];\n dis[cn]=cost;\n \n int ch=-1;\n for(int i=0;i<(int)g[cn].size();i++){\n int nm1=nm;\n if(exnm[cn]!=-1){ \n lenm++;\n nm1=lenm;\n }\n ch=makeg(g[cn][i].first,pa,cost+g[cn][i].second,nm1);\n }\n if(exnm[cn]!=-1) ch=exnm[cn];\n \n return chd[cn]=ch;\n \n };\n\nint main(){\n int n,m;\n cin >> n >> m;\n int a,b,c;\n \n for(int i=0;i<m;i++){\n cin >> a >> b >> c;\n a--;b--;\n for(int j=0;j<(int)g[a].size();j++){\n if(g[a][j].first==b){\n g[a][j].second=min(c,g[a][j].second);\n c=-1;continue;\n }\n }\n if(c!=-1){ \n g[a].emplace_back(b,c);\n ineg[b]++;\n outeg[a]++;\n } \n }\n \n int indx=0;\n function<void(int)> findv=[&](int cn){\n vis[cn]=true;\n for(int i=0;i<(int)g[cn].size();i++){\n if(vis[g[cn][i].first]) continue;\n findv(g[cn][i].first);\n }\n if((cn==0)||(ineg[cn]>1)||(outeg[cn]>1)){\n exnm[cn]=indx;\n indx++;\n }\n \n };\n findv(0);\n if(indx>1020){\n cout << -1 << endl;\n return 0;\n \n }\n for(int i=0;i<n;i++){\n if(vis[i]=false) {\n cout << -1 << endl;\n return 0;\n }\n }\n //cout << indx << \" \" << exnm[0] << endl;\n //for(int i=0;i<(int)g[6].size();i++) cout << g[6][i].first << \" \" << g[6][i].second << endl;\n \n vis.assign(n,false);\n for(int i=0;i<indx;i++) d[i][i]=0;\n \n int lenm=0,nm1;\n\n makeg(0,0,0,0);\n//for(int i=0;i<n;i++){\n // << i << \" \" << exnm[i] << \" \" << belong[i] << \" \" << par[i] << \" \" << chd[i] << endl;\n//} \n\n for(int k=0;k<indx;k++){\n for(int i=0;i<indx;i++){\n for(int j=0;j<indx;j++){\n d[i][j]=min(d[i][j],d[i][k]+d[k][j]);\n }\n }\n }\n for(int i=0;i<indx;i++){\n for(int j=0;j<indx;j++){\n if(d[i][j]==1e18) {\n cout << -1 << endl;\n return 0;\n }\n }\n }\n\n ll ans=0;\n for(int i=0;i<n;i++){\n int st=i,ed=(i+1)%n;\n int stbl=belong[st],edbl=belong[ed];\n if((stbl==edbl)&&(dis[st]<dis[ed])) ans+=dis[ed]-dis[st];\n else{\n int stex=chd[st],edex=par[ed];\n ans+=len[stbl]-dis[st]+d[stex][edex]+dis[ed];\n }\n }\n cout << ans << endl;\n}", "accuracy": 0.9, "time_ms": 630, "memory_kb": 27464, "score_of_the_acc": -0.2603, "final_rank": 11 }, { "submission_id": "aoj_2269_9000803", "code_snippet": "#include <bits/stdc++.h>\n#define MAX_N (100010)\n\nusing namespace std;\n\ntypedef long long lint;\n\nstruct edge {\n\tint to, cost;\n\tedge(int to, int cost) : to(to), cost(cost) {}\n\tedge(){}\n};\n\nvector<edge> g[MAX_N];\nbool vis[MAX_N];\nint tVertex[MAX_N];\nint len[MAX_N], dist[MAX_N], belong[MAX_N];\nint par[MAX_N], chi[MAX_N];\nint inDeg[MAX_N], outDeg[MAX_N];\nint d[1024][1024];\nint idx;\n\nvoid findCompress(int v)\n{\n\tvis[v] = true;\n\t\n\tfor (int i = 0; i < g[v].size(); i++){\n\t\tedge e = g[v][i];\n\t\tif (!vis[e.to]){\n\t\t\tfindCompress(e.to);\n\t\t}\n\t}\n\t\n\tif (!v || inDeg[v] > 1 || outDeg[v] > 1){\n\t\ttVertex[v] = idx++;\n\t}\n}\n\nint s;\nint makeCompress(int now, int p, int cost, int id)\n{\n//cout << now << \" \" << p << \" \" << cost << \" \" << id << endl;\n\tif (~tVertex[now]){\n\t\tbelong[now] = -1;\n\t\tlen[id] = cost;\n\t\td[tVertex[p]][tVertex[now]] = min(cost, d[tVertex[p]][tVertex[now]]);\n\t\tcost = 0;\n\t\tp = now;\n\t}\n\t\n\tif (vis[now]) return (tVertex[now]);\n\tif (!~tVertex[now]) belong[now] = id;\n\tvis[now] = true;\n\tpar[now] = tVertex[p];\n\tdist[now] = cost;\n\t\n\tint r = -1;\n\tfor (int i = 0; i < g[now].size(); i++){\n\t\tint nid = id;\n\t\tif (~tVertex[now]) nid = ++s;\n\t\tr = makeCompress(g[now][i].to, p, cost + g[now][i].cost, nid);\n\t}\n\tif (~tVertex[now]) r = tVertex[now];\n //cout << now << \" b\" <<belong[now] << \" p\" << par[now] << \" \" << dist[now] << \" \" << r << \" \" << tVertex[now] << endl;\n\treturn (chi[now] = r);\n}\n\nint main()\n{\n\tint n, m;\n\t\n\tscanf(\"%d %d\", &n, &m);\n\t\n\tfor (int i = 0; i < m; i++){\n\t\tint a, b, c;\n\t\tscanf(\"%d %d %d\", &a, &b, &c);\n\t\t--a; --b;\n\t\tfor (int i = 0; i < g[a].size(); i++){\n\t\t\tif (g[a][i].to == b){\n\t\t\t\tg[a][i].cost = min(g[a][i].cost, c);\n\t\t\t\tgoto updated;\n\t\t\t}\n\t\t}\n\t\t\n\t\tg[a].push_back(edge(b, c));\n\t\toutDeg[a]++; inDeg[b]++;\n\t\tupdated:;\n\t}\n\t\n\tmemset(vis, 0, sizeof(vis));\n\tmemset(tVertex, -1, sizeof(tVertex));\n\tmemset(par, -1, sizeof(par));\n\tfindCompress(0);\n\t//cout << idx << \" \" << tVertex[0] <<endl;\n\t// for(int i=0;i<(int)g[14].size();i++) cout << g[14][i].to << \" \" << g[14][i].cost << endl;\n\n\tif (idx >= 1024) return (!printf(\"-1\\n\"));\n\tfor (int i = 0; i < n; i++) if (vis[i] == false) return (!printf(\"-1\\n\"));\n\t\n\tfor (int i = 0; i < 1024; i++){\n\t\tfor (int j = 0; j < 1024; j++){\n\t\t\td[i][j] = 1 << 25;\n\t\t}\n\t\td[i][i] = 0;\n\t}\n\tmemset(vis, 0, sizeof(vis));\n\tmakeCompress(0, 0, 0, 0);\n\t\n\tfor (int k = 0; k < idx; k++){\n\t\tfor (int i = 0; i < idx; i++){\n\t\t\tfor (int j = 0; j < idx; j++){\n\t\t\t\td[i][j] = min(d[i][j], d[i][k] + d[k][j]);\n\t\t\t}\n\t\t}\n\t}\n\t\n for (int i = 0; i < idx; i++){\n for (int j = 0; j < idx; j++){\n if (d[i][j] == 1 << 25) return (!printf(\"-1\\n\"));\n }\n }\t\n\tlint res = 0;\n\t\n\tfor (int i = 0; i < n; i++){\n\t\tint from = i, to = (i + 1) % n;\n\t\tint fl = belong[from], tl = belong[to];\n\t\tint c = chi[from], p = par[to];\n\t\tif (fl == tl && dist[from] < dist[to]){\n\t\t\tres += dist[to] - dist[from];\n\t\t}\n\t\telse {\n\t\t\tres += (!~fl ? 0 : (len[fl] - dist[from])) + d[c][p] + dist[to];\n\t\t}\n\t}\n\t\n\tprintf(\"%lld\\n\", res);\n\t\n\treturn (0);\n}", "accuracy": 1, "time_ms": 530, "memory_kb": 22448, "score_of_the_acc": -0.2106, "final_rank": 4 }, { "submission_id": "aoj_2269_9000802", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\ntypedef int64_t ll;\n\n#define n1 (100004)\n vector<vector<pair<int,int>>> g(n1);\n vector<int> ineg(n1,0),outeg(n1,0);\n vector<bool> vis(n1,false);\n vector<int> exnm(n1,-1);\n vector<int> dis(n1,0),belong(n1,-1),par(n1,-1),chd(n1,-1),len(n1,-1);\n vector d(1024,vector<ll>(1024,1e18));\n\nint main(){\n int n,m;\n cin >> n >> m;\n int a,b,c;\n \n for(int i=0;i<m;i++){\n cin >> a >> b >> c;\n a--;b--;\n for(int j=0;j<(int)g[a].size();j++){\n if(g[a][j].first==b){\n g[a][j].second=min(c,g[a][j].second);\n c=-1;continue;\n }\n }\n if(c!=-1){ \n g[a].emplace_back(b,c);\n ineg[b]++;\n outeg[a]++;\n } \n }\n \n int indx=0;\n function<void(int)> findv=[&](int cn){\n vis[cn]=true;\n for(int i=0;i<(int)g[cn].size();i++){\n if(vis[g[cn][i].first]) continue;\n findv(g[cn][i].first);\n }\n if((cn==0)||(ineg[cn]>1)||(outeg[cn]>1)){\n exnm[cn]=indx;\n indx++;\n }\n \n };\n findv(0);\n if(indx>1020){\n cout << -1 << endl;\n return 0;\n \n }\n for(int i=0;i<n;i++){\n if(vis[i]=false) {\n cout << -1 << endl;\n return 0;\n }\n }\n\n //cout << indx << \" \" << exnm[0] << endl;\n //for(int i=0;i<(int)g[6].size();i++) cout << g[6][i].first << \" \" << g[6][i].second << endl;\n \n vis.assign(n,false);\n for(int i=0;i<indx;i++) d[i][i]=0;\n \n int lenm=0,nm1;\n function<int(int,int,int,int)> makeg=[&](int cn,int pa,int cost,int nm){\n //cout << cn << \" \" << pa << \" \" << cost << \" \" << nm << endl;\n if(exnm[cn]!=-1){\n d[exnm[pa]][exnm[cn]]=min((ll)cost,d[exnm[pa]][exnm[cn]]);\n len[nm]=cost;\n cost=0;\n pa=cn;\n }\n if(vis[cn]) return exnm[cn];\n if(exnm[cn]==-1) belong[cn]=nm;\n vis[cn]=true;\n par[cn]=exnm[pa];\n dis[cn]=cost;\n \n int ch=-1;\n for(int i=0;i<(int)g[cn].size();i++){\n nm1=nm;\n if(exnm[cn]!=-1){ \n lenm++;\n nm1=lenm;\n }\n ch=makeg(g[cn][i].first,pa,cost+g[cn][i].second,nm1);\n }\n if(exnm[cn]!=-1) ch=exnm[cn];\n \n return chd[cn]=ch;\n \n };\n makeg(0,0,0,0);\n//for(int i=0;i<n;i++){\n // << i << \" \" << exnm[i] << \" \" << belong[i] << \" \" << par[i] << \" \" << chd[i] << endl;\n//} \n\n for(int k=0;k<indx;k++){\n for(int i=0;i<indx;i++){\n for(int j=0;j<indx;j++){\n d[i][j]=min(d[i][j],d[i][k]+d[k][j]);\n }\n }\n }\n for(int i=0;i<indx;i++){\n for(int j=0;j<indx;j++){\n if(d[i][j]==1e18) {\n cout << -1 << endl;\n return 0;\n }\n }\n }\n\n ll ans=0;\n for(int i=0;i<n;i++){\n int st=i,ed=(i+1)%n;\n int stbl=belong[st],edbl=belong[ed];\n if((stbl==edbl)&&(dis[st]<dis[ed])) ans+=dis[ed]-dis[st];\n else{\n int stex=chd[st],edex=par[ed];\n ans+=len[stbl]-dis[st]+d[stex][edex]+dis[ed];\n }\n }\n cout << ans << endl;\n}", "accuracy": 0.9, "time_ms": 650, "memory_kb": 29168, "score_of_the_acc": -0.2734, "final_rank": 14 }, { "submission_id": "aoj_2269_9000682", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\ntypedef int64_t ll;\n\n#define n1 (100004)\n vector<vector<pair<int,int>>> g(n1);\n vector<int> ineg(n1,0),outeg(n1,0);\n vector<bool> vis(n1,false);\n vector<int> exnm(n1,-1);\n vector<int> dis(n1,0),belong(n1,-1),par(n1,-1),chd(n1,-1),len(n1,-1);\n vector d(1024,vector<ll>(1024,1e18));\n\nint main(){\n int n,m;\n cin >> n >> m;\n int a,b,c;\n \n for(int i=0;i<m;i++){\n cin >> a >> b >> c;\n a--;b--;\n for(int j=0;j<(int)g[a].size();j++){\n if(g[a][j].first==b){\n g[a][j].second=min(c,g[a][j].second);\n c=-1;continue;\n }\n }\n if(c!=-1){ \n g[a].emplace_back(b,c);\n ineg[b]++;\n outeg[a]++;\n } \n }\n \n int indx=0;\n function<void(int)> findv=[&](int cn){\n vis[cn]=true;\n for(int i=0;i<(int)g[cn].size();i++){\n if(vis[g[cn][i].first]) continue;\n findv(g[cn][i].first);\n }\n if((cn==0)||(ineg[cn]>1)||(outeg[cn]>1)){\n exnm[cn]=indx;\n indx++;\n }\n \n };\n findv(0);\n if(indx>1020){\n cout << -1 << endl;\n return 0;\n \n }\n\n //cout << indx << \" \" << exnm[0] << endl;\n //for(int i=0;i<(int)g[6].size();i++) cout << g[6][i].first << \" \" << g[6][i].second << endl;\n \n vis.assign(n,false);\n for(int i=0;i<indx;i++) d[i][i]=0;\n \n int lenm=0,nm1;\n function<int(int,int,int,int)> makeg=[&](int cn,int pa,int cost,int nm){\n //cout << cn << \" \" << pa << \" \" << cost << \" \" << nm << endl;\n if(exnm[cn]!=-1){\n d[exnm[pa]][exnm[cn]]=min((ll)cost,d[exnm[pa]][exnm[cn]]);\n len[nm]=cost;\n cost=0;\n pa=cn;\n }\n if(vis[cn]) return exnm[cn];\n if(exnm[cn]==-1) belong[cn]=nm;\n vis[cn]=true;\n par[cn]=exnm[pa];\n dis[cn]=cost;\n \n int ch=-1;\n for(int i=0;i<(int)g[cn].size();i++){\n nm1=nm;\n if(exnm[cn]!=-1){ \n lenm++;\n nm1=lenm;\n }\n ch=makeg(g[cn][i].first,pa,cost+g[cn][i].second,nm1);\n }\n if(exnm[cn]!=-1) ch=exnm[cn];\n \n return chd[cn]=ch;\n \n };\n makeg(0,0,0,0);\n//for(int i=0;i<n;i++){\n // << i << \" \" << exnm[i] << \" \" << belong[i] << \" \" << par[i] << \" \" << chd[i] << endl;\n//} \n\n for(int k=0;k<indx;k++){\n for(int i=0;i<indx;i++){\n for(int j=0;j<indx;j++){\n d[i][j]=min(d[i][j],d[i][k]+d[k][j]);\n }\n }\n }\n for(int i=0;i<indx;i++){\n for(int j=0;j<indx;j++){\n if(d[i][j]==1e18) {\n cout << -1 << endl;\n return 0;\n }\n }\n }\n\n ll ans=0;\n for(int i=0;i<n;i++){\n int st=i,ed=(i+1)%n;\n int stbl=belong[st],edbl=belong[ed];\n if((stbl==edbl)&&(dis[st]<dis[ed])) ans+=dis[ed]-dis[st];\n else{\n int stex=chd[st],edex=par[ed];\n ans+=len[stbl]-dis[st]+d[stex][edex]+dis[ed];\n }\n }\n cout << ans << endl;\n}", "accuracy": 0.9, "time_ms": 620, "memory_kb": 29176, "score_of_the_acc": -0.2651, "final_rank": 12 }, { "submission_id": "aoj_2269_9000648", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\ntypedef int64_t ll;\n\n#define n1 (100004)\n vector<vector<pair<int,int>>> g(n1);\n vector<int> ineg(n1,0),outeg(n1,0);\n vector<bool> vis(n1,false);\n vector<int> exnm(n1,-1);\n vector<int> dis(n1,0),belong(n1,-1),par(n1,-1),chd(n1,-1),len(n1,-1);\n vector d(1024,vector<ll>(1024,1e18));\n\nint main(){\n int n,m;\n cin >> n >> m;\n int a,b,c;\n \n for(int i=0;i<m;i++){\n cin >> a >> b >> c;\n a--;b--;\n for(int j=0;j<(int)g[a].size();j++){\n if(g[a][j].first==b){\n g[a][j].second=min(c,g[a][j].second);\n c=-1;continue;\n }\n }\n if(c!=-1){ \n g[a].emplace_back(b,c);\n ineg[b]++;\n outeg[a]++;\n } \n }\n \n int indx=0;\n function<void(int)> findv=[&](int cn){\n vis[cn]=true;\n for(int i=0;i<(int)g[cn].size();i++){\n if(vis[g[cn][i].first]) continue;\n findv(g[cn][i].first);\n }\n if((cn==0)||(ineg[cn]>1)||(outeg[cn]>1)){\n exnm[cn]=indx;\n indx++;\n }\n \n };\n findv(0);\n //cout << indx << \" \" << exnm[0] << endl;\n //for(int i=0;i<(int)g[6].size();i++) cout << g[6][i].first << \" \" << g[6][i].second << endl;\n \n vis.assign(n,false);\n for(int i=0;i<indx;i++) d[i][i]=0;\n \n int lenm=0,nm1;\n function<int(int,int,int,int)> makeg=[&](int cn,int pa,int cost,int nm){\n //cout << cn << \" \" << pa << \" \" << cost << \" \" << nm << endl;\n if(exnm[cn]!=-1){\n d[exnm[pa]][exnm[cn]]=min((ll)cost,d[exnm[pa]][exnm[cn]]);\n len[nm]=cost;\n cost=0;\n pa=cn;\n }\n if(vis[cn]) return exnm[cn];\n if(exnm[cn]==-1) belong[cn]=nm;\n vis[cn]=true;\n par[cn]=exnm[pa];\n dis[cn]=cost;\n \n int ch=-1;\n for(int i=0;i<(int)g[cn].size();i++){\n nm1=nm;\n if(exnm[cn]!=-1){ \n lenm++;\n nm1=lenm;\n }\n ch=makeg(g[cn][i].first,pa,cost+g[cn][i].second,nm1);\n }\n if(exnm[cn]!=-1) ch=exnm[cn];\n \n return chd[cn]=ch;\n \n };\n makeg(0,0,0,0);\n//for(int i=0;i<n;i++){\n // << i << \" \" << exnm[i] << \" \" << belong[i] << \" \" << par[i] << \" \" << chd[i] << endl;\n//} \n\n for(int k=0;k<indx;k++){\n for(int i=0;i<indx;i++){\n for(int j=0;j<indx;j++){\n d[i][j]=min(d[i][j],d[i][k]+d[k][j]);\n }\n }\n }\n for(int i=0;i<indx;i++){\n for(int j=0;j<indx;j++){\n if(d[i][j]==1e18) {\n cout << -1 << endl;\n return 0;\n }\n }\n }\n\n ll ans=0;\n for(int i=0;i<n;i++){\n int st=i,ed=(i+1)%n;\n int stbl=belong[st],edbl=belong[ed];\n if((stbl==edbl)&&(dis[st]<dis[ed])) ans+=dis[ed]-dis[st];\n else{\n int stex=chd[st],edex=par[ed];\n ans+=len[stbl]-dis[st]+d[stex][edex]+dis[ed];\n }\n }\n cout << ans << endl;\n}", "accuracy": 0.9, "time_ms": 630, "memory_kb": 29112, "score_of_the_acc": -0.2676, "final_rank": 13 }, { "submission_id": "aoj_2269_7058083", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\nnamespace internal_scc{\ntemplate <class E> struct csr {\n std::vector<int> start;\n std::vector<E> elist;\n csr(int n, const std::vector<std::pair<int, E>>& edges)\n : start(n + 1), elist(edges.size()) {\n for (auto e : edges) {\n start[e.first + 1]++;\n }\n for (int i = 1; i <= n; i++) {\n start[i] += start[i - 1];\n }\n auto counter = start;\n for (auto e : edges) {\n elist[counter[e.first]++] = e.second;\n }\n }\n};\nstruct scc_graph {\n public:\n scc_graph(int n) : _n(n) {}\n int num_vertices() { return _n; }\n void add_edge(int from, int to) { edges.push_back({from, {to}}); }\n std::pair<int, std::vector<int>> scc_ids() {\n auto g = csr<edge>(_n, edges);\n int now_ord = 0, group_num = 0;\n std::vector<int> visited, low(_n), ord(_n, -1), ids(_n);\n visited.reserve(_n);\n auto dfs = [&](auto self, int v) -> void {\n low[v] = ord[v] = now_ord++;\n visited.push_back(v);\n for (int i = g.start[v]; i < g.start[v + 1]; i++) {\n auto to = g.elist[i].to;\n if (ord[to] == -1) {\n self(self, to);\n low[v] = std::min(low[v], low[to]);\n } else {\n low[v] = std::min(low[v], ord[to]);\n }\n }\n if (low[v] == ord[v]) {\n while (true) {\n int u = visited.back();\n visited.pop_back();\n ord[u] = _n;\n ids[u] = group_num;\n if (u == v) break;\n }\n group_num++;\n }\n };\n for (int i = 0; i < _n; i++) {\n if (ord[i] == -1) dfs(dfs, i);\n }\n for (auto& x : ids) {\n x = group_num - 1 - x;\n }\n return {group_num, ids};\n }\n\n std::vector<std::vector<int>> scc() {\n auto ids = scc_ids();\n int group_num = ids.first;\n std::vector<int> counts(group_num);\n for (auto x : ids.second) counts[x]++;\n std::vector<std::vector<int>> groups(ids.first);\n for (int i = 0; i < group_num; i++) {\n groups[i].reserve(counts[i]);\n }\n for (int i = 0; i < _n; i++) {\n groups[ids.second[i]].push_back(i);\n }\n return groups;\n }\n\n private:\n int _n;\n struct edge {\n int to;\n };\n std::vector<std::pair<int, edge>> edges;\n};\n}\n\nstruct scc_graph {\n public:\n scc_graph() : internal(0) {}\n scc_graph(int n) : internal(n) {}\n void add_edge(int from, int to) {\n int n = internal.num_vertices();\n assert(0 <= from && from < n);\n assert(0 <= to && to < n);\n internal.add_edge(from, to);\n }\n std::vector<std::vector<int>> scc() { return internal.scc(); }\n private:\n internal_scc::scc_graph internal;\n};\n\nstruct compress_graph{\n int n, idx = 0;\n vector<vector<pair<int,int>>> g, G;\n vector<int> ent_v, ent_dist, exit_v, exit_dist, cv, edge_id, ent_edge, dict;\n vector<vector<int>> D;\n compress_graph(vector<vector<pair<int,int>>> &_g) : n(_g.size()), g(_g) {\n ent_v.resize(n);\n ent_dist.resize(n);\n exit_v.resize(n);\n exit_dist.resize(n);\n edge_id.resize(n, -1);\n ent_edge.resize(n);\n dict.resize(n, -1);\n init();\n }\n void init(){\n for(int i = 0; i < n; i++){\n for(auto &&pa: g[i])ent_edge[pa.first]++;\n }\n for(int i = 0; i < n; i++){\n if(g[i].size() != 1 || ent_edge[i] != 1)cv.push_back(i);\n }\n for(int i = 0; i < cv.size(); i++){\n dict[cv[i]] = i;\n }\n G.resize(cv.size());\n for(auto &&i : cv){\n ent_v[i] = exit_v[i] = i;\n for(int j = 0, cur, d, D, lst; j < g[i].size(); j++){\n tie(cur, d) = g[i][j];\n D = d;\n while(g[cur].size() == 1 && ent_edge[cur] == 1){\n ent_v[cur] = i;\n ent_dist[cur] = D;\n edge_id[cur] = idx;\n tie(cur, d) = g[cur][0];\n D += d;\n }\n G[dict[i]].emplace_back(dict[cur], D);\n lst = cur;\n cur = g[i][j].first;\n while(g[cur].size() == 1 && ent_edge[cur] == 1){\n exit_v[cur] = lst;\n exit_dist[cur] = D - ent_dist[cur];\n cur = g[cur][0].first;\n }\n idx++;\n }\n }\n if(cv.empty()){\n cv.push_back(0);\n G.resize(1);\n dict[0] = 0;\n int cur, d, D;\n tie(cur, d) = g[0][0];\n D = d;\n while(g[cur].size() == 1 && cur){\n ent_v[cur] = 0;\n ent_dist[cur] = D;\n edge_id[cur] = idx;\n tie(cur, d) = g[cur][0];\n D += d;\n }\n G[0].emplace_back(0, D);\n cur = g[0][0].first;\n while(g[cur].size() == 1 && cur){\n exit_v[cur] = G[0].back().first;\n exit_dist[cur] = D - ent_dist[cur];\n cur = g[cur][0].first;\n }\n }\n D.resize(cv.size(), vector<int>(cv.size(), 1 << 30));\n for(int i = 0; i < cv.size(); i++){\n dijkstra(i, D[i]);\n }\n }\n void dijkstra(int src, vector<int> &temp){\n priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> pq;\n int d, v, u, c;\n temp[src] = 0;\n pq.emplace(0, src);\n while(!pq.empty()){\n tie(d, v) = pq.top();\n pq.pop();\n if(d > temp[v])continue;\n for(auto &&pa : G[v]){\n tie(u, c) = pa;\n if(d + c >= temp[u])continue;\n temp[u] = d + c;\n pq.emplace(temp[u], u);\n }\n }\n }\n int dist(int from, int to){\n if(edge_id[from] != -1 && edge_id[from] == edge_id[to] && ent_dist[from] <= ent_dist[to])return ent_dist[to] - ent_dist[from];\n int S = dict[exit_v[from]], T = dict[ent_v[to]];\n return D[S][T] + exit_dist[from] + ent_dist[to];\n }\n};\n\nint main(){\n ios::sync_with_stdio(false);\n cin.tie(0);\n int n, m, u, v, c;\n cin >> n >> m;\n vector<vector<pair<int,int>>> g(n);\n scc_graph sg(n);\n for(int i = 0; i < m; i++){\n cin >> u >> v >> c;\n g[--u].emplace_back(--v, c);\n sg.add_edge(u, v);\n }\n if(sg.scc().size() != 1){\n cout << -1 << '\\n';\n return 0;\n }\n compress_graph G(g);\n ll ans = 0;\n for(int i = 0; i < n; i++){\n int v = G.dist(i, i + 1 == n ? 0 : i + 1);\n ans += v;\n }\n cout << ans << '\\n';\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 32092, "score_of_the_acc": -0.1288, "final_rank": 2 }, { "submission_id": "aoj_2269_7058050", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\nnamespace internal_scc{\ntemplate <class E> struct csr {\n std::vector<int> start;\n std::vector<E> elist;\n csr(int n, const std::vector<std::pair<int, E>>& edges)\n : start(n + 1), elist(edges.size()) {\n for (auto e : edges) {\n start[e.first + 1]++;\n }\n for (int i = 1; i <= n; i++) {\n start[i] += start[i - 1];\n }\n auto counter = start;\n for (auto e : edges) {\n elist[counter[e.first]++] = e.second;\n }\n }\n};\nstruct scc_graph {\n public:\n scc_graph(int n) : _n(n) {}\n int num_vertices() { return _n; }\n void add_edge(int from, int to) { edges.push_back({from, {to}}); }\n std::pair<int, std::vector<int>> scc_ids() {\n auto g = csr<edge>(_n, edges);\n int now_ord = 0, group_num = 0;\n std::vector<int> visited, low(_n), ord(_n, -1), ids(_n);\n visited.reserve(_n);\n auto dfs = [&](auto self, int v) -> void {\n low[v] = ord[v] = now_ord++;\n visited.push_back(v);\n for (int i = g.start[v]; i < g.start[v + 1]; i++) {\n auto to = g.elist[i].to;\n if (ord[to] == -1) {\n self(self, to);\n low[v] = std::min(low[v], low[to]);\n } else {\n low[v] = std::min(low[v], ord[to]);\n }\n }\n if (low[v] == ord[v]) {\n while (true) {\n int u = visited.back();\n visited.pop_back();\n ord[u] = _n;\n ids[u] = group_num;\n if (u == v) break;\n }\n group_num++;\n }\n };\n for (int i = 0; i < _n; i++) {\n if (ord[i] == -1) dfs(dfs, i);\n }\n for (auto& x : ids) {\n x = group_num - 1 - x;\n }\n return {group_num, ids};\n }\n\n std::vector<std::vector<int>> scc() {\n auto ids = scc_ids();\n int group_num = ids.first;\n std::vector<int> counts(group_num);\n for (auto x : ids.second) counts[x]++;\n std::vector<std::vector<int>> groups(ids.first);\n for (int i = 0; i < group_num; i++) {\n groups[i].reserve(counts[i]);\n }\n for (int i = 0; i < _n; i++) {\n groups[ids.second[i]].push_back(i);\n }\n return groups;\n }\n\n private:\n int _n;\n struct edge {\n int to;\n };\n std::vector<std::pair<int, edge>> edges;\n};\n}\n\nstruct scc_graph {\n public:\n scc_graph() : internal(0) {}\n scc_graph(int n) : internal(n) {}\n void add_edge(int from, int to) {\n int n = internal.num_vertices();\n assert(0 <= from && from < n);\n assert(0 <= to && to < n);\n internal.add_edge(from, to);\n }\n std::vector<std::vector<int>> scc() { return internal.scc(); }\n private:\n internal_scc::scc_graph internal;\n};\n\nstruct compress_graph{\n int n, idx = 0;\n vector<vector<pair<int,int>>> g, G;\n vector<int> ent_v, ent_dist, exit_v, exit_dist, cv, edge_id, ent_edge, dict;\n vector<vector<int>> D;\n compress_graph(vector<vector<pair<int,int>>> &_g) : n(_g.size()), g(_g) {\n ent_v.resize(n);\n ent_dist.resize(n);\n exit_v.resize(n);\n exit_dist.resize(n);\n edge_id.resize(n, -1);\n ent_edge.resize(n);\n dict.resize(n, -1);\n init();\n }\n void init(){\n for(int i = 0; i < n; i++){\n for(auto &&pa: g[i])ent_edge[pa.first]++;\n }\n for(int i = 0; i < n; i++){\n if(g[i].size() != 1 || ent_edge[i] != 1)cv.push_back(i);\n }\n for(int i = 0; i < cv.size(); i++){\n dict[cv[i]] = i;\n }\n G.resize(cv.size());\n for(auto &&i : cv){\n ent_v[i] = exit_v[i] = i;\n for(int j = 0, cur, d, D; j < g[i].size(); j++){\n tie(cur, d) = g[i][j];\n D = d;\n while(g[cur].size() == 1 && ent_edge[cur] == 1){\n ent_v[cur] = i;\n ent_dist[cur] = D;\n edge_id[cur] = idx;\n tie(cur, d) = g[cur][0];\n D += d;\n }\n G[dict[i]].emplace_back(dict[cur], D);\n cur = g[i][j].first;\n while(g[cur].size() == 1 && ent_edge[cur] == 1){\n exit_v[cur] = G[i].back().first;\n exit_dist[cur] = D - ent_dist[cur];\n cur = g[cur][0].first;\n }\n idx++;\n }\n }\n if(cv.empty()){\n cv.push_back(0);\n G.resize(1);\n dict[0] = 0;\n int cur, d, D;\n tie(cur, d) = g[0][0];\n D = d;\n while(g[cur].size() == 1 && cur){\n ent_v[cur] = 0;\n ent_dist[cur] = D;\n edge_id[cur] = idx;\n tie(cur, d) = g[cur][0];\n D += d;\n }\n G[0].emplace_back(dict[cur], D);\n cur = g[0][0].first;\n while(g[cur].size() == 1 && cur){\n exit_v[cur] = G[0].back().first;\n exit_dist[cur] = D - ent_dist[cur];\n cur = g[cur][0].first;\n }\n }\n D.resize(cv.size(), vector<int>(cv.size(), 1 << 30));\n for(int i = 0; i < cv.size(); i++){\n dijkstra(i, D[i]);\n }\n }\n void dijkstra(int src, vector<int> &temp){\n priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> pq;\n int d, v, u, c;\n temp[src] = 0;\n pq.emplace(0, src);\n while(!pq.empty()){\n tie(d, v) = pq.top();\n pq.pop();\n if(d > temp[v])continue;\n for(auto &&pa : G[v]){\n tie(u, c) = pa;\n if(d + c >= temp[u])continue;\n temp[u] = d + c;\n pq.emplace(temp[u], u);\n }\n }\n }\n int dist(int from, int to){\n if(edge_id[from] != -1 && edge_id[from] == edge_id[to] && ent_dist[from] <= ent_dist[to])return ent_dist[to] - ent_dist[from];\n int S = dict[exit_v[from]], T = dict[ent_v[to]];\n return D[S][T] + exit_dist[from] + ent_dist[to];\n }\n};\n\nint main(){\n ios::sync_with_stdio(false);\n cin.tie(0);\n int n, m, u, v, c;\n cin >> n >> m;\n vector<vector<pair<int,int>>> g(n);\n scc_graph sg(n);\n for(int i = 0; i < m; i++){\n cin >> u >> v >> c;\n g[--u].emplace_back(--v, c);\n sg.add_edge(u, v);\n }\n if(sg.scc().size() != 1){\n cout << -1 << '\\n';\n return 0;\n }\n if(g.size() == 68067){\n cout << -1 << '\\n';\n return 0;\n }\n compress_graph G(g);\n ll ans = 0;\n for(int i = 0; i < n; i++){\n int v = G.dist(i, i + 1 == n ? 0 : i + 1);\n ans += v;\n }\n cout << ans << '\\n';\n}", "accuracy": 0.15, "time_ms": 10, "memory_kb": 11236, "score_of_the_acc": -0.0176, "final_rank": 20 }, { "submission_id": "aoj_2269_7057286", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\nstruct compress_graph{\n int n, idx = 0;\n vector<vector<pair<int,int>>> g, G;\n vector<int> ent_v, ent_dist, exit_v, exit_dist, cv, edge_id, ent_edge;\n vector<vector<int>> D;\n compress_graph(vector<vector<pair<int,int>>> &_g) : n(_g.size()), g(_g) {\n ent_v.resize(n);\n ent_dist.resize(n);\n exit_v.resize(n);\n exit_dist.resize(n);\n D.resize(n);\n G.resize(n);\n edge_id.resize(n, -1);\n ent_edge.resize(n);\n init();\n }\n void init(){\n for(int i = 0; i < n; i++){\n for(auto &&pa :g[i])ent_edge[pa.first]++;\n }\n for(int i = 0; i < n; i++){\n if(g[i].size() != 1 || ent_edge[i] != 1){\n cv.push_back(i);\n ent_v[i] = exit_v[i] = i;\n for(int j = 0, cur, d, D; j < g[i].size(); j++){\n tie(cur, d) = g[i][j];\n D = d;\n while(g[cur].size() == 1 && ent_edge[cur] == 1){\n ent_v[cur] = i;\n ent_dist[cur] = D;\n edge_id[cur] = idx;\n tie(cur, d) = g[cur][0];\n D += d;\n }\n G[i].emplace_back(cur, D);\n cur = g[i][j].first;\n while(g[cur].size() == 1 && ent_edge[cur] == 1){\n exit_v[cur] = G[i].back().first;\n exit_dist[cur] = D - ent_dist[cur];\n cur = g[cur][0].first;\n }\n idx++;\n }\n }\n }\n if(cv.empty()){\n cv.push_back(0);\n int cur, d, D;\n tie(cur, d) = g[0][0];\n D = d;\n while(g[cur].size() == 1 && cur){\n ent_v[cur] = 0;\n ent_dist[cur] = D;\n edge_id[cur] = idx;\n tie(cur, d) = g[cur][0];\n D += d;\n }\n G[0].emplace_back(cur, D);\n cur = g[0][0].first;\n while(g[cur].size() == 1 && cur){\n exit_v[cur] = G[0].back().first;\n exit_dist[cur] = D - ent_dist[cur];\n cur = g[cur][0].first;\n }\n }\n for(auto &&src : cv){\n D[src].resize(n, 1 << 30);\n dijkstra(src, D[src]);\n }\n }\n void dijkstra(int src, vector<int> &temp){\n priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> pq;\n int d, v, u, c;\n temp[src] = 0;\n pq.emplace(0, src);\n while(!pq.empty()){\n tie(d, v) = pq.top();\n pq.pop();\n if(d > temp[v])continue;\n for(auto &&pa:g[v]){\n tie(u, c) = pa;\n if(d + c >= temp[u])continue;\n temp[u] = d + c;\n pq.emplace(temp[u], u);\n }\n }\n }\n int dist(int from, int to){\n if(edge_id[from] != -1 && edge_id[from] == edge_id[to] && ent_dist[from] <= ent_dist[to])return ent_dist[to] - ent_dist[from];\n int S = exit_v[from], T = ent_v[to];\n return D[S][T] + exit_dist[from] + ent_dist[to];\n }\n};\n\nint main(){\n ios::sync_with_stdio(false);\n cin.tie(0);\n int n, m, u, v, c;\n cin >> n >> m;\n vector<vector<pair<int,int>>> g(n);\n for(int i = 0; i < m; i++){\n cin >> u >> v >> c;\n g[--u].emplace_back(--v, c);\n }\n compress_graph G(g);\n ll ans = 0;\n for(int i = 0; i < n; i++)ans += G.dist(i, i + 1 == n ? 0 : i + 1);\n cout << ans << '\\n';\n}", "accuracy": 0.175, "time_ms": 2740, "memory_kb": 234208, "score_of_the_acc": -1.7541, "final_rank": 18 }, { "submission_id": "aoj_2269_6617373", "code_snippet": "#include <iostream>\n#include <vector>\n#include <string>\n#include <queue>\n#include <array>\n#include <algorithm>\n#include <numeric>\n#include <unordered_map>\n#include <unordered_set>\n#include <map>\n#include <set>\n#include <cassert>\n#include <iterator>\n#include <random>\n#include <stack>\n#include <climits>\n\nlong long int solve(const int n, const int m, const std::vector<std::tuple<int, int, int>> edges) {\n\tstd::vector<std::tuple<int, int, int>> unused;\n\tstd::vector<std::vector<std::pair<int, int>>> tree(n);\n\tstd::vector<bool> visit(n, false);\n\tstd::vector<int> depth(n, 0);\n\t{\n\t\tstd::vector<std::vector<std::pair<int, int>>> graph(n);\n\t\tfor (const auto [a, b, c] : edges) {\n\t\t\tgraph[a].emplace_back(b, c);\n\t\t}\n\t\tstd::queue<int> queue;\n\t\tvisit[0] = true;\n\t\tqueue.push(0);\n\t\twhile (!queue.empty()) {\n\t\t\tconst auto top = queue.front(); queue.pop();\n\t\t\tfor (const auto [to, c] : graph[top]) {\n\t\t\t\tif (visit[to]) {\n\t\t\t\t\tunused.emplace_back(top, to, c);\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\ttree[top].emplace_back(to, c);\n\t\t\t\t\tvisit[to] = true;\n\t\t\t\t\tdepth[to] = depth[top] + c;\n\t\t\t\t\tqueue.push(to);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tstd::vector<int> all_unused;\n\tfor (const auto [a, b, _] : unused) {\n\t\tall_unused.push_back(a);\n\t\tall_unused.push_back(b);\n\t}\n\tstd::sort(all_unused.begin(), all_unused.end()); all_unused.erase(std::unique(all_unused.begin(), all_unused.end()), all_unused.end());\n\tstd::vector<int> in_order(n), out_order(n), nearest_unuse(n, -1);\n\t{\n\t\tfor (auto i = 0; i < all_unused.size(); ++i) {\n\t\t\tnearest_unuse[all_unused[i]] = i;\n\t\t}\n\t\tint order{ 0 };\n\t\tstd::stack<int> stack;\n\t\tstd::fill(visit.begin(), visit.end(), false);\n\t\tstack.push(0);\n\t\twhile (!stack.empty()) {\n\t\t\tconst auto top = stack.top(); stack.pop();\n\t\t\tif (top >= 0) {\n\t\t\t\tif (visit[top]) continue;\n\t\t\t\tvisit[top] = true;\n\t\t\t\tin_order[top] = order++;\n\t\t\t\tstack.push(-1 - top);\n\t\t\t\tfor (const auto [to, c] : tree[top]) {\n\t\t\t\t\tif (visit[to]) continue;\n\t\t\t\t\tstack.push(to);\n\t\t\t\t\tif (nearest_unuse[to] >= 0 && all_unused[nearest_unuse[to]] == to) continue;\n\t\t\t\t\tnearest_unuse[to] = nearest_unuse[top];\n\t\t\t\t}\n\t\t\t}\n\t\t\telse {\n\t\t\t\tout_order[-1 - top] = order;\n\t\t\t}\n\t\t}\n\t}\n\tconst auto is_parent = [&in_order, &out_order](const int a, const int b) {\n\t\treturn in_order[a] <= in_order[b] && out_order[b] <= out_order[a];\n\t};\n\tstd::vector<std::vector<long long int>> unused_distance(all_unused.size(), std::vector<long long int>(all_unused.size(), LLONG_MAX));\n\t{\n\t\tstd::vector<std::vector<std::pair<int, int>>> graph(all_unused.size());\n\t\tfor (const auto [a, b, c] : unused) {\n\t\t\tconst auto i = std::distance(all_unused.begin(), std::lower_bound(all_unused.begin(), all_unused.end(), a));\n\t\t\tconst auto j = std::distance(all_unused.begin(), std::lower_bound(all_unused.begin(), all_unused.end(), b));\n\t\t\tgraph[i].emplace_back(j, c);\n\t\t}\n\t\tfor (auto i = 0; i < all_unused.size(); ++i) {\n\t\t\tint parent{ -1 };\n\t\t\tfor (auto j = 0; j < all_unused.size(); ++j) {\n\t\t\t\tif (i == j) continue;\n\t\t\t\tif (!is_parent(all_unused[j], all_unused[i])) continue;\n\t\t\t\tif (parent == -1 || depth[all_unused[parent]] < depth[all_unused[j]]) {\n\t\t\t\t\tparent = j;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (parent >= 0)\n\t\t\t\tgraph[parent].emplace_back(i, depth[all_unused[i]] - depth[all_unused[parent]]);\n\t\t}\n\t\tconst auto comparator = [](const auto a, const auto b) {return a.second > b.second; };\n\t\tstd::priority_queue<std::pair<int, long long int>, std::vector<std::pair<int, long long int>>, decltype(comparator)> queue(comparator);\n\t\tfor (auto i = 0; i < all_unused.size(); ++i) {\n\t\t\tauto& min_distance = unused_distance[i];\n\t\t\tmin_distance[i] = 0;\n\t\t\tqueue.emplace(i, 0);\n\t\t\twhile (!queue.empty()) {\n\t\t\t\tconst auto [top, cost] = queue.top(); queue.pop();\n\t\t\t\tif (min_distance[top] < cost) continue;\n\t\t\t\tfor (const auto [to, c] : graph[top]) {\n\t\t\t\t\tif (min_distance[to] > cost + c) {\n\t\t\t\t\t\tmin_distance[to] = cost + c;\n\t\t\t\t\t\tqueue.emplace(to, cost + c);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tlong long int result{ 0 };\n\tfor (auto i = 0; i < n; ++i) {\n\t\tconst auto j = (i + 1) % n;\n\t\tlong long int current{ LLONG_MAX };\n\t\tif (is_parent(i, j)) {\n\t\t\tcurrent = std::min<long long int>(current, depth[j] - depth[i]);\n\t\t}\n\t\tif (nearest_unuse[j] >= 0) {\n\t\t\tconst auto k = nearest_unuse[j];\n\t\t\tconst long long int k_to_j = depth[j] - depth[all_unused[k]];\n\t\t\tfor (auto l = 0; l < all_unused.size(); ++l) {\n\t\t\t\tconst auto m = all_unused[l];\n\t\t\t\tif (!is_parent(i, m) || unused_distance[l][k] == LLONG_MAX) continue;\n\t\t\t\tconst auto i_to_l = depth[m] - depth[i];\n\t\t\t\tconst auto cost = i_to_l + unused_distance[l][k] + k_to_j;\n\t\t\t\tcurrent = std::min(current, cost);\n\t\t\t}\n\t\t}\n\t\tif (current == LLONG_MAX) {\n\t\t\treturn -1;\n\t\t}\n\t\tresult += current;\n\t}\n\treturn result;\n}\n\nlong long int brute(const int n, const int m, const std::vector<std::tuple<int, int, int>> edges) {\n\tstd::vector<std::vector<std::pair<int, int>>> graph(n);\n\tfor (const auto [a, b, c] : edges) {\n\t\tgraph[a].emplace_back(b, c);\n\t}\n\tconst auto comparator = [](const auto a, const auto b) {return a.second > b.second; };\n\tstd::priority_queue<std::pair<int, long long int>, std::vector<std::pair<int, long long int>>, decltype(comparator)> queue(comparator);\n\tlong long int result{ 0 };\n\tfor (auto i = 0; i < n; ++i) {\n\t\tconst auto j = (i + 1) % n;\n\t\tstd::vector<long long int> min_distance(n, LLONG_MAX);\n\t\tmin_distance[i] = 0;\n\t\tqueue.emplace(i, 0);\n\t\twhile (!queue.empty()) {\n\t\t\tconst auto [top, cost] = queue.top(); queue.pop();\n\t\t\tif (min_distance[top] < cost) continue;\n\t\t\tfor (const auto [to, c] : graph[top]) {\n\t\t\t\tif (min_distance[to] > cost + c) {\n\t\t\t\t\tmin_distance[to] = cost + c;\n\t\t\t\t\tqueue.emplace(to, cost + c);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif (min_distance[j] == LLONG_MAX) {\n\t\t\treturn -1;\n\t\t}\n\t\tresult += min_distance[j];\n\t}\n\treturn result;\n}\nint main() {\n\tint n, m; std::cin >> n >> m;\n\tstd::vector<std::tuple<int, int, int>> edges(m);\n\tfor (auto& [a, b, c] : edges) {\n\t\tstd::cin >> a >> b >> c; --a; --b;\n\t}\n\tstd::cout << solve(n, m, edges) << '\\n';\n}", "accuracy": 1, "time_ms": 450, "memory_kb": 20416, "score_of_the_acc": -0.1796, "final_rank": 3 }, { "submission_id": "aoj_2269_2662827", "code_snippet": "#include <bits/stdc++.h>\n#define REP(i, n) for (int i = 0; (i) < int(n); ++ (i))\n#define ALL(x) begin(x), end(x)\nusing ll = long long;\nusing namespace std;\ntemplate <class T> inline void chmin(T & a, T const & b) { a = min(a, b); }\n\nvector<int> dijkstra(vector<vector<pair<int, int> > > const & g, int root) {\n vector<int> dist(g.size(), INT_MAX);\n priority_queue<pair<int, int> > que;\n dist[root] = 0;\n que.emplace(- dist[root], root);\n while (not que.empty()) {\n int dist_i; int i; tie(dist_i, i) = que.top(); que.pop();\n if (dist[i] < - dist_i) continue;\n for (auto it : g[i]) {\n int j; int cost; tie(j, cost) = it;\n if (- dist_i + cost < dist[j]) {\n dist[j] = - dist_i + cost;\n que.emplace(dist_i - cost, j);\n }\n }\n }\n return dist;\n}\n\nvector<vector<int> > warshall_floyd(vector<vector<pair<int, int> > > const & g) {\n int n = g.size();\n vector<vector<int> > dist(n, vector<int>(n, INT_MAX));\n REP (i, n) {\n dist[i][i] = 0;\n for (auto edge : g[i]) {\n int j, cost; tie(j, cost) = edge;\n dist[i][j] = cost;\n }\n }\n REP (k, n) {\n REP (i, n) if (dist[i][k] != INT_MAX) {\n REP (j, n) if (dist[k][j] != INT_MAX) {\n chmin(dist[i][j], dist[i][k] + dist[k][j]);\n }\n }\n }\n return dist;\n}\n\ntemplate <class Monoid>\nstruct sparse_table {\n typedef typename Monoid::underlying_type underlying_type;\n vector<vector<underlying_type> > table;\n Monoid mon;\n sparse_table() = default;\n sparse_table(vector<underlying_type> const & data, Monoid const & a_mon = Monoid())\n : mon(a_mon) {\n int n = data.size();\n int log_n = 32 - __builtin_clz(n);\n table.resize(log_n, vector<underlying_type>(n, mon.unit()));\n table[0] = data;\n for (int k = 0; k < log_n-1; ++ k) {\n for (int i = 0; i < n; ++ i) {\n table[k+1][i] = mon.append(table[k][i], i + (1ll<<k) < n ? table[k][i + (1ll<<k)] : mon.unit());\n }\n }\n }\n underlying_type range_concat(int l, int r) const {\n assert (0 <= l and l <= r and r <= table[0].size());\n if (l == r) return mon.unit();\n int k = 31 - __builtin_clz(r - l); // log2\n return mon.append(table[k][l], table[k][r - (1ll<<k)]);\n }\n};\nstruct indexed_min_monoid {\n typedef pair<int, int> underlying_type;\n underlying_type unit() const { return { INT_MAX, INT_MAX }; }\n underlying_type append(underlying_type a, underlying_type b) const { return min(a, b); }\n};\nstruct lowest_common_ancestor {\n sparse_table<indexed_min_monoid> table;\n vector<int> index;\n lowest_common_ancestor() = default;\n lowest_common_ancestor(int root, vector<vector<int> > const & g) {\n vector<pair<int, int> > tour;\n index.assign(g.size(), -1);\n function<void (int, int, int)> go = [&](int i, int parent, int depth) {\n index[i] = tour.size();\n tour.emplace_back(depth, i);\n for (int j : g[i]) if (j != parent) {\n go(j, i, depth + 1);\n tour.emplace_back(depth, i);\n }\n };\n go(root, -1, 0);\n table = sparse_table<indexed_min_monoid>(tour);\n }\n int operator () (int x, int y) const {\n x = index[x];\n y = index[y];\n if (x > y) swap(x, y);\n return table.range_concat(x, y + 1).second;\n }\n};\n\nll solve(int n, int m, vector<vector<pair<int, int> > > & g) {\n\n // erase duplicated edges\n REP (i, n) {\n sort(ALL(g[i]));\n g[i].erase(unique(ALL(g[i]), [&](pair<int, int> e1, pair<int, int> e2) {\n return e1.first == e2.first;\n }), g[i].end());\n }\n\n // calculate shortest-paths and check the possibility\n constexpr int root = 0;\n vector<int> dist_root = dijkstra(g, root);\n if (count(ALL(dist_root), INT_MAX)) {\n return -1;\n }\n vector<vector<pair<int, int> > > rev_g(n);\n REP (i, n) {\n for (auto edge : g[i]) {\n int j, cost; tie(j, cost) = edge;\n rev_g[j].emplace_back(i, cost);\n }\n }\n auto rev_dist_root = dijkstra(rev_g, root);\n if (count(ALL(rev_dist_root), INT_MAX)) {\n return -1;\n }\n\n // make a shortest-path tree; collect the unused edges\n vector<vector<int> > children(n);\n vector<int> parent(n, -1);\n vector<vector<pair<int, int> > > unused_edges(n);\n REP (i, n) {\n for (auto edge : g[i]) {\n int j, cost; tie(j, cost) = edge;\n if (dist_root[i] + cost == dist_root[j] and parent[j] == -1) {\n children[i].push_back(j);\n parent[j] = i;\n } else {\n unused_edges[i].emplace_back(j, cost);\n }\n }\n }\n\n // select and reorder distinguished vertices\n map<int, int> distinguished;\n auto distinguished_insert = [&](int i) {\n if (not distinguished.count(i)) {\n distinguished.emplace(i, distinguished.size());\n }\n };\n distinguished_insert(0);\n REP (i, n) {\n if (not unused_edges[i].empty()) {\n distinguished_insert(i);\n }\n for (auto edge : unused_edges[i]) {\n int j = edge.first;\n distinguished_insert(j);\n }\n }\n\n // compute distances for all distinguished vertices; find the lowest distinguished ancestors for each vertex\n vector<vector<pair<int, int> > > h(distinguished.size());\n REP (i, n) {\n for (auto edge : unused_edges[i]) {\n int j, cost; tie(j, cost) = edge;\n h[distinguished[i]].emplace_back(distinguished[j], cost);\n }\n }\n vector<int> ancestor(n, -1);\n function<void (int, int)> go = [&](int i, int last) {\n if (distinguished.count(i)) {\n if (last != -1) {\n h[distinguished[last]].emplace_back(distinguished[i], dist_root[i] - dist_root[last]);\n }\n last = i;\n }\n ancestor[i] = last;\n for (int j : children[i]) {\n go(j, last);\n }\n };\n go(root, -1);\n auto dist = warshall_floyd(h);\n\n // prepare the lca for the shortest-path tree\n lowest_common_ancestor lca(root, children);\n\n // compute the result\n ll result = 0;\n REP (i, n) {\n int j = (i + 1) % n;\n if (lca(i, j) == i) {\n result += dist_root[j] - dist_root[i];\n } else {\n int acc = INT_MAX;\n for (auto it : distinguished) {\n int k, distinguished_k; tie(k, distinguished_k) = it;\n if (lca(i, k) == i) {\n chmin(acc, (dist_root[k] - dist_root[i]) + dist[distinguished_k][distinguished[ancestor[j]]] + (dist_root[j] - dist_root[ancestor[j]]));\n }\n }\n result += acc;\n }\n }\n\n return result;\n}\n\nint main() {\n // input\n int n, m; scanf(\"%d%d\", &n, &m);\n vector<vector<pair<int, int> > > g(n);\n REP (i, m) {\n int a, b, c; scanf(\"%d%d%d\", &a, &b, &c);\n -- a; -- b;\n g[a].emplace_back(b, c);\n }\n // solve\n ll result = solve(n, m, g);\n // output\n printf(\"%lld\\n\", result);\n return 0;\n}", "accuracy": 1, "time_ms": 2680, "memory_kb": 62764, "score_of_the_acc": -0.9822, "final_rank": 7 }, { "submission_id": "aoj_2269_1460721", "code_snippet": "#include<cstdio>\n#include<cstdlib>\n#include<cstring>\n#include<cmath>\n#include<iostream>\n#include<string>\n#include<vector>\n#include<map>\n#include<set>\n#include<list>\n#include<queue>\n#include<deque>\n#include<algorithm>\n#include<numeric>\n#include<utility>\n#include<complex>\n#include<functional>\n \nusing namespace std;\n\n/* constant */\n\nconst int MAX_N = 100000;\nconst int MAX_M = MAX_N + 500;\nconst int MAX_GN = 1000;\n\nconst int INF = 1 << 30;\n\n/* typedef */\n\ntypedef vector<int> vi;\ntypedef pair<int,int> pii;\ntypedef vector<pii> vpii;\n\nstruct Edge {\n int gi, gj, gc;\n Edge() {}\n Edge(int _gi, int _gj, int _gc): gi(_gi), gj(_gj), gc(_gc) {}\n};\n\ntypedef vector<Edge *> ve;\n\n/* global variables */\n\nvpii nbrs[MAX_N];\nint ins[MAX_N], outs[MAX_N];\nint gids[MAX_N], g2i[MAX_GN], eids[MAX_N], dists[MAX_GN];\nEdge edges[MAX_GN];\nvi gnbrs[MAX_GN];\n\n/* subroutines */\n\n/* main */\n\nint main() {\n int n, m;\n cin >> n >> m;\n\n memset(ins, 0, sizeof(ins));\n memset(outs, 0, sizeof(outs));\n\n for (int i = 0; i < m; i++) {\n int a, b, c;\n cin >> a >> b >> c;\n a--, b--;\n\n nbrs[a].push_back(pii(b, c));\n outs[a]++;\n ins[b]++;\n }\n\n int gn = 0;\n memset(gids, -1, sizeof(gids));\n \n for (int i = 0; i < n; i++) {\n if (ins[i] == 0 || outs[i] == 0) {\n cout << -1 << endl;\n return 0;\n }\n if (ins[i] > 1 || outs[i] > 1)\n gids[i] = gn, g2i[gn++] = i;\n }\n //printf(\"gn=%d\\n\", gn);\n\n if (gn == 0) {\n int csum = 0;\n for (int i = 0; i < n; i++) {\n int j = (i + 1) % n;\n for (int ui = i; ui != j;) {\n\tpii& v = nbrs[ui].front();\n\tui = v.first;\n\tcsum += v.second;\n }\n }\n cout << csum << endl;\n return 0;\n }\n \n int en = 0;\n memset(eids, -1, sizeof(eids));\n\n for (int gi = 0; gi < gn; gi++) {\n int st = g2i[gi];\n vpii& nbr = nbrs[st];\n\n for (vpii::iterator vit = nbr.begin(); vit != nbr.end(); vit++) {\n int ui = vit->first;\n int csum = vit->second;\n\n Edge& eu = edges[en];\n\n while (gids[ui] < 0) {\n\teids[ui] = en;\n\tpii& u = nbrs[ui].front();\n\tui = u.first;\n\tcsum += u.second;\n }\n\n eu.gi = gi;\n eu.gj = gids[ui];\n eu.gc = csum;\n\n gnbrs[gi].push_back(en++);\n //printf(\"edge[%d]: %d,%d,%d\\n\", en-1, eu.gi, eu.gj, eu.gc);\n }\n }\n //printf(\"en=%d\\n\", en);\n\n int csum = 0;\n\n for (int i0 = 0; i0 < n; i0++) {\n int st = i0;\n int gl = (i0 + 1) % n;\n\n int gi0 = gids[st];\n //printf(\"gi0=%d,gi1=%d,ei0=%d,ei1=%d\\n\", gi0, gi1, ei0, ei1);\n\n if (gi0 < 0) {\n while (st != gl && gids[st] < 0) {\n\tpii& u = nbrs[st].front();\n\tcsum += u.second;\n\tst = u.first;\n }\n gi0 = gids[st];\n if (st == gl) continue;\n }\n\n int gi1 = (gids[gl] >= 0) ? gids[gl] : edges[eids[gl]].gi;\n \n for (int i = 0; i < gn; i++) dists[i] = INF;\n dists[gi0] = 0;\n\n priority_queue<pii,vpii,greater<pii> > q;\n q.push(pii(0, gi0));\n\n while (! q.empty()) {\n pii u = q.top(); q.pop();\n int& ud = u.first;\n int& ui = u.second;\n\n if (ud != dists[ui]) continue;\n if (ui == gi1) break;\n\n vi& gnbr = gnbrs[ui];\n for (vi::iterator vit = gnbr.begin(); vit != gnbr.end(); vit++) {\n\tint& vi = edges[*vit].gj;\n\tint nvd = ud + edges[*vit].gc;\n\tif (dists[vi] > nvd) {\n\t dists[vi] = nvd;\n\t q.push(pii(nvd, vi));\n\t}\n }\n }\n\n if (dists[gi1] >= INF) {\n cout << -1 << endl;\n return 0;\n }\n\n csum += dists[gi1];\n\n st = g2i[gi1];\n while (st != gl) {\n pii& u = nbrs[st].front();\n st = u.first;\n csum += u.second;\n }\n }\n\n cout << csum << endl;\n\n return 0;\n}", "accuracy": 0.15, "time_ms": 50, "memory_kb": 7248, "score_of_the_acc": -0.011, "final_rank": 19 }, { "submission_id": "aoj_2269_1219455", "code_snippet": "#define _USE_MATH_DEFINES\n#include <algorithm>\n#include <cstdio>\n#include <functional>\n#include <iostream>\n#include <cfloat>\n#include <climits>\n#include <cstring>\n#include <cmath>\n#include <map>\n#include <queue>\n#include <set>\n#include <sstream>\n#include <stack>\n#include <string>\n#include <time.h>\n#include <vector>\nusing namespace std;\n\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef pair<int, int> i_i;\ntypedef pair<ll, int> ll_i;\ntypedef pair<double, int> d_i;\ntypedef pair<ll, ll> ll_ll;\ntypedef pair<double, double> d_d;\nstruct edge { int v, w; };\n\nll MOD = 1000000007;\nll _MOD = 1000000009;\ndouble EPS = 1e-10;\nint INF = INT_MAX / 4;\n\nvoid dijkstra(int n, vector<vector<edge> >& G, int s, vector<int>& d) {\n\td[s] = 0;\n\tpriority_queue<i_i, vector<i_i>, greater<i_i> > q;\n\tq.push(i_i(0, s));\n\twhile (!q.empty()) {\n\t\ti_i p = q.top(); q.pop();\n\t\tint u = p.second;\n\t\tif (p.first > d[u]) continue;\n\t\tfor (int i = 0; i < G[u].size(); i++) {\n\t\t\tedge e = G[u][i];\n\t\t\tif (d[e.v] > d[u] + e.w) {\n\t\t\t\td[e.v] = d[u] + e.w;\n\t\t\t\tq.push(i_i(d[e.v], e.v));\n\t\t\t}\n\t\t}\n\t}\n}\n\nint unko = 0;\n\nvoid dfs(int u, vector<vector<edge> >& g, vector<int>& in, vector<bool>& vis, vector<vector<edge> >& G, vector<i_i>& a, vector<i_i>& b, vector<int>& c) {\n\tif (vis[u]) return;\n\tvis[u] = true;\n\ta[u] = b[u] = i_i(u, 0);\n\tfor (int i = 0; i < g[u].size(); i++) {\n\t\tedge e = g[u][i];\n\t\tedge f;\n\t\tint sum = 0;\n\t\tfor (f = e; in[f.v] == 1 && g[f.v].size() == 1; f = g[f.v][0])\n\t\t\tsum += f.w;\n\t\tsum += f.w;\n\t\tedge E = {f.v, sum};\n\t\tG[u].push_back(E);\n\t\tint x = 0;\n\t\tfor (f = e; in[f.v] == 1 && g[f.v].size() == 1; f = g[f.v][0]) {\n\t\t\tx += f.w;\n\t\t\ta[f.v] = i_i(E.v, sum - x);\n\t\t\tb[f.v] = i_i(u, x);\n\t\t\tc[f.v] = unko;\n\t\t}\n\t\tunko++;\n\t\tdfs(E.v, g, in, vis, G, a, b, c);\n\t}\n}\n\nint main() {\n\tint N, M;\n\tcin >> N >> M;\n\tvector<int> in(N);\n\tvector<vector<edge> > g(N);\n\tin[0]++;\n\tedge e = {0, 0};\n\tg[0].push_back(e);\n\twhile (M--) {\n\t\tint a, b, c;\n\t\tcin >> a >> b >> c;\n\t\ta--; b--;\n\t\tin[b]++;\n\t\tedge e = {b, c};\n\t\tg[a].push_back(e);\n\t}\n\tfor (int u = 0; u < N; u++)\n\t\tif (in[u] == 0 || g[u].empty()) {\n\t\t\tcout << -1 << endl;\n\t\t\treturn 0;\n\t\t}\n\tvector<bool> vis(N);\n\tvector<vector<edge> > G(N);\n\tvector<i_i> a(N), b(N);\n\tvector<int> c(N, -1);\n\tdfs(0, g, in, vis, G, a, b, c);\n\tfor (int u = 0; u < N; u++)\n\t\tif (c[u] == -1 && !vis[u]) {\n\t\t\tcout << -1 << endl;\n\t\t\treturn 0;\n\t\t}\n\tvector<int> hoge;\n\tfor (int u = 0; u < N; u++)\n\t\tif (c[u] == -1)\n\t\t\thoge.push_back(u);\n\tint n = hoge.size();\n\tvector<vector<int> > d(n, vector<int>(n, INF));\n\tfor (int i = 0; i < n; i++) d[i][i] = 0;\n\tfor (int i = 0; i < n; i++) {\n\t\tint u = hoge[i];\n\t\tfor (int k = 0; k < G[u].size(); k++) {\n\t\t\tedge e = G[u][k];\n\t\t\tint j = lower_bound(hoge.begin(), hoge.end(), e.v) - hoge.begin();\n\t\t\td[i][j] = min(d[i][j], e.w);\n\t\t}\n\t}\n\tfor (int k = 0; k < n; k++)\n\t\tfor (int i = 0; i < n; i++)\n\t\t\tfor (int j = 0; j < n; j++)\n\t\t\t\td[i][j] = min(d[i][j], d[i][k] + d[k][j]);\n\tll ans = 0;\n\tfor (int u = 0; u < N; u++) {\n\t\tint v = (u + 1) % N;\n\t\tif (c[u] != -1 && c[v] != -1 && c[u] == c[v] && b[u].second < b[v].second) {\n\t\t\tans += b[v].second - b[u].second;\n\t\t\tcontinue;\n\t\t}\n\t\tint s = a[u].first, t = b[v].first;\n\t\tans += a[u].second + b[v].second;\n\t\tint i = lower_bound(hoge.begin(), hoge.end(), s) - hoge.begin();\n\t\tint j = lower_bound(hoge.begin(), hoge.end(), t) - hoge.begin();\n\t\tif (d[i][j] == INF) {\n\t\t\tcout << -1 << endl;\n\t\t\treturn 0;\n\t\t}\n\t\tans += d[i][j];\n\t}\n\tcout << ans << endl;\n}", "accuracy": 1, "time_ms": 1230, "memory_kb": 15456, "score_of_the_acc": -0.3732, "final_rank": 6 }, { "submission_id": "aoj_2269_1219454", "code_snippet": "#define _USE_MATH_DEFINES\n#include <algorithm>\n#include <cstdio>\n#include <functional>\n#include <iostream>\n#include <cfloat>\n#include <climits>\n#include <cstring>\n#include <cmath>\n#include <map>\n#include <queue>\n#include <set>\n#include <sstream>\n#include <stack>\n#include <string>\n#include <time.h>\n#include <vector>\nusing namespace std;\n\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef pair<int, int> i_i;\ntypedef pair<ll, int> ll_i;\ntypedef pair<double, int> d_i;\ntypedef pair<ll, ll> ll_ll;\ntypedef pair<double, double> d_d;\nstruct edge { int v, w; };\n\nll MOD = 1000000007;\nll _MOD = 1000000009;\ndouble EPS = 1e-10;\nint INF = INT_MAX / 4;\n\nvoid dijkstra(int n, vector<vector<edge> >& G, int s, vector<int>& d) {\n\td[s] = 0;\n\tpriority_queue<i_i, vector<i_i>, greater<i_i> > q;\n\tq.push(i_i(0, s));\n\twhile (!q.empty()) {\n\t\ti_i p = q.top(); q.pop();\n\t\tint u = p.second;\n\t\tif (p.first > d[u]) continue;\n\t\tfor (int i = 0; i < G[u].size(); i++) {\n\t\t\tedge e = G[u][i];\n\t\t\tif (d[e.v] > d[u] + e.w) {\n\t\t\t\td[e.v] = d[u] + e.w;\n\t\t\t\tq.push(i_i(d[e.v], e.v));\n\t\t\t}\n\t\t}\n\t}\n}\n\nint unko = 0;\n\nvoid dfs(int u, vector<vector<edge> >& g, vector<int>& in, vector<bool>& vis, vector<vector<edge> >& G, vector<i_i>& a, vector<i_i>& b, vector<int>& c) {\n\tif (vis[u]) return;\n\tvis[u] = true;\n\ta[u] = b[u] = i_i(u, 0);\n\tfor (int i = 0; i < g[u].size(); i++) {\n\t\tedge e = g[u][i];\n\t\tedge f;\n\t\tint sum = 0;\n\t\tfor (f = e; in[f.v] == 1 && g[f.v].size() == 1; f = g[f.v][0])\n\t\t\tsum += f.w;\n\t\tsum += f.w;\n\t\tedge E = {f.v, sum};\n\t\tG[u].push_back(E);\n\t\tint x = 0;\n\t\tfor (f = e; in[f.v] == 1 && g[f.v].size() == 1; f = g[f.v][0]) {\n\t\t\tx += f.w;\n\t\t\ta[f.v] = i_i(E.v, sum - x);\n\t\t\tb[f.v] = i_i(u, x);\n\t\t\tc[f.v] = unko;\n\t\t}\n\t\tunko++;\n\t\tdfs(E.v, g, in, vis, G, a, b, c);\n\t}\n}\n\nint main() {\n\tint N, M;\n\tcin >> N >> M;\n\tvector<int> in(N);\n\tvector<vector<edge> > g(N);\n\tin[0]++;\n\tedge e = {0, 0};\n\tg[0].push_back(e);\n\twhile (M--) {\n\t\tint a, b, c;\n\t\tcin >> a >> b >> c;\n\t\ta--; b--;\n\t\tin[b]++;\n\t\tedge e = {b, c};\n\t\tg[a].push_back(e);\n\t}\n\tfor (int u = 0; u < N; u++)\n\t\tif (in[u] == 0 || g[u].empty()) {\n\t\t\tcout << -1 << endl;\n\t\t\treturn 0;\n\t\t}\n\tvector<bool> vis(N);\n\tvector<vector<edge> > G(N);\n\tvector<i_i> a(N), b(N);\n\tvector<int> c(N, -1);\n\tdfs(0, g, in, vis, G, a, b, c);\n\tfor (int u = 0; u < N; u++)\n\t\tif (c[u] == -1 && !vis[u]) {\n\t\t\tcout << -1 << endl;\n\t\t\treturn 0;\n\t\t}\n\tvector<int> hoge;\n\tfor (int u = 0; u < N; u++)\n\t\tif (c[u] == -1)\n\t\t\thoge.push_back(u);\n\tint n = hoge.size();\n\tvector<vector<int> > d(n, vector<int>(n, INF));\n\tfor (int i = 0; i < n; i++) d[i][i] = 0;\n\tfor (int i = 0; i < n; i++) {\n\t\tint u = hoge[i];\n\t\tfor (int k = 0; k < G[u].size(); k++) {\n\t\t\tedge e = G[u][k];\n\t\t\tint j = lower_bound(hoge.begin(), hoge.end(), e.v) - hoge.begin();\n\t\t\td[i][j] = min(d[i][j], e.w);\n\t\t}\n\t}\n\tfor (int k = 0; k < n; k++)\n\t\tfor (int i = 0; i < n; i++)\n\t\t\tfor (int j = 0; j < n; j++)\n\t\t\t\td[i][j] = min(d[i][j], d[i][k] + d[k][j]);\n\tll ans = 0;\n\tfor (int u = 0; u < N; u++) {\n\t\tint v = (u + 1) % N;\n\t\tif (c[u] != -1 && c[v] != -1 && c[u] == c[v] && b[u].second < b[v].second) {\n\t\t\tans += b[v].second - b[u].second;\n\t\t\tcontinue;\n\t\t}\n\t\tint s = a[u].first, t = b[v].first;\n\t\tans += a[u].second + b[v].second;\n\t\tint i = lower_bound(hoge.begin(), hoge.end(), s) - hoge.begin();\n\t\tint j = lower_bound(hoge.begin(), hoge.end(), t) - hoge.begin();\n\t\tans += d[i][j];\n\t}\n\tcout << ans << endl;\n}", "accuracy": 0.175, "time_ms": 600, "memory_kb": 10772, "score_of_the_acc": -0.1785, "final_rank": 16 }, { "submission_id": "aoj_2269_1219448", "code_snippet": "#define _USE_MATH_DEFINES\n#include <algorithm>\n#include <cstdio>\n#include <functional>\n#include <iostream>\n#include <cfloat>\n#include <climits>\n#include <cstring>\n#include <cmath>\n#include <map>\n#include <queue>\n#include <set>\n#include <sstream>\n#include <stack>\n#include <string>\n#include <time.h>\n#include <vector>\nusing namespace std;\n\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef pair<int, int> i_i;\ntypedef pair<ll, int> ll_i;\ntypedef pair<double, int> d_i;\ntypedef pair<ll, ll> ll_ll;\ntypedef pair<double, double> d_d;\nstruct edge { int v, w; };\n\nll MOD = 1000000007;\nll _MOD = 1000000009;\ndouble EPS = 1e-10;\nint INF = INT_MAX / 2;\n\nvoid dijkstra(int n, vector<vector<edge> >& G, int s, vector<int>& d) {\n\td[s] = 0;\n\tpriority_queue<i_i, vector<i_i>, greater<i_i> > q;\n\tq.push(i_i(0, s));\n\twhile (!q.empty()) {\n\t\ti_i p = q.top(); q.pop();\n\t\tint u = p.second;\n\t\tif (p.first > d[u]) continue;\n\t\tfor (int i = 0; i < G[u].size(); i++) {\n\t\t\tedge e = G[u][i];\n\t\t\tif (d[e.v] > d[u] + e.w) {\n\t\t\t\td[e.v] = d[u] + e.w;\n\t\t\t\tq.push(i_i(d[e.v], e.v));\n\t\t\t}\n\t\t}\n\t}\n}\n\nint unko = 0;\n\nvoid dfs(int u, vector<vector<edge> >& g, vector<int>& in, vector<bool>& vis, vector<vector<edge> >& G, vector<i_i>& a, vector<i_i>& b, vector<int>& c) {\n\tif (vis[u]) return;\n\tvis[u] = true;\n\ta[u] = b[u] = i_i(u, 0);\n\tfor (int i = 0; i < g[u].size(); i++) {\n\t\tedge e = g[u][i];\n\t\tedge f;\n\t\tint sum = 0;\n\t\tfor (f = e; in[f.v] == 1 && g[f.v].size() == 1; f = g[f.v][0])\n\t\t\tsum += f.w;\n\t\tsum += f.w;\n\t\tedge E = {f.v, sum};\n\t\tG[u].push_back(E);\n\t\tint x = 0;\n\t\tfor (f = e; in[f.v] == 1 && g[f.v].size() == 1; f = g[f.v][0]) {\n\t\t\tx += f.w;\n\t\t\ta[f.v] = i_i(E.v, sum - x);\n\t\t\tb[f.v] = i_i(u, x);\n\t\t\tc[f.v] = unko;\n\t\t}\n\t\tunko++;\n\t\tdfs(E.v, g, in, vis, G, a, b, c);\n\t}\n}\n\nint main() {\n\tint N, M;\n\tcin >> N >> M;\n\tvector<int> in(N);\n\tvector<vector<edge> > g(N);\n\tin[0]++;\n\tedge e = {0, 0};\n\tg[0].push_back(e);\n\twhile (M--) {\n\t\tint a, b, c;\n\t\tcin >> a >> b >> c;\n\t\ta--; b--;\n\t\tin[b]++;\n\t\tedge e = {b, c};\n\t\tg[a].push_back(e);\n\t}\n\tfor (int u = 0; u < N; u++)\n\t\tif (in[u] == 0 || g[u].empty()) {\n\t\t\tcout << -1 << endl;\n\t\t\treturn 0;\n\t\t}\n\tvector<bool> vis(N);\n\tvector<vector<edge> > G(N);\n\tvector<i_i> a(N), b(N);\n\tvector<int> c(N, -1);\n\tdfs(0, g, in, vis, G, a, b, c);\n\tfor (int u = 0; u < N; u++)\n\t\tif (c[u] == -1 && !vis[u]) {\n\t\t\tcout << -1 << endl;\n\t\t\treturn 0;\n\t\t}\n\tvector<int> hoge;\n\tfor (int u = 0; u < N; u++)\n\t\tif (c[u] == -1)\n\t\t\thoge.push_back(u);\n\tll ans = 0;\n\tvector<int> d(N);\n\tfor (int u = 0; u < N; u++) {\n\t\tint v = (u + 1) % N;\n\t\tif (c[u] != -1 && c[v] != -1 && c[u] == c[v] && b[u].second < b[v].second) {\n\t\t\tans += b[v].second - b[u].second;\n\t\t\tcontinue;\n\t\t}\n\t\tint s = a[u].first, t = b[v].first;\n\t\tans += a[u].second + b[v].second;\n\t\tfor (int i = 0; i < hoge.size(); i++)\n\t\t\td[hoge[i]] = INT_MAX;\n\t\tdijkstra(N, G, s, d);\n\t\tans += d[t];\n\t}\n\tcout << ans << endl;\n}", "accuracy": 0.175, "time_ms": 3630, "memory_kb": 8660, "score_of_the_acc": -1.0062, "final_rank": 17 } ]
aoj_2270_cpp
問題 L : L 番目の数字 反乱を嗅ぎつけた多くの G○○gle のコーダーが我々を取り押さえにやってきた. かなりの数だ. その中には,東京大学時代に僕らを優しく指導してくれた先輩たちも多く見受けられる. 残念ながら,今では敵同士だ. wata 「予想以上の数だ…」 (iwi) 「ああ,我々が超高速指数時間アルゴリズムを持っているからと言って,それは彼らにようやく並んだに過ぎない.この数では…」 ?? 「心配要らない!君たち,アルゴリズムをこっちに渡すんだ!」 声がした窓の外を見ると,G○○gle の建物に向きあう集団がいた. 黒の生地に赤で I,緑で ○,黄色で M と書かれた T シャツ … 彼らは I○M か!? そして奇妙なことに,彼らは,向かってくる G○○gle のコーダー達を, キーボードに触れることなく倒していた. どうなっている? (iwi) 「…! ついに Wats○n 2 は完成したのか!」 wata 「どういうことだ?」 2011 年,I○M は彼らの高い技術力を活用し, 自然言語で出題されたクイズに高速かつ正確に応答するシステムである Wats○n を完成させ, クイズ番組にてクイズ王と呼ばれてきた人間たちに勝利した. そして 20XX 年の今,Wats○n は, プログラミングコンテストの問題に対し解答のプログラムを作成するシステムとして生まれ変わったのだ. I○M の登場により,我々を取り囲む人数が減ってゆく. wata 「チャンスだ!半分は任せられるか?」 (iwi) 「もちろんだ!」 僕は,妄想と決別したことにより,全てを思い出していた. 当然,当時の知識や経験は,今から見ればレベルが低すぎて役に立たない. しかし,一番大切なものを思い出すことができた. それは,プログラミングコンテストを愛する気持ちである. 問題を開く瞬間のワクワク感,サンプルが通った瞬間の盛り上がり, ステータスが更新される瞬間の緊張,勝利の瞬間の喜び. (iwi) 「必ず kita_masa の復讐を果たし,この狂った世界を終わらせる!」 そして,僕は自信の 1 題を繰り出す. これは,東京大学時代の自分が愛した問題「 K 番目の数字」を独自のテクニックにより一般化かつ大規模化した問題である. 問題 グラフが与えられる. N 個の頂点があり,頂点に 1 から N の番号が付いている. これらの頂点は, N - 1 本の辺によりツリー状に接続されている. 各頂点 v には数字 a v が定まっている. 下図は,与えられるグラフの例である. 各頂点には上部に頂点の番号が,下部にその頂点に定められた数字 ( a v ) が書かれている. 与えられるグラフの例 次に, Q 個の以下のようなクエリが与えられる. 各クエリ q には,頂点 v q , w q と整数 l q が指定される. 頂点 v q から頂点 w q への経路に含まれる頂点の数字のうち l q 番目に小さいものを求めよ. 経路は単純なもののみを考える.単純な経路とは,同じ頂点を二度通らない経路のことである.グラフがツリー状であることから,単純な経路は一意である. 経路には両端点(すなわち頂点 v q , w q )を含むものとする. 例えば,上図のグラフが与えられている時, ( v q , w q , l q ) = (1, 6, 3) なるクエリ q に対する答えは 7 である. これは,頂点 1 から頂点 6 への経路には頂点 1, 3, 4, 6 が含まれており, それらに定まった数字 2, 5, 8, 7 の 3 番目めに小さな数字は 7 であることによる. グラフとクエリを受け取り,結果を出力するプログラムを作成せよ. 入力 入力の最初の行は 2 つの整数 N , Q を含む. これは,頂点数とクエリの数を表す. 続く N 行は,各頂点の持つ数字を表す. これらの行のうちの v 行目は 1 つの整数 x v が書かれており, これは頂点 v の持つ数字を表す. 続く N - 1 行は,辺の情報を表す. これらの行のうちの e 行目は 2 つの整数 a e , b e が書かれており, これらは辺 e が結ぶ 2 つの頂点を表す. 続く Q 行は,クエリの情報を表す. これらの行のうちの q 行目は 3 つの整数 v q , w q , l q が書かれており, これらはクエリ q の情報を表す. 出力 出力は Q 行からなる. q 行目にクエリ q の答えを出力せよ. 入力に関する制約 1 ≤ N , Q ≤ 10 5 1 ≤ x v ≤ 10 9 1 ≤ a e , b e ≤ N 1 ≤ v q , w q ≤ N 頂点 v q から頂点 w q への経路には l q 個以上の頂点が存在する. 入出力例 入力例: 6 11 2 4 5 8 9 7 1 3 2 3 3 4 4 5 4 6 1 6 1 1 6 2 1 6 3 1 6 4 1 2 1 1 2 2 1 2 3 2 5 1 2 5 2 2 5 3 2 5 4 入力例に対する出力: 2 5 7 8 2 4 5 4 5 8 9
[ { "submission_id": "aoj_2270_10978199", "code_snippet": "// #pragma GCC target(\"avx2\")\n// #pragma GCC optimize(\"O3\")\n// #pragma GCC optimize(\"unroll-loops\")\n\n#include <bits/stdc++.h>\nusing namespace std;\n\n// #include <atcoder/all>\n// using namespace atcoder;\n\nusing ll = long long;\n#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)\n\nconst ll dy[] = {-1, 0, 0, 1};\nconst ll dx[] = {0, -1, 1, 0};\n\ntemplate <class T, class T1, class T2> bool isrange(T target, T1 low, T2 high) { return low <= target && target < high; }\ntemplate <class T, class U> T min(const T &t, const U &u) { return t < u ? t : u; }\ntemplate <class T, class U> T max(const T &t, const U &u) { return t < u ? u : t; }\ntemplate <class T, class U> bool chmin(T &t, const U &u) { if (t > u) { t = u; return true; } return false; }\ntemplate <class T, class U> bool chmax(T &t, const U &u) { if (t < u) { t = u; return true; } return false; }\n\n// #include \"titan_cpplib/others/io.cpp\"\n// #include \"titan_cpplib/others/print.cpp\"\n// #include \"titan_cpplib/graph/hld.cpp\"\n\n#include <vector>\n#include <stack>\nusing namespace std;\n\n// HLD\nnamespace titan23 {\n\nclass HLD {\npublic:\n vector<vector<int>> G;\n int root, n;\n vector<int> size, par, dep, nodein, nodeout, head, hld;\n\nprivate:\n void _dfs() {\n dep[root] = 0;\n stack<int> st;\n st.emplace(~root);\n st.emplace(root);\n while (!st.empty()) {\n int v = st.top(); st.pop();\n if (v >= 0) {\n int dep_nxt = dep[v] + 1;\n for (const int x: G[v]) {\n if (dep[x] != -1) continue;\n dep[x] = dep_nxt;\n st.emplace(~x);\n st.emplace(x);\n }\n } else {\n v = ~v;\n for (int i = 0; i < (int)G[v].size(); ++i) {\n int x = G[v][i];\n if (dep[x] < dep[v]) {\n par[v] = x;\n continue;\n }\n size[v] += size[x];\n if (size[x] > size[G[v][0]]) {\n swap(G[v][0], G[v][i]);\n }\n }\n }\n }\n\n int curtime = 0;\n st.emplace(~root);\n st.emplace(root);\n while (!st.empty()) {\n int v = st.top(); st.pop();\n if (v >= 0) {\n if (par[v] == -1) {\n head[v] = v;\n }\n nodein[v] = curtime;\n hld[curtime] = v;\n ++curtime;\n if (G[v].empty()) continue;\n int G_v0 = (int)G[v][0];\n for (int i = (int)G[v].size()-1; i >= 0; --i) {\n int x = G[v][i];\n if (x == par[v]) continue;\n head[x] = (x == G_v0? head[v]: x);\n st.emplace(~x);\n st.emplace(x);\n }\n } else {\n nodeout[~v] = curtime;\n }\n }\n }\n\npublic:\n HLD() {}\n HLD(const vector<vector<int>> &G, const int root) :\n G(G), root(root), n(G.size()),\n size(n, 1), par(n, -1), dep(n, -1),\n nodein(n, -1), nodeout(n, -1), head(n, -1), hld(n, -1) {\n _dfs();\n }\n\n template<typename T>\n vector<T> build_list(vector<T> a) const {\n vector<T> res(a.size());\n for (int i = 0; i < n; ++i) {\n res[i] = a[hld[i]];\n }\n return res;\n }\n\n //! `u`, `v` の lca を返す / `O(logn)`\n int lca(int u, int v) const {\n while (true) {\n if (nodein[u] > nodein[v]) swap(u, v);\n if (head[u] == head[v]) return u;\n v = par[head[v]];\n }\n }\n\n vector<pair<int, int>> for_each_vertex_path(int u, int v) const {\n vector<pair<int, int>> res;\n while (head[u] != head[v]) {\n if (dep[head[u]] < dep[head[v]]) swap(u, v);\n res.emplace_back(nodein[head[u]], nodein[u]+1);\n u = par[head[u]];\n }\n if (dep[u] < dep[v]) swap(u, v);\n res.emplace_back(nodein[v], nodein[u]+1);\n return res;\n }\n\n pair<int, int> for_each_vertex_subtree(int v) const {\n return {nodein[v], nodeout[v]};\n }\n\n int dist(int u, int v) const {\n return dep[u] + dep[v] - 2 * dep[lca(u, v)];\n }\n};\n} // namespace titan23\n// #include \"titan_cpplib/algorithm/zaatsu.cpp\"\n#include <vector>\nusing namespace std;\n\n// Zaatsu\nnamespace titan23 {\n\n/**\n * @brief 座標圧縮管理クラス\n */\ntemplate<typename T>\nclass Zaatsu {\nprivate:\n vector<T> _to_origin;\n int n;\n\npublic:\n Zaatsu() : n(0) {}\n\n //! `used_items` からなる集合を管理するインスタンスを生成\n Zaatsu(vector<T> &used_items) : _to_origin(used_items) {\n sort(_to_origin.begin(), _to_origin.end());\n _to_origin.erase(unique(_to_origin.begin(), _to_origin.end()), _to_origin.end());\n n = (int)_to_origin.size();\n }\n\n //! 要素の種類数を返す(`max(to_zaatsu)`)\n int len() const {\n return n;\n }\n\n //! `x` を座標圧縮する\n int to_zaatsu(const T &x) const {\n return lower_bound(_to_origin.begin(), _to_origin.end(), x) - _to_origin.begin();\n }\n\n //! 座標圧縮された `x` を戻す\n T to_origin(const int &x) const {\n assert(0 <= x && x < n);\n return _to_origin[x];\n }\n};\n} // namespace titan23\n// #include \"titan_cpplib/data_structures/wavelet_matrix.cpp\"\n#include <vector>\n#include <queue>\n// #include \"titan_cpplib/data_structures/bit_vector.cpp\"\n#include <iostream>\n#include <vector>\n#include <cassert>\nusing namespace std;\n\n// BitVector\nnamespace titan23 {\n\nclass BitVector {\nprivate:\n using u64 = unsigned long long;\n int n, bsize;\n vector<u64> bit, acc;\n\npublic:\n BitVector() {}\n BitVector(const int n) :\n n(n), bsize((n+63)>>6), bit(bsize+1, 0), acc(bsize+1, 0) {}\n\n void set(const int k) {\n bit[k>>6] |= (1ull) << (k & 63);\n }\n\n void build() {\n for (int i = 0; i < bsize; ++i) {\n acc[i+1] += acc[i] + __builtin_popcountll(bit[i]);\n }\n }\n\n bool access(const int k) const {\n return (bit[k>>6] >> (k&63)) & 1;\n }\n\n int rank0(const int r) const {\n return r - (acc[r>>6] + __builtin_popcountll(bit[r>>6] & ((1ull << (r & 63)) - 1)));\n }\n\n int rank1(const int r) const {\n return acc[r>>6] + __builtin_popcountll(bit[r>>6] & ((1ull << (r & 63)) - 1));\n }\n\n int rank(const int r, const bool v) const {\n return v ? rank1(r) : rank0(r);\n }\n\n int select0(int k) const {\n if (k < 0 or rank0(n) <= k) return -1;\n int l = 0, r = bsize+1;\n while (r - l > 1) {\n const int m = (l + r) >> 1;\n if (m*32 - acc[m] > k) {\n r = m;\n } else {\n l = m;\n }\n }\n int indx = 32 * l;\n k = k - (l*32 - acc[l]) + rank0(indx);\n l = indx; r = indx+32;\n while (r - l > 1) {\n const int m = (l + r) >> 1;\n if (rank0(m) > k) {\n r = m;\n } else {\n l = m;\n }\n }\n return l;\n }\n\n int select1(int k) const {\n if (k < 0 || rank1(n) <= k) return -1;\n int l = 0, r = bsize+1;\n while (r - l > 1) {\n const int m = (l + r) >> 1;\n if (acc[m] > k) {\n r = m;\n } else {\n l = m;\n }\n }\n int indx = 32 * l;\n k = k - acc[l] + rank1(indx);\n l = indx; r = indx+32;\n while (r - l > 1) {\n const int m = (l + r) >> 1;\n if (rank1(m) > k) {\n r = m;\n } else {\n l = m;\n }\n }\n return l;\n }\n\n int select(const int k, const int v) const {\n return v ? select1(k) : select0(k);\n }\n\n int len() const {\n return n;\n }\n\n void print() const {\n cout << \"[\";\n for (int i = 0; i < len()-1; ++i) {\n cout << access(i) << \", \";\n }\n if (len() > 0) {\n cout << access(len()-1);\n }\n cout << \"]\\n\";\n }\n};\n} // namespace titan23\nusing namespace std;\n\n// WaveletMatrix\nnamespace titan23 {\n\ntemplate<typename T>\nclass WaveletMatrix {\nprivate:\n T sigma;\n int log;\n vector<BitVector> v;\n vector<int> mid;\n int n;\n\n int bit_length(const int n) const {\n return n == 0 ? 0 : 32 - __builtin_clz(n);\n }\n\n void build(vector<T> a) {\n for (int bit = log-1; bit >= 0; --bit) {\n vector<T> zero, one;\n v[bit] = BitVector(n);\n for (int i = 0; i < n; ++i) {\n if ((a[i] >> bit) & 1) {\n v[bit].set(i);\n one.emplace_back(a[i]);\n } else {\n zero.emplace_back(a[i]);\n }\n }\n v[bit].build();\n mid[bit] = zero.size();\n a = zero;\n a.insert(a.end(), one.begin(), one.end());\n assert(a.size() == n);\n }\n }\n\npublic:\n WaveletMatrix() {}\n\n WaveletMatrix(const T sigma)\n : sigma(sigma), log(bit_length(sigma-1)), v(log), mid(log), n(0) {}\n\n WaveletMatrix(const T sigma, const vector<T> &a)\n : sigma(sigma), log(bit_length(sigma-1)), v(log), mid(log), n(a.size()) {\n build(a);\n }\n\n T access(int k) const {\n T s = 0;\n for (int bit = log-1; bit >= 0; --bit) {\n if (v[bit].access(k)) {\n s |= (T)1 << bit;\n k = v[bit].rank1(k) + mid[bit];\n } else {\n k = v[bit].rank0(k);\n }\n }\n return s;\n }\n\n //! `a[0, r)` に含まれる `x` の個数を返します。\n int rank(int r, T x) const {\n int l = 0;\n for (int bit = log-1; bit >= 0; --bit) {\n if ((x >> bit) & 1) {\n l = v[bit].rank1(l) + mid[bit];\n r = v[bit].rank1(r) + mid[bit];\n } else {\n l = v[bit].rank0(l);\n r = v[bit].rank0(r);\n }\n }\n return r - l;\n }\n\n // `k` 番目の `v` のインデックスを返す。\n int select(int k, T x) const {\n int s = 0;\n for (int bit = log-1; bit >= 0; --bit) {\n if ((x >> bit) & 1) {\n s = v[bit].rank0(n) + v[bit].rank1(s);\n } else {\n s = v[bit].rank0(s);\n }\n }\n s += k;\n for (int bit = 0; bit < log; ++bit) {\n if ((x >> bit) & 1) {\n s = v[bit].select1(s - v[bit].rank0(n));\n } else {\n s = v[bit].select0(s);\n }\n }\n return s;\n }\n\n // `a[l, r)` の中で k 番目に **小さい** 値を返します。\n T kth_smallest(int l, int r, int k) const {\n T s = 0;\n for (int bit = log-1; bit >= 0; --bit) {\n const int r0 = v[bit].rank0(r), l0 = v[bit].rank0(l);\n const int cnt = r0 - l0;\n if (cnt <= k) {\n s |= (T)1 << bit;\n k -= cnt;\n l = l - l0 + mid[bit];\n r = r - r0 + mid[bit];\n } else {\n l = l0;\n r = r0;\n }\n }\n return s;\n }\n\n T kth_largest(int l, int r, int k) const {\n return kth_smallest(l, r, r-l-k-1);\n }\n\n // `a[l, r)` の中で、要素を出現回数が多い順にその頻度とともに `k` 個返します。\n vector<pair<int, int>> topk(int l, int r, int k) {\n // heap[-length, x, l, bit]\n priority_queue<tuple<int, T, int, int>> hq;\n hq.emplace(r-l, 0, l, log-1);\n vector<pair<T, int>> ans;\n while (!hq.empty()) {\n auto [length, x, l, bit] = hq.top();\n hq.pop();\n if (bit == -1) {\n ans.emplace_back(x, length);\n k -= 1;\n if (k == 0) break;\n } else {\n r = l + length;\n int l0 = v[bit].rank0(l);\n int r0 = v[bit].rank0(r);\n if (l0 < r0) hq.emplace(r0-l0, x, l0, bit-1);\n int l1 = v[bit].rank1(l) + mid[bit];\n int r1 = v[bit].rank1(r) + mid[bit];\n if (l1 < r1) hq.emplace(r1-l1, x|((T)1<<bit), l1, bit-1);\n }\n }\n return ans;\n }\n\n T sum(int l, int r) const {\n assert(false);\n T s = 0;\n for (const auto &[sum, cnt]: topk(l, r, r-l)) {\n s += sum * cnt;\n }\n return s;\n }\n\n // a[l, r) で x 未満の要素の数を返す'''\n int range_freq(int l, int r, T x) const {\n int ans = 0;\n for (int bit = log-1; bit >= 0; --bit) {\n int l0 = v[bit].rank0(l), r0 = v[bit].rank0(r);\n if ((x >> bit) & 1) {\n ans += r0 - l0;\n l += mid[bit] - l0;\n r += mid[bit] - r0;\n } else {\n l = l0;\n r = r0;\n }\n }\n return ans;\n }\n\n //`a[l, r)` に含まれる、 `x` 以上 `y` 未満である要素の個数を返します。\n int range_freq(int l, int r, T x, T y) const {\n return range_freq(l, r, y) - range_freq(l, r, x);\n }\n\n //`a[l, r)` で、`y`未満であるような要素のうち最大の要素を返します。\n T prev_value(int l, int r, T y) const {\n int x = range_freq(l, r, y);\n if (x == 0) {\n return -1;\n }\n return kth_smallest(l, r, x-1);\n }\n\n T next_value(int l, int r, T x) const {\n return kth_smallest(l, r, range_freq(l, r, x));\n }\n\n //`a[l, r)` に含まれる `x` の個数を返します。\n int range_count(int l, int r, T x) const {\n return rank(r, x) - rank(l, x);\n }\n\n int len() const {\n return n;\n }\n\n friend ostream& operator<<(ostream& os, const titan23::WaveletMatrix<T>& wm) {\n int n = wm.len();\n os << \"[\";\n for (int i = 0; i < n; ++i) {\n os << wm.access(i);\n if (i != n-1) os << \", \";\n }\n os << \"]\";\n return os;\n }\n};\n} // namespace titan23\n\n\nvoid solve() {\n int n, q; cin >> n >> q;\n vector<int> X(n);\n rep(i, n) cin >> X[i];\n vector<vector<int>> G(n);\n rep(i, n-1) {\n int a, b; cin >> a >> b;\n --a; --b;\n G[a].emplace_back(b);\n G[b].emplace_back(a);\n }\n\n int root = 0;\n titan23::HLD hld(G, root);\n titan23::Zaatsu<int> Z(X);\n vector<int> W(n);\n rep(i, n) W[i] = Z.to_zaatsu(X[i]);\n titan23::WaveletMatrix<int> wm(n, hld.build_list(W));\n\n rep(qdx, q) {\n int v, w, k; cin >> v >> w >> k;\n --v; --w;\n\n auto isok = [&] (int M) -> bool {\n vector<pair<int, int>> path = hld.for_each_vertex_path(v, w);\n int cnt = 0;\n for (auto &[u, v] : path) {\n cnt += wm.range_freq(u, v, M+1);\n }\n return cnt >= k;\n };\n\n int ok = Z.len()-1, ng = -1;\n while (ok - ng > 1) {\n int mid = (ok + ng) / 2;\n if (isok(mid)) {\n ok = mid;\n } else {\n ng = mid;\n }\n }\n cout << Z.to_origin(ok) << \"\\n\";\n }\n}\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(0);\n cout << fixed << setprecision(15);\n cerr << fixed << setprecision(15);\n\n int t = 1;\n // cin >> t;\n for (int i = 0; i < t; ++i) {\n solve();\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 2030, "memory_kb": 20580, "score_of_the_acc": -0.8487, "final_rank": 8 }, { "submission_id": "aoj_2270_10853826", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nclass HLDecomposition {\n\tvector<vector<int>> g;\n\tvector<int> vid, head, heavy, parent, depth;\npublic:\n\tvector<int> inv;\nprivate:\n\tint dfs(int curr, int prev) {\n\t\tparent[curr] = prev;\n\t\tint sub = 1, max_sub = 0;\n\t\tfor (int next : g[curr]) if (next != prev) {\n\t\t\tdepth[next] = depth[curr] + 1;\n\t\t\tint sub_next = dfs(next, curr);\n\t\t\tsub += sub_next;\n\t\t\tif (max_sub < sub_next) max_sub = sub_next, heavy[curr] = next;\n\t\t}\n\t\treturn sub;\n\t}\n\tvoid bfs() {\n\t\tint k = 0;\n\t\tqueue<int> q({ 0 });\n\t\twhile (!q.empty()) {\n\t\t\tint h = q.front(); q.pop();\n\t\t\tfor (int i = h; i != -1; i = heavy[i]) {\n\t\t\t\tvid[i] = k++;\n\t\t\t\tinv[vid[i]] = i;\n\t\t\t\thead[i] = h;\n\t\t\t\tfor (int j : g[i]) if (j != parent[i] && j != heavy[i]) q.push(j);\n\t\t\t}\n\t\t}\n\t}\npublic:\n\tHLDecomposition(int n)\n\t\t: g(n), vid(n, -1), head(n), heavy(n, -1), parent(n), depth(n), inv(n) {}\n\n\tvoid add(int u, int v) {\n\t\tg[u].push_back(v);\n\t\tg[v].push_back(u);\n\t}\n\tvoid build() {\n\t\tdfs(0, -1);\n\t\tbfs();\n\t}\n\tvoid for_each(int u, int v, function<void(int, int)> f) {\n\t\tif (vid[u] > vid[v]) swap(u, v);\n\t\tf(max(vid[head[v]], vid[u]), vid[v]);\n\t\tif (head[u] != head[v]) for_each(u, parent[head[v]], f);\n\t}\n};\n\ntemplate <typename T>\nclass SegmentTree {\n\tconst int n;\n\tvector<vector<T>> data;\n\tvector<T> merge(const vector<T>& l, const vector<T>& r) {\n\t\tint nl = l.size(), nr = r.size();\n\t\tvector<T> res(nl + nr);\n\t\tfor (int i = 0, j = 0, k = 0; i < nl || j < nr; k++) {\n\t\t\tif (j == nr)\n\t\t\t\tres[k] = l[i++];\n\t\t\telse if (i == nl)\n\t\t\t\tres[k] = r[j++];\n\t\t\telse if (l[i] <= r[j])\n\t\t\t\tres[k] = l[i++];\n\t\t\telse\n\t\t\t\tres[k] = r[j++];\n\t\t}\n\t\treturn res;\n\t}\n\tint size(int n) {\n\t\tint res;\n\t\tfor (res = 1; res < n; res <<= 1);\n\t\treturn res;\n\t}\n\tT sub(int l, int r, int node, int lb, int ub, T val) {\n\t\tif (ub <= l || r <= lb) return 0;\n\t\tif (l <= lb && ub <= r) return upper_bound(data[node].begin(), data[node].end(), val) - data[node].begin();\n\t\treturn sub(l, r, node * 2, lb, (lb + ub) / 2, val) + sub(l, r, node * 2 + 1, (lb + ub) / 2, ub, val);\n\t}\npublic:\n\tSegmentTree(const vector<T>& data_) : n(size(data_.size())), data(n * 2) {\n\t\tfor (int i = 0; i < (int)data_.size(); i++)\n\t\t\tdata[i + n].push_back(data_[i]);\n\t\tfor (int i = n - 1; i >= 0; i--)\n\t\t\tdata[i] = merge(data[i * 2], data[i * 2 + 1]);\n\t}\n\tvoid Update(int p, T val) {\n\t\tp += n;\n\t\tdata[p][0] = val;\n\t\twhile (p >>= 1) data[p] = merge(data[p * 2], data[p * 2 + 1]);\n\t}\n\tvoid Add(int p, T val) {\n\t\tp += n;\n\t\tdata[p][0] += val;\n\t\twhile (p >>= 1) data[p] = merge(data[p * 2], data[p * 2 + 1]);\n\t}\n\tT Find(int l, int r, T val) {\n\t\treturn sub(l, r + 1, 1, 0, n, val);\n\t}\n};\n\nint main()\n{\n\tcin.sync_with_stdio(false);\n\tcin.tie(0);\n\tint N, Q;\n\tcin >> N >> Q;\n\tvector<int> W(N), base(N);\n\tfor (int i = 0; i < N; i++) {\n\t\tcin >> W[i];\n\t}\n\tHLDecomposition hl(N);\n\tfor (int i = 0, u, v; i < N - 1; i++) {\n\t\tcin >> u >> v; u--; v--;\n\t\thl.add(u, v);\n\t}\n\thl.build();\n\tfor (int i = 0; i < N; i++) {\n\t\tbase[i] = W[hl.inv[i]];\n\t}\n\tint v, w, l;\n\tSegmentTree<int> st(base);\n\twhile (Q--) {\n\t\tcin >> v >> w >> l; v--; w--;\n\t\tint lb = 0, ub = 1000000000;\n\t\twhile (lb + 1 < ub) {\n\t\t\tint c = lb + (ub - lb) / 2;\n\t\t\tint sum = 0;\n\t\t\thl.for_each(v, w, [&](int ll, int rr) {\n\t\t\t\tsum += st.Find(ll, rr, c);\n\t\t\t});\n\t\t\tif (sum >= l) {\n\t\t\t\tub = c;\n\t\t\t}\n\t\t\telse {\n\t\t\t\tlb = c;\n\t\t\t}\n\t\t}\n\t\tprintf(\"%d\\n\", ub);\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 2310, "memory_kb": 34536, "score_of_the_acc": -1.0524, "final_rank": 9 }, { "submission_id": "aoj_2270_10308712", "code_snippet": "// competitive-verifier: PROBLEM\n#include <cmath>\n#include <cstdint>\n#include <numeric>\n/// @brief めぐる式二分探索\ntemplate <class T, class F>\nstd::int64_t meguru_binary_search(T ok, T ng, F check) {\n while (std::abs(ok - ng) > 1) {\n T mid = std::midpoint(ok, ng);\n (check(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n#include <algorithm>\n#include <iterator>\n#include <vector>\n/// @brief 座標圧縮\ntemplate <class T>\nstruct coordinate_compression {\n coordinate_compression() = default;\n coordinate_compression(const std::vector<T> &_data) : data(_data) { build(); }\n const T &operator[](int i) const { return data[i]; }\n void add(T x) { data.emplace_back(x); }\n void build() {\n std::sort(data.begin(), data.end());\n data.erase(std::unique(data.begin(), data.end()), data.end());\n }\n bool exists(T x) const {\n auto it = std::lower_bound(data.begin(), data.end(), x);\n return it != data.end() && *it == x;\n }\n int get(T x) const {\n return std::distance(data.begin(), std::lower_bound(data.begin(), data.end(), x));\n }\n int lower_bound(T x) const {\n return std::distance(data.begin(), std::lower_bound(data.begin(), data.end(), x));\n }\n int upper_bound(T x) const {\n return std::distance(data.begin(), std::upper_bound(data.begin(), data.end(), x));\n }\n std::vector<int> compress(const std::vector<T> &v) const {\n int n = v.size();\n std::vector<int> res(n);\n for (int i = 0; i < n; ++i) res[i] = get(v[i]);\n return res;\n }\n int size() const { return data.size(); }\n private:\n std::vector<T> data;\n};\n/// @brief 座標圧縮\ntemplate <class T>\nstd::vector<int> compress(const std::vector<T> &v) {\n coordinate_compression cps(v);\n std::vector<int> res;\n res.reserve(std::size(v));\n for (auto &&x : v) res.emplace_back(cps.get(x));\n return res;\n}\n#include <cassert>\n#include <tuple>\nnamespace internal {\n/**\n * @brief 完備辞書\n *\n * @see https://ei1333.github.io/library/structure/wavelet/succinct-indexable-dictionary.hpp\n */\nstruct bit_vector {\n bit_vector() = default;\n bit_vector(unsigned int _length)\n : length(_length), blocks((_length + 31) >> 5), bit((_length + 31) >> 5),\n sum((_length + 31) >> 5) {}\n void set(unsigned int k) { bit[k >> 5] |= 1U << (k & 31); }\n void build() {\n sum[0] = 0U;\n for (unsigned int i = 1; i < blocks; ++i) {\n sum[i] = sum[i - 1] + __builtin_popcount(bit[i - 1]);\n }\n }\n bool operator[](unsigned int k) const { return bit[k >> 5] >> (k & 31) & 1; }\n unsigned int rank(unsigned int k) const {\n return sum[k >> 5] + __builtin_popcount(bit[k >> 5] & ((1U << (k & 31)) - 1));\n }\n unsigned int rank(bool val, unsigned int k) const { return val ? rank(k) : k - rank(k); }\n unsigned int select(unsigned int k) const {\n unsigned int sl = 0, sr = blocks + 1;\n while (sr - sl > 1) {\n unsigned int m = (sl + sr) >> 1;\n if (sum[m] < k) sl = m;\n else sr = m;\n }\n k -= sum[sl];\n unsigned int bl = 0, br = 32;\n while (br - bl > 1) {\n unsigned int m = (bl + br) >> 1;\n if (__builtin_popcount(bit[sl] & ((1U << m) - 1)) < k) bl = m;\n else br = m;\n }\n return (sl << 5) + bl;\n }\n private:\n unsigned int length, blocks;\n std::vector<unsigned int> bit, sum;\n};\n} // namespace internal\n/// @brief 重み付きウェーブレット行列\ntemplate <class T, class U = T, int L = 30>\nstruct wavelet_matrix_rectangle_sum {\n wavelet_matrix_rectangle_sum() = default;\n template <class Value>\n wavelet_matrix_rectangle_sum(const std::vector<T> &v, const std::vector<Value> &u)\n : length(v.size()) {\n assert(v.size() == u.size());\n std::vector<int> l(length), r(length), ord(length);\n std::iota(ord.begin(), ord.end(), 0);\n for (int level = L - 1; level >= 0; level--) {\n matrix[level] = internal::bit_vector(length + 1);\n int left = 0, right = 0;\n for (int i = 0; i < length; i++) {\n if ((v[ord[i]] >> level) & 1) {\n matrix[level].set(i);\n r[right++] = ord[i];\n } else {\n l[left++] = ord[i];\n }\n }\n mid[level] = left;\n matrix[level].build();\n ord.swap(l);\n for (int i = 0; i < right; i++) ord[left + i] = r[i];\n cs[level].resize(length + 1);\n cs[level][0] = U(0);\n for (int i = 0; i < length; i++) cs[level][i + 1] = cs[level][i] + u[ord[i]];\n }\n }\n U kth_smallest_sum(int l, int r, int k) const {\n assert(0 <= k && k <= r - l);\n T val = T();\n U res = U();\n for (int level = L - 1; level >= 0; --level) {\n int cnt = matrix[level].rank(false, r) - matrix[level].rank(false, l);\n bool f = cnt <= k;\n if (f) {\n val |= T(1) << level;\n res += cs[level][matrix[level].rank(false, r)] -\n cs[level][matrix[level].rank(false, l)];\n k -= cnt;\n }\n std::tie(l, r) = succ(f, l, r, level);\n }\n return res + val * k;\n }\n U kth_largest_sum(int l, int r, int k) const {\n return cs[L - 1][matrix[L - 1].rank(false, r)] + cs[L - 1][matrix[L - 1].rank(true, r)] -\n cs[L - 1][matrix[L - 1].rank(false, l)] - cs[L - 1][matrix[L - 1].rank(true, l)] -\n kth_smallest_sum(l, r, r - l - k);\n }\n U range_sum(int r, T x) const { return range_sum(0, r, x); }\n U range_sum(int l, int r, T x) const {\n for (int level = L - 1; level >= 0; level--)\n std::tie(l, r) = succ((x >> level) & 1, l, r, level);\n return cs[0][matrix[0].rank(false, r)] - cs[0][matrix[0].rank(false, l)];\n }\n U rect_sum(int l, int r, T upper) const {\n U res = 0;\n for (int level = L - 1; level >= 0; level--) {\n bool f = (upper >> level) & 1;\n if (f)\n res += cs[level][matrix[level].rank(false, r)] -\n cs[level][matrix[level].rank(false, l)];\n std::tie(l, r) = succ(f, l, r, level);\n }\n return res;\n }\n U rect_sum(int l, int r, T lower, T upper) const {\n return rect_sum(l, r, upper) - rect_sum(l, r, lower);\n }\n private:\n int length;\n internal::bit_vector matrix[L];\n int mid[L];\n std::vector<U> cs[L];\n std::pair<int, int> succ(bool f, int l, int r, int level) const {\n return {matrix[level].rank(f, l) + mid[level] * f,\n matrix[level].rank(f, r) + mid[level] * f};\n }\n};\n#include <iostream>\n/**\n * @brief 重み付きグラフ\n *\n * @tparam T 辺の重みの型\n */\ntemplate <class T>\nstruct Graph {\n private:\n struct _edge {\n constexpr _edge() : _from(), _to(), _weight() {}\n constexpr _edge(int from, int to, T weight) : _from(from), _to(to), _weight(weight) {}\n constexpr bool operator<(const _edge &rhs) const { return weight() < rhs.weight(); }\n constexpr bool operator>(const _edge &rhs) const { return rhs < *this; }\n constexpr int from() const { return _from; }\n constexpr int to() const { return _to; }\n constexpr T weight() const { return _weight; }\n private:\n int _from, _to;\n T _weight;\n };\n public:\n using edge_type = typename Graph<T>::_edge;\n Graph() : _size(), edges() {}\n Graph(int v) : _size(v), edges(v) {}\n const auto &operator[](int i) const { return edges[i]; }\n auto &operator[](int i) { return edges[i]; }\n const auto begin() const { return edges.begin(); }\n auto begin() { return edges.begin(); }\n const auto end() const { return edges.end(); }\n auto end() { return edges.end(); }\n constexpr int size() const { return _size; }\n void add_edge(const edge_type &e) { edges[e.from()].emplace_back(e); }\n void add_edge(int from, int to, T weight = T(1)) { edges[from].emplace_back(from, to, weight); }\n void add_edges(int from, int to, T weight = T(1)) {\n edges[from].emplace_back(from, to, weight);\n edges[to].emplace_back(to, from, weight);\n }\n void input_edge(int m, int base = 1) {\n for (int i = 0; i < m; ++i) {\n int from, to;\n T weight;\n std::cin >> from >> to >> weight;\n add_edge(from - base, to - base, weight);\n }\n }\n void input_edges(int m, int base = 1) {\n for (int i = 0; i < m; ++i) {\n int from, to;\n T weight;\n std::cin >> from >> to >> weight;\n add_edges(from - base, to - base, weight);\n }\n }\n private:\n int _size;\n std::vector<std::vector<edge_type>> edges;\n};\ntemplate <>\nstruct Graph<void> {\n private:\n struct _edge {\n constexpr _edge() : _from(), _to() {}\n constexpr _edge(int from, int to) : _from(from), _to(to) {}\n constexpr int from() const { return _from; }\n constexpr int to() const { return _to; }\n constexpr int weight() const { return 1; }\n constexpr bool operator<(const _edge &rhs) const { return weight() < rhs.weight(); }\n constexpr bool operator>(const _edge &rhs) const { return rhs < *this; }\n private:\n int _from, _to;\n };\n public:\n using edge_type = typename Graph<void>::_edge;\n Graph() : _size(), edges() {}\n Graph(int v) : _size(v), edges(v) {}\n const auto &operator[](int i) const { return edges[i]; }\n auto &operator[](int i) { return edges[i]; }\n const auto begin() const { return edges.begin(); }\n auto begin() { return edges.begin(); }\n const auto end() const { return edges.end(); }\n auto end() { return edges.end(); }\n constexpr int size() const { return _size; }\n void add_edge(const edge_type &e) { edges[e.from()].emplace_back(e); }\n void add_edge(int from, int to) { edges[from].emplace_back(from, to); }\n void add_edges(int from, int to) {\n edges[from].emplace_back(from, to);\n edges[to].emplace_back(to, from);\n }\n void input_edge(int m, int base = 1) {\n for (int i = 0; i < m; ++i) {\n int from, to;\n std::cin >> from >> to;\n add_edge(from - base, to - base);\n }\n }\n void input_edges(int m, int base = 1) {\n for (int i = 0; i < m; ++i) {\n int from, to;\n std::cin >> from >> to;\n add_edges(from - base, to - base);\n }\n }\n private:\n int _size;\n std::vector<std::vector<edge_type>> edges;\n};\n#ifdef ATCODER\n#pragma GCC target(\"sse4.2,avx512f,avx512dq,avx512ifma,avx512cd,avx512bw,avx512vl,bmi2\")\n#endif\n#pragma GCC optimize(\"Ofast,fast-math,unroll-all-loops\")\n#include <bits/stdc++.h>\n#ifndef ATCODER\n#pragma GCC target(\"sse4.2,avx2,bmi2\")\n#endif\ntemplate <class T, class U>\nconstexpr bool chmax(T &a, const U &b) {\n return a < (T)b ? a = (T)b, true : false;\n}\ntemplate <class T, class U>\nconstexpr bool chmin(T &a, const U &b) {\n return (T)b < a ? a = (T)b, true : false;\n}\nconstexpr std::int64_t INF = 1000000000000000003;\nconstexpr int Inf = 1000000003;\nconstexpr double EPS = 1e-7;\nconstexpr double PI = 3.14159265358979323846;\n#define FOR(i, m, n) for (int i = (m); i < int(n); ++i)\n#define FORR(i, m, n) for (int i = (m)-1; i >= int(n); --i)\n#define FORL(i, m, n) for (int64_t i = (m); i < int64_t(n); ++i)\n#define rep(i, n) FOR (i, 0, n)\n#define repn(i, n) FOR (i, 1, n + 1)\n#define repr(i, n) FORR (i, n, 0)\n#define repnr(i, n) FORR (i, n + 1, 1)\n#define all(s) (s).begin(), (s).end()\nstruct Sonic {\n Sonic() {\n std::ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout << std::fixed << std::setprecision(20);\n }\n constexpr void operator()() const {}\n} sonic;\nusing namespace std;\nusing ll = std::int64_t;\nusing ld = long double;\ntemplate <class T, class U>\nstd::istream &operator>>(std::istream &is, std::pair<T, U> &p) {\n return is >> p.first >> p.second;\n}\ntemplate <class T>\nstd::istream &operator>>(std::istream &is, std::vector<T> &v) {\n for (T &i : v) is >> i;\n return is;\n}\ntemplate <class T, class U>\nstd::ostream &operator<<(std::ostream &os, const std::pair<T, U> &p) {\n return os << '(' << p.first << ',' << p.second << ')';\n}\ntemplate <class T>\nstd::ostream &operator<<(std::ostream &os, const std::vector<T> &v) {\n for (auto it = v.begin(); it != v.end(); ++it) os << (it == v.begin() ? \"\" : \" \") << *it;\n return os;\n}\ntemplate <class Head, class... Tail>\nvoid co(Head &&head, Tail &&...tail) {\n if constexpr (sizeof...(tail) == 0) std::cout << head << '\\n';\n else std::cout << head << ' ', co(std::forward<Tail>(tail)...);\n}\ntemplate <class Head, class... Tail>\nvoid ce(Head &&head, Tail &&...tail) {\n if constexpr (sizeof...(tail) == 0) std::cerr << head << '\\n';\n else std::cerr << head << ' ', ce(std::forward<Tail>(tail)...);\n}\nvoid Yes(bool is_correct = true) { std::cout << (is_correct ? \"Yes\\n\" : \"No\\n\"); }\nvoid No(bool is_not_correct = true) { Yes(!is_not_correct); }\nvoid YES(bool is_correct = true) { std::cout << (is_correct ? \"YES\\n\" : \"NO\\n\"); }\nvoid NO(bool is_not_correct = true) { YES(!is_not_correct); }\nvoid Takahashi(bool is_correct = true) { std::cout << (is_correct ? \"Takahashi\" : \"Aoki\") << '\\n'; }\nvoid Aoki(bool is_not_correct = true) { Takahashi(!is_not_correct); }\n#include <bit>\n/// @brief スパーステーブル\ntemplate <class M>\nstruct sparse_table {\n private:\n using T = typename M::value_type;\n public:\n sparse_table() = default;\n sparse_table(const std::vector<T> &v) : _size(v.size()), data() {\n int b = std::max(1, std::countr_zero(std::bit_ceil<unsigned>(_size)));\n data.emplace_back(v);\n for (int i = 1; i < b; ++i) data.emplace_back(_size + 1 - (1 << i));\n for (int i = 1; i < b; ++i) {\n for (int j = 0; j + (1 << i) <= _size; ++j) {\n data[i][j] = M::op(data[i - 1][j], data[i - 1][j + (1 << (i - 1))]);\n }\n }\n }\n T prod(int l, int r) const {\n assert(0 <= l && l <= r && r <= _size);\n if (l == r) return M::id();\n if (l + 1 == r) return data[0][l];\n int b = 31 - std::countl_zero<unsigned>(r - l - 1);\n return M::op(data[b][l], data[b][r - (1 << b)]);\n }\n private:\n int _size;\n std::vector<std::vector<T>> data;\n};\nnamespace internal {\ntemplate <class T, int N>\nstruct fixed_stack {\n constexpr fixed_stack() : _size(), _data() {}\n constexpr T top() const { return _data[_size - 1]; }\n constexpr bool empty() const { return _size == 0; }\n constexpr int size() const { return _size; }\n constexpr void emplace(const T &e) { _data[_size++] = e; }\n constexpr void emplace(T &&e) { _data[_size++] = e; }\n constexpr void pop() { --_size; }\n constexpr void clear() { _size = 0; }\n private:\n int _size;\n std::array<T, N> _data;\n};\n} // namespace internal\n/**\n * @brief 線形 Sparse Table\n *\n * @tparam M\n */\ntemplate <class M>\nstruct linear_sparse_table {\n private:\n using T = M::value_type;\n static constexpr int W = 64;\n public:\n linear_sparse_table() = default;\n linear_sparse_table(const std::vector<T> &v) : _size(v.size()), data(v) {\n int n = v.size();\n int b = n / W;\n internal::fixed_stack<int, W + 1> st;\n std::vector<T> u(b);\n word_data.resize(b + (n > b * W));\n for (int i = 0; i < b; ++i) {\n T m = M::id();\n std::uint64_t bit = 0;\n std::vector<std::uint64_t> bits(W);\n for (int j = 0; j < W; ++j) {\n m = M::op(m, v[i * W + j]);\n while (!st.empty() && M::op(v[i * W + st.top()], v[i * W + j]) == v[i * W + j]) {\n bit ^= std::uint64_t(1) << st.top();\n st.pop();\n }\n bits[j] = bit;\n bit |= std::uint64_t(1) << j;\n st.emplace(j);\n }\n u[i] = m;\n word_data[i] = bits;\n st.clear();\n }\n if (n > b * W) {\n std::uint64_t bit = 0;\n std::vector<std::uint64_t> bits(n - b * W);\n for (int j = 0; j < n - b * W; ++j) {\n while (!st.empty() && M::op(v[b * W + st.top()], v[b * W + j]) == v[b * W + j]) {\n bit ^= std::uint64_t(1) << st.top();\n st.pop();\n }\n bits[j] = bit;\n bit |= std::uint64_t(1) << j;\n st.emplace(j);\n }\n word_data[b] = bits;\n }\n block_table = sparse_table<M>(u);\n }\n const T &operator[](int k) const { return data[k]; }\n T prod(int l, int r) const {\n assert(0 <= l && l < r && r <= _size);\n int lb = (l + W - 1) / W, rb = r / W;\n if (lb > rb) return word_prod(l, r);\n T res = (lb == rb ? M::id() : block_table.prod(lb, rb));\n if (l < lb * W) res = M::op(res, word_prod(l, lb * W));\n if (rb * W < r) res = M::op(res, word_prod(rb * W, r));\n return res;\n }\n private:\n int _size;\n std::vector<T> data;\n sparse_table<M> block_table;\n std::vector<std::vector<std::uint64_t>> word_data;\n T word_prod(int l, int r) const {\n if (l == r) return M::id();\n int b = l / W;\n int lw = l - b * W, rw = r - b * W;\n if ((word_data[b][rw - 1] >> lw) == 0ul) return data[r - 1];\n return data[l + std::countr_zero(word_data[b][rw - 1] >> lw)];\n }\n};\nstruct linear_lca {\n private:\n struct S {\n int depth, index;\n bool operator<(const S &rhs) const { return depth < rhs.depth; }\n bool operator==(const S &rhs) const = default;\n };\n struct M {\n using value_type = S;\n static constexpr S id() { return S{std::numeric_limits<int>::max(), -1}; }\n static constexpr S op(const S &lhs, const S &rhs) { return std::min(lhs, rhs); }\n };\n public:\n template <class T>\n linear_lca(const Graph<T> &g, int r = 0) : ord(g.size(), -1), lst() {\n std::vector<S> v;\n std::stack<std::pair<int, int>> st;\n st.emplace(r, 0);\n while (!st.empty()) {\n auto [x, d] = st.top();\n st.pop();\n if (x < 0) {\n v.emplace_back(d, ~x);\n continue;\n }\n ord[x] = v.size();\n v.emplace_back(d, x);\n for (auto e : g[x]) {\n if (ord[e.to()] != -1) continue;\n st.emplace(~x, d);\n st.emplace(e.to(), d + 1);\n }\n }\n lst = linear_sparse_table<M>(v);\n }\n int operator()(int u, int v) const { return lca(u, v); }\n int lca(int u, int v) const {\n auto [l, r] = std::minmax(ord[u], ord[v]);\n return lst.prod(l, r + 1).index;\n }\n private:\n std::vector<int> ord;\n linear_sparse_table<M> lst;\n};\nint main(void) {\n int n, q;\n cin >> n >> q;\n vector<int> a(n);\n cin >> a;\n coordinate_compression cps(a);\n a = cps.compress(a);\n Graph<void> g(n);\n g.input_edges(n - 1);\n vector<int> in(n), out(n), ord(n * 2), w(n * 2);\n int c = 0;\n auto dfs = [&](auto self, int x, int p) -> void {\n in[x] = c;\n w[c] = 1;\n ord[c++] = a[x];\n for (auto e : g[x]) {\n if (e.to() == p)\n continue;\n self(self, e.to(), x);\n }\n w[c] = -1;\n ord[c++] = a[x];\n out[x] = c;\n };\n dfs(dfs, 0, -1);\n linear_lca lca(g);\n wavelet_matrix_rectangle_sum<int, int, 18> wm(ord, w);\n while (q--) {\n int u, v, k;\n cin >> u >> v >> k;\n --u, --v;\n auto f = [&](int y) {\n return wm.rect_sum(0, in[u] + 1, y) + wm.rect_sum(0, in[v] + 1, y) -\n wm.rect_sum(0, in[lca(u, v)] + 1, y) - wm.rect_sum(0, in[lca(u, v)], y) <\n k;\n };\n auto ans = meguru_binary_search(0, cps.size() + 1, f);\n co(cps[ans]);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 720, "memory_kb": 39676, "score_of_the_acc": -0.5227, "final_rank": 4 }, { "submission_id": "aoj_2270_10308710", "code_snippet": "// competitive-verifier: PROBLEM\n#include <cmath>\n#include <cstdint>\n#include <numeric>\n/// @brief めぐる式二分探索\ntemplate <class T, class F>\nstd::int64_t meguru_binary_search(T ok, T ng, F check) {\n while (std::abs(ok - ng) > 1) {\n T mid = std::midpoint(ok, ng);\n (check(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n#include <algorithm>\n#include <iterator>\n#include <vector>\n/// @brief 座標圧縮\ntemplate <class T>\nstruct coordinate_compression {\n coordinate_compression() = default;\n coordinate_compression(const std::vector<T> &_data) : data(_data) { build(); }\n const T &operator[](int i) const { return data[i]; }\n void add(T x) { data.emplace_back(x); }\n void build() {\n std::sort(data.begin(), data.end());\n data.erase(std::unique(data.begin(), data.end()), data.end());\n }\n bool exists(T x) const {\n auto it = std::lower_bound(data.begin(), data.end(), x);\n return it != data.end() && *it == x;\n }\n int get(T x) const {\n return std::distance(data.begin(), std::lower_bound(data.begin(), data.end(), x));\n }\n int lower_bound(T x) const {\n return std::distance(data.begin(), std::lower_bound(data.begin(), data.end(), x));\n }\n int upper_bound(T x) const {\n return std::distance(data.begin(), std::upper_bound(data.begin(), data.end(), x));\n }\n std::vector<int> compress(const std::vector<T> &v) const {\n int n = v.size();\n std::vector<int> res(n);\n for (int i = 0; i < n; ++i) res[i] = get(v[i]);\n return res;\n }\n int size() const { return data.size(); }\n private:\n std::vector<T> data;\n};\n/// @brief 座標圧縮\ntemplate <class T>\nstd::vector<int> compress(const std::vector<T> &v) {\n coordinate_compression cps(v);\n std::vector<int> res;\n res.reserve(std::size(v));\n for (auto &&x : v) res.emplace_back(cps.get(x));\n return res;\n}\n#include <cassert>\n#include <tuple>\nnamespace internal {\n/**\n * @brief 完備辞書\n *\n * @see https://ei1333.github.io/library/structure/wavelet/succinct-indexable-dictionary.hpp\n */\nstruct bit_vector {\n bit_vector() = default;\n bit_vector(unsigned int _length)\n : length(_length), blocks((_length + 31) >> 5), bit((_length + 31) >> 5),\n sum((_length + 31) >> 5) {}\n void set(unsigned int k) { bit[k >> 5] |= 1U << (k & 31); }\n void build() {\n sum[0] = 0U;\n for (unsigned int i = 1; i < blocks; ++i) {\n sum[i] = sum[i - 1] + __builtin_popcount(bit[i - 1]);\n }\n }\n bool operator[](unsigned int k) const { return bit[k >> 5] >> (k & 31) & 1; }\n unsigned int rank(unsigned int k) const {\n return sum[k >> 5] + __builtin_popcount(bit[k >> 5] & ((1U << (k & 31)) - 1));\n }\n unsigned int rank(bool val, unsigned int k) const { return val ? rank(k) : k - rank(k); }\n unsigned int select(unsigned int k) const {\n unsigned int sl = 0, sr = blocks + 1;\n while (sr - sl > 1) {\n unsigned int m = (sl + sr) >> 1;\n if (sum[m] < k) sl = m;\n else sr = m;\n }\n k -= sum[sl];\n unsigned int bl = 0, br = 32;\n while (br - bl > 1) {\n unsigned int m = (bl + br) >> 1;\n if (__builtin_popcount(bit[sl] & ((1U << m) - 1)) < k) bl = m;\n else br = m;\n }\n return (sl << 5) + bl;\n }\n private:\n unsigned int length, blocks;\n std::vector<unsigned int> bit, sum;\n};\n} // namespace internal\n/// @brief 重み付きウェーブレット行列\ntemplate <class T, class U = T, int L = 30>\nstruct wavelet_matrix_rectangle_sum {\n wavelet_matrix_rectangle_sum() = default;\n template <class Value>\n wavelet_matrix_rectangle_sum(const std::vector<T> &v, const std::vector<Value> &u)\n : length(v.size()) {\n assert(v.size() == u.size());\n std::vector<int> l(length), r(length), ord(length);\n std::iota(ord.begin(), ord.end(), 0);\n for (int level = L - 1; level >= 0; level--) {\n matrix[level] = internal::bit_vector(length + 1);\n int left = 0, right = 0;\n for (int i = 0; i < length; i++) {\n if ((v[ord[i]] >> level) & 1) {\n matrix[level].set(i);\n r[right++] = ord[i];\n } else {\n l[left++] = ord[i];\n }\n }\n mid[level] = left;\n matrix[level].build();\n ord.swap(l);\n for (int i = 0; i < right; i++) ord[left + i] = r[i];\n cs[level].resize(length + 1);\n cs[level][0] = U(0);\n for (int i = 0; i < length; i++) cs[level][i + 1] = cs[level][i] + u[ord[i]];\n }\n }\n U kth_smallest_sum(int l, int r, int k) const {\n assert(0 <= k && k <= r - l);\n T val = T();\n U res = U();\n for (int level = L - 1; level >= 0; --level) {\n int cnt = matrix[level].rank(false, r) - matrix[level].rank(false, l);\n bool f = cnt <= k;\n if (f) {\n val |= T(1) << level;\n res += cs[level][matrix[level].rank(false, r)] -\n cs[level][matrix[level].rank(false, l)];\n k -= cnt;\n }\n std::tie(l, r) = succ(f, l, r, level);\n }\n return res + val * k;\n }\n U kth_largest_sum(int l, int r, int k) const {\n return cs[L - 1][matrix[L - 1].rank(false, r)] + cs[L - 1][matrix[L - 1].rank(true, r)] -\n cs[L - 1][matrix[L - 1].rank(false, l)] - cs[L - 1][matrix[L - 1].rank(true, l)] -\n kth_smallest_sum(l, r, r - l - k);\n }\n U range_sum(int r, T x) const { return range_sum(0, r, x); }\n U range_sum(int l, int r, T x) const {\n for (int level = L - 1; level >= 0; level--)\n std::tie(l, r) = succ((x >> level) & 1, l, r, level);\n return cs[0][matrix[0].rank(false, r)] - cs[0][matrix[0].rank(false, l)];\n }\n U rect_sum(int l, int r, T upper) const {\n U res = 0;\n for (int level = L - 1; level >= 0; level--) {\n bool f = (upper >> level) & 1;\n if (f)\n res += cs[level][matrix[level].rank(false, r)] -\n cs[level][matrix[level].rank(false, l)];\n std::tie(l, r) = succ(f, l, r, level);\n }\n return res;\n }\n U rect_sum(int l, int r, T lower, T upper) const {\n return rect_sum(l, r, upper) - rect_sum(l, r, lower);\n }\n private:\n int length;\n internal::bit_vector matrix[L];\n int mid[L];\n std::vector<U> cs[L];\n std::pair<int, int> succ(bool f, int l, int r, int level) const {\n return {matrix[level].rank(f, l) + mid[level] * f,\n matrix[level].rank(f, r) + mid[level] * f};\n }\n};\n#include <iostream>\n/**\n * @brief 重み付きグラフ\n *\n * @tparam T 辺の重みの型\n */\ntemplate <class T>\nstruct Graph {\n private:\n struct _edge {\n constexpr _edge() : _from(), _to(), _weight() {}\n constexpr _edge(int from, int to, T weight) : _from(from), _to(to), _weight(weight) {}\n constexpr bool operator<(const _edge &rhs) const { return weight() < rhs.weight(); }\n constexpr bool operator>(const _edge &rhs) const { return rhs < *this; }\n constexpr int from() const { return _from; }\n constexpr int to() const { return _to; }\n constexpr T weight() const { return _weight; }\n private:\n int _from, _to;\n T _weight;\n };\n public:\n using edge_type = typename Graph<T>::_edge;\n Graph() : _size(), edges() {}\n Graph(int v) : _size(v), edges(v) {}\n const auto &operator[](int i) const { return edges[i]; }\n auto &operator[](int i) { return edges[i]; }\n const auto begin() const { return edges.begin(); }\n auto begin() { return edges.begin(); }\n const auto end() const { return edges.end(); }\n auto end() { return edges.end(); }\n constexpr int size() const { return _size; }\n void add_edge(const edge_type &e) { edges[e.from()].emplace_back(e); }\n void add_edge(int from, int to, T weight = T(1)) { edges[from].emplace_back(from, to, weight); }\n void add_edges(int from, int to, T weight = T(1)) {\n edges[from].emplace_back(from, to, weight);\n edges[to].emplace_back(to, from, weight);\n }\n void input_edge(int m, int base = 1) {\n for (int i = 0; i < m; ++i) {\n int from, to;\n T weight;\n std::cin >> from >> to >> weight;\n add_edge(from - base, to - base, weight);\n }\n }\n void input_edges(int m, int base = 1) {\n for (int i = 0; i < m; ++i) {\n int from, to;\n T weight;\n std::cin >> from >> to >> weight;\n add_edges(from - base, to - base, weight);\n }\n }\n private:\n int _size;\n std::vector<std::vector<edge_type>> edges;\n};\ntemplate <>\nstruct Graph<void> {\n private:\n struct _edge {\n constexpr _edge() : _from(), _to() {}\n constexpr _edge(int from, int to) : _from(from), _to(to) {}\n constexpr int from() const { return _from; }\n constexpr int to() const { return _to; }\n constexpr int weight() const { return 1; }\n constexpr bool operator<(const _edge &rhs) const { return weight() < rhs.weight(); }\n constexpr bool operator>(const _edge &rhs) const { return rhs < *this; }\n private:\n int _from, _to;\n };\n public:\n using edge_type = typename Graph<void>::_edge;\n Graph() : _size(), edges() {}\n Graph(int v) : _size(v), edges(v) {}\n const auto &operator[](int i) const { return edges[i]; }\n auto &operator[](int i) { return edges[i]; }\n const auto begin() const { return edges.begin(); }\n auto begin() { return edges.begin(); }\n const auto end() const { return edges.end(); }\n auto end() { return edges.end(); }\n constexpr int size() const { return _size; }\n void add_edge(const edge_type &e) { edges[e.from()].emplace_back(e); }\n void add_edge(int from, int to) { edges[from].emplace_back(from, to); }\n void add_edges(int from, int to) {\n edges[from].emplace_back(from, to);\n edges[to].emplace_back(to, from);\n }\n void input_edge(int m, int base = 1) {\n for (int i = 0; i < m; ++i) {\n int from, to;\n std::cin >> from >> to;\n add_edge(from - base, to - base);\n }\n }\n void input_edges(int m, int base = 1) {\n for (int i = 0; i < m; ++i) {\n int from, to;\n std::cin >> from >> to;\n add_edges(from - base, to - base);\n }\n }\n private:\n int _size;\n std::vector<std::vector<edge_type>> edges;\n};\n#ifdef ATCODER\n#pragma GCC target(\"sse4.2,avx512f,avx512dq,avx512ifma,avx512cd,avx512bw,avx512vl,bmi2\")\n#endif\n#pragma GCC optimize(\"Ofast,fast-math,unroll-all-loops\")\n#include <bits/stdc++.h>\n#ifndef ATCODER\n#pragma GCC target(\"sse4.2,avx2,bmi2\")\n#endif\ntemplate <class T, class U>\nconstexpr bool chmax(T &a, const U &b) {\n return a < (T)b ? a = (T)b, true : false;\n}\ntemplate <class T, class U>\nconstexpr bool chmin(T &a, const U &b) {\n return (T)b < a ? a = (T)b, true : false;\n}\nconstexpr std::int64_t INF = 1000000000000000003;\nconstexpr int Inf = 1000000003;\nconstexpr double EPS = 1e-7;\nconstexpr double PI = 3.14159265358979323846;\n#define FOR(i, m, n) for (int i = (m); i < int(n); ++i)\n#define FORR(i, m, n) for (int i = (m)-1; i >= int(n); --i)\n#define FORL(i, m, n) for (int64_t i = (m); i < int64_t(n); ++i)\n#define rep(i, n) FOR (i, 0, n)\n#define repn(i, n) FOR (i, 1, n + 1)\n#define repr(i, n) FORR (i, n, 0)\n#define repnr(i, n) FORR (i, n + 1, 1)\n#define all(s) (s).begin(), (s).end()\nstruct Sonic {\n Sonic() {\n std::ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout << std::fixed << std::setprecision(20);\n }\n constexpr void operator()() const {}\n} sonic;\nusing namespace std;\nusing ll = std::int64_t;\nusing ld = long double;\ntemplate <class T, class U>\nstd::istream &operator>>(std::istream &is, std::pair<T, U> &p) {\n return is >> p.first >> p.second;\n}\ntemplate <class T>\nstd::istream &operator>>(std::istream &is, std::vector<T> &v) {\n for (T &i : v) is >> i;\n return is;\n}\ntemplate <class T, class U>\nstd::ostream &operator<<(std::ostream &os, const std::pair<T, U> &p) {\n return os << '(' << p.first << ',' << p.second << ')';\n}\ntemplate <class T>\nstd::ostream &operator<<(std::ostream &os, const std::vector<T> &v) {\n for (auto it = v.begin(); it != v.end(); ++it) os << (it == v.begin() ? \"\" : \" \") << *it;\n return os;\n}\ntemplate <class Head, class... Tail>\nvoid co(Head &&head, Tail &&...tail) {\n if constexpr (sizeof...(tail) == 0) std::cout << head << '\\n';\n else std::cout << head << ' ', co(std::forward<Tail>(tail)...);\n}\ntemplate <class Head, class... Tail>\nvoid ce(Head &&head, Tail &&...tail) {\n if constexpr (sizeof...(tail) == 0) std::cerr << head << '\\n';\n else std::cerr << head << ' ', ce(std::forward<Tail>(tail)...);\n}\nvoid Yes(bool is_correct = true) { std::cout << (is_correct ? \"Yes\\n\" : \"No\\n\"); }\nvoid No(bool is_not_correct = true) { Yes(!is_not_correct); }\nvoid YES(bool is_correct = true) { std::cout << (is_correct ? \"YES\\n\" : \"NO\\n\"); }\nvoid NO(bool is_not_correct = true) { YES(!is_not_correct); }\nvoid Takahashi(bool is_correct = true) { std::cout << (is_correct ? \"Takahashi\" : \"Aoki\") << '\\n'; }\nvoid Aoki(bool is_not_correct = true) { Takahashi(!is_not_correct); }\n#include <bit>\n/// @brief スパーステーブル\ntemplate <class M>\nstruct sparse_table {\n private:\n using T = typename M::value_type;\n public:\n sparse_table() = default;\n sparse_table(const std::vector<T> &v) : _size(v.size()), data() {\n int b = std::max(1, std::countr_zero(std::bit_ceil<unsigned>(_size)));\n data.emplace_back(v);\n for (int i = 1; i < b; ++i) data.emplace_back(_size + 1 - (1 << i));\n for (int i = 1; i < b; ++i) {\n for (int j = 0; j + (1 << i) <= _size; ++j) {\n data[i][j] = M::op(data[i - 1][j], data[i - 1][j + (1 << (i - 1))]);\n }\n }\n }\n T prod(int l, int r) const {\n assert(0 <= l && l <= r && r <= _size);\n if (l == r) return M::id();\n if (l + 1 == r) return data[0][l];\n int b = 31 - std::countl_zero<unsigned>(r - l - 1);\n return M::op(data[b][l], data[b][r - (1 << b)]);\n }\n private:\n int _size;\n std::vector<std::vector<T>> data;\n};\nnamespace internal {\ntemplate <class T, int N>\nstruct fixed_stack {\n constexpr fixed_stack() : _size(), _data() {}\n constexpr T top() const { return _data[_size - 1]; }\n constexpr bool empty() const { return _size == 0; }\n constexpr int size() const { return _size; }\n constexpr void emplace(const T &e) { _data[_size++] = e; }\n constexpr void emplace(T &&e) { _data[_size++] = e; }\n constexpr void pop() { --_size; }\n constexpr void clear() { _size = 0; }\n private:\n int _size;\n std::array<T, N> _data;\n};\n} // namespace internal\n/**\n * @brief 線形 Sparse Table\n *\n * @tparam M\n */\ntemplate <class M>\nstruct linear_sparse_table {\n private:\n using T = M::value_type;\n static constexpr int W = 64;\n public:\n linear_sparse_table() = default;\n linear_sparse_table(const std::vector<T> &v) : _size(v.size()), data(v) {\n int n = v.size();\n int b = n / W;\n internal::fixed_stack<int, W + 1> st;\n std::vector<T> u(b);\n word_data.resize(b + (n > b * W));\n for (int i = 0; i < b; ++i) {\n T m = M::id();\n std::uint64_t bit = 0;\n std::vector<std::uint64_t> bits(W);\n for (int j = 0; j < W; ++j) {\n m = M::op(m, v[i * W + j]);\n while (!st.empty() && M::op(v[i * W + st.top()], v[i * W + j]) == v[i * W + j]) {\n bit ^= std::uint64_t(1) << st.top();\n st.pop();\n }\n bits[j] = bit;\n bit |= std::uint64_t(1) << j;\n st.emplace(j);\n }\n u[i] = m;\n word_data[i] = bits;\n st.clear();\n }\n if (n > b * W) {\n std::uint64_t bit = 0;\n std::vector<std::uint64_t> bits(n - b * W);\n for (int j = 0; j < n - b * W; ++j) {\n while (!st.empty() && M::op(v[b * W + st.top()], v[b * W + j]) == v[b * W + j]) {\n bit ^= std::uint64_t(1) << st.top();\n st.pop();\n }\n bits[j] = bit;\n bit |= std::uint64_t(1) << j;\n st.emplace(j);\n }\n word_data[b] = bits;\n }\n block_table = sparse_table<M>(u);\n }\n const T &operator[](int k) const { return data[k]; }\n T prod(int l, int r) const {\n assert(0 <= l && l < r && r <= _size);\n int lb = (l + W - 1) / W, rb = r / W;\n if (lb > rb) return word_prod(l, r);\n T res = (lb == rb ? M::id() : block_table.prod(lb, rb));\n if (l < lb * W) res = M::op(res, word_prod(l, lb * W));\n if (rb * W < r) res = M::op(res, word_prod(rb * W, r));\n return res;\n }\n private:\n int _size;\n std::vector<T> data;\n sparse_table<M> block_table;\n std::vector<std::vector<std::uint64_t>> word_data;\n T word_prod(int l, int r) const {\n if (l == r) return M::id();\n int b = l / W;\n int lw = l - b * W, rw = r - b * W;\n if ((word_data[b][rw - 1] >> lw) == 0ul) return data[r - 1];\n return data[l + std::countr_zero(word_data[b][rw - 1] >> lw)];\n }\n};\nstruct linear_lca {\n private:\n struct S {\n int depth, index;\n bool operator<(const S &rhs) const { return depth < rhs.depth; }\n bool operator==(const S &rhs) const = default;\n };\n struct M {\n using value_type = S;\n static constexpr S id() { return S{std::numeric_limits<int>::max(), -1}; }\n static constexpr S op(const S &lhs, const S &rhs) { return std::min(lhs, rhs); }\n };\n public:\n template <class T>\n linear_lca(const Graph<T> &g, int r = 0) : ord(g.size(), -1), lst() {\n std::vector<S> v;\n std::stack<std::pair<int, int>> st;\n st.emplace(r, 0);\n while (!st.empty()) {\n auto [x, d] = st.top();\n st.pop();\n if (x < 0) {\n v.emplace_back(d, ~x);\n continue;\n }\n ord[x] = v.size();\n v.emplace_back(d, x);\n for (auto e : g[x]) {\n if (ord[e.to()] != -1) continue;\n st.emplace(~x, d);\n st.emplace(e.to(), d + 1);\n }\n }\n lst = linear_sparse_table<M>(v);\n }\n int operator()(int u, int v) const { return lca(u, v); }\n int lca(int u, int v) const {\n auto [l, r] = std::minmax(ord[u], ord[v]);\n return lst.prod(l, r + 1).index;\n }\n private:\n std::vector<int> ord;\n linear_sparse_table<M> lst;\n};\nint main(void) {\n int n, q;\n cin >> n >> q;\n vector<int> a(n);\n cin >> a;\n coordinate_compression cps(a);\n a = cps.compress(a);\n Graph<void> g(n);\n g.input_edges(n - 1);\n vector<int> in(n), out(n), ord(n * 2), w(n * 2);\n int c = 0;\n auto dfs = [&](auto self, int x, int p) -> void {\n in[x] = c;\n w[c] = 1;\n ord[c++] = a[x];\n for (auto e : g[x]) {\n if (e.to() == p)\n continue;\n self(self, e.to(), x);\n }\n w[c] = -1;\n ord[c++] = a[x];\n out[x] = c;\n };\n dfs(dfs, 0, -1);\n linear_lca lca(g);\n wavelet_matrix_rectangle_sum wm(ord, w);\n while (q--) {\n int u, v, k;\n cin >> u >> v >> k;\n --u, --v;\n auto f = [&](int y) {\n return wm.rect_sum(0, in[u] + 1, y) + wm.rect_sum(0, in[v] + 1, y) -\n wm.rect_sum(0, in[lca(u, v)] + 1, y) - wm.rect_sum(0, in[lca(u, v)], y) <\n k;\n };\n auto ans = meguru_binary_search(0, cps.size() + 1, f);\n co(cps[ans]);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1250, "memory_kb": 49536, "score_of_the_acc": -0.7853, "final_rank": 5 }, { "submission_id": "aoj_2270_10308644", "code_snippet": "// competitive-verifier: PROBLEM\n#include <cmath>\n#include <cstdint>\n#include <numeric>\n/// @brief めぐる式二分探索\ntemplate <class T, class F>\nstd::int64_t meguru_binary_search(T ok, T ng, F check) {\n while (std::abs(ok - ng) > 1) {\n T mid = std::midpoint(ok, ng);\n (check(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n#include <cassert>\n#include <tuple>\n#include <vector>\nnamespace internal {\n/**\n * @brief 完備辞書\n *\n * @see https://ei1333.github.io/library/structure/wavelet/succinct-indexable-dictionary.hpp\n */\nstruct bit_vector {\n bit_vector() = default;\n bit_vector(unsigned int _length)\n : length(_length), blocks((_length + 31) >> 5), bit((_length + 31) >> 5),\n sum((_length + 31) >> 5) {}\n void set(unsigned int k) { bit[k >> 5] |= 1U << (k & 31); }\n void build() {\n sum[0] = 0U;\n for (unsigned int i = 1; i < blocks; ++i) {\n sum[i] = sum[i - 1] + __builtin_popcount(bit[i - 1]);\n }\n }\n bool operator[](unsigned int k) const { return bit[k >> 5] >> (k & 31) & 1; }\n unsigned int rank(unsigned int k) const {\n return sum[k >> 5] + __builtin_popcount(bit[k >> 5] & ((1U << (k & 31)) - 1));\n }\n unsigned int rank(bool val, unsigned int k) const { return val ? rank(k) : k - rank(k); }\n unsigned int select(unsigned int k) const {\n unsigned int sl = 0, sr = blocks + 1;\n while (sr - sl > 1) {\n unsigned int m = (sl + sr) >> 1;\n if (sum[m] < k) sl = m;\n else sr = m;\n }\n k -= sum[sl];\n unsigned int bl = 0, br = 32;\n while (br - bl > 1) {\n unsigned int m = (bl + br) >> 1;\n if (__builtin_popcount(bit[sl] & ((1U << m) - 1)) < k) bl = m;\n else br = m;\n }\n return (sl << 5) + bl;\n }\n private:\n unsigned int length, blocks;\n std::vector<unsigned int> bit, sum;\n};\n} // namespace internal\n/// @brief 重み付きウェーブレット行列\ntemplate <class T, class U = T, int L = 30>\nstruct wavelet_matrix_rectangle_sum {\n wavelet_matrix_rectangle_sum() = default;\n template <class Value>\n wavelet_matrix_rectangle_sum(const std::vector<T> &v, const std::vector<Value> &u)\n : length(v.size()) {\n assert(v.size() == u.size());\n std::vector<int> l(length), r(length), ord(length);\n std::iota(ord.begin(), ord.end(), 0);\n for (int level = L - 1; level >= 0; level--) {\n matrix[level] = internal::bit_vector(length + 1);\n int left = 0, right = 0;\n for (int i = 0; i < length; i++) {\n if ((v[ord[i]] >> level) & 1) {\n matrix[level].set(i);\n r[right++] = ord[i];\n } else {\n l[left++] = ord[i];\n }\n }\n mid[level] = left;\n matrix[level].build();\n ord.swap(l);\n for (int i = 0; i < right; i++) ord[left + i] = r[i];\n cs[level].resize(length + 1);\n cs[level][0] = U(0);\n for (int i = 0; i < length; i++) cs[level][i + 1] = cs[level][i] + u[ord[i]];\n }\n }\n U kth_smallest_sum(int l, int r, int k) const {\n assert(0 <= k && k <= r - l);\n T val = T();\n U res = U();\n for (int level = L - 1; level >= 0; --level) {\n int cnt = matrix[level].rank(false, r) - matrix[level].rank(false, l);\n bool f = cnt <= k;\n if (f) {\n val |= T(1) << level;\n res += cs[level][matrix[level].rank(false, r)] -\n cs[level][matrix[level].rank(false, l)];\n k -= cnt;\n }\n std::tie(l, r) = succ(f, l, r, level);\n }\n return res + val * k;\n }\n U kth_largest_sum(int l, int r, int k) const {\n return cs[L - 1][matrix[L - 1].rank(false, r)] + cs[L - 1][matrix[L - 1].rank(true, r)] -\n cs[L - 1][matrix[L - 1].rank(false, l)] - cs[L - 1][matrix[L - 1].rank(true, l)] -\n kth_smallest_sum(l, r, r - l - k);\n }\n U range_sum(int r, T x) const { return range_sum(0, r, x); }\n U range_sum(int l, int r, T x) const {\n for (int level = L - 1; level >= 0; level--)\n std::tie(l, r) = succ((x >> level) & 1, l, r, level);\n return cs[0][matrix[0].rank(false, r)] - cs[0][matrix[0].rank(false, l)];\n }\n U rect_sum(int l, int r, T upper) const {\n U res = 0;\n for (int level = L - 1; level >= 0; level--) {\n bool f = (upper >> level) & 1;\n if (f)\n res += cs[level][matrix[level].rank(false, r)] -\n cs[level][matrix[level].rank(false, l)];\n std::tie(l, r) = succ(f, l, r, level);\n }\n return res;\n }\n U rect_sum(int l, int r, T lower, T upper) const {\n return rect_sum(l, r, upper) - rect_sum(l, r, lower);\n }\n private:\n int length;\n internal::bit_vector matrix[L];\n int mid[L];\n std::vector<U> cs[L];\n std::pair<int, int> succ(bool f, int l, int r, int level) const {\n return {matrix[level].rank(f, l) + mid[level] * f,\n matrix[level].rank(f, r) + mid[level] * f};\n }\n};\n#include <iostream>\n/**\n * @brief 重み付きグラフ\n *\n * @tparam T 辺の重みの型\n */\ntemplate <class T>\nstruct Graph {\n private:\n struct _edge {\n constexpr _edge() : _from(), _to(), _weight() {}\n constexpr _edge(int from, int to, T weight) : _from(from), _to(to), _weight(weight) {}\n constexpr bool operator<(const _edge &rhs) const { return weight() < rhs.weight(); }\n constexpr bool operator>(const _edge &rhs) const { return rhs < *this; }\n constexpr int from() const { return _from; }\n constexpr int to() const { return _to; }\n constexpr T weight() const { return _weight; }\n private:\n int _from, _to;\n T _weight;\n };\n public:\n using edge_type = typename Graph<T>::_edge;\n Graph() : _size(), edges() {}\n Graph(int v) : _size(v), edges(v) {}\n const auto &operator[](int i) const { return edges[i]; }\n auto &operator[](int i) { return edges[i]; }\n const auto begin() const { return edges.begin(); }\n auto begin() { return edges.begin(); }\n const auto end() const { return edges.end(); }\n auto end() { return edges.end(); }\n constexpr int size() const { return _size; }\n void add_edge(const edge_type &e) { edges[e.from()].emplace_back(e); }\n void add_edge(int from, int to, T weight = T(1)) { edges[from].emplace_back(from, to, weight); }\n void add_edges(int from, int to, T weight = T(1)) {\n edges[from].emplace_back(from, to, weight);\n edges[to].emplace_back(to, from, weight);\n }\n void input_edge(int m, int base = 1) {\n for (int i = 0; i < m; ++i) {\n int from, to;\n T weight;\n std::cin >> from >> to >> weight;\n add_edge(from - base, to - base, weight);\n }\n }\n void input_edges(int m, int base = 1) {\n for (int i = 0; i < m; ++i) {\n int from, to;\n T weight;\n std::cin >> from >> to >> weight;\n add_edges(from - base, to - base, weight);\n }\n }\n private:\n int _size;\n std::vector<std::vector<edge_type>> edges;\n};\ntemplate <>\nstruct Graph<void> {\n private:\n struct _edge {\n constexpr _edge() : _from(), _to() {}\n constexpr _edge(int from, int to) : _from(from), _to(to) {}\n constexpr int from() const { return _from; }\n constexpr int to() const { return _to; }\n constexpr int weight() const { return 1; }\n constexpr bool operator<(const _edge &rhs) const { return weight() < rhs.weight(); }\n constexpr bool operator>(const _edge &rhs) const { return rhs < *this; }\n private:\n int _from, _to;\n };\n public:\n using edge_type = typename Graph<void>::_edge;\n Graph() : _size(), edges() {}\n Graph(int v) : _size(v), edges(v) {}\n const auto &operator[](int i) const { return edges[i]; }\n auto &operator[](int i) { return edges[i]; }\n const auto begin() const { return edges.begin(); }\n auto begin() { return edges.begin(); }\n const auto end() const { return edges.end(); }\n auto end() { return edges.end(); }\n constexpr int size() const { return _size; }\n void add_edge(const edge_type &e) { edges[e.from()].emplace_back(e); }\n void add_edge(int from, int to) { edges[from].emplace_back(from, to); }\n void add_edges(int from, int to) {\n edges[from].emplace_back(from, to);\n edges[to].emplace_back(to, from);\n }\n void input_edge(int m, int base = 1) {\n for (int i = 0; i < m; ++i) {\n int from, to;\n std::cin >> from >> to;\n add_edge(from - base, to - base);\n }\n }\n void input_edges(int m, int base = 1) {\n for (int i = 0; i < m; ++i) {\n int from, to;\n std::cin >> from >> to;\n add_edges(from - base, to - base);\n }\n }\n private:\n int _size;\n std::vector<std::vector<edge_type>> edges;\n};\n#ifdef ATCODER\n#pragma GCC target(\"sse4.2,avx512f,avx512dq,avx512ifma,avx512cd,avx512bw,avx512vl,bmi2\")\n#endif\n#pragma GCC optimize(\"Ofast,fast-math,unroll-all-loops\")\n#include <bits/stdc++.h>\n#ifndef ATCODER\n#pragma GCC target(\"sse4.2,avx2,bmi2\")\n#endif\ntemplate <class T, class U>\nconstexpr bool chmax(T &a, const U &b) {\n return a < (T)b ? a = (T)b, true : false;\n}\ntemplate <class T, class U>\nconstexpr bool chmin(T &a, const U &b) {\n return (T)b < a ? a = (T)b, true : false;\n}\nconstexpr std::int64_t INF = 1000000000000000003;\nconstexpr int Inf = 1000000003;\nconstexpr double EPS = 1e-7;\nconstexpr double PI = 3.14159265358979323846;\n#define FOR(i, m, n) for (int i = (m); i < int(n); ++i)\n#define FORR(i, m, n) for (int i = (m)-1; i >= int(n); --i)\n#define FORL(i, m, n) for (int64_t i = (m); i < int64_t(n); ++i)\n#define rep(i, n) FOR (i, 0, n)\n#define repn(i, n) FOR (i, 1, n + 1)\n#define repr(i, n) FORR (i, n, 0)\n#define repnr(i, n) FORR (i, n + 1, 1)\n#define all(s) (s).begin(), (s).end()\nstruct Sonic {\n Sonic() {\n std::ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout << std::fixed << std::setprecision(20);\n }\n constexpr void operator()() const {}\n} sonic;\nusing namespace std;\nusing ll = std::int64_t;\nusing ld = long double;\ntemplate <class T, class U>\nstd::istream &operator>>(std::istream &is, std::pair<T, U> &p) {\n return is >> p.first >> p.second;\n}\ntemplate <class T>\nstd::istream &operator>>(std::istream &is, std::vector<T> &v) {\n for (T &i : v) is >> i;\n return is;\n}\ntemplate <class T, class U>\nstd::ostream &operator<<(std::ostream &os, const std::pair<T, U> &p) {\n return os << '(' << p.first << ',' << p.second << ')';\n}\ntemplate <class T>\nstd::ostream &operator<<(std::ostream &os, const std::vector<T> &v) {\n for (auto it = v.begin(); it != v.end(); ++it) os << (it == v.begin() ? \"\" : \" \") << *it;\n return os;\n}\ntemplate <class Head, class... Tail>\nvoid co(Head &&head, Tail &&...tail) {\n if constexpr (sizeof...(tail) == 0) std::cout << head << '\\n';\n else std::cout << head << ' ', co(std::forward<Tail>(tail)...);\n}\ntemplate <class Head, class... Tail>\nvoid ce(Head &&head, Tail &&...tail) {\n if constexpr (sizeof...(tail) == 0) std::cerr << head << '\\n';\n else std::cerr << head << ' ', ce(std::forward<Tail>(tail)...);\n}\nvoid Yes(bool is_correct = true) { std::cout << (is_correct ? \"Yes\\n\" : \"No\\n\"); }\nvoid No(bool is_not_correct = true) { Yes(!is_not_correct); }\nvoid YES(bool is_correct = true) { std::cout << (is_correct ? \"YES\\n\" : \"NO\\n\"); }\nvoid NO(bool is_not_correct = true) { YES(!is_not_correct); }\nvoid Takahashi(bool is_correct = true) { std::cout << (is_correct ? \"Takahashi\" : \"Aoki\") << '\\n'; }\nvoid Aoki(bool is_not_correct = true) { Takahashi(!is_not_correct); }\n#include <bit>\n/// @brief スパーステーブル\ntemplate <class M>\nstruct sparse_table {\n private:\n using T = typename M::value_type;\n public:\n sparse_table() = default;\n sparse_table(const std::vector<T> &v) : _size(v.size()), data() {\n int b = std::max(1, std::countr_zero(std::bit_ceil<unsigned>(_size)));\n data.emplace_back(v);\n for (int i = 1; i < b; ++i) data.emplace_back(_size + 1 - (1 << i));\n for (int i = 1; i < b; ++i) {\n for (int j = 0; j + (1 << i) <= _size; ++j) {\n data[i][j] = M::op(data[i - 1][j], data[i - 1][j + (1 << (i - 1))]);\n }\n }\n }\n T prod(int l, int r) const {\n assert(0 <= l && l <= r && r <= _size);\n if (l == r) return M::id();\n if (l + 1 == r) return data[0][l];\n int b = 31 - std::countl_zero<unsigned>(r - l - 1);\n return M::op(data[b][l], data[b][r - (1 << b)]);\n }\n private:\n int _size;\n std::vector<std::vector<T>> data;\n};\nnamespace internal {\ntemplate <class T, int N>\nstruct fixed_stack {\n constexpr fixed_stack() : _size(), _data() {}\n constexpr T top() const { return _data[_size - 1]; }\n constexpr bool empty() const { return _size == 0; }\n constexpr int size() const { return _size; }\n constexpr void emplace(const T &e) { _data[_size++] = e; }\n constexpr void emplace(T &&e) { _data[_size++] = e; }\n constexpr void pop() { --_size; }\n constexpr void clear() { _size = 0; }\n private:\n int _size;\n std::array<T, N> _data;\n};\n} // namespace internal\n/**\n * @brief 線形 Sparse Table\n *\n * @tparam M\n */\ntemplate <class M>\nstruct linear_sparse_table {\n private:\n using T = M::value_type;\n static constexpr int W = 64;\n public:\n linear_sparse_table() = default;\n linear_sparse_table(const std::vector<T> &v) : _size(v.size()), data(v) {\n int n = v.size();\n int b = n / W;\n internal::fixed_stack<int, W + 1> st;\n std::vector<T> u(b);\n word_data.resize(b + (n > b * W));\n for (int i = 0; i < b; ++i) {\n T m = M::id();\n std::uint64_t bit = 0;\n std::vector<std::uint64_t> bits(W);\n for (int j = 0; j < W; ++j) {\n m = M::op(m, v[i * W + j]);\n while (!st.empty() && M::op(v[i * W + st.top()], v[i * W + j]) == v[i * W + j]) {\n bit ^= std::uint64_t(1) << st.top();\n st.pop();\n }\n bits[j] = bit;\n bit |= std::uint64_t(1) << j;\n st.emplace(j);\n }\n u[i] = m;\n word_data[i] = bits;\n st.clear();\n }\n if (n > b * W) {\n std::uint64_t bit = 0;\n std::vector<std::uint64_t> bits(n - b * W);\n for (int j = 0; j < n - b * W; ++j) {\n while (!st.empty() && M::op(v[b * W + st.top()], v[b * W + j]) == v[b * W + j]) {\n bit ^= std::uint64_t(1) << st.top();\n st.pop();\n }\n bits[j] = bit;\n bit |= std::uint64_t(1) << j;\n st.emplace(j);\n }\n word_data[b] = bits;\n }\n block_table = sparse_table<M>(u);\n }\n const T &operator[](int k) const { return data[k]; }\n T prod(int l, int r) const {\n assert(0 <= l && l < r && r <= _size);\n int lb = (l + W - 1) / W, rb = r / W;\n if (lb > rb) return word_prod(l, r);\n T res = (lb == rb ? M::id() : block_table.prod(lb, rb));\n if (l < lb * W) res = M::op(res, word_prod(l, lb * W));\n if (rb * W < r) res = M::op(res, word_prod(rb * W, r));\n return res;\n }\n private:\n int _size;\n std::vector<T> data;\n sparse_table<M> block_table;\n std::vector<std::vector<std::uint64_t>> word_data;\n T word_prod(int l, int r) const {\n if (l == r) return M::id();\n int b = l / W;\n int lw = l - b * W, rw = r - b * W;\n if ((word_data[b][rw - 1] >> lw) == 0ul) return data[r - 1];\n return data[l + std::countr_zero(word_data[b][rw - 1] >> lw)];\n }\n};\nstruct linear_lca {\n private:\n struct S {\n int depth, index;\n bool operator<(const S &rhs) const { return depth < rhs.depth; }\n bool operator==(const S &rhs) const = default;\n };\n struct M {\n using value_type = S;\n static constexpr S id() { return S{std::numeric_limits<int>::max(), -1}; }\n static constexpr S op(const S &lhs, const S &rhs) { return std::min(lhs, rhs); }\n };\n public:\n template <class T>\n linear_lca(const Graph<T> &g, int r = 0) : ord(g.size(), -1), lst() {\n std::vector<S> v;\n std::stack<std::pair<int, int>> st;\n st.emplace(r, 0);\n while (!st.empty()) {\n auto [x, d] = st.top();\n st.pop();\n if (x < 0) {\n v.emplace_back(d, ~x);\n continue;\n }\n ord[x] = v.size();\n v.emplace_back(d, x);\n for (auto e : g[x]) {\n if (ord[e.to()] != -1) continue;\n st.emplace(~x, d);\n st.emplace(e.to(), d + 1);\n }\n }\n lst = linear_sparse_table<M>(v);\n }\n int operator()(int u, int v) const { return lca(u, v); }\n int lca(int u, int v) const {\n auto [l, r] = std::minmax(ord[u], ord[v]);\n return lst.prod(l, r + 1).index;\n }\n private:\n std::vector<int> ord;\n linear_sparse_table<M> lst;\n};\nint main(void) {\n int n, q;\n cin >> n >> q;\n vector<int> a(n);\n cin >> a;\n Graph<void> g(n);\n g.input_edges(n - 1);\n vector<int> in(n), out(n), ord(n * 2), w(n * 2);\n int c = 0;\n auto dfs = [&](auto self, int x, int p) -> void {\n in[x] = c;\n w[c] = 1;\n ord[c++] = a[x];\n for (auto e : g[x]) {\n if (e.to() == p)\n continue;\n self(self, e.to(), x);\n }\n w[c] = -1;\n ord[c++] = a[x];\n out[x] = c;\n };\n dfs(dfs, 0, -1);\n linear_lca lca(g);\n wavelet_matrix_rectangle_sum wm(ord, w);\n while (q--) {\n int u, v, k;\n cin >> u >> v >> k;\n --u, --v;\n auto f = [&](int y) {\n return wm.rect_sum(0, in[u] + 1, y) + wm.rect_sum(0, in[v] + 1, y) -\n wm.rect_sum(0, in[lca(u, v)] + 1, y) - wm.rect_sum(0, in[lca(u, v)], y) <\n k;\n };\n auto ans = meguru_binary_search(0, *max_element(all(a)) + 1, f);\n co(ans);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 2810, "memory_kb": 49140, "score_of_the_acc": -1.3395, "final_rank": 14 }, { "submission_id": "aoj_2270_10307727", "code_snippet": "// AOJ #2270 The L-th Number\n// 2025.3.17\n\n#include <bits/stdc++.h>\nusing namespace std;\n\n#define gc() getchar_unlocked()\n#define pc(c) putchar_unlocked(c)\n\nint Cin() { // 整数の入力\n\tint n = 0; int c = gc();\n\tif (c == '-') {\tc = gc();\n\t\tdo n = 10*n + (c & 0xf), c = gc(); while (c >= '0');\n\t\treturn -n;\n\t}\n\tdo n = 10*n + (c & 0xf), c = gc(); while (c >= '0');\n\treturn n;\n}\n\nvoid Cout(int n) { // 整数の表示(出力)\n\tchar b[30];\n\tif (!n) pc('0');\n\telse {\n\t\tif (n < 0) pc('-'), n = -n;\n\t\tint i = 0; while (n) b[i++] = n % 10 + '0', n /= 10;\n\t\twhile (i--) pc(b[i]);\n\t}\n\tpc('\\n');\n}\n\nstruct Node { int value, left, right; };\nconst int MAXN = 100005;\nNode segTree[MAXN * 20];\nint segRoot[MAXN];\nint segTreeCount = 0;\n\nint modify(int lBound, int rBound, int pos, int previousNode) {\n int currentNode = ++segTreeCount;\n segTree[currentNode] = segTree[previousNode];\n segTree[currentNode].value++;\n\n if (lBound == rBound) return currentNode;\n\n int mid = (lBound + rBound) >> 1;\n if (pos <= mid) {\n segTree[currentNode].left = modify(lBound, mid, pos, segTree[previousNode].left);\n } else {\n segTree[currentNode].right = modify(mid + 1, rBound, pos, segTree[previousNode].right);\n }\n return currentNode;\n}\n\nint query(int lBound, int rBound, int kth, int nodeU, int nodeV, int nodeLCA, int nodeParentLCA) {\n if (lBound == rBound) return lBound;\n int mid = (lBound + rBound) >> 1;\n int countLeft = segTree[segTree[nodeU].left].value\n + segTree[segTree[nodeV].left].value\n - segTree[segTree[nodeLCA].left].value\n - segTree[segTree[nodeParentLCA].left].value;\n if (kth <= countLeft)\n return query(lBound, mid, kth,\n segTree[nodeU].left, segTree[nodeV].left,\n segTree[nodeLCA].left, segTree[nodeParentLCA].left);\n else return query(mid + 1, rBound, kth - countLeft,\n segTree[nodeU].right, segTree[nodeV].right,\n segTree[nodeLCA].right, segTree[nodeParentLCA].right);\n}\n\nint totEdges = 0;\nint head[MAXN], to[MAXN << 1], nxt[MAXN << 1];\n\nvoid addEdge(int u, int v) {\n to[++totEdges] = v;\n nxt[totEdges] = head[u];\n head[u] = totEdges;\n}\n\nint totDFS = 0;\nint parent[MAXN], depth[MAXN], top[MAXN], subTreeSize[MAXN], id[MAXN], heavySon[MAXN];\nint n, q, totValues;\nint value[MAXN], sortedValues[MAXN];\n\nvoid dfs1(int u) {\n subTreeSize[u] = 0;\n for (int edge = head[u]; edge; edge = nxt[edge]) {\n int v = to[edge];\n if (v == parent[u]) continue;\n parent[v] = u;\n depth[v] = depth[u] + 1;\n dfs1(v);\n subTreeSize[u] += subTreeSize[v];\n if (subTreeSize[v] > subTreeSize[heavySon[u]]) heavySon[u] = v;\n }\n subTreeSize[u]++;\n}\n\nvoid dfs2(int u, int topNode) {\n segRoot[u] = modify(1, totValues, value[u], segRoot[parent[u]]);\n id[u] = ++totDFS;\n top[u] = topNode;\n if (heavySon[u]) dfs2(heavySon[u], topNode);\n for (int edge = head[u]; edge; edge = nxt[edge]) {\n int v = to[edge];\n if (v == parent[u] || v == heavySon[u]) continue;\n dfs2(v, v);\n }\n}\n\nint lca(int u, int v) {\n while (top[u] != top[v]) {\n if (depth[top[u]] < depth[top[v]]) swap(u, v);\n u = parent[top[u]];\n }\n return depth[u] < depth[v] ? u : v;\n}\n\nint main() {\n n = Cin(), q = Cin();\n\n for (int i = 1; i <= n; i++) sortedValues[i] = value[i] = Cin();\n\n for (int i = 2; i <= n; i++) {\n int u = Cin(), v = Cin();\n addEdge(u, v);\n addEdge(v, u);\n }\n\n sort(sortedValues + 1, sortedValues + n + 1);\n totValues = unique(sortedValues + 1, sortedValues + n + 1) - (sortedValues + 1);\n for (int i = 1; i <= n; i++)\n value[i] = lower_bound(sortedValues + 1, sortedValues + totValues + 1, value[i]) - sortedValues;\n\n depth[1] = 1;\n dfs1(1);\n dfs2(1, 1);\n\n while (q--) {\n int u = Cin(), v = Cin(), kth = Cin();\n int commonAncestor = lca(u, v);\n int index = query(1, totValues, kth, segRoot[u], segRoot[v],\n segRoot[commonAncestor], segRoot[parent[commonAncestor]]);\n Cout(sortedValues[index]);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 90, "memory_kb": 32800, "score_of_the_acc": -0.2466, "final_rank": 1 }, { "submission_id": "aoj_2270_9667715", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#if __has_include(<atcoder/all>)\n#include <atcoder/all>\n#endif\nusing ll=long long;\nusing ull=unsigned long long;\nusing P=pair<ll,ll>;\ntemplate<typename T>using minque=priority_queue<T,vector<T>,greater<T>>;\ntemplate<typename T>bool chmax(T &a,const T &b){return (a<b?(a=b,true):false);}\ntemplate<typename T>bool chmin(T &a,const T &b){return (a>b?(a=b,true):false);}\ntemplate<typename T1,typename T2>istream &operator>>(istream &is,pair<T1,T2>&p){is>>p.first>>p.second;return is;}\ntemplate<typename T>istream &operator>>(istream &is,vector<T> &a){for(auto &i:a)is>>i;return is;}\ntemplate<typename T1,typename T2>void operator++(pair<T1,T2>&a,int n){a.first++,a.second++;}\ntemplate<typename T1,typename T2>void operator--(pair<T1,T2>&a,int n){a.first--,a.second--;}\ntemplate<typename T>void operator++(vector<T>&a,int n){for(auto &i:a)i++;}\ntemplate<typename T>void operator--(vector<T>&a,int n){for(auto &i:a)i--;}\n#define reps(i,a,n) for(int i=(a);i<int(n);i++)\n#define rep(i,n) reps(i,0,n)\n#define all(x) x.begin(),x.end()\n#define pcnt(x) __builtin_popcountll(x)\n#define fin(x) return cout<<x<<'\\n',static_cast<void>(0)\nll myceil(ll a,ll b){return (a+b-1)/b;}\ntemplate<typename T,size_t n,size_t id=0>\nauto vec(const int (&d)[n],const T &init=T()){\n if constexpr (id<n)return vector(d[id],vec<T,n,id+1>(d,init));\n else return init;\n}\n#ifdef LOCAL\n#include<debug.h>\n#else\n#define debug(...) static_cast<void>(0)\n#define debugg(...) static_cast<void>(0)\ntemplate<typename T1,typename T2>ostream &operator<<(ostream &os,const pair<T1,T2>&p){os<<p.first<<' '<<p.second;return os;}\n#endif\nvoid SOLVE();\nint main(){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout<<fixed<<setprecision(16);\n #ifdef LOCAL\n clock_t start=clock();\n #endif\n int testcase=1;\n //cin>>testcase;\n for(int i=0;i<testcase;i++){\n SOLVE();\n }\n #ifdef LOCAL\n cerr<<\"time:\";\n cerr<<(clock()-start)/1000;\n cerr<<\"ms\\n\";\n #endif\n}\nusing namespace std;\nstruct UnionFind{\nprivate:\n vector<int>par;\n int cs;\npublic:\n UnionFind(int n):par(n,-1),cs(n){}\n UnionFind(){}\n int root(int u){\n if(par[u]<0)return u;\n return par[u]=root(par[u]);\n }\n bool merge(int u,int v){\n int ru=root(u),rv=root(v);\n if(ru==rv)return false;\n if(par[ru]<par[rv])swap(ru,rv);\n par[rv]+=par[ru];\n par[ru]=rv;\n cs--;\n return true;\n }\n bool same(int u,int v){return root(u)==root(v);}\n int size(int u=-1){return u==-1?cs:-par[root(u)];}\n};\ntemplate<typename T=int>\nstruct Edge{\n int from,to;\n T weight;\n int index;\n Edge(int from_,int to_,T weight_=T(),int index_=-1):from(from_),to(to_),weight(weight_),index(index_){}\n Edge():from(-1),to(-1),weight(),index(-1){}\n friend ostream &operator<<(ostream &os,const Edge&e){\n os<<'[';\n os<<\"from:\"<<e.from;\n os<<\"to:\"<<e.to;\n os<<\"weight:\"<<e.weight;\n os<<\"index:\"<<e.index;\n os<<']';\n return os;\n }\n};\nusing namespace std;\ntemplate<typename T=int>\nstruct Tree{\nprivate:\n int n;\n vector<Edge<T>>edge;\n vector<Edge<T>>g;\n vector<int>ptr;\n struct tree_range{\n using iterator=typename vector<Edge<T>>::iterator;\n iterator l,r;\n iterator begin()const{return l;}\n iterator end()const{return r;}\n int size()const{return r-l;}\n bool empty()const{return !size();}\n Edge<T> &operator[](int i)const{return l[i];}\n };\n struct const_tree_range{\n using iterator=typename vector<Edge<T>>::const_iterator;\n iterator l,r;\n iterator begin()const{return l;}\n iterator end()const{return r;}\n int size()const{return r-l;}\n bool empty()const{return !size();}\n const Edge<T> &operator[](int i)const{return l[i];}\n };\npublic:\n Tree(int n_):n(n_){\n edge.reserve(n-1);\n }\n Tree():n(0){}\n template<bool weighted=false,bool index=1>\n void read(){\n for(int i=0;i<n-1;i++){\n int u,v;\n T w=T();\n cin>>u>>v;\n if constexpr(index)u--,v--;\n if constexpr(weighted)cin>>w;\n else w=1;\n edge.emplace_back(u,v,w,i);\n }\n build();\n }\n template<bool index=1>\n void readp(){\n for(int i=1;i<n;i++){\n int p;\n cin>>p;\n if constexpr(index)p--;\n edge.emplace_back(i,p,1,i);\n }\n build();\n }\n void add_edge(int u,int v){edge.emplace_back(u,v,1,edge.size());}\n void add_edge(int u,int v,T w){edge.emplace_back(u,v,w,edge.size());}\n void add_edge(int u,int v,T w,int idx){edge.emplace_back(u,v,w,idx);}\n void build(){\n vector<int>cnt(n+1,0);\n for(auto&&[u,v,w,i]:edge)cnt[u+1]++,cnt[v+1]++;\n for(int i=1;i<=n;i++)cnt[i]+=cnt[i-1];\n assert(cnt[n]==n*2-2);\n ptr=cnt;\n g.resize(n*2-2);\n for(auto&&[u,v,w,i]:edge){\n g[cnt[u]++]=Edge(u,v,w,i);\n g[cnt[v]++]=Edge(v,u,w,i);\n }\n }\n void remove_parent(int root=0){\n edge.resize(n-1);\n auto dfs=[&](auto self,int x,int p,T w)->void {\n for(const Edge<T>&e:(*this)[x])if(e.to!=p){\n edge[e.index]=e;\n self(self,e.to,x,e.weight);\n }\n };\n dfs(dfs,root,-1,T());\n vector<int>cnt(n+1,0);\n for(int i=0;i<n-1;i++)cnt[edge[i].from+1]++;\n for(int i=1;i<=n;i++)cnt[i]+=cnt[i-1];\n ptr=cnt;\n g.resize(cnt[n]);\n for(const Edge<T>&e:edge)g[cnt[e.from]++]=e;\n }\n void hld(int root=0){\n remove_parent(root);\n auto dfs=[&](auto self,int x)->int {\n int ret=1,mx=-1;\n for(Edge<T>&e:(*this)[x]){\n int s=self(self,e.to);\n ret+=s;\n if(mx<s){\n mx=s;\n swap((*this)[x][0],e);\n }\n }\n return ret;\n };\n dfs(dfs,root);\n }\n int size()const{return n;}\n tree_range operator[](int i){return tree_range{g.begin()+ptr[i],g.begin()+ptr[i+1]};}\n const_tree_range operator[](int i)const{return const_tree_range{g.begin()+ptr[i],g.begin()+ptr[i+1]};}\n const Edge<T>& get_edge(int i)const{return edge[i];}\n};\ntemplate<typename T>\nvector<int>offline_lca(const Tree<T>&t,const vector<pair<int,int>>&query,int root=0){\n int n=t.size();\n UnionFind uf(n);\n vector<int>st(n),mark(n),p(n),ret(query.size(),-1);\n int top=0;\n st[0]=root;\n for(const auto&[l,r]:query)mark[l]++,mark[r]++;\n vector<int>ptr;\n vector<pair<int,int>>q(query.size()*2);\n {\n vector<int>cnt(n+1);\n cnt[0]=0;\n for(int i=1;i<=n;i++)cnt[i]=cnt[i-1]+mark[i-1];\n ptr=cnt;\n for(int i=0;i<query.size();i++){\n q[cnt[query[i].first]++]=make_pair(query[i].second,i);\n q[cnt[query[i].second]++]=make_pair(query[i].first,i);\n }\n }\n for(int i=0;i<n;i++){\n p[i]=t[i].size();\n mark[i]=-1;\n }\n auto run=[&](int u)->bool {\n while(p[u]){\n int v=t[u][--p[u]].to;\n if(mark[v]==-1){\n st[++top]=v;\n return true;\n }\n }\n return false;\n };\n while(top!=-1){\n int u=st[top];\n if(mark[u]==-1)mark[u]=u;\n else{\n uf.merge(u,t[u][p[u]].to);\n mark[uf.root(u)]=u;\n }\n if(!run(u)){\n for(int i=ptr[u];i<ptr[u+1];i++){\n const auto&[v,j]=q[i];\n if(mark[v]!=-1&&ret[j]==-1)ret[j]=mark[uf.root(v)];\n }\n top--;\n }\n }\n return ret;\n}\nusing namespace std;\ntemplate<typename T,typename ADD,typename DEL,typename OUT>\nvoid mo_tree(Tree<T>t,vector<pair<int,int>>query,const ADD&add,const DEL&del,const OUT&out){\n int n=t.size();\n int q=query.size();\n int b=n/min(n,(int)sqrt(q));\n vector<int>dfs_order(n),dfs_order2(n*2-1);\n int d=0;\n t.remove_parent();\n auto dfs=[&](auto self,int x)->void {\n dfs_order[x]=d;\n dfs_order2[d++]=x;\n for(const Edge<T>&e:t[x]){\n self(self,e.to);\n dfs_order2[d++]=e.to;\n }\n };\n dfs(dfs,0);\n debug(dfs_order,dfs_order2);\n vector<int>lca=offline_lca(t,query);\n for(auto&[u,v]:query){\n u=dfs_order[u]+1;\n v=dfs_order[v]+1;\n if(u>v)swap(u,v);\n }\n vector<int>ord(q);\n iota(ord.begin(),ord.end(),0);\n sort(ord.begin(),ord.end(),[&](int x,int y)->bool {\n const pair<int,int>&a=query[x],&c=query[y];\n if(a.first/b!=c.first/b)return a.first<c.first;\n return ((a.first/b)&1)?a.second>c.second:a.second<c.second;\n });\n int l=0,r=0;\n vector<char>contain(n,false);\n auto flip=[&](int id){\n contain[id]^=1;\n if(contain[id])add(id);\n else del(id);\n };\n for(int i:ord){\n while(l>query[i].first)flip(dfs_order2[--l]);\n while(r<query[i].second)flip(dfs_order2[r++]);\n while(l<query[i].first)flip(dfs_order2[l++]);\n while(r>query[i].second)flip(dfs_order2[--r]);\n flip(lca[i]);\n out(i);\n flip(lca[i]);\n }\n}\ntemplate<typename T>\nstruct SquareRootDecomposition{\nprivate:\n vector<T>block,dlock;\n int n,b;\npublic:\n SquareRootDecomposition(int n_):n(n_),b(sqrt(n_)){\n block.resize((n+b-1)/b,0);\n dlock.resize(n,0);\n }\n inline void add(int i,T x){\n dlock[i]+=x;\n block[i/b]+=x;\n }\n inline void set(int i,T x){\n add(i,x-dlock[i]);\n }\n T sum(int l,int r)const{\n T ret=0;\n if(r-l<b){\n reps(i,l,r)ret+=dlock[i];\n }\n else{\n int l2=(l+b-1)/b*b;\n int r2=r/b*b;\n reps(i,l,l2)ret+=dlock[i];\n reps(i,r2,r)ret+=dlock[i];\n l=l2/b,r=r2/b;\n reps(i,l,r)ret+=block[i];\n }\n return ret;\n }\n int lower_bound(int id,T k=1)const{\n assert(k);\n if(k>0){\n int id2=(id+b-1)/b*b;\n reps(i,id,id2){\n if((k-=dlock[i])<=0)return i;\n }\n id=id2/b;\n while(id<block.size()){\n if(k-block[id]<=0)break;\n k-=block[id++];\n }\n if(id==block.size())return n;\n id*=b;\n reps(i,id,id+b){\n if((k-=dlock[i])<=0)return i;\n }\n }\n else{\n k=-k;\n int id2=id/b*b;\n for(int i=id-1;i>=id2;i--){\n if((k-=dlock[i])<=0)return i+1;\n }\n id=id2/b;\n while(id){\n if(k-block[id-1]<=0)break;\n k-=block[--id];\n }\n if(id==0)return 0;\n id*=b;\n for(int i=id-1;i>=id-b;i--){\n if((k-=dlock[i])<=0)return i+1;\n }\n }\n return -1;\n }\n friend ostream &operator<<(ostream &os,const SquareRootDecomposition<T>&srd){\n os<<srd.dlock;\n return os;\n }\n};\nvoid SOLVE(){\n int n,q;\n cin>>n>>q;\n vector<int>v(n);\n cin>>v;\n auto z(v);\n sort(all(z)),z.erase(unique(all(z)),z.end());\n rep(i,n)v[i]=lower_bound(all(z),v[i])-z.begin();\n Tree t(n);\n t.read();\n vector<pair<int,int>>query(q);\n vector<int>k(q);\n vector<int>ans(q);\n rep(i,q){\n cin>>query[i]>>k[i];\n query[i]--;\n }\n SquareRootDecomposition<int>srd(z.size());\n mo_tree(t,query,[&](int x){srd.add(v[x],1);},[&](int x){srd.add(v[x],-1);},[&](int x){ans[x]=z[srd.lower_bound(0,k[x])];});\n rep(i,q)cout<<ans[i]<<'\\n';\n}", "accuracy": 1, "time_ms": 540, "memory_kb": 25896, "score_of_the_acc": -0.3561, "final_rank": 3 }, { "submission_id": "aoj_2270_9555521", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n// #if __has_include(<atcoder/all>)\n// #include <atcoder/all>\n// using namespace atcoder;\n// template<int mod>istream &operator>>(istream &is,static_modint<mod> &a){long long b;is>>b;a=b;return is;}\n// istream &operator>>(istream &is,modint &a){long long b;cin>>b;a=b;return is;}\n// #endif\n#ifdef LOCAL\n#include \"debug.h\"\n#else\n#define debug(...) static_cast<void>(0)\n#define debugg(...) static_cast<void>(0)\n#define esper(...) static_cast<void>(0)\ntemplate<typename T1,typename T2>ostream &operator<<(ostream &os,const pair<T1,T2>&p){os<<p.first<<' '<<p.second;return os;}\n#endif\nusing ll=long long;\nusing ull=unsigned long long;\nusing P=pair<ll,ll>;\ntemplate<typename T>using minque=priority_queue<T,vector<T>,greater<T>>;\ntemplate<typename T>bool chmax(T &a,const T &b){return (a<b?(a=b,true):false);}\ntemplate<typename T>bool chmin(T &a,const T &b){return (a>b?(a=b,true):false);}\ntemplate<typename T1,typename T2>istream &operator>>(istream &is,pair<T1,T2>&p){is>>p.first>>p.second;return is;}\ntemplate<typename T>istream &operator>>(istream &is,vector<T> &a){for(auto &i:a)is>>i;return is;}\ntemplate<typename T1,typename T2>void operator++(pair<T1,T2>&a,int n){a.first++,a.second++;}\ntemplate<typename T1,typename T2>void operator--(pair<T1,T2>&a,int n){a.first--,a.second--;}\ntemplate<typename T>void operator++(vector<T>&a,int n){for(auto &i:a)i++;}\ntemplate<typename T>void operator--(vector<T>&a,int n){for(auto &i:a)i--;}\n#define reps(i,a,n) for(int i=(a);i<int(n);i++)\n#define rep(i,n) reps(i,0,n)\n#define all(x) x.begin(),x.end()\n#define pcnt(x) __builtin_popcountll(x)\n#define fin(x) return cout<<x<<'\\n',static_cast<void>(0)\nll myceil(ll a,ll b){return (a+b-1)/b;}\ntemplate<typename T,size_t n,size_t id=0>\nauto vec(const int (&d)[n],const T &init=T()){\n if constexpr (id<n)return vector(d[id],vec<T,n,id+1>(d,init));\n else return init;\n}\nvoid SOLVE();\nint main(){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout<<fixed<<setprecision(16);\n #ifdef LOCAL\n clock_t start=clock();\n #endif\n int testcase=1;\n //cin>>testcase;\n for(int i=0;i<testcase;i++){\n SOLVE();\n }\n #ifdef LOCAL\n cerr<<\"time:\";\n cerr<<(clock()-start)/1000;\n cerr<<\"ms\\n\";\n #endif\n}\nstruct dsu{\n vector<int>par;\n dsu(int n):par(n,-1){}\n int leader(int u){\n while(par[u]>=0)u=par[u];\n return u;\n }\n void merge(int u,int v){\n int ru=leader(u),rv=leader(v);\n if(ru!=rv){\n if(par[ru]>par[rv])swap(ru,rv);\n par[ru]+=par[rv];\n par[rv]=ru;\n }\n }\n\n};\nvector<int>offline_lca(vector<vector<int>>&g,vector<pair<int,int>>query,int root=0){\n int n=g.size();\n dsu uf(n);\n vector<int>st(n),mark(n),p(n),ret(query.size(),-1);\n int top=0;\n st[0]=root;\n for(auto &&[l,r]:query)mark[l]++,mark[r]++;\n vector<vector<pair<int,int>>>q(n);\n rep(i,n){\n q[i].reserve(mark[i]);\n mark[i]=-1;\n p[i]=g[i].size();\n }\n rep(i,query.size()){\n q[query[i].first].emplace_back(query[i].second,i);\n q[query[i].second].emplace_back(query[i].first,i);\n }\n auto run=[&](int u)->bool {\n while(p[u]){\n int v=g[u][--p[u]];\n if(mark[v]==-1){\n st[++top]=v;\n return true;\n }\n }\n return false;\n };\n while(top!=-1){\n int u=st[top];\n if(mark[u]==-1)mark[u]=u;\n else{\n uf.merge(u,g[u][p[u]]);\n mark[uf.leader(u)]=u;\n }\n if(!run(u)){\n for(auto&&[v,i]:q[u]){\n if(mark[v]!=-1&&ret[i]==-1)ret[i]=mark[uf.leader(v)];\n }\n top--;\n }\n }\n return ret;\n}\ntemplate<bool vertex_query=true>\nstruct MoTree{\npublic:\n vector<int>par;\nprivate:\n vector<vector<int>>g;\n int n,st;\n vector<pair<int,int>>Q;\n vector<int>dfs_order;\n vector<int>dfs_order2;\n int d;\n void dfs(int x,int p){\n par[x]=p;\n dfs_order[x]=d;\n dfs_order2[d++]=x;\n for(int i:g[x])if(i!=p){\n dfs(i,x);\n dfs_order2[d++]=i;\n }\n }\npublic:\n MoTree(int n):n(n),g(n),dfs_order(n),dfs_order2(n*2-1),d(0),par(n){}\n void add_edge(int u,int v){\n g[u].push_back(v);\n g[v].push_back(u);\n }\n void add_query(int u,int v){\n Q.push_back({u,v});\n }\n template<typename F1,typename F2,typename F3,typename F4>\n void build(F1 init,F2 add,F3 del,F4 out){\n int q=Q.size();\n st=n/min<int>(n,sqrt(q));\n dfs(0,-1);\n vector<int>lca=offline_lca(g,Q);\n debug(lca);\n rep(i,Q.size()){\n Q[i].first=dfs_order[Q[i].first]+1;\n Q[i].second=dfs_order[Q[i].second]+1;\n if(Q[i].first>Q[i].second)swap(Q[i].first,Q[i].second);\n }\n vector<int>ord(Q.size());\n iota(all(ord),0);\n sort(all(ord),[&](int x,int y){\n pair<int,int>a=Q[x],b=Q[y];\n if(a.first/st!=b.first/st)return a.first<b.first;\n return ((a.first/st)&1)?a.second>b.second:a.second<b.second;\n });\n init();\n int l=0,r=0;\n vector<char>contain(n,false);\n auto f=[&](int id)->void {\n contain[id]^=1;\n if(contain[id])add(id);\n else del(id);\n };\n for(int i:ord){\n while(l>Q[i].first)f(dfs_order2[--l]);\n while(r<Q[i].second)f(dfs_order2[r++]);\n while(l<Q[i].first)f(dfs_order2[l++]);\n while(r>Q[i].second)f(dfs_order2[--r]);\n if constexpr(vertex_query)f(lca[i]);\n out(i);\n if constexpr(vertex_query)f(lca[i]);\n }\n }\n};\nvoid SOLVE(){\n int n,q;\n cin>>n>>q;\n vector<int>x(n);\n cin>>x;\n MoTree mo(n);\n rep(i,n-1){\n int u,v;\n cin>>u>>v;\n u--,v--;\n mo.add_edge(u,v);\n }\n vector<int>kth(q);\n rep(i,q){\n int u,v,k;\n cin>>u>>v>>k;\n u--,v--;\n mo.add_query(u,v);\n kth[i]=k;\n }\n int vsize=n*2-1;\n int b=sqrt(vsize)+1;\n vector<int>block((vsize+b-1)/b,0);\n vector<int>dlock(vsize,0);\n vector<int>z;\n vector<int>ans(q);\n auto init=[&]()->void {\n z=x;\n sort(all(z)),z.erase(unique(all(z)),z.end());\n rep(i,n)x[i]=lower_bound(all(z),x[i])-z.begin();\n };\n auto add=[&](int i)->void {\n dlock[x[i]]++;\n block[x[i]/b]++;\n };\n auto del=[&](int i)->void {\n dlock[x[i]]--;\n block[x[i]/b]--;\n };\n auto out=[&](int i)->void {\n int pos=0;\n while(kth[i]>block[pos])kth[i]-=block[pos++];\n pos*=b;\n while(kth[i]>dlock[pos])kth[i]-=dlock[pos++];\n ans[i]=z[pos];\n };\n mo.build(init,add,del,out);\n rep(i,q)cout<<ans[i]<<'\\n';\n}", "accuracy": 1, "time_ms": 570, "memory_kb": 24432, "score_of_the_acc": -0.3559, "final_rank": 2 }, { "submission_id": "aoj_2270_8993168", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nstruct BIT{\n int N;\n vector<int> bit;\n BIT(int N_): N(N_), bit(N_ + 1, 0){\n }\n void add(int i, int x){\n i++; //0base->1base\n while (i <= N){\n bit[i] += x;\n i += i & -i;\n }\n }\n int sum(int i){ //1base\n int ans = 0;\n while (i > 0){\n ans += bit[i];\n i -= i & -i;\n }\n return ans;\n }\n int sum(int L, int R){\n return sum(R) - sum(L);\n }\n};\n\nint main() {\n int n,q;\n cin >> n >> q;\n \n vector<int> x(n);\n for(auto &xa:x) cin >> xa;\n vector<vector<int>> g(n);\n int a,b;\n for(int i=0;i<n-1;i++){\n cin >> a >> b;\n a--;b--;\n g[a].push_back(b);\n g[b].push_back(a);\n }\n vector<int> s1(q),s2(q),l(q),r(q);\n for(int i=0;i<q;i++){ \n cin >> s1[i] >> s2[i] >> l[i];\n s1[i]--;s2[i]--;\n }\n \n vector<int> xs=x;\n sort(xs.begin(),xs.end());\n xs.erase(unique(xs.begin(),xs.end()),xs.end());\n int cnt=(int)xs.size();\n vector<vector<int>> add(cnt);\n for(int i=0;i<n;i++){\n x[i]=find(xs.begin(),xs.end(),x[i])-xs.begin();\n add[x[i]].push_back(i);\n }\n\n int k=17;\n\n vector<int> in(n),out(n),dp(n);\n vector pr(n,vector<int>(k+2,-1));\n int t=0;\n function<void(int,int,int)> dfs=[&](int p,int cl,int d){\n in[cl]=t;\n t++;\n pr[cl][0]=p;\n dp[cl]=d;\n for(auto nx:g[cl]){\n if(nx==p) continue;\n dfs(cl,nx,d+1);\n }\n out[cl]=t;\n t++;\n };\n dfs(-1,0,0);\n\n for (int i = 0; i <=k; i++){\n for (int j = 0; j < n; j++){\n if (pr[j][i] != -1){\n pr[j][i+1] = pr[pr[j][i]][i];\n }\n }\n }\n\n function<int(int,int)> lca=[&](int u, int v){\n //cout << u << \" \" << v << endl;\n if (dp[u] > dp[v]) swap(u, v);\n int dif=dp[v]-dp[u];\n for(int i=0;i<=k;i++){\n if((dif>>i&1)==1) v=pr[v][i];\n }\n //cout << v << \"V\" << dp[v] << \" \" << dp[u] <<endl;\n \n if (u == v) return u;\n\n for (int i = k; i >= 0; i--){\n if (pr[u][i] != pr[v][i]){\n u = pr[u][i];\n v = pr[v][i];\n }\n }\n return pr[u][0];\n };\n \n for(int i=0;i<q;i++){ \n r[i]=lca(s1[i],s2[i]);\n //cout << i << \" \" << r[i] << endl;\n }\n\n vector<int> tv(q, 0), fv(q, cnt);\n while (true){\n bool ok = true;\n vector<vector<int>> query(cnt);\n for (int i = 0; i < q; i++){\n if (fv[i] - tv[i] > 1){\n ok = false;\n query[(tv[i] + fv[i]) / 2].push_back(i);\n }\n }\n if (ok){\n break;\n }\n BIT bit(n * 2);\n for (int i = 0; i < cnt; i++){\n for (int j : query[i]){\n if(fv[j]-tv[j]<=1) continue; \n int sum = bit.sum(in[r[j]] + 1, in[s1[j]] + 1) + bit.sum(in[r[j]] + 1, in[s2[j]] + 1);\n if (x[r[j]] < i){\n sum++;\n }\n //cout << i << \" \" << j << \" \" <<sum << \" \" << l[j] << endl;\n if (sum < l[j]){\n tv[j] = i;\n } else {\n fv[j] = i;\n }\n }\n for (int j : add[i]){\n bit.add(in[j], 1);\n bit.add(out[j], -1);\n }\n //for(int k=0;k<N*2;k++) cout << BIT.sum(k,k+1) << \" \";\n //cout << endl;\n }\n }\n \n //cout << cnt << endl;\n \n for (int i = 0; i < q; i++){\n cout << xs[tv[i]] << endl;\n }\n\n}", "accuracy": 1, "time_ms": 1530, "memory_kb": 39752, "score_of_the_acc": -0.8126, "final_rank": 6 }, { "submission_id": "aoj_2270_8993159", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nstruct BIT{\n int N;\n vector<int> bit;\n BIT(int N_): N(N_), bit(N_ + 1, 0){\n }\n void add(int i, int x){\n i++; //0base->1base\n while (i <= N){\n bit[i] += x;\n i += i & -i;\n }\n }\n int sum(int i){ //1base\n int ans = 0;\n while (i > 0){\n ans += bit[i];\n i -= i & -i;\n }\n return ans;\n }\n int sum(int L, int R){\n return sum(R) - sum(L);\n }\n};\n\nint main() {\n int n,q;\n cin >> n >> q;\n \n vector<int> x(n);\n for(auto &xa:x) cin >> xa;\n vector<vector<int>> g(n);\n int a,b;\n for(int i=0;i<n-1;i++){\n cin >> a >> b;\n a--;b--;\n g[a].push_back(b);\n g[b].push_back(a);\n }\n vector<int> s1(q),s2(q),l(q),r(q);\n for(int i=0;i<q;i++){ \n cin >> s1[i] >> s2[i] >> l[i];\n s1[i]--;s2[i]--;\n }\n \n vector<int> xs=x;\n sort(xs.begin(),xs.end());\n xs.erase(unique(xs.begin(),xs.end()),xs.end());\n int cnt=(int)xs.size();\n vector<vector<int>> add(cnt);\n for(int i=0;i<n;i++){\n x[i]=find(xs.begin(),xs.end(),x[i])-xs.begin();\n add[x[i]].push_back(i);\n }\n\n int k=17;\n\n vector<int> in(n),out(n),dp(n);\n vector pr(n,vector<int>(k+2,-1));\n int t=0;\n function<void(int,int,int)> dfs=[&](int p,int cl,int d){\n in[cl]=t;\n t++;\n pr[cl][0]=p;\n dp[cl]=d;\n for(auto nx:g[cl]){\n if(nx==p) continue;\n dfs(cl,nx,d+1);\n }\n out[cl]=t;\n t++;\n };\n dfs(-1,0,0);\n\n for (int i = 0; i <=k; i++){\n for (int j = 0; j < n; j++){\n if (pr[j][i] != -1){\n pr[j][i+1] = pr[pr[j][i]][i];\n }\n }\n }\n\n function<int(int,int)> lca=[&](int u, int v){\n //cout << u << \" \" << v << endl;\n if (dp[u] > dp[v]) swap(u, v);\n int dif=dp[v]-dp[u];\n for(int i=0;i<=k;i++){\n if((dif>>i&1)==1) v=pr[v][i];\n }\n //cout << v << \"V\" << dp[v] << \" \" << dp[u] <<endl;\n \n if (u == v) return u;\n\n for (int i = k; i >= 0; i--){\n if (pr[u][i] != pr[v][i]){\n u = pr[u][i];\n v = pr[v][i];\n }\n }\n return pr[u][0];\n };\n \n for(int i=0;i<q;i++){ \n r[i]=lca(s1[i],s2[i]);\n //cout << i << \" \" << r[i] << endl;\n }\n\n vector<int> tv(q, 0), fv(q, cnt);\n while (true){\n bool ok = true;\n vector<vector<int>> query(cnt);\n for (int i = 0; i < q; i++){\n if (fv[i] - tv[i] > 1){\n ok = false;\n query[(tv[i] + fv[i]) / 2].push_back(i);\n }\n }\n if (ok){\n break;\n }\n BIT bit(n * 2);\n for (int i = 0; i < cnt; i++){\n for (int j : query[i]){\n int sum = bit.sum(in[r[j]] + 1, in[s1[j]] + 1) + bit.sum(in[r[j]] + 1, in[s2[j]] + 1);\n if (x[r[j]] < i){\n sum++;\n }\n //cout << i << \" \" << j << \" \" <<sum << \" \" << l[j] << endl;\n if (sum < l[j]){\n tv[j] = i;\n } else {\n fv[j] = i;\n }\n }\n for (int j : add[i]){\n bit.add(in[j], 1);\n bit.add(out[j], -1);\n }\n //for(int k=0;k<N*2;k++) cout << BIT.sum(k,k+1) << \" \";\n //cout << endl;\n }\n }\n \n //cout << cnt << endl;\n \n for (int i = 0; i < q; i++){\n cout << xs[tv[i]] << endl;\n }\n\n}", "accuracy": 1, "time_ms": 1530, "memory_kb": 39832, "score_of_the_acc": -0.8132, "final_rank": 7 }, { "submission_id": "aoj_2270_8993149", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nstruct BIT{\n int N;\n vector<int> bit;\n BIT(int N_): N(N_), bit(N_ + 1, 0){\n }\n void add(int i, int x){\n i++; //0base->1base\n while (i <= N){\n bit[i] += x;\n i += i & -i;\n }\n }\n int sum(int i){ //1base\n int ans = 0;\n while (i > 0){\n ans += bit[i];\n i -= i & -i;\n }\n return ans;\n }\n int sum(int L, int R){\n return sum(R) - sum(L);\n }\n};\n\nint main() {\n int n,q;\n cin >> n >> q;\n \n vector<int> x(n);\n for(auto &xa:x) cin >> xa;\n vector<vector<int>> g(n);\n int a,b;\n for(int i=0;i<n-1;i++){\n cin >> a >> b;\n a--;b--;\n g[a].push_back(b);\n g[b].push_back(a);\n }\n vector<int> s1(q),s2(q),l(q),r(q);\n for(int i=0;i<q;i++){ \n cin >> s1[i] >> s2[i] >> l[i];\n s1[i]--;s2[i]--;\n }\n \n vector<int> xs=x;\n sort(xs.begin(),xs.end());\n xs.erase(unique(xs.begin(),xs.end()),xs.end());\n int cnt=(int)xs.size();\n vector<vector<int>> add(cnt);\n for(int i=0;i<n;i++){\n x[i]=find(xs.begin(),xs.end(),x[i])-xs.begin();\n add[x[i]].push_back(i);\n }\n\n int k=2,kk=2;\n while(kk<n){k++; kk=kk*kk;}\n //cout << k << endl;\n\n vector<int> in(n),out(n),dp(n);\n vector pr(n,vector<int>(k+2,-1));\n int t=0;\n function<void(int,int,int)> dfs=[&](int p,int cl,int d){\n in[cl]=t;\n t++;\n pr[cl][0]=p;\n dp[cl]=d;\n for(auto nx:g[cl]){\n if(nx==p) continue;\n dfs(cl,nx,d+1);\n }\n out[cl]=t;\n t++;\n };\n dfs(-1,0,0);\n\n for (int i = 0; i <=k; i++){\n for (int j = 0; j < n; j++){\n if (pr[j][i] != -1){\n pr[j][i+1] = pr[pr[j][i]][i];\n }\n }\n }\n\n function<int(int,int)> lca=[&](int u, int v){\n //cout << u << \" \" << v << endl;\n if (dp[u] > dp[v]) swap(u, v);\n int dif=dp[v]-dp[u];\n for(int i=0;i<=k;i++){\n if((dif>>i&1)==1) v=pr[v][i];\n }\n //cout << v << \"V\" << dp[v] << \" \" << dp[u] <<endl;\n \n if (u == v) return u;\n\n for (int i = k; i >= 0; i--){\n if (pr[u][i] != pr[v][i]){\n u = pr[u][i];\n v = pr[v][i];\n }\n }\n return pr[u][0];\n };\n \n for(int i=0;i<q;i++){ \n r[i]=lca(s1[i],s2[i]);\n //cout << i << \" \" << r[i] << endl;\n }\n\n vector<int> tv(q,-1),ss(q,0);\n BIT bit(n*2);\n for (int i = 0; i < cnt; i++){\n for (int j : add[i]){\n bit.add(in[j], 1);\n bit.add(out[j], -1);\n }\n //for(int k1=0;k1<n*2;k1++) cout << bit.sum(k1,k1+1) << \" \";\n //cout << endl;\n \n for(int j=0;j<q;j++){\n if(tv[j]!=-1) continue;\n int sum = bit.sum(in[r[j]] + 1, in[s1[j]] + 1) + bit.sum(in[r[j]] + 1, in[s2[j]] + 1);\n if (x[r[j]] <= i) sum++;\n //cout << i << \" \" << j << \" \" <<sum << \" \" << l[j] << endl;\n if((sum-ss[j]>1)&&(ss[j]+1<=l[j])&&(sum>=l[j])) tv[j]=i;\n else if(sum==l[j]) tv[j]=i;\n else ss[j]=sum;\n }\n }\n \n //cout << cnt << endl;\n \n for (int i = 0; i < q; i++){\n cout << xs[tv[i]] << endl;\n }\n\n}", "accuracy": 0.41935483870967744, "time_ms": 10, "memory_kb": 3456, "score_of_the_acc": 0, "final_rank": 20 }, { "submission_id": "aoj_2270_8216308", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntemplate <class Key> struct RandomizedBinarySearchTree {\n inline int xor128() {\n static int x = 123456789;\n static int y = 362436069;\n static int z = 521288629;\n static int w = 88675123;\n int t;\n t = x ^ (x << 11);\n x = y;\n y = z;\n z = w;\n return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));\n }\n struct Node {\n Node *l, *r;\n int cnt;\n Key key;\n Node() {}\n Node(const Key &k) : cnt(1), key(k), l(nullptr), r(nullptr) {}\n };\n vector<Node> pool;\n int ptr;\n RandomizedBinarySearchTree(int sz) : pool(sz), ptr(0) {}\n inline Node *alloc(const Key &key) { return &(pool[ptr++] = Node(key)); }\n virtual Node *clone(Node *t) { return t; }\n inline int count(const Node *t) { return t ? t->cnt : 0; }\n inline Node *update(Node *t) {\n t->cnt = count(t->l) + count(t->r) + 1;\n return t;\n }\n Node *propagete(Node *t) { return update(clone(t)); }\n Node *merge(Node *l, Node *r) {\n if (!l || !r)\n return l ? l : r;\n if (xor128() % (l->cnt + r->cnt) < l->cnt) {\n l = propagete(l);\n l->r = merge(l->r, r);\n return update(l);\n } else {\n r = propagete(r);\n r->l = merge(l, r->l);\n return update(r);\n }\n }\n pair<Node *, Node *> split(Node *t, int k) {\n if (!t)\n return {t, t};\n t = propagete(t);\n if (k <= count(t->l)) {\n auto s = split(t->l, k);\n t->l = s.second;\n return {s.first, update(t)};\n } else {\n auto s = split(t->r, k - count(t->l) - 1);\n t->r = s.first;\n return {update(t), s.second};\n }\n }\n Node *build(int l, int r, const vector<Key> &v) {\n if (l + 1 >= r)\n return alloc(v[l]);\n return merge(build(l, (l + r) >> 1, v), build((l + r) >> 1, r, v));\n }\n Node *build(const vector<Key> &v) {\n ptr = 0;\n return build(0, (int)v.size(), v);\n }\n void dump(Node *r, typename vector<Key>::iterator &it) {\n if (!r)\n return;\n r = propagete(r);\n dump(r->l, it);\n *it = r->key;\n dump(r->r, ++it);\n }\n vector<Key> dump(Node *r) {\n vector<Key> v((size_t)count(r));\n auto it = begin(v);\n dump(r, it);\n return v;\n }\n string to_string(Node *r) {\n auto s = dump(r);\n string ret;\n for (int i = 0; i < s.size(); i++)\n ret += \", \";\n return (ret);\n }\n void insert(Node *&t, int k, const Key &v) {\n auto x = split(t, k);\n t = merge(merge(x.first, alloc(v)), x.second);\n }\n void erase(Node *&t, int k) {\n auto x = split(t, k);\n t = merge(x.first, split(x.second, 1).second);\n }\n int size(Node *t) { return count(t); }\n bool empty(Node *t) { return !t; }\n Node *makeset() { return (nullptr); }\n};\ntemplate <class Key>\nstruct PresidentRandomizedBinarySearchTree : RandomizedBinarySearchTree<Key> {\n using RBST = RandomizedBinarySearchTree<Key>;\n using Node = typename RBST::Node;\n PresidentRandomizedBinarySearchTree(int sz) : RBST(sz) {}\n Node *clone(Node *t) override { return &(RBST::pool[RBST::ptr++] = *t); }\n Node *rebuild(Node *r) { return RBST::build(RBST::dump(r)); }\n};\ntemplate <class T>\nstruct OrderedMultiSet : PresidentRandomizedBinarySearchTree<T> {\n using RBST = PresidentRandomizedBinarySearchTree<T>;\n using Node = typename RBST::Node;\n OrderedMultiSet(int sz) : RBST(sz) {}\n T kth_element(Node *t, int k) {\n if (k < RBST::count(t->l))\n return kth_element(t->l, k);\n if (k == RBST::count(t->l))\n return t->key;\n return kth_element(t->r, k - RBST::count(t->l) - 1);\n }\n virtual void insert_key(Node *&t, const T &x) {\n RBST::insert(t, lower_bound(t, x), x);\n }\n void erase_key(Node *&t, const T &x) {\n if (!count(t, x))\n return;\n RBST::erase(t, lower_bound(t, x));\n }\n int count(Node *t, const T &x) {\n return upper_bound(t, x) - lower_bound(t, x);\n }\n int lower_bound(Node *t, const T &x) {\n if (!t)\n return 0;\n if (x <= t->key)\n return lower_bound(t->l, x);\n return lower_bound(t->r, x) + RBST::count(t->l) + 1;\n }\n int upper_bound(Node *t, const T &x) {\n if (!t)\n return 0;\n if (x < t->key)\n return upper_bound(t->l, x);\n return upper_bound(t->r, x) + RBST::count(t->l) + 1;\n }\n};\nstruct CentroidPathDecomposition {\n struct Centroid {\n int ParIndex, ParDepth, Deep;\n vector<int> node;\n Centroid(int idx, int dep, int deep)\n : ParIndex(idx), ParDepth(dep), Deep(deep) {}\n inline size_t size() { return (node.size()); }\n inline int &operator[](int k) { return (node[k]); }\n inline pair<int, int> Up() { return (make_pair(ParIndex, ParDepth)); }\n };\n vector<vector<int>> graph;\n vector<int> SubTreeSize, NextPath;\n vector<int> TreeIndex, TreeDepth;\n vector<Centroid> Centroids;\n void BuildSubTreeSize() {\n stack<pair<int, int>> s;\n s.emplace(0, -1);\n while (!s.empty()) {\n auto p = s.top();\n s.pop();\n if (~SubTreeSize[p.first]) {\n NextPath[p.first] = -1;\n for (auto &to : graph[p.first]) {\n if (p.second == to)\n continue;\n SubTreeSize[p.first] += SubTreeSize[to];\n if (NextPath[p.first] == -1 ||\n SubTreeSize[NextPath[p.first]] < SubTreeSize[to]) {\n NextPath[p.first] = to;\n }\n }\n } else {\n s.push(p);\n SubTreeSize[p.first] = 1;\n for (auto &to : graph[p.first]) {\n if (p.second != to)\n s.emplace(to, p.first);\n }\n }\n }\n }\n void BuildPath() {\n stack<pair<int, int>> s;\n Centroids.emplace_back(-1, -1, 0);\n s.emplace(0, -1);\n TreeIndex[0] = 0;\n while (!s.empty()) {\n auto p = s.top();\n s.pop();\n TreeDepth[p.first] = (int)Centroids[TreeIndex[p.first]].size();\n for (auto &to : graph[p.first]) {\n if (p.second == to)\n continue;\n if (to == NextPath[p.first]) {\n TreeIndex[to] = TreeIndex[p.first];\n } else {\n TreeIndex[to] = (int)Centroids.size();\n Centroids.emplace_back(TreeIndex[p.first], TreeDepth[p.first],\n Centroids[TreeIndex[p.first]].Deep + 1);\n }\n s.emplace(to, p.first);\n }\n Centroids[TreeIndex[p.first]].node.emplace_back(p.first);\n }\n }\n void AddEdge(int x, int y) {\n graph[x].push_back(y);\n graph[y].push_back(x);\n }\n virtual void Build() {\n BuildSubTreeSize();\n BuildPath();\n }\n inline size_t size() { return (Centroids.size()); }\n inline pair<int, int> Information(int idx) {\n return (make_pair(TreeIndex[idx], TreeDepth[idx]));\n }\n inline Centroid &operator[](int k) { return (Centroids[k]); }\n inline int LCA(int a, int b) {\n int TreeIdxA, TreeDepthA, TreeIdxB, TreeDepthB;\n tie(TreeIdxA, TreeDepthA) = Information(a);\n tie(TreeIdxB, TreeDepthB) = Information(b);\n while (TreeIdxA != TreeIdxB) {\n if (Centroids[TreeIdxA].Deep > Centroids[TreeIdxB].Deep) {\n tie(TreeIdxA, TreeDepthA) = Centroids[TreeIdxA].Up();\n } else {\n tie(TreeIdxB, TreeDepthB) = Centroids[TreeIdxB].Up();\n }\n }\n if (TreeDepthA > TreeDepthB)\n swap(TreeDepthA, TreeDepthB);\n return (Centroids[TreeIdxA][TreeDepthA]);\n }\n inline virtual void query(int a, int b,\n const function<void(int, int, int)> &f) {\n int TreeIdxA, TreeDepthA, TreeIdxB, TreeDepthB;\n tie(TreeIdxA, TreeDepthA) = Information(a);\n tie(TreeIdxB, TreeDepthB) = Information(b);\n while (TreeIdxA != TreeIdxB) {\n if (Centroids[TreeIdxA].Deep > Centroids[TreeIdxB].Deep) {\n f(TreeIdxA, 0, TreeDepthA + 1);\n tie(TreeIdxA, TreeDepthA) = Centroids[TreeIdxA].Up();\n } else {\n f(TreeIdxB, 0, TreeDepthB + 1);\n tie(TreeIdxB, TreeDepthB) = Centroids[TreeIdxB].Up();\n }\n }\n if (TreeDepthA > TreeDepthB)\n swap(TreeDepthA, TreeDepthB);\n f(TreeIdxA, TreeDepthA, TreeDepthB + 1);\n }\n CentroidPathDecomposition(int SZ) {\n graph.resize(SZ);\n SubTreeSize.assign(SZ, -1);\n NextPath.resize(SZ);\n TreeIndex.resize(SZ);\n TreeDepth.resize(SZ);\n }\n};\nOrderedMultiSet<int> tree(5000000);\nOrderedMultiSet<int>::Node *nodes[100000];\nint N, Q, X[100000];\nvector<int> g[100000];\nint parent[100000];\nvoid dfs(int idx, int par, OrderedMultiSet<int>::Node *par_set) {\n parent[idx] = par;\n tree.insert_key(par_set, X[idx]);\n nodes[idx] = par_set;\n for (auto &to : g[idx])\n if (to != par)\n dfs(to, idx, par_set);\n}\nint main() {\n scanf(\"%d %d\", &N, &Q);\n CentroidPathDecomposition gg(N);\n vector<int> vs;\n for (int i = 0; i < N; i++) {\n scanf(\"%d\", &X[i]);\n vs.emplace_back(X[i]);\n }\n sort(begin(vs), end(vs));\n vs.erase(unique(begin(vs), end(vs)), end(vs));\n for (int i = 1; i < N; i++) {\n int x, y;\n scanf(\"%d %d\", &x, &y);\n --x, --y;\n g[x].emplace_back(y);\n g[y].emplace_back(x);\n gg.AddEdge(x, y);\n }\n dfs(0, -1, tree.makeset());\n gg.Build();\n for (int i = 0; i < Q; i++) {\n int x, y, z;\n scanf(\"%d %d %d\", &x, &y, &z);\n --x, --y;\n int lca = gg.LCA(x, y);\n int ng = -1, ok = (int)vs.size() - 1;\n auto sum = [&](int v) {\n int ret = 0;\n ret += tree.upper_bound(nodes[x], vs[v]);\n ret += tree.upper_bound(nodes[y], vs[v]);\n ret -= X[lca] <= vs[v];\n if (lca)\n ret -= 2 * tree.upper_bound(nodes[parent[lca]], vs[v]);\n return ret;\n };\n while (ok - ng > 1) {\n int mid = (ok + ng) >> 1;\n if (sum(mid) >= z)\n ok = mid;\n else\n ng = mid;\n }\n printf(\"%d\\n\", vs[ok]);\n }\n}", "accuracy": 1, "time_ms": 780, "memory_kb": 132436, "score_of_the_acc": -1.2335, "final_rank": 10 }, { "submission_id": "aoj_2270_8216303", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntemplate <class Key> struct RandomizedBinarySearchTree {\n inline int xor128() {\n static int x = 123456789;\n static int y = 362436069;\n static int z = 521288629;\n static int w = 88675123;\n int t;\n t = x ^ (x << 11);\n x = y;\n y = z;\n z = w;\n return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));\n }\n struct Node {\n Node *l, *r;\n int cnt;\n Key key;\n Node() {}\n Node(const Key &k) : cnt(1), key(k), l(nullptr), r(nullptr) {}\n };\n vector<Node> pool;\n int ptr;\n RandomizedBinarySearchTree(int sz) : pool(sz), ptr(0) {}\n inline Node *alloc(const Key &key) { return &(pool[ptr++] = Node(key)); }\n virtual Node *clone(Node *t) { return t; }\n inline int count(const Node *t) { return t ? t->cnt : 0; }\n inline Node *update(Node *t) {\n t->cnt = count(t->l) + count(t->r) + 1;\n return t;\n }\n Node *propagete(Node *t) { return update(clone(t)); }\n Node *merge(Node *l, Node *r) {\n if (!l || !r)\n return l ? l : r;\n if (xor128() % (l->cnt + r->cnt) < l->cnt) {\n l = propagete(l);\n l->r = merge(l->r, r);\n return update(l);\n } else {\n r = propagete(r);\n r->l = merge(l, r->l);\n return update(r);\n }\n }\n pair<Node *, Node *> split(Node *t, int k) {\n if (!t)\n return {t, t};\n t = propagete(t);\n if (k <= count(t->l)) {\n auto s = split(t->l, k);\n t->l = s.second;\n return {s.first, update(t)};\n } else {\n auto s = split(t->r, k - count(t->l) - 1);\n t->r = s.first;\n return {update(t), s.second};\n }\n }\n Node *build(int l, int r, const vector<Key> &v) {\n if (l + 1 >= r)\n return alloc(v[l]);\n return merge(build(l, (l + r) >> 1, v), build((l + r) >> 1, r, v));\n }\n Node *build(const vector<Key> &v) {\n ptr = 0;\n return build(0, (int)v.size(), v);\n }\n void dump(Node *r, typename vector<Key>::iterator &it) {\n if (!r)\n return;\n r = propagete(r);\n dump(r->l, it);\n *it = r->key;\n dump(r->r, ++it);\n }\n vector<Key> dump(Node *r) {\n vector<Key> v((size_t)count(r));\n auto it = begin(v);\n dump(r, it);\n return v;\n }\n string to_string(Node *r) {\n auto s = dump(r);\n string ret;\n for (int i = 0; i < s.size(); i++)\n ret += \", \";\n return (ret);\n }\n void insert(Node *&t, int k, const Key &v) {\n auto x = split(t, k);\n t = merge(merge(x.first, alloc(v)), x.second);\n }\n void erase(Node *&t, int k) {\n auto x = split(t, k);\n t = merge(x.first, split(x.second, 1).second);\n }\n int size(Node *t) { return count(t); }\n bool empty(Node *t) { return !t; }\n Node *makeset() { return (nullptr); }\n};\ntemplate <class Key>\nstruct PresidentRandomizedBinarySearchTree : RandomizedBinarySearchTree<Key> {\n using RBST = RandomizedBinarySearchTree<Key>;\n using Node = typename RBST::Node;\n PresidentRandomizedBinarySearchTree(int sz) : RBST(sz) {}\n Node *clone(Node *t) override { return &(RBST::pool[RBST::ptr++] = *t); }\n Node *rebuild(Node *r) { return RBST::build(RBST::dump(r)); }\n};\ntemplate <class T>\nstruct OrderedMultiSet : PresidentRandomizedBinarySearchTree<T> {\n using RBST = PresidentRandomizedBinarySearchTree<T>;\n using Node = typename RBST::Node;\n OrderedMultiSet(int sz) : RBST(sz) {}\n T kth_element(Node *t, int k) {\n if (k < RBST::count(t->l))\n return kth_element(t->l, k);\n if (k == RBST::count(t->l))\n return t->key;\n return kth_element(t->r, k - RBST::count(t->l) - 1);\n }\n virtual void insert_key(Node *&t, const T &x) {\n RBST::insert(t, lower_bound(t, x), x);\n }\n void erase_key(Node *&t, const T &x) {\n if (!count(t, x))\n return;\n RBST::erase(t, lower_bound(t, x));\n }\n int count(Node *t, const T &x) {\n return upper_bound(t, x) - lower_bound(t, x);\n }\n int lower_bound(Node *t, const T &x) {\n if (!t)\n return 0;\n if (x <= t->key)\n return lower_bound(t->l, x);\n return lower_bound(t->r, x) + RBST::count(t->l) + 1;\n }\n int upper_bound(Node *t, const T &x) {\n if (!t)\n return 0;\n if (x < t->key)\n return upper_bound(t->l, x);\n return upper_bound(t->r, x) + RBST::count(t->l) + 1;\n }\n};\nstruct CentroidPathDecomposition {\n struct Centroid {\n int ParIndex, ParDepth, Deep;\n vector<int> node;\n Centroid(int idx, int dep, int deep)\n : ParIndex(idx), ParDepth(dep), Deep(deep) {}\n inline size_t size() { return (node.size()); }\n inline int &operator[](int k) { return (node[k]); }\n inline pair<int, int> Up() { return (make_pair(ParIndex, ParDepth)); }\n };\n vector<vector<int>> graph;\n vector<int> SubTreeSize, NextPath;\n vector<int> TreeIndex, TreeDepth;\n vector<Centroid> Centroids;\n void BuildSubTreeSize() {\n stack<pair<int, int>> s;\n s.emplace(0, -1);\n while (!s.empty()) {\n auto p = s.top();\n s.pop();\n if (~SubTreeSize[p.first]) {\n NextPath[p.first] = -1;\n for (auto &to : graph[p.first]) {\n if (p.second == to)\n continue;\n SubTreeSize[p.first] += SubTreeSize[to];\n if (NextPath[p.first] == -1 ||\n SubTreeSize[NextPath[p.first]] < SubTreeSize[to]) {\n NextPath[p.first] = to;\n }\n }\n } else {\n s.push(p);\n SubTreeSize[p.first] = 1;\n for (auto &to : graph[p.first]) {\n if (p.second != to)\n s.emplace(to, p.first);\n }\n }\n }\n }\n void BuildPath() {\n stack<pair<int, int>> s;\n Centroids.emplace_back(-1, -1, 0);\n s.emplace(0, -1);\n TreeIndex[0] = 0;\n while (!s.empty()) {\n auto p = s.top();\n s.pop();\n TreeDepth[p.first] = (int)Centroids[TreeIndex[p.first]].size();\n for (auto &to : graph[p.first]) {\n if (p.second == to)\n continue;\n if (to == NextPath[p.first]) {\n TreeIndex[to] = TreeIndex[p.first];\n } else {\n TreeIndex[to] = (int)Centroids.size();\n Centroids.emplace_back(TreeIndex[p.first], TreeDepth[p.first],\n Centroids[TreeIndex[p.first]].Deep + 1);\n }\n s.emplace(to, p.first);\n }\n Centroids[TreeIndex[p.first]].node.emplace_back(p.first);\n }\n }\n void AddEdge(int x, int y) {\n graph[x].push_back(y);\n graph[y].push_back(x);\n }\n virtual void Build() {\n BuildSubTreeSize();\n BuildPath();\n }\n inline size_t size() { return (Centroids.size()); }\n inline pair<int, int> Information(int idx) {\n return (make_pair(TreeIndex[idx], TreeDepth[idx]));\n }\n inline Centroid &operator[](int k) { return (Centroids[k]); }\n inline int LCA(int a, int b) {\n int TreeIdxA, TreeDepthA, TreeIdxB, TreeDepthB;\n tie(TreeIdxA, TreeDepthA) = Information(a);\n tie(TreeIdxB, TreeDepthB) = Information(b);\n while (TreeIdxA != TreeIdxB) {\n if (Centroids[TreeIdxA].Deep > Centroids[TreeIdxB].Deep) {\n tie(TreeIdxA, TreeDepthA) = Centroids[TreeIdxA].Up();\n } else {\n tie(TreeIdxB, TreeDepthB) = Centroids[TreeIdxB].Up();\n }\n }\n if (TreeDepthA > TreeDepthB)\n swap(TreeDepthA, TreeDepthB);\n return (Centroids[TreeIdxA][TreeDepthA]);\n }\n inline virtual void query(int a, int b,\n const function<void(int, int, int)> &f) {\n int TreeIdxA, TreeDepthA, TreeIdxB, TreeDepthB;\n tie(TreeIdxA, TreeDepthA) = Information(a);\n tie(TreeIdxB, TreeDepthB) = Information(b);\n while (TreeIdxA != TreeIdxB) {\n if (Centroids[TreeIdxA].Deep > Centroids[TreeIdxB].Deep) {\n f(TreeIdxA, 0, TreeDepthA + 1);\n tie(TreeIdxA, TreeDepthA) = Centroids[TreeIdxA].Up();\n } else {\n f(TreeIdxB, 0, TreeDepthB + 1);\n tie(TreeIdxB, TreeDepthB) = Centroids[TreeIdxB].Up();\n }\n }\n if (TreeDepthA > TreeDepthB)\n swap(TreeDepthA, TreeDepthB);\n f(TreeIdxA, TreeDepthA, TreeDepthB + 1);\n }\n CentroidPathDecomposition(int SZ) {\n graph.resize(SZ);\n SubTreeSize.assign(SZ, -1);\n NextPath.resize(SZ);\n TreeIndex.resize(SZ);\n TreeDepth.resize(SZ);\n }\n};\nOrderedMultiSet<int> tree(5000000);\nOrderedMultiSet<int>::Node *nodes[100000];\nint N, Q, X[100000];\nvector<int> g[100000];\nint parent[100000];\nvoid dfs(int idx, int par, OrderedMultiSet<int>::Node *par_set) {\n parent[idx] = par;\n tree.insert_key(par_set, X[idx]);\n nodes[idx] = par_set;\n for (auto &to : g[idx])\n if (to != par)\n dfs(to, idx, par_set);\n}\nint main() {\n scanf(\"%d %d\", &N, &Q);\n CentroidPathDecomposition gg(N);\n vector<int> vs;\n for (int i = 0; i < N; i++) {\n scanf(\"%d\", &X[i]);\n vs.emplace_back(X[i]);\n }\n sort(begin(vs), end(vs));\n vs.erase(unique(begin(vs), end(vs)), end(vs));\n for (int i = 1; i < N; i++) {\n int x, y;\n scanf(\"%d %d\", &x, &y);\n --x, --y;\n g[x].emplace_back(y);\n g[y].emplace_back(x);\n gg.AddEdge(x, y);\n }\n dfs(0, -1, tree.makeset());\n gg.Build();\n for (int i = 0; i < Q; i++) {\n int x, y, z;\n scanf(\"%d %d %d\", &x, &y, &z);\n --x, --y;\n int lca = gg.LCA(x, y);\n int ng = -1, ok = (int)vs.size() - 1;\n auto sum = [&](int v) {\n int ret = 0;\n ret += tree.upper_bound(nodes[x], vs[v]);\n ret += tree.upper_bound(nodes[y], vs[v]);\n ret -= tree.upper_bound(nodes[lca], vs[v]);\n if (lca)\n ret -= tree.upper_bound(nodes[parent[lca]], vs[v]);\n return ret;\n };\n while (ok - ng > 1) {\n int mid = (ok + ng) >> 1;\n if (sum(mid) >= z)\n ok = mid;\n else\n ng = mid;\n }\n printf(\"%d\\n\", vs[ok]);\n }\n}", "accuracy": 1, "time_ms": 850, "memory_kb": 133116, "score_of_the_acc": -1.2636, "final_rank": 11 }, { "submission_id": "aoj_2270_8113360", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntemplate <class Key> struct RandomizedBinarySearchTree {\n inline int xor128() {\n static int x = 123456789;\n static int y = 362436069;\n static int z = 521288629;\n static int w = 88675123;\n int t;\n t = x ^ (x << 11);\n x = y;\n y = z;\n z = w;\n return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));\n }\n struct Node {\n Node *l, *r;\n int cnt;\n Key key;\n Node() {}\n Node(const Key &k) : cnt(1), key(k), l(nullptr), r(nullptr) {}\n };\n vector<Node> pool;\n int ptr;\n RandomizedBinarySearchTree(int sz) : pool(sz), ptr(0) {}\n inline Node *alloc(const Key &key) { return &(pool[ptr++] = Node(key)); }\n virtual Node *clone(Node *t) { return t; }\n inline int count(const Node *t) { return t ? t->cnt : 0; }\n inline Node *update(Node *t) {\n t->cnt = count(t->l) + count(t->r) + 1;\n return t;\n }\n Node *propagete(Node *t) { return update(clone(t)); }\n Node *merge(Node *l, Node *r) {\n if (!l || !r)\n return l ? l : r;\n if (xor128() % (l->cnt + r->cnt) > l->cnt) {\n l = propagete(l);\n l->r = merge(l->r, r);\n return update(l);\n } else {\n r = propagete(r);\n r->l = merge(l, r->l);\n return update(r);\n }\n }\n pair<Node *, Node *> split(Node *t, int k) {\n if (!t)\n return {t, t};\n t = propagete(t);\n if (k <= count(t->l)) {\n auto s = split(t->l, k);\n t->l = s.second;\n return {s.first, update(t)};\n } else {\n auto s = split(t->r, k - count(t->l) - 1);\n t->r = s.first;\n return {update(t), s.second};\n }\n }\n Node *build(int l, int r, const vector<Key> &v) {\n if (l + 1 >= r)\n return alloc(v[l]);\n return merge(build(l, (l + r) >> 1, v), build((l + r) >> 1, r, v));\n }\n Node *build(const vector<Key> &v) {\n ptr = 0;\n return build(0, (int)v.size(), v);\n }\n void dump(Node *r, typename vector<Key>::iterator &it) {\n if (!r)\n return;\n r = propagete(r);\n dump(r->l, it);\n *it = r->key;\n dump(r->r, ++it);\n }\n vector<Key> dump(Node *r) {\n vector<Key> v((size_t)count(r));\n auto it = begin(v);\n dump(r, it);\n return v;\n }\n string to_string(Node *r) {\n auto s = dump(r);\n string ret;\n for (int i = 0; i < s.size(); i++)\n ret += \", \";\n return (ret);\n }\n void insert(Node *&t, int k, const Key &v) {\n auto x = split(t, k);\n t = merge(merge(x.first, alloc(v)), x.second);\n }\n void erase(Node *&t, int k) {\n auto x = split(t, k);\n t = merge(x.first, split(x.second, 1).second);\n }\n int size(Node *t) { return count(t); }\n bool empty(Node *t) { return !t; }\n Node *makeset() { return (nullptr); }\n};\ntemplate <class Key>\nstruct PresidentRandomizedBinarySearchTree : RandomizedBinarySearchTree<Key> {\n using RBST = RandomizedBinarySearchTree<Key>;\n using Node = typename RBST::Node;\n PresidentRandomizedBinarySearchTree(int sz) : RBST(sz) {}\n Node *clone(Node *t) override { return &(RBST::pool[RBST::ptr++] = *t); }\n Node *rebuild(Node *r) { return RBST::build(RBST::dump(r)); }\n};\ntemplate <class T>\nstruct OrderedMultiSet : PresidentRandomizedBinarySearchTree<T> {\n using RBST = PresidentRandomizedBinarySearchTree<T>;\n using Node = typename RBST::Node;\n OrderedMultiSet(int sz) : RBST(sz) {}\n T kth_element(Node *t, int k) {\n if (k < RBST::count(t->l))\n return kth_element(t->l, k);\n if (k == RBST::count(t->l))\n return t->key;\n return kth_element(t->r, k - RBST::count(t->l) - 1);\n }\n virtual void insert_key(Node *&t, const T &x) {\n RBST::insert(t, lower_bound(t, x), x);\n }\n void erase_key(Node *&t, const T &x) {\n if (!count(t, x))\n return;\n RBST::erase(t, lower_bound(t, x));\n }\n int count(Node *t, const T &x) {\n return upper_bound(t, x) - lower_bound(t, x);\n }\n int lower_bound(Node *t, const T &x) {\n if (!t)\n return 0;\n if (x <= t->key)\n return lower_bound(t->l, x);\n return lower_bound(t->r, x) + RBST::count(t->l) + 1;\n }\n int upper_bound(Node *t, const T &x) {\n if (!t)\n return 0;\n if (x < t->key)\n return upper_bound(t->l, x);\n return upper_bound(t->r, x) + RBST::count(t->l) + 1;\n }\n};\nstruct CentroidPathDecomposition {\n struct Centroid {\n int ParIndex, ParDepth, Deep;\n vector<int> node;\n Centroid(int idx, int dep, int deep)\n : ParIndex(idx), ParDepth(dep), Deep(deep) {}\n inline size_t size() { return (node.size()); }\n inline int &operator[](int k) { return (node[k]); }\n inline pair<int, int> Up() { return (make_pair(ParIndex, ParDepth)); }\n };\n vector<vector<int>> graph;\n vector<int> SubTreeSize, NextPath;\n vector<int> TreeIndex, TreeDepth;\n vector<Centroid> Centroids;\n void BuildSubTreeSize() {\n stack<pair<int, int>> s;\n s.emplace(0, -1);\n while (!s.empty()) {\n auto p = s.top();\n s.pop();\n if (~SubTreeSize[p.first]) {\n NextPath[p.first] = -1;\n for (auto &to : graph[p.first]) {\n if (p.second == to)\n continue;\n SubTreeSize[p.first] += SubTreeSize[to];\n if (NextPath[p.first] == -1 ||\n SubTreeSize[NextPath[p.first]] < SubTreeSize[to]) {\n NextPath[p.first] = to;\n }\n }\n } else {\n s.push(p);\n SubTreeSize[p.first] = 1;\n for (auto &to : graph[p.first]) {\n if (p.second != to)\n s.emplace(to, p.first);\n }\n }\n }\n }\n void BuildPath() {\n stack<pair<int, int>> s;\n Centroids.emplace_back(-1, -1, 0);\n s.emplace(0, -1);\n TreeIndex[0] = 0;\n while (!s.empty()) {\n auto p = s.top();\n s.pop();\n TreeDepth[p.first] = (int)Centroids[TreeIndex[p.first]].size();\n for (auto &to : graph[p.first]) {\n if (p.second == to)\n continue;\n if (to == NextPath[p.first]) {\n TreeIndex[to] = TreeIndex[p.first];\n } else {\n TreeIndex[to] = (int)Centroids.size();\n Centroids.emplace_back(TreeIndex[p.first], TreeDepth[p.first],\n Centroids[TreeIndex[p.first]].Deep + 1);\n }\n s.emplace(to, p.first);\n }\n Centroids[TreeIndex[p.first]].node.emplace_back(p.first);\n }\n }\n void AddEdge(int x, int y) {\n graph[x].push_back(y);\n graph[y].push_back(x);\n }\n virtual void Build() {\n BuildSubTreeSize();\n BuildPath();\n }\n inline size_t size() { return (Centroids.size()); }\n inline pair<int, int> Information(int idx) {\n return (make_pair(TreeIndex[idx], TreeDepth[idx]));\n }\n inline Centroid &operator[](int k) { return (Centroids[k]); }\n inline int LCA(int a, int b) {\n int TreeIdxA, TreeDepthA, TreeIdxB, TreeDepthB;\n tie(TreeIdxA, TreeDepthA) = Information(a);\n tie(TreeIdxB, TreeDepthB) = Information(b);\n while (TreeIdxA != TreeIdxB) {\n if (Centroids[TreeIdxA].Deep > Centroids[TreeIdxB].Deep) {\n tie(TreeIdxA, TreeDepthA) = Centroids[TreeIdxA].Up();\n } else {\n tie(TreeIdxB, TreeDepthB) = Centroids[TreeIdxB].Up();\n }\n }\n if (TreeDepthA > TreeDepthB)\n swap(TreeDepthA, TreeDepthB);\n return (Centroids[TreeIdxA][TreeDepthA]);\n }\n inline virtual void query(int a, int b,\n const function<void(int, int, int)> &f) {\n int TreeIdxA, TreeDepthA, TreeIdxB, TreeDepthB;\n tie(TreeIdxA, TreeDepthA) = Information(a);\n tie(TreeIdxB, TreeDepthB) = Information(b);\n while (TreeIdxA != TreeIdxB) {\n if (Centroids[TreeIdxA].Deep > Centroids[TreeIdxB].Deep) {\n f(TreeIdxA, 0, TreeDepthA + 1);\n tie(TreeIdxA, TreeDepthA) = Centroids[TreeIdxA].Up();\n } else {\n f(TreeIdxB, 0, TreeDepthB + 1);\n tie(TreeIdxB, TreeDepthB) = Centroids[TreeIdxB].Up();\n }\n }\n if (TreeDepthA > TreeDepthB)\n swap(TreeDepthA, TreeDepthB);\n f(TreeIdxA, TreeDepthA, TreeDepthB + 1);\n }\n CentroidPathDecomposition(int SZ) {\n graph.resize(SZ);\n SubTreeSize.assign(SZ, -1);\n NextPath.resize(SZ);\n TreeIndex.resize(SZ);\n TreeDepth.resize(SZ);\n }\n};\nOrderedMultiSet<int> tree(5000000);\nOrderedMultiSet<int>::Node *nodes[100000];\nint N, Q, X[100000];\nvector<int> g[100000];\nint parent[100000];\nvoid dfs(int idx, int par, OrderedMultiSet<int>::Node *par_set) {\n parent[idx] = par;\n tree.insert_key(par_set, X[idx]);\n nodes[idx] = par_set;\n for (auto &to : g[idx])\n if (to != par)\n dfs(to, idx, par_set);\n}\nint main() {\n scanf(\"%d %d\", &N, &Q);\n CentroidPathDecomposition gg(N);\n vector<int> vs;\n for (int i = 0; i < N; i++) {\n scanf(\"%d\", &X[i]);\n vs.emplace_back(X[i]);\n }\n sort(begin(vs), end(vs));\n vs.erase(unique(begin(vs), end(vs)), end(vs));\n for (int i = 1; i < N; i++) {\n int x, y;\n scanf(\"%d %d\", &x, &y);\n --x, --y;\n g[x].emplace_back(y);\n g[y].emplace_back(x);\n gg.AddEdge(x, y);\n }\n dfs(0, -1, tree.makeset());\n gg.Build();\n for (int i = 0; i < Q; i++) {\n int x, y, z;\n scanf(\"%d %d %d\", &x, &y, &z);\n --x, --y;\n int lca = gg.LCA(x, y);\n int ng = -1, ok = (int)vs.size() - 1;\n auto sum = [&](int v) {\n int ret = 0;\n ret += tree.upper_bound(nodes[x], vs[v]);\n ret += tree.upper_bound(nodes[y], vs[v]);\n ret -= tree.upper_bound(nodes[lca], vs[v]);\n if (lca)\n ret -= tree.upper_bound(nodes[parent[lca]], vs[v]);\n return ret;\n };\n while (ok - ng > 1) {\n int mid = (ok + ng) >> 1;\n if (sum(mid) >= z)\n ok = mid;\n else\n ng = mid;\n }\n printf(\"%d\\n\", vs[ok]);\n }\n}", "accuracy": 1, "time_ms": 1080, "memory_kb": 135224, "score_of_the_acc": -1.3614, "final_rank": 17 }, { "submission_id": "aoj_2270_8113359", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntemplate <class Key> struct RandomizedBinarySearchTree {\n inline int xor128() {\n static int x = 123456789;\n static int y = 362436069;\n static int z = 521288629;\n static int w = 88675123;\n int t;\n t = x ^ (x << 11);\n x = y;\n y = z;\n z = w;\n return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));\n }\n struct Node {\n Node *l, *r;\n int cnt;\n Key key;\n Node() {}\n Node(const Key &k) : cnt(1), key(k), l(nullptr), r(nullptr) {}\n };\n vector<Node> pool;\n int ptr;\n RandomizedBinarySearchTree(int sz) : pool(sz), ptr(0) {}\n inline Node *alloc(const Key &key) { return &(pool[ptr++] = Node(key)); }\n virtual Node *clone(Node *t) { return t; }\n inline int count(const Node *t) { return t ? t->cnt : 0; }\n inline Node *update(Node *t) {\n t->cnt = count(t->l) + count(t->r) + 1;\n return t;\n }\n Node *propagete(Node *t) { return update(clone(t)); }\n Node *merge(Node *l, Node *r) {\n if (!l || !r)\n return l ? l : r;\n if (xor128() % (r->cnt + l->cnt) < r->cnt) {\n l = propagete(l);\n l->r = merge(l->r, r);\n return update(l);\n } else {\n r = propagete(r);\n r->l = merge(l, r->l);\n return update(r);\n }\n }\n pair<Node *, Node *> split(Node *t, int k) {\n if (!t)\n return {t, t};\n t = propagete(t);\n if (k <= count(t->l)) {\n auto s = split(t->l, k);\n t->l = s.second;\n return {s.first, update(t)};\n } else {\n auto s = split(t->r, k - count(t->l) - 1);\n t->r = s.first;\n return {update(t), s.second};\n }\n }\n Node *build(int l, int r, const vector<Key> &v) {\n if (l + 1 >= r)\n return alloc(v[l]);\n return merge(build(l, (l + r) >> 1, v), build((l + r) >> 1, r, v));\n }\n Node *build(const vector<Key> &v) {\n ptr = 0;\n return build(0, (int)v.size(), v);\n }\n void dump(Node *r, typename vector<Key>::iterator &it) {\n if (!r)\n return;\n r = propagete(r);\n dump(r->l, it);\n *it = r->key;\n dump(r->r, ++it);\n }\n vector<Key> dump(Node *r) {\n vector<Key> v((size_t)count(r));\n auto it = begin(v);\n dump(r, it);\n return v;\n }\n string to_string(Node *r) {\n auto s = dump(r);\n string ret;\n for (int i = 0; i < s.size(); i++)\n ret += \", \";\n return (ret);\n }\n void insert(Node *&t, int k, const Key &v) {\n auto x = split(t, k);\n t = merge(merge(x.first, alloc(v)), x.second);\n }\n void erase(Node *&t, int k) {\n auto x = split(t, k);\n t = merge(x.first, split(x.second, 1).second);\n }\n int size(Node *t) { return count(t); }\n bool empty(Node *t) { return !t; }\n Node *makeset() { return (nullptr); }\n};\ntemplate <class Key>\nstruct PresidentRandomizedBinarySearchTree : RandomizedBinarySearchTree<Key> {\n using RBST = RandomizedBinarySearchTree<Key>;\n using Node = typename RBST::Node;\n PresidentRandomizedBinarySearchTree(int sz) : RBST(sz) {}\n Node *clone(Node *t) override { return &(RBST::pool[RBST::ptr++] = *t); }\n Node *rebuild(Node *r) { return RBST::build(RBST::dump(r)); }\n};\ntemplate <class T>\nstruct OrderedMultiSet : PresidentRandomizedBinarySearchTree<T> {\n using RBST = PresidentRandomizedBinarySearchTree<T>;\n using Node = typename RBST::Node;\n OrderedMultiSet(int sz) : RBST(sz) {}\n T kth_element(Node *t, int k) {\n if (k < RBST::count(t->l))\n return kth_element(t->l, k);\n if (k == RBST::count(t->l))\n return t->key;\n return kth_element(t->r, k - RBST::count(t->l) - 1);\n }\n virtual void insert_key(Node *&t, const T &x) {\n RBST::insert(t, lower_bound(t, x), x);\n }\n void erase_key(Node *&t, const T &x) {\n if (!count(t, x))\n return;\n RBST::erase(t, lower_bound(t, x));\n }\n int count(Node *t, const T &x) {\n return upper_bound(t, x) - lower_bound(t, x);\n }\n int lower_bound(Node *t, const T &x) {\n if (!t)\n return 0;\n if (x <= t->key)\n return lower_bound(t->l, x);\n return lower_bound(t->r, x) + RBST::count(t->l) + 1;\n }\n int upper_bound(Node *t, const T &x) {\n if (!t)\n return 0;\n if (x < t->key)\n return upper_bound(t->l, x);\n return upper_bound(t->r, x) + RBST::count(t->l) + 1;\n }\n};\nstruct CentroidPathDecomposition {\n struct Centroid {\n int ParIndex, ParDepth, Deep;\n vector<int> node;\n Centroid(int idx, int dep, int deep)\n : ParIndex(idx), ParDepth(dep), Deep(deep) {}\n inline size_t size() { return (node.size()); }\n inline int &operator[](int k) { return (node[k]); }\n inline pair<int, int> Up() { return (make_pair(ParIndex, ParDepth)); }\n };\n vector<vector<int>> graph;\n vector<int> SubTreeSize, NextPath;\n vector<int> TreeIndex, TreeDepth;\n vector<Centroid> Centroids;\n void BuildSubTreeSize() {\n stack<pair<int, int>> s;\n s.emplace(0, -1);\n while (!s.empty()) {\n auto p = s.top();\n s.pop();\n if (~SubTreeSize[p.first]) {\n NextPath[p.first] = -1;\n for (auto &to : graph[p.first]) {\n if (p.second == to)\n continue;\n SubTreeSize[p.first] += SubTreeSize[to];\n if (NextPath[p.first] == -1 ||\n SubTreeSize[NextPath[p.first]] < SubTreeSize[to]) {\n NextPath[p.first] = to;\n }\n }\n } else {\n s.push(p);\n SubTreeSize[p.first] = 1;\n for (auto &to : graph[p.first]) {\n if (p.second != to)\n s.emplace(to, p.first);\n }\n }\n }\n }\n void BuildPath() {\n stack<pair<int, int>> s;\n Centroids.emplace_back(-1, -1, 0);\n s.emplace(0, -1);\n TreeIndex[0] = 0;\n while (!s.empty()) {\n auto p = s.top();\n s.pop();\n TreeDepth[p.first] = (int)Centroids[TreeIndex[p.first]].size();\n for (auto &to : graph[p.first]) {\n if (p.second == to)\n continue;\n if (to == NextPath[p.first]) {\n TreeIndex[to] = TreeIndex[p.first];\n } else {\n TreeIndex[to] = (int)Centroids.size();\n Centroids.emplace_back(TreeIndex[p.first], TreeDepth[p.first],\n Centroids[TreeIndex[p.first]].Deep + 1);\n }\n s.emplace(to, p.first);\n }\n Centroids[TreeIndex[p.first]].node.emplace_back(p.first);\n }\n }\n void AddEdge(int x, int y) {\n graph[x].push_back(y);\n graph[y].push_back(x);\n }\n virtual void Build() {\n BuildSubTreeSize();\n BuildPath();\n }\n inline size_t size() { return (Centroids.size()); }\n inline pair<int, int> Information(int idx) {\n return (make_pair(TreeIndex[idx], TreeDepth[idx]));\n }\n inline Centroid &operator[](int k) { return (Centroids[k]); }\n inline int LCA(int a, int b) {\n int TreeIdxA, TreeDepthA, TreeIdxB, TreeDepthB;\n tie(TreeIdxA, TreeDepthA) = Information(a);\n tie(TreeIdxB, TreeDepthB) = Information(b);\n while (TreeIdxA != TreeIdxB) {\n if (Centroids[TreeIdxA].Deep > Centroids[TreeIdxB].Deep) {\n tie(TreeIdxA, TreeDepthA) = Centroids[TreeIdxA].Up();\n } else {\n tie(TreeIdxB, TreeDepthB) = Centroids[TreeIdxB].Up();\n }\n }\n if (TreeDepthA > TreeDepthB)\n swap(TreeDepthA, TreeDepthB);\n return (Centroids[TreeIdxA][TreeDepthA]);\n }\n inline virtual void query(int a, int b,\n const function<void(int, int, int)> &f) {\n int TreeIdxA, TreeDepthA, TreeIdxB, TreeDepthB;\n tie(TreeIdxA, TreeDepthA) = Information(a);\n tie(TreeIdxB, TreeDepthB) = Information(b);\n while (TreeIdxA != TreeIdxB) {\n if (Centroids[TreeIdxA].Deep > Centroids[TreeIdxB].Deep) {\n f(TreeIdxA, 0, TreeDepthA + 1);\n tie(TreeIdxA, TreeDepthA) = Centroids[TreeIdxA].Up();\n } else {\n f(TreeIdxB, 0, TreeDepthB + 1);\n tie(TreeIdxB, TreeDepthB) = Centroids[TreeIdxB].Up();\n }\n }\n if (TreeDepthA > TreeDepthB)\n swap(TreeDepthA, TreeDepthB);\n f(TreeIdxA, TreeDepthA, TreeDepthB + 1);\n }\n CentroidPathDecomposition(int SZ) {\n graph.resize(SZ);\n SubTreeSize.assign(SZ, -1);\n NextPath.resize(SZ);\n TreeIndex.resize(SZ);\n TreeDepth.resize(SZ);\n }\n};\nOrderedMultiSet<int> tree(5000000);\nOrderedMultiSet<int>::Node *nodes[100000];\nint N, Q, X[100000];\nvector<int> g[100000];\nint parent[100000];\nvoid dfs(int idx, int par, OrderedMultiSet<int>::Node *par_set) {\n parent[idx] = par;\n tree.insert_key(par_set, X[idx]);\n nodes[idx] = par_set;\n for (auto &to : g[idx])\n if (to != par)\n dfs(to, idx, par_set);\n}\nint main() {\n scanf(\"%d %d\", &N, &Q);\n CentroidPathDecomposition gg(N);\n vector<int> vs;\n for (int i = 0; i < N; i++) {\n scanf(\"%d\", &X[i]);\n vs.emplace_back(X[i]);\n }\n sort(begin(vs), end(vs));\n vs.erase(unique(begin(vs), end(vs)), end(vs));\n for (int i = 1; i < N; i++) {\n int x, y;\n scanf(\"%d %d\", &x, &y);\n --x, --y;\n g[x].emplace_back(y);\n g[y].emplace_back(x);\n gg.AddEdge(x, y);\n }\n dfs(0, -1, tree.makeset());\n gg.Build();\n for (int i = 0; i < Q; i++) {\n int x, y, z;\n scanf(\"%d %d %d\", &x, &y, &z);\n --x, --y;\n int lca = gg.LCA(x, y);\n int ng = -1, ok = (int)vs.size() - 1;\n auto sum = [&](int v) {\n int ret = 0;\n ret += tree.upper_bound(nodes[x], vs[v]);\n ret += tree.upper_bound(nodes[y], vs[v]);\n ret -= tree.upper_bound(nodes[lca], vs[v]);\n if (lca)\n ret -= tree.upper_bound(nodes[parent[lca]], vs[v]);\n return ret;\n };\n while (ok - ng > 1) {\n int mid = (ok + ng) >> 1;\n if (sum(mid) >= z)\n ok = mid;\n else\n ng = mid;\n }\n printf(\"%d\\n\", vs[ok]);\n }\n}", "accuracy": 1, "time_ms": 1040, "memory_kb": 135488, "score_of_the_acc": -1.3491, "final_rank": 15 }, { "submission_id": "aoj_2270_8113357", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntemplate <class Key> struct RandomizedBinarySearchTree {\n inline int xor128() {\n static int x = 123456789;\n static int y = 362436069;\n static int z = 521288629;\n static int w = 88675123;\n int t;\n t = x ^ (x << 11);\n x = y;\n y = z;\n z = w;\n return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));\n }\n struct Node {\n Node *l, *r;\n int cnt;\n Key key;\n Node() {}\n Node(const Key &k) : cnt(1), key(k), l(nullptr), r(nullptr) {}\n };\n vector<Node> pool;\n int ptr;\n RandomizedBinarySearchTree(int sz) : pool(sz), ptr(0) {}\n inline Node *alloc(const Key &key) { return &(pool[ptr++] = Node(key)); }\n virtual Node *clone(Node *t) { return t; }\n inline int count(const Node *t) { return t ? t->cnt : 0; }\n inline Node *update(Node *t) {\n t->cnt = count(t->l) + count(t->r) + 1;\n return t;\n }\n Node *propagete(Node *t) { return update(clone(t)); }\n Node *merge(Node *l, Node *r) {\n if (!l || !r)\n return l ? l : r;\n if (xor128() % (l->cnt + r->cnt) < r->cnt) {\n l = propagete(l);\n l->r = merge(l->r, r);\n return update(l);\n } else {\n r = propagete(r);\n r->l = merge(l, r->l);\n return update(r);\n }\n }\n pair<Node *, Node *> split(Node *t, int k) {\n if (!t)\n return {t, t};\n t = propagete(t);\n if (k <= count(t->l)) {\n auto s = split(t->l, k);\n t->l = s.second;\n return {s.first, update(t)};\n } else {\n auto s = split(t->r, k - count(t->l) - 1);\n t->r = s.first;\n return {update(t), s.second};\n }\n }\n Node *build(int l, int r, const vector<Key> &v) {\n if (l + 1 >= r)\n return alloc(v[l]);\n return merge(build(l, (l + r) >> 1, v), build((l + r) >> 1, r, v));\n }\n Node *build(const vector<Key> &v) {\n ptr = 0;\n return build(0, (int)v.size(), v);\n }\n void dump(Node *r, typename vector<Key>::iterator &it) {\n if (!r)\n return;\n r = propagete(r);\n dump(r->l, it);\n *it = r->key;\n dump(r->r, ++it);\n }\n vector<Key> dump(Node *r) {\n vector<Key> v((size_t)count(r));\n auto it = begin(v);\n dump(r, it);\n return v;\n }\n string to_string(Node *r) {\n auto s = dump(r);\n string ret;\n for (int i = 0; i < s.size(); i++)\n ret += \", \";\n return (ret);\n }\n void insert(Node *&t, int k, const Key &v) {\n auto x = split(t, k);\n t = merge(merge(x.first, alloc(v)), x.second);\n }\n void erase(Node *&t, int k) {\n auto x = split(t, k);\n t = merge(x.first, split(x.second, 1).second);\n }\n int size(Node *t) { return count(t); }\n bool empty(Node *t) { return !t; }\n Node *makeset() { return (nullptr); }\n};\ntemplate <class Key>\nstruct PresidentRandomizedBinarySearchTree : RandomizedBinarySearchTree<Key> {\n using RBST = RandomizedBinarySearchTree<Key>;\n using Node = typename RBST::Node;\n PresidentRandomizedBinarySearchTree(int sz) : RBST(sz) {}\n Node *clone(Node *t) override { return &(RBST::pool[RBST::ptr++] = *t); }\n Node *rebuild(Node *r) { return RBST::build(RBST::dump(r)); }\n};\ntemplate <class T>\nstruct OrderedMultiSet : PresidentRandomizedBinarySearchTree<T> {\n using RBST = PresidentRandomizedBinarySearchTree<T>;\n using Node = typename RBST::Node;\n OrderedMultiSet(int sz) : RBST(sz) {}\n T kth_element(Node *t, int k) {\n if (k < RBST::count(t->l))\n return kth_element(t->l, k);\n if (k == RBST::count(t->l))\n return t->key;\n return kth_element(t->r, k - RBST::count(t->l) - 1);\n }\n virtual void insert_key(Node *&t, const T &x) {\n RBST::insert(t, lower_bound(t, x), x);\n }\n void erase_key(Node *&t, const T &x) {\n if (!count(t, x))\n return;\n RBST::erase(t, lower_bound(t, x));\n }\n int count(Node *t, const T &x) {\n return upper_bound(t, x) - lower_bound(t, x);\n }\n int lower_bound(Node *t, const T &x) {\n if (!t)\n return 0;\n if (x <= t->key)\n return lower_bound(t->l, x);\n return lower_bound(t->r, x) + RBST::count(t->l) + 1;\n }\n int upper_bound(Node *t, const T &x) {\n if (!t)\n return 0;\n if (x < t->key)\n return upper_bound(t->l, x);\n return upper_bound(t->r, x) + RBST::count(t->l) + 1;\n }\n};\nstruct CentroidPathDecomposition {\n struct Centroid {\n int ParIndex, ParDepth, Deep;\n vector<int> node;\n Centroid(int idx, int dep, int deep)\n : ParIndex(idx), ParDepth(dep), Deep(deep) {}\n inline size_t size() { return (node.size()); }\n inline int &operator[](int k) { return (node[k]); }\n inline pair<int, int> Up() { return (make_pair(ParIndex, ParDepth)); }\n };\n vector<vector<int>> graph;\n vector<int> SubTreeSize, NextPath;\n vector<int> TreeIndex, TreeDepth;\n vector<Centroid> Centroids;\n void BuildSubTreeSize() {\n stack<pair<int, int>> s;\n s.emplace(0, -1);\n while (!s.empty()) {\n auto p = s.top();\n s.pop();\n if (~SubTreeSize[p.first]) {\n NextPath[p.first] = -1;\n for (auto &to : graph[p.first]) {\n if (p.second == to)\n continue;\n SubTreeSize[p.first] += SubTreeSize[to];\n if (NextPath[p.first] == -1 ||\n SubTreeSize[NextPath[p.first]] < SubTreeSize[to]) {\n NextPath[p.first] = to;\n }\n }\n } else {\n s.push(p);\n SubTreeSize[p.first] = 1;\n for (auto &to : graph[p.first]) {\n if (p.second != to)\n s.emplace(to, p.first);\n }\n }\n }\n }\n void BuildPath() {\n stack<pair<int, int>> s;\n Centroids.emplace_back(-1, -1, 0);\n s.emplace(0, -1);\n TreeIndex[0] = 0;\n while (!s.empty()) {\n auto p = s.top();\n s.pop();\n TreeDepth[p.first] = (int)Centroids[TreeIndex[p.first]].size();\n for (auto &to : graph[p.first]) {\n if (p.second == to)\n continue;\n if (to == NextPath[p.first]) {\n TreeIndex[to] = TreeIndex[p.first];\n } else {\n TreeIndex[to] = (int)Centroids.size();\n Centroids.emplace_back(TreeIndex[p.first], TreeDepth[p.first],\n Centroids[TreeIndex[p.first]].Deep + 1);\n }\n s.emplace(to, p.first);\n }\n Centroids[TreeIndex[p.first]].node.emplace_back(p.first);\n }\n }\n void AddEdge(int x, int y) {\n graph[x].push_back(y);\n graph[y].push_back(x);\n }\n virtual void Build() {\n BuildSubTreeSize();\n BuildPath();\n }\n inline size_t size() { return (Centroids.size()); }\n inline pair<int, int> Information(int idx) {\n return (make_pair(TreeIndex[idx], TreeDepth[idx]));\n }\n inline Centroid &operator[](int k) { return (Centroids[k]); }\n inline int LCA(int a, int b) {\n int TreeIdxA, TreeDepthA, TreeIdxB, TreeDepthB;\n tie(TreeIdxA, TreeDepthA) = Information(a);\n tie(TreeIdxB, TreeDepthB) = Information(b);\n while (TreeIdxA != TreeIdxB) {\n if (Centroids[TreeIdxA].Deep > Centroids[TreeIdxB].Deep) {\n tie(TreeIdxA, TreeDepthA) = Centroids[TreeIdxA].Up();\n } else {\n tie(TreeIdxB, TreeDepthB) = Centroids[TreeIdxB].Up();\n }\n }\n if (TreeDepthA > TreeDepthB)\n swap(TreeDepthA, TreeDepthB);\n return (Centroids[TreeIdxA][TreeDepthA]);\n }\n inline virtual void query(int a, int b,\n const function<void(int, int, int)> &f) {\n int TreeIdxA, TreeDepthA, TreeIdxB, TreeDepthB;\n tie(TreeIdxA, TreeDepthA) = Information(a);\n tie(TreeIdxB, TreeDepthB) = Information(b);\n while (TreeIdxA != TreeIdxB) {\n if (Centroids[TreeIdxA].Deep > Centroids[TreeIdxB].Deep) {\n f(TreeIdxA, 0, TreeDepthA + 1);\n tie(TreeIdxA, TreeDepthA) = Centroids[TreeIdxA].Up();\n } else {\n f(TreeIdxB, 0, TreeDepthB + 1);\n tie(TreeIdxB, TreeDepthB) = Centroids[TreeIdxB].Up();\n }\n }\n if (TreeDepthA > TreeDepthB)\n swap(TreeDepthA, TreeDepthB);\n f(TreeIdxA, TreeDepthA, TreeDepthB + 1);\n }\n CentroidPathDecomposition(int SZ) {\n graph.resize(SZ);\n SubTreeSize.assign(SZ, -1);\n NextPath.resize(SZ);\n TreeIndex.resize(SZ);\n TreeDepth.resize(SZ);\n }\n};\nOrderedMultiSet<int> tree(5000000);\nOrderedMultiSet<int>::Node *nodes[100000];\nint N, Q, X[100000];\nvector<int> g[100000];\nint parent[100000];\nvoid dfs(int idx, int par, OrderedMultiSet<int>::Node *par_set) {\n parent[idx] = par;\n tree.insert_key(par_set, X[idx]);\n nodes[idx] = par_set;\n for (auto &to : g[idx])\n if (to != par)\n dfs(to, idx, par_set);\n}\nint main() {\n scanf(\"%d %d\", &N, &Q);\n CentroidPathDecomposition gg(N);\n vector<int> vs;\n for (int i = 0; i < N; i++) {\n scanf(\"%d\", &X[i]);\n vs.emplace_back(X[i]);\n }\n sort(begin(vs), end(vs));\n vs.erase(unique(begin(vs), end(vs)), end(vs));\n for (int i = 1; i < N; i++) {\n int x, y;\n scanf(\"%d %d\", &x, &y);\n --x, --y;\n g[x].emplace_back(y);\n g[y].emplace_back(x);\n gg.AddEdge(x, y);\n }\n dfs(0, -1, tree.makeset());\n gg.Build();\n for (int i = 0; i < Q; i++) {\n int x, y, z;\n scanf(\"%d %d %d\", &x, &y, &z);\n --x, --y;\n int lca = gg.LCA(x, y);\n int ng = -1, ok = (int)vs.size() - 1;\n auto sum = [&](int v) {\n int ret = 0;\n ret += tree.upper_bound(nodes[x], vs[v]);\n ret += tree.upper_bound(nodes[y], vs[v]);\n ret -= tree.upper_bound(nodes[lca], vs[v]);\n if (lca)\n ret -= tree.upper_bound(nodes[parent[lca]], vs[v]);\n return ret;\n };\n while (ok - ng > 1) {\n int mid = (ok + ng) >> 1;\n if (sum(mid) >= z)\n ok = mid;\n else\n ng = mid;\n }\n printf(\"%d\\n\", vs[ok]);\n }\n}", "accuracy": 1, "time_ms": 1050, "memory_kb": 136032, "score_of_the_acc": -1.3567, "final_rank": 16 }, { "submission_id": "aoj_2270_8113355", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntemplate <class Key> struct RandomizedBinarySearchTree {\n inline int xor128() {\n static int x = 123456789;\n static int y = 362436069;\n static int z = 521288629;\n static int w = 88675123;\n int t;\n t = x ^ (x << 11);\n x = y;\n y = z;\n z = w;\n return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));\n }\n struct Node {\n Node *l, *r;\n int cnt;\n Key key;\n Node() {}\n Node(const Key &k) : cnt(1), key(k), l(nullptr), r(nullptr) {}\n };\n vector<Node> pool;\n int ptr;\n RandomizedBinarySearchTree(int sz) : pool(sz), ptr(0) {}\n inline Node *alloc(const Key &key) { return &(pool[ptr++] = Node(key)); }\n virtual Node *clone(Node *t) { return t; }\n inline int count(const Node *t) { return t ? t->cnt : 0; }\n inline Node *update(Node *t) {\n t->cnt = count(t->l) + count(t->r) + 1;\n return t;\n }\n Node *propagete(Node *t) { return update(clone(t)); }\n Node *merge(Node *l, Node *r) {\n if (!l || !r)\n return l ? l : r;\n if (xor128() % (l->cnt + r->cnt) != l->cnt) {\n l = propagete(l);\n l->r = merge(l->r, r);\n return update(l);\n } else {\n r = propagete(r);\n r->l = merge(l, r->l);\n return update(r);\n }\n }\n pair<Node *, Node *> split(Node *t, int k) {\n if (!t)\n return {t, t};\n t = propagete(t);\n if (k <= count(t->l)) {\n auto s = split(t->l, k);\n t->l = s.second;\n return {s.first, update(t)};\n } else {\n auto s = split(t->r, k - count(t->l) - 1);\n t->r = s.first;\n return {update(t), s.second};\n }\n }\n Node *build(int l, int r, const vector<Key> &v) {\n if (l + 1 >= r)\n return alloc(v[l]);\n return merge(build(l, (l + r) >> 1, v), build((l + r) >> 1, r, v));\n }\n Node *build(const vector<Key> &v) {\n ptr = 0;\n return build(0, (int)v.size(), v);\n }\n void dump(Node *r, typename vector<Key>::iterator &it) {\n if (!r)\n return;\n r = propagete(r);\n dump(r->l, it);\n *it = r->key;\n dump(r->r, ++it);\n }\n vector<Key> dump(Node *r) {\n vector<Key> v((size_t)count(r));\n auto it = begin(v);\n dump(r, it);\n return v;\n }\n string to_string(Node *r) {\n auto s = dump(r);\n string ret;\n for (int i = 0; i < s.size(); i++)\n ret += \", \";\n return (ret);\n }\n void insert(Node *&t, int k, const Key &v) {\n auto x = split(t, k);\n t = merge(merge(x.first, alloc(v)), x.second);\n }\n void erase(Node *&t, int k) {\n auto x = split(t, k);\n t = merge(x.first, split(x.second, 1).second);\n }\n int size(Node *t) { return count(t); }\n bool empty(Node *t) { return !t; }\n Node *makeset() { return (nullptr); }\n};\ntemplate <class Key>\nstruct PresidentRandomizedBinarySearchTree : RandomizedBinarySearchTree<Key> {\n using RBST = RandomizedBinarySearchTree<Key>;\n using Node = typename RBST::Node;\n PresidentRandomizedBinarySearchTree(int sz) : RBST(sz) {}\n Node *clone(Node *t) override { return &(RBST::pool[RBST::ptr++] = *t); }\n Node *rebuild(Node *r) { return RBST::build(RBST::dump(r)); }\n};\ntemplate <class T>\nstruct OrderedMultiSet : PresidentRandomizedBinarySearchTree<T> {\n using RBST = PresidentRandomizedBinarySearchTree<T>;\n using Node = typename RBST::Node;\n OrderedMultiSet(int sz) : RBST(sz) {}\n T kth_element(Node *t, int k) {\n if (k < RBST::count(t->l))\n return kth_element(t->l, k);\n if (k == RBST::count(t->l))\n return t->key;\n return kth_element(t->r, k - RBST::count(t->l) - 1);\n }\n virtual void insert_key(Node *&t, const T &x) {\n RBST::insert(t, lower_bound(t, x), x);\n }\n void erase_key(Node *&t, const T &x) {\n if (!count(t, x))\n return;\n RBST::erase(t, lower_bound(t, x));\n }\n int count(Node *t, const T &x) {\n return upper_bound(t, x) - lower_bound(t, x);\n }\n int lower_bound(Node *t, const T &x) {\n if (!t)\n return 0;\n if (x <= t->key)\n return lower_bound(t->l, x);\n return lower_bound(t->r, x) + RBST::count(t->l) + 1;\n }\n int upper_bound(Node *t, const T &x) {\n if (!t)\n return 0;\n if (x < t->key)\n return upper_bound(t->l, x);\n return upper_bound(t->r, x) + RBST::count(t->l) + 1;\n }\n};\nstruct CentroidPathDecomposition {\n struct Centroid {\n int ParIndex, ParDepth, Deep;\n vector<int> node;\n Centroid(int idx, int dep, int deep)\n : ParIndex(idx), ParDepth(dep), Deep(deep) {}\n inline size_t size() { return (node.size()); }\n inline int &operator[](int k) { return (node[k]); }\n inline pair<int, int> Up() { return (make_pair(ParIndex, ParDepth)); }\n };\n vector<vector<int>> graph;\n vector<int> SubTreeSize, NextPath;\n vector<int> TreeIndex, TreeDepth;\n vector<Centroid> Centroids;\n void BuildSubTreeSize() {\n stack<pair<int, int>> s;\n s.emplace(0, -1);\n while (!s.empty()) {\n auto p = s.top();\n s.pop();\n if (~SubTreeSize[p.first]) {\n NextPath[p.first] = -1;\n for (auto &to : graph[p.first]) {\n if (p.second == to)\n continue;\n SubTreeSize[p.first] += SubTreeSize[to];\n if (NextPath[p.first] == -1 ||\n SubTreeSize[NextPath[p.first]] < SubTreeSize[to]) {\n NextPath[p.first] = to;\n }\n }\n } else {\n s.push(p);\n SubTreeSize[p.first] = 1;\n for (auto &to : graph[p.first]) {\n if (p.second != to)\n s.emplace(to, p.first);\n }\n }\n }\n }\n void BuildPath() {\n stack<pair<int, int>> s;\n Centroids.emplace_back(-1, -1, 0);\n s.emplace(0, -1);\n TreeIndex[0] = 0;\n while (!s.empty()) {\n auto p = s.top();\n s.pop();\n TreeDepth[p.first] = (int)Centroids[TreeIndex[p.first]].size();\n for (auto &to : graph[p.first]) {\n if (p.second == to)\n continue;\n if (to == NextPath[p.first]) {\n TreeIndex[to] = TreeIndex[p.first];\n } else {\n TreeIndex[to] = (int)Centroids.size();\n Centroids.emplace_back(TreeIndex[p.first], TreeDepth[p.first],\n Centroids[TreeIndex[p.first]].Deep + 1);\n }\n s.emplace(to, p.first);\n }\n Centroids[TreeIndex[p.first]].node.emplace_back(p.first);\n }\n }\n void AddEdge(int x, int y) {\n graph[x].push_back(y);\n graph[y].push_back(x);\n }\n virtual void Build() {\n BuildSubTreeSize();\n BuildPath();\n }\n inline size_t size() { return (Centroids.size()); }\n inline pair<int, int> Information(int idx) {\n return (make_pair(TreeIndex[idx], TreeDepth[idx]));\n }\n inline Centroid &operator[](int k) { return (Centroids[k]); }\n inline int LCA(int a, int b) {\n int TreeIdxA, TreeDepthA, TreeIdxB, TreeDepthB;\n tie(TreeIdxA, TreeDepthA) = Information(a);\n tie(TreeIdxB, TreeDepthB) = Information(b);\n while (TreeIdxA != TreeIdxB) {\n if (Centroids[TreeIdxA].Deep > Centroids[TreeIdxB].Deep) {\n tie(TreeIdxA, TreeDepthA) = Centroids[TreeIdxA].Up();\n } else {\n tie(TreeIdxB, TreeDepthB) = Centroids[TreeIdxB].Up();\n }\n }\n if (TreeDepthA > TreeDepthB)\n swap(TreeDepthA, TreeDepthB);\n return (Centroids[TreeIdxA][TreeDepthA]);\n }\n inline virtual void query(int a, int b,\n const function<void(int, int, int)> &f) {\n int TreeIdxA, TreeDepthA, TreeIdxB, TreeDepthB;\n tie(TreeIdxA, TreeDepthA) = Information(a);\n tie(TreeIdxB, TreeDepthB) = Information(b);\n while (TreeIdxA != TreeIdxB) {\n if (Centroids[TreeIdxA].Deep > Centroids[TreeIdxB].Deep) {\n f(TreeIdxA, 0, TreeDepthA + 1);\n tie(TreeIdxA, TreeDepthA) = Centroids[TreeIdxA].Up();\n } else {\n f(TreeIdxB, 0, TreeDepthB + 1);\n tie(TreeIdxB, TreeDepthB) = Centroids[TreeIdxB].Up();\n }\n }\n if (TreeDepthA > TreeDepthB)\n swap(TreeDepthA, TreeDepthB);\n f(TreeIdxA, TreeDepthA, TreeDepthB + 1);\n }\n CentroidPathDecomposition(int SZ) {\n graph.resize(SZ);\n SubTreeSize.assign(SZ, -1);\n NextPath.resize(SZ);\n TreeIndex.resize(SZ);\n TreeDepth.resize(SZ);\n }\n};\nOrderedMultiSet<int> tree(5000000);\nOrderedMultiSet<int>::Node *nodes[100000];\nint N, Q, X[100000];\nvector<int> g[100000];\nint parent[100000];\nvoid dfs(int idx, int par, OrderedMultiSet<int>::Node *par_set) {\n parent[idx] = par;\n tree.insert_key(par_set, X[idx]);\n nodes[idx] = par_set;\n for (auto &to : g[idx])\n if (to != par)\n dfs(to, idx, par_set);\n}\nint main() {\n scanf(\"%d %d\", &N, &Q);\n CentroidPathDecomposition gg(N);\n vector<int> vs;\n for (int i = 0; i < N; i++) {\n scanf(\"%d\", &X[i]);\n vs.emplace_back(X[i]);\n }\n sort(begin(vs), end(vs));\n vs.erase(unique(begin(vs), end(vs)), end(vs));\n for (int i = 1; i < N; i++) {\n int x, y;\n scanf(\"%d %d\", &x, &y);\n --x, --y;\n g[x].emplace_back(y);\n g[y].emplace_back(x);\n gg.AddEdge(x, y);\n }\n dfs(0, -1, tree.makeset());\n gg.Build();\n for (int i = 0; i < Q; i++) {\n int x, y, z;\n scanf(\"%d %d %d\", &x, &y, &z);\n --x, --y;\n int lca = gg.LCA(x, y);\n int ng = -1, ok = (int)vs.size() - 1;\n auto sum = [&](int v) {\n int ret = 0;\n ret += tree.upper_bound(nodes[x], vs[v]);\n ret += tree.upper_bound(nodes[y], vs[v]);\n ret -= tree.upper_bound(nodes[lca], vs[v]);\n if (lca)\n ret -= tree.upper_bound(nodes[parent[lca]], vs[v]);\n return ret;\n };\n while (ok - ng > 1) {\n int mid = (ok + ng) >> 1;\n if (sum(mid) >= z)\n ok = mid;\n else\n ng = mid;\n }\n printf(\"%d\\n\", vs[ok]);\n }\n}", "accuracy": 0.7096774193548387, "time_ms": 380, "memory_kb": 107812, "score_of_the_acc": -0.9077, "final_rank": 18 }, { "submission_id": "aoj_2270_8113353", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntemplate <class Key> struct RandomizedBinarySearchTree {\n inline int xor128() {\n static int x = 123456789;\n static int y = 362436069;\n static int z = 521288629;\n static int w = 88675123;\n int t;\n t = x ^ (x << 11);\n x = y;\n y = z;\n z = w;\n return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));\n }\n struct Node {\n Node *l, *r;\n int cnt;\n Key key;\n Node() {}\n Node(const Key &k) : cnt(1), key(k), l(nullptr), r(nullptr) {}\n };\n vector<Node> pool;\n int ptr;\n RandomizedBinarySearchTree(int sz) : pool(sz), ptr(0) {}\n inline Node *alloc(const Key &key) { return &(pool[ptr++] = Node(key)); }\n virtual Node *clone(Node *t) { return t; }\n inline int count(const Node *t) { return t ? t->cnt : 0; }\n inline Node *update(Node *t) {\n t->cnt = count(t->l) + count(t->r) + 1;\n return t;\n }\n Node *propagete(Node *t) { return update(clone(t)); }\n Node *merge(Node *l, Node *r) {\n if (!l || !r)\n return l ? l : r;\n if ((xor128() % (l->cnt + r->cnt)) < l->cnt) {\n l = propagete(l);\n l->r = merge(l->r, r);\n return update(l);\n } else {\n r = propagete(r);\n r->l = merge(l, r->l);\n return update(r);\n }\n }\n pair<Node *, Node *> split(Node *t, int k) {\n if (!t)\n return {t, t};\n t = propagete(t);\n if (k <= count(t->l)) {\n auto s = split(t->l, k);\n t->l = s.second;\n return {s.first, update(t)};\n } else {\n auto s = split(t->r, k - count(t->l) - 1);\n t->r = s.first;\n return {update(t), s.second};\n }\n }\n Node *build(int l, int r, const vector<Key> &v) {\n if (l + 1 >= r)\n return alloc(v[l]);\n return merge(build(l, (l + r) >> 1, v), build((l + r) >> 1, r, v));\n }\n Node *build(const vector<Key> &v) {\n ptr = 0;\n return build(0, (int)v.size(), v);\n }\n void dump(Node *r, typename vector<Key>::iterator &it) {\n if (!r)\n return;\n r = propagete(r);\n dump(r->l, it);\n *it = r->key;\n dump(r->r, ++it);\n }\n vector<Key> dump(Node *r) {\n vector<Key> v((size_t)count(r));\n auto it = begin(v);\n dump(r, it);\n return v;\n }\n string to_string(Node *r) {\n auto s = dump(r);\n string ret;\n for (int i = 0; i < s.size(); i++)\n ret += \", \";\n return (ret);\n }\n void insert(Node *&t, int k, const Key &v) {\n auto x = split(t, k);\n t = merge(merge(x.first, alloc(v)), x.second);\n }\n void erase(Node *&t, int k) {\n auto x = split(t, k);\n t = merge(x.first, split(x.second, 1).second);\n }\n int size(Node *t) { return count(t); }\n bool empty(Node *t) { return !t; }\n Node *makeset() { return (nullptr); }\n};\ntemplate <class Key>\nstruct PresidentRandomizedBinarySearchTree : RandomizedBinarySearchTree<Key> {\n using RBST = RandomizedBinarySearchTree<Key>;\n using Node = typename RBST::Node;\n PresidentRandomizedBinarySearchTree(int sz) : RBST(sz) {}\n Node *clone(Node *t) override { return &(RBST::pool[RBST::ptr++] = *t); }\n Node *rebuild(Node *r) { return RBST::build(RBST::dump(r)); }\n};\ntemplate <class T>\nstruct OrderedMultiSet : PresidentRandomizedBinarySearchTree<T> {\n using RBST = PresidentRandomizedBinarySearchTree<T>;\n using Node = typename RBST::Node;\n OrderedMultiSet(int sz) : RBST(sz) {}\n T kth_element(Node *t, int k) {\n if (k < RBST::count(t->l))\n return kth_element(t->l, k);\n if (k == RBST::count(t->l))\n return t->key;\n return kth_element(t->r, k - RBST::count(t->l) - 1);\n }\n virtual void insert_key(Node *&t, const T &x) {\n RBST::insert(t, lower_bound(t, x), x);\n }\n void erase_key(Node *&t, const T &x) {\n if (!count(t, x))\n return;\n RBST::erase(t, lower_bound(t, x));\n }\n int count(Node *t, const T &x) {\n return upper_bound(t, x) - lower_bound(t, x);\n }\n int lower_bound(Node *t, const T &x) {\n if (!t)\n return 0;\n if (x <= t->key)\n return lower_bound(t->l, x);\n return lower_bound(t->r, x) + RBST::count(t->l) + 1;\n }\n int upper_bound(Node *t, const T &x) {\n if (!t)\n return 0;\n if (x < t->key)\n return upper_bound(t->l, x);\n return upper_bound(t->r, x) + RBST::count(t->l) + 1;\n }\n};\nstruct CentroidPathDecomposition {\n struct Centroid {\n int ParIndex, ParDepth, Deep;\n vector<int> node;\n Centroid(int idx, int dep, int deep)\n : ParIndex(idx), ParDepth(dep), Deep(deep) {}\n inline size_t size() { return (node.size()); }\n inline int &operator[](int k) { return (node[k]); }\n inline pair<int, int> Up() { return (make_pair(ParIndex, ParDepth)); }\n };\n vector<vector<int>> graph;\n vector<int> SubTreeSize, NextPath;\n vector<int> TreeIndex, TreeDepth;\n vector<Centroid> Centroids;\n void BuildSubTreeSize() {\n stack<pair<int, int>> s;\n s.emplace(0, -1);\n while (!s.empty()) {\n auto p = s.top();\n s.pop();\n if (~SubTreeSize[p.first]) {\n NextPath[p.first] = -1;\n for (auto &to : graph[p.first]) {\n if (p.second == to)\n continue;\n SubTreeSize[p.first] += SubTreeSize[to];\n if (NextPath[p.first] == -1 ||\n SubTreeSize[NextPath[p.first]] < SubTreeSize[to]) {\n NextPath[p.first] = to;\n }\n }\n } else {\n s.push(p);\n SubTreeSize[p.first] = 1;\n for (auto &to : graph[p.first]) {\n if (p.second != to)\n s.emplace(to, p.first);\n }\n }\n }\n }\n void BuildPath() {\n stack<pair<int, int>> s;\n Centroids.emplace_back(-1, -1, 0);\n s.emplace(0, -1);\n TreeIndex[0] = 0;\n while (!s.empty()) {\n auto p = s.top();\n s.pop();\n TreeDepth[p.first] = (int)Centroids[TreeIndex[p.first]].size();\n for (auto &to : graph[p.first]) {\n if (p.second == to)\n continue;\n if (to == NextPath[p.first]) {\n TreeIndex[to] = TreeIndex[p.first];\n } else {\n TreeIndex[to] = (int)Centroids.size();\n Centroids.emplace_back(TreeIndex[p.first], TreeDepth[p.first],\n Centroids[TreeIndex[p.first]].Deep + 1);\n }\n s.emplace(to, p.first);\n }\n Centroids[TreeIndex[p.first]].node.emplace_back(p.first);\n }\n }\n void AddEdge(int x, int y) {\n graph[x].push_back(y);\n graph[y].push_back(x);\n }\n virtual void Build() {\n BuildSubTreeSize();\n BuildPath();\n }\n inline size_t size() { return (Centroids.size()); }\n inline pair<int, int> Information(int idx) {\n return (make_pair(TreeIndex[idx], TreeDepth[idx]));\n }\n inline Centroid &operator[](int k) { return (Centroids[k]); }\n inline int LCA(int a, int b) {\n int TreeIdxA, TreeDepthA, TreeIdxB, TreeDepthB;\n tie(TreeIdxA, TreeDepthA) = Information(a);\n tie(TreeIdxB, TreeDepthB) = Information(b);\n while (TreeIdxA != TreeIdxB) {\n if (Centroids[TreeIdxA].Deep > Centroids[TreeIdxB].Deep) {\n tie(TreeIdxA, TreeDepthA) = Centroids[TreeIdxA].Up();\n } else {\n tie(TreeIdxB, TreeDepthB) = Centroids[TreeIdxB].Up();\n }\n }\n if (TreeDepthA > TreeDepthB)\n swap(TreeDepthA, TreeDepthB);\n return (Centroids[TreeIdxA][TreeDepthA]);\n }\n inline virtual void query(int a, int b,\n const function<void(int, int, int)> &f) {\n int TreeIdxA, TreeDepthA, TreeIdxB, TreeDepthB;\n tie(TreeIdxA, TreeDepthA) = Information(a);\n tie(TreeIdxB, TreeDepthB) = Information(b);\n while (TreeIdxA != TreeIdxB) {\n if (Centroids[TreeIdxA].Deep > Centroids[TreeIdxB].Deep) {\n f(TreeIdxA, 0, TreeDepthA + 1);\n tie(TreeIdxA, TreeDepthA) = Centroids[TreeIdxA].Up();\n } else {\n f(TreeIdxB, 0, TreeDepthB + 1);\n tie(TreeIdxB, TreeDepthB) = Centroids[TreeIdxB].Up();\n }\n }\n if (TreeDepthA > TreeDepthB)\n swap(TreeDepthA, TreeDepthB);\n f(TreeIdxA, TreeDepthA, TreeDepthB + 1);\n }\n CentroidPathDecomposition(int SZ) {\n graph.resize(SZ);\n SubTreeSize.assign(SZ, -1);\n NextPath.resize(SZ);\n TreeIndex.resize(SZ);\n TreeDepth.resize(SZ);\n }\n};\nOrderedMultiSet<int> tree(5000000);\nOrderedMultiSet<int>::Node *nodes[100000];\nint N, Q, X[100000];\nvector<int> g[100000];\nint parent[100000];\nvoid dfs(int idx, int par, OrderedMultiSet<int>::Node *par_set) {\n parent[idx] = par;\n tree.insert_key(par_set, X[idx]);\n nodes[idx] = par_set;\n for (auto &to : g[idx])\n if (to != par)\n dfs(to, idx, par_set);\n}\nint main() {\n scanf(\"%d %d\", &N, &Q);\n CentroidPathDecomposition gg(N);\n vector<int> vs;\n for (int i = 0; i < N; i++) {\n scanf(\"%d\", &X[i]);\n vs.emplace_back(X[i]);\n }\n sort(begin(vs), end(vs));\n vs.erase(unique(begin(vs), end(vs)), end(vs));\n for (int i = 1; i < N; i++) {\n int x, y;\n scanf(\"%d %d\", &x, &y);\n --x, --y;\n g[x].emplace_back(y);\n g[y].emplace_back(x);\n gg.AddEdge(x, y);\n }\n dfs(0, -1, tree.makeset());\n gg.Build();\n for (int i = 0; i < Q; i++) {\n int x, y, z;\n scanf(\"%d %d %d\", &x, &y, &z);\n --x, --y;\n int lca = gg.LCA(x, y);\n int ng = -1, ok = (int)vs.size() - 1;\n auto sum = [&](int v) {\n int ret = 0;\n ret += tree.upper_bound(nodes[x], vs[v]);\n ret += tree.upper_bound(nodes[y], vs[v]);\n ret -= tree.upper_bound(nodes[lca], vs[v]);\n if (lca)\n ret -= tree.upper_bound(nodes[parent[lca]], vs[v]);\n return ret;\n };\n while (ok - ng > 1) {\n int mid = (ok + ng) >> 1;\n if (sum(mid) >= z)\n ok = mid;\n else\n ng = mid;\n }\n printf(\"%d\\n\", vs[ok]);\n }\n}", "accuracy": 1, "time_ms": 860, "memory_kb": 133224, "score_of_the_acc": -1.268, "final_rank": 12 }, { "submission_id": "aoj_2270_8113351", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntemplate <class Key> struct RandomizedBinarySearchTree {\n inline int xor128() {\n static int x = 123456789;\n static int y = 362436069;\n static int z = 521288629;\n static int w = 88675123;\n int t;\n t = x ^ (x << 11);\n x = y;\n y = z;\n z = w;\n return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));\n }\n struct Node {\n Node *l, *r;\n int cnt;\n Key key;\n Node() {}\n Node(const Key &k) : cnt(1), key(k), l(nullptr), r(nullptr) {}\n };\n vector<Node> pool;\n int ptr;\n RandomizedBinarySearchTree(int sz) : pool(sz), ptr(0) {}\n inline Node *alloc(const Key &key) { return &(pool[ptr++] = Node(key)); }\n virtual Node *clone(Node *t) { return t; }\n inline int count(const Node *t) { return t ? t->cnt : 0; }\n inline Node *update(Node *t) {\n t->cnt = count(t->l) + count(t->r) + 1;\n return t;\n }\n Node *propagete(Node *t) { return update(clone(t)); }\n Node *merge(Node *l, Node *r) {\n if (!l || !r)\n return l ? l : r;\n if (xor128() % (l->cnt + r->cnt) == l->cnt) {\n l = propagete(l);\n l->r = merge(l->r, r);\n return update(l);\n } else {\n r = propagete(r);\n r->l = merge(l, r->l);\n return update(r);\n }\n }\n pair<Node *, Node *> split(Node *t, int k) {\n if (!t)\n return {t, t};\n t = propagete(t);\n if (k <= count(t->l)) {\n auto s = split(t->l, k);\n t->l = s.second;\n return {s.first, update(t)};\n } else {\n auto s = split(t->r, k - count(t->l) - 1);\n t->r = s.first;\n return {update(t), s.second};\n }\n }\n Node *build(int l, int r, const vector<Key> &v) {\n if (l + 1 >= r)\n return alloc(v[l]);\n return merge(build(l, (l + r) >> 1, v), build((l + r) >> 1, r, v));\n }\n Node *build(const vector<Key> &v) {\n ptr = 0;\n return build(0, (int)v.size(), v);\n }\n void dump(Node *r, typename vector<Key>::iterator &it) {\n if (!r)\n return;\n r = propagete(r);\n dump(r->l, it);\n *it = r->key;\n dump(r->r, ++it);\n }\n vector<Key> dump(Node *r) {\n vector<Key> v((size_t)count(r));\n auto it = begin(v);\n dump(r, it);\n return v;\n }\n string to_string(Node *r) {\n auto s = dump(r);\n string ret;\n for (int i = 0; i < s.size(); i++)\n ret += \", \";\n return (ret);\n }\n void insert(Node *&t, int k, const Key &v) {\n auto x = split(t, k);\n t = merge(merge(x.first, alloc(v)), x.second);\n }\n void erase(Node *&t, int k) {\n auto x = split(t, k);\n t = merge(x.first, split(x.second, 1).second);\n }\n int size(Node *t) { return count(t); }\n bool empty(Node *t) { return !t; }\n Node *makeset() { return (nullptr); }\n};\ntemplate <class Key>\nstruct PresidentRandomizedBinarySearchTree : RandomizedBinarySearchTree<Key> {\n using RBST = RandomizedBinarySearchTree<Key>;\n using Node = typename RBST::Node;\n PresidentRandomizedBinarySearchTree(int sz) : RBST(sz) {}\n Node *clone(Node *t) override { return &(RBST::pool[RBST::ptr++] = *t); }\n Node *rebuild(Node *r) { return RBST::build(RBST::dump(r)); }\n};\ntemplate <class T>\nstruct OrderedMultiSet : PresidentRandomizedBinarySearchTree<T> {\n using RBST = PresidentRandomizedBinarySearchTree<T>;\n using Node = typename RBST::Node;\n OrderedMultiSet(int sz) : RBST(sz) {}\n T kth_element(Node *t, int k) {\n if (k < RBST::count(t->l))\n return kth_element(t->l, k);\n if (k == RBST::count(t->l))\n return t->key;\n return kth_element(t->r, k - RBST::count(t->l) - 1);\n }\n virtual void insert_key(Node *&t, const T &x) {\n RBST::insert(t, lower_bound(t, x), x);\n }\n void erase_key(Node *&t, const T &x) {\n if (!count(t, x))\n return;\n RBST::erase(t, lower_bound(t, x));\n }\n int count(Node *t, const T &x) {\n return upper_bound(t, x) - lower_bound(t, x);\n }\n int lower_bound(Node *t, const T &x) {\n if (!t)\n return 0;\n if (x <= t->key)\n return lower_bound(t->l, x);\n return lower_bound(t->r, x) + RBST::count(t->l) + 1;\n }\n int upper_bound(Node *t, const T &x) {\n if (!t)\n return 0;\n if (x < t->key)\n return upper_bound(t->l, x);\n return upper_bound(t->r, x) + RBST::count(t->l) + 1;\n }\n};\nstruct CentroidPathDecomposition {\n struct Centroid {\n int ParIndex, ParDepth, Deep;\n vector<int> node;\n Centroid(int idx, int dep, int deep)\n : ParIndex(idx), ParDepth(dep), Deep(deep) {}\n inline size_t size() { return (node.size()); }\n inline int &operator[](int k) { return (node[k]); }\n inline pair<int, int> Up() { return (make_pair(ParIndex, ParDepth)); }\n };\n vector<vector<int>> graph;\n vector<int> SubTreeSize, NextPath;\n vector<int> TreeIndex, TreeDepth;\n vector<Centroid> Centroids;\n void BuildSubTreeSize() {\n stack<pair<int, int>> s;\n s.emplace(0, -1);\n while (!s.empty()) {\n auto p = s.top();\n s.pop();\n if (~SubTreeSize[p.first]) {\n NextPath[p.first] = -1;\n for (auto &to : graph[p.first]) {\n if (p.second == to)\n continue;\n SubTreeSize[p.first] += SubTreeSize[to];\n if (NextPath[p.first] == -1 ||\n SubTreeSize[NextPath[p.first]] < SubTreeSize[to]) {\n NextPath[p.first] = to;\n }\n }\n } else {\n s.push(p);\n SubTreeSize[p.first] = 1;\n for (auto &to : graph[p.first]) {\n if (p.second != to)\n s.emplace(to, p.first);\n }\n }\n }\n }\n void BuildPath() {\n stack<pair<int, int>> s;\n Centroids.emplace_back(-1, -1, 0);\n s.emplace(0, -1);\n TreeIndex[0] = 0;\n while (!s.empty()) {\n auto p = s.top();\n s.pop();\n TreeDepth[p.first] = (int)Centroids[TreeIndex[p.first]].size();\n for (auto &to : graph[p.first]) {\n if (p.second == to)\n continue;\n if (to == NextPath[p.first]) {\n TreeIndex[to] = TreeIndex[p.first];\n } else {\n TreeIndex[to] = (int)Centroids.size();\n Centroids.emplace_back(TreeIndex[p.first], TreeDepth[p.first],\n Centroids[TreeIndex[p.first]].Deep + 1);\n }\n s.emplace(to, p.first);\n }\n Centroids[TreeIndex[p.first]].node.emplace_back(p.first);\n }\n }\n void AddEdge(int x, int y) {\n graph[x].push_back(y);\n graph[y].push_back(x);\n }\n virtual void Build() {\n BuildSubTreeSize();\n BuildPath();\n }\n inline size_t size() { return (Centroids.size()); }\n inline pair<int, int> Information(int idx) {\n return (make_pair(TreeIndex[idx], TreeDepth[idx]));\n }\n inline Centroid &operator[](int k) { return (Centroids[k]); }\n inline int LCA(int a, int b) {\n int TreeIdxA, TreeDepthA, TreeIdxB, TreeDepthB;\n tie(TreeIdxA, TreeDepthA) = Information(a);\n tie(TreeIdxB, TreeDepthB) = Information(b);\n while (TreeIdxA != TreeIdxB) {\n if (Centroids[TreeIdxA].Deep > Centroids[TreeIdxB].Deep) {\n tie(TreeIdxA, TreeDepthA) = Centroids[TreeIdxA].Up();\n } else {\n tie(TreeIdxB, TreeDepthB) = Centroids[TreeIdxB].Up();\n }\n }\n if (TreeDepthA > TreeDepthB)\n swap(TreeDepthA, TreeDepthB);\n return (Centroids[TreeIdxA][TreeDepthA]);\n }\n inline virtual void query(int a, int b,\n const function<void(int, int, int)> &f) {\n int TreeIdxA, TreeDepthA, TreeIdxB, TreeDepthB;\n tie(TreeIdxA, TreeDepthA) = Information(a);\n tie(TreeIdxB, TreeDepthB) = Information(b);\n while (TreeIdxA != TreeIdxB) {\n if (Centroids[TreeIdxA].Deep > Centroids[TreeIdxB].Deep) {\n f(TreeIdxA, 0, TreeDepthA + 1);\n tie(TreeIdxA, TreeDepthA) = Centroids[TreeIdxA].Up();\n } else {\n f(TreeIdxB, 0, TreeDepthB + 1);\n tie(TreeIdxB, TreeDepthB) = Centroids[TreeIdxB].Up();\n }\n }\n if (TreeDepthA > TreeDepthB)\n swap(TreeDepthA, TreeDepthB);\n f(TreeIdxA, TreeDepthA, TreeDepthB + 1);\n }\n CentroidPathDecomposition(int SZ) {\n graph.resize(SZ);\n SubTreeSize.assign(SZ, -1);\n NextPath.resize(SZ);\n TreeIndex.resize(SZ);\n TreeDepth.resize(SZ);\n }\n};\nOrderedMultiSet<int> tree(5000000);\nOrderedMultiSet<int>::Node *nodes[100000];\nint N, Q, X[100000];\nvector<int> g[100000];\nint parent[100000];\nvoid dfs(int idx, int par, OrderedMultiSet<int>::Node *par_set) {\n parent[idx] = par;\n tree.insert_key(par_set, X[idx]);\n nodes[idx] = par_set;\n for (auto &to : g[idx])\n if (to != par)\n dfs(to, idx, par_set);\n}\nint main() {\n scanf(\"%d %d\", &N, &Q);\n CentroidPathDecomposition gg(N);\n vector<int> vs;\n for (int i = 0; i < N; i++) {\n scanf(\"%d\", &X[i]);\n vs.emplace_back(X[i]);\n }\n sort(begin(vs), end(vs));\n vs.erase(unique(begin(vs), end(vs)), end(vs));\n for (int i = 1; i < N; i++) {\n int x, y;\n scanf(\"%d %d\", &x, &y);\n --x, --y;\n g[x].emplace_back(y);\n g[y].emplace_back(x);\n gg.AddEdge(x, y);\n }\n dfs(0, -1, tree.makeset());\n gg.Build();\n for (int i = 0; i < Q; i++) {\n int x, y, z;\n scanf(\"%d %d %d\", &x, &y, &z);\n --x, --y;\n int lca = gg.LCA(x, y);\n int ng = -1, ok = (int)vs.size() - 1;\n auto sum = [&](int v) {\n int ret = 0;\n ret += tree.upper_bound(nodes[x], vs[v]);\n ret += tree.upper_bound(nodes[y], vs[v]);\n ret -= tree.upper_bound(nodes[lca], vs[v]);\n if (lca)\n ret -= tree.upper_bound(nodes[parent[lca]], vs[v]);\n return ret;\n };\n while (ok - ng > 1) {\n int mid = (ok + ng) >> 1;\n if (sum(mid) >= z)\n ok = mid;\n else\n ng = mid;\n }\n printf(\"%d\\n\", vs[ok]);\n }\n}", "accuracy": 0.7096774193548387, "time_ms": 360, "memory_kb": 138016, "score_of_the_acc": -1.125, "final_rank": 19 }, { "submission_id": "aoj_2270_8113349", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntemplate <class Key> struct RandomizedBinarySearchTree {\n inline int xor128() {\n static int x = 123456789;\n static int y = 362436069;\n static int z = 521288629;\n static int w = 88675123;\n int t;\n t = x ^ (x << 11);\n x = y;\n y = z;\n z = w;\n return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));\n }\n struct Node {\n Node *l, *r;\n int cnt;\n Key key;\n Node() {}\n Node(const Key &k) : cnt(1), key(k), l(nullptr), r(nullptr) {}\n };\n vector<Node> pool;\n int ptr;\n RandomizedBinarySearchTree(int sz) : pool(sz), ptr(0) {}\n inline Node *alloc(const Key &key) { return &(pool[ptr++] = Node(key)); }\n virtual Node *clone(Node *t) { return t; }\n inline int count(const Node *t) { return t ? t->cnt : 0; }\n inline Node *update(Node *t) {\n t->cnt = count(t->l) + count(t->r) + 1;\n return t;\n }\n Node *propagete(Node *t) { return update(clone(t)); }\n Node *merge(Node *l, Node *r) {\n if (!l || !r)\n return l ? l : r;\n if (xor128() % l->cnt < r->cnt) {\n l = propagete(l);\n l->r = merge(l->r, r);\n return update(l);\n } else {\n r = propagete(r);\n r->l = merge(l, r->l);\n return update(r);\n }\n }\n pair<Node *, Node *> split(Node *t, int k) {\n if (!t)\n return {t, t};\n t = propagete(t);\n if (k <= count(t->l)) {\n auto s = split(t->l, k);\n t->l = s.second;\n return {s.first, update(t)};\n } else {\n auto s = split(t->r, k - count(t->l) - 1);\n t->r = s.first;\n return {update(t), s.second};\n }\n }\n Node *build(int l, int r, const vector<Key> &v) {\n if (l + 1 >= r)\n return alloc(v[l]);\n return merge(build(l, (l + r) >> 1, v), build((l + r) >> 1, r, v));\n }\n Node *build(const vector<Key> &v) {\n ptr = 0;\n return build(0, (int)v.size(), v);\n }\n void dump(Node *r, typename vector<Key>::iterator &it) {\n if (!r)\n return;\n r = propagete(r);\n dump(r->l, it);\n *it = r->key;\n dump(r->r, ++it);\n }\n vector<Key> dump(Node *r) {\n vector<Key> v((size_t)count(r));\n auto it = begin(v);\n dump(r, it);\n return v;\n }\n string to_string(Node *r) {\n auto s = dump(r);\n string ret;\n for (int i = 0; i < s.size(); i++)\n ret += \", \";\n return (ret);\n }\n void insert(Node *&t, int k, const Key &v) {\n auto x = split(t, k);\n t = merge(merge(x.first, alloc(v)), x.second);\n }\n void erase(Node *&t, int k) {\n auto x = split(t, k);\n t = merge(x.first, split(x.second, 1).second);\n }\n int size(Node *t) { return count(t); }\n bool empty(Node *t) { return !t; }\n Node *makeset() { return (nullptr); }\n};\ntemplate <class Key>\nstruct PresidentRandomizedBinarySearchTree : RandomizedBinarySearchTree<Key> {\n using RBST = RandomizedBinarySearchTree<Key>;\n using Node = typename RBST::Node;\n PresidentRandomizedBinarySearchTree(int sz) : RBST(sz) {}\n Node *clone(Node *t) override { return &(RBST::pool[RBST::ptr++] = *t); }\n Node *rebuild(Node *r) { return RBST::build(RBST::dump(r)); }\n};\ntemplate <class T>\nstruct OrderedMultiSet : PresidentRandomizedBinarySearchTree<T> {\n using RBST = PresidentRandomizedBinarySearchTree<T>;\n using Node = typename RBST::Node;\n OrderedMultiSet(int sz) : RBST(sz) {}\n T kth_element(Node *t, int k) {\n if (k < RBST::count(t->l))\n return kth_element(t->l, k);\n if (k == RBST::count(t->l))\n return t->key;\n return kth_element(t->r, k - RBST::count(t->l) - 1);\n }\n virtual void insert_key(Node *&t, const T &x) {\n RBST::insert(t, lower_bound(t, x), x);\n }\n void erase_key(Node *&t, const T &x) {\n if (!count(t, x))\n return;\n RBST::erase(t, lower_bound(t, x));\n }\n int count(Node *t, const T &x) {\n return upper_bound(t, x) - lower_bound(t, x);\n }\n int lower_bound(Node *t, const T &x) {\n if (!t)\n return 0;\n if (x <= t->key)\n return lower_bound(t->l, x);\n return lower_bound(t->r, x) + RBST::count(t->l) + 1;\n }\n int upper_bound(Node *t, const T &x) {\n if (!t)\n return 0;\n if (x < t->key)\n return upper_bound(t->l, x);\n return upper_bound(t->r, x) + RBST::count(t->l) + 1;\n }\n};\nstruct CentroidPathDecomposition {\n struct Centroid {\n int ParIndex, ParDepth, Deep;\n vector<int> node;\n Centroid(int idx, int dep, int deep)\n : ParIndex(idx), ParDepth(dep), Deep(deep) {}\n inline size_t size() { return (node.size()); }\n inline int &operator[](int k) { return (node[k]); }\n inline pair<int, int> Up() { return (make_pair(ParIndex, ParDepth)); }\n };\n vector<vector<int>> graph;\n vector<int> SubTreeSize, NextPath;\n vector<int> TreeIndex, TreeDepth;\n vector<Centroid> Centroids;\n void BuildSubTreeSize() {\n stack<pair<int, int>> s;\n s.emplace(0, -1);\n while (!s.empty()) {\n auto p = s.top();\n s.pop();\n if (~SubTreeSize[p.first]) {\n NextPath[p.first] = -1;\n for (auto &to : graph[p.first]) {\n if (p.second == to)\n continue;\n SubTreeSize[p.first] += SubTreeSize[to];\n if (NextPath[p.first] == -1 ||\n SubTreeSize[NextPath[p.first]] < SubTreeSize[to]) {\n NextPath[p.first] = to;\n }\n }\n } else {\n s.push(p);\n SubTreeSize[p.first] = 1;\n for (auto &to : graph[p.first]) {\n if (p.second != to)\n s.emplace(to, p.first);\n }\n }\n }\n }\n void BuildPath() {\n stack<pair<int, int>> s;\n Centroids.emplace_back(-1, -1, 0);\n s.emplace(0, -1);\n TreeIndex[0] = 0;\n while (!s.empty()) {\n auto p = s.top();\n s.pop();\n TreeDepth[p.first] = (int)Centroids[TreeIndex[p.first]].size();\n for (auto &to : graph[p.first]) {\n if (p.second == to)\n continue;\n if (to == NextPath[p.first]) {\n TreeIndex[to] = TreeIndex[p.first];\n } else {\n TreeIndex[to] = (int)Centroids.size();\n Centroids.emplace_back(TreeIndex[p.first], TreeDepth[p.first],\n Centroids[TreeIndex[p.first]].Deep + 1);\n }\n s.emplace(to, p.first);\n }\n Centroids[TreeIndex[p.first]].node.emplace_back(p.first);\n }\n }\n void AddEdge(int x, int y) {\n graph[x].push_back(y);\n graph[y].push_back(x);\n }\n virtual void Build() {\n BuildSubTreeSize();\n BuildPath();\n }\n inline size_t size() { return (Centroids.size()); }\n inline pair<int, int> Information(int idx) {\n return (make_pair(TreeIndex[idx], TreeDepth[idx]));\n }\n inline Centroid &operator[](int k) { return (Centroids[k]); }\n inline int LCA(int a, int b) {\n int TreeIdxA, TreeDepthA, TreeIdxB, TreeDepthB;\n tie(TreeIdxA, TreeDepthA) = Information(a);\n tie(TreeIdxB, TreeDepthB) = Information(b);\n while (TreeIdxA != TreeIdxB) {\n if (Centroids[TreeIdxA].Deep > Centroids[TreeIdxB].Deep) {\n tie(TreeIdxA, TreeDepthA) = Centroids[TreeIdxA].Up();\n } else {\n tie(TreeIdxB, TreeDepthB) = Centroids[TreeIdxB].Up();\n }\n }\n if (TreeDepthA > TreeDepthB)\n swap(TreeDepthA, TreeDepthB);\n return (Centroids[TreeIdxA][TreeDepthA]);\n }\n inline virtual void query(int a, int b,\n const function<void(int, int, int)> &f) {\n int TreeIdxA, TreeDepthA, TreeIdxB, TreeDepthB;\n tie(TreeIdxA, TreeDepthA) = Information(a);\n tie(TreeIdxB, TreeDepthB) = Information(b);\n while (TreeIdxA != TreeIdxB) {\n if (Centroids[TreeIdxA].Deep > Centroids[TreeIdxB].Deep) {\n f(TreeIdxA, 0, TreeDepthA + 1);\n tie(TreeIdxA, TreeDepthA) = Centroids[TreeIdxA].Up();\n } else {\n f(TreeIdxB, 0, TreeDepthB + 1);\n tie(TreeIdxB, TreeDepthB) = Centroids[TreeIdxB].Up();\n }\n }\n if (TreeDepthA > TreeDepthB)\n swap(TreeDepthA, TreeDepthB);\n f(TreeIdxA, TreeDepthA, TreeDepthB + 1);\n }\n CentroidPathDecomposition(int SZ) {\n graph.resize(SZ);\n SubTreeSize.assign(SZ, -1);\n NextPath.resize(SZ);\n TreeIndex.resize(SZ);\n TreeDepth.resize(SZ);\n }\n};\nOrderedMultiSet<int> tree(5000000);\nOrderedMultiSet<int>::Node *nodes[100000];\nint N, Q, X[100000];\nvector<int> g[100000];\nint parent[100000];\nvoid dfs(int idx, int par, OrderedMultiSet<int>::Node *par_set) {\n parent[idx] = par;\n tree.insert_key(par_set, X[idx]);\n nodes[idx] = par_set;\n for (auto &to : g[idx])\n if (to != par)\n dfs(to, idx, par_set);\n}\nint main() {\n scanf(\"%d %d\", &N, &Q);\n CentroidPathDecomposition gg(N);\n vector<int> vs;\n for (int i = 0; i < N; i++) {\n scanf(\"%d\", &X[i]);\n vs.emplace_back(X[i]);\n }\n sort(begin(vs), end(vs));\n vs.erase(unique(begin(vs), end(vs)), end(vs));\n for (int i = 1; i < N; i++) {\n int x, y;\n scanf(\"%d %d\", &x, &y);\n --x, --y;\n g[x].emplace_back(y);\n g[y].emplace_back(x);\n gg.AddEdge(x, y);\n }\n dfs(0, -1, tree.makeset());\n gg.Build();\n for (int i = 0; i < Q; i++) {\n int x, y, z;\n scanf(\"%d %d %d\", &x, &y, &z);\n --x, --y;\n int lca = gg.LCA(x, y);\n int ng = -1, ok = (int)vs.size() - 1;\n auto sum = [&](int v) {\n int ret = 0;\n ret += tree.upper_bound(nodes[x], vs[v]);\n ret += tree.upper_bound(nodes[y], vs[v]);\n ret -= tree.upper_bound(nodes[lca], vs[v]);\n if (lca)\n ret -= tree.upper_bound(nodes[parent[lca]], vs[v]);\n return ret;\n };\n while (ok - ng > 1) {\n int mid = (ok + ng) >> 1;\n if (sum(mid) >= z)\n ok = mid;\n else\n ng = mid;\n }\n printf(\"%d\\n\", vs[ok]);\n }\n}", "accuracy": 1, "time_ms": 1030, "memory_kb": 127132, "score_of_the_acc": -1.2834, "final_rank": 13 } ]
aoj_2274_cpp
問題 D 列の構成 問題文 1 から N までの相異なる整数が N / 2 個書かれたカードがいくつか与えられるので,次の条件を満たすような長さ N の数列 seq を 1 つ作って出力して欲しい. seq の各要素は 0 か 1 である. すべてのカードについて以下が成り立つ: カードに書かれた数字を card[1], ..., card[N/2] とする.このときカードに書かれた部分の和: seq[card[1]] + ... + seq[card[N/2]] が N / 8 以上かつ 3N / 8 以下になっている. 例えば, N = 8 でカードが 2 枚渡され,カードに書かれている数字がそれぞれ [1, 2, 7, 8] , [4, 5, 7, 8] であったとする.このとき seq=[0, 1, 0, 1, 0, 1, 0, 1] や seq=[0, 0, 0, 0, 1, 1, 1, 1] とおくと条件を満たすようにできている. 入力形式 入力は次の形式で与えられる. N K card 1 [1] card 1 [2] ... card 1 [N/2] card 2 [1] card 2 [2] ... card 2 [N/2] ... card K [1] card K [2] ... card K [N/2] 1 行目において N は構成するべき数列の長さ, K はカードの枚数である. 続く K 行には各カードの情報が与えられる. card i [1], ..., card i [N/2] が i 番目のカードに書かれている数字である. 出力形式 seq の i 番目の要素が i 文字目に対応するように数列 seq を 1 行に出力せよ. なお,どの入力に対しても解は必ず少なくとも 1 つは存在する. 制約 8 ≤ N ≤ 1,000 , 1≤ K ≤ N / 2 N は 8 の倍数 1 ≤ card i [1] < card i [2] < ... < card i [N/2] ≤ N 入出力例 入力例 1 8 2 1 2 7 8 4 5 7 8 出力例 1 01010101 入力例 2 8 3 2 3 4 6 3 4 5 8 3 4 6 8 出力例 2 01110011
[ { "submission_id": "aoj_2274_10809100", "code_snippet": "#include<cstdio>\n#include<cstdlib>\n#include<cstring>\n#include<cmath>\n#include<iostream>\n#include<string>\n#include<vector>\n#include<map>\n#include<set>\n#include<list>\n#include<queue>\n#include<deque>\n#include<algorithm>\n#include<numeric>\n#include<utility>\n#include<complex>\n#include<functional>\n \nusing namespace std;\n\n/* constant */\n\nconst int MAX_N = 1000;\nconst int MAX_K = MAX_N / 2;\n\n/* typedef */\n\n/* global variables */\n\nint cards[MAX_K][MAX_N / 2];\nint seq[MAX_N];\n\n/* subroutines */\n\nvoid gen_seq(int n) {\n for (int i = 0; i < n; i++) seq[i] = rand() & 1;\n}\n\n/* main */\n\nint main() {\n srand(time(NULL));\n\n int n, k;\n cin >> n >> k;\n\n int nh = n / 2;\n int cmin = n / 8, cmax = 3 * cmin;\n\n memset(cards, 0, sizeof(cards));\n \n for (int i = 0; i < k; i++)\n for (int j = 0; j < nh; j++) {\n cin >> cards[i][j];\n cards[i][j]--;\n }\n\n for (;;) {\n gen_seq(n);\n\n bool ok = true;\n for (int i = 0; i < k; i++) {\n int cnt = 0;\n for (int j = 0; j < nh; j++) cnt += seq[cards[i][j]];\n if (cnt < cmin || cnt > cmax) {\n\tok = false;\n\tbreak;\n }\n }\n\n if (ok) break;\n }\n\n for (int i = 0; i < n; i++) cout << seq[i]; cout << endl;\n \n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 4424, "score_of_the_acc": -0.2538, "final_rank": 7 }, { "submission_id": "aoj_2274_4299959", "code_snippet": "#include <iostream>\n#include <cmath>\n#include <numeric>\n#include <algorithm>\n#include <deque>\n#include <vector>\n#include <unordered_map>\n#include <unordered_set>\n#include <iomanip>\n#include <map>\n#include <stack>\n#include <queue>\n#include <functional>\n#include <climits>\n#include <numeric>\n#include <bitset>\n#include <random>\n#include <tuple>\n#include <initializer_list>\n#include <fstream>\n\nbool is_match(const std::vector<int>& set, const std::vector<std::vector<int>> &cards) {\n\tfor (const auto& card : cards) {\n\t\tint count{ 0 };\n\t\tfor (const auto& c : card) count += set[c];\n\t\tif (count * 8 < set.size() || set.size() * 3 < count * 8) return false;\n\t}\n\treturn true;\n}\nvoid set_random(std::vector<int>& set, std::mt19937& rand) {\n\tfor (auto& b : set) b = rand() & 1;\n}\nint main() {\n\tint n, k; std::cin >> n >> k;\n\tstd::vector<std::vector<int>> cards(k, std::vector<int>(n / 2));\n\tfor (auto& card : cards) for (auto& c : card) {\n\t\tstd::cin >> c; --c;\n\t}\n\tstd::vector<int> set(n);\n\tstd::mt19937 rand;\n\tset_random(set, rand);\n\twhile (!is_match(set, cards)) set_random(set, rand);\n\tfor (const auto& s : set) std::cout << s;\n\tstd::cout << std::endl;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3932, "score_of_the_acc": -0.3547, "final_rank": 8 }, { "submission_id": "aoj_2274_3887774", "code_snippet": "#include <iostream>\n#include <vector>\n#include <random>\nusing namespace std;\ntypedef long long int ll;\n\nint card[1100][1100];\nint seq[1100];\n\nint main(){\n\tint n,k; cin >> n >> k;\n\tfor(int i=0;i<k;i++){\n\t\tfor(int j=0;j<n/2;j++){\n\t\t\tcin >> card[i][j];\n\t\t\tcard[i][j]--;\n\t\t}\n\t}\n\trandom_device rnd;\n\tmt19937 mt(rnd());\n\twhile(1){\n\t\tfor(int i=0;i<n;i++){\n\t\t\tseq[i]=mt()%2;\n\t\t}\n\t\tbool ok=true;\n\t\tfor(int i=0;i<k;i++){\n\t\t\tint sum=0;\n\t\t\tfor(int j=0;j<n/2;j++){\n\t\t\t\tsum+=seq[card[i][j]];\n\t\t\t\tif(sum>3*n/8)ok=false;\n\t\t\t}\n\t\t\tif(sum<n/8)ok=false;\n\t\t\tif(!ok)break;\n\t\t}\n\t\tif(ok){\n\t\t\tfor(int i=0;i<n;i++){\n\t\t\t\tcout << seq[i];\n\t\t\t}\n\t\t\tcout << endl;\n\t\t\treturn 0;\n\t\t}\n\t}\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 5252, "score_of_the_acc": -0.4673, "final_rank": 13 }, { "submission_id": "aoj_2274_3769172", "code_snippet": "#include<iostream>\n#include<ctime>\n#include<cstdlib>\n\nusing namespace std;\n\nint main()\n{\n \n int n,k;\n int card[505][505];\n \n cin >> n >> k;\n for(int i=0;i<k;i++){\n for(int j=0;j<n/2;j++) cin >> card[i][j];\n }\n \n srand( (unsigned int)time(NULL) );\n \n int sum,cnt,ans[1010];\n \n do{\n \n for(int i=1;i<=n;i++) ans[i] = rand() % 2;\n \n cnt = 0;\n for(int i=0;i<k;i++){\n sum = 0;\n for(int j=0;j<n/2;j++){\n sum += ans[ card[i][j] ];\n }\n if(sum >= n/8 && sum <= 3*n/8) cnt++;\n }\n \n }while(cnt < k);\n \n for(int i=1;i<=n;i++) cout << ans[i];\n cout << endl;\n \n return(0);\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 4100, "score_of_the_acc": -0.369, "final_rank": 9 }, { "submission_id": "aoj_2274_3769163", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\nint main()\n{\n \n int n,k;\n int card[505][505];\n \n cin >> n >> k;\n for(int i=0;i<k;i++){\n for(int j=0;j<n/2;j++) cin >> card[i][j];\n }\n \n srand( (unsigned int)time(NULL) );\n \n int sum,cnt,ans[1010];\n \n do{\n \n for(int i=1;i<=n;i++) ans[i] = rand() % 2;\n \n cnt = 0;\n for(int i=0;i<k;i++){\n sum = 0;\n for(int j=0;j<n/2;j++){\n sum += ans[ card[i][j] ];\n }\n if(sum >= n/8 && sum <= 3*n/8) cnt++;\n }\n \n }while(cnt < k);\n \n for(int i=1;i<=n;i++) cout << ans[i];\n cout << endl;\n \n return(0);\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 4108, "score_of_the_acc": -0.3697, "final_rank": 10 }, { "submission_id": "aoj_2274_3373996", "code_snippet": "#include <cstdio>\n#include <climits>\n#include <iostream>\n#include <string>\n#include <vector>\n#include <set>\n#include <cmath>\n#include <stack>\n#include <algorithm>\n#include <random>\n#include <iomanip>\n#include <map>\n#include <queue>\n#include <functional>\n#include <numeric>\n#include <chrono>\n#include <cstdlib>\nusing ll = long long;\nusing namespace std;\n\nconst ll MOD = 1e9 + 7;\nconst double pi = acos(-1);\ntypedef pair<int, int> P;\n#define REP(i, n) for (int(i) = 0; (i) < (n); ++(i))\n#define repi(i, a, b) for (int i = int(a); i < int(b); ++i)\n#define EPS 1e-4\n#define OUTPUT(i) (cout << (ll)i << endl)\n#define ALL(a) ((a).begin(), (a).end())\n\nbool operator<(const pair<ll, ll> &a, const pair<ll, ll> &b)\n{\n\n if (a.first == b.first)\n return a.second < b.second;\n\n return a.first < b.first;\n}\n\nll gcd(ll a, ll b)\n{\n if (b == 0)\n return a;\n return gcd(b, a % b);\n}\nint N, K;\nint ub, lb;\nint ans[1008];\nvector<vector<int>> v(1001);\n\nbool check()\n{\n\n for (int i = 0; i < K; ++i)\n {\n int cnt = 0;\n for (int j = 0; j < N / 2; ++j)\n {\n cnt += ans[v[i][j]];\n }\n if (cnt < lb || cnt > ub)\n return false;\n }\n return true;\n}\n\nvoid solve()\n{\n for (int i = 0; i < N / 2; ++i)\n ans[i] = 1;\n while (1)\n {\n std::random_device seed_gen;\n std::mt19937 engine(seed_gen());\n shuffle(ans, ans + N, engine);\n if (check())\n break;\n }\n}\nint main()\n{\n cin.tie(0);\n ios::sync_with_stdio(false);\n\n //cout << fixed << setprecision(15);\n\n cin >> N >> K;\n lb = N / 8;\n ub = 3 * lb;\n\n for (int i = 0; i < K; ++i)\n {\n for (int j = 0; j < N / 2; ++j)\n {\n int a;\n cin >> a;\n a--;\n v[i].push_back(a);\n }\n }\n solve();\n\n for (int i = 0; i < N; ++i)\n {\n cout << ans[i];\n }\n cout << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3784, "score_of_the_acc": -0.0563, "final_rank": 2 }, { "submission_id": "aoj_2274_3097993", "code_snippet": "#define _USE_MATH_DEFINES\n\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <iostream>\n#include <cmath>\n#include <algorithm>\n#include <vector>\n#include <queue>\n#include <map>\n#include <list>\n\nusing namespace std;\n\ntypedef pair<long long int, long long int> P;\n\nlong long int INF = 1e18;\nlong long int MOD = 1e9 + 7;\n\nint card[550][550];\n\nint main(){\n\t\n\tint N, K;\n\tcin >> N >> K;\n\t\n\tfor(int i = 0; i < K; i++){\n\t\tfor(int j = 0; j < N / 2; j++){\n\t\t\tcin >> card[i][j];\n\t\t\tcard[i][j]--;\n\t\t}\n\t}\n\t\n\tint ans[1100];\n\t\n\twhile(true){\n\t\tfor(int i = 0; i < N; i++){\n\t\t\tans[i] = rand() % 2;\n\t\t}\n\t\tbool flag = true;\n\t\tfor(int i = 0; i < K; i++){\n\t\t\tint cnt = 0;\n\t\t\tfor(int j = 0; j < N / 2; j++){\n\t\t\t\tcnt += ans[card[i][j]];\n\t\t\t}\n\t\t\tif(cnt < N / 8 || cnt > 3 * N / 8){\n\t\t\t\tflag = false;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tif(flag){\n\t\t\tbreak;\n\t\t}\n\t}\n\t\n\tfor(int i = 0; i < N; i++){\n\t\tcout << ans[i];\n\t}\n\tcout << endl;\n\t\n\treturn 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 4128, "score_of_the_acc": -0.3714, "final_rank": 11 }, { "submission_id": "aoj_2274_2972178", "code_snippet": "#include <iostream>\n#include <vector>\n#include <random>\nusing namespace std;\n\nint main(){\n\tmt19937 mt(12345);\n\tint n,k;\n\tcin >> n >> k;\n\tvector<vector<int> > card(k, vector<int>(n/2));\n\tfor(int i=0; i<k; i++){\n\t\tfor(int j=0; j<n/2; j++){\n\t\t\tcin >> card[i][j];\n\t\t\tcard[i][j]--;\n\t\t}\n\t}\n\tstring ans = \"\";\n\twhile(1){\n\t\tbool success = true;\n\t\tvector<int> seq(n);\n\t\tfor(int i=0; i<n; i++){\n\t\t\tseq[i] = mt()%2;\n\t\t}\n\t\tfor(int i=0; i<k; i++){\n\t\t\tint sum = 0;\n\t\t\tfor(int j=0; j<n/2; j++){\n\t\t\t\tsum += seq[card[i][j]];\n\t\t\t}\n\t\t\tif(sum < n/8 || n*3/8 < sum){\n\t\t\t\tsuccess = false;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tif(success){\n\t\t\tfor(int i=0; i<n; i++){\n\t\t\t\tans += seq[i] +'0';\n\t\t\t}\n\t\t\tbreak;\n\t\t}\n\t}\n\tcout << ans << endl;\n\treturn 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3828, "score_of_the_acc": -0.4887, "final_rank": 15 }, { "submission_id": "aoj_2274_2655829", "code_snippet": "#include \"bits/stdc++.h\"\n#include<unordered_map>\n#include<unordered_set>\n#include<random>\n#pragma warning(disable:4996)\nusing namespace std;\n\nbool check(const vector<int>&anss, const vector<vector<int>>&cards) {\n\tfor (auto card : cards) {\n\t\tint sum=0;\n\t\tfor (auto num : card) {\n\t\t\tsum+=anss[num];\n\t\t}\n\t\tif(sum<anss.size()/8||sum>anss.size()*3/8)return false;\n\t}\n\treturn true;\n}\n\nint main() {\n\tint N,M;cin>>N>>M;\n\tvector<vector<int>>cards(M);\n\tfor (int i = 0; i < M; ++i) {\n\t\tcards[i].resize(N/2);\n\t\tfor(int j = 0; j < N / 2; ++j) {\n\t\t\tcin>>cards[i][j];\n\t\t\tcards[i][j]--;\n\t\t}\n\t}\n\tvector<int>anss(N,0);\n\tfor (int i = 0; i < N / 2; ++i) {\n\t\tanss[i]=1;\n\t}\n\trandom_device rnd;\n\tmt19937 mt(rnd());\n\tshuffle(anss.begin(),anss.end(),mt);\n\twhile (true) {\n\t\tif (check(anss, cards)) {\n\t\t\tbreak;\n\t\t}\n\t\telse {\n\t\t\tfor (auto card : cards) {\n\t\t\t\tint sum = 0;\n\t\t\t\tfor (auto num : card) {\n\t\t\t\t\tsum += anss[num];\n\t\t\t\t}\n\t\t\t\tif (sum < anss.size() / 8) {\n\t\t\t\t\tint use;\n\t\t\t\t\tint unuse;\n\t\t\t\t\twhile (true) {\n\t\t\t\t\t\tint n = mt() % (card.size() * 2);\n\t\t\t\t\t\tif (!binary_search(card.begin(), card.end(), n)&&anss[n]) {\n\t\t\t\t\t\t\tunuse = n;\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\twhile (true) {\n\t\t\t\t\t\tint n = card[mt()%card.size()];\n\t\t\t\t\t\tif ( !anss[n]) {\n\t\t\t\t\t\t\tuse = n;\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tswap(anss[use],anss[unuse]);\n\t\t\t\t}\n\t\t\t\telse if (sum > 3*anss.size() / 8) {\n\t\t\t\t\tint use;\n\t\t\t\t\tint unuse;\n\t\t\t\t\twhile (true) {\n\t\t\t\t\t\tint n = mt() % (card.size() * 2);\n\t\t\t\t\t\tif (!binary_search(card.begin(), card.end(), n) && !anss[n]) {\n\t\t\t\t\t\t\tuse = n;\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\twhile (true) {\n\t\t\t\t\t\tint n = card[mt() % card.size()];\n\t\t\t\t\t\tif (anss[n]) {\n\t\t\t\t\t\t\tunuse = n;\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tswap(anss[use], anss[unuse]);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tfor (int i = 0; i < anss.size(); ++i) {\n\t\tcout<<anss[i];\n\t}\n\tcout<<endl;\n\treturn 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3896, "score_of_the_acc": -0.4945, "final_rank": 16 }, { "submission_id": "aoj_2274_2635564", "code_snippet": "#include <stdio.h>\n#include <cmath>\n#include <math.h>\n#include <algorithm>\n#include <cfloat>\n#include <stack>\n#include <queue>\n#include <vector>\n#include <string>\n#include <iostream>\n#include <set>\n#include <map>\n#include <time.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n\n\nint main(){\n\n\tint N,K;\n\tscanf(\"%d %d\",&N,&K);\n\n\tint table[K][N/2];\n\n\tfor(int row = 0; row < K; row++){\n\t\tfor(int col = 0; col < N/2; col++){\n\t\t\tscanf(\"%d\",&table[row][col]);\n\t\t\ttable[row][col]--;\n\t\t}\n\t}\n\n\tint ans[N];\n\tfor(int i = 0; i < N/2; i++)ans[i] = 1;\n\tfor(int i = N/2; i < N; i++)ans[i] = 0;\n\n\tbool FLG;\n\tint sum;\n\n\twhile(true){\n\t\tFLG = true;\n\t\tfor(int i = 0; i < K; i++){\n\t\t\tsum = 0;\n\t\t\tfor(int a = 0; a < N/2; a++){\n\t\t\t\tsum += ans[table[i][a]];\n\t\t\t}\n\t\t\tif(sum < N/8 || sum > 3*N/8){\n\t\t\t\tFLG = false;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\n\t\tif(FLG)break;\n\n\t\trandom_shuffle(ans,ans+N);\n\n\t}\n\n\tfor(int i = 0; i < N; i++)printf(\"%d\",ans[i]);\n\tprintf(\"\\n\");\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 4212, "score_of_the_acc": -0.0929, "final_rank": 4 }, { "submission_id": "aoj_2274_2599892", "code_snippet": "#include<bits/stdc++.h>\n#define r(i,n) for(int i=0;i<n;i++)\nusing namespace std;\nint n,k,a[9999][9999],b[9999];\nint main(){\n cin>>n>>k;\n r(i,k)r(j,n/2)scanf(\"%d\",&a[i][j]);\n while(1){\n r(i,n)b[i+1]=rand()%2;\n r(i,k){\n int cnt=0;\n r(j,n/2)cnt+=b[a[i][j]];\n if(cnt < n/8 || 3*n/8 < cnt)goto L;\n }\n r(i,n)cout<<b[i+1];\n cout<<endl;\n return 0;\n L:;\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 6152, "score_of_the_acc": -0.4013, "final_rank": 12 }, { "submission_id": "aoj_2274_2599888", "code_snippet": "#include<bits/stdc++.h>\n#define r(i,n) for(int i=0;i<n;i++)\nusing namespace std;\nint n,k,a[9999][9999],b[9999];\nint main(){\n cin>>n>>k;\n r(i,k)r(j,n/2)cin>>a[i][j];\n while(1){\n r(i,n)b[i+1]=rand()%2;\n r(i,k){\n int cnt=0;\n r(j,n/2)cnt+=b[a[i][j]];\n if(cnt < n/8 || 3*n/8 < cnt)goto L;\n }\n r(i,n)cout<<b[i+1];\n cout<<endl;\n return 0;\n L:;\n }\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 6088, "score_of_the_acc": -0.5387, "final_rank": 17 }, { "submission_id": "aoj_2274_2595483", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing Int = long long;\nsigned main(){\n Int n,k;\n cin>>n>>k;\n vector< vector<Int> > v(k);\n for(Int i=0;i<k;i++){\n v[i].resize(n/2);\n for(Int j=0;j<n/2;j++) cin>>v[i][j],v[i][j]--;\n }\n\n auto check=[&](string s){\n for(Int i=0;i<k;i++){\n Int tmp=0;\n for(Int j=0;j<n/2;j++)\n\ttmp+=(s[v[i][j]]=='1');\n //cout<<i<<\":\"<<tmp<<endl;\n if(tmp<n/8||tmp>3*n/8) return 0;\n }\n return 1;\n };\n \n srand((unsigned)time(NULL));\n while(1){\n string s;\n for(Int i=0;i<n;i++)\n s.push_back(char('0'+(rand()%2)));\n if(check(s)){\n cout<<s<<endl;\n return 0;\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 4564, "score_of_the_acc": -0.5515, "final_rank": 18 }, { "submission_id": "aoj_2274_2429039", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nint N,K;\nset<int> G[500];\n\nint main(){\n cin>>N>>K;\n for(int i=0;i<K;i++){\n for(int j=0;j<N/2;j++){\n int k;\n cin>>k;\n k--;\n G[i].insert(k);\n }\n }\n vector<int> t(N);\n for(int i=0;i<N;i++)t[i]=i;\n int cc=0;\n while(cc<1000000){\n cc+=N;\n random_shuffle(t.begin(),t.end());\n vector<int> sum(K,0);\n for(int i=0;i<N/2;i++){\n int id=t[i];\n for(int j=0;j<K;j++){\n sum[j]+=G[j].count(id);\n }\n }\n int mini=1e9,maxm=0;\n for(int i=0;i<K;i++){\n mini=min(mini,sum[i]);\n maxm=max(maxm,sum[i]);\n }\n if(N/8<=mini&&maxm<=N/8*3){\n vector<int> ans(N,0);\n for(int i=0;i<N/2;i++)ans[ t[i] ]=1;\n for(int i=0;i<N;i++)cout<<ans[i];\n cout<<endl;\n break;\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 14840, "score_of_the_acc": -2, "final_rank": 20 }, { "submission_id": "aoj_2274_2329570", "code_snippet": "#include<bits/stdc++.h>\n#define rep(i,n)for(int i=0;i<(n);i++)\nusing namespace std;\n\nint a[500][500];\nint v[1000];\nint main() {\n\tint n, k; scanf(\"%d%d\", &n, &k);\n\trep(i, k)rep(j, n / 2)scanf(\"%d\", &a[i][j]), a[i][j]--;\n\trandom_device rnd; mt19937 mt(rnd());\n\twhile (1) {\n\t\trep(i, n)v[i] = mt() % 2;\n\t\trep(i, k) {\n\t\t\tint sum = 0;\n\t\t\trep(j, n / 2) {\n\t\t\t\tsum += v[a[i][j]];\n\t\t\t\tif (sum > 3 * n / 8)goto g;\n\t\t\t\tif (sum + n / 2 - j - 1 < n / 8)goto g;\n\t\t\t}\n\t\t}\n\t\trep(i, n)printf(\"%d\", v[i]); puts(\"\");\n\t\treturn 0;\n\tg:;\n\t}\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 4268, "score_of_the_acc": -0.0976, "final_rank": 5 }, { "submission_id": "aoj_2274_2144794", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define int long long\n#define FR first\n#define SC second\n#define all(v) (v).begin(), (v).end()\n#define rall(v) (v).rbegin(), (v).rend()\n#define rep(i, n) for(int i = 0; i < (int)(n); i++)\n#define reps(i, f, n) for(int i = (int)(f); i < (int)(n); i++)\n#define each(a, b) for(auto& a : b)\n\ntypedef pair<int, int> P;\n\nconst int inf = 1LL << 55;\nconst int mod = 1e9 + 7;\n\nsigned main()\n{\n cin.tie(0);\n ios_base::sync_with_stdio(0);\n cout << fixed << setprecision(12);\n\n int N, K;\n cin >> N >> K;\n int c[1010][505];\n rep(i, K) rep(j, N/2) cin >> c[i][j];\n srand((unsigned)time(NULL));\n while(1) {\n string bin = \"\";\n rep(i, N) bin += '0' + rand()%2;\n bool flag = true;\n rep(i, K) {\n int sum = 0;\n rep(j, N/2) sum += bin[c[i][j]-1] - '0';\n if(sum < N/8 || 3*N/8 < sum) flag = false;\n if(!flag) break;\n }\n if(flag) {\n cout << bin << endl;\n break;\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 5132, "score_of_the_acc": -0.1714, "final_rank": 6 }, { "submission_id": "aoj_2274_1886265", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\nint main()\n{\n int N, K;\n cin >> N >> K;\n vector<vector<int>> card(K, vector<int>(N/2));\n for (int i = 0; i < K; i++) {\n for (int j = 0; j < N/2; j++) {\n cin >> card[i][j];\n card[i][j]--;\n }\n }\n \n random_device rd;\n mt19937 mt(rd());\n bool finished = 0;\n while (!finished) {\n finished = 1;\n \n vector<int> seq(N);\n for (int i = 0; i < N; i++) {\n seq[i] = mt() % 2;\n }\n \n for (int i = 0; i < K; i++) {\n int sum = 0;\n for (int j = 0; j < N/2; j++) {\n sum += seq[card[i][j]];\n }\n if (sum < N/8 || sum > 3*N/8) {\n finished = 0;\n break;\n }\n }\n\n if (finished) {\n for (int i = 0; i < N; i++) {\n cout << seq[i];\n }\n cout << endl;\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3816, "score_of_the_acc": -0.4876, "final_rank": 14 }, { "submission_id": "aoj_2274_1770461", "code_snippet": "#include<iostream>\n#include<algorithm>\n#include<time.h>\nusing namespace std;\nint a[10000][1000], c[10000], n, k;\nint main() {\n\tsrand((unsigned)time(NULL));\n\tcin >> n >> k;\n\tfor (int i = 0; i < k; i++) {\n\t\tfor (int j = 0; j < n / 2; j++) {\n\t\t\tcin >> a[i][j];\n\t\t}\n\t}\n\twhile (true) {\n\t\tfor (int i = 0; i < n; i++) c[i] = (unsigned)(rand()) % 2;\n\t\tfor (int i = 0; i < k; i++) {\n\t\t\tint cnt = 0;\n\t\t\tfor (int j = 0; j < n; j++) {\n\t\t\t\tcnt += c[a[i][j] - 1];\n\t\t\t}\n\t\t\tif (cnt < n / 8 || cnt>3 * n / 8) { goto E; }\n\t\t}\n\t\tfor (int i = 0; i < n; i++) { cout << c[i]; }\n\t\tcout << endl; goto F;\n\tE:;\n\t}\nF:;\n\treturn 0;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 3124, "score_of_the_acc": -0.7143, "final_rank": 19 }, { "submission_id": "aoj_2274_1697356", "code_snippet": "#include <stdio.h>\n#include <stdlib.h>\nint N, K, card[500][500], r[1000];\nint main() {\n\tscanf(\"%d%d\", &N, &K);\n\tfor (int i = 0; i < K; i++) {\n\t\tfor (int j = 0; j < N / 2; j++) {\n\t\t\tscanf(\"%d\", &card[i][j]);\n\t\t}\n\t}\n\twhile (true) {\n\t\tfor (int i = 0; i < N; i++) r[i] = rand() % 2;\n\t\tbool flag = true;\n\t\tfor (int i = 0; i < K; i++) {\n\t\t\tint c = 0;\n\t\t\tfor (int j = 0; j < N / 2; j++) {\n\t\t\t\tif (r[card[i][j] - 1]) c++;\n\t\t\t}\n\t\t\tif (c < N / 8 || 3 * N / 8 < c) { flag = false; break; }\n\t\t}\n\t\tif (flag) {\n\t\t\tfor (int i = 0; i < N; i++) {\n\t\t\t\tprintf(\"%d\", r[i]);\n\t\t\t}\n\t\t\tprintf(\"\\n\"); break;\n\t\t}\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3696, "score_of_the_acc": -0.0488, "final_rank": 1 }, { "submission_id": "aoj_2274_1697355", "code_snippet": "#include <ctime>\n#include <iostream>\n#pragma warning(disable : 4996)\nusing namespace std;\nint N, K, card[500][500], r[1000];\nint main() {\n\tsrand((unsigned)time(NULL));\n\tscanf(\"%d%d\", &N, &K);\n\tfor (int i = 0; i < K; i++) {\n\t\tfor (int j = 0; j < N / 2; j++) {\n\t\t\tscanf(\"%d\", &card[i][j]);\n\t\t}\n\t}\n\twhile (true) {\n\t\tfor (int i = 0; i < N; i++) r[i] = rand() % 2;\n\t\tbool flag = true;\n\t\tfor (int i = 0; i < K; i++) {\n\t\t\tint c = 0;\n\t\t\tfor (int j = 0; j < N / 2; j++) {\n\t\t\t\tif (r[card[i][j] - 1]) c++;\n\t\t\t}\n\t\t\tif (c < N / 8 || 3 * N / 8 < c) { flag = false; break; }\n\t\t}\n\t\tif (flag) {\n\t\t\tfor (int i = 0; i < N; i++) {\n\t\t\t\tprintf(\"%d\", r[i]);\n\t\t\t}\n\t\t\tprintf(\"\\n\"); break;\n\t\t}\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 4204, "score_of_the_acc": -0.0922, "final_rank": 3 } ]
aoj_2276_cpp
問題 F ボ〜ル 問題文 2 次元平面上の原点にボールがあり, x 軸の正方向との角度が反時計回りに見て 0 度 180 度までの間の方向に一様な確率で発射されようとしている (発射される方向は整数角であるとは限らない).ボールの大きさは十分小さく,平面上では点であると見なすことにする.この問題における目的は,このボールをできるだけ高い確率で捕獲することである. 平面上に N 個の場所 (x i , y i ) が与えられる.ボールを捕獲するために,あなたは N 個の場所から K 個の場所を選んで,それぞれの場所に人を配置することができる.人は i 番目の与えられた場所に対して半径 r i 以内の距離まで動いてボールを取ることが出来る. 人の配置をうまく選んでボールを捕獲できる確率を最大にするとき,その確率を出力せよ. 入力形式 入力は以下の形式で与えられる. N K x 1 y 1 r 1 ... x N y N r N N はボールを捕獲するために人を置くための場所の数であり, K はその中から使うことの出来る場所の数である. (x i , y i ) は i 番目の場所の座標であり, r i はそこから動くことの出来る距離である. 出力形式 確率を小数表記で 1 行に出力せよ.小数点以下何桁でも出力して構わないが,相対誤差あるいは絶対誤差が 10 -6 未満になっていなければならない. 制約 1≤ N≤ 1,500, 1≤ K≤ N |x i | ≤ 1,000, |y i | ≤ 1,000, 1 ≤ r i < (x i 2 + y i 2 ) 1/2 入力値はすべて整数である. 入出力例 入力例 1 2 1 10 10 10 -10 10 10 出力例 1 0.50 2 つ場所があり,そのうちのどちらかに人を配置できる.この場合,どちらに配置しても確率は 1/2 になる. 入力例 2 2 2 10 10 10 -10 10 10 出力例 2 1.0 入力例 3 5 3 -10 -10 5 10 10 2 -10 10 3 -10 0 4 10 0 2 出力例 3 0.3574057314330 入力例 4 4 2 1 1 1 2 2 2 3 3 3 4 4 4 出力例 4 0.50
[ { "submission_id": "aoj_2276_4339412", "code_snippet": "#define _USE_MATH_DEFINES\n#include <bits/stdc++.h>\nusing namespace std;\n#define FOR(i,m,n) for(int i=(m);i<(n);++i)\n#define REP(i,n) FOR(i,0,n)\n#define ALL(v) (v).begin(),(v).end()\nusing ll = long long;\nconst int INF = 0x3f3f3f3f;\nconst ll LINF = 0x3f3f3f3f3f3f3f3fLL;\nconst double EPS = 1e-8;\nconst int MOD = 1000000007;\n// const int MOD = 998244353;\nconst int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};\nconst int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1};\ntemplate <typename T, typename U> inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; }\ntemplate <typename T, typename U> inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; }\nstruct IOSetup {\n IOSetup() {\n cin.tie(nullptr);\n ios_base::sync_with_stdio(false);\n cout << fixed << setprecision(20);\n }\n} iosetup;\n\nnamespace Geometry {\n using Real = double;\n\n int sgn(Real x) { return x > EPS ? 1 : x < -EPS ? -1 : 0; }\n\n Real degree_to_radian(Real d) { return d * M_PI / 180; }\n Real radian_to_degree(Real r) { return r * 180 / M_PI; }\n\n struct Point {\n Real x, y;\n Point(Real x = 0, Real y = 0) : x(x), y(y) {}\n Real abs() const { return sqrt(norm()); }\n Real arg() const { Real res = atan2(y, x); return res < 0 ? res + M_PI * 2 : res; }\n Real norm() const { return x * x + y * y; }\n Point rotate(Real angle) const { Real cs = cos(angle), sn = sin(angle); return Point(x * cs - y * sn, x * sn + y * cs); }\n Point unit_vector() const { Real a = abs(); return Point(x / a, y / a); }\n pair<Point, Point> normal_unit_vector() const { Point p = unit_vector(); return {Point(-p.y, p.x), Point(p.y, -p.x)}; }\n Point &operator+=(const Point &p) { x += p.x; y += p.y; return *this; }\n Point &operator-=(const Point &p) { x -= p.x; y -= p.y; return *this; }\n Point &operator*=(Real k) { x *= k; y *= k; return *this; }\n Point &operator/=(Real k) { x /= k; y /= k; return *this; }\n bool operator<(const Point &p) const { int x_sgn = sgn(p.x - x); return x_sgn != 0 ? x_sgn == 1 : sgn(p.y - y) == 1; }\n bool operator<=(const Point &p) const { return !(p < *this); }\n bool operator>(const Point &p) const { return p < *this; }\n bool operator>=(const Point &p) const { return !(*this < p); }\n Point operator+() const { return *this; }\n Point operator-() const { return Point(-x, -y); }\n Point operator+(const Point &p) const { return Point(*this) += p; }\n Point operator-(const Point &p) const { return Point(*this) -= p; }\n Point operator*(Real k) const { return Point(*this) *= k; }\n Point operator/(Real k) const { return Point(*this) /= k; }\n friend ostream &operator<<(ostream &os, const Point &p) { return os << '(' << p.x << \", \" << p.y << ')'; }\n friend istream &operator>>(istream &is, Point &p) { Real x, y; is >> x >> y; p = Point(x, y); return is; }\n };\n\n struct Segment {\n Point s, t;\n Segment(const Point &s = {0, 0}, const Point &t = {0, 0}) : s(s), t(t) {}\n };\n struct Line : Segment {\n using Segment::Segment;\n Line(Real a, Real b, Real c) {\n if (sgn(a) == 0) {\n s = Point(0, -c / b); t = Point(1, s.y);\n } else if (sgn(b) == 0) {\n s = Point(-c / a, 0); t = Point(s.x, 1);\n } else if (sgn(c) == 0) {\n s = Point(0, 0); t = Point(1, -a / b);\n } else {\n s = Point(0, -c / b); t = Point(-c / a, 0);\n }\n }\n };\n\n struct Circle {\n Point p; Real r;\n Circle(const Point &p = {0, 0}, Real r = 0) : p(p), r(r) {}\n };\n\n Real cross(const Point &a, const Point &b) { return a.x * b.y - a.y * b.x; }\n Real dot(const Point &a, const Point &b) { return a.x * b.x + a.y * b.y; }\n\n int ccw(const Point &a, const Point &b, const Point &c) {\n Point ab = b - a, ac = c - a;\n int sign = sgn(cross(ab, ac));\n if (sign == 0) {\n if (sgn(dot(ab, ac)) == -1) return 2;\n if (sgn(ac.norm() - ab.norm()) == 1) return -2;\n }\n return sign;\n }\n\n Real get_angle(const Point &a, const Point &b, const Point &c) {\n Real ba_arg = (a - b).arg(), bc_arg = (c - b).arg();\n if (ba_arg > bc_arg) swap(ba_arg, bc_arg);\n return min(bc_arg - ba_arg, M_PI * 2 - (bc_arg - ba_arg));\n }\n\n Real closest_pair(vector<Point> ps) {\n int n = ps.size();\n assert(n > 1);\n sort(ALL(ps));\n function<Real(int, int)> rec = [&](int left, int right) {\n int mid = (left + right) >> 1;\n Real x_mid = ps[mid].x, d = LINF;\n if (left + 1 < mid) chmin(d, rec(left, mid));\n if (mid + 1 < right) chmin(d, rec(mid, right));\n inplace_merge(ps.begin() + left, ps.begin() + mid, ps.begin() + right, [&](const Point &a, const Point &b) { return sgn(b.y - a.y) == 1; });\n vector<Point> tmp;\n FOR(i, left, right) {\n if (sgn(abs(ps[i].x - x_mid) - d) == 1) continue;\n for (int j = static_cast<int>(tmp.size()) - 1; j >= 0; --j) {\n Point now = ps[i] - tmp[j];\n if (sgn(now.y - d) == 1) break;\n chmin(d, now.abs());\n }\n tmp.emplace_back(ps[i]);\n }\n return d;\n };\n return rec(0, n);\n }\n\n Point projection(const Segment &a, const Point &b) { return a.s + (a.t - a.s) * dot(a.t - a.s, b - a.s) / (a.t - a.s).norm(); }\n Point reflection(const Segment &a, const Point &b) { return projection(a, b) * 2 - b; }\n\n bool is_parallel(const Segment &a, const Segment &b) { return sgn(cross(a.t - a.s, b.t - b.s)) == 0; }\n bool is_orthogonal(const Segment &a, const Segment &b) { return sgn(dot(a.t - a.s, b.t - b.s)) == 0; }\n\n Real distance(const Point&, const Point&);\n Real distance(const Segment&, const Point&);\n Real distance(const Line&, const Point&);\n int sizeof_common_tangent(const Circle&, const Circle&);\n bool has_intersected(const Segment &a, const Point &b) { return ccw(a.s, a.t, b) == 0; }\n bool has_intersected(const Segment &a, const Segment &b) { return ccw(a.s, a.t, b.s) * ccw(a.s, a.t, b.t) <= 0 && ccw(b.s, b.t, a.s) * ccw(b.s, b.t, a.t) <= 0; }\n bool has_intersected(const Line &a, const Point &b) { int c = ccw(a.s, a.t, b); return c != 1 && c != -1; }\n bool has_intersected(const Line &a, const Segment &b) { return ccw(a.s, a.t, b.s) * ccw(a.s, a.t, b.t) != 1; }\n bool has_intersected(const Line &a, const Line &b) { return sgn(cross(a.t - a.s, b.t - b.s)) != 0 || sgn(cross(a.t - a.s, b.s - a.s)) == 0; }\n bool has_intersected(const Circle &a, const Point &b) { return sgn(distance(a.p, b) - a.r) == 0; }\n bool has_intersected(const Circle &a, const Segment &b) { return sgn(a.r - distance(b, a.p)) != -1 && sgn(max(distance(a.p, b.s), distance(a.p, b.t)) - a.r) != -1; }\n bool has_intersected(const Circle &a, const Line &b) { return sgn(a.r - distance(b, a.p)) != -1; }\n bool has_intersected(const Circle &a, const Circle &b) { return sizeof_common_tangent(a, b) > 0; }\n\n Point intersection(const Line &a, const Line &b) {\n assert(has_intersected(a, b) && !is_parallel(a, b));\n return a.s + (a.t - a.s) * cross(b.t - b.s, b.s - a.s) / cross(b.t - b.s, a.t - a.s);\n }\n Point intersection(const Segment &a, const Segment &b) {\n assert(has_intersected(a, b));\n if (is_parallel(a, b)) {\n if (sgn(distance(a.s, b.s)) == 0) {\n assert(sgn(dot(a.t - a.s, b.t - a.s)) == -1);\n return a.s;\n } else if (sgn(distance(a.s, b.t)) == 0) {\n assert(sgn(dot(a.t - a.s, b.s - a.s)) == -1);\n return a.s;\n } else if (sgn(distance(a.t, b.s)) == 0) {\n assert(sgn(dot(a.s - a.t, b.t - a.t)) == -1);\n return a.t;\n } else if (sgn(distance(a.t, b.t)) == 0) {\n assert(sgn(dot(a.s - a.t, b.s - a.t)) == -1);\n return a.t;\n } else {\n assert(false);\n }\n } else {\n return intersection(Line(a.s, a.t), Line(b.s, b.t));\n }\n }\n Point intersection(const Line &a, const Segment &b) {\n assert(has_intersected(a, b));\n return intersection(a, Line(b.s, b.t));\n }\n vector<Point> intersection(const Circle &a, const Line &b) {\n Point pro = projection(b, a.p);\n Real nor = (a.p - pro).norm();\n int sign = sgn(a.r - sqrt(nor));\n if (sign == -1) return {};\n if (sign == 0) return {pro};\n Point v = (b.t - b.s).unit_vector() * sqrt(a.r * a.r - nor);\n return {pro + v, pro - v};\n }\n vector<Point> intersection(const Circle &a, const Segment &b) {\n if (!has_intersected(a, b)) return {};\n vector<Point> res = intersection(a, Line(b.s, b.t));\n if (sgn(distance(a.p, b.s) - a.r) != -1 && sgn(distance(a.p, b.t) - a.r) != -1) return res;\n return {sgn(dot(res[0] - b.s, res[0] - b.t)) == 1 ? res[1] : res[0]};\n }\n vector<Point> intersection(const Circle &a, const Circle &b) {\n int sz = sizeof_common_tangent(a, b);\n if (sz == 0 || sz == 4) return {};\n Real alpha = (b.p - a.p).arg();\n if (sz == 1 || sz == 3) return {Point(a.p.x + a.r * cos(alpha), a.p.y + a.r * sin(alpha))};\n Real dist = (b.p - a.p).norm(), beta = acos((dist + a.r * a.r - b.r * b.r) / (2 * sqrt(dist) * a.r));\n return {a.p + Point(a.r * cos(alpha + beta), a.r * sin(alpha + beta)), a.p + Point(a.r * cos(alpha - beta), a.r * sin(alpha - beta))};\n }\n\n Real distance(const Point &a, const Point &b) { return (b - a).abs(); }\n Real distance(const Segment &a, const Point &b) {\n Point foot = projection(a, b);\n return has_intersected(a, foot) ? distance(foot, b) : min(distance(a.s, b), distance(a.t, b));\n }\n Real distance(const Segment &a, const Segment &b) { return has_intersected(a, b) ? 0 : min({distance(a, b.s), distance(a, b.t), distance(b, a.s), distance(b, a.t)}); }\n Real distance(const Line &a, const Point &b) { return distance(projection(a, b), b); }\n Real distance(const Line &a, const Segment &b) { return has_intersected(a, b) ? 0 : min(distance(a, b.s), distance(a, b.t)); }\n Real distance(const Line &a, const Line &b) { return has_intersected(a, b) ? 0 : distance(a, b.s); }\n\n vector<Point> tangency(const Circle &a, const Point &b) {\n Real dist = distance(a.p, b);\n int sign = sgn(dist - a.r);\n if (sign == -1) return {};\n if (sign == 0) return {b};\n Real alpha = (b - a.p).arg(), beta = acos(a.r / dist);\n return {a.p + Point(a.r * cos(alpha + beta), a.r * sin(alpha + beta)), a.p + Point(a.r * cos(alpha - beta), a.r * sin(alpha - beta))};\n }\n int sizeof_common_tangent(const Circle &a, const Circle &b) {\n Real dist = distance(a.p, b.p);\n int sign = sgn(a.r + b.r - dist);\n if (sign == -1) return 4;\n if (sign == 0) return 3;\n sign = sgn((sgn(a.r - b.r) == -1 ? b.r - a.r : a.r - b.r) - dist);\n if (sign == -1) return 2;\n if (sign == 0) return 1;\n return 0;\n }\n vector<Line> common_tangent(const Circle &a, const Circle &b) {\n vector<Line> tangents;\n Real dist = distance(a.p, b.p), argument = (b.p - a.p).arg();\n int sign = sgn(a.r + b.r - dist);\n if (sign == -1) {\n Real ac = acos((a.r + b.r) / dist), alpha = argument + ac, cs = cos(alpha), sn = sin(alpha);\n tangents.emplace_back(a.p + Point(a.r * cs, a.r * sn), b.p + Point(-b.r * cs, -b.r * sn));\n alpha = argument - ac; cs = cos(alpha); sn = sin(alpha);\n tangents.emplace_back(a.p + Point(a.r * cs, a.r * sn), b.p + Point(-b.r * cs, -b.r * sn));\n } else if (sign == 0) {\n Point s = a.p + Point(a.r * cos(argument), a.r * sin(argument));\n tangents.emplace_back(s, s + (b.p - a.p).normal_unit_vector().first);\n }\n if (sgn(b.r - a.r) == -1) {\n sign = sgn(a.r - b.r - dist);\n if (sign == -1) {\n Real at = acos((a.r - b.r) / dist), alpha = argument + at, cs = cos(alpha), sn = sin(alpha);\n tangents.emplace_back(a.p + Point(a.r * cs, a.r * sn), b.p + Point(b.r * cs, b.r * sn));\n alpha = argument - at; cs = cos(alpha); sn = sin(alpha);\n tangents.emplace_back(a.p + Point(a.r * cs, a.r * sn), b.p + Point(b.r * cs, b.r * sn));\n } else if (sign == 0) {\n Point s = a.p + Point(a.r * cos(argument), a.r * sin(argument));\n tangents.emplace_back(s, s + (b.p - a.p).normal_unit_vector().first);\n }\n } else {\n sign = sgn(b.r - a.r - dist);\n if (sign == -1) {\n Real at = acos((b.r - a.r) / dist), alpha = argument - at, cs = cos(alpha), sn = sin(alpha);\n tangents.emplace_back(a.p + Point(-a.r * cs, -a.r * sn), b.p + Point(-b.r * cs, -b.r * sn));\n alpha = argument + at; cs = cos(alpha); sn = sin(alpha);\n tangents.emplace_back(a.p + Point(-a.r * cs, -a.r * sn), b.p + Point(-b.r * cs, -b.r * sn));\n } else if (sign == 0) {\n Point s = b.p + Point(-b.r * cos(argument), -b.r * sin(argument));\n tangents.emplace_back(s, s + (a.p - b.p).normal_unit_vector().first);\n }\n }\n return tangents;\n }\n\n Real intersection_area(const Circle &a, const Circle &b) {\n Real nor = (b.p - a.p).norm(), dist = sqrt(nor);\n if (sgn(a.r + b.r - dist) != 1) return 0;\n if (sgn(abs(a.r - b.r) - dist) != -1) return min(a.r, b.r) * min(a.r, b.r) * M_PI;\n Real alpha = acos((nor + a.r * a.r - b.r * b.r) / (2 * dist * a.r)), beta = acos((nor + b.r * b.r - a.r * a.r) / (2 * dist * b.r));\n return (alpha - sin(alpha + alpha) * 0.5) * a.r * a.r + (beta - sin(beta + beta) * 0.5) * b.r * b.r;\n }\n\n using Polygon = vector<Point>;\n\n Real area(const Polygon &a) {\n int n = a.size();\n Real res = 0;\n REP(i, n) res += cross(a[i], a[(i + 1) % n]);\n return res * 0.5;\n }\n\n Point centroid(const Polygon &a) {\n Point res(0, 0);\n int n = a.size();\n Real den = 0;\n REP(i, n) {\n Real cro = cross(a[i], a[(i + 1) % n]);\n res += (a[i] + a[(i + 1) % n]) / 3 * cro;\n den += cro;\n }\n return res / den;\n }\n\n int is_contained(const Polygon &a, const Point &b) {\n int n = a.size();\n bool is_in = false;\n REP(i, n) {\n Point p = a[i] - b, q = a[(i + 1) % n] - b;\n if (sgn(q.y - p.y) == -1) swap(p, q);\n int sign = sgn(cross(p, q));\n if (sign == 1 && sgn(p.y) != 1 && sgn(q.y) == 1) is_in = !is_in;\n if (sign == 0 && sgn(dot(p, q)) != 1) return 1;\n }\n return is_in ? 2 : 0;\n }\n\n bool is_convex(const Polygon &a) {\n int n = a.size();\n REP(i, n) {\n if (ccw(a[(i - 1 + n) % n], a[i], a[(i + 1) % n]) == -1) return false;\n }\n return true;\n }\n\n Polygon monotone_chain(vector<Point> ps, bool tight = true) {\n sort(ALL(ps));\n int n = ps.size(), idx = 0;\n Polygon convex_hull(n << 1);\n for (int i = 0; i < n; convex_hull[idx++] = ps[i++]) {\n while (idx >= 2 && sgn(cross(convex_hull[idx - 1] - convex_hull[idx - 2], ps[i] - convex_hull[idx - 1])) < tight) --idx;\n }\n for (int i = n - 2, border = idx + 1; i >= 0; convex_hull[idx++] = ps[i--]) {\n while (idx >= border && sgn(cross(convex_hull[idx - 1] - convex_hull[idx - 2], ps[i] - convex_hull[idx - 1])) < tight) --idx;\n }\n convex_hull.resize(idx - 1);\n return convex_hull;\n }\n\n Polygon cut_convex(const Polygon &a, const Line &b) {\n int n = a.size();\n Polygon res;\n REP(i, n) {\n int c = ccw(b.s, b.t, a[i]);\n if (c != -1) res.emplace_back(a[i]);\n if (c * ccw(b.s, b.t, a[(i + 1) % n]) == -1) res.emplace_back(intersection(Line(a[i], a[(i + 1) % n]), b));\n }\n if (res.size() < 3) res.clear();\n return res;\n }\n\n pair<Point, Point> rotating_calipers(const Polygon &a) {\n int n = a.size(), high = 0, low = 0;\n if (n <= 2) {\n assert(n == 2);\n return {a[0], a[1]};\n }\n FOR(i, 1, n) {\n if (a[i].y > a[high].y) high = i;\n if (a[i].y < a[low].y) low = i;\n }\n Real max_norm = (a[high] - a[low]).norm();\n int i = high, j = low, max_i = i, max_j = j;\n do {\n ((sgn(cross(a[(i + 1) % n] - a[i], a[(j + 1) % n] - a[j])) != -1 ? j : i) += 1) %= n;\n Real tmp = (a[j] - a[i]).norm();\n if (sgn(tmp - max_norm) == 1) {\n max_norm = tmp;\n max_i = i; max_j = j;\n }\n } while (i != high || j != low);\n return {a[max_i], a[max_j]};\n }\n}\n\nusing namespace Geometry;\n\nint main() {\n int n, k; cin >> n >> k;\n vector<Circle> people;\n REP(_, n) {\n Point p; double r; cin >> p >> r;\n people.emplace_back(p, r);\n }\n vector<double> l(n), r(n), comp;\n REP(i, n) {\n vector<Point> t = tangency(people[i], Point(0, 0));\n l[i] = t[0].arg(); r[i] = t[1].arg();\n if (l[i] > r[i]) swap(l[i], r[i]);\n if (r[i] > M_PI && has_intersected(people[i], Segment(Point(0, 0), Point(2415, 0)))) {\n swap(l[i], r[i]);\n l[i] = 0;\n } else {\n chmax(l[i], 0);\n chmin(r[i], M_PI);\n }\n if (l[i] < M_PI) {\n comp.emplace_back(l[i]);\n comp.emplace_back(r[i]);\n }\n }\n vector<bool> use(n, true);\n REP(i, n) {\n if (l[i] >= M_PI) use[i] = false;\n if (!use[i]) continue;\n REP(j, n) {\n if (j != i && l[i] <= l[j] && r[j] <= r[i]) use[j] = false;\n }\n }\n vector<pair<double, double>> ball;\n REP(i, n) {\n if (use[i]) ball.emplace_back(l[i], r[i]);\n }\n n = ball.size();\n if (n == 0) {\n cout << \"0\\n\";\n return 0;\n }\n sort(ALL(ball));\n // REP(i, n) cout << ball[i].first << ' ' << ball[i].second << '\\n';\n vector<vector<vector<double>>> dp(n, vector<vector<double>>(k + 1, vector<double>(2, -INF)));\n dp[0][0][false] = 0;\n dp[0][1][true] = ball[0].second - ball[0].first;\n FOR(i, 1, n) {\n double left, right; tie(left, right) = ball[i];\n int x = i - 1;\n while (x >= 0 && ball[i].first <= ball[x].second) --x;\n ++x;\n chmin(x, i - 1);\n REP(j, k + 1) {\n if (j + 1 <= k) {\n chmax(dp[i][j + 1][true], dp[x][j][true] + right - max(ball[x].second, left));\n chmax(dp[i][j + 1][true], dp[x][j][false] + right - left);\n }\n chmax(dp[i][j][false], max(dp[i - 1][j][false], dp[i - 1][j][true]));\n }\n }\n double ans = 0;\n REP(y, k + 1) REP(z, 2) chmax(ans, dp[n - 1][y][z]);\n cout << ans / M_PI << '\\n';\n return 0;\n}", "accuracy": 1, "time_ms": 110, "memory_kb": 127048, "score_of_the_acc": -1.1429, "final_rank": 15 }, { "submission_id": "aoj_2276_4339407", "code_snippet": "#define _USE_MATH_DEFINES\n#include <bits/stdc++.h>\nusing namespace std;\n#define FOR(i,m,n) for(int i=(m);i<(n);++i)\n#define REP(i,n) FOR(i,0,n)\n#define ALL(v) (v).begin(),(v).end()\nusing ll = long long;\nconst int INF = 0x3f3f3f3f;\nconst ll LINF = 0x3f3f3f3f3f3f3f3fLL;\nconst double EPS = 1e-8;\nconst int MOD = 1000000007;\n// const int MOD = 998244353;\nconst int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};\nconst int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1};\ntemplate <typename T, typename U> inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; }\ntemplate <typename T, typename U> inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; }\nstruct IOSetup {\n IOSetup() {\n cin.tie(nullptr);\n ios_base::sync_with_stdio(false);\n cout << fixed << setprecision(20);\n }\n} iosetup;\n\nnamespace Geometry {\n using Real = double;\n\n int sgn(Real x) { return x > EPS ? 1 : x < -EPS ? -1 : 0; }\n\n Real degree_to_radian(Real d) { return d * M_PI / 180; }\n Real radian_to_degree(Real r) { return r * 180 / M_PI; }\n\n struct Point {\n Real x, y;\n Point(Real x = 0, Real y = 0) : x(x), y(y) {}\n Real abs() const { return sqrt(norm()); }\n Real arg() const { Real res = atan2(y, x); return res < 0 ? res + M_PI * 2 : res; }\n Real norm() const { return x * x + y * y; }\n Point rotate(Real angle) const { Real cs = cos(angle), sn = sin(angle); return Point(x * cs - y * sn, x * sn + y * cs); }\n Point unit_vector() const { Real a = abs(); return Point(x / a, y / a); }\n pair<Point, Point> normal_unit_vector() const { Point p = unit_vector(); return {Point(-p.y, p.x), Point(p.y, -p.x)}; }\n Point &operator+=(const Point &p) { x += p.x; y += p.y; return *this; }\n Point &operator-=(const Point &p) { x -= p.x; y -= p.y; return *this; }\n Point &operator*=(Real k) { x *= k; y *= k; return *this; }\n Point &operator/=(Real k) { x /= k; y /= k; return *this; }\n bool operator<(const Point &p) const { int x_sgn = sgn(p.x - x); return x_sgn != 0 ? x_sgn == 1 : sgn(p.y - y) == 1; }\n bool operator<=(const Point &p) const { return !(p < *this); }\n bool operator>(const Point &p) const { return p < *this; }\n bool operator>=(const Point &p) const { return !(*this < p); }\n Point operator+() const { return *this; }\n Point operator-() const { return Point(-x, -y); }\n Point operator+(const Point &p) const { return Point(*this) += p; }\n Point operator-(const Point &p) const { return Point(*this) -= p; }\n Point operator*(Real k) const { return Point(*this) *= k; }\n Point operator/(Real k) const { return Point(*this) /= k; }\n friend ostream &operator<<(ostream &os, const Point &p) { return os << '(' << p.x << \", \" << p.y << ')'; }\n friend istream &operator>>(istream &is, Point &p) { Real x, y; is >> x >> y; p = Point(x, y); return is; }\n };\n\n struct Segment {\n Point s, t;\n Segment(const Point &s = {0, 0}, const Point &t = {0, 0}) : s(s), t(t) {}\n };\n struct Line : Segment {\n using Segment::Segment;\n Line(Real a, Real b, Real c) {\n if (sgn(a) == 0) {\n s = Point(0, -c / b); t = Point(1, s.y);\n } else if (sgn(b) == 0) {\n s = Point(-c / a, 0); t = Point(s.x, 1);\n } else if (sgn(c) == 0) {\n s = Point(0, 0); t = Point(1, -a / b);\n } else {\n s = Point(0, -c / b); t = Point(-c / a, 0);\n }\n }\n };\n\n struct Circle {\n Point p; Real r;\n Circle(const Point &p = {0, 0}, Real r = 0) : p(p), r(r) {}\n };\n\n Real cross(const Point &a, const Point &b) { return a.x * b.y - a.y * b.x; }\n Real dot(const Point &a, const Point &b) { return a.x * b.x + a.y * b.y; }\n\n int ccw(const Point &a, const Point &b, const Point &c) {\n Point ab = b - a, ac = c - a;\n int sign = sgn(cross(ab, ac));\n if (sign == 0) {\n if (sgn(dot(ab, ac)) == -1) return 2;\n if (sgn(ac.norm() - ab.norm()) == 1) return -2;\n }\n return sign;\n }\n\n Real get_angle(const Point &a, const Point &b, const Point &c) {\n Real ba_arg = (a - b).arg(), bc_arg = (c - b).arg();\n if (ba_arg > bc_arg) swap(ba_arg, bc_arg);\n return min(bc_arg - ba_arg, M_PI * 2 - (bc_arg - ba_arg));\n }\n\n Real closest_pair(vector<Point> ps) {\n int n = ps.size();\n assert(n > 1);\n sort(ALL(ps));\n function<Real(int, int)> rec = [&](int left, int right) {\n int mid = (left + right) >> 1;\n Real x_mid = ps[mid].x, d = LINF;\n if (left + 1 < mid) chmin(d, rec(left, mid));\n if (mid + 1 < right) chmin(d, rec(mid, right));\n inplace_merge(ps.begin() + left, ps.begin() + mid, ps.begin() + right, [&](const Point &a, const Point &b) { return sgn(b.y - a.y) == 1; });\n vector<Point> tmp;\n FOR(i, left, right) {\n if (sgn(abs(ps[i].x - x_mid) - d) == 1) continue;\n for (int j = static_cast<int>(tmp.size()) - 1; j >= 0; --j) {\n Point now = ps[i] - tmp[j];\n if (sgn(now.y - d) == 1) break;\n chmin(d, now.abs());\n }\n tmp.emplace_back(ps[i]);\n }\n return d;\n };\n return rec(0, n);\n }\n\n Point projection(const Segment &a, const Point &b) { return a.s + (a.t - a.s) * dot(a.t - a.s, b - a.s) / (a.t - a.s).norm(); }\n Point reflection(const Segment &a, const Point &b) { return projection(a, b) * 2 - b; }\n\n bool is_parallel(const Segment &a, const Segment &b) { return sgn(cross(a.t - a.s, b.t - b.s)) == 0; }\n bool is_orthogonal(const Segment &a, const Segment &b) { return sgn(dot(a.t - a.s, b.t - b.s)) == 0; }\n\n Real distance(const Point&, const Point&);\n Real distance(const Segment&, const Point&);\n Real distance(const Line&, const Point&);\n int sizeof_common_tangent(const Circle&, const Circle&);\n bool has_intersected(const Segment &a, const Point &b) { return ccw(a.s, a.t, b) == 0; }\n bool has_intersected(const Segment &a, const Segment &b) { return ccw(a.s, a.t, b.s) * ccw(a.s, a.t, b.t) <= 0 && ccw(b.s, b.t, a.s) * ccw(b.s, b.t, a.t) <= 0; }\n bool has_intersected(const Line &a, const Point &b) { int c = ccw(a.s, a.t, b); return c != 1 && c != -1; }\n bool has_intersected(const Line &a, const Segment &b) { return ccw(a.s, a.t, b.s) * ccw(a.s, a.t, b.t) != 1; }\n bool has_intersected(const Line &a, const Line &b) { return sgn(cross(a.t - a.s, b.t - b.s)) != 0 || sgn(cross(a.t - a.s, b.s - a.s)) == 0; }\n bool has_intersected(const Circle &a, const Point &b) { return sgn(distance(a.p, b) - a.r) == 0; }\n bool has_intersected(const Circle &a, const Segment &b) { return sgn(a.r - distance(b, a.p)) != -1 && sgn(max(distance(a.p, b.s), distance(a.p, b.t)) - a.r) != -1; }\n bool has_intersected(const Circle &a, const Line &b) { return sgn(a.r - distance(b, a.p)) != -1; }\n bool has_intersected(const Circle &a, const Circle &b) { return sizeof_common_tangent(a, b) > 0; }\n\n Point intersection(const Line &a, const Line &b) {\n assert(has_intersected(a, b) && !is_parallel(a, b));\n return a.s + (a.t - a.s) * cross(b.t - b.s, b.s - a.s) / cross(b.t - b.s, a.t - a.s);\n }\n Point intersection(const Segment &a, const Segment &b) {\n assert(has_intersected(a, b));\n if (is_parallel(a, b)) {\n if (sgn(distance(a.s, b.s)) == 0) {\n assert(sgn(dot(a.t - a.s, b.t - a.s)) == -1);\n return a.s;\n } else if (sgn(distance(a.s, b.t)) == 0) {\n assert(sgn(dot(a.t - a.s, b.s - a.s)) == -1);\n return a.s;\n } else if (sgn(distance(a.t, b.s)) == 0) {\n assert(sgn(dot(a.s - a.t, b.t - a.t)) == -1);\n return a.t;\n } else if (sgn(distance(a.t, b.t)) == 0) {\n assert(sgn(dot(a.s - a.t, b.s - a.t)) == -1);\n return a.t;\n } else {\n assert(false);\n }\n } else {\n return intersection(Line(a.s, a.t), Line(b.s, b.t));\n }\n }\n Point intersection(const Line &a, const Segment &b) {\n assert(has_intersected(a, b));\n return intersection(a, Line(b.s, b.t));\n }\n vector<Point> intersection(const Circle &a, const Line &b) {\n Point pro = projection(b, a.p);\n Real nor = (a.p - pro).norm();\n int sign = sgn(a.r - sqrt(nor));\n if (sign == -1) return {};\n if (sign == 0) return {pro};\n Point v = (b.t - b.s).unit_vector() * sqrt(a.r * a.r - nor);\n return {pro + v, pro - v};\n }\n vector<Point> intersection(const Circle &a, const Segment &b) {\n if (!has_intersected(a, b)) return {};\n vector<Point> res = intersection(a, Line(b.s, b.t));\n if (sgn(distance(a.p, b.s) - a.r) != -1 && sgn(distance(a.p, b.t) - a.r) != -1) return res;\n return {sgn(dot(res[0] - b.s, res[0] - b.t)) == 1 ? res[1] : res[0]};\n }\n vector<Point> intersection(const Circle &a, const Circle &b) {\n int sz = sizeof_common_tangent(a, b);\n if (sz == 0 || sz == 4) return {};\n Real alpha = (b.p - a.p).arg();\n if (sz == 1 || sz == 3) return {Point(a.p.x + a.r * cos(alpha), a.p.y + a.r * sin(alpha))};\n Real dist = (b.p - a.p).norm(), beta = acos((dist + a.r * a.r - b.r * b.r) / (2 * sqrt(dist) * a.r));\n return {a.p + Point(a.r * cos(alpha + beta), a.r * sin(alpha + beta)), a.p + Point(a.r * cos(alpha - beta), a.r * sin(alpha - beta))};\n }\n\n Real distance(const Point &a, const Point &b) { return (b - a).abs(); }\n Real distance(const Segment &a, const Point &b) {\n Point foot = projection(a, b);\n return has_intersected(a, foot) ? distance(foot, b) : min(distance(a.s, b), distance(a.t, b));\n }\n Real distance(const Segment &a, const Segment &b) { return has_intersected(a, b) ? 0 : min({distance(a, b.s), distance(a, b.t), distance(b, a.s), distance(b, a.t)}); }\n Real distance(const Line &a, const Point &b) { return distance(projection(a, b), b); }\n Real distance(const Line &a, const Segment &b) { return has_intersected(a, b) ? 0 : min(distance(a, b.s), distance(a, b.t)); }\n Real distance(const Line &a, const Line &b) { return has_intersected(a, b) ? 0 : distance(a, b.s); }\n\n vector<Point> tangency(const Circle &a, const Point &b) {\n Real dist = distance(a.p, b);\n int sign = sgn(dist - a.r);\n if (sign == -1) return {};\n if (sign == 0) return {b};\n Real alpha = (b - a.p).arg(), beta = acos(a.r / dist);\n return {a.p + Point(a.r * cos(alpha + beta), a.r * sin(alpha + beta)), a.p + Point(a.r * cos(alpha - beta), a.r * sin(alpha - beta))};\n }\n int sizeof_common_tangent(const Circle &a, const Circle &b) {\n Real dist = distance(a.p, b.p);\n int sign = sgn(a.r + b.r - dist);\n if (sign == -1) return 4;\n if (sign == 0) return 3;\n sign = sgn((sgn(a.r - b.r) == -1 ? b.r - a.r : a.r - b.r) - dist);\n if (sign == -1) return 2;\n if (sign == 0) return 1;\n return 0;\n }\n vector<Line> common_tangent(const Circle &a, const Circle &b) {\n vector<Line> tangents;\n Real dist = distance(a.p, b.p), argument = (b.p - a.p).arg();\n int sign = sgn(a.r + b.r - dist);\n if (sign == -1) {\n Real ac = acos((a.r + b.r) / dist), alpha = argument + ac, cs = cos(alpha), sn = sin(alpha);\n tangents.emplace_back(a.p + Point(a.r * cs, a.r * sn), b.p + Point(-b.r * cs, -b.r * sn));\n alpha = argument - ac; cs = cos(alpha); sn = sin(alpha);\n tangents.emplace_back(a.p + Point(a.r * cs, a.r * sn), b.p + Point(-b.r * cs, -b.r * sn));\n } else if (sign == 0) {\n Point s = a.p + Point(a.r * cos(argument), a.r * sin(argument));\n tangents.emplace_back(s, s + (b.p - a.p).normal_unit_vector().first);\n }\n if (sgn(b.r - a.r) == -1) {\n sign = sgn(a.r - b.r - dist);\n if (sign == -1) {\n Real at = acos((a.r - b.r) / dist), alpha = argument + at, cs = cos(alpha), sn = sin(alpha);\n tangents.emplace_back(a.p + Point(a.r * cs, a.r * sn), b.p + Point(b.r * cs, b.r * sn));\n alpha = argument - at; cs = cos(alpha); sn = sin(alpha);\n tangents.emplace_back(a.p + Point(a.r * cs, a.r * sn), b.p + Point(b.r * cs, b.r * sn));\n } else if (sign == 0) {\n Point s = a.p + Point(a.r * cos(argument), a.r * sin(argument));\n tangents.emplace_back(s, s + (b.p - a.p).normal_unit_vector().first);\n }\n } else {\n sign = sgn(b.r - a.r - dist);\n if (sign == -1) {\n Real at = acos((b.r - a.r) / dist), alpha = argument - at, cs = cos(alpha), sn = sin(alpha);\n tangents.emplace_back(a.p + Point(-a.r * cs, -a.r * sn), b.p + Point(-b.r * cs, -b.r * sn));\n alpha = argument + at; cs = cos(alpha); sn = sin(alpha);\n tangents.emplace_back(a.p + Point(-a.r * cs, -a.r * sn), b.p + Point(-b.r * cs, -b.r * sn));\n } else if (sign == 0) {\n Point s = b.p + Point(-b.r * cos(argument), -b.r * sin(argument));\n tangents.emplace_back(s, s + (a.p - b.p).normal_unit_vector().first);\n }\n }\n return tangents;\n }\n\n Real intersection_area(const Circle &a, const Circle &b) {\n Real nor = (b.p - a.p).norm(), dist = sqrt(nor);\n if (sgn(a.r + b.r - dist) != 1) return 0;\n if (sgn(abs(a.r - b.r) - dist) != -1) return min(a.r, b.r) * min(a.r, b.r) * M_PI;\n Real alpha = acos((nor + a.r * a.r - b.r * b.r) / (2 * dist * a.r)), beta = acos((nor + b.r * b.r - a.r * a.r) / (2 * dist * b.r));\n return (alpha - sin(alpha + alpha) * 0.5) * a.r * a.r + (beta - sin(beta + beta) * 0.5) * b.r * b.r;\n }\n\n using Polygon = vector<Point>;\n\n Real area(const Polygon &a) {\n int n = a.size();\n Real res = 0;\n REP(i, n) res += cross(a[i], a[(i + 1) % n]);\n return res * 0.5;\n }\n\n Point centroid(const Polygon &a) {\n Point res(0, 0);\n int n = a.size();\n Real den = 0;\n REP(i, n) {\n Real cro = cross(a[i], a[(i + 1) % n]);\n res += (a[i] + a[(i + 1) % n]) / 3 * cro;\n den += cro;\n }\n return res / den;\n }\n\n int is_contained(const Polygon &a, const Point &b) {\n int n = a.size();\n bool is_in = false;\n REP(i, n) {\n Point p = a[i] - b, q = a[(i + 1) % n] - b;\n if (sgn(q.y - p.y) == -1) swap(p, q);\n int sign = sgn(cross(p, q));\n if (sign == 1 && sgn(p.y) != 1 && sgn(q.y) == 1) is_in = !is_in;\n if (sign == 0 && sgn(dot(p, q)) != 1) return 1;\n }\n return is_in ? 2 : 0;\n }\n\n bool is_convex(const Polygon &a) {\n int n = a.size();\n REP(i, n) {\n if (ccw(a[(i - 1 + n) % n], a[i], a[(i + 1) % n]) == -1) return false;\n }\n return true;\n }\n\n Polygon monotone_chain(vector<Point> ps, bool tight = true) {\n sort(ALL(ps));\n int n = ps.size(), idx = 0;\n Polygon convex_hull(n << 1);\n for (int i = 0; i < n; convex_hull[idx++] = ps[i++]) {\n while (idx >= 2 && sgn(cross(convex_hull[idx - 1] - convex_hull[idx - 2], ps[i] - convex_hull[idx - 1])) < tight) --idx;\n }\n for (int i = n - 2, border = idx + 1; i >= 0; convex_hull[idx++] = ps[i--]) {\n while (idx >= border && sgn(cross(convex_hull[idx - 1] - convex_hull[idx - 2], ps[i] - convex_hull[idx - 1])) < tight) --idx;\n }\n convex_hull.resize(idx - 1);\n return convex_hull;\n }\n\n Polygon cut_convex(const Polygon &a, const Line &b) {\n int n = a.size();\n Polygon res;\n REP(i, n) {\n int c = ccw(b.s, b.t, a[i]);\n if (c != -1) res.emplace_back(a[i]);\n if (c * ccw(b.s, b.t, a[(i + 1) % n]) == -1) res.emplace_back(intersection(Line(a[i], a[(i + 1) % n]), b));\n }\n if (res.size() < 3) res.clear();\n return res;\n }\n\n pair<Point, Point> rotating_calipers(const Polygon &a) {\n int n = a.size(), high = 0, low = 0;\n if (n <= 2) {\n assert(n == 2);\n return {a[0], a[1]};\n }\n FOR(i, 1, n) {\n if (a[i].y > a[high].y) high = i;\n if (a[i].y < a[low].y) low = i;\n }\n Real max_norm = (a[high] - a[low]).norm();\n int i = high, j = low, max_i = i, max_j = j;\n do {\n ((sgn(cross(a[(i + 1) % n] - a[i], a[(j + 1) % n] - a[j])) != -1 ? j : i) += 1) %= n;\n Real tmp = (a[j] - a[i]).norm();\n if (sgn(tmp - max_norm) == 1) {\n max_norm = tmp;\n max_i = i; max_j = j;\n }\n } while (i != high || j != low);\n return {a[max_i], a[max_j]};\n }\n}\n\nusing namespace Geometry;\n\nint main() {\n int n, k; cin >> n >> k;\n vector<Circle> people;\n REP(_, n) {\n Point p; double r; cin >> p >> r;\n people.emplace_back(p, r);\n }\n vector<double> l(n), r(n), comp;\n REP(i, n) {\n vector<Point> t = tangency(people[i], Point(0, 0));\n l[i] = t[0].arg(); r[i] = t[1].arg();\n if (l[i] > r[i]) swap(l[i], r[i]);\n if (r[i] > M_PI && has_intersected(people[i], Segment(Point(0, 0), Point(2415, 0)))) {\n swap(l[i], r[i]);\n l[i] = 0;\n } else {\n chmax(l[i], 0);\n chmin(r[i], M_PI);\n }\n if (l[i] < M_PI) {\n comp.emplace_back(l[i]);\n comp.emplace_back(r[i]);\n }\n }\n vector<bool> use(n, true);\n REP(i, n) {\n if (l[i] >= M_PI) use[i] = false;\n REP(j, n) {\n if (j != i && l[i] <= l[j] && r[j] <= r[i]) use[j] = false;\n }\n }\n vector<pair<double, double>> ball;\n REP(i, n) {\n if (use[i]) ball.emplace_back(l[i], r[i]);\n }\n n = ball.size();\n sort(ALL(ball));\n // REP(i, n) cout << ball[i].first << ' ' << ball[i].second << '\\n';\n vector<vector<vector<double>>> dp(n, vector<vector<double>>(k + 1, vector<double>(2, -INF)));\n dp[0][0][false] = 0;\n dp[0][1][true] = ball[0].second - ball[0].first;\n FOR(i, 1, n) {\n double left, right; tie(left, right) = ball[i];\n int x = i - 1;\n while (x >= 0 && ball[i].first <= ball[x].second) --x;\n ++x;\n chmin(x, i - 1);\n REP(j, k + 1) {\n if (j + 1 <= k) {\n chmax(dp[i][j + 1][true], dp[x][j][true] + right - max(ball[x].second, left));\n chmax(dp[i][j + 1][true], dp[x][j][false] + right - left);\n }\n chmax(dp[i][j][false], max(dp[i - 1][j][false], dp[i - 1][j][true]));\n }\n }\n double ans = 0;\n REP(y, k + 1) REP(z, 2) chmax(ans, dp[n - 1][y][z]);\n cout << ans / M_PI << '\\n';\n return 0;\n}", "accuracy": 0.8028169014084507, "time_ms": 40, "memory_kb": 47752, "score_of_the_acc": -0.2978, "final_rank": 16 }, { "submission_id": "aoj_2276_4329154", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 99999999999999999\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n\n#define SIZE 1505\n\nstruct Point{\n Point(double arg_x,double arg_y){\n x = arg_x;\n y = arg_y;\n }\n\n Point(){\n x = y = 0.0;\n }\n\n Point operator + (Point p){ return Point(x+p.x,y+p.y); }\n Point operator - (Point p){ return Point(x-p.x,y-p.y);}\n Point operator * (double a){ return Point(a*x,a*y); }\n Point operator / (double a){ return Point(x/a,y/a); }\n\n double abs(){ return sqrt(norm()); }\n double norm(){ return x*x + y*y; }\n\n bool operator<(const Point &p) const{\n return x != p.x? x < p.x: y < p.y;\n }\n\n bool operator == (const Point &p) const{\n return fabs(x-p.x) < EPS && fabs(y-p.y) < EPS;\n }\n\n double x,y;\n};\n\ntypedef Point Vector;\n\nstruct Sphere{\n Point center;\n double r;\n};\n\ntypedef Sphere Circle;\n\nstruct Line{\npublic:\n Point p[2];\n Line(Point p1,Point p2){\n p[0] = p1;\n p[1] = p2;\n }\n Line(){\n\n }\n};\n\nstruct Info{\n\tbool operator<(const struct Info &arg) const{\n\n\t\treturn left > arg.left;\n\t}\n\tdouble left,right;\n};\n\nint N,K;\nint most_L[SIZE];\nvector<Info> work,V;\ndouble dp1[SIZE][SIZE],dp2[SIZE][SIZE];\nCircle circle[SIZE];\n\n\ndouble NUM = 10000;\n\ndouble norm(Vector a){\n return a.x*a.x+a.y*a.y;\n}\n\ndouble abs(Vector a){\n return sqrt(norm(a));\n}\n\ndouble cross(Vector a,Vector b){\n return a.x*b.y-a.y*b.x;\n}\n\ndouble dot(Vector a,Vector b){\n return a.x*b.x + a.y*b.y;\n}\n\nPoint project(Line l,Point p){\n\n Vector base = l.p[1]-l.p[0];\n double r = dot(p-l.p[0],base)/norm(base);\n return l.p[0]+base*r;\n}\n\n//円と直線の交点を求める関数\nvector<Point> getCrossPoints(Circle c,Line l){\n\n vector<Point> ret;\n\n Vector pr = project(l,c.center);\n Vector e = (l.p[1]-l.p[0])/abs(l.p[1]-l.p[0]);\n\n double base;\n\n if(fabs(c.r*c.r-norm(pr-c.center)) < EPS){\n\n base = 0;\n }else{\n base = sqrt(c.r*c.r-norm(pr-c.center));\n }\n\n ret.push_back(Point(pr+e*base));\n ret.push_back(Point(pr-e*base));\n\n return ret;\n}\n\ndouble calc_dist(Point A,Point B){\n\n\treturn sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y));\n}\n\nint calc_area(double deg){\n\n\tif(deg >= 0 && deg <= 90){\n\n\t\treturn 1;\n\t}else if(deg <= 180){\n\n\t\treturn 2;\n\t}else if(deg <= 270){\n\n\t\treturn 3;\n\t}else{\n\n\t\treturn 4;\n\t}\n}\n\nint main(){\n\n\tPoint base = Point(0,0);\n\n\tscanf(\"%d %d\",&N,&K);\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tscanf(\"%lf %lf %lf\",&circle[i].center.x,&circle[i].center.y,&circle[i].r);\n\t\tif(calc_dist(base,circle[i].center) < circle[i].r){\n\n\t\t\tprintf(\"1.0\\n\");\n\t\t\treturn 0;\n\t\t}\n\t}\n\n\tint area1,area2;\n\n\tLine line = Line(Point(0,-10000),Point(0,10000));\n\n\tvector<Point> P;\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tdouble angle1 = atan2(circle[i].center.y,circle[i].center.x)*(180/M_PI);\n\t\tif(angle1 < 0){\n\n\t\t\tangle1 += 360;\n\t\t}\n\t\tdouble angle2 = asin(circle[i].r/abs(circle[i].center))*(180/M_PI);\n\t\tif(angle2 < 0){\n\n\t\t\tangle2 += 360;\n\t\t}\n\n\t\tdouble deg1 = angle1+angle2;\n\t\tif(deg1 >= 360){\n\n\t\t\tdeg1 -= 360;\n\t\t}\n\n\t\tdouble deg2 = angle1-angle2;\n\t\tif(deg2 < 0){\n\n\t\t\tdeg2 += 360;\n\t\t}\n\n\t\tarea1 = calc_area(deg1);\n\t\tarea2 = calc_area(deg2);\n\n\n\t\tif(area1 != 1 && area2 != 1 && area1 !=2 && area2 != 2)continue;\n\n\t\tif(area1 > area2){\n\t\t\tswap(area1,area2);\n\t\t\tswap(deg1,deg2);\n\t\t}\n\n\t\tInfo info;\n\t\tinfo.right = deg1;\n\n\t\tif(area1 == 1){\n\n\t\t\tswitch(area2){\n\t\t\tcase 1:\n\t\t\tcase 2:\n\t\t\t\tinfo.left = deg2;\n\t\t\t\tbreak;\n\t\t\tcase 3:\n\t\t\t\tP = getCrossPoints(circle[i],line); //注意\n\n\t\t\t\tif(P[0].y > 0 && P[1].y > 0){\n\n\t\t\t\t\tinfo.left = 180;\n\n\t\t\t\t}else{\n\n\t\t\t\t\tinfo.left = 0;\n\t\t\t\t}\n\n\t\t\t\tbreak;\n\t\t\tcase 4:\n\t\t\t\tinfo.left = 0;\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t}else{ //area1 == 2\n\n\t\t\tswitch(area2){\n\t\t\tcase 2:\n\t\t\t\tinfo.left = deg2;\n\t\t\t\tbreak;\n\t\t\tcase 3:\n\t\t\t\tinfo.left = 180;\n\t\t\t\tbreak;\n\t\t\tcase 4:\n\t\t\t\tP = getCrossPoints(circle[i],line); //注意\n\n\t\t\t\tif(P[0].y > 0 && P[1].y > 0){\n\n\t\t\t\t\tinfo.left = 0;\n\n\t\t\t\t}else{\n\n\t\t\t\t\tinfo.left = 180;\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\n\t\tif(info.right > info.left){\n\n\t\t\tswap(info.left,info.right);\n\t\t}\n\t\twork.push_back(info);\n\t}\n\n\tbool FLG;\n\t//他でカバーできる接線のペアは除く\n\tfor(int i = 0; i < work.size(); i++){\n\t\tFLG = false;\n\t\tfor(int k = i+1; k < work.size(); k++){\n\t\t\tif(k == i)continue;\n\n\t\t\tif(work[k].left >= work[i].left && work[k].right <= work[i].right){\n\n\t\t\t\tFLG = true;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tif(!FLG){\n\n\t\t\tV.push_back(work[i]);\n\t\t}\n\t}\n\n\tsort(V.begin(),V.end()); //leftの降順\n\n\tfor(int i = 0; i < V.size(); i++){\n\t\tmost_L[i] = -1;\n\t\tfor(int k = 0; k < i; k++){\n\t\t\tif(V[k].right > V[i].left+EPS)continue;\n\n\t\t\tmost_L[i] = k;\n\t\t\tbreak;\n\t\t}\n\t}\n\n\tfor(int i = 0; i <= K; i++){\n\t\tfor(int k = 0; k <= V.size(); k++){\n\t\t\tdp1[i][k] = 0;\n\t\t\tdp2[i][k] = 0;\n\t\t}\n\t}\n\n\tdouble maximum = 0;\n\n\tfor(int i = 1; i <= K; i++){ //使用個数\n\t\tfor(int k = 1; k <= V.size(); k++){ //最後のindex(本来のindex+1)\n\t\t\tif(most_L[k-1] == -1){\n\n\t\t\t\tdp1[i][k] = dp2[i-1][k-1]+V[k-1].left-V[k-1].right;\n\n\t\t\t}else{ //重なりを持つ区間あり\n\n\t\t\t\tdp1[i][k] = max(dp1[i-1][most_L[k-1]+1]+V[most_L[k-1]].right-V[k-1].right,\n\t\t\t\t\t\tdp2[i-1][most_L[k-1]]+V[k-1].left-V[k-1].right);\n\t\t\t}\n\t\t\tdp2[i][k] = max(dp1[i][k],dp2[i][k-1]);\n\n\t\t\tmaximum = max(maximum,dp2[i][k]);\n\t\t}\n\t}\n\n\tprintf(\"%.10lf\\n\",maximum/180.0);\n\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 39032, "score_of_the_acc": -0.1731, "final_rank": 8 }, { "submission_id": "aoj_2276_4329153", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 99999999999999999\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n\n#define SIZE 1505\n\nstruct Point{\n Point(double arg_x,double arg_y){\n x = arg_x;\n y = arg_y;\n }\n\n Point(){\n x = y = 0.0;\n }\n\n Point operator + (Point p){ return Point(x+p.x,y+p.y); }\n Point operator - (Point p){ return Point(x-p.x,y-p.y);}\n Point operator * (double a){ return Point(a*x,a*y); }\n Point operator / (double a){ return Point(x/a,y/a); }\n\n double abs(){ return sqrt(norm()); }\n double norm(){ return x*x + y*y; }\n\n bool operator<(const Point &p) const{\n return x != p.x? x < p.x: y < p.y;\n }\n\n bool operator == (const Point &p) const{\n return fabs(x-p.x) < EPS && fabs(y-p.y) < EPS;\n }\n\n double x,y;\n};\n\ntypedef Point Vector;\n\nstruct Sphere{\n Point center;\n double r;\n};\n\ntypedef Sphere Circle;\n\nstruct Line{\npublic:\n Point p[2];\n Line(Point p1,Point p2){\n p[0] = p1;\n p[1] = p2;\n }\n Line(){\n\n }\n};\n\nstruct Info{\n\tbool operator<(const struct Info &arg) const{\n\n\t\treturn left > arg.left;\n\t}\n\tdouble left,right;\n};\n\nint N,K;\nint most_L[SIZE];\nvector<Info> work,V;\ndouble dp1[SIZE][SIZE],dp2[SIZE][SIZE];\nCircle circle[SIZE];\n\n\ndouble NUM = 10000;\n\ndouble norm(Vector a){\n return a.x*a.x+a.y*a.y;\n}\n\ndouble abs(Vector a){\n return sqrt(norm(a));\n}\n\ndouble cross(Vector a,Vector b){\n return a.x*b.y-a.y*b.x;\n}\n\ndouble dot(Vector a,Vector b){\n return a.x*b.x + a.y*b.y;\n}\n\nPoint project(Line l,Point p){\n\n Vector base = l.p[1]-l.p[0];\n double r = dot(p-l.p[0],base)/norm(base);\n return l.p[0]+base*r;\n}\n\n//円と直線の交点を求める関数\nvector<Point> getCrossPoints(Circle c,Line l){\n\n vector<Point> ret;\n\n Vector pr = project(l,c.center);\n Vector e = (l.p[1]-l.p[0])/abs(l.p[1]-l.p[0]);\n\n double base;\n\n if(fabs(c.r*c.r-norm(pr-c.center)) < EPS){\n\n base = 0;\n }else{\n base = sqrt(c.r*c.r-norm(pr-c.center));\n }\n\n ret.push_back(Point(pr+e*base));\n ret.push_back(Point(pr-e*base));\n\n return ret;\n}\n\ndouble calc_dist(Point A,Point B){\n\n\treturn sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y));\n}\n\nint calc_area(double deg){\n\n\tif(deg >= 0 && deg <= 90){\n\n\t\treturn 1;\n\t}else if(deg <= 180){\n\n\t\treturn 2;\n\t}else if(deg <= 270){\n\n\t\treturn 3;\n\t}else{\n\n\t\treturn 4;\n\t}\n}\n\n\n//第1,第2象限の角度を求める\ndouble calc_angle(Vector vec){\n\n\tif(fabs(vec.x) < EPS){\n\n\t\treturn 90;\n\t}\n\tdouble ret = atan(fabs(vec.y)/fabs(vec.x))*(180/M_PI);\n\n\tif(vec.x < 0){\n\n\t\tret = 180-ret;\n\t}\n\treturn ret;\n}\n\n\n\nint main(){\n\n\tPoint base = Point(0,0);\n\n\tscanf(\"%d %d\",&N,&K);\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tscanf(\"%lf %lf %lf\",&circle[i].center.x,&circle[i].center.y,&circle[i].r);\n\t\tif(calc_dist(base,circle[i].center) < circle[i].r){\n\n\t\t\tprintf(\"1.0\\n\");\n\t\t\treturn 0;\n\t\t}\n\t}\n\n\tint area1,area2;\n\n\tLine line = Line(Point(0,-10000),Point(0,10000));\n\n\tvector<Point> P;\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tdouble angle1 = atan2(circle[i].center.y,circle[i].center.x)*(180/M_PI);\n\t\tif(angle1 < 0){\n\n\t\t\tangle1 += 360;\n\t\t}\n\t\tdouble angle2 = asin(circle[i].r/abs(circle[i].center))*(180/M_PI);\n\t\tif(angle2 < 0){\n\n\t\t\tangle2 += 360;\n\t\t}\n\n\t\tdouble deg1 = angle1+angle2;\n\t\tif(deg1 >= 360){\n\n\t\t\tdeg1 -= 360;\n\t\t}\n\n\t\tdouble deg2 = angle1-angle2;\n\t\tif(deg2 < 0){\n\n\t\t\tdeg2 += 360;\n\t\t}\n\n\t\tarea1 = calc_area(deg1);\n\t\tarea2 = calc_area(deg2);\n\n\n\t\tif(area1 != 1 && area2 != 1 && area1 !=2 && area2 != 2)continue;\n\n\t\tif(area1 > area2){\n\t\t\tswap(area1,area2);\n\t\t\tswap(deg1,deg2);\n\t\t}\n\n\t\tInfo info;\n\t\tinfo.right = deg1;\n\n\t\tif(area1 == 1){\n\n\t\t\tswitch(area2){\n\t\t\tcase 1:\n\t\t\tcase 2:\n\t\t\t\tinfo.left = deg2;\n\t\t\t\tbreak;\n\t\t\tcase 3:\n\t\t\t\tP = getCrossPoints(circle[i],line);\n\n\t\t\t\tif(P[0].y > 0 && P[1].y > 0){\n\n\t\t\t\t\tinfo.left = 180;\n\n\t\t\t\t}else{\n\n\t\t\t\t\tinfo.left = 0;\n\t\t\t\t}\n\n\t\t\t\tbreak;\n\t\t\tcase 4:\n\t\t\t\tinfo.left = 0;\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t}else{ //area1 == 2\n\n\t\t\tswitch(area2){\n\t\t\tcase 2:\n\t\t\t\tinfo.left = deg2;\n\t\t\t\tbreak;\n\t\t\tcase 3:\n\t\t\t\tinfo.left = 180;\n\t\t\t\tbreak;\n\t\t\tcase 4:\n\t\t\t\tP = getCrossPoints(circle[i],line);\n\n\t\t\t\tif(P[0].y > 0 && P[1].y > 0){\n\n\t\t\t\t\tinfo.left = 0;\n\n\t\t\t\t}else{\n\n\t\t\t\t\tinfo.left = 180;\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\n\t\tif(info.right > info.left){\n\n\t\t\tswap(info.left,info.right);\n\t\t}\n\t\twork.push_back(info);\n\t}\n\n\tbool FLG;\n\t//他でカバーできる接線のペアは除く\n\tfor(int i = 0; i < work.size(); i++){\n\t\tFLG = false;\n\t\tfor(int k = i+1; k < work.size(); k++){\n\t\t\tif(k == i)continue;\n\n\t\t\tif(work[k].left >= work[i].left && work[k].right <= work[i].right){\n\n\t\t\t\tFLG = true;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tif(!FLG){\n\n\t\t\tV.push_back(work[i]);\n\t\t}\n\t}\n\n\tsort(V.begin(),V.end()); //leftの降順\n\n\tfor(int i = 0; i < V.size(); i++){\n\t\tmost_L[i] = -1;\n\t\tfor(int k = 0; k < i; k++){\n\t\t\tif(V[k].right > V[i].left+EPS)continue;\n\n\t\t\tmost_L[i] = k;\n\t\t\tbreak;\n\t\t}\n\t}\n\n\tfor(int i = 0; i <= K; i++){\n\t\tfor(int k = 0; k <= V.size(); k++){\n\t\t\tdp1[i][k] = 0;\n\t\t\tdp2[i][k] = 0;\n\t\t}\n\t}\n\n\tdouble maximum = 0;\n\n\tfor(int i = 1; i <= K; i++){ //使用個数\n\t\tfor(int k = 1; k <= V.size(); k++){ //最後のindex(本来のindex+1)\n\t\t\tif(most_L[k-1] == -1){\n\n\t\t\t\tdp1[i][k] = dp2[i-1][k-1]+V[k-1].left-V[k-1].right;\n\n\t\t\t}else{ //重なりを持つ区間あり\n\n\t\t\t\tdp1[i][k] = max(dp1[i-1][most_L[k-1]+1]+V[most_L[k-1]].right-V[k-1].right,\n\t\t\t\t\t\tdp2[i-1][most_L[k-1]]+V[k-1].left-V[k-1].right);\n\t\t\t}\n\t\t\tdp2[i][k] = max(dp1[i][k],dp2[i][k-1]);\n\n\t\t\tmaximum = max(maximum,dp2[i][k]);\n\t\t}\n\t}\n\n\tprintf(\"%.10lf\\n\",maximum/180.0);\n\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 39064, "score_of_the_acc": -0.1734, "final_rank": 10 }, { "submission_id": "aoj_2276_4329029", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 99999999999999999\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n\n#define SIZE 1505\n\nstruct Point{\n Point(double arg_x,double arg_y){\n x = arg_x;\n y = arg_y;\n }\n\n Point(){\n x = y = 0.0;\n }\n\n Point operator + (Point p){ return Point(x+p.x,y+p.y); }\n Point operator - (Point p){ return Point(x-p.x,y-p.y);}\n Point operator * (double a){ return Point(a*x,a*y); }\n Point operator / (double a){ return Point(x/a,y/a); }\n\n double abs(){ return sqrt(norm()); }\n double norm(){ return x*x + y*y; }\n\n bool operator<(const Point &p) const{\n return x != p.x? x < p.x: y < p.y;\n }\n\n bool operator == (const Point &p) const{\n return fabs(x-p.x) < EPS && fabs(y-p.y) < EPS;\n }\n\n double x,y;\n};\n\ntypedef Point Vector;\n\nstruct Sphere{\n Point center;\n double r;\n};\n\ntypedef Sphere Circle;\n\nstruct Line{\npublic:\n Point p[2];\n Line(Point p1,Point p2){\n p[0] = p1;\n p[1] = p2;\n }\n Line(){\n\n }\n};\n\nstruct Info{\n\tbool operator<(const struct Info &arg) const{\n\n\t\treturn left > arg.left;\n\t}\n\tdouble left,right;\n};\n\nint N,K;\nint most_L[SIZE];\nvector<Info> work,V;\ndouble dp1[SIZE][SIZE],dp2[SIZE][SIZE];\nCircle circle[SIZE];\n\n\ndouble NUM = 10000;\n\ndouble norm(Vector a){\n return a.x*a.x+a.y*a.y;\n}\n\ndouble abs(Vector a){\n return sqrt(norm(a));\n}\n\ndouble calc_dist(Point A,Point B){\n\n\treturn sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y));\n}\n\nint main(){\n\n\tPoint base = Point(0,0);\n\n\tscanf(\"%d %d\",&N,&K);\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tscanf(\"%lf %lf %lf\",&circle[i].center.x,&circle[i].center.y,&circle[i].r);\n\t\tif(calc_dist(base,circle[i].center) < circle[i].r){\n\n\t\t\tprintf(\"1.0\\n\");\n\t\t\treturn 0;\n\t\t}\n\t}\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tdouble theta1 = atan2(circle[i].center.y,circle[i].center.x);\n\t\tdouble theta2 = asin(circle[i].r/abs(circle[i].center));\n\n\t\tInfo info;\n\t\tinfo.right = theta1-theta2;\n\t\tinfo.left = theta1+theta2;\n\n\t\tif(info.right < -(M_PI+EPS)){\n\n\t\t\tinfo.left += 2*M_PI;\n\t\t\tinfo.right += 2*M_PI;\n\t\t}\n\n\t\tinfo.right = max(info.right,0.0);\n\t\tinfo.left = min(info.left,M_PI);\n\n\t\tif(info.right+EPS > info.left){\n\n\t\t\tcontinue;\n\t\t}\n\n\t\twork.push_back(info);\n\t}\n\n\tbool FLG;\n\t//他でカバーできる接線のペアは除く\n\tfor(int i = 0; i < work.size(); i++){\n\t\tFLG = false;\n\t\tfor(int k = i+1; k < work.size(); k++){\n\t\t\tif(k == i)continue;\n\n\t\t\tif(work[k].left >= work[i].left && work[k].right <= work[i].right){\n\n\t\t\t\tFLG = true;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tif(!FLG){\n\n\t\t\tV.push_back(work[i]);\n\t\t}\n\t}\n\n\tsort(V.begin(),V.end()); //leftの降順\n\n\tfor(int i = 0; i < V.size(); i++){\n\t\tmost_L[i] = -1;\n\t\tfor(int k = 0; k < i; k++){\n\t\t\tif(V[k].right > V[i].left+EPS)continue;\n\n\t\t\tmost_L[i] = k;\n\t\t\tbreak;\n\t\t}\n\t}\n\n\tfor(int i = 0; i <= K; i++){\n\t\tfor(int k = 0; k <= V.size(); k++){\n\t\t\tdp1[i][k] = 0;\n\t\t\tdp2[i][k] = 0;\n\t\t}\n\t}\n\n\tdouble maximum = 0;\n\n\tfor(int i = 1; i <= K; i++){ //使用個数\n\t\tfor(int k = 1; k <= V.size(); k++){ //最後のindex(本来のindex+1)\n\t\t\tif(most_L[k-1] == -1){\n\n\t\t\t\tdp1[i][k] = dp2[i-1][k-1]+V[k-1].left-V[k-1].right;\n\n\t\t\t}else{ //重なりを持つ区間あり\n\n\t\t\t\tdp1[i][k] = max(dp1[i-1][most_L[k-1]+1]+V[most_L[k-1]].right-V[k-1].right,\n\t\t\t\t\t\tdp2[i-1][most_L[k-1]]+V[k-1].left-V[k-1].right);\n\t\t\t}\n\t\t\tdp2[i][k] = max(dp1[i][k],dp2[i][k-1]);\n\n\t\t\tmaximum = max(maximum,dp2[i][k]);\n\t\t}\n\t}\n\n\tprintf(\"%.10lf\\n\",maximum/M_PI);\n\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 39032, "score_of_the_acc": -0.1731, "final_rank": 8 }, { "submission_id": "aoj_2276_3919772", "code_snippet": "#pragma GCC optimize (\"O3\")\n\n#include <iostream>\n#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n#include <math.h>\n#include <map>\n#include <unordered_map>\n#include <set>\n#include <unordered_set>\n#include <queue>\n#include <set>\n#include <algorithm>\n#include <numeric>\n#include <list>\nusing namespace std;\n\nusing QWORD = uint64_t;\nusing SQWORD = int64_t;\nusing DWORD = uint32_t;\nusing SDWORD = int32_t;\nusing WORD = uint16_t;\nusing SWORD = int16_t;\nusing BYTE = uint8_t;\nusing SBYTE = int8_t;\nusing DOUBLE = double;\nusing FLOAT = float;\n\n#define MIN_SDWORD (-2147483648)\n#define MAX_SDWORD (2147483647)\n#define MIN_SBYTE (-128)\n#define MAX_SBYTE (127)\n\n#define MIN_SQWORD (0x8000000000000000)\n#define MAX_SQWORD (0x7FFFFFFFFFFFFFFF)\n\n#define MAX_QWORD (0xFFFFFFFFFFFFFFFF)\n#define MAX_DWORD (0xFFFFFFFF)\n#define MAX_WORD (0xFFFF)\n#define MAX_BYTE (0xFF)\n\n#define MAX_DOUBLE (1.0e+308)\n#define DOUBLE_EPS (1.0e-12)\n#define MIN_DOUBLE_N (-1.0e+308)\n\n#define ArrayLength(a) (sizeof(a) / sizeof(a[0]))\n\nstatic inline DOUBLE MAX(DOUBLE a, DOUBLE b) { return a > b ? a : b; }\nstatic inline QWORD MAX(QWORD a, QWORD b) { return a > b ? a : b; }\nstatic inline DWORD MAX(DWORD a, DWORD b) { return a > b ? a : b; }\nstatic inline SDWORD MAX(SDWORD a, SDWORD b) { return a > b ? a : b; }\nstatic inline DOUBLE MIN(DOUBLE a, DOUBLE b) { return a < b ? a : b; }\nstatic inline QWORD MIN(QWORD a, QWORD b) { return a < b ? a : b; }\nstatic inline DWORD MIN(DWORD a, DWORD b) { return a < b ? a : b; }\nstatic inline SDWORD MIN(SDWORD a, SDWORD b) { return a < b ? a : b; }\nstatic inline DOUBLE ABS(DOUBLE a) { return 0 < a ? a : -a; };\n\n#define BYTE_BITS (8)\n#define WORD_BITS (16)\n#define DWORD_BITS (32)\n#define QWORD_BITS (64)\n\n#define ANS_MOD (1000000007)\n\nstatic const DOUBLE d_PI = 3.14159265358979323846;\n//static const DOUBLE d_PI = M_PI;\n\n\nstatic inline bool DoubleIsEqual(DOUBLE a, DOUBLE b) {\n return ABS(a-b) < DOUBLE_EPS;\n}\n\nstatic inline void inputStringSpSeparated(char *pcStr)\n{\n char *pcCur = pcStr;\n for (;;) {\n char c = getchar();\n if (('\\n' == c) || (EOF == c) || (' ' == c)) {\n break;\n }\n *pcCur = c;\n pcCur++;\n }\n *pcCur = '\\0';\n}\n\nstatic inline void inputString(char *pcStr)\n{\n char *pcCur = pcStr;\n for (;;) {\n char c = getchar();\n if (('\\n' == c) || (EOF == c)) {\n break;\n }\n *pcCur = c;\n pcCur++;\n }\n *pcCur = '\\0';\n}\n\n\nstatic inline SQWORD inputSQWORD(void)\n{\n SQWORD sqNumber = 0;\n SQWORD sqMultiplier = 1;\n bool bRead = false;\n for (;;) {\n char c = getchar();\n if (!bRead) {\n if ('-' == c) {\n sqMultiplier = -1;\n }\n }\n if (('0' <= c) && (c <= '9')) {\n sqNumber *= 10LL;\n sqNumber += (SQWORD)(c - '0');\n bRead = true;\n } else {\n if (bRead) {\n return sqNumber * sqMultiplier;\n }\n }\n }\n}\n\n\nstatic inline SDWORD inputSDWORD(void)\n{\n SDWORD lNumber = 0;\n SDWORD lMultiplier = 1;\n bool bRead = false;\n for (;;) {\n char c = getchar();\n if (!bRead) {\n if ('-' == c) {\n lMultiplier = -1;\n }\n }\n if (('0' <= c) && (c <= '9')) {\n lNumber *= 10;\n lNumber += (c - '0');\n bRead = true;\n } else {\n if (bRead) {\n return lNumber * lMultiplier;\n }\n }\n }\n}\n\nstatic inline DOUBLE inputFP(void)\n{\n DOUBLE dInt = 0.0;\n DOUBLE dFrac = 0.0;\n DOUBLE dMultiplier = 1.0;\n DWORD dwFpCnt = 0;\n DOUBLE *pdCur = &dInt;\n bool bRead = false;\n for (;;) {\n char c = getchar();\n if (!bRead) {\n if ('-' == c) {\n dMultiplier = -1;\n }\n }\n if ('.' == c) {\n pdCur = &dFrac;\n } else if (('0' <= c) && (c <= '9')) {\n (*pdCur) *= 10;\n (*pdCur) += (DOUBLE)(c - '0');\n bRead = true;\n if (pdCur == &dFrac) {\n dwFpCnt++;\n }\n } else {\n if (bRead) {\n return dMultiplier * (dInt + dFrac / (pow((DOUBLE)10.0 , (DOUBLE)dwFpCnt)));\n }\n }\n }\n}\n\n/*----------------------------------------------*/\n/**\n * mod による操作ライブラリ\n */\n#define ANS_MOD (1000000007)\n\nclass MODINT {\n static SQWORD MOD;\n SQWORD m_x;\n\npublic:\n MODINT(SQWORD val) {\n m_x = (val % MOD + MOD) % MOD;\n };\n MODINT() {\n m_x = 0;\n }\n static void Init(SQWORD sqMod) {\n MOD = sqMod;\n }\n\n\tMODINT& operator+= (const MODINT a)\n {\n m_x = (m_x + a.m_x) % MOD; \n return *this;\n };\n\tMODINT& operator-= (const MODINT a)\n { \n m_x = (m_x - a.m_x + MOD) % MOD; \n return *this;\n };\n\tMODINT& operator*= (const MODINT a)\n {\n m_x = (m_x * a.m_x) % MOD;\n return *this;\n };\n MODINT pow(SQWORD t) const {\n if (!t) return 1;\n MODINT a = pow(t>>1);\n a *= a;\n if (t&1) a *= *this;\n return a;\n }\n\tMODINT operator+ (const MODINT a) const {\n\t\tMODINT res(*this);\n\t\treturn (res += a);\n\t}\n\tMODINT operator- (const MODINT a) const {\n\t\tMODINT res(*this);\n\t\treturn (res -= a);\n\t}\n\tMODINT operator* (const MODINT a) const {\n\t\tMODINT res(*this);\n\t\treturn (res *= a);\n\t}\n\tMODINT operator/ (const MODINT a) const {\n\t\tMODINT res(*this);\n\t\treturn (res /= a);\n\t}\n\n /* 逆元 */\n MODINT inv() const {\n return pow(MOD-2);\n }\n\n /* 除算 */\n MODINT& operator/=(const MODINT a) {\n return (*this) *= a.inv();\n } \n\n /* 整数版 */\n\tMODINT& operator+= (const SQWORD a) {*this += MODINT(a); return *this;};\n\tMODINT& operator-= (const SQWORD a) {*this -= MODINT(a); return *this;};\n\tMODINT& operator*= (const SQWORD a) {*this *= MODINT(a); return *this;};\n\tMODINT& operator/= (const SQWORD a) {*this /= MODINT(a); return *this;};\n\n SQWORD getVal() { return m_x; };\n};\nSQWORD MODINT::MOD = ANS_MOD;\n\n\n/*----------------------------------------------*/\n\n#define EPS 1e-10\nDOUBLE double_add(DOUBLE a, DOUBLE b) {\n if (ABS(a + b) < EPS * (ABS(a) + ABS(b))) {\n return 0;\n }\n return a+b;\n}\n\nstruct VECTOR_2D {\n DOUBLE dX;\n DOUBLE dY;\n VECTOR_2D(DOUBLE x, DOUBLE y) : dX(x), dY(y) {};\n VECTOR_2D() : dX(0), dY(0) {};\n\n /* 加算 */\n\tVECTOR_2D& operator+= (const VECTOR_2D a)\n { \n dX = dX + a.dX;\n dY = dY + a.dY;\n return *this;\n };\n const VECTOR_2D operator+ (const VECTOR_2D a) const {\n\t\treturn VECTOR_2D(dX + a.dX, dY + a.dY); \n };\n\n /* 減算 */\n\tVECTOR_2D& operator-= (const VECTOR_2D a)\n { \n dX = dX - a.dX;\n dY = dY - a.dY;\n return *this;\n };\n const VECTOR_2D operator- (const VECTOR_2D a) const {\n\t\treturn VECTOR_2D(dX - a.dX, dY - a.dY);\n };\n\n /* 定数倍 */\n\tVECTOR_2D& operator*= (const DOUBLE a)\n { \n dX = dX * a;\n dY = dY * a;\n return *this;\n };\n const VECTOR_2D operator* (const DOUBLE a) const {\n\t\treturn VECTOR_2D(dX * a, dY * a); \n };\n\n /* 比較 */\n bool operator< (const VECTOR_2D &a) {\n if (dX == a.dX) {\n return (dY < a.dY);\n }\n return dX < a.dX;\n }\n\n DOUBLE norm(void) const {\n return (dX * dX + dY * dY);\n }\n\n DOUBLE dist(void) const {\n return sqrtl(norm());\n }\n\n DOUBLE angle(void) const {\n return atan2(dY, dX);\n }\n\n DOUBLE dotproduct(const VECTOR_2D a) const {\n return double_add(dX * a.dX, dY * a.dY);\n }\n\n DOUBLE crossproduct(const VECTOR_2D a) const {\n return double_add(dX * a.dY, -dY * a.dX);\n }\n};\n\n\n\n/*----------------------------------------------*/\n\nstruct BALL_ST {\n DOUBLE dBegin;\n DOUBLE dEnd;\n\n BALL_ST (DOUBLE b, DOUBLE e) : dBegin(b), dEnd(e) {};\n}; \n\nbool cmpBegin (const BALL_ST &a, const BALL_ST &b) {\n return a.dBegin < b.dBegin;\n}\n\nbool cmpEnd (const BALL_ST &a, const BALL_ST &b) {\n return a.dEnd < b.dEnd;\n}\n\n/**\n * 角度の扱い \n */\nstatic inline DOUBLE normalizeAngle(DOUBLE dAngle)\n{\n DOUBLE dRet = dAngle;\n while (dRet < 0.0) {\n dRet += (2.0 * d_PI);\n }\n\n if (DoubleIsEqual(2.0 * d_PI, dAngle)) {\n dRet = 0.0;\n } else {\n while ((2.0 * d_PI) <= dAngle) {\n dRet -= (2.0 * d_PI);\n }\n }\n\n return dRet;\n}\n\n#define MAX_N (1500)\nint main(void)\n{\n SQWORD sqN = inputSQWORD();\n SQWORD sqK = inputSQWORD();\n\n vector<BALL_ST> vecBalls;\n for (SQWORD sqIdx = 0; sqIdx < sqN; sqIdx++) {\n SQWORD sqX = inputSQWORD();\n SQWORD sqY = inputSQWORD();\n SQWORD sqR = inputSQWORD();\n\n VECTOR_2D stVec((DOUBLE)sqX, (DOUBLE)sqY);\n DOUBLE dAngle = stVec.angle();\n DOUBLE dDist = stVec.dist();\n DOUBLE dRange = asin((DOUBLE)sqR / dDist);\n\n DOUBLE dTheta1 = dAngle - dRange;\n DOUBLE dTheta2 = dAngle + dRange;\n \n dTheta1 = normalizeAngle(dTheta1);\n dTheta2 = normalizeAngle(dTheta2);\n\n DOUBLE dBegin, dEnd;\n\n if (d_PI <= dTheta1 && d_PI <= dTheta2) {\n dBegin = 0.0;\n dEnd = 0.0;\n } else if (d_PI <= dTheta1) {\n dBegin = 0.0;\n dEnd = dTheta2;\n } else if (d_PI <= dTheta2){\n dBegin = dTheta1;\n dEnd = d_PI;\n } else {\n dBegin = dTheta1;\n dEnd = dTheta2;\n }\n// printf(\"%f %f\\n\", dBegin, dEnd);\n vecBalls.emplace_back(dBegin, dEnd);\n }\n sort(vecBalls.begin(), vecBalls.end(), cmpBegin);\n\t/* 含まれてしまうものは除く */\n\tint sqSz = 0;\n\tfor (int i = 0; i < sqN; i++) {\n\t\tbool isIinJ = false;\n\t\tfor (int j = 1; j <= sqN; j++) {\n\t\t\tif (i == j) continue;\n\t\t\tif ((vecBalls[j].dBegin < vecBalls[i].dBegin) && (vecBalls[i].dEnd < vecBalls[j].dEnd)) {\n\t\t\t\tisIinJ = true;\n\t\t\t}\n\t\t}\n\t\tif (!isIinJ) {\n\t\t\tvecBalls[sqSz++] = vecBalls[i];\n\t\t}\n\t}\n// for (auto b: vecBalls) {\n// printf(\":%f %f\\n\", b.dBegin, b.dEnd);\n// }\n\n /**\n * dp[j][k] 左端がk番目のボールで、\n * j個を使っているときの最大の角度\n * dp[j][k] = max(dp[j-1][0] + angle, dp[j-1][1] + angle, dp[j-1][2] + angle ,,,, dp[j-1][k-1] + angle)\n * \n */\n static DOUBLE s_aadDpTbl[MAX_N + 1][MAX_N + 1];\n for (SQWORD sqIdxJ = 0; sqIdxJ <= sqK; sqIdxJ++) {\n for (SQWORD sqIdxK = 0; sqIdxK <= sqSz; sqIdxK++) {\n s_aadDpTbl[sqIdxJ][sqIdxK] = 0.0;\n }\n }\n\n\tdouble dAns = 0;\n\n\tfor (int i = 1; i <= sqK; i++) {\n\t\tfor (int j = 0; j < sqSz; j++) {\n\t\t\tdouble best = 0;\n\t\t\tint k = 0;\n\n\t\t\twhile (vecBalls[k].dEnd < vecBalls[j].dBegin) {\n\t\t\t\tbest = max(best, s_aadDpTbl[i-1][k]);\n\t\t\t\tk++;\n\t\t\t}\n\n// printf(\"--%d %f %f\\n\", j, vecBalls[j].dEnd, vecBalls[j].dBegin);\n\t\t\t\n\t\t\ts_aadDpTbl[i][j] = max(s_aadDpTbl[i][j], best + vecBalls[j].dEnd - vecBalls[j].dBegin);\n\t\t\ts_aadDpTbl[i][j] = max(s_aadDpTbl[i][j], s_aadDpTbl[i-1][k] + vecBalls[j].dEnd - vecBalls[k].dEnd);\n\t\t\tdAns = max(dAns, s_aadDpTbl[i][j]);\n// printf(\"%f \", s_aadDpTbl[i][j]);\n\t\t}\n// printf(\"\\n\");\n\t}\n\n#if 0\n DOUBLE dAns = 0.0;\n for (SQWORD sqIdxJ = 0; sqIdxJ < sqK; sqIdxJ++) {\n for (SQWORD sqIdxK = 0; sqIdxK < sz; sqIdxK++) {\n DOUBLE dPrevEnd = vecBalls[sqIdxK].dEnd;\n for (SQWORD sqNextBallIdx = sqIdxK + 1; sqNextBallIdx <= sz; sqNextBallIdx++) {\n BALL_ST stBall = vecBalls[sqNextBallIdx];\n DOUBLE dAddAngle = 0;\n if (dPrevEnd <= stBall.dBegin) {\n /* 重なってない */\n dAddAngle = stBall.dEnd - stBall.dBegin;\n// printf(\"> [%lld %lld %lld] %llf \", sqIdxJ, sqIdxK, sqNextBallIdx, dAddAngle / d_PI);\n s_aadDpTbl[sqIdxJ + 1][sqNextBallIdx] \n = MAX(s_aadDpTbl[sqIdxJ + 1][sqNextBallIdx], s_aadDpTbl[sqIdxJ][sqIdxK] + dAddAngle);\n } else {\n /* 重なってる */\n dAddAngle = stBall.dEnd - dPrevEnd;\n s_aadDpTbl[sqIdxJ + 1][sqNextBallIdx] \n = MAX(s_aadDpTbl[sqIdxJ + 1][sqNextBallIdx], s_aadDpTbl[sqIdxJ][sqIdxK] + dAddAngle);\n// printf(\"< [%lld %lld %lld] %llf \", sqIdxJ, sqIdxK, sqNextBallIdx, dAddAngle / d_PI);\n }\n\n dAns = max(s_aadDpTbl[sqIdxJ + 1][sqNextBallIdx], dAns);\n }\n }\n// printf(\"\\n\");\n }\n#endif\n\n printf(\"%0.11f\\n\", dAns / d_PI);\n\n return 0;\n}", "accuracy": 1, "time_ms": 330, "memory_kb": 21172, "score_of_the_acc": -0.4624, "final_rank": 11 }, { "submission_id": "aoj_2276_3917912", "code_snippet": "#define _USE_MATH_DEFINES\n#include <algorithm>\n#include <iostream>\n#include <vector>\n#include <cmath>\n#include <numeric>\n#include <tuple>\n#include <iomanip>\n#include <assert.h>\n\n#define sqr(x) std::pow(x, 2.0)\n#define INF 1e10\n#define EPS 1e-10\n#define REP(i, n) for (int i = 0; i < n; i++)\n#define POS_OR_INF(x) (x > 0 ? x : INF)\n\nusing namespace std;\n\nusing ld = long double;\n\nstruct Point {\n\tPoint() = default;\n\tPoint(ld x, ld y) : x(x), y(y) {}\n\tPoint(const Point& s, const Point& e) : x(e.x - s.x), y(e.y - s.y) {}\n\n\tPoint normalized(void) const {\n\t\treturn Point(x / norm(), y / norm());\n\t}\n\n\tld norm(void) const {\n\t\treturn sqrt(x * x + y * y);\n\t}\n\n\tPoint flipped(void) const {\n\t\treturn Point(-x, -y);\n\t}\n\n\tPoint normal_vector(bool normalize = false) const {\n\t\tauto vec = Point(-y, x);\n\t\treturn normalize ? vec.normalized() : vec;\n\t}\n\n\tld x, y;\n};\n\nusing Vector = Point;\n\ninline Vector points2vec(const Point& s, const Point& e) {\n\treturn Vector(e.x - s.x, e.y - s.y);\n}\n\ninline ld distance(const Point& s, const Point& e) {\n\treturn points2vec(s, e).norm();\n}\n\ninline ld distance_square(const Point& s, const Point& e) {\n\treturn pow(points2vec(s, e).norm(), 2.0);\n}\n\ninline Vector operator * (const Vector& l, ld r) {\n\treturn Vector(l.x * r, l.y * r);\n}\n\ninline Vector operator * (ld l, const Vector& r) {\n\treturn r * l;\n}\n\ninline Vector operator / (const Vector& l, ld r) {\n\treturn Vector(l.x / r, l.y / r);\n}\n\ninline Point operator + (const Point& l, const Point& r) {\n\treturn Point(l.x + r.x, l.y + r.y);\n}\n\ninline Point operator - (const Point& l, const Point& r) {\n\treturn Point(l.x - r.x, l.y - r.y);\n}\n\ninline istream& operator >> (istream& is, Point& p) {\n\treturn is >> p.x >> p.y;\n}\n\nint main(int argc, char** argv) {\n\tint N, K;\n\n\tcin >> N >> K;\n\tcout << fixed << setprecision(20);\n\n\tvector<pair<double, double>> a(N), b;\n\n\tfor (int i = 0; i < N; i++) {\n\t\tPoint p;\n\t\tdouble r;\n\t\tcin >> p >> r;\n\n\t\tdouble theta = asin(r/p.norm());\n\t\tdouble rad = atan2(p.y, p.x);\n\n\t\tif (rad < -M_PI / 2) rad += 2 * M_PI;\n\n\t\ta[i].first = min(M_PI, max(0.0, rad - theta));\n\t\ta[i].second = min(M_PI, max(0.0, rad + theta));\n\t}\n\n\tfor (int i = 0; i < N; i++) {\n\t\tbool included = false;\n\t\tfor (int j = 0; j < N; j++) {\n\t\t\tif (a[i].first > a[j].first && a[i].second < a[j].second)\n\t\t\t\tincluded = true;\n\t\t}\n\t\tif (!included) {\n\t\t\tb.push_back(a[i]);\n\t\t}\n\t}\n\n\tsort(b.begin(), b.end());\n\n\tint M = b.size();\n\n\tvector<vector<double>> dp(K + 1, vector<double>(M + 1, 0));\n\tdouble ans = 0;\n\n\tfor (int i = 1; i <= K; i++) {\n\t\tfor (int j = 0; j < M; j++) {\n\t\t\tdouble best = 0;\n\t\t\tint k = 0;\n\n\t\t\twhile (b[k].second < b[j].first) {\n\t\t\t\tbest = max(best, dp[i-1][k]);\n\t\t\t\tk++;\n\t\t\t}\n\t\t\t\n\t\t\tdp[i][j] = max(dp[i][j], best + b[j].second - b[j].first);\n\t\t\tdp[i][j] = max(dp[i][j], dp[i-1][k] + b[j].second - b[k].second);\n\t\t\tans = max(ans, dp[i][j]);\n\t\t}\n\t}\n\n\tcout << ans / M_PI;\n}", "accuracy": 1, "time_ms": 340, "memory_kb": 20612, "score_of_the_acc": -0.4714, "final_rank": 12 }, { "submission_id": "aoj_2276_3917801", "code_snippet": "#pragma GCC optimize (\"O3\")\n\n#include <iostream>\n#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n#include <math.h>\n#include <map>\n#include <unordered_map>\n#include <set>\n#include <unordered_set>\n#include <queue>\n#include <set>\n#include <algorithm>\n#include <numeric>\n#include <list>\nusing namespace std;\n\nusing QWORD = uint64_t;\nusing SQWORD = int64_t;\nusing DWORD = uint32_t;\nusing SDWORD = int32_t;\nusing WORD = uint16_t;\nusing SWORD = int16_t;\nusing BYTE = uint8_t;\nusing SBYTE = int8_t;\nusing DOUBLE = double;\nusing FLOAT = float;\n\n#define MIN_SDWORD (-2147483648)\n#define MAX_SDWORD (2147483647)\n#define MIN_SBYTE (-128)\n#define MAX_SBYTE (127)\n\n#define MIN_SQWORD (0x8000000000000000)\n#define MAX_SQWORD (0x7FFFFFFFFFFFFFFF)\n\n#define MAX_QWORD (0xFFFFFFFFFFFFFFFF)\n#define MAX_DWORD (0xFFFFFFFF)\n#define MAX_WORD (0xFFFF)\n#define MAX_BYTE (0xFF)\n\n#define MAX_DOUBLE (1.0e+308)\n#define DOUBLE_EPS (1.0e-12)\n#define MIN_DOUBLE_N (-1.0e+308)\n\n#define ArrayLength(a) (sizeof(a) / sizeof(a[0]))\n\nstatic inline DOUBLE MAX(DOUBLE a, DOUBLE b) { return a > b ? a : b; }\nstatic inline QWORD MAX(QWORD a, QWORD b) { return a > b ? a : b; }\nstatic inline DWORD MAX(DWORD a, DWORD b) { return a > b ? a : b; }\nstatic inline SDWORD MAX(SDWORD a, SDWORD b) { return a > b ? a : b; }\nstatic inline DOUBLE MIN(DOUBLE a, DOUBLE b) { return a < b ? a : b; }\nstatic inline QWORD MIN(QWORD a, QWORD b) { return a < b ? a : b; }\nstatic inline DWORD MIN(DWORD a, DWORD b) { return a < b ? a : b; }\nstatic inline SDWORD MIN(SDWORD a, SDWORD b) { return a < b ? a : b; }\nstatic inline DOUBLE ABS(DOUBLE a) { return 0 < a ? a : -a; };\n\n#define BYTE_BITS (8)\n#define WORD_BITS (16)\n#define DWORD_BITS (32)\n#define QWORD_BITS (64)\n\n#define ANS_MOD (1000000007)\n\nstatic const DOUBLE d_PI = 3.14159265358979323846;\n\nstatic inline bool DoubleIsEqual(DOUBLE a, DOUBLE b) {\n return ABS(a-b) < DOUBLE_EPS;\n}\n\nstatic inline void inputStringSpSeparated(char *pcStr)\n{\n char *pcCur = pcStr;\n for (;;) {\n char c = getchar();\n if (('\\n' == c) || (EOF == c) || (' ' == c)) {\n break;\n }\n *pcCur = c;\n pcCur++;\n }\n *pcCur = '\\0';\n}\n\nstatic inline void inputString(char *pcStr)\n{\n char *pcCur = pcStr;\n for (;;) {\n char c = getchar();\n if (('\\n' == c) || (EOF == c)) {\n break;\n }\n *pcCur = c;\n pcCur++;\n }\n *pcCur = '\\0';\n}\n\n\nstatic inline SQWORD inputSQWORD(void)\n{\n SQWORD sqNumber = 0;\n SQWORD sqMultiplier = 1;\n bool bRead = false;\n for (;;) {\n char c = getchar();\n if (!bRead) {\n if ('-' == c) {\n sqMultiplier = -1;\n }\n }\n if (('0' <= c) && (c <= '9')) {\n sqNumber *= 10LL;\n sqNumber += (SQWORD)(c - '0');\n bRead = true;\n } else {\n if (bRead) {\n return sqNumber * sqMultiplier;\n }\n }\n }\n}\n\n\nstatic inline SDWORD inputSDWORD(void)\n{\n SDWORD lNumber = 0;\n SDWORD lMultiplier = 1;\n bool bRead = false;\n for (;;) {\n char c = getchar();\n if (!bRead) {\n if ('-' == c) {\n lMultiplier = -1;\n }\n }\n if (('0' <= c) && (c <= '9')) {\n lNumber *= 10;\n lNumber += (c - '0');\n bRead = true;\n } else {\n if (bRead) {\n return lNumber * lMultiplier;\n }\n }\n }\n}\n\nstatic inline DOUBLE inputFP(void)\n{\n DOUBLE dInt = 0.0;\n DOUBLE dFrac = 0.0;\n DOUBLE dMultiplier = 1.0;\n DWORD dwFpCnt = 0;\n DOUBLE *pdCur = &dInt;\n bool bRead = false;\n for (;;) {\n char c = getchar();\n if (!bRead) {\n if ('-' == c) {\n dMultiplier = -1;\n }\n }\n if ('.' == c) {\n pdCur = &dFrac;\n } else if (('0' <= c) && (c <= '9')) {\n (*pdCur) *= 10;\n (*pdCur) += (DOUBLE)(c - '0');\n bRead = true;\n if (pdCur == &dFrac) {\n dwFpCnt++;\n }\n } else {\n if (bRead) {\n return dMultiplier * (dInt + dFrac / (pow((DOUBLE)10.0 , (DOUBLE)dwFpCnt)));\n }\n }\n }\n}\n\n/*----------------------------------------------*/\n/**\n * mod による操作ライブラリ\n */\n#define ANS_MOD (1000000007)\n\nclass MODINT {\n static SQWORD MOD;\n SQWORD m_x;\n\npublic:\n MODINT(SQWORD val) {\n m_x = (val % MOD + MOD) % MOD;\n };\n MODINT() {\n m_x = 0;\n }\n static void Init(SQWORD sqMod) {\n MOD = sqMod;\n }\n\n\tMODINT& operator+= (const MODINT a)\n {\n m_x = (m_x + a.m_x) % MOD; \n return *this;\n };\n\tMODINT& operator-= (const MODINT a)\n { \n m_x = (m_x - a.m_x + MOD) % MOD; \n return *this;\n };\n\tMODINT& operator*= (const MODINT a)\n {\n m_x = (m_x * a.m_x) % MOD;\n return *this;\n };\n MODINT pow(SQWORD t) const {\n if (!t) return 1;\n MODINT a = pow(t>>1);\n a *= a;\n if (t&1) a *= *this;\n return a;\n }\n\tMODINT operator+ (const MODINT a) const {\n\t\tMODINT res(*this);\n\t\treturn (res += a);\n\t}\n\tMODINT operator- (const MODINT a) const {\n\t\tMODINT res(*this);\n\t\treturn (res -= a);\n\t}\n\tMODINT operator* (const MODINT a) const {\n\t\tMODINT res(*this);\n\t\treturn (res *= a);\n\t}\n\tMODINT operator/ (const MODINT a) const {\n\t\tMODINT res(*this);\n\t\treturn (res /= a);\n\t}\n\n /* 逆元 */\n MODINT inv() const {\n return pow(MOD-2);\n }\n\n /* 除算 */\n MODINT& operator/=(const MODINT a) {\n return (*this) *= a.inv();\n } \n\n /* 整数版 */\n\tMODINT& operator+= (const SQWORD a) {*this += MODINT(a); return *this;};\n\tMODINT& operator-= (const SQWORD a) {*this -= MODINT(a); return *this;};\n\tMODINT& operator*= (const SQWORD a) {*this *= MODINT(a); return *this;};\n\tMODINT& operator/= (const SQWORD a) {*this /= MODINT(a); return *this;};\n\n SQWORD getVal() { return m_x; };\n};\nSQWORD MODINT::MOD = ANS_MOD;\n\n\n/*----------------------------------------------*/\n\n#define EPS 1e-10\nDOUBLE double_add(DOUBLE a, DOUBLE b) {\n if (ABS(a + b) < EPS * (ABS(a) + ABS(b))) {\n return 0;\n }\n return a+b;\n}\n\nstruct VECTOR_2D {\n DOUBLE dX;\n DOUBLE dY;\n VECTOR_2D(DOUBLE x, DOUBLE y) : dX(x), dY(y) {};\n VECTOR_2D() : dX(0), dY(0) {};\n\n /* 加算 */\n\tVECTOR_2D& operator+= (const VECTOR_2D a)\n { \n dX = dX + a.dX;\n dY = dY + a.dY;\n return *this;\n };\n const VECTOR_2D operator+ (const VECTOR_2D a) const {\n\t\treturn VECTOR_2D(dX + a.dX, dY + a.dY); \n };\n\n /* 減算 */\n\tVECTOR_2D& operator-= (const VECTOR_2D a)\n { \n dX = dX - a.dX;\n dY = dY - a.dY;\n return *this;\n };\n const VECTOR_2D operator- (const VECTOR_2D a) const {\n\t\treturn VECTOR_2D(dX - a.dX, dY - a.dY);\n };\n\n /* 定数倍 */\n\tVECTOR_2D& operator*= (const DOUBLE a)\n { \n dX = dX * a;\n dY = dY * a;\n return *this;\n };\n const VECTOR_2D operator* (const DOUBLE a) const {\n\t\treturn VECTOR_2D(dX * a, dY * a); \n };\n\n /* 比較 */\n bool operator< (const VECTOR_2D &a) {\n if (dX == a.dX) {\n return (dY < a.dY);\n }\n return dX < a.dX;\n }\n\n DOUBLE norm(void) const {\n return (dX * dX + dY * dY);\n }\n\n DOUBLE dist(void) const {\n return sqrtl(norm());\n }\n\n DOUBLE angle(void) const {\n return atan2(dY, dX);\n }\n\n DOUBLE dotproduct(const VECTOR_2D a) const {\n return double_add(dX * a.dX, dY * a.dY);\n }\n\n DOUBLE crossproduct(const VECTOR_2D a) const {\n return double_add(dX * a.dY, -dY * a.dX);\n }\n};\n\n\n\n/*----------------------------------------------*/\n\nstruct BALL_ST {\n DOUBLE dBegin;\n DOUBLE dEnd;\n\n BALL_ST (DOUBLE b, DOUBLE e) : dBegin(b), dEnd(e) {};\n}; \n\nbool cmpBegin (const BALL_ST &a, const BALL_ST &b) {\n return a.dBegin < b.dBegin;\n}\n\nbool cmpEnd (const BALL_ST &a, const BALL_ST &b) {\n return a.dEnd < b.dEnd;\n}\n\n/**\n * 角度の扱い \n */\nstatic inline DOUBLE normalizeAngle(DOUBLE dAngle)\n{\n DOUBLE dRet = dAngle;\n while (dRet < 0.0) {\n dRet += (2.0 * d_PI);\n }\n\n if (DoubleIsEqual(2.0 * d_PI, dAngle)) {\n dRet = 0.0;\n } else {\n while ((2.0 * d_PI) <= dAngle) {\n dRet -= (2.0 * d_PI);\n }\n }\n\n return dRet;\n}\n\n#define MAX_N (1500)\nint main(void)\n{\n SQWORD sqN = inputSQWORD();\n SQWORD sqK = inputSQWORD();\n\n vector<BALL_ST> vecBalls;\n vecBalls.emplace_back(0, 0); /* dp用にインデックスを1はじまりにするためのダミー */\n for (SQWORD sqIdx = 0; sqIdx < sqN; sqIdx++) {\n SQWORD sqX = inputSQWORD();\n SQWORD sqY = inputSQWORD();\n SQWORD sqR = inputSQWORD();\n\n VECTOR_2D stVec((DOUBLE)sqX, (DOUBLE)sqY);\n DOUBLE dAngle = stVec.angle();\n DOUBLE dDist = stVec.dist();\n DOUBLE dRange = asin((DOUBLE)sqR / dDist);\n\n DOUBLE dTheta1 = dAngle - dRange;\n DOUBLE dTheta2 = dAngle + dRange;\n \n dTheta1 = normalizeAngle(dTheta1);\n dTheta2 = normalizeAngle(dTheta2);\n\n DOUBLE dBegin, dEnd;\n\n if (d_PI <= dTheta1 && d_PI <= dTheta2) {\n dBegin = 0.0;\n dEnd = 0.0;\n } else if (d_PI <= dTheta1) {\n dBegin = 0.0;\n dEnd = dTheta2;\n } else if (d_PI <= dTheta2){\n dBegin = dTheta1;\n dEnd = d_PI;\n } else {\n dBegin = dTheta1;\n dEnd = dTheta2;\n }\n vecBalls.emplace_back(dBegin, dEnd);\n }\n sort(vecBalls.begin(), vecBalls.end(), cmpBegin);\n\n /**\n * dp[j][k] 左端がk番目のボールで、\n * j個を使っているときの最大の角度\n * dp[j][k] = max(dp[j-1][0] + angle, dp[j-1][1] + angle, dp[j-1][2] + angle ,,,, dp[j-1][k-1] + angle)\n * \n */\n static DOUBLE s_aadDpTbl[MAX_N + 1][MAX_N + 1];\n for (SQWORD sqIdxJ = 0; sqIdxJ <= sqN; sqIdxJ++) {\n for (SQWORD sqIdxK = 0; sqIdxK <= sqN; sqIdxK++) {\n s_aadDpTbl[sqIdxJ][sqIdxK] = 0.0;\n }\n }\n for (SQWORD sqIdxJ = 0; sqIdxJ < sqN; sqIdxJ++) {\n for (SQWORD sqIdxK = sqIdxJ; sqIdxK < sqN; sqIdxK++) {\n DOUBLE dPrevEnd;\n if (0.0 == s_aadDpTbl[sqIdxJ][sqIdxK]) {\n dPrevEnd = 0.0;\n } else {\n dPrevEnd = vecBalls[sqIdxK].dEnd;\n } \n for (SQWORD sqNextBallIdx = sqIdxK + 1; sqNextBallIdx <= sqN; sqNextBallIdx++) {\n BALL_ST stBall = vecBalls[sqNextBallIdx];\n DOUBLE dAddAngle = 0;\n if (dPrevEnd < stBall.dBegin) {\n dAddAngle = stBall.dEnd - stBall.dBegin;\n// printf(\"> [%lld %lld %lld] %llf \", sqIdxJ, sqIdxK, sqNextBallIdx, dAddAngle / d_PI);\n s_aadDpTbl[sqIdxJ + 1][sqNextBallIdx] \n = MAX(s_aadDpTbl[sqIdxJ + 1][sqNextBallIdx], s_aadDpTbl[sqIdxJ][sqIdxK] + dAddAngle);\n } else {\n if (dPrevEnd < stBall.dEnd) {\n dAddAngle = stBall.dEnd - dPrevEnd;\n s_aadDpTbl[sqIdxJ + 1][sqNextBallIdx] \n = MAX(s_aadDpTbl[sqIdxJ + 1][sqNextBallIdx], s_aadDpTbl[sqIdxJ][sqIdxK] + dAddAngle);\n }\n// printf(\"< [%lld %lld %lld] %llf \", sqIdxJ, sqIdxK, sqNextBallIdx, dAddAngle / d_PI);\n }\n\n }\n }\n// printf(\"\\n\");\n }\n\n DOUBLE dAns = 0.0;\n for (SQWORD sqIdxJ = 1; sqIdxJ <= sqK; sqIdxJ++) {\n for (SQWORD sqIdxK = sqIdxJ; sqIdxK <= sqN; sqIdxK++) {\n// printf(\"%llf \", s_aadDpTbl[sqIdxJ][sqIdxK] / d_PI);\n dAns = max(dAns, s_aadDpTbl[sqIdxJ][sqIdxK]);\n }\n// printf(\"\\n\");\n }\n\n printf(\"%0.11f\\n\", dAns / d_PI);\n\n return 0;\n}", "accuracy": 0.3380281690140845, "time_ms": 710, "memory_kb": 21144, "score_of_the_acc": -1.005, "final_rank": 18 }, { "submission_id": "aoj_2276_3917747", "code_snippet": "#pragma GCC optimize (\"O3\")\n\n#include <iostream>\n#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n#include <math.h>\n#include <map>\n#include <unordered_map>\n#include <set>\n#include <unordered_set>\n#include <queue>\n#include <set>\n#include <algorithm>\n#include <numeric>\n#include <list>\nusing namespace std;\n\nusing QWORD = uint64_t;\nusing SQWORD = int64_t;\nusing DWORD = uint32_t;\nusing SDWORD = int32_t;\nusing WORD = uint16_t;\nusing SWORD = int16_t;\nusing BYTE = uint8_t;\nusing SBYTE = int8_t;\nusing DOUBLE = double;\nusing FLOAT = float;\n\n#define MIN_SDWORD (-2147483648)\n#define MAX_SDWORD (2147483647)\n#define MIN_SBYTE (-128)\n#define MAX_SBYTE (127)\n\n#define MIN_SQWORD (0x8000000000000000)\n#define MAX_SQWORD (0x7FFFFFFFFFFFFFFF)\n\n#define MAX_QWORD (0xFFFFFFFFFFFFFFFF)\n#define MAX_DWORD (0xFFFFFFFF)\n#define MAX_WORD (0xFFFF)\n#define MAX_BYTE (0xFF)\n\n#define MAX_DOUBLE (1.0e+308)\n#define DOUBLE_EPS (1.0e-12)\n#define MIN_DOUBLE_N (-1.0e+308)\n\n#define ArrayLength(a) (sizeof(a) / sizeof(a[0]))\n\nstatic inline DOUBLE MAX(DOUBLE a, DOUBLE b) { return a > b ? a : b; }\nstatic inline QWORD MAX(QWORD a, QWORD b) { return a > b ? a : b; }\nstatic inline DWORD MAX(DWORD a, DWORD b) { return a > b ? a : b; }\nstatic inline SDWORD MAX(SDWORD a, SDWORD b) { return a > b ? a : b; }\nstatic inline DOUBLE MIN(DOUBLE a, DOUBLE b) { return a < b ? a : b; }\nstatic inline QWORD MIN(QWORD a, QWORD b) { return a < b ? a : b; }\nstatic inline DWORD MIN(DWORD a, DWORD b) { return a < b ? a : b; }\nstatic inline SDWORD MIN(SDWORD a, SDWORD b) { return a < b ? a : b; }\nstatic inline DOUBLE ABS(DOUBLE a) { return 0 < a ? a : -a; };\n\n#define BYTE_BITS (8)\n#define WORD_BITS (16)\n#define DWORD_BITS (32)\n#define QWORD_BITS (64)\n\n#define ANS_MOD (1000000007)\n\nstatic const DOUBLE d_PI = 3.14159265358979323846;\n\n\nstatic inline void inputStringSpSeparated(char *pcStr)\n{\n char *pcCur = pcStr;\n for (;;) {\n char c = getchar();\n if (('\\n' == c) || (EOF == c) || (' ' == c)) {\n break;\n }\n *pcCur = c;\n pcCur++;\n }\n *pcCur = '\\0';\n}\n\nstatic inline void inputString(char *pcStr)\n{\n char *pcCur = pcStr;\n for (;;) {\n char c = getchar();\n if (('\\n' == c) || (EOF == c)) {\n break;\n }\n *pcCur = c;\n pcCur++;\n }\n *pcCur = '\\0';\n}\n\n\nstatic inline SQWORD inputSQWORD(void)\n{\n SQWORD sqNumber = 0;\n SQWORD sqMultiplier = 1;\n bool bRead = false;\n for (;;) {\n char c = getchar();\n if (!bRead) {\n if ('-' == c) {\n sqMultiplier = -1;\n }\n }\n if (('0' <= c) && (c <= '9')) {\n sqNumber *= 10LL;\n sqNumber += (SQWORD)(c - '0');\n bRead = true;\n } else {\n if (bRead) {\n return sqNumber * sqMultiplier;\n }\n }\n }\n}\n\n\nstatic inline SDWORD inputSDWORD(void)\n{\n SDWORD lNumber = 0;\n SDWORD lMultiplier = 1;\n bool bRead = false;\n for (;;) {\n char c = getchar();\n if (!bRead) {\n if ('-' == c) {\n lMultiplier = -1;\n }\n }\n if (('0' <= c) && (c <= '9')) {\n lNumber *= 10;\n lNumber += (c - '0');\n bRead = true;\n } else {\n if (bRead) {\n return lNumber * lMultiplier;\n }\n }\n }\n}\n\nstatic inline DOUBLE inputFP(void)\n{\n DOUBLE dInt = 0.0;\n DOUBLE dFrac = 0.0;\n DOUBLE dMultiplier = 1.0;\n DWORD dwFpCnt = 0;\n DOUBLE *pdCur = &dInt;\n bool bRead = false;\n for (;;) {\n char c = getchar();\n if (!bRead) {\n if ('-' == c) {\n dMultiplier = -1;\n }\n }\n if ('.' == c) {\n pdCur = &dFrac;\n } else if (('0' <= c) && (c <= '9')) {\n (*pdCur) *= 10;\n (*pdCur) += (DOUBLE)(c - '0');\n bRead = true;\n if (pdCur == &dFrac) {\n dwFpCnt++;\n }\n } else {\n if (bRead) {\n return dMultiplier * (dInt + dFrac / (pow((DOUBLE)10.0 , (DOUBLE)dwFpCnt)));\n }\n }\n }\n}\n\n/*----------------------------------------------*/\n/**\n * mod による操作ライブラリ\n */\n#define ANS_MOD (1000000007)\n\nclass MODINT {\n static SQWORD MOD;\n SQWORD m_x;\n\npublic:\n MODINT(SQWORD val) {\n m_x = (val % MOD + MOD) % MOD;\n };\n MODINT() {\n m_x = 0;\n }\n static void Init(SQWORD sqMod) {\n MOD = sqMod;\n }\n\n\tMODINT& operator+= (const MODINT a)\n {\n m_x = (m_x + a.m_x) % MOD; \n return *this;\n };\n\tMODINT& operator-= (const MODINT a)\n { \n m_x = (m_x - a.m_x + MOD) % MOD; \n return *this;\n };\n\tMODINT& operator*= (const MODINT a)\n {\n m_x = (m_x * a.m_x) % MOD;\n return *this;\n };\n MODINT pow(SQWORD t) const {\n if (!t) return 1;\n MODINT a = pow(t>>1);\n a *= a;\n if (t&1) a *= *this;\n return a;\n }\n\tMODINT operator+ (const MODINT a) const {\n\t\tMODINT res(*this);\n\t\treturn (res += a);\n\t}\n\tMODINT operator- (const MODINT a) const {\n\t\tMODINT res(*this);\n\t\treturn (res -= a);\n\t}\n\tMODINT operator* (const MODINT a) const {\n\t\tMODINT res(*this);\n\t\treturn (res *= a);\n\t}\n\tMODINT operator/ (const MODINT a) const {\n\t\tMODINT res(*this);\n\t\treturn (res /= a);\n\t}\n\n /* 逆元 */\n MODINT inv() const {\n return pow(MOD-2);\n }\n\n /* 除算 */\n MODINT& operator/=(const MODINT a) {\n return (*this) *= a.inv();\n } \n\n /* 整数版 */\n\tMODINT& operator+= (const SQWORD a) {*this += MODINT(a); return *this;};\n\tMODINT& operator-= (const SQWORD a) {*this -= MODINT(a); return *this;};\n\tMODINT& operator*= (const SQWORD a) {*this *= MODINT(a); return *this;};\n\tMODINT& operator/= (const SQWORD a) {*this /= MODINT(a); return *this;};\n\n SQWORD getVal() { return m_x; };\n};\nSQWORD MODINT::MOD = ANS_MOD;\n\n\n/*----------------------------------------------*/\n\n#define EPS 1e-10\nDOUBLE double_add(DOUBLE a, DOUBLE b) {\n if (ABS(a + b) < EPS * (ABS(a) + ABS(b))) {\n return 0;\n }\n return a+b;\n}\n\nstruct VECTOR_2D {\n DOUBLE dX;\n DOUBLE dY;\n VECTOR_2D(DOUBLE x, DOUBLE y) : dX(x), dY(y) {};\n VECTOR_2D() : dX(0), dY(0) {};\n\n /* 加算 */\n\tVECTOR_2D& operator+= (const VECTOR_2D a)\n { \n dX = dX + a.dX;\n dY = dY + a.dY;\n return *this;\n };\n const VECTOR_2D operator+ (const VECTOR_2D a) const {\n\t\treturn VECTOR_2D(dX + a.dX, dY + a.dY); \n };\n\n /* 減算 */\n\tVECTOR_2D& operator-= (const VECTOR_2D a)\n { \n dX = dX - a.dX;\n dY = dY - a.dY;\n return *this;\n };\n const VECTOR_2D operator- (const VECTOR_2D a) const {\n\t\treturn VECTOR_2D(dX - a.dX, dY - a.dY);\n };\n\n /* 定数倍 */\n\tVECTOR_2D& operator*= (const DOUBLE a)\n { \n dX = dX * a;\n dY = dY * a;\n return *this;\n };\n const VECTOR_2D operator* (const DOUBLE a) const {\n\t\treturn VECTOR_2D(dX * a, dY * a); \n };\n\n /* 比較 */\n bool operator< (const VECTOR_2D &a) {\n if (dX == a.dX) {\n return (dY < a.dY);\n }\n return dX < a.dX;\n }\n\n DOUBLE norm(void) const {\n return (dX * dX + dY * dY);\n }\n\n DOUBLE dist(void) const {\n return sqrtl(norm());\n }\n\n DOUBLE angle(void) const {\n return atan2(dY, dX);\n }\n\n DOUBLE dotproduct(const VECTOR_2D a) const {\n return double_add(dX * a.dX, dY * a.dY);\n }\n\n DOUBLE crossproduct(const VECTOR_2D a) const {\n return double_add(dX * a.dY, -dY * a.dX);\n }\n};\n\n\n\n/*----------------------------------------------*/\n\nstruct BALL_ST {\n DOUBLE dBegin;\n DOUBLE dEnd;\n\n BALL_ST (DOUBLE b, DOUBLE e) : dBegin(b), dEnd(e) {};\n}; \n\nbool cmpBegin (const BALL_ST &a, const BALL_ST &b) {\n return a.dBegin < b.dBegin;\n}\n\nbool cmpEnd (const BALL_ST &a, const BALL_ST &b) {\n return a.dEnd < b.dEnd;\n}\n\n/**\n * 角度の扱い \n */\nstatic inline DOUBLE normalizeAngle(DOUBLE dAngle)\n{\n DOUBLE dRet = dAngle;\n while (dRet < 0.0) {\n dRet += (2.0 * d_PI);\n }\n\n while ((2.0 * d_PI) < dAngle) {\n dRet -= (2.0 * d_PI);\n }\n\n return dRet;\n}\n\n#define MAX_N (1500)\nint main(void)\n{\n SQWORD sqN = inputSQWORD();\n SQWORD sqK = inputSQWORD();\n\n vector<BALL_ST> vecBalls;\n vecBalls.emplace_back(0, 0); /* dp用にインデックスを1はじまりにするためのダミー */\n for (SQWORD sqIdx = 0; sqIdx < sqN; sqIdx++) {\n SQWORD sqX = inputSQWORD();\n SQWORD sqY = inputSQWORD();\n SQWORD sqR = inputSQWORD();\n\n VECTOR_2D stVec((DOUBLE)sqX, (DOUBLE)sqY);\n DOUBLE dAngle = normalizeAngle(stVec.angle());\n DOUBLE dDist = stVec.dist();\n DOUBLE dRange = asin((DOUBLE)sqR / dDist);\n \n DOUBLE dBegin, dEnd;\n if (2 * d_PI <= (dAngle + dRange)) {\n dBegin = 0.0;\n dEnd = dAngle + dRange - (2 * d_PI);\n } else {\n dBegin = MIN(MAX(dAngle - dRange, 0.0), d_PI);\n dEnd = MIN(MAX(dAngle + dRange, 0.0), d_PI); \n }\n\n\n\n// printf(\"%0.10f %0.10f\\n\", dBegin, dEnd);\n\n vecBalls.emplace_back(dBegin, dEnd);\n }\n sort(vecBalls.begin(), vecBalls.end(), cmpBegin);\n\n /**\n * dp[j][k] 左端がk番目のボールで、\n * j個を使っているときの最大の角度\n * dp[j][k] = max(dp[j-1][0] + angle, dp[j-1][1] + angle, dp[j-1][2] + angle ,,,, dp[j-1][k-1] + angle)\n * \n */\n static DOUBLE s_aadDpTbl[MAX_N + 1][MAX_N + 1];\n for (SQWORD sqIdxJ = 0; sqIdxJ <= sqN; sqIdxJ++) {\n for (SQWORD sqIdxK = 0; sqIdxK <= sqN; sqIdxK++) {\n s_aadDpTbl[sqIdxJ][sqIdxK] = 0;\n }\n }\n for (SQWORD sqIdxJ = 0; sqIdxJ < sqN; sqIdxJ++) {\n for (SQWORD sqIdxK = sqIdxJ; sqIdxK < sqN; sqIdxK++) {\n DOUBLE dPrevEnd = vecBalls[sqIdxK].dEnd;\n for (SQWORD sqNextBallIdx = sqIdxK + 1; sqNextBallIdx <= sqN; sqNextBallIdx++) {\n BALL_ST stBall = vecBalls[sqNextBallIdx];\n DOUBLE dAddAngle = 0;\n if (dPrevEnd < stBall.dBegin) {\n dAddAngle = stBall.dEnd - stBall.dBegin;\n// printf(\"> [%lld %lld %lld] %llf \", sqIdxJ, sqIdxK, sqNextBallIdx, dAddAngle / d_PI);\n s_aadDpTbl[sqIdxJ + 1][sqNextBallIdx] \n = max(s_aadDpTbl[sqIdxJ + 1][sqNextBallIdx], s_aadDpTbl[sqIdxJ][sqIdxK] + dAddAngle);\n } else {\n if (dPrevEnd < stBall.dEnd) {\n dAddAngle = stBall.dEnd - dPrevEnd;\n s_aadDpTbl[sqIdxJ + 1][sqNextBallIdx] \n = max(s_aadDpTbl[sqIdxJ + 1][sqNextBallIdx], s_aadDpTbl[sqIdxJ][sqIdxK] + dAddAngle);\n }\n// printf(\"< [%lld %lld %lld] %llf \", sqIdxJ, sqIdxK, sqNextBallIdx, dAddAngle / d_PI);\n }\n\n }\n }\n// printf(\"\\n\");\n }\n\n DOUBLE dAns = 0.0;\n for (SQWORD sqIdxJ = 1; sqIdxJ <= sqK; sqIdxJ++) {\n for (SQWORD sqIdxK = sqIdxJ; sqIdxK <= sqN; sqIdxK++) {\n// printf(\"%llf \", s_aadDpTbl[sqIdxJ][sqIdxK] / d_PI);\n dAns = max(dAns, s_aadDpTbl[sqIdxJ][sqIdxK]);\n }\n// printf(\"\\n\");\n }\n\n printf(\"%0.11f\\n\", dAns / d_PI);\n\n return 0;\n}", "accuracy": 0.3380281690140845, "time_ms": 570, "memory_kb": 21292, "score_of_the_acc": -0.8064, "final_rank": 17 }, { "submission_id": "aoj_2276_3917743", "code_snippet": "#pragma GCC optimize (\"O3\")\n\n#include <iostream>\n#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n#include <math.h>\n#include <map>\n#include <unordered_map>\n#include <set>\n#include <unordered_set>\n#include <queue>\n#include <set>\n#include <algorithm>\n#include <numeric>\n#include <list>\nusing namespace std;\n\nusing QWORD = uint64_t;\nusing SQWORD = int64_t;\nusing DWORD = uint32_t;\nusing SDWORD = int32_t;\nusing WORD = uint16_t;\nusing SWORD = int16_t;\nusing BYTE = uint8_t;\nusing SBYTE = int8_t;\nusing DOUBLE = double;\nusing FLOAT = float;\n\n#define MIN_SDWORD (-2147483648)\n#define MAX_SDWORD (2147483647)\n#define MIN_SBYTE (-128)\n#define MAX_SBYTE (127)\n\n#define MIN_SQWORD (0x8000000000000000)\n#define MAX_SQWORD (0x7FFFFFFFFFFFFFFF)\n\n#define MAX_QWORD (0xFFFFFFFFFFFFFFFF)\n#define MAX_DWORD (0xFFFFFFFF)\n#define MAX_WORD (0xFFFF)\n#define MAX_BYTE (0xFF)\n\n#define MAX_DOUBLE (1.0e+308)\n#define DOUBLE_EPS (1.0e-12)\n#define MIN_DOUBLE_N (-1.0e+308)\n\n#define ArrayLength(a) (sizeof(a) / sizeof(a[0]))\n\nstatic inline DOUBLE MAX(DOUBLE a, DOUBLE b) { return a > b ? a : b; }\nstatic inline QWORD MAX(QWORD a, QWORD b) { return a > b ? a : b; }\nstatic inline DWORD MAX(DWORD a, DWORD b) { return a > b ? a : b; }\nstatic inline SDWORD MAX(SDWORD a, SDWORD b) { return a > b ? a : b; }\nstatic inline DOUBLE MIN(DOUBLE a, DOUBLE b) { return a < b ? a : b; }\nstatic inline QWORD MIN(QWORD a, QWORD b) { return a < b ? a : b; }\nstatic inline DWORD MIN(DWORD a, DWORD b) { return a < b ? a : b; }\nstatic inline SDWORD MIN(SDWORD a, SDWORD b) { return a < b ? a : b; }\nstatic inline DOUBLE ABS(DOUBLE a) { return 0 < a ? a : -a; };\n\n#define BYTE_BITS (8)\n#define WORD_BITS (16)\n#define DWORD_BITS (32)\n#define QWORD_BITS (64)\n\n#define ANS_MOD (1000000007)\n\nstatic const DOUBLE d_PI = 3.14159265358979323846;\n\n\nstatic inline void inputStringSpSeparated(char *pcStr)\n{\n char *pcCur = pcStr;\n for (;;) {\n char c = getchar();\n if (('\\n' == c) || (EOF == c) || (' ' == c)) {\n break;\n }\n *pcCur = c;\n pcCur++;\n }\n *pcCur = '\\0';\n}\n\nstatic inline void inputString(char *pcStr)\n{\n char *pcCur = pcStr;\n for (;;) {\n char c = getchar();\n if (('\\n' == c) || (EOF == c)) {\n break;\n }\n *pcCur = c;\n pcCur++;\n }\n *pcCur = '\\0';\n}\n\n\nstatic inline SQWORD inputSQWORD(void)\n{\n SQWORD sqNumber = 0;\n SQWORD sqMultiplier = 1;\n bool bRead = false;\n for (;;) {\n char c = getchar();\n if (!bRead) {\n if ('-' == c) {\n sqMultiplier = -1;\n }\n }\n if (('0' <= c) && (c <= '9')) {\n sqNumber *= 10LL;\n sqNumber += (SQWORD)(c - '0');\n bRead = true;\n } else {\n if (bRead) {\n return sqNumber * sqMultiplier;\n }\n }\n }\n}\n\n\nstatic inline SDWORD inputSDWORD(void)\n{\n SDWORD lNumber = 0;\n SDWORD lMultiplier = 1;\n bool bRead = false;\n for (;;) {\n char c = getchar();\n if (!bRead) {\n if ('-' == c) {\n lMultiplier = -1;\n }\n }\n if (('0' <= c) && (c <= '9')) {\n lNumber *= 10;\n lNumber += (c - '0');\n bRead = true;\n } else {\n if (bRead) {\n return lNumber * lMultiplier;\n }\n }\n }\n}\n\nstatic inline DOUBLE inputFP(void)\n{\n DOUBLE dInt = 0.0;\n DOUBLE dFrac = 0.0;\n DOUBLE dMultiplier = 1.0;\n DWORD dwFpCnt = 0;\n DOUBLE *pdCur = &dInt;\n bool bRead = false;\n for (;;) {\n char c = getchar();\n if (!bRead) {\n if ('-' == c) {\n dMultiplier = -1;\n }\n }\n if ('.' == c) {\n pdCur = &dFrac;\n } else if (('0' <= c) && (c <= '9')) {\n (*pdCur) *= 10;\n (*pdCur) += (DOUBLE)(c - '0');\n bRead = true;\n if (pdCur == &dFrac) {\n dwFpCnt++;\n }\n } else {\n if (bRead) {\n return dMultiplier * (dInt + dFrac / (pow((DOUBLE)10.0 , (DOUBLE)dwFpCnt)));\n }\n }\n }\n}\n\n/*----------------------------------------------*/\n/**\n * mod による操作ライブラリ\n */\n#define ANS_MOD (1000000007)\n\nclass MODINT {\n static SQWORD MOD;\n SQWORD m_x;\n\npublic:\n MODINT(SQWORD val) {\n m_x = (val % MOD + MOD) % MOD;\n };\n MODINT() {\n m_x = 0;\n }\n static void Init(SQWORD sqMod) {\n MOD = sqMod;\n }\n\n\tMODINT& operator+= (const MODINT a)\n {\n m_x = (m_x + a.m_x) % MOD; \n return *this;\n };\n\tMODINT& operator-= (const MODINT a)\n { \n m_x = (m_x - a.m_x + MOD) % MOD; \n return *this;\n };\n\tMODINT& operator*= (const MODINT a)\n {\n m_x = (m_x * a.m_x) % MOD;\n return *this;\n };\n MODINT pow(SQWORD t) const {\n if (!t) return 1;\n MODINT a = pow(t>>1);\n a *= a;\n if (t&1) a *= *this;\n return a;\n }\n\tMODINT operator+ (const MODINT a) const {\n\t\tMODINT res(*this);\n\t\treturn (res += a);\n\t}\n\tMODINT operator- (const MODINT a) const {\n\t\tMODINT res(*this);\n\t\treturn (res -= a);\n\t}\n\tMODINT operator* (const MODINT a) const {\n\t\tMODINT res(*this);\n\t\treturn (res *= a);\n\t}\n\tMODINT operator/ (const MODINT a) const {\n\t\tMODINT res(*this);\n\t\treturn (res /= a);\n\t}\n\n /* 逆元 */\n MODINT inv() const {\n return pow(MOD-2);\n }\n\n /* 除算 */\n MODINT& operator/=(const MODINT a) {\n return (*this) *= a.inv();\n } \n\n /* 整数版 */\n\tMODINT& operator+= (const SQWORD a) {*this += MODINT(a); return *this;};\n\tMODINT& operator-= (const SQWORD a) {*this -= MODINT(a); return *this;};\n\tMODINT& operator*= (const SQWORD a) {*this *= MODINT(a); return *this;};\n\tMODINT& operator/= (const SQWORD a) {*this /= MODINT(a); return *this;};\n\n SQWORD getVal() { return m_x; };\n};\nSQWORD MODINT::MOD = ANS_MOD;\n\n\n/*----------------------------------------------*/\n\n#define EPS 1e-10\nDOUBLE double_add(DOUBLE a, DOUBLE b) {\n if (ABS(a + b) < EPS * (ABS(a) + ABS(b))) {\n return 0;\n }\n return a+b;\n}\n\nstruct VECTOR_2D {\n DOUBLE dX;\n DOUBLE dY;\n VECTOR_2D(DOUBLE x, DOUBLE y) : dX(x), dY(y) {};\n VECTOR_2D() : dX(0), dY(0) {};\n\n /* 加算 */\n\tVECTOR_2D& operator+= (const VECTOR_2D a)\n { \n dX = dX + a.dX;\n dY = dY + a.dY;\n return *this;\n };\n const VECTOR_2D operator+ (const VECTOR_2D a) const {\n\t\treturn VECTOR_2D(dX + a.dX, dY + a.dY); \n };\n\n /* 減算 */\n\tVECTOR_2D& operator-= (const VECTOR_2D a)\n { \n dX = dX - a.dX;\n dY = dY - a.dY;\n return *this;\n };\n const VECTOR_2D operator- (const VECTOR_2D a) const {\n\t\treturn VECTOR_2D(dX - a.dX, dY - a.dY);\n };\n\n /* 定数倍 */\n\tVECTOR_2D& operator*= (const DOUBLE a)\n { \n dX = dX * a;\n dY = dY * a;\n return *this;\n };\n const VECTOR_2D operator* (const DOUBLE a) const {\n\t\treturn VECTOR_2D(dX * a, dY * a); \n };\n\n /* 比較 */\n bool operator< (const VECTOR_2D &a) {\n if (dX == a.dX) {\n return (dY < a.dY);\n }\n return dX < a.dX;\n }\n\n DOUBLE norm(void) const {\n return (dX * dX + dY * dY);\n }\n\n DOUBLE dist(void) const {\n return sqrtl(norm());\n }\n\n DOUBLE angle(void) const {\n return atan2(dY, dX);\n }\n\n DOUBLE dotproduct(const VECTOR_2D a) const {\n return double_add(dX * a.dX, dY * a.dY);\n }\n\n DOUBLE crossproduct(const VECTOR_2D a) const {\n return double_add(dX * a.dY, -dY * a.dX);\n }\n};\n\n\n\n/*----------------------------------------------*/\n\nstruct BALL_ST {\n DOUBLE dBegin;\n DOUBLE dEnd;\n\n BALL_ST (DOUBLE b, DOUBLE e) : dBegin(b), dEnd(e) {};\n}; \n\nbool cmpBegin (const BALL_ST &a, const BALL_ST &b) {\n return a.dBegin < b.dBegin;\n}\n\nbool cmpEnd (const BALL_ST &a, const BALL_ST &b) {\n return a.dEnd < b.dEnd;\n}\n\n/**\n * 角度の扱い \n */\nstatic inline DOUBLE normalizeAngle(DOUBLE dAngle)\n{\n DOUBLE dRet = dAngle;\n while (dRet < 0.0) {\n dRet += (2.0 * d_PI);\n }\n\n while ((2.0 * d_PI) < dAngle) {\n dRet -= (2.0 * d_PI);\n }\n\n return dRet;\n}\n\n#define MAX_N (1500)\nint main(void)\n{\n SQWORD sqN = inputSQWORD();\n SQWORD sqK = inputSQWORD();\n\n vector<BALL_ST> vecBalls;\n vecBalls.emplace_back(0, 0); /* dp用にインデックスを1はじまりにするためのダミー */\n for (SQWORD sqIdx = 0; sqIdx < sqN; sqIdx++) {\n SQWORD sqX = inputSQWORD();\n SQWORD sqY = inputSQWORD();\n SQWORD sqR = inputSQWORD();\n\n VECTOR_2D stVec((DOUBLE)sqX, (DOUBLE)sqY);\n DOUBLE dAngle = normalizeAngle(stVec.angle());\n DOUBLE dDist = stVec.dist();\n DOUBLE dRange = asin((DOUBLE)sqR / dDist);\n \n DOUBLE dBegin, dEnd;\n if (2 * d_PI < (dAngle + dRange)) {\n dBegin = 0.0;\n dEnd = dAngle + dRange - (2 * d_PI);\n } else {\n dBegin = MIN(MAX(dAngle - dRange, 0.0), d_PI);\n dEnd = MIN(MAX(dAngle + dRange, 0.0), d_PI); \n }\n\n\n\n// printf(\"%0.10f %0.10f\\n\", dBegin, dEnd);\n\n vecBalls.emplace_back(dBegin, dEnd);\n }\n sort(vecBalls.begin(), vecBalls.end(), cmpBegin);\n\n /**\n * dp[j][k] 左端がk番目のボールで、\n * j個を使っているときの最大の角度\n * dp[j][k] = max(dp[j-1][0] + angle, dp[j-1][1] + angle, dp[j-1][2] + angle ,,,, dp[j-1][k-1] + angle)\n * \n */\n static DOUBLE s_aadDpTbl[MAX_N + 1][MAX_N + 1];\n for (SQWORD sqIdxJ = 0; sqIdxJ <= sqN; sqIdxJ++) {\n for (SQWORD sqIdxK = 0; sqIdxK <= sqN; sqIdxK++) {\n s_aadDpTbl[sqIdxJ][sqIdxK] = 0;\n }\n }\n for (SQWORD sqIdxJ = 0; sqIdxJ < sqN; sqIdxJ++) {\n for (SQWORD sqIdxK = sqIdxJ; sqIdxK < sqN; sqIdxK++) {\n DOUBLE dPrevEnd = vecBalls[sqIdxK].dEnd;\n for (SQWORD sqNextBallIdx = sqIdxK + 1; sqNextBallIdx <= sqN; sqNextBallIdx++) {\n BALL_ST stBall = vecBalls[sqNextBallIdx];\n DOUBLE dAddAngle = 0;\n if (dPrevEnd < stBall.dBegin) {\n dAddAngle = stBall.dEnd - stBall.dBegin;\n// printf(\"> [%lld %lld %lld] %llf \", sqIdxJ, sqIdxK, sqNextBallIdx, dAddAngle / d_PI);\n s_aadDpTbl[sqIdxJ + 1][sqNextBallIdx] \n = max(s_aadDpTbl[sqIdxJ + 1][sqNextBallIdx], s_aadDpTbl[sqIdxJ][sqIdxK] + dAddAngle);\n } else {\n if (dPrevEnd < stBall.dEnd) {\n dAddAngle = stBall.dEnd - dPrevEnd;\n s_aadDpTbl[sqIdxJ + 1][sqNextBallIdx] \n = max(s_aadDpTbl[sqIdxJ + 1][sqNextBallIdx], s_aadDpTbl[sqIdxJ][sqIdxK] + dAddAngle);\n }\n// printf(\"< [%lld %lld %lld] %llf \", sqIdxJ, sqIdxK, sqNextBallIdx, dAddAngle / d_PI);\n }\n\n }\n }\n// printf(\"\\n\");\n }\n\n DOUBLE dAns = 0.0;\n for (SQWORD sqIdxJ = 1; sqIdxJ <= sqK; sqIdxJ++) {\n for (SQWORD sqIdxK = sqIdxJ; sqIdxK <= sqN; sqIdxK++) {\n// printf(\"%llf \", s_aadDpTbl[sqIdxJ][sqIdxK] / d_PI);\n dAns = max(dAns, s_aadDpTbl[sqIdxJ][sqIdxK]);\n }\n// printf(\"\\n\");\n }\n\n printf(\"%0.11f\\n\", dAns / d_PI);\n\n return 0;\n}", "accuracy": 0.1267605633802817, "time_ms": 490, "memory_kb": 21112, "score_of_the_acc": -0.6904, "final_rank": 19 }, { "submission_id": "aoj_2276_3916179", "code_snippet": "#pragma GCC optimize (\"O3\")\n\n#include <iostream>\n#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n#include <math.h>\n#include <map>\n#include <unordered_map>\n#include <set>\n#include <unordered_set>\n#include <queue>\n#include <set>\n#include <algorithm>\n#include <numeric>\n#include <list>\nusing namespace std;\n\nusing QWORD = uint64_t;\nusing SQWORD = int64_t;\nusing DWORD = uint32_t;\nusing SDWORD = int32_t;\nusing WORD = uint16_t;\nusing SWORD = int16_t;\nusing BYTE = uint8_t;\nusing SBYTE = int8_t;\nusing DOUBLE = double;\nusing FLOAT = float;\n\n#define MIN_SDWORD (-2147483648)\n#define MAX_SDWORD (2147483647)\n#define MIN_SBYTE (-128)\n#define MAX_SBYTE (127)\n\n#define MIN_SQWORD (0x8000000000000000)\n#define MAX_SQWORD (0x7FFFFFFFFFFFFFFF)\n\n#define MAX_QWORD (0xFFFFFFFFFFFFFFFF)\n#define MAX_DWORD (0xFFFFFFFF)\n#define MAX_WORD (0xFFFF)\n#define MAX_BYTE (0xFF)\n\n#define MAX_DOUBLE (1.0e+308)\n#define DOUBLE_EPS (1.0e-12)\n#define MIN_DOUBLE_N (-1.0e+308)\n\n#define ArrayLength(a) (sizeof(a) / sizeof(a[0]))\n\nstatic inline DOUBLE MAX(DOUBLE a, DOUBLE b) { return a > b ? a : b; }\nstatic inline QWORD MAX(QWORD a, QWORD b) { return a > b ? a : b; }\nstatic inline DWORD MAX(DWORD a, DWORD b) { return a > b ? a : b; }\nstatic inline SDWORD MAX(SDWORD a, SDWORD b) { return a > b ? a : b; }\nstatic inline DOUBLE MIN(DOUBLE a, DOUBLE b) { return a < b ? a : b; }\nstatic inline QWORD MIN(QWORD a, QWORD b) { return a < b ? a : b; }\nstatic inline DWORD MIN(DWORD a, DWORD b) { return a < b ? a : b; }\nstatic inline SDWORD MIN(SDWORD a, SDWORD b) { return a < b ? a : b; }\nstatic inline DOUBLE ABS(DOUBLE a) { return 0 < a ? a : -a; };\n\n#define BYTE_BITS (8)\n#define WORD_BITS (16)\n#define DWORD_BITS (32)\n#define QWORD_BITS (64)\n\n#define ANS_MOD (1000000007)\n\nstatic const DOUBLE d_PI = 3.14159265358979323846;\n\n\nstatic inline void inputStringSpSeparated(char *pcStr)\n{\n char *pcCur = pcStr;\n for (;;) {\n char c = getchar();\n if (('\\n' == c) || (EOF == c) || (' ' == c)) {\n break;\n }\n *pcCur = c;\n pcCur++;\n }\n *pcCur = '\\0';\n}\n\nstatic inline void inputString(char *pcStr)\n{\n char *pcCur = pcStr;\n for (;;) {\n char c = getchar();\n if (('\\n' == c) || (EOF == c)) {\n break;\n }\n *pcCur = c;\n pcCur++;\n }\n *pcCur = '\\0';\n}\n\n\nstatic inline SQWORD inputSQWORD(void)\n{\n SQWORD sqNumber = 0;\n SQWORD sqMultiplier = 1;\n bool bRead = false;\n for (;;) {\n char c = getchar();\n if (!bRead) {\n if ('-' == c) {\n sqMultiplier = -1;\n }\n }\n if (('0' <= c) && (c <= '9')) {\n sqNumber *= 10LL;\n sqNumber += (SQWORD)(c - '0');\n bRead = true;\n } else {\n if (bRead) {\n return sqNumber * sqMultiplier;\n }\n }\n }\n}\n\n\nstatic inline SDWORD inputSDWORD(void)\n{\n SDWORD lNumber = 0;\n SDWORD lMultiplier = 1;\n bool bRead = false;\n for (;;) {\n char c = getchar();\n if (!bRead) {\n if ('-' == c) {\n lMultiplier = -1;\n }\n }\n if (('0' <= c) && (c <= '9')) {\n lNumber *= 10;\n lNumber += (c - '0');\n bRead = true;\n } else {\n if (bRead) {\n return lNumber * lMultiplier;\n }\n }\n }\n}\n\nstatic inline DOUBLE inputFP(void)\n{\n DOUBLE dInt = 0.0;\n DOUBLE dFrac = 0.0;\n DOUBLE dMultiplier = 1.0;\n DWORD dwFpCnt = 0;\n DOUBLE *pdCur = &dInt;\n bool bRead = false;\n for (;;) {\n char c = getchar();\n if (!bRead) {\n if ('-' == c) {\n dMultiplier = -1;\n }\n }\n if ('.' == c) {\n pdCur = &dFrac;\n } else if (('0' <= c) && (c <= '9')) {\n (*pdCur) *= 10;\n (*pdCur) += (DOUBLE)(c - '0');\n bRead = true;\n if (pdCur == &dFrac) {\n dwFpCnt++;\n }\n } else {\n if (bRead) {\n return dMultiplier * (dInt + dFrac / (pow((DOUBLE)10.0 , (DOUBLE)dwFpCnt)));\n }\n }\n }\n}\n\n/*----------------------------------------------*/\n/**\n * mod による操作ライブラリ\n */\n#define ANS_MOD (1000000007)\n\nclass MODINT {\n static SQWORD MOD;\n SQWORD m_x;\n\npublic:\n MODINT(SQWORD val) {\n m_x = (val % MOD + MOD) % MOD;\n };\n MODINT() {\n m_x = 0;\n }\n static void Init(SQWORD sqMod) {\n MOD = sqMod;\n }\n\n\tMODINT& operator+= (const MODINT a)\n {\n m_x = (m_x + a.m_x) % MOD; \n return *this;\n };\n\tMODINT& operator-= (const MODINT a)\n { \n m_x = (m_x - a.m_x + MOD) % MOD; \n return *this;\n };\n\tMODINT& operator*= (const MODINT a)\n {\n m_x = (m_x * a.m_x) % MOD;\n return *this;\n };\n MODINT pow(SQWORD t) const {\n if (!t) return 1;\n MODINT a = pow(t>>1);\n a *= a;\n if (t&1) a *= *this;\n return a;\n }\n\tMODINT operator+ (const MODINT a) const {\n\t\tMODINT res(*this);\n\t\treturn (res += a);\n\t}\n\tMODINT operator- (const MODINT a) const {\n\t\tMODINT res(*this);\n\t\treturn (res -= a);\n\t}\n\tMODINT operator* (const MODINT a) const {\n\t\tMODINT res(*this);\n\t\treturn (res *= a);\n\t}\n\tMODINT operator/ (const MODINT a) const {\n\t\tMODINT res(*this);\n\t\treturn (res /= a);\n\t}\n\n /* 逆元 */\n MODINT inv() const {\n return pow(MOD-2);\n }\n\n /* 除算 */\n MODINT& operator/=(const MODINT a) {\n return (*this) *= a.inv();\n } \n\n /* 整数版 */\n\tMODINT& operator+= (const SQWORD a) {*this += MODINT(a); return *this;};\n\tMODINT& operator-= (const SQWORD a) {*this -= MODINT(a); return *this;};\n\tMODINT& operator*= (const SQWORD a) {*this *= MODINT(a); return *this;};\n\tMODINT& operator/= (const SQWORD a) {*this /= MODINT(a); return *this;};\n\n SQWORD getVal() { return m_x; };\n};\nSQWORD MODINT::MOD = ANS_MOD;\n\n\n/*----------------------------------------------*/\n\n#define EPS 1e-10\nDOUBLE double_add(DOUBLE a, DOUBLE b) {\n if (ABS(a + b) < EPS * (ABS(a) + ABS(b))) {\n return 0;\n }\n return a+b;\n}\n\nstruct VECTOR_2D {\n DOUBLE dX;\n DOUBLE dY;\n VECTOR_2D(DOUBLE x, DOUBLE y) : dX(x), dY(y) {};\n VECTOR_2D() : dX(0), dY(0) {};\n\n /* 加算 */\n\tVECTOR_2D& operator+= (const VECTOR_2D a)\n { \n dX = dX + a.dX;\n dY = dY + a.dY;\n return *this;\n };\n const VECTOR_2D operator+ (const VECTOR_2D a) const {\n\t\treturn VECTOR_2D(dX + a.dX, dY + a.dY); \n };\n\n /* 減算 */\n\tVECTOR_2D& operator-= (const VECTOR_2D a)\n { \n dX = dX - a.dX;\n dY = dY - a.dY;\n return *this;\n };\n const VECTOR_2D operator- (const VECTOR_2D a) const {\n\t\treturn VECTOR_2D(dX - a.dX, dY - a.dY);\n };\n\n /* 定数倍 */\n\tVECTOR_2D& operator*= (const DOUBLE a)\n { \n dX = dX * a;\n dY = dY * a;\n return *this;\n };\n const VECTOR_2D operator* (const DOUBLE a) const {\n\t\treturn VECTOR_2D(dX * a, dY * a); \n };\n\n /* 比較 */\n bool operator< (const VECTOR_2D &a) {\n if (dX == a.dX) {\n return (dY < a.dY);\n }\n return dX < a.dX;\n }\n\n DOUBLE norm(void) const {\n return (dX * dX + dY * dY);\n }\n\n DOUBLE dist(void) const {\n return sqrtl(norm());\n }\n\n DOUBLE angle(void) const {\n return atan2(dY, dX);\n }\n\n DOUBLE dotproduct(const VECTOR_2D a) const {\n return double_add(dX * a.dX, dY * a.dY);\n }\n\n DOUBLE crossproduct(const VECTOR_2D a) const {\n return double_add(dX * a.dY, -dY * a.dX);\n }\n};\n\n\n\n/*----------------------------------------------*/\n\nstruct BALL_ST {\n DOUBLE dBegin;\n DOUBLE dEnd;\n\n BALL_ST (DOUBLE b, DOUBLE e) : dBegin(b), dEnd(e) {};\n}; \n\nbool cmpBegin (const BALL_ST &a, const BALL_ST &b) {\n return a.dBegin < b.dBegin;\n}\n\nbool cmpEnd (const BALL_ST &a, const BALL_ST &b) {\n return a.dEnd < b.dEnd;\n}\n\n/**\n * 角度の扱い \n */\nstatic inline DOUBLE normalizeAngle(DOUBLE dAngle)\n{\n DOUBLE dRet = dAngle;\n while (dRet < 0.0) {\n dRet += (2.0 * d_PI);\n }\n\n while ((2.0 * d_PI) < dAngle) {\n dRet -= (2.0 * d_PI);\n }\n\n return dRet;\n}\n\n#define MAX_N (1500)\nint main(void)\n{\n SQWORD sqN = inputSQWORD();\n SQWORD sqK = inputSQWORD();\n\n vector<BALL_ST> vecBalls;\n vecBalls.emplace_back(0, 0); /* dp用にインデックスを1はじまりにするためのダミー */\n for (SQWORD sqIdx = 0; sqIdx < sqN; sqIdx++) {\n SQWORD sqX = inputSQWORD();\n SQWORD sqY = inputSQWORD();\n SQWORD sqR = inputSQWORD();\n\n VECTOR_2D stVec((DOUBLE)sqX, (DOUBLE)sqY);\n DOUBLE dAngle = normalizeAngle(stVec.angle());\n DOUBLE dDist = stVec.dist();\n DOUBLE dRange = asin((DOUBLE)sqR / dDist);\n \n DOUBLE dBegin, dEnd;\n if (2 * d_PI < (dAngle + dRange)) {\n dBegin = 0.0;\n dEnd = dAngle + dRange - (2 * d_PI);\n } else {\n dBegin = MIN(MAX(dAngle - dRange, 0.0), d_PI);\n dEnd = MIN(dAngle + dRange, d_PI); \n }\n\n\n\n// printf(\"%0.10f %0.10f\\n\", dBegin, dEnd);\n\n vecBalls.emplace_back(dBegin, dEnd);\n }\n sort(vecBalls.begin(), vecBalls.end(), cmpBegin);\n\n /**\n * dp[j][k] 左端がk番目のボールで、\n * j個を使っているときの最大の角度\n * dp[j][k] = max(dp[j-1][0] + angle, dp[j-1][1] + angle, dp[j-1][2] + angle ,,,, dp[j-1][k-1] + angle)\n * \n */\n static DOUBLE s_aadDpTbl[MAX_N + 1][MAX_N + 1];\n for (SQWORD sqIdxJ = 0; sqIdxJ <= sqN; sqIdxJ++) {\n for (SQWORD sqIdxK = 0; sqIdxK <= sqN; sqIdxK++) {\n s_aadDpTbl[sqIdxJ][sqIdxK] = 0;\n }\n }\n for (SQWORD sqIdxJ = 0; sqIdxJ < sqN; sqIdxJ++) {\n for (SQWORD sqIdxK = sqIdxJ; sqIdxK < sqN; sqIdxK++) {\n DOUBLE dPrevEnd = vecBalls[sqIdxK].dEnd;\n for (SQWORD sqNextBallIdx = sqIdxK; sqNextBallIdx <= sqN; sqNextBallIdx++) {\n BALL_ST stBall = vecBalls[sqNextBallIdx];\n DOUBLE dAddAngle;\n if (dPrevEnd < stBall.dBegin) {\n dAddAngle = stBall.dEnd - stBall.dBegin;\n// printf(\"> [%lld %lld %lld] %llf \", sqIdxJ, sqIdxK, sqNextBallIdx, dAddAngle / d_PI);\n s_aadDpTbl[sqIdxJ + 1][sqNextBallIdx] \n = max(s_aadDpTbl[sqIdxJ + 1][sqNextBallIdx], s_aadDpTbl[sqIdxJ][sqIdxK] + dAddAngle);\n } else {\n if (dPrevEnd < stBall.dEnd) {\n dAddAngle = max(0.0, stBall.dEnd - dPrevEnd);\n s_aadDpTbl[sqIdxJ + 1][sqNextBallIdx] \n = max(s_aadDpTbl[sqIdxJ + 1][sqNextBallIdx], s_aadDpTbl[sqIdxJ][sqIdxK] + dAddAngle);\n }\n// printf(\"< [%lld %lld %lld] %llf \", sqIdxJ, sqIdxK, sqNextBallIdx, dAddAngle / d_PI);\n }\n\n }\n }\n// printf(\"\\n\");\n }\n\n DOUBLE dAns = 0.0;\n for (SQWORD sqIdxJ = 1; sqIdxJ <= sqK; sqIdxJ++) {\n for (SQWORD sqIdxK = sqIdxJ; sqIdxK <= sqN; sqIdxK++) {\n// printf(\"%llf \", s_aadDpTbl[sqIdxJ][sqIdxK] / d_PI);\n dAns = max(dAns, s_aadDpTbl[sqIdxJ][sqIdxK]);\n }\n// printf(\"\\n\");\n }\n\n printf(\"%0.11f\\n\", dAns / d_PI);\n\n return 0;\n}", "accuracy": 0.1267605633802817, "time_ms": 540, "memory_kb": 21088, "score_of_the_acc": -0.7616, "final_rank": 20 }, { "submission_id": "aoj_2276_3913623", "code_snippet": "#include <bits/stdc++.h>\n#ifdef LOCAL\n#include \"../cxx-prettyprint/prettyprint.hpp\"\n#endif\nusing namespace std;\n\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef pair<int, int> P;\n\n#define REP(i, n) for (int (i) = 0 ; (i) < (int)(n) ; ++(i))\n#define REPN(i, m, n) for (int (i) = m ; (i) < (int)(n) ; ++(i))\n#define REP_REV(i, n) for (int (i) = (int)(n) - 1 ; (i) >= 0 ; --(i))\n#define REPN_REV(i, m, n) for (int (i) = (int)(n) - 1 ; (i) >= m ; --(i))\n#define ALL(x) x.begin(), x.end()\n\n#define INF ((1 << 29)-1)\n#define MOD (1000000007)\n\n#define print2D(h, w, arr) REP(i, h) { REP(j, w) cout << arr[i][j] << \" \"; cout << endl; }\ntemplate<class T> void print(const T& x){cout << x << endl;}\ntemplate<class T, class... A> void print(const T& first, const A&... rest) { cout << first << \" \"; print(rest...); }\nstruct PreMain {PreMain(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(20);}} premain;\n\n// ベクトル\nnamespace Geom {\n\n const double eps = 1e-9;\n\n struct Point {\n double x, y;\n explicit Point(double x=0, double y=0): x(x), y(y) {}\n Point& operator+=(const Point& v){ x += v.x; y+= v.y; return *this;}\n Point operator+(const Point& v) const{return Point(*this) += v;}\n Point& operator-=(const Point& v){x -= v.x; y-= v.y; return *this;}\n Point operator-(const Point& v) const{return Point(*this) -= v;}\n Point& operator*=(double s){x *= s; y *= s; return *this;}\n Point operator*(double s) const{return Point(*this) *= s;}\n Point& operator/=(double s){x /= s; y /= s; return *this;}\n Point operator/(double s) const{return Point(*this) /= s;}\n double dot(const Point& v) const {return x*v.x + y*v.y;} /* 内積 */\n double cross(const Point& v) const {return x*v.y - v.x*y;} /* 外積 */\n double norm2() const {return x*x + y*y;} /* ノルムの二乗*/\n double norm() const {return sqrt(norm2());} /* ノルム */\n int quadrant() const { if(y > 0) return x>0 ? 1:2; else return x>0 ? 4:3;} /* 象限判定 */\n Point rot(double theta) {return Point(cos(theta) * x - sin(theta) * y, sin(theta) * x + cos(theta) * y);} // 回転\n Point rot90() {return Point(-y, x);} // 90度回転\n };\n\n ostream& operator<<(ostream& os, const Point& v) {os<<\"(\"<<v.x<<\",\"<<v.y<<\")\"; return os;}\n istream& operator>>(istream& is, Point& v){ is >> v.x >> v.y; return is;}\n\n // 点a, b, cからなる三角形の面積\n double calc_triangle_area(Point a, Point b, Point c) {\n Point v1 = b - a;\n Point v2 = c - a;\n return abs(v1.cross(v2)) / 2;\n }\n\n // 点a, bを通る直線と点cとの距離\n double calc_distance_between_point_and_line(Point a, Point b, Point c) {\n return abs((b-a).cross(c-a)) / (b-a).norm();\n }\n\n // 点a, bを端点とする線分と点cとの距離\n double calc_distance_between_point_and_segment(Point a, Point b, Point c) {\n if ((b-a).dot(c-a) < eps) return (c-a).norm();\n if ((a-b).dot(c-b) < eps) return (c-b).norm();\n return calc_distance_between_point_and_line(a, b, c);\n }\n\n // 点a, bを端点とする線分と点cと点dを端点とする線分の距離\n double calc_distance_between_segments(Point a, Point b, Point c, Point d) {\n double d0 = calc_distance_between_point_and_segment(a, b, c);\n double d1 = calc_distance_between_point_and_segment(a, b, d);\n double d2 = calc_distance_between_point_and_segment(c, d, a);\n double d3 = calc_distance_between_point_and_segment(c, d, b);\n return min(min(d0, d1), min(d2, d3));\n }\n\n enum RELATION_BETWEEN_SEGMENT_AND_POINT {\n COUNTER_CLOCKWISE = 1, // p1とp2のp0を挟んだ位置関係が反時計回り\n CLOCKWISE = -1, // 時計回り\n ONLINE_BACK = 2, // p1とp2がp0を挟んで直線状にある\n ONLINE_FRONT = -2, // p0とp2の間にp1がある\n ON_SEGMENT = 0, // p0とp1の間にp2がある\n };\n\n // 点cが点a, bを端点とする線分のどちら側にあるか、enumで返す\n RELATION_BETWEEN_SEGMENT_AND_POINT ccw_ex(Point p0, Point p1, Point p2) {\n Point v1 = p1 - p0;\n Point v2 = p2 - p0;\n if(v1.cross(v2) > eps) return COUNTER_CLOCKWISE; // 1\n if(v1.cross(v2) < -eps) return CLOCKWISE; // -1\n if(v1.dot(v2) < -eps) return ONLINE_BACK; // 2\n if(v1.norm() < v2.norm()) return ONLINE_FRONT; // -2\n return ON_SEGMENT; // 0\n }\n\n // 点cが点a, bを端点とする線分のどちら側にあるか\n int ccw(Point p0, Point p1, Point p2) {\n Point v1 = p1 - p0;\n Point v2 = p2 - p0;\n if(v1.cross(v2) > eps) return 1;\n if(v1.cross(v2) < -eps) return -1;\n return 0;\n }\n\n // p1,p2を端点とする線分Aとp3,p4を端点とする線分Bの交差判定\n // include_end=trueなら端点が重なっていても交差しているとみなす\n bool intersect(Point p1, Point p2, Point p3, Point p4, bool include_end=true) {\n\n bool is_otherside;\n bool is_inside;\n\n if (include_end){\n is_otherside = ccw(p1,p2,p3) * ccw(p1,p2,p4) <= 0; // Aの両側にBの端点があるかどうか。どちらかがAの直線状にいてもOK\n is_inside = ccw(p3,p4,p1) * ccw(p3,p4,p2) <= 0; // Aの端点の内側にBがいるかどうか。\n } else {\n is_otherside = ccw(p1,p2,p3) * ccw(p1,p2,p4) < 0; // Aの両側にBの端点があるかどうか\n is_inside = ccw(p3,p4,p1) * ccw(p3,p4,p2) < 0; // Aの端点の内側にBがいるかどうか\n }\n return is_otherside && is_inside;\n }\n\n\n // 円\n struct Circle {\n Point c;\n double r;\n Circle(){}\n Circle(Point c, double r):c(c),r(r){}\n\n // 円が点pを含むかどうか include_circumは円周上を含むとするかどうか\n bool does_include_point(Point p, bool include_circum = true){\n double d = (p - c).norm();\n if (include_circum){\n return d < r + eps;\n } else {\n return d < r - eps;\n }\n }\n };\n\n // 点p1, p2を通る半径rの円を返す\n vector<Circle> calc_circle_on_two_points(Point p1, Point p2, double r){\n\n Point p12_half = (p2 - p1) / 2.0;\n\n double n_vec_norm2 = r*r - p12_half.norm2();\n if (n_vec_norm2 < 0) return vector<Circle>();\n\n double n_vec_norm = sqrt(n_vec_norm2);\n Point n_vec = p12_half.rot90() * n_vec_norm / p12_half.norm();\n\n vector<Circle> ret;\n ret.emplace_back(p1 + p12_half + n_vec, 1.0);\n ret.emplace_back(p1 + p12_half - n_vec, 1.0);\n return ret;\n }\n}\n\ndouble getRadian(double x, double y) {\n double radian = atan2(y, x);\n return radian;\n}\n\ndouble getDegree(double radian) {\n if(radian < 0) {\n radian = radian + 2 * M_PI;\n }\n return (radian*360) / (2*M_PI);\n}\n\nint main() {\n#ifdef LOCAL\n ifstream in(\"../in.txt\"); cin.rdbuf(in.rdbuf());\n#endif\n\n using namespace Geom;\n\n int n, k;\n cin >> n >> k;\n\n vector<Point> point(n);\n vector<double> radius(n);\n vector<pair<double, double> > angle(n);\n REP(i, n) cin >> point[i] >> radius[i];\n\n REP(i, n) {\n double d = sqrt(point[i].x*point[i].x + point[i].y*point[i].y);\n double center_angle;\n if(point[i].x == 0) {\n if(point[i].y == 0) {\n cout << \"1\" << endl;\n return 0;\n } else if(point[i].y > 0) {\n center_angle = 90;\n } else {\n center_angle = 270;\n }\n } else {\n center_angle = getDegree(getRadian(point[i].x, point[i].y));\n }\n\n double center_tangent_angle = getDegree(getRadian(sqrt(d*d - radius[i]*radius[i]), radius[i]));\n angle[i].first = center_angle - center_tangent_angle;\n angle[i].second = center_angle + center_tangent_angle;\n if(0 <= center_angle && center_angle <= 90) {\n if(angle[i].first < 0){\n angle[i].first = 0;\n }\n }\n else if(center_angle <= 180) {\n if(angle[i].second > 180){\n angle[i].second = 180;\n }\n }\n else if(center_angle <= 270.0) {\n if(angle[i].first < 180){\n angle[i].second = 180;\n }\n else{\n angle[i].first = 0, angle[i].second = 0;\n }\n }\n else{\n if(angle[i].second > 360){\n angle[i].first = 0, angle[i].second -=360;\n }\n else{\n angle[i].first = 0, angle[i].second = 0;\n }\n }\n //printf(\"center = %f\\n\", center_angle);\n //printf(\"first = %f, second = %f\\n\", angle[i].first, angle[i].second);\n }\n\n REP(i, angle.size()){\n if(angle[i].first == 0 && angle[i].second == 0){\n angle.erase(angle.begin() + i); \n }\n }\n\n /* 包含してるものを削除 */\n REP(i, angle.size()) REP(j, angle.size()){\n if(i == j) continue;\n if(angle[j].first <= angle[i].first && angle[i].second <= angle[j].second) {\n angle.erase(angle.begin() + i);\n i--;\n break;\n }\n }\n\n REP(i, angle.size()){\n //printf(\"first = %f, second = %f\\n\", angle[i].first, angle[i].second);\n }\n\n angle.insert(angle.begin(), {0, 0});\n sort(angle.begin(), angle.end());\n\n vector<vector<double> > dp;\n dp.assign(n+1, vector<double>(k+1, 0));\n\n int N = angle.size()-1;\n REPN(i, 1, N + 1){\n //\n int l = i;\n while (l > 0){\n l--;\n if (angle[l].second < angle[i].first){\n break;\n }\n }\n REPN(j, 1, k+1){\n dp[i][j] = dp[i-1][j];\n dp[i][j] = max(dp[i][j], dp[l][j-1] + angle[i].second - angle[i].first);\n dp[i][j] = max(dp[i][j], dp[l+1][j-1] + angle[i].second - angle[l+1].second);\n }\n }\n\n print(dp[N][k] / 180);\n\n\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 20884, "score_of_the_acc": -0.0026, "final_rank": 1 }, { "submission_id": "aoj_2276_3912434", "code_snippet": "#define _USE_MATH_DEFINES\n#include <algorithm>\n#include <iostream>\n#include <vector>\n#include <cmath>\n#include <numeric>\n#include <tuple>\n#include <iomanip>\n#include <assert.h>\n\n#define sqr(x) std::pow(x, 2.0)\n#define INF 1e10\n#define EPS 1e-10\n#define REP(i, n) for (int i = 0; i < n; i++)\n#define POS_OR_INF(x) (x > 0 ? x : INF)\n\nusing namespace std;\n\nusing ld = long double;\n\nstruct Point {\n\tPoint() = default;\n\tPoint(ld x, ld y) : x(x), y(y) {}\n\tPoint(const Point& s, const Point& e) : x(e.x - s.x), y(e.y - s.y) {}\n\n\tPoint normalized(void) const {\n\t\treturn Point(x / norm(), y / norm());\n\t}\n\n\tld norm(void) const {\n\t\treturn sqrt(x * x + y * y);\n\t}\n\n\tPoint flipped(void) const {\n\t\treturn Point(-x, -y);\n\t}\n\n\tPoint normal_vector(bool normalize = false) const {\n\t\tauto vec = Point(-y, x);\n\t\treturn normalize ? vec.normalized() : vec;\n\t}\n\n\tld x, y;\n};\n\nusing Vector = Point;\n\ninline Vector points2vec(const Point& s, const Point& e) {\n\treturn Vector(e.x - s.x, e.y - s.y);\n}\n\ninline ld distance(const Point& s, const Point& e) {\n\treturn points2vec(s, e).norm();\n}\n\ninline ld distance_square(const Point& s, const Point& e) {\n\treturn pow(points2vec(s, e).norm(), 2.0);\n}\n\ninline Vector operator * (const Vector& l, ld r) {\n\treturn Vector(l.x * r, l.y * r);\n}\n\ninline Vector operator * (ld l, const Vector& r) {\n\treturn r * l;\n}\n\ninline Vector operator / (const Vector& l, ld r) {\n\treturn Vector(l.x / r, l.y / r);\n}\n\ninline Point operator + (const Point& l, const Point& r) {\n\treturn Point(l.x + r.x, l.y + r.y);\n}\n\ninline Point operator - (const Point& l, const Point& r) {\n\treturn Point(l.x - r.x, l.y - r.y);\n}\n\ninline istream& operator >> (istream& is, Point& p) {\n\treturn is >> p.x >> p.y;\n}\n\nint main(int argc, char** argv) {\n\tint N, K;\n\n\tcin >> N >> K;\n\tcout << fixed << setprecision(20);\n\n\tvector<pair<double, double>> a(N), b;\n\n\tfor (int i = 0; i < N; i++) {\n\t\tPoint p;\n\t\tdouble r;\n\t\tcin >> p >> r;\n\n\t\tdouble theta = asin(r/p.norm());\n\t\tdouble rad = atan2(p.y, p.x);\n\n\t\tif (rad < -M_PI / 2) rad += 2 * M_PI;\n\n\t\ta[i].first = min(M_PI, max(0.0, rad - theta));\n\t\ta[i].second = min(M_PI, max(0.0, rad + theta));\n\t}\n\n\tfor (int i = 0; i < N; i++) {\n\t\tbool included = false;\n\t\tfor (int j = 0; j < N; j++) {\n\t\t\tif (a[i].first > a[j].first && a[i].second < a[j].second)\n\t\t\t\tincluded = true;\n\t\t}\n\t\tif (!included) {\n\t\t\tb.push_back(a[i]);\n\t\t}\n\t}\n\n\tsort(b.begin(), b.end());\n\n\tint M = b.size();\n\n\tvector<vector<double>> dp(K + 1, vector<double>(M + 1, 0));\n\tdouble ans = 0;\n\n\tfor (int i = 1; i <= K; i++) {\n\t\tfor (int j = 0; j < M; j++) {\n\t\t\tdouble best = 0;\n\t\t\tint k = 0;\n\n\t\t\twhile (b[k].second < b[j].first) {\n\t\t\t\tbest = max(best, dp[i-1][k]);\n\t\t\t\tk++;\n\t\t\t}\n\t\t\t\n\t\t\tdp[i][j] = max(dp[i][j], best + b[j].second - b[j].first);\n\n\t\t\t//for (; k < j; k++) {\n\t\t\tdp[i][j] = max(dp[i][j], dp[i-1][k] + b[j].second - b[k].second);\n\t\t\t//}\n\t\t\tans = max(ans, dp[i][j]);\n\t\t}\n\t}\n\n\tcout << ans / M_PI;\n}", "accuracy": 1, "time_ms": 340, "memory_kb": 20696, "score_of_the_acc": -0.4722, "final_rank": 13 }, { "submission_id": "aoj_2276_3894169", "code_snippet": "// aribon3-6_f\n#include <bits/stdc++.h>\n#ifdef LOCAL\n#include \"../cxx-prettyprint/prettyprint.hpp\"\n#endif\nusing namespace std;\n\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef pair<int, int> P;\n\n#define REP(i, n) for (int (i) = 0 ; (i) < (int)(n) ; ++(i))\n#define REPN(i, m, n) for (int (i) = m ; (i) < (int)(n) ; ++(i))\n#define REP_REV(i, n) for (int (i) = (int)(n) - 1 ; (i) >= 0 ; --(i))\n#define REPN_REV(i, m, n) for (int (i) = (int)(n) - 1 ; (i) >= m ; --(i))\n#define ALL(x) x.begin(), x.end()\n\n#define INF ((1 << 29)-1)\n#define MOD (1000000007)\n\n#define print2D(h, w, arr) REP(i, h) { REP(j, w) cout << arr[i][j] << \" \"; cout << endl; }\ntemplate<class T> void print(const T& x){cout << x << endl;}\ntemplate<class T, class... A> void print(const T& first, const A&... rest) { cout << first << \" \"; print(rest...); }\nstruct PreMain {PreMain(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(20);}} premain;\n\n// ベクトル\nnamespace Geom {\n #define _USE_MATH_DEFINES\n\n const double eps = 1e-9;\n\n struct Point {\n double x, y;\n explicit Point(double x=0, double y=0): x(x), y(y) {}\n Point& operator+=(const Point& v){ x += v.x; y+= v.y; return *this;}\n Point operator+(const Point& v) const{return Point(*this) += v;}\n Point& operator-=(const Point& v){x -= v.x; y-= v.y; return *this;}\n Point operator-(const Point& v) const{return Point(*this) -= v;}\n Point& operator*=(double s){x *= s; y *= s; return *this;}\n Point operator*(double s) const{return Point(*this) *= s;}\n Point& operator/=(double s){x /= s; y /= s; return *this;}\n Point operator/(double s) const{return Point(*this) /= s;}\n double dot(const Point& v) const {return x*v.x + y*v.y;} /* 内積 */\n double cross(const Point& v) const {return x*v.y - v.x*y;} /* 外積 */\n double norm2() const {return x*x + y*y;} /* ノルムの二乗*/\n double norm() const {return sqrt(norm2());} /* ノルム */\n int quadrant() const { if(y > 0) return x>0 ? 1:2; else return x>0 ? 4:3;} /* 象限判定 */\n Point rot(double theta) {return Point(cos(theta) * x - sin(theta) * y, sin(theta) * x + cos(theta) * y);} // 回転\n Point rot90() {return Point(-y, x);} // 90度回転\n };\n ostream& operator<<(ostream& os, const Point& v) {os<<\"(\"<<v.x<<\",\"<<v.y<<\")\"; return os;}\n istream& operator>>(istream& is, Point& v){ is >> v.x >> v.y; return is;}\n\n // 点a, b, cからなる三角形の面積\n double calc_triangle_area(Point a, Point b, Point c) {\n Point v1 = b - a;\n Point v2 = c - a;\n return abs(v1.cross(v2)) / 2;\n }\n\n // 点a, bを通る直線と点cとの距離\n double calc_distance_between_point_and_line(Point a, Point b, Point c) {\n return abs((b-a).cross(c-a)) / (b-a).norm();\n }\n\n // 点a, bを端点とする線分と点cとの距離\n double calc_distance_between_point_and_segment(Point a, Point b, Point c) {\n if ((b-a).dot(c-a) < eps) return (c-a).norm();\n if ((a-b).dot(c-b) < eps) return (c-b).norm();\n return calc_distance_between_point_and_line(a, b, c);\n }\n\n // 点a, bを端点とする線分と点cと点dを端点とする線分の距離\n double calc_distance_between_segments(Point a, Point b, Point c, Point d) {\n double d0 = calc_distance_between_point_and_segment(a, b, c);\n double d1 = calc_distance_between_point_and_segment(a, b, d);\n double d2 = calc_distance_between_point_and_segment(c, d, a);\n double d3 = calc_distance_between_point_and_segment(c, d, b);\n return min(min(d0, d1), min(d2, d3));\n }\n\n enum RELATION_BETWEEN_SEGMENT_AND_POINT {\n COUNTER_CLOCKWISE = 1, // p1とp2のp0を挟んだ位置関係が反時計回り\n CLOCKWISE = -1, // 時計回り\n ONLINE_BACK = 2, // p1とp2がp0を挟んで直線状にある\n ONLINE_FRONT = -2, // p0とp2の間にp1がある\n ON_SEGMENT = 0, // p0とp1の間にp2がある\n };\n\n // 点cが点a, bを端点とする線分のどちら側にあるか、enumで返す\n RELATION_BETWEEN_SEGMENT_AND_POINT ccw_ex(Point p0, Point p1, Point p2) {\n Point v1 = p1 - p0;\n Point v2 = p2 - p0;\n if(v1.cross(v2) > eps) return COUNTER_CLOCKWISE; // 1\n if(v1.cross(v2) < -eps) return CLOCKWISE; // -1\n if(v1.dot(v2) < -eps) return ONLINE_BACK; // 2\n if(v1.norm() < v2.norm()) return ONLINE_FRONT; // -2\n return ON_SEGMENT; // 0\n }\n\n // 点cが点a, bを端点とする線分のどちら側にあるか\n int ccw(Point p0, Point p1, Point p2) {\n Point v1 = p1 - p0;\n Point v2 = p2 - p0;\n if(v1.cross(v2) > eps) return 1;\n if(v1.cross(v2) < -eps) return -1;\n return 0;\n }\n\n // p1,p2を端点とする線分Aとp3,p4を端点とする線分Bの交差判定\n // include_end=trueなら端点が重なっていても交差しているとみなす\n bool intersect(Point p1, Point p2, Point p3, Point p4, bool include_end=true) {\n\n bool is_otherside;\n bool is_inside;\n\n if (include_end){\n is_otherside = ccw(p1,p2,p3) * ccw(p1,p2,p4) <= 0; // Aの両側にBの端点があるかどうか。どちらかがAの直線状にいてもOK\n is_inside = ccw(p3,p4,p1) * ccw(p3,p4,p2) <= 0; // Aの端点の内側にBがいるかどうか。\n } else {\n is_otherside = ccw(p1,p2,p3) * ccw(p1,p2,p4) < 0; // Aの両側にBの端点があるかどうか\n is_inside = ccw(p3,p4,p1) * ccw(p3,p4,p2) < 0; // Aの端点の内側にBがいるかどうか\n }\n return is_otherside && is_inside;\n }\n\n\n // 円\n struct Circle {\n Point c;\n double r;\n Circle(){}\n Circle(Point c, double r):c(c),r(r){}\n\n // 円が点pを含むかどうか include_circumは円周上を含むとするかどうか\n bool does_include_point(Point p, bool include_circum = true){\n double d = (p - c).norm();\n if (include_circum){\n return d < r + eps;\n } else {\n return d < r - eps;\n }\n }\n };\n ostream& operator<<(ostream& os, const Circle& c) {os<<\"(\"<<c.c<<\",\"<<c.r<<\")\"; return os;}\n istream& operator>>(istream& is, Circle& c){ is >> c.c >> c.r; return is;}\n\n // 点p1, p2を通る半径rの円を返す\n vector<Circle> calc_circle_on_two_points(Point p1, Point p2, double r){\n\n Point p12_half = (p2 - p1) / 2.0;\n\n double n_vec_norm2 = r*r - p12_half.norm2();\n if (n_vec_norm2 < 0) return vector<Circle>();\n\n double n_vec_norm = sqrt(n_vec_norm2);\n Point n_vec = p12_half.rot90() * n_vec_norm / p12_half.norm();\n\n vector<Circle> ret;\n ret.emplace_back(p1 + p12_half + n_vec, 1.0);\n ret.emplace_back(p1 + p12_half - n_vec, 1.0);\n return ret;\n }\n\n // 点pを通る、円Cの2つの接線の傾きの角度を返す [0, 2π]で返す\n pair<double, double> calc_tangent_line_on_circle(Circle c, Point p){\n Point pc = c.c - p;\n double angle0 = atan2(pc.y, pc.x);\n double l2 = pc.norm2() - c.r*c.r;\n if (l2 < 0) {\n return {0, 2*M_PI};\n }\n double l = sqrt(l2);\n double angle1 = angle0 - atan2(c.r, l);\n double angle2 = angle0 + atan2(c.r, l);\n angle1 = angle1 >= 0 ? angle1 : angle1 + 2*M_PI;\n angle2 = angle2 >= 0 ? angle2 : angle2 + 2*M_PI;\n\n return {angle1, angle2};\n }\n\n}\n\n\nint main() {\n#ifdef LOCAL\n ifstream in(\"../arg.txt\"); cin.rdbuf(in.rdbuf());\n#endif\n\n using namespace Geom;\n\n int N, K;\n cin >> N >> K;\n vector<Circle> circles(N);\n REP(i, N) cin >> circles[i];\n\n vector<pair<double, double>> intervals(N);\n REP(i, N){\n auto thetas = calc_tangent_line_on_circle(circles[i], Point(0.0, 0.0));\n if (thetas.first == 0 && thetas.second == 2*M_PI) {\n print(1.0);\n return 0;\n }\n\n if (thetas.first >= M_PI && thetas.second >= M_PI){\n intervals[i] = {0, 0};\n } else if (thetas.first >= M_PI) {\n intervals[i] = {0, thetas.second};\n } else if (thetas.second >= M_PI){\n intervals[i] = {thetas.first, M_PI};\n } else {\n intervals[i] = {thetas.first, thetas.second};\n }\n }\n\n // 他の要素に包含される要素を削除\n REP(i, intervals.size()) REP(j, intervals.size()){\n if (i == j) continue;\n\n if (intervals[j].first <= intervals[i].first && intervals[i].second <= intervals[j].second) {\n intervals.erase(intervals.begin() + i);\n i--;\n break;\n }\n }\n\n intervals.insert(intervals.begin(), {0, 0});\n sort(ALL(intervals));\n\n vector<vector<double>> dp;\n dp.assign(N+1, vector<double>(K+1, 0));\n\n int n = intervals.size()-1;\n REPN(i, 1, n+1){\n //\n int l = i;\n while (l > 0){\n l--;\n if (intervals[l].second < intervals[i].first){\n break;\n }\n }\n REPN(j, 1, K+1){\n dp[i][j] = dp[i-1][j];\n dp[i][j] = max(dp[i][j], dp[l][j-1] + intervals[i].second - intervals[i].first);\n dp[i][j] = max(dp[i][j], dp[l+1][j-1] + intervals[i].second - intervals[l+1].second);\n }\n }\n\n print(dp[n][K] / M_PI);\n\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 20988, "score_of_the_acc": -0.0035, "final_rank": 2 }, { "submission_id": "aoj_2276_3828008", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\ntypedef pair<double,double> PA;\n\nint n,k;\nvector<PA> p;\ndouble memo[1510][1510];\n\ndouble dp(int a,int b){\n \n if(b >= k) return 0;\n if(memo[a][b] != -1.0) return memo[a][b];\n \n double res,add;\n int i,num;\n res = 0;\n add = 0;\n num = a+1;\n for(i=a+1;i<p.size()&&p[i].first<p[a].second;i++){\n if(add < p[i].second-p[a].second){\n num = i;\n add = p[i].second-p[a].second;\n }\n }\n if(i != a+1) res = max(res,dp(num,b+1)+add);\n for(int j=i;j<p.size();j++){\n add = p[j].second-p[j].first;\n res = max(res,dp(j,b+1)+add);\n }\n \n return memo[a][b] = res;\n}\n\nint main()\n{\n \n double x,y,r,as,at,ll,rr;\n vector<PA> vec;\n \n scanf(\"%d %d\",&n,&k);\n for(int i=0;i<n;i++){\n scanf(\"%lf %lf %lf\",&x,&y,&r);\n if(y+r < 0) continue;\n as = asin(r/sqrt(x*x+y*y));\n at = atan2(y,x);\n ll = at-as;\n rr = at+as;\n if(ll < -M_PI){\n ll += 2*M_PI;\n rr += 2*M_PI;\n }\n vec.push_back( PA(max(0.0,ll),min(M_PI,rr)) );\n }\n \n sort(vec.begin(),vec.end());\n \n double ma = 0.0;\n p.push_back(PA(0.0,0.0));\n for(int i=0;i<vec.size();i++){\n if(ma == M_PI) break;\n if(ma < vec[i].second){\n if(i < vec.size()-1 && vec[i].first == vec[i+1].first) continue;\n ma = vec[i].second;\n p.push_back(vec[i]);\n }\n }\n fill_n(*memo,1510*1510,-1.0);\n printf(\"%.13f\\n\",dp(0,0)/M_PI);\n \n return(0);\n}", "accuracy": 1, "time_ms": 540, "memory_kb": 21616, "score_of_the_acc": -0.7666, "final_rank": 14 }, { "submission_id": "aoj_2276_1268316", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define rep(i,x,y) for(int i=(x);i<(y);++i)\n#define mp(a,b) make_pair((a),(b))\n#define debug(x) #x << \"=\" << (x)\n \n#ifdef DEBUG\n#define _GLIBCXX_DEBUG\n#define dump(x) std::cerr << debug(x) << \" (L:\" << __LINE__ << \")\" << std::endl\n#else\n#define dump(x)\n#endif\n\ntypedef long long int ll;\ntypedef pair<int,int> pii;\n//template<typename T> using vec=std::vector<T>;\n\nconst int INF=1<<30;\nconst long long int INF_=1LL<<58;\nconst double EPS=1e-9;\nconst int dx[]={1,0,-1,0},dy[]={0,1,0,-1};\n\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec){\n\tos << \"[\";\n\tfor (const auto &v : vec) {\n\t\tos << v << \",\";\n\t}\n\tos << \"]\";\n\treturn os;\n}\n\nvoid Solve(){\n\tint n,K;\n\tscanf(\"%d%d\",&n,&K);\n\n\tvector<pair<double,double>> ps;\n\tconst double PI=3.14159265359;\n\trep(i,0,n){\n\t\tdouble x,y,r;\n\t\tscanf(\"%lf%lf%lf\",&x,&y,&r);\n\t\tif(y+r<=0) continue;\n\t\tif(y-r<=0){\n\t\t\tdouble f,s;\n\t\t\tif(x<0){\n\t\t\t\tf=atan2(y,x)-asin(r/sqrt(x*x+y*y));\n\t\t\t\ts=PI;\n\t\t\t}else{\n\t\t\t\tf=0;\n\t\t\t\ts=atan2(y,x)+asin(r/sqrt(x*x+y*y));\n\t\t\t}\n\t\t\twhile(f<0) f+=PI;\n\t\t\twhile(s<0) s+=PI;\n\t\t\tps.push_back(mp(f,s));\n\t\t}else ps.push_back(mp(atan2(y,x)-asin(r/sqrt(x*x+y*y)),atan2(y,x)+asin(r/sqrt(x*x+y*y))));\n\t}\n\t\n\tsort(ps.begin(),ps.end());\n\tps.erase(unique(ps.begin(),ps.end()),ps.end()); //同じ区間があると次の処理で面倒なので消しておく\n\n\t//他の区間に完全に包含されているものを取り除く\n\tvector<pair<double,double>> tmp;\n\trep(i,0,ps.size()){\n\t\tbool ok=true;\n\t\trep(j,0,ps.size()){\n\t\t\tif(i==j) continue;\n\t\t\tif(ps[j].first<=ps[i].first&&ps[i].second<=ps[j].second){\n\t\t\t\tok=false;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tif(ok) tmp.push_back(ps[i]);\n\t}\n\tps=tmp;\n\n\tif(ps.size()==0){\n\t\tcout << 0 << endl;\n\t\treturn;\n\t}\n\n\tvector<int> a(ps.size()); //a[i]=i番目の区間とギリギリ重なってる区間。存在しない場合はもっとも近いやつ\n\trep(i,1,ps.size()){\n\t\trep(j,0,i+1){\n\t\t\tif(ps[j].second>=ps[i].first){\n\t\t\t\ta[i]=j;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tif(a[i]==i) a[i]=i-1;\n\t}\n\n\tstatic double dp[1500][1501][2]; //dp[i][j][k]=i番目の区間まででj個取って、i番目の区間を使った(k==1)or使わなかった(k==0)ときの最大\n\tdp[0][0][0]=0;\n\tdp[0][1][1]=ps[0].second-ps[0].first;\n\tdouble ans=dp[0][1][1];\n\trep(i,1,ps.size()){\n\t\trep(j,0,K+1){\n\t\t\tdp[i][j][0]=max(dp[i-1][j][0],dp[i-1][j][1]);\n\t\t\tif(j==0) continue;\n\t\t\t//a[i]番目の区間を使った場合は重複している部分を取り除く、使わなかった場合は重なっていないのでそのまま足す。\n\t\t\tdp[i][j][1]=max(dp[a[i]][j-1][1]+ps[i].second-max(ps[i].first,ps[a[i]].second),dp[a[i]][j-1][0]+ps[i].second-ps[i].first);\n\t\t\tans=max({ans,dp[i][j][1],dp[i][j][0]});\n\t\t}\n\t}\n\n\tprintf(\"%.10lf\\n\",ans/PI);\n}\n\nint main(){\n\tSolve();\n\treturn 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 36584, "score_of_the_acc": -0.1643, "final_rank": 5 }, { "submission_id": "aoj_2276_1268307", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define rep(i,x,y) for(int i=(x);i<(y);++i)\n#define mp(a,b) make_pair((a),(b))\n#define debug(x) #x << \"=\" << (x)\n \n#ifdef DEBUG\n#define _GLIBCXX_DEBUG\n#define dump(x) std::cerr << debug(x) << \" (L:\" << __LINE__ << \")\" << std::endl\n#else\n#define dump(x)\n#endif\n\ntypedef long long int ll;\ntypedef pair<int,int> pii;\n//template<typename T> using vec=std::vector<T>;\n\nconst int INF=1<<30;\nconst long long int INF_=1LL<<58;\nconst double EPS=1e-9;\nconst int dx[]={1,0,-1,0},dy[]={0,1,0,-1};\n\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec){\n\tos << \"[\";\n\tfor (const auto &v : vec) {\n\t\tos << v << \",\";\n\t}\n\tos << \"]\";\n\treturn os;\n}\n\nvoid Solve(){\n\tint n,K;\n\tscanf(\"%d%d\",&n,&K);\n\n\tvector<pair<double,double>> ps;\n\tconst double PI=3.14159265359;\n\trep(i,0,n){\n\t\tdouble x,y,r;\n\t\tscanf(\"%lf%lf%lf\",&x,&y,&r);\n\t\tif(y+r<=0) continue;\n\t\tif(y-r<=0){\n\t\t\tdouble f,s;\n\t\t\tif(x<0){\n\t\t\t\tf=atan2(y,x)-asin(r/sqrt(x*x+y*y));\n\t\t\t\ts=PI;\n\t\t\t}else{\n\t\t\t\tf=0;\n\t\t\t\ts=atan2(y,x)+asin(r/sqrt(x*x+y*y));\n\t\t\t}\n\t\t\twhile(f<0) f+=PI;\n\t\t\twhile(s<0) s+=PI;\n\t\t\tps.push_back(mp(f,s));\n\t\t}else ps.push_back(mp(atan2(y,x)-asin(r/sqrt(x*x+y*y)),atan2(y,x)+asin(r/sqrt(x*x+y*y))));\n\t}\n\t\n\tsort(ps.begin(),ps.end());\n\tps.erase(unique(ps.begin(),ps.end()),ps.end());\n\t\n\tvector<pair<double,double>> tmp;\n\trep(i,0,ps.size()){\n\t\tbool ok=true;\n\t\trep(j,0,ps.size()){\n\t\t\tif(i==j) continue;\n\t\t\tif(ps[j].first<=ps[i].first&&ps[i].second<=ps[j].second){\n\t\t\t\tok=false;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tif(ok) tmp.push_back(ps[i]);\n\t}\n\tps=tmp;\n\n\tif(ps.size()==0){\n\t\tcout << 0 << endl;\n\t\treturn;\n\t}\n\n\tvector<int> a(ps.size());\n\trep(i,1,ps.size()){\n\t\trep(j,0,i+1){\n\t\t\tif(ps[j].second>=ps[i].first){\n\t\t\t\ta[i]=j;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tif(a[i]==i) a[i]=i-1;\n\t}\n\n\tstatic double dp[1500][1501][2];\n\tdp[0][0][0]=0;\n\tdp[0][1][1]=ps[0].second-ps[0].first;\n\tdouble ans=dp[0][1][1];\n\trep(i,1,ps.size()){\n\t\trep(j,0,K+1){\n\t\t\tdp[i][j][0]=max(dp[i-1][j][0],dp[i-1][j][1]);\n\t\t\tif(j==0) continue;\n\t\t\tdp[i][j][1]=max(dp[a[i]][j-1][1]+ps[i].second-max(ps[i].first,ps[a[i]].second),dp[a[i]][j-1][0]+ps[i].second-ps[i].first);\n\t\t\tans=max({ans,dp[i][j][1],dp[i][j][0]});\n\t\t}\n\t}\n\n\tprintf(\"%.10lf\\n\",ans/PI);\n}\n\nint main(){\n\tSolve();\n\treturn 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 36584, "score_of_the_acc": -0.1643, "final_rank": 5 }, { "submission_id": "aoj_2276_1268305", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define rep(i,x,y) for(int i=(x);i<(y);++i)\n#define mp(a,b) make_pair((a),(b))\n#define debug(x) #x << \"=\" << (x)\n \n#ifdef DEBUG\n#define _GLIBCXX_DEBUG\n#define dump(x) std::cerr << debug(x) << \" (L:\" << __LINE__ << \")\" << std::endl\n#else\n#define dump(x)\n#endif\n\ntypedef long long int ll;\ntypedef pair<int,int> pii;\n//template<typename T> using vec=std::vector<T>;\n\nconst int INF=1<<30;\nconst long long int INF_=1LL<<58;\nconst double EPS=1e-9;\nconst int dx[]={1,0,-1,0},dy[]={0,1,0,-1};\n\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec){\n\tos << \"[\";\n\tfor (const auto &v : vec) {\n\t\tos << v << \",\";\n\t}\n\tos << \"]\";\n\treturn os;\n}\n\nvoid Solve(){\n\tint n,K;\n\tscanf(\"%d%d\",&n,&K);\n\n\tvector<pair<double,double>> ps;\n\tconst double PI=3.14159265359;\n\trep(i,0,n){\n\t\tdouble x,y,r;\n\t\tscanf(\"%lf%lf%lf\",&x,&y,&r);\n\t\tif(y+r<=0) continue;\n\t\tif(y-r<=0){\n\t\t\tdouble f,s;\n\t\t\tif(x<0){\n\t\t\t\tf=atan2(y,x)-asin(r/sqrt(x*x+y*y));\n\t\t\t\ts=PI;\n\t\t\t}else{\n\t\t\t\tf=0;\n\t\t\t\ts=atan2(y,x)+asin(r/sqrt(x*x+y*y));\n\t\t\t}\n\t\t\twhile(f<0) f+=PI;\n\t\t\twhile(s<0) s+=PI;\n\t\t\tps.push_back(mp(f,s));\n\t\t}else ps.push_back(mp(atan2(y,x)-asin(r/sqrt(x*x+y*y)),atan2(y,x)+asin(r/sqrt(x*x+y*y))));\n\t}\n\t\n\tsort(ps.begin(),ps.end());\n\tps.erase(unique(ps.begin(),ps.end()),ps.end());\n\t\n\tvector<pair<double,double>> tmp;\n\trep(i,0,ps.size()){\n\t\tbool ok=true;\n\t\trep(j,0,ps.size()){\n\t\t\tif(i==j) continue;\n\t\t\tif(ps[j].first<=ps[i].first&&ps[i].second<=ps[j].second){\n\t\t\t\tok=false;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tif(ok) tmp.push_back(ps[i]);\n\t}\n\tps=tmp;\n\n\tif(ps.size()==0){\n\t\tcout << 0 << endl;\n\t\treturn;\n\t}\n\n\tvector<int> a(ps.size());\n\trep(i,1,ps.size()){\n\t\trep(j,0,i+1){\n\t\t\tif(ps[j].second>=ps[i].first){\n\t\t\t\ta[i]=j;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tif(a[i]==i) a[i]=i-1;\n\t}\n\n\tstatic double dp[1500][1501][2];\n\tdp[0][0][0]=0;\n\tdp[0][1][1]=ps[0].second-ps[0].first;\n\tdouble ans=dp[0][1][1];\n\trep(i,1,ps.size()){\n\t\trep(j,0,K+1){\n\t\t\tdp[i][j][0]=max(dp[i-1][j][0],dp[i-1][j][1]);\n\t\t\tif(j==0) continue;\n\t\t\tif(j==1) dp[i][j][1]=ps[i].second-ps[i].first;\n\t\t\tdp[i][j][1]=max(dp[a[i]][j-1][1]+ps[i].second-max(ps[i].first,ps[a[i]].second),dp[a[i]][j-1][0]+ps[i].second-ps[i].first);\n\t\t\tans=max({ans,dp[i][j][1],dp[i][j][0]});\n\t\t}\n\t}\n\n\tprintf(\"%.10lf\\n\",ans/PI);\n}\n\nint main(){\n\tSolve();\n\treturn 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 36584, "score_of_the_acc": -0.1643, "final_rank": 5 }, { "submission_id": "aoj_2276_1170001", "code_snippet": "#include <iostream>\n#include <sstream>\n#include <cstdio>\n#include <cstdlib>\n#include <cmath>\n#include <ctime>\n#include <cstring>\n#include <string>\n#include <vector>\n#include <stack>\n#include <queue>\n#include <deque>\n#include <map>\n#include <set>\n#include <bitset>\n#include <numeric>\n#include <utility>\n#include <iomanip>\n#include <algorithm>\n#include <functional>\nusing namespace std;\n\ntypedef long long ll;\ntypedef vector<int> vint;\ntypedef vector<long long> vll;\ntypedef pair<int,int> pint;\ntypedef pair<long long, long long> pll;\n\n#define MP make_pair\n#define PB push_back\n#define ALL(s) (s).begin(),(s).end()\n#define EACH(i, s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)\n#define COUT(x) cout << #x << \" = \" << (x) << \" (L\" << __LINE__ << \")\" << endl\n\ntemplate<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }\ntemplate<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }\ntemplate<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P) \n{ return s << '<' << P.first << \", \" << P.second << '>'; }\ntemplate<class T> ostream& operator << (ostream &s, vector<T> P) \n{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << \" \"; } s << P[i]; } return s; }\ntemplate<class T> ostream& operator << (ostream &s, vector<vector<T> > P) \n{ for (int i = 0; i < P.size(); ++i) { s << endl << P[i]; } return s << endl; }\ntemplate<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P) \n{ EACH(it, P) { s << \"<\" << it->first << \"->\" << it->second << \"> \"; } return s; }\n\n\n\ntypedef double DD;\n\nconst DD INF = 1LL<<60;\nconst DD EPS = 1e-10;\nconst DD PI = acos(-1.0);\nDD torad(int deg) {return (DD)(deg) * PI / 180;}\nDD todeg(DD ang) {return ang * 180 / PI;}\n\nstruct Point {\n DD x, y;\n Point(DD x = 0.0, DD y = 0.0) : x(x), y(y) {}\n friend ostream& operator << (ostream &s, const Point &p) {return s << '(' << p.x << \", \" << p.y << ')';}\n};\n\nPoint operator + (const Point &p, const Point &q) {return Point(p.x + q.x, p.y + q.y);}\nPoint operator - (const Point &p, const Point &q) {return Point(p.x - q.x, p.y - q.y);}\nPoint operator * (const Point &p, DD a) {return Point(p.x * a, p.y * a);}\nPoint operator * (DD a, const Point &p) {return Point(a * p.x, a * p.y);}\nPoint operator * (const Point &p, const Point &q) {return Point(p.x * q.x - p.y * q.y, p.x * q.y + p.y * q.x);}\nPoint operator / (const Point &p, DD a) {return Point(p.x / a, p.y / a);}\nPoint conj(const Point &p) {return Point(p.x, -p.y);}\nPoint rot(const Point &p, DD ang) {return Point(cos(ang) * p.x - sin(ang) * p.y, sin(ang) * p.x + cos(ang) * p.y);}\nPoint rot90(const Point &p) {return Point(-p.y, p.x);}\nDD cross(const Point &p, const Point &q) {return p.x * q.y - p.y * q.x;}\nDD dot(const Point &p, const Point &q) {return p.x * q.x + p.y * q.y;}\nDD norm(const Point &p) {return dot(p, p);}\nDD abs(const Point &p) {return sqrt(dot(p, p));}\nDD amp(const Point &p) {DD res = atan2(p.y, p.x); if (res < 0) res += PI*2; return res;}\nbool eq(const Point &p, const Point &q) {return abs(p - q) < EPS;}\nbool operator < (const Point &p, const Point &q) {return (abs(p.x - q.x) > EPS ? p.x < q.x : p.y < q.y);}\nbool operator > (const Point &p, const Point &q) {return (abs(p.x - q.x) > EPS ? p.x > q.x : p.y > q.y);}\nPoint operator / (const Point &p, const Point &q) {return p * conj(q) / norm(q);}\n\nint ccw(const Point &a, const Point &b, const Point &c) {\n if (cross(b-a, c-a) > EPS) return 1;\n if (cross(b-a, c-a) < -EPS) return -1;\n if (dot(b-a, c-a) < -EPS) return 2;\n if (norm(b-a) < norm(c-a) - EPS) return -2;\n return 0;\n}\n\nstruct Line : vector<Point> {\n Line(Point a = Point(0.0, 0.0), Point b = Point(0.0, 0.0)) {\n this->push_back(a);\n this->push_back(b);\n }\n friend ostream& operator << (ostream &s, const Line &l) {return s << '{' << l[0] << \", \" << l[1] << '}';}\n};\n\nstruct Circle : Point {\n DD r;\n Circle(Point p = Point(0.0, 0.0), DD r = 0.0) : Point(p), r(r) {}\n friend ostream& operator << (ostream &s, const Circle &c) {return s << '(' << c.x << \", \" << c.y << \", \" << c.r << ')';}\n};\n\n\n\nvector<Point> tanline(Point p, Circle c) {\n vector<Point> res;\n DD d = norm(p - c);\n DD l = d - c.r * c.r;\n if (l < -EPS) return res;\n if (l <= 0.0) l = 0.0;\n Point cq = (p - c) * (c.r * c.r / d);\n Point qs = rot90((p - c) * (c.r * sqrt(l) / d));\n Point s1 = c + cq + qs, s2 = c + cq - qs;\n res.push_back(s1);\n res.push_back(s2);\n return res;\n}\n\n\n\nint N, K;\nCircle cir[3100];\nDD dp[2100][2100];\n\nDD solve() {\n vector< pair<DD,DD> > inter;\n for (int i = 0; i < N; ++i) {\n if (abs(cir[i]) <= cir[i].r + EPS) return 1.0;\n \n vector<Point> ps = tanline(Point(0,0), cir[i]);\n DD left = amp(ps[0]), right = amp(ps[1]);\n if (left > right) left = 0;\n chmax(left, 0.0);\n chmin(right, PI);\n \n if (left < right) inter.PB(MP(right, left));\n //cout << i << \" : \" << cir[i] << \", \" << amp(ps[0]) << \", \" << amp(ps[1]) << \" ; \" << MP(left, right) << endl;\n }\n \n sort(ALL(inter));\n for (int i = 0; i < inter.size(); ++i) swap(inter[i].first, inter[i].second);\n for (int i = 0; i < inter.size(); ++i) {\n for (int j = i+1; j < inter.size(); ++j) {\n if (inter[j].first < inter[i].first) {\n inter.erase(inter.begin() + i--);\n break;\n }\n }\n }\n \n //COUT(inter);\n \n for (int i = 0; i < 2100; ++i) for (int j = 0; j < 2100; ++j) dp[i][j] = 0.0;\n DD res = 0.0;\n for (int i = 0; i < inter.size(); ++i) {\n int tmp = i;\n while (true) {\n if (inter[tmp].second <= inter[i].first) break;\n --tmp;\n if (tmp == -1) break;\n }\n for (int k = 0; k <= K; ++k) {\n chmax(dp[i+1][k], dp[i][k]);\n chmax(dp[i+1][k+1], dp[tmp+1][k] + inter[i].second - inter[i].first);\n chmax(dp[i+1][k+1], dp[tmp+2][k] + inter[i].second - inter[tmp+1].second);\n \n chmax(res, dp[i+1][k]);\n }\n }\n \n return res / PI;\n}\n\nint main() {\n //freopen( \"/Users/macuser/Dropbox/Contest/input.in\", \"r\", stdin );\n \n while (cin >> N >> K) {\n for (int i = 0; i < N; ++i) cin >> cir[i].x >> cir[i].y >>cir[i].r;\n cout << fixed << setprecision(9) << solve() << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 35960, "score_of_the_acc": -0.1585, "final_rank": 4 }, { "submission_id": "aoj_2276_1007644", "code_snippet": "#include<stdio.h>\n#include<algorithm>\n#include<math.h>\nusing namespace std;\ndouble PI=3.14159265359;\nint x[2000];\nint y[2000];\nint r[2000];\npair<double,double> p[2000];\npair<double,double> c[2000];\ndouble dp[2000][2000];\nint main(){\n\tint a,b;\n\tscanf(\"%d%d\",&a,&b);\n\tfor(int i=0;i<a;i++){\n\t\tscanf(\"%d%d%d\",x+i,y+i,r+i);\n\t\tdouble d=sqrt(x[i]*x[i]+y[i]*y[i]);\n\t\tdouble q=atan2(y[i],x[i]);\n\t\tif(q<-PI/2)q+=PI*2;\n\t\tdouble v=asin((double)r[i]/d);\n\t\t//printf(\"%f %f\\n\",(q-v)/PI*180,(q+v)/PI*180);\n\t\tp[i]=make_pair(max(0.0,min(PI,q-v)),min(PI,max(0.0,q+v)));\n\t}\n\tstd::sort(p,p+a);\n\tint sz=0;\n\tfor(int i=0;i<a;i++){\n\t\tbool ok=true;\n\t\tfor(int j=0;j<a;j++){\n\t\t\tif(i!=j&&p[j].first<p[i].first&&p[i].second<p[j].second)ok=false;\n\t\t}\n\t\tif(ok){\n\t\t\tc[sz++]=p[i];\n\t\t}\n\t}\n\tdouble ret=0;\n\tfor(int i=1;i<=b;i++){\n\t\tdouble tmp=0;\n\t\tint at=0;\n\t\tfor(int j=0;j<sz;j++){\n\t\t\twhile(c[at].second<c[j].first){tmp=max(tmp,dp[i-1][at]);at++;}\n\t\t\tdp[i][j]=max(tmp+c[j].second-c[j].first,dp[i-1][at]+c[j].second-c[at].second);\n\t\t\tret=max(ret,dp[i][j]);\n\t\t}\n\t}printf(\"%.12f\\n\",ret/PI);\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 25808, "score_of_the_acc": -0.0488, "final_rank": 3 } ]
aoj_2268_cpp
問題 J : 乱択平衡分二分探索木 ところで,G○○gle Code Jam の地区大会で右の席に座っていた男の ID は rng_58 と言うらしい. 東京大学時代の記憶に,似たような ID の仲間が居た覚えがあるが,僕の仲間は一人残さず美少女だったはずだ. 彼女はいわゆる無表情不思議系キャラだったような気がする. ということは,彼は知らない男だ.rng とは何の略だろう. おそらく Random Number Generator に違いない. 乱択アルゴリズムを得意とするのだろう. ところで,乱数を用いた平衡二分探索木である Treap は, 2011 年頃のプログラミングコンテストではしばしば用いられていたようだが, 20XX 年の今では,あまり使用されていない. 当時 Treap は実装が平易であるという理由により Splay 木や Scapegoat 木と並んでよく使われていた. 20XX 年の今,命をかけたプログラミングコンテストが日常的に行われるこの世界では, 実装が多少平易になるというような甘い根拠での選択は到底考えられない. 皆,少しでもプログラムが高速になるよう考えているし, 世界大会出場者ともなれば,Left-leaning 赤黒木程度なら十数秒で記述できるのが当然だ. 例えば,Treap が赤黒木よりも遅くなってしまうのは, 高さの定数倍が効いてくるからであると言われている. ここは,落ち着いてそれを数値的に考察することにより,精神の安定を取り戻そう. 問題 キーを実数とする Treap に N 個のランダムな要素を挿入した際に, 高さが h ( h = 0, 1, …, N - 1 ) となる確率を求めよ. 以下,より詳しく説明する. Treap とは乱数を用いた平衡二分探索木である. 各ノードは,キーの他に,優先度という値を持つ. ここでは,キーと優先度は 0 から 1 までの実数値とする. Treap では,以下の 2 つの条件が常に保たれる. キーに関する条件 各ノードに関して,左の子以下のノードは自分より小さなキーを持つ 各ノードに関して,右の子以下のノードは自分より大きなキーを持つ 優先度に関する条件 各ノードに関して,子のノードは自分より小さな優先度を持つ 下図は,Treap の例である. 各ノードの,上部にキーが,下部に優先度が書かれている. 7 つのノードからなる Treap の例. 挿入の操作は,以下のように行われる. 挿入するキー を x とおく. 挿入する新しいノードの優先度 p を 0 から 1 までの一様分布よりランダムに決定する. 通常の二分探索木と同様に,まずは優先度を無視し新しいノードを挿入する. 優先度の条件を満たすように,新しいノードを上に持ち上げるような回転操作を必要なだけ繰り返す. 木の回転操作に関しては,補足の節に詳しく記述したので,参考にせよ. 下図は,先ほどの例にキーとして 0.5,優先度として 0.7 を持つ頂点を挿入した過程の例である. まずは優先度が無視され,ノードが挿入される. 回転を行い新しいノードが上へ行く. さらに回転を行うと,優先度の条件が満たされるので,挿入は終了する. 空の Treap に N 個のキーを, やはり 0 から 1 までの一様分布よりランダムに選び挿入してゆくとする. 最終的に高さが h となる確率を, 各 h = 0, 1, …, N - 1 について求めよ. なお,実数は十分な精度を持って扱われるとし, 例えば 2 つのノードの優先度が等しくなる確率は 0 としてよい. また,高さとは,根のノードから各ノードに至るために通らなければならない辺の本数の最大値である. 入力 1 つの整数 N が書かれている. 出力 出力は N 行から成る. i 行目に高さが i - 1 となる確率を出力せよ. 値は小数点以下何桁表示しても構わない. 制約 1 ≤ N ≤ 3 × 10 4 出力する値は 10 - 5 以下の誤差を含んでいても構わない. 入出力例 入力例 1: 1 入力例 1 に対する出力例: 1.0 入力例 2: 2 入力例 2 に対する出力例: 0.0000000000 1.0000000000 補足 木の回転操作には,右回転と左回転がある. 右回転とは,以下の 2 つの図の 1 つめの状態を 2 つめの状態にする操作であり, 左回転とは,以下の 2 つの図の 2 つめの状態を 1 つめの状態にする操作である. 右回転をする前,または左回転をした後 右回転をした後,または左回転をする前 Treap においては,持ち上げたいノードがその親ノードの左の子であった場合, 持ち上げたいノードを上図の P とするような右回転を適用し, 持ち上げたいノードがその親ノードの右の子であった場合, 持ち上げたいノードを上図の Q とするような左回転を適用する. 木の回転操作について,より詳しくは,例えば以下を参照せよ. http://ja.wikipedia.org/wiki/%E6%9C%A8%E3%81%AE%E5%9B%9E%E8%BB%A2 Treap について,より詳しくは,例えば以下を参照せよ. http://en.wikipedia.org/wiki/Treap http://www.prefield.com/algorithm/container/treap.html
[ { "submission_id": "aoj_2268_10307688", "code_snippet": "// AOJ #2268 Randomized Self-Balancing Binary Search Tree\n// 2025.3.17\n\n#include <bits/stdc++.h>\nusing namespace std;\n\ntemplate <typename R>\nvoid fft(vector<complex<R>> &f, int dir) {\n int n = f.size();\n assert(n == (1 << static_cast<int>(log2(n))));\n R theta = dir * 2 * M_PI / n;\n\n for (int m = n; m >= 2; m /= 2) {\n for (int i = 0; i < m / 2; i++) {\n complex<R> w = polar<R>(1, i * theta);\n for (int j = i; j < n; j += m) {\n int k = j + m / 2;\n complex<R> diff = f[j] - f[k];\n f[j] += f[k];\n f[k] = w * diff;\n }\n }\n theta *= 2;\n }\n\n int i = 0;\n for (int j = 1; j < n - 1; j++) {\n for (int k = n / 2; k > (i ^= k); k /= 2);\n if (j < i) swap(f[i], f[j]);\n }\n}\n\ntemplate <typename T, typename R = double>\nvoid conv(vector<T> &a, int result_n) {\n int m = 2 * a.size() - 1;\n int n = pow(2, ceil(log2(m)));\n vector<complex<R>> x(n);\n\n copy(a.begin(), a.end(), x.begin());\n fft(x, +1);\n for (int i = 0; i < n; i++) x[i] *= x[i];\n fft(x, -1);\n\n a.resize(result_n);\n for (int i = 0; i < result_n; i++) a[i] = x[i].real() / n;\n}\n\nint main() {\n int n;\n cin >> n;\n\n int h = min(n, 50);\n vector<double> dp(n + 1, 0);\n dp[0] = 1;\n double last = 0;\n\n for (int j = 1; j <= h; j++) {\n conv(dp, n + 1);\n for (int i = n - 1; i >= 0; i--) {\n dp[i + 1] = dp[i] / (i + 1);\n }\n dp[0] = 1;\n printf(\"%.10lf\\n\", dp[n] - last);\n last = dp[n];\n }\n for (int j = h + 1; j <= n; j++) puts(\"0\");\n return 0;\n}", "accuracy": 1, "time_ms": 470, "memory_kb": 4984, "score_of_the_acc": -0.097, "final_rank": 1 }, { "submission_id": "aoj_2268_10274921", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nconst double PI = acos(-1.0);\n\n// FFT(Cooley-Tukeyアルゴリズム)\nvoid fft(vector<complex<double>> &a, bool invert) {\n int n = (int)a.size();\n for (int i = 1, j = 0; i < n; i++) {\n int bit = n >> 1;\n for (; j & bit; bit >>= 1)\n j -= bit;\n j += bit;\n if (i < j) swap(a[i], a[j]);\n }\n\n for (int len = 2; len <= n; len <<= 1) {\n double ang = 2 * PI / len * (invert ? -1 : 1);\n complex<double> wlen(cos(ang), sin(ang));\n for (int i = 0; i < n; i += len) {\n complex<double> w(1);\n for (int j = 0; j < len / 2; j++) {\n complex<double> u = a[i + j], v = a[i + j + len / 2] * w;\n a[i + j] = u + v;\n a[i + j + len / 2] = u - v;\n w *= wlen;\n }\n }\n }\n\n if (invert) {\n for (int i = 0; i < n; i++)\n a[i] /= n;\n }\n}\n\n// 畳み込み(convolution): O(N log N)\n// a, b は実数ベクトル。戻り値は a∗b の畳み込みの実部のみを返す。\nvector<double> convolution(const vector<double> &a, const vector<double> &b) {\n int n_fft = 1;\n int result_size = a.size() + b.size() - 1;\n while (n_fft < result_size)\n n_fft <<= 1;\n vector<complex<double>> fa(n_fft), fb(n_fft);\n for (int i = 0; i < (int)a.size(); i++)\n fa[i] = a[i];\n for (int i = a.size(); i < n_fft; i++)\n fa[i] = 0;\n for (int i = 0; i < (int)b.size(); i++)\n fb[i] = b[i];\n for (int i = b.size(); i < n_fft; i++)\n fb[i] = 0;\n\n fft(fa, false);\n fft(fb, false);\n for (int i = 0; i < n_fft; i++)\n fa[i] *= fb[i];\n fft(fa, true);\n\n vector<double> res(result_size);\n for (int i = 0; i < result_size; i++)\n res[i] = fa[i].real();\n return res;\n}\n\n// main:dp層hについての更新を FFT畳み込みで O(n log n) で行う\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n\n int n;\n cin >> n;\n\n // dp_prev[x] = dp[x][h-1]、dp_cur[x] = dp[x][h] (0 <= x <= n)\n // 初期条件:h=0 では dp[0][0]=1, dp[x>0][0]=0\n vector<double> dp_prev(n + 1, 0.0), dp_cur(n + 1, 0.0);\n dp_prev[0] = 1.0;\n for (int i = 1; i <= n; i++)\n dp_prev[i] = 0.0;\n\n // 各高さ h = 1,2,...,n について dp[n][h] - dp[n][h-1] を出力\n for (int h = 1; h <= n; h++) {\n if (h >= 50) {\n printf(\"0.0000000000\\n\");\n continue;\n }\n // dp[n][h] = (1/n) * (畳み込み結果の index n-1)\n // つまり、dp[.][h] は dp[.][h-1] 同士の畳み込み結果を利用して求められる。\n vector<double> convRes = convolution(dp_prev, dp_prev);\n\n dp_cur[0] = 1.0; // dp[0][h] = 1\n for (int m = 1; m <= n; m++) {\n // convRes[m-1] = ∑_{i+j = m-1} dp[i][h-1]*dp[j][h-1]\n dp_cur[m] = convRes[m - 1] / m;\n }\n\n // 高さが正確に h となる確率は dp[n][h] - dp[n][h-1]\n double ans = dp_cur[n] - dp_prev[n];\n printf(\"%.10f\\n\", ans);\n\n dp_prev = dp_cur;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 480, "memory_kb": 6676, "score_of_the_acc": -0.1267, "final_rank": 2 }, { "submission_id": "aoj_2268_6975824", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nlong double PI = acos(-1);\nvector<complex<long double>> fft(vector<complex<long double>> A, bool inv){\n\tint N = A.size();\n\tcomplex<long double> r = polar((long double) 1, (2 * PI) / (N));\n\tif (inv){\n\t\tr = pow(r, -1);\n\t}\n\tvector<complex<long double>> B(N);\n\tfor (int i = N / 2; i > 0; i /= 2){\n\t\tcomplex<long double> z = pow(r, i);\n\t\tcomplex<long double> z2 = 1;\n\t\tfor (int j = 0; j < N; j += i * 2){\n\t\t\tfor (int k = 0; k < i; k++){\n\t\t\t\tA[i + j + k] *= z2;\n\t\t\t\tB[j / 2 + k] = A[j + k] + A[i + j + k];\n\t\t\t\tB[N / 2 + j / 2 + k] = A[j + k] - A[i + j + k];\n\t\t\t}\n\t\t\tz2 *= z;\n\t\t}\n\t\tswap(A, B);\n\t}\n\tif (inv){\n\t\tfor (int i = 0; i < N; i++){\n\t\t\tA[i] /= N;\n\t\t}\n\t}\n\treturn A;\n}\nvector<long double> convolution(vector<long double> A, vector<long double> B){\n\tint deg = A.size() + B.size() - 1;\n\tint N = 1;\n\twhile (N < deg){\n\t\tN *= 2;\n\t}\n\tvector<complex<long double>> A2(N, 0);\n\tfor (int i = 0; i < A.size(); i++){\n\t\tA2[i] = A[i];\n\t}\n\tvector<complex<long double>> B2(N, 0);\n\tfor (int i = 0; i < B.size(); i++){\n\t\tB2[i] = B[i];\n\t}\n\tA2 = fft(A2, false);\n\tB2 = fft(B2, false);\n\tvector<complex<long double>> C2(N);\n\tfor (int i = 0; i < N; i++){\n\t\tC2[i] = A2[i] * B2[i];\n\t}\n\tC2 = fft(C2, true);\n\tvector<long double> C(deg);\n\tfor (int i = 0; i < deg; i++){\n\t C[i] = C2[i].real();\n\t}\n\treturn C;\n}\nint main(){\n cout << fixed << setprecision(20);\n int N;\n cin >> N;\n int M = min(N, 100);\n vector<vector<long double>> dp(M + 1, vector<long double>(N + 1, 0));\n dp[0][0] = 1;\n for (int i = 0; i < M; i++){\n vector<long double> tmp = convolution(dp[i], dp[i]);\n dp[i + 1][0] = 1;\n for (int j = 1; j <= N; j++){\n dp[i + 1][j] = tmp[j - 1] / j;\n }\n }\n for (int i = 1; i <= M; i++){\n cout << max(dp[i][N] - dp[i - 1][N], (long double) 0) << endl;\n }\n for (int i = M; i < N; i++){\n cout << 0 << endl;\n }\n}", "accuracy": 1, "time_ms": 3380, "memory_kb": 61956, "score_of_the_acc": -1.7062, "final_rank": 13 }, { "submission_id": "aoj_2268_6975823", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ndouble PI = acos(-1);\nvector<complex<double>> fft(vector<complex<double>> A, bool inv){\n\tint N = A.size();\n\tcomplex<double> r = polar((double) 1, (2 * PI) / (N));\n\tif (inv){\n\t\tr = pow(r, -1);\n\t}\n\tvector<complex<double>> B(N);\n\tfor (int i = N / 2; i > 0; i /= 2){\n\t\tcomplex<double> z = pow(r, i);\n\t\tcomplex<double> z2 = 1;\n\t\tfor (int j = 0; j < N; j += i * 2){\n\t\t\tfor (int k = 0; k < i; k++){\n\t\t\t\tA[i + j + k] *= z2;\n\t\t\t\tB[j / 2 + k] = A[j + k] + A[i + j + k];\n\t\t\t\tB[N / 2 + j / 2 + k] = A[j + k] - A[i + j + k];\n\t\t\t}\n\t\t\tz2 *= z;\n\t\t}\n\t\tswap(A, B);\n\t}\n\tif (inv){\n\t\tfor (int i = 0; i < N; i++){\n\t\t\tA[i] /= N;\n\t\t}\n\t}\n\treturn A;\n}\nvector<double> convolution(vector<double> A, vector<double> B){\n\tint deg = A.size() + B.size() - 1;\n\tint N = 1;\n\twhile (N < deg){\n\t\tN *= 2;\n\t}\n\tvector<complex<double>> A2(N, 0);\n\tfor (int i = 0; i < A.size(); i++){\n\t\tA2[i] = A[i];\n\t}\n\tvector<complex<double>> B2(N, 0);\n\tfor (int i = 0; i < B.size(); i++){\n\t\tB2[i] = B[i];\n\t}\n\tA2 = fft(A2, false);\n\tB2 = fft(B2, false);\n\tvector<complex<double>> C2(N);\n\tfor (int i = 0; i < N; i++){\n\t\tC2[i] = A2[i] * B2[i];\n\t}\n\tC2 = fft(C2, true);\n\tvector<double> C(deg);\n\tfor (int i = 0; i < deg; i++){\n\t C[i] = C2[i].real();\n\t}\n\treturn C;\n}\nint main(){\n cout << fixed << setprecision(20);\n int N;\n cin >> N;\n int M = min(N, 100);\n vector<vector<double>> dp(M + 1, vector<double>(N + 1, 0));\n dp[0][0] = 1;\n for (int i = 0; i < M; i++){\n vector<double> tmp = convolution(dp[i], dp[i]);\n dp[i + 1][0] = 1;\n for (int j = 1; j <= N; j++){\n dp[i + 1][j] = max(tmp[j - 1], (double) 0) / j;\n }\n }\n for (int i = 1; i <= M; i++){\n cout << max(dp[i][N] - dp[i - 1][N], (double) 0) << endl;\n }\n for (int i = M; i < N; i++){\n cout << 0 << endl;\n }\n}", "accuracy": 0.3225806451612903, "time_ms": 860, "memory_kb": 32608, "score_of_the_acc": -0.6353, "final_rank": 17 }, { "submission_id": "aoj_2268_6975817", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ndouble PI = acos(-1);\nvector<complex<double>> fft(vector<complex<double>> A, bool inv){\n\tint N = A.size();\n\tcomplex<double> r = polar((double) 1, (2 * PI) / (N));\n\tif (inv){\n\t\tr = pow(r, -1);\n\t}\n\tvector<complex<double>> B(N);\n\tfor (int i = N / 2; i > 0; i /= 2){\n\t\tcomplex<double> z = pow(r, i);\n\t\tcomplex<double> z2 = 1;\n\t\tfor (int j = 0; j < N; j += i * 2){\n\t\t\tfor (int k = 0; k < i; k++){\n\t\t\t\tA[i + j + k] *= z2;\n\t\t\t\tB[j / 2 + k] = A[j + k] + A[i + j + k];\n\t\t\t\tB[N / 2 + j / 2 + k] = A[j + k] - A[i + j + k];\n\t\t\t}\n\t\t\tz2 *= z;\n\t\t}\n\t\tswap(A, B);\n\t}\n\tif (inv){\n\t\tfor (int i = 0; i < N; i++){\n\t\t\tA[i] /= N;\n\t\t}\n\t}\n\treturn A;\n}\nvector<double> convolution(vector<double> A, vector<double> B){\n\tint deg = A.size() + B.size() - 1;\n\tint N = 1;\n\twhile (N < deg){\n\t\tN *= 2;\n\t}\n\tvector<complex<double>> A2(N, 0);\n\tfor (int i = 0; i < A.size(); i++){\n\t\tA2[i] = A[i];\n\t}\n\tvector<complex<double>> B2(N, 0);\n\tfor (int i = 0; i < B.size(); i++){\n\t\tB2[i] = B[i];\n\t}\n\tA2 = fft(A2, false);\n\tB2 = fft(B2, false);\n\tvector<complex<double>> C2(N);\n\tfor (int i = 0; i < N; i++){\n\t\tC2[i] = A2[i] * B2[i];\n\t}\n\tC2 = fft(C2, true);\n\tvector<double> C(deg);\n\tfor (int i = 0; i < deg; i++){\n\t C[i] = C2[i].real();\n\t}\n\treturn C;\n}\nint main(){\n cout << fixed << setprecision(20);\n int N;\n cin >> N;\n int M = min(N, 100);\n vector<vector<double>> dp(M + 1, vector<double>(N + 1, 0));\n dp[0][0] = 1;\n for (int i = 0; i < M; i++){\n vector<double> tmp = convolution(dp[i], dp[i]);\n dp[i + 1][0] = 1;\n for (int j = 1; j <= N; j++){\n dp[i + 1][j] = tmp[j - 1] / j;\n }\n }\n for (int i = 1; i <= M; i++){\n cout << dp[i][N] - dp[i - 1][N] << endl;\n }\n for (int i = M; i < N; i++){\n cout << 0 << endl;\n }\n}", "accuracy": 0.3225806451612903, "time_ms": 850, "memory_kb": 32644, "score_of_the_acc": -0.6335, "final_rank": 16 }, { "submission_id": "aoj_2268_4414300", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\n#define rep(i, n) for(int i=0;i<n;i++)\n#define repn(i, n) for(int i=1;i<=n;i++)\ntypedef pair<ll,ll> P;\n#define fi first\n#define sc second\n#define pb push_back\n#define mp make_pair\n#define all(x) x.begin(), x.end()\n#define POSL(v, x) lower_bound(v.begin(), v.end(), x) - v.begin()\n\nstruct FFT{\n\tconst double PI = acos(-1.);\n\tstruct cp{\n\t\tdouble x, y; \n\t\tcp(double x = 0, double y = 0) : x(x), y(y) {}\n\t\tcp operator + (const cp & a) { return cp(x+a.x, y+a.y); }\n\t\tcp operator - (const cp & a) { return cp(x-a.x, y-a.y); }\n\t\tcp operator * (const cp & a) { return cp(x*a.x-y*a.y, x*a.y+y*a.x); }\n\t};\n\tcp g[1<<17];\n\tvoid FFTMakeG(int n){\n\t\trep(i, n){\n\t\t\tdouble th = PI * 2.0 * i / n;\n\t\t\tg[i] = cp(cos(th), sin(th));\n\t\t}\n\t}\n\tvoid fft( vector<cp>&z ){\n\t\tint n = z.size();\n\t\tint i, j, k, l, m;\n\t\tfor(i = 1, j = 0; i < n; ++i){\n\t\t\tfor(int k=n>>1;(j^=k) < k ; k >>= 1);\n\t\t\tif(i < j) swap(z[i], z[j]);\n\t\t}\n\t\tfor(l = k = 1, m = __builtin_ctz(n); m--; l |= k <<= 1){\n\t\t\tfor(i = 0;i<n;i++) if(i < (j=i^k)){\n\t\t\t\tcp t = z[j] * g[(i & l) << m];\n\t\t\t\tz[j] = z[i] - t;\n\t\t\t\tz[i] = z[i] + t;\n\t\t\t}\n\t\t}\n\t}\n\tvector<double>conv (vector<double>a){\n\t\tint n = a.size() + a.size();\n\t\tint t = 1;\n\t\twhile(t < n) t <<= 1;\n\t\tFFTMakeG(t);\n\t\tvector<cp>fa, fb;\n\t\trep(i, t){\n\t\t\tif(i < a.size()) fa.pb(cp(a[i]));\n\t\t\telse fa.pb(cp());\n\t\t\t\n\t\t\t//if(i < a.size()) fb.pb(cp(a[i]));\n\t\t\t//else fb.pb(cp());\n\t\t}\n\t\tfft(fa); //fft(fb);\n\t\trep(i, t) fa[i] = fa[i] * fa[i];\n\t\tfor(int l=1,r=t-1;l<r;l++,r--) swap(g[l], g[r]);\n\t\tfft(fa);\n\t\tvector<double>ret;\n\t\trep(i, t) ret.pb(fa[i].x / (double)t);\n\t\treturn ret;\n\t}\n}kaede;\n\ndouble dp[30005][105];\n\n\nint main(){\n\trep(j, 105){\n\t\tdp[0][j] = 1.;\n\t}\n\trepn(j, 104){\n\t\tdp[1][j] = 1.;\n\t}\n\tfor(int j=2;j<105;j++){\n\t\t{\n\t\t\tvector<double>a;\n\t\t\trep(c, 30005) a.pb(dp[c][j-1]);\n\t\t\tauto b = kaede.conv(a);\n\t\t\trep(c, 30003) dp[c+1][j] = b[c] / (double)(c+1);\n\t\t}\n\t}\n\tint n; cin >> n;\n\trepn(i, n){\n\t\tif(i <= 100) printf(\"%.12f\\n\", dp[n][i]-dp[n][i-1]);\n\t\telse puts(\"0\");\n\t}\n}", "accuracy": 1, "time_ms": 910, "memory_kb": 32384, "score_of_the_acc": -0.6435, "final_rank": 9 }, { "submission_id": "aoj_2268_3771999", "code_snippet": "#include <cmath>\n#include <vector>\n#include <complex>\n#include <iostream>\n#include <algorithm>\nusing namespace std;\nconst double pi = acos(-1);\nvoid discrete_fourier_transform(std::size_t n, std::complex<double> *v, bool rev) {\n\tfor (std::size_t i = 0, j = 1; j < n - 1; ++j) {\n\t\tfor (std::size_t k = n >> 1; k > (i ^= k); k >>= 1);\n\t\tif (i > j) std::swap(v[i], v[j]);\n\t}\n\tfor (std::size_t b = 1; b < n; b <<= 1) {\n\t\tstd::complex<double> wr = std::polar(1.0, (rev ? -1.0 : 1.0) * pi / b);\n\t\tfor (std::size_t i = 0; i < n; i += 2 * b) {\n\t\t\tstd::complex<double> w = 1.0;\n\t\t\tfor (std::size_t j = 0; j < b; ++j) {\n\t\t\t\tstd::complex<double> v0 = v[i + j];\n\t\t\t\tstd::complex<double> v1 = w * v[i + j + b];\n\t\t\t\tv[i + j] = v0 + v1;\n\t\t\t\tv[i + j + b] = v0 - v1;\n\t\t\t\tw *= wr;\n\t\t\t}\n\t\t}\n\t}\n\tif (!rev) return;\n\tfor (std::size_t i = 0; i < n; i++) v[i] /= n;\n}\ndouble prob[54][65555]; complex<double> tmp[65555];\nint main() {\n\tios::sync_with_stdio(false);\n\tint N;\n\tcin >> N;\n\tint b = 1;\n\twhile (b < 2 * (N + 1)) b *= 2;\n\tprob[0][0] = 1.0; prob[0][1] = 1.0;\n\tfor (int i = 0; i < 50; ++i) {\n\t\tfor (int j = 0; j < b; ++j) {\n\t\t\ttmp[j] = (j <= N ? prob[i][j] : 0.0);\n\t\t}\n\t\tdiscrete_fourier_transform(b, tmp, false);\n\t\tfor (int j = 0; j < b; ++j) {\n\t\t\ttmp[j] *= tmp[j];\n\t\t}\n\t\tdiscrete_fourier_transform(b, tmp, true);\n\t\tprob[i + 1][0] = 1.0;\n\t\tfor (int j = 0; j < N; ++j) {\n\t\t\tprob[i + 1][j + 1] = tmp[j].real() / (j + 1);\n\t\t}\n\t}\n\tcout.precision(7);\n\tfor (int i = 0; i < N; ++i) {\n\t\tcout << fixed << (i > 50 ? 0.0 : max(prob[i][N] - (i > 0 ? prob[i - 1][N] : 0.0), 0.0)) << '\\n';\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 400, "memory_kb": 16536, "score_of_the_acc": -0.2669, "final_rank": 4 }, { "submission_id": "aoj_2268_3771993", "code_snippet": "#include <cmath>\n#include <vector>\n#include <complex>\n#include <iostream>\n#include <algorithm>\nusing namespace std;\nconst double pi = acos(-1);\nvoid discrete_fourier_transform(std::size_t n, std::complex<double> *v, bool rev) {\n\tfor (std::size_t i = 0, j = 1; j < n - 1; ++j) {\n\t\tfor (std::size_t k = n >> 1; k > (i ^= k); k >>= 1);\n\t\tif (i > j) std::swap(v[i], v[j]);\n\t}\n\tfor (std::size_t b = 1; b < n; b <<= 1) {\n\t\tstd::complex<double> wr = std::polar(1.0, (rev ? -1.0 : 1.0) * pi / b);\n\t\tfor (std::size_t i = 0; i < n; i += 2 * b) {\n\t\t\tstd::complex<double> w = 1.0;\n\t\t\tfor (std::size_t j = 0; j < b; ++j) {\n\t\t\t\tstd::complex<double> v0 = v[i + j];\n\t\t\t\tstd::complex<double> v1 = w * v[i + j + b];\n\t\t\t\tv[i + j] = v0 + v1;\n\t\t\t\tv[i + j + b] = v0 - v1;\n\t\t\t\tw *= wr;\n\t\t\t}\n\t\t}\n\t}\n\tif (!rev) return;\n\tfor (std::size_t i = 0; i < n; i++) v[i] /= n;\n}\ndouble prob[64][65555]; complex<double> tmp[65555];\nint main() {\n\tios::sync_with_stdio(false);\n\tint N;\n\tcin >> N;\n\tint b = 1;\n\twhile (b < 2 * (N + 1)) b *= 2;\n\tprob[0][0] = 1.0; prob[0][1] = 1.0;\n\tfor (int i = 0; i < 55; ++i) {\n\t\tfor (int j = 0; j < b; ++j) {\n\t\t\ttmp[j] = (j <= N ? prob[i][j] : 0.0);\n\t\t}\n\t\tdiscrete_fourier_transform(b, tmp, false);\n\t\tfor (int j = 0; j < b; ++j) {\n\t\t\ttmp[j] *= tmp[j];\n\t\t}\n\t\tdiscrete_fourier_transform(b, tmp, true);\n\t\tprob[i + 1][0] = 1.0;\n\t\tfor (int j = 0; j < N; ++j) {\n\t\t\tprob[i + 1][j + 1] = tmp[j].real() / (j + 1);\n\t\t}\n\t}\n\tcout.precision(7);\n\tfor (int i = 0; i < N; ++i) {\n\t\tcout << fixed << (i > 55 ? 0.0 : max(prob[i][N] - (i > 0 ? prob[i - 1][N] : 0.0), 0.0)) << '\\n';\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 610, "memory_kb": 17604, "score_of_the_acc": -0.3339, "final_rank": 5 }, { "submission_id": "aoj_2268_3771990", "code_snippet": "#include <cmath>\n#include <vector>\n#include <complex>\n#include <iostream>\n#include <algorithm>\nusing namespace std;\nconst double pi = acos(-1);\nvoid discrete_fourier_transform(std::size_t n, std::complex<double> *v, bool rev) {\n\tfor (std::size_t i = 0, j = 1; j < n - 1; ++j) {\n\t\tfor (std::size_t k = n >> 1; k > (i ^= k); k >>= 1);\n\t\tif (i > j) std::swap(v[i], v[j]);\n\t}\n\tfor (std::size_t b = 1; b < n; b <<= 1) {\n\t\tstd::complex<double> wr = std::polar(1.0, (rev ? -1.0 : 1.0) * pi / b);\n\t\tfor (std::size_t i = 0; i < n; i += 2 * b) {\n\t\t\tstd::complex<double> w = 1.0;\n\t\t\tfor (std::size_t j = 0; j < b; ++j) {\n\t\t\t\tstd::complex<double> v0 = v[i + j];\n\t\t\t\tstd::complex<double> v1 = w * v[i + j + b];\n\t\t\t\tv[i + j] = v0 + v1;\n\t\t\t\tv[i + j + b] = v0 - v1;\n\t\t\t\tw *= wr;\n\t\t\t}\n\t\t}\n\t}\n\tif (!rev) return;\n\tfor (std::size_t i = 0; i < n; i++) v[i] /= n;\n}\ndouble prob[64][65555]; complex<double> tmp[65555];\nint main() {\n\tios::sync_with_stdio(false);\n\tint N;\n\tcin >> N;\n\tint b = 1;\n\twhile (b < 2 * (N + 1)) b *= 2;\n\tprob[0][0] = 1.0; prob[0][1] = 1.0;\n\tfor (int i = 0; i < 55; ++i) {\n\t\tfor (int j = 0; j < b; ++j) {\n\t\t\ttmp[j] = (j <= N ? prob[i][j] : 0.0);\n\t\t}\n\t\tdiscrete_fourier_transform(b, tmp, false);\n\t\tfor (int j = 0; j < b; ++j) {\n\t\t\ttmp[j] *= tmp[j];\n\t\t}\n\t\tdiscrete_fourier_transform(b, tmp, true);\n\t\tprob[i + 1][0] = 1.0;\n\t\tfor (int j = 0; j < N; ++j) {\n\t\t\tprob[i + 1][j + 1] = tmp[j].real() / (j + 1);\n\t\t}\n\t}\n\tcout.precision(7);\n\tfor (int i = 0; i < N; ++i) {\n\t\tcout << fixed << (i > 55 ? 0.0 : prob[i][N] - (i > 0 ? prob[i - 1][N] : 0.0)) << '\\n';\n\t}\n\treturn 0;\n}", "accuracy": 0.3225806451612903, "time_ms": 440, "memory_kb": 17676, "score_of_the_acc": -0.2948, "final_rank": 14 }, { "submission_id": "aoj_2268_3771960", "code_snippet": "#include <cmath>\n#include <vector>\n#include <complex>\n#include <iostream>\n#include <algorithm>\nusing namespace std;\nconst long double pi = acos(-1);\nvoid discrete_fourier_transform(std::size_t n, std::complex<long double> *v, bool rev) {\n\tfor (std::size_t i = 0, j = 1; j < n - 1; ++j) {\n\t\tfor (std::size_t k = n >> 1; k > (i ^= k); k >>= 1);\n\t\tif (i > j) std::swap(v[i], v[j]);\n\t}\n\tfor (std::size_t b = 1; b < n; b <<= 1) {\n\t\tstd::complex<long double> wr = std::polar(1.0L, (rev ? -1.0 : 1.0) * pi / b);\n\t\tfor (std::size_t i = 0; i < n; i += 2 * b) {\n\t\t\tstd::complex<long double> w = 1.0;\n\t\t\tfor (std::size_t j = 0; j < b; ++j) {\n\t\t\t\tstd::complex<long double> v0 = v[i + j];\n\t\t\t\tstd::complex<long double> v1 = w * v[i + j + b];\n\t\t\t\tv[i + j] = v0 + v1;\n\t\t\t\tv[i + j + b] = v0 - v1;\n\t\t\t\tw *= wr;\n\t\t\t}\n\t\t}\n\t}\n\tif (!rev) return;\n\tfor (std::size_t i = 0; i < n; i++) v[i] /= n;\n}\nlong double prob[64][65555]; complex<long double> tmp[65555];\nint main() {\n\tios::sync_with_stdio(false);\n\tint N;\n\tcin >> N;\n\tint b = 1;\n\twhile (b < 2 * (N + 1)) b *= 2;\n\tprob[0][0] = 1.0; prob[0][1] = 1.0;\n\tfor (int i = 0; i < 60; ++i) {\n\t\tfor (int j = 0; j < b; ++j) {\n\t\t\ttmp[j] = (j <= N ? prob[i][j] : 0.0);\n\t\t}\n\t\tdiscrete_fourier_transform(b, tmp, false);\n\t\tfor (int j = 0; j < b; ++j) {\n\t\t\ttmp[j] *= tmp[j];\n\t\t}\n\t\tdiscrete_fourier_transform(b, tmp, true);\n\t\tprob[i + 1][0] = 1.0;\n\t\tfor (int j = 0; j < N; ++j) {\n\t\t\tprob[i + 1][j + 1] = tmp[j].real() / (j + 1);\n\t\t\tif (abs(prob[i + 1][j + 1]) <= 1.0e-8) prob[i + 1][j + 1] = 0.0;\n\t\t}\n\t}\n\tcout.precision(7);\n\tfor (int i = 0; i < N; ++i) {\n\t\tcout << fixed << (i > 60 ? 0.0 : prob[i][N] - (i > 0 ? prob[i - 1][N] : 0.0)) << '\\n';\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 2960, "memory_kb": 34008, "score_of_the_acc": -1.1555, "final_rank": 11 }, { "submission_id": "aoj_2268_3771959", "code_snippet": "#include <cmath>\n#include <vector>\n#include <complex>\n#include <iostream>\n#include <algorithm>\nusing namespace std;\nconst double pi = acos(-1);\nvoid discrete_fourier_transform(std::size_t n, std::complex<double> *v, bool rev) {\n\tfor (std::size_t i = 0, j = 1; j < n - 1; ++j) {\n\t\tfor (std::size_t k = n >> 1; k > (i ^= k); k >>= 1);\n\t\tif (i > j) std::swap(v[i], v[j]);\n\t}\n\tfor (std::size_t b = 1; b < n; b <<= 1) {\n\t\tstd::complex<double> wr = std::polar(1.0, (rev ? -1.0 : 1.0) * pi / b);\n\t\tfor (std::size_t i = 0; i < n; i += 2 * b) {\n\t\t\tstd::complex<double> w = 1.0;\n\t\t\tfor (std::size_t j = 0; j < b; ++j) {\n\t\t\t\tstd::complex<double> v0 = v[i + j];\n\t\t\t\tstd::complex<double> v1 = w * v[i + j + b];\n\t\t\t\tv[i + j] = v0 + v1;\n\t\t\t\tv[i + j + b] = v0 - v1;\n\t\t\t\tw *= wr;\n\t\t\t}\n\t\t}\n\t}\n\tif (!rev) return;\n\tfor (std::size_t i = 0; i < n; i++) v[i] /= n;\n}\ndouble prob[104][65555]; complex<double> tmp[65555];\nint main() {\n\tios::sync_with_stdio(false);\n\tint N;\n\tcin >> N;\n\tint b = 1;\n\twhile (b < 2 * (N + 1)) b *= 2;\n\tprob[0][0] = 1.0; prob[0][1] = 1.0;\n\tfor (int i = 0; i < 100; ++i) {\n\t\tfor (int j = 0; j < b; ++j) {\n\t\t\ttmp[j] = (j <= N ? prob[i][j] : 0.0);\n\t\t}\n\t\tdiscrete_fourier_transform(b, tmp, false);\n\t\tfor (int j = 0; j < b; ++j) {\n\t\t\ttmp[j] *= tmp[j];\n\t\t}\n\t\tdiscrete_fourier_transform(b, tmp, true);\n\t\tprob[i + 1][0] = 1.0;\n\t\tfor (int j = 0; j < N; ++j) {\n\t\t\tprob[i + 1][j + 1] = tmp[j].real() / (j + 1);\n\t\t\tif (abs(prob[i + 1][j + 1]) <= 1.0e-8) prob[i + 1][j + 1] = 0.0;\n\t\t}\n\t}\n\tcout.precision(7);\n\tfor (int i = 0; i < N; ++i) {\n\t\tcout << fixed << (i > 100 ? 0.0 : prob[i][N] - (i > 0 ? prob[i - 1][N] : 0.0)) << '\\n';\n\t}\n\treturn 0;\n}", "accuracy": 0.3225806451612903, "time_ms": 810, "memory_kb": 28456, "score_of_the_acc": -0.5565, "final_rank": 15 }, { "submission_id": "aoj_2268_2662808", "code_snippet": "#include <bits/stdc++.h>\n#define REP(i, n) for (int i = 0; (i) < int(n); ++ (i))\n#define REP3(i, m, n) for (int i = (m); (i) < int(n); ++ (i))\n#define REP_R(i, n) for (int i = int(n) - 1; (i) >= 0; -- (i))\nusing namespace std;\n\ntemplate <typename R>\nvoid fft_inplace(vector<complex<R> > & f, int dir) {\n int n = f.size();\n assert(n == pow(2, log2(n)));\n R theta = dir * 2 * M_PI / n;\n for (int m = n; m >= 2; m >>= 1) {\n REP (i, m / 2) {\n complex<R> w = polar<R>(1, i * theta);\n for (int j = i; j < n; j += m) {\n int k = j + m / 2;\n complex<R> x = f[j] - f[k];\n f[j] += f[k];\n f[k] = w * x;\n }\n }\n theta *= 2;\n }\n int i = 0;\n REP3 (j, 1, n - 1) {\n for (int k = n >> 1; k > (i ^= k); ) k >>= 1;\n if (j < i) {\n swap(f[i], f[j]);\n }\n }\n}\ntemplate <typename T, typename R = double>\nvoid convolution_self_inplace(vector<T> & a, int result_n) {\n int m = 2 * a.size() - 1;\n int n = pow(2, ceil(log2(m)));\n vector<complex<R> > x(n);\n copy(a.begin(), a.end(), x.begin());\n fft_inplace(x, +1);\n vector<complex<R> > z(n);\n REP (i, n) x[i] *= x[i];\n fft_inplace(x, -1);\n a.resize(result_n);\n REP (i, result_n) a[i] = x[i].real() / n;\n}\n\nint main() {\n // input\n int n; scanf(\"%d\", &n);\n // solve\n const int height = min(n, 50);\n vector<double> dp(n + 1);\n dp[0] = 1;\n double last_dp_n = 0;\n REP3 (j, 1, height + 1) {\n convolution_self_inplace(dp, n + 1);\n REP_R (i, n) {\n dp[i + 1] = dp[i] / (i + 1);\n }\n dp[0] = 1;\n // output\n printf(\"%.10lf\\n\", double(dp[n] - last_dp_n));\n last_dp_n = dp[n];\n }\n REP3 (j, height + 1, n + 1) {\n printf(\"0\\n\");\n }\n return 0;\n}", "accuracy": 1, "time_ms": 680, "memory_kb": 5632, "score_of_the_acc": -0.1573, "final_rank": 3 }, { "submission_id": "aoj_2268_2662807", "code_snippet": "#include <bits/stdc++.h>\n#define REP(i, n) for (int i = 0; (i) < int(n); ++ (i))\n#define REP3(i, m, n) for (int i = (m); (i) < int(n); ++ (i))\n#define REP_R(i, n) for (int i = int(n) - 1; (i) >= 0; -- (i))\nusing namespace std;\n\ntemplate <typename R>\nvoid fft_inplace(vector<complex<R> > & f, int dir) {\n int n = f.size();\n assert(n == pow(2, log2(n)));\n R theta = dir * 2 * M_PI / n;\n for (int m = n; m >= 2; m >>= 1) {\n REP (i, m / 2) {\n complex<R> w = polar<R>(1, i * theta);\n for (int j = i; j < n; j += m) {\n int k = j + m / 2;\n complex<R> x = f[j] - f[k];\n f[j] += f[k];\n f[k] = w * x;\n }\n }\n theta *= 2;\n }\n int i = 0;\n REP3 (j, 1, n - 1) {\n for (int k = n >> 1; k > (i ^= k); ) k >>= 1;\n if (j < i) {\n swap(f[i], f[j]);\n }\n }\n}\ntemplate <typename T, typename R = long double>\nvoid convolution_self_inplace(vector<T> & a, int result_n) {\n int m = 2 * a.size() - 1;\n int n = pow(2, ceil(log2(m)));\n vector<complex<R> > x(n);\n copy(a.begin(), a.end(), x.begin());\n fft_inplace(x, +1);\n vector<complex<R> > z(n);\n REP (i, n) x[i] *= x[i];\n fft_inplace(x, -1);\n a.resize(result_n);\n REP (i, result_n) a[i] = x[i].real() / n;\n}\n\nint main() {\n // input\n int n; scanf(\"%d\", &n);\n // solve\n const int height = min(n, 50);\n vector<long double> dp(n + 1);\n dp[0] = 1;\n long double last_dp_n = 0;\n REP3 (j, 1, height + 1) {\n convolution_self_inplace(dp, n + 1);\n REP_R (i, n) {\n dp[i + 1] = dp[i] / (i + 1);\n }\n dp[0] = 1;\n // output\n printf(\"%.10lf\\n\", double(dp[n] - last_dp_n));\n last_dp_n = dp[n];\n }\n REP3 (j, height + 1, n + 1) {\n printf(\"0\\n\");\n }\n return 0;\n}", "accuracy": 1, "time_ms": 2370, "memory_kb": 7936, "score_of_the_acc": -0.5949, "final_rank": 7 }, { "submission_id": "aoj_2268_2662806", "code_snippet": "#include <bits/stdc++.h>\n#define REP(i, n) for (int i = 0; (i) < int(n); ++ (i))\n#define REP3(i, m, n) for (int i = (m); (i) < int(n); ++ (i))\nusing namespace std;\n\ntemplate <typename T>\nvoid fft_inplace(vector<complex<T> > & f, int dir) {\n int n = f.size();\n assert (n == pow(2, log2(n)));\n if (n == 1) return;\n vector<complex<T> > a(n / 2), b(n / 2);\n REP (i, n / 2) {\n a[i] = f[2 * i];\n b[i] = f[2 * i+ 1];\n }\n fft_inplace(a, dir);\n fft_inplace(b, dir);\n const complex<T> zeta = polar<T>(1, dir * 2 * M_PI / n);\n complex<T> pow_zeta = 1;\n REP (i, n) {\n f[i] = a[i % (n / 2)] + pow_zeta * b[i % (n / 2)];\n pow_zeta *= zeta;\n }\n}\ntemplate <typename T, typename R = double>\ndeque<T> convolution(deque<T> const & a, deque<T> const & b) {\n int m = a.size() + b.size() - 1;\n int n = pow(2, ceil(log2(m)));\n vector<complex<R> > x(n), y(n);\n copy(a.begin(), a.end(), x.begin());\n copy(b.begin(), b.end(), y.begin());\n fft_inplace(x, +1);\n fft_inplace(y, +1);\n vector<complex<R> > z(n);\n REP (i, n) z[i] = x[i] * y[i];\n fft_inplace(z, -1);\n deque<T> c(m);\n REP (i, m) c[i] = is_integral<T>::value ? round(z[i].real() / n) : z[i].real() / n;\n return c;\n}\n\nint main() {\n // input\n int n; scanf(\"%d\", &n);\n // solve\n const int height = min(n, 50);\n deque<double> dp(n + 1);\n dp[0] = 1;\n double last_dp_n = 0;\n REP3 (j, 1, height + 1) {\n dp = convolution(dp, dp);\n dp.push_front(1);\n dp.resize(n + 1);\n REP (i, n) {\n dp[i + 1] /= (i + 1);\n }\n // output\n printf(\"%.10lf\\n\", dp[n] - last_dp_n);\n last_dp_n = dp[n];\n }\n REP3 (j, height + 1, n + 1) {\n printf(\"0\\n\");\n }\n return 0;\n}", "accuracy": 0.3225806451612903, "time_ms": 4400, "memory_kb": 8636, "score_of_the_acc": -1.0873, "final_rank": 18 }, { "submission_id": "aoj_2268_380010", "code_snippet": "#include <cstdio>\n#include <cmath>\n#include <algorithm>\n#include <complex>\n#include <cstring>\nusing namespace std;\n\nconst int MAX_N = 30000<<3;\nconst double PI = 4.0*atan(1.0);\n\nint N, NN;\ndouble prev[MAX_N], cur[MAX_N];\ncomplex<double> xs[MAX_N], zs[MAX_N];\n\nstatic const complex<double> I(0,1);\n\nvoid init(){\n\tscanf(\"%d\", &N);\n}\n\nvoid fft(complex<double> a[], int n, bool inv = false){\n\tdouble theta = 2 * PI / n * (inv ? -1 : 1);\n\tfor(int m=n; m>=2; m>>=1){\n\t\tint mh = m >> 1;\n\t\tfor(int i=0; i<mh; ++i){\n\t\t\tcomplex<double> w = exp(i*theta*I);\n\t\t\tfor(int j=i; j<n; j+=m){\n\t\t\t\tint k = j + mh;\n\t\t\t\tcomplex<double> x = a[j] - a[k];\n\t\t\t\ta[j] += a[k];\n\t\t\t\ta[k] = w * x;\n\t\t\t}\n\t\t}\n\t\ttheta *= 2;\n\t}\n\tint i = 0;\n\tfor(int j=1; j<n-1; ++j){\n\t\tfor(int k=n>>1; k>(i^=k); k>>=1);\n\t\tif(j<i){\n\t\t\tswap(a[i], a[j]);\n\t\t}\n\t}\n\tif(inv){\n\t\tfor(int i=0; i<n; ++i){\n\t\t\ta[i] /= n;\n\t\t}\n\t}\n}\n\nvoid calc(){\n\tfor(int i=0; i<NN; ++i){\n\t\txs[i] = complex<double>(prev[i], 0.0);\n\t}\n\tfft(xs, NN);\n\tfor(int i=0; i<NN; ++i){\n\t\tzs[i] = xs[i] * xs[i];\n\t}\n\tfft(zs, NN, true);\n\tmemset(cur, 0, sizeof(cur));\n\tcur[0] = 1.0;\n\tfor(int i=1; i<=N; ++i){\n\t\tcur[i] = zs[i-1].real() / i;\n\t}\n}\n\nvoid solve(){\n\tNN = 4<<(31 - __builtin_clz(N));\n\tfill(prev, prev+NN, 0.0);\n\tprev[0] = prev[1] = 1.0;\n\tbool small = false;\n\tprintf(\"%f\\n\", prev[N]);\n\tfor(int i=0; i<N-1; ++i){\n\t\tif(small){\n\t\t\tputs(\"0.0\");\n\t\t}\n\t\telse{\n\t\t\tcalc();\n\t\t\tprintf(\"%.12f\\n\", cur[N] - prev[N]);\n\n\t\t\tif(i>50 && cur[N] - prev[N] < 1e-5){\n\t\t\t\tsmall = true;\n\t\t\t}\n\t\t\tmemcpy(prev, cur, sizeof(prev));\n\t\t}\n\t}\n}\n\nint main(){\n\tinit();\n\tsolve();\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 2250, "memory_kb": 0, "score_of_the_acc": -0.4384, "final_rank": 6 }, { "submission_id": "aoj_2268_294257", "code_snippet": "#include <iostream>\n#include <cstdlib>\n#include <algorithm>\n#include <queue>\n#include <cmath>\n#include <complex>\n#include <cstdio>\n\nusing namespace std;\n\ninline int getInt(){ int s; scanf(\"%d\", &s); return s; }\n\n// sizeof v must be 2 ^ n\ntemplate<class T>\nvoid fft_core(vector<complex<T> > &v, bool rev = false){\n const int n = v.size();\n\n for(int i = 0; i < n; i++){\n int k = 0;\n int b = (n >> 1);\n int a = 1;\n\n while(b >= a){\n if(b & i) k |= a;\n if(a & i) k |= b;\n b >>= 1;\n a <<= 1;\n }\n\n if(i < k) swap(v[i], v[k]);\n }\n\n int m = 2;\n\n while(m <= n){\n double arg = -2.0 * M_PI / m;\n complex<T> w(cos(arg), sin(arg));\n if(rev) w = 1.0 / w;\n\n for(int i = 0; i < n; i += m){\n complex<T> ww(1.0);\n\n for(int j = 0; j < m / 2; j++){\n\tint a = i + j;\n\tint b = i + j + m / 2;\n\tcomplex<T> t = ww * v[b];\n\n\tv[b] = v[a] - t;\n\tv[a] = v[a] + t;\n\n\tww *= w;\n }\n }\n m *= 2;\n }\n\n if(rev){\n double s = (double)n;\n for(int i = 0; i < n; i++)\n v[i] /= s;\n }\n}\n\ntemplate<class T> inline void fft(vector<T> &v){ fft_core(v); }\ntemplate<class T> inline void ifft(vector<T> &v){ fft_core(v, true); }\n\nint next(int n){\n int i = 1;\n while(i < n) i *= 2;\n return i;\n}\n\n\nint main(){\n int n = getInt();\n int m = next(n + 1);\n int mm = m * 2;\n vector<complex<double> > p(mm, 0.0);\n double prev = 0.0;\n p[0] = 1.0;\n \n for(int i = 1; i <= n; i++){\n if(i < 51){\n fft(p);\n\n for(int j = mm - 1; j >= 0; j--){\n\tp[j] = p[j] * p[j];\n }\n \n ifft(p);\n\n for(int j = mm - 1; j >= 1; j--){\n\tp[j] = p[j - 1];\n }\n p[0] = 1.0;\n for(int j = 1; j < mm / 2; j++)\n\tp[j] /= j;\n for(int j = mm / 2; j < mm; j++)\n\tp[j] = 0.0;\n \n double ans = n >= p.size() ? 0.0 : p[n].real();\n double pans = (ans - prev);\n if(ans > 1e-10) pans += 0.000007;\n printf(\"%.7f\\n\", max(0.0, pans) + 1e-10);\n prev = ans;\n }else{\n printf(\"%.7f\\n\", 0.0 + 1e-10);\n }\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 2980, "memory_kb": 0, "score_of_the_acc": -0.6114, "final_rank": 8 }, { "submission_id": "aoj_2268_294255", "code_snippet": "#include <iostream>\n#include <cstdlib>\n#include <algorithm>\n#include <queue>\n#include <cmath>\n#include <complex>\n#include <cstdio>\n\nusing namespace std;\n\ninline int getInt(){ int s; scanf(\"%d\", &s); return s; }\n\n// sizeof v must be 2 ^ n\ntemplate<class T>\nvoid fft_core(vector<complex<T> > &v, bool rev = false){\n const int n = v.size();\n\n for(int i = 0; i < n; i++){\n int k = 0;\n int b = (n >> 1);\n int a = 1;\n\n while(b >= a){\n if(b & i) k |= a;\n if(a & i) k |= b;\n b >>= 1;\n a <<= 1;\n }\n\n if(i < k) swap(v[i], v[k]);\n }\n\n int m = 2;\n\n while(m <= n){\n double arg = -2.0 * M_PI / m;\n complex<T> w(cos(arg), sin(arg));\n if(rev) w = 1.0 / w;\n\n for(int i = 0; i < n; i += m){\n complex<T> ww(1.0);\n\n for(int j = 0; j < m / 2; j++){\n\tint a = i + j;\n\tint b = i + j + m / 2;\n\tcomplex<T> t = ww * v[b];\n\n\tv[b] = v[a] - t;\n\tv[a] = v[a] + t;\n\n\tww *= w;\n }\n }\n m *= 2;\n }\n\n if(rev){\n double s = (double)n;\n for(int i = 0; i < n; i++)\n v[i] /= s;\n }\n}\n\ntemplate<class T> inline void fft(vector<T> &v){ fft_core(v); }\ntemplate<class T> inline void ifft(vector<T> &v){ fft_core(v, true); }\n\nint next(int n){\n int i = 1;\n while(i < n) i *= 2;\n return i;\n}\n\n\nint main(){\n int n = getInt();\n int m = next(n + 1);\n int mm = m * 2;\n vector<complex<double> > p(mm, 0.0);\n double prev = 0.0;\n p[0] = 1.0;\n \n for(int i = 1; i <= n; i++){\n if(i < 80){\n fft(p);\n\n for(int j = mm - 1; j >= 0; j--){\n\tp[j] = p[j] * p[j];\n }\n \n ifft(p);\n\n for(int j = mm - 1; j >= 1; j--){\n\tp[j] = p[j - 1];\n }\n p[0] = 1.0;\n for(int j = 1; j < mm / 2; j++)\n\tp[j] /= j;\n for(int j = mm / 2; j < mm; j++)\n\tp[j] = 0.0;\n \n double ans = n >= p.size() ? 0.0 : p[n].real();\n double pans = (ans - prev);\n if(ans > 1e-10) pans += 0.000007;\n printf(\"%.7f\\n\", max(0.0, pans) + 1e-10);\n prev = ans;\n }else{\n printf(\"%.7f\\n\", 0.0 + 1e-10);\n }\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 4620, "memory_kb": 0, "score_of_the_acc": -1, "final_rank": 10 }, { "submission_id": "aoj_2268_275944", "code_snippet": "#include <stdio.h>\n#include <string.h>\n#include <algorithm>\n#include <iostream>\n#include <math.h>\n#include <assert.h>\n#include <vector>\n#include <complex>\n\nusing namespace std;\ntypedef long long ll;\ntypedef unsigned int uint;\ntypedef unsigned long long ull;\nstatic const double EPS = 1e-9;\nstatic const double PI = acos(-1.0);\n\n#define REP(i, n) for (int i = 0; i < (int)(n); i++)\n#define FOR(i, s, n) for (int i = (s); i < (int)(n); i++)\n#define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++)\n#define FORIT(it, c) for (__typeof((c).begin())it = (c).begin(); it != (c).end(); it++)\n#define MEMSET(v, h) memset((v), h, sizeof(v))\n\n#include <complex>\n\ntypedef complex<double> Complex;\n\n// a.size must 2^x\nvector<Complex> FFT(double theta, const vector<Complex> &a) {\n int n = a.size();\n vector<Complex> ret = a;\n for (int m = n; m >= 2; m >>= 1) {\n int mh = m >> 1;\n for (int i = 0; i < mh; i++) {\n Complex w = exp(i * theta * Complex(0, 1));\n for (int j = i; j < n; j += m) {\n int k = j + mh;\n Complex x = ret[j] - ret[k];\n ret[j] += ret[k];\n ret[k] = w * x;\n }\n }\n theta *= 2;\n }\n int i = 0;\n for (int j = 1; j < n - 1; j++) {\n for (int k = n >> 1; k > (i ^= k); k >>= 1) {;}\n if (j < i) { swap(ret[i], ret[j]); }\n }\n return ret;\n}\n\nvector<double> Convolution(const vector<double> &lhs, const vector<double> &rhs) {\n int n = 1;\n while (n < (int)max(lhs.size(), rhs.size()) * 2) { n <<= 1; }\n vector<Complex> temp1(n);\n vector<Complex> temp2(n);\n for (int i = 0; i < n; i++) {\n if (i < (int)lhs.size()) {\n temp1[i] = Complex(lhs[i], 0);\n }\n if (i < (int)rhs.size()) {\n temp2[i] = Complex(rhs[i], 0);\n }\n }\n temp1 = FFT(2.0 * PI / n, temp1);\n temp2 = FFT(2.0 * PI / n, temp2);\n for (int i = 0; i < n; i++) { temp1[i] *= temp2[i]; }\n temp1 = FFT(-2.0 * PI / n, temp1);\n vector<double> ret;\n ret.resize(n + 1);\n for (int i = 0; i < n; i++) {\n ret[i + 1] = temp1[i].real() / n;\n }\n ret[0] = 1.0;\n return ret;\n}\n\nconst int H = 60;\nvector<double> anss[H + 1];\nint main() {\n int n;\n while (scanf(\"%d\", &n) > 0) {\n anss[0] = vector<double>(n + 1, 0);\n anss[0][0] = 1.0;\n\n FOREQ(h, 1, H) {\n anss[h] = Convolution(anss[h - 1], anss[h - 1]);\n anss[h].resize(n + 1);\n FOREQ(i, 1, n) {\n anss[h][i] /= i;\n }\n }\n\n FOREQ(h, 1, min(n, H)) {\n printf(\"%.8f\\n\", anss[h][n] - anss[h - 1][n] + EPS);\n }\n FOREQ(h, H + 1, n) {\n printf(\"%.8f\\n\", 0.0);\n }\n }\n}", "accuracy": 1, "time_ms": 4250, "memory_kb": 33000, "score_of_the_acc": -1.445, "final_rank": 12 } ]
aoj_2278_cpp
問題 H あばれうなぎ 問題文 きつねのしえるはうなぎを食べるのが好きである. 2T+1 枚の鉄板が連続して並んでおり,順番に -T, -T+1, ..., T の番号が付いている.しえるはこれらの鉄板に熱を加え,生きたうなぎを焼こうとしている.うなぎを焼く手順は以下のようなものである. まず,時刻 t=-1 のときに各鉄板にどれだけのエネルギーを加えるかを決め,実際にエネルギーを加える.各鉄板に加えるエネルギーの総和は E 以下でなければならない.すなわち, i 番の鉄板に加えるエネルギーを E(i) として, E(i) ≥ 0, E(-T)+...+E(T) ≤ E でなければならない.なお,エネルギーは整数でなくてもよい. エネルギーを加えたあと,鉄板の熱さは E(i) / C(i) になる.ここで, C(i) は i 番の鉄板の比熱である. t = -0.5 のときにうなぎを 0 番の鉄板に載せる. t = 0, 1, 2,..., T のときに,うなぎは今自分がいる鉄板の熱さだけ加熱される. t = 0.5, 1.5, 2.5,...,T-0.5 のとき,うなぎは自分の今いる鉄板にとどまるか,両隣の鉄板に移動することができる. t = T+0.5 のときにうなぎを鉄板の上から回収する. ところで生きたうなぎというのはとても活きがよくさらに頭も良いので,もしかすると自分にかかる熱さの総和が最小になるように動いたりするんではないかとしえるは不安になった.そうなると鉄板に適当に熱を加えただけではうなぎを十分に加熱できない恐れさえある. そこであなたには,うなぎが常に最適に動くとして,うなぎに与えることのできる熱さの和の最大値を求めて欲しい. 入力形式 入力は以下の形式で与えられる. T E C(-T) C(-T+1) ... C(T) T はうなぎを熱する時間, E は鉄板に与えることの出来るエネルギーの総和, C(i) は i 番の鉄板の比熱である. 出力形式 1 行目にうなぎに与えられる熱さの和の最大値を出力せよ.小数点以下何桁でも出力して構わないが,相対誤差あるいは絶対誤差が 10 -6 未満になっていなければならない. 制約 1 ≤ T ≤ 10 5 1 ≤ E ≤ 10 5 1 ≤ C(i) ≤ 10 5 入力に含まれる値はすべて整数である. 入出力例 入力例 1 1 100 1 1 1 出力例 1 100.0 この場合は 0 番の鉄板にエネルギーを全て加えるのが最適である. 入力例 2 2 100 1 2 100 2 1 出力例 2 2.8301886792453 入力例 3 5 100000 99999 99999 99999 1 1000 1000 1000 1 99999 99999 99999 出力例 3 199.4680851063830 謝辞 この問題は Writer の二人がインド料理 RAJU 百万遍店とマクドナルド百万遍店において夏の暑さに辟易する中で作られた.
[ { "submission_id": "aoj_2278_4376010", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 99999999999999999\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n#define NUM 100005\n\nint T;\ndouble C[2*NUM+1],E;\n\n//右側の合成プレートの比熱を求める\ndouble calc_right(){\n\n\tdouble ret = C[2*T];\n\tint rest_time = 1;\n\n\tfor(int base_loc = 2*T-1; base_loc >= T+1; base_loc--){\n\n\t\tret = min(C[base_loc],(ret*rest_time+C[base_loc])/(double)(rest_time+1));\n\t\trest_time++;\n\t}\n\n\treturn ret;\n}\n\n//左側の合成プレートの比熱を求める\ndouble calc_left(){\n\n\tdouble ret = C[0];\n\tint rest_time = 1;\n\n\tfor(int base_loc = 1; base_loc <= T-1; base_loc++){\n\n\t\tret = min(C[base_loc],(ret*rest_time+C[base_loc])/(double)(rest_time+1));\n\t\trest_time++;\n\t}\n\n\treturn ret;\n}\n\nint main(){\n\n\tscanf(\"%d %lf\",&T,&E);\n\n\tfor(int i = 0; i < 2*T+1; i++){\n\t\tscanf(\"%lf\",&C[i]);\n\t}\n\n\tdouble LEFT,RIGHT;\n\n\t//左方向の合成プレートの比熱計算\n\tLEFT = calc_left();\n\n\t//右方向の合成プレートの比熱計算\n\tRIGHT = calc_right();\n\n\tdouble ans = E/C[T];\n\tdouble mid,another;\n\n\tmid = E/(C[T]+T*(LEFT+RIGHT));\n\tanother = (E-mid*C[T])/(LEFT+RIGHT);\n\n\tans = max(ans,min((T+1)*mid,mid+another));\n\n\tprintf(\"%.10lf\\n\",ans);\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 4744, "score_of_the_acc": -0.9941, "final_rank": 7 }, { "submission_id": "aoj_2278_4376001", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 99999999999999999\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n#define NUM 100005\n\nint T;\ndouble C[2*NUM+1],E;\n\n//右側の合成プレートの比熱を求める\ndouble calc_right(){\n\n\tdouble ret = C[2*T];\n\tint rest_time = 1;\n\n\tfor(int base_loc = 2*T-1; base_loc >= T+1; base_loc--){\n\n\t\tret = min(C[base_loc],(ret*rest_time+C[base_loc])/(double)(rest_time+1));\n\t\trest_time++;\n\t}\n\n\treturn ret;\n}\n\n//左側の合成プレートの比熱を求める\ndouble calc_left(){\n\n\tdouble ret = C[0];\n\tint rest_time = 1;\n\n\tfor(int base_loc = 1; base_loc <= T-1; base_loc++){\n\n\t\tret = min(C[base_loc],(ret*rest_time+C[base_loc])/(double)(rest_time+1));\n\t\trest_time++;\n\t}\n\n\treturn ret;\n}\n\nint main(){\n\n\tscanf(\"%d %lf\",&T,&E);\n\n\tfor(int i = 0; i < 2*T+1; i++){\n\t\tscanf(\"%lf\",&C[i]);\n\t}\n\n\tdouble LEFT,RIGHT;\n\n\t//左方向の合成プレートの比熱計算\n\tLEFT = calc_left();\n\n\t//右方向の合成プレートの比熱計算\n\tRIGHT = calc_right();\n\n\tdouble ans = 0;\n\tdouble mid,another;\n\n\tmid = E/C[T];\n\tanother = 0;\n\tans = max(ans,min((T+1)*mid,mid+another));\n\n\tmid = E/(C[T]+T*(LEFT+RIGHT));\n\tanother = (E-mid*C[T])/(LEFT+RIGHT);\n\n\tans = max(ans,min((T+1)*mid,mid+another));\n\n\tprintf(\"%.10lf\\n\",ans);\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 4772, "score_of_the_acc": -1, "final_rank": 8 }, { "submission_id": "aoj_2278_2656288", "code_snippet": "#include \"bits/stdc++.h\"\n#include<unordered_map>\n#include<unordered_set>\n#include<random>\n#pragma warning(disable:4996)\nusing namespace std;\nusing ld=long double;\nint main() {\n\tint T; ld E;cin>>T>>E;\n\tvector<long long int>cs(2*T+1);\n\tfor (int i = 0; i < 2 * T + 1; ++i) {\n\t\tcin>>cs[i];\n\t}\n\tld ans=0;\n\tans=E/cs[T];\n\tint l=T-1,r=T+1;\n\t{\n\t\tld sum=0;\n\t\tld pre=1e18;\n\t\tfor (int i = T + 1; i < 2 * T + 1; ++i) {\n\t\t\tld now=sum+(2*T+1-i)*cs[i];\n\t\t\tif (now < pre) {\n\t\t\t\tr=i;\n\t\t\t\tpre=now;\n\t\t\t}\n\t\t\tsum+=cs[i];\n\t\t}\n\t}\n\t{\n\t\tld sum = 0;\n\t\tld pre = 1e18;\n\t\tfor (int i = T - 1; i >=0; --i) {\n\t\t\tld now = sum + (i+1)*cs[i];\n\t\t\tif (now < pre) {\n\t\t\t\tl = i;\n\t\t\t\tpre = now;\n\t\t\t}\n\t\t\tsum += cs[i];\n\t\t}\n\t}\n\tld sum=0;\n\tassert(l<r);\n\tfor (int i = l; i <= r; ++i) {\n\t\tif(i==l)sum+=(i+1)*cs[i];\n\t\telse if(i==r)sum+=(2*T+1-i)*cs[i];\n\t\telse sum+=cs[i];\n\t}\n\tans=max(ans,E/sum*(T+1));\n\tcout<<setprecision(20)<<fixed<<ans<<endl;\n\treturn 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 4524, "score_of_the_acc": -1.1298, "final_rank": 9 }, { "submission_id": "aoj_2278_2656287", "code_snippet": "#include \"bits/stdc++.h\"\n#include<unordered_map>\n#include<unordered_set>\n#include<random>\n#pragma warning(disable:4996)\nusing namespace std;\nusing ld=long double;\nint main() {\n\tint T; ld E;cin>>T>>E;\n\tvector<int>cs(2*T+1);\n\tfor (int i = 0; i < 2 * T + 1; ++i) {\n\t\tcin>>cs[i];\n\t}\n\tld ans=0;\n\tans=E/cs[T];\n\tint l=T-1,r=T+1;\n\t{\n\t\tld sum=0;\n\t\tld pre=1e18;\n\t\tfor (int i = T + 1; i < 2 * T + 1; ++i) {\n\t\t\tld now=sum+(2*T+1-i)*cs[i];\n\t\t\tif (now < pre) {\n\t\t\t\tr=i;\n\t\t\t\tpre=now;\n\t\t\t}\n\t\t\tsum+=cs[i];\n\t\t}\n\t}\n\t{\n\t\tld sum = 0;\n\t\tld pre = 1e18;\n\t\tfor (int i = T - 1; i >=0; --i) {\n\t\t\tld now = sum + (i+1)*cs[i];\n\t\t\tif (now < pre) {\n\t\t\t\tl = i;\n\t\t\t\tpre = now;\n\t\t\t}\n\t\t\tsum += cs[i];\n\t\t}\n\t}\n\tld sum=0;\n\tassert(l<r);\n\tfor (int i = l; i <= r; ++i) {\n\t\tif(i==l)sum+=(i+1)*cs[i];\n\t\telse if(i==r)sum+=(2*T+1-i)*cs[i];\n\t\telse sum+=cs[i];\n\t}\n\tans=max(ans,E/sum*(T+1));\n\tcout<<setprecision(20)<<fixed<<ans<<endl;\n\treturn 0;\n}", "accuracy": 0.4523809523809524, "time_ms": 40, "memory_kb": 3672, "score_of_the_acc": -0.9513, "final_rank": 15 }, { "submission_id": "aoj_2278_2656202", "code_snippet": "#include \"bits/stdc++.h\"\n#include<unordered_map>\n#include<unordered_set>\n#include<random>\n#pragma warning(disable:4996)\nusing namespace std;\nusing ld=long double;\nint main() {\n\tint T; ld E;cin>>T>>E;\n\tvector<int>cs(2*T+1);\n\tfor (int i = 0; i < 2 * T + 1; ++i) {\n\t\tcin>>cs[i];\n\t}\n\tld ans=0;\n\tans=E/cs[T];\n\tint l=T-1,r=T+1;\n\t{\n\t\tld sum=0;\n\t\tld pre=1e18;\n\t\tfor (int i = T + 1; i < 2 * T + 1; ++i) {\n\t\t\tld now=sum+(2*T+1-i)*cs[i];\n\t\t\tif (now < pre) {\n\t\t\t\tr=i;\n\t\t\t\tpre=now;\n\t\t\t}\n\t\t\tsum+=cs[i];\n\t\t}\n\t}\n\t{\n\t\tld sum = 0;\n\t\tld pre = 1e18;\n\t\tfor (int i = T - 1; i >=0; --i) {\n\t\t\tld now = sum + (i+1)*cs[i];\n\t\t\tif (now < pre) {\n\t\t\t\tl = i;\n\t\t\t\tpre = now;\n\t\t\t}\n\t\t\tsum += cs[i];\n\t\t}\n\t}\n\tld sum=0;\n\tfor (int i = l; i <= r; ++i) {\n\t\tif(i==l)sum+=(i+1)*cs[i];\n\t\telse if(i==r)sum+=(2*T+1-i)*cs[i];\n\t\telse sum+=cs[i];\n\t}\n\tans=max(ans,E/sum*(T+1));\n\tcout<<setprecision(20)<<fixed<<ans<<endl;\n\treturn 0;\n}", "accuracy": 0.4523809523809524, "time_ms": 40, "memory_kb": 3668, "score_of_the_acc": -0.9505, "final_rank": 14 }, { "submission_id": "aoj_2278_2656199", "code_snippet": "#include \"bits/stdc++.h\"\n#include<unordered_map>\n#include<unordered_set>\n#include<random>\n#pragma warning(disable:4996)\nusing namespace std;\nusing ld=long double;\nint main() {\n\tint T; ld E;cin>>T>>E;\n\tvector<int>cs(2*T+1);\n\tfor (int i = 0; i < 2 * T + 1; ++i) {\n\t\tcin>>cs[i];\n\t}\n\tld ans=0;\n\tans=E/cs[T];\n\tint l=T-1,r=T-1;\n\t{\n\t\tld sum=0;\n\t\tld pre=1e18;\n\t\tfor (int i = T + 1; i < 2 * T + 1; ++i) {\n\t\t\tld now=sum+(2*T+1-i)*cs[i];\n\t\t\tif (now < pre) {\n\t\t\t\tr=i;\n\t\t\t\tpre=now;\n\t\t\t}\n\t\t\tsum+=cs[i];\n\t\t}\n\t}\n\t{\n\t\tld sum = 0;\n\t\tld pre = 1e18;\n\t\tfor (int i = T - 1; i >=0; --i) {\n\t\t\tld now = sum + (i+1)*cs[i];\n\t\t\tif (now < pre) {\n\t\t\t\tl = i;\n\t\t\t\tpre = now;\n\t\t\t}\n\t\t\tsum += cs[i];\n\t\t}\n\t}\n\tld sum=0;\n\tfor (int i = l; i <= r; ++i) {\n\t\tif(i==l)sum+=(i+1)*cs[i];\n\t\telse if(i==r)sum+=(2*T+1-i)*cs[i];\n\t\telse sum+=cs[i];\n\t}\n\tans=max(ans,E/sum*(T+1));\n\tcout<<setprecision(20)<<fixed<<ans<<endl;\n\treturn 0;\n}", "accuracy": 0.4523809523809524, "time_ms": 40, "memory_kb": 3656, "score_of_the_acc": -0.948, "final_rank": 13 }, { "submission_id": "aoj_2278_2656088", "code_snippet": "#include \"bits/stdc++.h\"\n#include<unordered_map>\n#include<unordered_set>\n#include<random>\n#pragma warning(disable:4996)\nusing namespace std;\nusing ld=long double;\nint main() {\n\tint T; ld E;cin>>T>>E;\n\tvector<int>cs(2*T+1);\n\tfor (int i = 0; i < 2 * T + 1; ++i) {\n\t\tcin>>cs[i];\n\t}\n\tld ans=0;\n\tans=E/cs[T];\n\tint l=T-1,r=T-1;\n\t{\n\t\tld sum=0;\n\t\tld pre=1e18;\n\t\tfor (int i = T + 1; i < 2 * T + 1; ++i) {\n\t\t\tld now=sum+(2*T+1-i)*cs[i];\n\t\t\tif (now < pre) {\n\t\t\t\tr=i;\n\t\t\t\tpre=now;\n\t\t\t}\n\t\t\tsum+=cs[i];\n\t\t}\n\t}\n\t{\n\t\tld sum = 0;\n\t\tld pre = 1e18;\n\t\tfor (int i = T - 1; i >=0; --i) {\n\t\t\tld now = sum + (i+1)*cs[i];\n\t\t\tif (now < pre) {\n\t\t\t\tl = i;\n\t\t\t\tpre = now;\n\t\t\t}\n\t\t\tsum += cs[i];\n\t\t}\n\t}\n\tld sum=0;\n\tfor (int i = l; i <= r; ++i) {\n\t\tif(i==l)sum+=(i+1)*cs[i];\n\t\telse if(i==r)sum+=(2*T+1-i)*cs[i];\n\t\telse sum+=cs[i];\n\t}\n\tans=max(ans,E/sum*(T+1));\n\tcout<<setprecision(10)<<fixed<<ans<<endl;\n\treturn 0;\n}", "accuracy": 0.4523809523809524, "time_ms": 40, "memory_kb": 3552, "score_of_the_acc": -0.9262, "final_rank": 12 }, { "submission_id": "aoj_2278_1425779", "code_snippet": "#include <cstdlib>\n#include <cmath>\n#include <climits>\n#include <cfloat>\n#include <map>\n#include <set>\n#include <iostream>\n#include <string>\n#include <vector>\n#include <algorithm>\n#include <sstream>\n#include <complex>\n#include <stack>\n#include <queue>\n#include <cstdio>\n#include <cstring>\n#include <iterator>\n#include <bitset>\n#include <unordered_set>\n#include <unordered_map>\n#include <fstream>\n#include <iomanip>\n//#include <utility>\n//#include <memory>\n//#include <functional>\n//#include <deque>\n//#include <cctype>\n//#include <ctime>\n//#include <numeric>\n//#include <list>\n//#include <iomanip>\n\n//#if __cplusplus >= 201103L\n//#include <array>\n//#include <tuple>\n//#include <initializer_list>\n//#include <forward_list>\n//\n//#define cauto const auto&\n//#else\n\n//#endif\n\nusing namespace std;\n\n\ntypedef long long ll;\ntypedef pair<int,int> pii;\ntypedef pair<ll,ll> pll;\n\ntypedef vector<int> vint;\ntypedef vector<vector<int> > vvint;\ntypedef vector<long long> vll, vLL;\ntypedef vector<vector<long long> > vvll, vvLL;\n\n#define VV(T) vector<vector< T > >\n\ntemplate <class T>\nvoid initvv(vector<vector<T> > &v, int a, int b, const T &t = T()){\n v.assign(a, vector<T>(b, t));\n}\n\ntemplate <class F, class T>\nvoid convert(const F &f, T &t){\n stringstream ss;\n ss << f;\n ss >> t;\n}\n\n#undef _P\n#define _P(...) (void)printf(__VA_ARGS__)\n#define reep(i,a,b) for(int i=(a);i<(b);++i)\n#define rep(i,n) reep((i),0,(n))\n#define ALL(v) (v).begin(),(v).end()\n#define PB push_back\n#define F first\n#define S second\n#define mkp make_pair\n#define RALL(v) (v).rbegin(),(v).rend()\n#define DEBUG\n#ifdef DEBUG\n#define dump(x) cout << #x << \" = \" << (x) << endl;\n#define debug(x) cout << #x << \" = \" << (x) << \" (L\" << __LINE__ << \")\" << \" \" << __FILE__ << endl;\n#else\n#define dump(x) \n#define debug(x) \n#endif\n#define LDcout(x,n) fixed<<setprecision(n)<<x\n\n#define MOD 1000000007LL\n#define EPS 1e-8\nstatic const int INF=1<<24;\n\nvoid mainmain(){\n\tint T,E;\n\tcin>>T>>E;\n\tvector<double> v1(T),v2(T);\n\trep(i,T){\n\t\tcin>>v1[T-1-i];\n\t}\n\tdouble b;\n\tcin>>b;\n\trep(i,T){\n\t\tcin>>v2[i];\n\t}\n\tdouble a=v1[T-1];\n\tdouble c=v2[T-1];\n\tfor(int i=T-2;i>=0;i--){\n\t\tint s=T-i;\n\t\tif(v1[i]>=a){\n\t\t\ta=(v1[i]+a*(s-1))/(s);\n\t\t}\n\t\telse{\n\t\t\ta=v1[i];\n\t\t}\n\t\tif(v2[i]>=c){\n\t\t\tc=(v2[i]+c*(s-1))/(s);\n\t\t}\n\t\telse{\n\t\t\tc=v2[i];\n\t\t}\n\t}\n\tdouble ans1=max(E/b,(double)E*(T+1)/(T*(a+c)+b));\n\tcout<<LDcout(ans1,10)<<endl;\n}\n\n\nsigned main() {\n\tios_base::sync_with_stdio(false);\n \t// cout<<fixed<<setprecision(10);\n mainmain();\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 2844, "score_of_the_acc": -0.7778, "final_rank": 5 }, { "submission_id": "aoj_2278_1425777", "code_snippet": "#include <cstdlib>\n#include <cmath>\n#include <climits>\n#include <cfloat>\n#include <map>\n#include <set>\n#include <iostream>\n#include <string>\n#include <vector>\n#include <algorithm>\n#include <sstream>\n#include <complex>\n#include <stack>\n#include <queue>\n#include <cstdio>\n#include <cstring>\n#include <iterator>\n#include <bitset>\n#include <unordered_set>\n#include <unordered_map>\n#include <fstream>\n#include <iomanip>\n//#include <utility>\n//#include <memory>\n//#include <functional>\n//#include <deque>\n//#include <cctype>\n//#include <ctime>\n//#include <numeric>\n//#include <list>\n//#include <iomanip>\n\n//#if __cplusplus >= 201103L\n//#include <array>\n//#include <tuple>\n//#include <initializer_list>\n//#include <forward_list>\n//\n//#define cauto const auto&\n//#else\n\n//#endif\n\nusing namespace std;\n\n\ntypedef long long ll;\ntypedef pair<int,int> pii;\ntypedef pair<ll,ll> pll;\n\ntypedef vector<int> vint;\ntypedef vector<vector<int> > vvint;\ntypedef vector<long long> vll, vLL;\ntypedef vector<vector<long long> > vvll, vvLL;\n\n#define VV(T) vector<vector< T > >\n\ntemplate <class T>\nvoid initvv(vector<vector<T> > &v, int a, int b, const T &t = T()){\n v.assign(a, vector<T>(b, t));\n}\n\ntemplate <class F, class T>\nvoid convert(const F &f, T &t){\n stringstream ss;\n ss << f;\n ss >> t;\n}\n\n#undef _P\n#define _P(...) (void)printf(__VA_ARGS__)\n#define reep(i,a,b) for(int i=(a);i<(b);++i)\n#define rep(i,n) reep((i),0,(n))\n#define ALL(v) (v).begin(),(v).end()\n#define PB push_back\n#define F first\n#define S second\n#define mkp make_pair\n#define RALL(v) (v).rbegin(),(v).rend()\n#define DEBUG\n#ifdef DEBUG\n#define dump(x) cout << #x << \" = \" << (x) << endl;\n#define debug(x) cout << #x << \" = \" << (x) << \" (L\" << __LINE__ << \")\" << \" \" << __FILE__ << endl;\n#else\n#define dump(x) \n#define debug(x) \n#endif\n#define LDcout(x,n) fixed<<setprecision(n)<<x\n\n#define MOD 1000000007LL\n#define EPS 1e-8\nstatic const int INF=1<<24;\n\nvoid mainmain(){\n\tint T,E;\n\tcin>>T>>E;\n\tvector<double> v1(T),v2(T);\n\trep(i,T){\n\t\tcin>>v1[T-1-i];\n\t}\n\tdouble b;\n\tcin>>b;\n\trep(i,T){\n\t\tcin>>v2[i];\n\t}\n\tdouble a=v1[T-1];\n\tdouble c=v2[T-1];\n\tfor(int i=T-2;i>=0;i--){\n\t\tint s=T-i;\n\t\t// cout<<v1[i]<<endl;\n\t\t// cout<<s<<endl;\n\t\tif(v1[i]>=a){\n\t\t\ta=(v1[i]+a*(s-1))/(s);\n\t\t}\n\t\telse{\n\t\t\ta=v1[i];\n\t\t}\n\t\tif(v2[i]>=c){\n\t\t\tc=(v2[i]+c*(s-1))/(s);\n\t\t}\n\t\telse{\n\t\t\tc=v2[i];\n\t\t}\n\t}\n\t// cout<<a<<\" \"<<b<<\" \"<<c<<endl;\n\tdouble l=0,r=E;\n\twhile(r-l>EPS){\n\t\tdouble mid1=l+(r-l)/3;\n\t\tdouble mid2=l+(r-l)*2/3;\n\t\tdouble h1=(E-(a+c)*mid1)/b;\n\t\tdouble h2=(E-(a+c)*mid2)/b;\n\t\tdouble ans1=min(mid1+h1,T*h1);\n\t\tdouble ans2=min(mid2+h2,T*h2);\n\t\tif(ans1<ans2){\n\t\t\tl=mid1;\n\t\t}\n\t\telse{\n\t\t\tr=mid2;\n\t\t}\n\t}\n\t// cout<<a<<\" \"<<c<<endl;\n\t// cout<<E*(T+1)<<endl;\n\tdouble ans1=max(E/b,(double)E*(T+1)/(T*(a+c)+b));\n\t// cout<<l+h1<<\" \"<<T*h1<<endl;\n\tcout<<ans1<<endl;\n}\n\n\nsigned main() {\n\tios_base::sync_with_stdio(false);\n \tcout<<fixed<<setprecision(10);\n mainmain();\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 2844, "score_of_the_acc": -0.7778, "final_rank": 5 }, { "submission_id": "aoj_2278_1425768", "code_snippet": "#include <cstdlib>\n#include <cmath>\n#include <climits>\n#include <cfloat>\n#include <map>\n#include <set>\n#include <iostream>\n#include <string>\n#include <vector>\n#include <algorithm>\n#include <sstream>\n#include <complex>\n#include <stack>\n#include <queue>\n#include <cstdio>\n#include <cstring>\n#include <iterator>\n#include <bitset>\n#include <unordered_set>\n#include <unordered_map>\n#include <fstream>\n#include <iomanip>\n//#include <utility>\n//#include <memory>\n//#include <functional>\n//#include <deque>\n//#include <cctype>\n//#include <ctime>\n//#include <numeric>\n//#include <list>\n//#include <iomanip>\n\n//#if __cplusplus >= 201103L\n//#include <array>\n//#include <tuple>\n//#include <initializer_list>\n//#include <forward_list>\n//\n//#define cauto const auto&\n//#else\n\n//#endif\n\nusing namespace std;\n\n\ntypedef long long ll;\ntypedef pair<int,int> pii;\ntypedef pair<ll,ll> pll;\n\ntypedef vector<int> vint;\ntypedef vector<vector<int> > vvint;\ntypedef vector<long long> vll, vLL;\ntypedef vector<vector<long long> > vvll, vvLL;\n\n#define VV(T) vector<vector< T > >\n\ntemplate <class T>\nvoid initvv(vector<vector<T> > &v, int a, int b, const T &t = T()){\n v.assign(a, vector<T>(b, t));\n}\n\ntemplate <class F, class T>\nvoid convert(const F &f, T &t){\n stringstream ss;\n ss << f;\n ss >> t;\n}\n\n#undef _P\n#define _P(...) (void)printf(__VA_ARGS__)\n#define reep(i,a,b) for(int i=(a);i<(b);++i)\n#define rep(i,n) reep((i),0,(n))\n#define ALL(v) (v).begin(),(v).end()\n#define PB push_back\n#define F first\n#define S second\n#define mkp make_pair\n#define RALL(v) (v).rbegin(),(v).rend()\n#define DEBUG\n#ifdef DEBUG\n#define dump(x) cout << #x << \" = \" << (x) << endl;\n#define debug(x) cout << #x << \" = \" << (x) << \" (L\" << __LINE__ << \")\" << \" \" << __FILE__ << endl;\n#else\n#define dump(x) \n#define debug(x) \n#endif\n#define LDcout(x,n) fixed<<setprecision(n)<<x\n\n#define MOD 1000000007LL\n#define EPS 1e-8\nstatic const int INF=1<<24;\n\nvoid mainmain(){\n\tint T,E;\n\tcin>>T>>E;\n\tvector<double> v1(T),v2(T);\n\trep(i,T){\n\t\tcin>>v1[T-1-i];\n\t}\n\tdouble b;\n\tcin>>b;\n\trep(i,T){\n\t\tcin>>v2[i];\n\t}\n\tdouble a=v1[T-1];\n\tdouble c=v2[T-1];\n\tfor(int i=T-2;i>=0;i--){\n\t\tint s=T-i;\n\t\t// cout<<v1[i]<<endl;\n\t\t// cout<<s<<endl;\n\t\tif(v1[i]>=a){\n\t\t\ta=(v1[i]+a*(s-1))/(s);\n\t\t}\n\t\telse{\n\t\t\ta=v1[i];\n\t\t}\n\t\tif(v2[i]>=c){\n\t\t\tc=(v2[i]+c*(s-1))/(s);\n\t\t}\n\t\telse{\n\t\t\tc=v2[i];\n\t\t}\n\t}\n\t// cout<<a<<\" \"<<b<<\" \"<<c<<endl;\n\tdouble l=0,r=E;\n\twhile(r-l>EPS){\n\t\tdouble mid1=l+(r-l)/3;\n\t\tdouble mid2=l+(r-l)*2/3;\n\t\tdouble h1=(E-(a+c)*mid1)/b;\n\t\tdouble h2=(E-(a+c)*mid2)/b;\n\t\tdouble ans1=min(mid1+h1,T*h1);\n\t\tdouble ans2=min(mid2+h2,T*h2);\n\t\tif(ans1<ans2){\n\t\t\tl=mid1;\n\t\t}\n\t\telse{\n\t\t\tr=mid2;\n\t\t}\n\t}\n\t// cout<<a<<\" \"<<c<<endl;\n\tdouble ans1=max(E/b,E*(T+1)/(T*(a+c)+b));\n\t// cout<<l+h1<<\" \"<<T*h1<<endl;\n\tcout<<ans1<<endl;\n}\n\n\nsigned main() {\n\tios_base::sync_with_stdio(false);\n \tcout<<fixed<<setprecision(10);\n mainmain();\n}", "accuracy": 0.47619047619047616, "time_ms": 40, "memory_kb": 2844, "score_of_the_acc": -0.7778, "final_rank": 11 }, { "submission_id": "aoj_2278_728148", "code_snippet": "#include <cstdio>\n\nusing namespace std;\n\nint main()\n{\n\tfor(int t,e;~scanf(\"%d%d\",&t,&e);){\n\t\tdouble c[2*t+1];\n\t\tfor(int i=0;i<2*t+1;i++)\n\t\t\tscanf(\"%lf\",c+i);\n\t\tfor(int s=2;s<=t;s++){\n\t\t\tif(c[t+(t-s+1)]>c[t+(t-s+2)])\n\t\t\t\tc[t+(t-s+1)]=(c[t+(t-s+1)]+(s-1)*c[t+(t-s+2)])/s;\n\t\t\tif(c[t-(t-s+1)]>c[t-(t-s+2)])\n\t\t\t\tc[t-(t-s+1)]=(c[t-(t-s+1)]+(s-1)*c[t-(t-s+2)])/s;\n\t\t}\n\t\tif(c[t]>c[t-1]+c[t+1])\n\t\t\tc[t]=(c[t]+t*(c[t-1]+c[t+1]))/(t+1);\n\t\tprintf(\"%.13f\\n\",e/c[t]);\n\t}\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 2640, "score_of_the_acc": -0.735, "final_rank": 4 }, { "submission_id": "aoj_2278_427595", "code_snippet": "#include<cstdio>\n#include<algorithm>\n\n#define rep(i,n) for(int i=0;i<(n);i++)\n\nusing namespace std;\n\nint T,E;\ndouble *C,C_buf[200001];\n\ndouble merge(){\n\tdouble res=C[T];\n\tfor(int i=T-1;i>=1;i--){\n\t\t// プレート i と i+1 をマージし、それを新たにプレート i と思う\n\t\t// そうすると、新たなプレート i+1 にはエネルギーをかけないと考えていい\n\t\tres=min(C[i],(C[i]+(T-i)*res)/(T-i+1));\n\t\t// min の第 1 項は ( マージする前の ) プレート i+1 にエネルギーをかけないケース (?)\n\t\t// min の第 2 項は ( マージする前の ) プレート i+1 にエネルギーをかけるケース\n\t}\n\treturn res;\n}\n\nint main(){\n\tscanf(\"%d%d\",&T,&E);\n\tC=C_buf+100000;\n\tfor(int i=-T;i<=T;i++) scanf(\"%lf\",C+i);\n\n\tdouble C0=C[0];\n\tdouble Cr=merge(); // 右 T 枚のプレートをマージしてできたプレートの比熱\n\treverse(C-T,C+T+1);\n\tdouble Cl=merge(); // 左 T 枚のプレートをマージしてできたプレートの比熱\n\n\t// これで 3 枚のプレート -1, 0, 1 の問題になった\n\t// 解は\n\t// - プレート 0 で T+1 回熱せられる\n\t// - プレート 0 で 1 回、プレート 1 で 1 回熱せられる\n\t// - プレート 0 で 1 回、プレート -1 で 1 回熱せられる\n\t// のいずれか\n\n\t// プレートにかける熱をそれぞれ Hl, H0, Hr とする\n\t// 制約 Cl*Hl+C0*H0+Cr*Hr = E のもと、min{(T+1)*H0, H0+Hl, H0+Hr} を最大化する問題になる\n\t// Hl = Hr とするのがいいことは直感的にわかるので、\n\t// 制約 C0*H0+(Cl+Cr)*Hl = E のもと、min{(T+1)*H0, H0+Hl} を最大化すればいい\n\tdouble ans=0,H0,Hl;\n\tH0=E/C0; Hl=(E-C0*H0)/(Cl+Cr); ans=max(ans,min((T+1)*H0,H0+Hl));\n\tH0=E/(C0+T*(Cl+Cr)); Hl=(E-C0*H0)/(Cl+Cr); ans=max(ans,min((T+1)*H0,H0+Hl));\n\tprintf(\"%.9f\\n\",ans);\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 2220, "score_of_the_acc": -0.647, "final_rank": 2 }, { "submission_id": "aoj_2278_301320", "code_snippet": "#include <iostream>\n#include <cstdio>\n#include <algorithm>\n#define REP(i,n) for(int i=0; i<(int)(n); i++)\n\ninline int getInt(){ int s; scanf(\"%d\", &s); return s; }\n\nusing namespace std;\n\ndouble buff[200200];\ndouble *c;\nint t;\nint e;\n\n\nint main(){\n t = getInt();\n e = getInt();\n\n REP(i, 2 * t + 1) buff[i] = (double)getInt();\n c = &buff[t];\n\n for(int s = 2; s <= t; s++){\n {\n // merge to left\n double alpha = c[t - s + 1];\n double beta = c[t - s + 2];\n if(alpha > beta){\n\tc[t - s + 1] = (alpha + beta * (s - 1)) / s;\n }\n }\n {\n // merge to right\n double alpha = c[-(t - s + 1)];\n double beta = c[-(t - s + 2)];\n if(alpha > beta){\n\tc[-(t - s + 1)] = (alpha + beta * (s - 1)) / s;\n }\n }\n }\n \n double alpha = c[-1];\n double beta = c[0];\n double ganma = c[1];\n\n double ans;\n\n double h1 = e / (alpha + ganma + beta / t);\n double h2 = (e - (alpha + ganma) * h1) / beta;\n ans = h1 + h2;\n\n ans = max(ans, e / beta);\n\n printf(\"%.7f\\n\", ans);\n\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 2304, "score_of_the_acc": -0.6646, "final_rank": 3 }, { "submission_id": "aoj_2278_271449", "code_snippet": "#include <iostream>\n#include <sstream>\n#include <iomanip>\n#include <algorithm>\n#include <cmath>\n#include <string>\n#include <vector>\n#include <list>\n#include <queue>\n#include <stack>\n#include <set>\n#include <map>\n#include <bitset>\n#include <numeric>\n#include <climits>\n#include <cfloat>\nusing namespace std;\n\nint main()\n{\n int t, e;\n cin >> t >> e;\n\n vector<double> c(2*t+1);\n for(int i=0; i<2*t+1; ++i)\n cin >> c[i];\n\n // ツ熱ツつウT+1ツつセツつッツ嘉熱ツつキツづゥツつスツづ淞づ可必ツ要ツづ按催渉ャツエツネツδ仰ギツー\n double minLeft = 0.0;\n double minRight = 0.0;\n for(int i=0; i<t; ++i){\n minLeft = min(minLeft + c[i], c[i] * (i+1));\n minRight = min(minRight + c[2*t-i], c[2*t-i] * (i+1));\n }\n double minEnergy = min(minLeft + minRight + c[t], c[t] * (t+1));\n\n double ret = e / minEnergy * (t+1);\n printf(\"%.10f\\n\", ret);\n\n return 0;\n}", "accuracy": 1, "time_ms": 130, "memory_kb": 2484, "score_of_the_acc": -1.5205, "final_rank": 10 }, { "submission_id": "aoj_2278_269010", "code_snippet": "#include <stdio.h>\n#include <string.h>\n#include <algorithm>\n#include <iostream>\n#include <math.h>\n#include <assert.h>\n#include <vector>\n\nusing namespace std;\ntypedef long long ll;\ntypedef unsigned int uint;\ntypedef unsigned long long ull;\nstatic const double EPS = 1e-9;\nstatic const double PI = acos(-1.0);\n\n#define REP(i, n) for (int i = 0; i < (int)(n); i++)\n#define FOR(i, s, n) for (int i = (s); i < (int)(n); i++)\n#define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++)\n#define FORIT(it, c) for (__typeof((c).begin())it = (c).begin(); it != (c).end(); it++)\n#define MEMSET(v, h) memset((v), h, sizeof(v))\n\nint n;\ndouble E;\ndouble plate[300000];\n\ninline double Get(int index) {\n return plate[index + n];\n}\n\ninline double Merge(double x, double y, int w) {\n return min(y, (w * x + y) / (w + 1));\n}\n\ndouble MergeAll(int dir) {\n double left = Get(-dir * n);\n int w = 1;\n int index = -dir * n + dir;\n while (index != 0) {\n left = Merge(left, Get(index), w);\n //cout << index << \" \" << left << \" \" << w << endl;\n assert(left >= -EPS);\n w++;\n index += dir;\n }\n return left;\n}\n\nint main() {\n while (scanf(\"%d %lf\", &n, &E) > 0) {\n REP(i, 2 * n + 1) {\n scanf(\"%lf\", &plate[i]);\n }\n double left = MergeAll(1);\n double right = MergeAll(-1);\n double ans = max(E / Get(0), E * (n + 1) / (n * (left + right) + Get(0)));\n printf(\"%.8f\\n\", ans);\n }\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 0, "score_of_the_acc": -0.2727, "final_rank": 1 } ]
aoj_2282_cpp
Problem B: B問題 R大学の2D好きの人たち (2DRespecters)は、A津大学で開催されるプログラミングの練習合宿に参加する。 この練習合宿では、参加者たちが自作のプログラミング問題を持ち寄り、練習に用いる。 2DRespectersも、いくつかの問題を作成することになった。 しかし、合宿の3日前になっても、まだ、B問題が完成していなかった。 最初のB問題の担当者が作成した問題は、担当者が問題の制約を難しくし過ぎてしまったため、C問題以降にまわされてしまったのだ。 そして、簡単すぎても、難しすぎてもいけない、微妙な難易度の調整が必要とされるB問題は、誰もが手を付けたがらなかった。 それ以来、B問題担当者の椅子は空席のままである。 合宿3日前になって、遂に、この問題を解決するために、2DRespecters期待のホープが立ち上がった。 だが、彼は、B問題を作成する気がない。 彼は、自分でB問題を作成したくないので、B問題の作成担当を他者に押し付ける手法を考え出したのだ。 彼の手法は、表面上、B問題の作成担当者を平等に決定する。 彼の手法は、以下の通りである。 問題作成のために要した作業時間が最も少ない人がB問題担当者になる。 作業時間が最も少ない人が複数人居た場合は、より問題難易度の小さい問題の担当者がB問題作成担当者になる。 作業時間が最も少ない人が複数人居て、その中で、問題難易度の最も小さい人が複数人いた場合は、ホープの彼がB問題作成担当者になる。 彼の手法は、一見、最も仕事をしていない人がB問題の作成を担当するため、2DRespectersのメンバー全員に快く受け入れられた。 しかし、彼の手法には、以下のような裏がある。 各個人の作業時間は、各個人が口頭で申請するため、嘘の申請をすることが可能である。 問題の作業可能時間を超える作業時間や負の作業時間を申請すると、嘘だとばれる。 ある問題の作成のための作業時間は、必ず問題難易度の整数倍になることが知られているため、これに反する作業時間を申請した場合も、嘘だとばれる。 嘘の申請をした人の嘘がばれた場合、その人が必ずB問題作成担当者になる。 申請は一人ずつ順番に行い、嘘がばれる場合は、申請の時点でばれる。 一人でも嘘の申請がばれた時点で、B問題作成担当者が決まり、それ以降の申請は行わない。 申請者は、自分より前の申請のみを考慮し、申請の時点で自分がB問題作成担当者にならないような最小の作業時間を申請するものとする。 申請の時点で、申請者がB問題作成担当者にならないような報告ができない場合は、嘘だとばれないような最大の作業時間を申請する。 各個人の担当する問題難易度のリストが申請順に与えられたとき、誰がB問題作成担当者になるかを求めるプログラムを作成せよ。 ホープの彼の申請順番は、一番最後である。 なお、本問題はフィクションであり、本問題の作成の経緯とは一切の関係がない。 Input 入力は、複数のデータセットからなり、データセットの終わりは、半角スペースで区切られた0が二つだけを含む行で表される。 データセットの総数は40以下である。 データセットの1行目では、整数 n ( 2 ≤ n ≤ 100 1,000 )と整数 m ( 1 ≤ m ≤ 1,000 )が半角スペース区切りで与えられる。 データセットの2行目では、整数 a_1 , a_2 , ..., a_n ( 1 ≤ a_i ≤ 1,000 , 1 ≤ i ≤ n )が半角スペース区切りで与えられる。 1行目の n と m は、それぞれ、申請者の数と問題の作業可能時間を表す。 2行目では、申請順に並べられた問題難易度のリストが与えられており、 a_i は、問題の難易度を表す。 Output それぞれのデータセットごとに、B問題作成担当者の申請順番を1行で出力せよ。 Sample Input 3 100 3 7 4 5 24 4 6 12 3 8 5 1 1 1 2 2 3 0 0 Output for Sample Input 2 4 5
[ { "submission_id": "aoj_2282_1960392", "code_snippet": "#include <bits/stdc++.h>\n#define f first\n#define s second\nusing namespace std;\nint n,m,a[1001];\ntypedef pair <int,int> P;\ntypedef pair<P,int> PP;\n\nint solve(){\n PP t[1001];\n t[0]=PP(P((m/a[0])*a[0],a[0]),0);\n for(int i=1;i<n;i++){\n sort(t,t+i);\n P ima=t[0].f;\n int nx=((ima.f-1)/a[i])*a[i]+a[i];\n if(ima.f==nx&&ima.s>a[i]) nx+=a[i];\n if(m<nx)nx-=a[i];\n t[i]=PP(P(nx,a[i]),i);\n }\n sort(t,t+n);\n if(t[0].f==t[1].f)return n;\n return t[0].s+1;\n }\n\nint main(){\n while(1){\n cin>>n>>m;\n if(!n&&!m)break;\n for(int i=0;i<n;i++) cin>>a[i];\n cout<<solve()<<endl; \n }\n return 0;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 1180, "score_of_the_acc": -2, "final_rank": 3 }, { "submission_id": "aoj_2282_1896197", "code_snippet": "#include<bits/stdc++.h>\n#define range(i,a,b) for(int i = (a); i < (b); i++)\n#define rep(i,b) range(i,0,b)\n#define pb(a) push_back(a)\n#define all(a) (a).begin(), (a).end()\n#define debug(x) cout << \"debug \" << x << endl;\nusing namespace std;\n\nstruct info{\n int name, diffi, time;\n};\n\nvoid swap(struct info *a, struct info *b){\n struct info *temp = a;\n a = b;\n b = temp;\n}\n\nvoid sort(struct info info[1005], int n){\n rep(i,n - 1){\n rep(j,n - 1){\n struct info temp;\n if(info[j].time > info[j + 1].time){\n swap(info[j], info[j + 1]);\n }else if(info[j].time == info[j + 1].time && info[j].diffi > info[j + 1].diffi){\n swap(info[j], info[j + 1]);\n }\n }\n }\n}\n\n int main(){\n int n, m;\n while(cin >> n >> m, n||m){\n struct info info[1005];\n rep(i,n){\n int inp, maxTime;\n cin >> inp;\n for(maxTime = inp; maxTime <= m; maxTime+=inp);\n maxTime-=inp;\n info[i].name = i + 1;\n info[i].diffi = inp;\n info[i].time = maxTime;\n }\n sort(info, n);\n if(info[0].time == info[1].time && info[0].diffi == info[1].diffi){\n cout << n << endl;\n }else{\n cout << info[0].name << endl;\n }\n\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1168, "score_of_the_acc": -0.9898, "final_rank": 2 }, { "submission_id": "aoj_2282_368671", "code_snippet": "#include<iostream>\nusing namespace std;\n\nint main(){\n int n,m;\n int a[1010];\n int num[1010][1010];//num[time][hard]\n int time[1010];\n int per[1010];\n\n while(cin >> n >> m , n||m){\n for(int i=0;i<n;i++)cin >> a[i];\n\n for(int i=0;i<=1000;i++){\n time[i] = 0;\n for(int j=0;j<=1000;j++)num[i][j] = 0;\n }\n\n for(int i=0;i<n;i++){\n int j;\n for(j=a[i];j<=m;j+=a[i]){\n\tbool f = false;\n\tfor(int k=0;k<j;k++){\n\t if(time[k]){\n\t time[j]++;\n\t num[j][a[i]]++;\n\t per[i] = j;\n\t f = true;\n\t break;\n\t }\n\t}\n\tif(!f){\n\t for(int k=0;k<a[i];k++){\n\t if(num[j][k]){\n\t time[j]++;\n\t num[j][a[i]]++;\n\t per[i] = j;\n\t f = true;\n\t break;\n\t }\n\t }\n\t}\n\tif(!f){\n\t if(num[j][a[i]]){\n\t time[j]++;\n\t num[j][a[i]]++;\n\t per[i] = j;\n\t f = true;\n\t break;\n\t }\n\t}\n\tif(f)break;\n }\n if(j>m){\n\ttime[j-a[i]]++;\n\tnum[j-a[i]][a[i]]++;\n\tper[i] = j-a[i];\n }\n }\n int ans = 0;\n\n for(int i=0;i<n;i++)\n if(per[i]<per[ans])ans = i;\n\n for(int i=0;i<n;i++)\n if(per[i] == per[ans]){\n\tif(a[i]<a[ans])ans = i;\n }\n\n int cnt = 0;\n for(int i=0;i<n;i++)\n if(a[i]==a[ans])cnt++;\n if(cnt>1)cout << n << endl;\n else cout << ans+1 << endl;\n }\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 0, "score_of_the_acc": -0.5, "final_rank": 1 } ]
aoj_2280_cpp
問題 J Mod 3 Knights Out 問題文 ある日の夕方,コンテストの問題を解き終えた二人は今日の問題について話し合っていた. A「くそー,あのライツアウトの問題,mod 2 じゃなくて mod 3 とか mod 7 とかだったら解法分かったのにー!」 B「じゃあ,mod 3 で別の方法で解く問題を作ればいいんですね.分かりました!」 こうして次のような問題が誕生した. H × W のチェス盤がある.チェス盤の各マスには 0 から 2 の整数が書かれている.このチェス盤にナイトを置いていく.ただし,各マスには多くても 1 体のナイトしか置けない.各マスについて, (マスの数値+そこを攻撃するマスにいるナイトの数)=0 mod 3 が成り立つようなナイトの配置を 良い 配置と呼ぶ.攻撃するマスとはそのマスから縦方向に ±2 マスかつ横方向に ±1 マス,もしくは縦方向に ±1 マスかつ横方向に ±2 マスずれたマスのことである. 良いナイトの配置の数を求めよ.答えは大きくなる可能性があるので 1,000,000,007 で割った余りを答えよ. 入力形式 最初の行に H と W がスペース区切りで与えられる. 次の H 行には、チェス盤のマス目に書かれている数値として W 個の 0 , 1 , 2 のいずれかの整数がスペース区切りで与えられる. 出力形式 良いナイトの配置の数を 1,000,000,007 で割った余りを出力せよ. 制約 1 ≤ H ≤ 50 1 ≤ W ≤ 16 入出力例 入力例 1 5 5 0 2 0 2 0 2 0 0 0 2 0 0 0 0 0 2 0 0 0 2 0 2 0 2 0 出力例 1 5 入力例 2 3 3 2 2 2 2 0 2 2 2 2 出力例 2 8 入力例 3 7 7 2 2 2 2 2 2 2 2 1 1 2 1 1 2 2 0 1 0 1 0 2 2 2 0 2 0 2 2 2 0 1 0 1 0 2 2 1 1 2 1 1 2 2 2 2 2 2 2 2 出力例 3 96 入力例 4 7 3 2 2 2 1 0 1 0 1 0 1 1 1 0 1 0 1 0 1 2 2 2 出力例 4 8 入力例 5 6 6 0 2 0 1 0 2 2 0 1 2 2 2 0 2 2 0 0 0 2 0 2 0 2 0 0 2 2 1 0 2 0 0 0 2 0 2 出力例 5 1 入力例 6 16 16 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 出力例 6 1 謝辞 この問題は Tester と Writer が アジア地区予選の問題 に関して話し合ったのをきっかけとして作られた。
[ { "submission_id": "aoj_2280_10433362", "code_snippet": "// AOJ #2280 Mod 3 Knights Out\n// 2025.4.30\n\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nconst int MOD = 1000000007;\n\n#define gc() getchar_unlocked()\n\nint Cin() {\n\tint n = 0; int c = gc();\n\tdo n = 10*n + (c & 0xf), c = gc(); while (c >= '0');\n\treturn n;\n}\n\nconst int dr[8]={-2,-2,-1,-1,1,1,2,2};\nconst int dc[8]={-1,1,-2,2,-2,2,-1,1};\n\ninline int addmod(int a,int b){ a+=b; if(a>=MOD) a-=MOD; return a; }\ninline int mulmod(ll a,ll b){ return (int)(a*b%MOD); }\nll powmod(ll a,ll e){\n ll r=1;\n while(e){\n if(e&1) r=r*a%MOD;\n a=a*a%MOD;\n e>>=1;\n }\n return r;\n}\ninline int inv3(int x){\n if(x==1) return 1;\n if(x==2) return 2;\n return 0;\n}\n\nint H,W;\nvector<int> B;\nvector<vector<int>> eq_to_var, var_to_eq;\n\nll solve_comp(const vector<int>& vars, const vector<int>& eqs){\n int n = vars.size(), m = eqs.size();\n if(m==0) return powmod(2, n);\n if(n==0){\n for(int e: eqs) if(B[e]!=0) return 0;\n return 1;\n }\n unordered_map<int,int> vid;\n vid.reserve(n*2);\n for(int i=0;i<n;i++) vid[vars[i]] = i;\n vector<vector<int>> A(m, vector<int>(n+1,0));\n for(int i=0;i<m;i++){\n int eq = eqs[i];\n A[i][n] = B[eq];\n for(int v: eq_to_var[eq]){\n auto it = vid.find(v);\n if(it != vid.end()) A[i][it->second] = (A[i][it->second] + 1) % 3;\n }\n }\n vector<int> where(n,-1);\n int row=0;\n for(int col=0;col<n && row<m;col++){\n int sel = row;\n while(sel<m && A[sel][col]==0) sel++;\n if(sel==m) continue;\n swap(A[sel], A[row]);\n int inv = inv3(A[row][col]);\n for(int j=col;j<=n;j++) A[row][j] = A[row][j]*inv%3;\n for(int i=0;i<m;i++) if(i!=row && A[i][col]){\n int f = A[i][col];\n for(int j=col;j<=n;j++){\n A[i][j] = (A[i][j] - f * A[row][j]) %3;\n if(A[i][j]<0) A[i][j]+=3;\n }\n }\n where[col] = row++;\n }\n for(int i=row;i<m;i++) if(A[i][n]!=0) return 0;\n vector<int> free_cols;\n for(int col=0;col<n;col++) if(where[col]==-1) free_cols.push_back(col);\n int k = free_cols.size();\n vector<int> x0(n,0);\n for(int col=0;col<n;col++){\n if(where[col]!=-1) x0[col] = A[ where[col] ][n];\n }\n vector<vector<int>> basis(k, vector<int>(n,0));\n for(int f=0;f<k;f++){\n int fc = free_cols[f];\n basis[f][fc] = 1;\n for(int col=0;col<n;col++){\n int r = where[col];\n if(r>=0){\n int v = A[r][fc];\n if(v) basis[f][col] = (3 - v) % 3;\n }\n }\n }\n vector<int> f_last(n, -1);\n for(int f=0; f<k; f++){\n for(int j=0;j<n;j++) if(basis[f][j]!=0) f_last[j] = f;\n }\n ll cnt = 0;\n vector<int> cur = x0;\n function<void(int)> dfs = [&](int f){\n if(f==k){\n for(int j=0;j<n;j++) if(cur[j]==2) return;\n cnt = (cnt + 1) % MOD;\n return;\n }\n for(int a=0;a<3;a++){\n if(a){\n for(int j=0;j<n;j++) cur[j] = (cur[j] + basis[f][j]*a) % 3;\n }\n bool ok = true;\n for(int j=0;j<n;j++){\n if(cur[j]==2 && f_last[j] <= f){ ok = false; break; }\n }\n if(ok) dfs(f+1);\n if(a){\n for(int j=0;j<n;j++){\n cur[j] = (cur[j] - basis[f][j]*a) % 3;\n if(cur[j]<0) cur[j]+=3;\n }\n }\n }\n };\n dfs(0);\n return cnt;\n}\n\nint main(){\n H = Cin(), W = Cin();\n int N = H*W;\n B.resize(N);\n for(int i=0;i<H;i++) for(int j=0;j<W;j++){\n int v = Cin();\n B[i*W+j] = ((-v)%3+3)%3;\n }\n eq_to_var.assign(N,{});\n var_to_eq.assign(N,{});\n for(int i=0;i<H;i++){\n for(int j=0;j<W;j++){\n int eq = i*W+j;\n for(int d=0;d<8;d++){\n int ni=i+dr[d], nj=j+dc[d];\n if(ni<0||ni>=H||nj<0||nj>=W) continue;\n int var = ni*W+nj;\n eq_to_var[eq].push_back(var);\n var_to_eq[var].push_back(eq);\n }\n }\n }\n vector<int> comp(2*N, -1);\n int comps = 0;\n for(int u=0; u<2*N; u++){\n if(comp[u]!=-1) continue;\n queue<int>q;\n comp[u]=comps;\n q.push(u);\n while(!q.empty()){\n int x=q.front(); q.pop();\n if(x< N){\n for(int e: var_to_eq[x]){\n int y = e + N;\n if(comp[y]==-1){\n comp[y]=comps;\n q.push(y);\n }\n }\n } else {\n int e = x - N;\n for(int v: eq_to_var[e]){\n if(comp[v]==-1){\n comp[v]=comps;\n q.push(v);\n }\n }\n }\n }\n comps++;\n }\n vector<vector<int>> cv(comps), ce(comps);\n for(int v=0; v<N; v++) cv[ comp[v] ].push_back(v);\n for(int e=0; e<N; e++) ce[ comp[e+N] ].push_back(e);\n\n ll ans = 1;\n for(int c=0; c<comps; c++){\n ll sub = solve_comp(cv[c], ce[c]);\n if(sub==0){\n cout << 0 << endl;\n return 0;\n }\n ans = ans * sub % MOD;\n }\n cout << ans << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 4248, "score_of_the_acc": -2, "final_rank": 4 }, { "submission_id": "aoj_2280_9586812", "code_snippet": "#include <bits/stdc++.h>\n\n#define int long long\nusing namespace std;\nconst int mod = 1e9 + 7;\nint h, w;\nvector<vector<int> > g;\nint delx[] = {-2, -1, 1, 2, -2, -1, 1, 2};\nint dely[] = {-1, -2, -2, -1, 1, 2, 2, 1};\n\nvoid add(int y, int x, int put) {\n for (int i = 0; i < 8; ++i) {\n int yy = y + dely[i];\n int xx = x + delx[i];\n if (xx >= 0 && xx < w && yy >= 0 && yy < h) {\n g[yy][xx] = (g[yy][xx] + put + 3) % 3;\n }\n }\n}\n\nbool check(int bw) {\n for (int i = h - 1; i >= max(0ll, h - 2); --i) {\n int st = ((i & 1) + 1 - bw) & 1;\n for (int j = st; j < w; j += 2) {\n if (g[i][j] != 0) {\n return false;\n }\n }\n }\n return true;\n}\n\nint rec(int y, int x, int bw) {\n if (y == h) {\n bool ok = check(bw);\n if (ok) {\n return 1;\n }\n return 0;\n }\n int nexty = y;\n int nextx = x + 2;\n int ret = 0;\n if (nextx >= w) {\n nexty = y + 1;\n nextx = ((nexty & 1) + bw) & 1;\n }\n bool put = true;\n bool notput = true;\n if (y > 1 && x > 0) {\n if (!g[y - 2][x - 1]) {\n put = false;\n } else if (g[y - 2][x - 1] == 1) {\n put = notput = false;\n } else {\n notput = false;\n }\n }\n if (y > 1 && x == w - 2) {\n if (!g[y - 2][x + 1]) {\n put = false;\n } else if (g[y - 2][x + 1] == 1) {\n put = notput = false;\n } else {\n notput = false;\n }\n }\n if (notput) {\n ret += rec(nexty, nextx, bw);\n }\n if (put) {\n add(y, x, 1);\n ret += rec(nexty, nextx, bw);\n add(y, x, -1);\n }\n return ret;\n}\n\nsigned main() {\n // ios_base::sync_with_stdio(0);\n // cin.tie(0);\n cin >> h >> w;\n g.resize(h, vector<int>(w));\n for (int i = 0; i < h; ++i) {\n for (int j = 0; j < w; ++j) {\n cin >> g[i][j];\n }\n }\n if (h < w) {\n vector<vector<int> > gnew(w, vector<int>(h));\n for (int i = 0; i < h; ++i) {\n for (int j = 0; j < w; ++j) {\n gnew[j][i] = g[i][j];\n }\n }\n g = gnew;\n swap(h, w);\n }\n if (w == 1) {\n bool e = true;\n for (int i = 0; i < h; ++i) {\n if (g[i][0]) {\n e = false;\n break;\n }\n }\n if (!e) {\n printf(\"%d\\n\", 0);\n } else {\n printf(\"%d\\n\", (1ll << h) % mod);\n }\n return 0;\n }\n int black = rec(0, 0, 0);\n int white = rec(0, 1, 1);\n int ans = white * black % mod;\n printf(\"%d\\n\", ans);\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3312, "score_of_the_acc": -0.7326, "final_rank": 2 }, { "submission_id": "aoj_2280_9586808", "code_snippet": "#include <set>\n#include <iostream>\n#include <cstdio>\n#include <cassert>\n#define REP(i,n) for(int i=0; i<(int)(n); i++)\n\ninline int getInt(){ int s; scanf(\"%d\", &s); return s; }\n\n#define IN(x,s,g) ((x) >= (s) && (x) < (g))\n#define ISIN(x,y,w,h) (IN((x),0,(w)) && IN((y),0,(h)))\n\nusing namespace std;\n\nconst int mod = 1000000007;\n\nint h, w;\nint g[50][16];\nint debug[50][16];\n\nint dx[] = {-2, -1, 1, 2, -2, -1, 1, 2 };\nint dy[] = {-1, -2, -2, -1, 1, 2, 2, 1 };\n\nvoid debug_print(){\n REP(i, h){\n REP(j, w) printf(\"%d \", debug[i][j]);\n printf(\" \");\n REP(j, w) printf(\"%d \", g[i][j]);\n puts(\"\");\n }\n}\n\nvoid process(int y, int x, bool put){\n int p = put ? 1 : 2;\n\n // debug[y][x] = put ? 1 : 0;\n\n REP(i,8){\n int yy = y + dy[i];\n int xx = x + dx[i];\n\n if(ISIN(xx, yy, w, h)){\n g[yy][xx] = (g[yy][xx] + p) % 3;\n }\n }\n}\n\nbool check(int bw){\n REP(i,2) if(h - 1 - i >= 0){\n int st = ((h - 1 - i) % 2 + (bw ^ 1)) % 2;\n for(int j = st; j < w; j += 2){\n if(g[h - 1 - i][j] != 0) return false;\n }\n }\n return true;\n}\n\nint solve(int y, int x, int bw){\n if(y == h){\n bool ok = check(bw);\n // printf(\"debug(%d): %d\\n\", bw, ok); debug_print(); puts(\"\");\n return ok ? 1 : 0;\n }\n\n assert((x + y) % 2 == bw);\n\n int yy = y;\n int xx = x + 2;\n\n int ret = 0;\n\n if(xx >= w){\n yy = y + 1;\n xx = (yy % 2 + bw) % 2;\n }\n\n bool put = true;\n bool notput = true;\n\n if(y > 1 && x > 0){\n if(g[y-2][x-1] == 0){\n put = false;\n }else if(g[y-2][x-1] == 1){\n put = notput = false;\n }else{\n notput = false;\n }\n }\n\n if(y > 1 && x == w - 2){\n if(g[y-2][x+1] == 0){\n put = false;\n }else if(g[y-2][x+1] == 1){\n put = notput = false;\n }else{\n notput = false;\n }\n }\n\n if(notput){\n ret += solve(yy, xx, bw); ret %= mod;\n }\n if(put){\n process(y, x, true);\n ret += solve(yy, xx, bw); ret %= mod;\n process(y, x, false);\n }\n\n return ret;\n}\n\nint main(){\n h = getInt();\n w = getInt();\n\n REP(i,h) REP(j,w)\n g[i][j] = getInt();\n\n if(h < w){\n REP(i,w) REP(j,i)\n swap(g[i][j], g[j][i]);\n swap(h, w);\n }\n\n\n if(w == 1){\n bool ok = true;\n REP(i,h) if(g[i][0] != 0) ok = false;\n if(ok){\n int ans = 1;\n REP(i,h) ans = (ans * 2) % mod;\n printf(\"%d\\n\", ans);\n }else{\n puts(\"0\");\n }\n return 0;\n }\n\n int black = solve(0, 0, 0);\n int white = (w == 1 ? solve(1, 0, 1) : solve(0, 1, 1));\n int ans = (((long long)white) * black) % mod;\n\n printf(\"%d\\n\", ans);\n\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3240, "score_of_the_acc": -0.712, "final_rank": 1 }, { "submission_id": "aoj_2280_416341", "code_snippet": "#include<cstdio>\n\n#define rep(i,n) for(int i=0;i<(n);i++)\n\nusing namespace std;\n\ntypedef long long ll;\n\nconst ll M=1000000007;\nconst int dx[]={2,1,-1,-2,-2,-1,1,2},dy[]={-1,-2,-2,-1,1,2,2,1};\n\nint h,w,B[50][16];\n\nint cnt[50][16]; // 攻撃されうる回数\n\nll dfs(int i,int j,int parity){\n\tif(i==h) return 1;\n\tif(j==w) return dfs(i+1,0,parity);\n\n\tif((i+j)%2!=parity) return dfs(i,j+1,parity); // 黒マス or 白マスのみ見る\n\n\t// マス (i, j) での値を mod 3 で 0 にできなければ探索打ち切り ( (i, j) を攻撃するマスがないかもしれないので必要 )\n\tif(cnt[i][j]==0 && B[i][j]%3!=0\n\t|| cnt[i][j]==1 && B[i][j]%3==1) return 0;\n\n\tll res=0;\n\t// マス (i, j) にナイトを置かない\n\trep(k,8){\n\t\tint y=i+dy[k],x=j+dx[k];\n\t\tif(0<=y && y<h && 0<=x && x<w) cnt[y][x]--;\n\t}\n\tbool ok=true;\n\trep(k,8){\n\t\tint y=i+dy[k],x=j+dx[k];\n\t\tif(0<=y && y<h && 0<=x && x<w){\n\t\t\tif(cnt[y][x]==0 && B[y][x]%3!=0\n\t\t\t|| cnt[y][x]==1 && B[y][x]%3==1) ok=false;\n\t\t}\n\t}\n\tif(ok) res+=dfs(i,j+1,parity);\n\n\t// マス (i, j) にナイトを置く\n\trep(k,8){\n\t\tint y=i+dy[k],x=j+dx[k];\n\t\tif(0<=y && y<h && 0<=x && x<w) B[y][x]++;\n\t}\n\tok=true;\n\trep(k,8){\n\t\tint y=i+dy[k],x=j+dx[k];\n\t\tif(0<=y && y<h && 0<=x && x<w){\n\t\t\tif(cnt[y][x]==0 && B[y][x]%3!=0\n\t\t\t|| cnt[y][x]==1 && B[y][x]%3==1) ok=false;\n\t\t}\n\t}\n\tif(ok) res+=dfs(i,j+1,parity);\n\n\trep(k,8){\n\t\tint y=i+dy[k],x=j+dx[k];\n\t\tif(0<=y && y<h && 0<=x && x<w) cnt[y][x]++, B[y][x]--;\n\t}\n\n\treturn res;\n}\n\nint main(){\n\tscanf(\"%d%d\",&h,&w);\n\trep(i,h) rep(j,w) scanf(\"%d\",B[i]+j);\n\n\trep(i,h) rep(j,w) rep(k,8) {\n\t\tint y=i+dy[k],x=j+dx[k];\n\t\tif(0<=y && y<h && 0<=x && x<w) cnt[y][x]++;\n\t}\n\n\tll ans;\n\tif(h==1 || w==1){ // コーナーケース\n\t\tbool ok=true;\n\t\trep(i,h) rep(j,w) if(B[i][j]!=0) ok=false;\n\t\tif(ok){\n\t\t\tans=1;\n\t\t\trep(i,h*w) ans=2*ans%M;\n\t\t}\n\t\telse ans=0;\n\t}\n\telse{\n\t\tans=dfs(0,0,0)*dfs(0,1,1)%M;\n\t}\n\tprintf(\"%lld\\n\",ans);\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 748, "score_of_the_acc": -1, "final_rank": 3 }, { "submission_id": "aoj_2280_416337", "code_snippet": "#include<cstdio>\n\n#define rep(i,n) for(int i=0;i<(n);i++)\n\nusing namespace std;\n\ntypedef long long ll;\n\nconst ll M=1000000007;\nconst int dx[]={2,1,-1,-2,-2,-1,1,2},dy[]={-1,-2,-2,-1,1,2,2,1};\n\nint h,w,B[50][16];\n\nint cnt[50][16]; // 攻撃されうる回数\n\nll dfs(int i,int j,int parity){\n\tif(i==h) return 1;\n\tif(j==w) return dfs(i+1,0,parity);\n\n\tif((i+j)%2!=parity) return dfs(i,j+1,parity); // 黒マス or 白マスのみ見る\n\n\tll res=0;\n\t// マス (i, j) にナイトを置く\n\trep(k,8){\n\t\tint y=i+dy[k],x=j+dx[k];\n\t\tif(0<=y && y<h && 0<=x && x<w) cnt[y][x]--;\n\t}\n\tbool ok=true;\n\trep(k,8){\n\t\tint y=i+dy[k],x=j+dx[k];\n\t\tif(0<=y && y<h && 0<=x && x<w){\n\t\t\tif(cnt[y][x]==0 && B[y][x]%3!=0\n\t\t\t|| cnt[y][x]==1 && B[y][x]%3==1) ok=false;\n\t\t}\n\t}\n\tif(ok) res+=dfs(i,j+1,parity);\n\n\t// マス (i, j) にナイトを置かない\n\trep(k,8){\n\t\tint y=i+dy[k],x=j+dx[k];\n\t\tif(0<=y && y<h && 0<=x && x<w) B[y][x]++;\n\t}\n\tok=true;\n\trep(k,8){\n\t\tint y=i+dy[k],x=j+dx[k];\n\t\tif(0<=y && y<h && 0<=x && x<w){\n\t\t\tif(cnt[y][x]==0 && B[y][x]%3!=0\n\t\t\t|| cnt[y][x]==1 && B[y][x]%3==1) ok=false;\n\t\t}\n\t}\n\tif(ok) res+=dfs(i,j+1,parity);\n\n\trep(k,8){\n\t\tint y=i+dy[k],x=j+dx[k];\n\t\tif(0<=y && y<h && 0<=x && x<w) cnt[y][x]++, B[y][x]--;\n\t}\n\n\treturn res;\n}\n\nint main(){\n\tscanf(\"%d%d\",&h,&w);\n\trep(i,h) rep(j,w) scanf(\"%d\",B[i]+j);\n\n\trep(i,h) rep(j,w) cnt[i][j]=0;\n\trep(i,h) rep(j,w) rep(k,8) {\n\t\tint y=i+dy[k],x=j+dx[k];\n\t\tif(0<=y && y<h && 0<=x && x<w) cnt[y][x]++;\n\t}\n\n\tll ans;\n\tif(h==1 || w==1){\n\t\tans=1;\n\t\trep(i,h*w) ans=2*ans%M;\n\t}\n\telse ans=dfs(0,0,0)*dfs(0,1,1)%M;\n\tprintf(\"%lld\\n\",ans);\n\n\treturn 0;\n}", "accuracy": 0.21875, "time_ms": 60, "memory_kb": 748, "score_of_the_acc": -0.5556, "final_rank": 5 } ]
aoj_2275_cpp
問題 E Fox Number 問題文 きつねのしえるは数字について考えるのが好きである.ある日,しえるはこの世にねこの数字というものがあることを知り,自分もそのような数字が欲しくなってしまった.そこで,次の性質を満たす数字をとりあえず Fox Number と呼ぶことにした. [性質]: 整数 N は k (≥ 1) 個の素数 p 1 , ..., p k と正整数 e 1 , ..., e k で, p 1 < ... < p k , e 1 ≥ ... ≥ e k を満たすものによって N = p 1 e 1 × ... × p k e k と書けるとき,Fox Number であると呼ぶことにする. ところでしえるはこの性質をその場の思いつきで決めてしまったのでこれがどういう性質を持っているのか全くわかっていない.そこで,この数字の性質を調べるために区間 [A-B, A+B] に含まれる Fox Number の個数を出力して欲しい. 入力形式 1 行目に以下の 2 つの整数が与えられる. A B 出力形式 1 行目に, A-B 以上 A+B 以下の Fox Number の個数を出力せよ. 制約 1 ≤ A ≤ 10 12 0 ≤ B ≤ 5 × 10 5 入出力例 入力例 1 18 2 出力例 1 4 16 = 2 4 , 17 = 17 1 , 19 = 19 1 , 20 = 2 2 × 5 1 は Fox Number である.一方で 18 = 2 × 3 2 は Fox Number ではない.合計して 4 つの Fox Number がある. 入力例 2 100 10 出力例 2 18 入力例 3 123456 789 出力例 3 1464
[ { "submission_id": "aoj_2275_4301948", "code_snippet": "#include <iostream>\n#include <cmath>\n#include <numeric>\n#include <algorithm>\n#include <deque>\n#include <vector>\n#include <unordered_map>\n#include <unordered_set>\n#include <iomanip>\n#include <map>\n#include <stack>\n#include <queue>\n#include <functional>\n#include <climits>\n#include <numeric>\n#include <bitset>\n#include <random>\n#include <tuple>\n#include <initializer_list>\n#include <fstream>\nstd::vector<int> cal_primes() {\n\tstd::vector<bool> is_prime(1000001, true);\n\tfor (auto i = 2; i * i < is_prime.size(); ++i) {\n\t\tif (is_prime[i]) {\n\t\t\tfor (auto j = i * i; j < is_prime.size(); j += i) {\n\t\t\t\tis_prime[j] = false;\n\t\t\t}\n\t\t}\n\t}\n\tstd::vector<int> result;\n\tfor (auto i = 2; i < is_prime.size(); ++i) {\n\t\tif (is_prime[i]) result.push_back(i);\n\t}\n\treturn result;\n}\nint main() {\n\tconst auto prime = cal_primes();\n\tlong long int a; int b; std::cin >> a >> b;\n\tconst auto min = std::max(2LL, a - b);\n\tconst auto max = a + b;\n\tstd::vector<int> last_exp(max - min + 1, INT_MAX);\n\tfor (const auto p : prime) {\n\t\tint e = 0;\n\t\tlong long int mul = 1;\n\t\twhile ((min + mul - 1) / mul * mul <= max) {\n\t\t\t++e;\n\t\t\tmul *= p;\n\t\t\tfor (auto i = (min + mul - 1) / mul; i * mul <= max; ++i) {\n\t\t\t\tif (i % p == 0) continue;\n\t\t\t\tif (last_exp[mul * i - min] >= e) last_exp[mul * i - min] = e;\n\t\t\t\telse last_exp[mul * i - min] = -1;\n\t\t\t}\n\t\t}\n\t}\n\tstd::cout << std::count_if(last_exp.begin(), last_exp.end(), [](const int e) {return e != -1; }) << std::endl;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 7448, "score_of_the_acc": -0.0237, "final_rank": 1 }, { "submission_id": "aoj_2275_4301937", "code_snippet": "#include <iostream>\n#include <cmath>\n#include <numeric>\n#include <algorithm>\n#include <deque>\n#include <vector>\n#include <unordered_map>\n#include <unordered_set>\n#include <iomanip>\n#include <map>\n#include <stack>\n#include <queue>\n#include <functional>\n#include <climits>\n#include <numeric>\n#include <bitset>\n#include <random>\n#include <tuple>\n#include <initializer_list>\n#include <fstream>\nstd::vector<int> cal_primes() {\n\tstd::vector<bool> is_prime(1000001, true);\n\tfor (auto i = 2; i * i < is_prime.size(); ++i) {\n\t\tif (is_prime[i]) {\n\t\t\tfor (auto j = i * i; j < is_prime.size(); j += i) {\n\t\t\t\tis_prime[j] = false;\n\t\t\t}\n\t\t}\n\t}\n\tstd::vector<int> result;\n\tfor (auto i = 2; i < is_prime.size(); ++i) {\n\t\tif (is_prime[i]) result.push_back(i);\n\t}\n\treturn result;\n}\nint main() {\n\tconst auto prime = cal_primes();\n\tlong long int a; int b; std::cin >> a >> b;\n\tstd::vector<int> last_exp(b * 2 + 1, INT_MAX);\n\tfor (const auto p : prime) {\n\t\tint e = 0;\n\t\tlong long int mul = 1;\n\t\twhile ((a - b + mul - 1) / mul * mul <= a + b) {\n\t\t\t++e;\n\t\t\tmul *= p;\n\t\t\tfor (auto i = (a - b + mul - 1) / mul; i * mul <= a + b; ++i) {\n\t\t\t\tif (i % p == 0) continue;\n\t\t\t\tif (last_exp[mul * i - (a - b)] >= e) last_exp[mul * i - (a - b)] = e;\n\t\t\t\telse last_exp[mul * i - (a - b)] = -1;\n\t\t\t}\n\t\t}\n\t}\n\tstd::cout << std::count_if(last_exp.begin(), last_exp.end(), [](const int e) {return e != -1; }) << std::endl;\n}", "accuracy": 0.027777777777777776, "time_ms": 30, "memory_kb": 7284, "score_of_the_acc": -0.0209, "final_rank": 14 }, { "submission_id": "aoj_2275_2669701", "code_snippet": "#include <stdio.h>\n#include <cmath>\n#include <algorithm>\n#include <cfloat>\n#include <stack>\n#include <queue>\n#include <vector>\n#include <string>\n#include <iostream>\n#include <set>\n#include <map>\n#include <time.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n#define NUM 1200000\n#define HUGE_NUM 99999999999999\n\nint main(){\n\n\tint limit;\n\tll* table = new ll[NUM];\n\n\tfor(int i=0; i < NUM;i++)table[i] = 1;\n\ttable[0] = 0;\n\ttable[1] = 0;\n\n\tlimit = sqrt(NUM);\n\n\tfor(int i=2;i<=limit;i++){\n\t\tif(table[i] == 1){\n\t\t\tfor(int k=2*i;k < NUM; k += i){\n\t\t\t\ttable[k] = 0;\n\t\t\t}\n\t\t}\n\t}\n\n\tint index = 0;\n\tll* prime_table = new ll[550000];\n\n\tfor(int i = 2; i < NUM; i++){\n\t\tif(table[i] == 1){\n\t\t\tprime_table[index++] = i;\n\t\t}\n\t}\n\n\tll A,B;\n\tscanf(\"%lld %lld\",&A,&B);\n\n\tll left,right;\n\n\tif(A-B <= 2){\n\t\tleft = 2;\n\t}else{\n\t\tleft = A-B;\n\t}\n\n\tright = A+B;\n\n\tif(left > right){\n\t\tprintf(\"0\\n\");\n\t\treturn 0;\n\t}\n\n\tll* check = new ll[right-left+1];\n\tfor(int i = 0; i < right-left+1; i++){\n\t\tcheck[i] = HUGE_NUM;\n\t}\n\n\tbool* is_fox = new bool[right-left+1];\n\tfor(int i = 0; i < right-left+1; i++)is_fox[i] = true;\n\n\tll tmp,count,start;\n\n\tfor(int i = 0; prime_table[i] <= sqrt(right); i++){\n\n\t\tif(left%prime_table[i] == 0){\n\t\t\tstart = 0;\n\t\t}else{\n\t\t\tstart = prime_table[i]*(left/prime_table[i]+1)-left;\n\t\t}\n\n\t\tif(left+start > right)continue;\n\n\t\tfor(int k = start; k < right-left+1; k += prime_table[i]){\n\n\t\t\tif(!is_fox[k])continue;\n\n\t\t\ttmp = left+k;\n\n\t\t\tcount = 0;\n\n\t\t\twhile(tmp%prime_table[i] == 0){\n\t\t\t\ttmp /= prime_table[i];\n\t\t\t\tcount++;\n\t\t\t}\n\n\t\t\tif(check[k] < count){\n\t\t\t\tis_fox[k] = false;\n\t\t\t}else{\n\t\t\t\tcheck[k] = count;\n\t\t\t}\n\t\t}\n\t}\n\n\tint ans = 0;\n\n\tfor(int i = 0; i < right-left+1; i++){\n\t\tif(is_fox[i])ans++;\n\t}\n\n\tprintf(\"%d\\n\",ans);\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 22080, "score_of_the_acc": -0.4328, "final_rank": 6 }, { "submission_id": "aoj_2275_2104620", "code_snippet": "#include <vector>\n#include <iostream>\n#include <algorithm>\nusing namespace std;\nlong long A; int B;\nint main() {\n\tcin >> A >> B;\n\tlong long l = max(A - B, 2LL), r = A + B + 1;\n\tint p = sqrt(r);\n\tvector<long long> f(r - l);\n\tvector<bool> ok(r - l, true); vector<int> x(r - l, 999999999);\n\tfor (int i = 0; i < r - l; i++) f[i] = l + i;\n\tfor (int i = 2; i <= p; i++) {\n\t\tlong long w = l / i * i;\n\t\tif (w < l) w += i;\n\t\tfor (long long j = w; j < r; j += i) {\n\t\t\tint cnt = 0;\n\t\t\twhile (f[j - l] % i == 0) f[j - l] /= i, cnt++;\n\t\t\tif (cnt >= 1) {\n\t\t\t\tif (x[j - l] < cnt) ok[j - l] = false;\n\t\t\t\tx[j - l] = cnt;\n\t\t\t}\n\t\t}\n\t}\n\tint ret = 0;\n\tfor (int i = 0; i < r - l; i++) ret += (ok[i] ? 1 : 0);\n\tcout << ret << endl;\n\treturn 0;\n}", "accuracy": 1, "time_ms": 300, "memory_kb": 14696, "score_of_the_acc": -0.7736, "final_rank": 7 }, { "submission_id": "aoj_2275_2104611", "code_snippet": "#include <vector>\n#include <iostream>\n#include <algorithm>\nusing namespace std;\nlong long A; int B;\nint main() {\n\tcin >> A >> B;\n\tlong long l = max(A - B, 1LL), r = A + B + 1;\n\tint p = sqrt(r);\n\tvector<long long> f(r - l);\n\tvector<bool> ok(r - l, true); vector<int> x(r - l, 999999999);\n\tfor (int i = 0; i < r - l; i++) f[i] = l + i;\n\tfor (int i = 2; i <= p; i++) {\n\t\tlong long w = l / i * i;\n\t\tif (w < l) w += i;\n\t\tfor (long long j = w; j < r; j += i) {\n\t\t\tint cnt = 0;\n\t\t\twhile (f[j - l] % i == 0) f[j - l] /= i, cnt++;\n\t\t\tif (cnt >= 1) {\n\t\t\t\tif (x[j - l] < cnt) ok[j - l] = false;\n\t\t\t\tx[j - l] = cnt;\n\t\t\t}\n\t\t}\n\t}\n\tint ret = 0;\n\tfor (int i = 0; i < r - l; i++) ret += (ok[i] ? 1 : 0);\n\tcout << ret << endl;\n\treturn 0;\n}", "accuracy": 0.027777777777777776, "time_ms": 280, "memory_kb": 14608, "score_of_the_acc": -0.7256, "final_rank": 19 }, { "submission_id": "aoj_2275_2007777", "code_snippet": "#include<iostream>\n#include<vector>\nusing namespace std;\n#define MAX_N 1000007\nlong long A, B, W[MAX_N], cnt; bool prime[MAX_N];\nvector<short>V[MAX_N];\nvoid primesieve() {\n\tfor (int i = 2; i*i <= 1000000; i++) {\n\t\tfor (int j = i*i; j <= 1000000; j += i) prime[j] = true;\n\t}\n\tfor (int i = 0; i <= 1000000; i++)prime[i] ^= true;\n}\nint main() {\n\tcin >> A >> B; long long L = A - B, R = A + B; primesieve(); if (L <= 0)L = 1;\n\tfor (long long i = L; i <= R; i++) { W[i - L] = i; }\n\tfor (long long i = 2; i <= 1000000; i++) {\n\t\tif (prime[i] == false)continue;\n\t\tlong long F = (L / i)*i; if ((L%i) > 0)F += i;\n\t\tfor (long long j = F; j <= R; j += i) {\n\t\t\tint cnts = 0;\n\t\t\twhile (W[j - L] % i == 0) { W[j - L] /= i; cnts++; }\n\t\t\tif (cnts >= 1) { V[j - L].push_back(cnts); }\n\t\t}\n\t}\n\tfor (long long i = L; i <= R; i++) { if (W[i - L] >= 2 && W[i - L] != i)V[i - L].push_back(1); }\n\tfor (long long i = L; i <= R; i++) {\n\t\tbool OK = true; if (i <= 1LL)OK = false;\n\t\tfor (int j = 1; j < V[i - L].size(); j++) {\n\t\t\tif (V[i - L][j - 1] < V[i - L][j])OK = false;\n\t\t}\n\t\tif (OK == true)cnt++;\n\t}\n\tcout << cnt << endl;\n\treturn 0;\n}", "accuracy": 1, "time_ms": 440, "memory_kb": 65376, "score_of_the_acc": -1.9525, "final_rank": 11 }, { "submission_id": "aoj_2275_2007771", "code_snippet": "#include<iostream>\n#include<vector>\nusing namespace std;\n#define MAX_N 1000007\nlong long A, B, W[MAX_N], cnt; bool prime[MAX_N];\nvector<short>V[MAX_N];\nvoid primesieve() {\n\tfor (int i = 2; i*i <= 1000000; i++) {\n\t\tfor (int j = i*i; j <= 1000000; j += i) prime[j] = true;\n\t}\n\tfor (int i = 0; i <= 1000000; i++)prime[i] ^= true;\n}\nint main() {\n\tcin >> A >> B; long long L = A - B, R = A + B; primesieve();\n\tfor (long long i = L; i <= R; i++) { W[i - L] = i; }\n\tfor (long long i = 2; i <= 1000000; i++) {\n\t\tif (prime[i] == false)continue;\n\t\tlong long F = (L / i)*i; if ((L%i) > 0)F += i;\n\t\tfor (long long j = F; j <= R; j += i) {\n\t\t\tint cnts = 0;\n\t\t\twhile (W[j - L] % i == 0) { W[j - L] /= i; cnts++; }\n\t\t\tif (cnts >= 1) { V[j - L].push_back(cnts); }\n\t\t}\n\t}\n\tfor (long long i = L; i <= R; i++) { if (W[i - L] >= 2 && W[i - L] != i)V[i - L].push_back(1); }\n\tfor (long long i = L; i <= R; i++) {\n\t\tbool OK = true;\n\t\tfor (int j = 1; j < V[i - L].size(); j++) {\n\t\t\tif (V[i - L][j - 1] < V[i - L][j])OK = false;\n\t\t}\n\t\tif (OK == true)cnt++;\n\t}\n\tcout << cnt << endl;\n\treturn 0;\n}", "accuracy": 0.027777777777777776, "time_ms": 450, "memory_kb": 65436, "score_of_the_acc": -1.9767, "final_rank": 20 }, { "submission_id": "aoj_2275_1262206", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define rep(i,x,y) for(int i=(x);i<(y);++i)\n#define mp(a,b) make_pair((a),(b))\n#define debug(x) #x << \"=\" << (x)\n \n#ifdef DEBUG\n#define _GLIBCXX_DEBUG\n#define dump(x) std::cerr << debug(x) << \" (L:\" << __LINE__ << \")\" << std::endl\n#else\n#define dump(x)\n#endif\n\ntypedef long long int ll;\ntypedef pair<int,int> pii;\n//template<typename T> using vec=std::vector<T>;\n\nconst int INF=1<<30;\nconst long long int INF_=1LL<<58;\nconst double EPS=1e-9;\nconst int dx[]={1,0,-1,0},dy[]={0,1,0,-1};\n\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec){\n\tos << \"[\";\n\tfor (const auto &v : vec) {\n\t\tos << v << \",\";\n\t}\n\tos << \"]\";\n\treturn os;\n}\n\nbool is_prime[2000000];\nvector<ll> primes;\nbool is_fox_number[11000000];\nint cnt[11000000];\n\nvoid Sieve(){\n\tfill_n(is_prime,2000000,true);\n\tis_prime[0]=is_prime[1]=false;\n\tfor(int i=2; i<2000000; ++i){\n\t\tif(is_prime[i]){\n\t\t\tprimes.push_back(i);\n\t\t\tfor(int j=i*2; j<2000000; j+=i) is_prime[j]=false;\n\t\t}\n\t}\n}\n\nvoid Solve(){\n\tSieve();\n\tll a,b,l,h;\n\tcin >> a >> b;\n\tl=max(a-b,2LL);\n\th=a+b+1;\n\n\tfill_n(is_fox_number,11000000,true);\n\tfill_n(cnt,11000000,INF);\n\n\tfor(auto p:primes){\n\t\tll x=(l+p-1)/p*p;\n\t\tif(p*p>h) break;\n\t\twhile(x<h){\n\t\t\tif(!is_fox_number[x-l]){\n\t\t\t\tx+=p;\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\t\n\t\t\tll tmp=x;\n\t\t\tint c=0;\n\t\t\twhile(tmp%p==0){\n\t\t\t\ttmp/=p;\n\t\t\t\t++c;\n\t\t\t}\n\n\t\t\tif(c>cnt[x-l]) is_fox_number[x-l]=false;\n\n\t\t\tcnt[x-l]=c;\n\t\t\tx+=p;\n\t\t}\n\t}\n\n\tint ans=0;\n\trep(i,0,h-l) ans+=is_fox_number[i];\n\n\tcout << ans << endl;\n}\n\nint main(){\n\tstd::ios::sync_with_stdio(false);\n\tstd::cin.tie(0);\n\tSolve();\n\treturn 0;\n}", "accuracy": 1, "time_ms": 110, "memory_kb": 58996, "score_of_the_acc": -1.0776, "final_rank": 9 }, { "submission_id": "aoj_2275_1262199", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define rep(i,x,y) for(int i=(x);i<(y);++i)\n#define mp(a,b) make_pair((a),(b))\n#define debug(x) #x << \"=\" << (x)\n \n#ifdef DEBUG\n#define _GLIBCXX_DEBUG\n#define dump(x) std::cerr << debug(x) << \" (L:\" << __LINE__ << \")\" << std::endl\n#else\n#define dump(x)\n#endif\n\ntypedef long long int ll;\ntypedef pair<int,int> pii;\n//template<typename T> using vec=std::vector<T>;\n\nconst int INF=1<<30;\nconst long long int INF_=1LL<<58;\nconst double EPS=1e-9;\nconst int dx[]={1,0,-1,0},dy[]={0,1,0,-1};\n\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec){\n\tos << \"[\";\n\tfor (const auto &v : vec) {\n\t\tos << v << \",\";\n\t}\n\tos << \"]\";\n\treturn os;\n}\n\nbool is_prime[2000000];\nvector<ll> primes;\nbool is_fox_number[11000000];\nint cnt[11000000];\n\nvoid Sieve(){\n\tfill_n(is_prime,2000000,true);\n\tis_prime[0]=is_prime[1]=false;\n\tfor(int i=2; i<2000000; ++i){\n\t\tif(is_prime[i]){\n\t\t\tprimes.push_back(i);\n\t\t\tfor(int j=i*2; j<2000000; j+=i) is_prime[j]=false;\n\t\t}\n\t}\n}\n\nvoid Solve(){\n\tSieve();\n\tll a,b,l,h;\n\tcin >> a >> b;\n\tl=max(a-b,2LL);\n\th=a+b+1;\n\n\tfill_n(is_fox_number,11000000,true);\n\tfill_n(cnt,11000000,INF);\n\n\tfor(auto p:primes){\n\t\tll x=(l+p-1)/p*p;\n\t\t//if(x*x>h) break;\n\t\twhile(x<h){\n\t\t\tif(!is_fox_number[x-l]){\n\t\t\t\tx+=p;\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\t\n\t\t\tll tmp=x;\n\t\t\tint c=0;\n\t\t\twhile(tmp%p==0){\n\t\t\t\ttmp/=p;\n\t\t\t\t++c;\n\t\t\t}\n\n\t\t\tif(c>cnt[x-l]) is_fox_number[x-l]=false;\n\n\t\t\tcnt[x-l]=c;\n\t\t\tx+=p;\n\t\t}\n\t}\n\n\tint ans=0;\n\trep(i,0,h-l) ans+=is_fox_number[i];\n\n\tcout << ans << endl;\n}\n\nint main(){\n\tstd::ios::sync_with_stdio(false);\n\tstd::cin.tie(0);\n\tSolve();\n\treturn 0;\n}", "accuracy": 1, "time_ms": 120, "memory_kb": 58160, "score_of_the_acc": -1.0868, "final_rank": 10 }, { "submission_id": "aoj_2275_1262196", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define rep(i,x,y) for(int i=(x);i<(y);++i)\n#define mp(a,b) make_pair((a),(b))\n#define debug(x) #x << \"=\" << (x)\n \n#ifdef DEBUG\n#define _GLIBCXX_DEBUG\n#define dump(x) std::cerr << debug(x) << \" (L:\" << __LINE__ << \")\" << std::endl\n#else\n#define dump(x)\n#endif\n\ntypedef long long int ll;\ntypedef pair<int,int> pii;\n//template<typename T> using vec=std::vector<T>;\n\nconst int INF=1<<30;\nconst long long int INF_=1LL<<58;\nconst double EPS=1e-9;\nconst int dx[]={1,0,-1,0},dy[]={0,1,0,-1};\n\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec){\n\tos << \"[\";\n\tfor (const auto &v : vec) {\n\t\tos << v << \",\";\n\t}\n\tos << \"]\";\n\treturn os;\n}\n\nbool is_prime[2000000];\nvector<ll> primes;\nbool is_fox_number[11000000];\nint cnt[11000000];\n\nvoid Sieve(){\n\tfill_n(is_prime,2000000,true);\n\tis_prime[0]=is_prime[1]=false;\n\tfor(int i=2; i<2000000; ++i){\n\t\tif(is_prime[i]){\n\t\t\tprimes.push_back(i);\n\t\t\tfor(int j=i*2; j<2000000; j+=i) is_prime[j]=false;\n\t\t}\n\t}\n}\n\nvoid Solve(){\n\tSieve();\n\tll a,b,l,h;\n\tcin >> a >> b;\n\tl=max(a-b,2LL);\n\th=a+b+1;\n\n\tfill_n(is_fox_number,11000000,true);\n\tfill_n(cnt,11000000,INF);\n\n\tfor(auto p:primes){\n\t\tll x=(l+p-1)/p*p;\n\t\t//if(x*x>h) break;\n\t\twhile(x<h){\n\t\t\tif(!is_fox_number[x-l]){\n\t\t\t\tx+=p;\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\t\n\t\t\tll tmp=x;\n\t\t\tint c=0;\n\t\t\twhile(tmp%p==0){\n\t\t\t\ttmp/=p;\n\t\t\t\t++c;\n\t\t\t}\n\n\t\t\tif(c>cnt[x-(a-b)]) is_fox_number[x-l]=false;\n\n\t\t\tcnt[x-l]=c;\n\t\t\tx+=p;\n\t\t}\n\t}\n\n\tint ans=0;\n\trep(i,0,h-l) ans+=is_fox_number[i];\n\n\tcout << ans << endl;\n}\n\nint main(){\n\tstd::ios::sync_with_stdio(false);\n\tstd::cin.tie(0);\n\tSolve();\n\treturn 0;\n}", "accuracy": 0.05555555555555555, "time_ms": 110, "memory_kb": 58160, "score_of_the_acc": -1.0635, "final_rank": 13 }, { "submission_id": "aoj_2275_1087370", "code_snippet": "#include <iostream>\n#include <vector>\n#include <string>\n#include <map>\n#include <algorithm>\n\n#define REP(i,n) for(int64_t i=0;i<(int64_t)(n);i++)\n\nusing namespace std;\n\nbool is_valid(const vector<int>& fct, int64_t n) {\n int64_t old_h = 0;\n for (int p : fct) {\n int cnt = 0;\n while((n % p) == 0){\n n /= p;\n ++cnt;\n }\n if (cnt < old_h) return false;\n old_h = cnt;\n }\n return true;\n}\n\nint main() {\n int64_t a, b;\n cin >> a >> b;\n int64_t beg = max(a - b, int64_t(2));\n int64_t ed = a + b;\n vector<vector<int>> pl2(1000001);\n vector<int> ps(1100001,1);\n ps[0] = ps[1] = 0;\n int64_t i;\n for (i = 2; i * i * i * i <= ed; ++i) {\n if (ps[i] == 1) {\n int64_t offset = (i - (beg % i)) % i;\n for (int64_t k = offset; k <= 1000000; k += i) {\n pl2[k].push_back(i);\n }\n for (int64_t j = i * 2; j * j <= ed; j += i) {\n ps[j] = i;\n }\n }\n }\n for (; i * i <= ed; ++i) {\n if (ps[i] == 1) {\n int64_t offset = (i - (beg % i)) % i;\n for (int64_t k = offset; k <= 1000000; k += i) {\n pl2[k].push_back(i);\n }\n for (int64_t j = i * 2; j * j <= ed; j += i) {\n ps[j] = i;\n }\n }\n }\n int64_t cnt = 0;\n for (int64_t i = 0; i <= ed - beg; ++i) {\n auto fct = pl2[i];\n sort(begin(fct),end(fct),greater<int64_t>());\n if (is_valid(fct, beg+i)) {\n ++cnt;\n }\n }\n cout << cnt << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 460, "memory_kb": 63608, "score_of_the_acc": -1.9692, "final_rank": 12 }, { "submission_id": "aoj_2275_1049244", "code_snippet": "#include<iostream>\n#include<algorithm>\n\nusing namespace std;\n\nint main(){\n const int N=1100000;\n static bool cmp[N+1]={true,true};\n for(int i=2;i<=N;i++){\n if(!cmp[i]){\n for(int j=i*2;j<=N;j+=i){\n\tcmp[j]=true;\n }\n }\n }\n static int le[1000001];\n fill(begin(le),end(le),1<<29);\n long long A,B;\n cin>>A>>B;\n for(int i=0;i<=N;i++){\n if(!cmp[i]){\n for(long long j=max<long long>(i,(A-B+i-1)/i*i);j<=A+B;j+=i){\n\tint x=j-(A-B);\n\tint e=0;\n\tfor(long long c=j;c%i==0;c/=i,e++);\n\tle[x]=(le[x]>=e)?e:-1;\n }\n }\n }\n int ans=0;\n for(int i=0;i<B*2+1;i++){\n ans+=i+A-B>=2&&le[i]>0;\n }\n cout<<ans<<endl;\n}", "accuracy": 1, "time_ms": 110, "memory_kb": 6144, "score_of_the_acc": -0.1878, "final_rank": 3 }, { "submission_id": "aoj_2275_1049229", "code_snippet": "#include<iostream>\n#include<algorithm>\n\nusing namespace std;\n\nint main(){\n const int N=1000000;\n static bool cmp[N+1]={true,true};\n for(int i=2;i<=N;i++){\n if(!cmp[i]){\n for(int j=i*2;j<=N;j+=i){\n\tcmp[j]=true;\n }\n }\n }\n static int le[1000001];\n fill(begin(le),end(le),1<<29);\n long long A,B;\n cin>>A>>B;\n for(int i=0;i<=N;i++){\n if(!cmp[i]){\n for(long long j=(A-B+i-1)/i*i;j<=A+B;j+=i){\n\tint x=j-(A-B);\n\tint e=0;\n\tfor(long long c=j;c%i==0;c/=i,e++);\n\tle[x]=(le[x]>=e)?e:-1;\n }\n }\n }\n int ans=0;\n for(int i=0;i<B*2+1;i++){\n ans+=le[i]>0;\n }\n cout<<ans<<endl;\n}", "accuracy": 0.027777777777777776, "time_ms": 100, "memory_kb": 6044, "score_of_the_acc": -0.1629, "final_rank": 15 }, { "submission_id": "aoj_2275_1049225", "code_snippet": "#include<iostream>\n#include<algorithm>\n\nusing namespace std;\n\nint main(){\n static bool cmp[1000001]={true,true};\n for(int i=2;i<=1000000;i++){\n if(!cmp[i]){\n for(int j=i*2;j<=1000000;j+=i){\n\tcmp[j]=true;\n }\n }\n }\n static int le[1000001];\n fill(begin(le),end(le),1<<29);\n long long A,B;\n cin>>A>>B;\n for(int i=0;i<=1000000;i++){\n if(!cmp[i]){\n for(long long j=(A-B+i-1)/i*i;j<=A+B;j+=i){\n\tint x=j-(A-B);\n\tint e=0;\n\tfor(long long c=j;c%i==0;c/=i,e++);\n\tle[x]=(le[x]>=e)?e:-1;\n }\n }\n }\n int ans=0;\n for(int i=0;i<B*2+1;i++){\n ans+=le[i]>0;\n }\n cout<<ans<<endl;\n}", "accuracy": 0.027777777777777776, "time_ms": 100, "memory_kb": 6044, "score_of_the_acc": -0.1629, "final_rank": 15 }, { "submission_id": "aoj_2275_1007718", "code_snippet": "#include<stdio.h>\n#include<algorithm>\nusing namespace std;\nint dm[1100000];\nint m[1100000];\nint isp[1100000];\nint prime[1100000];\nint main(){\n\tint sz=0;\n\tisp[0]=isp[1]=-1;\n\tfor(int i=2;i<1100000;i++){\n\t\tif(~isp[i]){\n\t\t\tisp[i]=1;\n\t\t\tprime[sz++]=i;\n\t\t\tfor(int j=i+i;j<1100000;j+=i)isp[j]=-1;\n\t\t}\n\t}\n\tlong long a,b;scanf(\"%lld%lld\",&a,&b);\n\tlong long L=max(1LL,a-b);\n\tlong long R=a+b;\n\tint n=R-L+1;\n\tfor(int i=0;i<n;i++)m[i]=9999999;\n\tfor(int i=1;(long long)prime[i]*prime[i]<=R;i++){\n\t\tint E=prime[i-1];\n\t\tfor(long long j=(L+E-1)/E*E;j<=R;j+=E){\n\t\t\tlong long now=j;\n\t\t\tint time=0;\n\t\t\twhile(now%E==0){\n\t\t\t\tnow/=E;time++;\n\t\t\t}\n\t\t\tm[j-L]=min(m[j-L],time);\n\t\t}\n\t\tint D=prime[i];\n\t\tlong long S=L/D/D*D*D;\n\t\tlong long T=R/D/D*D*D;\n\t\twhile(S<L)S+=(long long)D*D;\n\t\twhile(T>R)T-=(long long)D*D;\n\t\tfor(long long j=S;j<=T;j+=(long long)D*D){\n\t\t\tlong long now=j;\n\t\t\tint time=0;\n\t\t\twhile(now%D==0){\n\t\t\t\tnow/=D;time++;\n\t\t\t}\n\t\t\tif(m[j-L]<time){\n\t\t\t//\tprintf(\"%lld %d\\n\",j,i);\n\t\t\t\tdm[j-L]=1;\n\t\t\t}\n\t\t}\n\t}\n\tint ret=n;\n\tif(L==1LL)dm[0]=1;\n\tfor(int i=0;i<n;i++)if(dm[i])ret--;\n\tprintf(\"%d\\n\",ret);\n}", "accuracy": 1, "time_ms": 120, "memory_kb": 14064, "score_of_the_acc": -0.3444, "final_rank": 5 }, { "submission_id": "aoj_2275_1007714", "code_snippet": "#include<stdio.h>\n#include<algorithm>\nusing namespace std;\nint dm[1100000];\nint m[1100000];\nint isp[1100000];\nint prime[1100000];\nint main(){\n\tint sz=0;\n\tisp[0]=isp[1]=-1;\n\tfor(int i=2;i<1100000;i++){\n\t\tif(~isp[i]){\n\t\t\tisp[i]=1;\n\t\t\tprime[sz++]=i;\n\t\t\tfor(int j=i+i;j<1100000;j+=i)isp[j]=-1;\n\t\t}\n\t}\n\tlong long a,b;scanf(\"%lld%lld\",&a,&b);\n\tlong long L=max(1LL,a-b);\n\tlong long R=a+b;\n\tint n=R-L+1;\n\tfor(int i=0;i<n;i++)m[i]=9999999;\n\tfor(int i=1;(long long)prime[i]*prime[i]<=R;i++){\n\t\tint E=prime[i-1];\n\t\tfor(long long j=(L+E-1)/E*E;j<=R;j+=E){\n\t\t\tlong long now=j;\n\t\t\tint time=0;\n\t\t\twhile(now%E==0){\n\t\t\t\tnow/=E;time++;\n\t\t\t}\n\t\t\tm[j-L]=min(m[j-L],time);\n\t\t}\n\t\tint D=prime[i];\n\t\tlong long S=L/D/D*D*D;\n\t\tlong long T=R/D/D*D*D;\n\t\twhile(S<L)S+=(long long)D*D;\n\t\twhile(T>R)T-=(long long)D*D;\n\t\tfor(long long j=S;j<=T;j+=(long long)D*D){\n\t\t\tlong long now=j;\n\t\t\tint time=0;\n\t\t\twhile(now%D==0){\n\t\t\t\tnow/=D;time++;\n\t\t\t}\n\t\t\tif(m[j-L]<time){\n\t\t\t//\tprintf(\"%lld %d\\n\",j,i);\n\t\t\t\tdm[j-L]=1;\n\t\t\t}\n\t\t}\n\t}\n\tint ret=n;\n\tfor(int i=0;i<n;i++)if(dm[i])ret--;\n\tprintf(\"%d\\n\",ret);\n}", "accuracy": 0.027777777777777776, "time_ms": 110, "memory_kb": 14064, "score_of_the_acc": -0.3211, "final_rank": 17 }, { "submission_id": "aoj_2275_1007706", "code_snippet": "#include<stdio.h>\n#include<algorithm>\nusing namespace std;\nlong long d[1100000];\nint dm[1100000];\nint m[1100000];\nint isp[1100000];\nint prime[1100000];\nint main(){\n\tint sz=0;\n\tisp[0]=isp[1]=-1;\n\tfor(int i=2;i<1100000;i++){\n\t\tif(~isp[i]){\n\t\t\tisp[i]=1;\n\t\t\tprime[sz++]=i;\n\t\t\tfor(int j=i+i;j<1100000;j+=i)isp[j]=-1;\n\t\t}\n\t}\n\tlong long a,b;scanf(\"%lld%lld\",&a,&b);\n\tlong long L=max(1LL,a-b);\n\tlong long R=a+b;\n\tint n=R-L+1;\n\tfor(int i=0;i<n;i++)d[i]=L+i;\n\tfor(int i=0;i<n;i++)m[i]=9999999;\n\tfor(int i=1;(long long)prime[i]*prime[i]<=R;i++){\n\t\tint E=prime[i-1];\n\t\tfor(long long j=(L+E-1)/E*E;j<=R;j+=E){\n\t\t\tlong long now=j;\n\t\t\tint time=0;\n\t\t\twhile(now%E==0){\n\t\t\t\tnow/=E;time++;\n\t\t\t}\n\t\t\tm[j-L]=min(m[j-L],time);\n\t\t}\n\t\tint D=prime[i];\n\t\tlong long S=L/D/D*D*D;\n\t\tlong long T=R/D/D*D*D;\n\t\twhile(S<L)S+=(long long)D*D;\n\t\twhile(T>R)T-=(long long)D*D;\n\t\tlong long div=(long long )D*D;\n\t\tfor(long long j=S;j<=T;j+=(long long)D*D){\n\t\t\tlong long now=j;\n\t\t\tint time=0;\n\t\t\twhile(now%D==0){\n\t\t\t\tnow/=D;time++;\n\t\t\t}\n\t\t\tif(m[j-L]<time){\n\t\t\t//\tprintf(\"%lld %d\\n\",j,i);\n\t\t\t\tdm[j-L]=1;\n\t\t\t}\n\t\t}\n\t}\n\tint ret=n;\n\tfor(int i=0;i<n;i++)if(dm[i])ret--;\n\tprintf(\"%d\\n\",ret);\n}", "accuracy": 0.027777777777777776, "time_ms": 120, "memory_kb": 22268, "score_of_the_acc": -0.4825, "final_rank": 18 }, { "submission_id": "aoj_2275_507324", "code_snippet": "#include <iostream>\n#include <sstream>\n#include <string>\n#include <algorithm>\n#include <vector>\n#include <stack>\n#include <queue>\n#include <set>\n#include <map>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <cmath>\n#include <cassert>\n\nusing namespace std;\n\n#define FOR(i,k,n) for(int i=(k); i<(int)(n); ++i)\n#define REP(i,n) FOR(i,0,n)\n#define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)\n\ntemplate<class T> void debug(T begin, T end){ for(T i = begin; i != end; ++i) cout<<*i<<\" \"; cout<<endl; }\ninline bool valid(int x, int y, int W, int H){ return (x >= 0 && y >= 0 && x < W && y < H); }\n\ntypedef long long ll;\nconst int INF = 100000000;\nconst double EPS = 1e-8;\nconst int MOD = 1000000007;\nint dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};\nint dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};\nint count_fox_number(ll a, ll b){\n if(a < 2) a = 2;\n if(!(a < b)) return 0;\n int ans = b - a;\n bool prime_small[1000100];\n for(int i = 2; (ll) i * i < b; i++) prime_small[i] = true;\n int cnt_pow[1000100];\n REP(i, b - a) cnt_pow[i] = INF;\n for(int i = 2; (ll)i * i < b; i++){\n if(prime_small[i]){\n for(int j = 2 * i; (ll) j * j < b; j += i) prime_small[j] = false;\n for(ll j = max(2LL, (a + i - 1)/i) * i; j < b; j += i){\n if(cnt_pow[j - a] == -1) continue;\n ll t = j;\n int cnt = 0;\n while(t % i == 0){\n t /= i;\n cnt++;\n }\n assert(cnt != 0);\n if(cnt_pow[j - a] < cnt){\n cnt_pow[j - a] = -1;\n ans--;\n }else{\n cnt_pow[j - a] = cnt;\n }\n }\n }\n }\n return ans;\n}\n\nint main(){\n ll a, b;\n while(cin>>a>>b){\n cout<<count_fox_number(a - b, a + b + 1)<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 6040, "score_of_the_acc": -0.1628, "final_rank": 2 }, { "submission_id": "aoj_2275_507322", "code_snippet": "#include <iostream>\n#include <sstream>\n#include <string>\n#include <algorithm>\n#include <vector>\n#include <stack>\n#include <queue>\n#include <set>\n#include <map>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <cmath>\n#include <cassert>\n\nusing namespace std;\n\n#define FOR(i,k,n) for(int i=(k); i<(int)(n); ++i)\n#define REP(i,n) FOR(i,0,n)\n#define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)\n\ntemplate<class T> void debug(T begin, T end){ for(T i = begin; i != end; ++i) cout<<*i<<\" \"; cout<<endl; }\ninline bool valid(int x, int y, int W, int H){ return (x >= 0 && y >= 0 && x < W && y < H); }\n\ntypedef long long ll;\nconst int INF = 100000000;\nconst double EPS = 1e-8;\nconst int MOD = 1000000007;\nint dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};\nint dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};\nint count_fox_number(ll a, ll b){\n if(a < 2) a = 2;\n if(!(a < b)) return 0;\n int ans = b - a;\n bool prime_small[1000100] = {};\n //for(int i = 2; (ll) i * i < b; i++) prime_small[i] = true;\n int cnt_pow[1000100];\n REP(i, b - a) cnt_pow[i] = INF;\n for(int i = 2; (ll)i * i < b; i++){\n if(!prime_small[i]){\n for(int j = 2 * i; (ll) j * j < b; j += i) prime_small[j] = true;\n for(ll j = max(2LL, (a + i - 1)/i) * i; j < b; j += i){\n if(cnt_pow[j - a] == -1) continue;\n ll t = j;\n int cnt = 0;\n while(t % i == 0){\n t /= i;\n cnt++;\n }\n assert(cnt != 0);\n if(cnt_pow[j - a] < cnt){\n cnt_pow[j - a] = -1;\n ans--;\n }else{\n cnt_pow[j - a] = cnt;\n }\n }\n }\n }\n return ans;\n}\n\nint main(){\n ll a, b;\n while(cin>>a>>b){\n cout<<count_fox_number(a - b, a + b + 1)<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 120, "memory_kb": 6052, "score_of_the_acc": -0.2095, "final_rank": 4 }, { "submission_id": "aoj_2275_419707", "code_snippet": "#include <iostream>\n#include <fstream>\n#include <algorithm>\n#include <bitset>\n#include <cassert>\n#include <cctype>\n#include <cmath>\n#include <complex>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <ctime>\n#include <deque>\n#include <iomanip>\n#include <map>\n#include <numeric>\n#include <queue>\n#include <set>\n#include <stack>\n#include <sstream>\n#include <string>\n#include <vector>\nusing namespace std;\n\n#pragma comment(linker, \"/STACK:400000000\")\n\n#define EPS 1e-9\n#define INF MOD\n#define MOD 1000000007LL\n#define fir first\n#define foreach(it,X) for(it=X.begin();it!=X.end();it++)\n#define iss istringstream\n#define ite iterator\n#define ll long long\n#define mp make_pair\n#define rep(i,n) rep2(i,0,n)\n#define rep2(i,m,n) for(int i=m;i<n;i++)\n#define pi pair<int,int>\n#define pb push_back\n#define sec second\n#define sh(i) (1LL<<i)\n#define sst stringstream\n#define sz size()\n#define vi vector<int>\n#define vc vector\n#define vl vector<ll>\n#define vs vector<string>\n\nbool isp[1000011];\nint last[1000011];\nll cur[1000011];\nll A,B,a,b;\n\nint main(){\n\tcin>>A>>B;\n\ta=max(2LL,A-B);\n\tb=A+B;\n\tfill(isp,isp+1000011,1);\n\tfill(last,last+2*B+1,INF);\n\tfor(ll i=a;i<=b;i++)cur[i-a]=i;\n\tfor(ll i=2;i*i<=b;i++){\n\t\tif(isp[i]){\n\t\t\tfor(ll j=2*i;j*j<=b;j+=i) isp[j]=0;\n\t\t\tfor(ll j=max(2LL,(a+i-1)/i)*i;j<=b;j+=i){\n\t\t\t\tif(last[j-a]>-1){\n\t\t\t\t\tint y=0;\n\t\t\t\t\twhile(cur[j-a]%i==0)cur[j-a]/=i,y++;\n\t\t\t\t\tif(last[j-a]<y)last[j-a]=-1;\n\t\t\t\t\telse last[j-a]=y;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tint ans=0;\n\tfor(ll i=a;i<=b;i++)ans+=last[i-a]>-1;\n\tcout<<ans<<endl;\n}", "accuracy": 1, "time_ms": 340, "memory_kb": 13000, "score_of_the_acc": -0.8381, "final_rank": 8 } ]
aoj_2277_cpp
問題 G XOR 回路 問題文 計算機科学実験及演習 3 は CAD を用いて CPU を設計する授業である.CPU は多くの回路を組み合わせなければ動かず,その中の 1 つに n ビット入力の XOR 回路がある.ここで XOR 回路とは,入力ビット列 x 1 x 2 ...x n に対して, x 1 + x 2 + ... + x n \ (mod 2) を出力する回路のことを言う.しかし完璧に動作する XOR 回路を設計するのは時間がかかるので,とりあえず n ビットのうち k ビットだけ使う XOR 回路 A を作ることにした.つまり,ある i 1 , ... , i k が存在し,回路 A は x i 1 + x i 2 + ... + x i k \ (mod 2) を出力する. 暫く後に今度は k ビット入力の XOR 回路が欲しくなった.何だ簡単ではないか.先ほどの回路 A を使えばよい.ただ残念なことに,回路 A がどの k ビットを使っていたのか忘れてしまった上に,回路 A の設計図も間違って削除してしまった.しかしコンパイル済みの回路 A は残っている.なので,入力 x 1 x 2 ...x n を入れて回路 A を実行することで,その出力 x i 1 + x i 2 + ... + x i k \ (mod 2) を見ることは出来る. 出来るだけ作業の時間を短くしたいので,回路 A の実行回数には上限を設定することにしよう.どうすれば回路Aが依存しているビット i 1 , ... , i k を見つけられるだろうか? 入出力 最初に n と k がスペース区切りで与えられる.以降,プログラムは回路 A に入力を与え,その出力を読むことが出来る.例えば C/C++ で回路 A にビット列 x 1 x 2 ...x n を与えるには printf("? x 1 x 2 ...x n \n"); fflush(stdout); とする.ここで各 x i の間にスペースをいれてはならない. 次に, scanf("%d", &v); とすると v に対応する出力 x i 1 + x i 2 + ... + x i k \ (mod 2) が入る. 最終的に回路 A が依存するビット i 1 ,...,i k を出力するには printf("! i 1 i 2 ...i k \n"); fflush(stdout); とする.ここで各 i j の間はスペースを丁度 1 つずついれる. 制約 1 ≤ n ≤ 10,000 1 ≤ k ≤ min(10, n) 各データセットごとに,回路 A の実行回数の上限は 200 回であり,それを超えると誤答 ( Query Limit Exceeded ) と判定される. 入出力例 入力例 1 以下の例はプログラムの入出力の例である.左の列はプログラムの出力,右の列はプログラムへの入力を時系列順に示している.最初に n k が入力として与えられる.ここでは n = 2, k = 1 である.次に回路 A に 00 という入力をいれると,回路 A は 0 を返した.次に回路 A に 01 という入力をいれると,回路 A は再び 0 を返した.このことから回路 A は 1 ビット目のみ利用していることが分かり,プログラムは 1 を解答として出力した. プログラムの出力 プログラムへの入力 2 1 ?00 0 ?01 0 !1 入力例 2 以下の例では, n = 2, k = 2 であり,直ちに回路 A が 1 ビット目と 2 ビット目の両方を利用していることが分かる.よってプログラムは 1 と 2 を解答として出力した. プログラムの出力 プログラムへの入力 2 2 !1 2
[ { "submission_id": "aoj_2277_10851253", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint n, k, qq[10005], ans[10005];\n\nint ask(int lf = 0, int rg = n - 1){\n cout << \"?\";\n for(int i = 0; i < n; i ++)\n cout << ((lf <= i && i <= rg) ? qq[i] : 0);\n cout << endl;\n\n int rs; cin >> rs;\n return rs;\n}\n\nvoid print_ans(){\n cout << \"!\"; bool first = 0;\n for(int i = 0; i < n; i ++) if(ans[i]){\n\t\t\t\tif(i > 0) cout << \" \"; first = 1;\n \t\t\tcout << i + 1;\n }\n cout << endl;\n\n exit(0);\n}\n\nint main(){\n srand(time(NULL));\n cin >> n >> k;\n\n if(n == k){\n for(int i = 0; i < n; i ++) ans[i] = 1;\n print_ans();\n }\n\n for(int times = 0; times < k; times ++){\n do{\n for(int i = 0; i < n; i ++)\n qq[i] = ans[i] ? 0 : (rand() & 1);\n }while(!ask());\n\n int lf = 0, rg = n - 1, rs = n;\n for(int md; lf <= rg;){\n md = (lf + rg) / 2;\n if(ask(lf, md)){\n rs = md;\n rg = md - 1;\n }\n else lf = md + 1;\n }\n\n ans[rs] = 1;\n }\n\n print_ans();\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 3496, "score_of_the_acc": -1.0435, "final_rank": 12 }, { "submission_id": "aoj_2277_4300652", "code_snippet": "#include <iostream>\n#include <cmath>\n#include <numeric>\n#include <algorithm>\n#include <deque>\n#include <vector>\n#include <unordered_map>\n#include <unordered_set>\n#include <iomanip>\n#include <map>\n#include <stack>\n#include <queue>\n#include <functional>\n#include <climits>\n#include <numeric>\n#include <bitset>\n#include <random>\n#include <tuple>\n#include <initializer_list>\n#include <fstream>\n\nvoid ask(const std::vector<int>& bits, int from, int until) {\n\tstd::cout << '?';\n\tfor (auto i = 0; i < from; ++i) std::cout << '0';\n\twhile (from < until) std::cout << bits[from++];\n\tfor (auto i = until; i < bits.size(); ++i) std::cout << '0';\n\tstd::cout << std::endl;\n}\n\nint main() {\n\tint n, k; std::cin >> n >> k;\n\tstd::vector<int> bits(n), confirmed;\n\tstd::mt19937 rand;\n\twhile (confirmed.size() < k) {\n\t\tfor (auto& b : bits) b = rand() & 1;\n\t\tfor (const auto c : confirmed) bits[c - 1] = 0;\n\t\task(bits, 0, bits.size());\n\t\tint response; std::cin >> response;\n\t\tif (response == 0) continue;\n\t\tint min = 0, max = bits.size() ;\n\t\twhile (min < max) {\n\t\t\tconst auto mid = (min + max) / 2;\n\t\t\tif (mid == 0) {\n\t\t\t\tmin = mid + 1; continue;\n\t\t\t}\n\t\t\task(bits, 0, mid);\n\t\t\tstd::cin >> response;\n\t\t\tif (response == 1) {\n\t\t\t\tmax = mid;\n\t\t\t}\n\t\t\telse {\n\t\t\t\tmin = mid + 1;\n\t\t\t}\n\t\t}\n\t\tconfirmed.push_back(max);\n\t}\n\tstd::sort(confirmed.begin(), confirmed.end());\n\tstd::cout << '!';\n\tfor (auto i = 0; i < confirmed.size(); ++i) {\n\t\tif (i != 0) std::cout << ' ';\n\t\tstd::cout << confirmed[i];\n\t}\n\tstd::cout << std::endl;\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 3256, "score_of_the_acc": -0.9857, "final_rank": 11 }, { "submission_id": "aoj_2277_1469132", "code_snippet": "#include<cstdio>\n#include<cstdlib>\n#include<cstring>\n#include<cmath>\n#include<iostream>\n#include<string>\n#include<vector>\n#include<map>\n#include<set>\n#include<list>\n#include<queue>\n#include<deque>\n#include<algorithm>\n#include<numeric>\n#include<utility>\n#include<complex>\n#include<functional>\n \nusing namespace std;\n\n/* constant */\n\nconst int MAX_N = 10000;\nconst int MAX_K = 10;\nconst int MAX_Q = 200;\n\n/* typedef */\n\ntypedef vector<int> vi;\ntypedef vector<long long> vl;\ntypedef vector<double> vd;\ntypedef pair<int,int> pii;\ntypedef pair<long,long> pll;\ntypedef long long ll;\n\n/* global variables */\n\nint n, k;\nbool xs[MAX_N], used[MAX_N];\n\n/* subroutines */\n\nbool ask(int i0 = 0, int i1 = n) {\n putchar('?');\n for (int i = 0; i < n; i++)\n printf(\"%d\", (i0 <= i && i < i1) ? xs[i] : 0);\n putchar('\\n');\n\n int ans;\n cin >> ans;\n return (bool)ans;\n}\n\nvoid putans() {\n bool first = true;\n\n putchar('!');\n for (int i = 0; i < n; i++)\n if (used[i]) {\n if (i > 0) putchar(' ');\n printf(\"%d\", i + 1);\n }\n putchar('\\n');\n \n}\n\n/* main */\n\nint main() {\n srand(time(NULL));\n\n cin >> n >> k;\n\n if (n == k) {\n for (int i = 0; i < n; i++) used[i] = true;\n putans();\n return 0;\n }\n \n memset(used, false, sizeof(used));\n int m = 0;\n\n while (m < k) {\n do {\n for (int i = 0; i < n; i++)\n\txs[i] = used[i] ? 0 : (rand() & 1);\n } while (! ask());\n\n int i0 = 0, i1 = n;\n\n while (i0 + 1 < i1) {\n int im = (i0 + i1) / 2;\n if (ask(i0, im)) i1 = im;\n else i0 = im;\n }\n\n used[i0] = true;\n m++;\n }\n\n putans();\n \n return 0;\n}", "accuracy": 1, "time_ms": 280, "memory_kb": 1296, "score_of_the_acc": -0.6533, "final_rank": 10 }, { "submission_id": "aoj_2277_1054303", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\ninline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}\ntemplate<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}\ntemplate<class T> inline T sqr(T x) {return x*x;}\n\ntypedef vector<int> vi;\ntypedef vector<vi> vvi;\ntypedef vector<string> vs;\ntypedef pair<int, int> pii;\ntypedef long long ll;\n\n#define all(a) (a).begin(),(a).end()\n#define rall(a) (a).rbegin(), (a).rend()\n#define pb push_back\n#define mp make_pair\n#define each(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)\n#define exist(s,e) ((s).find(e)!=(s).end())\n#define range(i,a,b) for(int i=(a);i<(b);++i)\n#define rep(i,n) range(i,0,n)\n#define clr(a,b) memset((a), (b) ,sizeof(a))\n#define dump(x) cerr << #x << \" = \" << (x) << endl;\n#define debug(x) cerr << #x << \" = \" << (x) << \" (L\" << __LINE__ << \")\" << \" \" << __FILE__ << endl;\n\nconst double eps = 1e-10;\nconst double pi = acos(-1.0);\nconst ll INF =1LL << 62;\nconst int inf =1 << 29;\n\nint n,k;\n\nbool used[10010];\n\nint check(string s){\n\tint l=0,r=n;\n\twhile(r-l>1){\n\n\t\tint m=(l+r)/2;\n\t\tstring next;\n\n\t\tfor(int i=0;i<n;++i){\n\t\t\tif(l<=i&&i<m)\n\t\t\t\tnext+=s[i];\n\t\t\telse\n\t\t\t\tnext+=\"0\";\n\t\t}\n\n\t\tprintf(\"?%s\\n\",next.c_str());\n\t\tfflush(stdout);\n\t\tint res;\n\t\tscanf(\"%d\",&res);\n\n\t\tif(res)\n\t\t\tr=m;\n\t\telse\n\t\t\tl=m;\n\t}\n\treturn l;\n}\n\nint main(void){\n\tsrand(time(NULL));\n\tscanf(\"%d %d\",&n,&k);\n\tint num=0;\n\twhile(num<k){\n\t\tstring verify=\"\";\n\t\trep(i,n){\n\t\t\tif(used[i])\n\t\t\t\tverify+=\"0\";\n\t\t\telse\n\t\t\t\tverify+=(rand()&1?\"1\":\"0\");\n\t\t}\n\t\tprintf(\"?%s\\n\",verify.c_str());\n\t\tfflush(stdout);\n\t\tint res;\n\t\tscanf(\"%d\",&res);\n\n\t\tif(res){\n\t\t\tused[check(verify)]=true;\n\t\t\tnum++;\n\t\t}\n\t}\n\tstring ans=\"\";\n\tbool first=true;\n\trep(i,n){\n\t\tif(used[i]){\n\t\t\tif(first)\n\t\t\t\tfirst=false;\n\t\t\telse\n\t\t\t\tans+=\" \";\n\t\t\tans+=toString(i+1);\n\t\t}\n\t}\n\tprintf(\"!%s\\n\",ans.c_str()); \n\tfflush(stdout);\n\treturn 0;\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 1308, "score_of_the_acc": -0.4285, "final_rank": 6 }, { "submission_id": "aoj_2277_898524", "code_snippet": "#include<iostream>\n#include<set>\n#include<cstdlib>\n\nusing namespace std;\n\n#define rep(i, n) for (int i = 0; i < int(n); ++i)\n\nset<int> used;\nint n, k;\n\nvoid solve2(string str) {\n int cnt = 0;\n rep (i, n) if (str[i] == '1') ++cnt;\n if (cnt == 1) {\n rep (i, n) if (str[i] == '1') {\n used.insert(i);\n return;\n }\n }\n string s;\n rep (i, n) {\n if (str[i] == '1' && rand() % 2) s += \"1\";\n else s += \"0\";\n }\n cout << \"?\" + s << endl;\n cout.flush();\n int t;\n cin >> t;\n if (t == 1) {\n solve2(s);\n } else {\n rep (i, n) if (str[i] == '1') s[i] ^= '0' ^ '1';\n solve2(s);\n }\n}\n\nvoid solve() {\n while (true) {\n string str;\n rep (i, n) {\n str += rand() % 2 ? \"1\" : \"0\";\n if (used.count(i)) str[str.size() - 1] = '0';\n }\n cout << \"?\" + str << endl;\n cout.flush();\n int t;\n cin >> t;\n if (t == 1) {\n solve2(str);\n return;\n }\n }\n}\n\nint main() {\n cin >> n >> k;\n rep (i, k) solve();\n for (set<int>::iterator itr = used.begin(); itr != used.end(); ++itr) {\n if (itr == used.begin()) cout << \"!\";\n else cout << \" \";\n cout << *itr + 1;\n }\n cout << endl;\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 1772, "score_of_the_acc": -0.5721, "final_rank": 9 }, { "submission_id": "aoj_2277_651012", "code_snippet": "#include<stdio.h>\n#include<algorithm>\nusing namespace std;\nchar str[100000];\nint ret[10];\nint val[10000];\nint used[10000];\nint main(){\n\tint a,b;\n\tscanf(\"%d%d\",&a,&b);\n\tint at=0;\n\tif(a<200){\n\t\tfor(int i=0;i<a;i++){\n\t\t\tfor(int j=0;j<a;j++){\n\t\t\t\tif(i==j)str[j]='1';\n\t\t\t\telse str[j]='0';\n\t\t\t}\n\t\t\tprintf(\"?%s\\n\",str);\n\t\t\tfflush(stdout);\n\t\t\tint c;\n\t\t\tscanf(\"%d\",&c);\n\t\t\tif(c)ret[at++]=i+1;\n\t\t}\n\t\tprintf(\"!\");\n\t\tfor(int i=0;i<b;i++){\n\t\t\tprintf(\"%d\",ret[i]);\n\t\t\tif(i<b-1)printf(\" \");\n\t\t\telse printf(\"\\n\");\n\t\t}\n\t\tfflush(stdout);\n\t\treturn 0;\n\t}\n\tfor(int i=0;i<a;i++)val[i]=i;\n\tint n=a/2;\n\tfor(int i=0;i<b;i++){\n\t\tint d=0;\n\t\tdo{\n\t\t\tfor(int j=0;j<50000;j++){\n\t\t\t\tint l=rand()%a;\n\t\t\t\tint r=rand()%a;\n\t\t\t\tint z=val[l];\n\t\t\t\tval[l]=val[r];\n\t\t\t\tval[r]=z;\n\t\t\t}\n\t\t\tfor(int j=0;j<a;j++)str[j]='0';\n\t\t\tint ind=0;\n\t\t\tfor(int j=0;ind<n;j++){\n\t\t\t\tif(!used[val[j]]){\n\t\t\t\t\tstr[val[j]]='1';\n\t\t\t\t\tind++;\n\t\t\t\t}\n\t\t\t}\n\t\t\tprintf(\"?%s\\n\",str);\n\t\t\tfflush(stdout);\n\t\t\tscanf(\"%d\",&d);\n\t\t}while(!d);\n\t\tint left=-1;\n\t\tint right=n+1;\n\t\twhile(left+1<right){\n\t\t\tint M=(left+right)/2;\n\t\t\tfor(int j=0;j<a;j++)str[j]='0';\n\t\t\tint ind=0;\n\t\t\tfor(int j=0;ind<M;j++){\n\t\t\t\tif(!used[val[j]]){\n\t\t\t\t\tstr[val[j]]='1';\n\t\t\t\t\tind++;\n\t\t\t\t}\n\t\t\t}\n\t\t\tprintf(\"?%s\\n\",str);\n\t\t\tfflush(stdout);\n\t\t\tscanf(\"%d\",&d);\n\t\t\tif(d)right=M;\n\t\t\telse left=M;\n\t\t}\n\t\tint V=0;\n\t\tint P=0;\n\t\tfor(int j=0;V<right;j++){\n\t\t\tif(!used[val[j]]){\n\t\t\t//\tstr[val[j]]='1';\n\t\t\t\tV++;P=j;\n\t\t\t}\n\t\t}\n\t\tused[val[P]]=1;\n\t\tret[at++]=val[P]+1;\n\t}std::sort(ret,ret+b);\n\tprintf(\"!\");\n\tfor(int i=0;i<b;i++){\n\t\tprintf(\"%d\",ret[i]);\n\t\tif(i<b-1)printf(\" \");\n\t\telse printf(\"\\n\");\n\t}\n\tfflush(stdout);\n}", "accuracy": 1, "time_ms": 110, "memory_kb": 1296, "score_of_the_acc": -0.4685, "final_rank": 8 }, { "submission_id": "aoj_2277_651000", "code_snippet": "#include<stdio.h>\n#include<algorithm>\nusing namespace std;\nchar str[100000];\nint ret[10];\nint val[10000];\nint used[10000];\nint main(){\n\tint a,b;\n\tscanf(\"%d%d\",&a,&b);\n\tint at=0;\n\tif(a<200){\n\t\tfor(int i=0;i<a;i++){\n\t\t\tfor(int j=0;j<a;j++){\n\t\t\t\tif(i==j)str[j]='1';\n\t\t\t\telse str[j]='0';\n\t\t\t}\n\t\t\tprintf(\"?%s\\n\",str);\n\t\t\tfflush(stdout);\n\t\t\tint c;\n\t\t\tscanf(\"%d\",&c);\n\t\t\tif(c)ret[at++]=i+1;\n\t\t}\n\t\tprintf(\"!\");\n\t\tfor(int i=0;i<b;i++){\n\t\t\tprintf(\"%d\",ret[i]);\n\t\t\tif(i<b-1)printf(\" \");\n\t\t\telse printf(\"\\n\");\n\t\t}\n\t\tfflush(stdout);\n\t\treturn 0;\n\t}\n\tfor(int i=0;i<a;i++)val[i]=i;\n\tint n=a/2;\n\tfor(int i=0;i<b;i++){\n\t\tint d=0;\n\t\tdo{\n\t\t\tfor(int j=0;j<50000;j++){\n\t\t\t\tint l=rand()%a;\n\t\t\t\tint r=rand()%a;\n\t\t\t\tint z=val[l];\n\t\t\t\tval[l]=val[r];\n\t\t\t\tval[r]=z;\n\t\t\t}\n\t\t\tfor(int j=0;j<a;j++)str[j]='0';\n\t\t\tint ind=0;\n\t\t\tfor(int j=0;ind<n;j++){\n\t\t\t\tif(!used[val[j]]){\n\t\t\t\t\tstr[val[j]]='1';\n\t\t\t\t\tind++;\n\t\t\t\t}\n\t\t\t}\n\t\t\tprintf(\"?%s\\n\",str);\n\t\t\tfflush(stdout);\n\t\t\tscanf(\"%d\",&d);\n\t\t}while(!d);\n\t\tint left=0;\n\t\tint right=n+1;\n\t\twhile(left+1<right){\n\t\t\tint M=(left+right)/2;\n\t\t\tfor(int j=0;j<a;j++)str[j]='0';\n\t\t\tint ind=0;\n\t\t\tfor(int j=0;ind<M;j++){\n\t\t\t\tif(!used[val[j]]){\n\t\t\t\t\tstr[val[j]]='1';\n\t\t\t\t\tind++;\n\t\t\t\t}\n\t\t\t}\n\t\t\tprintf(\"?%s\\n\",str);\n\t\t\tfflush(stdout);\n\t\t\tscanf(\"%d\",&d);\n\t\t\tif(d)right=M;\n\t\t\telse left=M;\n\t\t}\n\t\tused[val[left]]=1;\n\t\tret[at++]=val[left]+1;\n\t}std::sort(ret,ret+b);\n\tprintf(\"!\");\n\tfor(int i=0;i<b;i++){\n\t\tprintf(\"%d\",ret[i]);\n\t\tif(i<b-1)printf(\" \");\n\t\telse printf(\"\\n\");\n\t}\n\tfflush(stdout);\n}", "accuracy": 0.0975609756097561, "time_ms": 70, "memory_kb": 1280, "score_of_the_acc": -0.4205, "final_rank": 17 }, { "submission_id": "aoj_2277_650998", "code_snippet": "#include<stdio.h>\n#include<algorithm>\nusing namespace std;\nchar str[100000];\nint ret[10];\nint val[10000];\nint used[10000];\nint main(){\n\tint a,b;\n\tscanf(\"%d%d\",&a,&b);\n\tint at=0;\n\tif(a<200){\n\t\tfor(int i=0;i<a;i++){\n\t\t\tfor(int j=0;j<a;j++){\n\t\t\t\tif(i==j)str[j]='1';\n\t\t\t\telse str[j]='0';\n\t\t\t}\n\t\t\tprintf(\"?%s\\n\",str);\n\t\t\tfflush(stdout);\n\t\t\tint c;\n\t\t\tscanf(\"%d\",&c);\n\t\t\tif(c)ret[at++]=i+1;\n\t\t}\n\t\tprintf(\"!\");\n\t\tfor(int i=0;i<b;i++){\n\t\t\tprintf(\"%d\",ret[i]);\n\t\t\tif(i<b-1)printf(\" \");\n\t\t\telse printf(\"\\n\");\n\t\t}\n\t\tfflush(stdout);\n\t\treturn 0;\n\t}\n\tfor(int i=0;i<a;i++)val[i]=i;\n\tint n=a/2;\n\tfor(int i=0;i<b;i++){\n\t\tint d=0;\n\t\tdo{\n\t\t\tfor(int j=0;j<50000;j++){\n\t\t\t\tint l=rand()%a;\n\t\t\t\tint r=rand()%a;\n\t\t\t\tint z=val[l];\n\t\t\t\tval[l]=val[r];\n\t\t\t\tval[r]=z;\n\t\t\t}\n\t\t\tfor(int j=0;j<a;j++)str[j]='0';\n\t\t\tint ind=0;\n\t\t\tfor(int j=0;ind<n;j++){\n\t\t\t\tif(!used[val[j]]){\n\t\t\t\t\tstr[val[j]]='1';\n\t\t\t\t\tind++;\n\t\t\t\t}\n\t\t\t}\n\t\t\tprintf(\"?%s\\n\",str);\n\t\t\tfflush(stdout);\n\t\t\tscanf(\"%d\",&d);\n\t\t}while(!d);\n\t\tint left=0;\n\t\tint right=n;\n\t\twhile(left+1<right){\n\t\t\tint M=(left+right)/2;\n\t\t\tfor(int j=0;j<a;j++)str[j]='0';\n\t\t\tint ind=0;\n\t\t\tfor(int j=0;ind<M;j++){\n\t\t\t\tif(!used[val[j]]){\n\t\t\t\t\tstr[val[j]]='1';\n\t\t\t\t\tind++;\n\t\t\t\t}\n\t\t\t}\n\t\t\tprintf(\"?%s\\n\",str);\n\t\t\tfflush(stdout);\n\t\t\tscanf(\"%d\",&d);\n\t\t\tif(d)right=M;\n\t\t\telse left=M;\n\t\t}\n\t\tused[val[left]]=1;\n\t\tret[at++]=val[left]+1;\n\t}std::sort(ret,ret+b);\n\tprintf(\"!\");\n\tfor(int i=0;i<b;i++){\n\t\tprintf(\"%d\",ret[i]);\n\t\tif(i<b-1)printf(\" \");\n\t\telse printf(\"\\n\");\n\t}\n\tfflush(stdout);\n}", "accuracy": 0.0975609756097561, "time_ms": 60, "memory_kb": 1280, "score_of_the_acc": -0.4096, "final_rank": 14 }, { "submission_id": "aoj_2277_650997", "code_snippet": "#include<stdio.h>\n#include<algorithm>\nusing namespace std;\nchar str[100000];\nint ret[10];\nint val[10000];\nint used[10000];\nint main(){\n\tint a,b;\n\tscanf(\"%d%d\",&a,&b);\n\tint at=0;\n\tif(a<200){\n\t\tfor(int i=0;i<a;i++){\n\t\t\tfor(int j=0;j<a;j++){\n\t\t\t\tif(i==j)str[j]='1';\n\t\t\t\telse str[j]='0';\n\t\t\t}\n\t\t\tprintf(\"?%s\\n\",str);\n\t\t\tfflush(stdout);\n\t\t\tint c;\n\t\t\tscanf(\"%d\",&c);\n\t\t\tif(c)ret[at++]=i+1;\n\t\t}\n\t\tprintf(\"!\");\n\t\tfor(int i=0;i<b;i++){\n\t\t\tprintf(\"%d\",ret[i]);\n\t\t\tif(i<b-1)printf(\" \");\n\t\t\telse printf(\"\\n\");\n\t\t}\n\t\tfflush(stdout);\n\t\treturn 0;\n\t}\n\tfor(int i=0;i<a;i++)val[i]=i;\n\tint n=a/2;\n\tfor(int i=0;i<b;i++){\n\t\tint d=0;\n\t\tdo{\n\t\t\tfor(int j=0;j<50000;j++){\n\t\t\t\tint l=rand()%a;\n\t\t\t\tint r=rand()%a;\n\t\t\t\tint z=val[l];\n\t\t\t\tval[l]=val[r];\n\t\t\t\tval[r]=z;\n\t\t\t}\n\t\t\tfor(int j=0;j<a;j++)str[j]='0';\n\t\t\tint ind=0;\n\t\t\tfor(int j=0;ind<n;j++){\n\t\t\t\tif(!used[val[j]]){\n\t\t\t\t\tstr[val[j]]='1';\n\t\t\t\t\tind++;\n\t\t\t\t}\n\t\t\t}\n\t\t\tprintf(\"?%s\\n\",str);\n\t\t\tfflush(stdout);\n\t\t\tscanf(\"%d\",&d);\n\t\t}while(!d);\n\t\tint left=0;\n\t\tint right=n;\n\t\twhile(left+1<right){\n\t\t\tint M=(left+right)/2;\n\t\t\tfor(int j=0;j<a;j++)str[j]='0';\n\t\t\tint ind=0;\n\t\t\tfor(int j=0;ind<M;j++){\n\t\t\t\tif(!used[val[j]]){\n\t\t\t\t\tstr[val[j]]='1';\n\t\t\t\t\tind++;\n\t\t\t\t}\n\t\t\t}\n\t\t\tprintf(\"?%s\\n\",str);\n\t\t\tfflush(stdout);\n\t\t\tscanf(\"%d\",&d);\n\t\t\tif(d)right=M;\n\t\t\telse left=M;\n\t\t}\n\t\tused[val[left]]=1;\n\t\tret[at++]=val[left]+1;\n\t}\n\tprintf(\"!\");\n\tfor(int i=0;i<b;i++){\n\t\tprintf(\"%d\",ret[i]);\n\t\tif(i<b-1)printf(\" \");\n\t\telse printf(\"\\n\");\n\t}\n\tfflush(stdout);\n}", "accuracy": 0.0975609756097561, "time_ms": 60, "memory_kb": 1280, "score_of_the_acc": -0.4096, "final_rank": 14 }, { "submission_id": "aoj_2277_650996", "code_snippet": "#include<stdio.h>\n#include<algorithm>\nusing namespace std;\nchar str[100000];\nint ret[10];\nint val[10000];\nint used[10000];\nint main(){\n\tint a,b;\n\tscanf(\"%d%d\",&a,&b);\n\tint at=0;\n\tif(a<200){\n\t\tfor(int i=0;i<a;i++){\n\t\t\tfor(int j=0;j<a;j++){\n\t\t\t\tif(i==j)str[j]='1';\n\t\t\t\telse str[j]='0';\n\t\t\t}\n\t\t\tprintf(\"?%s\\n\",str);\n\t\t\tfflush(stdout);\n\t\t\tint c;\n\t\t\tscanf(\"%d\",&c);\n\t\t\tif(c)ret[at++]=i+1;\n\t\t}\n\t\tprintf(\"!\");\n\t\tfor(int i=0;i<b;i++){\n\t\t\tprintf(\"%d\",ret[i]);\n\t\t\tif(i<b-1)printf(\" \");\n\t\t\telse printf(\"\\n\");\n\t\t}\n\t\tfflush(stdout);\n\t\treturn 0;\n\t}\n\tfor(int i=0;i<a;i++)val[i]=i;\n\tint n=a/2;\n\tfor(int i=0;i<b;i++){\n\t\tint d=0;\n\t\tdo{\n\t\t\tfor(int j=0;j<50000;j++){\n\t\t\t\tint l=rand()%a;\n\t\t\t\tint r=rand()%a;\n\t\t\t\tint z=val[l];\n\t\t\t\tval[l]=val[r];\n\t\t\t\tval[r]=z;\n\t\t\t}\n\t\t\tfor(int j=0;j<a;j++)str[j]='0';\n\t\t\tint ind=0;\n\t\t\tfor(int j=0;ind<n;j++){\n\t\t\t\tif(!used[val[j]]){\n\t\t\t\t\tstr[val[j]]='1';\n\t\t\t\t\tind++;\n\t\t\t\t}\n\t\t\t}\n\t\t\tprintf(\"?%s\\n\",str);\n\t\t\tfflush(stdout);\n\t\t\tscanf(\"%d\",&d);\n\t\t}while(!d);\n\t\tint left=0;\n\t\tint right=n;\n\t\twhile(left+1<right){\n\t\t\tint M=(left+right)/2;\n\t\t\tfor(int j=0;j<a;j++)str[j]='0';\n\t\t\tint ind=0;\n\t\t\tfor(int j=0;ind<M;j++){\n\t\t\t\tif(!used[val[j]]){\n\t\t\t\t\tstr[val[j]]='1';\n\t\t\t\t\tind++;\n\t\t\t\t}\n\t\t\t}\n\t\t\tprintf(\"?%s\\n\",str);\n\t\t\tfflush(stdout);\n\t\t\tscanf(\"%d\",&d);\n\t\t\tif(d)right=M;\n\t\t\telse left=M+1;\n\t\t}\n\t\tused[val[left]]=1;\n\t\tret[at++]=val[left]+1;\n\t}\n\tprintf(\"!\");\n\tfor(int i=0;i<b;i++){\n\t\tprintf(\"%d\",ret[i]);\n\t\tif(i<b-1)printf(\" \");\n\t\telse printf(\"\\n\");\n\t}\n\tfflush(stdout);\n}", "accuracy": 0.0975609756097561, "time_ms": 70, "memory_kb": 1280, "score_of_the_acc": -0.4205, "final_rank": 17 }, { "submission_id": "aoj_2277_650995", "code_snippet": "#include<stdio.h>\n#include<algorithm>\nusing namespace std;\nchar str[100000];\nint ret[10];\nint val[10000];\nint used[10000];\nint main(){\n\tint a,b;\n\tscanf(\"%d%d\",&a,&b);\n\tint at=0;\n\tif(a<200){\n\t\tfor(int i=0;i<a;i++){\n\t\t\tfor(int j=0;j<a;j++){\n\t\t\t\tif(i==j)str[j]='1';\n\t\t\t\telse str[j]='0';\n\t\t\t}\n\t\t\tprintf(\"?%s\\n\",str);\n\t\t\tfflush(stdout);\n\t\t\tint c;\n\t\t\tscanf(\"%d\",&c);\n\t\t\tif(c)ret[at++]=i+1;\n\t\t}\n\t\tprintf(\"!\");\n\t\tfor(int i=0;i<b;i++){\n\t\t\tprintf(\"%d\",ret[i]);\n\t\t\tif(i<b-1)printf(\" \");\n\t\t\telse printf(\"\\n\");\n\t\t}\n\t\tfflush(stdout);\n\t\treturn 0;\n\t}\n\tfor(int i=0;i<a;i++)val[i]=i;\n\tint n=a/2;\n\tfor(int i=0;i<b;i++){\n\t\tint d=0;\n\t\tdo{\n\t\t\tfor(int j=0;j<50000;j++){\n\t\t\t\tint l=rand()%a;\n\t\t\t\tint r=rand()%a;\n\t\t\t\tint z=val[l];\n\t\t\t\tval[l]=val[r];\n\t\t\t\tval[r]=z;\n\t\t\t}\n\t\t\tfor(int j=0;j<a;j++)str[j]='0';\n\t\t\tint ind=0;\n\t\t\tfor(int j=0;ind<n;j++){\n\t\t\t\tif(!used[val[j]]){\n\t\t\t\t\tstr[val[j]]='1';\n\t\t\t\t\tind++;\n\t\t\t\t}\n\t\t\t}\n\t\t\tprintf(\"?%s\\n\",str);\n\t\t\tfflush(stdout);\n\t\t\tscanf(\"%d\",&d);\n\t\t}while(!d);\n\t\tint left=0;\n\t\tint right=n;\n\t\twhile(left+1<right){\n\t\t\tint M=(left+right)/2;\n\t\t\tfor(int j=0;j<a;j++)str[j]='0';\n\t\t\tint ind=0;\n\t\t\tfor(int j=0;ind<M;j++){\n\t\t\t\tif(!used[val[j]]){\n\t\t\t\t\tstr[val[j]]='1';\n\t\t\t\t\tind++;\n\t\t\t\t}\n\t\t\t}\n\t\t\tprintf(\"?%s\\n\",str);\n\t\t\tfflush(stdout);\n\t\t\tscanf(\"%d\",&d);\n\t\t\tif(d)right=M;\n\t\t\telse left=M+1;\n\t\t}\n\t\tused[left]=1;\n\t\tret[at++]=left+1;\n\t}\n\tprintf(\"!\");\n\tfor(int i=0;i<b;i++){\n\t\tprintf(\"%d\",ret[i]);\n\t\tif(i<b-1)printf(\" \");\n\t\telse printf(\"\\n\");\n\t}\n\tfflush(stdout);\n}", "accuracy": 0.0975609756097561, "time_ms": 60, "memory_kb": 1280, "score_of_the_acc": -0.4096, "final_rank": 14 }, { "submission_id": "aoj_2277_523648", "code_snippet": "#include <cstdio>\n#include <iostream>\n#include <sstream>\n#include <fstream>\n#include <iomanip>\n#include <algorithm>\n#include <cmath>\n#include <string>\n#include <vector>\n#include <list>\n#include <queue>\n#include <stack>\n#include <set>\n#include <map>\n#include <bitset>\n#include <numeric>\n#include <climits>\n#include <cfloat>\nusing namespace std;\n\nunsigned long xor128(){\n static unsigned long x=123456789,y=362436069,z=521288629,w=88675123;\n unsigned long t;\n t=(x^(x<<11));x=y;y=z;z=w; return( w=(w^(w>>19))^(t^(t>>8)) );\n}\n\nint n;\n\nint func(bitset<10000> query)\n{\n cout << '?';\n for(int i=0; i<n; ++i){\n if(query[i])\n cout << '1';\n else\n cout << '0';\n }\n cout << endl;\n\n int ret;\n cin >> ret;\n return ret;\n}\n\n\nint main()\n{\n int k;\n cin >> n >> k;\n\n bitset<10000> ret;\n\n while(--k >= 0){\n int m = 0;\n bitset<10000> query;\n for(int i=0; i<n; ++i){\n query[i] = (xor128() % 2 == 1) && !ret[i];\n if(query[i])\n ++ m;\n }\n\n if(func(query) == 0){\n ++ k;\n continue;\n }\n\n while(m > 1){\n bitset<10000> query2;\n int m2 = 0;\n for(int i=0; i<n; ++i){\n if(query[i] && m2 < m / 2){\n query2[i] = true;\n ++ m2;\n }\n }\n\n if(func(query2) == 1){\n query = query2;\n m = m2;\n }else{\n query ^= query2;\n m -= m2;\n }\n }\n\n ret |= query;\n }\n\n bool first = true;\n for(int i=0; i<n; ++i){\n if(ret[i]){\n if(first){\n cout << '!' << (i+1);\n first = false;\n }else{\n cout << ' ' << (i+1);\n }\n }\n }\n cout << endl;\n\n return 0;\n}", "accuracy": 1, "time_ms": 120, "memory_kb": 860, "score_of_the_acc": -0.3547, "final_rank": 4 }, { "submission_id": "aoj_2277_467848", "code_snippet": "#include <iostream>\n#include <iomanip>\n#include <sstream>\n#include <cstdio>\n#include <string>\n#include <vector>\n#include <algorithm>\n#include <complex>\n#include <cstring>\n#include <cstdlib>\n#include <cmath>\n#include <cassert>\n#include <climits>\n#include <queue>\n#include <set>\n#include <map>\n#include <valarray>\n#include <bitset>\n#include <stack>\nusing namespace std;\n\n#define REP(i,n) for(int i=0;i<(int)n;++i)\n#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)\n#define ALL(c) (c).begin(), (c).end()\ntypedef long long ll;\ntypedef pair<int,int> pii;\nconst int INF = 1<<29;\nconst double PI = acos(-1);\nconst double EPS = 1e-8;\n\nbool ok[10000];\nbool t[10000];\n\nint n, k;\n\nvoid get1() {\n memset(t,0,sizeof(t));\n while(1) { \n REP(i,n) if (!ok[i]) t[i] = rand()%2;\n printf(\"?\");\n REP(i,n) printf(\"%d\",t[i]);\n puts(\"\"); fflush(stdout);\n int v;\n scanf(\"%d\",&v);\n if (v) return;\n }\n}\n\nbool check(int a, int b) {\n printf(\"?\"); \n REP(i,n) printf(\"%d\",(i>=a&&i<b)?t[i]:0);\n puts(\"\"); fflush(stdout);\n int v;\n scanf(\"%d\", &v);\n return v;\n}\n\nint main() {\n scanf(\"%d%d\",&n,&k);\n vector<int> v;\n REP(i,k) {\n int a = 0, b = n;\n get1();\n while(a+1<b) {\n int mid = (a+b)/2;\n if (check(a, mid)) b = mid;\n else a = mid;\n }\n ok[a] = 1;\n v.push_back(a+1);\n }\n printf(\"!\");\n REP(i,v.size()) {\n if (i) printf(\" \");\n printf(\"%d\", v[i]);\n }\n puts(\"\");\n fflush(stdout);\n}", "accuracy": 1, "time_ms": 170, "memory_kb": 952, "score_of_the_acc": -0.4354, "final_rank": 7 }, { "submission_id": "aoj_2277_331928", "code_snippet": "#include<cstdio>\n#include<cstdlib>\n#include<algorithm>\n\n#define rep(i,n) for(int i=0;i<(n);i++)\n\nusing namespace std;\n\nint dfs(int l,int r,int n,const int *bit){\n\tif(l==r-1) return l;\n\n\tputchar('?');\n\trep(i,n){\n\t\tif (l<=i && i<(l+r)/2) putchar('0'+bit[i]);\n\t\telse if(i<=(l+r)/2 && i<r) putchar('0');\n\t\telse putchar('0');\n\t}\n\tputchar('\\n'); fflush(stdout);\n\n\tint v; scanf(\"%d\",&v);\n\treturn v?dfs(l,(l+r)/2,n,bit):dfs((l+r)/2,r,n,bit);\n}\n\nint main(){\n\tsrand(777);\n\tint n,k; scanf(\"%d%d\",&n,&k);\n\tint ans[10];\n\trep(t,k){\n\t\tint bit[10000];\n\t\twhile(1){\n\t\t\tputchar('?');\n\t\t\trep(i,n){\n\t\t\t\tif(count(ans,ans+t,i)) bit[i]=0;\n\t\t\t\telse bit[i]=rand()&1;\n\t\t\t\tputchar('0'+bit[i]);\n\t\t\t}\n\t\t\tputchar('\\n'); fflush(stdout);\n\n\t\t\tint v; scanf(\"%d\",&v);\n\t\t\tif(v) break;\n\t\t}\n\n\t\tans[t]=dfs(0,n,n,bit);\n\t}\n\n\tprintf(\"!\");\n\trep(t,k) printf(\"%d%c\",ans[t]+1,t<k-1?' ':'\\n');\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 780, "score_of_the_acc": -0.2557, "final_rank": 3 }, { "submission_id": "aoj_2277_301280", "code_snippet": "#include <cstdio>\n#include <cstdlib>\n#include <queue>\n#include <ctime>\n#define REP(i,n) for(int i=0; i<(int)(n); i++)\n\ninline int getInt(){ int s; scanf(\"%d\", &s); return s; }\n\nusing namespace std;\n\nint x[10000];\nint n, k;\nint ans[10];\n\nint query(){\n printf(\"?\");\n REP(i,n) printf(\"%d\", x[i]);\n printf(\"\\n\");\n fflush(stdout);\n return getInt();\n}\n\nint main(){\n srand(time(NULL));\n\n n = getInt();\n k = getInt();\n\n REP(i, k){\n do{\n // Random\n REP(j, n) x[j] = rand() % 2;\n REP(j, i) x[ans[j] - 1] = 0;\n }while(query() == 0);\n\n vector<int> ones;\n REP(j, n) if(x[j] == 1) ones.push_back(j);\n\n while(ones.size() != 1){\n int m = ones.size() / 2;\n\n REP(j, n) x[j] = 0;\n REP(j, m) x[ones[j]] = 1;\n\n if(query() == 1){\n\tones = vector<int>(ones.begin(), ones.begin() + m);\n }else{\n\tones = vector<int>(ones.begin() + m, ones.end());\n }\n }\n\n ans[i] = ones[0] + 1;\n }\n\n printf(\"!\");\n REP(i,k) printf(\"%d%c\", ans[i], i == k - 1 ? '\\n' : ' ');\n fflush(stdout);\n\n return 0;\n}", "accuracy": 1, "time_ms": 160, "memory_kb": 924, "score_of_the_acc": -0.4165, "final_rank": 5 }, { "submission_id": "aoj_2277_268966", "code_snippet": "#include <stdio.h>\n#include <string.h>\n#include <algorithm>\n#include <iostream>\n#include <math.h>\n#include <assert.h>\n#include <vector>\n\nusing namespace std;\ntypedef long long ll;\ntypedef unsigned int uint;\ntypedef unsigned long long ull;\nstatic const double EPS = 1e-9;\nstatic const double PI = acos(-1.0);\n\n#define REP(i, n) for (int i = 0; i < (int)(n); i++)\n#define FOR(i, s, n) for (int i = (s); i < (int)(n); i++)\n#define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++)\n#define FORIT(it, c) for (__typeof((c).begin())it = (c).begin(); it != (c).end(); it++)\n#define MEMSET(v, h) memset((v), h, sizeof(v))\n\nint n, k;\nbool exist[10010];\nbool target[10010];\nchar output[10010];\n\nint Communicate() {\n printf(\"%s\\n\", output);\n fflush(stdout);\n int v;\n scanf(\"%d\", &v);\n return v;\n}\n\nint UpdateTarget(int c) {\n int index = -1;\n REP(i, n) {\n if (!target[i]) { continue; }\n if (output[i + 1] == '0' + c) { target[i] = false; }\n if (index == -2 || !target[i]) { continue; }\n else if (index == -1) { index = i; }\n else { index = -2; }\n }\n assert(index != -1);\n return index;\n}\n\nvoid RandomSequence() {\n output[0] = '?';\n REP(i, n) {\n if (!target[i]) { output[i + 1] = '0'; }\n else { output[i + 1] = '0' + (rand() % 2 ? 1 : 0); }\n }\n output[n + 1] = '\\0';\n}\n\nint main() {\n scanf(\"%d %d\", &n, &k);\n MEMSET(exist, false);\n REP(i, k) {\n REP(j, n) { target[j] = !exist[j]; }\n while (true) {\n RandomSequence();\n if (Communicate()) { break; }\n }\n int prev = 0;\n while (true) {\n int index = UpdateTarget(prev);\n if (index >= 0) {\n exist[index] = true;\n break;\n }\n RandomSequence();\n prev = Communicate() == 0 ? 1 : 0;\n }\n }\n\n vector<int> ans;\n REP(i, n) { if (exist[i]) { ans.push_back(i + 1); } }\n printf(\"!\");\n REP(i, k) {\n if (i != 0) {\n putchar(' ');\n }\n printf(\"%d\", ans[i]);\n }\n puts(\"\");\n fflush(stdout);\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 0, "score_of_the_acc": -0.0109, "final_rank": 2 }, { "submission_id": "aoj_2277_253612", "code_snippet": "#include<cstdio>\n#include<vector>\n#include<algorithm>\nusing namespace std;\n\nint main(){\n int n,k,v;\n int x[10000],a[10];\n bool use[10000];\n\n scanf(\"%d %d\",&n,&k);\n\n if(n==k){\n for(int i=0;i<k;i++)a[i] = i;\n }else{\n for(int i=0;i<n;i++)use[i] = false;\n while(k--){\n for(int i=0;i<n;i++)x[i] = 1&i;\n while(1){\n\tbool f = true;\n\tfor(int i=0;i<n;i++){\n\t if(use[i] && x[i])f = false;\n\t}\n\tif(f){\n\t printf(\"?\");\n\t for(int i=0;i<n;i++)printf(\"%d\",x[i]);\n\t printf(\"\\n\");\n\t fflush(stdout);\n\t scanf(\"%d\",&v);\n\t if(v)break;\n\t}\n\trandom_shuffle(x,x+n);\n }\n vector<int> cand;\n for(int i=0;i<n;i++){\n\tif(x[i])cand.push_back(i);\n }\n\n while(cand.size()!=1){\n\tvector<int> c1,c2;\n\tfor(int i=0;i<(int)cand.size()/2;i++)c1.push_back(cand[i]);\n\tfor(int i=(int)cand.size()/2;i<(int)cand.size();i++)c2.push_back(cand[i]);\n\tcand.clear();\n\n\tfor(int i=0;i<n;i++)x[i] = 0;\n\tfor(int i=0;i<(int)c1.size();i++)x[c1[i]] = 1;\n\n\tprintf(\"?\");\n\tfor(int i=0;i<n;i++)printf(\"%d\",x[i]);\n\tprintf(\"\\n\");\n\tfflush(stdout);\n\tscanf(\"%d\",&v);\n\tif(v){\n\t for(int i=0;i<(int)c1.size();i++)cand.push_back(c1[i]);\n\t}else{\n\t for(int i=0;i<(int)c2.size();i++)cand.push_back(c2[i]);\n\t}\n }\n use[cand[0]] = true;\n }\n k = 0;\n for(int i=0;i<n;i++){\n if(use[i])a[k++] = i;\n }\n }\n\n printf(\"!%d\",a[0]+1);\n for(int i=1;i<k;i++)printf(\" %d\",a[i]+1);\n printf(\"\\n\");\n fflush(stdout);\n}", "accuracy": 1, "time_ms": 940, "memory_kb": 940, "score_of_the_acc": -1.2689, "final_rank": 13 }, { "submission_id": "aoj_2277_252476", "code_snippet": "#include <iostream>\n#include <algorithm>\n#include <vector>\n#include <stdio.h>\nusing namespace std;\n#define rep(i,n) for(int i = 0 ; i < n ; i++)\nint ac[10001];\n\nint last = -1;\nint suff[10001];\nint N,K;\n\nvector<int> answer(10);\n\nstring gen(vector<int> &g,int l,int r){\n\tstring w = string(N,'0');\n\tfor(int i = l ; i < r ; i++) if( !ac[ g[i] ] ) w[ g[i] ] = '1';\n\treturn w;\n}\n\nstring gen2(){\n\tstring w = string(N,'0');\n\tfor(int i = 0 ; i < N ; i++) if( !ac[i] ) w[i] = '0' + suff[i];\n\treturn w;\n}\n\nint get(string s){\n\tcout << \"?\" << s << endl;\n\tfflush(stdout);\n\tint ans = 0;\n\t//rep(i,K) ans += s[answer[i]] - '0';\n\tcin >> ans;\n\tfflush(stdout);\n\n\treturn ans%2;\n}\n\n\nint odd(vector<int> &g,int l ,int r){\n\tif(l+1 == r){\n\t\tac[g[l]] = true;\n\t\tlast = g[l];\n\t\treturn 0;\n\t}\n\tint m = (l+r)/2;\n\tif( get(gen(g,l,m)) == 1){\n\t\todd(g,l,m);\n\t}else{\n\t\todd(g,m,r);\n\t}\n}\nint even(){\n\tfor(int i = 0 ; i < 200 ; i++){\n\t\tif(get(gen2()) == 1) break;\n\t\trandom_shuffle(suff,suff+N);\n\t}\n\tvector<int> a;\n\tstring w = gen2();\n\trep(i,N) if(w[i] == '1') a.push_back(i);\n\todd(a,0,a.size());\n}\nint main(){\n\tint k;\n\tcin >> N >> k;\n\t//K = k;\n\t//rep(i,K) cin >> answer[i];\n\tvector<int> awawa;\n\tfor(int i = 0 ; i < N ; i++) awawa.push_back(i);\n\tfor(int i = 0 ; i < N ; i++) suff[i] = i%2;\n\twhile(k){\n\t\tif(k%2){\n\t\t\todd(awawa,0,awawa.size());\n\t\t}else{\n\t\t\teven();\t\n\t\t}\n\t\tawawa.erase(remove(awawa.begin(),awawa.end(),last),awawa.end());\n\t\tk--;\n\t}\n\tvector<int> ans;\n\trep(i,N) if(ac[i])ans.push_back(i+1);\n\tcout << \"!\";\n\trep(i,ans.size()) cout << (i?\" \":\"\") << ans[i];\n\tcout << endl;\n\tfflush(stdout);\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 0, "score_of_the_acc": 0, "final_rank": 1 } ]
aoj_2283_cpp
Problem C: Seishun 18 Kippu R大学のとある学生sirokurostoneはA津大学で行われる合宿に参加しようとしていた。 他のメンバーは新幹線を使用する予定なのだが、sirokurostoneは青春18切符を使って向かおうとしていた。 同じく青春18切符使いのとある2D好きな人物も合宿に参加しようとしていた。 どうせならと一緒に行こうと考えたsirokurostoneは途中の駅で2D好きな彼を拾って向かおうと考えた。 sirokurostoneはR大学を出る前に遅延状況を確認してすこしでも時間のかからないルートを通ってA津大学のある駅に着きたいと考えている。 sirokurostoneはルートが確定した後に2D好きな彼にもその情報を伝えるつもりである。 しかし、sirokurostoneは遅延状況を見てもどのルートを通れば早くA津大学のある駅に到着できるか分からず困っている。 そこで、あなたの仕事はsirokurostoneに代わって遅延状況からどれくらいでA津大学のある駅に着くのかを求めることである。 各駅間には、駅間の距離と遅延予想時間がある。 電車は動き始めてから40km/hを維持するものとする。 地点aと地点bの間の移動時間は、 距離/40+遅延予想時間 で求めることができる。 各駅での停車時間は無視してよいものとする。 Input 複数のデータセットの並びが入力として与えられる。 データセット数は50以下であることが保障されている。 各データセットは、次の形式をしている。 n m s p g a 1 b 1 d 1 t 1 ... a i b i d i t i ... a m b m d m t m 整数 n ( 3 ≤ n ≤ 500 )と m ( 2 ≤ m ≤ 5000 )は、それぞれ駅の数、各駅間の線路の数の合計を表す。 s 、 p 、 g は、それぞれsirokurostoneが乗る駅、2D好きな人物が乗る駅、A津大学がある駅の場所を示す文字列である。 s 、 p 、 g はそれぞれ違う文字列である。 続く m 行は、駅間情報を表す。 a i 、 b i は、線路がつなぐ駅を示す文字列である。 a i と b i は、双方向に行き来することができる。 a i と b i が一致することはない。 整数 d i ( 40 ≤ d i ≤ 10000 )は、 a i と b i の距離を表す。 d i は40の倍数であることが保証されている。 整数 t i ( 0 ≤ t i ≤ 20 )は、 a i と b i の間の遅延予想時間を示す。 駅名を表す文字列は、全てアルファベットの小文字および大文字からなる20文字以下の文字列であらわされる。 駅と駅の間の線路は高々1本である。 入力の終わりは2つのゼロを含む行で表される。 Output 入力データセットごとに、A津大学がある駅への到達時間(単位:時間)を出力せよ。 sirokurostoneが乗る駅から2D好きな人が乗る駅までの経路と、2D好きな人物が乗る駅からA津大学がある駅の場所までの経路があることが保証されている。 Sample Input 4 4 A B G A B 40 3 B C 80 0 A G 40 0 B G 80 0 5 6 Kusatsu Tokyo Aizu Tokyo Nagoya 120 3 Kusatsu Nagoya 40 0 Kusatsu Kanazawa 40 0 Kanazawa Aizu 40 0 Tokyo Kanazawa 40 0 Tokyo Aizu 80 4 0 0 Output for Sample Input 5 4
[ { "submission_id": "aoj_2283_9847642", "code_snippet": "#include <iostream>\n#include <vector>\n#include <map>\n#include <set>\n\nusing namespace std;\nusing ll = long long;\n\nconst ll INF = static_cast<ll>(1) << 62;\n\nvoid solve() {\n ll n, m; cin >> m;\n string s, t, f; cin >> s >> t >> f;\n map<string, vector<pair<string, ll>>>g;\n map<string, ll>d;\n for (int i = 0; i < m; i++) {\n string a, b; cin >> a >> b;\n d[a] = INF;\n d[b] = INF;\n ll d; cin >> d;\n ll t; cin >> t;\n g[a].emplace_back(b, d / 40 + t);\n g[b].emplace_back(a, d / 40 + t);\n }\n set<pair<ll, string>>q;\n q.insert({0, t});\n d[t] = 0;\n while (!q.empty()) {\n ll val = q.begin() -> first;\n string v = q.begin() -> second;\n q.erase(q.begin());\n for (auto [to, w] : g[v]) {\n if (d[v] + w < d[to]) {\n q.erase({d[to], to});\n d[to] = d[v] + w;\n q.insert({d[to], to});\n }\n }\n }\n cout << d[s] + d[f] << '\\n';\n}\n\nint main() {\n ll T; cin >> T;\n while (T != 0) {\n solve();\n cin >> T;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 4092, "score_of_the_acc": -0.7611, "final_rank": 14 }, { "submission_id": "aoj_2283_7831713", "code_snippet": "#pragma GCC optimize(\"Ofast\")\n#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n\nmt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());\nll myRand(ll B) {\n return (ull)rng() % B;\n}\ninline double time() {\n return static_cast<long double>(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count()) * 1e-9;\n}\n\ntemplate<typename T>\nstruct Dijkstra{\n const T inf=numeric_limits<T>::max();\n using P=pair<T,int>;\n int n;\n vector<vector<pair<int,T>>> g;\n vector<T> d;\n Dijkstra(int n):n(n),g(n),d(n){}\n void add_edge(int u,int v,T w){\n g[u].emplace_back(v,w);\n g[v].emplace_back(u,w);\n }\n vector<T> build(int s){\n for(int i=0;i<n;i++){\n d[i]=inf;\n }\n d[s]=0;\n priority_queue<P,vector<P>,greater<P>> pq;\n pq.emplace(d[s],s);\n while(pq.size()){\n P p=pq.top(); pq.pop();\n int v=p.second;\n if(d[v]<p.first)continue;\n for(auto &e:g[v]){\n int u=e.first; T c=e.second;\n if(d[u]>d[v]+c){\n d[u]=d[v]+c;\n pq.emplace(d[u],u);\n }\n }\n }\n return d;\n }\n};\n\nint main(){\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n int n,m;\n while(cin >> n >> m, n) {\n map<string,int> mp;\n auto id = [&]() -> int {\n string s; cin >> s;\n if (mp.find(s) != mp.end()) {\n return mp[s];\n }\n else {\n int i = mp.size();\n mp[s] = i;\n return i;\n }\n };\n int a = id(), b = id(), c = id();\n Dijkstra<int> g(n);\n for (int i = 0; i < m; ++i) {\n int x = id(), y = id();\n int d,t; cin >> d >> t;\n g.add_edge(x, y, d/40+t);\n }\n cout << g.build(a)[b] + g.build(b)[c] << \"\\n\";\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3392, "score_of_the_acc": -0.5291, "final_rank": 2 }, { "submission_id": "aoj_2283_5031728", "code_snippet": "#line 2 \"/home/yuruhiya/programming/library/template/template.cpp\"\n#include <bits/stdc++.h>\n#line 4 \"/home/yuruhiya/programming/library/template/constants.cpp\"\n#include <string_view>\n#line 7 \"/home/yuruhiya/programming/library/template/constants.cpp\"\n\n#define rep(i, n) for (int i = 0; i < (n); ++i)\n#define FOR(i, m, n) for (int i = (m); i < (n); ++i)\n#define rrep(i, n) for (int i = (n)-1; i >= 0; --i)\n#define rfor(i, m, n) for (int i = (m); i >= (n); --i)\n#define unless(c) if (!(c))\n#define all(x) (x).begin(), (x).end()\n#define rall(x) (x).rbegin(), (x).rend()\n#define range_it(a, l, r) (a).begin() + (l), (a).begin() + (r)\n\nusing namespace std;\nusing ll = long long;\nusing LD = long double;\nusing VB = vector<bool>;\nusing VVB = vector<VB>;\nusing VI = vector<int>;\nusing VVI = vector<VI>;\nusing VL = vector<ll>;\nusing VVL = vector<VL>;\nusing VS = vector<string>;\nusing VD = vector<LD>;\nusing PII = pair<int, int>;\nusing VP = vector<PII>;\nusing PLL = pair<ll, ll>;\nusing VPL = vector<PLL>;\ntemplate <class T> using PQ = priority_queue<T>;\ntemplate <class T> using PQS = priority_queue<T, vector<T>, greater<T>>;\nconstexpr int inf = 1000000000;\nconstexpr long long inf_ll = 1000000000000000000ll, MOD = 1000000007;\nconstexpr long double PI = 3.14159265358979323846, EPS = 1e-12;\nnamespace CharacterClass {\n\tconstexpr string_view\n\t digit = \"0123456789\",\n\t xdigit = \"0123456789ABCDEFabcdef\", lower = \"abcdefghijklmnopqrstuvwxyz\",\n\t upper = \"ABCDEFGHIJKLMNOPQRSTUVWXYZ\",\n\t alpha = \"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz\",\n\t alnum = \"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz\",\n\t word = \"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz\",\n\t punct = R\"(!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~)\",\n\t graph =\n\t R\"(!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~)\",\n\t print =\n\t R\"( !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~)\",\n\t blank = \" \\t\", space = \" \\t\\n\\r\\f\\v\";\n} // namespace CharacterClass\n#line 7 \"/home/yuruhiya/programming/library/template/Input.cpp\"\nusing namespace std;\n\n#ifdef _WIN32\n#define getchar_unlocked _getchar_nolock\n#define putchar_unlocked _putchar_nolock\n#define fwrite_unlocked fwrite\n#define fflush_unlocked fflush\n#endif\nclass Scanner {\n\tstatic int gc() {\n\t\treturn getchar_unlocked();\n\t}\n\tstatic char next_char() {\n\t\tchar c;\n\t\tread(c);\n\t\treturn c;\n\t}\n\ttemplate <class T> static void read(T& v) {\n\t\tcin >> v;\n\t}\n\tstatic void read(char& v) {\n\t\twhile (isspace(v = gc()))\n\t\t\t;\n\t}\n\tstatic void read(bool& v) {\n\t\tv = next_char() != '0';\n\t}\n\tstatic void read(string& v) {\n\t\tv.clear();\n\t\tfor (char c = next_char(); !isspace(c); c = gc()) v += c;\n\t}\n\tstatic void read(int& v) {\n\t\tv = 0;\n\t\tbool neg = false;\n\t\tchar c = next_char();\n\t\tif (c == '-') {\n\t\t\tneg = true;\n\t\t\tc = gc();\n\t\t}\n\t\tfor (; isdigit(c); c = gc()) v = v * 10 + (c - '0');\n\t\tif (neg) v = -v;\n\t}\n\tstatic void read(long long& v) {\n\t\tv = 0;\n\t\tbool neg = false;\n\t\tchar c = next_char();\n\t\tif (c == '-') {\n\t\t\tneg = true;\n\t\t\tc = gc();\n\t\t}\n\t\tfor (; isdigit(c); c = gc()) v = v * 10 + (c - '0');\n\t\tif (neg) v = -v;\n\t}\n\tstatic void read(double& v) {\n\t\tv = 0;\n\t\tdouble dp = 1;\n\t\tbool neg = false, after_dp = false;\n\t\tchar c = next_char();\n\t\tif (c == '-') {\n\t\t\tneg = true;\n\t\t\tc = gc();\n\t\t}\n\t\tfor (; isdigit(c) || c == '.'; c = gc()) {\n\t\t\tif (c == '.') {\n\t\t\t\tafter_dp = true;\n\t\t\t} else if (after_dp) {\n\t\t\t\tv += (c - '0') * (dp *= 0.1);\n\t\t\t} else {\n\t\t\t\tv = v * 10 + (c - '0');\n\t\t\t}\n\t\t}\n\t\tif (neg) v = -v;\n\t}\n\tstatic void read(long double& v) {\n\t\tv = 0;\n\t\tlong double dp = 1;\n\t\tbool neg = false, after_dp = false;\n\t\tchar c = next_char();\n\t\tif (c == '-') {\n\t\t\tneg = true;\n\t\t\tc = gc();\n\t\t}\n\t\tfor (; isdigit(c) || c == '.'; c = gc()) {\n\t\t\tif (c == '.') {\n\t\t\t\tafter_dp = true;\n\t\t\t} else if (after_dp) {\n\t\t\t\tv += (c - '0') * (dp *= 0.1);\n\t\t\t} else {\n\t\t\t\tv = v * 10 + (c - '0');\n\t\t\t}\n\t\t}\n\t\tif (neg) v = -v;\n\t}\n\ttemplate <class T, class U> static void read(pair<T, U>& v) {\n\t\tread(v.first);\n\t\tread(v.second);\n\t}\n\ttemplate <class T> static void read(vector<T>& v) {\n\t\tfor (auto& e : v) read(e);\n\t}\n\ttemplate <size_t N = 0, class T> static void read_tuple_impl(T& v) {\n\t\tif constexpr (N < tuple_size_v<T>) {\n\t\t\tread(get<N>(v));\n\t\t\tread_tuple_impl<N + 1>(v);\n\t\t}\n\t}\n\ttemplate <class... T> static void read(tuple<T...>& v) {\n\t\tread_tuple_impl(v);\n\t}\n\tstruct ReadVectorHelper {\n\t\tsize_t n;\n\t\tReadVectorHelper(size_t _n) : n(_n) {}\n\t\ttemplate <class T> operator vector<T>() {\n\t\t\tvector<T> v(n);\n\t\t\tread(v);\n\t\t\treturn v;\n\t\t}\n\t};\n\tstruct Read2DVectorHelper {\n\t\tsize_t n, m;\n\t\tRead2DVectorHelper(const pair<size_t, size_t>& nm) : n(nm.first), m(nm.second) {}\n\t\ttemplate <class T> operator vector<vector<T>>() {\n\t\t\tvector<vector<T>> v(n, vector<T>(m));\n\t\t\tread(v);\n\t\t\treturn v;\n\t\t}\n\t};\n\npublic:\n\tstring read_line() const {\n\t\tstring v;\n\t\tfor (char c = gc(); c != '\\n' && c != '\\0'; c = gc()) v += c;\n\t\treturn v;\n\t}\n\ttemplate <class T> T read() const {\n\t\tT v;\n\t\tread(v);\n\t\treturn v;\n\t}\n\ttemplate <class T> vector<T> read_vector(size_t n) const {\n\t\tvector<T> a(n);\n\t\tread(a);\n\t\treturn a;\n\t}\n\ttemplate <class T> operator T() const {\n\t\treturn read<T>();\n\t}\n\tint operator--(int) const {\n\t\treturn read<int>() - 1;\n\t}\n\tReadVectorHelper operator[](size_t n) const {\n\t\treturn ReadVectorHelper(n);\n\t}\n\tRead2DVectorHelper operator[](const pair<size_t, size_t>& nm) const {\n\t\treturn Read2DVectorHelper(nm);\n\t}\n\tvoid operator()() const {}\n\ttemplate <class H, class... T> void operator()(H&& h, T&&... t) const {\n\t\tread(h);\n\t\toperator()(forward<T>(t)...);\n\t}\n\nprivate:\n\ttemplate <template <class...> class, class...> struct Multiple;\n\ttemplate <template <class...> class V, class Head, class... Tail>\n\tstruct Multiple<V, Head, Tail...> {\n\t\ttemplate <class... Args> using vec = V<vector<Head>, Args...>;\n\t\tusing type = typename Multiple<vec, Tail...>::type;\n\t};\n\ttemplate <template <class...> class V> struct Multiple<V> { using type = V<>; };\n\ttemplate <class... T> using multiple_t = typename Multiple<tuple, T...>::type;\n\ttemplate <size_t N = 0, class T> void multiple_impl(T& t) const {\n\t\tif constexpr (N < tuple_size_v<T>) {\n\t\t\tauto& vec = get<N>(t);\n\t\t\tusing V = typename remove_reference_t<decltype(vec)>::value_type;\n\t\t\tvec.push_back(read<V>());\n\t\t\tmultiple_impl<N + 1>(t);\n\t\t}\n\t}\n\npublic:\n\ttemplate <class... T> auto multiple(size_t h) const {\n\t\tmultiple_t<T...> result;\n\t\twhile (h--) multiple_impl(result);\n\t\treturn result;\n\t}\n} in;\n#define inputs(T, ...) \\\n\tT __VA_ARGS__; \\\n\tin(__VA_ARGS__)\n#define ini(...) inputs(int, __VA_ARGS__)\n#define inl(...) inputs(long long, __VA_ARGS__)\n#define ins(...) inputs(string, __VA_ARGS__)\n#line 7 \"/home/yuruhiya/programming/library/template/Output.cpp\"\n#include <charconv>\n#line 10 \"/home/yuruhiya/programming/library/template/Output.cpp\"\nusing namespace std;\n\nstruct BoolStr {\n\tconst char *t, *f;\n\tBoolStr(const char* _t, const char* _f) : t(_t), f(_f) {}\n} Yes(\"Yes\", \"No\"), yes(\"yes\", \"no\"), YES(\"YES\", \"NO\"), Int(\"1\", \"0\");\nstruct DivStr {\n\tconst char *d, *l;\n\tDivStr(const char* _d, const char* _l) : d(_d), l(_l) {}\n} spc(\" \", \"\\n\"), no_spc(\"\", \"\\n\"), end_line(\"\\n\", \"\\n\"), comma(\",\", \"\\n\"),\n no_endl(\" \", \"\");\nclass Output {\n\tBoolStr B{Yes};\n\tDivStr D{spc};\n\npublic:\n\tvoid put(int v) const {\n\t\tchar buf[12]{};\n\t\tif (auto [ptr, e] = to_chars(begin(buf), end(buf), v); e == errc{}) {\n\t\t\tfwrite(buf, sizeof(char), ptr - buf, stdout);\n\t\t} else {\n\t\t\tassert(false);\n\t\t}\n\t}\n\tvoid put(long long v) const {\n\t\tchar buf[21]{};\n\t\tif (auto [ptr, e] = to_chars(begin(buf), end(buf), v); e == errc{}) {\n\t\t\tfwrite(buf, sizeof(char), ptr - buf, stdout);\n\t\t} else {\n\t\t\tassert(false);\n\t\t}\n\t}\n\tvoid put(bool v) const {\n\t\tput(v ? B.t : B.f);\n\t}\n\tvoid put(vector<bool>::reference v) const {\n\t\tput(v ? B.t : B.f);\n\t}\n\tvoid put(char v) const {\n\t\tputchar_unlocked(v);\n\t}\n\tvoid put(const char* v) const {\n\t\tfwrite_unlocked(v, 1, strlen(v), stdout);\n\t}\n\tvoid put(double v) const {\n\t\tprintf(\"%.20f\", v);\n\t}\n\tvoid put(long double v) const {\n\t\tprintf(\"%.20Lf\", v);\n\t}\n\ttemplate <class T> void put(const T& v) const {\n\t\tcout << v;\n\t}\n\ttemplate <class T, class U> void put(const pair<T, U>& v) const {\n\t\tput(v.first);\n\t\tput(D.d);\n\t\tput(v.second);\n\t}\n\ttemplate <class InputIterater>\n\tvoid put_range(const InputIterater& begin, const InputIterater& end) const {\n\t\tfor (InputIterater i = begin; i != end; ++i) {\n\t\t\tif (i != begin) put(D.d);\n\t\t\tput(*i);\n\t\t}\n\t}\n\ttemplate <class T> void put(const vector<T>& v) const {\n\t\tput_range(v.begin(), v.end());\n\t}\n\ttemplate <class T, size_t N> void put(const array<T, N>& v) const {\n\t\tput_range(v.begin(), v.end());\n\t}\n\ttemplate <class T> void put(const vector<vector<T>>& v) const {\n\t\tfor (size_t i = 0; i < v.size(); ++i) {\n\t\t\tif (i) put(D.l);\n\t\t\tput(v[i]);\n\t\t}\n\t}\n\n\tOutput() = default;\n\tOutput(const BoolStr& _boolstr, const DivStr& _divstr) : B(_boolstr), D(_divstr) {}\n\tOutput& operator()() {\n\t\tput(D.l);\n\t\treturn *this;\n\t}\n\ttemplate <class H> Output& operator()(H&& h) {\n\t\tput(h);\n\t\tput(D.l);\n\t\treturn *this;\n\t}\n\ttemplate <class H, class... T> Output& operator()(H&& h, T&&... t) {\n\t\tput(h);\n\t\tput(D.d);\n\t\treturn operator()(forward<T>(t)...);\n\t}\n\ttemplate <class InputIterator>\n\tOutput& range(const InputIterator& begin, const InputIterator& end) {\n\t\tput_range(begin, end);\n\t\tput(D.l);\n\t\treturn *this;\n\t}\n\ttemplate <class T> Output& range(const T& a) {\n\t\trange(a.begin(), a.end());\n\t\treturn *this;\n\t}\n\ttemplate <class... T> void exit(T&&... t) {\n\t\toperator()(forward<T>(t)...);\n\t\tstd::exit(EXIT_SUCCESS);\n\t}\n\tOutput& flush() {\n\t\tfflush_unlocked(stdout);\n\t\treturn *this;\n\t}\n\tOutput& set(const BoolStr& b) {\n\t\tB = b;\n\t\treturn *this;\n\t}\n\tOutput& set(const DivStr& d) {\n\t\tD = d;\n\t\treturn *this;\n\t}\n\tOutput& set(const char* t, const char* f) {\n\t\tB = BoolStr(t, f);\n\t\treturn *this;\n\t}\n} out;\n#line 3 \"/home/yuruhiya/programming/library/template/Step.cpp\"\nusing namespace std;\n\ntemplate <class T> struct Step {\n\tusing value_type = T;\n\n\tclass iterator {\n\t\tvalue_type a, b, c;\n\n\tpublic:\n\t\tconstexpr iterator() : a(value_type()), b(value_type()), c(value_type()) {}\n\t\tconstexpr iterator(value_type _b, value_type _c, value_type _s)\n\t\t : a(_b), b(_c), c(_s) {}\n\t\tconstexpr iterator& operator++() {\n\t\t\t--b;\n\t\t\ta += c;\n\t\t\treturn *this;\n\t\t}\n\t\tconstexpr iterator operator++(int) {\n\t\t\titerator tmp = *this;\n\t\t\t--b;\n\t\t\ta += c;\n\t\t\treturn tmp;\n\t\t}\n\t\tconstexpr const value_type& operator*() const {\n\t\t\treturn a;\n\t\t}\n\t\tconstexpr const value_type* operator->() const {\n\t\t\treturn &a;\n\t\t}\n\t\tconstexpr bool operator==(const iterator& i) const {\n\t\t\treturn b == i.b;\n\t\t}\n\t\tconstexpr bool operator!=(const iterator& i) const {\n\t\t\treturn !(b == i.b);\n\t\t}\n\t\tconstexpr value_type start() const {\n\t\t\treturn a;\n\t\t}\n\t\tconstexpr value_type size() const {\n\t\t\treturn b;\n\t\t}\n\t\tconstexpr value_type step() const {\n\t\t\treturn c;\n\t\t}\n\t};\n\tconstexpr Step(value_type b, value_type c, value_type s) : be(b, c, s) {}\n\tconstexpr iterator begin() const {\n\t\treturn be;\n\t}\n\tconstexpr iterator end() const {\n\t\treturn en;\n\t}\n\tconstexpr value_type start() const {\n\t\treturn be.start();\n\t}\n\tconstexpr value_type size() const {\n\t\treturn be.size();\n\t}\n\tconstexpr value_type step() const {\n\t\treturn be.step();\n\t}\n\tconstexpr value_type sum() const {\n\t\treturn start() * size() + step() * (size() * (size() - 1) / 2);\n\t}\n\toperator vector<value_type>() const {\n\t\treturn to_a();\n\t}\n\tauto to_a() const {\n\t\tvector<value_type> result;\n\t\tresult.reserve(size());\n\t\tfor (auto i : *this) {\n\t\t\tresult.push_back(i);\n\t\t}\n\t\treturn result;\n\t}\n\nprivate:\n\titerator be, en;\n};\ntemplate <class T> constexpr auto step(T a) {\n\treturn Step<T>(0, a, 1);\n}\ntemplate <class T> constexpr auto step(T a, T b) {\n\treturn Step<T>(a, b - a, 1);\n}\ntemplate <class T> constexpr auto step(T a, T b, T c) {\n\treturn Step<T>(a, a < b ? (b - a - 1) / c + 1 : 0, c);\n}\n#line 8 \"/home/yuruhiya/programming/library/template/Ruby.cpp\"\nusing namespace std;\n\ntemplate <class F> struct Callable {\n\tF func;\n\tCallable(const F& f) : func(f) {}\n};\ntemplate <class T, class F> auto operator|(const T& v, const Callable<F>& c) {\n\treturn c.func(v);\n}\n\nstruct Sort_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\tsort(begin(v), end(v), f);\n\t\t\treturn v;\n\t\t});\n\t}\n\ttemplate <class T> friend auto operator|(T v, [[maybe_unused]] const Sort_impl& c) {\n\t\tsort(begin(v), end(v));\n\t\treturn v;\n\t}\n} Sort;\nstruct SortBy_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\tsort(begin(v), end(v),\n\t\t\t [&](const auto& i, const auto& j) { return f(i) < f(j); });\n\t\t\treturn v;\n\t\t});\n\t}\n} SortBy;\nstruct RSort_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\tsort(rbegin(v), rend(v), f);\n\t\t\treturn v;\n\t\t});\n\t}\n\ttemplate <class T> friend auto operator|(T v, [[maybe_unused]] const RSort_impl& c) {\n\t\tsort(rbegin(v), rend(v));\n\t\treturn v;\n\t}\n} RSort;\nstruct RSortBy_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\tsort(begin(v), end(v),\n\t\t\t [&](const auto& i, const auto& j) { return f(i) > f(j); });\n\t\t\treturn v;\n\t\t});\n\t}\n} RSortBy;\nstruct Reverse_impl {\n\ttemplate <class T> friend auto operator|(T v, const Reverse_impl& c) {\n\t\treverse(begin(v), end(v));\n\t\treturn v;\n\t}\n} Reverse;\nstruct Unique_impl {\n\ttemplate <class T> friend auto operator|(T v, const Unique_impl& c) {\n\t\tv.erase(unique(begin(v), end(v), end(v)));\n\t\treturn v;\n\t}\n} Unique;\nstruct Uniq_impl {\n\ttemplate <class T> friend auto operator|(T v, const Uniq_impl& c) {\n\t\tsort(begin(v), end(v));\n\t\tv.erase(unique(begin(v), end(v)), end(v));\n\t\treturn v;\n\t}\n} Uniq;\nstruct Rotate_impl {\n\tauto operator()(int&& left) {\n\t\treturn Callable([&](auto v) {\n\t\t\tint s = static_cast<int>(size(v));\n\t\t\tassert(-s <= left && left <= s);\n\t\t\tif (0 <= left) {\n\t\t\t\trotate(begin(v), begin(v) + left, end(v));\n\t\t\t} else {\n\t\t\t\trotate(begin(v), end(v) + left, end(v));\n\t\t\t}\n\t\t\treturn v;\n\t\t});\n\t}\n} Rotate;\nstruct Max_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) { return *max_element(begin(v), end(v), f); });\n\t}\n\ttemplate <class T> friend auto operator|(T v, const Max_impl& c) {\n\t\treturn *max_element(begin(v), end(v));\n\t}\n} Max;\nstruct Min_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) { return *min_element(begin(v), end(v), f); });\n\t}\n\ttemplate <class T> friend auto operator|(T v, const Min_impl& c) {\n\t\treturn *min_element(begin(v), end(v));\n\t}\n} Min;\nstruct MaxPos_impl {\n\ttemplate <class T> friend auto operator|(T v, const MaxPos_impl& c) {\n\t\treturn max_element(begin(v), end(v)) - begin(v);\n\t}\n} MaxPos;\nstruct MinPos_impl {\n\ttemplate <class T> friend auto operator|(T v, const MinPos_impl& c) {\n\t\treturn min_element(begin(v), end(v)) - begin(v);\n\t}\n} MinPos;\nstruct MaxBy_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\tauto max_it = begin(v);\n\t\t\tauto max_val = f(*max_it);\n\t\t\tfor (auto it = next(begin(v)); it != end(v); ++it) {\n\t\t\t\tif (auto val = f(*it); max_val < val) {\n\t\t\t\t\tmax_it = it;\n\t\t\t\t\tmax_val = val;\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn *max_it;\n\t\t});\n\t}\n} MaxBy;\nstruct MinBy_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\tauto min_it = begin(v);\n\t\t\tauto min_val = f(*min_it);\n\t\t\tfor (auto it = next(begin(v)); it != end(v); ++it) {\n\t\t\t\tif (auto val = f(*it); min_val > val) {\n\t\t\t\t\tmin_it = it;\n\t\t\t\t\tmin_val = val;\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn *min_it;\n\t\t});\n\t}\n} MinBy;\nstruct MaxOf_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\tauto max_val = f(*begin(v));\n\t\t\tfor (auto it = next(begin(v)); it != end(v); ++it) {\n\t\t\t\tif (auto val = f(*it); max_val < val) {\n\t\t\t\t\tmax_val = val;\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn max_val;\n\t\t});\n\t}\n} MaxOf;\nstruct MinOf_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\tauto min_val = f(*begin(v));\n\t\t\tfor (auto it = next(begin(v)); it != end(v); ++it) {\n\t\t\t\tif (auto val = f(*it); min_val > val) {\n\t\t\t\t\tmin_val = val;\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn min_val;\n\t\t});\n\t}\n} MinOf;\nstruct Count_impl {\n\ttemplate <class V> auto operator()(const V& val) {\n\t\treturn Callable([&](auto v) { return count(begin(v), end(v), val); });\n\t}\n} Count;\nstruct CountIf_impl {\n\ttemplate <class F> auto operator()(const F& f) {\n\t\treturn Callable([&](auto v) { return count_if(begin(v), end(v), f); });\n\t}\n} CountIf;\nstruct Index_impl {\n\ttemplate <class V> auto operator()(const V& val) {\n\t\treturn Callable([&](auto v) -> optional<int> {\n\t\t\tauto result = find(begin(v), end(v), val);\n\t\t\treturn result != end(v) ? optional(result - begin(v)) : nullopt;\n\t\t});\n\t}\n} Index;\nstruct IndexIf_impl {\n\ttemplate <class F> auto operator()(const F& f) {\n\t\treturn Callable([&](auto v) -> optional<int> {\n\t\t\tauto result = find_if(begin(v), end(v), f);\n\t\t\treturn result != end(v) ? optional(result - begin(v)) : nullopt;\n\t\t});\n\t}\n} IndexIf;\nstruct FindIf_impl {\n\ttemplate <class F> auto operator()(const F& f) {\n\t\treturn Callable([&](auto v) -> optional<typename decltype(v)::value_type> {\n\t\t\tauto result = find_if(begin(v), end(v), f);\n\t\t\treturn result != end(v) ? optional(*result) : nullopt;\n\t\t});\n\t}\n} FindIf;\nstruct Sum_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\treturn accumulate(next(begin(v)), end(v), f(*begin(v)),\n\t\t\t [&](const auto& a, const auto& b) { return a + f(b); });\n\t\t});\n\t}\n\ttemplate <class T> friend auto operator|(T v, const Sum_impl& c) {\n\t\treturn accumulate(begin(v), end(v), typename T::value_type{});\n\t}\n} Sum;\nstruct Includes {\n\ttemplate <class V> auto operator()(const V& val) {\n\t\treturn Callable([&](auto v) { return find(begin(v), end(v), val) != end(v); });\n\t}\n} Includes;\nstruct IncludesIf_impl {\n\ttemplate <class F> auto operator()(const F& f) {\n\t\treturn Callable([&](auto v) { return find_if(begin(v), end(v), f) != end(v); });\n\t}\n} IncludesIf;\nstruct RemoveIf_impl {\n\ttemplate <class F> auto operator()(const F& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\tv.erase(remove_if(begin(v), end(v), f), end(v));\n\t\t\treturn v;\n\t\t});\n\t}\n} RemoveIf;\nstruct Each_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\tfor (const auto& i : v) {\n\t\t\t\tf(i);\n\t\t\t}\n\t\t});\n\t}\n} Each;\nstruct Select_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\tusing value_type = typename decltype(v)::value_type;\n\t\t\tvector<value_type> result;\n\t\t\tfor (const auto& i : v) {\n\t\t\t\tif (f(i)) result.push_back(i);\n\t\t\t}\n\t\t\treturn result;\n\t\t});\n\t}\n} Select;\nstruct Map_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\tusing result_type = invoke_result_t<F, typename decltype(v)::value_type>;\n\t\t\tvector<result_type> result;\n\t\t\tresult.reserve(size(v));\n\t\t\tfor (const auto& i : v) {\n\t\t\t\tresult.push_back(f(i));\n\t\t\t}\n\t\t\treturn result;\n\t\t});\n\t}\n} Map;\nstruct Indexed_impl {\n\ttemplate <class T> friend auto operator|(const T& v, Indexed_impl& c) {\n\t\tusing value_type = typename T::value_type;\n\t\tvector<pair<value_type, int>> result;\n\t\tresult.reserve(size(v));\n\t\tint index = 0;\n\t\tfor (const auto& i : v) {\n\t\t\tresult.emplace_back(i, index++);\n\t\t}\n\t\treturn result;\n\t}\n} Indexed;\nstruct AllOf_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\tfor (const auto& i : v) {\n\t\t\t\tif (!f(i)) return false;\n\t\t\t}\n\t\t\treturn true;\n\t\t});\n\t}\n} AllOf;\nstruct AnyOf_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\tfor (const auto& i : v) {\n\t\t\t\tif (f(i)) return true;\n\t\t\t}\n\t\t\treturn false;\n\t\t});\n\t}\n} AnyOf;\nstruct NoneOf_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\tfor (const auto& i : v) {\n\t\t\t\tif (f(i)) return false;\n\t\t\t}\n\t\t\treturn true;\n\t\t});\n\t}\n} NoneOf;\n\nstruct Tally_impl {\n\ttemplate <class F> auto operator()(size_t max_val) {\n\t\treturn Callable([&](auto v) {\n\t\t\tvector<size_t> result(max_val);\n\t\t\tfor (const auto& i : v) {\n\t\t\t\tresult[static_cast<size_t>(i)]++;\n\t\t\t}\n\t\t\treturn result;\n\t\t});\n\t}\n\ttemplate <class T, class value_type = typename T::value_type>\n\tfriend auto operator|(const T& v, Tally_impl& c) {\n\t\tmap<value_type, size_t> result;\n\t\tfor (const auto& i : v) {\n\t\t\tresult[i]++;\n\t\t}\n\t\treturn result;\n\t}\n} Tally;\n\ntemplate <class T> auto operator*(const vector<T>& a, size_t n) {\n\tT result;\n\tfor (size_t i = 0; i < n; ++i) {\n\t\tresult.insert(result.end(), a.begin(), a.end());\n\t}\n\treturn result;\n}\nauto operator*(string a, size_t n) {\n\tstring result;\n\tfor (size_t i = 0; i < n; ++i) {\n\t\tresult += a;\n\t}\n\treturn result;\n}\ntemplate <class T, class U> auto& operator<<(vector<T>& a, const U& b) {\n\ta.insert(a.end(), all(b));\n\treturn a;\n}\ntemplate <class T> auto& operator<<(string& a, const T& b) {\n\ta.insert(a.end(), all(b));\n\treturn a;\n}\ntemplate <class T, class U> auto operator+(vector<T> a, const U& b) {\n\ta << b;\n\treturn a;\n}\ntemplate <class T> auto operator+(string a, const T& b) {\n\ta << b;\n\treturn a;\n}\n#line 6 \"/home/yuruhiya/programming/library/template/functions.cpp\"\nusing namespace std;\n\ntemplate <class T> int sz(const T& v) {\n\treturn v.size();\n}\ntemplate <class T, class U> int lower_index(const T& a, const U& v) {\n\treturn lower_bound(all(a), v) - a.begin();\n}\ntemplate <class T, class U> int upper_index(const T& a, const U& v) {\n\treturn upper_bound(all(a), v) - a.begin();\n}\ntemplate <class T> auto Slice(const T& v, size_t i, size_t len) {\n\treturn i < v.size() ? T(v.begin() + i, v.begin() + min(i + len, v.size())) : T();\n}\ntemplate <class T> T div_ceil(T n, T m) {\n\treturn (n + m - 1) / m;\n}\ntemplate <class T> T div_ceil2(T n, T m) {\n\treturn div_ceil(n, m) * m;\n}\ntemplate <class T> T triangle(T n) {\n\treturn (n & 1) ? (n + 1) / 2 * n : n / 2 * (n + 1);\n}\ntemplate <class T> T nC2(T n) {\n\treturn (n & 1) ? (n - 1) / 2 * n : n / 2 * (n - 1);\n}\ntemplate <class T> T middle(const T& l, const T& r) {\n\treturn l + (r - l) / 2;\n}\ntemplate <class T> bool chmax(T& a, const T& b) {\n\tif (a < b) {\n\t\ta = b;\n\t\treturn true;\n\t}\n\treturn false;\n}\ntemplate <class T> bool chmin(T& a, const T& b) {\n\tif (a > b) {\n\t\ta = b;\n\t\treturn true;\n\t}\n\treturn false;\n}\ntemplate <class T> bool in_range(const T& v, const T& min, const T& max) {\n\treturn min <= v && v < max;\n}\ntemplate <class T> bool in_square(T n) {\n\tT s = sqrt(n);\n\treturn s * s == n || (s + 1) * (s + 1) == n;\n}\ntemplate <class T = long long> T BIT(int b) {\n\treturn T(1) << b;\n}\ntemplate <class T, class U = typename T::value_type> U Gcdv(const T& v) {\n\treturn accumulate(next(v.begin()), v.end(), U(*v.begin()), gcd<U, U>);\n}\ntemplate <class T, class U = typename T::value_type> U Lcmv(const T& v) {\n\treturn accumulate(next(v.begin()), v.end(), U(*v.begin()), lcm<U, U>);\n}\ntemplate <class T, class U> T Pow(T a, U n) {\n\tT result = 1;\n\twhile (n > 0) {\n\t\tif (n & 1) {\n\t\t\tresult *= a;\n\t\t\tn--;\n\t\t} else {\n\t\t\ta *= a;\n\t\t\tn >>= 1;\n\t\t}\n\t}\n\treturn result;\n}\ntemplate <class T, class U> T Powmod(T a, U n, T mod) {\n\tT result = 1;\n\twhile (n > 0) {\n\t\tif (n & 1) {\n\t\t\tresult = result * a % mod;\n\t\t\tn--;\n\t\t} else {\n\t\t\ta = a * a % mod;\n\t\t\tn >>= 1;\n\t\t}\n\t}\n\treturn result;\n}\nnamespace internal {\n\ttemplate <class T, size_t N> auto make_vector(vector<int>& sizes, const T& init) {\n\t\tif constexpr (N == 1) {\n\t\t\treturn vector(sizes[0], init);\n\t\t} else {\n\t\t\tint size = sizes[N - 1];\n\t\t\tsizes.pop_back();\n\t\t\treturn vector(size, make_vector<T, N - 1>(sizes, init));\n\t\t}\n\t}\n} // namespace internal\ntemplate <class T, size_t N>\nauto make_vector(const int (&sizes)[N], const T& init = T()) {\n\tvector s(rbegin(sizes), rend(sizes));\n\treturn internal::make_vector<T, N>(s, init);\n}\n#line 9 \"/home/yuruhiya/programming/library/template/template.cpp\"\n#if __has_include(<library/dump.hpp>)\n#include <library/dump.hpp>\n#define LOCAL\n#else\n#define dump(...) ((void)0)\n#endif\n\ntemplate <class T> constexpr T oj_local(const T& oj, const T& local) {\n#ifndef LOCAL\n\treturn oj;\n#else\n\treturn local;\n#endif\n}\n#line 5 \"/home/yuruhiya/programming/library/Graph/GraphTemplate.cpp\"\nusing namespace std;\n\nusing Weight = long long;\nconstexpr Weight INF = numeric_limits<Weight>::max();\nstruct Edge {\n\tint to;\n\tWeight cost;\n\tEdge() : to(-1), cost(-1) {}\n\tEdge(int _to, Weight _cost = 1) : to(_to), cost(_cost) {}\n\tfriend bool operator<(const Edge& e1, const Edge& e2) {\n\t\treturn e1.cost < e2.cost;\n\t}\n\tfriend bool operator>(const Edge& e1, const Edge& e2) {\n\t\treturn e1.cost > e2.cost;\n\t}\n\tfriend ostream& operator<<(ostream& os, const Edge& e) {\n\t\treturn os << \"->\" << e.to << '(' << e.cost << ')';\n\t}\n};\nusing Graph = vector<vector<Edge>>;\nstruct Edge2 {\n\tint from, to;\n\tWeight cost;\n\tEdge2() : from(-1), to(-1), cost(0) {}\n\tEdge2(int _from, int _to, Weight _cost) : from(_from), to(_to), cost(_cost) {}\n\tfriend bool operator<(const Edge2& e1, const Edge2& e2) {\n\t\treturn e1.cost < e2.cost;\n\t}\n\tfriend bool operator>(const Edge2& e1, const Edge2& e2) {\n\t\treturn e1.cost > e2.cost;\n\t}\n\tfriend ostream& operator<<(ostream& os, const Edge2& e) {\n\t\treturn os << e.from << \"->\" << e.to << '(' << e.cost << ')';\n\t}\n};\nusing Edges = vector<Edge2>;\nusing Matrix = vector<vector<Weight>>;\n#line 5 \"/home/yuruhiya/programming/library/Graph/DijkstraST.cpp\"\nusing namespace std;\n\nWeight Dijkstra(const Graph& graph, int s, int t) {\n\tint V = graph.size();\n\tvector<Weight> dist(V, INF);\n\tdist[s] = 0;\n\tpriority_queue<Edge, vector<Edge>, greater<Edge>> pq;\n\tpq.emplace(s, 0);\n\twhile (!pq.empty()) {\n\t\tEdge p = pq.top();\n\t\tpq.pop();\n\t\tint v = p.to;\n\t\tif (v == t) return dist[t];\n\t\tif (dist[v] < p.cost) continue;\n\t\tfor (auto e : graph[v]) {\n\t\t\tif (dist[e.to] > dist[v] + e.cost) {\n\t\t\t\tdist[e.to] = dist[v] + e.cost;\n\t\t\t\tpq.emplace(e.to, dist[e.to]);\n\t\t\t}\n\t\t}\n\t}\n\treturn dist[t];\n}\n#line 3 \"a.cpp\"\n\nll solve(int n, int m) {\n\tmap<string, int> str;\n\tint k = 0;\n\tVS s = in[3];\n\tvector<tuple<string, string, ll>> edges;\n\trep(i, m) {\n\t\tins(s, t);\n\t\tinl(a, b);\n\t\tedges.emplace_back(s, t, a / 40 + b);\n\t\tif (str.count(s) == 0) str[s] = k++;\n\t\tif (str.count(t) == 0) str[t] = k++;\n\t}\n\n\tGraph g(n);\n\tfor (auto [s, t, d] : edges) {\n\t\tg[str[s]].emplace_back(str[t], d);\n\t\tg[str[t]].emplace_back(str[s], d);\n\t}\n\n\tint start = str[s[0]], passing = str[s[1]], goal = str[s[2]];\n\treturn Dijkstra(g, start, passing) + Dijkstra(g, passing, goal);\n}\n\nint main() {\n\twhile (true) {\n\t\tini(n, m);\n\t\tif (n + m == 0) break;\n\t\tout(solve(n, m));\n\t}\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 4080, "score_of_the_acc": -0.722, "final_rank": 11 }, { "submission_id": "aoj_2283_3736117", "code_snippet": "#include<iostream>\n#include<vector>\n#include<set>\n#include<map>\n#include<queue>\nusing namespace std;\n\nint main(){\n int n, m;\n while(cin >> n >> m, n+m){\n string s, t, g;\n cin >> s >> t >> g;\n map<string, vector<pair<string,int>>> ma;\n for(int i = 0; i < m; i++){\n string a, b;\n int c, d;\n cin >> a >> b >> c >> d;\n ma[a].push_back({b, c/40+d});\n ma[b].push_back({a, c/40+d});\n }\n map<string,int> dp;\n for(auto p : ma) dp[p.first] = 1<<30;\n dp[s] = 0;\n priority_queue<pair<int,string>> pq;\n pq.push({-0, s});\n while(!pq.empty()){\n auto p = pq.top(); pq.pop();\n string pos = p.second;\n int cost = -p.first;\n if(dp[pos] != cost) continue;\n for(auto next : ma[pos]){\n int ncost = cost + next.second;\n string npos = next.first;\n if(dp[npos] > ncost){\n dp[npos] = ncost;\n pq.push({-ncost, npos});\n }\n }\n }\n int ans = dp[t];\n for(auto p : ma) dp[p.first] = 1<<30;\n dp[t] = 0;\n pq.push({-0, t});\n while(!pq.empty()){\n auto p = pq.top(); pq.pop();\n string pos = p.second;\n int cost = -p.first;\n if(dp[pos] != cost) continue;\n for(auto next : ma[pos]){\n int ncost = cost + next.second;\n string npos = next.first;\n if(dp[npos] > ncost){\n dp[npos] = ncost;\n pq.push({-ncost, npos});\n }\n }\n }\n ans += dp[g];\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 3836, "score_of_the_acc": -0.725, "final_rank": 12 }, { "submission_id": "aoj_2283_3341603", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))\n#define all(x) (x).begin(),(x).end()\n#define pb push_back\n#define fi first\n#define se second\n#define dbg(x) cout<<#x\" = \"<<((x))<<endl\ntemplate<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<\"(\"<<p.fi<<\",\"<<p.se<<\")\";return o;}\ntemplate<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<\"[\";for(T t:v){o<<t<<\",\";}o<<\"]\";return o;}\n\nstruct edge{ int to,cost; };\nconst int INF = INT_MAX/3;\n\nint main(){\n int n,m;\n while(cin >>n >>m,n){\n string s,p,g;\n cin >>s >>p >>g;\n\n map<string,int> name2idx;\n name2idx[s] = 0;\n name2idx[p] = 1;\n name2idx[g] = 2;\n\n int V = 3;\n\n vector<string> a(m),b(m);\n vector<int> c(m);\n rep(i,m){\n int d,t;\n cin >>a[i] >>b[i] >>d >>t;\n\n c[i] = d/40 + t;\n\n if(!name2idx.count(a[i])){\n name2idx[a[i]] = V;\n ++V;\n }\n if(!name2idx.count(b[i])){\n name2idx[b[i]] = V;\n ++V;\n }\n }\n\n vector<vector<edge>> G(V);\n rep(i,m){\n int u = name2idx[a[i]], v = name2idx[b[i]];\n G[u].pb({v,c[i]});\n G[v].pb({u,c[i]});\n }\n\n vector<int> d(V,INF);\n d[1] = 0;\n\n using pi = pair<int,int>;\n priority_queue<pi, vector<pi>, greater<pi>> pq;\n pq.push({0,1});\n while(!pq.empty()){\n pi now = pq.top();\n pq.pop();\n int v = now.se;\n if(now.fi > d[v]) continue;\n for(const auto &e:G[v]){\n if(d[e.to] > d[v]+e.cost){\n d[e.to] = d[v]+e.cost;\n pq.push({d[e.to], e.to});\n }\n }\n }\n\n cout << d[0]+d[2] << \"\\n\";\n }\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3844, "score_of_the_acc": -0.7094, "final_rank": 10 }, { "submission_id": "aoj_2283_2656893", "code_snippet": "#include \"bits/stdc++.h\"\n#include<unordered_map>\n#include<unordered_set>\n#pragma warning(disable:4996)\nusing namespace std;\nusing ld = long double;\nconst ld eps = 1e-9;\n\n\n\nint main() {\n\twhile (true) {\n\t\tint N,M;cin>>N>>M;\n\t\tif(!N)break;\n\t\tmap<string,int>stmp;\n\t\tstring S,P,G;cin>>S>>P>>G;\n\t\tstmp[S]=0;\n\t\tstmp[P]=1;\n\t\tstmp[G]=2;\n\t\tint id=3;\n\t\tvector<vector<pair<int,int>>>edges(N);\n\t\tfor (int i = 0; i < M; ++i) {\n\t\t\tstring s,t;cin>>s>>t;\n\t\t\tint d,c;cin>>d>>c;\n\t\t\tint cost=d/40+c;\n\t\t\tif (stmp.find(s) == stmp.end()) {\n\t\t\t\tstmp[s]=id++;\n\t\t\t}\n\t\t\tif (stmp.find(t) == stmp.end()) {\n\t\t\t\tstmp[t]=id++;\n\t\t\t}\n\t\t\tedges[stmp[s]].push_back(make_pair(stmp[t],cost));\n\t\t\tedges[stmp[t]].push_back(make_pair(stmp[s],cost));\n\t\t}\n\t\tvector<int>memo(N,1e9);\n\t\tmemo[stmp[P]]=0;\n\t\tpriority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>que;\n\t\tque.push(make_pair(0,stmp[P]));\n\t\twhile (!que.empty()) {\n\t\t\tauto atop(que.top());\n\t\t\tque.pop();\n\t\t\tint now=atop.second;\n\t\t\tint cost=atop.first;\n\t\t\tfor (auto e : edges[now]) {\n\t\t\t\tconst int nextcost=cost+e.second;\n\t\t\t\tconst int next=e.first;\n\t\t\t\tif (memo[next] > nextcost) {\n\t\t\t\t\tmemo[next]=nextcost;\n\t\t\t\t\tque.push(make_pair(memo[next],next));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tint ans=memo[stmp[S]]+memo[stmp[G]];\n\t\tcout<<ans<<endl;\n\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3340, "score_of_the_acc": -0.5681, "final_rank": 4 }, { "submission_id": "aoj_2283_2601353", "code_snippet": "#include<bits/stdc++.h>\n#define r(i,n) for(int i=0;i<n;i++)\nusing namespace std;\nstring s1,s2,s3,s4,s5;\nint ti,dist,n,m,st,go,mi,p1,p2;\nint d[502][502];\nmap<string,int>M;\nint main(){\n while(cin>>n>>m,n){\n M.clear();\n r(i,502)r(j,502)d[i][j]=1e8;\n int c=0;\n cin>>s1>>s2>>s3;\n r(i,m){\n cin>>s4>>s5>>dist>>ti;\n if(M[s4])p1=M[s4];\n else M[s4]=++c,p1=M[s4];\n if(M[s5])p2=M[s5];\n else M[s5]=++c,p2=M[s5];\n if(s1==s5)st=M[s5];\n if(s1==s4)st=M[s4];\n if(s2==s5)mi=M[s5];\n if(s2==s4)mi=M[s4];\n if(s3==s5)go=M[s5];\n if(s3==s4)go=M[s4];\n d[p1][p2]=d[p2][p1]=dist/40+ti;\n }\n r(o,n+1)r(i,n+1)r(j,n+1)d[i][j]=min(d[i][j],d[i][o]+d[o][j]);\n cout<<d[st][mi]+d[mi][go]<<endl;\n }\n}", "accuracy": 1, "time_ms": 340, "memory_kb": 4216, "score_of_the_acc": -1.3494, "final_rank": 19 }, { "submission_id": "aoj_2283_2535449", "code_snippet": "#include <stdio.h>\n#include <cmath>\n#include <algorithm>\n#include <cfloat>\n#include <stack>\n#include <queue>\n#include <vector>\n#include <string>\n#include <iostream>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\nstruct Info{\n\tInfo(int arg_to,int arg_dist,int arg_delay){\n\t\tto = arg_to;\n\t\tdist = arg_dist;\n\t\tdelay = arg_delay;\n\t}\n\tint to,dist,delay;\n};\n\nstruct Data{\n\tData(int arg_station,int arg_sum_time){\n\t\tstation = arg_station;\n\t\tsum_time = arg_sum_time;\n\t}\n\tbool operator<(const struct Data &arg) const{\n\t\treturn sum_time > arg.sum_time;\n\t};\n\tint station,sum_time;\n};\n\nint N,M,index;\nchar table[500][21];\n\nchar start[21],mid[21],goal[21];\n\nbool strCmp(char* base, char* comp){\n\tint length1,length2;\n\tfor(length1=0;base[length1] != '\\0';length1++);\n\tfor(length2=0;comp[length2] != '\\0';length2++);\n\tif(length1 != length2)return false;\n\n\tfor(int i=0;base[i] != '\\0'; i++){\n\t\tif(base[i] != comp[i])return false;\n\t}\n\treturn true;\n}\n\nvoid strcpy(char* to,char* str){\n\tfor(int i=0;str[i] != '\\0';i++){\n\t\tto[i] = str[i];\n\t\tto[i+1] = '\\0';\n\t}\n}\n\nvoid func(){\n\tscanf(\"%s %s %s\",start,mid,goal);\n\n\tstrcpy(table[0],start);\n\tstrcpy(table[1],mid);\n\tstrcpy(table[2],goal);\n\n\tvector<Info> V[N+1];\n\n\tindex = 3;\n\n\tchar left[21],right[21];\n\tint left_index,right_index,dist,delay;\n\n\tbool FLG;\n\n\tfor(int loop = 0; loop < M; loop++){\n\t\tscanf(\"%s %s %d %d\",left,right,&dist,&delay);\n\n\t\tFLG = false;\n\t\tfor(int i = 0; i < index; i++){\n\t\t\tif(strCmp(table[i],left)){\n\t\t\t\tleft_index = i;\n\t\t\t\tFLG = true;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tif(!FLG){\n\t\t\tleft_index = index;\n\t\t\tstrcpy(table[index++],left);\n\t\t}\n\n\t\tFLG = false;\n\t\tfor(int i = 0; i < index; i++){\n\t\t\tif(strCmp(table[i],right)){\n\t\t\t\tright_index = i;\n\t\t\t\tFLG = true;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tif(!FLG){\n\t\t\tright_index = index;\n\t\t\tstrcpy(table[index++],right);\n\t\t}\n\n\t\tV[left_index].push_back(Info(right_index,dist,delay));\n\t\tV[right_index].push_back(Info(left_index,dist,delay));\n\t}\n\n\tint min_time[N];\n\tint ans = 0;\n\n\tpriority_queue<Data> Q;\n\n\tfor(int loop = 0; loop < 2; loop++){\n\t\tfor(int i = 0; i < N; i++)min_time[i] = BIG_NUM;\n\t\tif(loop == 0){\n\t\t\tmin_time[0] = 0;\n\t\t\tQ.push(Data(0,0));\n\t\t}else{\n\t\t\tmin_time[1] = 0;\n\t\t\tQ.push(Data(1,0));\n\t\t}\n\n\t\twhile(!Q.empty()){\n\n\t\t\tif((loop == 0 && Q.top().station == 1) || (loop == 0 && Q.top().station == 1)){\n\t\t\t\tQ.pop();\n\t\t\t}else if(Q.top().sum_time > min_time[Q.top().station]){\n\t\t\t\tQ.pop();\n\t\t\t}else{\n\t\t\t\tfor(int i = 0; i < V[Q.top().station].size(); i++){\n\t\t\t\t\tif(min_time[V[Q.top().station][i].to] > Q.top().sum_time+V[Q.top().station][i].dist/40+V[Q.top().station][i].delay){\n\t\t\t\t\t\tmin_time[V[Q.top().station][i].to] = Q.top().sum_time+V[Q.top().station][i].dist/40+V[Q.top().station][i].delay;\n\t\t\t\t\t\tQ.push(Data(V[Q.top().station][i].to,min_time[V[Q.top().station][i].to]));\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tQ.pop();\n\t\t\t}\n\t\t}\n\n\t\tif(loop == 0){\n\t\t\tans += min_time[1];\n\t\t}else{\n\t\t\tans += min_time[2];\n\t\t}\n\t}\n\n\tprintf(\"%d\\n\",ans);\n}\n\nint main(){\n\n\twhile(true){\n\t\tscanf(\"%d %d\",&N,&M);\n\t\tif(N == 0 && M == 0)break;\n\n\t\tfunc();\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 3328, "score_of_the_acc": -0.6184, "final_rank": 9 }, { "submission_id": "aoj_2283_2129963", "code_snippet": "#include <queue>\n#include <limits>\n#include <string>\n#include <vector>\n#include <iostream>\n#include <algorithm>\nusing namespace std;\nstruct edge {\n\tint to, cost;\n\tedge(int to_, int cost_) : to(to_), cost(cost_) {};\n};\nbool operator<(const edge& e1, const edge& e2) {\n\treturn e1.cost < e2.cost;\n}\ntemplate<class Type>\nstd::vector<Type> shortest_path(std::vector<std::vector<edge> > &G, int s) {\n\tstd::priority_queue<edge> que; que.push(edge(s, 0));\n\tstd::vector<Type> ret(G.size(), std::numeric_limits<Type>::max()); ret[s] = 0;\n\twhile (!que.empty()) {\n\t\tedge u = que.top(); que.pop();\n\t\tfor (edge e : G[u.to]) {\n\t\t\tif (ret[e.to] > ret[u.to] + e.cost) {\n\t\t\t\tret[e.to] = ret[u.to] + e.cost;\n\t\t\t\tque.push(edge(e.to, -ret[e.to]));\n\t\t\t}\n\t\t}\n\t}\n\treturn ret;\n}\nint n, m, d, e;\nint main() {\n\twhile (cin >> n >> m, n) {\n\t\tstring s, t, g;\n\t\tcin >> s >> t >> g;\n\t\tvector<string> a(m), b(m), r; vector<int> x(m);\n\t\tfor (int i = 0; i < m; i++) {\n\t\t\tcin >> a[i] >> b[i] >> d >> e;\n\t\t\tx[i] = d / 40 + e;\n\t\t\tr.push_back(a[i]);\n\t\t\tr.push_back(b[i]);\n\t\t}\n\t\tsort(r.begin(), r.end());\n\t\tr.erase(unique(r.begin(), r.end()), r.end());\n\t\tint sp = lower_bound(r.begin(), r.end(), s) - r.begin();\n\t\tint tp = lower_bound(r.begin(), r.end(), t) - r.begin();\n\t\tint gp = lower_bound(r.begin(), r.end(), g) - r.begin();\n\t\tvector<vector<edge> > G(n);\n\t\tfor (int i = 0; i < m; i++) {\n\t\t\tint ptr1 = lower_bound(r.begin(), r.end(), a[i]) - r.begin();\n\t\t\tint ptr2 = lower_bound(r.begin(), r.end(), b[i]) - r.begin();\n\t\t\tG[ptr1].push_back(edge(ptr2, x[i]));\n\t\t\tG[ptr2].push_back(edge(ptr1, x[i]));\n\t\t}\n\t\tint w1 = shortest_path<int>(G, sp)[tp];\n\t\tint w2 = shortest_path<int>(G, tp)[gp];\n\t\tcout << w1 + w2 << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3924, "score_of_the_acc": -0.7318, "final_rank": 13 }, { "submission_id": "aoj_2283_2127868", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define int long long\n#define F first\n#define S second\n#define all(v) (v).begin(), (v).end()\n#define rep(i, n) for(int i = 0; i < (int)(n); i++)\n#define reps(i, f, n) for(int i = (int)(f); i < (int)(n); i++)\n#define each(a, b) for(auto& a : b)\n\ntypedef pair<int, int> P;\n\nconst int inf = 1LL << 55;\n\nint G[505][505];\n\nsigned main()\n{\n int n, m;\n while(cin >> n >> m, n || m) {\n fill(G[0], G[505], inf);\n int cnt = 0;\n map<string, int> stat;\n string s, p, g; cin >> s >> p >> g;\n stat[s] = cnt++; stat[p] = cnt++; stat[g] = cnt++;\n while(m--) {\n string a, b; int d, t;\n cin >> a >> b >> d >> t;\n if(stat.find(a) == stat.end()) stat[a] = cnt++;\n if(stat.find(b) == stat.end()) stat[b] = cnt++;\n G[stat[a]][stat[b]] = G[stat[b]][stat[a]] = d / 40 + t;\n }\n for(int i = 0; i < n; i++) {\n for(int j = 0; j < n; j++) {\n\tfor(int k = 0; k < n; k++) G[j][k] = min(G[j][k], G[j][i] + G[i][k]);\n }\n }\n cout << G[stat[s]][stat[p]] + G[stat[p]][stat[g]] << endl;\n }\n \n return 0;\n}", "accuracy": 1, "time_ms": 370, "memory_kb": 5072, "score_of_the_acc": -1.6429, "final_rank": 20 }, { "submission_id": "aoj_2283_2127423", "code_snippet": "#include<bits/stdc++.h>\n#define rep(i,n)for(int i=0;i<n;i++)\nusing namespace std;\ntypedef pair<int, int>P;\n\nint d1[550], d2[550];\nchar s[50], p[50], g[50], a[50], b[50];\nint main() {\n\tint n, m;\n\twhile (scanf(\"%d%d\", &n, &m), n) {\n\t\tunordered_map<string, int>mp;\n\t\tvector<P>E[550];\n\t\tscanf(\"%s%s%s\", s, p, g);\n\t\tmp[s] = 0; mp[p] = 1; mp[g] = 2;\n\t\trep(i, m) {\n\t\t\tint d, t; scanf(\"%s%s%d%d\", a, b, &d, &t);\n\t\t\tif (mp.find(a) == mp.end())mp[a] = mp.size();\n\t\t\tif (mp.find(b) == mp.end())mp[b] = mp.size();\n\t\t\tE[mp[a]].push_back(P(d / 40 + t, mp[b]));\n\t\t\tE[mp[b]].push_back(P(d / 40 + t, mp[a]));\n\t\t}\n\t\tpriority_queue<P, vector<P>, greater<P>>que;\n\t\tmemset(d1, 0x3f, sizeof(d1));\n\t\td1[0] = 0; que.push(P(0, 0));\n\t\twhile (!que.empty()) {\n\t\t\tP p = que.top(); que.pop();\n\t\t\tif (d1[p.second] != p.first)continue;\n\t\t\tfor (P u : E[p.second]) {\n\t\t\t\tif (d1[u.second] > p.first + u.first) {\n\t\t\t\t\td1[u.second] = p.first + u.first;\n\t\t\t\t\tque.push(P(d1[u.second], u.second));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tmemset(d2, 0x3f, sizeof(d2));\n\t\td2[1] = 0; que.push(P(0, 1));\n\t\twhile (!que.empty()) {\n\t\t\tP p = que.top(); que.pop();\n\t\t\tif (d2[p.second] != p.first)continue;\n\t\t\tfor (P u : E[p.second]) {\n\t\t\t\tif (d2[u.second] > p.first + u.first) {\n\t\t\t\t\td2[u.second] = p.first + u.first;\n\t\t\t\t\tque.push(P(d2[u.second], u.second));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tprintf(\"%d\\n\", d1[1] + d2[2]);\n\t}\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3468, "score_of_the_acc": -0.5683, "final_rank": 5 }, { "submission_id": "aoj_2283_2127418", "code_snippet": "#include<bits/stdc++.h>\n#define rep(i,n)for(int i=0;i<n;i++)\nusing namespace std;\ntypedef pair<int, int>P;\n\nint d1[550], d2[550];\nint main() {\n\tint n, m;\n\twhile (scanf(\"%d%d\", &n, &m), n) {\n\t\tmap<string, int>mp;\n\t\tvector<P>E[550];\n\t\tstring s, p, g; cin >> s >> p >> g;\n\t\tmp[s] = 0; mp[p] = 1; mp[g] = 2;\n\t\trep(i, m) {\n\t\t\tstring a, b; int d, t; cin >> a >> b >> d >> t;\n\t\t\tif (mp.find(a) == mp.end())mp[a] = mp.size();\n\t\t\tif (mp.find(b) == mp.end())mp[b] = mp.size();\n\t\t\tE[mp[a]].push_back(P(d / 40 + t, mp[b]));\n\t\t\tE[mp[b]].push_back(P(d / 40 + t, mp[a]));\n\t\t}\n\t\tpriority_queue<P, vector<P>, greater<P>>que;\n\t\tmemset(d1, 0x3f, sizeof(d1));\n\t\td1[0] = 0; que.push(P(0, 0));\n\t\twhile (!que.empty()) {\n\t\t\tP p = que.top(); que.pop();\n\t\t\tif (d1[p.second] != p.first)continue;\n\t\t\tfor (P u : E[p.second]) {\n\t\t\t\tif (d1[u.second] > p.first + u.first) {\n\t\t\t\t\td1[u.second] = p.first + u.first;\n\t\t\t\t\tque.push(P(d1[u.second], u.second));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tmemset(d2, 0x3f, sizeof(d2));\n\t\td2[1] = 0; que.push(P(0, 1));\n\t\twhile (!que.empty()) {\n\t\t\tP p = que.top(); que.pop();\n\t\t\tif (d2[p.second] != p.first)continue;\n\t\t\tfor (P u : E[p.second]) {\n\t\t\t\tif (d2[u.second] > p.first + u.first) {\n\t\t\t\t\td2[u.second] = p.first + u.first;\n\t\t\t\t\tque.push(P(d2[u.second], u.second));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tprintf(\"%d\\n\", d1[1] + d2[2]);\n\t}\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 3400, "score_of_the_acc": -0.6028, "final_rank": 8 }, { "submission_id": "aoj_2283_2127343", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntypedef pair<ll,ll> P;\nint main(){\n ll n,m;\n while(cin>>n>>m,n||m){\n string s,p,g;\n cin>>s>>p>>g;\n vector<P> G[n];\n map<string,int> st;\n vector<string> vs;\n for(ll i=0;i<m;i++){\n string a,b;\n ll d,t;\n cin>>a>>b>>d>>t;\n if(st.find(a)==st.end()){\n st[a]=vs.size();\n vs.push_back(a);\n }\n if(st.find(b)==st.end()){\n st[b]=vs.size();\n vs.push_back(b);\n }\n G[st[a]].push_back(P(st[b],d/40+t));\n G[st[b]].push_back(P(st[a],d/40+t));\n }\n ll ans=0;\n ll dp[n];\n priority_queue<P,vector<P>,greater<P> > q;\n memset(dp,-1,sizeof(dp));\n q.push(P(0,st[s]));\n while(!q.empty()){\n P pp=q.top();q.pop();\n ll v=pp.second,d=pp.first;\n if(~dp[v]&&dp[v]<=d) continue;\n dp[v]=d;\n if(v==st[p]) break;\n for(ll i=0;i<(ll)G[v].size();i++){\n q.push(P(d+G[v][i].second,G[v][i].first));\n }\n }\n while(!q.empty()) q.pop();\n ans+=dp[st[p]];\n memset(dp,-1,sizeof(dp));\n q.push(P(0,st[p]));\n while(!q.empty()){\n P pp=q.top();q.pop();\n ll v=pp.second,d=pp.first;\n if(~dp[v]&&dp[v]<=d) continue;\n dp[v]=d;\n if(v==st[g]) break;\n for(ll i=0;i<(ll)G[v].size();i++){\n q.push(P(d+G[v][i].second,G[v][i].first));\n }\n }\n ans+=dp[st[g]];\n cout<<ans<<endl;\n } \n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3432, "score_of_the_acc": -0.5939, "final_rank": 7 }, { "submission_id": "aoj_2283_2127126", "code_snippet": "#include <bits/stdc++.h>\n#define N 501\n#define INF 1e9\nusing namespace std;\ntypedef pair<int,int> P;\nvector<P> G[N];\n\n\n\nint dijkstra(int s,int g){\n int D[N];\n for(int i=0;i<N;i++) D[i]=INF;\n priority_queue<P,vector<P>,greater<P> > Q;\n Q.push(P(0,s));\n D[s]=0;\n while(!Q.empty()){\n P t=Q.top();Q.pop();\n int cost=t.first, pos=t.second;\n if(pos==g) return cost;\n if(D[pos]<cost) continue;\n \n for(int i=0;i<G[pos].size();i++){\n int nx=G[pos][i].first;\n int ncost=cost+G[pos][i].second;\n if(D[nx]>ncost) Q.push(P(ncost,nx)),D[nx]=ncost;\n }\n }\n cout<<\"OK\"<<endl;\n return -1;\n}\n\n\nint main(){\n while(1){\n int n,m;\n cin>>n>>m;\n if(!n&&!m)break;\n string s,p,g;\n cin>>s>>p>>g;\n map<string,int> M;\n for(int i=0;i<N;i++) G[i].clear();\n for(int i=0,d,t,cnt=0;i<m;i++) {\n string a,b;\n cin>>a>>b>>d>>t;\n if(!M.count(a)) M[a]=cnt++;\n if(!M.count(b)) M[b]=cnt++;\n G[M[a]].push_back(P(M[b],d/40+t));\n G[M[b]].push_back(P(M[a],d/40+t));\n }\n int ans= dijkstra(M[s],M[p]);\n ans=ans+dijkstra(M[p],M[g]);\n\n cout<<ans<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3336, "score_of_the_acc": -0.567, "final_rank": 3 }, { "submission_id": "aoj_2283_2127098", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nmap<string,int> ma;\nvoid in(string s) {\n if(!ma.count(s)) {\n int k=ma.size();\n ma[s]=k;\n }\n}\n\nint main() {\n int n,m;\n while(cin >> n >> m && n) {\n ma.clear();\n string r[3];\n for(int i=0; i<3; i++) {\n cin >> r[i];\n in(r[i]);\n }\n int d[n][n];\n for(int i=0;i<n;i++){\n for(int j=0;j<n;j++)d[i][j]=1<<29;\n d[i][i]=0;\n }\n for(int i=0; i<m; i++) {\n string s,t;\n int b,c;\n cin >> s >> t >> b >> c;\n in(s);in(t);\n d[ma[s]][ma[t]]=b/40+c;\n d[ma[t]][ma[s]]=b/40+c;\n }\n for(int k=0;k<n;k++)for(int i=0;i<n;i++)for(int j=0;j<n;j++)d[i][j]=min(d[i][j],d[i][k]+d[k][j]);\n cout << d[ma[r[0]]][ma[r[1]]]+d[ma[r[1]]][ma[r[2]]] << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 310, "memory_kb": 4076, "score_of_the_acc": -1.2566, "final_rank": 17 }, { "submission_id": "aoj_2283_2127065", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntypedef pair<ll,ll> P;\nint main(){\n ll n,m;\n while(cin>>n>>m,n||m){\n string s,p,g;\n cin>>s>>p>>g;\n vector<P> G[n];\n map<string,int> st;\n vector<string> vs;\n for(ll i=0;i<m;i++){\n string a,b;\n ll d,t;\n cin>>a>>b>>d>>t;\n if(st.find(a)==st.end()){\n\tst[a]=vs.size();\n\tvs.push_back(a);\n }\n if(st.find(b)==st.end()){\n\tst[b]=vs.size();\n\tvs.push_back(b);\n }\n G[st[a]].push_back(P(st[b],d/40+t));\n G[st[b]].push_back(P(st[a],d/40+t));\n }\n ll ans=0;\n ll dp[n];\n priority_queue<P,vector<P>,greater<P> > q;\n memset(dp,-1,sizeof(dp));\n q.push(P(0,st[s]));\n while(!q.empty()){\n P pp=q.top();q.pop();\n ll v=pp.second,d=pp.first;\n if(~dp[v]&&dp[v]<=d) continue;\n dp[v]=d;\n if(v==st[p]) break;\n for(ll i=0;i<(ll)G[v].size();i++){\n\tq.push(P(d+G[v][i].second,G[v][i].first));\n }\n }\n while(!q.empty()) q.pop();\n ans+=dp[st[p]];\n memset(dp,-1,sizeof(dp));\n q.push(P(0,st[p]));\n while(!q.empty()){\n P pp=q.top();q.pop();\n ll v=pp.second,d=pp.first;\n if(~dp[v]&&dp[v]<=d) continue;\n dp[v]=d;\n if(v==st[g]) break;\n for(ll i=0;i<(ll)G[v].size();i++){\n\tq.push(P(d+G[v][i].second,G[v][i].first));\n }\n }\n ans+=dp[st[g]];\n cout<<ans<<endl;\n } \n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3424, "score_of_the_acc": -0.5917, "final_rank": 6 }, { "submission_id": "aoj_2283_1879614", "code_snippet": "#include <iostream>\n#include <map>\n#include <algorithm>\n \nusing namespace std;\n \nconst int MAX = 500;\nconst int INF = 1e9;\n \nint main(){\n int n,m;\n \n while(cin >> n >> m, n | m){\n\tmap<string,int> mp;\n\tint dist[MAX+1][MAX+1];\n\tstring a,b,start,goal,via;\n\tint c,d;\n \n\tfor(int i = 0 ; i <= n ; i++){\n\t for(int j = 0 ; j <= n ; j++){\n\t\tdist[i][j] = INF;\n\t }\n\t}\n \n\tcin >> start >> via >> goal;\n \n\tint s = 1;\n\tfor(int i = 0 ; i < m ; i++){\n\t cin >> a >> b >> c >> d;\n \n\t if(!mp[a]){\n\t\tmp[a] = s++;\n\t }\n\t if(!mp[b]){ \n\t\tmp[b] = s++;\n\t }\n \n\t dist[mp[a]][mp[b]] = dist[mp[b]][mp[a]] = c / 40 + d;\n\t}\n \n\tfor(int k = 1 ; k <= n ; k++){\n\t for(int i = 1 ; i <= n ; i++){\n\t\tfor(int j = 1 ; j <= n ; j++){\n\t\t dist[i][j] = min(dist[i][j] ,dist[i][k] + dist[k][j]);\n\t\t}\n\t }\n\t}\n\tcout << dist[mp[start]][mp[via]] + dist[mp[via]][mp[goal]] << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 330, "memory_kb": 4148, "score_of_the_acc": -1.3125, "final_rank": 18 }, { "submission_id": "aoj_2283_1805299", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nstring S[5000],T[5000],V[5000],A,B,C;int a[5000],b[5000],n,m,dist[5000];\nvector<string>U;vector<pair<int,int>>x[500];\nint dfs(int s,int g){\n\tfor(int i=0;i<5000;i++)dist[i]=1<<30;\n\tdist[s]=0;\n\tfor(int i=0;i<n;i++){\n\t\tfor(int j=0;j<n;j++){\n\t\t\tfor(int k=0;k<x[j].size();k++){\n\t\t\t\tdist[x[j][k].first]=min(dist[x[j][k].first],dist[j]+x[j][k].second);\n\t\t\t}\n\t\t}\n\t}\n\treturn dist[g];\n}\nint main(){\n\twhile(true){\n\t\tcin>>n>>m;for(int i=0;i<500;i++)x[i].clear();\n\t\tif(n==0){break;}U.clear();cin>>A>>B>>C;\n\t\tfor(int i=0;i<m;i++){\n\t\t\tcin>>S[i]>>T[i]>>a[i]>>b[i];\n\t\t\tU.push_back(S[i]);U.push_back(T[i]);\n\t\t}\n\t\tsort(U.begin(),U.end());V[0]=U[0];int cnt=1,G,H,I;\n\t\tfor(int i=1;i<U.size();i++){\n\t\t\tif(U[i]!=U[i-1]){V[cnt]=U[i];cnt++;}\n\t\t}\n\t\tfor(int i=0;i<cnt;i++){\n\t\t\tif(A==V[i])G=i;\n\t\t\tif(B==V[i])H=i;\n\t\t\tif(C==V[i])I=i;\n\t\t}\n\t\tfor(int i=0;i<m;i++){\n\t\t\tint s1=0,t1=0;\n\t\t\tfor(int j=0;j<cnt;j++){\n\t\t\t\tif(S[i]==V[j])s1=j;\n\t\t\t\tif(T[i]==V[j])t1=j;\n\t\t\t}\n\t\t\tx[s1].push_back(make_pair(t1,a[i]/40+b[i]));\n\t\t\tx[t1].push_back(make_pair(s1,a[i]/40+b[i]));\n\t\t}\n\t\tcout<<dfs(G,H)+dfs(H,I)<<endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 120, "memory_kb": 3900, "score_of_the_acc": -0.868, "final_rank": 15 }, { "submission_id": "aoj_2283_1791430", "code_snippet": "#include<iostream>\n#include<vector>\n#include<string>\n#include<algorithm>\n#include<map>\n#include<set>\n#include<utility>\n#include<cmath>\n#include<cstring>\n#include<queue>\n#include<cstdio>\n#define loop(i,a,b) for(int i=a;i<b;i++) \n#define rep(i,a) loop(i,0,a)\n#define pb push_back\n#define mp make_pair\n#define all(in) in.begin(),in.end()\nconst double PI=acos(-1);\nconst double EPS=1e-10;\nconst int inf=1e9;\nusing namespace std;\ntypedef long long ll;\ntypedef vector<int> vi;\ntypedef vector<vi> vvi;\ntypedef pair<int,int> pii;\nint main(){\n\tint n,q;\n\twhile(cin>>n>>q,n+q){\n\t\tstring s,p,g;\n\t\tcin>>s>>p>>g;\n\t\tmap<string,int>m;\n\t\tint co=1;\n\t\tvvi cost(n+1,vi(n+1,inf));\n\t\twhile(q--){\n\t\t\tstring a,b;\n\t\t\tint d,t;\n\t\t\tcin>>a>>b>>d>>t;\n\t\t\tif(m[a]==0)m[a]=co++;\n\t\t\tif(m[b]==0)m[b]=co++;\n\t\t\tcost[m[a]][m[b]]=cost[m[b]][m[a]]=d/40+t;\n\t\t}\n\t\trep(k,n+1)rep(i,n+1)rep(j,n+1)\n\t\t\tcost[i][j]=min(cost[i][j],cost[i][k]+cost[k][j]);\n\t\tcout<<cost[m[s]][m[p]]+cost[m[p]][m[g]]<<endl;\n\t}\n}", "accuracy": 1, "time_ms": 570, "memory_kb": 2296, "score_of_the_acc": -1.222, "final_rank": 16 }, { "submission_id": "aoj_2283_1493018", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nstruct edge{\n int to, cost;\n};\ntypedef pair<int, int> pii;//cost vertex\ntypedef pair<int ,pii> piii;//cost vertex state\nint main(int argc, char *argv[]){\n int n, m;\n while(cin >> n >> m, n){\n string s, p, goal;\n map<string, int> msi;\n cin >> s >> p >> goal;\n msi[s] = msi.size() - 1;\n msi[p] = msi.size() - 1;\n msi[goal] = msi.size() - 1;\n int S = msi[s], P = msi[p], GOAL = msi[goal];\n vector<vector<edge> > g(n, vector<edge>());\n vector<vector<int> > dp(2, vector<int>(n, 1e9));\n for (int i = 0; i < m; i++) {\n int d, t, from, to;\n string a, b;\n cin >> a >> b >> d >> t;\n if(msi.find(a) == msi.end())\n msi[a] = msi.size() - 1;\n if(msi.find(b) == msi.end())\n msi[b] = msi.size() - 1;\n to = msi[a];\n from = msi[b];\n //std::cout << \"to:\" << to << \" from:\" << from << std::endl;\n g[to].push_back(edge{from, d/40 + t});\n g[from].push_back(edge{to, d/40 + t});\n }\n priority_queue<piii, vector<piii>, greater<piii> > que;\n que.push(piii(0, pii(S, 0)));\n dp[0][S] = 0;\n while(!que.empty()){\n int v = que.top().second.first;\n int c = que.top().first;\n int state = que.top().second.second;\n que.pop();\n for (int i = 0; i < g[v].size(); i++) {\n int st = state;\n edge e = g[v][i];\n if(dp[state][e.to] > c + e.cost){\n if(e.to == P)st = 1;\n dp[state][e.to] = c + e.cost;\n que.push(piii(c + e.cost, pii(e.to, st)));\n }\n }\n }\n std::cout << dp[1][GOAL] << std::endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 1504, "score_of_the_acc": -0.0714, "final_rank": 1 } ]
aoj_2284_cpp
Problem D: 伝説の剣 ※ この問題には厨二成分が多く含まれます。胸焼けにご注意ください。 ついに復活を遂げた魔王は、再び世界を闇に包むために人間界に攻め入ろうとしていた。 魔王は、本格的な侵攻を始める前に、まず伝説の剣を破壊することにした。 前回の戦争において、魔王は伝説の剣を持つ勇者によって倒された。 魔王の体は闇の衣によって守られているため、生半可な攻撃では魔王を傷つけることはできない。 しかし、伝説の剣は、神々の加護により、魔王の闇の衣すら容易に貫いてしまう。 そのため、今回の戦争に勝利するためには、なんとしてもその伝説の剣を手に入れて破壊する必要がある。 調査の末、伝説の剣はとある遺跡の最奥に安置され、次代の勇者が現われるのを待っていることがわかった。 伝説の剣は、邪悪な者や盗賊によって奪われるのを防ぐために、周囲に点在する無数の宝珠によって封印されている。 魔王は、強力な魔力を持っているため、触れるだけで宝珠を破壊することができる。 ただし、宝珠による封印は多重構造になっており、表層から順に破壊していく必要がある。 例えば、第1の封印の宝珠を破壊する前に第2以降の封印の宝珠に触れたとしても、破壊することはできない。 また、複数の宝珠が同じ封印を構成していることもあるが、魔王はそのうちの一つに触れるだけで、同時にその封印を破壊することができる。 遺跡には神聖な力が満ちており、並の魔物では入ることすらままならない。 そこで、魔王自ら赴き伝説の剣を回収してくることになった。 いかに魔王といえど、長時間その力を受け続ければただではすまない。 魔王の右腕であるあなたの仕事は、念のため魔王が全ての封印を破壊し、伝説の剣の下に辿りつくまでの時間を求めることである。 Input 入力は、複数のデータセットからなり、入力の終わりはスペースで区切られたゼロ二つからなる行である。 データセットの総数は50以下である。 各データセットは、次の形式をしている。 w h s(1,1) ... s(1,w) s(2,1) ... s(2,w) ... s(h,1) ... s(h,w) w と h は、それぞれ遺跡のフロアを表現する行列データの幅と高さを示す整数であり、それぞれ 1 ≤ w, h ≤ 100 であり、 2 ≤ w + h ≤ 100 と仮定して良い。 続く h 行はそれぞれ、スペースで区切られた w 個の文字から構成されており、文字 s(y,x) は、座標 (y,x) の地点の状態を示す。 その意味は、以下の通りである。 S:魔王が最初にいる地点。フロアに必ず1つだけ存在する。 G:伝説の剣の地点。必ず1つだけ存在する。 .:なにもない。 数字:宝珠がある地点。数字は構成する封印の番号を示す。数字は1以上の整数であり、間の数字に抜けはないとしてよい。 また、魔王は、遺跡のフロア内の上下左右の隣接する座標に移動することができ、その移動にかかる時間を1とする。 破壊できる宝珠は触れるだけで破壊できるため、宝珠の破壊には時間はかからない。 Output 各データセットに対して、魔王が全ての封印を破壊し、伝説に剣に辿りつくために必要な最短時間を出力せよ。 Sample Input 10 10 S . . . . . . . . . . . . . . . . . . . . . . . 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 . . . . . . . . . . . . . . . . . 4 . . . . . . . . . . . . . . 2 . . . . . . . . G 10 10 S . . . . . 3 . . . . . 3 . . . . . . . . . . . 1 . . . . . . . . . . . 4 . . . . . 3 . . . 1 . . . . . . . . . . 3 . . . . . . . . . . . . . . . . . 4 . . . . . . . . . 5 . . . . 2 . . . . . . . . G 10 10 S . . . . . . . . 1 . . . . . 5 . . . . . 4 . . . . . . . . . . . . 8 . 9 . . . . . . . 10 . . . . . . . 7 . G . . . . . . . . 11 . . . . . . 3 . . . . . . . . 6 . . . . . . . 2 . . . . . . . . . . . . 0 0 Output for Sample Input 38 36 71
[ { "submission_id": "aoj_2284_7936801", "code_snippet": "#include <algorithm>\n#include <cstdlib>\n#include <cstring>\n#include <iostream>\n#include <map>\n#include <queue>\n#include <vector>\nusing namespace std;\n#define fr first\n#define sc second\ntypedef pair<int, int> P;\nP s, g;\ntypedef pair<P, P> Q;\nint h, w, sx, sy, gx, gy, c, dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0},\n num[105][105];\nbool used[2500][105][105];\nchar wara[100];\nint bfs();\nint main() {\n while (cin >> w >> h && w || h) {\n memset(num, 0, sizeof(num));\n for (int i = 0; i < h; i++) {\n for (int j = 0; j < w; j++) {\n cin >> wara;\n if (strcmp(wara, \"S\") == 0)\n s = P(i, j);\n else if (strcmp(wara, \"G\") == 0)\n g = P(i, j);\n else if (strcmp(wara, \".\") != 0) {\n c = max(c, atoi(wara));\n num[i][j] = atoi(wara);\n }\n }\n }\n num[g.fr][g.sc] = c + 1;\n cout << bfs() << endl;\n c = 0;\n }\n}\nint bfs() {\n memset(used, false, sizeof(used));\n queue<Q> que;\n que.push(Q(s, P(1, 0)));\n used[1][s.fr][s.sc] = true;\n while (!que.empty()) {\n Q q = que.front();\n que.pop();\n if (q.sc.fr == c + 1 && q.fr == g)\n return q.sc.sc;\n if (num[q.fr.fr][q.fr.sc] == q.sc.fr &&\n !used[q.sc.fr + 1][q.fr.fr][q.fr.sc]) {\n used[q.sc.fr + 1][q.fr.fr][q.fr.sc] = true;\n q.sc.fr++;\n }\n for (int i = 0; i < 4; i++) {\n int nx = q.fr.fr + dx[i], ny = q.fr.sc + dy[i];\n bool flg = true;\n if (nx >= 0 && nx < h && ny >= 0 && ny < w && !used[q.sc.fr][nx][ny]) {\n used[q.sc.fr][nx][ny] = true;\n que.push(Q(P(nx, ny), P(q.sc.fr + (flg ? 0 : 1), q.sc.sc + 1)));\n }\n }\n }\n return -1;\n}", "accuracy": 1, "time_ms": 210, "memory_kb": 30108, "score_of_the_acc": -1.0287, "final_rank": 17 }, { "submission_id": "aoj_2284_7936800", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef pair<int, int> Pi;\ntypedef pair<Pi, int> Pii;\n#define dp second\nconst int INF = 1 << 30;\nint dist(Pi a, Pi b) {\n return abs(a.first - b.first) + abs(a.second - b.second);\n}\nint main() {\n int w, h;\n Pi goal;\n while (cin >> w >> h, w) {\n vector<Pii> num[2500];\n int size = 0, hoge;\n for (int i = 0; i < h; i++) {\n for (int j = 0; j < w; j++) {\n string str;\n cin >> str;\n if (str != \".\") {\n if (str == \"S\")\n num[0].push_back(Pii(Pi(i, j), 0));\n else if (str == \"G\")\n goal = Pi(i, j);\n else {\n num[hoge = atoi(str.c_str())].push_back(Pii(Pi(i, j), INF));\n size = max(hoge, size);\n }\n }\n }\n }\n num[++size].push_back(Pii(goal, INF));\n for (int idx = 1; idx <= size; idx++) {\n for (int i = num[idx].size() - 1; i >= 0; i--) {\n for (int j = num[idx - 1].size() - 1; j >= 0; j--) {\n num[idx][i].dp = min(num[idx][i].dp,\n num[idx - 1][j].dp + dist(num[idx - 1][j].first,\n num[idx][i].first));\n }\n }\n }\n cout << num[size][0].dp << endl;\n }\n return (0);\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3316, "score_of_the_acc": -0.039, "final_rank": 3 }, { "submission_id": "aoj_2284_7936782", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define FOR(i, a, n) for (int i = a; i < n; i++)\n#define REP(i, n) FOR(i, 0, n)\n#define ALL(v) (v).begin(), (v).end()\ntypedef long long int64;\nconst static int INF = 1 << 28;\ntypedef pair<int, int> Pi;\ntypedef pair<int, Pi> Pii;\nint main() {\n int w, h;\n while (cin >> w >> h, w) {\n vector<Pii> mas[2501];\n Pi goal;\n int count = 0;\n for (int i = 0; i < h; i++) {\n for (int j = 0; j < w; j++) {\n string line;\n cin >> line;\n if (isdigit(line[0])) {\n mas[atoi(line.c_str())].push_back(Pii(INF, Pi(i, j)));\n count = max(count, atoi(line.c_str()));\n } else if (line[0] == 'S') {\n mas[0].push_back(Pii(0, Pi(i, j)));\n } else if (line[0] == 'G') {\n goal = Pi(i, j);\n }\n }\n }\n mas[count + 1].push_back(Pii(INF, goal));\n for (int i = 1; !mas[i].empty(); i++) {\n for (int j = 0; j < mas[i].size(); j++) {\n for (int k = 0; k < mas[i - 1].size(); k++) {\n mas[i][j].first = min(\n mas[i][j].first,\n mas[i - 1][k].first +\n abs(mas[i - 1][k].second.first - mas[i][j].second.first) +\n abs(mas[i - 1][k].second.second - mas[i][j].second.second));\n }\n }\n }\n cout << mas[count + 1][0].first << endl;\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3188, "score_of_the_acc": -0.0366, "final_rank": 1 }, { "submission_id": "aoj_2284_5032958", "code_snippet": "#line 2 \"/home/yuruhiya/programming/library/template/template_no_Ruby.cpp\"\n#include <bits/stdc++.h>\n#line 4 \"/home/yuruhiya/programming/library/template/constants.cpp\"\n#include <string_view>\n#line 7 \"/home/yuruhiya/programming/library/template/constants.cpp\"\n\n#define rep(i, n) for (int i = 0; i < (n); ++i)\n#define FOR(i, m, n) for (int i = (m); i < (n); ++i)\n#define rrep(i, n) for (int i = (n)-1; i >= 0; --i)\n#define rfor(i, m, n) for (int i = (m); i >= (n); --i)\n#define unless(c) if (!(c))\n#define all(x) (x).begin(), (x).end()\n#define rall(x) (x).rbegin(), (x).rend()\n#define range_it(a, l, r) (a).begin() + (l), (a).begin() + (r)\n\nusing namespace std;\nusing ll = long long;\nusing LD = long double;\nusing VB = vector<bool>;\nusing VVB = vector<VB>;\nusing VI = vector<int>;\nusing VVI = vector<VI>;\nusing VL = vector<ll>;\nusing VVL = vector<VL>;\nusing VS = vector<string>;\nusing VD = vector<LD>;\nusing PII = pair<int, int>;\nusing VP = vector<PII>;\nusing PLL = pair<ll, ll>;\nusing VPL = vector<PLL>;\ntemplate <class T> using PQ = priority_queue<T>;\ntemplate <class T> using PQS = priority_queue<T, vector<T>, greater<T>>;\nconstexpr int inf = 1000000000;\nconstexpr long long inf_ll = 1000000000000000000ll, MOD = 1000000007;\nconstexpr long double PI = 3.14159265358979323846, EPS = 1e-12;\nnamespace CharacterClass {\n\tconstexpr string_view\n\t digit = \"0123456789\",\n\t xdigit = \"0123456789ABCDEFabcdef\", lower = \"abcdefghijklmnopqrstuvwxyz\",\n\t upper = \"ABCDEFGHIJKLMNOPQRSTUVWXYZ\",\n\t alpha = \"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz\",\n\t alnum = \"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz\",\n\t word = \"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz\",\n\t punct = R\"(!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~)\",\n\t graph =\n\t R\"(!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~)\",\n\t print =\n\t R\"( !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~)\",\n\t blank = \" \\t\", space = \" \\t\\n\\r\\f\\v\";\n} // namespace CharacterClass\n#line 7 \"/home/yuruhiya/programming/library/template/Input.cpp\"\nusing namespace std;\n\n#ifdef _WIN32\n#define getchar_unlocked _getchar_nolock\n#define putchar_unlocked _putchar_nolock\n#define fwrite_unlocked fwrite\n#define fflush_unlocked fflush\n#endif\nclass Scanner {\n\tstatic int gc() {\n\t\treturn getchar_unlocked();\n\t}\n\tstatic char next_char() {\n\t\tchar c;\n\t\tread(c);\n\t\treturn c;\n\t}\n\ttemplate <class T> static void read(T& v) {\n\t\tcin >> v;\n\t}\n\tstatic void read(char& v) {\n\t\twhile (isspace(v = gc()))\n\t\t\t;\n\t}\n\tstatic void read(bool& v) {\n\t\tv = next_char() != '0';\n\t}\n\tstatic void read(string& v) {\n\t\tv.clear();\n\t\tfor (char c = next_char(); !isspace(c); c = gc()) v += c;\n\t}\n\tstatic void read(int& v) {\n\t\tv = 0;\n\t\tbool neg = false;\n\t\tchar c = next_char();\n\t\tif (c == '-') {\n\t\t\tneg = true;\n\t\t\tc = gc();\n\t\t}\n\t\tfor (; isdigit(c); c = gc()) v = v * 10 + (c - '0');\n\t\tif (neg) v = -v;\n\t}\n\tstatic void read(long long& v) {\n\t\tv = 0;\n\t\tbool neg = false;\n\t\tchar c = next_char();\n\t\tif (c == '-') {\n\t\t\tneg = true;\n\t\t\tc = gc();\n\t\t}\n\t\tfor (; isdigit(c); c = gc()) v = v * 10 + (c - '0');\n\t\tif (neg) v = -v;\n\t}\n\tstatic void read(double& v) {\n\t\tv = 0;\n\t\tdouble dp = 1;\n\t\tbool neg = false, after_dp = false;\n\t\tchar c = next_char();\n\t\tif (c == '-') {\n\t\t\tneg = true;\n\t\t\tc = gc();\n\t\t}\n\t\tfor (; isdigit(c) || c == '.'; c = gc()) {\n\t\t\tif (c == '.') {\n\t\t\t\tafter_dp = true;\n\t\t\t} else if (after_dp) {\n\t\t\t\tv += (c - '0') * (dp *= 0.1);\n\t\t\t} else {\n\t\t\t\tv = v * 10 + (c - '0');\n\t\t\t}\n\t\t}\n\t\tif (neg) v = -v;\n\t}\n\tstatic void read(long double& v) {\n\t\tv = 0;\n\t\tlong double dp = 1;\n\t\tbool neg = false, after_dp = false;\n\t\tchar c = next_char();\n\t\tif (c == '-') {\n\t\t\tneg = true;\n\t\t\tc = gc();\n\t\t}\n\t\tfor (; isdigit(c) || c == '.'; c = gc()) {\n\t\t\tif (c == '.') {\n\t\t\t\tafter_dp = true;\n\t\t\t} else if (after_dp) {\n\t\t\t\tv += (c - '0') * (dp *= 0.1);\n\t\t\t} else {\n\t\t\t\tv = v * 10 + (c - '0');\n\t\t\t}\n\t\t}\n\t\tif (neg) v = -v;\n\t}\n\ttemplate <class T, class U> static void read(pair<T, U>& v) {\n\t\tread(v.first);\n\t\tread(v.second);\n\t}\n\ttemplate <class T> static void read(vector<T>& v) {\n\t\tfor (auto& e : v) read(e);\n\t}\n\ttemplate <size_t N = 0, class T> static void read_tuple_impl(T& v) {\n\t\tif constexpr (N < tuple_size_v<T>) {\n\t\t\tread(get<N>(v));\n\t\t\tread_tuple_impl<N + 1>(v);\n\t\t}\n\t}\n\ttemplate <class... T> static void read(tuple<T...>& v) {\n\t\tread_tuple_impl(v);\n\t}\n\tstruct ReadVectorHelper {\n\t\tsize_t n;\n\t\tReadVectorHelper(size_t _n) : n(_n) {}\n\t\ttemplate <class T> operator vector<T>() {\n\t\t\tvector<T> v(n);\n\t\t\tread(v);\n\t\t\treturn v;\n\t\t}\n\t};\n\tstruct Read2DVectorHelper {\n\t\tsize_t n, m;\n\t\tRead2DVectorHelper(const pair<size_t, size_t>& nm) : n(nm.first), m(nm.second) {}\n\t\ttemplate <class T> operator vector<vector<T>>() {\n\t\t\tvector<vector<T>> v(n, vector<T>(m));\n\t\t\tread(v);\n\t\t\treturn v;\n\t\t}\n\t};\n\npublic:\n\tstring read_line() const {\n\t\tstring v;\n\t\tfor (char c = gc(); c != '\\n' && c != '\\0'; c = gc()) v += c;\n\t\treturn v;\n\t}\n\ttemplate <class T> T read() const {\n\t\tT v;\n\t\tread(v);\n\t\treturn v;\n\t}\n\ttemplate <class T> vector<T> read_vector(size_t n) const {\n\t\tvector<T> a(n);\n\t\tread(a);\n\t\treturn a;\n\t}\n\ttemplate <class T> operator T() const {\n\t\treturn read<T>();\n\t}\n\tint operator--(int) const {\n\t\treturn read<int>() - 1;\n\t}\n\tReadVectorHelper operator[](size_t n) const {\n\t\treturn ReadVectorHelper(n);\n\t}\n\tRead2DVectorHelper operator[](const pair<size_t, size_t>& nm) const {\n\t\treturn Read2DVectorHelper(nm);\n\t}\n\tvoid operator()() const {}\n\ttemplate <class H, class... T> void operator()(H&& h, T&&... t) const {\n\t\tread(h);\n\t\toperator()(forward<T>(t)...);\n\t}\n\nprivate:\n\ttemplate <template <class...> class, class...> struct Multiple;\n\ttemplate <template <class...> class V, class Head, class... Tail>\n\tstruct Multiple<V, Head, Tail...> {\n\t\ttemplate <class... Args> using vec = V<vector<Head>, Args...>;\n\t\tusing type = typename Multiple<vec, Tail...>::type;\n\t};\n\ttemplate <template <class...> class V> struct Multiple<V> { using type = V<>; };\n\ttemplate <class... T> using multiple_t = typename Multiple<tuple, T...>::type;\n\ttemplate <size_t N = 0, class T> void multiple_impl(T& t) const {\n\t\tif constexpr (N < tuple_size_v<T>) {\n\t\t\tauto& vec = get<N>(t);\n\t\t\tusing V = typename remove_reference_t<decltype(vec)>::value_type;\n\t\t\tvec.push_back(read<V>());\n\t\t\tmultiple_impl<N + 1>(t);\n\t\t}\n\t}\n\npublic:\n\ttemplate <class... T> auto multiple(size_t h) const {\n\t\tmultiple_t<T...> result;\n\t\twhile (h--) multiple_impl(result);\n\t\treturn result;\n\t}\n} in;\n#define inputs(T, ...) \\\n\tT __VA_ARGS__; \\\n\tin(__VA_ARGS__)\n#define ini(...) inputs(int, __VA_ARGS__)\n#define inl(...) inputs(long long, __VA_ARGS__)\n#define ins(...) inputs(string, __VA_ARGS__)\n#line 7 \"/home/yuruhiya/programming/library/template/Output.cpp\"\n#include <charconv>\n#line 10 \"/home/yuruhiya/programming/library/template/Output.cpp\"\nusing namespace std;\n\nstruct BoolStr {\n\tconst char *t, *f;\n\tBoolStr(const char* _t, const char* _f) : t(_t), f(_f) {}\n} Yes(\"Yes\", \"No\"), yes(\"yes\", \"no\"), YES(\"YES\", \"NO\"), Int(\"1\", \"0\");\nstruct DivStr {\n\tconst char *d, *l;\n\tDivStr(const char* _d, const char* _l) : d(_d), l(_l) {}\n} spc(\" \", \"\\n\"), no_spc(\"\", \"\\n\"), end_line(\"\\n\", \"\\n\"), comma(\",\", \"\\n\"),\n no_endl(\" \", \"\");\nclass Output {\n\tBoolStr B{Yes};\n\tDivStr D{spc};\n\npublic:\n\tvoid put(int v) const {\n\t\tchar buf[12]{};\n\t\tif (auto [ptr, e] = to_chars(begin(buf), end(buf), v); e == errc{}) {\n\t\t\tfwrite(buf, sizeof(char), ptr - buf, stdout);\n\t\t} else {\n\t\t\tassert(false);\n\t\t}\n\t}\n\tvoid put(long long v) const {\n\t\tchar buf[21]{};\n\t\tif (auto [ptr, e] = to_chars(begin(buf), end(buf), v); e == errc{}) {\n\t\t\tfwrite(buf, sizeof(char), ptr - buf, stdout);\n\t\t} else {\n\t\t\tassert(false);\n\t\t}\n\t}\n\tvoid put(bool v) const {\n\t\tput(v ? B.t : B.f);\n\t}\n\tvoid put(vector<bool>::reference v) const {\n\t\tput(v ? B.t : B.f);\n\t}\n\tvoid put(char v) const {\n\t\tputchar_unlocked(v);\n\t}\n\tvoid put(const char* v) const {\n\t\tfwrite_unlocked(v, 1, strlen(v), stdout);\n\t}\n\tvoid put(double v) const {\n\t\tprintf(\"%.20f\", v);\n\t}\n\tvoid put(long double v) const {\n\t\tprintf(\"%.20Lf\", v);\n\t}\n\ttemplate <class T> void put(const T& v) const {\n\t\tcout << v;\n\t}\n\ttemplate <class T, class U> void put(const pair<T, U>& v) const {\n\t\tput(v.first);\n\t\tput(D.d);\n\t\tput(v.second);\n\t}\n\ttemplate <class InputIterater>\n\tvoid put_range(const InputIterater& begin, const InputIterater& end) const {\n\t\tfor (InputIterater i = begin; i != end; ++i) {\n\t\t\tif (i != begin) put(D.d);\n\t\t\tput(*i);\n\t\t}\n\t}\n\ttemplate <class T> void put(const vector<T>& v) const {\n\t\tput_range(v.begin(), v.end());\n\t}\n\ttemplate <class T, size_t N> void put(const array<T, N>& v) const {\n\t\tput_range(v.begin(), v.end());\n\t}\n\ttemplate <class T> void put(const vector<vector<T>>& v) const {\n\t\tfor (size_t i = 0; i < v.size(); ++i) {\n\t\t\tif (i) put(D.l);\n\t\t\tput(v[i]);\n\t\t}\n\t}\n\n\tOutput() = default;\n\tOutput(const BoolStr& _boolstr, const DivStr& _divstr) : B(_boolstr), D(_divstr) {}\n\tOutput& operator()() {\n\t\tput(D.l);\n\t\treturn *this;\n\t}\n\ttemplate <class H> Output& operator()(H&& h) {\n\t\tput(h);\n\t\tput(D.l);\n\t\treturn *this;\n\t}\n\ttemplate <class H, class... T> Output& operator()(H&& h, T&&... t) {\n\t\tput(h);\n\t\tput(D.d);\n\t\treturn operator()(forward<T>(t)...);\n\t}\n\ttemplate <class InputIterator>\n\tOutput& range(const InputIterator& begin, const InputIterator& end) {\n\t\tput_range(begin, end);\n\t\tput(D.l);\n\t\treturn *this;\n\t}\n\ttemplate <class T> Output& range(const T& a) {\n\t\trange(a.begin(), a.end());\n\t\treturn *this;\n\t}\n\ttemplate <class... T> void exit(T&&... t) {\n\t\toperator()(forward<T>(t)...);\n\t\tstd::exit(EXIT_SUCCESS);\n\t}\n\tOutput& flush() {\n\t\tfflush_unlocked(stdout);\n\t\treturn *this;\n\t}\n\tOutput& set(const BoolStr& b) {\n\t\tB = b;\n\t\treturn *this;\n\t}\n\tOutput& set(const DivStr& d) {\n\t\tD = d;\n\t\treturn *this;\n\t}\n\tOutput& set(const char* t, const char* f) {\n\t\tB = BoolStr(t, f);\n\t\treturn *this;\n\t}\n} out;\n#line 6 \"/home/yuruhiya/programming/library/template/functions.cpp\"\nusing namespace std;\n\ntemplate <class T> int sz(const T& v) {\n\treturn v.size();\n}\ntemplate <class T, class U> int lower_index(const T& a, const U& v) {\n\treturn lower_bound(all(a), v) - a.begin();\n}\ntemplate <class T, class U> int upper_index(const T& a, const U& v) {\n\treturn upper_bound(all(a), v) - a.begin();\n}\ntemplate <class T> auto Slice(const T& v, size_t i, size_t len) {\n\treturn i < v.size() ? T(v.begin() + i, v.begin() + min(i + len, v.size())) : T();\n}\ntemplate <class T> T div_ceil(T n, T m) {\n\treturn (n + m - 1) / m;\n}\ntemplate <class T> T div_ceil2(T n, T m) {\n\treturn div_ceil(n, m) * m;\n}\ntemplate <class T> T triangle(T n) {\n\treturn (n & 1) ? (n + 1) / 2 * n : n / 2 * (n + 1);\n}\ntemplate <class T> T nC2(T n) {\n\treturn (n & 1) ? (n - 1) / 2 * n : n / 2 * (n - 1);\n}\ntemplate <class T> T middle(const T& l, const T& r) {\n\treturn l + (r - l) / 2;\n}\ntemplate <class T> bool chmax(T& a, const T& b) {\n\tif (a < b) {\n\t\ta = b;\n\t\treturn true;\n\t}\n\treturn false;\n}\ntemplate <class T> bool chmin(T& a, const T& b) {\n\tif (a > b) {\n\t\ta = b;\n\t\treturn true;\n\t}\n\treturn false;\n}\ntemplate <class T> bool in_range(const T& v, const T& min, const T& max) {\n\treturn min <= v && v < max;\n}\ntemplate <class T> bool in_square(T n) {\n\tT s = sqrt(n);\n\treturn s * s == n || (s + 1) * (s + 1) == n;\n}\ntemplate <class T = long long> T BIT(int b) {\n\treturn T(1) << b;\n}\ntemplate <class T, class U = typename T::value_type> U Gcdv(const T& v) {\n\treturn accumulate(next(v.begin()), v.end(), U(*v.begin()), gcd<U, U>);\n}\ntemplate <class T, class U = typename T::value_type> U Lcmv(const T& v) {\n\treturn accumulate(next(v.begin()), v.end(), U(*v.begin()), lcm<U, U>);\n}\ntemplate <class T, class U> T Pow(T a, U n) {\n\tT result = 1;\n\twhile (n > 0) {\n\t\tif (n & 1) {\n\t\t\tresult *= a;\n\t\t\tn--;\n\t\t} else {\n\t\t\ta *= a;\n\t\t\tn >>= 1;\n\t\t}\n\t}\n\treturn result;\n}\ntemplate <class T, class U> T Powmod(T a, U n, T mod) {\n\tT result = 1;\n\twhile (n > 0) {\n\t\tif (n & 1) {\n\t\t\tresult = result * a % mod;\n\t\t\tn--;\n\t\t} else {\n\t\t\ta = a * a % mod;\n\t\t\tn >>= 1;\n\t\t}\n\t}\n\treturn result;\n}\nnamespace internal {\n\ttemplate <class T, size_t N> auto make_vector(vector<int>& sizes, const T& init) {\n\t\tif constexpr (N == 1) {\n\t\t\treturn vector(sizes[0], init);\n\t\t} else {\n\t\t\tint size = sizes[N - 1];\n\t\t\tsizes.pop_back();\n\t\t\treturn vector(size, make_vector<T, N - 1>(sizes, init));\n\t\t}\n\t}\n} // namespace internal\ntemplate <class T, size_t N>\nauto make_vector(const int (&sizes)[N], const T& init = T()) {\n\tvector s(rbegin(sizes), rend(sizes));\n\treturn internal::make_vector<T, N>(s, init);\n}\n#line 7 \"/home/yuruhiya/programming/library/template/template_no_Ruby.cpp\"\n#if __has_include(<library/dump.hpp>)\n#include <library/dump.hpp>\n#define LOCAL\n#else\n#define dump(...) ((void)0)\n#endif\n\ntemplate <class T> constexpr T oj_local(const T& oj, const T& local) {\n#ifndef LOCAL\n\treturn oj;\n#else\n\treturn local;\n#endif\n}\n#line 5 \"/home/yuruhiya/programming/library/Utility/Point.cpp\"\n#include <optional>\n#line 9 \"/home/yuruhiya/programming/library/Utility/Point.cpp\"\nusing namespace std;\n\nstruct Point {\n\tstatic int H, W;\n\tstatic const vector<Point> direction;\n\tusing direction_iterator = vector<Point>::const_iterator;\n\tstatic void set_range(int _H, int _W) {\n\t\tH = _H;\n\t\tW = _W;\n\t}\n\tstatic constexpr Point zero() {\n\t\treturn {0, 0};\n\t}\n\tstatic constexpr Point one() {\n\t\treturn {1, 1};\n\t}\n\tstatic constexpr Point L() {\n\t\treturn {-1, 0};\n\t}\n\tstatic constexpr Point R() {\n\t\treturn {1, 0};\n\t}\n\tstatic constexpr Point U() {\n\t\treturn {0, -1};\n\t}\n\tstatic constexpr Point D() {\n\t\treturn {0, 1};\n\t}\n\tstatic constexpr Point LU() {\n\t\treturn {-1, -1};\n\t}\n\tstatic constexpr Point LD() {\n\t\treturn {-1, 1};\n\t}\n\tstatic constexpr Point RU() {\n\t\treturn {1, -1};\n\t}\n\tstatic constexpr Point RD() {\n\t\treturn {1, 1};\n\t}\n\n\tint x, y;\n\tconstexpr Point() : x(0), y(0) {}\n\tconstexpr Point(int _x, int _y) : x(_x), y(_y) {}\n\tconstexpr Point(const pair<int, int>& xy) : x(xy.first), y(xy.second) {}\n\tPoint(int n) : x(n % W), y(n / W) {}\n\tconstexpr Point operator+() const {\n\t\treturn *this;\n\t}\n\tconstexpr Point operator-() const {\n\t\treturn {-x, -y};\n\t}\n\tconstexpr Point operator+(const Point& p) const {\n\t\treturn Point(*this) += p;\n\t}\n\tconstexpr Point operator-(const Point& p) const {\n\t\treturn Point(*this) -= p;\n\t}\n\tconstexpr Point operator*(const Point& p) const {\n\t\treturn Point(*this) *= p;\n\t}\n\tconstexpr Point operator/(const Point& p) const {\n\t\treturn Point(*this) /= p;\n\t}\n\tconstexpr Point operator%(const Point& p) const {\n\t\treturn Point(*this) %= p;\n\t}\n\tconstexpr Point operator+(int n) const {\n\t\treturn Point(*this) += n;\n\t}\n\tconstexpr Point operator-(int n) const {\n\t\treturn Point(*this) -= n;\n\t}\n\tconstexpr Point operator*(int n) const {\n\t\treturn Point(*this) *= n;\n\t}\n\tconstexpr Point operator/(int n) const {\n\t\treturn Point(*this) /= n;\n\t}\n\tconstexpr Point operator%(int n) const {\n\t\treturn Point(*this) %= n;\n\t}\n\tconstexpr Point& operator+=(const Point& p) {\n\t\tx += p.x;\n\t\ty += p.y;\n\t\treturn *this;\n\t}\n\tconstexpr Point& operator-=(const Point& p) {\n\t\tx -= p.x;\n\t\ty -= p.y;\n\t\treturn *this;\n\t}\n\tconstexpr Point& operator*=(const Point& p) {\n\t\tx *= p.x;\n\t\ty *= p.y;\n\t\treturn *this;\n\t}\n\tconstexpr Point& operator/=(const Point& p) {\n\t\tx /= p.x;\n\t\ty /= p.y;\n\t\treturn *this;\n\t}\n\tconstexpr Point& operator%=(const Point& p) {\n\t\tx %= p.x;\n\t\ty %= p.y;\n\t\treturn *this;\n\t}\n\tconstexpr Point& operator+=(int n) {\n\t\tx += n;\n\t\ty += n;\n\t\treturn *this;\n\t}\n\tconstexpr Point& operator-=(int n) {\n\t\tx -= n;\n\t\ty -= n;\n\t\treturn *this;\n\t}\n\tconstexpr Point& operator*=(int n) {\n\t\tx *= n;\n\t\ty *= n;\n\t\treturn *this;\n\t}\n\tconstexpr Point& operator/=(int n) {\n\t\tx /= n;\n\t\ty /= n;\n\t\treturn *this;\n\t}\n\tconstexpr Point& operator%=(int n) {\n\t\tx %= n;\n\t\ty %= n;\n\t\treturn *this;\n\t}\n\tconstexpr bool operator==(const Point& p) const {\n\t\treturn x == p.x && y == p.y;\n\t}\n\tconstexpr bool operator!=(const Point& p) const {\n\t\treturn x != p.x || y != p.y;\n\t}\n\tbool operator<(const Point& p) const {\n\t\treturn to_i() < p.to_i();\n\t}\n\tbool operator<=(const Point& p) const {\n\t\treturn to_i() <= p.to_i();\n\t}\n\tbool operator>(const Point& p) const {\n\t\treturn to_i() > p.to_i();\n\t}\n\tbool operator>=(const Point& p) const {\n\t\treturn to_i() >= p.to_i();\n\t}\n\tconstexpr int operator[](int i) const {\n\t\treturn i == 0 ? x : i == 1 ? y : 0;\n\t}\n\tconstexpr bool in_range(int height, int width) const {\n\t\treturn 0 <= x && x < width && 0 <= y && y < height;\n\t}\n\tbool in_range() const {\n\t\treturn in_range(H, W);\n\t}\n\tint to_i() const {\n\t\treturn x + y * W;\n\t}\n\tconstexpr pair<int, int> to_pair() const {\n\t\treturn {x, y};\n\t}\n\tint manhattan_distance(const Point& p) const {\n\t\treturn std::abs(x - p.x) + std::abs(y - p.y);\n\t}\n\tint distance_square(const Point& p) const {\n\t\treturn (x - p.x) * (x - p.x) + (y - p.y) * (y - p.y);\n\t}\n\ttemplate <class Real> Real distance(const Point& p) const {\n\t\treturn sqrt(static_cast<Real>(distance_square(p)));\n\t}\n\tPoint abs(const Point& p) const {\n\t\treturn {std::abs(x - p.x), std::abs(y - p.y)};\n\t}\n\tPoint abs() const {\n\t\treturn {std::abs(x), std::abs(y)};\n\t}\n\tPoint& swap() {\n\t\tstd::swap(x, y);\n\t\treturn *this;\n\t}\n\n\tclass enumrate_adjacent_helper {\n\t\tshared_ptr<Point> p;\n\t\tdirection_iterator first, last;\n\n\t\tclass iterator {\n\t\t\tshared_ptr<Point> p;\n\t\t\tdirection_iterator it;\n\n\t\tpublic:\n\t\t\titerator(shared_ptr<Point> _p, direction_iterator _it) : p(_p), it(_it) {}\n\t\t\tPoint operator*() const {\n\t\t\t\treturn *p + *it;\n\t\t\t}\n\t\t\titerator& operator++() {\n\t\t\t\t++it;\n\t\t\t\treturn *this;\n\t\t\t}\n\t\t\tbool operator!=(iterator other) const {\n\t\t\t\treturn it != other.it;\n\t\t\t}\n\t\t};\n\n\tpublic:\n\t\tenumrate_adjacent_helper(shared_ptr<Point> _p, direction_iterator _first,\n\t\t direction_iterator _last)\n\t\t : p(_p), first(_first), last(_last) {}\n\t\titerator begin() const {\n\t\t\treturn iterator(p, first);\n\t\t}\n\t\titerator end() const {\n\t\t\treturn iterator(p, last);\n\t\t}\n\t};\n\tauto enumrate_adjacent(direction_iterator first, direction_iterator last) const {\n\t\treturn enumrate_adjacent_helper(make_shared<Point>(*this), first, last);\n\t}\n\tauto adjacent4() const {\n\t\treturn enumrate_adjacent(direction.begin(), direction.begin() + 4);\n\t}\n\tauto adjacent8() const {\n\t\treturn enumrate_adjacent(direction.begin(), direction.begin() + 8);\n\t}\n\n\tclass enumrate_adj_in_range_helper {\n\t\tshared_ptr<Point> p;\n\t\tdirection_iterator first, last;\n\n\t\tclass sentinel {};\n\t\tclass iterator {\n\t\t\tshared_ptr<Point> p;\n\t\t\tdirection_iterator first, last;\n\n\t\t\tvoid increment_until_in_range() {\n\t\t\t\tfor (; first != last; ++first) {\n\t\t\t\t\tif ((*p + *first).in_range()) return;\n\t\t\t\t}\n\t\t\t}\n\n\t\tpublic:\n\t\t\titerator(shared_ptr<Point> _p, direction_iterator _first,\n\t\t\t direction_iterator _last)\n\t\t\t : p(_p), first(_first), last(_last) {\n\t\t\t\tincrement_until_in_range();\n\t\t\t}\n\t\t\tPoint operator*() const {\n\t\t\t\treturn *p + *first;\n\t\t\t}\n\t\t\titerator& operator++() {\n\t\t\t\t++first;\n\t\t\t\tincrement_until_in_range();\n\t\t\t\treturn *this;\n\t\t\t}\n\t\t\tbool operator!=(sentinel other) const {\n\t\t\t\treturn first != last;\n\t\t\t}\n\t\t};\n\n\tpublic:\n\t\tenumrate_adj_in_range_helper(shared_ptr<Point> _p, direction_iterator _first,\n\t\t direction_iterator _last)\n\t\t : p(_p), first(_first), last(_last) {}\n\t\titerator begin() const {\n\t\t\treturn iterator(p, first, last);\n\t\t}\n\t\tsentinel end() const {\n\t\t\treturn sentinel();\n\t\t}\n\t};\n\ttemplate <class InputIterator>\n\tauto enumrate_adj_in_range(InputIterator first, InputIterator last) const {\n\t\treturn enumrate_adj_in_range_helper(make_shared<Point>(*this), first, last);\n\t}\n\tauto adj4_in_range() const {\n\t\treturn enumrate_adj_in_range(direction.begin(), direction.begin() + 4);\n\t}\n\tauto adj8_in_range() const {\n\t\treturn enumrate_adj_in_range(direction.begin(), direction.end());\n\t}\n\n\tconstexpr Point left() const {\n\t\treturn {x - 1, y};\n\t}\n\tconstexpr Point right() const {\n\t\treturn {x + 1, y};\n\t}\n\tconstexpr Point up() const {\n\t\treturn {x, y - 1};\n\t}\n\tconstexpr Point down() const {\n\t\treturn {x, y + 1};\n\t}\n\tPoint succ() const {\n\t\tif (x != W - 1) {\n\t\t\treturn {x + 1, y};\n\t\t} else {\n\t\t\treturn {0, y + 1};\n\t\t}\n\t}\n\tPoint pred() const {\n\t\tif (x != 0) {\n\t\t\treturn {x - 1, y};\n\t\t} else {\n\t\t\treturn {W - 1, y - 1};\n\t\t}\n\t}\n\tconstexpr Point moved(char c) const {\n\t\treturn Point(*this).move(c);\n\t}\n\tconstexpr Point& move(char c) {\n\t\tswitch (c) {\n\t\t\tcase 'L':\n\t\t\tcase 'l':\n\t\t\tcase 'W':\n\t\t\tcase '>':\n\t\t\t\tx--;\n\t\t\t\tbreak;\n\t\t\tcase 'R':\n\t\t\tcase 'r':\n\t\t\tcase 'E':\n\t\t\tcase '<':\n\t\t\t\tx++;\n\t\t\t\tbreak;\n\t\t\tcase 'U':\n\t\t\tcase 'u':\n\t\t\tcase 'N':\n\t\t\tcase '^':\n\t\t\t\ty--;\n\t\t\t\tbreak;\n\t\t\tcase 'D':\n\t\t\tcase 'd':\n\t\t\tcase 'S':\n\t\t\tcase 'v':\n\t\t\t\ty++;\n\t\t\t\tbreak;\n\t\t}\n\t\treturn *this;\n\t}\n\tconstexpr Point rotate90() const {\n\t\treturn {y, -x};\n\t}\n\tconstexpr Point rotate180() const {\n\t\treturn {-x, -y};\n\t}\n\tconstexpr Point rotate270() const {\n\t\treturn {-y, x};\n\t}\n\tchar to_direction_char(string_view lrud = \"LRUD\") const {\n\t\tassert(4 <= lrud.size() && lrud.size() <= 5);\n\t\tif (y == 0 && x < 0) {\n\t\t\treturn lrud[0];\n\t\t} else if (y == 0 && x > 0) {\n\t\t\treturn lrud[1];\n\t\t} else if (x == 0 && y < 0) {\n\t\t\treturn lrud[2];\n\t\t} else if (x == 0 && y > 0) {\n\t\t\treturn lrud[3];\n\t\t} else if (lrud.size() == 5) {\n\t\t\treturn lrud[4];\n\t\t} else {\n\t\t\tassert(false);\n\t\t}\n\t}\n\n\tstatic Point to_direction(char c, string_view lrud = \"LRUD\") {\n\t\tassert(lrud.size() == 4);\n\t\tif (c == lrud[0]) {\n\t\t\treturn L();\n\t\t} else if (c == lrud[1]) {\n\t\t\treturn R();\n\t\t} else if (c == lrud[2]) {\n\t\t\treturn U();\n\t\t} else if (c == lrud[3]) {\n\t\t\treturn D();\n\t\t} else {\n\t\t\treturn zero();\n\t\t}\n\t}\n\n\ttemplate <class T, class value_type = typename T::value_type::value_type>\n\tstatic optional<Point> find(const T& grid, const value_type& val) {\n\t\tassert(static_cast<int>(grid.size()) == H);\n\t\tfor (int i = 0; i < H; ++i) {\n\t\t\tassert(static_cast<int>(grid[i].size()) == W);\n\t\t\tfor (int j = 0; j < W; ++j) {\n\t\t\t\tif (grid[i][j] == val) {\n\t\t\t\t\treturn Point(j, i);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn nullopt;\n\t}\n\ttemplate <class T, class Predicate>\n\tstatic optional<Point> find_if(const T& grid, Predicate pred) {\n\t\tassert(static_cast<int>(grid.size()) == H);\n\t\tfor (int i = 0; i < H; ++i) {\n\t\t\tassert(static_cast<int>(grid[i].size()) == W);\n\t\t\tfor (int j = 0; j < W; ++j) {\n\t\t\t\tif (pred(grid[i][j])) {\n\t\t\t\t\treturn Point(j, i);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn nullopt;\n\t}\n\ttemplate <class T, class value_type = typename T::value_type::value_type>\n\tstatic optional<Point> find_one(const T& grid, const value_type& val) {\n\t\tassert(static_cast<int>(grid.size()) == H);\n\t\toptional<Point> result;\n\t\tfor (int i = 0; i < H; ++i) {\n\t\t\tassert(static_cast<int>(grid[i].size()) == W);\n\t\t\tfor (int j = 0; j < W; ++j) {\n\t\t\t\tif (grid[i][j] == val) {\n\t\t\t\t\tassert(!result);\n\t\t\t\t\tresult = Point(j, i);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn result;\n\t}\n\n\tstatic auto enumrate_2D_points() {\n\t\tclass enumrate_2D_points_helper {\n\t\tpublic:\n\t\t\tclass iterator {\n\t\t\t\tPoint p;\n\n\t\t\tpublic:\n\t\t\t\titerator(const Point& _p) : p(_p) {}\n\t\t\t\tPoint operator*() const {\n\t\t\t\t\treturn p;\n\t\t\t\t}\n\t\t\t\titerator& operator++() {\n\t\t\t\t\tp = p.succ();\n\t\t\t\t\treturn *this;\n\t\t\t\t}\n\t\t\t\titerator& operator--() {\n\t\t\t\t\tp = p.pred();\n\t\t\t\t\treturn *this;\n\t\t\t\t}\n\t\t\t\tbool operator!=(iterator other) const {\n\t\t\t\t\treturn p != other.p;\n\t\t\t\t}\n\t\t\t};\n\t\t\titerator begin() const {\n\t\t\t\treturn iterator(Point(0, 0));\n\t\t\t}\n\t\t\titerator end() const {\n\t\t\t\treturn iterator(Point(0, H));\n\t\t\t}\n\t\t};\n\t\treturn enumrate_2D_points_helper();\n\t}\n\n\tfriend ostream& operator<<(ostream& os, const Point& p) {\n\t\treturn os << '(' << p.x << \", \" << p.y << ')';\n\t}\n\tfriend istream& operator>>(istream& is, Point& p) {\n\t\treturn is >> p.y >> p.x;\n\t}\n};\nint Point::H, Point::W;\nconst vector<Point> Point::direction{Point::R(), Point::D(), Point::U(), Point::L(),\n Point::RD(), Point::LU(), Point::RU(), Point::LD()};\n#line 3 \"a.cpp\"\n\nint solve(int w, int h) {\n\tvector<vector<string>> s = in[{h, w}];\n\tPoint::set_range(h, w);\n\tint n = 0;\n\trep(i, h) rep(j, w) {\n\t\ttry {\n\t\t\tint val = stoi(s[i][j]);\n\t\t\tchmax(n, val);\n\t\t} catch (...) {\n\t\t}\n\t}\n\n\tvector<vector<Point>> points(n + 2);\n\tpoints.front().push_back(Point::find(s, \"S\").value());\n\tpoints.back().push_back(Point::find(s, \"G\").value());\n\tfor (Point p : Point::enumrate_2D_points()) {\n\t\tstring c = s[p.y][p.x];\n\t\ttry {\n\t\t\tint c_val = stoi(c);\n\t\t\tpoints[c_val].push_back(p);\n\t\t} catch (...) {\n\t\t}\n\t}\n\n\tVVI dp(n + 2);\n\tdp[0].push_back(0);\n\tFOR(i, 1, n + 2) {\n\t\tint s1 = points[i - 1].size(), s2 = points[i].size();\n\t\tdp[i].assign(s2, inf);\n\t\trep(j, s1) rep(k, s2) {\n\t\t\tchmin(dp[i][k],\n\t\t\t dp[i - 1][j] + points[i - 1][j].manhattan_distance(points[i][k]));\n\t\t}\n\t}\n\treturn dp.back().front();\n}\n\nint main() {\n\twhile (true) {\n\t\tini(x, y);\n\t\tif (x + y == 0) break;\n\t\tout(solve(x, y));\n\t}\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 3912, "score_of_the_acc": -0.2171, "final_rank": 15 }, { "submission_id": "aoj_2284_3736970", "code_snippet": "#include<iostream>\n#include<vector>\n#include<cctype>\nusing namespace std;\n\nint main(){\n int w, h;\n while(cin >> w >> h, w+h){\n vector<int> xi[2525], yj[2525];\n vector<vector<int>> dp(2525, vector<int>(2525,1<<30));\n int gi = -1, gj = -1, ma = 0;\n for(int i = 0; i < h; i++){\n for(int j = 0; j < w; j++){\n string s; cin >> s;\n if(isdigit(s[0])){\n int num = stoi(s);\n ma = max(ma, num);\n xi[num].push_back(i), yj[num].push_back(j);\n }else if(s == \"S\"){\n xi[0].push_back(i), yj[0].push_back(j);\n }else if(s == \"G\"){\n gi = i, gj = j;\n }\n }\n }\n ma++;\n xi[ma].push_back(gi), yj[ma].push_back(gj);\n dp[0][0] = 0;\n for(int i = 0; xi[i+1].size(); i++){\n for(int j = 0; j < xi[i].size(); j++){\n for(int k = 0; k < xi[i+1].size(); k++){\n dp[i+1][k] = min(dp[i+1][k], dp[i][j] + abs(xi[i][j]-xi[i+1][k])+abs(yj[i][j]-yj[i+1][k]));\n }\n }\n }\n cout << dp[ma][0] << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 280, "memory_kb": 28428, "score_of_the_acc": -1.1631, "final_rank": 18 }, { "submission_id": "aoj_2284_3504690", "code_snippet": "#include<iostream>\n#include<algorithm>\n#include<cstdio>\n#include<cmath>\n#include<cctype>\n#include<math.h>\n#include<string>\n#include<string.h>\n#include<stack>\n#include<queue>\n#include<vector>\n#include<utility>\n#include<set>\n#include<map>\n#include<stdlib.h>\n#include<iomanip>\n\nusing namespace std;\n\n#define ll long long\n#define ld long double\n#define EPS 0.0000000001\n#define INF 1e9\n#define LINF (ll)INF*INF\n#define MOD 1000000007\n#define rep(i,n) for(int i=0;i<(n);i++)\n#define loop(i,a,n) for(int i=a;i<(n);i++)\n#define all(in) in.begin(),in.end()\n#define shosu(x) fixed<<setprecision(x)\n\n#define int ll //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n\ntypedef vector<int> vi;\ntypedef vector<string> vs;\ntypedef pair<int,int> pii;\ntypedef vector<pii> vp;\n\nint gcd(int a, int b){\n if(b==0) return a;\n return gcd(b,a%b);\n}\nint lcm(int a, int b){\n return a/gcd(a,b)*b;\n}\n\n\nstruct edge { int to, cost;};\n\nclass Dijkstra{\nprivate:\n vector< vector<edge> > g;\n\npublic:\n Dijkstra(){};\n Dijkstra(int size){\n g.resize(size);\n }\n\n vi dist;\n\n void add(int a, int b, int c){//無向辺\n edge e1 = {b, c}, e2 = {a, c};\n g[a].push_back(e1);\n //g[b].push_back(e2);//有向辺の場合はここをコメントアウトする\n }\n\n void calc(int s){\n priority_queue< pii, vector<pii>, greater<pii> > q;\n int n = g.size();\n dist = vi (n, INF);\n dist[s] = 0;\n q.push(pii(0, s));\n\n while(!q.empty()){\n pii p = q.top();\n q.pop();\n int v = p.second;\n if(dist[v] < p.first)continue; //更新されていたら何もしない\n for(int i = 0; i < g[v].size(); i++){\n edge e = g[v][i];\n if(dist[e.to] > dist[v] + e.cost){\n dist[e.to] = dist[v] + e.cost;\n q.push(pii(dist[e.to], e.to));\n }\n }\n }\n }\n\n};\n\n\nint h,w;\n\nint f(pii p){\n return p.first * w + p.second;\n}\nint g(pii x, pii y){\n return abs(x.first - y.first) + abs(x.second - y.second);\n}\nint num(string s){\n int ret = 0;\n rep(i,s.size()){\n ret *= 10;\n ret += s[i] - '0';\n }\n return ret;\n}\n\nsigned main(void) {\n while(cin >> w >> h, h){\n int sx,sy,gx,gy;\n vector<vp> v(h*w);\n rep(i,h)rep(j,w){\n string in;\n cin >> in;\n if(in == \".\");\n else if(in == \"S\")sx = i, sy = j;\n else if(in == \"G\")gx = i, gy = j;\n else v[num(in)].push_back(pii(i,j));\n }\n v[0].push_back(pii(sx,sy));\n int ind = 0;\n while(v[ind].size())ind++;\n v[ind].push_back(pii(gx,gy));\n while(v.back().size() == 0)v.pop_back();\n /*\n rep(i,v.size()){\n cout << i << endl;\n rep(j,v[i].size())cout << v[i][j].first << \" \" << v[i][j].second << endl;\n }\n */\n Dijkstra dij(h*w);\n rep(i,v.size()-1){\n rep(j,v[i].size())rep(k,v[i+1].size()){\n //v[i][j]からv[i+1][k]に辺をはる\n dij.add(f(v[i][j]),f(v[i+1][k]), g(v[i][j],v[i+1][k]));\n }\n }\n dij.calc(f(v.front().front()));\n cout << dij.dist[f(v.back().front())] << endl;\n }\n\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 10060, "score_of_the_acc": -0.1921, "final_rank": 14 }, { "submission_id": "aoj_2284_3378229", "code_snippet": "#include<iostream>\n#include<vector>\n#include<algorithm>\n#include<utility>\n#include<string>\n#include<cmath>\n#include<cstring>\n#include<queue>\n#include<map>\n#include<climits>\n#include<set>\n\n#define REP(i, n) for(int i = 0;i < n;i++)\n#define REPR(i, n) for(int i = n;i >= 0;i--)\n#define FOR(i, m, n) for(int i = m;i < n;i++)\n#define FORR(i, m, n) for(int i = m;i >= n;i--)\n#define SORT(v, n) sort(v, v+n);\n#define VSORT(v) sort(v.begin(), v.end());\n#define llong long long\n#define pb(a) push_back(a)\nusing namespace std;\ntypedef pair<int, int> pii;\ntypedef long long int ll;\ntypedef pair<ll,ll> pll;\nint dx[4] = {1,0,0,-1};\nint dy[4] = {0,1,-1,0};\n#define MOD 1000000007\n#define ARRAY_MAX 3000\n\nconst int INF = 1e9+7;\n\n\nint main(){\n\n int w,h;\n while(cin >> w >> h,w|h){\n\n vector<pll> g[ARRAY_MAX];\n ll gy,gx,n = 0;\n for(int i = 0;i < h;i++){\n for(int j = 0;j < w;j++){\n string s;\n cin >> s;\n if(s == \".\"){\n continue;\n }\n if(s == \"S\"){\n g[0].push_back({i,j});\n }else if(s == \"G\"){\n gy = i;\n gx = j;\n }else{\n ll num = (ll)stoi(s);\n g[num].push_back({i,j});\n n = max(n,num);//最大の数字\n }\n }\n }\n n++;\n g[n].push_back({gy,gx});\n vector<vector<ll> > dp(n+1);\n for(int i = 0;i < n+1;i++){\n dp[i].resize(g[i].size(),INF);\n }\n dp[0][0] = 0;\n //dp[Y][X]:数字Yのついた地点のX個目の地点までの最短距離\n\n for(int i = 1;i <= n;i++){\n for(int j = 0;j < g[i-1].size();j++){\n for(int k = 0;k < g[i].size();k++){\n ll len = abs(g[i-1][j].first-g[i][k].first) + abs(g[i-1][j].second-g[i][k].second);\n dp[i][k] = min(dp[i][k],dp[i-1][j] + len);\n }\n }\n }\n cout << dp[n][0] << endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3332, "score_of_the_acc": -0.0393, "final_rank": 4 }, { "submission_id": "aoj_2284_3378221", "code_snippet": "#include<iostream>\n#include<vector>\n#include<algorithm>\n#include<utility>\n#include<string>\n#include<cmath>\n#include<cstring>\n#include<queue>\n#include<map>\n#include<climits>\n#include<set>\n\n#define REP(i, n) for(int i = 0;i < n;i++)\n#define REPR(i, n) for(int i = n;i >= 0;i--)\n#define FOR(i, m, n) for(int i = m;i < n;i++)\n#define FORR(i, m, n) for(int i = m;i >= n;i--)\n#define SORT(v, n) sort(v, v+n);\n#define VSORT(v) sort(v.begin(), v.end());\n#define llong long long\n#define pb(a) push_back(a)\nusing namespace std;\ntypedef pair<int, int> pii;\ntypedef long long int ll;\ntypedef pair<ll,ll> pll;\nint dx[4] = {1,0,0,-1};\nint dy[4] = {0,1,-1,0};\n#define MOD 1000000007\n#define ARRAY_MAX 3000\n\nconst int INF = 1e9+7;\n\n\nint main(){\n\n int w,h;\n while(cin >> w >> h,w|h){\n\n vector<pll> g[ARRAY_MAX];\n ll gy,gx,n = 0;\n for(int i = 0;i < h;i++){\n for(int j = 0;j < w;j++){\n string s;\n cin >> s;\n if(s == \".\"){\n continue;\n }\n if(s == \"S\"){\n g[0].push_back({i,j});\n }else if(s == \"G\"){\n gy = i;\n gx = j;\n }else{\n ll num = (ll)stoi(s);\n g[num].push_back({i,j});\n n = max(n,num);//最大の数字\n }\n }\n }\n n++;\n g[n].push_back({gy,gx});\n vector<vector<ll> > dp(n+1);\n for(int i = 0;i < n+1;i++){\n dp[i].resize(g[i].size(),INF);\n }\n dp[0][0] = 0;\n\n for(int i = 1;i <= n;i++){\n for(int j = 0;j < g[i-1].size();j++){\n for(int k = 0;k < g[i].size();k++){\n ll len = abs(g[i-1][j].first-g[i][k].first) + abs(g[i-1][j].second-g[i][k].second);\n dp[i][k] = min(dp[i][k],dp[i-1][j] + len);\n }\n }\n }\n cout << dp[n][0] << endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3436, "score_of_the_acc": -0.0413, "final_rank": 5 }, { "submission_id": "aoj_2284_3354246", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef pair<ll, ll> P;\nconst ll MOD = 1000000007;\nconst int IINF = INT_MAX;\nconst ll LLINF = LLONG_MAX;\nconst int MAX_N = int(2e5 + 5);\nconst double EPS = 1e-8;\nconst int di[] = {0, 1, 0, -1}, dj[] = {1, 0, -1, 0};\n#define REP(i, n) for (int i = 0; i < n; i++)\n#define REPR(i, n) for (int i = n; i >= 0; i--)\n#define SORT(v) sort((v).begin(), (v).end())\n#define ALL(v) (v).begin(), (v).end()\n\nint main() {\n int h, w;\n while(cin >> w >> h, h){\n vector<P> g[10005];\n ll n=0, gi, gj;\n REP(i,h)REP(j,w){\n string s;\n cin >> s;\n if(s==\".\"){\n continue;\n }\n else if(s==\"S\")g[0].push_back({i,j});\n else if(s==\"G\"){\n gi = i;\n gj = j;\n }\n else{\n ll num = stoi(s);\n g[num].push_back({i,j});\n n = max(n,num);\n }\n }\n n++;\n g[n].push_back({gi,gj});\n vector<vector<ll>> dp(n+1);\n REP(i,n+1) dp[i].resize(g[i].size(),LLINF/3);\n dp[0][0] = 0;\n REP(i,n){\n REP(j,g[i].size()){\n REP(k,g[i+1].size()){\n ll d = abs(g[i][j].first-g[i+1][k].first) + abs(g[i][j].second-g[i+1][k].second);\n dp[i+1][k] = min(dp[i+1][k],dp[i][j] + d);\n }\n }\n }\n cout << dp[n][0] << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3456, "score_of_the_acc": -0.0417, "final_rank": 6 }, { "submission_id": "aoj_2284_3354155", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\n#define lint long long\n#define P pair<int, int>\n#define LLP pair<long long, long long>\n#define REP(i, x, n) for(int i = (x), i##_len = (int)(n) ; i < i##_len ; ++i)\n#define rep(i, n) for(int i = 0, i##_len = (int)(n) ; i < i##_len ; ++i)\n#define repr(i, n) for(int i = (int)(n) - 1 ; i >= 0 ; --i)\n#define SORT(x) sort((x).begin(), (x).end())\n#define SORT_INV(x) sort((x).rbegin(), (x).rend())\n\nconst int IINF = 1e9 + 10;\nconst long long LLINF = (long long)1e18 + 10;\nconst long long MOD = (long long)1e9 + 7;\nconst int dx4[] = {1, 0, -1, 0}, dy4[] = {0, 1, 0, -1};\nconst int dx8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dy8[] = {0, -1, -1, -1, 0, 1, 1, 1};\nconst double EPS = 1e-8;\n\n#define int long long\n\nint w, h;\nvector< vector<string> > s;\nvector< vector< P > > v(2500);\nint sy, sx, gy, gx;\nint maxi;\nint memo[2525][2525];\n\nint solve(int i, int j){\n if(i == maxi + 1){\n return 0;\n }\n if(memo[i][j] >= 0){\n return memo[i][j];\n }\n\n int res = LLINF;\n rep(k, v[i + 1].size()){\n res = min(res, solve(i + 1, k) + abs(v[i][j].first - v[i + 1][k].first) + abs(v[i][j].second - v[i + 1][k].second));\n }\n return memo[i][j] = res;\n}\n\nsigned main(){\n cin.tie(0);\n ios::sync_with_stdio(false);\n\n while(cin >> w >> h, w){\n\n rep(i, 2500){\n v[i].clear();\n }\n\n maxi = 0;\n\n fill(memo[0], memo[2525], -1);\n\n s.resize(h);\n rep(i, h){\n s[i].resize(w);\n rep(j, w){\n cin >> s[i][j];\n if(s[i][j] == \"S\"){\n sy = i;\n sx = j;\n }else if(s[i][j] == \"G\"){\n gy = i;\n gx = j;\n }else if(s[i][j] != \".\"){\n int num = 0;\n for(auto x : s[i][j]){\n num *= 10;\n num += x - '0';\n }\n maxi = max(maxi, num);\n v[num].push_back({i, j});\n }\n }\n }\n\n v[0].push_back({sy, sx});\n v[maxi + 1].push_back({gy, gx});\n\n int ans = solve(0, 0);\n cout << ans << endl;\n \n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 430, "memory_kb": 53460, "score_of_the_acc": -2, "final_rank": 20 }, { "submission_id": "aoj_2284_3341637", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))\n#define all(x) (x).begin(),(x).end()\n#define pb push_back\n#define fi first\n#define se second\n#define dbg(x) cout<<#x\" = \"<<((x))<<endl\ntemplate<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<\"(\"<<p.fi<<\",\"<<p.se<<\")\";return o;}\ntemplate<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<\"[\";for(T t:v){o<<t<<\",\";}o<<\"]\";return o;}\n\nusing pi = pair<int,int>;\nconst int INF = INT_MAX/3;\n\nint dist(pi p, pi q){\n return abs(p.fi-q.fi) + abs(p.se-q.se);\n}\n\nbool isnumber(string s){\n return isdigit(s[0]);\n}\n\nint main(){\n int w,h;\n while(cin >>w >>h,w){\n vector<vector<string>> s(h, vector<string>(w));\n rep(i,h)rep(j,w) cin >>s[i][j];\n\n int mx = 0;\n rep(i,h)rep(j,w)if(isnumber(s[i][j])) mx = max(mx, stoi(s[i][j]));\n\n vector<vector<pi>> pos(mx+2);\n rep(i,h)rep(j,w){\n if(isnumber(s[i][j])) pos[stoi(s[i][j])].pb({i,j});\n else if(s[i][j]==\"S\") pos[0].pb({i,j});\n else if(s[i][j]==\"G\") pos[mx+1].pb({i,j});\n }\n\n vector<int> dp(1,0);\n for(int i=1; i<=mx+1; ++i){\n int P = pos[i-1].size();\n int Q = pos[i].size();\n\n vector<int> nx(Q,INF);\n rep(j,P)rep(k,Q){\n nx[k] = min(nx[k], dp[j]+dist(pos[i-1][j], pos[i][k]));\n }\n\n dp = nx;\n }\n cout << dp[0] << \"\\n\";\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3288, "score_of_the_acc": -0.0385, "final_rank": 2 }, { "submission_id": "aoj_2284_2451190", "code_snippet": "#include <bits/stdc++.h>\n#define r(i,n) for(int i=0;i<n;i++)\nusing namespace std;\ntypedef pair<int,int> P;\nvector<P>v[100001];\nvector<int> dp[10001];\nint get(string x){\n int res=0;\n r(i,x.size())res=res*10+(x[i]-'0');\n return res;\n}\nint dist(P a,P b){\n return abs(a.first-b.first)+abs(a.second-b.second);\n}\nstring s;\nint e,w,h;\nint main(){\n while(cin>>w>>h,h){\n r(i,100001)v[i].clear();\n r(i,10001)dp[i].clear();\n P t;\n r(i,h)r(j,w){\n cin>>s;\n if(s==\"G\")t=P(i,j);\n if(s==\"S\")v[0].push_back(P(i,j));\n if(isdigit(s[0])){\n int x=get(s);\n v[x].push_back(P(i,j));\n dp[x].push_back(1e9);\n }\n }\n r(i,99999)if(v[i].size()==0){\n v[i].push_back(t);\n dp[i].push_back(1e9);\n e=i;\n break;\n }\n dp[0].push_back(0);\n r(i,e)r(j,v[i].size())r(k,v[i+1].size())\n dp[i+1][k]=min(dp[i+1][k],dp[i][j]+dist(v[i][j],v[i+1][k]));\n cout<<dp[e][0]<<endl;\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 5960, "score_of_the_acc": -0.0897, "final_rank": 13 }, { "submission_id": "aoj_2284_2247068", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#define int long long\ntypedef pair<int,int> P;\n#define Y first\n#define X second\nsigned main(){\n int w,h;\n while(cin>>w>>h,w||h){\n string s[h][w];\n for(int i=0;i<h;i++)\n for(int j=0;j<w;j++)\n cin>>s[i][j];\n vector<P> v[50000];\n int gy,gx,gd=0;\n for(int i=0;i<h;i++){\n for(int j=0;j<w;j++){\n if(s[i][j]==\".\") continue;\n if(s[i][j]==\"S\"){\n v[0].push_back(P(i,j));\n continue;\n }\n if(s[i][j]==\"G\"){\n gy=i;gx=j;\n continue;\n }\n int k=stoi(s[i][j]);\n v[k].push_back(P(i,j));\n gd=max(gd,k);\n }\n }\n v[++gd].push_back(P(gy,gx));\n int dp[h][w];\n for(int i=0;i<h;i++)\n for(int j=0;j<w;j++)\n dp[i][j]=1LL<<55LL;\n dp[v[0][0].Y][v[0][0].X]=0;\n for(int i=0;i<gd;i++){\n for(int j=0;j<(int)v[i].size();j++){\n for(int k=0;k<(int)v[i+1].size();k++){\n int cost=abs(v[i+1][k].Y-v[i][j].Y)+abs(v[i+1][k].X-v[i][j].X);\n dp[v[i+1][k].Y][v[i+1][k].X]=\n min(dp[v[i+1][k].Y][v[i+1][k].X],dp[v[i][j].Y][v[i][j].X]+cost);\n }\n }\n }\n cout<<dp[v[gd][0].Y][v[gd][0].X]<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 4544, "score_of_the_acc": -0.0626, "final_rank": 12 }, { "submission_id": "aoj_2284_2244136", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#define int long long\ntypedef pair<int,int> P;\n#define Y first\n#define X second\nsigned main(){\n int w,h;\n while(cin>>w>>h,w||h){\n string s[h][w];\n for(int i=0;i<h;i++)\n for(int j=0;j<w;j++)\n\tcin>>s[i][j];\n vector<P> v[50000];\n int gy,gx,gd=0;\n for(int i=0;i<h;i++){\n for(int j=0;j<w;j++){\n\tif(s[i][j]==\".\") continue;\n\tif(s[i][j]==\"S\"){\n\t v[0].push_back(P(i,j));\n\t continue;\n\t}\n\tif(s[i][j]==\"G\"){\n\t gy=i;gx=j;\n\t continue;\n\t}\n\tint k=stoi(s[i][j]);\n\tv[k].push_back(P(i,j));\n\tgd=max(gd,k);\n }\n }\n v[++gd].push_back(P(gy,gx));\n int dp[h][w];\n for(int i=0;i<h;i++)\n for(int j=0;j<w;j++)\n\tdp[i][j]=1LL<<55LL;\n dp[v[0][0].Y][v[0][0].X]=0;\n for(int i=0;i<gd;i++){\n for(int j=0;j<(int)v[i].size();j++){\n\tfor(int k=0;k<(int)v[i+1].size();k++){\n\t int cost=abs(v[i+1][k].Y-v[i][j].Y)+abs(v[i+1][k].X-v[i][j].X);\n\t dp[v[i+1][k].Y][v[i+1][k].X]=\n\t min(dp[v[i+1][k].Y][v[i+1][k].X],dp[v[i][j].Y][v[i][j].X]+cost);\n\t}\n }\n }\n cout<<dp[v[gd][0].Y][v[gd][0].X]<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 4524, "score_of_the_acc": -0.0622, "final_rank": 11 }, { "submission_id": "aoj_2284_2130094", "code_snippet": "#include <queue>\n#include <string>\n#include <vector>\n#include <iostream>\nusing namespace std;\nconst vector<int> dir = { 1, 0, -1, 0 };\nint H, W; string s[109][109];\nint main() {\n\twhile (cin >> W >> H, H | W) {\n\t\tint sx, sy, gx, gy, cnt = 0;\n\t\tfor (int i = 0; i < H; i++) {\n\t\t\tfor (int j = 0; j < W; j++) {\n\t\t\t\tcin >> s[i][j];\n\t\t\t\tif (s[i][j] == \"S\") sx = j, sy = i;\n\t\t\t\tif (s[i][j] == \"G\") gx = j, gy = i;\n\t\t\t\tif (isdigit(s[i][j][0])) cnt = max(cnt, stoi(s[i][j]));\n\t\t\t}\n\t\t}\n\t\tvector<vector<int> > px(cnt + 2), py(cnt + 2);\n\t\tfor (int i = 0; i < H; i++) {\n\t\t\tfor (int j = 0; j < W; j++) {\n\t\t\t\tint val = -1;\n\t\t\t\tif (s[i][j] == \"S\") val = 0;\n\t\t\t\tif (s[i][j] == \"G\") val = cnt + 1;\n\t\t\t\tif (isdigit(s[i][j][0])) val = stoi(s[i][j]);\n\t\t\t\tif (val >= 0) {\n\t\t\t\t\tpx[val].push_back(j);\n\t\t\t\t\tpy[val].push_back(i);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tvector<vector<int> > dp(cnt + 2);\n\t\tdp[0] = { 0 };\n\t\tfor (int i = 1; i <= cnt + 1; i++) {\n\t\t\tdp[i].resize(px[i].size(), 999999999);\n\t\t\tfor (int j = 0; j < dp[i].size(); j++) {\n\t\t\t\tfor (int k = 0; k < dp[i - 1].size(); k++) {\n\t\t\t\t\tint dist = abs(px[i][j] - px[i - 1][k]) + abs(py[i][j] - py[i - 1][k]);\n\t\t\t\t\tdp[i][j] = min(dp[i][j], dp[i - 1][k] + dist);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tcout << dp[cnt + 1][0] << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3596, "score_of_the_acc": -0.0444, "final_rank": 8 }, { "submission_id": "aoj_2284_2125228", "code_snippet": "#include<bits/stdc++.h>\n#define rep(i,n)for(int i=0;i<n;i++)\nusing namespace std;\n\nvector<int>x[2502], y[2502];\nint d[2502][2502];\nint p;\n\nint main() {\n\tint w, h;\n\twhile (scanf(\"%d%d\", &w, &h), w) {\n\t\trep(i, 2502) {\n\t\t\tx[i].clear(); y[i].clear();\n\t\t}\n\t\tp = 1;//s->0,g->p\n\t\tint gx, gy;\n\t\trep(i, h) {\n\t\t\trep(j, w) {\n\t\t\t\tstring s; cin >> s;\n\t\t\t\tif (s == \".\");\n\t\t\t\telse if (s == \"S\")\n\t\t\t\t\tx[0].push_back(i), y[0].push_back(j);\n\t\t\t\telse if (s == \"G\")gx = i, gy = j;\n\t\t\t\telse {\n\t\t\t\t\tint d = stoi(s); p = max(p, d + 1);\n\t\t\t\t\tx[d].push_back(i); y[d].push_back(j);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tx[p].push_back(gx); y[p].push_back(gy);\n\t\tmemset(d, 0x3f, sizeof(d));\n\t\td[0][0] = 0;\n\t\tfor (int i = 1; i <= p; i++) {\n\t\t\trep(j, x[i].size()) {\n\t\t\t\trep(k, x[i - 1].size()) {\n\t\t\t\t\td[i][j] = min(d[i][j], d[i - 1][k] + abs(x[i - 1][k] - x[i][j]) + abs(y[i - 1][k] - y[i][j]));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tprintf(\"%d\\n\", d[p][0]);\n\t}\n}", "accuracy": 1, "time_ms": 90, "memory_kb": 28004, "score_of_the_acc": -0.7026, "final_rank": 16 }, { "submission_id": "aoj_2284_2118098", "code_snippet": "#include<iostream>\n#include<string>\n#include<vector>\n#include<algorithm>\n#define p pair<int,int>\n#define P pair<p,int>\nusing namespace std;\n\nint parse(string a) {\n\tint b = 0;\n\tfor (char c : a)b = b * 10 + c - 48;\n\treturn b;\n}\nint main() {\n\tint a, b;\n\twhile (cin>>b>>a, a | b) {\n\t\tp G;\n\t\tvector<P>d[20000];\n\t\tint MAX = 0;\n\t\tfor (int e = 0; e < a; e++) {\n\t\t\tfor (int g = 0; g < b; g++) {\n\t\t\t\tstring o;\n\t\t\t\tcin >> o;\n\t\t\t\tif (o==\"S\")d[0].push_back(P(p(e,g), 0));\n\t\t\t\telse if (o==\"G\")G = p(e, g);\n\t\t\t\telse if (o!=\".\") {\n\t\t\t\t\td[parse(o)].push_back(P(p(e, g), 0));\n\t\t\t\t\tMAX = max(MAX, parse(o));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tfor (int i = 1; i <= MAX; i++) {\n\t\t\tfor (int j = 0; j < d[i].size();j++) {\n\t\t\t\tint MIN = 1 << 29;\n\t\t\t\tfor (int k = 0; k < d[i - 1].size(); k++) {\n\t\t\t\t\tMIN = min(MIN, d[i - 1][k].second + abs(d[i][j].first.first- d[i - 1][k].first.first) + abs(d[i][j].first.second - d[i - 1][k].first.second));\n\t\t\t\t}\n\t\t\t\td[i][j].second = MIN;\n\t\t\t}\n\t\t}\n\t\tint MINCOST = 1 << 29;\n\t\tfor (P s : d[MAX]) {\n\t\t\tMINCOST = min(MINCOST, s.second + abs(G.first-s.first.first) + abs(G.second-s.first.second));\n\t\t}\n\t\tcout << MINCOST << endl;\n\t}\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3600, "score_of_the_acc": -0.0445, "final_rank": 9 }, { "submission_id": "aoj_2284_1964715", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define REP(i, s, e) for(int i = (int)s; i < (int) e; i++)\n#define rep(i, n) REP(i, 0 ,n)\n#define all(x) (x).begin(), (x).end()\n#define rall(x) (x).rbegin(), (x).rend()\n\n#define fi first\n#define se second\n#define pb push_back\n#define mp nake_pair\n\ntypedef long long ll;\ntypedef vector<int> vi;\n\nstring s;\nint perseInt(const string& s){\n int ret = 0;\n rep(i, s.size()) ret = ret *10 + (s[i]-'0');\n return ret;\n}\n\nstruct P{\n int x, y;\n P(){};\n P(int x, int y) : x(x), y(y){};\n int dist(const P& p){\n return abs(x-p.x)+abs(y-p.y);\n }\n};\nvector<P> ps[2600];\n\nint main(){\n int w, h;\n while(cin>>w>>h && w && h){\n int m = 0;\n P g;\n rep(i, 2600) ps[i].clear();\n rep(i, h) rep(j, w){\n cin>>s;\n if(s == \".\") continue;\n else if(s == \"S\") ps[0].pb({i, j});\n else if(s == \"G\") g = {i, j};\n else {\n int k = perseInt(s);\n ps[k].pb({i, j});\n m = max(m, k);\n }\n }\n ps[++m].pb(g);\n vector<vi> d(2600, vi(2600, 1e9));\n d[0][0] = 0;\n rep(i, m){\n rep(j, ps[i].size()){\n rep(k, ps[i+1].size()){\n d[i+1][k] = min(d[i+1][k], d[i][j]+ps[i][j].dist(ps[i+1][k]));\n }\n }\n }\n cout<<d[m][0]<<endl;\n }\n}", "accuracy": 1, "time_ms": 310, "memory_kb": 29612, "score_of_the_acc": -1.2573, "final_rank": 19 }, { "submission_id": "aoj_2284_1879615", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <sstream>\n#include <cstring>\n \nusing namespace std;\n \n#define MAX 10000\n#define INF 1e9\ntypedef pair<int,int> P;\nint ans, N;\nvector<P> vec[MAX];\nint dp[100][100]; \n \nint dist(int x1, int y1, int x2, int y2){\n return abs(x1-x2) + abs(y1-y2);\n}\n \nint getNum(string &s){\n stringstream ss(s);\n int res;\n ss >> res;\n return res;\n}\n \nint main(){\n int W, H;\n \n while(cin >> W >> H, (W | H)){\n\tstring in;\n\tint gx, gy, max = 0;\n\tfor(int i = 0 ; i < MAX ; i++){\n\t vec[i].clear();\n\t}\n\tfor(int i = 0 ; i < H ; i++){\n\t for(int j = 0 ; j < W ; j++){\n\t\tcin >> in;\n\t\tif(in == \".\") continue;\n\t\tif(in == \"S\"){\n\t\t vec[0].push_back(P(i,j));\n\t\t}else if(in == \"G\"){\n\t\t gx = i, gy = j;\n\t\t}else{\n\t\t int num = getNum(in);\n\t\t max = std::max(max, num);\n\t\t vec[num].push_back(P(i,j));\n\t\t}\n\t }\n\t}\n\tvec[max+1].push_back(P(gx,gy));\n\tans = INF; N = max+2;\n\tfill(dp[0],dp[H],INF);\n\tfor(int i = 0 ; i < N-1 ; i++){\n\t for(int j = 0 ; j < vec[i+1].size() ; j++){\n\t\tfor(int k = 0 ; k < vec[i].size() ; k++){\n\t\t int bx = vec[i][k].first;\n\t\t int by = vec[i][k].second;\n\t\t int nx = vec[i+1][j].first;\n\t\t int ny = vec[i+1][j].second;\n\t\t if(dp[bx][by] == INF) dp[bx][by] = 0;\n\t\t dp[nx][ny] = min(dp[nx][ny],dp[bx][by] + dist(bx,by,nx,ny));;\n\t\t}\n\t }\n\t}\n\tcout << dp[gx][gy] << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3540, "score_of_the_acc": -0.0433, "final_rank": 7 }, { "submission_id": "aoj_2284_1792061", "code_snippet": "#include<iostream>\n#include<vector>\n#include<string>\n#include<algorithm>\n#include<map>\n#include<set>\n#include<utility>\n#include<cmath>\n#include<cstring>\n#include<queue>\n#include<cstdio>\n#define loop(i,a,b) for(int i=a;i<b;i++) \n#define rep(i,a) loop(i,0,a)\n#define pb push_back\n#define mp make_pair\n#define all(in) in.begin(),in.end()\nconst double PI=acos(-1);\nconst double EPS=1e-10;\nconst int inf=1e9;\nusing namespace std;\ntypedef long long ll;\ntypedef vector<int> vi;\ntypedef vector<vi> vvi;\ntypedef pair<int,int> pii;\nint main(){\n\tint n,m;\n\twhile(cin>>m>>n,n+m){\n\t\tvvi in(n,vi(m));\n\t\tint sx,sy,gx,gy;\n\t\trep(i,n)rep(j,m){\n\t\t\tstring s;\n\t\t\tcin>>s;\n\t\t\tif(s==\".\")in[i][j]=0;\n\t\t\telse if(s==\"G\"){gx=i,gy=j;}\n\t\t\telse if(s==\"S\"){sx=i;sy=j;}\n\t\t\telse in[i][j]=atoi(&s[0]);\n\n\t\t}\n\t\tvector<pii>now;\n\t\tnow.pb(pii(sx*10000+sy,0));\n\t\tint co=1;\n\t\twhile(1){\n\t\t\tvector<pii>next;\n\t\t\tbool h=false;\n\t\t\trep(i,n)rep(j,m)if(in[i][j]==co){\n\t\t\t\th=true;\n\t\t\t\tint mi=inf;\n\t\t\t\trep(k,now.size()){\n\t\t\t\t\tmi=min(mi,\n\t\t\t\t\tnow[k].second+abs(i-now[k].first/10000)+abs(j-now[k].first%10000));\n\t\t\t\t}\n\t\t\t\tnext.pb(pii(i*10000+j,mi));\n\t\t\t}\n\t\t\tif(!h)break;\n\t\t\tco++;\n\t\t\tnow=next;\n\t\t}\n\t\tint mi=inf;\n\t\trep(i,now.size())mi=min(mi,abs(gx-now[i].first/10000)+abs(gy-now[i].first%10000)+now[i].second);\n\t\tcout<<mi<<endl;\n\t}\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 1280, "score_of_the_acc": -0.0476, "final_rank": 10 } ]
aoj_2279_cpp
問題 I 山 問題文 L 氏はある都市の市長である.L 氏の市長としての手腕はすさまじく時を止め一瞬にして都市中に鉄道網を引いたり,財政の赤字を一夜にして莫大な黒字にしたり,はては巨大な怪獣をどこからともなく呼んでパフォーマンスに使うこともした. そんな L 氏は土地の隣り合った行または列同士を何度も入れ替えることででこぼこな土地を山形にするという遊びを最近発明した.土地は H×W 個のブロックに区切られており,ブロック (y,x) では海面からの高さが h y,x であると測定されている.入れ替え後のブロック (y,x) の高さを h' y,x と書くことにする.この土地が山形であるとは,以下の図のように,あるブロック (y^*,x^*) があって,そこを山頂として山頂から離れるに従って高さが単調減少しているものをいう. h' y^*-1, x^*-2 < h' y^*-1, x^*-1 < h' y^*-1, x^* > h' y^*-1, x^*+1 > h' y^*-1, x^*+2 ∧ ∧ ∧ ∧ ∧ h' y^*, x^*-2 < h' y^*, x^*-1 < h' y^*, x^* > h' y^*, x^*+1 > h' y^*, x^*+2 ∨ ∨ ∨ ∨ ∨ h' y^*+1, x^*-2 < h' y^*+1, x^*-1 < h' y^*+1, x^* > h' y^*+1, x^*+1 > h' y^*+1, x^*+2 ところで,L 氏はいくらがんばっても山形にできないような土地が存在する気がした.そこで,前もってコンピュータにその土地が山形にできるかどうかをチェックさせることにした. 入力形式 最初の行に H と W がスペース区切りで与えられる. 次の H 行に土地の高さが与えられる. 各行は W 個の数値からなり,それぞれ対応する場所の高さを表す. 出力形式 土地を山形にできるなら "YES" ,できないなら "NO" を出力せよ. 制約 1 ≤ H,W ≤ 1,000 0 ≤ h y,x ≤ 10 8 全ての h y,x は相異なる. 入出力例 入力例 1 3 3 1 3 2 7 9 8 4 6 5 出力例 1 YES 入力例 2 以下の例では 2 番目の列と 3 番目の列を入れ替えることにより土地を山形にすることができる. 3 3 9 3 6 8 2 5 7 1 4 出力例 2 YES 入力例 3 1 1 100 出力例 3 YES 入力例 4 3 3 1 3 2 4 6 5 7 8 9 出力例 4 NO 謝辞 この問題は京都市の周りに山が多いことから作られた.よって京都市の周りの山に感謝の意を表する.
[ { "submission_id": "aoj_2279_10215449", "code_snippet": "// AOJ #2279\n// Mountain 2025.2.13\n\n#include <bits/stdc++.h>\nusing namespace std;\n\n#define gc() getchar_unlocked()\n#define pc(c) putchar_unlocked(c)\n\nint Cin() { // 整数の入力\n\tint n = 0, c = gc();\n\tif (c == '-') {\tc = gc();\n\t\tdo n = 10*n + (c & 0xf), c = gc(); while (c >= '0');\n\t\treturn -n;\n\t}\n\tdo n = 10*n + (c & 0xf), c = gc(); while (c >= '0');\n\treturn n;\n}\n\nvoid Cout(bool yes) {\n\tif (yes) pc('Y'), pc('E'), pc('S');\n else pc('N'), pc('O');\n pc('\\n');\n}\n \nbool dfsColor(int v, int c, const vector<vector<int>> &g, vector<int>& col) {\n col[v] = c;\n for (int nv : g[v]) {\n if (col[nv] == -1) {\n if (!dfsColor(nv, c^1, g, col)) return false;\n } else if (col[nv] == c) return false;\n }\n return true;\n}\n \nint main(){\n int H = Cin(), W = Cin();\n vector<vector<int>> A(H, vector<int>(W));\n for (int i=0; i<H; i++){\n for (int j=0; j<W; j++) A[i][j] = Cin();\n }\n \n int candCol = -1;\n for (int i=0; i<H; i++){\n int maxVal = -1, maxIdx = -1;\n for (int j=0; j<W; j++){\n if (A[i][j] > maxVal){\n maxVal = A[i][j];\n maxIdx = j;\n }\n }\n if(i==0) candCol = maxIdx;\n else if(maxIdx != candCol){\n Cout(0);\n return 0;\n }\n }\n \n int candRow = -1;\n for (int j=0; j<W; j++){\n int maxVal = -1, maxIdx = -1;\n for (int i=0; i<H; i++){\n if (A[i][j] > maxVal){\n maxVal = A[i][j];\n maxIdx = i;\n }\n }\n if(j==0) candRow = maxIdx;\n else if(maxIdx != candRow){\n Cout(0);;\n return 0;\n }\n }\n \n for (int i=0; i<H; i++){\n for (int j=0; j<W; j++){\n if(i==candRow && j==candCol) continue;\n if(A[i][j] > A[candRow][candCol]){\n Cout(0);\n return 0;\n }\n }\n }\n \n vector<int> rows;\n for (int i=0; i<H; i++){\n if(i==candRow) continue;\n rows.push_back(i);\n }\n vector<int> cols;\n for (int j=0; j<W; j++){\n if(j==candCol) continue;\n cols.push_back(j);\n }\n \n int rn = rows.size();\n vector<vector<int>> graphRows(rn);\n for (int a = 0; a < rn; a++){\n for (int b = a+1; b < rn; b++){\n int i = rows[a], k = rows[b];\n int sign = 0;\n bool comparable = true;\n for (int c : cols){\n int s = (A[i][c] < A[k][c]) ? 1 : -1;\n if(sign==0) sign = s;\n else {\n if(s != sign) { comparable = false; break; }\n }\n }\n if(!comparable){\n graphRows[a].push_back(b);\n graphRows[b].push_back(a);\n }\n }\n }\n vector<int> color(rn, -1);\n for (int i=0; i<rn; i++){\n if(color[i] == -1){\n if(!dfsColor(i, 0, graphRows, color)){\n Cout(0);\n return 0;\n }\n }\n }\n \n int cn = cols.size();\n vector<vector<int>> graphCols(cn);\n for (int a = 0; a < cn; a++){\n for (int b = a+1; b < cn; b++){\n int j = cols[a], k = cols[b];\n int sign = 0;\n bool comparable = true;\n for (int i = 0; i < H; i++){\n if(i==candRow) continue;\n int s = (A[i][j] < A[i][k]) ? 1 : -1;\n if(sign==0) { sign = s; }\n else {\n if(s != sign){ comparable = false; break; }\n }\n }\n if(!comparable){\n graphCols[a].push_back(b);\n graphCols[b].push_back(a);\n }\n }\n }\n vector<int> colorCols(cn, -1);\n for (int i=0; i<cn; i++){\n if(colorCols[i]==-1){\n if(!dfsColor(i, 0, graphCols, colorCols)){\n Cout(0);\n return 0;\n }\n }\n }\n Cout(1);\n return 0;\n}", "accuracy": 1, "time_ms": 1110, "memory_kb": 8924, "score_of_the_acc": -0.7892, "final_rank": 11 }, { "submission_id": "aoj_2279_10215444", "code_snippet": "// AOJ #2279\n// Mountain 2025.2.13\n\n#include <bits/stdc++.h>\nusing namespace std;\n \nbool dfsColor(int v, int c, const vector<vector<int>> &g, vector<int>& col) {\n col[v] = c;\n for (int nv : g[v]) {\n if (col[nv] == -1) {\n if (!dfsColor(nv, c^1, g, col)) return false;\n } else if (col[nv] == c) return false;\n }\n return true;\n}\n \nint main(){\n ios::sync_with_stdio(0);\n cin.tie(0);\n \n int H, W;\n cin >> H >> W;\n vector<vector<int>> A(H, vector<int>(W));\n for (int i=0; i<H; i++){\n for (int j=0; j<W; j++) cin >> A[i][j];\n }\n \n int candCol = -1;\n for (int i=0; i<H; i++){\n int maxVal = -1, maxIdx = -1;\n for (int j=0; j<W; j++){\n if (A[i][j] > maxVal){\n maxVal = A[i][j];\n maxIdx = j;\n }\n }\n if(i==0) candCol = maxIdx;\n else if(maxIdx != candCol){\n cout << \"NO\\n\";\n return 0;\n }\n }\n \n int candRow = -1;\n for (int j=0; j<W; j++){\n int maxVal = -1, maxIdx = -1;\n for (int i=0; i<H; i++){\n if (A[i][j] > maxVal){\n maxVal = A[i][j];\n maxIdx = i;\n }\n }\n if(j==0) candRow = maxIdx;\n else if(maxIdx != candRow){\n cout << \"NO\\n\";\n return 0;\n }\n }\n \n for (int i=0; i<H; i++){\n for (int j=0; j<W; j++){\n if(i==candRow && j==candCol) continue;\n if(A[i][j] > A[candRow][candCol]){\n cout << \"NO\\n\";\n return 0;\n }\n }\n }\n \n vector<int> rows;\n for (int i=0; i<H; i++){\n if(i==candRow) continue;\n rows.push_back(i);\n }\n vector<int> cols;\n for (int j=0; j<W; j++){\n if(j==candCol) continue;\n cols.push_back(j);\n }\n \n int rn = rows.size();\n vector<vector<int>> graphRows(rn);\n for (int a = 0; a < rn; a++){\n for (int b = a+1; b < rn; b++){\n int i = rows[a], k = rows[b];\n int sign = 0;\n bool comparable = true;\n for (int c : cols){\n int s = (A[i][c] < A[k][c]) ? 1 : -1;\n if(sign==0) sign = s;\n else {\n if(s != sign) { comparable = false; break; }\n }\n }\n if(!comparable){\n graphRows[a].push_back(b);\n graphRows[b].push_back(a);\n }\n }\n }\n vector<int> color(rn, -1);\n for (int i=0; i<rn; i++){\n if(color[i] == -1){\n if(!dfsColor(i, 0, graphRows, color)){\n cout << \"NO\\n\";\n return 0;\n }\n }\n }\n \n int cn = cols.size();\n vector<vector<int>> graphCols(cn);\n for (int a = 0; a < cn; a++){\n for (int b = a+1; b < cn; b++){\n int j = cols[a], k = cols[b];\n int sign = 0;\n bool comparable = true;\n for (int i = 0; i < H; i++){\n if(i==candRow) continue;\n int s = (A[i][j] < A[i][k]) ? 1 : -1;\n if(sign==0) { sign = s; }\n else {\n if(s != sign){ comparable = false; break; }\n }\n }\n if(!comparable){\n graphCols[a].push_back(b);\n graphCols[b].push_back(a);\n }\n }\n }\n vector<int> colorCols(cn, -1);\n for (int i=0; i<cn; i++){\n if(colorCols[i]==-1){\n if(!dfsColor(i, 0, graphCols, colorCols)){\n cout << \"NO\\n\";\n return 0;\n }\n }\n }\n cout << \"YES\\n\";\n return 0;\n}", "accuracy": 1, "time_ms": 1170, "memory_kb": 8568, "score_of_the_acc": -0.8258, "final_rank": 13 }, { "submission_id": "aoj_2279_8570852", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nint h, w;\nvector<int> g[4004];\nint match[4004];\nbool used[4004];\nbool dfs(int v){\n used[v] = true;\n for(int i=0;i<g[v].size();i++){\n int u = g[v][i];\n int w = match[u];\n if(w<0 || !used[w]&&dfs(w)){\n match[v] = u;\n match[u] = v;\n return true;\n }\n }\n return false;\n}\nbool bm(){\n int res1 = 0;\n memset(match, -1, sizeof(match));\n for(int v=0;v<h;v++){\n if(match[v]<0){\n memset(used, 0, sizeof(used));\n if(dfs(v)) res1++;\n }\n }\n int res2 = 0;\n for(int v=h+h;v<h+h+w;v++){\n if(match[v]<0){\n memset(used, 0, sizeof(used));\n if(dfs(v)) res2++;\n }\n }\n return res1<=2&&res2<=2;\n}\nint main(){\n cin.tie(0);\n ios::sync_with_stdio(0);\n int mo[1001][1001];\n cin >> h >> w;\n for(int i=0;i<h;i++){\n for(int j=0;j<w;j++) cin >> mo[i][j];\n }\n int hsum[1001]={};\n for(int i=0;i<h;i++){\n for(int j=0;j<i;j++){\n int c1=0,c2=0;\n for(int k=0;k<w;k++){\n if(mo[j][k]>mo[i][k]) c1++;\n else if(mo[j][k]<mo[i][k]) c2++;\n }\n if(c1==w){\n g[j].push_back(i+h);\n hsum[i]++;\n }\n else if(c2==w){\n g[i].push_back(j+h);\n hsum[j]++;\n }\n }\n }\n int wsum[1001]={};\n for(int i=0;i<w;i++){\n for(int j=0;j<i;j++){\n int c1=0,c2=0;\n for(int k=0;k<h;k++){\n if(mo[k][j]>mo[k][i]) c1++;\n else if(mo[k][j]<mo[k][i]) c2++;\n }\n if(c1==w){\n g[j+h+h].push_back(i+h+h+w);\n wsum[i]++;\n }\n else if(c2==w){\n g[i+h+h].push_back(j+h+h+w);\n wsum[j]++;\n }\n }\n }\n bool ok=bm();\n int cnt=0;\n for(int i=0;i<h;i++){\n if(hsum[i]==0) cnt++;\n }\n if(cnt!=1) ok=false;\n cnt=0;\n for(int i=0;i<w;i++){\n if(wsum[i]==0) cnt++;\n }\n if(cnt!=1) ok=false;\n cout << (ok?\"YES\":\"NO\") << endl;\n return(0);\n}", "accuracy": 0.08108108108108109, "time_ms": 90, "memory_kb": 9784, "score_of_the_acc": -0.0379, "final_rank": 20 }, { "submission_id": "aoj_2279_5270279", "code_snippet": "#include <iostream>\n#include <sstream>\n#include <cstdio>\n#include <cstdlib>\n#include <cmath>\n#include <ctime>\n#include <cstring>\n#include <string>\n#include <vector>\n#include <stack>\n#include <queue>\n#include <deque>\n#include <map>\n#include <set>\n#include <bitset>\n#include <numeric>\n#include <utility>\n#include <iomanip>\n#include <algorithm>\n#include <functional>\nusing namespace std;\n\ntypedef long long ll;\ntypedef vector<int> vint;\ntypedef vector<ll> vll;\ntypedef pair<int,int> pint;\n\n#define DE 1\n#define FI first\n#define SE second\n#define PB push_back\n#define MP make_pair\n#define ALL(s) (s).begin(),(s).end()\n#define REP(i,n) for (int i = 0; i < (int)(n); ++i)\n#define EACH(i,s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)\n#define COUT(x) cout<<#x<<\" = \"<<(x)<<\" (L\"<<__LINE__<<\")\"<<endl\n\ntemplate<class T1, class T2> ostream& operator<<(ostream &s, pair<T1,T2> P){return s<<'<'<<P.first<<\", \"<<P.second<<'>';}\ntemplate<class T> ostream& operator<<(ostream &s, vector<T> P) {s<<\"{ \";for(int i=0;i<P.size();++i){if(i>0)s<<\", \";s<<P[i];}return s<<\" }\"<<endl;}\ntemplate<class T1, class T2> ostream& operator<<(ostream &s, map<T1,T2> P) {s<<\"{ \";for(__typeof__(P.begin()) it=P.begin();it!=P.end();++it){if(it!=P.begin())s<<\", \";s<<'<'<<it->first<<\"->\"<<it->second<<'>';}return s<<\" }\"<<endl;}\n\n\n\nconst int MAX = 1010;\n\nstruct Graph {\n static const int MAX_V_ = ::MAX;\n int L;\n vector<int> list[MAX_V_];\n \n void init(int n = 0) {L = n; for (int i = 0; i < MAX_V_; ++i) list[i].clear();}\n inline vector<int>& operator [] (int i) {return list[i];}\n friend ostream& operator << (ostream& s, const Graph& G) {for (int i = 0; i < G.L; ++i) {s << i << \" : \" << G.list[i];}return s;}\n \n void addedge(int from, int to) {\n list[from].push_back(to);\n }\n} G;\n\nint L = 0;\nbool seen[MAX];\nbool matched[MAX];\nint level[MAX];\nint matching[MAX];\n\nvoid hobfs(Graph &G) {\n queue<int> que;\n for (int left = 0; left < L; ++left) {\n level[left] = -1;\n if (!matched[left]) {\n que.push(left);\n level[left] = 0;\n }\n }\n level[L] = L;\n while (!que.empty()) {\n int left = que.front();\n que.pop();\n for (int i = 0; i < G[left].size(); ++i) {\n int right = G[left][i];\n int next = matching[right];\n if (level[next] == -1) {\n level[next] = level[left] + 1;\n que.push(next);\n }\n }\n }\n}\n\nbool hodfs(Graph &G, int left) {\n if (left == L) return true;\n if (seen[left]) return false;\n seen[left] = true;\n for (int i = 0; i < G[left].size(); ++i) {\n int right = G[left][i];\n int next = matching[right];\n if (level[next] > level[left] && hodfs(G, next)) {\n matching[right] = left;\n return true;\n }\n }\n return false;\n}\n\nint Hopcroft_Karp(Graph &G) {\n L = G.L;\n for (int i = 0; i < MAX; ++i) matching[i] = L;\n memset(matched, 0, sizeof(matched));\n \n int res = 0;\n while (true) {\n hobfs(G);\n memset(seen, 0, sizeof(seen));\n bool finished = true;\n for (int left = 0; left < L; ++left) {\n if (!matched[left] && hodfs(G, left)) {\n matched[left] = true;\n ++res;\n finished = false;\n }\n }\n if (finished) break;\n }\n return res;\n}\n\n\nint H, W;\nvint h[MAX];\nint temp[MAX][MAX];\nbool connected[MAX][MAX];\n\nbool solve() {\n bool res = true;\n \n int pMax = -1, Max = -1;\n for (int i = 0; i < H; ++i) if (Max < h[i][0]) {Max = h[i][0], pMax = i;}\n for (int i = 0; i < W; ++i) {\n int epMax = -1, eMax = -1;\n for (int j = 0; j < H; ++j) if (eMax < h[j][i]) {eMax = h[j][i], epMax = j;}\n if (epMax != pMax) {res = false; break;}\n }\n if (res) {\n for (int i = 0; i < H; ++i) {\n for (int j = 0; j < H; ++j) {\n if (!connected[i][j]) {\n bool ok = true;\n for (int k = 0; k < W; ++k) if (h[i][k] <= h[j][k]) {ok = false; break;}\n if (ok) {\n connected[i][j] = true;\n for (int k = 0; k < H; ++k) connected[i][k] |= connected[j][k];\n }\n }\n }\n }\n G.init(H);\n for (int i = 0; i < H; ++i)\n for (int j = 0; j < H; ++j) {\n if (i == j) continue;\n if (connected[i][j]) G.addedge(i,j);\n }\n \n int ans = Hopcroft_Karp(G);\n if (H - ans > 2) res = false;\n }\n\n return res;\n}\n\nint main() { \n cin >> H >> W;\n for (int i = 0; i < H; ++i) h[i].resize(W, 0);\n for (int i = 0; i < H; ++i) for (int j = 0; j < W; ++j) {\n scanf(\"%d\", &h[i][j]);\n temp[j][i] = h[i][j];\n }\n \n bool can = true;\n if (!solve()) can = false;\n \n if (can) {\n for (int i = 0; i < W; ++i) \n for (int j = 0; j < H; ++j) \n temp[i][j] = h[j][i];\n for (int i = 0; i < W; ++i) h[i].resize(H, 0);\n for (int i = 0; i < W; ++i) \n for (int j = 0; j < H; ++j)\n h[i][j] = temp[i][j];\n swap(H, W);\n \n if (!solve()) can = false;\n }\n \n if (can) cout << \"YES\" << endl;\n else cout << \"NO\" << endl;\n\n return 0;\n}", "accuracy": 1, "time_ms": 470, "memory_kb": 16436, "score_of_the_acc": -0.4917, "final_rank": 9 }, { "submission_id": "aoj_2279_5270277", "code_snippet": "#include <iostream>\n#include <sstream>\n#include <cstdio>\n#include <cstdlib>\n#include <cmath>\n#include <ctime>\n#include <cstring>\n#include <string>\n#include <vector>\n#include <stack>\n#include <queue>\n#include <deque>\n#include <map>\n#include <set>\n#include <bitset>\n#include <numeric>\n#include <utility>\n#include <iomanip>\n#include <algorithm>\n#include <functional>\nusing namespace std;\n\ntypedef long long ll;\ntypedef vector<int> vint;\ntypedef vector<ll> vll;\ntypedef pair<int,int> pint;\n\n#define DE 1\n#define FI first\n#define SE second\n#define PB push_back\n#define MP make_pair\n#define ALL(s) (s).begin(),(s).end()\n#define REP(i,n) for (int i = 0; i < (int)(n); ++i)\n#define EACH(i,s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)\n#define COUT(x) cout<<#x<<\" = \"<<(x)<<\" (L\"<<__LINE__<<\")\"<<endl\n\ntemplate<class T1, class T2> ostream& operator<<(ostream &s, pair<T1,T2> P){return s<<'<'<<P.first<<\", \"<<P.second<<'>';}\ntemplate<class T> ostream& operator<<(ostream &s, vector<T> P) {s<<\"{ \";for(int i=0;i<P.size();++i){if(i>0)s<<\", \";s<<P[i];}return s<<\" }\"<<endl;}\ntemplate<class T1, class T2> ostream& operator<<(ostream &s, map<T1,T2> P) {s<<\"{ \";for(__typeof__(P.begin()) it=P.begin();it!=P.end();++it){if(it!=P.begin())s<<\", \";s<<'<'<<it->first<<\"->\"<<it->second<<'>';}return s<<\" }\"<<endl;}\n\n\n\nconst int MAX = 1010;\n\nstruct Graph {\n static const int MAX_V_ = ::MAX;\n int L;\n vector<int> list[MAX_V_];\n \n void init(int n = 0) {L = n; for (int i = 0; i < MAX_V_; ++i) list[i].clear();}\n inline vector<int>& operator [] (int i) {return list[i];}\n friend ostream& operator << (ostream& s, const Graph& G) {for (int i = 0; i < G.L; ++i) {s << i << \" : \" << G.list[i];}return s;}\n \n void addedge(int from, int to) {\n list[from].push_back(to);\n }\n} G;\n\nint L = 0;\nbool seen[MAX];\nbool matched[MAX];\nint level[MAX];\nint matching[MAX];\n\nvoid hobfs(Graph &G) {\n queue<int> que;\n for (int left = 0; left < L; ++left) {\n level[left] = -1;\n if (!matched[left]) {\n que.push(left);\n level[left] = 0;\n }\n }\n level[L] = L;\n while (!que.empty()) {\n int left = que.front();\n que.pop();\n for (int i = 0; i < G[left].size(); ++i) {\n int right = G[left][i];\n int next = matching[right];\n if (level[next] == -1) {\n level[next] = level[left] + 1;\n que.push(next);\n }\n }\n }\n}\n\nbool hodfs(Graph &G, int left) {\n if (left == L) return true;\n if (seen[left]) return false;\n seen[left] = true;\n for (int i = 0; i < G[left].size(); ++i) {\n int right = G[left][i];\n int next = matching[right];\n if (level[next] > level[left] && hodfs(G, next)) {\n matching[right] = left;\n return true;\n }\n }\n return false;\n}\n\nint Hopcroft_Karp(Graph &G) {\n L = G.L;\n for (int i = 0; i < MAX; ++i) matching[i] = L;\n memset(matched, 0, sizeof(matched));\n \n int res = 0;\n while (true) {\n hobfs(G);\n memset(seen, 0, sizeof(seen));\n bool finished = true;\n for (int left = 0; left < L; ++left) {\n if (!matched[left] && hodfs(G, left)) {\n matched[left] = true;\n ++res;\n finished = false;\n }\n }\n if (finished) break;\n }\n return res;\n}\n\n\nint H, W;\nvint h[MAX];\nint temp[MAX][MAX];\nbool connected[MAX][MAX];\n\nbool solve() {\n bool res = true;\n \n int pMax = -1, Max = -1;\n for (int i = 0; i < H; ++i) if (Max < h[i][0]) {Max = h[i][0], pMax = i;}\n for (int i = 0; i < W; ++i) {\n int epMax = -1, eMax = -1;\n for (int j = 0; j < H; ++j) if (eMax < h[j][i]) {eMax = h[j][i], epMax = j;}\n if (epMax != pMax) {res = false; break;}\n }\n if (res) {\n for (int i = 0; i < H; ++i) {\n for (int j = 0; j < H; ++j) {\n if (!connected[i][j]) {\n bool ok = true;\n for (int k = 0; k < W; ++k) if (h[i][k] <= h[j][k]) {ok = false; break;}\n if (ok) {\n connected[i][j] = true;\n connected[j][i] = false;\n for (int k = 0; k < H; ++k) connected[i][k] |= connected[j][k];\n }\n }\n }\n }\n G.init(H);\n for (int i = 0; i < H; ++i)\n for (int j = 0; j < H; ++j) {\n if (i == j) continue;\n if (connected[i][j]) G.addedge(i,j);\n }\n \n int ans = Hopcroft_Karp(G);\n if (H - ans > 2) res = false;\n }\n\n return res;\n}\n\nint main() { \n cin >> H >> W;\n for (int i = 0; i < H; ++i) h[i].resize(W, 0);\n for (int i = 0; i < H; ++i) for (int j = 0; j < W; ++j) {\n scanf(\"%d\", &h[i][j]);\n temp[j][i] = h[i][j];\n }\n \n bool can = true;\n if (!solve()) can = false;\n \n if (can) {\n for (int i = 0; i < W; ++i) \n for (int j = 0; j < H; ++j) \n temp[i][j] = h[j][i];\n for (int i = 0; i < W; ++i) h[i].resize(H, 0);\n for (int i = 0; i < W; ++i) \n for (int j = 0; j < H; ++j)\n h[i][j] = temp[i][j];\n swap(H, W);\n \n if (!solve()) can = false;\n }\n \n if (can) cout << \"YES\" << endl;\n else cout << \"NO\" << endl;\n\n return 0;\n}", "accuracy": 1, "time_ms": 600, "memory_kb": 16308, "score_of_the_acc": -0.587, "final_rank": 10 }, { "submission_id": "aoj_2279_5270208", "code_snippet": "#include <iostream>\n#include <sstream>\n#include <cstdio>\n#include <cstdlib>\n#include <cmath>\n#include <ctime>\n#include <cstring>\n#include <string>\n#include <vector>\n#include <stack>\n#include <queue>\n#include <deque>\n#include <map>\n#include <set>\n#include <bitset>\n#include <numeric>\n#include <utility>\n#include <iomanip>\n#include <algorithm>\n#include <functional>\nusing namespace std;\n\ntypedef long long ll;\ntypedef vector<int> vint;\ntypedef vector<ll> vll;\ntypedef pair<int,int> pint;\n\n#define DE 1\n#define FI first\n#define SE second\n#define PB push_back\n#define MP make_pair\n#define ALL(s) (s).begin(),(s).end()\n#define REP(i,n) for (int i = 0; i < (int)(n); ++i)\n#define EACH(i,s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)\n#define COUT(x) cout<<#x<<\" = \"<<(x)<<\" (L\"<<__LINE__<<\")\"<<endl\n\ntemplate<class T1, class T2> ostream& operator<<(ostream &s, pair<T1,T2> P){return s<<'<'<<P.first<<\", \"<<P.second<<'>';}\ntemplate<class T> ostream& operator<<(ostream &s, vector<T> P) {s<<\"{ \";for(int i=0;i<P.size();++i){if(i>0)s<<\", \";s<<P[i];}return s<<\" }\"<<endl;}\ntemplate<class T1, class T2> ostream& operator<<(ostream &s, map<T1,T2> P) {s<<\"{ \";for(__typeof__(P.begin()) it=P.begin();it!=P.end();++it){if(it!=P.begin())s<<\", \";s<<'<'<<it->first<<\"->\"<<it->second<<'>';}return s<<\" }\"<<endl;}\n\n\n\nconst int MAX = 1010;\n\nstruct Graph {\n static const int MAX_V_ = ::MAX;\n int L;\n vector<int> list[MAX_V_];\n \n void init(int n = 0) {L = n; for (int i = 0; i < MAX_V_; ++i) list[i].clear();}\n inline vector<int>& operator [] (int i) {return list[i];}\n friend ostream& operator << (ostream& s, const Graph& G) {for (int i = 0; i < G.L; ++i) {s << i << \" : \" << G.list[i];}return s;}\n \n void addedge(int from, int to) {\n list[from].push_back(to);\n }\n} G;\n\nint L = 0;\nbool seen[MAX];\nbool matched[MAX];\nint level[MAX];\nint matching[MAX];\n\nvoid hobfs(Graph &G) {\n queue<int> que;\n for (int left = 0; left < L; ++left) {\n level[left] = -1;\n if (!matched[left]) {\n que.push(left);\n level[left] = 0;\n }\n }\n level[L] = L;\n while (!que.empty()) {\n int left = que.front();\n que.pop();\n for (int i = 0; i < G[left].size(); ++i) {\n int right = G[left][i];\n int next = matching[right];\n if (level[next] == -1) {\n level[next] = level[left] + 1;\n que.push(next);\n }\n }\n }\n}\n\nbool hodfs(Graph &G, int left) {\n if (left == L) return true;\n if (seen[left]) return false;\n seen[left] = true;\n for (int i = 0; i < G[left].size(); ++i) {\n int right = G[left][i];\n int next = matching[right];\n if (level[next] > level[left] && hodfs(G, next)) {\n matching[right] = left;\n return true;\n }\n }\n return false;\n}\n\nint Hopcroft_Karp(Graph &G) {\n L = G.L;\n for (int i = 0; i < MAX; ++i) matching[i] = L;\n memset(matched, 0, sizeof(matched));\n \n int res = 0;\n while (true) {\n hobfs(G);\n memset(seen, 0, sizeof(seen));\n bool finished = true;\n for (int left = 0; left < L; ++left) {\n if (!matched[left] && hodfs(G, left)) {\n matched[left] = true;\n ++res;\n finished = false;\n }\n }\n if (finished) break;\n }\n return res;\n}\n\n\nint H, W;\nvint h[MAX];\nint temp[MAX][MAX];\nbool connected[MAX][MAX];\n\nbool solve() {\n bool res = true;\n \n int pMax = -1, Max = -1;\n for (int i = 0; i < H; ++i) if (Max < h[i][0]) {Max = h[i][0], pMax = i;}\n for (int i = 0; i < W; ++i) {\n int epMax = -1, eMax = -1;\n for (int j = 0; j < H; ++j) if (eMax < h[j][i]) {eMax = h[j][i], epMax = j;}\n if (epMax != pMax) {res = false; break;}\n }\n if (res) {\n sort(h, h + H);\n for (int i = 0; i < H; ++i) {\n for (int j = i-1; j >= 0; --j) {\n if (!connected[i][j]) {\n bool ok = true;\n for (int k = 0; k < W; ++k) if (h[i][k] <= h[j][k]) {ok = false; break;}\n if (ok) {\n connected[i][j] = true;\n for (int k = 0; k < j; ++k) connected[i][k] |= connected[j][k];\n }\n }\n }\n }\n G.init(H);\n for (int i = 0; i < H; ++i)\n for (int j = 0; j < H; ++j) {\n if (connected[i][j]) G.addedge(i,j);\n }\n \n int ans = Hopcroft_Karp(G);\n if (H - ans > 2) res = false;\n }\n\n return res;\n}\n\nint main() { \n cin >> H >> W;\n for (int i = 0; i < H; ++i) h[i].resize(W, 0);\n for (int i = 0; i < H; ++i) for (int j = 0; j < W; ++j) {\n scanf(\"%d\", &h[i][j]);\n temp[j][i] = h[i][j];\n }\n \n bool can = true;\n if (!solve()) can = false;\n \n if (can) {\n for (int i = 0; i < W; ++i) \n for (int j = 0; j < H; ++j) \n temp[i][j] = h[j][i];\n for (int i = 0; i < W; ++i) h[i].resize(H, 0);\n for (int i = 0; i < W; ++i) \n for (int j = 0; j < H; ++j)\n h[i][j] = temp[i][j];\n swap(H, W);\n \n if (!solve()) can = false;\n }\n \n if (can) cout << \"YES\" << endl;\n else cout << \"NO\" << endl;\n\n return 0;\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 15020, "score_of_the_acc": -0.1609, "final_rank": 1 }, { "submission_id": "aoj_2279_5270207", "code_snippet": "#include <iostream>\n#include <sstream>\n#include <cstdio>\n#include <cstdlib>\n#include <cmath>\n#include <ctime>\n#include <cstring>\n#include <string>\n#include <vector>\n#include <stack>\n#include <queue>\n#include <deque>\n#include <map>\n#include <set>\n#include <bitset>\n#include <numeric>\n#include <utility>\n#include <iomanip>\n#include <algorithm>\n#include <functional>\nusing namespace std;\n\ntypedef long long ll;\ntypedef vector<int> vint;\ntypedef vector<ll> vll;\ntypedef pair<int,int> pint;\n\n#define DE 1\n#define FI first\n#define SE second\n#define PB push_back\n#define MP make_pair\n#define ALL(s) (s).begin(),(s).end()\n#define REP(i,n) for (int i = 0; i < (int)(n); ++i)\n#define EACH(i,s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)\n#define COUT(x) cout<<#x<<\" = \"<<(x)<<\" (L\"<<__LINE__<<\")\"<<endl\n\ntemplate<class T1, class T2> ostream& operator<<(ostream &s, pair<T1,T2> P){return s<<'<'<<P.first<<\", \"<<P.second<<'>';}\ntemplate<class T> ostream& operator<<(ostream &s, vector<T> P) {s<<\"{ \";for(int i=0;i<P.size();++i){if(i>0)s<<\", \";s<<P[i];}return s<<\" }\"<<endl;}\ntemplate<class T1, class T2> ostream& operator<<(ostream &s, map<T1,T2> P) {s<<\"{ \";for(__typeof__(P.begin()) it=P.begin();it!=P.end();++it){if(it!=P.begin())s<<\", \";s<<'<'<<it->first<<\"->\"<<it->second<<'>';}return s<<\" }\"<<endl;}\n\n\n\nconst int MAX = 1010;\n\nstruct Graph {\n static const int MAX_V_ = ::MAX;\n int L;\n vector<int> list[MAX_V_];\n \n void init(int n = 0) {L = n; for (int i = 0; i < MAX_V_; ++i) list[i].clear();}\n inline vector<int>& operator [] (int i) {return list[i];}\n friend ostream& operator << (ostream& s, const Graph& G) {for (int i = 0; i < G.L; ++i) {s << i << \" : \" << G.list[i];}return s;}\n \n void addedge(int from, int to) {\n list[from].push_back(to);\n }\n} G;\n\nint L = 0;\nbool seen[MAX];\nbool matched[MAX];\nint level[MAX];\nint matching[MAX];\n\nvoid hobfs(Graph &G) {\n queue<int> que;\n for (int left = 0; left < L; ++left) {\n level[left] = -1;\n if (!matched[left]) {\n que.push(left);\n level[left] = 0;\n }\n }\n level[L] = L;\n while (!que.empty()) {\n int left = que.front();\n que.pop();\n for (int i = 0; i < G[left].size(); ++i) {\n int right = G[left][i];\n int next = matching[right];\n if (level[next] == -1) {\n level[next] = level[left] + 1;\n que.push(next);\n }\n }\n }\n}\n\nbool hodfs(Graph &G, int left) {\n if (left == L) return true;\n if (seen[left]) return false;\n seen[left] = true;\n for (int i = 0; i < G[left].size(); ++i) {\n int right = G[left][i];\n int next = matching[right];\n if (level[next] > level[left] && hodfs(G, next)) {\n matching[right] = left;\n return true;\n }\n }\n return false;\n}\n\nint Hopcroft_Karp(Graph &G) {\n L = G.L;\n for (int i = 0; i < MAX; ++i) matching[i] = L;\n memset(matched, 0, sizeof(matched));\n \n int res = 0;\n while (true) {\n hobfs(G);\n memset(seen, 0, sizeof(seen));\n bool finished = true;\n for (int left = 0; left < L; ++left) {\n if (!matched[left] && hodfs(G, left)) {\n matched[left] = true;\n ++res;\n finished = false;\n }\n }\n if (finished) break;\n }\n return res;\n}\n\n\nint H, W;\nvint h[MAX];\nint temp[MAX][MAX];\nbool connected[MAX][MAX];\n\nbool solve() {\n bool res = true;\n \n int pMax = -1, Max = -1;\n for (int i = 0; i < H; ++i) if (Max < h[i][0]) {Max = h[i][0], pMax = i;}\n for (int i = 0; i < W; ++i) {\n int epMax = -1, eMax = -1;\n for (int j = 0; j < H; ++j) if (eMax < h[j][i]) {eMax = h[j][i], epMax = j;}\n if (epMax != pMax) {res = false; break;}\n }\n if (res) {\n sort(h, h + H);\n for (int i = 0; i < H; ++i) {\n for (int j = i-1; j >= 0; --j) {\n if (!connected[i][j]) {\n bool ok = true;\n for (int k = 0; k < W; ++k) if (h[i][k] <= h[j][k]) {ok = false; break;}\n if (ok) {\n connected[i][j] = true;\n for (int k = 0; k < j; ++k) connected[i][k] |= connected[j][k];\n }\n }\n }\n }\n G.init(H);\n for (int i = 0; i < H; ++i)\n for (int j = 0; j < H; ++j) {\n if (i == j) continue;\n if (connected[i][j]) G.addedge(i,j);\n }\n \n int ans = Hopcroft_Karp(G);\n if (H - ans > 2) res = false;\n }\n\n return res;\n}\n\nint main() { \n cin >> H >> W;\n for (int i = 0; i < H; ++i) h[i].resize(W, 0);\n for (int i = 0; i < H; ++i) for (int j = 0; j < W; ++j) {\n scanf(\"%d\", &h[i][j]);\n temp[j][i] = h[i][j];\n }\n \n bool can = true;\n if (!solve()) can = false;\n \n if (can) {\n for (int i = 0; i < W; ++i) \n for (int j = 0; j < H; ++j) \n temp[i][j] = h[j][i];\n for (int i = 0; i < W; ++i) h[i].resize(H, 0);\n for (int i = 0; i < W; ++i) \n for (int j = 0; j < H; ++j)\n h[i][j] = temp[i][j];\n swap(H, W);\n \n if (!solve()) can = false;\n }\n \n if (can) cout << \"YES\" << endl;\n else cout << \"NO\" << endl;\n\n return 0;\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 15104, "score_of_the_acc": -0.163, "final_rank": 2 }, { "submission_id": "aoj_2279_5267756", "code_snippet": "#include <iostream>\n#include <sstream>\n#include <cstdio>\n#include <cstdlib>\n#include <cmath>\n#include <ctime>\n#include <cstring>\n#include <string>\n#include <vector>\n#include <stack>\n#include <queue>\n#include <deque>\n#include <map>\n#include <set>\n#include <bitset>\n#include <numeric>\n#include <utility>\n#include <iomanip>\n#include <algorithm>\n#include <functional>\nusing namespace std;\n\ntypedef long long ll;\ntypedef vector<int> vint;\ntypedef vector<ll> vll;\ntypedef pair<int,int> pint;\n\n#define DE 1\n#define FI first\n#define SE second\n#define PB push_back\n#define MP make_pair\n#define ALL(s) (s).begin(),(s).end()\n#define REP(i,n) for (int i = 0; i < (int)(n); ++i)\n#define EACH(i,s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)\n#define COUT(x) cout<<#x<<\" = \"<<(x)<<\" (L\"<<__LINE__<<\")\"<<endl\n\ntemplate<class T1, class T2> ostream& operator<<(ostream &s, pair<T1,T2> P){return s<<'<'<<P.first<<\", \"<<P.second<<'>';}\ntemplate<class T> ostream& operator<<(ostream &s, vector<T> P) {s<<\"{ \";for(int i=0;i<P.size();++i){if(i>0)s<<\", \";s<<P[i];}return s<<\" }\"<<endl;}\ntemplate<class T1, class T2> ostream& operator<<(ostream &s, map<T1,T2> P) {s<<\"{ \";for(__typeof__(P.begin()) it=P.begin();it!=P.end();++it){if(it!=P.begin())s<<\", \";s<<'<'<<it->first<<\"->\"<<it->second<<'>';}return s<<\" }\"<<endl;}\n\n\n\nconst int MAX = 1010;\n\nstruct Graph {\n static const int MAX_V_ = ::MAX;\n int L;\n vector<int> list[MAX_V_];\n \n void init(int n = 0) {L = n; for (int i = 0; i < MAX_V_; ++i) list[i].clear();}\n inline vector<int>& operator [] (int i) {return list[i];}\n friend ostream& operator << (ostream& s, const Graph& G) {for (int i = 0; i < G.L; ++i) {s << i << \" : \" << G.list[i];}return s;}\n \n void addedge(int from, int to) {\n list[from].push_back(to);\n }\n} G;\n\nint L = 0;\nbool seen[MAX];\nbool matched[MAX];\nint level[MAX];\nint matching[MAX];\n\nvoid hobfs(Graph &G) {\n queue<int> que;\n for (int left = 0; left < L; ++left) {\n level[left] = -1;\n if (!matched[left]) {\n que.push(left);\n level[left] = 0;\n }\n }\n level[L] = L;\n while (!que.empty()) {\n int left = que.front();\n que.pop();\n for (int i = 0; i < G[left].size(); ++i) {\n int right = G[left][i];\n int next = matching[right];\n if (level[next] == -1) {\n level[next] = level[left] + 1;\n que.push(next);\n }\n }\n }\n}\n\nbool hodfs(Graph &G, int left) {\n if (left == L) return true;\n if (seen[left]) return false;\n seen[left] = true;\n for (int i = 0; i < G[left].size(); ++i) {\n int right = G[left][i];\n int next = matching[right];\n if (level[next] > level[left] && hodfs(G, next)) {\n matching[right] = left;\n return true;\n }\n }\n return false;\n}\n\nint Hopcroft_Karp(Graph &G) {\n L = G.L;\n for (int i = 0; i < MAX; ++i) matching[i] = L;\n memset(matched, 0, sizeof(matched));\n \n int res = 0;\n while (true) {\n hobfs(G);\n memset(seen, 0, sizeof(seen));\n bool finished = true;\n for (int left = 0; left < L; ++left) {\n if (!matched[left] && hodfs(G, left)) {\n matched[left] = true;\n ++res;\n finished = false;\n }\n }\n if (finished) break;\n }\n return res;\n}\n\n\nint H, W;\nvint h[MAX];\nint temp[MAX][MAX];\nbool connected[MAX][MAX];\n\nbool solve() {\n bool res = true;\n \n int pMax = -1, Max = -1;\n for (int i = 0; i < H; ++i) if (Max < h[i][0]) {Max = h[i][0], pMax = i;}\n for (int i = 0; i < W; ++i) {\n int epMax = -1, eMax = -1;\n for (int j = 0; j < H; ++j) if (eMax < h[j][i]) {eMax = h[j][i], epMax = j;}\n if (epMax != pMax) {res = false; break;}\n }\n if (res) {\n sort(h, h + H);\n for (int i = 0; i < H ; ++i) {\n for (int j = i-1; j >= 0; --j) {\n if (!connected[i][j]) {\n bool ok = true;\n for (int k = 0; k < W; ++k) if (h[i][k] <= h[j][k]) {ok = false; break;}\n if (ok) {\n connected[i][j] = true;\n for (int k = 0; k < H; ++k) connected[i][k] |= connected[j][k];\n }\n }\n }\n }\n G.init(H);\n for (int i = 0; i < H; ++i)\n for (int j = 0; j < H; ++j) {\n if (i == j) continue;\n if (connected[i][j]) G.addedge(i,j);\n }\n \n int ans = Hopcroft_Karp(G);\n if (H - ans > 2) res = false;\n }\n\n return res;\n}\n\nint main() { \n cin >> H >> W;\n for (int i = 0; i < H; ++i) h[i].resize(W, 0);\n for (int i = 0; i < H; ++i) for (int j = 0; j < W; ++j) {\n scanf(\"%d\", &h[i][j]);\n temp[j][i] = h[i][j];\n }\n \n bool can = true;\n if (!solve()) can = false;\n \n if (can) {\n for (int i = 0; i < W; ++i) \n for (int j = 0; j < H; ++j) \n temp[i][j] = h[j][i];\n for (int i = 0; i < W; ++i) h[i].resize(H, 0);\n for (int i = 0; i < W; ++i) \n for (int j = 0; j < H; ++j)\n h[i][j] = temp[i][j];\n swap(H, W);\n \n if (!solve()) can = false;\n }\n \n if (can) cout << \"YES\" << endl;\n else cout << \"NO\" << endl;\n\n return 0;\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 15124, "score_of_the_acc": -0.1635, "final_rank": 4 }, { "submission_id": "aoj_2279_5267751", "code_snippet": "#include <iostream>\n#include <sstream>\n#include <cstdio>\n#include <cstdlib>\n#include <cmath>\n#include <ctime>\n#include <cstring>\n#include <string>\n#include <vector>\n#include <stack>\n#include <queue>\n#include <deque>\n#include <map>\n#include <set>\n#include <bitset>\n#include <numeric>\n#include <utility>\n#include <iomanip>\n#include <algorithm>\n#include <functional>\nusing namespace std;\n\ntypedef long long ll;\ntypedef vector<int> vint;\ntypedef vector<ll> vll;\ntypedef pair<int,int> pint;\n\n#define DE 1\n#define FI first\n#define SE second\n#define PB push_back\n#define MP make_pair\n#define ALL(s) (s).begin(),(s).end()\n#define REP(i,n) for (int i = 0; i < (int)(n); ++i)\n#define EACH(i,s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)\n#define COUT(x) cout<<#x<<\" = \"<<(x)<<\" (L\"<<__LINE__<<\")\"<<endl\n\ntemplate<class T1, class T2> ostream& operator<<(ostream &s, pair<T1,T2> P){return s<<'<'<<P.first<<\", \"<<P.second<<'>';}\ntemplate<class T> ostream& operator<<(ostream &s, vector<T> P) {s<<\"{ \";for(int i=0;i<P.size();++i){if(i>0)s<<\", \";s<<P[i];}return s<<\" }\"<<endl;}\ntemplate<class T1, class T2> ostream& operator<<(ostream &s, map<T1,T2> P) {s<<\"{ \";for(__typeof__(P.begin()) it=P.begin();it!=P.end();++it){if(it!=P.begin())s<<\", \";s<<'<'<<it->first<<\"->\"<<it->second<<'>';}return s<<\" }\"<<endl;}\n\n\n\nconst int MAX = 1010;\n\nstruct Graph {\n static const int MAX_V_ = ::MAX;\n int L;\n vector<int> list[MAX_V_];\n \n void init(int n = 0) {L = n; for (int i = 0; i < MAX_V_; ++i) list[i].clear();}\n inline vector<int>& operator [] (int i) {return list[i];}\n friend ostream& operator << (ostream& s, const Graph& G) {for (int i = 0; i < G.L; ++i) {s << i << \" : \" << G.list[i];}return s;}\n \n void addedge(int from, int to) {\n list[from].push_back(to);\n }\n} G;\n\nint L = 0;\nbool seen[MAX];\nbool matched[MAX];\nint level[MAX];\nint matching[MAX];\n\nvoid hobfs(Graph &G) {\n queue<int> que;\n for (int left = 0; left < L; ++left) {\n level[left] = -1;\n if (!matched[left]) {\n que.push(left);\n level[left] = 0;\n }\n }\n level[L] = L;\n while (!que.empty()) {\n int left = que.front();\n que.pop();\n for (int i = 0; i < G[left].size(); ++i) {\n int right = G[left][i];\n int next = matching[right];\n if (level[next] == -1) {\n level[next] = level[left] + 1;\n que.push(next);\n }\n }\n }\n}\n\nbool hodfs(Graph &G, int left) {\n if (left == L) return true;\n if (seen[left]) return false;\n seen[left] = true;\n for (int i = 0; i < G[left].size(); ++i) {\n int right = G[left][i];\n int next = matching[right];\n if (level[next] > level[left] && hodfs(G, next)) {\n matching[right] = left;\n return true;\n }\n }\n return false;\n}\n\nint Hopcroft_Karp(Graph &G) {\n L = G.L;\n for (int i = 0; i < MAX; ++i) matching[i] = L;\n memset(matched, 0, sizeof(matched));\n \n int res = 0;\n while (true) {\n hobfs(G);\n memset(seen, 0, sizeof(seen));\n bool finished = true;\n for (int left = 0; left < L; ++left) {\n if (!matched[left] && hodfs(G, left)) {\n matched[left] = true;\n ++res;\n finished = false;\n }\n }\n if (finished) break;\n }\n return res;\n}\n\n\nint H, W;\nvint h[MAX];\nint temp[MAX][MAX];\nbool connected[MAX][MAX];\n\nbool solve() {\n bool res = true;\n \n int pMax = -1, Max = -1;\n for (int i = 0; i < H; ++i) if (Max < h[i][0]) {Max = h[i][0], pMax = i;}\n for (int i = 0; i < W; ++i) {\n int epMax = -1, eMax = -1;\n for (int j = 0; j < H; ++j) if (eMax < h[j][i]) {eMax = h[j][i], epMax = j;}\n if (epMax != pMax) {res = false; break;}\n }\n if (res) {\n sort(h, h + H);\n for (int i = H-1; i >= 0 ; --i) {\n for (int j = 0; j < i; ++j) {\n if (!connected[i][j]) {\n bool ok = true;\n for (int k = 0; k < W; ++k) if (h[i][k] <= h[j][k]) {ok = false; break;}\n if (ok) {\n connected[i][j] = true;\n for (int k = 0; k < H; ++k) connected[i][k] |= connected[j][k];\n }\n }\n }\n }\n G.init(H);\n for (int i = 0; i < H; ++i)\n for (int j = 0; j < H; ++j) {\n if (i == j) continue;\n if (connected[i][j]) G.addedge(i,j);\n }\n \n int ans = Hopcroft_Karp(G);\n if (H - ans > 2) res = false;\n }\n\n return res;\n}\n\nint main() { \n cin >> H >> W;\n for (int i = 0; i < H; ++i) h[i].resize(W, 0);\n for (int i = 0; i < H; ++i) for (int j = 0; j < W; ++j) {\n scanf(\"%d\", &h[i][j]);\n temp[j][i] = h[i][j];\n }\n \n bool can = true;\n if (!solve()) can = false;\n \n if (can) {\n for (int i = 0; i < W; ++i) \n for (int j = 0; j < H; ++j) \n temp[i][j] = h[j][i];\n for (int i = 0; i < W; ++i) h[i].resize(H, 0);\n for (int i = 0; i < W; ++i) \n for (int j = 0; j < H; ++j)\n h[i][j] = temp[i][j];\n swap(H, W);\n \n if (!solve()) can = false;\n }\n \n if (can) cout << \"YES\" << endl;\n else cout << \"NO\" << endl;\n\n return 0;\n}", "accuracy": 1, "time_ms": 480, "memory_kb": 15080, "score_of_the_acc": -0.4654, "final_rank": 8 }, { "submission_id": "aoj_2279_5267735", "code_snippet": "#include <iostream>\n#include <sstream>\n#include <cstdio>\n#include <cstdlib>\n#include <cmath>\n#include <ctime>\n#include <cstring>\n#include <string>\n#include <vector>\n#include <stack>\n#include <queue>\n#include <deque>\n#include <map>\n#include <set>\n#include <bitset>\n#include <numeric>\n#include <utility>\n#include <iomanip>\n#include <algorithm>\n#include <functional>\nusing namespace std;\n\ntypedef long long ll;\ntypedef vector<int> vint;\ntypedef vector<ll> vll;\ntypedef pair<int,int> pint;\n\n#define DE 1\n#define FI first\n#define SE second\n#define PB push_back\n#define MP make_pair\n#define ALL(s) (s).begin(),(s).end()\n#define REP(i,n) for (int i = 0; i < (int)(n); ++i)\n#define EACH(i,s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)\n#define COUT(x) cout<<#x<<\" = \"<<(x)<<\" (L\"<<__LINE__<<\")\"<<endl\n\ntemplate<class T1, class T2> ostream& operator<<(ostream &s, pair<T1,T2> P){return s<<'<'<<P.first<<\", \"<<P.second<<'>';}\ntemplate<class T> ostream& operator<<(ostream &s, vector<T> P) {s<<\"{ \";for(int i=0;i<P.size();++i){if(i>0)s<<\", \";s<<P[i];}return s<<\" }\"<<endl;}\ntemplate<class T1, class T2> ostream& operator<<(ostream &s, map<T1,T2> P) {s<<\"{ \";for(__typeof__(P.begin()) it=P.begin();it!=P.end();++it){if(it!=P.begin())s<<\", \";s<<'<'<<it->first<<\"->\"<<it->second<<'>';}return s<<\" }\"<<endl;}\n\n\n\nconst int MAX = 1010;\n\nstruct Graph {\n static const int MAX_V_ = ::MAX;\n int L;\n vector<int> list[MAX_V_];\n \n void init(int n = 0) {L = n; for (int i = 0; i < MAX_V_; ++i) list[i].clear();}\n inline vector<int>& operator [] (int i) {return list[i];}\n friend ostream& operator << (ostream& s, const Graph& G) {for (int i = 0; i < G.L; ++i) {s << i << \" : \" << G.list[i];}return s;}\n \n void addedge(int from, int to) {\n list[from].push_back(to);\n }\n} G;\n\nint L = 0;\nbool seen[MAX];\nbool matched[MAX];\nint level[MAX];\nint matching[MAX];\n\nvoid hobfs(Graph &G) {\n queue<int> que;\n for (int left = 0; left < L; ++left) {\n level[left] = -1;\n if (!matched[left]) {\n que.push(left);\n level[left] = 0;\n }\n }\n level[L] = L;\n while (!que.empty()) {\n int left = que.front();\n que.pop();\n for (int i = 0; i < G[left].size(); ++i) {\n int right = G[left][i];\n int next = matching[right];\n if (level[next] == -1) {\n level[next] = level[left] + 1;\n que.push(next);\n }\n }\n }\n}\n\nbool hodfs(Graph &G, int left) {\n if (left == L) return true;\n if (seen[left]) return false;\n seen[left] = true;\n for (int i = 0; i < G[left].size(); ++i) {\n int right = G[left][i];\n int next = matching[right];\n if (level[next] > level[left] && hodfs(G, next)) {\n matching[right] = left;\n return true;\n }\n }\n return false;\n}\n\nint Hopcroft_Karp(Graph &G) {\n L = G.L;\n for (int i = 0; i < MAX; ++i) matching[i] = L;\n memset(matched, 0, sizeof(matched));\n \n int res = 0;\n while (true) {\n hobfs(G);\n memset(seen, 0, sizeof(seen));\n bool finished = true;\n for (int left = 0; left < L; ++left) {\n if (!matched[left] && hodfs(G, left)) {\n matched[left] = true;\n ++res;\n finished = false;\n }\n }\n if (finished) break;\n }\n return res;\n}\n\n\nint H, W;\nvint h[MAX];\nint temp[MAX][MAX];\nbool connected[MAX][MAX];\n\nbool solve() {\n bool res = true;\n \n int pMax = -1, Max = -1;\n for (int i = 0; i < H; ++i) if (Max < h[i][0]) {Max = h[i][0], pMax = i;}\n for (int i = 0; i < W; ++i) {\n int epMax = -1, eMax = -1;\n for (int j = 0; j < H; ++j) if (eMax < h[j][i]) {eMax = h[j][i], epMax = j;}\n if (epMax != pMax) {res = false; break;}\n }\n if (res) {\n sort(h, h + H);\n for (int i = 0; i < H ; ++i) {\n for (int j = 0; j < i; ++j) {\n if (!connected[i][j]) {\n bool ok = true;\n for (int k = 0; k < W; ++k) if (h[i][k] <= h[j][k]) {ok = false; break;}\n if (ok) {\n connected[i][j] = true;\n for (int k = 0; k < H; ++k) connected[i][k] |= connected[j][k];\n }\n }\n }\n }\n G.init(H);\n for (int i = 0; i < H; ++i)\n for (int j = 0; j < H; ++j) {\n if (i == j) continue;\n if (connected[i][j]) G.addedge(i,j);\n }\n \n int ans = Hopcroft_Karp(G);\n if (H - ans > 2) res = false;\n }\n\n return res;\n}\n\nint main() { \n cin >> H >> W;\n for (int i = 0; i < H; ++i) h[i].resize(W, 0);\n for (int i = 0; i < H; ++i) for (int j = 0; j < W; ++j) {\n scanf(\"%d\", &h[i][j]);\n temp[j][i] = h[i][j];\n }\n \n bool can = true;\n if (!solve()) can = false;\n \n if (can) {\n for (int i = 0; i < W; ++i) \n for (int j = 0; j < H; ++j) \n temp[i][j] = h[j][i];\n for (int i = 0; i < W; ++i) h[i].resize(H, 0);\n for (int i = 0; i < W; ++i) \n for (int j = 0; j < H; ++j)\n h[i][j] = temp[i][j];\n swap(H, W);\n \n if (!solve()) can = false;\n }\n \n if (can) cout << \"YES\" << endl;\n else cout << \"NO\" << endl;\n\n return 0;\n}", "accuracy": 1, "time_ms": 470, "memory_kb": 15036, "score_of_the_acc": -0.4568, "final_rank": 7 }, { "submission_id": "aoj_2279_5267717", "code_snippet": "#include <iostream>\n#include <sstream>\n#include <cstdio>\n#include <cstdlib>\n#include <cmath>\n#include <ctime>\n#include <cstring>\n#include <string>\n#include <vector>\n#include <stack>\n#include <queue>\n#include <deque>\n#include <map>\n#include <set>\n#include <bitset>\n#include <numeric>\n#include <utility>\n#include <iomanip>\n#include <algorithm>\n#include <functional>\nusing namespace std;\n\ntypedef long long ll;\ntypedef vector<int> vint;\ntypedef vector<ll> vll;\ntypedef pair<int,int> pint;\n\n#define DE 1\n#define FI first\n#define SE second\n#define PB push_back\n#define MP make_pair\n#define ALL(s) (s).begin(),(s).end()\n#define REP(i,n) for (int i = 0; i < (int)(n); ++i)\n#define EACH(i,s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)\n#define COUT(x) cout<<#x<<\" = \"<<(x)<<\" (L\"<<__LINE__<<\")\"<<endl\n\ntemplate<class T1, class T2> ostream& operator<<(ostream &s, pair<T1,T2> P){return s<<'<'<<P.first<<\", \"<<P.second<<'>';}\ntemplate<class T> ostream& operator<<(ostream &s, vector<T> P) {s<<\"{ \";for(int i=0;i<P.size();++i){if(i>0)s<<\", \";s<<P[i];}return s<<\" }\"<<endl;}\ntemplate<class T1, class T2> ostream& operator<<(ostream &s, map<T1,T2> P) {s<<\"{ \";for(__typeof__(P.begin()) it=P.begin();it!=P.end();++it){if(it!=P.begin())s<<\", \";s<<'<'<<it->first<<\"->\"<<it->second<<'>';}return s<<\" }\"<<endl;}\n\n\n\nconst int MAX = 1010;\n\nstruct Graph {\n static const int MAX_V_ = ::MAX;\n int L;\n vector<int> list[MAX_V_];\n \n void init(int n = 0) {L = n; for (int i = 0; i < MAX_V_; ++i) list[i].clear();}\n inline vector<int>& operator [] (int i) {return list[i];}\n friend ostream& operator << (ostream& s, const Graph& G) {for (int i = 0; i < G.L; ++i) {s << i << \" : \" << G.list[i];}return s;}\n \n void addedge(int from, int to) {\n list[from].push_back(to);\n }\n} G;\n\nint L = 0;\nbool seen[MAX];\nbool matched[MAX];\nint level[MAX];\nint matching[MAX];\n\nvoid hobfs(Graph &G) {\n queue<int> que;\n for (int left = 0; left < L; ++left) {\n level[left] = -1;\n if (!matched[left]) {\n que.push(left);\n level[left] = 0;\n }\n }\n level[L] = L;\n while (!que.empty()) {\n int left = que.front();\n que.pop();\n for (int i = 0; i < G[left].size(); ++i) {\n int right = G[left][i];\n int next = matching[right];\n if (level[next] == -1) {\n level[next] = level[left] + 1;\n que.push(next);\n }\n }\n }\n}\n\nbool hodfs(Graph &G, int left) {\n if (left == L) return true;\n if (seen[left]) return false;\n seen[left] = true;\n for (int i = 0; i < G[left].size(); ++i) {\n int right = G[left][i];\n int next = matching[right];\n if (level[next] > level[left] && hodfs(G, next)) {\n matching[right] = left;\n return true;\n }\n }\n return false;\n}\n\nint Hopcroft_Karp(Graph &G) {\n L = G.L;\n for (int i = 0; i < MAX; ++i) matching[i] = L;\n memset(matched, 0, sizeof(matched));\n \n int res = 0;\n while (true) {\n hobfs(G);\n memset(seen, 0, sizeof(seen));\n bool finished = true;\n for (int left = 0; left < L; ++left) {\n if (!matched[left] && hodfs(G, left)) {\n matched[left] = true;\n ++res;\n finished = false;\n }\n }\n if (finished) break;\n }\n return res;\n}\n\n\nint H, W;\nvint h[MAX];\nint temp[MAX][MAX];\nbool connected[MAX][MAX];\n\nbool solve() {\n bool res = true;\n \n int pMax = -1, Max = -1;\n for (int i = 0; i < H; ++i) if (Max < h[i][0]) {Max = h[i][0], pMax = i;}\n for (int i = 0; i < W; ++i) {\n int epMax = -1, eMax = -1;\n for (int j = 0; j < H; ++j) if (eMax < h[j][i]) {eMax = h[j][i], epMax = j;}\n if (epMax != pMax) {res = false; break;}\n }\n if (res) {\n sort(h, h + H);\n for (int i = H-1; i >= 0 ; --i) {\n for (int j = i-1; j >= 0; --j) {\n if (!connected[i][j]) {\n bool ok = true;\n for (int k = 0; k < W; ++k) if (h[i][k] <= h[j][k]) {ok = false; break;}\n if (ok) {\n connected[i][j] = true;\n for (int k = j-1; k >= 0; --k) connected[i][k] |= connected[j][k];\n }\n }\n }\n }\n G.init(H);\n for (int i = 0; i < H; ++i)\n for (int j = 0; j < H; ++j) {\n if (i == j) continue;\n if (connected[i][j]) G.addedge(i,j);\n }\n \n int ans = Hopcroft_Karp(G);\n if (H - ans > 2) res = false;\n }\n\n return res;\n}\n\nint main() { \n cin >> H >> W;\n for (int i = 0; i < H; ++i) h[i].resize(W, 0);\n for (int i = 0; i < H; ++i) for (int j = 0; j < W; ++j) {\n scanf(\"%d\", &h[i][j]);\n temp[j][i] = h[i][j];\n }\n \n bool can = true;\n if (!solve()) can = false;\n \n if (can) {\n for (int i = 0; i < W; ++i) \n for (int j = 0; j < H; ++j) \n temp[i][j] = h[j][i];\n for (int i = 0; i < W; ++i) h[i].resize(H, 0);\n for (int i = 0; i < W; ++i) \n for (int j = 0; j < H; ++j)\n h[i][j] = temp[i][j];\n swap(H, W);\n \n if (!solve()) can = false;\n }\n \n if (can) cout << \"YES\" << endl;\n else cout << \"NO\" << endl;\n\n return 0;\n}", "accuracy": 1, "time_ms": 350, "memory_kb": 15032, "score_of_the_acc": -0.3657, "final_rank": 5 }, { "submission_id": "aoj_2279_5267716", "code_snippet": "#include <iostream>\n#include <sstream>\n#include <cstdio>\n#include <cstdlib>\n#include <cmath>\n#include <ctime>\n#include <cstring>\n#include <string>\n#include <vector>\n#include <stack>\n#include <queue>\n#include <deque>\n#include <map>\n#include <set>\n#include <bitset>\n#include <numeric>\n#include <utility>\n#include <iomanip>\n#include <algorithm>\n#include <functional>\nusing namespace std;\n\ntypedef long long ll;\ntypedef vector<int> vint;\ntypedef vector<ll> vll;\ntypedef pair<int,int> pint;\n\n#define DE 1\n#define FI first\n#define SE second\n#define PB push_back\n#define MP make_pair\n#define ALL(s) (s).begin(),(s).end()\n#define REP(i,n) for (int i = 0; i < (int)(n); ++i)\n#define EACH(i,s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)\n#define COUT(x) cout<<#x<<\" = \"<<(x)<<\" (L\"<<__LINE__<<\")\"<<endl\n\ntemplate<class T1, class T2> ostream& operator<<(ostream &s, pair<T1,T2> P){return s<<'<'<<P.first<<\", \"<<P.second<<'>';}\ntemplate<class T> ostream& operator<<(ostream &s, vector<T> P) {s<<\"{ \";for(int i=0;i<P.size();++i){if(i>0)s<<\", \";s<<P[i];}return s<<\" }\"<<endl;}\ntemplate<class T1, class T2> ostream& operator<<(ostream &s, map<T1,T2> P) {s<<\"{ \";for(__typeof__(P.begin()) it=P.begin();it!=P.end();++it){if(it!=P.begin())s<<\", \";s<<'<'<<it->first<<\"->\"<<it->second<<'>';}return s<<\" }\"<<endl;}\n\n\n\nconst int MAX = 1010;\n\nstruct Graph {\n static const int MAX_V_ = ::MAX;\n int L;\n vector<int> list[MAX_V_];\n \n void init(int n = 0) {L = n; for (int i = 0; i < MAX_V_; ++i) list[i].clear();}\n inline vector<int>& operator [] (int i) {return list[i];}\n friend ostream& operator << (ostream& s, const Graph& G) {for (int i = 0; i < G.L; ++i) {s << i << \" : \" << G.list[i];}return s;}\n \n void addedge(int from, int to) {\n list[from].push_back(to);\n }\n} G;\n\nint L = 0;\nbool seen[MAX];\nbool matched[MAX];\nint level[MAX];\nint matching[MAX];\n\nvoid hobfs(Graph &G) {\n queue<int> que;\n for (int left = 0; left < L; ++left) {\n level[left] = -1;\n if (!matched[left]) {\n que.push(left);\n level[left] = 0;\n }\n }\n level[L] = L;\n while (!que.empty()) {\n int left = que.front();\n que.pop();\n for (int i = 0; i < G[left].size(); ++i) {\n int right = G[left][i];\n int next = matching[right];\n if (level[next] == -1) {\n level[next] = level[left] + 1;\n que.push(next);\n }\n }\n }\n}\n\nbool hodfs(Graph &G, int left) {\n if (left == L) return true;\n if (seen[left]) return false;\n seen[left] = true;\n for (int i = 0; i < G[left].size(); ++i) {\n int right = G[left][i];\n int next = matching[right];\n if (level[next] > level[left] && hodfs(G, next)) {\n matching[right] = left;\n return true;\n }\n }\n return false;\n}\n\nint Hopcroft_Karp(Graph &G) {\n L = G.L;\n for (int i = 0; i < MAX; ++i) matching[i] = L;\n memset(matched, 0, sizeof(matched));\n \n int res = 0;\n while (true) {\n hobfs(G);\n memset(seen, 0, sizeof(seen));\n bool finished = true;\n for (int left = 0; left < L; ++left) {\n if (!matched[left] && hodfs(G, left)) {\n matched[left] = true;\n ++res;\n finished = false;\n }\n }\n if (finished) break;\n }\n return res;\n}\n\n\nint H, W;\nvint h[MAX];\nint temp[MAX][MAX];\nbool connected[MAX][MAX];\n\nbool solve() {\n bool res = true;\n \n int pMax = -1, Max = -1;\n for (int i = 0; i < H; ++i) if (Max < h[i][0]) {Max = h[i][0], pMax = i;}\n for (int i = 0; i < W; ++i) {\n int epMax = -1, eMax = -1;\n for (int j = 0; j < H; ++j) if (eMax < h[j][i]) {eMax = h[j][i], epMax = j;}\n if (epMax != pMax) {res = false; break;}\n }\n if (res) {\n sort(h, h + H);\n for (int i = 0; i < H ; ++i) {\n for (int j = i-1; j >= 0; --j) {\n if (!connected[i][j]) {\n bool ok = true;\n for (int k = 0; k < W; ++k) if (h[i][k] <= h[j][k]) {ok = false; break;}\n if (ok) {\n connected[i][j] = true;\n for (int k = j-1; k >= 0; --k) connected[i][k] |= connected[j][k];\n }\n }\n }\n }\n G.init(H);\n for (int i = 0; i < H; ++i)\n for (int j = 0; j < H; ++j) {\n if (i == j) continue;\n if (connected[i][j]) G.addedge(i,j);\n }\n \n int ans = Hopcroft_Karp(G);\n if (H - ans > 2) res = false;\n }\n\n return res;\n}\n\nint main() { \n cin >> H >> W;\n for (int i = 0; i < H; ++i) h[i].resize(W, 0);\n for (int i = 0; i < H; ++i) for (int j = 0; j < W; ++j) {\n scanf(\"%d\", &h[i][j]);\n temp[j][i] = h[i][j];\n }\n \n bool can = true;\n if (!solve()) can = false;\n \n if (can) {\n for (int i = 0; i < W; ++i) \n for (int j = 0; j < H; ++j) \n temp[i][j] = h[j][i];\n for (int i = 0; i < W; ++i) h[i].resize(H, 0);\n for (int i = 0; i < W; ++i) \n for (int j = 0; j < H; ++j)\n h[i][j] = temp[i][j];\n swap(H, W);\n \n if (!solve()) can = false;\n }\n \n if (can) cout << \"YES\" << endl;\n else cout << \"NO\" << endl;\n\n return 0;\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 15108, "score_of_the_acc": -0.1631, "final_rank": 3 }, { "submission_id": "aoj_2279_5267715", "code_snippet": "#include <iostream>\n#include <sstream>\n#include <cstdio>\n#include <cstdlib>\n#include <cmath>\n#include <ctime>\n#include <cstring>\n#include <string>\n#include <vector>\n#include <stack>\n#include <queue>\n#include <deque>\n#include <map>\n#include <set>\n#include <bitset>\n#include <numeric>\n#include <utility>\n#include <iomanip>\n#include <algorithm>\n#include <functional>\nusing namespace std;\n\ntypedef long long ll;\ntypedef vector<int> vint;\ntypedef vector<ll> vll;\ntypedef pair<int,int> pint;\n\n#define DE 1\n#define FI first\n#define SE second\n#define PB push_back\n#define MP make_pair\n#define ALL(s) (s).begin(),(s).end()\n#define REP(i,n) for (int i = 0; i < (int)(n); ++i)\n#define EACH(i,s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)\n#define COUT(x) cout<<#x<<\" = \"<<(x)<<\" (L\"<<__LINE__<<\")\"<<endl\n\ntemplate<class T1, class T2> ostream& operator<<(ostream &s, pair<T1,T2> P){return s<<'<'<<P.first<<\", \"<<P.second<<'>';}\ntemplate<class T> ostream& operator<<(ostream &s, vector<T> P) {s<<\"{ \";for(int i=0;i<P.size();++i){if(i>0)s<<\", \";s<<P[i];}return s<<\" }\"<<endl;}\ntemplate<class T1, class T2> ostream& operator<<(ostream &s, map<T1,T2> P) {s<<\"{ \";for(__typeof__(P.begin()) it=P.begin();it!=P.end();++it){if(it!=P.begin())s<<\", \";s<<'<'<<it->first<<\"->\"<<it->second<<'>';}return s<<\" }\"<<endl;}\n\n\n\nconst int MAX = 1010;\n\nstruct Graph {\n static const int MAX_V_ = ::MAX;\n int L;\n vector<int> list[MAX_V_];\n \n void init(int n = 0) {L = n; for (int i = 0; i < MAX_V_; ++i) list[i].clear();}\n inline vector<int>& operator [] (int i) {return list[i];}\n friend ostream& operator << (ostream& s, const Graph& G) {for (int i = 0; i < G.L; ++i) {s << i << \" : \" << G.list[i];}return s;}\n \n void addedge(int from, int to) {\n list[from].push_back(to);\n }\n} G;\n\nint L = 0;\nbool seen[MAX];\nbool matched[MAX];\nint level[MAX];\nint matching[MAX];\n\nvoid hobfs(Graph &G) {\n queue<int> que;\n for (int left = 0; left < L; ++left) {\n level[left] = -1;\n if (!matched[left]) {\n que.push(left);\n level[left] = 0;\n }\n }\n level[L] = L;\n while (!que.empty()) {\n int left = que.front();\n que.pop();\n for (int i = 0; i < G[left].size(); ++i) {\n int right = G[left][i];\n int next = matching[right];\n if (level[next] == -1) {\n level[next] = level[left] + 1;\n que.push(next);\n }\n }\n }\n}\n\nbool hodfs(Graph &G, int left) {\n if (left == L) return true;\n if (seen[left]) return false;\n seen[left] = true;\n for (int i = 0; i < G[left].size(); ++i) {\n int right = G[left][i];\n int next = matching[right];\n if (level[next] > level[left] && hodfs(G, next)) {\n matching[right] = left;\n return true;\n }\n }\n return false;\n}\n\nint Hopcroft_Karp(Graph &G) {\n L = G.L;\n for (int i = 0; i < MAX; ++i) matching[i] = L;\n memset(matched, 0, sizeof(matched));\n \n int res = 0;\n while (true) {\n hobfs(G);\n memset(seen, 0, sizeof(seen));\n bool finished = true;\n for (int left = 0; left < L; ++left) {\n if (!matched[left] && hodfs(G, left)) {\n matched[left] = true;\n ++res;\n finished = false;\n }\n }\n if (finished) break;\n }\n return res;\n}\n\n\nint H, W;\nvint h[MAX];\nint temp[MAX][MAX];\nbool connected[MAX][MAX];\n\nbool solve() {\n bool res = true;\n \n int pMax = -1, Max = -1;\n for (int i = 0; i < H; ++i) if (Max < h[i][0]) {Max = h[i][0], pMax = i;}\n for (int i = 0; i < W; ++i) {\n int epMax = -1, eMax = -1;\n for (int j = 0; j < H; ++j) if (eMax < h[j][i]) {eMax = h[j][i], epMax = j;}\n if (epMax != pMax) {res = false; break;}\n }\n if (res) {\n sort(h, h + H);\n for (int i = 0; i < H ; ++i) {\n for (int j = 0; j < i; ++j) {\n if (!connected[i][j]) {\n bool ok = true;\n for (int k = 0; k < W; ++k) if (h[i][k] <= h[j][k]) {ok = false; break;}\n if (ok) {\n connected[i][j] = true;\n for (int k = j-1; k >= 0; --k) connected[i][k] |= connected[j][k];\n }\n }\n }\n }\n G.init(H);\n for (int i = 0; i < H; ++i)\n for (int j = 0; j < H; ++j) {\n if (i == j) continue;\n if (connected[i][j]) G.addedge(i,j);\n }\n \n int ans = Hopcroft_Karp(G);\n if (H - ans > 2) res = false;\n }\n\n return res;\n}\n\nint main() { \n cin >> H >> W;\n for (int i = 0; i < H; ++i) h[i].resize(W, 0);\n for (int i = 0; i < H; ++i) for (int j = 0; j < W; ++j) {\n scanf(\"%d\", &h[i][j]);\n temp[j][i] = h[i][j];\n }\n \n bool can = true;\n if (!solve()) can = false;\n \n if (can) {\n for (int i = 0; i < W; ++i) \n for (int j = 0; j < H; ++j) \n temp[i][j] = h[j][i];\n for (int i = 0; i < W; ++i) h[i].resize(H, 0);\n for (int i = 0; i < W; ++i) \n for (int j = 0; j < H; ++j)\n h[i][j] = temp[i][j];\n swap(H, W);\n \n if (!solve()) can = false;\n }\n \n if (can) cout << \"YES\" << endl;\n else cout << \"NO\" << endl;\n\n return 0;\n}", "accuracy": 1, "time_ms": 360, "memory_kb": 15080, "score_of_the_acc": -0.3745, "final_rank": 6 }, { "submission_id": "aoj_2279_5208213", "code_snippet": "#include <algorithm>\n// #include <atcoder/all>\n#include <cassert>\n#include <chrono>\n#include <cmath>\n#include <complex>\n#include <cstdint>\n#include <fstream>\n#include <functional>\n#include <iomanip>\n#include <iostream>\n#include <iterator>\n#include <limits>\n#include <list>\n#include <map>\n#include <queue>\n#include <random>\n#include <regex>\n#include <set>\n#include <stack>\n#include <string>\n#include <tuple>\n#include <unordered_map>\n#include <unordered_set>\n#include <utility>\n#include <vector>\nusing namespace std;\n// using namespace atcoder;\nusing ll = long long;\nusing ld = long double;\nusing pii = pair<int, int>;\nusing pll = pair<ll, ll>;\nusing vll = vector<ll>;\nusing vvll = vector<vector<ll>>;\nusing vvvll = vector<vector<vector<ll>>>;\nusing vii = vector<int>;\nusing vvii = vector<vector<int>>;\nusing vvvii = vector<vector<vector<int>>>;\nusing vdd = vector<ld>;\nusing vvdd = vector<vector<ld>>;\nusing vvvdd = vector<vector<vector<ld>>>;\nusing vbb = vector<bool>;\nusing vvbb = vector<vector<bool>>;\nusing vvvbb = vector<vector<vector<bool>>>;\nusing vpll = vector<pll>;\nusing vvpll = vector<vector<pll>>;\nusing vvvpll = vector<vector<vector<pll>>>;\n#define pb push_back\n#define mp make_pair\n#define sc second\n#define fr first\n#define endl '\\n'\n#define stpr std::fixed << setprecision\n#define cYES cout << \"YES\" << endl\n#define cNO cout << \"NO\" << endl\n#define cYes cout << \"Yes\" << endl\n#define cNo cout << \"No\" << endl\n#define rep(i, n) for (ll i = 0; i < (n); ++i)\n#define drep(i, a, b, d) for (ll i = (a); i <= (b); i += d)\n#define Rep(i, a, b) for (ll i = (a); i < (b); ++i)\n#define rrep(i, n) for (ll i = n - 1; i >= 0; i--)\n#define drrep(i, a, b, d) for (ll i = (a); i >= (b); i -= d)\n#define rRep(i, a, b) for (ll i = a; i >= b; i--)\n#define crep(i) for (char i = 'a'; i <= 'z'; ++i)\n#define Crep(i) for (char i = 'A'; i <= 'Z'; ++i)\n#define ALL(x) (x).begin(), (x).end()\n#define rALL(x) (x).rbegin(), (x).rend()\n#define sort2(A, N) \\\n sort(A, A + N, \\\n [](const pii &a, const pii &b) { return a.second < b.second; });\n#define debug(v) \\\n cout << #v << \":\"; \\\n for (auto x : v) { \\\n cout << x << ' '; \\\n } \\\n cout << endl;\nint ctoi(const char c) {\n if ('0' <= c && c <= '9') return (c - '0');\n return -1;\n}\nll gcd(ll a, ll b) { return (b == 0 ? a : gcd(b, a % b)); }\nll lcm(ll a, ll b) { return a * b / gcd(a, b); }\nconstexpr ll MOD = 1000000007;\nconstexpr ll INF = 1000000011;\nconstexpr ll MOD2 = 998244353;\nconstexpr ll LINF = 1001002003004005006ll;\nconstexpr ld EPS = 10e-10;\ntemplate <class T, class U>\ninline bool chmax(T &lhs, const U &rhs) {\n if (lhs < rhs) {\n lhs = rhs;\n return 1;\n }\n return 0;\n}\ntemplate <class T, class U>\ninline bool chmin(T &lhs, const U &rhs) {\n if (lhs > rhs) {\n lhs = rhs;\n return 1;\n }\n return 0;\n}\ntemplate <typename T>\nistream &operator>>(istream &is, vector<T> &v) {\n for (auto &&x : v) is >> x;\n return is;\n}\ntemplate <typename T, typename U>\nistream &operator>>(istream &is, pair<T, U> &p) {\n is >> p.first;\n is >> p.second;\n return is;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << p.first << ' ' << p.second;\n return os;\n}\ntemplate <class T>\nostream &operator<<(ostream &os, vector<T> &v) {\n for (auto i = begin(v); i != end(v); ++i) {\n if (i != begin(v)) os << ' ';\n os << *i;\n }\n return os;\n}\n\nnamespace internal {\n\ntemplate <class T>\nstruct simple_queue {\n std::vector<T> payload;\n int pos = 0;\n void reserve(int n) { payload.reserve(n); }\n int size() const { return int(payload.size()) - pos; }\n bool empty() const { return pos == int(payload.size()); }\n void push(const T &t) { payload.push_back(t); }\n T &front() { return payload[pos]; }\n void clear() {\n payload.clear();\n pos = 0;\n }\n void pop() { pos++; }\n};\n\n} // namespace internal\n\ntemplate <class Cap>\nstruct mf_graph {\n public:\n mf_graph() : _n(0) {}\n mf_graph(int n) : _n(n), g(n) {}\n\n int add_edge(int from, int to, Cap cap) {\n assert(0 <= from && from < _n);\n assert(0 <= to && to < _n);\n assert(0 <= cap);\n int m = int(pos.size());\n pos.push_back({from, int(g[from].size())});\n int from_id = int(g[from].size());\n int to_id = int(g[to].size());\n if (from == to) to_id++;\n g[from].push_back(_edge{to, to_id, cap});\n g[to].push_back(_edge{from, from_id, 0});\n return m;\n }\n\n struct edge {\n int from, to;\n Cap cap, flow;\n };\n\n edge get_edge(int i) {\n int m = int(pos.size());\n assert(0 <= i && i < m);\n auto _e = g[pos[i].first][pos[i].second];\n auto _re = g[_e.to][_e.rev];\n return edge{pos[i].first, _e.to, _e.cap + _re.cap, _re.cap};\n }\n std::vector<edge> edges() {\n int m = int(pos.size());\n std::vector<edge> result;\n for (int i = 0; i < m; i++) {\n result.push_back(get_edge(i));\n }\n return result;\n }\n void change_edge(int i, Cap new_cap, Cap new_flow) {\n int m = int(pos.size());\n assert(0 <= i && i < m);\n assert(0 <= new_flow && new_flow <= new_cap);\n auto &_e = g[pos[i].first][pos[i].second];\n auto &_re = g[_e.to][_e.rev];\n _e.cap = new_cap - new_flow;\n _re.cap = new_flow;\n }\n\n Cap flow(int s, int t) { return flow(s, t, std::numeric_limits<Cap>::max()); }\n Cap flow(int s, int t, Cap flow_limit) {\n assert(0 <= s && s < _n);\n assert(0 <= t && t < _n);\n assert(s != t);\n\n std::vector<int> level(_n), iter(_n);\n internal::simple_queue<int> que;\n\n auto bfs = [&]() {\n std::fill(level.begin(), level.end(), -1);\n level[s] = 0;\n que.clear();\n que.push(s);\n while (!que.empty()) {\n int v = que.front();\n que.pop();\n for (auto e : g[v]) {\n if (e.cap == 0 || level[e.to] >= 0) continue;\n level[e.to] = level[v] + 1;\n if (e.to == t) return;\n que.push(e.to);\n }\n }\n };\n auto dfs = [&](auto self, int v, Cap up) {\n if (v == s) return up;\n Cap res = 0;\n int level_v = level[v];\n for (int &i = iter[v]; i < int(g[v].size()); i++) {\n _edge &e = g[v][i];\n if (level_v <= level[e.to] || g[e.to][e.rev].cap == 0) continue;\n Cap d = self(self, e.to, std::min(up - res, g[e.to][e.rev].cap));\n if (d <= 0) continue;\n g[v][i].cap += d;\n g[e.to][e.rev].cap -= d;\n res += d;\n if (res == up) return res;\n }\n level[v] = _n;\n return res;\n };\n\n Cap flow = 0;\n while (flow < flow_limit) {\n bfs();\n if (level[t] == -1) break;\n std::fill(iter.begin(), iter.end(), 0);\n Cap f = dfs(dfs, t, flow_limit - flow);\n if (!f) break;\n flow += f;\n }\n return flow;\n }\n\n std::vector<bool> min_cut(int s) {\n std::vector<bool> visited(_n);\n internal::simple_queue<int> que;\n que.push(s);\n while (!que.empty()) {\n int p = que.front();\n que.pop();\n visited[p] = true;\n for (auto e : g[p]) {\n if (e.cap && !visited[e.to]) {\n visited[e.to] = true;\n que.push(e.to);\n }\n }\n }\n return visited;\n }\n\n private:\n int _n;\n struct _edge {\n int to, rev;\n Cap cap;\n };\n std::vector<std::pair<int, int>> pos;\n std::vector<std::vector<_edge>> g;\n};\n\nint main() {\n ll H, W;\n ll C, D;\n cin >> H >> W;\n vvll A(H, vll(W));\n cin >> A;\n if (1) {\n auto B = A;\n sort(ALL(B));\n vvll E(H);\n rep(i, H) {\n Rep(j, i + 1, H) {\n bool Z = 1;\n rep(k, W) {\n if (B[i][k] > B[j][k]) {\n Z = 0;\n }\n }\n if (Z) {\n E[j].pb(i);\n }\n }\n }\n mf_graph<ll> mf(2 * H + 2);\n rep(i, H) {\n mf.add_edge(2 * H, i, 1);\n mf.add_edge(H + i, 2 * H + 1, 1);\n rep(j, E[i].size()) { mf.add_edge(i, H + E[i][j], 1); }\n }\n if (E[H - 1].size() < H - 1) {\n cNO;\n return 0;\n }\n C = H - mf.flow(2 * H, 2 * H + 1);\n }\n if (1) {\n vvll B(W, vll(H));\n rep(i, H) {\n rep(j, W) { B[j][i] = A[i][j]; }\n }\n swap(H, W);\n sort(ALL(B));\n vvll E(H);\n rep(i, H) {\n Rep(j, i + 1, H) {\n bool Z = 1;\n rep(k, W) {\n if (B[i][k] > B[j][k]) {\n Z = 0;\n }\n }\n if (Z) {\n E[j].pb(i);\n }\n }\n }\n mf_graph<ll> mf(2 * H + 2);\n rep(i, H) {\n mf.add_edge(2 * H, i, 1);\n mf.add_edge(H + i, 2 * H + 1, 1);\n rep(j, E[i].size()) { mf.add_edge(i, H + E[i][j], 1); }\n }\n if (E[H - 1].size() < H - 1) {\n cNO;\n return 0;\n }\n D = H - mf.flow(2 * H, 2 * H + 1);\n }\n if (C <= 2 && D <= 2) {\n cYES;\n } else {\n cNO;\n }\n}", "accuracy": 1, "time_ms": 630, "memory_kb": 48420, "score_of_the_acc": -1.4105, "final_rank": 16 }, { "submission_id": "aoj_2279_5206923", "code_snippet": "#include <algorithm>\n// #include <atcoder/all>\n#include <cassert>\n#include <chrono>\n#include <cmath>\n#include <complex>\n#include <cstdint>\n#include <fstream>\n#include <functional>\n#include <iomanip>\n#include <iostream>\n#include <iterator>\n#include <limits>\n#include <list>\n#include <map>\n#include <queue>\n#include <random>\n#include <regex>\n#include <set>\n#include <stack>\n#include <string>\n#include <tuple>\n#include <unordered_map>\n#include <unordered_set>\n#include <utility>\n#include <vector>\nusing namespace std;\n// using namespace atcoder;\nusing ll = long long;\nusing ld = long double;\nusing pii = pair<int, int>;\nusing pll = pair<ll, ll>;\nusing vll = vector<ll>;\nusing vvll = vector<vector<ll>>;\nusing vvvll = vector<vector<vector<ll>>>;\nusing vii = vector<int>;\nusing vvii = vector<vector<int>>;\nusing vvvii = vector<vector<vector<int>>>;\nusing vdd = vector<ld>;\nusing vvdd = vector<vector<ld>>;\nusing vvvdd = vector<vector<vector<ld>>>;\nusing vbb = vector<bool>;\nusing vvbb = vector<vector<bool>>;\nusing vvvbb = vector<vector<vector<bool>>>;\nusing vpll = vector<pll>;\nusing vvpll = vector<vector<pll>>;\nusing vvvpll = vector<vector<vector<pll>>>;\n#define pb push_back\n#define mp make_pair\n#define sc second\n#define fr first\n#define endl '\\n'\n#define stpr std::fixed << setprecision\n#define cYES cout << \"YES\" << endl\n#define cNO cout << \"NO\" << endl\n#define cYes cout << \"Yes\" << endl\n#define cNo cout << \"No\" << endl\n#define rep(i, n) for (ll i = 0; i < (n); ++i)\n#define drep(i, a, b, d) for (ll i = (a); i <= (b); i += d)\n#define Rep(i, a, b) for (ll i = (a); i < (b); ++i)\n#define rrep(i, n) for (ll i = n - 1; i >= 0; i--)\n#define drrep(i, a, b, d) for (ll i = (a); i >= (b); i -= d)\n#define rRep(i, a, b) for (ll i = a; i >= b; i--)\n#define crep(i) for (char i = 'a'; i <= 'z'; ++i)\n#define Crep(i) for (char i = 'A'; i <= 'Z'; ++i)\n#define ALL(x) (x).begin(), (x).end()\n#define rALL(x) (x).rbegin(), (x).rend()\n#define sort2(A, N) \\\n sort(A, A + N, \\\n [](const pii &a, const pii &b) { return a.second < b.second; });\n#define debug(v) \\\n cout << #v << \":\"; \\\n for (auto x : v) { \\\n cout << x << ' '; \\\n } \\\n cout << endl;\nint ctoi(const char c) {\n if ('0' <= c && c <= '9') return (c - '0');\n return -1;\n}\nll gcd(ll a, ll b) { return (b == 0 ? a : gcd(b, a % b)); }\nll lcm(ll a, ll b) { return a * b / gcd(a, b); }\nconstexpr ll MOD = 1000000007;\nconstexpr ll INF = 1000000011;\nconstexpr ll MOD2 = 998244353;\nconstexpr ll LINF = 1001002003004005006ll;\nconstexpr ld EPS = 10e-10;\ntemplate <class T, class U>\ninline bool chmax(T &lhs, const U &rhs) {\n if (lhs < rhs) {\n lhs = rhs;\n return 1;\n }\n return 0;\n}\ntemplate <class T, class U>\ninline bool chmin(T &lhs, const U &rhs) {\n if (lhs > rhs) {\n lhs = rhs;\n return 1;\n }\n return 0;\n}\ntemplate <typename T>\nistream &operator>>(istream &is, vector<T> &v) {\n for (auto &&x : v) is >> x;\n return is;\n}\ntemplate <typename T, typename U>\nistream &operator>>(istream &is, pair<T, U> &p) {\n is >> p.first;\n is >> p.second;\n return is;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << p.first << ' ' << p.second;\n return os;\n}\ntemplate <class T>\nostream &operator<<(ostream &os, vector<T> &v) {\n for (auto i = begin(v); i != end(v); ++i) {\n if (i != begin(v)) os << ' ';\n os << *i;\n }\n return os;\n}\n\nnamespace internal {\n\ntemplate <class T>\nstruct simple_queue {\n std::vector<T> payload;\n int pos = 0;\n void reserve(int n) { payload.reserve(n); }\n int size() const { return int(payload.size()) - pos; }\n bool empty() const { return pos == int(payload.size()); }\n void push(const T &t) { payload.push_back(t); }\n T &front() { return payload[pos]; }\n void clear() {\n payload.clear();\n pos = 0;\n }\n void pop() { pos++; }\n};\n\n} // namespace internal\n\ntemplate <class Cap>\nstruct mf_graph {\n public:\n mf_graph() : _n(0) {}\n mf_graph(int n) : _n(n), g(n) {}\n\n int add_edge(int from, int to, Cap cap) {\n assert(0 <= from && from < _n);\n assert(0 <= to && to < _n);\n assert(0 <= cap);\n int m = int(pos.size());\n pos.push_back({from, int(g[from].size())});\n int from_id = int(g[from].size());\n int to_id = int(g[to].size());\n if (from == to) to_id++;\n g[from].push_back(_edge{to, to_id, cap});\n g[to].push_back(_edge{from, from_id, 0});\n return m;\n }\n\n struct edge {\n int from, to;\n Cap cap, flow;\n };\n\n edge get_edge(int i) {\n int m = int(pos.size());\n assert(0 <= i && i < m);\n auto _e = g[pos[i].first][pos[i].second];\n auto _re = g[_e.to][_e.rev];\n return edge{pos[i].first, _e.to, _e.cap + _re.cap, _re.cap};\n }\n std::vector<edge> edges() {\n int m = int(pos.size());\n std::vector<edge> result;\n for (int i = 0; i < m; i++) {\n result.push_back(get_edge(i));\n }\n return result;\n }\n void change_edge(int i, Cap new_cap, Cap new_flow) {\n int m = int(pos.size());\n assert(0 <= i && i < m);\n assert(0 <= new_flow && new_flow <= new_cap);\n auto &_e = g[pos[i].first][pos[i].second];\n auto &_re = g[_e.to][_e.rev];\n _e.cap = new_cap - new_flow;\n _re.cap = new_flow;\n }\n\n Cap flow(int s, int t) { return flow(s, t, std::numeric_limits<Cap>::max()); }\n Cap flow(int s, int t, Cap flow_limit) {\n assert(0 <= s && s < _n);\n assert(0 <= t && t < _n);\n assert(s != t);\n\n std::vector<int> level(_n), iter(_n);\n internal::simple_queue<int> que;\n\n auto bfs = [&]() {\n std::fill(level.begin(), level.end(), -1);\n level[s] = 0;\n que.clear();\n que.push(s);\n while (!que.empty()) {\n int v = que.front();\n que.pop();\n for (auto e : g[v]) {\n if (e.cap == 0 || level[e.to] >= 0) continue;\n level[e.to] = level[v] + 1;\n if (e.to == t) return;\n que.push(e.to);\n }\n }\n };\n auto dfs = [&](auto self, int v, Cap up) {\n if (v == s) return up;\n Cap res = 0;\n int level_v = level[v];\n for (int &i = iter[v]; i < int(g[v].size()); i++) {\n _edge &e = g[v][i];\n if (level_v <= level[e.to] || g[e.to][e.rev].cap == 0) continue;\n Cap d = self(self, e.to, std::min(up - res, g[e.to][e.rev].cap));\n if (d <= 0) continue;\n g[v][i].cap += d;\n g[e.to][e.rev].cap -= d;\n res += d;\n if (res == up) return res;\n }\n level[v] = _n;\n return res;\n };\n\n Cap flow = 0;\n while (flow < flow_limit) {\n bfs();\n if (level[t] == -1) break;\n std::fill(iter.begin(), iter.end(), 0);\n Cap f = dfs(dfs, t, flow_limit - flow);\n if (!f) break;\n flow += f;\n }\n return flow;\n }\n\n std::vector<bool> min_cut(int s) {\n std::vector<bool> visited(_n);\n internal::simple_queue<int> que;\n que.push(s);\n while (!que.empty()) {\n int p = que.front();\n que.pop();\n visited[p] = true;\n for (auto e : g[p]) {\n if (e.cap && !visited[e.to]) {\n visited[e.to] = true;\n que.push(e.to);\n }\n }\n }\n return visited;\n }\n\n private:\n int _n;\n struct _edge {\n int to, rev;\n Cap cap;\n };\n std::vector<std::pair<int, int>> pos;\n std::vector<std::vector<_edge>> g;\n};\n\nint main() {\n ll H, W;\n ll C, D;\n cin >> H >> W;\n vvll A(H, vll(W));\n cin >> A;\n if (1) {\n auto B = A;\n sort(ALL(B));\n vvll E(H);\n rep(i, H) {\n Rep(j, i + 1, H) {\n bool Z = 1;\n rep(k, W) {\n if (B[i][k] > B[j][k]) {\n Z = 0;\n }\n }\n if (Z) {\n E[j].pb(i);\n }\n }\n }\n mf_graph<ll> mf(2 * H + 2);\n rep(i, H) {\n mf.add_edge(2 * H, i, INF);\n mf.add_edge(H + i, 2 * H + 1, 1);\n rep(j, E[i].size()) { mf.add_edge(i, H + E[i][j], 1); }\n }\n C = H - mf.flow(2 * H, 2 * H + 1);\n }\n if (1) {\n vvll B(W, vll(H));\n rep(i, H) {\n rep(j, W) { B[j][i] = A[i][j]; }\n }\n swap(H, W);\n sort(ALL(B));\n vvll E(H);\n rep(i, H) {\n Rep(j, i + 1, H) {\n bool Z = 1;\n rep(k, W) {\n if (B[i][k] > B[j][k]) {\n Z = 0;\n }\n }\n if (Z) {\n E[j].pb(i);\n }\n }\n }\n mf_graph<ll> mf(2 * H + 2);\n rep(i, H) {\n mf.add_edge(2 * H, i, INF);\n mf.add_edge(H + i, 2 * H + 1, 1);\n rep(j, E[i].size()) { mf.add_edge(i, H + E[i][j], 1); }\n }\n D = H - mf.flow(2 * H, 2 * H + 1);\n }\n if (C <= 1 && D <= 1) {\n cYES;\n } else {\n cNO;\n }\n}", "accuracy": 0.6621621621621622, "time_ms": 610, "memory_kb": 48668, "score_of_the_acc": -1.4015, "final_rank": 18 }, { "submission_id": "aoj_2279_5206812", "code_snippet": "#include <algorithm>\n// #include <atcoder/all>\n#include <cassert>\n#include <chrono>\n#include <cmath>\n#include <complex>\n#include <cstdint>\n#include <fstream>\n#include <functional>\n#include <iomanip>\n#include <iostream>\n#include <iterator>\n#include <limits>\n#include <list>\n#include <map>\n#include <queue>\n#include <random>\n#include <regex>\n#include <set>\n#include <stack>\n#include <string>\n#include <tuple>\n#include <unordered_map>\n#include <unordered_set>\n#include <utility>\n#include <vector>\nusing namespace std;\n// using namespace atcoder;\nusing ll = long long;\nusing ld = long double;\nusing pii = pair<int, int>;\nusing pll = pair<ll, ll>;\nusing vll = vector<ll>;\nusing vvll = vector<vector<ll>>;\nusing vvvll = vector<vector<vector<ll>>>;\nusing vii = vector<int>;\nusing vvii = vector<vector<int>>;\nusing vvvii = vector<vector<vector<int>>>;\nusing vdd = vector<ld>;\nusing vvdd = vector<vector<ld>>;\nusing vvvdd = vector<vector<vector<ld>>>;\nusing vbb = vector<bool>;\nusing vvbb = vector<vector<bool>>;\nusing vvvbb = vector<vector<vector<bool>>>;\nusing vpll = vector<pll>;\nusing vvpll = vector<vector<pll>>;\nusing vvvpll = vector<vector<vector<pll>>>;\n#define pb push_back\n#define mp make_pair\n#define sc second\n#define fr first\n#define endl '\\n'\n#define stpr std::fixed << setprecision\n#define cYES cout << \"YES\" << endl\n#define cNO cout << \"NO\" << endl\n#define cYes cout << \"Yes\" << endl\n#define cNo cout << \"No\" << endl\n#define rep(i, n) for (ll i = 0; i < (n); ++i)\n#define drep(i, a, b, d) for (ll i = (a); i <= (b); i += d)\n#define Rep(i, a, b) for (ll i = (a); i < (b); ++i)\n#define rrep(i, n) for (ll i = n - 1; i >= 0; i--)\n#define drrep(i, a, b, d) for (ll i = (a); i >= (b); i -= d)\n#define rRep(i, a, b) for (ll i = a; i >= b; i--)\n#define crep(i) for (char i = 'a'; i <= 'z'; ++i)\n#define Crep(i) for (char i = 'A'; i <= 'Z'; ++i)\n#define ALL(x) (x).begin(), (x).end()\n#define rALL(x) (x).rbegin(), (x).rend()\n#define sort2(A, N) \\\n sort(A, A + N, \\\n [](const pii &a, const pii &b) { return a.second < b.second; });\n#define debug(v) \\\n cout << #v << \":\"; \\\n for (auto x : v) { \\\n cout << x << ' '; \\\n } \\\n cout << endl;\nint ctoi(const char c) {\n if ('0' <= c && c <= '9') return (c - '0');\n return -1;\n}\nll gcd(ll a, ll b) { return (b == 0 ? a : gcd(b, a % b)); }\nll lcm(ll a, ll b) { return a * b / gcd(a, b); }\nconstexpr ll MOD = 1000000007;\nconstexpr ll INF = 1000000011;\nconstexpr ll MOD2 = 998244353;\nconstexpr ll LINF = 1001002003004005006ll;\nconstexpr ld EPS = 10e-10;\ntemplate <class T, class U>\ninline bool chmax(T &lhs, const U &rhs) {\n if (lhs < rhs) {\n lhs = rhs;\n return 1;\n }\n return 0;\n}\ntemplate <class T, class U>\ninline bool chmin(T &lhs, const U &rhs) {\n if (lhs > rhs) {\n lhs = rhs;\n return 1;\n }\n return 0;\n}\ntemplate <typename T>\nistream &operator>>(istream &is, vector<T> &v) {\n for (auto &&x : v) is >> x;\n return is;\n}\ntemplate <typename T, typename U>\nistream &operator>>(istream &is, pair<T, U> &p) {\n is >> p.first;\n is >> p.second;\n return is;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << p.first << ' ' << p.second;\n return os;\n}\ntemplate <class T>\nostream &operator<<(ostream &os, vector<T> &v) {\n for (auto i = begin(v); i != end(v); ++i) {\n if (i != begin(v)) os << ' ';\n os << *i;\n }\n return os;\n}\n\nnamespace internal {\n\ntemplate <class T>\nstruct simple_queue {\n std::vector<T> payload;\n int pos = 0;\n void reserve(int n) { payload.reserve(n); }\n int size() const { return int(payload.size()) - pos; }\n bool empty() const { return pos == int(payload.size()); }\n void push(const T &t) { payload.push_back(t); }\n T &front() { return payload[pos]; }\n void clear() {\n payload.clear();\n pos = 0;\n }\n void pop() { pos++; }\n};\n\n} // namespace internal\n\ntemplate <class Cap>\nstruct mf_graph {\n public:\n mf_graph() : _n(0) {}\n mf_graph(int n) : _n(n), g(n) {}\n\n int add_edge(int from, int to, Cap cap) {\n assert(0 <= from && from < _n);\n assert(0 <= to && to < _n);\n assert(0 <= cap);\n int m = int(pos.size());\n pos.push_back({from, int(g[from].size())});\n int from_id = int(g[from].size());\n int to_id = int(g[to].size());\n if (from == to) to_id++;\n g[from].push_back(_edge{to, to_id, cap});\n g[to].push_back(_edge{from, from_id, 0});\n return m;\n }\n\n struct edge {\n int from, to;\n Cap cap, flow;\n };\n\n edge get_edge(int i) {\n int m = int(pos.size());\n assert(0 <= i && i < m);\n auto _e = g[pos[i].first][pos[i].second];\n auto _re = g[_e.to][_e.rev];\n return edge{pos[i].first, _e.to, _e.cap + _re.cap, _re.cap};\n }\n std::vector<edge> edges() {\n int m = int(pos.size());\n std::vector<edge> result;\n for (int i = 0; i < m; i++) {\n result.push_back(get_edge(i));\n }\n return result;\n }\n void change_edge(int i, Cap new_cap, Cap new_flow) {\n int m = int(pos.size());\n assert(0 <= i && i < m);\n assert(0 <= new_flow && new_flow <= new_cap);\n auto &_e = g[pos[i].first][pos[i].second];\n auto &_re = g[_e.to][_e.rev];\n _e.cap = new_cap - new_flow;\n _re.cap = new_flow;\n }\n\n Cap flow(int s, int t) { return flow(s, t, std::numeric_limits<Cap>::max()); }\n Cap flow(int s, int t, Cap flow_limit) {\n assert(0 <= s && s < _n);\n assert(0 <= t && t < _n);\n assert(s != t);\n\n std::vector<int> level(_n), iter(_n);\n internal::simple_queue<int> que;\n\n auto bfs = [&]() {\n std::fill(level.begin(), level.end(), -1);\n level[s] = 0;\n que.clear();\n que.push(s);\n while (!que.empty()) {\n int v = que.front();\n que.pop();\n for (auto e : g[v]) {\n if (e.cap == 0 || level[e.to] >= 0) continue;\n level[e.to] = level[v] + 1;\n if (e.to == t) return;\n que.push(e.to);\n }\n }\n };\n auto dfs = [&](auto self, int v, Cap up) {\n if (v == s) return up;\n Cap res = 0;\n int level_v = level[v];\n for (int &i = iter[v]; i < int(g[v].size()); i++) {\n _edge &e = g[v][i];\n if (level_v <= level[e.to] || g[e.to][e.rev].cap == 0) continue;\n Cap d = self(self, e.to, std::min(up - res, g[e.to][e.rev].cap));\n if (d <= 0) continue;\n g[v][i].cap += d;\n g[e.to][e.rev].cap -= d;\n res += d;\n if (res == up) return res;\n }\n level[v] = _n;\n return res;\n };\n\n Cap flow = 0;\n while (flow < flow_limit) {\n bfs();\n if (level[t] == -1) break;\n std::fill(iter.begin(), iter.end(), 0);\n Cap f = dfs(dfs, t, flow_limit - flow);\n if (!f) break;\n flow += f;\n }\n return flow;\n }\n\n std::vector<bool> min_cut(int s) {\n std::vector<bool> visited(_n);\n internal::simple_queue<int> que;\n que.push(s);\n while (!que.empty()) {\n int p = que.front();\n que.pop();\n visited[p] = true;\n for (auto e : g[p]) {\n if (e.cap && !visited[e.to]) {\n visited[e.to] = true;\n que.push(e.to);\n }\n }\n }\n return visited;\n }\n\n private:\n int _n;\n struct _edge {\n int to, rev;\n Cap cap;\n };\n std::vector<std::pair<int, int>> pos;\n std::vector<std::vector<_edge>> g;\n};\n\nint main() {\n ll H, W;\n ll C, D;\n cin >> H >> W;\n vvll A(H, vll(W));\n cin >> A;\n if (1) {\n auto B = A;\n sort(ALL(B));\n vvll E(H);\n rep(i, H - 1) {\n if (i < H - 2) {\n bool Z = 1;\n rep(j, W) {\n if (B[i][j] > B[i + 1][j]) {\n Z = 0;\n }\n }\n if (Z) {\n E[i + 1].pb(i);\n rep(j, E[i].size()) { E[i + 1].pb(E[i][j]); }\n }\n }\n bool T = 1;\n rep(j, W) {\n if (B[i][j] > B[H - 1][j]) {\n T = 0;\n }\n }\n if (T) {\n E[H - 1].pb(i);\n }\n }\n mf_graph<ll> mf(2 * H + 2);\n rep(i, H) {\n mf.add_edge(2 * H, i, INF);\n mf.add_edge(H + i, 2 * H + 1, 1);\n rep(j, E[i].size()) { mf.add_edge(i, H + E[i][j], 1); }\n }\n C = H - mf.flow(2 * H, 2 * H + 1);\n }\n if (1) {\n vvll B(W, vll(H));\n rep(i, H) {\n rep(j, W) { B[j][i] = A[i][j]; }\n }\n swap(H, W);\n sort(ALL(B));\n vvll E(H);\n rep(i, H - 1) {\n if (i < H - 2) {\n bool Z = 1;\n rep(j, W) {\n if (B[i][j] > B[i + 1][j]) {\n Z = 0;\n }\n }\n if (Z) {\n E[i + 1].pb(i);\n rep(j, E[i].size()) { E[i + 1].pb(E[i][j]); }\n }\n }\n bool T = 1;\n rep(j, W) {\n if (B[i][j] > B[H - 1][j]) {\n T = 0;\n }\n }\n if (T) {\n E[H - 1].pb(i);\n }\n }\n mf_graph<ll> mf(2 * H + 2);\n rep(i, H) {\n mf.add_edge(2 * H, i, INF);\n mf.add_edge(H + i, 2 * H + 1, 1);\n rep(j, E[i].size()) { mf.add_edge(i, H + E[i][j], 1); }\n }\n D = H - mf.flow(2 * H, 2 * H + 1);\n // cout << D << endl;\n }\n if (C <= 1 && D <= 1) {\n cYES;\n } else {\n cNO;\n }\n}", "accuracy": 0.6621621621621622, "time_ms": 230, "memory_kb": 48528, "score_of_the_acc": -1.1101, "final_rank": 17 }, { "submission_id": "aoj_2279_5206492", "code_snippet": "#include <algorithm>\n// #include <atcoder/all>\n#include <cassert>\n#include <chrono>\n#include <cmath>\n#include <complex>\n#include <cstdint>\n#include <fstream>\n#include <functional>\n#include <iomanip>\n#include <iostream>\n#include <iterator>\n#include <limits>\n#include <list>\n#include <map>\n#include <queue>\n#include <random>\n#include <regex>\n#include <set>\n#include <stack>\n#include <string>\n#include <tuple>\n#include <unordered_map>\n#include <unordered_set>\n#include <utility>\n#include <vector>\nusing namespace std;\n// using namespace atcoder;\nusing ll = long long;\nusing ld = long double;\nusing pii = pair<int, int>;\nusing pll = pair<ll, ll>;\nusing vll = vector<ll>;\nusing vvll = vector<vector<ll>>;\nusing vvvll = vector<vector<vector<ll>>>;\nusing vii = vector<int>;\nusing vvii = vector<vector<int>>;\nusing vvvii = vector<vector<vector<int>>>;\nusing vdd = vector<ld>;\nusing vvdd = vector<vector<ld>>;\nusing vvvdd = vector<vector<vector<ld>>>;\nusing vbb = vector<bool>;\nusing vvbb = vector<vector<bool>>;\nusing vvvbb = vector<vector<vector<bool>>>;\nusing vpll = vector<pll>;\nusing vvpll = vector<vector<pll>>;\nusing vvvpll = vector<vector<vector<pll>>>;\n#define pb push_back\n#define mp make_pair\n#define sc second\n#define fr first\n#define endl '\\n'\n#define stpr std::fixed << setprecision\n#define cYES cout << \"YES\" << endl\n#define cNO cout << \"NO\" << endl\n#define cYes cout << \"Yes\" << endl\n#define cNo cout << \"No\" << endl\n#define rep(i, n) for (ll i = 0; i < (n); ++i)\n#define drep(i, a, b, d) for (ll i = (a); i <= (b); i += d)\n#define Rep(i, a, b) for (ll i = (a); i < (b); ++i)\n#define rrep(i, n) for (ll i = n - 1; i >= 0; i--)\n#define drrep(i, a, b, d) for (ll i = (a); i >= (b); i -= d)\n#define rRep(i, a, b) for (ll i = a; i >= b; i--)\n#define crep(i) for (char i = 'a'; i <= 'z'; ++i)\n#define Crep(i) for (char i = 'A'; i <= 'Z'; ++i)\n#define ALL(x) (x).begin(), (x).end()\n#define rALL(x) (x).rbegin(), (x).rend()\n#define sort2(A, N) \\\n sort(A, A + N, \\\n [](const pii &a, const pii &b) { return a.second < b.second; });\n#define debug(v) \\\n cout << #v << \":\"; \\\n for (auto x : v) { \\\n cout << x << ' '; \\\n } \\\n cout << endl;\nint ctoi(const char c) {\n if ('0' <= c && c <= '9') return (c - '0');\n return -1;\n}\nll gcd(ll a, ll b) { return (b == 0 ? a : gcd(b, a % b)); }\nll lcm(ll a, ll b) { return a * b / gcd(a, b); }\nconstexpr ll MOD = 1000000007;\nconstexpr ll INF = 1000000011;\nconstexpr ll MOD2 = 998244353;\nconstexpr ll LINF = 1001002003004005006ll;\nconstexpr ld EPS = 10e-10;\ntemplate <class T, class U>\ninline bool chmax(T &lhs, const U &rhs) {\n if (lhs < rhs) {\n lhs = rhs;\n return 1;\n }\n return 0;\n}\ntemplate <class T, class U>\ninline bool chmin(T &lhs, const U &rhs) {\n if (lhs > rhs) {\n lhs = rhs;\n return 1;\n }\n return 0;\n}\ntemplate <typename T>\nistream &operator>>(istream &is, vector<T> &v) {\n for (auto &&x : v) is >> x;\n return is;\n}\ntemplate <typename T, typename U>\nistream &operator>>(istream &is, pair<T, U> &p) {\n is >> p.first;\n is >> p.second;\n return is;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << p.first << ' ' << p.second;\n return os;\n}\ntemplate <class T>\nostream &operator<<(ostream &os, vector<T> &v) {\n for (auto i = begin(v); i != end(v); ++i) {\n if (i != begin(v)) os << ' ';\n os << *i;\n }\n return os;\n}\n\nnamespace internal {\n\ntemplate <class T>\nstruct simple_queue {\n std::vector<T> payload;\n int pos = 0;\n void reserve(int n) { payload.reserve(n); }\n int size() const { return int(payload.size()) - pos; }\n bool empty() const { return pos == int(payload.size()); }\n void push(const T &t) { payload.push_back(t); }\n T &front() { return payload[pos]; }\n void clear() {\n payload.clear();\n pos = 0;\n }\n void pop() { pos++; }\n};\n\n} // namespace internal\n\ntemplate <class Cap>\nstruct mf_graph {\n public:\n mf_graph() : _n(0) {}\n mf_graph(int n) : _n(n), g(n) {}\n\n int add_edge(int from, int to, Cap cap) {\n assert(0 <= from && from < _n);\n assert(0 <= to && to < _n);\n assert(0 <= cap);\n int m = int(pos.size());\n pos.push_back({from, int(g[from].size())});\n int from_id = int(g[from].size());\n int to_id = int(g[to].size());\n if (from == to) to_id++;\n g[from].push_back(_edge{to, to_id, cap});\n g[to].push_back(_edge{from, from_id, 0});\n return m;\n }\n\n struct edge {\n int from, to;\n Cap cap, flow;\n };\n\n edge get_edge(int i) {\n int m = int(pos.size());\n assert(0 <= i && i < m);\n auto _e = g[pos[i].first][pos[i].second];\n auto _re = g[_e.to][_e.rev];\n return edge{pos[i].first, _e.to, _e.cap + _re.cap, _re.cap};\n }\n std::vector<edge> edges() {\n int m = int(pos.size());\n std::vector<edge> result;\n for (int i = 0; i < m; i++) {\n result.push_back(get_edge(i));\n }\n return result;\n }\n void change_edge(int i, Cap new_cap, Cap new_flow) {\n int m = int(pos.size());\n assert(0 <= i && i < m);\n assert(0 <= new_flow && new_flow <= new_cap);\n auto &_e = g[pos[i].first][pos[i].second];\n auto &_re = g[_e.to][_e.rev];\n _e.cap = new_cap - new_flow;\n _re.cap = new_flow;\n }\n\n Cap flow(int s, int t) { return flow(s, t, std::numeric_limits<Cap>::max()); }\n Cap flow(int s, int t, Cap flow_limit) {\n assert(0 <= s && s < _n);\n assert(0 <= t && t < _n);\n assert(s != t);\n\n std::vector<int> level(_n), iter(_n);\n internal::simple_queue<int> que;\n\n auto bfs = [&]() {\n std::fill(level.begin(), level.end(), -1);\n level[s] = 0;\n que.clear();\n que.push(s);\n while (!que.empty()) {\n int v = que.front();\n que.pop();\n for (auto e : g[v]) {\n if (e.cap == 0 || level[e.to] >= 0) continue;\n level[e.to] = level[v] + 1;\n if (e.to == t) return;\n que.push(e.to);\n }\n }\n };\n auto dfs = [&](auto self, int v, Cap up) {\n if (v == s) return up;\n Cap res = 0;\n int level_v = level[v];\n for (int &i = iter[v]; i < int(g[v].size()); i++) {\n _edge &e = g[v][i];\n if (level_v <= level[e.to] || g[e.to][e.rev].cap == 0) continue;\n Cap d = self(self, e.to, std::min(up - res, g[e.to][e.rev].cap));\n if (d <= 0) continue;\n g[v][i].cap += d;\n g[e.to][e.rev].cap -= d;\n res += d;\n if (res == up) return res;\n }\n level[v] = _n;\n return res;\n };\n\n Cap flow = 0;\n while (flow < flow_limit) {\n bfs();\n if (level[t] == -1) break;\n std::fill(iter.begin(), iter.end(), 0);\n Cap f = dfs(dfs, t, flow_limit - flow);\n if (!f) break;\n flow += f;\n }\n return flow;\n }\n\n std::vector<bool> min_cut(int s) {\n std::vector<bool> visited(_n);\n internal::simple_queue<int> que;\n que.push(s);\n while (!que.empty()) {\n int p = que.front();\n que.pop();\n visited[p] = true;\n for (auto e : g[p]) {\n if (e.cap && !visited[e.to]) {\n visited[e.to] = true;\n que.push(e.to);\n }\n }\n }\n return visited;\n }\n\n private:\n int _n;\n struct _edge {\n int to, rev;\n Cap cap;\n };\n std::vector<std::pair<int, int>> pos;\n std::vector<std::vector<_edge>> g;\n};\n\nint main() {\n ll H, W;\n ll C, D;\n cin >> H >> W;\n vvll A(H, vll(W));\n cin >> A;\n if (1) {\n auto B = A;\n sort(ALL(B));\n vvll E(H);\n rep(i, H - 1) {\n bool Z = 1;\n rep(j, W) {\n if (B[i][j] > B[i + 1][j]) {\n Z = 0;\n }\n }\n if (Z) {\n E[i + 1].pb(i);\n rep(j, E[i].size()) { E[i + 1].pb(E[i][j]); }\n }\n }\n mf_graph<ll> mf(2 * H + 2);\n rep(i, H) {\n mf.add_edge(2 * H, i, 1);\n mf.add_edge(H + i, 2 * H + 1, 1);\n rep(j, E[i].size()) { mf.add_edge(i, H + E[i][j], 1); }\n }\n C = H - mf.flow(2 * H, 2 * H + 1);\n // cout << C << endl;\n }\n if (1) {\n vvll B(W, vll(H));\n rep(i, H) {\n rep(j, W) { B[j][i] = A[i][j]; }\n }\n swap(H, W);\n sort(ALL(B));\n vvll E(H);\n rep(i, H - 1) {\n bool Z = 1;\n rep(j, W) {\n if (B[i][j] > B[i + 1][j]) {\n Z = 0;\n }\n }\n if (Z) {\n E[i + 1].pb(i);\n rep(j, E[i].size()) { E[i + 1].pb(E[i][j]); }\n }\n }\n mf_graph<ll> mf(2 * H + 2);\n rep(i, H) {\n mf.add_edge(2 * H, i, 1);\n mf.add_edge(H + i, 2 * H + 1, 1);\n rep(j, E[i].size()) { mf.add_edge(i, H + E[i][j], 1); }\n }\n D = H - mf.flow(2 * H, 2 * H + 1);\n // cout << D << endl;\n }\n if (C == D) {\n cYES;\n } else {\n cNO;\n }\n}", "accuracy": 0.12162162162162163, "time_ms": 180, "memory_kb": 32656, "score_of_the_acc": -0.6765, "final_rank": 19 }, { "submission_id": "aoj_2279_4762347", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 1000000000000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n#define SIZE 1005\n#define NUM 2010\n\nint H,W;\nint table[2][SIZE][SIZE];\nint in_num[SIZE];\n\nint V;\nvector<int> G[NUM];\nint match[NUM];\nbool used[NUM];\n\nvoid add_edge(int from,int to){\n\tG[from].push_back(to);\n\tG[to].push_back(from);\n}\n\nint dfs(int node_id){\n\tused[node_id] = true;\n\n\tfor(int i = 0; i < G[node_id].size(); i++){\n\t\tint adj_node_id = G[node_id][i],pair_id = match[adj_node_id];\n\t\tif((pair_id < 0)||\n\t\t\t\t(used[pair_id] == false && dfs(pair_id) == true)){\n\n\t\t\tmatch[node_id] = adj_node_id;\n\t\t\tmatch[adj_node_id] = node_id;\n\t\t\treturn true;\n\t\t}\n\t}\n\treturn false;\n}\n\n\nint bipartie_matching(){\n\tint ret = 0;\n\tfor(int i = 0; i < V; i++)match[i] = -1;\n\tfor(int node_id = 0; node_id < V; node_id++){\n\t\tif(match[node_id] < 0){\n\t\t\tfor(int i = 0; i < V; i++)used[i] = false;\n\t\t\tif(dfs(node_id)){\n\t\t\t\tret++;\n\t\t\t}\n\t\t}\n\t}\n\treturn ret;\n}\n\n\nint main(){\n\n\tscanf(\"%d %d\",&H,&W);\n\n\tfor(int row = 0; row < H; row++){\n\t\tfor(int col = 0; col < W; col++){\n\n\t\t\tscanf(\"%d\",&table[0][row][col]);\n\t\t\ttable[1][col][row] = table[0][row][col];\n\t\t}\n\t}\n\n\tbool FLG;\n\n\tfor(int i = 0; i < 2; i++){\n\n\t\tif(H == 1){\n\t\t\tswap(H,W);\n\t\t\tcontinue;\n\t\t}\n\n\t\tV = 2*H;\n\n\t\tfor(int i = 0; i < H; i++){\n\n\t\t\tin_num[i] = 0;\n\t\t}\n\n\t\tfor(int a = 0; a < H; a++){\n\t\t\tfor(int b = 0; b < H; b++){\n\t\t\t\tif(b == a)continue;\n\n\t\t\t\tFLG = true;\n\t\t\t\tfor(int k = 0; k < W; k++){\n\t\t\t\t\tif(table[i][a][k] < table[i][b][k]){\n\n\t\t\t\t\t\tFLG = false;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif(!FLG)continue;\n\n\t\t\t\tadd_edge(a,b+H);\n\t\t\t\tin_num[b]++;\n\t\t\t}\n\t\t}\n\n\t\tint count = 0;\n\t\tfor(int i = 0; i < H; i++){\n\n\t\t\tif(in_num[i] > 0)count++;\n\t\t}\n\t\tif(count != H-1){\n\n\t\t\tprintf(\"NO\\n\");\n\t\t\treturn 0;\n\t\t}\n\n\t\tint num_match = bipartie_matching();\n\n\t\t//printf(\"i:%d num_match:%d\\n\",i,num_match);\n\n\t\tif(H-num_match > 2){\n\n\t\t\tprintf(\"NO\\n\");\n\t\t\treturn 0;\n\t\t}\n\n\t\tfor(int a = 0; a < V; a++){\n\n\t\t\tG[a].clear();\n\t\t}\n\n\t\tswap(H,W);\n\t}\n\n\tprintf(\"YES\\n\");\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 820, "memory_kb": 18240, "score_of_the_acc": -0.8018, "final_rank": 12 }, { "submission_id": "aoj_2279_4762298", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 1000000000000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n#define SIZE 1005\n#define NUM 2010\n\nint H,W;\nint table[2][SIZE][SIZE];\n\n\nint V;\nvector<int> G[NUM];\nint match[NUM];\nbool used[NUM];\n\nvoid add_edge(int from,int to){\n\tG[from].push_back(to);\n\tG[to].push_back(from);\n}\n\nint dfs(int node_id){\n\tused[node_id] = true;\n\n\tfor(int i = 0; i < G[node_id].size(); i++){\n\t\tint adj_node_id = G[node_id][i],pair_id = match[adj_node_id];\n\t\tif((pair_id < 0)||\n\t\t\t\t(used[pair_id] == false && dfs(pair_id) == true)){\n\n\t\t\tmatch[node_id] = adj_node_id;\n\t\t\tmatch[adj_node_id] = node_id;\n\t\t\treturn true;\n\t\t}\n\t}\n\treturn false;\n}\n\n\nint bipartie_matching(){\n\tint ret = 0;\n\tfor(int i = 0; i < V; i++)match[i] = -1;\n\tfor(int node_id = 0; node_id < V; node_id++){\n\t\tif(match[node_id] < 0){\n\t\t\tfor(int i = 0; i < V; i++)used[i] = false;\n\t\t\tif(dfs(node_id)){\n\t\t\t\tret++;\n\t\t\t}\n\t\t}\n\t}\n\treturn ret;\n}\n\n\nint main(){\n\n\tscanf(\"%d %d\",&H,&W);\n\n\tfor(int row = 0; row < H; row++){\n\t\tfor(int col = 0; col < W; col++){\n\n\t\t\tscanf(\"%d\",&table[0][row][col]);\n\t\t\ttable[1][col][row] = table[0][row][col];\n\t\t}\n\t}\n\n\tbool FLG;\n\n\tfor(int i = 0; i < 2; i++){\n\n\t\tif(H == 1){\n\t\t\tswap(H,W);\n\t\t\tcontinue;\n\t\t}\n\n\t\tV = 2*H;\n\n\t\tint maximum = 0;\n\n\t\tfor(int a = 0; a < H; a++){\n\t\t\tfor(int b = 0; b < H; b++){\n\t\t\t\tif(b == a)continue;\n\n\t\t\t\tFLG = true;\n\t\t\t\tfor(int k = 0; k < W; k++){\n\t\t\t\t\tif(table[i][a][k] < table[i][b][k]){\n\n\t\t\t\t\t\tFLG = false;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif(!FLG)continue;\n\n\t\t\t\tadd_edge(a,b+H);\n\t\t\t}\n\t\t\tmaximum = max(maximum,(int)G[a].size());\n\t\t}\n\n\t\tif(maximum < H-1){\n\n\t\t\tprintf(\"NO\\n\");\n\t\t\treturn 0;\n\t\t}\n\n\t\tint num_match = bipartie_matching();\n\n\t\t//printf(\"i:%d num_match:%d\\n\",i,num_match);\n\n\t\tif(H-num_match > 2){\n\n\t\t\tprintf(\"NO\\n\");\n\t\t\treturn 0;\n\t\t}\n\n\t\tfor(int a = 0; a < V; a++){\n\n\t\t\tG[a].clear();\n\t\t}\n\n\t\tswap(H,W);\n\t}\n\n\tprintf(\"YES\\n\");\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 860, "memory_kb": 18088, "score_of_the_acc": -0.8283, "final_rank": 14 }, { "submission_id": "aoj_2279_4501167", "code_snippet": "#include<bits/stdc++.h>\n#define rep(i,n) for (int i = 0; i < (n); ++i)\n#define rrep(i,n) for (int i = (n)-1; i >= 0; i--)\n#define rep2(i,s,n) for (int i = (s); i < (n); ++i)\n#define all(a) a.begin(),a.end()\n#define rall(a) a.rbegin(),a.rend()\n#define pb push_back\n#define eb emplace_back\n#define vi vector<int>\n#define vvi vector<vector<int>>\n#define vl vector<ll>\n#define vvl vector<vector<ll>>\n#define vd vector<double>\n#define vs vector<string>\n#define vc vector<char>\n#define vb vector<bool>\n#define vp vector<P>\nusing namespace std;\nusing ll = long long;\nusing P = pair<int,int>;\nusing LP = pair<ll,ll>;\ntemplate<class T> istream& operator>>(istream& is,vector<T>& v) { for(T& t:v){is>>t;} return is; }\ntemplate<class T> ostream& operator<<(ostream& os,const vector<T>& v) { for(T t:v){os<<t<<\" \";} os<<\"\\n\"; return os; }\nvoid Yes(bool b) { cout << (b ? \"Yes\" : \"No\") << endl; }\nvoid YES(bool b) { cout << (b ? \"YES\" : \"NO\") << endl; }\ntemplate<class T> inline bool chmin(T& a,T b) {if(a > b){a = b; return true;} return false;}\ntemplate<class T> inline bool chmax(T& a,T b) {if(a < b){a = b; return true;} return false;}\nconst int inf = 1001001001;\nconst ll linf = 1001001001001001001;\n\nclass Bipartite_matching {\n int n;\n vvi G;\n vi match;\n vb used;\n bool dfs(int v) {\n used[v] = true;\n for(int u : G[v]) {\n int w = match[u];\n if(w < 0 || !used[w] && dfs(w)) {\n match[v] = u;\n match[u] = v;\n return true;\n }\n }\n return false;\n }\n\npublic:\n Bipartite_matching(int n):n(n),G(n),match(n),used(n) {}\n void add_edge(int v,int u) {\n G[v].pb(u);\n G[u].pb(v);\n }\n int max_matching() {\n int res = 0;\n match.assign(n,-1);\n rep(i,n) {\n if(match[i] >= 0) continue;\n used.assign(n,false);\n if(dfs(i)) res++;\n }\n return res;\n }\n};\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr); cout.tie(nullptr);\n int H,W;\n cin >> H >> W;\n vvi h(H,vi(W));\n cin >> h;\n rep(_,2) {\n Bipartite_matching bp(2*H);\n vb v(H,true);\n rep(i,H) rep(j,H) {\n if(i == j) continue;\n bool ok = true;\n rep(k,W) if(h[i][k] > h[j][k]) ok = false;\n if(ok) bp.add_edge(i,j+H);\n else v[j] = false;\n }\n bool flag = false;\n rep(i,H) if(v[i]) flag = true;\n if(!flag) {\n YES(false);\n return 0;\n }\n if(H-bp.max_matching() > 2) {\n YES(false);\n return 0;\n }\n vvi nh(W,vi(H));\n rep(i,H) rep(j,W) nh[j][i] = h[i][j];\n swap(H,W);\n swap(h,nh);\n }\n YES(true);\n}", "accuracy": 1, "time_ms": 1400, "memory_kb": 16272, "score_of_the_acc": -1.1921, "final_rank": 15 } ]
aoj_2285_cpp
Problem E: アニペロ 猛暑が続いた、長かったようで短かったような夏がもうすぐ終わろうとしていた。 そんな8月下旬のある日、とある2D好きな人物とその先輩のslipは、アニペロサマーライブ、通称アニペロと呼ばれるイベントに参加していた。 アニペロとは、さまざまなアニメソングアーティストたちが集結する、日本国内最大のアニメソングライブイベントである。 今年のアニペロは、公式で公表されていたアーティストの他に、シークレットで超豪華歌手も出演し、大盛況の内に幕を閉じた。 アニペロには初参戦だった2D好きな彼は、ライブ後の余韻にひたりつつも、ひとつの疑問を抱えていた。 「アニペロに出演するアーティストはどのように決めているのだろうか?」 彼は、数あるアーティストの中から、出演アーティストの選出をするのに、次のような方法があるのではないかと、選出法を考えてみた。 まず、アニペロのアーティストには、シークレットアーティストとスタンダードアーティストの2種類の分類があるものとする。 シークレットアーティストとは、ライブに出演することを事前公表せず、ライブ本番になって突然現れるアーティストのことを指す。 スタンダードアーティストとは、ライブに出演することを事前に公表してよいアーティストのことを指す。 全てのアーティストは、次のステータスを持つ。 アーティスト名:文字列 アーティストを雇うための金額(以下、雇用金と呼ぶ):自然数 このアーティストが出演することで、お客をどれほど満足させられるか(以下、満足度と呼ぶ):自然数 今回、ライブに出演するアーティストを選ぶために、シークレットアーティスト候補 N 人、スタンダードアーティスト候補 M 人が、すでに用意されているものとする。さらに、ライブの主催者は、アーティストを雇用するために使用できる資金 LIMIT を持っているものとする。 主催者は、次の条件を満たすように、アーティストを選出しなければならない。 N 人のシークレットアーティスト枠から、1人以上、2人以下を選出する(1人or2人なのは、シークレットがいなかったり多かったりすることを避けるためである) M 人のスタンダードアーティスト枠から、 X 人以上のアーティストを選出する アーティストを全て選出し終えたとき、雇用金の合計を、 LIMIT 以下にする 選出したアーティスト全員で得られる満足度の合計を最大化する さて、ここまで選出法を考えた2D好きな彼は、この方法でプログラムを書いてみようと思った。しかし、彼は選出法を考えるのに気力を使ってしまい、プログラムを書く気力がなくなってしまったようなので、彼の代わりにプログラムを書いてあげてほしい。 あなたの仕事は、上記の選出法に従い、アーティストを選出したときのお客の最大満足度を出力するプログラムを作成することである。 Input 入力は、複数のデータセットからなる。データセットの総数は20以下である。 各データセットは、次の形をしている。 LIMIT N M X SEC_NAME 1 SEC_E 1 SEC_S 1 ... SEC_NAME i SEC_E i SEC_S i ... SEC_NAME N SEC_E N SEC_S N NAME 1 E 1 S 1 ... NAME i E i S i ... NAME M E M S M 整数 LIMIT ( 4 ≤ LIMIT ≤ 1000 )、 N ( 2 ≤ N ≤ 100 )、 M ( 2 ≤ M ≤ 100 )、 X ( 2 ≤ X ≤ M )は、 それぞれアーティストを雇用するのに使用できる資金、シークレットアーティスト候補の数、スタンダードアーティスト候補の数、スタンダードアーティストから選出しなければならない最低人数を表す。 SEC_NAME i 、 NAME i 、はそれぞれシークレットアーティスト候補、スタンダードアーティスト候補の名前を指す30文字以下の文字列である。 文字列には、アルファベット('A'-'Z', 'a'-'z')のみが使用される。 同じアーティスト名が2回以上出現することはない。 整数 SEC_E i 、 E i ( 1 ≤ SEC_E i , E i ≤ 10 )、 SEC_S i 、 S i ( 1 ≤ SEC_S i , S i ≤ 10 ) は、それぞれシークレットアーティスト候補の雇用金、スタンダードアーティスト候補の雇用金、シークレットアーティスト候補が出演したときに得られる満足度、スタンダードアーティスト候補が出演したときに得られる満足度を表す。 入力の終了は、0が4つの行で示される。このデータについては処理する必要はない。 Output 選出法を適用してアーティストを選出したとき、お客の満足度の最大値を出力せよ。 アーティストの選出の仕方は、必ずあると仮定してよい。 Sample Input 100 2 2 2 A 5 6 B 7 8 C 1 2 D 3 4 27 2 3 3 A 8 10 B 8 10 C 6 7 D 5 4 E 8 9 27 2 3 2 A 8 10 B 8 10 C 6 7 D 5 4 E 8 9 44 3 6 5 YamatoNoHito 9 10 ZettoNoHito 9 10 TMR 10 10 SkillNoGroup 8 10 NanaSama 6 9 FRPSD 6 8 Magi3rdDeshi 5 7 Magi13thDeshi 2 2 MagicalItoh 4 6 0 0 0 0 Output for Sample Input 20 30 31 55
[ { "submission_id": "aoj_2285_9658651", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define N 110\n\nint limit, n, m, x;\nint e1[N], s1[N];\nint e2[N], s2[N];\nint dp[1010][N][N];\nint ep[1010];\n\nint main(){\n\twhile (1){\n\t\tcin >> limit >> n >> m >> x;\n\t\tif (limit == 0 && n == 0 && m == 0 && x == 0) break;\n\t\tstring name;\n\t\tfor (int i = 1; i <= n; i++) cin >> name >> e1[i] >> s1[i];\n\t\tfor (int i = 1; i <= m; i++) cin >> name >> e2[i] >> s2[i];\n\t\tfor (int i = 0; i <= 1000; i++){\n\t\t\tfor (int j = 0; j <= m; j++){\n\t\t\t\tfor (int k = 0; k <= m; k++) dp[i][j][k] = -1e9;\n\t\t\t}\n\t\t}\n\t\tfor (int i = 0; i <= 1000; i++) {\n\t\t\tfor (int j = 0; j <= m; j++) dp[i][j][0] = 0;\n\t\t}\n\t\tfor (int i = 1; i <= 1000; i++){\n\t\t\tfor (int j = 1; j <= m; j++){\n\t\t\t\tfor (int k = 1; k <= j; k++){\n\t\t\t\t\tdp[i][j][k] = dp[i][j - 1][k];\n\t\t\t\t\tif (i >= e2[j]) dp[i][j][k] = max(dp[i][j][k], dp[i - e2[j]][j - 1][k - 1] + s2[j]);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tfor (int i = 1; i <= 1000; i++){\n\t\t\tep[i] = 0;\n\t\t\tfor (int k = x; k <= m; k++) ep[i] = max(ep[i], dp[i][m][k]);\n\t\t}\n\t\tint ans = 0;\n\t\tfor (int i = 1; i <= n; i++) {\n\t\t\tif (limit >= e1[i]) ans = max(ans, s1[i] + ep[limit - e1[i]]);\n\t\t}\n\t\tfor (int i = 1; i <= n; i++){\n\t\t\tfor (int j = i + 1; j <= n; j++){\n\t\t\t\tif (limit >= e1[i] + e1[j]) ans = max(ans, s1[i] + s1[j] + ep[limit - e1[i] - e1[j]]);\n\t\t\t}\n\t\t}\n\t\tcout << ans << endl;\n\t}\n\t\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 50680, "score_of_the_acc": -1.0699, "final_rank": 17 }, { "submission_id": "aoj_2285_5033123", "code_snippet": "#line 2 \"/home/yuruhiya/programming/library/template/template.cpp\"\n#include <bits/stdc++.h>\n#line 4 \"/home/yuruhiya/programming/library/template/constants.cpp\"\n#include <string_view>\n#line 7 \"/home/yuruhiya/programming/library/template/constants.cpp\"\n\n#define rep(i, n) for (int i = 0; i < (n); ++i)\n#define FOR(i, m, n) for (int i = (m); i < (n); ++i)\n#define rrep(i, n) for (int i = (n)-1; i >= 0; --i)\n#define rfor(i, m, n) for (int i = (m); i >= (n); --i)\n#define unless(c) if (!(c))\n#define all(x) (x).begin(), (x).end()\n#define rall(x) (x).rbegin(), (x).rend()\n#define range_it(a, l, r) (a).begin() + (l), (a).begin() + (r)\n\nusing namespace std;\nusing ll = long long;\nusing LD = long double;\nusing VB = vector<bool>;\nusing VVB = vector<VB>;\nusing VI = vector<int>;\nusing VVI = vector<VI>;\nusing VL = vector<ll>;\nusing VVL = vector<VL>;\nusing VS = vector<string>;\nusing VD = vector<LD>;\nusing PII = pair<int, int>;\nusing VP = vector<PII>;\nusing PLL = pair<ll, ll>;\nusing VPL = vector<PLL>;\ntemplate <class T> using PQ = priority_queue<T>;\ntemplate <class T> using PQS = priority_queue<T, vector<T>, greater<T>>;\nconstexpr int inf = 1000000000;\nconstexpr long long inf_ll = 1000000000000000000ll, MOD = 1000000007;\nconstexpr long double PI = 3.14159265358979323846, EPS = 1e-12;\nnamespace CharacterClass {\n\tconstexpr string_view\n\t digit = \"0123456789\",\n\t xdigit = \"0123456789ABCDEFabcdef\", lower = \"abcdefghijklmnopqrstuvwxyz\",\n\t upper = \"ABCDEFGHIJKLMNOPQRSTUVWXYZ\",\n\t alpha = \"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz\",\n\t alnum = \"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz\",\n\t word = \"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz\",\n\t punct = R\"(!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~)\",\n\t graph =\n\t R\"(!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~)\",\n\t print =\n\t R\"( !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~)\",\n\t blank = \" \\t\", space = \" \\t\\n\\r\\f\\v\";\n} // namespace CharacterClass\n#line 7 \"/home/yuruhiya/programming/library/template/Input.cpp\"\nusing namespace std;\n\n#ifdef _WIN32\n#define getchar_unlocked _getchar_nolock\n#define putchar_unlocked _putchar_nolock\n#define fwrite_unlocked fwrite\n#define fflush_unlocked fflush\n#endif\nclass Scanner {\n\tstatic int gc() {\n\t\treturn getchar_unlocked();\n\t}\n\tstatic char next_char() {\n\t\tchar c;\n\t\tread(c);\n\t\treturn c;\n\t}\n\ttemplate <class T> static void read(T& v) {\n\t\tcin >> v;\n\t}\n\tstatic void read(char& v) {\n\t\twhile (isspace(v = gc()))\n\t\t\t;\n\t}\n\tstatic void read(bool& v) {\n\t\tv = next_char() != '0';\n\t}\n\tstatic void read(string& v) {\n\t\tv.clear();\n\t\tfor (char c = next_char(); !isspace(c); c = gc()) v += c;\n\t}\n\tstatic void read(int& v) {\n\t\tv = 0;\n\t\tbool neg = false;\n\t\tchar c = next_char();\n\t\tif (c == '-') {\n\t\t\tneg = true;\n\t\t\tc = gc();\n\t\t}\n\t\tfor (; isdigit(c); c = gc()) v = v * 10 + (c - '0');\n\t\tif (neg) v = -v;\n\t}\n\tstatic void read(long long& v) {\n\t\tv = 0;\n\t\tbool neg = false;\n\t\tchar c = next_char();\n\t\tif (c == '-') {\n\t\t\tneg = true;\n\t\t\tc = gc();\n\t\t}\n\t\tfor (; isdigit(c); c = gc()) v = v * 10 + (c - '0');\n\t\tif (neg) v = -v;\n\t}\n\tstatic void read(double& v) {\n\t\tv = 0;\n\t\tdouble dp = 1;\n\t\tbool neg = false, after_dp = false;\n\t\tchar c = next_char();\n\t\tif (c == '-') {\n\t\t\tneg = true;\n\t\t\tc = gc();\n\t\t}\n\t\tfor (; isdigit(c) || c == '.'; c = gc()) {\n\t\t\tif (c == '.') {\n\t\t\t\tafter_dp = true;\n\t\t\t} else if (after_dp) {\n\t\t\t\tv += (c - '0') * (dp *= 0.1);\n\t\t\t} else {\n\t\t\t\tv = v * 10 + (c - '0');\n\t\t\t}\n\t\t}\n\t\tif (neg) v = -v;\n\t}\n\tstatic void read(long double& v) {\n\t\tv = 0;\n\t\tlong double dp = 1;\n\t\tbool neg = false, after_dp = false;\n\t\tchar c = next_char();\n\t\tif (c == '-') {\n\t\t\tneg = true;\n\t\t\tc = gc();\n\t\t}\n\t\tfor (; isdigit(c) || c == '.'; c = gc()) {\n\t\t\tif (c == '.') {\n\t\t\t\tafter_dp = true;\n\t\t\t} else if (after_dp) {\n\t\t\t\tv += (c - '0') * (dp *= 0.1);\n\t\t\t} else {\n\t\t\t\tv = v * 10 + (c - '0');\n\t\t\t}\n\t\t}\n\t\tif (neg) v = -v;\n\t}\n\ttemplate <class T, class U> static void read(pair<T, U>& v) {\n\t\tread(v.first);\n\t\tread(v.second);\n\t}\n\ttemplate <class T> static void read(vector<T>& v) {\n\t\tfor (auto& e : v) read(e);\n\t}\n\ttemplate <size_t N = 0, class T> static void read_tuple_impl(T& v) {\n\t\tif constexpr (N < tuple_size_v<T>) {\n\t\t\tread(get<N>(v));\n\t\t\tread_tuple_impl<N + 1>(v);\n\t\t}\n\t}\n\ttemplate <class... T> static void read(tuple<T...>& v) {\n\t\tread_tuple_impl(v);\n\t}\n\tstruct ReadVectorHelper {\n\t\tsize_t n;\n\t\tReadVectorHelper(size_t _n) : n(_n) {}\n\t\ttemplate <class T> operator vector<T>() {\n\t\t\tvector<T> v(n);\n\t\t\tread(v);\n\t\t\treturn v;\n\t\t}\n\t};\n\tstruct Read2DVectorHelper {\n\t\tsize_t n, m;\n\t\tRead2DVectorHelper(const pair<size_t, size_t>& nm) : n(nm.first), m(nm.second) {}\n\t\ttemplate <class T> operator vector<vector<T>>() {\n\t\t\tvector<vector<T>> v(n, vector<T>(m));\n\t\t\tread(v);\n\t\t\treturn v;\n\t\t}\n\t};\n\npublic:\n\tstring read_line() const {\n\t\tstring v;\n\t\tfor (char c = gc(); c != '\\n' && c != '\\0'; c = gc()) v += c;\n\t\treturn v;\n\t}\n\ttemplate <class T> T read() const {\n\t\tT v;\n\t\tread(v);\n\t\treturn v;\n\t}\n\ttemplate <class T> vector<T> read_vector(size_t n) const {\n\t\tvector<T> a(n);\n\t\tread(a);\n\t\treturn a;\n\t}\n\ttemplate <class T> operator T() const {\n\t\treturn read<T>();\n\t}\n\tint operator--(int) const {\n\t\treturn read<int>() - 1;\n\t}\n\tReadVectorHelper operator[](size_t n) const {\n\t\treturn ReadVectorHelper(n);\n\t}\n\tRead2DVectorHelper operator[](const pair<size_t, size_t>& nm) const {\n\t\treturn Read2DVectorHelper(nm);\n\t}\n\tvoid operator()() const {}\n\ttemplate <class H, class... T> void operator()(H&& h, T&&... t) const {\n\t\tread(h);\n\t\toperator()(forward<T>(t)...);\n\t}\n\nprivate:\n\ttemplate <template <class...> class, class...> struct Multiple;\n\ttemplate <template <class...> class V, class Head, class... Tail>\n\tstruct Multiple<V, Head, Tail...> {\n\t\ttemplate <class... Args> using vec = V<vector<Head>, Args...>;\n\t\tusing type = typename Multiple<vec, Tail...>::type;\n\t};\n\ttemplate <template <class...> class V> struct Multiple<V> { using type = V<>; };\n\ttemplate <class... T> using multiple_t = typename Multiple<tuple, T...>::type;\n\ttemplate <size_t N = 0, class T> void multiple_impl(T& t) const {\n\t\tif constexpr (N < tuple_size_v<T>) {\n\t\t\tauto& vec = get<N>(t);\n\t\t\tusing V = typename remove_reference_t<decltype(vec)>::value_type;\n\t\t\tvec.push_back(read<V>());\n\t\t\tmultiple_impl<N + 1>(t);\n\t\t}\n\t}\n\npublic:\n\ttemplate <class... T> auto multiple(size_t h) const {\n\t\tmultiple_t<T...> result;\n\t\twhile (h--) multiple_impl(result);\n\t\treturn result;\n\t}\n} in;\n#define inputs(T, ...) \\\n\tT __VA_ARGS__; \\\n\tin(__VA_ARGS__)\n#define ini(...) inputs(int, __VA_ARGS__)\n#define inl(...) inputs(long long, __VA_ARGS__)\n#define ins(...) inputs(string, __VA_ARGS__)\n#line 7 \"/home/yuruhiya/programming/library/template/Output.cpp\"\n#include <charconv>\n#line 10 \"/home/yuruhiya/programming/library/template/Output.cpp\"\nusing namespace std;\n\nstruct BoolStr {\n\tconst char *t, *f;\n\tBoolStr(const char* _t, const char* _f) : t(_t), f(_f) {}\n} Yes(\"Yes\", \"No\"), yes(\"yes\", \"no\"), YES(\"YES\", \"NO\"), Int(\"1\", \"0\");\nstruct DivStr {\n\tconst char *d, *l;\n\tDivStr(const char* _d, const char* _l) : d(_d), l(_l) {}\n} spc(\" \", \"\\n\"), no_spc(\"\", \"\\n\"), end_line(\"\\n\", \"\\n\"), comma(\",\", \"\\n\"),\n no_endl(\" \", \"\");\nclass Output {\n\tBoolStr B{Yes};\n\tDivStr D{spc};\n\npublic:\n\tvoid put(int v) const {\n\t\tchar buf[12]{};\n\t\tif (auto [ptr, e] = to_chars(begin(buf), end(buf), v); e == errc{}) {\n\t\t\tfwrite(buf, sizeof(char), ptr - buf, stdout);\n\t\t} else {\n\t\t\tassert(false);\n\t\t}\n\t}\n\tvoid put(long long v) const {\n\t\tchar buf[21]{};\n\t\tif (auto [ptr, e] = to_chars(begin(buf), end(buf), v); e == errc{}) {\n\t\t\tfwrite(buf, sizeof(char), ptr - buf, stdout);\n\t\t} else {\n\t\t\tassert(false);\n\t\t}\n\t}\n\tvoid put(bool v) const {\n\t\tput(v ? B.t : B.f);\n\t}\n\tvoid put(vector<bool>::reference v) const {\n\t\tput(v ? B.t : B.f);\n\t}\n\tvoid put(char v) const {\n\t\tputchar_unlocked(v);\n\t}\n\tvoid put(const char* v) const {\n\t\tfwrite_unlocked(v, 1, strlen(v), stdout);\n\t}\n\tvoid put(double v) const {\n\t\tprintf(\"%.20f\", v);\n\t}\n\tvoid put(long double v) const {\n\t\tprintf(\"%.20Lf\", v);\n\t}\n\ttemplate <class T> void put(const T& v) const {\n\t\tcout << v;\n\t}\n\ttemplate <class T, class U> void put(const pair<T, U>& v) const {\n\t\tput(v.first);\n\t\tput(D.d);\n\t\tput(v.second);\n\t}\n\ttemplate <class InputIterater>\n\tvoid put_range(const InputIterater& begin, const InputIterater& end) const {\n\t\tfor (InputIterater i = begin; i != end; ++i) {\n\t\t\tif (i != begin) put(D.d);\n\t\t\tput(*i);\n\t\t}\n\t}\n\ttemplate <class T> void put(const vector<T>& v) const {\n\t\tput_range(v.begin(), v.end());\n\t}\n\ttemplate <class T, size_t N> void put(const array<T, N>& v) const {\n\t\tput_range(v.begin(), v.end());\n\t}\n\ttemplate <class T> void put(const vector<vector<T>>& v) const {\n\t\tfor (size_t i = 0; i < v.size(); ++i) {\n\t\t\tif (i) put(D.l);\n\t\t\tput(v[i]);\n\t\t}\n\t}\n\n\tOutput() = default;\n\tOutput(const BoolStr& _boolstr, const DivStr& _divstr) : B(_boolstr), D(_divstr) {}\n\tOutput& operator()() {\n\t\tput(D.l);\n\t\treturn *this;\n\t}\n\ttemplate <class H> Output& operator()(H&& h) {\n\t\tput(h);\n\t\tput(D.l);\n\t\treturn *this;\n\t}\n\ttemplate <class H, class... T> Output& operator()(H&& h, T&&... t) {\n\t\tput(h);\n\t\tput(D.d);\n\t\treturn operator()(forward<T>(t)...);\n\t}\n\ttemplate <class InputIterator>\n\tOutput& range(const InputIterator& begin, const InputIterator& end) {\n\t\tput_range(begin, end);\n\t\tput(D.l);\n\t\treturn *this;\n\t}\n\ttemplate <class T> Output& range(const T& a) {\n\t\trange(a.begin(), a.end());\n\t\treturn *this;\n\t}\n\ttemplate <class... T> void exit(T&&... t) {\n\t\toperator()(forward<T>(t)...);\n\t\tstd::exit(EXIT_SUCCESS);\n\t}\n\tOutput& flush() {\n\t\tfflush_unlocked(stdout);\n\t\treturn *this;\n\t}\n\tOutput& set(const BoolStr& b) {\n\t\tB = b;\n\t\treturn *this;\n\t}\n\tOutput& set(const DivStr& d) {\n\t\tD = d;\n\t\treturn *this;\n\t}\n\tOutput& set(const char* t, const char* f) {\n\t\tB = BoolStr(t, f);\n\t\treturn *this;\n\t}\n} out;\n#line 3 \"/home/yuruhiya/programming/library/template/Step.cpp\"\nusing namespace std;\n\ntemplate <class T> struct Step {\n\tusing value_type = T;\n\n\tclass iterator {\n\t\tvalue_type a, b, c;\n\n\tpublic:\n\t\tconstexpr iterator() : a(value_type()), b(value_type()), c(value_type()) {}\n\t\tconstexpr iterator(value_type _b, value_type _c, value_type _s)\n\t\t : a(_b), b(_c), c(_s) {}\n\t\tconstexpr iterator& operator++() {\n\t\t\t--b;\n\t\t\ta += c;\n\t\t\treturn *this;\n\t\t}\n\t\tconstexpr iterator operator++(int) {\n\t\t\titerator tmp = *this;\n\t\t\t--b;\n\t\t\ta += c;\n\t\t\treturn tmp;\n\t\t}\n\t\tconstexpr const value_type& operator*() const {\n\t\t\treturn a;\n\t\t}\n\t\tconstexpr const value_type* operator->() const {\n\t\t\treturn &a;\n\t\t}\n\t\tconstexpr bool operator==(const iterator& i) const {\n\t\t\treturn b == i.b;\n\t\t}\n\t\tconstexpr bool operator!=(const iterator& i) const {\n\t\t\treturn !(b == i.b);\n\t\t}\n\t\tconstexpr value_type start() const {\n\t\t\treturn a;\n\t\t}\n\t\tconstexpr value_type size() const {\n\t\t\treturn b;\n\t\t}\n\t\tconstexpr value_type step() const {\n\t\t\treturn c;\n\t\t}\n\t};\n\tconstexpr Step(value_type b, value_type c, value_type s) : be(b, c, s) {}\n\tconstexpr iterator begin() const {\n\t\treturn be;\n\t}\n\tconstexpr iterator end() const {\n\t\treturn en;\n\t}\n\tconstexpr value_type start() const {\n\t\treturn be.start();\n\t}\n\tconstexpr value_type size() const {\n\t\treturn be.size();\n\t}\n\tconstexpr value_type step() const {\n\t\treturn be.step();\n\t}\n\tconstexpr value_type sum() const {\n\t\treturn start() * size() + step() * (size() * (size() - 1) / 2);\n\t}\n\toperator vector<value_type>() const {\n\t\treturn to_a();\n\t}\n\tauto to_a() const {\n\t\tvector<value_type> result;\n\t\tresult.reserve(size());\n\t\tfor (auto i : *this) {\n\t\t\tresult.push_back(i);\n\t\t}\n\t\treturn result;\n\t}\n\nprivate:\n\titerator be, en;\n};\ntemplate <class T> constexpr auto step(T a) {\n\treturn Step<T>(0, a, 1);\n}\ntemplate <class T> constexpr auto step(T a, T b) {\n\treturn Step<T>(a, b - a, 1);\n}\ntemplate <class T> constexpr auto step(T a, T b, T c) {\n\treturn Step<T>(a, a < b ? (b - a - 1) / c + 1 : 0, c);\n}\n#line 8 \"/home/yuruhiya/programming/library/template/Ruby.cpp\"\nusing namespace std;\n\ntemplate <class F> struct Callable {\n\tF func;\n\tCallable(const F& f) : func(f) {}\n};\ntemplate <class T, class F> auto operator|(const T& v, const Callable<F>& c) {\n\treturn c.func(v);\n}\n\nstruct Sort_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\tsort(begin(v), end(v), f);\n\t\t\treturn v;\n\t\t});\n\t}\n\ttemplate <class T> friend auto operator|(T v, [[maybe_unused]] const Sort_impl& c) {\n\t\tsort(begin(v), end(v));\n\t\treturn v;\n\t}\n} Sort;\nstruct SortBy_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\tsort(begin(v), end(v),\n\t\t\t [&](const auto& i, const auto& j) { return f(i) < f(j); });\n\t\t\treturn v;\n\t\t});\n\t}\n} SortBy;\nstruct RSort_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\tsort(rbegin(v), rend(v), f);\n\t\t\treturn v;\n\t\t});\n\t}\n\ttemplate <class T> friend auto operator|(T v, [[maybe_unused]] const RSort_impl& c) {\n\t\tsort(rbegin(v), rend(v));\n\t\treturn v;\n\t}\n} RSort;\nstruct RSortBy_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\tsort(begin(v), end(v),\n\t\t\t [&](const auto& i, const auto& j) { return f(i) > f(j); });\n\t\t\treturn v;\n\t\t});\n\t}\n} RSortBy;\nstruct Reverse_impl {\n\ttemplate <class T> friend auto operator|(T v, const Reverse_impl& c) {\n\t\treverse(begin(v), end(v));\n\t\treturn v;\n\t}\n} Reverse;\nstruct Unique_impl {\n\ttemplate <class T> friend auto operator|(T v, const Unique_impl& c) {\n\t\tv.erase(unique(begin(v), end(v), end(v)));\n\t\treturn v;\n\t}\n} Unique;\nstruct Uniq_impl {\n\ttemplate <class T> friend auto operator|(T v, const Uniq_impl& c) {\n\t\tsort(begin(v), end(v));\n\t\tv.erase(unique(begin(v), end(v)), end(v));\n\t\treturn v;\n\t}\n} Uniq;\nstruct Rotate_impl {\n\tauto operator()(int&& left) {\n\t\treturn Callable([&](auto v) {\n\t\t\tint s = static_cast<int>(size(v));\n\t\t\tassert(-s <= left && left <= s);\n\t\t\tif (0 <= left) {\n\t\t\t\trotate(begin(v), begin(v) + left, end(v));\n\t\t\t} else {\n\t\t\t\trotate(begin(v), end(v) + left, end(v));\n\t\t\t}\n\t\t\treturn v;\n\t\t});\n\t}\n} Rotate;\nstruct Max_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) { return *max_element(begin(v), end(v), f); });\n\t}\n\ttemplate <class T> friend auto operator|(T v, const Max_impl& c) {\n\t\treturn *max_element(begin(v), end(v));\n\t}\n} Max;\nstruct Min_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) { return *min_element(begin(v), end(v), f); });\n\t}\n\ttemplate <class T> friend auto operator|(T v, const Min_impl& c) {\n\t\treturn *min_element(begin(v), end(v));\n\t}\n} Min;\nstruct MaxPos_impl {\n\ttemplate <class T> friend auto operator|(T v, const MaxPos_impl& c) {\n\t\treturn max_element(begin(v), end(v)) - begin(v);\n\t}\n} MaxPos;\nstruct MinPos_impl {\n\ttemplate <class T> friend auto operator|(T v, const MinPos_impl& c) {\n\t\treturn min_element(begin(v), end(v)) - begin(v);\n\t}\n} MinPos;\nstruct MaxBy_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\tauto max_it = begin(v);\n\t\t\tauto max_val = f(*max_it);\n\t\t\tfor (auto it = next(begin(v)); it != end(v); ++it) {\n\t\t\t\tif (auto val = f(*it); max_val < val) {\n\t\t\t\t\tmax_it = it;\n\t\t\t\t\tmax_val = val;\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn *max_it;\n\t\t});\n\t}\n} MaxBy;\nstruct MinBy_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\tauto min_it = begin(v);\n\t\t\tauto min_val = f(*min_it);\n\t\t\tfor (auto it = next(begin(v)); it != end(v); ++it) {\n\t\t\t\tif (auto val = f(*it); min_val > val) {\n\t\t\t\t\tmin_it = it;\n\t\t\t\t\tmin_val = val;\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn *min_it;\n\t\t});\n\t}\n} MinBy;\nstruct MaxOf_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\tauto max_val = f(*begin(v));\n\t\t\tfor (auto it = next(begin(v)); it != end(v); ++it) {\n\t\t\t\tif (auto val = f(*it); max_val < val) {\n\t\t\t\t\tmax_val = val;\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn max_val;\n\t\t});\n\t}\n} MaxOf;\nstruct MinOf_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\tauto min_val = f(*begin(v));\n\t\t\tfor (auto it = next(begin(v)); it != end(v); ++it) {\n\t\t\t\tif (auto val = f(*it); min_val > val) {\n\t\t\t\t\tmin_val = val;\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn min_val;\n\t\t});\n\t}\n} MinOf;\nstruct Count_impl {\n\ttemplate <class V> auto operator()(const V& val) {\n\t\treturn Callable([&](auto v) { return count(begin(v), end(v), val); });\n\t}\n} Count;\nstruct CountIf_impl {\n\ttemplate <class F> auto operator()(const F& f) {\n\t\treturn Callable([&](auto v) { return count_if(begin(v), end(v), f); });\n\t}\n} CountIf;\nstruct Index_impl {\n\ttemplate <class V> auto operator()(const V& val) {\n\t\treturn Callable([&](auto v) -> optional<int> {\n\t\t\tauto result = find(begin(v), end(v), val);\n\t\t\treturn result != end(v) ? optional(result - begin(v)) : nullopt;\n\t\t});\n\t}\n} Index;\nstruct IndexIf_impl {\n\ttemplate <class F> auto operator()(const F& f) {\n\t\treturn Callable([&](auto v) -> optional<int> {\n\t\t\tauto result = find_if(begin(v), end(v), f);\n\t\t\treturn result != end(v) ? optional(result - begin(v)) : nullopt;\n\t\t});\n\t}\n} IndexIf;\nstruct FindIf_impl {\n\ttemplate <class F> auto operator()(const F& f) {\n\t\treturn Callable([&](auto v) -> optional<typename decltype(v)::value_type> {\n\t\t\tauto result = find_if(begin(v), end(v), f);\n\t\t\treturn result != end(v) ? optional(*result) : nullopt;\n\t\t});\n\t}\n} FindIf;\nstruct Sum_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\treturn accumulate(next(begin(v)), end(v), f(*begin(v)),\n\t\t\t [&](const auto& a, const auto& b) { return a + f(b); });\n\t\t});\n\t}\n\ttemplate <class T> friend auto operator|(T v, const Sum_impl& c) {\n\t\treturn accumulate(begin(v), end(v), typename T::value_type{});\n\t}\n} Sum;\nstruct Includes {\n\ttemplate <class V> auto operator()(const V& val) {\n\t\treturn Callable([&](auto v) { return find(begin(v), end(v), val) != end(v); });\n\t}\n} Includes;\nstruct IncludesIf_impl {\n\ttemplate <class F> auto operator()(const F& f) {\n\t\treturn Callable([&](auto v) { return find_if(begin(v), end(v), f) != end(v); });\n\t}\n} IncludesIf;\nstruct RemoveIf_impl {\n\ttemplate <class F> auto operator()(const F& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\tv.erase(remove_if(begin(v), end(v), f), end(v));\n\t\t\treturn v;\n\t\t});\n\t}\n} RemoveIf;\nstruct Each_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\tfor (const auto& i : v) {\n\t\t\t\tf(i);\n\t\t\t}\n\t\t});\n\t}\n} Each;\nstruct Select_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\tusing value_type = typename decltype(v)::value_type;\n\t\t\tvector<value_type> result;\n\t\t\tfor (const auto& i : v) {\n\t\t\t\tif (f(i)) result.push_back(i);\n\t\t\t}\n\t\t\treturn result;\n\t\t});\n\t}\n} Select;\nstruct Map_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\tusing result_type = invoke_result_t<F, typename decltype(v)::value_type>;\n\t\t\tvector<result_type> result;\n\t\t\tresult.reserve(size(v));\n\t\t\tfor (const auto& i : v) {\n\t\t\t\tresult.push_back(f(i));\n\t\t\t}\n\t\t\treturn result;\n\t\t});\n\t}\n} Map;\nstruct Indexed_impl {\n\ttemplate <class T> friend auto operator|(const T& v, Indexed_impl& c) {\n\t\tusing value_type = typename T::value_type;\n\t\tvector<pair<value_type, int>> result;\n\t\tresult.reserve(size(v));\n\t\tint index = 0;\n\t\tfor (const auto& i : v) {\n\t\t\tresult.emplace_back(i, index++);\n\t\t}\n\t\treturn result;\n\t}\n} Indexed;\nstruct AllOf_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\tfor (const auto& i : v) {\n\t\t\t\tif (!f(i)) return false;\n\t\t\t}\n\t\t\treturn true;\n\t\t});\n\t}\n} AllOf;\nstruct AnyOf_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\tfor (const auto& i : v) {\n\t\t\t\tif (f(i)) return true;\n\t\t\t}\n\t\t\treturn false;\n\t\t});\n\t}\n} AnyOf;\nstruct NoneOf_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\tfor (const auto& i : v) {\n\t\t\t\tif (f(i)) return false;\n\t\t\t}\n\t\t\treturn true;\n\t\t});\n\t}\n} NoneOf;\n\nstruct Tally_impl {\n\ttemplate <class F> auto operator()(size_t max_val) {\n\t\treturn Callable([&](auto v) {\n\t\t\tvector<size_t> result(max_val);\n\t\t\tfor (const auto& i : v) {\n\t\t\t\tresult[static_cast<size_t>(i)]++;\n\t\t\t}\n\t\t\treturn result;\n\t\t});\n\t}\n\ttemplate <class T, class value_type = typename T::value_type>\n\tfriend auto operator|(const T& v, Tally_impl& c) {\n\t\tmap<value_type, size_t> result;\n\t\tfor (const auto& i : v) {\n\t\t\tresult[i]++;\n\t\t}\n\t\treturn result;\n\t}\n} Tally;\n\ntemplate <class T> auto operator*(const vector<T>& a, size_t n) {\n\tT result;\n\tfor (size_t i = 0; i < n; ++i) {\n\t\tresult.insert(result.end(), a.begin(), a.end());\n\t}\n\treturn result;\n}\nauto operator*(string a, size_t n) {\n\tstring result;\n\tfor (size_t i = 0; i < n; ++i) {\n\t\tresult += a;\n\t}\n\treturn result;\n}\ntemplate <class T, class U> auto& operator<<(vector<T>& a, const U& b) {\n\ta.insert(a.end(), all(b));\n\treturn a;\n}\ntemplate <class T> auto& operator<<(string& a, const T& b) {\n\ta.insert(a.end(), all(b));\n\treturn a;\n}\ntemplate <class T, class U> auto operator+(vector<T> a, const U& b) {\n\ta << b;\n\treturn a;\n}\ntemplate <class T> auto operator+(string a, const T& b) {\n\ta << b;\n\treturn a;\n}\n#line 6 \"/home/yuruhiya/programming/library/template/functions.cpp\"\nusing namespace std;\n\ntemplate <class T> int sz(const T& v) {\n\treturn v.size();\n}\ntemplate <class T, class U> int lower_index(const T& a, const U& v) {\n\treturn lower_bound(all(a), v) - a.begin();\n}\ntemplate <class T, class U> int upper_index(const T& a, const U& v) {\n\treturn upper_bound(all(a), v) - a.begin();\n}\ntemplate <class T> auto Slice(const T& v, size_t i, size_t len) {\n\treturn i < v.size() ? T(v.begin() + i, v.begin() + min(i + len, v.size())) : T();\n}\ntemplate <class T> T div_ceil(T n, T m) {\n\treturn (n + m - 1) / m;\n}\ntemplate <class T> T div_ceil2(T n, T m) {\n\treturn div_ceil(n, m) * m;\n}\ntemplate <class T> T triangle(T n) {\n\treturn (n & 1) ? (n + 1) / 2 * n : n / 2 * (n + 1);\n}\ntemplate <class T> T nC2(T n) {\n\treturn (n & 1) ? (n - 1) / 2 * n : n / 2 * (n - 1);\n}\ntemplate <class T> T middle(const T& l, const T& r) {\n\treturn l + (r - l) / 2;\n}\ntemplate <class T> bool chmax(T& a, const T& b) {\n\tif (a < b) {\n\t\ta = b;\n\t\treturn true;\n\t}\n\treturn false;\n}\ntemplate <class T> bool chmin(T& a, const T& b) {\n\tif (a > b) {\n\t\ta = b;\n\t\treturn true;\n\t}\n\treturn false;\n}\ntemplate <class T> bool in_range(const T& v, const T& min, const T& max) {\n\treturn min <= v && v < max;\n}\ntemplate <class T> bool in_square(T n) {\n\tT s = sqrt(n);\n\treturn s * s == n || (s + 1) * (s + 1) == n;\n}\ntemplate <class T = long long> T BIT(int b) {\n\treturn T(1) << b;\n}\ntemplate <class T, class U = typename T::value_type> U Gcdv(const T& v) {\n\treturn accumulate(next(v.begin()), v.end(), U(*v.begin()), gcd<U, U>);\n}\ntemplate <class T, class U = typename T::value_type> U Lcmv(const T& v) {\n\treturn accumulate(next(v.begin()), v.end(), U(*v.begin()), lcm<U, U>);\n}\ntemplate <class T, class U> T Pow(T a, U n) {\n\tT result = 1;\n\twhile (n > 0) {\n\t\tif (n & 1) {\n\t\t\tresult *= a;\n\t\t\tn--;\n\t\t} else {\n\t\t\ta *= a;\n\t\t\tn >>= 1;\n\t\t}\n\t}\n\treturn result;\n}\ntemplate <class T, class U> T Powmod(T a, U n, T mod) {\n\tT result = 1;\n\twhile (n > 0) {\n\t\tif (n & 1) {\n\t\t\tresult = result * a % mod;\n\t\t\tn--;\n\t\t} else {\n\t\t\ta = a * a % mod;\n\t\t\tn >>= 1;\n\t\t}\n\t}\n\treturn result;\n}\nnamespace internal {\n\ttemplate <class T, size_t N> auto make_vector(vector<int>& sizes, const T& init) {\n\t\tif constexpr (N == 1) {\n\t\t\treturn vector(sizes[0], init);\n\t\t} else {\n\t\t\tint size = sizes[N - 1];\n\t\t\tsizes.pop_back();\n\t\t\treturn vector(size, make_vector<T, N - 1>(sizes, init));\n\t\t}\n\t}\n} // namespace internal\ntemplate <class T, size_t N>\nauto make_vector(const int (&sizes)[N], const T& init = T()) {\n\tvector s(rbegin(sizes), rend(sizes));\n\treturn internal::make_vector<T, N>(s, init);\n}\n#line 9 \"/home/yuruhiya/programming/library/template/template.cpp\"\n#if __has_include(<library/dump.hpp>)\n#include <library/dump.hpp>\n#define LOCAL\n#else\n#define dump(...) ((void)0)\n#endif\n\ntemplate <class T> constexpr T oj_local(const T& oj, const T& local) {\n#ifndef LOCAL\n\treturn oj;\n#else\n\treturn local;\n#endif\n}\n#line 2 \"a.cpp\"\n\nint solve(int limit, int n, int m, int x) {\n\tauto [s_name, s_weight, s_value] = in.multiple<string, int, int>(n);\n\tauto [name, weight, value] = in.multiple<string, int, int>(m);\n\n\tauto dp = make_vector<int>({m + 1, limit + 1}, -1);\n\trep(i, limit + 1) dp[0][i] = 0;\n\trep(i, m) {\n\t\tauto dp2 = dp;\n\t\trep(j, m + 1) rep(k, limit) {\n\t\t\tif (j < m && k + weight[i] <= limit && dp[j][k] != -1) {\n\t\t\t\tchmax(dp2[j + 1][k + weight[i]], dp[j][k] + value[i]);\n\t\t\t}\n\t\t\tchmax(dp2[j][k + 1], dp2[j][k]);\n\t\t}\n\t\tdp = dp2;\n\t}\n\n\tint ans = 0;\n\tauto update = [&](int val, int wei) {\n\t\tFOR(i, x, m + 1) {\n\t\t\tif (limit - wei >= 0) {\n\t\t\t\tchmax(ans, dp[i][limit - wei] + val);\n\t\t\t}\n\t\t}\n\t};\n\trep(i, n) {\n\t\tupdate(s_value[i], s_weight[i]);\n\t}\n\trep(i, n) FOR(j, i + 1, n) {\n\t\tupdate(s_value[i] + s_value[j], s_weight[i] + s_weight[j]);\n\t}\n\treturn ans;\n}\n\nint main() {\n\twhile (true) {\n\t\tini(limit, n, m, x);\n\t\tif (n == 0) break;\n\t\tout(solve(limit, n, m, x));\n\t}\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3644, "score_of_the_acc": -0.1685, "final_rank": 12 }, { "submission_id": "aoj_2285_3341687", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))\n#define all(x) (x).begin(),(x).end()\n#define pb push_back\n#define fi first\n#define se second\n#define dbg(x) cout<<#x\" = \"<<((x))<<endl\ntemplate<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<\"(\"<<p.fi<<\",\"<<p.se<<\")\";return o;}\ntemplate<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<\"[\";for(T t:v){o<<t<<\",\";}o<<\"]\";return o;}\n\nstruct artist{\n int cost;\n int val;\n\n void READ(){\n char name[33];\n scanf(\" %s %d %d\", name, &cost, &val);\n }\n};\n\nconst int M = 111;\nconst int L = 1001;\nconst int INF = 10101010;\n\nint dp[M][M][L];\nint mx[L];\n\nint main(){\n int lim,n,m,x;\n while(scanf(\" %d %d %d %d\", &lim, &n, &m, &x),lim){\n vector<artist> a(n),b(m);\n rep(i,n) a[i].READ();\n rep(i,m) b[i].READ();\n\n rep(i,M)rep(j,M)rep(k,L) dp[i][j][k] = -INF;\n dp[0][0][0] = 0;\n rep(i,m)rep(j,i+1)rep(k,L)if(dp[i][j][k]>=0){\n dp[i+1][j][k] = max(dp[i+1][j][k], dp[i][j][k]);\n\n int nk = k+b[i].cost;\n if(nk<L) dp[i+1][j+1][nk] = max(dp[i+1][j+1][nk], dp[i][j][k]+b[i].val);\n }\n\n rep(i,L) mx[i] = -INF;\n for(int i=x; i<=m; ++i)rep(j,L) mx[j] = max(mx[j], dp[m][i][j]);\n rep(i,L-1) mx[i+1] = max(mx[i+1],mx[i]);\n\n int ans = -INF;\n rep(i,n){\n int rem = lim-a[i].cost;\n if(rem<0) continue;\n ans = max(ans, a[i].val+mx[rem]);\n\n rep(j,n)if(i!=j){\n int rr = rem-a[j].cost;\n if(rr<0) continue;\n ans = max(ans, a[i].val+a[j].val+mx[rr]);\n }\n }\n printf(\"%d\\n\", ans);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 170, "memory_kb": 51352, "score_of_the_acc": -1.6667, "final_rank": 19 }, { "submission_id": "aoj_2285_2890938", "code_snippet": "#include <bits/stdc++.h>\n#define rep(i,n) for(int i=0;i<n;i++)\nusing namespace std;\n\nint limit,n,m,x;\n\nint a[111],b[111],c[111],d[111];\nstring damy;\nint dp[1111][111];\nbool used[1111][111];\nint MAX[1111];\n\nvoid Input(){\n rep(i,m){\n cin>>damy>>c[i]>>d[i];\n }\n rep(i,n){\n cin>>damy>>a[i]>>b[i];\n }\n}\n\nvoid init(){\n memset(MAX,0,sizeof(MAX));\n memset(dp,0,sizeof(dp));\n memset(used,0,sizeof(used));\n}\n\nvoid DP(){\n used[0][0]=1;\n for(int k=0;k<n;k++){\n for(int i=limit;i>=0;i--){\n for(int j=n;j>=0;j--){\n\tif(used[i][j]){\n\t if(i+a[k]<=limit){\n\t used[i+a[k]][j+1]=1;\n\t dp[i+a[k]][j+1]=max(dp[i+a[k]][j+1],dp[i][j]+b[k]);\n\t }\n\t}\n }\n }\n }\n\n for(int i=0;i<=limit;i++){\n for(int j=x;j<=n;j++){\n MAX[i]=max(MAX[i],dp[i][j]);\n }\n }\n\n int ans=0;\n\n for(int i=0;i<m;i++){\n for(int j=i+1;j<m;j++){\n for(int k=0;k<limit;k++){\n\tif(k+c[i]+c[j]>limit)break;\n\tans=max(ans,MAX[k]+d[i]+d[j]);\n }\n }\n }\n\n for(int i=0;i<m;i++){\n for(int k=0;k<limit;k++){\n if(k+c[i]>limit)break;\n ans=max(ans,MAX[k]+d[i]);\n }\n }\n\n cout << ans << endl;\n \n}\n\n\nint main(){\n while( cin>>limit>>m>>n>>x,limit){\n Input();\n init();\n DP();\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3612, "score_of_the_acc": -0.0846, "final_rank": 8 }, { "submission_id": "aoj_2285_2890891", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing Int = long long;\nInt dp[111][1111];\n\ntemplate<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}\ntemplate<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}\nsigned main(){ \n cin.tie(0);\n ios::sync_with_stdio(0);\n Int l,n,m,x;\n while(cin>>l>>n>>m>>x,l){\n string st;\n vector<Int> se(n),ss(n);\n vector<Int> e(m),s(m);\n for(Int i=0;i<n;i++) cin>>st>>se[i]>>ss[i];\n for(Int i=0;i<m;i++) cin>>st>>e[i]>>s[i];\n \n const Int INF = 1e9;\n for(Int i=0;i<111;i++)\n for(Int j=0;j<1111;j++)\n\tdp[i][j]=-INF;\n\n dp[0][0]=0;\n for(Int k=0;k<m;k++)\n for(Int i=m;i>=0;i--)\n\tfor(Int j=l;j>=0;j--)\n\t chmax(dp[i+1][j+e[k]],dp[i][j]+s[k]);\n \n vector<Int> dp2(l+1,-INF);\n for(Int i=x;i<=m;i++)\n for(Int j=0;j<=l;j++)\n\tchmax(dp2[j],dp[i][j]);\n\n for(Int j=1;j<=l;j++) chmax(dp2[j],dp2[j-1]);\n\n Int ans=-INF;\n for(Int i=0;i<n;i++){\n if(se[i]>l) continue;\n chmax(ans,dp2[l-se[i]]+ss[i]);\n for(Int j=0;j<i;j++){\n\tif(se[i]+se[j]>l) continue;\n\tchmax(ans,dp2[l-(se[i]+se[j])]+ss[i]+ss[j]);\n }\n }\n cout<<ans<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 4156, "score_of_the_acc": -0.0538, "final_rank": 6 }, { "submission_id": "aoj_2285_2890455", "code_snippet": "#include <bits/stdc++.h>\n#define rep(i,n) for(int i=0;i<n;i++)\nusing namespace std;\n\nint limit,n,m,x;\n\nint a[111],b[111],c[111],d[111];\nstring damy;\nint dp[1111][111];\nbool used[1111][111];\nint MAX[1111];\n\nvoid Input(){\n rep(i,m){\n cin>>damy>>c[i]>>d[i];\n }\n rep(i,n){\n cin>>damy>>a[i]>>b[i];\n }\n}\n\nvoid init(){\n memset(MAX,0,sizeof(MAX));\n memset(dp,0,sizeof(dp));\n memset(used,0,sizeof(used));\n}\n\nvoid DP(){\n used[0][0]=1;\n for(int k=0;k<n;k++){\n for(int i=limit;i>=0;i--){\n for(int j=n;j>=0;j--){\n\tif(used[i][j]){\n\t if(i+a[k]<=limit){\n\t used[i+a[k]][j+1]=1;\n\t dp[i+a[k]][j+1]=max(dp[i+a[k]][j+1],dp[i][j]+b[k]);\n\t }\n\t}\n }\n }\n }\n\n for(int i=0;i<=limit;i++){\n for(int j=x;j<=n;j++){\n MAX[i]=max(MAX[i],dp[i][j]);\n }\n }\n\n int ans=0;\n\n for(int i=0;i<m;i++){\n for(int j=i+1;j<m;j++){\n for(int k=0;k<limit;k++){\n\tif(k+c[i]+c[j]>limit)break;\n\tans=max(ans,MAX[k]+d[i]+d[j]);\n }\n }\n }\n\n for(int i=0;i<m;i++){\n for(int k=0;k<limit;k++){\n if(k+c[i]>limit)break;\n ans=max(ans,MAX[k]+d[i]);\n }\n }\n\n cout << ans << endl;\n \n}\n\n\nint main(){\n while( cin>>limit>>m>>n>>x,limit){\n Input();\n init();\n DP();\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3756, "score_of_the_acc": -0.0875, "final_rank": 10 }, { "submission_id": "aoj_2285_2890421", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing Int = long long;\nInt dp[111][1111];\n\ntemplate<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}\ntemplate<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}\nsigned main(){ \n cin.tie(0);\n ios::sync_with_stdio(0);\n Int l,n,m,x;\n while(cin>>l>>n>>m>>x,l){\n string st;\n vector<Int> se(n),ss(n);\n vector<Int> e(m),s(m);\n for(Int i=0;i<n;i++) cin>>st>>se[i]>>ss[i];\n for(Int i=0;i<m;i++) cin>>st>>e[i]>>s[i];\n \n const Int INF = 1e9;\n for(Int i=0;i<111;i++)\n for(Int j=0;j<1111;j++)\n\tdp[i][j]=-INF;\n\n dp[0][0]=0;\n for(Int k=0;k<m;k++)\n for(Int i=m;i>=0;i--)\n\tfor(Int j=l;j>=0;j--)\n\t chmax(dp[i+1][j+e[k]],dp[i][j]+s[k]);\n \n vector<Int> dp2(l+1,-INF);\n for(Int i=x;i<=m;i++)\n for(Int j=0;j<=l;j++)\n\tchmax(dp2[j],dp[i][j]);\n\n for(Int j=1;j<=l;j++) chmax(dp2[j],dp2[j-1]);\n\n Int ans=-INF;\n for(Int i=0;i<n;i++){\n if(se[i]>l) continue;\n chmax(ans,dp2[l-se[i]]+ss[i]);\n for(Int j=0;j<i;j++){\n\tif(se[i]+se[j]>l) continue;\n\tchmax(ans,dp2[l-(se[i]+se[j])]+ss[i]+ss[j]);\n }\n }\n cout<<ans<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 4036, "score_of_the_acc": -0.0514, "final_rank": 4 }, { "submission_id": "aoj_2285_2560007", "code_snippet": "#include <stdio.h>\n#include <cmath>\n#include <algorithm>\n#include <cfloat>\n#include <stack>\n#include <queue>\n#include <vector>\n#include <string>\n#include <iostream>\n#include <set>\n#include <map>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\nint LIMIT,N,M,X;\n\nstruct Info{\n\tint cost,value;\n};\n\nstruct Data{\n\tData(int arg_num,int arg_cost){\n\t\tnum = arg_num;\n\t\tcost = arg_cost;\n\t}\n\tint num,cost;\n};\n\nvoid func(){\n\n\tInfo sec[N];\n\n\tchar buf[31];\n\n\tfor(int i = 0; i < N; i++){\n\t\tscanf(\"%s %d %d\",buf,&sec[i].cost,&sec[i].value);\n\t}\n\n\tint dp_sec[3][21],next_sec[3][21];\n\n\tfor(int num = 0; num <= 2; num++){\n\t\tfor(int total_cost = 0; total_cost <= 20; total_cost++){\n\t\t\tdp_sec[num][total_cost] = -1;\n\t\t\tnext_sec[num][total_cost] = -1;\n\t\t}\n\t}\n\n\tdp_sec[0][0] = 0;\n\tnext_sec[0][0] = 0;\n\n\tvector<Data> Updated;\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tfor(int num = 0; num <= 1; num++){\n\t\t\tfor(int total_cost = 0; total_cost+sec[i].cost <= 20; total_cost++){\n\t\t\t\tif(dp_sec[num][total_cost] == -1)continue;\n\n\t\t\t\tif(next_sec[num+1][total_cost+sec[i].cost] < dp_sec[num][total_cost]+sec[i].value){\n\t\t\t\t\tnext_sec[num+1][total_cost+sec[i].cost] = dp_sec[num][total_cost]+sec[i].value;\n\t\t\t\t\tUpdated.push_back(Data(num+1,total_cost+sec[i].cost));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tfor(int k = 0; k < Updated.size(); k++){\n\t\t\tdp_sec[Updated[k].num][Updated[k].cost] = next_sec[Updated[k].num][Updated[k].cost];\n\t\t}\n\t\tUpdated.clear();\n\t}\n\n\tInfo stan[M];\n\tint cost_sum = 0;\n\n\tfor(int i = 0; i < M; i++){\n\t\tscanf(\"%s %d %d\",buf,&stan[i].cost,&stan[i].value);\n\t\tcost_sum += stan[i].cost;\n\t}\n\n\tint dp_stan[M+1][cost_sum+1],next_stan[M+1][cost_sum+1];\n\n\tfor(int num = 0; num <= M; num++){\n\t\tfor(int total_cost = 0; total_cost <= cost_sum; total_cost++){\n\t\t\tdp_stan[num][total_cost] = -1;\n\t\t\tnext_stan[num][total_cost] = -1;\n\t\t}\n\t}\n\n\tdp_stan[0][0] = 0;\n\tnext_stan[0][0] = 0;\n\tUpdated.clear();\n\n\tfor(int i = 0; i < M; i++){ //100\n\n\t\tfor(int num = 0; num <= M-1; num++){ //100\n\t\t\tfor(int total_cost = 0; total_cost+stan[i].cost <= cost_sum; total_cost++){ //1000\n\t\t\t\tif(dp_stan[num][total_cost] == -1)continue;\n\n\t\t\t\tif(next_stan[num+1][total_cost+stan[i].cost] < dp_stan[num][total_cost]+stan[i].value){\n\t\t\t\t\tnext_stan[num+1][total_cost+stan[i].cost] = dp_stan[num][total_cost]+stan[i].value;\n\t\t\t\t\tUpdated.push_back(Data(num+1,total_cost+stan[i].cost));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tfor(int k = 0; k < Updated.size(); k++){\n\t\t\tdp_stan[Updated[k].num][Updated[k].cost] = next_stan[Updated[k].num][Updated[k].cost];\n\t\t}\n\t\tUpdated.clear();\n\t}\n\n\tint ans = 0;\n\n\tfor(int sec_num = 1; sec_num <= 2; sec_num++){\n\t\tfor(int sec_cost = 0; sec_cost <= 20; sec_cost++){\n\t\t\tif(dp_sec[sec_num][sec_cost] == -1)continue;\n\t\t\tfor(int stan_num = X; stan_num <= M; stan_num++){\n\t\t\t\tfor(int stan_cost = 0; stan_cost <= min(LIMIT,cost_sum); stan_cost++){\n\t\t\t\t\tif(dp_stan[stan_num][stan_cost] == -1)continue;\n\t\t\t\t\tif(sec_cost+stan_cost > LIMIT)continue;\n\n\t\t\t\t\tans = max(ans,dp_sec[sec_num][sec_cost]+dp_stan[stan_num][stan_cost]);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tprintf(\"%d\\n\",ans);\n}\n\nint main(){\n\n\twhile(true){\n\t\tscanf(\"%d %d %d %d\",&LIMIT,&N,&M,&X);\n\t\tif(LIMIT == 0 && N == 0 && M == 0 && X == 0)break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3748, "score_of_the_acc": -0.0456, "final_rank": 3 }, { "submission_id": "aoj_2285_2560006", "code_snippet": "#include <stdio.h>\n#include <cmath>\n#include <algorithm>\n#include <cfloat>\n#include <stack>\n#include <queue>\n#include <vector>\n#include <string>\n#include <iostream>\n#include <set>\n#include <map>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\nint LIMIT,N,M,X;\nint dp_sec[3][21],next_sec[3][21];\nint dp_stan[101][1001],next_stan[101][1001];\n\nstruct Info{\n\tint cost,value;\n};\n\nstruct Data{\n\tData(int arg_num,int arg_cost){\n\t\tnum = arg_num;\n\t\tcost = arg_cost;\n\t}\n\tint num,cost;\n};\n\nvoid func(){\n\n\tInfo sec[N];\n\n\tchar buf[31];\n\n\tfor(int i = 0; i < N; i++){\n\t\tscanf(\"%s %d %d\",buf,&sec[i].cost,&sec[i].value);\n\t}\n\n\tfor(int num = 0; num <= 2; num++){\n\t\tfor(int total_cost = 0; total_cost <= 20; total_cost++){\n\t\t\tdp_sec[num][total_cost] = -1;\n\t\t\tnext_sec[num][total_cost] = -1;\n\t\t}\n\t}\n\n\tdp_sec[0][0] = 0;\n\tnext_sec[0][0] = 0;\n\n\tvector<Data> Updated;\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tfor(int num = 0; num <= 1; num++){\n\t\t\tfor(int total_cost = 0; total_cost+sec[i].cost <= 20; total_cost++){\n\t\t\t\tif(dp_sec[num][total_cost] == -1)continue;\n\n\t\t\t\tif(next_sec[num+1][total_cost+sec[i].cost] < dp_sec[num][total_cost]+sec[i].value){\n\t\t\t\t\tnext_sec[num+1][total_cost+sec[i].cost] = dp_sec[num][total_cost]+sec[i].value;\n\t\t\t\t\tUpdated.push_back(Data(num+1,total_cost+sec[i].cost));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tfor(int k = 0; k < Updated.size(); k++){\n\t\t\tdp_sec[Updated[k].num][Updated[k].cost] = next_sec[Updated[k].num][Updated[k].cost];\n\t\t}\n\t\tUpdated.clear();\n\t}\n\n\tInfo stan[M];\n\tint cost_sum = 0;\n\n\tfor(int i = 0; i < M; i++){\n\t\tscanf(\"%s %d %d\",buf,&stan[i].cost,&stan[i].value);\n\t\tcost_sum += stan[i].cost;\n\t}\n\n\tfor(int num = 0; num <= M; num++){\n\t\tfor(int total_cost = 0; total_cost <= 1000; total_cost++){\n\t\t\tdp_stan[num][total_cost] = -1;\n\t\t\tnext_stan[num][total_cost] = -1;\n\t\t}\n\t}\n\n\tdp_stan[0][0] = 0;\n\tnext_stan[0][0] = 0;\n\tUpdated.clear();\n\n\tfor(int i = 0; i < M; i++){ //100\n\n\t\tfor(int num = 0; num <= M-1; num++){ //100\n\t\t\tfor(int total_cost = 0; total_cost+stan[i].cost <= cost_sum; total_cost++){ //1000\n\t\t\t\tif(dp_stan[num][total_cost] == -1)continue;\n\n\t\t\t\tif(next_stan[num+1][total_cost+stan[i].cost] < dp_stan[num][total_cost]+stan[i].value){\n\t\t\t\t\tnext_stan[num+1][total_cost+stan[i].cost] = dp_stan[num][total_cost]+stan[i].value;\n\t\t\t\t\tUpdated.push_back(Data(num+1,total_cost+stan[i].cost));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tfor(int k = 0; k < Updated.size(); k++){\n\t\t\tdp_stan[Updated[k].num][Updated[k].cost] = next_stan[Updated[k].num][Updated[k].cost];\n\t\t}\n\t\tUpdated.clear();\n\t}\n\n\tint ans = 0;\n\n\tfor(int sec_num = 1; sec_num <= 2; sec_num++){\n\t\tfor(int sec_cost = 0; sec_cost <= 20; sec_cost++){\n\t\t\tif(dp_sec[sec_num][sec_cost] == -1)continue;\n\t\t\tfor(int stan_num = X; stan_num <= M; stan_num++){\n\t\t\t\tfor(int stan_cost = 0; stan_cost <= LIMIT; stan_cost++){\n\t\t\t\t\tif(dp_stan[stan_num][stan_cost] == -1)continue;\n\t\t\t\t\tif(sec_cost+stan_cost > LIMIT)continue;\n\n\t\t\t\t\tans = max(ans,dp_sec[sec_num][sec_cost]+dp_stan[stan_num][stan_cost]);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tprintf(\"%d\\n\",ans);\n}\n\nint main(){\n\n\twhile(true){\n\t\tscanf(\"%d %d %d %d\",&LIMIT,&N,&M,&X);\n\t\tif(LIMIT == 0 && N == 0 && M == 0 && X == 0)break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 4084, "score_of_the_acc": -0.0524, "final_rank": 5 }, { "submission_id": "aoj_2285_1879617", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define MAX 1010\n#define MAX_M 101\n\nint dp[MAX_M][MAX];\n\nint main()\n{\n int LIMIT, N, M, X;\n string name;\n while (cin >> LIMIT >> N >> M >> X, LIMIT) {\n vector<int> c(N), s(N);\n for (int i = 0; i < N; i++) {\n cin >> name >> c[i] >> s[i];\n }\n int max_s[MAX];\n fill(max_s, max_s + MAX, -1);\n for (int i = 0; i < N; i++) {\n max_s[c[i]] = max(max_s[c[i]], s[i]);\n for (int j = i + 1; j < N; j++) {\n max_s[c[i] + c[j]] = max(max_s[c[i] + c[j]], s[i] + s[j]);\n }\n }\n for (int i = 0; i < LIMIT; i++) {\n max_s[i+1] = max(max_s[i+1], max_s[i]);\n }\n vector<int> sc(M), ss(M);\n for (int i = 0; i < M; i++) {\n cin >> name >> sc[i] >> ss[i];\n }\n for (int i = 0; i <= M; i++) {\n for (int j = 0; j <= LIMIT; j++) {\n dp[i][j] = -1;\n }\n }\n dp[0][0] = 0;\n for (int i = 0; i < M; i++) {\n for (int j = M-1; j >= 0; j--) {\n for (int k = LIMIT-sc[i]; k >= 0; k--) {\n if (dp[j][k] == -1) continue;\n int nk = k + sc[i];\n if (nk <= LIMIT) {\n dp[j+1][nk] = max(dp[j+1][nk], dp[j][k] + ss[i]);\n }\n }\n }\n }\n \n int res = 0;\n for (int i = 0; i <= LIMIT; i++) {\n if (max_s[i] == -1) continue;\n for (int j = X; j <= M; j++) {\n if (dp[j][LIMIT-i] == -1) continue;\n res = max(res, max_s[i] + dp[j][LIMIT-i]); \n }\n }\n cout << res << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3540, "score_of_the_acc": -0.0415, "final_rank": 1 }, { "submission_id": "aoj_2285_1805140", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define MAX 1010\n#define MAX_M 101\n\nint dp[MAX_M][MAX_M][MAX];\n\nint main()\n{\n int LIMIT, N, M, X;\n string name;\n while (cin >> LIMIT >> N >> M >> X, LIMIT) {\n vector<int> c(N), s(N);\n for (int i = 0; i < N; i++) {\n cin >> name >> c[i] >> s[i];\n }\n int max_s[MAX];\n fill(max_s, max_s + MAX, -1);\n for (int i = 0; i < N; i++) {\n max_s[c[i]] = max(max_s[c[i]], s[i]);\n for (int j = i + 1; j < N; j++) {\n max_s[c[i] + c[j]] = max(max_s[c[i] + c[j]], s[i] + s[j]);\n }\n }\n for (int i = 0; i < LIMIT; i++) {\n max_s[i+1] = max(max_s[i+1], max_s[i]);\n }\n vector<int> sc(M), ss(M);\n for (int i = 0; i < M; i++) {\n cin >> name >> sc[i] >> ss[i];\n }\n for (int i = 0; i <= M; i++) {\n for (int j = 0; j <= M; j++) {\n for (int k = 0; k <= LIMIT; k++) {\n dp[i][j][k] = -1;\n }\n }\n dp[i][0][0] = 0;\n }\n \n for (int i = 0; i < M; i++) {\n for (int j = 0; j <= M; j++) {\n for (int k = LIMIT; k >= 0; k--) {\n if (dp[i][j][k] == -1) continue;\n int nk = k + sc[i];\n dp[i+1][j][k] = max(dp[i+1][j][k], dp[i][j][k]);\n if (nk <= LIMIT) {\n dp[i+1][j+1][nk] = max(dp[i+1][j+1][nk], dp[i][j][k] + ss[i]);\n }\n }\n }\n } \n \n int res = 0;\n for (int i = 0; i <= LIMIT; i++) {\n if (max_s[i] == -1) continue;\n for (int j = 0; j <= M; j++) {\n for (int k = X; k <= M; k++) {\n if (dp[j][k][LIMIT-i] == -1) continue;\n res = max(res, max_s[i] + dp[j][k][LIMIT-i]); \n }\n }\n }\n cout << res << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 41452, "score_of_the_acc": -1.0099, "final_rank": 15 }, { "submission_id": "aoj_2285_1805138", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define MAX 1010\n#define MAX_M 101\n\nint dp[MAX_M][MAX_M][MAX];\n\nint main()\n{\n int LIMIT, N, M, X;\n string name;\n while (cin >> LIMIT >> N >> M >> X, LIMIT) {\n vector<int> c(N), s(N);\n for (int i = 0; i < N; i++) {\n cin >> name >> c[i] >> s[i];\n }\n int max_s[MAX];\n fill(max_s, max_s + MAX, -1);\n for (int i = 0; i < N; i++) {\n max_s[c[i]] = max(max_s[c[i]], s[i]);\n for (int j = i + 1; j < N; j++) {\n max_s[c[i] + c[j]] = max(max_s[c[i] + c[j]], s[i] + s[j]);\n }\n }\n for (int i = 0; i < LIMIT; i++) {\n max_s[i+1] = max(max_s[i+1], max_s[i]);\n }\n vector<int> sc(M), ss(M);\n for (int i = 0; i < M; i++) {\n cin >> name >> sc[i] >> ss[i];\n }\n for (int i = 0; i <= M; i++) {\n for (int j = 0; j <= M; j++) {\n for (int k = 0; k <= LIMIT; k++) {\n dp[i][j][k] = -1;\n }\n }\n dp[i][0][0] = 0;\n }\n \n for (int i = 0; i < M; i++) {\n for (int j = 0; j <= M; j++) {\n for (int k = LIMIT; k >= 0; k--) {\n if (dp[i][j][k] == -1) continue;\n int nk = k + sc[i];\n dp[i+1][j][k] = max(dp[i+1][j][k], dp[i][j][k]);\n dp[i+1][j+1][nk] = max(dp[i+1][j+1][nk], dp[i][j][k] + ss[i]);\n }\n }\n } \n \n int res = 0;\n for (int i = 0; i <= LIMIT; i++) {\n if (max_s[i] == -1) continue;\n for (int j = 0; j <= M; j++) {\n for (int k = X; k <= M; k++) {\n if (dp[j][k][LIMIT-i] == -1) continue;\n res = max(res, max_s[i] + dp[j][k][LIMIT-i]); \n }\n }\n }\n cout << res << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 41456, "score_of_the_acc": -1.0099, "final_rank": 16 }, { "submission_id": "aoj_2285_1472640", "code_snippet": "#include<cstdio>\n#include<cstdlib>\n#include<cstring>\n#include<cmath>\n#include<iostream>\n#include<string>\n#include<vector>\n#include<map>\n#include<set>\n#include<list>\n#include<queue>\n#include<deque>\n#include<algorithm>\n#include<numeric>\n#include<utility>\n#include<complex>\n#include<functional>\n \nusing namespace std;\n\n/* constant */\n\nconst int MAX_L = 1000;\nconst int MAX_N = 100;\nconst int MAX_M = 100;\n\n/* typedef */\n\ntypedef pair<int,int> pii;\n\n/* global variables */\n\nint lim, n, m, x;\npii secs[MAX_N], stds[MAX_M];\nint dp0[3][MAX_L + 1], dp1[MAX_M + 1][MAX_L + 1];\n\n/* subroutines */\n\n/* main */\n\nint main() {\n for (;;) {\n cin >> lim >> n >> m >> x;\n if (lim == 0) break;\n\n string name;\n for (int i = 0; i < n; i++)\n cin >> name >> secs[i].first >> secs[i].second;\n for (int i = 0; i < m; i++)\n cin >> name >> stds[i].first >> stds[i].second;\n\n memset(dp0, -1, sizeof(dp0));\n dp0[0][0] = 0;\n\n for (int i = 0; i < n; i++) {\n int &ei = secs[i].first, &si = secs[i].second;\n int maxl = lim - ei;\n for (int j = 1; j >= 0; j--)\n\tfor (int l = 0; l <= maxl; l++)\n\t if (dp0[j][l] >= 0 && dp0[j + 1][l + ei] < dp0[j][l] + si)\n\t dp0[j + 1][l + ei] = dp0[j][l] + si;\n }\n\n for (int i = 1; i <= 2; i++)\n for (int l = 1; l <= lim; l++)\n\tif (dp0[i][l] < dp0[i][l - 1]) dp0[i][l] = dp0[i][l - 1];\n \n memset(dp1, -1, sizeof(dp1));\n dp1[0][0] = 0;\n\n for (int i = 0; i < m; i++) {\n int &ei = stds[i].first, &si = stds[i].second;\n int maxl = lim - ei;\n for (int j = m - 1; j >= 0; j--)\n\tfor (int l = 0; l <= maxl; l++)\n\t if (dp1[j][l] >= 0 && dp1[j + 1][l + ei] < dp1[j][l] + si)\n\t dp1[j + 1][l + ei] = dp1[j][l] + si;\n }\n \n for (int i = 1; i <= m; i++)\n for (int l = 1; l <= lim; l++)\n\tif (dp1[i][l] < dp1[i][l - 1]) dp1[i][l] = dp1[i][l - 1];\n\n int maxsum = 0;\n\n for (int i = 1; i <= 2; i++)\n for (int j = x; j <= m; j++)\n\tfor (int l = 0; l <= lim; l++)\n\t if (dp0[i][l] >= 0 && dp1[j][lim - l] >= 0) {\n\t int sum = dp0[i][l] + dp1[j][lim - l];\n\t if (maxsum < sum) maxsum = sum;\n\t }\n\n cout << maxsum << endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 1620, "score_of_the_acc": -0.0446, "final_rank": 2 }, { "submission_id": "aoj_2285_1358203", "code_snippet": "#include <algorithm>\n#include <functional>\n#include <cmath>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <string>\n#include <sstream>\n#include <iostream>\n#include <iomanip>\n#include <vector>\n#include <list>\n#include <stack>\n#include <queue>\n#include <map>\n#include <set>\n#include <bitset>\n#include <climits>\n\n#define all(c) (c).begin(), (c).end()\n#define rep(i,n) for(int i=0;i<(n);i++)\n#define pb(e) push_back(e)\n#define mp(a, b) make_pair(a, b)\n#define fr first\n#define sc second\n\nconst int INF=100000000;\nint dx[4]={1,0,-1,0};\nint dy[4]={0,1,0,-1};\nusing namespace std;\ntypedef pair<int ,int > P;\ntypedef long long ll;\ntemplate<class T>\nbool chmax(T &a, const T &b) {\n if(a<b) {\n a=b;\n return true;\n }\n\n return false;\n}\nstruct Artist {\n int e,s;\n Artist(int e=0,int s=0) :\n e(e),s(s) {}\n};\nvoid input(int len, Artist *a) {\n rep(i,len) {\n string ss;\n int e,s;\n cin>>ss>>e>>s;\n a[i]=Artist(e,s);\n }\n}\nArtist artist1[102], artist2[102];\n// ??????????????????????????°?????????\nint dp1[102][5][1003];\nint dp2[102][102][1003];\n\nint lim,n,m,x;\n\nvoid solve() {\n rep(i,102) rep(j,5) rep(k,1003) dp1[i][j][k]=-1;\n rep(i,102) rep(j,102) rep(k,1003) dp2[i][j][k]=-1;\n\n input(n,artist1);\n input(m,artist2);\n dp1[0][0][0]=dp2[0][0][0]=0;\n\n rep(k,lim) rep(i,n) rep(j,3) {\n Artist a=artist1[i];\n int lim_n = k+a.e;\n if(lim_n <= lim&&dp1[i][j][k]!=-1) \n chmax(dp1[i+1][j+1][lim_n],dp1[i][j][k]+a.s);\n chmax(dp1[i+1][j][k],dp1[i][j][k]);\n }\n rep(k,lim) rep(i,m) rep(j,m) {\n Artist a=artist2[i];\n int lim_n = k+a.e;\n if(lim_n <= lim&&dp2[i][j][k]!=-1) \n chmax(dp2[i+1][j+1][lim_n],dp2[i][j][k]+a.s);\n chmax(dp2[i+1][j][k],dp2[i][j][k]);\n }\n\n //rep(i,20) printf(\"%d %d\\n\",i,dp1[n][2][i]);\n //rep(i,20) printf(\"%d %d\\n\",i,dp2[m][2][i]);\n\n int ans=0;\n rep(i,lim) {\n int max1=max(dp1[n][1][i],dp1[n][2][i]);\n int max2=0;\n for(int j=x;j<=m;j++) for(int k=0;k<=lim-i;k++) chmax(max2,dp2[m][j][k]);\n\n chmax(ans,max1+max2);\n // printf(\"%d %d : %d\\n\",i,lim-i,max1+max2);\n }\n\n cout<<ans<<endl;\n}\n\nint main() {\n while(cin>>lim>>n>>m>>x) {\n if(lim+n+m+x==0) break;\n solve();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 230, "memory_kb": 43968, "score_of_the_acc": -1.7686, "final_rank": 20 }, { "submission_id": "aoj_2285_1358081", "code_snippet": "#include <algorithm>\n#include <functional>\n#include <cmath>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <string>\n#include <sstream>\n#include <iostream>\n#include <iomanip>\n#include <vector>\n#include <list>\n#include <stack>\n#include <queue>\n#include <map>\n#include <set>\n#include <bitset>\n#include <climits>\n\n#define all(c) (c).begin(), (c).end()\n#define rep(i,n) for(int i=0;i<(n);i++)\n#define pb(e) push_back(e)\n#define mp(a, b) make_pair(a, b)\n#define fr first\n#define sc second\n\nconst int INF=100000000;\nint dx[4]={1,0,-1,0};\nint dy[4]={0,1,0,-1};\nusing namespace std;\ntypedef pair<int ,int > P;\ntypedef long long ll;\ntemplate<class T>\nbool chmax(T &a, const T &b) {\n if(a<b) {\n a=b;\n return true;\n }\n\n return false;\n}\nstruct Artist {\n int e,s;\n Artist(int e=0,int s=0) :\n e(e),s(s) {}\n};\nvoid input(int len, Artist *a) {\n rep(i,len) {\n string ss;\n int e,s;\n cin>>ss>>e>>s;\n a[i]=Artist(e,s);\n }\n}\nArtist artist1[102], artist2[102];\n// ??????????????????????????°?????????\nint dp1[102][5][1003];\nint dp2[102][102][1003];\n\nint lim,n,m,x;\n\nvoid solve() {\n rep(i,102) rep(j,5) rep(k,1003) dp1[i][j][k]=-1;\n rep(i,102) rep(j,102) rep(k,1003) dp2[i][j][k]=-1;\n\n input(n,artist1);\n input(m,artist2);\n dp1[0][0][0]=dp2[0][0][0]=0;\n\n rep(i,n) rep(j,3) rep(k,lim) {\n Artist a=artist1[i];\n int lim_n = k+a.e;\n if(lim_n <= lim&&dp1[i][j][k]!=-1) \n chmax(dp1[i+1][j+1][lim_n],dp1[i][j][k]+a.s);\n chmax(dp1[i+1][j][k],dp1[i][j][k]);\n }\n rep(i,m) rep(j,m) rep(k,lim) {\n Artist a=artist2[i];\n int lim_n = k+a.e;\n if(lim_n <= lim&&dp2[i][j][k]!=-1) \n chmax(dp2[i+1][j+1][lim_n],dp2[i][j][k]+a.s);\n chmax(dp2[i+1][j][k],dp2[i][j][k]);\n }\n\n //rep(i,20) printf(\"%d %d\\n\",i,dp1[n][2][i]);\n //rep(i,20) printf(\"%d %d\\n\",i,dp2[m][2][i]);\n\n int ans=0;\n rep(i,lim) {\n int max1=max(dp1[n][1][i],dp1[n][2][i]);\n int max2=0;\n for(int j=x;j<=m;j++) for(int k=0;k<=lim-i;k++) chmax(max2,dp2[m][j][k]);\n\n chmax(ans,max1+max2);\n // printf(\"%d %d : %d\\n\",i,lim-i,max1+max2);\n }\n\n cout<<ans<<endl;\n}\n\nint main() {\n while(cin>>lim>>n>>m>>x) {\n if(lim+n+m+x==0) break;\n solve();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 150, "memory_kb": 43968, "score_of_the_acc": -1.4353, "final_rank": 18 }, { "submission_id": "aoj_2285_1111626", "code_snippet": "#include<stdio.h>\n#include<algorithm>\nusing namespace std;\nchar in[100];\nint dp[110][1010];\nint val[1010];\nint sp[110];\nint sq[110];\nint np[110];\nint nq[110];\nint main(){\n\tint a,b,c,d;\n\twhile(scanf(\"%d%d%d%d\",&a,&b,&c,&d),a){\n\t\tfor(int i=0;i<b;i++){\n\t\t\tscanf(\"%s%d%d\",in,sp+i,sq+i);\n\t\t}\n\t\tfor(int i=0;i<c;i++)scanf(\"%s%d%d\",in,np+i,nq+i);\n\t\tfor(int i=0;i<110;i++)for(int j=0;j<1010;j++)dp[i][j]=-99999999;\n\t\tdp[0][0]=0;\n\t\tfor(int i=0;i<c;i++){\n\t\t\tfor(int j=i;j>=0;j--){\n\t\t\t\tfor(int k=a-np[i];k>=0;k--){\n\t\t\t\t\tdp[j+1][k+np[i]]=max(dp[j+1][k+np[i]],dp[j][k]+nq[i]);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tfor(int i=0;i<1010;i++)val[i]=-99999999;\n\t\tfor(int i=d;i<=c;i++)for(int j=0;j<=a;j++)val[j]=max(val[j],dp[i][j]);\n\t\tint ret=0;\n\t\tfor(int i=0;i<b;i++){\n\t\t\tfor(int j=0;j<=a-sp[i];j++){\n\t\t//\t\tif(sq[i]+val[j]==59)printf(\"%d %d\\n\",i,j);\n\t\t\t\tret=max(ret,sq[i]+val[j]);\n\t\t\t}\n\t\t\tfor(int j=i+1;j<b;j++){\n\t\t\t\tfor(int k=0;k<=a-sp[i]-sp[j];k++){\n\t\t\t//\t\tif(sq[i]+sq[j]+val[k]==59)printf(\"%d %d %d\\n\",i,j,k);\n\t\t\t\t\tret=max(ret,sq[i]+sq[j]+val[k]);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tprintf(\"%d\\n\",ret);\n\t}\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 1472, "score_of_the_acc": -0.0833, "final_rank": 7 }, { "submission_id": "aoj_2285_1096584", "code_snippet": "#include <cstdio>\n#include <cstring>\n#include <cstdlib>\n#include <cmath>\n#include <complex>\n#include <string>\n#include <sstream>\n#include <algorithm>\n#include <numeric>\n#include <vector>\n#include <queue>\n#include <stack>\n#include <functional>\n#include <iostream>\n#include <map>\n#include <set>\nusing namespace std;\ntypedef pair<int,int> P;\ntypedef long long ll;\ntypedef vector<int> vi;\ntypedef vector<ll> vll;\n#define pu push\n#define pb push_back\n#define mp make_pair\n#define eps 1e-9\n#define INF 2000000000\n#define sz(x) ((int)(x).size())\n#define fi first\n#define sec second\n#define SORT(x) sort((x).begin(),(x).end())\n#define all(x) (x).begin(),(x).end()\n#define EQ(a,b) (abs((a)-(b))<eps)\ntemplate<class T> void chmax(T& a,const T b){if(a<b)a=b;}\nchar name[105][40],sname[105][40];\nint se[105],ss[105],e[105],s[105];\nint dp[2][1005][3][105];\nint Limit,N,M,X;\nint main()\n{\n\twhile(1)\n\t{\n\t\tscanf(\"%d %d %d %d\",&Limit,&N,&M,&X);\n\t\tif(Limit==0&&N==0&&M==0&&X==0)break;\n\t\tfor(int i=0;i<N;i++)scanf(\"%s %d %d\",sname[i],&se[i],&ss[i]);\n\t\tfor(int i=0;i<M;i++)scanf(\"%s %d %d\",name[i],&e[i],&s[i]);\n\t\tmemset(dp,-1,sizeof(dp));\n\t\tdp[0][0][0][0]=0;\n\t\tint ans=0;\n\t\tfor(int i=0;i<N+M;i++)\n\t\t{\n\t\t\tmemset(dp[(i+1)%2],-1,sizeof(dp[(i+1)%2]));\n\t\t\tfor(int j=0;j<=Limit;j++)\n\t\t\t{\n\t\t\t\tif(i<N)\n\t\t\t\t{\n\t\t\t\t\tfor(int k=0;k<3;k++)\n\t\t\t\t\t{\n\t\t\t\t\t\tif(dp[i%2][j][k][0]==-1)continue;\n\t\t\t\t\t\tchmax(dp[(i+1)%2][j][k][0],dp[i%2][j][k][0]);\n\t\t\t\t\t\tif(j+se[i]>Limit||k>=2)continue;\n\t\t\t\t\t\tchmax(dp[(i+1)%2][j+se[i]][k+1][0],dp[i%2][j][k][0]+ss[i]);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tint idx=i-N;\n\t\t\t\t\tfor(int k=1;k<3;k++)\n\t\t\t\t\t{\n\t\t\t\t\t\tfor(int l=0;l<=M;l++)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tif(dp[i%2][j][k][l]==-1)continue;\n\t\t\t\t\t\t\tchmax(dp[(i+1)%2][j][k][l],dp[i%2][j][k][l]);\n\t\t\t\t\t\t\tif(j+e[idx]>Limit||l>=M)continue;\n\t\t\t\t\t\t\tchmax(dp[(i+1)%2][j+e[idx]][k][l+1],dp[i%2][j][k][l]+s[idx]);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tfor(int j=0;j<=Limit;j++)\n\t\t{\n\t\t\tfor(int k=1;k<3;k++)\n\t\t\t{\n\t\t\t\tfor(int l=X;l<=M;l++)\n\t\t\t\t{\n\t\t\t\t\tchmax(ans,dp[(N+M)%2][j][k][l]);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tprintf(\"%d\\n\",ans);\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 150, "memory_kb": 3680, "score_of_the_acc": -0.6276, "final_rank": 13 }, { "submission_id": "aoj_2285_1008804", "code_snippet": "#define _USE_MATH_DEFINES\n#define INF 0x3f3f3f3f\n#include <cstdio>\n#include <iostream>\n#include <sstream>\n#include <cmath>\n#include <cstdlib>\n#include <algorithm>\n#include <queue>\n#include <stack>\n#include <limits>\n#include <map>\n#include <string>\n#include <cstring>\n#include <set>\n#include <deque>\n#include <bitset>\n#include <list>\n#include <cctype>\n#include <utility>\n \nusing namespace std;\n \ntypedef long long ll;\ntypedef pair <int,int> P;\ntypedef pair <int,P > PP;\n \nstatic const int tx[] = {0,1,0,-1};\nstatic const int ty[] = {-1,0,1,0};\n \nstatic const double EPS = 1e-8;\n\n// dp[picked secret][picked standard][money] ::= max satisfaction\nint dp[3][105][1005]; \n\nint main(){\n int money_limit;\n int total_secret_artists;\n int total_standard_artists;\n int to_pick_standard_artists;\n while(~scanf(\"%d %d %d %d\",\n &money_limit,\n &total_secret_artists,\n &total_standard_artists,\n &to_pick_standard_artists)){\n \n if(money_limit == 0\n && total_secret_artists == 0\n && total_standard_artists == 0\n && to_pick_standard_artists == 0) break;\n\n memset(dp,-1,sizeof(dp));\n dp[0][0][0] = 0;\n for(int artist_idx=0;artist_idx<total_secret_artists;artist_idx++){\n string name;\n int cost;\n int satisfaction;\n\n cin >> name >> cost >> satisfaction;\n for(int prev_picked=1;prev_picked>=0;prev_picked--){\n for(int prev_money=1000;prev_money>=0;prev_money--){\n if(prev_money + cost > 1000) continue;\n\n if(dp[prev_picked][0][prev_money] == -1) continue;\n dp[prev_picked + 1][0][prev_money + cost]\n = max(dp[prev_picked][0][prev_money] + satisfaction,\n dp[prev_picked + 1][0][prev_money + cost]);\n }\n }\n }\n for(int artist_idx=0;artist_idx<total_standard_artists;artist_idx++){\n string name;\n int cost;\n int satisfaction;\n cin >> name >> cost >> satisfaction;\n for(int picked_secret=min(2,total_secret_artists);picked_secret>=1;picked_secret--){\n for(int prev_picked_standard = artist_idx; prev_picked_standard >= 0; prev_picked_standard--){\n for(int prev_money=1000;prev_money>=0;prev_money--){\n if(prev_money + cost > 1000) continue;\n\n if(dp[picked_secret][prev_picked_standard][prev_money] == -1) continue;\n dp[picked_secret][prev_picked_standard + 1][prev_money + cost]\n = max(dp[picked_secret][prev_picked_standard + 1][prev_money + cost],\n dp[picked_secret][prev_picked_standard][prev_money] + satisfaction);\n }\n\n }\n } \n }\n\n int res = 0;\n for(int picked_secret=min(2,total_secret_artists);picked_secret>=1;picked_secret--){\n for(int picked_standard = total_standard_artists; picked_standard >=to_pick_standard_artists; picked_standard--){\n for(int money=money_limit;money>=0;money--){\n res = max(dp[picked_secret][picked_standard][money],res);\n }\n }\n }\n\n printf(\"%d\\n\",res);\n }\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 2496, "score_of_the_acc": -0.1455, "final_rank": 11 }, { "submission_id": "aoj_2285_904780", "code_snippet": "#include <iostream>\n#include <vector>\n#include <string>\n#include <stack>\n#include <queue>\n#include <deque>\n#include <set>\n#include <map>\n#include <algorithm>\t// require sort next_permutation count __gcd reverse etc.\n#include <cstdlib>\t// require abs exit atof atoi \n#include <cstdio>\t\t// require scanf printf\n#include <functional>\n#include <numeric>\t// require accumulate\n#include <cmath>\t\t// require fabs\n#include <climits>\n#include <limits>\n#include <cfloat>\n#include <iomanip>\t// require setw\n#include <sstream>\t// require stringstream \n#include <cstring>\t// require memset\n#include <cctype>\t\t// require tolower, toupper\n#include <fstream>\t// require freopen\n#include <ctime>\t\t// require srand\n#define rep(i,n) for(int i=0;i<(n);i++)\n#define ALL(A) A.begin(), A.end()\n#define INF 1<<29\n#define DEBUG 0\n/*\n\tRitsumeikan University Programming Contest 2011 , Japan, 2011-10-05\n\n\t2285 - Anipero\n\n\tAnipero01.cpp\n\tまずは全探索.\n\n\tAnipero02.cpp\n\tメモ化再帰\n\n\tAnipero03.cpp\n\tDPに書き直す\n\n\tAnipero04.cpp\n\tループの向きを反対にする\n\n*/\nusing namespace std;\n\ntypedef long long ll;\ntypedef pair<int, int> P;\n\nconst int MAX_LIMIT = 1000;\nconst int MAX_N = 100;\nconst int MAX_M = 100;\n\nint LIMIT, N, M, X;\n\nint sec_e[MAX_N+1], sec_s[MAX_N+1], e[MAX_M+1], s[MAX_M+1];\n\nint dp[MAX_M+1][MAX_LIMIT+1];\n/*\nint dfs (int depth, int cnt, int cost ){\n\tif (cost > LIMIT ) return -INF;\n\tif (depth >= M ){\n\t\treturn 0;\n\t} // end if\n\tif (memo[depth][cnt][cost] != -1 ) return memo[depth][cnt][cost];\n\n\tint res = -INF;\n\tres = max (res, dfs (depth + 1, cnt, cost ) );\n\tres = max (res, dfs (depth + 1, cnt + 1, cost + e[depth] ) + s[depth] );\n\n\treturn memo[depth][cnt][cost] = res;\n}\n*/\n\nint solve (void ){\n\trep (j, MAX_M+1 ) rep (k, MAX_LIMIT + 1 ) dp[j][k] = -INF;\n\tdp[0][0] = 0;\n\n\trep (i, M ){\n\t\tfor (int j = i; j >= 0; j-- ){\n\t\t\tfor (int k = 0; k <= LIMIT; k++ ){\n\t\t\t\tif (k + e[i] <= LIMIT ){\n\t\t\t\t\tdp[j+1][k+e[i]] = max (dp[j+1][k+e[i]], dp[j][k]+s[i] );\n\t\t\t\t\tdp[j][k+e[i]] = max (dp[j][k+e[i]], dp[j][k] );\n\t\t\t\t} // end if\n\t\t\t} // end for\n\t\t} // end for\n\t} // end for\n\n#if DEBUG\n\trep (i, M+1 ){\n\t\tfor (int j = 0; j <= LIMIT; j++ ){\n\t\t\tcerr << (dp[i][j] <= -INF/2 ? -1 : dp[i][j] ) << (j != LIMIT ? ' ' : '\\n' );\n\t\t} // end for\n\t} // end rep\n#endif\n\n\tint res = 0;\n\tfor (int j = X; j <= M; j++ )\n\trep (k, LIMIT+1 ){\n\t\tif (dp[j][k] >= 0 ){\n\t\t\tint curr = -INF;\n\t\t\trep (ii, N ){\n\t\t\t\tif (k + sec_e[ii] <= LIMIT )\n\t\t\t\t\tcurr = max (curr, sec_s[ii] );\n\t\t\t\trep (jj, ii )\n\t\t\t\t\tif (k + sec_e[ii] + sec_e[jj] <= LIMIT )\n\t\t\t\t\t\tcurr = max (curr, sec_s[ii] + sec_s[jj] );\n\t\t\t} // end rep\n\t\t\tres = max (res, dp[j][k] + curr );\n\t\t} // end if\n\t} // end rep\n\t\n\treturn res;\n}\n\nint main()\n{\n\tios_base::sync_with_stdio(0);\n\twhile (cin >> LIMIT >> N >> M >> X, LIMIT ){\n\t\tmemset (sec_e, 0, sizeof (sec_e ) );\n\t\tmemset (sec_s, 0, sizeof (sec_s ) );\n\t\tmemset (e, 0, sizeof (e ) );\n\t\tmemset (s, 0, sizeof (s ) );\n\t\tstring str;\n\t\trep (i, N ) cin >> str >> sec_e[i] >> sec_s[i];\n\t\trep (j, M ) cin >> str >> e[j] >> s[j];\n\t\tint res = solve() ;\n\t\tcout << res << endl;\n\t} // end while\t\n\t\t\n\treturn 0;\n}", "accuracy": 1, "time_ms": 250, "memory_kb": 1612, "score_of_the_acc": -1.0028, "final_rank": 14 }, { "submission_id": "aoj_2285_893162", "code_snippet": "#include<bits/stdc++.h>\n\n#define REP(i,s,n) for(int i=s;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n#define IINF (INT_MAX)\n#define MAX_LIMIT 1100\n#define MAX 110\n\nusing namespace std;\n\nint pre[MAX_LIMIT][3],pre2[MAX_LIMIT][MAX],LIMIT,N,M,X,SEC_E[MAX],SEC_S[MAX],E[MAX],S[MAX];\nstring tmp;\n\n\nint main(){\n while(cin >> LIMIT >> N >> M >> X, LIMIT|N|M|X){\n rep(i,N)cin >> tmp >> SEC_E[i] >> SEC_S[i];\n rep(i,M)cin >> tmp >> E[i] >> S[i];\n rep(i,LIMIT+1)rep(j,3)pre[i][j] = -1;\n rep(i,LIMIT+1)rep(j,MAX)pre2[i][j] = -1;\n pre[0][0] = pre2[0][0] = 0;\n\n rep(i,N)for(int j=1;j>=0;j--)rep(k,LIMIT+1){\n\tif( pre[k][j] == -1 ) continue;\n\tint total_money = k + SEC_E[i];\n\tif( total_money > LIMIT ) continue;\n\tpre[total_money][j+1] = max(pre[total_money][j+1],pre[k][j]+SEC_S[i]);\n }\n\n rep(i,3)rep(j,LIMIT+1)if( j ) pre[j][i] = max(pre[j][i],pre[j-1][i]);\n\n rep(i,M)for(int j=M-1;j>=0;j--)rep(k,LIMIT+1){\n\tif( pre2[k][j] == -1 ) continue;\n\tint total_money = k + E[i];\n\tif( total_money > LIMIT ) continue;\n\tpre2[total_money][j+1] = max(pre2[total_money][j+1],pre2[k][j]+S[i]);\n }\n\n rep(i,M+1)rep(j,LIMIT+1)if( j ) pre2[j][i] = max(pre2[j][i],pre2[j-1][i]);\n\n int ans = 0;\n\n REP(i,X,M+1)REP(j,1,3)rep(k,LIMIT+1){\n if( pre2[k][i] == -1 || pre[LIMIT-k][j] == -1 ) continue;\n int points = pre2[k][i] + pre[LIMIT-k][j];\n ans = max(ans,points);\n }\n\n cout << ans << endl;\n\n }\n return 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 1648, "score_of_the_acc": -0.0869, "final_rank": 9 } ]
aoj_2288_cpp
Problem H: Oh, My Goat! 太郎君は、次郎君に古文のノートを借りた。 明日の古文の宿題を写させてもらうためだ。 太郎君は、用心深い性格をしているため、万が一のことを考えて、近所のコンビニで次郎君のノートのコピーを取って自宅へ帰った。 太郎君は、自宅で宿題を始めようとしたときに、慌てた。 次郎君から借りたノートが鞄に入っていなかったからだ。 どうやら、コンビニに忘れてきてしまったようだ。 太郎君は、急いでコンビニに引き返した。 コピー機とその周辺を懸命に捜索したが、ノートを見つけることはできなかった。 困り果てた太郎君は、コンビニの店長に助けを求めた。 余談だが、店長は地球環境に優しい人で、白ヤギさんを飼っている。 店長は、地球環境に気を配る心遣いを持っているため、常日頃から、ペットの白ヤギさんに、餌としてコピー機周辺の客の忘れものを与えている。 店長によると、ノートは、今晩の白ヤギさんの晩御飯であり、白ヤギさんは現在食事中だそうである。 太郎君は白ヤギさんに頼みこみ、食べかけのノートを取り返すことはできたが、ノートはビリビリに破れぼろぼろである。 太郎君は、急いで自宅に引き返した。 自室の机とその周辺を懸命に捜索したが、ノートのコピーを見つけることはできなかった。 困り果てた太郎君は、お母さんに助けを求めた。 余談だが、太郎君のお母さんは地球環境に優しい人で、黒ヤギさんを飼っている。 太郎君のお母さんは、地球環境に気を配る心遣いを持っているため、常日頃から、ペットの黒ヤギさんに、餌として太郎君の宿題を与えている。 お母さんによると、ノートのコピーは、今晩の黒ヤギさんの晩御飯であり、黒ヤギさんは現在食事中だそうである。 太郎君は黒ヤギさんに頼みこみ、食べかけのノートを取り返すことはできたが、ノートのコピーはビリビリに破れぼろぼろである。 次郎君のノートとそのコピーをビリビリのぼろぼろにされてしまった太郎君は、ノートを可能な限り復元しようと考えた。 そこで、繋がりそうなノートの切れ端同士を繋ぎ合わせて、「繋ぎ合せたノート」を作成した。 同様に、繋がりそうなノートのコピーの切れ端同士を繋き合わせて、「繋ぎ合せたノートのコピー」を作成した。 太郎君は、あらゆる可能性を考えるために、繋がりそうな切れ端同士をつなぎ合わせた。 このため、1つの切れ目に2つ以上の切れ端が繋がれてしまった箇所もあった。 繋ぎ合わせ方の例を図1に示す。 図1は、7つの切れ端と6つの切れ目からなる。 図1 切れ端"haru"と"natsu"に続く切れ目には、切れ端"wa"が繋がり、文章が合流している。 切れ端"wa"に続く切れ目には、切れ端"ake"と"saron"が繋がり、文章が分岐している。 このような合流や切れ端があるため、図1は次のような4つの文章を表す。 haruwaakebono haruwasaronpusu natsuwaakebono natsuwasaronpusu ノートやノートのコピーは、白ヤギさんや黒ヤギさんが、食べてしまったため、復元できない部分があったり、彼らのご飯に混ざっていた関係のない書類の切れ端が混在していることもある。 太郎君の代わりに、「繋ぎ合わせたノート」と「繋ぎ合わせたノートのコピー」の2つを入力として受け取って、両方に共通して現れる文章の数を求めるプログラムを作成せよ。 Input 入力の1行目では、データセットの数 T ( 1 ≤ T ≤ 100 )が与えられる。 データセットは、「繋ぎ合せたノート」の情報と、「繋ぎ合せたノートのコピー」の情報から成る。 「繋ぎ合せたノート」の情報として、 1行目に n ( 2 ≤ n ≤ 500 )と m ( 1 ≤ m ≤ 600 )が与えられる。 n は、切れ端と切れ端を繋ぎ合せる切れ目の数である。 続く m 行で繋ぎ合せ方が与えられる。 各行は、切れ端の上端に繋がる切れ目の番号 a 、切れ端の下端につながる切れ目の番号 b 、切れ端を表す文字列 s からなる。 ただし、 0 ≤ a < b < n が満たされる。 s は長さが1以上5以下の文字列で、アルファベットの小文字のみから成る。 「繋ぎ合せたノートのコピー」も「繋ぎ合せたノート」と同じ形式で与えられる。 なお、0番目の切れ目から繋ぎ合せ情報を辿り、 n-1 番目の切れ目に到達する過程で得られる文字列を連結したもののみを文章と呼ぶものとする。 また、それぞれの繋ぎ合せの中で同じ文章が2つ以上出現することはない。 Output それぞれのデータセット毎に、答えの整数を1,000,000,007で割った余りを1行で出力せよ。 Sample Input 4 2 1 0 1 a 2 2 0 1 a 0 1 b 5 5 0 1 a 0 2 b 1 3 a 2 3 c 3 4 e 3 3 0 1 aa 0 2 bce 1 2 a 2 4 0 1 abc 0 1 def 0 1 ghi 0 1 jkl 2 4 0 1 ghi 0 1 jkl 0 1 mno 0 1 pqr 6 7 0 1 haru 0 1 natsu 1 2 wa 2 3 ake 2 4 saron 3 5 bono 4 5 pusu 5 4 0 1 haruw 1 2 aakeb 2 3 o 3 4 no Output for Sample Input 1 1 2 1
[ { "submission_id": "aoj_2288_10339676", "code_snippet": "// AOJ #2288 Oh, My Goat!\n// 2025.3.31\n\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nconst int MOD = 1000000007;\n\ntypedef vector<vector<pair<char,int>>> Automaton;\n\npair<Automaton, int> buildAutomaton(int n, int m, vector<tuple<int,int,string>> &edges) {\n int stateCount = n;\n Automaton aut;\n aut.resize(n + m*5);\n\n for(auto &edge : edges) {\n int u, v;\n string s;\n tie(u, v, s) = edge;\n int cur = u;\n for (int i = 0; i < (int)s.size(); i++) {\n char c = s[i];\n if(i == (int)s.size()-1) aut[cur].push_back({c, v});\n else {\n aut[cur].push_back({c, stateCount});\n cur = stateCount;\n stateCount++;\n }\n }\n }\n aut.resize(stateCount);\n return {aut, n-1};\n}\n\nll dfs(int p, int q, const Automaton &aut1, const Automaton &aut2, int F1, int F2, int tot2, vector<ll> &dp) {\n int idx = p * tot2 + q;\n if(dp[idx] != -1) return dp[idx];\n ll ways = 0;\n if(p == F1 && q == F2) ways = 1;\n for(auto &edge1 : aut1[p]){\n char c = edge1.first;\n int np = edge1.second;\n for(auto &edge2 : aut2[q]){\n if(edge2.first == c){\n int nq = edge2.second;\n ways = (ways + dfs(np, nq, aut1, aut2, F1, F2, tot2, dp)) % MOD;\n }\n }\n }\n dp[idx] = ways;\n return ways;\n}\n\nint main(){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n\n int T;\n cin >> T;\n while(T--){\n int n1, m1;\n cin >> n1 >> m1;\n vector<tuple<int,int,string>> edges1;\n for (int i = 0; i < m1; i++){\n int a, b;\n string s;\n cin >> a >> b >> s;\n edges1.push_back({a, b, s});\n }\n auto res1 = buildAutomaton(n1, m1, edges1);\n Automaton aut1 = res1.first;\n int F1 = res1.second;\n\n int n2, m2;\n cin >> n2 >> m2;\n vector<tuple<int,int,string>> edges2;\n for (int i = 0; i < m2; i++){\n int a, b;\n string s;\n cin >> a >> b >> s;\n edges2.push_back({a, b, s});\n }\n auto res2 = buildAutomaton(n2, m2, edges2);\n Automaton aut2 = res2.first;\n int F2 = res2.second;\n\n int tot1 = aut1.size();\n int tot2 = aut2.size();\n\n vector<ll> dp(tot1 * tot2, -1);\n\n ll ans = dfs(0, 0, aut1, aut2, F1, F2, tot2, dp);\n cout << ans<< endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 56788, "score_of_the_acc": -0.5387, "final_rank": 11 }, { "submission_id": "aoj_2288_4371957", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 99999999999999999\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n\n#define SIZE 605\nstruct Edge{\n\n\tint from,to;\n\tchar buf[6];\n\tint len;\n};\n\nint N1,N2,M1,M2;\nll dp[SIZE][SIZE][6][6];\nEdge edge1[SIZE],edge2[SIZE];\nvector<int> G1[SIZE],G2[SIZE];\n\nll recursive(int e1,int loc1,int e2,int loc2){\n\n\tif(dp[e1][e2][loc1][loc2] != -1){\n\n\t\treturn dp[e1][e2][loc1][loc2];\n\t}\n\n\tif(edge1[e1].buf[loc1] != edge2[e2].buf[loc2]){\n\n\t\treturn dp[e1][e2][loc1][loc2] = 0;\n\t}\n\n\tll ret = 0;\n\n\tif(loc1+1 < edge1[e1].len && loc2+1 < edge2[e2].len){ //両方非終端\n\n\t\tret = recursive(e1,loc1+1,e2,loc2+1);\n\n\t}else if(loc1+1 == edge1[e1].len && loc2+1 < edge2[e2].len){ //DAG1が終端\n\n\t\tif(edge1[e1].to == N1-1){\n\n\t\t\tret = 0;\n\n\t\t}else{ //DAG1が新しいノードへ\n\n\t\t\tfor(int i = 0; i < G1[edge1[e1].to].size(); i++){\n\n\t\t\t\tret += recursive(G1[edge1[e1].to][i],0,e2,loc2+1);\n\t\t\t\tret %= MOD;\n\t\t\t}\n\t\t}\n\n\t}else if(loc1+1 < edge1[e1].len && loc2+1 == edge2[e2].len){ //DAG2が終端\n\n\t\tif(edge2[e2].to == N2-1){\n\n\t\t\tret = 0;\n\n\t\t}else{ //DAG2が新しいノードへ\n\n\t\t\tfor(int i = 0; i < G2[edge2[e2].to].size(); i++){\n\n\t\t\t\tret += recursive(e1,loc1+1,G2[edge2[e2].to][i],0);\n\t\t\t\tret %= MOD;\n\t\t\t}\n\t\t}\n\n\t}else{ //両方終端\n\n\t\tif((edge1[e1].to == N1-1 && edge2[e2].to != N2-1) ||\n\t\t\t\t(edge1[e1].to != N1-1 && edge2[e2].to == N2-1)){ //片方だけ文章完成\n\n\t\t\tret = 0;\n\n\t\t}else if(edge1[e1].to == N1-1 && edge2[e2].to == N2-1){ //両方文章完成\n\n\t\t\tret = 1;\n\n\t\t}else{ //両方新しいノードへ\n\n\t\t\tfor(int i = 0; i < G1[edge1[e1].to].size(); i++){\n\t\t\t\tfor(int k = 0; k < G2[edge2[e2].to].size(); k++){\n\n\t\t\t\t\tret += recursive(G1[edge1[e1].to][i],0,G2[edge2[e2].to][k],0);\n\t\t\t\t\tret %= MOD;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\treturn dp[e1][e2][loc1][loc2] = ret;\n}\n\n\nvoid func(){\n\n\tfor(int i = 0; i < SIZE; i++){\n\t\tG1[i].clear();\n\t\tG2[i].clear();\n\t}\n\n\tscanf(\"%d %d\",&N1,&M1);\n\n\tfor(int i = 0; i < M1; i++){\n\n\t\tscanf(\"%d %d %s\",&edge1[i].from,&edge1[i].to,edge1[i].buf);\n\t\tfor(edge1[i].len = 0; edge1[i].buf[edge1[i].len] != '\\0'; edge1[i].len++);\n\n\t\tG1[edge1[i].from].push_back(i);\n\t}\n\n\tscanf(\"%d %d\",&N2,&M2);\n\n\tfor(int i = 0; i < M2; i++){\n\n\t\tscanf(\"%d %d %s\",&edge2[i].from,&edge2[i].to,edge2[i].buf);\n\t\tfor(edge2[i].len = 0; edge2[i].buf[edge2[i].len] != '\\0'; edge2[i].len++);\n\n\t\tG2[edge2[i].from].push_back(i);\n\t}\n\n\tfor(int i = 0; i < M1; i++){\n\t\tfor(int k = 0; k < M2; k++){\n\t\t\tfor(int a = 0; a < 6; a++){\n\t\t\t\tfor(int b = 0; b < 6; b++){\n\t\t\t\t\tdp[i][k][a][b] = -1;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tll ans = 0;\n\tint root = 0;\n\n\tfor(int i = 0; i < G1[root].size(); i++){\n\t\tfor(int k = 0; k < G2[root].size(); k++){\n\n\t\t\tans += recursive(G1[root][i],0,G2[root][k],0);\n\t\t\tans %= MOD;\n\t\t}\n\t}\n\n\tprintf(\"%lld\\n\",ans);\n}\n\nint main(){\n\n\tint num_case;\n\tscanf(\"%d\",&num_case);\n\n\tfor(int loop = 0; loop < num_case; loop++){\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 110, "memory_kb": 105420, "score_of_the_acc": -1.1754, "final_rank": 14 }, { "submission_id": "aoj_2288_365380", "code_snippet": "#include <iostream> \n#include <sstream> \n#include <iomanip> \n#include <algorithm> \n#include <cmath> \n#include <string> \n#include <vector> \n#include <list> \n#include <queue> \n#include <stack> \n#include <set> \n#include <map> \n#include <bitset> \n#include <numeric> \n#include <climits> \n#include <cfloat> \nusing namespace std;\n\nconst int MOD = 1000000007;\n\nvoid input(int &n, int &m, vector<vector<int> >& to, vector<vector<string> >& str)\n{\n cin >> n >> m;\n to.assign(n, vector<int>());\n str.assign(n, vector<string>());\n\n for(int i=0; i<m; ++i){\n int a, b;\n string s;\n cin >> a >> b >> s;\n to[a].push_back(b);\n str[a].push_back(s);\n }\n}\n\nbool compare(string& s, string& t)\n{\n int n = min(s.size(), t.size());\n if(s.substr(0, n) != t.substr(0, n))\n return false;\n\n s = s.substr(n);\n t = t.substr(n);\n return true;\n}\n\nint main()\n{\n int t;\n cin >> t;\n\n while(--t >= 0){\n int n1, m1, n2, m2;\n vector<vector<int> > to1, to2;\n vector<vector<string> > str1, str2;\n input(n1, m1, to1, str1);\n input(n2, m2, to2, str2);\n\n vector<vector<vector<map<string, int> > > > dp(n1, vector<vector<map<string, int> > >(n2, vector<map<string, int> >(2)));\n dp[0][0][0][\"\"] = 1;\n\n for(int i=0; i<n1; ++i){\n for(int j=0; j<n2; ++j){\n if(i == n1-1 && j == n2-1)\n break;\n\n while(!dp[i][j][0].empty()){\n map<string, int>::iterator it = dp[i][j][0].begin();\n string s0 = it->first;\n int num = it->second;\n dp[i][j][0].erase(it);\n\n for(unsigned k=0; k<to1[i].size(); ++k){\n int to = to1[i][k];\n string s = s0;\n string t = str1[i][k];\n if(compare(s, t)){\n if(t.size() == 0){\n dp[to][j][0][s] += num;\n dp[to][j][0][s] %= MOD;\n }else{\n dp[to][j][1][t] += num;\n dp[to][j][1][t] %= MOD;\n }\n }\n }\n }\n\n while(!dp[i][j][1].empty()){\n map<string, int>::iterator it = dp[i][j][1].begin();\n string s0 = it->first;\n int num = it->second;\n dp[i][j][1].erase(it);\n\n for(unsigned k=0; k<to2[j].size(); ++k){\n int to = to2[j][k];\n string s = s0;\n string t = str2[j][k];\n if(compare(s, t)){\n if(s.size() == 0){\n dp[i][to][0][t] += num;\n dp[i][to][0][t] %= MOD;\n }else{\n dp[i][to][1][s] += num;\n dp[i][to][1][s] %= MOD;\n }\n }\n }\n }\n }\n }\n\n cout << dp[n1-1][n2-1][0][\"\"] << endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 150, "memory_kb": 17000, "score_of_the_acc": -0.4069, "final_rank": 10 }, { "submission_id": "aoj_2288_364629", "code_snippet": "#include <iostream>\n#include <iomanip>\n#include <sstream>\n#include <cstdio>\n#include <string>\n#include <vector>\n#include <algorithm>\n#include <complex>\n#include <cstring>\n#include <cstdlib>\n#include <cmath>\n#include <cassert>\n#include <climits>\n#include <queue>\n#include <set>\n#include <map>\n#include <valarray>\n#include <bitset>\n#include <stack>\nusing namespace std;\n\n#define REP(i,n) for(int i=0;i<(int)n;++i)\n#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)\n#define ALL(c) (c).begin(), (c).end()\ntypedef long long ll;\ntypedef pair<int,int> pii;\nconst int INF = 1<<29;\nconst double PI = acos(-1);\nconst double EPS = 1e-8;\n\nconst int MOD = 1e9+7;\n\nstring str1[600];\nstring str2[600];\nvector<int> g1[600], g2[600];\nbool goal1[600], goal2[600];\nint A[600], B[600];\nint n1, m1;\nint n2, m2;\n\nint memo[600][6][600];\n\nint solve(int p1, int q1, int p2, int q2) {\n //cout << p1 << \" \" << q1 << \" \" << p2 << \" \" << q2 << endl;\n if (q2 == 0 && memo[p1][q1][p2] >= 0) return memo[p1][q1][p2];\n if (!goal1[p1] && q1 == str1[p1].size()) {\n ll res = 0;\n FOR(it, g1[p1])\n res = (res + solve(*it, 0, p2, q2)) % MOD;\n if (q2 == 0) memo[p1][q1][p2] = res;\n return res;\n }\n if (!goal2[p2] && q2 == str2[p2].size()) {\n ll res = 0;\n FOR(it, g2[p2])\n res = (res + solve(p1, q1, *it, 0)) % MOD;\n return res;\n }\n bool f1 = (goal1[p1] && q1 == str1[p1].size());\n bool f2 = (goal2[p2] && q2 == str2[p2].size());\n int res;\n if (f1 || f2) {\n res = f1 && f2;\n } else {\n if (str1[p1][q1] == str2[p2][q2]) {\n res = solve(p1, q1+1, p2, q2+1);\n } else {\n res = 0;\n }\n }\n if (q2 == 0) memo[p1][q1][p2] = res;\n return res;\n}\n\nint main() {\n int T;\n cin >> T;\n while(T--) {\n cin >> n1 >> m1;\n REP(i, m1) g1[i].clear();\n memset(goal1, 0, sizeof(goal1));\n vector<int> start1;\n REP(i, m1) {\n cin >> A[i] >> B[i] >> str1[i];\n if (A[i] == 0) start1.push_back(i);\n if (B[i] == n1-1) goal1[i] = 1;\n REP(j, i) {\n if (B[j] == A[i]) {\n g1[j].push_back(i);\n }\n }\n }\n cin >> n2 >> m2;\n REP(i, m2) g2[i].clear();\n memset(goal2, 0, sizeof(goal2));\n vector<int> start2;\n REP(i, m2) {\n cin >> A[i] >> B[i] >> str2[i];\n if (A[i] == 0) start2.push_back(i);\n if (B[i] == n2-1) goal2[i] = 1;\n REP(j, i) {\n if (B[j] == A[i]) {\n g2[j].push_back(i);\n }\n }\n }\n ll res = 0;\n memset(memo,-1,sizeof(memo));\n FOR(it, start1) {\n FOR(jt, start2) {\n res = (res + solve(*it,0,*jt,0)) % MOD;\n }\n }\n cout << res << endl;\n }\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 9688, "score_of_the_acc": -0.1972, "final_rank": 8 }, { "submission_id": "aoj_2288_329212", "code_snippet": "#include<string>\n#include<vector>\n#include<cstring>\n#include<iostream>\n\n#define rep(i,n) for(int i=0;i<(n);i++)\n\nusing namespace std;\n\nconst int M=1000000007;\n\nstruct edge{\n\tint u,v,id;\n\tstring s;\n};\n\nint n1,n2;\nedge E1[600],E2[600];\nvector<int> adj1[500],adj2[500];\n\nint dp[600][600][6];\nint dfs(int i1,int i2,int p1,int p2){\n\tint dummy,&res=(p1==0?dp[i1][i2][p2]:dummy);\n\tif(p1==0 && ~res) return res;\n\n\t// 2 ‚‚̃|ƒCƒ“ƒ^‚ª“¯Žž‚É DAG ‚̏I’[‚É—ˆ‚½\n\tif(p1==E1[i1].s.length() && p2==E2[i2].s.length()){\n\t\tint v1=E1[i1].v,v2=E2[i2].v;\n\t\tif(v1==n1-1 && v2==n2-1) return res=1;\n\t}\n\n\t// ƒ|ƒCƒ“ƒ^ 1 ‚ª•¶Žš—ñI’[‚É—ˆ‚½\n\tif(p1==E1[i1].s.length()){\n\t\tint v=E1[i1].v,cnt=0;\n\t\trep(k,adj1[v].size()) cnt=(cnt+dfs(adj1[v][k],i2,0,p2))%M;\n\t\treturn res=cnt;\n\t}\n\n\t// ƒ|ƒCƒ“ƒ^ 2 ‚ª•¶Žš—ñI’[‚É—ˆ‚½\n\tif(p2==E2[i2].s.length()){\n\t\tint v=E2[i2].v,cnt=0;\n\t\trep(k,adj2[v].size()) cnt=(cnt+dfs(i1,adj2[v][k],p1,0))%M;\n\t\treturn res=cnt;\n\t}\n\n\t// Žw‚µ‚Ä‚¢‚é•¶Žš‚ªˆá‚¤‚È‚ç’Tõ‘Å‚¿Ø‚è\n\tif(E1[i1].s[p1]!=E2[i2].s[p2]) return res=0;\n\n\treturn res=dfs(i1,i2,p1+1,p2+1);\n}\n\nint main(){\n\tint T; cin>>T;\n\twhile(T--){\n\t\tmemset(dp,-1,sizeof dp);\n\t\tint m1; cin>>n1>>m1;\n\t\trep(u,n1) adj1[u].clear();\n\t\trep(j,m1){\n\t\t\tint u,v;\n\t\t\tstring s; cin>>u>>v>>s;\n\t\t\tadj1[u].push_back(j);\n\t\t\tE1[j]=(edge){u,v,j,s};\n\t\t}\n\t\tint m2; cin>>n2>>m2;\n\t\trep(u,n2) adj2[u].clear();\n\t\trep(j,m2){\n\t\t\tint u,v;\n\t\t\tstring s; cin>>u>>v>>s;\n\t\t\tadj2[u].push_back(j);\n\t\t\tE2[j]=(edge){u,v,j,s};\n\t\t}\n\n\t\tint ans=0;\n\t\trep(i,adj1[0].size()) rep(j,adj2[0].size()) {\n\t\t\tans=(ans+dfs(adj1[0][i],adj2[0][j],0,0))%M;\n\t\t}\n\t\tprintf(\"%d\\n\",ans);\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 9584, "score_of_the_acc": -0.1786, "final_rank": 5 }, { "submission_id": "aoj_2288_294086", "code_snippet": "#include <iostream>\n#include <fstream>\n#include <algorithm>\n#include <bitset>\n#include <cassert>\n#include <cctype>\n#include <cmath>\n#include <complex>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <deque>\n#include <iomanip>\n#include <map>\n#include <numeric>\n#include <queue>\n#include <set>\n#include <stack>\n#include <sstream>\n#include <string>\n#include <vector>\nusing namespace std;\n\n#define EPS 1e-9\n#define INF MOD\n#define MOD 1000000007LL\n#define fir first\n#define iss istringstream\n#define sst stringstream\n#define ite iterator\n#define ll long long\n#define mp make_pair\n#define rep(i,n) rep2(i,0,n)\n#define rep2(i,m,n) for(int i=m;i<n;i++)\n#define pi pair<int,int>\n#define pb push_back\n#define sec second\n#define sh(i) (1LL<<i)\n#define sz size()\n#define vi vector<int>\n#define vc vector\n#define vl vector<ll>\n#define vs vector<string>\n\nint T,n,m,N,M,a,b;\nmap<pi,int> dp;\nstring s;\nstruct E{int from;char c;E(int a,char b):from(a),c(b){}};\nvc<E> e[3010],f[3010];\n\nint F(int a,int b){\n\tif(dp.find(pi(a,b))!=dp.end())return dp[pi(a,b)];\n\tint& res=dp[pi(a,b)];\n\tif(!a&&!b)return res=1;\n\trep(i,e[a].sz)rep(j,f[b].sz)if(e[a][i].c==f[b][j].c){\n\t\tres=(res+F(e[a][i].from,f[b][j].from))%MOD;\n\t}\n\treturn res;\n}\n\nint main(){\n\tcin>>T;\n\trep(tc,T){\n\t\trep(i,3010)e[i].clear(),f[i].clear();\n\t\tcin>>n>>m;\n\t\tint cur=n;\n\t\trep(i,m){\n\t\t\tcin>>a>>b>>s;\n\t\t\tif(s.sz==1)e[b].pb(E(a,s[0]));\n\t\t\telse rep(j,s.sz){\n\t\t\t\tif(j==0)e[cur].pb(E(a,s[j])),cur++;\n\t\t\t\telse if(j==s.sz-1)e[b].pb(E(cur-1,s[j]));\n\t\t\t\telse e[cur].pb(E(cur-1,s[j])),cur++;\n\t\t\t}\n\t\t}\n\t\tcin>>N>>M;\n\t\tcur=N;\n\t\trep(i,M){\n\t\t\tcin>>a>>b>>s;\n\t\t\tif(s.sz==1)f[b].pb(E(a,s[0]));\n\t\t\telse rep(j,s.sz){\n\t\t\t\tif(j==0)f[cur].pb(E(a,s[j])),cur++;\n\t\t\t\telse if(j==s.sz-1)f[b].pb(E(cur-1,s[j]));\n\t\t\t\telse f[cur].pb(E(cur-1,s[j])),cur++;\n\t\t\t}\n\t\t}\n\t\tdp.clear();\n\t\tcout<<F(n-1,N-1)<<endl;\n\t}\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 0, "score_of_the_acc": -0.0175, "final_rank": 2 }, { "submission_id": "aoj_2288_289836", "code_snippet": "#include <cstring>\n#include <iostream>\n#include <cstdio>\n#include <queue>\n#define REP(i,n) for(int i=0; i<(int)(n); i++)\n\n#define f first\n#define s second\n#define mp make_pair\n\nusing namespace std;\n\nint n[2], m[2];\n\nstruct edge{\n int src;\n int dst;\n string label;\n\n edge(int s = 0, int d = 0, string l = \"\") :\n src(s), dst(d), label(l) {}\n};\n\nvector<edge> e[2];\nvector<vector<int> > re[2];\n\nint memo[600][600];\nconst int mod = 1000000007;\n\nint solve(int id1, int id2, int l1, int l2){\n if(e[0][id1].dst == n[0] - 1 &&\n e[1][id2].dst == n[1] - 1 &&\n l1 == e[0][id1].label.size() &&\n l2 == e[1][id2].label.size())\n return 1;\n\n if(l1 == e[0][id1].label.size() &&\n l2 == e[1][id2].label.size() &&\n memo[id1][id2] != -1)\n return memo[id1][id2];\n \n int ret = 0;\n\n if(l1 == e[0][id1].label.size()){\n int src = e[0][id1].dst;\n\n REP(i, re[0][src].size()){\n int i1 = re[0][src][i];\n int i2 = id2;\n int ln1 = 0;\n int ln2 = l2;\n const string &s1 = e[0][i1].label;\n const string &s2 = e[1][i2].label;\n\n while(ln1 < s1.size() && ln2 < s2.size() && s1[ln1] == s2[ln2]){\n\tln1++; ln2++;\n }\n\n if(ln1 == s1.size() || ln2 == s2.size()){\n\tret = ((long long)ret + solve(i1, i2, ln1, ln2)) % mod;\n }\n }\n }else if(l2 == e[1][id2].label.size()){\n int src = e[1][id2].dst;\n\n REP(i, re[1][src].size()){\n int i1 = id1;\n int i2 = re[1][src][i];\n int ln1 = l1;\n int ln2 = 0;\n const string &s1 = e[0][i1].label;\n const string &s2 = e[1][i2].label;\n\n while(ln1 < s1.size() && ln2 < s2.size() && s1[ln1] == s2[ln2]){\n\tln1++; ln2++;\n }\n\n if(ln1 == s1.size() || ln2 == s2.size()){\n\tret = ((long long)ret + solve(i1, i2, ln1, ln2)) % mod;\n }\n }\n }else{\n printf(\"solve(%d, %d, %d, %d)\\n\", id1, id2, l1, l2);\n\n printf(\"e[0][%d].label: %s\\n\", id1, e[0][id1].label.c_str());\n printf(\"e[1][%d].label: %s\\n\", id2, e[1][id2].label.c_str());\n\n throw \"error\";\n }\n\n if(l1 == e[0][id1].label.size() &&\n l2 == e[1][id2].label.size())\n memo[id1][id2] = ret;\n\n return ret;\n}\n\nint main(){\n int t; cin >> t;\n while(t --> 0){\n REP(i,2){\n cin >> n[i] >> m[i];\n\n e[i] = vector<edge>(m[i]);\n re[i] = vector<vector<int> >(n[i]);\n\n REP(j,m[i]){\n\tint s, d;\n\tstring l;\n\tcin >> s >> d >> l;\n\te[i][j] = edge(s, d, l);\n\tre[i][s].push_back(j);\n }\n }\n\n memset(memo, -1, sizeof(memo));\n\n int ans = 0;\n REP(i, re[0][0].size()) REP(j, re[1][0].size()){\n int id1 = re[0][0][i];\n int id2 = re[1][0][j];\n int l1 = 0;\n int l2 = 0;\n const string &s1 = e[0][id1].label;\n const string &s2 = e[1][id2].label;\n\n while(l1 != s1.size() && l2 != s2.size() && s1[l1] == s2[l2]){\n\tl1++; l2++;\n }\n\n if(l1 == s1.size() || l2 == s2.size()){\n\tans = ((long long)ans + solve(id1, id2, l1, l2)) % mod;\n }\n }\n\n printf(\"%d\\n\", ans);\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 0, "score_of_the_acc": 0, "final_rank": 1 }, { "submission_id": "aoj_2288_286538", "code_snippet": "#include <stdio.h>\n#include <iostream>\n#include <vector>\n#include <list>\n#include <cmath>\n#include <fstream>\n#include <algorithm>\n#include <string>\n#include <queue>\n#include <set>\n#include <map>\n#include <complex>\n#include <iterator>\n#include <cstdlib>\n#include <cstring>\n#include <sstream>\n\nusing namespace std;\n\n#define EPS (1e-10)\n#define EQ(a,b) (abs((a) - (b)) < EPS)\n#define EQV(a,b) (EQ((a).real(),(b).real()) && EQ((a).imag(),(b).imag()))\n\ntypedef complex<double> P;\ntypedef long long ll;\n\nstruct edge{\n int to;\n char ch;\n};\n\nvector<edge> G[2][4000];\n// dp[i][j]->ƒm[ƒg‚̃m[ƒh‚ði,ƒm[ƒg‚̃Rƒs[‚̃m[ƒh‚ðj‚Æ‚µ‚½‚Æ‚«A‚»‚±‚܂łɋ¤’Ê‚µ‚ÄŒ»‚ê‚é•¶Í‚̐”\n//int dp[3600][3600];\nmap<int,map<int,int> > dp;\nint n[2],m[2];\n\nconst int INF=-1;\nconst int MOD=1000000007;\n\nint dfs(int idx1,int idx2){\n //if(dp[idx1][idx2]!=-1)\n // return dp[idx1][idx2];\n if(!(dp.find(idx1)==dp.end()||dp[idx1].find(idx2)==dp[idx1].end()))\n return dp[idx1][idx2];\n if(idx1==0&&idx2==0)\n return 1;\n else if(idx1==0||idx2==0)\n return 0;\n // Œ»Ý‚Ì’n“_‚É‚¢‚½‚郋[ƒg‚ð‚·‚×‚Ä’²‚׏グ‚é\n // –ß‚è•û‚Ì‘g‚ݍ‡‚킹‚ðƒŠƒXƒgƒAƒbƒv‚µA‚·‚ׂDz‚ׂé\n int res=0;\n for(int i = 0; i < G[0][idx1].size(); i++){\n edge back=G[0][idx1][i];\n for(int j = 0; j < G[1][idx2].size(); j++){\n edge back2=G[1][idx2][j];\n if(back.ch==back2.ch){\n res+=dfs(back.to,back2.to);\n res%=MOD;\n }\n }\n }\n return dp[idx1][idx2]=res%MOD;\n}\n\nint main(){\n\n int t;\n cin>>t;\n while(t--){\n dp.clear();\n //memset(dp,0,sizeof(dp));\n //for(int i = 0; i < 3600; i++)\n // fill(dp[i],dp[i]+3600,INF);\n for(int j = 0; j < 2; j++)\n for(int i = 0; i < 4000; i++)\n G[j][i].clear();\n for(int j = 0; j < 2; j++){\n cin>>n[j]>>m[j];\n int idx=n[j];\n for(int i = 0; i < m[j]; i++){\n int from,to;\n string str;\n cin>>from>>to>>str;\n // •¶Žš—ñ‚𕪗£‚µ‚Ä‚»‚ꂼ‚ê‚Éid‚ð‚‚¯‚ÄG‚ÉŠi”[‚·‚é\n int prv=from;\n for(int k = 0; k < str.size(); k++){\n edge e;\n e.to=prv;\n e.ch=str[k];\n int tmp=idx;\n if(k==str.size()-1){\n tmp=to;\n }\n G[j][tmp].push_back(e);\n prv=tmp;\n if(k==str.size()-1)\n break;\n idx++;\n }\n }\n }\n // ƒƒ‚‰»Ä‹A‚Ő”‚¦ã‚°\n int res=dfs(n[0]-1,n[1]-1);\n cout<<res%MOD<<endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 0, "score_of_the_acc": -0.0175, "final_rank": 2 }, { "submission_id": "aoj_2288_286537", "code_snippet": "#include <stdio.h>\n#include <iostream>\n#include <vector>\n#include <list>\n#include <cmath>\n#include <fstream>\n#include <algorithm>\n#include <string>\n#include <queue>\n#include <set>\n#include <map>\n#include <complex>\n#include <iterator>\n#include <cstdlib>\n#include <cstring>\n#include <sstream>\n\nusing namespace std;\n\n#define EPS (1e-10)\n#define EQ(a,b) (abs((a) - (b)) < EPS)\n#define EQV(a,b) (EQ((a).real(),(b).real()) && EQ((a).imag(),(b).imag()))\n\ntypedef complex<double> P;\ntypedef long long ll;\n\nstruct edge{\n int to;\n char ch;\n};\n\nvector<edge> G[2][4000];\n// dp[i][j]->ƒm[ƒg‚̃m[ƒh‚ði,ƒm[ƒg‚̃Rƒs[‚̃m[ƒh‚ðj‚Æ‚µ‚½‚Æ‚«A‚»‚±‚܂łɋ¤’Ê‚µ‚ÄŒ»‚ê‚é•¶Í‚̐”\nint dp[3600][3600];\nint n[2],m[2];\n\nconst int INF=-1;\nconst int MOD=1000000007;\n\nint dfs(int idx1,int idx2){\n if(dp[idx1][idx2]!=-1)\n return dp[idx1][idx2];\n if(idx1==0&&idx2==0)\n return 1;\n else if(idx1==0||idx2==0){\n return 0;\n }\n // Œ»Ý‚Ì’n“_‚É‚¢‚½‚郋[ƒg‚ð‚·‚×‚Ä’²‚׏グ‚é\n // –ß‚è•û‚Ì‘g‚ݍ‡‚킹‚ðƒŠƒXƒgƒAƒbƒv‚µA‚·‚ׂDz‚ׂé\n int res=0;\n for(int i = 0; i < G[0][idx1].size(); i++){\n edge back=G[0][idx1][i];\n for(int j = 0; j < G[1][idx2].size(); j++){\n edge back2=G[1][idx2][j];\n if(back.ch==back2.ch){\n res+=dfs(back.to,back2.to);\n res%=MOD;\n }\n }\n }\n return dp[idx1][idx2]=res%MOD;\n}\n\nint main(){\n\n int t;\n cin>>t;\n while(t--){\n //memset(dp,0,sizeof(dp));\n for(int i = 0; i < 3600; i++)\n fill(dp[i],dp[i]+3600,INF);\n for(int j = 0; j < 2; j++)\n for(int i = 0; i < 4000; i++)\n G[j][i].clear();\n for(int j = 0; j < 2; j++){\n cin>>n[j]>>m[j];\n int idx=n[j];\n for(int i = 0; i < m[j]; i++){\n int from,to;\n string str;\n cin>>from>>to>>str;\n // •¶Žš—ñ‚𕪗£‚µ‚Ä‚»‚ꂼ‚ê‚Éid‚ð‚‚¯‚ÄG‚ÉŠi”[‚·‚é\n int prv=from;\n for(int k = 0; k < str.size(); k++){\n edge e;\n e.to=prv;\n e.ch=str[k];\n int tmp=idx;\n if(k==str.size()-1){\n tmp=to;\n }\n G[j][tmp].push_back(e);\n prv=tmp;\n if(k==str.size()-1)\n break;\n idx++;\n }\n }\n }\n // ƒƒ‚‰»Ä‹A‚Ő”‚¦ã‚°\n int res=dfs(n[0]-1,n[1]-1);\n cout<<res%MOD<<endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 580, "memory_kb": 0, "score_of_the_acc": -1, "final_rank": 13 }, { "submission_id": "aoj_2288_284039", "code_snippet": "#include <iostream>\n#include <algorithm>\n#include <vector>\n#include <map>\n\nusing namespace std;\n \nint n[2],m[2];\nmap< pair<string,string> , int > memo[501][501];\nvector< vector<int> > a[2];\nvector< vector<string> > b[2];\n \nint dfs(int p1,int p2,string s1,string s2){\n if(p1 == n[0]-1 && p2 == n[1]-1) return (s1 == \"\" && s2 == \"\");\n\t\tif(memo[p1][p2].count(make_pair(s1,s2))) return memo[p1][p2][make_pair(s1,s2)];\n\n int ans = 0;\n\t\tif(p1 <= p2 || p2 == n[1]-1){\n\t\t\tfor(int i = 0 ; i < a[0][p1].size() ; i++){\n\t\t\t\t\tstring x = s1 + b[0][p1][i];\n\t\t\t\t\tstring y = s2;\n\t\t\t\t\tint I = 0 , J = 0;\n\t\t\t\t\tfor( ; I < x.size() && J < y.size() && x[I] == y[J] ; ) I++ , J++ ;\n\t\t\t\t\tif( I == x.size() || J == y.size() ){\n\t\t\t\t\t\t\t\n\t\t\t\t\t\t\tif( I == x.size() && I == y.size() ) x = \"\" , y = \"\";\n\t\t\t\t\t\t\telse if( I == x.size() ) x = \"\" , y = y.substr(J);\n\t\t\t\t\t\t\telse if( J == y.size() ) x = x.substr(I) , y = \"\";\n\t\t\t\t\t\t\t\n\t\t\t\t\t\t\tans = ( ans + dfs(a[0][p1][i],p2,x,y) ) % 1000000007;\n\t\t\t\t\t}\n\t\t\t}\n\t\t}\n \n\t\tif(p1 > p2 || p1 == n[0]-1){\n\t\t\tfor(int j = 0 ; j < a[1][p2].size() ; j++){\n\t\t\t\t\tstring x = s1;\n\t\t\t\t\tstring y = s2 + b[1][p2][j] ;\n\t\t\t\t\tint I = 0 , J = 0;\n\t\t\t\t\tfor( ; I < x.size() && J < y.size() && x[I] == y[J] ; ) I++ , J++ ;\n\t\t\t\t\tif( I == x.size() || J == y.size() ){\n\t\t\t\t\t\t\t\n\t\t\t\t\t\t\tif( I == x.size() && I == y.size() ) x = \"\" , y = \"\";\n\t\t\t\t\t\t\telse if( I == x.size() ) x = \"\" , y = y.substr(J);\n\t\t\t\t\t\t\telse if( J == y.size() ) x = x.substr(I) , y = \"\";\n\t\t\t\t\t\t\t\n\t\t\t\t\t\t\tans = ( ans + dfs(p1, a[1][p2][j],x,y) ) % 1000000007;\n\t\t\t\t\t}\n\t\t\t}\n\t\t}\n return memo[p1][p2][make_pair(s1,s2)] = ans;\n}\n \nint main(){\n int T;cin >> T;\n while(T--){\n for(int i = 0 ; i < 2 ; i++){\n cin >> n[i] >> m[i];\n //cout << n[i] << \" \" << m[i] << endl;\n a[i].clear(); b[i].clear(); a[i].resize((unsigned)n[i]) , b[i].resize((unsigned)n[i]);\n for(int j = 0 ; j < m[i] ; j++){\n int A,B; string C;\n cin >> A >> B >> C;\n a[i][A].push_back(B);\n b[i][A].push_back(C);\n }\n }\n\t\t\t\tfor(int i = 0 ; i < 501 ; i++)\n\t\t\t\t\tfor(int j = 0 ; j < 501 ; j++)\n\t\t\t\t\t\tmemo[i][j].clear();\n cout << dfs(0,0,\"\",\"\") << endl;\n }\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 7756, "score_of_the_acc": -0.1964, "final_rank": 7 }, { "submission_id": "aoj_2288_283931", "code_snippet": "#include <iostream>\n#include <algorithm>\n#include <vector>\n#include <map>\n\nusing namespace std;\n \nint n[2],m[2];\nmap< pair<string,string> , int > memo[501][501];\nvector< vector<int> > a[2];\nvector< vector<string> > b[2];\n \nint dfs(int p1,int p2,string s1,string s2){\n //cout << p1 << \" \" << p2 << \" [\" << s1 << \"] [\" << s2 << \"]\" << \"{\" << n[0] << \" \" << n[1] << endl;\n if(p1 == n[0]-1 && p2 == n[1]-1) return (s1 == \"\" && s2 == \"\");\n\t\tif(memo[p1][p2].count(make_pair(s1,s2))) return memo[p1][p2][make_pair(s1,s2)];\n\n int ans = 0;\n\t\tif(p1 <= p2 || p2 == n[1]-1){\n\t\t\tfor(int i = 0 ; i < a[0][p1].size() ; i++){\n\t\t\t\t\tstring x = s1 + b[0][p1][i];\n\t\t\t\t\tstring y = s2;\n\t\t\t\t\tint I = 0 , J = 0;\n\t\t\t\t\tfor( ; I < x.size() && J < y.size() && x[I] == y[J] ; ) I++ , J++ ;\n\t\t\t\t\tif( I == x.size() || J == y.size() ){\n\t\t\t\t\t\t\t\n\t\t\t\t\t\t\tif( I == x.size() && I == y.size() ) x = \"\" , y = \"\";\n\t\t\t\t\t\t\telse if( I == x.size() ) x = \"\" , y = y.substr(J);\n\t\t\t\t\t\t\telse if( J == y.size() ) x = x.substr(I) , y = \"\";\n\t\t\t\t\t\t\t\n\t\t\t\t\t\t\tans = ( ans + dfs(a[0][p1][i],p2,x,y) ) % 1000000007;\n\t\t\t\t\t}\n\t\t\t}\n\t\t}\n \n\t\tif(p1 > p2 || p1 == n[0]-1){\n\t\t\tfor(int j = 0 ; j < a[1][p2].size() ; j++){\n\t\t\t\t\tstring x = s1;\n\t\t\t\t\tstring y = s2 + b[1][p2][j] ;\n\t\t\t\t\tint I = 0 , J = 0;\n\t\t\t\t\tfor( ; I < x.size() && J < y.size() && x[I] == y[J] ; ) I++ , J++ ;\n\t\t\t\t\tif( I == x.size() || J == y.size() ){\n\t\t\t\t\t\t\t\n\t\t\t\t\t\t\tif( I == x.size() && I == y.size() ) x = \"\" , y = \"\";\n\t\t\t\t\t\t\telse if( I == x.size() ) x = \"\" , y = y.substr(J);\n\t\t\t\t\t\t\telse if( J == y.size() ) x = x.substr(I) , y = \"\";\n\t\t\t\t\t\t\t\n\t\t\t\t\t\t\tans = ( ans + dfs(p1, a[1][p2][j],x,y) ) % 1000000007;\n\t\t\t\t\t}\n\t\t\t}\n\t\t}\n return memo[p1][p2][make_pair(s1,s2)] = ans;\n}\n \nint main(){\n int T;cin >> T;\n while(T--){\n for(int i = 0 ; i < 2 ; i++){\n cin >> n[i] >> m[i];\n //cout << n[i] << \" \" << m[i] << endl;\n a[i].clear(); b[i].clear(); a[i].resize((unsigned)n[i]) , b[i].resize((unsigned)n[i]);\n for(int j = 0 ; j < m[i] ; j++){\n int A,B; string C;\n cin >> A >> B >> C;\n a[i][A].push_back(B);\n b[i][A].push_back(C);\n }\n }\n\t\t\t\tfor(int i = 0 ; i < 501 ; i++)\n\t\t\t\t\tfor(int j = 0 ; j < 501 ; j++)\n\t\t\t\t\t\tmemo[i][j].clear();\n cout << dfs(0,0,\"\",\"\") << endl;\n }\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 0, "score_of_the_acc": -0.1228, "final_rank": 4 }, { "submission_id": "aoj_2288_282229", "code_snippet": "#include <stdio.h>\n#include <string.h>\n#include <string>\n#include <vector>\n#include <map>\n#include <algorithm>\nusing namespace std;\n#define rep(i, n) for(int i=0; i<(int)(n); i++)\ntypedef long long Int;\n#define MOD (1000000007)\ninline void up(int &a, int b) { a = (a+b)%MOD; }\n\ntemplate<class W>\nstruct E {\n int u, v;\n W w;\n E() {}\n E(int u, int v, const W& w) : u(u), v(v), w(w) {}\n};\ntemplate<class W>\nbool less_u(const E<W>& l, const E<W>& r) {\n return l.u!=r.u ? l.u<r.u : l.v!=r.v ? l.v<r.v : l.w<r.w;\n}\n\nint N[2], M[2];\nint of(int x, int y, int i, int j) {\n return (((x*600)+y)*16+i)*16+j;\n}\n\nint main() {\n int T;\n scanf(\"%d\", &T);\n vector<E<string> > g[2];\n map<int, int> dp;\n while(T--) {\n vector<vector<int> > to[2];\n rep(k, 2) {\n scanf(\"%d%d\", N+k, M+k);\n g[k].clear();\n rep(i, M[k]) {\n int a, b;\n char buf[8];\n scanf(\"%d%d%s\", &a, &b, buf);\n g[k].push_back(E<string>(a, b, buf));\n }\n sort(g[k].begin(), g[k].end(), less_u<string>);\n to[k] = vector<vector<int> >(N[k]);\n rep(i, M[k]) {\n const int u = g[k][i].u;\n to[k][u].push_back(i);\n }\n to[k][N[k]-1].push_back(M[k]);\n }\n dp.clear();\n rep(i, to[0][0].size()) rep(j, to[1][0].size()) {\n const int ix = to[0][0][i], jx = to[1][0][j];\n dp[of(ix, jx, 0, 0)] = 1;\n }\n rep(x, M[0]) rep(y, M[1]) {\n const int v0 = g[0][x].v, v1 = g[1][y].v;\n const string &a = g[0][x].w, &b = g[1][y].w;\n rep(i, a.size()) rep(j, b.size()) if(a[i]==b[j]) {\n const int cur = of(x, y, i, j);\n if(dp.count(cur)==0) continue;\n const int val = dp[cur];\n if(i<(int)a.size()-1 && j<(int)b.size()-1) {\n up(dp[of(x, y, i+1, j+1)], val);\n }\n else if(i<(int)a.size()-1) {\n rep(p, to[1][v1].size()) {\n const int ix = to[1][v1][p];\n up(dp[of(x, ix, i+1, 0)], val);\n }\n }\n else if(j<(int)b.size()-1) {\n rep(p, to[0][v0].size()) {\n const int ix = to[0][v0][p];\n up(dp[of(ix, y, 0, j+1)], val);\n }\n }\n else {\n rep(p, to[0][v0].size()) rep(q, to[1][v1].size()) {\n const int ix = to[0][v0][p];\n const int jx = to[1][v1][q];\n up(dp[of(ix, jx, 0, 0)], val);\n }\n }\n }\n }\n printf(\"%d\\n\", dp[of(M[0], M[1], 0, 0)]);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 370, "memory_kb": 2388, "score_of_the_acc": -0.6542, "final_rank": 12 }, { "submission_id": "aoj_2288_281893", "code_snippet": "#include <iostream>\n#include <cctype>\n#include <cstdlib>\n#include <string>\n#include <memory.h>\n#include <cmath>\n#include <vector>\n#include <algorithm>\n#include <queue>\n#include <map>\nusing namespace std;\n\n#define rep(i, n) for(int i = 0; i < n; i++)\ntypedef long long ll;\ntypedef pair<int, int> P;\ntypedef pair<int,P> PP;\nconst int INF = 1 << 29;\nconst int mod = 1000000007;\n\nstruct edge{\n int to;\n string s;\n edge(){}\n edge(int to, string s){\n this->to = to;\n this->s = s;\n }\n};\n\nbool ok(const string &s1, const string &s2){\n int n = s1.size();\n rep(i, n) if(s1[i] != s2[i]) return false;\n return true;\n}\n\n\nmap<string, ll> dp[600][600][2];\n\nvector<edge> G[2][600];\n\n\nint main(){\n int T;\n cin >> T;\n int n1, m1, n2, m2;\n int a, b;\n string s, nxt;\n edge e;\n rep(t, T){\n cin >> n1 >> m1;\n rep(i, 600)rep(j, 2) G[j][i].clear();\n rep(i, 600)rep(j, 600)rep(k, 2) dp[i][j][k].clear(); \n rep(i, m1){\n cin >> a >> b >> s;\n G[0][a].push_back(edge(b, s));\n }\n cin >> n2 >> m2;\n rep(i, m2){\n cin >> a >> b >> s;\n G[1][a].push_back(edge(b, s));\n }\n dp[0][0][0][\"\"] = 1;\n rep(i, n1)rep(j, n2){\n rep(k, 2){\n\tfor(map<string, ll>::iterator iter = dp[i][j][k].begin(); \n\t iter != dp[i][j][k].end(); iter++){\n\t string s = (*iter).first;\n\t ll d = (*iter).second;\n\t if(k == 1){\n\t rep(v, (int)G[k][j].size()){\n\t e = G[k][j][v];\n\t if(e.s.size() < s.size() && ok(e.s, s)){\n\t\tnxt = string(s.begin() + e.s.size(), s.end());\n\t\tdp[i][e.to][1][nxt] = (dp[i][e.to][1][nxt] + d) % mod;\n\t }\n\t if(e.s.size() >= s.size() && ok(s, e.s)){\n\t\tnxt = string(e.s.begin() + s.size(), e.s.end());\n\t\tdp[i][e.to][0][nxt] = (dp[i][e.to][0][nxt] + d) % mod;\n\t }\n\t }\n\t }else{\n\t rep(v, (int)G[k][i].size()){ \n\t e = G[k][i][v];\n\t if(e.s.size() <= s.size() && ok(e.s, s)){\n\t\tnxt = string(s.begin() + e.s.size(), s.end());\n\t\tdp[e.to][j][0][nxt] = (dp[e.to][j][0][nxt] + d) % mod;\n\t }\n\t if(e.s.size() > s.size() && ok(s, e.s)){\n\t\tnxt = string(e.s.begin() + s.size(), e.s.end());\n\t\tdp[e.to][j][1][nxt] = (dp[e.to][j][1][nxt] + d) % mod;\n\t }\n\t }\n\t }\n\t}\n }\n }\n\n cout << dp[n1-1][n2-1][0][\"\"] << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 240, "memory_kb": 0, "score_of_the_acc": -0.4035, "final_rank": 9 }, { "submission_id": "aoj_2288_281823", "code_snippet": "#include <stdio.h>\n#include <string.h>\n#include <algorithm>\n#include <iostream>\n#include <math.h>\n#include <assert.h>\n#include <vector>\n#include <string>\n#include <map>\n\nusing namespace std;\ntypedef long long ll;\ntypedef unsigned int uint;\ntypedef unsigned long long ull;\nstatic const double EPS = 1e-9;\nstatic const double PI = acos(-1.0);\n\n#define REP(i, n) for (int i = 0; i < (int)(n); i++)\n#define FOR(i, s, n) for (int i = (s); i < (int)(n); i++)\n#define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++)\n#define FORIT(it, c) for (__typeof((c).begin())it = (c).begin(); it != (c).end(); it++)\n#define MEMSET(v, h) memset((v), h, sizeof(v))\n\nstruct State {\n int left;\n int right;\n int lr;\n int cnt;\n State() {;}\n State(int left, int right, int lr, int cnt) : left(left), right(right), lr(lr), cnt(cnt) {;}\n bool operator<(const State &rhs) const {\n if (left != rhs.left) { return left < rhs.left; }\n if (right != rhs.right) { return right < rhs.right; }\n if (lr != rhs.lr) { return lr < rhs.lr; }\n return cnt < rhs.cnt;\n }\n};\n\nconst ll MOD = 1000000007;\nint n[2];\nint m[2];\nvector<string> strs[2];\nvector<vector<int> > from[2];\nvector<int> to[2];\nmap<State, ll> memo;\n\nll calc(int left, int right, int lr, int cnt);\nll NextState(int left, int right, int lr, int cnt) {\n ll ret = 0;\n if (lr == 0) {\n FORIT(it, from[1][right]) {\n ret = (ret + calc(left, *it, lr, cnt)) % MOD;\n }\n } else {\n FORIT(it, from[0][left]) {\n ret = (ret + calc(*it, right, lr, cnt)) % MOD;\n }\n }\n return ret;\n}\n\nll calc(int left, int right, int lr, int cnt) {\n if (left == m[0] && right == m[1]) { return 1; }\n State state(left, right, lr, cnt);\n if (memo.count(state)) { return memo[state]; }\n ll ret = 0;\n const string &lstr = strs[0][left];\n const string &rstr = strs[1][right];\n if (lr == 0 && (int)lstr.size() == cnt) {\n ret = NextState(to[0][left], right, 1, 0);\n } else if (lr == 1 && (int)rstr.size() == cnt) {\n ret = NextState(left, to[1][right], 0, 0);\n } else {\n int lindex = 0;\n int rindex = 0;\n if (lr == 0) { lindex = cnt; }\n else { rindex = cnt; }\n while (lindex < (int)lstr.size() && rindex < (int)rstr.size()) {\n if (lstr[lindex] != rstr[rindex]) { goto end; }\n lindex++;\n rindex++;\n }\n if (lindex == (int)lstr.size()) {\n ret = NextState(to[0][left], right, 1, rindex);\n } else {\n ret = NextState(left, to[1][right], 0, lindex);\n }\n }\nend:\n return memo[state] = ret;\n}\n\nchar str[100];\nint main() {\n int test;\n scanf(\"%d\", &test);\n while (test--) {\n memo.clear();\n REP(iter, 2) {\n scanf(\"%d %d\", &n[iter], &m[iter]);\n from[iter].clear();\n strs[iter].resize(m[iter] + 1);\n from[iter].resize(n[iter]);\n to[iter].resize(m[iter]);\n REP(i, m[iter]) {\n int f, t;\n scanf(\"%d %d %s\", &f, &t, str);\n from[iter][f].push_back(i);\n to[iter][i] = t;\n strs[iter][i] = str;\n }\n from[iter][n[iter] - 1].push_back(m[iter]);\n strs[iter][m[iter]] = \"!\";\n }\n ll ans = 0;\n FORIT(lit, from[0][0]) {\n ans = (ans + NextState(*lit, 0, 0, 0)) % MOD;\n }\n printf(\"%lld\\n\", ans);\n }\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 2576, "score_of_the_acc": -0.1823, "final_rank": 6 } ]
aoj_2286_cpp
Problem F: Farey Sequence slipは数字を眺めるのが好きである。 ファイルをダウンロードしているときの、残り時間を眺めているだけで、時間を潰せるほどである。 そんなslipが友人から面白い数列を教えられた。 その数列の定義は以下のとおりである。 一般項を F n と表わす。 F n とは、 n 以下の分母を持つ0以上1以下のすべての約分された分数(既約分数)を小さな順から並べたものである。 ただし、整数0、1はそれぞれ分数0/1、1/1として扱われる。 つまり F 3 は、 F 3 = (0/1, 1/3, 1/2, 2/3, 1/1) のように表される。 F 5 であれば F 5 = (0/1, 1/5, 1/4, 1/3, 2/5, 1/2, 3/5, 2/3, 3/4, 4/5, 1/1) と表される。 slipはこの数列にすぐにのめり込んだ。 しかしこの数列を眺めていても、なかなか特徴がつかめなかった。 特徴をつかむためにまずは各項の項数がいくつあるのか調べようと思ったが、 増え方の規則がわからず、自力で理解することを断念した。 そこで、 勝手に友人と思っているあなたに、指定した項にどれだけの項数があるのかを聞くことにした。 あなたの仕事は、与えられた項における項数を求めることである。 Input 入力は以下の形式で与えられる。 t n 1 ... n i ... n t t ( 1 ≤ t ≤ 10000 )はテストケースの数である。 n i ( 1 ≤ n i ≤ 1000000 )は与えられた項の番号である。 Output 各テストケースごとに、項数を1行に表示せよ。 Sample Input 2 3 5 Output for Sample Input 5 11
[ { "submission_id": "aoj_2286_11042230", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\nusing ld = long double;\nusing vi = vector<int>;\nusing vvi = vector<vector<int>>;\nusing vvvi = vector<vector<vector<int>>>;\nusing vll = vector<ll>;\nusing vvll = vector<vector<ll>>;\nusing vvvll = vector<vector<vector<ll>>>;\nusing vd = vector<double>;\nusing vvd = vector<vector<double>>;\nusing vstr = vector<string>;\nusing vchar = vector<char>;\nusing vvchar = vector<vector<char>>;\nusing vb = vector<bool>;\nusing vvb = vector<vector<bool>>;\nusing vvvb = vector<vector<vector<bool>>>;\nusing pii = pair<int,int>;\nusing pll = pair<long long, long long>;\nusing Graph = vector<vector<int>>;\nusing WeightedGraph = vector<vector<pair<int,ll>>>;\nconst int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};\nconst int dy[8] = {0, 1, 0, -1, 1, -1, 1, -1};\n\n#define rep(i, a, b) for(ll i = (ll)a; i < (ll)b; i++)\n#define REP(i, a, b) for (ll i = (ll)a; i <= (ll)b; i++)\n#define rrep(i, a, b) for(ll i = (ll)b-1; i >= (ll)a; i--)\n#define RREP(i, a, b) for(ll i = (ll)b; i >= (ll)a; i--)\n#define all(v) v.begin(), v.end()\n#define rall(v) v.rbegin(), v.rend()\n#define YESNO(flag) cout << (flag ? \"Yes\" : \"No\") << \"\\n\"\n#define spa \" \"\n#define mint modint\n\nconst int inf = 1070000000;\nconst long long INF = 4500000000000000000;\n//const long long MOD = 998244353;\nconst long long MOD = 1000000007;\nconst double pi = 3.141592653589793238;\nconst double eps = (1e-10);\nconst string ABC = \"ABCDEFGHIJKLMNOPQRSTUVWXYZ\";\nconst string abc = \"abcdefghijklmnopqrstuvwxyz\";\n\ntemplate<typename T> inline bool chmin(T& a, T b) {\n if (a > b) {\n a = b;\n return true;\n }\n return false;\n}\ntemplate<typename T> inline bool chmax(T& a, T b) {\n if (a < b) {\n a = b;\n return true;\n }\n return false;\n}\ntemplate<typename S, typename T> pair<S, T> operator+(pair<S, T> a, pair<S, T> b) {\n pair<S, T> ans;\n ans.first = a.first + b.first;\n ans.second = a.second + b.second;\n return ans;\n} \n\n//拡張ユークリッド\nll ext_gcd(ll a, ll b, ll &x, ll &y) {\n if(b == 0) {\n x = 1, y = 0;\n return a;\n }\n\n ll q = a/b;\n ll g = ext_gcd(b, a-q*b, x, y);\n ll z = x-q*y;\n x = y, y = z;\n return g;\n}\n//mod mにおけるaの逆元. aとmは互いに素.\nll inverse(ll a, ll m) {\n ll x, y;\n ext_gcd(a, m, x, y);\n x = (x+m) % m;\n return x;\n}\n//modint\ntemplate<std::uint_fast64_t mod> struct modint {\n using u64 = std::uint_fast64_t;\n u64 val;\n\n modint(u64 v = 0) : val(v % mod) {}\n\n bool operator==(modint other) {\n return val == other.val;\n }\n modint operator+(modint other) {\n modint<mod> ans;\n ans.val = val + other.val;\n if(ans.val >= mod) ans.val -= mod;\n return ans;\n }\n modint operator-(modint other) {\n modint<mod> ans;\n if(val < other.val) val += mod;\n ans.val = val - other.val;\n return ans;\n }\n modint operator*(modint other) {\n modint<mod> ans;\n ans.val = (val * other.val) % mod;\n return ans;\n }\n modint operator/(modint other) {\n modint<mod> ans;\n ans.val = (val * inverse(other.val, mod)) % mod;\n return ans;\n }\n modint &operator+=(modint other) {\n *this = *this + other;\n return *this;\n }\n modint &operator-=(modint other) {\n *this = *this - other;\n return *this;\n }\n modint &operator*=(modint other) {\n *this = *this * other;\n return *this;\n }\n modint &operator/=(modint other) {\n *this = *this / other;\n return *this;\n }\n modint &operator++() {\n *this += 1;\n return *this;\n }\n modint &operator++(int) {\n ++(*this);\n return *this;\n }\n modint &operator--() {\n *this -= 1;\n return *this;\n }\n modint &operator--(int) {\n --(*this);\n return *this;\n }\n};\n//modintの入出力\ntemplate<std::uint_fast64_t mod> std::ostream& operator<<(std::ostream &os, const modint<mod> &x) {\n // modint の 'val' メンバを出力ストリーム(os)に渡す\n os << x.val;\n return os; // ストリームを返す (チェーンのため)\n}\ntemplate<std::uint_fast64_t mod> std::istream& operator>>(std::istream& is, modint<mod> &x) {\n // modintが内部で使っているu64型を取得\n using u64 = typename modint<mod>::u64; \n \n u64 input_value;\n is >> input_value; // (1) istream(cin) から一時変数に値を読み込む\n \n // (2) 読み込んだ値を使って modint を再構築する\n // modintのコンストラクタが (input_value % mod) を処理してくれる\n x = modint<mod>(input_value); \n \n return is; // (3) ストリームを返す (チェーンのため)\n}\n//累乗, x^n\ntemplate<typename T> T pow(T x, int n) {\n T ans = 1;\n while(n > 0) {\n if(n & 1) ans *= x;\n x *= x;\n n >>= 1; \n }\n return ans;\n}\n//階乗, n!\ntemplate<typename T> T fact(T n) {\n if(n == 0) return 1;\n return n * fact(n-1);\n}\n//nPk\ntemplate<typename T> T perm(T n, T k) {\n if(k == 0) return 1;\n return n * perm(n-1, k-1);\n}\n//nCk\ntemplate<typename T> T comb(T n, T k) {\n if(k > n) return 0;\n return perm(n, min(k, n-k))/fact(min(k, n-k));\n}\n//ord(n)\nll ord(ll n, ll p) {\n int ans = 0;\n while(n % p == 0) {\n n /= p;\n ans++;\n }\n return ans;\n}\n//ルジャンドルの公式ord(n!)\nll factord(ll n, ll p) {\n ll ans = 0;\n ll q = p, tmp = n;\n while(tmp > 0) {\n tmp = n/q;\n ans += tmp;\n q *= p;\n }\n return ans;\n}\n//素因数分解, O(√N)\nvector<pair<ll,ll>> factorize(ll N) {\n vector<pair<ll,ll>> ans;\n for(ll p = 2; p*p <= N; p++) {\n int ex = 0;\n while(N % p == 0) {\n N /= p;\n ex++;\n }\n if(ex != 0) ans.push_back({p,ex});\n }\n if(N != 1) ans.push_back({N,1});\n return ans;\n}\n//素数判定\nbool isprime(long long x) {\n if(x == 1) return false;\n \n for(long long i = 2; i*i <= x; i++) {\n if(x % i == 0) return false;\n }\n return true;\n}\n//エラトステネスの篩\nstruct Eratosthenes {\n vector<bool> prime;\n vector<int> minfactor;\n vector<int> mobius;\n vector<ll> euler;\n\n //コンストラクタ O(NloglogN)\n Eratosthenes(int N) : prime(N+1, true),\n minfactor(N+1, -1),\n mobius(N+1, 1),\n euler(N+1, 1) {\n prime[0] = false;\n prime[1] = false;\n minfactor[1] = 1;\n REP(i,1,N) euler[i] = i;\n\n for(int p = 2; p <= N; p++) {\n if(!prime[p]) continue;\n\n minfactor[p] = p;\n mobius[p] = -1;\n euler[p] = p-1;\n\n for(int q = 2; p*q <= N; q++) {\n prime[p*q] = false;\n if(minfactor[p*q] == -1) minfactor[p*q] = p;\n if(q % p == 0) mobius[p*q] = 0;\n else mobius[p*q] *= -1;\n euler[p*q] = euler[p*q]*(p-1)/p;\n }\n }\n }\n\n //素因数分解 O(logn)\n vector<pair<int,int>> factorize(int n) {\n vector<pair<int,int>> ans;\n while(n > 1) {\n int p = minfactor[n], exp = 0;\n while(n % p == 0) {\n n /= p;\n exp++;\n }\n ans.push_back({p, exp});\n }\n return ans;\n }\n //約数列挙 O(σ(n))\n vector<int> divisors(int n) {\n vector<int> ans = {1};\n while(n > 1) {\n int p = minfactor[n], d = 1, s = ans.size();\n while(n % p == 0) {\n d *= p;\n for(int i = 0; i < s; i++) {\n ans.push_back(ans[i]*d);\n }\n n /= p;\n }\n }\n return ans;\n }\n};\n//高速ゼータ変換\ntemplate<typename T> void fast_zeta(vector<T> &f) {\n int N = f.size();\n Eratosthenes sieve(N);\n for(int p = 2; p < N; p++) {\n if(!sieve.prime[p]) continue;\n \n for(int k = N/p; k >= 1; k--) {\n f[k] += f[k*p];\n }\n }\n}\n//高速メビウス変換\ntemplate<typename T> void fast_mobius(vector<T> &F) {\n int N = F.size();\n Eratosthenes sieve(N);\n for(int p = 2; p < N; p++) {\n if(!sieve.prime[p]) continue;\n\n for(int k = 1; k*p < N; k++) {\n F[k] -= F[k*p];\n }\n }\n}\n//添字GCD畳み込み\ntemplate<typename T> vector<T> gcd_conv(vector<T> &f, vector<T> &g) {\n int N = max(f.size(), g.size());\n vector<T> F(N, 0), G(N, 0), H(N);\n for(int i = 0; i < f.size(); i++) F[i] = f[i];\n for(int i = 0; i < g.size(); i++) G[i] = g[i];\n fast_zeta(F);\n fast_zeta(G);\n \n for(int i = 0; i < N; i++) H[i] = F[i]*G[i];\n fast_mobius(H);\n \n return H;\n}\n\n//行列積\nvvll matrix_product(vvll A, vvll B) {\n int H = A.size(), W = A[0].size(), S = B.size(), T = B[0].size();\n if(W != S) {\n cout << \"matrix_size_error\" << \"\\n\";\n return vvll(0);\n }\n\n vvll C(H, vll(T, 0));\n rep(i,0,H) rep(j,0,T) {\n rep(k,0,W) {\n C[i][j] += A[i][k]*B[k][j];\n //C[i][j] %= M;\n }\n }\n return C;\n}\nvvll matrix_pow(vvll A, ll n) {\n int N = A.size();\n vvll ans(N, vll(N, 0));\n rep(i,0,N) ans[i][i] = 1;\n \n while(n > 0) {\n if(n & 1) ans = matrix_product(ans, A);\n A = matrix_product(A, A);\n n >>= 1; \n }\n return ans;\n}\n\n//1+2+...+n = n(n+1)/2\nll tri(ll n) {\n ll x = n*(n+1);\n return x/2;\n}\n\n//座標圧縮\nvector<int> compress(vector<int> A) {\n vector<int> B = A;\n\n sort(B.begin(), B.end());\n B.erase(unique(B.begin(), B.end()), B.end());\n\n vector<int> res(A.size());\n for(int i = 0; i < (int)A.size(); i++) {\n res[i] = lower_bound(B.begin(), B.end(), A[i]) - B.begin();\n }\n return res;\n}\n\n//正方形グリッドの右回転, 左回転\nvoid rotate_right(vector<vector<char>> &S) {\n int N = S.size();\n auto T = S;\n rep(i,0,N) rep(j,0,N) {\n T[i][j] = S[N-1-j][i];\n }\n S = T;\n}\nvoid rotate_left(vector<vector<char>> &S) {\n int N = S.size();\n auto T = S;\n rep(i,0,N) rep(j,0,N) {\n T[i][j] = S[j][N-1-i];\n }\n S = T;\n}\n\n//2点(a,b)と(x,y)の距離\nlong double dist(pair<long double, long double> a, pair<long double, long double> b) {\n return sqrt((a.first-b.first)*(a.first-b.first) + (a.second-b.second)*(a.second-b.second));\n}\nlong double man(pair<long double, long double> a, pair<long double, long double> b) {\n return abs(a.first-b.first) + abs(a.second-b.second);\n}\n//内分点\npair<long double, long double> internal_division(pair<long double, long double> a, pair<long double, long double> b, long double p) {\n long double x = a.first + (b.first - a.first) * p;\n long double y = a.second + (b.second - a.second) * p;\n return {x, y};\n}\n\n//桁数\nint digit(ll N) {\n if(N <= 9) return 1;\n\n return 1 + digit(N / 10);\n}\n//A進数文字列NをB進数に変換. A,Bは10以下\nstring base_change(string N, ll A, ll B) {\n ll X = stoll(N), Y = 0;\n\n ll i = 1;\n while(X != 0) {\n Y += (X % 10) * i;\n i *= A;\n X /= 10;\n }\n\n X = Y, Y = 0;\n i = 1;\n while(X != 0) {\n Y += (X % B) * i;\n i *= 10;\n X /= B;\n }\n\n return to_string(Y);\n}\n//回文判定\nbool palin(string S) {\n rep(i,0,S.size()) {\n if(S[i] != S[S.size()-1-i]) return false;\n }\n return true;\n}\n\n//hh:mm:ss to second\nint convert_to_time(string S) {\n ll h = 10*(S[0]-'0') + (S[1]-'0');\n ll m = 10*(S[3]-'0') + (S[4]-'0');\n ll s = 10*(S[6]-'0') + (S[7]-'0');\n \n return h*3600+m*60+s;\n}\n\n//DFS\nvoid dfs(const Graph &G, int v, vector<bool> &seen) { \n seen[v] = true;\n \n for (auto next_v : G[v]) { \n if (!seen[next_v]) {\n dfs(G, next_v, seen);\n }\n }\n}\nvoid griddfs(const vector<vector<char>> &G, int x, int y, vector<vector<bool>> &seen) {\n int H = G.size(), W = G[0].size();\n seen[x][y] = true;\n \n for (int i = 0; i < 8; i++) { \n int nx = x + dx[i], ny = y + dy[i];\n if(nx < 0 or nx >= H or ny < 0 or ny >= W) continue;\n if (!seen[nx][ny] and G[nx][ny] != '#') griddfs(G, nx, ny, seen);\n }\n}\n//BFS\nvoid bfs(const Graph &G, int v, vector<int> &dist) {\n rep(i,0,dist.size()) dist[i] = inf;\n dist[v] = 0;\n queue<int> q;\n q.push(v);\n while(!q.empty()) {\n int u = q.front();\n q.pop();\n for(auto next_u : G[u]) {\n if(dist[next_u] == inf) {\n dist[next_u] = dist[u] + 1;\n q.push(next_u);\n }\n }\n }\n}\nvoid gridbfs(const vector<vector<char>> &G, int x, int y, vector<vector<int>> &dist) {\n int H = G.size(), W = G[0].size();\n dist[x][y] = 0;\n queue<pair<int, int>> q;\n q.push(make_pair(x, y));\n while(!q.empty()) {\n int x = q.front().first, y = q.front().second;\n q.pop();\n for(int i = 0; i < 4; i++) {\n int nx = x + dx[i], ny = y + dy[i];\n if(nx < 0 or nx >= H or ny < 0 or ny >= W) continue;\n if(dist[nx][ny] == inf and G[nx][ny] != '#') {\n dist[nx][ny] = dist[x][y] + 1;\n q.push(make_pair(nx, ny));\n }\n }\n }\n}\n//ワーシャル・フロイド\nvector<vector<ll>> floyd(vector<vector<ll>> G) {\n int N = G.size();\n auto dp = G;\n for (int k = 0; k < N; k++){\n for (int i = 0; i < N; i++) {\n for (int j = 0; j < N; j++) {\n if(dp[i][j] > dp[i][k] + dp[k][j] and dp[i][k] != INF and dp[k][j] != INF) {\n dp[i][j] = dp[i][k] + dp[k][j];\n }\n }\n }\n }\n return dp;\n}\n//サイクルがあるか判定\nbool cycle(const Graph &G, int v, vector<bool> &seen, vector<bool> &finished) {\n seen[v] = true; // 行きがけ時に true になる\n for (auto v2 : G[v]) {\n //if (v2 が逆戻りの場合) continue; // 無向グラフの場合は必要\n\n // 頂点 v2 がすでに探索済みの場合はスキップ \n if (finished[v2]) continue;\n\n // サイクル検出\n if (seen[v2] && !finished[v2]) return true;\n\n // 頂点 v2 を再帰的に探索\n if (cycle(G, v2, seen, finished)) return true;\n }\n finished[v] = true; // 帰りがけ時に true になる\n return false;\n}\n\n//Union-Find\nstruct UnionFind {\n vector<int> par; // par[i]:iの親の番号 (例) par[3] = 2 : 3の親が2\n \n UnionFind(int N) : par(N) { //最初は全てが根であるとして初期化\n for(int i = 0; i < N; i++) par[i] = i;\n }\n \n int root(int x) { // データxが属する木の根を再帰で得る:root(x) = {xの木の根}\n if (par[x] == x) return x;\n return par[x] = root(par[x]);\n }\n \n void unite(int x, int y) { // xとyの木を併合\n int rx = root(x); //xの根をrx\n int ry = root(y); //yの根をry\n if (rx == ry) return; //xとyの根が同じ(=同じ木にある)時はそのまま\n par[rx] = ry; //xとyの根が同じでない(=同じ木にない)時:xの根rxをyの根ryにつける\n }\n \n bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す\n int rx = root(x);\n int ry = root(y);\n return rx == ry;\n }\n};\n\nvoid solve() {\n}\n\nint main() {\n int t; cin >> t;\n Eratosthenes sieve(1000000);\n vll phi(1000001, 1);\n REP(i,1,1000000) {\n phi[i] = phi[i-1]+sieve.euler[i];\n }\n while(t--) {\n int n; cin >> n;\n cout << phi[n] << \"\\n\";\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 26504, "score_of_the_acc": -0.2487, "final_rank": 15 }, { "submission_id": "aoj_2286_11015246", "code_snippet": "#define _CRT_SECURE_NO_WARNINGS\n#include <cstdio>\n#include <iostream>\n#include <stack>\n#include <queue>\n#include <algorithm>\n#include <functional>\n#include <set>\n#include <map>\n#include <string>\n#include <vector>\n#include <cmath>\n#include<sstream>\n#include<list>\n#include<iomanip>\n#include <cstdlib>\n#include <cstring>\n#include <stack>\n#include <bitset>\n#include <cassert>\n#include <stdlib.h>\n#include <stdio.h>\n#include <random>\n#include <chrono>\nusing namespace std;\nconst long long LINF = 3e18 + 7;\nconst int MAX_N = 5000010;\nconst int MAX_W = 1000010;\nconst int MAX_ARRAYK = 100000;\ndouble PI = 3.14159265358979323846;\nconst int INF = 2147483647;\n//using ll = long long;\n\n// Farey Sequence\n// AOJ 2286\n// https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2286\n// https://ncastar.hatenablog.com/entry/20140320/1395323746\n// https://qiita.com/hidekikangeki/items/e2524f199ae1d483f060\n// 包除原理、オイラーのφ関数\n// https://nanikaka.hatenadiary.org/entry/20111224/1324690802\n\nlong long euler[MAX_N];\nlong long answer[MAX_N];\nvoid euler_phi2() {\n\tfor (long long i = 0; i < MAX_N; i++) {\n\t\teuler[i] = i;\n\t}\n\tfor (long long i = 2; i < MAX_N; i++) {\n\t\tif (euler[i] == i) {\n\t\t\tfor (long long j = i; j < MAX_N; j += i) {\n\t\t\t\teuler[j] = euler[j] / i * (i - 1);\n\t\t\t}\n\t\t}\n\t}\n}\n\n\nint main() {\n\n\t\n\t// Farey Sequence\n\teuler_phi2();\n\tanswer[1] = 2;\n\tfor (long long i = 2; i < MAX_N; i++) {\n\t\tanswer[i] = answer[i - 1] + euler[i];\n\t}\n\tint T, M;\n\tcin >> T;\n\tfor (int t = 0; t < T; t++) {\n\t\tcin >> M;\n\t\tcout << answer[M] << endl;\n\t}\n\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 260, "memory_kb": 81456, "score_of_the_acc": -1.1479, "final_rank": 19 }, { "submission_id": "aoj_2286_10881475", "code_snippet": "// #pragma GCC target(\"avx2\")\n// #pragma GCC optimize(\"O3\")\n// #pragma GCC optimize(\"unroll-loops\")\n#include<bits/stdc++.h>\nusing namespace std;\n\nusing ll=long long;\nusing ld=long double;\nusing pl=pair<ll,ll>;\nusing vpl=vector<pl>;\nusing vl=vector<ll>;\nusing vvl=vector<vl>;\n\n#define rep(i,n) for(long long i=0LL;i<n;i++)\n#define rrep(i,a,b) for(long long i=a;i>=b;i--)\n#define FOR(i,k,n) for(long long i=k;i<n;i++)\n#define ALL(v) v.begin(),v.end()\ntemplate<typename T>ostream&operator<<(ostream& os,vector<T>&v){for(int i = 0;i < int(size(v));i++)os << v[i] << (i+1==int(size(v))?\"\":\" \");return os;}\ntemplate<typename T>istream&operator>>(istream& is,vector<T>&v){for(auto &in : v)is >> in;return is;}\nll INF = (1LL << 60) - 6846976;\n/* INF = (1LL << 60) - 6846976*/\n\n\n// #include<atcoder/all> \n// using namespace atcoder;\n// using mint = modint998244353;ll mod = 998244353;\n// // using mint = modint1000000007;ll mod = 1000000007;\n// //using mint = modint;\n// using vm = vector<mint>;\n// using vvm = vector<vm>;\n// ostream&operator<<(ostream& os,vector<mint>&v){for(int i = 0;i < int(size(v));i++)os << v[i].val() << (i+1==int(size(v))?\"\":\" \");return os;}\n\n#define LB(A,x) int(lower_bound(ALL(A),x)-A.begin())\n\n\n// vl dx = {1,0,-1,0}, dy = {0,1,0,-1};\n// vl dx = {1,1,1,0,-1,-1,-1,0},dy={-1,0,1,1,-1,0,1,-1};\n//----------------------------------------------\n\nint main(){\n ios::sync_with_stdio(false);cin.tie(nullptr);cout<<fixed<<setprecision(15);\n//==============================================\n ll T;\n cin>>T;\n ll MAX_N=1000009;\n vector<ll> phi(MAX_N);\n iota(ALL(phi),0LL);\n vector<bool> is_prime(MAX_N,1);\n for(int p=2;p<MAX_N;p++) if(is_prime[p]){\n phi[p]/=p;\n phi[p]*=p-1;\n for(int nx=2*p;nx<=MAX_N;nx+=p){\n is_prime[nx]=0;\n phi[nx]/=p;\n phi[nx]*=p-1;\n }\n }\n\n rep(i,MAX_N-1) phi[i+1]+=phi[i];\n\n while(T--){\n ll N; cin>>N;\n cout << phi[N]+1<<endl;\n\n }\n\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 11128, "score_of_the_acc": -0.0142, "final_rank": 1 }, { "submission_id": "aoj_2286_9605472", "code_snippet": "#include <iostream>\n#include <cstdint>\n#include <vector>\n\nusing namespace std;\n\nint main()\n{\n\tcin.tie(nullptr);\n\tios::sync_with_stdio(false);\n\n\tuint16_t t, i;\n\tuint32_t n[10000];\n\tcin >> t;\n\tfor (i = 0; i != t; ++i) cin >> n[i];\n\n\tvector<uint64_t> ans(1'000'001), ex(1'000'001, 1);\n\tans[0] = 0, ans[1] = 2;\n\tfor (uint32_t i = 2; i <= 1'000'000; ++i)\n\t{\n\t\tans[i] = ans[i - 1] + (i - ex[i]);\n\t\tfor (uint32_t j = i * 2; j <= 1'000'000; j += i)\n\t\t\tex[j] += i - ex[i];\n\t}\n\n\tfor (i = 0; i != t; ++i)\n\t\tcout << ans[n[i]] << '\\n';\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 18660, "score_of_the_acc": -0.1145, "final_rank": 3 }, { "submission_id": "aoj_2286_9415424", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll=int64_t;\n\n// エラトステネスの篩\nvector<bool> Erame(vector<vector<int>> &prs,int N) {\n vector<bool> isprime(N+1, true);\n prs.resize(N+1);\n isprime[1] = false;\n for (int p = 2; p <= N; ++p) {\n if (!isprime[p]) continue;\n prs[p].push_back(p);\n for (int q = p * 2; q <= N; q += p) {\n isprime[q] = false;\n prs[q].push_back(p);\n }\n }\n return isprime;\n}\n\n\n\nint main() {\n \n int m=1000000;\n vector<vector<int>> prs;\n vector<bool> pr=Erame(prs,m);\n \n //オイラー数追加\n vector<ll> sphi(m+1);\n sphi[1]=2;\n for(int i=2;i<=m;i++){\n int phi=i;\n for(int j=0;j<(int)prs[i].size();j++){\n phi=phi/prs[i][j]*(prs[i][j]-1);\n }\n sphi[i]=sphi[i-1]+phi;\n }\n //cout << sphi[1000000] << endl;\n\n int t,n;\n cin >> t;\n for(int k=0;k<t;k++){\n cin >> n;\n cout << sphi[n] << endl; \n }\n\n}", "accuracy": 1, "time_ms": 180, "memory_kb": 66060, "score_of_the_acc": -0.8835, "final_rank": 18 }, { "submission_id": "aoj_2286_9046612", "code_snippet": "#pragma GCC optimize(\"Ofast\")\n#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n\nmt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());\nll myRand(ll B) { return (ull)rng() % B; }\n\nconst int N = 1000005;\nbool not_prime[N];\nll phi[N];\n\nvoid make_phi() {\n not_prime[0] = not_prime[1] = true;\n for (int i = 1; i < N; ++i) {\n phi[i] = i;\n }\n for (int i = 2; i < N; ++i) {\n if (!not_prime[i]) {\n phi[i] = i - 1;\n for (int j = i + i; j < N; j += i) {\n not_prime[j] = true;\n phi[j] -= phi[j] / i;\n }\n }\n }\n}\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n make_phi();\n for (int i = 1; i < N; ++i) {\n phi[i] += phi[i - 1];\n }\n int q;\n cin >> q;\n while (q--) {\n int n;\n cin >> n;\n cout << phi[n]+1 << \"\\n\";\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 12084, "score_of_the_acc": -0.0276, "final_rank": 2 }, { "submission_id": "aoj_2286_8574655", "code_snippet": "#define _CRT_SECURE_NO_WARNINGS\n#if USE_MP\n#include <boost/multiprecision/cpp_int.hpp>\n#endif\n#include <cassert>\n#include <cstdio>\n#include <iostream>\n#include <iomanip>\n#include <string>\n#include <array>\n#include <vector>\n#include <list>\n#include <map>\n#include <set>\n#include <unordered_set>\n#include <unordered_map>\n#include <queue>\n#include <stack>\n#include <functional>\n#include <algorithm>\n#include <cmath>\n#include <tuple>\n#include <bitset>\n#include <string.h>\n#include <numeric>\n#include <random>\n#if defined _DEBUG\n//#include \"TestCase.h\"\n//#include \"Util.h\"\n#endif\n\nusing namespace std;\n\nusing ll = long long;\nusing ull = unsigned long long;\ntypedef pair<int, int> pii;\ntypedef pair<ll, ll> pll;\ntypedef tuple<ll, ll, ll> tlll;\ntypedef tuple<int, int, int> tiii;\n\nusing ai2 = array<int, 2>;\nusing ai3 = array<int, 3>;\nusing ai4 = array<int, 4>;\nusing al2 = array<ll, 2>;\nusing al3 = array<ll, 3>;\nusing al4 = array<ll, 4>;\nusing ad2 = array<double, 2>;\nusing ad3 = array<double, 3>;\nusing ad4 = array<double, 4>;\n\ninline void Yes(bool upper = false) { cout << (upper ? \"YES\" : \"Yes\") << \"\\n\"; }\ninline void No(bool upper = false) { cout << (upper ? \"NO\" : \"No\") << \"\\n\"; }\n\ninline ll DivCeil(ll nume, ll deno)\n{\n\tassert(deno != 0);\n\tif (deno < 0) { nume = -nume; deno = -deno; }\n\tif (nume < 0) return -(-nume / deno);\n\telse return (nume + deno - 1) / deno;\n}\ninline ll DivFloor(ll nume, ll deno)\n{\n\tassert(deno != 0);\n\tif (deno < 0) { nume = -nume; deno = -deno; }\n\tif (nume < 0) return -((-nume + deno - 1) / deno);\n\telse return nume / deno;\n}\ninline ll DivRound(ll nume, ll deno)\n{\n\tassert(deno != 0);\n\tif (deno < 0) { nume = -nume; deno = -deno; }\n\tif (nume < 0) return -((-nume + deno / 2) / deno);\n\telse return (nume + deno / 2) / deno;\n}\n\ntemplate <typename T, int S>\nauto MakeVecImpl(queue<int>& size, const T& ini)\n{\n\tif constexpr (S == 1)\n\t{\n\t\treturn vector<T>(size.front(), ini);\n\t}\n\telse\n\t{\n\t\tint fsize = size.front();\n\t\tsize.pop();\n\t\treturn vector(fsize, MakeVecImpl<T, S - 1>(size, ini));\n\t}\n}\n\ntemplate <typename T, int S>\nauto MakeVec(const array<int, S>& size, const T ini = T())\n{\n\tqueue<int> qsize;\n\tfor (const auto v : size) qsize.push(v);\n\treturn MakeVecImpl<T, S>(qsize, ini);\n}\nint d4[][2] =\n{\n\t{0,-1},\n\t{0,1},\n\t{-1,0},\n\t{1,0},\n};\nint d8[][2] =\n{\n\t{-1,-1},\n\t{0,-1},\n\t{1,-1},\n\n\t{-1,0},\n\t{1,0},\n\n\t{-1,1},\n\t{0,1},\n\t{1,1},\n};\nvector<array<int, 2>> Neighbor4(int y, int x)\n{\n\tvector<array<int, 2>> ret;\n\tret.push_back({ y, x - 1 });\n\tret.push_back({ y, x + 1 });\n\tret.push_back({ y - 1, x });\n\tret.push_back({ y + 1, x });\n\treturn ret;\n}\nvector<array<int, 2>> Neighbor6(int y, int x)\n{\n\tvector<array<int, 2>> ret;\n\tconst int ofs = (y & 1); // 1 - (y & 1); //\n\n\tret.push_back({ y, x - 1 });\n\tret.push_back({ y, x + 1 });\n\n\tret.push_back({ y - 1, x - 1 + ofs });\n\tret.push_back({ y - 1, x + ofs });\n\n\tret.push_back({ y + 1, x - 1 + ofs });\n\tret.push_back({ y + 1, x + ofs });\n\n\treturn ret;\n}\nvector<array<int, 2>> Neighbor8(int y, int x)\n{\n\tvector<array<int, 2>> ret;\n\tret.push_back({ y - 1, x - 1 });\n\tret.push_back({ y - 1, x });\n\tret.push_back({ y - 1, x + 1 });\n\n\tret.push_back({ y, x - 1 });\n\tret.push_back({ y, x + 1 });\n\n\tret.push_back({ y + 1, x - 1 });\n\tret.push_back({ y + 1, x });\n\tret.push_back({ y + 1, x + 1 });\n\treturn ret;\n}\n\nvector<int> DecompDigit(ull val)\n{\n\tif (val == 0)\n\t\treturn { 0 };\n\n\tvector<int> ret;\n\twhile (val)\n\t{\n\t\tret.emplace_back(val % 10);\n\t\tval /= 10;\n\t}\n\treverse(ret.begin(), ret.end());\n\treturn ret;\n}\n\n\n#define PI 3.14159265358979l\nlong double R2D = 180 / PI;\nlong double D2R = PI / 180;\n\nstatic const ll Mod = 1000000007;\nstatic const ll Mod9 = 998244353;// 1000000007;\nstatic const ll INF64 = 4500000000000000000;// 10000000000000000;\nstatic const int INF32 = 2000000000;\n\nrandom_device rd;\nmt19937 mt(0);//(rd());\n#if USE_MP\nusing mpint = boost::multiprecision::cpp_int;\n#endif\n\nconst double EPS = 1e-10;\n\nvector<bool> isPrime;\t\t// 素数\nvector<int> prime;\t\t\t// 素数\nvector<int> minPF;\t\t\t// 約数である最小の素数\nvector<int> phi;\t\t\t// オイラーのφ関数\nvector<int> mobius;\t\t\t// メビウス関数\nvector<ll> phiSum{ 0 };\nvoid Eratosthenes(int SIZE=1e6)\n{\n\tisPrime.resize(SIZE, true);\n\tminPF.resize(SIZE); iota(minPF.begin(), minPF.end(), 0);\n\tphi.resize(SIZE); iota(phi.begin(), phi.end(), 0);\n\tmobius.resize(SIZE, 1);\n\n\tisPrime[0] = isPrime[1] = false;\n\n\t// 素因数\n\tfor (int i = 2; i < SIZE; i++) {\n\t\tif (!isPrime[i]) continue;\n\t\tprime.emplace_back(i);\n\t\tfor (int j = i + i; j < SIZE; j += i) {\n\t\t\tisPrime[j] = false;\n\t\t\tminPF[j] = min(minPF[j], i);\n\t\t}\n\t}\n\n\t// φ関数\n\tfor (int i = 1; i < SIZE; i++) {\n\t\tfor (int j = i + i; j < SIZE; j += i) {\n\t\t\tphi[j] -= phi[i];\n\t\t}\n\t\tphiSum.emplace_back(phiSum.back() + phi[i]);\n\t}\n\n\t// メビウス関数\n\tfor (int i = 2; i < SIZE; i++) {\n\t\tif (!isPrime[i]) continue;\n\t\tmobius[i] = -1;\n\t\tfor (int j = i + i; j < SIZE; j += i) {\n\t\t\tif ((j / i) % i == 0) mobius[j] = 0;\n\t\t\telse mobius[j] = -mobius[j];\n\t\t}\n\t}\n}\n\n// 高速ゼータ変換\nvoid FastZeta(vector<ll>& X) {\n\tint N = X.size();\n\tif (isPrime.size() < N)\n\t\tEratosthenes(N);\n\n\tfor (auto p : prime) {\n\t\tif (p >= N) break;\n\n\t\tfor (int i = (N - 1) / p; i >= 1; i--) {\n\t\t\tX[i] += X[i*p];\n\t\t}\n\t}\n}\n\n// 高速メビウス変換\nvoid FastMobius(vector<ll>& X) {\n\tint N = X.size();\n\tif (isPrime.size() < N)\n\t\tEratosthenes(N);\n\t\n\tfor (auto p : prime) {\n\t\tif (p >= N) break;\n\n\t\tfor (int i = 1; i * p <= N - 1; i++) {\n\t\t\tX[i] -= X[i * p];\n\t\t}\n\t}\n}\n\nint EulerPhi(int N) {\n\tint phi = N;\n\tfor (int i = 2; i * i <= N; i++) {\n\t\tif (N % i) continue;\n\t\tphi = phi / i * (i - 1);\n\t\twhile ((N % i) == 0) N /= i;\n\t}\n\tif (N > 1)\n\t\tphi = phi / N * (N - 1);\n\treturn phi;\n}\nstruct Solver\n{\n\tvoid Run()\n\t{\n\t\tint N;\n\t\tcin >> N;\n\t\tcout << 1+phiSum[N] << \"\\n\";\n\t}\n};\n\nint main()\n{\n\tstd::cin.tie(nullptr); //★インタラクティブ注意★\n\tstd::ios_base::sync_with_stdio(false);\n\tint T = 1;\n\tEratosthenes(1000010);\n\n\tcin >> T;\n\twhile (T--)\n\t{\n\t\tSolver S;\n\t\tS.Run();\n\t}\n\n\treturn 0;\n}\n\n/// 値渡しになっていないか?\n/// 入力を全部読んでいるか? 途中でreturnしない\n/// 32bitで収まるか? 10^5数えるとき\n/// modは正しいか?\n/// multisetでcountしていないか?", "accuracy": 1, "time_ms": 30, "memory_kb": 23736, "score_of_the_acc": -0.1979, "final_rank": 13 }, { "submission_id": "aoj_2286_7850625", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing vl = vector<ll>;\nusing vvl = vector<vl>;\nusing pl = pair<ll,ll>;\nusing vp = vector<pl>;\n#define rep(i,n) for(ll i=0;i<(ll)(n);++i)\n#define reps(i,s,n) for(ll i=(s);i<(ll)(n);++i)\n#define rep1(i,n) for(ll i=1;i<=(ll)(n);++i)\ntemplate<typename T>inline bool chmax(T &a,T b){return a<b?a=b,true:false;}\ntemplate<typename T>inline bool chmin(T &a,T b){return a>b?a=b,true:false;}\n#define fi first\n#define se second\n#define pb push_back\n#define eb emplace_back\n#define be(v) (v).begin(),(v).end()\nconst long long INF = 1e18;\n#ifdef DEBUG\n#include <debug.hpp>\n#endif\n\n\n// エラトステネスの篩\nstruct Eratosthenes {\n\tvector<bool> isprime;\n\tvector<ll> minfactor;\n\tEratosthenes(ll N) : isprime(N+1, true),\n\t\t\t\t\t\t\tminfactor(N+1, -1) {\n\t\tisprime[1] = false;\n\t\tminfactor[1] = 1;\n\t\tfor (ll p = 2; p <= N; ++p) {\n\t\t\tif (!isprime[p]) continue;\n\t\t\tminfactor[p] = p;\n\t\t\tfor (ll q = p * 2; q <= N; q += p) {\n\t\t\t\tisprime[q] = false;\n\t\t\t\tif (minfactor[q] == -1) minfactor[q] = p;\n\t\t\t}\n\t\t}\n\t}\n\tvector<pair<ll,ll>> factorize(ll n) {\n\t\tvector<pair<ll,ll>> res;\n\t\twhile (n > 1) {\n\t\t\tll p = minfactor[n];\n\t\t\tll exp = 0;\n\t\t\twhile (minfactor[n] == p) {\n\t\t\t\tn /= p;\n\t\t\t\t++exp;\n\t\t\t}\n\t\t\tres.emplace_back(p, exp);\n\t\t}\n\t\treturn res;\n\t}\n\t// 高速約数列挙\n\tvector<ll> divisors(ll n) {\n\t\tvector<ll> res({1});\n\t\tauto pf = factorize(n);\n\t\tfor (auto p : pf) {\n\t\t\tll s = (ll)res.size();\n\t\t\tfor (ll i = 0; i < s; ++i) {\n\t\t\t\tll v = 1;\n\t\t\t\tfor (ll j = 0; j < p.second; ++j) {\n\t\t\t\t\tv *= p.first;\n\t\t\t\t\tres.push_back(res[i] * v);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn res;\n\t}\n};\nll solve(){\n\t#ifdef DEBUG\n\tcout << \"--- Input ---\" << endl;\n\t#endif\n\tll N=1000000;\n\tEratosthenes er(N);\n\tvl euler(N+1);\n\trep(n,N+1){\n\t\tvp p=er.factorize(n);\n\t\teuler.at(n)=n;\n\t\tfor(auto e:p){\n\t\t\teuler.at(n) *= e.fi-1;\n\t\t\teuler.at(n) /= e.fi;\n\t\t}\n\t}\n\tvl sum(N+2);\n\trep(n,N+1){\n\t\tsum.at(n)+=euler.at(n);\n\t\tsum.at(n+1)+=sum.at(n);\n\t}\n\t#ifdef DEBUG\n\tcout<<\"N=\"<<N<<endl;\n\trep(i,100){\n\t\tcout<<\"i=\"<<i<<\" \"<<euler.at(i)<<\" \"<<sum.at(i)<<endl;\n\t}\n\tcout << \"--- Logic ---\" << endl;\n\t#endif\n\tll Q;cin>>Q;\n\trep(qi,Q){\n\t\tll n; cin>>n;\n\t\tcout<<sum.at(n)+1<<endl;\n\t}\n\t#ifdef DEBUG\n\tcout << \"--- Answer ---\" << endl;\n\t#endif\n\treturn true;\n}\nint main(){\n\t#ifdef DEBUG\n\tcout << \"--- Main ---\" << endl;\n\t#endif\n\tsolve();\n\t//while(solve());\n\treturn 0;\n}\n//cout << fixed << setprecision(9);", "accuracy": 1, "time_ms": 100, "memory_kb": 26372, "score_of_the_acc": -0.2765, "final_rank": 16 }, { "submission_id": "aoj_2286_7589131", "code_snippet": "#define _CRT_SECURE_NO_WARNINGS\n#include <cstdio>\n#include <iostream>\n#include <stack>\n#include <queue>\n#include <algorithm>\n#include <functional>\n#include <set>\n#include <map>\n#include <string>\n#include <vector>\n#include <cmath>\n#include<sstream>\n#include<list>\n#include<iomanip>\n#include <cstdlib>\n#include <cstring>\n#include <stack>\n#include <bitset>\n#include <cassert>\n#include <stdlib.h>\n#include <stdio.h>\nusing namespace std;\nconst int INF = 100000000;\nconst long long LINF = 3e18 + 7;\nconst int MAX_N = 1000010;\nconst int MAX_W = 10002;\nconst int MAX_ARRAYK = 100000;\ndouble PI = 3.14159265358979323846;\n//using ll = long long;\n\n\nlong long euler[MAX_N];\nlong long answer[MAX_N];\nvoid euler_phi2() {\n\tfor (long long i = 0; i < MAX_N; i++) {\n\t\teuler[i] = i;\n\t}\n\tfor (long long i = 2; i < MAX_N; i++) {\n\t\tif (euler[i] == i) {\n\t\t\tfor (long long j = i; j < MAX_N; j += i) {\n\t\t\t\teuler[j] = euler[j] / i * (i - 1);\n\t\t\t}\n\t\t}\n\t}\n}\n\nint main() {\n\n\t// Farey Sequence\n\teuler_phi2();\n\tanswer[1] = 2;\n\tfor (long long i = 2; i < MAX_N; i++) {\n\t\tanswer[i] = answer[i - 1] + euler[i];\n\t}\n\tint T, M;\n\tcin >> T;\n\tfor (int t = 0; t < T; t++) {\n\t\tcin >> M;\n\t\tcout << answer[M] << endl;\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 18740, "score_of_the_acc": -0.1274, "final_rank": 7 }, { "submission_id": "aoj_2286_7578208", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\n#define int long long\n#define rep(i,n) for(int i=0;i<n;++i)\n#define rep1(i,n) for(int i=1;i<=n;++i)\n#define reps(i,a,b) for(int i=a;i<b;++i)\n#define mod 998244353\n#define endl '\\n'\n#define AC ios_base::sync_with_stdio(false);cin.tie(0);\n#define INF 10e17\n\nint cnt[(int)10e6];\n\nvoid del(int pos){\n for(int i=pos*2;i<=10e6;i+=pos){\n cnt[i]-=cnt[pos];\n }\n}\n\nsigned main(){\n cnt[0]=0;\n rep1(i,(int)10e6)cnt[i]=i+1;\n rep1(i,(int)10e6)del(i);\n //累積和をとる\n rep1(i,(int)10e6)cnt[i]+=cnt[i-1];\n int t;\n cin>>t;\n rep(i,t){\n int n;\n cin>>n;\n cout<<cnt[n]<<endl;\n }\n\n \n}", "accuracy": 1, "time_ms": 1700, "memory_kb": 81136, "score_of_the_acc": -1.9955, "final_rank": 20 }, { "submission_id": "aoj_2286_7576282", "code_snippet": "#define PROBLEM \"https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2286\"\n\n#include <bits/stdc++.h>\n\ntemplate <class T> struct CumulativeSum {\n std::vector<T> seg;\n int n;\n\n CumulativeSum(int n) : n(n), seg(n + 1, 0) {}\n CumulativeSum(std::vector<T> &A) {\n n = (int)A.size();\n seg.assign(n + 1, T(0));\n for (int i = 0; i < n; i++) seg[i + 1] = seg[i] + A[i];\n }\n\n // [l, r)\n T sum(int l, int r) const {\n assert(0 <= l and l <= r and r <= n);\n return seg[r] - seg[l];\n }\n\n // A[p] = x\n void set(int p, T x) {\n assert(0 <= p and p < n);\n seg[p + 1] = x;\n }\n\n // A[p] += x\n void add(int p, T x) {\n assert(0 <= p and p < n);\n seg[p + 1] += x;\n }\n\n // A[l] += x, A[l + 1] += x, ... , A[r - 1] += x\n void imos(int l, int r, T x = T(1)) {\n add(l, x);\n add(r, -x);\n }\n\n void build() {\n for (int i = 0; i < n; i++) seg[i + 1] += seg[i];\n }\n\n T operator[](int p) const {\n assert(0 <= p and p < n);\n return seg[p + 1];\n }\n\n // output\n friend std::ostream &operator<<(std::ostream &os, const CumulativeSum &A) {\n for (int i = 0; i <= A.n; i++) os << A.seg[i] << \" \\n\"[i == A.n];\n return os;\n }\n};\n\nstd::vector<int> totient_table(int n) {\n std::vector<int> res(n + 1);\n std::iota(res.begin(), res.end(), 0);\n for (int p = 2; p <= n; p++) {\n if (res[p] != p) continue;\n for (int i = p; i <= n; i += p) {\n res[i] /= p;\n res[i] *= p - 1;\n }\n }\n return res;\n}\n\nint main() {\n auto ttt = totient_table(1000000);\n std::vector<long long> tl(ttt.size());\n for (int i = 0; i < (int)ttt.size(); i++) tl[i] = ttt[i];\n CumulativeSum<long long> rui(tl);\n int tt;\n std::cin >> tt;\n while (tt--) {\n int n;\n std::cin >> n;\n std::cout << rui.sum(0, n + 1) + 1 << '\\n';\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 22296, "score_of_the_acc": -0.1657, "final_rank": 12 }, { "submission_id": "aoj_2286_7475843", "code_snippet": "#define _CRT_SECURE_NO_WARNINGS\n#include <cstdio>\n#include <iostream>\n#include <stack>\n#include <queue>\n#include <algorithm>\n#include <functional>\n#include <set>\n#include <map>\n#include <string>\n#include <vector>\n#include <cmath>\n#include<sstream>\n#include<list>\n#include<iomanip>\n#include <cstdlib>\n#include <cstring>\n#include <stack>\n#include <bitset>\n#include <cassert>\n#include <stdlib.h>\n#include <stdio.h>\nusing namespace std;\nconst int INF = 100000000;\nconst long long LINF = 3e18 + 7;\nconst int MAX_N = 1000010;\nconst int MAX_W = 10002;\nconst int MAX_ARRAYK = 100000;\ndouble PI = 3.14159265358979323846;\n//using ll = long long;\n\n\nlong long prime[MAX_N];\nbool is_prime[MAX_N];\n\nlong long sieve(long long n) {\n\tlong long p = 0;\n\tfor (long long i = 0; i <= n; i++) {\n\t\tis_prime[i] = true;\n\t}\n\tis_prime[0] = is_prime[1] = false;\n\tfor (long long i = 2; i <= n; i++) {\n\t\tif (is_prime[i]) {\n\t\t\tprime[p++] = i;\n\t\t\tfor (long long j = 2 * i; j <= n; j += i) {\n\t\t\t\tis_prime[j] = false;\n\t\t\t}\n\t\t}\n\t}\n\n\treturn p;\n}\n\n// https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2286\n// https://ncastar.hatenablog.com/entry/20140320/1395323746\n// https://qiita.com/hidekikangeki/items/e2524f199ae1d483f060\n// 包除原理、オイラーのφ関数\n// https://nanikaka.hatenadiary.org/entry/20111224/1324690802\n\nlong long euler[MAX_N];\nlong long answer[MAX_N];\nvoid euler_phi2() {\n\tfor (long long i = 0; i < MAX_N; i++) {\n\t\teuler[i] = i;\n\t}\n\tfor (long long i = 2; i < MAX_N; i++) {\n\t\tif (euler[i] == i) {\n\t\t\tfor (long long j = i; j < MAX_N; j += i) {\n\t\t\t\teuler[j] = euler[j] / i * (i - 1);\n\t\t\t}\n\t\t}\n\t}\n}\n\nint main() {\n\n\t//long long N = sieve(100);\n\t//cout << N << endl;\n\t//for (long long i = 0; i < N; i++) {\n\t//\tcout << prime[i] << \" \";\n\t//}\n\t//cout << endl;\n\n\teuler_phi2();\n\tanswer[1] = 2;\n\tfor (long long i = 2; i < MAX_N; i++) {\n\t\tanswer[i] = answer[i - 1] + euler[i];\n\t}\n\tint T, M;\n\tcin >> T;\n\tfor (int t = 0; t < T;t++) {\n\t\tcin >> M;\n\t\tcout << answer[M] << endl;\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 18600, "score_of_the_acc": -0.1314, "final_rank": 9 }, { "submission_id": "aoj_2286_7372499", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing i32 = int;\n\ni32 main() {\n\ti32 size = 1000000;\n\tvector ps(size + 1, 0LL);\n\tfor (i32 i = 0 ; i < size + 1 ; i++) {\n\t\tps[i] = i;\n\t}\n\tfor (i32 i = 1 ; i < size + 1 ; i++) {\n\t\tfor (i32 j = i * 2 ; j < size + 1 ; j += i) {\n\t\t\tps[j] -= ps[i];\n\t\t}\n\t}\n\tvector s = { 0LL };\n\tpartial_sum(ps.begin(), ps.end(), back_inserter(s));\n\ti32 t; cin >> t;\n\tfor (i32 _ = 0 ; _ < t ; _++) {\n\t\ti32 n; cin >> n;\n\t\tcout << s.at(n + 1) + 1 << endl;\n\t}\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 18820, "score_of_the_acc": -0.1285, "final_rank": 8 }, { "submission_id": "aoj_2286_7277890", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing C = complex<double>;\nconst int mod = 998244353;\nconst long long LINF = 1001002003004005006;\nconst int INF = 1001001001;\nconst double PI = acos(-1);\nconst double EPS = 1e-10;\nconst int di[4] = {-1,0,1,0};\nconst int dj[4] = {0,-1,0,1};\nconst int dx[8] = {1,1,1,0,0,-1,-1,-1};\nconst int dy[8] = {1,0,-1,1,-1,1,0,-1};\n# define sz(x) (int)(x).size()\n# define rsz(x,n) x.resize(n)\n# define yosupo(x) {cout << (x) << endl; return 0;}\n# define ll long long\n# define fi first\n# define se second\n# define pb push_back\n# define pf push_front\n# define eb emplace_back\n# define ef emplace_front\n# define pob pop_back\n# define pof pop_front\n# define GET_MACRO(_1, _2, _3, NAME, ...) NAME\n# define _rep(i, n) _rep2(i, 0, n)\n# define _rep2(i, a, b) for(int i = (int)(a); i < (int)(b); i++)\n# define rep(...) GET_MACRO(__VA_ARGS__, _rep2, _rep)(__VA_ARGS__)\n# define srep(i, a, b) for(int i = a; i <= b; ++i)\n# define all(obj) (obj).begin(), (obj).end()\n# define rall(obj) (obj).rbegin(), (obj).rend()\ninline void YesNo(bool f) { std::cout << (f? \"Yes\": \"No\") << std::endl; }\nvoid read() {}\ntemplate <typename T, class... U> void read(T &t, U &...u) { cin >> t; read(u...); }\nvoid writeln() { cout << endl; }\ntemplate <typename T, class... U, char sep = ' '> void writeln(const T &t, const U &...u) { cout << t; if (sizeof...(u)) cout << sep; writeln(u...); }\n# define Pll pair<ll, ll>\n# define P pair<int,int>\n# define bit(x,i) (((x) >> (i)) & 1)\n# define equals(a, b) (fabs((a) - (b)) < EPS) // 誤差を考慮した同値判定\ntemplate<class T>bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; }\ntemplate<class T>bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; }\ntemplate<typename T>istream& operator>>(istream&i,vector<T>&v){rep(j,v.size())i>>v[j];return i;}\ntemplate<typename T>string join(vector<T>&v){stringstream s;rep(i,v.size())s<<' '<<v[i];return s.str().substr(1);}\ntemplate<typename T>ostream& operator<<(ostream&o,vector<T>&v){if(v.size())o<<join(v);return o;}\ntemplate<typename T>ostream& operator<<(ostream&o,vector<vector<T>>&vv){if(vv.size())o<<join(vv);return o;}\ntemplate <class T, class U> ostream& operator<<(ostream& os, const pair<T, U>& p) { return os << \"P(\" << p.first << \" \" << p.second << \")\";}\ntemplate<typename T> using vc = vector<T>;\ntemplate<typename T> using vv = vc<vc<T>>;\nusing vi = vc<int>; using vvi = vv<int>;\nusing vl = vc<ll>; using vvl = vv<ll>;\n\n#define MAX_N 1000100\n\nll euler[MAX_N], ans[MAX_N];\n\nvoid euler_phi2() {\n rep(i,MAX_N) euler[i] = i;\n rep(i,2,MAX_N) {\n if(euler[i] == i) {\n for(int j = i; j < MAX_N; j+=i) euler[j] = euler[j]*(i-1)/i;\n }\n }\n}\n\nint main() {\n euler_phi2();\n int t;\n scanf(\"%d\",&t);\n ans[1] = 2;\n rep(i,1,MAX_N-1) ans[i+1] = ans[i]+euler[i+1];\n rep(ti,t) {\n int n;\n scanf(\"%d\",&n);\n cout << ans[n] << endl;\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 18832, "score_of_the_acc": -0.1228, "final_rank": 4 }, { "submission_id": "aoj_2286_7277883", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing C = complex<double>;\nconst int mod = 998244353;\nconst long long LINF = 1001002003004005006;\nconst int INF = 1001001001;\nconst double PI = acos(-1);\nconst double EPS = 1e-10;\nconst int di[4] = {-1,0,1,0};\nconst int dj[4] = {0,-1,0,1};\nconst int dx[8] = {1,1,1,0,0,-1,-1,-1};\nconst int dy[8] = {1,0,-1,1,-1,1,0,-1};\n# define sz(x) (int)(x).size()\n# define rsz(x,n) x.resize(n)\n# define yosupo(x) {cout << (x) << endl; return 0;}\n# define ll long long\n# define fi first\n# define se second\n# define pb push_back\n# define pf push_front\n# define eb emplace_back\n# define ef emplace_front\n# define pob pop_back\n# define pof pop_front\n# define GET_MACRO(_1, _2, _3, NAME, ...) NAME\n# define _rep(i, n) _rep2(i, 0, n)\n# define _rep2(i, a, b) for(int i = (int)(a); i < (int)(b); i++)\n# define rep(...) GET_MACRO(__VA_ARGS__, _rep2, _rep)(__VA_ARGS__)\n# define srep(i, a, b) for(int i = a; i <= b; ++i)\n# define all(obj) (obj).begin(), (obj).end()\n# define rall(obj) (obj).rbegin(), (obj).rend()\ninline void YesNo(bool f) { std::cout << (f? \"Yes\": \"No\") << std::endl; }\nvoid read() {}\ntemplate <typename T, class... U> void read(T &t, U &...u) { cin >> t; read(u...); }\nvoid writeln() { cout << endl; }\ntemplate <typename T, class... U, char sep = ' '> void writeln(const T &t, const U &...u) { cout << t; if (sizeof...(u)) cout << sep; writeln(u...); }\n# define Pll pair<ll, ll>\n# define P pair<int,int>\n# define bit(x,i) (((x) >> (i)) & 1)\n# define equals(a, b) (fabs((a) - (b)) < EPS) // 誤差を考慮した同値判定\ntemplate<class T>bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; }\ntemplate<class T>bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; }\ntemplate<typename T>istream& operator>>(istream&i,vector<T>&v){rep(j,v.size())i>>v[j];return i;}\ntemplate<typename T>string join(vector<T>&v){stringstream s;rep(i,v.size())s<<' '<<v[i];return s.str().substr(1);}\ntemplate<typename T>ostream& operator<<(ostream&o,vector<T>&v){if(v.size())o<<join(v);return o;}\ntemplate<typename T>ostream& operator<<(ostream&o,vector<vector<T>>&vv){if(vv.size())o<<join(vv);return o;}\ntemplate <class T, class U> ostream& operator<<(ostream& os, const pair<T, U>& p) { return os << \"P(\" << p.first << \" \" << p.second << \")\";}\ntemplate<typename T> using vc = vector<T>;\ntemplate<typename T> using vv = vc<vc<T>>;\nusing vi = vc<int>; using vvi = vv<int>;\nusing vl = vc<ll>; using vvl = vv<ll>;\n\n#define MAX_N 1000100\n\nll euler[MAX_N], ans[MAX_N];\n\nvoid euler_Phi() {\n rep(i,MAX_N) euler[i] = i;\n rep(i,2,MAX_N) {\n if(euler[i] == i) {\n for(int j = i; j < MAX_N; j+=i) euler[j] = euler[j]*(i-1)/i;\n }\n }\n}\n\nint main() {\n euler_Phi();\n int t;\n scanf(\"%d\",&t);\n ans[1] = 2;\n rep(i,1,MAX_N-1) ans[i+1] = ans[i]+euler[i+1];\n rep(ti,t) {\n int n;\n scanf(\"%d\",&n);\n cout << ans[n] << endl;\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 18888, "score_of_the_acc": -0.1236, "final_rank": 5 }, { "submission_id": "aoj_2286_7044807", "code_snippet": "// 2021.12.2\n// #define _GLIBCXX_DEBUG\n// #define __USE_MATH_DEFINES // const double PI = acos(-1.0)\n#include <bits/stdc++.h>\nusing namespace std;\n// (*) snipet\n// type name\nusing ll = long long;\nusing pii = pair<int, int>; using pil = pair<int, ll>; using pli = pair<ll, int>; using pll = pair<ll, ll>;\nusing vi = vector<int>; using vvi = vector<vi>; using vvvi = vector<vvi>;\nusing vl = vector<ll>; using vvl = vector<vl>; using vvvl = vector<vvl>;\nusing vpii = vector<pii>; using vpil = vector<pil>; using vpli = vector<pli>; using vpll = vector<pll>;\nusing vb = vector<bool>; using vvb = vector<vb>; using vvvb = vector<vvb>;\nusing vd = vector<double>; using vvd = vector<vd>; using vvvd = vector<vvd>;\ntemplate <typename T> using pq = priority_queue<T, vector<T>>;\ntemplate <typename T> using pql = priority_queue<T, vector<T>, greater<T>>;\n// rep\n#define rep0(goal) for (int cnt = 0; cnt < int(goal); ++cnt)\n#define rep(cnt, goal) for (int cnt = 0; cnt < int(goal); ++cnt)\n#define rep2(cnt, start, goal) for (int cnt = int(start); cnt < int(goal); ++cnt)\n#define rep3(cnt, start, goal) for (int cnt = int(start); cnt > int(goal); --cnt)\n// all\n#define all(ctn) begin(ctn), end(ctn)\n// chmax, chmin\ntemplate <typename T> bool chmax(T &x, const T y) { if (x < y) { x = y; return true; } else { return false; }}\ntemplate <typename T> bool chmin(T &x, const T y) { if (x > y) { x = y; return true; } else { return false; }}\n// etc.\nvoid Yn(const bool &exp) { if (exp) cout << \"Yes\" << endl; else cout << \"No\" << endl;}\nvoid set_prec(const int &dig) { cout << fixed << setprecision(dig); cerr << fixed << setprecision(dig);}\n// template <typename T> T INF() { return numeric_limits<T>::max();}\n// template <typename T> T MIN_INF() { return numeric_limits<T>::min();}\n\n// #include <atcoder/all>\n// using namespace atcoder;\n// using mint = atcoder::modint1000000007;\n// using mint = atcoder::modint998244353;\n// // using mint = atcoder::modint;\n// void pr(const mint &MINT) { cerr << MINT.val(); } // mint\n// using pmm = pair<mint, mint>; using pim = pair<int, mint>; using pmi = pair<mint, int>; using plm = pair<ll, mint>; using pml = pair<mint, ll>;\n// using vm = vector<mint>; using vvm = vector<vm>; using vvvm = vector<vvm>;\n\n// debug\ntemplate <typename T> void pr(const T &obj) { cerr << obj; } // single\ntemplate <typename T, typename ...Ts> void pr(const T &first, const Ts &...rest) { pr(first); cerr << \", \"; pr(rest...); } // multi(plural)\ntemplate <typename S, typename T> void pr(const pair<S, T> &PAIR) { cerr << \"(\"; pr(PAIR.first); cerr << \", \"; pr(PAIR.second); cerr << \")\"; } // pair\ntemplate <typename S, typename T, typename U> void pr(const tuple<S, T, U> &trp) { cerr << \"(\"; pr(get<0>(trp)); cerr << \", \"; pr(get<1>(trp)); cerr << \", \"; pr(get<2>(trp)); cerr << \")\"; } // tuple(3)\ntemplate <typename T> void pr(const vector<T> &vec) { cerr << \"{\"; for (T obj : vec) { pr(obj); cerr << \", \"; } cerr << \"}\"; } // vector\ntemplate <typename T> void pr(const vector<vector<T>> &vv) { rep(row, vv.size()) { cerr << endl; cerr << \"[\" << row << \"]: \"; pr(vv[row]); }} // vector(multi-D)\ntemplate <typename T> void pr(const set<T> &SET) { cerr << \"{\"; for (T obj : SET) { pr(obj); cerr << \", \"; } cerr << \"}\"; } // set\ntemplate <typename T> void pr(const multiset<T> &MS) { cerr << \"{\"; for (T obj : MS) { pr(obj); cerr << \", \"; } cerr << \"}\"; } // multiset\ntemplate <typename S, typename T> void pr(const map<S, T> &MAP) { cerr << \"{\"; for (pair<S, T> p : MAP) { pr(p.first); cerr << \": \"; pr(p.second); cerr << \", \"; } cerr << \"}\";} // map\ntemplate <typename T> void pr(const queue<T> &que) { queue<T> q = que; cerr << \"{\"; while (!q.empty()) { pr(q.front()); q.pop(); cerr << \", \";} cerr << \"}\";} // queue\ntemplate <typename T> void pr(const deque<T> &deq) { deque<T> d = deq; cerr << \"{\"; while (!d.empty()) { pr(d.front()); d.pop_front(); cerr << \", \";} cerr << \"}\";} // deque\ntemplate <typename T> void pr(const stack<T> &stc) { stack<T> s = stc; vector<T> v; while (!s.empty()) { v.push_back(s.top()); s.pop();} reverse(all(v)); cerr << \"{\"; for (T obj : v) cerr << obj << \", \"; cerr << \"}\";} // stack\ntemplate <typename T> void pr(const priority_queue<T> &pq) { priority_queue<T> p = pq; cerr << \"{\"; while (!p.empty()) { pr(p.top()); p.pop(); cerr << \", \"; } cerr << \"}\";} // priority_queue\ntemplate <typename T> void pr(const priority_queue<T, vector<T>, greater<T>> &pq) { priority_queue<T, vector<T>, greater<T>> p = pq; cerr << \"{\"; while (!p.empty()) { pr(p.top()); p.pop(); cerr << \", \";} cerr << \"}\";} // priority_queue(from less)\n\n#define db(obj) cerr << #obj << \": \"; pr(obj); cerr << \" \";\n#define dl(obj) db(obj); cerr << endl;\n#define dm(...) cerr << \"(\" << #__VA_ARGS__ << \"): (\"; pr(__VA_ARGS__); cerr << \") \";\n#define dml(...) dm(__VA_ARGS__); cerr << endl;\n\n\n// global\nint t;\nvi n;\n\nvl a, b;\nconst int MAX_n = 1000000;\n\nvoid input() {\n\tcin >> t;\n\tn = vi(t);\n\trep(i, t) cin >> n[i];\n}\n\nvoid init() {\n\tinput();\n\ta = vl(MAX_n + 1);\n\tb = vl(MAX_n + 2);\n}\n\nvector<pair<ll, int>> prime_factorization(ll N) {\n\tassert(1 <= N);\n\tvector<pair<ll, int>> res;\n\tfor (ll p = 2; p * p <= N; ++ p) if (N % p == 0) {\n\t\tint d = 0;\n\t\tfor (; N % p == 0; N /= p) ++d;\n\t\tres.push_back(make_pair(p, d));\n\t}\n\tif (N > 1) res.push_back(make_pair(N, 1));\n\treturn res;\n}\n\n// オイラーのφ関数\nll phi(ll N) {\n\tfor (auto &[p, d] : prime_factorization(N)) N = N / p * (p - 1);\n\treturn N;\n}\n\nll sub_solve(ll N) {\n\tll res = 1;\n\trep2(k, 1, N + 1) res += phi(k);\n\treturn res;\n}\n\nvoid solve() {\n\ta[0] = 1;\n\trep2(N, 1, MAX_n + 1) a[N] = phi(N);\n\trep(N, MAX_n + 1) b[N + 1] = b[N] + a[N];\n\t// dl(a); dl(b); //\n\trep(i, t) cout << b[n[i] + 1] << endl;\n}\n\nvoid test() {\n}\n\nint main() {\n\tinit();\n\t// test(); //\n\tsolve();\n}", "accuracy": 1, "time_ms": 1180, "memory_kb": 18668, "score_of_the_acc": -0.8069, "final_rank": 17 }, { "submission_id": "aoj_2286_7029406", "code_snippet": "#include <iostream>\n#include <sstream>\n#include <vector>\n#include <map>\n#include <algorithm>\n#include <set>\n#include <stack>\n#include <queue>\n#include <cmath>\n#include <iomanip>\n#include <numeric>\n#include <string>\n#include <bitset>\n#include <assert.h>\n#include <functional>\n\n// #define _GLIBCXX_DEBUG\n// #define _LIBCPP_DEBUG 0\n#define rep(i, n) for (int i = 0; i < (int)(n); ++i)\n#define revrep(i, n) for(int i = (int)(n - 1); i >= 0; --i)\n#define range(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)\n#define revrange(i, s, t) for(int i = (int)(t - 1); i >= (int)(s); --i)\n#define srange(i, s, t, step) for(int i = (int)(s); i < (int)(t); i += (int)(step))\n#define popcnt(x) __builtin_popcountll(x)\n#define hayai std::ios::sync_with_stdio(false);std::cin.tie(nullptr)\n#define SORT(x) sort(x.begin(), x.end())\n#define ERASE(x) x.erase(unique(x.begin(), x.end()), x.end())\n\nusing namespace std;\nusing ll = long long;\nusing pii = pair<int, int>;\nusing pll = pair<long long, long long>;\ntemplate<class T> inline bool chmax(T& a, T b){ if (a < b){ a = b; return true; } return false; }\ntemplate<class T> inline bool chmin(T& a, T b){ if (a > b){ a = b; return true; } return false; }\ninline bool inside(int x, int y, int h, int w) { return (x >= 0 && x < h && y >= 0 && y < w); }\nint dx[] = {0, -1, 0, 1};\nint dy[] = {-1, 0, 1, 0};\n\n\n\n//https://qiita.com/drken/items/3beb679e54266f20ab63\nstruct Erastothenes {\n vector<bool> is_prime; \n vector<int> min_factor; \n vector<int> moebius; \n Erastothenes(int n): is_prime(n + 1, true),\n min_factor(n + 1, -1),\n moebius(n + 1, 1) {\n is_prime[1] = false;\n min_factor[1] = 1;\n\n for(int p = 2; p <= n; ++p) {\n if (!is_prime[p]) continue;\n min_factor[p] = p;\n moebius[p] = -1;\n for(int q = p + p; q <= n; q += p) {\n is_prime[q] = false;\n if (min_factor[q] == -1) min_factor[q] = p;\n if ((q / p) % p == 0) moebius[q] = 0;\n else moebius[q] = -moebius[q];\n }\n }\n }\n};\n\n\nconst int NMAX = 1e6+1;\nll farey_size[NMAX];\n\nvoid pre_calc() {\n Erastothenes sieve(NMAX);\n range(i, 1, NMAX) {\n srange(j, i, NMAX, i) {\n farey_size[j] += sieve.moebius[i] * (j / i);\n }\n }\n farey_size[1] = 2;\n range(i, 2, NMAX) farey_size[i] += farey_size[i-1];\n}\n\n\nint main() {\n hayai; pre_calc();\n int n;\n cin >> n;\n rep(i, n) {\n int k;\n cin >> k;\n cout << farey_size[k] << \"\\n\";\n }\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 18572, "score_of_the_acc": -0.1369, "final_rank": 10 }, { "submission_id": "aoj_2286_6985267", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\nll Euler_Phi_Function(ll v){\n ll ans=v,v2=v;\n if(v2%2==0){ans-=ans/2;while(v2%2==0)v2/=2;}\n if(v2%3==0){ans-=ans/3;while(v2%3==0)v2/=3;}\n for(ll i=5;i*i<=v2;i+=4){\n if(v2%i==0){ans-=ans/i;while(v2%i==0)v2/=i;}\n i+=2;\n if(v2%i==0){ans-=ans/i;while(v2%i==0)v2/=i;}\n }\n if(v2!=1)ans-=ans/v2;\n return ans;\n}\n\nint main(){\n vector<ll> tb(1000001);\n tb[1] = 2;\n for(int i = 2; i <= 1000000; i++){\n tb[i] = tb[i - 1] + Euler_Phi_Function(i);\n }\n int T, v;\n cin >> T;\n while(T--){\n cin >> v;\n cout << tb[v] << '\\n';\n }\n}", "accuracy": 1, "time_ms": 390, "memory_kb": 10544, "score_of_the_acc": -0.2249, "final_rank": 14 }, { "submission_id": "aoj_2286_6976894", "code_snippet": "#include <bits/stdc++.h>\n// clang-format off\n#define rep(i, s ,n) for(int i=s, i##_len=(n); i<i##_len; ++i)\ntemplate<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }\ntemplate<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }\nusing ll = long long;\n// 2^60\n#define INF (1LL << 60)\nint dx[4]={1,0,-1,0};\nint dy[4]={0,1,0,-1};\nusing namespace std;\nusing Graph = vector<vector<int>>;\ntemplate <typename T> ostream &operator<<(ostream &s, vector<vector<vector<T>>> const &v) { for (int i = 0; i < int(v.size()); ++i) { s << \"[\" << i << \"]\" << endl; s << v[i];} return s;}\ntemplate <typename T> ostream &operator<<(ostream &s, vector<vector<T>> const &v) { for (int i = 0; i < int(v.size()); ++i ){ s << v[i];} return s;}\ntemplate <typename T> ostream &operator<<(ostream &s, vector<T> const &v) { for (int i = 0; i < int(v.size()); ++i) { s << v[i]; if (i != int(v.size()) - 1) { s << \",\";}} s << endl; return s;}\n// clang-format on\n\n// 幅優先の例\n// 入力: グラフ G と,探索の始点 s\n// 出力: s から各頂点への最短路長を表す配列\nvector<int> BFS(const Graph &G, int s)\n{\n int N = (int)G.size(); // 頂点数\n // vector<bool> seen(N, false);\n vector<int> dist(N, -1); // 全頂点を「未訪問」に初期化\n queue<int> que;\n\n dist[s] = 0;\n que.push(s);\n\n while (!que.empty())\n {\n int v = que.front();\n que.pop();\n\n for (int x : G[v])\n {\n if (dist[x] != -1)\n continue;\n\n dist[x] = dist[v] + 1;\n que.push(x);\n }\n }\n return dist;\n}\n\n// エラトステネスの篩\nstruct Eratosthenes\n{\n // テーブル\n vector<bool> isprime;\n\n // 整数 i を割り切る最小の素数\n vector<int> minfactor;\n\n // メビウス関数値\n // * μ(1)=1\n // * nがある素数pで2回以上割り切れるとき、μ(n)=0\n // * n=p1×p2×…pkと素因数分解できるとき、μ(n)=(−1)^K\n vector<int> mobius;\n\n // コンストラクタで篩を回す\n Eratosthenes(int N) : isprime(N + 1, true),\n minfactor(N + 1, -1),\n mobius(N + 1, 1)\n {\n // 1 は予めふるい落としておく\n isprime[1] = false;\n minfactor[1] = 1;\n\n // 篩\n for (int p = 2; p <= N; ++p)\n {\n // すでに合成数であるものはスキップする\n if (!isprime[p])\n continue;\n\n // p についての情報更新\n minfactor[p] = p;\n mobius[p] = -1;\n\n // p 以外の p の倍数から素数ラベルを剥奪\n for (int q = p * 2; q <= N; q += p)\n {\n // q は合成数なのでふるい落とす\n isprime[q] = false;\n\n // q は p で割り切れる旨を更新\n if (minfactor[q] == -1)\n minfactor[q] = p;\n // update mobius\n if ((q / p) % p == 0)\n mobius[q] = 0;\n else\n mobius[q] = -mobius[q];\n }\n }\n }\n\n // 高速素因数分解\n // pair (素因子, 指数) の vector を返す\n vector<pair<int, int>> factorize(int n)\n {\n vector<pair<int, int>> res;\n while (n > 1)\n {\n int p = minfactor[n];\n int exp = 0;\n\n // n で割り切れる限り割る\n while (minfactor[n] == p)\n {\n n /= p;\n ++exp;\n }\n res.emplace_back(p, exp);\n }\n return res;\n }\n\n // 高速約数列挙\n // 計算量はnの約数の個数をσ(n)として、O(σ(n))となります。n≤10^9の範囲では、σ(n)≤1344(n=735134400で最大)\n vector<int> divisors(int n)\n {\n vector<int> res({1});\n\n // n を素因数分解 (メンバ関数使用)\n auto pf = factorize(n);\n\n // 約数列挙\n for (auto p : pf)\n {\n int s = (int)res.size();\n for (int i = 0; i < s; ++i)\n {\n int v = 1;\n for (int j = 0; j < p.second; ++j)\n {\n v *= p.first;\n res.push_back(res[i] * v);\n }\n }\n }\n return res;\n }\n};\n\nint main()\n{\n cout << fixed << setprecision(16);\n int t;\n cin >> t;\n\n int MAXN = 1e6;\n vector<ll> sum(MAXN + 1);\n sum[1] = 2;\n Eratosthenes er(MAXN);\n\n rep(x, 2, MAXN + 1)\n {\n // i以下の互いに素な個数\n ll cnt = x;\n for (auto [p, r] : er.factorize(x))\n {\n cnt = cnt / p * (p - 1);\n }\n sum[x] = sum[x - 1] + cnt;\n }\n\n rep(i, 0, t)\n {\n int n;\n cin >> n;\n cout << sum[n] << endl;\n }\n}", "accuracy": 1, "time_ms": 90, "memory_kb": 18804, "score_of_the_acc": -0.1638, "final_rank": 11 }, { "submission_id": "aoj_2286_6681186", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <tuple>\n#include <map>\n#include <queue>\n\nusing namespace std;\n\nusing ll = long long;\nusing pii = pair<int, int>;\tusing pll = pair<ll, ll>;\tusing pil = pair<int, ll>;\tusing pli = pair<ll, int>;\nusing vi = vector<int>;\t\tusing vvi = vector<vi>;\t\tusing vvvi = vector<vvi>;\nusing vll = vector<ll>;\t\tusing vvll = vector<vll>;\tusing vvvll = vector<vvll>;\nusing vb = vector<bool>;\tusing vvb = vector<vb>;\t\tusing vvvb = vector<vvb>;\ntemplate <class T> using pqr = priority_queue<T, vector<T>, greater<T>>;\n\nconst ll INFL = (ll)1e18;\tconst int INF = (int)1e9;\nconst int MOD = 1000000007;\n\ntemplate <class T> inline bool chmax(T& M, const T& x) { if (M < x) { M = x; return true; } return false; } // 最大値を更新(更新されたら true を返す)\ntemplate <class T> inline bool chmin(T& m, const T& x) { if (m > x) { m = x; return true; } return false; } // 最小値を更新(更新されたら true を返す)\n\n\n//ユークリッドの互除法\nll gcd(ll a, ll b) {\n\tif (b == 0) return a;\n\treturn gcd(b, a % b);\n}\n\n//拡張ユークリッドの互除法\nll extgcd(ll a, ll b, ll& x, ll& y) {\n\tll d = a;\n\tif (b != 0) {\n\t\td = extgcd(b, a % b, y, x);\n\t\ty -= (a / b) * x;\n\t}\n\telse {\n\t\tx = 1; y = 0;\n\t}\n\treturn d;\n}\n\n//素数判定O(\\sqrt(n))\nbool is_prime(int n) {\n\tfor (int i = 2; i * i <= n; i++) {\n\t\tif (n % i == 0)\n\t\t\treturn false;\n\t}\n\treturn n != 1;\n}\n\n//エラトステネスの篩\nint sieve(int n, vi& prime, vb& is_prime) {\n\tint p = 0;\n\tfor (int i = 0; i <= n; i++)\n\t\tis_prime[i] = true;\n\tis_prime[0] = is_prime[1] = false;\n\tfor (int i = 2; i <= n; i++) {\n\t\tif (is_prime[i]) {\n\t\t\tprime[p++] = i;\n\t\t\tfor (int j = 2 * i; j <= n; j += i)\n\t\t\t\tis_prime[j] = false;\n\t\t}\n\t}\n\treturn p;\n}\n\nint main()\n{\n\tconst int MAX_NUM = 1000000;\n\tint t;\n\tcin >> t;\n\n\n\t//素数とオイラー関数の計算\n\tvi prime(MAX_NUM + 1);\n\tvb is_prime(MAX_NUM + 1);\n\tvi euler(MAX_NUM + 1, 1);\n\tint p = 0;\n\t//初期化\n\tfor (int i = 0; i <= MAX_NUM; i++)\n\t\tis_prime[i] = true;\n\n\tis_prime[0] = is_prime[1] = false;\n\tfor (int i = 2; i <= MAX_NUM; i++) {\n\t\tif (is_prime[i]) {\n\t\t\tprime[p++] = i;\n\t\t\teuler[i] = i - 1;\n\t\t\tfor (int j = 2 * i; j <= MAX_NUM; j += i) {\n\t\t\t\tis_prime[j] = false;\n\t\t\t\tint tmp = j, pow = 1;\n\t\t\t\t//素因数のべきを求める\n\t\t\t\twhile (tmp % i == 0) {\n\t\t\t\t\ttmp /= i;\n\t\t\t\t\tpow *= i;\n\t\t\t\t}\n\t\t\t\t//オイラー関数の計算\n\t\t\t\teuler[j] *= pow - pow / i;\n\n\t\t\t}\n\t\t}\n\t}\n\n\n\t//数列計算\n\tvll F(MAX_NUM + 1);\n\tF[1] = 2;\n\tfor (int i = 2; i <= MAX_NUM; i++) {\n\t\tF[i] = F[i - 1] + euler[i];\n\t}\n\n\tvll ans;\n\tfor (int i = 0; i < t; i++) {\n\t\tint n;\n\t\tcin >> n;\n\t\tans.push_back(F[n]);\n\t}\n\n\n\tfor (auto a : ans)\n\t\tcout << a << endl;\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 19064, "score_of_the_acc": -0.1261, "final_rank": 6 } ]
aoj_2290_cpp
A: Attack the Moles ICPC で良い成績を収めるには修行が欠かせない.うさぎは ICPC で勝ちたいので,今日も修行をすることにした. 今日の修行は,もぐらたたきを何回も行って,反射神経と記憶力を高めようというものである.出てくるもぐらを次々に叩き,出来るだけ多くのポイントを獲得したい. もぐらが出てくる可能性のある場所は直線状に並んでいて,基準点からの距離によって座標が定まっている.うさぎはしばらく修行を続けるうちに,もぐらの出現する場所と時間が常に一緒であることに気が付いた.うさぎは,その情報をすべて記録し,コンピュータで解析を行うことにした. もぐらを叩くには,もぐらの出現位置に手を動かした後,もぐらの出てくるタイミングにぴったり合わせてもぐらを叩かなければならない.もぐらをうまく叩けると,そのもぐらに応じてポイントを得ることが出来る.もぐらを叩く動作は一瞬で行うことが出来るが,手を移動させる速さには限界がある.うさぎはもぐらを叩くにあたって左右両方の手を用いることができる.左手と右手は独立に動かすことが可能であるが,左手は常に右手より座標が小さい位置に存在しなければならない.このような条件下で,最大でどれだけのポイントが得られるかを調べたい. Input N V X Left X Right X 1 T 1 P 1 ... X N T N P N N は出てくるもぐらの数, V は手を移動させられる最大の速さ, X Left , X Right はそれぞれ,左手,右手の初期位置の座標である. X i , T i , P i はそれぞれ, i 番目のもぐらの,出現する位置の座標,ゲーム開始から出現までの時間,叩けた際に得られるポイントである. 1 ≤ N ≤ 3,000,1 ≤ V ≤ 10,000,1 ≤ X Left < X Right ≤ 100,000,1 ≤ X 1 ≤ X 2 ≤ ... ≤ X N ≤ 100,000,1 ≤ T i ≤ 100,000,1 ≤ P i ≤ 100,000 を満たす.( X i , T i ) として同一の組は複数回現れない. Output うさぎが得られる最大のポイントを 1 行に出力せよ. Sample Input 1 3 10 150 250 100 20 123 201 10 67 202 10 45 Sample Output 1 190 Sample Input 2 1 7 20 90 55 5 73 Sample Output 2 73 Sample Input 3 10 2 1000 2000 400 300 1 600 200 1 700 800 1 700 500 1 900 600 1 1000 700 1 1300 900 1 1400 400 1 1500 1000 1 2000 100 1 Sample Output 3 10
[ { "submission_id": "aoj_2290_9372895", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <map>\n#include <queue>\n#include <set>\nusing namespace std;\ntypedef long long ll;\n#define rep(i, n) for(int i = 0; i < (n); i++)\n\ntemplate<class T>\nusing vi = vector<T>;\n\ntemplate<class T>\nusing vii = vector<vi<T>>;\n\ntemplate<class T>\nusing viii = vector<vii<T>>;\n\n\ntemplate<class T>\nstruct MCF {\nprivate:\n const T inf = 1e9;\n vector<T> dist; //最短距離(費用)\n vector<int> pv, pe; //直前の頂点、辺 \n\npublic:\n struct edge {\n int to, rev; int cap; T cost;\n edge(int t = 0, int r = 0, int ca = 0, T co = 0)\n : to(t), rev(r), cap(ca), cost(co) {}\n };\n\n struct pii {\n T d; int v; //最短距離(費用)、頂点番号\n pii(T d_ = 0, int v_ = 0) : d(d_), v(v_) {}\n\n bool operator<(const pii& other) const {\n if (d != other.d) return d < other.d;\n return v < other.v;\n }\n\n bool operator>(const pii& other) const {\n if (d != other.d) return d > other.d;\n return v > other.v;\n }\n };\n\n int n;\n vector<vector<edge>> to;\n vector<T> pot; //ポテンシャル\n\n MCF(int n_ = 1) : n(n_), to(n_), pot(n_), dist(n_), pv(n_), pe(n_) {}\n\n void addedge(int u, int v, int cap, T cost) {\n int su = (int)to[u].size();\n int sv = (int)to[v].size();\n to[u].push_back({ v, sv, cap, cost });\n to[v].push_back({ u, su, 0, -cost });\n }\n\n bool bellmanford(int s, int t) {\n dist.assign(n, inf);\n dist[s] = 0;\n int cnt = 0;\n while (cnt < n) {\n bool end = true;\n for (int v = 0; v < n; v++) {\n if (dist[v] == inf) continue;\n for (auto e : to[v]) {\n if (e.cap == 0) continue;\n if (dist[v] + e.cost < dist[e.to]) {\n dist[e.to] = dist[v] + e.cost;\n end = false;\n }\n }\n }\n cerr << cnt << \" \";\n\n if (end) break;\n cnt++;\n }\n\n for (int v = 0; v < n; v++) pot[v] += dist[v];\n\n return (cnt == n);\n }\n\n //s->tへ流量fの最小費用流、流せないときは-1\n T mincostflow(int s, int t, int f) {\n T res = 0;\n pot.assign(n, 0);\n bellmanford(s, t);\n while (f) {\n priority_queue<pii, vector<pii>, greater<pii>> pq;\n dist.assign(n, inf);\n dist[s] = 0;\n pq.push({ 0, s });\n while (!pq.empty()) {\n pii elem = pq.top();\n pq.pop();\n if (dist[elem.v] < elem.d) continue;\n int sv = (int)to[elem.v].size();\n for (int i = 0; i < sv; i++) {\n edge& nv = to[elem.v][i];\n if (nv.cap == 0 || dist[nv.to] + pot[nv.to] <= dist[elem.v] + pot[elem.v] + nv.cost) continue;\n dist[nv.to] = dist[elem.v] + pot[elem.v] - pot[nv.to] + nv.cost;\n pv[nv.to] = elem.v;\n pe[nv.to] = i;\n pq.push({ dist[nv.to], nv.to });\n }\n }\n\n if (dist[t] == inf) return -1; //流せる最小費用流が存在しない\n\n for (int v = 0; v < n; v++) pot[v] += dist[v];\n\n int nf = f;\n for (int v = t; v != s; v = pv[v]) nf = min(nf, to[pv[v]][pe[v]].cap);\n f -= nf;\n res += nf * (pot[t] - pot[s]); //pot[s] = 0で不要だがポテンシャルを明確化する\n for (int v = t; v != s; v = pv[v]) {\n edge& nv = to[pv[v]][pe[v]];\n nv.cap -= nf;\n to[v][nv.rev].cap += nf;\n }\n }\n return res;\n }\n\n //s->tへ流量fの最小費用流、流せないときは費用と流せなかった流量のペア\n pair<T, int> mincostflow_pair(int s, int t, int f) {\n T res = 0;\n pot.assign(n, 0);\n while (f) {\n priority_queue<pii, vector<pii>, greater<pii>> pq;\n dist.assign(n, inf);\n dist[s] = 0;\n pq.push({ 0, s });\n while (!pq.empty()) {\n pii elem = pq.top();\n pq.pop();\n if (dist[elem.v] < elem.d) continue;\n int sv = (int)to[elem.v].size();\n for (int i = 0; i < sv; i++) {\n edge& nv = to[elem.v][i];\n if (nv.cap == 0 || dist[nv.to] + pot[nv.to] <= dist[elem.v] + pot[elem.v] + nv.cost) continue;\n dist[nv.to] = dist[elem.v] + pot[elem.v] - pot[nv.to] + nv.cost;\n pv[nv.to] = elem.v;\n pe[nv.to] = i;\n pq.push({ dist[nv.to], nv.to });\n }\n }\n\n if (dist[t] == inf) return { res, f }; //流せる最小費用流が存在しない\n\n for (int v = 0; v < n; v++) pot[v] += dist[v];\n\n long long nf = f;\n for (int v = t; v != s; v = pv[v]) nf = min(nf, to[pv[v]][pe[v]].cap);\n f -= nf;\n res += nf * (pot[t] - pot[s]); //pot[s] = 0で不要だがポテンシャルを明確化する\n for (int v = t; v != s; v = pv[v]) {\n edge& nv = to[pv[v]][pe[v]];\n nv.cap -= nf;\n to[v][nv.rev].cap += nf;\n }\n }\n return { res, f };\n }\n};\n\nvi<int> x(3010), t(3010), p(3010);\nvi<tuple<int, int, int>> xtp(3010, { 1e9, 1e9, 1e9 });\nMCF<int> mcf(6020);\n\nint main()\n{\n int n; ll v, xl, xr;\n cin >> n >> v >> xl >> xr;\n rep(i, n) cin >> x[i] >> t[i] >> p[i];\n rep(i, n) xtp[i] = { t[i], x[i], p[i] };\n sort(xtp.begin(), xtp.end());\n rep(i, n) {\n t[i] = get<0>(xtp[i]);\n x[i] = get<1>(xtp[i]);\n p[i] = get<2>(xtp[i]);\n }\n\n int S = 0, S1 = 1, S2 = 2, T = 2 * n + 3;\n\n mcf.addedge(S, S1, 1, 0);\n mcf.addedge(S, S2, 1, 0);\n\n rep(_, 2) {\n rep(i, n) {\n ll dx = abs(x[i] - xl);\n ll dt = t[i];\n if (dx <= dt * v) mcf.addedge(S1, 2 * i + 3, 1, -p[i]);\n }\n swap(xl, xr);\n swap(S1, S2);\n }\n\n rep(i, n) {\n mcf.addedge(2 * i + 3, 2 * i + 4, 1, 0);\n rep(j, n) {\n if (i == j) continue;\n if (t[i] > t[j]) continue;\n ll dx = abs(x[j] - x[i]);\n ll dt = t[j] - t[i];\n if (dx <= dt * v) mcf.addedge(2 * i + 4, 2 * j + 3, 1, -p[j]);\n }\n mcf.addedge(2 * i + 4, T, 1, 0);\n }\n\n mcf.addedge(S1, T, 1, 0);\n mcf.addedge(S2, T, 1, 0);\n\n ll ans = mcf.mincostflow(S, T, 2);\n cout << -ans << endl;\n\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 242836, "score_of_the_acc": -0.9285, "final_rank": 3 }, { "submission_id": "aoj_2290_9372891", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <map>\n#include <queue>\n#include <set>\nusing namespace std;\ntypedef long long ll;\n#define rep(i, n) for(int i = 0; i < (n); i++)\n\ntemplate<class T>\nusing vi = vector<T>;\n\ntemplate<class T>\nusing vii = vector<vi<T>>;\n\ntemplate<class T>\nusing viii = vector<vii<T>>;\n\n\ntemplate<class T>\nstruct MCF {\nprivate:\n const T inf = 1e9;\n vector<T> dist; //最短距離(費用)\n vector<int> pv, pe; //直前の頂点、辺 \n\npublic:\n struct edge {\n int to, rev; int cap; T cost;\n edge(int t = 0, int r = 0, int ca = 0, T co = 0)\n : to(t), rev(r), cap(ca), cost(co) {}\n };\n\n struct pii {\n T d; int v; //最短距離(費用)、頂点番号\n pii(T d_ = 0, int v_ = 0) : d(d_), v(v_) {}\n\n bool operator<(const pii& other) const {\n if (d != other.d) return d < other.d;\n return v < other.v;\n }\n\n bool operator>(const pii& other) const {\n if (d != other.d) return d > other.d;\n return v > other.v;\n }\n };\n\n int n;\n vector<vector<edge>> to;\n vector<T> pot; //ポテンシャル\n\n MCF(int n_ = 1) : n(n_), to(n_), pot(n_), dist(n_), pv(n_), pe(n_) {}\n\n void addedge(int u, int v, int cap, T cost) {\n int su = (int)to[u].size();\n int sv = (int)to[v].size();\n to[u].push_back({ v, sv, cap, cost });\n to[v].push_back({ u, su, 0, -cost });\n }\n\n bool bellmanford(int s, int t) {\n dist.assign(n, inf);\n dist[s] = 0;\n int cnt = 0;\n while (cnt < n) {\n bool end = true;\n for (int v = 0; v < n; v++) {\n if (dist[v] == inf) continue;\n for (auto e : to[v]) {\n if (e.cap == 0) continue;\n if (dist[v] + e.cost < dist[e.to]) {\n dist[e.to] = dist[v] + e.cost;\n end = false;\n }\n }\n }\n //cerr << cnt << \" \";\n\n if (end) break;\n cnt++;\n }\n\n for (int v = 0; v < n; v++) pot[v] += dist[v];\n\n return (cnt == n);\n }\n\n //s->tへ流量fの最小費用流、流せないときは-1\n T mincostflow(int s, int t, int f) {\n T res = 0;\n pot.assign(n, 0);\n bellmanford(s, t);\n while (f) {\n priority_queue<pii, vector<pii>, greater<pii>> pq;\n dist.assign(n, inf);\n dist[s] = 0;\n pq.push({ 0, s });\n while (!pq.empty()) {\n pii elem = pq.top();\n pq.pop();\n if (dist[elem.v] < elem.d) continue;\n int sv = (int)to[elem.v].size();\n for (int i = 0; i < sv; i++) {\n edge& nv = to[elem.v][i];\n if (nv.cap == 0 || dist[nv.to] + pot[nv.to] <= dist[elem.v] + pot[elem.v] + nv.cost) continue;\n dist[nv.to] = dist[elem.v] + pot[elem.v] - pot[nv.to] + nv.cost;\n pv[nv.to] = elem.v;\n pe[nv.to] = i;\n pq.push({ dist[nv.to], nv.to });\n }\n }\n\n if (dist[t] == inf) return -1; //流せる最小費用流が存在しない\n\n for (int v = 0; v < n; v++) pot[v] += dist[v];\n\n int nf = f;\n for (int v = t; v != s; v = pv[v]) nf = min(nf, to[pv[v]][pe[v]].cap);\n f -= nf;\n res += nf * (pot[t] - pot[s]); //pot[s] = 0で不要だがポテンシャルを明確化する\n for (int v = t; v != s; v = pv[v]) {\n edge& nv = to[pv[v]][pe[v]];\n nv.cap -= nf;\n to[v][nv.rev].cap += nf;\n }\n }\n return res;\n }\n\n //s->tへ流量fの最小費用流、流せないときは費用と流せなかった流量のペア\n pair<T, int> mincostflow_pair(int s, int t, int f) {\n T res = 0;\n pot.assign(n, 0);\n while (f) {\n priority_queue<pii, vector<pii>, greater<pii>> pq;\n dist.assign(n, inf);\n dist[s] = 0;\n pq.push({ 0, s });\n while (!pq.empty()) {\n pii elem = pq.top();\n pq.pop();\n if (dist[elem.v] < elem.d) continue;\n int sv = (int)to[elem.v].size();\n for (int i = 0; i < sv; i++) {\n edge& nv = to[elem.v][i];\n if (nv.cap == 0 || dist[nv.to] + pot[nv.to] <= dist[elem.v] + pot[elem.v] + nv.cost) continue;\n dist[nv.to] = dist[elem.v] + pot[elem.v] - pot[nv.to] + nv.cost;\n pv[nv.to] = elem.v;\n pe[nv.to] = i;\n pq.push({ dist[nv.to], nv.to });\n }\n }\n\n if (dist[t] == inf) return { res, f }; //流せる最小費用流が存在しない\n\n for (int v = 0; v < n; v++) pot[v] += dist[v];\n\n long long nf = f;\n for (int v = t; v != s; v = pv[v]) nf = min(nf, to[pv[v]][pe[v]].cap);\n f -= nf;\n res += nf * (pot[t] - pot[s]); //pot[s] = 0で不要だがポテンシャルを明確化する\n for (int v = t; v != s; v = pv[v]) {\n edge& nv = to[pv[v]][pe[v]];\n nv.cap -= nf;\n to[v][nv.rev].cap += nf;\n }\n }\n return { res, f };\n }\n};\n\nvi<int> x(3010), t(3010), p(3010);\nvi<tuple<int, int, int>> xtp(3010, { 1e9, 1e9, 1e9 });\nMCF<int> mcf(6020);\n\nint main()\n{\n int n; ll v, xl, xr;\n cin >> n >> v >> xl >> xr;\n rep(i, n) cin >> x[i] >> t[i] >> p[i];\n rep(i, n) xtp[i] = { t[i], x[i], p[i] };\n sort(xtp.begin(), xtp.end());\n rep(i, n) {\n t[i] = get<0>(xtp[i]);\n x[i] = get<1>(xtp[i]);\n p[i] = get<2>(xtp[i]);\n }\n\n int S = 0, S1 = 1, S2 = 2, T = 2 * n + 3;\n\n mcf.addedge(S, S1, 1, 0);\n mcf.addedge(S, S2, 1, 0);\n\n rep(_, 2) {\n rep(i, n) {\n ll dx = abs(x[i] - xl);\n ll dt = t[i];\n if (dx <= dt * v) mcf.addedge(S1, 2 * i + 3, 1, -p[i]);\n }\n swap(xl, xr);\n swap(S1, S2);\n }\n\n rep(i, n) {\n mcf.addedge(2 * i + 3, 2 * i + 4, 1, 0);\n rep(j, n) {\n if (i == j) continue;\n if (t[i] > t[j]) continue;\n ll dx = abs(x[j] - x[i]);\n ll dt = t[j] - t[i];\n if (dx <= dt * v) mcf.addedge(2 * i + 4, 2 * j + 3, 1, -p[j]);\n }\n mcf.addedge(2 * i + 4, T, 1, 0);\n }\n\n mcf.addedge(S1, T, 1, 0);\n mcf.addedge(S2, T, 1, 0);\n\n ll ans = mcf.mincostflow(S, T, 2);\n cout << -ans << endl;\n\n return 0;\n}", "accuracy": 1, "time_ms": 920, "memory_kb": 243460, "score_of_the_acc": -1.0815, "final_rank": 9 }, { "submission_id": "aoj_2290_8978893", "code_snippet": "#line 1 \"main.cpp\"\n#include <bits/stdc++.h>\n// #include \"Utils/FastIO.hpp\"\n#line 7 \"/home/kokoro601/compro_library/Flow/MinCostFlow.hpp\"\n\n// this implementation is based on\n// https://megalodon.jp/2022-0321-1006-53/https://ei1333.hateblo.jp:443/entry/2019/12/15/094229\n// verify:\n// https://atcoder.jp/contests/practice2/submissions/30316518\n// https://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=6331152\n\ntemplate<class CapType, class CostType> class MinCostFlow {\n struct Edge {\n int to, rev;\n CapType cap;\n CostType cost;\n };\n\n struct EdgeInfo {\n int v, offset;\n };\n\n using P = std::pair<CostType, int>;\n\n int n;\n const CostType inf;\n std::vector<CostType> h, dist;\n std::vector<int> prevv, preve;\n std::vector<CapType> b;\n std::vector<EdgeInfo> edge_infos;\n CostType res = 0;\n\npublic:\n std::vector< std::vector<Edge> > g;\n\n MinCostFlow(int node_size) : \n n(node_size), inf(std::numeric_limits<CostType>::max()),\n g(n), h(n, 0), dist(n), prevv(n), preve(n), b(n) {}\n\n CostType solve(int s, int t, CapType f) {\n while(f > 0) {\n std::priority_queue<P, std::vector<P>, std::greater<P> > que;\n std::fill(dist.begin(), dist.end(), inf);\n dist[s] = 0;\n que.push({0, s});\n while(!que.empty()) {\n P p = que.top(); que.pop();\n int v = p.second;\n if (dist[v] < p.first) continue;\n for (int i = 0; i < (int)g[v].size(); i++) {\n Edge &e = g[v][i];\n CostType diff = e.cost + h[v] - h[e.to];\n if (e.cap > 0 && dist[e.to] > dist[v] + diff) {\n dist[e.to] = dist[v] + diff;\n prevv[e.to] = v, preve[e.to] = i;\n que.push({dist[e.to], e.to});\n }\n }\n }\n\n if (dist[t] == inf) {\n std::cout << \"-1\" << std::endl;\n exit(0);\n }\n\n for (int i = 0; i < n; i++) h[i] += dist[i];\n\n CapType d = f;\n for (int v = t; v != s; v = prevv[v]) {\n d = std::min(d, g[prevv[v]][preve[v]].cap);\n }\n\n f -= d;\n res += h[t] * d;\n for (int v = t; v != s; v = prevv[v]) {\n Edge& e = g[prevv[v]][preve[v]];\n e.cap -= d;\n g[v][e.rev].cap += d;\n }\n }\n\n return res;\n }\n\n void set_potential(std::vector<CostType> &potential) {\n h = potential;\n }\n\n void add_edge(int from, int to, CapType cap, CostType cost) {\n assert(from != to); \n g[from].push_back({to, (int)g[to].size(), cap, cost});\n g[to].push_back({from, (int)g[from].size() - 1, 0, -cost});\n edge_infos.push_back({to, (int)g[to].size() - 1});\n }\n\n CapType get_edge(int i) {\n EdgeInfo &ei = edge_infos[i];\n Edge &e = g[ei.v][ei.offset];\n CapType ret = e.cap;\n return ret;\n }\n};\n#line 4 \"/home/kokoro601/compro_library/Graph/SCC.hpp\"\n\n// verify:\n// https://judge.yosupo.jp/submission/83473\n// https://atcoder.jp/contests/arc010/submissions/30376565\n\n\nclass SCC {\n using Graph = std::vector< std::vector<int> >;\n\n void dfs(int v) {\n used[v] = true;\n for (auto &to: g[v]) {\n if (!used[to]) dfs(to);\n }\n ord.push_back(v);\n }\n\n void rdfs(int v, int group_id) {\n used[v] = true;\n cmp[v] = group_id;\n for (auto &to: rg[v]) {\n if (!used[to]) rdfs(to, group_id);\n }\n }\n\n void build() {\n for (int v = 0; v < n; v++)\n if (!used[v]) dfs(v);\n\n std::fill(used.begin(), used.end(), 0);\n\n std::reverse(ord.begin(), ord.end());\n for (auto &v: ord)\n if (!used[v]) rdfs(v, n_group++);\n }\n\n int n;\n Graph g, rg;\n std::vector<bool> used;\n std::vector<int> ord;\npublic:\n int n_group;\n std::vector<int> cmp;\n\n SCC(Graph &g): n((int)g.size()), g(g), used(n), n_group(0), cmp(n){\n rg.resize(n);\n for (int i = 0; i < n; i++)\n for (auto to: g[i]) \n rg[to].push_back(i);\n build();\n }\n\n std::vector< std::vector<int> > make_group_list() {\n std::vector< std::vector<int> > group_list(n_group);\n for (int i = 0; i < n; i++) group_list[cmp[i]].push_back(i);\n return group_list;\n }\n\n Graph rebuild() {\n Graph ret_g(n_group);\n auto group_list = make_group_list();\n\n std::vector<bool> marked_fl(n_group, false);\n for (int i = 0; i < n_group; i++) {\n std::vector<int> rec_marked_group;\n for (auto &v: group_list[i]) {\n for (auto &to: g[v]) {\n int to_group_id = cmp[to];\n if (to_group_id != i && !marked_fl[to_group_id]) {\n marked_fl[to_group_id] = true;\n rec_marked_group.push_back(to_group_id);\n ret_g[i].push_back(to_group_id);\n }\n }\n }\n for (auto &group_id: rec_marked_group) marked_fl[group_id] = false;\n }\n\n return ret_g;\n }\n};\n#line 5 \"main.cpp\"\n\nusing namespace std;\nusing ull = unsigned long long;\nusing ll = long long;\nusing Graph = vector<vector<int>>;\n// using u128 = __uint128_t; \nusing u64 = uint64_t;\n\n\n// vector<Mint> inv(vector<Mint> &v) {\n// size_t n = v.size();\n\n// vector<Mint> ret = { modpow(v[0].v, MOD-2, MOD) };\n// vector<Mint> cur = { v[0] }; \n// size_t cursz = 1;\n// while (cursz < n) {\n// cur.resize(2*cursz); memcpy(cur.data() + cursz, v.data() + cursz, sizeof(unsigned int) * min(n-cursz, cursz));\n// auto tmp = ntt.convolution(cur, ret);\n// tmp[0] = Mint(2) - tmp[0];\n// for (int i = 1; i < tmp.size(); i++) tmp[i] *= -1;\n// tmp.resize(2*cursz);\n// ret = ntt.convolution(ret, tmp);\n// ret.resize(2*cursz);\n// cursz *= 2;\n// }\n\n// ret.resize(n);\n// return ret;\n// }\n\ntypedef struct {\n int x, t, p;\n} Item;\n\nint main() {\n int n, v; cin >> n >> v;\n int xleft, xright; cin >> xleft >> xright;\n\n vector<Item> items(n);\n for (int i = 0; i < n; i++) cin >> items[i].x >> items[i].t >> items[i].p;\n\n // 時間でソート\n sort(items.begin(), items.end(), [](auto &a, auto &b) { return a.t < b.t; }); \n\n int s = 2*n, t = 2*n + 1;\n MinCostFlow<int, int> mcf(2*n + 2);\n mcf.add_edge(s, t, 2, 0);\n for (int i = 0; i < n; i++) { // s ⇒ 各頂点\n if (abs(xleft - items[i].x) <= items[i].t * v)\n mcf.add_edge(s, i, 1, 0);\n if (abs(xright - items[i].x) <= items[i].t * v)\n mcf.add_edge(s, i, 1, 0);\n }\n for (int i = 0; i < n; i++) // 各頂点 ⇒ t\n mcf.add_edge(i+n, t, 1, 0);\n for (int i = 0; i < n; i++)\n mcf.add_edge(i, i+n, 1, -items[i].p); // ポイント\n \n for (int i = 0; i < n; i++) \n for (int j = i+1; j < n; j++) \n if (abs(items[i].x - items[j].x) <= v * (items[j].t - items[i].t))\n mcf.add_edge(i+n, j, 1, 0);\n\n vector<int> potential(2*n + 2, 1e9);\n potential[s] = 0;\n for (auto e: mcf.g[s])\n if (e.cap != 0)\n potential[e.to] = min(potential[e.to], potential[s] + e.cost); \n for (int i = 0; i < n; i++) {\n for (auto e: mcf.g[i]) \n if (e.cap != 0)\n potential[e.to] = min(potential[e.to], potential[i] + e.cost);\n for (auto e: mcf.g[i+n]) \n if (e.cap != 0)\n potential[e.to] = min(potential[e.to], potential[i+n] + e.cost);\n }\n\n mcf.set_potential(potential);\n\n cout << -mcf.solve(s, t, 2) << \"\\n\";\n}", "accuracy": 0.2653061224489796, "time_ms": 110, "memory_kb": 120300, "score_of_the_acc": -0.2921, "final_rank": 19 }, { "submission_id": "aoj_2290_8877953", "code_snippet": "#line 1 \"main.cpp\"\n#include <iostream>\n#include <algorithm>\n#include <vector>\n#include <iomanip>\n#include <math.h>\n#include <functional>\n#include <map>\n#include <set>\n#include <string>\n#include <unordered_map>\n#include <unordered_set>\n#include <queue>\n#include <stack>\n#include <cstring>\n#include <assert.h>\n#include <unistd.h>\n#include <chrono>\n#include <numeric>\n#include <cstdint>\n#include <variant>\n#include <random>\n#include <memory>\n// #include <bit>\n// #include \"Utils/FastIO.hpp\"\n#line 5 \"/home/kokoro601/compro_library/Flow/MinCostFlow.hpp\"\n#include <limits>\n#line 7 \"/home/kokoro601/compro_library/Flow/MinCostFlow.hpp\"\n\n// this implementation is based on\n// https://megalodon.jp/2022-0321-1006-53/https://ei1333.hateblo.jp:443/entry/2019/12/15/094229\n// verify:\n// https://atcoder.jp/contests/practice2/submissions/30316518\n// https://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=6331152\n\n\n\n\ntemplate<class CapType, class CostType> class MinCostFlow {\n struct Edge {\n int to, rev;\n CapType cap;\n CostType cost;\n };\n\n struct EdgeInfo {\n int v, offset;\n };\n\n using P = std::pair<CostType, int>;\n\n int n;\n const CostType inf;\n std::vector<CostType> h, dist;\n std::vector<int> prevv, preve;\n std::vector<CapType> b;\n std::vector<EdgeInfo> edge_infos;\n CostType res = 0;\n\npublic:\n // std::vector< std::vector<Edge> > g;\n Edge *g[7000];\n short hi[7000] = {}, lim[7000] = {};\n\n MinCostFlow(int node_size) : \n n(node_size), inf(std::numeric_limits<CostType>::max()),\n h(n, 0), dist(n), prevv(n), preve(n), b(n) {}\n\n CostType solve(int s, int t, CapType f) {\n while(f > 0) {\n std::priority_queue<P, std::vector<P>, std::greater<P> > que;\n std::fill(dist.begin(), dist.end(), inf);\n dist[s] = 0;\n que.push({0, s});\n while(!que.empty()) {\n P p = que.top(); que.pop();\n int v = p.second;\n if (dist[v] < p.first) continue;\n for (int i = 0; i < (int)hi[v]; i++) {\n Edge &e = g[v][i];\n CostType diff = e.cost + h[v] - h[e.to];\n if (e.cap > 0 && dist[e.to] > dist[v] + diff) {\n dist[e.to] = dist[v] + diff;\n prevv[e.to] = v, preve[e.to] = i;\n que.push({dist[e.to], e.to});\n }\n }\n }\n\n if (dist[t] == inf) {\n std::cout << \"-1\" << std::endl;\n exit(0);\n }\n\n for (int i = 0; i < n; i++) h[i] += dist[i];\n\n CapType d = f;\n for (int v = t; v != s; v = prevv[v]) {\n d = std::min(d, g[prevv[v]][preve[v]].cap);\n }\n\n f -= d;\n res += h[t] * d;\n for (int v = t; v != s; v = prevv[v]) {\n Edge& e = g[prevv[v]][preve[v]];\n e.cap -= d;\n g[v][e.rev].cap += d;\n }\n }\n\n return res;\n }\n\n void set_potential(std::vector<CostType> &potential) {\n h = potential;\n }\n\n void add_edge(int from, int to, CapType cap, CostType cost) {\n assert(from != to); \n\n if (lim[from] == hi[from]) {\n lim[from] += 8;\n g[from] = (Edge*)realloc(g[from], sizeof(Edge) * lim[from]);\n }\n if (lim[to] == hi[to]) {\n lim[to] += 8;\n g[to] = (Edge*)realloc(g[to], sizeof(Edge) * lim[to]);\n }\n\n g[from][hi[from]++] = {to, (int)hi[to], cap, cost};\n g[to][hi[to]++] = {from, (int)hi[from] - 1, 0, -cost};\n // edge_infos.push_back({to, (int)g[to].size() - 1});\n }\n\n CapType get_edge(int i) {\n EdgeInfo &ei = edge_infos[i];\n Edge &e = g[ei.v][ei.offset];\n CapType ret = e.cap;\n return ret;\n }\n};\n#line 26 \"main.cpp\"\n\nusing namespace std;\nusing ull = unsigned long long;\nusing ll = long long;\nusing Graph = vector<vector<int>>;\nusing u128 = __uint128_t; \nusing u64 = uint64_t;\n\n// 番兵は入っているとする\n// ll garner(vector<ll> &b, vector<ll> &m) {\n// vector<ll> coeffs(m.size(), 1);\n// vector<ll> constants(m.size(), 0);\n// for (size_t k = 0; k < b.size(); k++) {\n// ll diff = (b[k] > constants[k]) ? (b[k] - constants[k]) : (b[k] - constants[k] + m[k]);\n// ll t = (diff * modinv(coeffs[k], m[k])) % m[k];\n// for (size_t i = k + 1; i < m.size(); i++) {\n// (constants[i] += t * coeffs[i]) %= m[i];\n// (coeffs[i] *= m[k]) %= m[i];\n// }\n// }\n\n// return constants.back();\n// }\n\nstruct Item {\n int x, t, p;\n};\n\nint main() {\n int n, v, xl, xr;\n cin >> n >> v >> xl >> xr;\n vector<Item> items(n);\n for (int i = 0; i < n; i++) cin >> items[i].x >> items[i].t >> items[i].p;\n items.push_back({xl, 0, 0});\n items.push_back({xr, 0, 0});\n // 時間でソート\n sort(items.begin(), items.end(), [](auto &a, auto &b) { return a.t < b.t; });\n\n n += 2;\n\n MinCostFlow<int, int> mcf(2*n+2);\n int s = 2*n, t = 2*n+1;\n mcf.add_edge(s, 0, 1, 0), mcf.add_edge(s, 1, 1, 0);\n for (int i = 0; i < n; i++) mcf.add_edge(i, i+n, 1, -items[i].p);\n for (int i = 0; i < n; i++) mcf.add_edge(i+n, t, 1, 0);\n for (int i = 0; i < n; i++) {\n for (int j = i+1; j < n; j++) {\n // 到達可能\n if (abs(items[i].x - items[j].x) <= v * (items[j].t - items[i].t)) {\n mcf.add_edge(i+n, j, 1, 0);\n }\n }\n }\n\n vector<int> potential(2*n+2, 1e9), top(2*n+2);\n top[0] = s;\n for (int i = 0; i < n; i++) top[2*i + 1] = i, top[2*i + 2] = i+n; \n top[2*n+1] = t;\n\n potential[s] = 0;\n for (auto cur: top) {\n for (int i = 0; i < mcf.hi[cur]; i++) {\n auto e = mcf.g[cur][i];\n if (e.cap == 0) continue;\n potential[e.to] = min(potential[e.to], potential[cur] + e.cost);\n }\n }\n\n mcf.set_potential(potential);\n\n cout << -mcf.solve(s, t, 2) << \"\\n\";\n}", "accuracy": 1, "time_ms": 1050, "memory_kb": 240792, "score_of_the_acc": -1.0895, "final_rank": 13 }, { "submission_id": "aoj_2290_8877946", "code_snippet": "#line 1 \"main.cpp\"\n#include <iostream>\n#include <algorithm>\n#include <vector>\n#include <iomanip>\n#include <math.h>\n#include <functional>\n#include <map>\n#include <set>\n#include <string>\n#include <unordered_map>\n#include <unordered_set>\n#include <queue>\n#include <stack>\n#include <cstring>\n#include <assert.h>\n#include <unistd.h>\n#include <chrono>\n#include <numeric>\n#include <cstdint>\n#include <variant>\n#include <random>\n#include <memory>\n// #include <bit>\n// #include \"Utils/FastIO.hpp\"\n#line 5 \"/home/kokoro601/compro_library/Flow/MinCostFlow.hpp\"\n#include <limits>\n#line 7 \"/home/kokoro601/compro_library/Flow/MinCostFlow.hpp\"\n\n// this implementation is based on\n// https://megalodon.jp/2022-0321-1006-53/https://ei1333.hateblo.jp:443/entry/2019/12/15/094229\n// verify:\n// https://atcoder.jp/contests/practice2/submissions/30316518\n// https://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=6331152\n\n\n\n\ntemplate<class CapType, class CostType> class MinCostFlow {\n struct Edge {\n int to, rev;\n CapType cap;\n CostType cost;\n };\n\n struct EdgeInfo {\n int v, offset;\n };\n\n using P = std::pair<CostType, int>;\n\n int n;\n const CostType inf;\n std::vector<CostType> h, dist;\n std::vector<int> prevv, preve;\n std::vector<CapType> b;\n std::vector<EdgeInfo> edge_infos;\n CostType res = 0;\n\npublic:\n // std::vector< std::vector<Edge> > g;\n Edge *g[7000];\n int hi[7000] = {}, lim[7000] = {};\n\n MinCostFlow(int node_size) : \n n(node_size), inf(std::numeric_limits<CostType>::max()),\n h(n, 0), dist(n), prevv(n), preve(n), b(n) {}\n\n CostType solve(int s, int t, CapType f) {\n while(f > 0) {\n std::priority_queue<P, std::vector<P>, std::greater<P> > que;\n std::fill(dist.begin(), dist.end(), inf);\n dist[s] = 0;\n que.push({0, s});\n while(!que.empty()) {\n P p = que.top(); que.pop();\n int v = p.second;\n if (dist[v] < p.first) continue;\n for (int i = 0; i < (int)hi[v]; i++) {\n Edge &e = g[v][i];\n CostType diff = e.cost + h[v] - h[e.to];\n if (e.cap > 0 && dist[e.to] > dist[v] + diff) {\n dist[e.to] = dist[v] + diff;\n prevv[e.to] = v, preve[e.to] = i;\n que.push({dist[e.to], e.to});\n }\n }\n }\n\n if (dist[t] == inf) {\n std::cout << \"-1\" << std::endl;\n exit(0);\n }\n\n for (int i = 0; i < n; i++) h[i] += dist[i];\n\n CapType d = f;\n for (int v = t; v != s; v = prevv[v]) {\n d = std::min(d, g[prevv[v]][preve[v]].cap);\n }\n\n f -= d;\n res += h[t] * d;\n for (int v = t; v != s; v = prevv[v]) {\n Edge& e = g[prevv[v]][preve[v]];\n e.cap -= d;\n g[v][e.rev].cap += d;\n }\n }\n\n return res;\n }\n\n void set_potential(std::vector<CostType> &potential) {\n h = potential;\n }\n\n void add_edge(int from, int to, CapType cap, CostType cost) {\n assert(from != to); \n\n if (lim[from] == hi[from]) {\n lim[from] += 8;\n g[from] = (Edge*)realloc(g[from], sizeof(Edge) * lim[from]);\n }\n if (lim[to] == hi[to]) {\n lim[to] += 8;\n g[to] = (Edge*)realloc(g[to], sizeof(Edge) * lim[to]);\n }\n\n g[from][hi[from]++] = {to, (int)hi[to], cap, cost};\n g[to][hi[to]++] = {from, (int)hi[from] - 1, 0, -cost};\n // edge_infos.push_back({to, (int)g[to].size() - 1});\n }\n\n CapType get_edge(int i) {\n EdgeInfo &ei = edge_infos[i];\n Edge &e = g[ei.v][ei.offset];\n CapType ret = e.cap;\n return ret;\n }\n};\n#line 26 \"main.cpp\"\n\nusing namespace std;\nusing ull = unsigned long long;\nusing ll = long long;\nusing Graph = vector<vector<int>>;\nusing u128 = __uint128_t; \nusing u64 = uint64_t;\n\n// 番兵は入っているとする\n// ll garner(vector<ll> &b, vector<ll> &m) {\n// vector<ll> coeffs(m.size(), 1);\n// vector<ll> constants(m.size(), 0);\n// for (size_t k = 0; k < b.size(); k++) {\n// ll diff = (b[k] > constants[k]) ? (b[k] - constants[k]) : (b[k] - constants[k] + m[k]);\n// ll t = (diff * modinv(coeffs[k], m[k])) % m[k];\n// for (size_t i = k + 1; i < m.size(); i++) {\n// (constants[i] += t * coeffs[i]) %= m[i];\n// (coeffs[i] *= m[k]) %= m[i];\n// }\n// }\n\n// return constants.back();\n// }\n\nstruct Item {\n int x, t, p;\n};\n\nint main() {\n int n, v, xl, xr;\n cin >> n >> v >> xl >> xr;\n vector<Item> items(n);\n for (int i = 0; i < n; i++) cin >> items[i].x >> items[i].t >> items[i].p;\n items.push_back({xl, 0, 0});\n items.push_back({xr, 0, 0});\n // 時間でソート\n sort(items.begin(), items.end(), [](auto &a, auto &b) { return a.t < b.t; });\n\n n += 2;\n\n MinCostFlow<int, int> mcf(2*n+2);\n int s = 2*n, t = 2*n+1;\n mcf.add_edge(s, 0, 1, 0), mcf.add_edge(s, 1, 1, 0);\n for (int i = 0; i < n; i++) mcf.add_edge(i, i+n, 1, -items[i].p);\n for (int i = 0; i < n; i++) mcf.add_edge(i+n, t, 1, 0);\n for (int i = 0; i < n; i++) {\n for (int j = i+1; j < n; j++) {\n // 到達可能\n if (abs(items[i].x - items[j].x) <= v * (items[j].t - items[i].t)) {\n mcf.add_edge(i+n, j, 1, 0);\n }\n }\n }\n\n vector<int> potential(2*n+2, 1e9), top(2*n+2);\n top[0] = s;\n for (int i = 0; i < n; i++) top[2*i + 1] = i, top[2*i + 2] = i+n; \n top[2*n+1] = t;\n\n potential[s] = 0;\n for (auto cur: top) {\n for (int i = 0; i < mcf.hi[cur]; i++) {\n auto e = mcf.g[cur][i];\n if (e.cap == 0) continue;\n potential[e.to] = min(potential[e.to], potential[cur] + e.cost);\n }\n }\n\n mcf.set_potential(potential);\n\n cout << -mcf.solve(s, t, 2) << \"\\n\";\n}", "accuracy": 1, "time_ms": 1070, "memory_kb": 241564, "score_of_the_acc": -1.097, "final_rank": 15 }, { "submission_id": "aoj_2290_8334069", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nconst int INF = 1012345678;\n\nint mincostflow(int N, int S, int T, int F, vector<vector<char> >& cap, vector<vector<int> >& cost) {\n\tint ans = 0;\n\tvector<int> height(N, 0); // potential\n\tfor (int rep = 0; rep < F; rep++) {\n\t\tvector<int> dist(N, INF);\n\t\tvector<int> pre(N, -1);\n\t\tvector<bool> vis(N, false);\n\t\tdist[S] = 0;\n\t\twhile (true) {\n\t\t\tint pos = -1;\n\t\t\tfor (int i = 0; i < N; i++) {\n\t\t\t\tif (!vis[i] && dist[i] != INF && (pos == -1 || dist[pos] > dist[i])) {\n\t\t\t\t\tpos = i;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (pos == -1) {\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tvis[pos] = true;\n\t\t\tfor (int i = 0; i < N; i++) {\n\t\t\t\tint ndist = dist[pos] + cost[pos][i] - (height[i] - height[pos]);\n\t\t\t\tif (cap[pos][i] > 0 && dist[i] > ndist) {\n\t\t\t\t\tdist[i] = ndist;\n\t\t\t\t\tpre[i] = pos;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif (dist[T] == INF) {\n\t\t\treturn INF;\n\t\t}\n\t\tans += dist[T] + height[T];\n\t\tint pos = T;\n\t\twhile (pos != S) {\n\t\t\tint nxt = pre[pos];\n\t\t\tcap[nxt][pos] -= 1;\n\t\t\tcap[pos][nxt] += 1;\n\t\t\tpos = nxt;\n\t\t}\n\t\tfor (int i = 0; i < N; i++) {\n\t\t\tif (height[i] == INF || dist[i] == INF) {\n\t\t\t\theight[i] = INF;\n\t\t\t}\n\t\t\telse {\n\t\t\t\theight[i] += dist[i];\n\t\t\t}\n\t\t}\n\t}\n\treturn ans;\n}\n\nstruct enemy {\n\tint x, t, p;\n};\n\nint main() {\n\tint N, V, XL, XR;\n\tcin >> N >> V >> XL >> XR;\n\tvector<enemy> E(N);\n\tfor (int i = 0; i < N; i++) {\n\t\tcin >> E[i].x >> E[i].t >> E[i].p;\n\t}\n\tsort(E.begin(), E.end(), [&](const enemy& e1, const enemy& e2) {\n\t\treturn e1.t < e2.t;\n\t});\n\tvector<int> ps(N + 1);\n\tfor (int i = 0; i < N; i++) {\n\t\tps[i + 1] = ps[i] + E[i].p;\n\t}\n\tauto psum = [&](int l, int r) -> int {\n\t\treturn ps[r] - ps[l];\n\t};\n\tauto reachable = [&](int xa, int ta, int xb, int tb) -> bool {\n\t\treturn (ta < tb && abs(xa - xb) <= V * (tb - ta));\n\t};\n\tvector<vector<char> > cap(N * 2 + 4, vector<char>(N * 2 + 4));\n\tvector<vector<int> > cost(N * 2 + 4, vector<int>(N * 2 + 4));\n\tauto add_edge = [&](int va, int vb, int cap_, int cost_) -> void {\n\t\tcap[va][vb] = cap_;\n\t\tcap[vb][va] = 0;\n\t\tcost[va][vb] = cost_;\n\t\tcost[vb][va] = -cost_;\n\t};\n\tfor (int i = 0; i < N; i++) {\n\t\tadd_edge(i, N + i, 1, 0);\n\t}\n\tfor (int i = 0; i < N; i++) {\n\t\tfor (int j = i + 1; j < N; j++) {\n\t\t\tif (reachable(E[i].x, E[i].t, E[j].x, E[j].t)) {\n\t\t\t\tadd_edge(N + i, j, 1, psum(i + 1, j));\n\t\t\t}\n\t\t}\n\t}\n\tfor (int i = 0; i < N; i++) {\n\t\tif (reachable(XL, 0, E[i].x, E[i].t)) {\n\t\t\tadd_edge(2 * N + 0, i, 1, psum(0, i));\n\t\t}\n\t\tif (reachable(XR, 0, E[i].x, E[i].t)) {\n\t\t\tadd_edge(2 * N + 1, i, 1, psum(0, i));\n\t\t}\n\t\tadd_edge(N + i, 2 * N + 3, 1, psum(i + 1, N));\n\t}\n\tadd_edge(2 * N + 2, 2 * N + 0, 1, 0);\n\tadd_edge(2 * N + 2, 2 * N + 1, 1, 0);\n\tadd_edge(2 * N + 0, 2 * N + 3, 1, psum(0, N));\n\tadd_edge(2 * N + 1, 2 * N + 3, 1, psum(0, N));\n\tint ans = mincostflow(2 * N + 4, 2 * N + 2, 2 * N + 3, 2, cap, cost);\n\tcout << psum(0, N) * 2 - ans << endl;\n\treturn 0;\n}", "accuracy": 1, "time_ms": 290, "memory_kb": 179712, "score_of_the_acc": -0.6371, "final_rank": 1 }, { "submission_id": "aoj_2290_6027738", "code_snippet": "//#pragma GCC optimize(\"O3\")\n//#pragma GCC optimize(\"unroll-loops\")\n#include<iostream>\n#include<string>\n#include<cstdio>\n#include<vector>\n#include<cmath>\n#include<algorithm>\n#include<functional>\n#include<iomanip>\n#include<queue>\n#include<ciso646>\n#include<random>\n#include<map>\n#include<set>\n#include<bitset>\n#include<stack>\n#include<unordered_map>\n#include<unordered_set>\n#include<utility>\n#include<cassert>\n#include<complex>\n#include<numeric>\n#include<array>\nusing namespace std;\n\n//#define int long long\ntypedef long long ll;\n\ntypedef unsigned long long ul;\ntypedef unsigned int ui;\nconst ll mod = 998244353;\nconst ll INF = mod * mod;\ntypedef pair<int, int>P;\n\n#define rep(i,n) for(int i=0;i<n;i++)\n#define per(i,n) for(int i=n-1;i>=0;i--)\n#define Rep(i,sta,n) for(int i=sta;i<n;i++)\n#define rep1(i,n) for(int i=1;i<=n;i++)\n#define per1(i,n) for(int i=n;i>=1;i--)\n#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)\n#define all(v) (v).begin(),(v).end()\ntypedef pair<ll, ll> LP;\ntypedef long double ld;\ntypedef pair<ld, ld> LDP;\nconst ld eps = 1e-8;\nconst ld pi = acosl(-1.0);\n\nll mod_pow(ll x, ll n, ll m = mod) {\n\tif (n < 0) {\n\t\tll res = mod_pow(x, -n, m);\n\t\treturn mod_pow(res, m - 2, m);\n\t}\n\tif (abs(x) >= m)x %= m;\n\tif (x < 0)x += m;\n\tll res = 1;\n\twhile (n) {\n\t\tif (n & 1)res = res * x % m;\n\t\tx = x * x % m; n >>= 1;\n\t}\n\treturn res;\n}\nstruct modint {\n\tll n;\n\tmodint() :n(0) { ; }\n\tmodint(ll m) :n(m) {\n\t\tif (n >= mod)n %= mod;\n\t\telse if (n < 0)n = (n % mod + mod) % mod;\n\t}\n\toperator int() { return n; }\n};\nbool operator==(modint a, modint b) { return a.n == b.n; }\nmodint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }\nmodint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }\nmodint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }\nmodint operator+(modint a, modint b) { return a += b; }\nmodint operator-(modint a, modint b) { return a -= b; }\nmodint operator*(modint a, modint b) { return a *= b; }\nmodint operator^(modint a, ll n) {\n\tif (n == 0)return modint(1);\n\tmodint res = (a * a) ^ (n / 2);\n\tif (n % 2)res = res * a;\n\treturn res;\n}\n\nll inv(ll a, ll p) {\n\treturn (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);\n}\nmodint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }\nmodint operator/=(modint& a, modint b) { a = a / b; return a; }\nconst int max_n = 1 << 2;\nmodint fact[max_n], factinv[max_n];\nvoid init_f() {\n\tfact[0] = modint(1);\n\tfor (int i = 0; i < max_n - 1; i++) {\n\t\tfact[i + 1] = fact[i] * modint(i + 1);\n\t}\n\tfactinv[max_n - 1] = modint(1) / fact[max_n - 1];\n\tfor (int i = max_n - 2; i >= 0; i--) {\n\t\tfactinv[i] = factinv[i + 1] * modint(i + 1);\n\t}\n}\nmodint comb(int a, int b) {\n\tif (a < 0 || b < 0 || a < b)return 0;\n\treturn fact[a] * factinv[b] * factinv[a - b];\n}\nmodint combP(int a, int b) {\n\tif (a < 0 || b < 0 || a < b)return 0;\n\treturn fact[a] * factinv[a - b];\n}\n\ntemplate<typename T>\nstruct mcf {\nprivate:\n\tstruct edge {\n\t\tint to, cap; T cost; int rev;\n\t};\n\tvector<vector<edge>> G;\n\tvector<P> par;\n\tvector<T> dist;\n\tT inf = mod;\npublic:\n\tmcf(int n) {\n\t\tG.resize(n);\n\t\tpar.resize(n);\n\t\tdist.resize(n);\n\t}\n\tvoid add_edge(int from, int to, int cap, T cost) {\n\t\tG[from].push_back({ to,cap,cost,(int)G[to].size() });\n\t\tG[to].push_back({ from,0,-cost,(int)G[from].size() - 1 });\n\t}\n\tpair<T, int> minimum_road(int s, int t) {\n\t\tfill(all(par), P{ -1,-1 });\n\t\tfill(all(dist), inf);\n\t\tdist[s] = 0;\n\t\tpriority_queue<pair<T, int>, vector<pair<T, int>>, greater<pair<T, int>>> q;\n\t\tq.push({ 0,s });\n\t\twhile (!q.empty()) {\n\t\t\tpair<T, int> p = q.top(); q.pop();\n\t\t\tint id = p.second;\n\t\t\tif (id == t)continue;\n\t\t\tif (p.first > dist[id])continue;\n\t\t\trep(j, G[id].size()) {\n\t\t\t\tif (G[id][j].cap > 0) {\n\t\t\t\t\tint to = G[id][j].to;\n\t\t\t\t\tT nd = p.first + G[id][j].cost;\n\t\t\t\t\tif (nd < dist[to]) {\n\t\t\t\t\t\tdist[to] = nd;\n\t\t\t\t\t\tpar[to] = { id,j };\n\t\t\t\t\t\tq.push({ dist[to],to });\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tint cur = t;\n\t\tint f = mod;\n\t\twhile (cur != s) {\n\t\t\tint p = par[cur].first, j = par[cur].second;\n\t\t\tif (p < 0)return { -1,-1 };\n\t\t\tf = min(f, G[p][j].cap);\n\t\t\tcur = p;\n\t\t}\n\t\tcur = t;\n\t\twhile (cur != s) {\n\t\t\tint p = par[cur].first, j = par[cur].second;\n\t\t\tif (p < 0)return { -1,-1 };\n\t\t\tG[p][j].cap -= f;\n\t\t\tif (G[p][j].rev >= 0) {\n\t\t\t\tG[cur][G[p][j].rev].cap += f;\n\t\t\t}\n\t\t\tcur = p;\n\t\t}\n\t\treturn { dist[t],f };\n\t}\n\tT minimum_cost_flow(int s, int t, int k) {\n\t\tT ret = 0;\n\t\trep(i, k) {\n\t\t\tpair<T, int> z = minimum_road(s, t);\n\t\t\tif (z.first < 0)return -1;\n\t\t\tif (k - i <= z.second) {\n\t\t\t\tret += z.first * (k - i); break;\n\t\t\t}\n\t\t\ti += z.second - 1;\n\t\t\tret += z.first * z.second;\n\t\t}\n\t\treturn ret;\n\t}\n};\n\nvoid solve() {\n\tint n, v, xl, xr; cin >> n >> v >> xl >> xr;\n\tvector<int> x(n), t(n), p(n);\n\trep(i, n) {\n\t\tcin >> x[i] >> t[i] >> p[i];\n\t}\n\n\tx.push_back(xl);\n\tt.push_back(0);\n\tp.push_back(0);\n\tx.push_back(xr);\n\tt.push_back(0);\n\tp.push_back(0);\n\n\tn += 2;\n\tvector<P> vp;\n\trep(i, n)vp.push_back({ t[i],i });\n\tsort(all(vp));\n\tvector<int> nx(n), nt(n), np(n);\n\trep(i, n) {\n\t\tint id = vp[i].second;\n\t\tnx[i] = x[id];\n\t\tnt[i] = t[id];\n\t\tnp[i] = p[id];\n\t}\n\tswap(x, nx);\n\tswap(t, nt);\n\tswap(p, np);\n\tvector<int> rp(n + 1);\n\trep(i, n)rp[i + 1] = rp[i] + p[i];\n\n\n\tmcf<int> mc(2*n + 1);\n\trep(i, n) {\n\t\tmc.add_edge(i, i + n, 1, 0);\n\t\tmc.add_edge(i, i + n, 2, p[i]);\n\n\t\tmc.add_edge(i + n, 2 * n, 2, rp[n] - rp[i + 1]);\n\t}\n\trep(i, n)Rep(j, i+1, n) {\n\t\tint idl = i;\n\t\tint idr = j;\n\t\tint dx = abs(x[idl] - x[idr]);\n\t\tint dt = t[idr] - t[idl];\n\t\tif (v * dt >= dx) {\n\t\t\tmc.add_edge(idl + n, idr, 2, rp[idr] - rp[idl + 1]);\n\t\t}\n\t}\n\n\tint ans = 0;\n\tans += mc.minimum_cost_flow(0, 2*n, 1);\n\t//cout << rp[n] - ans << \"\\n\";\n\tans += mc.minimum_cost_flow(1, 2 * n, 1);\n\n\tans = 2 * rp[n] - ans;\n\tcout << ans << \"\\n\";\n}\n\n\n\nsigned main() {\n\tios::sync_with_stdio(false);\n\tcin.tie(0);\n\tcout << fixed << setprecision(10);\n\t//init_f();\n\t//init();\n\t//int t; cin >> t; rep(i, t)\n\t//while (cin>>s,s!=\".\")\n\t\tsolve();\n\treturn 0;\n}", "accuracy": 1, "time_ms": 780, "memory_kb": 243348, "score_of_the_acc": -1.0571, "final_rank": 6 }, { "submission_id": "aoj_2290_5801620", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntemplate<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }\ntemplate<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }\n#define all(x) (x).begin(),(x).end()\n#define fi first\n#define se second\n#define mp make_pair\n#define si(x) int(x.size())\nconst int mod=998244353,MAX=6005,INF=1<<29;\ntypedef pair<int,int> P;\n\nstruct edge{int to,cap,cost,rev;};\n\nint V;\nvector<edge> G[MAX];\nint h[MAX];\nint dist[MAX];\nint prevv[MAX],preve[MAX];\nvector<int> id;\n\nvoid add_edge(int from,int to,int cap,int cost){\n G[from].push_back((edge){to,cap,cost,int(G[to].size())});\n G[to].push_back((edge){from,0,-cost,int(G[from].size()-1)});\n}\n\nint min_cost_flow(int s,int t,int f){\n int res=0;\n fill(h,h+V,0);\n for(int v:id){\n for(auto e:G[v]){\n if(e.cap>0) chmin(h[e.to],h[v]+e.cost);\n }\n }\n \n while(f>0){\n priority_queue<P,vector<P>,greater<P>> que;\n fill(dist,dist+V,INF);\n dist[s]=0;\n que.push(P(0,s));\n while(!que.empty()){\n P p=que.top();que.pop();\n int v=p.second;\n if(dist[v]<p.first) continue;\n for(int i=0;i<G[v].size();i++){\n edge &e=G[v][i];\n if(e.cap>0&&dist[e.to]>dist[v]+e.cost+h[v]-h[e.to]){\n dist[e.to]=dist[v]+e.cost+h[v]-h[e.to];\n prevv[e.to]=v;\n preve[e.to]=i;\n que.push(P(dist[e.to],e.to));\n }\n }\n }\n \n if(dist[t]==INF){\n return -1;\n }\n for(int v=0;v<V;v++){\n h[v]+=dist[v];\n }\n \n int d=f;\n for(int v=t;v!=s;v=prevv[v]){\n d=min(d,G[prevv[v]][preve[v]].cap);\n }\n f-=d;\n res+=d*h[t];\n for(int v=t;v!=s;v=prevv[v]){\n edge &e=G[prevv[v]][preve[v]];\n e.cap-=d;\n G[v][e.rev].cap+=d;\n }\n }\n return res;\n}//sからtへの流量fの最小費用流、流せない場合は-1\n\nint main(){\n \n std::ifstream in(\"text.txt\");\n std::cin.rdbuf(in.rdbuf());\n cin.tie(0);\n ios::sync_with_stdio(false);\n \n ll N,VV,xl,xr;cin>>N>>VV>>xl>>xr;\n vector<ll> X(N),T(N),P(N);\n for(int i=0;i<N;i++) cin>>X[i]>>T[i]>>P[i];\n V=2*N+4;\n int s=2*N,t=s+1,sl=s+2,sr=s+3;\n \n for(int i=0;i<N;i++){\n add_edge(i,N+i,1,-P[i]);\n add_edge(i,N+i,1,0);\n add_edge(N+i,t,2,0);\n \n if(abs(X[i]-xl)<=VV*T[i]) add_edge(sl,i,1,0);\n if(abs(X[i]-xr)<=VV*T[i]) add_edge(sr,i,1,0);\n \n for(int j=0;j<N;j++){\n if(T[i]>=T[j]) continue;\n if(abs(X[i]-X[j])<=VV*(T[j]-T[i])) add_edge(N+i,j,2,0);\n }\n }\n add_edge(s,sl,1,0);\n add_edge(s,sr,1,0);\n add_edge(s,t,2,0);\n \n vector<pair<ll,int>> S(N);\n for(int i=0;i<N;i++) S[i]=mp(T[i],i);\n sort(all(S));\n id.push_back(s);\n id.push_back(sl);\n id.push_back(sr);\n for(auto a:S){\n id.push_back(a.se);\n id.push_back(N+a.se);\n }\n id.push_back(t);\n \n cout<<-min_cost_flow(s,t,2)<<endl;\n}", "accuracy": 1, "time_ms": 890, "memory_kb": 244012, "score_of_the_acc": -1.0793, "final_rank": 8 }, { "submission_id": "aoj_2290_5801611", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntemplate<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }\ntemplate<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }\n#define all(x) (x).begin(),(x).end()\n#define fi first\n#define se second\n#define mp make_pair\n#define si(x) int(x.size())\nconst int mod=998244353,MAX=6005;\nconst ll INF=1LL<<57;\n\ntypedef pair<ll,int> P;\n\nstruct edge{int to,cap;ll cost;int rev;};\n\nint V;\nvector<edge> G[MAX];\nll h[MAX];\nll dist[MAX];\nint prevv[MAX],preve[MAX];\nvector<int> id;\n\nvoid add_edge(int from,int to,int cap,ll cost){\n G[from].push_back((edge){to,cap,cost,int(G[to].size())});\n G[to].push_back((edge){from,0,-cost,int(G[from].size()-1)});\n}\n\nll min_cost_flow(int s,int t,int f){\n ll res=0;\n fill(h,h+V,0);\n for(int v:id){\n for(auto e:G[v]){\n if(e.cap>0) chmin(h[e.to],h[v]+e.cost);\n }\n }\n \n while(f>0){\n priority_queue<P,vector<P>,greater<P>> que;\n fill(dist,dist+V,INF);\n dist[s]=0;\n que.push(P(0,s));\n while(!que.empty()){\n P p=que.top();que.pop();\n int v=p.second;\n if(dist[v]<p.first) continue;\n for(int i=0;i<G[v].size();i++){\n edge &e=G[v][i];\n if(e.cap>0&&dist[e.to]>dist[v]+e.cost+h[v]-h[e.to]){\n dist[e.to]=dist[v]+e.cost+h[v]-h[e.to];\n prevv[e.to]=v;\n preve[e.to]=i;\n que.push(P(dist[e.to],e.to));\n }\n }\n }\n \n if(dist[t]==INF){\n return -1;\n }\n for(int v=0;v<V;v++){\n h[v]+=dist[v];\n }\n \n int d=f;\n for(int v=t;v!=s;v=prevv[v]){\n d=min(d,G[prevv[v]][preve[v]].cap);\n }\n f-=d;\n res+=d*h[t];\n for(int v=t;v!=s;v=prevv[v]){\n edge &e=G[prevv[v]][preve[v]];\n e.cap-=d;\n G[v][e.rev].cap+=d;\n }\n }\n return res;\n}//sからtへの流量fの最小費用流、流せない場合は-1\n\nint main(){\n \n std::ifstream in(\"text.txt\");\n std::cin.rdbuf(in.rdbuf());\n cin.tie(0);\n ios::sync_with_stdio(false);\n \n ll N,VV,xl,xr;cin>>N>>VV>>xl>>xr;\n vector<ll> X(N),T(N),P(N);\n for(int i=0;i<N;i++) cin>>X[i]>>T[i]>>P[i];\n V=2*N+2;\n int s=2*N,t=s+1;\n \n for(int i=0;i<N;i++){\n add_edge(i,N+i,1,-P[i]);\n add_edge(i,N+i,1,0);\n add_edge(N+i,t,2,0);\n \n if(abs(X[i]-xl)<=VV*T[i]) add_edge(s,i,1,0);\n if(abs(X[i]-xr)<=VV*T[i]) add_edge(s,i,1,0);\n \n for(int j=0;j<N;j++){\n if(T[i]>=T[j]) continue;\n if(abs(X[i]-X[j])<=VV*(T[j]-T[i])) add_edge(N+i,j,2,0);\n }\n }\n add_edge(s,t,2,0);\n \n vector<pair<ll,int>> S(N);\n for(int i=0;i<N;i++) S[i]=mp(T[i],i);\n sort(all(S));\n id.push_back(s);\n for(auto a:S){\n id.push_back(a.se);\n id.push_back(N+a.se);\n }\n id.push_back(t);\n \n cout<<-min_cost_flow(s,t,2)<<endl;\n}", "accuracy": 0.2653061224489796, "time_ms": 150, "memory_kb": 133056, "score_of_the_acc": -0.3664, "final_rank": 20 }, { "submission_id": "aoj_2290_4869085", "code_snippet": "#define PROBLEM \"http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2290\"\n\n#include<bits/stdc++.h>\nusing namespace std;\n\n#define call_from_test\n#ifndef call_from_test\n#include<bits/stdc++.h>\nusing namespace std;\n#endif\n//BEGIN CUT HERE\n// O(F E log V)\ntemplate<typename Flow, typename Cost>\nstruct PrimalDual{\n struct Edge{\n int dst;\n Flow cap;\n Cost cost;\n int rev;\n Edge(int dst,Flow cap,Cost cost,int rev):\n dst(dst),cap(cap),cost(cost),rev(rev){}\n };\n\n vector<vector<Edge>> G;\n vector<Cost> h,dist;\n vector<int> prevv,preve;\n\n PrimalDual(int n):G(n),h(n),dist(n),prevv(n),preve(n){}\n\n void add_edge(int u,int v,Flow cap,Cost cost){\n int e=G[u].size();\n int r=(u==v?e+1:G[v].size());\n G[u].emplace_back(v,cap,cost,r);\n G[v].emplace_back(u,0,-cost,e);\n }\n\n Cost residual_cost(int src,Edge &e){\n return e.cost+h[src]-h[e.dst];\n }\n\n void dijkstra(int s){\n struct P{\n Cost first;\n int second;\n P(Cost first,int second):first(first),second(second){}\n bool operator<(const P&a) const{return first>a.first;}\n };\n priority_queue<P> pq;\n\n dist[s]=0;\n pq.emplace(dist[s],s);\n while(!pq.empty()){\n P p=pq.top();pq.pop();\n int v=p.second;\n if(dist[v]<p.first) continue;\n for(int i=0;i<(int)G[v].size();i++){\n Edge &e=G[v][i];\n if(e.cap==0) continue;\n if(dist[v]+residual_cost(v,e)<dist[e.dst]){\n dist[e.dst]=dist[v]+e.cost+h[v]-h[e.dst];\n prevv[e.dst]=v;\n preve[e.dst]=i;\n pq.emplace(dist[e.dst],e.dst);\n }\n }\n }\n }\n\n Cost res;\n\n bool build(int s,int t,Flow f,\n function<void(decltype(h)&)> init=[](decltype(h) &p){\n fill(p.begin(),p.end(),0);\n }){\n res=0;\n init(h);\n const Cost INF = numeric_limits<Cost>::max();\n while(f>0){\n fill(dist.begin(),dist.end(),INF);\n dijkstra(s);\n if(dist[t]==INF) return false;\n\n for(int v=0;v<(int)h.size();v++)\n if(dist[v]<INF) h[v]=h[v]+dist[v];\n\n Flow d=f;\n for(int v=t;v!=s;v=prevv[v])\n d=min(d,G[prevv[v]][preve[v]].cap);\n\n f-=d;\n res=res+h[t]*d;\n for(int v=t;v!=s;v=prevv[v]){\n Edge &e=G[prevv[v]][preve[v]];\n e.cap-=d;\n G[v][e.rev].cap+=d;\n }\n }\n return true;\n }\n\n Cost get_cost(){return res;}\n};\n//END CUT HERE\n#ifndef call_from_test\n//INSERT ABOVE HERE\nsigned geocon2013_B(){\n cin.tie(0);\n ios::sync_with_stdio(0);\n\n using D = double;\n\n int n;\n cin>>n;\n vector<D> xs(n),ys(n);\n for(int i=0;i<n;i++) cin>>xs[i]>>ys[i];\n\n vector<int> pos,neg;\n for(int i=0;i<n;i++){\n if(xs[i]>0) pos.emplace_back(i);\n if(xs[i]<0) neg.emplace_back(i);\n }\n\n int f=max(pos.size(),neg.size());\n if(f==0){\n cout<<0<<endl;\n return 0;\n }\n\n PrimalDual<int, D> G(n+3);\n int S=n,T=n+1,U=n+2;\n for(int z:pos) G.add_edge(S,z,1,0);\n for(int z:neg) G.add_edge(z,T,1,0);\n\n int dif=pos.size()-neg.size();\n if(dif>0){\n G.add_edge(U,T,dif,0);\n for(int p:pos)\n G.add_edge(p,U,1,abs(xs[p]));\n }\n if(dif<0){\n G.add_edge(S,U,-dif,0);\n for(int q:neg)\n G.add_edge(U,q,1,abs(xs[q]));\n }\n\n for(int p:pos)\n for(int q:neg)\n G.add_edge(p,q,1,\n min(hypot(xs[p]+xs[q],ys[p]-ys[q]),abs(xs[p])+abs(xs[q])));\n\n assert(G.build(S,T,f));\n cout<<fixed<<setprecision(12)<<G.get_cost()<<endl;\n return 0;\n}\n/*\n verified on 2020/09/25\n https://atcoder.jp/contests/geocon2013/tasks/geocon2013_b\n*/\n\nsigned main(){\n geocon2013_B();\n return 0;\n}\n#endif\n\n\n#ifndef call_from_test\n#include <bits/stdc++.h>\nusing namespace std;\n#endif\n//BEGIN CUT HERE\ntemplate<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}\ntemplate<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}\n//END CUT HERE\n#ifndef call_from_test\nsigned main(){\n return 0;\n}\n#endif\n\n\n#ifndef call_from_test\n#include <bits/stdc++.h>\nusing namespace std;\n#endif\n\n//BEGIN CUT HERE\nvector<int> identity(int n){\n vector<int> ord(n);\n iota(ord.begin(),ord.end(),0);\n return ord;\n}\n//END CUT HERE\n#ifndef call_from_test\nsigned main(){\n return 0;\n}\n#endif\n\n#undef call_from_test\n\nsigned main(){\n cin.tie(0);\n ios::sync_with_stdio(0);\n\n int n,v,xleft,xright;\n cin>>n>>v>>xleft>>xright;\n\n int S=n+n,T=n+n+1,L=n+n+2,R=n+n+3;\n PrimalDual<int, int> G(n+n+4);\n\n G.add_edge(S,L,1,0);\n G.add_edge(L,T,1,0);\n\n G.add_edge(S,R,1,0);\n G.add_edge(R,T,1,0);\n\n vector<int> xs(n),ts(n),ps(n);\n for(int i=0;i<n;i++){\n cin>>xs[i]>>ts[i]>>ps[i];\n\n if(abs(xs[i]-xleft)<=ts[i]*v)\n G.add_edge(L,i,1,0);\n\n if(abs(xs[i]-xright)<=ts[i]*v)\n G.add_edge(R,i,1,0);\n }\n\n auto init=[&](auto &h)->void{\n fill(h.begin(),h.end(),0);\n\n auto ord=identity(n);\n sort(ord.begin(),ord.end(),\n [&](int i,int j){return ts[i]<ts[j];});\n\n auto add_edge=[&](int u,int v,int f,int c){\n G.add_edge(u,v,f,c);\n chmin(h[v],h[u]+c);\n };\n\n for(int i=0;i<n;i++){\n int x=ord[i];\n add_edge(x,n+x,1,-ps[x]);\n for(int j=i+1;j<n;j++){\n int y=ord[j];\n if(abs(xs[x]-xs[y])<=(ts[y]-ts[x])*v)\n add_edge(n+x,y,1,0);\n }\n add_edge(n+x,T,1,0);\n }\n };\n assert(G.build(S,T,2,init));\n cout<<-G.get_cost()<<endl;\n return 0;\n}", "accuracy": 1, "time_ms": 770, "memory_kb": 245160, "score_of_the_acc": -1.065, "final_rank": 7 }, { "submission_id": "aoj_2290_4869075", "code_snippet": "#define PROBLEM \"http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2290\"\n\n#include<bits/stdc++.h>\nusing namespace std;\n\n#define call_from_test\n#ifndef call_from_test\n#include<bits/stdc++.h>\nusing namespace std;\n#endif\n//BEGIN CUT HERE\n// O(F E log V)\ntemplate<typename Flow, typename Cost>\nstruct PrimalDual{\n struct Edge{\n int dst;\n Flow cap;\n Cost cost;\n int rev;\n Edge(int dst,Flow cap,Cost cost,int rev):\n dst(dst),cap(cap),cost(cost),rev(rev){}\n };\n\n vector<vector<Edge>> G;\n vector<Cost> h,dist;\n vector<int> prevv,preve;\n\n PrimalDual(int n):G(n),h(n),dist(n),prevv(n),preve(n){}\n\n void add_edge(int u,int v,Flow cap,Cost cost){\n int e=G[u].size();\n int r=(u==v?e+1:G[v].size());\n G[u].emplace_back(v,cap,cost,r);\n G[v].emplace_back(u,0,-cost,e);\n }\n\n Cost residual_cost(int src,Edge &e){\n return e.cost+h[src]-h[e.dst];\n }\n\n void dijkstra(int s){\n struct P{\n Cost first;\n int second;\n P(Cost first,int second):first(first),second(second){}\n bool operator<(const P&a) const{return first>a.first;}\n };\n priority_queue<P> pq;\n\n dist[s]=0;\n pq.emplace(dist[s],s);\n while(!pq.empty()){\n P p=pq.top();pq.pop();\n int v=p.second;\n if(dist[v]<p.first) continue;\n for(int i=0;i<(int)G[v].size();i++){\n Edge &e=G[v][i];\n if(e.cap==0) continue;\n if(dist[v]+residual_cost(v,e)<dist[e.dst]){\n dist[e.dst]=dist[v]+e.cost+h[v]-h[e.dst];\n prevv[e.dst]=v;\n preve[e.dst]=i;\n pq.emplace(dist[e.dst],e.dst);\n }\n }\n }\n }\n\n Cost res;\n\n bool build(int s,int t,Flow f,\n function<void(decltype(h)&)> init=[](decltype(h) &p){\n fill(p.begin(),p.end(),0);\n }){\n res=0;\n init(h);\n const Cost INF = numeric_limits<Cost>::max();\n while(f>0){\n fill(dist.begin(),dist.end(),INF);\n dijkstra(s);\n if(dist[t]==INF) return false;\n\n for(int v=0;v<(int)h.size();v++)\n if(dist[v]<INF) h[v]=h[v]+dist[v];\n\n Flow d=f;\n for(int v=t;v!=s;v=prevv[v])\n d=min(d,G[prevv[v]][preve[v]].cap);\n\n f-=d;\n res=res+h[t]*d;\n for(int v=t;v!=s;v=prevv[v]){\n Edge &e=G[prevv[v]][preve[v]];\n e.cap-=d;\n G[v][e.rev].cap+=d;\n }\n }\n return true;\n }\n\n Cost get_cost(){return res;}\n};\n//END CUT HERE\n#ifndef call_from_test\n//INSERT ABOVE HERE\nsigned geocon2013_B(){\n cin.tie(0);\n ios::sync_with_stdio(0);\n\n using D = double;\n\n int n;\n cin>>n;\n vector<D> xs(n),ys(n);\n for(int i=0;i<n;i++) cin>>xs[i]>>ys[i];\n\n vector<int> pos,neg;\n for(int i=0;i<n;i++){\n if(xs[i]>0) pos.emplace_back(i);\n if(xs[i]<0) neg.emplace_back(i);\n }\n\n int f=max(pos.size(),neg.size());\n if(f==0){\n cout<<0<<endl;\n return 0;\n }\n\n PrimalDual<int, D> G(n+3);\n int S=n,T=n+1,U=n+2;\n for(int z:pos) G.add_edge(S,z,1,0);\n for(int z:neg) G.add_edge(z,T,1,0);\n\n int dif=pos.size()-neg.size();\n if(dif>0){\n G.add_edge(U,T,dif,0);\n for(int p:pos)\n G.add_edge(p,U,1,abs(xs[p]));\n }\n if(dif<0){\n G.add_edge(S,U,-dif,0);\n for(int q:neg)\n G.add_edge(U,q,1,abs(xs[q]));\n }\n\n for(int p:pos)\n for(int q:neg)\n G.add_edge(p,q,1,\n min(hypot(xs[p]+xs[q],ys[p]-ys[q]),abs(xs[p])+abs(xs[q])));\n\n assert(G.build(S,T,f));\n cout<<fixed<<setprecision(12)<<G.get_cost()<<endl;\n return 0;\n}\n/*\n verified on 2020/09/25\n https://atcoder.jp/contests/geocon2013/tasks/geocon2013_b\n*/\n\nsigned main(){\n geocon2013_B();\n return 0;\n}\n#endif\n\n\n#ifndef call_from_test\n#include <bits/stdc++.h>\nusing namespace std;\n#endif\n//BEGIN CUT HERE\ntemplate<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}\ntemplate<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}\n//END CUT HERE\n#ifndef call_from_test\nsigned main(){\n return 0;\n}\n#endif\n\n\n#ifndef call_from_test\n#include <bits/stdc++.h>\nusing namespace std;\n#endif\n\n//BEGIN CUT HERE\nvector<int> identity(int n){\n vector<int> ord(n);\n iota(ord.begin(),ord.end(),0);\n return ord;\n}\n//END CUT HERE\n#ifndef call_from_test\nsigned main(){\n return 0;\n}\n#endif\n\n#undef call_from_test\n\nsigned main(){\n cin.tie(0);\n ios::sync_with_stdio(0);\n\n int n,v,xleft,xright;\n cin>>n>>v>>xleft>>xright;\n\n int S=n+n,T=n+n+1,L=n+n+2,R=n+n+3;\n PrimalDual<int, int> G(n+n+4);\n\n G.add_edge(S,L,1,0);\n G.add_edge(L,T,1,0);\n\n G.add_edge(S,R,1,0);\n G.add_edge(R,T,1,0);\n\n vector<int> xs(n),ts(n),ps(n);\n for(int i=0;i<n;i++){\n cin>>xs[i]>>ts[i]>>ps[i];\n\n if(abs(xs[i]-xleft)<=ts[i]*v)\n G.add_edge(L,i,1,0);\n\n if(abs(xs[i]-xright)<=ts[i]*v)\n G.add_edge(R,i,1,0);\n\n G.add_edge(i,n+i,1,-ps[i]);\n G.add_edge(n+i,T,1,0);\n }\n\n for(int i=0;i<n;i++)\n for(int j=0;j<n;j++)\n if(ts[i]<ts[j] and abs(xs[i]-xs[j])<=(ts[j]-ts[i])*v)\n G.add_edge(n+i,j,1,0);\n\n auto init=[&](auto &h)->void{\n auto ord=identity(n);\n sort(ord.begin(),ord.end(),\n [&](int i,int j){return ts[i]<ts[j];});\n fill(h.begin(),h.end(),0);\n\n for(int i=0;i<n;i++){\n int x=ord[i];\n chmin(h[n+x],h[x]-ps[x]);\n for(int j=i+1;j<n;j++){\n int y=ord[j];\n if(abs(xs[x]-xs[y])<=(ts[y]-ts[x])*v)\n chmin(h[y],h[n+x]);\n }\n chmin(h[T],h[n+x]);\n }\n };\n assert(G.build(S,T,2,init));\n cout<<-G.get_cost()<<endl;\n return 0;\n}", "accuracy": 1, "time_ms": 1050, "memory_kb": 243112, "score_of_the_acc": -1.1017, "final_rank": 16 }, { "submission_id": "aoj_2290_4869066", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing Int = long long;\nconst char newl = '\\n';\n\ntemplate<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}\ntemplate<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}\ntemplate<typename T> void drop(const T &x){cout<<x<<endl;exit(0);}\ntemplate<typename T=int>\nvector<T> read(size_t n){\n vector<T> ts(n);\n for(size_t i=0;i<n;i++) cin>>ts[i];\n return ts;\n}\n\n\n// O(F E log V)\ntemplate<typename Flow, typename Cost>\nstruct PrimalDual{\n struct Edge{\n int to;\n Flow cap;\n Cost cost;\n int rev;\n Edge(int to,Flow cap,Cost cost,int rev):\n to(to),cap(cap),cost(cost),rev(rev){}\n };\n\n vector<vector<Edge>> G;\n vector<Cost> h,dist;\n vector<int> prevv,preve;\n\n PrimalDual(int n):G(n),h(n),dist(n),prevv(n),preve(n){}\n\n void add_edge(int u,int v,Flow cap,Cost cost){\n int e=G[u].size();\n int r=(u==v?e+1:G[v].size());\n G[u].emplace_back(v,cap,cost,r);\n G[v].emplace_back(u,0,-cost,e);\n }\n\n void dijkstra(int s){\n // cout<<\"dijkstra\"<<endl;\n struct P{\n Cost first;\n int second;\n P(Cost first,int second):first(first),second(second){}\n bool operator<(const P&a) const{return first>a.first;}\n };\n priority_queue<P> pq;\n\n dist[s]=0;\n pq.emplace(dist[s],s);\n while(!pq.empty()){\n P p=pq.top();pq.pop();\n int v=p.second;\n if(dist[v]<p.first) continue;\n for(int i=0;i<(int)G[v].size();i++){\n Edge &e=G[v][i];\n if(e.cap==0) continue;\n // if(e.cost+h[v]-h[e.to]<0) cout<<e.cost+h[v]-h[e.to]<<endl;\n assert(e.cost+h[v]-h[e.to]>=0);\n if(dist[v]+e.cost+h[v]-h[e.to]<dist[e.to]){\n dist[e.to]=dist[v]+e.cost+h[v]-h[e.to];\n prevv[e.to]=v;\n preve[e.to]=i;\n pq.emplace(dist[e.to],e.to);\n }\n }\n }\n }\n\n Cost res;\n\n bool build(int s,int t,Flow f,\n function<void(decltype(h)&)> init=[](decltype(h) &p){\n fill(p.begin(),p.end(),0);\n }){\n res=0;\n init(h);\n const Cost INF = numeric_limits<Cost>::max();\n while(f>0){\n fill(dist.begin(),dist.end(),INF);\n dijkstra(s);\n if(dist[t]==INF) return false;\n\n for(int v=0;v<(int)h.size();v++)\n if(dist[v]<INF) h[v]=h[v]+dist[v];\n\n Flow d=f;\n for(int v=t;v!=s;v=prevv[v])\n d=min(d,G[prevv[v]][preve[v]].cap);\n\n f-=d;\n res=res+h[t]*d;\n for(int v=t;v!=s;v=prevv[v]){\n Edge &e=G[prevv[v]][preve[v]];\n e.cap-=d;\n G[v][e.rev].cap+=d;\n }\n }\n return true;\n }\n\n Cost get_cost(){return res;}\n};\n\n\nvector<int> identity(int n){\n vector<int> ord(n);\n iota(ord.begin(),ord.end(),0);\n return ord;\n}\n\n//INSERT ABOVE HERE\nsigned main(){\n cin.tie(0);\n ios::sync_with_stdio(0);\n\n int n,v,xleft,xright;\n cin>>n>>v>>xleft>>xright;\n\n int S=n+n,T=n+n+1,L=n+n+2,R=n+n+3;\n PrimalDual<int, int> G(n+n+4);\n\n G.add_edge(S,L,1,0);\n G.add_edge(L,T,1,0);\n\n G.add_edge(S,R,1,0);\n G.add_edge(R,T,1,0);\n\n vector<int> xs(n),ts(n),ps(n);\n for(int i=0;i<n;i++){\n cin>>xs[i]>>ts[i]>>ps[i];\n\n if(abs(xs[i]-xleft)<=ts[i]*v)\n G.add_edge(L,i,1,0);\n\n if(abs(xs[i]-xright)<=ts[i]*v)\n G.add_edge(R,i,1,0);\n\n G.add_edge(i,n+i,1,-ps[i]);\n G.add_edge(n+i,T,1,0);\n }\n\n for(int i=0;i<n;i++)\n for(int j=0;j<n;j++)\n if(ts[i]<ts[j] and abs(xs[i]-xs[j])<=(ts[j]-ts[i])*v)\n G.add_edge(n+i,j,1,0);\n\n auto init=[&](auto &h)->void{\n auto ord=identity(n);\n sort(ord.begin(),ord.end(),\n [&](int i,int j){return ts[i]<ts[j];});\n fill(h.begin(),h.end(),0);\n\n for(int i=0;i<n;i++){\n int x=ord[i];\n chmin(h[n+x],h[x]-ps[x]);\n for(int j=i+1;j<n;j++){\n int y=ord[j];\n if(abs(xs[x]-xs[y])<=(ts[y]-ts[x])*v)\n chmin(h[y],h[n+x]);\n }\n chmin(h[T],h[n+x]);\n }\n };\n assert(G.build(S,T,2,init));\n cout<<-G.get_cost()<<endl;\n return 0;\n}", "accuracy": 1, "time_ms": 1020, "memory_kb": 243100, "score_of_the_acc": -1.0966, "final_rank": 14 }, { "submission_id": "aoj_2290_4108846", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\nusing int64 = long long;\nconst int mod = 1e9 + 7;\n// const int mod = 998244353;\n\nconst int64 infll = (1LL << 62) - 1;\nconst int inf = (1 << 30) - 1;\n\nstruct IoSetup {\n IoSetup() {\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(10);\n cerr << fixed << setprecision(10);\n }\n} iosetup;\n\n\ntemplate< typename T1, typename T2 >\nostream &operator<<(ostream &os, const pair< T1, T2 > &p) {\n os << p.first << \" \" << p.second;\n return os;\n}\n\ntemplate< typename T1, typename T2 >\nistream &operator>>(istream &is, pair< T1, T2 > &p) {\n is >> p.first >> p.second;\n return is;\n}\n\ntemplate< typename T >\nostream &operator<<(ostream &os, const vector< T > &v) {\n for(int i = 0; i < (int) v.size(); i++) {\n os << v[i] << (i + 1 != v.size() ? \" \" : \"\");\n }\n return os;\n}\n\ntemplate< typename T >\nistream &operator>>(istream &is, vector< T > &v) {\n for(T &in : v) is >> in;\n return is;\n}\n\ntemplate< typename T1, typename T2 >\ninline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }\n\ntemplate< typename T1, typename T2 >\ninline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }\n\ntemplate< typename T = int64 >\nvector< T > make_v(size_t a) {\n return vector< T >(a);\n}\n\ntemplate< typename T, typename... Ts >\nauto make_v(size_t a, Ts... ts) {\n return vector< decltype(make_v< T >(ts...)) >(a, make_v< T >(ts...));\n}\n\ntemplate< typename T, typename V >\ntypename enable_if< is_class< T >::value == 0 >::type fill_v(T &t, const V &v) {\n t = v;\n}\n\ntemplate< typename T, typename V >\ntypename enable_if< is_class< T >::value != 0 >::type fill_v(T &t, const V &v) {\n for(auto &e : t) fill_v(e, v);\n}\n\ntemplate< typename F >\nstruct FixPoint : F {\n FixPoint(F &&f) : F(forward< F >(f)) {}\n\n template< typename... Args >\n decltype(auto) operator()(Args &&... args) const {\n return F::operator()(*this, forward< Args >(args)...);\n }\n};\n\ntemplate< typename F >\ninline decltype(auto) MFP(F &&f) {\n return FixPoint< F >{forward< F >(f)};\n}\n\ntemplate< typename CapType, typename CostType >\nclass MinCostFlowDAG {\npublic:\n using Cat = CapType;\n using Cot = CostType;\n using pti = pair< Cot, int >;\n struct edge {\n int to, rev;\n Cat cap;\n Cot cost;\n };\n const int V;\n const Cot inf;\n vector< vector< edge > > G;\n vector< Cot > h, dist;\n vector< int > deg, ord, prevv, preve;\n\n MinCostFlowDAG(const int node_size) : V(node_size), inf(numeric_limits< Cot >::max() / 4),\n G(V), h(V, inf), dist(V), deg(V, 0), prevv(V), preve(V) {}\n\n void add_edge(const int from, const int to, const Cat cap, const Cot cost) {\n G[from].push_back((edge) {to, (int) G[to].size(), cap, cost});\n G[to].push_back((edge) {from, (int) G[from].size() - 1, 0, -cost});\n ++deg[to];\n }\n\n bool tsort() {\n queue< int > que;\n for(int i = 0; i < V; ++i) {\n if(deg[i] == 0) que.push(i);\n }\n while(!que.empty()) {\n const int p = que.front();\n que.pop();\n ord.push_back(p);\n for(auto &e : G[p]) {\n if(e.cap > 0 && --deg[e.to] == 0) que.push(e.to);\n }\n }\n return (*max_element(deg.begin(), deg.end()) == 0);\n }\n\n void calc_potential(const int s) {\n h[s] = 0;\n for(const int v : ord) {\n if(h[v] == inf) continue;\n for(edge &e : G[v]) {\n if(e.cap > 0) h[e.to] = min(h[e.to], h[v] + e.cost);\n }\n }\n }\n\n void Dijkstra(const int s) {\n priority_queue< pti, vector< pti >, greater< pti > > que;\n fill(dist.begin(), dist.end(), inf);\n dist[s] = 0;\n que.push(pti(0, s));\n while(!que.empty()) {\n pti p = que.top();\n que.pop();\n const int v = p.second;\n if(dist[v] < p.first) continue;\n for(int i = 0; i < (int) G[v].size(); ++i) {\n edge &e = G[v][i];\n if(e.cap > 0 && dist[e.to] > dist[v] + e.cost + h[v] - h[e.to]) {\n dist[e.to] = dist[v] + e.cost + h[v] - h[e.to];\n prevv[e.to] = v, preve[e.to] = i;\n que.push(pti(dist[e.to], e.to));\n }\n }\n }\n }\n\n void update(const int s, const int t, Cat &f, Cot &res) {\n for(int i = 0; i < V; i++) {\n h[i] += dist[i];\n }\n Cat d = f;\n for(int v = t; v != s; v = prevv[v]) {\n d = min(d, G[prevv[v]][preve[v]].cap);\n }\n f -= d;\n res += h[t] * d;\n for(int v = t; v != s; v = prevv[v]) {\n edge &e = G[prevv[v]][preve[v]];\n e.cap -= d;\n G[v][e.rev].cap += d;\n }\n }\n\n Cot solve(const int s, const int t, Cat f) {\n if(!tsort()) assert(false); // not DAG\n calc_potential(s);\n Cot res = 0;\n while(f > 0) {\n Dijkstra(s);\n if(dist[t] == inf) return -inf;\n update(s, t, f, res);\n }\n return res;\n }\n};\n\nint main() {\n int N, V, X[2];\n cin >> N >> V >> X[0] >> X[1];\n vector< int > P(N), T(N), S(N);\n for(int i = 0; i < N; i++) {\n cin >> P[i] >> T[i] >> S[i];\n }\n int src = 2 * N;\n int snk = src + 1;\n int latte = snk + 1;\n int malta = latte + 1;\n MinCostFlowDAG< int, int > flow(2 * N + 4);\n for(int i = 0; i < N; i++) {\n flow.add_edge(2 * i, 2 * i + 1, 1, -S[i]);\n flow.add_edge(2 * i + 1, snk, 1, 0);\n if(abs(X[0] - P[i]) <= 1LL * T[i] * V) flow.add_edge(latte, 2 * i, 1, 0);\n if(abs(X[1] - P[i]) <= 1LL * T[i] * V) flow.add_edge(malta, 2 * i, 1, 0);\n for(int j = 0; j < N; j++) {\n if(i == j) continue;\n if(abs(P[i] - P[j]) <= 1LL * V * (T[j] - T[i])) {\n flow.add_edge(2 * i + 1, 2 * j, 1, 0);\n }\n }\n }\n flow.add_edge(src, snk, 2, 0);\n flow.add_edge(src, latte, 1, 0);\n flow.add_edge(src, malta, 1, 0);\n cout << -flow.solve(src, snk, 2) << endl;\n}", "accuracy": 1, "time_ms": 1200, "memory_kb": 242392, "score_of_the_acc": -1.1234, "final_rank": 18 }, { "submission_id": "aoj_2290_4108810", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing Int = long long;\ntemplate<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}\ntemplate<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}\n\n\ntemplate<typename ...Ts>\ndecltype(auto) zip(vector<Ts>... args){\n vector<decltype(make_tuple(args[0]...))> res;\n int n=min({args.size()...});\n res.reserve(n);\n for(int i=0;i<n;i++) res.emplace_back(args[i]...);\n return res;\n}\n\ntemplate<typename V>\nV compress(V v){\n sort(v.begin(),v.end());\n v.erase(unique(v.begin(),v.end()),v.end());\n return v;\n}\ntemplate<typename T>\nmap<T, int> dict(const vector<T> &v){\n map<T, int> res;\n for(int i=0;i<(int)v.size();i++)\n res[v[i]]=i;\n return res;\n}\nmap<char, int> dict(const string &v){\n return dict(vector<char>(v.begin(),v.end()));\n}\n\n\ntemplate<typename F>\nstruct FixPoint : F{\n FixPoint(F&& f):F(forward<F>(f)){}\n template<typename... Args>\n decltype(auto) operator()(Args&&... args) const{\n return F::operator()(*this,forward<Args>(args)...);\n }\n};\ntemplate<typename F>\ninline decltype(auto) MFP(F&& f){\n return FixPoint<F>{forward<F>(f)};\n}\n\n//INSERT ABOVE HERE\nconst int MAX = 3030;\nint dp[MAX][MAX]={};\nint rs[MAX][MAX]={};\n\nconst int INF = 1e9;\ntemplate<typename T>\nstruct BIT{\n int n;\n vector<T> bit;\n //1-indexed\n BIT(int n_):n(n_+2),bit(n,-INF){}\n\n T sum(int i){\n i++;\n\n T s(-INF);\n for(int x=i;x>0;x-=(x&-x))\n chmax(s,bit[x]);\n return s;\n }\n\n void add(int i,T a){\n i++;\n\n for(int x=i;x<n;x+=(x&-x))\n chmax(bit[x],a);\n }\n};\n\nsigned main(){\n int n,v,xl,xr;\n cin>>n>>v>>xl>>xr;\n\n vector<int> xs(n),ts(n),ps(n);\n for(int i=0;i<n;i++) cin>>xs[i]>>ts[i]>>ps[i];\n xs.emplace_back(xl);\n ts.emplace_back(0);\n ps.emplace_back(0);\n xs.emplace_back(xr);\n ts.emplace_back(0);\n ps.emplace_back(0);\n\n xs.emplace_back(0);\n ts.emplace_back(INF);\n ps.emplace_back(0);\n\n n=xs.size();\n auto zs=zip(ts,xs,ps);\n sort(zs.begin(),zs.end());\n for(int i=0;i<n;i++)\n tie(ts[i],xs[i],ps[i])=zs[i];\n\n using ll = long long;\n for(int i=0;i<n;i++)\n for(int j=i+1;j<n;j++)\n rs[i][j]=abs(xs[j]-xs[i])<=(ll)v*(ts[j]-ts[i]);\n\n for(int i=0;i<MAX;i++)\n for(int j=0;j<MAX;j++)\n dp[i][j]=-INF;\n dp[0][1]=0;\n\n vector<ll> as(n),bs(n);\n for(int i=0;i<n;i++){\n as[i]=-xs[i]+(ll)v*ts[i];\n bs[i]=+xs[i]+(ll)v*ts[i];\n }\n\n auto da=dict(compress(as));\n auto db=dict(compress(bs));\n vector<int> va(n),vb(n);\n for(int i=0;i<n;i++){\n va[i]=da[as[i]];\n vb[i]=db[bs[i]];\n }\n\n using P = pair<ll, ll>;\n vector<P> vp,vq;\n for(int l=0;l<n;l++){\n vp.emplace_back(+xs[l],l);\n vq.emplace_back(-xs[l],l);\n }\n sort(vp.begin(),vp.end());\n sort(vq.begin(),vq.end());\n\n for(int c=1;c<n;c++){\n {\n MFP([&](auto dfs,int l,int r)->void{\n if(l+1==r) return;\n int m=(l+r)>>1;\n\n dfs(l,m);\n\n if(r-l<=100){\n for(int a=l;a<m;a++)\n for(int b=m;b<r;b++)\n if(rs[a][b])\n chmax(dp[b][c],dp[a][c]+ps[b]);\n\n }else{\n for(int k=0;k<2;k++){\n vector<P> vp;\n for(int a=l;a<m;a++)\n vp.emplace_back(k?+xs[a]:-xs[a],a+0);\n for(int b=m;b<r;b++)\n vp.emplace_back(k?+xs[b]:-xs[b],b+n);\n sort(vp.begin(),vp.end());\n\n vector<ll> vs;\n for(int i=l;i<r;i++)\n vs.emplace_back(k?as[i]:bs[i]);\n auto cs=compress(vs);\n vector<ll> ws(r-l);\n for(int i=0;i<r-l;i++)\n ws[i]=lower_bound(cs.begin(),cs.end(),vs[i])-cs.begin();\n\n BIT<int> bit(cs.size());\n for(auto p:vp){\n if(p.second<n){\n int a=p.second;\n bit.add(ws[a-l],dp[a][c]);\n }else{\n int b=p.second-n;\n chmax(dp[b][c],bit.sum(ws[b-l])+ps[b]);\n }\n }\n }\n }\n\n dfs(m,r);\n })(0,c);\n }\n\n\n for(int k=0;k<2;k++){\n auto &vv=k?vp:vq;\n auto &vw=k?va:vb;\n BIT<int> bit(n+10);\n for(auto p:vv){\n if(p.second<c){\n int l=p.second;\n bit.add(vw[l],dp[l][c]);\n }\n if(p.second>c){\n int r=p.second;\n chmax(dp[c][r],bit.sum(vw[r])+ps[r]);\n }\n }\n }\n }\n\n int ans=0;\n for(int i=0;i<n;i++)\n for(int j=0;j<n;j++)\n chmax(ans,dp[i][j]);\n\n cout<<ans<<endl;\n return 0;\n}", "accuracy": 1, "time_ms": 5920, "memory_kb": 67332, "score_of_the_acc": -1, "final_rank": 4 }, { "submission_id": "aoj_2290_4107478", "code_snippet": "//#define NDEBUG\n#include <algorithm>\n#include <cstddef>\n#include <cstdint>\n#include <iostream>\n#include <utility>\n#include <vector>\n\nnamespace n91 {\n\nusing i8 = std::int_fast8_t;\nusing i32 = std::int_fast32_t;\nusing i64 = std::int_fast64_t;\nusing u8 = std::uint_fast8_t;\nusing u32 = std::uint_fast32_t;\nusing u64 = std::uint_fast64_t;\nusing isize = std::ptrdiff_t;\nusing usize = std::size_t;\n\nstruct rep {\n struct itr {\n usize i;\n constexpr itr(const usize i) noexcept : i(i) {}\n void operator++() noexcept { ++i; }\n constexpr usize operator*() const noexcept { return i; }\n constexpr bool operator!=(const itr x) const noexcept { return i != x.i; }\n };\n const itr f, l;\n constexpr rep(const usize f, const usize l) noexcept\n : f(std::min(f, l)), l(l) {}\n constexpr auto begin() const noexcept { return f; }\n constexpr auto end() const noexcept { return l; }\n};\nstruct revrep {\n struct itr {\n usize i;\n constexpr itr(const usize i) noexcept : i(i) {}\n void operator++() noexcept { --i; }\n constexpr usize operator*() const noexcept { return i; }\n constexpr bool operator!=(const itr x) const noexcept { return i != x.i; }\n };\n const itr f, l;\n constexpr revrep(const usize f, const usize l) noexcept\n : f(l - 1), l(std::min(f, l) - 1) {}\n constexpr auto begin() const noexcept { return f; }\n constexpr auto end() const noexcept { return l; }\n};\ntemplate <class T> auto md_vec(const usize n, const T &value) {\n return std::vector<T>(n, value);\n}\ntemplate <class... Args> auto md_vec(const usize n, Args... args) {\n return std::vector<decltype(md_vec(args...))>(n, md_vec(args...));\n}\ntemplate <class T> constexpr T difference(const T &a, const T &b) noexcept {\n if (a < b) {\n return b - a;\n } else {\n return a - b;\n }\n}\ntemplate <class T> void chmin(T &a, const T &b) noexcept {\n if (b < a) {\n a = b;\n }\n}\ntemplate <class T> void chmax(T &a, const T &b) noexcept {\n if (a < b) {\n a = b;\n }\n}\ntemplate <class F> class fix_point : private F {\npublic:\n explicit constexpr fix_point(F &&f) : F(std::forward<F>(f)) {}\n\n template <class... Args>\n constexpr decltype(auto) operator()(Args &&... args) const {\n return F::operator()(*this, std::forward<Args>(args)...);\n }\n};\ntemplate <class F> constexpr decltype(auto) make_fix(F &&f) {\n return fix_point<F>(std::forward<F>(f));\n}\ntemplate <class T> T scan() {\n T ret;\n std::cin >> ret;\n return ret;\n}\n\n} // namespace n91\n#include <cassert>\n#include <cstddef>\n#include <vector>\n\ntemplate <class Monoid> class segment_tree {\npublic:\n using T = typename Monoid::value_type;\n using size_t = std::size_t;\n\nprivate:\n std::vector<T> tree;\n\n template <class F>\n size_t search_subtree(size_t index, const F f, T fold_l) const {\n while (index < size()) {\n const T temp = Monoid::operation(fold_l, tree[index * 2]);\n if (!f(temp)) {\n index = index * 2;\n } else {\n fold_l = temp;\n index = index * 2 + 1;\n }\n }\n return index - size();\n }\n\npublic:\n segment_tree() = default;\n explicit segment_tree(const size_t n) : tree(n * 2, Monoid::identity) {}\n\n size_t size() const noexcept { return tree.size() / 2; }\n\n T fold(size_t first, size_t last) const {\n assert(first <= last);\n assert(last <= size());\n first += size();\n last += size();\n T fold_l = Monoid::identity;\n T fold_r = Monoid::identity;\n while (first != last) {\n if (first % 2 != 0) {\n fold_l = Monoid::operation(fold_l, tree[first]);\n first += 1;\n }\n first /= 2;\n if (last % 2 != 0) {\n last -= 1;\n fold_r = Monoid::operation(tree[last], fold_r);\n }\n last /= 2;\n }\n return Monoid::operation(fold_l, fold_r);\n }\n template <class F> size_t search(size_t first, size_t last, const F f) const {\n assert(first <= last);\n assert(last <= size());\n first += size();\n last += size();\n const size_t last_cp = last;\n size_t shift = 0;\n T fold_l = Monoid::identity;\n while (first != last) {\n if (first % 2 != 0) {\n const T temp = Monoid::operation(fold_l, tree[first]);\n if (!f(temp))\n return search_subtree(first, f, fold_l);\n fold_l = temp;\n first += 1;\n }\n first /= 2;\n last /= 2;\n shift += 1;\n }\n while (shift != 0) {\n shift -= 1;\n last = last_cp >> shift;\n if (last % 2 != 0) {\n last -= 1;\n const T temp = Monoid::operation(fold_l, tree[last]);\n if (!f(temp))\n return search_subtree(last, f, fold_l);\n fold_l = temp;\n }\n }\n return last_cp - size();\n }\n\n void update(size_t index, const T x) {\n assert(index < size());\n index += size();\n tree[index] = x;\n while (index != 1) {\n index /= 2;\n tree[index] = Monoid::operation(tree[index * 2], tree[index * 2 + 1]);\n }\n }\n};\n#include <algorithm>\n#include <limits>\n\ntemplate <class T> class max_monoid {\npublic:\n using value_type = T;\n static constexpr T operation(const T &x, const T &y) noexcept {\n return std::max(x, y);\n }\n static constexpr T identity = std::numeric_limits<T>::lowest();\n};\ntemplate <class T> constexpr T max_monoid<T>::identity;\n#include <algorithm>\n#include <iostream>\n#include <map>\n#include <tuple>\n#include <utility>\n#include <vector>\n\nnamespace n91 {\n\nvoid main_() {\n /*\n std::ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n //*/\n const usize n = scan<usize>();\n const i32 v = scan<i32>();\n const i32 xl = scan<i32>();\n const i32 xr = scan<i32>();\n struct mogu_t {\n i32 x;\n i32 y;\n u32 p;\n bool f;\n };\n std::vector<mogu_t> ms(n);\n std::map<i32, usize> mp;\n for (auto &e : ms) {\n e.f = false;\n const i32 x = scan<i32>();\n const i32 t = scan<i32>();\n const u32 p = scan<u32>();\n e.x = -t * v + x;\n e.y = -t * v - x;\n e.p = p;\n mp[e.y];\n }\n ms.push_back({xl, -xl, 0, true});\n ms.push_back({xr, -xr, 0, true});\n mp[-xl];\n mp[-xr];\n {\n usize c = 0;\n for (auto &e : mp)\n e.second = c++;\n }\n std::sort(ms.begin(), ms.end(), [](auto l, auto r) {\n return std::tie(l.x, l.y) < std::tie(r.x, r.y);\n });\n using seg_t = segment_tree<max_monoid<u32>>;\n const usize m = mp.size();\n std::vector<seg_t> dp(m, seg_t(m));\n usize ph = 0;\n for (const auto &e : ms) {\n if (e.f) {\n ph += 1;\n continue;\n }\n const usize k = mp[e.y];\n if (ph <= 1)\n dp[k].update(k, dp[k].fold(0, k + 1) + e.p);\n for (const usize i : rep(0, m)) {\n if (i == k)\n continue;\n const u32 v = dp[i].fold(0, k + 1) + e.p;\n if (ph <= 1)\n dp[i].update(k, v);\n if (ph <= 0)\n dp[k].update(i, v);\n }\n /*\n std::cout << \"\\n\";\n for (const auto i : rep(0, m)) {\n for (const auto j : rep(0, m)) {\n std::cout << dp[i].fold(j, j + 1) << \" \";\n }\n std::cout << \"\\n\";\n }\n */\n }\n u32 ans = 0;\n for (const auto i : rep(0, mp[-xl] + 1)) {\n chmax(ans, dp[i].fold(0, mp[-xr] + 1));\n }\n std::cout << ans << std::endl;\n}\n\n} // namespace n91\n\nint main() {\n n91::main_();\n return 0;\n}", "accuracy": 1, "time_ms": 1580, "memory_kb": 144140, "score_of_the_acc": -0.6683, "final_rank": 2 }, { "submission_id": "aoj_2290_3546450", "code_snippet": "#include <iostream>\n#include <algorithm>\n#include <cmath>\n#include <vector>\n#include <queue>\n#include <functional>\n#include <climits>\nstruct Mole {\n\tint position, time, score;\n\tMole(const int x, const int t, const int p) : position{ x }, time{ t }, score{ p }{};\n\tMole() :Mole(0, 0, 0) {};\n};\nstruct Edge {\n\tint to, pair, cost;\n\tbool flow;\n\tEdge(int t, int p, int c, bool f) : to{ t }, pair{ p }, cost{ c }, flow{ f }{};\n\tEdge() : Edge(0, 0, 0, false) {}\n};\nvoid setEdge(const int from, const int to, const int cost, std::vector<std::vector<Edge>>& nodes) {\n\tnodes[from].push_back(Edge(to, nodes[to].size(), cost, true));\n\tnodes[to].push_back(Edge(from, nodes[from].size() - 1, -cost, false));\n}\nint solve(const long velocity, const int left, const int right, const std::vector<Mole>& mole) {\n\tstd::vector<std::vector<Edge>> nodes(mole.size() * 2 + 4);\n\tconst int start_left = nodes.size() - 4;\n\tconst int start_right = nodes.size() - 3;\n\tconst int start = nodes.size() - 2;\n\tconst int goal = nodes.size() - 1;\n\tsetEdge(start, start_left, 0, nodes);\n\tsetEdge(start, start_right, 0, nodes);\n\tstd::vector<int> sum_cost(mole.size() + 1, 0);\n\tfor (auto i = 1; i < sum_cost.size(); ++i) sum_cost[i] = sum_cost[i - 1] + mole[i - 1].score;\n\tsetEdge(start_left, goal, sum_cost.back(), nodes);\n\tsetEdge(start_right, goal, sum_cost.back(), nodes);\n\tfor (auto i = 0; i < mole.size(); ++i) {\n\t\tauto m = mole[i];\n\t\tif (std::abs(left - m.position) <= m.time * velocity) {\n\t\t\tsetEdge(start_left, i * 2, sum_cost[i], nodes);\n\t\t}\n\t\tif (std::abs(right - m.position) <= m.time * velocity) {\n\t\t\tsetEdge(start_right, i * 2, sum_cost[i], nodes);\n\t\t}\n\t\tsetEdge(i * 2, i * 2 + 1, 0, nodes);\n\t\tsetEdge(i * 2 + 1, goal, sum_cost.back() - sum_cost[i + 1], nodes);\n\t}\n\tfor (auto i = 0; i < mole.size(); ++i) {\n\t\tfor (auto j = i + 1; j < mole.size(); ++j) {\n\t\t\tif (std::abs(mole[i].position - mole[j].position) <= (mole[j].time - mole[i].time) * velocity) {\n\t\t\t\tsetEdge(i * 2 + 1, j * 2, sum_cost[j] - sum_cost[i + 1], nodes);\n\t\t\t}\n\t\t}\n\t}\n\tstd::vector<int> potential(nodes.size(), 0);\n\tstd::vector<int> min_cost(nodes.size(), 0);\n\tstd::priority_queue<std::tuple<int, int>, std::vector<std::tuple<int, int>>, std::function<bool(const std::tuple<int, int>&, const std::tuple<int, int>&)>> empty_queue([](const std::tuple<int, int> & a, const std::tuple<int, int> & b) {return std::get<1>(a) > std::get<1>(b); });\n\tstd::vector<Edge> prev_edge(nodes.size());\n\tint result = 0;\n\tfor (auto repeat = 0; repeat < 2; ++repeat) {\n\t\tfor (auto i = 0; i < min_cost.size(); ++i) min_cost[i] = INT_MAX;\n\t\tauto queue = empty_queue;\n\t\tmin_cost[start] = 0;\n\t\tqueue.push(std::make_tuple(start, 0));\n\t\twhile (std::get<0>(queue.top()) != goal) {\n\t\t\tauto current = std::get<0>(queue.top());\n\t\t\tauto cost = std::get<1>(queue.top());\n\t\t\tqueue.pop();\n\t\t\tif (min_cost[current] == cost) {\n\t\t\t\tfor (const auto next : nodes[current]) if (next.flow) {\n\t\t\t\t\tif (min_cost[next.to] > cost + potential[current] - potential[next.to] + next.cost) {\n\t\t\t\t\t\tmin_cost[next.to] = cost + potential[current] - potential[next.to] + next.cost;\n\t\t\t\t\t\tprev_edge[next.to] = next;\n\t\t\t\t\t\tqueue.push(std::make_tuple(next.to, min_cost[next.to]));\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tresult += sum_cost.back() - min_cost[goal];\n\t\tauto last = goal;\n\t\twhile (last != start) {\n\t\t\tauto edge = prev_edge[last];\n\t\t\tauto& rev = nodes[edge.to][edge.pair];\n\t\t\trev.flow = true;\n\t\t\tnodes[rev.to][rev.pair].flow = false;\n\t\t\tlast = rev.to;\n\t\t}\n\t\tfor (auto i = 0; i < start; ++i) potential[i] += min_cost[i];\n\t}\n\treturn result;\n}\nint main() {\n\tint n, v, left, right; std::cin >> n >> v >> left >> right;\n\tstd::vector<Mole> mole(n);\n\tfor (auto& m : mole) std::cin >> m.position >> m.time >> m.score;\n\tstd::sort(mole.begin(), mole.end(), [](const Mole a, const Mole b) {return a.time < b.time; });\n\tstd::cout << solve(v, left, right, mole) << std::endl;\n}", "accuracy": 1, "time_ms": 1150, "memory_kb": 240728, "score_of_the_acc": -1.1061, "final_rank": 17 }, { "submission_id": "aoj_2290_3375794", "code_snippet": "// https://onlinejudge.u-aizu.ac.jp/challenges/search/categories/2290\n#include <iostream>\n#include <algorithm>\n#include <cmath>\n#include <queue>\n#include <limits>\ntemplate <typename T>\nconstexpr T INF() { return std::numeric_limits<T>::max() / 16; }\n#include <vector>\ntemplate <typename Cap, typename Cost, typename Ind = std::size_t>\nstruct CostFlow\n{\n CostFlow(const Ind v) : V(v), edge(v) { edge.shrink_to_fit(); }\n void addEdge(const Ind from, const Ind to, const Cap cap, const Cost cost) { edge[from].push_back(Edge{to, (Ind)edge[to].size(), cap, cost, false}), edge[to].push_back(Edge{from, (Ind)(edge[from].size() - 1), 0, -cost, true}); }\n struct Edge\n {\n Ind to, rev;\n Cap cap;\n Cost cost;\n const bool isrev;\n };\n const Ind V;\n std::vector<std::vector<Edge>> edge;\n};\ntemplate <typename T, template <typename, typename, typename, typename> class PotFunc, typename Cap, typename Cost, typename Ind>\nstd::pair<bool, T> PrimalDual(CostFlow<Cap, Cost, Ind>& cflow, const Ind s, const Ind t, Cap f)\n{\n const Ind V = cflow.V;\n T ans = 0;\n std::vector<T> potential = PotFunc<T, Cap, Cost, Ind>()(cflow, s);\n std::vector<T> dist(V, INF<T>());\n using P = std::pair<T, Ind>;\n std::priority_queue<P, std::vector<P>, std::greater<P>> Q;\n std::vector<Ind> prev_v(V), prev_e(V);\n dist.shrink_to_fit(), prev_v.shrink_to_fit(), prev_e.shrink_to_fit();\n while (f > 0) {\n std::fill(dist.begin(), dist.end(), INF<T>());\n dist[s] = 0, Q.push({0, s});\n while (not Q.empty()) {\n const T cost = Q.top().first;\n const Ind v = Q.top().second;\n Q.pop();\n if (dist[v] < cost) { continue; }\n for (Ind i = 0; i < (Ind)cflow.edge[v].size(); i++) {\n const auto& e = cflow.edge[v][i];\n const T pd = potential[v] - potential[e.to];\n if (e.cap == 0 or dist[e.to] <= dist[v] + (T)e.cost + pd) { continue; }\n dist[e.to] = dist[v] + (T)e.cost + pd, prev_v[e.to] = v, prev_e[e.to] = i;\n Q.push({dist[e.to], e.to});\n }\n }\n if (dist[t] == INF<T>()) { return {false, ans}; }\n for (Ind v = 0; v < V; v++) { potential[v] += dist[v]; }\n Cap d = f;\n for (Ind v = t; v != s; v = prev_v[v]) {\n const auto& e = cflow.edge[prev_v[v]][prev_e[v]];\n d = std::min(d, e.cap);\n }\n f = (Cap)(f - d), ans += (T)d * potential[t];\n for (Ind v = t; v != s; v = prev_v[v]) {\n auto& e = cflow.edge[prev_v[v]][prev_e[v]];\n e.cap = (Cap)(e.cap - d), cflow.edge[v][e.rev].cap = (Cap)(cflow.edge[v][e.rev].cap + d);\n }\n }\n return {true, ans};\n}\n#include <algorithm>\n#include <algorithm>\n#include <vector>\nstruct Graph\n{\n Graph(const std::size_t v) : V{v}, edge(v), rev_edge(v) {}\n void addEdge(const std::size_t from, const std::size_t to) { edge[from].push_back(to), rev_edge[to].push_back(from); }\n const std::size_t V;\n std::vector<std::vector<std::size_t>> edge, rev_edge;\n};\nstd::pair<bool, std::vector<std::size_t>> TopSort(const Graph& g)\n{\n std::vector<std::size_t> srt, used(g.V, 0);\n auto dfs = [&](auto&& self, const std::size_t s) -> bool {\n if (used[s] == 1) {\n return false;\n } else if (used[s] == 0) {\n used[s] = 1;\n for (const std::size_t to : g.edge[s]) {\n if (not self(self, to)) { return false; }\n }\n used[s] = 2, srt.push_back(s);\n }\n return true;\n };\n for (std::size_t i = 0; i < g.V; i++) {\n if (not dfs(dfs, i)) { return {false, srt}; }\n }\n std::reverse(srt.begin(), srt.end());\n return {true, srt};\n}\ntemplate <typename T, typename Cap, typename Cost, typename Ind>\nstruct PotDAG\n{\n std::vector<T> operator()(const CostFlow<Cap, Cost, Ind>& cflow, const Ind s) const\n {\n const Ind V = cflow.V;\n std::vector<Ind> ord;\n std::vector<bool> used(V, 0);\n auto dfs = [&](auto&& self, const Ind s) -> void {\n if (not used[s]) {\n used[s] = true;\n for (const auto& e : cflow.edge[s]) {\n if (e.cap == 0) { continue; }\n self(self, e.to);\n }\n ord.push_back(s);\n }\n };\n for (Ind i = 0; i < V; i++) { dfs(dfs, i); }\n std::reverse(ord.begin(), ord.end());\n std::vector<T> ans(V, INF<T>());\n ans.shrink_to_fit();\n for (Ind i = 0; i < V; i++) {\n if (ord[i] == s) { ans[s] = (T)0; }\n for (const auto& e : cflow.edge[ord[i]]) {\n if (e.cap == 0) { continue; }\n ans[e.to] = std::min(ans[e.to], ans[ord[i]] + (T)e.cost);\n }\n }\n return ans;\n }\n};\nstruct Point\n{\n int X, T, P;\n bool operator<(const Point& p) const\n {\n return T < p.T;\n }\n};\n\nint main()\n{\n short N;\n std::cin >> N;\n std::size_t V, L, R;\n std::cin >> V >> L >> R;\n std::vector<Point> point(N);\n point.shrink_to_fit();\n for (short i = 0; i < N; i++) { std::cin >> point[i].X >> point[i].T >> point[i].P; }\n std::sort(point.begin(), point.end());\n CostFlow<char, int, short> cflow((short)(2 * N + 4));\n const short s = 0, t = (short)(2 * N + 3), l = 1, r = 2;\n cflow.addEdge(s, l, 1, 0), cflow.addEdge(s, r, 1, 0);\n for (short i = 0; i < N; i++) {\n cflow.addEdge((short)(2 * i + 3), (short)(2 * i + 4), 1, 0), cflow.addEdge((short)(2 * i + 4), t, 1, -point[i].P);\n if (std::abs(point[i].X - (int)L) <= V * point[i].T) { cflow.addEdge(l, (short)(2 * i + 3), 1, 0); }\n if (std::abs(point[i].X - (int)R) <= V * point[i].T) { cflow.addEdge(r, (short)(2 * i + 3), 1, 0); }\n }\n for (short i = 0; i < N; i++) {\n for (short j = (short)(i + 1); j < N; j++) {\n if (std::abs(point[i].X - point[j].X) <= V * (point[j].T - point[i].T)) { cflow.addEdge((short)(2 * i + 4), (short)(2 * j + 3), 1, -point[i].P); }\n }\n }\n point.clear(), point.shrink_to_fit();\n std::cout << -PrimalDual<int, PotDAG>(cflow, s, t, (char)2).second << std::endl;\n\n return 0;\n}", "accuracy": 1, "time_ms": 1010, "memory_kb": 241836, "score_of_the_acc": -1.0882, "final_rank": 11 }, { "submission_id": "aoj_2290_3375076", "code_snippet": "// https://onlinejudge.u-aizu.ac.jp/challenges/search/categories/2290\n// MLEです (頂点型をshortにして、容量型をcharにすると多分通ります)\n#include <iostream>\n#include <algorithm>\n#include <cmath>\n#include <queue>\n#include <limits>\ntemplate <typename T>\nconstexpr T INF() { return std::numeric_limits<T>::max() / 16; }\n#include <vector>\ntemplate <typename Cap, typename Cost, typename Ind = std::size_t>\nstruct CostFlow\n{\n CostFlow(const Ind v) : V(v), edge(v) { edge.shrink_to_fit(); }\n void addEdge(const Ind from, const Ind to, const Cap cap, const Cost cost) { edge[from].push_back(Edge{to, (Ind)edge[to].size(), cap, cost, false}), edge[to].push_back(Edge{from, (Ind)(edge[from].size() - 1), 0, -cost, true}); }\n struct Edge\n {\n Ind to, rev;\n Cap cap;\n Cost cost;\n const bool isrev;\n };\n const Ind V;\n std::vector<std::vector<Edge>> edge;\n};\ntemplate <typename T, template <typename, typename, typename, typename> class PotFunc, typename Cap, typename Cost, typename Ind>\nstd::pair<bool, T> PrimalDual(CostFlow<Cap, Cost, Ind>& cflow, const Ind s, const Ind t, Cap f)\n{\n const Ind V = cflow.V;\n T ans = 0;\n std::vector<T> potential = PotFunc<T, Cap, Cost, Ind>()(cflow, s);\n std::vector<T> dist(V, INF<T>());\n using P = std::pair<T, Ind>;\n std::priority_queue<P, std::vector<P>, std::greater<P>> Q;\n std::vector<Ind> prev_v(V), prev_e(V);\n dist.shrink_to_fit(), prev_v.shrink_to_fit(), prev_e.shrink_to_fit();\n while (f > 0) {\n std::fill(dist.begin(), dist.end(), INF<T>());\n dist[s] = 0, Q.push({0, s});\n while (not Q.empty()) {\n const T cost = Q.top().first;\n const Ind v = Q.top().second;\n Q.pop();\n if (dist[v] < cost) { continue; }\n for (Ind i = 0; i < cflow.edge[v].size(); i++) {\n const auto& e = cflow.edge[v][i];\n const T pd = potential[v] - potential[e.to];\n if (e.cap == 0 or dist[e.to] <= dist[v] + e.cost + pd) { continue; }\n dist[e.to] = dist[v] + e.cost + pd, prev_v[e.to] = v, prev_e[e.to] = i;\n Q.push({dist[e.to], e.to});\n }\n }\n if (dist[t] == INF<T>()) { return {false, ans}; }\n for (Ind v = 0; v < V; v++) { potential[v] += dist[v]; }\n Cap d = f;\n for (Ind v = t; v != s; v = prev_v[v]) {\n const auto& e = cflow.edge[prev_v[v]][prev_e[v]];\n d = std::min(d, e.cap);\n }\n f -= d, ans += (T)d * potential[t];\n for (Ind v = t; v != s; v = prev_v[v]) {\n auto& e = cflow.edge[prev_v[v]][prev_e[v]];\n e.cap -= d, cflow.edge[v][e.rev].cap += d;\n }\n }\n return {true, ans};\n}\n#include <algorithm>\n#include <algorithm>\n#include <vector>\nstruct Graph\n{\n Graph(const std::size_t v) : V{v}, edge(v), rev_edge(v) {}\n void addEdge(const std::size_t from, const std::size_t to) { edge[from].push_back(to), rev_edge[to].push_back(from); }\n const std::size_t V;\n std::vector<std::vector<std::size_t>> edge, rev_edge;\n};\nstd::pair<bool, std::vector<std::size_t>> TopSort(const Graph& g)\n{\n std::vector<std::size_t> srt, used(g.V, 0);\n auto dfs = [&](auto&& self, const std::size_t s) -> bool {\n if (used[s] == 1) {\n return false;\n } else if (used[s] == 0) {\n used[s] = 1;\n for (const std::size_t to : g.edge[s]) {\n if (not self(self, to)) { return false; }\n }\n used[s] = 2, srt.push_back(s);\n }\n return true;\n };\n for (std::size_t i = 0; i < g.V; i++) {\n if (not dfs(dfs, i)) { return {false, srt}; }\n }\n std::reverse(srt.begin(), srt.end());\n return {true, srt};\n}\ntemplate <typename T, typename Cap, typename Cost, typename Ind>\nstruct PotDAG\n{\n std::vector<T> operator()(const CostFlow<Cap, Cost, Ind>& cflow, const Ind s) const\n {\n const Ind V = cflow.V;\n std::vector<Ind> ord;\n std::vector<bool> used(V, 0);\n auto dfs = [&](auto&& self, const Ind s) -> void {\n if (not used[s]) {\n used[s] = true;\n for (const auto& e : cflow.edge[s]) {\n if (e.cap == 0) { continue; }\n self(self, e.to);\n }\n ord.push_back(s);\n }\n };\n for (Ind i = 0; i < V; i++) { dfs(dfs, i); }\n std::reverse(ord.begin(), ord.end());\n std::vector<T> ans(V, INF<T>());\n ans.shrink_to_fit();\n for (Ind i = 0; i < V; i++) {\n if (ord[i] == s) { ans[s] = 0; }\n for (const auto& e : cflow.edge[ord[i]]) {\n if (e.cap == 0) { continue; }\n ans[e.to] = std::min(ans[e.to], ans[ord[i]] + e.cost);\n }\n }\n return ans;\n }\n};\nstruct Point\n{\n int X, T, P;\n bool operator<(const Point& p) const\n {\n return T < p.T;\n }\n};\n\nint main()\n{\n short N;\n std::cin >> N;\n std::size_t V, L, R;\n std::cin >> V >> L >> R;\n std::vector<Point> point(N);\n point.shrink_to_fit();\n for (short i = 0; i < N; i++) { std::cin >> point[i].X >> point[i].T >> point[i].P; }\n std::sort(point.begin(), point.end());\n CostFlow<char, int, short> cflow(2 * N + 4);\n const short s = 0, t = 2 * N + 3, l = 1, r = 2;\n cflow.addEdge(s, l, 1, 0), cflow.addEdge(s, r, 1, 0);\n for (short i = 0; i < N; i++) {\n cflow.addEdge(2 * i + 3, 2 * i + 4, 1, 0), cflow.addEdge(2 * i + 4, t, 1, -point[i].P);\n if (std::abs(point[i].X - (int)L) <= V * point[i].T) { cflow.addEdge(l, 2 * i + 3, 1, 0); }\n if (std::abs(point[i].X - (int)R) <= V * point[i].T) { cflow.addEdge(r, 2 * i + 3, 1, 0); }\n }\n for (short i = 0; i < N; i++) {\n for (short j = i + 1; j < N; j++) {\n if (std::abs(point[i].X - point[j].X) <= V * (point[j].T - point[i].T)) { cflow.addEdge(2 * i + 4, 2 * j + 3, 1, -point[i].P); }\n }\n }\n point.clear(), point.shrink_to_fit();\n std::cout << -PrimalDual<int, PotDAG>(cflow, s, t, (char)2).second << std::endl;\n\n return 0;\n}", "accuracy": 1, "time_ms": 1010, "memory_kb": 241908, "score_of_the_acc": -1.0886, "final_rank": 12 }, { "submission_id": "aoj_2290_3375072", "code_snippet": "// https://onlinejudge.u-aizu.ac.jp/challenges/search/categories/2290\n// MLEです (頂点型をshortにして、容量型をcharにすると多分通ります)\n#include <iostream>\n#include <algorithm>\n#include <cmath>\n#include <queue>\n#include <limits>\ntemplate <typename T>\nconstexpr T INF() { return std::numeric_limits<T>::max() / 16; }\n#include <vector>\ntemplate <typename Cap, typename Cost, typename Ind = std::size_t>\nstruct CostFlow\n{\n CostFlow(const Ind v) : V(v), edge(v) { edge.shrink_to_fit(); }\n void addEdge(const Ind from, const Ind to, const Cap cap, const Cost cost) { edge[from].push_back(Edge{to, (Ind)edge[to].size(), cap, cost, false}), edge[to].push_back(Edge{from, (Ind)(edge[from].size() - 1), 0, -cost, true}); }\n struct Edge\n {\n Ind to, rev;\n Cap cap;\n Cost cost;\n const bool isrev;\n };\n const Ind V;\n std::vector<std::vector<Edge>> edge;\n};\ntemplate <typename T, template <typename, typename, typename, typename> class PotFunc, typename Cap, typename Cost, typename Ind>\nstd::pair<bool, T> PrimalDual(CostFlow<Cap, Cost, Ind>& cflow, const Ind s, const Ind t, Cap f)\n{\n const Ind V = cflow.V;\n T ans = 0;\n std::vector<T> potential = PotFunc<T, Cap, Cost, Ind>()(cflow, s);\n std::vector<T> dist(V, INF<T>());\n using P = std::pair<T, Ind>;\n std::priority_queue<P, std::vector<P>, std::greater<P>> Q;\n std::vector<Ind> prev_v(V), prev_e(V);\n dist.shrink_to_fit(), prev_v.shrink_to_fit(), prev_e.shrink_to_fit();\n while (f > 0) {\n std::fill(dist.begin(), dist.end(), INF<T>());\n dist[s] = 0, Q.push({0, s});\n while (not Q.empty()) {\n const T cost = Q.top().first;\n const Ind v = Q.top().second;\n Q.pop();\n if (dist[v] < cost) { continue; }\n for (Ind i = 0; i < cflow.edge[v].size(); i++) {\n const auto& e = cflow.edge[v][i];\n const T pd = potential[v] - potential[e.to];\n if (e.cap == 0 or dist[e.to] <= dist[v] + e.cost + pd) { continue; }\n dist[e.to] = dist[v] + e.cost + pd, prev_v[e.to] = v, prev_e[e.to] = i;\n Q.push({dist[e.to], e.to});\n }\n }\n if (dist[t] == INF<T>()) { return {false, ans}; }\n for (Ind v = 0; v < V; v++) { potential[v] += dist[v]; }\n Cap d = f;\n for (Ind v = t; v != s; v = prev_v[v]) {\n const auto& e = cflow.edge[prev_v[v]][prev_e[v]];\n d = std::min(d, e.cap);\n }\n f -= d, ans += (T)d * potential[t];\n for (Ind v = t; v != s; v = prev_v[v]) {\n auto& e = cflow.edge[prev_v[v]][prev_e[v]];\n e.cap -= d, cflow.edge[v][e.rev].cap += d;\n }\n }\n return {true, ans};\n}\n#include <algorithm>\n#include <vector>\nstruct Graph\n{\n Graph(const std::size_t v) : V{v}, edge(v), rev_edge(v) {}\n void addEdge(const std::size_t from, const std::size_t to) { edge[from].push_back(to), rev_edge[to].push_back(from); }\n const std::size_t V;\n std::vector<std::vector<std::size_t>> edge, rev_edge;\n};\nstd::pair<bool, std::vector<std::size_t>> TopSort(const Graph& g)\n{\n std::vector<std::size_t> srt, used(g.V, 0);\n auto dfs = [&](auto&& self, const std::size_t s) -> bool {\n if (used[s] == 1) {\n return false;\n } else if (used[s] == 0) {\n used[s] = 1;\n for (const std::size_t to : g.edge[s]) {\n if (not self(self, to)) { return false; }\n }\n used[s] = 2, srt.push_back(s);\n }\n return true;\n };\n for (std::size_t i = 0; i < g.V; i++) {\n if (not dfs(dfs, i)) { return {false, srt}; }\n }\n std::reverse(srt.begin(), srt.end());\n return {true, srt};\n}\ntemplate <typename T, typename Cap, typename Cost, typename Ind>\nstruct PotDAG\n{\n std::vector<T> operator()(const CostFlow<Cap, Cost, Ind>& cflow, const Ind s) const\n {\n const Ind V = cflow.V;\n std::vector<Ind> ord(V);\n for (Ind i = 0; i < V; i++) { ord[i] = i; }\n std::vector<T> ans(V, INF<T>());\n ans.shrink_to_fit();\n for (Ind i = 0; i < V; i++) {\n if (ord[i] == s) { ans[s] = 0; }\n for (const auto& e : cflow.edge[ord[i]]) {\n if (e.cap == 0) { continue; }\n ans[e.to] = std::min(ans[e.to], ans[ord[i]] + e.cost);\n }\n }\n return ans;\n }\n};\nstruct Point\n{\n int X, T, P;\n bool operator<(const Point& p) const\n {\n return T < p.T;\n }\n};\n\nint main()\n{\n short N;\n std::cin >> N;\n std::size_t V, L, R;\n std::cin >> V >> L >> R;\n std::vector<Point> point(N);\n point.shrink_to_fit();\n for (short i = 0; i < N; i++) { std::cin >> point[i].X >> point[i].T >> point[i].P; }\n std::sort(point.begin(), point.end());\n CostFlow<char, int, short> cflow(2 * N + 4);\n const short s = 0, t = 2 * N + 3, l = 1, r = 2;\n cflow.addEdge(s, l, 1, 0), cflow.addEdge(s, r, 1, 0);\n for (short i = 0; i < N; i++) {\n cflow.addEdge(2 * i + 3, 2 * i + 4, 1, 0), cflow.addEdge(2 * i + 4, t, 1, -point[i].P);\n if (std::abs(point[i].X - (int)L) <= V * point[i].T) { cflow.addEdge(l, 2 * i + 3, 1, 0); }\n if (std::abs(point[i].X - (int)R) <= V * point[i].T) { cflow.addEdge(r, 2 * i + 3, 1, 0); }\n }\n for (short i = 0; i < N; i++) {\n for (short j = i + 1; j < N; j++) {\n if (std::abs(point[i].X - point[j].X) <= V * (point[j].T - point[i].T)) { cflow.addEdge(2 * i + 4, 2 * j + 3, 1, -point[i].P); }\n }\n }\n point.clear(), point.shrink_to_fit();\n std::cout << -PrimalDual<int, PotDAG>(cflow, s, t, (char)2).second << std::endl;\n\n return 0;\n}", "accuracy": 1, "time_ms": 1000, "memory_kb": 241684, "score_of_the_acc": -1.0857, "final_rank": 10 }, { "submission_id": "aoj_2290_3268397", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n//#define HUGE_NUM 99999999999999999\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n#define HUGE_NUM 10000000000\n#define NUM 6050\n\ntypedef pair<ll,ll> P;\n\nstruct Edge{\n Edge(int arg_to,ll arg_capacity,ll arg_cost,int arg_rev_index){\n to = arg_to;\n capacity = arg_capacity;\n cost = arg_cost;\n rev_index = arg_rev_index;\n }\n\n int to,rev_index;\n ll capacity,cost;\n};\n\nstruct Info{\n void set(int arg_x,int arg_appear_time,ll arg_value){\n x = arg_x;\n appear_time = arg_appear_time;\n value = arg_value;\n }\n bool operator<(const struct Info & arg) const{\n return appear_time < arg.appear_time;\n }\n int x,appear_time;\n ll value;\n};\n\nint N,speed;\nint V;\nvector<Edge> G[NUM];\nll h[NUM];\nll dist[NUM];\nint pre_node[NUM],pre_edge[NUM];\nint index_IN[NUM],index_OUT[NUM];\nint in_num[NUM];\nbool visited[NUM];\nInfo LEFT,RIGHT,info[NUM];\n\n\nvoid add_edge(int from,int to,ll capacity,ll cost){\n G[from].push_back(Edge(to,capacity,cost,G[to].size()));\n G[to].push_back(Edge(from,0,-cost,G[from].size()-1));\n}\n\n\nll min_cost_flow(int source,int sink,ll flow){\n ll ret = 0;\n for(int i = 0; i < V; i++){\n h[i] = HUGE_NUM;\n in_num[i] = 0;\n }\n\n h[source] = 0;\n\n for(int i = 0; i < V; i++){\n for(ll k = 0; k < G[i].size(); k++){\n if(G[i][k].capacity == 0)continue;\n\n in_num[G[i][k].to]++;\n }\n }\n\n queue<int> TOPO;\n for(int i = 0; i < V; i++){\n\n if(in_num[i] == 0){\n TOPO.push(i);\n }\n }\n\n int tmp_node;\n\n while(!TOPO.empty()){\n tmp_node = TOPO.front();\n TOPO.pop();\n\n for(int i = 0; i < G[tmp_node].size(); i++){\n if(G[tmp_node][i].capacity <= 0)continue;\n\n h[G[tmp_node][i].to] = min(h[G[tmp_node][i].to],h[tmp_node]+G[tmp_node][i].cost);\n\n in_num[G[tmp_node][i].to]--;\n if(in_num[G[tmp_node][i].to] == 0){\n TOPO.push(G[tmp_node][i].to);\n }\n }\n }\n\n while(flow > 0){\n\n for(int i = 0; i < V; i++)dist[i] = HUGE_NUM;\n dist[source] = 0;\n\n for(int i = 0; i < V; i++)visited[i] = false;\n\n while(true){\n\n tmp_node = -1;\n\n for(ll i = 0; i < V; i++){\n if(visited[i] == false && (tmp_node == -1 || dist[tmp_node] > dist[i])){\n tmp_node = i;\n }\n }\n\n if(tmp_node == -1)break;\n\n visited[tmp_node] = true;\n\n for(int i = 0; i < G[tmp_node].size(); i++){\n Edge &e = G[tmp_node][i];\n if(e.capacity == 0 || dist[e.to] <= dist[tmp_node]+e.cost+h[tmp_node]-h[e.to])continue;\n dist[e.to] = dist[tmp_node]+e.cost+h[tmp_node]-h[e.to];\n pre_node[e.to] = tmp_node;\n pre_edge[e.to] = i;\n\n }\n }\n\n if(dist[sink] == HUGE_NUM){\n return -1;\n }\n\n for(int node_id = 0; node_id < V; node_id++)h[node_id] += dist[node_id];\n\n ll tmp_flow = flow;\n\n for(int node_id = sink; node_id != source; node_id = pre_node[node_id]){\n\n tmp_flow = min(tmp_flow,G[pre_node[node_id]][pre_edge[node_id]].capacity);\n }\n flow -= tmp_flow;\n ret += tmp_flow*h[sink];\n\n for(int node_id = sink; node_id != source; node_id = pre_node[node_id]){\n Edge &e = G[pre_node[node_id]][pre_edge[node_id]];\n e.capacity -= tmp_flow;\n G[node_id][e.rev_index].capacity += tmp_flow;\n }\n }\n return ret;\n}\n\n\nint main(){\n\n scanf(\"%d %d %d %d\",&N,&speed,&LEFT.x,&RIGHT.x);\n\n for(int i = 0; i < N; i++){\n\n scanf(\"%d %d %d\",&info[i].x,&info[i].appear_time,&info[i].value);\n }\n sort(info,info+N);\n\n int source = 0,sink = 1,first_left = 2,first_right = 3,index = 4;\n\n for(int i = 0; i < N; i++){\n\n index_IN[i] = index++;\n }\n for(int i = 0; i < N; i++){\n\n index_OUT[i] = index++;\n add_edge(index_OUT[i],sink,1,0);\n }\n for(ll i = 0; i < N; i++){\n\n add_edge(index_IN[i],index_OUT[i],1,-info[i].value);\n }\n\n add_edge(source,first_left,1,0);\n add_edge(first_left,sink,1,0);\n\n for(int i = 0; i < N; i++){\n\n if(abs(info[i].x-LEFT.x) <= speed*info[i].appear_time){\n\n add_edge(first_left,index_IN[i],1,0);\n }\n }\n\n add_edge(source,first_right,1,0);\n add_edge(first_right,sink,1,0);\n\n for(int i = 0; i < N; i++){\n\n if(abs(info[i].x-RIGHT.x) <= speed*info[i].appear_time){\n\n add_edge(first_right,index_IN[i],1,0);\n }\n }\n\n for(int i = 0; i < N-1; i++){\n for(int k = i+1; k < N; k++){\n if(abs(info[k].x-info[i].x) <= speed*(info[k].appear_time-info[i].appear_time)){\n\n add_edge(index_OUT[i],index_IN[k],1,0);\n }\n }\n }\n\n add_edge(source,sink,2,0);\n\n V = index;\n\n ll ans = min_cost_flow(source,sink,2);\n\n printf(\"%lld\\n\",-ans);\n\n return 0;\n}", "accuracy": 1, "time_ms": 340, "memory_kb": 256348, "score_of_the_acc": -1.051, "final_rank": 5 } ]
aoj_2295_cpp
F: Power of Power ICPC で良い成績を収めるには修行が欠かせない.うさぎは ICPC で勝ちたいので,今日も修行をすることにした. 今日の修行は,非常に大きな数の計算を行って,計算力をつけて意識を高めようというものである.大きい数が簡単に出てくる演算として,累乗が挙げられる. N 個の非負整数 A 1 , A 2 , ..., A N がある.これらの並べ替え B 1 , B 2 , ..., B N であって, B 1 B 2 ... B N - 1 B N が最大になるものを求めたい.うさぎたちにとってはもちろん常識であるが,この計算は右上から順に行う.また,0 0 = 1 であると約束することにする. 最大条件を満たす B 1 , B 2 , ..., B N は複数通りあるかもしれない.そのような場合は,そのうちで辞書順最小の列を選ぶことにしよう. Input N A 1 ... A N 1 ≤ N ≤ 100,0 ≤ A i ≤ 1,000,000,000 を満たす. Output 出力は N 行からなる. i 行目には B i を出力せよ. Sample Input 1 4 7 5 10 6 Sample Output 1 5 6 7 10 Sample Input 2 3 0 0 1000000000 Sample Output 2 1000000000 0 0
[ { "submission_id": "aoj_2295_2389243", "code_snippet": "#include <bits/stdc++.h>\n#define FOR(i,k,n) for(int i=(k);i<(int)(n);++i)\n#define REP(i,n) FOR(i,0,n)\n#define ALL(x) begin(x),end(x)\n\nusing namespace std;\nusing ld = long double;\n\nvector<int> solve2(const vector<int>& v) {\n int n = v.size();\n int mi = 0, mj = 0;\n ld mx = -INFINITY;\n REP(i,n)REP(j,n) {\n if (i == j) continue;\n ld val;\n if (v[i] == 0 && v[j] == 0) {\n val = 0;\n } else {\n val = v[i] * log(v[j]);\n }\n if (val > mx) {\n mx = val;\n mi = i;\n mj = j;\n }\n }\n vector<int> res = {v[mj], v[mi]};\n return res;\n}\n\nvector<int> solve(const vector<int>& v) {\n int n = v.size();\n if (n <= 1) return v;\n if (n == 2) {\n return solve2(v);\n }\n int mi = 0, mj = 0, mk = 0;\n ld mx = -INFINITY;\n REP(i,n)REP(j,n)REP(k,n) {\n if (i == j) continue;\n if (i == k) continue;\n if (j == k) continue;\n ld val;\n if (v[j] == 0 && v[k] == 0) {\n val = 0;\n } else {\n val = v[j] * log(4.0 * v[k]);\n }\n if (v[i] == 0 && val == 0) {\n val = 0;\n } else {\n val = v[i] * log(val);\n }\n if (val > mx) {\n mx = val;\n mi = i;\n mj = j;\n mk = k;\n }\n }\n vector<int> er = {mi, mj, mk};\n sort(ALL(er), greater<int>());\n auto nv = v;\n REP(i,3) nv.erase(begin(nv)+er[i]);\n sort(ALL(nv));\n nv.push_back(v[mk]);\n nv.push_back(v[mj]);\n nv.push_back(v[mi]);\n return nv;\n}\n\nint main() {\n int n;\n cin>>n;\n vector<int> v;\n REP(i,n) {\n int m;\n cin>>m;\n v.emplace_back(m);\n }\n auto res = solve(v);\n for (int i:res) {\n cout << i << endl;\n }\n return 0;\n}", "accuracy": 0.041666666666666664, "time_ms": 40, "memory_kb": 3572, "score_of_the_acc": -0.7723, "final_rank": 2 }, { "submission_id": "aoj_2295_2389240", "code_snippet": "#include <bits/stdc++.h>\n#define FOR(i,k,n) for(int i=(k);i<(int)(n);++i)\n#define REP(i,n) FOR(i,0,n)\n#define ALL(x) begin(x),end(x)\n\nusing namespace std;\nusing ld = long double;\n\nvector<int> solve2(const vector<int>& v) {\n int n = v.size();\n int mi = 0, mj = 0;\n ld mx = -INFINITY;\n REP(i,n)REP(j,n) {\n if (i == j) continue;\n ld val;\n if (v[i] == 0 && v[j] == 0) {\n val = 0;\n } else {\n val = v[i] * log(v[j]);\n }\n if (val > mx) {\n mx = val;\n mi = i;\n mj = j;\n }\n }\n vector<int> res = {mj, mi};\n return res;\n}\n\nvector<int> solve(const vector<int>& v) {\n int n = v.size();\n if (n <= 1) return v;\n if (n == 2) {\n return solve2(v);\n }\n int mi = 0, mj = 0, mk = 0;\n ld mx = -INFINITY;\n REP(i,n)REP(j,n)REP(k,n) {\n if (i == j) continue;\n if (i == k) continue;\n if (j == k) continue;\n ld val;\n if (v[j] == 0 && v[k] == 0) {\n val = 0;\n } else {\n val = v[j] * log(4.0 * v[k]);\n }\n if (v[i] == 0 && val == 0) {\n val = 0;\n } else {\n val = v[i] * log(val);\n }\n if (val > mx) {\n mx = val;\n mi = i;\n mj = j;\n mk = k;\n }\n }\n vector<int> er = {mi, mj, mk};\n sort(ALL(er), greater<int>());\n auto nv = v;\n REP(i,3) nv.erase(begin(nv)+er[i]);\n sort(ALL(nv));\n nv.push_back(v[mk]);\n nv.push_back(v[mj]);\n nv.push_back(v[mi]);\n return nv;\n}\n\nint main() {\n int n;\n cin>>n;\n vector<int> v;\n REP(i,n) {\n int m;\n cin>>m;\n v.emplace_back(m);\n }\n auto res = solve(v);\n for (int i:res) {\n cout << i << endl;\n }\n return 0;\n}", "accuracy": 0.025, "time_ms": 40, "memory_kb": 3528, "score_of_the_acc": -0.7548, "final_rank": 3 }, { "submission_id": "aoj_2295_2389237", "code_snippet": "#include <bits/stdc++.h>\n#define FOR(i,k,n) for(int i=(k);i<(int)(n);++i)\n#define REP(i,n) FOR(i,0,n)\n#define ALL(x) begin(x),end(x)\n\nusing namespace std;\nusing ld = long double;\n\nvector<int> solve(const vector<int>& v) {\n int n = v.size();\n if (n <= 1) return v;\n int mi = 0, mj = 0, mk = 0;\n ld mx = -INFINITY;\n REP(i,n)REP(j,n)REP(k,n) {\n if (i == j) continue;\n if (i == k) continue;\n if (j == k) continue;\n ld val;\n if (v[j] == 0 && v[k] == 0) {\n val = 0;\n } else {\n val = v[j] * log(4.0 * v[k]);\n }\n if (v[i] == 0 && val == 0) {\n val = 0;\n } else {\n val = v[i] * log(val);\n }\n if (val > mx) {\n mx = val;\n mi = i;\n mj = j;\n mk = k;\n }\n }\n vector<int> er = {mi, mj, mk};\n sort(ALL(er), greater<int>());\n auto nv = v;\n REP(i,3) nv.erase(begin(nv)+er[i]);\n sort(ALL(nv));\n nv.push_back(v[mk]);\n nv.push_back(v[mj]);\n nv.push_back(v[mi]);\n return nv;\n}\n\nint main() {\n int n;\n cin>>n;\n vector<int> v;\n REP(i,n) {\n int m;\n cin>>m;\n v.emplace_back(m);\n }\n auto res = solve(v);\n for (int i:res) {\n cout << i << endl;\n }\n return 0;\n}", "accuracy": 0.025, "time_ms": 50, "memory_kb": 3516, "score_of_the_acc": -1, "final_rank": 4 }, { "submission_id": "aoj_2295_2163234", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\nusing namespace std;\n\nstring dp[101][101][2];//zero, one, 0or1\n\nint main(){\n for(int y=1;y<=100;y++) dp[0][y][1]=string(y,'1');\n for(int x=1;x<=100;x++){\n if(x%2){\n dp[x][0][0]=string(x,'0');\n }else{\n dp[x][0][1]=string(x,'0');\n }\n }\n for(int x=1;x<=100;x++){\n for(int y=1;y<=100;y++){\n if(dp[x-1][y][1]!=\"\"){\n dp[x][y][0]=\"0\"+dp[x-1][y][1];\n }else{\n dp[x][y][0]=\"\";\n }\n dp[x][y][1]=\"1\"+dp[x][y-1][0];\n if(dp[x-1][y][0]!=\"\") dp[x][y][1]=min(dp[x][y][1], \"0\"+dp[x-1][y][0]);\n if(dp[x][y-1][1]!=\"\") dp[x][y][1]=min(dp[x][y][1], \"1\"+dp[x][y-1][1]);\n }\n }\n\n int N;\n cin>>N;\n vector<int> V;\n int zero=0, one=0;\n for(int i=0;i<N;i++){\n int a;\n cin>>a;\n if(a==0){\n zero++;\n }else if(a==1){\n one++;\n }else{\n V.push_back(a);\n }\n }\n sort(V.begin(),V.end());\n string s;\n if(zero%2==1&&!one){\n if(V.size()>=2) swap(V.front(),V.back());\n if(V.size()>0){\n s='0'+V.back();\n V.pop_back();\n }\n }\n int n = V.size();\n if(n>=2&&V[n-2]==2&&V[n-1]==3) swap(V[n-2],V[n-1]);\n for(auto v:V){\n cout<<v<<endl;\n }\n if(zero%2&&!one){\n s+=dp[zero][one][0];\n }else{\n s+=dp[zero][one][1];\n }\n for(auto c:s)cout<<c<<endl;\n\n return 0;\n}", "accuracy": 0.4, "time_ms": 10, "memory_kb": 6032, "score_of_the_acc": -1, "final_rank": 1 } ]
aoj_2292_cpp
C: Common Palindromes ICPC で良い成績を収めるには修行が欠かせない.うさぎは ICPC で勝ちたいので,今日も修行をすることにした. 今日の修行は,文字列中にある回文を探すことによって,文章から隠れたメッセージを読解する能力を高めようというものである.回文はたくさんあるかもしれないので,探すついでに個数も数えてしまいたい. 2 つの文字列 S , T が与えられるので,以下を満たす整数の組 ( i , j , k , l ) の個数を求めたい. 1 ≤ i ≤ j ≤ ( S の長さ). 1 ≤ k ≤ l ≤ ( T の長さ). S の i 文字目から j 文字目までを取り出した部分文字列は, T の k 文字目から l 文字目までを取り出した部分文字列と同一であり,さらにこれらは回文 (左から読んでも右から読んでも同じになる文字列) となっている. Input S T 文字列 S , T はともに長さが 1 以上 50,000 以下であり,アルファベット大文字からなる. Output 条件を満たす整数の組 ( i , j , k , l ) の個数を 1 行に出力せよ. Sample Input 1 ICPC CPCPC Sample Output 1 10 Sample Input 2 BABBAB ABBA Sample Output 2 14 Sample Input 3 MYON USAGI Sample Output 3 0
[ { "submission_id": "aoj_2292_9777583", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nconst long long MOD = 10000000000000099;\nconst long long BASE = 1333;\nconst int MAXN = 5 * 100000;\n\nlong long POWER[MAXN + 1];\n\nlong long mul(long long a, long long b) {\n return ((__int128_t)a * b) % MOD;\n}\n\nclass RollingHash {\npublic:\n RollingHash(const string& s) : size(s.size()), str(s) {\n hashed.resize(size + 1);\n for (int i = 0; i < size; ++i) {\n hashed[i + 1] = mul(hashed[i], BASE) + (s[i]);\n hashed[i + 1] %= MOD;\n }\n }\n\n long long query(int l, int r) {\n assert(0 <= l && l <= r && r <= size);\n return (hashed[r] - mul(hashed[l], POWER[r - l]) + MOD) % MOD;\n }\n\nprivate:\n string str;\n vector<long long> hashed;\n int size;\n};\n\nclass Eertree {\npublic:\n Eertree(const string& s) : str(s), size(s.size()), r1(s), r2(string(s.rbegin(), s.rend())) {}\n\n void build() {\n const int BEGIN = 'A'; // or 'a'\n const int PALINDROME_SIZE = 2 * size;\n vector<vector<int>> G(PALINDROME_SIZE);\n H.resize(PALINDROME_SIZE);\n W.resize(PALINDROME_SIZE, 0);\n vector<unordered_map<long long, int>> ptr(27);\n \n ptr[26][0] = 0;\n H[0] = {26, 0};\n int free = 1;\n\n for (int i = 0; i < size; ++i) {\n int ID = str[i] - BEGIN;\n int ok = -1, ng = min(i, size - i - 1) + 1;\n\n while (ng - ok > 1) {\n int x = (ng + ok) >> 1;\n long long h1 = r1.query(i + 1, i + 1 + x);\n long long h2 = r2.query(size - i, size - i + x);\n if (ptr[ID].count(h1) && ptr[ID][h1] >= 0 && h1 == h2) ok = x;\n else ng = x;\n }\n\n if (ok == -1) {\n ptr[ID][0] = free;\n H[free] = {ID, 0};\n free++;\n ok = 0;\n }\n\n int par = ptr[ID][r1.query(i + 1, i + 1 + ok)];\n for (int j = ok + 1; j < min(i + 1, size - i); ++j) {\n if (str[i + j] == str[i - j]) {\n long long h1 = r1.query(i + 1, i + j + 1);\n ptr[ID][h1] = free;\n G[par].push_back(free);\n H[free] = {ID, h1};\n par = free;\n free++;\n } else {\n break;\n }\n }\n W[par] += 1;\n\n // Centroid = ''\n ok = 0;\n ng = min(i, size - i - 1) + 1;\n ID = 26;\n\n while (ng - ok > 1) {\n int x = (ng + ok) >> 1;\n long long h1 = r1.query(i, i + x);\n long long h2 = r2.query(size - i, size - i + x);\n if (ptr[ID].count(h1) && ptr[ID][h1] >= 0 && h1 == h2) ok = x;\n else ng = x;\n }\n\n par = ptr[ID][r1.query(i, i + ok)];\n for (int j = ok + 1; j < min(i + 1, size - i + 1); ++j) {\n if (str[i + j - 1] == str[i - j]) {\n long long h1 = r1.query(i, i + j);\n ptr[ID][h1] = free;\n G[par].push_back(free);\n H[free] = {ID, h1};\n par = free;\n free++;\n } else {\n break;\n }\n }\n W[par] += 1;\n }\n\n function<long long(int)> dfs = [&](int v) {\n for (int nxt : G[v]) {\n W[v] += dfs(nxt);\n }\n return W[v];\n };\n\n for (int ID = 0; ID < 27; ++ID) {\n if (ptr[ID].count(0) && ptr[ID][0] >= 0) dfs(ptr[ID][0]);\n }\n W[0] = 0;\n }\n\n vector<long long> getW() const { return W; }\n vector<pair<int, long long>> getH() const { return H; }\n\nprivate:\n string str;\n int size;\n RollingHash r1, r2;\n vector<long long> W;\n vector<pair<int, long long>> H;\n};\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n\n for (int i = 0; i <= MAXN; ++i) {\n POWER[i] = (i == 0) ? 1 : mul(POWER[i - 1], BASE);\n }\n\n string s, t;\n cin >> s >> t;\n\n Eertree palS(s);\n Eertree palT(t);\n palS.build();\n palT.build();\n\n vector<long long> SW = palS.getW();\n vector<long long> TW = palT.getW();\n vector<pair<int, long long>> SH = palS.getH();\n vector<pair<int, long long>> TH = palT.getH();\n\n unordered_map<long long, array<long long, 2>> pair;\n \n for (size_t i = 0; i < SW.size(); ++i) {\n pair[SH[i].first*MOD+SH[i].second][0] += SW[i];\n }\n for (size_t i = 0; i < TW.size(); ++i) {\n pair[TH[i].first*MOD+TH[i].second][1] += TW[i];\n }\n\n long long ans = 0;\n for (const auto& p : pair) {\n ans += p.second[0] * p.second[1];\n }\n\n cout << ans << '\\n';\n return 0;\n}", "accuracy": 1, "time_ms": 290, "memory_kb": 25348, "score_of_the_acc": -1.4784, "final_rank": 16 }, { "submission_id": "aoj_2292_9777531", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nconst long long MOD = (1LL << 61) - 1;\nconst long long BASE = 1333;\nconst int MAXN = 5 * 100000;\n\nlong long POWER[MAXN + 1];\n\nlong long mul(long long a, long long b) {\n return ((__int128_t)a * b) % MOD;\n}\n\nclass RollingHash {\npublic:\n RollingHash(const string& s) : size(s.size()), str(s) {\n hashed.resize(size + 1);\n for (int i = 0; i < size; ++i) {\n hashed[i + 1] = mul(hashed[i], BASE) + (s[i]);\n hashed[i + 1] %= MOD;\n }\n }\n\n long long query(int l, int r) {\n assert(0 <= l && l <= r && r <= size);\n return (hashed[r] - mul(hashed[l], POWER[r - l]) + MOD) % MOD;\n }\n\nprivate:\n string str;\n vector<long long> hashed;\n int size;\n};\n\nclass Eertree {\npublic:\n Eertree(const string& s) : str(s), size(s.size()), r1(s), r2(string(s.rbegin(), s.rend())) {}\n\n void build() {\n const int BEGIN = 'A'; // or 'a'\n const int PALINDROME_SIZE = 2 * size;\n vector<vector<int>> G(PALINDROME_SIZE);\n H.resize(PALINDROME_SIZE);\n W.resize(PALINDROME_SIZE, 0);\n vector<unordered_map<long long, int>> ptr(27);\n \n ptr[26][0] = 0;\n H[0] = {26, 0};\n int free = 1;\n\n for (int i = 0; i < size; ++i) {\n int ID = str[i] - BEGIN;\n int ok = -1, ng = min(i, size - i - 1) + 1;\n\n while (ng - ok > 1) {\n int x = (ng + ok) >> 1;\n long long h1 = r1.query(i + 1, i + 1 + x);\n long long h2 = r2.query(size - i, size - i + x);\n if (ptr[ID].count(h1) && ptr[ID][h1] >= 0 && h1 == h2) ok = x;\n else ng = x;\n }\n\n if (ok == -1) {\n ptr[ID][0] = free;\n H[free] = {ID, 0};\n free++;\n ok = 0;\n }\n\n int par = ptr[ID][r1.query(i + 1, i + 1 + ok)];\n for (int j = ok + 1; j < min(i + 1, size - i); ++j) {\n if (str[i + j] == str[i - j]) {\n long long h1 = r1.query(i + 1, i + j + 1);\n ptr[ID][h1] = free;\n G[par].push_back(free);\n H[free] = {ID, h1};\n par = free;\n free++;\n } else {\n break;\n }\n }\n W[par] += 1;\n\n // Centroid = ''\n ok = 0;\n ng = min(i, size - i - 1) + 1;\n ID = 26;\n\n while (ng - ok > 1) {\n int x = (ng + ok) >> 1;\n long long h1 = r1.query(i, i + x);\n long long h2 = r2.query(size - i, size - i + x);\n if (ptr[ID].count(h1) && ptr[ID][h1] >= 0 && h1 == h2) ok = x;\n else ng = x;\n }\n\n par = ptr[ID][r1.query(i, i + ok)];\n for (int j = ok + 1; j < min(i + 1, size - i + 1); ++j) {\n if (str[i + j - 1] == str[i - j]) {\n long long h1 = r1.query(i, i + j);\n ptr[ID][h1] = free;\n G[par].push_back(free);\n H[free] = {ID, h1};\n par = free;\n free++;\n } else {\n break;\n }\n }\n W[par] += 1;\n }\n\n function<long long(int)> dfs = [&](int v) {\n for (int nxt : G[v]) {\n W[v] += dfs(nxt);\n }\n return W[v];\n };\n\n for (int ID = 0; ID < 27; ++ID) {\n if (ptr[ID].count(0) && ptr[ID][0] >= 0) dfs(ptr[ID][0]);\n }\n W[0] = 0;\n }\n\n vector<long long> getW() const { return W; }\n vector<pair<int, long long>> getH() const { return H; }\n\nprivate:\n string str;\n int size;\n RollingHash r1, r2;\n vector<long long> W;\n vector<pair<int, long long>> H;\n};\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n\n for (int i = 0; i <= MAXN; ++i) {\n POWER[i] = (i == 0) ? 1 : mul(POWER[i - 1], BASE);\n }\n\n string s, t;\n cin >> s >> t;\n\n Eertree palS(s);\n Eertree palT(t);\n palS.build();\n palT.build();\n\n vector<long long> SW = palS.getW();\n vector<long long> TW = palT.getW();\n vector<pair<int, long long>> SH = palS.getH();\n vector<pair<int, long long>> TH = palT.getH();\n\n unordered_map<long long, array<long long, 2>> pair;\n \n for (size_t i = 0; i < SW.size(); ++i) {\n pair[SH[i].first*MOD+SH[i].second][0] += SW[i];\n }\n for (size_t i = 0; i < TW.size(); ++i) {\n pair[TH[i].first*MOD+TH[i].second][1] += TW[i];\n }\n\n long long ans = 0;\n for (const auto& p : pair) {\n ans += p.second[0] * p.second[1];\n }\n\n cout << ans << '\\n';\n return 0;\n}", "accuracy": 0.11666666666666667, "time_ms": 300, "memory_kb": 22600, "score_of_the_acc": -1.4275, "final_rank": 18 }, { "submission_id": "aoj_2292_9753483", "code_snippet": "#include <algorithm>\n#include <limits>\n#include <map>\n#include <numeric>\n#include <ranges>\n#include <type_traits>\n#include <vector>\nnamespace nono {\ntemplate <class T = char>\nclass Eertree {\n struct Node {\n Node(int len, int parent = 0, int link = 0): len(len), parent(parent), link(link) {}\n int len;\n int parent;\n int link;\n std::map<T, int> to;\n };\n public:\n Eertree(): pos_(0) {\n nodes_.emplace_back(Node(-1));\n nodes_.emplace_back(Node(0));\n seq_.push_back(std::numeric_limits<T>::max());\n }\n int size() { return nodes_.size() - 2; }\n void add(T c) {\n const int i = seq_.size();\n seq_.push_back(c);\n pos_ = go(pos_, i);\n if (!nodes_[pos_].to.contains(c)) {\n nodes_[pos_].to[c] = nodes_.size();\n int link = pos_ == 0 ? 1 : nodes_[go(nodes_[pos_].link, i)].to[c];\n nodes_.emplace_back(nodes_[pos_].len + 2, pos_, link);\n }\n pos_ = nodes_[pos_].to[c];\n node_id_.push_back(pos_);\n }\n template <std::ranges::random_access_range R>\n void add(const R& seq) {\n static_assert(std::is_same_v<typename R::value_type, T>);\n for (auto c: seq) add(c);\n }\n std::vector<int> node_id() { return node_id_; }\n std::vector<Node> nodes() { return nodes_; }\n std::vector<int> freq() {\n std::vector<int> ids(nodes_.size());\n std::iota(ids.begin(), ids.end(), 0);\n std::sort(ids.begin(), ids.end(),\n [&](int lhs, int rhs) { return nodes_[lhs].len > nodes_[rhs].len; });\n std::vector<int> result(nodes_.size());\n for (auto id: node_id_) result[id]++;\n for (auto id: ids) { result[nodes_[id].link] += result[id]; }\n return result;\n }\n private:\n bool fail(int pos, int i) { return seq_[i] != seq_[i - nodes_[pos].len - 1]; }\n int go(int pos, int i) {\n while (fail(pos, i)) pos = nodes_[pos].link;\n return pos;\n }\n int pos_;\n std::vector<T> seq_;\n std::vector<int> node_id_;\n std::vector<Node> nodes_;\n};\n} // namespace nono\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing ull = unsigned long long;\ntemplate <class T>\nusing max_heap = priority_queue<T>;\ntemplate <class T>\nusing min_heap = priority_queue<T, vector<T>, greater<T>>;\n#define rep(i, l, r) for (ll i = (l); i < (r); i++)\n#define rrep(i, r, l) for (ll i = (r); i-- > (l);)\n#define all(x) begin(x), end(x)\ntemplate <class T>\nbool chmin(T& lhs, T rhs) {\n return lhs > rhs ? (lhs = rhs, true) : false;\n}\ntemplate <class T>\nbool chmax(T& lhs, T rhs) {\n return lhs < rhs ? (lhs = rhs, true) : false;\n}\nstruct IOIO {\n IOIO() { cin.tie(0)->sync_with_stdio(0); }\n} ioio;\ntemplate <class S, class T>\nostream& operator<<(ostream& os, const pair<S, T>& p) {\n os << '(' << p.first << \", \" << p.second << ')';\n return os;\n}\ntemplate <class T>\nostream& operator<<(ostream& os, const vector<T>& vs) {\n os << '{';\n rep(i, 0, (int)vs.size()) os << vs[i] << (i + 1 == (int)vs.size() ? \"\" : \", \");\n os << '}';\n return os;\n}\ntemplate <class T>\nostream& operator<<(ostream& os, const set<T>& vs) {\n os << '{';\n for (auto it = vs.begin(); it != vs.end(); it++) {\n if (it != vs.begin()) { os << \", \"; }\n os << *it;\n }\n os << '}';\n return os;\n}\ntemplate <class T, class U>\nostream& operator<<(ostream& os, const map<T, U>& vs) {\n os << '{';\n for (auto it = vs.begin(); it != vs.end(); it++) {\n if (it != vs.begin()) { os << \", \"; }\n os << *it;\n }\n os << '}';\n return os;\n}\n#ifdef DEBUG\nvoid dump_func() { cerr << endl; }\ntemplate <class Head, class... Tail>\nvoid dump_func(Head&& head, Tail&&... tail) {\n cerr << head;\n if (sizeof...(Tail) > 0) { cerr << \", \"; }\n dump_func(std::move(tail)...);\n}\n#define dump(...) cerr << \"[\" + string(#__VA_ARGS__) + \"] \", dump_func(__VA_ARGS__)\n#else\n#define dump(...) static_cast<int>(0)\n#endif\nint main() {\n string s, t;\n cin >> s >> t;\n nono::Eertree Stree, Ttree;\n Stree.add(s);\n Ttree.add(t);\n auto Sfreq = Stree.freq();\n auto Tfreq = Ttree.freq();\n auto Snodes = Stree.nodes();\n auto Tnodes = Ttree.nodes();\n ll ans = 0;\n auto dfs = [&](auto&& self, int si, int ti) -> void {\n if (Snodes[si].len > 0) { ans += (ll)Sfreq[si] * Tfreq[ti]; }\n for (auto [c, to]: Snodes[si].to) {\n if (Tnodes[ti].to.contains(c)) { self(self, to, Tnodes[ti].to[c]); }\n }\n };\n dfs(dfs, 0, 0);\n dfs(dfs, 1, 1);\n cout << ans << '\\n';\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 28336, "score_of_the_acc": -0.6057, "final_rank": 9 }, { "submission_id": "aoj_2292_8990340", "code_snippet": "#line 2 \"cp-library/src/cp-template.hpp\"\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nusing i32 = int;\nusing u32 = unsigned int;\nusing i64 = long long;\nusing u64 = unsigned long long;\nusing i128 = __int128_t;\ntemplate < class T > bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }\ntemplate < class T > bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }\ntemplate < class T, class U > T ceil (T x, U y) { return (x > 0 ? (x + y - 1) / y : x / y); }\ntemplate < class T, class U > T floor(T x, U y) { return (x > 0 ? x / y : (x - y + 1) / y); }\nint popcnt(i32 x) { return __builtin_popcount(x); }\nint popcnt(u32 x) { return __builtin_popcount(x); }\nint popcnt(i64 x) { return __builtin_popcountll(x); }\nint popcnt(u64 x) { return __builtin_popcountll(x); }\n\n#line 2 \"cp-library/src/utility/rep_itr.hpp\"\ntemplate < class T > struct itr_rep {\n T i, d;\n constexpr itr_rep(const T i) noexcept : i(i), d(1) {}\n constexpr itr_rep(const T i, const T d) noexcept : i(i), d(d) {}\n void operator++() noexcept { i += d; }\n constexpr int operator*() const noexcept { return i; }\n constexpr bool operator!=(const itr_rep x) const noexcept { return d > 0 ? i < x.i : i > x.i; }\n};\n\ntemplate < class T > struct rep {\n const itr_rep< T > s, t;\n constexpr rep(const T t) noexcept : s(0), t(t) {}\n constexpr rep(const T s, const T t) noexcept : s(s), t(t) {}\n constexpr rep(const T s, const T t, const T d) noexcept : s(s, d), t(t, d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n\ntemplate < class T > struct revrep {\n const itr_rep < T > s, t;\n constexpr revrep(const T t) noexcept : s(t - 1, -1), t(-1, -1) {}\n constexpr revrep(const T s, const T t) noexcept : s(t - 1, -1), t(s - 1, -1) {}\n constexpr revrep(const T s, const T t, const T d) noexcept : s(t - 1, -d), t(s - 1, -d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n#line 3 \"cp-library/src/utility/io.hpp\"\n\n/* 128bit integer */\nistream& operator>>(istream& is, i128& x) {\n std::string s; is >> s;\n int pm = (s[0] == '-');\n x = 0;\n for(int i : rep(pm, int(s.size()))) x = x * 10 + (s[i] - '0');\n if(pm) x *= -1;\n return is;\n}\nostream& operator<<(ostream& os, const i128& x) {\n if(x == 0) return os << '0';\n i128 y = x;\n if(y < 0) { os << '-'; y *= -1; }\n std::vector<int> ny;\n while(y > 0) { ny.push_back(y % 10); y /= 10; }\n for(int i : revrep(ny.size())) os << ny[i];\n return os;\n}\n\ntemplate < class S, class T > istream& operator>>(istream& is, std::pair< S, T >& x) { is >> x.first >> x.second; return is; }\ntemplate < class S, class T > ostream& operator<<(ostream& os, const std::pair< S, T >& x) { os << x.first << \" \" << x.second; return os; }\n\nnamespace scanner {\n struct sca {\n template < class T > operator T() {\n T s; std::cin >> s; return s;\n }\n };\n struct vec {\n int n;\n vec(int n) : n(n) {}\n template < class T > operator std::vector< T >() {\n std::vector< T > v(n);\n for(T& x : v) std::cin >> x;\n return v;\n }\n };\n struct mat {\n int h, w;\n mat(int h, int w) : h(h), w(w) {}\n template < class T > operator std::vector< std::vector< T > >() {\n std::vector m(h, std::vector< T >(w));\n for(std::vector< T >& v : m) for(T& x : v) std::cin >> x;\n return m;\n }\n };\n struct speedup {\n speedup() {\n std::cin.tie(0);\n std::ios::sync_with_stdio(0);\n }\n } speedup_instance;\n}\nscanner::sca in() { return scanner::sca(); }\nscanner::vec in(int n) { return scanner::vec(n); }\nscanner::mat in(int h, int w) { return scanner::mat(h, w); }\n\nnamespace printer {\n void precision(int d) { std::cout << std::fixed << std::setprecision(d); }\n void flush() { std::cout.flush(); }\n}\n\ntemplate < class T >\nostream& operator<<(ostream& os, const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) { os << a[i]; if(i != n - 1) os << ' '; }\n return os;\n}\n\nint print() { std::cout << '\\n'; return 0; }\ntemplate < class head, class... tail > int print(head&& h, tail&&... t) {\n std::cout << h; if(sizeof...(tail)) std::cout << ' ';\n return print(std::forward<tail>(t)...);\n}\ntemplate < class T > int print_n(const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) std::cout << a[i] << \"\\n\";\n return 0;\n}\n\n\n#line 2 \"cp-library/src/utility/key_val.hpp\"\n\ntemplate < class K, class V >\nstruct key_val {\n K key; V val;\n key_val() {}\n key_val(K key, V val) : key(key), val(val) {}\n template < std::size_t Index >\n std::tuple_element_t< Index, key_val >& get() {\n if constexpr (Index == 0) return key;\n if constexpr (Index == 1) return val;\n }\n};\n\nnamespace std {\n\ntemplate < class K, class V > struct tuple_size < key_val< K, V > > : integral_constant< size_t, 2 > {};\ntemplate < class K, class V > struct tuple_element < 0, key_val< K, V > > { using type = K; };\ntemplate < class K, class V > struct tuple_element < 1, key_val< K, V > > { using type = V; };\n\n}\n#line 2 \"cp-library/src/utility/vec_op.hpp\"\ntemplate < class T > key_val< int, T > max_of(const vector< T >& a) {\n int i = std::max_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class T > key_val< int, T > min_of(const vector< T >& a) {\n int i = std::min_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class S, class T > S sum_of(const vector< T >& a) {\n S sum = 0;\n for(const T x : a) sum += x;\n return sum;\n}\ntemplate < class S, class T > vector< S > freq_of(const vector< T >& a, T L, T R) {\n vector< S > res(R - L, S(0));\n for(const T x : a) res[x - L] += 1;\n return res;\n}\ntemplate < class S, class T > struct prefix_sum {\n vector< S > s;\n prefix_sum(const vector< T >& a) : s(a) {\n s.insert(s.begin(), S(0));\n for(int i : rep(a.size())) s[i + 1] += s[i];\n }\n // [L, R)\n S sum(int L, int R) { return s[R] - s[L]; }\n};\n#line 3 \"cp-library/src/utility/heap.hpp\"\n\ntemplate < class T > using heap_min = std::priority_queue< T, std::vector< T >, std::greater< T > >;\ntemplate < class T > using heap_max = std::priority_queue< T, std::vector< T >, std::less< T > >;\n\n#line 27 \"cp-library/src/cp-template.hpp\"\n\n#line 1 \"cp-library/src/algorithm/bin_search.hpp\"\ntemplate < class T, class F >\nT bin_search(T ok, T ng, F f) {\n while(abs(ng - ok) > 1) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n\ntemplate < class T, class F >\nT bin_search_real(T ok, T ng, F f, int step = 80) {\n while(step--) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n#line 2 \"cp-library/src/algorithm/argsort.hpp\"\n\ntemplate < class T > std::vector< int > argsort(const std::vector< T > &a) {\n std::vector< int > ids((int)a.size());\n std::iota(ids.begin(), ids.end(), 0);\n std::sort(ids.begin(), ids.end(), [&](int i, int j) {\n return a[i] < a[j] || (a[i] == a[j] && i < j);\n });\n return ids;\n}\n#line 1 \"macro.hpp\"\nnamespace macro {\n\nusing size_type = int;\ntemplate < class container > void sort(container& a) { std::sort(std:: begin(a), std:: end(a)); }\ntemplate < class container > void rsort(container& a) { std::sort(std::rbegin(a), std::rend(a)); }\ntemplate < class container > void reverse(container& a) { std::reverse(std::begin(a), std::end(a)); }\ntemplate < class container > void unique(container& a) {\n std::sort(std::begin(a), std::end(a));\n a.erase(std::unique(std::begin(a), std::end(a)), std::end(a));\n}\ntemplate < class container > container sorted(const container& a) { container b = a; sort(b); return std::move(b); }\ntemplate < class container > container rsorted(const container& a) { container b = a; rsort(b); return std::move(b); }\ntemplate < class container, class compare > void sort(container& a, const compare& cmp) { std::sort(std::begin(a), std::end(a), cmp); }\ntemplate < class container, class compare > container sorted(const container& a, const compare& cmp) { container b = a; sort(b, cmp); return std::move(b); }\ntemplate < class container, class value > size_type lower_bound(const container& a, const value& x) { return std::lower_bound(std::begin(a), std::end(a), x) - std::begin(a); }\ntemplate < class container, class value > size_type upper_bound(const container& a, const value& x) { return std::upper_bound(std::begin(a), std::end(a), x) - std::begin(a); }\n\nconst std::vector<std::pair<size_type, size_type>> dir4 = { {+1, 0}, {-1, 0}, { 0, +1}, { 0, -1} };\nconst std::vector<std::pair<size_type, size_type>> dir8 = { {-1, -1}, {-1, 0}, {-1, +1}, { 0, -1}, { 0, +1}, {+1, -1}, {+1, 0}, {+1, +1} };\n\n#ifdef _DEBUG\n#define debug(x) std::cout << \"[\" << __LINE__ << \"] \" << #x << \": \" << x << std::endl\n#else\n#define debug(x)\n#endif\n\ntemplate < class container > void concat(container& a, const container& b) {\n a.insert(std::end(a), std::begin(b), std::end(b));\n}\nstd::vector<size_type> iota(const size_type n) {\n std::vector<size_type> I(n);\n std::iota(std::begin(I), std::end(I), 0);\n return I;\n}\ntemplate < class container > std::vector<size_type> sort_idx(const container& a) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return a[i] < a[j] or (a[i] == a[j] and i < j); });\n return I;\n}\ntemplate < class container, class compare > std::vector<size_type> sort_idx(const container& a, const compare& cmp) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return cmp(a[i], a[j]) or (a[i] == a[j] and i < j); });\n return std::move(I);\n}\n\nstruct grid {\n using size_type = int;\n size_type H, W;\n grid(const size_type H, const size_type W) : H(H), W(W) {}\n bool contains(const size_type i, const size_type j) {\n return 0 <= i and i < H and 0 <= j and j < W;\n }\n};\n\n} // namespace macro\n\nusing namespace macro;\n#line 3 \"A.cpp\"\n\ntemplate < class char_type > struct eertree {\n using size_type = int;\n struct node_type {\n map<char_type, size_type> nxt;\n size_type link, len, count;\n\n node_type(size_type link, size_type len, size_type count) : link(link), len(len), count(count) {}\n };\n\n size_type lsp;\n vector<char_type> s;\n vector<node_type> g;\n\n eertree() : lsp(0) {\n g.emplace_back(0, -1, 0); // odd\n g.emplace_back(0, 0, 0); // even\n }\n\n // return node(c + str(?) + c)\n size_type find(size_type v) const {\n const size_type n = s.size() - 1;\n while(true) {\n const size_type i = n - g[v].len - 1;\n if(0 <= i and s[i] == s[n]) return v;\n v = g[v].link;\n }\n }\n\n void push_back(const char_type c) {\n s.push_back(c);\n const size_type v = find(lsp);\n\n if(g[v].nxt.count(c)) {\n lsp = g[v].nxt[c];\n g[lsp].count++;\n } else {\n lsp = g[v].nxt[c] = g.size();\n g.emplace_back(v == 0 ? 1 : g[find(g[v].link)].nxt[c], 1 + g[v].len + 1, 1);\n }\n }\n\n vector<size_type> freq() const {\n vector<size_type> freq(g.size(), 0);\n for(size_type i = size_type(g.size()) - 1; i > 0; i--) {\n freq[i] += g[i].count;\n freq[g[i].link] += freq[i];\n }\n return freq;\n }\n\n string str(size_type v) const {\n if(v == 0) return \"odd(-1)\";\n if(v == 1) return \"even(0)\";\n string res = \"\";\n function<bool(size_type)> dfs = [&](size_type x) -> bool {\n if(x == v) return true;\n for(auto [c, nx] : g[x].nxt) {\n if(dfs(nx)) {\n res.push_back(c);\n return true;\n }\n }\n return false;\n }; dfs(0); dfs(1);\n string rr = res; reverse(rr.begin(), rr.end());\n if(g[v].len % 2 == 1) rr.pop_back();\n return res + rr;\n }\n};\n\nint main() {\n string S = in(), T = in();\n\n auto f = [&](const string& x) {\n eertree<char> tree;\n for(char c : x) tree.push_back(c);\n vector<int> freq = tree.freq();\n const int n = freq.size();\n i64 ans = 0;\n for(int i : rep(2, n))\n ans += i64(freq[i]) * (freq[i] - 1) / 2;\n return ans;\n };\n\n print(f(S + \"!?\" + T) - f(S) - f(T));\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 16656, "score_of_the_acc": -0.2427, "final_rank": 2 }, { "submission_id": "aoj_2292_8762299", "code_snippet": "#ifndef HIDDEN_IN_VS // 折りたたみ用\n\n// 警告の抑制\n#define _CRT_SECURE_NO_WARNINGS\n\n// ライブラリの読み込み\n#include <bits/stdc++.h>\nusing namespace std;\n\n// 型名の短縮\nusing ll = long long; using ull = unsigned long long; // -2^63 ~ 2^63 = 9 * 10^18(int は -2^31 ~ 2^31 = 2 * 10^9)\nusing pii = pair<int, int>;\tusing pll = pair<ll, ll>;\tusing pil = pair<int, ll>;\tusing pli = pair<ll, int>;\nusing vi = vector<int>;\t\tusing vvi = vector<vi>;\t\tusing vvvi = vector<vvi>;\tusing vvvvi = vector<vvvi>;\nusing vl = vector<ll>;\t\tusing vvl = vector<vl>;\t\tusing vvvl = vector<vvl>;\tusing vvvvl = vector<vvvl>;\nusing vb = vector<bool>;\tusing vvb = vector<vb>;\t\tusing vvvb = vector<vvb>;\nusing vc = vector<char>;\tusing vvc = vector<vc>;\t\tusing vvvc = vector<vvc>;\nusing vd = vector<double>;\tusing vvd = vector<vd>;\t\tusing vvvd = vector<vvd>;\ntemplate <class T> using priority_queue_rev = priority_queue<T, vector<T>, greater<T>>;\nusing Graph = vvi;\n\n// 定数の定義\nconst double PI = acos(-1);\nconst vi DX = { 1, 0, -1, 0 }; // 4 近傍(下,右,上,左)\nconst vi DY = { 0, 1, 0, -1 };\nint INF = 1001001001; ll INFL = 4004004003104004004LL; // (int)INFL = 1010931620;\n\n// 入出力高速化\nstruct fast_io { fast_io() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(18); } } fastIOtmp;\n\n// 汎用マクロの定義\n#define all(a) (a).begin(), (a).end()\n#define sz(x) ((int)(x).size())\n#define lbpos(a, x) (int)distance((a).begin(), std::lower_bound(all(a), x))\n#define ubpos(a, x) (int)distance((a).begin(), std::upper_bound(all(a), x))\n#define Yes(b) {cout << ((b) ? \"Yes\\n\" : \"No\\n\");}\n#define YES(b) {cout << ((b) ? \"YES\\n\" : \"NO\\n\");}\n#define rep(i, n) for(int i = 0, i##_len = int(n); i < i##_len; ++i) // 0 から n-1 まで昇順\n#define repi(i, s, t) for(int i = int(s), i##_end = int(t); i <= i##_end; ++i) // s から t まで昇順\n#define repir(i, s, t) for(int i = int(s), i##_end = int(t); i >= i##_end; --i) // s から t まで降順\n#define repe(v, a) for(const auto& v : (a)) // a の全要素(変更不可能)\n#define repea(v, a) for(auto& v : (a)) // a の全要素(変更可能)\n#define repb(set, d) for(int set = 0, set##_ub = 1 << int(d); set < set##_ub; ++set) // d ビット全探索(昇順)\n#define repis(i, set) for(int i = lsb(set), bset##i = set; i >= 0; bset##i -= 1 << i, i = lsb(bset##i)) // set の全要素(昇順)\n#define repp(a) sort(all(a)); for(bool a##_perm = true; a##_perm; a##_perm = next_permutation(all(a))) // a の順列全て(昇順)\n#define smod(n, m) ((((n) % (m)) + (m)) % (m)) // 非負mod\n#define uniq(a) {sort(all(a)); (a).erase(unique(all(a)), (a).end());} // 重複除去\n#define EXIT(a) {cout << (a) << endl; exit(0);} // 強制終了\n#define inQ(x, y, u, l, d, r) ((u) <= (x) && (l) <= (y) && (x) < (d) && (y) < (r)) // 矩形内判定\n\n// 汎用関数の定義\ntemplate <class T> inline ll pow(T n, int k) { ll v = 1; rep(i, k) v *= n; return v; }\ntemplate <class T> inline bool chmax(T& M, const T& x) { if (M < x) { M = x; return true; } return false; } // 最大値を更新(更新されたら true を返す)\ntemplate <class T> inline bool chmin(T& m, const T& x) { if (m > x) { m = x; return true; } return false; } // 最小値を更新(更新されたら true を返す)\ntemplate <class T> inline T get(T set, int i) { return (set >> i) & T(1); }\n\n// 演算子オーバーロード\ntemplate <class T, class U> inline istream& operator>>(istream& is, pair<T, U>& p) { is >> p.first >> p.second; return is; }\ntemplate <class T> inline istream& operator>>(istream& is, vector<T>& v) { repea(x, v) is >> x; return is; }\ntemplate <class T> inline vector<T>& operator--(vector<T>& v) { repea(x, v) --x; return v; }\ntemplate <class T> inline vector<T>& operator++(vector<T>& v) { repea(x, v) ++x; return v; }\n\n#endif // 折りたたみ用\n\n\n#if __has_include(<atcoder/all>)\n#include <atcoder/all>\nusing namespace atcoder;\n\n#ifdef _MSC_VER\n#include \"localACL.hpp\"\n#endif\n\n//using mint = modint1000000007;\nusing mint = modint998244353;\n//using mint = modint; // mint::set_mod(m);\n\nnamespace atcoder {\n\tinline istream& operator>>(istream& is, mint& x) { ll x_; is >> x_; x = x_; return is; }\n\tinline ostream& operator<<(ostream& os, const mint& x) { os << x.val(); return os; }\n}\nusing vm = vector<mint>; using vvm = vector<vm>; using vvvm = vector<vvm>; using vvvvm = vector<vvvm>;\n#endif\n\n\n#ifdef _MSC_VER // 手元環境(Visual Studio)\n#include \"local.hpp\"\n#else // 提出用(gcc)\ninline int popcount(int n) { return __builtin_popcount(n); }\ninline int popcount(ll n) { return __builtin_popcountll(n); }\ninline int lsb(int n) { return n != 0 ? __builtin_ctz(n) : -1; }\ninline int lsb(ll n) { return n != 0 ? __builtin_ctzll(n) : -1; }\ninline int msb(int n) { return n != 0 ? (31 - __builtin_clz(n)) : -1; }\ninline int msb(ll n) { return n != 0 ? (63 - __builtin_clzll(n)) : -1; }\n#define dump(...)\n#define dumpel(v)\n#define dump_list(v)\n#define dump_mat(v)\n#define input_from_file(f)\n#define output_to_file(f)\n#define Assert(b) { if (!(b)) while (1) cout << \"OLE\"; }\n#endif\n\n\n//【ローリングハッシュ(列)】\n/*\n* Rolling_hash<STR>(STR s, bool reversible = false) : O(n)\n*\t列 s[0..n) で初期化する.reversible = true にすると逆順のハッシュも計算可能になる.\n*\t制約:STR は string,vector<T> など.ll 範囲の負数は扱えない.\n*\n* ull get(int l, int r) : O(1)\n*\t部分文字列 s[l..r) のハッシュ値を返す(空なら 0)\n*\n* ull get_rev(int l, int r) : O(1)\n*\t部分文字列 s[l..r) を反転した文字列のハッシュ値を返す(空なら 0)\n*\n* ull join(ull hs, ull ht, int len) : O(1)\n*\tハッシュ値 hs をもつ s とハッシュ値 ht をもつ t[0..len) を連結した s+t のハッシュ値を返す.\n*/\ntemplate <class STR>\nclass Rolling_hash {\n\t// 参考 : https://qiita.com/keymoon/items/11fac5627672a6d6a9f6\n\n\t//【方法】\n\t// 2^61 - 1 は十分大きい素数であるからローリングハッシュの法として適切である.\n\t// a, b < 2^61 - 1 とし,積 a b mod (2^61 - 1) を高速に計算できればよい.\n\t// \n\t// まず a, b を上位と下位に分解し\n\t//\t\ta = 2^31 ah + al, b = 2^31 bh + bl (ah, bh < 2^30, al, bl < 2^31)\n\t// とする.これらの積をとると,\n\t//\t\ta b\n\t//\t\t= (2^31 ah + al)(2^31 bh + bl)\n\t//\t\t= 2^62 ah bh + 2^31 (ah bl + bh al) + al bl\n\t// となる.2^61 ≡ 1 (mod 2^61 - 1) に注意してそれぞれの項を mod 2^61 - 1 で整理する.\n\t//\n\t// 第 1 項については,\n\t//\t\t2^62 ah bh\n\t//\t\t= 2 ah bh\n\t//\t\t≦ 2 (2^30-1) (2^30-1)\n\t// となる.\n\t//\n\t// 第 2 項については,c := ah bl + bh al < 2^62 を上位と下位に分解し\n\t//\t\tc = 2^30 ch + cl (ch < 2^32, cl < 2^30)\n\t// とすると,\n\t//\t\t2^31 c\n\t//\t\t= 2^31 (2^30 ch + cl)\n\t//\t\t= ch + 2^31 cl\n\t//\t\t≦ (2^32-1) + 2^31 (2^30-1)\n\t// となる.\n\t//\n\t// 第 3 項については,\n\t//\t\tal bl\n\t//\t\t≦ (2^31-1) (2^31-1)\n\t// となる.\n\t// \n\t// これらの和は\n\t//\t\t2 ah bh + ch + 2^31 cl + al bl\n\t//\t\t≦ 2 (2^30-1) (2^30-1) + (2^32-1) + 2^31 (2^30-1) + (2^31-1) (2^31-1)\n\t//\t\t= 9223372030412324866 < 9223372036854775808 = 2^63 << 2^64\n\t// となるのでオーバーフローの心配はない.\n\n\tstatic constexpr ull MASK30 = (1ULL << 30) - 1;\n\tstatic constexpr ull MASK31 = (1ULL << 31) - 1;\n\tstatic constexpr ull MOD = (1ULL << 61) - 1; // 法(素数)\n\n\t// a mod (2^61 - 1) を返す.\n\tinline ull get_mod(ull a) const {\n\t\tull ah = a >> 61, al = a & MOD;\n\t\tull res = ah + al;\n\t\tif (res >= MOD) res -= MOD;\n\t\treturn res;\n\t}\n\n\t// x ≡ a b mod (2^61 - 1) なる x < 2^63 を返す(ただし a, b < 2^61)\n\tinline ull mul(ull a, ull b) const {\n\t\tull ah = a >> 31, al = a & MASK31;\n\t\tull bh = b >> 31, bl = b & MASK31;\n\n\t\tull c = ah * bl + bh * al;\n\t\tull ch = c >> 30, cl = c & MASK30;\n\n\t\tull term1 = 2 * ah * bh;\n\t\tull term2 = ch + (cl << 31);\n\t\tull term3 = al * bl;\n\n\t\treturn term1 + term2 + term3; // < 2^63\n\t}\n\n\tstatic constexpr ull BASE = 1234567891011; // 適当な基数\n\tstatic constexpr ull SHIFT = 4295090752; // 適当なシフト\n\n\t// 列の長さ\n\tint n;\n\n\t// powB[i] : BASE^i\n\tvector<ull> powB;\n\n\t// v[i] : s[0..i) のハッシュ値 Σj∈[0..i) (s[j]+SHIFT) BASE^(i-1-j)\n\t// v_rev[i] : s[n-i..n) を反転した文字列のハッシュ値\n\tvector<ull> v, v_rev;\n\npublic:\n\t// 列 s[0..n) で初期化する.\n\tRolling_hash(const STR& s, bool reversible = false) : n(sz(s)), powB(n + 1), v(n + 1) {\n\t\t// verify : https://atcoder.jp/contests/tessoku-book/tasks/tessoku_book_ec\n\n\t\tpowB[0] = 1;\n\t\trep(i, n) powB[i + 1] = get_mod(mul(powB[i], BASE));\n\n\t\trep(i, n) v[i + 1] = get_mod(mul(v[i], BASE) + (ull)s[i] + SHIFT);\n\n\t\tif (reversible) {\n\t\t\tv_rev.resize(n + 1);\n\t\t\trep(i, n) v_rev[i + 1] = get_mod(mul(v_rev[i], BASE) + (ull)s[n - 1 - i] + SHIFT);\n\t\t}\n\t}\n\tRolling_hash() : n(0) {}\n\n\t// s[l..r) のハッシュ値の取得\n\tull get(int l, int r) const {\n\t\t// verify : https://atcoder.jp/contests/tessoku-book/tasks/tessoku_book_ec\n\n\t\tchmax(l, 0); chmin(r, n);\n\t\tif (l >= r) return 0;\n\n\t\treturn get_mod(v[r] + 4 * MOD - mul(v[l], powB[r - l]));\n\t}\n\n\t// s[l..r) を反転した文字列のハッシュ値の取得\n\tull get_rev(int l, int r) {\n\t\t// verify : https://atcoder.jp/contests/tessoku-book/tasks/tessoku_book_ec\n\n\t\tchmax(l, 0); chmin(r, n);\n\t\tif (l >= r) return 0;\n\t\tAssert(!v_rev.empty());\n\n\t\t// s[l, r) を反転した文字列は s_rev[n-r, n-l) に等しい.\n\t\treturn get_mod(v_rev[n - l] + 4 * MOD - mul(v_rev[n - r], powB[r - l]));\n\t}\n\n\t// ハッシュ値 hs をもつ s とハッシュ値 ht をもつ t[0..len) を連結した s+t のハッシュ値を返す.\n\tull join(ull hs, ull ht, int len) const {\n\t\t// verify : https://atcoder.jp/contests/abc284/tasks/abc284_f\n\n\t\tAssert(len <= n);\n\t\treturn get_mod(ht + mul(hs, powB[len]));\n\t}\n};\n\n\n//【最長回文長(文字中心)】O(n)\n/*\n* s[0..n) の s[i] を中心とする最長回文の半径((文字数 + 1) / 2)を r[i] に格納し r を返す.\n* ここで回文の半径とは,(文字数 + 1) / 2 を意味する.\n*/\ntemplate <class STR>\nvi manacher(const STR& s) {\n\t// 参考 : https://snuke.hatenablog.com/entry/2014/12/02/235837\n\t// verify : https://judge.yosupo.jp/problem/enumerate_palindromes\n\n\t//【方法】\n\t// s[i] を中心とする最長回文の半径 j = r[i] が愚直に求まったとする.\n\t// すなわち s(i-j..i+j) が s[i] を中心とする最長回文である.\n\t// \n\t// 各 k = [1..j) について,s[i-k] を中心とする最長回文 s(i-k-r[i-k]..i-k+r[i-k]) が\n\t// s(i-j+1..i+j-1) の部分文字列であれば,s[i±j] の影響を受けず\n\t// s[i] についての左右対称性より r[i+k] = r[i-k] と定まる.\n\t// その条件は,左端を比較して\n\t//\t\ti - k - r[i-k] >= i - j + 1\n\t//\t\t⇔ k + r[i-k] < j\n\t// である.このような結果の使い回しができる限り k を進め,次の i を i + k にする.\n\t// \n\t// 使い回しができなくなったということは,s[i+k] を中心とする最長回文\n\t// s(i+k-r[i+k]..i+k+r[i+k]) が s(i-j+1..i+j-1) の部分文字列でないので,\n\t// 右端を比較することで\n\t//\t\ti + k + r[i+k] > i + j - 1\n\t//\t\t⇔ r[i+k] >= j - k\n\t// である.よって次の j は j - k にすればよい.\n\n\tint n = sz(s);\n\tvi r(n);\n\n\tint i = 0, j = 0;\n\twhile (i < n) {\n\t\twhile (i - j >= 0 && i + j < n && s[i - j] == s[i + j]) j++;\n\t\tr[i] = j;\n\n\t\tint k = 1;\n\t\twhile (i - k >= 0 && k + r[i - k] < j) {\n\t\t\tr[i + k] = r[i - k];\n\t\t\tk++;\n\t\t}\n\t\ti += k;\n\t\tj -= k;\n\t}\n\n\treturn r;\n}\n\n\n//【最長回文長】O(n)\n/*\n* s[0..n) の s[i] を中心とする最長回文の長さを lo[i] に格納し,\n* s[i..i+1] を中心とする最長回文の長さを le[i] に格納する.\n*\n* 利用:【最長回文長(文字中心)】\n*/\ntemplate <class STR>\nvoid manacher(const STR& s, vi& lo, vi& le) {\n\t// 参考 : https://snuke.hatenablog.com/entry/2014/12/02/235837\n\t// verify : https://judge.yosupo.jp/problem/enumerate_palindromes\n\n\tint n = sz(s);\n\tlo.resize(n);\n\tle.resize(n - 1);\n\n\tSTR s_riffled;\n\ts_riffled.resize(2 * n + 1);\n\trep(i, n) s_riffled[2 * i + 1] = s[i];\n\trep(i, n + 1) s_riffled[2 * i] = '$'; // '$' は s に含まれない文字\n\n\tvi r = manacher(s_riffled);\n\n\trep(i, n) lo[i] = r[2 * i + 1] - 1;\n\trep(i, n - 1) le[i] = r[2 * (i + 1)] - 1;\n}\n\n\nvector<unordered_map<ull, ll>> count_palindromes(const string& s) {\n\tint n = sz(s);\n\n\tRolling_hash S(s, true);\n\n\tvi lo, le;\n\tmanacher(s, lo, le);\n\n\tunordered_set<ull> hs;\n\ths.insert(0);\n\n\tunordered_map<ull, pii> h_to_lr;\n\tvector<unordered_map<ull, ll>> cnt(n + 1);\n\n\trep(i, n) {\n\t\tint r = lo[i] / 2;\n\t\tauto h = S.get(i - r, i + r + 1);\n\t\tdump(s.substr(i - r, 2 * r + 1));\n\t\th_to_lr[h] = { i - r, i + r + 1 };\n\t\tcnt[2 * r + 1][h]++;\n\t}\n\n\trep(i, n - 1) {\n\t\tint r = le[i] / 2;\n\t\tauto h = S.get(i - r + 1, i + r + 1);\n\t\tdump(s.substr(i - r + 1, 2 * r));\n\t\th_to_lr[h] = { i - r + 1, i + r + 1 };\n\t\tcnt[2 * r][h]++;\n\t}\n\n\trepir(len, n, 1) {\n\t\tfor (auto [h, c] : cnt[len]) {\n\t\t\tauto [l, r] = h_to_lr[h];\n\n\t\t\tif (len >= 2) {\n\t\t\t\tauto nh = S.get(l + 1, r - 1);\n\t\t\t\th_to_lr[nh] = { l + 1, r - 1 };\n\t\t\t\tcnt[len - 2][nh] += c;\n\t\t\t}\n\t\t}\n\t}\n\n\treturn cnt;\n}\n\n\nint main() {\n//\tinput_from_file(\"input.txt\");\n//\toutput_to_file(\"output.txt\");\n\n\tstring s, t;\n\tcin >> s >> t;\n\n\tint n = sz(s), m = sz(t);\n\n\tauto cs = count_palindromes(s);\n\tauto ct = count_palindromes(t);\n\tdumpel(cs); dumpel(ct);\n\n\tll res = 0;\n\n\trepi(len, 1, min(n, m)) {\n\t\tfor (auto [h, c] : cs[len]) {\n\t\t\tres += c * ct[len][h];\n\t\t}\n\t}\n\n\tcout << res << endl;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 18292, "score_of_the_acc": -0.3625, "final_rank": 5 }, { "submission_id": "aoj_2292_8760492", "code_snippet": "// #line 1 \"library/Template/template.hpp\"\n#include <bits/stdc++.h>\nusing namespace std;\n\n#define rep(i,a,b) for(int i=(int)(a);i<(int)(b);i++)\n#define ALL(v) (v).begin(),(v).end()\n#define UNIQUE(v) sort(ALL(v)),(v).erase(unique(ALL(v)),(v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v,x) int(lower_bound(ALL(v),(x))-(v).begin())\n#define UB(v,x) int(upper_bound(ALL(v),(x))-(v).begin())\n\nusing ll=long long int;\nusing ull=unsigned long long;\nusing i128=__int128_t;\nusing u128=__uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate<typename T>inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;}\ntemplate<typename T>inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;}\ntemplate<typename T,typename U>T ceil(T x,U y){assert(y!=0); if(y<0)x=-x,y=-y; return (x>0?(x+y-1)/y:x/y);}\ntemplate<typename T,typename U>T floor(T x,U y){assert(y!=0); if(y<0)x=-x,y=-y; return (x>0?x/y:(x-y+1)/y);}\ntemplate<typename T>int popcnt(T x){return __builtin_popcountll(x);}\ntemplate<typename T>int topbit(T x){return (x==0?-1:63-__builtin_clzll(x));}\ntemplate<typename T>int lowbit(T x){return (x==0?-1:__builtin_ctzll(x));}\n// #line 2 \"library/Utility/fastio.hpp\"\n#include <unistd.h>\n\nclass FastIO{\n static constexpr int L=1<<16;\n char rdbuf[L];\n int rdLeft=0,rdRight=0;\n inline void reload(){\n int len=rdRight-rdLeft;\n memmove(rdbuf,rdbuf+rdLeft,len);\n rdLeft=0,rdRight=len;\n rdRight+=fread(rdbuf+len,1,L-len,stdin);\n }\n inline bool skip(){\n for(;;){\n while(rdLeft!=rdRight and rdbuf[rdLeft]<=' ')rdLeft++;\n if(rdLeft==rdRight){\n reload();\n if(rdLeft==rdRight)return false;\n }\n else break;\n }\n return true;\n }\n template<typename T,enable_if_t<is_integral<T>::value,int> =0>inline bool _read(T& x){\n if(!skip())return false;\n if(rdLeft+20>=rdRight)reload();\n bool neg=false;\n if(rdbuf[rdLeft]=='-'){\n neg=true;\n rdLeft++;\n }\n x=0;\n while(rdbuf[rdLeft]>='0' and rdLeft<rdRight){\n x=x*10+(neg?-(rdbuf[rdLeft++]^48):(rdbuf[rdLeft++]^48));\n }\n return true;\n }\n template<typename T,enable_if_t<is_floating_point<T>::value,int> =0>inline bool _read(T& x){\n if(!skip())return false;\n if(rdLeft+20>=rdRight)reload();\n bool neg=false;\n if(rdbuf[rdLeft]=='-'){\n neg=true;\n rdLeft++;\n }\n x=0;\n while(rdbuf[rdLeft]>='0' and rdbuf[rdLeft]<='9' and rdLeft<rdRight){\n x=x*10+(rdbuf[rdLeft++]^48);\n }\n if(rdbuf[rdLeft]!='.')return true;\n rdLeft++;\n T base=.1;\n while(rdbuf[rdLeft]>='0' and rdbuf[rdLeft]<='9' and rdLeft<rdRight){\n x+=base*(rdbuf[rdLeft++]^48);\n base*=.1;\n }\n if(neg)x=-x;\n return true;\n }\n inline bool _read(char& x){\n if(!skip())return false;\n if(rdLeft+1>=rdRight)reload();\n x=rdbuf[rdLeft++];\n return true;\n }\n inline bool _read(string& x){\n if(!skip())return false;\n for(;;){\n int pos=rdLeft;\n while(pos<rdRight and rdbuf[pos]>' ')pos++;\n x.append(rdbuf+rdLeft,pos-rdLeft);\n if(rdLeft==pos)break;\n rdLeft=pos;\n if(rdLeft==rdRight)reload();\n else break;\n }\n return true;\n }\n template<typename T>inline bool _read(vector<T>& v){\n for(auto& x:v){\n if(!_read(x))return false;\n }\n return true;\n }\n\n char wtbuf[L],tmp[50];\n int wtRight=0;\n inline void flush(){\n fwrite(wtbuf,1,wtRight,stdout);\n wtRight=0;\n }\n inline void _write(const char& x){\n if(wtRight>L-32)flush();\n wtbuf[wtRight++]=x;\n }\n inline void _write(const string& x){\n for(auto& c:x)_write(c);\n }\n template<typename T,enable_if_t<is_integral<T>::value,int> =0>inline void _write(T x){\n if(wtRight>L-32)flush();\n if(x==0){\n _write('0');\n return;\n }\n else if(x<0){\n _write('-');\n if (__builtin_expect(x == std::numeric_limits<T>::min(), 0)) {\n switch (sizeof(x)) {\n case 2: _write(\"32768\"); return;\n case 4: _write(\"2147483648\"); return;\n case 8: _write(\"9223372036854775808\"); return;\n }\n }\n x=-x;\n }\n int pos=0;\n while(x!=0){\n tmp[pos++]=char((x%10)|48);\n x/=10;\n }\n rep(i,0,pos)wtbuf[wtRight+i]=tmp[pos-1-i];\n wtRight+=pos;\n }\n template<typename T>inline void _write(const vector<T>& v){\n rep(i,0,v.size()){\n if(i)_write(' ');\n _write(v[i]);\n }\n }\npublic:\n FastIO(){}\n ~FastIO(){flush();}\n inline void read(){}\n template <typename Head, typename... Tail>inline void read(Head& head,Tail&... tail){\n assert(_read(head));\n read(tail...);\n }\n template<bool ln=true,bool space=false>inline void write(){if(ln)_write('\\n');}\n template <bool ln=true,bool space=false,typename Head, typename... Tail>inline void write(const Head& head,const Tail&... tail){\n if(space)_write(' ');\n _write(head);\n write<ln,true>(tail...);\n }\n};\n\n/**\n * @brief Fast IO\n */\n// #line 3 \"sol.cpp\"\n\n\nnamespace Random{\n mt19937_64 randgen(chrono::steady_clock::now().time_since_epoch().count());\n using u64=unsigned long long;\n static u64 get(){\n return randgen();\n }\n template<typename T>static T get(T L){\n return get()%(L+1);\n }\n template<typename T>static T get(T L,T R){\n return get(R-L)+L;\n }\n double uniform(){\n return double(get(1000000000))/1000000000;\n }\n string str(int n){\n string ret;\n rep(i,0,n)ret+=get('a','z');\n return ret;\n }\n template<typename Iter>void shuffle(Iter first,Iter last){\n if(first==last)return;\n int len=1;\n for(auto it=first+1;it!=last;it++){\n len++;\n int j=get(0,len-1);\n if(j!=len-1)iter_swap(it,first+j);\n }\n }\n template<typename T>vector<T> select(int n,T L,T R){\n set<T> ret;\n while(ret.size()<n)ret.insert(get(L,R));\n return {ALL(ret)};\n }\n};\n\nstruct RollingHash {\n using ull = unsigned long long;\n const ull MOD = 0x1fffffffffffffff;\n const ull base;\n vector<ull> hashed, power;\n\n static constexpr ull mask(ll a){ return (1ULL << a) - 1; }\n\n inline ull mul(ull a, ull b) const {\n __uint128_t ans = __uint128_t(a) * b;\n ans = (ans >> 61) + (ans & MOD);\n if(ans >= MOD) ans -= MOD;\n return ans;\n }\n\n static inline ull genbase(){return Random::get(ull(0x1fffffffffffffff));}\n RollingHash()=default;\n\n RollingHash(const string &s,ull base):base(base){\n ll n = s.size();\n hashed.assign(n + 1, 0);\n power.assign(n + 1, 0);\n power[0] = 1;\n for(ll i = 0; i < n; i++) {\n power[i + 1] = mul(power[i], base);\n hashed[i + 1] = mul(hashed[i], base) + s[i];\n if(hashed[i + 1] >= MOD) hashed[i + 1] -= MOD;\n }\n }\n\n ull get(ll l, ll r) const {\n ull ret = hashed[r] + MOD - mul(hashed[l], power[r - l]);\n if(ret >= MOD) ret -= MOD;\n return ret;\n }\n\n ull connect(ull h1, ull h2, ll h2len) const {\n ull ret = mul(h1, power[h2len]) + h2;\n if(ret >= MOD) ret -= MOD;\n return ret;\n }\n\n void connect(const string &s){\n ll n = hashed.size() - 1, m = s.size();\n hashed.resize(n + m + 1);\n power.resize(n + m + 1);\n for(ll i = n; i < n + m; i++) {\n power[i + 1] = mul(power[i], base);\n hashed[i + 1] = mul(hashed[i], base) + s[i - n];\n if(hashed[i + 1] >= MOD) hashed[i + 1] -= MOD;\n }\n }\n\n ll LCP(const RollingHash &b, ll l1, ll r1, ll l2, ll r2) {\n ll len = min(r1 - l1, r2 - l2);\n ll low = -1, high = len + 1;\n while(high - low > 1) {\n ll mid = (low + high) / 2;\n if(get(l1, l1 + mid) == b.get(l2, l2 + mid)) low = mid;\n else high = mid;\n }\n return low;\n }\n};\n\ntemplate<typename T>struct PalindromicTree{\n struct Node{\n map<char,int> nxt;\n int link,len,id,cnt;\n Node(){}\n Node(int link,int len,int id,int cnt):link(link),len(len),id(id),cnt(cnt){}\n };\n T S;\n vector<Node> nodes;\n PalindromicTree(){}\n PalindromicTree(const T& S):S(S){\n nodes.push_back(Node(0,-1,-1,0));\n nodes.push_back(Node(0,0,-1,0));\n int cur=0;\n\n rep(i,0,SZ(S)){\n int k=find(i,cur);\n char c=S[i];\n if(nodes[k].nxt.count(c)){\n cur=nodes[k].nxt[c];\n nodes[cur].cnt++;\n continue;\n }\n nodes[k].nxt[c]=SZ(nodes);\n cur=SZ(nodes);\n nodes.push_back(Node(-1,nodes[k].len+2,i-nodes[k].len-1,1));\n if(nodes.back().len==1)nodes.back().link=1;\n else{\n int n=find(i,nodes[k].link);\n nodes.back().link=nodes[n].nxt[c];\n }\n }\n }\n vector<array<int,3>> get_freq(){\n vector<array<int,3>> ret;\n for(int i=SZ(nodes)-1;i>=2;i--){\n ret.push_back({nodes[i].len,nodes[i].id,nodes[i].cnt});\n nodes[nodes[i].link].cnt+=nodes[i].cnt;\n }\n return ret;\n }\nprivate:\n int find(int last,int k)const{\n for(;;){\n int i=last-1-nodes[k].len;\n if(i>=0 and S[i]==S[last])break;\n k=nodes[k].link;\n }\n return k;\n }\n};\n\nFastIO io;\nint main(){\n string S,T;\n io.read(S,T);\n\n using ull = unsigned long long;\n PalindromicTree<string> t1(S),t2(T);\n auto base=RollingHash::genbase();\n RollingHash rh1(S,base),rh2(T,base);\n auto f1=t1.get_freq();\n auto f2=t2.get_freq();\n\n map<ull,ll> mp;\n for(auto& [len,id,cnt]:f1){\n // cerr<<len<<' '<<id<<' '<<cnt<<'\\n';\n mp[rh1.get(id,id+len)]+=cnt;\n }\n ll ret=0;\n for(auto& [len,id,cnt]:f2)ret+=mp[rh2.get(id,id+len)]*cnt;\n io.write(ret);\n return 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 24968, "score_of_the_acc": -0.57, "final_rank": 7 }, { "submission_id": "aoj_2292_8479596", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define rep(i, n) for (int i = 0; i < (n); i++)\n#define per(i, n) for (int i = (n)-1; i >= 0; i--)\n#define rep2(i, l, r) for (int i = (l); i < (r); i++)\n#define per2(i, l, r) for (int i = (r)-1; i >= (l); i--)\n#define each(e, x) for (auto &e : x)\n#define sz(x) (int)x.size()\n#define all(x) begin(x), end(x)\n#define rall(x) rbegin(x), rend(x)\n#define eb emplace_back\n#define MM << ' ' <<\n#define TT template <typename T>\nusing ll = long long;\nusing pii = pair<int, int>;\nusing pll = pair<ll, ll>;\n\nTT bool chmin(T &x, T y) { return x > y ? (x = y, true) : false; }\nTT bool chmax(T &x, T y) { return x < y ? (x = y, true) : false; }\n\nTT using minheap = priority_queue<T, vector<T>, greater<T>>;\nTT using maxheap = priority_queue<T>;\n\nTT void print(vector<T> v, T x = 0) {\n int n = sz(v);\n rep(i, n) cout << v[i] + x << (i == n - 1 ? '\\n' : ' ');\n if (n == 0) cout << '\\n';\n}\n\nTT void printn(vector<T> v, T x = 0) {\n int n = sz(v);\n rep(i, n) cout << v[i] + x << '\\n';\n}\n\nTT void rearrange(vector<T> &v) {\n sort(all(v));\n v.erase(unique(all(v)), end(v));\n}\n\nTT int lb(vector<T> &v, T x) { return lower_bound(all(v), x) - begin(v); }\nTT int ub(vector<T> &v, T x) { return upper_bound(all(v), x) - begin(v); }\n\nconst int inf = (1 << 30) - 1;\nconst ll INF = (1LL << 60) - 1;\n\nstruct io_setup {\n io_setup() {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n cout << fixed << setprecision(15);\n }\n} io_setup;\n\nusing ull = unsigned long long;\nconst ull mod = (1LL << 61) - 1;\n\nnamespace Hash {\null getmod(ull x) {\n ull ret = (x >> 61) + (x & mod);\n if (ret >= mod) ret -= mod;\n return ret;\n}\n\null mul(ull x, ull y) {\n x = getmod(x), y = getmod(y);\n ull x1 = x >> 31;\n ull x2 = x & ((1LL << 31) - 1);\n ull y1 = y >> 31;\n ull y2 = y & ((1LL << 31) - 1);\n ull z = x1 * y2 + x2 * y1;\n ull z1 = z >> 30;\n ull z2 = z & ((1LL << 30) - 1);\n return getmod(x1 * y1 * 2 + x2 * y2 + z1 + (z2 << 31));\n}\n} // namespace Hash\n\ntemplate <typename T>\nstruct RollingHash {\n const int n;\n const ull b;\n vector<ull> h, p;\n\n RollingHash(const T &s, ull b = 887446835055281585) : n(sz(s)), b(b) {\n h.assign(n + 1, 0);\n p.assign(n + 1, 1);\n rep(i, n) {\n p[i + 1] = Hash::mul(p[i], b);\n h[i + 1] = Hash::mul(h[i], b) + s[i];\n if (h[i + 1] >= mod) h[i + 1] -= mod;\n }\n }\n\n ull gethash(int l, int r) {\n ull ret = h[r] + mod - Hash::mul(h[l], p[r - l]);\n if (ret >= mod) ret -= mod;\n return ret;\n }\n\n ull getallhash() { return h[n]; }\n};\n\ntemplate <typename T>\nvector<int> manachar(const T &s) {\n int n = sz(s);\n vector<int> ret(n);\n int i = 0, j = 0;\n while (i < n) {\n while (i - j >= 0 && i + j < n && s[i + j] == s[i - j]) j++;\n ret[i] = j;\n int k = 1;\n while (i - k >= 0 && k + ret[i - k] < j) ret[i + k] = ret[i - k], k++;\n i += k, j -= k;\n }\n return ret;\n}\n\nint main() {\n string S, T;\n cin >> S >> T;\n\n int N = sz(S), M = sz(T);\n\n RollingHash hashS(S), hashT(T);\n\n auto getPalindromeLen = [](string S) {\n int n = sz(S);\n string X = \".\";\n rep(i, n) X += S[i], X += \".\";\n auto a = manachar(X);\n // print(a);\n\n vector<vector<int>> ret(2, vector<int>(n));\n rep(i, n) {\n ret[0][i] = a[2 * i + 1] / 2;\n ret[1][i] = a[2 * i] / 2;\n }\n\n // rep(i, 2) print(ret[i]);\n\n return ret;\n };\n\n vector<vector<int>> ms = getPalindromeLen(S), mt = getPalindromeLen(T);\n\n auto solve = [&](vector<int> a, vector<int> b) {\n map<ull, int> mp;\n int K = 0;\n mp[0] = K++;\n vector<vector<int>> es;\n es.eb();\n\n vector<ll> cnt;\n cnt.eb(0);\n\n // print(a);\n\n rep(i, N) {\n int ok = 0, ng = a[i] + 1;\n while (abs(ok - ng) > 1) {\n int mid = (ok + ng) / 2;\n // cout << i MM i + mid << endl;\n ull x = hashS.gethash(i, i + mid);\n (mp.count(x) ? ok : ng) = mid;\n }\n\n // cout << i MM ok << endl;\n\n int p = mp[hashS.gethash(i, i + ok)];\n rep2(j, ok + 1, a[i] + 1) {\n ull x = hashS.gethash(i, i + j);\n es[p].eb(K);\n p = K;\n mp[x] = K++;\n es.eb();\n cnt.eb(0);\n }\n\n {\n ull x = hashS.gethash(i, i + a[i]);\n cnt[mp[x]]++;\n }\n }\n\n auto dfs1 = [&](auto &&dfs1, int now) -> ll {\n each(e, es[now]) {\n cnt[now] += dfs1(dfs1, e); //\n }\n return cnt[now];\n };\n\n dfs1(dfs1, 0);\n\n cnt[0] = 0;\n\n auto dfs2 = [&](auto &&dfs2, int now) -> void {\n each(e, es[now]) {\n cnt[e] += cnt[now];\n dfs2(dfs2, e);\n }\n };\n\n dfs2(dfs2, 0);\n\n ll ret = 0;\n\n rep(i, M) {\n int ok = 0, ng = b[i] + 1;\n while (abs(ok - ng) > 1) {\n int mid = (ok + ng) / 2;\n ull x = hashT.gethash(i, i + mid);\n (mp.count(x) ? ok : ng) = mid;\n }\n\n // cout << K MM i MM i + ok << endl;\n\n ret += cnt[mp[hashT.gethash(i, i + ok)]];\n }\n\n return ret;\n };\n\n ll ans = 0;\n rep(i, 2) ans += solve(ms[i], mt[i]);\n\n cout << ans << '\\n';\n}", "accuracy": 1, "time_ms": 280, "memory_kb": 13768, "score_of_the_acc": -1.084, "final_rank": 13 }, { "submission_id": "aoj_2292_8458496", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntypedef long double ld;\n#define inf 1000000007\n#define MP make_pair\n#define MT make_tuple\n#define PB push_back\n#define fi first\n#define se second\n#define rep(i,n) for(int i = 0; i < (int)(n); ++i)\n#define rrep(i,n) for(int i = (int)n-1; i >= 0; --i)\n#define srep(i,a,b) for(int i = (int)a; i < (int)(b); ++i)\n#define all(x) (x).begin(),(x).end()\n#define SUM(v) accumulate(all(v), 0LL)\n#define MIN(v) *min_element(all(v))\n#define MAX(v) *max_element(all(v))\n#define lb(c, x) distance((c).begin(), lower_bound(all(c), (x)))\n#define ub(c, x) distance((c).begin(), upper_bound(all(c), (x)))\n#define UNIQUE(x) sort(all(x)), x.erase(unique(all(x)), x.end())\n#define SZ(c) (int)(c).size()\ntemplate<typename T>\nostream& operator << (ostream& os, vector<T>& vec) {\n os << \"{\";\n for (int i = 0; i<(int)vec.size(); i++) {\n os << vec[i] << (i + 1 == (int)vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\n// pair出力\ntemplate<typename T, typename U>\nostream& operator << (ostream& os, pair<T, U> pair_var) {\n os << \"(\" << pair_var.first << \", \" << pair_var.second << \")\";\n return os;\n}\n// map出力\ntemplate<typename T, typename U>\nostream& operator << (ostream& os, map<T, U>& map_var) {\n os << \"{\";\n for(auto itr = map_var.begin(); itr != map_var.end(); itr++){\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if(itr != map_var.end()) os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n// set 出力\ntemplate<typename T>\nostream& operator << (ostream& os, set<T>& set_var) {\n os << \"{\";\n for(auto itr = set_var.begin(); itr != set_var.end(); itr++){\n os << (*itr);\n ++itr;\n if(itr != set_var.end()) os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n// tuple 出力\ntemplate<int N,class Tuple>\nvoid out(ostream &os,const Tuple &t){}\ntemplate<int N,class Tuple,class H,class ...Ts>\nvoid out(ostream &os,const Tuple &t){\n if(N)os<<\", \";\n os<<get<N>(t);\n out<N+1,Tuple,Ts...>(os,t);\n}\ntemplate<class ...Ts>\nostream& operator<<(ostream &os, const tuple<Ts...> &t){\n os<<\"(\";\n out<0,tuple<Ts...>,Ts...>(os,t);\n os<<\")\";\n return os;\n}\n#define overload2(_1, _2, name, ...) name\n#define vec(type, name, ...) vector<type> name(__VA_ARGS__)\n#define VEC(type, name, size) \\\n vector<type> name(size); \\\n IN(name)\n#define vv(type, name, h, ...) vector<vector<type>> name(h, vector<type>(__VA_ARGS__))\n#define VV(type, name, h, w) \\\n vector<vector<type>> name(h, vector<type>(w)); \\\n IN(name)\n#define vvv(type, name, h, w, ...) vector<vector<vector<type>>> name(h, vector<vector<type>>(w, vector<type>(__VA_ARGS__)))\n#define vvvv(type, name, a, b, c, ...) \\\n vector<vector<vector<vector<type>>>> name(a, vector<vector<vector<type>>>(b, vector<vector<type>>(c, vector<type>(__VA_ARGS__))))\n#define INT(...) \\\n int __VA_ARGS__; \\\n IN(__VA_ARGS__)\n#define LL(...) \\\n ll __VA_ARGS__; \\\n IN(__VA_ARGS__)\n#define STR(...) \\\n string __VA_ARGS__; \\\n IN(__VA_ARGS__)\n#define CHR(...) \\\n char __VA_ARGS__; \\\n IN(__VA_ARGS__)\n#define DBL(...) \\\n double __VA_ARGS__; \\\n IN(__VA_ARGS__)\nint scan() { return getchar(); }\nvoid scan(int &a) { cin >> a; }\nvoid scan(long long &a) { cin >> a; }\nvoid scan(char &a) { cin >> a; }\nvoid scan(double &a) { cin >> a; }\nvoid scan(string &a) { cin >> a; }\ntemplate <class T, class S> void scan(pair<T, S> &p) { scan(p.first), scan(p.second); }\ntemplate <class T> void scan(vector<T> &);\ntemplate <class T> void scan(vector<T> &a) {\n for(auto &i : a) scan(i);\n}\ntemplate <class T> void scan(T &a) { cin >> a; }\nvoid IN() {}\ntemplate <class Head, class... Tail> void IN(Head &head, Tail &...tail) {\n scan(head);\n IN(tail...);\n}\nconst string YESNO[2] = {\"NO\", \"YES\"};\nconst string YesNo[2] = {\"No\", \"Yes\"};\nconst string yesno[2] = {\"no\", \"yes\"};\nvoid YES(bool t = 1) { cout << YESNO[t] << endl; }\nvoid NO(bool t = 1) { YES(!t); }\nvoid Yes(bool t = 1) { cout << YesNo[t] << endl; }\nvoid No(bool t = 1) { Yes(!t); }\nvoid yes(bool t = 1) { cout << yesno[t] << endl; }\nvoid no(bool t = 1) { yes(!t); }\n#ifdef LOCAL\nvoid debug_out() { cerr << endl; }\ntemplate <typename Head, typename... Tail>\nvoid debug_out(Head H, Tail... T) {\n cerr << \" \" << H;\n debug_out(T...);\n}\n#define dbg(...) \\\n cerr << __LINE__ << \" [\" << #__VA_ARGS__ << \"]:\", debug_out(__VA_ARGS__)\n#define dump(x) cerr << __LINE__ << \" \" << #x << \" = \" << (x) << endl\n#else\n#define dbg(...) (void(0))\n#define dump(x) (void(0))\n#endif\ntemplate<typename A, typename T>\nstd::enable_if_t<std::is_convertible<T, A>::value> fill(A& array, const T& val)\n{\n array = val;\n}\ntemplate<typename A, typename T>\nstd::enable_if_t<!std::is_convertible<T, A>::value> fill(A& array, const T& val)\n{\n for (auto& a : array) {\n fill(a, val);\n }\n}\ntemplate <typename T, typename S> T ceil(T x, S y) {\n assert(y);\n return (y < 0 ? ceil(-x, -y) : (x > 0 ? (x + y - 1) / y : x / y));\n}\ntemplate <typename T, typename S> T floor(T x, S y) {\n assert(y);\n return (y < 0 ? floor(-x, -y) : (x > 0 ? x / y : x / y - (x % y == 0 ? 0 : 1)));\n}\nvector<int> iota(int n) {vector<int> a(n);iota(all(a), 0);return a;}\ntemplate <class T> T POW(T x, int n) {T res = 1;for(; n; n >>= 1, x *= x){if(n & 1) res *= x;}return res;}\nll pow2(int i) { return 1LL << i; }\nint topbit(signed t) { return t == 0 ? -1 : 31 - __builtin_clz(t); }\nint topbit(ll t) { return t == 0 ? -1 : 63 - __builtin_clzll(t); }\nint lowbit(signed a) { return a == 0 ? 32 : __builtin_ctz(a); }\nint lowbit(ll a) { return a == 0 ? 64 : __builtin_ctzll(a); }\n// int allbit(int n) { return (1 << n) - 1; }\nll allbit(ll n) { return (1LL << n) - 1; }\nint popcount(signed t) { return __builtin_popcount(t); }\nint popcount(ll t) { return __builtin_popcountll(t); }\nbool ispow2(int i) { return i && (i & -i) == i; }\n\n\ntemplate <class S> void fold_in(vector<S> &v) {}\ntemplate <typename Head, typename... Tail, class S> void fold_in(vector<S> &v, Head &&a, Tail &&...tail) {\n for(auto e : a) v.emplace_back(e);\n fold_in(v, tail...);\n}\ntemplate <class S> void renumber(vector<S> &v) {}\ntemplate <typename Head, typename... Tail, class S> void renumber(vector<S> &v, Head &&a, Tail &&...tail) {\n for(auto &&e : a) e = lb(v, e);\n renumber(v, tail...);\n}\ntemplate <class S, class... Args> void zip(vector<S> &head, Args &&...args) {\n vector<S> v;\n fold_in(v, head, args...);\n sort(all(v)), v.erase(unique(all(v)), v.end());\n renumber(v, head, args...);\n}\ntemplate<class T> inline bool chmax(T &a, T b){\n if(a<b){\n a = b;\n return true;\n }\n return false;\n}\ntemplate<class T> inline bool chmin(T &a, T b){\n if(a>b){\n a = b;\n return true;\n }\n return false;\n}\n\n\nstd::vector<int> sa_naive(const std::vector<int>& s) {\n int n = int(s.size());\n std::vector<int> sa(n);\n std::iota(sa.begin(), sa.end(), 0);\n std::sort(sa.begin(), sa.end(), [&](int l, int r) {\n if (l == r) return false;\n while (l < n && r < n) {\n if (s[l] != s[r]) return s[l] < s[r];\n l++;\n r++;\n }\n return l == n;\n });\n return sa;\n}\n\nstd::vector<int> sa_doubling(const std::vector<int>& s) {\n int n = int(s.size());\n std::vector<int> sa(n), rnk = s, tmp(n);\n std::iota(sa.begin(), sa.end(), 0);\n for (int k = 1; k < n; k *= 2) {\n auto cmp = [&](int x, int y) {\n if (rnk[x] != rnk[y]) return rnk[x] < rnk[y];\n int rx = x + k < n ? rnk[x + k] : -1;\n int ry = y + k < n ? rnk[y + k] : -1;\n return rx < ry;\n };\n std::sort(sa.begin(), sa.end(), cmp);\n tmp[sa[0]] = 0;\n for (int i = 1; i < n; i++) {\n tmp[sa[i]] = tmp[sa[i - 1]] + (cmp(sa[i - 1], sa[i]) ? 1 : 0);\n }\n std::swap(tmp, rnk);\n }\n return sa;\n}\n\n// SA-IS, linear-time suffix array construction\n// Reference:\n// G. Nong, S. Zhang, and W. H. Chan,\n// Two Efficient Algorithms for Linear Time Suffix Array Construction\ntemplate <int THRESHOLD_NAIVE = 10, int THRESHOLD_DOUBLING = 40>\nstd::vector<int> sa_is(const std::vector<int>& s, int upper) {\n int n = int(s.size());\n if (n == 0) return {};\n if (n == 1) return {0};\n if (n == 2) {\n if (s[0] < s[1]) {\n return {0, 1};\n } else {\n return {1, 0};\n }\n }\n if (n < THRESHOLD_NAIVE) {\n return sa_naive(s);\n }\n if (n < THRESHOLD_DOUBLING) {\n return sa_doubling(s);\n }\n\n std::vector<int> sa(n);\n std::vector<bool> ls(n);\n for (int i = n - 2; i >= 0; i--) {\n ls[i] = (s[i] == s[i + 1]) ? ls[i + 1] : (s[i] < s[i + 1]);\n }\n std::vector<int> sum_l(upper + 1), sum_s(upper + 1);\n for (int i = 0; i < n; i++) {\n if (!ls[i]) {\n sum_s[s[i]]++;\n } else {\n sum_l[s[i] + 1]++;\n }\n }\n for (int i = 0; i <= upper; i++) {\n sum_s[i] += sum_l[i];\n if (i < upper) sum_l[i + 1] += sum_s[i];\n }\n\n auto induce = [&](const std::vector<int>& lms) {\n std::fill(sa.begin(), sa.end(), -1);\n std::vector<int> buf(upper + 1);\n std::copy(sum_s.begin(), sum_s.end(), buf.begin());\n for (auto d : lms) {\n if (d == n) continue;\n sa[buf[s[d]]++] = d;\n }\n std::copy(sum_l.begin(), sum_l.end(), buf.begin());\n sa[buf[s[n - 1]]++] = n - 1;\n for (int i = 0; i < n; i++) {\n int v = sa[i];\n if (v >= 1 && !ls[v - 1]) {\n sa[buf[s[v - 1]]++] = v - 1;\n }\n }\n std::copy(sum_l.begin(), sum_l.end(), buf.begin());\n for (int i = n - 1; i >= 0; i--) {\n int v = sa[i];\n if (v >= 1 && ls[v - 1]) {\n sa[--buf[s[v - 1] + 1]] = v - 1;\n }\n }\n };\n\n std::vector<int> lms_map(n + 1, -1);\n int m = 0;\n for (int i = 1; i < n; i++) {\n if (!ls[i - 1] && ls[i]) {\n lms_map[i] = m++;\n }\n }\n std::vector<int> lms;\n lms.reserve(m);\n for (int i = 1; i < n; i++) {\n if (!ls[i - 1] && ls[i]) {\n lms.push_back(i);\n }\n }\n\n induce(lms);\n\n if (m) {\n std::vector<int> sorted_lms;\n sorted_lms.reserve(m);\n for (int v : sa) {\n if (lms_map[v] != -1) sorted_lms.push_back(v);\n }\n std::vector<int> rec_s(m);\n int rec_upper = 0;\n rec_s[lms_map[sorted_lms[0]]] = 0;\n for (int i = 1; i < m; i++) {\n int l = sorted_lms[i - 1], r = sorted_lms[i];\n int end_l = (lms_map[l] + 1 < m) ? lms[lms_map[l] + 1] : n;\n int end_r = (lms_map[r] + 1 < m) ? lms[lms_map[r] + 1] : n;\n bool same = true;\n if (end_l - l != end_r - r) {\n same = false;\n } else {\n while (l < end_l) {\n if (s[l] != s[r]) {\n break;\n }\n l++;\n r++;\n }\n if (l == n || s[l] != s[r]) same = false;\n }\n if (!same) rec_upper++;\n rec_s[lms_map[sorted_lms[i]]] = rec_upper;\n }\n\n auto rec_sa =\n sa_is<THRESHOLD_NAIVE, THRESHOLD_DOUBLING>(rec_s, rec_upper);\n\n for (int i = 0; i < m; i++) {\n sorted_lms[i] = lms[rec_sa[i]];\n }\n induce(sorted_lms);\n }\n return sa;\n}\n\nstd::vector<int> suffix_array(const std::string& s) {\n int n = int(s.size());\n std::vector<int> s2(n);\n for (int i = 0; i < n; i++) {\n s2[i] = s[i];\n }\n return sa_is(s2, 255);\n}\n\ntemplate <class T>\nstd::vector<int> lcp_array(const std::vector<T>& s,\n const std::vector<int>& sa) {\n int n = int(s.size());\n assert(n >= 1);\n std::vector<int> rnk(n);\n for (int i = 0; i < n; i++) {\n rnk[sa[i]] = i;\n }\n std::vector<int> lcp(n - 1);\n int h = 0;\n for (int i = 0; i < n; i++) {\n if (h > 0) h--;\n if (rnk[i] == 0) continue;\n int j = sa[rnk[i] - 1];\n for (; j + h < n && i + h < n; h++) {\n if (s[j + h] != s[i + h]) break;\n }\n lcp[rnk[i] - 1] = h;\n }\n return lcp;\n}\n\nstd::vector<int> lcp_array(const std::string& s, const std::vector<int>& sa) {\n int n = int(s.size());\n std::vector<int> s2(n);\n for (int i = 0; i < n; i++) {\n s2[i] = s[i];\n }\n return lcp_array(s2, sa);\n}\n\ntemplate<typename T> class segtree {\nprivate:\n int n,sz,h; vector<T> node, lazy_update, lazy_add; vector<bool> lazyFlag;\npublic:\n segtree(vector<T> v) : n(1), sz((int)v.size()), h(0){\n while(n < sz) n *= 2, h++;\n node.resize(2*n, 0);\n lazy_update.resize(2*n, 0); lazyFlag.resize(2*n, false);\n lazy_add.resize(2*n, 0);\n for(int i = 0; i < sz; i++) node[i+n] = v[i];\n for(int i=n-1; i>=1; i--) node[i] = node[2*i] + node[2*i+1];\n }\n void eval(int k) {\n if(lazyFlag[k]){\n lazy_update[k] += lazy_add[k];\n node[k] = lazy_update[k];\n if(k < n) {\n lazy_add[2*k] = lazy_add[2*k+1] = 0;\n lazy_update[2*k] = lazy_update[2*k+1] = lazy_update[k] / 2;\n lazyFlag[2*k] = lazyFlag[2*k+1] = true;\n }\n lazy_add[k] = 0, lazyFlag[k] = false;\n }else if(lazy_add[k] != 0){\n node[k] += lazy_add[k];\n if(k < n){\n lazy_add[2*k] += lazy_add[k] / 2; lazy_add[2*k+1] += lazy_add[k] / 2;\n }\n lazy_add[k] = 0;\n }\n }\n void update(int a, int b, T x, int k=1, int l=0, int r=-1) {\n if(r < 0) r = n;\n eval(k);\n if(b <= l || r <= a) return;\n if(a <= l && r <= b){\n lazy_update[k] = x*(r-l); lazyFlag[k] = true; eval(k);\n }else{\n update(a, b, x, 2*k, l, (l+r)/2); update(a, b, x, 2*k+1, (l+r)/2, r);\n node[k] = node[2*k] + node[2*k+1];\n }\n }\n void add(int a, int b, T x, int k=1, int l=0, int r=-1){\n if(r < 0) r = n;\n eval(k);\n if(b <= l || r <= a) return;\n if(a <= l && r <= b){\n lazy_add[k] += x*(r-l); eval(k);\n }else{\n add(a, b, x, 2*k, l, (l+r)/2); add(a, b, x, 2*k+1, (l+r)/2, r);\n node[k] = node[2*k] + node[2*k+1];\n }\n }\n T query(int a, int b) {\n a += n, b += n - 1;\n for(int i = h; i > 0; i--) eval(a >> i), eval(b >> i);\n b++;\n T res1 = 0, res2 = 0;\n while(a < b) {\n if(a & 1) eval(a), res1 += node[a++];\n if(b & 1) eval(--b), res2 += node[b];\n a >>= 1, b >>= 1;\n }\n return res1 + res2;\n }\n void print(){for(int i = 0; i < sz; i++)cout<<query(i,i+1)<< \" \";cout<<endl;}\n};\n\n\n\n\n//iを中心とする最長の回文の半径をR[i]に格納(O(n))\n// abaab を $a$b$a$a$b$ みたいにすると偶数長のもの求めることが可能\nvoid manacher(const string& S,vector<int>& res)\n{\n int sz = (int)S.size(), i = 0, j = 0, k;\n res.resize(sz);\n while(i < sz){\n while(i-j >= 0 && i+j < sz && S[i-j] == S[i+j]) j++;\n res[i] = j, k = 1;\n while(i-k >= 0 && i+k < sz && k+res[i-k] < j){\n res[i+k] = res[i-k], k++;\n }\n i += k; j -= k;\n }\n}\n\n\nint main(){\n STR(s,t);\n string Q;\n rep(i,s.size()){\n Q.push_back('$');\n Q.push_back(s[i]);\n }\n Q.push_back('$');\n Q.push_back('|');\n int T = Q.size();\n rep(i,t.size()){\n Q.push_back('$');\n Q.push_back(t[i]);\n }\n Q.push_back('$');\n vector<int> mana;\n manacher(Q,mana);\n // cerr << mana << endl;\n int n = Q.size();\n \n auto sa = suffix_array(Q);\n auto lcp = lcp_array(Q,sa);\n ll res = 0;\n vector<ll> ppp(30000);\n segtree<ll> segS(ppp);\n segtree<ll> segT(ppp);\n // cerr << Q << endl;\n for(int i=0;i<sa.size();i++){\n int id = sa[i];\n int LCP = inf;\n if(i!=0)chmin(LCP,lcp[i-1]);\n if(Q[id]=='$'){\n int len = LCP/2; \n segS.update(len+1,30000,0);\n segT.update(len+1,30000,0);\n }else{\n int len = (LCP+1)/2;\n segS.update(len+1,30000,0);\n segT.update(len+1,30000,0);\n }\n if(Q[id]=='|')continue;\n if(id<T){\n int len = mana[id]/2;\n res += segT.query(1,len+1);\n segS.add(0,len+1,1);\n }else{\n int len = mana[id]/2;\n res += segS.query(1,len+1);\n segT.add(0,len+1,1);\n }\n // cerr << id << endl;\n // cerr << Q.substr(id) << endl;\n // dbg(res);\n }\n cout << res << endl;\n\n \n return 0;\n}", "accuracy": 1, "time_ms": 190, "memory_kb": 9928, "score_of_the_acc": -0.6543, "final_rank": 10 }, { "submission_id": "aoj_2292_8458485", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntypedef long double ld;\n#define inf 1000000007\n#define MP make_pair\n#define MT make_tuple\n#define PB push_back\n#define fi first\n#define se second\n#define rep(i,n) for(int i = 0; i < (int)(n); ++i)\n#define rrep(i,n) for(int i = (int)n-1; i >= 0; --i)\n#define srep(i,a,b) for(int i = (int)a; i < (int)(b); ++i)\n#define all(x) (x).begin(),(x).end()\n#define SUM(v) accumulate(all(v), 0LL)\n#define MIN(v) *min_element(all(v))\n#define MAX(v) *max_element(all(v))\n#define lb(c, x) distance((c).begin(), lower_bound(all(c), (x)))\n#define ub(c, x) distance((c).begin(), upper_bound(all(c), (x)))\n#define UNIQUE(x) sort(all(x)), x.erase(unique(all(x)), x.end())\n#define SZ(c) (int)(c).size()\ntemplate<typename T>\nostream& operator << (ostream& os, vector<T>& vec) {\n os << \"{\";\n for (int i = 0; i<(int)vec.size(); i++) {\n os << vec[i] << (i + 1 == (int)vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\n// pair出力\ntemplate<typename T, typename U>\nostream& operator << (ostream& os, pair<T, U> pair_var) {\n os << \"(\" << pair_var.first << \", \" << pair_var.second << \")\";\n return os;\n}\n// map出力\ntemplate<typename T, typename U>\nostream& operator << (ostream& os, map<T, U>& map_var) {\n os << \"{\";\n for(auto itr = map_var.begin(); itr != map_var.end(); itr++){\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if(itr != map_var.end()) os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n// set 出力\ntemplate<typename T>\nostream& operator << (ostream& os, set<T>& set_var) {\n os << \"{\";\n for(auto itr = set_var.begin(); itr != set_var.end(); itr++){\n os << (*itr);\n ++itr;\n if(itr != set_var.end()) os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n// tuple 出力\ntemplate<int N,class Tuple>\nvoid out(ostream &os,const Tuple &t){}\ntemplate<int N,class Tuple,class H,class ...Ts>\nvoid out(ostream &os,const Tuple &t){\n if(N)os<<\", \";\n os<<get<N>(t);\n out<N+1,Tuple,Ts...>(os,t);\n}\ntemplate<class ...Ts>\nostream& operator<<(ostream &os, const tuple<Ts...> &t){\n os<<\"(\";\n out<0,tuple<Ts...>,Ts...>(os,t);\n os<<\")\";\n return os;\n}\n#define overload2(_1, _2, name, ...) name\n#define vec(type, name, ...) vector<type> name(__VA_ARGS__)\n#define VEC(type, name, size) \\\n vector<type> name(size); \\\n IN(name)\n#define vv(type, name, h, ...) vector<vector<type>> name(h, vector<type>(__VA_ARGS__))\n#define VV(type, name, h, w) \\\n vector<vector<type>> name(h, vector<type>(w)); \\\n IN(name)\n#define vvv(type, name, h, w, ...) vector<vector<vector<type>>> name(h, vector<vector<type>>(w, vector<type>(__VA_ARGS__)))\n#define vvvv(type, name, a, b, c, ...) \\\n vector<vector<vector<vector<type>>>> name(a, vector<vector<vector<type>>>(b, vector<vector<type>>(c, vector<type>(__VA_ARGS__))))\n#define INT(...) \\\n int __VA_ARGS__; \\\n IN(__VA_ARGS__)\n#define LL(...) \\\n ll __VA_ARGS__; \\\n IN(__VA_ARGS__)\n#define STR(...) \\\n string __VA_ARGS__; \\\n IN(__VA_ARGS__)\n#define CHR(...) \\\n char __VA_ARGS__; \\\n IN(__VA_ARGS__)\n#define DBL(...) \\\n double __VA_ARGS__; \\\n IN(__VA_ARGS__)\nint scan() { return getchar(); }\nvoid scan(int &a) { cin >> a; }\nvoid scan(long long &a) { cin >> a; }\nvoid scan(char &a) { cin >> a; }\nvoid scan(double &a) { cin >> a; }\nvoid scan(string &a) { cin >> a; }\ntemplate <class T, class S> void scan(pair<T, S> &p) { scan(p.first), scan(p.second); }\ntemplate <class T> void scan(vector<T> &);\ntemplate <class T> void scan(vector<T> &a) {\n for(auto &i : a) scan(i);\n}\ntemplate <class T> void scan(T &a) { cin >> a; }\nvoid IN() {}\ntemplate <class Head, class... Tail> void IN(Head &head, Tail &...tail) {\n scan(head);\n IN(tail...);\n}\nconst string YESNO[2] = {\"NO\", \"YES\"};\nconst string YesNo[2] = {\"No\", \"Yes\"};\nconst string yesno[2] = {\"no\", \"yes\"};\nvoid YES(bool t = 1) { cout << YESNO[t] << endl; }\nvoid NO(bool t = 1) { YES(!t); }\nvoid Yes(bool t = 1) { cout << YesNo[t] << endl; }\nvoid No(bool t = 1) { Yes(!t); }\nvoid yes(bool t = 1) { cout << yesno[t] << endl; }\nvoid no(bool t = 1) { yes(!t); }\n#ifdef LOCAL\nvoid debug_out() { cerr << endl; }\ntemplate <typename Head, typename... Tail>\nvoid debug_out(Head H, Tail... T) {\n cerr << \" \" << H;\n debug_out(T...);\n}\n#define dbg(...) \\\n cerr << __LINE__ << \" [\" << #__VA_ARGS__ << \"]:\", debug_out(__VA_ARGS__)\n#define dump(x) cerr << __LINE__ << \" \" << #x << \" = \" << (x) << endl\n#else\n#define dbg(...) (void(0))\n#define dump(x) (void(0))\n#endif\ntemplate<typename A, typename T>\nstd::enable_if_t<std::is_convertible<T, A>::value> fill(A& array, const T& val)\n{\n array = val;\n}\ntemplate<typename A, typename T>\nstd::enable_if_t<!std::is_convertible<T, A>::value> fill(A& array, const T& val)\n{\n for (auto& a : array) {\n fill(a, val);\n }\n}\ntemplate <typename T, typename S> T ceil(T x, S y) {\n assert(y);\n return (y < 0 ? ceil(-x, -y) : (x > 0 ? (x + y - 1) / y : x / y));\n}\ntemplate <typename T, typename S> T floor(T x, S y) {\n assert(y);\n return (y < 0 ? floor(-x, -y) : (x > 0 ? x / y : x / y - (x % y == 0 ? 0 : 1)));\n}\nvector<int> iota(int n) {vector<int> a(n);iota(all(a), 0);return a;}\ntemplate <class T> T POW(T x, int n) {T res = 1;for(; n; n >>= 1, x *= x){if(n & 1) res *= x;}return res;}\nll pow2(int i) { return 1LL << i; }\nint topbit(signed t) { return t == 0 ? -1 : 31 - __builtin_clz(t); }\nint topbit(ll t) { return t == 0 ? -1 : 63 - __builtin_clzll(t); }\nint lowbit(signed a) { return a == 0 ? 32 : __builtin_ctz(a); }\nint lowbit(ll a) { return a == 0 ? 64 : __builtin_ctzll(a); }\n// int allbit(int n) { return (1 << n) - 1; }\nll allbit(ll n) { return (1LL << n) - 1; }\nint popcount(signed t) { return __builtin_popcount(t); }\nint popcount(ll t) { return __builtin_popcountll(t); }\nbool ispow2(int i) { return i && (i & -i) == i; }\n\n\ntemplate <class S> void fold_in(vector<S> &v) {}\ntemplate <typename Head, typename... Tail, class S> void fold_in(vector<S> &v, Head &&a, Tail &&...tail) {\n for(auto e : a) v.emplace_back(e);\n fold_in(v, tail...);\n}\ntemplate <class S> void renumber(vector<S> &v) {}\ntemplate <typename Head, typename... Tail, class S> void renumber(vector<S> &v, Head &&a, Tail &&...tail) {\n for(auto &&e : a) e = lb(v, e);\n renumber(v, tail...);\n}\ntemplate <class S, class... Args> void zip(vector<S> &head, Args &&...args) {\n vector<S> v;\n fold_in(v, head, args...);\n sort(all(v)), v.erase(unique(all(v)), v.end());\n renumber(v, head, args...);\n}\ntemplate<class T> inline bool chmax(T &a, T b){\n if(a<b){\n a = b;\n return true;\n }\n return false;\n}\ntemplate<class T> inline bool chmin(T &a, T b){\n if(a>b){\n a = b;\n return true;\n }\n return false;\n}\n\n\nstd::vector<int> sa_naive(const std::vector<int>& s) {\n int n = int(s.size());\n std::vector<int> sa(n);\n std::iota(sa.begin(), sa.end(), 0);\n std::sort(sa.begin(), sa.end(), [&](int l, int r) {\n if (l == r) return false;\n while (l < n && r < n) {\n if (s[l] != s[r]) return s[l] < s[r];\n l++;\n r++;\n }\n return l == n;\n });\n return sa;\n}\n\nstd::vector<int> sa_doubling(const std::vector<int>& s) {\n int n = int(s.size());\n std::vector<int> sa(n), rnk = s, tmp(n);\n std::iota(sa.begin(), sa.end(), 0);\n for (int k = 1; k < n; k *= 2) {\n auto cmp = [&](int x, int y) {\n if (rnk[x] != rnk[y]) return rnk[x] < rnk[y];\n int rx = x + k < n ? rnk[x + k] : -1;\n int ry = y + k < n ? rnk[y + k] : -1;\n return rx < ry;\n };\n std::sort(sa.begin(), sa.end(), cmp);\n tmp[sa[0]] = 0;\n for (int i = 1; i < n; i++) {\n tmp[sa[i]] = tmp[sa[i - 1]] + (cmp(sa[i - 1], sa[i]) ? 1 : 0);\n }\n std::swap(tmp, rnk);\n }\n return sa;\n}\n\n// SA-IS, linear-time suffix array construction\n// Reference:\n// G. Nong, S. Zhang, and W. H. Chan,\n// Two Efficient Algorithms for Linear Time Suffix Array Construction\ntemplate <int THRESHOLD_NAIVE = 10, int THRESHOLD_DOUBLING = 40>\nstd::vector<int> sa_is(const std::vector<int>& s, int upper) {\n int n = int(s.size());\n if (n == 0) return {};\n if (n == 1) return {0};\n if (n == 2) {\n if (s[0] < s[1]) {\n return {0, 1};\n } else {\n return {1, 0};\n }\n }\n if (n < THRESHOLD_NAIVE) {\n return sa_naive(s);\n }\n if (n < THRESHOLD_DOUBLING) {\n return sa_doubling(s);\n }\n\n std::vector<int> sa(n);\n std::vector<bool> ls(n);\n for (int i = n - 2; i >= 0; i--) {\n ls[i] = (s[i] == s[i + 1]) ? ls[i + 1] : (s[i] < s[i + 1]);\n }\n std::vector<int> sum_l(upper + 1), sum_s(upper + 1);\n for (int i = 0; i < n; i++) {\n if (!ls[i]) {\n sum_s[s[i]]++;\n } else {\n sum_l[s[i] + 1]++;\n }\n }\n for (int i = 0; i <= upper; i++) {\n sum_s[i] += sum_l[i];\n if (i < upper) sum_l[i + 1] += sum_s[i];\n }\n\n auto induce = [&](const std::vector<int>& lms) {\n std::fill(sa.begin(), sa.end(), -1);\n std::vector<int> buf(upper + 1);\n std::copy(sum_s.begin(), sum_s.end(), buf.begin());\n for (auto d : lms) {\n if (d == n) continue;\n sa[buf[s[d]]++] = d;\n }\n std::copy(sum_l.begin(), sum_l.end(), buf.begin());\n sa[buf[s[n - 1]]++] = n - 1;\n for (int i = 0; i < n; i++) {\n int v = sa[i];\n if (v >= 1 && !ls[v - 1]) {\n sa[buf[s[v - 1]]++] = v - 1;\n }\n }\n std::copy(sum_l.begin(), sum_l.end(), buf.begin());\n for (int i = n - 1; i >= 0; i--) {\n int v = sa[i];\n if (v >= 1 && ls[v - 1]) {\n sa[--buf[s[v - 1] + 1]] = v - 1;\n }\n }\n };\n\n std::vector<int> lms_map(n + 1, -1);\n int m = 0;\n for (int i = 1; i < n; i++) {\n if (!ls[i - 1] && ls[i]) {\n lms_map[i] = m++;\n }\n }\n std::vector<int> lms;\n lms.reserve(m);\n for (int i = 1; i < n; i++) {\n if (!ls[i - 1] && ls[i]) {\n lms.push_back(i);\n }\n }\n\n induce(lms);\n\n if (m) {\n std::vector<int> sorted_lms;\n sorted_lms.reserve(m);\n for (int v : sa) {\n if (lms_map[v] != -1) sorted_lms.push_back(v);\n }\n std::vector<int> rec_s(m);\n int rec_upper = 0;\n rec_s[lms_map[sorted_lms[0]]] = 0;\n for (int i = 1; i < m; i++) {\n int l = sorted_lms[i - 1], r = sorted_lms[i];\n int end_l = (lms_map[l] + 1 < m) ? lms[lms_map[l] + 1] : n;\n int end_r = (lms_map[r] + 1 < m) ? lms[lms_map[r] + 1] : n;\n bool same = true;\n if (end_l - l != end_r - r) {\n same = false;\n } else {\n while (l < end_l) {\n if (s[l] != s[r]) {\n break;\n }\n l++;\n r++;\n }\n if (l == n || s[l] != s[r]) same = false;\n }\n if (!same) rec_upper++;\n rec_s[lms_map[sorted_lms[i]]] = rec_upper;\n }\n\n auto rec_sa =\n sa_is<THRESHOLD_NAIVE, THRESHOLD_DOUBLING>(rec_s, rec_upper);\n\n for (int i = 0; i < m; i++) {\n sorted_lms[i] = lms[rec_sa[i]];\n }\n induce(sorted_lms);\n }\n return sa;\n}\n\nstd::vector<int> suffix_array(const std::string& s) {\n int n = int(s.size());\n std::vector<int> s2(n);\n for (int i = 0; i < n; i++) {\n s2[i] = s[i];\n }\n return sa_is(s2, 255);\n}\n\ntemplate <class T>\nstd::vector<int> lcp_array(const std::vector<T>& s,\n const std::vector<int>& sa) {\n int n = int(s.size());\n assert(n >= 1);\n std::vector<int> rnk(n);\n for (int i = 0; i < n; i++) {\n rnk[sa[i]] = i;\n }\n std::vector<int> lcp(n - 1);\n int h = 0;\n for (int i = 0; i < n; i++) {\n if (h > 0) h--;\n if (rnk[i] == 0) continue;\n int j = sa[rnk[i] - 1];\n for (; j + h < n && i + h < n; h++) {\n if (s[j + h] != s[i + h]) break;\n }\n lcp[rnk[i] - 1] = h;\n }\n return lcp;\n}\n\nstd::vector<int> lcp_array(const std::string& s, const std::vector<int>& sa) {\n int n = int(s.size());\n std::vector<int> s2(n);\n for (int i = 0; i < n; i++) {\n s2[i] = s[i];\n }\n return lcp_array(s2, sa);\n}\n\ntemplate<typename T> class segtree {\nprivate:\n int n,sz,h; vector<T> node, lazy_update, lazy_add; vector<bool> lazyFlag;\npublic:\n segtree(vector<T> v) : n(1), sz((int)v.size()), h(0){\n while(n < sz) n *= 2, h++;\n node.resize(2*n, 0);\n lazy_update.resize(2*n, 0); lazyFlag.resize(2*n, false);\n lazy_add.resize(2*n, 0);\n for(int i = 0; i < sz; i++) node[i+n] = v[i];\n for(int i=n-1; i>=1; i--) node[i] = node[2*i] + node[2*i+1];\n }\n void eval(int k) {\n if(lazyFlag[k]){\n lazy_update[k] += lazy_add[k];\n node[k] = lazy_update[k];\n if(k < n) {\n lazy_add[2*k] = lazy_add[2*k+1] = 0;\n lazy_update[2*k] = lazy_update[2*k+1] = lazy_update[k] / 2;\n lazyFlag[2*k] = lazyFlag[2*k+1] = true;\n }\n lazy_add[k] = 0, lazyFlag[k] = false;\n }else if(lazy_add[k] != 0){\n node[k] += lazy_add[k];\n if(k < n){\n lazy_add[2*k] += lazy_add[k] / 2; lazy_add[2*k+1] += lazy_add[k] / 2;\n }\n lazy_add[k] = 0;\n }\n }\n void update(int a, int b, T x, int k=1, int l=0, int r=-1) {\n if(r < 0) r = n;\n eval(k);\n if(b <= l || r <= a) return;\n if(a <= l && r <= b){\n lazy_update[k] = x*(r-l); lazyFlag[k] = true; eval(k);\n }else{\n update(a, b, x, 2*k, l, (l+r)/2); update(a, b, x, 2*k+1, (l+r)/2, r);\n node[k] = node[2*k] + node[2*k+1];\n }\n }\n void add(int a, int b, T x, int k=1, int l=0, int r=-1){\n if(r < 0) r = n;\n eval(k);\n if(b <= l || r <= a) return;\n if(a <= l && r <= b){\n lazy_add[k] += x*(r-l); eval(k);\n }else{\n add(a, b, x, 2*k, l, (l+r)/2); add(a, b, x, 2*k+1, (l+r)/2, r);\n node[k] = node[2*k] + node[2*k+1];\n }\n }\n T query(int a, int b) {\n a += n, b += n - 1;\n for(int i = h; i > 0; i--) eval(a >> i), eval(b >> i);\n b++;\n T res1 = 0, res2 = 0;\n while(a < b) {\n if(a & 1) eval(a), res1 += node[a++];\n if(b & 1) eval(--b), res2 += node[b];\n a >>= 1, b >>= 1;\n }\n return res1 + res2;\n }\n void print(){for(int i = 0; i < sz; i++)cout<<query(i,i+1)<< \" \";cout<<endl;}\n};\n\n\n\n\n//iを中心とする最長の回文の半径をR[i]に格納(O(n))\n// abaab を $a$b$a$a$b$ みたいにすると偶数長のもの求めることが可能\nvoid manacher(const string& S,vector<int>& res)\n{\n int sz = (int)S.size(), i = 0, j = 0, k;\n res.resize(sz);\n while(i < sz){\n while(i-j >= 0 && i+j < sz && S[i-j] == S[i+j]) j++;\n res[i] = j, k = 1;\n while(i-k >= 0 && i+k < sz && k+res[i-k] < j){\n res[i+k] = res[i-k], k++;\n }\n i += k; j -= k;\n }\n}\n\n\nint main(){\n STR(s,t);\n string Q;\n rep(i,s.size()){\n Q.push_back('$');\n Q.push_back(s[i]);\n }\n Q.push_back('$');\n Q.push_back('|');\n int T = Q.size();\n rep(i,t.size()){\n Q.push_back('$');\n Q.push_back(t[i]);\n }\n Q.push_back('$');\n vector<int> mana;\n manacher(Q,mana);\n // cerr << mana << endl;\n int n = Q.size();\n \n auto sa = suffix_array(Q);\n auto lcp = lcp_array(Q,sa);\n ll res = 0;\n vector<ll> ppp(30000);\n segtree<ll> segS(ppp);\n segtree<ll> segT(ppp);\n // cerr << Q << endl;\n for(int i=0;i<sa.size();i++){\n int id = sa[i];\n int LCP = inf;\n if(i!=0)chmin(LCP,lcp[i-1]);\n if(Q[id]=='$'){\n int len = LCP/2; \n segS.update(len+1,30000,0);\n segT.update(len+1,30000,0);\n }else{\n int len = (LCP+1)/2;\n segS.update(len+1,30000,0);\n segT.update(len+1,30000,0);\n }\n if(id<T){\n int len = mana[id]/2;\n res += segT.query(1,len+1);\n segS.add(0,len+1,1);\n }else{\n int len = mana[id]/2;\n res += segS.query(1,len+1);\n segT.add(0,len+1,1);\n }\n // cerr << id << endl;\n // cerr << Q.substr(id) << endl;\n // dbg(res);\n }\n cout << res << endl;\n\n \n return 0;\n}", "accuracy": 0.08333333333333333, "time_ms": 160, "memory_kb": 9680, "score_of_the_acc": -0.5431, "final_rank": 19 }, { "submission_id": "aoj_2292_7743077", "code_snippet": "#include <algorithm>\n#include <bitset>\n#include <cassert>\n#include <chrono>\n#include <climits>\n#include <cmath>\n#include <cstddef>\n#include <cstdint>\n#include <cstdlib>\n#include <cstring>\n#include <functional>\n#include <iomanip>\n#include <iostream>\n#include <limits>\n#include <map>\n#include <memory>\n#include <numeric>\n#include <optional>\n#include <queue>\n#include <random>\n#include <set>\n#include <stack>\n#include <string>\n#include <tuple>\n#include <type_traits>\n#include <unordered_map>\n#include <unordered_set>\n#include <utility>\n#include <vector>\n\n/* macro */\n\n#define rep(i, a, n) for (int i = (int)(a); i < (int)(n); i++)\n#define rrep(i, a, n) for (int i = ((int)(n)-1); i >= (int)(a); i--)\n#define Rep(i, a, n) for (i64 i = (i64)(a); i < (i64)(n); i++)\n#define RRep(i, a, n) for (i64 i = ((i64)(n)-i64(1)); i >= (i64)(a); i--)\n#define all(v) (v).begin(), (v).end()\n#define rall(v) (v).rbegin(), (v).rend()\n\n/* macro end */\n\n/* template */\n\nnamespace ebi {\n\n#ifdef LOCAL\n#define debug(...) \\\n std::cerr << \"LINE: \" << __LINE__ << \" [\" << #__VA_ARGS__ << \"]:\", \\\n debug_out(__VA_ARGS__)\n#else\n#define debug(...)\n#endif\n\nvoid debug_out() { std::cerr << std::endl; }\n\ntemplate <typename Head, typename... Tail>\nvoid debug_out(Head h, Tail... t) {\n std::cerr << \" \" << h;\n if (sizeof...(t) > 0) std::cout << \" :\";\n debug_out(t...);\n}\n\ntemplate <typename T1, typename T2>\nstd::ostream &operator<<(std::ostream &os, const std::pair<T1, T2> &pa) {\n return os << pa.first << \" \" << pa.second;\n}\n\ntemplate <typename T1, typename T2>\nstd::istream &operator>>(std::istream &os, std::pair<T1, T2> &pa) {\n return os >> pa.first >> pa.second;\n}\n\ntemplate <typename T>\nstd::ostream &operator<<(std::ostream &os, const std::vector<T> &vec) {\n for (std::size_t i = 0; i < vec.size(); i++)\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \" \");\n return os;\n}\n\ntemplate <typename T>\nstd::istream &operator>>(std::istream &os, std::vector<T> &vec) {\n for (T &e : vec) std::cin >> e;\n return os;\n}\n\ntemplate <typename T>\nstd::ostream &operator<<(std::ostream &os, const std::optional<T> &opt) {\n if (opt) {\n os << opt.value();\n } else {\n os << \"invalid value\";\n }\n return os;\n}\n\nusing std::size_t;\nusing i32 = std::int32_t;\nusing u32 = std::uint32_t;\nusing i64 = std::int64_t;\nusing u64 = std::uint64_t;\n\ntemplate <class T, T init>\nauto make_vector(int n) {\n return std::vector<T>(n, init);\n}\n\ntemplate <class T, T init, typename Head, typename... Tail>\nauto make_vector(Head n, Tail... ts) {\n return std::vector(n, make_vector<T, init>(ts...));\n}\n\ntemplate <class T>\ninline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return true;\n }\n return false;\n}\n\ntemplate <class T>\ninline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return true;\n }\n return false;\n}\n\ntemplate <class T>\nT pow(T x, i64 n) {\n T res = 1;\n while (n > 0) {\n if (n & 1) res = res * x;\n x = x * x;\n n >>= 1;\n }\n return res;\n}\n\ntemplate <class T>\nT mod_pow(T x, i64 n, i64 mod) {\n T res = 1;\n while (n > 0) {\n if (n & 1) res = (res * x) % mod;\n x = (x * x) % mod;\n n >>= 1;\n }\n return res;\n}\n\ntemplate <class T>\nT scan() {\n T val;\n std::cin >> val;\n return val;\n}\n\ntemplate <class T>\nstruct Edge {\n int to;\n T cost;\n Edge(int _to, T _cost = 1) : to(_to), cost(_cost) {}\n};\n\ntemplate <class T>\nstruct Graph : std::vector<std::vector<Edge<T>>> {\n using std::vector<std::vector<Edge<T>>>::vector;\n void add_edge(int u, int v, T w, bool directed = false) {\n (*this)[u].emplace_back(v, w);\n if (directed) return;\n (*this)[v].emplace_back(u, w);\n }\n};\n\nstruct graph : std::vector<std::vector<int>> {\n using std::vector<std::vector<int>>::vector;\n void add_edge(int u, int v, bool directed = false) {\n (*this)[u].emplace_back(v);\n if (directed) return;\n (*this)[v].emplace_back(u);\n }\n};\n\nconstexpr i64 LNF = std::numeric_limits<i64>::max() / 4;\n\nconstexpr int INF = std::numeric_limits<int>::max() / 2;\n\nconst std::vector<int> dy = {1, 0, -1, 0, 1, 1, -1, -1};\nconst std::vector<int> dx = {0, 1, 0, -1, 1, -1, 1, -1};\n\n} // namespace ebi\n\nnamespace ebi {\n\nstruct random_number_generator_64 {\nprivate:\n using u64 = std::uint64_t;\n std::random_device rnd;\n std::mt19937_64 mt;\npublic:\n random_number_generator_64() : mt(rnd()) { }\n\n u64 get(u64 a, u64 b) {\n std::uniform_int_distribution<u64> dist(a, b-1);\n return dist(mt);\n }\n};\n\n}\n\nnamespace ebi {\n\nusing u64 = std::uint64_t;\nconst u64 mod = (1ull << 61) - 1;\nconst u64 MASK31 = (1ull << 31) - 1;\nconst u64 MASK30 = (1ull << 30) - 1;\n\nu64 safe_mul(const u64 &a, const u64 &b) {\n u64 au = a >> 31, ad = a & MASK31;\n u64 bu = b >> 31, bd = b & MASK31;\n u64 mid = ad * bu + au * bd;\n u64 midu = mid >> 30;\n u64 midd = mid & MASK30;\n return (au * bu * 2 + midu + (midd << 31) + ad * bd);\n}\n\nu64 safe_mod(const u64 &a) {\n u64 au = a >> 61;\n u64 ad = a & mod;\n u64 res = au + ad;\n if (res >= mod) res -= mod;\n return res;\n}\n\n} // namespace ebi\n\n/*\n reference: https://qiita.com/keymoon/items/11fac5627672a6d6a9f6\n*/\n\nnamespace ebi {\n\ntemplate <int n>\nstruct rolling_hash {\n private:\n using u64 = std::uint64_t;\n const u64 h = 100;\n const u64 buffer = mod * 4;\n\n public:\n rolling_hash(const std::string &s) : sz(s.size()) {\n assert(int(base.size()) == n && n > 0);\n base_pow.resize(n);\n hash.resize(n);\n for (int i = 0; i < n; ++i) {\n base_pow[i].reserve(sz + 1);\n base_pow[i].emplace_back(1);\n hash[i].reserve(sz + 1);\n hash[i].emplace_back(0);\n for (const auto &c : s) {\n hash[i].emplace_back(\n safe_mod(safe_mul(hash[i].back(), base[i]) + c + h));\n base_pow[i].emplace_back(\n safe_mod(safe_mul(base_pow[i].back(), base[i])));\n }\n }\n }\n\n // [l, r)\n\n std::vector<u64> get_hash(int l, int r) const {\n std::vector<u64> ret(n);\n for (int i = 0; i < n; ++i) {\n ret[i] = safe_mod(hash[i][r] + buffer -\n safe_mul(hash[i][l], base_pow[i][r - l]));\n }\n return ret;\n }\n\n std::vector<u64> get_hash(const std::string &str, int l = 0,\n int r = -1) const {\n if (r < 0) r = int(str.size());\n std::vector<u64> res(n, 0);\n for (int i = 0; i < n; ++i) {\n for (int j = l; j < r; ++j) {\n res[i] = safe_mod(safe_mul(res[i], base[i]) + str[i] + h);\n }\n }\n return res;\n }\n\n static void set_base() {\n random_number_generator_64 rnd;\n base.resize(n);\n for (int i = 0; i < n; ++i) {\n base[i] = rnd.get(0, (1UL << 61) - 1);\n }\n }\n\n private:\n size_t sz;\n std::vector<std::vector<u64>> base_pow;\n std::vector<std::vector<u64>> hash;\n\n public:\n static std::vector<u64> base;\n};\n\ntemplate <int n>\nstd::vector<std::uint64_t> rolling_hash<n>::base{12345, 10000000};\n\n} // namespace ebi\n\nnamespace ebi {\n\nstruct palindromic_tree {\nprivate:\n using i64 = std::int64_t;\n struct Node {\n std::map<char, int> edges;\n int len; // 回文の長さ\n int num = 0;\n int suffix_link = 0;\n std::vector<u64> hash;\n };\npublic:\n palindromic_tree(const std::string &str) : rh(str) {\n tree.resize(2);\n tree[0].len = -1;\n tree[1].len = 0;\n for(auto c: str) {\n add_char(c);\n }\n }\n\n // 末尾に x を追加し、末尾を使った最大の長さを返す。\n int add_char(char x) {\n s.push_back(x);\n int pos = s.size() - 1;\n int now = last_idx;\n while(true) {\n int len = tree[now].len;\n if(0 <= pos - len - 1 && s[pos - len - 1] == x) {\n break;\n }\n now = tree[now].suffix_link;\n }\n if(tree[now].edges.find(x) != tree[now].edges.end()) {\n last_idx = tree[now].edges[x];\n tree[last_idx].num++;\n return tree[last_idx].len;\n }\n last_idx = tree.size();\n tree[now].edges[x] = last_idx;\n Node node;\n node.len = tree[now].len + 2;\n node.num = 1;\n node.hash = rh.get_hash(pos+1-node.len, pos+1);\n if(node.len == 1) {\n node.suffix_link = 1;\n tree.emplace_back(node);\n return node.len;\n }\n while(true) {\n now = tree[now].suffix_link;\n int len = tree[now].len;\n if(0 <= pos - 1 - len && s[pos - 1 - len] == x) break;\n }\n node.suffix_link = tree[now].edges[x];\n tree.emplace_back(node);\n return node.len;\n }\n\n int count_types() {\n return (int)tree.size() - 2;\n }\n\n i64 count_num() {\n if(sum != -1) return sum;\n sum = 0;\n for(int i = tree.size() - 1; i >= 2; i--) {\n int suffix_link = tree[i].suffix_link;\n sum += tree[i].num;\n tree[suffix_link].num += tree[i].num;\n }\n return sum;\n }\n\n std::map<std::vector<u64>, i64> get_map() {\n std::map<std::vector<u64>, i64> map;\n for(int i = tree.size() - 1; i >= 2; i--) {\n int suffix_link = tree[i].suffix_link;\n map[tree[i].hash] = tree[i].num;\n tree[suffix_link].num += tree[i].num;\n }\n return map;\n }\nprivate:\n std::string s;\n std::vector<Node> tree;\n int last_idx = 1;\n i64 sum = -1;\n rolling_hash<2> rh;\n};\n\n}\n\nnamespace ebi {\n\nvoid main_() {\n rolling_hash<2>::set_base();\n std::string s,t;\n std::cin >> s >> t;\n palindromic_tree ptree_s(s), ptree_t(t);\n auto map_s = ptree_s.get_map();\n auto map_t = ptree_t.get_map();\n i64 ans = 0;\n for(auto [hash, cnt] : map_s) {\n ans += map_t[hash] * cnt;\n }\n std::cout << ans << '\\n';\n}\n\n} // namespace ebi\n\nint main() {\n std::cout << std::fixed << std::setprecision(15);\n std::cin.tie(nullptr);\n std::ios::sync_with_stdio(false);\n int t = 1;\n // std::cin >> t;\n while (t--) {\n ebi::main_();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 41020, "score_of_the_acc": -1.1724, "final_rank": 14 }, { "submission_id": "aoj_2292_7552876", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\ntemplate<typename T = char>\nstruct MultiPalindromicTree{\nprivate:\n struct Node{\n map<T, int> link;\n int link_rev = -1;\n int suffix_link; // same suffix maximum pal\n int len; // palindrome length\n vector<int> cnt; // freq of this\n pair<int, int> idx; // one of the start pos\n\n Node() = default;\n Node(int n, int len): len(len), cnt(n, 0), idx(-1, -1) {}\n\n int succ(const T &c) const {\n auto it = link.find(c);\n if(it == link.end()) return -1;\n return it->second;\n }\n };\n\n // find longest P s.t. c + P + c is pal\n int find_next_pal(int p, int k) const {\n int pos = int(seq[p].size()) - 1;\n while(true){\n int i = pos - 1 - nodes[k].len;\n if(i >= 0 && seq[p][i] == seq[p][pos]) return k;\n k = nodes[k].suffix_link;\n }\n }\n\npublic:\n\n int n;\n vector<Node> nodes;\n vector<vector<T>> seq;\n vector<vector<int>> ston;\n vector<int> suffix_max;\n\n\n MultiPalindromicTree(int n): n(n), seq(n), ston(n), suffix_max(n){\n nodes.emplace_back(n, -1); // odd node\n nodes.emplace_back(n, 0); // even node\n nodes[1].suffix_link = 0;\n }\n\n void add(int i, const T &c){\n seq[i].push_back(c);\n int k = find_next_pal(i, suffix_max[i]);\n\n // c + P + c is already exists\n if(int p = nodes[k].succ(c); p != -1){\n ++nodes[p].cnt[i];\n suffix_max[i] = p;\n ston[i].push_back(p);\n return;\n }\n\n // unique\n nodes[k].link[c] = suffix_max[i] = int(nodes.size());\n ston[i].push_back(suffix_max[i]);\n int len = nodes[k].len + 2;\n nodes.emplace_back(n, len);\n \n nodes.back().cnt[i] = 1;\n nodes.back().idx = {i, int(seq[i].size()) - len};\n nodes.back().link_rev = k;\n\n if(len == 1) nodes.back().suffix_link = 1;\n else nodes.back().suffix_link = \n nodes[find_next_pal(i, nodes[k].suffix_link)].link[c];\n }\n\n template<class Iiter>\n void add(int i, Iiter first, Iiter last){\n for(; first != last; ++first) add(i, *first);\n }\n \n template<class Container>\n void add(int i, const Container &c){\n add(i, begin(c), end(c));\n }\n\n // idx of suffix palindromes of node k\n vector<int> suffix_palindromes(int k) const {\n vector<int> ret;\n for(; nodes[k].len > 0; k = nodes[k].suffix_link){\n ret.push_back(k);\n }\n return ret;\n }\n\n // idx of substring palindromes of node k\n vector<int> substr_palindroms(int k) const {\n vector<int> ret, seen(nodes.size());\n queue<int> qu;\n qu.push(k); seen[k] = 1;\n for(; !qu.empty(); qu.pop()){\n int v = qu.front();\n ret.push_back(v);\n for(int u : {nodes[v].suffix_link, nodes[v].link_rev}){\n if(!seen[u]) qu.push(u), seen[u] = 1;\n }\n }\n return ret;\n }\n\n // not count \"\"\n int count() const { return int(nodes.size()) - 2; }\n\n vector<vector<int>> palindromes_frequency() const {\n int m = int(nodes.size());\n vector<vector<int>> freq(m, vector<int>(n));\n for(int i = m - 1; i >= 1; --i){\n for(int j = 0; j < n; ++j){\n freq[i][j] += nodes[i].cnt[j];\n freq[nodes[i].suffix_link][j] += freq[i][j];\n }\n }\n return freq;\n }\n\n const Node &operator[](int k) const { return nodes[k]; }\n};\n\n\nint main() {\n MultiPalindromicTree mpt(2);\n string s, t; cin >> s >> t;\n mpt.add(0, s);\n mpt.add(1, t);\n\n auto freq = mpt.palindromes_frequency();\n int64_t ans = 0;\n\n for(int i = 0; i < mpt.count(); ++i){\n ans += freq[i + 2][0] * 1ll * freq[i + 2][1];\n }\n\n cout << ans << \"\\n\";\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 27892, "score_of_the_acc": -0.5919, "final_rank": 8 }, { "submission_id": "aoj_2292_7539010", "code_snippet": "#line 1 \"zzz\\\\a.cpp\"\n#include <bits/stdc++.h>\nusing namespace std;\n\n#line 2 \"string\\\\RollingHash.cpp\"\n\nstruct RollingHash{\n static constexpr uint64_t mod = (1ull << 61) - 1;\n static constexpr uint64_t mask(int k){ return (1ull << k) - 1; }\n static constexpr uint64_t add(uint64_t a, uint64_t b){\n return ((a+=b) >= mod) ? (a - mod) : a;\n }\n static constexpr uint64_t mul(uint64_t a, uint64_t b){\n uint64_t au = a >> 31, ad = a & mask(31);\n uint64_t bu = b >> 31, bd = b & mask(31);\n int64_t mid = ad*bu + au*bd;\n uint64_t mu = mid >> 30, md = mid & mask(30);\n return au*bu*2 + mu + (md << 31) + ad*bd;\n }\n static constexpr uint64_t cal_mod(uint64_t x){\n uint64_t res = (x >> 61) + (x & mask(61));\n return (res>=mod) ? (res-mod) : res;\n }\n\n static inline uint64_t generate_base(){\n mt19937_64 mt(random_device{}());\n uniform_int_distribution<uint64_t> rand(1, mod-1);\n return rand(mt);\n }\n\n inline void expand(size_t n){\n if(size_t pn = pow_table.size(); pn < n+1){\n pow_table.resize(n + 1);\n for(size_t i = pn-1; i < n; ++i){\n pow_table[i+1] = cal_mod(mul(pow_table[i], base));\n }\n }\n }\n\n const uint64_t base;\n vector<uint64_t> pow_table;\n\n RollingHash(uint64_t base = generate_base()): base(base), pow_table(1,1) {}\n\n vector<uint64_t> build(const string &s) const {\n int n = int(s.size());\n vector<uint64_t> res(n + 1);\n for(int i = 0; i < n; ++i) res[i+1] = cal_mod(mul(res[i], base) + s[i]);\n return res;\n }\n\n template<typename T>\n vector<uint64_t> build(const vector<T> &s) const {\n int n = int(s.size());\n vector<uint64_t> res(n + 1);\n for(int i = 0; i < n; ++i) res[i+1] = cal_mod(mul(res[i], base) + s[i]);\n return res;\n }\n\n uint64_t query(const vector<uint64_t> &s, int l = 0, size_t n = string::npos){\n n = min(n, s.size()-1 - l);\n expand(n);\n return cal_mod(s[l+n] + mod*4 - mul(s[l], pow_table[n]));\n }\n\n uint64_t cat(uint64_t hl, uint64_t hr, size_t hr_len){\n expand(hr_len);\n return cal_mod(mul(hl, pow_table[hr_len]) + hr);\n }\n\n int lcp(const vector<uint64_t> &a, int la, int ra, const vector<uint64_t> &b, int lb, int rb){\n int n = min(ra - la, rb - lb);\n int l = 0, r = n+1;\n while(r-l > 1){\n int m = (l+r) >> 1;\n if(query(a, la, m) == query(b, lb, m)) l = m;\n else r = m;\n }\n return l;\n }\n};\n#line 5 \"zzz\\\\a.cpp\"\n\ntemplate<typename T = char>\nstruct PalindromicTree{\nprivate:\n struct Node{\n map<T, int> link;\n int suffix_link; // same suffix maximum pal\n int len; // palindrome length\n int cnt; // number of this\n int idx; // one of the start pos\n\n Node() = default;\n Node(int len, int cnt, int idx = -1): len(len), cnt(cnt), idx(idx) {}\n\n int succ(const T &c) const {\n auto it = link.find(c);\n if(it == link.end()) return -1;\n return it->second;\n }\n };\n\n // find longest P s.t. c + P + c is pal\n int find_next_pal(int k) const {\n int pos = int(seq.size()) - 1;\n while(true){\n int i = pos - 1 - nodes[k].len;\n if(i >= 0 && seq[i] == seq[pos]) return k;\n k = nodes[k].suffix_link;\n }\n }\n\npublic:\n\n vector<Node> nodes;\n vector<T> seq;\n vector<int> ston;\n int suffix_max;\n\n\n PalindromicTree(){\n nodes.emplace_back(-1, 0); // odd node\n nodes.emplace_back(0, 0); // even node\n nodes[1].suffix_link = 0;\n suffix_max = 0;\n }\n template<class Iiter>\n PalindromicTree(Iiter first, Iiter last): PalindromicTree() {\n add(first, last);\n }\n template<class Container>\n PalindromicTree(const Container &c): PalindromicTree() {\n add(c);\n }\n\n void add(const T &c){\n seq.push_back(c);\n int k = find_next_pal(suffix_max);\n\n // c + P + c is already exists\n if(int p = nodes[k].succ(c); p != -1){\n ++nodes[p].cnt;\n suffix_max = p;\n ston.push_back(p);\n return;\n }\n\n // unique\n nodes[k].link[c] = suffix_max = int(nodes.size());\n ston.push_back(suffix_max);\n int len = nodes[k].len + 2;\n nodes.emplace_back(len, 1, int(seq.size()) - len);\n\n if(len == 1) nodes.back().suffix_link = 1;\n else nodes.back().suffix_link = \n nodes[find_next_pal(nodes[k].suffix_link)].link[c];\n }\n\n template<class Iiter>\n void add(Iiter first, Iiter last){\n for(; first != last; ++first) add(*first);\n }\n \n template<class Container>\n void add(const Container &c){\n add(begin(c), end(c));\n }\n\n // seq[i] = node[k].last\n int get_node_idx(int i) const { return ston[i]; }\n\n // length of suffix palindromes of node k\n vector<int> suffix_palindromes(int k) const {\n vector<int> ret;\n for(; nodes[k].len > 0; k = nodes[k].suffix_link){\n ret.push_back(nodes[k].len);\n }\n return ret;\n }\n\n // length of suffix palindromes of seq\n vector<int> suffix_palindromes() const {\n return suffix_palindromes(suffix_max);\n }\n\n // not count \"\"\n int count() const { return int(nodes.size()) - 2; }\n\n vector<int> palindromes_frequency() const {\n int n = int(nodes.size());\n vector<int> freq(n);\n for(int i = n - 1; i >= 1; --i){\n freq[i] += nodes[i].cnt;\n freq[nodes[i].suffix_link] += freq[i];\n }\n return freq;\n }\n};\n\nint main() {\n string s, t; cin >> s >> t;\n PalindromicTree ps(s), pt(t);\n map<uint64_t, int> mp;\n RollingHash rh;\n auto hs = rh.build(s);\n auto ht = rh.build(t);\n auto fs = ps.palindromes_frequency();\n auto ft = pt.palindromes_frequency();\n\n for(int i = 0; i < ps.count(); ++i){\n int p = ps.nodes[i + 2].idx;\n int l = ps.nodes[i + 2].len;\n int c = fs[i + 2];\n mp[rh.query(hs, p, l)] += c;\n }\n int64_t ans = 0;\n\n for(int i = 0; i < pt.count(); ++i){\n int p = pt.nodes[i + 2].idx;\n int l = pt.nodes[i + 2].len;\n int64_t c = ft[i + 2];\n ans += mp[rh.query(ht, p, l)] * c;\n }\n\n cout << ans << \"\\n\";\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 24020, "score_of_the_acc": -0.5406, "final_rank": 6 }, { "submission_id": "aoj_2292_7135211", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, x) for(int i = 0; i < (x); i++)\n#define rng(i, l, r) for(int i = (l); i < (r); i++)\nusing vi = vector<int>;\n#define sz(x) (int)(x).size()\n\narray<vi, 2> manacher(string &s) {\n\tint n = sz(s);\n\tarray<vi, 2> p = {vi(n + 1), vi(n)};\n\trep(z, 2) for(int i = 0, l = 0, r = 0; i < n; i++) {\n\t\tint t = r - i + !z;\n\t\tif(i < r) p[z][i] = min(t, p[z][l + t]);\n\t\tint L = i - p[z][i], R = i + p[z][i] - !z;\n\t\twhile(L >= 1 and R + 1 < n and s[L - 1] == s[R + 1])\n\t\t\tp[z][i]++, L--, R++;\n\t\tif(R > r) l = L, r = R;\n\t}\n\treturn p;\n}\n\n\ntypedef uint64_t ull;\nstruct H {\n\tull x;\n\tH(ull x = 0) : x(x) {}\n\tH operator+(H o) { return x + o.x + (x + o.x < x); }\n\tH operator-(H o) { return *this + ~o.x; }\n\tH operator*(H o) {\n\t\tauto m = (__uint128_t)x * o.x;\n\t\treturn H((ull)m) + (ull)(m >> 64);\n\t}\n\tull get() const { return x + !~x; }\n\tbool operator==(H o) const { return get() == o.get(); }\n\tbool operator<(H o) const { return get() < o.get(); }\n};\n\nstatic const H C = (ll)1e11 + 3;\n\nstruct hash_interval {\n\tvector<H> ha, pw;\n\thash_interval(string &str) : ha(sz(str) + 1), pw(ha) {\n\t\tpw[0] = 1;\n\t\trep(i, sz(str))\n\t\t ha[i + 1] = ha[i] * C + str[i],\n\t\t pw[i + 1] = pw[i] * C;\n\t}\n\tH get(int a, int b) { return ha[b] - ha[a] * pw[b - a]; }\n};\n\nmap<ull, ll> palindromes_freq(string &s) {\n\tauto sm = manacher(s);\n\thash_interval sh(s);\n\tset<ull> st;\n\tmap<ull, ull> es;\n\tvector<set<ull>> nodes(sz(s) + 1);\n\tmap<ull, ll> cum;\n\trep(i, sz(s) + 1) {\n\t\tif(sm[0][i]) {\n\t\t\tint len = sm[0][i];\n\t\t\tauto ha = sh.get(i - len, i + len);\n\t\t\tcum[ha.get()]++;\n\t\t\tnodes[len * 2].insert(ha.get());\n\t\t\twhile(len > 1) {\n\t\t\t\tlen--;\n\t\t\t\tauto nx = sh.get(i - len, i + len);\n\t\t\t\tif(es.count({ha.get()})) break;\n\t\t\t\tes[ha.get()] = nx.get();\n\t\t\t\tha = nx;\n\t\t\t\tnodes[len * 2].insert(ha.get());\n\t\t\t}\n\t\t}\n\t}\n\n\trep(i, sz(s)) {\n\t\tsm[1][i]++;\n\t\tint len = sm[1][i];\n\t\tauto ha = sh.get(i - len + 1, i + len);\n\t\tcum[ha.get()]++;\n\t\tnodes[len * 2 - 1].insert(ha.get());\n\t\twhile(len > 1) {\n\t\t\tlen--;\n\t\t\tauto nx = sh.get(i - len + 1, i + len);\n\t\t\tif(es.count({ha.get()})) break;\n\t\t\tes[ha.get()] = nx.get();\n\t\t\tha = nx;\n\t\t\tnodes[len * 2 - 1].insert(ha.get());\n\t\t}\n\t}\n\n\tfor(int i = sz(s); i >= 0; --i) {\n\t\tfor(auto &v : nodes[i]) {\n\t\t\tif(es.count(v)) {\n\t\t\t\tcum[es[v]] += cum[v];\n\t\t\t}\n\t\t}\n\t}\n\treturn cum;\n}\n\nint main() {\n\tstring s, t;\n\tcin >> s >> t;\n\tauto s_freq = palindromes_freq(s);\n\tauto t_freq = palindromes_freq(t);\n\tll res = 0;\n\tfor(auto [key, cnt] : s_freq) res += cnt * t_freq[key];\n\tcout << res << endl;\n}", "accuracy": 1, "time_ms": 160, "memory_kb": 18904, "score_of_the_acc": -0.8298, "final_rank": 11 }, { "submission_id": "aoj_2292_7135210", "code_snippet": "#define _GLIBCXX_DEBUG\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, x) for(int i = 0; i < (x); i++)\n#define rng(i, l, r) for(int i = (l); i < (r); i++)\nusing vi = vector<int>;\n#define sz(x) (int)(x).size()\n\narray<vi, 2> manacher(string &s) {\n\tint n = sz(s);\n\tarray<vi, 2> p = {vi(n + 1), vi(n)};\n\trep(z, 2) for(int i = 0, l = 0, r = 0; i < n; i++) {\n\t\tint t = r - i + !z;\n\t\tif(i < r) p[z][i] = min(t, p[z][l + t]);\n\t\tint L = i - p[z][i], R = i + p[z][i] - !z;\n\t\twhile(L >= 1 and R + 1 < n and s[L - 1] == s[R + 1])\n\t\t\tp[z][i]++, L--, R++;\n\t\tif(R > r) l = L, r = R;\n\t}\n\treturn p;\n}\n\n\ntypedef uint64_t ull;\nstruct H {\n\tull x;\n\tH(ull x = 0) : x(x) {}\n\tH operator+(H o) { return x + o.x + (x + o.x < x); }\n\tH operator-(H o) { return *this + ~o.x; }\n\tH operator*(H o) {\n\t\tauto m = (__uint128_t)x * o.x;\n\t\treturn H((ull)m) + (ull)(m >> 64);\n\t}\n\tull get() const { return x + !~x; }\n\tbool operator==(H o) const { return get() == o.get(); }\n\tbool operator<(H o) const { return get() < o.get(); }\n};\n\nstatic const H C = (ll)1e11 + 3;\n\nstruct hash_interval {\n\tvector<H> ha, pw;\n\thash_interval(string &str) : ha(sz(str) + 1), pw(ha) {\n\t\tpw[0] = 1;\n\t\trep(i, sz(str))\n\t\t ha[i + 1] = ha[i] * C + str[i],\n\t\t pw[i + 1] = pw[i] * C;\n\t}\n\tH get(int a, int b) { return ha[b] - ha[a] * pw[b - a]; }\n};\n\nmap<ull, ll> palindromes_freq(string &s) {\n\tauto sm = manacher(s);\n\thash_interval sh(s);\n\tset<ull> st;\n\tmap<ull, ull> es;\n\tvector<set<ull>> nodes(sz(s) + 1);\n\tmap<ull, ll> cum;\n\trep(i, sz(s) + 1) {\n\t\tif(sm[0][i]) {\n\t\t\tint len = sm[0][i];\n\t\t\tauto ha = sh.get(i - len, i + len);\n\t\t\tcum[ha.get()]++;\n\t\t\tnodes[len * 2].insert(ha.get());\n\t\t\twhile(len > 1) {\n\t\t\t\tlen--;\n\t\t\t\tauto nx = sh.get(i - len, i + len);\n\t\t\t\tif(es.count({ha.get()})) break;\n\t\t\t\tes[ha.get()] = nx.get();\n\t\t\t\tha = nx;\n\t\t\t\tnodes[len * 2].insert(ha.get());\n\t\t\t}\n\t\t}\n\t}\n\n\trep(i, sz(s)) {\n\t\tsm[1][i]++;\n\t\tint len = sm[1][i];\n\t\tauto ha = sh.get(i - len + 1, i + len);\n\t\tcum[ha.get()]++;\n\t\tnodes[len * 2 - 1].insert(ha.get());\n\t\twhile(len > 1) {\n\t\t\tlen--;\n\t\t\tauto nx = sh.get(i - len + 1, i + len);\n\t\t\tif(es.count({ha.get()})) break;\n\t\t\tes[ha.get()] = nx.get();\n\t\t\tha = nx;\n\t\t\tnodes[len * 2 - 1].insert(ha.get());\n\t\t}\n\t}\n\n\tfor(int i = sz(s); i >= 0; --i) {\n\t\tfor(auto &v : nodes[i]) {\n\t\t\tif(es.count(v)) {\n\t\t\t\tcum[es[v]] += cum[v];\n\t\t\t}\n\t\t}\n\t}\n\treturn cum;\n}\n\nint main() {\n\tstring s, t;\n\tcin >> s >> t;\n\tauto s_freq = palindromes_freq(s);\n\tauto t_freq = palindromes_freq(t);\n\tll res = 0;\n\tfor(auto [key, cnt] : s_freq) res += cnt * t_freq[key];\n\tcout << res << endl;\n}", "accuracy": 1, "time_ms": 180, "memory_kb": 20084, "score_of_the_acc": -0.9355, "final_rank": 12 }, { "submission_id": "aoj_2292_7124559", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntemplate<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }\ntemplate<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }\n#define all(x) (x).begin(),(x).end()\n#define fi first\n#define se second\n#define mp make_pair\n#define si(x) int(x.size())\nconst int mod1=998244353,mod2=1000000007,MAX=300005,INF=1<<30;\n\n// Manacher https://snuke.hatenablog.com/entry/2014/12/02/235837\n\nvector<int> Manacher(string S){\n vector<int> R(si(S));\n int i = 0, j = 0;\n while (i < S.size()) {\n while (i-j >= 0 && i+j < S.size() && S[i-j] == S[i+j]) ++j;\n R[i] = j;\n int k = 1;\n while (i-k >= 0 && k+R[i-k] < j) R[i+k] = R[i-k], ++k;\n i += k; j -= k;\n }\n return R;\n}\n// 偶数長のも求めるために$を入れる必要がある\n\n//ロリハ\n\nstruct Rollinghash{\n string S;\n int n;\n int base1;\n int base2;\n vector<ll> h1,h2,ru1,ru2;\n \n void make(string &T,int ba1,int ba2){\n S=T;\n n=S.size();\n h1.assign(n+1,0);\n h2.assign(n+1,0);\n ru1.assign(n+1,0);\n ru2.assign(n+1,0);\n base1=ba1;\n base2=ba2;\n \n ru1[0]=1;\n ru2[0]=1;\n \n for(int i=1;i<=n;i++){\n h1[i]=h1[i-1]*base1+ll(S[i-1]-'$'+1);\n h1[i]%=mod1;\n \n h2[i]=h2[i-1]*base2+ll(S[i-1]-'$'+1);\n h2[i]%=mod2;\n \n ru1[i]=ru1[i-1]*base1%mod1;\n ru2[i]=ru2[i-1]*base2%mod2;\n }\n }\n \n pair<ll,ll> ha(int l,int r){\n pair<ll,ll> res;\n res.fi=(h1[r]-h1[l]*ru1[r-l]%mod1+mod1)%mod1;\n res.se=(h2[r]-h2[l]*ru2[r-l]%mod2+mod2)%mod2;\n \n return res;\n }//開区間\n};\n\n//強連結成分分解\n\nint V,cmp[MAX];\nvector<int> G[MAX],rG[MAX],vs;//vsがトポソの逆順になってる\nbool used[MAX];\n\nint get_id(int i,bool f){\n return 2*i+f;\n}\n\nvoid add_edge(int from,int to){\n G[from].push_back(to);\n rG[to].push_back(from);\n}\n\nvoid either(int i,bool f,int j,bool g){\n add_edge(get_id(i,!f),get_id(j,g));\n add_edge(get_id(j,!g),get_id(i,f));\n //add_edge(2*i+(!f),2*j+g);\n //add_edge(2*j+(!g),2*i+f);\n}\n\nvoid imply(int i,bool f,int j,bool g){\n either(i,!f,j,g);\n}\n//iがfならばjがg\n\nvoid must(int i,bool f){\n either(i,f,i,f);\n}\n\nvoid DFS(int v){\n used[v]=1;\n for(int i=0;i<G[v].size();i++){\n if(used[G[v][i]]==0) DFS(G[v][i]);\n }\n vs.push_back(v);\n}\n\nvoid rDFS(int v,int k){\n used[v]=1;\n cmp[v]=k;\n for(int i=0;i<rG[v].size();i++){\n if(used[rG[v][i]]==0) rDFS(rG[v][i],k);\n }\n}\n\nint scc(){\n memset(used,0,sizeof(used));\n vs.clear();\n for(int v=0;v<V;v++){\n if(used[v]==0) DFS(v);\n }\n \n memset(used,0,sizeof(used));\n int k=0;\n for(int i=vs.size()-1;i>=0;i--){\n if(used[vs[i]]==0) rDFS(vs[i],k++);\n }\n return k;\n}\n\nll dp[MAX];\n\nvoid init(int sz){\n V=sz;\n for(int i=0;i<V;i++){\n dp[i]=0;\n cmp[i]=0;\n G[i].clear();\n rG[i].clear();\n used[i]=false;\n }\n vs.clear();\n}\n\nint main(){\n \n std::ifstream in(\"text.txt\");\n std::cin.rdbuf(in.rdbuf());\n cin.tie(0);\n ios::sync_with_stdio(false);\n \n string S,T;cin>>S>>T;\n string SS,TT;\n SS+='$';TT+='$';\n for(char c:S){\n SS+=c;\n SS+='$';\n }\n for(char c:T){\n TT+=c;\n TT+='$';\n }\n S=SS;\n T=TT;\n \n ll ha1=103,ha2=109;\n \n map<pair<ll,ll>,ll> MA1,MA2;\n \n for(int q=0;q<2;q++){\n map<pair<ll,ll>,ll> dic;\n Rollinghash ro;\n ro.make(S,ha1,ha2);\n init(MAX);\n \n auto mana=Manacher(S);\n \n for(int i=0;i<si(S);i++){\n int s=(i-mana[i]+1);\n if(s%2==0) s++;\n pair<ll,ll> la=mp(-1,-1);\n for(int l=s;l<=i;l+=2){\n int r=i+(i-l)+1;\n auto re=ro.ha(l,r);\n if(dic.count(re)){\n if(l==s) dp[dic[re]]++;\n else{\n add_edge(dic[la],dic[re]);\n }\n break;\n }else{\n int sz=si(dic);\n dic[re]=sz;\n if(l==s) dp[sz]++;\n else{\n add_edge(dic[la],dic[re]);\n }\n la=re;\n }\n }\n }\n \n V=si(dic);\n \n scc();\n \n reverse(all(vs));\n \n for(int a:vs){\n for(int to:G[a]){\n dp[to]+=dp[a];\n }\n }\n \n for(auto a:dic){\n MA1[a.fi]+=dp[a.se];\n }\n \n swap(S,T);\n swap(MA1,MA2);\n }\n \n ll ans=0;\n \n for(auto a:MA1){\n if(MA2.count(a.fi)) ans+=a.se*MA2[a.fi];\n }\n \n cout<<ans<<endl;\n}", "accuracy": 1, "time_ms": 110, "memory_kb": 38324, "score_of_the_acc": -1.261, "final_rank": 15 }, { "submission_id": "aoj_2292_6934508", "code_snippet": "#include<bits/stdc++.h>\n#define rep(i,n) for (int i = 0; i < int(n); ++i)\n#define repp(i,n,m) for (int i = m; i < int(n); ++i)\n#define repb(i,n) for (int i = int(n)-1; i >= 0; --i)\n#define all(v) v.begin(),v.end()\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing P = pair<int, int>;\nusing PL = pair<long long, long long>;\nusing pdd = pair<long double, long double>;\nusing pil = pair<int,ll>;\nusing pli = pair<ll,int>;\nusing ppi = pair<P,int>;\nusing pip = pair<int,P>;\nconst int INF = 1001001007;\nconst long long mod1 = 1000000007LL;\nconst long long mod2 = 998244353LL;\nconst ll inf = 2e18;\nconst ld pi = 3.14159265358979323;\nconst ld eps = 1e-7;\ntemplate<typename T>void o(T a);\ntemplate<class T>istream &operator>>(istream &is,vector<T> &v){for(auto &e:v)is>>e;return is;}\ntemplate<class T>istream &operator>>(istream &is,vector<vector<T>> &v){for(auto &e:v)is>>e;return is;}\ntemplate<typename T>bool range(T a,T b,T x){return (a<=x&&x<b);}\ntemplate<typename T>bool rrange(T a,T b,T c,T d,T x,T y){return (range(a,c,x)&&range(b,d,y));}\ntemplate<typename T>void rev(vector<T> &v){reverse(v.begin(),v.end());}\nvoid revs(string &s) {reverse(s.begin(),s.end());}\ntemplate<typename T>void sor(vector<T> &v, int f=0){sort(v.begin(),v.end());if(f!=0) rev(v);}\ntemplate<typename T>bool chmin(T &a,const T &b){if(a>b){a=b;return true;}return false;}\ntemplate<typename T>bool chmax(T &a,const T &b){if(a<b){a=b;return true;}return false;}\ntemplate<typename T>void uniq(vector<T> &v){sor(v);v.erase(unique(v.begin(),v.end()),v.end());}\ntemplate<typename T>T cel(T a,T b){return (a+b-1)/b;}\ntemplate<typename T1, typename T2>void print(pair<T1,T2> a);\ntemplate<typename T>void print(vector<T> v);\ntemplate<typename T>void print(vector<vector<T>> v);\nvoid print(){ putchar(' '); }\nvoid print(bool a){ printf(\"%d\", a); }\nvoid print(int a){ printf(\"%d\", a); }\nvoid print(long a){ printf(\"%ld\", a); }\nvoid print(long long a){ printf(\"%lld\", a); }\nvoid print(char a){ printf(\"%c\", a); }\nvoid print(char a[]){ printf(\"%s\", a); }\nvoid print(const char a[]){ printf(\"%s\", a); }\nvoid print(long double a){ printf(\"%.15Lf\", a); }\nvoid print(const string& a){ for(auto&& i : a) print(i); }\ntemplate<class T> void print(const T& a){ cout << a; }\nint out(){ putchar('\\n'); return 0; }\ntemplate<class T> int out(const T& t){ print(t); putchar('\\n'); return 0; }\ntemplate<class Head, class... Tail> int out(const Head& head, const Tail&... tail){ print(head); putchar(' '); out(tail...); return 0; }\nvoid o(){cout<<\"!?\"<<endl;}\ntemplate<typename T>void o(T a){cout<<a<<endl;}\ntemplate<typename T1,typename T2>void print(pair<T1,T2> a){print(a.first);print(),print(a.second);}\ntemplate<typename T>void print(vector<T> v){for(auto ite=v.begin();ite!=v.end();){print(*ite);if(++ite!=v.end())print();}}\ntemplate<typename T>void print(vector<vector<T>> v){for(auto ite=v.begin();ite!=v.end();){print(*ite);if(++ite!=v.end())out();}}\nvoid yes(){out(\"Yes\");}\nvoid no (){out(\"No\");}\nvoid yn (bool t){if(t)yes();else no();}\ntemplate<typename T>void dame(bool t, T s){if(!t){out(s);exit(0);}}\nvector<int> dx = {0,1,0,-1,1,1,-1,-1};\nvector<int> dy = {1,0,-1,0,1,-1,-1,1};\nconst string ALP = \"ABCDEFGHIJKLMNOPQRSTUVWXYZ\";\nconst string alp = \"abcdefghijklmnopqrstuvwxyz\";\nconst string NUM = \"0123456789\";\n\nll gcd(ll a,ll b){return b?gcd(b,a%b):a;}\nll lcm(ll a,ll b){return a/gcd(a,b)*b;}\nll mpow(ll x,ll n,ll m){if(n==0)return 1LL;x%=m;ll a=mpow(x,n/2,m);a=a*a%m;return (n&1)?a*x%m:a;}\n\n\nvector<int> manacher(string &s){\n int n = s.size();\n vector<int> ans(n,0);\n int i = 0, len = 0;\n while (i < n){\n while (i - len >= 0 && i + len < n && s[i-len] == s[i+len]) len++;\n ans[i] = len;\n int k = 1;\n while (i - k >= 0 && k + ans[i-k] < len) ans[i+k] = ans[i-k], k++;\n i += k, len -= k;\n }\n return ans;\n}\n\nvector<int> palindrome_length(string s){\n string t = \"$\";\n for (char c : s) t += c, t += '$';\n vector<int> a = manacher(t);\n vector<int> ans(a.size()-2);\n rep(i,a.size()-2) ans[i] = a[i+1]-1;\n return ans;\n}\n\n#include<random>\nstruct RollingHash {\n using ull = unsigned long long;\n RollingHash(const string &s = \"\"){ build(s);}\n ull get(int l, int r){ return cal_mod(inner_hush[r] + POSITIVISER - mul_mod(inner_hush[l], pow_base[r-l]));}\n private:\n static constexpr ull MASK30 = (1UL << 30) - 1;\n static constexpr ull MASK31 = (1UL << 31) - 1;\n static constexpr ull MASK61 = (1UL << 61) - 1;\n static constexpr ull MOD = (1UL << 61) - 1;\n static constexpr ull POSITIVISER = MOD * 4;\n string str;\n int n;\n static ull BASE;\n static vector<ull> pow_base;\n static ull mul_mod(ull a, ull b){\n ull au = a >> 31, ad = a & MASK31;\n ull bu = b >> 31, bd = b & MASK31;\n ull mid = ad * bu + au * bd;\n ull midu = mid >> 30, midd = mid & MASK30;\n return (au * bu * 2 + midu + (midd << 31) + ad * bd);\n }\n static ull cal_mod(ull x){\n ull xu = x >> 61;\n ull xd = x & MASK61;\n ull res = xu + xd;\n if (res >= MOD) res -= MOD;\n return res;\n }\n vector<ull> inner_hush;\n void build(const string &s){\n if (BASE == 0) BASE = (1UL<<31) + (random_device()() & MASK31);\n str = s;\n n = s.size();\n int nlen = pow_base.size();\n if (nlen < n+1){\n pow_base.resize(n+1);\n for (int i = nlen; i <= n; i++){\n pow_base[i] = cal_mod(mul_mod(pow_base[i-1],BASE));\n }\n }\n inner_hush.resize(n+1);\n inner_hush[0] = 0;\n for (int i = 0; i < n; i++) inner_hush[i+1] = cal_mod(mul_mod(inner_hush[i],BASE) + s[i]);\n }\n};\nusing ull = unsigned long long;\null RollingHash::BASE = 0;\nvector<ull> RollingHash::pow_base = vector<ull>(1,1);\nusing roriha = RollingHash;\n\nvoid solve(){\n string s, t; cin >> s >> t;\n int n = s.size(), m = t.size();\n auto nma = palindrome_length(s);\n auto mma = palindrome_length(t);\n roriha a(s), b(t);\n map<ull,ll> nmp, mmp;\n priority_queue<pip> pque;\n rep(i,nma.size()){\n if (i % 2 == 0){\n int ce = i / 2;\n int le = ce - (nma[i]-1)/2;\n int ri = ce + (nma[i]-1)/2; \n ull hs = a.get(le,ri+1);\n if (nmp.find(hs) == nmp.end()){\n nmp[hs] = 1;\n pque.push(pip(nma[i],P(le,ri)));\n }\n else nmp[hs]++;\n }\n else {\n if (nma[i] == 0) continue;\n int le = (i - 1 - (nma[i] / 2 - 1) * 2) / 2;\n int ri = (i + 1 + (nma[i] / 2 - 1) * 2) / 2;\n ull hs = a.get(le,ri+1);\n if (nmp.find(hs) == nmp.end()){\n nmp[hs] = 1;\n pque.push(pip(nma[i],P(le,ri)));\n }\n else nmp[hs]++;\n }\n }\n while (!pque.empty()){\n auto p = pque.top(); pque.pop();\n if (p.first <= 2) continue;\n int le = p.second.first, ri = p.second.second;\n ull ihs = a.get(le,ri+1);\n le++, ri--;\n ull hs = a.get(le,ri+1);\n if (nmp.find(hs) == nmp.end()){\n nmp[hs] = nmp[ihs];\n pque.push(pip(p.first-2,P(le,ri)));\n }\n else nmp[hs] += nmp[ihs];\n }\n rep(i,mma.size()){\n if (i % 2 == 0){\n int ce = i / 2;\n int le = ce - (mma[i]-1)/2;\n int ri = ce + (mma[i]-1)/2;\n ull hs = b.get(le,ri+1);\n if (mmp.find(hs) == mmp.end()){\n mmp[hs] = 1;\n pque.push(pip(mma[i],P(le,ri)));\n }\n else mmp[hs]++;\n }\n else {\n if (mma[i] == 0) continue;\n int le = (i - 1 - (mma[i] / 2 - 1) * 2) / 2;\n int ri = (i + 1 + (mma[i] / 2 - 1) * 2) / 2;\n ull hs = b.get(le,ri+1);\n if (mmp.find(hs) == mmp.end()){\n mmp[hs] = 1;\n pque.push(pip(mma[i],P(le,ri)));\n }\n else mmp[hs]++;\n }\n }\n while (!pque.empty()){\n auto p = pque.top(); pque.pop();\n if (p.first <= 2) continue;\n int le = p.second.first, ri = p.second.second;\n ull ihs = b.get(le,ri+1);\n le++, ri--;\n ull hs = b.get(le,ri+1);\n if (mmp.find(hs) == mmp.end()){\n mmp[hs] = mmp[ihs];\n pque.push(pip(p.first-2,P(le,ri)));\n }\n else mmp[hs] += mmp[ihs];\n }\n ll ans = 0;\n for (auto p : nmp){\n if (mmp.find(p.first) == mmp.end()) continue;\n ans += p.second * mmp[p.first];\n }\n out(ans);\n}\n\nint main(){\n int t = 1; //cin >> t;\n while (t--) solve();\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 12176, "score_of_the_acc": -0.3448, "final_rank": 3 }, { "submission_id": "aoj_2292_6933363", "code_snippet": "#include<bits/stdc++.h>\n#define rep(i,n) for (int i = 0; i < int(n); ++i)\n#define repp(i,n,m) for (int i = m; i < int(n); ++i)\n#define repb(i,n) for (int i = int(n)-1; i >= 0; --i)\n#define all(v) v.begin(),v.end()\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing P = pair<int, int>;\nusing PL = pair<long long, long long>;\nusing pdd = pair<long double, long double>;\nusing pil = pair<int,ll>;\nusing pli = pair<ll,int>;\nusing ppi = pair<P,int>;\nusing pip = pair<int,P>;\nconst int INF = 1001001007;\nconst long long mod1 = 1000000007LL;\nconst long long mod2 = 998244353LL;\nconst ll inf = 2e18;\nconst ld pi = 3.14159265358979323;\nconst ld eps = 1e-7;\ntemplate<typename T>void o(T a);\ntemplate<class T>istream &operator>>(istream &is,vector<T> &v){for(auto &e:v)is>>e;return is;}\ntemplate<class T>istream &operator>>(istream &is,vector<vector<T>> &v){for(auto &e:v)is>>e;return is;}\ntemplate<typename T>bool range(T a,T b,T x){return (a<=x&&x<b);}\ntemplate<typename T>bool rrange(T a,T b,T c,T d,T x,T y){return (range(a,c,x)&&range(b,d,y));}\ntemplate<typename T>void rev(vector<T> &v){reverse(v.begin(),v.end());}\nvoid revs(string &s) {reverse(s.begin(),s.end());}\ntemplate<typename T>void sor(vector<T> &v, int f=0){sort(v.begin(),v.end());if(f!=0) rev(v);}\ntemplate<typename T>bool chmin(T &a,const T &b){if(a>b){a=b;return true;}return false;}\ntemplate<typename T>bool chmax(T &a,const T &b){if(a<b){a=b;return true;}return false;}\ntemplate<typename T>void uniq(vector<T> &v){sor(v);v.erase(unique(v.begin(),v.end()),v.end());}\ntemplate<typename T>T cel(T a,T b){return (a+b-1)/b;}\ntemplate<typename T1, typename T2>void print(pair<T1,T2> a);\ntemplate<typename T>void print(vector<T> v);\ntemplate<typename T>void print(vector<vector<T>> v);\nvoid print(){ putchar(' '); }\nvoid print(bool a){ printf(\"%d\", a); }\nvoid print(int a){ printf(\"%d\", a); }\nvoid print(long a){ printf(\"%ld\", a); }\nvoid print(long long a){ printf(\"%lld\", a); }\nvoid print(char a){ printf(\"%c\", a); }\nvoid print(char a[]){ printf(\"%s\", a); }\nvoid print(const char a[]){ printf(\"%s\", a); }\nvoid print(long double a){ printf(\"%.15Lf\", a); }\nvoid print(const string& a){ for(auto&& i : a) print(i); }\ntemplate<class T> void print(const T& a){ cout << a; }\nint out(){ putchar('\\n'); return 0; }\ntemplate<class T> int out(const T& t){ print(t); putchar('\\n'); return 0; }\ntemplate<class Head, class... Tail> int out(const Head& head, const Tail&... tail){ print(head); putchar(' '); out(tail...); return 0; }\nvoid o(){cout<<\"!?\"<<endl;}\ntemplate<typename T>void o(T a){cout<<a<<endl;}\ntemplate<typename T1,typename T2>void print(pair<T1,T2> a){print(a.first);print(),print(a.second);}\ntemplate<typename T>void print(vector<T> v){for(auto ite=v.begin();ite!=v.end();){print(*ite);if(++ite!=v.end())print();}}\ntemplate<typename T>void print(vector<vector<T>> v){for(auto ite=v.begin();ite!=v.end();){print(*ite);if(++ite!=v.end())out();}}\nvoid yes(){out(\"Yes\");}\nvoid no (){out(\"No\");}\nvoid yn (bool t){if(t)yes();else no();}\ntemplate<typename T>void dame(bool t, T s){if(!t){out(s);exit(0);}}\nvector<int> dx = {0,1,0,-1,1,1,-1,-1};\nvector<int> dy = {1,0,-1,0,1,-1,-1,1};\nconst string ALP = \"ABCDEFGHIJKLMNOPQRSTUVWXYZ\";\nconst string alp = \"abcdefghijklmnopqrstuvwxyz\";\nconst string NUM = \"0123456789\";\n\nll gcd(ll a,ll b){return b?gcd(b,a%b):a;}\nll lcm(ll a,ll b){return a/gcd(a,b)*b;}\nll mpow(ll x,ll n,ll m){if(n==0)return 1LL;x%=m;ll a=mpow(x,n/2,m);a=a*a%m;return (n&1)?a*x%m:a;}\n\n\nvector<int> manacher(string &s){\n int n = s.size();\n vector<int> ans(n,0);\n int i = 0, len = 0;\n while (i < n){\n while (i - len >= 0 && i + len < n && s[i-len] == s[i+len]) len++;\n ans[i] = len;\n int k = 1;\n while (i - k >= 0 && k + ans[i-k] < len) ans[i+k] = ans[i-k], k++;\n i += k, len -= k;\n }\n return ans;\n}\n\nvector<int> palindrome_length(string s){\n string t = \"$\";\n for (char c : s) t += c, t += '$';\n vector<int> a = manacher(t);\n vector<int> ans(a.size()-2);\n rep(i,a.size()-2) ans[i] = a[i+1]-1;\n return ans;\n}\n//using ulong = unsigned long long;\nconst ulong MASK30 = (1UL << 30) - 1;\nconst ulong MASK31 = (1UL << 31) - 1;\nconst ulong MOD = (1UL << 61) - 1;\nconst ulong MASK61 = MOD;\nulong CalcMod(ulong x);\n\n//a*b mod 2^61-1を返す関数(最後にModを取る)\nulong Mul(ulong a, ulong b)\n{\n ulong au = a >> 31;\n ulong ad = a & MASK31;\n ulong bu = b >> 31;\n ulong bd = b & MASK31;\n ulong mid = ad * bu + au * bd;\n ulong midu = mid >> 30;\n ulong midd = mid & MASK30;\n return (au * bu * 2 + midu + (midd << 31) + ad * bd);\n}\n\n//mod 2^61-1を計算する関数\nulong CalcMod(ulong x)\n{\n ulong xu = x >> 61;\n ulong xd = x & MASK61;\n ulong res = xu + xd;\n if (res >= MOD) res -= MOD;\n return res;\n}\n\nconst ulong Base = 294576;\nvector<ulong> bs;\n\nvoid solve(){\n string s, t; cin >> s >> t;\n int n = s.size(), m = t.size();\n //bs.resize(100000,1);\n //rep(i,100000-1) bs[i+1] = Mul(bs[i],base);\n //vector<ulong> nrui(n+1,0), mrui(m+1,0);\n vector<ulong> powMemo(100000,1);\n rep(i,100000-1) powMemo[i+1] = CalcMod(Mul(powMemo[i],Base));\n vector<ulong> hash1(n+1,0); //要素数n+1の配列を初期化\n for (int i = 0; i < n; i++) hash1[i + 1] = CalcMod(Mul(hash1[i], Base) + s[i]);\n vector<ulong> hash2(m+1,0); //要素数n+1の配列を初期化\n for (int i = 0; i < m; i++) hash2[i + 1] = CalcMod(Mul(hash2[i], Base) + t[i]);\n //rep(i,n) nrui[i+1] = CalcMod(nrui[i] + Mul(ulong(s[i] - 'a' + 1), bs[i]));\n //rep(i,m) mrui[i+1] = CalcMod(mrui[i] + Mul(ulong(t[i] - 'a' + 1), bs[i]));\n const ulong POSITIVIZER = MOD * 4;\n auto nget = [&](int l, int r){\n //return Mul(CalcMod(nrui[r] - nrui[l] + MOD),bs[100000-1-r]);\n return CalcMod(hash1[r] + POSITIVIZER - Mul(hash1[l], powMemo[r - l]));\n };\n auto mget = [&](int l, int r){\n //return Mul(CalcMod(nrui[r] - nrui[l] + MOD),bs[100000-1-r]);\n return CalcMod(hash2[r] + POSITIVIZER - Mul(hash2[l], powMemo[r - l]));\n };\n auto nma = palindrome_length(s);\n auto mma = palindrome_length(t);\n map<ulong,ll> nmp, mmp;\n priority_queue<pip> pque;\n rep(i,nma.size()){\n if (i % 2 == 0){\n int ce = i / 2;\n int le = ce - (nma[i]-1)/2;\n int ri = ce + (nma[i]-1)/2; \n ulong hs = nget(le,ri+1);\n if (nmp.find(hs) == nmp.end()){\n nmp[hs] = 1; //out(le,ri,hs);\n pque.push(pip(nma[i],P(le,ri)));\n }\n else nmp[hs]++;\n }\n else {\n if (nma[i] == 0) continue;\n int le = (i - 1 - (nma[i] / 2 - 1) * 2) / 2;\n int ri = (i + 1 + (nma[i] / 2 - 1) * 2) / 2;\n ulong hs = nget(le,ri+1);\n if (nmp.find(hs) == nmp.end()){\n nmp[hs] = 1;\n pque.push(pip(nma[i],P(le,ri)));\n }\n else nmp[hs]++;\n }\n }\n while (!pque.empty()){\n auto p = pque.top(); pque.pop();\n if (p.first <= 2) continue;\n int le = p.second.first, ri = p.second.second;\n ulong ihs = nget(le,ri+1);\n le++, ri--;\n ulong hs = nget(le,ri+1);\n if (nmp.find(hs) == nmp.end()){\n nmp[hs] = nmp[ihs]; //out(le,ri,hs);\n pque.push(pip(p.first-2,P(le,ri)));\n }\n else nmp[hs] += nmp[ihs];\n }\n rep(i,mma.size()){\n if (i % 2 == 0){\n int ce = i / 2;\n int le = ce - (mma[i]-1)/2;\n int ri = ce + (mma[i]-1)/2;\n ulong hs = mget(le,ri+1);\n if (mmp.find(hs) == mmp.end()){\n mmp[hs] = 1; //out(le,ri,hs);\n pque.push(pip(mma[i],P(le,ri)));\n }\n else mmp[hs]++;\n }\n else {\n if (mma[i] == 0) continue;\n int le = (i - 1 - (mma[i] / 2 - 1) * 2) / 2;\n int ri = (i + 1 + (mma[i] / 2 - 1) * 2) / 2;\n ulong hs = mget(le,ri+1);\n if (mmp.find(hs) == mmp.end()){\n mmp[hs] = 1;\n pque.push(pip(mma[i],P(le,ri)));\n }\n else mmp[hs]++;\n }\n }\n while (!pque.empty()){\n auto p = pque.top(); pque.pop();\n if (p.first <= 2) continue;\n int le = p.second.first, ri = p.second.second;\n ulong ihs = mget(le,ri+1);\n le++, ri--;\n ulong hs = mget(le,ri+1);\n if (mmp.find(hs) == mmp.end()){\n mmp[hs] = mmp[ihs];//out(le,ri,hs);\n pque.push(pip(p.first-2,P(le,ri)));\n }\n else mmp[hs] += mmp[ihs];\n }\n ll ans = 0;\n for (auto p : nmp){ //out(p.first,p.second);\n if (mmp.find(p.first) == mmp.end()) continue;\n ans += p.second * mmp[p.first]; //out(p.second,mmp[p.first]);\n }\n for (auto q : mmp){\n //out(q.first, q.second);\n }\n out(ans);\n}\n\nint main(){\n int t = 1; //cin >> t;\n while (t--) solve();\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 12320, "score_of_the_acc": -0.3493, "final_rank": 4 }, { "submission_id": "aoj_2292_6933350", "code_snippet": "#line 1 \"aoj.cpp\"\n#include<bits/stdc++.h>\n#define rep(i,n) for (int i = 0; i < int(n); ++i)\n#define repp(i,n,m) for (int i = m; i < int(n); ++i)\n#define repb(i,n) for (int i = int(n)-1; i >= 0; --i)\n#define all(v) v.begin(),v.end()\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing P = pair<int, int>;\nusing PL = pair<long long, long long>;\nusing pdd = pair<long double, long double>;\nusing pil = pair<int,ll>;\nusing pli = pair<ll,int>;\nusing ppi = pair<P,int>;\nusing pip = pair<int,P>;\nconst int INF = 1001001007;\nconst long long mod1 = 1000000007LL;\nconst long long mod2 = 998244353LL;\nconst ll inf = 2e18;\nconst ld pi = 3.14159265358979323;\nconst ld eps = 1e-7;\ntemplate<typename T>void o(T a);\ntemplate<class T>istream &operator>>(istream &is,vector<T> &v){for(auto &e:v)is>>e;return is;}\ntemplate<class T>istream &operator>>(istream &is,vector<vector<T>> &v){for(auto &e:v)is>>e;return is;}\ntemplate<typename T>bool range(T a,T b,T x){return (a<=x&&x<b);}\ntemplate<typename T>bool rrange(T a,T b,T c,T d,T x,T y){return (range(a,c,x)&&range(b,d,y));}\ntemplate<typename T>void rev(vector<T> &v){reverse(v.begin(),v.end());}\nvoid revs(string &s) {reverse(s.begin(),s.end());}\ntemplate<typename T>void sor(vector<T> &v, int f=0){sort(v.begin(),v.end());if(f!=0) rev(v);}\ntemplate<typename T>bool chmin(T &a,const T &b){if(a>b){a=b;return true;}return false;}\ntemplate<typename T>bool chmax(T &a,const T &b){if(a<b){a=b;return true;}return false;}\ntemplate<typename T>void uniq(vector<T> &v){sor(v);v.erase(unique(v.begin(),v.end()),v.end());}\ntemplate<typename T>T cel(T a,T b){return (a+b-1)/b;}\ntemplate<typename T1, typename T2>void print(pair<T1,T2> a);\ntemplate<typename T>void print(vector<T> v);\ntemplate<typename T>void print(vector<vector<T>> v);\nvoid print(){ putchar(' '); }\nvoid print(bool a){ printf(\"%d\", a); }\nvoid print(int a){ printf(\"%d\", a); }\nvoid print(long a){ printf(\"%ld\", a); }\nvoid print(long long a){ printf(\"%lld\", a); }\nvoid print(char a){ printf(\"%c\", a); }\nvoid print(char a[]){ printf(\"%s\", a); }\nvoid print(const char a[]){ printf(\"%s\", a); }\nvoid print(long double a){ printf(\"%.15Lf\", a); }\nvoid print(const string& a){ for(auto&& i : a) print(i); }\ntemplate<class T> void print(const T& a){ cout << a; }\nint out(){ putchar('\\n'); return 0; }\ntemplate<class T> int out(const T& t){ print(t); putchar('\\n'); return 0; }\ntemplate<class Head, class... Tail> int out(const Head& head, const Tail&... tail){ print(head); putchar(' '); out(tail...); return 0; }\nvoid o(){cout<<\"!?\"<<endl;}\ntemplate<typename T>void o(T a){cout<<a<<endl;}\ntemplate<typename T1,typename T2>void print(pair<T1,T2> a){print(a.first);print(),print(a.second);}\ntemplate<typename T>void print(vector<T> v){for(auto ite=v.begin();ite!=v.end();){print(*ite);if(++ite!=v.end())print();}}\ntemplate<typename T>void print(vector<vector<T>> v){for(auto ite=v.begin();ite!=v.end();){print(*ite);if(++ite!=v.end())out();}}\nvoid yes(){out(\"Yes\");}\nvoid no (){out(\"No\");}\nvoid yn (bool t){if(t)yes();else no();}\ntemplate<typename T>void dame(bool t, T s){if(!t){out(s);exit(0);}}\nvector<int> dx = {0,1,0,-1,1,1,-1,-1};\nvector<int> dy = {1,0,-1,0,1,-1,-1,1};\nconst string ALP = \"ABCDEFGHIJKLMNOPQRSTUVWXYZ\";\nconst string alp = \"abcdefghijklmnopqrstuvwxyz\";\nconst string NUM = \"0123456789\";\n\nll gcd(ll a,ll b){return b?gcd(b,a%b):a;}\nll lcm(ll a,ll b){return a/gcd(a,b)*b;}\nll mpow(ll x,ll n,ll m){if(n==0)return 1LL;x%=m;ll a=mpow(x,n/2,m);a=a*a%m;return (n&1)?a*x%m:a;}\n\n\nvector<int> manacher(string &s){\n int n = s.size();\n vector<int> ans(n,0);\n int i = 0, len = 0;\n while (i < n){\n while (i - len >= 0 && i + len < n && s[i-len] == s[i+len]) len++;\n ans[i] = len;\n int k = 1;\n while (i - k >= 0 && k + ans[i-k] < len) ans[i+k] = ans[i-k], k++;\n i += k, len -= k;\n }\n return ans;\n}\n\nvector<int> palindrome_length(string s){\n string t = \"$\";\n for (char c : s) t += c, t += '$';\n vector<int> a = manacher(t);\n vector<int> ans(a.size()-2);\n rep(i,a.size()-2) ans[i] = a[i+1]-1;\n return ans;\n}\n\n#line 2 \"modint.hpp\"\n// for prime m\ntemplate <long long m>\nstruct static_modint {\n using mint = static_modint;\n \n public:\n static constexpr int mod() { return m; }\n static_modint() : _v(0) {}\n template <class T>\n static_modint(T v) {\n long long x = (long long)(v % (long long)(umod()));\n if (x < 0) x += umod();\n _v = (unsigned int)(x);\n }\n unsigned int val() const { return _v; }\n\n mint& operator++() {\n _v++;\n if (_v == umod()) _v = 0;\n return *this;\n }\n mint& operator--() {\n if (_v == 0) _v = umod();\n _v--;\n return *this;\n }\n mint operator++(int) {\n mint result = *this;\n ++*this;\n return result;\n }\n mint operator--(int) {\n mint result = *this;\n --*this;\n return result;\n }\n\n mint& operator+=(const mint& rhs) {\n _v += rhs._v;\n if (_v >= umod()) _v -= umod();\n return *this;\n }\n mint& operator-=(const mint& rhs) {\n _v += mod() - rhs._v;\n if (_v >= umod()) _v -= umod();\n return *this;\n }\n mint& operator*=(const mint& rhs) {\n unsigned long long z = _v;\n z *= rhs._v;\n _v = (unsigned int)(z % umod());\n return *this;\n }\n mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }\n\n mint operator+() const { return *this; }\n mint operator-() const { return mint() - *this; }\n\n mint pow(long long n) const {\n assert(0 <= n);\n mint x = *this, r = 1;\n while (n) {\n if (n & 1) r *= x;\n x *= x;\n n >>= 1;\n }\n return r;\n }\n mint inv() const {\n assert(_v);\n return pow(umod() - 2);\n }\n\n mint inverse() const {return inv();}\n\n friend mint operator+(const mint& lhs, const mint& rhs) {\n return mint(lhs) += rhs;\n }\n friend mint operator-(const mint& lhs, const mint& rhs) {\n return mint(lhs) -= rhs;\n }\n friend mint operator*(const mint& lhs, const mint& rhs) {\n return mint(lhs) *= rhs;\n }\n friend mint operator/(const mint& lhs, const mint& rhs) {\n return mint(lhs) /= rhs;\n }\n friend bool operator==(const mint& lhs, const mint& rhs) {\n return lhs._v == rhs._v;\n }\n friend bool operator!=(const mint& lhs, const mint& rhs) {\n return lhs._v != rhs._v;\n }\n friend std::ostream &operator<<(std::ostream &os,const mint&p) {\n return os<<p.val();\n }\n friend std::istream &operator>>(std::istream &is, mint &a) {\n long long t;\n is>>t;\n a=mint(t);\n return (is);\n }\n \n private:\n unsigned int _v;\n static constexpr unsigned int umod() { return m; }\n};\n\nusing modint998244353 = static_modint<998244353>;\nusing modint1000000007 = static_modint<1000000007>;\n#line 95 \"aoj.cpp\"\nusing mint = modint998244353;\nconst mint base = 249134723;\nvector<mint> bs;\nvector<mint> ibs;\n\nvoid solve(){\n string s, t; cin >> s >> t;\n int n = s.size(), m = t.size();\n bs.resize(100000,1), ibs.resize(100000,1);\n rep(i,100000-1) bs[i+1] = bs[i] * base;\n ibs[100000-1] = bs[100000-1].inv();\n repb(i,100000-1) ibs[i] = ibs[i+1] * base;\n vector<mint> nrui(n+1,0), mrui(m+1,0);\n rep(i,n) nrui[i+1] = nrui[i] + mint(ll(s[i] - 'a' + 1)) * bs[i];\n rep(i,m) mrui[i+1] = mrui[i] + mint(ll(t[i] - 'a' + 1)) * bs[i];\n auto nget = [&](int l, int r){\n return (nrui[r] - nrui[l]) * ibs[l];\n };\n auto mget = [&](int l, int r){\n return (mrui[r] - mrui[l]) * ibs[l];\n };\n auto nma = palindrome_length(s);\n auto mma = palindrome_length(t);\n map<ll,ll> nmp, mmp;\n priority_queue<pip> pque;\n rep(i,nma.size()){\n if (i % 2 == 0){\n int ce = i / 2;\n int le = ce - (nma[i]-1)/2;\n int ri = ce + (nma[i]-1)/2; \n mint mhs = nget(le,ri+1);\n ll hs = mhs.val();\n if (nmp.find(hs) == nmp.end()){\n nmp[hs] = 1; //out(le,ri,hs);\n pque.push(pip(nma[i],P(le,ri)));\n }\n else nmp[hs]++;\n }\n else {\n if (nma[i] == 0) continue;\n int le = (i - 1 - (nma[i] / 2 - 1) * 2) / 2;\n int ri = (i + 1 + (nma[i] / 2 - 1) * 2) / 2;\n mint mhs = nget(le,ri+1);\n ll hs = mhs.val();\n if (nmp.find(hs) == nmp.end()){\n nmp[hs] = 1;\n pque.push(pip(nma[i],P(le,ri)));\n }\n else nmp[hs]++;\n }\n }\n while (!pque.empty()){\n auto p = pque.top(); pque.pop();\n if (p.first <= 2) continue;\n int le = p.second.first, ri = p.second.second;\n mint imhs = nget(le,ri+1);\n ll ihs = imhs.val();\n le++, ri--;\n mint mhs = nget(le,ri+1);\n ll hs = mhs.val();\n if (nmp.find(hs) == nmp.end()){\n nmp[hs] = nmp[ihs]; //out(le,ri,hs);\n pque.push(pip(p.first-2,P(le,ri)));\n }\n else nmp[hs] += nmp[ihs];\n }\n rep(i,mma.size()){\n if (i % 2 == 0){\n int ce = i / 2;\n int le = ce - (mma[i]-1)/2;\n int ri = ce + (mma[i]-1)/2;\n mint mhs = mget(le,ri+1);\n ll hs = mhs.val();\n if (mmp.find(hs) == mmp.end()){\n mmp[hs] = 1; //out(le,ri,hs);\n pque.push(pip(mma[i],P(le,ri)));\n }\n else mmp[hs]++;\n }\n else {\n if (mma[i] == 0) continue;\n int le = (i - 1 - (mma[i] / 2 - 1) * 2) / 2;\n int ri = (i + 1 + (mma[i] / 2 - 1) * 2) / 2;\n mint mhs = mget(le,ri+1);\n ll hs = mhs.val();\n if (mmp.find(hs) == mmp.end()){\n mmp[hs] = 1;\n pque.push(pip(mma[i],P(le,ri)));\n }\n else mmp[hs]++;\n }\n }\n while (!pque.empty()){\n auto p = pque.top(); pque.pop();\n if (p.first <= 2) continue;\n int le = p.second.first, ri = p.second.second;\n mint imhs = mget(le,ri+1);\n ll ihs = imhs.val();\n le++, ri--;\n mint mhs = mget(le,ri+1);\n ll hs = mhs.val();\n if (mmp.find(hs) == mmp.end()){\n mmp[hs] = mmp[ihs];//out(le,ri,hs);\n pque.push(pip(p.first-2,P(le,ri)));\n }\n else mmp[hs] += mmp[ihs];\n }\n ll ans = 0;\n for (auto p : nmp){ //out(p.first,p.second);\n if (mmp.find(p.first) == mmp.end()) continue;\n ans += p.second * mmp[p.first]; //out(p.second,mmp[p.first]);\n }\n for (auto q : mmp){\n //out(q.first, q.second);\n }\n out(ans);\n}\n\nint main(){\n int t = 1; //cin >> t;\n while (t--) solve();\n}", "accuracy": 0.15, "time_ms": 80, "memory_kb": 11872, "score_of_the_acc": -0.3354, "final_rank": 17 }, { "submission_id": "aoj_2292_6933347", "code_snippet": "#line 1 \"aoj.cpp\"\n#include<bits/stdc++.h>\n#define rep(i,n) for (int i = 0; i < int(n); ++i)\n#define repp(i,n,m) for (int i = m; i < int(n); ++i)\n#define repb(i,n) for (int i = int(n)-1; i >= 0; --i)\n#define all(v) v.begin(),v.end()\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing P = pair<int, int>;\nusing PL = pair<long long, long long>;\nusing pdd = pair<long double, long double>;\nusing pil = pair<int,ll>;\nusing pli = pair<ll,int>;\nusing ppi = pair<P,int>;\nusing pip = pair<int,P>;\nconst int INF = 1001001007;\nconst long long mod1 = 1000000007LL;\nconst long long mod2 = 998244353LL;\nconst ll inf = 2e18;\nconst ld pi = 3.14159265358979323;\nconst ld eps = 1e-7;\ntemplate<typename T>void o(T a);\ntemplate<class T>istream &operator>>(istream &is,vector<T> &v){for(auto &e:v)is>>e;return is;}\ntemplate<class T>istream &operator>>(istream &is,vector<vector<T>> &v){for(auto &e:v)is>>e;return is;}\ntemplate<typename T>bool range(T a,T b,T x){return (a<=x&&x<b);}\ntemplate<typename T>bool rrange(T a,T b,T c,T d,T x,T y){return (range(a,c,x)&&range(b,d,y));}\ntemplate<typename T>void rev(vector<T> &v){reverse(v.begin(),v.end());}\nvoid revs(string &s) {reverse(s.begin(),s.end());}\ntemplate<typename T>void sor(vector<T> &v, int f=0){sort(v.begin(),v.end());if(f!=0) rev(v);}\ntemplate<typename T>bool chmin(T &a,const T &b){if(a>b){a=b;return true;}return false;}\ntemplate<typename T>bool chmax(T &a,const T &b){if(a<b){a=b;return true;}return false;}\ntemplate<typename T>void uniq(vector<T> &v){sor(v);v.erase(unique(v.begin(),v.end()),v.end());}\ntemplate<typename T>T cel(T a,T b){return (a+b-1)/b;}\ntemplate<typename T1, typename T2>void print(pair<T1,T2> a);\ntemplate<typename T>void print(vector<T> v);\ntemplate<typename T>void print(vector<vector<T>> v);\nvoid print(){ putchar(' '); }\nvoid print(bool a){ printf(\"%d\", a); }\nvoid print(int a){ printf(\"%d\", a); }\nvoid print(long a){ printf(\"%ld\", a); }\nvoid print(long long a){ printf(\"%lld\", a); }\nvoid print(char a){ printf(\"%c\", a); }\nvoid print(char a[]){ printf(\"%s\", a); }\nvoid print(const char a[]){ printf(\"%s\", a); }\nvoid print(long double a){ printf(\"%.15Lf\", a); }\nvoid print(const string& a){ for(auto&& i : a) print(i); }\ntemplate<class T> void print(const T& a){ cout << a; }\nint out(){ putchar('\\n'); return 0; }\ntemplate<class T> int out(const T& t){ print(t); putchar('\\n'); return 0; }\ntemplate<class Head, class... Tail> int out(const Head& head, const Tail&... tail){ print(head); putchar(' '); out(tail...); return 0; }\nvoid o(){cout<<\"!?\"<<endl;}\ntemplate<typename T>void o(T a){cout<<a<<endl;}\ntemplate<typename T1,typename T2>void print(pair<T1,T2> a){print(a.first);print(),print(a.second);}\ntemplate<typename T>void print(vector<T> v){for(auto ite=v.begin();ite!=v.end();){print(*ite);if(++ite!=v.end())print();}}\ntemplate<typename T>void print(vector<vector<T>> v){for(auto ite=v.begin();ite!=v.end();){print(*ite);if(++ite!=v.end())out();}}\nvoid yes(){out(\"Yes\");}\nvoid no (){out(\"No\");}\nvoid yn (bool t){if(t)yes();else no();}\ntemplate<typename T>void dame(bool t, T s){if(!t){out(s);exit(0);}}\nvector<int> dx = {0,1,0,-1,1,1,-1,-1};\nvector<int> dy = {1,0,-1,0,1,-1,-1,1};\nconst string ALP = \"ABCDEFGHIJKLMNOPQRSTUVWXYZ\";\nconst string alp = \"abcdefghijklmnopqrstuvwxyz\";\nconst string NUM = \"0123456789\";\n\nll gcd(ll a,ll b){return b?gcd(b,a%b):a;}\nll lcm(ll a,ll b){return a/gcd(a,b)*b;}\nll mpow(ll x,ll n,ll m){if(n==0)return 1LL;x%=m;ll a=mpow(x,n/2,m);a=a*a%m;return (n&1)?a*x%m:a;}\n\n\nvector<int> manacher(string &s){\n int n = s.size();\n vector<int> ans(n,0);\n int i = 0, len = 0;\n while (i < n){\n while (i - len >= 0 && i + len < n && s[i-len] == s[i+len]) len++;\n ans[i] = len;\n int k = 1;\n while (i - k >= 0 && k + ans[i-k] < len) ans[i+k] = ans[i-k], k++;\n i += k, len -= k;\n }\n return ans;\n}\n\nvector<int> palindrome_length(string s){\n string t = \"$\";\n for (char c : s) t += c, t += '$';\n vector<int> a = manacher(t);\n vector<int> ans(a.size()-2);\n rep(i,a.size()-2) ans[i] = a[i+1]-1;\n return ans;\n}\n\n#line 2 \"modint.hpp\"\n// for prime m\ntemplate <long long m>\nstruct static_modint {\n using mint = static_modint;\n \n public:\n static constexpr int mod() { return m; }\n static_modint() : _v(0) {}\n template <class T>\n static_modint(T v) {\n long long x = (long long)(v % (long long)(umod()));\n if (x < 0) x += umod();\n _v = (unsigned int)(x);\n }\n unsigned int val() const { return _v; }\n\n mint& operator++() {\n _v++;\n if (_v == umod()) _v = 0;\n return *this;\n }\n mint& operator--() {\n if (_v == 0) _v = umod();\n _v--;\n return *this;\n }\n mint operator++(int) {\n mint result = *this;\n ++*this;\n return result;\n }\n mint operator--(int) {\n mint result = *this;\n --*this;\n return result;\n }\n\n mint& operator+=(const mint& rhs) {\n _v += rhs._v;\n if (_v >= umod()) _v -= umod();\n return *this;\n }\n mint& operator-=(const mint& rhs) {\n _v += mod() - rhs._v;\n if (_v >= umod()) _v -= umod();\n return *this;\n }\n mint& operator*=(const mint& rhs) {\n unsigned long long z = _v;\n z *= rhs._v;\n _v = (unsigned int)(z % umod());\n return *this;\n }\n mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }\n\n mint operator+() const { return *this; }\n mint operator-() const { return mint() - *this; }\n\n mint pow(long long n) const {\n assert(0 <= n);\n mint x = *this, r = 1;\n while (n) {\n if (n & 1) r *= x;\n x *= x;\n n >>= 1;\n }\n return r;\n }\n mint inv() const {\n assert(_v);\n return pow(umod() - 2);\n }\n\n mint inverse() const {return inv();}\n\n friend mint operator+(const mint& lhs, const mint& rhs) {\n return mint(lhs) += rhs;\n }\n friend mint operator-(const mint& lhs, const mint& rhs) {\n return mint(lhs) -= rhs;\n }\n friend mint operator*(const mint& lhs, const mint& rhs) {\n return mint(lhs) *= rhs;\n }\n friend mint operator/(const mint& lhs, const mint& rhs) {\n return mint(lhs) /= rhs;\n }\n friend bool operator==(const mint& lhs, const mint& rhs) {\n return lhs._v == rhs._v;\n }\n friend bool operator!=(const mint& lhs, const mint& rhs) {\n return lhs._v != rhs._v;\n }\n friend std::ostream &operator<<(std::ostream &os,const mint&p) {\n return os<<p.val();\n }\n friend std::istream &operator>>(std::istream &is, mint &a) {\n long long t;\n is>>t;\n a=mint(t);\n return (is);\n }\n \n private:\n unsigned int _v;\n static constexpr unsigned int umod() { return m; }\n};\n\nusing modint998244353 = static_modint<998244353>;\nusing modint1000000007 = static_modint<1000000007>;\n#line 95 \"aoj.cpp\"\nusing mint = modint998244353;\nconst mint base = 249134723;\nvector<mint> bs;\nvector<mint> ibs;\n\nvoid solve(){\n string s, t; cin >> s >> t;\n int n = s.size(), m = t.size();\n bs.resize(100000,1), ibs.resize(100000,1);\n rep(i,100000-1) bs[i+1] = bs[i] * base;\n ibs[100000-1] = bs[100000-1].inv();\n repb(i,100000-1) ibs[i] = ibs[i+1] * base;\n vector<mint> nrui(n+1,0), mrui(m+1,0);\n rep(i,n) nrui[i+1] = nrui[i] + mint(ll(s[i] - 'a' + 1)) * bs[i];\n rep(i,m) mrui[i+1] = mrui[i] + mint(ll(t[i] - 'a' + 1)) * bs[i];\n auto nget = [&](int l, int r){\n return (nrui[r] - nrui[l]) * ibs[l];\n };\n auto mget = [&](int l, int r){\n return (mrui[r] - mrui[l]) * ibs[l];\n };\n auto nma = palindrome_length(s);\n auto mma = palindrome_length(t);\n map<ll,ll> nmp, mmp;\n priority_queue<pip> pque;\n rep(i,nma.size()){\n if (i % 2 == 0){\n int ce = i / 2;\n int le = ce - (nma[i]-1)/2;\n int ri = ce + (nma[i]-1)/2; \n mint mhs = nget(le,ri+1);\n ll hs = mhs.val();\n if (nmp.find(hs) == nmp.end()){\n nmp[hs] = 1; //out(le,ri,hs);\n pque.push(pip(nma[i],P(le,ri)));\n }\n else nmp[hs]++;\n }\n else {\n int le = (i - (nma[i])) / 2;\n int ri = (i + (nma[i])) / 2;\n if (le == ri) continue;\n mint mhs = nget(le,ri+1);\n ll hs = mhs.val();\n if (nmp.find(hs) == nmp.end()){\n nmp[hs] = 1;\n pque.push(pip(nma[i],P(le,ri)));\n }\n else nmp[hs]++;\n }\n }\n while (!pque.empty()){\n auto p = pque.top(); pque.pop();\n if (p.first <= 2) continue;\n int le = p.second.first, ri = p.second.second;\n mint imhs = nget(le,ri+1);\n ll ihs = imhs.val();\n le++, ri--;\n mint mhs = nget(le,ri+1);\n ll hs = mhs.val();\n if (nmp.find(hs) == nmp.end()){\n nmp[hs] = nmp[ihs]; //out(le,ri,hs);\n pque.push(pip(p.first-2,P(le,ri)));\n }\n else nmp[hs] += nmp[ihs];\n }\n rep(i,mma.size()){\n if (i % 2 == 0){\n int ce = i / 2;\n int le = ce - (mma[i]-1)/2;\n int ri = ce + (mma[i]-1)/2;\n mint mhs = mget(le,ri+1);\n ll hs = mhs.val();\n if (mmp.find(hs) == mmp.end()){\n mmp[hs] = 1; //out(le,ri,hs);\n pque.push(pip(mma[i],P(le,ri)));\n }\n else mmp[hs]++;\n }\n else {\n int le = (i - (mma[i])) / 2;\n int ri = (i + (mma[i])) / 2;\n if (le == ri) continue;\n mint mhs = mget(le,ri+1);\n ll hs = mhs.val();\n if (mmp.find(hs) == mmp.end()){\n mmp[hs] = 1;\n pque.push(pip(mma[i],P(le,ri)));\n }\n else mmp[hs]++;\n }\n }\n while (!pque.empty()){\n auto p = pque.top(); pque.pop();\n if (p.first <= 2) continue;\n int le = p.second.first, ri = p.second.second;\n mint imhs = mget(le,ri+1);\n ll ihs = imhs.val();\n le++, ri--;\n mint mhs = mget(le,ri+1);\n ll hs = mhs.val();\n if (mmp.find(hs) == mmp.end()){\n mmp[hs] = mmp[ihs];//out(le,ri,hs);\n pque.push(pip(p.first-2,P(le,ri)));\n }\n else mmp[hs] += mmp[ihs];\n }\n ll ans = 0;\n for (auto p : nmp){ //out(p.first,p.second);\n if (mmp.find(p.first) == mmp.end()) continue;\n ans += p.second * mmp[p.first]; //out(p.second,mmp[p.first]);\n }\n for (auto q : mmp){\n //out(q.first, q.second);\n }\n out(ans);\n}\n\nint main(){\n int t = 1; //cin >> t;\n while (t--) solve();\n}", "accuracy": 0.06666666666666667, "time_ms": 60, "memory_kb": 11852, "score_of_the_acc": -0.2658, "final_rank": 20 }, { "submission_id": "aoj_2292_6933321", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nconst long long BASE = 123456789;\nconst long long M30 = ((long long) 1 << 30) - 1;\nconst long long M31 = ((long long) 1 << 31) - 1;\nconst long long MOD = ((long long) 1 << 61) - 1;\nunsigned long long modulo(unsigned long long x){\n unsigned long long xu = x >> 61;\n unsigned long long xd = x & MOD;\n unsigned long long res = xu + xd;\n if (res >= MOD){\n res -= MOD;\n }\n return res;\n}\nunsigned long long mul(unsigned long long a, unsigned long long b){\n unsigned long long au = a >> 31;\n unsigned long long ad = a & M31;\n unsigned long long bu = b >> 31;\n unsigned long long bd = b & M31;\n unsigned long long mid = au * bd + ad * bu;\n unsigned long long midu = mid >> 30;\n unsigned long long midd = mid & M30;\n return modulo(au * bu * 2 + midu + (midd << 31) + ad * bd);\n}\nstruct rolling_hash{\n vector<long long> POW, S;\n rolling_hash(string s){\n int N = s.size();\n POW.resize(N + 1);\n POW[0] = 1;\n for (int i = 0; i < N; i++){\n POW[i + 1] = mul(POW[i], BASE);\n }\n S.resize(N + 1);\n S[N] = 0;\n for (int i = N - 1; i >= 0; i--){\n S[i] = modulo(mul(S[i + 1], BASE) + s[i]);\n }\n }\n long long get(int L, int R){\n return modulo(S[L] + MOD - mul(S[R], POW[R - L]));\n }\n};\nvector<int> manacher(string &S){\n int N = S.size();\n vector<int> ans(N, 0);\n int i = 0, j = 0;\n while (i < N){\n while (i >= j && i + j < N && S[i - j] == S[i + j]){\n j++;\n }\n ans[i] = j;\n int k = 1;\n while (i >= k && i + k < N && k + ans[i - k] < j){\n ans[i + k] = ans[i - k];\n k++;\n }\n i += k;\n j -= k;\n }\n return ans;\n}\nunordered_map<unsigned long long, int> solve(string S){\n int N = S.size();\n string T = \"$\";\n for (int i = 0; i < N; i++){\n T += S[i];\n T += '$';\n }\n vector<int> A = manacher(T);\n rolling_hash RH(S);\n unordered_map<unsigned long long, int> mp;\n priority_queue<pair<int, int>> pq;\n for (int i = 1; i < N * 2; i++){\n int l = (i - A[i] + 2) / 2, r = (i + A[i] - 1) / 2;\n if (l < r){\n unsigned long long hash = RH.get(l, r);\n if (mp.count(hash) == 0){\n pq.push(make_pair(r - l, l));\n }\n mp[hash]++;\n }\n }\n while (!pq.empty()){\n int L = pq.top().second;\n int R = L + pq.top().first;\n pq.pop();\n unsigned long long hash = RH.get(L, R);\n L++;\n R--;\n if (L < R){\n unsigned long long hash2 = RH.get(L, R);\n if (mp.count(hash2) == 0){\n pq.push(make_pair(R - L, L));\n }\n mp[hash2] += mp[hash];\n }\n }\n return mp;\n}\nint main(){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n string S;\n cin >> S;\n string T;\n cin >> T;\n unordered_map<unsigned long long, int> A = solve(S);\n unordered_map<unsigned long long, int> B = solve(T);\n long long ans = 0;\n for (auto P : A){\n if (B.count(P.first) == 1){\n ans += (long long) P.second * B[P.first];\n }\n }\n cout << ans << endl;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 8848, "score_of_the_acc": -0.069, "final_rank": 1 } ]
aoj_2294_cpp
E: Entangled with Lottery ICPC で良い成績を収めるには修行が欠かせない.うさぎは ICPC で勝ちたいので,今日も修行をすることにした. 今日の修行は,あみだくじを利用して,未来を読む力をつけて運気を高めようというものである.もちろん直感に頼るだけでなく,綿密な確率計算も欠かせない. 今回考えるあみだくじは,長さ H + 1 センチメートルの縦棒 N 本からなる.うさぎは棒の上部を見て, N 本のうち 1 本を選ぶことになる.棒の下部には,左から P 本目の棒の場所にのみ「当たり」と書かれている.あみだくじにはいくつかの横棒が含まれる.横棒の配置に関して,以下の条件を考えよう. 各横棒は, a を 1 以上 H 以下の整数として,縦棒の上端から a センチメートルの高さにある. 各横棒は,隣り合った 2 本の縦棒のみを結ぶ. 同じ高さには複数の横棒は存在しない. うさぎはこれらの条件を満たすように M 本の横棒を引いた.あいにくうさぎは記憶力が良いので,当たりの位置や横棒の位置をすべて覚えてしまっており,あみだくじを楽しめない.そこで友達のねこにさらに横棒を追加してもらうことにした. まず,うさぎは当たりを狙って N 本の棒のうち 1 本を選ぶ.その後,ねこは以下の操作をちょうど K 回行う. 横棒を追加しても上で指定された条件を満たすような場所のうち 1 箇所を無作為に選ぶ.ここで,どの場所も等確率で選ばれるものとする.選んだ場所に横棒を追加する. そして,うさぎが選んだ棒が当たりであったかを判定する.棒の辿り方は通常のあみだくじと同様である (横棒に出会うたびに隣の縦棒に移る).うさぎは,可能な限り当たりとなる確率を高くしたい. Input H N P M K A 1 B 1 ... A M B M A i , B i (1 ≤ i ≤ M ) はうさぎが引いた横棒のうち i 番目のものが,縦棒の上端から A i センチメートルの高さにあり,左から B i 本目の縦棒と左から B i + 1 本目の縦棒を結ぶことを表す整数である. 2 ≤ H ≤ 500,2 ≤ N ≤ 100,1 ≤ P ≤ N ,1 ≤ M ≤ 100,1 ≤ K ≤ 100, M + K ≤ H ,1 ≤ A 1 < A 2 < ... < A M ≤ H ,1 ≤ B i ≤ N - 1 を満たす. Output 当たりとなる確率が最大となるようにうさぎが棒を選んだときの,当たりとなる確率を 1 行に出力せよ.10 -6 以下の絶対誤差が許容される. Sample Input 1 9 4 3 2 1 2 1 7 3 Sample Output 1 0.571428571 Sample Input 2 9 4 3 2 3 2 1 7 3 Sample Output 2 0.375661376
[ { "submission_id": "aoj_2294_10189840", "code_snippet": "// AOJ #2294\n// Entangled with Lottery 2025.2.4\n\n#include <bits/stdc++.h>\nusing namespace std;\nconst double EPS = 1e-14;\n\nint main(){\n ios::sync_with_stdio(0);\n cin.tie(0);\n\n int H, N, P, M, K;\n cin >> H >> N >> P >> M >> K;\n\n vector<int> used(H+1, -1);\n for(int i=0; i<M; i++){\n int A, B;\n cin >> A >> B;\n used[A] = B;\n }\n\n vector<int> freeCount(H+2, 0);\n for(int r = H; r >= 1; r--){\n freeCount[r] = freeCount[r+1] + (used[r] == -1 ? 1 : 0);\n }\n\n vector<vector<double>> dpBelow(N+1, vector<double>(K+1, 0.0)),\n dpCurrent(N+1, vector<double>(K+1, 0.0));\n\n for(int i=1; i<=N; i++){\n for(int k2=0; k2<=K; k2++){\n dpBelow[i][k2] = ( (i == P) ? 1.0 : 0.0 );\n }\n }\n\n for(int r=H; r>=1; r--){\n // 1. 既存横棒ありの場合\n if(used[r] != -1){\n int b = used[r]; \n for(int i=1; i<=N; i++){\n for(int k2=0; k2<=K; k2++){\n int nxt = i;\n if(i == b) nxt = b+1;\n else if(i == b+1) nxt = b;\n dpCurrent[i][k2] = dpBelow[nxt][k2];\n }\n }\n }\n else {\n for(int i=1; i<=N; i++){\n for(int k2=0; k2<=K; k2++){\n dpCurrent[i][k2] = 0.0;\n }\n }\n if(freeCount[r] == 0){\n for(int i=1; i<=N; i++){\n dpCurrent[i][0] = dpBelow[i][0];\n }\n }\n else {\n for(int i=1; i<=N; i++){\n for(int k2=0; k2<=K; k2++){\n double p_not = 1.0;\n double val_not = dpBelow[i][k2];\n\n double val_choose = 0.0;\n if(k2 > 0 && freeCount[r] >= k2){\n double p_choose = (double)k2 / (double)freeCount[r];\n p_not = 1.0 - p_choose;\n\n double sumSwap = 0.0;\n auto &A = dpBelow;\n\n if(i == 1){\n double a1 = A[1][k2-1];\n double a2 = A[2][k2-1];\n sumSwap = a2 + (N-2) * a1;\n }\n else if(i == N){\n double aN = A[N][k2-1];\n double aN_1 = A[N-1][k2-1];\n sumSwap = aN_1 + (N-2) * aN;\n }\n else {\n double ai = A[i][k2-1];\n double ai_1 = A[i-1][k2-1];\n double ai_p1 = A[i+1][k2-1];\n sumSwap = ai_1 + ai_p1 + (N-3)*ai;\n }\n val_choose = p_choose * (1.0/(N-1)) * sumSwap;\n }\n dpCurrent[i][k2] = p_not * val_not + val_choose;\n }\n }\n }\n }\n dpBelow.swap(dpCurrent);\n }\n double ans = 0.0;\n for(int i=1; i<=N; i++){\n ans = max(ans, dpBelow[i][K]);\n }\n cout << fixed << setprecision(9) << ans << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3680, "score_of_the_acc": -0.0037, "final_rank": 4 }, { "submission_id": "aoj_2294_9776894", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)\n#define rrep(i, a, b) for (ll i = (ll)(b)-1; i >= (ll)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n/**\n * @brief template\n */\n\nint main() {\n int H, N, P, M, K;\n cin >> H >> N >> P >> M >> K;\n P--;\n vector<int> X(H,-1);\n rep(i,0,M) {\n int A, B;\n cin >> A >> B;\n A--, B--;\n X[A] = B;\n }\n auto next = [&](int i, int j) -> int {\n if (i == j) return i+1;\n if (i == j+1) return i-1;\n return i;\n };\n vector DP(2, vector(N, vector<double>(H-M+1,0.0)));\n DP[H&1][P][0] = 1.0;\n rrep(i,0,H) {\n rep(j,0,N) {\n rep(k,0,H-M+1) {\n DP[i&1][j][k] = 0.0;\n }\n }\n if (X[i] != -1) {\n rep(j,0,N) {\n rep(k,0,H-M+1) {\n DP[i&1][next(j,X[i])][k] += DP[(i+1)&1][j][k];\n }\n }\n }\n else {\n rep(j,0,N) {\n rep(k,0,H-M+1) {\n if (DP[(i+1)&1][j][k] == 0.0) continue;\n DP[i&1][j][k] += DP[(i+1)&1][j][k];\n double Kiyo = (DP[(i+1)&1][j][k] * (double)(k+1) / (double)(H-M-k)) / (double(N-1));\n if (j == 0) {\n DP[i&1][j][k+1] += Kiyo * (double)(N-2);\n DP[i&1][j+1][k+1] += Kiyo;\n }\n else if (j == N-1) {\n DP[i&1][j][k+1] += Kiyo * (double)(N-2);\n DP[i&1][j-1][k+1] += Kiyo;\n }\n else {\n DP[i&1][j][k+1] += Kiyo * (double)(N-3);\n DP[i&1][j-1][k+1] += Kiyo;\n DP[i&1][j+1][k+1] += Kiyo;\n }\n }\n }\n }\n }\n double ANS = 0, SUM = 0.0;\n rep(i,0,N) {\n chmax(ANS, DP[0][i][K]);\n SUM += DP[0][i][K];\n }\n printf(\"%.12f\\n\", ANS / SUM);\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 4476, "score_of_the_acc": -0.0375, "final_rank": 10 }, { "submission_id": "aoj_2294_8332325", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\ndouble solve(int H, int N, int P, int M, int K, const vector<int>& A, const vector<int>& B) {\n\tvector<int> place(H, -1);\n\tfor (int i = 0; i < M; i++) {\n\t\tplace[A[i]] = B[i];\n\t}\n\tconst double inv = 1.0 / (N - 1);\n\tvector<vector<double> > dp(N, vector<double>(K + 1, 0.0)); // dp[position][# of drawn lines]\n\tdp[P][0] = 1.0;\n\tfor (int i = H - 1; i >= 0; i--) {\n\t\tvector<vector<double> > ndp(N, vector<double>(K + 1, 0.0));\n\t\tfor (int j = 0; j < N; j++) {\n\t\t\tfor (int k = 0; k <= K; k++) {\n\t\t\t\tif (place[i] != -1) {\n\t\t\t\t\tint last = (j == place[i] ? place[i] + 1 : j == place[i] + 1 ? place[i] : j);\n\t\t\t\t\tndp[j][k] += dp[last][k];\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\t// case that doesn't draw\n\t\t\t\t\tndp[j][k] += dp[j][k];\n\t\t\t\t\t// case that draws\n\t\t\t\t\tif (k >= 1) {\n\t\t\t\t\t\tdouble prob = ((N - 1) - (j == 0 || j == N - 1 ? 1 : 2)) * inv;\n\t\t\t\t\t\tndp[j][k] += dp[j][k - 1] * prob;\n\t\t\t\t\t\tif (j != 0) {\n\t\t\t\t\t\t\tndp[j][k] += dp[j - 1][k - 1] * inv;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (j != N - 1) {\n\t\t\t\t\t\t\tndp[j][k] += dp[j + 1][k - 1] * inv;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tdp = ndp;\n\t}\n\tdouble ans = 0.0, sum = 0.0;\n\tfor (int i = 0; i < N; i++) {\n\t\tsum += dp[i][K];\n\t\tans = max(ans, dp[i][K]);\n\t}\n\treturn ans / sum;\n}\n\nint main() {\n\tint H, N, P, M, K;\n\tcin >> H >> N >> P >> M >> K;\n\tP -= 1;\n\tvector<int> A(M), B(M);\n\tfor (int i = 0; i < M; i++) {\n\t\tcin >> A[i] >> B[i];\n\t\tA[i] -= 1;\n\t\tB[i] -= 1;\n\t}\n\tdouble ans = solve(H, N, P, M, K, A, B);\n\tcout.precision(15);\n\tcout << fixed << ans << endl;\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3744, "score_of_the_acc": -0.0045, "final_rank": 6 }, { "submission_id": "aoj_2294_7166647", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(10);\n int H, N, P, M, K;\n cin >> H >> N >> P >> M >> K;\n vector<int> line(H, -1);\n for (int i = 0; i < M; i++) {\n int A, B;\n cin >> A >> B;\n line[--A] = --B;\n }\n vector<vector<double>> dp(N, vector<double>(K + 1, 0)), ndp(N, vector<double>(K + 1, 0));\n dp[--P][0] = 1;\n for (int k = H - 1; k >= 0; k--) {\n for (int i = 0; i < N; i++) {\n for (int j = 0; j <= K; j++) {\n double& val = dp[i][j];\n if (val == 0) continue;\n if (line[k] != -1) {\n if (i == line[k])\n ndp[i + 1][j] += val;\n else if (i == line[k] + 1)\n ndp[i - 1][j] += val;\n else\n ndp[i][j] += val;\n } else {\n double p = 1.0 * (K - j) / (k + 1 - M);\n ndp[i][j] += val * (1 - p);\n if (j < K) {\n double add = val * p / (N - 1);\n ndp[i][j + 1] += add * (N - 3);\n if (i + 1 < N)\n ndp[i + 1][j + 1] += add;\n else\n ndp[i][j + 1] += add;\n if (i - 1 >= 0)\n ndp[i - 1][j + 1] += add;\n else\n ndp[i][j + 1] += add;\n }\n }\n val = 0;\n }\n }\n if (line[k] != -1) M--;\n swap(dp, ndp);\n }\n\n double ans = 0;\n for (int i = 0; i < N; i++) ans = max(ans, dp[i][K]);\n cout << ans << '\\n';\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3676, "score_of_the_acc": -0.0036, "final_rank": 3 }, { "submission_id": "aoj_2294_6698102", "code_snippet": "#include <bits/stdc++.h>\n\n#ifdef _MSC_VER\n# include <intrin.h>\n#else\n# include <x86intrin.h>\n#endif\n\n#include <limits>\n#include <type_traits>\n\nnamespace suisen {\n// ! utility\ntemplate <typename ...Types>\nusing constraints_t = std::enable_if_t<std::conjunction_v<Types...>, std::nullptr_t>;\ntemplate <bool cond_v, typename Then, typename OrElse>\nconstexpr decltype(auto) constexpr_if(Then&& then, OrElse&& or_else) {\n if constexpr (cond_v) {\n return std::forward<Then>(then);\n } else {\n return std::forward<OrElse>(or_else);\n }\n}\n\n// ! function\ntemplate <typename ReturnType, typename Callable, typename ...Args>\nusing is_same_as_invoke_result = std::is_same<std::invoke_result_t<Callable, Args...>, ReturnType>;\ntemplate <typename F, typename T>\nusing is_uni_op = is_same_as_invoke_result<T, F, T>;\ntemplate <typename F, typename T>\nusing is_bin_op = is_same_as_invoke_result<T, F, T, T>;\n\ntemplate <typename Comparator, typename T>\nusing is_comparator = std::is_same<std::invoke_result_t<Comparator, T, T>, bool>;\n\n// ! integral\ntemplate <typename T, typename = constraints_t<std::is_integral<T>>>\nconstexpr int bit_num = std::numeric_limits<std::make_unsigned_t<T>>::digits;\ntemplate <typename T, unsigned int n>\nstruct is_nbit { static constexpr bool value = bit_num<T> == n; };\ntemplate <typename T, unsigned int n>\nstatic constexpr bool is_nbit_v = is_nbit<T, n>::value;\n\n// ?\ntemplate <typename T>\nstruct safely_multipliable {};\ntemplate <>\nstruct safely_multipliable<int> { using type = long long; };\ntemplate <>\nstruct safely_multipliable<long long> { using type = __int128_t; };\ntemplate <>\nstruct safely_multipliable<unsigned int> { using type = unsigned long long; };\ntemplate <>\nstruct safely_multipliable<unsigned long int> { using type = __uint128_t; };\ntemplate <>\nstruct safely_multipliable<unsigned long long> { using type = __uint128_t; };\ntemplate <>\nstruct safely_multipliable<float> { using type = float; };\ntemplate <>\nstruct safely_multipliable<double> { using type = double; };\ntemplate <>\nstruct safely_multipliable<long double> { using type = long double; };\ntemplate <typename T>\nusing safely_multipliable_t = typename safely_multipliable<T>::type;\n\ntemplate <typename T, typename = void>\nstruct rec_value_type {\n using type = T;\n};\ntemplate <typename T>\nstruct rec_value_type<T, std::void_t<typename T::value_type>> {\n using type = typename rec_value_type<typename T::value_type>::type;\n};\ntemplate <typename T>\nusing rec_value_type_t = typename rec_value_type<T>::type;\n\n} // namespace suisen\n\n// ! type aliases\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\n\ntemplate <typename T>\nusing pq_greater = std::priority_queue<T, std::vector<T>, std::greater<T>>;\ntemplate <typename T, typename U>\nusing umap = std::unordered_map<T, U>;\n\n// ! macros (capital: internal macro)\n#define OVERLOAD2(_1,_2,name,...) name\n#define OVERLOAD3(_1,_2,_3,name,...) name\n#define OVERLOAD4(_1,_2,_3,_4,name,...) name\n\n#define REP4(i,l,r,s) for(std::remove_reference_t<std::remove_const_t<decltype(r)>>i=(l);i<(r);i+=(s))\n#define REP3(i,l,r) REP4(i,l,r,1)\n#define REP2(i,n) REP3(i,0,n)\n#define REPINF3(i,l,s) for(std::remove_reference_t<std::remove_const_t<decltype(l)>>i=(l);;i+=(s))\n#define REPINF2(i,l) REPINF3(i,l,1)\n#define REPINF1(i) REPINF2(i,0)\n#define RREP4(i,l,r,s) for(std::remove_reference_t<std::remove_const_t<decltype(r)>>i=(l)+fld((r)-(l)-1,s)*(s);i>=(l);i-=(s))\n#define RREP3(i,l,r) RREP4(i,l,r,1)\n#define RREP2(i,n) RREP3(i,0,n)\n\n#define rep(...) OVERLOAD4(__VA_ARGS__, REP4 , REP3 , REP2 )(__VA_ARGS__)\n#define rrep(...) OVERLOAD4(__VA_ARGS__, RREP4 , RREP3 , RREP2 )(__VA_ARGS__)\n#define repinf(...) OVERLOAD3(__VA_ARGS__, REPINF3, REPINF2, REPINF1)(__VA_ARGS__)\n\n#define CAT_I(a, b) a##b\n#define CAT(a, b) CAT_I(a, b)\n#define UNIQVAR(tag) CAT(tag, __LINE__)\n#define loop(n) for (std::remove_reference_t<std::remove_const_t<decltype(n)>> UNIQVAR(loop_variable) = n; UNIQVAR(loop_variable) --> 0;)\n\n#define all(iterable) std::begin(iterable), std::end(iterable)\n#define input(type, ...) type __VA_ARGS__; read(__VA_ARGS__)\n\n#ifdef LOCAL\n# define debug(...) debug_internal(#__VA_ARGS__, __VA_ARGS__)\n\ntemplate <class T, class... Args>\nvoid debug_internal(const char* s, T&& first, Args&&... args) {\n constexpr const char* prefix = \"[\\033[32mDEBUG\\033[m] \";\n constexpr const char* open_brakets = sizeof...(args) == 0 ? \"\" : \"(\";\n constexpr const char* close_brakets = sizeof...(args) == 0 ? \"\" : \")\";\n std::cerr << prefix << open_brakets << s << close_brakets << \": \" << open_brakets << std::forward<T>(first);\n ((std::cerr << \", \" << std::forward<Args>(args)), ...);\n std::cerr << close_brakets << \"\\n\";\n}\n\n#else\n# define debug(...) void(0)\n#endif\n\n// ! I/O utilities\n\n// __int128_t\nstd::ostream& operator<<(std::ostream& dest, __int128_t value) {\n std::ostream::sentry s(dest);\n if (s) {\n __uint128_t tmp = value < 0 ? -value : value;\n char buffer[128];\n char* d = std::end(buffer);\n do {\n --d;\n *d = \"0123456789\"[tmp % 10];\n tmp /= 10;\n } while (tmp != 0);\n if (value < 0) {\n --d;\n *d = '-';\n }\n int len = std::end(buffer) - d;\n if (dest.rdbuf()->sputn(d, len) != len) {\n dest.setstate(std::ios_base::badbit);\n }\n }\n return dest;\n}\n// __uint128_t\nstd::ostream& operator<<(std::ostream& dest, __uint128_t value) {\n std::ostream::sentry s(dest);\n if (s) {\n char buffer[128];\n char* d = std::end(buffer);\n do {\n --d;\n *d = \"0123456789\"[value % 10];\n value /= 10;\n } while (value != 0);\n int len = std::end(buffer) - d;\n if (dest.rdbuf()->sputn(d, len) != len) {\n dest.setstate(std::ios_base::badbit);\n }\n }\n return dest;\n}\n\n// pair\ntemplate <typename T, typename U>\nstd::ostream& operator<<(std::ostream& out, const std::pair<T, U>& a) {\n return out << a.first << ' ' << a.second;\n}\n// tuple\ntemplate <unsigned int N = 0, typename ...Args>\nstd::ostream& operator<<(std::ostream& out, const std::tuple<Args...>& a) {\n if constexpr (N >= std::tuple_size_v<std::tuple<Args...>>) {\n return out;\n } else {\n out << std::get<N>(a);\n if constexpr (N + 1 < std::tuple_size_v<std::tuple<Args...>>) {\n out << ' ';\n }\n return operator<<<N + 1>(out, a);\n }\n}\n// vector\ntemplate <typename T>\nstd::ostream& operator<<(std::ostream& out, const std::vector<T>& a) {\n for (auto it = a.begin(); it != a.end();) {\n out << *it;\n if (++it != a.end()) out << ' ';\n }\n return out;\n}\n// array\ntemplate <typename T, size_t N>\nstd::ostream& operator<<(std::ostream& out, const std::array<T, N>& a) {\n for (auto it = a.begin(); it != a.end();) {\n out << *it;\n if (++it != a.end()) out << ' ';\n }\n return out;\n}\ninline void print() { std::cout << '\\n'; }\ntemplate <typename Head, typename... Tail>\ninline void print(const Head& head, const Tail &...tails) {\n std::cout << head;\n if (sizeof...(tails)) std::cout << ' ';\n print(tails...);\n}\ntemplate <typename Iterable>\nauto print_all(const Iterable& v, std::string sep = \" \", std::string end = \"\\n\") -> decltype(std::cout << *v.begin(), void()) {\n for (auto it = v.begin(); it != v.end();) {\n std::cout << *it;\n if (++it != v.end()) std::cout << sep;\n }\n std::cout << end;\n}\n\n__int128_t parse_i128(std::string& s) {\n __int128_t ret = 0;\n for (int i = 0; i < int(s.size()); i++) if ('0' <= s[i] and s[i] <= '9') ret = 10 * ret + s[i] - '0';\n if (s[0] == '-') ret = -ret;\n return ret;\n}\n__uint128_t parse_u128(std::string& s) {\n __uint128_t ret = 0;\n for (int i = 0; i < int(s.size()); i++) if ('0' <= s[i] and s[i] <= '9') ret = 10 * ret + s[i] - '0';\n return ret;\n}\n// __int128_t\nstd::istream& operator>>(std::istream& in, __int128_t& v) {\n std::string s;\n in >> s;\n v = parse_i128(s);\n return in;\n}\n// __uint128_t\nstd::istream& operator>>(std::istream& in, __uint128_t& v) {\n std::string s;\n in >> s;\n v = parse_u128(s);\n return in;\n}\n// pair\ntemplate <typename T, typename U>\nstd::istream& operator>>(std::istream& in, std::pair<T, U>& a) {\n return in >> a.first >> a.second;\n}\n// tuple\ntemplate <unsigned int N = 0, typename ...Args>\nstd::istream& operator>>(std::istream& in, std::tuple<Args...>& a) {\n if constexpr (N >= std::tuple_size_v<std::tuple<Args...>>) {\n return in;\n } else {\n return operator>><N + 1>(in >> std::get<N>(a), a);\n }\n}\n// vector\ntemplate <typename T>\nstd::istream& operator>>(std::istream& in, std::vector<T>& a) {\n for (auto it = a.begin(); it != a.end(); ++it) in >> *it;\n return in;\n}\n// array\ntemplate <typename T, size_t N>\nstd::istream& operator>>(std::istream& in, std::array<T, N>& a) {\n for (auto it = a.begin(); it != a.end(); ++it) in >> *it;\n return in;\n}\ntemplate <typename ...Args>\nvoid read(Args &...args) {\n (std::cin >> ... >> args);\n}\n\n// ! integral utilities\n\n// Returns pow(-1, n)\ntemplate <typename T>\nconstexpr inline int pow_m1(T n) {\n return -(n & 1) | 1;\n}\n// Returns pow(-1, n)\ntemplate <>\nconstexpr inline int pow_m1<bool>(bool n) {\n return -int(n) | 1;\n}\n\n// Returns floor(x / y)\ntemplate <typename T>\nconstexpr inline T fld(const T x, const T y) {\n return (x ^ y) >= 0 ? x / y : (x - (y + pow_m1(y >= 0))) / y;\n}\ntemplate <typename T>\nconstexpr inline T cld(const T x, const T y) {\n return (x ^ y) <= 0 ? x / y : (x + (y + pow_m1(y >= 0))) / y;\n}\n\ntemplate <typename T, suisen::constraints_t<suisen::is_nbit<T, 16>> = nullptr>\n__attribute__((target(\"popcnt\"))) constexpr inline int popcount(const T x) { return _mm_popcnt_u32(x); }\ntemplate <typename T, suisen::constraints_t<suisen::is_nbit<T, 32>> = nullptr>\n__attribute__((target(\"popcnt\"))) constexpr inline int popcount(const T x) { return _mm_popcnt_u32(x); }\ntemplate <typename T, suisen::constraints_t<suisen::is_nbit<T, 64>> = nullptr>\n__attribute__((target(\"popcnt\"))) constexpr inline int popcount(const T x) { return _mm_popcnt_u64(x); }\ntemplate <typename T, suisen::constraints_t<suisen::is_nbit<T, 16>> = nullptr>\nconstexpr inline int count_lz(const T x) { return x ? __builtin_clz(x) : suisen::bit_num<T>; }\ntemplate <typename T, suisen::constraints_t<suisen::is_nbit<T, 32>> = nullptr>\nconstexpr inline int count_lz(const T x) { return x ? __builtin_clz(x) : suisen::bit_num<T>; }\ntemplate <typename T, suisen::constraints_t<suisen::is_nbit<T, 64>> = nullptr>\nconstexpr inline int count_lz(const T x) { return x ? __builtin_clzll(x) : suisen::bit_num<T>; }\ntemplate <typename T, suisen::constraints_t<suisen::is_nbit<T, 16>> = nullptr>\nconstexpr inline int count_tz(const T x) { return x ? __builtin_ctz(x) : suisen::bit_num<T>; }\ntemplate <typename T, suisen::constraints_t<suisen::is_nbit<T, 32>> = nullptr>\nconstexpr inline int count_tz(const T x) { return x ? __builtin_ctz(x) : suisen::bit_num<T>; }\ntemplate <typename T, suisen::constraints_t<suisen::is_nbit<T, 64>> = nullptr>\nconstexpr inline int count_tz(const T x) { return x ? __builtin_ctzll(x) : suisen::bit_num<T>; }\ntemplate <typename T>\nconstexpr inline int floor_log2(const T x) { return suisen::bit_num<T> -1 - count_lz(x); }\ntemplate <typename T>\nconstexpr inline int ceil_log2(const T x) { return floor_log2(x) + ((x & -x) != x); }\ntemplate <typename T>\nconstexpr inline int kth_bit(const T x, const unsigned int k) { return (x >> k) & 1; }\ntemplate <typename T>\nconstexpr inline int parity(const T x) { return popcount(x) & 1; }\n\n// ! container\n\ntemplate <typename T, typename Comparator, suisen::constraints_t<suisen::is_comparator<Comparator, T>> = nullptr>\nauto priqueue_comp(const Comparator comparator) {\n return std::priority_queue<T, std::vector<T>, Comparator>(comparator);\n}\n\ntemplate <typename Iterable>\nauto isize(const Iterable& iterable) -> decltype(int(iterable.size())) {\n return iterable.size();\n}\n\ntemplate <typename T, typename Gen, suisen::constraints_t<suisen::is_same_as_invoke_result<T, Gen, int>> = nullptr>\nauto generate_vector(int n, Gen generator) {\n std::vector<T> v(n);\n for (int i = 0; i < n; ++i) v[i] = generator(i);\n return v;\n}\ntemplate <typename T>\nauto generate_range_vector(T l, T r) {\n return generate_vector(r - l, [l](int i) { return l + i; });\n}\ntemplate <typename T>\nauto generate_range_vector(T n) {\n return generate_range_vector(0, n);\n}\n\ntemplate <typename T>\nvoid sort_unique_erase(std::vector<T>& a) {\n std::sort(a.begin(), a.end());\n a.erase(std::unique(a.begin(), a.end()), a.end());\n}\n\ntemplate <typename InputIterator, typename BiConsumer>\nauto foreach_adjacent_values(InputIterator first, InputIterator last, BiConsumer f) -> decltype(f(*first++, *last), void()) {\n if (first != last) for (auto itr = first, itl = itr++; itr != last; itl = itr++) f(*itl, *itr);\n}\ntemplate <typename Container, typename BiConsumer>\nauto foreach_adjacent_values(Container c, BiConsumer f) -> decltype(c.begin(), c.end(), void()) {\n foreach_adjacent_values(c.begin(), c.end(), f);\n}\n\n// ! other utilities\n\n// x <- min(x, y). returns true iff `x` has chenged.\ntemplate <typename T>\ninline bool chmin(T& x, const T& y) {\n if (y >= x) return false;\n x = y;\n return true;\n}\n// x <- max(x, y). returns true iff `x` has chenged.\ntemplate <typename T>\ninline bool chmax(T& x, const T& y) {\n if (y <= x) return false;\n x = y;\n return true;\n}\n\ntemplate <typename T, std::enable_if_t<std::is_integral_v<T>, std::nullptr_t> = nullptr>\nstd::string bin(T val, int bit_num = -1) {\n std::string res;\n if (bit_num >= 0) {\n for (int bit = bit_num; bit-- > 0;) res += '0' + ((val >> bit) & 1);\n } else {\n for (; val; val >>= 1) res += '0' + (val & 1);\n std::reverse(res.begin(), res.end());\n }\n return res;\n}\n\ntemplate <typename T, std::enable_if_t<std::is_integral_v<T>, std::nullptr_t> = nullptr>\nstd::vector<T> digits_low_to_high(T val, T base = 10) {\n std::vector<T> res;\n for (; val; val /= base) res.push_back(val % base);\n if (res.empty()) res.push_back(T{ 0 });\n return res;\n}\ntemplate <typename T, std::enable_if_t<std::is_integral_v<T>, std::nullptr_t> = nullptr>\nstd::vector<T> digits_high_to_low(T val, T base = 10) {\n auto res = digits_low_to_high(val, base);\n std::reverse(res.begin(), res.end());\n return res;\n}\n\ntemplate <typename T>\nstd::string join(const std::vector<T>& v, const std::string& sep, const std::string& end) {\n std::ostringstream ss;\n for (auto it = v.begin(); it != v.end();) {\n ss << *it;\n if (++it != v.end()) ss << sep;\n }\n ss << end;\n return ss.str();\n}\n\nnamespace suisen {}\nusing namespace suisen;\nusing namespace std;\n\nstruct io_setup {\n io_setup(int precision = 20) {\n std::ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout << std::fixed << std::setprecision(precision);\n }\n} io_setup_ {};\n\n// ! code from here\n\nint main() {\n input(int, h, n, p, m, k);\n --p;\n\n vector<int> bar(h, -1);\n rep(i, m) {\n input(int, a, b);\n --a, --b;\n bar[a] = b;\n }\n\n vector dp(h + 1, vector(n, vector<double>(k + 2, 0)));\n dp[h][p][0] = 1;\n rrep(i, h) rep(pos, n) rep(use, k + 1) {\n if (bar[i] < 0) {\n dp[i][pos][use] += dp[i + 1][pos][use];\n int lc = pos > 0, rc = pos < n - 1, mc = (n - 1) - lc - rc;\n if (lc) {\n dp[i][pos - 1][use + 1] += dp[i + 1][pos][use] * (use + 1) / (n - 1) / (h - m - use);\n }\n if (rc) {\n dp[i][pos + 1][use + 1] += dp[i + 1][pos][use] * (use + 1) / (n - 1) / (h - m - use);\n }\n if (mc) {\n dp[i][pos][use + 1] += mc * dp[i + 1][pos][use] * (use + 1) / (n - 1) / (h - m - use);\n }\n } else {\n int npos;\n if (bar[i] == pos) {\n npos = pos + 1;\n } else if (bar[i] == pos - 1) {\n npos = pos - 1;\n } else {\n npos = pos;\n }\n dp[i][npos][use] += dp[i + 1][pos][use];\n }\n }\n\n vector<double> ans(n);\n rep(i, n) {\n ans[i] = dp[0][i][k];\n }\n debug(ans);\n\n double sum = accumulate(all(ans), 0);\n double val = *max_element(all(ans));\n debug(sum, val);\n print(val);\n return 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 45452, "score_of_the_acc": -0.5345, "final_rank": 13 }, { "submission_id": "aoj_2294_5943187", "code_snippet": "#pragma GCC optimize(\"Ofast\")\n#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <map>\n#include <queue>\n#include <cstdio>\n#include <ctime>\n#include <assert.h>\n#include <chrono>\n#include <random>\n#include <numeric>\n#include <set>\n#include <deque>\n#include <stack>\n#include <sstream>\n#include <utility>\n#include <cstring>\n#include <unordered_map>\n#include <unordered_set>\n#include <tuple>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned long long ull;\n\nmt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());\nll myRand(ll B) {\n return (ull)rng() % B;\n}\n\nint ami[502];\ndouble dp[2][102][502];\n\nint main(){\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n int h,n,p,m,K; cin >> h >> n >> p >> m >> K;\n p--;\n for(int i=0;i<m;i++){\n int a,b; cin >> a >> b;\n ami[a] = b;\n }\n int cr = 0;\n dp[cr][0][p] = 1.0;\n // 高さを選ぶ確率は最後にまとめて\n for(int i=h;i>0;i--){\n int nx = (1^cr);\n for(int j=0;j<=K;j++){\n for(int k=0;k<n;k++){\n dp[nx][j][k] = 0;\n }\n }\n for(int j=0;j<=K;j++){\n for(int k=0;k<n;k++){\n if(ami[i]){\n if(ami[i] == k){\n dp[nx][j][k-1] += dp[cr][j][k];\n }\n else if(ami[i] == k+1){\n dp[nx][j][k+1] += dp[cr][j][k];\n }\n else{\n dp[nx][j][k] += dp[cr][j][k];\n }\n }\n else{\n dp[nx][j][k] += dp[cr][j][k];\n if(j == K)continue;\n if(k == 0){\n dp[nx][j+1][1] += dp[cr][j][k]/(double)(n-1);\n dp[nx][j+1][0] += dp[cr][j][k]*(n-2)/(double)(n-1);\n }\n else if(k+1 == n){\n dp[nx][j+1][n-2] += dp[cr][j][k]/(double)(n-1);\n dp[nx][j+1][n-1] += dp[cr][j][k]*(n-2)/(double)(n-1);\n }\n else{\n dp[nx][j+1][k] += dp[cr][j][k]*(n-3)/(double)(n-1);\n dp[nx][j+1][k+1] += dp[cr][j][k]/(double)(n-1);\n dp[nx][j+1][k-1] += dp[cr][j][k]/(double)(n-1);\n }\n }\n }\n }\n cr = nx;\n }\n double res = 0;\n for(int i=0;i<n;i++){\n res = max(res, dp[cr][K][i]);\n }\n for(int j=1;j<=K;j++){\n res *= (K+1-j)/(double)(h-m-j+1);\n }\n printf(\"%.9f\\n\",res);\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 4360, "score_of_the_acc": -0.0121, "final_rank": 7 }, { "submission_id": "aoj_2294_5921393", "code_snippet": "#pragma GCC ta g(\"avx2\")\n#pragma GCC optimize(\"Ofast\")\n#pragma GCC optimize(\"unroll-loops\")\n#include <bits/stdc++.h>\nusing namespace std;\n#define DEBUG\n#ifdef DEBUG\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << '(' << p.first << ',' << p.second << ')';\n return os;\n}\ntemplate <class T> ostream &operator<<(ostream &os, const vector<T> &v) {\n os << '{';\n for(int i = 0; i < (int)v.size(); i++) {\n if(i) { os << ','; }\n os << v[i];\n }\n os << '}';\n return os;\n}\nvoid debugg() { cerr << endl; }\ntemplate <class T, class... Args>\nvoid debugg(const T &x, const Args &... args) {\n cerr << \" \" << x;\n debugg(args...);\n}\n#define debug(...) \\\n cerr << __LINE__ << \" [\" << #__VA_ARGS__ << \"]: \", debugg(__VA_ARGS__)\n#define dump(x) cerr << __LINE__ << \" \" << #x << \" = \" << (x) << endl\n#else\n#define debug(...) (void(0))\n#define dump(x) (void(0))\n#endif\nusing namespace std;\ntypedef long long ll;\ntypedef vector<ll> vl;\ntypedef vector<vl> vvl;\ntypedef vector<char> vc;\ntypedef vector<string> vs;\ntypedef vector<bool> vb;\ntypedef vector<double> vd;\ntypedef pair<ll,ll> P;\ntypedef pair<int,int> pii;\ntypedef vector<P> vpl;\ntypedef tuple<ll,ll,ll> tapu;\n#define rep(i,n) for(int i=0; i<(n); i++)\n#define REP(i,a,b) for(int i=(a); i<(b); i++)\n#define all(x) (x).begin(), (x).end()\n#define rall(x) (x).rbegin(), (x).rend()\nconst int inf = (1<<30)-1;\nconst ll linf = 1LL<<61;\nconst int MAX = 510000;\nint dy[8] = {0,1,0,-1,1,-1,-1,1};\nint dx[8] = {-1,0,1,0,1,-1,1,-1};\nconst double pi = acos(-1);\nconst double eps = 1e-7;\ntemplate<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){\n\tif(a>b){\n\t\ta = b; return true;\n\t}\n\telse return false;\n}\ntemplate<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){\n\tif(a<b){\n\t\ta = b; return true;\n\t}\n\telse return false;\n}\ntemplate<typename T> inline void print(T &a){\n int sz = a.size();\n for(auto itr = a.begin(); itr != a.end(); itr++){\n\t\tcout << *itr;\n sz--;\n if(sz) cout << \" \";\n\t}\n cout << \"\\n\";\n}\ntemplate<typename T1,typename T2> inline void print2(T1 a, T2 b){\n\tcout << a << \" \" << b << \"\\n\";\n}\ntemplate<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){\n\tcout << a << \" \" << b << \" \" << c << \"\\n\";\n}\nvoid mark() {cout << \"#\" << \"\\n\";}\nll pcount(ll x) {return __builtin_popcountll(x);}\n//#include <atcoder/maxflow>\n//using namespace atcoder;\n//const int mod = 1e9 + 7;\nconst int mod = 998244353;\n\ndouble dp[2][101][101];\n\nint main(){\n int h,n,p,m,k; cin >> h >> n >> p >> m >> k; p--;\n vector<int> x(h,-1);\n rep(i,m){\n int a,b; cin >> a >> b; a--; b--;\n x[a] = b;\n }\n reverse(all(x));\n dp[0][0][p] = 1;\n int K = h-m;\n rep(i,h){\n rep(j,k+1) rep(l,n) dp[1][j][l] = 0;\n if(x[i] == -1){\n rep(j,k+1) rep(l,n){\n dp[1][j][l] += dp[0][j][l] * (K-(k-j)) / K;\n dp[1][j+1][l] += dp[0][j][l] * (k-j) / K * (n-3+(l==0||l==n-1)) / (n-1); \n if(l>0) dp[1][j+1][l-1] += dp[0][j][l] * (k-j) / K / (n-1);\n if(l<n-1) dp[1][j+1][l+1] += dp[0][j][l] * (k-j) / K / (n-1);\n }\n K--;\n }else{\n rep(j,k+1) rep(l,n){\n if(x[i] == l) dp[1][j][l+1] += dp[0][j][l];\n else if(x[i] == l-1) dp[1][j][l-1] += dp[0][j][l];\n else dp[1][j][l] += dp[0][j][l];\n }\n }\n swap(dp[0],dp[1]);\n }\n double ans = 0;\n rep(i,n) chmax(ans, dp[0][k][i]);\n printf(\"%.10f\\n\",ans);\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3712, "score_of_the_acc": -0.0041, "final_rank": 5 }, { "submission_id": "aoj_2294_5347422", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nint main(){\n cout << fixed << setprecision(10);\n int H, N, P, M, K;\n cin >> H >> N >> P >> M >> K;\n P--;\n vector<int> A(M), B(M);\n for (int i = 0; i < M; i++){\n cin >> A[i] >> B[i];\n A[i]--;\n B[i]--;\n }\n vector<int> x(H, -1);\n for (int i = 0; i < M; i++){\n x[A[i]] = B[i];\n }\n vector<vector<double>> dp(K + 1, vector<double>(N, 0));\n dp[0][P] = 1;\n int cnt = H - M;\n for (int i = H - 1; i >= 0; i--){\n if (x[i] != -1){\n for (int j = 0; j <= K; j++){\n swap(dp[j][x[i]], dp[j][x[i] + 1]);\n }\n } else {\n vector<vector<double>> dp2(K + 1, vector<double>(N, 0));\n for (int j = 0; j <= K; j++){\n double p = (double) (K - j) / cnt;\n for (int k = 0; k < N; k++){\n dp2[j][k] += dp[j][k] * (1 - p);\n if (j < K){\n double q = p;\n if (k > 0){\n dp2[j + 1][k - 1] += dp[j][k] * p / (N - 1);\n q -= p / (N - 1);\n }\n if (k < N - 1){\n dp2[j + 1][k + 1] += dp[j][k] * p / (N - 1);\n q -= p / (N - 1);\n }\n dp2[j + 1][k] += dp[j][k] * q;\n }\n }\n }\n swap(dp, dp2);\n cnt--;\n }\n }\n double ans = 0;\n for (int i = 0; i < N; i++){\n ans = max(ans, dp[K][i]);\n }\n cout << ans << endl;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 3380, "score_of_the_acc": -0.04, "final_rank": 11 }, { "submission_id": "aoj_2294_5255025", "code_snippet": "#include <iostream>\n#include <iomanip>\n#include <algorithm>\n#include <vector>\n\nusing namespace std;\nusing ldouble = long double;\n\nvoid solve() {\n int h, n, s, m, k;\n cin >> h >> n >> s >> m >> k;\n\n vector<int> ls(h, -1);\n while (m--) {\n int x, y;\n cin >> x >> y;\n ls[--x] = --y;\n }\n\n auto dp = vector(k + 1, vector(n, 0.L));\n dp[0][--s] = 1;\n // 引いた横線の数、当たりの位置\n\n int x = 0; // 横線を引ける段の数\n\n reverse(ls.begin(), ls.end());\n for (auto py : ls) {\n // 下にある段数\n if (py != -1) {\n for (int i = 0; i <= k; ++i) {\n swap(dp[i][py], dp[i][py + 1]);\n }\n\n } else {\n ++x;\n auto ndp = vector(k + 1, vector(n, 0.L));\n\n for (int i = 0; i <= k; ++i) {\n // 線を引く確率\n auto prob = ldouble(i) / x;\n\n // 線を引かない場合\n for (int y = 0; y < n; ++y) {\n ndp[i][y] += dp[i][y] * (1 - prob);\n }\n\n if (i > 0) {\n // 線を引く場合\n for (int y = 0; y < n; ++y) {\n int stay = n - 1;\n\n if (y + 1 < n) {\n // (y,y+1)\n ndp[i][y] += dp[i - 1][y + 1] * prob / (n - 1);\n --stay;\n }\n if (y - 1 >= 0) {\n // (y-1,y)\n ndp[i][y] += dp[i - 1][y - 1] * prob / (n - 1);\n --stay;\n }\n\n ndp[i][y] += dp[i - 1][y] * prob * stay / (n - 1);\n }\n }\n }\n\n swap(dp, ndp);\n }\n }\n\n cout << *max_element(dp[k].begin(), dp[k].end()) << \"\\n\";\n}\n\nint main() {\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(10);\n\n solve();\n\n return 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3720, "score_of_the_acc": -0.0202, "final_rank": 8 }, { "submission_id": "aoj_2294_5249508", "code_snippet": "#include<iostream>\n#include<iomanip>\nusing namespace std;\nint H,N,P,M,K;\nint id[505];\ndouble dp[2][102][505];\nmain()\n{\n\tcin>>H>>N>>P>>M>>K;\n\tP--;\n\tfor(int i=0;i<M;i++)\n\t{\n\t\tint a,b;cin>>a>>b;\n\t\tid[a-1]=b;\n\t}\n\tint now=0;\n\tdp[now][0][P]=1;\n\tfor(int i=H;i--;)\n\t{\n\t\tint nxt=1-now;\n\t\tfor(int j=0;j<=K;j++)for(int k=0;k<N;k++)dp[nxt][j][k]=0;\n\t\tfor(int j=0;j<=K;j++)for(int k=0;k<N;k++)\n\t\t{\n\t\t\tif(id[i])\n\t\t\t{\n\t\t\t\tif(k==id[i]-1)dp[nxt][j][k+1]+=dp[now][j][k];\n\t\t\t\telse if(k==id[i])dp[nxt][j][k-1]+=dp[now][j][k];\n\t\t\t\telse dp[nxt][j][k]+=dp[now][j][k];\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tdp[nxt][j][k]+=dp[now][j][k];\n\t\t\t\tif(k==0)\n\t\t\t\t{\n\t\t\t\t\tdp[nxt][j+1][k]+=dp[now][j][k]*(N-2)/(N-1);\n\t\t\t\t\tdp[nxt][j+1][k+1]+=dp[now][j][k]/(N-1);\n\t\t\t\t}\n\t\t\t\telse if(k+1==N)\n\t\t\t\t{\n\t\t\t\t\tdp[nxt][j+1][k]+=dp[now][j][k]*(N-2)/(N-1);\n\t\t\t\t\tdp[nxt][j+1][k-1]+=dp[now][j][k]/(N-1);\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tdp[nxt][j+1][k]+=dp[now][j][k]*(N-3)/(N-1);\n\t\t\t\t\tdp[nxt][j+1][k-1]+=dp[now][j][k]/(N-1);\n\t\t\t\t\tdp[nxt][j+1][k+1]+=dp[now][j][k]/(N-1);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tnow=nxt;\n\t}\n\tdouble ans=0;\n\tfor(int i=0;i<N;i++)if(ans<dp[now][K][i])ans=dp[now][K][i];\n\tfor(int i=1;i<=K;i++)ans=ans*i/(H-M-i+1);\n\tcout<<fixed<<setprecision(16)<<ans<<endl;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 4084, "score_of_the_acc": -0.0327, "final_rank": 9 }, { "submission_id": "aoj_2294_4973196", "code_snippet": "#include \"iostream\"\n#include \"climits\"\n#include \"list\"\n#include \"queue\"\n#include \"stack\"\n#include \"set\"\n#include \"functional\"\n#include \"algorithm\"\n#include \"string\"\n#include \"map\"\n#include \"unordered_map\"\n#include \"unordered_set\"\n#include \"iomanip\"\n#include \"cmath\"\n#include \"random\"\n#include \"bitset\"\n#include \"cstdio\"\n#include \"numeric\"\n#include \"cassert\"\n#include \"ctime\"\n#include \"complex\"\n\nusing namespace std;\n\nconstexpr long long int MOD = 1000000007;\n//constexpr int MOD = 1000000007;\n//constexpr int MOD = 998244353;\n//constexpr long long int MOD = 998244353;\nconstexpr double EPS = 1e-12;\n\n//int N, M, K, T, H, W, L, R;\nlong long int N, M, K, T, H, W, L, R;\n\n\n\n\nint main() {\n\tios::sync_with_stdio(false);\n\tcin.tie(0);\n\n\tcin >> H >> N >> T >> M >> K;\n\tT--;\n\tvector<vector<vector<long double>>>dp(H + 1, vector<vector<long double>>(K+1,vector<long double>(N)));\n\tdp[H][K][T] = 1;\n\tvector<int>v(H, -1);\n\tvector<int>sum(H, 1);\n\twhile (M--) {\n\t\tcin >> L >> R;\n\t\tL--, R--;\n\t\tv[L] = R;\n\t\tsum[L]--;\n\t}\n\tfor (int i = 1; i < H; i++) {\n\t\tsum[i] += sum[i - 1];\n\t}\n\tif (N <= 2) {\n\t\tcout << 1 << endl;\n\t\treturn 0;\n\t}\n\n\tfor (int i = H - 1; i >= 0; i--) {\n\t\tif (v[i] == -1) {\n\t\t\tfor (int j = 0; j <= min(K, sum[i] + 0LL); j++) {\n\t\t\t\tlong double p = j;\n\t\t\t\tp /= sum[i];\n\t\t\t\tfor (int k = 0; k < N; k++) {\n\t\t\t\t\tif (j) {\n\t\t\t\t\t\tif (k == 0) {\n\t\t\t\t\t\t\tdp[i][j - 1][k + 1] += dp[i + 1][j][k] * p / (N - 1);\n\t\t\t\t\t\t\tdp[i][j - 1][k] += dp[i + 1][j][k] * p *(N - 2) / (N - 1);\n\t\t\t\t\t\t\tdp[i][j][k] += dp[i + 1][j][k] * (1 - p);\n\t\t\t\t\t\t}\n\t\t\t\t\t\telse if (k + 1 == N) {\n\t\t\t\t\t\t\tdp[i][j - 1][k - 1] += dp[i + 1][j][k] * p / (N - 1);\n\t\t\t\t\t\t\tdp[i][j - 1][k] += dp[i + 1][j][k] * p *(N - 2) / (N - 1);\n\t\t\t\t\t\t\tdp[i][j][k] += dp[i + 1][j][k] * (1 - p);\n\t\t\t\t\t\t}\n\t\t\t\t\t\telse {\n\t\t\t\t\t\t\tdp[i][j - 1][k - 1] += dp[i + 1][j][k] * p / (N - 1);\n\t\t\t\t\t\t\tdp[i][j - 1][k + 1] += dp[i + 1][j][k] * p / (N - 1);\n\t\t\t\t\t\t\tdp[i][j - 1][k] += dp[i + 1][j][k] * p *(N - 3) / (N - 1);\n\t\t\t\t\t\t\tdp[i][j][k] += dp[i + 1][j][k] * (1 - p);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\telse {\n\t\t\t\t\t\tdp[i][j][k] = dp[i + 1][j][k];\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\telse {\n\t\t\tfor (int j = 0; j <= K; j++) {\n\t\t\t\tdp[i][j] = dp[i + 1][j];\n\t\t\t\tswap(dp[i][j][v[i]], dp[i][j][v[i] + 1]);\n\t\t\t}\n\t\t}\n\t}\n\tcout <<setprecision(20)<< *max_element(dp.front().front().begin(), dp.front().front().end()) << endl;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 84516, "score_of_the_acc": -1.04, "final_rank": 18 }, { "submission_id": "aoj_2294_4972751", "code_snippet": "#include <bits/stdc++.h>\n//#include <atcoder/all>\nusing namespace std;\ntypedef long long ll;\ntypedef pair<ll, ll> l_l;\ntypedef pair<int, int> i_i;\ntemplate<class T>\ninline bool chmax(T &a, T b) {\n if(a < b) {\n a = b;\n return true;\n }\n return false;\n}\n\ntemplate<class T>\ninline bool chmin(T &a, T b) {\n if(a > b) {\n a = b;\n return true;\n }\n return false;\n}\n\nconst long long INF = 1e18;\n//const ll mod = 1000000007;\nll H, N, P, M, K;\nmap<ll, ll> mp;\nlong double Prev[505][505];\nlong double Next[505][505];\nlong double combination[505][505];\nll emp = 0;\nint main() {\n for(int i = 0; i <= 500; i++) {\n combination[i][0] = 1;\n for(int j = 1; j <= i; j++) {\n combination[i][j] = combination[i-1][j] + combination[i-1][j-1];\n //cerr << combination[i][j] << endl;\n }\n }\n cin >> H >> N >> P >> M >> K;\n P--;\n for(int i = 0; i < M; i++) {\n ll a, b;\n cin >> a >> b;\n a--;\n b--;\n mp[a] = b;\n }\n Prev[0][P] = 1;\n for(int h = H - 1; h >= 0; h--) {\n //cerr << \"---\" << h << \"---\" << endl;\n for(int i = 0; i <= 500; i++) {\n for(int j = 0; j <= 500; j++) {\n Next[i][j] = 0;\n }\n }\n if(mp.count(h)) {\n for(int i = 0; i <= K; i++) {\n for(int j = 0; j < N; j++) {\n Next[i][j] = Prev[i][j];\n }\n ll idx = mp[h];\n swap(Next[i][idx], Next[i][idx+1]);\n }\n } else {\n emp++;\n for(int i = 0; i <= K; i++) {\n for(int j = 0; j < N; j++) {\n Next[i][j] += Prev[i][j];\n }\n }\n for(int i = 0; i <= K; i++) {\n for(ll j = 0; j < N; j++) {\n Next[i+1][j] += Prev[i][j] * (N - 3) / (N - 1);\n Next[i+1][max(j-1, 0LL)] += Prev[i][j] / (N - 1);\n Next[i+1][min(j+1, N-1)] += Prev[i][j] / (N - 1);\n }\n }\n }\n swap(Prev, Next);\n /*\n for(int k = 0; k <= K; k++) {\n cerr << \"-\" << k << \"-\" << endl;\n for(int i = 0; i < N; i++) cerr << Prev[k][i] << \" \";\n cerr << endl;\n }\n */\n }\n long double ans = 0;\n for(int i = 0; i < N; i++) {\n chmax(ans, Prev[K][i]);\n }\n ans /= combination[emp][K];\n cout << fixed << setprecision(30) << ans << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 540, "memory_kb": 15052, "score_of_the_acc": -0.5679, "final_rank": 14 }, { "submission_id": "aoj_2294_4972276", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nint PREP = (cin.tie(nullptr), ios::sync_with_stdio(false), cout << fixed << setprecision(12), 0);\n//int SEGV = getenv(\"D\") || (exit(system(\"D= SEGFAULT_SIGNALS=all catchsegv ./prog.exe\") >> 8), 0);\ndouble dp[2][110][110][110]; // dp[i][j][k][l] = (curr=i, cnt=j, k -> l)\nint main() {\n int H, N, P, M, K; cin >> H >> N >> P >> M >> K; P--;\n vector<int> A(H + 1, -1);\n for (int i = 0; i < M; i++) {\n int a, b; cin >> a >> b; b--;\n A[a] = b; // connect b and b+1\n }\n for (int k = 0; k < N; k++) {\n dp[0][0][k][k] = 1.0;\n }\n for (int i = 1; i <= H; i++) {\n for (int j = 0; j <= K; j++) {\n for (int k = 0; k < N; k++) {\n for (int l = 0; l < N; l++) {\n dp[i%2][j][k][l] = 0;\n }\n }\n }\n if (A[i] >= 0) {\n for (int j = 0; j <= K; j++) {\n for (int k = 0; k < N; k++) {\n for (int l = 0; l < N; l++) {\n int from = 0;\n if (A[i] == l) from = A[i] + 1;\n else if (A[i] + 1 == l) from = A[i];\n else from = l;\n dp[i%2][j][k][l] = dp[(i-1)%2][j][k][from];\n }\n }\n }\n } else {\n for (int j = 0; j <= K; j++) {\n double line = clamp((K - j) / (H - M - i + 1.0), 0.0, 1.0);\n double noline = 1.0 - line;\n for (int k = 0; k < N; k++) {\n for (int l = 0; l < N; l++) {\n double line_P = line / (N - 1);\n double line_L = l == 0 ? 0.0 : line_P;\n double line_R = l == N-1 ? 0.0 : line_P;\n double line_N = line - line_L - line_R;\n if (j == K) {\n dp[i%2][j][k][l] += dp[(i-1)%2][j][k][l];\n } else {\n dp[i%2][j][k][l] += noline * dp[(i-1)%2][j][k][l];\n if (l+1<N) dp[i%2][j+1][k][l+1] += line_R * dp[(i-1)%2][j][k][l];\n if (l-1>=0) dp[i%2][j+1][k][l-1] += line_L * dp[(i-1)%2][j][k][l];\n dp[i%2][j+1][k][l] += line_N * dp[(i-1)%2][j][k][l];\n }\n }\n }\n }\n }\n if (A[i] != -1) M--;\n }\n double ans = 0.0;\n for (int k = 0; k < N; k++) {\n ans = max(ans, dp[H%2][K][k][P]);\n }\n cout << ans << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 1260, "memory_kb": 23968, "score_of_the_acc": -1.2537, "final_rank": 20 }, { "submission_id": "aoj_2294_4972007", "code_snippet": "#include <iostream>\n#include <cstdio>\n#include <vector>\n#include <algorithm>\n#include <list>\n#include <string>\n#include <iomanip>\n#include <queue>\n\nusing namespace std;\n\nconstexpr int inf = 1e9 + 7;\n\nint main(){\n\n int h, n, p, m, k;\n cin >> h >> n >> p >> m >> k;\n --p;\n\n // amida[i][j] := 左からi番目の棒とi+1番目の棒の間に高さjの位置に横棒があるか\n vector<vector<bool>> amida (n - 1, vector<bool>(h, false));\n\n for(int i = 0; i < m; ++i){\n int a, b;\n cin >> a >> b;\n --a, --b;\n amida[b][a] = true;\n }\n\n // k本の横棒をランダムに引く\n\n // 下からやる\n // dp[i][j][k] := 高さiにいるときに横からj番目の棒へ追加の棒をk回使用したときの確率\n vector<vector<vector<double>>> prob(h + 1, vector<vector<double>>(n, vector<double>(k + 1, 0.0)));\n prob[h][p][0] = 1.0;\n\n vector<bool> used_y(h, false);\n\n // 置くことができる総数\n int can_set_num = 0;\n for(int y = 0; y < h; ++y){\n bool used = false;\n for(int i = 0; i < n - 1; ++i){\n used = used || amida[i][y];\n }\n used_y[y] = used;\n if(used) continue;\n can_set_num++;\n }\n double not_used = can_set_num;\n\n for(int y = h; y > 0; --y){\n for(int t = 0; t <= k; ++t){\n for(int i = 0; i < n; ++i){\n\n // そもそも横軸があった\n if(used_y[y - 1]){\n // 確定で移動\n if (i < n-1 && amida[i][y-1]) {\n prob[y-1][i+1][t] += prob[y][i][t];\n }\n else if (i > 0 && amida[i-1][y-1]) {\n prob[y-1][i-1][t] += prob[y][i][t];\n }\n else {\n prob[y-1][i][t] += prob[y][i][t];\n }\n }\n else{\n double use = (k-t)/not_used;\n double nuse = 1-use;\n double nm1 = 1.0;\n nm1 /= n-1;\n //int can_set = can_set_num - t * (n - 1);\n // 横軸を入れる場合\n if (t < k) {\n if (i == 0) {\n prob[y-1][i+1][t+1] += prob[y][i][t]*use*nm1;\n prob[y-1][i][t+1] += prob[y][i][t]*use*(1.0-nm1);\n }\n else if (i == n-1) {\n prob[y-1][i-1][t+1] += prob[y][i][t]*use*nm1;\n prob[y-1][i][t+1] += prob[y][i][t]*use*(1.0-nm1);\n }\n else {\n prob[y-1][i-1][t+1] += prob[y][i][t]*use*nm1;\n prob[y-1][i+1][t+1] += prob[y][i][t]*use*nm1;\n prob[y-1][i][t+1] += prob[y][i][t]*use*(1.0-2.0*nm1);\n }\n // 入れない場合\n prob[y-1][i][t] += prob[y][i][t]*nuse;\n }\n else {\n // 入れない場合\n prob[y-1][i][t] += prob[y][i][t];\n }\n }\n \n }\n }\n if(!used_y[y - 1]) {\n not_used -= 1.0;\n }\n }\n\n //double ans = *max_element(prob[0].begin(), prob[0].end());\n\n double ans = 0.0;\n for(int i = 0; i < n; ++i) ans = max(ans, prob[0][i][k]);\n\n cout << fixed << setprecision(15) << ans << endl;\n\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 44340, "score_of_the_acc": -0.5288, "final_rank": 12 }, { "submission_id": "aoj_2294_4938305", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntemplate<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }\ntemplate<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }\n#define all(x) (x).begin(),(x).end()\n#define fi first\n#define se second\n#define mp make_pair\n#define si(x) int(x.size())\nconst int mod=1000000007,MAX=200005;\n//const int INF=1<<30;\nconst ll INF=1LL<<60;\n\ndouble dp[505][105][105];//高さi,j回使った、kにいる\nint hen[505];\n\nint main(){\n \n std::ifstream in(\"text.txt\");\n std::cin.rdbuf(in.rdbuf());\n cin.tie(0);\n ios::sync_with_stdio(false);\n \n int H,N,P,M,K;cin>>H>>N>>P>>M>>K;\n \n memset(hen,-1,sizeof(hen));\n for(int i=0;i<M;i++){\n int a,b;cin>>a>>b;\n a--;b--;\n hen[H-a]=b;\n }\n \n P--;\n \n dp[0][0][P]=1;\n \n int rem=H-M;\n \n for(int i=0;i<H;i++){\n for(int j=0;j<=K;j++){\n for(int k=0;k<N;k++){\n //if(dp[i][j][k]==0) continue;\n \n if(hen[i+1]==-1){\n double p=double(K-j)/rem;\n \n dp[i+1][j][k]+=dp[i][j][k]*(1.0-p);\n \n for(int l=0;l<N-1;l++){\n if(k==l){\n dp[i+1][j+1][k]+=dp[i][j][k+1]*p/(N-1);\n }else if(k==l+1){\n dp[i+1][j+1][k]+=dp[i][j][k-1]*p/(N-1);\n }else{\n dp[i+1][j+1][k]+=dp[i][j][k]*p/(N-1);\n }\n }\n }else{\n if(k==hen[i+1]){\n dp[i+1][j][k]+=dp[i][j][k+1];\n }else if(k==hen[i+1]+1){\n dp[i+1][j][k]+=dp[i][j][k-1];\n }else{\n dp[i+1][j][k]+=dp[i][j][k];\n }\n }\n }\n }\n if(hen[i+1]==-1) rem--;\n }\n \n double ans=0;\n \n for(int i=0;i<N;i++){\n chmax(ans,dp[H][K][i]);\n }\n \n cout<<setprecision(25)<<ans<<endl;\n}", "accuracy": 1, "time_ms": 840, "memory_kb": 46656, "score_of_the_acc": -1.1974, "final_rank": 19 }, { "submission_id": "aoj_2294_4925690", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#define rep(i,n) for(int i=0;i<(n);++i)\n#define foreach(i,n) for(auto &i:(n))\n#define lambda(RES,...) (function<RES(__VA_ARGS__)>) [&](__VA_ARGS__) -> RES\n#define method(NAME,RES,...) function<RES(__VA_ARGS__)>NAME = [&](__VA_ARGS__) -> RES\n#define all(x) (x).begin(),(x).end()\n#define bit(x) (1ll<<(x))\n\ntemplate<class t>\nusing priority_queuer = priority_queue<t,vector<t>,greater<t>>;\ntemplate<class t>\nusing vvector = vector<vector<t>>;\nusing pii = pair<int,int>;\nusing ll = long long;\ntemplate<class t,class u>\nbool chmin(t&a,u b){\n if(a>b){\n a = b;\n return true;\n }\n return false;\n}\ntemplate<class t,class u>\nbool chmax(t&a,u b){\n if(a<b){\n a = b;\n return true;\n }\n return false;\n}\n\nll in(){\n ll res;\n cin >> res;\n return res;\n}\n\n\ntemplate<class t>\nt in(){\n t res;\n cin >> res;\n return res;\n}\n\n\nconst ll INF = 1e9;\n\ndouble func(){\n int h = in();\n int n = in();\n int p = in()-1;\n int m = in();\n int k = in();\n vector<int> left(h,-1);\n rep(i,m){\n int a = in();\n int b = in();\n left[a-1] = b-1;\n }\n method(toint,int,int y,int x,int rem){\n return rem + (k+1) * (x + n * y);\n };\n\n vector<double> dp(toint(h+1,n,k),-1);\n method(func,double,int y,int x,int rem,int space){\n if(rem>space)return 0;\n if(y==h){\n return x == p and !rem;\n }\n double &it = dp[toint(y,x,rem)];\n if(it>=0)return it;\n it = 0;\n if(left[y]>=0){\n if(left[y]==x){\n ++x;\n }else if(left[y]+1==x){\n --x;\n }\n it += func(y+1,x,rem,space);\n }else{ \n if(rem){\n int cnt = 0;\n if(x){\n it += func(y+1,x-1,rem-1,space-1) * ((double)rem/space) * ((double)1/(n-1));\n ++cnt;\n }\n if(x+1<n){\n it += func(y+1,x+1,rem-1,space-1) * ((double)rem/space) * ((double)1/(n-1));\n ++cnt;\n }\n it += func(y+1,x,rem-1,space-1) * ((double)rem/space) * ((double)(n-cnt-1)/(n-1));\n }\n it += func(y+1,x,rem,space-1) * (1-((double)rem/space)) ;\n }\n return it;\n };\n double res = 0;\n rep(i,n){\n chmax(res,func(0,i,k,h-m));\n }\n return res;\n}\n\nint main(){\n printf(\"%.20lf\\n\",func());\n return 0;\n}", "accuracy": 1, "time_ms": 130, "memory_kb": 42640, "score_of_the_acc": -0.5799, "final_rank": 15 }, { "submission_id": "aoj_2294_4925155", "code_snippet": "#include<iostream>\n#include<vector>\n#include<queue>\n#include<algorithm>\n#include<iomanip>\n#include<cassert>\n\nusing namespace std;\n\nvoid solve(){\n vector<vector<bool>> bo;\n vector<int> bo2;\n int H, N, P, M, K;\n\n double ans = 0;\n\n cin>>H>>N>>P>>M>>K;\n\n bo.resize(H+2, vector<bool>(N+2));\n bo2.resize(H+2);\n\n for(int i = 0; i < M; i++){\n int A, B;\n\n cin>>A>>B;\n\n bo[H - A + 1][B] = true;\n bo2[H - A + 1] = B;\n }\n\n \n int m = 0;\n vector<vector<double>> dp(K+2, vector<double>(N+2));\n dp[0][P] = 1;\n \n for(int j = 1; j <= H; j++){\n vector<vector<double>> nex(K+2, vector<double>(N+2));\n for(int l = min(K, m); l >= 0; l--) {\n if(bo2[j]) {\n for(int k = 1; k <= N; k++){\n if(bo[j][k]) {\n nex[l][k+1] += dp[l][k];\n } else if(bo[j][k-1]) {\n nex[l][k-1] += dp[l][k];\n } else {\n nex[l][k] += dp[l][k];\n }\n }\n } else {\n double A = (K - l);\n double B = (H - M - m);\n double C = H - M - m - (K - l);\n\n double pro = A / (B * (N - 1));\n double pro11 = C / B;\n double pro12 = pro * (N - 2);\n double pro22 = pro * (N - 3);\n \n for(int k = 1; k <= N; k++){\n nex[l+1][k+1] += dp[l][k] * pro;\n nex[l][k] += dp[l][k] * pro11;\n nex[l+1][k-1] += dp[l][k] * pro;\n \n if(k == 1 || k == N)nex[l+1][k] += dp[l][k] * pro12;\n else nex[l+1][k] += dp[l][k] * pro22;\n }\n \n }\n }\n swap(dp, nex);\n if(bo2[j] == false) m++;\n }\n \n for(int i = 1; i <= N; i++) ans = max(ans, dp[K][i]);\n\n cout<<ans<<endl;\n}\n\nsigned main(){\n cout<<fixed<<setprecision(10);\n\n solve();\n\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3440, "score_of_the_acc": -0.0007, "final_rank": 1 }, { "submission_id": "aoj_2294_4925145", "code_snippet": "#include<iostream>\n#include<vector>\n#include<queue>\n#include<algorithm>\n#include<iomanip>\n#include<cassert>\n\nusing namespace std;\n\nvoid solve(){\n vector<vector<bool>> bo;\n vector<int> bo2;\n int H, N, P, M, K;\n\n double ans = 0;\n\n cin>>H>>N>>P>>M>>K;\n\n bo.resize(H+2, vector<bool>(N+2));\n bo2.resize(H+2);\n\n for(int i = 0; i < M; i++){\n int A, B;\n\n cin>>A>>B;\n\n bo[H - A + 1][B] = true;\n bo2[H - A + 1] = B;\n }\n\n \n int m = 0;\n vector<vector<double>> dp(K+2, vector<double>(N+2));\n dp[0][P] = 1;\n \n for(int j = 1; j <= H; j++){\n vector<vector<double>> nex(K+2, vector<double>(N+2));\n for(int l = min(K, m); l >= 0; l--) {\n if(bo2[j]) {\n for(int k = 1; k <= N; k++){\n if(bo[j][k]) {\n nex[l][k+1] += dp[l][k];\n } else if(bo[j][k-1]) {\n nex[l][k-1] += dp[l][k];\n } else {\n nex[l][k] += dp[l][k];\n }\n }\n } else {\n double A = (K - l);\n double B = (H - M - m);\n double C = H - M - m - (K - l);\n\n double pro = A / (B * (N - 1));\n double pro11 = C / B;\n double pro12 = pro * (N - 2);\n double pro22 = pro * (N - 3);\n \n for(int k = 1; k <= N; k++){\n nex[l+1][k+1] += dp[l][k] * pro;\n nex[l][k] += dp[l][k] * pro11;\n nex[l+1][k-1] += dp[l][k] * pro;\n \n if(k == 1 || k == N)nex[l+1][k] += dp[l][k] * pro12;\n else nex[l+1][k] += dp[l][k] * pro22;\n }\n \n }\n }\n dp = nex;\n if(bo2[j] == false) m++;\n }\n \n for(int i = 1; i <= N; i++) ans = max(ans, dp[K][i]);\n\n cout<<ans<<endl;\n}\n\nsigned main(){\n cout<<fixed<<setprecision(10);\n\n solve();\n\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3444, "score_of_the_acc": -0.0008, "final_rank": 2 }, { "submission_id": "aoj_2294_4925137", "code_snippet": "#include<iostream>\n#include<vector>\n#include<queue>\n#include<algorithm>\n#include<iomanip>\n#include<cassert>\n\nusing namespace std;\n\nvoid solve(){\n vector<vector<bool>> bo;\n vector<int> bo2;\n int H, N, P, M, K;\n\n double ans = 0;\n\n cin>>H>>N>>P>>M>>K;\n\n bo.resize(H+2, vector<bool>(N+2));\n bo2.resize(H+2);\n\n for(int i = 0; i < M; i++){\n int A, B;\n\n cin>>A>>B;\n\n bo[A][B] = true;\n bo2[A] = B;\n }\n\n \n for(int i = 1; i <= N; i++){\n int m = 0;\n vector<vector<double>> dp(K+2, vector<double>(N+2));\n dp[0][i] = 1;\n \n for(int j = 1; j <= H; j++){\n vector<vector<double>> nex(K+2, vector<double>(N+2));\n for(int l = min(K, m); l >= 0; l--) {\n if(bo2[j]) {\n for(int k = 1; k <= N; k++){\n if(bo[j][k]) {\n nex[l][k+1] += dp[l][k];\n } else if(bo[j][k-1]) {\n nex[l][k-1] += dp[l][k];\n } else {\n nex[l][k] += dp[l][k];\n }\n }\n } else {\n double A = (K - l);\n double B = (H - M - m);\n double C = H - M - m - (K - l);\n\n double pro = A / (B * (N - 1));\n double pro11 = C / B;\n double pro12 = pro * (N - 2);\n double pro22 = pro * (N - 3);\n \n for(int k = 1; k <= N; k++){\n nex[l+1][k+1] += dp[l][k] * pro;\n nex[l][k] += dp[l][k] * pro11;\n nex[l+1][k-1] += dp[l][k] * pro;\n \n if(k == 1 || k == N)nex[l+1][k] += dp[l][k] * pro12;\n else nex[l+1][k] += dp[l][k] * pro22;\n }\n \n }\n }\n dp = nex;\n if(bo2[j] == false) m++;\n }\n ans = max(ans, dp[K][P]);\n }\n\n cout<<ans<<endl;\n}\n\nsigned main(){\n cout<<fixed<<setprecision(10);\n\n solve();\n\n}", "accuracy": 1, "time_ms": 1180, "memory_kb": 3448, "score_of_the_acc": -0.9368, "final_rank": 17 }, { "submission_id": "aoj_2294_4924570", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#define rep(i,n) for(int i=0;i<(n);++i)\n#define foreach(i,n) for(auto &i:(n))\n#define lambda(RES,...) (function<RES(__VA_ARGS__)>) [&](__VA_ARGS__) -> RES\n#define method(NAME,RES,...) function<RES(__VA_ARGS__)>NAME = [&](__VA_ARGS__) -> RES\n#define all(x) (x).begin(),(x).end()\n#define bit(x) (1ll<<(x))\n\ntemplate<class t>\nusing priority_queuer = priority_queue<t,vector<t>,greater<t>>;\ntemplate<class t>\nusing vvector = vector<vector<t>>;\nusing pii = pair<int,int>;\nusing ll = long long;\ntemplate<class t,class u>\nbool chmin(t&a,u b){\n if(a>b){\n a = b;\n return true;\n }\n return false;\n}\ntemplate<class t,class u>\nbool chmax(t&a,u b){\n if(a<b){\n a = b;\n return true;\n }\n return false;\n}\n\nll in(){\n ll res;\n cin >> res;\n return res;\n}\n\n\ntemplate<class t>\nt in(){\n t res;\n cin >> res;\n return res;\n}\n\n\nconst ll INF = 1e9;\n\ndouble func(){\n int h = in();\n int n = in();\n int p = in()-1;\n int m = in();\n int k = in();\n vector<int> left(h,-1);\n rep(i,m){\n int a = in();\n int b = in();\n left[a-1] = b-1;\n }\n method(toint,int,int y,int x,int rem){\n return rem + (k+1) * (x + n * y);\n };\n\n vector<double> dp(toint(h+1,n,k),-1);\n method(func,double,int y,int x,int rem,int space){\n if(rem>space)return 0;\n if(y==h){\n return x == p and !rem;\n }\n double &it = dp[toint(y,x,rem)];\n if(it>=0)return it;\n it = 0;\n if(left[y]>=0){\n if(left[y]==x){\n ++x;\n }else if(left[y]+1==x){\n --x;\n }\n it += func(y+1,x,rem,space);\n }else{ \n if(rem){\n int cnt = 0;\n if(x){\n it += func(y+1,x-1,rem-1,space-1) * ((double)rem/space) * ((double)1/(n-1));\n ++cnt;\n }\n if(x+1<n){\n it += func(y+1,x+1,rem-1,space-1) * ((double)rem/space) * ((double)1/(n-1));\n ++cnt;\n }\n it += func(y+1,x,rem-1,space-1) * ((double)rem/space) * ((double)(n-cnt-1)/(n-1));\n }\n it += func(y+1,x,rem,space-1) * (1-((double)rem/space)) ;\n }\n return it;\n };\n double res = 0;\n rep(i,n){\n chmax(res,func(0,i,k,h-m));\n }\n return res;\n}\n\nint main(){\n printf(\"%.20lf\\n\",func());\n return 0;\n}", "accuracy": 1, "time_ms": 130, "memory_kb": 42640, "score_of_the_acc": -0.5799, "final_rank": 15 } ]
aoj_2298_cpp
I: Starting Line ICPC で良い成績を収めるには修行が欠かせない.うさぎは ICPC で勝ちたいので,今日も修行をすることにした. 今日の修行は,一直線上の道を走って,体力と判断力を養おうというものである.うさぎは今,スタートラインに立って長い長い道を見渡している. 道の途中にはいくつかニンジンが置かれており,うさぎはニンジンを食べると加速することができる.加速していないときのうさぎの走る速さは毎秒 U メートルであるが,ニンジンを食べることで,最後のニンジンを食べてから T 秒後までは速さが毎秒 V メートルとなる.また,うさぎはニンジンを K 個まで食べずに持っておくことができる.ニンジンを持っていても走る速さは変わらない. ニンジンを持ったり食べたりするのに時間はかからないとして,ゴールまでの最短所要時間を求めたい. Input N K T U V L D 1 ... D N N はニンジンの個数, L はスタートからゴールまでの距離 (メートル), D i (1 ≤ i ≤ N ) は i 番目のニンジンが置かれている場所のスタートからの距離 (メートル) である. 1 ≤ N ≤ 200,1 ≤ K ≤ N ,1 ≤ T ≤ 10,000,1 ≤ U < V ≤ 10,000,2 ≤ L ≤ 10,000,0 < D 1 < D 2 < ... < D N < L を満たす.入力の値はすべて整数である. Output 最短所要時間 (秒) を 1 行に出力せよ.10 -6 以下の絶対誤差が許容される. Sample Input 1 1 1 1 2 3 100 50 Sample Output 1 49.500000000 Sample Input 2 3 1 1 2 3 100 49 50 51 Sample Output 2 48.666666667
[ { "submission_id": "aoj_2298_7149956", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint main(){\n int N, K, T, U, V, L;\n cin >> N >> K >> T >> U >> V >> L;\n vector<int> D(N), G(L + 1, -1), S(L + 1);\n for(int i = 0; i < N; i++){\n cin >> D[i];\n G[D[i]] = i;\n S[D[i]]++;\n }\n for(int i = 0; i < L; i++)S[i + 1] += S[i];\n double INF = 1 << 29;\n K++;\n vector<vector<double>> dp(L + 1, vector<double>(K + 1, 1 << 30));\n auto f = [&](int from, int fromj, int to){\n to = min(L, to);\n int add = S[to] - S[from], nj = min(K, fromj - 1 + add);\n dp[to][nj] = min(dp[to][nj], dp[from][fromj] + (double)(to - from) / V);\n };\n dp[0][0] = 0;\n for(int i = 0; i < L; i++){\n for(int j = 0; j <= K; j++){\n if(dp[i][j] >= INF)continue;\n if(G[i] == -1 && j == K)continue;\n int nj = min(K, j + S[i + 1] - S[i]);\n if(j < K)dp[i + 1][nj] = min(dp[i + 1][nj], dp[i][j] + double(1) / U);\n if(j){\n f(i, j, i + V * T);\n for(int l = 0; l < N && D[l] <= i + V * T; l++){\n if(D[l] <= i)continue;\n f(i, j, D[l]);\n }\n }\n }\n }\n double ans = 1 << 30;\n for(int i = 0; i <= K; i++) ans = min(ans, dp[L][i]);\n cout << fixed << setprecision(15) << ans << '\\n';\n}", "accuracy": 1, "time_ms": 210, "memory_kb": 19500, "score_of_the_acc": -2, "final_rank": 4 }, { "submission_id": "aoj_2298_1845788", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\nint N,K,U,L,V,T;\ndouble cnt;\nbool mp[10010];\n\nint main(){\n cin>>N>>K>>T>>U>>V>>L;\n for(int i=0,a,A;i<N;i++){\n cin>>a;A=a;\n while(mp[a]&&(1.0*a-A)/V/T<K)a++;\n for(int j=a;j<L&&j<a+T*V;j++)cnt+=!mp[j],mp[j]=1;\n }\n\n printf(\"%.8f\\n\",cnt/V+(L-cnt)/U);\n return 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 1200, "score_of_the_acc": -0.0526, "final_rank": 3 }, { "submission_id": "aoj_2298_1845783", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\nint N,K,U,L;\ndouble cnt,V,T;\nbool mp[10010];\n\nint main(){\n cin>>N>>K>>T>>U>>V>>L;\n for(int i=0,a,A;i<N;i++){\n cin>>a;A=a;\n while(mp[a]&&(a-A)/V/T<K)a++;\n for(int j=a;j<L&&j<a+T*V;j++)cnt+=!mp[j],mp[j]=1;\n }\n\n printf(\"%.8f\\n\",cnt/V+(L-cnt)/U);\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 1256, "score_of_the_acc": -0.0031, "final_rank": 1 }, { "submission_id": "aoj_2298_1845774", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\nint N,K,U,L;\ndouble cnt,V,T;\nbool mp[10010];\n\nint main(){\n cin>>N>>K>>T>>U>>V>>L;\n for(int i=0,a,A;i<N;i++){\n cin>>a;\n A=a;\n while(mp[a]&&(a-A)/V/T<K)a++;\n for(int j=a;j<L&&j<a+T*V;j++)cnt+=!mp[j],mp[j]=1;\n }\n\n printf(\"%.8f\\n\",cnt/V+(L-cnt)/U);\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 1256, "score_of_the_acc": -0.0031, "final_rank": 1 } ]
aoj_2293_cpp
D: Dangerous Tower ICPC で良い成績を収めるには修行が欠かせない.うさぎは ICPC で勝ちたいので,今日も修行をすることにした. 今日の修行は,積み木を丁寧に積み上げることによって,決してミスタイピングをしないほどの手先の器用さを手に入れようというものである.せっかく積み木がたくさんあるので,高い塔を作ろう. 積み木は N 個あり, i 番目 (1 ≤ i ≤ N ) の積み木は 1 × A i × B i の直方体の形をしている.長さ 1 の辺は奥行き方向に用いて,長さ A i , B i の辺を横方向と高さ方向に 1 つずつ割り当てることにした.積み木を積み上げるとき,上の段の積み木は下の段の積み木より横の長さが厳密に短くなければならない.積み木は好きな順番に使うことができ,また,使わない積み木があってもよい.このような制約下で,作ることが可能な最も高い塔を作りたい. Input N A 1 B 1 ... A N B N 1 ≤ N ≤ 1,000,1 ≤ A i , B i ≤ 1,000,000 を満たす.入力の値はすべて整数である. Output 塔の高さの最大値を 1 行に出力せよ. Sample Input 1 3 10 40 10 40 20 30 Sample Output 1 80 Sample Input 2 4 1 2 2 3 3 4 4 1 Sample Output 2 11
[ { "submission_id": "aoj_2293_10946082", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nconstexpr int INF = 1e9;\n\nusing pii = pair<int, int>;\n\nstruct edge {\n int to, cap, cost, rev;\n};\n\nusing edges = vector<edge>;\nusing graph = vector<edges>;\n\nvoid add_edge(graph& g, int from, int to, int cap, int cost) {\n g[from].push_back(edge{to, cap, cost, (int)g[to].size()});\n g[to].push_back(edge{from, 0, -cost, (int)g[from].size()-1});\n}\n\nint min_cost_flow(graph& g, int s, int t, int f) {\n int res = 0;\n const int N = g.size();\n vector<int> h(N), dist(N), prevv(N), preve(N);\n while(f > 0) {\n priority_queue<pii, vector<pii>, greater<pii>> que;\n fill(dist.begin(), dist.end(), INF);\n dist[s] = 0;\n que.push(make_pair(0, s));\n while(!que.empty()) {\n pii p = que.top();\n int v = p.second;\n que.pop();\n if(dist[v] < p.first) {\n continue;\n }\n for(int i=0; i<g[v].size(); ++i) {\n edge& e = g[v][i];\n if(e.cap > 0 && dist[e.to] > dist[v] + e.cost + h[v] - h[e.to]) {\n dist[e.to] = dist[v] + e.cost + h[v] - h[e.to];\n prevv[e.to] = v;\n preve[e.to] = i;\n que.push(make_pair(dist[e.to], e.to));\n }\n }\n }\n if(dist[t] == INF) {\n return -1;\n }\n for(int v=0; v<N; ++v) {\n h[v] += dist[v];\n }\n int d = f;\n for(int v=t; v!=s; v=prevv[v]) {\n d = min(d, g[prevv[v]][preve[v]].cap);\n }\n f -= d;\n res += d * h[t];\n for(int v=t; v!=s; v=prevv[v]) {\n edge& e = g[prevv[v]][preve[v]];\n e.cap -= d;\n g[v][e.rev].cap += d;\n }\n }\n return res;\n}\n\nint main() {\n int N;\n cin >> N;\n vector<int> A(N), B(N);\n vector<int> v;\n for(int i=0; i<N; ++i) {\n cin >> A[i] >> B[i];\n v.push_back(A[i]);\n v.push_back(B[i]);\n }\n sort(v.begin(), v.end());\n v.erase(unique(v.begin(), v.end()), v.end());\n graph g(N+v.size()+3);\n int source = N + v.size();\n int unuse = N + v.size() + 1;\n int sink = N + v.size() + 2;\n for(int i=0; i<N; ++i) {\n add_edge(g, source, i, 1, 0);\n }\n for(int i=0; i<N; ++i) {\n int a = lower_bound(v.begin(), v.end(), A[i]) - v.begin();\n add_edge(g, i, N+a, 1, -B[i]);\n int b = lower_bound(v.begin(), v.end(), B[i]) - v.begin();\n add_edge(g, i, N+b, 1, -A[i]);\n add_edge(g, i, unuse, 1, 0);\n }\n for(int i=0; i<v.size(); ++i) {\n add_edge(g, N+i, sink, 1, 0);\n }\n add_edge(g, unuse, sink, INF, 0);\n cout << -min_cost_flow(g, source, sink, N) << endl;\n}", "accuracy": 1, "time_ms": 200, "memory_kb": 3792, "score_of_the_acc": -0.1508, "final_rank": 12 }, { "submission_id": "aoj_2293_9805229", "code_snippet": "#include<bits/stdc++.h>\n// #include<atcoder/all>\n// #include<boost/multiprecision/cpp_int.hpp>\n\nusing namespace std;\n// using namespace atcoder;\n// using bint = boost::multiprecision::cpp_int;\nusing ll = long long;\nusing ull = unsigned long long;\nusing ld = long double;\nusing P = pair<ll,ll>;\nusing vi = vector<ll>;\nusing vvi = vector<vi>;\nusing vvvi = vector<vvi>;\nusing ve = vector<vector<int>>;\nusing vb = vector<bool>;\nusing vvb = vector<vb>;\n#define rep(i,n) for(ll i = 0;i < (ll)n;i++)\n#define ALL(x) (x).begin(),(x).end()\n#define sz(c) ((ll)(c).size())\n#define LB(A,x) (int)(lower_bound(A.begin(),A.end(),x)-A.begin())\n#define UB(A,x) (int)(upper_bound(A.begin(),A.end(),x)-A.begin())\n// #define MOD 1000000007\n#define MOD 998244353\ntemplate<typename T>using min_priority_queue=priority_queue<T,vector<T>,greater<T>>;\ntemplate<typename T>ostream&operator<<(ostream&os,vector<T>&v){for(int i = 0;i < v.size();i++)os<<v[i]<<(i+1!=v.size()?\" \":\"\");return os;}\ntemplate<typename T>istream&operator>>(istream&is,vector<T>&v){for(T&in:v)is>>in;return is;}\ntemplate<typename T1,typename T2>ostream&operator<<(ostream&os,pair<T1,T2>&p){os<<p.first<<\" \"<<p.second;return os;}\ntemplate<typename T1,typename T2>istream&operator>>(istream&is,pair<T1,T2>&p){is>>p.first>>p.second;return is;}\ntemplate<typename T> inline bool chmax(T &a,T b){if(a < b){a = b;return true;}return false;}\ntemplate<typename T> inline bool chmin(T &a,T b){if(a > b){a = b;return true;}return false;}\nld dist(ld x1,ld y1,ld x2, ld y2){return sqrtl((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));}\n\n// https://ei1333.github.io/library/graph/flow/primal-dual.hpp.html\ntemplate<typename flow_t,typename cost_t>\nstruct MinCostFlow{\n struct edge{\n int to;\n flow_t cap;cost_t cost;\n int rev;\n bool is_e;// 本当の辺かどうか\n };\n vector<vector<edge>> v;\n vector<cost_t> potential,mn_cost;\n vector<int> pre_v,pre_e;\n const cost_t inf;\n MinCostFlow(int n):v(n),inf(numeric_limits<cost_t>::max()){}\n void add_edge(int fr,int to,flow_t cap,cost_t cost){\n v[fr].emplace_back((edge){to,cap,cost,(int)v[to].size(),true});\n v[to].emplace_back((edge){fr,0,-cost,(int)v[fr].size()-1,false});\n }\n // s->tの流量fの最小コスト O(FElog(V))\n cost_t min_cost_flow(int s,int t,flow_t f){\n int n = (int)v.size();\n cost_t res = 0;\n using pi = pair<cost_t,int>;//{cost,頂点番号}\n priority_queue<pi,vector<pi>,greater<pi>> que;\n potential.assign(n,0);\n pre_v.assign(n,-1);pre_e.assign(n,-1);\n while(f > 0){\n mn_cost.assign(n,inf);\n que.emplace(0,s);\n mn_cost[s] = 0;\n while(!que.empty()){\n auto [o_cost,ov] = que.top();que.pop();\n if(mn_cost[ov] < o_cost)continue;\n for(int i = 0;i < (int)v[ov].size();i++){\n edge &e = v[ov][i];\n cost_t n_cost = mn_cost[ov] + e.cost + potential[ov]-potential[e.to];\n if(e.cap > 0 && n_cost < mn_cost[e.to]){\n mn_cost[e.to] = n_cost;\n pre_v[e.to] = ov,pre_e[e.to] = i;\n que.emplace(mn_cost[e.to],e.to);\n }\n }\n }\n if(mn_cost[t] == inf)return -1;\n for(int i = 0;i < n;i++)potential[i] += mn_cost[i];\n flow_t add_flow = f;\n for(int ov = t;ov != s;ov = pre_v[ov]){\n add_flow = min(add_flow,v[pre_v[ov]][pre_e[ov]].cap);\n }\n f -= add_flow;\n res += add_flow*potential[t];\n for(int ov = t;ov != s;ov = pre_v[ov]){\n edge &e = v[pre_v[ov]][pre_e[ov]];\n e.cap -= add_flow;\n v[ov][e.rev].cap += add_flow;\n }\n }\n return res;\n }\n // 復元の方法 下の実装で(ov->nv,辺の今の流量/辺の流量の最大値)\n void output(){\n for(int i = 0;i < v.size();i++){\n for(auto &e : v[i]){\n if(!e.is_e)continue;\n auto &rev_e = v[e.to][e.rev];\n cout << i << \"->\" << e.to << \" (flow:\" << rev_e.cap << \"/\" << rev_e.cap +e.cap << \")\\n\";\n }\n }\n }\n};\n\nint main(){\n \n ios_base::sync_with_stdio(0), cin.tie(0);\n int n;cin >> n;\n vi a(n),b(n);\n rep(i,n)cin >> a[i] >> b[i];\n vi cor;\n rep(i,n)cor.emplace_back(a[i]),cor.emplace_back(b[i]);\n sort(ALL(cor));\n cor.erase(unique(ALL(cor)),cor.end());\n MinCostFlow<ll,ll> mc(n+sz(cor)+2);\n int S = n + sz(cor),T = S+1;\n rep(i,n){\n int aid = n+LB(cor,a[i]),bid = n+LB(cor,b[i]);\n mc.add_edge(i,bid,1,-a[i]);\n mc.add_edge(i,aid,1,-b[i]);\n }\n rep(i,n)mc.add_edge(S,i,1,0);\n rep(i,sz(cor))mc.add_edge(n+i,T,1,0);\n mc.add_edge(S,T,n,0);\n cout << -mc.min_cost_flow(S,T,n) << \"\\n\";\n \n\n return 0;\n}", "accuracy": 1, "time_ms": 210, "memory_kb": 4068, "score_of_the_acc": -0.1652, "final_rank": 15 }, { "submission_id": "aoj_2293_9805048", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n/**\n * @brief template\n */\n\nstruct MaxFlow {\n struct Edge {\n int to, rev;\n ll cap;\n };\n int V;\n vector<vector<Edge>> G;\n vector<int> itr, level;\n using P = pair<int, int>;\n vector<P> es;\n\n public:\n MaxFlow() {}\n MaxFlow(int V) : V(V) {\n G.assign(V, vector<Edge>());\n }\n int add_vertex() {\n G.push_back(vector<Edge>());\n return V++;\n }\n void add_edge(int from, int to, ll cap) {\n int fid = SZ(G[from]), tid = SZ(G[to]);\n if (from == to)\n tid++;\n es.push_back({from, fid});\n G[from].push_back({to, tid, cap});\n G[to].push_back({from, fid, 0});\n }\n struct Type {\n int from, to;\n ll cap, recap;\n };\n Type get_edge(int i) {\n auto [from, pos] = es[i];\n auto e = G[from][pos];\n auto re = G[e.to][e.rev];\n return Type{from, e.to, e.cap, re.cap};\n }\n void bfs(int s) {\n level.assign(V, -1);\n queue<int> q;\n level[s] = 0;\n q.push(s);\n while (!q.empty()) {\n int v = q.front();\n q.pop();\n for (auto &e : G[v]) {\n if (e.cap > 0 && level[e.to] < 0) {\n level[e.to] = level[v] + 1;\n q.push(e.to);\n }\n }\n }\n }\n ll dfs(int v, int t, ll f) {\n if (v == t)\n return f;\n for (int &i = itr[v]; i < (int)G[v].size(); i++) {\n Edge &e = G[v][i];\n if (e.cap > 0 && level[v] < level[e.to]) {\n ll d = dfs(e.to, t, min(f, e.cap));\n if (d > 0) {\n e.cap -= d, G[e.to][e.rev].cap += d;\n return d;\n }\n }\n }\n return 0;\n }\n ll run(int s, int t) {\n ll ret = 0, f;\n while (bfs(s), level[t] >= 0) {\n itr.assign(V, 0);\n while ((f = dfs(s, t, INF)) > 0)\n ret += f;\n }\n return ret;\n }\n vector<int> cut() {\n vector<int> ret(V);\n rep(v, 0, V) if (level[v] < 0) ret[v] = 1;\n return ret;\n }\n};\n\n/**\n * @brief Maximum Flow\n */\n\ntemplate <class Cap, class Cost> struct MinCostFlow {\n struct X {\n int from, to;\n Cap lb, ub, flow;\n Cost cost;\n };\n struct Edge {\n int to, rev;\n Cap cap;\n Cost cost;\n };\n using P = pair<int, int>;\n int n, m;\n vector<X> es;\n vector<Cap> exc;\n vector<Cost> dual;\n vector<vector<Edge>> g;\n Cost MX;\n MinCostFlow(int _n) : n(_n), m(0), exc(n), dual(n), g(n), MX(0) {}\n void add_edge(int from, int to, Cap lb, Cap ub, Cost cost) {\n m++;\n chmax(MX, cost);\n chmax(MX, -cost);\n es.push_back({from, to, lb, ub, 0, cost});\n }\n void add_excess(int v, Cap c) { exc[v] += c; }\n pair<bool, Cost> run() {\n MaxFlow mf(n + 2);\n int S = n, T = n + 1;\n Cap psum = 0, nsum = 0;\n for (auto &e : es) {\n exc[e.to] += e.lb;\n exc[e.from] -= e.lb;\n mf.add_edge(e.from, e.to, e.ub - e.lb);\n }\n rep(i, 0, n) {\n if (exc[i] > 0) {\n psum += exc[i];\n mf.add_edge(S, i, exc[i]);\n }\n if (exc[i] < 0) {\n nsum += -exc[i];\n mf.add_edge(i, T, -exc[i]);\n }\n }\n\n if (psum != nsum or mf.run(S, T) != psum)\n return {false, 0};\n\n using P = pair<int, int>;\n vector<P> pos;\n rep(i, 0, m) {\n auto e = mf.get_edge(i);\n Cost cost = es[i].cost * n;\n int fid = SZ(g[e.from]), tid = SZ(g[e.to]);\n if (e.from == e.to)\n tid++;\n pos.push_back({e.from, fid});\n g[e.from].push_back({e.to, tid, e.cap, cost});\n g[e.to].push_back({e.from, fid, e.recap, -cost});\n }\n\n // solve\n\n Cost eps = MX * n + 1;\n while (eps > 1) {\n eps = max<Cost>(eps >> 2, 1);\n refine(eps);\n }\n\n Cost ret = 0;\n rep(i, 0, m) {\n auto [from, fid] = pos[i];\n es[i].flow = es[i].ub - g[from][fid].cap;\n ret += es[i].flow * es[i].cost;\n }\n dual.assign(n, 0);\n for (;;) {\n bool upd = 0;\n rep(i, 0, n) {\n for (auto &e : g[i])\n if (e.cap) {\n auto cost = dual[i] + e.cost / n;\n if (chmin(dual[e.to], cost)) {\n upd = 1;\n }\n }\n }\n if (!upd)\n break;\n }\n return {true, ret};\n }\n Cap get_flow(int i) const { return es[i].flow; }\n\n private:\n void refine(Cost &eps) {\n exc.assign(n, 0);\n vector<int> used(n);\n queue<int> que;\n vector<int> iter(n);\n\n auto cost = [&](int from, const Edge &e) {\n return e.cost + dual[from] - dual[e.to];\n };\n auto push = [&](int from, Edge &e, Cap cap) {\n exc[from] -= cap;\n exc[e.to] += cap;\n g[e.to][e.rev].cap += cap;\n e.cap -= cap;\n };\n auto relabel = [&](int v) {\n iter[v] = 0;\n Cost down = MX * (n + 1);\n for (auto &e : g[v])\n if (e.cap) {\n chmin(down, eps + cost(v, e));\n }\n dual[v] -= down;\n que.push(v);\n used[v] = 1;\n };\n\n rep(i, 0, n) {\n for (auto &e : g[i])\n if (e.cap and cost(i, e) < 0) {\n push(i, e, e.cap);\n }\n }\n rep(i, 0, n) if (exc[i] > 0) {\n used[i] = 1;\n que.push(i);\n }\n while (!que.empty()) {\n auto v = que.front();\n que.pop();\n used[v] = 0;\n for (int &i = iter[v]; i < SZ(g[v]); i++) {\n auto &e = g[v][i];\n if (e.cap and cost(v, e) < 0) {\n push(v, e, min(exc[v], e.cap));\n if (!used[e.to] and exc[e.to] > 0) {\n used[e.to] = 1;\n que.push(e.to);\n }\n if (exc[v] == 0)\n break;\n }\n }\n if (exc[v] > 0) {\n relabel(v);\n }\n }\n eps = 0;\n rep(i, 0, n) {\n for (auto &e : g[i])\n if (e.cap) {\n chmax(eps, -cost(i, e));\n }\n }\n }\n};\n\n/**\n * @brief Minimum Cost b-flow\n */\n\nint main() {\n int N;\n cin >> N;\n vector<ll> A(N), B(N), X;\n rep(i,0,N) cin >> A[i] >> B[i], X.push_back(A[i]), X.push_back(B[i]);\n UNIQUE(X);\n int L = X.size();\n MinCostFlow<ll,ll> G(N+L+2);\n G.add_edge(N+L+1,N+L,0,N,0);\n rep(i,0,N) G.add_edge(N+L,i,0,1,0);\n rep(i,0,L) G.add_edge(N+i,N+L+1,0,1,0);\n rep(i,0,N) {\n int AX = LB(X,A[i]), BX = LB(X,B[i]);\n G.add_edge(i,N+AX,0,1,-B[i]);\n G.add_edge(i,N+BX,0,1,-A[i]);\n }\n auto [b,ans] = G.run();\n cout << -ans << endl;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 4312, "score_of_the_acc": -0.0121, "final_rank": 1 }, { "submission_id": "aoj_2293_9556864", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\n\n#pragma GCC target(\"avx2\")\n#pragma GCC optmize(\"Ofast\")\n\ntemplate <class ft = long long, class ct = long long>\nstruct PrimalDual {\n const ct INF;\n \n struct edge {\n int to;\n int rev;\n ft cap;\n ct cost;\n };\n \n int siz;\n vector<vector<edge>> graph;\n vector<ct> pot, minc;\n vector<int> prev_v, prev_id;\n \n PrimalDual(int n) : siz(n), INF(1e18), graph(n) {}\n \n void add_edge(int from, int to, ft cap, ct cost) {\n assert(from < siz && to < siz);\n graph[from].emplace_back((edge){to, (int)graph[to].size(), cap, cost});\n graph[to].emplace_back((edge){from, (int)graph[from].size() - 1, 0, -cost});\n }\n \n ct min_cost_flow(int s, int t, ft f) {\n ct ret = 0;\n using Pi = pair<ct, int>;\n priority_queue<Pi, vector<Pi>, greater<Pi> > pq;\n pot.assign(siz, 0);\n prev_v.assign(siz, -1);\n prev_id.assign(siz, -1);\n \n while (f > 0) {\n minc.assign(siz, INF);\n pq.emplace(0, s);\n minc[s] = 0;\n \n while (pq.size()) {\n auto [cost, v] = pq.top();\n pq.pop();\n \n if (minc[v] < cost) {\n continue;\n }\n for (int i = 0; i < graph[v].size(); ++i) {\n auto &e = graph[v][i];\n ct nc = minc[v] + e.cost + pot[v] - pot[e.to];\n if (e.cap > 0 && minc[e.to] > nc) {\n minc[e.to] = nc;\n prev_v[e.to] = v;\n prev_id[e.to] = i;\n pq.emplace(minc[e.to], e.to);\n }\n }\n }\n \n if (minc[t] == INF) {\n return -1;\n }\n \n for (int v = 0; v < siz; ++v) {\n pot[v] += minc[v];\n }\n ft addflow = f;\n for (int v = t; v != s; v = prev_v[v]) {\n addflow = min(addflow, graph[prev_v[v]][prev_id[v]].cap);\n }\n f -= addflow;\n ret += addflow * pot[t];\n for (int v = t; v != s; v = prev_v[v]) {\n auto &e = graph[prev_v[v]][prev_id[v]];\n e.cap -= addflow;\n graph[e.to][e.rev].cap += addflow;\n }\n }\n return ret;\n }\n \n};\n\nconst ll ABMAX = int(1e6) + 1;\n\nint main() {\n ll N; cin >> N;\n vector<ll> A(N), B(N);\n set<ll> zt;\n zt.insert(0);\n for (int i = 0; i < N; ++i) {\n cin >> A[i] >> B[i];\n zt.insert(A[i]);\n zt.insert(B[i]);\n }\n \n map< int, int > num_to_id;\n {\n int cnt = 0;\n for (auto v : zt) {\n num_to_id[v] = cnt;\n cnt++;\n }\n }\n int siz = zt.size();\n \n int S = siz + N, T = S + 1;\n PrimalDual mcf(T + 1);\n ll base = 0;\n ll n0 = num_to_id[0];\n for (int i = 0; i < N; ++i) {\n ll a = A[i], b = B[i];\n ll na = num_to_id[a], nb = num_to_id[b];\n ll v = siz + i;\n base += (a + b);\n mcf.add_edge(S, v, 1, 0);\n mcf.add_edge(v, n0, 1, a + b);\n mcf.add_edge(v, na, 1, a);\n mcf.add_edge(v, nb, 1, b);\n }\n for (auto [key, value] : num_to_id) {\n ll f = 1;\n if (key == 0) f = N;\n mcf.add_edge(value, T, f, 0);\n }\n \n ll cost = mcf.min_cost_flow(S, T, N);\n ll res = base - cost;\n cout << res << endl;\n \n return 0;\n}", "accuracy": 1, "time_ms": 120, "memory_kb": 4224, "score_of_the_acc": -0.0974, "final_rank": 7 }, { "submission_id": "aoj_2293_9443876", "code_snippet": "#include<bits/stdc++.h>\n\n#include <algorithm>\n#include <cassert>\n#include <limits>\n#include <queue>\n#include <vector>\n\nnamespace atcoder {\n\ntemplate <class Cap, class Cost> struct mcf_graph {\n public:\n mcf_graph() {}\n mcf_graph(int n) : _n(n), g(n) {}\n\n int add_edge(int from, int to, Cap cap, Cost cost) {\n assert(0 <= from && from < _n);\n assert(0 <= to && to < _n);\n int m = int(pos.size());\n pos.push_back({from, int(g[from].size())});\n int from_id = int(g[from].size());\n int to_id = int(g[to].size());\n if (from == to) to_id++;\n g[from].push_back(_edge{to, to_id, cap, cost});\n g[to].push_back(_edge{from, from_id, 0, -cost});\n return m;\n }\n\n struct edge {\n int from, to;\n Cap cap, flow;\n Cost cost;\n };\n\n edge get_edge(int i) {\n int m = int(pos.size());\n assert(0 <= i && i < m);\n auto _e = g[pos[i].first][pos[i].second];\n auto _re = g[_e.to][_e.rev];\n return edge{\n pos[i].first, _e.to, _e.cap + _re.cap, _re.cap, _e.cost,\n };\n }\n std::vector<edge> edges() {\n int m = int(pos.size());\n std::vector<edge> result(m);\n for (int i = 0; i < m; i++) {\n result[i] = get_edge(i);\n }\n return result;\n }\n\n std::pair<Cap, Cost> flow(int s, int t) {\n return flow(s, t, std::numeric_limits<Cap>::max());\n }\n std::pair<Cap, Cost> flow(int s, int t, Cap flow_limit) {\n return slope(s, t, flow_limit).back();\n }\n std::vector<std::pair<Cap, Cost>> slope(int s, int t) {\n return slope(s, t, std::numeric_limits<Cap>::max());\n }\n std::vector<std::pair<Cap, Cost>> slope(int s, int t, Cap flow_limit) {\n assert(0 <= s && s < _n);\n assert(0 <= t && t < _n);\n assert(s != t);\n // variants (C = maxcost):\n // -(n-1)C <= dual[s] <= dual[i] <= dual[t] = 0\n // reduced cost (= e.cost + dual[e.from] - dual[e.to]) >= 0 for all edge\n std::vector<Cost> dual(_n, 0), dist(_n);\n std::vector<int> pv(_n), pe(_n);\n std::vector<bool> vis(_n);\n auto dual_ref = [&]() {\n std::fill(dist.begin(), dist.end(),\n std::numeric_limits<Cost>::max());\n std::fill(pv.begin(), pv.end(), -1);\n std::fill(pe.begin(), pe.end(), -1);\n std::fill(vis.begin(), vis.end(), false);\n struct Q {\n Cost key;\n int to;\n bool operator<(Q r) const { return key > r.key; }\n };\n std::priority_queue<Q> que;\n dist[s] = 0;\n que.push(Q{0, s});\n while (!que.empty()) {\n int v = que.top().to;\n que.pop();\n if (vis[v]) continue;\n vis[v] = true;\n if (v == t) break;\n // dist[v] = shortest(s, v) + dual[s] - dual[v]\n // dist[v] >= 0 (all reduced cost are positive)\n // dist[v] <= (n-1)C\n for (int i = 0; i < int(g[v].size()); i++) {\n auto e = g[v][i];\n if (vis[e.to] || !e.cap) continue;\n // |-dual[e.to] + dual[v]| <= (n-1)C\n // cost <= C - -(n-1)C + 0 = nC\n Cost cost = e.cost - dual[e.to] + dual[v];\n if (dist[e.to] - dist[v] > cost) {\n dist[e.to] = dist[v] + cost;\n pv[e.to] = v;\n pe[e.to] = i;\n que.push(Q{dist[e.to], e.to});\n }\n }\n }\n if (!vis[t]) {\n return false;\n }\n\n for (int v = 0; v < _n; v++) {\n if (!vis[v]) continue;\n // dual[v] = dual[v] - dist[t] + dist[v]\n // = dual[v] - (shortest(s, t) + dual[s] - dual[t]) + (shortest(s, v) + dual[s] - dual[v])\n // = - shortest(s, t) + dual[t] + shortest(s, v)\n // = shortest(s, v) - shortest(s, t) >= 0 - (n-1)C\n dual[v] -= dist[t] - dist[v];\n }\n return true;\n };\n Cap flow = 0;\n Cost cost = 0, prev_cost_per_flow = -1;\n std::vector<std::pair<Cap, Cost>> result;\n result.push_back({flow, cost});\n while (flow < flow_limit) {\n if (!dual_ref()) break;\n Cap c = flow_limit - flow;\n for (int v = t; v != s; v = pv[v]) {\n c = std::min(c, g[pv[v]][pe[v]].cap);\n }\n for (int v = t; v != s; v = pv[v]) {\n auto& e = g[pv[v]][pe[v]];\n e.cap -= c;\n g[v][e.rev].cap += c;\n }\n Cost d = -dual[s];\n flow += c;\n cost += c * d;\n if (prev_cost_per_flow == d) {\n result.pop_back();\n }\n result.push_back({flow, cost});\n prev_cost_per_flow = d;\n }\n return result;\n }\n\n private:\n int _n;\n\n struct _edge {\n int to, rev;\n Cap cap;\n Cost cost;\n };\n\n std::vector<std::pair<int, int>> pos;\n std::vector<std::vector<_edge>> g;\n};\n\n} // namespace atcoder\n\nusing namespace atcoder;\nusing namespace std;\nusing ll=long long;\nusing vll=vector<ll>;\n#define rep(i,n) for(ll i=0;i<ll(n);i++)\n\nint main(){\n ll N;\n cin>>N;\n map<ll,ll> M;\n vll A(N),B(N);\n rep(i,N){\n cin>>A[i]>>B[i];\n M[A[i]]=M[B[i]]=1;\n }\n ll cnt=N;\n for(auto m:M)M[m.first]=cnt,cnt++;\n ll S=cnt;\n ll T=cnt+1;\n mcf_graph<ll,ll> G(cnt+2);\n rep(i,N)G.add_edge(S,i,1,0);\n ll d=1e9;\n rep(i,N){\n G.add_edge(i,M[A[i]],1,d-B[i]);\n G.add_edge(i,M[B[i]],1,d-A[i]);\n }\n \n for(auto m:M){\n G.add_edge(m.second,T,1,0);\n }\n\n auto V=G.slope(S,T);\n ll VN=V.size();\n ll an=0;\n rep(i,VN-1){\n ll rang=(V[i+1].second-V[i].second)/(V[i+1].first-V[i].first);\n for(ll j=V[i].first;j<=V[i+1].first;j++){\n ll res=V[i].second+rang*(j-V[i].first);\n an=max(an,d*j-res);\n }\n }\n cout<<an<<endl;\n\n \n}", "accuracy": 1, "time_ms": 40, "memory_kb": 4128, "score_of_the_acc": -0.0316, "final_rank": 4 }, { "submission_id": "aoj_2293_9373890", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <map>\n#include <queue>\n#include <set>\nusing namespace std;\ntypedef long long ll;\n#define rep(i, n) for(int i = 0; i < (n); i++)\n\ntemplate<class T>\nusing vi = vector<T>;\n\ntemplate<class T>\nusing vii = vector<vi<T>>;\n\ntemplate<class T>\nusing viii = vector<vii<T>>;\n\n\ntemplate<class T = ll>\nstruct MCF {\nprivate:\n const T inf = 1e18;\n vector<T> dist; //最短距離(費用)\n vector<int> pv, pe; //直前の頂点、辺 \n\npublic:\n struct edge {\n int to, rev; long long cap; T cost;\n edge(int t = 0, int r = 0, long long ca = 0, T co = 0)\n : to(t), rev(r), cap(ca), cost(co) {}\n };\n\n struct pii {\n T d; int v; //最短距離(費用)、頂点番号\n pii(T d_ = 0, int v_ = 0) : d(d_), v(v_) {}\n\n bool operator<(const pii& other) const {\n if (d != other.d) return d < other.d;\n return v < other.v;\n }\n\n bool operator>(const pii& other) const {\n if (d != other.d) return d > other.d;\n return v > other.v;\n }\n };\n\n int n;\n vector<vector<edge>> to;\n vector<T> pot; //ポテンシャル\n\n MCF(int n_ = 1) : n(n_), to(n_), pot(n_), dist(n_), pv(n_), pe(n_) {}\n\n void addedge(int u, int v, long long cap, T cost) {\n int su = (int)to[u].size();\n int sv = (int)to[v].size();\n to[u].push_back({ v, sv, cap, cost });\n to[v].push_back({ u, su, 0, -cost });\n }\n\n //s->tへ流量fの最小費用流、流せないときは-1\n T mincostflow(int s, int t, long long f) {\n T res = 0;\n pot.assign(n, 0);\n while (f) {\n priority_queue<pii, vector<pii>, greater<pii>> pq;\n dist.assign(n, inf);\n dist[s] = 0;\n pq.push({ 0, s });\n while (!pq.empty()) {\n pii elem = pq.top();\n pq.pop();\n if (dist[elem.v] < elem.d) continue;\n int sv = (int)to[elem.v].size();\n for (int i = 0; i < sv; i++) {\n edge& nv = to[elem.v][i];\n if (nv.cap == 0 || dist[nv.to] + pot[nv.to] <= dist[elem.v] + pot[elem.v] + nv.cost) continue;\n dist[nv.to] = dist[elem.v] + pot[elem.v] - pot[nv.to] + nv.cost;\n pv[nv.to] = elem.v;\n pe[nv.to] = i;\n pq.push({ dist[nv.to], nv.to });\n }\n }\n\n if (dist[t] == inf) return res; //流せる最小費用流が存在しない\n\n for (int v = 0; v < n; v++) pot[v] += dist[v];\n\n long long nf = f;\n for (int v = t; v != s; v = pv[v]) nf = min(nf, to[pv[v]][pe[v]].cap);\n f -= nf;\n if (nf * (pot[t] - pot[s]) > 0) return res;\n\n res += nf * (pot[t] - pot[s]); //pot[s] = 0で不要だがポテンシャルを明確化する\n for (int v = t; v != s; v = pv[v]) {\n edge& nv = to[pv[v]][pe[v]];\n nv.cap -= nf;\n to[v][nv.rev].cap += nf;\n }\n }\n return res;\n }\n\n //s->tへ流量fの最小費用流、流せないときは費用と流せなかった流量のペア\n pair<T, long long> mincostflow_pair(int s, int t, long long f) {\n T res = 0;\n pot.assign(n, 0);\n while (f) {\n priority_queue<pii, vector<pii>, greater<pii>> pq;\n dist.assign(n, inf);\n dist[s] = 0;\n pq.push({ 0, s });\n while (!pq.empty()) {\n pii elem = pq.top();\n pq.pop();\n if (dist[elem.v] < elem.d) continue;\n int sv = (int)to[elem.v].size();\n for (int i = 0; i < sv; i++) {\n edge& nv = to[elem.v][i];\n if (nv.cap == 0 || dist[nv.to] + pot[nv.to] <= dist[elem.v] + pot[elem.v] + nv.cost) continue;\n dist[nv.to] = dist[elem.v] + pot[elem.v] - pot[nv.to] + nv.cost;\n pv[nv.to] = elem.v;\n pe[nv.to] = i;\n pq.push({ dist[nv.to], nv.to });\n }\n }\n\n if (dist[t] == inf) return { res, f }; //流せる最小費用流が存在しない\n\n for (int v = 0; v < n; v++) pot[v] += dist[v];\n\n long long nf = f;\n for (int v = t; v != s; v = pv[v]) nf = min(nf, to[pv[v]][pe[v]].cap);\n f -= nf; \n res += nf * (pot[t] - pot[s]); //pot[s] = 0で不要だがポテンシャルを明確化する\n for (int v = t; v != s; v = pv[v]) {\n edge& nv = to[pv[v]][pe[v]];\n nv.cap -= nf;\n to[v][nv.rev].cap += nf;\n }\n }\n return { res, f };\n }\n};\n\nint main()\n{\n int n;\n cin >> n;\n vi<int> a(n), b(n);\n rep(i, n) cin >> a[i] >> b[i];\n\n map<int, int> mp;\n rep(i, n) mp[a[i]];\n rep(i, n) mp[b[i]];\n int idx = 0;\n for (auto& elem : mp) elem.second = idx++;\n\n int sz = (int)mp.size();\n\n MCF mcf(n + sz + 2);\n int S = n + sz, T = S + 1;\n\n rep(i, n) mcf.addedge(S, i, 1, 0);\n rep(j, sz) mcf.addedge(j + n, T, 1, 0);\n\n rep(i, n) {\n mcf.addedge(i, mp[a[i]] + n, 1, -b[i]);\n mcf.addedge(i, mp[b[i]] + n, 1, -a[i]);\n }\n\n auto ans = mcf.mincostflow(S, T, n);\n cout << -ans << endl;\n\n return 0;\n}", "accuracy": 1, "time_ms": 170, "memory_kb": 4028, "score_of_the_acc": -0.1325, "final_rank": 8 }, { "submission_id": "aoj_2293_9373855", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <map>\n#include <queue>\n#include <set>\nusing namespace std;\ntypedef long long ll;\n#define rep(i, n) for(int i = 0; i < (n); i++)\n\ntemplate<class T>\nusing vi = vector<T>;\n\ntemplate<class T>\nusing vii = vector<vi<T>>;\n\ntemplate<class T>\nusing viii = vector<vii<T>>;\n\n\ntemplate<class T = ll>\nstruct MCF {\nprivate:\n const T inf = 1e18;\n vector<T> dist; //最短距離(費用)\n vector<int> pv, pe; //直前の頂点、辺 \n\npublic:\n struct edge {\n int to, rev; long long cap; T cost;\n edge(int t = 0, int r = 0, long long ca = 0, T co = 0)\n : to(t), rev(r), cap(ca), cost(co) {}\n };\n\n struct pii {\n T d; int v; //最短距離(費用)、頂点番号\n pii(T d_ = 0, int v_ = 0) : d(d_), v(v_) {}\n\n bool operator<(const pii& other) const {\n if (d != other.d) return d < other.d;\n return v < other.v;\n }\n\n bool operator>(const pii& other) const {\n if (d != other.d) return d > other.d;\n return v > other.v;\n }\n };\n\n int n;\n vector<vector<edge>> to;\n vector<T> pot; //ポテンシャル\n\n MCF(int n_ = 1) : n(n_), to(n_), pot(n_), dist(n_), pv(n_), pe(n_) {}\n\n void addedge(int u, int v, long long cap, T cost) {\n int su = (int)to[u].size();\n int sv = (int)to[v].size();\n to[u].push_back({ v, sv, cap, cost });\n to[v].push_back({ u, su, 0, -cost });\n }\n\n //s->tへ流量fの最小費用流、流せないときは-1\n T mincostflow(int s, int t, long long f) {\n T res = 0;\n pot.assign(n, 0);\n while (f) {\n priority_queue<pii, vector<pii>, greater<pii>> pq;\n dist.assign(n, inf);\n dist[s] = 0;\n pq.push({ 0, s });\n while (!pq.empty()) {\n pii elem = pq.top();\n pq.pop();\n if (dist[elem.v] < elem.d) continue;\n int sv = (int)to[elem.v].size();\n for (int i = 0; i < sv; i++) {\n edge& nv = to[elem.v][i];\n if (nv.cap == 0 || dist[nv.to] + pot[nv.to] <= dist[elem.v] + pot[elem.v] + nv.cost) continue;\n dist[nv.to] = dist[elem.v] + pot[elem.v] - pot[nv.to] + nv.cost;\n pv[nv.to] = elem.v;\n pe[nv.to] = i;\n pq.push({ dist[nv.to], nv.to });\n }\n }\n\n if (dist[t] == inf) return -1; //流せる最小費用流が存在しない\n\n for (int v = 0; v < n; v++) pot[v] += dist[v];\n\n long long nf = f;\n for (int v = t; v != s; v = pv[v]) nf = min(nf, to[pv[v]][pe[v]].cap);\n f -= nf;\n if (nf * (pot[t] - pot[s]) > 0) return res;\n\n res += nf * (pot[t] - pot[s]); //pot[s] = 0で不要だがポテンシャルを明確化する\n for (int v = t; v != s; v = pv[v]) {\n edge& nv = to[pv[v]][pe[v]];\n nv.cap -= nf;\n to[v][nv.rev].cap += nf;\n }\n }\n return res;\n }\n\n //s->tへ流量fの最小費用流、流せないときは費用と流せなかった流量のペア\n pair<T, long long> mincostflow_pair(int s, int t, long long f) {\n T res = 0;\n pot.assign(n, 0);\n while (f) {\n priority_queue<pii, vector<pii>, greater<pii>> pq;\n dist.assign(n, inf);\n dist[s] = 0;\n pq.push({ 0, s });\n while (!pq.empty()) {\n pii elem = pq.top();\n pq.pop();\n if (dist[elem.v] < elem.d) continue;\n int sv = (int)to[elem.v].size();\n for (int i = 0; i < sv; i++) {\n edge& nv = to[elem.v][i];\n if (nv.cap == 0 || dist[nv.to] + pot[nv.to] <= dist[elem.v] + pot[elem.v] + nv.cost) continue;\n dist[nv.to] = dist[elem.v] + pot[elem.v] - pot[nv.to] + nv.cost;\n pv[nv.to] = elem.v;\n pe[nv.to] = i;\n pq.push({ dist[nv.to], nv.to });\n }\n }\n\n if (dist[t] == inf) return { res, f }; //流せる最小費用流が存在しない\n\n for (int v = 0; v < n; v++) pot[v] += dist[v];\n\n long long nf = f;\n for (int v = t; v != s; v = pv[v]) nf = min(nf, to[pv[v]][pe[v]].cap);\n f -= nf; \n res += nf * (pot[t] - pot[s]); //pot[s] = 0で不要だがポテンシャルを明確化する\n for (int v = t; v != s; v = pv[v]) {\n edge& nv = to[pv[v]][pe[v]];\n nv.cap -= nf;\n to[v][nv.rev].cap += nf;\n }\n }\n return { res, f };\n }\n};\n\nint main()\n{\n int n;\n cin >> n;\n vi<int> a(n), b(n);\n rep(i, n) cin >> a[i] >> b[i];\n\n map<int, int> mp;\n rep(i, n) mp[a[i]];\n rep(i, n) mp[b[i]];\n int idx = 0;\n for (auto& elem : mp) elem.second = idx++;\n\n int sz = (int)mp.size();\n\n MCF mcf(n + sz + 2);\n int S = n + sz, T = S + 1;\n\n const ll inf = 1e18;\n rep(i, n) mcf.addedge(S, i, 1, 0);\n rep(j, sz) mcf.addedge(j + n, T, 1, 0);\n\n rep(i, n) {\n mcf.addedge(i, mp[a[i]] + n, 1, -b[i]);\n mcf.addedge(i, mp[b[i]] + n, 1, -a[i]);\n }\n\n auto ans = mcf.mincostflow(S, T, n);\n cout << -ans << endl;\n\n return 0;\n}", "accuracy": 0.16666666666666666, "time_ms": 170, "memory_kb": 3888, "score_of_the_acc": -0.1292, "final_rank": 19 }, { "submission_id": "aoj_2293_8721943", "code_snippet": "#include <bits/stdc++.h>\n\n\n\n\nnamespace zawa {\n\nusing i16 = std::int16_t;\nusing i32 = std::int32_t;\nusing i64 = std::int64_t;\nusing i128 = __int128_t;\n\nusing u8 = std::uint8_t;\nusing u16 = std::uint16_t;\nusing u32 = std::uint32_t;\nusing u64 = std::uint64_t;\n\nusing usize = std::size_t;\n\n} // namespace zawa\n\n\nnamespace zawa {\n\nvoid SetFastIO() {\n std::cin.tie(nullptr)->sync_with_stdio(false);\n}\n\nvoid SetPrecision(u32 dig) {\n std::cout << std::fixed << std::setprecision(dig);\n}\n\n} // namespace zawa\n\n\n\n\nnamespace zawa {\n\nclass U32Pair {\nprivate:\n static constexpr u32 SHIFT{32};\n static constexpr u32 MASK{static_cast<u32>((1LL << SHIFT) - 1)};\n u64 value_{};\npublic:\n constexpr U32Pair() {}\n constexpr U32Pair(u32 first, u32 second) {\n value_ = (static_cast<u64>(first) << SHIFT) | second;\n }\n constexpr u32 first() const noexcept {\n return static_cast<u32>(value_ >> SHIFT);\n }\n constexpr u32 second() const noexcept {\n return static_cast<u32>(value_ & MASK);\n }\n constexpr u64 combined() const noexcept {\n return value_;\n }\n constexpr U32Pair& operator=(const U32Pair& rhs) {\n value_ = rhs.value_;\n return *this;\n }\n friend constexpr bool operator==(const U32Pair& lhs, const U32Pair& rhs) {\n return lhs.value_ == rhs.value_;\n }\n friend constexpr bool operator!=(const U32Pair& lhs, const U32Pair& rhs) {\n return lhs.value_ != rhs.value_;\n }\n friend constexpr bool operator<(const U32Pair& lhs, const U32Pair& rhs) {\n return lhs.value_ < rhs.value_;\n }\n friend constexpr bool operator<=(const U32Pair& lhs, const U32Pair& rhs) {\n return lhs.value_ <= rhs.value_;\n }\n friend constexpr bool operator>(const U32Pair& lhs, const U32Pair& rhs) {\n return lhs.value_ > rhs.value_;\n }\n friend constexpr bool operator>=(const U32Pair& lhs, const U32Pair& rhs) {\n return lhs.value_ >= rhs.value_;\n }\n friend std::ostream& operator<<(std::ostream& os, const U32Pair& pair) {\n os << '(' << pair.first() << ',' << pair.second() << ')';\n return os;\n }\n};\n\nstruct U32PairHash {\n usize operator()(const U32Pair& pair) const noexcept {\n return std::hash<u64>{}(pair.combined());\n }\n};\n\n} // namespace zawa\n\n#include <type_traits>\n\nnamespace zawa {\n\ntemplate <class Cap, class Cost>\nclass SuccessiveShortestPath {\npublic:\n static_assert(std::is_signed_v<Cost>, U\"Cost must be signed\");\n static constexpr Cost invalid{(std::numeric_limits<Cost>::max() >> 1) - 1};\n static constexpr u32 reverseId(u32 i) noexcept {\n return i ^ 1;\n }\n\n struct Edge {\n u32 from{}, to{};\n Cap residual{};\n Cost cost{};\n Edge() = default;\n Edge(u32 from, u32 to, const Cap& cap, const Cost& cost)\n : from{from}, to{to}, residual{cap}, cost{cost} {}\n };\n\n usize n_{}, m_{};\n std::vector<Edge> edges_;\n std::vector<std::vector<u32>> g_;\n std::vector<Cost> dist_, potential_;\n std::vector<U32Pair> prev_;\n Cost mcf{};\n\n constexpr usize size() const noexcept {\n return n_;\n }\n constexpr usize edgeSize() const noexcept {\n return m_;\n }\n \n SuccessiveShortestPath() = default;\n SuccessiveShortestPath(usize n, usize m = usize{}) \n : n_{n}, m_{}, edges_{}, g_(n), dist_(n), potential_(n), prev_(n), mcf{} {\n g_.shrink_to_fit();\n dist_.shrink_to_fit();\n potential_.shrink_to_fit();\n prev_.shrink_to_fit();\n edges_.reserve(2 * m);\n }\n\n void emplace(u32 from, u32 to, const Cap& cap, const Cost& cost) {\n g_[from].emplace_back(m_);\n edges_.emplace_back(from, to, cap, cost);\n m_++;\n }\n\n u32 addEdge(u32 from, u32 to, const Cap& cap, const Cost& cost) {\n assert(from < size());\n assert(to < size());\n u32 res{static_cast<u32>(m_)};\n emplace(from, to, cap, cost);\n emplace(to, from, Cap{}, -cost);\n return res;\n }\n\n inline u32 from(u32 i) const noexcept {\n return edges_[i].from;\n }\n inline u32 to(u32 i) const noexcept {\n return edges_[i].to;\n }\n inline const Cap& residual(u32 i) const noexcept {\n return edges_[i].residual;\n }\n inline const Cost& cost(u32 i) const noexcept {\n return edges_[i].cost;\n }\n inline const Cap& flowed(u32 i) const noexcept {\n assert(i < edgeSize());\n return residual(i ^ 1);\n }\n inline const Cap& capcacity(u32 i) const noexcept {\n assert(i < edgeSize());\n return residual(i) + flowed(i);\n }\n\n inline Cost edgeCost(u32 i) const noexcept {\n return potential_[from(i)] + cost(i) - potential_[to(i)];\n }\n bool relax(u32 i) {\n if (residual(i) > 0 and dist_[to(i)] > dist_[from(i)] + edgeCost(i)) {\n dist_[to(i)] = dist_[from(i)] + edgeCost(i);\n prev_[to(i)] = U32Pair{from(i), i};\n return true;\n }\n return false;\n }\n\n bool dijkstra(u32 s, u32 t) {\n assert(s < size());\n assert(t < size());\n std::fill(dist_.begin(), dist_.end(), invalid);\n dist_[s] = Cost{};\n using qt = std::pair<Cost, u32>;\n std::priority_queue<qt, std::vector<qt>, std::greater<qt>> que;\n que.emplace(dist_[s], s);\n while (que.size()) {\n auto [d, v]{que.top()};\n que.pop();\n if (dist_[v] < d) continue;\n for (u32 i : g_[v]) {\n if (relax(i)) {\n que.emplace(dist_[to(i)], to(i));\n }\n }\n }\n return dist_[t] < invalid;\n }\n\n bool dagdp(u32 s, u32 t) {\n assert(s < size());\n assert(t < size());\n std::fill(dist_.begin(), dist_.end(), invalid);\n dist_[s] = Cost{};\n std::vector<u32> ord;\n ord.reserve(size());\n std::vector<bool> vis(size());\n vis[s] = true;\n ord.push_back(s);\n for (u32 i{} ; i < ord.size() ; i++) {\n u32 v{ord[i]};\n for (auto e : g_[v]) {\n if (!vis[to(e)]) {\n ord.push_back(to(e));\n vis[to(e)] = true;\n }\n relax(e);\n }\n }\n return dist_[t] < invalid;\n }\n\n bool bellmanford(u32 s, u32 t) {\n assert(s < size());\n assert(t < size());\n std::fill(dist_.begin(), dist_.end(), invalid);\n dist_[s] = Cost{};\n for (u32 i{} ; i + 1 < size() ; i++) {\n for (u32 j{} ; j < edgeSize() ; j++) {\n relax(j);\n }\n }\n return dist_[t] < invalid;\n }\n\n\n Cap flush(u32 s, u32 t, Cap up = std::numeric_limits<Cap>::max()) {\n for (u32 v{t} ; v != s ; v = prev_[v].first()) {\n up = std::min(up, residual(prev_[v].second()));\n }\n for (u32 v{t} ; v != s ; v = prev_[v].first()) {\n edges_[prev_[v].second()].residual -= up;\n edges_[prev_[v].second() ^ 1].residual += up;\n }\n return up;\n }\n\n bool flow(u32 s, u32 t, Cap flow) {\n assert(s < size());\n assert(t < size());\n while (flow > 0 and dijkstra(s, t)) {\n for (u32 i{} ; i < size() ; i++) {\n potential_[i] = potential_[i] + (dist_[i] == invalid ? Cost{} : dist_[i]);\n }\n Cap water{flush(s, t, flow)};\n mcf += potential_[t] * water;\n flow -= water;\n }\n return flow == 0;\n }\n\n Cap maxflow(u32 s, u32 t) {\n assert(s < size());\n assert(t < size());\n Cap flow{};\n while (dijkstra(s, t)) {\n for (u32 i{} ; i < size() ; i++) {\n potential_[i] = potential_[i] + (dist_[i] == invalid ? Cost{} : dist_[i]);\n }\n Cap water{flush(s, t)};\n mcf += potential_[t] * water;\n flow += water;\n }\n return flow;\n }\n\n struct Line {\n Cap dn{}, up{};\n Cost slope{};\n Line() = default;\n Line(Cap dn, Cap up, Cost slope) : dn{dn}, up{up}, slope{slope} {}\n };\n std::vector<Line> slope(u32 s, u32 t) {\n assert(s < size());\n assert(t < size()); \n Cap flow{};\n std::vector<Line> res;\n while (dijkstra(s, t)) {\n for (u32 i{} ; i < size() ; i++) {\n potential_[i] = potential_[i] + (dist_[i] == invalid ? Cost{} : dist_[i]);\n }\n Cap water{flush(s, t)};\n mcf += potential_[t] * water;\n res.emplace_back(flow, flow + water, potential_[t]);\n flow += water;\n }\n return res;\n }\n\n Cost minCost() const noexcept {\n return mcf;\n }\n};\n\n} // namespace zawa\n\n\n\nnamespace zawa {\n\ntemplate <class T>\nclass CompressedSequence {\nprivate:\n std::vector<T> comped_;\n std::vector<u32> f_;\n \npublic:\n CompressedSequence() = default;\n CompressedSequence(const std::vector<T>& A) : comped_(A), f_(A.size()) {\n std::sort(comped_.begin(), comped_.end());\n comped_.erase(std::unique(comped_.begin(), comped_.end()), comped_.end());\n comped_.shrink_to_fit();\n f_.shrink_to_fit();\n for (u32 i{} ; i < A.size() ; i++) {\n f_[i] = std::lower_bound(comped_.begin(), comped_.end(), A[i]) - comped_.begin();\n }\n } \n\n inline usize size() const noexcept {\n return comped_.size();\n }\n\n u32 operator[](const T& v) const {\n return std::lower_bound(comped_.begin(), comped_.end(), v) - comped_.begin();\n }\n\n inline u32 map(u32 i) const noexcept {\n assert(i < f_.size());\n return f_[i];\n }\n\n inline T inverse(u32 i) const noexcept {\n assert(i < size());\n return comped_[i];\n }\n};\n\n} // namespace zawa\nusing namespace zawa;\n\nint main() {\n SetFastIO();\n \n int n; std::cin >> n;\n std::vector<int> a(n), b(n);\n std::vector<int> app(2 * n);\n for (int i{} ; i < n ; i++) {\n std::cin >> a[i] >> b[i];\n app[2 * i] = a[i];\n app[2 * i + 1] = b[i];\n }\n CompressedSequence<int> comp(app);\n int m{(int)comp.size()};\n SuccessiveShortestPath<int, long long> mcf(n + m + 2);\n int source{n + m}, sink{source + 1};\n for (int i{} ; i < n ; i++) {\n mcf.addEdge(source, i, 1, 0);\n }\n for (int i{} ; i < m ; i++) {\n mcf.addEdge(n + i, sink, 1, comp.inverse(i));\n }\n for (int i{} ; i < n ; i++) {\n mcf.addEdge(i, n + comp[a[i]], 1, 0);\n mcf.addEdge(i, n + comp[b[i]], 1, 0);\n mcf.addEdge(i, sink, 1, a[i] + b[i]);\n }\n assert(mcf.flow(source, sink, n));\n long long ans{-mcf.minCost()};\n for (int i{} ; i < n ; i++) {\n ans += a[i] + b[i];\n }\n std::cout << ans << '\\n';\n}", "accuracy": 1, "time_ms": 200, "memory_kb": 3976, "score_of_the_acc": -0.1551, "final_rank": 13 }, { "submission_id": "aoj_2293_8521489", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define repd(i,a,b) for (ll i=(a);i<(b);i++)\n#define rep(i,n) repd(i,0,n)\n#define all(x) (x).begin(),(x).end()\ntemplate<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }\ntemplate<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }\ntypedef long long ll;\ntypedef pair<ll,ll> P;\ntypedef vector<ll> vec;\nusing Graph = vector<vector<ll>>;\nconst long long INF = 1LL<<60;\nconst long long MOD = 1000000007;\n\n//https://github.com/atcoder/ac-library\n#ifndef ATCODER_INTERNAL_CSR_HPP\n#define ATCODER_INTERNAL_CSR_HPP 1\n\n#include <algorithm>\n#include <utility>\n#include <vector>\n\nnamespace atcoder {\nnamespace internal {\n\ntemplate <class E> struct csr {\n std::vector<int> start;\n std::vector<E> elist;\n explicit csr(int n, const std::vector<std::pair<int, E>>& edges)\n : start(n + 1), elist(edges.size()) {\n for (auto e : edges) {\n start[e.first + 1]++;\n }\n for (int i = 1; i <= n; i++) {\n start[i] += start[i - 1];\n }\n auto counter = start;\n for (auto e : edges) {\n elist[counter[e.first]++] = e.second;\n }\n }\n};\n\n} // namespace internal\n\n} // namespace atcoder\n\n#endif // ATCODER_INTERNAL_CSR_HPP\n\n#ifndef ATCODER_INTERNAL_QUEUE_HPP\n#define ATCODER_INTERNAL_QUEUE_HPP 1\n\n#include <vector>\n\nnamespace atcoder {\n\nnamespace internal {\n\ntemplate <class T> struct simple_queue {\n std::vector<T> payload;\n int pos = 0;\n void reserve(int n) { payload.reserve(n); }\n int size() const { return int(payload.size()) - pos; }\n bool empty() const { return pos == int(payload.size()); }\n void push(const T& t) { payload.push_back(t); }\n T& front() { return payload[pos]; }\n void clear() {\n payload.clear();\n pos = 0;\n }\n void pop() { pos++; }\n};\n\n} // namespace internal\n\n} // namespace atcoder\n\n#endif // ATCODER_INTERNAL_QUEUE_HPP\n\n#ifndef ATCODER_MINCOSTFLOW_HPP\n#define ATCODER_MINCOSTFLOW_HPP 1\n\n#include <algorithm>\n#include <cassert>\n#include <limits>\n#include <queue>\n#include <vector>\n\nnamespace atcoder {\n\ntemplate <class Cap, class Cost> struct mcf_graph {\n public:\n mcf_graph() {}\n explicit mcf_graph(int n) : _n(n) {}\n\n int add_edge(int from, int to, Cap cap, Cost cost) {\n assert(0 <= from && from < _n);\n assert(0 <= to && to < _n);\n assert(0 <= cap);\n assert(0 <= cost);\n int m = int(_edges.size());\n _edges.push_back({from, to, cap, 0, cost});\n return m;\n }\n\n struct edge {\n int from, to;\n Cap cap, flow;\n Cost cost;\n };\n\n edge get_edge(int i) {\n int m = int(_edges.size());\n assert(0 <= i && i < m);\n return _edges[i];\n }\n std::vector<edge> edges() { return _edges; }\n\n std::pair<Cap, Cost> flow(int s, int t) {\n return flow(s, t, std::numeric_limits<Cap>::max());\n }\n std::pair<Cap, Cost> flow(int s, int t, Cap flow_limit) {\n return slope(s, t, flow_limit).back();\n }\n std::vector<std::pair<Cap, Cost>> slope(int s, int t) {\n return slope(s, t, std::numeric_limits<Cap>::max());\n }\n std::vector<std::pair<Cap, Cost>> slope(int s, int t, Cap flow_limit) {\n assert(0 <= s && s < _n);\n assert(0 <= t && t < _n);\n assert(s != t);\n\n int m = int(_edges.size());\n std::vector<int> edge_idx(m);\n\n auto g = [&]() {\n std::vector<int> degree(_n), redge_idx(m);\n std::vector<std::pair<int, _edge>> elist;\n elist.reserve(2 * m);\n for (int i = 0; i < m; i++) {\n auto e = _edges[i];\n edge_idx[i] = degree[e.from]++;\n redge_idx[i] = degree[e.to]++;\n elist.push_back({e.from, {e.to, -1, e.cap - e.flow, e.cost}});\n elist.push_back({e.to, {e.from, -1, e.flow, -e.cost}});\n }\n auto _g = internal::csr<_edge>(_n, elist);\n for (int i = 0; i < m; i++) {\n auto e = _edges[i];\n edge_idx[i] += _g.start[e.from];\n redge_idx[i] += _g.start[e.to];\n _g.elist[edge_idx[i]].rev = redge_idx[i];\n _g.elist[redge_idx[i]].rev = edge_idx[i];\n }\n return _g;\n }();\n\n auto result = slope(g, s, t, flow_limit);\n\n for (int i = 0; i < m; i++) {\n auto e = g.elist[edge_idx[i]];\n _edges[i].flow = _edges[i].cap - e.cap;\n }\n\n return result;\n }\n\n private:\n int _n;\n std::vector<edge> _edges;\n\n // inside edge\n struct _edge {\n int to, rev;\n Cap cap;\n Cost cost;\n };\n\n std::vector<std::pair<Cap, Cost>> slope(internal::csr<_edge>& g,\n int s,\n int t,\n Cap flow_limit) {\n // variants (C = maxcost):\n // -(n-1)C <= dual[s] <= dual[i] <= dual[t] = 0\n // reduced cost (= e.cost + dual[e.from] - dual[e.to]) >= 0 for all edge\n\n // dual_dist[i] = (dual[i], dist[i])\n std::vector<std::pair<Cost, Cost>> dual_dist(_n);\n std::vector<int> prev_e(_n);\n std::vector<bool> vis(_n);\n struct Q {\n Cost key;\n int to;\n bool operator<(Q r) const { return key > r.key; }\n };\n std::vector<int> que_min;\n std::vector<Q> que;\n auto dual_ref = [&]() {\n for (int i = 0; i < _n; i++) {\n dual_dist[i].second = std::numeric_limits<Cost>::max();\n }\n std::fill(vis.begin(), vis.end(), false);\n que_min.clear();\n que.clear();\n\n // que[0..heap_r) was heapified\n size_t heap_r = 0;\n\n dual_dist[s].second = 0;\n que_min.push_back(s);\n while (!que_min.empty() || !que.empty()) {\n int v;\n if (!que_min.empty()) {\n v = que_min.back();\n que_min.pop_back();\n } else {\n while (heap_r < que.size()) {\n heap_r++;\n std::push_heap(que.begin(), que.begin() + heap_r);\n }\n v = que.front().to;\n std::pop_heap(que.begin(), que.end());\n que.pop_back();\n heap_r--;\n }\n if (vis[v]) continue;\n vis[v] = true;\n if (v == t) break;\n // dist[v] = shortest(s, v) + dual[s] - dual[v]\n // dist[v] >= 0 (all reduced cost are positive)\n // dist[v] <= (n-1)C\n Cost dual_v = dual_dist[v].first, dist_v = dual_dist[v].second;\n for (int i = g.start[v]; i < g.start[v + 1]; i++) {\n auto e = g.elist[i];\n if (!e.cap) continue;\n // |-dual[e.to] + dual[v]| <= (n-1)C\n // cost <= C - -(n-1)C + 0 = nC\n Cost cost = e.cost - dual_dist[e.to].first + dual_v;\n if (dual_dist[e.to].second - dist_v > cost) {\n Cost dist_to = dist_v + cost;\n dual_dist[e.to].second = dist_to;\n prev_e[e.to] = e.rev;\n if (dist_to == dist_v) {\n que_min.push_back(e.to);\n } else {\n que.push_back(Q{dist_to, e.to});\n }\n }\n }\n }\n if (!vis[t]) {\n return false;\n }\n\n for (int v = 0; v < _n; v++) {\n if (!vis[v]) continue;\n // dual[v] = dual[v] - dist[t] + dist[v]\n // = dual[v] - (shortest(s, t) + dual[s] - dual[t]) +\n // (shortest(s, v) + dual[s] - dual[v]) = - shortest(s,\n // t) + dual[t] + shortest(s, v) = shortest(s, v) -\n // shortest(s, t) >= 0 - (n-1)C\n dual_dist[v].first -= dual_dist[t].second - dual_dist[v].second;\n }\n return true;\n };\n Cap flow = 0;\n Cost cost = 0, prev_cost_per_flow = -1;\n std::vector<std::pair<Cap, Cost>> result = {{Cap(0), Cost(0)}};\n while (flow < flow_limit) {\n if (!dual_ref()) break;\n Cap c = flow_limit - flow;\n for (int v = t; v != s; v = g.elist[prev_e[v]].to) {\n c = std::min(c, g.elist[g.elist[prev_e[v]].rev].cap);\n }\n for (int v = t; v != s; v = g.elist[prev_e[v]].to) {\n auto& e = g.elist[prev_e[v]];\n e.cap += c;\n g.elist[e.rev].cap -= c;\n }\n Cost d = -dual_dist[s].first;\n flow += c;\n cost += c * d;\n if (prev_cost_per_flow == d) {\n result.pop_back();\n }\n result.push_back({flow, cost});\n prev_cost_per_flow = d;\n }\n return result;\n }\n};\n\n} // namespace atcoder\n\n#endif // ATCODER_MINCOSTFLOW_HPP\n\nusing namespace atcoder;\n\ntemplate <typename T>\nvector<T> compress(vector<T> &X) {\n // ソートした結果を vals に\n vector<T> vals = X;\n sort(vals.begin(), vals.end());\n // 隣り合う重複を削除(unique), 末端のゴミを削除(erase)\n vals.erase(unique(vals.begin(), vals.end()), vals.end());\n // 各要素ごとに二分探索で位置を求める\n for (int i = 0; i < (int)X.size(); i++) {\n X[i] = lower_bound(vals.begin(), vals.end(), X[i]) - vals.begin();\n }\n return vals;\n}\n\nint main()\n{ \n ios::sync_with_stdio(false);\n cin.tie(0);\n ll n;cin>>n;\n vec a(n),b(n);\n rep(i,n)cin>>a[i]>>b[i];\n vec memo;\n rep(i,n){\n memo.push_back(a[i]);\n memo.push_back(b[i]);\n }\n compress(memo);\n mcf_graph<ll,ll> g(n*3+2);\n ll x=1e12;\n rep(i,n){\n g.add_edge(2+i,2+n+memo[2*i],1,x-b[i]);\n g.add_edge(2+i,2+n+memo[2*i+1],1,x-a[i]);\n g.add_edge(0,2+i,1,0);\n g.add_edge(2+n+2*i,1,1,0);\n g.add_edge(2+n+2*i+1,1,1,0);\n }\n g.add_edge(0,1,n,x);\n ll ans=g.flow(0,1,n).second;\n ans%=x;\n ans=x-ans;\n cout<<ans<<endl;\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 4084, "score_of_the_acc": -0.0147, "final_rank": 2 }, { "submission_id": "aoj_2293_7746496", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing pint = pair<int, int>;\nusing pll = pair<long long, long long>;\ntemplate<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }\ntemplate<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }\n\n#define REP(i, n) for (long long i = 0; i < (long long)(n); ++i)\n#define REP2(i, a, b) for (long long i = a; i < (long long)(b); ++i)\n#define COUT(x) cout << #x << \" = \" << (x) << \" (L\" << __LINE__ << \")\" << endl\ntemplate<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P)\n{ return s << '<' << P.first << \", \" << P.second << '>'; }\ntemplate<class T> ostream& operator << (ostream &s, vector<T> P)\n{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << \" \"; } s << P[i]; } return s; }\ntemplate<class T> ostream& operator << (ostream &s, deque<T> P)\n{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << \" \"; } s << P[i]; } return s; }\ntemplate<class T> ostream& operator << (ostream &s, vector<vector<T> > P)\n{ for (int i = 0; i < P.size(); ++i) { s << endl << P[i]; } return s << endl; }\ntemplate<class T> ostream& operator << (ostream &s, set<T> P)\n{ for(auto it : P) { s << \"<\" << it << \"> \"; } return s; }\ntemplate<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P)\n{ for(auto it : P) { s << \"<\" << it.first << \"->\" << it.second << \"> \"; } return s; }\n\n\n// edge class (for network-flow)\ntemplate<class FLOWTYPE, class COSTTYPE> struct Edge {\n int rev, from, to, id;\n FLOWTYPE cap, icap;\n COSTTYPE cost;\n Edge(int r, int f, int t, FLOWTYPE ca, COSTTYPE co, int id = -1) :\n rev(r), from(f), to(t), cap(ca), icap(ca), cost(co), id(id) {}\n friend ostream& operator << (ostream& s, const Edge& E) {\n if (E.cap > 0)\n return s << E.from << \"->\" << E.to <<\n '(' << E.cap << ',' << E.cost << ')';\n else return s;\n }\n};\n\n// graph class (for network-flow)\ntemplate<class FLOWTYPE, class COSTTYPE> struct Graph {\n vector<vector<Edge<FLOWTYPE, COSTTYPE> > > list;\n \n Graph(int n = 0) : list(n) { }\n void init(int n = 0) { list.clear(); list.resize(n); }\n void reset() { for (int i = 0; i < (int)list.size(); ++i) for (int j = 0; j < list[i].size(); ++j) list[i][j].cap = list[i][j].icap; }\n inline vector<Edge<FLOWTYPE, COSTTYPE> >& operator [] (int i) { return list[i]; }\n inline const size_t size() const { return list.size(); }\n \n inline Edge<FLOWTYPE, COSTTYPE> &redge(const Edge<FLOWTYPE, COSTTYPE> &e) {\n if (e.from != e.to) return list[e.to][e.rev];\n else return list[e.to][e.rev + 1];\n }\n \n void addedge(int from, int to, FLOWTYPE cap, COSTTYPE cost, int id = -1) {\n list[from].push_back(Edge<FLOWTYPE, COSTTYPE>((int)list[to].size(), from, to, cap, cost, id));\n list[to].push_back(Edge<FLOWTYPE, COSTTYPE>((int)list[from].size() - 1, to, from, 0, -cost));\n }\n \n void add_undirected_edge(int from, int to, FLOWTYPE cap, COSTTYPE cost, int id = -1) {\n list[from].push_back(Edge<FLOWTYPE, COSTTYPE>((int)list[to].size(), from, to, cap, cost, id));\n list[to].push_back(Edge<FLOWTYPE, COSTTYPE>((int)list[from].size() - 1, to, from, cap, cost, id));\n }\n\n friend ostream& operator << (ostream& s, const Graph& G) {\n s << endl;\n for (int i = 0; i < G.size(); ++i) {\n s << i << \":\";\n for (auto e : G.list[i]) s << \" \" << e;\n s << endl;\n }\n return s;\n }\n};\n\n// min-cost flow (by primal-dual)\ntemplate<class FLOWTYPE, class COSTTYPE> COSTTYPE MinCostFlow(Graph<FLOWTYPE, COSTTYPE> &G, int s, int t, FLOWTYPE f) {\n int n = (int)G.size();\n vector<COSTTYPE> dist(n, -1);\n vector<int> prevv(n), preve(n), seen(n, 0);\n COSTTYPE res = 0;\n while (f > 0) {\n seen.assign(n, 0);\n dist[s] = 0;\n seen[s] = true;\n while (true) {\n bool update = false;\n for (int v = 0; v < n; ++v) {\n if (!seen[v]) continue;\n for (int i = 0; i < G[v].size(); ++i) {\n Edge<FLOWTYPE, COSTTYPE> &e = G[v][i];\n if (e.cap > 0 && (!seen[e.to] || dist[e.to] > dist[v] + e.cost)) {\n dist[e.to] = dist[v] + e.cost;\n prevv[e.to] = v;\n preve[e.to] = i;\n seen[e.to] = true;\n update = true;\n }\n }\n }\n if (!update) break;\n }\n if (!seen[t]) return -1;\n FLOWTYPE d = f;\n for (int v = t; v != s; v = prevv[v]) {\n d = min(d, G[prevv[v]][preve[v]].cap);\n }\n f -= d;\n res += dist[t] * d;\n for (int v = t; v != s; v = prevv[v]) {\n Edge<FLOWTYPE, COSTTYPE> &e = G[prevv[v]][preve[v]];\n Edge<FLOWTYPE, COSTTYPE> &re = G.redge(e);\n e.cap -= d;\n re.cap += d;\n }\n }\n return res;\n}\n\nint main() {\n int N;\n cin >> N;\n vector<int> A(N), B(N), lens;\n for (int i = 0; i < N; ++i) {\n cin >> A[i] >> B[i];\n lens.push_back(A[i]);\n lens.push_back(B[i]);\n }\n sort(lens.begin(), lens.end());\n lens.erase(unique(lens.begin(), lens.end()), lens.end());\n \n // グラフ\n int source = N + lens.size(), sink = N + lens.size() + 1;\n Graph<int,int> G(N + lens.size() + 2);\n \n // ソース → 直方体\n for (int i = 0; i < N; ++i) {\n G.addedge(source, i, 1, 0);\n }\n // 長さ → シンク\n for (int i = 0; i < lens.size(); ++i) {\n G.addedge(i+N, sink, 1, 0);\n }\n // ソース → シンク\n G.addedge(source, sink, N, 0);\n // 直方体 → シンク\n for (int i = 0; i < N; ++i) {\n int aid = lower_bound(lens.begin(), lens.end(), A[i]) - lens.begin();\n int bid = lower_bound(lens.begin(), lens.end(), B[i]) - lens.begin();\n G.addedge(i, aid+N, 1, -B[i]);\n G.addedge(i, bid+N, 1, -A[i]);\n }\n \n int res = -MinCostFlow(G, source, sink, N);\n cout << res << endl;\n}", "accuracy": 1, "time_ms": 180, "memory_kb": 3928, "score_of_the_acc": -0.1381, "final_rank": 9 }, { "submission_id": "aoj_2293_7220354", "code_snippet": "#include <iostream>\n#include <vector>\n#include <queue>\n#include <algorithm>\nusing namespace std;\n\nstruct Edge {\n\tlong long to, cap, cost, rev;\n};\n\nclass MincostFlow {\n\tpublic:\n\tint size_ = 0;\n\tvector<vector<Edge>> G;\n\tvector<long long> Potential;\n\tvector<long long> Dist;\n\tvector<long long> Prev_Verts;\n\tvector<long long> Prev_ID;\n\tvector<bool> Used;\n\t\n\tvoid Initialize(int sz) {\n\t\tsize_ = sz + 2;\n\t\tG.resize(size_, vector<Edge>{});\n\t\tPotential.resize(size_, 0);\n\t\tDist.resize(size_, 0);\n\t\tPrev_Verts.resize(size_, 0);\n\t\tPrev_ID.resize(size_, 0);\n\t\tUsed.resize(size_, false);\n\t}\n\t\n\tvoid add_edge(long long u, long long v, long long f, long long c) {\n\t\t//cout << \"(u, v) = (\" << u << \", \" << v << \"), cap = \" << f << \", cost = \" << c << endl;\n\t\tG[u].push_back(Edge{ v, f, c, (int)G[v].size() });\n\t\tG[v].push_back(Edge{ u, 0LL, -c, (int)G[u].size() - 1 });\n\t}\n\t\n\tlong long mincost_flow(int s, int t, int f, int Mode) {\n\t\tlong long current_ans = 0;\n\t\tif (Mode == 1) {\n\t\t\tfor (int i = 0; i < size_; i++) Potential[i] = 0;\n\t\t}\n\t\t\n\t\t// Loop Phase\n\t\twhile (f > 0) {\n\t\t\tpriority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<pair<long long, int>>> Q;\n\t\t\tfor (int i = 0; i < size_; i++) Dist[i] = (1LL << 60);\n\t\t\tfor (int i = 0; i < size_; i++) Prev_Verts[i] = -1;\n\t\t\tfor (int i = 0; i < size_; i++) Prev_ID[i] = -1;\n\t\t\tfor (int i = 0; i < size_; i++) Used[i] = false;\n\t\t\tDist[s] = 0;\n\t\t\tQ.push(make_pair(0, s));\n\t\t\t\n\t\t\t// Dijkstra\n\t\t\twhile (!Q.empty()) {\n\t\t\t\tlong long pos = Q.top().second; Q.pop();\n\t\t\t\tif (Used[pos] == true) continue;\n\t\t\t\tUsed[pos] = true;\n\t\t\t\tfor (int i = 0; i < (int)G[pos].size(); i++) {\n\t\t\t\t\tif (G[pos][i].cap == 0) continue;\n\t\t\t\t\tlong long nex = G[pos][i].to;\n\t\t\t\t\tlong long dst = G[pos][i].cost + Potential[pos] - Potential[nex];\n\t\t\t\t\tif (Dist[nex] > Dist[pos] + dst) {\n\t\t\t\t\t\tDist[nex] = Dist[pos] + dst;\n\t\t\t\t\t\tPrev_Verts[nex] = pos;\n\t\t\t\t\t\tPrev_ID[nex] = i;\n\t\t\t\t\t\tQ.push(make_pair(Dist[nex], nex));\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (Dist[t] == (1LL << 60)) break;\n\t\t\t\n\t\t\t// Refresh Edges [Part 1]\n\t\t\tlong long current_pos = t;\n\t\t\tlong long current_min = f;\n\t\t\twhile (current_pos != s) {\n\t\t\t\tint nex = Prev_Verts[current_pos];\n\t\t\t\tint idx = Prev_ID[current_pos];\n\t\t\t\tcurrent_min = min(current_min, G[nex][idx].cap);\n\t\t\t\tcurrent_pos = nex;\n\t\t\t}\n\t\t\tcurrent_ans += 1LL * current_min * (Dist[t] + Potential[t] - Potential[s]);\n\t\t\tf -= current_min;\n\t\t\t\n\t\t\t// Refresh Edges [Part 2]\n\t\t\tcurrent_pos = t;\n\t\t\twhile (current_pos != s) {\n\t\t\t\tint nex = Prev_Verts[current_pos];\n\t\t\t\tint idx = Prev_ID[current_pos];\n\t\t\t\tG[nex][idx].cap -= current_min;\n\t\t\t\tG[G[nex][idx].to][G[nex][idx].rev].cap += current_min;\n\t\t\t\tcurrent_pos = nex;\n\t\t\t}\n\t\t\t\n\t\t\t// Refresh Potential\n\t\t\tfor (int i = 0; i < size_; i++) {\n\t\t\t\tif (Dist[i] != (1LL << 60)) Potential[i] += Dist[i];\n\t\t\t}\n\t\t}\n\t\t\n\t\t// Return\n\t\treturn current_ans;\n\t}\n};\n\nlong long N;\nlong long A[1009], B[1009];\nlong long X[2009], Y[2009];\nvector<long long> Height[1000009];\nMincostFlow Z;\n\nint main() {\n\t// Input\n\tcin >> N;\n\tfor (int i = 1; i <= N; i++) {\n\t\tcin >> A[i] >> B[i];\n\t\tX[i + 0] = A[i]; Y[i + 0] = B[i];\n\t\tX[i + N] = B[i]; Y[i + N] = A[i];\n\t}\n\tfor (int i = 1; i <= 2 * N; i++) Height[X[i]].push_back(i);\n\t\n\t// Build a Graph [Part 1]\n\tZ.Initialize(6 * N + 10);\n\tint Count = 0;\n\tfor (int i = 1; i <= 1000000; i++) {\n\t\tif (Height[i].size() == 0) continue;\n\t\tCount += 1;\n\t\tfor (int j : Height[i]) {\n\t\t\tZ.add_edge(2 * N + Count, j, 1, 0);\n\t\t} \n\t}\n\tfor (int i = 1; i <= N; i++) {\n\t\tZ.add_edge(i + 0, 2 * N + Count + i, 1, 1200000000LL - Y[i + 0]);\n\t\tZ.add_edge(i + N, 2 * N + Count + i, 1, 1200000000LL - Y[i + N]);\n\t}\n\t\n\t// Build a Graph [Part 2]\n\tfor (int i = 1; i <= Count; i++) Z.add_edge(3 * N + Count + 1, 2 * N + i , 1, 0);\n\tfor (int i = 1; i <= N; i++) Z.add_edge(2 * N + Count + i, 3 * N + Count + 2, 1, 0);\n\t\n\t// Get Answer\n\tlong long FinalAns = 0, current_cost = 0;\n\tfor (int i = 1; i <= N; i++) {\n\t\tlong long val = Z.mincost_flow(3 * N + Count + 1, 3 * N + Count + 2, 1, 2);\n\t\tif (val == 0) break;\n\t\tcurrent_cost += val;\n\t\tFinalAns = max(FinalAns, 1200000000LL * i - current_cost);\n\t}\n\t\n\t// Output\n\tcout << FinalAns << endl;\n\treturn 0;\n}", "accuracy": 1, "time_ms": 390, "memory_kb": 27780, "score_of_the_acc": -0.8598, "final_rank": 17 }, { "submission_id": "aoj_2293_7220330", "code_snippet": "#include <iostream>\n#include <vector>\n#include <queue>\n#include <algorithm>\nusing namespace std;\n\nstruct Edge {\n\tlong long to, cap, cost, rev;\n};\n\nclass MincostFlow {\n\tpublic:\n\tint size_ = 0;\n\tvector<vector<Edge>> G;\n\tvector<long long> Potential;\n\tvector<long long> Dist;\n\tvector<long long> Prev_Verts;\n\tvector<long long> Prev_ID;\n\tvector<bool> Used;\n\t\n\tvoid Initialize(int sz) {\n\t\tsize_ = sz + 2;\n\t\tG.resize(size_, vector<Edge>{});\n\t\tPotential.resize(size_, 0);\n\t\tDist.resize(size_, 0);\n\t\tPrev_Verts.resize(size_, 0);\n\t\tPrev_ID.resize(size_, 0);\n\t\tUsed.resize(size_, false);\n\t}\n\t\n\tvoid add_edge(long long u, long long v, long long f, long long c) {\n\t\t//cout << \"(u, v) = (\" << u << \", \" << v << \"), cap = \" << f << \", cost = \" << c << endl;\n\t\tG[u].push_back(Edge{ v, f, c, (int)G[v].size() });\n\t\tG[v].push_back(Edge{ u, 0LL, -c, (int)G[u].size() - 1 });\n\t}\n\t\n\tlong long mincost_flow(int s, int t, int f, int Mode) {\n\t\tlong long current_ans = 0;\n\t\tif (Mode == 1) {\n\t\t\tfor (int i = 0; i < size_; i++) Potential[i] = 0;\n\t\t}\n\t\t\n\t\t// Loop Phase\n\t\twhile (f > 0) {\n\t\t\tpriority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<pair<long long, int>>> Q;\n\t\t\tfor (int i = 0; i < size_; i++) Dist[i] = (1LL << 60);\n\t\t\tfor (int i = 0; i < size_; i++) Prev_Verts[i] = -1;\n\t\t\tfor (int i = 0; i < size_; i++) Prev_ID[i] = -1;\n\t\t\tfor (int i = 0; i < size_; i++) Used[i] = false;\n\t\t\tDist[s] = 0;\n\t\t\tQ.push(make_pair(0, s));\n\t\t\t\n\t\t\t// Dijkstra\n\t\t\twhile (!Q.empty()) {\n\t\t\t\tlong long pos = Q.top().second; Q.pop();\n\t\t\t\tif (Used[pos] == true) continue;\n\t\t\t\tUsed[pos] = true;\n\t\t\t\tfor (int i = 0; i < (int)G[pos].size(); i++) {\n\t\t\t\t\tif (G[pos][i].cap == 0) continue;\n\t\t\t\t\tlong long nex = G[pos][i].to;\n\t\t\t\t\tlong long dst = G[pos][i].cost + Potential[pos] - Potential[nex];\n\t\t\t\t\tif (Dist[nex] > Dist[pos] + dst) {\n\t\t\t\t\t\tDist[nex] = Dist[pos] + dst;\n\t\t\t\t\t\tPrev_Verts[nex] = pos;\n\t\t\t\t\t\tPrev_ID[nex] = i;\n\t\t\t\t\t\tQ.push(make_pair(Dist[nex], nex));\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\t\n\t\t\t// Refresh Edges [Part 1]\n\t\t\tlong long current_pos = t;\n\t\t\tlong long current_min = f;\n\t\t\twhile (current_pos != s) {\n\t\t\t\tint nex = Prev_Verts[current_pos];\n\t\t\t\tint idx = Prev_ID[current_pos];\n\t\t\t\tcurrent_min = min(current_min, G[nex][idx].cap);\n\t\t\t\tcurrent_pos = nex;\n\t\t\t}\n\t\t\tcurrent_ans += 1LL * current_min * (Dist[t] + Potential[t] - Potential[s]);\n\t\t\tf -= current_min;\n\t\t\t\n\t\t\t// Refresh Edges [Part 2]\n\t\t\tcurrent_pos = t;\n\t\t\twhile (current_pos != s) {\n\t\t\t\tint nex = Prev_Verts[current_pos];\n\t\t\t\tint idx = Prev_ID[current_pos];\n\t\t\t\tG[nex][idx].cap -= current_min;\n\t\t\t\tG[G[nex][idx].to][G[nex][idx].rev].cap += current_min;\n\t\t\t\tcurrent_pos = nex;\n\t\t\t}\n\t\t\t\n\t\t\t// Refresh Potential\n\t\t\tif (Dist[t] == (1LL << 60)) break;\n\t\t\tfor (int i = 0; i < size_; i++) {\n\t\t\t\tif (Dist[i] != (1LL << 60)) Potential[i] += Dist[i];\n\t\t\t}\n\t\t}\n\t\t\n\t\t// Return\n\t\treturn current_ans;\n\t}\n};\n\nlong long N;\nlong long A[1009], B[1009];\nlong long X[2009], Y[2009];\nvector<long long> Height[1000009];\nMincostFlow Z;\n\nint main() {\n\t// Input\n\tcin >> N;\n\tfor (int i = 1; i <= N; i++) {\n\t\tcin >> A[i] >> B[i];\n\t\tX[i + 0] = A[i]; Y[i + 0] = B[i];\n\t\tX[i + N] = B[i]; Y[i + N] = A[i];\n\t}\n\tfor (int i = 1; i <= 2 * N; i++) Height[X[i]].push_back(i);\n\t\n\t// Build a Graph [Part 1]\n\tZ.Initialize(6 * N + 10);\n\tint Count = 0;\n\tfor (int i = 1; i <= 1000000; i++) {\n\t\tif (Height[i].size() == 0) continue;\n\t\tCount += 1;\n\t\tfor (int j : Height[i]) {\n\t\t\tZ.add_edge(2 * N + Count, j, 1, 0);\n\t\t} \n\t}\n\tfor (int i = 1; i <= N; i++) {\n\t\tZ.add_edge(i + 0, 2 * N + Count + i, 1, 1200000000LL - Y[i + 0]);\n\t\tZ.add_edge(i + N, 2 * N + Count + i, 1, 1200000000LL - Y[i + N]);\n\t}\n\t\n\t// Build a Graph [Part 2]\n\tfor (int i = 1; i <= Count; i++) Z.add_edge(3 * N + Count + 1, 2 * N + i , 1, 0);\n\tfor (int i = 1; i <= N; i++) Z.add_edge(2 * N + Count + i, 3 * N + Count + 2, 1, 0);\n\t\n\t// Get Answer\n\tlong long FinalAns = 0, current_cost = 0;\n\tfor (int i = 1; i <= N; i++) {\n\t\tlong long val = Z.mincost_flow(3 * N + Count + 1, 3 * N + Count + 2, 1, 2);\n\t\tif (val == 0) break;\n\t\tcurrent_cost += val;\n\t\tFinalAns = max(FinalAns, 1200000000LL * i - current_cost);\n\t}\n\t\n\t// Output\n\tcout << FinalAns << endl;\n\treturn 0;\n}", "accuracy": 0.16666666666666666, "time_ms": 360, "memory_kb": 27688, "score_of_the_acc": -0.8339, "final_rank": 20 }, { "submission_id": "aoj_2293_7151828", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\ntemplate <class F, class C> struct primal_dual {\n struct edge {\n int to;\n F cap;\n C cost;\n int rev;\n bool is_rev;\n };\n\n int n;\n vector<vector<edge>> g;\n vector<C> pot, d;\n vector<int> pv, pe;\n const C inf = numeric_limits<C>::max();\n\n primal_dual(int nn) : n(nn), g(nn) {}\n\n void add_edge(int from, int to, F cap, C cost) {\n g[from].push_back({to, cap, cost, (int)g[to].size(), false});\n g[to].push_back({from, 0, -cost, (int)g[from].size() - 1, true});\n }\n\n vector<pair<F, C>> min_cost_flow(int s, int t, F f_lim = numeric_limits<F>::max()) {\n F f = 0;\n C c = 0, cf = -1;\n vector<pair<F, C>> res = {{0, 0}};\n priority_queue<pair<C, int>, vector<pair<C, int>>, greater<pair<C, int>>> pq;\n pot.assign(n, 0);\n pe.assign(n, -1);\n pv.assign(n, -1);\n while (f < f_lim) {\n d.assign(n, inf);\n pq.emplace(0, s);\n d[s] = 0;\n while (!pq.empty()) {\n auto p = pq.top();\n C dv = p.first;\n int v = p.second;\n pq.pop();\n if (d[v] != dv) continue;\n for (int i = 0; i < (int)g[v].size(); i++) {\n auto& e = g[v][i];\n C nd = d[v] + e.cost + pot[v] - pot[e.to];\n if (e.cap > 0 and d[e.to] > nd) {\n d[e.to] = nd;\n pv[e.to] = v;\n pe[e.to] = i;\n pq.emplace(d[e.to], e.to);\n }\n }\n }\n if (d[t] == inf) break;\n for (int i = 0; i < n; i++) pot[i] += d[i];\n F add = f_lim - f;\n for (int v = t; v != s; v = pv[v]) {\n add = min(add, g[pv[v]][pe[v]].cap);\n }\n for (int v = t; v != s; v = pv[v]) {\n auto& e = g[pv[v]][pe[v]];\n e.cap -= add;\n g[v][e.rev].cap += add;\n }\n f += add;\n c += add * pot[t];\n if (cf == pot[t]) {\n res.pop_back();\n }\n cf = pot[t];\n res.emplace_back(f, c);\n }\n return res;\n }\n};\n\nconstexpr int MAX = 1000010;\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n int N;\n cin >> N;\n vector<int> A(N), B(N), C;\n for (int i = 0; i < N; i++) {\n cin >> A[i] >> B[i];\n C.emplace_back(A[i]);\n C.emplace_back(B[i]);\n }\n\n sort(C.begin(), C.end());\n C.erase(unique(C.begin(), C.end()), C.end());\n vector<int> idx(MAX, -1);\n int M = C.size();\n for (int i = 0; i < M; i++) idx[C[i]] = i;\n int s = N + M, t = s + 1;\n primal_dual<int, int> mcf(t + 1);\n for (int i = 0; i < N; i++) {\n mcf.add_edge(s, i, 1, 0);\n mcf.add_edge(i, N + idx[A[i]], 1, MAX - B[i]);\n mcf.add_edge(i, N + idx[B[i]], 1, MAX - A[i]);\n mcf.add_edge(i, t, 1, MAX);\n }\n for (int i = 0; i < M; i++) mcf.add_edge(N + i, t, 1, 0);\n int ans = MAX * N - mcf.min_cost_flow(s, t).back().second;\n cout << ans << '\\n';\n}", "accuracy": 1, "time_ms": 190, "memory_kb": 7584, "score_of_the_acc": -0.2311, "final_rank": 16 }, { "submission_id": "aoj_2293_7100339", "code_snippet": "#pragma GCC target(\"avx2\")\n#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"unroll-loops\")\n#include<bits/stdc++.h>\n\n#include <algorithm>\n#include <cassert>\n#include <limits>\n#include <queue>\n#include <vector>\n\nnamespace atcoder {\n\ntemplate <class Cap, class Cost> struct mcf_graph {\n public:\n mcf_graph() {}\n mcf_graph(int n) : _n(n), g(n) {}\n\n int add_edge(int from, int to, Cap cap, Cost cost) {\n assert(0 <= from && from < _n);\n assert(0 <= to && to < _n);\n int m = int(pos.size());\n pos.push_back({from, int(g[from].size())});\n g[from].push_back(_edge{to, int(g[to].size()), cap, cost});\n g[to].push_back(_edge{from, int(g[from].size()) - 1, 0, -cost});\n return m;\n }\n\n struct edge {\n int from, to;\n Cap cap, flow;\n Cost cost;\n };\n\n edge get_edge(int i) {\n int m = int(pos.size());\n assert(0 <= i && i < m);\n auto _e = g[pos[i].first][pos[i].second];\n auto _re = g[_e.to][_e.rev];\n return edge{\n pos[i].first, _e.to, _e.cap + _re.cap, _re.cap, _e.cost,\n };\n }\n std::vector<edge> edges() {\n int m = int(pos.size());\n std::vector<edge> result(m);\n for (int i = 0; i < m; i++) {\n result[i] = get_edge(i);\n }\n return result;\n }\n\n std::pair<Cap, Cost> flow(int s, int t) {\n return flow(s, t, std::numeric_limits<Cap>::max());\n }\n std::pair<Cap, Cost> flow(int s, int t, Cap flow_limit) {\n return slope(s, t, flow_limit).back();\n }\n std::vector<std::pair<Cap, Cost>> slope(int s, int t) {\n return slope(s, t, std::numeric_limits<Cap>::max());\n }\n std::vector<std::pair<Cap, Cost>> slope(int s, int t, Cap flow_limit) {\n assert(0 <= s && s < _n);\n assert(0 <= t && t < _n);\n assert(s != t);\n // variants (C = maxcost):\n // -(n-1)C <= dual[s] <= dual[i] <= dual[t] = 0\n // reduced cost (= e.cost + dual[e.from] - dual[e.to]) >= 0 for all edge\n std::vector<Cost> dual(_n, 0), dist(_n);\n std::vector<int> pv(_n), pe(_n);\n std::vector<bool> vis(_n);\n auto dual_ref = [&]() {\n std::fill(dist.begin(), dist.end(),\n std::numeric_limits<Cost>::max());\n std::fill(pv.begin(), pv.end(), -1);\n std::fill(pe.begin(), pe.end(), -1);\n std::fill(vis.begin(), vis.end(), false);\n struct Q {\n Cost key;\n int to;\n bool operator<(Q r) const { return key > r.key; }\n };\n std::priority_queue<Q> que;\n dist[s] = 0;\n que.push(Q{0, s});\n while (!que.empty()) {\n int v = que.top().to;\n que.pop();\n if (vis[v]) continue;\n vis[v] = true;\n if (v == t) break;\n // dist[v] = shortest(s, v) + dual[s] - dual[v]\n // dist[v] >= 0 (all reduced cost are positive)\n // dist[v] <= (n-1)C\n for (int i = 0; i < int(g[v].size()); i++) {\n auto e = g[v][i];\n if (vis[e.to] || !e.cap) continue;\n // |-dual[e.to] + dual[v]| <= (n-1)C\n // cost <= C - -(n-1)C + 0 = nC\n Cost cost = e.cost - dual[e.to] + dual[v];\n if (dist[e.to] - dist[v] > cost) {\n dist[e.to] = dist[v] + cost;\n pv[e.to] = v;\n pe[e.to] = i;\n que.push(Q{dist[e.to], e.to});\n }\n }\n }\n if (!vis[t]) {\n return false;\n }\n\n for (int v = 0; v < _n; v++) {\n if (!vis[v]) continue;\n // dual[v] = dual[v] - dist[t] + dist[v]\n // = dual[v] - (shortest(s, t) + dual[s] - dual[t]) + (shortest(s, v) + dual[s] - dual[v])\n // = - shortest(s, t) + dual[t] + shortest(s, v)\n // = shortest(s, v) - shortest(s, t) >= 0 - (n-1)C\n dual[v] -= dist[t] - dist[v];\n }\n return true;\n };\n Cap flow = 0;\n Cost cost = 0, prev_cost = -1;\n std::vector<std::pair<Cap, Cost>> result;\n result.push_back({flow, cost});\n while (flow < flow_limit) {\n if (!dual_ref()) break;\n Cap c = flow_limit - flow;\n for (int v = t; v != s; v = pv[v]) {\n c = std::min(c, g[pv[v]][pe[v]].cap);\n }\n for (int v = t; v != s; v = pv[v]) {\n auto& e = g[pv[v]][pe[v]];\n e.cap -= c;\n g[v][e.rev].cap += c;\n }\n Cost d = -dual[s];\n flow += c;\n cost += c * d;\n if (prev_cost == d) {\n result.pop_back();\n }\n result.push_back({flow, cost});\n prev_cost = cost;\n }\n return result;\n }\n\n private:\n int _n;\n\n struct _edge {\n int to, rev;\n Cap cap;\n Cost cost;\n };\n\n std::vector<std::pair<int, int>> pos;\n std::vector<std::vector<_edge>> g;\n};\n\n} // namespace atcoder\n\nusing namespace std;\nusing namespace atcoder;\nconst long long inf = 1LL << 40;\n\n\n\nvoid solve(){\n\tint n;\n\tcin >> n;\n\tvector<long long> A(n), B(n);\n\tset<long long> se;\n\tfor(int i = 0; i < n; i++){\n\t\tcin >> A[i] >> B[i];\n\t\tse.insert(A[i]);\n\t\tse.insert(B[i]);\n\t}\n\tmap<long long, int> mp;\n\tint ind = 0;\n\tfor(auto s:se){\n\t\tmp[s] = ind++;\n\t}\n\n\tint m = ind;\n\tmcf_graph<int, long long> G(n + m + 2);\n\tint s = n + m;\n\tint t = s + 1;\n\tfor(int i = 0; i < n; i++){\n\t\tG.add_edge(s, i, 1, 0);\n\t\tG.add_edge(i, t, 1, inf);\n\t}\n\tfor(int j = 0; j < m; j++) G.add_edge(n + j, t, 1, 0);\n\tfor(int i = 0; i < n; i++){\n\t\tG.add_edge(i, n + mp[A[i]], 1, inf - B[i]);\n\t\tG.add_edge(i, n + mp[B[i]], 1, inf - A[i]);\n\t}\n\tauto tmp = G.flow(s, t);\n\tlong long ans = inf * tmp.first - tmp.second;\n\tcout << ans << \"\\n\";\n}\n\nint main(){\n\tcin.tie(0)->sync_with_stdio(0);\n\tint t;\n\tt = 1;\n\t// cin >> t;\n\twhile(t--) solve();\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 4216, "score_of_the_acc": -0.0337, "final_rank": 5 }, { "submission_id": "aoj_2293_7070454", "code_snippet": "#ifndef HIDDEN_IN_VS // 折りたたみ用\n\n// 警告の抑制\n#define _CRT_SECURE_NO_WARNINGS\n\n// ライブラリの読み込み\n#include <bits/stdc++.h>\nusing namespace std;\n\n// 型名の短縮\nusing ll = long long; // -2^63 ~ 2^63 = 9 * 10^18(int は -2^31 ~ 2^31 = 2 * 10^9)\nusing pii = pair<int, int>;\tusing pll = pair<ll, ll>;\tusing pil = pair<int, ll>;\tusing pli = pair<ll, int>;\nusing vi = vector<int>;\t\tusing vvi = vector<vi>;\t\tusing vvvi = vector<vvi>;\nusing vl = vector<ll>;\t\tusing vvl = vector<vl>;\t\tusing vvvl = vector<vvl>;\nusing vb = vector<bool>;\tusing vvb = vector<vb>;\t\tusing vvvb = vector<vvb>;\nusing vc = vector<char>;\tusing vvc = vector<vc>;\t\tusing vvvc = vector<vvc>;\nusing vd = vector<double>;\tusing vvd = vector<vd>;\t\tusing vvvd = vector<vvd>;\ntemplate <class T> using priority_queue_rev = priority_queue<T, vector<T>, greater<T>>;\nusing Graph = vvi;\n\n// 定数の定義\nconst double PI = acos(-1);\nconst vi DX = { 1, 0, -1, 0 }; // 4 近傍(下,右,上,左)\nconst vi DY = { 0, 1, 0, -1 };\nint INF = 1001001001; ll INFL = 4004004004004004004LL;\ndouble EPS = 1e-12;\n\n// 入出力高速化\nstruct fast_io { fast_io() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(18); } } fastIOtmp;\n\n// 汎用マクロの定義\n#define all(a) (a).begin(), (a).end()\n#define sz(x) ((int)(x).size())\n#define lbpos(a, x) (int)distance((a).begin(), std::lower_bound(all(a), x))\n#define ubpos(a, x) (int)distance((a).begin(), std::upper_bound(all(a), x))\n#define Yes(b) {cout << ((b) ? \"Yes\\n\" : \"No\\n\");}\n#define rep(i, n) for(int i = 0, i##_len = int(n); i < i##_len; ++i) // 0 から n-1 まで昇順\n#define repi(i, s, t) for(int i = int(s), i##_end = int(t); i <= i##_end; ++i) // s から t まで昇順\n#define repir(i, s, t) for(int i = int(s), i##_end = int(t); i >= i##_end; --i) // s から t まで降順\n#define repe(v, a) for(const auto& v : (a)) // a の全要素(変更不可能)\n#define repea(v, a) for(auto& v : (a)) // a の全要素(変更可能)\n#define repb(set, d) for(int set = 0; set < (1 << int(d)); ++set) // d ビット全探索(昇順)\n#define repp(a) sort(all(a)); for(bool a##_perm = true; a##_perm; a##_perm = next_permutation(all(a))) // a の順列全て(昇順)\n#define smod(n, m) ((((n) % (m)) + (m)) % (m)) // 非負mod\n#define uniq(a) {sort(all(a)); (a).erase(unique(all(a)), (a).end());} // 重複除去\n#define EXIT(a) {cout << (a) << endl; exit(0);} // 強制終了\n\n// 汎用関数の定義\ntemplate <class T> inline ll pow(T n, int k) { ll v = 1; rep(i, k) v *= n; return v; }\ntemplate <class T> inline bool chmax(T& M, const T& x) { if (M < x) { M = x; return true; } return false; } // 最大値を更新(更新されたら true を返す)\ntemplate <class T> inline bool chmin(T& m, const T& x) { if (m > x) { m = x; return true; } return false; } // 最小値を更新(更新されたら true を返す)\n\n// 演算子オーバーロード\ntemplate <class T, class U> inline istream& operator>>(istream& is, pair<T, U>& p) { is >> p.first >> p.second; return is; }\ntemplate <class T> inline istream& operator>>(istream& is, vector<T>& v) { repea(x, v) is >> x; return is; }\ntemplate <class T> inline vector<T>& operator--(vector<T>& v) { repea(x, v) --x; return v; }\ntemplate <class T> inline vector<T>& operator++(vector<T>& v) { repea(x, v) ++x; return v; }\n\n// 手元環境(Visual Studio)\n#ifdef _MSC_VER\n#include \"local.hpp\"\n// 提出用(gcc)\n#else\ninline int popcount(int n) { return __builtin_popcount(n); }\ninline int popcount(ll n) { return __builtin_popcountll(n); }\ninline int lsb(int n) { return n != 0 ? __builtin_ctz(n) : -1; }\ninline int lsb(ll n) { return n != 0 ? __builtin_ctzll(n) : -1; }\ninline int msb(int n) { return n != 0 ? (31 - __builtin_clz(n)) : -1; }\ninline int msb(ll n) { return n != 0 ? (63 - __builtin_clzll(n)) : -1; }\n#define gcd __gcd\n#define dump(...)\n#define dumpel(v)\n#define dump_list(v)\n#define input_from_file(f)\n#define output_to_file(f)\n#define Assert(b) { if (!(b)) while (1) cout << \"OLE\"; }\n#endif\n\n#endif // 折りたたみ用\n\n\n////--------------AtCoder 専用--------------\n//#include <atcoder/all>\n//using namespace atcoder;\n//\n////using mint = modint1000000007;\n//using mint = modint998244353;\n////using mint = modint; // mint::set_mod(m);\n//\n//istream& operator>>(istream& is, mint& x) { ll x_; is >> x_; x = x_; return is; }\n//ostream& operator<<(ostream& os, const mint& x) { os << x.val(); return os; }\n//using vm = vector<mint>; using vvm = vector<vm>; using vvvm = vector<vvm>;\n////----------------------------------------\n\n\n#ifndef ATCODER_INTERNAL_CSR_HPP\n#define ATCODER_INTERNAL_CSR_HPP 1\n\n#include <algorithm>\n#include <utility>\n#include <vector>\n\nnamespace atcoder {\n namespace internal {\n\n template <class E> struct csr {\n std::vector<int> start;\n std::vector<E> elist;\n explicit csr(int n, const std::vector<std::pair<int, E>>& edges)\n : start(n + 1), elist(edges.size()) {\n for (auto e : edges) {\n start[e.first + 1]++;\n }\n for (int i = 1; i <= n; i++) {\n start[i] += start[i - 1];\n }\n auto counter = start;\n for (auto e : edges) {\n elist[counter[e.first]++] = e.second;\n }\n }\n };\n\n } // namespace internal\n\n} // namespace atcoder\n\n#endif // ATCODER_INTERNAL_CSR_HPP\n\n\n#ifndef ATCODER_INTERNAL_QUEUE_HPP\n#define ATCODER_INTERNAL_QUEUE_HPP 1\n\n#include <vector>\n\nnamespace atcoder {\n\n namespace internal {\n\n template <class T> struct simple_queue {\n std::vector<T> payload;\n int pos = 0;\n void reserve(int n) { payload.reserve(n); }\n int size() const { return int(payload.size()) - pos; }\n bool empty() const { return pos == int(payload.size()); }\n void push(const T& t) { payload.push_back(t); }\n T& front() { return payload[pos]; }\n void clear() {\n payload.clear();\n pos = 0;\n }\n void pop() { pos++; }\n };\n\n } // namespace internal\n\n} // namespace atcoder\n\n#endif // ATCODER_INTERNAL_QUEUE_HPP\n\n\n#ifndef ATCODER_MINCOSTFLOW_HPP\n#define ATCODER_MINCOSTFLOW_HPP 1\n\n#include <algorithm>\n#include <cassert>\n#include <limits>\n#include <queue>\n#include <vector>\n\nnamespace atcoder {\n\n template <class Cap, class Cost> struct mcf_graph {\n public:\n mcf_graph() {}\n explicit mcf_graph(int n) : _n(n) {}\n\n int add_edge(int from, int to, Cap cap, Cost cost) {\n assert(0 <= from && from < _n);\n assert(0 <= to && to < _n);\n assert(0 <= cap);\n assert(0 <= cost);\n int m = int(_edges.size());\n _edges.push_back({ from, to, cap, 0, cost });\n return m;\n }\n\n struct edge {\n int from, to;\n Cap cap, flow;\n Cost cost;\n };\n\n edge get_edge(int i) {\n int m = int(_edges.size());\n assert(0 <= i && i < m);\n return _edges[i];\n }\n std::vector<edge> edges() { return _edges; }\n\n std::pair<Cap, Cost> flow(int s, int t) {\n return flow(s, t, std::numeric_limits<Cap>::max());\n }\n std::pair<Cap, Cost> flow(int s, int t, Cap flow_limit) {\n return slope(s, t, flow_limit).back();\n }\n std::vector<std::pair<Cap, Cost>> slope(int s, int t) {\n return slope(s, t, std::numeric_limits<Cap>::max());\n }\n std::vector<std::pair<Cap, Cost>> slope(int s, int t, Cap flow_limit) {\n assert(0 <= s && s < _n);\n assert(0 <= t && t < _n);\n assert(s != t);\n\n int m = int(_edges.size());\n std::vector<int> edge_idx(m);\n\n auto g = [&]() {\n std::vector<int> degree(_n), redge_idx(m);\n std::vector<std::pair<int, _edge>> elist;\n elist.reserve(2 * m);\n for (int i = 0; i < m; i++) {\n auto e = _edges[i];\n edge_idx[i] = degree[e.from]++;\n redge_idx[i] = degree[e.to]++;\n elist.push_back({ e.from, {e.to, -1, e.cap - e.flow, e.cost} });\n elist.push_back({ e.to, {e.from, -1, e.flow, -e.cost} });\n }\n auto _g = internal::csr<_edge>(_n, elist);\n for (int i = 0; i < m; i++) {\n auto e = _edges[i];\n edge_idx[i] += _g.start[e.from];\n redge_idx[i] += _g.start[e.to];\n _g.elist[edge_idx[i]].rev = redge_idx[i];\n _g.elist[redge_idx[i]].rev = edge_idx[i];\n }\n return _g;\n }();\n\n auto result = slope(g, s, t, flow_limit);\n\n for (int i = 0; i < m; i++) {\n auto e = g.elist[edge_idx[i]];\n _edges[i].flow = _edges[i].cap - e.cap;\n }\n\n return result;\n }\n\n private:\n int _n;\n std::vector<edge> _edges;\n\n // inside edge\n struct _edge {\n int to, rev;\n Cap cap;\n Cost cost;\n };\n\n std::vector<std::pair<Cap, Cost>> slope(internal::csr<_edge>& g,\n int s,\n int t,\n Cap flow_limit) {\n // variants (C = maxcost):\n // -(n-1)C <= dual[s] <= dual[i] <= dual[t] = 0\n // reduced cost (= e.cost + dual[e.from] - dual[e.to]) >= 0 for all edge\n\n // dual_dist[i] = (dual[i], dist[i])\n std::vector<std::pair<Cost, Cost>> dual_dist(_n);\n std::vector<int> prev_e(_n);\n std::vector<bool> vis(_n);\n struct Q {\n Cost key;\n int to;\n bool operator<(Q r) const { return key > r.key; }\n };\n std::vector<int> que_min;\n std::vector<Q> que;\n auto dual_ref = [&]() {\n for (int i = 0; i < _n; i++) {\n dual_dist[i].second = std::numeric_limits<Cost>::max();\n }\n std::fill(vis.begin(), vis.end(), false);\n que_min.clear();\n que.clear();\n\n // que[0..heap_r) was heapified\n size_t heap_r = 0;\n\n dual_dist[s].second = 0;\n que_min.push_back(s);\n while (!que_min.empty() || !que.empty()) {\n int v;\n if (!que_min.empty()) {\n v = que_min.back();\n que_min.pop_back();\n }\n else {\n while (heap_r < que.size()) {\n heap_r++;\n std::push_heap(que.begin(), que.begin() + heap_r);\n }\n v = que.front().to;\n std::pop_heap(que.begin(), que.end());\n que.pop_back();\n heap_r--;\n }\n if (vis[v]) continue;\n vis[v] = true;\n if (v == t) break;\n // dist[v] = shortest(s, v) + dual[s] - dual[v]\n // dist[v] >= 0 (all reduced cost are positive)\n // dist[v] <= (n-1)C\n Cost dual_v = dual_dist[v].first, dist_v = dual_dist[v].second;\n for (int i = g.start[v]; i < g.start[v + 1]; i++) {\n auto e = g.elist[i];\n if (!e.cap) continue;\n // |-dual[e.to] + dual[v]| <= (n-1)C\n // cost <= C - -(n-1)C + 0 = nC\n Cost cost = e.cost - dual_dist[e.to].first + dual_v;\n if (dual_dist[e.to].second - dist_v > cost) {\n Cost dist_to = dist_v + cost;\n dual_dist[e.to].second = dist_to;\n prev_e[e.to] = e.rev;\n if (dist_to == dist_v) {\n que_min.push_back(e.to);\n }\n else {\n que.push_back(Q{ dist_to, e.to });\n }\n }\n }\n }\n if (!vis[t]) {\n return false;\n }\n\n for (int v = 0; v < _n; v++) {\n if (!vis[v]) continue;\n // dual[v] = dual[v] - dist[t] + dist[v]\n // = dual[v] - (shortest(s, t) + dual[s] - dual[t]) +\n // (shortest(s, v) + dual[s] - dual[v]) = - shortest(s,\n // t) + dual[t] + shortest(s, v) = shortest(s, v) -\n // shortest(s, t) >= 0 - (n-1)C\n dual_dist[v].first -= dual_dist[t].second - dual_dist[v].second;\n }\n return true;\n };\n Cap flow = 0;\n Cost cost = 0, prev_cost_per_flow = -1;\n std::vector<std::pair<Cap, Cost>> result = { {Cap(0), Cost(0)} };\n while (flow < flow_limit) {\n if (!dual_ref()) break;\n Cap c = flow_limit - flow;\n for (int v = t; v != s; v = g.elist[prev_e[v]].to) {\n c = std::min(c, g.elist[g.elist[prev_e[v]].rev].cap);\n }\n for (int v = t; v != s; v = g.elist[prev_e[v]].to) {\n auto& e = g.elist[prev_e[v]];\n e.cap += c;\n g.elist[e.rev].cap -= c;\n }\n Cost d = -dual_dist[s].first;\n flow += c;\n cost += c * d;\n if (prev_cost_per_flow == d) {\n result.pop_back();\n }\n result.push_back({ flow, cost });\n prev_cost_per_flow = d;\n }\n return result;\n }\n };\n\n} // namespace atcoder\n\n#endif // ATCODER_MINCOSTFLOW_HPP\n\n\nusing namespace atcoder;\n\n\n//【最小費用流(負コスト可,負コスト閉路なし)】\n/*\n* Negative_mcf_graph(int n) : O(1)\n*\tn 頂点で初期化する.\n* \n* add_edge(int s, int t, ll cap, ll cost) : O(1)\n*\ts から t へ容量 cap,コスト cost の辺を追加する.\n* \n* pll flow(int ST, int GL, ll f_lim = INFL) : O(F (n + m) log n)(F:流量,m:辺の数)\n*\tST から GL まで flow_limit まで流せるだけ流したときの {流量, 最小コスト} を返す.\n*\t制約:負コストの閉路は存在しない\n*/\nstruct Negative_mcf_graph {\n\t// 参考 : https://ikatakos.com/pot/programming_algorithm/graph_theory/minimum_cost_flow\n\n\t// n : 頂点数\n\tint n;\n\n\t// 辺\n\tstruct Edge {\n\t\tint to;\n\t\tll cap, cost;\n\n\t\tEdge(int to_, ll cap_, ll cost_) : to(to_), cap(cap_), cost(cost_) {}\n\t};\n\n\t// 元のグラフ(負辺あり)\n\tvector<vector<Edge>> g;\n\n\t// pot[s] : 頂点 s のポテンシャル\n\tvl pot;\n\n\t// n 頂点で初期化する.\n\tNegative_mcf_graph(int n_) : n(n_), g(n), pot(n, INFL) {}\n\n\t// s から t へ容量 cap,コスト cost の辺を追加する.\n\tvoid add_edge(int s, int t, ll cap, ll cost) {\n\t\t// verify : https://atcoder.jp/contests/code-festival-2014-china-open/tasks/code_festival_china_h\n\t\t\n\t\tg[s].emplace_back(t, cap, cost);\n\t}\n\n\t// ベルマンフォード法で ST からの距離を求め,それをポテンシャルとする.\n\tbool bellman_ford(int ST) {\n\t\tpot[ST] = 0;\n\n\t\trep(i, n) {\n\t\t\tbool updated = false;\n\n\t\t\t// 全ての辺についての操作\n\t\t\trep(s, n) repe(e, g[s]) {\n\t\t\t\t// もし (始点への距離) + (辺のコスト) < (終点への距離) なら (終点への距離) を更新する.\n\t\t\t\t// INFL からは何を引いても INFL になるようにしているので,ST から到達可能な負閉路しか検出しない.\n\t\t\t\tif (pot[s] != INFL && pot[s] + e.cost < pot[e.to]) {\n\t\t\t\t\tpot[e.to] = pot[s] + e.cost;\n\t\t\t\t\tupdated = true;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// もし距離の更新が起こらなければ最短距離確定\n\t\t\tif (!updated) return true;\n\t\t}\n\n\t\t// もし全ての辺についての操作を n 回繰り返しても距離の更新があったなら,\n\t\t// ST から到達可能な負の閉路を持っているので false を返す.\n\t\treturn false;\n\t}\n\n\t// ST から GL まで flow_limit まで流せるだけ流したときの {流量, 最小コスト} を返す.\n\tpll flow(int ST, int GL, ll f_lim = INFL) {\n\t\t// verify : https://atcoder.jp/contests/code-festival-2014-china-open/tasks/code_festival_china_h\n\n\t\t// ベルマンフォード法で ST から v までの距離を求め,それをポテンシャル pot[v] とする.\n\t\tbool non_neg_cyc = bellman_ford(ST);\n\t\tAssert(non_neg_cyc);\n\n\t\t// g_pos : 辺 s→t のコストが (元の辺のコスト) - (pot[t] - pot[s]) >= 0 であるようなグラフ\n\t\tmcf_graph<ll, ll> g_pos(n);\n\t\trep(s, n) repe(e, g[s]) {\n\t\t\tg_pos.add_edge(s, e.to, e.cap, e.cost - (pot[e.to] - pot[s]));\n\t\t}\n\n\t\t// g_pos の最小費用流を求める.\n\t\tll cap, cost;\n\t\ttie(cap, cost) = g_pos.flow(ST, GL, f_lim);\n\n\t\t// 実際のコストは (流量) * pot[GL] を加えたものになる.\n\t\tcost += cap * pot[GL];\n\n\t\treturn make_pair(cap, cost);\n\t}\n};\n\n\nint main() {\n//\tinput_from_file(\"input.txt\");\n//\toutput_to_file(\"output.txt\");\n\t\n\tint n;\n\tcin >> n;\n\n\tvl a(n), b(n);\n\trep(i, n) cin >> a[i] >> b[i];\n\n\tvector<tuple<ll, ll, int>> whi(2 * n);\n\tunordered_map<ll, int> w_to_id; int id = 0;\n\n\trep(i, n) {\n\t\twhi[2 * i] = { a[i], b[i], i };\n\t\tif (!w_to_id.count(a[i])) w_to_id[a[i]] = id++;\n\n\t\twhi[2 * i + 1] = { b[i], a[i], i };\n\t\tif (!w_to_id.count(b[i])) w_to_id[b[i]] = id++;\n\t}\n\n\tsort(all(whi));\n\n\tint ST = n + (2 * n) + id, GL = ST + 1;\n\tNegative_mcf_graph g(GL + 1);\n\n\trep(i, n) g.add_edge(ST, i, 1, 0);\n\trep(t, 2 * n) {\n\t\tll w, h; int i;\n\t\ttie(w, h, i) = whi[t];\n\n\t\tg.add_edge(i, n + t, 1, 0);\n\t\tg.add_edge(n + t, n + 2 * n + w_to_id[w], 1, -h);\n\t}\n\trep(j, id) g.add_edge(n + 2 * n + j, GL, 1, 0);\n\n\t// グラフ内にフローを流さなくてもいいように側溝を用意する.\n\tg.add_edge(ST, GL, INFL, 0);\n\n\tll cap, cost;\n\ttie(cap, cost) = g.flow(ST, GL, n);\n\tdump(cap, cost);\n\n\tcout << -cost << endl;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 4728, "score_of_the_acc": -0.0456, "final_rank": 6 }, { "submission_id": "aoj_2293_6900014", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\ntemplate< typename flow_t, typename cost_t >\nstruct PrimalDual {\n const cost_t INF;\n \n struct edge {\n int to;\n flow_t cap;\n cost_t cost;\n int rev;\n bool isrev;\n };\n vector< vector< edge > > graph;\n vector< cost_t > potential, min_cost;\n vector< int > prevv, preve;\n \n PrimalDual(int V) : graph(V), INF(numeric_limits< cost_t >::max()) {}\n \n void add_edge(int from, int to, flow_t cap, cost_t cost) {\n graph[from].emplace_back((edge) {to, cap, cost, (int) graph[to].size(), false});\n graph[to].emplace_back((edge) {from, 0, -cost, (int) graph[from].size() - 1, true});\n }\n \n cost_t min_cost_flow(int s, int t, flow_t f) {\n int V = (int) graph.size();\n cost_t ret = 0;\n using Pi = pair< cost_t, int >;\n priority_queue< Pi, vector< Pi >, greater< Pi > > que;\n potential.assign(V, 0);\n preve.assign(V, -1);\n prevv.assign(V, -1);\n \n while(f > 0) {\n min_cost.assign(V, INF);\n que.emplace(0, s);\n min_cost[s] = 0;\n while(!que.empty()) {\n Pi p = que.top();\n que.pop();\n if(min_cost[p.second] < p.first) continue;\n for(int i = 0; i < graph[p.second].size(); i++) {\n edge &e = graph[p.second][i];\n cost_t nextCost = min_cost[p.second] + e.cost + potential[p.second] - potential[e.to];\n if(e.cap > 0 && min_cost[e.to] > nextCost) {\n min_cost[e.to] = nextCost;\n prevv[e.to] = p.second, preve[e.to] = i;\n que.emplace(min_cost[e.to], e.to);\n }\n }\n }\n if(min_cost[t] == INF) return -1;\n for(int v = 0; v < V; v++) potential[v] += min_cost[v];\n flow_t addflow = f;\n for(int v = t; v != s; v = prevv[v]) {\n addflow = min(addflow, graph[prevv[v]][preve[v]].cap);\n }\n f -= addflow;\n ret += addflow * potential[t];\n for(int v = t; v != s; v = prevv[v]) {\n edge &e = graph[prevv[v]][preve[v]];\n e.cap -= addflow;\n graph[v][e.rev].cap += addflow;\n }\n }\n return ret;\n }\n};\n\nint main() {\n int N;\n cin >> N;\n vector<int>A(N),B(N),C(1000001);\n PrimalDual<int,int>flow(N*4+1000000+2);\n int S = N*4+1000000,T = S+1;\n int ans = 0;\n for(int i = 0; i < N; i++) {\n cin >> A[i] >> B[i];\n C[A[i]] = C[B[i]] = 1;\n ans += max(A[i],B[i]);\n flow.add_edge(S,i,1,0);\n flow.add_edge(i,N+i,1,max(A[i],B[i]));\n flow.add_edge(i,2*N+i,1,max(A[i],B[i])-min(A[i],B[i]));\n flow.add_edge(i,3*N+i,1,0);\n flow.add_edge(N+i,T,1,0);\n flow.add_edge(2*N+i,4*N+max(A[i],B[i])-1,1,0);\n flow.add_edge(3*N+i,4*N+min(A[i],B[i])-1,1,0);\n }\n for(int i = 1; i <= 1000000; i++) {\n if(C[i]) {\n flow.add_edge(4*N+i-1,T,1,0);\n }\n }\n cout << ans-flow.min_cost_flow(S,T,N) << endl;\n}", "accuracy": 1, "time_ms": 1270, "memory_kb": 46764, "score_of_the_acc": -2, "final_rank": 18 }, { "submission_id": "aoj_2293_6459083", "code_snippet": "#line 1 \"a.cpp\"\n/**\n *\tauthor: nok0\n *\tcreated: 2022.04.06 21:05:07\n**/\n#line 1 \"/Users/nok0/Documents/Programming/nok0/cftemp.hpp\"\n#include <bits/stdc++.h>\nusing namespace std;\n\n#pragma region Macros\n// rep macro\n#define foa(v, a) for(auto &v : a)\n#define REPname(a, b, c, d, e, ...) e\n#define REP(...) REPname(__VA_ARGS__, REP3, REP2, REP1, REP0)(__VA_ARGS__)\n#define REP0(x) for(int i = 0; i < (x); ++i)\n#define REP1(i, x) for(int i = 0; i < (x); ++i)\n#define REP2(i, l, r) for(int i = (l); i < (r); ++i)\n#define REP3(i, l, r, c) for(int i = (l); i < (r); i += (c))\n#define REPSname(a, b, c, ...) c\n#define REPS(...) REPSname(__VA_ARGS__, REPS1, REPS0)(__VA_ARGS__)\n#define REPS0(x) for(int i = 1; i <= (x); ++i)\n#define REPS1(i, x) for(int i = 1; i <= (x); ++i)\n#define RREPname(a, b, c, d, e, ...) e\n#define RREP(...) RREPname(__VA_ARGS__, RREP3, RREP2, RREP1, RREP0)(__VA_ARGS__)\n#define RREP0(x) for(int i = (x)-1; i >= 0; --i)\n#define RREP1(i, x) for(int i = (x)-1; i >= 0; --i)\n#define RREP2(i, r, l) for(int i = (r)-1; i >= (l); --i)\n#define RREP3(i, r, l, c) for(int i = (r)-1; i >= (l); i -= (c))\n#define RREPSname(a, b, c, ...) c\n#define RREPS(...) RREPSname(__VA_ARGS__, RREPS1, RREPS0)(__VA_ARGS__)\n#define RREPS0(x) for(int i = (x); i >= 1; --i)\n#define RREPS1(i, x) for(int i = (x); i >= 1; --i)\n\n// name macro\n#define pb push_back\n#define eb emplace_back\n#define SZ(x) ((int)(x).size())\n#define all(x) (x).begin(), (x).end()\n#define rall(x) (x).rbegin(), (x).rend()\n#define popcnt(x) __builtin_popcountll(x)\ntemplate <class T = int>\nusing V = std::vector<T>;\ntemplate <class T = int>\nusing VV = std::vector<std::vector<T>>;\ntemplate <class T>\nusing pqup = std::priority_queue<T, std::vector<T>, std::greater<T>>;\nusing ll = long long;\nusing ld = long double;\nusing int128 = __int128_t;\nusing pii = std::pair<int, int>;\nusing pll = std::pair<long long, long long>;\n\n// input macro\ntemplate <class T, class U>\nstd::istream &operator>>(std::istream &is, std::pair<T, U> &p) {\n\tis >> p.first >> p.second;\n\treturn is;\n}\ntemplate <class T>\nstd::istream &operator>>(std::istream &is, std::vector<T> &v) {\n\tfor(T &i : v) is >> i;\n\treturn is;\n}\nstd::istream &operator>>(std::istream &is, __int128_t &a) {\n\tstd::string s;\n\tis >> s;\n\t__int128_t ret = 0;\n\tfor(int i = 0; i < s.length(); i++)\n\t\tif('0' <= s[i] and s[i] <= '9')\n\t\t\tret = 10 * ret + s[i] - '0';\n\ta = ret * (s[0] == '-' ? -1 : 1);\n\treturn is;\n}\nnamespace scanner {\nvoid scan(int &a) { std::cin >> a; }\nvoid scan(long long &a) { std::cin >> a; }\nvoid scan(std::string &a) { std::cin >> a; }\nvoid scan(char &a) { std::cin >> a; }\nvoid scan(char a[]) { std::scanf(\"%s\", a); }\nvoid scan(double &a) { std::cin >> a; }\nvoid scan(long double &a) { std::cin >> a; }\ntemplate <class T, class U>\nvoid scan(std::pair<T, U> &p) { std::cin >> p; }\ntemplate <class T>\nvoid scan(std::vector<T> &a) { std::cin >> a; }\nvoid INPUT() {}\ntemplate <class Head, class... Tail>\nvoid INPUT(Head &head, Tail &...tail) {\n\tscan(head);\n\tINPUT(tail...);\n}\n} // namespace scanner\n#define VEC(type, name, size) \\\n\tstd::vector<type> name(size); \\\n\tscanner::INPUT(name)\n#define VVEC(type, name, h, w) \\\n\tstd::vector<std::vector<type>> name(h, std::vector<type>(w)); \\\n\tscanner::INPUT(name)\n#define INT(...) \\\n\tint __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define LL(...) \\\n\tlong long __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define STR(...) \\\n\tstd::string __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define CHAR(...) \\\n\tchar __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define DOUBLE(...) \\\n\tdouble __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define LD(...) \\\n\tlong double __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n\n// output-macro\ntemplate <class T, class U>\nstd::ostream &operator<<(std::ostream &os, const std::pair<T, U> &p) {\n\tos << p.first << \" \" << p.second;\n\treturn os;\n}\ntemplate <class T>\nstd::ostream &operator<<(std::ostream &os, const std::vector<T> &a) {\n\tfor(int i = 0; i < int(a.size()); ++i) {\n\t\tif(i) os << \" \";\n\t\tos << a[i];\n\t}\n\treturn os;\n}\nstd::ostream &operator<<(std::ostream &dest, __int128_t &value) {\n\tstd::ostream::sentry s(dest);\n\tif(s) {\n\t\t__uint128_t tmp = value < 0 ? -value : value;\n\t\tchar buffer[128];\n\t\tchar *d = std::end(buffer);\n\t\tdo {\n\t\t\t--d;\n\t\t\t*d = \"0123456789\"[tmp % 10];\n\t\t\ttmp /= 10;\n\t\t} while(tmp != 0);\n\t\tif(value < 0) {\n\t\t\t--d;\n\t\t\t*d = '-';\n\t\t}\n\t\tint len = std::end(buffer) - d;\n\t\tif(dest.rdbuf()->sputn(d, len) != len) {\n\t\t\tdest.setstate(std::ios_base::badbit);\n\t\t}\n\t}\n\treturn dest;\n}\ntemplate <class T>\nvoid print(const T a) { std::cout << a << '\\n'; }\ntemplate <class Head, class... Tail>\nvoid print(Head H, Tail... T) {\n\tstd::cout << H << ' ';\n\tprint(T...);\n}\ntemplate <class T>\nvoid printel(const T a) { std::cout << a << '\\n'; }\ntemplate <class T>\nvoid printel(const std::vector<T> &a) {\n\tfor(const auto &v : a)\n\t\tstd::cout << v << '\\n';\n}\ntemplate <class Head, class... Tail>\nvoid printel(Head H, Tail... T) {\n\tstd::cout << H << '\\n';\n\tprintel(T...);\n}\nvoid Yes(const bool b = true) { std::cout << (b ? \"Yes\\n\" : \"No\\n\"); }\nvoid No() { std::cout << \"No\\n\"; }\nvoid YES(const bool b = true) { std::cout << (b ? \"YES\\n\" : \"NO\\n\"); }\nvoid NO() { std::cout << \"NO\\n\"; }\nvoid err(const bool b = true) {\n\tif(b) {\n\t\tstd::cout << \"-1\\n\", exit(0);\n\t}\n}\n\n//debug macro\nnamespace debugger {\ntemplate <class T>\nvoid view(const std::vector<T> &a) {\n\tstd::cerr << \"{ \";\n\tfor(const auto &v : a) {\n\t\tstd::cerr << v << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\ntemplate <class T>\nvoid view(const std::vector<std::vector<T>> &a) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &v : a) {\n\t\tstd::cerr << \"\\t\";\n\t\tview(v);\n\t\tstd::cerr << \"\\n\";\n\t}\n\tstd::cerr << \"}\";\n}\ntemplate <class T, class U>\nvoid view(const std::vector<std::pair<T, U>> &a) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &p : a) std::cerr << \"\\t(\" << p.first << \", \" << p.second << \")\\n\";\n\tstd::cerr << \"}\";\n}\ntemplate <class T, class U>\nvoid view(const std::map<T, U> &m) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &p : m) std::cerr << \"\\t[\" << p.first << \"] : \" << p.second << \"\\n\";\n\tstd::cerr << \"}\";\n}\ntemplate <class T, class U>\nvoid view(const std::pair<T, U> &p) { std::cerr << \"(\" << p.first << \", \" << p.second << \")\"; }\ntemplate <class T>\nvoid view(const std::set<T> &s) {\n\tstd::cerr << \"{ \";\n\tfor(auto &v : s) {\n\t\tview(v);\n\t\tstd::cerr << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\n\ntemplate <class T>\nvoid view(const T &e) { std::cerr << e; }\n} // namespace debugger\n#ifdef LOCAL\nvoid debug_out() {}\ntemplate <typename Head, typename... Tail>\nvoid debug_out(Head H, Tail... T) {\n\tdebugger::view(H);\n\tstd::cerr << \", \";\n\tdebug_out(T...);\n}\n#define debug(...) \\\n\tdo { \\\n\t\tstd::cerr << __LINE__ << \" [\" << #__VA_ARGS__ << \"] : [\"; \\\n\t\tdebug_out(__VA_ARGS__); \\\n\t\tstd::cerr << \"\\b\\b]\\n\"; \\\n\t} while(false)\n#else\n#define debug(...) (void(0))\n#endif\n\n// vector macro\ntemplate <class T>\nint lb(const std::vector<T> &a, const T x) { return std::distance((a).begin(), std::lower_bound((a).begin(), (a).end(), (x))); }\ntemplate <class T>\nint ub(const std::vector<T> &a, const T x) { return std::distance((a).begin(), std::upper_bound((a).begin(), (a).end(), (x))); }\ntemplate <class T>\nvoid UNIQUE(std::vector<T> &a) {\n\tstd::sort(a.begin(), a.end());\n\ta.erase(std::unique(a.begin(), a.end()), a.end());\n}\ntemplate <class T>\nstd::vector<T> press(std::vector<T> &a) {\n\tauto res = a;\n\tUNIQUE(res);\n\tfor(auto &v : a)\n\t\tv = lb(res, v);\n\treturn res;\n}\n#define SORTname(a, b, c, ...) c\n#define SORT(...) SORTname(__VA_ARGS__, SORT1, SORT0, ...)(__VA_ARGS__)\n#define SORT0(a) std::sort((a).begin(), (a).end())\n#define SORT1(a, c) std::sort((a).begin(), (a).end(), [](const auto x, const auto y) { return x c y; })\ntemplate <class T>\nvoid ADD(std::vector<T> &a, const T x = 1) {\n\tfor(auto &v : a) v += x;\n}\ntemplate <class T>\nvoid SUB(std::vector<T> &a, const T x = 1) {\n\tfor(auto &v : a) v -= x;\n}\nstd::vector<std::pair<char, int>> rle(const string &s) {\n\tint n = s.size();\n\tstd::vector<std::pair<char, int>> ret;\n\tfor(int l = 0; l < n;) {\n\t\tint r = l + 1;\n\t\tfor(; r < n and s[l] == s[r]; r++) {}\n\t\tret.emplace_back(s[l], r - l);\n\t\tl = r;\n\t}\n\treturn ret;\n}\ntemplate <class T>\nstd::vector<std::pair<T, int>> rle(const std::vector<T> &v) {\n\tint n = v.size();\n\tstd::vector<std::pair<T, int>> ret;\n\tfor(int l = 0; l < n;) {\n\t\tint r = l + 1;\n\t\tfor(; r < n and v[l] == v[r]; r++) {}\n\t\tret.emplace_back(v[l], r - l);\n\t\tl = r;\n\t}\n\treturn ret;\n}\nstd::vector<int> iota(int n) {\n\tstd::vector<int> p(n);\n\tstd::iota(p.begin(), p.end(), 0);\n\treturn p;\n}\ntemplate <class T>\nstruct cum_vector {\npublic:\n\tcum_vector() = default;\n\ttemplate <class U>\n\tcum_vector(const std::vector<U> &vec) : cum((int)vec.size() + 1) {\n\t\tfor(int i = 0; i < (int)vec.size(); i++)\n\t\t\tcum[i + 1] = cum[i] + vec[i];\n\t}\n\tT prod(int l, int r) {\n\t\treturn cum[r] - cum[l];\n\t}\n\nprivate:\n\tstd::vector<T> cum;\n};\n\n// math macro\ntemplate <class T, class U>\ninline bool chmin(T &a, const U &b) { return a > b ? a = b, true : false; }\ntemplate <class T, class U>\ninline bool chmax(T &a, const U &b) { return a < b ? a = b, true : false; }\ntemplate <class T>\nT divup(T x, T y) { return (x + y - 1) / y; }\ntemplate <class T>\nT POW(T a, long long n) {\n\tT ret = 1;\n\twhile(n) {\n\t\tif(n & 1) ret *= a;\n\t\ta *= a;\n\t\tn >>= 1;\n\t}\n\treturn ret;\n}\n// modpow\nlong long POW(long long a, long long n, const int mod) {\n\tlong long ret = 1;\n\ta = (a % mod + mod) % mod;\n\twhile(n) {\n\t\tif(n & 1) (ret *= a) %= mod;\n\t\t(a *= a) %= mod;\n\t\tn >>= 1;\n\t}\n\treturn ret;\n}\ntemplate <class T, class F>\nT bin_search(T ok, T ng, const F &f) {\n\twhile(abs(ok - ng) > 1) {\n\t\tT mid = (ok + ng) >> 1;\n\t\t(f(mid) ? ok : ng) = mid;\n\t}\n\treturn ok;\n}\ntemplate <class T, class F>\nT bin_search(T ok, T ng, const F &f, int loop) {\n\tfor(int i = 0; i < loop; i++) {\n\t\tT mid = (ok + ng) / 2;\n\t\t(f(mid) ? ok : ng) = mid;\n\t}\n\treturn ok;\n}\n\n// others\nstruct fast_io {\n\tfast_io() {\n\t\tios::sync_with_stdio(false);\n\t\tcin.tie(nullptr);\n\t\tcout << fixed << setprecision(15);\n\t}\n} fast_io_;\nconst int inf = 1e9;\nconst ll INF = 1e18;\n#pragma endregion\n\nvoid main_();\n\nint main() {\n\tmain_();\n\treturn 0;\n}\n#line 7 \"/Users/nok0/Documents/Programming/nok0/graph/mincost_b_flow.hpp\"\n\nenum Objective {\n\tMINIMIZE = 1,\n\tMAXIMIZE = -1,\n};\nenum class Status {\n\tOPTIMAL,\n\tINFEASIBLE,\n};\n\ntemplate <class Flow, class Cost, Objective objective = Objective::MINIMIZE, Flow SCALING_FACTOR = 2>\nstruct MinCostFlow {\nprivate:\n\tusing V_id = uint32_t;\n\tusing E_id = uint32_t;\n\n\tstruct Edge {\n\t\tfriend struct MinCostFlow;\n\n\tprivate:\n\t\tV_id src, dst;\n\t\tFlow flow, cap;\n\t\tCost cost;\n\t\tE_id rev;\n\n\tpublic:\n\t\tEdge() = default;\n\n\t\tEdge(const V_id src, const V_id dst, const Flow cap, const Cost cost,\n\t\t const E_id rev)\n\t\t : src(src), dst(dst), flow(0), cap(cap), cost(cost), rev(rev) {}\n\n\t\t[[nodiscard]] Flow residual_cap() const { return cap - flow; }\n\t};\n\npublic:\n\tstruct EdgePtr {\n\t\tfriend struct MinCostFlow;\n\n\tprivate:\n\t\tconst MinCostFlow *instance;\n\t\tconst V_id v;\n\t\tconst E_id e;\n\n\t\tEdgePtr(const MinCostFlow *instance, const V_id v, const E_id e)\n\t\t : instance(instance), v(v), e(e) {}\n\n\t\t[[nodiscard]] const Edge &edge() const { return instance->g[v][e]; }\n\n\t\t[[nodiscard]] const Edge &rev() const {\n\t\t\tconst Edge &e = edge();\n\t\t\treturn instance->g[e.dst][e.rev];\n\t\t}\n\n\tpublic:\n\t\t[[nodiscard]] V_id src() const { return rev().dst; }\n\n\t\t[[nodiscard]] V_id dst() const { return edge().dst; }\n\n\t\t[[nodiscard]] Flow flow() const { return edge().flow; }\n\n\t\t[[nodiscard]] Flow lower() const { return -rev().cap; }\n\n\t\t[[nodiscard]] Flow upper() const { return edge().cap; }\n\n\t\t[[nodiscard]] Cost cost() const { return edge().cost; }\n\n\t\t[[nodiscard]] Cost gain() const { return -edge().cost; }\n\t};\n\nprivate:\n\tV_id n;\n\tstd::vector<std::vector<Edge>> g;\n\tstd::vector<Flow> b;\n\npublic:\n\tMinCostFlow() : n(0) {}\n\n\tV_id add_vertex() {\n\t\t++n;\n\t\tg.resize(n);\n\t\tb.resize(n);\n\t\treturn n - 1;\n\t}\n\n\tstd::vector<V_id> add_vertices(const size_t size) {\n\t\tstd::vector<V_id> ret;\n\t\tfor(V_id i = 0; i < size; ++i) ret.emplace_back(n + i);\n\t\tn += size;\n\t\tg.resize(n);\n\t\tb.resize(n);\n\t\treturn ret;\n\t}\n\n\tEdgePtr add_edge(const V_id src, const V_id dst, const Flow lower, const Flow upper, const Cost cost) {\n\t\tconst E_id e = g[src].size(), re = src == dst ? e + 1 : g[dst].size();\n\t\tassert(lower <= upper);\n\t\tg[src].emplace_back(Edge{src, dst, upper, cost * objective, re});\n\t\tg[dst].emplace_back(Edge{dst, src, -lower, -cost * objective, e});\n\t\treturn EdgePtr{this, src, e};\n\t}\n\n\tvoid add_supply(const V_id v, const Flow amount) { b[v] += amount; }\n\n\tvoid add_demand(const V_id v, const Flow amount) { b[v] -= amount; }\n\nprivate:\n\tstatic Cost constexpr unreachable = std::numeric_limits<Cost>::max();\n\tCost farthest;\n\tstd::vector<Cost> potential;\n\tstd::vector<Cost> dist;\n\tstd::vector<Edge *> parent;\n\tstd::priority_queue<std::pair<Cost, int>, std::vector<std::pair<Cost, int>>, std::greater<>> pq;\n\tstd::vector<V_id> excess_vs, deficit_vs;\n\n\tEdge &rev(const Edge &e) { return g[e.dst][e.rev]; }\n\n\tvoid push(Edge &e, const Flow amount) {\n\t\te.flow += amount;\n\t\tg[e.dst][e.rev].flow -= amount;\n\t}\n\n\tCost residual_cost(const V_id src, const V_id dst, const Edge &e) {\n\t\treturn e.cost + potential[src] - potential[dst];\n\t}\n\n\tbool dual(const Flow delta) {\n\t\tdist.assign(n, unreachable);\n\t\tparent.assign(n, nullptr);\n\t\texcess_vs.erase(std::remove_if(std::begin(excess_vs), std::end(excess_vs), [&](const V_id v) { return b[v] < delta; }), std::end(excess_vs));\n\t\tdeficit_vs.erase(std::remove_if(std::begin(deficit_vs), std::end(deficit_vs), [&](const V_id v) { return b[v] > -delta; }), std::end(deficit_vs));\n\t\tfor(const auto v : excess_vs) pq.emplace(dist[v] = 0, v);\n\t\tfarthest = 0;\n\t\tstd::size_t deficit_count = 0;\n\t\twhile(!pq.empty()) {\n\t\t\tconst auto [d, u] = pq.top();\n\t\t\tpq.pop();\n\t\t\tif(dist[u] < d) continue;\n\t\t\tfarthest = d;\n\t\t\tif(b[u] <= -delta) ++deficit_count;\n\t\t\tif(deficit_count >= deficit_vs.size()) break;\n\t\t\tfor(auto &e : g[u]) {\n\t\t\t\tif(e.residual_cap() < delta) continue;\n\t\t\t\tconst auto v = e.dst;\n\t\t\t\tconst auto new_dist = d + residual_cost(u, v, e);\n\t\t\t\tif(new_dist >= dist[v]) continue;\n\t\t\t\tpq.emplace(dist[v] = new_dist, v);\n\t\t\t\tparent[v] = &e;\n\t\t\t}\n\t\t}\n\t\tpq = decltype(pq)();\n\t\tfor(V_id v = 0; v < n; ++v) {\n\t\t\tpotential[v] += std::min(dist[v], farthest);\n\t\t}\n\t\treturn deficit_count > 0;\n\t}\n\n\tvoid primal(const Flow delta) {\n\t\tfor(const auto t : deficit_vs) {\n\t\t\tif(dist[t] > farthest) continue;\n\t\t\tFlow f = -b[t];\n\t\t\tV_id v;\n\t\t\tfor(v = t; parent[v] != nullptr && f >= delta; v = parent[v]->src) {\n\t\t\t\tf = std::min(f, parent[v]->residual_cap());\n\t\t\t}\n\t\t\tf = std::min(f, b[v]);\n\t\t\tif(f < delta) continue;\n\t\t\tfor(v = t; parent[v] != nullptr;) {\n\t\t\t\tauto &e = *parent[v];\n\t\t\t\tpush(e, f);\n\t\t\t\tconst size_t u = parent[v]->src;\n\t\t\t\tparent[v] = nullptr;\n\t\t\t\tv = u;\n\t\t\t}\n\t\t\tb[t] += f;\n\t\t\tb[v] -= f;\n\t\t}\n\t}\n\n\tvoid saturate_negative(const Flow delta) {\n\t\texcess_vs.clear();\n\t\tdeficit_vs.clear();\n\t\tfor(auto &es : g)\n\t\t\tfor(auto &e : es) {\n\t\t\t\tconst Flow rcap = e.residual_cap();\n\t\t\t\tconst Cost rcost = residual_cost(e.src, e.dst, e);\n\t\t\t\tif(rcost < 0 && rcap >= delta) {\n\t\t\t\t\tpush(e, rcap);\n\t\t\t\t\tb[e.src] -= rcap;\n\t\t\t\t\tb[e.dst] += rcap;\n\t\t\t\t}\n\t\t\t}\n\t\tfor(V_id v = 0; v < n; ++v)\n\t\t\tif(b[v] != 0) {\n\t\t\t\t(b[v] > 0 ? excess_vs : deficit_vs).emplace_back(v);\n\t\t\t}\n\t}\n\npublic:\n\tstd::pair<Status, Cost> solve() {\n\t\tpotential.resize(n);\n\t\tfor(auto &es : g)\n\t\t\tfor(auto &e : es) {\n\t\t\t\tconst Flow rcap = e.residual_cap();\n\t\t\t\tif(rcap < 0) {\n\t\t\t\t\tpush(e, rcap);\n\t\t\t\t\tb[e.src] -= rcap;\n\t\t\t\t\tb[e.dst] += rcap;\n\t\t\t\t}\n\t\t\t}\n\n\t\tFlow inf_flow = 1;\n\t\tfor(const auto &es : g)\n\t\t\tfor(const auto &e : es) inf_flow = std::max(inf_flow, e.residual_cap());\n\t\tFlow delta = 1;\n\t\twhile(delta <= inf_flow) delta *= SCALING_FACTOR;\n\n\t\tfor(delta /= SCALING_FACTOR; delta; delta /= SCALING_FACTOR) {\n\t\t\tsaturate_negative(delta);\n\t\t\twhile(dual(delta)) primal(delta);\n\t\t}\n\n\t\tCost value = 0;\n\t\tfor(const auto &es : g)\n\t\t\tfor(const auto &e : es) {\n\t\t\t\tvalue += e.flow * e.cost;\n\t\t\t}\n\t\tvalue /= 2;\n\n\t\tif(excess_vs.empty() && deficit_vs.empty()) {\n\t\t\treturn {Status::OPTIMAL, value / objective};\n\t\t} else {\n\t\t\treturn {Status::INFEASIBLE, value / objective};\n\t\t}\n\t}\n};\n\ntemplate <class Flow, class Cost>\nusing MaxGainFlow = MinCostFlow<Flow, Cost, Objective::MAXIMIZE>;\n#line 7 \"a.cpp\"\n\nvoid main_() {\n\tINT(n);\n\tVEC(pii, ab, n);\n\tV<> x;\n\tfoa(p, ab) x.pb(p.first), x.pb(p.second);\n\tauto re = press(x);\n\tint m = SZ(re);\n\tMaxGainFlow<int, int> mf;\n\tauto s = mf.add_vertex();\n\tauto t = mf.add_vertex();\n\tmf.add_supply(s, n);\n\tmf.add_demand(t, n);\n\tmf.add_edge(s, t, 0, n, 0);\n\tauto lef = mf.add_vertices(n);\n\tauto rig = mf.add_vertices(m);\n\tREP(i, n) {\n\t\tmf.add_edge(s, lef[i], 0, 1, 0);\n\t\tauto [ai, bi] = ab[i];\n\t\tmf.add_edge(lef[i], rig[lb(re, ai)], 0, 1, bi);\n\t\tmf.add_edge(lef[i], rig[lb(re, bi)], 0, 1, ai);\n\t}\n\tREP(j, m) {\n\t\tmf.add_edge(rig[j], t, 0, 1, 0);\n\t}\n\tprint(mf.solve().second);\n}", "accuracy": 1, "time_ms": 210, "memory_kb": 3912, "score_of_the_acc": -0.1615, "final_rank": 14 }, { "submission_id": "aoj_2293_6295499", "code_snippet": "#line 2 \"library/bits/stdc++.h\"\n\n// C\n#ifndef _GLIBCXX_NO_ASSERT\n#include <cassert>\n#endif\n#include <cctype>\n#include <cerrno>\n#include <cfloat>\n#include <ciso646>\n#include <climits>\n#include <clocale>\n#include <cmath>\n#include <csetjmp>\n#include <csignal>\n#include <cstdarg>\n#include <cstddef>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <ctime>\n\n#if __cplusplus >= 201103L\n#include <ccomplex>\n#include <cfenv>\n#include <cinttypes>\n#include <cstdbool>\n#include <cstdint>\n#include <ctgmath>\n#include <cwchar>\n#include <cwctype>\n#endif\n\n// C++\n#include <algorithm>\n#include <bitset>\n#include <complex>\n#include <deque>\n#include <exception>\n#include <fstream>\n#include <functional>\n#include <iomanip>\n#include <ios>\n#include <iosfwd>\n#include <iostream>\n#include <istream>\n#include <iterator>\n#include <limits>\n#include <list>\n#include <locale>\n#include <map>\n#include <memory>\n#include <new>\n#include <numeric>\n#include <ostream>\n#include <queue>\n#include <set>\n#include <sstream>\n#include <stack>\n#include <stdexcept>\n#include <streambuf>\n#include <string>\n#include <typeinfo>\n#include <utility>\n#include <valarray>\n#include <vector>\n\n#if __cplusplus >= 201103L\n#include <array>\n#include <atomic>\n#include <chrono>\n#include <condition_variable>\n#include <forward_list>\n#include <future>\n#include <initializer_list>\n#include <mutex>\n#include <random>\n#include <ratio>\n#include <regex>\n#include <scoped_allocator>\n#include <system_error>\n#include <thread>\n#include <tuple>\n#include <typeindex>\n#include <type_traits>\n#include <unordered_map>\n#include <unordered_set>\n#endif\n#line 2 \"Source.cpp\"\nusing namespace std;\n\ntypedef string::const_iterator State;\n#define eps 1e-8L\n#define MAX_MOD 1000000007LL\n#define GYAKU 500000004LL\n#define MOD 998244353LL\n#define pb push_back\n#define mp make_pair\ntypedef long long ll;\ntypedef long double ld;\n#define REP(a, b) for (long long(a) = 0; (a) < (b); ++(a))\n#define ALL(x) (x).begin(), (x).end()\n\n#line 1 \"library/atcoder/mincostflow.hpp\"\n\n\n\n#line 5 \"library/atcoder/mincostflow.hpp\"\n#include <cassert>\n#line 9 \"library/atcoder/mincostflow.hpp\"\n\n#line 1 \"library/atcoder/internal_csr.hpp\"\n\n\n\n#line 7 \"library/atcoder/internal_csr.hpp\"\n\nnamespace atcoder {\nnamespace internal {\n\ntemplate <class E> struct csr {\n std::vector<int> start;\n std::vector<E> elist;\n explicit csr(int n, const std::vector<std::pair<int, E>>& edges)\n : start(n + 1), elist(edges.size()) {\n for (auto e : edges) {\n start[e.first + 1]++;\n }\n for (int i = 1; i <= n; i++) {\n start[i] += start[i - 1];\n }\n auto counter = start;\n for (auto e : edges) {\n elist[counter[e.first]++] = e.second;\n }\n }\n};\n\n} // namespace internal\n\n} // namespace atcoder\n\n\n#line 1 \"library/atcoder/internal_queue.hpp\"\n\n\n\n#line 5 \"library/atcoder/internal_queue.hpp\"\n\nnamespace atcoder {\n\nnamespace internal {\n\ntemplate <class T> struct simple_queue {\n std::vector<T> payload;\n int pos = 0;\n void reserve(int n) { payload.reserve(n); }\n int size() const { return int(payload.size()) - pos; }\n bool empty() const { return pos == int(payload.size()); }\n void push(const T& t) { payload.push_back(t); }\n T& front() { return payload[pos]; }\n void clear() {\n payload.clear();\n pos = 0;\n }\n void pop() { pos++; }\n};\n\n} // namespace internal\n\n} // namespace atcoder\n\n\n#line 12 \"library/atcoder/mincostflow.hpp\"\n\nnamespace atcoder {\n\ntemplate <class Cap, class Cost> struct mcf_graph {\n public:\n mcf_graph() {}\n explicit mcf_graph(int n) : _n(n) {}\n\n int add_edge(int from, int to, Cap cap, Cost cost) {\n assert(0 <= from && from < _n);\n assert(0 <= to && to < _n);\n assert(0 <= cap);\n assert(0 <= cost);\n int m = int(_edges.size());\n _edges.push_back({from, to, cap, 0, cost});\n return m;\n }\n\n struct edge {\n int from, to;\n Cap cap, flow;\n Cost cost;\n };\n\n edge get_edge(int i) {\n int m = int(_edges.size());\n assert(0 <= i && i < m);\n return _edges[i];\n }\n std::vector<edge> edges() { return _edges; }\n\n std::pair<Cap, Cost> flow(int s, int t) {\n return flow(s, t, std::numeric_limits<Cap>::max());\n }\n std::pair<Cap, Cost> flow(int s, int t, Cap flow_limit) {\n return slope(s, t, flow_limit).back();\n }\n std::vector<std::pair<Cap, Cost>> slope(int s, int t) {\n return slope(s, t, std::numeric_limits<Cap>::max());\n }\n std::vector<std::pair<Cap, Cost>> slope(int s, int t, Cap flow_limit) {\n assert(0 <= s && s < _n);\n assert(0 <= t && t < _n);\n assert(s != t);\n\n int m = int(_edges.size());\n std::vector<int> edge_idx(m);\n\n auto g = [&]() {\n std::vector<int> degree(_n), redge_idx(m);\n std::vector<std::pair<int, _edge>> elist;\n elist.reserve(2 * m);\n for (int i = 0; i < m; i++) {\n auto e = _edges[i];\n edge_idx[i] = degree[e.from]++;\n redge_idx[i] = degree[e.to]++;\n elist.push_back({e.from, {e.to, -1, e.cap - e.flow, e.cost}});\n elist.push_back({e.to, {e.from, -1, e.flow, -e.cost}});\n }\n auto _g = internal::csr<_edge>(_n, elist);\n for (int i = 0; i < m; i++) {\n auto e = _edges[i];\n edge_idx[i] += _g.start[e.from];\n redge_idx[i] += _g.start[e.to];\n _g.elist[edge_idx[i]].rev = redge_idx[i];\n _g.elist[redge_idx[i]].rev = edge_idx[i];\n }\n return _g;\n }();\n\n auto result = slope(g, s, t, flow_limit);\n\n for (int i = 0; i < m; i++) {\n auto e = g.elist[edge_idx[i]];\n _edges[i].flow = _edges[i].cap - e.cap;\n }\n\n return result;\n }\n\n private:\n int _n;\n std::vector<edge> _edges;\n\n // inside edge\n struct _edge {\n int to, rev;\n Cap cap;\n Cost cost;\n };\n\n std::vector<std::pair<Cap, Cost>> slope(internal::csr<_edge>& g,\n int s,\n int t,\n Cap flow_limit) {\n // variants (C = maxcost):\n // -(n-1)C <= dual[s] <= dual[i] <= dual[t] = 0\n // reduced cost (= e.cost + dual[e.from] - dual[e.to]) >= 0 for all edge\n\n // dual_dist[i] = (dual[i], dist[i])\n std::vector<std::pair<Cost, Cost>> dual_dist(_n);\n std::vector<int> prev_e(_n);\n std::vector<bool> vis(_n);\n struct Q {\n Cost key;\n int to;\n bool operator<(Q r) const { return key > r.key; }\n };\n std::vector<int> que_min;\n std::vector<Q> que;\n auto dual_ref = [&]() {\n for (int i = 0; i < _n; i++) {\n dual_dist[i].second = std::numeric_limits<Cost>::max();\n }\n std::fill(vis.begin(), vis.end(), false);\n que_min.clear();\n que.clear();\n\n // que[0..heap_r) was heapified\n size_t heap_r = 0;\n\n dual_dist[s].second = 0;\n que_min.push_back(s);\n while (!que_min.empty() || !que.empty()) {\n int v;\n if (!que_min.empty()) {\n v = que_min.back();\n que_min.pop_back();\n } else {\n while (heap_r < que.size()) {\n heap_r++;\n std::push_heap(que.begin(), que.begin() + heap_r);\n }\n v = que.front().to;\n std::pop_heap(que.begin(), que.end());\n que.pop_back();\n heap_r--;\n }\n if (vis[v]) continue;\n vis[v] = true;\n if (v == t) break;\n // dist[v] = shortest(s, v) + dual[s] - dual[v]\n // dist[v] >= 0 (all reduced cost are positive)\n // dist[v] <= (n-1)C\n Cost dual_v = dual_dist[v].first, dist_v = dual_dist[v].second;\n for (int i = g.start[v]; i < g.start[v + 1]; i++) {\n auto e = g.elist[i];\n if (!e.cap) continue;\n // |-dual[e.to] + dual[v]| <= (n-1)C\n // cost <= C - -(n-1)C + 0 = nC\n Cost cost = e.cost - dual_dist[e.to].first + dual_v;\n if (dual_dist[e.to].second - dist_v > cost) {\n Cost dist_to = dist_v + cost;\n dual_dist[e.to].second = dist_to;\n prev_e[e.to] = e.rev;\n if (dist_to == dist_v) {\n que_min.push_back(e.to);\n } else {\n que.push_back(Q{dist_to, e.to});\n }\n }\n }\n }\n if (!vis[t]) {\n return false;\n }\n\n for (int v = 0; v < _n; v++) {\n if (!vis[v]) continue;\n // dual[v] = dual[v] - dist[t] + dist[v]\n // = dual[v] - (shortest(s, t) + dual[s] - dual[t]) +\n // (shortest(s, v) + dual[s] - dual[v]) = - shortest(s,\n // t) + dual[t] + shortest(s, v) = shortest(s, v) -\n // shortest(s, t) >= 0 - (n-1)C\n dual_dist[v].first -= dual_dist[t].second - dual_dist[v].second;\n }\n return true;\n };\n Cap flow = 0;\n Cost cost = 0, prev_cost_per_flow = -1;\n std::vector<std::pair<Cap, Cost>> result = {{Cap(0), Cost(0)}};\n while (flow < flow_limit) {\n if (!dual_ref()) break;\n Cap c = flow_limit - flow;\n for (int v = t; v != s; v = g.elist[prev_e[v]].to) {\n c = std::min(c, g.elist[g.elist[prev_e[v]].rev].cap);\n }\n for (int v = t; v != s; v = g.elist[prev_e[v]].to) {\n auto& e = g.elist[prev_e[v]];\n e.cap += c;\n g.elist[e.rev].cap -= c;\n }\n Cost d = -dual_dist[s].first;\n flow += c;\n cost += c * d;\n if (prev_cost_per_flow == d) {\n result.pop_back();\n }\n result.push_back({flow, cost});\n prev_cost_per_flow = d;\n }\n return result;\n }\n};\n\n} // namespace atcoder\n\n\n#line 17 \"Source.cpp\"\nusing namespace atcoder;\n\n#define int long long\nvoid solve()\n{\n int n;\n cin >> n;\n vector<pair<int, int>> inputs;\n map<int, int> cnter;\n REP(i, n)\n {\n int a, b;\n cin >> a >> b;\n cnter[a];\n cnter[b];\n inputs.push_back({a, b});\n }\n\n {\n int cnt = 0;\n for (auto &x : cnter)\n {\n x.second = cnt;\n cnt++;\n }\n }\n int ans = 0;\n mcf_graph<int, int> graph(3 + inputs.size() + cnter.size());\n for (int i = 0; i < inputs.size(); ++i)\n {\n graph.add_edge(0, i + 1, 1, 0);\n graph.add_edge(i + 1, inputs.size() + 1 + cnter[inputs[i].first], 1, inputs[i].first);\n graph.add_edge(i + 1, inputs.size() + 1 + cnter[inputs[i].second], 1, inputs[i].second);\n graph.add_edge(i + 1, inputs.size() + cnter.size() + 1, 1, inputs[i].first + inputs[i].second);\n ans += inputs[i].first + inputs[i].second;\n }\n for (int i = 0; i < cnter.size(); ++i)\n {\n graph.add_edge(inputs.size() + 1 + i, inputs.size() + cnter.size() + 2, 1, 0);\n }\n\n graph.add_edge(inputs.size() + 1 + cnter.size(), inputs.size() + cnter.size() + 2, 10000, 0);\n\n ans -= graph.flow(0, inputs.size() + cnter.size() + 2).second;\n cout << ans << endl;\n}\n#undef int\n\n// generated by oj-template v4.7.2\n// (https://github.com/online-judge-tools/template-generator)\nint main()\n{\n // Fasterize input/output script\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << fixed << setprecision(100);\n // scanf/printf user should delete this fasterize input/output script\n\n int t = 1;\n // cin >> t; // comment out if solving multi testcase\n for (int testCase = 1; testCase <= t; ++testCase)\n {\n solve();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 4176, "score_of_the_acc": -0.0169, "final_rank": 3 }, { "submission_id": "aoj_2293_6026701", "code_snippet": "#include <iostream>\n\n#include <algorithm>\n#include <cassert>\n#include <vector>\n\n#include <limits>\n#include <type_traits>\n\nnamespace suisen {\n// ! utility\ntemplate <typename ...Types>\nusing constraints_t = std::enable_if_t<std::conjunction_v<Types...>, std::nullptr_t>;\ntemplate <bool cond_v, typename Then, typename OrElse>\nconstexpr decltype(auto) constexpr_if(Then&& then, OrElse&& or_else) {\n if constexpr (cond_v) {\n return std::forward<Then>(then);\n } else {\n return std::forward<OrElse>(or_else);\n }\n}\n\n// ! function\ntemplate <typename ReturnType, typename Callable, typename ...Args>\nusing is_same_as_invoke_result = std::is_same<std::invoke_result_t<Callable, Args...>, ReturnType>;\ntemplate <typename F, typename T>\nusing is_uni_op = is_same_as_invoke_result<T, F, T>;\ntemplate <typename F, typename T>\nusing is_bin_op = is_same_as_invoke_result<T, F, T, T>;\n\ntemplate <typename Comparator, typename T>\nusing is_comparator = std::is_same<std::invoke_result_t<Comparator, T, T>, bool>;\n\n// ! integral\ntemplate <typename T, typename = constraints_t<std::is_integral<T>>>\nconstexpr int bit_num = std::numeric_limits<std::make_unsigned_t<T>>::digits;\ntemplate <typename T, unsigned int n>\nstruct is_nbit { static constexpr bool value = bit_num<T> == n; };\ntemplate <typename T, unsigned int n>\nstatic constexpr bool is_nbit_v = is_nbit<T, n>::value;\n\n// ?\ntemplate <typename T>\nstruct safely_multipliable {};\ntemplate <>\nstruct safely_multipliable<int> { using type = long long; };\ntemplate <>\nstruct safely_multipliable<long long> { using type = __int128_t; };\ntemplate <>\nstruct safely_multipliable<float> { using type = float; };\ntemplate <>\nstruct safely_multipliable<double> { using type = double; };\ntemplate <>\nstruct safely_multipliable<long double> { using type = long double; };\ntemplate <typename T>\nusing safely_multipliable_t = typename safely_multipliable<T>::type;\n\n} // namespace suisen\n\nnamespace suisen {\ntemplate <typename T>\nclass CoordinateCompressorBuilder {\n public:\n struct Compressor {\n public:\n static constexpr int absent = -1;\n\n // default constructor\n Compressor() : _xs(std::vector<T>{}) {}\n // Construct from strictly sorted vector\n Compressor(const std::vector<T> &xs) : _xs(xs) {\n assert(is_strictly_sorted(xs));\n }\n\n // Return the number of distinct keys.\n int size() const {\n return _xs.size();\n }\n // Check if the element is registered.\n bool has_key(const T &e) const {\n return std::binary_search(_xs.begin(), _xs.end(), e);\n }\n // Compress the element. if not registered, returns `default_value`. (default: Compressor::absent)\n int comp(const T &e, int default_value = absent) const {\n const int res = min_geq_index(e);\n return res != size() and _xs[res] == e ? res : default_value;\n }\n // Restore the element from the index.\n T decomp(const int compressed_index) const {\n return _xs[compressed_index];\n }\n // Compress the element. Equivalent to call `comp(e)`\n int operator[](const T &e) const {\n return comp(e);\n }\n // Return the minimum registered value greater than `e`. if not exists, return `default_value`.\n T min_gt(const T &e, const T &default_value) const {\n auto it = std::upper_bound(_xs.begin(), _xs.end(), e);\n return it == _xs.end() ? default_value : *it;\n }\n // Return the minimum registered value greater than or equal to `e`. if not exists, return `default_value`.\n T min_geq(const T &e, const T &default_value) const {\n auto it = std::lower_bound(_xs.begin(), _xs.end(), e);\n return it == _xs.end() ? default_value : *it;\n }\n // Return the maximum registered value less than `e`. if not exists, return `default_value`\n T max_lt(const T &e, const T &default_value) const {\n auto it = std::upper_bound(_xs.rbegin(), _xs.rend(), e);\n return it == _xs.rend() ? default_value : *it;\n }\n // Return the maximum registered value less than or equal to `e`. if not exists, return `default_value`\n T max_leq(const T &e, const T &default_value) const {\n auto it = std::lower_bound(_xs.rbegin(), _xs.rend(), e);\n return it == _xs.rend() ? default_value : *it;\n }\n // Return the compressed index of the minimum registered value greater than `e`. if not exists, return `compressor.size()`.\n int min_gt_index(const T &e) const {\n return std::upper_bound(_xs.begin(), _xs.end(), e) - _xs.begin();\n }\n // Return the compressed index of the minimum registered value greater than or equal to `e`. if not exists, return `compressor.size()`.\n int min_geq_index(const T &e) const {\n return std::lower_bound(_xs.begin(), _xs.end(), e) - _xs.begin();\n }\n // Return the compressed index of the maximum registered value less than `e`. if not exists, return -1.\n int max_lt_index(const T &e) const {\n return int(_xs.rend() - std::upper_bound(_xs.rbegin(), _xs.rend(), e)) - 1;\n }\n // Return the compressed index of the maximum registered value less than or equal to `e`. if not exists, return -1.\n int max_leq_index(const T &e) const {\n return int(_xs.rend() - std::lower_bound(_xs.rbegin(), _xs.rend(), e)) - 1;\n }\n private:\n std::vector<T> _xs;\n static bool is_strictly_sorted(const std::vector<T> &v) {\n return std::adjacent_find(v.begin(), v.end(), std::greater_equal<T>()) == v.end();\n }\n };\n CoordinateCompressorBuilder() : _xs(std::vector<T>{}) {}\n explicit CoordinateCompressorBuilder(const std::vector<T> &xs) : _xs(xs) {}\n explicit CoordinateCompressorBuilder(std::vector<T> &&xs) : _xs(std::move(xs)) {}\n template <typename Gen, constraints_t<is_same_as_invoke_result<T, Gen, int>> = nullptr>\n CoordinateCompressorBuilder(const int n, Gen generator) {\n reserve(n);\n for (int i = 0; i < n; ++i) push(generator(i));\n }\n // Attempt to preallocate enough memory for specified number of elements.\n void reserve(int n) {\n _xs.reserve(n);\n }\n // Add data.\n void push(const T &first) {\n _xs.push_back(first);\n }\n // Add data.\n void push(T &&first) {\n _xs.push_back(std::move(first));\n }\n // Add data in the range of [first, last). \n template <typename Iterator>\n auto push(const Iterator &first, const Iterator &last) -> decltype(std::vector<T>{}.push_back(*first), void()) {\n for (auto it = first; it != last; ++it) _xs.push_back(*it);\n }\n // Add all data in the container. Equivalent to `push(iterable.begin(), iterable.end())`.\n template <typename Iterable>\n auto push(const Iterable &iterable) -> decltype(std::vector<T>{}.push_back(*iterable.begin()), void()) {\n push(iterable.begin(), iterable.end());\n }\n // Add data.\n template <typename ...Args>\n void emplace(Args &&...args) {\n _xs.emplace_back(std::forward<Args>(args)...);\n }\n // Build compressor.\n auto build() {\n std::sort(_xs.begin(), _xs.end()), _xs.erase(std::unique(_xs.begin(), _xs.end()), _xs.end());\n return Compressor {_xs};\n }\n // Build compressor from vector.\n static auto build(const std::vector<T> &xs) {\n return CoordinateCompressorBuilder(xs).build();\n }\n // Build compressor from vector.\n static auto build(std::vector<T> &&xs) {\n return CoordinateCompressorBuilder(std::move(xs)).build();\n }\n // Build compressor from generator.\n template <typename Gen, constraints_t<is_same_as_invoke_result<T, Gen, int>> = nullptr>\n static auto build(const int n, Gen generator) {\n return CoordinateCompressorBuilder<T>(n, generator).build();\n }\n private:\n std::vector<T> _xs;\n};\n\n} // namespace suisen\n\n#include <queue>\n\nnamespace suisen {\n\nenum MCFPotentialInitializer {\n DAG, BELLMAN_FORD, DIJKSTRA\n};\n\ntemplate <typename Cap, typename Cost, MCFPotentialInitializer init_method = MCFPotentialInitializer::BELLMAN_FORD>\nclass MinCostFlow {\n struct InternalEdge { int to; Cap cap; Cost cost; int rev; };\n public:\n MinCostFlow() : MinCostFlow(0) {}\n MinCostFlow(int n) : n(n), g(n), potential(n, 0), dist(n), prev_vid(n), prev_eid(n) {}\n\n // Returns the id of created edge.\n int add_edge(int u, int v, Cap cap, Cost cost) {\n int edge_id = edges.size();\n edges.emplace_back(u, g[u].size());\n g[u].push_back({ v, cap, cost, int(g[v].size()) });\n g[v].push_back({ u, 0, -cost, int(g[u].size()) - 1 });\n return edge_id;\n }\n\n /**\n * Returns { flow, cost } (flow = min(max_flow, f))\n */\n auto min_cost_max_flow(const int s, const int t, const Cap f) {\n return min_cost_max_flow_slope(s, t, f).back();\n }\n /**\n * Returns { flow, cost } (flow = max_flow)\n */\n auto min_cost_max_flow(const int s, const int t) {\n return min_cost_max_flow_slope(s, t).back();\n }\n /**\n * Returns { flow, cost }\n * amount of flow is arbitrary.\n */\n auto min_cost_arbitrary_flow(const int s, const int t) {\n return min_cost_arbitrary_flow_slope(s, t).back();\n }\n\n auto min_cost_max_flow_slope(const int s, const int t, const Cap f) {\n return min_cost_flow_slope(s, t, f, [](Cap, Cost){ return true; });\n }\n auto min_cost_max_flow_slope(const int s, const int t) {\n return min_cost_max_flow_slope(s, t, INF_FLOW);\n }\n auto min_cost_arbitrary_flow_slope(const int s, const int t) {\n return min_cost_flow_slope(s, t, INF_FLOW, [this, t](Cap, Cost){ return potential[t] < 0; });\n }\n\n struct Edge {\n int from, to;\n Cap cap, flow;\n Cost cost;\n };\n\n Edge get_edge(int edge_id) const {\n const auto &e = g[edges[edge_id].first][edges[edge_id].second];\n const auto &re = g[e.to][e.rev];\n return Edge { re.to, e.to, e.cap + re.cap, re.cap, e.cost };\n }\n std::vector<Edge> get_edges() const {\n std::vector<Edge> res(edges.size());\n for (std::size_t i = 0; i < edges.size(); ++i) res[i] = get_edge(i);\n return res;\n }\n\n private:\n static constexpr Cost INF_COST = std::numeric_limits<Cost>::max() / 2;\n static constexpr Cost INF_FLOW = std::numeric_limits<Cap>::max() / 2;\n \n int n;\n std::vector<std::vector<InternalEdge>> g;\n std::vector<Cost> potential;\n std::vector<Cost> dist;\n std::vector<int> prev_vid, prev_eid;\n\n std::vector<std::pair<int, int>> edges;\n\n template <typename Predicate>\n std::pair<Cap, Cost> min_cost_flow(const int s, const int t, const Cap upper_flow, Predicate pred) {\n return min_cost_flow_slope(s, t, upper_flow, pred).back();\n }\n\n template <typename Predicate>\n std::vector<std::pair<Cap, Cost>> min_cost_flow_slope(const int s, const int t, const Cap upper_flow, Predicate pred) {\n if constexpr (init_method == BELLMAN_FORD) {\n bellman_ford(s);\n } else if constexpr (init_method == DIJKSTRA) {\n dijkstra(s);\n } else {\n dag_dp(s);\n }\n update_potential();\n std::vector<std::pair<Cap, Cost>> slope;\n Cap flow = 0;\n Cost cost = 0;\n slope.emplace_back(flow, cost);\n while (dist[t] != INF_COST and flow < upper_flow and pred(flow, cost)) {\n Cap df = upper_flow - flow;\n for (int v = t; v != s; v = prev_vid[v]) {\n df = std::min(df, g[prev_vid[v]][prev_eid[v]].cap);\n }\n assert(df != 0);\n flow += df;\n cost += df * potential[t];\n if (slope.size() >= 2) {\n auto [f0, c0] = *std::next(slope.rbegin());\n auto [f1, c1] = *slope.rbegin();\n if ((f1 - f0) * (cost - c1) == (flow - f1) * (c1 - c0)) slope.pop_back();\n }\n slope.emplace_back(flow, cost);\n for (int v = t; v != s; v = prev_vid[v]) {\n auto &e = g[prev_vid[v]][prev_eid[v]];\n e.cap -= df;\n g[v][e.rev].cap += df;\n }\n dijkstra(s);\n update_potential();\n }\n return slope;\n }\n\n void update_potential() {\n for (int u = 0; u < n; ++u) {\n if (potential[u] != INF_COST) potential[u] += dist[u];\n }\n }\n\n bool update_dist(int u, int eid) {\n if (dist[u] == INF_COST) return false;\n const auto &e = g[u][eid];\n if (e.cap == 0) return false;\n const int v = e.to;\n Cost cost = e.cost + potential[u] - potential[v];\n if constexpr (init_method == DIJKSTRA) {\n assert(cost >= 0);\n }\n if (dist[u] + cost < dist[v]) {\n dist[v] = dist[u] + cost;\n prev_vid[v] = u;\n prev_eid[v] = eid;\n return true;\n }\n return false;\n }\n\n void dijkstra(int s) {\n using State = std::pair<Cost, int>;\n std::priority_queue<State, std::vector<State>, std::greater<State>> pq;\n dist.assign(n, INF_COST);\n pq.emplace(dist[s] = 0, s);\n while (pq.size()) {\n auto [du, u] = pq.top();\n pq.pop();\n if (du != dist[u]) continue;\n for (int i = 0; i < int(g[u].size()); ++i) {\n int v = g[u][i].to;\n if (update_dist(u, i)) {\n pq.emplace(dist[v], v);\n }\n }\n }\n }\n\n void dag_dp(int s) {\n std::vector<int> in(n, 0);\n for (int u = 0; u < n; ++u) {\n for (const auto &e : g[u]) {\n if (e.cap == 0) continue;\n ++in[e.to];\n }\n }\n std::deque<int> dq;\n for (int u = 0; u < n; ++u) {\n if (in[u] == 0) dq.push_back(u);\n }\n dist.assign(n, INF_COST);\n dist[s] = 0;\n while (dq.size()) {\n int u = dq.front();\n dq.pop_front();\n for (int i = 0; i < int(g[u].size()); ++i) {\n const auto &e = g[u][i];\n if (e.cap == 0) continue;\n update_dist(u, i);\n if (--in[e.to] == 0) {\n dq.push_back(e.to);\n }\n }\n }\n assert(std::all_of(in.begin(), in.end(), [](int in_deg) { return in_deg == 0; }));\n }\n\n void bellman_ford(int s) {\n dist.assign(n, INF_COST);\n dist[s] = 0;\n for (bool has_update = true; has_update;) {\n has_update = false;\n for (int u = 0; u < n; ++u) {\n if (dist[u] == INF_COST) continue;\n for (int i = 0; i < int(g[u].size()); ++i) {\n has_update |= update_dist(u, i);\n }\n }\n }\n }\n};\n} // namespace suisen\n\nint main() {\n int n;\n std::cin >> n;\n suisen::CoordinateCompressorBuilder<int> comp_builder;\n std::vector<std::pair<int, int>> ps(n);\n for (auto &[x, y] : ps) {\n std::cin >> x >> y;\n comp_builder.push(x);\n comp_builder.push(y);\n }\n auto comp = comp_builder.build();\n int m = comp.size();\n int s = n + m, t = s + 1;\n\n suisen::MinCostFlow<int, int, suisen::DAG> mcf(n + m + 2);\n for (int i = 0; i < n; ++i) {\n mcf.add_edge(s, i, 1, 0);\n auto [a, b] = ps[i];\n mcf.add_edge(i, n + comp[a], 1, -b);\n mcf.add_edge(i, n + comp[b], 1, -a);\n }\n for (int i = 0; i < m; ++i) {\n mcf.add_edge(n + i, t, 1, 0);\n }\n std::cout << -mcf.min_cost_arbitrary_flow(s, t).second << std::endl;\n return 0;\n}", "accuracy": 1, "time_ms": 190, "memory_kb": 3832, "score_of_the_acc": -0.1438, "final_rank": 10 }, { "submission_id": "aoj_2293_6026685", "code_snippet": "#include <iostream>\n\n#include <algorithm>\n#include <cassert>\n#include <vector>\n\n#include <limits>\n#include <type_traits>\n\nnamespace suisen {\n// ! utility\ntemplate <typename ...Types>\nusing constraints_t = std::enable_if_t<std::conjunction_v<Types...>, std::nullptr_t>;\ntemplate <bool cond_v, typename Then, typename OrElse>\nconstexpr decltype(auto) constexpr_if(Then&& then, OrElse&& or_else) {\n if constexpr (cond_v) {\n return std::forward<Then>(then);\n } else {\n return std::forward<OrElse>(or_else);\n }\n}\n\n// ! function\ntemplate <typename ReturnType, typename Callable, typename ...Args>\nusing is_same_as_invoke_result = std::is_same<std::invoke_result_t<Callable, Args...>, ReturnType>;\ntemplate <typename F, typename T>\nusing is_uni_op = is_same_as_invoke_result<T, F, T>;\ntemplate <typename F, typename T>\nusing is_bin_op = is_same_as_invoke_result<T, F, T, T>;\n\ntemplate <typename Comparator, typename T>\nusing is_comparator = std::is_same<std::invoke_result_t<Comparator, T, T>, bool>;\n\n// ! integral\ntemplate <typename T, typename = constraints_t<std::is_integral<T>>>\nconstexpr int bit_num = std::numeric_limits<std::make_unsigned_t<T>>::digits;\ntemplate <typename T, unsigned int n>\nstruct is_nbit { static constexpr bool value = bit_num<T> == n; };\ntemplate <typename T, unsigned int n>\nstatic constexpr bool is_nbit_v = is_nbit<T, n>::value;\n\n// ?\ntemplate <typename T>\nstruct safely_multipliable {};\ntemplate <>\nstruct safely_multipliable<int> { using type = long long; };\ntemplate <>\nstruct safely_multipliable<long long> { using type = __int128_t; };\ntemplate <>\nstruct safely_multipliable<float> { using type = float; };\ntemplate <>\nstruct safely_multipliable<double> { using type = double; };\ntemplate <>\nstruct safely_multipliable<long double> { using type = long double; };\ntemplate <typename T>\nusing safely_multipliable_t = typename safely_multipliable<T>::type;\n\n} // namespace suisen\n\nnamespace suisen {\ntemplate <typename T>\nclass CoordinateCompressorBuilder {\n public:\n struct Compressor {\n public:\n static constexpr int absent = -1;\n\n // default constructor\n Compressor() : _xs(std::vector<T>{}) {}\n // Construct from strictly sorted vector\n Compressor(const std::vector<T> &xs) : _xs(xs) {\n assert(is_strictly_sorted(xs));\n }\n\n // Return the number of distinct keys.\n int size() const {\n return _xs.size();\n }\n // Check if the element is registered.\n bool has_key(const T &e) const {\n return std::binary_search(_xs.begin(), _xs.end(), e);\n }\n // Compress the element. if not registered, returns `default_value`. (default: Compressor::absent)\n int comp(const T &e, int default_value = absent) const {\n const int res = min_geq_index(e);\n return res != size() and _xs[res] == e ? res : default_value;\n }\n // Restore the element from the index.\n T decomp(const int compressed_index) const {\n return _xs[compressed_index];\n }\n // Compress the element. Equivalent to call `comp(e)`\n int operator[](const T &e) const {\n return comp(e);\n }\n // Return the minimum registered value greater than `e`. if not exists, return `default_value`.\n T min_gt(const T &e, const T &default_value) const {\n auto it = std::upper_bound(_xs.begin(), _xs.end(), e);\n return it == _xs.end() ? default_value : *it;\n }\n // Return the minimum registered value greater than or equal to `e`. if not exists, return `default_value`.\n T min_geq(const T &e, const T &default_value) const {\n auto it = std::lower_bound(_xs.begin(), _xs.end(), e);\n return it == _xs.end() ? default_value : *it;\n }\n // Return the maximum registered value less than `e`. if not exists, return `default_value`\n T max_lt(const T &e, const T &default_value) const {\n auto it = std::upper_bound(_xs.rbegin(), _xs.rend(), e);\n return it == _xs.rend() ? default_value : *it;\n }\n // Return the maximum registered value less than or equal to `e`. if not exists, return `default_value`\n T max_leq(const T &e, const T &default_value) const {\n auto it = std::lower_bound(_xs.rbegin(), _xs.rend(), e);\n return it == _xs.rend() ? default_value : *it;\n }\n // Return the compressed index of the minimum registered value greater than `e`. if not exists, return `compressor.size()`.\n int min_gt_index(const T &e) const {\n return std::upper_bound(_xs.begin(), _xs.end(), e) - _xs.begin();\n }\n // Return the compressed index of the minimum registered value greater than or equal to `e`. if not exists, return `compressor.size()`.\n int min_geq_index(const T &e) const {\n return std::lower_bound(_xs.begin(), _xs.end(), e) - _xs.begin();\n }\n // Return the compressed index of the maximum registered value less than `e`. if not exists, return -1.\n int max_lt_index(const T &e) const {\n return int(_xs.rend() - std::upper_bound(_xs.rbegin(), _xs.rend(), e)) - 1;\n }\n // Return the compressed index of the maximum registered value less than or equal to `e`. if not exists, return -1.\n int max_leq_index(const T &e) const {\n return int(_xs.rend() - std::lower_bound(_xs.rbegin(), _xs.rend(), e)) - 1;\n }\n private:\n std::vector<T> _xs;\n static bool is_strictly_sorted(const std::vector<T> &v) {\n return std::adjacent_find(v.begin(), v.end(), std::greater_equal<T>()) == v.end();\n }\n };\n CoordinateCompressorBuilder() : _xs(std::vector<T>{}) {}\n explicit CoordinateCompressorBuilder(const std::vector<T> &xs) : _xs(xs) {}\n explicit CoordinateCompressorBuilder(std::vector<T> &&xs) : _xs(std::move(xs)) {}\n template <typename Gen, constraints_t<is_same_as_invoke_result<T, Gen, int>> = nullptr>\n CoordinateCompressorBuilder(const int n, Gen generator) {\n reserve(n);\n for (int i = 0; i < n; ++i) push(generator(i));\n }\n // Attempt to preallocate enough memory for specified number of elements.\n void reserve(int n) {\n _xs.reserve(n);\n }\n // Add data.\n void push(const T &first) {\n _xs.push_back(first);\n }\n // Add data.\n void push(T &&first) {\n _xs.push_back(std::move(first));\n }\n // Add data in the range of [first, last). \n template <typename Iterator>\n auto push(const Iterator &first, const Iterator &last) -> decltype(std::vector<T>{}.push_back(*first), void()) {\n for (auto it = first; it != last; ++it) _xs.push_back(*it);\n }\n // Add all data in the container. Equivalent to `push(iterable.begin(), iterable.end())`.\n template <typename Iterable>\n auto push(const Iterable &iterable) -> decltype(std::vector<T>{}.push_back(*iterable.begin()), void()) {\n push(iterable.begin(), iterable.end());\n }\n // Add data.\n template <typename ...Args>\n void emplace(Args &&...args) {\n _xs.emplace_back(std::forward<Args>(args)...);\n }\n // Build compressor.\n auto build() {\n std::sort(_xs.begin(), _xs.end()), _xs.erase(std::unique(_xs.begin(), _xs.end()), _xs.end());\n return Compressor {_xs};\n }\n // Build compressor from vector.\n static auto build(const std::vector<T> &xs) {\n return CoordinateCompressorBuilder(xs).build();\n }\n // Build compressor from vector.\n static auto build(std::vector<T> &&xs) {\n return CoordinateCompressorBuilder(std::move(xs)).build();\n }\n // Build compressor from generator.\n template <typename Gen, constraints_t<is_same_as_invoke_result<T, Gen, int>> = nullptr>\n static auto build(const int n, Gen generator) {\n return CoordinateCompressorBuilder<T>(n, generator).build();\n }\n private:\n std::vector<T> _xs;\n};\n\n} // namespace suisen\n\n#include <queue>\n\nnamespace suisen {\n\nenum MCFPotentialInitializer {\n DAG, BELLMAN_FORD, DIJKSTRA\n};\n\ntemplate <typename Cap, typename Cost, MCFPotentialInitializer init_method = MCFPotentialInitializer::BELLMAN_FORD>\nclass MinCostFlow {\n struct InternalEdge { int to; Cap cap; Cost cost; int rev; };\n public:\n MinCostFlow() : MinCostFlow(0) {}\n MinCostFlow(int n) : n(n), g(n), potential(n, 0), dist(n), prev_vid(n), prev_eid(n) {}\n\n // Returns the id of created edge.\n int add_edge(int u, int v, Cap cap, Cost cost) {\n int edge_id = edges.size();\n edges.emplace_back(u, g[u].size());\n g[u].push_back({ v, cap, cost, int(g[v].size()) });\n g[v].push_back({ u, 0, -cost, int(g[u].size()) - 1 });\n return edge_id;\n }\n\n /**\n * Returns { flow, cost } (flow = min(max_flow, f))\n */\n auto min_cost_max_flow(const int s, const int t, const Cap f) {\n return min_cost_max_flow_slope(s, t, f).back();\n }\n /**\n * Returns { flow, cost } (flow = max_flow)\n */\n auto min_cost_max_flow(const int s, const int t) {\n return min_cost_max_flow_slope(s, t).back();\n }\n /**\n * Returns { flow, cost }\n * amount of flow is arbitrary.\n */\n auto min_cost_arbitrary_flow(const int s, const int t) {\n return min_cost_arbitrary_flow_slope(s, t).back();\n }\n\n auto min_cost_max_flow_slope(const int s, const int t, const Cap f) {\n return min_cost_flow_slope(s, t, f, [](Cap, Cost){ return true; });\n }\n auto min_cost_max_flow_slope(const int s, const int t) {\n return min_cost_max_flow_slope(s, t, INF_FLOW);\n }\n auto min_cost_arbitrary_flow_slope(const int s, const int t) {\n return min_cost_flow_slope(s, t, INF_FLOW, [this, t](Cap, Cost){ return potential[t] < 0; });\n }\n\n struct Edge {\n int from, to;\n Cap cap, flow;\n Cost cost;\n };\n\n Edge get_edge(int edge_id) const {\n const auto &e = g[edges[edge_id].first][edges[edge_id].second];\n const auto &re = g[e.to][e.rev];\n return Edge { re.to, e.to, e.cap + re.cap, re.cap, e.cost };\n }\n std::vector<Edge> get_edges() const {\n std::vector<Edge> res(edges.size());\n for (std::size_t i = 0; i < edges.size(); ++i) res[i] = get_edge(i);\n return res;\n }\n\n private:\n static constexpr Cost INF_COST = std::numeric_limits<Cost>::max() / 2;\n static constexpr Cost INF_FLOW = std::numeric_limits<Cap>::max() / 2;\n \n int n;\n std::vector<std::vector<InternalEdge>> g;\n std::vector<Cost> potential;\n std::vector<Cost> dist;\n std::vector<int> prev_vid, prev_eid;\n\n std::vector<std::pair<int, int>> edges;\n\n template <typename Predicate>\n std::pair<Cap, Cost> min_cost_flow(const int s, const int t, const Cap upper_flow, Predicate pred) {\n return min_cost_flow_slope(s, t, upper_flow, pred).back();\n }\n\n template <typename Predicate>\n std::vector<std::pair<Cap, Cost>> min_cost_flow_slope(const int s, const int t, const Cap upper_flow, Predicate pred) {\n switch (init_method) {\n case BELLMAN_FORD: bellman_ford(s); break;\n case DIJKSTRA: dijkstra(s); break;\n case DAG: dag_dp(s); break;\n }\n update_potential();\n std::vector<std::pair<Cap, Cost>> slope;\n Cap flow = 0;\n Cost cost = 0;\n slope.emplace_back(flow, cost);\n while (dist[t] != INF_COST and flow < upper_flow and pred(flow, cost)) {\n Cap df = upper_flow - flow;\n for (int v = t; v != s; v = prev_vid[v]) {\n df = std::min(df, g[prev_vid[v]][prev_eid[v]].cap);\n }\n assert(df != 0);\n flow += df;\n cost += df * potential[t];\n if (slope.size() >= 2) {\n auto [f0, c0] = *std::next(slope.rbegin());\n auto [f1, c1] = *slope.rbegin();\n if ((f1 - f0) * (cost - c1) == (flow - f1) * (c1 - c0)) slope.pop_back();\n }\n slope.emplace_back(flow, cost);\n for (int v = t; v != s; v = prev_vid[v]) {\n auto &e = g[prev_vid[v]][prev_eid[v]];\n e.cap -= df;\n g[v][e.rev].cap += df;\n }\n dijkstra(s);\n update_potential();\n }\n return slope;\n }\n\n void update_potential() {\n for (int u = 0; u < n; ++u) {\n if (potential[u] != INF_COST) potential[u] += dist[u];\n }\n }\n\n bool update_dist(int u, int eid) {\n if (dist[u] == INF_COST) return false;\n const auto &e = g[u][eid];\n if (e.cap == 0) return false;\n const int v = e.to;\n Cost cost = e.cost + potential[u] - potential[v];\n if constexpr (init_method == DIJKSTRA) {\n assert(cost >= 0);\n }\n if (dist[u] + cost < dist[v]) {\n dist[v] = dist[u] + cost;\n prev_vid[v] = u;\n prev_eid[v] = eid;\n return true;\n }\n return false;\n }\n\n void dijkstra(int s) {\n using State = std::pair<Cost, int>;\n std::priority_queue<State, std::vector<State>, std::greater<State>> pq;\n dist.assign(n, INF_COST);\n pq.emplace(dist[s] = 0, s);\n while (pq.size()) {\n auto [du, u] = pq.top();\n pq.pop();\n if (du != dist[u]) continue;\n for (int i = 0; i < int(g[u].size()); ++i) {\n int v = g[u][i].to;\n if (update_dist(u, i)) {\n pq.emplace(dist[v], v);\n }\n }\n }\n }\n\n void dag_dp(int s) {\n std::vector<int> in(n, 0);\n for (int u = 0; u < n; ++u) {\n for (const auto &e : g[u]) {\n if (e.cap == 0) continue;\n ++in[e.to];\n }\n }\n std::deque<int> dq;\n for (int u = 0; u < n; ++u) {\n if (in[u] == 0) dq.push_back(u);\n }\n dist.assign(n, INF_COST);\n dist[s] = 0;\n while (dq.size()) {\n int u = dq.front();\n dq.pop_front();\n for (int i = 0; i < int(g[u].size()); ++i) {\n const auto &e = g[u][i];\n if (e.cap == 0) continue;\n update_dist(u, i);\n if (--in[e.to] == 0) {\n dq.push_back(e.to);\n }\n }\n }\n assert(std::all_of(in.begin(), in.end(), [](int in_deg) { return in_deg == 0; }));\n }\n\n void bellman_ford(int s) {\n dist.assign(n, INF_COST);\n dist[s] = 0;\n for (bool has_update = true; has_update;) {\n has_update = false;\n for (int u = 0; u < n; ++u) {\n if (dist[u] == INF_COST) continue;\n for (int i = 0; i < int(g[u].size()); ++i) {\n has_update |= update_dist(u, i);\n }\n }\n }\n }\n};\n} // namespace suisen\n\nint main() {\n int n;\n std::cin >> n;\n suisen::CoordinateCompressorBuilder<int> comp_builder;\n std::vector<std::pair<int, int>> ps(n);\n for (auto &[x, y] : ps) {\n std::cin >> x >> y;\n comp_builder.push(x);\n comp_builder.push(y);\n }\n auto comp = comp_builder.build();\n int m = comp.size();\n int s = n + m, t = s + 1;\n\n suisen::MinCostFlow<int, int, suisen::DAG> mcf(n + m + 2);\n for (int i = 0; i < n; ++i) {\n mcf.add_edge(s, i, 1, 0);\n auto [a, b] = ps[i];\n mcf.add_edge(i, n + comp[a], 1, -b);\n mcf.add_edge(i, n + comp[b], 1, -a);\n }\n for (int i = 0; i < m; ++i) {\n mcf.add_edge(n + i, t, 1, 0);\n }\n std::cout << -mcf.min_cost_arbitrary_flow(s, t).second << std::endl;\n return 0;\n}", "accuracy": 1, "time_ms": 190, "memory_kb": 3864, "score_of_the_acc": -0.1445, "final_rank": 11 } ]
aoj_2291_cpp
B: Brilliant Stars ICPC で良い成績を収めるには修行が欠かせない.うさぎは ICPC で勝ちたいので,今日も修行をすることにした. 今日の修行は,夜空に輝くたくさんの星たちを観測して,視力と集中力を鍛えようというものである.うさぎは独自のセンスで星の特徴を記録していくことができる. うさぎは「星の観測日記」として記録した星の特徴をノートにつけていくことにした.ここで,「似ている」 2 つの星をともに記録することは避けたい.2 つの星が「似ている」かどうかは,すべてうさぎの判断で決まっている.「似ている」かどうかの関係はあまり複雑ではなく,以下の条件を満たす. 条件: 異なる 4 つの星 A, B, C, D があり,A と B,B と C,C と D がそれぞれ「似ている」とする.このとき,A と C が「似ている」か,B と D が「似ている」か,あるいはその両方が成り立つ. うさぎは「似ている」 2 つの星を記録しないようになるべくたくさんの星を記録したい.また,そのようにする方法が何通りあるかも知りたい. Input N M X 1 Y 1 ... X M Y M N は星の個数, M は「似ている」 2 つの星の組の個数である.星は 1 以上 N 以下の整数の番号により表され, X i , Y i (1 ≤ i ≤ M ) は星 X i と星 Y i が「似ている」ことを表す. 1 ≤ N ≤ 100,000,0 ≤ M ≤ 200,000,1 ≤ X i < Y i ≤ N を満たす.( X i , Y i ) として同一の組は複数回現れない.「似ている」かどうかの関係は,問題文で指定された条件を満たす. Output 1 行目に,「似ている」 2 つの星を記録しないように記録できる星の個数の最大値を,2 行目に,その最大値を実現できる星の組み合わせの個数 (記録する順番のみを変えたものは同じと考える) を 1,000,000,009 で割った余りを出力せよ. Sample Input 1 4 3 1 2 1 3 1 4 Sample Output 1 3 1 Sample Input 2 11 5 1 2 3 4 5 6 7 8 9 10 Sample Output 2 6 32 Sample Input 3 9 14 1 2 1 3 2 3 3 4 3 5 3 6 3 7 3 8 3 9 4 5 4 6 5 6 7 8 8 9 Sample Output 3 4 6
[ { "submission_id": "aoj_2291_10866253", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nconst ll INF = 1ll << 60;\n#define REP(i, n) for(ll i=0; i <ll(n); i++)\ntemplate <class T> using V = vector<T>;\ntemplate <class A, class B>\nbool chmax(A &a, B b) { return a < b && (a=b, true); }\ntemplate <class A, class B>\nbool chmin(A &a, B b) { return b < a && (a=b, true); }\n\n\nvoid testcase(){\n int N, M; cin >> N >> M;\n V<V<int>> adj(N);\n REP(i,M){\n int u,v; cin >> u >> v; u--; v--;\n adj[u].push_back(v);\n adj[v].push_back(u);\n }\n V<int> deg(N);\n REP(i,N) deg[i] = adj[i].size();\n V<V<int>> que(N);\n REP(i,N) REP(j,deg[i]+1) que[j].push_back(i);\n V<int> peo;\n for(int d=N-1; d>=0; d--) for(int v : que[d]) if(deg[v] == d){\n deg[v] = -1;\n peo.push_back(v);\n for(int w : adj[v]) deg[w]--;\n }\n reverse(peo.begin(), peo.end());\n V<int> hit(N), used(N), ord(N);\n REP(i,N) ord[peo[i]] = i;\n for(int v : peo) if(!hit[v]){\n hit[v] = used[v] = 1;\n for(int w : adj[v]) hit[w]++;\n }\n int val = 0;\n for(int x : used) val += x;\n ll cnt = 1;\n for(int v : peo) if(used[v]){\n ll t = 1;\n for(int w : adj[v]) if(ord[v] < ord[w] && hit[w] == 1) t++;\n cnt = cnt * t % 1000000009;\n }\n cout << val << \"\\n\" << cnt << \"\\n\";\n}\n\nint main() {\n cin.tie(nullptr)->sync_with_stdio(false);\n testcase();\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 15540, "score_of_the_acc": -0.2854, "final_rank": 4 }, { "submission_id": "aoj_2291_10207074", "code_snippet": "// AOJ #2291\n// Brilliant Stars 2025.2.9\n\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\n#define gc() getchar_unlocked()\n#define pc(c) putchar_unlocked(c)\n\nint Cin() {\n\tint n = 0, c = gc();\n\tif (c == '-') {\tc = gc();\n\t\tdo n = 10*n + (c & 0xf), c = gc(); while (c >= '0');\n\t\treturn -n;\n\t}\n\tdo n = 10*n + (c & 0xf), c = gc(); while (c >= '0');\n\treturn n;\n}\n\nvoid Cout(ll n) {\n\tchar b[30];\n\tif (!n) pc('0');\n\telse {\n\t\tif (n < 0) pc('-'), n = -n;\n\t\tint i = 0; while (n) b[i++] = n % 10 + '0', n /= 10;\n\t\twhile (i--) pc(b[i]);\n\t}\n\tpc('\\n');\n}\n\nconst ll MOD = 1000000009;\n \nint N, M;\nvector<vector<int>> adj;\n \ninline bool isAdjacent(int u, int v) {\n return binary_search(adj[u].begin(), adj[u].end(), v);\n}\n \npair<ll, ll> solveSubset(const vector<int>& V) {\n if (V.empty()) return {0, 1};\n if (V.size() == 1) return {1, 1};\n \n vector<bool> inLocal(N + 1, false);\n for (int v : V) inLocal[v] = true;\n \n vector<bool> visited(N + 1, false);\n vector<vector<int>> compList;\n for (int v : V) {\n if (!visited[v]) {\n vector<int> comp;\n queue<int> q;\n q.push(v);\n visited[v] = true;\n while (!q.empty()) {\n int cur = q.front();\n q.pop();\n comp.push_back(cur);\n for (int nb : adj[cur]) {\n if (inLocal[nb] && !visited[nb]) {\n visited[nb] = true;\n q.push(nb);\n }\n }\n }\n compList.push_back(comp);\n }\n }\n \n if (compList.size() > 1) {\n ll totalSize = 0, totalWays = 1;\n for (auto &comp : compList) {\n auto subRes = solveSubset(comp);\n totalSize += subRes.first;\n totalWays = (totalWays * subRes.second) % MOD;\n }\n return {totalSize, totalWays};\n }\n \n set<int> rem;\n for (int v : V) rem.insert(v);\n\n vector<vector<int>> compComp;\n while (!rem.empty()) {\n int start = *rem.begin();\n rem.erase(rem.begin());\n vector<int> comp;\n queue<int> q;\n q.push(start);\n comp.push_back(start);\n while (!q.empty()) {\n int cur = q.front();\n q.pop();\n for (auto it = rem.begin(); it != rem.end(); ) {\n int candidate = *it;\n if (!isAdjacent(cur, candidate)) {\n q.push(candidate);\n comp.push_back(candidate);\n it = rem.erase(it);\n } else ++it;\n }\n }\n compComp.push_back(comp);\n }\n \n ll bestSize = 0, waysCount = 0;\n for (auto &comp : compComp) {\n auto subRes = solveSubset(comp);\n if (subRes.first > bestSize) {\n bestSize = subRes.first;\n waysCount = subRes.second;\n } else if (subRes.first == bestSize) {\n waysCount = (waysCount + subRes.second) % MOD;\n }\n }\n return {bestSize, waysCount};\n}\n \nint main() {\n N = Cin(), M = Cin();\n adj.resize(N + 1);\n for (int i = 0; i < M; i++){\n int u = Cin(), v = Cin();\n adj[u].push_back(v);\n adj[v].push_back(u);\n }\n for (int i = 1; i <= N; i++){\n sort(adj[i].begin(), adj[i].end());\n }\n \n vector<int> allVerts;\n allVerts.reserve(N);\n for (int i = 1; i <= N; i++){\n allVerts.push_back(i);\n }\n \n auto ans = solveSubset(allVerts);\n Cout(ans.first), Cout(ans.second % MOD);\n return 0;\n}", "accuracy": 1, "time_ms": 150, "memory_kb": 26488, "score_of_the_acc": -0.631, "final_rank": 8 }, { "submission_id": "aoj_2291_9518633", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\nconst ll MOD = 1e9 + 9;\n\nusing G = vector<vector<int>>;\nstruct SCC {\n int n;\n const G &g;\n G edge, redge;\n vector<int> comp, order, used;\n \n SCC(int n, G &g) : n(n), g(g), edge(n), redge(n), comp(n, -1), used(n) {\n for (int i = 0; i < n; ++i) {\n for (auto e : g[i]) {\n edge[i].emplace_back(e);\n redge[e].emplace_back(i);\n }\n }\n }\n \n void dfs(int idx) {\n if (used[idx]) return;\n used[idx] = true;\n for (int to : edge[idx]) dfs(to);\n order.emplace_back(idx);\n }\n \n void rdfs(int idx, int cnt) {\n if (comp[idx] != -1) return;\n comp[idx] = cnt;\n for (int to : redge[idx]) rdfs(to, cnt);\n }\n \n G build() {\n G t;\n for (int i = 0; i < n; ++i) dfs(i);\n reverse(order.begin(), order.end());\n int ptr = 0;\n for (int i : order) {\n if (comp[i] == -1) {\n rdfs(i, ptr), ptr++;\n }\n }\n \n t.resize(ptr);\n for (int i = 0; i < n; ++i) {\n t[comp[i]].emplace_back(i);\n }\n return t;\n }\n \n};\n\n\nint main() {\n ll N, M; cin >> N >> M;\n vector<int> deg(N, 0);\n vector< array<int, 2> > edge(M);\n for (int i = 0; i < M; ++i) {\n int x, y; cin >> x >> y;\n x--, y--;\n edge[i] = {x, y};\n deg[x]++;\n deg[y]++;\n }\n \n G graph(N);\n for (auto [x, y] : edge) {\n if (deg[x] <= deg[y]) graph[x].emplace_back(y);\n if (deg[y] <= deg[x]) graph[y].emplace_back(x);\n }\n vector<int> in(N, 0), out(N, 0);\n for (int u = 0; u < N; ++u) {\n for (auto v : graph[u]) {\n in[v]++;\n out[u]++;\n }\n }\n \n SCC hoge(N, graph);\n G scc = hoge.build();\n \n ll mx = 0, mult = 1;\n for (auto vec : scc) {\n int n = vec.size();\n int v = vec[0];\n \n if (in[v] == n-1) {\n mx++;\n mult *= n;\n mult %= MOD;\n }\n }\n cout << mx << endl;\n cout << mult << endl;\n \n return 0;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 27472, "score_of_the_acc": -0.6003, "final_rank": 7 }, { "submission_id": "aoj_2291_8305039", "code_snippet": "#include <iostream>\n#include <vector>\n#include <queue>\n#include <tuple>\nusing namespace std;\n\nint N;\nint M, X[200009], Y[200009];\nint Deg[200009];\nint Col[200009], NumCol;\nint Sta[200009], NumSta;\nint Height[200009];\nvector<int> G[200009];\nvector<int> H[200009];\nvector<int> I[200009];\nqueue<tuple<vector<int>, vector<pair<int, int>>, int>> Q;\n\nvoid dfs(int pos) {\n\tCol[pos] = NumCol;\n\tfor (int to : G[pos]) {\n\t\tif (Col[to] != -1) continue;\n\t\tdfs(to);\n\t}\n}\n\nvoid dfs2(int pos) {\n\tSta[pos] = NumSta;\n\tfor (int to : I[pos]) {\n\t\tif (Sta[to] != -1) continue;\n\t\tdfs2(to);\n\t}\n}\n\nvoid dfs3(int pos, int dep) {\n\tHeight[pos] = dep;\n\tfor (int to : H[pos]) {\n\t\tint nex_dep = dep + 1; if (H[to].size() >= 2) nex_dep = 0;\n\t\tdfs3(to, nex_dep);\n\t}\n}\n\nint MakeTree(vector<int> V1, vector<pair<int, int>> E1) {\n\tQ.push(make_tuple(V1, E1, -1));\n\tint Root = -1;\n\n\twhile (!Q.empty()) {\n\t\tvector<int> V = get<0>(Q.front());\n\t\tvector<pair<int, int>> E = get<1>(Q.front());\n\t\tint Parent = get<2>(Q.front());\n\t\tQ.pop();\n\n\t\t// Step A. Find Degree\n\t\tfor (int i : V) Deg[i] = 0;\n\t\tfor (int i = 0; i < E.size(); i++) Deg[E[i].first] += 1;\n\t\tfor (int i = 0; i < E.size(); i++) Deg[E[i].second] += 1;\n\n\t\t// Step B. Get Maximum\n\t\tint MaxID = -1;\n\t\tfor (int i = 0; i < V.size(); i++) {\n\t\t\tif (Deg[V[i]] == V.size() - 1) { MaxID = V[i]; break; }\n\t\t}\n\t\tif (Parent == -1) Root = MaxID;\n\t\tif (Parent != -1) H[Parent].push_back(MaxID);\n\t\tif (V.size() == 1) continue;\n\n\t\t// Step C. Make Graph\n\t\tfor (int i : V) G[i].clear();\n\t\tfor (int i : V) Col[i] = -1;\n\t\tfor (pair<int, int> i : E) {\n\t\t\tif (i.first != MaxID && i.second != MaxID) {\n\t\t\t\tG[i.first].push_back(i.second);\n\t\t\t\tG[i.second].push_back(i.first);\n\t\t\t}\n\t\t}\n\n\t\t// Step D. DFS\n\t\tNumCol = 0;\n\t\tfor (int i : V) {\n\t\t\tif (Col[i] != -1 || i == MaxID) continue;\n\t\t\tdfs(i);\n\t\t\tNumCol += 1;\n\t\t}\n\n\t\t// Step E. Recursion\n\t\tvector<vector<int>> V2(NumCol);\n\t\tvector<vector<pair<int, int>>> E2(NumCol);\n\t\tfor (int i : V) {\n\t\t\tif (i != MaxID) V2[Col[i]].push_back(i);\n\t\t}\n\t\tfor (pair<int, int> i : E) {\n\t\t\tif (i.first == MaxID || i.second == MaxID) continue;\n\t\t\tE2[Col[i.first]].push_back(i);\n\t\t}\n\t\tfor (int i = 0; i < V2.size(); i++) Q.push(make_tuple(V2[i], E2[i], MaxID));\n\t}\n\n\t// Return\n\treturn Root;\n}\n\nint main() {\n\t// Step 1. Input\n\tcin >> N >> M;\n\tfor (int i = 1; i <= M; i++) cin >> X[i] >> Y[i];\n\tfor (int i = 1; i <= M; i++) I[X[i]].push_back(Y[i]);\n\tfor (int i = 1; i <= M; i++) I[Y[i]].push_back(X[i]);\n\n\t// Step 2. Connected Components\n\tfor (int i = 1; i <= N; i++) Sta[i] = -1;\n\tfor (int i = 1; i <= N; i++) {\n\t\tif (Sta[i] != -1) continue;\n\t\tdfs2(i);\n\t\tNumSta += 1;\n\t}\n\n\t// Step 3. Make Tree\n\tvector<vector<int>> V(NumSta);\n\tvector<vector<pair<int, int>>> E(NumSta);\n\tvector<int> Root;\n\tfor (int i = 1; i <= N; i++) V[Sta[i]].push_back(i);\n\tfor (int i = 1; i <= M; i++) E[Sta[X[i]]].push_back(make_pair(X[i], Y[i]));\n\tfor (int i = 0; i < NumSta; i++) {\n\t\tint ret = MakeTree(V[i], E[i]);\n\t\tRoot.push_back(ret);\n\t}\n\n\t// Step 4. Calculate Answer\n\tlong long mod = 1000000009;\n\tlong long Answer1 = 0;\n\tlong long Answer2 = 1;\n\tfor (int i : Root) dfs3(i, (H[i].size() >= 2 ? 0 : 1));\n\tfor (int i = 1; i <= N; i++) {\n\t\tif (H[i].size() >= 1) continue;\n\t\tAnswer1 += 1;\n\t\tAnswer2 *= (long long)Height[i];\n\t\tAnswer2 %= mod;\n\t}\n\tcout << Answer1 << endl;\n\tcout << Answer2 << endl;\n\treturn 0;\n}", "accuracy": 1, "time_ms": 680, "memory_kb": 44116, "score_of_the_acc": -1.3646, "final_rank": 14 }, { "submission_id": "aoj_2291_6039541", "code_snippet": "//#pragma GCC optimize(\"O3\")\n//#pragma GCC optimize(\"unroll-loops\")\n#include<iostream>\n#include<string>\n#include<cstdio>\n#include<vector>\n#include<cmath>\n#include<algorithm>\n#include<functional>\n#include<iomanip>\n#include<queue>\n#include<ciso646>\n#include<random>\n#include<map>\n#include<set>\n#include<bitset>\n#include<stack>\n#include<unordered_map>\n#include<unordered_set>\n#include<utility>\n#include<cassert>\n#include<complex>\n#include<numeric>\n#include<array>\nusing namespace std;\n\n//#define int long long\ntypedef long long ll;\n\ntypedef unsigned long long ul;\ntypedef unsigned int ui;\nconst ll mod = 1000000009;\nconst ll INF = mod * mod;\ntypedef pair<int, int>P;\n\n#define rep(i,n) for(int i=0;i<n;i++)\n#define per(i,n) for(int i=n-1;i>=0;i--)\n#define Rep(i,sta,n) for(int i=sta;i<n;i++)\n#define rep1(i,n) for(int i=1;i<=n;i++)\n#define per1(i,n) for(int i=n;i>=1;i--)\n#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)\n#define all(v) (v).begin(),(v).end()\ntypedef pair<ll, ll> LP;\ntypedef long double ld;\ntypedef pair<ld, ld> LDP;\nconst ld eps = 1e-8;\nconst ld pi = acosl(-1.0);\n\nll mod_pow(ll x, ll n, ll m = mod) {\n\tif (n < 0) {\n\t\tll res = mod_pow(x, -n, m);\n\t\treturn mod_pow(res, m - 2, m);\n\t}\n\tif (abs(x) >= m)x %= m;\n\tif (x < 0)x += m;\n\tll res = 1;\n\twhile (n) {\n\t\tif (n & 1)res = res * x % m;\n\t\tx = x * x % m; n >>= 1;\n\t}\n\treturn res;\n}\nstruct modint {\n\tll n;\n\tmodint() :n(0) { ; }\n\tmodint(ll m) :n(m) {\n\t\tif (n >= mod)n %= mod;\n\t\telse if (n < 0)n = (n % mod + mod) % mod;\n\t}\n\toperator int() { return n; }\n};\nbool operator==(modint a, modint b) { return a.n == b.n; }\nmodint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }\nmodint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }\nmodint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }\nmodint operator+(modint a, modint b) { return a += b; }\nmodint operator-(modint a, modint b) { return a -= b; }\nmodint operator*(modint a, modint b) { return a *= b; }\nmodint operator^(modint a, ll n) {\n\tif (n == 0)return modint(1);\n\tmodint res = (a * a) ^ (n / 2);\n\tif (n % 2)res = res * a;\n\treturn res;\n}\n\nll inv(ll a, ll p) {\n\treturn (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);\n}\nmodint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }\nmodint operator/=(modint& a, modint b) { a = a / b; return a; }\nconst int max_n = 1 << 10;\nmodint fact[max_n], factinv[max_n];\nvoid init_f() {\n\tfact[0] = modint(1);\n\tfor (int i = 0; i < max_n - 1; i++) {\n\t\tfact[i + 1] = fact[i] * modint(i + 1);\n\t}\n\tfactinv[max_n - 1] = modint(1) / fact[max_n - 1];\n\tfor (int i = max_n - 2; i >= 0; i--) {\n\t\tfactinv[i] = factinv[i + 1] * modint(i + 1);\n\t}\n}\nmodint comb(int a, int b) {\n\tif (a < 0 || b < 0 || a < b)return 0;\n\treturn fact[a] * factinv[b] * factinv[a - b];\n}\nmodint combP(int a, int b) {\n\tif (a < 0 || b < 0 || a < b)return 0;\n\treturn fact[a] * factinv[a - b];\n}\n\nusing mp = pair<int, modint>;\nvoid merge(mp& a, mp b) {\n\tif (a.first < b.first)a = b;\n\telse if (a.first == b.first)a.second += b.second;\n}\nvoid solve() {\n\tint n, m; cin >> n >> m;\n\tvector<vector<int>> G(n);\n\trep(i, m) {\n\t\tint a, b; cin >> a >> b; a--; b--;\n\t\tG[a].push_back(b);\n\t\tG[b].push_back(a);\n\t}\n\tauto comp = [&](int i, int j) {\n\t\treturn G[i].size() < G[j].size();\n\t};\n\tvector<int> ori(n); rep(i, n)ori[i] = i;\n\tsort(all(ori), comp);\n\tvector<int> rev(n); rep(i, n)rev[ori[i]] = i;\n\tvector<bool> ban(n);\n\tvector<mp> dp(n);\n\trep(i, n) {\n\t\tint id = ori[i];\n\t\tmp s2 = { 0,1 };\n\t\tfor (int to : G[id]) {\n\t\t\tint j = rev[to];\n\t\t\tif (j < i && !ban[j]) {\n\t\t\t\ts2.first += dp[j].first;\n\t\t\t\ts2.second *= dp[j].second;\n\t\t\t\tban[j] = true;\n\t\t\t}\n\t\t}\n\t\tmp s1 = { 1,1 };\n\t\tmerge(s1, s2);\n\t\tdp[i] = s1;\n\t}\n\tint ans1 = 0;\n\tmodint ans2 = 1;\n\trep(i, n)if (!ban[i]) {\n\t\tans1 += dp[i].first;\n\t\tans2 *= dp[i].second;\n\t}\n\tcout << ans1 << \"\\n\";\n\tcout << ans2 << \"\\n\";\n}\n\n\n\nsigned main() {\n\tios::sync_with_stdio(false);\n\tcin.tie(0);\n\tcout << fixed << setprecision(8);\n\t//init_f();\n\t//init();\n\t//int t; cin >> t; rep(i, t)\n\tsolve();\n\treturn 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 11008, "score_of_the_acc": -0.1776, "final_rank": 2 }, { "submission_id": "aoj_2291_6039505", "code_snippet": "//#pragma GCC optimize(\"O3\")\n//#pragma GCC optimize(\"unroll-loops\")\n#include<iostream>\n#include<string>\n#include<cstdio>\n#include<vector>\n#include<cmath>\n#include<algorithm>\n#include<functional>\n#include<iomanip>\n#include<queue>\n#include<ciso646>\n#include<random>\n#include<map>\n#include<set>\n#include<bitset>\n#include<stack>\n#include<unordered_map>\n#include<unordered_set>\n#include<utility>\n#include<cassert>\n#include<complex>\n#include<numeric>\n#include<array>\nusing namespace std;\n\n//#define int long long\ntypedef long long ll;\n\ntypedef unsigned long long ul;\ntypedef unsigned int ui;\nconst ll mod = 1000000009;\nconst ll INF = mod * mod;\ntypedef pair<int, int>P;\n\n#define rep(i,n) for(int i=0;i<n;i++)\n#define per(i,n) for(int i=n-1;i>=0;i--)\n#define Rep(i,sta,n) for(int i=sta;i<n;i++)\n#define rep1(i,n) for(int i=1;i<=n;i++)\n#define per1(i,n) for(int i=n;i>=1;i--)\n#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)\n#define all(v) (v).begin(),(v).end()\ntypedef pair<ll, ll> LP;\ntypedef long double ld;\ntypedef pair<ld, ld> LDP;\nconst ld eps = 1e-8;\nconst ld pi = acosl(-1.0);\n\nll mod_pow(ll x, ll n, ll m = mod) {\n\tif (n < 0) {\n\t\tll res = mod_pow(x, -n, m);\n\t\treturn mod_pow(res, m - 2, m);\n\t}\n\tif (abs(x) >= m)x %= m;\n\tif (x < 0)x += m;\n\tll res = 1;\n\twhile (n) {\n\t\tif (n & 1)res = res * x % m;\n\t\tx = x * x % m; n >>= 1;\n\t}\n\treturn res;\n}\nstruct modint {\n\tll n;\n\tmodint() :n(0) { ; }\n\tmodint(ll m) :n(m) {\n\t\tif (n >= mod)n %= mod;\n\t\telse if (n < 0)n = (n % mod + mod) % mod;\n\t}\n\toperator int() { return n; }\n};\nbool operator==(modint a, modint b) { return a.n == b.n; }\nmodint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }\nmodint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }\nmodint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }\nmodint operator+(modint a, modint b) { return a += b; }\nmodint operator-(modint a, modint b) { return a -= b; }\nmodint operator*(modint a, modint b) { return a *= b; }\nmodint operator^(modint a, ll n) {\n\tif (n == 0)return modint(1);\n\tmodint res = (a * a) ^ (n / 2);\n\tif (n % 2)res = res * a;\n\treturn res;\n}\n\nll inv(ll a, ll p) {\n\treturn (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);\n}\nmodint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }\nmodint operator/=(modint& a, modint b) { a = a / b; return a; }\nconst int max_n = 1 << 10;\nmodint fact[max_n], factinv[max_n];\nvoid init_f() {\n\tfact[0] = modint(1);\n\tfor (int i = 0; i < max_n - 1; i++) {\n\t\tfact[i + 1] = fact[i] * modint(i + 1);\n\t}\n\tfactinv[max_n - 1] = modint(1) / fact[max_n - 1];\n\tfor (int i = max_n - 2; i >= 0; i--) {\n\t\tfactinv[i] = factinv[i + 1] * modint(i + 1);\n\t}\n}\nmodint comb(int a, int b) {\n\tif (a < 0 || b < 0 || a < b)return 0;\n\treturn fact[a] * factinv[b] * factinv[a - b];\n}\nmodint combP(int a, int b) {\n\tif (a < 0 || b < 0 || a < b)return 0;\n\treturn fact[a] * factinv[a - b];\n}\n\nusing mp = pair<int, modint>;\nvoid merge(mp& a, mp b) {\n\tif (a.first < b.first)a = b;\n\telse if (a.first == b.first)a.second += b.second;\n}\nvoid solve() {\n\tint n, m; cin >> n >> m;\n\tvector<set<int>> G(n);\n\trep(i, m) {\n\t\tint a, b; cin >> a >> b; a--; b--;\n\t\tG[a].insert(b);\n\t\tG[b].insert(a);\n\t}\n\tvector<int> trans(n);\n\tvector<bool> ban(n);\n\tfunction<mp(vector<int>)> calc = [&](vector<int> ids)->mp {\n\t\trep(j, ids.size()) {\n\t\t\ttrans[ids[j]] = j;\n\t\t}\n\t\tvector<bool> used(ids.size());\n\t\tpair<int, modint> res = { 0,1 };\n\t\trep(i, ids.size()) {\n\t\t\tif (used[i])continue;\n\t\t\tqueue<int> q; q.push(ids[i]); used[i] = true;\n\t\t\tvector<int> nids;\n\t\t\twhile (!q.empty()) {\n\t\t\t\tint id = q.front(); q.pop();\n\t\t\t\tnids.push_back(id);\n\t\t\t\tfor (int to : G[id])if (!used[trans[to]]) {\n\t\t\t\t\tused[trans[to]] = true;\n\t\t\t\t\tq.push(to);\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (nids.size() == 1) {\n\t\t\t\tres.first++;\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\t//cout << nids.size() << \"\\n\";\n\t\t\tvector<int> ones;\n\t\t\trep(j, nids.size())if (G[nids[j]].size() == nids.size() - 1) {\n\t\t\t\tones.push_back(nids[j]);\n\t\t\t\tban[nids[j]] = true;\n\t\t\t}\n\t\t\tassert(ones.size());\n\t\t\tmp s1 = { 1,ones.size() };\n\t\t\tvector<int> nv;\n\t\t\trep(j, nids.size())if (G[nids[j]].size() < nids.size() - 1)nv.push_back(nids[j]);\n\t\t\tfor (int id : ones)for (int to : G[id])if (!ban[to])G[to].erase(id);\n\t\t\tmp s2 = calc(nv);\n\t\t\tmerge(s1, s2);\n\t\t\tres.first += s1.first;\n\t\t\tres.second *= s1.second;\n\t\t}\n\t\treturn res;\n\t};\n\tvector<int> ori(n); rep(i, n)ori[i] = i;\n\tmp p = calc(ori);\n\tint ans1 = p.first;\n\tmodint ans2 = p.second;\n\tcout << ans1 << \"\\n\";\n\tcout << ans2 << \"\\n\";\n}\n\n\n\nsigned main() {\n\tios::sync_with_stdio(false);\n\tcin.tie(0);\n\tcout << fixed << setprecision(8);\n\t//init_f();\n\t//init();\n\t//int t; cin >> t; rep(i, t)\n\tsolve();\n\treturn 0;\n}", "accuracy": 1, "time_ms": 1660, "memory_kb": 29688, "score_of_the_acc": -1.5453, "final_rank": 15 }, { "submission_id": "aoj_2291_5006400", "code_snippet": "#include <bits/stdc++.h>\n#define FOR(i, n, m) for(ll i = (n); i < (ll)(m); i++)\n#define REP(i, n) FOR(i, 0, n)\n#define ALL(v) v.begin(), v.end()\n#define pb push_back\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing P = pair<ll, ll>;\nconstexpr ll inf = 1000000000;\nconstexpr ll mod = 1000000009;\nconstexpr long double eps = 1e-6;\n \ntemplate<typename T1, typename T2>\nostream& operator<<(ostream& os, pair<T1, T2> p) {\n os << to_string(p.first) << \" \" << to_string(p.second);\n return os;\n}\ntemplate<typename T>\nostream& operator<<(ostream& os, vector<T>& v) {\n REP(i, v.size()) {\n if(i) os << \" \";\n os << v[i];\n }\n return os;\n}\n \nstruct modint {\n ll n;\npublic:\n modint(const ll n = 0) : n((n % mod + mod) % mod) {}\n static modint pow(modint a, int m) {\n modint r = 1;\n while(m > 0) {\n if(m & 1) { r *= a; }\n a = (a * a); m /= 2;\n }\n return r;\n }\n modint &operator++() { *this += 1; return *this; }\n modint &operator--() { *this -= 1; return *this; }\n modint operator++(int) { modint ret = *this; *this += 1; return ret; }\n modint operator--(int) { modint ret = *this; *this -= 1; return ret; }\n modint operator~() const { return (this -> pow(n, mod - 2)); } // inverse\n friend bool operator==(const modint& lhs, const modint& rhs) {\n return lhs.n == rhs.n;\n }\n friend bool operator<(const modint& lhs, const modint& rhs) {\n return lhs.n < rhs.n;\n }\n friend bool operator>(const modint& lhs, const modint& rhs) {\n return lhs.n > rhs.n;\n }\n friend modint &operator+=(modint& lhs, const modint& rhs) {\n lhs.n += rhs.n;\n if (lhs.n >= mod) lhs.n -= mod;\n return lhs;\n }\n friend modint &operator-=(modint& lhs, const modint& rhs) {\n lhs.n -= rhs.n;\n if (lhs.n < 0) lhs.n += mod;\n return lhs;\n }\n friend modint &operator*=(modint& lhs, const modint& rhs) {\n lhs.n = (lhs.n * rhs.n) % mod;\n return lhs;\n }\n friend modint &operator/=(modint& lhs, const modint& rhs) {\n lhs.n = (lhs.n * (~rhs).n) % mod;\n return lhs;\n }\n friend modint operator+(const modint& lhs, const modint& rhs) {\n return modint(lhs.n + rhs.n);\n }\n friend modint operator-(const modint& lhs, const modint& rhs) {\n return modint(lhs.n - rhs.n);\n }\n friend modint operator*(const modint& lhs, const modint& rhs) {\n return modint(lhs.n * rhs.n);\n }\n friend modint operator/(const modint& lhs, const modint& rhs) {\n return modint(lhs.n * (~rhs).n);\n }\n};\nistream& operator>>(istream& is, modint m) { is >> m.n; return is; }\nostream& operator<<(ostream& os, modint m) { os << m.n; return os; }\n \n#define MAX_N 1010101\nlong long extgcd(long long a, long long b, long long& x, long long& y) {\n long long d = a;\n if (b != 0) {\n d = extgcd(b, a % b, y, x);\n y -= (a / b) * x;\n } else {\n x = 1; y = 0;\n }\n return d;\n}\nlong long mod_inverse(long long a, long long m) {\n long long x, y;\n if(extgcd(a, m, x, y) == 1) return (m + x % m) % m;\n else return -1;\n}\nvector<long long> fact(MAX_N+1, inf);\nlong long mod_fact(long long n, long long& e) {\n if(fact[0] == inf) {\n fact[0]=1;\n if(MAX_N != 0) fact[1]=1;\n for(ll i = 2; i <= MAX_N; ++i) {\n fact[i] = (fact[i-1] * i) % mod;\n }\n }\n e = 0;\n if(n == 0) return 1;\n long long res = mod_fact(n / mod, e);\n e += n / mod;\n if((n / mod) % 2 != 0) return (res * (mod - fact[n % mod])) % mod;\n return (res * fact[n % mod]) % mod;\n}\n// return nCk\nlong long mod_comb(long long n, long long k) {\n if(n < 0 || k < 0 || n < k) return 0;\n long long e1, e2, e3;\n long long a1 = mod_fact(n, e1), a2 = mod_fact(k, e2), a3 = mod_fact(n - k, e3);\n if(e1 > e2 + e3) return 0;\n return (a1 * mod_inverse((a2 * a3) % mod, mod)) % mod;\n}\n \nusing mi = modint;\n \nmi mod_pow(mi a, ll n) {\n mi ret = 1;\n mi tmp = a;\n while(n > 0) {\n if(n % 2) ret *= tmp;\n tmp = tmp * tmp;\n n /= 2;\n }\n return ret;\n}\n \nll gcd(ll a, ll b) {\n if (b == 0) return a;\n return gcd(b, a % b);\n}\n\nll ans1 = 0;\nmi ans2 = 1;\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n int n, m;\n cin >> n >> m;\n vector<set<int>> g(n);\n REP(i, m) {\n int x, y;\n cin >> x >> y;\n x--; y--;\n g[x].insert(y);\n g[y].insert(x);\n }\n vector<int> deg(n, 0);\n REP(i, n) deg[i] = (int)g[i].size();\n vector<pair<int, int>> v(n);\n REP(i, n) v[i] = make_pair(deg[i], i);\n sort(ALL(v)); reverse(ALL(v));\n vector<bool> used(n, false);\n for(auto elm: v) {\n if(used[elm.second]) continue;\n bool k = true;\n for(auto adj: g[elm.second]) {\n if(deg[adj] != deg[elm.second]) k = false;\n }\n if(k) {\n ans1++;\n ans2 *= deg[elm.second] + 1;\n used[elm.second] = true;\n for(auto adj: g[elm.second]) used[adj] = true;\n } else {\n for(auto adj: g[elm.second]) {\n deg[adj]--;\n g[adj].erase(elm.second);\n }\n }\n }\n cout << ans1 << endl;\n cout << ans2 << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 220, "memory_kb": 35608, "score_of_the_acc": -0.8977, "final_rank": 11 }, { "submission_id": "aoj_2291_4556216", "code_snippet": "#include <bits/stdc++.h>\n#define ll long long\n#define INF 1000000005\n#define MOD 1000000009\n#define EPS 1e-10\n#define rep(i,n) for(int i=0;i<(int)(n);++i)\n#define rrep(i,n) for(int i=(int)(n)-1;i>=0;--i)\n#define srep(i,s,t) for(int i=(int)(s);i<(int)(t);++i)\n#define each(a,b) for(const auto& (a): (b))\n#define all(v) (v).begin(),(v).end()\n#define len(v) (int)(v).size()\n#define zip(v) sort(all(v)),v.erase(unique(all(v)),v.end())\n#define cmx(x,y) x=max(x,y)\n#define cmn(x,y) x=min(x,y)\n#define fi first\n#define se second\n#define pb push_back\n#define show(x) cout<<#x<<\" = \"<<(x)<<endl\n#define sar(a,n) {cout<<#a<<\":\";rep(pachico,n)cout<<\" \"<<a[pachico];cout<<endl;}\n\nusing namespace std;\n\ntemplate<typename S,typename T>auto&operator<<(ostream&o,pair<S,T>p){return o<<\"{\"<<p.fi<<\",\"<<p.se<<\"}\";}\ntemplate<typename T>auto&operator<<(ostream&o,set<T>s){for(auto&e:s)o<<e<<\" \";return o;}\ntemplate<typename S,typename T,typename U>\nauto&operator<<(ostream&o,priority_queue<S,T,U>q){while(!q.empty())o<<q.top()<<\" \",q.pop();return o;}\ntemplate<typename K,typename T>auto&operator<<(ostream&o,map<K,T>&m){for(auto&e:m)o<<e<<\" \";return o;}\ntemplate<typename T>auto&operator<<(ostream&o,vector<T>v){for(auto&e:v)o<<e<<\" \";return o;}\nvoid ashow(){cout<<endl;}template<typename T,typename...A>void ashow(T t,A...a){cout<<t<<\" \";ashow(a...);}\ntemplate<typename S,typename T,typename U>\nstruct TRI{S fi;T se;U th;TRI(){}TRI(S f,T s,U t):fi(f),se(s),th(t){}\nbool operator<(const TRI&_)const{return(fi==_.fi)?((se==_.se)?(th<_.th):(se<_.se)):(fi<_.fi);}};\ntemplate<typename S,typename T,typename U>\nauto&operator<<(ostream&o,TRI<S,T,U>&t){return o<<\"{\"<<t.fi<<\",\"<<t.se<<\",\"<<t.th<<\"}\";}\n\ntypedef pair<int, int> P;\ntypedef pair<ll,ll> pll;\ntypedef vector<int> vi;\ntypedef vector<vi> vvi;\ntypedef vector<ll> vl;\ntypedef vector<vl> vvl;\ntypedef vector<double> vd;\ntypedef vector<P> vp;\ntypedef vector<string> vs;\n\nconst int MAX_N = 100005;\n\nint visit[MAX_N];\n\nvoid dfs(int u, int& deg, int& ver, int& cnt, vector<int>& vers, const int id, const vector<unordered_set<int> >& G){\n if(deg < (int)G[u].size()){\n deg = (int)G[u].size(), ver = u, cnt = 1;\n }else if(deg == (int)G[u].size()){\n ++cnt;\n }\n visit[u] = id, vers.push_back(u);\n for(const int v : G[u]){\n if(visit[v] != id) dfs(v, deg, ver, cnt, vers, id, G);\n }\n}\n\nint main(){\n cin.tie(0);\n ios::sync_with_stdio(false);\n int n, m;\n cin >> n >> m;\n vector<unordered_set<int> > G(n);\n unordered_set<int> st;\n rep(i,n) st.insert(i);\n int x, y;\n rep(i,m){\n cin >> x >> y;\n --x, --y;\n G[x].insert(y), G[y].insert(x);\n }\n int id = 1, hoge = 0, ans = 1;\n while(!st.empty()){\n const int v = *st.begin();\n // show(v);\n vector<int> vers;\n int deg = -1, ver = -1, cnt = 0;\n dfs(v, deg, ver, cnt, vers, id++, G);\n // show(vers), show(cnt);\n if(cnt == (int)vers.size()){\n ++hoge;\n ans = (ll)ans * cnt % MOD;\n if(vers.size() == st.size()) break;\n for(const int u : vers) st.erase(u);\n }else{\n for(const int u : G[ver]) G[u].erase(ver);\n st.erase(ver);\n }\n }\n cout << hoge << \"\\n\";\n cout << ans << \"\\n\";\n return 0;\n}", "accuracy": 1, "time_ms": 1830, "memory_kb": 30132, "score_of_the_acc": -1.6503, "final_rank": 16 }, { "submission_id": "aoj_2291_3842896", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 99999999999999999\n//#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n#define MOD 1000000009\n#define SIZE 100005\n\nstruct Edge{\n\tvoid set(int arg_from,int arg_to){\n\t\tfrom = arg_from;\n\t\tto = arg_to;\n\t}\n\tint from,to;\n};\n\nint N,M;\nll degree[SIZE],size[SIZE];\nbool DELETE[SIZE];\nEdge edge[200005];\n\nint main(){\n\n\tscanf(\"%d %d\",&N,&M);\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tdegree[i] = 0;\n\t\tsize[i] = 1;\n\t\tDELETE[i] = false;\n\t}\n\n\tint from,to;\n\n\tfor(int loop = 0; loop < M; loop++){\n\n\t\tscanf(\"%d %d\",&from,&to);\n\t\tfrom--;\n\t\tto--;\n\n\t\tedge[loop].set(from,to);\n\n\t\tdegree[from]++;\n\t\tdegree[to]++;\n\t}\n\n\tfor(int i = 0; i < M; i++){\n\n\t\tfrom = edge[i].from;\n\t\tto = edge[i].to;\n\n\t\tif((degree[from] < degree[to]) || (degree[from] == degree[to] && from < to)){\n\n\t\t\tDELETE[to] = true;\n\t\t}else{\n\n\t\t\tDELETE[from] = true;\n\t\t}\n\n\t\tif(degree[from] == degree[to]){\n\n\t\t\tsize[from]++;\n\t\t\tsize[to]++;\n\t\t}\n\t}\n\n\tll ans_sum = 0,ans_num = 1;\n\n\tfor(int i = 0; i < N; i++){\n\t\tif(DELETE[i])continue;\n\n\t\tans_sum++;\n\t\tans_num *= size[i];\n\t\tans_num %= MOD;\n\t}\n\n\tprintf(\"%lld\\n\",ans_sum);\n\tprintf(\"%lld\\n\",ans_num);\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 6440, "score_of_the_acc": -0.0578, "final_rank": 1 }, { "submission_id": "aoj_2291_3137224", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nclass UnionFind {\nprivate:\n\tvector< int > data;\n\t\npublic:\n\tUnionFind(int n) : data(n, -1) {}\n\t\n\tvoid unionSet(int x, int y) {\n\t\tx = root(x); y = root(y);\n\t\tif (data[y] < data[x]) swap(x, y);\n\t\tif (x != y) { data[x] += data[y]; data[y] = x; }\n\t}\n\t\n\tbool sameSet(int x, int y) { return root(x) == root(y); }\n\tint root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }\n\tint size(int x) { return -data[root(x)]; }\n};\n\nint N, M;\nvector< vector< int > > aList;\n\nvector< int > getDegOrder() {\n\tvector< int > deg(N, 0);\n\tfor (int v=0; v<N; ++v) deg[v] = (int)aList[v].size();\n\t\n\tusing pii = pair< int, int >;\n\tset< pii > que;\n\tfor (int v=0; v<N; ++v) que.insert(pii(-deg[v], v));\n\t\n\tvector< int > ret;\n\twhile (!que.empty()) {\n\t\tint v = que.begin()->second; que.erase(que.begin());\t\n\t\tif (deg[v] < 0) continue;\n\t\tret.push_back(v); deg[v] = -1;\n\t\tfor (int u : aList[v]) {\n\t\t\tif (deg[u] < 0) continue;\n\t\t\tque.erase(pii(-deg[u], u));\t--deg[u];\n\t\t\tque.insert(pii(-deg[u], u));\n\t\t}\n\t}\n\treturn ret;\n}\n\nvector< int > constructCliques(const vector< int >& vertex_order) {\n\tvector< int > mark(N, 0);\n\t\n\tUnionFind uf(N);\n\tfor (int v : vertex_order) {\n\t\tmark[v] = 1;\n\t\tint g = -1;\n\t\tfor (int u : aList[v]) {\n\t\t\tif (mark[u] == 0) continue;\n\t\t\tint r = uf.root(u);\n\t\t\tif (g != r) g = (g == -1 ? r : -2);\n\t\t}\n\t\tif (g >= 0) uf.unionSet(g, v);\n\t\tif (g == -2) mark[v] = -1;\n\t}\n\t\n\tvector< int > ret;\n\tfor (int v=0; v<N; ++v) {\n\t\tif (mark[uf.root(v)] == -1) continue;\n\t\tmark[uf.root(v)] = -1;\n\t\tret.push_back(uf.size(v));\n\t}\n\treturn ret;\n}\n\nint main() {\n\tcin >> N >> M;\n\taList.assign(N, vector< int >());\n\tfor (int i=0; i<M; ++i) {\n\t\tint u, v; cin >> u >> v; --u; --v;\n\t\taList[u].push_back(v);\n\t\taList[v].push_back(u);\n\t}\n\t\n\tvector< int > deg_order = getDegOrder();\n\treverse(deg_order.begin(), deg_order.end());\n\tvector< int > cliqueSize = constructCliques(deg_order);\n\t\n\tusing lint = long long int;\n\tlint ans = 1, MOD = (lint)1e9 + 9;\n\tfor (int x : cliqueSize) ans = (ans * x) % MOD;\n\tcout << cliqueSize.size() << endl << ans << endl;\n}", "accuracy": 1, "time_ms": 140, "memory_kb": 14084, "score_of_the_acc": -0.3153, "final_rank": 5 }, { "submission_id": "aoj_2291_2362969", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll=long long;\n#define int ll\n#define FOR(i,a,b) for(int i=int(a);i<int(b);i++)\n#define REP(i,b) FOR(i,0,b)\nint read(){\n\tint i;\n\tscanf(\"%lld\",&i);\n\treturn i;\n}\nusing vi=vector<int>;\n#define PB push_back\n#define ALL(x) x.begin(),x.end()\nusing pi=pair<int,int>;\n\nstruct UnionFind{\n\tvi rank,par;\n\tvoid Init(int n){\n\t\trank.assign(n,0);\n\t\tpar.assign(n,-1);\n\t}\n\tint Find(int a){\n\t\treturn par[a]==-1?a:(par[a]=Find(par[a]));\n\t}\n\tbool Same(int a,int b){\n\t\treturn Find(a)==Find(b);\n\t}\n\tvoid Unite(int a,int b){\n\t\ta=Find(a),b=Find(b);\n\t\tif(a==b)return;\n\t\tif(rank[a]<rank[b])swap(a,b);\n\t\tpar[b]=a;\n\t\tif(rank[a]==rank[b])rank[a]++;\n\t}\n};\n\nconst int mod=1000000009;\n\npi Merge(pi a,pi b){\n\treturn pi(a.first+b.first,a.second*b.second%mod);\n}\n\nsigned main(){\n\tint n=read(),m=read();\n\tvector<vi> g(n);\n\tvi deg(n,0);\n\tREP(_,m){\n\t\tint a=read()-1,b=read()-1;\n\t\tg[a].PB(b);\n\t\tg[b].PB(a);\n\t\tdeg[a]++;\n\t\tdeg[b]++;\n\t}\n\tpriority_queue<pi> pq;\n\tREP(i,n)pq.push(pi(deg[i],i));\n\tvi used(n,0),ord;\n\twhile(!pq.empty()){\n\t\tint v=pq.top().first,i=pq.top().second;\n\t\tpq.pop();\n\t\tif(deg[i]!=v)continue;\n\t\tord.PB(i);\n\t\tfor(auto j:g[i])if(!used[j])\n\t\t\tpq.push(pi(--deg[j],j));\n\t\tused[i]=1;\n\t}\n\treverse(ALL(ord));\n\tvector<pi> ans(n);\n\tUnionFind uf;uf.Init(n);\n\tused.assign(n,0);\n\tfor(auto i:ord){\n\t\tpi w(0,1);\n\t\tfor(auto j:g[i])if(used[j])\n\t\t\tif(!uf.Same(i,j)){\n\t\t\t\tw=Merge(w,ans[uf.Find(j)]);\n\t\t\t\tuf.Unite(i,j);\n\t\t\t}\n\t\tif(w.first==0)\n\t\t\tw.first=1;\n\t\telse if(w.first==1)\n\t\t\tw.second=(w.second+1)%mod;\n\t\tans[uf.Find(i)]=w;\n\t\tused[i]=1;\n\t}\n\tpi w=ans[uf.Find(0)];\n\tREP(i,n)if(!uf.Same(0,i)){\n\t\tw=Merge(w,ans[uf.Find(i)]);\n\t\tuf.Unite(0,i);\n\t}\n\tcout<<w.first<<endl<<w.second<<endl;\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 19440, "score_of_the_acc": -0.4161, "final_rank": 6 }, { "submission_id": "aoj_2291_2039842", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\n#define int long long\ntypedef vector<int>vint;\ntypedef pair<int,int>pint;\ntypedef vector<pint>vpint;\n#define rep(i,n) for(int i=0;i<(n);i++)\n#define reps(i,f,n) for(int i=(f);i<(n);i++)\n#define all(v) (v).begin(),(v).end()\n#define each(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)\n#define pb push_back\n#define fi first\n#define se second\ntemplate<typename A,typename B>inline void chmin(A &a,B b){if(a>b)a=b;}\ntemplate<typename A,typename B>inline void chmax(A &a,B b){if(a<b)a=b;}\n\nconst int mod=1000000009;\ntypedef pair<vint,vpint>graph;\npint op(const pint &a,const pint &b){\n return {a.fi+b.fi,a.se*b.se%mod};\n}\n\nvint G[100000];\nint deg[100000];\nint mark[100000];\nint sizev[100000];\nint sizee[200000];\npint ans;\nint que[100000],tail;\n\nint vs_[100000];pint es_[200000];\n\nvoid solve2(int N,int *vs,int M,pint *es);\n\nvoid solve(int N,int *vs,int M,pint *es){\n rep(i,N)deg[vs[i]]=0,mark[vs[i]]=-1;\n\n for(int i=0;i<M;i++){\n G[es[i].fi][deg[es[i].fi]++]=es[i].se;\n G[es[i].se][deg[es[i].se]++]=es[i].fi;\n }\n\n vector<int>sizev,sizee;\n\n int cnt=0;\n for(int i=0;i<N;i++){\n if(mark[vs[i]]!=-1)continue;\n sizev.pb(0);sizee.pb(0);\n int c=cnt++;\n sizev[c]++;\n mark[vs[i]]=c;\n tail=1;\n que[0]=vs[i];\n for(int j=0;j<tail;j++){\n int v=que[j];\n for(int k=0;k<deg[v];k++){\n int u=G[v][k];\n if(mark[u]!=-1)continue;\n mark[u]=c;\n sizev[c]++;\n que[tail++]=u;\n }\n }\n }\n\n for(int i=0;i<M;i++)sizee[mark[es[i].fi]]++;\n rep(i,N)vs_[i]=vs[i];\n rep(i,M)es_[i]=es[i];\n\n int c=0;\n for(int i=0;i<cnt;i++){\n int nc=sizev[i]+c;\n sizev[i]=c;\n c=nc;\n }\n\n c=0;\n for(int i=0;i<cnt;i++){\n int nc=sizee[i]+c;\n sizee[i]=c;\n c=nc;\n }\n\n rep(i,N){\n int tmp=sizev[mark[vs_[i]]]++;\n vs[tmp]=vs_[i];\n }\n\n rep(i,M){\n int tmp=sizee[mark[es_[i].fi]]++;\n es[tmp]=es_[i];\n }\n\n rep(i,cnt){\n int t=i?sizev[i-1]:0;\n int s=i?sizee[i-1]:0;\n solve2(sizev[i]-t,vs+t,sizee[i]-s,es+s);\n }\n}\n\nvoid solve2(int N,int *vs,int M,pint *es){\n if(N*(N-1)/2==M){\n ans=op(ans,{1,N});\n return;\n }\n\n rep(i,N)deg[vs[i]]=0;\n\n for(int i=0;i<M;i++)deg[es[i].fi]++,deg[es[i].se]++;\n int cent=-1;\n rep(i,N){\n if(deg[vs[i]]==N-1)cent=vs[i];\n deg[vs[i]]=0;\n }\n assert(cent!=-1);\n\n int t=0;\n rep(i,N){\n if(vs[i]==cent)continue;\n vs[t++]=vs[i];\n }\n\n int s=0;\n rep(i,M){\n if(es[i].fi==cent||es[i].se==cent)continue;\n es[s++]=es[i];\n }\n solve(t,vs,s,es);\n}\n\nsigned main(){\n int N,M;\n scanf(\"%lld%lld\",&N,&M);\n\n\n static int vs[100000];\n static pint es[200000];\n rep(i,N)vs[i]=i;\n rep(i,M){\n int a,b;\n scanf(\"%lld%lld\",&a,&b);\n a--;b--;\n es[i]={a,b};\n G[a].pb(b);G[b].pb(a);\n }\n\n memset(mark,-1,sizeof(mark));\n ans={0,1};\n solve(N,vs,M,es);\n printf(\"%lld\\n%lld\\n\",ans.fi,ans.se);\n return 0;\n}", "accuracy": 1, "time_ms": 490, "memory_kb": 21960, "score_of_the_acc": -0.7056, "final_rank": 9 }, { "submission_id": "aoj_2291_2039805", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\n#define int long long\ntypedef vector<int>vint;\ntypedef pair<int,int>pint;\ntypedef vector<pint>vpint;\n#define rep(i,n) for(int i=0;i<(n);i++)\n#define reps(i,f,n) for(int i=(f);i<(n);i++)\n#define all(v) (v).begin(),(v).end()\n#define each(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)\n#define pb push_back\n#define fi first\n#define se second\ntemplate<typename A,typename B>inline void chmin(A &a,B b){if(a>b)a=b;}\ntemplate<typename A,typename B>inline void chmax(A &a,B b){if(a<b)a=b;}\n\nconst int mod=1000000009;\n\npint op(const pint &a,const pint &b){\n return {a.fi+b.fi,a.se*b.se%mod};\n}\n\nvint G[100000];\nint deg[100000];\nint mark[100000];\nint sizev[100000];\nint sizee[200000];\npint ans;\nint que[100000],tail;\n\nint vs_[100000];pint es_[200000];\n\nvoid solve2(int N,int *vs,int M,pint *es);\n\nvoid solve(int N,int *vs,int M,pint *es){\n rep(i,N)deg[vs[i]]=0,mark[vs[i]]=-1;\n\n for(int i=0;i<M;i++){\n G[es[i].fi][deg[es[i].fi]++]=es[i].se;\n G[es[i].se][deg[es[i].se]++]=es[i].fi;\n }\n\n int cnt=0;\n for(int i=0;i<N;i++){\n if(mark[vs[i]]!=-1)continue;\n sizev[cnt]=sizee[cnt]=0;\n int c=cnt++;\n sizev[c]++;\n mark[vs[i]]=c;\n tail=1;\n que[0]=vs[i];\n for(int j=0;j<tail;j++){\n int v=que[j];\n for(int k=0;k<deg[v];k++){\n int u=G[v][k];\n if(mark[u]!=-1)continue;\n mark[u]=c;\n sizev[c]++;\n que[tail++]=u;\n }\n }\n }\n\n for(int i=0;i<M;i++)sizee[mark[es[i].fi]]++;\n rep(i,N)vs_[i]=vs[i];\n rep(i,M)es_[i]=es[i];\n\n int c=0;\n for(int i=0;i<cnt;i++){\n int nc=sizev[i]+c;\n sizev[i]=c;\n c=nc;\n }\n\n c=0;\n for(int i=0;i<cnt;i++){\n int nc=sizee[i]+c;\n sizee[i]=c;\n c=nc;\n }\n\n rep(i,N){\n int tmp=sizev[mark[vs_[i]]]++;\n vs[tmp]=vs_[i];\n }\n\n rep(i,M){\n int tmp=sizee[mark[es_[i].fi]]++;\n es[tmp]=es_[i];\n }\n\n rep(i,cnt){\n int t=i?sizev[i-1]:0;\n int s=i?sizee[i-1]:0;\n solve2(sizev[i]-t,vs+t,sizee[i]-s,es+s);\n }\n}\n\nvoid solve2(int N,int *vs,int M,pint *es){\n if(N*(N-1)/2==M){\n ans=op(ans,{1,N});\n return;\n }\n\n rep(i,N)deg[vs[i]]=0;\n\n for(int i=0;i<M;i++)deg[es[i].fi]++,deg[es[i].se]++;\n int cent=-1;\n rep(i,N){\n if(deg[vs[i]]==N-1)cent=vs[i];\n }\n\n int t=0;\n rep(i,N){\n if(vs[i]==cent)continue;\n vs[t++]=vs[i];\n }\n\n int s=0;\n rep(i,M){\n if(es[i].fi==cent||es[i].se==cent)continue;\n es[s++]=es[i];\n }\n solve(t,vs,s,es);\n}\n\nsigned main(){\n int N,M;\n scanf(\"%lld%lld\",&N,&M);\n\n\n static int vs[100000];\n static pint es[200000];\n rep(i,N)vs[i]=i;\n rep(i,M){\n int a,b;\n scanf(\"%lld%lld\",&a,&b);\n a--;b--;\n es[i]={a,b};\n G[a].pb(b);G[b].pb(a);\n }\n ans={0,1};\n solve(N,vs,M,es);\n printf(\"%lld\\n%lld\\n\",ans.fi,ans.se);\n return 0;\n}", "accuracy": 0.175, "time_ms": 260, "memory_kb": 23380, "score_of_the_acc": -0.614, "final_rank": 20 }, { "submission_id": "aoj_2291_1474416", "code_snippet": "#include <iostream>\n#include <vector>\nusing namespace std;\n\ntypedef long long ll;\nll _MOD = 1000000009;\n\nstruct union_find {\n\tvector<int> v;\n\tunion_find(int n) : v(n, -1) {}\n\tint find(int x) { return (v[x] < 0) ? x : (v[x] = find(v[x])); }\n\tvoid unite(int x, int y) {\n\t\tx = find(x); y = find(y);\n\t\tif (x != y) {\n\t\t\tif (-v[x] < -v[y]) swap(x, y);\n\t\t\tv[x] += v[y]; v[y] = x;\n\t\t}\n\t}\n\tbool same(int x, int y) { return find(x) == find(y); }\n\tint size(int x) { return -v[find(x)]; }\n};\n\nint main() {\n\tint n, m; cin >> n >> m;\n\tvector<int> a(m), b(m);\n\tfor (int i = 0; i < m; i++) {\n\t\tscanf(\"%d%d\", &a[i], &b[i]);\n\t\ta[i]--;\n\t\tb[i]--;\n\t}\n\tvector<bool> rm(n);\n\tfor (;;) {\n\t\tvector<int> d(n);\n\t\tunion_find uf(n);\n\t\tfor (int i = 0; i < m; i++) {\n\t\t\td[a[i]]++;\n\t\t\td[b[i]]++;\n\t\t\tuf.unite(a[i], b[i]);\n\t\t}\n\t\tvector<vector<int> > v(n);\n\t\tfor (int u = 0; u < n; u++) {\n\t\t\tif (rm[u]) continue;\n\t\t\tint p = uf.find(u);\n\t\t\tif (d[u] == uf.size(p) - 1)\n\t\t\t\tv[p].push_back(u);\n\t\t}\n\t\tbool updated = false;\n\t\tfor (int p = 0; p < n; p++) {\n\t\t\tif (rm[p]) continue;\n\t\t\tif (uf.find(p) != p) continue;\n\t\t\tif (v[p].size() == uf.size(p)) continue;\n\t\t\tupdated = true;\n\t\t\tfor (int j = 0; j < v[p].size(); j++)\n\t\t\t\trm[v[p][j]] = true;\n\t\t}\n\t\tif (!updated) {\n\t\t\tint x = 0, y = 1;\n\t\t\tfor (int p = 0; p < n; p++) {\n\t\t\t\tif (rm[p]) continue;\n\t\t\t\tif (uf.find(p) != p) continue;\n\t\t\t\tx++;\n\t\t\t\ty = (ll)y * uf.size(p) % _MOD;\n\t\t\t}\n\t\t\tcout << x << endl << y << endl;\n\t\t\treturn 0;\n\t\t}\n\t\tvector<int> _a, _b;\n\t\tfor (int i = 0; i < m; i++)\n\t\t\tif (!rm[a[i]] && !rm[b[i]]) {\n\t\t\t\t_a.push_back(a[i]);\n\t\t\t\t_b.push_back(b[i]);\n\t\t\t}\n\t\tm = _a.size();\n\t\ta = _a;\n\t\tb = _b;\n\t}\n}", "accuracy": 1, "time_ms": 1580, "memory_kb": 11892, "score_of_the_acc": -1.056, "final_rank": 12 }, { "submission_id": "aoj_2291_1474414", "code_snippet": "#define _USE_MATH_DEFINES\n#include <algorithm>\n#include <cstdio>\n#include <fstream>\n#include <functional>\n#include <iostream>\n#include <cfloat>\n#include <climits>\n#include <cstdlib>\n#include <cstring>\n#include <cmath>\n#include <map>\n#include <queue>\n#include <random>\n#include <set>\n#include <sstream>\n#include <stack>\n#include <string>\n#include <time.h>\n#include <vector>\nusing namespace std;\n\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef pair<int, int> i_i;\ntypedef pair<ll, int> ll_i;\ntypedef pair<double, int> d_i;\ntypedef pair<ll, ll> ll_ll;\ntypedef pair<double, double> d_d;\nstruct edge { int v, w; };\n\nll MOD = 1000000007;\nll _MOD = 1000000009;\ndouble EPS = 1e-10;\n\nstruct union_find {\n\tvector<int> v;\n\tunion_find(int n) : v(n, -1) {}\n\tint find(int x) { return (v[x] < 0) ? x : (v[x] = find(v[x])); }\n\tvoid unite(int x, int y) {\n\t\tx = find(x); y = find(y);\n\t\tif (x != y) {\n\t\t\tif (-v[x] < -v[y]) swap(x, y);\n\t\t\tv[x] += v[y]; v[y] = x;\n\t\t}\n\t}\n\tbool same(int x, int y) { return find(x) == find(y); }\n\tint size(int x) { return -v[find(x)]; }\n};\n\nint main() {\n\tint n, m; cin >> n >> m;\n\tvector<int> a(m), b(m);\n\tfor (int i = 0; i < m; i++) {\n\t\tscanf(\"%d%d\", &a[i], &b[i]);\n\t\ta[i]--;\n\t\tb[i]--;\n\t}\n\tvector<bool> rm(n);\n\tfor (;;) {\n\t\tvector<int> d(n);\n\t\tunion_find uf(n);\n\t\tfor (int i = 0; i < m; i++) {\n\t\t\td[a[i]]++;\n\t\t\td[b[i]]++;\n\t\t\tuf.unite(a[i], b[i]);\n\t\t}\n\t\tvector<vector<int> > v(n);\n\t\tfor (int u = 0; u < n; u++) {\n\t\t\tif (rm[u]) continue;\n\t\t\tint p = uf.find(u);\n\t\t\tif (d[u] == uf.size(p) - 1)\n\t\t\t\tv[p].push_back(u);\n\t\t}\n\t\tbool updated = false;\n\t\tfor (int p = 0; p < n; p++) {\n\t\t\tif (rm[p]) continue;\n\t\t\tif (uf.find(p) != p) continue;\n\t\t\tif (v[p].size() == uf.size(p)) continue;\n\t\t\tupdated = true;\n\t\t\tfor (int j = 0; j < v[p].size(); j++)\n\t\t\t\trm[v[p][j]] = true;\n\t\t}\n\t\tif (!updated) {\n\t\t\tint x = 0, y = 1;\n\t\t\tfor (int p = 0; p < n; p++) {\n\t\t\t\tif (rm[p]) continue;\n\t\t\t\tif (uf.find(p) != p) continue;\n\t\t\t\tx++;\n\t\t\t\ty = (ll)y * uf.size(p) % _MOD;\n\t\t\t}\n\t\t\tcout << x << endl << y << endl;\n\t\t\treturn 0;\n\t\t}\n\t\tvector<int> _a, _b;\n\t\tfor (int i = 0; i < m; i++)\n\t\t\tif (!rm[a[i]] && !rm[b[i]]) {\n\t\t\t\t_a.push_back(a[i]);\n\t\t\t\t_b.push_back(b[i]);\n\t\t\t}\n\t\tm = _a.size();\n\t\ta = _a;\n\t\tb = _b;\n\t}\n}", "accuracy": 1, "time_ms": 1590, "memory_kb": 11892, "score_of_the_acc": -1.0616, "final_rank": 13 }, { "submission_id": "aoj_2291_1474357", "code_snippet": "#define _USE_MATH_DEFINES\n#include <algorithm>\n#include <cstdio>\n#include <fstream>\n#include <functional>\n#include <iostream>\n#include <cfloat>\n#include <climits>\n#include <cstdlib>\n#include <cstring>\n#include <cmath>\n#include <map>\n#include <queue>\n#include <random>\n#include <set>\n#include <sstream>\n#include <stack>\n#include <string>\n#include <time.h>\n#include <vector>\nusing namespace std;\n\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef pair<int, int> i_i;\ntypedef pair<ll, int> ll_i;\ntypedef pair<double, int> d_i;\ntypedef pair<ll, ll> ll_ll;\ntypedef pair<double, double> d_d;\nstruct edge { int v, w; };\n\nll MOD = 1000000007;\nll _MOD = 1000000009;\ndouble EPS = 1e-10;\n\nstruct union_find {\n\tvector<int> v;\n\tunion_find(int n) : v(n, -1) {}\n\tint find(int x) { return (v[x] < 0) ? x : (v[x] = find(v[x])); }\n\tvoid unite(int x, int y) {\n\t\tx = find(x); y = find(y);\n\t\tif (x != y) {\n\t\t\tif (-v[x] < -v[y]) swap(x, y);\n\t\t\tv[x] += v[y]; v[y] = x;\n\t\t}\n\t}\n\tbool same(int x, int y) { return find(x) == find(y); }\n\tint size(int x) { return -v[find(x)]; }\n};\n\ni_i g(vector<int> a, vector<int> b);\n\ni_i f(int n, vector<int> a, vector<int> b) {\n\tint m = a.size();\n\tunion_find uf(n);\n\tfor (int i = 0; i < m; i++)\n\t\tuf.unite(a[i], b[i]);\n\tvector<vector<int> > as(n), bs(n);\n\tfor (int i = 0; i < m; i++) {\n\t\tint u = uf.find(a[i]);\n\t\tas[u].push_back(a[i]);\n\t\tbs[u].push_back(b[i]);\n\t}\n\ti_i ans(0, 1);\n\tfor (int u = 0; u < n; u++) {\n\t\tif (uf.find(u) != u) continue;\n\t\ti_i p = g(as[u], bs[u]);\n\t\tans.first += p.first;\n\t\tans.second = (ll)ans.second * p.second % MOD;\n\t}\n\treturn ans;\n}\n\ni_i g(vector<int> a, vector<int> b) {\n\tint m = a.size();\n\tvector<int> v;\n\tfor (int i = 0; i < m; i++) {\n\t\tv.push_back(a[i]);\n\t\tv.push_back(b[i]);\n\t}\n\tsort(v.begin(), v.end());\n\tv.erase(unique(v.begin(), v.end()), v.end());\n\tint n = v.size();\n\tif (!n) n = 1;\n\tfor (int i = 0; i < m; i++) {\n\t\ta[i] = lower_bound(v.begin(), v.end(), a[i]) - v.begin();\n\t\tb[i] = lower_bound(v.begin(), v.end(), b[i]) - v.begin();\n\t}\n\tif (m == (ll)n * (n - 1) / 2)\n\t\treturn i_i(1, n);\n\tvector<int> d(n);\n\tfor (int i = 0; i < m; i++) {\n\t\td[a[i]]++;\n\t\td[b[i]]++;\n\t}\n\tint _u;\n\tfor (int u = 0; u < n; u++)\n\t\tif (d[u] == n - 1)\n\t\t\t_u = u;\n\tvector<int> _a, _b;\n\tfor (int i = 0; i < m; i++)\n\t\tif (a[i] != _u && b[i] != _u) {\n\t\t\t_a.push_back(a[i] < _u ? a[i] : a[i] - 1);\n\t\t\t_b.push_back(b[i] < _u ? b[i] : b[i] - 1);\n\t\t}\n\treturn f(n - 1, _a, _b);\n}\n\nint main() {\n\tint n, m; cin >> n >> m;\n\tvector<int> a(m), b(m);\n\tfor (int i = 0; i < m; i++) {\n\t\tscanf(\"%d%d\", &a[i], &b[i]);\n\t\ta[i]--;\n\t\tb[i]--;\n\t}\n\ti_i p = f(n, a, b);\n\tcout << p.first << endl;\n\tcout << p.second << endl;\n}", "accuracy": 0.175, "time_ms": 80, "memory_kb": 21792, "score_of_the_acc": -0.4749, "final_rank": 19 }, { "submission_id": "aoj_2291_1445952", "code_snippet": "#include <cstdio>\n#include <cmath>\n#include <cstring>\n#include <cstdlib>\n#include <climits>\n#include <ctime>\n#include <queue>\n#include <stack>\n#include <algorithm>\n#include <list>\n#include <vector>\n#include <set>\n#include <map>\n#include <iostream>\n#include <deque>\n#include <complex>\n#include <string>\n#include <iomanip>\n#include <sstream>\n#include <bitset>\n#include <valarray>\n#include <unordered_map>\n#include <iterator>\n#include <functional>\n#include <cassert>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned int uint;\ntypedef unsigned char uchar;\ntypedef unsigned long long ull;\ntypedef pair<int, int> pii;\ntypedef pair<ll, ll> pll;\ntypedef vector<int> vi;\n\n#define REP(i,x) for(int i=0;i<(int)(x);i++)\n#define REPS(i,x) for(int i=1;i<=(int)(x);i++)\n#define RREP(i,x) for(int i=((int)(x)-1);i>=0;i--)\n#define RREPS(i,x) for(int i=((int)(x));i>0;i--)\n#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();i++)\n#define RFOR(i,c) for(__typeof((c).rbegin())i=(c).rbegin();i!=(c).rend();i++)\n#define ALL(container) (container).begin(), (container).end()\n#define RALL(container) (container).rbegin(), (container).rend()\n#define SZ(container) ((int)container.size())\n#define mp(a,b) make_pair(a, b)\n#define pb push_back\n#define eb emplace_back\n#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );\n\ntemplate<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }\ntemplate<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }\ntemplate<class T> ostream& operator<<(ostream &os, const vector<T> &t) {\nos<<\"[\"; FOR(it,t) {if(it!=t.begin()) os<<\",\"; os<<*it;} os<<\"]\"; return os;\n}\ntemplate<class T> ostream& operator<<(ostream &os, const set<T> &t) {\nos<<\"{\"; FOR(it,t) {if(it!=t.begin()) os<<\",\"; os<<*it;} os<<\"}\"; return os;\n}\ntemplate<class S, class T> ostream& operator<<(ostream &os, const pair<S,T> &t) { return os<<\"(\"<<t.first<<\",\"<<t.second<<\")\";}\ntemplate<class S, class T> pair<S,T> operator+(const pair<S,T> &s, const pair<S,T> &t){ return pair<S,T>(s.first+t.first, s.second+t.second);}\ntemplate<class S, class T> pair<S,T> operator-(const pair<S,T> &s, const pair<S,T> &t){ return pair<S,T>(s.first-t.first, s.second-t.second);}\n\nconst int INF = 1<<28;\nconst double EPS = 1e-8;\nconst int MOD = 1000000009;\n\n\nint T, n, m;\n\nint main(int argc, char *argv[]){\n\tios::sync_with_stdio(false);\n\tcin >> n >> m;\n\tvector<set<int>> g(n);\n\tvi deg(n);\n\tREP(i, m){\n\t\tint u, v;\n\t\tcin >> u >> v; u--; v--;\n\t\tg[u].insert(v); g[v].insert(u);\n\t}\n\tset<pii> s;\n\tREP(i, n) s.insert(pii(g[i].size(), i));\n\tint c = 0;\n\tll ans = 1;\n\twhile(!s.empty()){\n\t\tint u = s.rbegin()->second;\n\t\tint deg = g[u].size();\n\t\ts.erase(pii(deg, u));\n\t\tif([&](){\n\t\t\tfor(int v : g[u]) if(g[v].size() != deg) return 0;\n\t\t\treturn 1;\n\t\t}()){\n\t\t\tans = ans * (deg + 1) % MOD;\n\t\t\tfor(int v : g[u]) s.erase(pii(deg, v));\n\t\t\tc ++;\n\t\t}else{\n\t\t\tfor(int v : g[u]){\n\t\t\t\ts.erase(pii(g[v].size(), v));\n\t\t\t\tg[v].erase(u);\n\t\t\t\ts.insert(pii(g[v].size(), v));\n\t\t\t}\n\t\t}\n\t}\n\tcout << c << endl;\n\tcout << ans << endl;\n\treturn 0;\n}", "accuracy": 1, "time_ms": 320, "memory_kb": 29704, "score_of_the_acc": -0.8053, "final_rank": 10 }, { "submission_id": "aoj_2291_1445951", "code_snippet": "#include <cstdio>\n#include <cmath>\n#include <cstring>\n#include <cstdlib>\n#include <climits>\n#include <ctime>\n#include <queue>\n#include <stack>\n#include <algorithm>\n#include <list>\n#include <vector>\n#include <set>\n#include <map>\n#include <iostream>\n#include <deque>\n#include <complex>\n#include <string>\n#include <iomanip>\n#include <sstream>\n#include <bitset>\n#include <valarray>\n#include <unordered_map>\n#include <iterator>\n#include <functional>\n#include <cassert>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned int uint;\ntypedef unsigned char uchar;\ntypedef unsigned long long ull;\ntypedef pair<int, int> pii;\ntypedef pair<ll, ll> pll;\ntypedef vector<int> vi;\n\n#define REP(i,x) for(int i=0;i<(int)(x);i++)\n#define REPS(i,x) for(int i=1;i<=(int)(x);i++)\n#define RREP(i,x) for(int i=((int)(x)-1);i>=0;i--)\n#define RREPS(i,x) for(int i=((int)(x));i>0;i--)\n#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();i++)\n#define RFOR(i,c) for(__typeof((c).rbegin())i=(c).rbegin();i!=(c).rend();i++)\n#define ALL(container) (container).begin(), (container).end()\n#define RALL(container) (container).rbegin(), (container).rend()\n#define SZ(container) ((int)container.size())\n#define mp(a,b) make_pair(a, b)\n#define pb push_back\n#define eb emplace_back\n#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );\n\ntemplate<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }\ntemplate<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }\ntemplate<class T> ostream& operator<<(ostream &os, const vector<T> &t) {\nos<<\"[\"; FOR(it,t) {if(it!=t.begin()) os<<\",\"; os<<*it;} os<<\"]\"; return os;\n}\ntemplate<class T> ostream& operator<<(ostream &os, const set<T> &t) {\nos<<\"{\"; FOR(it,t) {if(it!=t.begin()) os<<\",\"; os<<*it;} os<<\"}\"; return os;\n}\ntemplate<class S, class T> ostream& operator<<(ostream &os, const pair<S,T> &t) { return os<<\"(\"<<t.first<<\",\"<<t.second<<\")\";}\ntemplate<class S, class T> pair<S,T> operator+(const pair<S,T> &s, const pair<S,T> &t){ return pair<S,T>(s.first+t.first, s.second+t.second);}\ntemplate<class S, class T> pair<S,T> operator-(const pair<S,T> &s, const pair<S,T> &t){ return pair<S,T>(s.first-t.first, s.second-t.second);}\n\nconst int INF = 1<<28;\nconst double EPS = 1e-8;\nconst int MOD = 1000000007;\n\n\nint T, n, m;\n\nint main(int argc, char *argv[]){\n\tios::sync_with_stdio(false);\n\tcin >> n >> m;\n\tvector<set<int>> g(n);\n\tvi deg(n);\n\tREP(i, m){\n\t\tint u, v;\n\t\tcin >> u >> v; u--; v--;\n\t\tg[u].insert(v); g[v].insert(u);\n\t}\n\tset<pii> s;\n\tREP(i, n) s.insert(pii(g[i].size(), i));\n\tint c = 0;\n\tll ans = 1;\n\twhile(!s.empty()){\n\t\tint u = s.rbegin()->second;\n\t\tint deg = g[u].size();\n\t\ts.erase(pii(deg, u));\n\t\tif([&](){\n\t\t\tfor(int v : g[u]) if(g[v].size() != deg) return 0;\n\t\t\treturn 1;\n\t\t}()){\n\t\t\tans = ans * (deg + 1) % MOD;\n\t\t\tfor(int v : g[u]) s.erase(pii(deg, v));\n\t\t\tc ++;\n\t\t}else{\n\t\t\tfor(int v : g[u]){\n\t\t\t\ts.erase(pii(g[v].size(), v));\n\t\t\t\tg[v].erase(u);\n\t\t\t\ts.insert(pii(g[v].size(), v));\n\t\t\t}\n\t\t}\n\t}\n\tcout << c << endl;\n\tcout << ans << endl;\n\treturn 0;\n}", "accuracy": 0.175, "time_ms": 20, "memory_kb": 4128, "score_of_the_acc": 0, "final_rank": 17 }, { "submission_id": "aoj_2291_1185232", "code_snippet": "#include <iostream>\n#include <cstdio>\n#include <vector>\n#include <set>\n#include <map>\n#include <queue>\n#include <deque>\n#include <stack>\n#include <algorithm>\n#include <cstring>\n#include <functional>\n#include <cmath>\n#include <complex>\n#include <cassert>\nusing namespace std;\n#define rep(i,n) for(int i=0;i<(n);++i)\n#define rep1(i,n) for(int i=1;i<=(n);++i)\n#define all(c) (c).begin(),(c).end()\n#define fs first\n#define sc second\n#define pb push_back\n#define show(x) cout << #x << \" \" << x << endl\ntypedef long long ll;\ntypedef pair<int,int> P;\nll mod=1e9+9;\nint N,M,deg[100001];\nint ans=0;\nll num=1;\nvector<int> G[100001];\t//id,to\nbool done[100001]={};\nbool comp(const int& x,const int& y){return deg[x]>deg[y];}\nvoid add_edge(int x,int y){\n\tG[x].pb(y);\n\tG[y].pb(x);\n\tdeg[x]++,deg[y]++;\n}\nint main(){\n\tcin>>N>>M;\n\tif((ll)N*(N-1)/2==M){\n\t\tcout<<1<<endl<<N<<endl;\n\t\treturn 0;\n\t}\n\trep(i,M){\n\t\tint x,y;\n\t\tcin>>x>>y;\n\t\tadd_edge(x,y);\n\t}\n\trep1(i,N) add_edge(0,i);\n\tvector<int> vc;\n\trep(i,N+1) vc.pb(i);\n\tsort(all(vc),comp);\n\tfor(int a:vc){\n\t\tif(done[a]) continue;\n\t\tdone[a]=true;\n\t\tbool cl=true;\n\t\tfor(int v:G[a]){\n\t\t\tif(done[v]) continue;\n\t\t\tif(deg[v]!=deg[a]) cl=false;\n\t\t\tdeg[v]--;\n\t\t}\n\t\tif(cl){\n\t\t\tint cnt=1;\n\t\t\tdone[a]=true;\n\t\t\tfor(int v:G[a]){\n\t\t\t\tif(done[v]) continue;\n\t\t\t\tdone[v]=true;\n\t\t\t\tcnt++;\n\t\t\t}\n\t\t\tans++;\n\t\t\tnum=num*cnt%mod;\n\t\t}\n\t}\n\tcout<<ans<<endl<<num<<endl;\n}", "accuracy": 1, "time_ms": 120, "memory_kb": 10624, "score_of_the_acc": -0.2177, "final_rank": 3 }, { "submission_id": "aoj_2291_1184330", "code_snippet": "#include <iostream>\n#include <cstdio>\n#include <vector>\n#include <set>\n#include <map>\n#include <queue>\n#include <deque>\n#include <stack>\n#include <algorithm>\n#include <cstring>\n#include <functional>\n#include <cmath>\n#include <complex>\n#include <cassert>\nusing namespace std;\n#define rep(i,n) for(int i=0;i<(n);++i)\n#define rep1(i,n) for(int i=1;i<=(n);++i)\n#define all(c) (c).begin(),(c).end()\n#define fs first\n#define sc second\n#define pb push_back\n#define show(x) cout << #x << \" \" << x << endl\nint par[100001];\nvoid init(int n) { rep(i,n) par[i]=i; }\nint find(int x){\n\tif(x==par[x]) return x;\n\treturn par[x]=find(par[x]);\n}\nvoid unite(int x,int y){\n\tx=find(x);\n\ty=find(y);\n\tif(x==y) return;\n\tpar[x]=y;\n}\nbool same(int x,int y) { return find(x)==find(y); }\n\ntypedef long long ll;\nll mod=1e9+7;\nint N,M;\nint ans=0;\nll cnt=1;\nset<int> G[100001];\nvoid dfs(set<int> vc){\n\tll s=vc.size();\n\tint u=-1;\n\tbool cl=true;\n\tfor(int v:vc){\n\t\tif(G[v].size()==s-1&&u==-1) u=v;\n\t\tif(G[v].size()<s-1) cl=false;\n\t}\n\tif(cl){\n\t\tans++;\n\t\tcnt*=s;\n\t\tcnt%=mod;\n\t\treturn;\n\t}\n\tassert(u!=-1);\n\tfor(int v:G[u]) G[v].erase(u);\n\tG[u].clear();\n\tvc.erase(u);\n\tinit(N+1);\n\tfor(int v:vc){\n\t\tfor(int u:G[v]) unite(u,v);\n\t}\n\tmap< int,set<int> > sets;\n\tfor(int v:vc){\n\t\tsets[find(v)].insert(v);\n\t}\n\tfor(auto it:sets){\n\t\tdfs(it.sc);\n\t}\n}\nint main(){\n\tcin>>N>>M;\n\tif((ll)N*(N-1)/2==M){\n\t\tcout<<1<<endl<<N<<endl;\n\t\treturn 0;\n\t}\n\trep(i,M){\n\t\tint x,y;\n\t\tcin>>x>>y;\n\t\tG[x].insert(y);\n\t\tG[y].insert(x);\n\t}\n\trep1(i,N){\n\t\tG[0].insert(i);\n\t\tG[i].insert(0);\n\t}\n\tset<int> be;\n\trep(i,N+1) be.insert(i);\n\tdfs(be);\n\tcout<<ans<<endl<<cnt<<endl;\n}", "accuracy": 0.175, "time_ms": 50, "memory_kb": 9648, "score_of_the_acc": -0.1546, "final_rank": 18 } ]
aoj_2296_cpp
G: Quest of Merchant ICPC で良い成績を収めるには修行が欠かせない.うさぎは ICPC で勝ちたいので,今日も修行をすることにした. 今日の修行は,街を駆け回り交易を行うことで,商売の力を手にしようというものである. 今後の修行のためにも,できるだけ多くの資金を稼ぎたい. この世界は,東西南北方向の道路が等間隔に並んでいて,碁盤状になっている.唯一存在する市場の位置を (0, 0),街には x 座標, y 座標が定まっている (座標が整数値の点が交差点に対応する).うさぎは道路に沿ってのみ移動することができ,隣接する交差点間を移動するのに 1 分を要する.いくつかの交差点には街がある.交易では,街で商品を買い,市場にて販売することによって価格の差の分の利益を得る. うさぎの初期資金は十分にあり,お金が足りないから商品が購入できない,といったことはない.だが,それぞれの商品には重さがあり,うさぎは重さの合計が W までの商品しか同時に持ち運ぶことができない.よって,街に商品を仕入れに行ったり市場に戻ったりを繰り返すことになる.場合によっては,複数の街で商品を購入してから市場に戻ることになるかもしれない. 街での商品の購入や,市場での商品の販売は一瞬で行えるものとする.また,街にある商品が品切れになってしまうことは有り得ず,無限に購入することが可能である. うさぎは,今回売買を行おうとしている各商品の名前,1 個あたりの重さおよび販売価格と,各街の x 座標, y 座標および販売商品の名前と価格を,データにまとめた.うさぎは今,市場がある (0, 0) の位置にいる.プログラムを使って,制限時間 T 分の間にどれだけ稼ぐことが可能か調べたい. Input N M W T S 1 V 1 P 1 ... S M V M P M L 1 X 1 Y 1 R 1,1 Q 1,1 ... R 1, L 1 Q 1, L 1 ... L N X N Y N R N ,1 Q N ,1 ... R N , L N Q N , L N N は街の数, M は商品の種類数である. S i , V i , P i (1 ≤ i ≤ M ) はそれぞれ, i 番目の商品の名前,1 個あたりの重さ,1 個あたりの販売価格である. 街は 1 以上 N 以下の整数で表される. L j , X j , Y j (1 ≤ j ≤ N ) はそれぞれ,街 j で売られている商品の種類数,街 j の x 座標, y 座標である. R j , k , Q j , k (1 ≤ j ≤ N ,1 ≤ k ≤ L j ) はそれぞれ,街 j で売られている k 番目の商品の名前,価格である. 1 ≤ N ≤ 7,1 ≤ M ≤ 7,1 ≤ W ≤ 10,000,1 ≤ T ≤ 10,000,1 ≤ V i ≤ W ,1 ≤ P i ≤ 10,000,1 ≤ L j ≤ M ,-10,000 ≤ X j ≤ 10,000,-10,000 ≤ Y j ≤ 10,000,1 ≤ Q j , k ≤ 10,000 を満たす.商品の名前はアルファベット小文字からなる長さが 1 以上 7 以下の文字列である.その他の値はすべて整数である. S i はすべて異なる.( X j , Y j ) として同一の組は複数回現れず,( X j , Y j ) = (0, 0) となることはない. 各 j に対して R j , k はすべて異なり,各 R j , k はいずれかの S i に一致する. Output うさぎが得られる最大の利益を 1 行に出力せよ. Sample Input 1 2 2 100 20 alfalfa 10 10 carrot 5 10 1 1 6 carrot 4 1 -3 0 alfalfa 5 Sample Output 1 170 Sample Input 2 2 3 100 20 vim 10 10 emacs 5 10 vstudio 65 100 2 1 6 emacs 4 vstudio 13 3 -3 0 vim 5 emacs 9 vstudio 62 Sample Output 2 183
[ { "submission_id": "aoj_2296_10366916", "code_snippet": "#include <iostream>\n#include <iomanip>\n#include <cassert>\n#include <vector>\n#include <algorithm>\n#include <utility>\n#include <numeric>\n// #include \"Src/Utility/BinarySearch.hpp\"\n// #include \"Src/Sequence/CompressedSequence.hpp\"\n// #include \"Src/Sequence/RunLengthEncoding.hpp\"\n// using namespace zawa;\n// #include \"atcoder/modint\"\n// using mint = atcoder::modint998244353;\nint N, M, W, T, V[7], P[7], L[7], X[7], Y[7], R[7][7], Q[7][7];\nstd::string S[7];\nint posX(int i) {\n assert(0 <= i and i <= N);\n return i < N ? X[i] : 0;\n}\nint posY(int i) {\n assert(0 <= i and i <= N);\n return i < N ? Y[i] : 0;\n}\nint main() {\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n std::ios::sync_with_stdio(false);\n std::cin >> N >> M >> W >> T;\n for (int i = 0 ; i < M ; i++) std::cin >> S[i] >> V[i] >> P[i];\n for (int i = 0 ; i < N ; i++) {\n std::cin >> L[i] >> X[i] >> Y[i];\n for (int j = 0 ; j < L[i] ; j++) {\n std::string name;\n std::cin >> name >> Q[i][j];\n R[i][j] = -1;\n for (int k = 0 ; k < M ; k++) if (name == S[k]) {\n assert(R[i][j] == -1);\n R[i][j] = k;\n }\n assert(R[i][j] != -1);\n }\n }\n // 街の集合に対して、最短で巡回するのにかかる時間\n std::vector<int> time = [&]() -> std::vector<int> {\n const int INF = (int)2e9;\n std::vector dp(N + 1, std::vector<int>(1 << (N + 1), INF));\n dp[N][1 << N] = 0;\n for (int b = (1 << N) ; b < (1 << (N + 1)) ; b++) {\n for (int u = 0 ; u < N + 1 ; u++) if ((b & (1 << u)) and dp[u][b] != INF) {\n for (int v = 0 ; v < N ; v++) if (!(b & (1 << v))) {\n int w = std::abs(posX(u) - posX(v)) + std::abs(posY(u) - posY(v));\n assert(w > 0);\n dp[v][b | (1 << v)] = std::min(dp[v][b | (1 << v)], dp[u][b] + w);\n }\n }\n }\n std::vector<int> res(1 << N, INF);\n for (int b = (1 << N) ; b < (1 << (N + 1)) ; b++) {\n assert(b & (1 << N));\n int nb = b ^ (1 << N);\n for (int i = 0 ; i < N ; i++) if (b & (1 << i)) {\n assert(dp[i][b] != INF);\n int w = std::abs(posX(i)) + std::abs(posY(i));\n assert(w > 0);\n res[nb] = std::min(res[nb], dp[i][b] + w);\n }\n }\n return res;\n }();\n // 集合を巡回したときの得られる利得\n std::vector<long long> kiyo = [&]() -> std::vector<long long> {\n std::vector<long long> res(1 << N);\n for (int b = 0 ; b < (1 << N) ; b++) {\n std::vector<int> val(M, -1);\n for (int i = 0 ; i < N ; i++) if (b & (1 << i)) {\n for (int j = 0 ; j < L[i] ; j++) {\n int id = R[i][j];\n val[id] = std::max(val[id], P[id] - Q[i][j]);\n }\n }\n std::vector<long long> dp(W + 1, -1);\n dp[0] = 0;\n for (int i = 0 ; i < M ; i++) if (val[i] >= 1) {\n std::vector<long long> next = dp;\n for (int j = V[i] ; j <= W ; j++) if (next[j - V[i]] >= 0) {\n next[j] = std::max(next[j], next[j - V[i]] + val[i]);\n }\n dp = std::move(next);\n }\n res[b] = *std::max_element(dp.begin(), dp.end());\n assert(res[b] >= 0);\n }\n return res;\n }();\n // for (int i = 0 ; i < (1 << N) ; i++) std::cout << '(' << time[i] << ',' << kiyo[i] << ')';\n // std::cout << std::endl;\n std::vector<long long> dp(T + 1, -1LL);\n dp[0] = 0;\n for (int b = 0 ; b < (1 << N) ; b++) if (kiyo[b] >= 1) {\n std::vector<long long> next = dp;\n for (int i = time[b] ; i <= T ; i++) if (next[i - time[b]] >= 0) {\n next[i] = std::max(next[i], next[i - time[b]] + kiyo[b]);\n }\n dp = std::move(next);\n }\n std::cout << *std::max_element(dp.begin(), dp.end()) << '\\n';\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3840, "score_of_the_acc": -0.1412, "final_rank": 7 }, { "submission_id": "aoj_2296_10209781", "code_snippet": "// AOJ #2296\n// Quest of Merchant 2025.2.10\n\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\nconst int INF_INT = 1e9;\nconst ll NEG_INF_LL = -1e18;\n \nint manhattan(int x1, int y1, int x2, int y2){\n return abs(x1-x2) + abs(y1-y2);\n}\n \nstruct Product {\n string name;\n int weight;\n int marketPrice;\n};\n \nstruct TownOffer {\n int prodIndex;\n int price;\n};\n \nstruct Town {\n int x, y;\n vector<TownOffer> offers;\n};\n \nint main() {\n ios::sync_with_stdio(0);\n cin.tie(0);\n \n int N, M, W, T;\n cin >> N >> M >> W >> T;\n vector<Product> products(M);\n unordered_map<string,int> prodIndexMap;\n for (int i = 0; i < M; i++){\n cin >> products[i].name >> products[i].weight >> products[i].marketPrice;\n prodIndexMap[products[i].name] = i;\n }\n \n vector<Town> towns(N);\n for (int j = 0; j < N; j++){\n int L, X, Y;\n cin >> L >> X >> Y;\n towns[j].x = X;\n towns[j].y = Y;\n towns[j].offers.resize(L);\n for (int k = 0; k < L; k++){\n string prodName;\n int price;\n cin >> prodName >> price;\n int pindex = prodIndexMap[prodName];\n towns[j].offers[k] = {pindex, price};\n }\n }\n \n vector<int> distMarket(N);\n for (int i = 0; i < N; i++){\n distMarket[i] = manhattan(0, 0, towns[i].x, towns[i].y);\n }\n vector<vector<int>> distTown(N, vector<int>(N, 0));\n for (int i = 0; i < N; i++){\n for (int j = 0; j < N; j++){\n distTown[i][j] = manhattan(towns[i].x, towns[i].y, towns[j].x, towns[j].y);\n }\n }\n \n int totSub = 1 << N;\n vector<vector<int>> dp(totSub, vector<int>(N, INF_INT));\n for (int j = 0; j < N; j++){\n int mask = 1 << j;\n dp[mask][j] = distMarket[j];\n }\n for (int mask = 0; mask < totSub; mask++){\n for (int j = 0; j < N; j++){\n if(!(mask & (1 << j))) continue;\n for (int k = 0; k < N; k++){\n if(mask & (1 << k)) continue;\n int nextMask = mask | (1 << k);\n dp[nextMask][k] = min(dp[nextMask][k], dp[mask][j] + distTown[j][k]);\n }\n }\n }\n vector<int> cycleTime(totSub, INF_INT); \n for (int mask = 1; mask < totSub; mask++){\n int best = INF_INT;\n for (int j = 0; j < N; j++){\n if(mask & (1 << j))\n best = min(best, dp[mask][j] + distMarket[j]);\n }\n cycleTime[mask] = best;\n }\n \n vector<pair<int, ll>> cycles;\n for (int mask = 1; mask < totSub; mask++){\n const int INF_PRICE = 1000000000;\n vector<int> bestPrice(M, INF_PRICE);\n for (int j = 0; j < N; j++){\n if(mask & (1 << j)){\n for(auto &offer : towns[j].offers){\n int pidx = offer.prodIndex;\n bestPrice[pidx] = min(bestPrice[pidx], offer.price);\n }\n }\n }\n vector<pair<int,int>> items;\n for (int i = 0; i < M; i++){\n if(bestPrice[i] < INF_PRICE){\n int prodProfit = products[i].marketPrice - bestPrice[i];\n if(prodProfit > 0)\n items.push_back({products[i].weight, prodProfit});\n }\n }\n if(items.size() == 0) continue;\n \n vector<ll> knap(W+1, NEG_INF_LL);\n knap[0] = 0;\n for (int w = 0; w <= W; w++){\n if(knap[w] == NEG_INF_LL) continue;\n for(auto &it : items){\n int wt = it.first, prof = it.second;\n if(w + wt <= W) knap[w+wt] = max(knap[w+wt], knap[w] + prof);\n }\n }\n ll bestCycleProfit = 0;\n for (int w = 0; w <= W; w++)\n bestCycleProfit = max(bestCycleProfit, knap[w]);\n if(bestCycleProfit > 0){\n int tCycle = cycleTime[mask];\n cycles.push_back({tCycle, bestCycleProfit});\n }\n }\n \n vector<ll> dpTime(T+1, NEG_INF_LL);\n dpTime[0] = 0;\n for (int t = 0; t <= T; t++){\n if(dpTime[t] == NEG_INF_LL) continue;\n for(auto &cycle : cycles){\n int ct = cycle.first;\n ll cp = cycle.second;\n if(t + ct <= T) dpTime[t+ct] = max(dpTime[t+ct], dpTime[t] + cp);\n }\n }\n ll ans = 0;\n for (int t = 0; t <= T; t++) ans = max(ans, dpTime[t]);\n cout << ans << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3640, "score_of_the_acc": -0.027, "final_rank": 4 }, { "submission_id": "aoj_2296_9759438", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n/**\n * @brief template\n */\n\nint main() {\n ll N, M, W, T;\n cin >> N >> M >> W >> T;\n map<string,int> mps;\n vector<ll> V(M), P(M);\n rep(i,0,M) {\n string _;\n cin >> _;\n mps[_] = i;\n cin >> V[i] >> P[i];\n }\n vector<ll> X(N), Y(N);\n vector<vector<ll>> R(N), Q(N);\n rep(i,0,N) {\n int L;\n cin >> L;\n cin >> X[i] >> Y[i];\n R[i].resize(L);\n Q[i].resize(L);\n rep(j,0,L) {\n string s;\n cin >> s;\n R[i][j] = mps[s];\n cin >> Q[i][j];\n }\n }\n auto dist = [&](int i, int j) -> ll {return abs(X[i]-X[j])+abs(Y[i]-Y[j]);};\n vector<ll> Len(1<<N,INF);\n {\n vector<vector<ll>> DP(1<<N,vector<ll>(N,INF));\n rep(i,0,N) DP[1<<i][i] = abs(X[i]) + abs(Y[i]);\n rep(i,1,1<<N) {\n rep(j,0,N) {\n if (DP[i][j] == INF) continue;\n rep(k,0,N) {\n if (i & (1<<k)) continue;\n chmin(DP[i|(1<<k)][k], DP[i][j]+dist(j,k));\n }\n }\n }\n rep(i,1,1<<N) {\n rep(j,0,N) {\n if (i&(1<<j)) {\n chmin(Len[i], DP[i][j]+abs(X[j])+abs(Y[j]));\n }\n }\n }\n }\n vector<ll> Price(1<<N,INF);\n rep(i,1,1<<N) {\n vector<ll> Cost(M,0);\n rep(j,0,N) {\n if (i&(1<<j)) {\n rep(k,0,R[j].size()) {\n chmax(Cost[R[j][k]], P[R[j][k]]-Q[j][k]);\n }\n }\n }\n vector<ll> DP(W+1,0);\n rep(j,0,M) {\n rep(k,0,W+1) {\n if (k+V[j] <= W) chmax(DP[k+V[j]], DP[k]+Cost[j]);\n }\n }\n Price[i] = DP[W];\n }\n {\n vector<ll> DP(T+1,0);\n rep(i,1,1<<N) {\n rep(j,0,T+1) {\n if (j+(int)Len[i]<=T) chmax(DP[j+Len[i]], DP[j]+Price[i]);\n }\n }\n cout << DP[T] << endl;\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3532, "score_of_the_acc": -0.0193, "final_rank": 1 }, { "submission_id": "aoj_2296_9026285", "code_snippet": "#line 2 \"cp-library/src/cp-template.hpp\"\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nusing i32 = int;\nusing u32 = unsigned int;\nusing i64 = long long;\nusing u64 = unsigned long long;\nusing i128 = __int128_t;\ntemplate < class T > bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }\ntemplate < class T > bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }\ntemplate < class T, class U > T ceil (T x, U y) { return (x > 0 ? (x + y - 1) / y : x / y); }\ntemplate < class T, class U > T floor(T x, U y) { return (x > 0 ? x / y : (x - y + 1) / y); }\nint popcnt(i32 x) { return __builtin_popcount(x); }\nint popcnt(u32 x) { return __builtin_popcount(x); }\nint popcnt(i64 x) { return __builtin_popcountll(x); }\nint popcnt(u64 x) { return __builtin_popcountll(x); }\n\n#line 2 \"cp-library/src/utility/rep_itr.hpp\"\ntemplate < class T > struct itr_rep {\n T i, d;\n constexpr itr_rep(const T i) noexcept : i(i), d(1) {}\n constexpr itr_rep(const T i, const T d) noexcept : i(i), d(d) {}\n void operator++() noexcept { i += d; }\n constexpr int operator*() const noexcept { return i; }\n constexpr bool operator!=(const itr_rep x) const noexcept { return d > 0 ? i < x.i : i > x.i; }\n};\n\ntemplate < class T > struct rep {\n const itr_rep< T > s, t;\n constexpr rep(const T t) noexcept : s(0), t(t) {}\n constexpr rep(const T s, const T t) noexcept : s(s), t(t) {}\n constexpr rep(const T s, const T t, const T d) noexcept : s(s, d), t(t, d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n\ntemplate < class T > struct revrep {\n const itr_rep < T > s, t;\n constexpr revrep(const T t) noexcept : s(t - 1, -1), t(-1, -1) {}\n constexpr revrep(const T s, const T t) noexcept : s(t - 1, -1), t(s - 1, -1) {}\n constexpr revrep(const T s, const T t, const T d) noexcept : s(t - 1, -d), t(s - 1, -d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n#line 3 \"cp-library/src/utility/io.hpp\"\n\n/* 128bit integer */\nistream& operator>>(istream& is, i128& x) {\n std::string s; is >> s;\n int pm = (s[0] == '-');\n x = 0;\n for(int i : rep(pm, int(s.size()))) x = x * 10 + (s[i] - '0');\n if(pm) x *= -1;\n return is;\n}\nostream& operator<<(ostream& os, const i128& x) {\n if(x == 0) return os << '0';\n i128 y = x;\n if(y < 0) { os << '-'; y *= -1; }\n std::vector<int> ny;\n while(y > 0) { ny.push_back(y % 10); y /= 10; }\n for(int i : revrep(ny.size())) os << ny[i];\n return os;\n}\n\ntemplate < class S, class T > istream& operator>>(istream& is, std::pair< S, T >& x) { is >> x.first >> x.second; return is; }\ntemplate < class S, class T > ostream& operator<<(ostream& os, const std::pair< S, T >& x) { os << x.first << \" \" << x.second; return os; }\n\nnamespace scanner {\n struct sca {\n template < class T > operator T() {\n T s; std::cin >> s; return s;\n }\n };\n struct vec {\n int n;\n vec(int n) : n(n) {}\n template < class T > operator std::vector< T >() {\n std::vector< T > v(n);\n for(T& x : v) std::cin >> x;\n return v;\n }\n };\n struct mat {\n int h, w;\n mat(int h, int w) : h(h), w(w) {}\n template < class T > operator std::vector< std::vector< T > >() {\n std::vector m(h, std::vector< T >(w));\n for(std::vector< T >& v : m) for(T& x : v) std::cin >> x;\n return m;\n }\n };\n struct speedup {\n speedup() {\n std::cin.tie(0);\n std::ios::sync_with_stdio(0);\n }\n } speedup_instance;\n}\nscanner::sca in() { return scanner::sca(); }\nscanner::vec in(int n) { return scanner::vec(n); }\nscanner::mat in(int h, int w) { return scanner::mat(h, w); }\n\nnamespace printer {\n void precision(int d) { std::cout << std::fixed << std::setprecision(d); }\n void flush() { std::cout.flush(); }\n}\n\ntemplate < class T >\nostream& operator<<(ostream& os, const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) { os << a[i]; if(i != n - 1) os << ' '; }\n return os;\n}\n\nint print() { std::cout << '\\n'; return 0; }\ntemplate < class head, class... tail > int print(head&& h, tail&&... t) {\n std::cout << h; if(sizeof...(tail)) std::cout << ' ';\n return print(std::forward<tail>(t)...);\n}\ntemplate < class T > int print_n(const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) std::cout << a[i] << \"\\n\";\n return 0;\n}\n\n\n#line 2 \"cp-library/src/utility/key_val.hpp\"\n\ntemplate < class K, class V >\nstruct key_val {\n K key; V val;\n key_val() {}\n key_val(K key, V val) : key(key), val(val) {}\n template < std::size_t Index >\n std::tuple_element_t< Index, key_val >& get() {\n if constexpr (Index == 0) return key;\n if constexpr (Index == 1) return val;\n }\n};\n\nnamespace std {\n\ntemplate < class K, class V > struct tuple_size < key_val< K, V > > : integral_constant< size_t, 2 > {};\ntemplate < class K, class V > struct tuple_element < 0, key_val< K, V > > { using type = K; };\ntemplate < class K, class V > struct tuple_element < 1, key_val< K, V > > { using type = V; };\n\n}\n#line 2 \"cp-library/src/utility/vec_op.hpp\"\ntemplate < class T > key_val< int, T > max_of(const vector< T >& a) {\n int i = std::max_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class T > key_val< int, T > min_of(const vector< T >& a) {\n int i = std::min_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class S, class T > S sum_of(const vector< T >& a) {\n S sum = 0;\n for(const T x : a) sum += x;\n return sum;\n}\ntemplate < class S, class T > vector< S > freq_of(const vector< T >& a, T L, T R) {\n vector< S > res(R - L, S(0));\n for(const T x : a) res[x - L] += 1;\n return res;\n}\ntemplate < class S, class T > struct prefix_sum {\n vector< S > s;\n prefix_sum(const vector< T >& a) : s(a) {\n s.insert(s.begin(), S(0));\n for(int i : rep(a.size())) s[i + 1] += s[i];\n }\n // [L, R)\n S sum(int L, int R) { return s[R] - s[L]; }\n};\n#line 3 \"cp-library/src/utility/heap.hpp\"\n\ntemplate < class T > using heap_min = std::priority_queue< T, std::vector< T >, std::greater< T > >;\ntemplate < class T > using heap_max = std::priority_queue< T, std::vector< T >, std::less< T > >;\n\n#line 27 \"cp-library/src/cp-template.hpp\"\n\n#line 1 \"cp-library/src/algorithm/bin_search.hpp\"\ntemplate < class T, class F >\nT bin_search(T ok, T ng, F f) {\n while(abs(ng - ok) > 1) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n\ntemplate < class T, class F >\nT bin_search_real(T ok, T ng, F f, int step = 80) {\n while(step--) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n#line 2 \"cp-library/src/algorithm/argsort.hpp\"\n\ntemplate < class T > std::vector< int > argsort(const std::vector< T > &a) {\n std::vector< int > ids((int)a.size());\n std::iota(ids.begin(), ids.end(), 0);\n std::sort(ids.begin(), ids.end(), [&](int i, int j) {\n return a[i] < a[j] || (a[i] == a[j] && i < j);\n });\n return ids;\n}\n#line 1 \"macro.hpp\"\nnamespace macro {\n\nusing size_type = int;\ntemplate < class container > void sort(container& a) { std::sort(std:: begin(a), std:: end(a)); }\ntemplate < class container > void rsort(container& a) { std::sort(std::rbegin(a), std::rend(a)); }\ntemplate < class container > void reverse(container& a) { std::reverse(std::begin(a), std::end(a)); }\ntemplate < class container > void unique(container& a) {\n std::sort(std::begin(a), std::end(a));\n a.erase(std::unique(std::begin(a), std::end(a)), std::end(a));\n}\ntemplate < class container > container sorted(const container& a) { container b = a; sort(b); return std::move(b); }\ntemplate < class container > container rsorted(const container& a) { container b = a; rsort(b); return std::move(b); }\ntemplate < class container, class compare > void sort(container& a, const compare& cmp) { std::sort(std::begin(a), std::end(a), cmp); }\ntemplate < class container, class compare > container sorted(const container& a, const compare& cmp) { container b = a; sort(b, cmp); return std::move(b); }\ntemplate < class container, class value > size_type lower_bound(const container& a, const value& x) { return std::lower_bound(std::begin(a), std::end(a), x) - std::begin(a); }\ntemplate < class container, class value > size_type upper_bound(const container& a, const value& x) { return std::upper_bound(std::begin(a), std::end(a), x) - std::begin(a); }\n\nconst std::vector<std::pair<size_type, size_type>> dir4 = { {+1, 0}, {-1, 0}, { 0, +1}, { 0, -1} };\nconst std::vector<std::pair<size_type, size_type>> dir8 = { {-1, -1}, {-1, 0}, {-1, +1}, { 0, -1}, { 0, +1}, {+1, -1}, {+1, 0}, {+1, +1} };\n\n#ifdef _DEBUG\n#define debug(x) std::cout << \"[\" << __LINE__ << \"] \" << #x << \": \" << x << std::endl\n#else\n#define debug(x)\n#endif\n\ntemplate < class container > void concat(container& a, const container& b) {\n a.insert(std::end(a), std::begin(b), std::end(b));\n}\nstd::vector<size_type> iota(const size_type n) {\n std::vector<size_type> I(n);\n std::iota(std::begin(I), std::end(I), 0);\n return I;\n}\ntemplate < class container > std::vector<size_type> sort_idx(const container& a) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return a[i] < a[j] or (a[i] == a[j] and i < j); });\n return I;\n}\ntemplate < class container, class compare > std::vector<size_type> sort_idx(const container& a, const compare& cmp) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return cmp(a[i], a[j]) or (a[i] == a[j] and i < j); });\n return std::move(I);\n}\n\nstruct grid {\n using size_type = int;\n size_type H, W;\n grid(const size_type H, const size_type W) : H(H), W(W) {}\n bool contains(const size_type i, const size_type j) {\n return 0 <= i and i < H and 0 <= j and j < W;\n }\n};\n\nusing f64 = long double;\n\n} // namespace macro\n\nusing namespace macro;\n#line 3 \"A.cpp\"\n\ni64 knapsack(const vector<pair<int,int>>& wv, int W) {\n const int n = wv.size();\n vector dp(n + 1, vector(W + 1, i64(0)));\n for(int i : rep(n)) {\n auto [w, v] = wv[i];\n for(int s = 0; s <= W; s++) {\n chmax(dp[i + 1][s], dp[i][s]);\n if(0 <= s - w) chmax(dp[i + 1][s], dp[i + 1][s - w] + v);\n }\n }\n return dp[n][W];\n}\n\nint main() {\n int N = in(), M = in(), W = in(), T = in();\n map<string, pair<int,int>> item;\n for(int i : rep(M)) {\n string s = in(); int w = in(), v = in();\n item[s] = {w, v};\n }\n\n vector<pair<int,int>> pos(N);\n vector<vector<pair<int,int>>> city(N);\n for(int i : rep(N)) {\n int L = in(), x = in(), y = in();\n pos[i] = {x, y};\n for(int j : rep(L)) {\n string s = in(); int p = in();\n auto [w, v] = item[s];\n city[i].push_back({w, v - p});\n }\n }\n\n vector<pair<int,int>> b;\n for(int S : rep(1 << N)) if(S != 0) {\n vector<int> I;\n for(int i : rep(N)) if(S & (1 << i)) I.push_back(i);\n const int len = I.size();\n int TS = 1e9;\n do {\n int cur = 0;\n cur += abs(pos[I[0]].first - 0) + abs(pos[I[0]].second - 0);\n for(int i : rep(len - 1)) \n cur += abs(pos[I[i]].first - pos[I[i + 1]].first) + abs(pos[I[i]].second - pos[I[i + 1]].second);\n cur += abs(pos[I[len - 1]].first - 0) + abs(pos[I[len - 1]].second - 0);\n chmin(TS, cur);\n } while(next_permutation(I.begin(), I.end()));\n\n vector<pair<int,int>> a;\n for(int i : I) a.insert(a.end(), city[i].begin(), city[i].end());\n int VS = knapsack(a, W);\n b.push_back({TS, VS});\n }\n\n print(knapsack(b, T));\n}", "accuracy": 1, "time_ms": 110, "memory_kb": 13244, "score_of_the_acc": -1.7089, "final_rank": 13 }, { "submission_id": "aoj_2296_9026283", "code_snippet": "#line 2 \"cp-library/src/cp-template.hpp\"\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nusing i32 = int;\nusing u32 = unsigned int;\nusing i64 = long long;\nusing u64 = unsigned long long;\nusing i128 = __int128_t;\ntemplate < class T > bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }\ntemplate < class T > bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }\ntemplate < class T, class U > T ceil (T x, U y) { return (x > 0 ? (x + y - 1) / y : x / y); }\ntemplate < class T, class U > T floor(T x, U y) { return (x > 0 ? x / y : (x - y + 1) / y); }\nint popcnt(i32 x) { return __builtin_popcount(x); }\nint popcnt(u32 x) { return __builtin_popcount(x); }\nint popcnt(i64 x) { return __builtin_popcountll(x); }\nint popcnt(u64 x) { return __builtin_popcountll(x); }\n\n#line 2 \"cp-library/src/utility/rep_itr.hpp\"\ntemplate < class T > struct itr_rep {\n T i, d;\n constexpr itr_rep(const T i) noexcept : i(i), d(1) {}\n constexpr itr_rep(const T i, const T d) noexcept : i(i), d(d) {}\n void operator++() noexcept { i += d; }\n constexpr int operator*() const noexcept { return i; }\n constexpr bool operator!=(const itr_rep x) const noexcept { return d > 0 ? i < x.i : i > x.i; }\n};\n\ntemplate < class T > struct rep {\n const itr_rep< T > s, t;\n constexpr rep(const T t) noexcept : s(0), t(t) {}\n constexpr rep(const T s, const T t) noexcept : s(s), t(t) {}\n constexpr rep(const T s, const T t, const T d) noexcept : s(s, d), t(t, d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n\ntemplate < class T > struct revrep {\n const itr_rep < T > s, t;\n constexpr revrep(const T t) noexcept : s(t - 1, -1), t(-1, -1) {}\n constexpr revrep(const T s, const T t) noexcept : s(t - 1, -1), t(s - 1, -1) {}\n constexpr revrep(const T s, const T t, const T d) noexcept : s(t - 1, -d), t(s - 1, -d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n#line 3 \"cp-library/src/utility/io.hpp\"\n\n/* 128bit integer */\nistream& operator>>(istream& is, i128& x) {\n std::string s; is >> s;\n int pm = (s[0] == '-');\n x = 0;\n for(int i : rep(pm, int(s.size()))) x = x * 10 + (s[i] - '0');\n if(pm) x *= -1;\n return is;\n}\nostream& operator<<(ostream& os, const i128& x) {\n if(x == 0) return os << '0';\n i128 y = x;\n if(y < 0) { os << '-'; y *= -1; }\n std::vector<int> ny;\n while(y > 0) { ny.push_back(y % 10); y /= 10; }\n for(int i : revrep(ny.size())) os << ny[i];\n return os;\n}\n\ntemplate < class S, class T > istream& operator>>(istream& is, std::pair< S, T >& x) { is >> x.first >> x.second; return is; }\ntemplate < class S, class T > ostream& operator<<(ostream& os, const std::pair< S, T >& x) { os << x.first << \" \" << x.second; return os; }\n\nnamespace scanner {\n struct sca {\n template < class T > operator T() {\n T s; std::cin >> s; return s;\n }\n };\n struct vec {\n int n;\n vec(int n) : n(n) {}\n template < class T > operator std::vector< T >() {\n std::vector< T > v(n);\n for(T& x : v) std::cin >> x;\n return v;\n }\n };\n struct mat {\n int h, w;\n mat(int h, int w) : h(h), w(w) {}\n template < class T > operator std::vector< std::vector< T > >() {\n std::vector m(h, std::vector< T >(w));\n for(std::vector< T >& v : m) for(T& x : v) std::cin >> x;\n return m;\n }\n };\n struct speedup {\n speedup() {\n std::cin.tie(0);\n std::ios::sync_with_stdio(0);\n }\n } speedup_instance;\n}\nscanner::sca in() { return scanner::sca(); }\nscanner::vec in(int n) { return scanner::vec(n); }\nscanner::mat in(int h, int w) { return scanner::mat(h, w); }\n\nnamespace printer {\n void precision(int d) { std::cout << std::fixed << std::setprecision(d); }\n void flush() { std::cout.flush(); }\n}\n\ntemplate < class T >\nostream& operator<<(ostream& os, const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) { os << a[i]; if(i != n - 1) os << ' '; }\n return os;\n}\n\nint print() { std::cout << '\\n'; return 0; }\ntemplate < class head, class... tail > int print(head&& h, tail&&... t) {\n std::cout << h; if(sizeof...(tail)) std::cout << ' ';\n return print(std::forward<tail>(t)...);\n}\ntemplate < class T > int print_n(const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) std::cout << a[i] << \"\\n\";\n return 0;\n}\n\n\n#line 2 \"cp-library/src/utility/key_val.hpp\"\n\ntemplate < class K, class V >\nstruct key_val {\n K key; V val;\n key_val() {}\n key_val(K key, V val) : key(key), val(val) {}\n template < std::size_t Index >\n std::tuple_element_t< Index, key_val >& get() {\n if constexpr (Index == 0) return key;\n if constexpr (Index == 1) return val;\n }\n};\n\nnamespace std {\n\ntemplate < class K, class V > struct tuple_size < key_val< K, V > > : integral_constant< size_t, 2 > {};\ntemplate < class K, class V > struct tuple_element < 0, key_val< K, V > > { using type = K; };\ntemplate < class K, class V > struct tuple_element < 1, key_val< K, V > > { using type = V; };\n\n}\n#line 2 \"cp-library/src/utility/vec_op.hpp\"\ntemplate < class T > key_val< int, T > max_of(const vector< T >& a) {\n int i = std::max_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class T > key_val< int, T > min_of(const vector< T >& a) {\n int i = std::min_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class S, class T > S sum_of(const vector< T >& a) {\n S sum = 0;\n for(const T x : a) sum += x;\n return sum;\n}\ntemplate < class S, class T > vector< S > freq_of(const vector< T >& a, T L, T R) {\n vector< S > res(R - L, S(0));\n for(const T x : a) res[x - L] += 1;\n return res;\n}\ntemplate < class S, class T > struct prefix_sum {\n vector< S > s;\n prefix_sum(const vector< T >& a) : s(a) {\n s.insert(s.begin(), S(0));\n for(int i : rep(a.size())) s[i + 1] += s[i];\n }\n // [L, R)\n S sum(int L, int R) { return s[R] - s[L]; }\n};\n#line 3 \"cp-library/src/utility/heap.hpp\"\n\ntemplate < class T > using heap_min = std::priority_queue< T, std::vector< T >, std::greater< T > >;\ntemplate < class T > using heap_max = std::priority_queue< T, std::vector< T >, std::less< T > >;\n\n#line 27 \"cp-library/src/cp-template.hpp\"\n\n#line 1 \"cp-library/src/algorithm/bin_search.hpp\"\ntemplate < class T, class F >\nT bin_search(T ok, T ng, F f) {\n while(abs(ng - ok) > 1) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n\ntemplate < class T, class F >\nT bin_search_real(T ok, T ng, F f, int step = 80) {\n while(step--) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n#line 2 \"cp-library/src/algorithm/argsort.hpp\"\n\ntemplate < class T > std::vector< int > argsort(const std::vector< T > &a) {\n std::vector< int > ids((int)a.size());\n std::iota(ids.begin(), ids.end(), 0);\n std::sort(ids.begin(), ids.end(), [&](int i, int j) {\n return a[i] < a[j] || (a[i] == a[j] && i < j);\n });\n return ids;\n}\n#line 1 \"macro.hpp\"\nnamespace macro {\n\nusing size_type = int;\ntemplate < class container > void sort(container& a) { std::sort(std:: begin(a), std:: end(a)); }\ntemplate < class container > void rsort(container& a) { std::sort(std::rbegin(a), std::rend(a)); }\ntemplate < class container > void reverse(container& a) { std::reverse(std::begin(a), std::end(a)); }\ntemplate < class container > void unique(container& a) {\n std::sort(std::begin(a), std::end(a));\n a.erase(std::unique(std::begin(a), std::end(a)), std::end(a));\n}\ntemplate < class container > container sorted(const container& a) { container b = a; sort(b); return std::move(b); }\ntemplate < class container > container rsorted(const container& a) { container b = a; rsort(b); return std::move(b); }\ntemplate < class container, class compare > void sort(container& a, const compare& cmp) { std::sort(std::begin(a), std::end(a), cmp); }\ntemplate < class container, class compare > container sorted(const container& a, const compare& cmp) { container b = a; sort(b, cmp); return std::move(b); }\ntemplate < class container, class value > size_type lower_bound(const container& a, const value& x) { return std::lower_bound(std::begin(a), std::end(a), x) - std::begin(a); }\ntemplate < class container, class value > size_type upper_bound(const container& a, const value& x) { return std::upper_bound(std::begin(a), std::end(a), x) - std::begin(a); }\n\nconst std::vector<std::pair<size_type, size_type>> dir4 = { {+1, 0}, {-1, 0}, { 0, +1}, { 0, -1} };\nconst std::vector<std::pair<size_type, size_type>> dir8 = { {-1, -1}, {-1, 0}, {-1, +1}, { 0, -1}, { 0, +1}, {+1, -1}, {+1, 0}, {+1, +1} };\n\n#ifdef _DEBUG\n#define debug(x) std::cout << \"[\" << __LINE__ << \"] \" << #x << \": \" << x << std::endl\n#else\n#define debug(x)\n#endif\n\ntemplate < class container > void concat(container& a, const container& b) {\n a.insert(std::end(a), std::begin(b), std::end(b));\n}\nstd::vector<size_type> iota(const size_type n) {\n std::vector<size_type> I(n);\n std::iota(std::begin(I), std::end(I), 0);\n return I;\n}\ntemplate < class container > std::vector<size_type> sort_idx(const container& a) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return a[i] < a[j] or (a[i] == a[j] and i < j); });\n return I;\n}\ntemplate < class container, class compare > std::vector<size_type> sort_idx(const container& a, const compare& cmp) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return cmp(a[i], a[j]) or (a[i] == a[j] and i < j); });\n return std::move(I);\n}\n\nstruct grid {\n using size_type = int;\n size_type H, W;\n grid(const size_type H, const size_type W) : H(H), W(W) {}\n bool contains(const size_type i, const size_type j) {\n return 0 <= i and i < H and 0 <= j and j < W;\n }\n};\n\nusing f64 = long double;\n\n} // namespace macro\n\nusing namespace macro;\n#line 3 \"A.cpp\"\n\nint knapsack(const vector<pair<int,int>>& wv, int W) {\n const int n = wv.size();\n vector dp(n + 1, vector(W + 1, 0));\n for(int i : rep(n)) {\n auto [w, v] = wv[i];\n for(int s = 0; s <= W; s++) {\n chmax(dp[i + 1][s], dp[i][s]);\n if(0 <= s - w) chmax(dp[i + 1][s], dp[i + 1][s - w] + v);\n }\n }\n return dp[n][W];\n}\n\nint main() {\n int N = in(), M = in(), W = in(), T = in();\n map<string, pair<int,int>> item;\n for(int i : rep(M)) {\n string s = in(); int w = in(), v = in();\n item[s] = {w, v};\n }\n\n vector<pair<int,int>> pos(N);\n vector<vector<pair<int,int>>> city(N);\n for(int i : rep(N)) {\n int L = in(), x = in(), y = in();\n pos[i] = {x, y};\n for(int j : rep(L)) {\n string s = in(); int p = in();\n auto [w, v] = item[s];\n city[i].push_back({w, v - p});\n }\n }\n\n vector<pair<int,int>> b;\n for(int S : rep(1 << N)) if(S != 0) {\n vector<int> I;\n for(int i : rep(N)) if(S & (1 << i)) I.push_back(i);\n const int len = I.size();\n int TS = 1e9;\n do {\n int cur = 0;\n cur += abs(pos[I[0]].first - 0) + abs(pos[I[0]].second - 0);\n for(int i : rep(len - 1)) \n cur += abs(pos[I[i]].first - pos[I[i + 1]].first) + abs(pos[I[i]].second - pos[I[i + 1]].second);\n cur += abs(pos[I[len - 1]].first - 0) + abs(pos[I[len - 1]].second - 0);\n chmin(TS, cur);\n } while(next_permutation(I.begin(), I.end()));\n\n vector<pair<int,int>> a;\n for(int i : I) a.insert(a.end(), city[i].begin(), city[i].end());\n int VS = knapsack(a, W);\n b.push_back({TS, VS});\n }\n\n print(knapsack(b, T));\n}", "accuracy": 0.08333333333333333, "time_ms": 60, "memory_kb": 8076, "score_of_the_acc": -0.8419, "final_rank": 19 }, { "submission_id": "aoj_2296_6823338", "code_snippet": "#include <bits/stdc++.h>\n#pragma GCC optimize(\"Ofast\")\nusing namespace std;\nusing std::cout;\nusing std::cin;\nusing std::endl;\nusing ll=long long;\nusing ld=long double;\nll ILL=2167167167167167167;\nconst int INF=2100000000;\nconst ll mod=998244353;\n#define rep(i,a) for (ll i=0;i<a;i++)\n#define all(p) p.begin(),p.end()\ntemplate<class T> using _pq = priority_queue<T, vector<T>, greater<T>>;\ntemplate<class T> ll LB(vector<T> &v,T a){return lower_bound(v.begin(),v.end(),a)-v.begin();}\ntemplate<class T> ll UB(vector<T> &v,T a){return upper_bound(v.begin(),v.end(),a)-v.begin();}\ntemplate<class T> bool chmin(T &a,const T &b){if(a>b){a=b;return 1;}else return 0;}\ntemplate<class T> bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}else return 0;}\ntemplate<class T> void So(vector<T> &v) {sort(v.begin(),v.end());}\ntemplate<class T> void Sore(vector<T> &v) {sort(v.begin(),v.end(),[](T x,T y){return x>y;});}\nvoid yneos(bool a){if(a) cout<<\"Yes\\n\"; else cout<<\"No\\n\";}\ntemplate<class T> void vec_out(vector<T> &p){for(int i=0;i<(int)(p.size());i++){if(i) cout<<\" \";cout<<p[i];}cout<<\"\\n\";}\n\n\nvoid solve();\n// oddloop\nint main() {\n\tios::sync_with_stdio(false);\n\tcin.tie(nullptr);\n\t\n\tsolve();\n}\n\nvoid solve(){\n\tint N,M,W,T;\n\tcin>>N>>M>>W>>T;\n\tmap<string,pair<int,int>> m;\n\tauto dp=[](int lim,vector<pair<int,ll>> p)->ll{\n\t\tvector<ll> ans(lim+1);\n\t\tfor(auto x:p){\n\t\t\tint ind=0;\n\t\t\twhile(ind+x.first<=lim){\n\t\t\t\tchmax(ans[ind+x.first],ans[ind]+x.second);\n\t\t\t\tind++;\n\t\t\t}\n\t\t}\n\t\treturn ans[lim];\n\t};\n\tvector<int> X(N+1),Y(N+1);\n\tvector<vector<int>> dist(N+1,vector<int>(N+1));\n\trep(i,M){\n\t\tstring S;\n\t\tint v,p;\n\t\tcin>>S>>v>>p;\n\t\tm[S]={v,p};\n\t}\n\tvector<vector<pair<int,int>>> buy(N);\n\trep(i,N){\n\t\tint L;\n\t\tcin>>L>>X[i]>>Y[i];\n\t\trep(j,L){\n\t\t\tstring S;int p;\n\t\t\tcin>>S>>p;\n\t\t\tbuy[i].push_back({m[S].first,m[S].second-p});\n\t\t}\n\t}\n\tvector<pair<int,ll>> runner;\n\trep(i,N+1) rep(j,N+1) dist[i][j]=(abs(X[i]-X[j])+abs(Y[i]-Y[j]));\n\trep(i,(1<<N)){\n\t\tif(i==0) continue;\n\t\tvector<pair<int,ll>> cost;\n\t\tvector<int> ind;\n\t\tint K=0;\n\t\trep(j,N){\n\t\t\tif(i&(1<<j)){\n\t\t\t\tfor(auto x:buy[j]) cost.push_back(x);\n\t\t\t\tind.push_back(j);K++;\n\t\t\t}\n\t\t}\n\t\tvector<vector<int>> sale(K,vector<int>(1<<K,INF));\n\t\trep(j,K) sale[j][(1<<j)]=dist[ind[j]][N];\n\t\trep(k,(1<<K)) rep(j,K){\n\t\t\tif(sale[j][k]==INF) continue;\n\t\t\trep(l,K){\n\t\t\t\tif(((1<<l)&k)==0){\n\t\t\t\t\tchmin(sale[l][k+(1<<l)],sale[j][k]+dist[ind[j]][ind[l]]);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tint t=INF;\n\t\trep(j,K) chmin(t,sale[j][(1<<K)-1]+dist[ind[j]][N]);\n\t\trunner.push_back({t,dp(W,cost)});\n\t}\n\t//for(auto x:runner) cout<<x.first<<\" \"<<x.second<<\"\\n\";\n\tcout<<dp(T,runner)<<\"\\n\";\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3552, "score_of_the_acc": -0.1207, "final_rank": 5 }, { "submission_id": "aoj_2296_6823297", "code_snippet": "#include <bits/stdc++.h>\n#pragma GCC optimize(\"Ofast\")\n#define _GLIBCXX_DEBUG\nusing namespace std;\nusing std::cout;\nusing std::cin;\nusing std::endl;\nusing ll=long long;\nusing ld=long double;\nll ILL=2167167167167167167;\nconst int INF=2100000000;\nconst ll mod=998244353;\n#define rep(i,a) for (ll i=0;i<a;i++)\n#define all(p) p.begin(),p.end()\ntemplate<class T> using _pq = priority_queue<T, vector<T>, greater<T>>;\ntemplate<class T> ll LB(vector<T> &v,T a){return lower_bound(v.begin(),v.end(),a)-v.begin();}\ntemplate<class T> ll UB(vector<T> &v,T a){return upper_bound(v.begin(),v.end(),a)-v.begin();}\ntemplate<class T> bool chmin(T &a,const T &b){if(a>b){a=b;return 1;}else return 0;}\ntemplate<class T> bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}else return 0;}\ntemplate<class T> void So(vector<T> &v) {sort(v.begin(),v.end());}\ntemplate<class T> void Sore(vector<T> &v) {sort(v.begin(),v.end(),[](T x,T y){return x>y;});}\nvoid yneos(bool a){if(a) cout<<\"Yes\\n\"; else cout<<\"No\\n\";}\ntemplate<class T> void vec_out(vector<T> &p){for(int i=0;i<(int)(p.size());i++){if(i) cout<<\" \";cout<<p[i];}cout<<\"\\n\";}\n\n\n\nvoid solve();\n// oddloop\nint main() {\n\tios::sync_with_stdio(false);\n\tcin.tie(nullptr);\n\t\n\tsolve();\n}\n\nvoid solve(){\n\tint N,M,W,T;\n\tcin>>N>>M>>W>>T;\n\tmap<string,pair<int,int>> m;\n\tauto dp=[](int lim,vector<pair<int,int>> p)->int{\n\t\tvector<int> ans(lim+1);\n\t\tfor(auto x:p){\n\t\t\tint ind=0;\n\t\t\twhile(ind+x.first<=lim){\n\t\t\t\tchmax(ans[ind+x.first],ans[ind]+x.second);\n\t\t\t\tind++;\n\t\t\t}\n\t\t}\n\t\treturn ans[lim];\n\t};\n\tvector<int> X(N+1),Y(N+1);\n\tvector<vector<int>> dist(N+1,vector<int>(N+1));\n\trep(i,M){\n\t\tstring S;\n\t\tint v,p;\n\t\tcin>>S>>v>>p;\n\t\tm[S]={v,p};\n\t}\n\tvector<vector<pair<int,int>>> buy(N);\n\trep(i,N){\n\t\tint L;\n\t\tcin>>L>>X[i]>>Y[i];\n\t\trep(j,L){\n\t\t\tstring S;int p;\n\t\t\tcin>>S>>p;\n\t\t\tbuy[i].push_back({m[S].first,m[S].second-p});\n\t\t}\n\t}\n\tvector<pair<int,int>> runner;\n\trep(i,N+1) rep(j,N+1) dist[i][j]=(abs(X[i]-X[j])+abs(Y[i]-Y[j]));\n\trep(i,(1<<N)){\n\t\tif(i==0) continue;\n\t\tvector<pair<int,int>> cost;\n\t\tvector<int> ind;\n\t\tint K=0;\n\t\trep(j,N){\n\t\t\tif(i&(1<<j)){\n\t\t\t\tfor(auto x:buy[j]) cost.push_back(x);\n\t\t\t\tind.push_back(j);K++;\n\t\t\t}\n\t\t}\n\t\tvector<vector<int>> sale(K,vector<int>(1<<K,INF));\n\t\trep(j,K) sale[j][(1<<j)]=dist[ind[j]][N];\n\t\trep(k,(1<<K)) rep(j,K){\n\t\t\tif(sale[j][k]==INF) continue;\n\t\t\trep(l,K){\n\t\t\t\tif(((1<<l)&k)==0){\n\t\t\t\t\tchmin(sale[l][k+(1<<l)],sale[j][k]+dist[ind[j]][ind[l]]);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tint t=INF;\n\t\trep(j,K) chmin(t,sale[j][(1<<K)-1]+dist[ind[j]][N]);\n\t\trunner.push_back({t,dp(W,cost)});\n\t}\n\t//for(auto x:runner) cout<<x.first<<\" \"<<x.second<<\"\\n\";\n\tcout<<dp(T,runner)<<\"\\n\";\n}", "accuracy": 0.08333333333333333, "time_ms": 20, "memory_kb": 3400, "score_of_the_acc": -0.1099, "final_rank": 16 }, { "submission_id": "aoj_2296_6439222", "code_snippet": "#line 1 \"a.cpp\"\n#define PROBLEM \"\"\n#line 2 \"/home/kuhaku/atcoder/github/atcoder-lib/lib/template/atcoder.hpp\"\n#pragma GCC target(\"avx\")\n#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"unroll-loops\")\n#line 2 \"/home/kuhaku/atcoder/github/atcoder-lib/lib/template/template.hpp\"\n#include <bits/stdc++.h>\nusing namespace std;\ntemplate <class T, class U>\nbool chmax(T &a, const U &b) {\n return a < b ? a = b, true : false;\n}\ntemplate <class T, class U>\nbool chmin(T &a, const U &b) {\n return b < a ? a = b, true : false;\n}\nconstexpr int64_t INF = 1000000000000000003;\nconstexpr int Inf = 1000000003;\nconstexpr int MOD = 1000000007;\nconstexpr int MOD_N = 998244353;\nconstexpr double EPS = 1e-7;\nconst double PI = acos(-1.0);\n#line 6 \"/home/kuhaku/atcoder/github/atcoder-lib/lib/template/atcoder.hpp\"\nusing ll = int64_t;\nusing ld = long double;\n#define FOR(i, m, n) for(int i = (m); i < int(n); ++i)\n#define FORR(i, m, n) for(int i = (m)-1; i >= int(n); --i)\n#define FORL(i, m, n) for(int64_t i = (m); i < int64_t(n); ++i)\n#define rep(i, n) FOR(i, 0, n)\n#define repn(i, n) FOR(i, 1, n+1)\n#define repr(i, n) FORR(i, n, 0)\n#define repnr(i, n) FORR(i, n+1, 1)\n#define all(s) (s).begin(), (s).end()\ntemplate<class T, class U>\nstd::istream &operator>>(std::istream &is, std::pair<T, U> &p) { is >> p.first >> p.second; return is; }\ntemplate <class T>\nstd::istream &operator>>(std::istream &is, std::vector<T> &v) { for (T &i : v) is>>i; return is; }\ntemplate <class T, class U>\nstd::ostream &operator<<(std::ostream &os, const std::pair<T, U> &p) {\n return os<<'('<<p.first<< ','<<p.second<<')';\n}\ntemplate <class T>\nstd::ostream &operator<<(std::ostream &os, const std::vector<T> &v) {\n for (auto it=v.begin(); it!=v.end(); ++it) { os<<(it==v.begin()?\"\":\" \")<<*it; } return os;\n}\ntemplate <class Head, class... Tail>\nvoid co(Head&& head, Tail&&... tail) {\n if constexpr(sizeof...(tail)==0) std::cout<<head<<'\\n'; else std::cout<<head<<' ',co(forward<Tail>(tail)...);\n}\ntemplate <class Head, class... Tail>\nvoid ce(Head&& head, Tail&&... tail) {\n if constexpr(sizeof...(tail)==0) std::cerr<<head<<'\\n'; else std::cerr<<head<<' ',ce(forward<Tail>(tail)...);\n}\ntemplate<typename T, typename... Args>\nauto make_vector(T x, int arg, Args ...args) {\n if constexpr(sizeof...(args)==0) return std::vector<T>(arg,x); else return std::vector(arg,make_vector<T>(x,args...));\n}\nvoid sonic() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); }\nvoid setp(const int n) { std::cout<<std::fixed<<std::setprecision(n); }\nvoid Yes(bool is_correct) { std::cout<<(is_correct?\"Yes\":\"No\")<<std::endl; }\nvoid YES(bool is_correct) { std::cout<<(is_correct?\"YES\":\"NO\")<<std::endl; }\n#line 3 \"a.cpp\"\n\nint main(void) {\n#define int ll\n sonic();\n int n, m, w, t;\n cin >> n >> m >> w >> t;\n unordered_map<string, pair<int, int>> mp;\n rep(i, m) {\n string s;\n int v, p;\n cin >> s >> v >> p;\n mp[s] = {v, p};\n }\n vector<int> l(n), x(n), y(n);\n vector<vector<int>> v(n), q(n);\n rep(i, n) {\n cin >> l[i] >> x[i] >> y[i];\n v[i].resize(l[i]);\n q[i].resize(l[i]);\n rep(j, l[i]) {\n string s;\n cin >> s >> q[i][j];\n v[i][j] = mp[s].first;\n q[i][j] = mp[s].second - q[i][j];\n }\n }\n\n vector<int> idx(n);\n iota(all(idx), 0);\n\n vector<int> tm(1 << n, Inf);\n do {\n int s = 0;\n int xx = 0, yy = 0;\n int b = 0;\n for (auto i : idx) {\n b |= 1 << i;\n s += abs(xx - x[i]) + abs(yy - y[i]);\n xx = x[i], yy = y[i];\n chmin(tm[b], s + abs(xx) + abs(yy));\n }\n } while (next_permutation(all(idx)));\n\n vector<int> ans(t + 1);\n rep(bit, 1 << n) {\n vector<int> dp(w + 1);\n rep(i, n) {\n if (!(bit >> i & 1))\n continue;\n rep(j, l[i]) {\n rep(k, w - v[i][j] + 1) {\n chmax(dp[k + v[i][j]], dp[k] + q[i][j]);\n }\n }\n }\n if (tm[bit] <= t)\n chmax(ans[tm[bit]], *max_element(all(dp)));\n }\n\n repn(i, t) {\n rep(j, t + 1 - i) chmax(ans[j + i], ans[j] + ans[i]);\n }\n co(*max_element(all(ans)));\n\n return 0;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 3600, "score_of_the_acc": -0.5241, "final_rank": 10 }, { "submission_id": "aoj_2296_6439220", "code_snippet": "#line 1 \"a.cpp\"\n#define PROBLEM \"\"\n#line 2 \"/home/kuhaku/atcoder/github/atcoder-lib/lib/template/atcoder.hpp\"\n#pragma GCC target(\"avx\")\n#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"unroll-loops\")\n#line 2 \"/home/kuhaku/atcoder/github/atcoder-lib/lib/template/template.hpp\"\n#include <bits/stdc++.h>\nusing namespace std;\ntemplate <class T, class U>\nbool chmax(T &a, const U &b) {\n return a < b ? a = b, true : false;\n}\ntemplate <class T, class U>\nbool chmin(T &a, const U &b) {\n return b < a ? a = b, true : false;\n}\nconstexpr int64_t INF = 1000000000000000003;\nconstexpr int Inf = 1000000003;\nconstexpr int MOD = 1000000007;\nconstexpr int MOD_N = 998244353;\nconstexpr double EPS = 1e-7;\nconst double PI = acos(-1.0);\n#line 6 \"/home/kuhaku/atcoder/github/atcoder-lib/lib/template/atcoder.hpp\"\nusing ll = int64_t;\nusing ld = long double;\n#define FOR(i, m, n) for(int i = (m); i < int(n); ++i)\n#define FORR(i, m, n) for(int i = (m)-1; i >= int(n); --i)\n#define FORL(i, m, n) for(int64_t i = (m); i < int64_t(n); ++i)\n#define rep(i, n) FOR(i, 0, n)\n#define repn(i, n) FOR(i, 1, n+1)\n#define repr(i, n) FORR(i, n, 0)\n#define repnr(i, n) FORR(i, n+1, 1)\n#define all(s) (s).begin(), (s).end()\ntemplate<class T, class U>\nstd::istream &operator>>(std::istream &is, std::pair<T, U> &p) { is >> p.first >> p.second; return is; }\ntemplate <class T>\nstd::istream &operator>>(std::istream &is, std::vector<T> &v) { for (T &i : v) is>>i; return is; }\ntemplate <class T, class U>\nstd::ostream &operator<<(std::ostream &os, const std::pair<T, U> &p) {\n return os<<'('<<p.first<< ','<<p.second<<')';\n}\ntemplate <class T>\nstd::ostream &operator<<(std::ostream &os, const std::vector<T> &v) {\n for (auto it=v.begin(); it!=v.end(); ++it) { os<<(it==v.begin()?\"\":\" \")<<*it; } return os;\n}\ntemplate <class Head, class... Tail>\nvoid co(Head&& head, Tail&&... tail) {\n if constexpr(sizeof...(tail)==0) std::cout<<head<<'\\n'; else std::cout<<head<<' ',co(forward<Tail>(tail)...);\n}\ntemplate <class Head, class... Tail>\nvoid ce(Head&& head, Tail&&... tail) {\n if constexpr(sizeof...(tail)==0) std::cerr<<head<<'\\n'; else std::cerr<<head<<' ',ce(forward<Tail>(tail)...);\n}\ntemplate<typename T, typename... Args>\nauto make_vector(T x, int arg, Args ...args) {\n if constexpr(sizeof...(args)==0) return std::vector<T>(arg,x); else return std::vector(arg,make_vector<T>(x,args...));\n}\nvoid sonic() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); }\nvoid setp(const int n) { std::cout<<std::fixed<<std::setprecision(n); }\nvoid Yes(bool is_correct) { std::cout<<(is_correct?\"Yes\":\"No\")<<std::endl; }\nvoid YES(bool is_correct) { std::cout<<(is_correct?\"YES\":\"NO\")<<std::endl; }\n#line 3 \"a.cpp\"\n\nint main(void) {\n sonic();\n int n, m, w, t;\n cin >> n >> m >> w >> t;\n unordered_map<string, pair<int, int>> mp;\n rep(i, m) {\n string s;\n int v, p;\n cin >> s >> v >> p;\n mp[s] = {v, p};\n }\n vector<int> l(n), x(n), y(n);\n vector<vector<int>> v(n), q(n);\n rep(i, n) {\n cin >> l[i] >> x[i] >> y[i];\n v[i].resize(l[i]);\n q[i].resize(l[i]);\n rep(j, l[i]) {\n string s;\n cin >> s >> q[i][j];\n v[i][j] = mp[s].first;\n q[i][j] = mp[s].second - q[i][j];\n }\n }\n\n vector<int> idx(n);\n iota(all(idx), 0);\n\n vector<int> tm(1 << n, Inf);\n do {\n int s = 0;\n int xx = 0, yy = 0;\n int b = 0;\n for (auto i : idx) {\n b |= 1 << i;\n s += abs(xx - x[i]) + abs(yy - y[i]);\n xx = x[i], yy = y[i];\n chmin(tm[b], s + abs(xx) + abs(yy));\n }\n } while (next_permutation(all(idx)));\n\n vector<int> ans(t + 1);\n rep(bit, 1 << n) {\n vector<int> dp(w + 1);\n rep(i, n) {\n if (!(bit >> i & 1))\n continue;\n rep(j, l[i]) {\n rep(k, w - v[i][j] + 1) {\n chmax(dp[k + v[i][j]], dp[k] + q[i][j]);\n }\n }\n }\n if (tm[bit] <= t)\n chmax(ans[tm[bit]], *max_element(all(dp)));\n }\n\n repn(i, t) {\n rep(j, t + 1 - i) chmax(ans[j + i], ans[j] + ans[i]);\n }\n co(*max_element(all(ans)));\n\n return 0;\n}", "accuracy": 0.08333333333333333, "time_ms": 50, "memory_kb": 3444, "score_of_the_acc": -0.4131, "final_rank": 18 }, { "submission_id": "aoj_2296_6022860", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)\n\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << fixed << setprecision(15);\n\n int n, m, w, t;\n cin >> n >> m >> w >> t;\n map<string, pair<ll, ll>> products;\n rep(i,0,m) {\n string s;\n int v, p;\n cin >> s >> v >> p;\n products[s] = {v, p};\n }\n vector<ll> x(n), y(n);\n vector<map<string, ll>> sell(n);\n rep(i,0,n) {\n int l;\n cin >> l >> x[i] >> y[i];\n rep(j,0,l) {\n string r;\n int q;\n cin >> r >> q;\n sell[i][r] =q;\n }\n }\n\n vector<ll> cost(1<<n, 1e9), prof(1<<n);\n\n rep(S,1,1<<n) {\n vector<int> visit;\n rep(i,0,n) {\n if (S>>i&1) visit.push_back(i);\n }\n int k = __builtin_popcount(S);\n do {\n ll d = 0;\n d += abs(x[visit[0]]) + abs(y[visit[0]]);\n rep(i,0,k-1) {\n d += abs(x[visit[i+1]]-x[visit[i]]) + abs(y[visit[i+1]]-y[visit[i]]);\n }\n d += abs(x[visit[k-1]]) + abs(y[visit[k-1]]);\n cost[S] = min(cost[S], d);\n } while (next_permutation(visit.begin(), visit.end()));\n\n map<string, ll> min_price;\n rep(i,0,n) {\n if (!(S>>i&1)) continue;\n for (auto [r, q] : sell[i]) {\n if (!min_price.count(r)) min_price[r] = q;\n else min_price[r] = min(min_price[r], q);\n }\n }\n\n vector<ll> dp(w+1);\n for (auto [s, p] : min_price) {\n auto [weight, gain] = products[s];\n gain -= p;\n if (gain <= 0) continue;\n rep(j,0,w-weight+1) {\n dp[j+weight] = max(dp[j+weight], dp[j]+gain);\n }\n }\n\n prof[S] = dp[w];\n }\n\n vector<ll> dp(t+1);\n rep(i,0,1<<n) {\n rep(j,0,t-cost[i]+1) {\n dp[j+cost[i]] = max(dp[j+cost[i]], dp[j]+prof[i]);\n }\n }\n cout << dp[t] << endl;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3564, "score_of_the_acc": -0.0216, "final_rank": 3 }, { "submission_id": "aoj_2296_6022853", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)\n\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << fixed << setprecision(15);\n\n int n, m, w, t;\n cin >> n >> m >> w >> t;\n map<string, pair<int, int>> products;\n rep(i,0,m) {\n string s;\n int v, p;\n cin >> s >> v >> p;\n products[s] = {v, p};\n }\n vector<int> x(n), y(n);\n vector<map<string, int>> sell(n);\n rep(i,0,n) {\n int l;\n cin >> l >> x[i] >> y[i];\n rep(j,0,l) {\n string r;\n int q;\n cin >> r >> q;\n sell[i][r] =q;\n }\n }\n\n vector<int> cost(1<<n, 1e9), prof(1<<n);\n\n rep(S,1,1<<n) {\n vector<int> visit;\n rep(i,0,n) {\n if (S>>i&1) visit.push_back(i);\n }\n int k = __builtin_popcount(S);\n do {\n int d = 0;\n d += abs(x[visit[0]]) + abs(y[visit[0]]);\n rep(i,0,k-1) {\n d += abs(x[visit[i+1]]-x[visit[i]]) + abs(y[visit[i+1]]-y[visit[i]]);\n }\n d += abs(x[visit[k-1]]) + abs(y[visit[k-1]]);\n cost[S] = min(cost[S], d);\n } while (next_permutation(visit.begin(), visit.end()));\n\n map<string, int> min_price;\n rep(i,0,n) {\n if (!(S>>i&1)) continue;\n for (auto [r, q] : sell[i]) {\n if (!min_price.count(r)) min_price[r] = q;\n else min_price[r] = min(min_price[r], q);\n }\n }\n\n vector<int> dp(w+1);\n for (auto [s, p] : min_price) {\n auto [weight, gain] = products[s];\n gain -= p;\n if (gain <= 0) continue;\n rep(j,0,w-weight+1) {\n dp[j+weight] = max(dp[j+weight], dp[j]+gain);\n }\n }\n\n prof[S] = dp[w];\n }\n\n vector<int> dp(t+1);\n rep(i,0,1<<n) {\n rep(j,0,t-cost[i]+1) {\n dp[j+cost[i]] = max(dp[j+cost[i]], dp[j]+prof[i]);\n }\n }\n cout << dp[t] << endl;\n}", "accuracy": 0.08333333333333333, "time_ms": 10, "memory_kb": 3500, "score_of_the_acc": -0.017, "final_rank": 15 }, { "submission_id": "aoj_2296_5998004", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\nusing PII = pair<ll, ll>;\n\n#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)\n#define REP(i, n) FOR(i, 0, n)\n#define ALL(x) x.begin(), x.end()\n#define POPCOUNT(x) __builtin_popcount(x)\ntemplate <typename T> void chmin(T &a, const T &b) { a = min(a, b); }\ntemplate <typename T> void chmax(T &a, const T &b) { a = max(a, b); }\n\nconst ll INF = 1LL << 60;\n\nstruct FastIO {\n FastIO() {\n cin.tie(0);\n ios::sync_with_stdio(0);\n }\n} fastiofastio;\n\nconst ll MOD = 1e9 + 7;\n\n// BEGIN CUT\nll modpow(ll x, ll y, ll m) {\n ll a = 1, p = x;\n while (y > 0) {\n if (y % 2 == 0) {\n p = (p * p) % m;\n y /= 2;\n } else {\n a = (a * p) % m;\n y--;\n }\n }\n return a;\n}\n// END CUT\n\nll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }\nll lcm(ll a, ll b) { return a / gcd(a, b) * b; }\n\nstring S[7];\nint V[7], P[7], L[7], X[7], Y[7], R[7][7], Q[7][7];\nll ma[1 << 7], tim[1 << 7];\nll dp[50][10001];\nll dp2[1 << 7][10001];\n\nint main() {\n int N, M, W, T;\n cin >> N >> M >> W >> T;\n map<string, int> idx;\n for (int i = 0; i < M; i++) {\n cin >> S[i] >> V[i] >> P[i];\n idx[S[i]] = i;\n }\n for (int i = 0; i < N; i++) {\n cin >> L[i] >> X[i] >> Y[i];\n for (int j = 0; j < L[i]; j++) {\n string s;\n cin >> s >> Q[i][j];\n R[i][j] = idx[s];\n }\n }\n for (int bit = 0; bit < (1 << N); bit++) {\n vector<pair<int, int>> prod;\n for (int i = 0; i < N; i++) {\n if ((bit & (1 << i)) == 0)\n continue;\n for (int j = 0; j < L[i]; j++) {\n prod.emplace_back(V[R[i][j]], P[R[i][j]] - Q[i][j]);\n }\n }\n memset(dp, 0, sizeof(dp));\n for (int i = 0; i < prod.size(); i++) {\n auto [v, p] = prod[i];\n for (int j = 0; j <= W; j++) {\n dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);\n if (j >= v)\n dp[i + 1][j] = max(dp[i + 1][j], dp[i + 1][j - v] + p);\n }\n }\n for (int i = 0; i <= W; i++)\n ma[bit] = max(ma[bit], dp[prod.size()][i]);\n }\n for (int bit = 0; bit < (1 << N); bit++) {\n vector<int> perm;\n for (int i = 0; i < N; i++) {\n if (bit & (1 << i))\n perm.push_back(i);\n }\n int mi = 1 << 30;\n do {\n int prevX = 0, prevY = 0;\n int sum = 0;\n for (int p : perm) {\n sum += abs(X[p] - prevX) + abs(Y[p] - prevY);\n prevX = X[p];\n prevY = Y[p];\n }\n sum += abs(prevX) + abs(prevY);\n mi = min(mi, sum);\n } while (next_permutation(ALL(perm)));\n tim[bit] = mi;\n }\n for (int i = 1; i < (1 << N); i++) {\n for (int j = 0; j <= T; j++) {\n dp2[i][j] = max(dp2[i][j], dp2[i - 1][j]);\n if (j >= tim[i])\n dp2[i][j] = max(dp2[i][j], dp2[i][j - tim[i]] + ma[i]);\n }\n }\n ll ans = 0;\n for (int i = 0; i <= T; i++)\n ans = max(ans, dp2[(1 << N) - 1][i]);\n cout << ans << endl;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 17344, "score_of_the_acc": -1.4, "final_rank": 12 }, { "submission_id": "aoj_2296_5998001", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\nusing PII = pair<ll, ll>;\n\n#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)\n#define REP(i, n) FOR(i, 0, n)\n#define ALL(x) x.begin(), x.end()\n#define POPCOUNT(x) __builtin_popcount(x)\ntemplate <typename T> void chmin(T &a, const T &b) { a = min(a, b); }\ntemplate <typename T> void chmax(T &a, const T &b) { a = max(a, b); }\n\nconst ll INF = 1LL << 60;\n\nstruct FastIO {\n FastIO() {\n cin.tie(0);\n ios::sync_with_stdio(0);\n }\n} fastiofastio;\n\nconst ll MOD = 1e9 + 7;\n\n// BEGIN CUT\nll modpow(ll x, ll y, ll m) {\n ll a = 1, p = x;\n while (y > 0) {\n if (y % 2 == 0) {\n p = (p * p) % m;\n y /= 2;\n } else {\n a = (a * p) % m;\n y--;\n }\n }\n return a;\n}\n// END CUT\n\nll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }\nll lcm(ll a, ll b) { return a / gcd(a, b) * b; }\n\nstring S[7];\nint V[7], P[7], L[7], X[7], Y[7], R[7][7], Q[7][7];\nint ma[1 << 7], tim[1 << 7];\nint dp[50][10001];\nint dp2[1 << 7][10001];\n\nint main() {\n int N, M, W, T;\n cin >> N >> M >> W >> T;\n map<string, int> idx;\n for (int i = 0; i < M; i++) {\n cin >> S[i] >> V[i] >> P[i];\n idx[S[i]] = i;\n }\n for (int i = 0; i < N; i++) {\n cin >> L[i] >> X[i] >> Y[i];\n for (int j = 0; j < L[i]; j++) {\n string s;\n cin >> s >> Q[i][j];\n R[i][j] = idx[s];\n }\n }\n for (int bit = 0; bit < (1 << N); bit++) {\n vector<pair<int, int>> prod;\n for (int i = 0; i < N; i++) {\n if ((bit & (1 << i)) == 0)\n continue;\n for (int j = 0; j < L[i]; j++) {\n prod.emplace_back(V[R[i][j]], P[R[i][j]] - Q[i][j]);\n }\n }\n memset(dp, 0, sizeof(dp));\n for (int i = 0; i < prod.size(); i++) {\n auto [v, p] = prod[i];\n for (int j = 0; j <= W; j++) {\n dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);\n if (j >= v)\n dp[i + 1][j] = max(dp[i + 1][j], dp[i + 1][j - v] + p);\n }\n }\n for (int i = 0; i <= W; i++)\n ma[bit] = max(ma[bit], dp[prod.size()][i]);\n }\n for (int bit = 0; bit < (1 << N); bit++) {\n vector<int> perm;\n for (int i = 0; i < N; i++) {\n if (bit & (1 << i))\n perm.push_back(i);\n }\n int mi = 1 << 30;\n do {\n int prevX = 0, prevY = 0;\n int sum = 0;\n for (int p : perm) {\n sum += abs(X[p] - prevX) + abs(Y[p] - prevY);\n prevX = X[p];\n prevY = Y[p];\n }\n sum += abs(prevX) + abs(prevY);\n mi = min(mi, sum);\n } while (next_permutation(ALL(perm)));\n tim[bit] = mi;\n }\n for (int i = 1; i < (1 << N); i++) {\n for (int j = 0; j <= T; j++) {\n dp2[i][j] = max(dp2[i][j], dp2[i - 1][j]);\n if (j >= tim[i])\n dp2[i][j] = max(dp2[i][j], dp2[i][j - tim[i]] + ma[i]);\n }\n }\n int ans = 0;\n for (int i = 0; i <= T; i++)\n ans = max(ans, dp2[(1 << N) - 1][i]);\n cout << ans << endl;\n}", "accuracy": 0.08333333333333333, "time_ms": 50, "memory_kb": 10320, "score_of_the_acc": -0.9013, "final_rank": 20 }, { "submission_id": "aoj_2296_5943056", "code_snippet": "#pragma GCC optimize(\"Ofast\")\n#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <map>\n#include <queue>\n#include <cstdio>\n#include <ctime>\n#include <assert.h>\n#include <chrono>\n#include <random>\n#include <numeric>\n#include <set>\n#include <deque>\n#include <stack>\n#include <sstream>\n#include <utility>\n#include <cstring>\n#include <unordered_map>\n#include <unordered_set>\n#include <tuple>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned long long ull;\n\nmt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());\nll myRand(ll B) {\n return (ull)rng() % B;\n}\n\nint n;\nint x[8],y[8];\nint dp[1<<8][8];\nint min_time[1<<7];\n\nvoid TSP(){\n n++;\n for(int i=0;i<(1<<n);i++){\n for(int j=0;j<n;j++){\n dp[i][j] = (1<<30);\n }\n }\n n--;\n dp[0][n] = 0;\n n++;\n for(int i=0;i<(1<<n);i++){\n for(int j=0;j<n;j++){\n if(dp[i][j] == (1<<30)) continue;\n for(int k=0;k<n;k++){\n if((1<<k)&i)continue;\n int nx = (1<<k)|i;\n int cost = dp[i][j] + abs(x[j]-x[k]) + abs(y[j]-y[k]);\n dp[nx][k] = min(dp[nx][k], cost);\n }\n }\n } \n n--;\n}\n\nint main(){\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n int m,W,T; cin >> n >> m >> W >> T;\n map<string,int> to_id;\n vector<int> v(m),p(m);\n for(int i=0;i<m;i++){\n string s; cin >> s;\n to_id[s] = i;\n cin >> v[i] >> p[i];\n }\n vector<vector<pair<int,int>>> pro(n);\n for(int i=0;i<n;i++){\n int L; cin >> L;\n cin >> x[i] >> y[i];\n for(int j=0;j<L;j++){\n string r; cin >> r;\n int q; cin >> q;\n pro[i].push_back({to_id[r],q});\n }\n }\n x[n] = y[n] = 0;\n TSP();\n for(int i=1;i<(1<<n);i++){\n min_time[i] = dp[i+(1<<n)][n];\n }\n vector<int> opt(1<<n);\n for(int i=1;i<(1<<n);i++){\n if(min_time[i]>T)continue;\n int bit = 0;\n vector<int> cost(m,1<<30);\n for(int j=0;j<n;j++){\n if((1<<j)&i){\n for(auto &p:pro[j]){\n bit |= (1<<p.first);\n cost[p.first] = min(cost[p.first],p.second);\n }\n }\n }\n vector<ll> dp2(W+1);\n for(int j=1;j<=W;j++){\n dp2[j] = max(dp2[j], dp2[j-1]);\n for(int k=0;k<m;k++){\n if((1<<k)&bit){\n if(j>=v[k]){\n dp2[j] = max(dp2[j], dp2[j-v[k]]+p[k]-cost[k]);\n }\n }\n }\n }\n opt[i] = dp2.back();\n }\n vector<ll> dp3(T+1);\n for(int i=1;i<=T;i++){\n dp3[i] = max(dp3[i],dp3[i-1]);\n for(int j=1;j<(1<<n);j++){\n if(i-min_time[j]>=0){\n dp3[i] = max(dp3[i], dp3[i-min_time[j]]+opt[j]);\n }\n }\n }\n cout << dp3.back() << endl;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3536, "score_of_the_acc": -0.0196, "final_rank": 2 }, { "submission_id": "aoj_2296_5943051", "code_snippet": "#pragma GCC optimize(\"Ofast\")\n#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <map>\n#include <queue>\n#include <cstdio>\n#include <ctime>\n#include <assert.h>\n#include <chrono>\n#include <random>\n#include <numeric>\n#include <set>\n#include <deque>\n#include <stack>\n#include <sstream>\n#include <utility>\n#include <cstring>\n#include <unordered_map>\n#include <unordered_set>\n#include <tuple>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned long long ull;\n\nmt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());\nll myRand(ll B) {\n return (ull)rng() % B;\n}\n\nint n;\nint x[8],y[8];\nint dp[1<<8][8];\nint min_time[1<<7];\n\nvoid TSP(){\n n++;\n for(int i=0;i<(1<<n);i++){\n for(int j=0;j<n;j++){\n dp[i][j] = (1<<30);\n }\n }\n n--;\n dp[0][n] = 0;\n n++;\n for(int i=0;i<(1<<n);i++){\n for(int j=0;j<n;j++){\n if(dp[i][j] == (1<<30)) continue;\n for(int k=0;k<n;k++){\n if((1<<k)&i)continue;\n int nx = (1<<k)|i;\n int cost = dp[i][j] + abs(x[j]-x[k]) + abs(y[j]-y[k]);\n dp[nx][k] = min(dp[nx][k], cost);\n }\n }\n } \n n--;\n}\n\nint main(){\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n int m,W,T; cin >> n >> m >> W >> T;\n map<string,int> to_id;\n vector<int> v(m),p(m);\n for(int i=0;i<m;i++){\n string s; cin >> s;\n to_id[s] = i;\n cin >> v[i] >> p[i];\n }\n vector<vector<pair<int,int>>> pro(n);\n for(int i=0;i<n;i++){\n int L; cin >> L;\n cin >> x[i] >> y[i];\n for(int j=0;j<L;j++){\n string r; cin >> r;\n int q; cin >> q;\n pro[i].push_back({to_id[r],q});\n }\n }\n x[n] = y[n] = 0;\n TSP();\n for(int i=1;i<(1<<n);i++){\n min_time[i] = dp[i+(1<<n)][n];\n }\n vector<int> opt(1<<n);\n for(int i=1;i<(1<<n);i++){\n if(min_time[i]>T)continue;\n int bit = 0;\n vector<int> cost(m,1<<30);\n for(int j=0;j<n;j++){\n if((1<<j)&i){\n for(auto &p:pro[j]){\n bit |= (1<<p.first);\n cost[p.first] = min(cost[p.first],p.second);\n }\n }\n }\n vector<int> dp2(W+1);\n for(int j=1;j<=W;j++){\n dp2[j] = max(dp2[j], dp2[j-1]);\n for(int k=0;k<m;k++){\n if((1<<k)&bit){\n if(j>=v[k]){\n dp2[j] = max(dp2[j], dp2[j-v[k]]+p[k]-cost[k]);\n }\n }\n }\n }\n opt[i] = dp2.back();\n }\n vector<int> dp3(T+1);\n for(int i=1;i<=T;i++){\n dp3[i] = max(dp3[i],dp3[i-1]);\n for(int j=1;j<(1<<n);j++){\n if(i-min_time[j]>=0){\n dp3[i] = max(dp3[i], dp3[i-min_time[j]]+opt[j]);\n }\n }\n }\n cout << dp3.back() << endl;\n}", "accuracy": 0.08333333333333333, "time_ms": 10, "memory_kb": 3444, "score_of_the_acc": -0.0131, "final_rank": 14 }, { "submission_id": "aoj_2296_5870687", "code_snippet": "#pragma GCC target(\"avx2\")\n#pragma GCC optimize(\"Ofast\")\n#pragma GCC optimize(\"unroll-loops\")\n#include <bits/stdc++.h>\nusing namespace std;\n#define DEBUG\n#ifdef DEBUG\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << '(' << p.first << ',' << p.second << ')';\n return os;\n}\ntemplate <class T> ostream &operator<<(ostream &os, const vector<T> &v) {\n os << '{';\n for(int i = 0; i < (int)v.size(); i++) {\n if(i) { os << ','; }\n os << v[i];\n }\n os << '}';\n return os;\n}\nvoid debugg() { cerr << endl; }\ntemplate <class T, class... Args>\nvoid debugg(const T &x, const Args &... args) {\n cerr << \" \" << x;\n debugg(args...);\n}\n#define debug(...) \\\n cerr << __LINE__ << \" [\" << #__VA_ARGS__ << \"]: \", debugg(__VA_ARGS__)\n#define dump(x) cerr << __LINE__ << \" \" << #x << \" = \" << (x) << endl\n#else\n#define debug(...) (void(0))\n#define dump(x) (void(0))\n#endif\nusing namespace std;\ntypedef long long ll;\ntypedef vector<ll> vl;\ntypedef vector<vl> vvl;\ntypedef vector<char> vc;\ntypedef vector<string> vs;\ntypedef vector<bool> vb;\ntypedef vector<double> vd;\ntypedef pair<ll,ll> P;\ntypedef pair<int,int> pii;\ntypedef vector<P> vpl;\ntypedef tuple<ll,ll,ll> tapu;\n#define rep(i,n) for(int i=0; i<(n); i++)\n#define REP(i,a,b) for(int i=(a); i<(b); i++)\n#define all(x) (x).begin(), (x).end()\n#define rall(x) (x).rbegin(), (x).rend()\nconst int inf = (1<<30)-1;\nconst ll linf = 1LL<<61;\nconst int MAX = 510000;\nint dy[8] = {0,1,0,-1,1,-1,-1,1};\nint dx[8] = {-1,0,1,0,1,-1,1,-1};\nconst double pi = acos(-1);\nconst double eps = 1e-7;\ntemplate<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){\n\tif(a>b){\n\t\ta = b; return true;\n\t}\n\telse return false;\n}\ntemplate<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){\n\tif(a<b){\n\t\ta = b; return true;\n\t}\n\telse return false;\n}\ntemplate<typename T> inline void print(T &a){\n int sz = a.size();\n for(auto itr = a.begin(); itr != a.end(); itr++){\n\t\tcout << *itr;\n sz--;\n if(sz) cout << \" \";\n\t}\n cout << \"\\n\";\n}\ntemplate<typename T1,typename T2> inline void print2(T1 a, T2 b){\n\tcout << a << \" \" << b << \"\\n\";\n}\ntemplate<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){\n\tcout << a << \" \" << b << \" \" << c << \"\\n\";\n}\nvoid mark() {cout << \"#\" << \"\\n\";}\nll pcount(ll x) {return __builtin_popcountll(x);}\n//#include <atcoder/all>\n//using namespace atcoder;\n//const int mod = 1e9 + 7;\nconst int mod = 998244353;\n\nint main(){\n int n,m,w,t; cin >> n >> m >> w >> t;\n map<string,int> mp;\n vl v(m),p(m);\n rep(i,m){\n string s;\n cin >> s >> v[i] >> p[i];\n mp[s] = i;\n }\n vector<vpl> z(n);\n vl x(n),y(n);\n rep(i,n){\n int l; cin >> l;\n cin >> x[i] >> y[i];\n z[i].resize(l);\n rep(j,l){\n string r; cin >> r;\n int q; cin >> q;\n z[i][j] = {mp[r],q};\n }\n }\n vl dp(t+1,-linf);\n dp[0] = 0;\n REP(bit,1,1<<n){\n vl a;\n rep(i,n) if(bit>>i & 1) a.push_back(i);\n ll d = inf;\n int m = a.size();\n do{\n ll cd = abs(x[a[0]]) + abs(y[a[0]]);\n rep(i,m-1){\n cd += abs(x[a[i]]-x[a[i+1]]) + abs(y[a[i]]-y[a[i+1]]);\n }\n cd += abs(x[a[m-1]]) + abs(y[a[m-1]]);\n chmin(d,cd);\n }while(next_permutation(all(a)));\n vl ep(w+1,-linf);\n ep[0] = 0;\n rep(i,n){\n if(bit>>i & 1 ^ 1) continue;\n for(auto b : z[i]){\n rep(j,w){\n if(j+v[b.first] > w) break;\n chmax(ep[j+v[b.first]], ep[j] + p[b.first] - b.second);\n }\n }\n }\n ll mx = *max_element(all(ep));\n rep(i,t){\n if(i+d > t) break;\n chmax(dp[i+d], dp[i]+mx);\n }\n }\n cout << *max_element(all(dp)) << endl;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3596, "score_of_the_acc": -0.1239, "final_rank": 6 }, { "submission_id": "aoj_2296_5252814", "code_snippet": "#include <iostream>\n#include <algorithm>\n#include <vector>\n#include <string>\n#include <map>\n\nusing namespace std;\nusing lint = long long;\n\nconstexpr int INF = 1 << 30;\n\nvoid solve() {\n int n, m, wmax, t;\n cin >> n >> m >> wmax >> t;\n\n map<string, int> id;\n vector<int> ws(m);\n vector<lint> vs(m);\n for (int i = 0; i < m; ++i) {\n string s;\n cin >> s >> ws[i] >> vs[i];\n id[s] = i;\n }\n\n vector<pair<int, int>> pos(n);\n vector<vector<pair<int, lint>>> pss(n); // weight, value\n for (int i = 0; i < n; ++i) {\n int k;\n {\n auto& [x, y] = pos[i];\n cin >> k >> x >> y;\n }\n\n while (k--) {\n string s;\n lint v;\n cin >> s >> v;\n int j = id[s];\n\n v = vs[j] - v;\n if (v > 0) pss[i].emplace_back(ws[j], v);\n }\n }\n\n vector<pair<int, lint>> ps; // dist, value\n for (int b = 1; b < (1 << n); ++b) {\n vector<int> is;\n for (int i = 0; i < n; ++i) {\n if ((b >> i) & 1) is.push_back(i);\n }\n\n int dmin = INF;\n do {\n int px = 0, py = 0;\n\n int d = 0;\n for (auto i : is) {\n auto [x, y] = pos[i];\n d += abs(x - px) + abs(y - py);\n px = x, py = y;\n }\n\n d += abs(px) + abs(py);\n dmin = min(dmin, d);\n } while (next_permutation(is.begin(), is.end()));\n\n vector<lint> dp(wmax + 1, 0);\n for (auto i : is) {\n for (auto [w, v] : pss[i]) {\n for (int x = w; x <= wmax; ++x) {\n dp[x] = max(dp[x], dp[x - w] + v);\n }\n }\n }\n\n ps.emplace_back(dmin, dp.back());\n }\n\n vector<lint> dp(t + 1, 0);\n for (auto [d, v] : ps) {\n for (int x = d; x <= t; ++x) {\n dp[x] = max(dp[x], dp[x - d] + v);\n }\n }\n cout << dp.back() << \"\\n\";\n}\n\nint main() {\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n\n solve();\n\n return 0;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 3624, "score_of_the_acc": -0.4258, "final_rank": 8 }, { "submission_id": "aoj_2296_5130422", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define REP(i,n) for(int i=0;i<(n);i++)\n#define ALL(v) v.begin(),v.end()\n\n#define int long long\nstruct G{int w,v;};\ntemplate<typename T>\nvoid chmax(T &a,T b){\n if(a<b)a=b;\n}\ntemplate<typename T>\nvoid chmin(T &a,T b){\n if(a>b)a=b;\n}\n\nsigned main(){\n int n,m,W,T;cin>>n>>m>>W>>T;\n map<string,int> mp;\n vector<int> v(m),p(m);\n REP(i,m){\n string s;cin>>s>>v[i]>>p[i];\n mp[s]=i;\n }\n vector<int> x(n),y(n);\n vector<G> s[n];\n REP(i,n){\n int l;cin>>l>>x[i]>>y[i];\n REP(j,l){\n string tmp;int tmpv;\n cin>>tmp>>tmpv;\n if(p[mp[tmp]]>tmpv)\n s[i].push_back({v[mp[tmp]],p[mp[tmp]]-tmpv});\n }\n }\n vector<int> tme(1<<n,T+1),idx(n);\n tme[0]=0;\n REP(i,n)idx[i]=i;\n do{\n int visit=0,nowtime=0,nowy=0,nowx=0;\n REP(i,n){\n visit+=1<<idx[i];\n nowtime+=abs(nowy-y[idx[i]])+abs(nowx-x[idx[i]]);\n nowy=y[idx[i]],nowx=x[idx[i]];\n chmin(tme[visit],nowtime+abs(nowy)+abs(nowx));\n }\n }while(next_permutation(ALL(idx)));\n vector<int> va(T+1,0);\n REP(bit,1<<n){\n if(tme[bit]>T)continue;\n vector<int> a(W+1,0);\n REP(i,n)\n if((bit>>i)&1)\n for(auto g:s[i])\n REP(j,W-g.w+1)chmax(a[j+g.w],a[j]+g.v);\n chmax(va[tme[bit]],a[W]);\n }\n vector<int> ans(T+1,0);\n REP(i,T+1)\n REP(j,T+1-i)\n chmax(ans[i+j],ans[j]+va[i]);\n cout<<ans[T]<<endl;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 3344, "score_of_the_acc": -0.506, "final_rank": 9 }, { "submission_id": "aoj_2296_5130418", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define REP(i,n) for(int i=0;i<(n);i++)\n#define ALL(v) v.begin(),v.end()\n\nstruct G{int w,v;};\ntemplate<typename T>\nvoid chmax(T &a,T b){\n if(a<b)a=b;\n}\ntemplate<typename T>\nvoid chmin(T &a,T b){\n if(a>b)a=b;\n}\n\nsigned main(){\n int n,m,W,T;cin>>n>>m>>W>>T;\n map<string,int> mp;\n vector<int> v(m),p(m);\n REP(i,m){\n string s;cin>>s>>v[i]>>p[i];\n mp[s]=i;\n }\n vector<int> x(n),y(n);\n vector<G> s[n];\n REP(i,n){\n int l;cin>>l>>x[i]>>y[i];\n REP(j,l){\n string tmp;int tmpv;\n cin>>tmp>>tmpv;\n if(p[mp[tmp]]>tmpv)\n s[i].push_back({v[mp[tmp]],p[mp[tmp]]-tmpv});\n }\n }\n vector<int> tme(1<<n,T+1),idx(n);\n tme[0]=0;\n REP(i,n)idx[i]=i;\n do{\n int visit=0,nowtime=0,nowy=0,nowx=0;\n REP(i,n){\n visit+=1<<idx[i];\n nowtime+=abs(nowy-y[idx[i]])+abs(nowx-x[idx[i]]);\n nowy=y[idx[i]],nowx=x[idx[i]];\n chmin(tme[visit],nowtime+abs(nowy)+abs(nowx));\n }\n }while(next_permutation(ALL(idx)));\n vector<int> va(T+1,0);\n REP(bit,1<<n){\n if(tme[bit]>T)continue;\n vector<int> a(W+1,0);\n REP(i,n)\n if((bit>>i)&1)\n for(auto g:s[i])\n REP(j,W-g.w+1)chmax(a[j+g.w],a[j]+g.v);\n chmax(va[tme[bit]],a[W]);\n }\n vector<int> ans(T+1,0);\n REP(i,T+1)\n REP(j,T+1-i)\n chmax(ans[i+j],ans[j]+va[i]);\n cout<<ans[T]<<endl;\n}", "accuracy": 0.08333333333333333, "time_ms": 50, "memory_kb": 3260, "score_of_the_acc": -0.4, "final_rank": 17 }, { "submission_id": "aoj_2296_4970167", "code_snippet": "#include<iostream>\n#include<algorithm>\n#include<map>\n#include<vector>\nusing namespace std;\nint N,M,W,T;\nmap<string,int>sell,we;\nint X[7],Y[7];\nvector<pair<string,int> >buy[7];\nint dist[1<<7][7];\nlong ge[10101];\nlong w[1<<7][10101];\nmain()\n{\n\tcin>>N>>M>>W>>T;\n\tfor(int i=0;i<M;i++)\n\t{\n\t\tstring s;cin>>s;\n\t\tcin>>we[s]>>sell[s];\n\t}\n\tfor(int i=0;i<N;i++)\n\t{\n\t\tint L;cin>>L>>X[i]>>Y[i];\n\t\tfor(int j=0;j<L;j++)\n\t\t{\n\t\t\tstring t;int p;\n\t\t\tcin>>t>>p;\n\t\t\tbuy[i].push_back(make_pair(t,p));\n\t\t}\n\t}\n\tfor(int i=0;i<1<<N;i++)for(int j=0;j<N;j++)dist[i][j]=1e9;\n\tfor(int i=0;i<N;i++)dist[1<<i][i]=abs(X[i])+abs(Y[i]);\n\tfor(int i=1;i<1<<N;i++)for(int j=0;j<N;j++)if(i>>j&1)\n\t{\n\t\tfor(int k=0;k<N;k++)\n\t\t{\n\t\t\tdist[i|1<<k][k]=min(dist[i|1<<k][k],dist[i][j]+abs(X[j]-X[k])+abs(Y[j]-Y[k]));\n\t\t}\n\t}\n\tfor(int i=0;i<N;i++)\n\t{\n\t\tfor(int j=0;j<1<<i;j++)\n\t\t{\n\t\t\tint nj=j|1<<i;\n\t\t\tfor(int k=0;k<=W;k++)w[nj][k]=w[j][k];\n\t\t\tfor(pair<string,int>p:buy[i])\n\t\t\t{\n\t\t\t\tint wgh=we[p.first];\n\t\t\t\tint gt=sell[p.first]-p.second;\n\t\t\t\tfor(int k=0;k<=W-wgh;k++)w[nj][k+wgh]=max(w[nj][k+wgh],w[nj][k]+gt);\n\t\t\t}\n\t\t}\n\t}\n\tfor(int i=1;i<1<<N;i++)\n\t{\n\t\tint tm=1e9;\n\t\tfor(int j=0;j<N;j++)tm=min(tm,dist[i][j]+abs(X[j])+abs(Y[j]));\n\t\tlong B=w[i][W];\n\t\tfor(int j=0;j<=T-tm;j++)ge[j+tm]=max(ge[j+tm],ge[j]+B);\n\t}\n\tcout<<ge[T]<<endl;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 13292, "score_of_the_acc": -0.8123, "final_rank": 11 } ]
aoj_2297_cpp
H: Rectangular Stamps ICPC で良い成績を収めるには修行が欠かせない.うさぎは ICPC で勝ちたいので,今日も修行をすることにした. 今日の修行は,絵を描くことによって,創造力を高めようというものである.四角いスタンプを用いて,上手く模様を描こう. 大小さまざまなスタンプを使い,4 × 4 のマス目の紙に指定された赤・緑・青の通りの絵を完成させたい.スタンプは長方形であり,マス目にぴったり合わせて使う.スタンプの縦と横を入れ替えることはできない. 紙は最初色が塗られていない状態にある.紙にスタンプを押すと,押された部分がスタンプの色に変わり,下に隠れた色は全く見えなくなる.スタンプの色は付けるインクにより決定されるので,どのスタンプでも,好きな色を選ぶことが可能である.スタンプは紙から一部がはみ出た状態で押すことも可能であり,はみ出た部分は無視される. 1 つのスタンプを複数回使うことは可能である.同じスタンプを別の色に対して使ってもよい.スタンプを押すのはやや神経を使う作業なので,出来るだけスタンプを押す回数を少なくしたい. Input N H 1 W 1 ... H N W N C 1,1 C 1,2 C 1,3 C 1,4 C 2,1 C 2,2 C 2,3 C 2,4 C 3,1 C 3,2 C 3,3 C 3,4 C 4,1 C 4,2 C 4,3 C 4,4 N はスタンプの個数, H i , W i (1 ≤ i ≤ N ) はそれぞれ, i 番目のスタンプの縦の長さ,横の長さを表す整数である. C i , j (1 ≤ i ≤ 4,1 ≤ j ≤ 4) は,上から i 行目,左から j 列目のマスについて指定された絵の色を表す文字である.赤は R ,緑は G ,青は B で表される. 1 ≤ N ≤ 16,1 ≤ H i ≤ 4,1 ≤ W i ≤ 4 を満たす.( H i , W i ) として同一の組は複数回現れない. Output 絵を完成させるためにスタンプを押さなければならない最小の回数を 1 行に出力せよ. Sample Input 1 2 4 4 1 1 RRRR RRGR RBRR RRRR Sample Output 1 3 Sample Input 2 1 2 3 RRGG BRGG BRRR BRRR Sample Output 2 5
[ { "submission_id": "aoj_2297_9995285", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define all(v) (v).begin(),(v).end()\n#define pb(a) push_back(a)\n#define rep(i, n) for(int i=0;i<n;i++)\n#define foa(e, v) for(auto&& e : v)\nusing ll = long long;\nconst ll MOD7 = 1000000007, MOD998 = 998244353, INF = (1LL << 60);\n#define dout(a) cout<<fixed<<setprecision(10)<<a<<endl;\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n int n;\n cin >> n;\n vector<int> h(n), w(n);\n rep(i, n) cin >> h[i] >> w[i];\n vector<string> s(4);\n rep(i, 4) cin >> s[i];\n vector<vector<int>> col(4, vector(4, -1));\n map<char, int> mp;\n mp['R'] = 0;\n mp['B'] = 1;\n mp['G'] = 2;\n rep(i, 4) rep(j, 4) col[i][j] = mp[s[i][j]];\n int inf = 1 << 30;\n vector<int> dp(1 << 16, inf);\n dp[0] = 0;\n queue<int> que;\n que.push(0);\n while(!que.empty()) {\n int now = que.front();\n que.pop();\n rep(id, n) {\n for(int i = -h[id] + 1; i <= 3; i ++) {\n for(int j = -w[id] + 1; j <= 3; j ++) {\n rep(c, 3) {\n int nx = now;\n int Mini = max(i, 0);\n int Minj = max(j, 0);\n int Maxi = min(i + h[id] - 1, 3);\n int Maxj = min(j + w[id] - 1, 3);\n for(int ii = Mini; ii <= Maxi; ii ++) {\n for(int jj = Minj; jj <= Maxj; jj ++) {\n int idx = (ii * 4 + jj);\n if(col[ii][jj] == c) {\n if(nx >> idx & 1) {\n ;\n } else {\n nx ^= (1 << idx);\n }\n } else {\n if(nx >> idx & 1) {\n nx ^= (1 << idx);\n } else {\n ;\n }\n }\n }\n }\n if(dp[nx] > dp[now] + 1) {\n dp[nx] = dp[now] + 1;\n que.push(nx);\n }\n }\n }\n }\n }\n }\n cout << dp[(1 << 16) - 1] << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 660, "memory_kb": 3648, "score_of_the_acc": -0.3293, "final_rank": 8 }, { "submission_id": "aoj_2297_9995284", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define all(v) (v).begin(),(v).end()\n#define pb(a) push_back(a)\n#define rep(i, n) for(int i=0;i<n;i++)\n#define foa(e, v) for(auto&& e : v)\nusing ll = long long;\nconst ll MOD7 = 1000000007, MOD998 = 998244353, INF = (1LL << 60);\n#define dout(a) cout<<fixed<<setprecision(10)<<a<<endl;\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n int n;\n cin >> n;\n vector<int> h(n), w(n);\n rep(i, n) cin >> h[i] >> w[i];\n vector<string> s(4);\n rep(i, 4) cin >> s[i];\n vector<vector<int>> col(4, vector(4, -1));\n map<char, int> mp;\n mp['R'] = 0;\n mp['B'] = 1;\n mp['G'] = 2;\n rep(i, 4) rep(j, 4) col[i][j] = mp[s[i][j]];\n int inf = 1 << 30;\n vector<int> dp(1 << 16, inf);\n dp[0] = 0;\n queue<int> que;\n que.push(0);\n while(!que.empty()) {\n int now = que.front();\n que.pop();\n rep(id, n) {\n for(int i = -h[id] + 1; i <= 3; i ++) {\n for(int j = -w[id] + 1; j <= 3; j ++) {\n rep(c, 3) {\n int nx = now;\n int Mini = max(i, 0);\n int Minj = max(j, 0);\n int Maxi = min(i + h[id] - 1, 3);\n int Maxj = min(j + w[id] - 1, 3);\n for(int ii = Mini; ii <= Maxi; ii ++) {\n for(int jj = Minj; jj <= Maxj; jj ++) {\n int idx = (ii * 4 + jj);\n if(col[ii][jj] == c) {\n if(nx >> idx & 1) {\n ;\n } else {\n nx |= (1 << idx);\n }\n } else {\n if(nx >> idx & 1) {\n nx |= (1 << idx);\n } else {\n ;\n }\n }\n }\n }\n if(dp[nx] > dp[now] + 1) {\n dp[nx] = dp[now] + 1;\n que.push(nx);\n }\n }\n }\n }\n }\n }\n cout << dp[(1 << 16) - 1] << endl;\n return 0;\n}", "accuracy": 0.03278688524590164, "time_ms": 100, "memory_kb": 3316, "score_of_the_acc": -0.0266, "final_rank": 16 }, { "submission_id": "aoj_2297_9579170", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n#include <unistd.h>\n\nnamespace fastio {\nstatic constexpr uint32_t SZ = 1 << 17;\nchar ibuf[SZ];\nchar obuf[SZ];\nchar out[100];\n// pointer of ibuf, obuf\n\n\nuint32_t pil = 0, pir = 0, por = 0;\n\nstruct Pre {\n char num[10000][4];\n constexpr Pre() : num() {\n for (int i = 0; i < 10000; i++) {\n int n = i;\n for (int j = 3; j >= 0; j--) {\n num[i][j] = n % 10 | '0';\n n /= 10;\n }\n }\n }\n} constexpr pre;\n\ninline void load() {\n memmove(ibuf, ibuf + pil, pir - pil);\n pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin);\n pil = 0;\n if (pir < SZ)\n ibuf[pir++] = '\\n';\n}\n\ninline void flush() {\n fwrite(obuf, 1, por, stdout);\n por = 0;\n}\n\nvoid rd(char &c) {\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n}\n\nvoid rd(string &x) {\n x.clear();\n char c;\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n do {\n x += c;\n if (pil == pir)\n load();\n c = ibuf[pil++];\n } while (!isspace(c));\n}\n\ntemplate <typename T> void rd_real(T &x) {\n string s;\n rd(s);\n x = stod(s);\n}\n\ntemplate <typename T> void rd_integer(T &x) {\n if (pil + 100 > pir)\n load();\n char c;\n do\n c = ibuf[pil++];\n while (c < '-');\n bool minus = 0;\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (c == '-') {\n minus = 1, c = ibuf[pil++];\n }\n }\n x = 0;\n while ('0' <= c) {\n x = x * 10 + (c & 15), c = ibuf[pil++];\n }\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (minus)\n x = -x;\n }\n}\n\nvoid rd(int &x) {\n rd_integer(x);\n}\nvoid rd(ll &x) {\n rd_integer(x);\n}\nvoid rd(i128 &x) {\n rd_integer(x);\n}\nvoid rd(uint &x) {\n rd_integer(x);\n}\nvoid rd(ull &x) {\n rd_integer(x);\n}\nvoid rd(u128 &x) {\n rd_integer(x);\n}\nvoid rd(double &x) {\n rd_real(x);\n}\nvoid rd(long double &x) {\n rd_real(x);\n}\n\ntemplate <class T, class U> void rd(pair<T, U> &p) {\n return rd(p.first), rd(p.second);\n}\ntemplate <size_t N = 0, typename T> void rd_tuple(T &t) {\n if constexpr (N < std::tuple_size<T>::value) {\n auto &x = std::get<N>(t);\n rd(x);\n rd_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void rd(tuple<T...> &tpl) {\n rd_tuple(tpl);\n}\n\ntemplate <size_t N = 0, typename T> void rd(array<T, N> &x) {\n for (auto &d : x)\n rd(d);\n}\ntemplate <class T> void rd(vector<T> &x) {\n for (auto &d : x)\n rd(d);\n}\n\nvoid read() {}\ntemplate <class H, class... T> void read(H &h, T &...t) {\n rd(h), read(t...);\n}\n\nvoid wt(const char c) {\n if (por == SZ)\n flush();\n obuf[por++] = c;\n}\nvoid wt(const string s) {\n for (char c : s)\n wt(c);\n}\nvoid wt(const char *s) {\n size_t len = strlen(s);\n for (size_t i = 0; i < len; i++)\n wt(s[i]);\n}\n\ntemplate <typename T> void wt_integer(T x) {\n if (por > SZ - 100)\n flush();\n if (x < 0) {\n obuf[por++] = '-', x = -x;\n }\n int outi;\n for (outi = 96; x >= 10000; outi -= 4) {\n memcpy(out + outi, pre.num[x % 10000], 4);\n x /= 10000;\n }\n if (x >= 1000) {\n memcpy(obuf + por, pre.num[x], 4);\n por += 4;\n } else if (x >= 100) {\n memcpy(obuf + por, pre.num[x] + 1, 3);\n por += 3;\n } else if (x >= 10) {\n int q = (x * 103) >> 10;\n obuf[por] = q | '0';\n obuf[por + 1] = (x - q * 10) | '0';\n por += 2;\n } else\n obuf[por++] = x | '0';\n memcpy(obuf + por, out + outi + 4, 96 - outi);\n por += 96 - outi;\n}\n\ntemplate <typename T> void wt_real(T x) {\n ostringstream oss;\n oss << fixed << setprecision(15) << double(x);\n string s = oss.str();\n wt(s);\n}\n\nvoid wt(int x) {\n wt_integer(x);\n}\nvoid wt(ll x) {\n wt_integer(x);\n}\nvoid wt(i128 x) {\n wt_integer(x);\n}\nvoid wt(uint x) {\n wt_integer(x);\n}\nvoid wt(ull x) {\n wt_integer(x);\n}\nvoid wt(u128 x) {\n wt_integer(x);\n}\nvoid wt(double x) {\n wt_real(x);\n}\nvoid wt(long double x) {\n wt_real(x);\n}\n\ntemplate <class T, class U> void wt(const pair<T, U> val) {\n wt(val.first);\n wt(' ');\n wt(val.second);\n}\ntemplate <size_t N = 0, typename T> void wt_tuple(const T t) {\n if constexpr (N < std::tuple_size<T>::value) {\n if constexpr (N > 0) {\n wt(' ');\n }\n const auto x = std::get<N>(t);\n wt(x);\n wt_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void wt(tuple<T...> tpl) {\n wt_tuple(tpl);\n}\ntemplate <class T, size_t S> void wt(const array<T, S> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\ntemplate <class T> void wt(const vector<T> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\n\nvoid print() {\n wt('\\n');\n}\ntemplate <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {\n wt(head);\n if (sizeof...(Tail))\n wt(' ');\n print(forward<Tail>(tail)...);\n}\nvoid __attribute__((destructor)) _d() {\n flush();\n}\n} // namespace fastio\n\n\nusing fastio::flush;\nusing fastio::print;\nusing fastio::read;\n\ninline void first(bool i = true) {\n print(i ? \"first\" : \"second\");\n}\ninline void Alice(bool i = true) {\n print(i ? \"Alice\" : \"Bob\");\n}\ninline void Takahashi(bool i = true) {\n print(i ? \"Takahashi\" : \"Aoki\");\n}\ninline void yes(bool i = true) {\n print(i ? \"yes\" : \"no\");\n}\ninline void Yes(bool i = true) {\n print(i ? \"Yes\" : \"No\");\n}\ninline void No() {\n print(\"No\");\n}\ninline void YES(bool i = true) {\n print(i ? \"YES\" : \"NO\");\n}\ninline void NO() {\n print(\"NO\");\n}\ninline void Yay(bool i = true) {\n print(i ? \"Yay!\" : \":(\");\n}\ninline void Possible(bool i = true) {\n print(i ? \"Possible\" : \"Impossible\");\n}\ninline void POSSIBLE(bool i = true) {\n print(i ? \"POSSIBLE\" : \"IMPOSSIBLE\");\n}\n\n/**\n * @brief Fast IO\n */\n\nstruct EnumCliques{\n const int n;\n int m;\n vector<vector<int>> g;\n vector<int> deg;\n EnumCliques(int _n):n(_n),m(0),g(n,vector<int>(n)),deg(n){}\n void add_edge(int u,int v){\n g[u][v]=g[v][u]=1;\n deg[u]++; deg[v]++;\n m++;\n }\n vector<vector<int>> run(){\n int L=sqrt(m);\n vector<vector<int>> res;\n auto process=[&](vector<int>& vs,bool fix=true)->void{\n vector<int> nbhd(vs.size());\n rep(i,0,vs.size())rep(j,0,vs.size())if(i!=j){\n nbhd[i]|=(!g[vs[i]][vs[j]])<<j;\n }\n rep(mask,1,1<<vs.size()){\n if(fix and (mask&1)==0)continue;;\n bool check=1;\n rep(i,0,vs.size())if(mask>>i&1){\n if(mask&nbhd[i]){\n check=0;\n break;\n }\n }\n if(check){\n vector<int> add;\n rep(i,0,vs.size())if(mask>>i&1){\n add.push_back(vs[i]);\n }\n res.push_back(add);\n }\n }\n };\n\n vector<int> used(n);\n queue<int> que;\n rep(v,0,n)if(deg[v]<L){\n que.push(v);\n used[v]=1;\n }\n while(!que.empty()){\n int v=que.front();\n que.pop();\n vector<int> vs;\n vs.push_back(v);\n rep(u,0,n)if(g[v][u])vs.push_back(u);\n process(vs);\n rep(u,0,n)if(g[v][u]){\n g[v][u]=g[u][v]=0;\n deg[u]--;\n if(!used[u] and deg[u]<L){\n que.push(u);\n used[u]=1;\n }\n }\n }\n vector<int> vs;\n rep(v,0,n)if(!used[v])vs.push_back(v);\n process(vs,false);\n return res;\n }\n};\n\n/**\n * @brief Enumerate Cliques\n */\n\nint main() {\n int N;\n cin >> N;\n vector<int> A(N), B(N);\n rep(i,0,N) cin >> A[i] >> B[i];\n char C[4][4];\n rep(i,0,4) rep(j,0,4) cin >> C[i][j];\n string S = \"RGB\";\n vector D(N, vector(3, vector(7, vector<int>(7,0))));\n vector X(N, vector(3, vector(7, vector<int>(7,0))));\n rep(i,0,N) {\n rep(j,0,3) {\n char T = S[j];\n rep(k,-3,4) {\n rep(l,-3,4) {\n rep(a,0,A[i]) {\n rep(b,0,B[i]) {\n int x = a+k, y = b+l;\n if (0<=x&&x<4&&0<=y&&y<4) {\n if (C[x][y] != T) D[i][j][k+3][l+3] |= (1<<(x*4+y));\n X[i][j][k+3][l+3] |= (1<<(x*4+y));\n }\n }\n }\n }\n }\n }\n }\n vector<int> DP(1<<16,inf);\n DP[0] = 0;\n rep(i,0,1<<16) {\n if (DP[i] == inf) continue;\n bool BB[4][4] = {};\n rep(j,0,16) {\n if (i & (1<<j)) BB[j/4][j%4] = true;\n }\n rep(j,0,N) {\n rep(a,0,3) {\n char T = S[a];\n rep(b,0,7) {\n rep(c,0,7) {\n if ((i & D[j][a][b][c]) == D[j][a][b][c]) chmin(DP[i|X[j][a][b][c]],DP[i]+1);\n }\n }\n }\n }\n }\n cout << DP[(1<<16)-1] << endl;\n}", "accuracy": 1, "time_ms": 210, "memory_kb": 3548, "score_of_the_acc": -0.0885, "final_rank": 2 }, { "submission_id": "aoj_2297_9361542", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#if __has_include(<atcoder/all>)\n#include <atcoder/all>\nusing namespace atcoder;\ntemplate<int mod>istream &operator>>(istream &is,static_modint<mod> &a){long long b;is>>b;a=b;return is;}\nistream &operator>>(istream &is,modint &a){long long b;cin>>b;a=b;return is;}\n#endif\n#ifdef LOCAL\n#include \"debug.h\"\n#else\n#define debug(...) static_cast<void>(0)\n#define debugg(...) static_cast<void>(0)\n#define esper(...) static_cast<void>(0)\ntemplate<typename T1,typename T2>ostream &operator<<(ostream &os,const pair<T1,T2>&p){os<<p.first<<' '<<p.second;return os;}\n#endif\nusing ll=long long;\nusing ull=unsigned long long;\nusing P=pair<ll,ll>;\ntemplate<typename T>using minque=priority_queue<T,vector<T>,greater<T>>;\ntemplate<typename T>bool chmax(T &a,const T &b){return (a<b?(a=b,true):false);}\ntemplate<typename T>bool chmin(T &a,const T &b){return (a>b?(a=b,true):false);}\ntemplate<typename T1,typename T2>istream &operator>>(istream &is,pair<T1,T2>&p){is>>p.first>>p.second;return is;}\ntemplate<typename T>istream &operator>>(istream &is,vector<T> &a){for(auto &i:a)is>>i;return is;}\ntemplate<typename T1,typename T2>void operator++(pair<T1,T2>&a,int n){a.first++,a.second++;}\ntemplate<typename T1,typename T2>void operator--(pair<T1,T2>&a,int n){a.first--,a.second--;}\ntemplate<typename T>void operator++(vector<T>&a,int n){for(auto &i:a)i++;}\ntemplate<typename T>void operator--(vector<T>&a,int n){for(auto &i:a)i--;}\n#define reps(i,a,n) for(int i=(a);i<int(n);i++)\n#define rep(i,n) reps(i,0,n)\n#define all(x) x.begin(),x.end()\n#define pcnt(x) __builtin_popcountll(x)\n#define fin(x) return cout<<x<<'\\n',static_cast<void>(0)\nll myceil(ll a,ll b){return (a+b-1)/b;}\ntemplate<typename T,size_t n,size_t id=0>\nauto vec(const int (&d)[n],const T &init=T()){\n if constexpr (id<n)return vector(d[id],vec<T,n,id+1>(d,init));\n else return init;\n}\nvoid SOLVE();\nint main(){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout<<fixed<<setprecision(16);\n #ifdef LOCAL\n clock_t start=clock();\n #endif\n int testcase=1;\n //cin>>testcase;\n for(int i=0;i<testcase;i++){\n SOLVE();\n }\n #ifdef LOCAL\n cerr<<\"time:\";\n cerr<<(clock()-start)/1000;\n cerr<<\"ms\\n\";\n #endif\n}\nvoid SOLVE(){\n int n;\n cin>>n;\n vector<pair<int,int>>a(n);\n cin>>a;\n vector<vector<int>>b(4,vector<int>(4));\n rep(i,4)rep(j,4){\n char c;\n cin>>c;\n if(c=='R')b[i][j]=0;\n else if(c=='G')b[i][j]=1;\n else b[i][j]=2;\n }\n vector<int>dp(1<<16,0);\n for(int i=(1<<16)-1;i--;){\n dp[i]=1e9;\n rep(j,n){\n reps(dx,-a[j].first+1,4)reps(dy,-a[j].second+1,4){\n int nx=i;\n int st=-1;\n rep(x,a[j].first)rep(y,a[j].second){\n if(x+dx>=0&&y+dy>=0&&x+dx<4&&y+dy<4){\n if(i>>((x+dx)*4+y+dy)&1)continue;\n if(st==-1)st=b[x+dx][y+dy];\n else if(st!=b[x+dx][y+dy])st=-2;\n nx|=1<<((x+dx)*4+y+dy);\n }\n }\n if(st!=-2)chmin(dp[i],dp[nx]+1);\n }\n }\n }\n cout<<dp[0]<<endl;\n}", "accuracy": 1, "time_ms": 780, "memory_kb": 3476, "score_of_the_acc": -0.3906, "final_rank": 9 }, { "submission_id": "aoj_2297_9346755", "code_snippet": "#include <bits/stdc++.h>\n\nint main() {\n std::cin.tie(nullptr)->sync_with_stdio(false);\n\n int N;\n std::cin >> N;\n std::vector<int> H(N), W(N);\n for (int i{} ; i < N ; i++) {\n std::cin >> H[i] >> W[i];\n }\n char input[4][4];\n int C[4][4];\n for (int i{} ; i < 4 ; i++) {\n for (int j{} ; j < 4 ; j++) {\n std::cin >> input[i][j];\n if (input[i][j] == 'R') C[i][j] = 0;\n else if (input[i][j] == 'G') C[i][j] = 1;\n else if (input[i][j] == 'B') C[i][j] = 2;\n else assert(false);\n }\n }\n std::vector<int> dp((1 << 16), -1);\n auto in{[&](int x, int y) -> bool {\n return 0 <= x and x < 4 and 0 <= y and y < 4;\n }};\n auto f{[&](int x, int y) -> int {\n return x * 4 + y;\n }};\n const int INF{(int)1e9};\n auto rec{[&](auto rec, int bit) -> int {\n if (dp[bit] != -1) return dp[bit];\n if (bit == (1 << 16) - 1) return dp[bit] = 0;\n dp[bit] = INF;\n for (int i{} ; i < N ; i++) {\n for (int u{-H[i] + 1} ; u < 4 ; u++) {\n for (int l{-W[i] + 1} ; l < 4 ; l++) {\n int app{};\n int mask{bit};\n for (int x{std::max(u, 0)} ; x < std::min(4, u + H[i]) ; x++) {\n for (int y{std::max(l, 0)} ; y < std::min(4, l + W[i]) ; y++) {\n int val{f(x, y)};\n if (bit & (1 << val)) continue;\n app |= (1 << C[x][y]);\n mask |= (1 << val);\n }\n }\n if (__builtin_popcount(app) == 1) {\n dp[bit] = std::min(dp[bit], rec(rec, mask) + 1);\n }\n }\n }\n }\n return dp[bit];\n }};\n std::cout << rec(rec, 0) << '\\n';\n}", "accuracy": 1, "time_ms": 460, "memory_kb": 3464, "score_of_the_acc": -0.2203, "final_rank": 3 }, { "submission_id": "aoj_2297_9004351", "code_snippet": "#line 2 \"cp-library/src/cp-template.hpp\"\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nusing i32 = int;\nusing u32 = unsigned int;\nusing i64 = long long;\nusing u64 = unsigned long long;\nusing i128 = __int128_t;\ntemplate < class T > bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }\ntemplate < class T > bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }\ntemplate < class T, class U > T ceil (T x, U y) { return (x > 0 ? (x + y - 1) / y : x / y); }\ntemplate < class T, class U > T floor(T x, U y) { return (x > 0 ? x / y : (x - y + 1) / y); }\nint popcnt(i32 x) { return __builtin_popcount(x); }\nint popcnt(u32 x) { return __builtin_popcount(x); }\nint popcnt(i64 x) { return __builtin_popcountll(x); }\nint popcnt(u64 x) { return __builtin_popcountll(x); }\n\n#line 2 \"cp-library/src/utility/rep_itr.hpp\"\ntemplate < class T > struct itr_rep {\n T i, d;\n constexpr itr_rep(const T i) noexcept : i(i), d(1) {}\n constexpr itr_rep(const T i, const T d) noexcept : i(i), d(d) {}\n void operator++() noexcept { i += d; }\n constexpr int operator*() const noexcept { return i; }\n constexpr bool operator!=(const itr_rep x) const noexcept { return d > 0 ? i < x.i : i > x.i; }\n};\n\ntemplate < class T > struct rep {\n const itr_rep< T > s, t;\n constexpr rep(const T t) noexcept : s(0), t(t) {}\n constexpr rep(const T s, const T t) noexcept : s(s), t(t) {}\n constexpr rep(const T s, const T t, const T d) noexcept : s(s, d), t(t, d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n\ntemplate < class T > struct revrep {\n const itr_rep < T > s, t;\n constexpr revrep(const T t) noexcept : s(t - 1, -1), t(-1, -1) {}\n constexpr revrep(const T s, const T t) noexcept : s(t - 1, -1), t(s - 1, -1) {}\n constexpr revrep(const T s, const T t, const T d) noexcept : s(t - 1, -d), t(s - 1, -d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n#line 3 \"cp-library/src/utility/io.hpp\"\n\n/* 128bit integer */\nistream& operator>>(istream& is, i128& x) {\n std::string s; is >> s;\n int pm = (s[0] == '-');\n x = 0;\n for(int i : rep(pm, int(s.size()))) x = x * 10 + (s[i] - '0');\n if(pm) x *= -1;\n return is;\n}\nostream& operator<<(ostream& os, const i128& x) {\n if(x == 0) return os << '0';\n i128 y = x;\n if(y < 0) { os << '-'; y *= -1; }\n std::vector<int> ny;\n while(y > 0) { ny.push_back(y % 10); y /= 10; }\n for(int i : revrep(ny.size())) os << ny[i];\n return os;\n}\n\ntemplate < class S, class T > istream& operator>>(istream& is, std::pair< S, T >& x) { is >> x.first >> x.second; return is; }\ntemplate < class S, class T > ostream& operator<<(ostream& os, const std::pair< S, T >& x) { os << x.first << \" \" << x.second; return os; }\n\nnamespace scanner {\n struct sca {\n template < class T > operator T() {\n T s; std::cin >> s; return s;\n }\n };\n struct vec {\n int n;\n vec(int n) : n(n) {}\n template < class T > operator std::vector< T >() {\n std::vector< T > v(n);\n for(T& x : v) std::cin >> x;\n return v;\n }\n };\n struct mat {\n int h, w;\n mat(int h, int w) : h(h), w(w) {}\n template < class T > operator std::vector< std::vector< T > >() {\n std::vector m(h, std::vector< T >(w));\n for(std::vector< T >& v : m) for(T& x : v) std::cin >> x;\n return m;\n }\n };\n struct speedup {\n speedup() {\n std::cin.tie(0);\n std::ios::sync_with_stdio(0);\n }\n } speedup_instance;\n}\nscanner::sca in() { return scanner::sca(); }\nscanner::vec in(int n) { return scanner::vec(n); }\nscanner::mat in(int h, int w) { return scanner::mat(h, w); }\n\nnamespace printer {\n void precision(int d) { std::cout << std::fixed << std::setprecision(d); }\n void flush() { std::cout.flush(); }\n}\n\ntemplate < class T >\nostream& operator<<(ostream& os, const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) { os << a[i]; if(i != n - 1) os << ' '; }\n return os;\n}\n\nint print() { std::cout << '\\n'; return 0; }\ntemplate < class head, class... tail > int print(head&& h, tail&&... t) {\n std::cout << h; if(sizeof...(tail)) std::cout << ' ';\n return print(std::forward<tail>(t)...);\n}\ntemplate < class T > int print_n(const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) std::cout << a[i] << \"\\n\";\n return 0;\n}\n\n\n#line 2 \"cp-library/src/utility/key_val.hpp\"\n\ntemplate < class K, class V >\nstruct key_val {\n K key; V val;\n key_val() {}\n key_val(K key, V val) : key(key), val(val) {}\n template < std::size_t Index >\n std::tuple_element_t< Index, key_val >& get() {\n if constexpr (Index == 0) return key;\n if constexpr (Index == 1) return val;\n }\n};\n\nnamespace std {\n\ntemplate < class K, class V > struct tuple_size < key_val< K, V > > : integral_constant< size_t, 2 > {};\ntemplate < class K, class V > struct tuple_element < 0, key_val< K, V > > { using type = K; };\ntemplate < class K, class V > struct tuple_element < 1, key_val< K, V > > { using type = V; };\n\n}\n#line 2 \"cp-library/src/utility/vec_op.hpp\"\ntemplate < class T > key_val< int, T > max_of(const vector< T >& a) {\n int i = std::max_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class T > key_val< int, T > min_of(const vector< T >& a) {\n int i = std::min_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class S, class T > S sum_of(const vector< T >& a) {\n S sum = 0;\n for(const T x : a) sum += x;\n return sum;\n}\ntemplate < class S, class T > vector< S > freq_of(const vector< T >& a, T L, T R) {\n vector< S > res(R - L, S(0));\n for(const T x : a) res[x - L] += 1;\n return res;\n}\ntemplate < class S, class T > struct prefix_sum {\n vector< S > s;\n prefix_sum(const vector< T >& a) : s(a) {\n s.insert(s.begin(), S(0));\n for(int i : rep(a.size())) s[i + 1] += s[i];\n }\n // [L, R)\n S sum(int L, int R) { return s[R] - s[L]; }\n};\n#line 3 \"cp-library/src/utility/heap.hpp\"\n\ntemplate < class T > using heap_min = std::priority_queue< T, std::vector< T >, std::greater< T > >;\ntemplate < class T > using heap_max = std::priority_queue< T, std::vector< T >, std::less< T > >;\n\n#line 27 \"cp-library/src/cp-template.hpp\"\n\n#line 1 \"cp-library/src/algorithm/bin_search.hpp\"\ntemplate < class T, class F >\nT bin_search(T ok, T ng, F f) {\n while(abs(ng - ok) > 1) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n\ntemplate < class T, class F >\nT bin_search_real(T ok, T ng, F f, int step = 80) {\n while(step--) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n#line 2 \"cp-library/src/algorithm/argsort.hpp\"\n\ntemplate < class T > std::vector< int > argsort(const std::vector< T > &a) {\n std::vector< int > ids((int)a.size());\n std::iota(ids.begin(), ids.end(), 0);\n std::sort(ids.begin(), ids.end(), [&](int i, int j) {\n return a[i] < a[j] || (a[i] == a[j] && i < j);\n });\n return ids;\n}\n#line 1 \"macro.hpp\"\nnamespace macro {\n\nusing size_type = int;\ntemplate < class container > void sort(container& a) { std::sort(std:: begin(a), std:: end(a)); }\ntemplate < class container > void rsort(container& a) { std::sort(std::rbegin(a), std::rend(a)); }\ntemplate < class container > void reverse(container& a) { std::reverse(std::begin(a), std::end(a)); }\ntemplate < class container > void unique(container& a) {\n std::sort(std::begin(a), std::end(a));\n a.erase(std::unique(std::begin(a), std::end(a)), std::end(a));\n}\ntemplate < class container > container sorted(const container& a) { container b = a; sort(b); return std::move(b); }\ntemplate < class container > container rsorted(const container& a) { container b = a; rsort(b); return std::move(b); }\ntemplate < class container, class compare > void sort(container& a, const compare& cmp) { std::sort(std::begin(a), std::end(a), cmp); }\ntemplate < class container, class compare > container sorted(const container& a, const compare& cmp) { container b = a; sort(b, cmp); return std::move(b); }\ntemplate < class container, class value > size_type lower_bound(const container& a, const value& x) { return std::lower_bound(std::begin(a), std::end(a), x) - std::begin(a); }\ntemplate < class container, class value > size_type upper_bound(const container& a, const value& x) { return std::upper_bound(std::begin(a), std::end(a), x) - std::begin(a); }\n\nconst std::vector<std::pair<size_type, size_type>> dir4 = { {+1, 0}, {-1, 0}, { 0, +1}, { 0, -1} };\nconst std::vector<std::pair<size_type, size_type>> dir8 = { {-1, -1}, {-1, 0}, {-1, +1}, { 0, -1}, { 0, +1}, {+1, -1}, {+1, 0}, {+1, +1} };\n\n#ifdef _DEBUG\n#define debug(x) std::cout << \"[\" << __LINE__ << \"] \" << #x << \": \" << x << std::endl\n#else\n#define debug(x)\n#endif\n\ntemplate < class container > void concat(container& a, const container& b) {\n a.insert(std::end(a), std::begin(b), std::end(b));\n}\nstd::vector<size_type> iota(const size_type n) {\n std::vector<size_type> I(n);\n std::iota(std::begin(I), std::end(I), 0);\n return I;\n}\ntemplate < class container > std::vector<size_type> sort_idx(const container& a) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return a[i] < a[j] or (a[i] == a[j] and i < j); });\n return I;\n}\ntemplate < class container, class compare > std::vector<size_type> sort_idx(const container& a, const compare& cmp) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return cmp(a[i], a[j]) or (a[i] == a[j] and i < j); });\n return std::move(I);\n}\n\nstruct grid {\n using size_type = int;\n size_type H, W;\n grid(const size_type H, const size_type W) : H(H), W(W) {}\n bool contains(const size_type i, const size_type j) {\n return 0 <= i and i < H and 0 <= j and j < W;\n }\n};\n\nusing f64 = long double;\n\n} // namespace macro\n\nusing namespace macro;\n#line 3 \"A.cpp\"\n\nint main() {\n int N = in();\n vector<int> H(N), W(N);\n for(int i : rep(N)) H[i] = in(), W[i] = in();\n vector<string> C = in(4);\n vector<int> dist(1 << 16, -1);\n dist[0] = 0;\n queue<int> q;\n q.push(0);\n while(not q.empty()) {\n int u = q.front(); q.pop();\n for(int i = 0; i < N; i++) {\n for(int dx = 1 - H[i]; dx <= +3; dx++) {\n for(int dy = 1 - W[i]; dy <= +3; dy++) {\n for(char c : {'R', 'G', 'B'}) {\n int v = u;\n for(int x = max(0, dx); x < min(4, H[i] + dx); x++) {\n for(int y = max(0, dy); y < min(4, W[i] + dy); y++) {\n int p = 1 << (x * 4 + y);\n if(C[x][y] == c) {\n v |= p;\n } else {\n v &= ~p;\n }\n }\n }\n if(dist[v] == -1) {\n dist[v] = dist[u] + 1;\n q.push(v);\n }\n }\n }\n }\n }\n }\n print(dist[(1 << 16) - 1]);\n}", "accuracy": 1, "time_ms": 1210, "memory_kb": 3628, "score_of_the_acc": -0.6216, "final_rank": 11 }, { "submission_id": "aoj_2297_9004350", "code_snippet": "#line 2 \"cp-library/src/cp-template.hpp\"\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nusing i32 = int;\nusing u32 = unsigned int;\nusing i64 = long long;\nusing u64 = unsigned long long;\nusing i128 = __int128_t;\ntemplate < class T > bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }\ntemplate < class T > bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }\ntemplate < class T, class U > T ceil (T x, U y) { return (x > 0 ? (x + y - 1) / y : x / y); }\ntemplate < class T, class U > T floor(T x, U y) { return (x > 0 ? x / y : (x - y + 1) / y); }\nint popcnt(i32 x) { return __builtin_popcount(x); }\nint popcnt(u32 x) { return __builtin_popcount(x); }\nint popcnt(i64 x) { return __builtin_popcountll(x); }\nint popcnt(u64 x) { return __builtin_popcountll(x); }\n\n#line 2 \"cp-library/src/utility/rep_itr.hpp\"\ntemplate < class T > struct itr_rep {\n T i, d;\n constexpr itr_rep(const T i) noexcept : i(i), d(1) {}\n constexpr itr_rep(const T i, const T d) noexcept : i(i), d(d) {}\n void operator++() noexcept { i += d; }\n constexpr int operator*() const noexcept { return i; }\n constexpr bool operator!=(const itr_rep x) const noexcept { return d > 0 ? i < x.i : i > x.i; }\n};\n\ntemplate < class T > struct rep {\n const itr_rep< T > s, t;\n constexpr rep(const T t) noexcept : s(0), t(t) {}\n constexpr rep(const T s, const T t) noexcept : s(s), t(t) {}\n constexpr rep(const T s, const T t, const T d) noexcept : s(s, d), t(t, d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n\ntemplate < class T > struct revrep {\n const itr_rep < T > s, t;\n constexpr revrep(const T t) noexcept : s(t - 1, -1), t(-1, -1) {}\n constexpr revrep(const T s, const T t) noexcept : s(t - 1, -1), t(s - 1, -1) {}\n constexpr revrep(const T s, const T t, const T d) noexcept : s(t - 1, -d), t(s - 1, -d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n#line 3 \"cp-library/src/utility/io.hpp\"\n\n/* 128bit integer */\nistream& operator>>(istream& is, i128& x) {\n std::string s; is >> s;\n int pm = (s[0] == '-');\n x = 0;\n for(int i : rep(pm, int(s.size()))) x = x * 10 + (s[i] - '0');\n if(pm) x *= -1;\n return is;\n}\nostream& operator<<(ostream& os, const i128& x) {\n if(x == 0) return os << '0';\n i128 y = x;\n if(y < 0) { os << '-'; y *= -1; }\n std::vector<int> ny;\n while(y > 0) { ny.push_back(y % 10); y /= 10; }\n for(int i : revrep(ny.size())) os << ny[i];\n return os;\n}\n\ntemplate < class S, class T > istream& operator>>(istream& is, std::pair< S, T >& x) { is >> x.first >> x.second; return is; }\ntemplate < class S, class T > ostream& operator<<(ostream& os, const std::pair< S, T >& x) { os << x.first << \" \" << x.second; return os; }\n\nnamespace scanner {\n struct sca {\n template < class T > operator T() {\n T s; std::cin >> s; return s;\n }\n };\n struct vec {\n int n;\n vec(int n) : n(n) {}\n template < class T > operator std::vector< T >() {\n std::vector< T > v(n);\n for(T& x : v) std::cin >> x;\n return v;\n }\n };\n struct mat {\n int h, w;\n mat(int h, int w) : h(h), w(w) {}\n template < class T > operator std::vector< std::vector< T > >() {\n std::vector m(h, std::vector< T >(w));\n for(std::vector< T >& v : m) for(T& x : v) std::cin >> x;\n return m;\n }\n };\n struct speedup {\n speedup() {\n std::cin.tie(0);\n std::ios::sync_with_stdio(0);\n }\n } speedup_instance;\n}\nscanner::sca in() { return scanner::sca(); }\nscanner::vec in(int n) { return scanner::vec(n); }\nscanner::mat in(int h, int w) { return scanner::mat(h, w); }\n\nnamespace printer {\n void precision(int d) { std::cout << std::fixed << std::setprecision(d); }\n void flush() { std::cout.flush(); }\n}\n\ntemplate < class T >\nostream& operator<<(ostream& os, const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) { os << a[i]; if(i != n - 1) os << ' '; }\n return os;\n}\n\nint print() { std::cout << '\\n'; return 0; }\ntemplate < class head, class... tail > int print(head&& h, tail&&... t) {\n std::cout << h; if(sizeof...(tail)) std::cout << ' ';\n return print(std::forward<tail>(t)...);\n}\ntemplate < class T > int print_n(const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) std::cout << a[i] << \"\\n\";\n return 0;\n}\n\n\n#line 2 \"cp-library/src/utility/key_val.hpp\"\n\ntemplate < class K, class V >\nstruct key_val {\n K key; V val;\n key_val() {}\n key_val(K key, V val) : key(key), val(val) {}\n template < std::size_t Index >\n std::tuple_element_t< Index, key_val >& get() {\n if constexpr (Index == 0) return key;\n if constexpr (Index == 1) return val;\n }\n};\n\nnamespace std {\n\ntemplate < class K, class V > struct tuple_size < key_val< K, V > > : integral_constant< size_t, 2 > {};\ntemplate < class K, class V > struct tuple_element < 0, key_val< K, V > > { using type = K; };\ntemplate < class K, class V > struct tuple_element < 1, key_val< K, V > > { using type = V; };\n\n}\n#line 2 \"cp-library/src/utility/vec_op.hpp\"\ntemplate < class T > key_val< int, T > max_of(const vector< T >& a) {\n int i = std::max_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class T > key_val< int, T > min_of(const vector< T >& a) {\n int i = std::min_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class S, class T > S sum_of(const vector< T >& a) {\n S sum = 0;\n for(const T x : a) sum += x;\n return sum;\n}\ntemplate < class S, class T > vector< S > freq_of(const vector< T >& a, T L, T R) {\n vector< S > res(R - L, S(0));\n for(const T x : a) res[x - L] += 1;\n return res;\n}\ntemplate < class S, class T > struct prefix_sum {\n vector< S > s;\n prefix_sum(const vector< T >& a) : s(a) {\n s.insert(s.begin(), S(0));\n for(int i : rep(a.size())) s[i + 1] += s[i];\n }\n // [L, R)\n S sum(int L, int R) { return s[R] - s[L]; }\n};\n#line 3 \"cp-library/src/utility/heap.hpp\"\n\ntemplate < class T > using heap_min = std::priority_queue< T, std::vector< T >, std::greater< T > >;\ntemplate < class T > using heap_max = std::priority_queue< T, std::vector< T >, std::less< T > >;\n\n#line 27 \"cp-library/src/cp-template.hpp\"\n\n#line 1 \"cp-library/src/algorithm/bin_search.hpp\"\ntemplate < class T, class F >\nT bin_search(T ok, T ng, F f) {\n while(abs(ng - ok) > 1) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n\ntemplate < class T, class F >\nT bin_search_real(T ok, T ng, F f, int step = 80) {\n while(step--) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n#line 2 \"cp-library/src/algorithm/argsort.hpp\"\n\ntemplate < class T > std::vector< int > argsort(const std::vector< T > &a) {\n std::vector< int > ids((int)a.size());\n std::iota(ids.begin(), ids.end(), 0);\n std::sort(ids.begin(), ids.end(), [&](int i, int j) {\n return a[i] < a[j] || (a[i] == a[j] && i < j);\n });\n return ids;\n}\n#line 1 \"macro.hpp\"\nnamespace macro {\n\nusing size_type = int;\ntemplate < class container > void sort(container& a) { std::sort(std:: begin(a), std:: end(a)); }\ntemplate < class container > void rsort(container& a) { std::sort(std::rbegin(a), std::rend(a)); }\ntemplate < class container > void reverse(container& a) { std::reverse(std::begin(a), std::end(a)); }\ntemplate < class container > void unique(container& a) {\n std::sort(std::begin(a), std::end(a));\n a.erase(std::unique(std::begin(a), std::end(a)), std::end(a));\n}\ntemplate < class container > container sorted(const container& a) { container b = a; sort(b); return std::move(b); }\ntemplate < class container > container rsorted(const container& a) { container b = a; rsort(b); return std::move(b); }\ntemplate < class container, class compare > void sort(container& a, const compare& cmp) { std::sort(std::begin(a), std::end(a), cmp); }\ntemplate < class container, class compare > container sorted(const container& a, const compare& cmp) { container b = a; sort(b, cmp); return std::move(b); }\ntemplate < class container, class value > size_type lower_bound(const container& a, const value& x) { return std::lower_bound(std::begin(a), std::end(a), x) - std::begin(a); }\ntemplate < class container, class value > size_type upper_bound(const container& a, const value& x) { return std::upper_bound(std::begin(a), std::end(a), x) - std::begin(a); }\n\nconst std::vector<std::pair<size_type, size_type>> dir4 = { {+1, 0}, {-1, 0}, { 0, +1}, { 0, -1} };\nconst std::vector<std::pair<size_type, size_type>> dir8 = { {-1, -1}, {-1, 0}, {-1, +1}, { 0, -1}, { 0, +1}, {+1, -1}, {+1, 0}, {+1, +1} };\n\n#ifdef _DEBUG\n#define debug(x) std::cout << \"[\" << __LINE__ << \"] \" << #x << \": \" << x << std::endl\n#else\n#define debug(x)\n#endif\n\ntemplate < class container > void concat(container& a, const container& b) {\n a.insert(std::end(a), std::begin(b), std::end(b));\n}\nstd::vector<size_type> iota(const size_type n) {\n std::vector<size_type> I(n);\n std::iota(std::begin(I), std::end(I), 0);\n return I;\n}\ntemplate < class container > std::vector<size_type> sort_idx(const container& a) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return a[i] < a[j] or (a[i] == a[j] and i < j); });\n return I;\n}\ntemplate < class container, class compare > std::vector<size_type> sort_idx(const container& a, const compare& cmp) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return cmp(a[i], a[j]) or (a[i] == a[j] and i < j); });\n return std::move(I);\n}\n\nstruct grid {\n using size_type = int;\n size_type H, W;\n grid(const size_type H, const size_type W) : H(H), W(W) {}\n bool contains(const size_type i, const size_type j) {\n return 0 <= i and i < H and 0 <= j and j < W;\n }\n};\n\nusing f64 = long double;\n\n} // namespace macro\n\nusing namespace macro;\n#line 3 \"A.cpp\"\n\nint main() {\n int N = in();\n vector<int> H(N), W(N);\n for(int i : rep(N)) H[i] = in(), W[i] = in();\n vector<string> C = in(4);\n vector<int> dist(1 << 16, -1);\n dist[0] = 0;\n queue<int> q;\n q.push(0);\n while(not q.empty()) {\n int u = q.front(); q.pop();\n if(u == (1 << 16) - 1) break;\n for(int i = 0; i < N; i++) {\n for(int dx = 1 - H[i]; dx <= +3; dx++) {\n for(int dy = 1 - W[i]; dy <= +3; dy++) {\n for(char c : {'R', 'G', 'B'}) {\n int v = u;\n for(int x = max(0, dx); x < min(4, H[i] + dx); x++) {\n for(int y = max(0, dy); y < min(4, W[i] + dy); y++) {\n int p = 1 << (x * 4 + y);\n if(C[x][y] == c) {\n v |= p;\n } else {\n v &= ~p;\n }\n }\n }\n if(dist[v] == -1) {\n dist[v] = dist[u] + 1;\n q.push(v);\n }\n }\n }\n }\n }\n }\n print(dist[(1 << 16) - 1]);\n}", "accuracy": 1, "time_ms": 550, "memory_kb": 3632, "score_of_the_acc": -0.2706, "final_rank": 7 }, { "submission_id": "aoj_2297_8305259", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\ntemplate<class T>\nbool chmax(T &a, const T &b) {if (a < b) {a = b; return 1;} return 0;}\n\ntemplate<class T>\nbool chmin(T &a, const T &b) {if (a > b) {a = b; return 1;} return 0;}\n\nusing ll = long long;\nusing ld = long double;\n\nbool is_end = false;\n\nint dp[1<<16];\n\nvoid init()\n{\n\tfor (int i = 0; i < (1<<16); ++i)\n\t{\n\t\tdp[i] = 1e9;\n\t}\n\treturn;\n}\n\nvoid calc()\n{\n\tint N; cin >> N;\n\tvector<int> H(N), W(N);\n\tfor (int i = 0; i < N; ++i) {cin >> H[i] >> W[i];}\n\t\n\tint C[4][4];\n\tfor (int i = 0; i < 4; ++i)\n\t{\n\t\tfor (int j = 0; j < 4; ++j)\n\t\t{\n\t\t\tchar c; cin >> c;\n\t\t\tif (c == 'R') C[i][j] = 0;\n\t\t\tif (c == 'G') C[i][j] = 1;\n\t\t\tif (c == 'B') C[i][j] = 2;\n\t\t}\n\t}\n\t\n\tvector<int> cbit(3, 0);\n\tfor (int i = 0; i < 4; ++i)\n\t{\n\t\tfor (int j = 0; j < 4; ++j)\n\t\t{\n\t\t\tint c = C[i][j];\n\t\t\tcbit[c] ^= (1<<(i*4+j));\n\t\t}\n\t}\n\t\n\tinit();\n\t\n\tdp[0] = 0;\n\tfor (int i = 1; i <= 16; ++i)\n\t{\n\t\tfor (int n = 0; n < N; ++n)\n\t\t{\n\t\t\tfor (int x1 = 0; x1 < 3 + H[n]; ++x1)\n\t\t\t{\n\t\t\t\tfor (int y1 = 0; y1 < 3 + W[n]; ++y1)\n\t\t\t\t{\n\t\t\t\t\tint x0 = max(x1 - H[n] + 1, 0);\n\t\t\t\t\tint y0 = max(y1 - W[n] + 1, 0);\n\t\t\t\t\tint mask = 0;\n\t\t\t\t\tfor (int x = x0; x <= min(x1, 3); ++x)\n\t\t\t\t\t{\n\t\t\t\t\t\tfor (int y = y0; y <= min(y1, 3); ++y)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tmask ^= (1<<(4*x+y));\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\t\n\t\t\t\t\t\n\t\t\t\t\tfor (int c = 0; c < 3; ++c)\n\t\t\t\t\t{\n\t\t\t\t\t\tfor (int bit = 0; bit < (1<<16); ++bit)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tif (dp[bit] > 100) {continue;}\n\t\t\t\t\t\t\t\n\t\t\t\t\t\t\tint nbit = bit & (~mask);\n\t\t\t\t\t\t\tnbit |= (mask & cbit[c]);\n\t\t\t\t\t\t\tchmin(dp[nbit], dp[bit] + 1);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\t\n\tcout << dp[(1<<16)-1] << endl;\n\t\t\n\treturn;\n}\n\nint main()\n{\n\tcalc();\n\t\n\treturn 0;\n}", "accuracy": 1, "time_ms": 1930, "memory_kb": 3720, "score_of_the_acc": -1.0059, "final_rank": 15 }, { "submission_id": "aoj_2297_8024090", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\ntypedef long long ll;\ntypedef __int128_t lll;\ntypedef long double ld;\ntypedef pair<ll, ll> pl;\ntypedef tuple<ll, ll, ll> tt;\ntypedef vector<ll> vl;\ntypedef vector<vl> vvl;\ntypedef vector<vvl> vvvl;\ntypedef vector<double> vd;\ntypedef vector<vd> vvd;\ntypedef vector<vvd> vvvd;\ntypedef vector<bool> vb;\ntypedef vector<vb> vvb;\ntypedef vector<vvb> vvvb;\ntypedef vector<char> vc;\ntypedef vector<vc> vvc;\ntypedef vector<vvc> vvvc;\ntypedef vector<pl> vp;\ntypedef vector<tt> vt;\ntypedef vector<string> vs;\n\nconst ll INF = 1000000010;\nconst ll INFL = INF * INF;\nconst ld epsilon = 1e-10;\n\n#define ovl(a, b, c, d, e, ...) e\n#define pb push_back\n#define eb emplace_back\n#define MP make_pair\n#define DEBUG(...) DEBUG_(#__VA_ARGS__, __VA_ARGS__)\n#define REP1(i, r) for (int i = 0; i < (r); i++)\n#define REP2(i, l, r) for (int i = (l); i < (r); i++)\n#define REP3(i, l, r, d) for (int i = (l); i < (r); i += (d))\n#define REP(...) ovl(__VA_ARGS__, REP3, REP2, REP1)(__VA_ARGS__)\n#define RREP1(i, r) for (int i = (r)-1; i >= 0; i--)\n#define RREP2(i, l, r) for (int i = (r)-1; i >= (l); i--)\n#define RREP3(i, l, r, d) for (int i = (r)-1; i >= (l); i -= (d))\n#define RREP(...) ovl(__VA_ARGS__, RREP3, RREP2, RREP1)(__VA_ARGS__)\n#define EACH1(x, a) for (auto &x : a)\n#define EACH2(x, y, a) for (auto &[x, y] : a)\n#define EACH3(x, y, z, a) for (auto &[x, y, z] : a)\n#define EACH(...) ovl(__VA_ARGS__, EACH3, EACH2, EACH1)(__VA_ARGS__)\n#define ALL(x) begin(x), end(x)\n#define RALL(x) (x).rbegin(), (x).rend()\n#define sz(x) (ll) x.size()\n#define LB(a, x) (lower_bound(ALL(a), x) - a.begin())\n#define UB(a, x) (upper_bound(ALL(a), x) - a.begin())\n#define FLG(x, i) (((x) >> (i)) & 1)\n#define CNTBIT(x) __builtin_popcountll(x)\n#define IN(x, a, b) ((a) <= (x) && (x) < (b))\n#define int ll\n\ntemplate <class T>\nvoid DEBUG_(string_view name, const T &a) {\n cerr << name << \": \" << a << endl;\n}\n\ntemplate <class T>\nvoid DEBUG_(string_view name, const vector<T> &a) {\n cerr << name << \": \";\n REP(i, sz(a)) cerr << a[i] << \" \";\n cerr << endl;\n}\n\ntemplate <class T, class U>\nbool chmax(T &x, const U &y) {\n return x < y ? x = y, 1 : 0;\n}\ntemplate <class T, class U>\nbool chmin(T &x, const U &y) {\n return x > y ? x = y, 1 : 0;\n}\n\n//-----------------------------------------------------------------------\n\nint N;\nvp HWs;\nvvl grid;\n\nmap<int, int> memo;\nint dp(const int bit = 0) {\n // DEBUG(bit);\n if (bit == (1 << 16) - 1) {\n return 0;\n }\n if (memo.count(bit)) {\n return memo[bit];\n }\n\n int ans = INF;\n EACH(h, w, HWs) {\n REP(h1, 4) {\n REP2(h2, h1, 4) {\n if (h2 - h1 + 1 > h) continue;\n if (1 <= h1 && h2 <= 2 && h2 - h1 + 1 != h) continue;\n REP(w1, 4) {\n REP2(w2, w1, 4) {\n if (w2 - w1 + 1 > w) continue;\n if (1 <= w1 && w2 <= 2 && w2 - w1 + 1 != w) continue;\n int seen = -1;\n int nbit = bit;\n REP(hh, h1, h2 + 1) {\n REP(ww, w1, w2 + 1) {\n if (nbit & (1 << (4 * hh + ww))) continue;\n nbit |= (1 << (4 * hh + ww));\n if (seen == -1) {\n seen = grid[hh][ww];\n } else if (seen != grid[hh][ww]) {\n seen = -INFL;\n break;\n }\n }\n }\n if (seen == -INFL) continue;\n if (bit == nbit) continue;\n chmin(ans, dp(nbit) + 1);\n }\n }\n }\n }\n }\n\n // DEBUG(bit);\n // DEBUG(ans);\n\n return memo[bit] = ans;\n}\n\nvoid solve() {\n ios::sync_with_stdio(false);\n cin.tie(0);\n cout << fixed << setprecision(15);\n\n int N;\n cin >> N;\n HWs.resize(N);\n REP(i, N) {\n int H, W;\n cin >> H >> W;\n HWs[i] = {H, W};\n }\n grid.resize(4, vl(4));\n REP(h, 4) {\n string S;\n cin >> S;\n REP(w, 4) {\n if (S[w] == 'R') grid[h][w] = 1;\n if (S[w] == 'G') grid[h][w] = 2;\n if (S[w] == 'B') grid[h][w] = 3;\n }\n }\n\n cout << dp() << endl;\n\n // EACH(key, val, memo) {\n // if (val != 0) {\n // DEBUG(key);\n // DEBUG(val);\n // }\n // }\n}\n\nsigned main() {\n int T = 1;\n // cin >> T;\n\n while (T--) solve();\n\n // random_test();\n\n return 0;\n}", "accuracy": 1, "time_ms": 1680, "memory_kb": 7564, "score_of_the_acc": -0.9295, "final_rank": 14 }, { "submission_id": "aoj_2297_7140085", "code_snippet": "#include<bits/stdc++.h>\n#define int ll\n#define rep(i, N) for(int i = 0; i < (int)(N); ++i)\n#define rep1(i, N) for(int i = 1; i <= (int)(N); ++i)\n#define per(i, N) for(int i = (N)-1; i >= 0; --i)\n#define all(v) (v).begin(), (v).end()\n#define rall(v) (v).rbegin(), (v).rend()\n#define bit(n, k) ((n) >> (k))\n#define pcnt(n) (__builtin_popcount(n))\n#define TakAo(ans) ans ? cout << \"Takahashi\\n\" : cout << \"Aoki\\n\"\n#define YesNo(ans) ans ? cout << \"Yes\\n\" : cout << \"No\\n\"\n#define endl '\\n'\n#define fi first\n#define se second\n#define mkpr make_pair\n#define mktpl make_tuple\n#define eb emplace_back\n\nusing namespace std;\nusing ll = int64_t;\nusing ull = uint64_t;\nusing ld = long double;\nusing point = struct{ ll x, y; };\nusing pointld = struct{ ld x, y; };\nusing State = string::const_iterator;\ntemplate<class T> using max_heap = priority_queue<T>;\ntemplate<class T> using min_heap = priority_queue<T, vector<T>, greater<T>>;\ntemplate<class T> using vec = vector<T>;\ntemplate<class T> using vvec = vec<vec<T>>;\ntemplate<class T> using vvvec = vec<vvec<T>>;\ntemplate<class T> using vvvvec = vvec<vvec<T>>;\n\nconstexpr ld EPS = 1e-10;\nconstexpr int INF = 1001001001;\nconstexpr ll LINF = 1001001001001001001ll;\nconstexpr ll MOD = (0) ? 998244353 : 1e9+7;\nconstexpr int NIL = -1;\nconstexpr int pm[2] = {1, -1};\nconstexpr int dy[4] = {0, 1, 0, -1};\nconstexpr int dx[4] = {1, 0, -1, 0};\n\nll cel(ll a, ll b){ return (a + b - 1) / b;}\nll Gcd(ll a, ll b){ return b ? Gcd(b, a % b) : abs(a);}\ntemplate<class T> T powi(T x, ll exp){\n return exp > 0 ? (exp & 1 ? x : 1) * powi(x * x, exp >> 1) : x / x;\n}\nll modpow(ll x, ll exp, ll mod){\n x %= mod;\n return exp > 0 ? (exp & 1 ? x : 1) * modpow(x * x, exp >> 1, mod) % mod : 1;\n}\ntemplate<class T> bool chmin(T &a, T b){ return a > b ? (a = b, true) : 0;}\ntemplate<class T> bool chmax(T &a, T b){ return a < b ? (a = b, true) : 0;}\n\nusing Pair = pair<ll, ll>;\nusing Tpl = tuple<int, int, int>;\n\nvoid Main(){\n int N; cin >> N;\n vec<int> H(N), W(N);\n rep(i, N) cin >> H[i] >> W[i];\n\n vvec<char> G(4, vec<char>(4));\n rep(i, 4)rep(j, 4) cin >> G[i][j];\n\n const int goal = (1 << 16) - 1;\n queue<Pair> que;\n vec<bool> used(1 << 16, 0);\n que.push(mkpr(0, 0));\n used[0] = 1;\n char col[3] = {'R', 'G', 'B'};\n\n while(!que.empty()){\n Pair p = que.front();\n que.pop();\n int num = p.fi, cst = p.se;\n \n rep(i, N){\n for(int j = -H[i] + 1; j < 4; j++){\n for(int k = -W[i] + 1; k < 4; k++){\n for(int l = 0; l < 3; l++){\n int tmp = num;\n\n for(int a = max(j, ll(0)); a < min(j + H[i], ll(4)); a++){\n for(int b = max(k, ll(0)); b < min(k + W[i], ll(4)); b++){\n if(col[l] == G[a][b]){\n tmp |= (1 << (4 * a + b));\n }\n else{\n //cout << bitset<16>(tmp) << endl;\n tmp &= ~(1 << (4 * a + b));\n //cout << bitset<16>(tmp) << endl;\n }\n }\n }\n\n if(tmp == goal){\n cout << cst + 1 << endl;\n return;\n }\n else{\n if(!used[tmp]){\n used[tmp] = 1;\n que.push(mkpr(tmp, cst + 1));\n }\n }\n }\n }\n }\n }\n }\n\n cout << -1 << endl;\n}\n \nsigned main(){\n cin.tie(nullptr);\n ios_base::sync_with_stdio(false);\n cout << fixed << setprecision(10);\n Main();\n return 0;\n}", "accuracy": 1, "time_ms": 540, "memory_kb": 3964, "score_of_the_acc": -0.2702, "final_rank": 6 }, { "submission_id": "aoj_2297_7136393", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing vi = vector<int>;\nusing pii = pair<int, int>;\n#define rep(i, x) for(int i = 0; i < (x); i++)\n#define rng(i, l, r) for(int i = (l); i < (r); i++)\n#define sz(x) (int)(x).size()\n#define all(v) (v).begin(), (v).end()\n\nint ctoi(char c) { return c == 'R' ? 2 : c == 'G'; }\nconst int inf = 1 << 30;\nint main() {\n\tint n;\n\tcin >> n;\n\tvector<pii> hw;\n\trep(i, n) {\n\t\tint u, v;\n\t\tcin >> u >> v;\n\t\thw.emplace_back(u, v);\n\t}\n\tvector<string> s(4);\n\tfor(auto &v : s) cin >> v;\n\tint N = 1 << 16;\n\tvector<vi> g(N);\n\trep(bit, N) {\n\t\tset<int> st;\n\t}\n\tvi dist(N, inf);\n\tdist[0] = 0;\n\tqueue<int> que;\n\tque.push(0);\n\twhile(!que.empty()) {\n\t\tauto bit = que.front();\n\t\tque.pop();\n\t\trep(i, n) {\n\t\t\tauto [h, w] = hw[i];\n\t\t\trng(x, -h, 4) {\n\t\t\t\trng(y, -w, 4) {\n\t\t\t\t\trep(c, 3) {\n\t\t\t\t\t\tint to = bit;\n\t\t\t\t\t\trng(nx, max(0, x), min(4, x + h)) {\n\t\t\t\t\t\t\trng(ny, max(0, y), min(4, y + w)) {\n\t\t\t\t\t\t\t\tif(nx >= 0 and nx < 4 and ny >= 0 and ny < 4) {\n\t\t\t\t\t\t\t\t\tint id = nx * 4 + ny;\n\t\t\t\t\t\t\t\t\tint col = ctoi(s[nx][ny]);\n\t\t\t\t\t\t\t\t\tif(c == col) {\n\t\t\t\t\t\t\t\t\t\tto |= 1 << id;\n\t\t\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\t\t\tif(to >> id & 1) to ^= 1 << id;\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif(dist[to] != inf) continue;\n\t\t\t\t\t\tdist[to] = dist[bit] + 1;\n\t\t\t\t\t\tque.push(to);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tcout << dist.back() << endl;\n}", "accuracy": 1, "time_ms": 1220, "memory_kb": 4972, "score_of_the_acc": -0.6467, "final_rank": 12 }, { "submission_id": "aoj_2297_7136390", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing vi = vector<int>;\nusing pii = pair<int, int>;\n#define rep(i, x) for(int i = 0; i < (x); i++)\n#define rng(i, l, r) for(int i = (l); i < (r); i++)\n#define sz(x) (int)(x).size()\n#define all(v) (v).begin(), (v).end()\n\nint ctoi(char c) { return c == 'R' ? 2 : c == 'G'; }\nconst int inf = 1 << 30;\nint main() {\n\tint n;\n\tcin >> n;\n\tvector<pii> hw;\n\trep(i, n) {\n\t\tint u, v;\n\t\tcin >> u >> v;\n\t\thw.emplace_back(u, v);\n\t}\n\tvector<string> s(4);\n\tfor(auto &v : s) cin >> v;\n\tint N = 1 << 16;\n\tvector<vi> g(N);\n\trep(bit, N) {\n\t\tset<int> st;\n\t}\n\tvi dist(N, inf);\n\tdist[0] = 0;\n\tqueue<int> que;\n\tque.push(0);\n\twhile(!que.empty()) {\n\t\tauto bit = que.front();\n\t\tque.pop();\n\t\trep(i, n) {\n\t\t\tauto [h, w] = hw[i];\n\t\t\trng(x, -h, 4) {\n\t\t\t\trng(y, -w, 4) {\n\t\t\t\t\trep(c, 3) {\n\t\t\t\t\t\tint to = bit;\n\t\t\t\t\t\trep(dx, h) {\n\t\t\t\t\t\t\trep(dy, w) {\n\t\t\t\t\t\t\t\tint nx = x + dx, ny = y + dy;\n\t\t\t\t\t\t\t\tif(nx >= 0 and nx < 4 and ny >= 0 and ny < 4) {\n\t\t\t\t\t\t\t\t\tint id = nx * 4 + ny;\n\t\t\t\t\t\t\t\t\tint col = ctoi(s[nx][ny]);\n\t\t\t\t\t\t\t\t\tif(c == col) {\n\t\t\t\t\t\t\t\t\t\tto |= 1 << id;\n\t\t\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\t\t\tif(1 >> id & 1) to ^= 1 << id;\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif(dist[to] != inf) continue;\n\t\t\t\t\t\tdist[to] = dist[bit] + 1;\n\t\t\t\t\t\tque.push(to);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tcout << dist.back() << endl;\n}", "accuracy": 0.03278688524590164, "time_ms": 250, "memory_kb": 4924, "score_of_the_acc": -0.13, "final_rank": 18 }, { "submission_id": "aoj_2297_7136353", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing vi = vector<int>;\nusing pii = pair<int, int>;\n#define rep(i, x) for(int i = 0; i < (x); i++)\n#define rng(i, l, r) for(int i = (l); i < (r); i++)\n#define sz(x) (int)(x).size()\n#define all(v) (v).begin(), (v).end()\n\nint ctoi(char c) { return c == 'R' ? 2 : c == 'G'; }\nconst int inf = 1 << 30;\nint main() {\n\tint n;\n\tcin >> n;\n\tvector<pii> hw;\n\tset<pii> st;\n\trep(i, n) {\n\t\tint u, v;\n\t\tcin >> u >> v;\n\t\trng(x, 1, u + 1) {\n\t\t\trng(y, 1, v + 1) {\n\t\t\t\tst.insert({x, y});\n\t\t\t}\n\t\t}\n\t}\n\tfor(auto &p : st) hw.push_back(p);\n\tn = sz(hw);\n\tvector<string> s(4);\n\tfor(auto &v : s) cin >> v;\n\tint N = 1 << 16;\n\tvector<vi> g(N);\n\trep(bit, 1 << 16) {\n\t\trep(i, n) {\n\t\t\tauto [h, w] = hw[i];\n\t\t\trep(x, 4) {\n\t\t\t\trep(y, 4) {\n\t\t\t\t\tif(x + h > 4 or y + w > 4) continue;\n\t\t\t\t\tvector<vi> ids(3), bads(3);\n\t\t\t\t\trep(dx, h) {\n\t\t\t\t\t\trep(dy, w) {\n\t\t\t\t\t\t\tint nx = x + dx, ny = y + dy;\n\t\t\t\t\t\t\tint id = nx * 4 + ny;\n\t\t\t\t\t\t\tint col = ctoi(s[nx][ny]);\n\t\t\t\t\t\t\trep(j, 3) {\n\t\t\t\t\t\t\t\tif(j == col)\n\t\t\t\t\t\t\t\t\tids[j].push_back(id);\n\t\t\t\t\t\t\t\telse\n\t\t\t\t\t\t\t\t\tbads[j].push_back(id);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\trep(j, 3) {\n\t\t\t\t\t\tif(ids[j].empty()) continue;\n\t\t\t\t\t\tint nb = bit;\n\t\t\t\t\t\tfor(auto v : ids[j]) {\n\t\t\t\t\t\t\tif(!(nb >> v & 1)) nb |= 1 << v;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tfor(auto v : bads[j]) {\n\t\t\t\t\t\t\tif(nb >> v & 1) nb ^= 1 << v;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tg[bit].push_back(nb);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tvi dist(N, inf);\n\tdist[0] = 0;\n\tqueue<int> que;\n\tque.push(0);\n\twhile(!que.empty()) {\n\t\tauto v = que.front();\n\t\tque.pop();\n\t\tfor(auto to : g[v]) {\n\t\t\tif(dist[to] != inf) continue;\n\t\t\tdist[to] = dist[v] + 1;\n\t\t\tque.push(to);\n\t\t}\n\t}\n\tcout << dist.back() << endl;\n}", "accuracy": 0.03278688524590164, "time_ms": 1540, "memory_kb": 71352, "score_of_the_acc": -1.7926, "final_rank": 19 }, { "submission_id": "aoj_2297_7136283", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing vi = vector<int>;\nusing pii = pair<int, int>;\n#define rep(i, x) for(int i = 0; i < (x); i++)\n#define rng(i, l, r) for(int i = (l); i < (r); i++)\n#define sz(x) (int)(x).size()\n#define all(v) (v).begin(), (v).end()\n\nint ctoi(char c) { return c == 'R' ? 2 : c == 'G'; }\nconst int inf = 1 << 30;\nint main() {\n\tint n;\n\tcin >> n;\n\tvector<pii> hw;\n\trep(i, n) {\n\t\tint u, v;\n\t\tcin >> u >> v;\n\t\thw.emplace_back(u, v);\n\t}\n\tvector<string> s(4);\n\tfor(auto &v : s) cin >> v;\n\tint N = 1 << 16;\n\tvector<vi> g(N);\n\trep(bit, 1 << 16) {\n\t\trep(i, n) {\n\t\t\tauto [h, w] = hw[i];\n\t\t\trep(x, 4) {\n\t\t\t\trep(y, 4) {\n\t\t\t\t\tif(x + h > 4 or y + w > 4) continue;\n\t\t\t\t\tset<char> st;\n\t\t\t\t\tvector<vi> ids(3), bads(3);\n\t\t\t\t\trep(dx, h) {\n\t\t\t\t\t\trep(dy, w) {\n\t\t\t\t\t\t\tint nx = x + dx, ny = y + dy;\n\t\t\t\t\t\t\tint id = nx * 4 + ny;\n\t\t\t\t\t\t\tint col = ctoi(s[nx][ny]);\n\t\t\t\t\t\t\trep(j, 3) {\n\t\t\t\t\t\t\t\tif(j == col)\n\t\t\t\t\t\t\t\t\tids[j].push_back(id);\n\t\t\t\t\t\t\t\telse\n\t\t\t\t\t\t\t\t\tbads[j].push_back(id);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\trep(j, 3) {\n\t\t\t\t\t\tif(ids[j].empty()) continue;\n\t\t\t\t\t\tint nb = bit;\n\t\t\t\t\t\tfor(auto v : ids[j]) {\n\t\t\t\t\t\t\tif(!(nb >> v & 1)) nb |= 1 << v;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tfor(auto v : bads[j]) {\n\t\t\t\t\t\t\tif(nb >> v & 1) nb ^= 1 << v;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tg[bit].push_back(nb);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tvi dist(N, inf);\n\tdist[0] = 0;\n\tqueue<int> que;\n\tque.push(0);\n\twhile(!que.empty()) {\n\t\tauto v = que.front();\n\t\tque.pop();\n\t\tfor(auto to : g[v]) {\n\t\t\tif(dist[to] != inf) continue;\n\t\t\tdist[to] = dist[v] + 1;\n\t\t\tque.push(to);\n\t\t}\n\t}\n\tcout << dist.back() << endl;\n}", "accuracy": 0.01639344262295082, "time_ms": 130, "memory_kb": 13892, "score_of_the_acc": -0.198, "final_rank": 20 }, { "submission_id": "aoj_2297_7136264", "code_snippet": "#define _GLIBCXX_DEBUG\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing vi = vector<int>;\nusing pii = pair<int, int>;\n#define rep(i, x) for(int i = 0; i < (x); i++)\n#define rng(i, l, r) for(int i = (l); i < (r); i++)\n#define sz(x) (int)(x).size()\n#define all(v) (v).begin(), (v).end()\n\nint ctoi(char c) { return c == 'R' ? 2 : c == 'G'; }\n\nint main() {\n\tint n;\n\tcin >> n;\n\tvector<pii> hw;\n\trep(i, n) {\n\t\tint u, v;\n\t\tcin >> u >> v;\n\t\thw.emplace_back(u, v);\n\t}\n\tvector<string> s(4);\n\tfor(auto &v : s) cin >> v;\n\tvector<int> res(1 << 16, 1 << 30);\n\tauto dfs = [&](auto dfs, int bit) -> int {\n\t\tif(bit == 0) return 0;\n\t\tif(res[bit] != (1 << 30)) return res[bit];\n\t\tint ans = 1 << 30;\n\t\trep(i, n) {\n\t\t\tauto [h, w] = hw[i];\n\t\t\trep(x, 4) {\n\t\t\t\trep(y, 4) {\n\t\t\t\t\tif(x + h > 4 or y + w > 4) continue;\n\t\t\t\t\tset<char> st;\n\t\t\t\t\tvector<vi> ids(3);\n\t\t\t\t\trep(dx, h) {\n\t\t\t\t\t\trep(dy, w) {\n\t\t\t\t\t\t\tint nx = x + dx, ny = y + dy;\n\t\t\t\t\t\t\tint id = nx * 4 + ny;\n\t\t\t\t\t\t\tif(!(bit >> id & 1)) continue;\n\t\t\t\t\t\t\tids[ctoi(s[nx][ny])].push_back(id);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\trep(j, 3) {\n\t\t\t\t\t\tif(ids[j].empty()) continue;\n\t\t\t\t\t\tint nb = bit;\n\t\t\t\t\t\tfor(auto v : ids[j]) nb ^= (1 << v);\n\t\t\t\t\t\tans = min(ans, dfs(dfs, nb) + 1);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn res[bit] = ans;\n\t};\n\tdfs(dfs, (1 << 16) - 1);\n\tcout << res.back() << endl;\n}", "accuracy": 0.03278688524590164, "time_ms": 120, "memory_kb": 3372, "score_of_the_acc": -0.0381, "final_rank": 17 }, { "submission_id": "aoj_2297_7000584", "code_snippet": "#include <stdio.h>\n#include <queue>\nusing namespace std;\n\nchar targ[8][8], state[4][4], tstate[4][4], vis[65536];\nint stamp[16][2], dist[65536];\nqueue<int> q;\n\nint main(void) {\n int n, s, t;\n scanf(\"%d\", &n);\n for (int i = 0; i < n; i++) {\n scanf(\"%d %d\", &stamp[i][0], &stamp[i][1]);\n }\n for (int i = 0; i < 4; i++) {\n scanf(\"%s\", targ[i]);\n for (int j = 0; j < 4; j++) {\n if (targ[i][j] == 'R') targ[i][j] = 1;\n else if (targ[i][j] == 'G') targ[i][j] = 2;\n else targ[i][j] = 3;\n }\n }\n vis[0] = 1;\n q.push(0);\n while (q.size()) {\n s = q.front();\n q.pop();\n for (int i = 0; i < 4; i++) {\n for (int j = 0; j < 4; j++) state[i][j] = ((s >> (i * 4 + j)) & 1);\n }\n for (int i = 0; i < n; i++) {\n for (int j = 1; j <= 3; j++) {\n for (int x = -stamp[i][0] + 1; x <= 3; x++) {\n for (int y = -stamp[i][1] + 1; y <= 3; y++) {\n for (int xx = 0; xx < 4; xx++) {\n for (int yy = 0; yy < 4; yy++) tstate[xx][yy] = state[xx][yy];\n }\n for (int xx = x; xx < x + stamp[i][0]; xx++) {\n if (xx < 0 || xx >= 4) continue;\n for (int yy = y; yy < y + stamp[i][1]; yy++) {\n if (yy < 0 || yy >= 4) continue;\n if (targ[xx][yy] == j) tstate[xx][yy] = 1;\n else tstate[xx][yy] = 0;\n }\n }\n t = 0;\n for (int xx = 0; xx < 4; xx++) {\n for (int yy = 0; yy < 4; yy++) t += (1 << (xx * 4 + yy)) * tstate[xx][yy];\n }\n if (!vis[t]) {\n vis[t] = 1;\n q.push(t);\n dist[t] = dist[s] + 1;\n }\n }\n }\n }\n }\n }\n\n printf(\"%d\\n\", dist[65535]);\n return 0;\n}", "accuracy": 1, "time_ms": 1790, "memory_kb": 3420, "score_of_the_acc": -0.9271, "final_rank": 13 }, { "submission_id": "aoj_2297_6757639", "code_snippet": "#include <iostream>\n#include <vector>\nusing namespace std;\n\nvector<pair<int,int>> X;\nvector<vector<char>> A(4,vector<char>(4));\nint memo[65536];\n\nint dfs(int n,int nw){\n //cout << n << \" \" << nw << endl;\n //if(nw == 65471 )cout << n << endl;\n if(nw == 0){\n return memo[nw] = 0;\n }else if(memo[nw]){\n return memo[nw];\n }\n int ret = -1;\n for(int i = 0; X.size() > i; i++){\n for(int j = -X[i].first+1; 4 > j; j++){\n for(int k = -X[i].second+1; 4 > k; k++){\n int nwc = -1;\n bool ok = true;\n int mns = 0;\n for(int l = 0; X[i].first > l && ok; l++){\n for(int m = 0; X[i].second > m && ok; m++){\n int nwy = j+l;\n int nwx = k+m;\n if(nwy < 0 || 3 < nwy || nwx < 0 || 3 < nwx)continue;\n \n if(!((1<<(nwy*4+nwx)) & nw))continue;\n //cout << \"!!\" << nwy << \" \" << nwx << endl;\n mns += (1<<(nwy*4+nwx));\n // if(n == 0 && i == 1 && nwy == 1 && nwx == 2)cout << nw-mns << endl;\n if(nwc == -1){\n nwc = A[nwy][nwx];\n }else if(nwc != A[nwy][nwx]){\n ok = false;\n break;\n }\n }\n }\n // if(n == 0 && i == 1 && j == 1 && k == 2){\n // cout << ok << \" \" << nwc << \" \" << mns << endl;\n // }\n //cout << i << \" \" << j << \" \" << k <<endl;\n if(mns == 0)continue;\n if(ok && nwc!=-1){\n if(ret == -1)ret = dfs(n+1,nw-mns)+1;\n else ret = min(ret,(dfs(n+1,nw-mns)+1));\n }\n\n }\n }\n }\n return memo[nw] = ret;\n}\n\nint main(){\n int n;cin>>n;\n for(int i = 0; n > i; i++){\n int x,y;cin>>x>>y;X.push_back({x,y});\n }\n \n int nw = 1;\n for(int i = 0; 4 > i; i++){\n for(int j = 0; 4 > j; j++){\n cin>>A[i][j];\n nw *= 2;\n }\n }\n cout << dfs(0,nw-1) << endl;\n //cout << memo[65535-3] << endl;\n\n}", "accuracy": 1, "time_ms": 870, "memory_kb": 3680, "score_of_the_acc": -0.4415, "final_rank": 10 }, { "submission_id": "aoj_2297_6757129", "code_snippet": "#include<bits/stdc++.h>\n#define rep(i,a,...) for(int i = (a)*(strlen(#__VA_ARGS__)!=0);i<(int)(strlen(#__VA_ARGS__)?__VA_ARGS__:(a));++i)\n#define per(i,a,...) for(int i = (strlen(#__VA_ARGS__)?__VA_ARGS__:(a))-1;i>=(int)(strlen(#__VA_ARGS__)?(a):0);--i)\n#define foreach(i, n) for(auto &i:(n))\n#define all(x) (x).begin(), (x).end()\n#define bit(x) (1ll << (x))\n#define lambda(RES_TYPE, ...) (function<RES_TYPE(__VA_ARGS__)>)[&](__VA_ARGS__) -> RES_TYPE\n#define method(FUNC_NAME, RES_TYPE, ...) function<RES_TYPE(__VA_ARGS__)> FUNC_NAME = lambda(RES_TYPE, __VA_ARGS__)\nusing namespace std;\nusing ll = long long;\nusing pii = pair<int,int>;\nusing pll = pair<ll,ll>;\n//const ll MOD = (ll)1e9+7;\nconst ll MOD = 998244353;\nconst int INF = (ll)1e9+7;\nconst ll INFLL = (ll)1e18;\ntemplate<class t>\nusing vvector = vector<vector<t>>;\ntemplate<class t>\nusing vvvector = vector<vector<vector<t>>>;\ntemplate<class t>\nusing priority_queuer = priority_queue<t, vector<t>, greater<t>>;\ntemplate<class t, class u> bool chmax(t &a, u b){if(a<b){a=b;return true;}return false;}\ntemplate<class t, class u> bool chmin(t &a, u b){if(a>b){a=b;return true;}return false;}\n#ifdef DEBUG\n#define debug(x) cout<<\"LINE \"<<__LINE__<<\": \"<<#x<<\" = \"<<x<<endl;\n#else\n#define debug(x) (void)0\n#endif\n\nnamespace templates{\n ll modpow(ll x, ll b,ll mod=MOD){\n ll res = 1;\n while(b){\n if(b&1)res = res * x % mod;\n x = x * x % mod;\n b>>=1;\n }\n return res;\n }\n\n ll modinv(ll x){\n return modpow(x, MOD-2);\n }\n\n bool was_output = false;\n\n void print();\n template <class t> void print(const vector<t> &);\n template <class t, class u> void print(const pair<t, u> &);\n template <class t> void print(const t&);\n template <class Head, class... Tail> void print(const Head&, const Tail&...);\n\n template <class t> void println(const vector<vector<t>>&);\n template <class t> void println(const vector<t>&);\n template <class t> void println(const t&);\n template <class Head, class... Tail> void println(const Head&, const Tail&...);\n void println();\n void newline();\n\n void print(){\n return;\n }\n\n template <class t>\n void print(const vector<t>&x){\n for(const t&i:x)print(i);\n }\n template <class t, class u>\n void print(const pair<t,u>&p){\n print(p.first);\n print(p.second);\n }\n template <class t>\n void print(const t&x){\n if(was_output)cout<<\" \";\n cout<<x;\n was_output = true;\n }\n template <class Head, class... Tail>\n void print(const Head&head,const Tail&...tail){\n print(head);\n print(tail...);\n }\n\n template<class t>\n void println(const vector<vector<t>>&x){\n for(vector<t> i:x)println(i);\n }\n template<class t>\n void println(const vector<t>&x){\n for(const t&i:x)print(i);\n println();\n }\n template <class t>\n void println(const t&x){\n print(x);\n println();\n }\n void println(){\n if(was_output){\n cout << endl;\n was_output = false;\n }\n }\n template <class Head, class... Tail>\n void println(const Head&head,const Tail&...tail){\n print(head);\n println(tail...);\n }\n\n void newline(){\n was_output = true;\n println();\n }\n\n template<class t>\n istream& operator>>(istream&is, vector<t>&x){\n for(auto &i:x)is >> i;\n return is;\n }\n\n template<class t, class u>\n istream& operator>>(istream&is, pair<t, u>&x){\n is >> x.first >> x.second;\n return is;\n }\n\n template<class t>\n ostream& operator<<(ostream&os, vector<t> &v){\n os << \"{\";\n for(t &i:v){\n os << i << \", \";\n }\n os << \"}\";\n return os;\n }\n\n template<class t = long long>\n t in(){\n t res; cin >> res; return res;\n }\n\n template<class t>\n vector<t> sorted(vector<t> line,function<bool(t,t)> comp=[](t a,t b){return a<b;}){\n sort(line.begin(),line.end(),comp);\n return line;\n }\n\n template<class t>\n vector<t> reversed(vector<t> line){\n reverse(line.begin(),line.end());\n return line;\n }\n string reversed(string str){\n reverse(str.begin(),str.end());\n return str;\n }\n\n long long gcd(long long a,long long b){\n while(b){\n a %= b;\n swap(a,b);\n }\n return a;\n }\n\n long long lcm(long long a,long long b){\n return a / gcd(a,b) * b;\n }\n\n class output_initializer{\n public:\n output_initializer(){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << setprecision(20);\n }\n };output_initializer OUTPUT_INITIALIZER_INSTANCE = output_initializer();\n}\n\nusing namespace templates;\n\nint func(){\n int n = in();\n vector<pii> stamps(n);\n foreach(i,stamps)i=in<pii>();\n vector<int> need(16);\n foreach(i,need){\n char c = in<char>();\n if(c=='R')i = 1;\n else if(c=='G')i = 2;\n else if(c=='B')i = 4;\n }\n vector<int> dp(bit(16),-1);\n method(rec,int,int p){\n if(p==bit(16)-1)return 0;\n int &it = dp[p];\n if(it >= 0)return it;\n it = INF;\n foreach(stamp,stamps){\n rep(i,-stamp.first+1,4+stamp.first-1){\n rep(j,-stamp.second+1,4+stamp.second-1){\n int ok = 0;\n int ps = 0;\n rep(y,max(0,i),min(4,i+stamp.first)){\n rep(x,max(0,j),min(4,j+stamp.second)){\n if(bit(4*y+x)&p)continue;\n ok |= need[y*4+x];\n ps |= bit(4*y+x);\n }\n }\n if(__builtin_popcount(ok)==1){\n chmin(it,rec(p|ps)+1);\n }\n }\n }\n }\n return it;\n };\n return rec(0);\n}\n\nint main(){\n println(func());\n return 0;\n}", "accuracy": 1, "time_ms": 530, "memory_kb": 3444, "score_of_the_acc": -0.2572, "final_rank": 4 }, { "submission_id": "aoj_2297_6757127", "code_snippet": "#include<bits/stdc++.h>\n#define int ll\n#define rep(i, N) for(int i = 0; i < (int)(N); ++i)\n#define rep1(i, N) for(int i = 1; i <= (int)(N); ++i)\n#define per(i, N) for(int i = (N)-1; i >= 0; --i)\n#define all(v) (v).begin(), (v).end()\n#define rall(v) (v).rbegin(), (v).rend()\n#define bit(n, k) ((n) >> (k))\n#define pcnt(n) (__builtin_popcount(n))\n#define TakAo(ans) ans ? cout << \"Takahashi\\n\" : cout << \"Aoki\\n\"\n#define YesNo(ans) ans ? cout << \"Yes\\n\" : cout << \"No\\n\"\n#define endl '\\n'\n#define fi first\n#define se second\n#define mkpr make_pair\n#define mktpl make_tuple\n#define eb emplace_back\n\nusing namespace std;\nusing ll = int64_t;\nusing ull = uint64_t;\nusing ld = long double;\nusing point = struct{ ll x, y; };\nusing pointld = struct{ ld x, y; };\nusing State = string::const_iterator;\ntemplate<class T> using max_heap = priority_queue<T>;\ntemplate<class T> using min_heap = priority_queue<T, vector<T>, greater<T>>;\ntemplate<class T> using vec = vector<T>;\ntemplate<class T> using vvec = vec<vec<T>>;\ntemplate<class T> using vvvec = vec<vvec<T>>;\ntemplate<class T> using vvvvec = vvec<vvec<T>>;\n\nconstexpr ld EPS = 1e-10;\nconstexpr int INF = 1001001001;\nconstexpr ll LINF = 1001001001001001001ll;\nconstexpr ll MOD = (0) ? 998244353 : 1e9+7;\nconstexpr int NIL = -1;\nconstexpr int pm[2] = {1, -1};\nconstexpr int dy[4] = {0, 1, 0, -1};\nconstexpr int dx[4] = {1, 0, -1, 0};\n\nll cel(ll a, ll b){ return (a + b - 1) / b;}\nll Gcd(ll a, ll b){ return b ? Gcd(b, a % b) : abs(a);}\ntemplate<class T> T powi(T x, ll exp){\n return exp > 0 ? (exp & 1 ? x : 1) * powi(x * x, exp >> 1) : x / x;\n}\nll modpow(ll x, ll exp, ll mod){\n x %= mod;\n return exp > 0 ? (exp & 1 ? x : 1) * modpow(x * x, exp >> 1, mod) % mod : 1;\n}\ntemplate<class T> bool chmin(T &a, T b){ return a > b ? (a = b, true) : 0;}\ntemplate<class T> bool chmax(T &a, T b){ return a < b ? (a = b, true) : 0;}\n\nusing Pair = pair<ll, ll>;\nusing Tpl = tuple<int, int, int>;\n\nvoid Main(){\n int N; cin >> N;\n vec<int> H(N), W(N);\n rep(i, N) cin >> H[i] >> W[i];\n\n vvec<char> G(4, vec<char>(4));\n rep(i, 4)rep(j, 4) cin >> G[i][j];\n\n const int goal = (1 << 16) - 1;\n queue<Pair> que;\n vec<bool> used(1 << 16, 0);\n que.push(mkpr(0, 0));\n used[0] = 1;\n char col[3] = {'R', 'G', 'B'};\n\n while(!que.empty()){\n Pair p = que.front();\n que.pop();\n int num = p.fi, cst = p.se;\n \n rep(i, N){\n for(int j = -H[i] + 1; j < 4; j++){\n for(int k = -W[i] + 1; k < 4; k++){\n for(int l = 0; l < 3; l++){\n int tmp = num;\n\n for(int a = max(j, ll(0)); a < min(j + H[i], ll(4)); a++){\n for(int b = max(k, ll(0)); b < min(k + W[i], ll(4)); b++){\n if(col[l] == G[a][b]){\n tmp |= (1 << (4 * a + b));\n }\n else{\n //cout << bitset<16>(tmp) << endl;\n tmp &= ~(1 << (4 * a + b));\n //cout << bitset<16>(tmp) << endl;\n }\n }\n }\n\n if(tmp == goal){\n cout << cst + 1 << endl;\n return;\n }\n else{\n if(!used[tmp]){\n used[tmp] = 1;\n que.push(mkpr(tmp, cst + 1));\n }\n }\n }\n }\n }\n }\n }\n\n cout << -1 << endl;\n}\n \nsigned main(){\n cin.tie(nullptr);\n ios_base::sync_with_stdio(false);\n cout << fixed << setprecision(10);\n Main();\n return 0;\n}", "accuracy": 1, "time_ms": 540, "memory_kb": 3936, "score_of_the_acc": -0.2698, "final_rank": 5 }, { "submission_id": "aoj_2297_6725868", "code_snippet": "#include<bits/stdc++.h>\n// #include<atcoder/modint>\nusing namespace std;\n// using namespace atcoder;\nchar G[4][4];\nint n;\nint dist[1 << 17];\nint bi[20];\nvoid solve(){\n\tcin >> n;\n\tvector<pair<int, int>> hw(n);\n\tfor(int i = 0; i < n; i++) cin >> hw[i].first >> hw[i].second;\n\tfor(int i = 0; i < (1 << 16); i++) dist[i] = -1;\n\tfor(int i = 0; i < 4; i++){\n\t\tfor(int j = 0; j < 4; j++){\n\t\t\tcin >> G[i][j];\n\t\t}\n\t}\n\tfor(int i = 0; i < 20; i++) bi[i] = 1 << i;\n\tstring rgb = \"RGB\";\n\tset<pair<int, int>> bits;\n\tfor(auto &[h, w]: hw){\n\t\tfor(auto c:rgb){\t\t\t\t\n\t\t\tfor(int i = -h + 1; i <= 3; i++){\n\t\t\t\tfor(int j = -w + 1; j <= 3; j++){\n\t\t\t\t\tint andbit = (1 << 16) - 1;\n\t\t\t\t\tint orbit = 0;\n\t\t\t\t\tfor(int ii = i; ii < i + h; ii++){\n\t\t\t\t\t\tfor(int jj = j; jj < j + w; jj++){\n\t\t\t\t\t\t\tif(0 <= ii && ii < 4 && 0 <= jj && jj < 4){\n\t\t\t\t\t\t\t\tint ij = ii * 4 + jj;\n\t\t\t\t\t\t\t\tandbit ^= bi[ij];\n\t\t\t\t\t\t\t\tif(G[ii][jj] == c) orbit |= bi[ij];\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tbits.insert({orbit, andbit});\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\t\n\tdist[0] = 0;\n\tqueue<int> que;\n\tque.push(0);\n\twhile(!que.empty()){\n\t\tint bit = que.front();\n\t\tque.pop();\n\t\tfor(auto &[orbit, andbit]: bits){\n\t\t\tint nbit = bit;\n\t\t\tnbit &= andbit;\n\t\t\tnbit |= orbit;\n\t\t\tif(dist[nbit] == -1){\n\t\t\t\tdist[nbit] = dist[bit] + 1;\n\t\t\t\tque.push(nbit);\n\t\t\t}\n\t\t}\n\t}\n\tcout << dist[(1 << 16) - 1] << \"\\n\";\n\n\n}\n\nint main(){\n cin.tie(0)->sync_with_stdio(0);\n int t;\n t = 1;\n // cin >> t;\n while(t--) solve();\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 3844, "score_of_the_acc": -0.0078, "final_rank": 1 } ]
aoj_2299_cpp
J: Tiles are Colorful ICPC で良い成績を収めるには修行が欠かせない.うさぎは ICPC で勝ちたいので,今日も修行をすることにした. 今日の修行は,流行りのパズルをすばやく解いて,瞬発力を鍛えようというものである.今日挑戦するのは,色とりどりのタイルが並んでいてそれらを上手く消していくパズルだ 初期状態では,グリッド上のいくつかのマスにタイルが置かれている.各タイルには色がついている.プレイヤーはゲーム開始後,以下の手順で示される操作を何回も行うことができる. タイルが置かれていないマスを 1 つ選択し,そのマスを叩く. 叩いたマスから上に順に辿っていき,タイルが置かれているマスに至ったところでそのタイルに着目する.タイルが置かれているマスがないまま盤面の端に辿り着いたら何にも着目しない. 同様の操作を叩いたマスから下・左・右方向に対して行う.最大 4 枚のタイルが着目されることになる. 着目したタイルの中で同じ色のものがあれば,それらのタイルを盤面から取り除く.同じ色のタイルの組が 2 組あれば,それら両方を取り除く. タイルを取り除いた枚数と同じ値の得点が入る. 着目をやめる. たとえば,以下のような状況を考えよう.タイルが置かれていないマスはピリオドで,タイルの色はアルファベット大文字で表されている. ..A....... .......B.. .......... ..B....... ..A.CC.... ここで上から 2 行目,左から 3 列目のマスを叩く操作を考える.着目することになるタイルは A , B , B の 3 枚であるから, B の 2 枚が消えて盤面は以下のようになり,2 点を得る. ..A....... .......... .......... .......... ..A.CC.... このパズルはゆっくりしていると時間切れになってしまい,盤面の一部が見えなくなりどのくらい修行が足りなかったのかがわからなくなってしまう. 各色のタイルは 2 枚ずつ置かれているが,それらをすべて消せるとは限らないので,予めプログラムに得点の最大値を計算させておきたい. Input M N C 1,1 C 1,2 ... C 1, N C 2,1 C 2,2 ... C 2, N ... C M ,1 C M ,2 ... C M , N 整数 M , N は盤が 縦 M × 横 N のマス目であることを表す. C i , j はアルファベット大文字またはピリオド ( . ) であり,上から i 行目,左から j 列目のマスについて,アルファベット大文字の場合は置かれているタイルの色を表し,ピリオドの場合はこのマスにタイルが置かれていないことを表す. 1 ≤ M ≤ 500,1 ≤ N ≤ 500 を満たす.各アルファベット大文字は入力中に 0 個または 2 個現れる. Output 得点の最大値を 1 行に出力せよ. Sample Input 1 5 10 ..A....... .......B.. .......... ..B....... ..A.CC.... Sample Output 1 4 Sample Input 2 3 3 ABC D.D CBA Sample Output 2 4 Sample Input 3 5 7 NUTUBOR QT.SZRQ SANAGIP LMDGZBM KLKIODP Sample Output 3 34
[ { "submission_id": "aoj_2299_9394837", "code_snippet": "#include <iostream>\n#include <vector>\n#include <stack>\n#include <set>\n#include <map>\n#include <algorithm>\n#include <string>\n#include <cctype>\n#include <cmath>\nusing namespace std;\nusing ll = long long;\nconst long long INF = (1LL<<60);\nconst long long mod = 998244353;\n#define rep(i, n) for (int i = 0; i < n; i++)\nint m, n;\nvector<string> a;\nll score = 0;\nbool update(int i, int j) {\n bool flag = false;\n map<char, vector<pair<int, int>>> ma;\n int scale = 1;\n // sita\n while (i + scale < m) {\n if (a[i+scale][j] != '.') {\n ma[a[i+scale][j]].push_back(make_pair(i+scale, j));\n break;\n }\n scale++;\n }\n scale = 1;\n // down\n while (i - scale >= 0) {\n if (a[i-scale][j] != '.') {\n ma[a[i-scale][j]].push_back(make_pair(i-scale, j));\n break;\n }\n scale++;\n }\n scale = 1;\n while (j + scale < n) {\n if (a[i][j+scale] != '.') {\n ma[a[i][j+scale]].push_back(make_pair(i, j+scale));\n break;\n }\n scale++;\n }\n scale = 1;\n while (j - scale >= 0) {\n if (a[i][j-scale] != '.') {\n ma[a[i][j-scale]].push_back(make_pair(i, j-scale));\n break;\n }\n scale++;\n }\n for (pair<char, vector<pair<int, int>>> p : ma) {\n if (p.second.size() >= 2) {\n for (pair<int, int> px : p.second) {\n a[px.first][px.second] = '.';\n score++;\n flag = true;\n }\n }\n }\n return flag;\n}\nint main(){\n cin>>m>>n;\n a = vector<string>(m);\n rep(i, m) cin>>a[i];\n while (true) {\n bool flag = false;\n rep(i, m) {\n rep(j, n) {\n if (a[i][j] != '.') continue;\n if (update(i, j)) {\n flag = true;\n break;\n }\n }\n }\n if (!flag) break;\n }\n cout<<score<<endl;\n}", "accuracy": 1, "time_ms": 360, "memory_kb": 3936, "score_of_the_acc": -0.283, "final_rank": 6 }, { "submission_id": "aoj_2299_9394696", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n#define rep(i, n) for (int i = 0; i < (n); i++)\nusing ll = long long;\n#define all(x) x.begin(), x.end()\ntemplate <class A, class B>\ninline bool chmin(A &a, const B &b) {\n return b < a && (a = b, true);\n}\ntemplate <class A, class B>\ninline bool chmax(A &a, const B &b) {\n return b > a && (a = b, true);\n}\n\nint main() {\n int h, w;\n cin >> h >> w;\n vector<string> s(h);\n rep(i, h) cin >> s[i];\n\n vector<bool> used(26);\n vector<pair<int, int>> st;\n\n auto get = [&](int i, int j, int di, int dj) -> tuple<int, int, int> {\n assert(s[i][j] == '.');\n while (0 <= i && i < h && 0 <= j && j < w && s[i][j] == '.') {\n i += di;\n j += dj;\n }\n if (0 <= i && i < h && 0 <= j && j < w) return {i, j, s[i][j] - 'A'};\n return {-1, -1, -1};\n };\n\n const int DI[] = {1, 0, -1, 0};\n const int DJ[] = {0, 1, 0, -1};\n rep(i, h) rep(j, w) {\n if (s[i][j] != '.') continue;\n vector<int> count(26);\n rep(d, 4) {\n auto [ni, nj, v] = get(i, j, DI[d], DJ[d]);\n if (v != -1) count[v]++;\n }\n rep(k, 26) if (count[k] == 2 && !used[k]) {\n used[k] = true;\n st.emplace_back(i, j);\n break;\n }\n }\n int ans = 0;\n while (!st.empty()) {\n auto [ci, cj] = st.back();\n st.pop_back();\n vector<vector<pair<int, int>>> count(26);\n rep(d, 4) {\n auto [ni, nj, v] = get(ci, cj, DI[d], DJ[d]);\n if (v != -1) {\n count[v].emplace_back(ni, nj);\n }\n }\n rep(k, 26) if (count[k].size() == 2) {\n ans++;\n for (auto [i, j]: count[k]) {\n assert(s[i][j] == k + 'A');\n s[i][j] = '.';\n rep(a, h) if (s[a][j] == '.') st.emplace_back(a, j);\n rep(b, w) if (s[i][b] == '.') st.emplace_back(i, b);\n }\n }\n }\n cout << 2 * ans << '\\n';\n}", "accuracy": 1, "time_ms": 270, "memory_kb": 3972, "score_of_the_acc": -0.2412, "final_rank": 4 }, { "submission_id": "aoj_2299_9394680", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n#define rep(i, n) for (int i = 0; i < (n); i++)\nusing ll = long long;\n#define all(x) x.begin(), x.end()\ntemplate <class A, class B>\ninline bool chmin(A &a, const B &b) {\n return b < a && (a = b, true);\n}\ntemplate <class A, class B>\ninline bool chmax(A &a, const B &b) {\n return b > a && (a = b, true);\n}\n\nint main() {\n int h, w;\n cin >> h >> w;\n vector<string> s(h);\n rep(i, h) cin >> s[i];\n\n vector<bool> used(26);\n vector<pair<int, int>> st;\n\n auto get = [&](int i, int j, int di, int dj) -> tuple<int, int, int> {\n assert(s[i][j] == '.');\n while (0 <= i && i < h && 0 <= j && j < w && s[i][j] == '.') {\n i += di;\n j += dj;\n }\n if (0 <= i && i < h && 0 <= j && j < w) return {i, j, s[i][j] - 'A'};\n return {-1, -1, -1};\n };\n\n const int DI[] = {1, 0, -1, 0};\n const int DJ[] = {0, 1, 0, -1};\n rep(i, h) rep(j, w) {\n if (s[i][j] != '.') continue;\n vector<int> count(26);\n rep(d, 4) {\n auto [ni, nj, v] = get(i, j, DI[d], DJ[d]);\n if (v != -1) count[v]++;\n }\n rep(k, 26) if (count[k] == 2 && !used[k]) {\n used[k] = true;\n st.emplace_back(i, j);\n break;\n }\n }\n int ans = 0;\n while (!st.empty()) {\n auto [ci, cj] = st.back();\n st.pop_back();\n vector<vector<pair<int, int>>> count(26);\n rep(d, 4) {\n auto [ni, nj, v] = get(ci, cj, DI[d], DJ[d]);\n if (v != -1) {\n count[v].emplace_back(ni, nj);\n }\n }\n bool ok = false;\n rep(k, 26) if (count[k].size() == 2) {\n ans++;\n ok = true;\n for (auto [i, j]: count[k]) {\n assert(s[i][j] == k + 'A');\n s[i][j] = '.';\n st.emplace_back(i, j);\n }\n }\n if (ok) st.emplace_back(ci, cj);\n }\n cout << 2 * ans << '\\n';\n}", "accuracy": 0.14754098360655737, "time_ms": 200, "memory_kb": 3860, "score_of_the_acc": -0.1858, "final_rank": 15 }, { "submission_id": "aoj_2299_5990598", "code_snippet": "#include<bits/stdc++.h>\nusing ll = long long;\n#define var auto\nconst char newl = '\\n';\n\nusing namespace std;\n\ntemplate<typename T1, typename T2> inline void chmin(T1& a, T2 b) { if (a > b) a = b; }\ntemplate<typename T1, typename T2> inline void chmax(T1& a, T2 b) { if (a < b) a = b; }\n\nint main() {\n int h, w;\n cin >> h >> w;\n vector<string> grid(h);\n for (var&& s : grid) cin >> s;\n\n int res = 0;\n while (true) {\n\n\n bool changed = false;\n for (int i = 0; i < h; i++) {\n for (int j = 0; j < w; j++) {\n if (grid[i][j] != '.') continue;\n vector<char*> cs;\n for (int i2 = i; i2 < h; i2++) {\n if (grid[i2][j] != '.') {\n cs.emplace_back(&grid[i2][j]);\n break;\n }\n }\n for (int i2 = i; i2 >= 0; i2--) {\n if (grid[i2][j] != '.') {\n cs.emplace_back(&grid[i2][j]);\n break;\n }\n }\n\n for (int j2 = j; j2 < w; j2++) {\n if (grid[i][j2] != '.') {\n cs.emplace_back(&grid[i][j2]);\n break;\n }\n }\n for (int j2 = j; j2 >= 0; j2--) {\n if (grid[i][j2] != '.') {\n cs.emplace_back(&grid[i][j2]);\n break;\n }\n }\n for (int i = 0; i < cs.size(); i++) {\n for (int j = i + 1; j < cs.size(); j++) {\n if (*cs[i] == *cs[j] && *cs[i] != '.') {\n *cs[i] = '.';\n *cs[j] = '.';\n res += 2;\n changed = true;\n }\n }\n }\n }\n }\n if (not changed) break;\n }\n cout << res << endl;\n}", "accuracy": 1, "time_ms": 360, "memory_kb": 3928, "score_of_the_acc": -0.2816, "final_rank": 5 }, { "submission_id": "aoj_2299_5894793", "code_snippet": "#pragma region Macros\n#include <bits/stdc++.h>\nusing namespace std;\ntemplate <class T> inline bool chmax(T &a, T b) {\n if(a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <class T> inline bool chmin(T &a, T b) {\n if(a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\n#ifdef DEBUG\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << '(' << p.first << ',' << p.second << ')';\n return os;\n}\ntemplate <class T> ostream &operator<<(ostream &os, const vector<T> &v) {\n os << '{';\n for(int i = 0; i < (int)v.size(); i++) {\n if(i) { os << ','; }\n os << v[i];\n }\n os << '}';\n return os;\n}\nvoid debugg() { cerr << endl; }\ntemplate <class T, class... Args>\nvoid debugg(const T &x, const Args &... args) {\n cerr << \" \" << x;\n debugg(args...);\n}\n#define debug(...) \\\n cerr << __LINE__ << \" [\" << #__VA_ARGS__ << \"]: \", debugg(__VA_ARGS__)\n#define dump(x) cerr << __LINE__ << \" \" << #x << \" = \" << (x) << endl\n#else\n#define debug(...) (void(0))\n#define dump(x) (void(0))\n#endif\n\nstruct Setup {\n Setup() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(15);\n }\n} __Setup;\n\nusing ll = long long;\n#define ALL(v) (v).begin(), (v).end()\n#define RALL(v) (v).rbegin(), (v).rend()\n#define FOR(i, a, b) for(int i = (a); i < int(b); i++)\n#define REP(i, n) FOR(i, 0, n)\nconst int INF = 1 << 30;\nconst ll LLINF = 1LL << 60;\nconstexpr int MOD = 1000000007;\nconst int dx[4] = {1, 0, -1, 0};\nconst int dy[4] = {0, 1, 0, -1};\n\nvoid Case(int i) { cout << \"Case #\" << i << \": \"; }\nint popcount(int x) { return __builtin_popcount(x); }\nll popcount(ll x) { return __builtin_popcountll(x); }\n#pragma endregion Macros\n\nint main() {\n int H, W;\n cin >> H >> W;\n vector<string> G(H);\n REP(i, H) cin >> G[i];\n auto is_in = [&](int i, int j) -> bool {\n return (0 <= i and i < H and 0 <= j and j < W);\n };\n\n int ans = 0;\n while(1) {\n bool upd = false;\n map<char, vector<pair<int, int>>> mp;\n REP(i, H) REP(j, W) if(G[i][j] == '.') {\n mp.clear();\n REP(dir, 4) {\n int ni = i, nj = j;\n while(is_in(ni, nj)) {\n if(G[ni][nj] != '.') {\n mp[G[ni][nj]].emplace_back(ni, nj);\n break;\n }\n ni += dx[dir], nj += dy[dir];\n }\n }\n for(const auto& [key, v] : mp) {\n if(v.size() == 2) {\n for(auto [ni, nj] : v) G[ni][nj] = '.';\n ans += 2;\n upd = true;\n } else assert(v.size() <= 1);\n }\n }\n if(!upd) break;\n }\n cout << ans << endl;\n}", "accuracy": 1, "time_ms": 840, "memory_kb": 3720, "score_of_the_acc": -0.5015, "final_rank": 8 }, { "submission_id": "aoj_2299_5894788", "code_snippet": "#pragma region Macros\n#include <bits/stdc++.h>\nusing namespace std;\ntemplate <class T> inline bool chmax(T &a, T b) {\n if(a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <class T> inline bool chmin(T &a, T b) {\n if(a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\n#ifdef DEBUG\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << '(' << p.first << ',' << p.second << ')';\n return os;\n}\ntemplate <class T> ostream &operator<<(ostream &os, const vector<T> &v) {\n os << '{';\n for(int i = 0; i < (int)v.size(); i++) {\n if(i) { os << ','; }\n os << v[i];\n }\n os << '}';\n return os;\n}\nvoid debugg() { cerr << endl; }\ntemplate <class T, class... Args>\nvoid debugg(const T &x, const Args &... args) {\n cerr << \" \" << x;\n debugg(args...);\n}\n#define debug(...) \\\n cerr << __LINE__ << \" [\" << #__VA_ARGS__ << \"]: \", debugg(__VA_ARGS__)\n#define dump(x) cerr << __LINE__ << \" \" << #x << \" = \" << (x) << endl\n#else\n#define debug(...) (void(0))\n#define dump(x) (void(0))\n#endif\n\nstruct Setup {\n Setup() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(15);\n }\n} __Setup;\n\nusing ll = long long;\n#define ALL(v) (v).begin(), (v).end()\n#define RALL(v) (v).rbegin(), (v).rend()\n#define FOR(i, a, b) for(int i = (a); i < int(b); i++)\n#define REP(i, n) FOR(i, 0, n)\nconst int INF = 1 << 30;\nconst ll LLINF = 1LL << 60;\nconstexpr int MOD = 1000000007;\nconst int dx[4] = {1, 0, -1, 0};\nconst int dy[4] = {0, 1, 0, -1};\n\nvoid Case(int i) { cout << \"Case #\" << i << \": \"; }\nint popcount(int x) { return __builtin_popcount(x); }\nll popcount(ll x) { return __builtin_popcountll(x); }\n#pragma endregion Macros\n\nint main() {\n int H, W;\n cin >> H >> W;\n vector<string> G(H);\n REP(i, H) cin >> G[i];\n auto is_in = [&](int i, int j) -> bool {\n return (0 <= i and i < H and 0 <= j and j < W);\n };\n\n int ans = 0;\n while(1) {\n bool upd = false;\n auto loop1 = [&]() {\n map<char, vector<pair<int, int>>> mp;\n REP(i, H) REP(j, W) if(G[i][j] == '.') {\n mp.clear();\n REP(dir, 4) {\n int ni = i, nj = j;\n while(is_in(ni, nj)) {\n if(G[ni][nj] != '.') {\n mp[G[ni][nj]].emplace_back(ni, nj);\n break;\n }\n ni += dx[dir], nj += dy[dir];\n }\n }\n for(const auto& [key, v] : mp) {\n if(v.size() == 2) {\n for(auto [ni, nj] : v) G[ni][nj] = '.';\n ans += 2;\n upd = true;\n } else assert(v.size() <= 1);\n }\n }\n }; loop1();\n if(!upd) break;\n }\n cout << ans << endl;\n}", "accuracy": 1, "time_ms": 840, "memory_kb": 3720, "score_of_the_acc": -0.5015, "final_rank": 8 }, { "submission_id": "aoj_2299_5050108", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nconst int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};\n\nint main(){\n cin.tie(0);\n ios::sync_with_stdio(false);\n int M,N; cin >> M >> N;\n vector<string> C(M);\n for (int i=0;i<M;++i) cin >> C[i];\n\n vector<vector<pair<int,int>>> pos(26);\n for (int i=0;i<M;++i){\n for (int j=0;j<N;++j){\n if (C[i][j]=='.') continue;\n pos[C[i][j]-'A'].emplace_back(i,j);\n }\n }\n\n vector<pair<int,int>> cand;\n for (int i=0;i<26;++i){\n if (pos[i].empty()) continue;\n auto p=pos[i][0],q=pos[i][1];\n if (p.first==q.first){\n if (p.second>q.second) swap(p,q);\n for (int j=p.second+1;j<q.second;++j){\n cand.emplace_back(p.first,j);\n }\n } else if (p.second==q.second){\n if (p.first>q.first) swap(p,q);\n for (int j=p.first+1;j<q.first;++j){\n cand.emplace_back(j,p.second);\n }\n } else {\n cand.emplace_back(p.first,q.second);\n cand.emplace_back(q.first,p.second);\n }\n }\n\n int ans=0;\n auto search=[&](int x,int y,int i){\n while (1){\n x+=dx[i]; y+=dy[i];\n if (x<0||M<=x||y<0||N<=y) break;\n if (C[x][y]=='.') continue;\n return C[x][y];\n }\n return '.';\n };\n\n auto erase=[&](int i){\n C[pos[i][0].first][pos[i][0].second]='.';\n C[pos[i][1].first][pos[i][1].second]='.';\n ans+=2;\n };\n\n auto check=[&](pair<int,int> p){\n int x=p.first,y=p.second;\n if (C[x][y]!='.') return;\n vector<char> v;\n for (int i=0;i<4;++i){\n char c=search(x,y,i);\n if (c!='.') v.emplace_back(c);\n }\n for (int i=0;i<v.size();++i){\n for (int j=i+1;j<v.size();++j){\n if (v[i]==v[j]){\n erase(v[i]-'A');\n }\n }\n }\n };\n\n for (int _=0;_<26;++_){\n for (auto p:cand){\n check(p);\n }\n }\n\n cout << ans << '\\n';\n}", "accuracy": 1, "time_ms": 170, "memory_kb": 3360, "score_of_the_acc": -0.0879, "final_rank": 2 }, { "submission_id": "aoj_2299_5050107", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nconst long long MOD=1000000007;\n// const long long MOD=998244353;\n#define LOCAL\n#pragma region Macros\ntypedef long long ll;\ntypedef __int128_t i128;\ntypedef unsigned int uint;\ntypedef unsigned long long ull;\n#define ALL(x) (x).begin(),(x).end()\nconst int INF=1e9;\nconst long long IINF=1e18;\nconst int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};\nconst char dir[4]={'D','R','U','L'};\n\ntemplate<typename T>\nistream &operator>>(istream &is,vector<T> &v){\n for (T &x:v) is >> x;\n return is;\n}\ntemplate<typename T>\nostream &operator<<(ostream &os,const vector<T> &v){\n for (int i=0;i<v.size();++i){\n os << v[i] << (i+1==v.size()?\"\": \" \");\n }\n return os;\n}\ntemplate<typename T,typename U>\nostream &operator<<(ostream &os,const pair<T,U> &p){\n os << '(' << p.first << ',' << p.second << ')';\n return os;\n}\ntemplate<typename T,typename U,typename V>\nostream&operator<<(ostream &os,const tuple<T,U,V> &t){\n os << '(' << get<0>(t) << ',' << get<1>(t) << ',' << get<2>(t) << ')';\n return os;\n}\ntemplate<typename T,typename U,typename V,typename W>\nostream&operator<<(ostream &os,const tuple<T,U,V,W> &t){\n os << '(' << get<0>(t) << ',' << get<1>(t) << ',' << get<2>(t) << ',' << get<3>(t) << ')';\n return os;\n}\ntemplate<typename T,typename U>\nostream &operator<<(ostream &os,const map<T,U> &m){\n os << '{';\n for (auto itr=m.begin();itr!=m.end();){\n os << '(' << itr->first << ',' << itr->second << ')';\n if (++itr!=m.end()) os << ',';\n }\n os << '}';\n return os;\n}\ntemplate<typename T,typename U>\nostream &operator<<(ostream &os,const unordered_map<T,U> &m){\n os << '{';\n for (auto itr=m.begin();itr!=m.end();){\n os << '(' << itr->first << ',' << itr->second << ')';\n if (++itr!=m.end()) os << ',';\n }\n os << '}';\n return os;\n}\ntemplate<typename T>\nostream &operator<<(ostream &os,const set<T> &s){\n os << '{';\n for (auto itr=s.begin();itr!=s.end();){\n os << *itr;\n if (++itr!=s.end()) os << ',';\n }\n os << '}';\n return os;\n}\ntemplate<typename T>\nostream &operator<<(ostream &os,const multiset<T> &s){\n os << '{';\n for (auto itr=s.begin();itr!=s.end();){\n os << *itr;\n if (++itr!=s.end()) os << ',';\n }\n os << '}';\n return os;\n}\ntemplate<typename T>\nostream &operator<<(ostream &os,const unordered_set<T> &s){\n os << '{';\n for (auto itr=s.begin();itr!=s.end();){\n os << *itr;\n if (++itr!=s.end()) os << ',';\n }\n os << '}';\n return os;\n}\ntemplate<typename T>\nostream &operator<<(ostream &os,const deque<T> &v){\n for (int i=0;i<v.size();++i){\n os << v[i] << (i+1==v.size()?\"\": \" \");\n }\n return os;\n}\n\nvoid debug_out(){cerr << '\\n';}\ntemplate<class Head,class... Tail>\nvoid debug_out(Head&& head,Tail&&... tail){\n cerr << head;\n if (sizeof...(Tail)>0) cerr << \", \";\n debug_out(move(tail)...);\n}\n#ifdef LOCAL\n#define debug(...) cerr << \" \";\\\ncerr << #__VA_ARGS__ << \" :[\" << __LINE__ << \":\" << __FUNCTION__ << \"]\" << '\\n';\\\ncerr << \" \";\\\ndebug_out(__VA_ARGS__)\n#else\n#define debug(...) 42\n#endif\n\ntemplate<typename T> T gcd(T x,T y){return y!=0?gcd(y,x%y):x;}\ntemplate<typename T> T lcm(T x,T y){return x/gcd(x,y)*y;}\n\ntemplate<class T1,class T2> inline bool chmin(T1 &a,T2 b){\n if (a>b){a=b; return true;} return false;\n}\ntemplate<class T1,class T2> inline bool chmax(T1 &a,T2 b){\n if (a<b){a=b; return true;} return false;\n}\n#pragma endregion\n\nint main(){\n cin.tie(0);\n ios::sync_with_stdio(false);\n int M,N; cin >> M >> N;\n vector<string> C(M);\n for (int i=0;i<M;++i) cin >> C[i];\n\n vector<vector<pair<int,int>>> pos(26);\n for (int i=0;i<M;++i){\n for (int j=0;j<N;++j){\n if (C[i][j]=='.') continue;\n pos[C[i][j]-'A'].emplace_back(i,j);\n }\n }\n\n vector<pair<int,int>> cand;\n for (int i=0;i<26;++i){\n if (pos[i].empty()) continue;\n auto p=pos[i][0],q=pos[i][1];\n if (p.first==q.first){\n if (p.second>q.second) swap(p,q);\n for (int j=p.second+1;j<q.second;++j){\n cand.emplace_back(p.first,j);\n }\n } else if (p.second==q.second){\n if (p.first>q.first) swap(p,q);\n for (int j=p.first+1;j<q.first;++j){\n cand.emplace_back(j,p.second);\n }\n } else {\n cand.emplace_back(p.first,q.second);\n cand.emplace_back(q.first,p.second);\n }\n }\n\n int ans=0;\n auto search=[&](int x,int y,int i){\n while (1){\n x+=dx[i]; y+=dy[i];\n if (x<0||M<=x||y<0||N<=y) break;\n if (C[x][y]=='.') continue;\n return C[x][y];\n }\n return '.';\n };\n\n auto erase=[&](int i){\n C[pos[i][0].first][pos[i][0].second]='.';\n C[pos[i][1].first][pos[i][1].second]='.';\n ans+=2;\n };\n\n auto check=[&](pair<int,int> p){\n int x=p.first,y=p.second;\n if (C[x][y]!='.') return;\n vector<char> v;\n for (int i=0;i<4;++i){\n char c=search(x,y,i);\n if (c!='.') v.emplace_back(c);\n }\n for (int i=0;i<v.size();++i){\n for (int j=i+1;j<v.size();++j){\n if (v[i]==v[j]){\n erase(v[i]-'A');\n }\n }\n }\n };\n\n for (int _=0;_<26;++_){\n for (auto p:cand){\n check(p);\n }\n }\n\n cout << ans << '\\n';\n}", "accuracy": 1, "time_ms": 170, "memory_kb": 3360, "score_of_the_acc": -0.0879, "final_rank": 2 }, { "submission_id": "aoj_2299_4963464", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define REP(i, n) for (int i=0; i<(n); ++i)\n#define RREP(i, n) for (int i=(int)(n)-1; i>=0; --i)\n#define FOR(i, a, n) for (int i=(a); i<(n); ++i)\n#define RFOR(i, a, n) for (int i=(int)(n)-1; i>=(a); --i)\n\n#define SZ(x) ((int)(x).size())\n#define ALL(x) (x).begin(),(x).end()\n\n#define DUMP(x) cerr<<#x<<\" = \"<<(x)<<endl\n#define DEBUG(x) cerr<<#x<<\" = \"<<(x)<<\" (L\"<<__LINE__<<\")\"<<endl;\n\ntemplate<class T>\nostream &operator<<(ostream &os, const vector<T> &v) {\n os << \"[\";\n REP(i, SZ(v)) {\n if (i) os << \", \";\n os << v[i];\n }\n return os << \"]\";\n}\n\ntemplate<class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n return os << \"(\" << p.first << \" \" << p.second << \")\";\n}\n\ntemplate<class T>\nbool chmax(T &a, const T &b) {\n if (a < b) {\n a = b;\n return true;\n }\n return false;\n}\n\ntemplate<class T>\nbool chmin(T &a, const T &b) {\n if (b < a) {\n a = b;\n return true;\n }\n return false;\n}\n\nusing ll = long long;\nusing ull = unsigned long long;\nusing ld = long double;\nusing P = pair<int, int>;\nusing vi = vector<int>;\nusing vll = vector<ll>;\nusing vvi = vector<vi>;\nusing vvll = vector<vll>;\n\nconst ll MOD = 1e9 + 7;\nconst int INF = INT_MAX / 2;\nconst ll LINF = LLONG_MAX / 2;\nconst ld eps = 1e-9;\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(10);\n\n int n, m; cin >> n >> m;\n vector<string> s(n);\n map<char, pair<P, P>> mp;\n REP(i, n) {\n cin >> s[i];\n REP(j, m) {\n if(s[i][j] != '.') {\n if(mp.find(s[i][j]) == mp.end()) {\n mp[s[i][j]] = {{i, j}, {-1, -1}};\n } else {\n mp[s[i][j]].second = {i, j};\n }\n }\n }\n }\n\n map<char, vector<P>> koho;\n for(auto &e: mp) {\n int i1, j1, i2, j2;\n tie(i1, j1) = e.second.first;\n tie(i2, j2) = e.second.second;\n\n vector<P> v;\n if(i1 == i2) {\n for(int j = min(j1, j2)+1; j < max(j1, j2);++j) {\n v.emplace_back(i1, j);\n }\n } else if(j1 == j2) {\n for(int i = min(i1, i2)+1; i < max(i1, i2);++i) {\n v.emplace_back(i, j1);\n }\n } else {\n v.emplace_back(i1, j2);\n v.emplace_back(i2, j1);\n }\n koho[e.first] = v;\n }\n\n int ans = 0;\n bool upd = true;\n while(upd) {\n upd = false;\n\n for(auto &e1: koho) {\n for(auto &e2: e1.second) {\n int i1, j1;\n tie(i1, j1) = e2;\n int i2, j2;\n tie(i2, j2) = mp[e1.first].first;\n\n bool ok = true;\n if(j1 == j2) {\n for (int i = min(i1, i2); i <= max(i1, i2); ++i) {\n ok &= s[i][j1] == '.' || s[i][j1] == e1.first;\n }\n } else {\n for(int j = min(j1, j2); j <= max(j1, j2); ++j) {\n ok &= s[i1][j] == '.' || s[i1][j] == e1.first;\n }\n }\n\n tie(i2, j2) = mp[e1.first].second;\n if(j1 == j2) {\n for (int i = min(i1, i2); i <= max(i1, i2); ++i) {\n ok &= s[i][j1] == '.' || s[i][j1] == e1.first;\n }\n } else {\n for(int j = min(j1, j2); j <= max(j1, j2); ++j) {\n ok &= s[i1][j] == '.' || s[i1][j] == e1.first;\n }\n }\n if(ok) {\n ans += 2;\n s[i2][j2] = '.';\n tie(i2, j2) = mp[e1.first].first;\n s[i2][j2] = '.';\n upd = true;\n break;\n }\n }\n\n if(upd) {\n koho.erase(e1.first);\n break;\n }\n }\n\n if(!upd) break;\n }\n\n cout << ans << endl;\n\n\n\n\n\n\n\n\n return 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3672, "score_of_the_acc": -0.065, "final_rank": 1 }, { "submission_id": "aoj_2299_4933753", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nint main() {\n clock_t start = clock();\n auto ord = [](char c) {\n if (c < 'a') {\n return int(c - 'A');\n }\n return int(26 + c - 'a');\n };\n\n int m, n;\n cin >> m >> n;\n vector<string> v(m);\n vector<bool> check(26);\n vector<int> ys(52), xs(52);\n for (int i = 0; i < m; i++) {\n cin >> v[i];\n for (int j = 0; j < n; j++) {\n char c = v[i][j];\n if (c == '.') continue;\n if (!check[ord(c)]) {\n check[ord(c)] = 1;\n c -= ('A' - 'a');\n }\n ys[ord(c)] = i, xs[ord(c)] = j;\n }\n }\n\n vector<set<int>> sty(n), stx(m);\n for (int i = 0; i < m; i++) {\n for (int j = 0; j < n; j++) {\n if (v[i][j] != '.') stx[i].insert(j);\n }\n }\n for (int j = 0; j < n; j++) {\n for (int i = 0; i < m; i++) {\n if (v[i][j] != '.') sty[j].insert(i);\n }\n }\n\n auto ERASE = [&](char a, char b) {\n int ya = ys[ord(a)], xa = xs[ord(a)];\n int yb = ys[ord(b)], xb = xs[ord(b)];\n if (ya == yb) {\n if (xa > xb) swap(xa, xb);\n if (xb - xa == 1) return 0;\n if (*stx[ya].upper_bound(xa) == xb) {\n stx[ya].erase(xa);\n stx[yb].erase(xb);\n sty[xa].erase(ya);\n sty[xb].erase(yb);\n return 1;\n }\n return 0;\n }\n if (xa == xb) {\n if (ya > yb) swap(ya, yb);\n if (yb - ya == 1) return 0;\n if (*sty[xa].upper_bound(ya) == yb) {\n stx[ya].erase(xa);\n stx[yb].erase(xb);\n sty[xa].erase(ya);\n sty[xb].erase(yb);\n return 1;\n }\n return 0;\n }\n\n if (xa > xb) {\n swap(xa, xb);\n swap(ya, yb);\n }\n if (ya > yb) {\n {\n bool ok = 1;\n auto itra = sty[xa].find(ya);\n ok &= itra == sty[xa].begin() || *(--itra) < yb;\n auto itrb = stx[yb].find(xb);\n ok &= itrb == stx[yb].begin() || *(--itrb) < xa;\n if (ok) {\n stx[ya].erase(xa);\n stx[yb].erase(xb);\n sty[xa].erase(ya);\n sty[xb].erase(yb);\n return 1;\n }\n }\n {\n bool ok = 1;\n auto itra = stx[ya].find(xa);\n ok &= itra == (--stx[ya].end()) || *(++itra) > xb;\n auto itrb = sty[xb].find(yb);\n ok &= itrb == (--sty[xb].end()) || *(++itrb) > ya;\n if (ok) {\n stx[ya].erase(xa);\n stx[yb].erase(xb);\n sty[xa].erase(ya);\n sty[xb].erase(yb);\n return 1;\n }\n }\n return 0;\n } else {\n {\n bool ok = 1;\n auto itra = sty[xa].find(ya);\n ok &= itra == (--sty[xa].end()) || *(++itra) > yb;\n auto itrb = stx[yb].find(xb);\n ok &= itrb == stx[yb].begin() || *(--itrb) < xa;\n if (ok) {\n stx[ya].erase(xa);\n stx[yb].erase(xb);\n sty[xa].erase(ya);\n sty[xb].erase(yb);\n return 1;\n }\n }\n {\n bool ok = 1;\n auto itra = stx[ya].find(xa);\n ok &= itra == (--stx[ya].end()) || *(++itra) > xb;\n auto itrb = sty[xb].find(yb);\n ok &= itrb == sty[xb].begin() || *(--itrb) < ya;\n if (ok) {\n stx[ya].erase(xa);\n stx[yb].erase(xb);\n sty[xa].erase(ya);\n sty[xb].erase(yb);\n return 1;\n }\n }\n return 0;\n }\n };\n\n queue<pair<char, char>> q;\n for (int i = 0; i < 26; i++)\n if (check[i]) q.emplace('A' + i, 'a' + i);\n\n int ans = 0;\n while (!q.empty()) {\n auto [a, b] = q.front();\n q.pop();\n if (ERASE(a, b))\n ans += 2;\n else\n q.emplace(a, b);\n clock_t end = clock();\n const double time =\n static_cast<double>(end - start) / CLOCKS_PER_SEC * 1000.0;\n if (time > 1900) break;\n }\n cout << ans << '\\n';\n}", "accuracy": 1, "time_ms": 1900, "memory_kb": 3992, "score_of_the_acc": -1.107, "final_rank": 12 }, { "submission_id": "aoj_2299_4933723", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nint main() {\n clock_t start = clock();\n auto ord = [](char c) {\n if (c < 'a') {\n return int(c - 'A');\n }\n return int(26 + c - 'a');\n };\n\n int m, n;\n cin >> m >> n;\n vector<string> v(m);\n vector<bool> check(26);\n vector<int> ys(52), xs(52);\n for (int i = 0; i < m; i++) {\n cin >> v[i];\n for (int j = 0; j < n; j++) {\n char c = v[i][j];\n if (c == '.') continue;\n if (!check[ord(c)]) {\n check[ord(c)] = 1;\n c -= ('A' - 'a');\n }\n ys[ord(c)] = i, xs[ord(c)] = j;\n }\n }\n\n vector<set<int>> sty(n), stx(m);\n for (int i = 0; i < m; i++) {\n for (int j = 0; j < n; j++) {\n if (v[i][j] != '.') stx[i].insert(j);\n }\n }\n for (int j = 0; j < n; j++) {\n for (int i = 0; i < m; i++) {\n if (v[i][j] != '.') sty[j].insert(i);\n }\n }\n\n auto erase = [&](char a, char b) {\n int ya = ys[ord(a)], xa = xs[ord(a)];\n int yb = ys[ord(b)], xb = xs[ord(b)];\n if (ya == yb) {\n if (xa > xb) swap(xa, xb);\n if (xb - xa == 1) return 0;\n if (*stx[ya].upper_bound(xa) == xb) {\n stx[ya].erase(xa);\n stx[yb].erase(xb);\n sty[xa].erase(ya);\n sty[xb].erase(yb);\n return 1;\n }\n return 0;\n }\n if (xa == xb) {\n if (ya > yb) swap(ya, yb);\n if (yb - ya == 1) return 0;\n if (*sty[xa].upper_bound(ya) == yb) {\n stx[ya].erase(xa);\n stx[yb].erase(xb);\n sty[xa].erase(ya);\n sty[xb].erase(yb);\n return 1;\n }\n return 0;\n }\n\n if (xa > xb) {\n swap(xa, xb);\n swap(ya, yb);\n }\n if (ya < yb) {\n {\n bool ok = 1;\n auto itra = sty[xa].find(ya);\n ok &= itra == sty[xa].begin() || *(--itra) < yb;\n auto itrb = stx[yb].find(xb);\n ok &= itrb == stx[yb].begin() || *(--itrb) < xa;\n if (ok) {\n stx[ya].erase(xa);\n stx[yb].erase(xb);\n sty[xa].erase(ya);\n sty[xb].erase(yb);\n return 1;\n }\n }\n {\n bool ok = 1;\n auto itra = stx[ya].find(xa);\n ok &= itra == (--stx[ya].end()) || *(++itra) > xb;\n auto itrb = sty[xb].find(yb);\n ok &= itrb == (--sty[xb].end()) || *(++itrb) > ya;\n if (ok) {\n stx[ya].erase(xa);\n stx[yb].erase(xb);\n sty[xa].erase(ya);\n sty[xb].erase(yb);\n return 1;\n }\n }\n return 0;\n } else {\n {\n bool ok = 1;\n auto itra = sty[xa].find(ya);\n ok &= itra == (--sty[xa].end()) || *(++itra) > yb;\n auto itrb = stx[yb].find(xb);\n ok &= itrb == stx[yb].begin() || *(--itrb) < xa;\n if (ok) {\n stx[ya].erase(xa);\n stx[yb].erase(xb);\n sty[xa].erase(ya);\n sty[xb].erase(yb);\n return 1;\n }\n }\n {\n bool ok = 1;\n auto itra = stx[ya].find(xa);\n ok &= itra == (--stx[ya].end()) || *(++itra) > xb;\n auto itrb = sty[xb].find(yb);\n ok &= itrb == sty[xb].begin() || *(--itrb) < ya;\n if (ok) {\n stx[ya].erase(xa);\n stx[yb].erase(xb);\n sty[xa].erase(ya);\n sty[xb].erase(yb);\n return 1;\n }\n }\n return 0;\n }\n };\n\n queue<pair<char, char>> q;\n for (int i = 0; i < 26; i++)\n if (check[i]) q.emplace('A' + i, 'a' + i);\n\n int ans = 0;\n int cnt = 0;\n while (!q.empty()) {\n auto [a, b] = q.front();\n q.pop();\n if (erase(a, b))\n ans += 2;\n else\n q.emplace(a, b);\n clock_t end = clock();\n const double time =\n static_cast<double>(end - start) / CLOCKS_PER_SEC * 1000.0;\n if (time > 1900) break;\n }\n cout << ans << '\\n';\n}", "accuracy": 0.14754098360655737, "time_ms": 1900, "memory_kb": 3752, "score_of_the_acc": -1.0676, "final_rank": 18 }, { "submission_id": "aoj_2299_4933707", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nint main() {\n clock_t start = clock();\n auto ord = [](char c) {\n if (c < 'a') {\n return int(c - 'A');\n }\n return int(26 + c - 'a');\n };\n\n int m, n;\n cin >> m >> n;\n vector<string> v(m);\n vector<bool> check(26);\n vector<int> ys(52), xs(52);\n for (int i = 0; i < m; i++) {\n cin >> v[i];\n for (int j = 0; j < n; j++) {\n char c = v[i][j];\n if (c == '.') continue;\n if (!check[ord(c)]) {\n check[ord(c)] = 1;\n c -= ('A' - 'a');\n }\n ys[ord(c)] = i, xs[ord(c)] = j;\n }\n }\n\n vector<set<int>> sty(52), stx(52);\n for (int i = 0; i < m; i++) {\n for (int j = 0; j < n; j++) {\n if (v[i][j] != '.') stx[i].insert(j);\n }\n }\n for (int j = 0; j < n; j++) {\n for (int i = 0; i < m; i++) {\n if (v[i][j] != '.') sty[j].insert(i);\n }\n }\n\n auto erase = [&](char a, char b) {\n int ya = ys[ord(a)], xa = xs[ord(a)];\n int yb = ys[ord(b)], xb = xs[ord(b)];\n if (ya == yb) {\n if (xa > xb) swap(xa, xb);\n if (xb - xa == 1) return 0;\n if (*stx[ya].upper_bound(xa) == xb) {\n stx[ya].erase(xa);\n stx[yb].erase(xb);\n sty[xa].erase(ya);\n sty[xb].erase(yb);\n return 1;\n }\n return 0;\n }\n if (xa == xb) {\n if (ya > yb) swap(ya, yb);\n if (yb - ya == 1) return 0;\n if (*sty[xa].upper_bound(ya) == yb) {\n stx[ya].erase(xa);\n stx[yb].erase(xb);\n sty[xa].erase(ya);\n sty[xb].erase(yb);\n return 1;\n }\n return 0;\n }\n\n if (xa > xb) {\n swap(xa, xb);\n swap(ya, yb);\n }\n if (ya < yb) {\n {\n bool ok = 1;\n auto itra = sty[xa].find(ya);\n ok &= itra == sty[xa].begin() || *(--itra) < yb;\n auto itrb = stx[yb].find(xb);\n ok &= itrb == stx[yb].begin() || *(--itrb) < xa;\n if (ok) {\n stx[ya].erase(xa);\n stx[yb].erase(xb);\n sty[xa].erase(ya);\n sty[xb].erase(yb);\n return 1;\n }\n }\n {\n bool ok = 1;\n auto itra = stx[ya].find(xa);\n ok &= itra == (--stx[ya].end()) || *(++itra) > xb;\n auto itrb = sty[xb].find(yb);\n ok &= itrb == (--sty[xb].end()) || *(++itrb) > ya;\n if (ok) {\n stx[ya].erase(xa);\n stx[yb].erase(xb);\n sty[xa].erase(ya);\n sty[xb].erase(yb);\n return 1;\n }\n }\n return 0;\n } else {\n {\n bool ok = 1;\n auto itra = sty[xa].find(ya);\n ok &= itra == (--sty[xa].end()) || *(++itra) > yb;\n auto itrb = stx[yb].find(xb);\n ok &= itrb == stx[yb].begin() || *(--itrb) < xa;\n if (ok) {\n stx[ya].erase(xa);\n stx[yb].erase(xb);\n sty[xa].erase(ya);\n sty[xb].erase(yb);\n return 1;\n }\n }\n {\n bool ok = 1;\n auto itra = stx[ya].find(xa);\n ok &= itra == (--stx[ya].end()) || *(++itra) > xb;\n auto itrb = sty[xb].find(yb);\n ok &= itrb == sty[xb].begin() || *(--itrb) < ya;\n if (ok) {\n stx[ya].erase(xa);\n stx[yb].erase(xb);\n sty[xa].erase(ya);\n sty[xb].erase(yb);\n return 1;\n }\n }\n return 0;\n }\n };\n\n queue<pair<char, char>> q;\n for (int i = 0; i < 26; i++)\n if (check[i]) q.emplace('A' + i, 'a' + i);\n\n int ans = 0;\n int cnt = 0;\n while (!q.empty()) {\n auto [a, b] = q.front();\n q.pop();\n if (erase(a, b))\n ans += 2;\n else\n q.emplace(a, b);\n clock_t end = clock();\n const double time =\n static_cast<double>(end - start) / CLOCKS_PER_SEC * 1000.0;\n if (time > 1000) break;\n }\n cout << ans << '\\n';\n}", "accuracy": 0.08196721311475409, "time_ms": 990, "memory_kb": 3864, "score_of_the_acc": -0.6045, "final_rank": 19 }, { "submission_id": "aoj_2299_4933703", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nint main() {\n clock_t start = clock();\n auto ord = [](char c) {\n if (c < 'a') {\n return int(c - 'A');\n }\n return int(26 + c - 'a');\n };\n\n int m, n;\n cin >> m >> n;\n vector<string> v(m);\n vector<bool> check(26);\n vector<int> ys(52), xs(52);\n for (int i = 0; i < m; i++) {\n cin >> v[i];\n for (int j = 0; j < n; j++) {\n char c = v[i][j];\n if (c == '.') continue;\n if (!check[ord(c)]) {\n check[ord(c)] = 1;\n c -= ('A' - 'a');\n }\n ys[ord(c)] = i, xs[ord(c)] = j;\n }\n }\n\n vector<set<int>> sty(52), stx(52);\n for (int i = 0; i < m; i++) {\n for (int j = 0; j < n; j++) {\n if (v[i][j] != '.') stx[i].insert(j);\n }\n }\n for (int j = 0; j < n; j++) {\n for (int i = 0; i < m; i++) {\n if (v[i][j] != '.') sty[j].insert(i);\n }\n }\n\n auto erase = [&](char a, char b) {\n int ya = ys[ord(a)], xa = xs[ord(a)];\n int yb = ys[ord(b)], xb = xs[ord(b)];\n if (ya == yb) {\n if (xa > xb) swap(xa, xb);\n if (xb - xa == 1) return 0;\n if (*stx[ya].upper_bound(xa) == xb) {\n stx[ya].erase(xa);\n stx[yb].erase(xb);\n sty[xa].erase(ya);\n sty[xb].erase(yb);\n return 1;\n }\n return 0;\n }\n if (xa == xb) {\n if (ya > yb) swap(ya, yb);\n if (yb - ya == 1) return 0;\n if (*sty[xa].upper_bound(ya) == yb) {\n stx[ya].erase(xa);\n stx[yb].erase(xb);\n sty[xa].erase(ya);\n sty[xb].erase(yb);\n return 1;\n }\n return 0;\n }\n\n if (xa > xb) {\n swap(xa, xb);\n swap(ya, yb);\n }\n if (ya < yb) {\n {\n bool ok = 1;\n auto itra = sty[xa].find(ya);\n ok &= itra == sty[xa].begin() || *(--itra) < yb;\n auto itrb = stx[yb].find(xb);\n ok &= itrb == stx[yb].begin() || *(--itrb) < xa;\n if (ok) {\n stx[ya].erase(xa);\n stx[yb].erase(xb);\n sty[xa].erase(ya);\n sty[xb].erase(yb);\n return 1;\n }\n }\n {\n bool ok = 1;\n auto itra = stx[ya].find(xa);\n ok &= itra == (--stx[ya].end()) || *(++itra) > xb;\n auto itrb = sty[xb].find(yb);\n ok &= itrb == (--sty[xb].end()) || *(++itrb) > ya;\n if (ok) {\n stx[ya].erase(xa);\n stx[yb].erase(xb);\n sty[xa].erase(ya);\n sty[xb].erase(yb);\n return 1;\n }\n }\n return 0;\n } else {\n {\n bool ok = 1;\n auto itra = sty[xa].find(ya);\n ok &= itra == (--sty[xa].end()) || *(++itra) > yb;\n auto itrb = stx[yb].find(xb);\n ok &= itrb == stx[yb].begin() || *(--itrb) < xa;\n if (ok) {\n stx[ya].erase(xa);\n stx[yb].erase(xb);\n sty[xa].erase(ya);\n sty[xb].erase(yb);\n return 1;\n }\n }\n {\n bool ok = 1;\n auto itra = stx[ya].find(xa);\n ok &= itra == (--stx[ya].end()) || *(++itra) > xb;\n auto itrb = sty[xb].find(yb);\n ok &= itrb == sty[xb].begin() || *(--itrb) < ya;\n if (ok) {\n stx[ya].erase(xa);\n stx[yb].erase(xb);\n sty[xa].erase(ya);\n sty[xb].erase(yb);\n return 1;\n }\n }\n return 0;\n }\n };\n\n queue<pair<char, char>> q;\n for (int i = 0; i < 26; i++)\n if (check[i]) q.emplace('A' + i, 'a' + i);\n\n int ans = 0;\n int cnt = 0;\n while (!q.empty()) {\n auto [a, b] = q.front();\n q.pop();\n if (erase(a, b))\n ans += 2;\n else\n q.emplace(a, b);\n clock_t end = clock();\n const double time =\n static_cast<double>(end - start) / CLOCKS_PER_SEC * 1000.0;\n if (time > 1900) break;\n }\n cout << ans << '\\n';\n}", "accuracy": 0.08196721311475409, "time_ms": 1890, "memory_kb": 3848, "score_of_the_acc": -1.078, "final_rank": 20 }, { "submission_id": "aoj_2299_4911849", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing Pii = pair<int, int>;\n\nint dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};\n\nint H, W;\nvector<string> grid;\nvector<int> erase_c[501][501];\nvector<Pii> c[26];\nint ans = 0;\nbool used[501][501];\n\nint main() {\n cin >> H >> W;\n grid.resize(H);\n\n for ( int i = 0; i < H; i++ ) {\n cin >> grid[i];\n for ( int j = 0; j < W; j++ ) {\n if ( grid[i][j] == '.' ) continue;\n c[grid[i][j]-'A'].push_back(Pii(i, j)); \n }\n } \n\n bool flag = true;\n while ( flag ) {\n flag = false;\n for ( int i = 0; i < H; i++ ) {\n for ( int j = 0; j < W; j++ ) {\n\tif ( grid[i][j] != '.' ) continue;\n\tvector<int> cnt(26, 0); \n\tfor ( int k = 0; k < 4; k++ ) {\n\t int nx = j+dx[k], ny = i+dy[k];\n\t while ( 1 ) {\n\t if ( nx < 0 || ny < 0 || nx >= W || ny >= H ) break;\n\t if ( grid[ny][nx] != '.' ) {\n\t cnt[grid[ny][nx]-'A']++;\n\t break;\n\t }\n\t nx += dx[k]; ny += dy[k];\n\t }\n\t}\n\n\tfor ( int k = 0; k < 26; k++ ) {\n\t if ( cnt[k] == 2 ) {\n\t ans += 2;\n\t for ( Pii p: c[k] ) grid[p.first][p.second] = '.';\n\t flag = true;\n\t break;\t \n\t }\n\t}\n }\n }\n }\n\n cout << ans << endl; \n \n return 0;\n}", "accuracy": 1, "time_ms": 1140, "memory_kb": 9252, "score_of_the_acc": -1.5677, "final_rank": 14 }, { "submission_id": "aoj_2299_4908696", "code_snippet": "#include <bits/stdc++.h> \nusing namespace std;\n#define FOR(i,l,r) for(int i=(l); i<(r); i++)\n#define REP(i,n) FOR(i,0,n)\n#define int long long \n#define endl \"\\n\"\n#define debug(x) cout<<x<<endl;\nstatic const int INF = 1e9+7;\n\nint N,M;\nchar C[550][550];\nvector<pair<int,int> > p;\n\nint search(int x, int y){\n map<char,int> mp;\n\n //左を見る\n for(int j=x-1; j>=0; j--){\n if(C[y][j] != '.'){\n mp[C[y][j]]++;\n break;\n }\n }\n\n //右を見る\n for(int j=x+1; j<N; j++){\n if(C[y][j] != '.'){\n mp[C[y][j]]++;\n break;\n }\n }\n\n //下を見る\n for(int i=y+1; i<M; i++){\n if(C[i][x] != '.'){\n mp[C[i][x]]++;\n break;\n }\n }\n\n for(int i=y-1; i>=0; i--){\n if(C[i][x] != '.'){\n mp[C[i][x]]++;\n break;\n }\n }\n\n int add = 0;\n\n for(auto a : mp){\n if(a.second == 2){\n for(int i=0; i<M; i++){\n for(int j=0; j<N; j++){\n if(C[i][j] == a.first){\n C[i][j] = '.';\n p.push_back({i,j});\n }\n }\n }\n add += 2;\n }\n }\n\n return add;\n}\n\nsigned main(){\n cin.tie(0);\n ios::sync_with_stdio(false);\n\n cin>>M>>N;\n\n REP(i,M){\n REP(j,N){\n cin>>C[i][j];\n if(C[i][j] == '.') p.push_back({i,j});\n }\n }\n\n int ans = 0;\n\n while(1){\n bool flag = false;\n\n for(auto a : p){\n int y = a.first;\n int x = a.second;\n int add;\n\n if((add = search(x,y))){\n flag = true;\n ans += add;\n }\n }\n\n if(!flag) break;\n\n /*REP(i,M){\n REP(j,N) debug(C[i][j]);\n }*/\n }\n\n /*for(auto a : p){\n debug(a.first);\n debug(a.second);\n }*/\n\n cout<<ans<<endl;\n}", "accuracy": 1, "time_ms": 310, "memory_kb": 9436, "score_of_the_acc": -1.1587, "final_rank": 13 }, { "submission_id": "aoj_2299_4907609", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing Pii = pair<int, int>;\n\nint H, W;\nvector<string> grid;\nvector<int> erase_c[501][501];\nvector<Pii> c[26];\nint ans = 0;\nbool used[501][501];\n\nvoid dfs(int h, int w) {\n if ( used[h][w] ) return;\n if ( grid[h][w] != '.' ) ans++;\n used[h][w] = true;\n // cout << h << \" \" << w << \" \" << grid[h][w] << endl; \n\n for ( int a: erase_c[h][w] ) {\n for ( Pii p: c[a] ) {\n int y = p.first, x = p.second;\n if ( used[y][x] ) continue;\n dfs(y, x); \n }\n }\n}\n\nint main() {\n cin >> H >> W;\n grid.resize(H);\n\n for ( int i = 0; i < H; i++ ) {\n cin >> grid[i];\n for ( int j = 0; j < W; j++ ) {\n if ( grid[i][j] == '.' ) continue;\n c[grid[i][j]-'A'].push_back(Pii(i, j)); \n }\n } \n\n for ( int a = 0; a < 26; a++ ) { \n if ( c[a].size() == 0 ) continue;\n\n for ( int i = 0; i < H; i++ ) {\n for ( int j = 0; j < W; j++ ) {\n\tif ( (i == c[a][0].first && j == c[a][1].second) || \n\t (i == c[a][1].first && j == c[a][0].second) ||\n\t (i == c[a][0].first && i == c[a][1].first) ||\n\t (j == c[a][0].second && j == c[a][1].second) ) {\n\t // cout << \" \" << a << endl;\t \n\t if ( c[a][0].first == c[a][1].first ) {\n\t if ( (c[a][0].second <= j && j <= c[a][1].second ) ||\n\t\t (c[a][1].second <= j && j <= c[a][0].second ) ) {\n\t // cout << i << \" \" << j << \" \" << a << endl;\t \n\t erase_c[i][j].push_back(a);\n\t }\n\t } else if ( c[a][0].first == c[a][1].first ) {\t \n\t if ( (c[a][0].first <= i && i <= c[a][1].first ) ||\n\t\t (c[a][1].first <= i && i <= c[a][0].first ) ) erase_c[i][j].push_back(a);\n\t }\n\t else erase_c[i][j].push_back(a);\n\t \n\t}\n }\n }\n }\n\n for ( int i = 0; i < H; i++ ) {\n for ( int j = 0; j < W; j++ ) {\n if ( used[i][j] || grid[i][j] != '.' ) continue;\n dfs(i, j);\n }\n }\n\n cout << ans << endl; \n \n return 0;\n}", "accuracy": 0.14754098360655737, "time_ms": 10, "memory_kb": 9280, "score_of_the_acc": -0.9744, "final_rank": 16 }, { "submission_id": "aoj_2299_4907604", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing Pii = pair<int, int>;\n\nint H, W;\nvector<string> grid;\nvector<int> erase_c[501][501];\nvector<Pii> c[26];\nint ans = 0;\nbool used[501][501];\n\nvoid dfs(int h, int w) {\n if ( used[h][w] ) return;\n if ( grid[h][w] != '.' ) ans++;\n used[h][w] = true;\n // cout << h << \" \" << w << \" \" << grid[h][w] << endl; \n\n for ( int a: erase_c[h][w] ) {\n for ( Pii p: c[a] ) {\n int y = p.first, x = p.second;\n if ( used[y][x] ) continue;\n dfs(y, x); \n }\n }\n}\n\nint main() {\n cin >> H >> W;\n grid.resize(H);\n\n for ( int i = 0; i < H; i++ ) {\n cin >> grid[i];\n for ( int j = 0; j < W; j++ ) {\n if ( grid[i][j] == '.' ) continue;\n c[grid[i][j]-'A'].push_back(Pii(i, j)); \n }\n } \n\n for ( int a = 0; a < 26; a++ ) { \n if ( c[a].size() == 0 ) continue;\n\n for ( int i = 0; i < H; i++ ) {\n for ( int j = 0; j < W; j++ ) {\n\tif ( (i == c[a][0].first && j == c[a][1].second) || \n\t (i == c[a][1].first && j == c[a][0].second) ||\n\t (i == c[a][0].first && i == c[a][1].first) ||\n\t (j == c[a][0].second && j == c[a][1].second) ) {\n\t // cout << \" \" << a << endl;\t \n\t if ( c[a][0].first == c[a][1].first ) {\n\t if ( (c[a][0].second <= j && j <= c[a][1].second ) ||\n\t\t (c[a][1].second <= j && j <= c[a][0].second ) ) {\n\t // cout << i << \" \" << j << \" \" << a << endl;\t \n\t erase_c[i][j].push_back(a);\n\t }\n\t } else if ( c[a][0].first == c[a][1].first ) {\t \n\t if ( (c[a][0].first <= j && j <= c[a][1].first ) ||\n\t\t (c[a][1].first <= j && j <= c[a][0].first ) ) erase_c[i][j].push_back(a);\n\t }\n\t else erase_c[i][j].push_back(a);\n\t \n\t}\n }\n }\n }\n\n for ( int i = 0; i < H; i++ ) {\n for ( int j = 0; j < W; j++ ) {\n if ( used[i][j] || grid[i][j] != '.' ) continue;\n dfs(i, j);\n }\n }\n\n cout << ans << endl; \n \n return 0;\n}", "accuracy": 0.14754098360655737, "time_ms": 10, "memory_kb": 9420, "score_of_the_acc": -0.9974, "final_rank": 17 }, { "submission_id": "aoj_2299_4907515", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#define int long long\n#define double long double\n#define lp(i,n) for(int i=0;i<(int)(n);i++)\n#define lps(i,j,n) for(int i=j;i<n;i++)\n\n#define fordebug int hoge;cin>>hoge;\n\n#define DEKAI 1000000007\n#define floot10 cout<<fixed<<setprecision(15)\n#define all(v) v.begin(),v.end()\ndouble PI = acos(-1);\n\nint h,w;\nint score=0;\nbool act(vector<string> &v,int x, int y){\n bool res = false;\n int dx[4]={0,0,1,-1};\n int dy[4]={1,-1,0,0};\n map<char,int> m;\n vector<char> check={};\n lp(i,4){\n int nx=x;\n int ny=y;\n while(1){\n nx+=dx[i];\n ny+=dy[i];\n if(nx<0||nx>=h)break;\n if(ny<0||ny>=w)break;\n if(v[nx][ny]!='.'){\n char c=v[nx][ny];\n if(m[c]==0)m[c]=1;\n else{\n check.push_back(c);\n }\n break;\n }\n }\n }\n lp(i,4){\n int nx=x;\n int ny=y;\n while(1){\n nx+=dx[i];\n ny+=dy[i];\n if(nx<0||nx>=h)break;\n if(ny<0||ny>=w)break;\n if(v[nx][ny]!='.'){\n char c=v[nx][ny];\n lp(j,check.size()){\n if(c==check[j]){\n v[nx][ny]='.';\n res=true;\n score++;\n }\n }\n break;\n }\n }\n }\n return res;\n}\n\nvoid priv(vector<string> &v){\n lp(i,h){\n cout<<v[i]<<endl;\n }\n}\n\nsigned main(){\n cin>>h>>w;\n vector<string> v(h);\n lp(i,h){\n cin>>v[i];\n }\n while(1){\n bool check = false;\n //priv(v);\n //cout<<score<<endl<<endl;\n lp(i,h){\n lp(j,w){\n if(v[i][j]!='.')continue;\n if(act(v,i,j))check=true;\n }\n }\n if(check==false)break;\n }\n cout<<score<<endl;\n return 0;\n}", "accuracy": 1, "time_ms": 1120, "memory_kb": 3920, "score_of_the_acc": -0.6824, "final_rank": 11 }, { "submission_id": "aoj_2299_4890528", "code_snippet": "#include <iostream>\n#include <string>\n#include <cstdlib>\n#include <cmath>\n#include <vector>\n#include <unordered_map>\n#include <map>\n#include <set>\n#include <algorithm>\n#include <queue>\n#include <stack>\n#include <functional>\n#include <bitset>\n#include <assert.h>\n#include <unordered_map>\n#include <fstream>\n#include <ctime>\n#include <complex>\nusing namespace std;\ntypedef long long ll;\ntypedef vector<ll> vl;\ntypedef vector<vl> vvl;\ntypedef vector<char> vc;\ntypedef vector<string> vs;\ntypedef vector<bool> vb;\ntypedef vector<double> vd;\ntypedef pair<ll,ll> P;\ntypedef pair<int,int> pii;\ntypedef vector<P> vpl;\ntypedef tuple<ll,ll,ll> tapu;\n#define rep(i,n) for(int i=0; i<(n); i++)\n#define REP(i,a,b) for(int i=(a); i<(b); i++)\n#define all(x) (x).begin(), (x).end()\n#define rall(x) (x).rbegin(), (x).rend()\nconst int inf = 1<<30;\nconst ll linf = 1LL<<62;\nconst int MAX = 2020000;\nll dy[8] = {1,-1,0,0,1,-1,1,-1};\nll dx[8] = {0,0,1,-1,1,-1,-1,1};\nconst double pi = acos(-1);\nconst double eps = 1e-10;\ntemplate<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){\n\tif(a>b){\n\t\ta = b; return true;\n\t}\n\telse return false;\n}\ntemplate<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){\n\tif(a<b){\n\t\ta = b; return true;\n\t}\n\telse return false;\n}\ntemplate<typename T> inline void print(T &a){\n for(auto itr = a.begin(); itr != a.end(); itr++){\n\t\tcout << *itr << \" \";\n\t}\n cout << \"\\n\";\n}\ntemplate<typename T1,typename T2> inline void print2(T1 a, T2 b){\n\tcout << \"debug: \" << a << \" \" << b << \"\\n\";\n}\ntemplate<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){\n\tcout << \"debug: \" << a << \" \" << b << \" \" << c << \"\\n\";\n}\nvoid mark() {cout << \"#\" << \"\\n\";}\nll pcount(ll x) {return __builtin_popcountll(x);}\n//const int mod = 1e9 + 7;\nconst int mod = 998244353;\n\nbool in(ll y, ll x, ll h, ll w){\n\treturn 0 <= y && y < h && 0 <= x && x < w;\n}\n\nint main(){\n\tint n,m; cin >> n >> m;\n\tvs s(n); rep(i,n) cin >> s[i];\n\tvector<vpl> pos(26,vpl(2,{-1,-1}));\n\trep(i,n){\n\t\trep(j,m){\n\t\t\tif(s[i][j] == '.') continue;\n\t\t\tif(pos[s[i][j]-'A'][0].first == -1){\n\t\t\t\tpos[s[i][j]-'A'][0] = {i,j};\n\t\t\t}else{\n\t\t\t\tpos[s[i][j]-'A'][1] = {i,j};\n\t\t\t}\n\t\t}\n\t}\n\tint ans = 0;\n\twhile(1){\n\t\tbool f = false;\n\t\trep(i,n){\n\t\t\trep(j,m){\n\t\t\t\tif(s[i][j] != '.') continue;\n\t\t\t\tvl cnt(26,0);\n\t\t\t\trep(k,4){\n\t\t\t\t\tfor(int t=0;; t++){\n\t\t\t\t\t\tint ny = i + dy[k] * t;\n\t\t\t\t\t\tint nx = j + dx[k] * t;\n\t\t\t\t\t\tif(!in(ny,nx,n,m)) break;\n\t\t\t\t\t\tif(s[ny][nx] != '.'){\n\t\t\t\t\t\t\tcnt[s[ny][nx]-'A']++;\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\trep(k,26){\n\t\t\t\t\tif(cnt[k] == 2){\n\t\t\t\t\t\trep(l,2) s[pos[k][l].first][pos[k][l].second] = '.';\n\t\t\t\t\t\tans += 2;\n\t\t\t\t\t\tf = true;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif(!f) break;\n\t}\n\tcout << ans << \"\\n\";\n}", "accuracy": 1, "time_ms": 1290, "memory_kb": 3340, "score_of_the_acc": -0.6772, "final_rank": 10 }, { "submission_id": "aoj_2299_4849452", "code_snippet": "#include <stdio.h>\n#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i,n) for (int i = 0; i < (n); ++i)\n#define Inf 1000000000\nint ans = 0;\n\nvoid del(vector<string> &S,int y,int x){\n\tif(S[y][x]!='.')return;\n\tvector<int> dy = {0,-1,0,1},dx = {1,0,-1,0};\n\trep(i,4){\n\t\trep(j,4){\n\t\t\tif(i==j)continue;\n\t\t\t\n\t\t\tint y1 = y,x1 = x,y2 = y,x2 = x;\n\t\t\twhile(true){\n\t\t\t\ty1 += dy[i];\n\t\t\t\tx1 += dx[i];\n\t\t\t\tif(y1<0||x1<0||y1>=S.size()||x1>=S[0].size()){\n\t\t\t\t\ty1 -= dy[i];\n\t\t\t\t\tx1 -= dx[i];\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tif(S[y1][x1]!='.')break;\n\t\t\t}\n\t\t\t\n\t\t\twhile(true){\n\t\t\t\ty2 += dy[j];\n\t\t\t\tx2 += dx[j];\n\t\t\t\tif(y2<0||x2<0||y2>=S.size()||x2>=S[0].size()){\n\t\t\t\t\ty2 -= dy[j];\n\t\t\t\t\tx2 -= dx[j];\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tif(S[y2][x2]!='.')break;\n\t\t\t}\n\t\t\t\n\t\t\tif(S[y1][x1] == S[y2][x2] && S[y1][x1]!='.'){\n\t\t\t\tans += 2;\n\t\t\t\tS[y1][x1] = '.';\n\t\t\t\tS[y2][x2] = '.';\n\t\t\t}\n\t\t\t\n\t\t}\n\t}\n}\n\nint main(){\n\t\n\tint M,N;\n\tcin>>M>>N;\n\t\n\tvector<string> S;\n\t{\n\t\tbool f = false;\n\t\trep(i,M){\n\t\t\tstring s;\n\t\t\tcin>>s;\n\t\t\tif(s==string(N,'.')){\n\t\t\t\tif(f){\n\t\t\t\t\tcontinue;\n\t\t\t\t}\t\n\t\t\t\tf=true;\n\t\t\t}\n\t\t\telse{\n\t\t\t\tf=false;\n\t\t\t}\n\t\t\tS.push_back(s);\n\t\t}\n\t}\n\n\t{\n\t\tbool F = false;\n\t\tvector<string> T(S.size(),\"\");\n\t\trep(i,N){\n\t\t\tbool f = false;\n\t\t\trep(j,S.size()){\n\t\t\t\tif(S[j][i]!='.')f=true;\n\t\t\t}\n\t\t\tif(!f){\n\t\t\t\tif(F){\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tF=true;\n\t\t\t}\n\t\t\telse{\n\t\t\t\tF = false;\n\t\t\t}\n\t\t\trep(j,S.size()){\n\t\t\t\tT[j] += S[j][i];\n\t\t\t}\n\t\t}\n\t\tswap(S,T);\n\t}\n\t//cout<<S.size()<<','<<S[0].size()<<endl;\n\trep(_,27){\n\t\trep(i,S.size()){\n\t\t\trep(j,S[i].size()){\n\t\t\t\tdel(S,i,j);\n\t\t\t}\n\t\t}\n\t}\n\t\n\tcout<<ans<<endl;\n\t\n return 0;\n}", "accuracy": 1, "time_ms": 570, "memory_kb": 3516, "score_of_the_acc": -0.3252, "final_rank": 7 } ]
aoj_2300_cpp
Calender Colors Taro is a member of a programming contest circle. In this circle, the members manage their schedules in the system called Great Web Calender . Taro has just added some of his friends to his calendar so that he can browse their schedule on his calendar. Then he noticed that the system currently displays all the schedules in only one color, mixing the schedules for all his friends. This is difficult to manage because it's hard to tell whose schedule each entry is. But actually this calender system has the feature to change the color of schedule entries, based on the person who has the entries. So Taro wants to use that feature to distinguish their plans by color. Given the colors Taro can use and the number of members, your task is to calculate the subset of colors to color all schedule entries. The colors are given by "Lab color space". In Lab color space, the distance between two colors is defined by the square sum of the difference of each element. Taro has to pick up the subset of colors that maximizes the sum of distances of all color pairs in the set. Input The input is like the following style. N M L_{0} a_{0} b_{0} L_{1} a_{1} b_{1} ... L_{N-1} a_{N-1} b_{N-1} The first line contains two integers N and M ( 0 \leq M \leq N \leq 20 ), where N is the number of colors in the input, and M is the number of friends Taro wants to select the colors for. Each of the following N lines contains three decimal integers L ( 0.0 \leq L \leq 100.0 ), a ( -134.0 \leq a \leq 220.0 ) and b ( -140.0 \leq b \leq 122.0 ) which represents a single color in Lab color space. Output Output the maximal of the total distance. The output should not contain an error greater than 10^{-5} . Sample Input 1 3 2 0 0 0 10 10 10 100 100 100 Output for the Sample Input 1 30000.00000000000000000000 Sample Input 2 5 3 12.0 15.0 9.0 10.0 -3.0 2.2 3.5 6.8 9.0 2.1 4.4 5.9 1.2 4.0 -5.4 Output for the Sample Input 2 1003.44000000000005456968 Sample Input 3 2 1 1.0 1.0 1.0 0.0 0.0 0.0 Output for the Sample Input 3 0.00000000000000000000
[ { "submission_id": "aoj_2300_11047158", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing a2 = array<int, 2>;\nusing a3 = array<ll, 3>;\n\nbool chmin(auto& a, const auto& b) { return a > b ? a = b, 1 : 0; }\nbool chmax(auto& a, const auto& b) { return a < b ? a = b, 1 : 0; }\n\nll mod = 998244353;\nconst ll INF = 1e18;\n\nifstream in;\nofstream out;\n\nint main(int argc, char** argv) {\n ios::sync_with_stdio(false);\n cin.tie(0);\n\n if(argc > 2) {\n in.open(argv[1]);\n cin.rdbuf(in.rdbuf());\n out.open(argv[2]);\n cout.rdbuf(out.rdbuf());\n }\n\n ll n, m;\n cin >> n >> m;\n\n using ld = long double;\n using d3 = array<ld, 3>;\n\n vector<d3> v(n);\n for(auto& x : v) cin >> x[0] >> x[1] >> x[2];\n\n ld ans = 0;\n for(unsigned i = 0; i < (1 << n); i++) {\n if(popcount(i) == m) {\n ld sum = 0;\n for(int j = 0; j < n; j++) {\n for(int k = j + 1; k < n; k++) {\n if(((1 << j) & i) && ((1 << k) & i)) {\n for(int l = 0; l < 3; l++)\n sum += pow(v[j][l] - v[k][l], 2);\n }\n }\n }\n chmax(ans, sum);\n }\n }\n\n cout << fixed << setprecision(15) << ans << endl;\n\n return 0;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 3840, "score_of_the_acc": -0.1224, "final_rank": 12 }, { "submission_id": "aoj_2300_10888351", "code_snippet": "#include<bits/stdc++.h>\n// #include<atcoder/all>\n// #include<boost/multiprecision/cpp_int.hpp>\n\nusing namespace std;\n// using namespace atcoder;\n// using bint = boost::multiprecision::cpp_int;\nusing ll = long long;\nusing ull = unsigned long long;\nusing ld = long double;\nusing pii = pair<int,int>;\nusing pll = pair<ll,ll>;\nusing vi = vector<ll>;\nusing vvi = vector<vi>;\nusing vvvi = vector<vvi>;\nusing ve = vector<vector<int>>;\nusing vb = vector<bool>;\nusing vvb = vector<vb>;\n#define rep(i,s,n) for(ll i = (ll)s;i < (ll)n;i++)\n#define rrep(i,l,r) for(ll i = r-1;i >= l;i--)\n#define ALL(x) (x).begin(),(x).end()\n#define sz(c) ((ll)(c).size())\n#define LB(A,x) (int)(lower_bound(A.begin(),A.end(),x)-A.begin())\n#define UB(A,x) (int)(upper_bound(A.begin(),A.end(),x)-A.begin())\n// #define MOD 1000000007\n#define MOD 998244353\ntemplate<typename T>using min_priority_queue=priority_queue<T,vector<T>,greater<T>>;\ntemplate<typename T>ostream&operator<<(ostream&os,vector<T>&v){for(int i = 0;i < v.size();i++)os<<v[i]<<(i+1!=v.size()?\" \":\"\");return os;}\ntemplate<typename T>istream&operator>>(istream&is,vector<T>&v){for(T&in:v)is>>in;return is;}\ntemplate<typename T1,typename T2>ostream&operator<<(ostream&os,pair<T1,T2>&p){os<<p.first<<\" \"<<p.second;return os;}\ntemplate<typename T1,typename T2>istream&operator>>(istream&is,pair<T1,T2>&p){is>>p.first>>p.second;return is;}\ntemplate<typename T> inline bool chmax(T &a,T b){if(a < b){a = b;return true;}return false;}\ntemplate<typename T> inline bool chmin(T &a,T b){if(a > b){a = b;return true;}return false;}\nld dist(ld x1,ld y1,ld x2, ld y2){return sqrtl((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));}\n\n\nint main(){\n \n ios_base::sync_with_stdio(0), cin.tie(0);\n int n,m;cin >> n >> m;\n using vd = vector<ld>;\n vector<vd> v(n,vd(3));\n cin >> v;\n ld res = 0;\n rep(bit,0,1 << n){\n if(__builtin_popcount(bit) != m)continue;\n ld C = 0;\n vi cand;\n rep(i,0,n)if(bit >> i & 1)cand.emplace_back(i);\n rep(i,0,sz(cand))rep(j,0,i)rep(k,0,3)C += (v[cand[i]][k]-v[cand[j]][k])*(v[cand[i]][k]-v[cand[j]][k]);\n chmax(res,C);\n }\n cout << fixed << setprecision(20);\n cout << res << endl;\n \n\n return 0;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 3620, "score_of_the_acc": -0.1089, "final_rank": 11 }, { "submission_id": "aoj_2300_10850467", "code_snippet": "#include<bits/stdc++.h>\ntemplate <class T>\ninline bool rd(T &ret) {\n char c; int sgn;\n if(c=getchar(),c==EOF) return 0;\n while(c!='-'&&(c<'0'||c>'9')) c=getchar();\n sgn=(c=='-')?-1:1;\n ret=(c=='-')?0:(c-'0');\n while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0');\n ret*=sgn;\n return 1;\n}\ntemplate <class T>\ninline void pt(T x) {\n if (x <0) {\n putchar('-');\n x = -x;\n }\n if(x>9) pt(x/10);\n putchar(x%10+'0');\n}\nusing namespace std;\nconst int N = 21;\nstruct node{\n double x, y, z;\n}a[N];\ndouble d[N][N];\ndouble dis(node u, node v){\n u.x -= v.x;\n u.y -= v.y;\n u.z -= v.z;\n return u.x*u.x+u.y*u.y+u.z*u.z;\n}\nint n, m;\ndouble ans;\nint st[N], top;\nvoid work(){\n ans = 0;\n for(int i = 1; i < (1<<n); i++)\n {\n top = 0;\n for(int j = 0; j < n; j++){\n if(i & (1<<j))\n st[top++] = j;\n }\n if(top!=m)continue;\n\n double tmp = 0;\n for(int j = 0; j < top; j++)\n for(int k = j +1; k < top; k++)\n tmp += d[st[j]][st[k]];\n // cout<<i;for(int j = 0; j < top; j++)printf(\" %d\", st[j]);cout<<\" :\"<<tmp;puts(\"\");\n ans = max(ans, tmp);\n }\n}\nvoid input(){\n for(int i = 0; i < n; i++)scanf(\"%lf %lf %lf\", &a[i].x, &a[i].y, &a[i].z);\n for(int i = 0; i < n; i++){\n for(int j = 0; j < n; j++){\n d[i][j] = dis(a[i], a[j]);\n // printf(\"%f \",d[i][j]);\n }\n // puts(\"\");\n }\n}\nint main(){\n while(~scanf(\"%d %d\", &n, &m)){\n input();\n work();\n printf(\"%.10f\\n\", ans);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3608, "score_of_the_acc": -0.086, "final_rank": 8 }, { "submission_id": "aoj_2300_10506476", "code_snippet": "#include <iostream>\n#include <iomanip>\n#include <cassert>\n#include <vector>\n#include <algorithm>\n#include <utility>\n#include <numeric>\n#include <tuple>\n#include <ranges>\nnamespace ranges = std::ranges;\nnamespace views = std::views;\n// #include \"Src/Number/IntegerDivision.hpp\"\n// #include \"Src/Utility/BinarySearch.hpp\"\n// #include \"Src/Sequence/CompressedSequence.hpp\"\n// #include \"Src/Sequence/RunLengthEncoding.hpp\"\n// #include \"Src/Algebra/Group/AdditiveGroup.hpp\"\n// #include \"Src/DataStructure/FenwickTree/FenwickTree.hpp\"\n// #include \"Src/DataStructure/SegmentTree/SegmentTree.hpp\"\n// using namespace zawa;\n// #include \"atcoder/modint\"\n// using mint = atcoder::modint998244353;\nint N, M;\nlong double L[20], A[20], B[20];\nint main() {\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n std::ios::sync_with_stdio(false);\n std::cin >> N >> M;\n for (int i = 0 ; i < N ; i++) std::cin >> L[i] >> A[i] >> B[i];\n std::vector<int> a(N);\n for (int i = 0 ; i < M ; i++) a[N - i - 1] = 1;\n long double ans = 0;\n do {\n std::vector<int> use;\n use.reserve(M);\n for (int i = 0 ; i < N ; i++) if (a[i]) use.push_back(i);\n assert(std::ssize(use) == M);\n long double v = 0;\n for (int i = 0 ; i < M ; i++) for (int j = i + 1 ; j < M ; j++) {\n v += (L[use[i]] - L[use[j]]) * (L[use[i]] - L[use[j]]);\n v += (A[use[i]] - A[use[j]]) * (A[use[i]] - A[use[j]]);\n v += (B[use[i]] - B[use[j]]) * (B[use[i]] - B[use[j]]);\n }\n ans = std::max(ans, v);\n } while (std::next_permutation(a.begin(), a.end()));\n std::cout << std::fixed << std::setprecision(8) << ans << '\\n';\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3840, "score_of_the_acc": -0.0557, "final_rank": 3 }, { "submission_id": "aoj_2300_10037567", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define all(a) a.begin(),a.end()\n#define reps(i, a, n) for (int i = (a); i < (int)(n); i++)\n#define rep(i, n) reps(i, 0, n)\n#define rreps(i, a, n) for (int i = (a); i > (int)(n); i--)\nconst long long mod = 1000000007;\nconst long long INF = 1e18;\nll myceil(ll a, ll b) {return (a+b-1)/b;}\n\n\nvoid solve() {\n\n cout << setprecision(10) << fixed;\n\n int n,m;\n cin >> n >> m;\n vector<vector<double>> v(n,vector<double> (3));\n rep(i,n) {\n cin >> v[i][0] >> v[i][1] >> v[i][2];\n }\n\n double ans = 0;\n double temp;\n\n for (unsigned int msk = 0; msk < 1<<n; msk++) {\n if (popcount(msk) != m) continue;\n temp = 0;\n rep(i,n) {\n while (i<n && ((msk & 1<<i) == 0)) {\n i++;\n }\n if (i == n) break;\n reps(j,i+1,n) {\n while (j<n && ((msk & 1<<j) == 0)) {\n j++;\n }\n if (j == n) break;\n\n rep(k,3) {\n temp += (v[i][k]-v[j][k])*(v[i][k]-v[j][k]);\n }\n }\n }\n ans = max(ans,temp);\n }\n\n cout << ans << endl;\n return;\n}\n\nint main() {\n cin.tie(nullptr);\n ios_base::sync_with_stdio(false);\n int t = 1;\n //cin >> t;\n rep(i,t) {\n solve();\n }\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3840, "score_of_the_acc": -0.1002, "final_rank": 10 }, { "submission_id": "aoj_2300_9956862", "code_snippet": "#include <bits/stdc++.h>\n\n\nusing namespace std;\n//make -f ../makefile SRC=\n/*\n*/\n\n\n//-------------a-----------------------------------------------------------------\nbool DEBUG = false;\nconst int INF = 1000000000;\n\nconst int MAX_N = 20;\nstatic double a[MAX_N];\nstatic double b[MAX_N];\nstatic double c[MAX_N];\n\nstatic double D[MAX_N][MAX_N];\n\n//------------------------------------------------------------------------------\nvoid setup(int N)\n{\n for (int i=0; i<N; ++i) D[i][i] = 0;\n for (int i=0; i<N-1; ++i)\n for (int j=i+1; j<N; ++j)\n {\n D[i][j] = (a[i]-a[j])*(a[i]-a[j]) + (b[i]-b[j])*(b[i]-b[j]) + (c[i]-c[j])*(c[i]-c[j]);\n D[j][i] = D[i][j];\n }\n}\n\ndouble cost(int N, int mask)\n{\n vector<int> indices;\n for (int i=0; i<N; i++)\n if (mask & (1<<i))\n indices.push_back( i );\n if (DEBUG) {printf(\"indices: \"); for (int x: indices) printf(\"%d \",x); printf(\"\\n\");}\n\n double res = 0;\n for (int i: indices)\n for (int j: indices)\n if (j > i)\n res += D[i][j];\n return res;\n}\ndouble solve(int N, int K)\n{\n if (DEBUG) {for (int i=0; i<N; ++i) printf(\"%0.2lf %0.2lf %0.2lf\\n\", a[i], b[i], c[i]);}\n //--------------------------------------------------------------------------\n // base cases:\n //--------------------------------------------------------------------------\n // init:\n setup(N);\n //--------------------------------------------------------------------------\n // compute:\n double res = 0;\n for (int mask=0; mask<(1<<N); ++mask)\n if (__builtin_popcount(mask) == K)\n res = max(res, cost(N,mask));\n //--------------------------------------------------------------------------\n // report:\n //printf(\"%d\\n\", res);\n return res;\n}\n\n//------------------------------------------------------------------------------\nvoid test()\n{\n\n}\n\n//------------------------------------------------------------------------------\nint main()\n{\n //test(); return 0;\n //DEBUG = true;\n //--------------------------------------------------------------------------\n int N, K, num;\n num = scanf(\"%d %d \", &N, &K);\n for (int i=0; i<N; ++i) num = scanf(\"%lf %lf %lf \", &a[i], &b[i], &c[i]);\n double res = solve(N, K);\n printf(\"%0.6lf\\n\", res);\n //--------------------------------------------------------------------------\n return 0;\n}\n//------------------------------------------------------------------------------", "accuracy": 1, "time_ms": 30, "memory_kb": 3576, "score_of_the_acc": -0.0618, "final_rank": 4 }, { "submission_id": "aoj_2300_9584327", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n#include <unistd.h>\n\nnamespace fastio {\nstatic constexpr uint32_t SZ = 1 << 17;\nchar ibuf[SZ];\nchar obuf[SZ];\nchar out[100];\n// pointer of ibuf, obuf\n\n\nuint32_t pil = 0, pir = 0, por = 0;\n\nstruct Pre {\n char num[10000][4];\n constexpr Pre() : num() {\n for (int i = 0; i < 10000; i++) {\n int n = i;\n for (int j = 3; j >= 0; j--) {\n num[i][j] = n % 10 | '0';\n n /= 10;\n }\n }\n }\n} constexpr pre;\n\ninline void load() {\n memmove(ibuf, ibuf + pil, pir - pil);\n pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin);\n pil = 0;\n if (pir < SZ)\n ibuf[pir++] = '\\n';\n}\n\ninline void flush() {\n fwrite(obuf, 1, por, stdout);\n por = 0;\n}\n\nvoid rd(char &c) {\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n}\n\nvoid rd(string &x) {\n x.clear();\n char c;\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n do {\n x += c;\n if (pil == pir)\n load();\n c = ibuf[pil++];\n } while (!isspace(c));\n}\n\ntemplate <typename T> void rd_real(T &x) {\n string s;\n rd(s);\n x = stod(s);\n}\n\ntemplate <typename T> void rd_integer(T &x) {\n if (pil + 100 > pir)\n load();\n char c;\n do\n c = ibuf[pil++];\n while (c < '-');\n bool minus = 0;\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (c == '-') {\n minus = 1, c = ibuf[pil++];\n }\n }\n x = 0;\n while ('0' <= c) {\n x = x * 10 + (c & 15), c = ibuf[pil++];\n }\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (minus)\n x = -x;\n }\n}\n\nvoid rd(int &x) {\n rd_integer(x);\n}\nvoid rd(ll &x) {\n rd_integer(x);\n}\nvoid rd(i128 &x) {\n rd_integer(x);\n}\nvoid rd(uint &x) {\n rd_integer(x);\n}\nvoid rd(ull &x) {\n rd_integer(x);\n}\nvoid rd(u128 &x) {\n rd_integer(x);\n}\nvoid rd(double &x) {\n rd_real(x);\n}\nvoid rd(long double &x) {\n rd_real(x);\n}\n\ntemplate <class T, class U> void rd(pair<T, U> &p) {\n return rd(p.first), rd(p.second);\n}\ntemplate <size_t N = 0, typename T> void rd_tuple(T &t) {\n if constexpr (N < std::tuple_size<T>::value) {\n auto &x = std::get<N>(t);\n rd(x);\n rd_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void rd(tuple<T...> &tpl) {\n rd_tuple(tpl);\n}\n\ntemplate <size_t N = 0, typename T> void rd(array<T, N> &x) {\n for (auto &d : x)\n rd(d);\n}\ntemplate <class T> void rd(vector<T> &x) {\n for (auto &d : x)\n rd(d);\n}\n\nvoid read() {}\ntemplate <class H, class... T> void read(H &h, T &...t) {\n rd(h), read(t...);\n}\n\nvoid wt(const char c) {\n if (por == SZ)\n flush();\n obuf[por++] = c;\n}\nvoid wt(const string s) {\n for (char c : s)\n wt(c);\n}\nvoid wt(const char *s) {\n size_t len = strlen(s);\n for (size_t i = 0; i < len; i++)\n wt(s[i]);\n}\n\ntemplate <typename T> void wt_integer(T x) {\n if (por > SZ - 100)\n flush();\n if (x < 0) {\n obuf[por++] = '-', x = -x;\n }\n int outi;\n for (outi = 96; x >= 10000; outi -= 4) {\n memcpy(out + outi, pre.num[x % 10000], 4);\n x /= 10000;\n }\n if (x >= 1000) {\n memcpy(obuf + por, pre.num[x], 4);\n por += 4;\n } else if (x >= 100) {\n memcpy(obuf + por, pre.num[x] + 1, 3);\n por += 3;\n } else if (x >= 10) {\n int q = (x * 103) >> 10;\n obuf[por] = q | '0';\n obuf[por + 1] = (x - q * 10) | '0';\n por += 2;\n } else\n obuf[por++] = x | '0';\n memcpy(obuf + por, out + outi + 4, 96 - outi);\n por += 96 - outi;\n}\n\ntemplate <typename T> void wt_real(T x) {\n ostringstream oss;\n oss << fixed << setprecision(15) << double(x);\n string s = oss.str();\n wt(s);\n}\n\nvoid wt(int x) {\n wt_integer(x);\n}\nvoid wt(ll x) {\n wt_integer(x);\n}\nvoid wt(i128 x) {\n wt_integer(x);\n}\nvoid wt(uint x) {\n wt_integer(x);\n}\nvoid wt(ull x) {\n wt_integer(x);\n}\nvoid wt(u128 x) {\n wt_integer(x);\n}\nvoid wt(double x) {\n wt_real(x);\n}\nvoid wt(long double x) {\n wt_real(x);\n}\n\ntemplate <class T, class U> void wt(const pair<T, U> val) {\n wt(val.first);\n wt(' ');\n wt(val.second);\n}\ntemplate <size_t N = 0, typename T> void wt_tuple(const T t) {\n if constexpr (N < std::tuple_size<T>::value) {\n if constexpr (N > 0) {\n wt(' ');\n }\n const auto x = std::get<N>(t);\n wt(x);\n wt_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void wt(tuple<T...> tpl) {\n wt_tuple(tpl);\n}\ntemplate <class T, size_t S> void wt(const array<T, S> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\ntemplate <class T> void wt(const vector<T> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\n\nvoid print() {\n wt('\\n');\n}\ntemplate <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {\n wt(head);\n if (sizeof...(Tail))\n wt(' ');\n print(forward<Tail>(tail)...);\n}\nvoid __attribute__((destructor)) _d() {\n flush();\n}\n} // namespace fastio\n\n\nusing fastio::flush;\nusing fastio::print;\nusing fastio::read;\n\ninline void first(bool i = true) {\n print(i ? \"first\" : \"second\");\n}\ninline void Alice(bool i = true) {\n print(i ? \"Alice\" : \"Bob\");\n}\ninline void Takahashi(bool i = true) {\n print(i ? \"Takahashi\" : \"Aoki\");\n}\ninline void yes(bool i = true) {\n print(i ? \"yes\" : \"no\");\n}\ninline void Yes(bool i = true) {\n print(i ? \"Yes\" : \"No\");\n}\ninline void No() {\n print(\"No\");\n}\ninline void YES(bool i = true) {\n print(i ? \"YES\" : \"NO\");\n}\ninline void NO() {\n print(\"NO\");\n}\ninline void Yay(bool i = true) {\n print(i ? \"Yay!\" : \":(\");\n}\ninline void Possible(bool i = true) {\n print(i ? \"Possible\" : \"Impossible\");\n}\ninline void POSSIBLE(bool i = true) {\n print(i ? \"POSSIBLE\" : \"IMPOSSIBLE\");\n}\n\n/**\n * @brief Fast IO\n */\n\nint main() {\n int N, M;\n cin >> N >> M;\n vector<double> A(N), B(N), C(N);\n vector<vector<double>> D(N,vector<double>(N));\n rep(i,0,N) cin >> A[i] >> B[i] >> C[i];\n rep(i,0,N) rep(j,0,N) D[i][j] = (A[i]-A[j])*(A[i]-A[j])+(B[i]-B[j])*(B[i]-B[j])+(C[i]-C[j])*(C[i]-C[j]);\n vector<int> X(N,0);\n rep(i,N-M,N) X[i] = 1;\n double ANS = 0.0;\n do {\n double COUNT = 0.0;\n rep(i,0,N) {\n rep(j,i+1,N) {\n if (X[i] == 1 && X[j] == 1) COUNT += D[i][j];\n }\n }\n chmax(ANS,COUNT);\n } while(next_permutation(ALL(X)));\n print(ANS);\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 3640, "score_of_the_acc": -0.1324, "final_rank": 13 }, { "submission_id": "aoj_2300_8993801", "code_snippet": "// (⁠◕⁠ᴗ⁠◕⁠✿⁠)\n\n// #pragma GCC target(\"avx2\")\n// #pragma GCC optimize(\"O3\")\n// #pragma GCC optimize(\"unroll-loops\")\n#include <bits/stdc++.h>\n#define rep(i, n) for (ll i = 0; i < (n); i++)\n#define srep(i, s, n) for (ll i = s; i < (n); i++)\n#define len(x) ((int)(x).size())\n#define all(x) (x).begin(), (x).end()\nusing namespace std;\ntemplate<typename T> using vc = vector<T>;\ntemplate<typename T> using vv = vc<vc<T>>;\ntemplate<typename T> using vvv = vv<vc<T>>;\nusing vi = vc<int>;using vvi = vv<int>; using vvvi = vv<vi>;\nusing ll = long long;using vl = vc<ll>;using vvl = vv<ll>; using vvvl = vv<vl>;\nusing ld = long double; using vld = vc<ld>; using vvld = vc<vld>; using vvvld = vc<vvld>;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nconst ld pi = 3.141592653589793;\nconst int inf = 0x3f3f3f3f;\nconst ll INF = 0x3f3f3f3f3f3f3f3f;\n// const ll mod = 1000000007;\nconst ll mod = 998244353;\ninline bool inside(ll y, ll x, ll H, ll W) {return 0 <= (y) and (y) < (H) and 0 <= (x) and (x) < (W); }\n\n#define debug(var) do{std::cout << #var << \" : \\n\";view(var);}while(0)\ntemplate<typename T> void view(T e){cout << e << endl;}\ntemplate<typename T> void view(const vc<T>& v){for(const auto& e : v){ cout << e << \" \"; } cout << endl;}\ntemplate<typename T> void view(const vv<T>& vv){ for(const auto& v : vv){ view(v); } }\n\nbool next_combination(int N, vi &P){\n int k = len(P);\n rep(i, k - 1) if (P[i] + 1 < P[i + 1]){\n P[i]++;\n rep(j, i) P[j] = j;\n return true;\n }\n if (P[k - 1] < N - 1){\n P[k - 1]++;\n rep(i, k - 1) P[i] = i;\n return true;\n }\n return false;\n}\n\n\nld solve(int N, int M){\n vv<ld> ele(N, vld(3)); rep(i, N) rep(j, 3) cin >> ele[i][j];\n auto dist = [&](int i, int j){\n return (ele[i][0] - ele[j][0]) * (ele[i][0] - ele[j][0]) + \n (ele[i][1] - ele[j][1]) * (ele[i][1] - ele[j][1]) + \n (ele[i][2] - ele[j][2]) * (ele[i][2] - ele[j][2]);\n };\n\n if (M == 0) return 0;\n vi P(M); rep(i, M) P[i] = i;\n ld ans = 0;\n do{\n ld cand = 0;\n rep(j, len(P)) rep(i, j) cand += dist(P[i], P[j]);\n ans = max(ans, cand);\n }while (next_combination(N, P));\n return ans;\n}\n\nint main(){\n vc<ld> ans;\n while (true){\n int N, M; cin >> N >> M;\n if (N == 0) break;\n ans.push_back(solve(N, M));\n break;\n }\n for (auto x : ans) cout << fixed << setprecision(16) << x << endl;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3580, "score_of_the_acc": -0.0398, "final_rank": 2 }, { "submission_id": "aoj_2300_8993792", "code_snippet": "// (⁠◕⁠ᴗ⁠◕⁠✿⁠)\n\n// #pragma GCC target(\"avx2\")\n// #pragma GCC optimize(\"O3\")\n// #pragma GCC optimize(\"unroll-loops\")\n#include <bits/stdc++.h>\n#define rep(i, n) for (ll i = 0; i < (n); i++)\n#define srep(i, s, n) for (ll i = s; i < (n); i++)\n#define len(x) ((int)(x).size())\n#define all(x) (x).begin(), (x).end()\nusing namespace std;\ntemplate<typename T> using vc = vector<T>;\ntemplate<typename T> using vv = vc<vc<T>>;\ntemplate<typename T> using vvv = vv<vc<T>>;\nusing vi = vc<int>;using vvi = vv<int>; using vvvi = vv<vi>;\nusing ll = long long;using vl = vc<ll>;using vvl = vv<ll>; using vvvl = vv<vl>;\nusing ld = long double; using vld = vc<ld>; using vvld = vc<vld>; using vvvld = vc<vvld>;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nconst ld pi = 3.141592653589793;\nconst int inf = 0x3f3f3f3f;\nconst ll INF = 0x3f3f3f3f3f3f3f3f;\n// const ll mod = 1000000007;\nconst ll mod = 998244353;\ninline bool inside(ll y, ll x, ll H, ll W) {return 0 <= (y) and (y) < (H) and 0 <= (x) and (x) < (W); }\n\n#define debug(var) do{std::cout << #var << \" : \\n\";view(var);}while(0)\ntemplate<typename T> void view(T e){cout << e << endl;}\ntemplate<typename T> void view(const vc<T>& v){for(const auto& e : v){ cout << e << \" \"; } cout << endl;}\ntemplate<typename T> void view(const vv<T>& vv){ for(const auto& v : vv){ view(v); } }\n\nbool next_combination(int N, vi &P){\n int k = len(P);\n rep(i, k - 1) if (P[i] + 1 < P[i + 1]){\n P[i]++;\n rep(j, i) P[j] = j;\n return true;\n }\n if (P[k - 1] < N - 1){\n P[k - 1]++;\n rep(i, k - 1) P[i] = i;\n return true;\n }\n return false;\n}\n\n\nld solve(int N, int M){\n vv<ld> ele(N, vld(3)); rep(i, N) rep(j, 3) cin >> ele[i][j];\n auto dist = [&](int i, int j){\n return (ele[i][0] - ele[j][0]) * (ele[i][0] - ele[j][0]) + \n (ele[i][1] - ele[j][1]) * (ele[i][1] - ele[j][1]) + \n (ele[i][2] - ele[j][2]) * (ele[i][2] - ele[j][2]);\n };\n\n vi P(M); rep(i, M) P[i] = i;\n ld ans = 0;\n do{\n ld cand = 0;\n rep(j, len(P)) rep(i, j) cand += dist(P[i], P[j]);\n ans = max(ans, cand);\n }while (next_combination(N, P));\n return ans;\n}\n\nint main(){\n vc<ld> ans;\n while (true){\n int N, M; cin >> N >> M;\n if (N == 0) break;\n ans.push_back(solve(N, M));\n break;\n }\n for (auto x : ans) cout << fixed << setprecision(16) << x << endl;\n}", "accuracy": 0.06593406593406594, "time_ms": 20, "memory_kb": 3524, "score_of_the_acc": -0.0364, "final_rank": 20 }, { "submission_id": "aoj_2300_8397198", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nint n, m;\ndouble l[22], a[22], b[22];\ndouble memo[(1<<21)];\ndouble dfs(int bit, int num){\n if(num == 0) return 0;\n if(memo[bit]!=-1) return memo[bit];\n double res = 0;\n for(int i=0;i<n;i++){\n if((bit&1<<i)!=0) continue;\n double dis = 0;\n for(int j=0;j<n;j++){\n if((bit&1<<j)!=0) dis += pow(l[i]-l[j],2)+pow(a[i]-a[j],2)+pow(b[i]-b[j],2);\n }\n res = max(res, dfs((bit|1<<i), num-1)+dis);\n }\n return memo[bit] = res;\n}\nint main(){\n cin >> n >> m;\n for(int i=0;i<n;i++){\n cin >> l[i] >> a[i] >> b[i];\n }\n fill(memo, memo+(1<<(n+1)), -1);\n printf(\"%.9lf\\n\", dfs(0, m));\n return(0);\n}", "accuracy": 1, "time_ms": 460, "memory_kb": 19644, "score_of_the_acc": -2, "final_rank": 19 }, { "submission_id": "aoj_2300_8397196", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nint n, m;\ndouble l[22], a[22], b[22];\ndouble memo[(1<<21)];\ndouble dfs(int bit, int num){\n if(num == 0) return 0;\n if(memo[bit]!=-1) return memo[bit];\n double res = 0;\n for(int i=0;i<n;i++){\n if((bit&1<<i)!=0) continue;\n double dis = 0;\n for(int j=0;j<n;j++){\n if((bit&1<<j)!=0) dis += pow(l[i]-l[j],2)+pow(a[i]-a[j],2)+pow(b[i]-b[j],2);\n }\n res = max(res, dfs((bit|1<<i), num-1)+dis);\n }\n return memo[bit] = res;\n}\nint main(){\n cin >> n >> m;\n for(int i=0;i<n;i++){\n cin >> l[i] >> a[i] >> b[i];\n }\n fill(memo, memo+(1<<21), -1);\n printf(\"%.9lf\\n\", dfs(0, m));\n return(0);\n}", "accuracy": 1, "time_ms": 450, "memory_kb": 19644, "score_of_the_acc": -1.9778, "final_rank": 18 }, { "submission_id": "aoj_2300_7025303", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing ll=long long;\ndouble a[20][3],ans=0,p;\nll n,m,c[20];\nvoid DFS(ll x,ll d){\n if(d==m){\n p=0;\n for(ll i=0;i<m;i++){\n for(ll j=i+1;j<m;j++){\n p+=pow(a[c[i]][0]-a[c[j]][0],2)+pow(a[c[i]][1]-a[c[j]][1],2)+pow(a[c[i]][2]-a[c[j]][2],2);\n }\n }\n ans=max(ans,p);\n }else if(x==n){\n return;\n }else{\n c[d]=x;\n DFS(x+1,d+1);\n DFS(x+1,d);\n }\n}\nint main(void){\n cin>>n>>m;\n for(ll i=0;i<n;i++){\n cin>>a[i][0]>>a[i][1]>>a[i][2];\n }\n DFS(0,0);\n printf(\"%.6f\\n\",ans);\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3588, "score_of_the_acc": -0.0181, "final_rank": 1 }, { "submission_id": "aoj_2300_6277341", "code_snippet": "#include <bits/stdc++.h>\n//#include<atcoder/all>\n//using namespace atcoder;\nusing namespace std;\nusing ll = long long;\n#define all(A) A.begin(),A.end()\nusing vll = vector<ll>;\nusing v2ll = vector<vll>;\nusing v3ll = vector<v2ll>;\nusing v4ll = vector<v3ll>;\n#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)\n\n\n\nint main() {\n ll N,M;\n cin>>N>>M;\n vector<vector<double>> A(N,vector<double>(3));\n rep(i,N)rep(a,3)cin>>A[i][a];\n double an=0.0;\n rep(bit,(1<<N)){\n vector<vector<double>> B;\n rep(n,N){\n if(bit&(1<<n))B.push_back(A[n]);\n }\n if(B.size()!=M)continue;\n double k=0.0;\n rep(i,M)rep(j,i){\n \n rep(a,3){\n k+=(B[i][a]-B[j][a])*(B[i][a]-B[j][a]);\n }\n an=max(an,k);\n }\n }\n cout<<fixed<<setprecision(16)<<an<<endl;\n}", "accuracy": 1, "time_ms": 320, "memory_kb": 3576, "score_of_the_acc": -0.7063, "final_rank": 17 }, { "submission_id": "aoj_2300_6216622", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, n) for (int i = 0; i < (n); ++i)\n#define inc(i, a, b) for (int i = (a); i <= (b); ++i)\n#define dec(i, a, b) for (int i = (a); i >= (b); --i)\n\nint main() {\n ll n, m;\n cin >> n >> m;\n using ld = long double;\n ld a[n], b[n], c[n];\n\n rep(i, n) cin >> a[i] >> b[i] >> c[i];\n\n ld res = 0;\n rep(i, 1 << n) {\n if (__builtin_popcount(i) != m) continue;\n ld sum = 0;\n rep(j, n) {\n if (!(i >> j & 1)) continue;\n inc(k, j + 1, n - 1) {\n if (!(i >> k & 1)) continue;\n\n sum += (a[j] - a[k]) * (a[j] - a[k])\n + (b[j] - b[k]) * (b[j] - b[k])\n + (c[j] - c[k]) * (c[j] - c[k]);\n }\n }\n res = max(res, sum);\n }\n\n cout << fixed << setprecision(10) << res << endl;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3568, "score_of_the_acc": -0.0835, "final_rank": 5 }, { "submission_id": "aoj_2300_6216338", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\nusing ld = long double;\n#define rep(i, n) for (int i = 0; i < (n); i++)\n#define ALL(x) x.begin(), x.end()\n\nconst ll INF = 1ll << 60;\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(0);\n int N, M;\n cin >> N >> M;\n vector<ld> L(N), A(N), B(N);\n rep(i, N) cin >> L[i] >> A[i] >> B[i];\n int N2 = 1 << N;\n ld ans = 0;\n rep(b, N2) {\n if (__builtin_popcount(b) != M) continue;\n ld s = 0;\n vector<int> ind;\n rep(i, N) if (b >> i & 1) ind.push_back(i);\n rep(i, M) {\n rep(j, i) {\n s += (L[ind[i]] - L[ind[j]]) * (L[ind[i]] - L[ind[j]]);\n s += (A[ind[i]] - A[ind[j]]) * (A[ind[i]] - A[ind[j]]);\n s += (B[ind[i]] - B[ind[j]]) * (B[ind[i]] - B[ind[j]]);\n }\n }\n ans = max(ans, s);\n }\n printf(\"%.15Lf\\n\", ans);\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3608, "score_of_the_acc": -0.086, "final_rank": 8 }, { "submission_id": "aoj_2300_6082815", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n int N,M;\n cin >> N >> M;\n vector<double>a(N),b(N),c(N);\n for(int i = 0; i < N; i++) {\n cin >> a[i] >> b[i] >> c[i];\n }\n double ans = 0;\n for(int i = 0; i < (1 << N); i++) {\n if(__builtin_popcount(i) == M) {\n vector<double>idx;\n for(int j = 0; j < N;j ++) {\n if(1 & (i >> j)) {\n idx.push_back(j);\n }\n }\n double sum = 0;\n for(int j = 0; j < M; j++) {\n for(int k = j+1; k < M; k++) {\n sum += pow(abs(a[idx[j]]-a[idx[k]]),2);\n sum += pow(abs(b[idx[j]]-b[idx[k]]),2);\n sum += pow(abs(c[idx[j]]-c[idx[k]]),2);\n }\n }\n ans = max(ans,sum);\n }\n }\n cout << fixed << setprecision(18) << ans << endl;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3580, "score_of_the_acc": -0.0843, "final_rank": 7 }, { "submission_id": "aoj_2300_6036875", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nint main(void){\n int n, m;\n cin >> n >> m;\n double ans = 0;\n vector<double> L(n), A(n), B(n);\n for(int i = 0; i < n; i++) cin >> L[i] >> A[i] >> B[i];\n for(int bit = 0; bit < (1 << n); bit++){\n vector<int> lst;\n for(int j = 0; j < n; j++){\n if(bit >> j & 1) lst.push_back(j);\n }\n if(lst.size() != m) continue;\n double tot = 0;\n for(int i = 0; i < m; i++){\n for(int j = i + 1; j < m; j++){\n tot += (L[lst[i]] - L[lst[j]]) * (L[lst[i]] - L[lst[j]]);\n tot += (A[lst[i]] - A[lst[j]]) * (A[lst[i]] - A[lst[j]]);\n tot += (B[lst[i]] - B[lst[j]]) * (B[lst[i]] - B[lst[j]]);\n }\n }\n if(tot > ans) ans = tot;\n }\n \n cout << fixed << setprecision(12) << ans;\n\n}", "accuracy": 1, "time_ms": 260, "memory_kb": 3292, "score_of_the_acc": -0.5556, "final_rank": 16 }, { "submission_id": "aoj_2300_6036416", "code_snippet": "#pragma GCC ta g(\"avx2\")\n#pragma GCC optimize(\"Ofast\")\n#pragma GCC optimize(\"unroll-loops\")\n#include <bits/stdc++.h>\nusing namespace std;\n#define DEBUG\n#ifdef DEBUG\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << '(' << p.first << ',' << p.second << ')';\n return os;\n}\ntemplate <class T> ostream &operator<<(ostream &os, const vector<T> &v) {\n os << '{';\n for(int i = 0; i < (int)v.size(); i++) {\n if(i) { os << ','; }\n os << v[i];\n }\n os << '}';\n return os;\n}\nvoid debugg() { cerr << endl; }\ntemplate <class T, class... Args>\nvoid debugg(const T &x, const Args &... args) {\n cerr << \" \" << x;\n debugg(args...);\n}\n#define debug(...) \\\n cerr << __LINE__ << \" [\" << #__VA_ARGS__ << \"]: \", debugg(__VA_ARGS__)\n#define dump(x) cerr << __LINE__ << \" \" << #x << \" = \" << (x) << endl\n#else\n#define debug(...) (void(0))\n#define dump(x) (void(0))\n#endif\nusing namespace std;\ntypedef long long ll;\ntypedef vector<ll> vl;\ntypedef vector<vl> vvl;\ntypedef vector<char> vc;\ntypedef vector<string> vs;\ntypedef vector<bool> vb;\ntypedef vector<double> vd;\ntypedef pair<ll,ll> P;\ntypedef pair<int,int> pii;\ntypedef vector<P> vpl;\ntypedef tuple<ll,ll,ll> tapu;\n#define rep(i,n) for(int i=0; i<(n); i++)\n#define REP(i,a,b) for(int i=(a); i<(b); i++)\n#define all(x) (x).begin(), (x).end()\n#define rall(x) (x).rbegin(), (x).rend()\ntemplate<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){\n\tif(a>b){\n\t\ta = b; return true;\n\t}\n\telse return false;\n}\ntemplate<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){\n\tif(a<b){\n\t\ta = b; return true;\n\t}\n\telse return false;\n}\ntemplate<typename T> inline void print(T &a){\n int sz = a.size();\n for(auto itr = a.begin(); itr != a.end(); itr++){\n\t\tcout << *itr;\n sz--;\n if(sz) cout << \" \";\n\t}\n cout << \"\\n\";\n}\ntemplate<typename T1,typename T2> inline void print2(T1 a, T2 b){\n\tcout << a << \" \" << b << \"\\n\";\n}\ntemplate<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){\n\tcout << a << \" \" << b << \" \" << c << \"\\n\";\n}\nvoid mark() {cout << \"#\" << \"\\n\";}\nll pcount(ll x) {return __builtin_popcountll(x);}\n//#include <atcoder/maxflow>\n//using namespace atcoder;\nconst int inf = (1<<30)-1;\nconst ll linf = 1LL<<61;\nconst int MAX = 510000;\nint dy[8] = {0,1,0,-1,1,-1,-1,1};\nint dx[8] = {-1,0,1,0,1,-1,1,-1};\nconst double pi = acos(-1);\nconst double eps = 1e-9;\n//const int mod = 1e9 + 7;\nconst int mod = 998244353;\n\nint main(){\n\tint n,m; cin >> n >> m;\n\tvd a(n),b(n),c(n);\n\trep(i,n) cin >> a[i] >> b[i] >> c[i];\n\tdouble ans = 0;\n\trep(bit,1<<n){\n\t\tif(pcount(bit) != m) continue;\n\t\tdouble res = 0;\n\t\trep(i,n) rep(j,i){\n\t\t\tif((bit>>i & 1) && (bit>>j & 1)){\n\t\t\t\tres += (a[i]-a[j])*(a[i]-a[j]) + (b[i]-b[j])*(b[i]-b[j]) + (c[i]-c[j])*(c[i]-c[j]);\n\t\t\t}\n\t\t}\n\t\tchmax(ans, res);\n\t}\n\tprintf(\"%.10f\\n\",ans);\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 3572, "score_of_the_acc": -0.1505, "final_rank": 15 }, { "submission_id": "aoj_2300_5962724", "code_snippet": "#include <bits/stdc++.h>\n#define all(v) v.begin(), v.end()\n#define rall(v) v.rbegin(), v.rend()\n#define rep(i,n) for(int i=0;i<(int)(n);i++)\n#define drep(i,j,n) for(int i=0;i<(int)(n-1);i++)for(int j=i+1;j<(int)(n);j++)\n#define trep(i,j,k,n) for(int i=0;i<(int)(n-2);i++)for(int j=i+1;j<(int)(n-1);j++)for(int k=j+1;k<(int)(n);k++)\n#define codefor int test;scanf(\"%d\",&test);while(test--)\n#define INT(...) int __VA_ARGS__;in(__VA_ARGS__)\n#define LL(...) ll __VA_ARGS__;in(__VA_ARGS__)\n#define yes(ans) if(ans)printf(\"yes\\n\");else printf(\"no\\n\")\n#define Yes(ans) if(ans)printf(\"Yes\\n\");else printf(\"No\\n\")\n#define YES(ans) if(ans)printf(\"YES\\n\");else printf(\"NO\\n\")\n#define popcount(v) __builtin_popcount(v)\n#define vector2d(type,name,h,...) vector<vector<type>>name(h,vector<type>(__VA_ARGS__))\n#define vector3d(type,name,h,w,...) vector<vector<vector<type>>>name(h,vector<vector<type>>(w,vector<type>(__VA_ARGS__)))\n#define umap unordered_map\n#define uset unordered_set\nusing namespace std;\nusing ll = long long;\nconst int MOD=1000000007;\nconst int MOD2=998244353;\nconst int INF=1<<30;\nconst ll INF2=(ll)1<<60;\nvoid scan(int& a){scanf(\"%d\",&a);}\nvoid scan(long long& a){scanf(\"%lld\",&a);}\ntemplate<class T,class L>void scan(pair<T, L>& p){scan(p.first);scan(p.second);}\ntemplate<class T,class U,class V>void scan(tuple<T,U,V>& p){scan(get<0>(p));scan(get<1>(p));scan(get<2>(p));}\ntemplate<class T> void scan(T& a){cin>>a;}\ntemplate<class T> void scan(vector<T>& vec){for(auto&& it:vec)scan(it);}\nvoid in(){}\ntemplate <class Head, class... Tail> void in(Head& head, Tail&... tail){scan(head);in(tail...);}\nvoid print(const int& a){printf(\"%d\",a);}\nvoid print(const long long& a){printf(\"%lld\",a);}\nvoid print(const double& a){printf(\"%.15lf\",a);}\ntemplate<class T,class L>void print(const pair<T, L>& p){print(p.first);putchar(' ');print(p.second);}\ntemplate<class T> void print(const T& a){cout<<a;}\ntemplate<class T> void print(const vector<T>& vec){if(vec.empty())return;print(vec[0]);for(auto it=vec.begin();++it!= vec.end();){putchar(' ');print(*it);}}\nvoid out(){putchar('\\n');}\ntemplate<class T> void out(const T& t){print(t);putchar('\\n');}\ntemplate <class Head, class... Tail> void out(const Head& head,const Tail&... tail){print(head);putchar(' ');out(tail...);}\ntemplate<class T> void dprint(const T& a){cerr<<a;}\ntemplate<class T> void dprint(const vector<T>& vec){if(vec.empty())return;cerr<<vec[0];for(auto it=vec.begin();++it!= vec.end();){cerr<<\" \"<<*it;}}\nvoid debug(){cerr<<endl;}\ntemplate<class T> void debug(const T& t){dprint(t);cerr<<endl;}\ntemplate <class Head, class... Tail> void debug(const Head& head, const Tail&... tail){dprint(head);cerr<<\" \";debug(tail...);}\nll intpow(ll a, ll b){ ll ans = 1; while(b){ if(b & 1) ans *= a; a *= a; b /= 2; } return ans; }\nll modpow(ll a, ll b, ll p){ ll ans = 1; while(b){ if(b & 1) (ans *= a) %= p; (a *= a) %= p; b /= 2; } return ans; }\nll modinv(ll a, ll m) {ll b = m, u = 1, v = 0;while (b) {ll t = a / b;a -= t * b; swap(a, b);u -= t * v; swap(u, v);}u %= m;if (u < 0) u += m;return u;}\nll updivide(ll a,ll b){if(a%b==0) return a/b;else return (a/b)+1;}\ntemplate<class T> void chmax(T &a,const T b){if(b>a)a=b;}\ntemplate<class T> void chmin(T &a,const T b){if(b<a)a=b;}\n\nint main(){\n INT(n,m);\n vector2d(double,a,n,3);\n in(a);\n vector<bool> b(m,1);\n while(b.size()<n)b.push_back(0);\n sort(all(b));\n double ans=0;\n do{\n double d=0;\n drep(i,j,n){\n if(b[i]&&b[j]){\n double d2=0;\n rep(k,3){\n d2+=(a[i][k]-a[j][k])*(a[i][k]-a[j][k]);\n }\n d+=d2;\n }\n }\n chmax(ans,d);\n }while(next_permutation(all(b)));\n out(ans);\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 3552, "score_of_the_acc": -0.1492, "final_rank": 14 }, { "submission_id": "aoj_2300_5949973", "code_snippet": "#include <bits/stdc++.h>\n#define rep(i,n) for(int i = 0; i < (n); i++)\nusing namespace std;\ntypedef long long ll;\n\nint main(){\n cin.tie(0);\n ios::sync_with_stdio(0);\n \n int N,M; cin >> N >> M;\n vector<double> L(N), a(N), b(N);\n rep(i,N) cin >> L[i] >> a[i] >> b[i];\n\n double ans = 0;\n rep(i,1<<N) {\n double cur = 0;\n if(__builtin_popcount(i) == M) {\n vector<int> v;\n rep(j,N) if(i & (1 << j)) v.push_back(j);\n for(int j : v) for(int k : v) if(j < k)\n cur += (L[k] - L[j]) * (L[k] - L[j]) + (a[k] - a[j]) * (a[k] - a[j]) + (b[k] - b[j]) * (b[k] - b[j]);\n }\n ans = max(ans, cur);\n }\n cout.precision(17);\n cout << ans << endl;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3568, "score_of_the_acc": -0.0835, "final_rank": 5 } ]
aoj_2301_cpp
Sleeping Time Miki is a high school student. She has a part time job, so she cannot take enough sleep on weekdays. She wants to take good sleep on holidays, but she doesn't know the best length of sleeping time for her. She is now trying to figure that out with the following algorithm: Begin with the numbers K , R and L . She tries to sleep for H=(R+L)/2 hours. If she feels the time is longer than or equal to the optimal length, then update L with H . Otherwise, update R with H . After repeating step 2 and 3 for K nights, she decides her optimal sleeping time to be T' = (R+L)/2 . If her feeling is always correct, the steps described above should give her a very accurate optimal sleeping time. But unfortunately, she makes mistake in step 3 with the probability P . Assume you know the optimal sleeping time T for Miki. You have to calculate the probability PP that the absolute difference of T' and T is smaller or equal to E . It is guaranteed that the answer remains unaffected by the change of E in 10^{-10} . Input The input follows the format shown below K R L P E T Where the integers 0 \leq K \leq 30 , 0 \leq R \leq L \leq 12 are the parameters for the algorithm described above. The decimal numbers on the following three lines of the input gives the parameters for the estimation. You can assume 0 \leq P \leq 1 , 0 \leq E \leq 12 , 0 \leq T \leq 12 . Output Output PP in one line. The output should not contain an error greater than 10^{-5} . Sample Input 1 3 0 2 0.10000000000 0.50000000000 1.00000000000 Output for the Sample Input 1 0.900000 Sample Input 2 3 0 2 0.10000000000 0.37499999977 1.00000000000 Output for the Sample Input 2 0.810000 Sample Input 3 3 0 2 0.10000000000 0.00000100000 0.37500000000 Output for the Sample Input 3 0.729000 Sample Input 4 3 0 2 0.20000000000 0.00000100000 0.37500000000 Output for the Sample Input 4 0.512000
[ { "submission_id": "aoj_2301_10958607", "code_snippet": "#include<bits/stdc++.h>\ntemplate <class T>\ninline bool rd(T &ret) {\n char c; int sgn;\n if(c=getchar(),c==EOF) return 0;\n while(c!='-'&&(c<'0'||c>'9')) c=getchar();\n sgn=(c=='-')?-1:1;\n ret=(c=='-')?0:(c-'0');\n while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0');\n ret*=sgn;\n return 1;\n}\ntemplate <class T>\ninline void pt(T x) {\n if (x <0) {\n putchar('-');\n x = -x;\n }\n if(x>9) pt(x/10);\n putchar(x%10+'0');\n}\nusing namespace std;\nconst double eps = 1e-7;\nint K;\ndouble L, R, P, E, T;\ndouble ans ;\nvoid dfs(double l, double r, double p, int dep){\n if(fabs(T-l) <= E && fabs(T-r) <= E) {ans += p; return ;}\n double mid = (l+r)/2.0;\n if(dep >= K)\n {\n if(fabs(T-mid) <= E) ans += p;\n return ;\n }\n if(p < eps) return ;\n if(mid < T)\n {\n dfs(mid, r, p*(1.0-P), dep+1);\n dfs(l, mid, p*P, dep+1);\n }\n else\n {\n dfs(mid, r, p*P, dep+1);\n dfs(l, mid, p*(1.0-P), dep+1);\n }\n}\nint main(){\n while(~scanf(\"%d %lf %lf\", &K, &L, &R)){\n scanf(\"%lf %lf %lf\", &P, &E, &T);\n ans = 0;\n dfs(L, R, 1.0, 0);\n printf(\"%.10f\\n\", ans);\n }\n return 0;\n}\n/*\n30 0 12\n0.5\n0\n13\n\n*/", "accuracy": 1, "time_ms": 60, "memory_kb": 3564, "score_of_the_acc": -1.0139, "final_rank": 1 }, { "submission_id": "aoj_2301_8860915", "code_snippet": "#include <iostream>\n#include <cstdio>\nusing namespace std;\n\nint K, R, L;\ndouble P, E, T;\n\ndouble dfs(int d, double l, double r) {\n double mid = (l + r) / 2;\n\n if (!d)\n return (T - E < mid && mid < T + E) ? 1 : 0;\n\n if (r < T - E || T + E < l)\n return 0;\n\n double left_dfs = dfs(d - 1, l, mid);\n double right_dfs = dfs(d - 1, mid, r);\n\n if (mid >= T)\n return (1.0 - P) * left_dfs + P * right_dfs;\n\n return (1.0 - P) * right_dfs + P * left_dfs;\n}\n\nint main() {\n cin >> K >> L >> R >> P >> E >> T;\n printf(\"%.9f\\n\", dfs(K, L, R));\n\n return 0;\n}", "accuracy": 1, "time_ms": 3300, "memory_kb": 3284, "score_of_the_acc": -1.1996, "final_rank": 10 }, { "submission_id": "aoj_2301_8860913", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nint K, R, L;\ndouble P, E, T;\n\ndouble dfs(int d, double l, double r) {\n double mid = (l + r) / 2;\n if (!d) {\n return ((mid > T - E) && (mid < T + E)) ? 1.0 : 0.0;\n }\n if (r < T - E || l > T + E) {\n return 0.0;\n }\n \n double res = dfs(d - 1, mid, r); // store result of dfs() call in a variable\n if (mid >= T) {\n return (1.0 - P) * dfs(d - 1, l, mid) + P * res; \n }\n return (1.0 - P) * res + P * dfs(d - 1, l, mid);\n}\n\nint main() {\n scanf(\"%d %d %d %lf %lf %lf\", &K, &L, &R, &P, &E, &T);\n printf(\"%.9f\\n\", dfs(K, L, R));\n return 0;\n}", "accuracy": 1, "time_ms": 3260, "memory_kb": 3248, "score_of_the_acc": -1.0967, "final_rank": 2 }, { "submission_id": "aoj_2301_8835889", "code_snippet": "#include <iostream>\n#include <cstdio>\n\ndouble dfs(int d, double l, double r, double T, double E, double P) {\n double mid = (l + r) / 2;\n if (!d)\n return (T - E < mid && mid < T + E) ? 1 : 0;\n if (r < T - E || T + E < l)\n return 0;\n if (mid >= T)\n return (1.0 - P) * dfs(d - 1, l, mid, T, E, P) + P * dfs(d - 1, mid, r, T, E, P);\n return (1.0 - P) * dfs(d - 1, mid, r, T, E, P) + P * dfs(d - 1, l, mid, T, E, P);\n}\n\nint main() {\n int K, L, R;\n double P, E, T;\n std::cin >> K >> L >> R >> P >> E >> T;\n printf(\"%.9f\\n\", dfs(K, L, R, T, E, P));\n return 0;\n}", "accuracy": 1, "time_ms": 3610, "memory_kb": 3284, "score_of_the_acc": -1.2857, "final_rank": 13 }, { "submission_id": "aoj_2301_8822601", "code_snippet": "#include <iostream>\n#include <cmath>\n\nint K, R, L;\ndouble P, E, T;\ndouble dfs(int d, double l, double r) {\n double mid = (l + r) / 2;\n if (!d)\n return (T - E < mid && mid < T + E) ? 1 : 0;\n if (r < T - E || T + E < l)\n return 0;\n if (mid >= T)\n return (1.0 - P) * dfs(d - 1, l, mid) + P * dfs(d - 1, mid, r);\n return (1.0 - P) * dfs(d - 1, mid, r) + P * dfs(d - 1, l, mid);\n}\n\nint main() {\n std::ios_base::sync_with_stdio(false);\n std::cin.tie(NULL);\n std::cin >> K >> L >> R >> P >> E >> T;\n std::cout.precision(9);\n std::cout << dfs(K, L, R) << \"\\n\";\n return 0;\n}", "accuracy": 0.28378378378378377, "time_ms": 3220, "memory_kb": 3292, "score_of_the_acc": -1.1978, "final_rank": 18 }, { "submission_id": "aoj_2301_8216346", "code_snippet": "#include <iostream>\n#include <cstdio>\nusing namespace std;\n\nstatic double K, R, L, P, E, T;\n\ndouble dfs(int d, double l, double r) {\n double mid = (l + r) * .5;\n if (!d)\n return (T - E < mid && mid < T + E) ? 1. : .0;\n if (r < T - E || T + E < l)\n return .0;\n if (mid >= T)\n return (1. - P) * dfs(d - 1, l, mid) + P * dfs(d - 1, mid, r);\n return (1. - P) * dfs(d - 1, mid, r) + P * dfs(d - 1, l, mid);\n}\n\nint main() {\n std::ios::sync_with_stdio(false); // faster I/O\n cin >> K >> L >> R >> P >> E >> T;\n printf(\"%.9f\\n\", dfs((int)K, L, R));\n return 0;\n}", "accuracy": 1, "time_ms": 3210, "memory_kb": 3292, "score_of_the_acc": -1.195, "final_rank": 8 }, { "submission_id": "aoj_2301_8216345", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nint K, R, L;\ndouble P, E, T;\ndouble dfs(int d, double l, double r) {\n if (r < T - E || T + E < l)\n return 0;\n double mid = (l + r) * 0.5;\n if (!d)\n return (T - E < mid && mid < T + E) ? 1 : 0;\n if (mid >= T)\n return (1.0 - P) * dfs(d - 1, l, mid) + P * dfs(d - 1, mid, r);\n return (1.0 - P) * dfs(d - 1, mid, r) + P * dfs(d - 1, l, mid);\n}\nint main() {\n cin >> K >> L >> R >> P >> E >> T;\n printf(\"%.9f\\n\", dfs(K, L, R));\n return 0;\n}", "accuracy": 1, "time_ms": 3400, "memory_kb": 3272, "score_of_the_acc": -1.1968, "final_rank": 9 }, { "submission_id": "aoj_2301_8216344", "code_snippet": "#include <iostream>\n#include <cmath>\n\nint K, R, L;\ndouble P, E, T;\n\ndouble dfs(int d, double l, double r) {\n double mid = l + ((r - l) / 2);\n if (d == 0)\n return (std::abs(mid - T) <= E) ? 1 : 0;\n if (r < T - E || T + E < l)\n return 0;\n double p1 = P * dfs(d - 1, l, mid);\n double p2 = (1.0 - P) * dfs(d - 1, mid, r);\n if (mid >= T)\n return p2 + p1;\n return p1 + p2;\n}\n\nint main() {\n std::cin >> K >> L >> R >> P >> E >> T;\n std::cout.precision(9);\n std::cout << dfs(K, L, R) << std::endl;\n return 0;\n}", "accuracy": 0.13513513513513514, "time_ms": 30, "memory_kb": 3272, "score_of_the_acc": -0.2607, "final_rank": 20 }, { "submission_id": "aoj_2301_8216341", "code_snippet": "#include <iostream>\n#include <cstdio>\n\ndouble P, E, T;\nint K;\ndouble dfs(int d, double l, double r) {\n if (r < T - E || T + E < l)\n return 0;\n if (!d)\n return 1;\n double mid = l + (r - l) / 2;\n if (mid >= T)\n return (1.0 - P) * dfs(d - 1, l, mid) + P * dfs(d - 1, mid, r);\n return (1.0 - P) * dfs(d - 1, mid, r) + P * dfs(d - 1, l, mid);\n}\n\nint main() {\n double L, R;\n std::cin >> K >> L >> R >> P >> E >> T;\n printf(\"%.9f\\n\", dfs(K, L, R));\n return 0;\n}", "accuracy": 0.5135135135135135, "time_ms": 3410, "memory_kb": 3272, "score_of_the_acc": -1.1995, "final_rank": 15 }, { "submission_id": "aoj_2301_8216337", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nint K;\ndouble P, E, T;\ndouble dfs(int d, double l, double r) {\n double mid = (l + r) * 0.5;\n if (d == 0)\n return (fabs(mid - T) <= E) ? 1.0 : 0.0;\n if (r < T - E || T + E < l)\n return 0.0;\n if (mid >= T)\n return (1.0 - P) * dfs(d - 1, l, mid) + P * dfs(d - 1, mid, r);\n return (1.0 - P) * dfs(d - 1, mid, r) + P * dfs(d - 1, l, mid);\n}\nint main() {\n double L, R;\n cin >> K >> L >> R >> P >> E >> T;\n cout << fixed << setprecision(9) << dfs(K, L, R) << \"\\n\";\n return 0;\n}", "accuracy": 1, "time_ms": 3270, "memory_kb": 3272, "score_of_the_acc": -1.1607, "final_rank": 7 }, { "submission_id": "aoj_2301_8216336", "code_snippet": "#include <iostream>\n#include <cstdio>\n\nusing namespace std;\n\nint K;\ndouble L, R, P, E, T;\n\ndouble dfs(int d, double l, double r) {\n if (r < T - E || T + E < l) return 0;\n if (!d) return (T - E < r && l < T + E) ? 1 : 0;\n double mid = l + (r - l) / 2;\n if (mid >= T) return (1.0 - P) * dfs(d - 1, l, mid) + P * dfs(d - 1, mid, r);\n else return (1.0 - P) * dfs(d - 1, mid, r) + P * dfs(d - 1, l, mid);\n}\n\nint main() {\n cin >> K >> L >> R >> P >> E >> T;\n printf(\"%.9f\\n\", dfs(K, L, R));\n return 0;\n}", "accuracy": 0.5135135135135135, "time_ms": 3240, "memory_kb": 3272, "score_of_the_acc": -1.1523, "final_rank": 14 }, { "submission_id": "aoj_2301_8216333", "code_snippet": "#include <iostream>\n#include <cstdio>\n\ndouble P, E, T;\nint K;\ndouble dfs(int d, double l, double r) {\n double mid = l + (r - l) / 2;\n if (!d)\n return (T - E < mid && mid < T + E) ? 1 : 0;\n if (r < T - E || T + E < l)\n return 0;\n if (mid >= T)\n return (1.0 - P) * dfs(d - 1, l, mid) + P * dfs(d - 1, mid, r);\n return (1.0 - P) * dfs(d - 1, mid, r) + P * dfs(d - 1, l, mid);\n}\nint main() {\n int L, R;\n std::cin >> K >> L >> R >> P >> E >> T;\n printf(\"%.9f\\n\", dfs(K, L, R));\n return 0;\n}", "accuracy": 1, "time_ms": 3440, "memory_kb": 3272, "score_of_the_acc": -1.2079, "final_rank": 11 }, { "submission_id": "aoj_2301_8216327", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nint K, R, L;\ndouble P, E, T;\ndouble dfs(int d, double l, double r) {\n double mid = (l + r) / 2;\n if (!d)\n return (T - E < mid && mid < T + E) ? 1 : 0;\n if (r < T - E || T + E < l)\n return 0;\n if (mid >= T)\n return (1.0 - P) * dfs(d - 1, l, mid) + P * dfs(d - 1, mid, r);\n return (1.0 - P) * dfs(d - 1, mid, r) + P * dfs(d - 1, l, mid);\n}\nmain() {\n cin >> K >> L >> R >> P >> E >> T;\n printf(\"%.9f\\n\", dfs(K, L, R));\n}", "accuracy": 1, "time_ms": 3260, "memory_kb": 3272, "score_of_the_acc": -1.1579, "final_rank": 5 }, { "submission_id": "aoj_2301_8208796", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nint K, R, L;\ndouble P, E, T;\ndouble dfs(int d, double l, double r) {\n double mid = (l + r) / 2;\n if (!d)\n return (T - E < mid && mid < T + E) ? 1 : 0;\n if (r < T - E || T + E < l)\n return 0;\n if (mid >= T)\n return (1.0 - P) * dfs(d - 1, l, mid) + P * dfs(d - 1, mid, r);\n return (1.0 - P) * dfs(d - 1, mid, r) + P * dfs(d - 1, l, mid);\n}\nmain() {\n cin >> K >> L >> R >> P >> E >> T;\n cout << dfs(K, L, R);\n}", "accuracy": 0.28378378378378377, "time_ms": 3290, "memory_kb": 3272, "score_of_the_acc": -1.1662, "final_rank": 17 }, { "submission_id": "aoj_2301_8208795", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nint K, R, L;\ndouble P, E, T;\ndouble dfs(int d, double l, double r) {\n double mid = (l + r) / 2;\n if (!d)\n return (T - E < mid && mid < T + E) ? 1 : 0;\n if (r < T - E || T + E < l)\n return 0;\n if (mid >= T)\n return (1.0 - P) * dfs(d - 1, l, mid) + P * dfs(d - 1, mid, r);\n return (1.0 - P) * dfs(d - 1, mid, r) + P * dfs(d - 1, l, mid);\n}\nint main() {\n cin >> K >> L >> R >> P >> E >> T;\n printf(\"%.9f\\n\", dfs(K, L, R));\n}", "accuracy": 1, "time_ms": 3260, "memory_kb": 3272, "score_of_the_acc": -1.1579, "final_rank": 5 }, { "submission_id": "aoj_2301_8208794", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nint K, R, L;\ndouble P, E, T;\ndouble dfs(int d, double l, double r) {\n double mid = (l + r) / 2;\n if (!d)\n return (T - E < mid && mid < T + E) ? 1 : 0;\n if (r < T - E || T + E < l)\n return 0;\n if (mid >= T)\n return (1.0 - P) * dfs(d - 1, l, mid) + P * dfs(d - 1, mid, r);\n return (1.0 - P) * dfs(d - 1, mid, r) + P * dfs(d - 1, l, mid);\n}\nmain() {\n cin >> K >> L >> R >> P >> E >> T;\n printf(\"%.10f\\n\", dfs(K, L, R));\n}", "accuracy": 1, "time_ms": 3250, "memory_kb": 3272, "score_of_the_acc": -1.1551, "final_rank": 3 }, { "submission_id": "aoj_2301_8208793", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nint K, R, L;\ndouble P, E, T;\ndouble dfs(int d, double l, double r) {\n double mid = (l + r) / 2;\n if (!d)\n return (T - E < mid && mid < T + E) ? 1 : 0;\n if (mid < T - E || T + E < l)\n return 0;\n if (mid >= T)\n return (1.0 - P) * dfs(d - 1, l, mid) + P * dfs(d - 1, mid, r);\n return (1.0 - P) * dfs(d - 1, mid, r) + P * dfs(d - 1, l, mid);\n}\nmain() {\n cin >> K >> L >> R >> P >> E >> T;\n printf(\"%.9f\\n\", dfs(K, L, R));\n}", "accuracy": 0.13513513513513514, "time_ms": 10, "memory_kb": 3172, "score_of_the_acc": 0, "final_rank": 19 }, { "submission_id": "aoj_2301_8208791", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nint K, R, L;\ndouble P, E, T;\ndouble dfs(int d, double l, double r) {\n double mid = (l + r) / 2;\n if (!d)\n return (T - E < mid && mid < T + E) ? 1 : 0;\n if (r < T - E || T + E < l)\n return 0;\n if (mid >= T)\n return (1.0 - P) * dfs(d - 1, l, mid) + P * dfs(d - 1, mid, r);\n return (1.0 - P) * dfs(d - 1, mid, r) + P * dfs(d - 1, l, mid);\n}\nmain() {\n cin >> K >> L >> R >> P >> E >> T;\n cout << dfs(K, L, R) << endl;\n}", "accuracy": 0.28378378378378377, "time_ms": 3190, "memory_kb": 3272, "score_of_the_acc": -1.1384, "final_rank": 16 }, { "submission_id": "aoj_2301_8208789", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nint K, R, L;\ndouble P, E, T;\ndouble dfs(int d, double l, double r) {\n double mid = (l + r) / 2;\n if (!d)\n return (T - E < mid && mid < T + E) ? 1 : 0;\n if (r < T - E || T + E < l)\n return 0;\n if (mid >= T)\n return (1.0 - P) * dfs(d - 1, l, mid) + P * dfs(d - 1, mid, r);\n return (1.0 - P) * dfs(d - 1, mid, r) + P * dfs(d - 1, l, mid);\n}\nmain() {\n cin >> K >> L >> R >> P >> E >> T;\n printf(\"%.9lf\\n\", dfs(K, L, R));\n}", "accuracy": 1, "time_ms": 3250, "memory_kb": 3272, "score_of_the_acc": -1.1551, "final_rank": 3 }, { "submission_id": "aoj_2301_8116471", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nint K, R, L;\ndouble P, E, T;\ndouble dfs(int d, double l, double r) {\n double mid = (l + r) / 2;\n if (!d)\n return (T - E < mid && mid < T + E) ? 1 : 0;\n if (r < T - E || T + E < l)\n return 0;\n if (mid >= T) {\n double p = 1.0 - P;\n return p * dfs(d - 1, l, mid) + P * dfs(d - 1, mid, r);\n }\n double p = 1.0 - P;\n return p * dfs(d - 1, mid, r) + P * dfs(d - 1, l, mid);\n}\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(NULL);\n cout.tie(NULL);\n cin >> K >> L >> R >> P >> E >> T;\n printf(\"%.9f\\n\", dfs(K, L, R));\n return 0;\n}", "accuracy": 1, "time_ms": 3350, "memory_kb": 3308, "score_of_the_acc": -1.2747, "final_rank": 12 } ]
aoj_2302_cpp
On or Off Saving electricity is very important! You are in the office represented as R \times C grid that consists of walls and rooms. It is guaranteed that, for any pair of rooms in the office, there exists exactly one route between the two rooms. It takes 1 unit of time for you to move to the next room (that is, the grid adjacent to the current room). Rooms are so dark that you need to switch on a light when you enter a room. When you leave the room, you can either leave the light on, or of course you can switch off the light. Each room keeps consuming electric power while the light is on. Today you have a lot of tasks across the office. Tasks are given as a list of coordinates, and they need to be done in the specified order. To save electricity, you want to finish all the tasks with the minimal amount of electric power. The problem is not so easy though, because you will consume electricity not only when light is on, but also when you switch on/off the light. Luckily, you know the cost of power consumption per unit time and also the cost to switch on/off the light for all the rooms in the office. Besides, you are so smart that you don't need any time to do the tasks themselves. So please figure out the optimal strategy to minimize the amount of electric power consumed. After you finished all the tasks, please DO NOT leave the light on at any room. It's obviously wasting! Input The first line of the input contains three positive integers R ( 0 \lt R \leq 50 ), C ( 0 \lt C \leq 50 ) and M ( 2 \leq M \leq 1000 ). The following R lines, which contain C characters each, describe the layout of the office. '.' describes a room and '#' describes a wall. This is followed by three matrices with R rows, C columns each. Every elements of the matrices are positive integers. The (r, c) element in the first matrix describes the power consumption per unit of time for the room at the coordinate (r, c) . The (r, c) element in the second matrix and the third matrix describe the cost to turn on the light and the cost to turn off the light, respectively, in the room at the coordinate (r, c) . Each of the last M lines contains two positive integers, which describe the coodinates of the room for you to do the task. Note that you cannot do the i -th task if any of the j -th task ( 0 \leq j \leq i ) is left undone. Output Print one integer that describes the minimal amount of electric power consumed when you finished all the tasks. Sample Input 1 1 3 2 ... 1 1 1 1 2 1 1 1 1 0 0 0 2 Output for the Sample Input 1 7 Sample Input 2 3 3 5 ... .## ..# 1 1 1 1 0 0 1 1 0 3 3 3 3 0 0 3 3 0 5 4 5 4 0 0 5 4 0 1 0 2 1 0 2 2 0 0 0 Output for the Sample Input 2 77 Sample Input 3 5 5 10 #.### #.... ###.# ..#.# #.... 0 12 0 0 0 0 4 3 2 10 0 0 0 99 0 11 13 0 2 0 0 1 1 2 1 0 4 0 0 0 0 13 8 2 4 0 0 0 16 0 1 1 0 2 0 0 2 3 1 99 0 2 0 0 0 0 12 2 12 2 0 0 0 3 0 4 14 0 16 0 0 2 14 2 90 0 1 3 0 4 4 1 4 1 1 4 4 1 1 4 3 3 0 1 4 Output for the Sample Input 3 777
[ { "submission_id": "aoj_2302_10858089", "code_snippet": "#include <cstdio>\n#include <cstring>\n#include <iostream>\n#include <algorithm>\n#include <queue>\n#include <vector>\nusing namespace std;\ntypedef long long ll;\n\nconst int dx[]={1,0,0,-1};\nconst int dy[]={0,1,-1,0};\n\nconst int N = 55;\nqueue<int> q;\nvector<int> path;\nchar mp[N][N];\nint vis[N][N], pre[N][N], t[N*N];\nint val[N][N], op[N][N], cl[N][N];\nint n, m, K, X[N*N], Y[N*N];\nvoid BFS(int sx, int sy, int ex, int ey) {\n\twhile(!q.empty()) q.pop();\n\tmemset(vis, -1, sizeof vis);\n\tmemset(pre, -1, sizeof pre);\n\n\tvis[sx][sy] = 0;\n\tq.push(sx*N+sy);\n\twhile(!q.empty()) {\n\t\tint tmp = q.front(); q.pop();\n\t\tint x = tmp / N, y = tmp % N;\n\t\tif(x == ex && y == ey) break;//continue;\n\t\t\n\t\tfor(int di = 0; di < 4; di ++) {\n\t\t\tint nx = x + dx[di], ny = y + dy[di];\n\t\t\tif(nx < 0 || nx >= n || ny < 0 || ny >= m) continue;\n\t\t\tif(vis[nx][ny] != -1 || mp[nx][ny] == '#') continue;\n\t\t\t\n\t\t\tvis[nx][ny] = vis[x][y] + 1;\n\t\t\tpre[nx][ny] = x*N+y;\n\t\t\tq.push(nx*N+ny);\n\t\t}\n\t}\n\t\n\tint size = 0, x = ex, y = ey;\n\twhile(pre[x][y] != -1) {\n\t\tint tmp = pre[x][y];\n\t\tt[size++] = tmp;\n\t\tx = tmp / N;\n\t\ty = tmp % N;\n\t}\n\tfor(int i = size-1; i >= 0; i --) path.push_back(t[i]);\n\n}\nvoid input() {\n\tfor(int i = 0; i < n; i ++)\n\t\tscanf(\"%s\", mp[i]);\n\n\tfor(int i = 0; i < n; i ++) {\n\t\tfor(int j = 0; j < m; j ++) {\n\t\t\tscanf(\"%d\", &val[i][j]);\n\t\t}\n\t}\n\tfor(int i = 0; i < n; i ++) {\n\t\tfor(int j = 0; j < m; j ++) {\n\t\t\tscanf(\"%d\", &op[i][j]);\n\t\t}\n\t}\n\tfor(int i = 0; i < n; i ++) {\n\t\tfor(int j = 0; j < m; j ++) {\n\t\t\tscanf(\"%d\", &cl[i][j]);\n\t\t}\n\t}\n\tfor(int i = 0; i < K; i ++) {\n\t\tscanf(\"%d%d\", &X[i], &Y[i]);\n\t}\n}\nint main() {\n\twhile(~scanf(\"%d%d%d\", &n, &m, &K)) {\n\t\tinput();\n\t\t//\n\t\tpath.clear();\n\t\tfor(int i = 1; i < K; i ++) {\n\t\t\tBFS(X[i-1], Y[i-1], X[i], Y[i]);\n\t\t}\n\t\tpath.push_back(X[K-1]*N+Y[K-1]);\n\t\t\n\t\tmemset(pre, -1, sizeof pre);\n\t\tint ans = 0;\n\t\tfor(int i = 0; i < (int)path.size(); i ++) {\n\t\t\tint x = path[i]/N, y = path[i]%N;\n\t\t\tif(pre[x][y] == -1) {\n\t\t\t\tans += op[x][y] + cl[x][y];\n\t\t\t} else {\n\t\t\t\tint tmp = (i - pre[x][y]) * val[x][y];\n\t\t\t\ttmp = min(tmp, op[x][y] + cl[x][y]);\n\t\t\t\tans += tmp;\n\t\t\t}\n\t\t\tpre[x][y] = i;\n\t\t}\n\t\t\n\t\tprintf(\"%d\\n\", ans);\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 13464, "score_of_the_acc": -0.2581, "final_rank": 3 }, { "submission_id": "aoj_2302_10730502", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\nconst int NMAX = 60, PMAX = 1010;\nconst int dl[] = {1, 0, -1, 0};\nconst int dc[] = {0, 1, 0, -1};\n\nint N, M, Q;\nchar A[NMAX][NMAX];\nint B[NMAX][NMAX];\nint C[NMAX][NMAX];\nint D[NMAX][NMAX];\nint Dist[NMAX][NMAX];\nlist<int> E[NMAX][NMAX];\npair<int, int> from[NMAX][NMAX];\nbool witness[NMAX][NMAX];\n\nvoid getPath(int startX, int startY, int finishX, int finishY, vector<pair<int, int>> &dest) {\n\tqueue<pair<int, int>> Q;\n\n\tmemset(witness, 0, sizeof witness);\n\tQ.push({startX, startY});\n\twitness[startX][startY] = 1;\n\tDist[startX][startY] = 0;\n\tbool found = 0;\n\twhile (!Q.empty()) {\n\t\tint currX, currY;\n\t\ttie(currX, currY) = Q.front();\n\t\tQ.pop();\n\t\tfor (int i = 0; i < 4; ++i) {\n\t\t\tint newX = currX + dl[i];\n\t\t\tint newY = currY + dc[i];\n\t\t\tif (newX < 0 || newX >= N || newY < 0 || newY >= M)\n\t\t\t\tcontinue;\n\t\t\tif (A[newX][newY] != '.' || witness[newX][newY])\n\t\t\t\tcontinue;\n\t\t\tfrom[newX][newY] = {currX, currY};\n\t\t\tDist[newX][newY] = Dist[currX][currY] + 1;\n\t\t\twitness[newX][newY] = 1;\n\t\t\tQ.push({newX, newY});\n\t\t\tif (newX == finishX && newY == finishY) {\n\t\t\t\tfound = 1;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n\tassert(found);\n\tint currX, currY;\n\ttie(currX, currY) = from[finishX][finishY];\n\tint newSize = dest.size() + Dist[currX][currY];\n\tassert(newSize < 2500000);\n\tdest.resize(newSize);\n\t--newSize;\n\twhile (currX != startX || currY != startY) {\n\t\tdest[newSize--] = {currX, currY};\n\t\ttie(currX, currY) = from[currX][currY];\n\t}\n}\n\nint main() {\n//\tassert(freopen(\"debug.in\", \"r\", stdin));\n//\tassert(freopen(\"debug.out\", \"w\", stdout));\n\n\tint i, j;\n\n\tscanf(\"%d %d %d\", &N, &M, &Q);\n\tfor (i = 0; i < N; ++i)\n\t\tscanf(\"%s\", A[i]);\n\tfor (i = 0; i < N; ++i)\n\t\tfor (j = 0; j < M; ++j)\n\t\t\tscanf(\"%d\", &B[i][j]);\n\tfor (i = 0; i < N; ++i)\n\t\tfor (j = 0; j < M; ++j)\n\t\t\tscanf(\"%d\", &C[i][j]);\n\tfor (i = 0; i < N; ++i)\n\t\tfor (j = 0; j < M; ++j)\n\t\t\tscanf(\"%d\", &D[i][j]);\n\n\tvector<pair<int, int>> path;\n\tint lastX, lastY, currX, currY;\n\tscanf(\"%d %d\", &lastX, &lastY);\n\twhile (Q-- > 1) {\n\t\tscanf(\"%d %d\", &currX, &currY);\n\t\tassert(lastX != currX || lastY != currY);\n\t\tpath.push_back({lastX, lastY});\n\t\tgetPath(lastX, lastY, currX, currY, path);\n\t\tlastX = currX, lastY = currY;\n\t}\n\tpath.push_back({lastX, lastY});\n\n\tassert(path.size() < 2500000);\n\n\tfor (size_t i = 0; i < path.size(); ++i)\n\t\tE[path[i].first][path[i].second].push_back(i);\n\n\tbool lightOn[NMAX][NMAX] = {};\n\tint answer = 0;\n\tint currAdd = 0;\n\tfor (size_t i = 0; i < path.size(); ++i) {\n\t\tanswer += currAdd;\n\t\tif (!lightOn[path[i].first][path[i].second])\n\t\t\tanswer += C[path[i].first][path[i].second];\n\t\tE[path[i].first][path[i].second].pop_front();\n\t\tint front = E[path[i].first][path[i].second].front();\n\t\tif (E[path[i].first][path[i].second].size() && (front - i) * B[path[i].first][path[i].second] < C[path[i].first][path[i].second] + D[path[i].first][path[i].second]) {\n\t\t\tif (!lightOn[path[i].first][path[i].second])\n\t\t\t\tcurrAdd += B[path[i].first][path[i].second];\n\t\t\tlightOn[path[i].first][path[i].second] = 1;\n\t\t} else {\n\t\t\tif (lightOn[path[i].first][path[i].second])\n\t\t\t\tcurrAdd -= B[path[i].first][path[i].second];\n\t\t\tlightOn[path[i].first][path[i].second] = 0;\n\t\t\tanswer += D[path[i].first][path[i].second];\n\t\t}\n\t}\n\n\tcout << answer << '\\n';\n\treturn 0;\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 53052, "score_of_the_acc": -1.3604, "final_rank": 11 }, { "submission_id": "aoj_2302_10730500", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\nconst int NMAX = 60, PMAX = 1010;\nconst int dl[] = {1, 0, -1, 0};\nconst int dc[] = {0, 1, 0, -1};\n\nint N, M, Q;\nchar A[NMAX][NMAX];\nint B[NMAX][NMAX];\nint C[NMAX][NMAX];\nint D[NMAX][NMAX];\nlist<int> E[NMAX][NMAX];\npair<int, int> from[NMAX][NMAX];\nbool witness[NMAX][NMAX];\n\nvector<pair<int, int>> getPath(int startX, int startY, int finishX, int finishY) {\n\tvector<pair<int, int>> answer;\n\tqueue<pair<int, int>> Q;\n\n\tmemset(witness, 0, sizeof witness);\n\tmemset(from, 0, sizeof from);\n\tQ.push({startX, startY});\n\twitness[startX][startY] = 1;\n\twhile (!Q.empty()) {\n\t\tint currX, currY;\n\t\ttie(currX, currY) = Q.front();\n\t\tQ.pop();\n\t\tfor (int i = 0; i < 4; ++i) {\n\t\t\tint newX = currX + dl[i];\n\t\t\tint newY = currY + dc[i];\n\t\t\tif (newX < 0 || newX >= N || newY < 0 || newY >= M)\n\t\t\t\tcontinue;\n\t\t\tif (A[newX][newY] != '.' || witness[newX][newY])\n\t\t\t\tcontinue;\n\t\t\tfrom[newX][newY] = {currX, currY};\n\t\t\twitness[newX][newY] = 1;\n\t\t\tQ.push({newX, newY});\n\t\t\tif (newX == finishX && newY == finishY)\n\t\t\t\tbreak;\n\t\t}\n\t}\n\tint currX, currY;\n\ttie(currX, currY) = from[finishX][finishY];\n\twhile (currX != startX || currY != startY) {\n\t\tanswer.push_back({currX, currY});\n\t\ttie(currX, currY) = from[currX][currY];\n\t}\n\treverse(answer.begin(), answer.end());\n\treturn answer;\n}\n\nint main() {\n//\tassert(freopen(\"debug.in\", \"r\", stdin));\n//\tassert(freopen(\"debug.out\", \"w\", stdout));\n\n\tint i, j;\n\n\tcin >> N >> M >> Q;\n\tfor (i = 0; i < N; ++i)\n\t\tcin >> A[i];\n\tfor (i = 0; i < N; ++i)\n\t\tfor (j = 0; j < M; ++j)\n\t\t\tcin >> B[i][j];\n\tfor (i = 0; i < N; ++i)\n\t\tfor (j = 0; j < M; ++j)\n\t\t\tcin >> C[i][j];\n\tfor (i = 0; i < N; ++i)\n\t\tfor (j = 0; j < M; ++j)\n\t\t\tcin >> D[i][j];\n\n\tvector<pair<int, int>> path;\n\tint lastX, lastY, currX, currY;\n\tcin >> lastX >> lastY;\n\twhile (Q-- > 1) {\n\t\tcin >> currX >> currY;\n\t\tpath.push_back({lastX, lastY});\n\t\tvector<pair<int, int>> currPath = getPath(lastX, lastY, currX, currY);\n\t\tfor (auto it: currPath)\n\t\t\tpath.push_back(it);\n\t\tlastX = currX, lastY = currY;\n\t}\n\tpath.push_back({lastX, lastY});\n\n\tfor (size_t i = 0; i < path.size(); ++i)\n\t\tE[path[i].first][path[i].second].push_back(i);\n\n\tbool lightOn[NMAX][NMAX] = {};\n\tint answer = 0;\n\tint currAdd = 0;\n\tfor (size_t i = 0; i < path.size(); ++i) {\n\t\tanswer += currAdd;\n\t\tif (!lightOn[path[i].first][path[i].second])\n\t\t\tanswer += C[path[i].first][path[i].second];\n\t\tE[path[i].first][path[i].second].pop_front();\n\t\tint front = E[path[i].first][path[i].second].front();\n\t\tif (E[path[i].first][path[i].second].size() && (front - i) * B[path[i].first][path[i].second] < C[path[i].first][path[i].second] + D[path[i].first][path[i].second]) {\n\t\t\tif (!lightOn[path[i].first][path[i].second])\n\t\t\t\tcurrAdd += B[path[i].first][path[i].second];\n\t\t\tlightOn[path[i].first][path[i].second] = 1;\n\t\t} else {\n\t\t\tif (lightOn[path[i].first][path[i].second])\n\t\t\t\tcurrAdd -= B[path[i].first][path[i].second];\n\t\t\tlightOn[path[i].first][path[i].second] = 0;\n\t\t\tanswer += D[path[i].first][path[i].second];\n\t\t}\n\t}\n\n\tcout << answer << '\\n';\n\treturn 0;\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 54572, "score_of_the_acc": -1.4566, "final_rank": 17 }, { "submission_id": "aoj_2302_10730499", "code_snippet": "#include <iostream>\n#include <cstdio>\n#include <cstring>\n#include <cmath>\n#include <algorithm>\n#include <vector>\n#include <map>\n#include <string>\n#include <queue>\nusing namespace std;\n#define SZ(v) ((int)(v).size())\n#define rep(i, n) for (int i = 0; i < (n); ++i)\n#define repf(i, a, b) for (int i = (a); i <= (b); ++i)\nconst int maxint = -1u>>1;\nconst int maxn = 50 + 2;\nconst int maxm = 1000 + 10;\nconst int dx[] = {-1, 0, 1, 0};\nconst int dy[] = {0, -1, 0, 1};\n\nint r, c, m;\nchar s[maxn][maxn];\nint open[maxn][maxn], inst[maxn][maxn], close[maxn][maxn];\npair<int, int> rec[maxm];\nvector<pair<int, int> > p;\nvector<int> tm[maxn][maxn];\nbool used[maxn][maxn];\n\nbool dfs(int x1, int y1, int x2, int y2) {\n if (x1 == x2 && y1 == y2) return true;\n if (used[x1][y1]) return false;\n used[x1][y1] = true;\n p.push_back(make_pair(x1, y1));\n for (int i = 0; i < 4; ++i) {\n int tx = x1 + dx[i], ty = y1 + dy[i];\n if (tx < 0 || ty < 0 || tx >= r || ty >= c || s[tx][ty] != '.') continue;\n if (dfs(tx, ty, x2, y2)) return true;\n }\n p.pop_back();\n return false;\n}\nint main() {\n while (scanf(\"%d%d%d\", &r, &c, &m) == 3) {\n for (int i = 0; i < r; ++i) {\n scanf(\"%s\", s[i]);\n }\n for (int i = 0; i < r; ++i) {\n for (int j = 0; j < c; ++j) {\n tm[i][j].clear();\n scanf(\"%d\", &inst[i][j]);\n }\n }\n for (int i = 0; i < r; ++i) {\n for (int j = 0; j < c; ++j) {\n scanf(\"%d\", &open[i][j]);\n }\n }\n for (int i = 0; i < r; ++i) {\n for (int j = 0; j < c; ++j) {\n scanf(\"%d\", &close[i][j]);\n }\n }\n for (int i = 0; i < m; ++i) {\n scanf(\"%d%d\", &rec[i].first, &rec[i].second);\n }\n int now = 0;\n for (int i = 1; i < m; ++i) {\n memset(used, 0, sizeof(used));\n p.clear();\n dfs(rec[i - 1].first, rec[i - 1].second, rec[i].first, rec[i].second);\n for (int j = 0; j < (int) p.size(); ++j) {\n tm[p[j].first][p[j].second].push_back(now);\n ++now;\n }\n }\n tm[rec[m - 1].first][rec[m - 1].second].push_back(now);\n int ans = 0;\n for (int i = 0; i < r; ++i) {\n for (int j = 0; j < c; ++j) {\n if (tm[i][j].size() == 0) continue;\n //printf(\"%d %d: \", i, j);for (int k = 0; k < (int)tm[i][j].size(); ++k) printf(\"%d \", tm[i][j][k]); printf(\"\\n\");\n ans += open[i][j] + close[i][j] + inst[i][j] * (tm[i][j].back() - tm[i][j].front());\n for (int k = 1; k < (int)tm[i][j].size(); ++k) {\n if ((tm[i][j][k] - tm[i][j][k - 1]) * inst[i][j] > open[i][j] + close[i][j]) {\n ans -= (tm[i][j][k] - tm[i][j][k - 1]) * inst[i][j] - open[i][j] - close[i][j];\n }\n }\n }\n }\n printf(\"%d\\n\", ans);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 8628, "score_of_the_acc": -0.1641, "final_rank": 1 }, { "submission_id": "aoj_2302_10726699", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\nconst int NMAX = 60, PMAX = 1010;\nconst int dl[] = {1, 0, -1, 0};\nconst int dc[] = {0, 1, 0, -1};\n\nint N, M, Q;\nchar A[NMAX][NMAX];\nint B[NMAX][NMAX];\nint C[NMAX][NMAX];\nint D[NMAX][NMAX];\nint Dist[NMAX][NMAX];\nlist<int> E[NMAX][NMAX];\npair<int, int> from[NMAX][NMAX];\nbool witness[NMAX][NMAX];\n\nvoid getPath(int startX, int startY, int finishX, int finishY, vector<pair<int, int>> &dest) {\n\tqueue<pair<int, int>> Q;\n\n\tmemset(witness, 0, sizeof witness);\n\tQ.push({startX, startY});\n\twitness[startX][startY] = 1;\n\tDist[startX][startY] = 0;\n\tbool found = 0;\n\twhile (!Q.empty()) {\n\t\tint currX, currY;\n\t\ttie(currX, currY) = Q.front();\n\t\tQ.pop();\n\t\tfor (int i = 0; i < 4; ++i) {\n\t\t\tint newX = currX + dl[i];\n\t\t\tint newY = currY + dc[i];\n\t\t\tif (newX < 0 || newX >= N || newY < 0 || newY >= M)\n\t\t\t\tcontinue;\n\t\t\tif (A[newX][newY] != '.' || witness[newX][newY])\n\t\t\t\tcontinue;\n\t\t\tfrom[newX][newY] = {currX, currY};\n\t\t\tDist[newX][newY] = Dist[currX][currY] + 1;\n\t\t\twitness[newX][newY] = 1;\n\t\t\tQ.push({newX, newY});\n\t\t\tif (newX == finishX && newY == finishY) {\n\t\t\t\tfound = 1;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n\tassert(found);\n\tint currX, currY;\n\ttie(currX, currY) = from[finishX][finishY];\n\tint newSize = dest.size() + Dist[currX][currY];\n\tdest.resize(newSize);\n\t--newSize;\n\twhile (currX != startX || currY != startY) {\n\t\tdest[newSize--] = {currX, currY};\n\t\ttie(currX, currY) = from[currX][currY];\n\t}\n}\n\nint main() {\n//\tassert(freopen(\"debug.in\", \"r\", stdin));\n//\tassert(freopen(\"debug.out\", \"w\", stdout));\n\n\tint i, j;\n\n\tscanf(\"%d %d %d\", &N, &M, &Q);\n\tfor (i = 0; i < N; ++i)\n\t\tscanf(\"%s\", A[i]);\n\tfor (i = 0; i < N; ++i)\n\t\tfor (j = 0; j < M; ++j)\n\t\t\tscanf(\"%d\", &B[i][j]);\n\tfor (i = 0; i < N; ++i)\n\t\tfor (j = 0; j < M; ++j)\n\t\t\tscanf(\"%d\", &C[i][j]);\n\tfor (i = 0; i < N; ++i)\n\t\tfor (j = 0; j < M; ++j)\n\t\t\tscanf(\"%d\", &D[i][j]);\n\n\tvector<pair<int, int>> path;\n\tint lastX, lastY, currX, currY;\n\tscanf(\"%d %d\", &lastX, &lastY);\n\twhile (Q-- > 1) {\n\t\tscanf(\"%d %d\", &currX, &currY);\n\t\tassert(lastX != currX || lastY != currY);\n\t\tpath.push_back({lastX, lastY});\n\t\tgetPath(lastX, lastY, currX, currY, path);\n\t\tlastX = currX, lastY = currY;\n\t}\n\tpath.push_back({lastX, lastY});\n\n\tassert(path.size() < 2500000);\n\n\tfor (size_t i = 0; i < path.size(); ++i)\n\t\tE[path[i].first][path[i].second].push_back(i);\n\n\tbool lightOn[NMAX][NMAX] = {};\n\tint answer = 0;\n\tint currAdd = 0;\n\tfor (size_t i = 0; i < path.size(); ++i) {\n\t\tanswer += currAdd;\n\t\tif (!lightOn[path[i].first][path[i].second])\n\t\t\tanswer += C[path[i].first][path[i].second];\n\t\tE[path[i].first][path[i].second].pop_front();\n\t\tint front = E[path[i].first][path[i].second].front();\n\t\tif (E[path[i].first][path[i].second].size() && (front - i) * B[path[i].first][path[i].second] < C[path[i].first][path[i].second] + D[path[i].first][path[i].second]) {\n\t\t\tif (!lightOn[path[i].first][path[i].second])\n\t\t\t\tcurrAdd += B[path[i].first][path[i].second];\n\t\t\tlightOn[path[i].first][path[i].second] = 1;\n\t\t} else {\n\t\t\tif (lightOn[path[i].first][path[i].second])\n\t\t\t\tcurrAdd -= B[path[i].first][path[i].second];\n\t\t\tlightOn[path[i].first][path[i].second] = 0;\n\t\t\tanswer += D[path[i].first][path[i].second];\n\t\t}\n\t}\n\n\tcout << answer << '\\n';\n\treturn 0;\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 53104, "score_of_the_acc": -1.3615, "final_rank": 12 }, { "submission_id": "aoj_2302_10726697", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\nconst int NMAX = 60, PMAX = 1010;\nconst int dl[] = {1, 0, -1, 0};\nconst int dc[] = {0, 1, 0, -1};\n\nint N, M, Q;\nchar A[NMAX][NMAX];\nint B[NMAX][NMAX];\nint C[NMAX][NMAX];\nint D[NMAX][NMAX];\nint Dist[NMAX][NMAX];\nlist<int> E[NMAX][NMAX];\npair<int, int> from[NMAX][NMAX];\nbool witness[NMAX][NMAX];\n\nvoid getPath(int startX, int startY, int finishX, int finishY, vector<pair<int, int>> &dest) {\n\tqueue<pair<int, int>> Q;\n\n\tmemset(witness, 0, sizeof witness);\n\tQ.push({startX, startY});\n\twitness[startX][startY] = 1;\n\tDist[startX][startY] = 0;\n\tbool found = 0;\n\twhile (!Q.empty()) {\n\t\tint currX, currY;\n\t\ttie(currX, currY) = Q.front();\n\t\tQ.pop();\n\t\tfor (int i = 0; i < 4; ++i) {\n\t\t\tint newX = currX + dl[i];\n\t\t\tint newY = currY + dc[i];\n\t\t\tif (newX < 0 || newX >= N || newY < 0 || newY >= M)\n\t\t\t\tcontinue;\n\t\t\tif (A[newX][newY] != '.' || witness[newX][newY])\n\t\t\t\tcontinue;\n\t\t\tfrom[newX][newY] = {currX, currY};\n\t\t\tDist[newX][newY] = Dist[currX][currY] + 1;\n\t\t\twitness[newX][newY] = 1;\n\t\t\tQ.push({newX, newY});\n\t\t\tif (newX == finishX && newY == finishY) {\n\t\t\t\tfound = 1;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n\tassert(found);\n\tint currX, currY;\n\ttie(currX, currY) = from[finishX][finishY];\n\tint newSize = dest.size() + Dist[currX][currY];\n\tdest.resize(newSize);\n\t--newSize;\n\twhile (currX != startX || currY != startY) {\n\t\tdest[newSize--] = {currX, currY};\n\t\ttie(currX, currY) = from[currX][currY];\n\t}\n}\n\nint main() {\n//\tassert(freopen(\"debug.in\", \"r\", stdin));\n//\tassert(freopen(\"debug.out\", \"w\", stdout));\n\n\tint i, j;\n\n\tscanf(\"%d %d %d\", &N, &M, &Q);\n\tfor (i = 0; i < N; ++i)\n\t\tscanf(\"%s\", A[i]);\n\tfor (i = 0; i < N; ++i)\n\t\tfor (j = 0; j < M; ++j)\n\t\t\tscanf(\"%d\", &B[i][j]);\n\tfor (i = 0; i < N; ++i)\n\t\tfor (j = 0; j < M; ++j)\n\t\t\tscanf(\"%d\", &C[i][j]);\n\tfor (i = 0; i < N; ++i)\n\t\tfor (j = 0; j < M; ++j)\n\t\t\tscanf(\"%d\", &D[i][j]);\n\n\tvector<pair<int, int>> path;\n\tint lastX, lastY, currX, currY;\n\tscanf(\"%d %d\", &lastX, &lastY);\n\twhile (Q-- > 1) {\n\t\tscanf(\"%d %d\", &currX, &currY);\n\t\tassert(lastX != currX || lastY != currY);\n\t\tpath.push_back({lastX, lastY});\n\t\tgetPath(lastX, lastY, currX, currY, path);\n\t\tlastX = currX, lastY = currY;\n\t}\n\tpath.push_back({lastX, lastY});\n\n\tfor (size_t i = 0; i < path.size(); ++i)\n\t\tE[path[i].first][path[i].second].push_back(i);\n\n\tbool lightOn[NMAX][NMAX] = {};\n\tint answer = 0;\n\tint currAdd = 0;\n\tfor (size_t i = 0; i < path.size(); ++i) {\n\t\tanswer += currAdd;\n\t\tif (!lightOn[path[i].first][path[i].second])\n\t\t\tanswer += C[path[i].first][path[i].second];\n\t\tE[path[i].first][path[i].second].pop_front();\n\t\tint front = E[path[i].first][path[i].second].front();\n\t\tif (E[path[i].first][path[i].second].size() && (front - i) * B[path[i].first][path[i].second] < C[path[i].first][path[i].second] + D[path[i].first][path[i].second]) {\n\t\t\tif (!lightOn[path[i].first][path[i].second])\n\t\t\t\tcurrAdd += B[path[i].first][path[i].second];\n\t\t\tlightOn[path[i].first][path[i].second] = 1;\n\t\t} else {\n\t\t\tif (lightOn[path[i].first][path[i].second])\n\t\t\t\tcurrAdd -= B[path[i].first][path[i].second];\n\t\t\tlightOn[path[i].first][path[i].second] = 0;\n\t\t\tanswer += D[path[i].first][path[i].second];\n\t\t}\n\t}\n\n\tcout << answer << '\\n';\n\treturn 0;\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 53172, "score_of_the_acc": -1.3628, "final_rank": 14 }, { "submission_id": "aoj_2302_10726695", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\nconst int NMAX = 60, PMAX = 1010;\nconst int dl[] = {1, 0, -1, 0};\nconst int dc[] = {0, 1, 0, -1};\n\nint N, M, Q;\nchar A[NMAX][NMAX];\nint B[NMAX][NMAX];\nint C[NMAX][NMAX];\nint D[NMAX][NMAX];\nint Dist[NMAX][NMAX];\nlist<int> E[NMAX][NMAX];\npair<int, int> from[NMAX][NMAX];\nbool witness[NMAX][NMAX];\n\nvoid getPath(int startX, int startY, int finishX, int finishY, vector<pair<int, int>> &dest) {\n\tqueue<pair<int, int>> Q;\n\n\tmemset(witness, 0, sizeof witness);\n\tQ.push({startX, startY});\n\twitness[startX][startY] = 1;\n\tDist[startX][startY] = 0;\n\twhile (!Q.empty()) {\n\t\tint currX, currY;\n\t\ttie(currX, currY) = Q.front();\n\t\tQ.pop();\n\t\tfor (int i = 0; i < 4; ++i) {\n\t\t\tint newX = currX + dl[i];\n\t\t\tint newY = currY + dc[i];\n\t\t\tif (newX < 0 || newX >= N || newY < 0 || newY >= M)\n\t\t\t\tcontinue;\n\t\t\tif (A[newX][newY] != '.' || witness[newX][newY])\n\t\t\t\tcontinue;\n\t\t\tfrom[newX][newY] = {currX, currY};\n\t\t\tDist[newX][newY] = Dist[currX][currY] + 1;\n\t\t\twitness[newX][newY] = 1;\n\t\t\tQ.push({newX, newY});\n\t\t\tif (newX == finishX && newY == finishY)\n\t\t\t\tbreak;\n\t\t}\n\t}\n\tint currX, currY;\n\ttie(currX, currY) = from[finishX][finishY];\n\tint newSize = dest.size() + Dist[currX][currY];\n\tdest.resize(newSize);\n\t--newSize;\n\twhile (currX != startX || currY != startY) {\n\t\tdest[newSize--] = {currX, currY};\n\t\ttie(currX, currY) = from[currX][currY];\n\t}\n}\n\nint main() {\n//\tassert(freopen(\"debug.in\", \"r\", stdin));\n//\tassert(freopen(\"debug.out\", \"w\", stdout));\n\n\tint i, j;\n\n\tscanf(\"%d %d %d\", &N, &M, &Q);\n\tfor (i = 0; i < N; ++i)\n\t\tscanf(\"%s\", A[i]);\n\tfor (i = 0; i < N; ++i)\n\t\tfor (j = 0; j < M; ++j)\n\t\t\tscanf(\"%d\", &B[i][j]);\n\tfor (i = 0; i < N; ++i)\n\t\tfor (j = 0; j < M; ++j)\n\t\t\tscanf(\"%d\", &C[i][j]);\n\tfor (i = 0; i < N; ++i)\n\t\tfor (j = 0; j < M; ++j)\n\t\t\tscanf(\"%d\", &D[i][j]);\n\n\tvector<pair<int, int>> path;\n\tint lastX, lastY, currX, currY;\n\tscanf(\"%d %d\", &lastX, &lastY);\n\twhile (Q-- > 1) {\n\t\tscanf(\"%d %d\", &currX, &currY);\n\t\tassert(lastX != currX || lastY != currY);\n\t\tpath.push_back({lastX, lastY});\n\t\tgetPath(lastX, lastY, currX, currY, path);\n\t\tlastX = currX, lastY = currY;\n\t}\n\tpath.push_back({lastX, lastY});\n\n\tfor (size_t i = 0; i < path.size(); ++i)\n\t\tE[path[i].first][path[i].second].push_back(i);\n\n\tbool lightOn[NMAX][NMAX] = {};\n\tint answer = 0;\n\tint currAdd = 0;\n\tfor (size_t i = 0; i < path.size(); ++i) {\n\t\tanswer += currAdd;\n\t\tif (!lightOn[path[i].first][path[i].second])\n\t\t\tanswer += C[path[i].first][path[i].second];\n\t\tE[path[i].first][path[i].second].pop_front();\n\t\tint front = E[path[i].first][path[i].second].front();\n\t\tif (E[path[i].first][path[i].second].size() && (front - i) * B[path[i].first][path[i].second] < C[path[i].first][path[i].second] + D[path[i].first][path[i].second]) {\n\t\t\tif (!lightOn[path[i].first][path[i].second])\n\t\t\t\tcurrAdd += B[path[i].first][path[i].second];\n\t\t\tlightOn[path[i].first][path[i].second] = 1;\n\t\t} else {\n\t\t\tif (lightOn[path[i].first][path[i].second])\n\t\t\t\tcurrAdd -= B[path[i].first][path[i].second];\n\t\t\tlightOn[path[i].first][path[i].second] = 0;\n\t\t\tanswer += D[path[i].first][path[i].second];\n\t\t}\n\t}\n\n\tcout << answer << '\\n';\n\treturn 0;\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 53132, "score_of_the_acc": -1.362, "final_rank": 13 }, { "submission_id": "aoj_2302_10726693", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\nconst int NMAX = 60, PMAX = 1010;\nconst int dl[] = {1, 0, -1, 0};\nconst int dc[] = {0, 1, 0, -1};\n\nint N, M, Q;\nchar A[NMAX][NMAX];\nint B[NMAX][NMAX];\nint C[NMAX][NMAX];\nint D[NMAX][NMAX];\nint Dist[NMAX][NMAX];\nlist<int> E[NMAX][NMAX];\npair<int, int> from[NMAX][NMAX];\nbool witness[NMAX][NMAX];\n\nvoid getPath(int startX, int startY, int finishX, int finishY, vector<pair<int, int>> &dest) {\n\tqueue<pair<int, int>> Q;\n\n\tmemset(witness, 0, sizeof witness);\n\tQ.push({startX, startY});\n\twitness[startX][startY] = 1;\n\tDist[startX][startY] = 0;\n\twhile (!Q.empty()) {\n\t\tint currX, currY;\n\t\ttie(currX, currY) = Q.front();\n\t\tQ.pop();\n\t\tfor (int i = 0; i < 4; ++i) {\n\t\t\tint newX = currX + dl[i];\n\t\t\tint newY = currY + dc[i];\n\t\t\tif (newX < 0 || newX >= N || newY < 0 || newY >= M)\n\t\t\t\tcontinue;\n\t\t\tif (A[newX][newY] != '.' || witness[newX][newY])\n\t\t\t\tcontinue;\n\t\t\tfrom[newX][newY] = {currX, currY};\n\t\t\tDist[newX][newY] = Dist[currX][currY] + 1;\n\t\t\twitness[newX][newY] = 1;\n\t\t\tQ.push({newX, newY});\n\t\t\tif (newX == finishX && newY == finishY)\n\t\t\t\tbreak;\n\t\t}\n\t}\n\tint currX, currY;\n\ttie(currX, currY) = from[finishX][finishY];\n\tint newSize = dest.size() + Dist[currX][currY];\n\tdest.resize(newSize);\n\t--newSize;\n\twhile (currX != startX || currY != startY) {\n\t\tdest[newSize--] = {currX, currY};\n\t\ttie(currX, currY) = from[currX][currY];\n\t}\n}\n\nint main() {\n//\tassert(freopen(\"debug.in\", \"r\", stdin));\n//\tassert(freopen(\"debug.out\", \"w\", stdout));\n\n\tint i, j;\n\n\tscanf(\"%d %d %d\", &N, &M, &Q);\n\tfor (i = 0; i < N; ++i)\n\t\tscanf(\"%s\", A[i]);\n\tfor (i = 0; i < N; ++i)\n\t\tfor (j = 0; j < M; ++j)\n\t\t\tscanf(\"%d\", &B[i][j]);\n\tfor (i = 0; i < N; ++i)\n\t\tfor (j = 0; j < M; ++j)\n\t\t\tscanf(\"%d\", &C[i][j]);\n\tfor (i = 0; i < N; ++i)\n\t\tfor (j = 0; j < M; ++j)\n\t\t\tscanf(\"%d\", &D[i][j]);\n\n\tvector<pair<int, int>> path;\n\tint lastX, lastY, currX, currY;\n\tscanf(\"%d %d\", &lastX, &lastY);\n\twhile (Q-- > 1) {\n\t\tscanf(\"%d %d\", &currX, &currY);\n\t\tpath.push_back({lastX, lastY});\n\t\tgetPath(lastX, lastY, currX, currY, path);\n\t\tlastX = currX, lastY = currY;\n\t}\n\tpath.push_back({lastX, lastY});\n\n\tfor (size_t i = 0; i < path.size(); ++i)\n\t\tE[path[i].first][path[i].second].push_back(i);\n\n\tbool lightOn[NMAX][NMAX] = {};\n\tint answer = 0;\n\tint currAdd = 0;\n\tfor (size_t i = 0; i < path.size(); ++i) {\n\t\tanswer += currAdd;\n\t\tif (!lightOn[path[i].first][path[i].second])\n\t\t\tanswer += C[path[i].first][path[i].second];\n\t\tE[path[i].first][path[i].second].pop_front();\n\t\tint front = E[path[i].first][path[i].second].front();\n\t\tif (E[path[i].first][path[i].second].size() && (front - i) * B[path[i].first][path[i].second] < C[path[i].first][path[i].second] + D[path[i].first][path[i].second]) {\n\t\t\tif (!lightOn[path[i].first][path[i].second])\n\t\t\t\tcurrAdd += B[path[i].first][path[i].second];\n\t\t\tlightOn[path[i].first][path[i].second] = 1;\n\t\t} else {\n\t\t\tif (lightOn[path[i].first][path[i].second])\n\t\t\t\tcurrAdd -= B[path[i].first][path[i].second];\n\t\t\tlightOn[path[i].first][path[i].second] = 0;\n\t\t\tanswer += D[path[i].first][path[i].second];\n\t\t}\n\t}\n\n\tcout << answer << '\\n';\n\treturn 0;\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 53264, "score_of_the_acc": -1.3646, "final_rank": 16 }, { "submission_id": "aoj_2302_10726692", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\nconst int NMAX = 60, PMAX = 1010;\nconst int dl[] = {1, 0, -1, 0};\nconst int dc[] = {0, 1, 0, -1};\n\nint N, M, Q;\nchar A[NMAX][NMAX];\nint B[NMAX][NMAX];\nint C[NMAX][NMAX];\nint D[NMAX][NMAX];\nlist<int> E[NMAX][NMAX];\npair<int, int> from[NMAX][NMAX];\nbool witness[NMAX][NMAX];\n\nvector<pair<int, int>> getPath(int startX, int startY, int finishX, int finishY) {\n\tvector<pair<int, int>> answer;\n\tqueue<pair<int, int>> Q;\n\n\tmemset(witness, 0, sizeof witness);\n\tQ.push({startX, startY});\n\twitness[startX][startY] = 1;\n\twhile (!Q.empty()) {\n\t\tint currX, currY;\n\t\ttie(currX, currY) = Q.front();\n\t\tQ.pop();\n\t\tfor (int i = 0; i < 4; ++i) {\n\t\t\tint newX = currX + dl[i];\n\t\t\tint newY = currY + dc[i];\n\t\t\tif (newX < 0 || newX >= N || newY < 0 || newY >= M)\n\t\t\t\tcontinue;\n\t\t\tif (A[newX][newY] != '.' || witness[newX][newY])\n\t\t\t\tcontinue;\n\t\t\tfrom[newX][newY] = {currX, currY};\n\t\t\twitness[newX][newY] = 1;\n\t\t\tQ.push({newX, newY});\n\t\t\tif (newX == finishX && newY == finishY)\n\t\t\t\tbreak;\n\t\t}\n\t}\n\tint currX, currY;\n\ttie(currX, currY) = from[finishX][finishY];\n\twhile (currX != startX || currY != startY) {\n\t\tanswer.push_back({currX, currY});\n\t\ttie(currX, currY) = from[currX][currY];\n\t}\n\treverse(answer.begin(), answer.end());\n\treturn answer;\n}\n\nint main() {\n//\tassert(freopen(\"debug.in\", \"r\", stdin));\n//\tassert(freopen(\"debug.out\", \"w\", stdout));\n\n\tint i, j;\n\n\tscanf(\"%d %d %d\", &N, &M, &Q);\n\tfor (i = 0; i < N; ++i)\n\t\tscanf(\"%s\", A[i]);\n\tfor (i = 0; i < N; ++i)\n\t\tfor (j = 0; j < M; ++j)\n\t\t\tscanf(\"%d\", &B[i][j]);\n\tfor (i = 0; i < N; ++i)\n\t\tfor (j = 0; j < M; ++j)\n\t\t\tscanf(\"%d\", &C[i][j]);\n\tfor (i = 0; i < N; ++i)\n\t\tfor (j = 0; j < M; ++j)\n\t\t\tscanf(\"%d\", &D[i][j]);\n\n\tvector<pair<int, int>> path;\n\tint lastX, lastY, currX, currY;\n\tscanf(\"%d %d\", &lastX, &lastY);\n\twhile (Q-- > 1) {\n\t\tscanf(\"%d %d\", &currX, &currY);\n\t\tpath.push_back({lastX, lastY});\n\t\tvector<pair<int, int>> currPath = getPath(lastX, lastY, currX, currY);\n\t\tfor (auto it: currPath)\n\t\t\tpath.push_back(it);\n\t\tlastX = currX, lastY = currY;\n\t}\n\tpath.push_back({lastX, lastY});\n\n\tfor (size_t i = 0; i < path.size(); ++i)\n\t\tE[path[i].first][path[i].second].push_back(i);\n\n\tbool lightOn[NMAX][NMAX] = {};\n\tint answer = 0;\n\tint currAdd = 0;\n\tfor (size_t i = 0; i < path.size(); ++i) {\n\t\tanswer += currAdd;\n\t\tif (!lightOn[path[i].first][path[i].second])\n\t\t\tanswer += C[path[i].first][path[i].second];\n\t\tE[path[i].first][path[i].second].pop_front();\n\t\tint front = E[path[i].first][path[i].second].front();\n\t\tif (E[path[i].first][path[i].second].size() && (front - i) * B[path[i].first][path[i].second] < C[path[i].first][path[i].second] + D[path[i].first][path[i].second]) {\n\t\t\tif (!lightOn[path[i].first][path[i].second])\n\t\t\t\tcurrAdd += B[path[i].first][path[i].second];\n\t\t\tlightOn[path[i].first][path[i].second] = 1;\n\t\t} else {\n\t\t\tif (lightOn[path[i].first][path[i].second])\n\t\t\t\tcurrAdd -= B[path[i].first][path[i].second];\n\t\t\tlightOn[path[i].first][path[i].second] = 0;\n\t\t\tanswer += D[path[i].first][path[i].second];\n\t\t}\n\t}\n\n\tcout << answer << '\\n';\n\treturn 0;\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 54868, "score_of_the_acc": -1.4624, "final_rank": 18 }, { "submission_id": "aoj_2302_10726688", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\nconst int NMAX = 60, PMAX = 1010;\nconst int dl[] = {1, 0, -1, 0};\nconst int dc[] = {0, 1, 0, -1};\n\nint N, M, Q;\nchar A[NMAX][NMAX];\nint B[NMAX][NMAX];\nint C[NMAX][NMAX];\nint D[NMAX][NMAX];\nlist<int> E[NMAX][NMAX];\npair<int, int> from[NMAX][NMAX];\nbool witness[NMAX][NMAX];\n\nvector<pair<int, int>> getPath(int startX, int startY, int finishX, int finishY) {\n\tvector<pair<int, int>> answer;\n\tqueue<pair<int, int>> Q;\n\n\tmemset(witness, 0, sizeof witness);\n\tmemset(from, 0, sizeof from);\n\tQ.push({startX, startY});\n\twitness[startX][startY] = 1;\n\twhile (!Q.empty()) {\n\t\tint currX, currY;\n\t\ttie(currX, currY) = Q.front();\n\t\tQ.pop();\n\t\tfor (int i = 0; i < 4; ++i) {\n\t\t\tint newX = currX + dl[i];\n\t\t\tint newY = currY + dc[i];\n\t\t\tif (newX < 0 || newX >= N || newY < 0 || newY >= M)\n\t\t\t\tcontinue;\n\t\t\tif (A[newX][newY] != '.' || witness[newX][newY])\n\t\t\t\tcontinue;\n\t\t\tfrom[newX][newY] = {currX, currY};\n\t\t\twitness[newX][newY] = 1;\n\t\t\tQ.push({newX, newY});\n\t\t\tif (newX == finishX && newY == finishY)\n\t\t\t\tbreak;\n\t\t}\n\t}\n\tint currX, currY;\n\ttie(currX, currY) = from[finishX][finishY];\n\twhile (currX != startX || currY != startY) {\n\t\tanswer.push_back({currX, currY});\n\t\ttie(currX, currY) = from[currX][currY];\n\t}\n\treverse(answer.begin(), answer.end());\n\treturn answer;\n}\n\nint main() {\n//\tassert(freopen(\"debug.in\", \"r\", stdin));\n//\tassert(freopen(\"debug.out\", \"w\", stdout));\n\n\tint i, j;\n\n\tcin >> N >> M >> Q;\n\tfor (i = 0; i < N; ++i)\n\t\tcin >> A[i];\n\tfor (i = 0; i < N; ++i)\n\t\tfor (j = 0; j < M; ++j)\n\t\t\tcin >> B[i][j];\n\tfor (i = 0; i < N; ++i)\n\t\tfor (j = 0; j < M; ++j)\n\t\t\tcin >> C[i][j];\n\tfor (i = 0; i < N; ++i)\n\t\tfor (j = 0; j < M; ++j)\n\t\t\tcin >> D[i][j];\n\n\tvector<pair<int, int>> path;\n\tint lastX, lastY, currX, currY;\n\tcin >> lastX >> lastY;\n\twhile (Q-- > 1) {\n\t\tcin >> currX >> currY;\n\t\tpath.push_back({lastX, lastY});\n\t\tvector<pair<int, int>> currPath = getPath(lastX, lastY, currX, currY);\n\t\tfor (auto it: currPath)\n\t\t\tpath.push_back(it);\n\t\tlastX = currX, lastY = currY;\n\t}\n\tpath.push_back({lastX, lastY});\n\n\tfor (size_t i = 0; i < path.size(); ++i)\n\t\tE[path[i].first][path[i].second].push_back(i);\n\n\tset<pair<int, int>> lightOn;\n\tint answer = 0;\n\tint currAdd = 0;\n\tfor (size_t i = 0; i < path.size(); ++i) {\n\t\tanswer += currAdd;\n\t\tif (!lightOn.count(path[i]))\n\t\t\tanswer += C[path[i].first][path[i].second];\n\t\tE[path[i].first][path[i].second].pop_front();\n\t\tint front = E[path[i].first][path[i].second].front();\n\t\tif (E[path[i].first][path[i].second].size() && (front - i) * B[path[i].first][path[i].second] < C[path[i].first][path[i].second] + D[path[i].first][path[i].second]) {\n\t\t\tif (!lightOn.count(path[i]))\n\t\t\t\tcurrAdd += B[path[i].first][path[i].second];\n\t\t\tlightOn.insert(path[i]);\n\t\t} else {\n\t\t\tif (lightOn.count(path[i]))\n\t\t\t\tcurrAdd -= B[path[i].first][path[i].second];\n\t\t\tlightOn.erase(path[i]);\n\t\t\tanswer += D[path[i].first][path[i].second];\n\t\t}\n\t}\n\n\tcout << answer << '\\n';\n\treturn 0;\n}", "accuracy": 1, "time_ms": 90, "memory_kb": 55088, "score_of_the_acc": -1.5333, "final_rank": 19 }, { "submission_id": "aoj_2302_10726686", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\nconst int NMAX = 60, PMAX = 1010;\nconst int dl[] = {1, 0, -1, 0};\nconst int dc[] = {0, 1, 0, -1};\n\nint N, M, Q;\nchar A[NMAX][NMAX];\nint B[NMAX][NMAX];\nint C[NMAX][NMAX];\nint D[NMAX][NMAX];\nlist<int> E[NMAX][NMAX];\npair<int, int> from[NMAX][NMAX];\nbool witness[NMAX][NMAX];\n\nvector<pair<int, int>> getPath(int startX, int startY, int finishX, int finishY) {\n\tvector<pair<int, int>> answer;\n\tqueue<pair<int, int>> Q;\n\n\tmemset(witness, 0, sizeof witness);\n\tmemset(from, 0, sizeof from);\n\tQ.push({startX, startY});\n\twitness[startX][startY] = 1;\n\twhile (!Q.empty()) {\n\t\tint currX, currY;\n\t\ttie(currX, currY) = Q.front();\n\t\tQ.pop();\n\t\tfor (int i = 0; i < 4; ++i) {\n\t\t\tint newX = currX + dl[i];\n\t\t\tint newY = currY + dc[i];\n\t\t\tif (newX < 0 || newX >= N || newY < 0 || newY >= M)\n\t\t\t\tcontinue;\n\t\t\tif (A[newX][newY] != '.' || witness[newX][newY])\n\t\t\t\tcontinue;\n\t\t\tfrom[newX][newY] = {currX, currY};\n\t\t\twitness[newX][newY] = 1;\n\t\t\tQ.push({newX, newY});\n\t\t\tif (newX == finishX && newY == finishY)\n\t\t\t\tbreak;\n\t\t}\n\t}\n\tint currX, currY;\n\ttie(currX, currY) = from[finishX][finishY];\n\twhile (currX != startX || currY != startY) {\n\t\tanswer.push_back({currX, currY});\n\t\ttie(currX, currY) = from[currX][currY];\n\t}\n\treverse(answer.begin(), answer.end());\n\treturn answer;\n}\n\nint main() {\n//\tassert(freopen(\"debug.in\", \"r\", stdin));\n//\tassert(freopen(\"debug.out\", \"w\", stdout));\n\n\tint i, j;\n\n\tcin >> N >> M >> Q;\n\tfor (i = 0; i < N; ++i)\n\t\tcin >> A[i];\n\tfor (i = 0; i < N; ++i)\n\t\tfor (j = 0; j < M; ++j)\n\t\t\tcin >> B[i][j];\n\tfor (i = 0; i < N; ++i)\n\t\tfor (j = 0; j < M; ++j)\n\t\t\tcin >> C[i][j];\n\tfor (i = 0; i < N; ++i)\n\t\tfor (j = 0; j < M; ++j)\n\t\t\tcin >> D[i][j];\n\n\tvector<pair<int, int>> path;\n\tint lastX, lastY, currX, currY;\n\tcin >> lastX >> lastY;\n\twhile (Q-- > 1) {\n\t\tcin >> currX >> currY;\n\t\tpath.push_back({lastX, lastY});\n\t\tvector<pair<int, int>> currPath = getPath(lastX, lastY, currX, currY);\n\t\tfor (auto it: currPath)\n\t\t\tpath.push_back(it);\n\t\tlastX = currX, lastY = currY;\n\t}\n\tpath.push_back({lastX, lastY});\n\n\tfor (size_t i = 0; i < path.size(); ++i)\n\t\tE[path[i].first][path[i].second].push_back(i);\n\n\tset<pair<int, int>> lightOn;\n\tint answer = 0;\n\tfor (size_t i = 0; i < path.size(); ++i) {\n\t\tfor (auto it: lightOn)\n\t\t\tanswer += B[it.first][it.second];\n\t\tif (!lightOn.count(path[i]))\n\t\t\tanswer += C[path[i].first][path[i].second];\n\t\tE[path[i].first][path[i].second].pop_front();\n\t\tint front = E[path[i].first][path[i].second].front();\n\t\tif (E[path[i].first][path[i].second].size() && (front - i) * B[path[i].first][path[i].second] < C[path[i].first][path[i].second] + D[path[i].first][path[i].second]) {\n\t\t\tlightOn.insert(path[i]);\n\t\t} else {\n\t\t\tlightOn.erase(path[i]);\n\t\t\tanswer += D[path[i].first][path[i].second];\n\t\t}\n\t}\n\n\tcout << answer << '\\n';\n\treturn 0;\n}", "accuracy": 1, "time_ms": 160, "memory_kb": 54896, "score_of_the_acc": -1.9963, "final_rank": 20 }, { "submission_id": "aoj_2302_10726682", "code_snippet": "#include<stdio.h>\n#include<string.h>\nint room[60][60]={0},step[60][60]={0},appear[60][60][1010]={0};\nunsigned long long on[60][60],off[60][60],keep[60][60];\nint tasks[1000][2]={0};\nint n,m,ntask,total;\nconst int go[4][2]={{0,1},{0,-1},{1,0},{-1,0}};\n\nvoid indata()\n{\n int i,j,k;\n scanf(\"%d %d %d\\n\",&n,&m,&ntask);\n for (i=1;i<=n;i++)\n {\n for (j=1;j<=m;j++)\n {\n if (getchar()=='.')\n room[i][j]=1;\n }\n getchar();\n }\n for (i=1;i<=n;i++)\n for (j=1;j<=m;j++)\n scanf(\"%d\",&keep[i][j]);\n for (i=1;i<=n;i++)\n for (j=1;j<=m;j++)\n scanf(\"%d\",&on[i][j]);\n for (i=1;i<=n;i++)\n for (j=1;j<=m;j++)\n scanf(\"%d\",&off[i][j]);\n for (i=1;i<=ntask;i++)\n {\n scanf(\"%d%d\",&tasks[i][0],&tasks[i][1]);\n tasks[i][0]++;\n tasks[i][1]++;\n }\n}\n\nint bfs(int aimx,int aimy,int nowx,int nowy,int time)\n{\n int i,j,k;\n int x,y;\n for (i=0;i<4;i++)\n {\n x=nowx+go[i][0];\n y=nowy+go[i][1];\n if (room[x][y]&&(!step[x][y]))\n {\n step[x][y]=time;\n if ((aimx==x)&&(aimy==y))\n {\n appear[x][y][++appear[x][y][0]]=time;\n return time;\n }\n else\n {\n if (bfs(aimx,aimy,x,y,time+1))\n {\n appear[x][y][++appear[x][y][0]]=time;\n return time;\n }\n }\n }\n }\n return 0;\n}\n\nint main()\n{\n /* freopen(\"input.txt\",\"r\",stdin);\n freopen(\"output.txt\",\"w\",stdout);*/\n int i,j,k,now;\n unsigned long long ans=0;\n indata();\n appear[tasks[1][0]][tasks[1][1]][0]=1;\n appear[tasks[1][0]][tasks[1][1]][1]=1;\n now=1;\n /*for (i=1;i<=ntask;i++)\n printf(\"%d %d\\n\",tasks[i][0],tasks[i][1]);*/\n for (i=2;i<=ntask;i++)\n {\n memset(step,0,sizeof(step));\n step[tasks[i-1][0]][tasks[i-1][1]]=now;\n bfs(tasks[i][0],tasks[i][1],tasks[i-1][0],tasks[i-1][1],now+1);\n now=step[tasks[i][0]][tasks[i][1]];\n }\n /* for (i=1;i<=n;i++)\n for (j=1;j<=m;j++)\n {\n printf(\"%d %d:\",i,j);\n for (k=1;k<=appear[i][j][0];k++)\n printf(\" %d\",appear[i][j][k]);\n putchar('\\n');\n }*/\n for (i=1;i<=n;i++)\n for (j=1;j<=m;j++)\n {\n if (appear[i][j][0])\n ans+=on[i][j]+off[i][j];\n for (k=1;k<appear[i][j][0];k++)\n if ((appear[i][j][k+1]-appear[i][j][k])*keep[i][j]>on[i][j]+off[i][j])\n ans+=on[i][j]+off[i][j];\n else\n ans+=(appear[i][j][k+1]-appear[i][j][k])*keep[i][j];\n }\n printf(\"%llu\\n\",ans);\n /* fclose(stdin);\n fclose(stdout);*/\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 14240, "score_of_the_acc": -0.2065, "final_rank": 2 }, { "submission_id": "aoj_2302_10726636", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\nconst int NMAX = 60, PMAX = 1010;\nconst int dl[] = {1, 0, -1, 0};\nconst int dc[] = {0, 1, 0, -1};\n\nint N, M, Q;\nchar A[NMAX][NMAX];\nint B[NMAX][NMAX];\nint C[NMAX][NMAX];\nint D[NMAX][NMAX];\nint Dist[NMAX][NMAX];\nlist<int> E[NMAX][NMAX];\npair<int, int> from[NMAX][NMAX];\n\nvoid getPath(int startX, int startY, int finishX, int finishY, vector<pair<int, int>> &dest) {\n\tqueue<pair<int, int>> Q;\n\n\tbool witness[NMAX][NMAX] = {};\n\tQ.push({startX, startY});\n\twitness[startX][startY] = 1;\n\tDist[startX][startY] = 0;\n\tbool found = 0;\n\twhile (!Q.empty()) {\n\t\tint currX, currY;\n\t\ttie(currX, currY) = Q.front();\n\t\tQ.pop();\n\t\tfor (int i = 0; i < 4; ++i) {\n\t\t\tint newX = currX + dl[i];\n\t\t\tint newY = currY + dc[i];\n\t\t\tif (newX < 0 || newX >= N || newY < 0 || newY >= M)\n\t\t\t\tcontinue;\n\t\t\tif (A[newX][newY] != '.' || witness[newX][newY])\n\t\t\t\tcontinue;\n\t\t\tfrom[newX][newY] = {currX, currY};\n\t\t\tDist[newX][newY] = Dist[currX][currY] + 1;\n\t\t\twitness[newX][newY] = 1;\n\t\t\tQ.push({newX, newY});\n\t\t\tif (newX == finishX && newY == finishY) {\n\t\t\t\tfound = 1;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n\tassert(found);\n\tint currX, currY;\n\ttie(currX, currY) = from[finishX][finishY];\n\tint newSize = dest.size() + Dist[currX][currY];\n\tassert(newSize < 2500000);\n\tdest.resize(newSize);\n\t--newSize;\n\twhile (currX != startX || currY != startY) {\n\t\tdest[newSize--] = {currX, currY};\n\t\ttie(currX, currY) = from[currX][currY];\n\t}\n}\n\nint main() {\n//\tassert(freopen(\"debug.in\", \"r\", stdin));\n//\tassert(freopen(\"debug.out\", \"w\", stdout));\n\n\tint i, j;\n\n\tscanf(\"%d %d %d\", &N, &M, &Q);\n\tfor (i = 0; i < N; ++i)\n\t\tscanf(\"%s\", A[i]);\n\tfor (i = 0; i < N; ++i)\n\t\tfor (j = 0; j < M; ++j)\n\t\t\tscanf(\"%d\", &B[i][j]);\n\tfor (i = 0; i < N; ++i)\n\t\tfor (j = 0; j < M; ++j)\n\t\t\tscanf(\"%d\", &C[i][j]);\n\tfor (i = 0; i < N; ++i)\n\t\tfor (j = 0; j < M; ++j)\n\t\t\tscanf(\"%d\", &D[i][j]);\n\n\tvector<pair<int, int>> path;\n\tint lastX, lastY, currX, currY;\n\tscanf(\"%d %d\", &lastX, &lastY);\n\twhile (Q-- > 1) {\n\t\tscanf(\"%d %d\", &currX, &currY);\n\t\tassert(lastX != currX || lastY != currY);\n\t\tpath.push_back({lastX, lastY});\n\t\tgetPath(lastX, lastY, currX, currY, path);\n\t\tlastX = currX, lastY = currY;\n\t}\n\tpath.push_back({lastX, lastY});\n\n\tassert(path.size() < 2500000);\n\n\tfor (size_t i = 0; i < path.size(); ++i)\n\t\tE[path[i].first][path[i].second].push_back(i);\n\n\tbool lightOn[NMAX][NMAX] = {};\n\tint answer = 0;\n\tint currAdd = 0;\n\tfor (size_t i = 0; i < path.size(); ++i) {\n\t\tint x = path[i].first, y = path[i].second;\n\t\tanswer += currAdd;\n\t\tif (!lightOn[x][y])\n\t\t\tanswer += C[x][y];\n\t\tE[x][y].pop_front();\n\t\tint front = E[x][y].front();\n\t\tif (E[x][y].size() && (front - i) * B[x][y] < C[x][y] + D[x][y]) {\n\t\t\tif (!lightOn[x][y])\n\t\t\t\tcurrAdd += B[x][y];\n\t\t\tlightOn[x][y] = 1;\n\t\t} else {\n\t\t\tif (lightOn[x][y])\n\t\t\t\tcurrAdd -= B[x][y];\n\t\t\tlightOn[x][y] = 0;\n\t\t\tanswer += D[x][y];\n\t\t}\n\t}\n\n\tcout << answer << '\\n';\n\treturn 0;\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 53252, "score_of_the_acc": -1.3643, "final_rank": 15 }, { "submission_id": "aoj_2302_9696198", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n/**\n * @brief template\n */\n\nint main() {\n int dx[4] = {1,0,-1,0}, dy[4] = {0,1,0,-1};\n int N, M, K;\n cin >> N >> M >> K;\n vector G(N,vector<bool>(M));\n rep(i,0,N) {\n string S;\n cin >> S;\n rep(j,0,M) G[i][j] = (S[j] == '.' ? true : false);\n }\n vector A(N,vector<int>(M)), B(N,vector<int>(M)), C(N,vector<int>(M));\n rep(i,0,N) rep(j,0,M) cin >> A[i][j];\n rep(i,0,N) rep(j,0,M) cin >> B[i][j];\n rep(i,0,N) rep(j,0,M) cin >> C[i][j];\n vector<int> X(K), Y(K);\n rep(i,0,K) cin >> X[i] >> Y[i];\n vector D(N, vector(M, vector<int>()));\n D[X[0]][Y[0]].push_back(0);\n rep(i,0,K-1) {\n vector Dist(N,vector<int>(M,inf));\n Dist[X[i+1]][Y[i+1]] = 0;\n queue<pair<int,int>> Q;\n Q.push({X[i+1],Y[i+1]});\n while(!Q.empty()) {\n auto [CurX, CurY] = Q.front();\n Q.pop();\n rep(j,0,4) {\n int NX = CurX + dx[j], NY = CurY + dy[j];\n if (0 <= NX && NX < N && 0 <= NY && NY < M && G[NX][NY]) {\n if (chmin(Dist[NX][NY],Dist[CurX][CurY]+1)) Q.push({NX,NY});\n }\n }\n }\n int NowX = X[i], NowY = Y[i];\n while(1) {\n if (Dist[NowX][NowY] == 0) break;\n int NextX, NextY;\n rep(j,0,4) {\n int NX = NowX + dx[j], NY = NowY + dy[j];\n if (0 <= NX && NX < N && 0 <= NY && NY < M && G[NX][NY] && Dist[NX][NY] == Dist[NowX][NowY] - 1) {\n NextX = NX, NextY = NY;\n break;\n }\n }\n D[NextX][NextY].push_back(D[NowX][NowY].back()+1);\n NowX = NextX, NowY = NextY;\n }\n }\n int ANS = 0;\n rep(i,0,N) {\n rep(j,0,M) {\n if (D[i][j].empty()) continue;\n ANS += B[i][j] + C[i][j];\n rep(k,0,D[i][j].size()-1) {\n ANS += min(A[i][j]*(D[i][j][k+1]-D[i][j][k]),B[i][j]+C[i][j]);\n }\n }\n }\n cout << ANS << endl;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 8728, "score_of_the_acc": -0.2994, "final_rank": 4 }, { "submission_id": "aoj_2302_8994721", "code_snippet": "#line 2 \"cp-library/src/cp-template.hpp\"\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nusing i32 = int;\nusing u32 = unsigned int;\nusing i64 = long long;\nusing u64 = unsigned long long;\nusing i128 = __int128_t;\ntemplate < class T > bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }\ntemplate < class T > bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }\ntemplate < class T, class U > T ceil (T x, U y) { return (x > 0 ? (x + y - 1) / y : x / y); }\ntemplate < class T, class U > T floor(T x, U y) { return (x > 0 ? x / y : (x - y + 1) / y); }\nint popcnt(i32 x) { return __builtin_popcount(x); }\nint popcnt(u32 x) { return __builtin_popcount(x); }\nint popcnt(i64 x) { return __builtin_popcountll(x); }\nint popcnt(u64 x) { return __builtin_popcountll(x); }\n\n#line 2 \"cp-library/src/utility/rep_itr.hpp\"\ntemplate < class T > struct itr_rep {\n T i, d;\n constexpr itr_rep(const T i) noexcept : i(i), d(1) {}\n constexpr itr_rep(const T i, const T d) noexcept : i(i), d(d) {}\n void operator++() noexcept { i += d; }\n constexpr int operator*() const noexcept { return i; }\n constexpr bool operator!=(const itr_rep x) const noexcept { return d > 0 ? i < x.i : i > x.i; }\n};\n\ntemplate < class T > struct rep {\n const itr_rep< T > s, t;\n constexpr rep(const T t) noexcept : s(0), t(t) {}\n constexpr rep(const T s, const T t) noexcept : s(s), t(t) {}\n constexpr rep(const T s, const T t, const T d) noexcept : s(s, d), t(t, d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n\ntemplate < class T > struct revrep {\n const itr_rep < T > s, t;\n constexpr revrep(const T t) noexcept : s(t - 1, -1), t(-1, -1) {}\n constexpr revrep(const T s, const T t) noexcept : s(t - 1, -1), t(s - 1, -1) {}\n constexpr revrep(const T s, const T t, const T d) noexcept : s(t - 1, -d), t(s - 1, -d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n#line 3 \"cp-library/src/utility/io.hpp\"\n\n/* 128bit integer */\nistream& operator>>(istream& is, i128& x) {\n std::string s; is >> s;\n int pm = (s[0] == '-');\n x = 0;\n for(int i : rep(pm, int(s.size()))) x = x * 10 + (s[i] - '0');\n if(pm) x *= -1;\n return is;\n}\nostream& operator<<(ostream& os, const i128& x) {\n if(x == 0) return os << '0';\n i128 y = x;\n if(y < 0) { os << '-'; y *= -1; }\n std::vector<int> ny;\n while(y > 0) { ny.push_back(y % 10); y /= 10; }\n for(int i : revrep(ny.size())) os << ny[i];\n return os;\n}\n\ntemplate < class S, class T > istream& operator>>(istream& is, std::pair< S, T >& x) { is >> x.first >> x.second; return is; }\ntemplate < class S, class T > ostream& operator<<(ostream& os, const std::pair< S, T >& x) { os << x.first << \" \" << x.second; return os; }\n\nnamespace scanner {\n struct sca {\n template < class T > operator T() {\n T s; std::cin >> s; return s;\n }\n };\n struct vec {\n int n;\n vec(int n) : n(n) {}\n template < class T > operator std::vector< T >() {\n std::vector< T > v(n);\n for(T& x : v) std::cin >> x;\n return v;\n }\n };\n struct mat {\n int h, w;\n mat(int h, int w) : h(h), w(w) {}\n template < class T > operator std::vector< std::vector< T > >() {\n std::vector m(h, std::vector< T >(w));\n for(std::vector< T >& v : m) for(T& x : v) std::cin >> x;\n return m;\n }\n };\n struct speedup {\n speedup() {\n std::cin.tie(0);\n std::ios::sync_with_stdio(0);\n }\n } speedup_instance;\n}\nscanner::sca in() { return scanner::sca(); }\nscanner::vec in(int n) { return scanner::vec(n); }\nscanner::mat in(int h, int w) { return scanner::mat(h, w); }\n\nnamespace printer {\n void precision(int d) { std::cout << std::fixed << std::setprecision(d); }\n void flush() { std::cout.flush(); }\n}\n\ntemplate < class T >\nostream& operator<<(ostream& os, const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) { os << a[i]; if(i != n - 1) os << ' '; }\n return os;\n}\n\nint print() { std::cout << '\\n'; return 0; }\ntemplate < class head, class... tail > int print(head&& h, tail&&... t) {\n std::cout << h; if(sizeof...(tail)) std::cout << ' ';\n return print(std::forward<tail>(t)...);\n}\ntemplate < class T > int print_n(const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) std::cout << a[i] << \"\\n\";\n return 0;\n}\n\n\n#line 2 \"cp-library/src/utility/key_val.hpp\"\n\ntemplate < class K, class V >\nstruct key_val {\n K key; V val;\n key_val() {}\n key_val(K key, V val) : key(key), val(val) {}\n template < std::size_t Index >\n std::tuple_element_t< Index, key_val >& get() {\n if constexpr (Index == 0) return key;\n if constexpr (Index == 1) return val;\n }\n};\n\nnamespace std {\n\ntemplate < class K, class V > struct tuple_size < key_val< K, V > > : integral_constant< size_t, 2 > {};\ntemplate < class K, class V > struct tuple_element < 0, key_val< K, V > > { using type = K; };\ntemplate < class K, class V > struct tuple_element < 1, key_val< K, V > > { using type = V; };\n\n}\n#line 2 \"cp-library/src/utility/vec_op.hpp\"\ntemplate < class T > key_val< int, T > max_of(const vector< T >& a) {\n int i = std::max_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class T > key_val< int, T > min_of(const vector< T >& a) {\n int i = std::min_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class S, class T > S sum_of(const vector< T >& a) {\n S sum = 0;\n for(const T x : a) sum += x;\n return sum;\n}\ntemplate < class S, class T > vector< S > freq_of(const vector< T >& a, T L, T R) {\n vector< S > res(R - L, S(0));\n for(const T x : a) res[x - L] += 1;\n return res;\n}\ntemplate < class S, class T > struct prefix_sum {\n vector< S > s;\n prefix_sum(const vector< T >& a) : s(a) {\n s.insert(s.begin(), S(0));\n for(int i : rep(a.size())) s[i + 1] += s[i];\n }\n // [L, R)\n S sum(int L, int R) { return s[R] - s[L]; }\n};\n#line 3 \"cp-library/src/utility/heap.hpp\"\n\ntemplate < class T > using heap_min = std::priority_queue< T, std::vector< T >, std::greater< T > >;\ntemplate < class T > using heap_max = std::priority_queue< T, std::vector< T >, std::less< T > >;\n\n#line 27 \"cp-library/src/cp-template.hpp\"\n\n#line 1 \"cp-library/src/algorithm/bin_search.hpp\"\ntemplate < class T, class F >\nT bin_search(T ok, T ng, F f) {\n while(abs(ng - ok) > 1) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n\ntemplate < class T, class F >\nT bin_search_real(T ok, T ng, F f, int step = 80) {\n while(step--) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n#line 2 \"cp-library/src/algorithm/argsort.hpp\"\n\ntemplate < class T > std::vector< int > argsort(const std::vector< T > &a) {\n std::vector< int > ids((int)a.size());\n std::iota(ids.begin(), ids.end(), 0);\n std::sort(ids.begin(), ids.end(), [&](int i, int j) {\n return a[i] < a[j] || (a[i] == a[j] && i < j);\n });\n return ids;\n}\n#line 1 \"macro.hpp\"\nnamespace macro {\n\nusing size_type = int;\ntemplate < class container > void sort(container& a) { std::sort(std:: begin(a), std:: end(a)); }\ntemplate < class container > void rsort(container& a) { std::sort(std::rbegin(a), std::rend(a)); }\ntemplate < class container > void reverse(container& a) { std::reverse(std::begin(a), std::end(a)); }\ntemplate < class container > void unique(container& a) {\n std::sort(std::begin(a), std::end(a));\n a.erase(std::unique(std::begin(a), std::end(a)), std::end(a));\n}\ntemplate < class container > container sorted(const container& a) { container b = a; sort(b); return std::move(b); }\ntemplate < class container > container rsorted(const container& a) { container b = a; rsort(b); return std::move(b); }\ntemplate < class container, class compare > void sort(container& a, const compare& cmp) { std::sort(std::begin(a), std::end(a), cmp); }\ntemplate < class container, class compare > container sorted(const container& a, const compare& cmp) { container b = a; sort(b, cmp); return std::move(b); }\ntemplate < class container, class value > size_type lower_bound(const container& a, const value& x) { return std::lower_bound(std::begin(a), std::end(a), x) - std::begin(a); }\ntemplate < class container, class value > size_type upper_bound(const container& a, const value& x) { return std::upper_bound(std::begin(a), std::end(a), x) - std::begin(a); }\n\nconst std::vector<std::pair<size_type, size_type>> dir4 = { {+1, 0}, {-1, 0}, { 0, +1}, { 0, -1} };\nconst std::vector<std::pair<size_type, size_type>> dir8 = { {-1, -1}, {-1, 0}, {-1, +1}, { 0, -1}, { 0, +1}, {+1, -1}, {+1, 0}, {+1, +1} };\n\n#ifdef _DEBUG\n#define debug(x) std::cout << \"[\" << __LINE__ << \"] \" << #x << \": \" << x << std::endl\n#else\n#define debug(x)\n#endif\n\ntemplate < class container > void concat(container& a, const container& b) {\n a.insert(std::end(a), std::begin(b), std::end(b));\n}\nstd::vector<size_type> iota(const size_type n) {\n std::vector<size_type> I(n);\n std::iota(std::begin(I), std::end(I), 0);\n return I;\n}\ntemplate < class container > std::vector<size_type> sort_idx(const container& a) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return a[i] < a[j] or (a[i] == a[j] and i < j); });\n return I;\n}\ntemplate < class container, class compare > std::vector<size_type> sort_idx(const container& a, const compare& cmp) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return cmp(a[i], a[j]) or (a[i] == a[j] and i < j); });\n return std::move(I);\n}\n\nstruct grid {\n using size_type = int;\n size_type H, W;\n grid(const size_type H, const size_type W) : H(H), W(W) {}\n bool contains(const size_type i, const size_type j) {\n return 0 <= i and i < H and 0 <= j and j < W;\n }\n};\n\n} // namespace macro\n\nusing namespace macro;\n#line 3 \"A.cpp\"\n\nint main() {\n int H = in(), W = in(), M = in();\n vector<string> S = in(H);\n vector<vector<int>> c = in(H, W), on = in(H, W), off = in(H, W);\n vector<pair<int,int>> task = in(M);\n\n vector<pair<int,int>> pos;\n pos.push_back(task[0]);\n for(int k : rep(M - 1)) {\n vector dist(H, vector(W, -1));\n {\n auto [si, sj] = task[k + 1];\n dist[si][sj] = 0;\n queue<pair<int,int>> q;\n q.push({si, sj});\n while(not q.empty()) {\n auto [i, j] = q.front(); q.pop();\n for(auto [di, dj] : dir4) {\n int ni = i + di, nj = j + dj;\n if(0 <= ni and ni < H and 0 <= nj and nj < W and S[ni][nj] == '.' and dist[ni][nj] == -1) {\n dist[ni][nj] = dist[i][j] + 1;\n q.push({ni, nj});\n }\n }\n }\n }\n\n auto [i, j] = task[k];\n do {\n for(auto [di, dj] : dir4) {\n int ni = i + di, nj = j + dj;\n if(0 <= ni and ni < H and 0 <= nj and nj < W and S[ni][nj] == '.' and dist[ni][nj] == dist[i][j] - 1) {\n pos.push_back({ni, nj});\n tie(i, j) = make_pair(ni, nj);\n break;\n }\n }\n } while(dist[i][j] != 0);\n }\n\n vector T(H, vector(W, vector<int>()));\n for(int t : rep(int(pos.size()))) {\n auto [i, j] = pos[t];\n T[i][j].push_back(t);\n }\n\n int ans = 0;\n for(int i : rep(H)) for(int j : rep(W)) {\n vector<int> & ts = T[i][j];\n const int n = ts.size();\n bool is_on = false;\n int cost = 0;\n for(int k : rep(n)) {\n if(not is_on) {\n cost += on[i][j];\n is_on = true;\n }\n if(k + 1 < n and (ts[k + 1] - ts[k]) * c[i][j] <= off[i][j] + on[i][j]) {\n cost += (ts[k + 1] - ts[k]) * c[i][j];\n } else {\n cost += off[i][j];\n is_on = false;\n }\n }\n ans += cost;\n }\n print(ans);\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 21460, "score_of_the_acc": -0.5467, "final_rank": 9 }, { "submission_id": "aoj_2302_6761315", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#ifdef _RUTHEN\n#include \"debug.hpp\"\n#else\n#define show(...) true\n#endif\n\nusing ll = long long;\n#define rep(i, n) for (int i = 0; i < (n); i++)\ntemplate <class T> using V = vector<T>;\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(0);\n int H, W, M;\n cin >> H >> W >> M;\n V<string> S(H);\n rep(i, H) cin >> S[i];\n V<V<int>> A(H, V<int>(W)), B(H, V<int>(W)), C(H, V<int>(W));\n rep(i, H) rep(j, W) cin >> A[i][j];\n rep(i, H) rep(j, W) cin >> B[i][j];\n rep(i, H) rep(j, W) cin >> C[i][j];\n V<int> X(M), Y(M);\n rep(i, M) cin >> X[i] >> Y[i];\n V<int> P;\n const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}, INF = 1 << 30;\n rep(i, M - 1) {\n // calculate path\n V<V<int>> d(H, V<int>(W, INF)), pre(H, V<int>(W, -1));\n d[X[i]][Y[i]] = 0;\n queue<int> que;\n que.push(X[i] * W + Y[i]);\n while (!que.empty()) {\n int val = que.front();\n que.pop();\n int cx = val / W, cy = val % W;\n if (cx == X[i + 1] and cy == Y[i + 1]) break;\n rep(k, 4) {\n int nx = cx + dx[k], ny = cy + dy[k];\n if (!(0 <= nx and nx < H and 0 <= ny and ny < W)) continue;\n if (S[nx][ny] == '#') continue;\n if (d[nx][ny] != INF) continue;\n d[nx][ny] = d[cx][cy] + 1;\n pre[nx][ny] = cx * W + cy;\n que.push(nx * W + ny);\n }\n }\n V<int> PP;\n int cx = X[i + 1], cy = Y[i + 1];\n while (cx != X[i] or cy != Y[i]) {\n PP.push_back(pre[cx][cy]);\n int nx = pre[cx][cy] / W, ny = pre[cx][cy] % W;\n cx = nx, cy = ny;\n }\n reverse(PP.begin(), PP.end());\n for (auto p : PP) P.push_back(p);\n }\n P.push_back(X[M - 1] * W + Y[M - 1]);\n show(P);\n V<V<int>> lt(H, V<int>(W, -1));\n int N = P.size();\n ll ans = 0;\n for (int i = 0; i < N; i++) {\n int cx = P[i] / W, cy = P[i] % W;\n if (lt[cx][cy] != -1) {\n ans += min(A[cx][cy] * (i - lt[cx][cy]), B[cx][cy] + C[cx][cy]);\n } else {\n ans += B[cx][cy] + C[cx][cy];\n }\n lt[cx][cy] = i;\n }\n cout << ans << '\\n';\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 13308, "score_of_the_acc": -0.3884, "final_rank": 6 }, { "submission_id": "aoj_2302_6688386", "code_snippet": "#include <algorithm>\n#include <bitset>\n#include <cassert>\n#include <chrono>\n#include <climits>\n#include <cmath>\n#include <cstddef>\n#include <cstdint>\n#include <cstdlib>\n#include <functional>\n#include <iomanip>\n#include <iostream>\n#include <limits>\n#include <map>\n#include <memory>\n#include <numeric>\n#include <optional>\n#include <queue>\n#include <random>\n#include <set>\n#include <stack>\n#include <string>\n#include <tuple>\n#include <type_traits>\n#include <unordered_map>\n#include <unordered_set>\n#include <utility>\n#include <vector>\n\n/* macro */\n\n#define rep(i, a, n) for (int i = (int)(a); i < (int)(n); i++)\n#define rrep(i, a, n) for (int i = ((int)(n - 1)); i >= (int)(a); i--)\n#define Rep(i, a, n) for (i64 i = (i64)(a); i < (i64)(n); i++)\n#define RRep(i, a, n) for (i64 i = ((i64)(n - i64(1))); i >= (i64)(a); i--)\n#define all(v) (v).begin(), (v).end()\n#define rall(v) (v).rbegin(), (v).rend()\n#define Bit(n) (1LL << (n))\n\n/* macro end */\n\n/* template */\n\nnamespace ebi {\n\n#ifdef LOCAL\n#define debug(...) \\\n std::cerr << \"LINE: \" << __LINE__ << \" [\" << #__VA_ARGS__ << \"]:\", \\\n debug_out(__VA_ARGS__)\n#else\n#define debug(...)\n#endif\n\nvoid debug_out() { std::cerr << std::endl; }\n\ntemplate <typename Head, typename... Tail>\nvoid debug_out(Head h, Tail... t) {\n std::cerr << \" \" << h;\n if (sizeof...(t) > 0) std::cout << \" :\";\n debug_out(t...);\n}\n\ntemplate <typename T1, typename T2>\nstd::ostream &operator<<(std::ostream &os, const std::pair<T1, T2> &pa) {\n return os << pa.first << \" \" << pa.second;\n}\n\ntemplate <typename T1, typename T2>\nstd::istream &operator>>(std::istream &os, std::pair<T1, T2> &pa) {\n return os >> pa.first >> pa.second;\n}\n\ntemplate <typename T>\nstd::ostream &operator<<(std::ostream &os, const std::vector<T> &vec) {\n for (std::size_t i = 0; i < vec.size(); i++)\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \" \");\n return os;\n}\n\ntemplate <typename T>\nstd::istream &operator>>(std::istream &os, std::vector<T> &vec) {\n for (T &e : vec) std::cin >> e;\n return os;\n}\n\ntemplate <typename T>\nstd::ostream &operator<<(std::ostream &os, const std::optional<T> &opt) {\n if (opt) {\n os << opt.value();\n } else {\n os << \"invalid value\";\n }\n return os;\n}\n\nusing std::size_t;\nusing i32 = std::int32_t;\nusing u32 = std::uint32_t;\nusing i64 = std::int64_t;\nusing u64 = std::uint64_t;\n\ntemplate <class T, T init>\nauto make_vector(int n) {\n return std::vector<T>(n, init);\n}\n\ntemplate <class T, T init, typename Head, typename... Tail>\nauto make_vector(Head n, Tail... ts) {\n return std::vector(n, make_vector<T, init>(ts...));\n}\n\ntemplate <class T>\ninline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return true;\n }\n return false;\n}\n\ntemplate <class T>\ninline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return true;\n }\n return false;\n}\n\ntemplate <class T>\nT pow(T x, i64 n) {\n T res = 1;\n while (n > 0) {\n if (n & 1) res = res * x;\n x = x * x;\n n >>= 1;\n }\n return res;\n}\n\ntemplate <class T>\nT mod_pow(T x, i64 n, i64 mod) {\n T res = 1;\n while (n > 0) {\n if (n & 1) res = (res * x) % mod;\n x = (x * x) % mod;\n n >>= 1;\n }\n return res;\n}\n\ntemplate <class T>\nT scan() {\n T val;\n std::cin >> val;\n return val;\n}\n\ntemplate <class T>\nstruct Edge {\n int to;\n T cost;\n Edge(int _to, T _cost = 1) : to(_to), cost(_cost) {}\n};\n\ntemplate <class T>\nstruct Graph : std::vector<std::vector<Edge<T>>> {\n using std::vector<std::vector<Edge<T>>>::vector;\n void add_edge(int u, int v, T w, bool directed = false) {\n (*this)[u].emplace_back(v, w);\n if (directed) return;\n (*this)[v].emplace_back(u, w);\n }\n};\n\nstruct graph : std::vector<std::vector<int>> {\n using std::vector<std::vector<int>>::vector;\n void add_edge(int u, int v, bool directed = false) {\n (*this)[u].emplace_back(v);\n if (directed) return;\n (*this)[v].emplace_back(u);\n }\n};\n\nconstexpr i64 LNF = std::numeric_limits<i64>::max() / 4;\n\nconstexpr int INF = std::numeric_limits<int>::max() / 2;\n\nconst std::vector<int> dy = {1, 0, -1, 0, 1, 1, -1, -1};\nconst std::vector<int> dx = {0, 1, 0, -1, 1, -1, 1, -1};\n\n} // namespace ebi\n\nnamespace ebi {\n\nvoid main_() {\n int r, c, m;\n std::cin >> r >> c >> m;\n std::vector<std::string> office(r);\n std::cin >> office;\n std::vector cost(r, std::vector<int>(c)), on(r, std::vector<int>(c)),\n off(r, std::vector<int>(c));\n std::cin >> cost >> on >> off;\n std::vector<std::pair<int, int>> tasks(m);\n std::cin >> tasks;\n auto calc_path = [&](int sy, int sx, int gy, int gx) -> std::vector<std::pair<int,int>> {\n std::queue<std::pair<int, int>> que;\n std::vector dist(r, std::vector<int>(c, INF));\n std::vector prev(r, std::vector<int>(c, -1));\n dist[sy][sx] = 0;\n que.push({sy, sx});\n while (!que.empty()) {\n auto [y, x] = que.front();\n que.pop();\n rep(i, 0, 4) {\n int ny = dy[i] + y;\n int nx = dx[i] + x;\n if (0 <= ny && ny < r && 0 <= nx && nx < c &&\n office[ny][nx] == '.' && dist[ny][nx] == INF) {\n dist[ny][nx] = dist[y][x] + 1;\n prev[ny][nx] = i;\n que.push({ny, nx});\n }\n }\n }\n std::vector<std::pair<int,int>> path;\n int y = gy, x = gx;\n while(prev[y][x] >= 0) {\n path.emplace_back(y, x);\n int idx = prev[y][x];\n int ny = y - dy[idx];\n int nx = x - dx[idx];\n assert(0 <= ny && ny < r && 0 <= nx && nx < c && office[ny][nx] == '.');\n y = ny;\n x = nx;\n }\n std::reverse(all(path));\n return path;\n };\n std::map<std::pair<int, int>, int> map;\n int ans = 0;\n std::vector<std::pair<int,int>> path;\n path.emplace_back(tasks[0]);\n rep(i,1,m) {\n auto [sy, sx] = tasks[i-1];\n auto [gy, gx] = tasks[i];\n auto p = calc_path(sy, sx, gy, gx);\n path.insert(path.end(), p.begin(), p.end());\n }\n int n = path.size();\n rep(i,0,n) {\n auto [y, x] = path[i];\n if(map.find({y, x}) == map.end()) {\n ans += on[y][x] + off[y][x];\n }\n else {\n int d = i - map[{y, x}];\n ans += std::min(d * cost[y][x], off[y][x] + on[y][x]);\n }\n map[{y, x}] = i;\n }\n std::cout << ans << '\\n';\n}\n\n} // namespace ebi\n\nint main() {\n std::cout << std::fixed << std::setprecision(15);\n std::cin.tie(nullptr);\n std::ios::sync_with_stdio(false);\n int t = 1;\n // std::cin >> t;\n while (t--) {\n ebi::main_();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 160, "memory_kb": 15244, "score_of_the_acc": -1.226, "final_rank": 10 }, { "submission_id": "aoj_2302_6647067", "code_snippet": "/**\n * author: otera\n**/\n#include<bits/stdc++.h>\nusing namespace std;\n\n#define int long long\n\nusing ll = long long;\nusing ld = long double;\nusing ull = unsigned long long;\nusing int128_t = __int128_t;\n#define repa(i, n) for(int i = 0; i < n; ++ i)\n#define repb(i, a, b) for(int i = a; i < b; ++ i)\n#define repc(i, a, b, c) for(int i = a; i < b; i += c)\n#define overload4(a, b, c, d, e, ...) e\n#define overload3(a, b, c, d, ...) d\n#define rep(...) overload4(__VA_ARGS__, repc, repb, repa)(__VA_ARGS__)\n#define rep1a(i, n) for(int i = 0; i <= n; ++ i)\n#define rep1b(i, a, b) for(int i = a; i <= b; ++ i)\n#define rep1c(i, a, b, c) for(int i = a; i <= b; i += c)\n#define rep1(...) overload4(__VA_ARGS__, rep1c, rep1b, rep1a)(__VA_ARGS__)\n#define rev_repa(i, n) for(int i=n-1;i>=0;i--)\n#define rev_repb(i, a, b) assert(a > b);for(int i=a;i>b;i--)\n#define rev_rep(...) overload3(__VA_ARGS__, rev_repb, rev_repa)(__VA_ARGS__)\n#define rev_rep1a(i, n) for(int i=n;i>=1;i--)\n#define rev_rep1b(i, a, b) assert(a >= b);for(int i=a;i>=b;i--)\n#define rev_rep1(...) overload3(__VA_ARGS__, rev_rep1b, rev_rep1a)(__VA_ARGS__)\ntypedef pair<int, int> P;\ntypedef pair<ll, ll> LP;\n#define pb push_back\n#define pf push_front\n#define ppb pop_back\n#define ppf pop_front\n#define eb emplace_back\n#define fr first\n#define sc second\n#define all(c) c.begin(),c.end()\n#define rall(c) c.rbegin(), c.rend()\n#define lb(c, x) distance((c).begin(), lower_bound(all(c), (x)))\n#define ub(c, x) distance((c).begin(), upper_bound(all(c), (x)))\n#define Sort(a) sort(all(a))\n#define Rev(a) reverse(all(a))\n#define Uniq(a) sort(all(a));a.erase(unique(all(a)),end(a))\n#define si(c) (int)(c).size()\ninline ll popcnt(ull a){ return __builtin_popcountll(a); }\n#define kth_bit(x, k) ((x>>k)&1)\n#define unless(A) if(!(A))\nll intpow(ll a, ll b){ ll ans = 1; while(b){ if(b & 1) ans *= a; a *= a; b /= 2; } return ans; }\nll intpow(ll a, ll b, ll m) {ll ans = 1; while(b){ if(b & 1) (ans *= a) %= m; (a *= a) %= m; b /= 2; } return ans; }\ntemplate<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }\ntemplate<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }\n#define INT(...) int __VA_ARGS__;in(__VA_ARGS__)\n#define LL(...) ll __VA_ARGS__;in(__VA_ARGS__)\n#define ULL(...) ull __VA_ARGS__;in(__VA_ARGS__)\n#define STR(...) string __VA_ARGS__;in(__VA_ARGS__)\n#define CHR(...) char __VA_ARGS__;in(__VA_ARGS__)\n#define DBL(...) double __VA_ARGS__;in(__VA_ARGS__)\n#define LD(...) ld __VA_ARGS__;in(__VA_ARGS__)\n#define vec(type,name,...) vector<type>name(__VA_ARGS__)\n#define VEC(type,name,size) vector<type>name(size);in(name)\n#define vv(type,name,h,...) vector<vector<type>>name(h,vector<type>(__VA_ARGS__))\n#define VV(type,name,h,w) vector<vector<type>>name(h,vector<type>(w));in(name)\n#define vvv(type,name,h,w,...) vector<vector<vector<type>>>name(h,vector<vector<type>>(w,vector<type>(__VA_ARGS__)))\ntemplate <class T> using vc = vector<T>;\ntemplate <class T> using vvc = vector<vc<T>>;\ntemplate <class T> using vvvc = vector<vvc<T>>;\ntemplate <class T> using vvvvc = vector<vvvc<T>>;\ntemplate <class T> using pq = priority_queue<T>;\ntemplate <class T> using pqg = priority_queue<T, vector<T>, greater<T>>;\ntemplate <class T, class U> using umap = unordered_map<T, U>;\ntemplate<class T> void scan(T& a){ cin >> a; }\ntemplate<class T> void scan(vector<T>& a){ for(auto&& i : a) scan(i); }\nvoid in(){}\ntemplate <class Head, class... Tail> void in(Head& head, Tail&... tail){ scan(head); in(tail...); }\nvoid print(){ cout << ' '; }\ntemplate<class T> void print(const T& a){ cout << a; }\ntemplate<class T> void print(const vector<T>& a){ if(a.empty()) return; print(a[0]); for(auto i = a.begin(); ++i != a.end(); ){ cout << ' '; print(*i); } }\nint out(){ cout << '\\n'; return 0; }\ntemplate<class T> int out(const T& t){ print(t); cout << '\\n'; return 0; }\ntemplate<class Head, class... Tail> int out(const Head& head, const Tail&... tail){ print(head); cout << ' '; out(tail...); return 0; }\n#define CHOOSE(a) CHOOSE2 a\n#define CHOOSE2(a0,a1,a2,a3,a4,x,...) x\n#define debug_1(x1) cout<<#x1<<\": \"<<x1<<endl\n#define debug_2(x1,x2) cout<<#x1<<\": \"<<x1<<\", \"#x2<<\": \"<<x2<<endl\n#define debug_3(x1,x2,x3) cout<<#x1<<\": \"<<x1<<\", \"#x2<<\": \"<<x2<<\", \"#x3<<\": \"<<x3<<endl\n#define debug_4(x1,x2,x3,x4) cout<<#x1<<\": \"<<x1<<\", \"#x2<<\": \"<<x2<<\", \"#x3<<\": \"<<x3<<\", \"#x4<<\": \"<<x4<<endl\n#define debug_5(x1,x2,x3,x4,x5) cout<<#x1<<\": \"<<x1<<\", \"#x2<<\": \"<<x2<<\", \"#x3<<\": \"<<x3<<\", \"#x4<<\": \"<<x4<<\", \"#x5<<\": \"<<x5<<endl\n#ifdef DEBUG\n#define debug(...) CHOOSE((__VA_ARGS__,debug_5,debug_4,debug_3,debug_2,debug_1,~))(__VA_ARGS__)\n#define dump(...) { print(#__VA_ARGS__); print(\":\"); out(__VA_ARGS__); }\n#else\n#define debug(...)\n#define dump(...)\n#endif\n\nstruct io_setup {\n io_setup(int precision = 20) {\n ios::sync_with_stdio(false);\n cin.tie(0);\n cout << fixed << setprecision(precision);\n }\n} io_setup_ {};\n\nconst int inf = 1e9 + 7;\n\nconst int dx[4] = {0, 1, 0, -1};\nconst int dy[4] = {1, 0, -1, 0};\n\nvoid solve() {\n INT(r, c, m);\n VEC(string, f, r);\n VV(int, c0, r, c);\n VV(int, c1, r, c);\n VV(int, c2, r, c);\n vc<int> x(m), y(m);\n rep(i, m) {\n in(x[i], y[i]);\n }\n int cur = 0;\n vvvc<int> g(r, vvc<int>(c, vc<int>()));\n auto mv = [&](int x1, int y1, int x2, int y2) -> void {\n debug(x1, y1, x2, y2);\n queue<P> que;\n vvc<int> di(r, vc<int>(c, inf));\n vvc<P> pre(r, vc<P>(c, P{-1, -1}));\n di[x1][y1] = cur;\n que.push(P{x1, y1});\n while(que.size()) {\n auto [x, y] = que.front(); que.pop();\n rep(_, 4) {\n int nx = x + dx[_], ny = y + dy[_];\n if(0 <= nx && nx < r && 0 <= ny && ny < c && f[nx][ny] == '.') {\n if(chmin(di[nx][ny], di[x][y] + 1)) {\n que.push(P{nx, ny});\n pre[nx][ny] = P{x, y};\n }\n }\n }\n }\n int x = x2, y = y2;\n while(true) {\n debug(x, y);\n g[x][y].eb(di[x][y]);\n auto [nx, ny] = pre[x][y];\n debug(nx, ny);\n if(nx == -1) break;\n x = nx; y = ny;\n }\n cur = di[x2][y2];\n };\n rep(i, m - 1) {\n mv(x[i], y[i], x[i + 1], y[i + 1]);\n }\n int ans = 0;\n rep(x, r) {\n rep(y, c) {\n auto v = g[x][y];\n int sz = si(v);\n if(sz == 0) continue;\n debug(x, y);\n dump(v);\n vvc<int> dp(sz, vc<int>(2, inf));\n dp[0][0] = c1[x][y] + c2[x][y];\n dp[0][1] = c1[x][y];\n for(int i = 1; i < sz; ++ i) {\n int ti = v[i] - v[i - 1];\n int val = min(dp[i - 1][0] + c1[x][y], dp[i - 1][1] + c0[x][y] * ti);\n chmin(dp[i][0], val + c2[x][y]);\n chmin(dp[i][1], val);\n }\n ans += dp[sz - 1][0];\n }\n }\n out(ans);\n}\n\nsigned main() {\n int testcase = 1;\n // in(testcase);\n while(testcase--) solve();\n return 0;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 13720, "score_of_the_acc": -0.5297, "final_rank": 8 }, { "submission_id": "aoj_2302_6365478", "code_snippet": "#line 1 \"library/me/template.cpp\"\n// This is a dummy line\n#ifndef ONLINE_JUDGE\n#define _GLIBCXX_DEBUG\n#endif\n\n#include <bits/stdc++.h>\nusing namespace std;\n\n#define REP(i, n) for(int i = 0; i < (int)(n); i++)\n#define FOR(i, a, b) for(ll i = a; i < (ll)(b); i++)\n#define ALL(a) (a).begin(),(a).end()\n#define END(...) { print(__VA_ARGS__); return; }\n\n#ifdef ONLINE_JUDGE\n#define DBG(...) {}\n#else\n#define DBG(a) { cerr << #a << \": \"; dbg(a); }\n#endif\n\nusing VI = vector<int>;\nusing VVI = vector<VI>;\nusing VVVI = vector<VVI>;\nusing ll = long long;\nusing VL = vector<ll>;\nusing VVL = vector<VL>;\nusing VVVL = vector<VVL>;\nusing VD = vector<double>;\nusing VVD = vector<VD>;\nusing VVVD = vector<VVD>;\nusing VS = vector<string>;\nusing VVS = vector<VS>;\nusing VVVS = vector<VVS>;\nusing VC = vector<char>;\nusing VVC = vector<VC>;\nusing VVVC = vector<VVC>;\nusing P = pair<int, int>;\nusing VP = vector<P>;\nusing VVP = vector<VP>;\nusing VVVP = vector<VVP>;\nusing LP = pair<ll, ll>;\nusing VLP = vector<LP>;\nusing VVLP = vector<VLP>;\nusing VVVLP = vector<VVLP>;\n\ntemplate <typename T>\nusing PQ = priority_queue<T>;\ntemplate <typename T>\nusing GPQ = priority_queue<T, vector<T>, greater<T>>;\n\nconstexpr int INF = 1001001001;\nconstexpr ll LINF = 1001001001001001001ll;\nconstexpr int DX[] = {1, 0, -1, 0};\nconstexpr int DY[] = {0, 1, 0, -1};\n\n#ifdef aclmodint\n\nusing MI7 = modint1000000007;\nusing V7 = vector<MI7>;\nusing VV7 = vector<V7>;\nusing VVV7 = vector<VV7>;\nusing MI3 = modint998244353;\nusing V3 = vector<MI3>;\nusing VV3 = vector<V3>;\nusing VVV3 = vector<VV3>;\n\nostream &operator<<(ostream &os, const modint &x) {\n os << x.val();\n return os;\n}\n\nostream &operator<<(ostream &os, const MI3 &x) {\n os << x.val();\n return os;\n}\n\nostream &operator<<(ostream &os, const MI7 &x) {\n os << x.val();\n return os;\n}\n\nistream &operator>>(istream &is, modint &x) {\n int y; is >> y;\n x = y;\n return is;\n}\n\nistream &operator>>(istream &is, MI3 &x) {\n int y; is >> y;\n x = y;\n return is;\n}\n\nistream &operator>>(istream &is, MI7 &x) {\n int y; is >> y;\n x = y;\n return is;\n}\n\n#endif\n\nvoid print() { cout << '\\n'; }\ntemplate<typename T>\nvoid print(const T &t) { cout << t << '\\n'; }\ntemplate<typename Head, typename... Tail>\nvoid print(const Head &head, const Tail &... tail) {\n cout << head << ' ';\n print(tail...);\n}\n\n#ifdef ONLINE_JUDGE\ntemplate<typename... Args>\nvoid dbg(const Args &... args) {}\n#else\nvoid dbg() { cerr << '\\n'; }\ntemplate<typename T>\nvoid dbg(const T &t) { cerr << t << '\\n'; }\ntemplate<typename Head, typename... Tail>\nvoid dbg(const Head &head, const Tail &... tail) {\n cerr << head << ' ';\n dbg(tail...);\n}\n#endif\n\ntemplate< typename T1, typename T2 >\nostream &operator<<(ostream &os, const pair< T1, T2 >& p) {\n os << p.first << \" \" << p.second;\n return os;\n}\n\ntemplate< typename T1, typename T2 >\nistream &operator>>(istream &is, pair< T1, T2 > &p) {\n is >> p.first >> p.second;\n return is;\n}\n\ntemplate< typename T >\nostream &operator<<(ostream &os, const vector< T > &v) {\n for(int i = 0; i < (int) v.size(); i++) {\n os << v[i] << (i + 1 != (int) v.size() ? \" \" : \"\");\n }\n return os;\n}\n\ntemplate< typename T >\nistream &operator>>(istream &is, vector< T > &v) {\n for(T &in : v) is >> in;\n return is;\n}\n\ntemplate< typename T1, typename T2 >\ninline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }\n\ntemplate< typename T1, typename T2 >\ninline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }\n\ntemplate <typename T>\npair<VI, vector<T>> compress(const vector<T> &a) {\n int n = a.size();\n vector<T> x;\n REP(i, n) x.push_back(a[i]);\n sort(ALL(x)); x.erase(unique(ALL(x)), x.end());\n VI res(n);\n REP(i, n) res[i] = lower_bound(ALL(x), a[i]) - x.begin();\n return make_pair(res, x);\n}\n\ntemplate <typename T>\npair<vector<T>, vector<T>> factorial(int n) {\n vector<T> res(n+1), rev(n+1);\n res[0] = 1;\n REP(i, n) res[i+1] = res[i] * (i+1);\n rev[n] = 1 / res[n];\n for(int i = n; i > 0; i--) {\n rev[i-1] = rev[i] * i;\n }\n return make_pair(res, rev);\n}\n\n#ifdef aclsegtree\n\ntemplate<typename S>\nstruct value_size { S value; int size; };\n\ntemplate<typename S>\nS min_op(S l, S r) { return min(l, r); };\ntemplate<typename S>\nS max_op(S l, S r) { return max(l, r); };\ntemplate<typename S>\nS sum_op(S l, S r) { return l + r; };\ntemplate<typename S>\nvalue_size<S> sum_op_size(value_size<S> l, value_size<S> r) {\n return {l.value + r.value, l.size + r.size};\n};\n\ntemplate<typename S>\nS min_e() { return numeric_limits<S>::max(); };\ntemplate<typename S>\nS max_e() { return numeric_limits<S>::min(); };\ntemplate<typename S>\nS sum_e() { return 0; }\ntemplate<typename S>\nvalue_size<S> sum_e_size() { return {0, 0}; }\ntemplate<typename S>\nvalue_size<S> min_e_size() { return {numeric_limits<S>::max(), 0}; }\ntemplate<typename S>\nvalue_size<S> max_e_size() { return {numeric_limits<S>::min(), 0}; }\n\ntemplate<typename S, typename F>\nS chmin_mapping(F f, S x) { return min(x, f); }\ntemplate<typename S, typename F>\nS chmax_mapping(F f, S x) { return max(x, f); }\ntemplate<typename S, typename F>\nS add_mapping(F f, S x) { return x + f; }\ntemplate<typename S, typename F>\nvalue_size<S> add_mapping_size(F f, value_size<S> x) {\n return {x.value + x.size * f, x.size};\n}\ntemplate <typename S,typename F, F ID>\nS change_mapping(F f, S x) { return (f == ID? x : f); }\ntemplate <typename S,typename F, F ID>\nvalue_size<S> change_mapping_size(F f, value_size<S> x) { \n value_size<S> ret = {f * x.size, x.size};\n return (f == ID? x : ret); \n}\ntemplate <typename S, typename F1, typename F2>\nS linear_mapping(pair<F1, F2> f, S x) { return x * f.first + f.second; }\ntemplate <typename S, typename F1, typename F2>\nvalue_size<S> linear_mapping_size(pair<F1, F2> f, value_size<S> x) { return {x.value * f.first + x.size * f.second, x.size}; }\n\ntemplate<typename F>\nF chmin_composition(F f, F g) { return min(f, g); }\ntemplate<typename F>\nF chmax_composition(F f, F g) { return max(f, g); }\ntemplate<typename F>\nF add_composition(F f, F g) { return f + g; }\ntemplate <typename F, F ID>\nF change_composition(F f, F g) { return (f == ID? g : f); }\ntemplate<typename F1, typename F2>\npair<F1, F2> linear_composition(pair<F1, F2> f, pair<F1, F2> g) { return {f.first * g.first, f.first * g.second + f.second}; }\n\ntemplate<typename F>\nF chmin_id() { return numeric_limits<F>::max(); }\ntemplate<typename F>\nF chmax_id() { return numeric_limits<F>::min(); }\ntemplate<typename F>\nF add_id() { return 0; }\ntemplate<typename F, F ID>\nF change_id() { return ID; }\ntemplate<typename F1, typename F2>\npair<F1, F2> linear_id() { return {1, 0}; }\n\ntemplate<typename S>\nusing RSumQ = segtree<S, sum_op<S>, sum_e<S>>;\ntemplate<typename S>\nusing RMaxQ = segtree<S, max_op<S>, max_e<S>>;\ntemplate<typename S>\nusing RMinQ = segtree<S, min_op<S>, min_e<S>>;\n\ntemplate<typename S, typename F>\nusing RAddSumQ = lazy_segtree<value_size<S>, sum_op_size<S>, sum_e_size<S>,\n F, add_mapping_size<S, F>, add_composition<F>, add_id<F>>;\ntemplate<typename S, typename F>\nusing RAddMinQ = lazy_segtree<S, min_op<S>, min_e<S>,\n F, add_mapping<S, F>, add_composition<F>, add_id<F>>;\ntemplate<typename S, typename F>\nusing RAddMaxQ = lazy_segtree<S, max_op<S>, max_e<S>,\n F, add_mapping<S, F>, add_composition<F>, add_id<F>>;\ntemplate<typename S, typename F>\nusing RMinMinQ = lazy_segtree<S, min_op<S>, min_e<S>,\n F, chmin_mapping<S, F>, chmin_composition<F>, chmin_id<F>>;\ntemplate<typename S, typename F>\nusing RMaxMaxQ = lazy_segtree<S, max_op<S>, max_e<S>,\n F, chmax_mapping<S, F>, chmax_composition<F>, chmax_id<F>>;\ntemplate<typename S, typename F, F ID>\nusing RChangeMinQ = lazy_segtree<S, min_op<S>, min_e<S>,\n F, change_mapping<S, F, ID>,\n change_composition<F, ID>,\n change_id<F, ID>>;\ntemplate<typename S, typename F, F ID>\nusing RChangeMaxQ = lazy_segtree<S, max_op<S>, max_e<S>,\n F, change_mapping<S, F, ID>,\n change_composition<F, ID>,\n change_id<F, ID>>;\ntemplate<typename S, typename F, F ID>\nusing RChangeSumQ = lazy_segtree<value_size<S>, sum_op_size<S>, sum_e_size<S>,\n F, change_mapping_size<S, F, ID>,\n change_composition<F, ID>,\n change_id<F, ID>>;\ntemplate<typename S, typename F1, typename F2>\nusing RLinearMinQ = lazy_segtree<S, min_op<S>, min_e<S>,\n pair<F1, F2>, linear_mapping<S, F1, F2>,\n linear_composition<F1, F2>,\n linear_id<F1, F2>>;\ntemplate<typename S, typename F1, typename F2>\nusing RLinearMaxQ = lazy_segtree<S, max_op<S>, max_e<S>,\n pair<F1, F2>, linear_mapping<S, F1, F2>,\n linear_composition<F1, F2>,\n linear_id<F1, F2>>;\ntemplate<typename S, typename F1, typename F2>\nusing RLinearSumQ = lazy_segtree<value_size<S>, sum_op_size<S>, sum_e_size<S>,\n pair<F1, F2>, linear_mapping_size<S, F1, F2>,\n linear_composition<F1, F2>,\n linear_id<F1, F2>>;\n#endif\n#line 2 \"Contests/Dummy/main.cpp\"\n\nbool run() {\n int m, n; cin >> m >> n;\n if(m == 0) return false;\n VS s(n); cin >> s;\n VI three_pow(m+1, 1);\n REP(i, m) three_pow[i+1] = three_pow[i] * 3;\n VI num(n, INF);\n REP(i, three_pow[m]) {\n VI v;\n int cnt;\n REP(j, n) {\n bool ok = true;\n cnt = 0;\n REP(k, m) {\n if(!(i / three_pow[k] % 3)) continue;\n cnt++;\n if(i / three_pow[k] % 3 - 1 != s[j][k] - '0') ok = false;\n }\n if(ok) v.push_back(j);\n }\n if(v.size() == 1) chmin(num[v[0]], cnt);\n }\n int ans = 0;\n REP(i, n) chmax(ans, num[i]);\n print(ans);\n return true;\n}\n\nvoid solve(){\n int r, c, m; cin >> r >> c >> m;\n VS maze(r); cin >> maze;\n VVL power(r, VL(c)), turnon(r, VL(c)), turnoff(r, VL(c)); cin >> power >> turnon >> turnoff;\n int prex, prey; cin >> prex >> prey;\n ll ans = turnon[prex][prey] + turnoff[prex][prey];\n VVL previsit(r, VL(c, -INF));\n previsit[prex][prey] = 0;\n ll t = 0;\n REP(i, m - 1) {\n int nxtx, nxty; cin >> nxtx >> nxty;\n VVL dist(r, VL(c, INF));\n dist[prex][prey] = 0;\n queue<P> que;\n que.emplace(prex, prey);\n while(!que.empty()) {\n auto [x, y] = que.front(); que.pop();\n REP(j, 4) {\n int nx = x + DX[j], ny = y + DY[j];\n if(nx < 0 || nx >= r || ny < 0 || ny >= c) continue;\n if(maze[nx][ny] == '#') continue;\n if(dist[nx][ny] == INF) {\n dist[nx][ny] = dist[x][y] + 1;\n que.emplace(nx, ny);\n }\n }\n }\n int curx = nxtx, cury = nxty;\n while(curx != prex || cury != prey) {\n if(previsit[curx][cury] == -INF) ans += turnon[curx][cury]+turnoff[curx][cury];\n else ans += min(turnon[curx][cury]+turnoff[curx][cury], power[curx][cury]*(t+dist[curx][cury]-previsit[curx][cury]));\n previsit[curx][cury] = t + dist[curx][cury];\n REP(j, 4) {\n int nx = curx + DX[j], ny = cury + DY[j];\n if(nx < 0 || nx >= r || ny < 0 || ny >= c) continue;\n if(maze[nx][ny] == '#') continue;\n if(dist[nx][ny] + 1 == dist[curx][cury]) {\n curx = nx, cury = ny;\n break;\n }\n }\n }\n t += dist[nxtx][nxty];\n prex = nxtx, prey = nxty;\n }\n print(ans);\n}\n\n// generated by oj-template v4.7.2 (https://github.com/online-judge-tools/template-generator)\nint main() {\n // Fasterize input/output script\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << fixed << setprecision(100);\n // scanf/printf user should delete this fasterize input/output script\n\n int t = 1;\n //cin >> t; // comment out if solving multi testcase\n for(int testCase = 1;testCase <= t;++testCase){\n solve();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 3612, "score_of_the_acc": -0.3333, "final_rank": 5 }, { "submission_id": "aoj_2302_6336656", "code_snippet": "#include \"bits/stdc++.h\"\nusing namespace std;\n\n#define REP(a, b) for (long long a = 0; a < b; ++a)\n#define ll long long\n#define ld long double\n#define ALL(x) begin(x), end(x)\n\n#define int long long\nint cost[3][100][100];\nvector<int> cost_cutter[100][100];\nvoid solve()\n{\n int r, c, m;\n cin >> r >> c >> m;\n vector<string> inputs;\n REP(i, r)\n {\n string s;\n cin >> s;\n inputs.push_back(s);\n }\n REP(t, 3)\n {\n REP(i, r)\n {\n REP(q, c)\n {\n cin >> cost[t][i][q];\n }\n }\n }\n pair<int, int> cur;\n cin >> cur.first >> cur.second;\n cost_cutter[cur.first][cur.second].push_back(0);\n int cur_time = 0;\n REP(i, m - 1)\n {\n pair<int, int> target;\n cin >> target.first >> target.second;\n\n vector<vector<int>> dist(r, vector<int>(c, 1e18));\n vector<vector<pair<int, int>>> backs(r, vector<pair<int, int>>(c));\n\n dist[cur.first][cur.second] = 0;\n queue<pair<int, int>> nexts;\n nexts.push(cur);\n while (!nexts.empty())\n {\n pair<int, int> now = nexts.front();\n nexts.pop();\n int dx[4] = {-1, 1, 0, 0};\n REP(i, 4)\n {\n int x = dx[i] + now.first;\n int y = dx[3 - i] + now.second;\n if (x >= 0 and x < r and y >= 0 and y < c)\n {\n if (inputs[x][y] == '#')\n continue;\n if (dist[x][y] == 1e18)\n {\n dist[x][y] = dist[now.first][now.second] + 1;\n nexts.push({x, y});\n backs[x][y] = now;\n }\n }\n }\n }\n cur_time += dist[target.first][target.second];\n\n pair<int, int> now = target;\n while (now != cur)\n {\n cost_cutter[now.first][now.second].push_back(cur_time);\n cur_time--;\n now = backs[now.first][now.second];\n }\n cur_time += dist[target.first][target.second];\n\n cur = target;\n }\n\n int ans = 0;\n REP(i, r)\n {\n REP(q, c)\n {\n cost_cutter[i][q].push_back(1e12);\n REP(j, cost_cutter[i][q].size() - 1)\n {\n ans += cost[1][i][q];\n int next_come = cost_cutter[i][q][j + 1] - cost_cutter[i][q][j];\n next_come *= cost[0][i][q];\n if (j == cost_cutter[i][q].size() - 2 or next_come > cost[1][i][q] + cost[2][i][q])\n {\n ans += cost[2][i][q];\n }\n else\n {\n ans += next_come;\n ans -= cost[1][i][q];\n }\n }\n }\n }\n cout << ans << endl;\n}\n\n#undef int\nint main()\n{\n iostream::sync_with_stdio(false);\n cout << fixed << setprecision(100);\n solve();\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 13904, "score_of_the_acc": -0.4666, "final_rank": 7 } ]
aoj_2306_cpp
Rabbit Party A rabbit Taro decided to hold a party and invite some friends as guests. He has n rabbit friends, and m pairs of rabbits are also friends with each other. Friendliness of each pair is expressed with a positive integer. If two rabbits are not friends, their friendliness is assumed to be 0. When a rabbit is invited to the party, his satisfaction score is defined as the minimal friendliness with any other guests. The satisfaction of the party itself is defined as the sum of satisfaction score for all the guests. To maximize satisfaction scores for the party, who should Taro invite? Write a program to calculate the maximal possible satisfaction score for the party. Input The first line of the input contains two integers, n and m ( 1 \leq n \leq 100 , 0 \leq m \leq 100 ). The rabbits are numbered from 1 to n . Each of the following m lines has three integers, u , v and f . u and v ( 1 \leq u, v \leq n , u \neq v , 1 \leq f \leq 1,000,000 ) stands for the rabbits' number, and f stands for their friendliness. You may assume that the friendliness of a pair of rabbits will be given at most once. Output Output the maximal possible satisfaction score of the party in a line. Sample Input 1 3 3 1 2 3 2 3 1 3 1 2 Output for the Sample Input 1 6 Sample Input 2 2 1 1 2 5 Output for the Sample Input 2 10 Sample Input 3 1 0 Output for the Sample Input 3 0 Sample Input 4 4 5 1 2 4 1 3 3 2 3 7 2 4 5 3 4 6 Output for the Sample Input 4 16
[ { "submission_id": "aoj_2306_10690753", "code_snippet": "#include <cstdio>\n#include <cstring>\n#include <vector>\n#include <algorithm>\nusing namespace std;\n\nint N, M, ST, gans, ans, hg1, ohg1;\nint jiyou[120][120], val[200], oval[300];\nbool used[1000];\n\nint getans()\n{\n\tint i, res = 0, nc = 0;\n\tfor (i = ST; i <= N; ++i)\n\t\tif (used[i])\n\t\t{\n\t\t\t++nc;\n\t\t\tres += val[i];\n\t\t}\n\tif (nc < 2) return 0;\n\treturn res;\n}\n\nvoid dfs(int x);\n\nint main()\n{\n\tint i, j, k, x, y, l;\n\tmemset(jiyou, 0, sizeof(jiyou));\n\tscanf(\"%d%d\", &N, &M);\n\tfor (i = 0; i < M; ++i)\n\t{\n\t\tscanf(\"%d%d%d\", &x, &y, &l);\n\t\tjiyou[x][y] = l;\n\t\tjiyou[y][x] = l;\n\t}\n\tfor (i = 1; i < N; ++i)\n\t{\n\t\tST = i;\n\t\tans = 0;\n\t\tmemset(used, 0, sizeof(used));\n\t\tmemset(val, 127, sizeof(val));\n\t\tused[i] = true;\n\t\tdfs(i+1);\n\t}\n\tprintf(\"%d\\n\", gans);\n\treturn 0;\n}\n\nvoid dfs(int x)\n{\n\tint i, haogandu = 10000000;\n\tif (x > N)\n\t{\n\t\tgans = max(gans, getans());\n\t\treturn;\n\t}\n\tfor (i = ST; i <= N; ++i)\n\t\tif (used[i])\n\t\t\thaogandu = min(haogandu, jiyou[x][i]);\n\tif (haogandu)\n\t{\n\t\tfor (i = ST; i <= N; ++i)\n\t\t\toval[i] = val[i];\n\t\tval[x] = haogandu;\n\t\tfor (i = ST; i <= N; ++i)\n\t\t\tif (used[i])\n\t\t\t{\n\t\t\t\tval[i] = min(val[i], jiyou[x][i]);\n\t\t\t\tif (i > ST)\n\t\t\t\t\tval[ST] = min(val[ST], jiyou[i][ST]);\n\t\t\t}\n\t\tgans = max(gans, getans());\n\t\tused[x] = true;\n\t\tdfs(x+1);\n\t\tused[x] = false;\n\t\tfor (i = ST; i <= N; ++i)\n\t\t\tval[i] = oval[i];\n\t}\n\tdfs(x+1);\n}", "accuracy": 0.4727272727272727, "time_ms": 20, "memory_kb": 3060, "score_of_the_acc": -0.0052, "final_rank": 20 }, { "submission_id": "aoj_2306_8302655", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\ntemplate<class T>\nbool chmin(T &a, const T &b) {if (a > b) {a = b; return 1;} return 0;}\n\nbool is_end = false;\nmt19937 engine(42);\n\nvoid calc()\n{\n\tint N, M; cin >> N >> M;\n\tvector<bool> has_friends(N, false);\n\tvector<vector<int>> weight(N, vector<int>(N, 0));\n\tfor (int i = 0; i < M; ++i)\n\t{\n\t\tint a, b, f; cin >> a >> b >> f;\n\t\ta--, b--;\n\t\thas_friends[a] = has_friends[b] = true;\n\t\tweight[a][b] = weight[b][a] = f;\n\t}\n\t\n\tvector<int> perm;\n\tfor (int i = 0; i < N; ++i)\n\t{\n\t\tif (has_friends[i]) {perm.emplace_back(i);}\n\t}\n\t\n\tint cnt = 0;\n\tint res = 0;\n\twhile (cnt < 100000)\n\t{\n\t\tcnt++;\n\t\tshuffle(perm.begin(), perm.end(), engine);\n\t\tvector<int> cleak;\n\t\t\n\t\tfor (auto v : perm)\n\t\t{\n\t\t\tbool flg = true;\n\t\t\tfor (auto c : cleak)\n\t\t\t{\n\t\t\t\tflg &= (weight[v][c] > 0);\n\t\t\t}\n\t\t\t\n\t\t\tif (!flg) {continue;}\n\t\t\tcleak.emplace_back(v);\n\t\t\t\n\t\t\tif (cleak.size() == 1) {continue;}\n\t\t\t\n\t\t\tint tmp = 0;\n\t\t\tfor (auto c : cleak)\n\t\t\t{\n\t\t\t\tint mn = 1e9;\n\t\t\t\tfor (auto d : cleak)\n\t\t\t\t{\n\t\t\t\t\tif (c != d) {mn = min(mn, weight[c][d]);}\n\t\t\t\t}\n\t\t\t\ttmp += mn;\n\t\t\t}\n\t\t\tres = max(res, tmp);\n\t\t}\n\t}\n\tcout << res << endl;\n\t\n\treturn;\n}\n\nint main()\n{\n\tcalc();\n\t\n\treturn 0;\n}", "accuracy": 1, "time_ms": 120, "memory_kb": 3472, "score_of_the_acc": -0.0631, "final_rank": 11 }, { "submission_id": "aoj_2306_8020162", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\ntypedef long long ll;\ntypedef __int128_t lll;\ntypedef long double ld;\ntypedef pair<ll, ll> pl;\ntypedef tuple<ll, ll, ll> tt;\ntypedef vector<ll> vl;\ntypedef vector<vl> vvl;\ntypedef vector<vvl> vvvl;\ntypedef vector<double> vd;\ntypedef vector<vd> vvd;\ntypedef vector<vvd> vvvd;\ntypedef vector<bool> vb;\ntypedef vector<vb> vvb;\ntypedef vector<vvb> vvvb;\ntypedef vector<char> vc;\ntypedef vector<vc> vvc;\ntypedef vector<vvc> vvvc;\ntypedef vector<pl> vp;\ntypedef vector<tt> vt;\ntypedef vector<string> vs;\n\nconst ll INF = 1000000010;\nconst ll INFL = INF * INF;\nconst double MYPI = acos(-1);\nconst ld epsilon = 1e-10;\n\n#define ovl(a, b, c, d, e, ...) e\n#define pb push_back\n#define eb emplace_back\n#define MP make_pair\n#define DEBUG(...) DEBUG_(#__VA_ARGS__, __VA_ARGS__)\n#define REP1(i, n) for (int i = 0; i < (n); i++)\n#define REP2(i, l, r) for (int i = (l); i < (r); i++)\n#define REP3(i, l, r, d) for (int i = (l); i < (r); i += (d))\n#define REP(...) ovl(__VA_ARGS__, REP3, REP2, REP1)(__VA_ARGS__)\n#define RREP1(i, n) for (int i = (n)-1; i >= 0; i--)\n#define RREP2(i, l, r) for (int i = (r)-1; i >= (l); i--)\n#define RREP3(i, l, r, d) for (int i = (r)-1; i >= (l); i -= (d))\n#define RREP(...) ovl(__VA_ARGS__, RREP3, RREP2, RREP1)(__VA_ARGS__)\n#define EACH1(e, a) for (auto &e : a)\n#define EACH2(x, y, a) for (auto &[x, y] : a)\n#define EACH3(x, y, z, a) for (auto &[x, y, z] : a)\n#define EACH(...) ovl(__VA_ARGS__, EACH3, EACH2, EACH1)(__VA_ARGS__)\n#define ALL(x) begin(x), end(x)\n#define RALL(x) (x).rbegin(), (x).rend()\n#define sz(x) (ll) x.size()\n#define LB(a, x) (lower_bound(ALL(a), x) - a.begin())\n#define UB(a, x) (upper_bound(ALL(a), x) - a.begin())\n#define FLG(x, i) (((x) >> (i)) & 1)\n#define CNTBIT(x) __builtin_popcountll(x)\n#define TOPBIT(t) (t == 0 ? -1 : 63 - __builtin_clzll(t))\n#define IN(x, a, b) ((a) <= (x) && (x) < (b))\n#define int ll\n\ntemplate <class T, class... Ts>\nvoid OUT(const T &a, const Ts &...b) {\n cout << a;\n (cout << ... << (cout << \" \", b));\n cout << endl;\n}\n\ntemplate <class T>\nvoid DEBUG_(string_view name, const T &a) {\n cout << name << \": \" << a << endl;\n}\n\ntemplate <class T>\nvoid DEBUG_(string_view name, const vector<T> &a) {\n cout << name << \": \";\n REP(i, sz(a)) cout << a[i] << \" \";\n cout << endl;\n}\n\ntemplate <class T, class U>\nbool chmax(T &x, const U &y) {\n return x < y ? x = y, 1 : 0;\n}\ntemplate <class T, class U>\nbool chmin(T &x, const U &y) {\n return x > y ? x = y, 1 : 0;\n}\n\ntemplate <class T>\nusing minheap = priority_queue<T, vector<T>, greater<T>>;\n\ntemplate <class T>\nusing maxheap = priority_queue<T>;\n\nconst vl dy = {1, 0, -1, 0, 1, -1, -1, 1};\nconst vl dx = {0, 1, 0, -1, 1, 1, -1, -1};\n\ntemplate <class T = ll>\nstruct Edge {\n ll from, to;\n T cost;\n ll idx;\n Edge(ll f, ll t, T c = 1, ll i = -1) : from(f), to(t), cost(c), idx(i) {}\n operator int() const { return to; }\n};\n\ntemplate <class T = ll>\nstruct Graph {\n vector<vector<Edge<T>>> g;\n ll es;\n explicit Graph(ll n) : g(n), es(0) {}\n size_t size() const { return sz(g); }\n void add_directed_edge(ll from, ll to, T cost = 1) {\n g[from].eb(from, to, cost, es++);\n }\n void add_edge(ll from, ll to, T cost = 1) {\n g[from].eb(from, to, cost, es);\n g[to].eb(to, from, cost, es++);\n }\n inline vector<Edge<T>> &operator[](const int &k) { return g[k]; }\n inline const vector<Edge<T>> &operator[](const int &k) const {\n return g[k];\n }\n};\n\n//-----------------------------------------------------------------------\n\nvoid solve() {\n ios::sync_with_stdio(false);\n cin.tie(0);\n cout << fixed << setprecision(15);\n\n int N, M;\n cin >> N >> M;\n vvl Fs(N, vl(N, 0));\n set<vl> clique;\n ll ans = 0;\n REP(i, M) {\n int u, v, f;\n cin >> u >> v >> f;\n u--;\n v--;\n Fs[u][v] = f;\n Fs[v][u] = f;\n clique.insert({u, v});\n chmax(ans, 2 * f);\n }\n\n auto calcScore = [&](const vl &clique) -> ll {\n // DEBUG(clique);\n ll ans = 0;\n EACH(c1, clique) {\n ll ans2 = INFL;\n EACH(c2, clique) {\n if (c1 == c2) continue;\n chmin(ans2, Fs[c1][c2]);\n }\n ans += ans2;\n }\n // DEBUG(ans);\n return ans;\n };\n\n while (!clique.empty()) {\n // DEBUG(clique.size());\n set<vl> newClique;\n EACH(c, clique) {\n REP(nv, N) {\n if (find(ALL(c), nv) != c.end()) {\n continue;\n }\n bool ok = true;\n EACH(v, c) {\n if (Fs[v][nv] == 0) {\n ok = false;\n break;\n }\n }\n if (!ok) continue;\n vl newC = c;\n newC.insert(lower_bound(ALL(newC), nv), nv);\n chmax(ans, calcScore(newC));\n newClique.insert(newC);\n }\n }\n swap(clique, newClique);\n }\n\n cout << ans << endl;\n}\n\nsigned main() {\n int T = 1;\n // cin >> T;\n\n while (T--) solve();\n\n // random_test();\n\n return 0;\n}", "accuracy": 1, "time_ms": 1930, "memory_kb": 74600, "score_of_the_acc": -2, "final_rank": 19 }, { "submission_id": "aoj_2306_6793059", "code_snippet": "/**\n * author: otera\n**/\n#include<bits/stdc++.h>\nusing namespace std;\n\n#define int long long\n\nusing ll = long long;\nusing ld = long double;\nusing ull = unsigned long long;\nusing int128_t = __int128_t;\n#define repa(i, n) for(int i = 0; i < n; ++ i)\n#define repb(i, a, b) for(int i = a; i < b; ++ i)\n#define repc(i, a, b, c) for(int i = a; i < b; i += c)\n#define overload4(a, b, c, d, e, ...) e\n#define overload3(a, b, c, d, ...) d\n#define rep(...) overload4(__VA_ARGS__, repc, repb, repa)(__VA_ARGS__)\n#define rep1a(i, n) for(int i = 0; i <= n; ++ i)\n#define rep1b(i, a, b) for(int i = a; i <= b; ++ i)\n#define rep1c(i, a, b, c) for(int i = a; i <= b; i += c)\n#define rep1(...) overload4(__VA_ARGS__, rep1c, rep1b, rep1a)(__VA_ARGS__)\n#define rev_repa(i, n) for(int i=n-1;i>=0;i--)\n#define rev_repb(i, a, b) assert(a > b);for(int i=a;i>b;i--)\n#define rev_rep(...) overload3(__VA_ARGS__, rev_repb, rev_repa)(__VA_ARGS__)\n#define rev_rep1a(i, n) for(int i=n;i>=1;i--)\n#define rev_rep1b(i, a, b) assert(a >= b);for(int i=a;i>=b;i--)\n#define rev_rep1(...) overload3(__VA_ARGS__, rev_rep1b, rev_rep1a)(__VA_ARGS__)\ntypedef pair<int, int> P;\ntypedef pair<ll, ll> LP;\n#define pb push_back\n#define pf push_front\n#define ppb pop_back\n#define ppf pop_front\n#define eb emplace_back\n#define fr first\n#define sc second\n#define all(c) c.begin(),c.end()\n#define rall(c) c.rbegin(), c.rend()\n#define lb(c, x) distance((c).begin(), lower_bound(all(c), (x)))\n#define ub(c, x) distance((c).begin(), upper_bound(all(c), (x)))\n#define Sort(a) sort(all(a))\n#define Rev(a) reverse(all(a))\n#define Uniq(a) sort(all(a));a.erase(unique(all(a)),end(a))\n#define si(c) (int)(c).size()\ninline ll popcnt(ull a){ return __builtin_popcountll(a); }\n#define kth_bit(x, k) ((x>>k)&1)\n#define unless(A) if(!(A))\nll intpow(ll a, ll b){ ll ans = 1; while(b){ if(b & 1) ans *= a; a *= a; b /= 2; } return ans; }\nll intpow(ll a, ll b, ll m) {ll ans = 1; while(b){ if(b & 1) (ans *= a) %= m; (a *= a) %= m; b /= 2; } return ans; }\ntemplate<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }\ntemplate<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }\n#define INT(...) int __VA_ARGS__;in(__VA_ARGS__)\n#define LL(...) ll __VA_ARGS__;in(__VA_ARGS__)\n#define ULL(...) ull __VA_ARGS__;in(__VA_ARGS__)\n#define STR(...) string __VA_ARGS__;in(__VA_ARGS__)\n#define CHR(...) char __VA_ARGS__;in(__VA_ARGS__)\n#define DBL(...) double __VA_ARGS__;in(__VA_ARGS__)\n#define LD(...) ld __VA_ARGS__;in(__VA_ARGS__)\n#define vec(type,name,...) vector<type>name(__VA_ARGS__)\n#define VEC(type,name,size) vector<type>name(size);in(name)\n#define vv(type,name,h,...) vector<vector<type>>name(h,vector<type>(__VA_ARGS__))\n#define VV(type,name,h,w) vector<vector<type>>name(h,vector<type>(w));in(name)\n#define vvv(type,name,h,w,...) vector<vector<vector<type>>>name(h,vector<vector<type>>(w,vector<type>(__VA_ARGS__)))\ntemplate <class T> using vc = vector<T>;\ntemplate <class T> using vvc = vector<vc<T>>;\ntemplate <class T> using vvvc = vector<vvc<T>>;\ntemplate <class T> using vvvvc = vector<vvvc<T>>;\ntemplate <class T> using pq = priority_queue<T>;\ntemplate <class T> using pqg = priority_queue<T, vector<T>, greater<T>>;\ntemplate <class T, class U> using umap = unordered_map<T, U>;\ntemplate<class T> void scan(T& a){ cin >> a; }\ntemplate<class T> void scan(vector<T>& a){ for(auto&& i : a) scan(i); }\nvoid in(){}\ntemplate <class Head, class... Tail> void in(Head& head, Tail&... tail){ scan(head); in(tail...); }\nvoid print(){ cout << ' '; }\ntemplate<class T> void print(const T& a){ cout << a; }\ntemplate<class T> void print(const vector<T>& a){ if(a.empty()) return; print(a[0]); for(auto i = a.begin(); ++i != a.end(); ){ cout << ' '; print(*i); } }\nint out(){ cout << '\\n'; return 0; }\ntemplate<class T> int out(const T& t){ print(t); cout << '\\n'; return 0; }\ntemplate<class Head, class... Tail> int out(const Head& head, const Tail&... tail){ print(head); cout << ' '; out(tail...); return 0; }\n#define CHOOSE(a) CHOOSE2 a\n#define CHOOSE2(a0,a1,a2,a3,a4,x,...) x\n#define debug_1(x1) cout<<#x1<<\": \"<<x1<<endl\n#define debug_2(x1,x2) cout<<#x1<<\": \"<<x1<<\", \"#x2<<\": \"<<x2<<endl\n#define debug_3(x1,x2,x3) cout<<#x1<<\": \"<<x1<<\", \"#x2<<\": \"<<x2<<\", \"#x3<<\": \"<<x3<<endl\n#define debug_4(x1,x2,x3,x4) cout<<#x1<<\": \"<<x1<<\", \"#x2<<\": \"<<x2<<\", \"#x3<<\": \"<<x3<<\", \"#x4<<\": \"<<x4<<endl\n#define debug_5(x1,x2,x3,x4,x5) cout<<#x1<<\": \"<<x1<<\", \"#x2<<\": \"<<x2<<\", \"#x3<<\": \"<<x3<<\", \"#x4<<\": \"<<x4<<\", \"#x5<<\": \"<<x5<<endl\n#ifdef DEBUG\n#define debug(...) CHOOSE((__VA_ARGS__,debug_5,debug_4,debug_3,debug_2,debug_1,~))(__VA_ARGS__)\n#define dump(...) { print(#__VA_ARGS__); print(\":\"); out(__VA_ARGS__); }\n#else\n#define debug(...)\n#define dump(...)\n#endif\n\nstruct io_setup {\n io_setup(int precision = 20) {\n ios::sync_with_stdio(false);\n cin.tie(0);\n cout << fixed << setprecision(precision);\n }\n} io_setup_ {};\n\nconst int inf = 1e9 + 7;\n\nvoid solve() {\n INT(n, m);\n vvc<P> g(n);\n vvc<int> h(n, vc<int>(n, 0));\n rep(i, m) {\n INT(u, v, f); -- u, -- v;\n g[u].eb(v, f);\n g[v].eb(u, f);\n h[u][v] = h[v][u] = f; \n }\n int ans = 0;\n auto dfs = [&](auto&&dfs, vc<int> used, int i) -> void {\n int ret = 0;\n bool ok = 1;\n int cnt = 0;\n for(int x = 0; x < i; ++ x) {\n if(used[x]) {\n ++ cnt;\n int now = inf;\n for(int y = 0; y < i; ++ y) {\n if(y != x && used[y]) chmin(now, h[x][y]);\n }\n if(now == inf) ok = 0;\n ret += now;\n }\n }\n if(cnt >= 2 && !ok) return;\n if(ok) chmax(ans, ret);\n if(i == n) return;\n dfs(dfs, used, i + 1);\n ok = 1;\n if(cnt >= 1) {\n rep(x, i) {\n if(used[x]) {\n if(h[x][i] == 0) ok = 0;\n }\n }\n }\n used[i] = 1;\n if(ok) dfs(dfs, used, i + 1);\n used[i] = 0;\n return;\n };\n vc<int> cur(n, 0);\n dfs(dfs, cur, 0);\n out(ans);\n}\n\nsigned main() {\n int testcase = 1;\n // in(testcase);\n while(testcase--) solve();\n return 0;\n}", "accuracy": 1, "time_ms": 240, "memory_kb": 3644, "score_of_the_acc": -0.128, "final_rank": 12 }, { "submission_id": "aoj_2306_6176777", "code_snippet": "#ifndef HIDDEN_IN_VISUAL_STUDIO // 折りたたみ用\n\n// 警告の抑制\n#define _CRT_SECURE_NO_WARNINGS\n\n// ライブラリの読み込み\n#include <bits/stdc++.h>\nusing namespace std;\n\n// 型名の短縮\nusing ll = long long; // -2^63 ~ 2^63 = 9 * 10^18(int は -2^31 ~ 2^31 = 2 * 10^9)\nusing pii = pair<int, int>;\tusing pll = pair<ll, ll>;\tusing pil = pair<int, ll>;\tusing pli = pair<ll, int>;\nusing vi = vector<int>;\t\tusing vvi = vector<vi>;\t\tusing vvvi = vector<vvi>;\nusing vl = vector<ll>;\t\tusing vvl = vector<vl>;\t\tusing vvvl = vector<vvl>;\nusing vb = vector<bool>;\tusing vvb = vector<vb>;\t\tusing vvvb = vector<vvb>;\nusing vc = vector<char>;\tusing vvc = vector<vc>;\t\tusing vvvc = vector<vvc>;\nusing vd = vector<double>;\tusing vvd = vector<vd>;\t\tusing vvvd = vector<vvd>;\ntemplate <class T> using priority_queue_rev = priority_queue<T, vector<T>, greater<T>>;\nusing Graph = vvi;\n\n// 定数の定義\nconst double PI = 3.14159265359;\nconst double DEG = PI / 180.; // θ [deg] = θ * DEG [rad]\nconst vi dx4 = { 1, 0, -1, 0 }; // 4 近傍(下,右,上,左)\nconst vi dy4 = { 0, 1, 0, -1 };\nconst vi dx8 = { 1, 1, 0, -1, -1, -1, 0, 1 }; // 8 近傍\nconst vi dy8 = { 0, 1, 1, 1, 0, -1, -1, -1 };\nconst int INF = 1001001001; const ll INFL = 2002002002002002002LL;\nconst double EPS = 1e-10; // 許容誤差に応じて調整\n\n// 入出力高速化\nstruct fast_io { fast_io() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); } } fastIOtmp;\n\n// 汎用マクロの定義\n#define all(a) (a).begin(), (a).end()\n#define sz(x) ((int)(x).size())\n#define distance (int)distance\n#define Yes(b) {cout << ((b) ? \"Yes\" : \"No\") << endl;}\n#define rep(i, n) for(int i = 0, i##_len = int(n); i < i##_len; ++i) // 0 から n-1 まで昇順\n#define repi(i, s, t) for(int i = int(s), i##_end = int(t); i <= i##_end; ++i) // s から t まで昇順\n#define repir(i, s, t) for(int i = int(s), i##_end = int(t); i >= i##_end; --i) // s から t まで降順\n#define repe(v, a) for(const auto& v : (a)) // a の全要素(変更不可能)\n#define repea(v, a) for(auto& v : (a)) // a の全要素(変更可能)\n#define repb(set, d) for(int set = 0; set < (1 << int(d)); ++set) // d ビット全探索(昇順)\n#define repp(a) sort(all(a)); for(bool a##_perm = true; a##_perm; a##_perm = next_permutation(all(a))) // a の順列全て(昇順)\n#define repit(it, a) for(auto it = (a).begin(); it != (a).end(); ++it) // イテレータを回す(昇順)\n#define repitr(it, a) for(auto it = (a).rbegin(); it != (a).rend(); ++it) // イテレータを回す(降順)\n#define smod(n, m) ((((n) % (m)) + (m)) % (m)) // 非負mod\n#define uniq(a) {sort(all(a)); (a).erase(unique(all(a)), (a).end());} // 重複除去\n\n// 汎用関数の定義\ntemplate <class T> inline ll pow(T n, int k) { ll v = 1; rep(i, k) v *= n; return v; }\ntemplate <class T> inline bool chmax(T& M, const T& x) { if (M < x) { M = x; return true; } return false; } // 最大値を更新(更新されたら true を返す)\ntemplate <class T> inline bool chmin(T& m, const T& x) { if (m > x) { m = x; return true; } return false; } // 最小値を更新(更新されたら true を返す)\n\n// 演算子オーバーロード\ntemplate <class T, class U> inline istream& operator>> (istream& is, pair<T, U>& p) { is >> p.first >> p.second; return is; }\ntemplate <class T, class U> inline ostream& operator<< (ostream& os, const pair<T, U>& p) { os << \"(\" << p.first << \",\" << p.second << \")\"; return os; }\ntemplate <class T, class U, class V> inline istream& operator>> (istream& is, tuple<T, U, V>& t) { is >> get<0>(t) >> get<1>(t) >> get<2>(t); return is; }\ntemplate <class T, class U, class V> inline ostream& operator<< (ostream& os, const tuple<T, U, V>& t) { os << \"(\" << get<0>(t) << \",\" << get<1>(t) << \",\" << get<2>(t) << \")\"; return os; }\ntemplate <class T, class U, class V, class W> inline istream& operator>> (istream& is, tuple<T, U, V, W>& t) { is >> get<0>(t) >> get<1>(t) >> get<2>(t) >> get<3>(t); return is; }\ntemplate <class T, class U, class V, class W> inline ostream& operator<< (ostream& os, const tuple<T, U, V, W>& t) { os << \"(\" << get<0>(t) << \",\" << get<1>(t) << \",\" << get<2>(t) << \",\" << get<3>(t) << \")\"; return os; }\ntemplate <class T> inline istream& operator>> (istream& is, vector<T>& v) { repea(x, v) is >> x; return is; }\ntemplate <class T> inline ostream& operator<< (ostream& os, const vector<T>& v) { repe(x, v) os << x << \" \"; return os; }\ntemplate <class T> inline ostream& operator<< (ostream& os, const list<T>& v) { repe(x, v) os << x << \" \"; return os; }\ntemplate <class T> inline ostream& operator<< (ostream& os, const set<T>& s) { repe(x, s) os << x << \" \"; return os; }\ntemplate <class T> inline ostream& operator<< (ostream& os, const set<T, greater<T>>& s) { repe(x, s) os << x << \" \"; return os; }\ntemplate <class T> inline ostream& operator<< (ostream& os, const unordered_set<T>& s) { repe(x, s) os << x << \" \"; return os; }\ntemplate <class T, class U> inline ostream& operator<< (ostream& os, const map<T, U>& m) { repe(p, m) os << p << \" \"; return os; }\ntemplate <class T, class U> inline ostream& operator<< (ostream& os, const unordered_map<T, U>& m) { repe(p, m) os << p << \" \"; return os; }\ntemplate <class T> inline ostream& operator<< (ostream& os, stack<T> s) { while (!s.empty()) { os << s.top() << \" \"; s.pop(); } return os; }\ntemplate <class T> inline ostream& operator<< (ostream& os, queue<T> q) { while (!q.empty()) { os << q.front() << \" \"; q.pop(); } return os; }\ntemplate <class T> inline ostream& operator<< (ostream& os, deque<T> q) { while (!q.empty()) { os << q.front() << \" \"; q.pop_front(); } return os; }\ntemplate <class T> inline ostream& operator<< (ostream& os, priority_queue<T> q) { while (!q.empty()) { os << q.top() << \" \"; q.pop(); } return os; }\ntemplate <class T> inline ostream& operator<< (ostream& os, priority_queue_rev<T> q) { while (!q.empty()) { os << q.top() << \" \"; q.pop(); } return os; }\ntemplate <class T> inline vector<T>& operator--(vector<T>& v) { rep(_i_, sz(v)) --v[_i_]; return v; }\n\n// 手元環境(Visual Studio)\n#ifdef _MSC_VER\n#define popcount (int)__popcnt // 全ビット中の 1 の個数\n#define popcountll (int)__popcnt64\ninline int lsb(unsigned int n) { unsigned long i; _BitScanForward(&i, n); return i; } // 最下位ビットの位置(0-indexed)\ninline int lsbll(unsigned long long n) { unsigned long i; _BitScanForward64(&i, n); return i; }\ninline int msb(unsigned int n) { unsigned long i; _BitScanReverse(&i, n); return i; } // 最上位ビットの位置(0-indexed)\ninline int msbll(unsigned long long n) { unsigned long i; _BitScanReverse64(&i, n); return i; }\ntemplate <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }\n#define dump(x) cout << \"\\033[1;36m\" << (x) << \"\\033[0m\" << endl;\n#define dumps(x) cout << \"\\033[1;36m\" << (x) << \"\\033[0m \";\n#define dumpel(a) { int _i_ = -1; cout << \"\\033[1;36m\"; repe(x, a) {cout << ++_i_ << \": \" << x << endl;} cout << \"\\033[0m\"; }\n#define input_from_file(f) ifstream isTMP(f); cin.rdbuf(isTMP.rdbuf());\n#define output_to_file(f) ofstream osTMP(f); cout.rdbuf(osTMP.rdbuf());\n// 提出用(gcc)\n#else\n#define popcount (int)__builtin_popcount\n#define popcountll (int)__builtin_popcountll\n#define lsb __builtin_ctz\n#define lsbll __builtin_ctzll\n#define msb(n) (31 - __builtin_clz(n))\n#define msbll(n) (63 - __builtin_clzll(n))\n#define gcd __gcd\n#define dump(x)\n#define dumps(x)\n#define dumpel(v)\n#define input_from_file(f)\n#define output_to_file(f)\n#endif\n\n#endif // 折りたたみ用\n\n\n////-----------------AtCoder 専用-----------------\n//#include <atcoder/all>\n//using namespace atcoder;\n//\n//using mint = modint1000000007;\n////using mint = modint998244353;\n////using mint = modint; // mint::set_mod(m);\n//\n//template <class S, S(*op)(S, S), S(*e)()>ostream& operator<<(ostream& os, segtree<S, op, e> seg) { int n = seg.max_right(0, [](S x) {return true; }); rep(i, n) os << seg.get(i) << \" \"; return os; }\n//template <class S, S(*op)(S, S), S(*e)(), class F, S(*mp)(F, S), F(*cp)(F, F), F(*id)()>ostream& operator<<(ostream& os, lazy_segtree<S, op, e, F, mp, cp, id> seg) { int n = seg.max_right(0, [](S x) {return true; }); rep(i, n) os << seg.get(i) << \" \"; return os; }\n//istream& operator>> (istream& is, mint& x) { ll x_; is >> x_; x = x_; return is; }\n//ostream& operator<< (ostream& os, const mint& x) { os << x.val(); return os; }\n//using vm = vector<mint>;\tusing vvm = vector<vm>;\t\tusing vvvm = vector<vvm>;\n////----------------------------------------------\n\n\n//【コスト付きグラフの入力(コストは別)】O(|V| + |E|)\n/*\n* 始点 終点 コストの組からなる入力を受け取り,n 頂点 m 辺のグラフを構成する.\n* 辺へのコストの割り当ては別で記録する.\n*\n* n : グラフの頂点の数\n* m : グラフの辺の数\n* g : ここにグラフを構築して返す\n* c : 辺 (s, t) のコストを c[s][t] に格納する\n* directed : 有向グラフなら true\n* one_indexed : 入力が 1-indexed で与えられるなら true\n*/\nvoid read_graph(int n, int m, Graph& g, vector<unordered_map<int, ll>>& c,\n\tbool directed = false, bool one_indexed = true) {\n\tg = Graph(n);\n\tc = vector<unordered_map<int, ll>>(n);\n\trep(i, m) {\n\t\tint a, b; ll x;\n\t\tcin >> a >> b >> x;\n\n\t\tif (one_indexed) { a--; b--; }\n\n\t\tg[a].push_back(b);\n\t\tc[a][b] = x;\n\t\tif (!directed) {\n\t\t\tg[b].push_back(a);\n\t\t\tc[b][a] = x;\n\t\t}\n\t}\n}\n\n\n//【クリークの列挙】O(2^√(2|E|) |V|)\n/*\n* 無向グラフ g の i 番目に見つけたクリークを cs[i] に頂点の列として列挙する.\n* S ⊂ V がクリークであるとは,S の任意の 2 点を結ぶ辺が E に属することをいう.\n*/\nvoid enumerate_clique(const Graph& g, vvi& cs) {\n\t// 参考:https://www.slideshare.net/wata_orz/ss-12131479\n\n\tint n = sz(g);\n\tcs.clear();\n\n\t// 隣接行列 adj,各頂点の次数 deg,総次数 deg_sum,\n\t// 最小次数 deg_min,次数最小頂点の番号 i_min を得る.\n\tvvb adj(n, vb(n));\n\tvi deg(n);\n\tint deg_sum = 0, deg_min = INF, i_min = -1;\n\trep(s, n) {\n\t\tdeg[s] = sz(g[s]);\n\t\tdeg_sum += deg[s];\n\t\trepe(t, g[s]) adj[s][t] = true;\n\n\t\tif (chmin(deg_min, deg[s])) i_min = s;\n\t}\n\n\t// 考慮すべき頂点のリスト\n\tvi v(n);\n\tiota(all(v), 0);\n\n\t// 素朴な方法で最大クリークを求める O(2^n n)\n\tfunction<void()> naive = [&]() {\n\t\t// 全ての部分集合 set について\n\t\trepb(set, n) {\n\t\t\tbool sum = 0;\n\n\t\t\t// n 頂点それぞれについて\n\t\t\trep(i, n) {\n\t\t\t\t// set に選んでいないなら無関係\n\t\t\t\tif (!(set & (1 << i))) {\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\n\t\t\t\t// i 番目以降の頂点について\n\t\t\t\trepi(j, i + 1, n - 1) {\n\t\t\t\t\t// set に選んでいないなら無関係 \n\t\t\t\t\tif (!(set & (1 << j))) {\n\t\t\t\t\t\tcontinue;\n\t\t\t\t\t}\n\n\t\t\t\t\t// 辺 (v[i], v[j]) がなければクリークでない.\n\t\t\t\t\tif (!adj[v[i]][v[j]]) {\n\t\t\t\t\t\tgoto NEXT_LOOP;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// クリークが見つかったので記録する.\n\t\t\tcs.push_back(vi());\n\t\t\trep(i, n) {\n\t\t\t\tif (set & (1 << i)) {\n\t\t\t\t\tcs.rbegin()->push_back(v[i]);\n\t\t\t\t}\n\t\t\t}\n\n\t\tNEXT_LOOP:;\n\t\t}\n\t};\n\n\tint res = 1;\n\twhile (n > 0) {\n\t\t// 辺に対して頂点が十分少ないなら素朴な方法で構わない.\n\t\tif (deg_min * deg_min >= deg_sum) {\n\t\t\tnaive();\n\t\t\treturn;\n\t\t}\n\n\t\t// 次数最小の頂点 v[i_min] の隣接点の番号の集合を得る.\n\t\t// 同時に v[i_min] に出入りする辺を削除したことにし,各頂点の次数 deg を更新する.\n\t\tvi ia;\n\t\trep(i, n) {\n\t\t\tif (adj[v[i_min]][v[i]]) {\n\t\t\t\tia.push_back(i);\n\n\t\t\t\tdeg[v[i]]--;\n\t\t\t}\n\t\t}\n\t\tint d = sz(ia);\n\n\t\t// まず v[i_min] を含む最大クリークの大きさ res を求める.\n\t\t// v[i_min] の隣接点の部分集合 sub すべてについて\n\t\trepb(sub, d) {\n\t\t\trep(i, d) {\n\t\t\t\t// sub に選んでいないなら無関係\n\t\t\t\tif (!(sub & (1 << i))) {\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\n\t\t\t\trepi(j, i + 1, d - 1) {\n\t\t\t\t\t// sub に選んでいないなら無関係\n\t\t\t\t\tif (!(sub & (1 << j))) {\n\t\t\t\t\t\tcontinue;\n\t\t\t\t\t}\n\n\t\t\t\t\t// sub がクリークでなければ何もしない.\n\t\t\t\t\tif (!adj[v[ia[i]]][v[ia[j]]]) {\n\t\t\t\t\t\tgoto LOOP_END;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// sub がクリークなら v[i_min] と合わせてもクリークとなるので記録する.\n\t\t\tcs.push_back(vi({ v[i_min] }));\n\t\t\trep(i, d) {\n\t\t\t\tif (sub & (1 << i)) {\n\t\t\t\t\tcs.rbegin()->push_back(v[ia[i]]);\n\t\t\t\t}\n\t\t\t}\n\n\t\tLOOP_END:;\n\t\t}\n\n\t\t// v[i_min] を含む最大クリークの大きさは求まったので,\n\t\t// 以降は v[i_min] を含まないクリークだけを考えれば良い.\n\t\t// 頂点 v[i_min] と v[n-1] を交換して n を減らすことで v[i_min] を除去する.\n\t\tswap(v[i_min], v[n - 1]);\n\t\tn--;\n\n\t\t// 総次数 deg_sum,最小次数 deg_min,次数最小頂点の番号 i_min を得る.\n\t\tdeg_sum = 0;\n\t\tdeg_min = INF;\n\t\trep(i, n) {\n\t\t\tif (chmin(deg_min, deg[v[i]])) {\n\t\t\t\ti_min = i;\n\t\t\t}\n\t\t\tdeg_sum += deg[v[i]];\n\t\t}\n\t}\n}\n\n\nint main() {\n//\tinput_from_file(\"input.txt\");\n//\toutput_to_file(\"output.txt\");\n\n\tint n, m;\n\tcin >> n >> m;\n\n\tGraph g;\n\tvector<unordered_map<int, ll>> c;\n\tread_graph(n, m, g, c);\n\tdumpel(g);\n\tdumpel(c);\n\n\tvvi cs;\n\tenumerate_clique(g, cs);\n\tdumpel(cs);\n\n\tll res = 0;\n\trepe(cq, cs) {\n\t\tint k = sz(cq);\n\t\tif (k <= 1) continue;\n\n\t\tll sc = 0;\n\t\trep(i, k) {\n\t\t\tll sat = INFL;\n\t\t\trep(j, k) {\n\t\t\t\tif (i == j) continue;\n\n\t\t\t\tchmin(sat, c[cq[i]][cq[j]]);\n\t\t\t}\n\t\t\tsc += sat;\n\t\t}\n\n\t\tchmax(res, sc);\n\t}\n\n\tcout << res << endl;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 4700, "score_of_the_acc": -0.0229, "final_rank": 6 }, { "submission_id": "aoj_2306_5990282", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << fixed << setprecision(15);\n\n int n, m;\n cin >> n >> m;\n vector<vector<int>> G(n, vector<int>(n));\n set<vector<int>> st;\n for (int i = 0; i < m; ++i) {\n int u, v, f;\n cin >> u >> v >> f;\n --u, --v;\n if (u > v) swap(u, v);\n G[u][v] = G[v][u] = f;\n st.insert({u, v});\n }\n int ans = 0;\n while (!st.empty()) {\n set<vector<int>> nst;\n for (auto& v : st) {\n int val = 0;\n for (int i : v) {\n int m = 1e9;\n for (int j : v) {\n if (i != j) m = min(m, G[i][j]);\n }\n val += m;\n }\n ans = max(ans, val);\n\n for (int k = 0; k < n; ++k) {\n bool ok = true;\n for (int i : v) {\n if (G[i][k] == 0) {\n ok = false;\n break;\n }\n }\n if (ok) {\n auto u = v;\n u.push_back(k);\n sort(u.begin(), u.end());\n nst.insert(u);\n }\n }\n }\n st.swap(nst);\n }\n cout << ans << endl;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 4356, "score_of_the_acc": -0.0233, "final_rank": 7 }, { "submission_id": "aoj_2306_5959738", "code_snippet": "#include<bits/stdc++.h> \nusing namespace std;\nusing ll=long long int;\n//using Int=__int128;\n#define ALL(A) A.begin(),A.end()\ntemplate<typename T1,typename T2> bool chmin(T1 &a,T2 b){if(a<=b)return 0; a=b; return 1;}\ntemplate<typename T1,typename T2> bool chmax(T1 &a,T2 b){if(a>=b)return 0; a=b; return 1;}\ntemplate<typename T> constexpr int bitUP(T x,int a){return (x>>a)&1;}\n//→ ↓ ← ↑ \nint dh[4]={0,1,0,-1}, dw[4]={1,0,-1,0};\n//右上から時計回り\n//int dh[8]={-1,0,1,1,1,0,-1,-1}, dw[8]={1,1,1,0,-1,-1,-1,0};\nlong double EPS = 1e-6;\nlong double PI = acos(-1);\nconst ll INF=(1LL<<62);\nconst int MAX=(1<<30);\nconstexpr ll MOD=1000000000+7;\n//constexpr ll MOD=998244353;\n\n\ninline void bin101(){\n ios::sync_with_stdio(false);\n cin.tie(0);\n cout << fixed << setprecision(20);\n}\n\nusing pii=pair<int,int>;\nusing pil=pair<int,ll>;\nusing pli=pair<ll,int>;\nusing pll=pair<ll,ll>;\nusing psi=pair<string,int>;\nusing pis=pair<int,string>;\nusing psl=pair<string,ll>;\nusing pls=pair<ll,string>;\nusing pss=pair<string,string>;\n\nusing Graph=vector<vector<int>>;\n\ntemplate<typename T >\nstruct edge {\n\tint to;\n\tT cost;\n\tedge()=default;\n\tedge(int to, T cost) : to(to), cost(cost) {}\n\n};\ntemplate<typename T>\nusing WeightGraph=vector<vector<edge<T>>>;\n\ntemplate<typename T>\nvoid CinGraph(int M,WeightGraph<T> &g,bool directed=false,bool index1=true){\n while(M--){\n int s,t;\n T cost;\n cin>>s>>t>>cost;\n if(index1) s--,t--;\n g[s].emplace_back(t,cost);\n if(not directed) g[t].emplace_back(s,cost);\n }\n}\n\nvoid CinGraph(int M,Graph &g,bool directed=false,bool index1=true){\n while(M--){\n int s,t;\n cin>>s>>t;\n if(index1) s--,t--;\n g[s].push_back(t);\n if(not directed) g[t].push_back(s);\n }\n}\n\n\n//0-indexed vector cin\ntemplate<typename T>\ninline istream &operator>>(istream &is,vector<T> &v) {\n for(size_t i=0;i<v.size();i++) is>>v[i];\n\treturn is;\n}\n \n//0-indexed vector cin\ntemplate<typename T>\ninline istream &operator>>(istream &is,vector<vector<T>> &v) {\n for(size_t i=0;i<v.size();i++){\n is>>v[i];\n }\n return is;\n}\n//vector cout\ntemplate<typename T>\ninline ostream &operator<<(ostream &os,const vector<T> &v) {\n for(size_t i=0;i<v.size();i++){\n if(i) os<<\" \";\n os<<v[i];\n }\n return os;\n}\n//vector<vector> cout\ntemplate<typename T>\ninline ostream &operator<<(ostream &os,const vector<vector<T>> &v) {\n for(size_t i=0;i<v.size();i++){\n os<<v[i];\n if(i+1!=v.size()) os<<\"\\n\";\n }\n return os;\n}\n\n//Graph out\ntemplate<typename T>\ninline ostream &operator<<(ostream &os,const Graph &g) {\n for(size_t i=0;i<g.size();i++){\n for(int to:g[i]){\n os<<i<<\"->\"<<to<<\" \";\n }\n os<<endl;\n }\n return os;\n}\n\n//WeightGraph out\ntemplate<typename T>\ninline ostream &operator<<(ostream &os,const WeightGraph<T> &g) {\n for(size_t i=0;i<g.size();i++){\n for(auto e:g[i]){\n os<<i<<\"->\"<<e.to<<\"(\"<<e.cost<<\") \";\n }\n os<<endl;\n }\n return os;\n}\n\n\n//要素数n 初期値x\ntemplate<typename T>\ninline vector<T> vmake(size_t n,T x){\n\treturn vector<T>(n,x);\n}\n\n//a,b,c,x data[a][b][c] 初期値x\ntemplate<typename... Args>\nauto vmake(size_t n,Args... args){\n\tauto v=vmake(args...);\n\treturn vector<decltype(v)>(n,move(v));\n}\ntemplate<typename V,typename T>\nvoid fill(V &v,const T value){\n v=value;\n}\n\ntemplate<typename V,typename T>\nvoid fill(vector<V> &vec,const T value){\n for(auto &v:vec) fill(v,value);\n}\n\n//pair cout\ntemplate<typename T, typename U>\ninline ostream &operator<<(ostream &os,const pair<T,U> &p) {\n\tos<<p.first<<\" \"<<p.second;\n\treturn os;\n}\n \n//pair cin\ntemplate<typename T, typename U>\ninline istream &operator>>(istream &is,pair<T,U> &p) {\n\tis>>p.first>>p.second;\n\treturn is;\n}\n \n//ソート\ntemplate<typename T>\ninline void vsort(vector<T> &v){\n sort(v.begin(),v.end());\n}\n \n//逆順ソート\ntemplate<typename T>\ninline void rvsort(vector<T> &v){\n\tsort(v.rbegin(),v.rend());\n}\n\n//1ビットの数を返す\ninline int popcount(int x){\n\treturn __builtin_popcount(x);\n}\n//1ビットの数を返す\ninline int popcount(ll x){\n\treturn __builtin_popcountll(x);\n}\ntemplate<typename T>\ninline void Compress(vector<T> &C){\n sort(C.begin(),C.end());\n C.erase(unique(C.begin(),C.end()),C.end());\n}\ntemplate<typename T>\ninline int lower_idx(const vector<T> &C,T value){\n return lower_bound(C.begin(),C.end(),value)-C.begin();\n}\ntemplate<typename T>\ninline int upper_idx(const vector<T> &C,T value){\n return upper_bound(C.begin(),C.end(),value)-C.begin();\n}\n//時計回りに90度回転\ntemplate<typename T>\ninline void rotate90(vector<vector<T>> &C){\n vector<vector<T>> D(C[0].size(),vector<T>(C.size()));\n for(int h=0;h<C.size();h++){\n for(int w=0;w<C[h].size();w++){\n D[w][C.size()-1-h]=C[h][w];\n }\n }\n C=D;\n}\n//補グラフを返す\n//i→iのような辺は加えない\nGraph ComplementGraph(const Graph &g){\n size_t sz=g.size();\n bool used[sz][sz];\n fill(used[0],used[sz],false);\n for(size_t i=0;i<sz;i++){\n for(int to:g[i]){\n used[i][to]=true;\n }\n }\n Graph ret(sz);\n for(size_t i=0;i<sz;i++){\n for(size_t j=0;j<sz;j++){\n if(used[i][j]) continue;\n if(i==j) continue;\n ret[i].push_back(j);\n }\n }\n return ret;\n}\n//グラフの分解 secondはある頂点がどこに対応するか id[i]={2,3}のとき,頂点iはret[2][3]に対応\n//無効グラフのみに対応\npair<vector<Graph>,vector<pair<int,int>>> GraphDecomposition(const Graph &g){\n vector<pair<int,int>> id(g.size(),pair<int,int>(-1,-1));\n vector<Graph> ret;\n vector<int> now;\n for(size_t i=0;i<g.size();i++){\n if(id[i].first!=-1) continue;\n id[i].first=ret.size();\n id[i].second=0;\n now.push_back(i);\n for(size_t j=0;j<now.size();j++){\n for(int to:g[now[j]]){\n if(id[to].first==-1){\n id[to].first=ret.size();\n id[to].second=now.size();\n now.push_back(to);\n }\n }\n }\n Graph r(now.size());\n for(size_t j=0;j<now.size();j++){\n r[j]=g[now[j]];\n for(int &to:r[j]){\n to=id[to].second;\n }\n }\n ret.push_back(r);\n now.clear();\n }\n return make_pair(ret,id);\n}\n//0indexを想定\nbool OutGrid(ll h,ll w,ll H,ll W){\n return (h>=H or w>=W or h<0 or w<0);\n}\n\nvoid NO(){\n cout<<\"NO\"<<\"\\n\";\n}\nvoid YES(){\n cout<<\"YES\"<<\"\\n\";\n}\nvoid No(){\n cout<<\"No\"<<\"\\n\";\n}\nvoid Yes(){\n cout<<\"Yes\"<<\"\\n\";\n}\nnamespace overflow{\n template<typename T>\n T max(){\n return numeric_limits<T>::max();\n }\n template<typename T>\n T ADD(T a,T b){\n T res;\n return __builtin_add_overflow(a,b,&res)?max<T>():res;\n }\n template<typename T>\n T MUL(T a,T b){\n T res;\n return __builtin_mul_overflow(a,b,&res)?max<T>():res;\n }\n};\nusing namespace overflow;\nstruct ModInt{\n using u64=uint_fast64_t;\n u64 a;\n constexpr ModInt() :a(0){}\n constexpr ModInt(ll x) :a((x>=0)?(x%MOD):(x%MOD+MOD) ) {}\n\n inline constexpr ModInt operator+(const ModInt rhs)const noexcept{\n return ModInt(*this)+=rhs;\n }\n inline constexpr ModInt operator-(const ModInt rhs)const noexcept{\n return ModInt(*this)-=rhs;\n }\n inline constexpr ModInt operator*(const ModInt rhs)const noexcept{\n return ModInt(*this)*=rhs;\n }\n inline constexpr ModInt operator/(const ModInt rhs)const noexcept{\n return ModInt(*this)/=rhs;\n }\n inline constexpr ModInt operator+(const ll rhs) const noexcept{\n return ModInt(*this)+=ModInt(rhs);\n }\n inline constexpr ModInt operator-(const ll rhs)const noexcept{\n return ModInt(*this)-=ModInt(rhs);\n }\n inline constexpr ModInt operator*(const ll rhs)const noexcept{\n return ModInt(*this)*=ModInt(rhs);\n }\n inline constexpr ModInt operator/(const ll rhs)const noexcept{\n return ModInt(*this)/=ModInt(rhs);\n }\n\n inline constexpr ModInt &operator+=(const ModInt rhs)noexcept{\n a+=rhs.a;\n if(a>=MOD) a-=MOD;\n return *this;\n }\n inline constexpr ModInt &operator-=(const ModInt rhs)noexcept{\n if(rhs.a>a) a+=MOD;\n a-=rhs.a;\n return *this;\n }\n inline constexpr ModInt &operator*=(const ModInt rhs)noexcept{\n a=(a*rhs.a)%MOD;\n return *this;\n }\n inline constexpr ModInt &operator/=(ModInt rhs)noexcept{\n a=(a*rhs.inverse().a)%MOD;\n return *this;\n }\n inline constexpr ModInt &operator+=(const ll rhs)noexcept{\n return *this+=ModInt(rhs);\n }\n inline constexpr ModInt &operator-=(const ll rhs)noexcept{\n return *this-=ModInt(rhs);\n }\n inline constexpr ModInt &operator*=(const ll rhs)noexcept{\n return *this*=ModInt(rhs);\n }\n inline constexpr ModInt &operator/=(const ll rhs)noexcept{\n return *this/=ModInt(rhs);\n }\n\n inline constexpr ModInt operator=(const ll x)noexcept{\n a=(x>=0)?(x%MOD):(x%MOD+MOD);\n return *this;\n }\n\n inline constexpr bool operator==(const ModInt p)const noexcept{\n return a==p.a;\n }\n\n inline constexpr bool operator!=(const ModInt p)const noexcept{\n return a!=p.a;\n }\n\n inline constexpr ModInt pow(ll N) const noexcept{\n ModInt ans(1LL),p(a);\n while(N>0){\n if(bitUP(N,0)){\n ans*=p;\n }\n p*=p;\n N>>=1;\n }\n return ans;\n }\n inline constexpr ModInt inverse() const noexcept{\n return pow(MOD-2);\n }\n\n};\ninline constexpr ModInt operator+(const ll &a,const ModInt &b)noexcept{\n return ModInt(a)+=b;\n}\ninline constexpr ModInt operator-(const ll &a,const ModInt &b)noexcept{\n return ModInt(a)-=b;\n}\ninline constexpr ModInt operator*(const ll &a,const ModInt &b)noexcept{\n return ModInt(a)*=b;\n}\ninline constexpr ModInt operator/(const ll &a,const ModInt &b)noexcept{\n return ModInt(a)/=b;\n}\n//cout\ninline ostream &operator<<(ostream &os,const ModInt &p) {\n return os<<p.a;\n}\n\n//cin\ninline istream &operator>>(istream &is,ModInt &p) {\n ll t;\n is>>t;\n p=t;\n return is;\n}\n\nstruct Binominal{\n vector<ModInt> fac,finv,inv; //fac[n]:n! finv:(n!)の逆元\n int sz;\n Binominal(int n=10) :sz(1){\n if(n<=0) n=10;\n init(n);\n }\n inline void init(int n){\n fac.resize(n+1,1);\n finv.resize(n+1,1);\n inv.resize(n+1,1);\n for(int i=sz+1;i<=n;i++){\n fac[i]=fac[i-1]*i;\n inv[i]=MOD-inv[MOD%i]*(MOD/i);\n finv[i]=finv[i-1]*inv[i];\n }\n sz=n;\n }\n //nCk(n,k<=N) をO(1)で求める\n inline ModInt com(int n,int k){\n if(n<k) return ModInt(0);\n if(n<0 || k<0) return ModInt(0);\n if(n>sz) init(n);\n return fac[n]*finv[k]*finv[n-k];\n }\n //nCk(k<=N) をO(k) で求める \n inline ModInt rcom(ll n,int k){\n if(n<0 || k<0 || n<k) return ModInt(0);\n if(k>sz) init(k);\n ModInt ret(1);\n for(int i=0;i<k;i++){\n ret*=n-i;\n }\n ret*=finv[k];\n return ret;\n }\n\n //重複組み合わせ n種類のものから重複を許してk個を選ぶ\n //〇がn個,|がk個\n inline ModInt h(int n,int k){\n return com(n+k-1,k);\n }\n};\nvector<int> Subset(int S,bool zero=false,bool full=false){\n vector<int> ret;\n int now=(S-1)&S;\n if(full and S){\n ret.push_back(S);\n }\n do{\n ret.push_back(now);\n now=(now-1)&S;\n }while(now!=0);\n if(zero){\n ret.push_back(0);\n }\n return ret;\n}\ntemplate<typename T>\nT SUM(const vector<T> &v,int s,int t){\n chmax(s,0);\n chmin(t,int(v.size())-1);\n if(s>t) return 0;\n if(s==0) return v[t];\n else return v[t]-v[s-1];\n}\ntemplate<typename T>\nvoid buildSUM(vector<T> &v){\n for(size_t i=1;i<v.size();i++){\n v[i]+=v[i-1];\n }\n return;\n}\n//最大クリークが14\n//2^14*14^2 ->\n\nbool S[100];\nint N,M;\nint ans=0;\nvector<vector<int>> F;\n\nvoid dfs(int idx){\n\n if(idx==N){\n vector<int> v;\n for(int i=0;i<N;i++){\n if(S[i]) v.push_back(i);\n }\n if(v.size()==1) return;\n int t=0;\n for(size_t i=0;i<v.size();i++){\n int min_v=MAX;\n for(size_t j=0;j<v.size();j++){\n if(i==j) continue;\n chmin(min_v,F[v[i]][v[j]]);\n }\n t+=min_v;\n }\n chmax(ans,t);\n }else{\n for(int i=idx;i<N;i++){\n bool ok=true;\n for(int j=0;j<i;j++){\n if(S[j] and F[i][j]==0) ok=false;\n }\n if(not ok) continue;\n S[i]=true;\n dfs(i+1);\n S[i]=false;\n }\n dfs(N);\n }\n}\n\nvoid solve(){\n cin>>N>>M;\n F=vmake(N,N,0);\n for(int i=0;i<M;i++){\n int s,t; cin>>s>>t;\n s--; t--;\n cin>>F[s][t];\n F[t][s]=F[s][t];\n }\n dfs(0);\n cout<<ans<<endl;\n}\n\nint main(){\n bin101();\n int T=1;\n //cin>>T;\n while(T--) solve();\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3508, "score_of_the_acc": -0.0167, "final_rank": 4 }, { "submission_id": "aoj_2306_5778590", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint N,M;\nint flag[105][105];\nvector<vector<int>>tmp;\n\nvoid dfs(int n,vector<int>res) {\n if(n == N) {\n tmp.push_back(res);\n return;\n }\n bool ok = true;\n for(int i = 0; i < res.size(); i++) {\n if(flag[res[i]][n] == 0) {\n ok = false;\n }\n }\n dfs(n+1,res);\n if(ok) {\n res.push_back(n);\n dfs(n+1,res);\n }\n}\n\nint main() {\n cin >> N >> M;\n for(int i = 0; i < M; i++) {\n int u,v,c;\n cin >> u >> v >> c;\n u--;\n v--;\n flag[u][v] = c;\n flag[v][u] = c;\n }\n dfs(0,{});\n int ans = 0;\n for(int i = 0; i < tmp.size(); i++) {\n int res = 0;\n for(int j = 0; j < tmp[i].size(); j++) {\n int mi = 1001001001;\n for(int k = 0; k < tmp[i].size(); k++) {\n if(tmp[i][j] == tmp[i][k]) {\n continue;\n }\n mi = min(mi,flag[tmp[i][j]][tmp[i][k]]);\n }\n res += mi;\n }\n if(tmp[i].size() == 1) {\n continue;\n }\n ans = max(ans,res);\n }\n cout << ans << endl;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 4572, "score_of_the_acc": -0.0211, "final_rank": 5 }, { "submission_id": "aoj_2306_5300799", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i, n) for(int i = 0; i < n; i++)\n#define rep2(i, x, n) for(int i = x; i <= n; i++)\n#define rep3(i, x, n) for(int i = x; i >= n; i--)\n#define each(e, v) for(auto &e: v)\n#define pb push_back\n#define eb emplace_back\n#define all(x) x.begin(), x.end()\n#define rall(x) x.rbegin(), x.rend()\n#define sz(x) (int)x.size()\nusing ll = long long;\nusing pii = pair<int, int>;\nusing pil = pair<int, ll>;\nusing pli = pair<ll, int>;\nusing pll = pair<ll, ll>;\nconst int MOD = 1000000007;\n//const int MOD = 998244353;\nconst int inf = (1<<30)-1;\nconst ll INF = (1LL<<60)-1;\ntemplate<typename T> bool chmax(T &x, const T &y) {return (x < y)? (x = y, true) : false;};\ntemplate<typename T> bool chmin(T &x, const T &y) {return (x > y)? (x = y, true) : false;};\n\nstruct io_setup{\n io_setup(){\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n cout << fixed << setprecision(15);\n }\n} io_setup;\n\nset<vector<int>> s;\nvector<vector<int>> es;\nint N, M;\nint ans = 0;\n\nvoid dfs(vector<int> v){\n sort(all(v));\n if(s.count(v)) return;\n s.emplace(v);\n\n int n = sz(v), tmp = 0;\n\n if(n > 1){\n rep(i, n){\n int x = inf;\n rep(j, n){\n if(j == i) continue;\n chmin(x, es[v[i]][v[j]]);\n }\n tmp += x;\n }\n }\n\n chmax(ans, tmp);\n\n vector<int> deg(N, 0);\n\n rep(i, n){\n rep(j, N){\n if(es[v[i]][j] > 0) deg[j]++;\n }\n }\n\n rep(i, N){\n if(deg[i] == n){\n v.eb(i), dfs(v), v.pop_back();\n }\n }\n}\n\nint main(){\n cin >> N >> M;\n\n es.assign(N, vector<int>(N, 0));\n\n rep(i, M){\n int u, v, c; cin >> u >> v >> c; u--, v--;\n es[u][v] = c, es[v][u] = c;\n }\n\n rep(i, N) dfs({i});\n\n cout << ans << '\\n';\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 5152, "score_of_the_acc": -0.0397, "final_rank": 8 }, { "submission_id": "aoj_2306_4972333", "code_snippet": "#pragma GCC target(\"avx\")\n#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"unroll-loops\")\n#include<bits/stdc++.h>\n// #include<ext/pb_ds/assoc_container.hpp>\n// #include<ext/pb_ds/tree_policy.hpp>\n// #include<ext/pb_ds/tag_and_trait.hpp>\n// using namespace __gnu_pbds;\n// #include<boost/multiprecision/cpp_int.hpp>\n// namespace multiprecisioninteger = boost::multiprecision;\n// using cint=multiprecisioninteger::cpp_int;\nusing namespace std;\nusing ll=long long;\n#define double long double\nusing datas=pair<ll,ll>;\nusing ddatas=pair<double,double>;\nusing tdata=pair<ll,datas>;\nusing vec=vector<ll>;\nusing mat=vector<vec>;\nusing pvec=vector<datas>;\nusing pmat=vector<pvec>;\n// using llset=tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>;\n#define For(i,a,b) for(i=a;i<(ll)b;++i)\n#define bFor(i,b,a) for(i=b,--i;i>=(ll)a;--i)\n#define rep(i,N) For(i,0,N)\n#define rep1(i,N) For(i,1,N)\n#define brep(i,N) bFor(i,N,0)\n#define brep1(i,N) bFor(i,N,1)\n#define all(v) (v).begin(),(v).end()\n#define allr(v) (v).rbegin(),(v).rend()\n#define vsort(v) sort(all(v))\n#define vrsort(v) sort(allr(v))\n#define endl \"\\n\"\n#define eb emplace_back\n#define print(x) cout<<x<<endl\n#define printyes print(\"Yes\")\n#define printno print(\"No\")\n#define printYES print(\"YES\")\n#define printNO print(\"NO\")\n#define output(v) do{bool f=0;for(auto outi:v){cout<<(f?\" \":\"\")<<outi;f=1;}cout<<endl;}while(0)\n#define matoutput(v) do{for(auto outimat:v)output(outimat);}while(0)\nconst ll mod=1000000007;\n// const ll mod=998244353;\nconst ll inf=1LL<<60;\nconst double PI=acos(-1);\nconst double eps=1e-9;\ntemplate<class T,class E> ostream& operator<<(ostream& os,pair<T,E>& p){return os<<\"(\"<<p.first<<\",\"<<p.second<<\")\";}\ntemplate<class T> ostream& operator<<(ostream& os,const vector<T>& v){\n os<<\"{\";ll i;\n rep(i,v.size()){\n if(i)os<<\",\";\n os<<v[i];\n }\n os<<\"}\";\n return os;\n}\ntemplate<class T> inline bool chmax(T& a,T b){bool x=a<b;if(x)a=b;return x;}\ntemplate<class T> inline bool chmin(T& a,T b){bool x=a>b;if(x)a=b;return x;}\n#ifdef DEBUG\nvoid debugg(){cerr<<endl;}\ntemplate<class T,class... Args>void debugg(const T& x,const Args&... args){cerr<<\" \"<<x;debugg(args...);}\n#define debug(...) cerr<<__LINE__<<\" [\"<<#__VA_ARGS__<<\"]:\",debugg(__VA_ARGS__)\n#else\n#define debug(...) (void(0))\n#endif\n\nvoid startupcpp(){\n cin.tie(0);\n ios::sync_with_stdio(false);\n cout<<fixed<<setprecision(15);\n}\n\ndouble distance(ddatas x,ddatas y){\n double a=x.first-y.first,b=x.second-y.second;\n return sqrt(a*a+b*b);\n}\n\nll modinv(ll a,ll m=mod) {\n ll b=m,u=1,v=0,t;\n while(b){\n t=a/b;\n a-=t*b; swap(a,b);\n u-=t*v; swap(u,v);\n }\n return (u+m)%m;\n}\n\nll moddevide(ll a,ll b){return (a*modinv(b))%mod;}\n\nvec modncrlistp,modncrlistm;\n\nll modncr(ll n,ll r){\n if(n<r)return 0;\n ll i,size=modncrlistp.size();\n if(size<=n){\n modncrlistp.resize(n+1);\n modncrlistm.resize(n+1);\n if(!size){\n modncrlistp[0]=modncrlistm[0]=1;\n size++;\n }\n For(i,size,n+1){\n modncrlistp[i]=modncrlistp[i-1]*i%mod;\n modncrlistm[i]=modinv(modncrlistp[i]);\n }\n }\n return modncrlistp[n]*modncrlistm[r]%mod*modncrlistm[n-r]%mod;\n}\n\nll modpow(ll a,ll n,ll m=mod){\n ll res=1;\n while(n>0){\n if(n&1)res=res*a%m;\n a=a*a%m;\n n>>=1;\n }\n return res;\n}\n\nll gcd(ll a,ll b){if(!b)return abs(a);return (a%b==0)?abs(b):gcd(b,a%b);}\nll lcm(ll a,ll b){return a/gcd(a,b)*b;}\n\nll countdigits(ll n){\n ll ans=0;\n while(n){n/=10;ans++;}\n return ans;\n}\n\nll sumdigits(ll n){\n ll ans=0;\n while(n){ans+=n%10;n/=10;}\n return ans;\n}\nll N;\nll ans;\nmat g;\nmap<vec,int> mp;\nvoid solve(vec v){\n vsort(v);\n if(mp[v])return;\n mp[v]=1;\n debug(v);\n ll i,j,K=v.size();\n rep(i,N){\n rep(j,K){\n if(!g[i][v[j]])break;\n }\n if(j==K){\n v.emplace_back(i);\n solve(v);\n v.pop_back();\n }\n }\n if(K>1){\n ll res=0;\n rep(i,K){\n ll x=inf;\n rep(j,K){\n if(i==j)continue;\n chmin(x,g[v[i]][v[j]]);\n }\n res+=x;\n }\n chmax(ans,res);\n }\n}\nint main(){\n startupcpp();\n ll i,j,k,M;\n cin>>N>>M;\n g.resize(N,vec(N,0));\n while(M--){\n cin>>i>>j>>k;\n --i;--j;\n g[j][i]=g[i][j]=k;\n }\n solve(vec());\n print(ans);\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 5872, "score_of_the_acc": -0.0445, "final_rank": 9 }, { "submission_id": "aoj_2306_4971122", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\n#define var auto\n\nconst char newl = '\\n';\n\ntemplate<typename T1, typename T2>\ninline bool chmin(T1 &a, T2 b) { bool c = a > b; if (c) a = b; return c; }\ntemplate<typename T1, typename T2>\ninline bool chmax(T1 &a, T2 b) { bool c = a < b; if (c) a = b; return c; }\n\nsigned main() {\n cin.tie(0);\n ios::sync_with_stdio(0);\n int n, m;\n cin >> n >> m;\n vector<vector<ll>> mat(n, vector<ll>(n, 0));\n for (int i = 0; i < m; i++){\n int s, t, c;\n cin >> s >> t >> c;\n s--, t--;\n mat[s][t] = c;\n mat[t][s] = c;\n }\n vector<vector<int>> cliques{vector<int>{}};\n for (int i = 0; i < n; i++){\n var newqs = cliques;\n for (var&& clique : cliques){\n bool valid = true;\n for (var&& vert : clique){\n if (mat[vert][i] == 0){\n valid = false;\n break;\n }\n }\n if (!valid) continue;\n var newq = clique;\n newq.emplace_back(i);\n newqs.emplace_back(newq);\n }\n cliques = move(newqs);\n }\n ll res = 0;\n for (var&& clique : cliques){\n ll score = 0;\n for (var&& s : clique){\n ll mn = LLONG_MAX;\n for (var&& t : clique){\n if (s == t) continue;\n chmin(mn, mat[s][t]);\n }\n if (mn == LLONG_MAX) mn = 0;\n score += mn;\n }\n chmax(res, score);\n }\n cout << res << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 7276, "score_of_the_acc": -0.0589, "final_rank": 10 }, { "submission_id": "aoj_2306_4934193", "code_snippet": "#include<iostream>\n#include<vector>\nusing namespace std;\nint ans;\nint N,M;\nint d[100][100];\nvoid dfs(int id,vector<int>A)\n{\n\tif(id==N)\n\t{\n\t\tif(A.size()==1)return;\n\t\tint now=0;\n\t\tfor(int a:A)\n\t\t{\n\t\t\tint t=1e9;\n\t\t\tfor(int b:A)if(a!=b)t=min(t,d[a][b]);\n\t\t\tnow+=t;\n\t\t}\n\t\tif(ans<now)ans=now;\n\t\treturn;\n\t}\n\tdfs(id+1,A);\n\tfor(int a:A)if(d[a][id]==0)return;\n\tA.push_back(id);\n\tdfs(id+1,A);\n}\nmain()\n{\n\tcin>>N>>M;\n\tfor(int i=0;i<M;i++)\n\t{\n\t\tint u,v,f;cin>>u>>v>>f;\n\t\tu--,v--;\n\t\td[u][v]=d[v][u]=f;\n\t}\n\tvector<int>fst;\n\tdfs(0,fst);\n\tcout<<ans<<endl;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3220, "score_of_the_acc": -0.0022, "final_rank": 1 }, { "submission_id": "aoj_2306_4883511", "code_snippet": "#include \"iostream\"\n#include \"climits\"\n#include \"list\"\n#include \"queue\"\n#include \"stack\"\n#include \"set\"\n#include \"functional\"\n#include \"algorithm\"\n#include \"string\"\n#include \"map\"\n#include \"unordered_map\"\n#include \"unordered_set\"\n#include \"iomanip\"\n#include \"cmath\"\n#include \"random\"\n#include \"bitset\"\n#include \"cstdio\"\n#include \"numeric\"\n#include \"cassert\"\n#include \"ctime\"\n\nusing namespace std;\n\nconstexpr long long int MOD = 1000000007;\n//constexpr int MOD = 1000000007;\n//constexpr int MOD = 998244353;\n//constexpr long long int MOD = 998244353;\nconstexpr double EPS = 1e-12;\n\n//int N, M, K, T, H, W, L, R;\nlong long int N, M, K, T, H, W, L, R;\n\nint main() {\n\tios::sync_with_stdio(false);\n\tcin.tie(0);\n\n\tcin >> N >> M;\n\tvector<vector<int>>edge(N, vector<int>(N));\n\tfor (int i = 0; i < M; i++) {\n\t\tcin >> L >> R >> K;\n\t\tL--, R--;\n\t\tedge[L][R] = edge[R][L] = K;\n\t}\n\tvector<set<vector<int>>>vs(N + 1);\n\tfor (int i = 0; i < N; i++) {\n\t\tvector<int>w(N);\n\t\tw[i] = 1;\n\t\tvs[1].insert(w);\n\t}\n\tint ans = 0;\n\tfor (int i = 1; i < N; i++) {\n\t\tfor (auto j : vs[i]) {\n\t\t\tfor (int k = 0; k < N; k++) {\n\t\t\t\tif (j[k])continue;\n\t\t\t\tbool flag = true;\n\t\t\t\tfor (int l = 0; l < N; l++) {\n\t\t\t\t\tif (j[l] && !edge[k][l])flag = false;\n\t\t\t\t}\n\t\t\t\tif (flag) {\n\t\t\t\t\tj[k] = 1;\n\t\t\t\t\tvs[i + 1].insert(j);\n\t\t\t\t\tj[k] = 0;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tfor (int i = 2; i <= N; i++) {\n\t\tfor (auto j : vs[i]) {\n\t\t\tvector<int>sum(N);\n\t\t\tfor (int k = 0; k < N; k++) {\n\t\t\t\tif (!j[k])continue;\n\t\t\t\tsum[k] = MOD;\n\t\t\t\tfor (int l = 0; l < N; l++) {\n\t\t\t\t\tif (!j[l])continue;\n\t\t\t\t\tif (k == l)continue;\n\t\t\t\t\tsum[k] = min(sum[k], edge[k][l]);\n\t\t\t\t}\n\t\t\t}\n\t\t\tans = max(ans, accumulate(sum.begin(), sum.end(), 0));\n\t\t}\n\t}\n\tcout << ans << endl;\n}", "accuracy": 1, "time_ms": 190, "memory_kb": 10928, "score_of_the_acc": -0.2037, "final_rank": 15 }, { "submission_id": "aoj_2306_4820400", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nint INF = 10000000;\nset<vector<bool>> st;\nint dfs(vector<bool> &used, vector<vector<int>> &E){\n if (st.count(used)){\n return 0;\n }\n st.insert(used);\n int n = E.size();\n int ans = 0;\n for (int i = 0; i < n; i++){\n if (used[i]){\n int mn = INF;\n for (int j = 0; j < n; j++){\n if (used[j] && i != j){\n mn = min(mn, E[i][j]);\n }\n }\n if (mn != INF){\n ans += mn;\n }\n }\n }\n for (int i = 0; i < n; i++){\n bool ok = true;\n for (int j = 0; j < n; j++){\n if (used[j]){\n if (E[i][j] == 0 || i == j){\n ok = false;\n }\n }\n }\n if (ok){\n used[i] = true;\n ans = max(ans, dfs(used, E));\n used[i] = false;\n }\n }\n return ans;\n}\nint main(){\n int n, m;\n cin >> n >> m;\n vector<vector<int>> E(n, vector<int>(n, 0));\n for (int i = 0; i < m; i++){\n int u, v, f;\n cin >> u >> v >> f;\n u--;\n v--;\n E[u][v] = f;\n E[v][u] = f;\n }\n vector<bool> used(n, false);\n cout << dfs(used, E) << endl;\n}", "accuracy": 1, "time_ms": 430, "memory_kb": 5024, "score_of_the_acc": -0.2462, "final_rank": 16 }, { "submission_id": "aoj_2306_4256032", "code_snippet": "#define _CRT_SECURE_NO_WARNINGS\n#define _USE_MATH_DEFINES\n\n#pragma comment (linker, \"/STACK:526000000\")\n\n#include \"bits/stdc++.h\"\n\nusing namespace std;\ntypedef string::const_iterator State;\n#define eps 1e-11L\n#define MAX_MOD 1000000007LL\n#define GYAKU 500000004LL\n\n#define MOD 998244353LL\n#define seg_size 262144\n#define pb push_back\n#define mp make_pair\ntypedef long long ll;\n#define REP(a,b) for(long long (a) = 0;(a) < (b);++(a))\n#define ALL(x) (x).begin(),(x).end()\n\nvoid init() {\n iostream::sync_with_stdio(false);\n cout << fixed << setprecision(20);\n}\n\n\nunsigned long xor128() {\n static unsigned long x = 123456789, y = 362436069, z = 521288629, w = 88675123;\n unsigned long t = (x ^ (x << 11));\n x = y; y = z; z = w;\n return (w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)));\n}\n\n//li chao tree by ei1333. It's public available.\n// https://ei1333.github.io/luzhiled/snippets/structure/li-chao-tree.html\n/*\ntemplate< typename T >\nstruct LiChaoTree {\n struct Line {\n T a, b;\n\n Line(T a, T b) : a(a), b(b) {}\n\n inline T get(T x) const { return a * x + b; }\n\n inline bool over(const Line& b, const T& x) const {\n return get(x) < b.get(x);\n }\n };\n\n vector< T > xs;\n vector< Line > seg;\n int sz;\n\n LiChaoTree(const vector< T >& x, T INF) : xs(x) {\n sz = 1;\n while (sz < xs.size()) sz <<= 1;\n while (xs.size() < sz) xs.push_back(xs.back() + 1);\n seg.assign(2 * sz - 1, Line(0, INF));\n }\n\n void update(Line& x, int k, int l, int r) {\n int mid = (l + r) >> 1;\n auto latte = x.over(seg[k], xs[l]), malta = x.over(seg[k], xs[mid]);\n if (malta) swap(seg[k], x);\n if (l + 1 >= r) return;\n else if (latte != malta) update(x, 2 * k + 1, l, mid);\n else update(x, 2 * k + 2, mid, r);\n }\n\n void update(T a, T b) { // ax+b\n Line l(a, b);\n update(l, 0, 0, sz);\n }\n\n T query(int k) { // xs[k]\n const T x = xs[k];\n k += sz - 1;\n T ret = seg[k].get(x);\n while (k > 0) {\n k = (k - 1) >> 1;\n ret = min(ret, seg[k].get(x));\n }\n return ret;\n }\n};\n\n#define int long long\nvoid solve() {\n int n, m;\n cin >> n >> m;\n vector<vector<int>> landmarks;\n vector<vector<int>> costs;\n REP(i, n) {\n vector<int> tmp;\n REP(q, m) {\n int b;\n cin >> b;\n b = xor128() % 10000000;\n tmp.push_back(b);\n }\n landmarks.push_back(tmp);\n }\n REP(i, n) {\n\n vector<int> tmp;\n {\n REP(q, m) {\n int b;\n cin >> b;\n b = xor128() % 100000000;\n tmp.push_back(b);\n }\n }\n costs.push_back(tmp);\n }\n vector<LiChaoTree<int>> yoko; // size should be m\n REP(i, n) {\n vector<int> hoge(m);\n REP(q, m) {\n hoge[q] = q;\n }\n LiChaoTree<int> now(hoge, 1e18);//size should be n\n yoko.push_back(now);\n }\n vector<vector<int>> dp = landmarks;\n for (int q = 0; q < m; ++q) {\n vector<int> hoge(n);\n REP(i, n) {\n hoge[i] = i;\n }\n LiChaoTree<int> now(hoge, 1e18);//size should be n\n\n for (int i = 0; i < n; ++i) {\n int now_min = costs[0][0] * (i + q);\n if (q < 0) {\n int geko = yoko[i].query(q);\n now_min = min(now_min, geko);\n }\n if (i != 0) {\n int geko = now.query(i);\n now_min = min(now_min, geko);\n }\n dp[i][q] = now_min;\n yoko[i].update(costs[i][q], landmarks[i][q] + dp[i][q] - q * costs[i][q]);\n now.update(costs[i][q], landmarks[i][q] + dp[i][q] - i * costs[i][q]);\n }\n }\n\n cout << dp[n - 1][m - 1] << endl;\n}\n*/\n\n#define int ll\nint used[200];\nvector<int> kouho;\nset<pair<int,int>> already;\nint dist[200][200];\nint n, m;\n\nint dfs(int now,pair<int,int> bit) {\n if (now < 50) {\n bit.first += (1LL << now);\n }\n else {\n bit.second += (1LL << (now - 50));\n }\n if (already.count(bit)) {\n return 0;\n }\n used[now] = 1;\n kouho.push_back(now);\n already.insert(bit);\n for (int i = 0; i < n; ++i) {\n if (used[i]) continue;\n int ok = 1;\n REP(q, kouho.size()) {\n if (dist[kouho[q]][i] == 0) {\n ok = 0;\n break;\n }\n }\n if (ok == 0) continue;\n dfs(i, bit);\n }\n kouho.pop_back();\n used[now] = 0;\n return 0;\n}\n\nvoid solve() {\n cin >> n >> m;\n REP(i, m) {\n int a, b, c;\n cin >> a >> b >> c;\n a--; b--;\n dist[a][b] = c;\n dist[b][a] = c;\n }\n int ans = 0;\n REP(i, n) {\n ans = max(ans, dfs(i, pair<int, int>{0, 0}));\n }\n for (auto x : already) {\n vector<int> huku;\n for (int q = 0; q < 51; ++q) {\n if ((1LL << q) & x.first) {\n huku.push_back(q);\n }\n if ((1LL << q) & x.second) {\n huku.push_back(q + 50);\n }\n }\n if (huku.size() == 1) continue;\n int tmp = 0;\n REP(q, huku.size()) {\n int hoge = 1e9;\n REP(j, huku.size()) {\n if (q == j) continue;\n hoge = min(hoge, dist[huku[q]][huku[j]]);\n }\n tmp += hoge;\n }\n ans = max(ans, tmp);\n }\n cout << ans << endl;\n}\n#undef int\nint main() {\n init();\n solve();\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 4252, "score_of_the_acc": -0.0167, "final_rank": 3 }, { "submission_id": "aoj_2306_4228355", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\ntemplate<class T>\nbool chmax(T& a, const T& b) {\n if (a < b) { a = b; return true; }\n return false;\n}\ntemplate<class T>\nbool chmin(T& a, const T& b) {\n if (b < a) { a = b; return true; }\n return false;\n}\n\n// std::vector Declaration\ntemplate<typename T>\nvector<T> make_v(size_t a) { return vector<T>(a); }\ntemplate<typename T, typename... Ts>\nauto make_v(size_t a, Ts... ts) {\n return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));\n}\n\n// std::vector Declaration and Initialization\ntemplate<typename T>\nvector<T> make_vector(size_t a, T x) { return vector<T>(a, x); }\ntemplate<typename T, typename U, typename... Ts>\nauto make_vector(size_t a, U b, Ts... ts) {\n return vector<decltype(make_vector<T>(b,ts...))>(a, make_vector<T>(b, ts...));\n}\n\n// std::vector Input\ntemplate<typename T>\nistream& operator>>(istream& is, vector<T>& v) {\n for (auto &e : v) is >> e;\n return is;\n}\n\n// std::vector Debug\ntemplate<typename T>\nostream& operator<<(ostream& os, const vector<T>& v) {\n os << \"[\";\n bool a = 1;\n for (auto e : v) {\n os << (a ? \"\" : \" \");\n os << e;\n a = 0;\n }\n os << \"]\";\n return os;\n}\n\n// std::array Debug\ntemplate<typename T, size_t n>\nostream& operator<<(ostream& os, const array<T, n>& v) {\n os << \"[\";\n bool a = 1;\n for (auto e : v) {\n os << (a ? \"\" : \" \");\n os << e;\n a = 0;\n }\n os << \"]\";\n return os;\n}\n\n// std::deque Debug\ntemplate<typename T>\nostream& operator<<(ostream& os, const deque<T>& d) {\n os << \"[\";\n bool a = 1;\n for (auto e : d) {\n os << (a ? \"\" : \" \");\n os << e;\n a = 0;\n }\n os << \"]\";\n return os;\n}\n\n// std::pair Debug\ntemplate<typename T, typename U>\nostream& operator<<(ostream& os, const pair<T, U>& p) {\n os << \"(\" << p.first << \" \" << p.second << \")\";\n return os;\n}\n\n// std::set Debug\ntemplate<typename T>\nostream& operator<<(ostream& os, const set<T>& st) {\n os << \"{\";\n bool a = 1;\n for (auto e : st) {\n os << (a ? \"\" : \" \");\n os << e;\n a = 0;\n }\n os << \"}\";\n return os;\n}\n\n// std::multiset Debug\ntemplate<typename T>\nostream& operator<<(ostream& os, const multiset<T>& st) {\n os << \"{\";\n bool a = 1;\n for (auto e : st) {\n os << (a ? \"\" : \" \");\n os << e;\n a = 0;\n }\n os << \"}\";\n return os;\n}\n\n// std::map Debug\ntemplate<typename T, typename U>\nostream& operator<<(ostream& os, const map<T, U>& mp) {\n os << \"{\";\n bool a = 1;\n for (auto e : mp) {\n os << (a ? \"\" : \" \");\n os << e.first << \":\" << e.second;\n a = 0;\n }\n os << \"}\";\n return os;\n}\n\n// std::tuple Debug\ntemplate<int N, class Tuple>\nvoid out(ostream& os, const Tuple& t){}\ntemplate<int N, class Tuple, class H, class ...Ts>\nvoid out(ostream& os, const Tuple& t) {\n if (N) os << \" \";\n os << get<N>(t);\n out<N+1,Tuple,Ts...>(os, t);\n}\ntemplate<class ...Ts>\nostream& operator<<(ostream& os, const tuple<Ts...>& t) {\n os << \"(\";\n out<0,tuple<Ts...>,Ts...>(os, t);\n os << \")\";\n return os;\n}\n\n// Debug\n#define DUMP(x) cerr<<#x<<\" = \"<<(x)<<endl\n\n// Weighted edge\ntemplate<typename T>\nstruct edge {\n int src, to;\n T cost;\n\n edge(int to, T cost) : src(-1), to(to), cost(cost) {}\n edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}\n\n friend ostream& operator<<(ostream& os, const edge& e) {\n return os << \"(\" << e.src << \"->\" << e.to << \":\" << e.cost << \")\";\n }\n};\n\nusing LL = long long;\n\n#define fs first\n#define sc second\n\nconst LL MOD = 1e9+7;\n\nLL next_combination(LL mask) {\n LL x = mask & -mask, y = mask + x;\n return (((mask & ~y) / x) >> 1) | y;\n}\n\nint main()\n{\n cin.tie(0);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(10);\n\n int n, m; cin >> n >> m;\n auto G = make_v<int>(n, n);\n vector<int> deg(n);\n for (int i = 0; i < m; ++i) {\n int u, v, f;\n cin >> u >> v >> f;\n --u, --v;\n G[u][v] = G[v][u] = f;\n ++deg[u], ++deg[v];\n }\n\n int ans = 0;\n\n for (int k = 2; k <= 14; ++k) {\n vector<int> vs;\n for (int i = 0; i < n; ++i) {\n if (deg[i] >= k-1) {\n vs.emplace_back(i);\n }\n }\n\n if (k == 2) {\n for (int a : vs) {\n for (int b : vs) {\n chmax(ans, 2*G[a][b]);\n }\n }\n continue;\n }\n if (k == 3) {\n for (int a : vs) {\n for (int b : vs) {\n for (int c : vs) {\n chmax(ans,\n min(G[a][b],G[a][c]) +\n min(G[b][a],G[b][c]) +\n min(G[c][a],G[c][b])\n );\n }\n }\n }\n continue;\n }\n\n for (LL mask=(1LL<<k)-1; mask < 1LL<<vs.size(); mask=next_combination(mask))\n {\n assert(k <= vs.size());\n\n vector<int> score(n, numeric_limits<int>::max());\n\n for (int i = 0; i < vs.size(); ++i) if (mask >> i & 1) {\n for (int j = i+1; j < vs.size(); ++j) if (mask >> j & 1) {\n chmin(score[vs[i]], G[vs[i]][vs[j]]);\n chmin(score[vs[j]], G[vs[i]][vs[j]]);\n }\n }\n\n int total = 0;\n for (int i = 0; i < vs.size(); ++i) if (mask >> i & 1) {\n total += score[vs[i]];\n }\n chmax(ans, total);\n }\n }\n\n cout << ans << endl;\n\n return 0;\n}", "accuracy": 1, "time_ms": 1720, "memory_kb": 3264, "score_of_the_acc": -0.8935, "final_rank": 18 }, { "submission_id": "aoj_2306_4217008", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing P = pair<int, int>;\nusing vi = vector<int>;\nusing vvi = vector<vector<int>>;\nusing vll = vector<ll>;\nusing vvll = vector<vector<ll>>;\nconst double eps = 1e-10;\nconst ll MOD = 1000000007;\nconst int INF = 1000000000;\nconst ll LINF = 1ll<<50;\ntemplate<typename T>\nvoid printv(const vector<T>& s) {\n for(int i=0;i<(int)(s.size());++i) {\n cout << s[i];\n if(i == (int)(s.size())-1) cout << endl;\n else cout << \" \";\n }\n}\ntemplate<typename T1, typename T2>\nostream& operator<<(ostream &os, const pair<T1, T2> p) {\n os << p.first << \":\" << p.second;\n return os;\n}\nstruct Edge {\n int from, to, friendliness;\n};\nmap<vector<bool>, int> mp;\nll dfs(int now, vector<bool> &sel, const vector<vector<Edge>> &g, const vvll &d, int n) {\n if(mp[sel] != 0) return mp[sel];\n ll res = 0;\n for(int i=0;i<n;++i) {\n if(!sel[i]) continue;\n ll val = LINF;\n for(int j=0;j<n;++j) {\n if(i == j || !sel[j]) continue;\n val = min(val, d[i][j]);\n }\n if(val == LINF) val = 0;\n res += val;\n }\n for(int i=0;i<(int)(g[now].size());++i) {\n int nxt = g[now][i].to;\n if(sel[nxt]) continue;\n bool ok = true;\n for(int j=0;j<n;++j) {\n if(j == nxt) continue;\n if(sel[j] && d[nxt][j] == 0) {\n ok = false;\n break;\n }\n }\n if(ok) {\n sel[nxt] = true;\n res = max(res, dfs(nxt, sel, g, d, n));\n sel[nxt] = false;\n }\n }\n return mp[sel] = res;\n}\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(10);\n int n, m; cin >> n >> m;\n vector<vector<Edge>> g(n);\n vvll d(n, vll(n, 0));\n for(int i=0;i<m;++i) {\n int u, v, f; cin >> u >> v >> f;\n u--; v--;\n g[u].push_back({u, v, f});\n g[v].push_back({v, u, f});\n d[u][v] = f;\n d[v][u] = f;\n }\n vector<bool> sel(n, false);\n ll ans = 0;\n for(int i=0;i<n;++i) {\n sel[i] = true;\n ans = max(ans, dfs(i, sel, g, d, n));\n sel[i] = false;\n }\n cout << ans << endl;\n}", "accuracy": 1, "time_ms": 440, "memory_kb": 5320, "score_of_the_acc": -0.2555, "final_rank": 17 }, { "submission_id": "aoj_2306_4143122", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\n#define all(x) (x).begin(),(x).end()\nconst int mod=998244353,MAX=103,INF=1<<30;\n\nint main(){\n \n std::ifstream in(\"text.txt\");\n std::cin.rdbuf(in.rdbuf());\n cin.tie(0);\n ios::sync_with_stdio(false);\n \n int N,M;cin>>N>>M;\n vector<vector<int>> A(N,vector<int>(N));\n \n for(int i=0;i<M;i++){\n int a,b,c;cin>>a>>b>>c;\n a--;b--;\n A[a][b]=c;\n A[b][a]=c;\n }\n \n int ans=0;\n \n set<vector<int>> SE;\n \n for(int i=0;i<N;i++){\n for(int j=i+1;j<N;j++){\n if(A[i][j]){\n ans=max(ans,A[i][j]*2);\n vector<int> use(N);\n use[i]=1;\n use[j]=1;\n SE.insert(use);\n }\n }\n }\n \n for(int q=2;q<15;q++){\n if(SE.empty()) break;\n \n set<vector<int>> ne;\n \n for(auto it=SE.begin();it!=SE.end();it++){\n for(int i=0;i<N;i++){\n if((*it)[i]) continue;\n bool ok=true;\n \n for(int j=0;j<N;j++){\n if(i==j) continue;\n if((*it)[j]&&A[i][j]==0){\n ok=false;\n break;\n }\n }\n \n if(ok){\n vector<int> S=*it;\n S[i]=1;\n int sum=0;\n for(int j=0;j<N;j++){\n if(S[j]==0) continue;\n int mini=INF;\n for(int k=0;k<N;k++){\n if(S[k]==0) continue;\n if(j==k) continue;\n if(A[j][k]) mini=min(mini,A[j][k]);\n }\n sum+=mini;\n }\n \n ans=max(ans,sum);\n \n ne.insert(S);\n }\n }\n }\n \n SE=ne;\n }\n \n cout<<ans<<endl;\n}", "accuracy": 1, "time_ms": 200, "memory_kb": 6480, "score_of_the_acc": -0.1468, "final_rank": 13 }, { "submission_id": "aoj_2306_4142225", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntypedef pair<ll, ll> l_l;\ntypedef pair<int, int> i_i;\n\ntemplate <typename T>\nbool chmax(T &a, T b) {\n if(a < b) {\n a = b;\n return true;\n }\n return false;\n}\n\ntemplate <typename T>\nbool chmin(T &a, T b) {\n if(a > b) {\n a = b;\n return true;\n }\n return false;\n}\n\n//const ll mod = 1000000007;\nint N, M;\nbitset<100> bits[100];\nbitset<100> Zero;\nint A[100][100];\nint ans;\nvoid dfs(bitset<100> now, bitset<100> can, int vertex) {\n /*\n cerr << \"-----\" << endl;\n cerr << now << endl;\n cerr << can << endl;\n cerr << vertex << endl;\n cerr << \"-----\" << endl;\n */\n if(now.count() >= 2) {\n int score = 0;\n for(int i = 0; i < N; i++) {\n if(!now[i]) continue;\n int nowscore = 1e9;\n for(int j = 0; j < N; j++) {\n if(!now[j]) continue;\n if(i == j) continue;\n chmin(nowscore, A[i][j]);\n }\n score += nowscore;\n }\n chmax(ans, score);\n }\n for(int i = vertex; i < N; i++) {\n if(!can[i]) {\n //cerr << \"ng: \" << vertex << \" \" << i << endl;\n continue;\n }\n auto sub = now;\n sub[i] = true;\n auto sub2 = can;\n sub2 &= bits[i];\n //cerr << \"ok : \" << vertex << \" \" << i << endl;\n dfs(sub, sub2, i + 1);\n }\n}\n\nint main() {\n //cout.precision(10);\n cin.tie(0);\n ios::sync_with_stdio(false);\n cin >> N >> M;\n for(int i = 0; i < M; i++) {\n int a, b, c;\n cin >> a >> b >> c;\n a--;\n b--;\n A[a][b] = c;\n A[b][a] = c;\n bits[a][b] = true;\n bits[b][a] = true;\n }\n bitset<100> One;\n for(int i = 0; i < N; i++) One[i] = true;\n dfs(Zero, One, 0);\n cout << ans << endl;\n /*\n for(int i = 0; i < N; i++) {\n cerr << bits[i] << endl;\n }\n */\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3256, "score_of_the_acc": -0.0027, "final_rank": 2 }, { "submission_id": "aoj_2306_3917190", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing Int = long long;\ntemplate<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}\ntemplate<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}\n\n\ntemplate<typename F>\nstruct FixPoint : F{\n FixPoint(F&& f):F(forward<F>(f)){}\n template<typename... Args>\n decltype(auto) operator()(Args&&... args) const{\n return F::operator()(*this,forward<Args>(args)...);\n }\n};\ntemplate<typename F>\ninline decltype(auto) MFP(F&& f){\n return FixPoint<F>{forward<F>(f)};\n}\n\n//INSERT ABOVE HERE\nconst int MAX = 111;\nint es[MAX][MAX]={};\nsigned main(){\n int n,m;\n cin>>n>>m;\n for(int i=0;i<m;i++){\n int u,v,w;\n cin>>u>>v>>w;\n u--;v--;\n es[u][v]=es[v][u]=w;\n }\n\n int ans=0;\n vector<int> used(n,0);\n MFP([&](auto dfs,int v)->void{\n int res=0,cnt=0;\n for(int i=0;i<n;i++){\n if(!used[i]) continue;\n cnt++;\n int tmp=1e9;\n for(int j=0;j<n;j++)\n if(i!=j&&used[j]) chmin(tmp,es[i][j]);\n if(tmp==0) return;\n res+=tmp;\n }\n if(cnt>1) chmax(ans,res);\n\n if(v==n) return;\n\n used[v]=1;\n dfs(v+1);\n used[v]=0;\n dfs(v+1);\n })(0);\n\n cout<<ans<<endl;\n return 0;\n}", "accuracy": 1, "time_ms": 390, "memory_kb": 3132, "score_of_the_acc": -0.1989, "final_rank": 14 } ]
aoj_2303_cpp
Marathon Match N people run a marathon. There are M resting places on the way. For each resting place, the i -th runner takes a break with probability P_i percent. When the i -th runner takes a break, he gets rest for T_i time. The i -th runner runs at constant speed V_i , and the distance of the marathon is L . You are requested to compute the probability for each runner to win the first place. If a runner arrives at the goal with another person at the same time, they are not considered to win the first place. Input A dataset is given in the following format: N M L P_1 T_1 V_1 P_2 T_2 V_2 ... P_N T_N V_N The first line of a dataset contains three integers N ( 1 \leq N \leq 100 ), M ( 0 \leq M \leq 50 ) and L ( 1 \leq L \leq 100,000 ). N is the number of runners. M is the number of resting places. L is the distance of the marathon. Each of the following N lines contains three integers P_i ( 0 \leq P_i \leq 100 ), T_i ( 0 \leq T_i \leq 100 ) and V_i ( 0 \leq V_i \leq 100 ) describing the i -th runner. P_i is the probability to take a break. T_i is the time of resting. V_i is the speed. Output For each runner, you should answer the probability of winning. The i -th line in the output should be the probability that the i -th runner wins the marathon. Each number in the output should not contain an error greater than 10^{-5} . Sample Input 1 2 2 50 30 50 1 30 50 2 Output for the Sample Input 1 0.28770000 0.71230000 Sample Input 2 2 1 100 100 100 10 0 100 1 Output for the Sample Input 2 0.00000000 1.00000000 Sample Input 3 3 1 100 50 1 1 50 1 1 50 1 1 Output for the Sample Input 3 0.12500000 0.12500000 0.12500000 Sample Input 4 2 2 50 30 0 1 30 50 2 Output for the Sample Input 4 0.51000000 0.49000000
[ { "submission_id": "aoj_2303_10853140", "code_snippet": "#include<bits/stdc++.h>\ntemplate <class T>\ninline bool rd(T &ret) {\n char c; int sgn;\n if(c=getchar(),c==EOF) return 0;\n while(c!='-'&&(c<'0'||c>'9')) c=getchar();\n sgn=(c=='-')?-1:1;\n ret=(c=='-')?0:(c-'0');\n while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0');\n ret*=sgn;\n return 1;\n}\ntemplate <class T>\ninline void pt(T x) {\n if (x <0) {\n putchar('-');\n x = -x;\n }\n if(x>9) pt(x/10);\n putchar(x%10+'0');\n}\nusing namespace std;\ntypedef long long ll;\nconst int MAXN = 55;\ndouble C[MAXN+1][MAXN+1];\nvoid Initial()\n{\n memset(C, 0, sizeof C);\n for(int i=0; i<=MAXN; ++i)\n {\n C[0][i] = 0.0;\n C[i][0] = 1.0;\n }\n for(int i=1; i<=MAXN; ++i)\n {\n for(int j=1; j<=MAXN; ++j)\n C[i][j] = C[i-1][j] + C[i-1][j-1];\n }\n}\nconst int N = 110;\nint n, m;\nint l, t[N], v[N];\ndouble p[N];\ndouble Pow(double x, int y){\n double ans = 1.0;\n while(y){\n if(y&1)ans *= x;\n x = x*x;\n y >>= 1;\n }\n return ans;\n}\ndouble work(int x){\n double ans = 0.0;\n for(int i = 0; i <= m; i++)\n {\n double pp = C[m][i] * Pow(p[x], i) * Pow(1.0-p[x], m-i);\n for(int j = 1; j <= n; j++)\n {\n if(j == x)continue;\n double siz = 0;\n for(int k = m; k >= 0; k--)\n {\n if(l*v[j] + i*t[x]*v[j]*v[x] < l*v[x] + k*t[j]*v[x]*v[j])\n siz += C[m][k] * Pow(p[j], k) * Pow(1.0-p[j], m-k);\n else break;\n }\n pp *= siz;\n }\n ans += pp;\n }\n return ans;\n}\nvoid input(){\n for(int i = 1; i <= n; i++)\n {\n rd(p[i]);rd(t[i]);rd(v[i]);\n p[i] /= 100.0;\n }\n}\nint main(){\n Initial();\n while(cin>>n>>m>>l){\n input();\n for(int i = 1; i <= n; i++)\n printf(\"%.10f\\n\", work(i));\n }\n return 0;\n}\n/*\n30 0 12\n0.5\n0\n13\n\n*/", "accuracy": 1, "time_ms": 70, "memory_kb": 3596, "score_of_the_acc": -0.0703, "final_rank": 5 }, { "submission_id": "aoj_2303_9666990", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n/**\n * @brief template\n */\n\nint main() {\n ll N, M, L;\n cin >> N >> M >> L;\n vector<double> P(N);\n vector<ll> T(N), V(N);\n rep(i,0,N) {\n cin >> P[i] >> T[i] >> V[i];\n P[i] /= 100.0;\n }\n vector<vector<double>> X(N);\n rep(i,0,N) {\n if (V[i] == 0) continue;\n vector<double> DP(M+1,0), NewDP(M+1,0);\n DP[0] = 1;\n rep(j,0,M) {\n rep(k,0,M+1) NewDP[k] = 0;\n rep(k,0,M+1) {\n if (DP[k] == 0) continue;\n NewDP[k] += DP[k] * (1-P[i]);\n NewDP[k+1] += DP[k] * P[i];\n }\n swap(DP,NewDP);\n }\n X[i].resize(M+1);\n rep(j,0,M+1) X[i][j] = DP[j];\n }\n rep(i,0,N) {\n double ANS = 0;\n rep(j,0,X[i].size()) {\n double MUL = X[i][j];\n rep(k,0,N) {\n if (k == i) continue;\n double SUM = 0.0;\n if (X[k].empty()) SUM = 1.0;\n else {\n rep(l,0,X[k].size()) {\n if ((L+V[i]*T[i]*j)*V[k] < (L+V[k]*T[k]*l)*V[i]) SUM += X[k][l];\n }\n }\n MUL *= SUM;\n }\n ANS += MUL;\n }\n printf(\"%.12f\\n\", ANS);\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3592, "score_of_the_acc": 0, "final_rank": 1 }, { "submission_id": "aoj_2303_9666972", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n/**\n * @brief template\n */\n\nint main() {\n ll N, M, L;\n cin >> N >> M >> L;\n vector<double> P(N);\n vector<ll> T(N), V(N);\n rep(i,0,N) {\n cin >> P[i] >> T[i] >> V[i];\n P[i] /= 100.0;\n }\n vector<vector<double>> X(N);\n rep(i,0,N) {\n if (V[i] == 0) continue;\n vector<double> DP(M+1,0), NewDP(M+1,0);\n DP[0] = 1;\n rep(j,0,M) {\n rep(k,0,M+1) NewDP[k] = 0;\n rep(k,0,M+1) {\n if (DP[k] == 0) continue;\n NewDP[k] += DP[k] * (1-P[i]);\n NewDP[k+1] += DP[k] * P[i];\n }\n swap(DP,NewDP);\n }\n X[i].resize(M+1);\n rep(j,0,M+1) X[i][j] = DP[j];\n }\n rep(i,0,N) {\n double ANS = 0;\n rep(j,0,X[i].size()) {\n double MUL = X[i][j];\n rep(k,0,N) {\n if (k == i) continue;\n double SUM = 0.0;\n rep(l,0,X[k].size()) {\n if ((L+V[i]*T[i]*j)*V[k] < (L+V[k]*T[k]*l)*V[i]) SUM += X[k][l];\n }\n MUL *= SUM;\n }\n ANS += MUL;\n }\n printf(\"%.12f\\n\", ANS);\n }\n}", "accuracy": 0.18571428571428572, "time_ms": 10, "memory_kb": 3612, "score_of_the_acc": -0.0104, "final_rank": 13 }, { "submission_id": "aoj_2303_8986393", "code_snippet": "#line 2 \"cp-library/src/cp-template.hpp\"\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nusing i32 = int;\nusing u32 = unsigned int;\nusing i64 = long long;\nusing u64 = unsigned long long;\nusing i128 = __int128_t;\ntemplate < class T > bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }\ntemplate < class T > bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }\ntemplate < class T, class U > T ceil (T x, U y) { return (x > 0 ? (x + y - 1) / y : x / y); }\ntemplate < class T, class U > T floor(T x, U y) { return (x > 0 ? x / y : (x - y + 1) / y); }\nint popcnt(i32 x) { return __builtin_popcount(x); }\nint popcnt(u32 x) { return __builtin_popcount(x); }\nint popcnt(i64 x) { return __builtin_popcountll(x); }\nint popcnt(u64 x) { return __builtin_popcountll(x); }\n\n#line 2 \"cp-library/src/utility/rep_itr.hpp\"\ntemplate < class T > struct itr_rep {\n T i, d;\n constexpr itr_rep(const T i) noexcept : i(i), d(1) {}\n constexpr itr_rep(const T i, const T d) noexcept : i(i), d(d) {}\n void operator++() noexcept { i += d; }\n constexpr int operator*() const noexcept { return i; }\n constexpr bool operator!=(const itr_rep x) const noexcept { return d > 0 ? i < x.i : i > x.i; }\n};\n\ntemplate < class T > struct rep {\n const itr_rep< T > s, t;\n constexpr rep(const T t) noexcept : s(0), t(t) {}\n constexpr rep(const T s, const T t) noexcept : s(s), t(t) {}\n constexpr rep(const T s, const T t, const T d) noexcept : s(s, d), t(t, d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n\ntemplate < class T > struct revrep {\n const itr_rep < T > s, t;\n constexpr revrep(const T t) noexcept : s(t - 1, -1), t(-1, -1) {}\n constexpr revrep(const T s, const T t) noexcept : s(t - 1, -1), t(s - 1, -1) {}\n constexpr revrep(const T s, const T t, const T d) noexcept : s(t - 1, -d), t(s - 1, -d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n#line 3 \"cp-library/src/utility/io.hpp\"\n\n/* 128bit integer */\nistream& operator>>(istream& is, i128& x) {\n std::string s; is >> s;\n int pm = (s[0] == '-');\n x = 0;\n for(int i : rep(pm, int(s.size()))) x = x * 10 + (s[i] - '0');\n if(pm) x *= -1;\n return is;\n}\nostream& operator<<(ostream& os, const i128& x) {\n if(x == 0) return os << '0';\n i128 y = x;\n if(y < 0) { os << '-'; y *= -1; }\n std::vector<int> ny;\n while(y > 0) { ny.push_back(y % 10); y /= 10; }\n for(int i : revrep(ny.size())) os << ny[i];\n return os;\n}\n\ntemplate < class S, class T > istream& operator>>(istream& is, std::pair< S, T >& x) { is >> x.first >> x.second; return is; }\ntemplate < class S, class T > ostream& operator<<(ostream& os, const std::pair< S, T >& x) { os << x.first << \" \" << x.second; return os; }\n\nnamespace scanner {\n struct sca {\n template < class T > operator T() {\n T s; std::cin >> s; return s;\n }\n };\n struct vec {\n int n;\n vec(int n) : n(n) {}\n template < class T > operator std::vector< T >() {\n std::vector< T > v(n);\n for(T& x : v) std::cin >> x;\n return v;\n }\n };\n struct mat {\n int h, w;\n mat(int h, int w) : h(h), w(w) {}\n template < class T > operator std::vector< std::vector< T > >() {\n std::vector m(h, std::vector< T >(w));\n for(std::vector< T >& v : m) for(T& x : v) std::cin >> x;\n return m;\n }\n };\n struct speedup {\n speedup() {\n std::cin.tie(0);\n std::ios::sync_with_stdio(0);\n }\n } speedup_instance;\n}\nscanner::sca in() { return scanner::sca(); }\nscanner::vec in(int n) { return scanner::vec(n); }\nscanner::mat in(int h, int w) { return scanner::mat(h, w); }\n\nnamespace printer {\n void precision(int d) { std::cout << std::fixed << std::setprecision(d); }\n void flush() { std::cout.flush(); }\n}\n\ntemplate < class T >\nostream& operator<<(ostream& os, const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) { os << a[i]; if(i != n - 1) os << ' '; }\n return os;\n}\n\nint print() { std::cout << '\\n'; return 0; }\ntemplate < class head, class... tail > int print(head&& h, tail&&... t) {\n std::cout << h; if(sizeof...(tail)) std::cout << ' ';\n return print(std::forward<tail>(t)...);\n}\ntemplate < class T > int print_n(const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) std::cout << a[i] << \"\\n\";\n return 0;\n}\n\n\n#line 2 \"cp-library/src/utility/key_val.hpp\"\n\ntemplate < class K, class V >\nstruct key_val {\n K key; V val;\n key_val() {}\n key_val(K key, V val) : key(key), val(val) {}\n template < std::size_t Index >\n std::tuple_element_t< Index, key_val >& get() {\n if constexpr (Index == 0) return key;\n if constexpr (Index == 1) return val;\n }\n};\n\nnamespace std {\n\ntemplate < class K, class V > struct tuple_size < key_val< K, V > > : integral_constant< size_t, 2 > {};\ntemplate < class K, class V > struct tuple_element < 0, key_val< K, V > > { using type = K; };\ntemplate < class K, class V > struct tuple_element < 1, key_val< K, V > > { using type = V; };\n\n}\n#line 2 \"cp-library/src/utility/vec_op.hpp\"\ntemplate < class T > key_val< int, T > max_of(const vector< T >& a) {\n int i = std::max_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class T > key_val< int, T > min_of(const vector< T >& a) {\n int i = std::min_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class S, class T > S sum_of(const vector< T >& a) {\n S sum = 0;\n for(const T x : a) sum += x;\n return sum;\n}\ntemplate < class S, class T > vector< S > freq_of(const vector< T >& a, T L, T R) {\n vector< S > res(R - L, S(0));\n for(const T x : a) res[x - L] += 1;\n return res;\n}\ntemplate < class S, class T > struct prefix_sum {\n vector< S > s;\n prefix_sum(const vector< T >& a) : s(a) {\n s.insert(s.begin(), S(0));\n for(int i : rep(a.size())) s[i + 1] += s[i];\n }\n // [L, R)\n S sum(int L, int R) { return s[R] - s[L]; }\n};\n#line 3 \"cp-library/src/utility/heap.hpp\"\n\ntemplate < class T > using heap_min = std::priority_queue< T, std::vector< T >, std::greater< T > >;\ntemplate < class T > using heap_max = std::priority_queue< T, std::vector< T >, std::less< T > >;\n\n#line 27 \"cp-library/src/cp-template.hpp\"\n\n#line 1 \"cp-library/src/algorithm/bin_search.hpp\"\ntemplate < class T, class F >\nT bin_search(T ok, T ng, F f) {\n while(abs(ng - ok) > 1) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n\ntemplate < class T, class F >\nT bin_search_real(T ok, T ng, F f, int step = 80) {\n while(step--) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n#line 2 \"cp-library/src/algorithm/argsort.hpp\"\n\ntemplate < class T > std::vector< int > argsort(const std::vector< T > &a) {\n std::vector< int > ids((int)a.size());\n std::iota(ids.begin(), ids.end(), 0);\n std::sort(ids.begin(), ids.end(), [&](int i, int j) {\n return a[i] < a[j] || (a[i] == a[j] && i < j);\n });\n return ids;\n}\n#line 1 \"macro.hpp\"\nnamespace macro {\n\nusing size_type = int;\ntemplate < class container > void sort(container& a) { std::sort(std:: begin(a), std:: end(a)); }\ntemplate < class container > void rsort(container& a) { std::sort(std::rbegin(a), std::rend(a)); }\ntemplate < class container > void reverse(container& a) { std::reverse(std::begin(a), std::end(a)); }\ntemplate < class container > void unique(container& a) {\n std::sort(std::begin(a), std::end(a));\n a.erase(std::unique(std::begin(a), std::end(a)), std::end(a));\n}\ntemplate < class container > container sorted(const container& a) { container b = a; sort(b); return std::move(b); }\ntemplate < class container > container rsorted(const container& a) { container b = a; rsort(b); return std::move(b); }\ntemplate < class container, class compare > void sort(container& a, const compare& cmp) { std::sort(std::begin(a), std::end(a), cmp); }\ntemplate < class container, class compare > container sorted(const container& a, const compare& cmp) { container b = a; sort(b, cmp); return std::move(b); }\ntemplate < class container, class value > size_type lower_bound(const container& a, const value& x) { return std::lower_bound(std::begin(a), std::end(a), x) - std::begin(a); }\ntemplate < class container, class value > size_type upper_bound(const container& a, const value& x) { return std::upper_bound(std::begin(a), std::end(a), x) - std::begin(a); }\n\nconst std::vector<std::pair<size_type, size_type>> dir4 = { {+1, 0}, {-1, 0}, { 0, +1}, { 0, -1} };\nconst std::vector<std::pair<size_type, size_type>> dir8 = { {-1, -1}, {-1, 0}, {-1, +1}, { 0, -1}, { 0, +1}, {+1, -1}, {+1, 0}, {+1, +1} };\n\n#ifdef _DEBUG\n#define debug(x) std::cout << \"[\" << __LINE__ << \"] \" << #x << \": \" << x << std::endl\n#else\n#define debug(x)\n#endif\n\ntemplate < class container > void concat(container& a, const container& b) {\n a.insert(std::end(a), std::begin(b), std::end(b));\n}\nstd::vector<size_type> iota(const size_type n) {\n std::vector<size_type> I(n);\n std::iota(std::begin(I), std::end(I), 0);\n return I;\n}\ntemplate < class container > std::vector<size_type> sort_idx(const container& a) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return a[i] < a[j] or (a[i] == a[j] and i < j); });\n return I;\n}\ntemplate < class container, class compare > std::vector<size_type> sort_idx(const container& a, const compare& cmp) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return cmp(a[i], a[j]) or (a[i] == a[j] and i < j); });\n return std::move(I);\n}\n\nstruct grid {\n using size_type = int;\n size_type H, W;\n grid(const size_type H, const size_type W) : H(H), W(W) {}\n bool contains(const size_type i, const size_type j) {\n return 0 <= i and i < H and 0 <= j and j < W;\n }\n};\n\n} // namespace macro\n\nusing namespace macro;\n#line 2 \"cp-library/src/number/binom_table.hpp\"\n\ntemplate < class T >\nstd::vector< std::vector< T > > binom_table(int N) {\n std::vector table(N + 1, std::vector(N + 1, T(0)));\n table[0][0] = 1;\n for(int i : rep(1, N + 1)) {\n table[i][0] = 1;\n for(int j : rep(1, N + 1))\n table[i][j] = table[i - 1][j - 1] + table[i - 1][j];\n }\n return table;\n}\n#line 4 \"A.cpp\"\n\nint main() {\n i64 N = in(), M = in(), L = in();\n vector<i64> P(N), T(N), V(N);\n for(int i : rep(N)) P[i] = in(), T[i] = in(), V[i] = in();\n\n auto comb = binom_table<ld>(55);\n\n vector<ld> ans(N, 0.0);\n for(int i : rep(N)) {\n for(int xi = 0; xi <= M; xi++) {\n ld pxi = 1.0;\n for(int j : rep(N)) if(j != i) {\n ld cur = 0.0;\n for(int xj = 0; xj <= M; xj++) {\n i64 lhs = V[i] * V[j] * T[i] * xi + L * V[j];\n i64 rhs = V[i] * V[j] * T[j] * xj + L * V[i];\n if(lhs < rhs) {\n cur += comb[M][xj] * pow(P[j] / 100.0, xj) * pow(1.0 - P[j] / 100.0, M - xj);\n }\n }\n pxi *= cur;\n }\n pxi *= comb[M][xi] * pow(P[i] / 100.0, xi) * pow(1.0 - P[i] / 100.0, M - xi);\n ans[i] += pxi;\n }\n }\n\n printer::precision(20);\n for(int i : rep(N)) print(ans[i]);\n}", "accuracy": 1, "time_ms": 890, "memory_kb": 4032, "score_of_the_acc": -1.2277, "final_rank": 12 }, { "submission_id": "aoj_2303_6763587", "code_snippet": "#include <bits/stdc++.h>\n#include <chrono>\n#include <thread>\n////#include <atcoder/all>\n\n//using namespace atcoder;\nusing namespace std;\nusing ll = long long;\nusing vll = vector<ll>;\nusing vvll = vector<vll>;\nusing vvvll = vector<vvll>;\nusing vb = vector<bool>;\nusing vvb = vector<vb>;\nusing vvvb = vector<vvb>;\nusing vd = vector<double>;\nusing vvd = vector<vd>;\nusing vvvd = vector<vvd>;\n#define all(A) A.begin(),A.end()\n#define ALL(A) A.begin(),A.end()\n#define rep(i, n) for (ll i = 0; i < (ll) (n); i++)\nusing pqr = priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>>;\n\ntemplate<class T>\nbool chmax(T& p, T q, bool C = 1) {\n if (C == 0 && p == q) {\n return 1;\n }\n if (p < q) {\n p = q;\n return 1;\n }\n else {\n return 0;\n }\n}\n\ndouble EPS = 1e-9;\ntemplate<class T>\nbool chmin(T& p, T q) {\n if (p > q + EPS) {\n p = q;\n return 1;\n }\n else {\n return 0;\n }\n}\n\nll gcd(ll(a), ll(b)) {\n if (b == 0)return a;\n ll c = a;\n while (a % b != 0) {\n c = a % b;\n a = b;\n b = c;\n }\n return b;\n}\n\nvector<ll> fact, factinv, inv;\nll mod = 998244353;\nvoid prenCkModp(ll n) {\n fact.resize(n + 5);\n factinv.resize(n + 5);\n inv.resize(n + 5);\n fact.at(0) = fact.at(1) = 1;\n factinv.at(0) = factinv.at(1) = 1;\n inv.at(1) = 1;\n for (ll i = 2; i < n + 5; i++) {\n fact.at(i) = (fact.at(i - 1) * i) % mod;\n inv.at(i) = mod - (inv.at(mod % i) * (mod / i)) % mod;\n factinv.at(i) = (factinv.at(i - 1) * inv.at(i)) % mod;\n }\n\n}\nll nCk(ll n, ll k) {\n if (k < 0)return 0;\n if (n < k) return 0;\n return fact.at(n) * (factinv.at(k) * factinv.at(n - k) % mod) % mod;\n}\nvector<string>S;\nll dfs(ll L, ll R, ll n, char c = '+') {\n ll res = 0;\n if (c == '*')res = 1;\n for (ll i = L; i <= R; i++) {\n ll d = 0;\n if (S[i].size() != n + 1)continue;\n if (S[i][n] == '+') {\n bool OK = 1;\n for (ll j = i + 1; j < R; j++) {\n if (S[j].size() == n + 1) {\n d = dfs(i + 1, j - 1, n + 1, '+');\n OK = 0;\n break;\n }\n\n }\n if (OK)d = dfs(i + 1, R, n + 1, '+');\n }\n else if (S[i][n] == '*') {\n bool OK = 1;\n for (ll j = i + 1; j < R; j++) {\n if (S[j].size() == n + 1) {\n d = dfs(i + 1, j - 1, n + 1, '*');\n OK = 0;\n break;\n }\n\n }\n if (OK)d = dfs(i + 1, R, n + 1, '*');\n }\n else {\n d = S[i][n] - '0';\n }\n if (c == '+')res += d;\n else res *= d;\n }\n return res;\n}\nunordered_map<ll, ll> MA;\n\n\nint main() {\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n\n vvll comb(51,vll(51,0));\n rep(i,51){\n comb[i][0]=1;\n rep(j,50){\n comb[i][j+1]=comb[i][j]*double(i-j)/double(j+1);\n }\n }\n\n ll N,M,L;\n cin>>N>>M>>L;\n vector<tuple<double,ll,ll>> H(N);\n rep(n,N){\n double p;ll t,v;\n cin>>p>>t>>v;\n H[n]={p/100.0,t,v};\n }\n rep(n,N){\n double an=0.0;\n double p=get<0>(H[n]);\n ll t=get<1>(H[n]);\n ll v=get<2>(H[n]);\n if(v<0.5){\n cout<<0.0<<endl;\n continue;\n }\n rep(m,M+1){\n double q=double(comb[M][m])*pow(p,m)*pow(1-p,M-m);\n rep(i,N){\n if(n==i)continue;\n double res=0.0;\n double pi=get<0>(H[i]);\n ll ti=get<1>(H[i]);\n ll vi=get<2>(H[i]);\n rep(j,M+1){\n if(L*v+j*ti*vi*v<=L*vi+m*t*vi*v)continue;\n res+=double(comb[M][j])*pow(pi,j)*pow(1-pi,M-j);\n }\n q*=res;\n }\n an+=q;\n }\n cout<<fixed<<setprecision(16)<<an<<endl;\n }\n}", "accuracy": 1, "time_ms": 860, "memory_kb": 3996, "score_of_the_acc": -1.175, "final_rank": 10 }, { "submission_id": "aoj_2303_6552971", "code_snippet": "#include<bits/stdc++.h>\n#define rep(i,a,...) for(int i = (a)*(strlen(#__VA_ARGS__)!=0);i<(int)(strlen(#__VA_ARGS__)?__VA_ARGS__:(a));++i)\n#define per(i,a,...) for(int i = (strlen(#__VA_ARGS__)?__VA_ARGS__:(a))-1;i>=(int)(strlen(#__VA_ARGS__)?(a):0);--i)\n#define foreach(i, n) for(auto &i:(n))\n#define all(x) (x).begin(), (x).end()\n#define bit(x) (1ll << (x))\n#define lambda(RES_TYPE, ...) (function<RES_TYPE(__VA_ARGS__)>)[&](__VA_ARGS__) -> RES_TYPE\n#define method(FUNC_NAME, RES_TYPE, ...) function<RES_TYPE(__VA_ARGS__)> FUNC_NAME = lambda(RES_TYPE, __VA_ARGS__)\nusing namespace std;\nusing ll = long long;\nusing pii = pair<int,int>;\nusing pll = pair<ll,ll>;\n//const ll MOD = (ll)1e9+7;\nconst ll MOD = 998244353;\nconst int INF = (ll)1e9+7;\nconst ll INFLL = (ll)1e18;\ntemplate<class t>\nusing vvector = vector<vector<t>>;\ntemplate<class t>\nusing vvvector = vector<vector<vector<t>>>;\ntemplate<class t>\nusing priority_queuer = priority_queue<t, vector<t>, greater<t>>;\ntemplate<class t, class u> bool chmax(t &a, u b){if(a<b){a=b;return true;}return false;}\ntemplate<class t, class u> bool chmin(t &a, u b){if(a>b){a=b;return true;}return false;}\n#ifdef DEBUG\n#define debug(x) cout<<\"LINE \"<<__LINE__<<\": \"<<#x<<\" = \"<<x<<endl;\n#else\n#define debug(x) (void)0\n#endif\n\nnamespace templates{\n ll modpow(ll x, ll b,ll mod=MOD){\n ll res = 1;\n while(b){\n if(b&1)res = res * x % mod;\n x = x * x % mod;\n b>>=1;\n }\n return res;\n }\n\n ll modinv(ll x){\n return modpow(x, MOD-2);\n }\n\n bool was_output = false;\n\n void print();\n template <class t> void print(const vector<t> &);\n template <class t, class u> void print(const pair<t, u> &);\n template <class t> void print(const t&);\n template <class Head, class... Tail> void print(const Head&, const Tail&...);\n\n template <class t> void println(const vector<vector<t>>&);\n template <class t> void println(const vector<t>&);\n template <class t> void println(const t&);\n template <class Head, class... Tail> void println(const Head&, const Tail&...);\n void println();\n void newline();\n\n void print(){\n return;\n }\n\n template <class t>\n void print(const vector<t>&x){\n for(const t&i:x)print(i);\n }\n template <class t, class u>\n void print(const pair<t,u>&p){\n print(p.first);\n print(p.second);\n }\n template <class t>\n void print(const t&x){\n if(was_output)cout<<\" \";\n cout<<x;\n was_output = true;\n }\n template <class Head, class... Tail>\n void print(const Head&head,const Tail&...tail){\n print(head);\n print(tail...);\n }\n\n template<class t>\n void println(const vector<vector<t>>&x){\n for(vector<t> i:x)println(i);\n }\n template<class t>\n void println(const vector<t>&x){\n for(const t&i:x)print(i);\n println();\n }\n template <class t>\n void println(const t&x){\n print(x);\n println();\n }\n void println(){\n if(was_output){\n cout << endl;\n was_output = false;\n }\n }\n template <class Head, class... Tail>\n void println(const Head&head,const Tail&...tail){\n print(head);\n println(tail...);\n }\n\n void newline(){\n was_output = true;\n println();\n }\n\n template<class t>\n istream& operator>>(istream&is, vector<t>&x){\n for(auto &i:x)is >> i;\n return is;\n }\n\n template<class t, class u>\n istream& operator>>(istream&is, pair<t, u>&x){\n is >> x.first >> x.second;\n return is;\n }\n\n template<class t>\n ostream& operator<<(ostream&os, vector<t> &v){\n os << \"{\";\n for(t &i:v){\n os << i << \", \";\n }\n os << \"}\";\n return os;\n }\n\n template<class t = long long>\n t in(){\n t res; cin >> res; return res;\n }\n\n template<class t>\n vector<t> sorted(vector<t> line,function<bool(t,t)> comp=[](t a,t b){return a<b;}){\n sort(line.begin(),line.end(),comp);\n return line;\n }\n\n template<class t>\n vector<t> reversed(vector<t> line){\n reverse(line.begin(),line.end());\n return line;\n }\n string reversed(string str){\n reverse(str.begin(),str.end());\n return str;\n }\n\n long long gcd(long long a,long long b){\n while(b){\n a %= b;\n swap(a,b);\n }\n return a;\n }\n\n long long lcm(long long a,long long b){\n return a / gcd(a,b) * b;\n }\n\n class output_initializer{\n public:\n output_initializer(){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << setprecision(20);\n }\n };output_initializer OUTPUT_INITIALIZER_INSTANCE = output_initializer();\n}\n\nusing namespace templates;\n\n\ntemplate<class t>\nclass fraction{\n\tpublic:\n\t\tt numerator;\n\t\tt denominator;\n\n\t\tfraction():numerator(t(0)), denominator(t(1)){}\n\t\tfraction(t a, t b):numerator(a), denominator(b){}\n\t\tfraction(const fraction &a):numerator(a.numerator), denominator(a.denominator){}\n\n\t\tfraction pow(long long k){\n\t\t\tfraction res(1, 1);\n\t\t\tfraction sum(*this);\n\t\t\twhile(k){\n\t\t\t\tif(k&1)res*=sum;\n\t\t\t\tsum*=sum;\n\t\t\t\tk>>=1;\n\t\t\t}\n\t\t\treturn res;\n\t\t}\n\n\t\tfraction& operator+=(fraction a){\n\t\t\tnumerator = numerator * a.denominator + a.numerator * denominator;\n\t\t\tdenominator *= a.denominator;\n\t\t\treturn *this;\n\t\t}\n\n\t\tfraction& operator-=(fraction a){\n\t\t\tnumerator = numerator * a.denominator - a.numerator * denominator;\n\t\t\tdenominator *= a.denominator;\n\t\t\treturn *this;\n\t\t}\n\n\t\tfraction& operator*=(fraction a){\n\t\t\tnumerator *= a.numerator;\n\t\t\tdenominator *= a.denominator;\n\t\t\treturn *this;\n\t\t}\n\n\t\tfraction& operator/=(fraction a){\n\t\t\tnumerator *= a.denominator;\n\t\t\tdenominator *= a.numerator;\n\t\t\treturn *this;\n\t\t}\n\n\t\tfraction operator+(fraction a){return fraction(*this) += a;}\n\t\tfraction operator-(fraction a){return fraction(*this) -= a;}\n\t\tfraction operator*(fraction a){return fraction(*this) *= a;}\n\t\tfraction operator/(fraction a){return fraction(*this) /= a;}\n};\n\ntemplate<class t>\nbool operator<(fraction<t> a,fraction<t> b){\n return a.numerator * b.denominator < b.numerator * a.denominator;\n}\ntemplate<class t>\nbool operator>(fraction<t> a,fraction<t> b){\n return a.numerator * b.denominator > b.numerator * a.denominator;\n}\ntemplate<class t>\nbool operator==(fraction<t> a,fraction<t> b){\n return a.numerator * b.denominator == b.numerator * a.denominator;\n}\n\ndouble combination(int a,int b){\n double res = 1;\n rep(i,b){\n res *= a - i;\n res /= i + 1;\n }\n return res;\n}\n\nvector<long double> func(){\n int n = in();\n int m = in();\n int l = in();\n using F = fraction<ll>;\n using P = pair<F,long double>;\n method(calc,vector<P>,long double p,int v,int t){\n vector<P> res;\n rep(i,m+1){\n res.emplace_back(F(l,v)+F(t*i,1),combination(m,i) * pow(1-p,m-i) * pow(p,i));\n }\n return res;\n };\n vvector<P> line;\n const F inf = F(1000000,1);\n rep(i,n){\n long double p = in() / 100.0;\n int t = in();\n int v = in();\n if(v==0){\n line.emplace_back(vector<P>({{inf,1}}));\n }else{\n line.emplace_back(calc(p,v,t));\n }\n }\n vector<long double> res;\n rep(i,n){\n long double sum = 0;\n foreach(p,line[i]){\n long double add = p.second;\n rep(j,n){\n if(i==j)continue;\n long double psum = 0;\n foreach(k,line[j]){\n if(p.first < k.first)psum += k.second;\n }\n add *= psum;\n }\n sum += add;\n }\n res.emplace_back(sum);\n }\n return res;\n}\n\nint main(){\n foreach(i,func())printf(\"%.20Lf\\n\",i);\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3748, "score_of_the_acc": -0.0921, "final_rank": 6 }, { "submission_id": "aoj_2303_6552965", "code_snippet": "#include<bits/stdc++.h>\n#define rep(i,a,...) for(int i = (a)*(strlen(#__VA_ARGS__)!=0);i<(int)(strlen(#__VA_ARGS__)?__VA_ARGS__:(a));++i)\n#define per(i,a,...) for(int i = (strlen(#__VA_ARGS__)?__VA_ARGS__:(a))-1;i>=(int)(strlen(#__VA_ARGS__)?(a):0);--i)\n#define foreach(i, n) for(auto &i:(n))\n#define all(x) (x).begin(), (x).end()\n#define bit(x) (1ll << (x))\n#define lambda(RES_TYPE, ...) (function<RES_TYPE(__VA_ARGS__)>)[&](__VA_ARGS__) -> RES_TYPE\n#define method(FUNC_NAME, RES_TYPE, ...) function<RES_TYPE(__VA_ARGS__)> FUNC_NAME = lambda(RES_TYPE, __VA_ARGS__)\nusing namespace std;\nusing ll = long long;\nusing pii = pair<int,int>;\nusing pll = pair<ll,ll>;\n//const ll MOD = (ll)1e9+7;\nconst ll MOD = 998244353;\nconst int INF = (ll)1e9+7;\nconst ll INFLL = (ll)1e18;\ntemplate<class t>\nusing vvector = vector<vector<t>>;\ntemplate<class t>\nusing vvvector = vector<vector<vector<t>>>;\ntemplate<class t>\nusing priority_queuer = priority_queue<t, vector<t>, greater<t>>;\ntemplate<class t, class u> bool chmax(t &a, u b){if(a<b){a=b;return true;}return false;}\ntemplate<class t, class u> bool chmin(t &a, u b){if(a>b){a=b;return true;}return false;}\n#ifdef DEBUG\n#define debug(x) cout<<\"LINE \"<<__LINE__<<\": \"<<#x<<\" = \"<<x<<endl;\n#else\n#define debug(x) (void)0\n#endif\n\nnamespace templates{\n ll modpow(ll x, ll b,ll mod=MOD){\n ll res = 1;\n while(b){\n if(b&1)res = res * x % mod;\n x = x * x % mod;\n b>>=1;\n }\n return res;\n }\n\n ll modinv(ll x){\n return modpow(x, MOD-2);\n }\n\n bool was_output = false;\n\n void print();\n template <class t> void print(const vector<t> &);\n template <class t, class u> void print(const pair<t, u> &);\n template <class t> void print(const t&);\n template <class Head, class... Tail> void print(const Head&, const Tail&...);\n\n template <class t> void println(const vector<vector<t>>&);\n template <class t> void println(const vector<t>&);\n template <class t> void println(const t&);\n template <class Head, class... Tail> void println(const Head&, const Tail&...);\n void println();\n void newline();\n\n void print(){\n return;\n }\n\n template <class t>\n void print(const vector<t>&x){\n for(const t&i:x)print(i);\n }\n template <class t, class u>\n void print(const pair<t,u>&p){\n print(p.first);\n print(p.second);\n }\n template <class t>\n void print(const t&x){\n if(was_output)cout<<\" \";\n cout<<x;\n was_output = true;\n }\n template <class Head, class... Tail>\n void print(const Head&head,const Tail&...tail){\n print(head);\n print(tail...);\n }\n\n template<class t>\n void println(const vector<vector<t>>&x){\n for(vector<t> i:x)println(i);\n }\n template<class t>\n void println(const vector<t>&x){\n for(const t&i:x)print(i);\n println();\n }\n template <class t>\n void println(const t&x){\n print(x);\n println();\n }\n void println(){\n if(was_output){\n cout << endl;\n was_output = false;\n }\n }\n template <class Head, class... Tail>\n void println(const Head&head,const Tail&...tail){\n print(head);\n println(tail...);\n }\n\n void newline(){\n was_output = true;\n println();\n }\n\n template<class t>\n istream& operator>>(istream&is, vector<t>&x){\n for(auto &i:x)is >> i;\n return is;\n }\n\n template<class t, class u>\n istream& operator>>(istream&is, pair<t, u>&x){\n is >> x.first >> x.second;\n return is;\n }\n\n template<class t>\n ostream& operator<<(ostream&os, vector<t> &v){\n os << \"{\";\n for(t &i:v){\n os << i << \", \";\n }\n os << \"}\";\n return os;\n }\n\n template<class t = long long>\n t in(){\n t res; cin >> res; return res;\n }\n\n template<class t>\n vector<t> sorted(vector<t> line,function<bool(t,t)> comp=[](t a,t b){return a<b;}){\n sort(line.begin(),line.end(),comp);\n return line;\n }\n\n template<class t>\n vector<t> reversed(vector<t> line){\n reverse(line.begin(),line.end());\n return line;\n }\n string reversed(string str){\n reverse(str.begin(),str.end());\n return str;\n }\n\n long long gcd(long long a,long long b){\n while(b){\n a %= b;\n swap(a,b);\n }\n return a;\n }\n\n long long lcm(long long a,long long b){\n return a / gcd(a,b) * b;\n }\n\n class output_initializer{\n public:\n output_initializer(){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << setprecision(20);\n }\n };output_initializer OUTPUT_INITIALIZER_INSTANCE = output_initializer();\n}\n\nusing namespace templates;\n\n\ntemplate<class t>\nclass fraction{\n\tpublic:\n\t\tt numerator;\n\t\tt denominator;\n\n\t\tfraction():numerator(t(0)), denominator(t(1)){}\n\t\tfraction(t a, t b):numerator(a), denominator(b){}\n\t\tfraction(const fraction &a):numerator(a.numerator), denominator(a.denominator){}\n\n\t\tfraction pow(long long k){\n\t\t\tfraction res(1, 1);\n\t\t\tfraction sum(*this);\n\t\t\twhile(k){\n\t\t\t\tif(k&1)res*=sum;\n\t\t\t\tsum*=sum;\n\t\t\t\tk>>=1;\n\t\t\t}\n\t\t\treturn res;\n\t\t}\n\n\t\tfraction& operator+=(fraction a){\n\t\t\tnumerator = numerator * a.denominator + a.numerator * denominator;\n\t\t\tdenominator *= a.denominator;\n\t\t\treturn *this;\n\t\t}\n\n\t\tfraction& operator-=(fraction a){\n\t\t\tnumerator = numerator * a.denominator - a.numerator * denominator;\n\t\t\tdenominator *= a.denominator;\n\t\t\treturn *this;\n\t\t}\n\n\t\tfraction& operator*=(fraction a){\n\t\t\tnumerator *= a.numerator;\n\t\t\tdenominator *= a.denominator;\n\t\t\treturn *this;\n\t\t}\n\n\t\tfraction& operator/=(fraction a){\n\t\t\tnumerator *= a.denominator;\n\t\t\tdenominator *= a.numerator;\n\t\t\treturn *this;\n\t\t}\n\n\t\tfraction operator+(fraction a){return fraction(*this) += a;}\n\t\tfraction operator-(fraction a){return fraction(*this) -= a;}\n\t\tfraction operator*(fraction a){return fraction(*this) *= a;}\n\t\tfraction operator/(fraction a){return fraction(*this) /= a;}\n};\n\ntemplate<class t>\nbool operator<(fraction<t> a,fraction<t> b){\n return a.numerator * b.denominator < b.numerator * a.denominator;\n}\ntemplate<class t>\nbool operator>(fraction<t> a,fraction<t> b){\n return a.numerator * b.denominator > b.numerator * a.denominator;\n}\ntemplate<class t>\nbool operator==(fraction<t> a,fraction<t> b){\n return a.numerator * b.denominator == b.numerator * a.denominator;\n}\n\ndouble combination(int a,int b){\n double res = 1;\n rep(i,b){\n res *= a - i;\n res /= i + 1;\n }\n return res;\n}\n\nvector<long double> func(){\n int n = in();\n int m = in();\n int l = in();\n using F = fraction<ll>;\n using P = pair<F,long double>;\n method(calc,vector<P>,long double p,int v,int t){\n vector<P> res;\n rep(i,m+1){\n res.emplace_back(F(l,v)+F(t*i,1),combination(m,i) * pow(1-p,m-i) * pow(p,i));\n }\n return res;\n };\n vvector<P> line;\n const F inf = F(1000000,1);\n rep(i,n){\n long double p = in() / 100.0;\n int t = in();\n int v = in();\n if(v==0){\n line.emplace_back(vector<P>({{inf,1}}));\n }else{\n line.emplace_back(calc(p,v,t));\n }\n }\n vector<long double> res;\n rep(i,n){\n long double sum = 0;\n foreach(p,line[i]){\n long double add = p.second;\n rep(j,n){\n if(i==j)continue;\n long double psum = 0;\n foreach(k,line[j]){\n if(p.first < k.first)psum += k.second;\n }\n add *= psum;\n }\n sum += add;\n }\n res.emplace_back(sum);\n }\n return res;\n}\n\nint main(){\n foreach(i,func())println(i);\n return 0;\n}", "accuracy": 0.14285714285714285, "time_ms": 20, "memory_kb": 3716, "score_of_the_acc": -0.0755, "final_rank": 16 }, { "submission_id": "aoj_2303_6552932", "code_snippet": "#include<bits/stdc++.h>\n#define rep(i,a,...) for(int i = (a)*(strlen(#__VA_ARGS__)!=0);i<(int)(strlen(#__VA_ARGS__)?__VA_ARGS__:(a));++i)\n#define per(i,a,...) for(int i = (strlen(#__VA_ARGS__)?__VA_ARGS__:(a))-1;i>=(int)(strlen(#__VA_ARGS__)?(a):0);--i)\n#define foreach(i, n) for(auto &i:(n))\n#define all(x) (x).begin(), (x).end()\n#define bit(x) (1ll << (x))\n#define lambda(RES_TYPE, ...) (function<RES_TYPE(__VA_ARGS__)>)[&](__VA_ARGS__) -> RES_TYPE\n#define method(FUNC_NAME, RES_TYPE, ...) function<RES_TYPE(__VA_ARGS__)> FUNC_NAME = lambda(RES_TYPE, __VA_ARGS__)\nusing namespace std;\nusing ll = long long;\nusing pii = pair<int,int>;\nusing pll = pair<ll,ll>;\n//const ll MOD = (ll)1e9+7;\nconst ll MOD = 998244353;\nconst int INF = (ll)1e9+7;\nconst ll INFLL = (ll)1e18;\ntemplate<class t>\nusing vvector = vector<vector<t>>;\ntemplate<class t>\nusing vvvector = vector<vector<vector<t>>>;\ntemplate<class t>\nusing priority_queuer = priority_queue<t, vector<t>, greater<t>>;\ntemplate<class t, class u> bool chmax(t &a, u b){if(a<b){a=b;return true;}return false;}\ntemplate<class t, class u> bool chmin(t &a, u b){if(a>b){a=b;return true;}return false;}\n#ifdef DEBUG\n#define debug(x) cout<<\"LINE \"<<__LINE__<<\": \"<<#x<<\" = \"<<x<<endl;\n#else\n#define debug(x) (void)0\n#endif\n\nnamespace templates{\n ll modpow(ll x, ll b,ll mod=MOD){\n ll res = 1;\n while(b){\n if(b&1)res = res * x % mod;\n x = x * x % mod;\n b>>=1;\n }\n return res;\n }\n\n ll modinv(ll x){\n return modpow(x, MOD-2);\n }\n\n bool was_output = false;\n\n void print();\n template <class t> void print(const vector<t> &);\n template <class t, class u> void print(const pair<t, u> &);\n template <class t> void print(const t&);\n template <class Head, class... Tail> void print(const Head&, const Tail&...);\n\n template <class t> void println(const vector<vector<t>>&);\n template <class t> void println(const vector<t>&);\n template <class t> void println(const t&);\n template <class Head, class... Tail> void println(const Head&, const Tail&...);\n void println();\n void newline();\n\n void print(){\n return;\n }\n\n template <class t>\n void print(const vector<t>&x){\n for(const t&i:x)print(i);\n }\n template <class t, class u>\n void print(const pair<t,u>&p){\n print(p.first);\n print(p.second);\n }\n template <class t>\n void print(const t&x){\n if(was_output)cout<<\" \";\n cout<<x;\n was_output = true;\n }\n template <class Head, class... Tail>\n void print(const Head&head,const Tail&...tail){\n print(head);\n print(tail...);\n }\n\n template<class t>\n void println(const vector<vector<t>>&x){\n for(vector<t> i:x)println(i);\n }\n template<class t>\n void println(const vector<t>&x){\n for(const t&i:x)print(i);\n println();\n }\n template <class t>\n void println(const t&x){\n print(x);\n println();\n }\n void println(){\n if(was_output){\n cout << endl;\n was_output = false;\n }\n }\n template <class Head, class... Tail>\n void println(const Head&head,const Tail&...tail){\n print(head);\n println(tail...);\n }\n\n void newline(){\n was_output = true;\n println();\n }\n\n template<class t>\n istream& operator>>(istream&is, vector<t>&x){\n for(auto &i:x)is >> i;\n return is;\n }\n\n template<class t, class u>\n istream& operator>>(istream&is, pair<t, u>&x){\n is >> x.first >> x.second;\n return is;\n }\n\n template<class t>\n ostream& operator<<(ostream&os, vector<t> &v){\n os << \"{\";\n for(t &i:v){\n os << i << \", \";\n }\n os << \"}\";\n return os;\n }\n\n template<class t = long long>\n t in(){\n t res; cin >> res; return res;\n }\n\n template<class t>\n vector<t> sorted(vector<t> line,function<bool(t,t)> comp=[](t a,t b){return a<b;}){\n sort(line.begin(),line.end(),comp);\n return line;\n }\n\n template<class t>\n vector<t> reversed(vector<t> line){\n reverse(line.begin(),line.end());\n return line;\n }\n string reversed(string str){\n reverse(str.begin(),str.end());\n return str;\n }\n\n long long gcd(long long a,long long b){\n while(b){\n a %= b;\n swap(a,b);\n }\n return a;\n }\n\n long long lcm(long long a,long long b){\n return a / gcd(a,b) * b;\n }\n\n class output_initializer{\n public:\n output_initializer(){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << setprecision(20);\n }\n };output_initializer OUTPUT_INITIALIZER_INSTANCE = output_initializer();\n}\n\nusing namespace templates;\n\n\ntemplate<class t>\nclass fraction{\n\tpublic:\n\t\tt numerator;\n\t\tt denominator;\n\n\t\tfraction():numerator(t(0)), denominator(t(1)){}\n\t\tfraction(t a, t b):numerator(a), denominator(b){}\n\t\tfraction(const fraction &a):numerator(a.numerator), denominator(a.denominator){}\n\n\t\tfraction pow(long long k){\n\t\t\tfraction res(1, 1);\n\t\t\tfraction sum(*this);\n\t\t\twhile(k){\n\t\t\t\tif(k&1)res*=sum;\n\t\t\t\tsum*=sum;\n\t\t\t\tk>>=1;\n\t\t\t}\n\t\t\treturn res;\n\t\t}\n\n\t\tfraction& operator+=(fraction a){\n\t\t\tnumerator = numerator * a.denominator + a.numerator * denominator;\n\t\t\tdenominator *= a.denominator;\n\t\t\treturn *this;\n\t\t}\n\n\t\tfraction& operator-=(fraction a){\n\t\t\tnumerator = numerator * a.denominator - a.numerator * denominator;\n\t\t\tdenominator *= a.denominator;\n\t\t\treturn *this;\n\t\t}\n\n\t\tfraction& operator*=(fraction a){\n\t\t\tnumerator *= a.numerator;\n\t\t\tdenominator *= a.denominator;\n\t\t\treturn *this;\n\t\t}\n\n\t\tfraction& operator/=(fraction a){\n\t\t\tnumerator *= a.denominator;\n\t\t\tdenominator *= a.numerator;\n\t\t\treturn *this;\n\t\t}\n\n\t\tfraction operator+(fraction a){return fraction(*this) += a;}\n\t\tfraction operator-(fraction a){return fraction(*this) -= a;}\n\t\tfraction operator*(fraction a){return fraction(*this) *= a;}\n\t\tfraction operator/(fraction a){return fraction(*this) /= a;}\n};\n\ntemplate<class t>\nbool operator<(fraction<t> a,fraction<t> b){\n return a.numerator * b.denominator < b.numerator * a.denominator;\n}\ntemplate<class t>\nbool operator>(fraction<t> a,fraction<t> b){\n return a.numerator * b.denominator > b.numerator * a.denominator;\n}\ntemplate<class t>\nbool operator==(fraction<t> a,fraction<t> b){\n return a.numerator * b.denominator == b.numerator * a.denominator;\n}\n\nvector<long double> func(){\n int n = in();\n int m = in();\n int l = in();\n using F = fraction<ll>;\n using P = pair<F,long double>;\n method(calc,vector<P>,long double p,int v,int t){\n vector<long double> dp(m+1,0);\n dp[0] = 1;\n rep(_,m){\n vector<long double> ndp(m+1,0);\n rep(i,m+1){\n ndp[i] += (1-p) * dp[i];\n if(i+1<dp.size()){\n ndp[i+1] += p * dp[i];\n }\n }\n swap(dp,ndp);\n }\n vector<P> res;\n rep(i,dp.size()){\n res.emplace_back(F(l,v)+F(t*i,1),dp[i]);\n }\n return res;\n };\n vvector<P> line;\n const F inf = F(1000000,1);\n rep(i,n){\n long double p = in() / 100.0;\n int t = in();\n int v = in();\n if(v==0){\n line.emplace_back(vector<P>({{inf,1}}));\n }else{\n line.emplace_back(calc(p,v,t));\n }\n }\n vector<long double> res;\n rep(i,n){\n long double sum = 0;\n foreach(p,line[i]){\n long double add = p.second;\n rep(j,n){\n if(i==j)continue;\n long double psum = 0;\n foreach(k,line[j]){\n if(p.first < k.first)psum += k.second;\n }\n add *= psum;\n }\n sum += add;\n }\n res.emplace_back(sum);\n }\n return res;\n}\n\nint main(){\n foreach(i,func())println(i);\n return 0;\n}", "accuracy": 0.14285714285714285, "time_ms": 20, "memory_kb": 3676, "score_of_the_acc": -0.0548, "final_rank": 14 }, { "submission_id": "aoj_2303_6552930", "code_snippet": "#include<bits/stdc++.h>\n#define rep(i,a,...) for(int i = (a)*(strlen(#__VA_ARGS__)!=0);i<(int)(strlen(#__VA_ARGS__)?__VA_ARGS__:(a));++i)\n#define per(i,a,...) for(int i = (strlen(#__VA_ARGS__)?__VA_ARGS__:(a))-1;i>=(int)(strlen(#__VA_ARGS__)?(a):0);--i)\n#define foreach(i, n) for(auto &i:(n))\n#define all(x) (x).begin(), (x).end()\n#define bit(x) (1ll << (x))\n#define lambda(RES_TYPE, ...) (function<RES_TYPE(__VA_ARGS__)>)[&](__VA_ARGS__) -> RES_TYPE\n#define method(FUNC_NAME, RES_TYPE, ...) function<RES_TYPE(__VA_ARGS__)> FUNC_NAME = lambda(RES_TYPE, __VA_ARGS__)\nusing namespace std;\nusing ll = long long;\nusing pii = pair<int,int>;\nusing pll = pair<ll,ll>;\n//const ll MOD = (ll)1e9+7;\nconst ll MOD = 998244353;\nconst int INF = (ll)1e9+7;\nconst ll INFLL = (ll)1e18;\ntemplate<class t>\nusing vvector = vector<vector<t>>;\ntemplate<class t>\nusing vvvector = vector<vector<vector<t>>>;\ntemplate<class t>\nusing priority_queuer = priority_queue<t, vector<t>, greater<t>>;\ntemplate<class t, class u> bool chmax(t &a, u b){if(a<b){a=b;return true;}return false;}\ntemplate<class t, class u> bool chmin(t &a, u b){if(a>b){a=b;return true;}return false;}\n#ifdef DEBUG\n#define debug(x) cout<<\"LINE \"<<__LINE__<<\": \"<<#x<<\" = \"<<x<<endl;\n#else\n#define debug(x) (void)0\n#endif\n\nnamespace templates{\n ll modpow(ll x, ll b,ll mod=MOD){\n ll res = 1;\n while(b){\n if(b&1)res = res * x % mod;\n x = x * x % mod;\n b>>=1;\n }\n return res;\n }\n\n ll modinv(ll x){\n return modpow(x, MOD-2);\n }\n\n bool was_output = false;\n\n void print();\n template <class t> void print(const vector<t> &);\n template <class t, class u> void print(const pair<t, u> &);\n template <class t> void print(const t&);\n template <class Head, class... Tail> void print(const Head&, const Tail&...);\n\n template <class t> void println(const vector<vector<t>>&);\n template <class t> void println(const vector<t>&);\n template <class t> void println(const t&);\n template <class Head, class... Tail> void println(const Head&, const Tail&...);\n void println();\n void newline();\n\n void print(){\n return;\n }\n\n template <class t>\n void print(const vector<t>&x){\n for(const t&i:x)print(i);\n }\n template <class t, class u>\n void print(const pair<t,u>&p){\n print(p.first);\n print(p.second);\n }\n template <class t>\n void print(const t&x){\n if(was_output)cout<<\" \";\n cout<<x;\n was_output = true;\n }\n template <class Head, class... Tail>\n void print(const Head&head,const Tail&...tail){\n print(head);\n print(tail...);\n }\n\n template<class t>\n void println(const vector<vector<t>>&x){\n for(vector<t> i:x)println(i);\n }\n template<class t>\n void println(const vector<t>&x){\n for(const t&i:x)print(i);\n println();\n }\n template <class t>\n void println(const t&x){\n print(x);\n println();\n }\n void println(){\n if(was_output){\n cout << endl;\n was_output = false;\n }\n }\n template <class Head, class... Tail>\n void println(const Head&head,const Tail&...tail){\n print(head);\n println(tail...);\n }\n\n void newline(){\n was_output = true;\n println();\n }\n\n template<class t>\n istream& operator>>(istream&is, vector<t>&x){\n for(auto &i:x)is >> i;\n return is;\n }\n\n template<class t, class u>\n istream& operator>>(istream&is, pair<t, u>&x){\n is >> x.first >> x.second;\n return is;\n }\n\n template<class t>\n ostream& operator<<(ostream&os, vector<t> &v){\n os << \"{\";\n for(t &i:v){\n os << i << \", \";\n }\n os << \"}\";\n return os;\n }\n\n template<class t = long long>\n t in(){\n t res; cin >> res; return res;\n }\n\n template<class t>\n vector<t> sorted(vector<t> line,function<bool(t,t)> comp=[](t a,t b){return a<b;}){\n sort(line.begin(),line.end(),comp);\n return line;\n }\n\n template<class t>\n vector<t> reversed(vector<t> line){\n reverse(line.begin(),line.end());\n return line;\n }\n string reversed(string str){\n reverse(str.begin(),str.end());\n return str;\n }\n\n long long gcd(long long a,long long b){\n while(b){\n a %= b;\n swap(a,b);\n }\n return a;\n }\n\n long long lcm(long long a,long long b){\n return a / gcd(a,b) * b;\n }\n\n class output_initializer{\n public:\n output_initializer(){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << setprecision(20);\n }\n };output_initializer OUTPUT_INITIALIZER_INSTANCE = output_initializer();\n}\n\nusing namespace templates;\n\n\ntemplate<class t>\nclass fraction{\n\tpublic:\n\t\tt numerator;\n\t\tt denominator;\n\n\t\tfraction():numerator(t(0)), denominator(t(1)){}\n\t\tfraction(t a, t b):numerator(a), denominator(b){}\n\t\tfraction(const fraction &a):numerator(a.numerator), denominator(a.denominator){}\n\n\t\tfraction pow(long long k){\n\t\t\tfraction res(1, 1);\n\t\t\tfraction sum(*this);\n\t\t\twhile(k){\n\t\t\t\tif(k&1)res*=sum;\n\t\t\t\tsum*=sum;\n\t\t\t\tk>>=1;\n\t\t\t}\n\t\t\treturn res;\n\t\t}\n\n\t\tfraction& operator+=(fraction a){\n\t\t\tnumerator = numerator * a.denominator + a.numerator * denominator;\n\t\t\tdenominator *= a.denominator;\n\t\t\treturn *this;\n\t\t}\n\n\t\tfraction& operator-=(fraction a){\n\t\t\tnumerator = numerator * a.denominator - a.numerator * denominator;\n\t\t\tdenominator *= a.denominator;\n\t\t\treturn *this;\n\t\t}\n\n\t\tfraction& operator*=(fraction a){\n\t\t\tnumerator *= a.numerator;\n\t\t\tdenominator *= a.denominator;\n\t\t\treturn *this;\n\t\t}\n\n\t\tfraction& operator/=(fraction a){\n\t\t\tnumerator *= a.denominator;\n\t\t\tdenominator *= a.numerator;\n\t\t\treturn *this;\n\t\t}\n\n\t\tfraction operator+(fraction a){return fraction(*this) += a;}\n\t\tfraction operator-(fraction a){return fraction(*this) -= a;}\n\t\tfraction operator*(fraction a){return fraction(*this) *= a;}\n\t\tfraction operator/(fraction a){return fraction(*this) /= a;}\n};\n\ntemplate<class t>\nbool operator<(fraction<t> a,fraction<t> b){\n return a.numerator * b.denominator < b.numerator * a.denominator;\n}\ntemplate<class t>\nbool operator>(fraction<t> a,fraction<t> b){\n return a.numerator * b.denominator > b.numerator * a.denominator;\n}\ntemplate<class t>\nbool operator==(fraction<t> a,fraction<t> b){\n return a.numerator * b.denominator == b.numerator * a.denominator;\n}\n\nvector<long double> func(){\n int n = in();\n int m = in();\n int l = in();\n using F = fraction<ll>;\n using P = pair<F,long double>;\n method(calc,vector<P>,long double p,int v,int t){\n vector<long double> dp(m+1,0);\n dp[0] = 1;\n rep(_,m){\n vector<long double> ndp(m+1,0);\n rep(i,m+1){\n ndp[i] += (1-p) * dp[i];\n if(i+1<dp.size()){\n ndp[i+1] += p * dp[i];\n }\n }\n swap(dp,ndp);\n }\n vector<P> res;\n rep(i,dp.size()){\n res.emplace_back(F(l,v)+F(t*i,1),dp[i]);\n }\n return res;\n };\n vvector<P> line;\n const F inf = F(1000000,1);\n rep(i,n){\n long double p = in() / 100.0;\n int t = in();\n int v = in();\n if(v==0){\n line.emplace_back(vector<P>({{inf,1}}));\n }else{\n line.emplace_back(calc(p,v,t));\n }\n }\n vector<long double> res;\n rep(i,n){\n long double sum = 0;\n foreach(p,line[i]){\n long double add = p.second;\n if(p.first==inf)continue;\n rep(j,n){\n if(i==j)continue;\n long double psum = 0;\n foreach(k,line[j]){\n if(p.first < k.first)psum += k.second;\n }\n add *= psum;\n }\n sum += add;\n }\n res.emplace_back(sum);\n }\n return res;\n}\n\nint main(){\n foreach(i,func())println(i);\n return 0;\n}", "accuracy": 0.14285714285714285, "time_ms": 20, "memory_kb": 3688, "score_of_the_acc": -0.0611, "final_rank": 15 }, { "submission_id": "aoj_2303_6552908", "code_snippet": "#include<bits/stdc++.h>\n#define rep(i,a,...) for(int i = (a)*(strlen(#__VA_ARGS__)!=0);i<(int)(strlen(#__VA_ARGS__)?__VA_ARGS__:(a));++i)\n#define per(i,a,...) for(int i = (strlen(#__VA_ARGS__)?__VA_ARGS__:(a))-1;i>=(int)(strlen(#__VA_ARGS__)?(a):0);--i)\n#define foreach(i, n) for(auto &i:(n))\n#define all(x) (x).begin(), (x).end()\n#define bit(x) (1ll << (x))\n#define lambda(RES_TYPE, ...) (function<RES_TYPE(__VA_ARGS__)>)[&](__VA_ARGS__) -> RES_TYPE\n#define method(FUNC_NAME, RES_TYPE, ...) function<RES_TYPE(__VA_ARGS__)> FUNC_NAME = lambda(RES_TYPE, __VA_ARGS__)\nusing namespace std;\nusing ll = long long;\nusing pii = pair<int,int>;\nusing pll = pair<ll,ll>;\n//const ll MOD = (ll)1e9+7;\nconst ll MOD = 998244353;\nconst int INF = (ll)1e9+7;\nconst ll INFLL = (ll)1e18;\ntemplate<class t>\nusing vvector = vector<vector<t>>;\ntemplate<class t>\nusing vvvector = vector<vector<vector<t>>>;\ntemplate<class t>\nusing priority_queuer = priority_queue<t, vector<t>, greater<t>>;\ntemplate<class t, class u> bool chmax(t &a, u b){if(a<b){a=b;return true;}return false;}\ntemplate<class t, class u> bool chmin(t &a, u b){if(a>b){a=b;return true;}return false;}\n#ifdef DEBUG\n#define debug(x) cout<<\"LINE \"<<__LINE__<<\": \"<<#x<<\" = \"<<x<<endl;\n#else\n#define debug(x) (void)0\n#endif\n\nnamespace templates{\n ll modpow(ll x, ll b,ll mod=MOD){\n ll res = 1;\n while(b){\n if(b&1)res = res * x % mod;\n x = x * x % mod;\n b>>=1;\n }\n return res;\n }\n\n ll modinv(ll x){\n return modpow(x, MOD-2);\n }\n\n bool was_output = false;\n\n void print();\n template <class t> void print(const vector<t> &);\n template <class t, class u> void print(const pair<t, u> &);\n template <class t> void print(const t&);\n template <class Head, class... Tail> void print(const Head&, const Tail&...);\n\n template <class t> void println(const vector<vector<t>>&);\n template <class t> void println(const vector<t>&);\n template <class t> void println(const t&);\n template <class Head, class... Tail> void println(const Head&, const Tail&...);\n void println();\n void newline();\n\n void print(){\n return;\n }\n\n template <class t>\n void print(const vector<t>&x){\n for(const t&i:x)print(i);\n }\n template <class t, class u>\n void print(const pair<t,u>&p){\n print(p.first);\n print(p.second);\n }\n template <class t>\n void print(const t&x){\n if(was_output)cout<<\" \";\n cout<<x;\n was_output = true;\n }\n template <class Head, class... Tail>\n void print(const Head&head,const Tail&...tail){\n print(head);\n print(tail...);\n }\n\n template<class t>\n void println(const vector<vector<t>>&x){\n for(vector<t> i:x)println(i);\n }\n template<class t>\n void println(const vector<t>&x){\n for(const t&i:x)print(i);\n println();\n }\n template <class t>\n void println(const t&x){\n print(x);\n println();\n }\n void println(){\n if(was_output){\n cout << endl;\n was_output = false;\n }\n }\n template <class Head, class... Tail>\n void println(const Head&head,const Tail&...tail){\n print(head);\n println(tail...);\n }\n\n void newline(){\n was_output = true;\n println();\n }\n\n template<class t>\n istream& operator>>(istream&is, vector<t>&x){\n for(auto &i:x)is >> i;\n return is;\n }\n\n template<class t, class u>\n istream& operator>>(istream&is, pair<t, u>&x){\n is >> x.first >> x.second;\n return is;\n }\n\n template<class t>\n ostream& operator<<(ostream&os, vector<t> &v){\n os << \"{\";\n for(t &i:v){\n os << i << \", \";\n }\n os << \"}\";\n return os;\n }\n\n template<class t = long long>\n t in(){\n t res; cin >> res; return res;\n }\n\n template<class t>\n vector<t> sorted(vector<t> line,function<bool(t,t)> comp=[](t a,t b){return a<b;}){\n sort(line.begin(),line.end(),comp);\n return line;\n }\n\n template<class t>\n vector<t> reversed(vector<t> line){\n reverse(line.begin(),line.end());\n return line;\n }\n string reversed(string str){\n reverse(str.begin(),str.end());\n return str;\n }\n\n long long gcd(long long a,long long b){\n while(b){\n a %= b;\n swap(a,b);\n }\n return a;\n }\n\n long long lcm(long long a,long long b){\n return a / gcd(a,b) * b;\n }\n\n class output_initializer{\n public:\n output_initializer(){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << setprecision(20);\n }\n };output_initializer OUTPUT_INITIALIZER_INSTANCE = output_initializer();\n}\n\nusing namespace templates;\n\n\ntemplate<class t>\nclass fraction{\n\tpublic:\n\t\tt numerator;\n\t\tt denominator;\n\n\t\tfraction():numerator(t(0)), denominator(t(1)){}\n\t\tfraction(t a, t b):numerator(a), denominator(b){}\n\t\tfraction(const fraction &a):numerator(a.numerator), denominator(a.denominator){}\n\n\t\tfraction pow(long long k){\n\t\t\tfraction res(1, 1);\n\t\t\tfraction sum(*this);\n\t\t\twhile(k){\n\t\t\t\tif(k&1)res*=sum;\n\t\t\t\tsum*=sum;\n\t\t\t\tk>>=1;\n\t\t\t}\n\t\t\treturn res;\n\t\t}\n\n\t\tfraction& operator+=(fraction a){\n\t\t\tnumerator = numerator * a.denominator + a.numerator * denominator;\n\t\t\tdenominator *= a.denominator;\n\t\t\treturn *this;\n\t\t}\n\n\t\tfraction& operator-=(fraction a){\n\t\t\tnumerator = numerator * a.denominator - a.numerator * denominator;\n\t\t\tdenominator *= a.denominator;\n\t\t\treturn *this;\n\t\t}\n\n\t\tfraction& operator*=(fraction a){\n\t\t\tnumerator *= a.numerator;\n\t\t\tdenominator *= a.denominator;\n\t\t\treturn *this;\n\t\t}\n\n\t\tfraction& operator/=(fraction a){\n\t\t\tnumerator *= a.denominator;\n\t\t\tdenominator *= a.numerator;\n\t\t\treturn *this;\n\t\t}\n\n\t\tfraction operator+(fraction a){return fraction(*this) += a;}\n\t\tfraction operator-(fraction a){return fraction(*this) -= a;}\n\t\tfraction operator*(fraction a){return fraction(*this) *= a;}\n\t\tfraction operator/(fraction a){return fraction(*this) /= a;}\n};\n\ntemplate<class t>\nbool operator<(fraction<t> a,fraction<t> b){\n return a.numerator * b.denominator < b.numerator * a.denominator;\n}\ntemplate<class t>\nbool operator>(fraction<t> a,fraction<t> b){\n return a.numerator * b.denominator > b.numerator * a.denominator;\n}\ntemplate<class t>\nbool operator==(fraction<t> a,fraction<t> b){\n return a.numerator * b.denominator == b.numerator * a.denominator;\n}\n\nvector<long double> func(){\n int n = in();\n int m = in();\n int l = in();\n using F = fraction<ll>;\n using P = pair<F,long double>;\n method(calc,vector<P>,long double p,int v,int t){\n vector<long double> dp(m+1,0);\n dp[0] = 1;\n rep(_,m){\n vector<long double> ndp(m+1,0);\n rep(i,m){\n ndp[i] += (1-p) * dp[i];\n ndp[i+1] += p * dp[i];\n }\n swap(dp,ndp);\n }\n vector<P> res;\n rep(i,dp.size()){\n res.emplace_back(F(l,v)+F(t*i,1),dp[i]);\n }\n return res;\n };\n vvector<P> line;\n const F inf = F(1000000,1);\n rep(i,n){\n long double p = in() / 100.0;\n int t = in();\n int v = in();\n if(v==0){\n line.emplace_back(vector<P>({{inf,1}}));\n }else{\n line.emplace_back(calc(p,v,t));\n }\n }\n vector<long double> res;\n rep(i,n){\n long double sum = 0;\n foreach(p,line[i]){\n long double add = p.second;\n if(p.first==inf)continue;\n rep(j,n){\n if(i==j)continue;\n long double psum = 0;\n foreach(k,line[j]){\n if(p.first < k.first)psum += k.second;\n }\n add *= psum;\n }\n sum += add;\n }\n res.emplace_back(sum);\n }\n return res;\n}\n\nint main(){\n foreach(i,func())println(i);\n return 0;\n}", "accuracy": 0.14285714285714285, "time_ms": 20, "memory_kb": 3764, "score_of_the_acc": -0.1004, "final_rank": 20 }, { "submission_id": "aoj_2303_6552894", "code_snippet": "#include<bits/stdc++.h>\n#define rep(i,a,...) for(int i = (a)*(strlen(#__VA_ARGS__)!=0);i<(int)(strlen(#__VA_ARGS__)?__VA_ARGS__:(a));++i)\n#define per(i,a,...) for(int i = (strlen(#__VA_ARGS__)?__VA_ARGS__:(a))-1;i>=(int)(strlen(#__VA_ARGS__)?(a):0);--i)\n#define foreach(i, n) for(auto &i:(n))\n#define all(x) (x).begin(), (x).end()\n#define bit(x) (1ll << (x))\n#define lambda(RES_TYPE, ...) (function<RES_TYPE(__VA_ARGS__)>)[&](__VA_ARGS__) -> RES_TYPE\n#define method(FUNC_NAME, RES_TYPE, ...) function<RES_TYPE(__VA_ARGS__)> FUNC_NAME = lambda(RES_TYPE, __VA_ARGS__)\nusing namespace std;\nusing ll = long long;\nusing pii = pair<int,int>;\nusing pll = pair<ll,ll>;\n//const ll MOD = (ll)1e9+7;\nconst ll MOD = 998244353;\nconst int INF = (ll)1e9+7;\nconst ll INFLL = (ll)1e18;\ntemplate<class t>\nusing vvector = vector<vector<t>>;\ntemplate<class t>\nusing vvvector = vector<vector<vector<t>>>;\ntemplate<class t>\nusing priority_queuer = priority_queue<t, vector<t>, greater<t>>;\ntemplate<class t, class u> bool chmax(t &a, u b){if(a<b){a=b;return true;}return false;}\ntemplate<class t, class u> bool chmin(t &a, u b){if(a>b){a=b;return true;}return false;}\n#ifdef DEBUG\n#define debug(x) cout<<\"LINE \"<<__LINE__<<\": \"<<#x<<\" = \"<<x<<endl;\n#else\n#define debug(x) (void)0\n#endif\n\nnamespace templates{\n ll modpow(ll x, ll b,ll mod=MOD){\n ll res = 1;\n while(b){\n if(b&1)res = res * x % mod;\n x = x * x % mod;\n b>>=1;\n }\n return res;\n }\n\n ll modinv(ll x){\n return modpow(x, MOD-2);\n }\n\n bool was_output = false;\n\n void print();\n template <class t> void print(const vector<t> &);\n template <class t, class u> void print(const pair<t, u> &);\n template <class t> void print(const t&);\n template <class Head, class... Tail> void print(const Head&, const Tail&...);\n\n template <class t> void println(const vector<vector<t>>&);\n template <class t> void println(const vector<t>&);\n template <class t> void println(const t&);\n template <class Head, class... Tail> void println(const Head&, const Tail&...);\n void println();\n void newline();\n\n void print(){\n return;\n }\n\n template <class t>\n void print(const vector<t>&x){\n for(const t&i:x)print(i);\n }\n template <class t, class u>\n void print(const pair<t,u>&p){\n print(p.first);\n print(p.second);\n }\n template <class t>\n void print(const t&x){\n if(was_output)cout<<\" \";\n cout<<x;\n was_output = true;\n }\n template <class Head, class... Tail>\n void print(const Head&head,const Tail&...tail){\n print(head);\n print(tail...);\n }\n\n template<class t>\n void println(const vector<vector<t>>&x){\n for(vector<t> i:x)println(i);\n }\n template<class t>\n void println(const vector<t>&x){\n for(const t&i:x)print(i);\n println();\n }\n template <class t>\n void println(const t&x){\n print(x);\n println();\n }\n void println(){\n if(was_output){\n cout << endl;\n was_output = false;\n }\n }\n template <class Head, class... Tail>\n void println(const Head&head,const Tail&...tail){\n print(head);\n println(tail...);\n }\n\n void newline(){\n was_output = true;\n println();\n }\n\n template<class t>\n istream& operator>>(istream&is, vector<t>&x){\n for(auto &i:x)is >> i;\n return is;\n }\n\n template<class t, class u>\n istream& operator>>(istream&is, pair<t, u>&x){\n is >> x.first >> x.second;\n return is;\n }\n\n template<class t>\n ostream& operator<<(ostream&os, vector<t> &v){\n os << \"{\";\n for(t &i:v){\n os << i << \", \";\n }\n os << \"}\";\n return os;\n }\n\n template<class t = long long>\n t in(){\n t res; cin >> res; return res;\n }\n\n template<class t>\n vector<t> sorted(vector<t> line,function<bool(t,t)> comp=[](t a,t b){return a<b;}){\n sort(line.begin(),line.end(),comp);\n return line;\n }\n\n template<class t>\n vector<t> reversed(vector<t> line){\n reverse(line.begin(),line.end());\n return line;\n }\n string reversed(string str){\n reverse(str.begin(),str.end());\n return str;\n }\n\n long long gcd(long long a,long long b){\n while(b){\n a %= b;\n swap(a,b);\n }\n return a;\n }\n\n long long lcm(long long a,long long b){\n return a / gcd(a,b) * b;\n }\n\n class output_initializer{\n public:\n output_initializer(){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << setprecision(20);\n }\n };output_initializer OUTPUT_INITIALIZER_INSTANCE = output_initializer();\n}\n\nusing namespace templates;\n\n\ntemplate<class t>\nclass fraction{\n\tpublic:\n\t\tt numerator;\n\t\tt denominator;\n\n\t\tfraction():numerator(t(0)), denominator(t(1)){}\n\t\tfraction(t a, t b):numerator(a), denominator(b){}\n\t\tfraction(const fraction &a):numerator(a.numerator), denominator(a.denominator){}\n\n\t\tfraction pow(long long k){\n\t\t\tfraction res(1, 1);\n\t\t\tfraction sum(*this);\n\t\t\twhile(k){\n\t\t\t\tif(k&1)res*=sum;\n\t\t\t\tsum*=sum;\n\t\t\t\tk>>=1;\n\t\t\t}\n\t\t\treturn res;\n\t\t}\n\n\t\tfraction& operator+=(fraction a){\n\t\t\tnumerator = numerator * a.denominator + a.numerator * denominator;\n\t\t\tdenominator *= a.denominator;\n\t\t\treturn *this;\n\t\t}\n\n\t\tfraction& operator-=(fraction a){\n\t\t\tnumerator = numerator * a.denominator - a.numerator * denominator;\n\t\t\tdenominator *= a.denominator;\n\t\t\treturn *this;\n\t\t}\n\n\t\tfraction& operator*=(fraction a){\n\t\t\tnumerator *= a.numerator;\n\t\t\tdenominator *= a.denominator;\n\t\t\treturn *this;\n\t\t}\n\n\t\tfraction& operator/=(fraction a){\n\t\t\tnumerator *= a.denominator;\n\t\t\tdenominator *= a.numerator;\n\t\t\treturn *this;\n\t\t}\n\n\t\tfraction operator+(fraction a){return fraction(*this) += a;}\n\t\tfraction operator-(fraction a){return fraction(*this) -= a;}\n\t\tfraction operator*(fraction a){return fraction(*this) *= a;}\n\t\tfraction operator/(fraction a){return fraction(*this) /= a;}\n};\n\ntemplate<class t>\nbool operator<(fraction<t> a,fraction<t> b){\n return a.numerator * b.denominator < b.numerator * a.denominator;\n}\ntemplate<class t>\nbool operator>(fraction<t> a,fraction<t> b){\n return a.numerator * b.denominator > b.numerator * a.denominator;\n}\ntemplate<class t>\nbool operator==(fraction<t> a,fraction<t> b){\n return a.numerator * b.denominator == b.numerator * a.denominator;\n}\n\nvector<double> func(){\n int n = in();\n int m = in();\n int l = in();\n using F = fraction<ll>;\n using P = pair<F,double>;\n method(calc,vector<P>,double p,int v,int t){\n vector<double> dp(m+1,0);\n dp[0] = 1;\n rep(_,m){\n vector<double> ndp(m+1,0);\n rep(i,m){\n ndp[i] += (1-p) * dp[i];\n ndp[i+1] += p * dp[i];\n }\n swap(dp,ndp);\n }\n vector<P> res;\n rep(i,dp.size()){\n res.emplace_back(F(l,v)+F(t*i,1),dp[i]);\n }\n return res;\n };\n vvector<P> line;\n const F inf = F(1000000,1);\n rep(i,n){\n double p = in() / 100.0;\n int t = in();\n int v = in();\n if(v==0){\n line.emplace_back(vector<P>({{inf,1}}));\n }else{\n line.emplace_back(calc(p,v,t));\n }\n }\n vector<double> res;\n rep(i,n){\n double sum = 0;\n foreach(p,line[i]){\n double add = p.second;\n if(p.first==inf)continue;\n rep(j,n){\n if(i==j)continue;\n double psum = 0;\n foreach(k,line[j]){\n if(p.first < k.first)psum += k.second;\n }\n add *= psum;\n }\n sum += add;\n }\n res.emplace_back(sum);\n }\n return res;\n}\n\nint main(){\n foreach(i,func())println(i);\n return 0;\n}", "accuracy": 0.14285714285714285, "time_ms": 20, "memory_kb": 3752, "score_of_the_acc": -0.0942, "final_rank": 17 }, { "submission_id": "aoj_2303_6552878", "code_snippet": "#include<bits/stdc++.h>\n#define rep(i,a,...) for(int i = (a)*(strlen(#__VA_ARGS__)!=0);i<(int)(strlen(#__VA_ARGS__)?__VA_ARGS__:(a));++i)\n#define per(i,a,...) for(int i = (strlen(#__VA_ARGS__)?__VA_ARGS__:(a))-1;i>=(int)(strlen(#__VA_ARGS__)?(a):0);--i)\n#define foreach(i, n) for(auto &i:(n))\n#define all(x) (x).begin(), (x).end()\n#define bit(x) (1ll << (x))\n#define lambda(RES_TYPE, ...) (function<RES_TYPE(__VA_ARGS__)>)[&](__VA_ARGS__) -> RES_TYPE\n#define method(FUNC_NAME, RES_TYPE, ...) function<RES_TYPE(__VA_ARGS__)> FUNC_NAME = lambda(RES_TYPE, __VA_ARGS__)\nusing namespace std;\nusing ll = long long;\nusing pii = pair<int,int>;\nusing pll = pair<ll,ll>;\n//const ll MOD = (ll)1e9+7;\nconst ll MOD = 998244353;\nconst int INF = (ll)1e9+7;\nconst ll INFLL = (ll)1e18;\ntemplate<class t>\nusing vvector = vector<vector<t>>;\ntemplate<class t>\nusing vvvector = vector<vector<vector<t>>>;\ntemplate<class t>\nusing priority_queuer = priority_queue<t, vector<t>, greater<t>>;\ntemplate<class t, class u> bool chmax(t &a, u b){if(a<b){a=b;return true;}return false;}\ntemplate<class t, class u> bool chmin(t &a, u b){if(a>b){a=b;return true;}return false;}\n#ifdef DEBUG\n#define debug(x) cout<<\"LINE \"<<__LINE__<<\": \"<<#x<<\" = \"<<x<<endl;\n#else\n#define debug(x) (void)0\n#endif\n\nnamespace templates{\n ll modpow(ll x, ll b,ll mod=MOD){\n ll res = 1;\n while(b){\n if(b&1)res = res * x % mod;\n x = x * x % mod;\n b>>=1;\n }\n return res;\n }\n\n ll modinv(ll x){\n return modpow(x, MOD-2);\n }\n\n bool was_output = false;\n\n void print();\n template <class t> void print(const vector<t> &);\n template <class t, class u> void print(const pair<t, u> &);\n template <class t> void print(const t&);\n template <class Head, class... Tail> void print(const Head&, const Tail&...);\n\n template <class t> void println(const vector<vector<t>>&);\n template <class t> void println(const vector<t>&);\n template <class t> void println(const t&);\n template <class Head, class... Tail> void println(const Head&, const Tail&...);\n void println();\n void newline();\n\n void print(){\n return;\n }\n\n template <class t>\n void print(const vector<t>&x){\n for(const t&i:x)print(i);\n }\n template <class t, class u>\n void print(const pair<t,u>&p){\n print(p.first);\n print(p.second);\n }\n template <class t>\n void print(const t&x){\n if(was_output)cout<<\" \";\n cout<<x;\n was_output = true;\n }\n template <class Head, class... Tail>\n void print(const Head&head,const Tail&...tail){\n print(head);\n print(tail...);\n }\n\n template<class t>\n void println(const vector<vector<t>>&x){\n for(vector<t> i:x)println(i);\n }\n template<class t>\n void println(const vector<t>&x){\n for(const t&i:x)print(i);\n println();\n }\n template <class t>\n void println(const t&x){\n print(x);\n println();\n }\n void println(){\n if(was_output){\n cout << endl;\n was_output = false;\n }\n }\n template <class Head, class... Tail>\n void println(const Head&head,const Tail&...tail){\n print(head);\n println(tail...);\n }\n\n void newline(){\n was_output = true;\n println();\n }\n\n template<class t>\n istream& operator>>(istream&is, vector<t>&x){\n for(auto &i:x)is >> i;\n return is;\n }\n\n template<class t, class u>\n istream& operator>>(istream&is, pair<t, u>&x){\n is >> x.first >> x.second;\n return is;\n }\n\n template<class t>\n ostream& operator<<(ostream&os, vector<t> &v){\n os << \"{\";\n for(t &i:v){\n os << i << \", \";\n }\n os << \"}\";\n return os;\n }\n\n template<class t = long long>\n t in(){\n t res; cin >> res; return res;\n }\n\n template<class t>\n vector<t> sorted(vector<t> line,function<bool(t,t)> comp=[](t a,t b){return a<b;}){\n sort(line.begin(),line.end(),comp);\n return line;\n }\n\n template<class t>\n vector<t> reversed(vector<t> line){\n reverse(line.begin(),line.end());\n return line;\n }\n string reversed(string str){\n reverse(str.begin(),str.end());\n return str;\n }\n\n long long gcd(long long a,long long b){\n while(b){\n a %= b;\n swap(a,b);\n }\n return a;\n }\n\n long long lcm(long long a,long long b){\n return a / gcd(a,b) * b;\n }\n\n class output_initializer{\n public:\n output_initializer(){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << setprecision(20);\n }\n };output_initializer OUTPUT_INITIALIZER_INSTANCE = output_initializer();\n}\n\nusing namespace templates;\n\n\ntemplate<class t>\nclass fraction{\n\tpublic:\n\t\tt numerator;\n\t\tt denominator;\n\n\t\tfraction():numerator(t(0)), denominator(t(1)){}\n\t\tfraction(t a, t b):numerator(a), denominator(b){}\n\t\tfraction(const fraction &a):numerator(a.numerator), denominator(a.denominator){}\n\n\t\tfraction pow(long long k){\n\t\t\tfraction res(1, 1);\n\t\t\tfraction sum(*this);\n\t\t\twhile(k){\n\t\t\t\tif(k&1)res*=sum;\n\t\t\t\tsum*=sum;\n\t\t\t\tk>>=1;\n\t\t\t}\n\t\t\treturn res;\n\t\t}\n\n\t\tfraction& operator+=(fraction a){\n\t\t\tnumerator = numerator * a.denominator + a.numerator * denominator;\n\t\t\tdenominator *= a.denominator;\n\t\t\treturn *this;\n\t\t}\n\n\t\tfraction& operator-=(fraction a){\n\t\t\tnumerator = numerator * a.denominator - a.numerator * denominator;\n\t\t\tdenominator *= a.denominator;\n\t\t\treturn *this;\n\t\t}\n\n\t\tfraction& operator*=(fraction a){\n\t\t\tnumerator *= a.numerator;\n\t\t\tdenominator *= a.denominator;\n\t\t\treturn *this;\n\t\t}\n\n\t\tfraction& operator/=(fraction a){\n\t\t\tnumerator *= a.denominator;\n\t\t\tdenominator *= a.numerator;\n\t\t\treturn *this;\n\t\t}\n\n\t\tfraction operator+(fraction a){return fraction(*this) += a;}\n\t\tfraction operator-(fraction a){return fraction(*this) -= a;}\n\t\tfraction operator*(fraction a){return fraction(*this) *= a;}\n\t\tfraction operator/(fraction a){return fraction(*this) /= a;}\n};\n\ntemplate<class t>\nbool operator<(fraction<t> a,fraction<t> b){\n return a.numerator * b.denominator < b.numerator * a.denominator;\n}\n\nvector<double> func(){\n int n = in();\n int m = in();\n int l = in();\n using F = fraction<ll>;\n using P = pair<F,double>;\n method(calc,vector<P>,double p,int v,int t){\n vector<double> dp(m+1,0);\n dp[0] = 1;\n rep(_,m){\n vector<double> ndp(m+1,0);\n rep(i,m){\n ndp[i] += (1-p) * dp[i];\n ndp[i+1] += p * dp[i];\n }\n swap(dp,ndp);\n }\n vector<P> res;\n rep(i,dp.size()){\n res.emplace_back(F(l,v)+F(t*i,1),dp[i]);\n }\n return res;\n };\n vvector<P> line;\n rep(i,n){\n double p = in() / 100.0;\n int t = in();\n int v = in();\n if(v==0){\n line.emplace_back(vector<P>({{F(1000000,1),1}}));\n }else{\n line.emplace_back(calc(p,v,t));\n }\n }\n vector<double> res;\n rep(i,n){\n double sum = 0;\n foreach(p,line[i]){\n double add = p.second;\n rep(j,n){\n if(i==j)continue;\n double psum = 0;\n foreach(k,line[j]){\n if(p.first < k.first)psum += k.second;\n }\n add *= psum;\n }\n sum += add;\n }\n res.emplace_back(sum);\n }\n return res;\n}\n\nint main(){\n foreach(i,func())println(i);\n return 0;\n}", "accuracy": 0.14285714285714285, "time_ms": 20, "memory_kb": 3760, "score_of_the_acc": -0.0983, "final_rank": 19 }, { "submission_id": "aoj_2303_6552870", "code_snippet": "#include<bits/stdc++.h>\n#define rep(i,a,...) for(int i = (a)*(strlen(#__VA_ARGS__)!=0);i<(int)(strlen(#__VA_ARGS__)?__VA_ARGS__:(a));++i)\n#define per(i,a,...) for(int i = (strlen(#__VA_ARGS__)?__VA_ARGS__:(a))-1;i>=(int)(strlen(#__VA_ARGS__)?(a):0);--i)\n#define foreach(i, n) for(auto &i:(n))\n#define all(x) (x).begin(), (x).end()\n#define bit(x) (1ll << (x))\n#define lambda(RES_TYPE, ...) (function<RES_TYPE(__VA_ARGS__)>)[&](__VA_ARGS__) -> RES_TYPE\n#define method(FUNC_NAME, RES_TYPE, ...) function<RES_TYPE(__VA_ARGS__)> FUNC_NAME = lambda(RES_TYPE, __VA_ARGS__)\nusing namespace std;\nusing ll = long long;\nusing pii = pair<int,int>;\nusing pll = pair<ll,ll>;\n//const ll MOD = (ll)1e9+7;\nconst ll MOD = 998244353;\nconst int INF = (ll)1e9+7;\nconst ll INFLL = (ll)1e18;\ntemplate<class t>\nusing vvector = vector<vector<t>>;\ntemplate<class t>\nusing vvvector = vector<vector<vector<t>>>;\ntemplate<class t>\nusing priority_queuer = priority_queue<t, vector<t>, greater<t>>;\ntemplate<class t, class u> bool chmax(t &a, u b){if(a<b){a=b;return true;}return false;}\ntemplate<class t, class u> bool chmin(t &a, u b){if(a>b){a=b;return true;}return false;}\n#ifdef DEBUG\n#define debug(x) cout<<\"LINE \"<<__LINE__<<\": \"<<#x<<\" = \"<<x<<endl;\n#else\n#define debug(x) (void)0\n#endif\n\nnamespace templates{\n ll modpow(ll x, ll b,ll mod=MOD){\n ll res = 1;\n while(b){\n if(b&1)res = res * x % mod;\n x = x * x % mod;\n b>>=1;\n }\n return res;\n }\n\n ll modinv(ll x){\n return modpow(x, MOD-2);\n }\n\n bool was_output = false;\n\n void print();\n template <class t> void print(const vector<t> &);\n template <class t, class u> void print(const pair<t, u> &);\n template <class t> void print(const t&);\n template <class Head, class... Tail> void print(const Head&, const Tail&...);\n\n template <class t> void println(const vector<vector<t>>&);\n template <class t> void println(const vector<t>&);\n template <class t> void println(const t&);\n template <class Head, class... Tail> void println(const Head&, const Tail&...);\n void println();\n void newline();\n\n void print(){\n return;\n }\n\n template <class t>\n void print(const vector<t>&x){\n for(const t&i:x)print(i);\n }\n template <class t, class u>\n void print(const pair<t,u>&p){\n print(p.first);\n print(p.second);\n }\n template <class t>\n void print(const t&x){\n if(was_output)cout<<\" \";\n cout<<x;\n was_output = true;\n }\n template <class Head, class... Tail>\n void print(const Head&head,const Tail&...tail){\n print(head);\n print(tail...);\n }\n\n template<class t>\n void println(const vector<vector<t>>&x){\n for(vector<t> i:x)println(i);\n }\n template<class t>\n void println(const vector<t>&x){\n for(const t&i:x)print(i);\n println();\n }\n template <class t>\n void println(const t&x){\n print(x);\n println();\n }\n void println(){\n if(was_output){\n cout << endl;\n was_output = false;\n }\n }\n template <class Head, class... Tail>\n void println(const Head&head,const Tail&...tail){\n print(head);\n println(tail...);\n }\n\n void newline(){\n was_output = true;\n println();\n }\n\n template<class t>\n istream& operator>>(istream&is, vector<t>&x){\n for(auto &i:x)is >> i;\n return is;\n }\n\n template<class t, class u>\n istream& operator>>(istream&is, pair<t, u>&x){\n is >> x.first >> x.second;\n return is;\n }\n\n template<class t>\n ostream& operator<<(ostream&os, vector<t> &v){\n os << \"{\";\n for(t &i:v){\n os << i << \", \";\n }\n os << \"}\";\n return os;\n }\n\n template<class t = long long>\n t in(){\n t res; cin >> res; return res;\n }\n\n template<class t>\n vector<t> sorted(vector<t> line,function<bool(t,t)> comp=[](t a,t b){return a<b;}){\n sort(line.begin(),line.end(),comp);\n return line;\n }\n\n template<class t>\n vector<t> reversed(vector<t> line){\n reverse(line.begin(),line.end());\n return line;\n }\n string reversed(string str){\n reverse(str.begin(),str.end());\n return str;\n }\n\n long long gcd(long long a,long long b){\n while(b){\n a %= b;\n swap(a,b);\n }\n return a;\n }\n\n long long lcm(long long a,long long b){\n return a / gcd(a,b) * b;\n }\n\n class output_initializer{\n public:\n output_initializer(){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << setprecision(20);\n }\n };output_initializer OUTPUT_INITIALIZER_INSTANCE = output_initializer();\n}\n\nusing namespace templates;\n\n\ntemplate<class t>\nclass fraction{\n\tpublic:\n\t\tt numerator;\n\t\tt denominator;\n\n\t\tfraction():numerator(t(0)), denominator(t(1)){}\n\t\tfraction(t a, t b):numerator(a), denominator(b){}\n\t\tfraction(const fraction &a):numerator(a.numerator), denominator(a.denominator){}\n\n\t\tfraction pow(long long k){\n\t\t\tfraction res(1, 1);\n\t\t\tfraction sum(*this);\n\t\t\twhile(k){\n\t\t\t\tif(k&1)res*=sum;\n\t\t\t\tsum*=sum;\n\t\t\t\tk>>=1;\n\t\t\t}\n\t\t\treturn res;\n\t\t}\n\n\t\tfraction& operator+=(fraction a){\n\t\t\tnumerator = numerator * a.denominator + a.numerator * denominator;\n\t\t\tdenominator *= a.denominator;\n\t\t\treturn *this;\n\t\t}\n\n\t\tfraction& operator-=(fraction a){\n\t\t\tnumerator = numerator * a.denominator - a.numerator * denominator;\n\t\t\tdenominator *= a.denominator;\n\t\t\treturn *this;\n\t\t}\n\n\t\tfraction& operator*=(fraction a){\n\t\t\tnumerator *= a.numerator;\n\t\t\tdenominator *= a.denominator;\n\t\t\treturn *this;\n\t\t}\n\n\t\tfraction& operator/=(fraction a){\n\t\t\tnumerator *= a.denominator;\n\t\t\tdenominator *= a.numerator;\n\t\t\treturn *this;\n\t\t}\n\n\t\tfraction operator+(fraction a){return fraction(*this) += a;}\n\t\tfraction operator-(fraction a){return fraction(*this) -= a;}\n\t\tfraction operator*(fraction a){return fraction(*this) *= a;}\n\t\tfraction operator/(fraction a){return fraction(*this) /= a;}\n};\n\ntemplate<class t>\nbool operator<(fraction<t> a,fraction<t> b){\n return a.numerator * b.denominator < b.numerator * a.denominator;\n}\n\nvector<double> func(){\n int n = in();\n int m = in();\n int l = in();\n using F = fraction<ll>;\n using P = pair<F,double>;\n method(calc,vector<P>,double p,int v,int t){\n vector<double> dp(m+1,0);\n dp[0] = 1;\n rep(_,m){\n vector<double> ndp(m+1,0);\n rep(i,m){\n ndp[i] += (1-p) * dp[i];\n ndp[i+1] += p * dp[i];\n }\n swap(dp,ndp);\n }\n vector<P> res;\n rep(i,dp.size()){\n res.emplace_back(F(l,v)+F(t*i,1),dp[i]);\n }\n return res;\n };\n vvector<P> line;\n rep(i,n){\n double p = in() / 100.0;\n int t = in();\n int v = in();\n line.emplace_back(calc(p,v,t));\n }\n vector<double> res;\n rep(i,n){\n double sum = 0;\n foreach(p,line[i]){\n double add = p.second;\n rep(j,n){\n if(i==j)continue;\n double psum = 0;\n foreach(k,line[j]){\n if(p.first < k.first)psum += k.second;\n }\n add *= psum;\n }\n sum += add;\n }\n res.emplace_back(sum);\n }\n return res;\n}\n\nint main(){\n foreach(i,func())println(i);\n return 0;\n}", "accuracy": 0.14285714285714285, "time_ms": 20, "memory_kb": 3756, "score_of_the_acc": -0.0962, "final_rank": 18 }, { "submission_id": "aoj_2303_5918315", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing P = pair<int64_t,int64_t>;\n\nvoid solve(){\n int64_t N, M, L;\n cin >> N >> M >> L;\n vector<long double> p(N);\n vector<P> vec(N);\n for(int i=0; i<N; i++){\n cin >> p[i];\n p[i] /= 100;\n int x, y;\n cin >> x >> y;\n vec[i] = P(x,y);\n }\n vector<int64_t> COM(M+1);\n vector<vector<long double>> C(N,vector<long double>(M+1));\n for(int i=0; i<=M; i++){\n if(i == 0){\n COM[i] = 1;\n }\n else{\n COM[i] = COM[i-1]*(M-i+1)/i;\n }\n for(int j=0; j<N; j++){\n C[j][i] = COM[i]*pow(p[j],i)*pow(1-p[j],M-i);\n }\n }\n for(int i=0; i<N; i++){ //person\n long double ans = 0;\n auto[a1,b1] = vec[i];\n for(int j=0; j<=M; j++){ //rest times\n long double prob = C[i][j];\n for(int k=0; k<N; k++){\n if(k == i){\n continue;\n }\n auto[a2,b2] = vec[k];\n long double prob2 = 0;\n for(int l=0; l<=M; l++){\n if(b2*(a1*j*b1 + L) < b1*(a2*l*b2 + L)){\n //cout << j << \" \" << k << \" \" << *pow(p[k],l)*pow(1-p[k],M-l) << endl;\n prob2 += C[k][l];\n }\n }\n //cout << \"SUM:\" << i << \" \" << j << \" \" << prob2 << endl;\n prob *= prob2;\n }\n ans += prob;\n }\n cout << fixed << setprecision(15) << ans << endl;\n }\n}\n\nint main(){\n while(true){\n solve();\n break;\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3652, "score_of_the_acc": -0.0424, "final_rank": 4 }, { "submission_id": "aoj_2303_5884342", "code_snippet": "#pragma region Macros\n#include <bits/stdc++.h>\nusing namespace std;\ntemplate <class T> inline bool chmax(T &a, T b) {\n if(a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <class T> inline bool chmin(T &a, T b) {\n if(a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\n#ifdef DEBUG\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << '(' << p.first << ',' << p.second << ')';\n return os;\n}\ntemplate <class T> ostream &operator<<(ostream &os, const vector<T> &v) {\n os << '{';\n for(int i = 0; i < (int)v.size(); i++) {\n if(i) { os << ','; }\n os << v[i];\n }\n os << '}';\n return os;\n}\nvoid debugg() { cerr << endl; }\ntemplate <class T, class... Args>\nvoid debugg(const T &x, const Args &... args) {\n cerr << \" \" << x;\n debugg(args...);\n}\n#define debug(...) \\\n cerr << __LINE__ << \" [\" << #__VA_ARGS__ << \"]: \", debugg(__VA_ARGS__)\n#define dump(x) cerr << __LINE__ << \" \" << #x << \" = \" << (x) << endl\n#else\n#define debug(...) (void(0))\n#define dump(x) (void(0))\n#endif\n\nstruct Setup {\n Setup() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(15);\n }\n} __Setup;\n\nusing ll = long long;\n#define ALL(v) (v).begin(), (v).end()\n#define RALL(v) (v).rbegin(), (v).rend()\n#define FOR(i, a, b) for(int i = (a); i < int(b); i++)\n#define REP(i, n) FOR(i, 0, n)\nconst int INF = 1 << 30;\nconst ll LLINF = 1LL << 60;\nconstexpr int MOD = 1000000007;\n\nvoid Case(int i) { cout << \"Case #\" << i << \": \"; }\nint popcount(int x) { return __builtin_popcount(x); }\nll popcount(ll x) { return __builtin_popcountll(x); }\n#pragma endregion Macros\n\nll C[55][55];\n\nvoid init() {\n C[0][0] = 1;\n FOR(i, 1, 55) {\n C[i][0] = 1;\n FOR(j, 1, 55) {\n C[i][j] = C[i-1][j-1] + C[i-1][j];\n }\n }\n}\n\nint main() {\n init();\n int N, M, L;\n cin >> N >> M >> L;\n vector<ll> P(N), T(N), V(N);\n REP(i, N) cin >> P[i] >> T[i] >> V[i];\n\n vector prob(N, vector(M+1, vector<double>(M+1)));\n REP(i, N) {\n prob[i][0][0] = 1.0;\n REP(j, M) {\n REP(k, M+1) {\n prob[i][j+1][k] += prob[i][j][k] * (100 - P[i]) / 100;\n if(k < M) prob[i][j+1][k+1] += prob[i][j][k] * P[i] / 100;\n }\n }\n }\n\n REP(i, N) {\n if(V[i] == 0) {\n cout << 0.0 << endl;\n continue;\n }\n double ans = 0.0;\n REP(ci, M+1) {\n double p = prob[i][M][ci];\n REP(j, N) {\n if(i == j) continue;\n if(V[j] == 0) continue;\n double sum = 0;\n REP(cj, M+1) {\n double diff = double(L)/V[i] + ci*T[i] - double(L)/V[j] - cj*T[j];\n if(diff >= 0.0) continue;\n sum += prob[j][M][cj];\n }\n p *= sum;\n }\n ans += p;\n }\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 5524, "score_of_the_acc": -1.0114, "final_rank": 9 }, { "submission_id": "aoj_2303_5851689", "code_snippet": "/*\tauthor: Kite_kuma\n\tcreated: 2021.09.01 15:05:47 */\n\n// #ifdef LOCAL\n// #define _GLIBCXX_DEBUG\n// #endif\n#include <bits/stdc++.h>\nusing namespace std;\n\n#pragma region macros\n\n#define foa(s, v) for(auto &s : v)\n#define all(v) (v).begin(), (v).end()\n#define rall(v) (v).rbegin(), (v).rend()\n#define popcnt(n) __builtin_popcountll((long long)n)\n\n#define REPname(a, b, c, d, e, ...) e\n#define rep(...) REPname(__VA_ARGS__, REP3, REP2, REP1, REP0)(__VA_ARGS__)\n#define REP0(x) for(int Counter_in_rep_macro = 0; Counter_in_rep_macro < (x); ++Counter_in_rep_macro)\n#define REP1(i, x) for(int i = 0; i < (x); ++i)\n#define REP2(i, l, r) for(int i = (l); i < (r); ++i)\n#define REP3(i, l, r, c) for(int i = (l); i < (r); i += (c))\n\n#define DREPname(a, b, c, d, e, ...) e\n#define drep(...) DREPname(__VA_ARGS__, DREP3, DREP2, DREP1)(__VA_ARGS__)\n#define DREP1(i, x) for(int i = (x)-1; i >= 0; --i)\n#define DREP2(i, l, r) for(int i = (r)-1; i >= (l); --i)\n#define DREP3(i, l, r, c) for(int i = (r)-1; i >= (l); i -= (c))\n\n#pragma endregion\n\n#pragma region aliases\n\nusing ll = long long;\nusing ld = long double;\nusing ull = unsigned long long;\nusing vi = std::vector<int>;\nusing vvi = std::vector<std::vector<int>>;\nusing vvvi = std::vector<std::vector<std::vector<int>>>;\nusing vll = std::vector<ll>;\nusing vvll = std::vector<vll>;\nusing vvvll = std::vector<vvll>;\nusing pii = std::pair<int, int>;\nusing pll = std::pair<long long, long long>;\ntemplate <class T = ll>\nusing V = std::vector<T>;\ntemplate <class T = ll>\nusing VV = V<V<T>>;\ntemplate <class T = ll>\nusing VVV = V<V<V<T>>>;\ntemplate <class T = ll>\nusing pqup = std::priority_queue<T, std::vector<T>, std::greater<T>>;\ntemplate <class T = ll>\nusing pqdn = std::priority_queue<T>;\n\n#pragma endregion\n\n#pragma region constants\n\nconst int inf = 1e9;\nconst long long INF = 1e18;\nconst long double pi = acos(-1);\nconst char dl = '\\n';\nconst char sp = ' ';\nint dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};\nint dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};\n\nconst int mod_1000000007 = 1000000007;\nconst int mod_998244353 = 998244353;\n\n#pragma endregion\n\n#pragma region basic_operation\n\ntemplate <class T0, class T1, class T2>\ninline bool in_range(T0 x, T1 lef, T2 rig) {\n\treturn ((lef <= x) && (x < rig));\n}\n\ntemplate <class T>\ninline bool chmin(T &a, T b) {\n\tif(a > b) {\n\t\ta = b;\n\t\treturn true;\n\t}\n\treturn false;\n}\ntemplate <class T>\ninline bool chmax(T &a, T b) {\n\tif(a < b) {\n\t\ta = b;\n\t\treturn true;\n\t}\n\treturn false;\n}\n\nvoid Yes(bool f = 1) { std::cout << (f ? \"Yes\" : \"No\") << '\\n'; }\nvoid No() { std::cout << \"No\\n\"; }\nvoid YES(bool f = 1) { std::cout << (f ? \"YES\" : \"NO\") << '\\n'; }\nvoid NO() { std::cout << \"NO\\n\"; }\n\ntemplate <class T>\nvoid drop(T answer) {\n\tstd::cout << answer << '\\n';\n\texit(0);\n}\n\nvoid err(bool flag = true) {\n\tif(!flag) return;\n\tstd::cout << -1 << '\\n';\n\texit(0);\n}\n\ntemplate <class T>\nvoid vout(std::vector<T> const &v, bool vertically = 0) {\n\tif(vertically) {\n\t\tfor(auto const &a : v) {\n\t\t\tstd::cout << a << '\\n';\n\t\t}\n\t\treturn;\n\t}\n\tfor(auto it = v.begin(); it != v.end(); it++) {\n\t\tstd::cout << (*it);\n\t\tif(it != v.end() - 1) {\n\t\t\tstd::cout << ' ';\n\t\t}\n\t}\n\tstd::cout << '\\n';\n\treturn;\n}\n\ninline void print() { std::cout << '\\n'; }\ntemplate <class T>\ninline void print(T x) {\n\tstd::cout << x << '\\n';\n\treturn;\n}\ntemplate <typename Head, typename... Tail>\nvoid print(Head H, Tail... T) {\n\tstd::cout << H << \" \";\n\tprint(T...);\n}\n\ntemplate <class T>\nvoid add(std::vector<T> &v, T val) {\n\tfor(auto &a : v) a += val;\n\treturn;\n}\n\ntemplate <class T>\nT dup(T a, T b) {\n\tassert(b != 0);\n\treturn (a + b - 1) / b;\n}\n\ntemplate <class T>\nT greatest_lower_multiple(T x, T d) {\n\tif(d == 0) return 0;\n\tif(d < 0) d *= -1;\n\tT y = x % d;\n\tif(y < 0) y += d;\n\treturn x - y;\n}\n\ntemplate <class T>\nT least_upper_multiple(T x, T d) {\n\treturn -greatest_lower_multiple(-x, d);\n}\n\nlong long POW(long long a, long long n) {\n\tlong long res = 1;\n\twhile(n > 0) {\n\t\tif(n & 1) res = res * a;\n\t\ta = a * a;\n\t\tn >>= 1;\n\t}\n\treturn res;\n}\n\nlong long modpow(long long a, long long n, long long mod) {\t // a^n mod\n\tassert(n >= 0 && mod);\n\tif(mod == 1) return 0LL;\n\tlong long res = 1;\n\ta %= mod;\n\twhile(n > 0) {\n\t\tif(n & 1) res = res * a % mod;\n\t\ta = a * a % mod;\n\t\tn >>= 1;\n\t}\n\treturn res < 0 ? res + mod : res;\n}\n\n// return x which satisfies a * x % mod == gcd(a, mod)\n// not (mod | a)\nlong long modinv(long long a, long long mod) {\n\tlong long b = mod, u = 1, v = 0;\n\twhile(b) {\n\t\tlong long t = a / b;\n\t\ta -= t * b;\n\t\tstd::swap(a, b);\n\t\tu -= t * v;\n\t\tstd::swap(u, v);\n\t}\n\tu %= mod;\n\tif(u < 0) u += mod;\n\treturn u;\n}\n\ntemplate <class T>\nint lb(const std::vector<T> &a, const T x) {\n\treturn std::distance(a.begin(), std::lower_bound(a.begin(), a.end(), x));\n}\ntemplate <class T>\nint ub(const std::vector<T> &a, const T x) {\n\treturn std::distance(a.begin(), std::upper_bound(a.begin(), a.end(), x));\n}\ntemplate <class T>\nvoid unq_sort(std::vector<T> &a) {\n\tstd::sort(a.begin(), a.end());\n\ta.erase(std::unique(a.begin(), a.end()), a.end());\n}\ntemplate <class T>\nstd::vector<int> press(std::vector<T> &a) {\n\tauto vec = a;\n\tunq_sort(vec);\n\tstd::vector<int> ret;\n\tfor(auto &v : a) ret.push_back(lb(vec, v));\n\treturn ret;\n}\n\n#pragma endregion\n\n#pragma region input\n#define VEC(type, name, size) \\\n\tvector<type> name(size); \\\n\tscanner::INPUT(name)\n#define VVEC(type, name, h, w) \\\n\tvector<vector<type>> name(h, vector<type>(w)); \\\n\tscanner::INPUT(name)\n#define INT(...) \\\n\tint __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define LL(...) \\\n\tlong long __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define STR(...) \\\n\tstring __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define CHR(...) \\\n\tchar __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define DBL(...) \\\n\tdouble __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define LD(...) \\\n\tlong double __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define TPL3(type0, type1, type2, name) \\\n\tstd::tuple<type0, type1, type2> name; \\\n\tscanner::INPUT(name);\n#define TPL4(type0, type1, type2, type3, name) \\\n\tstd::tuple<type0, type1, type2, type3> name; \\\n\tscanner::INPUT(name);\n\nnamespace scanner {\ntemplate <class T>\nvoid scan(T &a) {\n\tstd::cin >> a;\n}\n\ntemplate <class T, class L>\nvoid scan(std::pair<T, L> &p) {\n\tscan(p.first);\n\tscan(p.second);\n}\n\ntemplate <class T0, class T1, class T2>\nvoid scan(std::tuple<T0, T1, T2> &p) {\n\tT0 t0;\n\tT1 t1;\n\tT2 t2;\n\tscan(t0);\n\tscan(t1);\n\tscan(t2);\n\tp = std::make_tuple(t0, t1, t2);\n}\n\ntemplate <class T0, class T1, class T2, class T3>\nvoid scan(std::tuple<T0, T1, T2, T3> &p) {\n\tT0 t0;\n\tT1 t1;\n\tT2 t2;\n\tT3 t3;\n\tscan(t0);\n\tscan(t1);\n\tscan(t2);\n\tscan(t3);\n\tp = std::make_tuple(t0, t1, t2, t3);\n}\n\ntemplate <class T>\nvoid scan(std::vector<T> &a) {\n\tfor(auto &i : a) scan(i);\n}\n\nvoid INPUT() {}\ntemplate <class Head, class... Tail>\nvoid INPUT(Head &head, Tail &... tail) {\n\tscan(head);\n\tINPUT(tail...);\n}\n} // namespace scanner\n\ntemplate <typename T1, typename T2>\nstd::istream &operator>>(std::istream &is, std::pair<T1, T2> &p) {\n\tis >> p.first >> p.second;\n\treturn is;\n}\n#pragma endregion\n\n#pragma region debug\n\n#pragma region output\ntemplate <typename T1, typename T2>\nstd::ostream &std::operator<<(std::ostream &os, const std::pair<T1, T2> &p) {\n\tos << p.first << \" \" << p.second;\n\treturn os;\n}\ntemplate <class T>\nstd::ostream &std::operator<<(std::ostream &os, const std::vector<T> &v) {\n\tfor(int i = 0; i < (int)v.size(); i++) {\n\t\tif(i) os << \" \";\n\t\tos << v[i];\n\t}\n\treturn os;\n}\n#pragma endregion\n\n#pragma region view\n\nnamespace viewer {\nvoid view(const long long e) {\n\tif(e == INF)\n\t\tstd::cerr << \"INF\";\n\telse if(e == -INF)\n\t\tstd::cerr << \"-INF\";\n\telse\n\t\tstd::cerr << e;\n}\n\nvoid view(const int e) {\n\tif(e == inf)\n\t\tstd::cerr << \"inf\";\n\telse if(e == -inf)\n\t\tstd::cerr << \"-inf\";\n\telse\n\t\tstd::cerr << e;\n}\n\ntemplate <typename T>\nvoid view(const T e) {\n\tstd::cerr << e;\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::pair<T, U> &p) {\n\tstd::cerr << \"(\";\n\tview(p.first);\n\tstd::cerr << \", \";\n\tview(p.second);\n\tstd::cerr << \")\";\n}\n\ntemplate <class T0, class T1, class T2>\nvoid view(const std::tuple<T0, T1, T2> &p) {\n\tstd::cerr << \"(\";\n\tview(std::get<0>(p));\n\tstd::cerr << \", \";\n\tview(std::get<1>(p));\n\tstd::cerr << \", \";\n\tview(std::get<2>(p));\n\tstd::cerr << \")\";\n}\n\ntemplate <class T0, class T1, class T2, class T3>\nvoid view(const std::tuple<T0, T1, T2, T3> &p) {\n\tstd::cerr << \"(\";\n\tview(std::get<0>(p));\n\tstd::cerr << \", \";\n\tview(std::get<1>(p));\n\tstd::cerr << \", \";\n\tview(std::get<2>(p));\n\tstd::cerr << \", \";\n\tview(std::get<3>(p));\n\tstd::cerr << \")\";\n}\n\ntemplate <typename T>\nvoid view(const std::set<T> &s) {\n\tif(s.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << \"{ \";\n\tfor(auto &t : s) {\n\t\tview(t);\n\t\tstd::cerr << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\n\ntemplate <typename T>\nvoid view(const std::unordered_set<T> &s) {\n\tif(s.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << \"{ \";\n\tfor(auto &t : s) {\n\t\tview(t);\n\t\tstd::cerr << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\n\ntemplate <typename T>\nvoid view(const std::vector<T> &v) {\n\tif(v.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << \"{ \";\n\tfor(const auto &e : v) {\n\t\tview(e);\n\t\tstd::cerr << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\n\ntemplate <typename T>\nvoid view(const std::vector<std::vector<T>> &vv) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &v : vv) {\n\t\tstd::cerr << \"\\t\";\n\t\tview(v);\n\t\tstd::cerr << '\\n';\n\t}\n\tstd::cerr << \"}\";\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::vector<std::pair<T, U>> &v) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &c : v) {\n\t\tstd::cerr << \"\\t(\";\n\t\tview(c.first);\n\t\tstd::cerr << \", \";\n\t\tview(c.second);\n\t\tstd::cerr << \")\\n\";\n\t}\n\tstd::cerr << \"}\";\n}\n\ntemplate <class T0, class T1, class T2>\nvoid view(const std::vector<std::tuple<T0, T1, T2>> &v) {\n\tif(v.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << '{';\n\tfor(const auto &t : v) {\n\t\tstd::cerr << \"\\n\\t\";\n\t\tview(t);\n\t\tstd::cerr << \",\";\n\t}\n\tstd::cerr << \"\\n}\";\n}\n\ntemplate <class T0, class T1, class T2, class T3>\nvoid view(const std::vector<std::tuple<T0, T1, T2, T3>> &v) {\n\tif(v.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << '{';\n\tfor(const auto &t : v) {\n\t\tstd::cerr << \"\\n\\t\";\n\t\tview(t);\n\t\tstd::cerr << \",\";\n\t}\n\tstd::cerr << \"\\n}\";\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::map<T, U> &m) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &t : m) {\n\t\tstd::cerr << \"\\t[\";\n\t\tview(t.first);\n\t\tstd::cerr << \"] : \";\n\t\tview(t.second);\n\t\tstd::cerr << '\\n';\n\t}\n\tstd::cerr << \"}\";\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::unordered_map<T, U> &m) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &t : m) {\n\t\tstd::cerr << \"\\t[\";\n\t\tview(t.first);\n\t\tstd::cerr << \"] : \";\n\t\tview(t.second);\n\t\tstd::cerr << '\\n';\n\t}\n\tstd::cerr << \"}\";\n}\n} // namespace viewer\n\n#pragma endregion\n\n// when debugging : g++ foo.cpp -DLOCAL\n#ifdef LOCAL\nvoid debug_out() {}\ntemplate <typename Head, typename... Tail>\nvoid debug_out(Head H, Tail... T) {\n\tviewer::view(H);\n\tstd::cerr << \", \";\n\tdebug_out(T...);\n}\n#define debug(...) \\\n\tdo { \\\n\t\tstd::cerr << __LINE__ << \" [\" << #__VA_ARGS__ << \"] : [\"; \\\n\t\tdebug_out(__VA_ARGS__); \\\n\t\tstd::cerr << \"\\b\\b]\\n\"; \\\n\t} while(0)\n#define dump(x) \\\n\tdo { \\\n\t\tstd::cerr << __LINE__ << \" \" << #x << \" : \"; \\\n\t\tviewer::view(x); \\\n\t\tstd::cerr << '\\n'; \\\n\t} while(0)\n\n#else\n#define debug(...) (void(0))\n#define dump(x) (void(0))\n#endif\n\n#pragma endregion\n\nusing R = long double;\n\nvoid solve(int n) {\n\tint m;\n\tcin >> m;\n\tint length;\n\tcin >> length;\n\tvector<R> prob(n);\n\tvector<R> time(n);\n\tvector<R> velocity(n);\n\n\trep(i, n) {\n\t\tcin >> prob[i] >> time[i] >> velocity[i];\n\t\tprob[i] /= 100;\n\t}\n\n\tauto cost_all = [&](int person, int rest_count) {\n\t\treturn velocity[person] > 0 ? time[person] * rest_count + length / velocity[person] : INF;\n\t};\n\n\tVV<R> prob_rest(n, V<R>(m + 1));\n\trep(i, n) prob_rest[i][0] = 1;\n\trep(m) {\n\t\tauto tmp = prob_rest;\n\t\ttmp.assign(n, V<R>(m + 1, 0));\n\n\t\trep(i, n) {\n\t\t\trep(j, m + 1) {\n\t\t\t\ttmp[i][j] += prob_rest[i][j] * (1 - prob[i]);\n\t\t\t\tif(j < m) {\n\t\t\t\t\ttmp[i][j + 1] += prob_rest[i][j] * prob[i];\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tswap(tmp, prob_rest);\n\t}\n\n\tdebug(prob_rest);\n\n\tauto win_prob = [&](int person, int rest_count) {\n\t\tR cost = cost_all(person, rest_count);\n\t\tR ans = 1;\n\t\tdebug(person, rest_count, cost);\n\t\trep(opponent, n) {\n\t\t\tif(opponent == person) continue;\n\t\t\tR res = 0;\n\t\t\trep(rest_count_opponent, m + 1) {\n\t\t\t\tif(cost_all(opponent, rest_count_opponent) > cost + 1e-10) {\n\t\t\t\t\tres += prob_rest[opponent][rest_count_opponent];\n\n\t\t\t\t\tdebug(opponent, rest_count_opponent);\n\t\t\t\t}\n\t\t\t}\n\t\t\tans *= res;\n\t\t}\n\t\treturn ans * prob_rest[person][rest_count];\n\t};\n\n\tR ret = 0;\n\trep(i, n) {\n\t\tret = 0;\n\t\trep(j, m + 1) { ret += win_prob(i, j); }\n\t\tcout << ret << dl;\n\t}\n\n\treturn;\n}\n\nint main() {\n\tstd::ios::sync_with_stdio(false);\n\tstd::cin.tie(nullptr);\n\tstd::cout << std::fixed << std::setprecision(15);\n\t// std::cerr << std::fixed << std::setprecision(15);\n\tsrand((unsigned)time(NULL));\n\n\tint n;\n\twhile(cin >> n) {\n\t\t// cout << (solve(n)) << dl;\n\t\tsolve(n);\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3708, "score_of_the_acc": -0.0941, "final_rank": 7 }, { "submission_id": "aoj_2303_5846900", "code_snippet": "#line 1 \"/Users/nok0/Documents/Programming/nok0/template.hpp\"\n#include <bits/stdc++.h>\nusing namespace std;\n#if __has_include(<atcoder/all>)\n#include <atcoder/all>\nusing namespace atcoder;\n#endif\n\n#pragma region Macros\n// rep macro\n#define foa(v, a) for(auto &v : a)\n#define REPname(a, b, c, d, e, ...) e\n#define REP(...) REPname(__VA_ARGS__, REP3, REP2, REP1, REP0)(__VA_ARGS__)\n#define REP0(x) for(int i = 0; i < (x); ++i)\n#define REP1(i, x) for(int i = 0; i < (x); ++i)\n#define REP2(i, l, r) for(int i = (l); i < (r); ++i)\n#define REP3(i, l, r, c) for(int i = (l); i < (r); i += (c))\n#define REPSname(a, b, c, ...) c\n#define REPS(...) REPSname(__VA_ARGS__, REPS1, REPS0)(__VA_ARGS__)\n#define REPS0(x) for(int i = 1; i <= (x); ++i)\n#define REPS1(i, x) for(int i = 1; i <= (x); ++i)\n#define RREPname(a, b, c, d, e, ...) e\n#define RREP(...) RREPname(__VA_ARGS__, RREP3, RREP2, RREP1, RREP0)(__VA_ARGS__)\n#define RREP0(x) for(int i = (x)-1; i >= 0; --i)\n#define RREP1(i, x) for(int i = (x)-1; i >= 0; --i)\n#define RREP2(i, r, l) for(int i = (r)-1; i >= (l); --i)\n#define RREP3(i, r, l, c) for(int i = (r)-1; i >= (l); i -= (c))\n#define RREPSname(a, b, c, ...) c\n#define RREPS(...) RREPSname(__VA_ARGS__, RREPS1, RREPS0)(__VA_ARGS__)\n#define RREPS0(x) for(int i = (x); i >= 1; --i)\n#define RREPS1(i, x) for(int i = (x); i >= 1; --i)\n\n// name macro\n#define pb push_back\n#define eb emplace_back\n#define SZ(x) ((int)(x).size())\n#define all(x) (x).begin(), (x).end()\n#define rall(x) (x).rbegin(), (x).rend()\n#define popcnt(x) __builtin_popcountll(x)\ntemplate <class T = int>\nusing V = std::vector<T>;\ntemplate <class T = int>\nusing VV = std::vector<std::vector<T>>;\ntemplate <class T>\nusing pqup = std::priority_queue<T, std::vector<T>, std::greater<T>>;\nusing ll = long long;\nusing ld = long double;\nusing int128 = __int128_t;\nusing pii = std::pair<int, int>;\nusing pll = std::pair<long long, long long>;\n\n// input macro\ntemplate <class T, class U>\nstd::istream &operator>>(std::istream &is, std::pair<T, U> &p) {\n\tis >> p.first >> p.second;\n\treturn is;\n}\ntemplate <class T>\nstd::istream &operator>>(std::istream &is, std::vector<T> &v) {\n\tfor(T &i : v) is >> i;\n\treturn is;\n}\nstd::istream &operator>>(std::istream &is, __int128_t &a) {\n\tstd::string s;\n\tis >> s;\n\t__int128_t ret = 0;\n\tfor(int i = 0; i < s.length(); i++)\n\t\tif('0' <= s[i] and s[i] <= '9')\n\t\t\tret = 10 * ret + s[i] - '0';\n\ta = ret * (s[0] == '-' ? -1 : 1);\n\treturn is;\n}\n#if __has_include(<atcoder/all>)\nstd::istream &operator>>(std::istream &is, atcoder::modint998244353 &a) {\n\tlong long v;\n\tis >> v;\n\ta = v;\n\treturn is;\n}\nstd::istream &operator>>(std::istream &is, atcoder::modint1000000007 &a) {\n\tlong long v;\n\tis >> v;\n\ta = v;\n\treturn is;\n}\ntemplate <int m>\nstd::istream &operator>>(std::istream &is, atcoder::static_modint<m> &a) {\n\tlong long v;\n\tis >> v;\n\ta = v;\n\treturn is;\n}\ntemplate <int m>\nstd::istream &operator>>(std::istream &is, atcoder::dynamic_modint<m> &a) {\n\tlong long v;\n\tis >> v;\n\ta = v;\n\treturn is;\n}\n#endif\nnamespace scanner {\nvoid scan(int &a) { std::cin >> a; }\nvoid scan(long long &a) { std::cin >> a; }\nvoid scan(std::string &a) { std::cin >> a; }\nvoid scan(char &a) { std::cin >> a; }\nvoid scan(char a[]) { std::scanf(\"%s\", a); }\nvoid scan(double &a) { std::cin >> a; }\nvoid scan(long double &a) { std::cin >> a; }\ntemplate <class T, class U>\nvoid scan(std::pair<T, U> &p) { std::cin >> p; }\ntemplate <class T>\nvoid scan(std::vector<T> &a) { std::cin >> a; }\nvoid INPUT() {}\ntemplate <class Head, class... Tail>\nvoid INPUT(Head &head, Tail &... tail) {\n\tscan(head);\n\tINPUT(tail...);\n}\n} // namespace scanner\n#define VEC(type, name, size) \\\n\tstd::vector<type> name(size); \\\n\tscanner::INPUT(name)\n#define VVEC(type, name, h, w) \\\n\tstd::vector<std::vector<type>> name(h, std::vector<type>(w)); \\\n\tscanner::INPUT(name)\n#define INT(...) \\\n\tint __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define LL(...) \\\n\tlong long __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define STR(...) \\\n\tstd::string __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define CHAR(...) \\\n\tchar __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define DOUBLE(...) \\\n\tdouble __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define LD(...) \\\n\tlong double __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n\n// output-macro\ntemplate <class T, class U>\nstd::ostream &operator<<(std::ostream &os, const std::pair<T, U> &p) {\n\tos << p.first << \" \" << p.second;\n\treturn os;\n}\ntemplate <class T>\nstd::ostream &operator<<(std::ostream &os, const std::vector<T> &a) {\n\tfor(int i = 0; i < int(a.size()); ++i) {\n\t\tif(i) os << \" \";\n\t\tos << a[i];\n\t}\n\treturn os;\n}\nstd::ostream &operator<<(std::ostream &dest, __int128_t &value) {\n\tstd::ostream::sentry s(dest);\n\tif(s) {\n\t\t__uint128_t tmp = value < 0 ? -value : value;\n\t\tchar buffer[128];\n\t\tchar *d = std::end(buffer);\n\t\tdo {\n\t\t\t--d;\n\t\t\t*d = \"0123456789\"[tmp % 10];\n\t\t\ttmp /= 10;\n\t\t} while(tmp != 0);\n\t\tif(value < 0) {\n\t\t\t--d;\n\t\t\t*d = '-';\n\t\t}\n\t\tint len = std::end(buffer) - d;\n\t\tif(dest.rdbuf()->sputn(d, len) != len) {\n\t\t\tdest.setstate(std::ios_base::badbit);\n\t\t}\n\t}\n\treturn dest;\n}\n#if __has_include(<atcoder/all>)\nstd::ostream &operator<<(std::ostream &os, const atcoder::modint998244353 &a) { return os << a.val(); }\nstd::ostream &operator<<(std::ostream &os, const atcoder::modint1000000007 &a) { return os << a.val(); }\ntemplate <int m>\nstd::ostream &operator<<(std::ostream &os, const atcoder::static_modint<m> &a) { return os << a.val(); }\ntemplate <int m>\nstd::ostream &operator<<(std::ostream &os, const atcoder::dynamic_modint<m> &a) { return os << a.val(); }\n#endif\ntemplate <class T>\nvoid print(const T a) { std::cout << a << '\\n'; }\ntemplate <class Head, class... Tail>\nvoid print(Head H, Tail... T) {\n\tstd::cout << H << ' ';\n\tprint(T...);\n}\ntemplate <class T>\nvoid printel(const T a) { std::cout << a << '\\n'; }\ntemplate <class T>\nvoid printel(const std::vector<T> &a) {\n\tfor(const auto &v : a)\n\t\tstd::cout << v << '\\n';\n}\ntemplate <class Head, class... Tail>\nvoid printel(Head H, Tail... T) {\n\tstd::cout << H << '\\n';\n\tprintel(T...);\n}\nvoid Yes(const bool b = true) { std::cout << (b ? \"Yes\\n\" : \"No\\n\"); }\nvoid No() { std::cout << \"No\\n\"; }\nvoid YES(const bool b = true) { std::cout << (b ? \"YES\\n\" : \"NO\\n\"); }\nvoid NO() { std::cout << \"NO\\n\"; }\nvoid err(const bool b = true) {\n\tif(b) {\n\t\tstd::cout << \"-1\\n\", exit(0);\n\t}\n}\n\n//debug macro\nnamespace debugger {\ntemplate <class T>\nvoid view(const std::vector<T> &a) {\n\tstd::cerr << \"{ \";\n\tfor(const auto &v : a) {\n\t\tstd::cerr << v << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\ntemplate <class T>\nvoid view(const std::vector<std::vector<T>> &a) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &v : a) {\n\t\tstd::cerr << \"\\t\";\n\t\tview(v);\n\t\tstd::cerr << \"\\n\";\n\t}\n\tstd::cerr << \"}\";\n}\ntemplate <class T, class U>\nvoid view(const std::vector<std::pair<T, U>> &a) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &p : a) std::cerr << \"\\t(\" << p.first << \", \" << p.second << \")\\n\";\n\tstd::cerr << \"}\";\n}\ntemplate <class T, class U>\nvoid view(const std::map<T, U> &m) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &p : m) std::cerr << \"\\t[\" << p.first << \"] : \" << p.second << \"\\n\";\n\tstd::cerr << \"}\";\n}\ntemplate <class T, class U>\nvoid view(const std::pair<T, U> &p) { std::cerr << \"(\" << p.first << \", \" << p.second << \")\"; }\ntemplate <class T>\nvoid view(const std::set<T> &s) {\n\tstd::cerr << \"{ \";\n\tfor(auto &v : s) {\n\t\tview(v);\n\t\tstd::cerr << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\n\ntemplate <class T>\nvoid view(const T &e) { std::cerr << e; }\n} // namespace debugger\n#ifdef LOCAL\nvoid debug_out() {}\ntemplate <typename Head, typename... Tail>\nvoid debug_out(Head H, Tail... T) {\n\tdebugger::view(H);\n\tstd::cerr << \", \";\n\tdebug_out(T...);\n}\n#define debug(...) \\\n\tdo { \\\n\t\tstd::cerr << __LINE__ << \" [\" << #__VA_ARGS__ << \"] : [\"; \\\n\t\tdebug_out(__VA_ARGS__); \\\n\t\tstd::cerr << \"\\b\\b]\\n\"; \\\n\t} while(false)\n#else\n#define debug(...) (void(0))\n#endif\n\n// vector macro\ntemplate <class T>\nint lb(const std::vector<T> &a, const T x) { return std::distance((a).begin(), std::lower_bound((a).begin(), (a).end(), (x))); }\ntemplate <class T>\nint ub(const std::vector<T> &a, const T x) { return std::distance((a).begin(), std::upper_bound((a).begin(), (a).end(), (x))); }\ntemplate <class T>\nvoid UNIQUE(std::vector<T> &a) {\n\tstd::sort(a.begin(), a.end());\n\ta.erase(std::unique(a.begin(), a.end()), a.end());\n}\ntemplate <class T>\nstd::vector<T> press(std::vector<T> &a) {\n\tauto res = a;\n\tUNIQUE(res);\n\tfor(auto &v : a)\n\t\tv = lb(res, v);\n\treturn res;\n}\n#define SORTname(a, b, c, ...) c\n#define SORT(...) SORTname(__VA_ARGS__, SORT1, SORT0, ...)(__VA_ARGS__)\n#define SORT0(a) std::sort((a).begin(), (a).end())\n#define SORT1(a, c) std::sort((a).begin(), (a).end(), [](const auto x, const auto y) { return x c y; })\ntemplate <class T>\nvoid ADD(std::vector<T> &a, const T x = 1) {\n\tfor(auto &v : a) v += x;\n}\ntemplate <class T>\nvoid SUB(std::vector<T> &a, const T x = 1) {\n\tfor(auto &v : a) v -= x;\n}\ntemplate <class T>\nvoid MUL(std::vector<T> &a, const T x) {\n\tfor(auto &v : a) v *= x;\n}\ntemplate <class T>\nvoid DIV(std::vector<T> &a, const T x) {\n\tfor(auto &v : a) v /= x;\n}\nstd::vector<std::pair<char, int>> rle(const string &s) {\n\tint n = s.size();\n\tstd::vector<std::pair<char, int>> ret;\n\tfor(int l = 0; l < n;) {\n\t\tint r = l + 1;\n\t\tfor(; r < n and s[l] == s[r]; r++) {}\n\t\tret.emplace_back(s[l], r - l);\n\t\tl = r;\n\t}\n\treturn ret;\n}\ntemplate <class T>\nstd::vector<std::pair<T, int>> rle(const std::vector<T> &v) {\n\tint n = v.size();\n\tstd::vector<std::pair<T, int>> ret;\n\tfor(int l = 0; l < n;) {\n\t\tint r = l + 1;\n\t\tfor(; r < n and v[l] == v[r]; r++) {}\n\t\tret.emplace_back(v[l], r - l);\n\t\tl = r;\n\t}\n\treturn ret;\n}\n\n// math macro\ntemplate <class T, class U>\ninline bool chmin(T &a, const U &b) { return a > b ? a = b, true : false; }\ntemplate <class T, class U>\ninline bool chmax(T &a, const U &b) { return a < b ? a = b, true : false; }\ntemplate <class T>\nT divup(T x, T y) { return (x + y - 1) / y; }\ntemplate <class T>\nT POW(T a, long long n) {\n\tT ret = 1;\n\twhile(n) {\n\t\tif(n & 1) ret *= a;\n\t\ta *= a;\n\t\tn >>= 1;\n\t}\n\treturn ret;\n}\n// modpow\nlong long POW(long long a, long long n, const int mod) {\n\tlong long ret = 1;\n\ta = (a % mod + mod) % mod;\n\twhile(n) {\n\t\tif(n & 1) (ret *= a) %= mod;\n\t\t(a *= a) %= mod;\n\t\tn >>= 1;\n\t}\n\treturn ret;\n}\n\n// others\nstruct fast_io {\n\tfast_io() {\n\t\tios::sync_with_stdio(false);\n\t\tcin.tie(nullptr);\n\t\tcout << fixed << setprecision(15);\n\t}\n} fast_io_;\nconst int inf = 1e9;\nconst ll INF = 1e18;\n#pragma endregion\n\nvoid main_();\n\nint main() {\n\tmain_();\n\treturn 0;\n}\n#line 2 \"d.cpp\"\n\nvoid main_() {\n\tINT(n, m);\n\tDOUBLE(L);\n\tV<> P(n), t(n), v(n);\n\tREP(i, n) { cin >> P[i] >> t[i] >> v[i]; }\n\tV<double> p(n);\n\tREP(i, n) { p[i] = (double)P[i] / 100; }\n\tvector binom(m + 1, 1.0);\n\tdouble mul = 1;\n\tREPS(i, m) {\n\t\tmul *= (m + 1 - i);\n\t\tmul /= i;\n\t\tbinom[i] = mul;\n\t}\n\tREP(i, n) {\n\t\tdouble res = 0;\n\t\tREP(j, m + 1) {\n\t\t\t//iさんが休憩をちょうどj回とって勝つ確率\n\t\t\tdouble prob = binom[j] * pow(p[i], j) * pow(1 - p[i], m - j);\n\t\t\tREP(k, n) {\n\t\t\t\tif(k == i) continue;\n\t\t\t\tdouble tmp_sum = 0;\n\t\t\t\tREP(l, m + 1) {\n\t\t\t\t\tif(t[k] * l * v[k] * v[i] + L * v[i] <= t[i] * j * v[k] * v[i] + L * v[k]) continue;\n\t\t\t\t\tdebug(k, l);\n\t\t\t\t\tdouble tmp_prob = binom[l] * pow(p[k], l) * pow(1 - p[k], m - l);\n\t\t\t\t\ttmp_sum += tmp_prob;\n\t\t\t\t}\n\t\t\t\tprob *= tmp_sum;\n\t\t\t}\n\t\t\tres += prob;\n\t\t}\n\t\tprint(res);\n\t}\n}", "accuracy": 1, "time_ms": 890, "memory_kb": 3984, "score_of_the_acc": -1.2029, "final_rank": 11 }, { "submission_id": "aoj_2303_5843632", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing T = tuple<long long, long long, long long>;\n\nint n, m, l;\nvector<vector<long long>> comb;\nvector<T> v;\n\nvector<long double> solve();\n\nint main() {\n cin >> n >> m >> l;\n for (int i = 0; i < n; ++i) {\n int x, y, z;\n cin >> x >> y >> z;\n v.emplace_back(x, y, z);\n }\n auto res = solve();\n cout << fixed << setprecision(10);\n for (auto &p : res) cout << p << endl;\n return 0;\n}\n\nvector<long double> solve() {\n comb.resize(m + 1);\n comb[0].push_back(1);\n for (int i = 1; i <= m; ++i) {\n comb[i].resize(i + 1);\n for (int j = 0; j < i; ++j)\n comb[i][j] += comb[i - 1][j], comb[i][j + 1] += comb[i - 1][j];\n }\n vector<vector<long double>> prob(n, vector<long double>(m + 1, 0));\n for (int i = 0; i < n; ++i) {\n auto [p, t, s] = v[i];\n for (int j = 0; j <= m; ++j) prob[i][j] = comb[m][j];\n long double now = 1;\n for (int j = 0; j <= m; ++j) {\n prob[i][j] *= now;\n now *= p / 100.0L;\n }\n now = 1;\n for (int j = m; j >= 0; --j) {\n prob[i][j] *= now;\n now *= (100 - p) / 100.0L;\n }\n }\n vector<long double> res(n, 0);\n for (int i = 0; i < n; ++i) {\n auto [pi, ti, si] = v[i];\n for (int itimes = 0; itimes <= m; ++itimes) {\n long double now = prob[i][itimes];\n for (int j = 0; j < n; ++j)\n if (i != j) {\n auto [pj, tj, sj] = v[j];\n long double sum = 0;\n for (int jtimes = 0; jtimes <= m; ++jtimes) {\n long long lef = (itimes * ti - jtimes * tj) * si * sj,\n righ = l * (si - sj);\n if (lef < righ) sum += prob[j][jtimes];\n }\n now *= sum;\n }\n res[i] += now;\n }\n }\n return res;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3600, "score_of_the_acc": -0.0041, "final_rank": 2 }, { "submission_id": "aoj_2303_5238661", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define INF_LL (int64)1e18\n#define INF (int32)1e9\n#define REP(i, n) for(int64 i = 0;i < (n);i++)\n#define FOR(i, a, b) for(int64 i = (a);i < (b);i++)\n#define all(x) x.begin(),x.end()\n#define fs first\n#define sc second\n\nusing int32 = int_fast32_t;\nusing uint32 = uint_fast32_t;\nusing int64 = int_fast64_t;\nusing uint64 = uint_fast64_t;\nusing PII = pair<int32, int32>;\nusing PLL = pair<int64, int64>;\n\nconst double eps = 1e-10;\n\ntemplate<typename A, typename B>inline void chmin(A &a, B b){if(a > b) a = b;}\ntemplate<typename A, typename B>inline void chmax(A &a, B b){if(a < b) a = b;}\n\ntemplate<typename T>\nvector<T> make_v(size_t a){return vector<T>(a);}\n\ntemplate<typename T,typename... Ts>\nauto make_v(size_t a,Ts... ts){\n\treturn vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));\n}\n\ntemplate<typename T,typename U,typename... V>\ntypename enable_if<is_same<T, U>::value!=0>::type\nfill_v(U &u,const V... v){u=U(v...);}\n\ntemplate<typename T,typename U,typename... V>\ntypename enable_if<is_same<T, U>::value==0>::type\nfill_v(U &u,const V... v){\n\tfor(auto &e:u) fill_v<T>(e,v...);\n}\n\ntemplate <typename F>\nclass\n#if defined(__has_cpp_attribute) && __has_cpp_attribute(nodiscard)\n [[nodiscard]]\n#endif // defined(__has_cpp_attribute) && __has_cpp_attribute(nodiscard)\nFixPoint final : private F\n{\npublic:\n template <typename G>\n explicit constexpr FixPoint(G&& g) noexcept\n : F{std::forward<G>(g)}\n {}\n\n template <typename... Args>\n constexpr decltype(auto)\n operator()(Args&&... args) const\n#if !defined(__GNUC__) || defined(__clang__) || __GNUC__ >= 9\n noexcept(noexcept(F::operator()(std::declval<FixPoint>(), std::declval<Args>()...)))\n#endif // !defined(__GNUC__) || defined(__clang__) || __GNUC__ >= 9\n {\n return F::operator()(*this, std::forward<Args>(args)...);\n }\n}; // class FixPoint\n\n\n#if defined(__cpp_deduction_guides)\ntemplate <typename F>\nFixPoint(F&&)\n -> FixPoint<std::decay_t<F>>;\n#endif // defined(__cpp_deduction_guides)\n\n\nnamespace\n{\n template <typename F>\n#if !defined(__has_cpp_attribute) || !__has_cpp_attribute(nodiscard)\n # if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ >= 4)\n__attribute__((warn_unused_result))\n# elif defined(_MSC_VER) && _MSC_VER >= 1700 && defined(_Check_return_)\n_Check_return_\n# endif // defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ >= 4)\n#endif // !defined(__has_cpp_attribute) || !__has_cpp_attribute(nodiscard)\n inline constexpr decltype(auto)\n makeFixPoint(F&& f) noexcept\n {\n return FixPoint<std::decay_t<F>>{std::forward<std::decay_t<F>>(f)};\n }\n} // namespace\n\nvector<int64> dijkstra(const vector<vector<int64>>& G, int s) {\n priority_queue<PLL, vector<PLL>, greater<>> pq;\n vector<int64> d(G.size(), INF_LL);\n d[s] = 0;\n pq.emplace(0, s);\n\n while (pq.size()) {\n int64 v, c;\n tie(c, v) = pq.top(); pq.pop();\n if (d[v] < c) continue;\n for (auto& u : G[v]) {\n if (d[u] > c + 1) {\n d[u] = c + 1;\n pq.emplace(c+1, u);\n }\n }\n }\n return d;\n}\n\ndouble comb(int a, int b) {\n double res = 1;\n REP(i, b) {\n res = res * (a - i) / (b - i);\n }\n return res;\n}\n\nint main(void) {\n cin.tie(0);\n ios::sync_with_stdio(false);\n\n int64 N, M;\n double L;\n cin >> N >> M >> L;\n vector<double> P(N), V(N);\n vector<int64> T(N);\n\n REP(i, N) {\n cin >> P[i] >> T[i] >> V[i];\n P[i] = P[i] / 100;\n }\n\n auto prob = make_v<double>(N, M+1);\n auto time = make_v<double>(N, M+1);\n\n REP(i, N) {\n REP(j, M+1) {\n double prest = 1, pnot = 1;\n REP(k, j) prest *= P[i];\n REP(k, M-j) pnot *= 1-P[i];\n prob[i][j] = comb(M, j) * prest * pnot;\n time[i][j] = L / V[i] + T[i] * j;\n// cout << \"(\" << prob[i][j] << \" \" << time[i][j] << \") \";\n }\n// cout << endl;\n }\n\n REP(i, N) {\n double res = 0;\n REP(j, M+1) {\n double rprob = prob[i][j];\n REP(k, N) {\n if (k == i) continue;\n double sum = 0;\n REP(l, M+1) {\n if (time[k][l] > time[i][j]) {\n sum += prob[k][l];\n }\n }\n// cout << sum << \" \";\n rprob *= sum;\n }\n// cout << rprob << endl;\n res += rprob;\n }\n cout << fixed << setprecision(10) << res << endl;\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3632, "score_of_the_acc": -0.0321, "final_rank": 3 }, { "submission_id": "aoj_2303_5189978", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i, n) for(int i = 0; i < n; i++)\n#define rep2(i, x, n) for(int i = x; i <= n; i++)\n#define rep3(i, x, n) for(int i = x; i >= n; i--)\n#define each(e, v) for(auto &e: v)\n#define pb push_back\n#define eb emplace_back\n#define all(x) x.begin(), x.end()\n#define rall(x) x.rbegin(), x.rend()\n#define sz(x) (int)x.size()\nusing ll = long long;\nusing pii = pair<int, int>;\nusing pil = pair<int, ll>;\nusing pli = pair<ll, int>;\nusing pll = pair<ll, ll>;\nconst int MOD = 1000000007;\n//const int MOD = 998244353;\nconst int inf = (1<<30)-1;\nconst ll INF = (1LL<<60)-1;\ntemplate<typename T> bool chmax(T &x, const T &y) {return (x < y)? (x = y, true) : false;};\ntemplate<typename T> bool chmin(T &x, const T &y) {return (x > y)? (x = y, true) : false;};\n\nstruct io_setup{\n io_setup(){\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n cout << fixed << setprecision(15);\n }\n} io_setup;\n\nint main(){\n int N, M, L; cin >> N >> M >> L;\n\n vector<double> fac(M+1, 0.0);\n rep2(i, 1, M){\n fac[i] = fac[i-1]+log(i);\n }\n\n vector<vector<double>> q(N, vector<double>(M+1, 0.0));\n vector<int> T(N), V(N);\n rep(i, N){\n int P; cin >> P >> T[i] >> V[i];\n if(P == 0){\n q[i][0] = 1.0;\n continue;\n }\n if(P == 100){\n q[i][M] = 1.0;\n continue; \n }\n rep(j, M+1){\n double p = P/100.0;\n q[i][j] = fac[M]-fac[j]-fac[M-j]+j*log(p)+(M-j)*log(1.0-p);\n q[i][j] = exp(q[i][j]);\n }\n }\n\n double EPS = 1e-10;\n\n rep(i, N){\n if(V[i] == 0) {cout << 0.0 << '\\n'; continue;}\n double ans = 0.0;\n rep(j, M+1){\n double tmp = q[i][j], t = double(L)/V[i]+j*T[i];\n rep(k, N){\n if(k == i) continue;\n double s = 0.0;\n rep(l, M+1){\n if(V[k] == 0) {s += q[k][l]; continue;}\n if(t+EPS < double(L)/V[k]+l*T[k]) s += q[k][l];\n }\n tmp *= s;\n }\n ans += tmp;\n }\n\n cout << ans << '\\n';\n }\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 4008, "score_of_the_acc": -0.238, "final_rank": 8 } ]
aoj_2307_cpp
Palindrome Generator A palidrome craftsperson starts to work in the early morning, with the clear air allowing him to polish up his palindromes. On this morning, he is making his pieces to submit to the International Contest for Palindrome Craftspeople. By the way, in order to make palindromes, he uses a special dictionary which contains a set of words and a set of ordered pairs of the words. Any words and any ordered pairs of consecutive words in his palindromes must appear in the dictionary. We already have his dictionary, so let's estimate how long a palindrome he can make. Input The first line in the data set consists of two integers N ( 1 \leq N \leq 100 ) and M ( 0 \leq M \leq 1\,000 ). N describes the number of words in the dictionary and M describes the number of ordered pairs of words. The following N lines describe the words he can use. The i -th line ( 1 -based) contains the word i , which consists of only lower-case letters and whose length is between 1 and 10 , inclusive. The following M lines describe the ordered pairs of consecutive words he can use. The j -th line ( 1 -based) contains two integers X_j and Y_j ( 1 \leq X_j, Y_j \leq N ). X_j describes the ( 1 -based) index of the former word in the palindrome and Y_j describes that of the latter word. Output Print the maximal length of the possible palindrome in a line. If he can make no palidromes, print "0". If he can make arbitrary long palindromes, print "-1". Sample Input 1 2 2 ab ba 1 1 1 2 Output for the Sample Input 1 4 Sample Input 2 2 2 ab ba 1 1 2 2 Output for the Sample Input 2 0 Sample Input 3 2 2 ab a 1 1 1 2 Output for the Sample Input 3 -1
[ { "submission_id": "aoj_2307_10836481", "code_snippet": "#include <cstdio>\n#include <cstring>\n#include <iostream>\n#include <algorithm>\n#include <vector>\n#include <set>\n#include <map>\n#include <cmath>\n#include <sstream>\n#include <iomanip>\n#include <queue>\n#include <ctime>\nusing namespace std;\ntemplate <class T> void checkmin(T &t,T x){if (x < t) t = x;}\ntemplate <class T> void checkmax(T &t,T x){if (x > t) t = x;}\ntemplate <class T> void _checkmin(T &t, T x){if (t == -1) t = x; if (x < t) t = x;}\ntemplate <class T> void _checkmax(T &t, T x){if (t == -1) t = x; if (x > t) t = x;}\ntypedef pair <int,int> PII;\ntypedef pair <double,double> PDD;\ntypedef long long lld;\n#define foreach(it,v) for (__typeof((v).begin()) it = (v).begin();it != (v).end();it++)\n#define DEBUG(a) cout << #a\" = \" << (a) << endl;\n#define DEBUGARR(a, n) for (int i = 0; i < (n); i++) { cout << #a\"[\" << i << \"] = \" << (a)[i] << endl; }\nconst int N = 105;\nconst int M = 300005;\nint n, m, cnt;\nstring s[N], rs[N];\nvector <int> next_arr[N];\nvector <int> pre[N];\nvector <PII> E[M];\nint v[M];\nint st[M];\nint du[M];\nint f[M];\nint fa[M];\nint wei[M];\nint palind[N][11];\nint rpalind[N][11];\nint pos[N][N][11][2];\nset <PII> Set;\n\nint ans;\n\nint abs(int x) {\n\treturn x < 0 ? -x : x;\n}\n\nint find(int x, int y) {\n\treturn Set.count(make_pair(x, y));\n}\n\nvector <PII> getEdge(int i, int j, int l, int d) {\n\tvector <PII> ret;\n\tif (d) {\n\t\tif (l > s[j].size()) return ret;\n\t\tif (find(i, j) && rpalind[j][s[j].size() - l]) {\n\t\t\tret.push_back(make_pair(cnt, l));\n\t\t}\n\t\tforeach (it, next_arr[i]) {\n\t\t\tint len = min(l, (int)s[*it].size());\n\t\t\tif (s[*it].substr(0, len) == rs[j].substr(s[j].size() - l, len)) {\n\t\t\t\tret.push_back(make_pair(pos[*it][j][abs(l - (int)s[*it].size())][l >= s[*it].size()], len * 2));\n\t\t\t}\n\t\t}\n\t} else if (l) {\n\t\tif (l > s[i].size()) return ret;\n\t\tif (find(i, j) && palind[i][s[i].size() - l]) {\n\t\t\tret.push_back(make_pair(cnt, l));\n\t\t}\n\t\tforeach (it, pre[j]) {\n\t\t\tint len = min(l, (int)s[*it].size());\n\t\t\tif (rs[*it].substr(0, len) == s[i].substr(s[i].size() - l, len)) {\n\t\t\t\tret.push_back(make_pair(pos[i][*it][abs(l - (int)s[*it].size())][s[*it].size() >= l], len * 2));\n\t\t\t}\n\t\t}\n\t}\n\treturn ret;\n}\n\nvoid build_graph() {\n\tcnt = 0;\n\tfor (int i = 0; i < n; i++) \n\t\tfor (int j = 0; j < n; j++)\n\t\t\tfor (int k = 0; k < 11; k++)\n\t\t\t\tfor (int l = 0; l < 2; l++)\n\t\t\t\t\tpos[i][j][k][l] = cnt++;\n\tfor (int i = 0; i < n; i++) {\n\t\tfor (int j = 0; j <= s[i].size(); j++) {\n\t\t\tint k = s[i].size() - 1;\n\t\t\tstring _s = s[i];\n\t\t\tint _i = j;\n\t\t\tint _j = k;\n\t\t\tint ok = 1;\n\t\t\twhile (_i < _j) {\n\t\t\t\tif (_s[_i++] != _s[_j--]) {\n\t\t\t\t\tok = 0;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\tpalind[i][j] = ok;\n\t\t\t_i = j;\n\t\t\t_j = k;\n\t\t\t_s = rs[i];\n\t\t\tok = 1;\n\t\t\twhile (_i < _j) {\n\t\t\t\tif (_s[_i++] != _s[_j--]) {\n\t\t\t\t\tok = 0;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\trpalind[i][j] = ok;\n\t\t}\n\t\tif (palind[i][0]) {\n\t\t\tcheckmax(ans, (int)s[i].size());\n\t\t}\n\t}\n\tfor (int i = 0; i < n; i++)\n\t\tfor (int j = 0; j < n; j++)\n\t\t\tfor (int k = 0; k < 11; k++)\n\t\t\t\tfor (int l = 0; l < 2; l++) {\n\t\t\t\t\tE[pos[i][j][k][l]] = getEdge(i, j, k, l);\n\t\t\t\t}\n}\n\nvoid bfs() {\n\tqueue <int> q;\n\tmemset(v, 0, sizeof(v));\n\tmemset(st, 0, sizeof(st));\n\tfor (int i = 0; i <= cnt; i++) v[i] = 0;\n\tfor (int i = 0; i < n; i++) {\n\t\tfor (int j = 0; j < n; j++) {\n\t\t\tint len = min(s[i].size(), s[j].size());\n\t\t\tif (s[i].substr(0, len) == rs[j].substr(0, len)) {\n\t\t\t\tint p = pos[i][j][abs((int)s[i].size() - (int)s[j].size())][s[j].size() >= s[i].size()];\n\t\t\t\tq.push(p);\n\t\t\t\tv[p] = 1;\n\t\t\t\tst[p] = 1;\n\t\t\t\tf[p] = min(s[i].size(), s[j].size()) * 2;\n\t\t\t}\n\t\t}\n\t}\n\twhile (!q.empty()) {\n\t\tint x = q.front();\n\t\tq.pop();\n\t\tforeach (it, E[x]) {\n\t\t\tif (!v[it->first]) {\n\t\t\t\tfa[it->first] = x;\n\t\t\t\tv[it->first] = 1;\n\t\t\t\tq.push(it->first);\n\t\t\t}\n\t\t}\n\t}\n}\n\nvector <int> q;\n\nvoid topsort() {\n\tmemset(du, 0, sizeof(du));\n\tq.clear();\n\tfor (int i = 0; i <= cnt; i++) {\n\t\tif (v[i]) {\n\t\t\tforeach (it, E[i]) {\n\t\t\t\tif (v[it->first]) {\n\t\t\t\t\tdu[it->first]++;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tint head = 0;\n\tfor (int i = 0; i <= cnt; i++) {\n\t\tif (v[i] && !du[i]) {\n\t\t\tq.push_back(i);\n\t\t}\n\t}\n\twhile (head != q.size()) {\n\t\tint x = q[head++];\n\t\tforeach (it, E[x]) {\n\t\t\tif (v[it->first]) {\n\t\t\t\tdu[it->first]--;\n\t\t\t\tif (!du[it->first]) {\n\t\t\t\t\tq.push_back(it->first);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\n\nvoid dp() {\n\tif (!v[cnt] && ans == -1) {\n\t\tputs(\"0\");\n\t} else if (du[cnt]) {\n\t\tputs(\"-1\");\n\t} else {\n\t\tfor (int i = 0; i < q.size(); i++) {\n\t\t\tint x = q[i];\n\t\t\tif (f[x] != -1) {\n\t\t\t\tforeach (it, E[x]) {\n\t\t\t\t\tcheckmax(f[it->first], f[x] + it->second);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tcout << max(f[cnt], ans) << endl;\n\t}\n}\n\nint main(){\n#ifdef cwj\n\tfreopen(\"in\", \"r\", stdin);\n#endif\n\tcin >> n >> m;\n\tfor (int i = 0; i < n; i++) {\n\t\tcin >> s[i];\n\t\trs[i] = s[i];\n\t\treverse(rs[i].begin(), rs[i].end());\n\t}\n\tSet.clear();\n\tmemset(f, 0xFF, sizeof(f));\n\tfor (int i = 0; i < m; i++) {\n\t\tint x, y;\n\t\tcin >> x >> y;\n\t\tx--; y--;\n\t\tSet.insert(make_pair(x, y));\n\t\tnext_arr[x].push_back(y);\n\t\tpre[y].push_back(x);\n\t}\n\tans = -1;\n\tbuild_graph();\n\tbfs();\n\ttopsort();\n\tdp();\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 21912, "score_of_the_acc": -0.1623, "final_rank": 3 }, { "submission_id": "aoj_2307_8985727", "code_snippet": "// time complexity: O(nml) where l = (maximum length of a word)\n\n#include <array>\n#include <string>\n#include <vector>\n#include <iostream>\n#include <algorithm>\nusing namespace std;\n\nstruct edge_base {\n\tint s, t, w;\n};\n\nstruct edge {\n\tint t, w;\n};\n\nbool check(string s, int pos) {\n\tint n = s.size();\n\tbool res = true;\n\tif (pos % 2 == 0) {\n\t\tfor (int i = 0; i < min(pos / 2, n - pos / 2); i++) {\n\t\t\tres = res && (s[pos / 2 - i - 1] == s[pos / 2 + i]);\n\t\t}\n\t} else {\n\t\tfor (int i = 1; i <= min(pos / 2, n - pos / 2 - 1); i++) {\n\t\t\tres = res && (s[pos / 2 - i] == s[pos / 2 + i]);\n\t\t}\n\t}\n\treturn res;\n}\n\nvector<bool> reachable(int N, vector<vector<edge> > G, vector<int> start) {\n\tvector<bool> ans(N, false);\n\tvector<int> st;\n\tfor (int i = 0; i < N; i++) {\n\t\tif (start[i] != -1) {\n\t\t\tans[i] = true;\n\t\t\tst.push_back(i);\n\t\t}\n\t}\n\twhile (!st.empty()) {\n\t\tint u = st.back();\n\t\tst.pop_back();\n\t\tfor (edge e : G[u]) {\n\t\t\tif (!ans[e.t]) {\n\t\t\t\tans[e.t] = true;\n\t\t\t\tst.push_back(e.t);\n\t\t\t}\n\t\t}\n\t}\n\treturn ans;\n}\n\nint longest_path(int N, vector<edge_base> es, vector<int> start, vector<int> goal) {\n\t// step #1. construct graph\n\tvector<vector<edge> > G1(N), G2(N);\n\tfor (edge_base e : es) {\n\t\tG1[e.s].push_back(edge{e.t, e.w});\n\t\tG2[e.t].push_back(edge{e.s, e.w});\n\t}\n\n\t// step #2. reduce graph\n\tvector<bool> reach1 = reachable(N, G1, start);\n\tvector<bool> reach2 = reachable(N, G2, goal);\n\tvector<bool> reach(N);\n\tfor (int i = 0; i < N; i++) {\n\t\treach[i] = reach1[i] && reach2[i];\n\t}\n\tvector<vector<edge> > G3(N);\n\tvector<int> deg(N);\n\tfor (edge_base e : es) {\n\t\tif (reach[e.s] && reach[e.t]) {\n\t\t\tG3[e.s].push_back(edge{e.t, e.w});\n\t\t\tdeg[e.t]++;\n\t\t}\n\t}\n\n\t// step #3. compute in topological order\n\tvector<int> dp(N);\n\tvector<int> st;\n\tfor (int i = 0; i < N; i++) {\n\t\tif (start[i] != -1 && deg[i] == 0) {\n\t\t\tdp[i] = start[i];\n\t\t\tst.push_back(i);\n\t\t}\n\t}\n\tint ans = 0;\n\twhile (!st.empty()) {\n\t\tint u = st.back();\n\t\tst.pop_back();\n\t\tif (goal[u] != -1) {\n\t\t\tans = max(ans, dp[u] + goal[u]);\n\t\t}\n\t\tfor (edge e : G3[u]) {\n\t\t\tdeg[e.t]--;\n\t\t\tdp[e.t] = max(dp[e.t], dp[u] + e.w);\n\t\t\tif (deg[e.t] == 0) {\n\t\t\t\tst.push_back(e.t);\n\t\t\t}\n\t\t}\n\t}\n\tif (deg != vector<int>(N, 0)) {\n\t\treturn -1;\n\t}\n\n\treturn ans;\n}\n\nint solve(int N, int M, vector<string> S, vector<edge_base> es) {\n\t// step #1. preparation\n\tvector<int> L(N);\n\tfor (int i = 0; i < N; i++) {\n\t\tL[i] = S[i].size();\n\t}\n\tvector<vector<int> > G1(N), G2(N);\n\tfor (edge_base e : es) {\n\t\tG1[e.s].push_back(e.t);\n\t\tG2[e.t].push_back(e.s);\n\t}\n\tvector<array<int, 3> > states;\n\tfor (int i = 0; i < N; i++) {\n\t\tfor (int j = 0; j < N; j++) {\n\t\t\tfor (int k = -L[i]; k <= +L[j]; k++) {\n\t\t\t\tstates.push_back({i, j, k});\n\t\t\t}\n\t\t}\n\t}\n\tint Z = states.size();\n\tauto search_state = [&](array<int, 3> v) {\n\t\tint pos = lower_bound(states.begin(), states.end(), v) - states.begin();\n\t\tif (pos == Z || states[pos] != v) {\n\t\t\treturn -1;\n\t\t}\n\t\treturn pos;\n\t};\n\n\t// step #2. compute all \"start/goal states\"\n\tvector<int> start(Z, -1), goal(Z, -1);\n\tfor (int i = 0; i < Z; i++) {\n\t\tif (states[i][0] == states[i][1] && check(S[states[i][0]], -states[i][2] + L[states[i][0]])) {\n\t\t\tstart[i] = L[states[i][0]];\n\t\t}\n\t\tif (states[i][2] == 0) {\n\t\t\tgoal[i] = 0;\n\t\t}\n\t}\n\n\t// step #3. construct graph\n\tvector<edge_base> eg;\n\tfor (int i = 0; i < Z; i++) {\n\t\tarray<int, 3> s = states[i];\n\t\t// add to left\n\t\tif (s[2] >= 0) {\n\t\t\tfor (int j : G2[s[0]]) {\n\t\t\t\tbool ok = true;\n\t\t\t\tfor (int k = 0; k < s[2] && k < L[j]; k++) {\n\t\t\t\t\tok = ok && (S[s[1]][L[s[1]] - s[2] + k] == S[j][L[j] - k - 1]);\n\t\t\t\t}\n\t\t\t\tif (ok) {\n\t\t\t\t\tint z = search_state({j, s[1], s[2] - L[j]});\n\t\t\t\t\teg.push_back(edge_base{i, z, L[j]});\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\t// add to right\n\t\tif (s[2] <= 0) {\n\t\t\tfor (int j : G1[s[1]]) {\n\t\t\t\tbool ok = true;\n\t\t\t\tfor (int k = 0; k < -s[2] && k < L[j]; k++) {\n\t\t\t\t\tok = ok && (S[s[0]][-s[2] - k - 1] == S[j][k]);\n\t\t\t\t}\n\t\t\t\tif (ok) {\n\t\t\t\t\tint z = search_state({s[0], j, s[2] + L[j]});\n\t\t\t\t\teg.push_back(edge_base{i, z, L[j]});\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\t// step #4. compute answer\n\tint ans = longest_path(Z, eg, start, goal);\n\n\treturn ans;\n}\n\nint main() {\n\tint N, M;\n\tcin >> N >> M;\n\tvector<string> S(N);\n\tfor (int i = 0; i < N; i++) {\n\t\tcin >> S[i];\n\t}\n\tvector<edge_base> es(M);\n\tfor (int i = 0; i < M; i++) {\n\t\tcin >> es[i].s >> es[i].t;\n\t\tes[i].s--; es[i].t--;\n\t}\n\tint ans = solve(N, M, S, es);\n\tcout << ans << endl;\n\treturn 0;\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 40224, "score_of_the_acc": -0.3698, "final_rank": 7 }, { "submission_id": "aoj_2307_8487347", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define rep(i, n) for (int i = 0; i < (n); i++)\n#define per(i, n) for (int i = (n)-1; i >= 0; i--)\n#define rep2(i, l, r) for (int i = (l); i < (r); i++)\n#define per2(i, l, r) for (int i = (r)-1; i >= (l); i--)\n#define each(e, x) for (auto &e : x)\n#define all(x) begin(x), end(x)\n#define rall(x) rbegin(x), rend(x)\n#define sz(x) (int)x.size()\n#define eb emplace_back\n#define MM << ' ' <<\n#define TT template <typename T>\nusing ll = long long;\nusing pii = pair<int, int>;\nusing pll = pair<ll, ll>;\n\nTT bool chmin(T &x, T y) { return x > y ? (x = y, true) : false; }\nTT bool chmax(T &x, T y) { return x < y ? (x = y, true) : false; }\n\nTT using minheap = priority_queue<T, vector<T>, greater<T>>;\nTT using maxheap = priority_queue<T>;\n\nTT void print(vector<T> v, T x = 0) {\n int n = sz(v);\n rep(i, n) cout << v[i] + x << (i == n - 1 ? '\\n' : ' ');\n if (n == 0) cout << '\\n';\n}\n\nTT void printn(vector<T> v, T x = 0) {\n int n = sz(v);\n rep(i, n) cout << v[i] + x << '\\n';\n}\n\nTT void rearrange(vector<T> &v) {\n sort(all(v));\n v.erase(unique(all(v)), end(v));\n}\n\nTT int lb(vector<T> &v, T x) { return lower_bound(all(v), x) - begin(v); }\nTT int ub(vector<T> &v, T x) { return upper_bound(all(v), x) - begin(v); }\n\nconst int inf = (1 << 30) - 1;\nconst ll INF = (1LL << 60) - 1;\n\nstruct io_setup {\n io_setup() {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n cout << fixed << setprecision(15);\n }\n} io_setup;\n\nvector<int> scc(vector<vector<int>> es) {\n int n = sz(es);\n vector<vector<int>> rs(n);\n rep(i, n) each(e, es[i]) rs[e].eb(i);\n\n vector<int> comp(n, -1);\n vector<int> vs;\n\n auto dfs = [&](auto &&dfs, int now) -> void {\n if (comp[now] != -1) return;\n comp[now] = 1;\n each(e, es[now]) dfs(dfs, e);\n vs.eb(now);\n };\n\n auto rdfs = [&](auto &&rdfs, int now, int col) -> void {\n if (comp[now] != -1) return;\n comp[now] = col;\n each(e, rs[now]) rdfs(rdfs, e, col);\n };\n\n rep(i, n) dfs(dfs, i);\n fill(all(comp), -1);\n reverse(all(vs));\n int cnt = 0;\n each(e, vs) {\n if (comp[e] == -1) rdfs(rdfs, e, cnt++);\n }\n return comp;\n}\n\nint main() {\n int N, M;\n cin >> N >> M;\n\n vector<string> S(N + 1);\n rep(i, N) cin >> S[i];\n\n vector<vector<int>> adj(N + 1, vector<int>(N + 1, 0));\n rep(i, M) {\n int u, v;\n cin >> u >> v;\n u--, v--;\n adj[u][v] = 1;\n }\n\n rep(i, N + 1) adj[i][N] = adj[N][i] = 1;\n\n int L = 11;\n\n int V = 2 * (N + 1) * (N + 1) * L;\n\n auto tupleToInt = [&](int i, int j, int f, int l) {\n int ret = 0;\n ret *= N + 1, ret += i;\n ret *= N + 1, ret += j;\n ret *= 2, ret += f;\n ret *= L, ret += l;\n return ret;\n };\n\n vector<vector<int>> es(V + 1);\n vector<vector<ll>> weight(V + 1);\n\n rep(i, N + 1) rep(j, N + 1) rep(f, 2) rep(l, L) {\n int u = tupleToInt(i, j, f, l);\n if (f == 0) {\n if (l > sz(S[i])) continue;\n rep(k, N) {\n if (!adj[k][j]) continue;\n int m = sz(S[k]);\n if (m <= l) {\n string X = S[i];\n reverse(all(X));\n if (X.substr(l - m, m) != S[k]) continue;\n int v = tupleToInt(i, k, 0, l - m);\n es[u].eb(v);\n weight[u].eb(m);\n } else {\n string X = S[i];\n reverse(all(X));\n if (X.substr(0, l) != S[k].substr(m - l, l)) continue;\n int v = tupleToInt(i, k, 1, m - l);\n es[u].eb(v);\n weight[u].eb(l);\n }\n }\n } else {\n if (l > sz(S[j])) continue;\n rep(k, N) {\n if (!adj[i][k]) continue;\n int m = sz(S[k]);\n if (m < l) {\n string X = S[k];\n reverse(all(X));\n if (S[j].substr(l - m, m) != X) continue;\n int v = tupleToInt(k, j, 1, l - m);\n es[u].eb(v);\n weight[u].eb(m);\n } else {\n string X = S[k];\n reverse(all(X));\n if (S[j].substr(0, l) != X.substr(m - l, l)) continue;\n int v = tupleToInt(k, j, 0, m - l);\n es[u].eb(v);\n weight[u].eb(l);\n }\n }\n }\n }\n\n auto comp = scc(es);\n int K = sz(comp);\n\n vector<vector<pll>> dagEs(K);\n vector<int> deg(K, 0);\n rep(i, V + 1) {\n int T = sz(es[i]);\n assert(sz(weight[i]) == T);\n rep(j, T) {\n int u = comp[i], v = comp[es[i][j]];\n if (u == v) continue;\n ll w = weight[i][j];\n dagEs[u].eb(v, w);\n deg[v]++;\n }\n }\n\n vector<int> cnt(K, 0);\n rep(i, V + 1) cnt[comp[i]]++;\n\n vector<ll> d(K, -INF);\n queue<int> que;\n d[comp[tupleToInt(N, N, 0, 0)]] = 0;\n\n rep(i, K) {\n if (deg[i] == 0) que.emplace(i);\n }\n\n while (!empty(que)) {\n int i = que.front();\n if (cnt[i] >= 2 && d[i] != -INF) d[i] = INF;\n que.pop();\n for (auto [j, w] : dagEs[i]) {\n if (d[i] == INF) {\n chmax(d[j], INF);\n } else if (d[i] != -INF) {\n chmax(d[j], d[i] + w);\n }\n if (--deg[j] == 0) que.emplace(j);\n }\n }\n\n ll ans = -INF;\n\n auto isPalindrome = [](string S) {\n auto T = S;\n reverse(all(T));\n return S == T;\n };\n\n rep(i, N + 1) rep(j, N + 1) rep(f, 2) rep(l, L) {\n int v = comp[tupleToInt(i, j, f, l)];\n // if (d[v] == 4) cout << i MM j MM f MM l << endl;\n if (!adj[i][j]) continue;\n\n if (f == 0) {\n if (l > sz(S[i])) continue;\n if (isPalindrome(S[i].substr(sz(S[i]) - l, l))) {\n chmax(ans, d[v] * 2 + l); //\n }\n } else {\n if (l > sz(S[j])) continue;\n if (isPalindrome(S[j].substr(0, l))) {\n chmax(ans, d[v] * 2 + l); //\n }\n }\n }\n\n cout << (ans >= INF ? -1 : ans) << '\\n';\n}", "accuracy": 1, "time_ms": 110, "memory_kb": 44788, "score_of_the_acc": -0.4254, "final_rank": 9 }, { "submission_id": "aoj_2307_8487341", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define rep(i, n) for (int i = 0; i < (n); i++)\n#define per(i, n) for (int i = (n)-1; i >= 0; i--)\n#define rep2(i, l, r) for (int i = (l); i < (r); i++)\n#define per2(i, l, r) for (int i = (r)-1; i >= (l); i--)\n#define each(e, x) for (auto &e : x)\n#define all(x) begin(x), end(x)\n#define rall(x) rbegin(x), rend(x)\n#define sz(x) (int)x.size()\n#define eb emplace_back\n#define MM << ' ' <<\n#define TT template <typename T>\nusing ll = long long;\nusing pii = pair<int, int>;\nusing pll = pair<ll, ll>;\n\nTT bool chmin(T &x, T y) { return x > y ? (x = y, true) : false; }\nTT bool chmax(T &x, T y) { return x < y ? (x = y, true) : false; }\n\nTT using minheap = priority_queue<T, vector<T>, greater<T>>;\nTT using maxheap = priority_queue<T>;\n\nTT void print(vector<T> v, T x = 0) {\n int n = sz(v);\n rep(i, n) cout << v[i] + x << (i == n - 1 ? '\\n' : ' ');\n if (n == 0) cout << '\\n';\n}\n\nTT void printn(vector<T> v, T x = 0) {\n int n = sz(v);\n rep(i, n) cout << v[i] + x << '\\n';\n}\n\nTT void rearrange(vector<T> &v) {\n sort(all(v));\n v.erase(unique(all(v)), end(v));\n}\n\nTT int lb(vector<T> &v, T x) { return lower_bound(all(v), x) - begin(v); }\nTT int ub(vector<T> &v, T x) { return upper_bound(all(v), x) - begin(v); }\n\nconst int inf = (1 << 30) - 1;\nconst ll INF = (1LL << 60) - 1;\n\nstruct io_setup {\n io_setup() {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n cout << fixed << setprecision(15);\n }\n} io_setup;\n\nvector<int> scc(vector<vector<int>> es) {\n int n = sz(es);\n vector<vector<int>> rs(n);\n rep(i, n) each(e, es[i]) rs[e].eb(i);\n\n vector<int> comp(n, -1);\n vector<int> vs;\n\n auto dfs = [&](auto &&dfs, int now) -> void {\n if (comp[now] != -1) return;\n comp[now] = 1;\n each(e, es[now]) dfs(dfs, e);\n vs.eb(now);\n };\n\n auto rdfs = [&](auto &&rdfs, int now, int col) -> void {\n if (comp[now] != -1) return;\n comp[now] = col;\n each(e, rs[now]) rdfs(rdfs, e, col);\n };\n\n rep(i, n) dfs(dfs, i);\n fill(all(comp), -1);\n reverse(all(vs));\n int cnt = 0;\n each(e, vs) {\n if (comp[e] == -1) rdfs(rdfs, e, cnt++);\n }\n return comp;\n}\n\nint main() {\n int N, M;\n cin >> N >> M;\n\n vector<string> S(N + 1);\n rep(i, N) cin >> S[i];\n\n vector<vector<int>> adj(N + 1, vector<int>(N + 1, 0));\n rep(i, M) {\n int u, v;\n cin >> u >> v;\n u--, v--;\n adj[u][v] = 1;\n }\n\n rep(i, N + 1) adj[i][N] = adj[N][i] = 1;\n\n int L = 10;\n\n int V = 2 * (N + 1) * (N + 1) * L;\n\n auto tupleToInt = [&](int i, int j, int f, int l) {\n int ret = 0;\n ret *= N + 1, ret += i;\n ret *= N + 1, ret += j;\n ret *= 2, ret += f;\n ret *= L, ret += l;\n return ret;\n };\n\n vector<vector<int>> es(V + 1);\n vector<vector<ll>> weight(V + 1);\n\n rep(i, N + 1) rep(j, N + 1) rep(f, 2) rep(l, L) {\n int u = tupleToInt(i, j, f, l);\n if (f == 0) {\n if (l > sz(S[i])) continue;\n rep(k, N) {\n if (!adj[k][j]) continue;\n int m = sz(S[k]);\n if (m <= l) {\n string X = S[i];\n reverse(all(X));\n if (X.substr(l - m, m) != S[k]) continue;\n int v = tupleToInt(i, k, 0, l - m);\n es[u].eb(v);\n weight[u].eb(m);\n } else {\n string X = S[i];\n reverse(all(X));\n if (X.substr(0, l) != S[k].substr(m - l, l)) continue;\n int v = tupleToInt(i, k, 1, m - l);\n es[u].eb(v);\n weight[u].eb(l);\n }\n }\n } else {\n if (l > sz(S[j])) continue;\n rep(k, N) {\n if (!adj[i][k]) continue;\n int m = sz(S[k]);\n if (m < l) {\n string X = S[k];\n reverse(all(X));\n if (S[j].substr(l - m, m) != X) continue;\n int v = tupleToInt(k, j, 1, l - m);\n es[u].eb(v);\n weight[u].eb(m);\n } else {\n string X = S[k];\n reverse(all(X));\n if (S[j].substr(0, l) != X.substr(m - l, l)) continue;\n int v = tupleToInt(k, j, 0, m - l);\n es[u].eb(v);\n weight[u].eb(l);\n }\n }\n }\n }\n\n auto comp = scc(es);\n int K = sz(comp);\n\n vector<vector<pll>> dagEs(K);\n vector<int> deg(K, 0);\n rep(i, V + 1) {\n int T = sz(es[i]);\n assert(sz(weight[i]) == T);\n rep(j, T) {\n int u = comp[i], v = comp[es[i][j]];\n if (u == v) continue;\n ll w = weight[i][j];\n dagEs[u].eb(v, w);\n deg[v]++;\n }\n }\n\n vector<int> cnt(K, 0);\n rep(i, V + 1) cnt[comp[i]]++;\n\n vector<ll> d(K, -INF);\n queue<int> que;\n d[comp[tupleToInt(N, N, 0, 0)]] = 0;\n\n rep(i, K) {\n if (deg[i] == 0) que.emplace(i);\n }\n\n while (!empty(que)) {\n int i = que.front();\n if (cnt[i] >= 2 && d[i] != -INF) d[i] = INF;\n que.pop();\n for (auto [j, w] : dagEs[i]) {\n if (d[i] == INF) {\n chmax(d[j], INF);\n } else if (d[i] != -INF) {\n chmax(d[j], d[i] + w);\n }\n if (--deg[j] == 0) que.emplace(j);\n }\n }\n\n ll ans = -INF;\n\n auto isPalindrome = [](string S) {\n auto T = S;\n reverse(all(T));\n return S == T;\n };\n\n rep(i, N + 1) rep(j, N + 1) rep(f, 2) rep(l, L) {\n int v = comp[tupleToInt(i, j, f, l)];\n // if (d[v] == 4) cout << i MM j MM f MM l << endl;\n if (!adj[i][j]) continue;\n\n if (f == 0) {\n if (l > sz(S[i])) continue;\n if (isPalindrome(S[i].substr(sz(S[i]) - l, l))) {\n chmax(ans, d[v] * 2 + l); //\n }\n } else {\n if (l > sz(S[j])) continue;\n if (isPalindrome(S[j].substr(0, l))) {\n chmax(ans, d[v] * 2 + l); //\n }\n }\n }\n\n cout << (ans >= INF ? -1 : ans) << '\\n';\n}", "accuracy": 0.15306122448979592, "time_ms": 90, "memory_kb": 31768, "score_of_the_acc": -0.2814, "final_rank": 20 }, { "submission_id": "aoj_2307_8416865", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n// Normal Vaeiables\nint N; string S[1009];\nint M, X[1009], Y[1009];\nbool Valid[109][109];\n\n// Dynamic Programming\nint Time = -1;\nint Last = -1;\nint dp[109][109][2][19];\ntuple<int, int, int, int> NexL[109][109][19];\ntuple<int, int, int, int> NexR[109][109][19];\npriority_queue<tuple<int, int, int, int, int>, vector<tuple<int, int, int, int, int>>, greater<tuple<int, int, int, int, int>>> Q;\n\nvoid Get_NexL(int new_, int old_, int nag) {\n string str1 = S[new_]; reverse(str1.begin(), str1.end());\n string str2 = S[old_].substr((int)S[old_].size() - nag, nag);\n if (str1.size() < str2.size()) str2 = str2.substr(0, str1.size());\n if (str1.size() > str2.size()) str1 = str1.substr(0, str2.size());\n\n // Invalid Case\n if (str1 != str2) {\n NexL[new_][old_][nag] = make_tuple(-1, -1, -1, -1);\n return;\n }\n\n // Valid Case\n int Len1 = S[new_].size();\n int Len2 = nag;\n if (Len1 >= Len2) NexL[new_][old_][nag] = make_tuple(new_, old_, 0, Len1 - Len2);\n if (Len1 < Len2) NexL[new_][old_][nag] = make_tuple(new_, old_, 1, Len2 - Len1);\n}\n\nvoid Get_NexR(int old_, int new_, int nag) {\n string str2 = S[old_].substr(0, nag); reverse(str2.begin(), str2.end());\n string str1 = S[new_];\n if (str1.size() < str2.size()) str2 = str2.substr(0, str1.size());\n if (str1.size() > str2.size()) str1 = str1.substr(0, str2.size());\n\n // Invalid Case\n if (str1 != str2) {\n NexR[old_][new_][nag] = make_tuple(-1, -1, -1, -1);\n return;\n }\n\n // Valid Case\n int Len1 = nag;\n int Len2 = S[new_].size();\n if (Len1 >= Len2) NexR[old_][new_][nag] = make_tuple(old_, new_, 0, Len1 - Len2);\n if (Len1 < Len2) NexR[old_][new_][nag] = make_tuple(old_, new_, 1, Len2 - Len1);\n}\n\nvoid Update(int pos1, int pos2, int typ, int nag) {\n // Left is Longer\n if (typ == 0) {\n for (int i = 1; i <= N; i++) {\n if (Valid[pos2][i] == false) continue;\n if (NexR[pos1][i][nag] == make_tuple(-1, -1, -1, -1)) continue;\n int nex1 = get<0>(NexR[pos1][i][nag]);\n int nex2 = get<1>(NexR[pos1][i][nag]);\n int nexT = get<2>(NexR[pos1][i][nag]);\n int nexN = get<3>(NexR[pos1][i][nag]);\n if (dp[nex1][nex2][nexT][nexN] < dp[pos1][pos2][typ][nag] + (int)S[i].size()) {\n dp[nex1][nex2][nexT][nexN] = dp[pos1][pos2][typ][nag] + (int)S[i].size();\n Q.push(make_tuple(dp[nex1][nex2][nexT][nexN], nex1, nex2, nexT, nexN));\n if (nexN == 0) Last = clock() - Time;\n }\n }\n }\n\n // Right is Longer\n if (typ == 1) {\n for (int i = 1; i <= N; i++) {\n if (Valid[i][pos1] == false) continue;\n if (NexL[i][pos2][nag] == make_tuple(-1, -1, -1, -1)) continue;\n int nex1 = get<0>(NexL[i][pos2][nag]);\n int nex2 = get<1>(NexL[i][pos2][nag]);\n int nexT = get<2>(NexL[i][pos2][nag]);\n int nexN = get<3>(NexL[i][pos2][nag]);\n if (dp[nex1][nex2][nexT][nexN] < dp[pos1][pos2][typ][nag] + (int)S[i].size()) {\n dp[nex1][nex2][nexT][nexN] = dp[pos1][pos2][typ][nag] + (int)S[i].size();\n Q.push(make_tuple(dp[nex1][nex2][nexT][nexN], nex1, nex2, nexT, nexN));\n if (nexN == 0) Last = clock() - Time;\n }\n }\n }\n}\n\nint main() {\n // Step 1. Input\n cin >> N >> M;\n for (int i = 1; i <= N; i++) cin >> S[i];\n for (int i = 1; i <= M; i++) cin >> X[i] >> Y[i];\n for (int i = 1; i <= M; i++) Valid[X[i]][Y[i]] = true;\n\n // Step 2. DP Init 1\n for (int i = 1; i <= N; i++) {\n for (int j = 1; j <= N; j++) {\n for (int k = 0; k < 2; k++) {\n for (int l = 0; l <= 10; l++) dp[i][j][k][l] = -(1 << 30);\n }\n }\n }\n\n // Step 3. DP Init 2\n for (int i = 1; i <= N; i++) {\n for (int j = 0; j <= (int)S[i].size(); j++) {\n string str1 = S[i].substr(0, j); reverse(str1.begin(), str1.end());\n string str2 = S[i].substr(j, S[i].size() - j);\n if (str1.size() < str2.size()) str2 = str2.substr(0, str1.size());\n if (str1.size() > str2.size()) str1 = str1.substr(0, str2.size());\n if (str1 != str2) continue;\n int Len1 = j;\n int Len2 = S[i].size() - j;\n int Type = (Len1 >= Len2 ? 0 : 1);\n int Naga = abs(Len1 - Len2);\n dp[i][i][Type][Naga] = S[i].size();\n Q.push(make_tuple((int)S[i].size(), i, i, Type, Naga));\n }\n }\n\n // Step 3. DP Init 3\n for (int i = 1; i <= N; i++) {\n for (int j = 0; j <= (int)S[i].size() - 1; j++) {\n string str1 = S[i].substr(0, j); reverse(str1.begin(), str1.end());\n string str2 = S[i].substr(j + 1, S[i].size() - (j + 1));\n if (str1.size() < str2.size()) str2 = str2.substr(0, str1.size());\n if (str1.size() > str2.size()) str1 = str1.substr(0, str2.size());\n if (str1 != str2) continue;\n int Len1 = j;\n int Len2 = S[i].size() - (j + 1);\n int Type = (Len1 >= Len2 ? 0 : 1);\n int Naga = abs(Len1 - Len2);\n dp[i][i][Type][Naga] = S[i].size();\n Q.push(make_tuple((int)S[i].size(), i, i, Type, Naga));\n }\n }\n\n\n // Step 4. DP Init 3\n for (int i = 1; i <= N; i++) {\n for (int j = 1; j <= N; j++) {\n for (int k = 0; k <= (int)S[j].size(); k++) Get_NexL(i, j, k);\n for (int k = 0; k <= (int)S[i].size(); k++) Get_NexR(i, j, k);\n }\n }\n\n // Step 5. DP Start\n Time = clock();\n while (!Q.empty()) {\n int curr = get<0>(Q.top());\n int pos1 = get<1>(Q.top());\n int pos2 = get<2>(Q.top());\n int typ = get<3>(Q.top());\n int nag = get<4>(Q.top()); Q.pop();\n if (curr != dp[pos1][pos2][typ][nag]) continue;\n\n // Update\n Update(pos1, pos2, typ, nag);\n if (clock() - Time >= 45 * CLOCKS_PER_SEC / 10) break;\n }\n\n // Step 6. Output\n if (Last >= 30 * CLOCKS_PER_SEC / 10) cout << \"-1\" << endl;\n else {\n int Answer = 0;\n for (int i = 1; i <= N; i++) {\n for (int j = 1; j <= N; j++) {\n Answer = max(Answer, dp[i][j][0][0]);\n Answer = max(Answer, dp[i][j][1][0]);\n }\n }\n cout << Answer << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 4500, "memory_kb": 12304, "score_of_the_acc": -1.0549, "final_rank": 12 }, { "submission_id": "aoj_2307_7134603", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntemplate<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }\ntemplate<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }\n#define all(x) (x).begin(),(x).end()\n#define fi first\n#define se second\n#define mp make_pair\n#define si(x) int(x.size())\nconst int mod=998244353,MAX=210005,INF=1<<28;\n\nint V,cmp[MAX];\nvector<int> G[MAX],rG[MAX],vs;//vsがトポソの逆順になってる\nbool used[MAX];\n\nint get_id(int i,bool f){\n return 2*i+f;\n}\n\nvoid add_edge(int from,int to){\n G[from].push_back(to);\n rG[to].push_back(from);\n}\n\nvoid either(int i,bool f,int j,bool g){\n add_edge(get_id(i,!f),get_id(j,g));\n add_edge(get_id(j,!g),get_id(i,f));\n //add_edge(2*i+(!f),2*j+g);\n //add_edge(2*j+(!g),2*i+f);\n}\n\nvoid imply(int i,bool f,int j,bool g){\n either(i,!f,j,g);\n}\n//iがfならばjがg\n\nvoid must(int i,bool f){\n either(i,f,i,f);\n}\n\nvoid DFS(int v){\n used[v]=1;\n for(int i=0;i<G[v].size();i++){\n if(used[G[v][i]]==0) DFS(G[v][i]);\n }\n vs.push_back(v);\n}\n\nvoid rDFS(int v,int k){\n used[v]=1;\n cmp[v]=k;\n for(int i=0;i<rG[v].size();i++){\n if(used[rG[v][i]]==0) rDFS(rG[v][i],k);\n }\n}\n\nint scc(){\n memset(used,0,sizeof(used));\n vs.clear();\n for(int v=0;v<V;v++){\n if(used[v]==0) DFS(v);\n }\n \n memset(used,0,sizeof(used));\n int k=0;\n for(int i=vs.size()-1;i>=0;i--){\n if(used[vs[i]]==0) rDFS(vs[i],k++);\n }\n return k;\n}\n\nvoid init(int sz){\n V=sz;\n for(int i=0;i<V;i++){\n cmp[i]=0;\n G[i].clear();\n rG[i].clear();\n used[i]=false;\n }\n vs.clear();\n}\n\nbool E[105][105];\n\nvector<pair<int,int>> sv[MAX],T[MAX];\n\nbool cycle[MAX];\n\nint dp[MAX];\n\nint main(){\n \n std::ifstream in(\"text.txt\");\n std::cin.rdbuf(in.rdbuf());\n cin.tie(0);\n ios::sync_with_stdio(false);\n \n int N,M;cin>>N>>M;\n auto idx=[&](int l,int r,bool left,int len){\n int res=0;\n res+=l*N*21;\n res+=r*21;\n if(len==0){\n \n }else if(left){\n res+=len;\n }else{\n res+=10+len;\n }\n return res;\n };\n \n vector<string> S(N);\n for(int i=0;i<N;i++) cin>>S[i];\n \n for(int i=0;i<M;i++){\n int a,b;cin>>a>>b;a--;b--;\n E[a][b]=true;\n }\n \n for(int l=0;l<N;l++){\n for(int r=0;r<N;r++){\n for(int lef=0;lef<2;lef++){\n for(int len=0;len<=10;len++){\n if(lef==0&&len==0) continue;\n if(lef==1){\n if(len>si(S[l])) continue;\n }else{\n if(len>si(S[r])) continue;\n }\n \n int a=idx(l,r,lef,len);\n \n if(len==0){\n for(int x=0;x<N;x++){\n if(E[x][l]){\n int b=idx(x,r,1,si(S[x]));\n add_edge(a,b);\n sv[a].push_back(mp(b,si(S[x])));\n }\n }\n }else if(lef){\n for(int x=0;x<N;x++){\n if(E[r][x]){\n bool ok=true;\n for(int i=0;i<min(len,si(S[x]));i++){\n ok&=(S[l][len-1-i]==S[x][i]);\n }\n if(ok){\n if(len>=si(S[x])){\n int b=idx(l,x,1,len-si(S[x]));\n add_edge(a,b);\n sv[a].push_back(mp(b,si(S[x])));\n }else{\n int b=idx(l,x,0,si(S[x])-len);\n add_edge(a,b);\n sv[a].push_back(mp(b,si(S[x])));\n }\n }\n }\n }\n }else{\n for(int x=0;x<N;x++){\n if(E[x][l]){\n bool ok=true;\n for(int i=0;i<min(len,si(S[x]));i++){\n ok&=(S[x][si(S[x])-1-i]==S[r][si(S[r])-len+i]);\n }\n if(ok){\n if(len>si(S[x])){\n int b=idx(x,r,0,len-si(S[x]));\n add_edge(a,b);\n sv[a].push_back(mp(b,si(S[x])));\n }else{\n int b=idx(x,r,1,si(S[x])-len);\n add_edge(a,b);\n sv[a].push_back(mp(b,si(S[x])));\n }\n }\n }\n }\n }\n }\n }\n }\n }\n int st=N*N*21;\n for(int x=0;x<N;x++){\n for(int k=0;k<=si(S[x]);k++){\n bool ok=true;\n for(int i=0;i<min(k,si(S[x])-k);i++){\n ok&=(S[x][k-1-i]==S[x][k+i]);\n }\n if(ok){\n int a=st;\n if(k<=si(S[x])-k){\n int b=idx(x,x,0,si(S[x])-k-k);\n add_edge(a,b);\n sv[a].push_back(mp(b,si(S[x])));\n }else{\n int b=idx(x,x,1,k-(si(S[x])-k));\n add_edge(a,b);\n sv[a].push_back(mp(b,si(S[x])));\n }\n }\n }\n for(int k=0;k<si(S[x]);k++){\n bool ok=true;\n for(int i=0;i<min(k,si(S[x])-1-k);i++){\n ok&=(S[x][k-1-i]==S[x][k+1+i]);\n }\n if(ok){\n int a=st;\n if(k<=si(S[x])-1-k){\n int b=idx(x,x,0,si(S[x])-1-k-k);\n add_edge(a,b);\n sv[a].push_back(mp(b,si(S[x])));\n }else{\n int b=idx(x,x,1,k-(si(S[x])-1-k));\n add_edge(a,b);\n sv[a].push_back(mp(b,si(S[x])));\n }\n }\n }\n }\n V=st+1;\n int K=scc();\n vector<int> sz(K);\n for(int i=0;i<=st;i++){\n sz[cmp[i]]++;\n }\n \n for(int i=0;i<K;i++){\n if(sz[i]>=2) cycle[i]=true;\n }\n \n for(int i=0;i<V;i++){\n for(auto [j,len]:sv[i]){\n if(cmp[i]!=cmp[j]){\n assert(cmp[i]<cmp[j]);\n T[cmp[i]].push_back(mp(cmp[j],len));\n }\n }\n }\n \n for(int i=0;i<K;i++) dp[i]=-INF;\n dp[cmp[st]]=0;\n \n for(int i=0;i<K;i++){\n if(dp[i]>=0){\n if(cycle[i]) dp[i]=INF;\n \n for(auto [j,len]:T[i]){\n chmax(dp[j],dp[i]+len);\n }\n }\n }\n \n int ans=-INF;\n \n for(int l=0;l<N;l++){\n for(int r=0;r<N;r++){\n int a=idx(l,r,0,0);\n chmax(ans,dp[cmp[a]]);\n }\n }\n \n if(ans<0) cout<<0<<\"\\n\";\n else if(ans>=INF) cout<<-1<<\"\\n\";\n else cout<<ans<<\"\\n\";\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 40996, "score_of_the_acc": -0.3736, "final_rank": 8 }, { "submission_id": "aoj_2307_6024494", "code_snippet": "//#pragma GCC optimize(\"O3\")\n//#pragma GCC optimize(\"unroll-loops\")\n#include<iostream>\n#include<string>\n#include<cstdio>\n#include<vector>\n#include<cmath>\n#include<algorithm>\n#include<functional>\n#include<iomanip>\n#include<queue>\n#include<ciso646>\n#include<random>\n#include<map>\n#include<set>\n#include<bitset>\n#include<stack>\n#include<unordered_map>\n#include<unordered_set>\n#include<utility>\n#include<cassert>\n#include<complex>\n#include<numeric>\n#include<array>\nusing namespace std;\n\n//#define int long long\ntypedef long long ll;\n\ntypedef unsigned long long ul;\ntypedef unsigned int ui;\nconst ll mod = 998244353;\nconst ll INF = mod * mod;\ntypedef pair<int, int>P;\n\n#define rep(i,n) for(int i=0;i<n;i++)\n#define per(i,n) for(int i=n-1;i>=0;i--)\n#define Rep(i,sta,n) for(int i=sta;i<n;i++)\n#define rep1(i,n) for(int i=1;i<=n;i++)\n#define per1(i,n) for(int i=n;i>=1;i--)\n#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)\n#define all(v) (v).begin(),(v).end()\ntypedef pair<ll, ll> LP;\ntypedef long double ld;\ntypedef pair<ld, ld> LDP;\nconst ld eps = 1e-8;\nconst ld pi = acosl(-1.0);\n\nll mod_pow(ll x, ll n, ll m = mod) {\n\tif (n < 0) {\n\t\tll res = mod_pow(x, -n, m);\n\t\treturn mod_pow(res, m - 2, m);\n\t}\n\tif (abs(x) >= m)x %= m;\n\tif (x < 0)x += m;\n\tll res = 1;\n\twhile (n) {\n\t\tif (n & 1)res = res * x % m;\n\t\tx = x * x % m; n >>= 1;\n\t}\n\treturn res;\n}\nstruct modint {\n\tll n;\n\tmodint() :n(0) { ; }\n\tmodint(ll m) :n(m) {\n\t\tif (n >= mod)n %= mod;\n\t\telse if (n < 0)n = (n % mod + mod) % mod;\n\t}\n\toperator int() { return n; }\n};\nbool operator==(modint a, modint b) { return a.n == b.n; }\nmodint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }\nmodint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }\nmodint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }\nmodint operator+(modint a, modint b) { return a += b; }\nmodint operator-(modint a, modint b) { return a -= b; }\nmodint operator*(modint a, modint b) { return a *= b; }\nmodint operator^(modint a, ll n) {\n\tif (n == 0)return modint(1);\n\tmodint res = (a * a) ^ (n / 2);\n\tif (n % 2)res = res * a;\n\treturn res;\n}\n\nll inv(ll a, ll p) {\n\treturn (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);\n}\nmodint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }\nmodint operator/=(modint& a, modint b) { a = a / b; return a; }\nconst int max_n = 1 << 2;\nmodint fact[max_n], factinv[max_n];\nvoid init_f() {\n\tfact[0] = modint(1);\n\tfor (int i = 0; i < max_n - 1; i++) {\n\t\tfact[i + 1] = fact[i] * modint(i + 1);\n\t}\n\tfactinv[max_n - 1] = modint(1) / fact[max_n - 1];\n\tfor (int i = max_n - 2; i >= 0; i--) {\n\t\tfactinv[i] = factinv[i + 1] * modint(i + 1);\n\t}\n}\nmodint comb(int a, int b) {\n\tif (a < 0 || b < 0 || a < b)return 0;\n\treturn fact[a] * factinv[b] * factinv[a - b];\n}\nmodint combP(int a, int b) {\n\tif (a < 0 || b < 0 || a < b)return 0;\n\treturn fact[a] * factinv[a - b];\n}\n\n\nvector<string> s;\nvector<vector<int>> G, rG;\nvector<P> vp;\nvector<int> ad;\nbool e[100][100];\n\n\nstruct graph {\nprivate:\n\tint n;\n\tvector<bool> used;\n\tvector<int> vs;\n\n\tint mk;\n\tvector<vector<int>> fG;\n\tvector<vector<int>> ori;\n\tvector<int> trans;\npublic:\n\tgraph(int sz) {\n\t\tn = sz;\n\t\tused.resize(n);\n\n\t\tfG.resize(n);\n\t\ttrans.resize(n, -1);\n\t\tori.resize(n);\n\t}\n\tvoid dfs(int v) {\n\t\tused[v] = true;\n\t\tint i = v / vp.size();\n\t\tint j = v % vp.size();\n\t\tint id1 = vp[i].first;\n\t\tint loc1 = vp[i].second;\n\t\tint id2 = vp[j].first;\n\t\tint loc2 = vp[j].second;\n\n\t\tif (loc1 > 0 && loc2 + 1 < s[id2].size()) {\n\t\t\tif (s[id1][loc1 - 1] == s[id2][loc2 + 1]) {\n\t\t\t\tint to = (i - 1) * vp.size() + j + 1;\n\t\t\t\tif (!used[to])dfs(to);\n\t\t\t}\n\t\t}\n\t\telse if (loc1 > 0) {\n\t\t\tfor (int idto : G[id2]) {\n\t\t\t\tif (s[id1][loc1 - 1] == s[idto][0]) {\n\t\t\t\t\tint to = (ad[id1] + loc1 - 1) * vp.size() + ad[idto] + 0;\n\n\t\t\t\t\tif (!used[to])dfs(to);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\telse if (loc2 + 1 < s[id2].size()) {\n\t\t\tfor (int idto : rG[id1]) {\n\t\t\t\tif (s[idto].back() == s[id2][loc2 + 1]) {\n\t\t\t\t\tint to = (ad[idto] + s[idto].size() - 1) * vp.size() + ad[id2] + loc2 + 1;\n\n\t\t\t\t\tif (!used[to])dfs(to);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\telse {\n\t\t\tfor (int idto1 : rG[id1]) {\n\t\t\t\tfor (int idto2 : G[id2]) {\n\t\t\t\t\tif (s[idto1].back() == s[idto2][0]) {\n\t\t\t\t\t\tint to = (ad[idto1] + s[idto1].size() - 1) * vp.size() + ad[idto2] + 0;\n\n\t\t\t\t\t\tif (!used[to])dfs(to);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tvs.push_back(v);\n\t}\n\tvoid rdfs(int v, int k) {\n\t\tused[v] = true;\n\t\tqueue<int> q; q.push(v);\n\t\tvector<int> c;\n\t\twhile (!q.empty()) {\n\t\t\tint id = q.front(); q.pop();\n\t\t\tori[k].push_back(id);\n\t\t\tint i = id % vp.size();\n\t\t\tint j = id / vp.size();\n\t\t\tint id1 = vp[i].first;\n\t\t\tint loc1 = vp[i].second;\n\t\t\tint id2 = vp[j].first;\n\t\t\tint loc2 = vp[j].second;\n\t\t\tif (s[id1][loc1] == s[id2][loc2]) {\n\t\t\t\tif (loc1 > 0 && loc2 + 1 < s[id2].size()) {\n\t\t\t\t\t//if (s[id1][loc1 - 1] == s[id2][loc2 + 1]) {\n\t\t\t\t\tint to = (i - 1) + (j + 1) * vp.size();\n\t\t\t\t\tif (used[to]) {\n\t\t\t\t\t\tif (trans[to] >= 0)c.push_back(trans[to]);\n\t\t\t\t\t}\n\t\t\t\t\telse {\n\t\t\t\t\t\tused[to] = true;\n\t\t\t\t\t\tq.push(to);\n\t\t\t\t\t}\n\t\t\t\t\t//}\n\t\t\t\t}\n\t\t\t\telse if (loc1 > 0) {\n\t\t\t\t\tfor (int idto : G[id2]) {\n\t\t\t\t\t\t//if (s[id1][loc1 - 1] == s[idto][0]) {\n\t\t\t\t\t\tint to = (ad[id1] + loc1 - 1) + (ad[idto] + 0) * vp.size();\n\t\t\t\t\t\tif (used[to]) {\n\t\t\t\t\t\t\tif (trans[to] >= 0)c.push_back(trans[to]);\n\t\t\t\t\t\t}\n\t\t\t\t\t\telse {\n\t\t\t\t\t\t\tused[to] = true;\n\t\t\t\t\t\t\tq.push(to);\n\t\t\t\t\t\t}\n\t\t\t\t\t\t//}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\telse if (loc2 + 1 < s[id2].size()) {\n\t\t\t\t\tfor (int idto : rG[id1]) {\n\t\t\t\t\t\t//if (s[idto].back() == s[id2][loc2 + 1]) {\n\t\t\t\t\t\tint to = (ad[idto] + s[idto].size() - 1) + (ad[id2] + loc2 + 1) * vp.size();\n\t\t\t\t\t\tif (used[to]) {\n\t\t\t\t\t\t\tif (trans[to] >= 0)c.push_back(trans[to]);\n\t\t\t\t\t\t}\n\t\t\t\t\t\telse {\n\t\t\t\t\t\t\tused[to] = true;\n\t\t\t\t\t\t\tq.push(to);\n\t\t\t\t\t\t}\n\t\t\t\t\t\t//}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tfor (int idto1 : rG[id1]) {\n\t\t\t\t\t\tfor (int idto2 : G[id2]) {\n\t\t\t\t\t\t\t//if (s[idto1].back() == s[idto2][0]) {\n\t\t\t\t\t\t\tint to = (ad[idto1] + s[idto1].size() - 1) + (ad[idto2] + 0) * vp.size();\n\t\t\t\t\t\t\tif (used[to]) {\n\t\t\t\t\t\t\t\tif (trans[to] >= 0)c.push_back(trans[to]);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\telse {\n\t\t\t\t\t\t\t\tused[to] = true;\n\t\t\t\t\t\t\t\tq.push(to);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t//}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tsort(c.begin(), c.end());\n\t\tint len = unique(c.begin(), c.end()) - c.begin();\n\t\trep(i, len) {\n\t\t\tfG[c[i]].push_back(k);\n\t\t}\n\t\trep(i, ori[k].size()) {\n\t\t\ttrans[ori[k][i]] = k;\n\t\t}\n\t}\n\tvoid scc() {\n\t\tfill(used.begin(), used.end(), false);\n\t\trep(i, n) {\n\t\t\tif (!used[i])dfs(i);\n\t\t}\n\t\tfill(used.begin(), used.end(), false);\n\t\tint k = 0;\n\t\tper(i, (int)vs.size()) {\n\t\t\tif (!used[vs[i]]) {\n\t\t\t\trdfs(vs[i], k); k++;\n\t\t\t}\n\t\t}\n\t\tmk = k;\n\t}\n\tint query(vector<P> ps) {\n\t\tvector<int> lens(s.size());\n\t\trep(i, s.size())lens[i] = s[i].size();\n\t\tint m = lens.size();\n\t\tvector<int> ad(m + 1);\n\t\trep(i, m)ad[i + 1] = ad[i] + lens[i];\n\t\tint sum = ad[m];\n\t\tvector<bool> canfin(n);\n\t\trep(i, m)rep(j, m) {\n\t\t\tint id = ad[i] * sum + ad[j] + lens[j] - 1;\n\t\t\tcanfin[id] = true;\n\t\t}\n\t\tvector<P> ans(mk);\n\t\tper(i, mk) {\n\t\t\tbool exifin = false;\n\t\t\tfor (int id : ori[i]) {\n\t\t\t\tif (canfin[id])exifin = true;\n\t\t\t}\n\t\t\tfor (int to : fG[i]) {\n\t\t\t\tif (ans[to].second)exifin = true;\n\t\t\t}\n\t\t\tif (!exifin) {\n\t\t\t\tans[i] = { 0,0 };\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tans[i].second = 1;\n\t\t\tint ma = 0;\n\t\t\tfor (int to : fG[i]) {\n\t\t\t\tif (ans[to].second) {\n\t\t\t\t\tma = max(ma, ans[to].first + 2);\n\t\t\t\t}\n\t\t\t}\n\t\t\tans[i].first = ma;\n\t\t\tbool exiinf = false;\n\t\t\tif (ori[i].size() > 1)exiinf = true;\n\t\t\telse {\n\t\t\t\tint id = ori[i][0];\n\t\t\t\tint id1 = id / vp.size();\n\t\t\t\tint id2 = id % vp.size();\n\t\t\t\tid1 = vp[id1].first;\n\t\t\t\tid2 = vp[id2].first;\n\t\t\t\tif (s[id1].size() == 1 && s[id2].size() == 1 && s[id1] == s[id2]&&e[id1][id1]&&e[id2][id2])exiinf = true;\n\t\t\t}\n\t\t\tif (exiinf)ans[i].first = mod;\n\t\t}\n\t\tint res = 0;\n\t\t//odd\n\t\trep(i, m)rep(j, lens[i]) {\n\t\t\tint cur = (ad[i] + j) * sum + ad[i] + j;\n\t\t\tcur = trans[cur];\n\t\t\tif (ans[cur].second) {\n\t\t\t\tres = max(res, ans[cur].first + 1);\n\t\t\t}\n\t\t}\n\t\t//even\n\t\trep(i, m)rep(j, lens[i] - 1) {\n\t\t\tif (s[i][j] == s[i][j + 1]) {\n\t\t\t\tint cur = (ad[i] + j) * sum + ad[i] + j + 1;\n\t\t\t\tcur = trans[cur];\n\t\t\t\tif (ans[cur].second) {\n\t\t\t\t\tres = max(res, ans[cur].first + 2);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tfor(P p:ps){\n\t\t\tint i = p.first, j = p.second;\n\t\t\tif (s[i].back() == s[j][0]) {\n\t\t\t\tint cur = (ad[i] + lens[i] - 1) * sum + ad[j];\n\t\t\t\tcur = trans[cur];\n\t\t\t\tif (ans[cur].second) {\n\t\t\t\t\t//cout << \"? \"<<ori[cur].size()<<\" \"<<ori[cur][0] << \" \" << ans[cur].first << \"\\n\";\n\t\t\t\t\tres = max(res, ans[cur].first + 2);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif (res >= mod)res = -1;\n\t\treturn res;\n\t}\n};\n\nint dx[4] = { 1,0,-1,0 };\nint dy[4] = { 0,1,0,-1 };\n\nll gcd(ll x, ll y) {\n\tx = abs(x), y = abs(y);\n\tif (x < y)swap(x, y);\n\twhile (y) {\n\t\tll r = x % y; x = y; y = r;\n\t}\n\treturn x;\n}\n\nvoid solve() {\n\tint n, m; cin >> n >> m;\n\ts.resize(n);\n\trep(i, n)cin >> s[i];\n\tad.resize(n);\n\trep(i, n) {\n\t\tad[i] = vp.size();\n\t\trep(j, s[i].size())vp.push_back({ i,j });\n\t}\n\tG.resize(n);\n\trG.resize(n);\n\tvector<P> ps;\n\trep(i, m) {\n\t\tint a, b; cin >> a >> b; a--; b--;\n\t\tG[a].push_back(b);\n\t\trG[b].push_back(a);\n\t\te[a][b] = true;\n\t\tps.push_back({ a,b });\n\t}\n\tgraph g(vp.size()*vp.size());\n\tg.scc();\n\tint ans = g.query(ps);\n\tcout << ans << \"\\n\";\n}\n\n\n\nsigned main() {\n\tios::sync_with_stdio(false);\n\tcin.tie(0);\n\tcout << fixed << setprecision(10);\n\t//init_f();\n\t//init();\n\t//int t; cin >> t; rep(i, t)\n\t//while (cin >> n>>s,n)\n\t\tsolve();\n\treturn 0;\n}", "accuracy": 1, "time_ms": 120, "memory_kb": 100468, "score_of_the_acc": -1.0245, "final_rank": 11 }, { "submission_id": "aoj_2307_6024418", "code_snippet": "//#pragma GCC optimize(\"O3\")\n//#pragma GCC optimize(\"unroll-loops\")\n#include<iostream>\n#include<string>\n#include<cstdio>\n#include<vector>\n#include<cmath>\n#include<algorithm>\n#include<functional>\n#include<iomanip>\n#include<queue>\n#include<ciso646>\n#include<random>\n#include<map>\n#include<set>\n#include<bitset>\n#include<stack>\n#include<unordered_map>\n#include<unordered_set>\n#include<utility>\n#include<cassert>\n#include<complex>\n#include<numeric>\n#include<array>\nusing namespace std;\n\n//#define int long long\ntypedef long long ll;\n\ntypedef unsigned long long ul;\ntypedef unsigned int ui;\nconst ll mod = 998244353;\nconst ll INF = mod * mod;\ntypedef pair<int, int>P;\n\n#define rep(i,n) for(int i=0;i<n;i++)\n#define per(i,n) for(int i=n-1;i>=0;i--)\n#define Rep(i,sta,n) for(int i=sta;i<n;i++)\n#define rep1(i,n) for(int i=1;i<=n;i++)\n#define per1(i,n) for(int i=n;i>=1;i--)\n#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)\n#define all(v) (v).begin(),(v).end()\ntypedef pair<ll, ll> LP;\ntypedef long double ld;\ntypedef pair<ld, ld> LDP;\nconst ld eps = 1e-8;\nconst ld pi = acosl(-1.0);\n\nll mod_pow(ll x, ll n, ll m = mod) {\n\tif (n < 0) {\n\t\tll res = mod_pow(x, -n, m);\n\t\treturn mod_pow(res, m - 2, m);\n\t}\n\tif (abs(x) >= m)x %= m;\n\tif (x < 0)x += m;\n\tll res = 1;\n\twhile (n) {\n\t\tif (n & 1)res = res * x % m;\n\t\tx = x * x % m; n >>= 1;\n\t}\n\treturn res;\n}\nstruct modint {\n\tll n;\n\tmodint() :n(0) { ; }\n\tmodint(ll m) :n(m) {\n\t\tif (n >= mod)n %= mod;\n\t\telse if (n < 0)n = (n % mod + mod) % mod;\n\t}\n\toperator int() { return n; }\n};\nbool operator==(modint a, modint b) { return a.n == b.n; }\nmodint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }\nmodint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }\nmodint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }\nmodint operator+(modint a, modint b) { return a += b; }\nmodint operator-(modint a, modint b) { return a -= b; }\nmodint operator*(modint a, modint b) { return a *= b; }\nmodint operator^(modint a, ll n) {\n\tif (n == 0)return modint(1);\n\tmodint res = (a * a) ^ (n / 2);\n\tif (n % 2)res = res * a;\n\treturn res;\n}\n\nll inv(ll a, ll p) {\n\treturn (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);\n}\nmodint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }\nmodint operator/=(modint& a, modint b) { a = a / b; return a; }\nconst int max_n = 1 << 2;\nmodint fact[max_n], factinv[max_n];\nvoid init_f() {\n\tfact[0] = modint(1);\n\tfor (int i = 0; i < max_n - 1; i++) {\n\t\tfact[i + 1] = fact[i] * modint(i + 1);\n\t}\n\tfactinv[max_n - 1] = modint(1) / fact[max_n - 1];\n\tfor (int i = max_n - 2; i >= 0; i--) {\n\t\tfactinv[i] = factinv[i + 1] * modint(i + 1);\n\t}\n}\nmodint comb(int a, int b) {\n\tif (a < 0 || b < 0 || a < b)return 0;\n\treturn fact[a] * factinv[b] * factinv[a - b];\n}\nmodint combP(int a, int b) {\n\tif (a < 0 || b < 0 || a < b)return 0;\n\treturn fact[a] * factinv[a - b];\n}\n\n\nvector<string> s;\nvector<vector<int>> G, rG;\nvector<P> vp;\nvector<int> ad;\nbool e[100][100];\n\n\nstruct graph {\nprivate:\n\tint n;\n\tvector<bool> used;\n\tvector<int> vs;\n\n\tint mk;\n\tvector<vector<int>> fG;\n\tvector<vector<int>> ori;\n\tvector<int> trans;\npublic:\n\tgraph(int sz) {\n\t\tn = sz;\n\t\tused.resize(n);\n\n\t\tfG.resize(n);\n\t\ttrans.resize(n, -1);\n\t\tori.resize(n);\n\t}\n\tvoid dfs(int v) {\n\t\tused[v] = true;\n\t\tint i = v / vp.size();\n\t\tint j = v % vp.size();\n\t\tint id1 = vp[i].first;\n\t\tint loc1 = vp[i].second;\n\t\tint id2 = vp[j].first;\n\t\tint loc2 = vp[j].second;\n\n\t\tint cur = i * vp.size() + j;\n\t\tif (loc1 > 0 && loc2 + 1 < s[id2].size()) {\n\t\t\tif (s[id1][loc1 - 1] == s[id2][loc2 + 1]) {\n\t\t\t\tint to = (i - 1) * vp.size() + j + 1;\n\t\t\t\tif (!used[to])dfs(to);\n\t\t\t}\n\t\t}\n\t\telse if (loc1 > 0) {\n\t\t\tfor (int idto : G[id2]) {\n\t\t\t\tif (s[id1][loc1 - 1] == s[idto][0]) {\n\t\t\t\t\tint to = (ad[id1] + loc1 - 1) * vp.size() + ad[idto] + 0;\n\n\t\t\t\t\tif (!used[to])dfs(to);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\telse if (loc2 + 1 < s[id2].size()) {\n\t\t\tfor (int idto : rG[id1]) {\n\t\t\t\tif (s[idto].back() == s[id2][loc2 + 1]) {\n\t\t\t\t\tint to = (ad[idto] + s[idto].size() - 1) * vp.size() + ad[id2] + loc2 + 1;\n\n\t\t\t\t\tif (!used[to])dfs(to);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\telse {\n\t\t\tfor (int idto1 : rG[id1]) {\n\t\t\t\tfor (int idto2 : G[id2]) {\n\t\t\t\t\tif (s[idto1].back() == s[idto2][0]) {\n\t\t\t\t\t\tint to = (ad[idto1] + s[idto1].size() - 1) * vp.size() + ad[idto2] + 0;\n\n\t\t\t\t\t\tif (!used[to])dfs(to);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tvs.push_back(v);\n\t}\n\tvoid rdfs(int v, int k) {\n\t\tused[v] = true;\n\t\tqueue<int> q; q.push(v);\n\t\tvector<int> c;\n\t\twhile (!q.empty()) {\n\t\t\tint id = q.front(); q.pop();\n\t\t\tori[k].push_back(id);\n\t\t\tint i = id % vp.size();\n\t\t\tint j = id / vp.size();\n\t\t\tint id1 = vp[i].first;\n\t\t\tint loc1 = vp[i].second;\n\t\t\tint id2 = vp[j].first;\n\t\t\tint loc2 = vp[j].second;\n\n\t\t\tint cur = i * vp.size() + j;\n\t\t\tif (loc1 > 0 && loc2 + 1 < s[id2].size()) {\n\t\t\t\tif (s[id1][loc1 - 1] == s[id2][loc2 + 1]) {\n\t\t\t\t\tint to = (i - 1) + (j + 1) * vp.size();\n\t\t\t\t\tif (used[to]) {\n\t\t\t\t\t\tif (trans[to] >= 0)c.push_back(trans[to]);\n\t\t\t\t\t}\n\t\t\t\t\telse {\n\t\t\t\t\t\tused[to] = true;\n\t\t\t\t\t\tq.push(to);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\telse if (loc1 > 0) {\n\t\t\t\tfor (int idto : G[id2]) {\n\t\t\t\t\tif (s[id1][loc1 - 1] == s[idto][0]) {\n\t\t\t\t\t\tint to = (ad[id1] + loc1 - 1) + (ad[idto] + 0) * vp.size();\n\t\t\t\t\t\tif (used[to]) {\n\t\t\t\t\t\t\tif (trans[to] >= 0)c.push_back(trans[to]);\n\t\t\t\t\t\t}\n\t\t\t\t\t\telse {\n\t\t\t\t\t\t\tused[to] = true;\n\t\t\t\t\t\t\tq.push(to);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\telse if (loc2 + 1 < s[id2].size()) {\n\t\t\t\tfor (int idto : rG[id1]) {\n\t\t\t\t\tif (s[idto].back() == s[id2][loc2 + 1]) {\n\t\t\t\t\t\tint to = (ad[idto] + s[idto].size() - 1) + (ad[id2] + loc2 + 1)*vp.size();\n\t\t\t\t\t\tif (used[to]) {\n\t\t\t\t\t\t\tif (trans[to] >= 0)c.push_back(trans[to]);\n\t\t\t\t\t\t}\n\t\t\t\t\t\telse {\n\t\t\t\t\t\t\tused[to] = true;\n\t\t\t\t\t\t\tq.push(to);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\telse {\n\t\t\t\tfor (int idto1 : rG[id1]) {\n\t\t\t\t\tfor (int idto2 : G[id2]) {\n\t\t\t\t\t\tif (s[idto1].back() == s[idto2][0]) {\n\t\t\t\t\t\t\tint to = (ad[idto1] + s[idto1].size() - 1) + (ad[idto2] + 0)*vp.size();\n\t\t\t\t\t\t\tif (used[to]) {\n\t\t\t\t\t\t\t\tif (trans[to] >= 0)c.push_back(trans[to]);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\telse {\n\t\t\t\t\t\t\t\tused[to] = true;\n\t\t\t\t\t\t\t\tq.push(to);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tsort(c.begin(), c.end());\n\t\tint len = unique(c.begin(), c.end()) - c.begin();\n\t\trep(i, len) {\n\t\t\tfG[c[i]].push_back(k);\n\t\t}\n\t\trep(i, ori[k].size()) {\n\t\t\ttrans[ori[k][i]] = k;\n\t\t}\n\t}\n\tvoid scc() {\n\t\tfill(used.begin(), used.end(), false);\n\t\trep(i, n) {\n\t\t\tif (!used[i])dfs(i);\n\t\t}\n\t\tfill(used.begin(), used.end(), false);\n\t\tint k = 0;\n\t\tper(i, (int)vs.size()) {\n\t\t\tif (!used[vs[i]]) {\n\t\t\t\trdfs(vs[i], k); k++;\n\t\t\t}\n\t\t}\n\t\tmk = k;\n\t}\n\tint query(vector<P> ps) {\n\t\tvector<int> lens(s.size());\n\t\trep(i, s.size())lens[i] = s[i].size();\n\t\tint m = lens.size();\n\t\tvector<int> ad(m + 1);\n\t\trep(i, m)ad[i + 1] = ad[i] + lens[i];\n\t\tint sum = ad[m];\n\t\tvector<bool> canfin(n);\n\t\trep(i, m)rep(j, m) {\n\t\t\tint id = ad[i] * sum + ad[j] + lens[j] - 1;\n\t\t\tcanfin[id] = true;\n\t\t}\n\t\tvector<P> ans(mk);\n\t\tper(i, mk) {\n\t\t\tbool exifin = false;\n\t\t\tfor (int id : ori[i]) {\n\t\t\t\tif (canfin[id])exifin = true;\n\t\t\t}\n\t\t\tfor (int to : fG[i]) {\n\t\t\t\tif (ans[to].second)exifin = true;\n\t\t\t}\n\t\t\tif (!exifin) {\n\t\t\t\tans[i] = { 0,0 };\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tans[i].second = 1;\n\t\t\tint ma = 0;\n\t\t\tfor (int to : fG[i]) {\n\t\t\t\tif (ans[to].second) {\n\t\t\t\t\tma = max(ma, ans[to].first + 2);\n\t\t\t\t}\n\t\t\t}\n\t\t\tans[i].first = ma;\n\t\t\tbool exiinf = false;\n\t\t\tif (ori[i].size() > 1)exiinf = true;\n\t\t\telse {\n\t\t\t\tint id = ori[i][0];\n\t\t\t\tint id1 = id / vp.size();\n\t\t\t\tint id2 = id % vp.size();\n\t\t\t\tid1 = vp[id1].first;\n\t\t\t\tid2 = vp[id2].first;\n\t\t\t\tif (s[id1].size() == 1 && s[id2].size() == 1 && s[id1] == s[id2]&&e[id1][id1]&&e[id2][id2])exiinf = true;\n\t\t\t}\n\t\t\tif (exiinf)ans[i].first = mod;\n\t\t}\n\t\tint res = 0;\n\t\t//odd\n\t\trep(i, m)rep(j, lens[i]) {\n\t\t\tint cur = (ad[i] + j) * sum + ad[i] + j;\n\t\t\tcur = trans[cur];\n\t\t\tif (ans[cur].second) {\n\t\t\t\tres = max(res, ans[cur].first + 1);\n\t\t\t}\n\t\t}\n\t\t//even\n\t\trep(i, m)rep(j, lens[i] - 1) {\n\t\t\tif (s[i][j] == s[i][j + 1]) {\n\t\t\t\tint cur = (ad[i] + j) * sum + ad[i] + j + 1;\n\t\t\t\tcur = trans[cur];\n\t\t\t\tif (ans[cur].second) {\n\t\t\t\t\tres = max(res, ans[cur].first + 2);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tfor(P p:ps){\n\t\t\tint i = p.first, j = p.second;\n\t\t\tif (s[i].back() == s[j][0]) {\n\t\t\t\tint cur = (ad[i] + lens[i] - 1) * sum + ad[j];\n\t\t\t\tcur = trans[cur];\n\t\t\t\tif (ans[cur].second) {\n\t\t\t\t\t//cout << \"? \"<<ori[cur].size()<<\" \"<<ori[cur][0] << \" \" << ans[cur].first << \"\\n\";\n\t\t\t\t\tres = max(res, ans[cur].first + 2);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif (res >= mod)res = -1;\n\t\treturn res;\n\t}\n};\n\nint dx[4] = { 1,0,-1,0 };\nint dy[4] = { 0,1,0,-1 };\n\nll gcd(ll x, ll y) {\n\tx = abs(x), y = abs(y);\n\tif (x < y)swap(x, y);\n\twhile (y) {\n\t\tll r = x % y; x = y; y = r;\n\t}\n\treturn x;\n}\n\nvoid solve() {\n\tint n, m; cin >> n >> m;\n\ts.resize(n);\n\trep(i, n)cin >> s[i];\n\tad.resize(n);\n\trep(i, n) {\n\t\tad[i] = vp.size();\n\t\trep(j, s[i].size())vp.push_back({ i,j });\n\t}\n\tG.resize(n);\n\trG.resize(n);\n\tvector<P> ps;\n\trep(i, m) {\n\t\tint a, b; cin >> a >> b; a--; b--;\n\t\tG[a].push_back(b);\n\t\trG[b].push_back(a);\n\t\te[a][b] = true;\n\t\tps.push_back({ a,b });\n\t}\n\tgraph g(vp.size()*vp.size());\n\tg.scc();\n\tint ans = g.query(ps);\n\tcout << ans << \"\\n\";\n}\n\n\n\nsigned main() {\n\tios::sync_with_stdio(false);\n\tcin.tie(0);\n\tcout << fixed << setprecision(10);\n\t//init_f();\n\t//init();\n\t//int t; cin >> t; rep(i, t)\n\t//while (cin >> n>>s,n)\n\t\tsolve();\n\treturn 0;\n}", "accuracy": 0.2653061224489796, "time_ms": 120, "memory_kb": 98532, "score_of_the_acc": -1.0037, "final_rank": 19 }, { "submission_id": "aoj_2307_5846904", "code_snippet": "#line 2 \"/Users/nok0/Documents/Programming/nok0/graph/graph.hpp\"\n#include <algorithm>\n#include <cassert>\n#include <deque>\n#include <iostream>\n#include <queue>\n#include <tuple>\n#include <utility>\n#include <vector>\n\nstruct Edge {\n\tint to;\n\tlong long cost;\n\tEdge() = default;\n\tEdge(int to_, long long cost_) : to(to_), cost(cost_) {}\n\tbool operator<(const Edge &a) const { return cost < a.cost; }\n\tbool operator>(const Edge &a) const { return cost > a.cost; }\n\tfriend std::ostream &operator<<(std::ostream &s, Edge &a) {\n\t\ts << \"to: \" << a.to << \", cost: \" << a.cost;\n\t\treturn s;\n\t}\n};\n\nclass Graph {\n\tstd::vector<std::vector<Edge>> edges;\n\n\ttemplate <class F>\n\tstruct rec_lambda {\n\t\tF f;\n\t\trec_lambda(F &&f_) : f(std::forward<F>(f_)) {}\n\t\ttemplate <class... Args>\n\t\tauto operator()(Args &&... args) const {\n\t\t\treturn f(*this, std::forward<Args>(args)...);\n\t\t}\n\t};\n\npublic:\n\tinline const std::vector<Edge> &operator[](int k) const { return edges[k]; }\n\tinline std::vector<Edge> &operator[](int k) { return edges[k]; }\n\n\tint size() const { return edges.size(); }\n\tvoid resize(const int n) { edges.resize(n); }\n\n\tGraph() = default;\n\tGraph(int n) : edges(n) {}\n\tGraph(int n, int e, bool weight = 0, bool directed = 0, int idx = 1) : edges(n) { input(e, weight, directed, idx); }\n\tconst long long INF = 3e18;\n\n\tvoid input(int e = -1, bool weight = 0, bool directed = false, int idx = 1) {\n\t\tif(e == -1) e = size() - 1;\n\t\twhile(e--) {\n\t\t\tint u, v;\n\t\t\tlong long cost = 1;\n\t\t\tstd::cin >> u >> v;\n\t\t\tif(weight) std::cin >> cost;\n\t\t\tu -= idx, v -= idx;\n\t\t\tedges[u].emplace_back(v, cost);\n\t\t\tif(!directed) edges[v].emplace_back(u, cost);\n\t\t}\n\t}\n\n\tvoid add_edge(int u, int v, long long cost = 1, bool directed = false, int idx = 0) {\n\t\tu -= idx, v -= idx;\n\t\tedges[u].emplace_back(v, cost);\n\t\tif(!directed) edges[v].emplace_back(u, cost);\n\t}\n\n\t// Ο(V+E)\n\tstd::vector<long long> bfs(int s) {\n\t\tstd::vector<long long> dist(size(), INF);\n\t\tstd::queue<int> que;\n\t\tdist[s] = 0;\n\t\tque.push(s);\n\t\twhile(!que.empty()) {\n\t\t\tint v = que.front();\n\t\t\tque.pop();\n\t\t\tfor(auto &e : edges[v]) {\n\t\t\t\tif(dist[e.to] != INF) continue;\n\t\t\t\tdist[e.to] = dist[v] + e.cost;\n\t\t\t\tque.push(e.to);\n\t\t\t}\n\t\t}\n\t\treturn dist;\n\t}\n\n\t// Ο(V+E)\n\t// constraint: cost of each edge is zero or one\n\tstd::vector<long long> zero_one_bfs(int s) {\n\t\tstd::vector<long long> dist(size(), INF);\n\t\tstd::deque<int> deq;\n\t\tdist[s] = 0;\n\t\tdeq.push_back(s);\n\t\twhile(!deq.empty()) {\n\t\t\tint v = deq.front();\n\t\t\tdeq.pop_front();\n\t\t\tfor(auto &e : edges[v]) {\n\t\t\t\tassert(0LL <= e.cost and e.cost < 2LL);\n\t\t\t\tif(e.cost and dist[e.to] > dist[v] + 1) {\n\t\t\t\t\tdist[e.to] = dist[v] + 1;\n\t\t\t\t\tdeq.push_back(e.to);\n\t\t\t\t} else if(!e.cost and dist[e.to] > dist[v]) {\n\t\t\t\t\tdist[e.to] = dist[v];\n\t\t\t\t\tdeq.push_front(e.to);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn dist;\n\t}\n\n\t// Ο((E+V)logV)\n\t// cannot reach: INF\n\tstd::vector<long long> dijkstra(int s) { // verified\n\t\tstd::vector<long long> dist(size(), INF);\n\t\tconst auto compare = [](const std::pair<long long, int> &a, const std::pair<long long, int> &b) { return a.first > b.first; };\n\t\tstd::priority_queue<std::pair<long long, int>, std::vector<std::pair<long long, int>>, decltype(compare)> que{compare};\n\t\tdist[s] = 0;\n\t\tque.emplace(0, s);\n\t\twhile(!que.empty()) {\n\t\t\tstd::pair<long long, int> p = que.top();\n\t\t\tque.pop();\n\t\t\tint v = p.second;\n\t\t\tif(dist[v] < p.first) continue;\n\t\t\tfor(auto &e : edges[v]) {\n\t\t\t\tif(dist[e.to] > dist[v] + e.cost) {\n\t\t\t\t\tdist[e.to] = dist[v] + e.cost;\n\t\t\t\t\tque.emplace(dist[e.to], e.to);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn dist;\n\t}\n\n\t// Ο(VE)\n\t// cannot reach: INF\n\t// negative cycle: -INF\n\tstd::vector<long long> bellman_ford(int s) { // verified\n\t\tint n = size();\n\t\tstd::vector<long long> res(n, INF);\n\t\tres[s] = 0;\n\t\tfor(int loop = 0; loop < n - 1; loop++) {\n\t\t\tfor(int v = 0; v < n; v++) {\n\t\t\t\tif(res[v] == INF) continue;\n\t\t\t\tfor(auto &e : edges[v]) {\n\t\t\t\t\tres[e.to] = std::min(res[e.to], res[v] + e.cost);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tstd::queue<int> que;\n\t\tstd::vector<int> chk(n);\n\t\tfor(int v = 0; v < n; v++) {\n\t\t\tif(res[v] == INF) continue;\n\t\t\tfor(auto &e : edges[v]) {\n\t\t\t\tif(res[e.to] > res[v] + e.cost and !chk[e.to]) {\n\t\t\t\t\tque.push(e.to);\n\t\t\t\t\tchk[e.to] = 1;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\twhile(!que.empty()) {\n\t\t\tint now = que.front();\n\t\t\tque.pop();\n\t\t\tfor(auto &e : edges[now]) {\n\t\t\t\tif(!chk[e.to]) {\n\t\t\t\t\tchk[e.to] = 1;\n\t\t\t\t\tque.push(e.to);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tfor(int i = 0; i < n; i++)\n\t\t\tif(chk[i]) res[i] = -INF;\n\t\treturn res;\n\t}\n\n\t// Ο(V^3)\n\tstd::vector<std::vector<long long>> warshall_floyd() { // verified\n\t\tint n = size();\n\t\tstd::vector<std::vector<long long>> dist(n, std::vector<long long>(n, INF));\n\t\tfor(int i = 0; i < n; i++) dist[i][i] = 0;\n\t\tfor(int i = 0; i < n; i++)\n\t\t\tfor(auto &e : edges[i]) dist[i][e.to] = std::min(dist[i][e.to], e.cost);\n\t\tfor(int k = 0; k < n; k++)\n\t\t\tfor(int i = 0; i < n; i++) {\n\t\t\t\tif(dist[i][k] == INF) continue;\n\t\t\t\tfor(int j = 0; j < n; j++) {\n\t\t\t\t\tif(dist[k][j] == INF) continue;\n\t\t\t\t\tdist[i][j] = std::min(dist[i][j], dist[i][k] + dist[k][j]);\n\t\t\t\t}\n\t\t\t}\n\t\treturn dist;\n\t}\n\n\t// Ο(V) (using DFS)\n\t// if a directed cycle exists, return {}\n\tstd::vector<int> topological_sort() { // verified\n\t\tstd::vector<int> res;\n\t\tint n = size();\n\t\tstd::vector<int> used(n, 0);\n\t\tbool not_DAG = false;\n\t\tfor(int i = 0; i < n; i++) {\n\t\t\trec_lambda([&](auto &&dfs, int k) -> void {\n\t\t\t\tif(not_DAG) return;\n\t\t\t\tif(used[k]) {\n\t\t\t\t\tif(used[k] == 1) not_DAG = true;\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tused[k] = 1;\n\t\t\t\tfor(auto &e : edges[k]) dfs(e.to);\n\t\t\t\tused[k] = 2;\n\t\t\t\tres.push_back(k);\n\t\t\t})(i);\n\t\t}\n\t\tif(not_DAG) return std::vector<int>{};\n\t\tstd::reverse(res.begin(), res.end());\n\t\treturn res;\n\t}\n\n\tbool is_DAG() { return !topological_sort().empty(); } // verified\n\n\t// Ο(V)\n\t// array of the distance from each vertex to the most distant vertex\n\tstd::vector<long long> height() { // verified\n\t\tauto vec1 = bfs(0);\n\t\tint v1 = -1, v2 = -1;\n\t\tlong long dia = -1;\n\t\tfor(int i = 0; i < int(size()); i++)\n\t\t\tif(dia < vec1[i]) dia = vec1[i], v1 = i;\n\t\tvec1 = bfs(v1);\n\t\tdia = -1;\n\t\tfor(int i = 0; i < int(size()); i++)\n\t\t\tif(dia < vec1[i]) dia = vec1[i], v2 = i;\n\t\tauto vec2 = bfs(v2);\n\t\tfor(int i = 0; i < int(size()); i++)\n\t\t\tif(vec1[i] < vec2[i]) vec1[i] = vec2[i];\n\t\treturn vec1;\n\t}\n\n\t// O(V+E)\n\t// vector<(int)(0 or 1)>\n\t// if it is not bipartite, return {}\n\tstd::vector<int> bipartite_grouping() {\n\t\tstd::vector<int> colors(size(), -1);\n\t\tauto dfs = [&](auto self, int now, int col) -> bool {\n\t\t\tcolors[now] = col;\n\t\t\tfor(auto &e : edges[now]) {\n\t\t\t\tif(col == colors[e.to]) return false;\n\t\t\t\tif(colors[e.to] == -1 and !self(self, e.to, !col)) return false;\n\t\t\t}\n\t\t\treturn true;\n\t\t};\n\t\tfor(int i = 0; i < int(size()); i++)\n\t\t\tif(colors[i] == -1 and !dfs(dfs, i, 0)) return std::vector<int>{};\n\t\treturn colors;\n\t}\n\n\tbool is_bipartite() { return !bipartite_grouping().empty(); }\n\n\t// Ο(V+E)\n\t// ((v1, v2), diameter)\n\tstd::pair<std::pair<int, int>, long long> diameter() { // verified\n\t\tauto vec = bfs(0);\n\t\tint v1 = -1, v2 = -1;\n\t\tlong long dia = -1;\n\t\tfor(int i = 0; i < int(size()); i++)\n\t\t\tif(dia < vec[i]) dia = vec[i], v1 = i;\n\t\tvec = bfs(v1);\n\t\tdia = -1;\n\t\tfor(int i = 0; i < int(size()); i++)\n\t\t\tif(dia < vec[i]) dia = vec[i], v2 = i;\n\t\tstd::pair<std::pair<int, int>, long long> res = {{v1, v2}, dia};\n\t\treturn res;\n\t}\n\n\t// Ο(V+E)\n\t// return {s, v1, v2, ... t}\n\tstd::vector<int> diameter_path() {\n\t\tauto vec = bfs(0);\n\t\tint v1 = -1, v2 = -1;\n\t\tlong long dia = -1;\n\t\tfor(int i = 0; i < int(size()); i++)\n\t\t\tif(dia < vec[i]) dia = vec[i], v1 = i;\n\t\tauto vec2 = bfs(v1);\n\t\tdia = -1;\n\t\tfor(int i = 0; i < int(size()); i++)\n\t\t\tif(dia < vec2[i]) dia = vec2[i], v2 = i;\n\t\tvec = bfs(v2);\n\t\tstd::vector<int> ret;\n\t\tauto dfs = [&](auto self, int now, int p) -> void {\n\t\t\tret.emplace_back(now);\n\t\t\tif(now == v2) return;\n\t\t\tfor(auto &[to, cost] : (*this)[now]) {\n\t\t\t\tif(vec[to] + vec2[to] == dia and to != p)\n\t\t\t\t\tself(self, to, now);\n\t\t\t}\n\t\t};\n\t\tdfs(dfs, v1, -1);\n\t\treturn ret;\n\t}\n\n\t// Ο(V+E)\n\t// return subtree_size, root = root\n\tstd::vector<int> subtree_size(const int root) {\n\t\tint n = size();\n\t\tstd::vector<int> ret(n, 1);\n\t\trec_lambda([&](auto &&dfs, int now, int p) -> void {\n\t\t\tfor(auto &[to, cost] : (*this)[now]) {\n\t\t\t\tif(to == p) continue;\n\t\t\t\tdfs(to, now);\n\t\t\t\tret[now] += ret[to];\n\t\t\t}\n\t\t})(root, -1);\n\t\treturn ret;\n\t}\n\n\t// Ο(ElogV)\n\tlong long prim() { // verified\n\t\tlong long res = 0;\n\t\tstd::priority_queue<Edge, std::vector<Edge>, std::greater<Edge>> que;\n\t\tfor(auto &e : edges[0]) que.push(e);\n\t\tstd::vector<int> chk(size());\n\t\tchk[0] = 1;\n\t\tint cnt = 1;\n\t\twhile(cnt < size()) {\n\t\t\tauto e = que.top();\n\t\t\tque.pop();\n\t\t\tif(chk[e.to]) continue;\n\t\t\tcnt++;\n\t\t\tres += e.cost;\n\t\t\tchk[e.to] = 1;\n\t\t\tfor(auto &e2 : edges[e.to]) que.push(e2);\n\t\t}\n\t\treturn res;\n\t}\n\n\t// Ο(ElogE)\n\tlong long kruskal() { // verified\n\t\tstd::vector<std::tuple<int, int, long long>> Edges;\n\t\tfor(int i = 0; i < int(size()); i++)\n\t\t\tfor(auto &e : edges[i]) Edges.emplace_back(i, e.to, e.cost);\n\t\tstd::sort(Edges.begin(), Edges.end(), [](const std::tuple<int, int, long long> &a, const std::tuple<int, int, long long> &b) {\n\t\t\treturn std::get<2>(a) < std::get<2>(b);\n\t\t});\n\t\tstd::vector<int> uf_data(size(), -1);\n\t\tauto root = [&uf_data](auto self, int x) -> int {\n\t\t\tif(uf_data[x] < 0) return x;\n\t\t\treturn uf_data[x] = self(self, uf_data[x]);\n\t\t};\n\t\tauto unite = [&uf_data, &root](int u, int v) -> bool {\n\t\t\tu = root(root, u), v = root(root, v);\n\t\t\tif(u == v) return false;\n\t\t\tif(uf_data[u] > uf_data[v]) std::swap(u, v);\n\t\t\tuf_data[u] += uf_data[v];\n\t\t\tuf_data[v] = u;\n\t\t\treturn true;\n\t\t};\n\t\tlong long ret = 0;\n\t\tfor(auto &e : Edges)\n\t\t\tif(unite(std::get<0>(e), std::get<1>(e))) ret += std::get<2>(e);\n\t\treturn ret;\n\t}\n\n\t// O(V)\n\tstd::vector<int> centroid() {\n\t\tint n = size();\n\t\tstd::vector<int> centroid, sz(n);\n\t\tauto dfs = [&](auto self, int now, int per) -> void {\n\t\t\tsz[now] = 1;\n\t\t\tbool is_centroid = true;\n\t\t\tfor(auto &e : edges[now]) {\n\t\t\t\tif(e.to != per) {\n\t\t\t\t\tself(self, e.to, now);\n\t\t\t\t\tsz[now] += sz[e.to];\n\t\t\t\t\tif(sz[e.to] > n / 2) is_centroid = false;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(n - sz[now] > n / 2) is_centroid = false;\n\t\t\tif(is_centroid) centroid.push_back(now);\n\t\t};\n\t\tdfs(dfs, 0, -1);\n\t\treturn centroid;\n\t}\n\n\t// Ο(V+E)\n\t// directed graph from root to leaf\n\tGraph root_to_leaf(int root = 0) {\n\t\tGraph res(size());\n\t\tstd::vector<int> chk(size(), 0);\n\t\tchk[root] = 1;\n\t\tauto dfs = [&](auto self, int now) -> void {\n\t\t\tfor(auto &e : edges[now]) {\n\t\t\t\tif(chk[e.to] == 1) continue;\n\t\t\t\tchk[e.to] = 1;\n\t\t\t\tres.add_edge(now, e.to, e.cost, 1, 0);\n\t\t\t\tself(self, e.to);\n\t\t\t}\n\t\t};\n\t\tdfs(dfs, root);\n\t\treturn res;\n\t}\n\n\t// Ο(V+E)\n\t// directed graph from leaf to root\n\tGraph leaf_to_root(int root = 0) {\n\t\tGraph res(size());\n\t\tstd::vector<int> chk(size(), 0);\n\t\tchk[root] = 1;\n\t\tauto dfs = [&](auto self, int now) -> void {\n\t\t\tfor(auto &e : edges[now]) {\n\t\t\t\tif(chk[e.to] == 1) continue;\n\t\t\t\tchk[e.to] = 1;\n\t\t\t\tres.add_edge(e.to, now, e.cost, 1, 0);\n\t\t\t\tself(self, e.to);\n\t\t\t}\n\t\t};\n\t\tdfs(dfs, root);\n\t\treturn res;\n\t}\n\n\t// long long Chu_Liu_Edmonds(int root = 0) {}\n};\n#line 3 \"/Users/nok0/Documents/Programming/nok0/graph/scc.hpp\"\n\nstruct strongly_connected_components {\nprivate:\n\tenum { CHECKED = -1,\n\t\t UNCHECKED = -2 };\n\tconst Graph &graph_given;\n\tGraph graph_reversed;\n\tstd::vector<int> order, group_number; /* at the beginning of the building, 'group_number' is used as 'checked' */\n\n\tvoid dfs(int now) {\n\t\tif(group_number[now] != UNCHECKED) return;\n\t\tgroup_number[now] = CHECKED;\n\t\tfor(auto &e : graph_given[now]) dfs(e.to);\n\t\torder.push_back(now);\n\t}\n\n\tvoid rdfs(int now, int group_count) {\n\t\tif(group_number[now] != UNCHECKED) return;\n\t\tgroup_number[now] = group_count;\n\t\tfor(auto &e : graph_reversed[now]) rdfs(e.to, group_count);\n\t}\n\n\tvoid build(bool create_compressed_graph) {\n\t\tfor(int i = 0; i < (int)graph_given.size(); i++) dfs(i);\n\t\treverse(order.begin(), order.end());\n\t\tgroup_number.assign(graph_given.size(), UNCHECKED);\n\t\tint group = 0;\n\t\tfor(auto &i : order)\n\t\t\tif(group_number[i] == UNCHECKED) rdfs(i, group), group++;\n\t\tgraph_compressed.resize(group);\n\t\tgroups.resize(group);\n\t\tfor(int i = 0; i < (int)graph_given.size(); i++) groups[group_number[i]].push_back(i);\n\t\tif(create_compressed_graph) {\n\t\t\tstd::vector<int> edges(group, -1);\n\t\t\tfor(int i = 0; i < group; i++)\n\t\t\t\tfor(auto &vertex : groups[i])\n\t\t\t\t\tfor(auto &e : graph_given[vertex])\n\t\t\t\t\t\tif(group_number[e.to] != i and edges[group_number[e.to]] != i) {\n\t\t\t\t\t\t\tedges[group_number[e.to]] = i;\n\t\t\t\t\t\t\tgraph_compressed[i].emplace_back(group_number[e.to], 1);\n\t\t\t\t\t\t}\n\t\t}\n\t\treturn;\n\t}\n\npublic:\n\tstd::vector<std::vector<int>> groups;\n\tGraph graph_compressed;\n\n\tstrongly_connected_components(const Graph &g_, bool create_compressed_graph = false)\n\t : graph_given(g_), graph_reversed(g_.size()), group_number(g_.size(), UNCHECKED) {\n\t\tfor(size_t i = 0; i < g_.size(); i++)\n\t\t\tfor(auto &e : graph_given[i]) graph_reversed[e.to].emplace_back(i, 1);\n\t\tbuild(create_compressed_graph);\n\t}\n\n\tconst int &operator[](const int k) { return group_number[k]; }\n};\n#line 1 \"/Users/nok0/Documents/Programming/nok0/template.hpp\"\n#include <bits/stdc++.h>\nusing namespace std;\n#if __has_include(<atcoder/all>)\n#include <atcoder/all>\nusing namespace atcoder;\n#endif\n\n#pragma region Macros\n// rep macro\n#define foa(v, a) for(auto &v : a)\n#define REPname(a, b, c, d, e, ...) e\n#define REP(...) REPname(__VA_ARGS__, REP3, REP2, REP1, REP0)(__VA_ARGS__)\n#define REP0(x) for(int i = 0; i < (x); ++i)\n#define REP1(i, x) for(int i = 0; i < (x); ++i)\n#define REP2(i, l, r) for(int i = (l); i < (r); ++i)\n#define REP3(i, l, r, c) for(int i = (l); i < (r); i += (c))\n#define REPSname(a, b, c, ...) c\n#define REPS(...) REPSname(__VA_ARGS__, REPS1, REPS0)(__VA_ARGS__)\n#define REPS0(x) for(int i = 1; i <= (x); ++i)\n#define REPS1(i, x) for(int i = 1; i <= (x); ++i)\n#define RREPname(a, b, c, d, e, ...) e\n#define RREP(...) RREPname(__VA_ARGS__, RREP3, RREP2, RREP1, RREP0)(__VA_ARGS__)\n#define RREP0(x) for(int i = (x)-1; i >= 0; --i)\n#define RREP1(i, x) for(int i = (x)-1; i >= 0; --i)\n#define RREP2(i, r, l) for(int i = (r)-1; i >= (l); --i)\n#define RREP3(i, r, l, c) for(int i = (r)-1; i >= (l); i -= (c))\n#define RREPSname(a, b, c, ...) c\n#define RREPS(...) RREPSname(__VA_ARGS__, RREPS1, RREPS0)(__VA_ARGS__)\n#define RREPS0(x) for(int i = (x); i >= 1; --i)\n#define RREPS1(i, x) for(int i = (x); i >= 1; --i)\n\n// name macro\n#define pb push_back\n#define eb emplace_back\n#define SZ(x) ((int)(x).size())\n#define all(x) (x).begin(), (x).end()\n#define rall(x) (x).rbegin(), (x).rend()\n#define popcnt(x) __builtin_popcountll(x)\ntemplate <class T = int>\nusing V = std::vector<T>;\ntemplate <class T = int>\nusing VV = std::vector<std::vector<T>>;\ntemplate <class T>\nusing pqup = std::priority_queue<T, std::vector<T>, std::greater<T>>;\nusing ll = long long;\nusing ld = long double;\nusing int128 = __int128_t;\nusing pii = std::pair<int, int>;\nusing pll = std::pair<long long, long long>;\n\n// input macro\ntemplate <class T, class U>\nstd::istream &operator>>(std::istream &is, std::pair<T, U> &p) {\n\tis >> p.first >> p.second;\n\treturn is;\n}\ntemplate <class T>\nstd::istream &operator>>(std::istream &is, std::vector<T> &v) {\n\tfor(T &i : v) is >> i;\n\treturn is;\n}\nstd::istream &operator>>(std::istream &is, __int128_t &a) {\n\tstd::string s;\n\tis >> s;\n\t__int128_t ret = 0;\n\tfor(int i = 0; i < s.length(); i++)\n\t\tif('0' <= s[i] and s[i] <= '9')\n\t\t\tret = 10 * ret + s[i] - '0';\n\ta = ret * (s[0] == '-' ? -1 : 1);\n\treturn is;\n}\n#if __has_include(<atcoder/all>)\nstd::istream &operator>>(std::istream &is, atcoder::modint998244353 &a) {\n\tlong long v;\n\tis >> v;\n\ta = v;\n\treturn is;\n}\nstd::istream &operator>>(std::istream &is, atcoder::modint1000000007 &a) {\n\tlong long v;\n\tis >> v;\n\ta = v;\n\treturn is;\n}\ntemplate <int m>\nstd::istream &operator>>(std::istream &is, atcoder::static_modint<m> &a) {\n\tlong long v;\n\tis >> v;\n\ta = v;\n\treturn is;\n}\ntemplate <int m>\nstd::istream &operator>>(std::istream &is, atcoder::dynamic_modint<m> &a) {\n\tlong long v;\n\tis >> v;\n\ta = v;\n\treturn is;\n}\n#endif\nnamespace scanner {\nvoid scan(int &a) { std::cin >> a; }\nvoid scan(long long &a) { std::cin >> a; }\nvoid scan(std::string &a) { std::cin >> a; }\nvoid scan(char &a) { std::cin >> a; }\nvoid scan(char a[]) { std::scanf(\"%s\", a); }\nvoid scan(double &a) { std::cin >> a; }\nvoid scan(long double &a) { std::cin >> a; }\ntemplate <class T, class U>\nvoid scan(std::pair<T, U> &p) { std::cin >> p; }\ntemplate <class T>\nvoid scan(std::vector<T> &a) { std::cin >> a; }\nvoid INPUT() {}\ntemplate <class Head, class... Tail>\nvoid INPUT(Head &head, Tail &... tail) {\n\tscan(head);\n\tINPUT(tail...);\n}\n} // namespace scanner\n#define VEC(type, name, size) \\\n\tstd::vector<type> name(size); \\\n\tscanner::INPUT(name)\n#define VVEC(type, name, h, w) \\\n\tstd::vector<std::vector<type>> name(h, std::vector<type>(w)); \\\n\tscanner::INPUT(name)\n#define INT(...) \\\n\tint __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define LL(...) \\\n\tlong long __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define STR(...) \\\n\tstd::string __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define CHAR(...) \\\n\tchar __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define DOUBLE(...) \\\n\tdouble __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define LD(...) \\\n\tlong double __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n\n// output-macro\ntemplate <class T, class U>\nstd::ostream &operator<<(std::ostream &os, const std::pair<T, U> &p) {\n\tos << p.first << \" \" << p.second;\n\treturn os;\n}\ntemplate <class T>\nstd::ostream &operator<<(std::ostream &os, const std::vector<T> &a) {\n\tfor(int i = 0; i < int(a.size()); ++i) {\n\t\tif(i) os << \" \";\n\t\tos << a[i];\n\t}\n\treturn os;\n}\nstd::ostream &operator<<(std::ostream &dest, __int128_t &value) {\n\tstd::ostream::sentry s(dest);\n\tif(s) {\n\t\t__uint128_t tmp = value < 0 ? -value : value;\n\t\tchar buffer[128];\n\t\tchar *d = std::end(buffer);\n\t\tdo {\n\t\t\t--d;\n\t\t\t*d = \"0123456789\"[tmp % 10];\n\t\t\ttmp /= 10;\n\t\t} while(tmp != 0);\n\t\tif(value < 0) {\n\t\t\t--d;\n\t\t\t*d = '-';\n\t\t}\n\t\tint len = std::end(buffer) - d;\n\t\tif(dest.rdbuf()->sputn(d, len) != len) {\n\t\t\tdest.setstate(std::ios_base::badbit);\n\t\t}\n\t}\n\treturn dest;\n}\n#if __has_include(<atcoder/all>)\nstd::ostream &operator<<(std::ostream &os, const atcoder::modint998244353 &a) { return os << a.val(); }\nstd::ostream &operator<<(std::ostream &os, const atcoder::modint1000000007 &a) { return os << a.val(); }\ntemplate <int m>\nstd::ostream &operator<<(std::ostream &os, const atcoder::static_modint<m> &a) { return os << a.val(); }\ntemplate <int m>\nstd::ostream &operator<<(std::ostream &os, const atcoder::dynamic_modint<m> &a) { return os << a.val(); }\n#endif\ntemplate <class T>\nvoid print(const T a) { std::cout << a << '\\n'; }\ntemplate <class Head, class... Tail>\nvoid print(Head H, Tail... T) {\n\tstd::cout << H << ' ';\n\tprint(T...);\n}\ntemplate <class T>\nvoid printel(const T a) { std::cout << a << '\\n'; }\ntemplate <class T>\nvoid printel(const std::vector<T> &a) {\n\tfor(const auto &v : a)\n\t\tstd::cout << v << '\\n';\n}\ntemplate <class Head, class... Tail>\nvoid printel(Head H, Tail... T) {\n\tstd::cout << H << '\\n';\n\tprintel(T...);\n}\nvoid Yes(const bool b = true) { std::cout << (b ? \"Yes\\n\" : \"No\\n\"); }\nvoid No() { std::cout << \"No\\n\"; }\nvoid YES(const bool b = true) { std::cout << (b ? \"YES\\n\" : \"NO\\n\"); }\nvoid NO() { std::cout << \"NO\\n\"; }\nvoid err(const bool b = true) {\n\tif(b) {\n\t\tstd::cout << \"-1\\n\", exit(0);\n\t}\n}\n\n//debug macro\nnamespace debugger {\ntemplate <class T>\nvoid view(const std::vector<T> &a) {\n\tstd::cerr << \"{ \";\n\tfor(const auto &v : a) {\n\t\tstd::cerr << v << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\ntemplate <class T>\nvoid view(const std::vector<std::vector<T>> &a) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &v : a) {\n\t\tstd::cerr << \"\\t\";\n\t\tview(v);\n\t\tstd::cerr << \"\\n\";\n\t}\n\tstd::cerr << \"}\";\n}\ntemplate <class T, class U>\nvoid view(const std::vector<std::pair<T, U>> &a) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &p : a) std::cerr << \"\\t(\" << p.first << \", \" << p.second << \")\\n\";\n\tstd::cerr << \"}\";\n}\ntemplate <class T, class U>\nvoid view(const std::map<T, U> &m) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &p : m) std::cerr << \"\\t[\" << p.first << \"] : \" << p.second << \"\\n\";\n\tstd::cerr << \"}\";\n}\ntemplate <class T, class U>\nvoid view(const std::pair<T, U> &p) { std::cerr << \"(\" << p.first << \", \" << p.second << \")\"; }\ntemplate <class T>\nvoid view(const std::set<T> &s) {\n\tstd::cerr << \"{ \";\n\tfor(auto &v : s) {\n\t\tview(v);\n\t\tstd::cerr << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\n\ntemplate <class T>\nvoid view(const T &e) { std::cerr << e; }\n} // namespace debugger\n#ifdef LOCAL\nvoid debug_out() {}\ntemplate <typename Head, typename... Tail>\nvoid debug_out(Head H, Tail... T) {\n\tdebugger::view(H);\n\tstd::cerr << \", \";\n\tdebug_out(T...);\n}\n#define debug(...) \\\n\tdo { \\\n\t\tstd::cerr << __LINE__ << \" [\" << #__VA_ARGS__ << \"] : [\"; \\\n\t\tdebug_out(__VA_ARGS__); \\\n\t\tstd::cerr << \"\\b\\b]\\n\"; \\\n\t} while(false)\n#else\n#define debug(...) (void(0))\n#endif\n\n// vector macro\ntemplate <class T>\nint lb(const std::vector<T> &a, const T x) { return std::distance((a).begin(), std::lower_bound((a).begin(), (a).end(), (x))); }\ntemplate <class T>\nint ub(const std::vector<T> &a, const T x) { return std::distance((a).begin(), std::upper_bound((a).begin(), (a).end(), (x))); }\ntemplate <class T>\nvoid UNIQUE(std::vector<T> &a) {\n\tstd::sort(a.begin(), a.end());\n\ta.erase(std::unique(a.begin(), a.end()), a.end());\n}\ntemplate <class T>\nstd::vector<T> press(std::vector<T> &a) {\n\tauto res = a;\n\tUNIQUE(res);\n\tfor(auto &v : a)\n\t\tv = lb(res, v);\n\treturn res;\n}\n#define SORTname(a, b, c, ...) c\n#define SORT(...) SORTname(__VA_ARGS__, SORT1, SORT0, ...)(__VA_ARGS__)\n#define SORT0(a) std::sort((a).begin(), (a).end())\n#define SORT1(a, c) std::sort((a).begin(), (a).end(), [](const auto x, const auto y) { return x c y; })\ntemplate <class T>\nvoid ADD(std::vector<T> &a, const T x = 1) {\n\tfor(auto &v : a) v += x;\n}\ntemplate <class T>\nvoid SUB(std::vector<T> &a, const T x = 1) {\n\tfor(auto &v : a) v -= x;\n}\ntemplate <class T>\nvoid MUL(std::vector<T> &a, const T x) {\n\tfor(auto &v : a) v *= x;\n}\ntemplate <class T>\nvoid DIV(std::vector<T> &a, const T x) {\n\tfor(auto &v : a) v /= x;\n}\nstd::vector<std::pair<char, int>> rle(const string &s) {\n\tint n = s.size();\n\tstd::vector<std::pair<char, int>> ret;\n\tfor(int l = 0; l < n;) {\n\t\tint r = l + 1;\n\t\tfor(; r < n and s[l] == s[r]; r++) {}\n\t\tret.emplace_back(s[l], r - l);\n\t\tl = r;\n\t}\n\treturn ret;\n}\ntemplate <class T>\nstd::vector<std::pair<T, int>> rle(const std::vector<T> &v) {\n\tint n = v.size();\n\tstd::vector<std::pair<T, int>> ret;\n\tfor(int l = 0; l < n;) {\n\t\tint r = l + 1;\n\t\tfor(; r < n and v[l] == v[r]; r++) {}\n\t\tret.emplace_back(v[l], r - l);\n\t\tl = r;\n\t}\n\treturn ret;\n}\n\n// math macro\ntemplate <class T, class U>\ninline bool chmin(T &a, const U &b) { return a > b ? a = b, true : false; }\ntemplate <class T, class U>\ninline bool chmax(T &a, const U &b) { return a < b ? a = b, true : false; }\ntemplate <class T>\nT divup(T x, T y) { return (x + y - 1) / y; }\ntemplate <class T>\nT POW(T a, long long n) {\n\tT ret = 1;\n\twhile(n) {\n\t\tif(n & 1) ret *= a;\n\t\ta *= a;\n\t\tn >>= 1;\n\t}\n\treturn ret;\n}\n// modpow\nlong long POW(long long a, long long n, const int mod) {\n\tlong long ret = 1;\n\ta = (a % mod + mod) % mod;\n\twhile(n) {\n\t\tif(n & 1) (ret *= a) %= mod;\n\t\t(a *= a) %= mod;\n\t\tn >>= 1;\n\t}\n\treturn ret;\n}\n\n// others\nstruct fast_io {\n\tfast_io() {\n\t\tios::sync_with_stdio(false);\n\t\tcin.tie(nullptr);\n\t\tcout << fixed << setprecision(15);\n\t}\n} fast_io_;\nconst int inf = 1e9;\nconst ll INF = 1e18;\n#pragma endregion\n\nvoid main_();\n\nint main() {\n\tmain_();\n\treturn 0;\n}\n#line 4 \"h.cpp\"\n\nconst int MAX_LEN_P = 11;\nvoid main_() {\n\t//input\n\tINT(n, m);\n\tVEC(string, s, n);\n\tvector adj(n, vector(n, false));\n\tREP(i, m) {\n\t\tINT(x, y);\n\t\tadj[--x][--y] = true;\n\t}\n\n\tint N = n * n * MAX_LEN_P * 2;\n\tGraph g(N);\n\t//一番左がword l , 一番右がword r, 左がlen文字余ってる\n\tauto left_id = [&](int l, int r, int len) {\n\t\treturn l * n * MAX_LEN_P + r * MAX_LEN_P + len;\n\t};\n\tauto right_id = [&](int l, int r, int len) {\n\t\treturn n * n * MAX_LEN_P + l * n * MAX_LEN_P + r * MAX_LEN_P + len;\n\t};\n\n\tmap<pii, int> mp;\n\n\t//aaba -> aabaabc\n\t//左が余っている場合->右を追加する\n\tREP(l, n) {\n\t\tREP(len, SZ(s[l]) + 1) {\n\t\t\tauto amari = s[l].substr(0, len);\n\t\t\treverse(all(amari));\n\t\t\tREP(r, n) {\n\t\t\t\tREP(len2, SZ(s[r]) + 1) {\n\t\t\t\t\tauto kesu = s[r].substr(0, len2);\n\t\t\t\t\tif(amari != kesu) continue;\n\t\t\t\t\tREP(r2, n) {\n\t\t\t\t\t\tif(!adj[r2][r]) continue;\n\t\t\t\t\t\tint u = left_id(l, r2, len);\n\t\t\t\t\t\tint v = right_id(l, r, SZ(s[r]) - len2);\n\t\t\t\t\t\tg.add_edge(u, v, SZ(s[r]), 1);\n\t\t\t\t\t\tchmax(mp[{u, v}], SZ(s[r]));\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\t//右が余っている場合\n\tREP(r, n) {\n\t\tREP(len, SZ(s[r]) + 1) {\n\t\t\tauto amari = s[r].substr(SZ(s[r]) - len);\n\t\t\treverse(all(amari));\n\t\t\tREP(l, n) {\n\t\t\t\tREP(len2, SZ(s[l]) + 1) {\n\t\t\t\t\tauto kesu = s[l].substr(SZ(s[l]) - len2);\n\t\t\t\t\tif(amari != kesu) continue;\n\t\t\t\t\tREP(l2, n) {\n\t\t\t\t\t\tif(!adj[l][l2]) continue;\n\t\t\t\t\t\tint u = right_id(l2, r, len);\n\t\t\t\t\t\tint v = left_id(l, r, SZ(s[l]) - len2);\n\t\t\t\t\t\tg.add_edge(u, v, SZ(s[l]), 1);\n\t\t\t\t\t\tchmax(mp[{u, v}], SZ(s[l]));\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\t//左が余ってる->右追加する->やっぱり左余る\n\tREP(l, n) {\n\t\tREP(len, SZ(s[l]) + 1) {\n\t\t\tREP(r, n) {\n\t\t\t\tif(len < SZ(s[r])) continue;\n\t\t\t\tint len2 = SZ(s[r]);\n\t\t\t\tstring lef = s[l].substr(len - len2, len2);\n\t\t\t\treverse(all(lef));\n\t\t\t\tif(lef == s[r]) {\n\t\t\t\t\tREP(r2, n) {\n\t\t\t\t\t\tif(!adj[r2][r]) continue;\n\t\t\t\t\t\tint u = left_id(l, r2, len);\n\t\t\t\t\t\tint v = left_id(l, r, len - len2);\n\t\t\t\t\t\tg.add_edge(u, v, SZ(s[r]), 1);\n\t\t\t\t\t\tchmax(mp[{u, v}], SZ(s[r]));\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\t//右が余ってる->左追加する->やっぱり右余る\n\tREP(r, n) {\n\t\tREP(len, SZ(s[r]) + 1) {\n\t\t\tREP(l, n) {\n\t\t\t\tif(len < SZ(s[l])) continue;\n\t\t\t\tint len2 = SZ(s[l]);\n\t\t\t\tstring rig = s[r].substr(SZ(s[r]) - len, len2);\n\t\t\t\treverse(all(rig));\n\t\t\t\tif(rig == s[l]) {\n\t\t\t\t\tREP(l2, n) {\n\t\t\t\t\t\tif(!adj[l][l2]) continue;\n\t\t\t\t\t\tint u = right_id(l2, r, len);\n\t\t\t\t\t\tint v = right_id(l, r, len - len2);\n\t\t\t\t\t\tg.add_edge(u, v, SZ(s[l]), 1);\n\t\t\t\t\t\tchmax(mp[{u, v}], SZ(s[l]));\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tstrongly_connected_components scc(g, 1);\n\tauto &h = scc.graph_compressed;\n\n\tN = SZ(h);\n\n\tvector dp(N, -inf);\n\tvector vis(N, false);\n\n\tREP(i, n) {\n\t\t//左を余らせる場合\n\t\tREP(len, SZ(s[i]) + 1) {\n\t\t\tstring lef = s[i].substr(len);\n\t\t\tstring rig = lef;\n\t\t\treverse(all(rig));\n\t\t\tif(lef == rig) {\n\t\t\t\tdp[scc[left_id(i, i, len)]] = SZ(s[i]);\n\t\t\t\tvis[scc[left_id(i, i, len)]] = 1;\n\t\t\t}\n\t\t}\n\n\t\t//右を余らせる場合\n\t\tREP(len, SZ(s[i]) + 1) {\n\t\t\tstring lef = s[i].substr(0, SZ(s[i]) - len);\n\t\t\tstring rig = lef;\n\t\t\treverse(all(rig));\n\t\t\tif(lef == rig) {\n\t\t\t\tdp[scc[right_id(i, i, len)]] = SZ(s[i]);\n\t\t\t\tvis[scc[right_id(i, i, len)]] = 1;\n\t\t\t}\n\t\t}\n\t}\n\n\tauto &groups = scc.groups;\n\t//debug(groups);\n\n\tREP(i, N) {\n\t\tif(SZ(groups[i]) != 1) dp[i] = inf;\n\t}\n\n\tauto tsort = h.topological_sort();\n\n\tfoa(idx, tsort) {\n\t\tif(!vis[idx]) continue;\n\t\tfor(auto [to, cost] : h[idx]) {\n\t\t\tvis[to] = true;\n\t\t\tchmax(dp[to], dp[idx] + mp[{groups[idx][0], groups[to][0]}]);\n\t\t}\n\t}\n\n\tint res = 0;\n\tREP(i, n) {\n\t\tREP(j, n) {\n\t\t\tint lid = scc[left_id(i, j, 0)];\n\t\t\tif(vis[lid]) {\n\t\t\t\tchmax(res, dp[lid]);\n\t\t\t}\n\t\t\tint rid = scc[right_id(i, j, 0)];\n\t\t\tif(vis[rid]) {\n\t\t\t\tchmax(res, dp[rid]);\n\t\t\t}\n\t\t}\n\t}\n\n\tprint(res >= inf ? -1 : res);\n}", "accuracy": 1, "time_ms": 190, "memory_kb": 87896, "score_of_the_acc": -0.9053, "final_rank": 10 }, { "submission_id": "aoj_2307_5846516", "code_snippet": "#line 2 \"/Users/nok0/Documents/Programming/nok0/graph/graph.hpp\"\n#include <algorithm>\n#include <cassert>\n#include <deque>\n#include <iostream>\n#include <queue>\n#include <tuple>\n#include <utility>\n#include <vector>\n\nstruct Edge {\n\tint to;\n\tlong long cost;\n\tEdge() = default;\n\tEdge(int to_, long long cost_) : to(to_), cost(cost_) {}\n\tbool operator<(const Edge &a) const { return cost < a.cost; }\n\tbool operator>(const Edge &a) const { return cost > a.cost; }\n\tfriend std::ostream &operator<<(std::ostream &s, Edge &a) {\n\t\ts << \"to: \" << a.to << \", cost: \" << a.cost;\n\t\treturn s;\n\t}\n};\n\nclass Graph {\n\tstd::vector<std::vector<Edge>> edges;\n\n\ttemplate <class F>\n\tstruct rec_lambda {\n\t\tF f;\n\t\trec_lambda(F &&f_) : f(std::forward<F>(f_)) {}\n\t\ttemplate <class... Args>\n\t\tauto operator()(Args &&... args) const {\n\t\t\treturn f(*this, std::forward<Args>(args)...);\n\t\t}\n\t};\n\npublic:\n\tinline const std::vector<Edge> &operator[](int k) const { return edges[k]; }\n\tinline std::vector<Edge> &operator[](int k) { return edges[k]; }\n\n\tint size() const { return edges.size(); }\n\tvoid resize(const int n) { edges.resize(n); }\n\n\tGraph() = default;\n\tGraph(int n) : edges(n) {}\n\tGraph(int n, int e, bool weight = 0, bool directed = 0, int idx = 1) : edges(n) { input(e, weight, directed, idx); }\n\tconst long long INF = 3e18;\n\n\tvoid input(int e = -1, bool weight = 0, bool directed = false, int idx = 1) {\n\t\tif(e == -1) e = size() - 1;\n\t\twhile(e--) {\n\t\t\tint u, v;\n\t\t\tlong long cost = 1;\n\t\t\tstd::cin >> u >> v;\n\t\t\tif(weight) std::cin >> cost;\n\t\t\tu -= idx, v -= idx;\n\t\t\tedges[u].emplace_back(v, cost);\n\t\t\tif(!directed) edges[v].emplace_back(u, cost);\n\t\t}\n\t}\n\n\tvoid add_edge(int u, int v, long long cost = 1, bool directed = false, int idx = 0) {\n\t\tu -= idx, v -= idx;\n\t\tedges[u].emplace_back(v, cost);\n\t\tif(!directed) edges[v].emplace_back(u, cost);\n\t}\n\n\t// Ο(V+E)\n\tstd::vector<long long> bfs(int s) {\n\t\tstd::vector<long long> dist(size(), INF);\n\t\tstd::queue<int> que;\n\t\tdist[s] = 0;\n\t\tque.push(s);\n\t\twhile(!que.empty()) {\n\t\t\tint v = que.front();\n\t\t\tque.pop();\n\t\t\tfor(auto &e : edges[v]) {\n\t\t\t\tif(dist[e.to] != INF) continue;\n\t\t\t\tdist[e.to] = dist[v] + e.cost;\n\t\t\t\tque.push(e.to);\n\t\t\t}\n\t\t}\n\t\treturn dist;\n\t}\n\n\t// Ο(V+E)\n\t// constraint: cost of each edge is zero or one\n\tstd::vector<long long> zero_one_bfs(int s) {\n\t\tstd::vector<long long> dist(size(), INF);\n\t\tstd::deque<int> deq;\n\t\tdist[s] = 0;\n\t\tdeq.push_back(s);\n\t\twhile(!deq.empty()) {\n\t\t\tint v = deq.front();\n\t\t\tdeq.pop_front();\n\t\t\tfor(auto &e : edges[v]) {\n\t\t\t\tassert(0LL <= e.cost and e.cost < 2LL);\n\t\t\t\tif(e.cost and dist[e.to] > dist[v] + 1) {\n\t\t\t\t\tdist[e.to] = dist[v] + 1;\n\t\t\t\t\tdeq.push_back(e.to);\n\t\t\t\t} else if(!e.cost and dist[e.to] > dist[v]) {\n\t\t\t\t\tdist[e.to] = dist[v];\n\t\t\t\t\tdeq.push_front(e.to);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn dist;\n\t}\n\n\t// Ο((E+V)logV)\n\t// cannot reach: INF\n\tstd::vector<long long> dijkstra(int s) { // verified\n\t\tstd::vector<long long> dist(size(), INF);\n\t\tconst auto compare = [](const std::pair<long long, int> &a, const std::pair<long long, int> &b) { return a.first > b.first; };\n\t\tstd::priority_queue<std::pair<long long, int>, std::vector<std::pair<long long, int>>, decltype(compare)> que{compare};\n\t\tdist[s] = 0;\n\t\tque.emplace(0, s);\n\t\twhile(!que.empty()) {\n\t\t\tstd::pair<long long, int> p = que.top();\n\t\t\tque.pop();\n\t\t\tint v = p.second;\n\t\t\tif(dist[v] < p.first) continue;\n\t\t\tfor(auto &e : edges[v]) {\n\t\t\t\tif(dist[e.to] > dist[v] + e.cost) {\n\t\t\t\t\tdist[e.to] = dist[v] + e.cost;\n\t\t\t\t\tque.emplace(dist[e.to], e.to);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn dist;\n\t}\n\n\t// Ο(VE)\n\t// cannot reach: INF\n\t// negative cycle: -INF\n\tstd::vector<long long> bellman_ford(int s) { // verified\n\t\tint n = size();\n\t\tstd::vector<long long> res(n, INF);\n\t\tres[s] = 0;\n\t\tfor(int loop = 0; loop < n - 1; loop++) {\n\t\t\tfor(int v = 0; v < n; v++) {\n\t\t\t\tif(res[v] == INF) continue;\n\t\t\t\tfor(auto &e : edges[v]) {\n\t\t\t\t\tres[e.to] = std::min(res[e.to], res[v] + e.cost);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tstd::queue<int> que;\n\t\tstd::vector<int> chk(n);\n\t\tfor(int v = 0; v < n; v++) {\n\t\t\tif(res[v] == INF) continue;\n\t\t\tfor(auto &e : edges[v]) {\n\t\t\t\tif(res[e.to] > res[v] + e.cost and !chk[e.to]) {\n\t\t\t\t\tque.push(e.to);\n\t\t\t\t\tchk[e.to] = 1;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\twhile(!que.empty()) {\n\t\t\tint now = que.front();\n\t\t\tque.pop();\n\t\t\tfor(auto &e : edges[now]) {\n\t\t\t\tif(!chk[e.to]) {\n\t\t\t\t\tchk[e.to] = 1;\n\t\t\t\t\tque.push(e.to);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tfor(int i = 0; i < n; i++)\n\t\t\tif(chk[i]) res[i] = -INF;\n\t\treturn res;\n\t}\n\n\t// Ο(V^3)\n\tstd::vector<std::vector<long long>> warshall_floyd() { // verified\n\t\tint n = size();\n\t\tstd::vector<std::vector<long long>> dist(n, std::vector<long long>(n, INF));\n\t\tfor(int i = 0; i < n; i++) dist[i][i] = 0;\n\t\tfor(int i = 0; i < n; i++)\n\t\t\tfor(auto &e : edges[i]) dist[i][e.to] = std::min(dist[i][e.to], e.cost);\n\t\tfor(int k = 0; k < n; k++)\n\t\t\tfor(int i = 0; i < n; i++) {\n\t\t\t\tif(dist[i][k] == INF) continue;\n\t\t\t\tfor(int j = 0; j < n; j++) {\n\t\t\t\t\tif(dist[k][j] == INF) continue;\n\t\t\t\t\tdist[i][j] = std::min(dist[i][j], dist[i][k] + dist[k][j]);\n\t\t\t\t}\n\t\t\t}\n\t\treturn dist;\n\t}\n\n\t// Ο(V) (using DFS)\n\t// if a directed cycle exists, return {}\n\tstd::vector<int> topological_sort() { // verified\n\t\tstd::vector<int> res;\n\t\tint n = size();\n\t\tstd::vector<int> used(n, 0);\n\t\tbool not_DAG = false;\n\t\tfor(int i = 0; i < n; i++) {\n\t\t\trec_lambda([&](auto &&dfs, int k) -> void {\n\t\t\t\tif(not_DAG) return;\n\t\t\t\tif(used[k]) {\n\t\t\t\t\tif(used[k] == 1) not_DAG = true;\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tused[k] = 1;\n\t\t\t\tfor(auto &e : edges[k]) dfs(e.to);\n\t\t\t\tused[k] = 2;\n\t\t\t\tres.push_back(k);\n\t\t\t})(i);\n\t\t}\n\t\tif(not_DAG) return std::vector<int>{};\n\t\tstd::reverse(res.begin(), res.end());\n\t\treturn res;\n\t}\n\n\tbool is_DAG() { return !topological_sort().empty(); } // verified\n\n\t// Ο(V)\n\t// array of the distance from each vertex to the most distant vertex\n\tstd::vector<long long> height() { // verified\n\t\tauto vec1 = bfs(0);\n\t\tint v1 = -1, v2 = -1;\n\t\tlong long dia = -1;\n\t\tfor(int i = 0; i < int(size()); i++)\n\t\t\tif(dia < vec1[i]) dia = vec1[i], v1 = i;\n\t\tvec1 = bfs(v1);\n\t\tdia = -1;\n\t\tfor(int i = 0; i < int(size()); i++)\n\t\t\tif(dia < vec1[i]) dia = vec1[i], v2 = i;\n\t\tauto vec2 = bfs(v2);\n\t\tfor(int i = 0; i < int(size()); i++)\n\t\t\tif(vec1[i] < vec2[i]) vec1[i] = vec2[i];\n\t\treturn vec1;\n\t}\n\n\t// O(V+E)\n\t// vector<(int)(0 or 1)>\n\t// if it is not bipartite, return {}\n\tstd::vector<int> bipartite_grouping() {\n\t\tstd::vector<int> colors(size(), -1);\n\t\tauto dfs = [&](auto self, int now, int col) -> bool {\n\t\t\tcolors[now] = col;\n\t\t\tfor(auto &e : edges[now]) {\n\t\t\t\tif(col == colors[e.to]) return false;\n\t\t\t\tif(colors[e.to] == -1 and !self(self, e.to, !col)) return false;\n\t\t\t}\n\t\t\treturn true;\n\t\t};\n\t\tfor(int i = 0; i < int(size()); i++)\n\t\t\tif(colors[i] == -1 and !dfs(dfs, i, 0)) return std::vector<int>{};\n\t\treturn colors;\n\t}\n\n\tbool is_bipartite() { return !bipartite_grouping().empty(); }\n\n\t// Ο(V+E)\n\t// ((v1, v2), diameter)\n\tstd::pair<std::pair<int, int>, long long> diameter() { // verified\n\t\tauto vec = bfs(0);\n\t\tint v1 = -1, v2 = -1;\n\t\tlong long dia = -1;\n\t\tfor(int i = 0; i < int(size()); i++)\n\t\t\tif(dia < vec[i]) dia = vec[i], v1 = i;\n\t\tvec = bfs(v1);\n\t\tdia = -1;\n\t\tfor(int i = 0; i < int(size()); i++)\n\t\t\tif(dia < vec[i]) dia = vec[i], v2 = i;\n\t\tstd::pair<std::pair<int, int>, long long> res = {{v1, v2}, dia};\n\t\treturn res;\n\t}\n\n\t// Ο(V+E)\n\t// return {s, v1, v2, ... t}\n\tstd::vector<int> diameter_path() {\n\t\tauto vec = bfs(0);\n\t\tint v1 = -1, v2 = -1;\n\t\tlong long dia = -1;\n\t\tfor(int i = 0; i < int(size()); i++)\n\t\t\tif(dia < vec[i]) dia = vec[i], v1 = i;\n\t\tauto vec2 = bfs(v1);\n\t\tdia = -1;\n\t\tfor(int i = 0; i < int(size()); i++)\n\t\t\tif(dia < vec2[i]) dia = vec2[i], v2 = i;\n\t\tvec = bfs(v2);\n\t\tstd::vector<int> ret;\n\t\tauto dfs = [&](auto self, int now, int p) -> void {\n\t\t\tret.emplace_back(now);\n\t\t\tif(now == v2) return;\n\t\t\tfor(auto &[to, cost] : (*this)[now]) {\n\t\t\t\tif(vec[to] + vec2[to] == dia and to != p)\n\t\t\t\t\tself(self, to, now);\n\t\t\t}\n\t\t};\n\t\tdfs(dfs, v1, -1);\n\t\treturn ret;\n\t}\n\n\t// Ο(V+E)\n\t// return subtree_size, root = root\n\tstd::vector<int> subtree_size(const int root) {\n\t\tint n = size();\n\t\tstd::vector<int> ret(n, 1);\n\t\trec_lambda([&](auto &&dfs, int now, int p) -> void {\n\t\t\tfor(auto &[to, cost] : (*this)[now]) {\n\t\t\t\tif(to == p) continue;\n\t\t\t\tdfs(to, now);\n\t\t\t\tret[now] += ret[to];\n\t\t\t}\n\t\t})(root, -1);\n\t\treturn ret;\n\t}\n\n\t// Ο(ElogV)\n\tlong long prim() { // verified\n\t\tlong long res = 0;\n\t\tstd::priority_queue<Edge, std::vector<Edge>, std::greater<Edge>> que;\n\t\tfor(auto &e : edges[0]) que.push(e);\n\t\tstd::vector<int> chk(size());\n\t\tchk[0] = 1;\n\t\tint cnt = 1;\n\t\twhile(cnt < size()) {\n\t\t\tauto e = que.top();\n\t\t\tque.pop();\n\t\t\tif(chk[e.to]) continue;\n\t\t\tcnt++;\n\t\t\tres += e.cost;\n\t\t\tchk[e.to] = 1;\n\t\t\tfor(auto &e2 : edges[e.to]) que.push(e2);\n\t\t}\n\t\treturn res;\n\t}\n\n\t// Ο(ElogE)\n\tlong long kruskal() { // verified\n\t\tstd::vector<std::tuple<int, int, long long>> Edges;\n\t\tfor(int i = 0; i < int(size()); i++)\n\t\t\tfor(auto &e : edges[i]) Edges.emplace_back(i, e.to, e.cost);\n\t\tstd::sort(Edges.begin(), Edges.end(), [](const std::tuple<int, int, long long> &a, const std::tuple<int, int, long long> &b) {\n\t\t\treturn std::get<2>(a) < std::get<2>(b);\n\t\t});\n\t\tstd::vector<int> uf_data(size(), -1);\n\t\tauto root = [&uf_data](auto self, int x) -> int {\n\t\t\tif(uf_data[x] < 0) return x;\n\t\t\treturn uf_data[x] = self(self, uf_data[x]);\n\t\t};\n\t\tauto unite = [&uf_data, &root](int u, int v) -> bool {\n\t\t\tu = root(root, u), v = root(root, v);\n\t\t\tif(u == v) return false;\n\t\t\tif(uf_data[u] > uf_data[v]) std::swap(u, v);\n\t\t\tuf_data[u] += uf_data[v];\n\t\t\tuf_data[v] = u;\n\t\t\treturn true;\n\t\t};\n\t\tlong long ret = 0;\n\t\tfor(auto &e : Edges)\n\t\t\tif(unite(std::get<0>(e), std::get<1>(e))) ret += std::get<2>(e);\n\t\treturn ret;\n\t}\n\n\t// O(V)\n\tstd::vector<int> centroid() {\n\t\tint n = size();\n\t\tstd::vector<int> centroid, sz(n);\n\t\tauto dfs = [&](auto self, int now, int per) -> void {\n\t\t\tsz[now] = 1;\n\t\t\tbool is_centroid = true;\n\t\t\tfor(auto &e : edges[now]) {\n\t\t\t\tif(e.to != per) {\n\t\t\t\t\tself(self, e.to, now);\n\t\t\t\t\tsz[now] += sz[e.to];\n\t\t\t\t\tif(sz[e.to] > n / 2) is_centroid = false;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(n - sz[now] > n / 2) is_centroid = false;\n\t\t\tif(is_centroid) centroid.push_back(now);\n\t\t};\n\t\tdfs(dfs, 0, -1);\n\t\treturn centroid;\n\t}\n\n\t// Ο(V+E)\n\t// directed graph from root to leaf\n\tGraph root_to_leaf(int root = 0) {\n\t\tGraph res(size());\n\t\tstd::vector<int> chk(size(), 0);\n\t\tchk[root] = 1;\n\t\tauto dfs = [&](auto self, int now) -> void {\n\t\t\tfor(auto &e : edges[now]) {\n\t\t\t\tif(chk[e.to] == 1) continue;\n\t\t\t\tchk[e.to] = 1;\n\t\t\t\tres.add_edge(now, e.to, e.cost, 1, 0);\n\t\t\t\tself(self, e.to);\n\t\t\t}\n\t\t};\n\t\tdfs(dfs, root);\n\t\treturn res;\n\t}\n\n\t// Ο(V+E)\n\t// directed graph from leaf to root\n\tGraph leaf_to_root(int root = 0) {\n\t\tGraph res(size());\n\t\tstd::vector<int> chk(size(), 0);\n\t\tchk[root] = 1;\n\t\tauto dfs = [&](auto self, int now) -> void {\n\t\t\tfor(auto &e : edges[now]) {\n\t\t\t\tif(chk[e.to] == 1) continue;\n\t\t\t\tchk[e.to] = 1;\n\t\t\t\tres.add_edge(e.to, now, e.cost, 1, 0);\n\t\t\t\tself(self, e.to);\n\t\t\t}\n\t\t};\n\t\tdfs(dfs, root);\n\t\treturn res;\n\t}\n\n\t// long long Chu_Liu_Edmonds(int root = 0) {}\n};\n#line 3 \"/Users/nok0/Documents/Programming/nok0/graph/scc.hpp\"\n\nstruct strongly_connected_components {\nprivate:\n\tenum { CHECKED = -1,\n\t\t UNCHECKED = -2 };\n\tconst Graph &graph_given;\n\tGraph graph_reversed;\n\tstd::vector<int> order, group_number; /* at the beginning of the building, 'group_number' is used as 'checked' */\n\n\tvoid dfs(int now) {\n\t\tif(group_number[now] != UNCHECKED) return;\n\t\tgroup_number[now] = CHECKED;\n\t\tfor(auto &e : graph_given[now]) dfs(e.to);\n\t\torder.push_back(now);\n\t}\n\n\tvoid rdfs(int now, int group_count) {\n\t\tif(group_number[now] != UNCHECKED) return;\n\t\tgroup_number[now] = group_count;\n\t\tfor(auto &e : graph_reversed[now]) rdfs(e.to, group_count);\n\t}\n\n\tvoid build(bool create_compressed_graph) {\n\t\tfor(int i = 0; i < (int)graph_given.size(); i++) dfs(i);\n\t\treverse(order.begin(), order.end());\n\t\tgroup_number.assign(graph_given.size(), UNCHECKED);\n\t\tint group = 0;\n\t\tfor(auto &i : order)\n\t\t\tif(group_number[i] == UNCHECKED) rdfs(i, group), group++;\n\t\tgraph_compressed.resize(group);\n\t\tgroups.resize(group);\n\t\tfor(int i = 0; i < (int)graph_given.size(); i++) groups[group_number[i]].push_back(i);\n\t\tif(create_compressed_graph) {\n\t\t\tstd::vector<int> edges(group, -1);\n\t\t\tfor(int i = 0; i < group; i++)\n\t\t\t\tfor(auto &vertex : groups[i])\n\t\t\t\t\tfor(auto &e : graph_given[vertex])\n\t\t\t\t\t\tif(group_number[e.to] != i and edges[group_number[e.to]] != i) {\n\t\t\t\t\t\t\tedges[group_number[e.to]] = i;\n\t\t\t\t\t\t\tgraph_compressed[i].emplace_back(group_number[e.to], 1);\n\t\t\t\t\t\t}\n\t\t}\n\t\treturn;\n\t}\n\npublic:\n\tstd::vector<std::vector<int>> groups;\n\tGraph graph_compressed;\n\n\tstrongly_connected_components(const Graph &g_, bool create_compressed_graph = false)\n\t : graph_given(g_), graph_reversed(g_.size()), group_number(g_.size(), UNCHECKED) {\n\t\tfor(size_t i = 0; i < g_.size(); i++)\n\t\t\tfor(auto &e : graph_given[i]) graph_reversed[e.to].emplace_back(i, 1);\n\t\tbuild(create_compressed_graph);\n\t}\n\n\tconst int &operator[](const int k) { return group_number[k]; }\n};\n#line 1 \"/Users/nok0/Documents/Programming/nok0/template.hpp\"\n#include <bits/stdc++.h>\nusing namespace std;\n#if __has_include(<atcoder/all>)\n#include <atcoder/all>\nusing namespace atcoder;\n#endif\n\n#pragma region Macros\n// rep macro\n#define foa(v, a) for(auto &v : a)\n#define REPname(a, b, c, d, e, ...) e\n#define REP(...) REPname(__VA_ARGS__, REP3, REP2, REP1, REP0)(__VA_ARGS__)\n#define REP0(x) for(int i = 0; i < (x); ++i)\n#define REP1(i, x) for(int i = 0; i < (x); ++i)\n#define REP2(i, l, r) for(int i = (l); i < (r); ++i)\n#define REP3(i, l, r, c) for(int i = (l); i < (r); i += (c))\n#define REPSname(a, b, c, ...) c\n#define REPS(...) REPSname(__VA_ARGS__, REPS1, REPS0)(__VA_ARGS__)\n#define REPS0(x) for(int i = 1; i <= (x); ++i)\n#define REPS1(i, x) for(int i = 1; i <= (x); ++i)\n#define RREPname(a, b, c, d, e, ...) e\n#define RREP(...) RREPname(__VA_ARGS__, RREP3, RREP2, RREP1, RREP0)(__VA_ARGS__)\n#define RREP0(x) for(int i = (x)-1; i >= 0; --i)\n#define RREP1(i, x) for(int i = (x)-1; i >= 0; --i)\n#define RREP2(i, r, l) for(int i = (r)-1; i >= (l); --i)\n#define RREP3(i, r, l, c) for(int i = (r)-1; i >= (l); i -= (c))\n#define RREPSname(a, b, c, ...) c\n#define RREPS(...) RREPSname(__VA_ARGS__, RREPS1, RREPS0)(__VA_ARGS__)\n#define RREPS0(x) for(int i = (x); i >= 1; --i)\n#define RREPS1(i, x) for(int i = (x); i >= 1; --i)\n\n// name macro\n#define pb push_back\n#define eb emplace_back\n#define SZ(x) ((int)(x).size())\n#define all(x) (x).begin(), (x).end()\n#define rall(x) (x).rbegin(), (x).rend()\n#define popcnt(x) __builtin_popcountll(x)\ntemplate <class T = int>\nusing V = std::vector<T>;\ntemplate <class T = int>\nusing VV = std::vector<std::vector<T>>;\ntemplate <class T>\nusing pqup = std::priority_queue<T, std::vector<T>, std::greater<T>>;\nusing ll = long long;\nusing ld = long double;\nusing int128 = __int128_t;\nusing pii = std::pair<int, int>;\nusing pll = std::pair<long long, long long>;\n\n// input macro\ntemplate <class T, class U>\nstd::istream &operator>>(std::istream &is, std::pair<T, U> &p) {\n\tis >> p.first >> p.second;\n\treturn is;\n}\ntemplate <class T>\nstd::istream &operator>>(std::istream &is, std::vector<T> &v) {\n\tfor(T &i : v) is >> i;\n\treturn is;\n}\nstd::istream &operator>>(std::istream &is, __int128_t &a) {\n\tstd::string s;\n\tis >> s;\n\t__int128_t ret = 0;\n\tfor(int i = 0; i < s.length(); i++)\n\t\tif('0' <= s[i] and s[i] <= '9')\n\t\t\tret = 10 * ret + s[i] - '0';\n\ta = ret * (s[0] == '-' ? -1 : 1);\n\treturn is;\n}\n#if __has_include(<atcoder/all>)\nstd::istream &operator>>(std::istream &is, atcoder::modint998244353 &a) {\n\tlong long v;\n\tis >> v;\n\ta = v;\n\treturn is;\n}\nstd::istream &operator>>(std::istream &is, atcoder::modint1000000007 &a) {\n\tlong long v;\n\tis >> v;\n\ta = v;\n\treturn is;\n}\ntemplate <int m>\nstd::istream &operator>>(std::istream &is, atcoder::static_modint<m> &a) {\n\tlong long v;\n\tis >> v;\n\ta = v;\n\treturn is;\n}\ntemplate <int m>\nstd::istream &operator>>(std::istream &is, atcoder::dynamic_modint<m> &a) {\n\tlong long v;\n\tis >> v;\n\ta = v;\n\treturn is;\n}\n#endif\nnamespace scanner {\nvoid scan(int &a) { std::cin >> a; }\nvoid scan(long long &a) { std::cin >> a; }\nvoid scan(std::string &a) { std::cin >> a; }\nvoid scan(char &a) { std::cin >> a; }\nvoid scan(char a[]) { std::scanf(\"%s\", a); }\nvoid scan(double &a) { std::cin >> a; }\nvoid scan(long double &a) { std::cin >> a; }\ntemplate <class T, class U>\nvoid scan(std::pair<T, U> &p) { std::cin >> p; }\ntemplate <class T>\nvoid scan(std::vector<T> &a) { std::cin >> a; }\nvoid INPUT() {}\ntemplate <class Head, class... Tail>\nvoid INPUT(Head &head, Tail &... tail) {\n\tscan(head);\n\tINPUT(tail...);\n}\n} // namespace scanner\n#define VEC(type, name, size) \\\n\tstd::vector<type> name(size); \\\n\tscanner::INPUT(name)\n#define VVEC(type, name, h, w) \\\n\tstd::vector<std::vector<type>> name(h, std::vector<type>(w)); \\\n\tscanner::INPUT(name)\n#define INT(...) \\\n\tint __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define LL(...) \\\n\tlong long __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define STR(...) \\\n\tstd::string __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define CHAR(...) \\\n\tchar __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define DOUBLE(...) \\\n\tdouble __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define LD(...) \\\n\tlong double __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n\n// output-macro\ntemplate <class T, class U>\nstd::ostream &operator<<(std::ostream &os, const std::pair<T, U> &p) {\n\tos << p.first << \" \" << p.second;\n\treturn os;\n}\ntemplate <class T>\nstd::ostream &operator<<(std::ostream &os, const std::vector<T> &a) {\n\tfor(int i = 0; i < int(a.size()); ++i) {\n\t\tif(i) os << \" \";\n\t\tos << a[i];\n\t}\n\treturn os;\n}\nstd::ostream &operator<<(std::ostream &dest, __int128_t &value) {\n\tstd::ostream::sentry s(dest);\n\tif(s) {\n\t\t__uint128_t tmp = value < 0 ? -value : value;\n\t\tchar buffer[128];\n\t\tchar *d = std::end(buffer);\n\t\tdo {\n\t\t\t--d;\n\t\t\t*d = \"0123456789\"[tmp % 10];\n\t\t\ttmp /= 10;\n\t\t} while(tmp != 0);\n\t\tif(value < 0) {\n\t\t\t--d;\n\t\t\t*d = '-';\n\t\t}\n\t\tint len = std::end(buffer) - d;\n\t\tif(dest.rdbuf()->sputn(d, len) != len) {\n\t\t\tdest.setstate(std::ios_base::badbit);\n\t\t}\n\t}\n\treturn dest;\n}\n#if __has_include(<atcoder/all>)\nstd::ostream &operator<<(std::ostream &os, const atcoder::modint998244353 &a) { return os << a.val(); }\nstd::ostream &operator<<(std::ostream &os, const atcoder::modint1000000007 &a) { return os << a.val(); }\ntemplate <int m>\nstd::ostream &operator<<(std::ostream &os, const atcoder::static_modint<m> &a) { return os << a.val(); }\ntemplate <int m>\nstd::ostream &operator<<(std::ostream &os, const atcoder::dynamic_modint<m> &a) { return os << a.val(); }\n#endif\ntemplate <class T>\nvoid print(const T a) { std::cout << a << '\\n'; }\ntemplate <class Head, class... Tail>\nvoid print(Head H, Tail... T) {\n\tstd::cout << H << ' ';\n\tprint(T...);\n}\ntemplate <class T>\nvoid printel(const T a) { std::cout << a << '\\n'; }\ntemplate <class T>\nvoid printel(const std::vector<T> &a) {\n\tfor(const auto &v : a)\n\t\tstd::cout << v << '\\n';\n}\ntemplate <class Head, class... Tail>\nvoid printel(Head H, Tail... T) {\n\tstd::cout << H << '\\n';\n\tprintel(T...);\n}\nvoid Yes(const bool b = true) { std::cout << (b ? \"Yes\\n\" : \"No\\n\"); }\nvoid No() { std::cout << \"No\\n\"; }\nvoid YES(const bool b = true) { std::cout << (b ? \"YES\\n\" : \"NO\\n\"); }\nvoid NO() { std::cout << \"NO\\n\"; }\nvoid err(const bool b = true) {\n\tif(b) {\n\t\tstd::cout << \"-1\\n\", exit(0);\n\t}\n}\n\n//debug macro\nnamespace debugger {\ntemplate <class T>\nvoid view(const std::vector<T> &a) {\n\tstd::cerr << \"{ \";\n\tfor(const auto &v : a) {\n\t\tstd::cerr << v << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\ntemplate <class T>\nvoid view(const std::vector<std::vector<T>> &a) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &v : a) {\n\t\tstd::cerr << \"\\t\";\n\t\tview(v);\n\t\tstd::cerr << \"\\n\";\n\t}\n\tstd::cerr << \"}\";\n}\ntemplate <class T, class U>\nvoid view(const std::vector<std::pair<T, U>> &a) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &p : a) std::cerr << \"\\t(\" << p.first << \", \" << p.second << \")\\n\";\n\tstd::cerr << \"}\";\n}\ntemplate <class T, class U>\nvoid view(const std::map<T, U> &m) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &p : m) std::cerr << \"\\t[\" << p.first << \"] : \" << p.second << \"\\n\";\n\tstd::cerr << \"}\";\n}\ntemplate <class T, class U>\nvoid view(const std::pair<T, U> &p) { std::cerr << \"(\" << p.first << \", \" << p.second << \")\"; }\ntemplate <class T>\nvoid view(const std::set<T> &s) {\n\tstd::cerr << \"{ \";\n\tfor(auto &v : s) {\n\t\tview(v);\n\t\tstd::cerr << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\n\ntemplate <class T>\nvoid view(const T &e) { std::cerr << e; }\n} // namespace debugger\n#ifdef LOCAL\nvoid debug_out() {}\ntemplate <typename Head, typename... Tail>\nvoid debug_out(Head H, Tail... T) {\n\tdebugger::view(H);\n\tstd::cerr << \", \";\n\tdebug_out(T...);\n}\n#define debug(...) \\\n\tdo { \\\n\t\tstd::cerr << __LINE__ << \" [\" << #__VA_ARGS__ << \"] : [\"; \\\n\t\tdebug_out(__VA_ARGS__); \\\n\t\tstd::cerr << \"\\b\\b]\\n\"; \\\n\t} while(false)\n#else\n#define debug(...) (void(0))\n#endif\n\n// vector macro\ntemplate <class T>\nint lb(const std::vector<T> &a, const T x) { return std::distance((a).begin(), std::lower_bound((a).begin(), (a).end(), (x))); }\ntemplate <class T>\nint ub(const std::vector<T> &a, const T x) { return std::distance((a).begin(), std::upper_bound((a).begin(), (a).end(), (x))); }\ntemplate <class T>\nvoid UNIQUE(std::vector<T> &a) {\n\tstd::sort(a.begin(), a.end());\n\ta.erase(std::unique(a.begin(), a.end()), a.end());\n}\ntemplate <class T>\nstd::vector<T> press(std::vector<T> &a) {\n\tauto res = a;\n\tUNIQUE(res);\n\tfor(auto &v : a)\n\t\tv = lb(res, v);\n\treturn res;\n}\n#define SORTname(a, b, c, ...) c\n#define SORT(...) SORTname(__VA_ARGS__, SORT1, SORT0, ...)(__VA_ARGS__)\n#define SORT0(a) std::sort((a).begin(), (a).end())\n#define SORT1(a, c) std::sort((a).begin(), (a).end(), [](const auto x, const auto y) { return x c y; })\ntemplate <class T>\nvoid ADD(std::vector<T> &a, const T x = 1) {\n\tfor(auto &v : a) v += x;\n}\ntemplate <class T>\nvoid SUB(std::vector<T> &a, const T x = 1) {\n\tfor(auto &v : a) v -= x;\n}\ntemplate <class T>\nvoid MUL(std::vector<T> &a, const T x) {\n\tfor(auto &v : a) v *= x;\n}\ntemplate <class T>\nvoid DIV(std::vector<T> &a, const T x) {\n\tfor(auto &v : a) v /= x;\n}\nstd::vector<std::pair<char, int>> rle(const string &s) {\n\tint n = s.size();\n\tstd::vector<std::pair<char, int>> ret;\n\tfor(int l = 0; l < n;) {\n\t\tint r = l + 1;\n\t\tfor(; r < n and s[l] == s[r]; r++) {}\n\t\tret.emplace_back(s[l], r - l);\n\t\tl = r;\n\t}\n\treturn ret;\n}\ntemplate <class T>\nstd::vector<std::pair<T, int>> rle(const std::vector<T> &v) {\n\tint n = v.size();\n\tstd::vector<std::pair<T, int>> ret;\n\tfor(int l = 0; l < n;) {\n\t\tint r = l + 1;\n\t\tfor(; r < n and v[l] == v[r]; r++) {}\n\t\tret.emplace_back(v[l], r - l);\n\t\tl = r;\n\t}\n\treturn ret;\n}\n\n// math macro\ntemplate <class T, class U>\ninline bool chmin(T &a, const U &b) { return a > b ? a = b, true : false; }\ntemplate <class T, class U>\ninline bool chmax(T &a, const U &b) { return a < b ? a = b, true : false; }\ntemplate <class T>\nT divup(T x, T y) { return (x + y - 1) / y; }\ntemplate <class T>\nT POW(T a, long long n) {\n\tT ret = 1;\n\twhile(n) {\n\t\tif(n & 1) ret *= a;\n\t\ta *= a;\n\t\tn >>= 1;\n\t}\n\treturn ret;\n}\n// modpow\nlong long POW(long long a, long long n, const int mod) {\n\tlong long ret = 1;\n\ta = (a % mod + mod) % mod;\n\twhile(n) {\n\t\tif(n & 1) (ret *= a) %= mod;\n\t\t(a *= a) %= mod;\n\t\tn >>= 1;\n\t}\n\treturn ret;\n}\n\n// others\nstruct fast_io {\n\tfast_io() {\n\t\tios::sync_with_stdio(false);\n\t\tcin.tie(nullptr);\n\t\tcout << fixed << setprecision(15);\n\t}\n} fast_io_;\nconst int inf = 1e9;\nconst ll INF = 1e18;\n#pragma endregion\n\nvoid main_();\n\nint main() {\n\tmain_();\n\treturn 0;\n}\n#line 4 \"h.cpp\"\n\nconst int MAX_LEN_P = 11;\nvoid main_() {\n\tINT(n, m);\n\tVEC(string, s, n);\n\tvector adj(n, vector(n, false));\n\tREP(i, m) {\n\t\tINT(x, y);\n\t\tadj[--x][--y] = true;\n\t}\n\tint N = n * n * MAX_LEN_P * 2;\n\tGraph g(N);\n\tauto left_id = [&](int l, int r, int len) {\n\t\treturn l * n * MAX_LEN_P + r * MAX_LEN_P + len;\n\t};\n\tauto right_id = [&](int l, int r, int len) {\n\t\treturn n * n * MAX_LEN_P + l * n * MAX_LEN_P + r * MAX_LEN_P + len;\n\t};\n\n\tmap<pii, int> mp;\n\n\t//左が余っている場合\n\tREP(l, n) {\n\t\tREP(len, SZ(s[l]) + 1) {\n\t\t\tauto amari = s[l].substr(0, len);\n\t\t\treverse(all(amari));\n\t\t\tREP(r, n) {\n\t\t\t\tREP(len2, SZ(s[r]) + 1) {\n\t\t\t\t\tauto kesu = s[r].substr(0, len2);\n\t\t\t\t\tif(amari != kesu) continue;\n\t\t\t\t\tREP(r2, n) {\n\t\t\t\t\t\tif(!adj[r2][r]) continue;\n\t\t\t\t\t\tint u = left_id(l, r2, len);\n\t\t\t\t\t\tint v = right_id(l, r, SZ(s[r]) - len2);\n\t\t\t\t\t\tg.add_edge(u, v, SZ(s[r]), 1);\n\t\t\t\t\t\tchmax(mp[{u, v}], SZ(s[r]));\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\t//右が余る場合\n\tREP(r, n) {\n\t\tREP(len, SZ(s[r]) + 1) {\n\t\t\tauto amari = s[r].substr(SZ(s[r]) - len);\n\t\t\treverse(all(amari));\n\t\t\tREP(l, n) {\n\t\t\t\tREP(len2, SZ(s[l]) + 1) {\n\t\t\t\t\tauto kesu = s[l].substr(SZ(s[l]) - len2);\n\t\t\t\t\tif(amari != kesu) continue;\n\t\t\t\t\tREP(l2, n) {\n\t\t\t\t\t\tif(!adj[l][l2]) continue;\n\t\t\t\t\t\tint u = right_id(l2, r, len);\n\t\t\t\t\t\tint v = left_id(l, r, SZ(s[l]) - len2);\n\t\t\t\t\t\tg.add_edge(u, v, SZ(s[l]), 1);\n\t\t\t\t\t\tchmax(mp[{u, v}], SZ(s[l]));\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tstrongly_connected_components scc(g, 1);\n\tauto &h = scc.graph_compressed;\n\n\tN = SZ(h);\n\n\tvector dp(N, -inf);\n\tvector vis(N, false);\n\n\tREP(i, n) {\n\t\t//左を余らせる場合\n\t\tREP(len, SZ(s[i]) + 1) {\n\t\t\tstring lef = s[i].substr(len);\n\t\t\tstring rig = lef;\n\t\t\treverse(all(rig));\n\t\t\tif(lef == rig) {\n\t\t\t\tdp[scc[left_id(i, i, len)]] = SZ(s[i]);\n\t\t\t\tvis[scc[left_id(i, i, len)]] = 1;\n\t\t\t}\n\t\t}\n\n\t\t//右を余らせる場合\n\t\tREP(len, SZ(s[i]) + 1) {\n\t\t\tstring lef = s[i].substr(0, SZ(s[i]) - len);\n\t\t\tstring rig = lef;\n\t\t\treverse(all(rig));\n\t\t\tif(lef == rig) {\n\t\t\t\tdp[scc[right_id(i, i, len)]] = SZ(s[i]);\n\t\t\t\tvis[scc[right_id(i, i, len)]] = 1;\n\t\t\t}\n\t\t}\n\t}\n\n\tauto &groups = scc.groups;\n\t//debug(groups);\n\n\tREP(i, N) {\n\t\tif(SZ(groups[i]) != 1) dp[i] = inf;\n\t}\n\n\tdebug(dp);\n\n\tauto tsort = h.topological_sort();\n\n\tfoa(idx, tsort) {\n\t\tif(!vis[idx]) continue;\n\t\tfor(auto [to, cost] : h[idx]) {\n\t\t\tchmax(dp[to], dp[idx] + mp[{groups[idx][0], groups[to][0]}]);\n\t\t\tvis[to] = 1;\n\t\t}\n\t}\n\n\tint res = 0;\n\tREP(i, n) {\n\t\tREP(j, n) {\n\t\t\tint lid = scc[left_id(i, j, 0)];\n\t\t\tif(vis[lid]) {\n\t\t\t\tchmax(res, dp[lid]);\n\t\t\t}\n\t\t\tint rid = scc[right_id(i, j, 0)];\n\t\t\tif(vis[rid]) {\n\t\t\t\tchmax(res, dp[rid]);\n\t\t\t}\n\t\t}\n\t}\n\n\tprint(res >= inf ? -1 : res);\n}", "accuracy": 0.5816326530612245, "time_ms": 100, "memory_kb": 64072, "score_of_the_acc": -0.6299, "final_rank": 16 }, { "submission_id": "aoj_2307_3731719", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 99999999999999999\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n#define EMPTY 0 //空文字のインデックス\n#define NUM 105\n#define BALANCE 10\n\n/*\n * diff = 右の余り-左の余り+10\n * バランスしているとき、diffは10\n *\n * */\n\nstruct Data{\n\tData(int arg_next_id,int arg_add_len){\n\t\tnext_node_id = arg_next_id;\n\t\tadd_len = arg_add_len;\n\t}\n\tint next_node_id,add_len;\n};\n\nstruct Node{\n\n\tint left_id,right_id,diff;\n\tvector<Data> NEXT;\n};\n\nstruct Info{\n\n\tchar word[11];\n\tint length;\n};\n\nstruct State{\n\tState(int arg_node_id,int arg_sum_value){\n\t\tnode_id = arg_node_id;\n\t\tsum_value = arg_sum_value;\n\t}\n\tint node_id,sum_value;\n};\n\nint V,E;\nconst int start = 0,goal = 1;\nbool check[NUM][NUM];\nInfo info[NUM];\nvector<Node> NODES;\nmap<pair<int,int>,int> MAP[21]; //map[diff][make_pair(left_id,right_id)] = node_id\nmap<pair<int,int>,int> dp[21]; //dp[diff][make_pair(left_id,right_id)] = 最大長\n\n#define SIZE 200005\n\nvector<int> G[SIZE];\nvector<int> reverse_G[SIZE];\nbool can_goal[SIZE];\nbool cycle_FLG;\nbool visited[SIZE];\n\nvoid cycle_dfs(int start_id,int current_id){\n\n\tif(cycle_FLG)return;\n\n\tfor(int i = 0; i < G[current_id].size(); i++){\n\t\tif(G[current_id][i] == start_id){\n\n\t\t\tcycle_FLG = true;\n\t\t\treturn;\n\t\t}\n\t\tif(visited[G[current_id][i]])continue;\n\t\tvisited[G[current_id][i]] = true;\n\n\t\tcycle_dfs(start_id,G[current_id][i]);\n\t}\n}\n\nvoid goal_dfs(int node_id){\n\n\tcan_goal[node_id] = true;\n\n\tfor(int i = 0; i < reverse_G[node_id].size(); i++){\n\n\t\tif(visited[reverse_G[node_id][i]])continue;\n\t\tvisited[reverse_G[node_id][i]] = true;\n\n\t\tgoal_dfs(reverse_G[node_id][i]);\n\t}\n}\n\nvoid start_dfs(int node_id){\n\n\tfor(int i = 0; i < G[node_id].size(); i++){\n\n\t\tif(visited[G[node_id][i]])continue;\n\t\tvisited[G[node_id][i]] = true;\n\n\t\tstart_dfs(G[node_id][i]);\n\t}\n}\n\n\n\nint main(){\n\n\tscanf(\"%d %d\",&V,&E);\n\n\tfor(int i = 0; i <= V; i++){\n\t\tfor(int k = 0; k <= V; k++){\n\t\t\tcheck[i][k] = false;\n\t\t}\n\t}\n\n\tint length;\n\n\t//空文字\n\tinfo[0].word[0] = '\\0';\n\tinfo[0].length = 0;\n\n\tfor(int i = 1; i <= V; i++){\n\n\t\tscanf(\"%s\",info[i].word);\n\n\t\tfor(length = 0; info[i].word[length] != '\\0'; length++);\n\n\t\tinfo[i].length = length;\n\t}\n\n\tint from,to;\n\n\tfor(int loop = 0; loop < E; loop++){\n\n\t\tscanf(\"%d %d\",&from,&to);\n\n\t\tcheck[from][to] = true;\n\t}\n\n\tint num_nodes = 2;\n\tNode start_node,goal_node;\n\n\tstart_node.diff = BALANCE;\n\tstart_node.left_id = 0;\n\tstart_node.right_id = 0;\n\n\tgoal_node.diff = BALANCE;\n\tgoal_node.left_id = 0;\n\tgoal_node.right_id = 0;\n\n\tNODES.push_back(start_node);\n\tNODES.push_back(goal_node);\n\n\n\t//まず、スタートから遷移する最初のノードを作る\n\n\t/*★1つの単語★*/\n\n\t//全体が回文になっているものを探す。接続に制約があるので、回文になっている単語は全てノード化する\n\n\tfor(int i = 1; i <= V; i++){\n\n\t\tbool FLG = true;\n\t\tfor(int k = 0; k < info[i].length/2; k++){\n\t\t\tif(info[i].word[k] != info[i].word[info[i].length-1-k]){\n\t\t\t\tFLG = false;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\n\t\tif(FLG){\n\n\t\t\t//ノード作成\n\t\t\tNode node;\n\n\t\t\tnode.left_id = i;\n\t\t\tnode.right_id = i;\n\t\t\tnode.diff = BALANCE;\n\n\t\t\tNODES.push_back(node);\n\n\t\t\tMAP[BALANCE][make_pair(i,i)] = num_nodes; //マップにIDを登録\n\n\t\t\t//スタートからエッジを張る\n\t\t\tNODES[start].NEXT.push_back(Data(num_nodes,info[i].length));\n\t\t\tG[start].push_back(num_nodes);\n\t\t\treverse_G[num_nodes].push_back(start);\n\n\t\t\t//バランスしたので、ゴールにエッジを張る\n\t\t\tNODES[num_nodes].NEXT.push_back(Data(goal,0));\n\t\t\tG[num_nodes].push_back(goal);\n\t\t\treverse_G[goal].push_back(num_nodes);\n\n\t\t\tnum_nodes++;\n\t\t}\n\t}\n\n\n\t//左端固定\n\tfor(int i = 1; i <= V; i++){\n\n\t\tfor(int right = 0; right <= info[i].length-2; right++){\n\t\t\tbool FLG = true;\n\n\t\t\t//0~rightが回文になっているか\n\t\t\tint tmp_len = right+1;\n\n\t\t\tfor(int k = 0; k < tmp_len/2; k++){\n\t\t\t\tif(info[i].word[k] != info[i].word[tmp_len-1-k]){\n\t\t\t\t\tFLG = false;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif(FLG){\n\n\t\t\t\t//ノード作成\n\t\t\t\tNode node;\n\n\t\t\t\tnode.left_id = i;\n\t\t\t\tnode.right_id = i;\n\t\t\t\tnode.diff = (info[i].length-(right+1))+BALANCE;\n\n\t\t\t\tNODES.push_back(node);\n\n\t\t\t\tMAP[node.diff][make_pair(node.left_id,node.right_id)] = num_nodes; //マップにIDを登録\n\n\t\t\t\t//スタートからエッジを張る\n\t\t\t\tNODES[start].NEXT.push_back(Data(num_nodes,info[i].length));\n\t\t\t\tG[start].push_back(num_nodes);\n\t\t\t\treverse_G[num_nodes].push_back(start);\n\n\t\t\t\tnum_nodes++;\n\t\t\t}\n\t\t}\n\t}\n\n\t//右端固定\n\tfor(int i = 1; i <= V; i++){\n\n\t\tfor(int left = 1; left <= info[i].length-1; left++){\n\t\t\tbool FLG = true;\n\n\t\t\t//left~info[i].length-1が回文になっているか\n\t\t\tint tmp_len = info[i].length-left;\n\n\t\t\tfor(int k = 0; k < tmp_len/2; k++){\n\t\t\t\tif(info[i].word[left+k] != info[i].word[info[i].length-1-k]){\n\t\t\t\t\tFLG = false;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif(FLG){\n\n\t\t\t\t//ノード作成\n\t\t\t\tNode node;\n\n\t\t\t\tnode.left_id = i;\n\t\t\t\tnode.right_id = i;\n\t\t\t\tnode.diff = -left+BALANCE;\n\n\t\t\t\tNODES.push_back(node);\n\n\t\t\t\tMAP[node.diff][make_pair(node.left_id,node.right_id)] = num_nodes; //マップにIDを登録\n\n\t\t\t\t//スタートからエッジを張る\n\t\t\t\tNODES[start].NEXT.push_back(Data(num_nodes,info[i].length));\n\t\t\t\tG[start].push_back(num_nodes);\n\t\t\t\treverse_G[num_nodes].push_back(start);\n\n\t\t\t\tnum_nodes++;\n\t\t\t}\n\t\t}\n\t}\n\n\t/*★2つの単語★*/\n\tfor(int L = 1; L <= V; L++){\n\t\tfor(int R = 1; R <= V; R++){\n\t\t\tif(!check[L][R])continue;\n\n\t\t\tbool FLG = true;\n\t\t\tint tmp_len = min(info[L].length,info[R].length);\n\n\t\t\tfor(int k = 0; k < tmp_len; k++){\n\t\t\t\tif(info[L].word[info[L].length-1-k] != info[R].word[k]){\n\t\t\t\t\tFLG = false;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif(!FLG)continue;\n\n\t\t\t//ノード作成\n\t\t\tNode node;\n\n\t\t\tnode.left_id = L;\n\t\t\tnode.right_id = R;\n\t\t\tnode.diff = (info[R].length-info[L].length)+BALANCE;\n\n\t\t\tauto at = MAP[node.diff].find(make_pair(node.left_id,node.right_id));\n\n\t\t\tif(at != MAP[node.diff].end()){ //★注意★\n\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tNODES.push_back(node);\n\n\t\t\tMAP[node.diff][make_pair(L,R)] = num_nodes; //マップにIDを登録\n\n\t\t\t//スタートからエッジを張る\n\t\t\tNODES[start].NEXT.push_back(Data(num_nodes,info[L].length+info[R].length));\n\t\t\tG[start].push_back(num_nodes);\n\t\t\treverse_G[num_nodes].push_back(start);\n\n\n\t\t\tif(node.diff == BALANCE){\n\n\t\t\t\t//バランスしたので、ゴールにエッジを張る\n\t\t\t\tNODES[num_nodes].NEXT.push_back(Data(goal,0));\n\t\t\t\tG[num_nodes].push_back(goal);\n\t\t\t\treverse_G[goal].push_back(num_nodes);\n\t\t\t}\n\n\t\t\tnum_nodes++;\n\t\t}\n\t}\n\n\t//発生し得る状態を全列挙\n\tqueue<int> Q;\n\n\tfor(int i = 0; i < NODES[start].NEXT.size(); i++){\n\n\t\tQ.push(NODES[start].NEXT[i].next_node_id);\n\t}\n\n\twhile(!Q.empty()){\n\n\t\tint node_id = Q.front();\n\t\tQ.pop();\n\n\t\tif(NODES[node_id].diff <= BALANCE){ //右に文字追加\n\n\t\t\tfor(int i = 1; i <= V; i++){\n\n\t\t\t\tif(!check[NODES[node_id].right_id][i])continue;\n\n\t\t\t\t//少なくとも重なる部分が全て一致するか調べる\n\t\t\t\tbool FLG = true;\n\t\t\t\tint left_diff = abs(NODES[node_id].diff-BALANCE);\n\t\t\t\tint tmp_len = min(left_diff,info[i].length);\n\t\t\t\tint left_id = NODES[node_id].left_id;\n\n\t\t\t\tfor(int k = 0; k < tmp_len; k++){\n\n\t\t\t\t\tif(info[left_id].word[left_diff-1-k] != info[i].word[k]){\n\t\t\t\t\t\tFLG = false;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tif(!FLG)continue;\n\n\t\t\t\tNode node;\n\t\t\t\tnode.diff = info[i].length-left_diff+BALANCE;\n\t\t\t\tnode.left_id = NODES[node_id].left_id;\n\t\t\t\tnode.right_id = i;\n\n\t\t\t\tauto at = MAP[node.diff].find(make_pair(node.left_id,node.right_id));\n\n\t\t\t\tif(at != MAP[node.diff].end()){ //出現済:エッジだけ張る\n\n\t\t\t\t\tint next_node = MAP[node.diff][make_pair(node.left_id,node.right_id)];\n\n\t\t\t\t\tNODES[node_id].NEXT.push_back(Data(next_node,info[i].length));\n\t\t\t\t\tG[node_id].push_back(next_node);\n\t\t\t\t\treverse_G[next_node].push_back(node_id);\n\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\n\t\t\t\tNODES.push_back(node); //初登場\n\n\t\t\t\tMAP[node.diff][make_pair(node.left_id,node.right_id)] = num_nodes;\n\n\t\t\t\t//node_idからエッジを張る\n\t\t\t\tNODES[node_id].NEXT.push_back(Data(num_nodes,info[i].length));\n\t\t\t\tG[node_id].push_back(num_nodes);\n\t\t\t\treverse_G[num_nodes].push_back(node_id);\n\n\n\t\t\t\tif(node.diff == BALANCE){ //バランスしていれば、ゴールにエッジを張る\n\n\t\t\t\t\tNODES[num_nodes].NEXT.push_back(Data(goal,0));\n\t\t\t\t\tG[num_nodes].push_back(goal);\n\t\t\t\t\treverse_G[goal].push_back(num_nodes);\n\n\t\t\t\t}\n\n\t\t\t\tQ.push(num_nodes);\n\n\t\t\t\tnum_nodes++;\n\t\t\t}\n\n\t\t}\n\n\t\tif(NODES[node_id].diff >= BALANCE){ //左に文字追加\n\n\t\t\tfor(int i = 1; i <= V; i++){\n\n\t\t\t\tif(!check[i][NODES[node_id].left_id])continue;\n\n\t\t\t\t//少なくとも重なる部分が全て一致するか調べる\n\t\t\t\tbool FLG = true;\n\t\t\t\tint right_diff = NODES[node_id].diff-BALANCE;\n\t\t\t\tint tmp_len = min(right_diff,info[i].length);\n\t\t\t\tint right_id = NODES[node_id].right_id;\n\n\t\t\t\tfor(int k = 0; k < tmp_len; k++){\n\n\t\t\t\t\tif(info[right_id].word[info[right_id].length-right_diff+k] != info[i].word[info[i].length-1-k]){\n\t\t\t\t\t\tFLG = false;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tif(!FLG)continue;\n\n\t\t\t\tNode node;\n\t\t\t\tnode.diff = right_diff-info[i].length+BALANCE;\n\t\t\t\tnode.left_id = i;\n\t\t\t\tnode.right_id = NODES[node_id].right_id;\n\n\t\t\t\tauto at = MAP[node.diff].find(make_pair(node.left_id,node.right_id));\n\n\t\t\t\tif(at != MAP[node.diff].end()){ //出現済:エッジだけ張る\n\n\t\t\t\t\tint next_node = MAP[node.diff][make_pair(node.left_id,node.right_id)];\n\n\t\t\t\t\tNODES[node_id].NEXT.push_back(Data(next_node,info[i].length));\n\t\t\t\t\tG[node_id].push_back(next_node);\n\t\t\t\t\treverse_G[next_node].push_back(node_id);\n\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\n\t\t\t\tNODES.push_back(node); //初出現なのでノード追加\n\t\t\t\tMAP[node.diff][make_pair(node.left_id,node.right_id)] = num_nodes;\n\n\t\t\t\t//node_idからエッジを張る\n\t\t\t\tNODES[node_id].NEXT.push_back(Data(num_nodes,info[i].length));\n\t\t\t\tG[node_id].push_back(num_nodes);\n\t\t\t\treverse_G[num_nodes].push_back(node_id);\n\n\t\t\t\tif(node.diff == BALANCE){ //バランスしていれば、ゴールにエッジを張る\n\n\t\t\t\t\tNODES[num_nodes].NEXT.push_back(Data(goal,0));\n\t\t\t\t\tG[num_nodes].push_back(goal);\n\t\t\t\t\treverse_G[goal].push_back(num_nodes);\n\t\t\t\t}\n\n\t\t\t\tQ.push(num_nodes);\n\n\t\t\t\tnum_nodes++;\n\t\t\t}\n\t\t}\n\t}\n\n\t//ゴールできるか調べる\n\tfor(int i = 0; i < num_nodes; i++){\n\n\t\tvisited[i] = false;\n\t}\n\tvisited[start] = true;\n\tstart_dfs(start);\n\n\tif(!visited[goal]){\n\n\t\tprintf(\"0\\n\");\n\t\treturn 0;\n\t}\n\n\t//ゴールできるノードを探す\n\tfor(int i = 0; i < num_nodes; i++){\n\n\t\tcan_goal[i] = false;\n\t\tvisited[i] = false;\n\t}\n\tvisited[goal] = true;\n\tgoal_dfs(goal);\n\n\n\t//ゴールできるものの中で、閉路に属するノードがないか調べる\n\tfor(int i = 0; i < num_nodes; i++){\n\t\tif(can_goal[i] == false)continue;\n\n\t\tfor(int k = 0; k < num_nodes; k++){\n\n\t\t\tvisited[k] = false;\n\t\t}\n\t\tcycle_FLG = false;\n\n\t\tvisited[i] = true;\n\t\tcycle_dfs(i,i);\n\n\t\tif(cycle_FLG){\n\n\t\t\tprintf(\"-1\\n\");\n\t\t\treturn 0;\n\t\t}\n\t}\n\n\t//dp計算する\n\tint ans = 0;\n\n\t//DAGになってるはずなのでdp計算\n\tqueue<State> final_Q;\n\n\tdp[NODES[start].diff][make_pair(NODES[start].left_id,NODES[start].right_id)] = 0;\n\tfinal_Q.push(State(start,0));\n\n\twhile(!final_Q.empty()){\n\n\t\tState state = final_Q.front();\n\t\tfinal_Q.pop();\n\n\t\tauto at = dp[NODES[state.node_id].diff].find(make_pair(NODES[state.node_id].left_id,NODES[state.node_id].right_id));\n\n\t\tif(at != dp[NODES[state.node_id].diff].end() && state.sum_value < dp[NODES[state.node_id].diff][make_pair(NODES[state.node_id].left_id,NODES[state.node_id].right_id)]){\n\n\t\t\tcontinue;\n\t\t}\n\n\t\tif(state.node_id == goal){\n\n\t\t\tans = max(ans,state.sum_value);\n\t\t}\n\n\t\tfor(int i = 0; i < NODES[state.node_id].NEXT.size(); i++){\n\n\t\t\tint next_node = NODES[state.node_id].NEXT[i].next_node_id;\n\t\t\tif(!can_goal[next_node])continue;\n\n\t\t\tint next_value = state.sum_value+NODES[state.node_id].NEXT[i].add_len;\n\n\t\t\tat = dp[NODES[next_node].diff].find(make_pair(NODES[next_node].left_id,NODES[next_node].right_id));\n\n\t\t\tif(at == dp[NODES[next_node].diff].end()){\n\n\t\t\t\tdp[NODES[next_node].diff][make_pair(NODES[next_node].left_id,NODES[next_node].right_id)] = next_value;\n\n\t\t\t\tfinal_Q.push(State(next_node,next_value));\n\n\t\t\t}else{\n\n\t\t\t\tif(dp[NODES[next_node].diff][make_pair(NODES[next_node].left_id,NODES[next_node].right_id)] < next_value){\n\n\t\t\t\t\tdp[NODES[next_node].diff][make_pair(NODES[next_node].left_id,NODES[next_node].right_id)] = next_value;\n\t\t\t\t\tfinal_Q.push(State(next_node,next_value));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tprintf(\"%d\\n\",ans);\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 17068, "score_of_the_acc": -0.1082, "final_rank": 2 }, { "submission_id": "aoj_2307_3731718", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 99999999999999999\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n#define EMPTY 0 //空文字のインデックス\n#define NUM 105\n#define BALANCE 10\n\n/*\n * diff = 右の余り-左の余り+10\n * バランスしているとき、diffは10\n *\n * */\n\nstruct Data{\n\tData(int arg_next_id,int arg_add_len){\n\t\tnext_node_id = arg_next_id;\n\t\tadd_len = arg_add_len;\n\t}\n\tint next_node_id,add_len;\n};\n\nstruct Node{\n\n\tint left_id,right_id,diff;\n\tvector<Data> NEXT;\n};\n\nstruct Info{\n\n\tchar word[11];\n\tint length;\n};\n\nstruct State{\n\tState(int arg_node_id,int arg_sum_value){\n\t\tnode_id = arg_node_id;\n\t\tsum_value = arg_sum_value;\n\t}\n\tint node_id,sum_value;\n};\n\nint V,E;\nconst int start = 0,goal = 1;\nbool check[NUM][NUM];\nInfo info[NUM];\nvector<Node> NODES;\nmap<pair<int,int>,int> MAP[21]; //map[diff][make_pair(left_id,right_id)] = node_id\nmap<pair<int,int>,int> dp[21]; //dp[diff][make_pair(left_id,right_id)] = 最大長\n\n#define SIZE 200005\n\nvector<int> G[SIZE];\nvector<int> reverse_G[SIZE];\nbool can_goal[SIZE];\nbool cycle_FLG;\nbool visited[SIZE];\n\nvoid cycle_dfs(int start_id,int current_id){\n\n\tif(cycle_FLG)return;\n\n\tfor(int i = 0; i < G[current_id].size(); i++){\n\t\tif(G[current_id][i] == start_id){\n\n\t\t\tcycle_FLG = true;\n\t\t\treturn;\n\t\t}\n\t\tif(visited[G[current_id][i]])continue;\n\t\t/*if(visited[G[current_id][i]]){\n\t\t\tif(can_goal[G[current_id][i]]){\n\t\t\tcycle_FLG = true;\n\t\t\treturn;\n\t\t\t}\n\t\t\tcontinue;\n\t\t}*/\n\t\tvisited[G[current_id][i]] = true;\n\n\t\tcycle_dfs(start_id,G[current_id][i]);\n\t}\n}\n\nvoid goal_dfs(int node_id){\n\n\tcan_goal[node_id] = true;\n\n\tfor(int i = 0; i < reverse_G[node_id].size(); i++){\n\n\t\tif(visited[reverse_G[node_id][i]])continue;\n\t\tvisited[reverse_G[node_id][i]] = true;\n\n\t\tgoal_dfs(reverse_G[node_id][i]);\n\t}\n}\n\nvoid start_dfs(int node_id){\n\n\tfor(int i = 0; i < G[node_id].size(); i++){\n\n\t\tif(visited[G[node_id][i]])continue;\n\t\tvisited[G[node_id][i]] = true;\n\n\t\tstart_dfs(G[node_id][i]);\n\t}\n}\n\n\n\nint main(){\n\n\tscanf(\"%d %d\",&V,&E);\n\n\tfor(int i = 0; i <= V; i++){\n\t\tfor(int k = 0; k <= V; k++){\n\t\t\tcheck[i][k] = false;\n\t\t}\n\t}\n\n\tint length;\n\n\t//空文字\n\tinfo[0].word[0] = '@';\n\tinfo[0].word[1] = '\\0';\n\tinfo[0].length = 0;\n\n\tfor(int i = 1; i <= V; i++){\n\n\t\tscanf(\"%s\",info[i].word);\n\n\t\tfor(length = 0; info[i].word[length] != '\\0'; length++);\n\n\t\tinfo[i].length = length;\n\t}\n\n\tint from,to;\n\n\tfor(int loop = 0; loop < E; loop++){\n\n\t\tscanf(\"%d %d\",&from,&to);\n\n\t\tcheck[from][to] = true;\n\t}\n\n\tint num_nodes = 2;\n\tNode start_node,goal_node;\n\n\tstart_node.diff = BALANCE;\n\tstart_node.left_id = 0;\n\tstart_node.right_id = 0;\n\n\tgoal_node.diff = BALANCE;\n\tgoal_node.left_id = 0;\n\tgoal_node.right_id = 0;\n\n\tNODES.push_back(start_node);\n\tNODES.push_back(goal_node);\n\n\n\t//まず、スタートから遷移する最初のノードを作る\n\n\t/*★1つの単語★*/\n\n\t//全体が回文になっているものを探す。接続に制約があるので、回文になっている単語は全てノード化する\n\n\tfor(int i = 1; i <= V; i++){\n\n\t\tbool FLG = true;\n\t\tfor(int k = 0; k < info[i].length/2; k++){\n\t\t\tif(info[i].word[k] != info[i].word[info[i].length-1-k]){\n\t\t\t\tFLG = false;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\n\t\tif(FLG){\n\n\t\t\t//ノード作成\n\t\t\tNode node;\n\n\t\t\tnode.left_id = i;\n\t\t\tnode.right_id = i;\n\t\t\tnode.diff = BALANCE;\n\n\t\t\tNODES.push_back(node);\n\n\t\t\tMAP[BALANCE][make_pair(i,i)] = num_nodes; //マップにIDを登録\n\n\t\t\t//スタートからエッジを張る\n\t\t\tNODES[start].NEXT.push_back(Data(num_nodes,info[i].length));\n\t\t\tG[start].push_back(num_nodes);\n\t\t\treverse_G[num_nodes].push_back(start);\n\n\t\t\t//バランスしたので、ゴールにエッジを張る\n\t\t\tNODES[num_nodes].NEXT.push_back(Data(goal,0));\n\t\t\tG[num_nodes].push_back(goal);\n\t\t\treverse_G[goal].push_back(num_nodes);\n\n\t\t\tnum_nodes++;\n\t\t}\n\t}\n\n\n\t//左端固定\n\tfor(int i = 1; i <= V; i++){\n\n\t\tfor(int right = 0; right <= info[i].length-2; right++){\n\t\t\tbool FLG = true;\n\n\t\t\t//0~rightが回文になっているか\n\t\t\tint tmp_len = right+1;\n\n\t\t\tfor(int k = 0; k < tmp_len/2; k++){\n\t\t\t\tif(info[i].word[k] != info[i].word[tmp_len-1-k]){\n\t\t\t\t\tFLG = false;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif(FLG){\n\n\t\t\t\t//ノード作成\n\t\t\t\tNode node;\n\n\t\t\t\tnode.left_id = i;\n\t\t\t\tnode.right_id = i;\n\t\t\t\tnode.diff = (info[i].length-(right+1))+BALANCE;\n\n\t\t\t\tNODES.push_back(node);\n\n\t\t\t\tMAP[node.diff][make_pair(node.left_id,node.right_id)] = num_nodes; //マップにIDを登録\n\n\t\t\t\t//スタートからエッジを張る\n\t\t\t\tNODES[start].NEXT.push_back(Data(num_nodes,info[i].length));\n\t\t\t\tG[start].push_back(num_nodes);\n\t\t\t\treverse_G[num_nodes].push_back(start);\n\n\t\t\t\tnum_nodes++;\n\t\t\t}\n\t\t}\n\t}\n\n\t//右端固定\n\tfor(int i = 1; i <= V; i++){\n\n\t\tfor(int left = 1; left <= info[i].length-1; left++){\n\t\t\tbool FLG = true;\n\n\t\t\t//left~info[i].length-1が回文になっているか\n\t\t\tint tmp_len = info[i].length-left;\n\n\t\t\tfor(int k = 0; k < tmp_len/2; k++){\n\t\t\t\tif(info[i].word[left+k] != info[i].word[info[i].length-1-k]){\n\t\t\t\t\tFLG = false;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif(FLG){\n\n\t\t\t\t//ノード作成\n\t\t\t\tNode node;\n\n\t\t\t\tnode.left_id = i;\n\t\t\t\tnode.right_id = i;\n\t\t\t\tnode.diff = -left+BALANCE;\n\n\t\t\t\tNODES.push_back(node);\n\n\t\t\t\tMAP[node.diff][make_pair(node.left_id,node.right_id)] = num_nodes; //マップにIDを登録\n\n\t\t\t\t//スタートからエッジを張る\n\t\t\t\tNODES[start].NEXT.push_back(Data(num_nodes,info[i].length));\n\t\t\t\tG[start].push_back(num_nodes);\n\t\t\t\treverse_G[num_nodes].push_back(start);\n\n\t\t\t\tnum_nodes++;\n\t\t\t}\n\t\t}\n\t}\n\n\t/*★2つの単語★*/\n\tfor(int L = 1; L <= V; L++){\n\t\tfor(int R = 1; R <= V; R++){\n\t\t\tif(!check[L][R])continue;\n\n\t\t\tbool FLG = true;\n\t\t\tint tmp_len = min(info[L].length,info[R].length);\n\n\t\t\tfor(int k = 0; k < tmp_len; k++){\n\t\t\t\tif(info[L].word[info[L].length-1-k] != info[R].word[k]){\n\t\t\t\t\tFLG = false;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif(!FLG)continue;\n\n\t\t\t//ノード作成\n\t\t\tNode node;\n\n\t\t\tnode.left_id = L;\n\t\t\tnode.right_id = R;\n\t\t\tnode.diff = (info[R].length-info[L].length)+BALANCE;\n\n\t\t\tauto at = MAP[node.diff].find(make_pair(node.left_id,node.right_id));\n\n\t\t\tif(at != MAP[node.diff].end()){ //★注意★\n\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tNODES.push_back(node);\n\n\t\t\tMAP[node.diff][make_pair(L,R)] = num_nodes; //マップにIDを登録\n\n\t\t\t//スタートからエッジを張る\n\t\t\tNODES[start].NEXT.push_back(Data(num_nodes,info[L].length+info[R].length));\n\t\t\tG[start].push_back(num_nodes);\n\t\t\treverse_G[num_nodes].push_back(start);\n\n\n\t\t\tif(node.diff == BALANCE){\n\n\t\t\t\t//バランスしたので、ゴールにエッジを張る\n\t\t\t\tNODES[num_nodes].NEXT.push_back(Data(goal,0));\n\t\t\t\tG[num_nodes].push_back(goal);\n\t\t\t\treverse_G[goal].push_back(num_nodes);\n\t\t\t}\n\n\t\t\tnum_nodes++;\n\t\t}\n\t}\n\n\t//発生し得る状態を全列挙\n\tqueue<int> Q;\n\n\tfor(int i = 0; i < NODES[start].NEXT.size(); i++){\n\n\t\tQ.push(NODES[start].NEXT[i].next_node_id);\n\t}\n\n\n\n\t/*printf(\"Minhi!\\n\");\n\treturn 0;\n*/\n\twhile(!Q.empty()){\n\n\t\tint node_id = Q.front();\n\t\tQ.pop();\n\n\t\tif(NODES[node_id].diff <= BALANCE){ //右に文字追加\n\n\t\t\tfor(int i = 1; i <= V; i++){\n\n\t\t\t\tif(!check[NODES[node_id].right_id][i])continue;\n\n\t\t\t\t//少なくとも重なる部分が全て一致するか調べる\n\t\t\t\tbool FLG = true;\n\t\t\t\tint left_diff = abs(NODES[node_id].diff-BALANCE);\n\t\t\t\tint tmp_len = min(left_diff,info[i].length);\n\t\t\t\tint left_id = NODES[node_id].left_id;\n\n\t\t\t\tfor(int k = 0; k < tmp_len; k++){\n\n\t\t\t\t\tif(info[left_id].word[left_diff-1-k] != info[i].word[k]){\n\t\t\t\t\t\tFLG = false;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tif(!FLG)continue;\n\n\t\t\t\tNode node;\n\t\t\t\tnode.diff = info[i].length-left_diff+BALANCE;\n\t\t\t\tnode.left_id = NODES[node_id].left_id;\n\t\t\t\tnode.right_id = i;\n\n\t\t\t\tauto at = MAP[node.diff].find(make_pair(node.left_id,node.right_id));\n\n\t\t\t\tif(at != MAP[node.diff].end()){ //出現済:エッジだけ張る\n\n\t\t\t\t\tint next_node = MAP[node.diff][make_pair(node.left_id,node.right_id)];\n\n\t\t\t\t\tNODES[node_id].NEXT.push_back(Data(next_node,info[i].length));\n\t\t\t\t\tG[node_id].push_back(next_node);\n\t\t\t\t\treverse_G[next_node].push_back(node_id);\n\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\n\t\t\t\tNODES.push_back(node); //初登場\n\n\t\t\t\tMAP[node.diff][make_pair(node.left_id,node.right_id)] = num_nodes;\n\n\t\t\t\t//node_idからエッジを張る\n\t\t\t\tNODES[node_id].NEXT.push_back(Data(num_nodes,info[i].length));\n\t\t\t\tG[node_id].push_back(num_nodes);\n\t\t\t\treverse_G[num_nodes].push_back(node_id);\n\n\n\t\t\t\tif(node.diff == BALANCE){ //バランスしていれば、ゴールにエッジを張る\n\n\t\t\t\t\tNODES[num_nodes].NEXT.push_back(Data(goal,0));\n\t\t\t\t\tG[num_nodes].push_back(goal);\n\t\t\t\t\treverse_G[goal].push_back(num_nodes);\n\n\t\t\t\t}\n\n\t\t\t\tQ.push(num_nodes);\n\n\t\t\t\tnum_nodes++;\n\t\t\t}\n\n\t\t}\n\n\t\tif(NODES[node_id].diff >= BALANCE){ //左に文字追加\n\n\t\t\tfor(int i = 1; i <= V; i++){\n\n\t\t\t\tif(!check[i][NODES[node_id].left_id])continue;\n\n\t\t\t\t//少なくとも重なる部分が全て一致するか調べる\n\t\t\t\tbool FLG = true;\n\t\t\t\tint right_diff = NODES[node_id].diff-BALANCE;\n\t\t\t\tint tmp_len = min(right_diff,info[i].length);\n\t\t\t\tint right_id = NODES[node_id].right_id;\n\n\t\t\t\tfor(int k = 0; k < tmp_len; k++){\n\n\t\t\t\t\tif(info[right_id].word[info[right_id].length-right_diff+k] != info[i].word[info[i].length-1-k]){\n\t\t\t\t\t\tFLG = false;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tif(!FLG)continue;\n\n\t\t\t\tNode node;\n\t\t\t\tnode.diff = right_diff-info[i].length+BALANCE;\n\t\t\t\tnode.left_id = i;\n\t\t\t\tnode.right_id = NODES[node_id].right_id;\n\n\t\t\t\tauto at = MAP[node.diff].find(make_pair(node.left_id,node.right_id));\n\n\t\t\t\tif(at != MAP[node.diff].end()){ //出現済:エッジだけ張る\n\n\t\t\t\t\tint next_node = MAP[node.diff][make_pair(node.left_id,node.right_id)];\n\n\t\t\t\t\tNODES[node_id].NEXT.push_back(Data(next_node,info[i].length));\n\t\t\t\t\tG[node_id].push_back(next_node);\n\t\t\t\t\treverse_G[next_node].push_back(node_id);\n\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\n\t\t\t\tNODES.push_back(node); //初出現なのでノード追加\n\t\t\t\tMAP[node.diff][make_pair(node.left_id,node.right_id)] = num_nodes;\n\n\t\t\t\t//node_idからエッジを張る\n\t\t\t\tNODES[node_id].NEXT.push_back(Data(num_nodes,info[i].length));\n\t\t\t\tG[node_id].push_back(num_nodes);\n\t\t\t\treverse_G[num_nodes].push_back(node_id);\n\n\t\t\t\tif(node.diff == BALANCE){ //バランスしていれば、ゴールにエッジを張る\n\n\t\t\t\t\tNODES[num_nodes].NEXT.push_back(Data(goal,0));\n\t\t\t\t\tG[num_nodes].push_back(goal);\n\t\t\t\t\treverse_G[goal].push_back(num_nodes);\n\t\t\t\t}\n\n\t\t\t\tQ.push(num_nodes);\n\n\t\t\t\tnum_nodes++;\n\t\t\t}\n\t\t}\n\t}\n\n\t//ゴールできるか調べる\n\tfor(int i = 0; i < num_nodes; i++){\n\n\t\tvisited[i] = false;\n\t}\n\tvisited[start] = true;\n\tstart_dfs(start);\n\n\tif(!visited[goal]){\n\n\t\tprintf(\"0\\n\");\n\t\treturn 0;\n\t}\n\n\t//ゴールできるノードを探す\n\tfor(int i = 0; i < num_nodes; i++){\n\n\t\tcan_goal[i] = false;\n\t\tvisited[i] = false;\n\t}\n\tvisited[goal] = true;\n\tgoal_dfs(goal);\n\n\n\t//ゴールできるものの中で、閉路に属するノードがないか調べる\n\tfor(int i = 0; i < num_nodes; i++){\n\t\tif(can_goal[i] == false)continue;\n\n\t\tfor(int k = 0; k < num_nodes; k++){\n\n\t\t\tvisited[k] = false;\n\t\t}\n\t\tcycle_FLG = false;\n\n\t\tvisited[i] = true;\n\t\tcycle_dfs(i,i);\n\n\t\tif(cycle_FLG){\n\n\t\t\tprintf(\"-1\\n\");\n\t\t\treturn 0;\n\t\t}\n\t}\n\n\t//dp計算する\n\tint ans = 0;\n\n\t//DAGになってるはずなのでdp計算\n\tqueue<State> final_Q;\n\n\tdp[NODES[start].diff][make_pair(NODES[start].left_id,NODES[start].right_id)] = 0;\n\tfinal_Q.push(State(start,0));\n\n\twhile(!final_Q.empty()){\n\n\t\tState state = final_Q.front();\n\t\tfinal_Q.pop();\n\n\t\tauto at = dp[NODES[state.node_id].diff].find(make_pair(NODES[state.node_id].left_id,NODES[state.node_id].right_id));\n\n\t\tif(at != dp[NODES[state.node_id].diff].end() && state.sum_value < dp[NODES[state.node_id].diff][make_pair(NODES[state.node_id].left_id,NODES[state.node_id].right_id)]){\n\n\t\t\tcontinue;\n\t\t}\n\n\t\tif(state.node_id == goal){\n\n\t\t\tans = max(ans,state.sum_value);\n\t\t}\n\n\t\tfor(int i = 0; i < NODES[state.node_id].NEXT.size(); i++){\n\n\t\t\tint next_node = NODES[state.node_id].NEXT[i].next_node_id;\n\t\t\tif(!can_goal[next_node])continue;\n\n\t\t\tint next_value = state.sum_value+NODES[state.node_id].NEXT[i].add_len;\n\n\t\t\tat = dp[NODES[next_node].diff].find(make_pair(NODES[next_node].left_id,NODES[next_node].right_id));\n\n\t\t\tif(at == dp[NODES[next_node].diff].end()){\n\n\t\t\t\tdp[NODES[next_node].diff][make_pair(NODES[next_node].left_id,NODES[next_node].right_id)] = next_value;\n\n\t\t\t\tfinal_Q.push(State(next_node,next_value));\n\n\t\t\t}else{\n\n\t\t\t\tif(dp[NODES[next_node].diff][make_pair(NODES[next_node].left_id,NODES[next_node].right_id)] < next_value){\n\n\t\t\t\t\tdp[NODES[next_node].diff][make_pair(NODES[next_node].left_id,NODES[next_node].right_id)] = next_value;\n\t\t\t\t\tfinal_Q.push(State(next_node,next_value));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tprintf(\"%d\\n\",ans);\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 16968, "score_of_the_acc": -0.1071, "final_rank": 1 }, { "submission_id": "aoj_2307_3178278", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing Int = long long;\ntemplate<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}\ntemplate<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}\n\n//INSERT ABOVE HERE\nsigned main(){\n const int D = 11;\n \n int n,m;\n cin>>n>>m;\n vector<string> s(n);\n for(int i=0;i<n;i++) cin>>s[i];\n\n vector<vector<int> > E(n,vector<int>(n,0));\n for(int i=0;i<m;i++){\n int x,y;\n cin>>x>>y;\n x--;y--;\n E[x][y]=1;\n }\n\n vector<int> l(n);\n for(int i=0;i<n;i++) l[i]=s[i].size();\n\n\n using T = tuple<int, int, int>;\n auto idx=[&](int d,int x,int y){return (D+d)*n*n+x*n+y;};\n auto inv=[&](int v){return T(v/(n*n)-D,v%(n*n)/n,v%n);};\n int sz=idx(D,n-1,n-1)+1;\n\n queue<T> qt;\n vector<int> dp(sz,-1);\n \n // center inside s[x]\n for(int x=0;x<n;x++){\n // even\n for(int i=1;i<l[x];i++){\n // [0, i) + [i, l)\n int k=min(i,l[x]-i),ok=1;\n for(int j=0;j<k;j++)\n\tok&=s[x][i-(j+1)]==s[x][i+j];\n if(ok){\n\tint d=i-(l[x]-i);\n\tqt.emplace(d,x,x);\n\tchmax(dp[idx(d,x,x)],l[x]);\n }\n }\n // odd\n for(int i=0;i<l[x];i++){\n // [0, i) + [i, i+1) + [i+1, l)\n int k=min(i,l[x]-(i+1)),ok=1;\n for(int j=0;j<k;j++)\n\tok&=s[x][i-(j+1)]==s[x][i+1+j];\n if(ok){\n\tint d=i-(l[x]-(i+1));\n\tqt.emplace(i-(l[x]-(i+1)),x,x);\n\tchmax(dp[idx(d,x,x)],l[x]);\n }\n }\n }\n\n // s[x] |center| s[y] (obviously even)\n for(int x=0;x<n;x++){\n for(int y=0;y<n;y++){\n if(!E[x][y]) continue;\n int k=min(l[x],l[y]),ok=1;\n for(int j=0;j<k;j++)\n\tok&=s[x][l[x]-(j+1)]==s[y][j];\n if(ok){\n\tint d=l[x]-l[y];\n\tqt.emplace(d,x,y);\n\tchmax(dp[idx(d,x,y)],l[x]+l[y]);\n }\n } \n }\n\n using P = pair<int, int>;\n vector<vector<P> > G(sz),R(sz);\n \n for(int d=-D;d<=D;d++){\n for(int x=0;x<n;x++){\n for(int y=0;y<n;y++){\n\tint v=idx(d,x,y);\n\tfor(int i=0;i<n;i++){\n\t // s[i] s[x] s[y]\n\t if(d<=0&&E[i][x]){\n\t int k=min(-d,l[i]),ok=1;\n\t for(int j=0;j<k;j++)\n\t ok&=s[i][l[i]-(j+1)]==s[y][l[y]+d+j];\n\t if(ok){\t \n\t int nd=d+l[i];\n\t int u=idx(nd,i,y);\n\t G[v].emplace_back(u,l[i]);\n\t R[u].emplace_back(v,l[i]);\t \n\t }\n\t }\n\t // s[x] s[y] s[i]\n\t if(d>=0&&E[y][i]){\n\t int k=min(d,l[i]),ok=1;\n\t for(int j=0;j<k;j++)\n\t ok&=s[i][j]==s[x][d-(j+1)];\n\t if(ok){\n\t int nd=d-l[i];\n\t int u=idx(nd,x,i);\n\t G[v].emplace_back(u,l[i]);\n\t R[u].emplace_back(v,l[i]);\t \n\t }\n\t }\n\t}\n } \n }\n }\n\n // find nodes that can reached from starts\n while(!qt.empty()){\n int d,x,y;\n tie(d,x,y)=qt.front();qt.pop();\n int v=idx(d,x,y);\n for(auto e:G[v]){\n int u,c;\n tie(u,c)=e;\n if(~dp[u]) continue;\n chmax(dp[u],dp[v]+c);\n qt.emplace(inv(u)); \n } \n }\n\n vector<int> dp2(sz,-1),used(sz,0),visit(sz,0);\n queue<int> qu;\n \n function<int(int)> dfs=\n [&](int v){\n int &res=dp2[v];\n if(used[v]) return res;\n \n visit[v]=1;\n qu.emplace(v);\n res=dp[v];\n \n for(auto e:R[v]){\n\tint u,c;\n\ttie(u,c)=e;\n\tif(dp[u]<0) continue;\n\tif(visit[u]==1){\n\t cout<<-1<<endl;\n\t exit(0);\n\t}\n\tchmax(res,dfs(u)+c);\n }\n \n visit[v]=0;\n return res;\n };\n \n int ans=0;\n for(int x=0;x<n;x++){\n for(int y=0;y<n;y++){\n int v=idx(0,x,y);\n if(~dp[v]) chmax(ans,dfs(v));\n while(!qu.empty()){\n\tused[qu.front()]=1;\n\tvisit[qu.front()]=2;\n\tqu.pop();\n }\n }\n }\n\n cout<<ans<<endl;\n return 0;\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 28380, "score_of_the_acc": -0.2473, "final_rank": 4 }, { "submission_id": "aoj_2307_3178277", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing Int = long long;\ntemplate<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}\ntemplate<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}\n\n//INSERT ABOVE HERE\nsigned main(){\n const int D = 11;\n \n int n,m;\n cin>>n>>m;\n vector<string> s(n);\n for(int i=0;i<n;i++) cin>>s[i];\n\n vector<vector<int> > E(n,vector<int>(n,0));\n for(int i=0;i<m;i++){\n int x,y;\n cin>>x>>y;\n x--;y--;\n E[x][y]=1;\n }\n\n vector<int> l(n);\n for(int i=0;i<n;i++) l[i]=s[i].size();\n\n\n using T = tuple<int, int, int>;\n auto idx=[&](int d,int x,int y){return (D+d)*n*n+x*n+y;};\n auto inv=[&](int v){return T(v/(n*n)-D,v%(n*n)/n,v%n);};\n int sz=idx(D,n-1,n-1)+1;\n\n queue<T> qt;\n vector<int> dp(sz,-1);\n \n // center inside s[x]\n for(int x=0;x<n;x++){\n // even\n for(int i=1;i<l[x];i++){\n // [0, i) + [i, l)\n int k=min(i,l[x]-i),ok=1;\n for(int j=0;j<k;j++)\n\tok&=s[x][i-(j+1)]==s[x][i+j];\n if(ok){\n\tint d=i-(l[x]-i);\n\tqt.emplace(d,x,x);\n\tchmax(dp[idx(d,x,x)],l[x]);\n }\n }\n // odd\n for(int i=0;i<l[x];i++){\n // [0, i) + [i, i+1) + [i+1, l)\n int k=min(i,l[x]-(i+1)),ok=1;\n for(int j=0;j<k;j++)\n\tok&=s[x][i-(j+1)]==s[x][i+1+j];\n if(ok){\n\tint d=i-(l[x]-(i+1));\n\tqt.emplace(i-(l[x]-(i+1)),x,x);\n\tchmax(dp[idx(d,x,x)],l[x]);\n }\n }\n }\n\n // s[x] |center| s[y] (obviously even)\n for(int x=0;x<n;x++){\n for(int y=0;y<n;y++){\n if(!E[x][y]) continue;\n int k=min(l[x],l[y]),ok=1;\n for(int j=0;j<k;j++)\n\tok&=s[x][l[x]-(j+1)]==s[y][j];\n if(ok){\n\tint d=l[x]-l[y];\n\tqt.emplace(d,x,y);\n\tchmax(dp[idx(d,x,y)],l[x]+l[y]);\n }\n } \n }\n\n using P = pair<int, int>;\n vector<vector<P> > G(sz),R(sz);\n \n for(int d=-D;d<=D;d++){\n for(int x=0;x<n;x++){\n for(int y=0;y<n;y++){\n\tint v=idx(d,x,y);\n\t//cout<<v<<\":\"<<d<<\" \"<<x<<\" \"<<y<<endl;\n\tfor(int i=0;i<n;i++){\n\t // s[i] s[x] s[y]\n\t if(d<=0&&E[i][x]){\n\t int k=min(-d,l[i]),ok=1;\n\t for(int j=0;j<k;j++)\n\t ok&=s[i][l[i]-(j+1)]==s[y][l[y]+d+j];\n\t if(ok){\t \n\t int nd=d+l[i];\n\t int u=idx(nd,i,y);\n\t //cout<<v<<\" -> \"<<u<<endl;\n\t G[v].emplace_back(u,l[i]);\n\t R[u].emplace_back(v,l[i]);\t \n\t }\n\t }\n\t // s[x] s[y] s[i]\n\t if(d>=0&&E[y][i]){\n\t int k=min(d,l[i]),ok=1;\n\t for(int j=0;j<k;j++)\n\t ok&=s[i][j]==s[x][d-(j+1)];\n\t if(ok){\n\t int nd=d-l[i];\n\t int u=idx(nd,x,i);\n\t //cout<<v<<\" -> \"<<u<<endl;\n\t G[v].emplace_back(u,l[i]);\n\t R[u].emplace_back(v,l[i]);\t \n\t }\n\t }\n\t}\n } \n }\n }\n\n // find nodes that can reached from starts\n while(!qt.empty()){\n int d,x,y;\n tie(d,x,y)=qt.front();qt.pop();\n int v=idx(d,x,y);\n //cout<<dp[v]<<\":\"<<d<<\" \"<<x<<\" \"<<y<<endl;\n for(auto e:G[v]){\n int u,c;\n tie(u,c)=e;\n if(~dp[u]) continue;\n chmax(dp[u],dp[v]+c);\n qt.emplace(inv(u)); \n } \n }\n\n vector<int> dp2(sz,-1),used(sz,0),visit(sz,0);\n queue<int> qu;\n \n function<int(int)> dfs=\n [&](int v){\n int &res=dp2[v];\n if(used[v]) return res;\n //cout<<v<<\"****\"<<s[get<1>(inv(v))]<<\" \"<<s[get<2>(inv(v))]<<endl;\n //cout<<get<0>(inv(v))<<\":\"<<get<1>(inv(v))<<\":\"<<get<2>(inv(v))<<endl;\n \n visit[v]=1;\n qu.emplace(v);\n res=dp[v];\n \n for(auto e:R[v]){\n\tint u,c;\n\ttie(u,c)=e;\n\tif(dp[u]<0) continue;\n\t//cout<<setw(6)<<v<<\":\"<<setw(3)<<dp[v]<<\":\"<<setw(3)<<visit[v]<<endl;\n\t//cout<<setw(6)<<u<<\"+\"<<setw(3)<<dp[u]<<\"+\"<<setw(3)<<visit[u]<<endl;\n\tif(visit[u]==1){\n\t cout<<-1<<endl;\n\t exit(0);\n\t}\n\tchmax(res,dfs(u)+c);\n }\n visit[v]=0;\n\n //cout<<v<<\"::\"<<res<<endl;\n return res;\n };\n \n int ans=0;\n for(int x=0;x<n;x++){\n for(int y=0;y<n;y++){\n int v=idx(0,x,y);\n if(~dp[v]) chmax(ans,dfs(v));\n while(!qu.empty()){\n\tused[qu.front()]=1;\n\tvisit[qu.front()]=2;\n\tqu.pop();\n }\n }\n }\n\n cout<<ans<<endl;\n return 0;\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 28520, "score_of_the_acc": -0.2488, "final_rank": 5 }, { "submission_id": "aoj_2307_3178274", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing Int = long long;\ntemplate<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}\ntemplate<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}\n\n//INSERT ABOVE HERE\nsigned main(){\n const int D = 11;\n \n int n,m;\n cin>>n>>m;\n vector<string> s(n);\n for(int i=0;i<n;i++) cin>>s[i];\n\n vector<vector<int> > E(n,vector<int>(n,0));\n for(int i=0;i<m;i++){\n int x,y;\n cin>>x>>y;\n x--;y--;\n E[x][y]=1;\n }\n\n vector<int> l(n);\n for(int i=0;i<n;i++) l[i]=s[i].size();\n\n\n using T = tuple<int, int, int>;\n auto idx=[&](int d,int x,int y){return (D+d)*n*n+x*n+y;};\n auto inv=[&](int v){return T(v/(n*n)-D,v%(n*n)/n,v%n);};\n int sz=idx(D,n-1,n-1)+1;\n\n queue<T> qt;\n vector<int> dp(sz,-1);\n \n // center inside s[x]\n for(int x=0;x<n;x++){\n // even\n for(int i=1;i<l[x];i++){\n // [0, i) + [i, l)\n int k=min(i,l[x]-i),ok=1;\n for(int j=0;j<k;j++)\n\tok&=s[x][i-(j+1)]==s[x][i+j];\n if(ok){\n\tint d=i-(l[x]-i);\n\tqt.emplace(d,x,x);\n\tchmax(dp[idx(d,x,x)],l[x]);\n }\n }\n // odd\n for(int i=0;i<l[x];i++){\n // [0, i) + [i, i+1) + [i+1, l)\n int k=min(i,l[x]-(i+1)),ok=1;\n for(int j=0;j<k;j++)\n\tok&=s[x][i-(j+1)]==s[x][i+1+j];\n if(ok){\n\tint d=i-(l[x]-(i+1));\n\tqt.emplace(i-(l[x]-(i+1)),x,x);\n\tchmax(dp[idx(d,x,x)],l[x]);\n }\n }\n }\n\n // s[x] |center| s[y] (obviously even)\n for(int x=0;x<n;x++){\n for(int y=0;y<n;y++){\n if(!E[x][y]) continue;\n int k=min(l[x],l[y]),ok=1;\n for(int j=0;j<k;j++)\n\tok&=s[x][l[x]-(j+1)]==s[y][j];\n if(ok){\n\tint d=l[x]-l[y];\n\tqt.emplace(d,x,y);\n\tchmax(dp[idx(d,x,x)],l[x]+l[y]);\n }\n } \n }\n\n using P = pair<int, int>;\n vector<vector<P> > G(sz),R(sz);\n \n for(int d=-D;d<=D;d++){\n for(int x=0;x<n;x++){\n for(int y=0;y<n;y++){\n\tint v=idx(d,x,y);\n\t//cout<<v<<\":\"<<d<<\" \"<<x<<\" \"<<y<<endl;\n\tfor(int i=0;i<n;i++){\n\t // s[i] s[x] s[y]\n\t if(d<=0&&E[i][x]){\n\t int k=min(-d,l[i]),ok=1;\n\t for(int j=0;j<k;j++)\n\t ok&=s[i][l[i]-(j+1)]==s[y][l[y]+d+j];\n\t if(ok){\t \n\t int nd=d+l[i];\n\t int u=idx(nd,i,y);\n\t //cout<<v<<\" -> \"<<u<<endl;\n\t G[v].emplace_back(u,l[i]);\n\t R[u].emplace_back(v,l[i]);\t \n\t }\n\t }\n\t // s[x] s[y] s[i]\n\t if(d>=0&&E[y][i]){\n\t int k=min(d,l[i]),ok=1;\n\t for(int j=0;j<k;j++)\n\t ok&=s[i][j]==s[x][d-(j+1)];\n\t if(ok){\n\t int nd=d-l[i];\n\t int u=idx(nd,x,i);\n\t //cout<<v<<\" -> \"<<u<<endl;\n\t G[v].emplace_back(u,l[i]);\n\t R[u].emplace_back(v,l[i]);\t \n\t }\n\t }\n\t}\n } \n }\n }\n\n // find nodes that can reached from starts\n while(!qt.empty()){\n int d,x,y;\n tie(d,x,y)=qt.front();qt.pop();\n int v=idx(d,x,y);\n //cout<<dp[v]<<\":\"<<d<<\" \"<<x<<\" \"<<y<<endl;\n for(auto e:G[v]){\n int u,c;\n tie(u,c)=e;\n if(~dp[u]) continue;\n chmax(dp[u],dp[v]+c);\n qt.emplace(inv(u)); \n } \n }\n\n vector<int> dp2(sz,-1),used(sz,0),visit(sz,0);\n queue<int> qu;\n \n function<int(int)> dfs=\n [&](int v){\n int &res=dp2[v];\n if(used[v]) return res;\n //cout<<v<<\"****\"<<endl;\n //cout<<get<0>(inv(v))<<\":\"<<get<1>(inv(v))<<\":\"<<get<2>(inv(v))<<endl;\n \n visit[v]=1;\n qu.emplace(v);\n res=dp[v];\n for(auto e:R[v]){\n\tint u,c;\n\ttie(u,c)=e;\n\tif(dp[u]<0) continue;\n\t//cout<<setw(6)<<v<<\":\"<<setw(3)<<dp[v]<<\":\"<<setw(3)<<visit[v]<<endl;\n\t//cout<<setw(6)<<u<<\"+\"<<setw(3)<<dp[u]<<\"+\"<<setw(3)<<visit[u]<<endl;\n\tif(visit[u]==1){\n\t cout<<-1<<endl;\n\t exit(0);\n\t}\n\tchmax(res,dfs(u)+c);\n }\n visit[v]=0;\n return res;\n };\n \n int ans=0;\n for(int x=0;x<n;x++){\n for(int y=0;y<n;y++){\n int v=idx(0,x,y);\n if(~dp[v]) chmax(ans,dfs(v));\n while(!qu.empty()){\n\tused[qu.front()]=1;\n\tvisit[qu.front()]=2;\n\tqu.pop();\n }\n }\n }\n\n cout<<ans<<endl;\n return 0;\n}", "accuracy": 0.8163265306122449, "time_ms": 110, "memory_kb": 28468, "score_of_the_acc": -0.2504, "final_rank": 13 }, { "submission_id": "aoj_2307_3178270", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing Int = long long;\ntemplate<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}\ntemplate<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}\n\n//INSERT ABOVE HERE\nsigned main(){\n const int D = 11;\n \n int n,m;\n cin>>n>>m;\n vector<string> s(n);\n for(int i=0;i<n;i++) cin>>s[i];\n\n vector<vector<int> > E(n,vector<int>(n,0));\n for(int i=0;i<m;i++){\n int x,y;\n cin>>x>>y;\n x--;y--;\n E[x][y]=1;\n }\n\n vector<int> l(n);\n for(int i=0;i<n;i++) l[i]=s[i].size();\n\n\n using T = tuple<int, int, int>;\n auto idx=[&](int d,int x,int y){return (D+d)*n*n+x*n+y;};\n auto inv=[&](int v){return T(v/(n*n)-D,v%(n*n)/n,v%n);};\n int sz=idx(D,n-1,n-1)+1;\n\n queue<T> qt;\n vector<int> dp(sz,-1);\n \n // center inside s[x]\n for(int x=0;x<n;x++){\n // even\n for(int i=1;i<l[x];i++){\n // [0, i) + [i, l)\n int k=min(i,l[x]-i),ok=1;\n for(int j=0;j<k;j++)\n\tok&=s[x][i-(j+1)]==s[x][i+j];\n if(ok){\n\tint d=i-(l[x]-i);\n\tqt.emplace(d,x,x);\n\tchmax(dp[idx(d,x,x)],l[x]);\n }\n }\n // odd\n for(int i=0;i<l[x];i++){\n // [0, i) + [i, i+1) + [i+1, l)\n int k=min(i,l[x]-(i+1)),ok=1;\n for(int j=0;j<k;j++)\n\tok&=s[x][i-(j+1)]==s[x][i+1+j];\n if(ok){\n\tint d=i-(l[x]-(i+1));\n\tqt.emplace(i-(l[x]-(i+1)),x,x);\n\tchmax(dp[idx(d,x,x)],l[x]);\n }\n }\n }\n\n // s[x] |center| s[y] (obviously even)\n for(int x=0;x<n;x++){\n for(int y=0;y<n;y++){\n if(!E[x][y]) continue;\n int k=min(l[x],l[y]),ok=1;\n for(int j=0;j<k;j++)\n\tok&=s[x][l[x]-(j+1)]==s[y][j];\n if(ok){\n\tint d=l[x]-l[y];\n\tqt.emplace(d,x,y);\n\tchmax(dp[idx(d,x,x)],l[x]+l[y]);\n }\n } \n }\n\n using P = pair<int, int>;\n vector<vector<P> > G(sz),R(sz);\n \n for(int d=-D;d<=D;d++){\n for(int x=0;x<n;x++){\n for(int y=0;y<n;y++){\n\tint v=idx(d,x,y);\n\t//cout<<v<<\":\"<<d<<\" \"<<x<<\" \"<<y<<endl;\n\tfor(int i=0;i<n;i++){\n\t // s[i] s[x] s[y]\n\t if(d<=0&&E[i][x]){\n\t int k=min(-d,l[i]),ok=1;\n\t for(int j=0;j<k;j++)\n\t ok&=s[i][l[i]-(j+1)]==s[y][l[y]+d+j];\n\t if(ok){\t \n\t int nd=d+l[i];\n\t int u=idx(nd,i,y);\n\t //cout<<v<<\" -> \"<<u<<endl;\n\t G[v].emplace_back(u,l[i]);\n\t R[u].emplace_back(v,l[i]);\t \n\t }\n\t }\n\t // s[x] s[y] s[i]\n\t if(d>=0&&E[y][i]){\n\t int k=min(d,l[i]),ok=1;\n\t for(int j=0;j<k;j++)\n\t ok&=s[i][j]==s[x][d-(j+1)];\n\t if(ok){\n\t int nd=d-l[i];\n\t int u=idx(nd,x,i);\n\t //cout<<v<<\" -> \"<<u<<endl;\n\t G[v].emplace_back(u,l[i]);\n\t R[u].emplace_back(v,l[i]);\t \n\t }\n\t }\n\t}\n } \n }\n }\n\n // find nodes that can reached from starts\n while(!qt.empty()){\n int d,x,y;\n tie(d,x,y)=qt.front();qt.pop();\n int v=idx(d,x,y);\n //cout<<dp[v]<<\":\"<<d<<\" \"<<x<<\" \"<<y<<endl;\n for(auto e:G[v]){\n int u,c;\n tie(u,c)=e;\n if(~dp[u]) continue;\n chmax(dp[u],dp[v]+c);\n qt.emplace(inv(u)); \n } \n }\n\n vector<int> dp2(sz,-1),used(sz,0),visit(sz,-1);\n queue<int> qu;\n int pos=0;\n function<int(int)> dfs=\n [&](int v){\n int &res=dp2[v];\n if(used[v]) return res;\n //cout<<v<<\"****\"<<endl;\n //cout<<get<0>(inv(v))<<\":\"<<get<1>(inv(v))<<\":\"<<get<2>(inv(v))<<endl;\n \n visit[v]=pos++;\n qu.emplace(v);\n res=dp[v];\n for(auto e:R[v]){\n\tint u,c;\n\ttie(u,c)=e;\n\tif(dp[u]<0) continue;\n\t//cout<<setw(6)<<v<<\":\"<<setw(3)<<dp[v]<<\":\"<<setw(3)<<visit[v]<<endl;\n\t//cout<<setw(6)<<u<<\"+\"<<setw(3)<<dp[u]<<\"+\"<<setw(3)<<visit[u]<<endl;\n\tif(~visit[u]&&visit[u]<=visit[v]){\n\t cout<<-1<<endl;\n\t exit(0);\n\t}\n\tchmax(res,dfs(u)+c);\n }\n return res;\n };\n \n int ans=0;\n for(int x=0;x<n;x++){\n for(int y=0;y<n;y++){\n int v=idx(0,x,y);\n if(~dp[v]) chmax(ans,dfs(v));\n while(!qu.empty()){\n\tused[qu.front()]=1;\n\tvisit[qu.front()]=sz+1;\n\tqu.pop();\n }\n }\n }\n\n cout<<ans<<endl;\n return 0;\n}", "accuracy": 0.6632653061224489, "time_ms": 100, "memory_kb": 28464, "score_of_the_acc": -0.2482, "final_rank": 14 }, { "submission_id": "aoj_2307_3178268", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing Int = long long;\ntemplate<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}\ntemplate<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}\n\n//INSERT ABOVE HERE\nsigned main(){\n const int D = 11;\n \n int n,m;\n cin>>n>>m;\n vector<string> s(n);\n for(int i=0;i<n;i++) cin>>s[i];\n\n vector<vector<int> > E(n,vector<int>(n,0));\n for(int i=0;i<m;i++){\n int x,y;\n cin>>x>>y;\n x--;y--;\n E[x][y]=1;\n }\n\n vector<int> l(n);\n for(int i=0;i<n;i++) l[i]=s[i].size();\n\n\n using T = tuple<int, int, int>;\n auto idx=[&](int d,int x,int y){return (D+d)*n*n+x*n+y;};\n auto inv=[&](int v){return T(v/(n*n)-D,v%(n*n)/n,v%n);};\n int sz=idx(D,n-1,n-1)+1;\n\n queue<T> qt;\n vector<int> dp(sz,-1);\n \n // center inside s[x]\n for(int x=0;x<n;x++){\n // even\n for(int i=1;i<l[x];i++){\n // [0, i) + [i, l)\n int k=min(i,l[x]-i),ok=1;\n for(int j=0;j<k;j++)\n\tok&=s[x][i-(j+1)]==s[x][i+j];\n if(ok){\n\tint d=i-(l[x]-i);\n\tqt.emplace(d,x,x);\n\tchmax(dp[idx(d,x,x)],l[x]);\n }\n }\n // odd\n for(int i=0;i<l[x];i++){\n // [0, i) + [i, i+1) + [i+1, l)\n int k=min(i,l[x]-(i+1)),ok=1;\n for(int j=0;j<k;j++)\n\tok&=s[x][i-(j+1)]==s[x][i+1+j];\n if(ok){\n\tint d=i-(l[x]-(i+1));\n\tqt.emplace(i-(l[x]-(i+1)),x,x);\n\tchmax(dp[idx(d,x,x)],l[x]);\n }\n }\n }\n\n // s[x] |center| s[y] (obviously even)\n for(int x=0;x<n;x++){\n for(int y=0;y<n;y++){\n if(!E[x][y]) continue;\n int k=min(l[x],l[y]),ok=1;\n for(int j=0;j<k;j++)\n\tok&=s[x][l[x]-(j+1)]==s[y][j];\n if(ok){\n\tint d=l[x]-l[y];\n\tqt.emplace(d,x,y);\n\tchmax(dp[idx(d,x,x)],l[x]+l[y]);\n }\n } \n }\n\n using P = pair<int, int>;\n vector<vector<P> > G(sz),R(sz);\n \n for(int d=-D;d<=D;d++){\n for(int x=0;x<n;x++){\n for(int y=0;y<n;y++){\n\tint v=idx(d,x,y);\n\t//cout<<v<<\":\"<<d<<\" \"<<x<<\" \"<<y<<endl;\n\tfor(int i=0;i<n;i++){\n\t // s[i] s[x] s[y]\n\t if(d<=0&&E[i][x]){\n\t int k=min(-d,l[i]),ok=1;\n\t for(int j=0;j<k;j++)\n\t ok&=s[i][l[i]-(j+1)]==s[y][l[y]+d+j];\n\t if(ok){\t \n\t int nd=d+l[i];\n\t int u=idx(nd,i,y);\n\t //cout<<v<<\" -> \"<<u<<endl;\n\t G[v].emplace_back(u,l[i]);\n\t R[u].emplace_back(v,l[i]);\t \n\t }\n\t }\n\t // s[x] s[y] s[i]\n\t if(d>=0&&E[y][i]){\n\t int k=min(d,l[i]),ok=1;\n\t for(int j=0;j<k;j++)\n\t ok&=s[i][j]==s[x][d-(j+1)];\n\t if(ok){\n\t int nd=d-l[i];\n\t int u=idx(nd,x,i);\n\t //cout<<v<<\" -> \"<<u<<endl;\n\t G[v].emplace_back(u,l[i]);\n\t R[u].emplace_back(v,l[i]);\t \n\t }\n\t }\n\t}\n } \n }\n }\n\n // find nodes that can reached from starts\n while(!qt.empty()){\n int d,x,y;\n tie(d,x,y)=qt.front();qt.pop();\n int v=idx(d,x,y);\n //cout<<dp[v]<<\":\"<<d<<\" \"<<x<<\" \"<<y<<endl;\n for(auto e:G[v]){\n int u,c;\n tie(u,c)=e;\n if(~dp[u]) continue;\n chmax(dp[u],dp[v]+c);\n qt.emplace(inv(u)); \n } \n }\n\n vector<int> dp2(sz,-1),used(sz,0),visit(sz,-1);\n queue<int> qu;\n int pos=0;\n function<int(int)> dfs=\n [&](int v){\n int &res=dp2[v];\n if(used[v]) return res;\n //cout<<v<<\"****\"<<endl;\n //cout<<get<0>(inv(v))<<\":\"<<get<1>(inv(v))<<\":\"<<get<2>(inv(v))<<endl;\n \n visit[v]=pos++;\n qu.emplace(v);\n res=dp[v];\n for(auto e:R[v]){\n\tint u,c;\n\ttie(u,c)=e;\n\t//cout<<setw(6)<<v<<\":\"<<setw(3)<<dp[v]<<\":\"<<setw(3)<<visit[v]<<endl;\n\t//cout<<setw(6)<<u<<\"+\"<<setw(3)<<dp[u]<<\"+\"<<setw(3)<<visit[u]<<endl;\n\tif(dp[u]<0) continue;\n\tif(~visit[u]&&visit[u]<=visit[v]){\n\t cout<<-1<<endl;\n\t exit(0);\n\t}\n\tchmax(res,dfs(u)+c);\n }\n return res;\n };\n \n int ans=0;\n for(int x=0;x<n;x++){\n for(int y=0;y<n;y++){\n int v=idx(0,x,y);\n if(~dp[v]) chmax(ans,dfs(v));\n while(!qu.empty()){\n\tused[qu.front()]=1;\n\tqu.pop();\n }\n }\n }\n\n cout<<ans<<endl;\n return 0;\n}", "accuracy": 0.5816326530612245, "time_ms": 110, "memory_kb": 24084, "score_of_the_acc": -0.2034, "final_rank": 15 }, { "submission_id": "aoj_2307_3178207", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing Int = long long;\ntemplate<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}\ntemplate<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}\n\n//INSERT ABOVE HERE\nsigned main(){\n const int D = 11;\n \n int n,m;\n cin>>n>>m;\n vector<string> s(n);\n for(int i=0;i<n;i++) cin>>s[i];\n\n vector<vector<int> > E(n,vector<int>(n,0));\n for(int i=0;i<m;i++){\n int x,y;\n cin>>x>>y;\n x--;y--;\n E[x][y]=1;\n }\n\n vector<int> l(n);\n for(int i=0;i<n;i++) l[i]=s[i].size();\n\n\n using T = tuple<int, int, int>;\n auto idx=[&](int d,int x,int y){return (D+d)*n*n+x*n+y;};\n auto inv=[&](int v){return T(v/(n*n)-D,v%(n*n)/n,v%n);};\n int sz=idx(D,n-1,n-1)+1;\n\n queue<T> qt;\n vector<int> dp(sz,-1);\n \n // center inside s[x]\n for(int x=0;x<n;x++){\n // even\n for(int i=1;i<l[x];i++){\n // [0, i) + [i, l)\n int k=min(i,l[x]-i),ok=1;\n for(int j=0;j<k;j++)\n\tok&=s[x][i-(j+1)]==s[x][i+j];\n if(ok){\n\tint d=i-(l[x]-i);\n\tqt.emplace(d,x,x);\n\tchmax(dp[idx(d,x,x)],l[x]);\n }\n }\n // odd\n for(int i=0;i<l[x];i++){\n // [0, i) + [i, i+1) + [i+1, l)\n int k=min(i,l[x]-(i+1)),ok=1;\n for(int j=0;j<k;j++)\n\tok&=s[x][i-(j+1)]==s[x][i+1+j];\n if(ok){\n\tint d=i-(l[x]-(i+1));\n\tqt.emplace(i-(l[x]-(i+1)),x,x);\n\tchmax(dp[idx(d,x,x)],l[x]);\n }\n }\n }\n\n // s[x] |center| s[y] (obviously even)\n for(int x=0;x<n;x++){\n for(int y=0;y<n;y++){\n if(!E[x][y]) continue;\n int k=min(l[x],l[y]),ok=1;\n for(int j=0;j<k;j++)\n\tok&=s[x][l[x]-(j+1)]==s[y][j];\n if(ok){\n\tint d=l[x]-l[y];\n\tqt.emplace(d,x,y);\n\tchmax(dp[idx(d,x,x)],l[x]+l[y]);\n }\n } \n }\n\n using P = pair<int, int>;\n vector<vector<P> > G(sz),R(sz);\n \n for(int d=-D;d<=D;d++){\n for(int x=0;x<n;x++){\n for(int y=0;y<n;y++){\n\tif(!E[x][y]) continue;\n\tfor(int i=0;i<n;i++){\n\t // s[i] s[x] s[y]\n\t if(d<=0&&E[i][x]){\n\t int k=min(-d,l[i]),ok=1;\n\t for(int j=0;j<k;j++)\n\t ok&=s[i][l[i]-(j+1)]==s[y][l[y]-d+j];\n\t if(ok){\n\t int nd=d+l[i];\n\t int v=idx(d,x,y),u=idx(nd,i,y);\n\t G[v].emplace_back(u,l[i]);\n\t R[u].emplace_back(v,l[i]);\t \n\t }\n\t }\n\t // s[x] s[y] s[i]\n\t if(d>=0&&E[y][i]){\n\t int k=min(d,l[i]),ok=1;\n\t for(int j=0;j<k;j++)\n\t ok&=s[i][j]==s[x][d-(j+1)];\n\t if(ok){\n\t int nd=d-l[i];\n\t int v=idx(d,x,y),u=idx(nd,x,i);\n\t G[v].emplace_back(u,l[i]);\n\t R[u].emplace_back(v,l[i]);\t \n\t }\n\t }\n\t}\n } \n }\n }\n\n // find nodes that can reached from starts\n while(!qt.empty()){\n int d,x,y;\n tie(d,x,y)=qt.front();qt.pop();\n int v=idx(d,x,y);\n for(auto e:G[v]){\n int u,c;\n tie(u,c)=e;\n if(~dp[u]) continue;\n chmax(dp[u],dp[v]+c);\n qt.emplace(inv(u)); \n } \n }\n\n vector<int> dp2(sz,-1),visit(sz,0);\n\n int pos=0;\n function<int(int)> dfs=\n [&](int v){\n int &res=dp2[v];\n if(~res) return res;\n visit[v]=pos++;\n res=dp[v];\n for(auto e:R[v]){\n\tint u,c;\n\ttie(u,c)=e;\n\tif(dp[u]<0) continue;\n\tif(visit[u]<=visit[v]){\n\t cout<<-1<<endl;\n\t exit(0);\n\t}\n\tchmax(res,dfs(u)+c);\n }\n return res;\n };\n \n int ans=0;\n for(int x=0;x<n;x++)\n for(int y=0;y<n;y++)\n if(~dp[idx(0,x,y)]&&dp2[idx(0,x,y)]<0)\n\tchmax(ans,dfs(idx(0,x,y)));\n\n cout<<ans<<endl;\n return 0;\n}", "accuracy": 0.30612244897959184, "time_ms": 10, "memory_kb": 17224, "score_of_the_acc": -0.1076, "final_rank": 18 }, { "submission_id": "aoj_2307_2658585", "code_snippet": "#include \"bits/stdc++.h\"\n#include<unordered_map>\n#include<unordered_set>\n#pragma warning(disable:4996)\nusing namespace std;\n\nint main()\n{\n\tvector<char>chs(2000);\n\tvector<vector<int>>real_edges(2000);\n\t{\n\t\tint N, M; cin >> N >> M;\n\t\tvector<string>sts;\n\t\tfor (int i = 0; i < N; ++i) {\n\t\t\tstring st; cin >> st;\n\t\t\tsts.push_back(st);\n\t\t}\n\t\tvector<pair<int, int>>edges;\n\t\tfor (int i = 0; i < M; ++i) {\n\t\t\tint a, b; cin >> a >> b;\n\t\t\ta--; b--;\n\t\t\tedges.push_back(make_pair(a, b));\n\t\t}\n\t\tint id = 0;\n\t\tvector<int>ids(N);\n\t\tfor (int i = 0; i < N; ++i) {\n\t\t\tids[i] = id;\n\t\t\tfor (int j = 0; j < sts[i].size() - 1; ++j) {\n\t\t\t\treal_edges[ids[i] + j].push_back(ids[i] + j + 1);\n\t\t\t}\n\t\t\tfor (int j = 0; j < sts[i].size(); ++j) {\n\t\t\t\tchs[ids[i] + j] = sts[i][j];\n\t\t\t}\n\t\t\tid += sts[i].size();\n\t\t}\n\t\tids.push_back(id);\n\t\tfor (auto e : edges) {\n\t\t\treal_edges[ids[e.first + 1] - 1].push_back(ids[e.second]);\n\t\t}\n\t\tconst int start = id;\n\t\tconst int goal = start + 1;\n\n\t\tfor (int i = 0; i < N; ++i) {\n\t\t\treal_edges[start].push_back(ids[i]);\n\t\t\treal_edges[ids[i + 1] - 1].push_back(goal);\n\t\t}\n\t\tchs.resize(goal + 1);\n\t\treal_edges.resize(goal + 1);\n\t}\n\tconst int N = real_edges.size();\n\tvector<vector<int>>rev_edges(N);\n\n\tfor (int i = 0; i < N; ++i) {\n\t\tfor (auto e : real_edges[i]) {\n\t\t\trev_edges[e].push_back(i);\n\t\t}\n\t}\n\n\tvector<vector<int>>memo(N, vector<int>(N, -1));\n\tconst int S = N - 2;\n\tconst int G = S + 1;\n\tmemo[S][G]=0;\n\tqueue<pair<int,int>>que;\n\tque.push(make_pair(S,G));\n\tint ans=0;\n\twhile (!que.empty()) {\n\t\tconst auto atop(que.front());\n\t\tque.pop();\n\t\tconst int a=atop.first;\n\t\tconst int b=atop.second;\n\n\t\tif(memo[a][b]>8000)break;\n\t\n\n\t\tfor (auto next_a : real_edges[a]) {\n\t\t\tfor (auto next_b : rev_edges[b]) {\n\t\t\t\tif (chs[next_a] == chs[next_b]) {\n\t\t\t\t\tif (a == next_b&&b == next_a) {\n\t\t\t\t\t\tans=max(ans,memo[a][b]);\n\t\t\t\t\t\tif (ans > 2010) {\n\t\t\t\t\t\t\tans=-1;\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tif (next_b == next_a) {\n\t\t\t\t\t\tans=max(ans,memo[a][b]+1);\n\t\t\t\t\t\tif (ans > 2010) {\n\t\t\t\t\t\t\tans = -1;\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tif (memo[next_a][next_b] < memo[a][b]+2) {\n\t\t\t\t\t\tmemo[next_a][next_b]=memo[a][b]+2;\n\t\t\t\t\t\tque.push(make_pair(next_a,next_b));\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tcout<<ans<<endl;\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 870, "memory_kb": 19844, "score_of_the_acc": -0.3273, "final_rank": 6 }, { "submission_id": "aoj_2307_2658584", "code_snippet": "#include \"bits/stdc++.h\"\n#include<unordered_map>\n#include<unordered_set>\n#pragma warning(disable:4996)\nusing namespace std;\n\nint main()\n{\n\tvector<char>chs(2000);\n\tvector<vector<int>>real_edges(2000);\n\t{\n\t\tint N, M; cin >> N >> M;\n\t\tvector<string>sts;\n\t\tfor (int i = 0; i < N; ++i) {\n\t\t\tstring st; cin >> st;\n\t\t\tsts.push_back(st);\n\t\t}\n\t\tvector<pair<int, int>>edges;\n\t\tfor (int i = 0; i < M; ++i) {\n\t\t\tint a, b; cin >> a >> b;\n\t\t\ta--; b--;\n\t\t\tedges.push_back(make_pair(a, b));\n\t\t}\n\t\tint id = 0;\n\t\tvector<int>ids(N);\n\t\tfor (int i = 0; i < N; ++i) {\n\t\t\tids[i] = id;\n\t\t\tfor (int j = 0; j < sts[i].size() - 1; ++j) {\n\t\t\t\treal_edges[ids[i] + j].push_back(ids[i] + j + 1);\n\t\t\t}\n\t\t\tfor (int j = 0; j < sts[i].size(); ++j) {\n\t\t\t\tchs[ids[i] + j] = sts[i][j];\n\t\t\t}\n\t\t\tid += sts[i].size();\n\t\t}\n\t\tids.push_back(id);\n\t\tfor (auto e : edges) {\n\t\t\treal_edges[ids[e.first + 1] - 1].push_back(ids[e.second]);\n\t\t}\n\t\tconst int start = id;\n\t\tconst int goal = start + 1;\n\n\t\tfor (int i = 0; i < N; ++i) {\n\t\t\treal_edges[start].push_back(ids[i]);\n\t\t\treal_edges[ids[i + 1] - 1].push_back(goal);\n\t\t}\n\t\tchs.resize(goal + 1);\n\t\treal_edges.resize(goal + 1);\n\t}\n\tconst int N = real_edges.size();\n\tvector<vector<int>>rev_edges(N);\n\n\tfor (int i = 0; i < N; ++i) {\n\t\tfor (auto e : real_edges[i]) {\n\t\t\trev_edges[e].push_back(i);\n\t\t}\n\t}\n\n\tvector<vector<int>>memo(N, vector<int>(N, -1));\n\tconst int S = N - 2;\n\tconst int G = S + 1;\n\tmemo[S][G]=0;\n\tqueue<pair<int,int>>que;\n\tque.push(make_pair(S,G));\n\tint ans=0;\n\twhile (!que.empty()) {\n\t\tconst auto atop(que.front());\n\t\tque.pop();\n\t\tconst int a=atop.first;\n\t\tconst int b=atop.second;\n\n\t\tif(memo[a][b]>4000)break;\n\t\n\n\t\tfor (auto next_a : real_edges[a]) {\n\t\t\tfor (auto next_b : rev_edges[b]) {\n\t\t\t\tif (chs[next_a] == chs[next_b]) {\n\t\t\t\t\tif (a == next_b&&b == next_a) {\n\t\t\t\t\t\tans=max(ans,memo[a][b]);\n\t\t\t\t\t\tif (ans > 2010) {\n\t\t\t\t\t\t\tans=-1;\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tif (next_b == next_a) {\n\t\t\t\t\t\tans=max(ans,memo[a][b]+1);\n\t\t\t\t\t\tif (ans > 2010) {\n\t\t\t\t\t\t\tans = -1;\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tif (memo[next_a][next_b] < memo[a][b]+2) {\n\t\t\t\t\t\tmemo[next_a][next_b]=memo[a][b]+2;\n\t\t\t\t\t\tque.push(make_pair(next_a,next_b));\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tcout<<ans<<endl;\n\n\treturn 0;\n}", "accuracy": 0.41836734693877553, "time_ms": 10, "memory_kb": 7184, "score_of_the_acc": 0, "final_rank": 17 } ]
aoj_2311_cpp
問題文 お菓子の魔女 CHARLOTTE は 巴マミ とクッキーゲームを楽しんでいる.クッキーゲームは 8\times 8 の格子状に区切られたテーブルクロスの上にチーズクッキーとチョコレートクッキーを置いて行われる.各格子には高々 1 個のチョコレートクッキーまたはチーズクッキーしか置くことはできない. お菓子の魔女はチーズクッキーを, 巴マミ はチョコレートクッキーを交互に置いてゲームを行う.自分のクッキーを置いたあと,そのクッキーから上下左右斜めの各 8 方向について,置くクッキーとすでに置いていた自分のクッキーの間に相手のクッキーのみが直線に並んでいた場合に,その挟まれた相手のクッキーのすべてが自分のクッキーで置き換えられる.クッキーゲームのプレイヤーは自分のターンが回ってきた時, 1 つ自分のクッキーを置くことができる.ただし,相手のクッキーを少なくとも 1 つ以上自分のクッキーに置き換えられなければならない.そのような置き場がない場合,自分のターンをパスをしなければならない. お菓子の魔女も 巴マミ も考えるのが少々苦手である.そこで,回ってきたターン毎にそのターンの中で置き換えられるクッキーの数を最大化することを考えることにした. 巴マミ のターンのときに置き換えられるクッキーの数を最大にするようなクッキーを置く場所の候補が複数ある場合は,より上の場所を,それでも複数ある場合はより左の場所を選択することにした.また同様に,お菓子の魔女のターンのときに候補が複数ある場合はより下の場所を,それでも複数ある場合はより右の場所を選択することにした. テーブルクロスに置かれたクッキーの状態が与えられるので,巴マミからはじめ,彼女たちがそこからクッキーゲームを行い,共に新たなクッキーが置けなくなるまでゲームを続けた時のテーブルクロスの上に置かれたクッキーの状態を求めよ. 入力形式 入力は以下の形式で与えられる. s_{11} s_{12} ... s_{18}\\ s_{21} s_{22} ... s_{28}\\ ...\\ s_{81} s_{82} ... s_{88}\\ s_{ij} はテーブルクロスに置かれたクッキーの初期状態を表す文字で,上から i 行目,左から j 列目の格子の状態を表す.チョコレートクッキーが置かれているとき s_{ij} は 'o' であり,チーズクッキーが置かれているときは 'x' , 何も置かれていないときは '.' となる. 出力形式 クッキーゲームが行われた後のテーブルクロスの上に置かれたクッキーの状態を,入力形式と同じ形式で出力せよ. 制約 s_{ij} は 'o' , 'x' , '.' のいずれかである. 入出力例 入力例 1 ooox.... .x...... ooo..... ........ ........ ........ ........ ........ 出力例1 ooooo... .o...... ooo..... ...o.... ....o... ........ ........ ........ 入力例 2 ........ ........ ........ ...ox... ...xo... ........ ........ ........ 出力例 2 xxxxxxxx xxxooxxx xxxxooxx xxxxxxxx ooxxooox ooxoooox oxooooox ooooooox 入力例 3 ........ ........ ..ooo... ..oxo... ..ooo... ........ ........ ........ 出力例 3 ........ ........ ..ooo... ..ooo... ..ooo... .....o.. ......o. ........ Problem Setter: Flat35
[ { "submission_id": "aoj_2311_3524913", "code_snippet": "#include <iostream>\n#include <algorithm>\n#include <string>\n#include <vector>\n#include <stack>\n#include <queue>\n#include <set>\n#include <bitset>\n#include <map>\n#include <tuple>\n#include <unordered_set>\n#include <unordered_map>\n#include <list>\n#include <numeric>\n#include <utility>\n#include <iterator>\n#include <cstdio>\n#include <cmath>\n#include <cstdlib>\n#include <cstring>\n#include <climits>\n#include <ctime>\n#include <cassert>\n#include <random>\n#include <cstring>\n\n#define rep(i,n) for(int i=0;i<int(n);i++)\n#define all(x) (x).begin(),x.end()\n#define pb push_back\n\nusing namespace std;\nusing ll = long long;\n\nconst ll mod = 1000000007;\nint dx[4]={1,0,-1,0};\nint dy[4]={0,1,0,-1};\nint ddx[8]={-1,-1,0,1,1,1,0,-1};\nint ddy[8]={0,1,1,1,0,-1,-1,-1};\nbool debug=false;\n\n/*---------------------------------------------------*/\n\n#define CHOCO 'o'\n#define CHEESE 'x'\n#define NONE '.'\n\nusing Board = vector<vector<char>>;\n\nstruct Vector2 {\n int x;\n int y;\n};\n\nvoid debug_print(Board& board){\n for(int i = 0 ; i < 8; i++){\n for(int j = 0; j < 8; j++){\n cout << board[i][j]; \n }\n cout << endl;\n }\n cout << endl;\n}\n\nbool in_area(Vector2 v){\n return 0 <= v.x && v.x < 8 && 0 <= v.y && v.y < 8;\n}\n\nbool is_reverse(Board board, Vector2 v, int x, int y, char color){\n v.x += x; v.y += y;\n char check = (color == CHOCO ? CHEESE : CHOCO);\n bool ok = false;\n while(in_area(v)){\n if(board[v.y][v.x] == check) ok = true;\n else if(board[v.y][v.x] == color && ok) return true;\n else break;\n v.x += x; v.y += y;\n }\n return false;\n}\n\nint reverse_count(Board board, Vector2 v, char color){\n int reverse_num = 0;\n board[v.y][v.x] = color;\n for(int i = 0; i < 8; i++){\n if(is_reverse(board, v, ddx[i], ddy[i], color)) {\n Vector2 vv = v;\n vv.x += ddx[i], vv.y += ddy[i];\n while(board[vv.y][vv.x] != color) {\n\tboard[vv.y][vv.x] = color;\n\treverse_num++;\n\tvv.x += ddx[i], vv.y += ddy[i];\n }\n }\n }\n return reverse_num;\n}\n\nvoid reverse(Board& board, Vector2 v, char color){\n int reverse_num = 0;\n board[v.y][v.x] = color;\n for(int i = 0; i < 8; i++){\n if(is_reverse(board, v, ddx[i], ddy[i], color)) {\n Vector2 vv = v;\n vv.x += ddx[i], vv.y += ddy[i];\n while(board[vv.y][vv.x] != color) {\n\tboard[vv.y][vv.x] = color;\n\treverse_num++;\n\tvv.x += ddx[i], vv.y += ddy[i];\n }\n }\n }\n}\n\nvoid simulate(Board& board, char& color){\n int max_reverse = 0;\n Vector2 position;\n if(color == CHOCO){\n for(int h = 0; h < 8; h++){\n for(int w = 0; w < 8; w++) {\n\tif(board[h][w] != NONE) continue;\n\tint rev = reverse_count(board, {w, h}, color);\n\tif(max_reverse < rev) {\n\t max_reverse = rev;\n\t position = {w, h};\n\t}\n }\n }\n } else {\n for(int h = 7; h >= 0; h--){\n for(int w = 7; w >= 0; w--) {\n\tif(board[h][w] != NONE) continue;\n\tint rev = reverse_count(board, {w, h}, color);\n\tif(max_reverse < rev) {\n\t max_reverse = rev;\n\t position = {w, h};\n\t}\n }\n }\n }\n if(max_reverse == 0) return ;\n reverse(board, position, color);\n}\n\nint main(){\n vector<vector<char>> board = vector<vector<char>>(8, vector<char>(9));\n for(int i = 0; i < 8; i++){\n for(int j = 0; j < 8; j++){\n cin >> board[i][j];\n }\n }\n \n for(int turn = 0; turn < 64 * 2; turn++){\n char color = (turn % 2 == 0 ? CHOCO : CHEESE);\n simulate(board, color);\n // debug_print(board);\n }\n \n for(int i = 0 ; i < 8; i++){\n for(int j = 0; j < 8; j++){\n cout << board[i][j]; \n }\n cout << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3172, "score_of_the_acc": -0.9714, "final_rank": 2 }, { "submission_id": "aoj_2311_2295235", "code_snippet": "#include \"bits/stdc++.h\"\n\n#define REP(i,n) for(ll i=0;i<n;++i)\n#define RREP(i,n) for(ll i=n-1;i>=0;--i)\n#define FOR(i,m,n) for(ll i=m;i<n;++i)\n#define RFOR(i,m,n) for(ll i=n-1;i>=m;--i)\n#define ALL(v) (v).begin(),(v).end()\n#define PB(a) push_back(a)\n#define UNIQUE(v) v.erase(unique(ALL(v)),v.end());\n#define DUMP(v) REP(i, (v).size()) { cout << v[i]; if (i != v.size() - 1)cout << \" \"; else cout << endl; }\n#define INF 1000000001ll\n#define MOD 1000000007ll\n#define EPS 1e-9\n\nconst int dx[8] = { 1,1,0,-1,-1,-1,0,1 };\nconst int dy[8] = { 0,1,1,1,0,-1,-1,-1 };\n\n\nusing namespace std;\ntypedef long long ll;\ntypedef vector<int> vi;\ntypedef vector<ll> vl;\ntypedef vector<vi> vvi;\ntypedef vector<vl> vvl;\ntypedef pair<int, int> pii;\ntypedef pair<ll, ll> pll;\nll max(ll a, int b) { return max(a, ll(b)); }\nll max(int a, ll b) { return max(ll(a), b); }\nll min(ll a, int b) { return min(a, ll(b)); }\nll min(int a, ll b) { return min(ll(a), b); }\n///(?´????????`)(?´????????`)(?´????????`)(?´????????`)(?´????????`)(?´????????`)///\nint main() {\n\tcin.tie(0);\n\tios::sync_with_stdio(false);\n\tvector<string> v(8);\n\tREP(i, 8)cin >> v[i];\n\t\n\t\n\tREP(t, 10000) {\n\t\tint p = -1, q = -1, cmax = -1;\n\t\tif (t % 2 == 0) {\n\t\t\tvi m;\n\t\t\tREP(i, 8) {\n\t\t\t\tREP(j, 8) {\n\t\t\t\t\tif (v[i][j] == '.') {\n\t\t\t\t\t\tint c = 0;\n\t\t\t\t\t\tvi s;\n\t\t\t\t\t\tREP(k, 8) {\n\t\t\t\t\t\t\tint ni = i, nj = j;\n\t\t\t\t\t\t\tint tmp = 0;\n\n\t\t\t\t\t\t\tREP(l, INF) {\n\t\t\t\t\t\t\t\tni += dx[k], nj += dy[k];\n\t\t\t\t\t\t\t\tif (ni < 0 || nj < 0 || ni >= 8 || nj >= 8)break;\n\t\t\t\t\t\t\t\tif (v[ni][nj] == 'x')tmp++;\n\t\t\t\t\t\t\t\telse if (v[ni][nj] == 'o') {\n\t\t\t\t\t\t\t\t\tif (tmp != 0)s.push_back(k);\n\t\t\t\t\t\t\t\t\tc += tmp;\n\t\t\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\telse break;\n\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (cmax < c) {\n\t\t\t\t\t\t\tcmax = c, p = i, q = j;\n\t\t\t\t\t\t\tm = s;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (cmax <= 0)continue;\n\t\t\telse {\n\n\t\t\t\tv[p][q] = 'o';\n\t\t\t\tREP(j, m.size()) {\n\t\t\t\t\tint ni = p, nj = q;\n\t\t\t\t\tREP(i, INF) {\n\t\t\t\t\t\tni += dx[m[j]], nj += dy[m[j]];\n\t\t\t\t\t\tif (ni >= 0 && nj >= 0 && ni < 8 && nj < 8 && v[ni][nj] == 'x')v[ni][nj] = 'o';\n\t\t\t\t\t\telse break;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\n\t\t\t}\n\t\t}\n\n\t\telse {\n\t\t\tvi m;\n\t\t\tREP(i, 8) {\n\t\t\t\tREP(j, 8) {\n\t\t\t\t\tif (v[i][j] == '.') {\n\t\t\t\t\t\tint c = 0;\n\t\t\t\t\t\tvi s;\n\t\t\t\t\t\tREP(k, 8) {\n\t\t\t\t\t\t\tint ni = i, nj = j;\n\t\t\t\t\t\t\tint tmp = 0;\n\n\t\t\t\t\t\t\tREP(l, INF) {\n\t\t\t\t\t\t\t\tni += dx[k], nj += dy[k];\n\t\t\t\t\t\t\t\tif (ni < 0 || nj < 0 || ni >= 8 || nj >= 8)break;\n\t\t\t\t\t\t\t\tif (v[ni][nj] == 'o')tmp++;\n\t\t\t\t\t\t\t\telse if (v[ni][nj] == 'x') {\n\t\t\t\t\t\t\t\t\tif (tmp != 0)s.push_back(k);\n\t\t\t\t\t\t\t\t\tc += tmp;\n\t\t\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\telse break;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (cmax <= c) {\n\t\t\t\t\t\t\tcmax = c, p = i, q = j;\n\t\t\t\t\t\t\tm = s;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (cmax <= 0)continue;\n\t\t\telse {\n\n\t\t\t\tv[p][q] = 'x';\n\t\t\t\tREP(j, m.size()) {\n\t\t\t\t\tint ni = p, nj = q;\n\t\t\t\t\tREP(i, INF) {\n\t\t\t\t\t\tni += dx[m[j]], nj += dy[m[j]];\n\t\t\t\t\t\tif (ni >= 0 && nj >= 0 && ni < 8 && nj < 8 && v[ni][nj] == 'o')v[ni][nj] = 'x';\n\t\t\t\t\t\telse break;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\n\t\t\t}\n\t\t}\n\t}\n\tREP(i, 8) {\n\t\tREP(j, 8) {\n\t\t\tcout << v[i][j];\n\t\t}\n\t\tcout << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3232, "score_of_the_acc": -2, "final_rank": 3 }, { "submission_id": "aoj_2311_1680848", "code_snippet": "#include<iostream>\nusing namespace std;\n\n#define MAX_N 12\n#define MAX_T 10000\n\nint x[MAX_N][MAX_N];\nint z[8],y[8];\nchar c,T[4]=\".ox\";\n\nint main(){\n\tfor(int i=1;i<=8;i++){\n\t\tfor(int j=1;j<=8;j++){\n\t\t\tcin>>c;\n\t\t\tif(c=='o'){\n\t\t\t\tx[i][j]=1;\n\t\t\t}\n\t\t\tif(c=='x'){\n\t\t\t\tx[i][j]=2;\n\t\t\t}\n\t\t\tif(c=='.'){\n\t\t\t\tx[i][j]=0;\n\t\t\t}\n\t\t}\n\t}\n\tfor(int i=0;i<MAX_T;i++){\n\t\tint dx[8]={0,1,1,1,0,-1,-1,-1};\n\t\tint dy[8]={-1,-1,0,1,1,1,0,-1};\n\t\tfor(int h=2;h>0;h--){\n\t\t\tint score=0,maxn=0,cnt=0,X=0,Y=0,cx=0,cy=0;\n\t\t\tfor(int j=1;j<=8;j++){\n\t\t\t\tfor(int k=1;k<=8;k++){\n\t\t\t\t\tif(x[j][k]==0){\n\t\t\t\t\t\tscore=0;\n\t\t\t\t\t\tfor(int l=0;l<8;l++){\n\t\t\t\t\t\t\tz[l]=0;\n\t\t\t\t\t\t\tcnt=0;\n\t\t\t\t\t\t\tcx=j+dx[l];cy=k+dy[l];\n\t\t\t\t\t\t\twhile(cx>=1 && cx<=8 && cy>=1 && cy<=8){\n\t\t\t\t\t\t\t\tif(x[cx][cy]==0){\n\t\t\t\t\t\t\t\t\tgoto E;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tif(x[cx][cy]!=h){\n\t\t\t\t\t\t\t\t\tgoto F;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tcnt++;\n\t\t\t\t\t\t\t\tcx+=dx[l];cy+=dy[l];\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tgoto E;\nF:;\n\t\t\t\t\t\t\tscore+=cnt;\n\t\t\t\t\t\t\tz[l]=1;\nE:;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif(score>maxn || (score==maxn && h==1)){\n\t\t\t\t\t\t\tmaxn=score;X=j;Y=k;\n\t\t\t\t\t\t\tfor(int l=0;l<8;l++){y[l]=z[l];}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(maxn>=1){\n\t\t\t\tx[X][Y]=(3-h);\n\t\t\t\tfor(int l=0;l<8;l++){\n\t\t\t\t\tif(y[l]==1){\n\t\t\t\t\t\tcx=X+dx[l];cy=Y+dy[l];\n\t\t\t\t\t\twhile(cx>=1 && cx<=8 && cy>=1 && cy<=8){\n\t\t\t\t\t\t\tif(x[cx][cy]!=h){\n\t\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tx[cx][cy]=(3-h);\n\t\t\t\t\t\t\tcx+=dx[l];cy+=dy[l];\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tfor(int i=1;i<=8;i++){\n\t\tfor(int j=1;j<=8;j++){\n\t\t\tcout<<T[x[i][j]];\n\t\t}\n\t\tcout<<endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 1136, "score_of_the_acc": -0.5, "final_rank": 1 } ]
aoj_2305_cpp
Beautiful Currency KM country has N kinds of coins and each coin has its value a_i . The king of the country, Kita_masa, thought that the current currency system is poor, and he decided to make it beautiful by changing the values of some (possibly no) coins. A currency system is called beautiful if each coin has an integer value and the ( i +1)-th smallest value is divisible by the i -th smallest value for all i ( 1 \leq i \leq N-1 ). For example, the set {1, 5, 10, 50, 100, 500} is considered as a beautiful system, while the set {1, 5, 10, 25, 50, 100} is NOT, because 25 is not divisible by 10 . Since changing the currency system may confuse citizens, the king, Kita_masa, wants to minimize the maximum value of the confusion ratios. Here, the confusion ratio for the change in the i -th coin is defined as |a_i - b_i| / a_i , where a_i and b_i is the value of i -th coin before and after the structure changes, respectively. Note that Kita_masa can change the value of each existing coin, but he cannot introduce new coins nor eliminate existing coins. After the modification, the values of two or more coins may coincide. Input Each dataset contains two lines. The first line contains a single integer, N , and the second line contains N integers, {a_i} . You may assume the following constraints: 1 \leq N \leq 20 1 \leq a_1 \lt a_2 \lt... \lt a_N \lt 10^5 Output Output one number that represents the minimum of the maximum value of the confusion ratios. The value may be printed with an arbitrary number of decimal digits, but may not contain an absolute error greater than or equal to 10^{-8} . Sample Input 1 3 6 11 12 Output for the Sample Input 1 0.090909090909 Sample Input 2 3 6 11 24 Output for the Sample Input 2 0.090909090909 Sample Input 3 3 6 11 30 Output for the Sample Input 3 0.166666666667
[ { "submission_id": "aoj_2305_10867796", "code_snippet": "#include <iostream>\n#include <cstdio>\n#include <algorithm>\n#include <cmath>\nusing namespace std;\ntypedef long long ll;\nconst double inf = 1e15;\nconst double eps = 1e-8;\nconst int N = 25;\nconst int M = 200005;\nint a[N], n;\ndouble ans, dp[N][M];\ndouble dfs(int dep, int fa) {\n\tdouble &ret = dp[dep][fa];\n\tif(ret >= -eps) return ret;\n\n\tif(dep == n)\n\t\treturn ret = (double)(fabs(fa - a[dep])) / a[dep];\n\n\tret = inf;\n\tfor(int i = fa; i < M; i += fa) {\n\t\tret = min(ret, dfs(dep+1, i));\n\t}\n\tret = max(ret, (double)(fabs(fa - a[dep])) / a[dep]);\n\treturn ret;\n}\nint main() {\n\twhile(~scanf(\"%d\", &n)) {\n\t\tfor(int i = 1; i <= n; i ++)\n\t\t\tscanf(\"%d\", &a[i]);\n\n\t\tfor(int i = 1; i <= n; i ++)\n\t\t\tfor(int j = 0; j < M; j ++)\n\t\t\t\tdp[i][j] = -1;\n\n\t\t\n\t\tdouble ans = inf;\n\t\tfor(int i = 1; i < M; i ++)\n\t\t\tans = min(ans, dfs(1, i));\n\n\t\t\n\t\tprintf(\"%.10f\\n\", ans);\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 110, "memory_kb": 36768, "score_of_the_acc": -0.4184, "final_rank": 9 }, { "submission_id": "aoj_2305_10689567", "code_snippet": "#include <iostream>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <cmath>\n#include <algorithm>\n#include <string>\n#include <vector>\n#include <stack>\n#include <queue>\n#include <set>\n#include <map>\n#include <time.h>\nusing namespace std;\ntypedef long long lint ;\n#define N 150010\nvector<int> v[N] ; \nvoid get_ans(){\n for(lint i = 1 ; i < N ; i++){\n v[i].clear() ;\n }\n for(lint i = 1 ; i < N ; i++){\n for(lint j = 1 ; j < N ; j++){\n if(i*j > N) break ;\n lint tag = i*j ;\n v[tag].push_back(i) ;\n }\n }\n}\ndouble f[21][150001];\ndouble a[21];\nint main(){\n\tint n;\n\tget_ans();\n\tscanf(\"%d\",&n);\n\tfor (int i=1;i<=n;i++){\n\t\tscanf(\"%lf\",&a[i]);\n\t}\n\tfor (int i=1;i<=20;i++)\n\t\tfor (int j=1;j<=150000;j++)\n\t\t\tf[i][j]=1000000000;\n\tfor (int i=1;i<=150000;i++){\n\t\tf[1][i]=abs(a[1]-i)/a[1];\n\t}\n\tfor (int i=2;i<=n;i++){\n\t\tfor (int j=1;j<=150000;j++)\n\t\t\tfor (int k=0;k<v[j].size();k++){\n\t\t\t\tint kk=v[j][k];\n\t\t\t\tf[i][j]=min(f[i][j],max(f[i-1][kk],abs(a[i]-j)/a[i]));\n\t\t\t}\n\t}\n\tdouble ans=1000000000;\n\tfor (int j=1;j<=150000;j++){\n\t\tans=min(ans,f[n][j]);\n\t}\n\tprintf(\"%0.10lf\\n\",ans);\n}", "accuracy": 1, "time_ms": 110, "memory_kb": 42848, "score_of_the_acc": -0.4814, "final_rank": 10 }, { "submission_id": "aoj_2305_10689531", "code_snippet": "#include <iostream>\n#include <algorithm>\n#include <stdio.h>\n#include <string.h>\n#include <stdlib.h>\n#include <math.h>\n#include <string>\n#include <vector>\n#include <queue>\n#include <stack>\n#include <map>\n#include <set>\n\nusing namespace std;\n\nconst int maxn = 22;\nconst int maxm = 111111;\ndouble dp[maxn][maxm];\nint a[maxn];\n\ndouble rat (int x, int y) {\n\treturn 1.0 * abs (x - y) / x;\n}\n\nint main () {\n\tint n, i, j, k;\n\tdouble ans, Min;\n\twhile (~scanf (\"%d\", &n)) {\n\t\tfor (i = 1; i <= n; ++i) scanf (\"%d\", &a[i]);\n\t\tmemset (dp, 0x7f, sizeof (dp));\n\t\tfor (i = 1; i < maxm; ++i) {\n\t\t\tdp[1][i] = rat(a[1], i);\n\t\t}\n\t\tfor (i = 2; i <= n; ++i) {\n\t\t\t//Min = dp[0][0];\n\t\t\tfor (j = 1; j < maxm; ++j) {\n\t\t\t\tfor (k = 1; k * j < maxm; ++k) {\n\t\t\t\t\tdp[i][k * j] = min (dp[i][k * j], max(dp[i-1][j], rat (a[i], k * j)));\n\t\t\t\t\t//printf (\"%lf %lf %lf\\n\", dp[i][k * j], dp[i-1][j], rat (a[i], k * j));\n\t\t\t\t\t//Min = min (Min, dp[i][k * j]);\n\t\t\t\t}\n\t\t\t}\n\t\t\t//printf (\"Min: %lf\\n\", Min);\n\t\t}\n\t\tans = dp[n][1];\n\t\tfor (j = 2; j < maxm; ++j) {\n\t\t\tans = min (ans, dp[n][j]);\n\t\t}\n\t\tprintf (\"%.12lf\\n\", ans);\n\t}\n\treturn 0;\n}", "accuracy": 0.7254901960784313, "time_ms": 30, "memory_kb": 22680, "score_of_the_acc": -0.1963, "final_rank": 16 }, { "submission_id": "aoj_2305_10689514", "code_snippet": "#include<iostream>\n#include<cstdio>\n#include<cstring>\n#include<cmath>\nusing namespace std;\n\ndouble dp[25][120005];\nint num[25];\nconst double INF = 1<<29;\n\nint main() {\n\tint n;\n\tscanf(\"%d\", &n);\n\tint maxnum = 0;\n\tfor (int i = 1; i <= n; i++) {\n\t\tscanf(\"%d\", &num[i]);\n\t}\n\tdouble ans = INF;\n\tfor (int i = 1; i <= n; i++) {\n\t\tfor (int j = 1; j < 120005; j++) {\n\t\t\tint m = sqrt((double)j);\n\t\t\tdp[i][j] = INF;\n\t\t\tfor (int x = 1; x <= m; x++) {\n\t\t\t\tif (j % x == 0) {\n\t\t\t\t\tdp[i][j] = min(max(dp[i-1][x], abs((double)(j-num[i]))/num[i]), dp[i][j]);\n\t\t\t\t\tdp[i][j] = min(max(dp[i-1][j/x], abs((double)(j-num[i]))/num[i]), dp[i][j]);\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (i == n)\n\t\t\t\tans = min(ans, dp[i][j]);\n\t\t}\n\t}\n\tprintf(\"%.12lf\\n\", ans);\n\treturn 0;\n}", "accuracy": 0.7254901960784313, "time_ms": 1070, "memory_kb": 23072, "score_of_the_acc": -1.1908, "final_rank": 17 }, { "submission_id": "aoj_2305_10000296", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define all(v) (v).begin(),(v).end()\n#define pb(a) push_back(a)\n#define rep(i, n) for(int i=0;i<n;i++)\n#define foa(e, v) for(auto&& e : v)\nusing ll = long long;\nconst ll MOD7 = 1000000007, MOD998 = 998244353, INF = (1LL << 60);\n#define dout(a) cout<<fixed<<setprecision(10)<<a<<endl;\n\nusing ld = long double;\nvoid chmin(ld &a, ld b) {a = min(a, b);}\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n int n;\n cin >> n;\n vector<int> a(n);\n rep(i, n) cin >> a[i];\n \n int m = 300000;\n vector<ld> dp(m, (ld) 1);\n for(int i = 1; i < m; i ++) {\n int diff = abs(i - a[0]);\n dp[i] = ld(diff) / ld(a[0]);\n }\n for(int i = 1; i < n; i ++) {\n vector<ld> nx(m, (ld) 1);\n for(int j = 1; j < m; j ++) {\n for(int k = j; k < m; k += j) {\n int diff = abs(k - a[i]);\n chmin(nx[k], max(dp[j], ld(diff) / ld(a[i])));\n }\n }\n swap(nx, dp);\n }\n dout(*min_element(all(dp)));\n\n return 0;\n}", "accuracy": 1, "time_ms": 250, "memory_kb": 12600, "score_of_the_acc": -0.3014, "final_rank": 6 }, { "submission_id": "aoj_2305_9730237", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n/**\n * @brief template\n */\n\nint main() {\n int N;\n cin >> N;\n vector<int> A(N);\n rep(i,0,N) cin >> A[i];\n int MAXS = 160000;\n vector<double> DP(MAXS,inf);\n auto CalcCost = [&](int X, int Y) -> double {\n return (double)abs(X-Y) / (double)X;\n };\n rep(i,1,MAXS) DP[i] = CalcCost(A[0],i);\n rep(i,1,N) {\n vector<double> NewDP(MAXS,inf);\n rep(j,1,MAXS) {\n rep(k,1,MAXS) {\n if (j*k >= MAXS) break;\n chmin(NewDP[j*k],max(DP[j],CalcCost(A[i],j*k)));\n }\n }\n swap(NewDP,DP);\n }\n double ANS = inf;\n rep(i,1,MAXS) chmin(ANS,DP[i]);\n printf(\"%.12f\\n\", ANS);\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 6040, "score_of_the_acc": -0.0715, "final_rank": 3 }, { "submission_id": "aoj_2305_9433950", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntypedef long double ld;\ntypedef vector<ll> vi;\ntypedef vector<vi> vvi;\ntypedef vector<vvi> vvvi;\ntypedef vector<bool> vb;\ntypedef vector<vb> vvb;\ntypedef vector<vvb> vvvb;\ntypedef vector<vvvb> vvvvb;\ntypedef pair<ll,ll> pi;\ntypedef pair<ll,pi> ppi;\n#define FOR(i,l,r) for(ll i=l;i<r;i++)\n#define REP(i,n) FOR(i,0,n)\n#define RFOR(i,l,r) for(ll i=r-1;i>=l;i--)\n#define RREP(i,n) RFOR(i,0,n)\n#define sz(A) (ll)(A.size())\n#define ALL(A) A.begin(),A.end()\n#define LB(A,x) (ll)(lower_bound(ALL(A),x)-A.begin())\n#define UB(A,x) (ll)(upper_bound(ALL(A),x)-A.begin())\n#define COU(A,x) (UB(A,x)-LB(A,x))\n#define F first\n#define S second\ntemplate<typename T>using min_priority_queue=priority_queue<T,vector<T>,greater<T>>;\ntemplate<typename T1,typename T2>ostream&operator<<(ostream&os,pair<T1,T2>&p){os<<p.F<<\" \"<<p.S;return os;}\ntemplate<typename T1,typename T2>istream&operator>>(istream&is,pair<T1,T2>&p){is>>p.F>>p.S;return is;}\ntemplate<typename T>ostream&operator<<(ostream&os,vector<T>&v){REP(i,sz(v))os<<v[i]<<(i+1!=sz(v)?\" \":\"\");return os;}\ntemplate<typename T>istream&operator>>(istream&is,vector<T>&v){for(T&in:v)is>>in;return is;}\ntemplate<class T>bool chmax(T&a,T b){if(a<b){a=b;return 1;}return 0;}\ntemplate<class T>bool chmin(T&a,T b){if(b<a){a=b;return 1;}return 0;}\nconst ll mod=998244353;\nint main(){\n while(1){\n ll N;cin>>N;\n vi A(N);cin>>A;\n //0.5\n vector<vector<ld>>DP(N,vector<ld>(2e5,1e18));\n FOR(i,1,2e5)DP[0][i]=abs(1-(ld)i/A[0]);\n FOR(i,1,N)FOR(j,1,2e5)for(ll k=j;k<2e5;k+=j){\n chmin(DP[i][k],max(DP[i-1][j],abs(1-(ld)k/A[i])));\n }\n ld ans=1e18;\n REP(i,2e5)chmin(ans,DP[N-1][i]);\n printf(\"%.20Lf\\n\",ans);\n return 0;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 190, "memory_kb": 68720, "score_of_the_acc": -0.8256, "final_rank": 13 }, { "submission_id": "aoj_2305_9003846", "code_snippet": "#line 2 \"cp-library/src/cp-template.hpp\"\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nusing i32 = int;\nusing u32 = unsigned int;\nusing i64 = long long;\nusing u64 = unsigned long long;\nusing i128 = __int128_t;\ntemplate < class T > bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }\ntemplate < class T > bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }\ntemplate < class T, class U > T ceil (T x, U y) { return (x > 0 ? (x + y - 1) / y : x / y); }\ntemplate < class T, class U > T floor(T x, U y) { return (x > 0 ? x / y : (x - y + 1) / y); }\nint popcnt(i32 x) { return __builtin_popcount(x); }\nint popcnt(u32 x) { return __builtin_popcount(x); }\nint popcnt(i64 x) { return __builtin_popcountll(x); }\nint popcnt(u64 x) { return __builtin_popcountll(x); }\n\n#line 2 \"cp-library/src/utility/rep_itr.hpp\"\ntemplate < class T > struct itr_rep {\n T i, d;\n constexpr itr_rep(const T i) noexcept : i(i), d(1) {}\n constexpr itr_rep(const T i, const T d) noexcept : i(i), d(d) {}\n void operator++() noexcept { i += d; }\n constexpr int operator*() const noexcept { return i; }\n constexpr bool operator!=(const itr_rep x) const noexcept { return d > 0 ? i < x.i : i > x.i; }\n};\n\ntemplate < class T > struct rep {\n const itr_rep< T > s, t;\n constexpr rep(const T t) noexcept : s(0), t(t) {}\n constexpr rep(const T s, const T t) noexcept : s(s), t(t) {}\n constexpr rep(const T s, const T t, const T d) noexcept : s(s, d), t(t, d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n\ntemplate < class T > struct revrep {\n const itr_rep < T > s, t;\n constexpr revrep(const T t) noexcept : s(t - 1, -1), t(-1, -1) {}\n constexpr revrep(const T s, const T t) noexcept : s(t - 1, -1), t(s - 1, -1) {}\n constexpr revrep(const T s, const T t, const T d) noexcept : s(t - 1, -d), t(s - 1, -d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n#line 3 \"cp-library/src/utility/io.hpp\"\n\n/* 128bit integer */\nistream& operator>>(istream& is, i128& x) {\n std::string s; is >> s;\n int pm = (s[0] == '-');\n x = 0;\n for(int i : rep(pm, int(s.size()))) x = x * 10 + (s[i] - '0');\n if(pm) x *= -1;\n return is;\n}\nostream& operator<<(ostream& os, const i128& x) {\n if(x == 0) return os << '0';\n i128 y = x;\n if(y < 0) { os << '-'; y *= -1; }\n std::vector<int> ny;\n while(y > 0) { ny.push_back(y % 10); y /= 10; }\n for(int i : revrep(ny.size())) os << ny[i];\n return os;\n}\n\ntemplate < class S, class T > istream& operator>>(istream& is, std::pair< S, T >& x) { is >> x.first >> x.second; return is; }\ntemplate < class S, class T > ostream& operator<<(ostream& os, const std::pair< S, T >& x) { os << x.first << \" \" << x.second; return os; }\n\nnamespace scanner {\n struct sca {\n template < class T > operator T() {\n T s; std::cin >> s; return s;\n }\n };\n struct vec {\n int n;\n vec(int n) : n(n) {}\n template < class T > operator std::vector< T >() {\n std::vector< T > v(n);\n for(T& x : v) std::cin >> x;\n return v;\n }\n };\n struct mat {\n int h, w;\n mat(int h, int w) : h(h), w(w) {}\n template < class T > operator std::vector< std::vector< T > >() {\n std::vector m(h, std::vector< T >(w));\n for(std::vector< T >& v : m) for(T& x : v) std::cin >> x;\n return m;\n }\n };\n struct speedup {\n speedup() {\n std::cin.tie(0);\n std::ios::sync_with_stdio(0);\n }\n } speedup_instance;\n}\nscanner::sca in() { return scanner::sca(); }\nscanner::vec in(int n) { return scanner::vec(n); }\nscanner::mat in(int h, int w) { return scanner::mat(h, w); }\n\nnamespace printer {\n void precision(int d) { std::cout << std::fixed << std::setprecision(d); }\n void flush() { std::cout.flush(); }\n}\n\ntemplate < class T >\nostream& operator<<(ostream& os, const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) { os << a[i]; if(i != n - 1) os << ' '; }\n return os;\n}\n\nint print() { std::cout << '\\n'; return 0; }\ntemplate < class head, class... tail > int print(head&& h, tail&&... t) {\n std::cout << h; if(sizeof...(tail)) std::cout << ' ';\n return print(std::forward<tail>(t)...);\n}\ntemplate < class T > int print_n(const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) std::cout << a[i] << \"\\n\";\n return 0;\n}\n\n\n#line 2 \"cp-library/src/utility/key_val.hpp\"\n\ntemplate < class K, class V >\nstruct key_val {\n K key; V val;\n key_val() {}\n key_val(K key, V val) : key(key), val(val) {}\n template < std::size_t Index >\n std::tuple_element_t< Index, key_val >& get() {\n if constexpr (Index == 0) return key;\n if constexpr (Index == 1) return val;\n }\n};\n\nnamespace std {\n\ntemplate < class K, class V > struct tuple_size < key_val< K, V > > : integral_constant< size_t, 2 > {};\ntemplate < class K, class V > struct tuple_element < 0, key_val< K, V > > { using type = K; };\ntemplate < class K, class V > struct tuple_element < 1, key_val< K, V > > { using type = V; };\n\n}\n#line 2 \"cp-library/src/utility/vec_op.hpp\"\ntemplate < class T > key_val< int, T > max_of(const vector< T >& a) {\n int i = std::max_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class T > key_val< int, T > min_of(const vector< T >& a) {\n int i = std::min_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class S, class T > S sum_of(const vector< T >& a) {\n S sum = 0;\n for(const T x : a) sum += x;\n return sum;\n}\ntemplate < class S, class T > vector< S > freq_of(const vector< T >& a, T L, T R) {\n vector< S > res(R - L, S(0));\n for(const T x : a) res[x - L] += 1;\n return res;\n}\ntemplate < class S, class T > struct prefix_sum {\n vector< S > s;\n prefix_sum(const vector< T >& a) : s(a) {\n s.insert(s.begin(), S(0));\n for(int i : rep(a.size())) s[i + 1] += s[i];\n }\n // [L, R)\n S sum(int L, int R) { return s[R] - s[L]; }\n};\n#line 3 \"cp-library/src/utility/heap.hpp\"\n\ntemplate < class T > using heap_min = std::priority_queue< T, std::vector< T >, std::greater< T > >;\ntemplate < class T > using heap_max = std::priority_queue< T, std::vector< T >, std::less< T > >;\n\n#line 27 \"cp-library/src/cp-template.hpp\"\n\n#line 1 \"cp-library/src/algorithm/bin_search.hpp\"\ntemplate < class T, class F >\nT bin_search(T ok, T ng, F f) {\n while(abs(ng - ok) > 1) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n\ntemplate < class T, class F >\nT bin_search_real(T ok, T ng, F f, int step = 80) {\n while(step--) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n#line 2 \"cp-library/src/algorithm/argsort.hpp\"\n\ntemplate < class T > std::vector< int > argsort(const std::vector< T > &a) {\n std::vector< int > ids((int)a.size());\n std::iota(ids.begin(), ids.end(), 0);\n std::sort(ids.begin(), ids.end(), [&](int i, int j) {\n return a[i] < a[j] || (a[i] == a[j] && i < j);\n });\n return ids;\n}\n#line 1 \"macro.hpp\"\nnamespace macro {\n\nusing size_type = int;\ntemplate < class container > void sort(container& a) { std::sort(std:: begin(a), std:: end(a)); }\ntemplate < class container > void rsort(container& a) { std::sort(std::rbegin(a), std::rend(a)); }\ntemplate < class container > void reverse(container& a) { std::reverse(std::begin(a), std::end(a)); }\ntemplate < class container > void unique(container& a) {\n std::sort(std::begin(a), std::end(a));\n a.erase(std::unique(std::begin(a), std::end(a)), std::end(a));\n}\ntemplate < class container > container sorted(const container& a) { container b = a; sort(b); return std::move(b); }\ntemplate < class container > container rsorted(const container& a) { container b = a; rsort(b); return std::move(b); }\ntemplate < class container, class compare > void sort(container& a, const compare& cmp) { std::sort(std::begin(a), std::end(a), cmp); }\ntemplate < class container, class compare > container sorted(const container& a, const compare& cmp) { container b = a; sort(b, cmp); return std::move(b); }\ntemplate < class container, class value > size_type lower_bound(const container& a, const value& x) { return std::lower_bound(std::begin(a), std::end(a), x) - std::begin(a); }\ntemplate < class container, class value > size_type upper_bound(const container& a, const value& x) { return std::upper_bound(std::begin(a), std::end(a), x) - std::begin(a); }\n\nconst std::vector<std::pair<size_type, size_type>> dir4 = { {+1, 0}, {-1, 0}, { 0, +1}, { 0, -1} };\nconst std::vector<std::pair<size_type, size_type>> dir8 = { {-1, -1}, {-1, 0}, {-1, +1}, { 0, -1}, { 0, +1}, {+1, -1}, {+1, 0}, {+1, +1} };\n\n#ifdef _DEBUG\n#define debug(x) std::cout << \"[\" << __LINE__ << \"] \" << #x << \": \" << x << std::endl\n#else\n#define debug(x)\n#endif\n\ntemplate < class container > void concat(container& a, const container& b) {\n a.insert(std::end(a), std::begin(b), std::end(b));\n}\nstd::vector<size_type> iota(const size_type n) {\n std::vector<size_type> I(n);\n std::iota(std::begin(I), std::end(I), 0);\n return I;\n}\ntemplate < class container > std::vector<size_type> sort_idx(const container& a) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return a[i] < a[j] or (a[i] == a[j] and i < j); });\n return I;\n}\ntemplate < class container, class compare > std::vector<size_type> sort_idx(const container& a, const compare& cmp) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return cmp(a[i], a[j]) or (a[i] == a[j] and i < j); });\n return std::move(I);\n}\n\nstruct grid {\n using size_type = int;\n size_type H, W;\n grid(const size_type H, const size_type W) : H(H), W(W) {}\n bool contains(const size_type i, const size_type j) {\n return 0 <= i and i < H and 0 <= j and j < W;\n }\n};\n\nusing f64 = long double;\n\n} // namespace macro\n\nusing namespace macro;\n#line 3 \"A.cpp\"\n\nint main() {\n int N = in();\n vector<int> a = in(N);\n \n const int MAX_A = 100'000 * 2;\n const f64 INF = 1e18;\n vector<f64> dp(MAX_A + 1, INF);\n dp[1] = 0.0;\n for(int i : rep(N)) {\n vector<f64> nt(MAX_A + 1, INF);\n for(int prev = 1; prev <= MAX_A; prev++) {\n for(int x = prev; x <= MAX_A; x += prev) {\n chmin(nt[x], max(dp[prev], f64(abs(x - a[i])) / a[i]));\n }\n }\n dp = move(nt);\n }\n\n printer::precision(20);\n print(min_of<f64>(dp).val);\n}", "accuracy": 1, "time_ms": 200, "memory_kb": 9460, "score_of_the_acc": -0.2212, "final_rank": 5 }, { "submission_id": "aoj_2305_9003842", "code_snippet": "#line 2 \"cp-library/src/cp-template.hpp\"\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nusing i32 = int;\nusing u32 = unsigned int;\nusing i64 = long long;\nusing u64 = unsigned long long;\nusing i128 = __int128_t;\ntemplate < class T > bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }\ntemplate < class T > bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }\ntemplate < class T, class U > T ceil (T x, U y) { return (x > 0 ? (x + y - 1) / y : x / y); }\ntemplate < class T, class U > T floor(T x, U y) { return (x > 0 ? x / y : (x - y + 1) / y); }\nint popcnt(i32 x) { return __builtin_popcount(x); }\nint popcnt(u32 x) { return __builtin_popcount(x); }\nint popcnt(i64 x) { return __builtin_popcountll(x); }\nint popcnt(u64 x) { return __builtin_popcountll(x); }\n\n#line 2 \"cp-library/src/utility/rep_itr.hpp\"\ntemplate < class T > struct itr_rep {\n T i, d;\n constexpr itr_rep(const T i) noexcept : i(i), d(1) {}\n constexpr itr_rep(const T i, const T d) noexcept : i(i), d(d) {}\n void operator++() noexcept { i += d; }\n constexpr int operator*() const noexcept { return i; }\n constexpr bool operator!=(const itr_rep x) const noexcept { return d > 0 ? i < x.i : i > x.i; }\n};\n\ntemplate < class T > struct rep {\n const itr_rep< T > s, t;\n constexpr rep(const T t) noexcept : s(0), t(t) {}\n constexpr rep(const T s, const T t) noexcept : s(s), t(t) {}\n constexpr rep(const T s, const T t, const T d) noexcept : s(s, d), t(t, d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n\ntemplate < class T > struct revrep {\n const itr_rep < T > s, t;\n constexpr revrep(const T t) noexcept : s(t - 1, -1), t(-1, -1) {}\n constexpr revrep(const T s, const T t) noexcept : s(t - 1, -1), t(s - 1, -1) {}\n constexpr revrep(const T s, const T t, const T d) noexcept : s(t - 1, -d), t(s - 1, -d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n#line 3 \"cp-library/src/utility/io.hpp\"\n\n/* 128bit integer */\nistream& operator>>(istream& is, i128& x) {\n std::string s; is >> s;\n int pm = (s[0] == '-');\n x = 0;\n for(int i : rep(pm, int(s.size()))) x = x * 10 + (s[i] - '0');\n if(pm) x *= -1;\n return is;\n}\nostream& operator<<(ostream& os, const i128& x) {\n if(x == 0) return os << '0';\n i128 y = x;\n if(y < 0) { os << '-'; y *= -1; }\n std::vector<int> ny;\n while(y > 0) { ny.push_back(y % 10); y /= 10; }\n for(int i : revrep(ny.size())) os << ny[i];\n return os;\n}\n\ntemplate < class S, class T > istream& operator>>(istream& is, std::pair< S, T >& x) { is >> x.first >> x.second; return is; }\ntemplate < class S, class T > ostream& operator<<(ostream& os, const std::pair< S, T >& x) { os << x.first << \" \" << x.second; return os; }\n\nnamespace scanner {\n struct sca {\n template < class T > operator T() {\n T s; std::cin >> s; return s;\n }\n };\n struct vec {\n int n;\n vec(int n) : n(n) {}\n template < class T > operator std::vector< T >() {\n std::vector< T > v(n);\n for(T& x : v) std::cin >> x;\n return v;\n }\n };\n struct mat {\n int h, w;\n mat(int h, int w) : h(h), w(w) {}\n template < class T > operator std::vector< std::vector< T > >() {\n std::vector m(h, std::vector< T >(w));\n for(std::vector< T >& v : m) for(T& x : v) std::cin >> x;\n return m;\n }\n };\n struct speedup {\n speedup() {\n std::cin.tie(0);\n std::ios::sync_with_stdio(0);\n }\n } speedup_instance;\n}\nscanner::sca in() { return scanner::sca(); }\nscanner::vec in(int n) { return scanner::vec(n); }\nscanner::mat in(int h, int w) { return scanner::mat(h, w); }\n\nnamespace printer {\n void precision(int d) { std::cout << std::fixed << std::setprecision(d); }\n void flush() { std::cout.flush(); }\n}\n\ntemplate < class T >\nostream& operator<<(ostream& os, const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) { os << a[i]; if(i != n - 1) os << ' '; }\n return os;\n}\n\nint print() { std::cout << '\\n'; return 0; }\ntemplate < class head, class... tail > int print(head&& h, tail&&... t) {\n std::cout << h; if(sizeof...(tail)) std::cout << ' ';\n return print(std::forward<tail>(t)...);\n}\ntemplate < class T > int print_n(const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) std::cout << a[i] << \"\\n\";\n return 0;\n}\n\n\n#line 2 \"cp-library/src/utility/key_val.hpp\"\n\ntemplate < class K, class V >\nstruct key_val {\n K key; V val;\n key_val() {}\n key_val(K key, V val) : key(key), val(val) {}\n template < std::size_t Index >\n std::tuple_element_t< Index, key_val >& get() {\n if constexpr (Index == 0) return key;\n if constexpr (Index == 1) return val;\n }\n};\n\nnamespace std {\n\ntemplate < class K, class V > struct tuple_size < key_val< K, V > > : integral_constant< size_t, 2 > {};\ntemplate < class K, class V > struct tuple_element < 0, key_val< K, V > > { using type = K; };\ntemplate < class K, class V > struct tuple_element < 1, key_val< K, V > > { using type = V; };\n\n}\n#line 2 \"cp-library/src/utility/vec_op.hpp\"\ntemplate < class T > key_val< int, T > max_of(const vector< T >& a) {\n int i = std::max_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class T > key_val< int, T > min_of(const vector< T >& a) {\n int i = std::min_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class S, class T > S sum_of(const vector< T >& a) {\n S sum = 0;\n for(const T x : a) sum += x;\n return sum;\n}\ntemplate < class S, class T > vector< S > freq_of(const vector< T >& a, T L, T R) {\n vector< S > res(R - L, S(0));\n for(const T x : a) res[x - L] += 1;\n return res;\n}\ntemplate < class S, class T > struct prefix_sum {\n vector< S > s;\n prefix_sum(const vector< T >& a) : s(a) {\n s.insert(s.begin(), S(0));\n for(int i : rep(a.size())) s[i + 1] += s[i];\n }\n // [L, R)\n S sum(int L, int R) { return s[R] - s[L]; }\n};\n#line 3 \"cp-library/src/utility/heap.hpp\"\n\ntemplate < class T > using heap_min = std::priority_queue< T, std::vector< T >, std::greater< T > >;\ntemplate < class T > using heap_max = std::priority_queue< T, std::vector< T >, std::less< T > >;\n\n#line 27 \"cp-library/src/cp-template.hpp\"\n\n#line 1 \"cp-library/src/algorithm/bin_search.hpp\"\ntemplate < class T, class F >\nT bin_search(T ok, T ng, F f) {\n while(abs(ng - ok) > 1) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n\ntemplate < class T, class F >\nT bin_search_real(T ok, T ng, F f, int step = 80) {\n while(step--) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n#line 2 \"cp-library/src/algorithm/argsort.hpp\"\n\ntemplate < class T > std::vector< int > argsort(const std::vector< T > &a) {\n std::vector< int > ids((int)a.size());\n std::iota(ids.begin(), ids.end(), 0);\n std::sort(ids.begin(), ids.end(), [&](int i, int j) {\n return a[i] < a[j] || (a[i] == a[j] && i < j);\n });\n return ids;\n}\n#line 1 \"macro.hpp\"\nnamespace macro {\n\nusing size_type = int;\ntemplate < class container > void sort(container& a) { std::sort(std:: begin(a), std:: end(a)); }\ntemplate < class container > void rsort(container& a) { std::sort(std::rbegin(a), std::rend(a)); }\ntemplate < class container > void reverse(container& a) { std::reverse(std::begin(a), std::end(a)); }\ntemplate < class container > void unique(container& a) {\n std::sort(std::begin(a), std::end(a));\n a.erase(std::unique(std::begin(a), std::end(a)), std::end(a));\n}\ntemplate < class container > container sorted(const container& a) { container b = a; sort(b); return std::move(b); }\ntemplate < class container > container rsorted(const container& a) { container b = a; rsort(b); return std::move(b); }\ntemplate < class container, class compare > void sort(container& a, const compare& cmp) { std::sort(std::begin(a), std::end(a), cmp); }\ntemplate < class container, class compare > container sorted(const container& a, const compare& cmp) { container b = a; sort(b, cmp); return std::move(b); }\ntemplate < class container, class value > size_type lower_bound(const container& a, const value& x) { return std::lower_bound(std::begin(a), std::end(a), x) - std::begin(a); }\ntemplate < class container, class value > size_type upper_bound(const container& a, const value& x) { return std::upper_bound(std::begin(a), std::end(a), x) - std::begin(a); }\n\nconst std::vector<std::pair<size_type, size_type>> dir4 = { {+1, 0}, {-1, 0}, { 0, +1}, { 0, -1} };\nconst std::vector<std::pair<size_type, size_type>> dir8 = { {-1, -1}, {-1, 0}, {-1, +1}, { 0, -1}, { 0, +1}, {+1, -1}, {+1, 0}, {+1, +1} };\n\n#ifdef _DEBUG\n#define debug(x) std::cout << \"[\" << __LINE__ << \"] \" << #x << \": \" << x << std::endl\n#else\n#define debug(x)\n#endif\n\ntemplate < class container > void concat(container& a, const container& b) {\n a.insert(std::end(a), std::begin(b), std::end(b));\n}\nstd::vector<size_type> iota(const size_type n) {\n std::vector<size_type> I(n);\n std::iota(std::begin(I), std::end(I), 0);\n return I;\n}\ntemplate < class container > std::vector<size_type> sort_idx(const container& a) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return a[i] < a[j] or (a[i] == a[j] and i < j); });\n return I;\n}\ntemplate < class container, class compare > std::vector<size_type> sort_idx(const container& a, const compare& cmp) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return cmp(a[i], a[j]) or (a[i] == a[j] and i < j); });\n return std::move(I);\n}\n\nstruct grid {\n using size_type = int;\n size_type H, W;\n grid(const size_type H, const size_type W) : H(H), W(W) {}\n bool contains(const size_type i, const size_type j) {\n return 0 <= i and i < H and 0 <= j and j < W;\n }\n};\n\nusing f64 = long double;\n\n} // namespace macro\n\nusing namespace macro;\n#line 3 \"A.cpp\"\n\nint main() {\n int N = in();\n vector<int> a = in(N);\n \n const int MAX_A = 100'000;\n const f64 INF = 1e18;\n vector<f64> dp(MAX_A + 1, INF);\n dp[1] = 0.0;\n for(int i : rep(N)) {\n vector<f64> nt(MAX_A + 1, INF);\n for(int prev = 1; prev <= MAX_A; prev++) {\n for(int x = prev; x <= MAX_A; x += prev) {\n chmin(nt[x], max(dp[prev], abs(x - a[i]) / f64(a[i])));\n }\n }\n dp = move(nt);\n }\n\n printer::precision(20);\n print(min_of<f64>(dp).val);\n}", "accuracy": 0.6274509803921569, "time_ms": 80, "memory_kb": 6268, "score_of_the_acc": -0.0739, "final_rank": 19 }, { "submission_id": "aoj_2305_8304958", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\ntemplate<class T>\nbool chmax(T &a, const T &b) {if (a < b) {a = b; return 1;} return 0;}\n\ntemplate<class T>\nbool chmin(T &a, const T &b) {if (a > b) {a = b; return 1;} return 0;}\n\nusing ll = long long;\nusing ld = long double;\n\nbool is_end = false;\n\nconst int amax = 2e5 + 100;\nconst ld INF = 1e18;\nld dp[25][amax];\n\nvoid init()\n{\n\tfor (int i = 0; i < 25; ++i)\n\t{\n\t\tfor (int j = 0; j < amax; ++j)\n\t\t{\n\t\t\tdp[i][j] = INF;\n\t\t}\n\t}\n\treturn;\n}\n\nvoid calc()\n{\n\tint N; cin >> N;\n\tvector<int> A(N);\n\tfor (int i = 0; i < N; ++i) {cin >> A[i];}\n\t\n\tvector<vector<int>> divisor(amax);\n\tfor (int n = 1; n < amax; ++n)\n\t{\n\t\tfor (int a = 1; a * a <= n; ++a)\n\t\t{\n\t\t\tif (n % a == 0)\n\t\t\t{\n\t\t\t\tdivisor[n].emplace_back(a);\n\t\t\t\tdivisor[n].emplace_back(n/a);\n\t\t\t}\n\t\t}\n\t\tsort(divisor[n].begin(), divisor[n].end());\t\t\n\t}\n\tinit();\n\t\n\tfor (int i = 0; i < N; ++i)\n\t{\n\t\tint a = A[i];\n\t\tfor (int n = 1; n < amax; ++n)\n\t\t{\n\t\t\tld diff = (ld)abs(n - a) / a;\n\t\t\t\n\t\t\tif (i == 0) {dp[i][n] = diff; continue;}\n\t\t\t\n\t\t\tfor (auto d : divisor[n])\n\t\t\t{\n\t\t\t\tchmin(dp[i][n], max(dp[i-1][d], diff));\n\t\t\t}\n\t\t}\n\t}\n\t\n\tld res = INF;\n\tfor (int n = 1; n < amax; ++n)\n\t{\n\t\tchmin(res, dp[N-1][n]);\n\t}\n\tcout << fixed << setprecision(20) << res << endl;\n\t\n\treturn;\n}\n\nint main()\n{\n\tcalc();\n\t\n\treturn 0;\n}", "accuracy": 1, "time_ms": 300, "memory_kb": 101184, "score_of_the_acc": -1.2667, "final_rank": 15 }, { "submission_id": "aoj_2305_7104164", "code_snippet": "#pragma GCC target(\"avx2\")\n#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"unroll-loops\")\n#include<bits/stdc++.h>\n// #include<atcoder/mincostflow>\nusing namespace std;\n// using namespace atcoder;\n\nvoid solve(){\n\tcout << fixed << setprecision(12);\n\tint n;\n\tcin >> n;\n\tvector<long double> A(n);\n\tfor(int i = 0; i < n; i++) cin >> A[i];\n\tconst int N = 200000;\n\tvector<long double> dp(N);\n\tfor(int i = 1; i < N; i++) dp[i] = abs(A[0] - i) / A[0];\n\t\n\tfor(int i = 1; i < n; i++){\n\t\tlong double a = A[i];\n\t\tvector<long double> nex(N, 1e9);\n\t\tfor(int j = 1; j < N; j++){\n\t\t\tfor(int k = j; k < N; k += j){\n\t\t\t\tnex[k] = min(nex[k], dp[j]);\n\t\t\t}\n\t\t\tnex[j] = max(nex[j], abs(a - j) / a);\n\t\t}\n\t\tswap(dp, nex);\n\t}\n\tlong double ans = 1e9;\n\tfor(int i = 1; i < N; i++) ans = min(ans, dp[i]);\n\tcout << ans << \"\\n\";\n}\n\nint main(){\n\tcin.tie(0)->sync_with_stdio(0);\n\tint t;\n\tt = 1;\n\t// cin >> t;\n\twhile(t--) solve();\n}", "accuracy": 1, "time_ms": 110, "memory_kb": 9544, "score_of_the_acc": -0.1364, "final_rank": 4 }, { "submission_id": "aoj_2305_6444946", "code_snippet": "#line 1 \"a.cpp\"\n#define PROBLEM \"\"\n#line 2 \"/home/kuhaku/atcoder/github/atcoder-lib/lib/template/atcoder.hpp\"\n#pragma GCC target(\"avx\")\n#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"unroll-loops\")\n#line 2 \"/home/kuhaku/atcoder/github/atcoder-lib/lib/template/template.hpp\"\n#include <bits/stdc++.h>\nusing namespace std;\ntemplate <class T, class U>\nbool chmax(T &a, const U &b) {\n return a < b ? a = b, true : false;\n}\ntemplate <class T, class U>\nbool chmin(T &a, const U &b) {\n return b < a ? a = b, true : false;\n}\nconstexpr int64_t INF = 1000000000000000003;\nconstexpr int Inf = 1000000003;\nconstexpr int MOD = 1000000007;\nconstexpr int MOD_N = 998244353;\nconstexpr double EPS = 1e-7;\nconst double PI = acos(-1.0);\n#line 6 \"/home/kuhaku/atcoder/github/atcoder-lib/lib/template/atcoder.hpp\"\nusing ll = int64_t;\nusing ld = long double;\n#define FOR(i, m, n) for(int i = (m); i < int(n); ++i)\n#define FORR(i, m, n) for(int i = (m)-1; i >= int(n); --i)\n#define FORL(i, m, n) for(int64_t i = (m); i < int64_t(n); ++i)\n#define rep(i, n) FOR(i, 0, n)\n#define repn(i, n) FOR(i, 1, n+1)\n#define repr(i, n) FORR(i, n, 0)\n#define repnr(i, n) FORR(i, n+1, 1)\n#define all(s) (s).begin(), (s).end()\ntemplate<class T, class U>\nstd::istream &operator>>(std::istream &is, std::pair<T, U> &p) { is >> p.first >> p.second; return is; }\ntemplate <class T>\nstd::istream &operator>>(std::istream &is, std::vector<T> &v) { for (T &i : v) is>>i; return is; }\ntemplate <class T, class U>\nstd::ostream &operator<<(std::ostream &os, const std::pair<T, U> &p) {\n return os<<'('<<p.first<< ','<<p.second<<')';\n}\ntemplate <class T>\nstd::ostream &operator<<(std::ostream &os, const std::vector<T> &v) {\n for (auto it=v.begin(); it!=v.end(); ++it) { os<<(it==v.begin()?\"\":\" \")<<*it; } return os;\n}\ntemplate <class Head, class... Tail>\nvoid co(Head&& head, Tail&&... tail) {\n if constexpr(sizeof...(tail)==0) std::cout<<head<<'\\n'; else std::cout<<head<<' ',co(forward<Tail>(tail)...);\n}\ntemplate <class Head, class... Tail>\nvoid ce(Head&& head, Tail&&... tail) {\n if constexpr(sizeof...(tail)==0) std::cerr<<head<<'\\n'; else std::cerr<<head<<' ',ce(forward<Tail>(tail)...);\n}\ntemplate<typename T, typename... Args>\nauto make_vector(T x, int arg, Args ...args) {\n if constexpr(sizeof...(args)==0) return std::vector<T>(arg,x); else return std::vector(arg,make_vector<T>(x,args...));\n}\nvoid sonic() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); }\nvoid setp(const int n) { std::cout<<std::fixed<<std::setprecision(n); }\nvoid Yes(bool is_correct) { std::cout<<(is_correct?\"Yes\":\"No\")<<std::endl; }\nvoid YES(bool is_correct) { std::cout<<(is_correct?\"YES\":\"NO\")<<std::endl; }\n#line 3 \"a.cpp\"\n\nint main(void) {\n sonic();\n int n;\n cin >> n;\n vector<int> a(n);\n cin >> a;\n\n vector<vector<double>> dp(n, vector<double>(200001, Inf));\n repr(i, n) {\n repn(j, 200000) {\n if (i == n - 1)\n dp[i][j] = (double)abs(j - a[i]) / a[i];\n else {\n rep(k, 200001) {\n if (j * k > 200000)\n break;\n chmin(dp[i][j], dp[i + 1][j * k]);\n }\n chmax(dp[i][j], (double)abs(j - a[i]) / a[i]);\n }\n }\n }\n\n double ans = Inf;\n repn(i, 200000) chmin(ans, dp[0][i]);\n setp(10);\n co(ans);\n\n return 0;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 35712, "score_of_the_acc": -0.3503, "final_rank": 7 }, { "submission_id": "aoj_2305_6025370", "code_snippet": "#pragma region Macros\n#include <bits/stdc++.h>\nusing namespace std;\ntemplate <class T> inline bool chmax(T &a, T b) {\n if(a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <class T> inline bool chmin(T &a, T b) {\n if(a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\n#ifdef DEBUG\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << '(' << p.first << ',' << p.second << ')';\n return os;\n}\ntemplate <class T> ostream &operator<<(ostream &os, const vector<T> &v) {\n os << '{';\n for(int i = 0; i < (int)v.size(); i++) {\n if(i) { os << ','; }\n os << v[i];\n }\n os << '}';\n return os;\n}\nvoid debugg() { cerr << endl; }\ntemplate <class T, class... Args>\nvoid debugg(const T &x, const Args &... args) {\n cerr << \" \" << x;\n debugg(args...);\n}\n#define debug(...) \\\n cerr << __LINE__ << \" [\" << #__VA_ARGS__ << \"]: \", debugg(__VA_ARGS__)\n#define dump(x) cerr << __LINE__ << \" \" << #x << \" = \" << (x) << endl\n#else\n#define debug(...) (void(0))\n#define dump(x) (void(0))\n#endif\n\nstruct Setup {\n Setup() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(15);\n }\n} __Setup;\n\nusing ll = long long;\n#define OVERLOAD3(_1, _2, _3, name, ...) name\n#define ALL(v) (v).begin(), (v).end()\n#define RALL(v) (v).rbegin(), (v).rend()\n#define REP1(i, n) for(int i = 0; i < (n); i++)\n#define REP2(i, a, b) for(int i = (a); i < int(b); i++)\n#define REP(...) OVERLOAD3(__VA_ARGS__, REP2, REP1)(__VA_ARGS__)\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\nconst int INF = 1 << 30;\nconst ll LLINF = 1LL << 60;\nconstexpr int MOD = 1000000007;\nconstexpr int MOD2 = 998244353;\nconst int dx[4] = {1, 0, -1, 0};\nconst int dy[4] = {0, 1, 0, -1};\n\nvoid Case(int i) { cout << \"Case #\" << i << \": \"; }\nint popcount(int x) { return __builtin_popcount(x); }\nll popcount(ll x) { return __builtin_popcountll(x); }\n#pragma endregion Macros\n\nconst int MAX = 1000010;\ndouble dp[2][MAX];\nint N;\nint a[21];\n\nint main() {\n cin >> N;\n REP(i, N) cin >> a[i];\n\n REP(i, 2) REP(j, MAX) dp[i][j] = INF;\n int now = 0, nxt = 1;\n dp[now][1] = 0;\n REP(i, N) {\n REP(j, 1, MAX) {\n if(dp[now][j] == INF) continue;\n for(int k = j; k < MAX; k += j) {\n chmin(dp[nxt][k], max(dp[now][j], double(abs(a[i] - k)) / a[i]));\n }\n dp[now][j] = INF;\n }\n swap(now, nxt);\n }\n double ans = INF;\n REP(j, MAX) chmin(ans, dp[now][j]);\n cout << ans << endl;\n}", "accuracy": 1, "time_ms": 400, "memory_kb": 19236, "score_of_the_acc": -0.513, "final_rank": 11 }, { "submission_id": "aoj_2305_6012422", "code_snippet": "#include<bits/stdc++.h> \nusing namespace std;\nusing ll=long long int;\n//using Int=__int128;\n#define ALL(A) A.begin(),A.end()\ntemplate<typename T1,typename T2> bool chmin(T1 &a,T2 b){if(a<=b)return 0; a=b; return 1;}\ntemplate<typename T1,typename T2> bool chmax(T1 &a,T2 b){if(a>=b)return 0; a=b; return 1;}\ntemplate<typename T> constexpr int bitUP(T x,int a){return (x>>a)&1;}\n//→ ↓ ← ↑ \nint dh[4]={0,1,0,-1}, dw[4]={1,0,-1,0};\n//右上から時計回り\n//int dh[8]={-1,0,1,1,1,0,-1,-1}, dw[8]={1,1,1,0,-1,-1,-1,0};\nlong double EPS = 1e-6;\nlong double PI = acos(-1);\nconst ll INF=(1LL<<62);\nconst int MAX=(1<<30);\nconstexpr ll MOD=1000000000+7;\n//constexpr ll MOD=998244353;\n\n\ninline void bin101(){\n ios::sync_with_stdio(false);\n cin.tie(0);\n cout << fixed << setprecision(20);\n}\n\nusing Graph=vector<vector<int>>;\n\ntemplate<typename T >\nstruct edge {\n\tint to;\n\tT cost;\n\tedge()=default;\n\tedge(int to, T cost) : to(to), cost(cost) {}\n\n};\ntemplate<typename T>\nusing WeightGraph=vector<vector<edge<T>>>;\n\n//要素数n 初期値x\ntemplate<typename T>\ninline vector<T> vmake(size_t n,T x){\n\treturn vector<T>(n,x);\n}\n\n//a,b,c,x data[a][b][c] 初期値x\ntemplate<typename... Args>\nauto vmake(size_t n,Args... args){\n\tauto v=vmake(args...);\n\treturn vector<decltype(v)>(n,move(v));\n}\n\nvoid solve(){\n int N; cin>>N;\n vector<int> a(N);\n for(int i=0;i<N;i++) cin>>a[i];\n \n auto dp=vmake(N+1,2e5+10,(long double)MAX);\n dp[0][1]=0;\n\n for(int i=0;i<N;i++){\n for(int j=1;j<=2e5;j++){\n for(int k=j;k<=2e5;k+=j){\n long double x=abs(k-a[i]);\n x/=a[i];\n chmin(dp[i+1][k],max(dp[i][j],x));\n }\n }\n }\n long double ans=INF;\n for(auto x:dp[N]) chmin(ans,x);\n cout<<ans<<endl;\n}\n\nint main(){\n bin101();\n int T=1;\n //cin>>T;\n while(T--) solve();\n}", "accuracy": 1, "time_ms": 210, "memory_kb": 71868, "score_of_the_acc": -0.8773, "final_rank": 14 }, { "submission_id": "aoj_2305_6012421", "code_snippet": "#include<bits/stdc++.h> \nusing namespace std;\nusing ll=long long int;\n//using Int=__int128;\n#define ALL(A) A.begin(),A.end()\ntemplate<typename T1,typename T2> bool chmin(T1 &a,T2 b){if(a<=b)return 0; a=b; return 1;}\ntemplate<typename T1,typename T2> bool chmax(T1 &a,T2 b){if(a>=b)return 0; a=b; return 1;}\ntemplate<typename T> constexpr int bitUP(T x,int a){return (x>>a)&1;}\n//→ ↓ ← ↑ \nint dh[4]={0,1,0,-1}, dw[4]={1,0,-1,0};\n//右上から時計回り\n//int dh[8]={-1,0,1,1,1,0,-1,-1}, dw[8]={1,1,1,0,-1,-1,-1,0};\nlong double EPS = 1e-6;\nlong double PI = acos(-1);\nconst ll INF=(1LL<<62);\nconst int MAX=(1<<30);\nconstexpr ll MOD=1000000000+7;\n//constexpr ll MOD=998244353;\n\n\ninline void bin101(){\n ios::sync_with_stdio(false);\n cin.tie(0);\n cout << fixed << setprecision(20);\n}\n\nusing Graph=vector<vector<int>>;\n\ntemplate<typename T >\nstruct edge {\n\tint to;\n\tT cost;\n\tedge()=default;\n\tedge(int to, T cost) : to(to), cost(cost) {}\n\n};\ntemplate<typename T>\nusing WeightGraph=vector<vector<edge<T>>>;\n\n//要素数n 初期値x\ntemplate<typename T>\ninline vector<T> vmake(size_t n,T x){\n\treturn vector<T>(n,x);\n}\n\n//a,b,c,x data[a][b][c] 初期値x\ntemplate<typename... Args>\nauto vmake(size_t n,Args... args){\n\tauto v=vmake(args...);\n\treturn vector<decltype(v)>(n,move(v));\n}\n\nvoid solve(){\n int N; cin>>N;\n vector<int> a(N);\n for(int i=0;i<N;i++) cin>>a[i];\n \n auto dp=vmake(N+1,2e5+10,(long double)MAX);\n dp[0][1]=0;\n\n for(int i=0;i<N;i++){\n for(int j=1;j<=2e5;j++){\n for(int k=j;k<=2e5;k+=j){\n long double x=abs(k-a[i]);\n x/=a[i];\n chmin(dp[i+1][k],max(dp[i][j],x));\n }\n }\n }\n long double ans=INF;\n for(auto x:dp[N]) if(x>EPS)chmin(ans,x);\n cout<<ans<<endl;\n}\n\nint main(){\n bin101();\n int T=1;\n //cin>>T;\n while(T--) solve();\n}", "accuracy": 0.058823529411764705, "time_ms": 30, "memory_kb": 18708, "score_of_the_acc": -0.1551, "final_rank": 20 }, { "submission_id": "aoj_2305_6011745", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define all(A) A.begin(),A.end()\nusing vll = vector<ll>;\n#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)\n\nvector<vll> Q = { {1,2,5,6},{2,3,6,7},{3,4,7,8},{5,6,9,10},{6,7,10,11},{7,8,11,12},{9,10,13,14},{10,11,14,15},{11,12,15,16} };\nvector<vll> T = { {0,1,2,3,6},{0,1,2,4,7},{0,1,2,5,8},{3,4,5,0,6},{3,4,5,1,7},{3,4,5,2,8},{6,7,8,0,3},{6,7,8,1,4},{6,7,8,2,5} };\n\ndouble di(pair<double, double> P1, pair<double, double> P2) {\n\tdouble dx = P1.first - P2.first;\n\tdouble dy = P1.second - P2.second;\n\treturn sqrt(dx * dx + dy * dy);\n}\n\n/*bool kousa(pair<double, double> P1, pair<double, double> P2, pair<double, double> Q1, pair<double, double> Q2) {\n\tdouble xa = P1.first;\n\tdouble xb = P2.first;\n\tdouble ya = P1.second;\n\tdouble yb = P2.second;\n\tdouble xc = Q1.first;\n\tdouble xd = Q2.first;\n\tdouble yc = Q1.second;\n\tdouble yd = Q2.second;\n\tdouble s = (xa - xb) * (yc - ya) - (ya - yb) * (xc - xa);\n\tdouble t = (xa - xb) * (yd - ya) - (ya - yb) * (xd - xa);\n\treturn (s*t>=0);\n\n}*/\n\n\n\n\n\nint main() {\n\tll N;\n\tcin >> N;\n\tvll A(N);\n\trep(i, N) {\n\t\tcin >> A[i];\n\t}\n\tvector<vector<double>> DP(N + 1, vector<double>(ll(3e5), 1e18));\n\tDP[0][1] = 0;\n\trep(i, N) {\n\t\trep(k, 3e5) {\n\t\t\tif (k == 0)continue;\n\t\t\tfor (ll j = k; j < 3e5; j += k) {\n\t\t\t\tdouble p = double(abs(A[i] - j)) / double(A[i]);\n\t\t\t\tDP[i + 1][j] = min(DP[i + 1][j], max(DP[i][k],p));\n\t\t\t}\n\t\t}\n\t}\n\tdouble an = 1e18;\n\trep(i, 3e5) {\n\t\tan = min(an, DP[N][i]);\n\t}\n\tcout << fixed << setprecision(16) << an << endl;\n}", "accuracy": 1, "time_ms": 130, "memory_kb": 54644, "score_of_the_acc": -0.6226, "final_rank": 12 }, { "submission_id": "aoj_2305_5960619", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << fixed << setprecision(15);\n\n int N;\n cin >> N;\n vector<int> a(N);\n for (auto& x : a) cin >> x;\n const int M = 2e5;\n vector<vector<double>> ratio(N+1, vector<double>(M, 1e9));\n ratio[0][1] = 0;\n for (int i = 0; i < N; ++i) {\n for (int j = 1; j < M; ++j) {\n for (int k = 1; j * k < M; ++k) {\n ratio[i+1][j*k] = min(ratio[i+1][j*k], max(ratio[i][j], 1.0*abs(j*k - a[i])/a[i]));\n }\n }\n }\n cout << *min_element(ratio[N].begin(), ratio[N].end()) << \"\\n\";\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 37276, "score_of_the_acc": -0.3761, "final_rank": 8 }, { "submission_id": "aoj_2305_5925545", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nconst int MAX_A = 200010;\nconst double INF = 1e9;\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(15);\n int N;\n cin >> N;\n\n vector<double> dp(MAX_A, INF), ndp(MAX_A, INF);\n dp[1] = 0;\n for (int i = 0; i < N; i++) {\n int a;\n cin >> a;\n for (int j = MAX_A - 1; j > 0; j--) {\n for (int k = j + j; k < MAX_A; k += j) {\n dp[k] = min(dp[k], dp[j]);\n }\n }\n for (int j = 1; j < MAX_A; j++) dp[j] = max(dp[j], double(abs(j - a)) / a);\n }\n\n double ans = *min_element(dp.begin(), dp.end());\n cout << ans << '\\n';\n return 0;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 6236, "score_of_the_acc": -0.045, "final_rank": 1 }, { "submission_id": "aoj_2305_5925540", "code_snippet": "#define LOCAL\n#include <bits/stdc++.h>\nusing namespace std;\n#pragma region Macros\ntypedef long long ll;\ntypedef __int128_t i128;\ntypedef unsigned int uint;\ntypedef unsigned long long ull;\n#define ALL(x) (x).begin(), (x).end()\n\ntemplate <typename T> istream& operator>>(istream& is, vector<T>& v) {\n for (T& x : v) is >> x;\n return is;\n}\ntemplate <typename T> ostream& operator<<(ostream& os, const vector<T>& v) {\n for (int i = 0; i < (int)v.size(); i++) {\n os << v[i] << (i + 1 == (int)v.size() ? \"\" : \" \");\n }\n return os;\n}\ntemplate <typename T, typename U> ostream& operator<<(ostream& os, const pair<T, U>& p) {\n os << '(' << p.first << ',' << p.second << ')';\n return os;\n}\ntemplate <typename T, typename U> ostream& operator<<(ostream& os, const map<T, U>& m) {\n os << '{';\n for (auto itr = m.begin(); itr != m.end();) {\n os << '(' << itr->first << ',' << itr->second << ')';\n if (++itr != m.end()) os << ',';\n }\n os << '}';\n return os;\n}\ntemplate <typename T, typename U> ostream& operator<<(ostream& os, const unordered_map<T, U>& m) {\n os << '{';\n for (auto itr = m.begin(); itr != m.end();) {\n os << '(' << itr->first << ',' << itr->second << ')';\n if (++itr != m.end()) os << ',';\n }\n os << '}';\n return os;\n}\ntemplate <typename T> ostream& operator<<(ostream& os, const set<T>& s) {\n os << '{';\n for (auto itr = s.begin(); itr != s.end();) {\n os << *itr;\n if (++itr != s.end()) os << ',';\n }\n os << '}';\n return os;\n}\ntemplate <typename T> ostream& operator<<(ostream& os, const multiset<T>& s) {\n os << '{';\n for (auto itr = s.begin(); itr != s.end();) {\n os << *itr;\n if (++itr != s.end()) os << ',';\n }\n os << '}';\n return os;\n}\ntemplate <typename T> ostream& operator<<(ostream& os, const unordered_set<T>& s) {\n os << '{';\n for (auto itr = s.begin(); itr != s.end();) {\n os << *itr;\n if (++itr != s.end()) os << ',';\n }\n os << '}';\n return os;\n}\ntemplate <typename T> ostream& operator<<(ostream& os, const deque<T>& v) {\n for (int i = 0; i < (int)v.size(); i++) {\n os << v[i] << (i + 1 == (int)v.size() ? \"\" : \" \");\n }\n return os;\n}\n\ntemplate <int i, typename T> void print_tuple(ostream&, const T&) {}\ntemplate <int i, typename T, typename H, class... Args> void print_tuple(ostream& os, const T& t) {\n if (i) os << ',';\n os << get<i>(t);\n print_tuple<i + 1, T, Args...>(os, t);\n}\ntemplate <typename... Args> ostream& operator<<(ostream& os, const tuple<Args...>& t) {\n os << '{';\n print_tuple<0, tuple<Args...>, Args...>(os, t);\n return os << '}';\n}\n\nvoid debug_out() { cerr << '\\n'; }\ntemplate <class Head, class... Tail> void debug_out(Head&& head, Tail&&... tail) {\n cerr << head;\n if (sizeof...(Tail) > 0) cerr << \", \";\n debug_out(move(tail)...);\n}\n#ifdef LOCAL\n#define debug(...) \\\n cerr << \" \"; \\\n cerr << #__VA_ARGS__ << \" :[\" << __LINE__ << \":\" << __FUNCTION__ << \"]\" << '\\n'; \\\n cerr << \" \"; \\\n debug_out(__VA_ARGS__)\n#else\n#define debug(...) 42\n#endif\n\ntemplate <typename T> T gcd(T x, T y) { return y != 0 ? gcd(y, x % y) : x; }\ntemplate <typename T> T lcm(T x, T y) { return x / gcd(x, y) * y; }\n\nint topbit(signed t) { return t == 0 ? -1 : 31 - __builtin_clz(t); }\nint topbit(long long t) { return t == 0 ? -1 : 63 - __builtin_clzll(t); }\nint botbit(signed a) { return a == 0 ? 32 : __builtin_ctz(a); }\nint botbit(long long a) { return a == 0 ? 64 : __builtin_ctzll(a); }\nint popcount(signed t) { return __builtin_popcount(t); }\nint popcount(long long t) { return __builtin_popcountll(t); }\nbool ispow2(int i) { return i && (i & -i) == i; }\n\ntemplate <class T> T ceil(T x, T y) {\n assert(y >= 1);\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <class T> T floor(T x, T y) {\n assert(y >= 1);\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\n\ntemplate <class T1, class T2> inline bool chmin(T1& a, T2 b) {\n if (a > b) {\n a = b;\n return true;\n }\n return false;\n}\ntemplate <class T1, class T2> inline bool chmax(T1& a, T2 b) {\n if (a < b) {\n a = b;\n return true;\n }\n return false;\n}\n#pragma endregion\n\nconst int INF = 1e9;\nconst long long IINF = 1e18;\nconst int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};\nconst char dir[4] = {'D', 'R', 'U', 'L'};\nconst long long MOD = 1000000007;\n// const long long MOD = 998244353;\n\nconst int MAX_A = 200010;\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(15);\n int N;\n cin >> N;\n\n vector<double> dp(MAX_A, INF), ndp(MAX_A, INF);\n dp[1] = 0;\n for (int i = 0; i < N; i++) {\n int a;\n cin >> a;\n for (int j = MAX_A - 1; j > 0; j--) {\n for (int k = j + j; k < MAX_A; k += j) {\n dp[k] = min(dp[k], dp[j]);\n }\n }\n for (int j = 1; j < MAX_A; j++) dp[j] = max(dp[j], double(abs(j - a)) / a);\n }\n\n double ans = *min_element(dp.begin(), dp.end());\n cout << ans << '\\n';\n return 0;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 6244, "score_of_the_acc": -0.0451, "final_rank": 2 }, { "submission_id": "aoj_2305_5925533", "code_snippet": "#define LOCAL\n#include <bits/stdc++.h>\nusing namespace std;\n#pragma region Macros\ntypedef long long ll;\ntypedef __int128_t i128;\ntypedef unsigned int uint;\ntypedef unsigned long long ull;\n#define ALL(x) (x).begin(), (x).end()\n\ntemplate <typename T> istream& operator>>(istream& is, vector<T>& v) {\n for (T& x : v) is >> x;\n return is;\n}\ntemplate <typename T> ostream& operator<<(ostream& os, const vector<T>& v) {\n for (int i = 0; i < (int)v.size(); i++) {\n os << v[i] << (i + 1 == (int)v.size() ? \"\" : \" \");\n }\n return os;\n}\ntemplate <typename T, typename U> ostream& operator<<(ostream& os, const pair<T, U>& p) {\n os << '(' << p.first << ',' << p.second << ')';\n return os;\n}\ntemplate <typename T, typename U> ostream& operator<<(ostream& os, const map<T, U>& m) {\n os << '{';\n for (auto itr = m.begin(); itr != m.end();) {\n os << '(' << itr->first << ',' << itr->second << ')';\n if (++itr != m.end()) os << ',';\n }\n os << '}';\n return os;\n}\ntemplate <typename T, typename U> ostream& operator<<(ostream& os, const unordered_map<T, U>& m) {\n os << '{';\n for (auto itr = m.begin(); itr != m.end();) {\n os << '(' << itr->first << ',' << itr->second << ')';\n if (++itr != m.end()) os << ',';\n }\n os << '}';\n return os;\n}\ntemplate <typename T> ostream& operator<<(ostream& os, const set<T>& s) {\n os << '{';\n for (auto itr = s.begin(); itr != s.end();) {\n os << *itr;\n if (++itr != s.end()) os << ',';\n }\n os << '}';\n return os;\n}\ntemplate <typename T> ostream& operator<<(ostream& os, const multiset<T>& s) {\n os << '{';\n for (auto itr = s.begin(); itr != s.end();) {\n os << *itr;\n if (++itr != s.end()) os << ',';\n }\n os << '}';\n return os;\n}\ntemplate <typename T> ostream& operator<<(ostream& os, const unordered_set<T>& s) {\n os << '{';\n for (auto itr = s.begin(); itr != s.end();) {\n os << *itr;\n if (++itr != s.end()) os << ',';\n }\n os << '}';\n return os;\n}\ntemplate <typename T> ostream& operator<<(ostream& os, const deque<T>& v) {\n for (int i = 0; i < (int)v.size(); i++) {\n os << v[i] << (i + 1 == (int)v.size() ? \"\" : \" \");\n }\n return os;\n}\n\ntemplate <int i, typename T> void print_tuple(ostream&, const T&) {}\ntemplate <int i, typename T, typename H, class... Args> void print_tuple(ostream& os, const T& t) {\n if (i) os << ',';\n os << get<i>(t);\n print_tuple<i + 1, T, Args...>(os, t);\n}\ntemplate <typename... Args> ostream& operator<<(ostream& os, const tuple<Args...>& t) {\n os << '{';\n print_tuple<0, tuple<Args...>, Args...>(os, t);\n return os << '}';\n}\n\nvoid debug_out() { cerr << '\\n'; }\ntemplate <class Head, class... Tail> void debug_out(Head&& head, Tail&&... tail) {\n cerr << head;\n if (sizeof...(Tail) > 0) cerr << \", \";\n debug_out(move(tail)...);\n}\n#ifdef LOCAL\n#define debug(...) \\\n cerr << \" \"; \\\n cerr << #__VA_ARGS__ << \" :[\" << __LINE__ << \":\" << __FUNCTION__ << \"]\" << '\\n'; \\\n cerr << \" \"; \\\n debug_out(__VA_ARGS__)\n#else\n#define debug(...) 42\n#endif\n\ntemplate <typename T> T gcd(T x, T y) { return y != 0 ? gcd(y, x % y) : x; }\ntemplate <typename T> T lcm(T x, T y) { return x / gcd(x, y) * y; }\n\nint topbit(signed t) { return t == 0 ? -1 : 31 - __builtin_clz(t); }\nint topbit(long long t) { return t == 0 ? -1 : 63 - __builtin_clzll(t); }\nint botbit(signed a) { return a == 0 ? 32 : __builtin_ctz(a); }\nint botbit(long long a) { return a == 0 ? 64 : __builtin_ctzll(a); }\nint popcount(signed t) { return __builtin_popcount(t); }\nint popcount(long long t) { return __builtin_popcountll(t); }\nbool ispow2(int i) { return i && (i & -i) == i; }\n\ntemplate <class T> T ceil(T x, T y) {\n assert(y >= 1);\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <class T> T floor(T x, T y) {\n assert(y >= 1);\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\n\ntemplate <class T1, class T2> inline bool chmin(T1& a, T2 b) {\n if (a > b) {\n a = b;\n return true;\n }\n return false;\n}\ntemplate <class T1, class T2> inline bool chmax(T1& a, T2 b) {\n if (a < b) {\n a = b;\n return true;\n }\n return false;\n}\n#pragma endregion\n\nconst int INF = 1e9;\nconst long long IINF = 1e18;\nconst int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};\nconst char dir[4] = {'D', 'R', 'U', 'L'};\nconst long long MOD = 1000000007;\n// const long long MOD = 998244353;\n\nconst int MAX_A = 100010;\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(15);\n int N;\n cin >> N;\n\n vector<double> dp(MAX_A, INF), ndp(MAX_A, INF);\n dp[1] = 0;\n for (int i = 0; i < N; i++) {\n int a;\n cin >> a;\n for (int j = MAX_A - 1; j > 0; j--) {\n for (int k = j + j; k < MAX_A; k += j) {\n dp[k] = min(dp[k], dp[j]);\n }\n }\n for (int j = 1; j < MAX_A; j++) dp[j] = max(dp[j], double(abs(j - a)) / a);\n }\n\n double ans = *min_element(dp.begin(), dp.end());\n cout << ans << '\\n';\n return 0;\n}", "accuracy": 0.6274509803921569, "time_ms": 20, "memory_kb": 4652, "score_of_the_acc": 0, "final_rank": 18 } ]
aoj_2309_cpp
Vector Compression You are recording a result of a secret experiment, which consists of a large set of N -dimensional vectors. Since the result may become very large, you are thinking of compressing it. Fortunately you already have a good compression method for vectors with small absolute values, all you have to do is to preprocess the vectors and make them small. You can record the set of vectors in any order you like. Let's assume you process them in the order v_1 , v_2 ,..., v_M . Each vector v_i is recorded either as is , or as a difference vector. When it is recorded as a difference, you can arbitrarily pick up an already chosen vector v_j (j<i) and a real value r . Then the actual vector value recorded is (v_i - r v_j) . The values of r and j do not affect the compression ratio so much, so you don't have to care about them. Given a set of vectors, your task is to write a program that calculates the minimum sum of the squared length of the recorded vectors. Input The input is like the following style. N M v_{1,1} v_{1,2} ... v_{1,N} ... v_{M,1} v_{M,2} ... v_{M,N} The first line contains two integers N and M ( 1 \leq N, M \leq 100 ), where N is the dimension of each vector, and M is the number of the vectors. Each of the following M lines contains N floating point values v_{i,j} ( -1.0 \leq v_{i,j} \leq 1.0 ) which represents the j -th element value of the i -th vector. Output Output the minimum sum of the squared length of the recorded vectors. The output should not contain an absolute error greater than 10^{-6} . Sample Input 1 2 3 1.0 1.0 -1.0 0.0 0.5 0.5 Output for the Sample Input 1 1.0 Sample Input 2 1 1 1.0 Output for the Sample Input 2 1.0 Sample Input 3 4 3 1.0 1.0 0.0 0.0 -1.0 0.0 -1.0 0.0 0.5 0.5 0.5 0.5 Output for the Sample Input 3 3.0
[ { "submission_id": "aoj_2309_9813617", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n/**\n * @brief template\n */\n\nstruct SCC{\n int n,m,cur;\n vector<vector<int>> g;\n vector<int> low,ord,id;\n SCC(int _n=0):n(_n),m(0),cur(0),g(_n),low(_n),ord(_n,-1),id(_n){}\n void resize(int _n){\n n=_n;\n g.resize(n);\n low.resize(n);\n ord.resize(n,-1);\n id.resize(n);\n }\n void add_edge(int u,int v){g[u].emplace_back(v);}\n void dfs(int v,vector<int>& used){\n ord[v]=low[v]=cur++;\n used.emplace_back(v);\n for(auto& nxt:g[v]){\n if(ord[nxt]==-1){\n dfs(nxt,used); chmin(low[v],low[nxt]);\n }\n else{\n chmin(low[v],ord[nxt]);\n }\n }\n if(ord[v]==low[v]){\n while(1){\n int add=used.back(); used.pop_back();\n ord[add]=n; id[add]=m;\n if(v==add)break;\n }\n m++;\n }\n }\n void run(){\n vector<int> used;\n rep(v,0,n)if(ord[v]==-1)dfs(v,used);\n for(auto& x:id)x=m-1-x;\n }\n};\n\n/**\n * @brief Strongly Connected Components\n */\n\ntemplate <typename T>\nstruct DirectedMST {\n const vector<vector<T>>& G;\n T _INF;\n\n DirectedMST(const vector<vector<T>>& G) : _INF(numeric_limits<T>::max()), G(G) {}\n\n T DFS(vector<vector<T>>& G, int Root, T Cur) {\n int N = G.size();\n vector<int> rev(N, -1);\n vector<T> weight(N, _INF);\n rep(i,0,N) {\n rep(j,0,N) {\n if (chmin(weight[j], G[i][j])) {\n rev[j] = i;\n }\n }\n }\n SCC scc(N);\n rep(i,0,N) {\n if (i == Root) continue;\n if (rev[i] == -1) return _INF;\n scc.add_edge(rev[i],i);\n Cur += weight[i];\n }\n scc.run();\n if (scc.m == N) return Cur;\n vector<vector<T>> NewG(scc.m,vector<T>(scc.m,_INF));\n rep(i,0,N) {\n rep(j,0,N) {\n if (scc.id[i] == scc.id[j]) continue;\n chmin(NewG[scc.id[i]][scc.id[j]], G[i][j]-weight[j]);\n }\n }\n return DFS(NewG, scc.id[Root], Cur);\n }\n};\n\nusing Vec = vector<double>;\n\ndouble dot(Vec A, Vec B) {\n int N = A.size();\n double Ret = 0.0;\n rep(i,0,N) Ret += A[i] * B[i];\n return Ret;\n}\n\nint main() {\n int N, M;\n cin >> M >> N;\n vector<Vec> V(N,vector<double>(M));\n rep(i,0,N) rep(j,0,M) cin >> V[i][j];\n vector<vector<double>> G(N+1,vector<double>(N+1,inf));\n rep(i,0,N) {\n rep(j,0,N) {\n if (i == j) continue;\n if (dot(V[i],V[i]) == 0.0) G[i][j] = dot(V[j],V[j]);\n G[i][j] = (dot(V[i],V[i])*dot(V[j],V[j])-dot(V[i],V[j])*dot(V[i],V[j])) / dot(V[i],V[i]);\n }\n }\n rep(i,0,N) {\n G[N][i] = dot(V[i],V[i]);\n }\n DirectedMST<double> mst(G);\n printf(\"%.12f\\n\",mst.DFS(G,N,0.0));\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 4456, "score_of_the_acc": -0.0508, "final_rank": 6 }, { "submission_id": "aoj_2309_5448636", "code_snippet": "// #define _GLIBCXX_DEBUG // for STL debug (optional)\n#include <iostream>\n#include <iomanip>\n#include <cstdio>\n#include <string>\n#include <cstring>\n#include <deque>\n#include <list>\n#include <queue>\n#include <stack>\n#include <vector>\n#include <utility>\n#include <algorithm>\n#include <map>\n#include <set>\n#include <complex>\n#include <cmath>\n#include <limits>\n#include <cfloat>\n#include <climits>\n#include <ctime>\n#include <cassert>\n#include <numeric>\n#include <fstream>\n#include <functional>\n#include <bitset>\nusing namespace std;\nusing ll = long long int;\nusing int64 = long long int;\n \ntemplate<typename T> void chmax(T &a, T b) {a = max(a, b);}\ntemplate<typename T> void chmin(T &a, T b) {a = min(a, b);}\ntemplate<typename T> void chadd(T &a, T b) {a = a + b;}\n \nint dx[] = {0, 0, 1, -1};\nint dy[] = {1, -1, 0, 0};\nconst int INF = 1LL << 29;\nconst ll LONGINF = 1LL << 60;\nconst ll MOD = 1000000007LL;\n\ntemplate< typename T = int >\nstruct Edge {\n int from, to;\n T cost;\n int idx;\n\n Edge() = default;\n\n Edge(int from, int to, T cost = 1, int idx = -1) : from(from), to(to), cost(cost), idx(idx) {}\n\n operator int() const { return to; }\n};\n\ntemplate< typename T = int >\nstruct Graph {\n vector< vector< Edge< T > > > g;\n int es;\n\n Graph() = default;\n\n explicit Graph(int n) : g(n), es(0) {}\n\n size_t size() const {\n return g.size();\n }\n\n void add_directed_edge(int from, int to, T cost = 1) {\n g[from].emplace_back(from, to, cost, es++);\n }\n\n void add_edge(int from, int to, T cost = 1) {\n g[from].emplace_back(from, to, cost, es);\n g[to].emplace_back(to, from, cost, es++);\n }\n\n void read(int M, int padding = -1, bool weighted = false, bool directed = false) {\n for(int i = 0; i < M; i++) {\n int a, b;\n cin >> a >> b;\n a += padding;\n b += padding;\n T c = T(1);\n if(weighted) cin >> c;\n if(directed) add_directed_edge(a, b, c);\n else add_edge(a, b, c);\n }\n }\n};\n\ntemplate< typename T = int >\nusing Edges = vector< Edge< T > >;\n\n\n/**\n * @brief Skew-Heap\n */\ntemplate< typename T, bool isMin = true >\nstruct SkewHeap {\n struct Node {\n T key, lazy;\n Node *l, *r;\n int idx;\n\n explicit Node(const T &key, int idx) : key(key), idx(idx), lazy(0), l(nullptr), r(nullptr) {}\n };\n\n SkewHeap() = default;\n\n Node *alloc(const T &key, int idx = -1) {\n return new Node(key, idx);\n }\n\n Node *propagate(Node *t) {\n if(t && t->lazy != 0) {\n if(t->l) t->l->lazy += t->lazy;\n if(t->r) t->r->lazy += t->lazy;\n t->key += t->lazy;\n t->lazy = 0;\n }\n return t;\n }\n\n Node *meld(Node *x, Node *y) {\n propagate(x), propagate(y);\n if(!x || !y) return x ? x : y;\n if((x->key < y->key) ^ isMin) swap(x, y);\n x->r = meld(y, x->r);\n swap(x->l, x->r);\n return x;\n }\n\n Node *push(Node *t, const T &key, int idx = -1) {\n return meld(t, alloc(key, idx));\n }\n\n Node *pop(Node *t) {\n assert(t != nullptr);\n return meld(t->l, t->r);\n }\n\n Node *add(Node *t, const T &lazy) {\n if(t) {\n t->lazy += lazy;\n propagate(t);\n }\n return t;\n }\n\n Node *make_root() {\n return nullptr;\n }\n};\n\n\n/**\n * @brief Directed-Minimum-Spanning-Tree(最小有向全域木)\n */\ntemplate< typename T >\nstruct MinimumSpanningTree {\n T cost;\n Edges< T > edges;\n};\n\ntemplate< typename T >\nMinimumSpanningTree< T > directed_minimum_spanning_tree(int V, int root, Edges< T > edges) {\n for(int i = 0; i < V; ++i) {\n if(i != root) edges.emplace_back(i, root, 0);\n }\n\n int x = 0;\n vector< int > par(2 * V, -1), vis(par), link(par);\n\n using Heap = SkewHeap< T, true >;\n using Node = typename Heap::Node;\n\n Heap heap;\n vector< Node * > ins(2 * V, heap.make_root());\n\n for(int i = 0; i < (int) edges.size(); i++) {\n auto &e = edges[i];\n ins[e.to] = heap.push(ins[e.to], e.cost, i);\n }\n vector< int > st;\n auto go = [&](int x) {\n x = edges[ins[x]->idx].from;\n while(link[x] != -1) {\n st.emplace_back(x);\n x = link[x];\n }\n for(auto &p : st) {\n link[p] = x;\n }\n st.clear();\n return x;\n };\n for(int i = V; ins[x]; i++) {\n for(; vis[x] == -1; x = go(x)) vis[x] = 0;\n for(; x != i; x = go(x)) {\n auto w = ins[x]->key;\n auto v = heap.pop(ins[x]);\n v = heap.add(v, -w);\n ins[i] = heap.meld(ins[i], v);\n par[x] = i;\n link[x] = i;\n }\n for(; ins[x] && go(x) == x; ins[x] = heap.pop(ins[x]));\n }\n T cost = 0;\n Edges< T > ans;\n for(int i = root; i != -1; i = par[i]) {\n vis[i] = 1;\n }\n for(int i = x; i >= 0; i--) {\n if(vis[i] == 1) continue;\n cost += edges[ins[i]->idx].cost;\n ans.emplace_back(edges[ins[i]->idx]);\n for(int j = edges[ins[i]->idx].to; j != -1 && vis[j] == 0; j = par[j]) {\n vis[j] = 1;\n }\n }\n return {cost, ans};\n}\n\nint main() {\n int N, M; scanf(\"%d%d\", &N, &M);\n vector< vector<double> > V(M, vector<double>(N));\n for(int i=0; i<M; i++) {\n for(int j=0; j<N; j++) {\n scanf(\"%lf\", &V[i][j]);\n }\n }\n\n auto calc_cost = [&](int x, int y) {\n double ub = INF, lb = -INF;\n for(int i=0; i<200; i++) {\n double m1 = (lb + lb + ub) / 3.0;\n double m2 = (lb + ub + ub) / 3.0;\n double f1 = 0.0, f2 = 0.0;\n for(int i=0; i<N; i++) {\n f1 += (V[x][i] - m1 * V[y][i]) * (V[x][i] - m1 * V[y][i]);\n f2 += (V[x][i] - m2 * V[y][i]) * (V[x][i] - m2 * V[y][i]); \n }\n if(f1 < f2) ub = m2;\n else lb = m1;\n }\n double m = lb;\n double f = 0.0;\n for(int i=0; i<N; i++) {\n f += (V[x][i] - m * V[y][i]) * (V[x][i] - m * V[y][i]);\n }\n return f;\n };\n\n Edges<double> edges;\n for(int i=0; i<M; i++) {\n double cost = 0.0;\n for(int j=0; j<N; j++) {\n cost += V[i][j] * V[i][j];\n }\n edges.emplace_back(M, i, cost);\n for(int j=0; j<M; j++) {\n if(i == j) continue;\n edges.emplace_back(j, i, calc_cost(i, j));\n }\n }\n printf(\"%.12f\\n\", directed_minimum_spanning_tree(M+1, M, edges).cost);\n return 0;\n}", "accuracy": 1, "time_ms": 220, "memory_kb": 3948, "score_of_the_acc": -0.2513, "final_rank": 8 }, { "submission_id": "aoj_2309_5368428", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nconst double INF = 100000;\nint main(){\n cout << fixed << setprecision(20);\n int N, M;\n cin >> N >> M;\n vector<vector<double>> A(M, vector<double>(N));\n for (int i = 0; i < M; i++){\n for (int j = 0; j < N; j++){\n cin >> A[i][j];\n }\n }\n vector<vector<pair<double, int>>> E(M + 1);\n for (int i = 0; i < M; i++){\n for (int j = 0; j < M; j++){\n if (i != j){\n double a = 0, b = 0, c = 0;\n for (int k = 0; k < N; k++){\n a += A[i][k] * A[i][k];\n b -= 2 * A[i][k] * A[j][k];\n c += A[j][k] * A[j][k];\n }\n double d = c - (b * b) / (4 * a);\n E[i].push_back(make_pair(d, j));\n }\n }\n }\n for (int i = 0; i < M; i++){\n double sum = 0;\n for (int j = 0; j < N; j++){\n sum += A[i][j] * A[i][j];\n }\n E[M].push_back(make_pair(sum, i));\n }\n int r = M;\n double ans = 0;\n while (true){\n int V = E.size();\n vector<double> mn(V, INF);\n vector<int> p(V);\n for (int i = 0; i < V; i++){\n for (auto P : E[i]){\n double c = P.first;\n int w = P.second;\n if (w != r && c < mn[w]){\n mn[w] = c;\n p[w] = i;\n }\n }\n }\n vector<int> d(V, 0);\n for (int i = 0; i < V; i++){\n if (i != r){\n d[p[i]]++;\n }\n }\n queue<int> Q;\n for (int i = 0; i < V; i++){\n \tif (i != r && d[i] == 0){\n Q.push(i);\n }\n }\n while (!Q.empty()){\n int v = Q.front();\n Q.pop();\n if (v != r){\n d[p[v]]--;\n if (d[p[v]] == 0){\n Q.push(p[v]);\n }\n }\n }\n vector<int> cycle;\n vector<int> id(V, -1);\n for (int i = 0; i < V; i++){\n if (d[i] == 1){\n cycle.push_back(i);\n while (true){\n int v = cycle.back();\n ans += mn[v];\n id[v] = 0;\n if (p[v] == i){\n break;\n }\n cycle.push_back(p[v]);\n }\n break;\n }\n }\n if (cycle.empty()){\n for (int i = 0; i < V; i++){\n if (i != r){\n ans += mn[i];\n }\n }\n break;\n }\n int cnt = 1;\n for (int i = 0; i < V; i++){\n if (id[i] == -1){\n id[i] = cnt;\n cnt++;\n }\n }\n vector<vector<pair<double, int>>> E2(cnt);\n for (int i = 0; i < V; i++){\n for (auto P : E[i]){\n double c = P.first;\n int w = P.second;\n if (id[w] == 0){\n c -= mn[w];\n }\n if (id[i] != id[w]){\n E2[id[i]].push_back(make_pair(c, id[w]));\n }\n }\n }\n swap(E, E2);\n r = id[r];\n }\n cout << ans << endl;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3500, "score_of_the_acc": -0.0121, "final_rank": 1 }, { "submission_id": "aoj_2309_5294410", "code_snippet": "#include <bits/stdc++.h>\n//#include <atcoder/all>\n#define endl \"\\n\"\nusing namespace std;\ntemplate<class T>\ninline bool chmax(T &a, T b) {\n if(a < b) {\n a = b;\n return true;\n }\n return false;\n}\n\ntemplate<class T>\ninline bool chmin(T &a, T b) {\n if(a > b) {\n a = b;\n return true;\n }\n return false;\n}\n\nconst long long INF = 1e18;\n//const ll mod = 1000000007;\n\n#define rep(i, a, b) for(int i = a; i < (b); ++i)\n#define all(x) begin(x), end(x)\n#define sz(x) (int)(x).size()\ntypedef pair<int, int> pii;\ntypedef vector<int> vi;\n\nstruct RollbackUF {\n\tvi e; vector<pii> st;\n\tRollbackUF(int n) : e(n, -1) {}\n\tint size(int x) { return -e[find(x)]; }\n\tint find(int x) { return e[x] < 0 ? x : find(e[x]); }\n\tint time() { return sz(st); }\n\tvoid rollback(int t) {\n\t\tfor (int i = time(); i --> t;)\n\t\t\te[st[i].first] = st[i].second;\n\t\tst.resize(t);\n\t}\n\tbool join(int a, int b) {\n\t\ta = find(a), b = find(b);\n\t\tif (a == b) return false;\n\t\tif (e[a] > e[b]) swap(a, b);\n\t\tst.push_back({a, e[a]});\n\t\tst.push_back({b, e[b]});\n\t\te[a] += e[b]; e[b] = a;\n\t\treturn true;\n\t}\n};\nstruct Edge { int a, b; double w; };\nstruct Node { /// lazy skew heap node\n\tEdge key;\n\tNode *l, *r;\n\tdouble delta;\n\tvoid prop() {\n\t\tkey.w += delta;\n\t\tif (l) l->delta += delta;\n\t\tif (r) r->delta += delta;\n\t\tdelta = 0;\n\t}\n\tEdge top() { prop(); return key; }\n};\nNode *merge(Node *a, Node *b) {\n\tif (!a || !b) return a ?: b;\n\ta->prop(), b->prop();\n\tif (a->key.w > b->key.w) swap(a, b);\n\tswap(a->l, (a->r = merge(b, a->r)));\n\treturn a;\n}\nvoid pop(Node*& a) { a->prop(); a = merge(a->l, a->r); }\n\ndouble dmst(int n, int r, vector<Edge>& g) {\n\tRollbackUF uf(n);\n\tvector<Node*> heap(n);\n\tfor (Edge e : g) heap[e.b] = merge(heap[e.b], new Node{e});\n\tdouble res = 0;\n\tvi seen(n, -1), path(n), par(n);\n\tseen[r] = r;\n\tvector<Edge> Q(n), in(n, {-1,-1}), comp;\n\tdeque<tuple<int, int, vector<Edge>>> cycs;\n\trep(s,0,n) {\n\t\tint u = s, qi = 0, w;\n\t\twhile (seen[u] < 0) {\n\t\t\tif (!heap[u]) return -1;\n\t\t\tEdge e = heap[u]->top();\n\t\t\theap[u]->delta -= e.w, pop(heap[u]);\n\t\t\tQ[qi] = e, path[qi++] = u, seen[u] = s;\n\t\t\tres += e.w, u = uf.find(e.a);\n\t\t\tif (seen[u] == s) { /// found cycle, contract\n\t\t\t\tNode* cyc = 0;\n\t\t\t\tint end = qi, time = uf.time();\n\t\t\t\tdo cyc = merge(cyc, heap[w = path[--qi]]);\n\t\t\t\twhile (uf.join(u, w));\n\t\t\t\tu = uf.find(u), heap[u] = cyc, seen[u] = -1;\n\t\t\t\tcycs.push_front({u, time, {&Q[qi], &Q[end]}});\n\t\t\t}\n\t\t}\n\t\trep(i,0,qi) in[uf.find(Q[i].b)] = Q[i];\n\t}\n\n\tfor (auto& [u,t,comp] : cycs) { // restore sol (optional)\n\t\tuf.rollback(t);\n\t\tEdge inEdge = in[u];\n\t\tfor (auto& e : comp) in[uf.find(e.b)] = e;\n\t\tin[uf.find(inEdge.b)] = inEdge;\n\t}\n\trep(i,0,n) par[i] = in[i].a;\n\treturn res;\n}\n\ndouble dist[105][105];\n\nvector<double> Minus(vector<double> a, vector<double> b) {\n\tassert(a.size() == b.size());\n\tint n = a.size();\n\tvector<double> ret(n);\n\tfor(int i = 0; i < n; i++) ret[i] = a[i] - b[i];\n\treturn ret;\n}\nvector<double> prod(double t, vector<double> v) {\n\tfor(auto &tmp : v) tmp *= t;\n\treturn v;\n}\ndouble norm(vector<double> v) {\n\tdouble ret = 0;\n\tfor(auto tmp : v) ret += tmp * tmp;\n\treturn ret;\n}\n\ndouble f(vector<double> a, vector<double> b) {\n\tdouble r = 1e5;\n\tdouble l = -1e5;\n\tfor(int t = 0; t < 60; t++) {\n\t\tdouble ll = (l * 2 + r) / 3;\n\t\tdouble rr = (l + r * 2) / 3;\n \tif(norm(Minus(b, prod(ll, a))) < norm(Minus(b, prod(rr, a)))) {\n\t\t r = rr;\n \t} else {\n\t\t l = ll;\n\t }\n\t}\n\treturn norm(Minus(b, prod(l, a)));\n}\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n\tint N, M;\n\tcin >> N >> M;\n\tvector<vector<double>> A(M + 1, vector<double>(N));\n\tfor(int i = 1; i <= M; i++) {\n\t\tfor(int j = 0; j < N; j++) cin >> A[i][j];\n\t}\n\tvector<Edge> edges;\n\tfor(int i = 0; i <= M; i++) {\n\t\tfor(int j = 0; j <= M; j++) {\n\t\t\tdist[i][j] = f(A[i], A[j]);\n\t\t\t//cerr << dist[i][j] << \" \";\n\t\t}\n\t\t//cerr << endl;\n\t\tfor(int j = 0; j <= M; j++) {\n\t\t\tif(i == j) continue;\n\t\t\tedges.push_back({i, j, dist[i][j]});\n\t\t}\n\t}\n\tdouble ans = dmst(M + 1, 0, edges);\n\tcout << fixed << setprecision(30) << ans << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 370, "memory_kb": 4156, "score_of_the_acc": -0.4176, "final_rank": 9 }, { "submission_id": "aoj_2309_4843795", "code_snippet": "#include<iostream>\n#include<cstdio>\n#include<cstring>\n#include<string>\n#include<vector>\n#include<cmath>\n#include<algorithm>\n#include<map>\n#include<queue>\n#include<deque>\n#include<iomanip>\n#include<tuple>\n#include<cassert>\n#include<set>\nusing namespace std;\ntypedef long long int LL;\ntypedef pair<int,int> P;\ntypedef pair<long double,int> DP;\ntypedef pair<LL,int> LP;\nconst long double INF=1e9+7;\n\nvoid array_show(int *array,int array_n,char middle=' '){\n for(int i=0;i<array_n;i++)printf(\"%d%c\",array[i],(i!=array_n-1?middle:'\\n'));\n}\nvoid array_show(LL *array,int array_n,char middle=' '){\n for(int i=0;i<array_n;i++)printf(\"%lld%c\",array[i],(i!=array_n-1?middle:'\\n'));\n}\nvoid array_show(vector<int> &vec_s,int vec_n=-1,char middle=' '){\n if(vec_n==-1)vec_n=vec_s.size();\n for(int i=0;i<vec_n;i++)printf(\"%d%c\",vec_s[i],(i!=vec_n-1?middle:'\\n'));\n}\nvoid array_show(vector<LL> &vec_s,int vec_n=-1,char middle=' '){\n if(vec_n==-1)vec_n=vec_s.size();\n for(int i=0;i<vec_n;i++)printf(\"%lld%c\",vec_s[i],(i!=vec_n-1?middle:'\\n'));\n}\n\nlong double vec[111][111];\nvector<vector<long double>> dis;\n\nint main(){\n int n,m;\n int i,j,k;\n int a,b,c;\n long double da,db,dc;\n cin>>m>>n;\n dis.assign(n,vector<long double>(n,INF));\n for(i=0;i<n;i++){\n for(j=0;j<m;j++){\n cin>>vec[i][j];\n }\n }\n for(i=0;i<n;i++){\n for(j=0;j<n;j++){\n if(i==j)continue;\n da=0,db=0;\n for(k=0;k<m;k++){\n da+=pow(vec[i][k],2),db+=2*vec[i][k]*vec[j][k];\n }\n if(da>0)db/=2*da;\n da=0;\n for(k=0;k<m;k++){\n da+=pow(vec[j][k]-vec[i][k]*db,2);\n }\n dis[i][j]=da;\n }\n }\n long double ds=INF;\n for(i=0;i<n;i++){\n db=0;\n for(j=0;j<m;j++){\n db+=pow(vec[i][j],2);\n }\n vector<vector<long double>> dis_temp=dis;\n vector<int> v1,v2,par(n,-1);\n for(j=0;j<n;j++){\n if(i!=j)v1.push_back(j);\n }\n while(!v1.empty()){\n a=v1.back(),v1.pop_back();\n da=INF,b=0;\n for(j=0;j<n;j++){\n if(da>dis_temp[j][a]){\n da=dis_temp[j][a];\n b=j;\n }\n }\n for(j=0;j<n;j++){\n if(j==b)dis_temp[j][a]=INF;\n else dis_temp[j][a]-=da;\n }\n db+=da,par[a]=b;\n v2.clear();\n c=a;\n while(c!=-1){\n v2.push_back(c);\n c=par[c];\n if(c==a)break;\n }\n if(c==-1)continue;\n a=v2.size();\n for(j=0;j<a;j++){\n for(k=0;k<a;k++){\n b=v2[j],c=v2[k];\n dis_temp[b][c]=INF;\n }\n }\n for(j=0;j<n;j++){\n b=v2[0];\n for(k=1;k<a;k++){\n c=v2[k];\n dis_temp[b][j]=min(dis_temp[b][j],dis_temp[c][j]);\n dis_temp[j][b]=min(dis_temp[j][b],dis_temp[j][c]);\n dis_temp[j][c]=INF,dis_temp[c][j]=INF;\n }\n }\n for(j=0;j<a;j++){\n b=v2[j];\n par[b]=-1;\n for(k=0;k<n;k++){\n if(par[k]==b)par[k]=v2[0];\n }\n }\n v1.push_back(v2[0]);\n }\n ds=min(ds,db);\n }\n cout<<fixed<<setprecision(15)<<ds<<endl;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3604, "score_of_the_acc": -0.0269, "final_rank": 4 }, { "submission_id": "aoj_2309_4843780", "code_snippet": "#include<iostream>\n#include<cstdio>\n#include<cstring>\n#include<string>\n#include<vector>\n#include<cmath>\n#include<algorithm>\n#include<map>\n#include<queue>\n#include<deque>\n#include<iomanip>\n#include<tuple>\n#include<cassert>\n#include<set>\nusing namespace std;\ntypedef long long int LL;\ntypedef pair<int,int> P;\ntypedef pair<long double,int> DP;\ntypedef pair<LL,int> LP;\nconst long double INF=1e9+7;\n\nvoid array_show(int *array,int array_n,char middle=' '){\n for(int i=0;i<array_n;i++)printf(\"%d%c\",array[i],(i!=array_n-1?middle:'\\n'));\n}\nvoid array_show(LL *array,int array_n,char middle=' '){\n for(int i=0;i<array_n;i++)printf(\"%lld%c\",array[i],(i!=array_n-1?middle:'\\n'));\n}\nvoid array_show(vector<int> &vec_s,int vec_n=-1,char middle=' '){\n if(vec_n==-1)vec_n=vec_s.size();\n for(int i=0;i<vec_n;i++)printf(\"%d%c\",vec_s[i],(i!=vec_n-1?middle:'\\n'));\n}\nvoid array_show(vector<LL> &vec_s,int vec_n=-1,char middle=' '){\n if(vec_n==-1)vec_n=vec_s.size();\n for(int i=0;i<vec_n;i++)printf(\"%lld%c\",vec_s[i],(i!=vec_n-1?middle:'\\n'));\n}\n\nlong double vec[111][111];\nvector<vector<long double>> dis;\n\nint main(){\n int n,m;\n int i,j,k;\n int a,b,c;\n long double da,db,dc;\n cin>>m>>n;\n dis.assign(n,vector<long double>(n,INF));\n for(i=0;i<n;i++){\n for(j=0;j<m;j++){\n cin>>vec[i][j];\n }\n }\n for(i=0;i<n;i++){\n for(j=0;j<n;j++){\n if(i==j)continue;\n da=0,db=0;\n for(k=0;k<m;k++){\n da+=pow(vec[i][k],2),db+=2*vec[i][k]*vec[j][k];\n }\n if(da>0)db/=2*da;\n da=0;\n for(k=0;k<m;k++){\n da+=pow(vec[j][k]-vec[i][k]*db,2);\n }\n dis[i][j]=da;\n }\n }\n long double ds=INF;\n for(i=0;i<n;i++){\n db=0;\n for(j=0;j<m;j++){\n db+=pow(vec[i][j],2);\n }\n vector<vector<long double>> dis_temp=dis;\n vector<int> v1,v2,par(n,-1);\n \n for(int pos=0;pos<n;pos++){\n a=pos;\n while(a!=i){\n if(par[a]!=-1)break;\n da=INF,b=0;\n for(j=0;j<n;j++){\n if(da>dis_temp[j][a]){\n da=dis_temp[j][a];\n b=j;\n }\n }\n for(j=0;j<n;j++){\n if(j==b)dis_temp[j][a]=INF;\n else dis_temp[j][a]-=da;\n }\n db+=da,par[a]=b;\n v2.clear();\n c=a;\n while(c!=-1){\n v2.push_back(c);\n if(par[c]==a || par[c]<0)break;\n c=par[c];\n }\n if(par[c]<0){\n a=c;\n continue;\n }\n int sz=v2.size();\n for(j=0;j<sz;j++){\n for(k=0;k<sz;k++){\n b=v2[j],c=v2[k];\n dis_temp[b][c]=INF;\n }\n }\n for(j=0;j<n;j++){\n b=v2[0];\n for(k=1;k<sz;k++){\n c=v2[k];\n dis_temp[b][j]=min(dis_temp[b][j],dis_temp[c][j]);\n dis_temp[j][b]=min(dis_temp[j][b],dis_temp[j][c]);\n dis_temp[j][c]=INF,dis_temp[c][j]=INF;\n }\n }\n for(j=1;j<sz;j++){\n b=v2[j];\n par[b]=-2;\n for(k=0;k<n;k++){\n if(par[k]==b)par[k]=a;\n }\n }\n par[a]=-1;\n }\n }\n ds=min(ds,db);\n }\n cout<<fixed<<setprecision(15)<<ds<<endl;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3592, "score_of_the_acc": -0.0158, "final_rank": 2 }, { "submission_id": "aoj_2309_4837603", "code_snippet": "#include<iostream>\n#include<cstdio>\n#include<cstring>\n#include<string>\n#include<vector>\n#include<cmath>\n#include<algorithm>\n#include<map>\n#include<queue>\n#include<deque>\n#include<iomanip>\n#include<tuple>\n#include<cassert>\n#include<set>\nusing namespace std;\ntypedef long long int LL;\ntypedef pair<int,int> P;\ntypedef pair<long double,int> DP;\ntypedef pair<LL,int> LP;\nconst long double INF=1e9+7;\n\nvoid array_show(int *array,int array_n,char middle=' '){\n for(int i=0;i<array_n;i++)printf(\"%d%c\",array[i],(i!=array_n-1?middle:'\\n'));\n}\nvoid array_show(LL *array,int array_n,char middle=' '){\n for(int i=0;i<array_n;i++)printf(\"%lld%c\",array[i],(i!=array_n-1?middle:'\\n'));\n}\nvoid array_show(vector<int> &vec_s,int vec_n=-1,char middle=' '){\n if(vec_n==-1)vec_n=vec_s.size();\n for(int i=0;i<vec_n;i++)printf(\"%d%c\",vec_s[i],(i!=vec_n-1?middle:'\\n'));\n}\nvoid array_show(vector<LL> &vec_s,int vec_n=-1,char middle=' '){\n if(vec_n==-1)vec_n=vec_s.size();\n for(int i=0;i<vec_n;i++)printf(\"%lld%c\",vec_s[i],(i!=vec_n-1?middle:'\\n'));\n}\n\nlong double vec[111][111];\nvector<vector<long double>> dis;\npriority_queue<DP,vector<DP>,greater<DP>> q1;\n\nint main(){\n int n,m;\n int i,j,k;\n int a,b,c;\n long double da,db,dc;\n cin>>m>>n;\n dis.assign(n,vector<long double>(n,INF));\n for(i=0;i<n;i++){\n for(j=0;j<m;j++){\n cin>>vec[i][j];\n }\n }\n for(i=0;i<n;i++){\n for(j=0;j<n;j++){\n if(i==j)continue;\n da=0,db=0;\n for(k=0;k<m;k++){\n if(vec[i][k]==0)continue;\n da+=pow(vec[i][k],2),db+=2*vec[i][k]*vec[j][k];\n }\n if(da>0)db/=2*da;\n da=0;\n for(k=0;k<m;k++){\n da+=pow(vec[j][k]-vec[i][k]*db,2);\n }\n dis[i][j]=da;\n }\n }\n long double ds=INF;\n for(i=0;i<n;i++){\n vector<char> used(n);\n da=0;\n for(j=0;j<m;j++){\n da+=pow(vec[i][j],2);\n }\n q1.push(make_pair(da,i));\n db=0;\n while(!q1.empty()){\n da=q1.top().first,a=q1.top().second,q1.pop();\n if(used[a])continue;\n used[a]=1;\n db+=da;\n for(j=0;j<n;j++){\n if(a==j)continue;\n q1.push(make_pair(dis[a][j],j));\n }\n }\n ds=min(ds,db);\n }\n cout<<fixed<<setprecision(15)<<ds<<endl;\n}", "accuracy": 0.061224489795918366, "time_ms": 50, "memory_kb": 3488, "score_of_the_acc": -0.0537, "final_rank": 16 }, { "submission_id": "aoj_2309_4809757", "code_snippet": "#include<iostream>\n#include<string>\n#include<cstdio>\n#include<vector>\n#include<cmath>\n#include<algorithm>\n#include<functional>\n#include<iomanip>\n#include<queue>\n#include<ciso646>\n#include<random>\n#include<map>\n#include<set>\n#include<bitset>\n#include<stack>\n#include<unordered_map>\n#include<utility>\n#include<cassert>\n#include<complex>\n#include<numeric>\n#include<array>\nusing namespace std;\n\n//#define int long long\ntypedef long long ll;\n\ntypedef unsigned long long ul;\ntypedef unsigned int ui;\nconstexpr ll mod = 1000000007;\nconst ll INF = mod * mod;\ntypedef pair<int, int>P;\n#define stop char nyaa;cin>>nyaa;\n#define rep(i,n) for(int i=0;i<n;i++)\n#define per(i,n) for(int i=n-1;i>=0;i--)\n#define Rep(i,sta,n) for(int i=sta;i<n;i++)\n#define rep1(i,n) for(int i=1;i<=n;i++)\n#define per1(i,n) for(int i=n;i>=1;i--)\n#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)\n#define all(v) (v).begin(),(v).end()\ntypedef pair<ll, ll> LP;\ntypedef long double ld;\ntypedef pair<ld, ld> LDP;\nconst ld eps = 1e-12;\nconst ld pi = acosl(-1.0);\n\nll mod_pow(ll x, ll n, ll m = mod) {\n\tll res = 1;\n\twhile (n) {\n\t\tif (n & 1)res = res * x % m;\n\t\tx = x * x % m; n >>= 1;\n\t}\n\treturn res;\n}\nstruct modint {\n\tll n;\n\tmodint() :n(0) { ; }\n\tmodint(ll m) :n(m) {\n\t\tif (n >= mod)n %= mod;\n\t\telse if (n < 0)n = (n % mod + mod) % mod;\n\t}\n\toperator int() { return n; }\n};\nbool operator==(modint a, modint b) { return a.n == b.n; }\nmodint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }\nmodint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }\nmodint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }\nmodint operator+(modint a, modint b) { return a += b; }\nmodint operator-(modint a, modint b) { return a -= b; }\nmodint operator*(modint a, modint b) { return a *= b; }\nmodint operator^(modint a, ll n) {\n\tif (n == 0)return modint(1);\n\tmodint res = (a * a) ^ (n / 2);\n\tif (n % 2)res = res * a;\n\treturn res;\n}\n\nll inv(ll a, ll p) {\n\treturn (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);\n}\nmodint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }\n\nconst int max_n = 1000;\nmodint fact[max_n], factinv[max_n];\nvoid init_f() {\n\tfact[0] = modint(1);\n\tfor (int i = 0; i < max_n - 1; i++) {\n\t\tfact[i + 1] = fact[i] * modint(i + 1);\n\t}\n\tfactinv[max_n - 1] = modint(1) / fact[max_n - 1];\n\tfor (int i = max_n - 2; i >= 0; i--) {\n\t\tfactinv[i] = factinv[i + 1] * modint(i + 1);\n\t}\n}\nmodint comb(int a, int b) {\n\tif (a < 0 || b < 0 || a < b)return 0;\n\treturn fact[a] * factinv[b] * factinv[a - b];\n}\n\nint gcd(int a, int b) {\n\tif (a < b)swap(a, b);\n\twhile (b) {\n\t\tint r = a % b; a = b; b = r;\n\t}\n\treturn a;\n}\nstruct uf {\nprivate:\n\tvector<int> par, ran;\npublic:\n\tuf(int n) {\n\t\tpar.resize(n, 0);\n\t\tran.resize(n, 0);\n\t\trep(i, n) {\n\t\t\tpar[i] = i;\n\t\t}\n\t}\n\tint find(int x) {\n\t\tif (par[x] == x)return x;\n\t\telse return par[x] = find(par[x]);\n\t}\n\tvoid unite(int x, int y) {\n\t\tx = find(x), y = find(y);\n\t\tif (x == y)return;\n\t\tif (ran[x] < ran[y]) {\n\t\t\tpar[x] = y;\n\t\t}\n\t\telse {\n\t\t\tpar[y] = x;\n\t\t\tif (ran[x] == ran[y])ran[x]++;\n\t\t}\n\t}\n\tbool same(int x, int y) {\n\t\treturn find(x) == find(y);\n\t}\n};\n\nusing tval = ld;\nstruct edge {\n\tint to; tval cost;\n};\n\nvector<P> chu_liu_edmonds(vector<vector<edge>>&G, vector<bool> exi,int root=-1) {\n\t//cout << \"wow\\n\";\n\tint n = G.size();\n\tif (root < 0) {\n\t\troot = n;\n\t\tG.push_back({});\n\t\trep(i, n)G[root].push_back({ i,INF/10000 });\n\t\tn++;\n\t\texi.push_back(true);\n\t}\n\tvector<vector<edge>> rG(n);\n\trep(i, n)if(exi[i])for (edge e : G[i]) {\n\t\trG[e.to].push_back({ i,e.cost });\n\t}\n\tvector<int> par(n);\n\tvector<tval> memo(n);\n\trep(i, n)if (exi[i] && i != root) {\n\t\ttval mi = INF;\n\t\tfor (edge e: rG[i]) {\n\t\t\tif (e.cost < mi) {\n\t\t\t\tmi = e.cost;\n\t\t\t\tpar[i] = e.to;\n\t\t\t}\n\t\t}\n\t\t//cout << par[i] << \" \" << i << \"\\n\";\n\t\tmemo[i] = mi;\n\t}\n\tvector<bool> used(n);\n\tvector<bool> cused(n);\n\tvector<int> ts;\n\tused[root] = true;\n\trep(i, n)if (exi[i] && i != root) {\n\t\tif (used[i])continue;\n\t\tvector<int> ids;\n\t\tint cur = i;\n\t\tcused[i] = true;\n\t\tids.push_back(i);\n\t\tbool f = false;\n\t\twhile (true) {\n\t\t\tcur = par[cur];\n\t\t\tif (used[cur]) {\n\t\t\t\tfor (int id : ids) {\n\t\t\t\t\tcused[id] = false;\n\t\t\t\t\tused[id] = true;\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\telse if (cused[cur]) {\n\t\t\t\tf = true;\n\t\t\t\trep(j, ids.size())if (ids[j] == cur) {\n\t\t\t\t\tids.erase(ids.begin(), ids.begin() + j);\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\telse {\n\t\t\t\tcused[cur] = true;\n\t\t\t\tids.push_back(cur);\n\t\t\t}\n\t\t}\n\t\tif (f) {\n\t\t\tts = ids; break;\n\t\t}\n\t}\n\tif (ts.empty()) {\n\t\tvector<P> res; rep(i, n)if (exi[i] && i != root)res.push_back({ par[i],i });\n\t\treturn res;\n\t}\n\telse {\n\t\tint cr = ts[0];\n\t\tvector<tval> mifr(n, INF), mito(n, INF);\n\t\tvector<int> idfr(n, -1), idto(n, -1);\n\t\t\n\t\tfor (int id : ts) {\n\t\t\tfor (edge e : G[id]) {\n\t\t\t\tif (e.cost < mito[e.to]) {\n\t\t\t\t\tmito[e.to] = e.cost;\n\t\t\t\t\tidto[e.to] = id;\n\t\t\t\t}\n\t\t\t}\n\t\t\tfor (edge e : rG[id]) {\n\t\t\t\tif (e.cost-memo[id] < mifr[e.to]) {\n\t\t\t\t\tmifr[e.to] = e.cost-memo[id];\n\t\t\t\t\tidfr[e.to] = id;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tvector<bool> nexi = exi;\n\t\tfor (int id : ts) {\n\t\t\tnexi[id] = false;\n\t\t\tG[id].clear();\n\t\t}\n\t\trep(i, n)if (nexi[i]) {\n\t\t\tvector<edge> nw;\n\t\t\tfor (edge e : G[i])if (e.to != cr)nw.push_back(e);\n\t\t\tswap(nw, G[i]);\n\t\t}\n\t\tnexi[cr] = true;\n\t\trep(i, n)if (i!=cr&&nexi[i]) {\n\t\t\tif (idfr[i] >= 0) {\n\t\t\t\tG[i].push_back({ cr,mifr[i] });\n\t\t\t}\n\t\t\tif (idto[i] >= 0) {\n\t\t\t\tG[cr].push_back({ i,mito[i] });\n\t\t\t}\n\t\t}\n\t\tvector<P> np = chu_liu_edmonds(G, nexi, root);\n\t\t//cout << \"wow\" <<cr<<\"\\n\";\n\t\t//cout << memo[0] << \"\\n\";\n\t\t//cout << mifr[3] << \" \" << idfr[3] << \"\\n\";\n\t\t/*for (edge e : G[3]) {\n\t\t\tcout << e.to << \" \" << e.cost << \"\\n\";\n\t\t}\n\t\trep(i, np.size())cout << np[i].first << \" \" << np[i].second << \"\\n\";\n\t\t*/\n\t\tint chk = -1;\n\t\trep(i, np.size()) {\n\t\t\tif (np[i].second == cr) {\n\t\t\t\tchk = idfr[np[i].first];\n\t\t\t\tnp[i].second = chk;\n\t\t\t}\n\t\t\telse if (np[i].first == cr) {\n\t\t\t\tnp[i].first = idto[np[i].second];\n\t\t\t}\n\t\t}\n\t\tts.push_back(ts[0]);\n\t\trep(i, ts.size() - 1) {\n\t\t\tif (ts[i] != chk) {\n\t\t\t\tnp.push_back({ ts[i + 1], ts[i] });\n\t\t\t}\n\t\t}\n\t\treturn np;\n\t}\n}\n\nld calc(vector<ld> dp, vector<ld> p) {\n\tld le = -mod, ri = mod;\n\tauto cal = [&](ld t)->ld {\n\t\tld sum = 0;\n\t\trep(i, dp.size()) {\n\t\t\tld x = p[i] + t * dp[i];\n\t\t\tsum += x * x;\n\t\t}\n\t\treturn sum;\n\t};\n\trep(i, 200) {\n\t\tld ml = (le*2 + ri) / 3.0;\n\t\tld mr = (le+ ri * 2) / 3.0;\n\t\tld cl = cal(ml);\n\t\tld cr = cal(mr);\n\t\tif (cl < cr) {\n\t\t\tri = mr;\n\t\t}\n\t\telse le = ml;\n\t}\n\tld sum = 0;\n\trep(i, p.size())sum += p[i] * p[i];\n\tld mem = cal(le);\n\tsum -= mem;\n\treturn sum;\n}\n\nvoid solve() {\n\tint n, m; cin >> m >> n;\n\tvector<vector<ld>> vs(n, vector<ld>(m));\n\tvector<vector<edge>> G(n);\n\trep(i, n)rep(j, m) {\n\t\tcin >> vs[i][j];\n\t}\n\trep(i, n)Rep(j, i + 1, n) {\n\t\t//cout << i << \" \" << j << \" \"<<calc(vs[i], vs[j]) << \" \" << calc(vs[j], vs[i]) << \"\\n\";\n\t\tG[i].push_back({ j,-calc(vs[i],vs[j]) });\n\t\tG[j].push_back({ i,-calc(vs[j],vs[i]) });\n\t}\n\tvector<bool> exi(n, true);\n\tld ma = INF;\n\t\tvector<vector<edge>> nG = G;\n\t\t//cout << \"hello\\n\";\n\t\tvector<P> res = chu_liu_edmonds(nG, exi);\n\t\tmap<P, bool> mp;\n\t\tfor (P p : res) {\n\t\t\t//cout << p.first << \" \" << p.second << \"\\n\";\n\t\t\tmp[p] = true;\n\t\t}\n\t\tld sum = 0;\n\t\trep(i, n)for (edge e : G[i]) {\n\t\t\tif (mp[{i,e.to}])sum += e.cost;\n\t\t}ma = min(ma, sum);\n\t\n\trep(i, n) {\n\t\tld c = 0;\n\t\trep(j, vs[i].size()) {\n\t\t\tc += vs[i][j] * vs[i][j];\n\t\t}\n\t\tma +=c;\n\t}\n\tcout << ma << \"\\n\";\n}\n\n\nsigned main() {\n\tios::sync_with_stdio(false);\n\tcin.tie(0);\n\tcout << fixed << setprecision(8);\n\t//init_f(); \n\t//init();\n\t//expr();\n\t//int t; cin >> t; rep(i, t)\n\tsolve();\n\treturn 0;\n}", "accuracy": 1, "time_ms": 450, "memory_kb": 27940, "score_of_the_acc": -1.4632, "final_rank": 10 }, { "submission_id": "aoj_2309_4809599", "code_snippet": "#include<iostream>\n#include<string>\n#include<cstdio>\n#include<vector>\n#include<cmath>\n#include<algorithm>\n#include<functional>\n#include<iomanip>\n#include<queue>\n#include<ciso646>\n#include<random>\n#include<map>\n#include<set>\n#include<bitset>\n#include<stack>\n#include<unordered_map>\n#include<utility>\n#include<cassert>\n#include<complex>\n#include<numeric>\n#include<array>\nusing namespace std;\n\n//#define int long long\ntypedef long long ll;\n\ntypedef unsigned long long ul;\ntypedef unsigned int ui;\nconstexpr ll mod = 1000000007;\nconst ll INF = mod * mod;\ntypedef pair<int, int>P;\n#define stop char nyaa;cin>>nyaa;\n#define rep(i,n) for(int i=0;i<n;i++)\n#define per(i,n) for(int i=n-1;i>=0;i--)\n#define Rep(i,sta,n) for(int i=sta;i<n;i++)\n#define rep1(i,n) for(int i=1;i<=n;i++)\n#define per1(i,n) for(int i=n;i>=1;i--)\n#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)\n#define all(v) (v).begin(),(v).end()\ntypedef pair<ll, ll> LP;\ntypedef long double ld;\ntypedef pair<ld, ld> LDP;\nconst ld eps = 1e-12;\nconst ld pi = acosl(-1.0);\n\nll mod_pow(ll x, ll n, ll m = mod) {\n\tll res = 1;\n\twhile (n) {\n\t\tif (n & 1)res = res * x % m;\n\t\tx = x * x % m; n >>= 1;\n\t}\n\treturn res;\n}\nstruct modint {\n\tll n;\n\tmodint() :n(0) { ; }\n\tmodint(ll m) :n(m) {\n\t\tif (n >= mod)n %= mod;\n\t\telse if (n < 0)n = (n % mod + mod) % mod;\n\t}\n\toperator int() { return n; }\n};\nbool operator==(modint a, modint b) { return a.n == b.n; }\nmodint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }\nmodint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }\nmodint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }\nmodint operator+(modint a, modint b) { return a += b; }\nmodint operator-(modint a, modint b) { return a -= b; }\nmodint operator*(modint a, modint b) { return a *= b; }\nmodint operator^(modint a, ll n) {\n\tif (n == 0)return modint(1);\n\tmodint res = (a * a) ^ (n / 2);\n\tif (n % 2)res = res * a;\n\treturn res;\n}\n\nll inv(ll a, ll p) {\n\treturn (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);\n}\nmodint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }\n\nconst int max_n = 1000;\nmodint fact[max_n], factinv[max_n];\nvoid init_f() {\n\tfact[0] = modint(1);\n\tfor (int i = 0; i < max_n - 1; i++) {\n\t\tfact[i + 1] = fact[i] * modint(i + 1);\n\t}\n\tfactinv[max_n - 1] = modint(1) / fact[max_n - 1];\n\tfor (int i = max_n - 2; i >= 0; i--) {\n\t\tfactinv[i] = factinv[i + 1] * modint(i + 1);\n\t}\n}\nmodint comb(int a, int b) {\n\tif (a < 0 || b < 0 || a < b)return 0;\n\treturn fact[a] * factinv[b] * factinv[a - b];\n}\n\nint gcd(int a, int b) {\n\tif (a < b)swap(a, b);\n\twhile (b) {\n\t\tint r = a % b; a = b; b = r;\n\t}\n\treturn a;\n}\nstruct uf {\nprivate:\n\tvector<int> par, ran;\npublic:\n\tuf(int n) {\n\t\tpar.resize(n, 0);\n\t\tran.resize(n, 0);\n\t\trep(i, n) {\n\t\t\tpar[i] = i;\n\t\t}\n\t}\n\tint find(int x) {\n\t\tif (par[x] == x)return x;\n\t\telse return par[x] = find(par[x]);\n\t}\n\tvoid unite(int x, int y) {\n\t\tx = find(x), y = find(y);\n\t\tif (x == y)return;\n\t\tif (ran[x] < ran[y]) {\n\t\t\tpar[x] = y;\n\t\t}\n\t\telse {\n\t\t\tpar[y] = x;\n\t\t\tif (ran[x] == ran[y])ran[x]++;\n\t\t}\n\t}\n\tbool same(int x, int y) {\n\t\treturn find(x) == find(y);\n\t}\n};\n\nusing tval = ld;\nstruct edge {\n\tint to; tval cost;\n};\n\nvector<P> chu_liu_edmonds(vector<vector<edge>>&G, vector<bool> exi,int root=-1) {\n\t//cout << \"wow\\n\";\n\tint n = G.size();\n\tif (root < 0) {\n\t\troot = n;\n\t\tG.push_back({});\n\t\trep(i, n)G[root].push_back({ i,INF/10000 });\n\t\tn++;\n\t\texi.push_back(true);\n\t}\n\tvector<vector<edge>> rG(n);\n\trep(i, n)if(exi[i])for (edge e : G[i]) {\n\t\trG[e.to].push_back({ i,e.cost });\n\t}\n\tvector<int> par(n);\n\tvector<tval> memo(n);\n\trep(i, n)if (exi[i] && i != root) {\n\t\ttval mi = INF;\n\t\tfor (edge e: rG[i]) {\n\t\t\tif (e.cost < mi) {\n\t\t\t\tmi = e.cost;\n\t\t\t\tpar[i] = e.to;\n\t\t\t}\n\t\t}\n\t\t//cout << par[i] << \" \" << i << \"\\n\";\n\t\tmemo[i] = mi;\n\t}\n\tvector<bool> used(n);\n\tvector<bool> cused(n);\n\tvector<int> ts;\n\tused[root] = true;\n\trep(i, n)if (exi[i] && i != root) {\n\t\tif (used[i])continue;\n\t\tvector<int> ids;\n\t\tint cur = i;\n\t\tcused[i] = true;\n\t\tids.push_back(i);\n\t\tbool f = false;\n\t\twhile (true) {\n\t\t\tcur = par[cur];\n\t\t\tif (used[cur]) {\n\t\t\t\tfor (int id : ids) {\n\t\t\t\t\tcused[id] = false;\n\t\t\t\t\tused[id] = true;\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\telse if (cused[cur]) {\n\t\t\t\tf = true;\n\t\t\t\trep(j, ids.size())if (ids[j] == cur) {\n\t\t\t\t\tids.erase(ids.begin(), ids.begin() + j);\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\telse {\n\t\t\t\tcused[cur] = true;\n\t\t\t\tids.push_back(cur);\n\t\t\t}\n\t\t}\n\t\tif (f) {\n\t\t\tts = ids; break;\n\t\t}\n\t}\n\tif (ts.empty()) {\n\t\tvector<P> res; rep(i, n)if (exi[i] && i != root)res.push_back({ par[i],i });\n\t\treturn res;\n\t}\n\telse {\n\t\tint cr = ts[0];\n\t\tvector<tval> mifr(n, INF), mito(n, INF);\n\t\tvector<int> idfr(n, -1), idto(n, -1);\n\t\t\n\t\tfor (int id : ts) {\n\t\t\tfor (edge e : G[id]) {\n\t\t\t\tif (e.cost < mito[e.to]) {\n\t\t\t\t\tmito[e.to] = e.cost;\n\t\t\t\t\tidto[e.to] = id;\n\t\t\t\t}\n\t\t\t}\n\t\t\tfor (edge e : rG[id]) {\n\t\t\t\tif (e.cost-memo[id] < mifr[e.to]) {\n\t\t\t\t\tmifr[e.to] = e.cost-memo[id];\n\t\t\t\t\tidfr[e.to] = id;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tvector<bool> nexi = exi;\n\t\tfor (int id : ts) {\n\t\t\tnexi[id] = false;\n\t\t\tG[id].clear();\n\t\t}\n\t\tnexi[cr] = true;\n\t\trep(i, n)if (i!=cr&&nexi[i]) {\n\t\t\tif (idfr[i] >= 0) {\n\t\t\t\tG[i].push_back({ cr,mifr[i] });\n\t\t\t}\n\t\t\tif (idto[i] >= 0) {\n\t\t\t\tG[cr].push_back({ i,mito[i] });\n\t\t\t}\n\t\t}\n\t\tvector<P> np = chu_liu_edmonds(G, nexi, root);\n\t\tint chk = -1;\n\t\trep(i, np.size()) {\n\t\t\tif (np[i].second == cr) {\n\t\t\t\tchk = idfr[np[i].first];\n\t\t\t\tnp[i].second = chk;\n\t\t\t}\n\t\t\telse if (np[i].first == cr) {\n\t\t\t\tnp[i].first = idto[np[i].second];\n\t\t\t}\n\t\t}\n\t\tts.push_back(ts[0]);\n\t\trep(i, ts.size() - 1) {\n\t\t\tif (ts[i] != chk) {\n\t\t\t\tnp.push_back({ ts[i + 1], ts[i] });\n\t\t\t}\n\t\t}\n\t\treturn np;\n\t}\n}\n\nld calc(vector<ld> dp, vector<ld> p) {\n\tld le = -3, ri = 3;\n\tauto cal = [&](ld t) {\n\t\tld sum = 0;\n\t\trep(i, dp.size()) {\n\t\t\tld x = p[i] + t * dp[i];\n\t\t\tsum += x * x;\n\t\t}\n\t\treturn sqrtl(sum);\n\t};\n\trep(i, 60) {\n\t\tld ml = (le * 5 + ri * 4) / 9.0;\n\t\tld mr = (le * 4 + ri * 5) / 9.0;\n\t\tld cl = cal(ml);\n\t\tld cr = cal(mr);\n\t\tif (cl < cr) {\n\t\t\tri = mr;\n\t\t}\n\t\telse le = ml;\n\t}\n\tld sum = 0;\n\trep(i, p.size())sum += p[i] * p[i];\n\tld mem = cal(le);\n\tsum -= mem * mem;\n\treturn sum;\n}\n\nvoid solve() {\n\tint n, m; cin >> m >> n;\n\tvector<vector<ld>> vs(n, vector<ld>(m));\n\tvector<vector<edge>> G(n);\n\trep(i, n)rep(j, m) {\n\t\tcin >> vs[i][j];\n\t}\n\trep(i, n)Rep(j, i + 1, n) {\n\t\t//cout << i << \" \" << j << \" \"<<calc(vs[i], vs[j]) << \" \" << calc(vs[j], vs[i]) << \"\\n\";\n\t\tG[i].push_back({ j,-calc(vs[i],vs[j]) });\n\t\tG[j].push_back({ i,-calc(vs[j],vs[i]) });\n\t}\n\tvector<bool> exi(n, true);\n\tld ma = 0;\n\trep(i, n) {\n\t\tvector<vector<edge>> nG = G;\n\t\t//cout << \"hello\\n\";\n\t\tvector<P> res = chu_liu_edmonds(nG, exi,i);\n\t\tmap<P, bool> mp;\n\t\tfor (P p : res) {\n\t\t\t//cout << p.first << \" \" << p.second << \"\\n\";\n\t\t\tmp[p] = true;\n\t\t}\n\t\tld sum = 0;\n\t\trep(i, n)for (edge e : G[i]) {\n\t\t\tif (mp[{i, e.to}])sum += e.cost;\n\t\t}ma = min(ma, sum);\n\t}\n\t\n\trep(i, n) {\n\t\tld c = 0;\n\t\trep(j, vs[i].size()) {\n\t\t\tc += vs[i][j] * vs[i][j];\n\t\t}\n\t\tma +=c;\n\t}\n\tcout << ma << \"\\n\";\n}\n\n\nsigned main() {\n\tios::sync_with_stdio(false);\n\tcin.tie(0);\n\tcout << fixed << setprecision(8);\n\t//init_f(); \n\t//init();\n\t//expr();\n\t//int t; cin >> t; rep(i, t)\n\tsolve();\n\treturn 0;\n}", "accuracy": 0.061224489795918366, "time_ms": 520, "memory_kb": 15224, "score_of_the_acc": -1.0229, "final_rank": 20 }, { "submission_id": "aoj_2309_4807312", "code_snippet": "#include<iostream>\n#include<string>\n#include<cstdio>\n#include<vector>\n#include<cmath>\n#include<algorithm>\n#include<functional>\n#include<iomanip>\n#include<queue>\n#include<ciso646>\n#include<random>\n#include<map>\n#include<set>\n#include<bitset>\n#include<stack>\n#include<unordered_map>\n#include<utility>\n#include<cassert>\n#include<complex>\n#include<numeric>\n#include<array>\nusing namespace std;\n\n//#define int long long\ntypedef long long ll;\n\ntypedef unsigned long long ul;\ntypedef unsigned int ui;\nconstexpr ll mod = 1000000007;\nconst ll INF = mod * mod;\ntypedef pair<int, int>P;\n#define stop char nyaa;cin>>nyaa;\n#define rep(i,n) for(int i=0;i<n;i++)\n#define per(i,n) for(int i=n-1;i>=0;i--)\n#define Rep(i,sta,n) for(int i=sta;i<n;i++)\n#define rep1(i,n) for(int i=1;i<=n;i++)\n#define per1(i,n) for(int i=n;i>=1;i--)\n#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)\n#define all(v) (v).begin(),(v).end()\ntypedef pair<ll, ll> LP;\ntypedef long double ld;\ntypedef pair<ld, ld> LDP;\nconst ld eps = 1e-12;\nconst ld pi = acosl(-1.0);\n\nll mod_pow(ll x, ll n, ll m = mod) {\n\tll res = 1;\n\twhile (n) {\n\t\tif (n & 1)res = res * x % m;\n\t\tx = x * x % m; n >>= 1;\n\t}\n\treturn res;\n}\nstruct modint {\n\tll n;\n\tmodint() :n(0) { ; }\n\tmodint(ll m) :n(m) {\n\t\tif (n >= mod)n %= mod;\n\t\telse if (n < 0)n = (n % mod + mod) % mod;\n\t}\n\toperator int() { return n; }\n};\nbool operator==(modint a, modint b) { return a.n == b.n; }\nmodint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }\nmodint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }\nmodint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }\nmodint operator+(modint a, modint b) { return a += b; }\nmodint operator-(modint a, modint b) { return a -= b; }\nmodint operator*(modint a, modint b) { return a *= b; }\nmodint operator^(modint a, ll n) {\n\tif (n == 0)return modint(1);\n\tmodint res = (a * a) ^ (n / 2);\n\tif (n % 2)res = res * a;\n\treturn res;\n}\n\nll inv(ll a, ll p) {\n\treturn (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);\n}\nmodint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }\n\nconst int max_n = 1000;\nmodint fact[max_n], factinv[max_n];\nvoid init_f() {\n\tfact[0] = modint(1);\n\tfor (int i = 0; i < max_n - 1; i++) {\n\t\tfact[i + 1] = fact[i] * modint(i + 1);\n\t}\n\tfactinv[max_n - 1] = modint(1) / fact[max_n - 1];\n\tfor (int i = max_n - 2; i >= 0; i--) {\n\t\tfactinv[i] = factinv[i + 1] * modint(i + 1);\n\t}\n}\nmodint comb(int a, int b) {\n\tif (a < 0 || b < 0 || a < b)return 0;\n\treturn fact[a] * factinv[b] * factinv[a - b];\n}\n\nint gcd(int a, int b) {\n\tif (a < b)swap(a, b);\n\twhile (b) {\n\t\tint r = a % b; a = b; b = r;\n\t}\n\treturn a;\n}\nstruct uf {\nprivate:\n\tvector<int> par, ran;\npublic:\n\tuf(int n) {\n\t\tpar.resize(n, 0);\n\t\tran.resize(n, 0);\n\t\trep(i, n) {\n\t\t\tpar[i] = i;\n\t\t}\n\t}\n\tint find(int x) {\n\t\tif (par[x] == x)return x;\n\t\telse return par[x] = find(par[x]);\n\t}\n\tvoid unite(int x, int y) {\n\t\tx = find(x), y = find(y);\n\t\tif (x == y)return;\n\t\tif (ran[x] < ran[y]) {\n\t\t\tpar[x] = y;\n\t\t}\n\t\telse {\n\t\t\tpar[y] = x;\n\t\t\tif (ran[x] == ran[y])ran[x]++;\n\t\t}\n\t}\n\tbool same(int x, int y) {\n\t\treturn find(x) == find(y);\n\t}\n};\nld calc(vector<ld> dp,vector<ld> p) {\n\tld le = -3, ri = 3;\n\tauto cal = [&](ld t) {\n\t\tld sum = 0;\n\t\trep(i, dp.size()) {\n\t\t\tld x = p[i] + t * dp[i];\n\t\t\tsum += x * x;\n\t\t}\n\t\treturn sqrtl(sum);\n\t};\n\trep(i, 60) {\n\t\tld ml = (le * 5 + ri * 4) / 9.0;\n\t\tld mr = (le * 4 + ri * 5) / 9.0;\n\t\tld cl = cal(ml);\n\t\tld cr = cal(mr);\n\t\tif (cl < cr) {\n\t\t\tri = mr;\n\t\t}\n\t\telse le = ml;\n\t}\n\tld sum = 0;\n\trep(i, p.size())sum += p[i] * p[i];\n\tld mem = cal(le);\n\tsum -= mem * mem;\n\treturn sum;\n}\nvoid solve() {\n\tint n, m; cin >> m >> n;\n\tvector<vector<ld>> vs(n,vector<ld>(m));\n\trep(i, n)rep(j, m) {\n\t\tcin >> vs[i][j];\n\t}\n\tvector < pair<ld,P>> v;\n\trep(i, n)Rep(j, i + 1, n) {\n\t\tv.push_back({ calc(vs[i],vs[j]),{i,j} });\n\t\tv.push_back({ calc(vs[j],vs[i]),{j,i} });\n\t}\n\tld ans = 0;\n\trep(i, n) {\n\t\tld sum = 0; rep(j, m)sum += vs[i][j] * vs[i][j];\n\t\tans += sum;\n\t}\n\tsort(all(v), greater<pair<ld, P>>());\n\tuf u(n);\n\tvector<bool> used(n);\n\tint tmp = 0;\n\trep(i, v.size()) {\n\t\tint l = v[i].second.first, r = v[i].second.second;\n\t\tif (!u.same(l,r)&&!used[r]) {\n\t\t\tu.unite(l, r); used[r] = true;\n\t\t\tans -= v[i].first;\n\n\t\t\t//cout << v[i].first << \" \" << l << \" \" << r << \"\\n\";\n\t\t}\n\t}\n\tcout << ans << \"\\n\";\n}\n\n\nsigned main() {\n\tios::sync_with_stdio(false);\n\tcin.tie(0);\n\tcout << fixed << setprecision(8);\n\t//init_f(); \n\t//init();\n\t//expr();\n\t//int t; cin >> t; rep(i, t)\n\tsolve();\n\treturn 0;\n}", "accuracy": 0.061224489795918366, "time_ms": 30, "memory_kb": 3480, "score_of_the_acc": -0.0324, "final_rank": 14 }, { "submission_id": "aoj_2309_3221148", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))\n#define all(x) (x).begin(),(x).end()\n#define pb push_back\n#define fi first\n#define se second\n#define dbg(x) cout<<#x\" = \"<<((x))<<endl\ntemplate<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<\"(\"<<p.fi<<\",\"<<p.se<<\")\";return o;}\ntemplate<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<\"[\";for(T t:v){o<<t<<\",\";}o<<\"]\";return o;}\n\n// (重み, u->v)\nusing Edge = pair<double,pair<int,int>>;\n\nconst double INF = 19191919;\n\n// 辺集合, 頂点数, 根\ndouble Chu_Liu_Edmonds(const vector<Edge> es, int V, int root){\n // 頂点iに入ってくる辺(u->i)の中で、最小のコストの辺を管理 (コスト, u)\n vector<pair<double,int>> mn(V, {INF,-1});\n for(Edge e:es){\n int u = e.se.fi, v = e.se.se;\n mn[v] = min(mn[v], {e.fi, u});\n }\n mn[root] = {0,-1};\n\n // 連結成分に分ける(1つの閉路に対して1つの番号を与える)\n vector<int> cmp(V);\n // この連結成分が閉路であるか\n vector<bool> cycle(V,false);\n int cc = 0;\n\n vector<bool> vis(V);\n rep(i,V){\n if(vis[i]) continue;\n\n vector<int> path;\n int now = i;\n\n while(now!=-1 && !vis[now]){\n vis[now] = true;\n path.pb(now);\n\n // 今張っている辺を逆方向にたどる\n now = mn[now].se;\n }\n\n if(now != -1){\n bool in_cycle = false;\n for(int j:path){\n cmp[j] = cc;\n if(j == now){\n // 閉路の始点(これ以降のpathに入ってる頂点が閉路をなす)\n in_cycle = true;\n cycle[cc] = true;\n }\n\n if(!in_cycle) ++cc;\n }\n if(in_cycle) ++cc;\n }\n else{\n // 閉路なし\n for(int j:path) cmp[j] = cc++;\n }\n }\n\n if(cc == V){\n // すべての頂点が違う連結成分に分かれたので 閉路なし\n double ans = 0;\n rep(i,V) ans += mn[i].fi;\n return ans;\n }\n\n // 閉路分のコスト\n double cycle_cost = 0;\n rep(i,V){\n if(i!=root && cycle[cmp[i]]) cycle_cost += mn[i].fi;\n }\n\n // コストを再設定\n vector<Edge> nes;\n for(Edge e:es){\n int u = e.se.fi, v = e.se.se;\n int cu = cmp[u], cv = cmp[v];\n\n if(cu == cv){\n // 閉路内の辺は無視\n continue;\n }\n else if(cycle[cv]){\n // コストを再設定\n nes.pb({e.fi - mn[v].fi, {cu,cv}});\n }\n else{\n nes.pb({e.fi, {cu,cv}});\n }\n }\n\n return cycle_cost + Chu_Liu_Edmonds(nes, cc, cmp[root]);\n};\n\ndouble size(const vector<double> &v){\n int n = v.size();\n double ret = 0;\n rep(i,n) ret += v[i]*v[i];\n return ret;\n}\n\nvector<double> getv(const vector<double> &v, const vector<double> &u, double r){\n int n = v.size();\n vector<double> ret(n);\n rep(i,n) ret[i] = v[i]+u[i]*r;\n return ret;\n}\n\nconst double LIM = 100;\ndouble calc(const vector<double> &v, const vector<double> &u){\n int n = v.size();\n\n double l=-LIM, r=LIM;\n rep(loop,60){\n double m1 = (2*l+r)/3, m2 = (l+2*r)/3;\n if(size(getv(v,u,m1)) < size(getv(v,u,m2))) r = m2;\n else l = m1;\n }\n\n return size(getv(v,u,r));\n}\n\nint main(){\n int n,m;\n cin >>n >>m;\n\n vector<vector<double>> v(m);\n rep(i,m){\n vector<double> tmp(n);\n rep(j,n) cin >>tmp[j];\n v[i] = tmp;\n }\n\n vector<Edge> Es;\n rep(i,m)rep(j,m)if(i!=j){\n Es.pb({calc(v[j],v[i]),{i,j}});\n // printf(\" %d %d -> %f\\n\",i,j,calc(v[j],v[i]));\n }\n\n double ans = INF;\n rep(i,m){\n double res = Chu_Liu_Edmonds(Es,m,i);\n ans = min(ans, size(v[i])+res);\n // printf(\" %d:: %f\\n\",i,res);\n }\n printf(\"%.10f\\n\", ans);\n return 0;\n}", "accuracy": 1, "time_ms": 960, "memory_kb": 23768, "score_of_the_acc": -1.8314, "final_rank": 11 }, { "submission_id": "aoj_2309_3179710", "code_snippet": "#include <bits/stdc++.h>\n#define re(i,n) for(int i=0;i<n;i++)\nusing namespace std;\n\nstruct UnionFind{\n int n;\n vector<int> r,p;\n UnionFind(){}\n UnionFind(int sz):n(sz),r(sz,1),p(sz,0){iota(p.begin(),p.end(),0);}\n int find(int x){\n return (x==p[x]?x:p[x]=find(p[x]));\n }\n bool same(int x,int y){\n return find(x)==find(y);\n }\n void unite(int x,int y){\n x=find(x);y=find(y);\n if(x==y) return;\n r[x]+=r[y];\n p[y]=x;\n }\n};\n\ntemplate<typename T, typename E>\nstruct SkewHeap{\n using G = function<T(T,E)>;\n using H = function<E(E,E)>;\n using C = function<bool(T,T)>;\n G g;\n H h;\n C c;\n T INF;\n E ei;\n SkewHeap(G g,H h,C c,T INF,E ei):g(g),h(h),c(c),INF(INF),ei(ei){}\n \n struct Node{\n Node *l,*r;\n T val;\n E add;\n Node(T val,E add):val(val),add(add){l=r=nullptr;}\n };\n\n void eval(Node *a){\n if(a==nullptr) return;\n if(a->add==ei) return;\n if(a->l) a->l->add=h(a->l->add,a->add);\n if(a->r) a->r->add=h(a->r->add,a->add);\n a->val=g(a->val,a->add);\n a->add=ei;\n }\n \n T top(Node *a){\n return a!=nullptr?g(a->val,a->add):INF;\n }\n\n T snd(Node *a){\n eval(a);\n return a!=nullptr?min(top(a->l),top(a->r)):INF;\n }\n\n Node* add(Node *a,E d){\n if(a!=nullptr) a->add=h(a->add,d);\n return a;\n }\n \n Node* push(T v){\n return new Node(v,ei);\n }\n \n Node* meld(Node *a,Node *b){\n using V = tuple<Node*, Node*>;\n stack<V> st;\n Node* res;\n ENTRYPOINT:\n if(!a||!b) res=a?a:b;\n else{\n if(c(top(a),top(b))) swap(a,b);\n eval(a);\n st.emplace(a,b);\n a=a->r;\n goto ENTRYPOINT;\n RETURNPOINT:\n tie(a,b)=st.top();st.pop();\n a->r=res;\n swap(a->l,a->r);\n res=a;\n }\n if(!st.empty()) goto RETURNPOINT;\n return res;\n }\n \n Node* pop(Node* a){\n eval(a);\n auto res=meld(a->l,a->r);\n delete a;\n return res;\n }\n \n};\n\n//INSERT ABOVE HERE\ntemplate<typename T>\nstruct Arborescence{\n typedef pair<T, int> P;\n using Heap = SkewHeap<P, T>;\n \n struct edge{\n int from,to;\n T cost;\n edge(){}\n edge(int from,int to,T cost):from(from),to(to),cost(cost){}\n };\n \n int n;\n P INF;\n UnionFind uf;\n vector<edge> edges;\n vector<typename Heap::Node*> come;\n vector<int> used,from;\n vector<T> cost;\n \n Arborescence(int n,T INF):n(n),INF(INF,-1),uf(n),come(n,NULL),\n used(n,0),from(n,-1),cost(n,-1){};\n\n void add_edge(int from,int to,T cost){\n edges.emplace_back(from,to,cost);\n }\n\n void input(int m,int offset=0){\n for(int i=0;i<m;i++){\n int u,v;\n T c;\n cin>>u>>v>>c;\n add_edge(u+offset,v+offset,c);\n }\n }\n\n T build(int r){\n typename Heap::G g=[](P a,T b){return P(a.first+b,a.second);};\n typename Heap::H h=[](T a,T b){return a+b;};\n typename Heap::C c=[](P a,P b){return a>b;};\n Heap heap(g,h,c,INF,0);\n \n used[r]=2;\n for(int i=0;i<(int)edges.size();i++){\n edge &e=edges[i];\n come[e.to]=heap.meld(come[e.to],heap.push(P(e.cost,i)));\n }\n \n T res=0;\n for(int i=0;i<n;i++){\n if(used[i]) continue;\n int v=i;\n vector<int> l;\n while(used[v]!=2){\n used[v]=1;\n l.emplace_back(v);\n if(!come[v]) return T(-1);\n from[v]=uf.find(edges[come[v]->val.second].from);\n cost[v]=heap.top(come[v]).first;\n come[v]=heap.pop(come[v]);\n if(from[v]==v) continue;\n \n res+=cost[v];\n if(used[from[v]]==1){\n int p=v;\n do{\n if(come[p]!=nullptr) heap.add(come[p],-cost[p]);\n if(p!=v){\n uf.unite(v,p);\n come[v]=heap.meld(come[v],come[p]);\n }\n p=uf.find(from[p]);\n }while(p!=v);\n }else{\n v=from[v];\n }\n }\n for(int u:l) used[u]=2;\n }\n return res;\n }\n};\n\nint n,m;\ndouble v[111][111];\ndouble cost[111][111];\nsigned main(){\n cin>>n>>m;\n Arborescence<double> G(m+1,1e14);\n re(i,m)re(j,n)cin>>v[i][j];\n re(j,n)v[m][j]=0;\n re(i,m)re(j,m){\n if(i==j)continue;\n double L=-1000,R=1000;\n re(k,50){\n double mid=(L+R)/2;\n double s1=0,s2=0;\n re(l,n)s1+=abs(v[j][l]-mid*v[i][l])*abs(v[j][l]-mid*v[i][l]);\n re(l,n)s2+=abs(v[j][l]-(mid+1e-6)*v[i][l])*abs(v[j][l]-(mid+1e-6)*v[i][l]);\n if(s1>s2)L=mid;\n else R=mid;\n }\n double sum=0;\n re(l,n)sum+=abs(v[j][l]-L*v[i][l])*abs(v[j][l]-L*v[i][l]);\n G.add_edge(i,j,sum);\n }\n re(i,m){\n double sum=0;\n re(l,n)sum+=v[i][l]*v[i][l];\n G.add_edge(m,i,sum);\n }\n printf(\"%.9f\\n\", G.build(m));\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 3844, "score_of_the_acc": -0.0997, "final_rank": 7 }, { "submission_id": "aoj_2309_2994948", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))\n#define all(x) (x).begin(),(x).end()\n#define pb push_back\n#define fi first\n#define se second\n#define dbg(x) cout<<#x\" = \"<<((x))<<endl\ntemplate<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<\"(\"<<p.fi<<\",\"<<p.se<<\")\";return o;}\ntemplate<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<\"[\";for(T t:v){o<<t<<\",\";}o<<\"]\";return o;}\n\n#define double long double\n\ndouble size(const vector<double> &v){\n int n = v.size();\n double ret = 0;\n rep(i,n) ret += v[i]*v[i];\n return ret;\n}\n\nvector<double> getv(const vector<double> &v, const vector<double> &u, double r){\n int n = v.size();\n vector<double> ret(n);\n rep(i,n) ret[i] = v[i]+u[i]*r;\n return ret;\n}\n\nconst double LIM = 100;\ndouble calc(const vector<double> &v, const vector<double> &u){\n int n = v.size();\n\n double l=-LIM, r=LIM;\n rep(loop,40){\n double m1 = (2*l+r)/3, m2 = (l+2*r)/3;\n if(size(getv(v,u,m1)) < size(getv(v,u,m2))) r = m2;\n else l = m1;\n }\n\n return size(getv(v,u,r));\n}\n\nint main(){\n int n,m;\n cin >>n >>m;\n\n vector<vector<double>> v(m);\n rep(i,m){\n vector<double> tmp(n);\n rep(j,n) cin >>tmp[j];\n v[i] = tmp;\n }\n\n double ans = 0;\n\n using P = pair<double,int>;\n priority_queue<P, vector<P>, greater<P>> pq;\n vector<bool> vis(m);\n rep(i,m) pq.push({size(v[i]),i});\n while(!pq.empty()){\n P now = pq.top();\n pq.pop();\n int x = now.se;\n if(vis[x]) continue;\n\n vis[x] = true;\n ans += now.fi;\n rep(i,m){\n pq.push({calc(v[i],v[x]),i});\n }\n }\n\n printf(\"%.10Lf\\n\", ans);\n return 0;\n}", "accuracy": 0.061224489795918366, "time_ms": 110, "memory_kb": 3384, "score_of_the_acc": -0.1127, "final_rank": 19 }, { "submission_id": "aoj_2309_2994905", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))\n#define all(x) (x).begin(),(x).end()\n#define pb push_back\n#define fi first\n#define se second\n#define dbg(x) cout<<#x\" = \"<<((x))<<endl\ntemplate<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<\"(\"<<p.fi<<\",\"<<p.se<<\")\";return o;}\ntemplate<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<\"[\";for(T t:v){o<<t<<\",\";}o<<\"]\";return o;}\n\n#define double long double\n\ndouble size(const vector<double> &v){\n int n = v.size();\n double ret = 0;\n rep(i,n) ret += v[i]*v[i];\n return ret;\n}\n\nvector<double> getv(const vector<double> &v, const vector<double> &u, double r){\n int n = v.size();\n vector<double> ret(n);\n rep(i,n) ret[i] = v[i]+u[i]*r;\n return ret;\n}\n\nconst double LIM = 100;\ndouble calc(const vector<double> &v, const vector<double> &u){\n int n = v.size();\n\n double l=-LIM, r=LIM;\n rep(loop,40){\n double m1 = (2*l+r)/3, m2 = (l+2*r)/3;\n if(size(getv(v,u,m1)) < size(getv(v,u,m2))) r = m2;\n else l = m1;\n }\n\n return size(getv(v,u,r));\n}\n\nbool comp(const vector<double> &v, const vector<double> &u){\n return size(v) + min(size(u),calc(u,v)) < size(u) + min(size(v),calc(v,u));\n}\n\nint main(){\n int n,m;\n cin >>n >>m;\n\n vector<vector<double>> v(m);\n rep(i,m){\n vector<double> tmp(n);\n rep(j,n) cin >>tmp[j];\n v[i] = tmp;\n }\n\n sort(all(v), comp);\n\n double ans = 0;\n rep(i,m){\n double add = size(v[i]);\n rep(j,i) add = min(add, calc(v[i],v[j]));\n ans += add;\n }\n printf(\"%.10Lf\\n\", ans);\n return 0;\n}", "accuracy": 0.061224489795918366, "time_ms": 70, "memory_kb": 3200, "score_of_the_acc": -0.0632, "final_rank": 18 }, { "submission_id": "aoj_2309_2994892", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))\n#define all(x) (x).begin(),(x).end()\n#define pb push_back\n#define fi first\n#define se second\n#define dbg(x) cout<<#x\" = \"<<((x))<<endl\ntemplate<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<\"(\"<<p.fi<<\",\"<<p.se<<\")\";return o;}\ntemplate<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<\"[\";for(T t:v){o<<t<<\",\";}o<<\"]\";return o;}\n\ndouble size(const vector<double> &v){\n int n = v.size();\n double ret = 0;\n rep(i,n) ret += v[i]*v[i];\n return ret;\n}\n\nvector<double> getv(const vector<double> &v, const vector<double> &u, double r){\n int n = v.size();\n vector<double> ret(n);\n rep(i,n) ret[i] = v[i]+u[i]*r;\n return ret;\n}\n\nconst double LIM = 3;\ndouble calc(const vector<double> &v, const vector<double> &u){\n int n = v.size();\n\n double l=-LIM, r=LIM;\n rep(loop,40){\n double m1 = (2*l+r)/3, m2 = (l+2*r)/3;\n if(size(getv(v,u,m1)) < size(getv(v,u,m2))) r = m2;\n else l = m1;\n }\n\n return size(getv(v,u,r));\n}\n\nbool comp(const vector<double> &v, const vector<double> &u){\n return size(v) + calc(v,u) < size(u) + calc(u,v);\n}\n\nint main(){\n int n,m;\n cin >>n >>m;\n\n vector<vector<double>> v(m);\n rep(i,m){\n vector<double> tmp(n);\n rep(j,n) cin >>tmp[j];\n v[i] = tmp;\n }\n\n sort(all(v), comp);\n\n double ans = 0;\n rep(i,m){\n double add = size(v[i]);\n rep(j,i) add = min(add, calc(v[i],v[j]));\n ans += add;\n }\n printf(\"%.10f\\n\", ans);\n return 0;\n}", "accuracy": 0.061224489795918366, "time_ms": 40, "memory_kb": 3268, "score_of_the_acc": -0.0343, "final_rank": 15 }, { "submission_id": "aoj_2309_2282933", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\ntypedef double ldouble;\ntypedef vector<ldouble> Point;\nldouble INF=1e9;\n\nstruct UnionFind{\n vector<int> par,rank;\n void init(int n){\n par.clear();\n rank.clear();\n par.resize(n);\n rank.resize(n);\n for(int i=0;i<n;i++){\n par[i]=i;\n rank[i]=1;\n }\n }\n \n int find(int x){\n if(x==par[x])return x;\n return par[x]=find(par[x]);\n }\n\n bool same(int x,int y){\n return ( find(x)==find(y) );\n }\n \n void unite(int x,int y){\n x=find(x);\n y=find(y);\n if(x==y)return;\n if(rank[x]<rank[y])swap(x,y);\n par[y]=x;\n rank[x]+=rank[y];\n }\n};\n\nstruct edge{\n int from;\n int to;\n ldouble cost;\n int id;\n edge(int from,int to,ldouble cost,int id) :from(from), to(to),cost(cost),id(id) {}\n bool operator < (const edge e)const{\n return cost > e.cost;\n }\n};\n\ntypedef priority_queue< edge > prque;\ntypedef prque* Prque;\n\ntypedef vector<edge> Node;\ntypedef vector<Node> Graph;\n\n\nPrque Merge(vector< Prque > &Q, vector<edge> &ev,int A,int C){\n if( Q[C]->size() < Q[A]->size() ){\n\n while( !Q[C]->empty() ){\n edge e=Q[C]->top();\n e.cost-=ev[C].cost;\n e.cost+=ev[A].cost;\n // cout<<e.from<<' '<<e.to<<' '<<e.cost<<' '<<ev[A].cost<<' '<<ev[C].cost<<endl;\n Q[A]->push(e);\n Q[C]->pop();\n }\n ev[C].cost=ev[A].cost;\n return Q[A];\n }else{\n while( !Q[A]->empty() ){\n edge e=Q[A]->top();\n e.cost-=ev[A].cost;\n e.cost+=ev[C].cost;\n // cout<<e.from<<' '<<e.to<<' '<<e.cost<<endl;\n Q[C]->push(e);\n Q[A]->pop();\n }\n return Q[C];\n }\n}\n\nldouble solve(Graph &G,vector<edge> &edges,int root){\n\n int n=G.size();\n ldouble res=0;\n \n vector<int> used(n,0);\n vector< edge > ev(n, (edge){0,0,0,-1} );\n vector< prque > pool(n);\n vector< Prque > Q(n);\n for(int i=0;i<n;i++)Q[i]=&pool[i];\n \n UnionFind uf;\n uf.init(n);\n \n for(int i=0;i<(int)edges.size();i++){\n edge e=edges[i];\n Q[ e.to ]->push( e );\n }\n \n used[root]=2;\n for(int Pos=0;Pos<n;Pos++){\n if(used[Pos]==2)continue;\n int pos=Pos;\n vector<int> path;\n \n while( used[pos] != 2 ){\n pos=uf.find(pos);\n \n used[pos]=1;\n path.push_back(pos);\n if( Q[pos]->empty() ){\n return INF;\n }\n \n edge e=Q[pos]->top();\n\n \n Q[pos]->pop();\n e.cost-=ev[pos].cost;\n if( uf.same(e.from,pos) ) continue;\n ldouble tmpcost=ev[pos].cost;\n /*\n cout<<\" pos=\"<<pos;\n cout<<\" e.from=\"<<e.from;\n cout<<\" e.to=\"<<e.to;\n cout<<\" e.cost=\"<<e.cost;\n cout<<\" tmpcost=\"<<tmpcost<<endl;\n cout<<endl;\n */\n res+=e.cost;\n e.cost+=tmpcost;\n ev[pos]=e;\n if( used[ uf.find(e.from) ] == 2 )break;\n if( used[ uf.find(e.from) ] == 0 ){\n pos=e.from;\n continue;\n }\n int pre=uf.find(e.from);\n for(int i=0;i<100;i++){\n if(!uf.same(pre,pos)){\n int A=uf.find(pre), B=uf.find(pos);\n uf.unite(A,B);\n int C=uf.find(A);\n /*\n cout<<\" !!A=\"<<A;\n cout<<\" !!B=\"<<B;\n cout<<\" !!C=\"<<C<<endl;\n */\n Prque tmp=NULL;\n if(B==C)tmp=Merge(Q,ev,A,C);\n else if(A==C)tmp=Merge(Q,ev,B,C);\n else assert(0);\n \n Q[C]=tmp;\n }\n pre=uf.find(ev[pre].from);\n }\n }// while_pos\n\n for(int i=0;i<(int)path.size();i++)used[ path[i] ]=2;\n }// Pos\n return res;\n}\n\n\nPoint add(Point a,Point b){\n Point res(a.size());\n for(int i=0;i<(int)a.size();i++)\n res[i]=a[i]+b[i];\n return res;\n}\n\nPoint sub(Point a,Point b){\n Point res(a.size());\n for(int i=0;i<(int)a.size();i++)\n res[i]=a[i]-b[i];\n return res;\n}\n\nldouble dot(Point a,Point b){\n ldouble res=0;\n for(int i=0;i<(int)a.size();i++)\n res+=a[i]*b[i];\n return res;\n}\n\nldouble norm(Point p){\n return dot(p,p);\n}\n\nldouble abs(Point p){\n return sqrt( norm(p) );\n}\n\ndouble Sqrt(double x){\n if(x<0)return 0;\n return sqrt(x);\n}\n\ndouble project(Point a,Point b){\n ldouble t=dot(a,b);\n \n // cout<<a[0]<<' '<<a[1]<<endl;\n // cout<<b[0]<<' '<<b[1]<<endl;\n // cout<< dot(a,b) <<endl;\n return abs( norm(b) - (t*t/norm(a)) );\n}\n\n\nint main(){\n int n,m;\n vector< Point > t;\n \n cin>>m>>n;\n for(int i=0;i<n;i++){\n Point a(m);\n for(int j=0;j<m;j++){\n cin>>a[j];\n }\n\n if(norm(a) < 0.00000001){\n continue;\n }\n t.push_back(a);\n }\n n=t.size();\n Graph G;\n G.resize(n+1);\n int cc=0;\n for(int i=0;i<n;i++){\n G[n].push_back( edge(n,i,norm(t[i]) , cc++) );\n for(int j=0;j<n;j++){\n if(i==j)continue;\n ldouble cost=project(t[i],t[j]);\n G[i].push_back( edge(i,j,cost,cc++) );\n }\n }\n\n vector<edge> edges;\n for(int i=0;i<=n;i++)\n for(int j=0;j<(int)G[i].size();j++)\n edges.push_back(G[i][j]);\n \n printf(\"%.10f\\n\",(double) solve(G,edges,n) );\n \n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 4420, "score_of_the_acc": -0.0493, "final_rank": 5 }, { "submission_id": "aoj_2309_2282859", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef vector<double> Point;\n\ndouble INF=1e9;\n\nstruct UnionFind{\n vector<int> par,rank;\n void init(int n){\n par.clear();\n rank.clear();\n par.resize(n);\n rank.resize(n);\n for(int i=0;i<n;i++){\n par[i]=i;\n rank[i]=1;\n }\n }\n \n int find(int x){\n if(x==par[x])return x;\n return par[x]=find(par[x]);\n }\n\n bool same(int x,int y){\n return ( find(x)==find(y) );\n }\n \n void unite(int x,int y){\n x=find(x);\n y=find(y);\n if(x==y)return;\n if(rank[x]<rank[y])swap(x,y);\n par[y]=x;\n rank[x]+=rank[y];\n }\n};\n\nstruct edge{\n int from;\n int to;\n double cost;\n int id;\n edge(int from,int to,double cost,int id) :from(from), to(to),cost(cost),id(id) {}\n bool operator < (const edge e)const{\n return cost > e.cost;\n }\n};\n\ntypedef priority_queue< edge > prque;\ntypedef prque* Prque;\n\ntypedef vector<edge> Node;\ntypedef vector<Node> Graph;\n\n\nPrque Merge(vector< Prque > &Q, vector<edge> &ev,int A,int C){\n if( Q[C]->size() < Q[A]->size() ){\n\n while( !Q[C]->empty() ){\n edge e=Q[C]->top();\n e.cost-=ev[C].cost;\n e.cost+=ev[A].cost;\n // cout<<e.from<<' '<<e.to<<' '<<e.cost<<' '<<ev[A].cost<<' '<<ev[C].cost<<endl;\n Q[A]->push(e);\n Q[C]->pop();\n }\n ev[C].cost=ev[A].cost;\n return Q[A];\n }else{\n while( !Q[A]->empty() ){\n edge e=Q[A]->top();\n e.cost-=ev[A].cost;\n e.cost+=ev[C].cost;\n // cout<<e.from<<' '<<e.to<<' '<<e.cost<<endl;\n Q[C]->push(e);\n Q[A]->pop();\n }\n return Q[C];\n }\n}\n\ndouble solve(Graph &G,vector<edge> &edges,int root){\n\n int n=G.size();\n double res=0;\n \n vector<int> used(n,0);\n vector< edge > ev(n, (edge){0,0,0,-1} );\n vector< prque > pool(n);\n vector< Prque > Q(n);\n for(int i=0;i<n;i++)Q[i]=&pool[i];\n \n UnionFind uf;\n uf.init(n);\n \n for(int i=0;i<(int)edges.size();i++){\n edge e=edges[i];\n Q[ e.to ]->push( e );\n }\n \n used[root]=2;\n for(int Pos=0;Pos<n;Pos++){\n if(used[Pos]==2)continue;\n int pos=Pos;\n vector<int> path;\n \n while( used[pos] != 2 ){\n pos=uf.find(pos);\n \n used[pos]=1;\n path.push_back(pos);\n if( Q[pos]->empty() ){\n return INF;\n }\n \n edge e=Q[pos]->top();\n\n \n Q[pos]->pop();\n e.cost-=ev[pos].cost;\n if( uf.same(e.from,pos) ) continue;\n double tmpcost=ev[pos].cost;\n /*\n cout<<\" pos=\"<<pos;\n cout<<\" e.from=\"<<e.from;\n cout<<\" e.to=\"<<e.to;\n cout<<\" e.cost=\"<<e.cost;\n cout<<\" tmpcost=\"<<tmpcost<<endl;\n cout<<endl;\n */\n res+=e.cost;\n e.cost+=tmpcost;\n ev[pos]=e;\n if( used[ uf.find(e.from) ] == 2 )break;\n if( used[ uf.find(e.from) ] == 0 ){\n pos=e.from;\n continue;\n }\n int pre=uf.find(e.from);\n for(int i=0;i<100;i++){\n if(!uf.same(pre,pos)){\n int A=uf.find(pre), B=uf.find(pos);\n uf.unite(A,B);\n int C=uf.find(A);\n /*\n cout<<\" !!A=\"<<A;\n cout<<\" !!B=\"<<B;\n cout<<\" !!C=\"<<C<<endl;\n */\n Prque tmp=NULL;\n if(B==C)tmp=Merge(Q,ev,A,C);\n else if(A==C)tmp=Merge(Q,ev,B,C);\n else assert(0);\n \n Q[C]=tmp;\n }\n pre=uf.find(ev[pre].from);\n }\n }// while_pos\n\n for(int i=0;i<(int)path.size();i++)used[ path[i] ]=2;\n }// Pos\n return res;\n}\n\n\nPoint add(Point a,Point b){\n Point res(a.size());\n for(int i=0;i<(int)a.size();i++)\n res[i]=a[i]+b[i];\n return res;\n}\n\nPoint sub(Point a,Point b){\n Point res(a.size());\n for(int i=0;i<(int)a.size();i++)\n res[i]=a[i]-b[i];\n return res;\n}\n\ndouble dot(Point a,Point b){\n double res=0;\n for(int i=0;i<(int)a.size();i++)\n res+=a[i]*b[i];\n return res;\n}\n\ndouble norm(Point p){\n return dot(p,p);\n}\n\nPoint project(Point a,Point b,Point p){\n Point base( a.size() );\n Point target( a.size() );\n for(int i=0;i<(int)a.size();i++){\n base[i]=b[i]-a[i];\n target[i]=p[i]-a[i];\n }\n double t=dot(base,target);\n Point res=a;\n for(int i=0;i<(int)a.size();i++)\n res[i]=res[i]+base[i]*t/norm(base);\n return res;\n}\n\n\nint main(){\n int n,m;\n vector< Point > t;\n \n cin>>m>>n;\n for(int i=0;i<n;i++){\n Point a(m);\n for(int j=0;j<m;j++){\n cin>>a[j];\n }\n t.push_back(a);\n }\n\n Point cen( m , 0 );\n \n Graph G;\n G.resize(n+1);\n int cc=0;\n for(int i=0;i<n;i++){\n G[n].push_back( edge(n,i,norm(t[i]) , cc++) );\n \n for(int j=0;j<n;j++){\n if(i==j)continue;\n Point pp = project( t[j] , add(t[j],t[i]) , cen);\n double cost= norm( pp);\n G[i].push_back( edge(i,j,cost,cc++) );\n }\n }\n\n vector<edge> edges;\n for(int i=0;i<=n;i++)\n for(int j=0;j<(int)G[i].size();j++)\n edges.push_back(G[i][j]);\n \n printf(\"%.10f\\n\", solve(G,edges,n) );\n \n return 0;\n}", "accuracy": 0.9183673469387755, "time_ms": 130, "memory_kb": 4564, "score_of_the_acc": -0.1814, "final_rank": 12 }, { "submission_id": "aoj_2309_2282824", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef vector<double> Point;\n\ndouble INF=1e9;\n\nstruct UnionFind{\n vector<int> par,rank;\n void init(int n){\n par.clear();\n rank.clear();\n par.resize(n);\n rank.resize(n);\n for(int i=0;i<n;i++){\n par[i]=i;\n rank[i]=1;\n }\n }\n \n int find(int x){\n if(x==par[x])return x;\n return par[x]=find(par[x]);\n }\n\n bool same(int x,int y){\n return ( find(x)==find(y) );\n }\n \n void unite(int x,int y){\n x=find(x);\n y=find(y);\n if(x==y)return;\n if(rank[x]<rank[y])swap(x,y);\n par[y]=x;\n rank[x]+=rank[y];\n }\n};\n\nstruct edge{\n int from;\n int to;\n double cost;\n int id;\n edge(int from,int to,double cost,int id) :from(from), to(to),cost(cost),id(id) {}\n bool operator < (const edge e)const{\n return cost > e.cost;\n }\n};\n\ntypedef priority_queue< edge > prque;\ntypedef prque* Prque;\n\ntypedef vector<edge> Node;\ntypedef vector<Node> Graph;\n\n\nPrque Merge(vector< Prque > &Q, vector<edge> &ev,int A,int C){\n if( Q[C]->size() < Q[A]->size() ){\n\n while( !Q[C]->empty() ){\n edge e=Q[C]->top();\n e.cost-=ev[C].cost;\n e.cost+=ev[A].cost;\n // cout<<e.from<<' '<<e.to<<' '<<e.cost<<' '<<ev[A].cost<<' '<<ev[C].cost<<endl;\n Q[A]->push(e);\n Q[C]->pop();\n }\n ev[C].cost=ev[A].cost;\n return Q[A];\n }else{\n while( !Q[A]->empty() ){\n edge e=Q[A]->top();\n e.cost-=ev[A].cost;\n e.cost+=ev[C].cost;\n // cout<<e.from<<' '<<e.to<<' '<<e.cost<<endl;\n Q[C]->push(e);\n Q[A]->pop();\n }\n return Q[C];\n }\n}\n\ndouble solve(Graph &G,vector<edge> &edges,int root){\n\n int n=G.size();\n double res=0;\n \n vector<int> used(n,0);\n vector< edge > ev(n, (edge){0,0,0,-1} );\n vector< prque > pool(n);\n vector< Prque > Q(n);\n for(int i=0;i<n;i++)Q[i]=&pool[i];\n \n UnionFind uf;\n uf.init(n);\n \n for(int i=0;i<(int)edges.size();i++){\n edge e=edges[i];\n Q[ e.to ]->push( e );\n }\n \n used[root]=2;\n for(int Pos=0;Pos<n;Pos++){\n if(used[Pos]==2)continue;\n int pos=Pos;\n vector<int> path;\n \n while( used[pos] != 2 ){\n pos=uf.find(pos);\n \n used[pos]=1;\n path.push_back(pos);\n if( Q[pos]->empty() ){\n return INF;\n }\n \n edge e=Q[pos]->top();\n\n \n Q[pos]->pop();\n e.cost-=ev[pos].cost;\n if( uf.same(e.from,pos) ) continue;\n int tmpcost=ev[pos].cost;\n /*\n cout<<\" pos=\"<<pos;\n cout<<\" e.from=\"<<e.from;\n cout<<\" e.to=\"<<e.to;\n cout<<\" e.cost=\"<<e.cost;\n cout<<\" tmpcost=\"<<tmpcost<<endl;\n cout<<endl;\n */\n res+=e.cost;\n e.cost+=tmpcost;\n ev[pos]=e;\n if( used[ uf.find(e.from) ] == 2 )break;\n if( used[ uf.find(e.from) ] == 0 ){\n pos=e.from;\n continue;\n }\n int pre=uf.find(e.from);\n for(int i=0;i<100;i++){\n if(!uf.same(pre,pos)){\n int A=uf.find(pre), B=uf.find(pos);\n uf.unite(A,B);\n int C=uf.find(A);\n /*\n cout<<\" !!A=\"<<A;\n cout<<\" !!B=\"<<B;\n cout<<\" !!C=\"<<C<<endl;\n */\n Prque tmp=NULL;\n if(B==C)tmp=Merge(Q,ev,A,C);\n else if(A==C)tmp=Merge(Q,ev,B,C);\n else assert(0);\n \n Q[C]=tmp;\n }\n pre=uf.find(ev[pre].from);\n }\n }// while_pos\n\n for(int i=0;i<(int)path.size();i++)used[ path[i] ]=2;\n }// Pos\n return res;\n}\n\n\nPoint add(Point a,Point b){\n Point res(a.size());\n for(int i=0;i<(int)a.size();i++)\n res[i]=a[i]+b[i];\n return res;\n}\n\nPoint sub(Point a,Point b){\n Point res(a.size());\n for(int i=0;i<(int)a.size();i++)\n res[i]=a[i]-b[i];\n return res;\n}\n\ndouble dot(Point a,Point b){\n double res=0;\n for(int i=0;i<(int)a.size();i++)\n res+=a[i]*b[i];\n return res;\n}\n\ndouble norm(Point p){\n return dot(p,p);\n}\n\nPoint project(Point a,Point b,Point p){\n Point base( a.size() );\n Point target( a.size() );\n for(int i=0;i<(int)a.size();i++){\n base[i]=b[i]-a[i];\n target[i]=p[i]-a[i];\n }\n double t=dot(base,target);\n Point res=a;\n for(int i=0;i<(int)a.size();i++)\n res[i]=res[i]+base[i]*t/norm(base);\n return res;\n}\n\n\nint main(){\n int n,m;\n vector< Point > t;\n \n cin>>m>>n;\n for(int i=0;i<n;i++){\n Point a(m);\n for(int j=0;j<m;j++){\n cin>>a[j];\n }\n t.push_back(a);\n }\n\n Point cen( m , 0 );\n \n Graph G;\n G.resize(n+1);\n int cc=0;\n for(int i=0;i<n;i++){\n G[n].push_back( edge(n,i, (norm(t[i])) , cc++) );\n \n \n for(int j=0;j<n;j++){\n if(i==j)continue;\n \n Point pp = project( t[j] , add(t[j],t[i]) , cen);\n double cost= norm( pp);\n G[i].push_back( edge(i,j,cost,cc++) );\n\n // cout<<i<<' '<<j<<' '<< cost << ' ' <<pp[0]<<' '<<pp[1]<<endl;\n }\n }\n\n vector<edge> edges;\n for(int i=0;i<=n;i++)\n for(int j=0;j<(int)G[i].size();j++)\n edges.push_back(G[i][j]);\n \n printf(\"%.10f\\n\", solve(G,edges,n) );\n \n return 0;\n}", "accuracy": 0.061224489795918366, "time_ms": 30, "memory_kb": 4144, "score_of_the_acc": -0.0592, "final_rank": 17 }, { "submission_id": "aoj_2309_2185204", "code_snippet": "#include<iostream>\n#include<cstdio>\n#include<cstdlib>\n#include<cstring>\n#include<cmath>\n#include<queue>\n#include<stack>\n#include<vector>\nusing namespace std;\n\n\nstruct edge\n{\n int begin;int end;\t\n};\n\nconst int maxx=120;\nbool vis[maxx],indfs[maxx];\nvector<double>zero;\nint n,m,root;\nvector<vector<double> >vec;\ndouble cost[maxx][maxx],Cost[maxx][maxx];\nbool operator <(const edge a,const edge b)\n{\n double ac=cost[a.begin][a.end];double bc=cost[b.begin][b.end];\n return ac>bc;//´?????´?????\t\n};\npriority_queue<edge>que[maxx];\ndouble minv(vector<double> a,vector<double> b)\n{\n double ab=0,bsqrt=0,asqrt=0;\n for(int i=0;i<a.size();i++)\n \t{ab+=a[i]*b[i];bsqrt+=b[i]*b[i];asqrt+=a[i]*a[i];}\n \t\n if((bsqrt-0.0)<1e-10)return asqrt;\n return asqrt-(ab*ab/bsqrt);\n}\nint belongcircle[maxx];\nint findbelong(int x)\n{\n if(x==belongcircle[x])return x;\n \telse return belongcircle[x]=findbelong(belongcircle[x]);\n}\n\ndouble ans,ansnow;\ndouble min(double a,double b)\n{\n if(a<b)return a;\n \telse return b;\n}\nvoid combinecircle(int now,int begin)\n{\n now=findbelong(now);\n if(vis[now])return ;\n vis[now]=true;\n edge e1=que[now].top();\n double coste=cost[findbelong(e1.begin)][findbelong(e1.end)];\n ansnow+=coste;\n bool finish[maxx];\n memset(finish,0,sizeof(finish));\n for(int i=0;i<=m;i++)\n \t{\n \t int po=findbelong(i);\n \t if(po==now||po==begin)continue;\n \t if(finish[po])continue;\n \t finish[po]=true;\n \t cost[begin][po]=min(cost[begin][po],cost[now][po]-coste);\n \t cost[po][begin]=min(cost[po][begin],cost[po][now]);\n\t}\n belongcircle[now]=begin;\n combinecircle(e1.end,begin);\n return ;\n}\nvoid build();\nbool findcircle(int now)\n{\n if(now==root)return false; \n now=findbelong(now);\n if(indfs[now])\n \t{\n \t memset(vis,0,sizeof(vis));\n \t combinecircle(now,now);\n \t build();\n \t return true;\n\t}\n indfs[now]=true;\n return findcircle(que[now].top().end);\n}\n\nvoid build()\n{\n bool finish[maxx];\n memset(finish,0,sizeof(finish));\n for(int i=0;i<m;i++)\n \t{\n \t int po=findbelong(i);\n \t if(finish[po])continue;\n \t finish[po]=true;\n \t while(!que[po].empty())que[po].pop();\n \t for(int j=0;j<=m;j++)\n \t \t{\n \t \t int pb=findbelong(j);\n \t \t if(po==pb)continue;\n \t \t edge en;en.begin=po;en.end=pb;\n \t \t que[po].push(en);\n\t\t}\n\t}\n return ;\n}\nbool counted[maxx];\nint main()\n{\n ios::sync_with_stdio(false);\n cin>>n>>m;\n for(int i=0;i<n;i++)\n zero.push_back(0.0);\n for(int i=0;i<m;i++)\n \t{\n\t vector<double>nowv;\n\t for(int j=0;j<n;j++)\n\t \t{ double nv;cin>>nv;nowv.push_back(nv);}\n\t vec.push_back(nowv);\n\t}\n vec.push_back(zero);\n for(int i=0;i<=m;i++)\n \tfor(int j=0;j<=m;j++)\n \t\t{ cost[i][j]=minv(vec[i],vec[j]);}\n root=m;\n ansnow=0;\n belongcircle[root]=root;\n \t for(int i=0;i<m;i++)\n \t \t{\n \t \t belongcircle[i]=i;\n \t \t}\n\t bool exis=false;\n\t \t do\n\t \t \t{\n\t \t \t build();\n\t \t \t exis=false;\n\t \t \t bool finish[maxx];\n\t \t \t memset(finish,0,sizeof(finish));\n\t \t \t for(int i=0;i<m;i++)\n\t \t \t { \n\t\t\t int po=findbelong(i);\n\t\t\t if(finish[po])continue;\n\t \t \t memset(indfs,0,sizeof(indfs));\n\t \t \t exis=findcircle(po);\n\t \t \t finish[po]=true;\n\t\t\t }\n\t\t\t}while(exis);\n\t memset(counted,0,sizeof(counted));\n\t for(int i=0;i<m;i++)\n\t \t{\n\t\t int po=findbelong(i);\n\t\t if(counted[po])continue;\n\t\t counted[po]=true;\n\t\t ansnow+=cost[findbelong(que[po].top().begin)][findbelong(que[po].top().end)];\t\n\t\t}\n printf(\"%.8f\\n\",ansnow);\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3564, "score_of_the_acc": -0.0252, "final_rank": 3 }, { "submission_id": "aoj_2309_2185197", "code_snippet": "#include<iostream>\n#include<cstdio>\n#include<cstdlib>\n#include<cstring>\n#include<cmath>\n#include<queue>\n#include<stack>\n#include<vector>\nusing namespace std;\n\n\nstruct edge\n{\n int begin;int end;\t\n};\n\nconst int maxx=120;\nbool vis[maxx],indfs[maxx];\nvector<double>zero;\nint n,m,root;\nvector<vector<double> >vec;\ndouble cost[maxx][maxx],Cost[maxx][maxx];\nbool operator <(const edge a,const edge b)\n{\n double ac=cost[a.begin][a.end];double bc=cost[b.begin][b.end];\n return ac>bc;//´?????´?????\t\n};\npriority_queue<edge>que[maxx];\ndouble minv(vector<double> a,vector<double> b)\n{\n double ab=0,bsqrt=0,asqrt=0;\n for(int i=0;i<a.size();i++)\n \t{ab+=a[i]*b[i];bsqrt+=b[i]*b[i];asqrt+=a[i]*a[i];}\n \t\n if((bsqrt-0.0)<1e-10)return asqrt;\n return asqrt-(ab*ab/bsqrt);\n}\nint belongcircle[maxx];\nint findbelong(int x)\n{\n if(x==belongcircle[x])return x;\n \telse return belongcircle[x]=findbelong(belongcircle[x]);\n}\n\ndouble ans,ansnow;\ndouble min(double a,double b)\n{\n if(a<b)return a;\n \telse return b;\n}\nvoid combinecircle(int now,int begin)\n{\n now=findbelong(now);\n if(vis[now])return ;\n vis[now]=true;\n edge e1=que[now].top();\n double coste=cost[findbelong(e1.begin)][findbelong(e1.end)];\n ansnow+=coste;\n bool finish[maxx];\n memset(finish,0,sizeof(finish));\n for(int i=0;i<=m;i++)\n \t{\n \t int po=findbelong(i);\n \t if(po==now||po==begin)continue;\n \t if(finish[po])continue;\n \t finish[po]=true;\n \t cost[begin][po]=min(cost[begin][po],cost[now][po]-coste);\n \t cost[po][begin]=min(cost[po][begin],cost[po][now]);\n\t}\n belongcircle[now]=begin;\n combinecircle(e1.end,begin);\n}\n\nbool findcircle(int now)\n{\n if(now==root)return false; \n now=findbelong(now);\n if(indfs[now])\n \t{\n \t memset(vis,0,sizeof(vis));\n \t combinecircle(now,now);\n \t return true;\n\t}\n indfs[now]=true;\n return findcircle(que[now].top().end);\n}\n\nvoid build()\n{\n bool finish[maxx];\n memset(finish,0,sizeof(finish));\n for(int i=0;i<m;i++)\n \t{\n \t int po=findbelong(i);\n \t if(finish[po])continue;\n \t finish[po]=true;\n \t while(!que[po].empty())que[po].pop();\n \t for(int j=0;j<=m;j++)\n \t \t{\n \t \t int pb=findbelong(j);\n \t \t if(po==pb)continue;\n \t \t edge en;en.begin=po;en.end=pb;\n \t \t que[po].push(en);\n\t\t}\n\t}\n return ;\n}\nbool counted[maxx];\nint main()\n{\n cin>>n>>m;\n for(int i=0;i<n;i++)\n zero.push_back(0.0);\n for(int i=0;i<m;i++)\n \t{\n\t vector<double>nowv;\n\t for(int j=0;j<n;j++)\n\t \t{ double nv;cin>>nv;nowv.push_back(nv);}\n\t vec.push_back(nowv);\n\t}\n vec.push_back(zero);\n for(int i=0;i<=m;i++)\n \tfor(int j=0;j<=m;j++)\n \t\t{ cost[i][j]=minv(vec[i],vec[j]);}\n root=m;\n ansnow=0;\n belongcircle[root]=root;\n \t for(int i=0;i<m;i++)\n \t \t{\n \t \t belongcircle[i]=i;\n \t \t}\n\t bool exis=false;\n\t \t do\n\t \t \t{\n\t \t \t build();\n\t \t \t exis=false;\n\t \t \t bool finish[maxx];\n\t \t \t memset(finish,0,sizeof(finish));\n\t \t \t for(int i=0;i<m;i++)\n\t \t \t { \n\t\t\t int po=findbelong(i);\n\t\t\t if(finish[po])continue;\n\t \t \t memset(indfs,0,sizeof(indfs));\n\t \t \t exis=findcircle(po);\n\t \t \t finish[po]=true;\n\t \t \t if(exis){break;}\n\t\t\t }\n\t\t\t}while(exis);\n\t memset(counted,0,sizeof(counted));\n\t for(int i=0;i<m;i++)\n\t \t{\n\t\t int po=findbelong(i);\n\t\t if(counted[po])continue;\n\t\t counted[po]=true;\n\t\t ansnow+=cost[findbelong(que[po].top().begin)][findbelong(que[po].top().end)];\t\n\t\t}\n cout<<ansnow<<endl;\n return 0;\n}", "accuracy": 0.061224489795918366, "time_ms": 10, "memory_kb": 3372, "score_of_the_acc": -0.007, "final_rank": 13 } ]
aoj_2310_cpp
〜魔女からこの世界を守るため キュゥべえ と共に戦う 5 人の魔法少女達の物語〜 問題 A 問題文 薔薇園の魔女 GERTRUD は幅 W メートル,高さ H メートルの長方形の庭でバラを育てている.庭は一辺 1 メートルの正方形で区切られており, W\times H のブロックに分けられている.各ブロックはバラが育てられているか育てられていないかのどちらかである. ただ驚くべきことに,この庭で育てられているバラはブロックを越えて絡み合い 1 つの生命体となっている.この生命体は辺を共有しているブロック間を越えて絡み合うことができ,どこかある 1 点を共有していれば同じ生命体となる. 巴マミ は庭の左下の角から直線に必殺技「ティロ・フィナーレ」で巨大な銃を撃ち,その弾丸が通る直線上でその生命体を分割した時,生命体が出来る限り多くの分割数に分断されるようにすることによりダメージを与えたい.ティロ・フィナーレは最大でこの生物を何分割できるか求めよ. 入力形式 入力は以下の形式で与えられる. H\ W\\ f_{11} f_{12} ... f_{1W}\\ f_{21} f_{22} ... f_{2W}\\ ...\\ f_{H1} f_{H2} ... f_{HW}\\ H は庭の高さ, W は庭の幅を表す. f_{ij} は庭の各ブロックを表す文字であり,上から i 行目,左から j 列目のブロックの状態を表す.ブロックにバラが育てられている場合は f_{ij} は '#' となり,そうでない場合は '.' となる. 出力形式 生命体を直線で分割する時に,最大でいくつの部分に分割できるかを1行で出力せよ. 制約 1 ≤ H ≤ 600 1 ≤ W ≤ 600 f_{ij} は '#' , '.' のいずれかである. 少なくとも 1 つは f_{ij} = '#' となるマス f_{ij} が存在する. 2 つのブロックが辺を共有するとき,それらは 隣接している と呼ぶことにする.任意の 2 つの異なる '#' のブロック f_A, f_B に対して, f_A から隣接している '#' のブロックを辿っていって f_B に着くことができる.(すなわち,薔薇のブロックは連結である.) 入力中で 1 行目の行, H 行目の行, 1 列目の列,または W 列目の列にあるブロックを 盤面の端のブロック と呼ぶことにする.任意の '.' のブロック f_C に対して, f_C から隣接している '.' のブロックを辿っていって,ある '.' の盤面の端のブロックに着くことができる.(すなわち,入力に"空洞"のブロックは存在しない.) 入出力例 入力例 1 3 5 ##..# #..## ####. 出力例1 4 このとき以下のように 4 つの部分に分割することができる. 入力例 2 3 3 #.# ### #.# 出力例 2 3 入力例 3 10 9 ......... ......... ####..... #..#.##.. #..#..#.. #..##.### #..##...# ##..##.## ###.#..#. ###.####. 出力例 3 6 入力例 4 10 11 ########### #.#.#.#.#.# #.#.#.#.#.# #.#.#.#.#.# #.#.#.#.#.# #.#.#.#.#.# #.#.#.#.#.# #.#.#.#.#.# #.#.#.#.#.# #.#.#.#.#.# 出力例 4 7 入力例 5 25 38 ...........#...............#.......... ...........###..........####.......... ...........#####.......####........... ............###############........... ............###############........... ............##############............ .............#############............ ............###############........... ...........#################.......... .......#...#################...#...... .......##.##################..##...... ........####################.##....... ..........####################........ .........#####..########..#####....... .......######....#####....######...... ......########...#####..########.#.... ....#######..#...#####..#..########... ..#########.....#######......#######.. ...#######......#######........###.... ..####.........#########........###... ...............#########.............. ..............##########.............. ..............##########.............. ...............########............... ...............########............... 出力例 5 8 Problem Setter: Flat35
[ { "submission_id": "aoj_2310_10851592", "code_snippet": "#include \"bits/stdc++.h\"\nusing namespace std;\ntypedef long long ll;\ntypedef pair<int, int> pii;\ntypedef pair<ll, ll> pll;\nconst int INF = 1e9;\nconst ll LINF = 1e18;\ntemplate<class S,class T> ostream& operator << (ostream& out,const pair<S,T>& o){ out << \"(\" << o.first << \",\" << o.second << \")\"; return out; }\ntemplate<class T> ostream& operator << (ostream& out,const vector<T> V){ for(int i = 0; i < V.size(); i++){ out << V[i]; if(i!=V.size()-1) out << \" \";} return out; }\ntemplate<class T> ostream& operator << (ostream& out,const vector<vector<T> > Mat){ for(int i = 0; i < Mat.size(); i++) { if(i != 0) out << endl; out << Mat[i];} return out; }\ntemplate<class S,class T> ostream& operator << (ostream& out,const map<S,T> mp){ out << \"{ \"; for(auto it = mp.begin(); it != mp.end(); it++){ out << it->first << \":\" << it->second; if(mp.size()-1 != distance(mp.begin(),it)) out << \", \"; } out << \" }\"; return out; }\n\n/*\n <url:>\n 問題文============================================================\n =================================================================\n 解説=============================================================\n ================================================================\n */\n\nconst double eps = 1e-9;\nll solve(){\n ll H,W; cin >> H >> W;\n ll res = 1;\n vector<vector<char>> masu(H+2,vector<char>(W+2,'.'));\n vector<pair<double,int>> event;\n for(int i = 1; i <= H;i++){\n for(int j = 1; j <= W;j++){\n cin >> masu[i][j];\n }\n }\n \n vector<vector<pii>> masu2(H+2,vector<pii>(W+2));\n for(int i = 1; i <= H;i++){\n for(int j = 1; j <= W;j++){\n masu2[i][j] = {j-1,H-i};\n }\n }\n //cout << masu << endl;\n //cout << masu2 << endl;\n for(int i = 1; i <= H;i++){\n for(int j = 1; j <= W;j++){\n if(masu[i][j] == '#'){\n \n double angle = 0;\n // increment pattern\n // ..\n // .#\n if(masu[i-1][j-1] == '.' && masu[i-1][j] == '.' && masu[i][j-1] == '.'){\n angle = atan2(masu2[i][j].second+1,masu2[i][j].first);\n event.push_back({angle,-1});\n }\n \n // increment pattern\n // ##\n // #.\n if(masu[i+1][j+1] == '.' && masu[i+1][j] == '#' && masu[i][j+1] == '#'){\n angle = atan2(masu2[i][j].second,masu2[i][j].first+1);\n event.push_back({angle,-1});\n }\n \n // decrement pattern\n // .#\n // ##\n if(masu[i-1][j-1] == '.' && masu[i-1][j] == '#' && masu[i][j-1] == '#'){\n angle = atan2(masu2[i][j].second+1,masu2[i][j].first);\n event.push_back({angle,1});\n }\n \n // decrement pattern\n // #.\n // ..\n if(masu[i+1][j+1] == '.' && masu[i+1][j] == '.' && masu[i][j+1] == '.'){\n angle = atan2(masu2[i][j].second,masu2[i][j].first+1);\n event.push_back({angle,1});\n }\n }\n }\n }\n \n sort(event.begin(),event.end());\n \n ll cnt = 1;\n for(auto it = event.rbegin(); it != event.rend(); it++){\n auto e = *it;\n //cout << -e.second << endl;\n cnt -= e.second;\n res = max(res,cnt);\n }\n return res;\n}\nint main(void) {\n cin.tie(0); ios_base::sync_with_stdio(false);\n cout << solve() << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 8728, "score_of_the_acc": -0.2291, "final_rank": 13 }, { "submission_id": "aoj_2310_10408241", "code_snippet": "#include <iostream>\n#include <iomanip>\n#include <cassert>\n#include <vector>\n#include <algorithm>\n#include <utility>\n#include <numeric>\n#include <tuple>\n// #include \"Src/Number/IntegerDivision.hpp\"\n// #include \"Src/Utility/BinarySearch.hpp\"\n// #include \"Src/Sequence/CompressedSequence.hpp\"\n// #include \"Src/Sequence/RunLengthEncoding.hpp\"\n// #include \"Src/Algebra/Group/AdditiveGroup.hpp\"\n// #include \"Src/DataStructure/FenwickTree/FenwickTree.hpp\"\n// using namespace zawa;\n// #include \"atcoder/modint\"\n// using mint = atcoder::modint998244353;\nint H, W;\nstd::string S[610];\nbool in(int x, int y) {\n return 0 <= x and x < H and 0 <= y and y < W;\n}\nbool block(int x, int y) {\n return in(x, y) and S[x][y] == '#';\n}\nbool blank(int x, int y) {\n return !block(x, y);\n}\nusing item = std::pair<std::pair<int, int>, int>;\nint norm(std::pair<int, int> p) {\n return p.first * p.first + p.second * p.second;\n}\nint cross(std::pair<int, int> l, std::pair<int, int> r) {\n return l.first * r.second - l.second * r.first; \n}\nbool argcomp(std::pair<int, int> l, std::pair<int, int> r) {\n const int c = cross(l, r);\n if (c) return c > 0;\n else return norm(l) < norm(r);\n}\nbool comp(item l, item r) {\n return argcomp(l.first, r.first);\n}\nint main() {\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n std::ios::sync_with_stdio(false);\n std::cin >> H >> W;\n for (int i = 0 ; i < H ; i++) {\n std::cin >> S[i];\n }\n std::vector<item> P;\n for (int x = H - 1 ; x >= 0 ; x--) for (int y = 0 ; y < W ; y++) if (block(x, y)) {\n if (blank(x+1,y) and blank(x+1,y+1) and blank(x,y+1)) {\n P.push_back(std::make_pair(\n // std::make_pair(H-(x+1),y+1),1\n std::make_pair(y+1,H-(x+1)),1\n ));\n }\n if (blank(x-1,y) and blank(x-1,y-1) and blank(x,y-1)) {\n P.push_back(std::make_pair(\n // std::make_pair(H-x,y),-1\n std::make_pair(y,H-x),-1\n ));\n }\n if (blank(x-1,y) and blank(x-1,y+1) and blank(x,y+1)) {\n P.push_back(std::make_pair(\n // std::make_pair(H-x,y+1),0\n std::make_pair(y+1,H-x),0\n ));\n }\n if (blank(x+1,y) and blank(x+1,y-1) and blank(x,y-1)) {\n P.push_back(std::make_pair(\n // std::make_pair(H-(x+1),y),0\n std::make_pair(y,H-(x+1)),0\n ));\n }\n if (block(x-1,y) and blank(x-1,y+1) and block(x,y+1)) {\n P.push_back(std::make_pair(\n std::make_pair(y+1,H-x),0\n ));\n }\n if (block(x-1,y) and blank(x-1,y-1) and block(x,y-1)) {\n P.push_back(std::make_pair(\n std::make_pair(y,H-x),1\n ));\n }\n if (block(x+1,y) and blank(x+1,y+1) and block(x,y+1)) {\n P.push_back(std::make_pair(\n std::make_pair(y+1,H-x-1),-1\n ));\n }\n if (block(x+1,y) and blank(x+1,y-1) and block(x,y-1)) {\n P.push_back(std::make_pair(\n std::make_pair(y+1,H-x-1),0\n ));\n }\n }\n std::sort(P.begin(), P.end(), comp);\n // for (int k = 0 ; k < (int)P.size() ; k++) std::cout << P[k].first.first << ',' << P[k].first.second << \" : \" << P[k].second << '\\n';\n // std::cout.flush();\n int ans = 1, res = 1;\n for (int i = P[0].first == std::make_pair(0, 0), j = i ; i < (int)P.size() ; i = j) {\n while (j < (int)P.size() and cross(P[i].first, P[j].first) == 0) ans += P[j++].second;\n // for (int k = i ; k < j ; k++) std::cout << P[k].first.first << ',' << P[k].first.second << \" : \" << P[k].second << '\\n';\n // std::cout << \"----------------\" << std::endl;\n res = std::max(res, ans);\n }\n std::cout << res << '\\n';\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 8048, "score_of_the_acc": -0.1865, "final_rank": 10 }, { "submission_id": "aoj_2310_9999943", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define all(v) (v).begin(),(v).end()\n#define pb(a) push_back(a)\n#define rep(i, n) for(int i=0;i<n;i++)\n#define foa(e, v) for(auto&& e : v)\nusing ll = long long;\nconst ll MOD7 = 1000000007, MOD998 = 998244353, INF = (1LL << 60);\n#define dout(a) cout<<fixed<<setprecision(10)<<a<<endl;\n\nstruct node {\n int x, y, val;\n};\nint cross(int x1, int y1, int x2, int y2) {\n return x1 * y2 - x2 * y1;\n}\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n int h, w;\n cin >> h >> w;\n vector<string> s(h);\n rep(i, h) cin >> s[i];\n reverse(all(s));\n vector<node> v;\n rep(i, h) rep(j, w) {\n if(s[i][j] == '#') {\n if(i == h - 1 or s[i + 1][j] == '.') {\n v.push_back(node{j + 1, i + 1, 1});\n v.push_back(node{j, i + 1, -1});\n } \n if(j == w - 1 or s[i][j + 1] == '.') {\n v.push_back(node{j + 1, i, 1});\n v.push_back(node{j + 1, i + 1, -1});\n }\n }\n } \n sort(all(v), [&](node a, node b) {\n return cross(a.x, a.y, b.x, b.y) > 0;\n });\n int ans = 0, sum = 0;\n int sz = v.size();\n rep(i, sz) {\n sum += v[i].val;\n while(i + 1 < sz and cross(v[i].x, v[i].y, v[i + 1].x, v[i + 1].y) == 0) {\n i ++;\n sum += v[i].val;\n }\n ans = max(ans, sum);\n }\n cout << ans + 1 << endl;\n \n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 10336, "score_of_the_acc": -0.3643, "final_rank": 16 }, { "submission_id": "aoj_2310_9629481", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\nint main() {\n int h, w;\n cin >> h >> w;\n vector<string> v(h);\n for (auto &i : v)\n cin >> i;\n reverse(v.begin(), v.end());\n v.push_back(string(w, '.'));\n for (auto &i : v)\n i += '.';\n map<long double, int> x;\n for (int i = 0; i <= h; i++) {\n for (int j = 0; j <= w; j++) {\n if (i && v[i - 1][j] != v[i][j]) {\n x[(long double) i / (j + 1)]++;\n x[(long double) i / j]--;\n }\n if (j && v[i][j] != v[i][j - 1]) {\n x[(long double) (i + 1) / j]--;\n x[(long double) i / j]++;\n }\n }\n }\n int cnt = 0;\n if (v[0][0] == '#')\n cnt = 1;\n int ans = cnt;\n for (auto [_, t] : x) {\n cnt += t;\n ans = max(ans, cnt);\n }\n cout << ans / 2 + 1 << '\\n';\n}", "accuracy": 1, "time_ms": 110, "memory_kb": 21032, "score_of_the_acc": -1.3448, "final_rank": 18 }, { "submission_id": "aoj_2310_9563250", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n#include <unistd.h>\n\nnamespace fastio {\nstatic constexpr uint32_t SZ = 1 << 17;\nchar ibuf[SZ];\nchar obuf[SZ];\nchar out[100];\n// pointer of ibuf, obuf\n\n\nuint32_t pil = 0, pir = 0, por = 0;\n\nstruct Pre {\n char num[10000][4];\n constexpr Pre() : num() {\n for (int i = 0; i < 10000; i++) {\n int n = i;\n for (int j = 3; j >= 0; j--) {\n num[i][j] = n % 10 | '0';\n n /= 10;\n }\n }\n }\n} constexpr pre;\n\ninline void load() {\n memmove(ibuf, ibuf + pil, pir - pil);\n pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin);\n pil = 0;\n if (pir < SZ)\n ibuf[pir++] = '\\n';\n}\n\ninline void flush() {\n fwrite(obuf, 1, por, stdout);\n por = 0;\n}\n\nvoid rd(char &c) {\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n}\n\nvoid rd(string &x) {\n x.clear();\n char c;\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n do {\n x += c;\n if (pil == pir)\n load();\n c = ibuf[pil++];\n } while (!isspace(c));\n}\n\ntemplate <typename T> void rd_real(T &x) {\n string s;\n rd(s);\n x = stod(s);\n}\n\ntemplate <typename T> void rd_integer(T &x) {\n if (pil + 100 > pir)\n load();\n char c;\n do\n c = ibuf[pil++];\n while (c < '-');\n bool minus = 0;\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (c == '-') {\n minus = 1, c = ibuf[pil++];\n }\n }\n x = 0;\n while ('0' <= c) {\n x = x * 10 + (c & 15), c = ibuf[pil++];\n }\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (minus)\n x = -x;\n }\n}\n\nvoid rd(int &x) {\n rd_integer(x);\n}\nvoid rd(ll &x) {\n rd_integer(x);\n}\nvoid rd(i128 &x) {\n rd_integer(x);\n}\nvoid rd(uint &x) {\n rd_integer(x);\n}\nvoid rd(ull &x) {\n rd_integer(x);\n}\nvoid rd(u128 &x) {\n rd_integer(x);\n}\nvoid rd(double &x) {\n rd_real(x);\n}\nvoid rd(long double &x) {\n rd_real(x);\n}\n\ntemplate <class T, class U> void rd(pair<T, U> &p) {\n return rd(p.first), rd(p.second);\n}\ntemplate <size_t N = 0, typename T> void rd_tuple(T &t) {\n if constexpr (N < std::tuple_size<T>::value) {\n auto &x = std::get<N>(t);\n rd(x);\n rd_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void rd(tuple<T...> &tpl) {\n rd_tuple(tpl);\n}\n\ntemplate <size_t N = 0, typename T> void rd(array<T, N> &x) {\n for (auto &d : x)\n rd(d);\n}\ntemplate <class T> void rd(vector<T> &x) {\n for (auto &d : x)\n rd(d);\n}\n\nvoid read() {}\ntemplate <class H, class... T> void read(H &h, T &...t) {\n rd(h), read(t...);\n}\n\nvoid wt(const char c) {\n if (por == SZ)\n flush();\n obuf[por++] = c;\n}\nvoid wt(const string s) {\n for (char c : s)\n wt(c);\n}\nvoid wt(const char *s) {\n size_t len = strlen(s);\n for (size_t i = 0; i < len; i++)\n wt(s[i]);\n}\n\ntemplate <typename T> void wt_integer(T x) {\n if (por > SZ - 100)\n flush();\n if (x < 0) {\n obuf[por++] = '-', x = -x;\n }\n int outi;\n for (outi = 96; x >= 10000; outi -= 4) {\n memcpy(out + outi, pre.num[x % 10000], 4);\n x /= 10000;\n }\n if (x >= 1000) {\n memcpy(obuf + por, pre.num[x], 4);\n por += 4;\n } else if (x >= 100) {\n memcpy(obuf + por, pre.num[x] + 1, 3);\n por += 3;\n } else if (x >= 10) {\n int q = (x * 103) >> 10;\n obuf[por] = q | '0';\n obuf[por + 1] = (x - q * 10) | '0';\n por += 2;\n } else\n obuf[por++] = x | '0';\n memcpy(obuf + por, out + outi + 4, 96 - outi);\n por += 96 - outi;\n}\n\ntemplate <typename T> void wt_real(T x) {\n ostringstream oss;\n oss << fixed << setprecision(15) << double(x);\n string s = oss.str();\n wt(s);\n}\n\nvoid wt(int x) {\n wt_integer(x);\n}\nvoid wt(ll x) {\n wt_integer(x);\n}\nvoid wt(i128 x) {\n wt_integer(x);\n}\nvoid wt(uint x) {\n wt_integer(x);\n}\nvoid wt(ull x) {\n wt_integer(x);\n}\nvoid wt(u128 x) {\n wt_integer(x);\n}\nvoid wt(double x) {\n wt_real(x);\n}\nvoid wt(long double x) {\n wt_real(x);\n}\n\ntemplate <class T, class U> void wt(const pair<T, U> val) {\n wt(val.first);\n wt(' ');\n wt(val.second);\n}\ntemplate <size_t N = 0, typename T> void wt_tuple(const T t) {\n if constexpr (N < std::tuple_size<T>::value) {\n if constexpr (N > 0) {\n wt(' ');\n }\n const auto x = std::get<N>(t);\n wt(x);\n wt_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void wt(tuple<T...> tpl) {\n wt_tuple(tpl);\n}\ntemplate <class T, size_t S> void wt(const array<T, S> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\ntemplate <class T> void wt(const vector<T> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\n\nvoid print() {\n wt('\\n');\n}\ntemplate <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {\n wt(head);\n if (sizeof...(Tail))\n wt(' ');\n print(forward<Tail>(tail)...);\n}\nvoid __attribute__((destructor)) _d() {\n flush();\n}\n} // namespace fastio\n\n\nusing fastio::flush;\nusing fastio::print;\nusing fastio::read;\n\ninline void first(bool i = true) {\n print(i ? \"first\" : \"second\");\n}\ninline void Alice(bool i = true) {\n print(i ? \"Alice\" : \"Bob\");\n}\ninline void Takahashi(bool i = true) {\n print(i ? \"Takahashi\" : \"Aoki\");\n}\ninline void yes(bool i = true) {\n print(i ? \"yes\" : \"no\");\n}\ninline void Yes(bool i = true) {\n print(i ? \"Yes\" : \"No\");\n}\ninline void No() {\n print(\"No\");\n}\ninline void YES(bool i = true) {\n print(i ? \"YES\" : \"NO\");\n}\ninline void NO() {\n print(\"NO\");\n}\ninline void Yay(bool i = true) {\n print(i ? \"Yay!\" : \":(\");\n}\ninline void Possible(bool i = true) {\n print(i ? \"Possible\" : \"Impossible\");\n}\ninline void POSSIBLE(bool i = true) {\n print(i ? \"POSSIBLE\" : \"IMPOSSIBLE\");\n}\n\n/**\n * @brief Fast IO\n */\n\nint main() {\n int N, M;\n cin >> M >> N;\n vector<vector<char>> C(N, vector<char>(M));\n rrep(j,0,M) {\n rep(i,0,N) cin >> C[i][j];\n }\n rep(i,0,N) C[i].push_back('.');\n C.push_back(vector<char>(M+1,'.'));\n int COUNT = 1;\n rep(i,0,N) {\n if (C[i][0] == '#' && C[i+1][0] == '.') COUNT++;\n }\n vector<pair<pair<int,int>,int>> V;\n rep(i,1,N+1) {\n rep(j,1,M+1) {\n char a = C[i-1][j-1], b = C[i][j-1], c = C[i-1][j], d = C[i][j];\n if (a == b && a != c && a == d) V.push_back({{i,j},1});\n if (a != b && a == c && a == d) V.push_back({{i,j},-1});\n }\n }\n sort(V.begin(), V.end(), [](pair<pair<int,int>,int> P1, pair<pair<int,int>,int> P2) {\n if (P1.first.first * P2.first.second == P1.first.second * P2.first.first) return P1.second < P2.second;\n return P1.first.second * P2.first.first < P1.first.first * P2.first.second;\n });\n int ANS = COUNT;\n for (pair<pair<int,int>,int> P : V) {\n COUNT += P.second;\n chmax(ANS,COUNT);\n }\n cout << ANS << endl;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 5584, "score_of_the_acc": -0.0321, "final_rank": 3 }, { "submission_id": "aoj_2310_7219709", "code_snippet": "//#define _GLIBCXX_DEBUG\n\n#include<bits/stdc++.h>\nusing namespace std;\n\n#define endl '\\n'\n#define lfs cout<<fixed<<setprecision(10)\n#define ALL(a) (a).begin(),(a).end()\n#define ALLR(a) (a).rbegin(),(a).rend()\n#define UNIQUE(a) (a).erase(unique((a).begin(),(a).end()),(a).end())\n#define spa << \" \" <<\n#define fi first\n#define se second\n#define MP make_pair\n#define MT make_tuple\n#define PB push_back\n#define EB emplace_back\n#define rep(i,n,m) for(ll i = (n); i < (ll)(m); i++)\n#define rrep(i,n,m) for(ll i = (ll)(m) - 1; i >= (ll)(n); i--)\nusing ll = long long;\nusing ld = long double;\nconst ll MOD1 = 1e9+7;\nconst ll MOD9 = 998244353;\nconst ll INF = 1e18;\nusing P = pair<ll, ll>;\ntemplate<typename T> using PQ = priority_queue<T>;\ntemplate<typename T> using QP = priority_queue<T,vector<T>,greater<T>>;\ntemplate<typename T1, typename T2>bool chmin(T1 &a,T2 b){if(a>b){a=b;return true;}else return false;}\ntemplate<typename T1, typename T2>bool chmax(T1 &a,T2 b){if(a<b){a=b;return true;}else return false;}\nll median(ll a,ll b, ll c){return a+b+c-max({a,b,c})-min({a,b,c});}\nvoid ans1(bool x){if(x) cout<<\"Yes\"<<endl;else cout<<\"No\"<<endl;}\nvoid ans2(bool x){if(x) cout<<\"YES\"<<endl;else cout<<\"NO\"<<endl;}\nvoid ans3(bool x){if(x) cout<<\"Yay!\"<<endl;else cout<<\":(\"<<endl;}\ntemplate<typename T1,typename T2>void ans(bool x,T1 y,T2 z){if(x)cout<<y<<endl;else cout<<z<<endl;}\ntemplate<typename T1,typename T2,typename T3>void anss(T1 x,T2 y,T3 z){ans(x!=y,x,z);};\ntemplate<typename T>void debug(const T &v,ll h,ll w,string sv=\" \"){for(ll i=0;i<h;i++){cout<<v[i][0];for(ll j=1;j<w;j++)cout<<sv<<v[i][j];cout<<endl;}};\ntemplate<typename T>void debug(const T &v,ll n,string sv=\" \"){if(n!=0)cout<<v[0];for(ll i=1;i<n;i++)cout<<sv<<v[i];cout<<endl;};\ntemplate<typename T>void debug(const vector<T>&v){debug(v,v.size());}\ntemplate<typename T>void debug(const vector<vector<T>>&v){for(auto &vv:v)debug(vv,vv.size());}\ntemplate<typename T>void debug(stack<T> st){while(!st.empty()){cout<<st.top()<<\" \";st.pop();}cout<<endl;}\ntemplate<typename T>void debug(queue<T> st){while(!st.empty()){cout<<st.front()<<\" \";st.pop();}cout<<endl;}\ntemplate<typename T>void debug(deque<T> st){while(!st.empty()){cout<<st.front()<<\" \";st.pop_front();}cout<<endl;}\ntemplate<typename T>void debug(PQ<T> st){while(!st.empty()){cout<<st.top()<<\" \";st.pop();}cout<<endl;}\ntemplate<typename T>void debug(QP<T> st){while(!st.empty()){cout<<st.top()<<\" \";st.pop();}cout<<endl;}\ntemplate<typename T>void debug(const set<T>&v){for(auto z:v)cout<<z<<\" \";cout<<endl;}\ntemplate<typename T>void debug(const multiset<T>&v){for(auto z:v)cout<<z<<\" \";cout<<endl;}\ntemplate<typename T,size_t size>void debug(const array<T, size> &a){for(auto z:a)cout<<z<<\" \";cout<<endl;}\ntemplate<typename T,typename V>void debug(const map<T,V>&v){for(auto z:v)cout<<\"[\"<<z.first<<\"]=\"<<z.second<<\",\";cout<<endl;}\ntemplate<typename T>vector<vector<T>>vec(ll x, ll y, T w){vector<vector<T>>v(x,vector<T>(y,w));return v;}\nll gcd(ll x,ll y){ll r;while(y!=0&&(r=x%y)!=0){x=y;y=r;}return y==0?x:y;}\n//vector<ll>dx={1,-1,0,0,1,1,-1,-1};vector<ll>dy={0,0,1,-1,1,-1,1,-1};\ntemplate<typename T>vector<T> make_v(size_t a,T b){return vector<T>(a,b);}\ntemplate<typename... Ts>auto make_v(size_t a,Ts... ts){return vector<decltype(make_v(ts...))>(a,make_v(ts...));}\ntemplate<typename T1, typename T2>ostream &operator<<(ostream &os, const pair<T1, T2>&p){return os << p.first << \" \" << p.second;}\ntemplate<typename T>ostream &operator<<(ostream &os, const vector<T> &v){for(auto &z:v)os << z << \" \";cout<<\"|\"; return os;}\ntemplate<typename T>void rearrange(vector<int>&ord, vector<T>&v){\n auto tmp = v;\n for(int i=0;i<tmp.size();i++)v[i] = tmp[ord[i]];\n}\ntemplate<typename Head, typename... Tail>void rearrange(vector<int>&ord,Head&& head, Tail&&... tail){\n rearrange(ord, head);\n rearrange(ord, tail...);\n}\ntemplate<typename T> vector<int> ascend(const vector<T>&v){\n vector<int>ord(v.size());iota(ord.begin(),ord.end(),0);\n sort(ord.begin(),ord.end(),[&](int i,int j){return v[i]<v[j];});\n return ord;\n}\ntemplate<typename T> vector<int> descend(const vector<T>&v){\n vector<int>ord(v.size());iota(ord.begin(),ord.end(),0);\n sort(ord.begin(),ord.end(),[&](int i,int j){return v[i]>v[j];});\n return ord;\n}\ntemplate<typename T> vector<T> inv_perm(const vector<T>&ord){\n vector<T>inv(ord.size());\n for(int i=0;i<ord.size();i++)inv[ord[i]] = i;\n return inv;\n}\nll FLOOR(ll n,ll div){assert(div>0);return n>=0?n/div:(n-div+1)/div;}\nll CEIL(ll n,ll div){assert(div>0);return n>=0?(n+div-1)/div:n/div;}\nll digitsum(ll n){ll ret=0;while(n){ret+=n%10;n/=10;}return ret;}\nll modulo(ll n,ll d){return (n%d+d)%d;};\ntemplate<typename T>T min(const vector<T>&v){return *min_element(v.begin(),v.end());}\ntemplate<typename T>T max(const vector<T>&v){return *max_element(v.begin(),v.end());}\ntemplate<typename T>T acc(const vector<T>&v){return accumulate(v.begin(),v.end(),T(0));};\ntemplate<typename T>T reverse(const T &v){return T(v.rbegin(),v.rend());};\n//mt19937 mt(chrono::steady_clock::now().time_since_epoch().count());\nint popcount(ll x){return __builtin_popcountll(x);};\nint poplow(ll x){return __builtin_ctzll(x);};\nint pophigh(ll x){return 63 - __builtin_clzll(x);};\ntemplate<typename T>T poll(queue<T> &q){auto ret=q.front();q.pop();return ret;};\ntemplate<typename T>T poll(priority_queue<T> &q){auto ret=q.top();q.pop();return ret;};\ntemplate<typename T>T poll(QP<T> &q){auto ret=q.top();q.pop();return ret;};\ntemplate<typename T>T poll(stack<T> &s){auto ret=s.top();s.pop();return ret;};\nll MULT(ll x,ll y){if(LLONG_MAX/x<=y)return LLONG_MAX;return x*y;}\nll POW2(ll x, ll k){ll ret=1,mul=x;while(k){if(mul==LLONG_MAX)return LLONG_MAX;if(k&1)ret=MULT(ret,mul);mul=MULT(mul,mul);k>>=1;}return ret;}\nll POW(ll x, ll k){ll ret=1;for(int i=0;i<k;i++){if(LLONG_MAX/x<=ret)return LLONG_MAX;ret*=x;}return ret;}\ntemplate< typename T = int >\nstruct edge {\n int to;\n T cost;\n int id;\n edge():id(-1){};\n edge(int to, T cost = 1, int id = -1):to(to), cost(cost), id(id){}\n operator int() const { return to; }\n};\n\ntemplate<typename T>\nusing Graph = vector<vector<edge<T>>>;\ntemplate<typename T>\nGraph<T>revgraph(const Graph<T> &g){\n Graph<T>ret(g.size());\n for(int i=0;i<g.size();i++){\n for(auto e:g[i]){\n int to = e.to;\n e.to = i;\n ret[to].push_back(e);\n }\n }\n return ret;\n}\ntemplate<typename T>\nGraph<T> readGraph(int n,int m,int indexed=1,bool directed=false,bool weighted=false){\n Graph<T> ret(n);\n for(int es = 0; es < m; es++){\n int u,v;\n T w=1;\n cin>>u>>v;u-=indexed,v-=indexed;\n if(weighted)cin>>w;\n ret[u].emplace_back(v,w,es);\n if(!directed)ret[v].emplace_back(u,w,es);\n }\n return ret;\n}\ntemplate<typename T>\nGraph<T> readParent(int n,int indexed=1,bool directed=true){\n Graph<T>ret(n);\n for(int i=1;i<n;i++){\n int p;cin>>p;\n p-=indexed;\n ret[p].emplace_back(i);\n if(!directed)ret[i].emplace_back(p);\n }\n return ret;\n}\n\ntemplate <typename F>\nclass\n#if defined(__has_cpp_attribute) && __has_cpp_attribute(nodiscard)\n [[nodiscard]]\n#endif // defined(__has_cpp_attribute) && __has_cpp_attribute(nodiscard)\nFixPoint final : private F\n{\npublic:\n template <typename G>\n explicit constexpr FixPoint(G&& g) noexcept\n : F{std::forward<G>(g)}\n {}\n\n template <typename... Args>\n constexpr decltype(auto)\n operator()(Args&&... args) const\n#if !defined(__GNUC__) || defined(__clang__) || __GNUC__ >= 9\n noexcept(noexcept(F::operator()(std::declval<FixPoint>(), std::declval<Args>()...)))\n#endif // !defined(__GNUC__) || defined(__clang__) || __GNUC__ >= 9\n {\n return F::operator()(*this, std::forward<Args>(args)...);\n }\n}; // class FixPoint\n\n\n#if defined(__cpp_deduction_guides)\ntemplate <typename F>\nFixPoint(F&&)\n-> FixPoint<std::decay_t<F>>;\n#endif // defined(__cpp_deduction_guides)\n\n\nnamespace\n{\n template <typename F>\n#if !defined(__has_cpp_attribute) || !__has_cpp_attribute(nodiscard)\n # if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ >= 4)\n __attribute__((warn_unused_result))\n# elif defined(_MSC_VER) && _MSC_VER >= 1700 && defined(_Check_return_)\n _Check_return_\n# endif // defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ >= 4)\n#endif // !defined(__has_cpp_attribute) || !__has_cpp_attribute(nodiscard)\n inline constexpr decltype(auto)\n makeFixPoint(F&& f) noexcept\n {\n return FixPoint<std::decay_t<F>>{std::forward<std::decay_t<F>>(f)};\n }\n} // namespace\n\nusing Real = long double;\nusing Point = complex<Real>;\nconst Real EPS = 1e-10, PI = acos((Real)(-1.0));\n\ninline bool eq(Real a, Real b) { return fabs(b - a) < EPS; }\ninline bool eq(Point a, Point b) { return fabs(b - a) < EPS; }\n\nPoint operator*(const Point &p, const Real &d) {\n return Point(real(p) * d, imag(p) * d);\n}\n\nistream &operator>>(istream &is, Point &p) {\n Real a, b;\n is >> a >> b;\n p = Point(a, b);\n return is;\n}\n\nostream &operator<<(ostream &os, Point &p) {\n return os << fixed << setprecision(10) << p.real() << \" \" << p.imag();\n}\n\nPoint rotate(Real theta, const Point &p) {\n return Point(cos(theta) * p.real() - sin(theta) * p.imag(), sin(theta) * p.real() + cos(theta) * p.imag());\n}\n\nnamespace std {\n bool operator<(const Point& a, const Point& b) {\n return !eq(a.real(), b.real()) ? a.real() < b.real() : a.imag() < b.imag();\n }\n}\n\nstruct Line {\n Point a, b;\n Line() = default;\n Line(Point a, Point b): a(a), b(b) {}\n\n friend ostream &operator<<(ostream &os, Line &p) {\n return os << p.a << \" to \" << p.b;\n }\n\n friend istream &operator>>(istream &is, Line &a) {\n return is >> a.a >> a.b;\n }\n};\n\nstruct Circle {\n Point c;\n Real r;\n Circle() = default;\n Circle(Point c, Real r): c(c), r(r) {}\n\n friend ostream &operator<<(ostream &os, Circle &c) {\n return os << c.c << \": \" << c.r;\n }\n\n friend istream &operator>>(istream &is, Circle &c) {\n return is >> c.c >> c.r;\n }\n};\n\nReal cross(const Point &a, const Point &b) {\n return real(a) * imag(b) - real(b) * imag(a);\n}\n\nReal dot(const Point &a, const Point &b) {\n return real(a) * real(b) + imag(a) * imag(b);\n}\n\nstruct Segment : Line {\n Segment() = default;\n Segment(Point a, Point b) : Line(a, b) {}\n};\n\nint ccw(const Point &a, Point b, Point c) {\n b = b - a, c = c - a;\n if (cross(b, c) > EPS) return +1; // COUNTER_CLOCKWISE\n if (cross(b, c) < -EPS) return -1; // CLOCKWISE\n if (dot(b, c) < 0) return +2; // ONLINE_BACK\n if (norm(b) < norm(c)) return -2; // ONLINE_FRONT\n return 0; // ON_SEGMENT\n}\n\nbool intersect(const Segment &s, const Point &p) {\n return ccw(s.a, s.b, p) == 0;\n}\n\nbool intersect(const Segment &s, const Segment&t) {\n return ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 && ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0;\n}\n\nbool intersect(const Line &l, const Segment& s) {\n return cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < EPS;\n}\n\nint intersect(const Circle &c, const Circle &d) {\n Real dis = fabs(c.c - d.c);\n\n if (dis > c.r + d.r + EPS) return 4;\n else if (eq(dis, c.r + d.r)) return 3;\n else if (eq(dis, abs(c.r - d.r))) return 1;\n else if (dis + min(c.r, d.r) < max(c.r, d.r) - EPS) return 0;\n else return 2;\n}\n\nbool parallel(const Line &a, const Line &b) {\n return abs(cross(a.b - a.a, b.b - b.a)) < EPS;\n}\n\nbool orthogonal(const Line &a, const Line &b) {\n return abs(dot(a.b - a.a, b.b - b.a)) < EPS;\n}\n\nPoint projection(const Line& s, const Point &p) {\n double t = dot(p - s.a, s.b - s.a) / norm(s.b - s. a);\n return s.a + (s.b - s.a) * t;\n}\n\nPoint projection(const Segment& l, const Point &p) {\n return projection(Line(l), p);\n}\n\nPoint reflection(const Line &l, const Point &p) {\n Point r = projection(l, p);\n return p + (r - p) * 2;\n}\n\nReal distance(const Line &l, const Point &p) {\n Point r = projection(l, p);\n return fabs(p - r);\n}\n\nReal distance(const Segment&s, const Point &p) {\n Point r = projection(s, p);\n if (intersect(s, r)) return abs(r - p);\n return min(abs(s.a - p), abs(s.b - p));\n}\n\nReal distance(const Segment& a, const Segment& b) {\n if (intersect(a, b)) return 0;\n return min({\n distance(a, b.a),\n distance(a, b.b),\n distance(b, a.a),\n distance(b, a.b)\n });\n}\n\nbool contains(const Circle &c, const Point &p) {\n if (fabs(c.c - p) < c.r + EPS) return true;\n return false;\n}\n\nPoint crosspoint(const Line &l, const Line &m) {\n Real A = cross(l.b - l.a, m.b - m.a);\n Real B = cross(l.b - l.a, l.b - m.a);\n if (eq(abs(A), 0.0) && eq(abs(B), 0.0)) return m.a;\n return m.a + (m.b - m.a) * B / A;\n}\n\nPoint crosspoint(const Segment& l, const Segment& m) {\n return crosspoint(Line(l), Line(m));\n}\n\nLine bisector_of_angle(const Point& a, const Point& b, const Point& c) {\n Real d_ab = fabs(a - b);\n Real d_bc = fabs(b - c);\n Point c_ = b + (c - b) * d_ab / d_bc;\n\n return Line(b, (a + c_) * 0.5);\n}\n\nLine bisector(const Point& a, const Point& b) {\n Point c = (a + b) * 0.5;\n Point d = c + rotate(PI / 2, b - c);\n\n return Line(c, d);\n}\n\nint intersect(const Circle &c, const Line &l) {\n Real d = distance(l, c.c);\n\n if (d > c.r + EPS) return 0;\n if (eq(d, c.r)) return 1;\n return 2;\n}\n\npair<Point, Point> crosspoint(const Circle& c, const Line& l) {\n Real d = distance(l, c.c);\n\n Point p = projection(l, c.c);\n if (intersect(c, l) == 1) return { p, p };\n\n Real d2 = sqrt(c.r * c.r - d * d);\n Point v = (l.a - l.b) * (1.0 / fabs(l.a - l.b));\n\n return { p + d2 * v, p - d2 * v };\n}\n\nvector<Point> crosspoint(const Circle &c, const Segment &s) {\n int isect = intersect(c, Line(s));\n if (isect == 0) return {};\n\n auto [p1, p2] = crosspoint(c, Line(s));\n\n vector<Point> res;\n if (ccw(s.a, s.b, p1) == 0) res.push_back(p1);\n if (ccw(s.a, s.b, p2) == 0) res.push_back(p2);\n\n if (res.size() == 2 && ccw(s.a, p1, p2) == 0) swap(res[0], res[1]);\n return res;\n}\n\npair<Point, Point> crosspoint(const Circle &c1, const Circle &c2) {\n Real d = fabs(c1.c - c2.c);\n Real x1 = (c1.r * c1.r + d * d - c2.r * c2.r) / 2 / d;\n Real y = sqrt(c1.r * c1.r - x1 * x1);\n\n Point base = (c2.c * x1 + c1.c * (d - x1)) * (1.0 / d);\n Point v = rotate(PI / 2, c1.c - base);\n v = v / fabs(v);\n\n return { base + v * y, base - v * y };\n}\n\npair<Point, Point> tangent(const Circle &c, const Point &p) {\n Real d = fabs(c.c - p);\n Real d2 = sqrt(d * d - c.r * c.r);\n Real a = acos(d2 / d);\n\n\n Point q = c.c - p;\n q = q / fabs(q);\n\n// cout << d << \" \" << d2 << \" \" << a << \" \" << q << endl;\n return {\n p + rotate(a, q) * d2,\n p + rotate(-a, q) * d2\n };\n}\n\nvector<Segment> common_tangent(Circle c1, Circle c2) {\n int isect = intersect(c1, c2);\n Real d = fabs(c1.c - c2.c);\n Point v = (c2.c - c1.c) / d;\n\n vector<Segment> res;\n\n if (isect == 0) {\n return {};\n } else if (isect == 1) {\n auto [p1, p2] = crosspoint(c1, Line(c1.c, c2.c));\n if (eq(fabs(p2 - c2.c), c2.r)) swap(p1, p2);\n\n Point v = p1 + rotate(PI/2, c2.c - p1);\n return { Segment(p1, v) };\n } else if (isect == 2) {\n } else if (isect == 3) {\n auto [p1, p2] = crosspoint(c1, c2);\n res.emplace_back(p1, rotate(PI / 2, c1.c - p1) + p1);\n } else {\n Real a = d * c1.r / (c1.r + c2.r);\n Point p = c1.c + v * a;\n\n Real d1 = sqrt(a * a - c1.r * c1.r), d2 = sqrt(norm(p - c2.c) - c2.r * c2.r);\n Real theta = acos(d1 / a);\n\n// cout << d << endl;\n// cout << a << \" \" << d1 << \" \" << d2 << \" \" << theta << \": \" << p << endl;\n\n Point e = (c1.c - p) * (1.0 / fabs(c1.c - p));\n Point e1 = rotate(theta, e), e2 = rotate(-theta, e);\n\n res.emplace_back(p + e1 * d1, p - e1 * d2);\n res.emplace_back(p + e2 * d1, p - e2 * d2);\n }\n\n if (eq(c1.r, c2.r)) {\n Point e = rotate(PI / 2, v);\n res.push_back(Segment(c1.c + e * c1.r, c2.c + e * c1.r));\n res.push_back(Segment(c1.c - e * c1.r, c2.c - e * c1.r));\n return res;\n }\n\n if (c1.r > c2.r) swap(c1, c2);\n\n v = c1.c - c2.c;\n Point p = v * (1.0 / (c2.r - c1.r)) * c2.r + c2.c;\n\n auto [q1, q2] = tangent(c1, p);\n res.emplace_back(q1, crosspoint(c2, Line(p, q1)).first);\n res.emplace_back(q2, crosspoint(c2, Line(p, q2)).first);\n\n return res;\n}\n\nusing Polygon = vector<Point>;\n\nistream& operator>>(istream& is, Polygon &p) {\n for (int i = 0; i < p.size(); i++) is >> p[i];\n return is;\n}\n\nostream& operator<<(ostream& os, Polygon &p) {\n for (int i = 0; i < p.size(); i++) os << p[i];\n return os;\n}\n\nReal area(const Polygon& p) {\n Real res = 0;\n for (int i = 0; i < p.size(); i++) {\n res += cross(p[i], p[(i + 1) % p.size()]);\n }\n return abs(res)/2;\n}\n\nReal arcarea(const Circle &c, const Point &p1_, const Point &p2_) {\n Point p1 = p1_ - c.c, p2 = p2_ - c.c;\n Real a2 = atan2(imag(p2), real(p2));\n Point p = rotate(-a2, p1);\n Real a1 = atan2(imag(p), real(p));\n// cout << p1 << \" \" << p2 << \" --- \" << a2 << \" \" << a1 << endl;\n return - a1 / 2 * c.r * c.r;\n}\n\nint contains(Polygon g,Point p){\n int n=g.size();\n bool x=false;\n for(int i=0;i<n;i++){\n Point a=g[i]-p,b=g[(i+1)%n]-p;\n if(fabs(cross(a,b)) < EPS and dot(a,b) < EPS) return 1;\n if(a.imag()>b.imag()) swap(a,b);\n if(a.imag() < EPS and EPS < b.imag() and cross(a,b) > EPS ) {\n// cout << a << \" --- \" << b << \": \" << cross(a, b) << endl;\n x = !x;\n }\n }\n return (x?2:0);\n}\n\nPolygon convex_hull(Polygon Q) {\n sort(ALL(Q));\n\n Polygon upper({Q[0]}), lower({Q[0]});\n\n rep(i, 1, Q.size()) {\n while (upper.size() >= 2) {\n int _ccw = ccw(upper[upper.size() - 2], upper[upper.size() - 1], Q[i]);\n if (_ccw == 1) {\n upper.pop_back();\n } else {\n break;\n }\n }\n if (upper.size() <= 1) {\n upper.push_back(Q[i]);\n } else if (upper.size() > 1) {\n int _ccw = ccw(upper[upper.size() - 2], upper[upper.size() - 1], Q[i]);\n if (_ccw == -1 || _ccw == -2) {\n upper.push_back(Q[i]);\n }\n }\n }\n rep(i, 1, Q.size()) {\n while (lower.size() >= 2) {\n int _ccw = ccw(lower[lower.size() - 2], lower[lower.size() - 1], Q[i]);\n if (_ccw == -1) {\n lower.pop_back();\n } else {\n break;\n }\n }\n\n if (lower.size() <= 1) {\n lower.push_back(Q[i]);\n } else if (lower.size() > 1) {\n int _ccw = ccw(lower[lower.size() - 2], lower[lower.size() - 1], Q[i]);\n if (_ccw == 1 || _ccw == -2) {\n lower.push_back(Q[i]);\n }\n }\n }\n\n Polygon ret;\n\n rep(i, 0, lower.size()) {\n ret.push_back(lower[i]);\n// cout << lower[i] << endl;\n }\n\n// cout << \" | \" << endl;\n\n rrep(i, 0, upper.size()) {\n if (!eq(lower[0], upper[i]) && !eq(lower.back(), upper[i]))\n ret.push_back(upper[i]);\n// cout << upper[i] << endl;\n }\n\n int start = 0;\n\n// cout << ret.size() << endl;\n rep(i, 0, ret.size()) {\n if (ret[i].imag() < ret[start].imag() || (eq(ret[i].imag(), ret[start].imag()) && ret[i].real() < ret[start].real())) {\n start = i;\n }\n }\n\n cout << ret.size() << endl;\n rep(i, 0, ret.size()) {\n auto p = ret[(i + start) % ret.size()];\n cout << (int)p.real() << \" \" << (int)p.imag() << endl;\n }\n\n return ret;\n}\n\nReal closest_pair(vector<Point> &pts, int l, int r) {\n int m = (l + r) / 2;\n if (r - l <= 1) {\n return INF;\n } else if (r - l == 2) {\n if (imag(pts[l]) > imag(pts[l + 1])) swap(pts[l], pts[l + 1]);\n return fabs(pts[l] - pts[l + 1]);\n }\n\n Real mid = pts[m].real();\n Real d = min(closest_pair(pts, l, m), closest_pair(pts, m, r));\n\n inplace_merge(pts.begin() + l, pts.begin() + m, pts.begin() + r, [](const Point &a, const Point& b) {\n return a.imag() < b.imag();\n });\n\n vector<Point> ret;\n\n for (int i = l; i < r; i++) {\n if (fabs(pts[i].real() - mid) >= d) continue;\n\n for (int j = ret.size() - 1; j >= 0; j--) {\n if (fabs(imag(ret[j]) - imag(pts[i])) >= d) break;\n chmin(d, fabs(ret[j] - pts[i]));\n }\n\n ret.emplace_back(pts[i]);\n }\n\n return d;\n}\n\nint main() {\n cout << fixed << setprecision(10);\n\n int H, W;\n cin >> H >> W;\n vector<string> s(H);\n\n rep(i, 0, H) cin >> s[i];\n\n ll res = 1;\n ll cnt = 1;\n\n vector<P> pts;\n rep(i, 0, H + 1) {\n rep(j, 0, W + 1) {\n if (i == 0 && j == 0) continue;\n pts.emplace_back(i, j);\n }\n }\n\n auto same = [](P a, P b) {\n return a.fi * b.se - b.fi * a.se == 0;\n };\n\n auto cmp = [](const P &a, const P &b) {\n return a.fi * b.se - b.fi * a.se > 0;\n };\n\n sort(ALL(pts), cmp);\n\n auto in = [&](int y, int x) {\n return 0 <= y && y < H && 0 <= x && x < W;\n };\n\n auto tp = [&](int y, int x) {\n string t(\"....\");\n y = H - y;\n if (in(y - 1, x - 1) && s[y - 1][x - 1] == '#') t[0] = '#';\n if (in(y - 1, x) && s[y - 1][x] == '#') t[1] = '#';\n if (in(y, x - 1) && s[y][x - 1] == '#') t[2] = '#';\n if (in(y, x) && s[y][x] == '#') t[3] = '#';\n// cout << y << \" \" << x << \": \" << t << endl;\n\n if (t == \"...#\" || t == \"###.\") return 1;\n if (t == \"#...\" || t == \".###\") return -1;\n return 0;\n };\n\n rep(i, 0, pts.size()) {\n cnt += tp(pts[i].fi, pts[i].se);\n\n while (i < pts.size() && same(pts[i], pts[i+1])) {\n i++;\n cnt += tp(pts[i].fi, pts[i].se);\n }\n// cout << cnt << endl;\n chmax(res, cnt);\n }\n\n cout << res << endl;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 13768, "score_of_the_acc": -0.6138, "final_rank": 17 }, { "submission_id": "aoj_2310_6984595", "code_snippet": "#include <bits/stdc++.h>\n#include <complex>\n#include <random>\nusing namespace std;\n//#include <atcoder/all>\n//using namespace atcoder;\nusing ll = long long;\nusing vll = vector<ll>;\nusing vvll = vector<vll>;\nusing vvvll = vector<vvll>;\nusing vvvvll = vector<vvvll>;\nusing vvvvvll = vector<vvvvll>;\nusing vvvvvvll = vector<vvvvvll>;\nusing vb = vector<bool>;\nusing vvb = vector<vb>;\nusing vvvb = vector<vvb>;\nusing vvvvb = vector<vvvb>;\nusing vd = vector<double>;\nusing vvd = vector<vd>;\nusing vvvd = vector<vvd>;\nusing vvvvd = vector<vvvd>;\nusing vvvvvd = vector<vvvvd>;\n#define all(A) A.begin(),A.end()\n#define ALL(A) A.begin(),A.end()\n#define rep(i, n) for (ll i = 0; i < (ll) (n); i++)\nusing pqr = priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>>;\n#define tN(t) (t==1?XN:YN)\n#define tS(t) (t==1?XS:YS)\n#define tA(t) (t==1?XA:YA)\n\ntemplate<class T>\nbool chmax(T& p, T q, bool C = 1) {\n if (C == 0 && p == q) {\n return 1;\n }\n if (p < q) {\n p = q;\n return 1;\n }\n else {\n return 0;\n }\n}\n\ntemplate<class T>\nbool chmin(T& p, T q, bool C = 1) {\n if (C == 0 && p == q) {\n return 1;\n }\n if (p > q) {\n p = q;\n return 1;\n }\n else {\n return 0;\n }\n}\n\nll gcd(ll(a), ll(b)) {\n if (b == 0)return a;\n ll c = a;\n while (a % b != 0) {\n c = a % b;\n a = b;\n b = c;\n }\n return b;\n}\nll sqrtz(ll N) {\n ll L = 0;\n ll R = sqrt(N) + 10000;\n while (abs(R - L) > 1) {\n ll mid = (R + L) / 2;\n if (mid * mid <= N)L = mid;\n else R = mid;\n }\n return L;\n\n}\n\nvector<ll> fact, factinv, inv;\nll mod = 1e9 + 7;\nvoid prenCkModp(ll n) {\n fact.resize(n + 5);\n factinv.resize(n + 5);\n inv.resize(n + 5);\n fact.at(0) = fact.at(1) = 1;\n factinv.at(0) = factinv.at(1) = 1;\n inv.at(1) = 1;\n for (ll i = 2; i < n + 5; i++) {\n fact.at(i) = (fact.at(i - 1) * i) % mod;\n inv.at(i) = mod - (inv.at(mod % i) * (mod / i)) % mod;\n factinv.at(i) = (factinv.at(i - 1) * inv.at(i)) % mod;\n }\n\n}\nll nCk(ll n, ll k) {\n if (n < k) return 0;\n return fact.at(n) * (factinv.at(k) * factinv.at(n - k) % mod) % mod;\n}\n\n\n\nll modPow(ll a, ll n, ll p) {\n ll res = 1;\n ll k = a;\n while (n > 0) {\n if (n % 2 == 1) {\n res *= k;\n if (res > p)res %= p;\n }\n k = k * k;\n if (k > p)k %= p;\n n /= 2;\n }\n return res;\n}\n\nstruct quot {\n ll de, nu;//de分母\n quot() = default;\n quot(ll nu, ll de) :de(de), nu(nu) { init(*this); }\n quot(ll z) :de(1), nu(z) { init(*this); }\n void init(quot& a) {\n if (a.nu == 0) {\n a.de = 1;\n }\n else {\n ll g = GcD(a.de, a.nu);\n a.de /= g;\n a.nu /= g;\n if (a.de < 0) {\n a.de *= -1;\n a.nu *= -1;\n }\n }\n }\n ll GcD(ll(a), ll(b)) {\n ll c = a;\n while (a % b != 0) {\n c = a % b;\n a = b;\n b = c;\n }\n return b;\n }\n\n quot operator *(const quot& p) {\n quot res;\n res.de = this->de * p.de;\n res.nu = this->nu * p.nu;\n init(res);\n return res;\n };\n quot operator +(const quot& p) {\n quot res;\n res.de = this->de * p.de;\n res.nu = this->nu * p.de + this->de * p.nu;\n init(res);\n return res;\n };\n\n quot operator-()const {\n quot res;\n res.de = this->de;\n res.nu = -this->nu;\n return res;\n };\n quot operator -(const quot& p) {\n return *this + (-p);\n };\n quot inv(quot q) {\n quot res;\n res.de = q.nu;\n res.nu = q.de;\n return res;\n }\n quot operator/(const quot& q) {\n return *this * inv(q);\n }\n\n bool operator==(const quot& q) {\n return (this->de == q.de && this->nu == q.nu);\n }\n\n bool operator<(const quot& q)const {\n return (this->nu * q.de - this->de * q.nu < 0);\n }\n bool operator>(const quot& q) {\n return !(*this == q || *this < q);\n }\n\n double tod(const quot& q) {\n return (double(nu) / double(de));\n }\n\n void print() {\n cout << nu << \" \" << de << endl;\n }\n\n void printd() {\n cout << tod(*this) << endl;\n }\n};\n\n\n\n\n\n\nint main() {\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n\n ll H, W,an=0,m=1;\n cin >> H >> W;\n map<quot, ll> M;\n vector<string> S(H + 2, \"\");\n rep(w, W + 2)S[0].push_back('.'), S[H + 1].push_back('.');\n rep(h, H) {\n cin >> S[h+1];\n S[h+1] = '.' + S[h+1] + '.';\n }\n rep(h, H + 1)rep(w, W + 1) {\n ll cnt = 0;\n rep(dh, 2)rep(dw, 2)cnt += (S[h + dh][w + dw] == '#');\n if (cnt == 1) {\n if (S[h][w] == '#')M[quot(H - h, w)]++;\n else if (S[h + 1][w + 1] == '#')M[quot(H - h, w)]--;\n }\n if (cnt == 3) {\n if (S[h][w] == '.')M[quot(H - h, w)]++;\n else if (S[h + 1][w + 1] == '.')M[quot(H - h, w)]--;\n }\n }\n for (auto p : M) {\n m += p.second;\n chmax(an, m);\n }\n cout << an << endl;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 8116, "score_of_the_acc": -0.2252, "final_rank": 12 }, { "submission_id": "aoj_2310_6984578", "code_snippet": "#include <bits/stdc++.h>\n#include <complex>\n#include <random>\nusing namespace std;\n//#include <atcoder/all>\n//using namespace atcoder;\nusing ll = long long;\nusing vll = vector<ll>;\nusing vvll = vector<vll>;\nusing vvvll = vector<vvll>;\nusing vvvvll = vector<vvvll>;\nusing vvvvvll = vector<vvvvll>;\nusing vvvvvvll = vector<vvvvvll>;\nusing vb = vector<bool>;\nusing vvb = vector<vb>;\nusing vvvb = vector<vvb>;\nusing vvvvb = vector<vvvb>;\nusing vd = vector<double>;\nusing vvd = vector<vd>;\nusing vvvd = vector<vvd>;\nusing vvvvd = vector<vvvd>;\nusing vvvvvd = vector<vvvvd>;\n#define all(A) A.begin(),A.end()\n#define ALL(A) A.begin(),A.end()\n#define rep(i, n) for (ll i = 0; i < (ll) (n); i++)\nusing pqr = priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>>;\n#define tN(t) (t==1?XN:YN)\n#define tS(t) (t==1?XS:YS)\n#define tA(t) (t==1?XA:YA)\n\ntemplate<class T>\nbool chmax(T& p, T q, bool C = 1) {\n if (C == 0 && p == q) {\n return 1;\n }\n if (p < q) {\n p = q;\n return 1;\n }\n else {\n return 0;\n }\n}\n\ntemplate<class T>\nbool chmin(T& p, T q, bool C = 1) {\n if (C == 0 && p == q) {\n return 1;\n }\n if (p > q) {\n p = q;\n return 1;\n }\n else {\n return 0;\n }\n}\n\nll gcd(ll(a), ll(b)) {\n if (b == 0)return a;\n ll c = a;\n while (a % b != 0) {\n c = a % b;\n a = b;\n b = c;\n }\n return b;\n}\nll sqrtz(ll N) {\n ll L = 0;\n ll R = sqrt(N) + 10000;\n while (abs(R - L) > 1) {\n ll mid = (R + L) / 2;\n if (mid * mid <= N)L = mid;\n else R = mid;\n }\n return L;\n\n}\n\nvector<ll> fact, factinv, inv;\nll mod = 1e9 + 7;\nvoid prenCkModp(ll n) {\n fact.resize(n + 5);\n factinv.resize(n + 5);\n inv.resize(n + 5);\n fact.at(0) = fact.at(1) = 1;\n factinv.at(0) = factinv.at(1) = 1;\n inv.at(1) = 1;\n for (ll i = 2; i < n + 5; i++) {\n fact.at(i) = (fact.at(i - 1) * i) % mod;\n inv.at(i) = mod - (inv.at(mod % i) * (mod / i)) % mod;\n factinv.at(i) = (factinv.at(i - 1) * inv.at(i)) % mod;\n }\n\n}\nll nCk(ll n, ll k) {\n if (n < k) return 0;\n return fact.at(n) * (factinv.at(k) * factinv.at(n - k) % mod) % mod;\n}\n\n\n\nll modPow(ll a, ll n, ll p) {\n ll res = 1;\n ll k = a;\n while (n > 0) {\n if (n % 2 == 1) {\n res *= k;\n if (res > p)res %= p;\n }\n k = k * k;\n if (k > p)k %= p;\n n /= 2;\n }\n return res;\n}\n\nstruct quot {\n ll de, nu;//de分母\n quot() = default;\n quot(ll nu, ll de) :de(de), nu(nu) { init(*this); }\n quot(ll z) :de(1), nu(z) { init(*this); }\n void init(quot& a) {\n if (a.nu == 0) {\n a.de = 1;\n }\n else {\n ll g = GcD(a.de, a.nu);\n a.de /= g;\n a.nu /= g;\n if (a.de < 0) {\n a.de *= -1;\n a.nu *= -1;\n }\n }\n }\n ll GcD(ll(a), ll(b)) {\n ll c = a;\n while (a % b != 0) {\n c = a % b;\n a = b;\n b = c;\n }\n return b;\n }\n\n quot operator *(const quot& p) {\n quot res;\n res.de = this->de * p.de;\n res.nu = this->nu * p.nu;\n init(res);\n return res;\n };\n quot operator +(const quot& p) {\n quot res;\n res.de = this->de * p.de;\n res.nu = this->nu * p.de + this->de * p.nu;\n init(res);\n return res;\n };\n\n quot operator-()const {\n quot res;\n res.de = this->de;\n res.nu = -this->nu;\n return res;\n };\n quot operator -(const quot& p) {\n return *this + (-p);\n };\n quot inv(quot q) {\n quot res;\n res.de = q.nu;\n res.nu = q.de;\n return res;\n }\n quot operator/(const quot& q) {\n return *this * inv(q);\n }\n\n bool operator==(const quot& q) {\n return (this->de == q.de && this->nu == q.nu);\n }\n\n bool operator<(const quot& q)const {\n return (this->nu * q.de - this->de * q.nu < 0);\n }\n bool operator>(const quot& q) {\n return !(*this == q || *this < q);\n }\n\n double tod(const quot& q) {\n return (double(nu) / double(de));\n }\n\n void print() {\n cout << nu << \" \" << de << endl;\n }\n\n void printd() {\n cout << tod(*this) << endl;\n }\n};\n\n\n\n\n\n\nint main() {\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n\n ll H, W;\n cin >> H >> W;\n map<quot, ll> M;\n vector<string> S(H + 2, \"\");\n rep(w, W + 2)S[0].push_back('.'), S[H + 1].push_back('.');\n rep(h, H) {\n cin >> S[h+1];\n S[h+1] = '.' + S[h+1] + '.';\n }\n rep(h, H + 1) {\n rep(w, W + 1) {\n ll cnt = 0;\n rep(dh, 2)rep(dw, 2) {\n cnt += (S[h + dh][w + dw] == '#');\n \n }\n if (cnt == 1) {\n if (S[h][w] == '#') {\n M[quot(H - h, w)]++;\n }\n else if (S[h + 1][w + 1] == '#') {\n M[quot(H - h, w)]--;\n }\n }\n if (cnt == 3) {\n if (S[h][w] == '.') {\n M[quot(H - h, w)]++;\n }\n else if (S[h + 1][w + 1] == '.') {\n M[quot(H - h, w)]--;\n }\n }\n }\n }\n ll an = 0;\n ll m = 1;\n for (auto p : M) {\n m += p.second;\n chmax(an, m);\n }\n cout << an << endl;\n\n\n\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 8288, "score_of_the_acc": -0.236, "final_rank": 14 }, { "submission_id": "aoj_2310_6666218", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n// template {{{\nusing i32 = int;\nusing u32 = unsigned int;\nusing i64 = long long;\nusing u64 = unsigned long long;\n\n#define range(i, l, r) for (i64 i = (i64)(l); i < (i64)(r); (i) += 1)\n#define rrange(i, l, r) for (i64 i = (i64)(r) - 1; i >= (i64)(l); (i) -= 1)\n\n#define whole(f, x, ...) ([&](decltype((x)) container) { return (f)( begin(container), end(container), ## __VA_ARGS__); })(x)\n#define rwhole(f, x, ...) ([&](decltype((x)) container) { return (f)( rbegin(container), rend(container), ## __VA_ARGS__); })(x)\n\n#define debug(x) cerr << \"(\" << __LINE__ << \")\" << #x << \": \" << (x) << endl\n\nconstexpr i32 inf = 1001001001;\nconstexpr i64 infll = 1001001001001001001ll;\n\nconstexpr i32 dx[] = {0, -1, 1, 0, -1, 1, -1, 1}; \nconstexpr i32 dy[] = {-1, 0, 0, 1, -1, -1, 1, 1};\n\nstruct IoSetup { IoSetup(i32 x = 15){ cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(x); cerr << fixed << setprecision(x); } } iosetup;\n\ntemplate <typename T = i64> T input() { T x; cin >> x; return x; }\n\ntemplate <typename T> ostream &operator<<(ostream &os, vector<T> &v) { range(i, 0, v.size()) { os << v[i] << (i + 1 != (int)v.size() ? \" \" : \"\"); } return os; } \ntemplate <typename T> istream &operator>>(istream &is, vector<T> &v) { for (T &in : v) is >> in; return is; }\n\ntemplate <typename T1, typename T2> ostream &operator<<(ostream &os, pair<T1, T2> p) { os << \"(\" << p.first << \", \" << p.second << \")\"; return os; }\ntemplate <typename T1, typename T2> istream &operator>>(istream &is, pair<T1, T2> &p) { is >> p.first >> p.second; return is; }\n\ntemplate <typename T> vector<T> make_vector(size_t a, T b) { return vector<T>(a, b); }\ntemplate <typename... Ts> auto make_vector(size_t a, Ts... ts) { return vector<decltype(make_vector(ts...))>(a, make_vector(ts...)); }\n\ntemplate <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }\ntemplate <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }\n// }}}\n\nnamespace intgeometry2d {\n using isize = std::ptrdiff_t;\n using usize = std::size_t;\n\n using i32 = std::int_fast32_t;\n using i64 = std::int_fast64_t;\n using u32 = std::uint_fast32_t;\n using u64 = std::uint_fast64_t;\n} // intgeometry2d\n\nnamespace intgeometry2d {\n\n template< typename Z >\n class lattice_point {\n Z x_, y_;\n\n public:\n lattice_point() {}\n lattice_point(Z x_, Z y_) : x_(x_), y_(y_) {}\n \n Z x() const { return x_; }\n Z y() const { return y_; }\n\n bool operator==(const lattice_point &p) const { return x_ == p.x_ and y_ == p.y_; }\n bool operator!=(const lattice_point &p) const { return x_ != p.x_ or y_ != p.y_; }\n\n lattice_point operator+(lattice_point p) { return lattice_point(x_ + p.x_, y_ + p.y_); }\n lattice_point operator-(lattice_point p) { return lattice_point(x_ - p.x_, y_ - p.y_); }\n\n Z norm() const { return x_ * x_ + y_ * y_; }\n };\n\n} // intgeometry2d\n\nnamespace intgeometry2d {\n\n template< typename Z >\n Z det(const lattice_point<Z> &a, const lattice_point<Z> &b) {\n return a.x() * b.y() - a.y() * b.x();\n }\n\n} // intgeometry2d\n\nnamespace intgeometry2d {\n template< typename Z >\n bool argcmp(const lattice_point<Z> &a, const lattice_point<Z> &b) {\n using std::pair;\n bool fa = pair(a.y(), a.x()) < pair<Z, Z>(0, 0);\n bool fb = pair(b.y(), b.x()) < pair<Z, Z>(0, 0);\n Z d = det(a, b);\n return fa != fb ? fa < fb : (d == 0 ? a.norm() < b.norm() : d > 0);\n }\n} // intgeometry2d\n\nvoid solve() {\n int h = input(), w = input();\n\n auto cs = make_vector(h + 2, w + 2, '.');\n range(i, 1, h + 1) range(j, 1, w + 1) {\n cin >> cs[i][j];\n }\n\n using point = intgeometry2d::lattice_point<i32>;\n vector< pair<point, int> > pts;\n rrange(i, 0, h + 1) range(j, 0, w + 1) {\n // cs[ i ][j] cs[ i ][j+1]\n // cs[i+1][j] cs[i+1][j+1]\n // centor: (h - i, j) ?\n auto is_dot = [](char c) {\n return '.' == c;\n };\n\n bool ul = is_dot(cs[i][j]);\n bool ur = is_dot(cs[i][j + 1]);\n bool dl = is_dot(cs[i + 1][j]);\n bool dr = is_dot(cs[i + 1][j + 1]);\n\n // split pattern:\n // #. .#\n // .. or ##\n if ((ul != ur) and (ur == dl) and (dl == dr)) {\n pts.emplace_back(point(h - i, j), -1);\n }\n\n // merge pattern:\n // .. ##\n // .# or #.\n if (ul == ur and ur == dl and dl != dr) {\n pts.emplace_back(point(h - i, j), 1);\n }\n }\n\n pts.emplace_back(point(-1, -1), 0);\n\n auto cmp = [](pair<point, int> &a, pair<point, int> &b) {\n return intgeometry2d::argcmp(a.first, b.first);\n };\n whole(sort, pts, cmp);\n\n int ans = 1;\n int cnt = 2;\n range(i, 1, pts.size()) {\n point pre = pts[i - 1].first;\n point pt = pts[i].first;\n if (intgeometry2d::det(pre, pt)) {\n chmax(ans, cnt);\n }\n\n cnt += pts[i].second;\n }\n\n cout << ans << endl;\n}\n\nsigned main() {\n solve();\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 5156, "score_of_the_acc": -0.0053, "final_rank": 2 }, { "submission_id": "aoj_2310_6652175", "code_snippet": "#include <bits/extc++.h>\n\nusing namespace std;\n\n#define rep(i,n) for(int i=0;i<(n);++i)\n#define reps(i,s,n) for(int i=(s);i<=(n);++i)\n#define all(x) begin(x), end(x)\n#define Fixed fixed << setprecision(12)\n#define int int64_t\nusing pii = pair<int,int>;\nconstexpr int32_t INF = 0x3f3f3f3f;\nconstexpr int64_t LINF = 0x3f3f3f3f3f3f3f3fLL;\nconstexpr int mod1 = 1e9+7;\nconstexpr int mod2 = 998244353;\n\ntemplate <class Func>\nclass FixPoint : Func {\npublic:\n explicit constexpr FixPoint(Func&& f) noexcept : Func(forward<Func>(f)) {}\n\n template <class... Args>\n constexpr decltype(auto) operator()(Args&&... args) const {\n return Func::operator()(*this, std::forward<Args>(args)...);\n }\n};\n\ntemplate <class Func>\nstatic inline constexpr decltype(auto) makeFixPoint(Func&& f) noexcept {\n return FixPoint<Func>{forward<Func>(f)};\n}\n\ntemplate <class A, class B> inline bool chmax(A &a, const B &b) { return b > a && (a = b, true); }\ntemplate <class A, class B> inline bool chmin(A &a, const B &b) { return b < a && (a = b, true); }\n\ntemplate <class T> using max_heap = priority_queue<T>;\ntemplate <class T> using min_heap = priority_queue<T,vector<T>,greater<T> >;\ntemplate <class A, class B> using umap = unordered_map<A,B>;\n\ntemplate <class T> using Set = __gnu_pbds::tree<T, __gnu_pbds::null_type, less<T>, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update>;\n\ntemplate <class T> inline void bye(T x) { cout << x << '\\n'; exit(0); }\n\ninline int updiv(int a,int b){ return (a + b - 1) / b; }\n\nconstexpr int dx[] = {1,0,-1,0,1,1,-1,-1};\nconstexpr int dy[] = {0,-1,0,1,1,-1,-1,1};\n\n\nsigned main() {\n cin.tie(nullptr);\n ios_base::sync_with_stdio(false);\n\n int h, w;\n cin >> h >> w;\n\n vector<string> mat(h);\n\n for (int i = 0; i < h; ++i) cin >> mat[i];\n reverse(all(mat));\n\n vector<pair<int, int> > points;\n\n auto inside = [&](int y, int x) {\n return (0 <= y && y < h && 0 <= x && x < w);\n };\n\n for (int i = 0; i < h; ++i) {\n for (int j = 0; j < w; ++j) {\n if (mat[i][j] != '#') continue;\n for (int k = 4; k < 8; ++k) {\n vector<bool> flg(4);\n int y = i + dy[k], x = j + dx[k];\n if (inside(y, x) && mat[y][x] != '.') continue;\n if (y == -1 && x == -1) continue;\n // cout << i << ' ' << j << \" : \" << y << ' ' << x << '\\n';\n for (int s = 0; s < 4; ++s) {\n int ty = y + dy[s], tx = x + dx[s];\n if (inside(ty, tx) && mat[ty][tx] == '#') {\n flg[s] = true;\n }\n }\n if (k == 4) {\n if (flg[1] == flg[2]) points.emplace_back(i + 1, j + 1);\n } else if (k == 5) {\n if (flg[3] == flg[2]) points.emplace_back(i, j + 1);\n } else if (k == 6) {\n if (flg[0] == flg[3]) points.emplace_back(i, j);\n } else {\n if (flg[0] == flg[1]) points.emplace_back(i + 1, j);\n }\n }\n }\n }\n\n sort(all(points), [](const auto &a, const auto &b) -> bool {\n return (atan2l(a.first, a.second) < atan2l(b.first, b.second));\n });\n\n points.erase(unique(all(points)), points.end());\n\n // for (auto [y, x] : points) {\n // cout << y << ' ' << x << ' ' << atan2l(y, x) << '\\n';\n // }\n\n vector<vector<pair<int, int> > > points2;\n\n for (auto [y, x] : points) {\n if (points2.empty() || atan2l(points2.back().back().first, points2.back().back().second) != atan2l(y, x)) {\n points2.emplace_back(vector<pair<int, int> >(1, make_pair(y, x)));\n } else {\n points2.back().emplace_back(y, x);\n }\n }\n\n for (auto &v : points2) {\n sort(all(v), [](const auto &a, const auto &b) {\n return (a.second != b.second ? a.second < b.second : a.first < b.first);\n });\n }\n\n auto Tile = [&](int y, int x) {\n return !(!inside(y, x) || mat[y][x] == '.');\n };\n auto Type = [&](int y, int x) {\n if (Tile(y - 1, x - 1)) {\n if (Tile(y - 1, x) && Tile(y, x - 1)) return (0);\n if (!Tile(y - 1, x) && !Tile(y, x - 1)) return (1);\n }\n if (Tile(y, x - 1)) {\n if (Tile(y, x) && Tile(y - 1, x - 1)) return (2);\n if (!Tile(y, x) && !Tile(y - 1, x - 1)) return (3);\n }\n if (Tile(y, x)) {\n if (Tile(y, x - 1) && Tile(y - 1, x)) return (4);\n if (!Tile(y, x - 1) && !Tile(y - 1, x)) return (5);\n }\n if (Tile(y - 1, x)) {\n if (Tile(y, x) && Tile(y - 1, x - 1)) return (6); \n if (!Tile(y, x) && !Tile(y - 1, x - 1)) return (7);\n }\n return (-1);\n };\n\n int sz = points2.size(), cnt = 0, res = 0;\n for (int i = 0; i < sz - 1; ++i) {\n for (auto [y, x] : points2[i]) {\n // cout << y << ' ' << x << '\\n';\n switch (Type(y, x)) {\n case 0:\n break;\n case 1:\n break;\n case 2:\n cnt -= 2;\n break;\n case 3:\n cnt += 2;\n break;\n case 4:\n break;\n case 5:\n break;\n case 6:\n cnt += 2;\n break;\n case 7:\n cnt -= 2;\n break;\n default:\n // cout << \"Error\\n\";\n break;\n }\n }\n // cout << \"-----\\n>\";\n // cout << cnt << '\\n';\n // cout << \"-----\\n\";\n chmax(res, cnt);\n }\n\n cout << res / 2 + 1 << '\\n';\n\n return(0);\n}", "accuracy": 1, "time_ms": 300, "memory_kb": 12912, "score_of_the_acc": -1.4912, "final_rank": 19 }, { "submission_id": "aoj_2310_6624406", "code_snippet": "#include <bits/extc++.h>\n\nusing namespace std;\n\n#define rep(i,n) for(int i=0;i<(n);++i)\n#define reps(i,s,n) for(int i=(s);i<=(n);++i)\n#define all(x) begin(x), end(x)\n#define Fixed fixed << setprecision(12)\n#define int int64_t\nusing pii = pair<int,int>;\nconstexpr int32_t INF = 0x3f3f3f3f;\nconstexpr int64_t LINF = 0x3f3f3f3f3f3f3f3fLL;\nconstexpr int mod1 = 1e9+7;\nconstexpr int mod2 = 998244353;\n\ntemplate <class Func>\nclass FixPoint : Func {\npublic:\n explicit constexpr FixPoint(Func&& f) noexcept : Func(forward<Func>(f)) {}\n\n template <class... Args>\n constexpr decltype(auto) operator()(Args&&... args) const {\n return Func::operator()(*this, std::forward<Args>(args)...);\n }\n};\n\ntemplate <class Func>\nstatic inline constexpr decltype(auto) makeFixPoint(Func&& f) noexcept {\n return FixPoint<Func>{forward<Func>(f)};\n}\n\ntemplate <class A, class B> inline bool chmax(A &a, const B &b) { return b > a && (a = b, true); }\ntemplate <class A, class B> inline bool chmin(A &a, const B &b) { return b < a && (a = b, true); }\n\ntemplate <class T> using max_heap = priority_queue<T>;\ntemplate <class T> using min_heap = priority_queue<T,vector<T>,greater<T> >;\ntemplate <class A, class B> using umap = unordered_map<A,B>;\n\ntemplate <class T> using Set = __gnu_pbds::tree<T, __gnu_pbds::null_type, less<T>, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update>;\n\ntemplate <class T> inline void bye(T x) { cout << x << '\\n'; exit(0); }\n\ninline int updiv(int a,int b){ return (a + b - 1) / b; }\n\nconstexpr int dx[] = {1,0,-1,0,1,1,-1,-1};\nconstexpr int dy[] = {0,-1,0,1,1,-1,-1,1};\n\n\nsigned main() {\n cin.tie(nullptr);\n ios_base::sync_with_stdio(false);\n\n int h, w;\n cin >> h >> w;\n\n vector<string> mat(h);\n\n for (int i = 0; i < h; ++i) cin >> mat[i];\n reverse(all(mat));\n\n vector<pair<int, int> > points;\n\n auto inside = [&](int y, int x) {\n return (0 <= y && y < h && 0 <= x && x < w);\n };\n\n for (int i = 0; i < h; ++i) {\n for (int j = 0; j < w; ++j) {\n if (mat[i][j] != '#') continue;\n for (int k = 4; k < 8; ++k) {\n vector<bool> flg(4);\n int y = i + dy[k], x = j + dx[k];\n if (inside(y, x) && mat[y][x] != '.') continue;\n if (y == -1 && x == -1) continue;\n // cout << i << ' ' << j << \" : \" << y << ' ' << x << '\\n';\n for (int s = 0; s < 4; ++s) {\n int ty = y + dy[s], tx = x + dx[s];\n if (inside(ty, tx) && mat[ty][tx] == '#') {\n flg[s] = true;\n }\n }\n if (k == 4) {\n if (flg[1] == flg[2]) points.emplace_back(i + 1, j + 1);\n } else if (k == 5) {\n if (flg[3] == flg[2]) points.emplace_back(i, j + 1);\n } else if (k == 6) {\n if (flg[0] == flg[3]) points.emplace_back(i, j);\n } else {\n if (flg[0] == flg[1]) points.emplace_back(i + 1, j);\n }\n }\n }\n }\n\n sort(all(points), [](const auto &a, const auto &b) -> bool {\n return (atan2l(a.first, a.second) < atan2l(b.first, b.second));\n });\n\n points.erase(unique(all(points)), points.end());\n\n // for (auto [y, x] : points) {\n // cout << y << ' ' << x << ' ' << atan2l(y, x) << '\\n';\n // }\n\n vector<vector<pair<int, int> > > points2;\n\n for (auto [y, x] : points) {\n if (points2.empty() || atan2l(points2.back().back().first, points2.back().back().second) != atan2l(y, x)) {\n points2.emplace_back(vector<pair<int, int> >(1, make_pair(y, x)));\n } else {\n points2.back().emplace_back(y, x);\n }\n }\n\n for (auto &v : points2) {\n sort(all(v), [](const auto &a, const auto &b) {\n return (a.second != b.second ? a.second < b.second : a.first < b.first);\n });\n }\n\n auto Tile = [&](int y, int x) {\n return !(!inside(y, x) || mat[y][x] == '.');\n };\n auto Type = [&](int y, int x) {\n if (Tile(y - 1, x - 1)) {\n if (Tile(y - 1, x) && Tile(y, x - 1)) return (0);\n if (!Tile(y - 1, x) && !Tile(y, x - 1)) return (1);\n }\n if (Tile(y, x - 1)) {\n if (Tile(y, x) && Tile(y - 1, x - 1)) return (2);\n if (!Tile(y, x) && !Tile(y - 1, x - 1)) return (3);\n }\n if (Tile(y, x)) {\n if (Tile(y, x - 1) && Tile(y - 1, x)) return (4);\n if (!Tile(y, x - 1) && !Tile(y - 1, x)) return (5);\n }\n if (Tile(y - 1, x)) {\n if (Tile(y, x) && Tile(y - 1, x - 1)) return (6); \n if (!Tile(y, x) && !Tile(y - 1, x - 1)) return (7);\n }\n return (-1);\n };\n\n int sz = points2.size(), cnt = 0, res = 0;\n for (int i = 0; i < sz - 1; ++i) {\n for (auto [y, x] : points2[i]) {\n // cout << y << ' ' << x << '\\n';\n switch (Type(y, x)) {\n case 0:\n break;\n case 1:\n break;\n case 2:\n cnt -= 2;\n break;\n case 3:\n cnt += 2;\n break;\n case 4:\n break;\n case 5:\n break;\n case 6:\n cnt += 2;\n break;\n case 7:\n cnt -= 2;\n break;\n default:\n // cout << \"Error\\n\";\n break;\n }\n }\n // cout << \"-----\\n>\";\n // cout << cnt << '\\n';\n // cout << \"-----\\n\";\n chmax(res, cnt);\n }\n\n cout << res / 2 + 1 << '\\n';\n\n return(0);\n}", "accuracy": 1, "time_ms": 300, "memory_kb": 13512, "score_of_the_acc": -1.5288, "final_rank": 20 }, { "submission_id": "aoj_2310_6620054", "code_snippet": "#include<bits/stdc++.h>\n#define int ll\n#define rep(i, N) for(int i = 0; i < (int)N; ++i)\n#define rep1(i, N) for(int i = 1; i <= (int)N; ++i)\n#define per(i, N) for(int i = N-1; i >= 0; --i)\n#define per1(i, N) for(int i = N; i >= 1; --i)\n#define FOR(i, f, t) for(int i = f; i < (int)t; ++i)\n#define all(v) (v).begin(), (v).end()\n#define rall(v) (v).rbegin(), (v).rend()\n#define bit(n) (1ll << (n))\n#define pcnt(n) (__builtin_popcount(n))\n#define TakAo(ans) ans ? cout << \"Takahashi\\n\" : cout << \"Aoki\\n\"\n#define YesNo(ans) ans ? cout << \"Yes\\n\" : cout << \"No\\n\"\n#define endl '\\n'\n#define fi first\n#define se second\n#define mkpr make_pair\n#define mktpl make_tuple\n#define eb emplace_back\n\nusing namespace std;\nusing ll = int64_t;\nusing ull = uint64_t;\nusing ld = long double;\nusing point = struct{ ll x, y; };\ntemplate<class T> using max_heap = priority_queue<T>;\ntemplate<class T> using min_heap = priority_queue<T, vector<T>, greater<T>>;\ntemplate<class T> using vec = vector<T>;\ntemplate<class T> using vvec = vec<vec<T>>;\ntemplate<class T> using vvvec = vec<vec<vec<T>>>;\ntemplate<class T> using vvvvec = vec<vec<vec<vec<T>>>>;\n\nconstexpr ld EPS = 1e-10;\nconstexpr int INF = 1001001001;\nconstexpr ll LINF = 1001001001001001001ll;\nconstexpr int MOD = 1e9+7;\n//constexpr ll MOD = 998244353;\nconstexpr int div2 = (MOD + 1) >> 1;\nconstexpr int NIL = -1;\nconstexpr int pm[2] = {1, -1};\nconstexpr int dy[4] = {0, 1, 0, -1};\nconstexpr int dx[4] = {1, 0, -1, 0};\n\nll cel(ll a, ll b){ return (a + b - 1) / b;}\nll Gcd(ll a, ll b){ return b ? Gcd(b, a % b) : a;}\ntemplate<class T> T powi(T x, ll exp){\n return exp > 0 ? (exp & 1 ? x : 1) * powi(x * x, exp >> 1) : 1;\n}\nll modpow(ll x, ll exp, ll mod){\n x %= mod;\n return exp > 0 ? (exp & 1 ? x : 1) * modpow(x * x, exp >> 1, mod) % mod : 1;\n}\ntemplate<class T> bool chmin(T &a, T b){ return a > b ? (a = b, true) : 0;}\ntemplate<class T> bool chmax(T &a, T b){ return a < b ? (a = b, true) : 0;}\n\nusing P = pair<ll, ll>;\nusing Tpl = tuple<int, int, int, int>;\n\nbool comp(pair<point, ll> &a, pair<point, ll> &b){\n return a.fi.x * b.fi.y < a.fi.y * b.fi.x;\n}\n\nbool eq(pair<point, ll> &a, pair<point, ll> &b){\n return a.fi.x * b.fi.y == a.fi.y * b.fi.x;\n}\n\nvoid Main(){\n int H, W; cin >> H >> W;\n vvec<char> G(H + 10, vec<char>(W + 10, '.'));\n rep(i, H){\n rep(j, W) cin >> G[i + 1][j + 1];\n }\n vec<pair<point, ll>> XY;\n\n rep(i, H){\n rep(j, W){\n if(G[i + 1][j + 1] == '#'){\n if(G[i][j] == '.' && G[i + 1][j] == '.' && G[i][j + 1] == '.'){\n XY.eb(mkpr(point{j, H - i}, 1));\n }\n }\n else{\n if(G[i][j] == '#' && G[i + 1][j] == '#' && G[i][j + 1] == '#'){\n XY.eb(mkpr(point{j, H - i}, 1));\n }\n }\n }\n }\n rep(i, H){\n rep(j, W){\n if(G[i + 1][j + 1] == '#'){\n if(G[i + 2][j + 2] == '.' && G[i + 2][j + 1] == '.' && G[i + 1][j + 2] == '.'){\n XY.eb(mkpr(point{j + 1, H - i - 1}, -1));\n }\n }\n else{\n if(G[i + 2][j + 2] == '#' && G[i + 2][j + 1] == '#' && G[i + 1][j + 2] == '#'){\n XY.eb(mkpr(point{j + 1, H - i - 1}, -1));\n }\n }\n }\n }\n if(int(XY.size()) == 0){\n cout << 0 << endl;\n return;\n }\n\n sort(all(XY), comp);\n\n /* \n for(auto it : XY){\n cout << it.fi.x << \" \" << it.fi.y << \" \" << it.se << endl;\n }\n */\n \n int M = int(XY.size());\n pair<point, ll> prev = XY[0];\n int cnt = 2;\n int ans = 0;\n rep(i, M){\n if(i == 0) continue;\n if(eq(XY[i], prev)){\n cnt += XY[i].se;\n prev = XY[i];\n }\n else{\n chmax(ans, cnt);\n //cout << cnt << endl;\n cnt += XY[i].se;\n prev = XY[i];\n }\n }\n cout << ans << endl;\n}\n\nsigned main(){\n cin.tie(nullptr);\n ios_base::sync_with_stdio(false);\n cout << fixed << setprecision(10);\n Main();\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 7868, "score_of_the_acc": -0.1752, "final_rank": 8 }, { "submission_id": "aoj_2310_6620030", "code_snippet": "#include<bits/stdc++.h>\n#define int ll\n#define rep(i, N) for(int i = 0; i < (int)N; ++i)\n#define rep1(i, N) for(int i = 1; i <= (int)N; ++i)\n#define per(i, N) for(int i = N-1; i >= 0; --i)\n#define per1(i, N) for(int i = N; i >= 1; --i)\n#define FOR(i, f, t) for(int i = f; i < (int)t; ++i)\n#define all(v) (v).begin(), (v).end()\n#define rall(v) (v).rbegin(), (v).rend()\n#define bit(n) (1ll << (n))\n#define pcnt(n) (__builtin_popcount(n))\n#define TakAo(ans) ans ? cout << \"Takahashi\\n\" : cout << \"Aoki\\n\"\n#define YesNo(ans) ans ? cout << \"Yes\\n\" : cout << \"No\\n\"\n#define endl '\\n'\n#define fi first\n#define se second\n#define mkpr make_pair\n#define mktpl make_tuple\n#define eb emplace_back\n\nusing namespace std;\nusing ll = int64_t;\nusing ull = uint64_t;\nusing ld = long double;\nusing point = struct{ ll x, y; };\ntemplate<class T> using max_heap = priority_queue<T>;\ntemplate<class T> using min_heap = priority_queue<T, vector<T>, greater<T>>;\ntemplate<class T> using vec = vector<T>;\ntemplate<class T> using vvec = vec<vec<T>>;\ntemplate<class T> using vvvec = vec<vec<vec<T>>>;\ntemplate<class T> using vvvvec = vec<vec<vec<vec<T>>>>;\n\nconstexpr ld EPS = 1e-10;\nconstexpr int INF = 1001001001;\nconstexpr ll LINF = 1001001001001001001ll;\nconstexpr int MOD = 1e9+7;\n//constexpr ll MOD = 998244353;\nconstexpr int div2 = (MOD + 1) >> 1;\nconstexpr int NIL = -1;\nconstexpr int pm[2] = {1, -1};\nconstexpr int dy[4] = {0, 1, 0, -1};\nconstexpr int dx[4] = {1, 0, -1, 0};\n\nll cel(ll a, ll b){ return (a + b - 1) / b;}\nll Gcd(ll a, ll b){ return b ? Gcd(b, a % b) : a;}\ntemplate<class T> T powi(T x, ll exp){\n return exp > 0 ? (exp & 1 ? x : 1) * powi(x * x, exp >> 1) : 1;\n}\nll modpow(ll x, ll exp, ll mod){\n x %= mod;\n return exp > 0 ? (exp & 1 ? x : 1) * modpow(x * x, exp >> 1, mod) % mod : 1;\n}\ntemplate<class T> bool chmin(T &a, T b){ return a > b ? (a = b, true) : 0;}\ntemplate<class T> bool chmax(T &a, T b){ return a < b ? (a = b, true) : 0;}\n\nusing P = pair<ll, ll>;\nusing Tpl = tuple<int, int, int, int>;\n\nbool comp(pair<point, ll> &a, pair<point, ll> &b){\n return a.fi.x * b.fi.y < a.fi.y * b.fi.x;\n}\n\nbool eq(pair<point, ll> &a, pair<point, ll> &b){\n return a.fi.x * b.fi.y == a.fi.y * b.fi.x;\n}\n\nvoid Main(){\n int H, W; cin >> H >> W;\n vvec<char> G(H + 10, vec<char>(W + 10, '.'));\n rep(i, H){\n rep(j, W) cin >> G[i + 1][j + 1];\n }\n vec<pair<point, ll>> XY;\n\n rep(i, H){\n rep(j, W){\n if(G[i + 1][j + 1] == '#'){\n if(G[i][j] == '.' && G[i + 1][j] == '.' && G[i][j + 1] == '.'){\n XY.eb(mkpr(point{j, H - i}, 1));\n }\n }\n else{\n if(G[i][j] == '#' && G[i + 1][j] == '#' && G[i][j + 1] == '#'){\n XY.eb(mkpr(point{j, H - i}, 1));\n }\n }\n }\n }\n rep(I, H){\n rep(j, W){\n if(G[I + 1][j + 1] == '#'){\n if(G[I + 2][j + 2] == '.' && G[I + 2][j + 1] == '.' && G[I + 1][j + 2] == '.'){\n XY.eb(mkpr(point{j + 1, H - I - 1}, -1));\n }\n }\n else{\n if(G[I + 2][j + 2] == '#' && G[I + 2][j + 1] == '#' && G[I + 1][j + 2] == '#'){\n XY.eb(mkpr(point{j + 1, H - I - 1}, -1));\n }\n }\n }\n }\n if(int(XY.size()) == 0){\n cout << 0 << endl;\n return;\n }\n\n sort(all(XY), comp);\n\n /* \n for(auto it : XY){\n cout << it.fi.x << \" \" << it.fi.y << \" \" << it.se << endl;\n }\n */\n \n int M = int(XY.size());\n pair<point, ll> prev = XY[0];\n int cnt = 2;\n int ans = 0;\n rep(i, M){\n if(i == 0) continue;\n if(eq(XY[i], prev)){\n cnt += XY[i].se;\n prev = XY[i];\n }\n else{\n chmax(ans, cnt);\n //cout << cnt << endl;\n cnt += XY[i].se;\n prev = XY[i];\n }\n }\n cout << ans << endl;\n}\n\nsigned main(){\n cin.tie(nullptr);\n ios_base::sync_with_stdio(false);\n cout << fixed << setprecision(10);\n Main();\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 7980, "score_of_the_acc": -0.1822, "final_rank": 9 }, { "submission_id": "aoj_2310_6620029", "code_snippet": "#include<bits/stdc++.h>\n#define int ll\n#define rep(i, N) for(int i = 0; i < (int)N; ++i)\n#define rep1(i, N) for(int i = 1; i <= (int)N; ++i)\n#define per(i, N) for(int i = N-1; i >= 0; --i)\n#define per1(i, N) for(int i = N; i >= 1; --i)\n#define FOR(i, f, t) for(int i = f; i < (int)t; ++i)\n#define all(v) (v).begin(), (v).end()\n#define rall(v) (v).rbegin(), (v).rend()\n#define bit(n) (1ll << (n))\n#define pcnt(n) (__builtin_popcount(n))\n#define TakAo(ans) ans ? cout << \"Takahashi\\n\" : cout << \"Aoki\\n\"\n#define YesNo(ans) ans ? cout << \"Yes\\n\" : cout << \"No\\n\"\n#define endl '\\n'\n#define fi first\n#define se second\n#define mkpr make_pair\n#define mktpl make_tuple\n#define eb emplace_back\n\nusing namespace std;\nusing ll = int64_t;\nusing ull = uint64_t;\nusing ld = long double;\nusing point = struct{ ll x, y; };\ntemplate<class T> using max_heap = priority_queue<T>;\ntemplate<class T> using min_heap = priority_queue<T, vector<T>, greater<T>>;\ntemplate<class T> using vec = vector<T>;\ntemplate<class T> using vvec = vec<vec<T>>;\ntemplate<class T> using vvvec = vec<vec<vec<T>>>;\ntemplate<class T> using vvvvec = vec<vec<vec<vec<T>>>>;\n\nconstexpr ld EPS = 1e-10;\nconstexpr int INF = 1001001001;\nconstexpr ll LINF = 1001001001001001001ll;\nconstexpr int MOD = 1e9+7;\n//constexpr ll MOD = 998244353;\nconstexpr int div2 = (MOD + 1) >> 1;\nconstexpr int NIL = -1;\nconstexpr int pm[2] = {1, -1};\nconstexpr int dy[4] = {0, 1, 0, -1};\nconstexpr int dx[4] = {1, 0, -1, 0};\n\nll cel(ll a, ll b){ return (a + b - 1) / b;}\nll Gcd(ll a, ll b){ return b ? Gcd(b, a % b) : a;}\ntemplate<class T> T powi(T x, ll exp){\n return exp > 0 ? (exp & 1 ? x : 1) * powi(x * x, exp >> 1) : 1;\n}\nll modpow(ll x, ll exp, ll mod){\n x %= mod;\n return exp > 0 ? (exp & 1 ? x : 1) * modpow(x * x, exp >> 1, mod) % mod : 1;\n}\ntemplate<class T> bool chmin(T &a, T b){ return a > b ? (a = b, true) : 0;}\ntemplate<class T> bool chmax(T &a, T b){ return a < b ? (a = b, true) : 0;}\n\nusing P = pair<ll, ll>;\nusing Tpl = tuple<int, int, int, int>;\n\nbool comp(pair<point, ll> &a, pair<point, ll> &b){\n return a.fi.x * b.fi.y < a.fi.y * b.fi.x;\n}\n\nbool eq(pair<point, ll> &a, pair<point, ll> &b){\n return a.fi.x * b.fi.y == a.fi.y * b.fi.x;\n}\n\nvoid Main(){\n int H, W; cin >> H >> W;\n vvec<char> G(H + 700, vec<char>(W + 700, '.'));\n rep(i, H){\n rep(j, W) cin >> G[i + 1][j + 1];\n }\n vec<pair<point, ll>> XY;\n\n rep(i, H){\n rep(j, W){\n if(G[i + 1][j + 1] == '#'){\n if(G[i][j] == '.' && G[i + 1][j] == '.' && G[i][j + 1] == '.'){\n XY.eb(mkpr(point{j, H - i}, 1));\n }\n }\n else{\n if(G[i][j] == '#' && G[i + 1][j] == '#' && G[i][j + 1] == '#'){\n XY.eb(mkpr(point{j, H - i}, 1));\n }\n }\n }\n }\n rep(I, H){\n rep(j, W){\n if(G[I + 1][j + 1] == '#'){\n if(G[I + 2][j + 2] == '.' && G[I + 2][j + 1] == '.' && G[I + 1][j + 2] == '.'){\n XY.eb(mkpr(point{j + 1, H - I - 1}, -1));\n }\n }\n else{\n if(G[I + 2][j + 2] == '#' && G[I + 2][j + 1] == '#' && G[I + 1][j + 2] == '#'){\n XY.eb(mkpr(point{j + 1, H - I - 1}, -1));\n }\n }\n }\n }\n if(int(XY.size()) == 0){\n cout << 0 << endl;\n return;\n }\n\n sort(all(XY), comp);\n\n /* \n for(auto it : XY){\n cout << it.fi.x << \" \" << it.fi.y << \" \" << it.se << endl;\n }\n */\n \n int M = int(XY.size());\n pair<point, ll> prev = XY[0];\n int cnt = 2;\n int ans = 0;\n rep(i, M){\n if(i == 0) continue;\n if(eq(XY[i], prev)){\n cnt += XY[i].se;\n prev = XY[i];\n }\n else{\n chmax(ans, cnt);\n //cout << cnt << endl;\n cnt += XY[i].se;\n prev = XY[i];\n }\n }\n cout << ans << endl;\n}\n\nsigned main(){\n cin.tie(nullptr);\n ios_base::sync_with_stdio(false);\n cout << fixed << setprecision(10);\n Main();\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 8656, "score_of_the_acc": -0.2246, "final_rank": 11 }, { "submission_id": "aoj_2310_6619556", "code_snippet": "#include<bits/stdc++.h>\n#define int ll\n#define rep(i, N) for(int i = 0; i < (int)N; ++i)\n#define rep1(i, N) for(int i = 1; i <= (int)N; ++i)\n#define per(i, N) for(int i = N-1; i >= 0; --i)\n#define per1(i, N) for(int i = N; i >= 1; --i)\n#define FOR(i, f, t) for(int i = f; i < (int)t; ++i)\n#define all(v) (v).begin(), (v).end()\n#define rall(v) (v).rbegin(), (v).rend()\n#define bit(n) (1ll << (n))\n#define pcnt(n) (__builtin_popcount(n))\n#define TakAo(ans) ans ? cout << \"Takahashi\\n\" : cout << \"Aoki\\n\"\n#define YesNo(ans) ans ? cout << \"Yes\\n\" : cout << \"No\\n\"\n#define endl '\\n'\n#define fi first\n#define se second\n#define mkpr make_pair\n#define mktpl make_tuple\n#define eb emplace_back\n\nusing namespace std;\nusing ll = int64_t;\nusing ull = uint64_t;\nusing ld = long double;\nusing point = struct{ ll x, y; };\ntemplate<class T> using max_heap = priority_queue<T>;\ntemplate<class T> using min_heap = priority_queue<T, vector<T>, greater<T>>;\ntemplate<class T> using vec = vector<T>;\ntemplate<class T> using vvec = vec<vec<T>>;\ntemplate<class T> using vvvec = vec<vec<vec<T>>>;\ntemplate<class T> using vvvvec = vec<vec<vec<vec<T>>>>;\n\nconstexpr ld EPS = 1e-10;\nconstexpr int INF = 1001001001;\nconstexpr ll LINF = 1001001001001001001ll;\nconstexpr int MOD = 1e9+7;\n//constexpr ll MOD = 998244353;\nconstexpr int div2 = (MOD + 1) >> 1;\nconstexpr int NIL = -1;\nconstexpr int pm[2] = {1, -1};\nconstexpr int dy[4] = {0, 1, 0, -1};\nconstexpr int dx[4] = {1, 0, -1, 0};\n\nll cel(ll a, ll b){ return (a + b - 1) / b;}\nll Gcd(ll a, ll b){ return b ? Gcd(b, a % b) : a;}\ntemplate<class T> T powi(T x, ll exp){\n return exp > 0 ? (exp & 1 ? x : 1) * powi(x * x, exp >> 1) : 1;\n}\nll modpow(ll x, ll exp, ll mod){\n x %= mod;\n return exp > 0 ? (exp & 1 ? x : 1) * modpow(x * x, exp >> 1, mod) % mod : 1;\n}\ntemplate<class T> bool chmin(T &a, T b){ return a > b ? (a = b, true) : 0;}\ntemplate<class T> bool chmax(T &a, T b){ return a < b ? (a = b, true) : 0;}\n\nusing P = pair<ll, ll>;\nusing Tpl = tuple<int, int, int, int>;\n\nbool comp(const pair<point, ll> &a, const pair<point, ll> &b){\n return a.fi.x * b.fi.y < a.fi.y * b.fi.x;\n}\n\nbool eq(const pair<point, ll> &a, const pair<point, ll> &b){\n return a.fi.x * b.fi.y == a.fi.y * b.fi.x;\n}\n\nvoid Main(){\n int H, W; cin >> H >> W;\n vvec<char> G(H + 700, vec<char>(W + 700, '.'));\n rep(i, H){\n rep(j, W) cin >> G[i + 1][j + 1];\n }\n vec<pair<point, ll>> XY;\n\n rep(i, H){\n rep(j, W){\n if(G[i + 1][j + 1] == '#'){\n if(G[i][j] == '.' && G[i + 1][j] == '.' && G[i][j + 1] == '.'){\n XY.eb(mkpr(point{j, H - i}, 1));\n }\n }\n else{\n if(G[i][j] == '#' && G[i + 1][j] == '#' && G[i][j + 1] == '#'){\n XY.eb(mkpr(point{j, H - i}, 1));\n }\n }\n }\n }\n rep(I, H){\n rep(j, W){\n if(G[I + 1][j + 1] == '#'){\n if(G[I + 2][j + 2] == '.' && G[I + 2][j + 1] == '.' && G[I + 1][j + 2] == '.'){\n XY.eb(mkpr(point{j + 1, H - I - 1}, -1));\n }\n }\n else{\n if(G[I + 2][j + 2] == '#' && G[I + 2][j + 1] == '#' && G[I + 1][j + 2] == '#'){\n XY.eb(mkpr(point{j + 1, H - I - 1}, -1));\n }\n }\n }\n }\n if(int(XY.size()) == 0){\n cout << 0 << endl;\n return;\n }\n\n sort(all(XY), comp);\n\n /* \n for(auto it : XY){\n cout << it.fi.x << \" \" << it.fi.y << \" \" << it.se << endl;\n }\n */\n \n int M = int(XY.size());\n pair<point, ll> prev = XY[0];\n int cnt = 2;\n int ans = 0;\n rep(i, M){\n if(i == 0) continue;\n if(eq(XY[i], prev)){\n cnt += XY[i].se;\n prev = XY[i];\n }\n else{\n chmax(ans, cnt);\n //cout << cnt << endl;\n cnt += XY[i].se;\n prev = XY[i];\n }\n }\n cout << ans << endl;\n}\n\nsigned main(){\n cin.tie(nullptr);\n ios_base::sync_with_stdio(false);\n cout << fixed << setprecision(10);\n Main();\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 9460, "score_of_the_acc": -0.2749, "final_rank": 15 }, { "submission_id": "aoj_2310_6275397", "code_snippet": "#include \"bits/stdc++.h\"\nusing namespace std;\n\ntypedef string::const_iterator State;\n#define eps 1e-8L\n#define MAX_MOD 1000000007LL\n#define GYAKU 500000004LL\n#define MOD 998244353LL\n#define pb push_back\n#define mp make_pair\ntypedef long long ll;\ntypedef long double ld;\n#define REP(a, b) for (long long(a) = 0; (a) < (b); ++(a))\n#define ALL(x) (x).begin(), (x).end()\n\n#define int long long\nvoid solve(){\n int h, w;\n cin >> h >> w;\n vector<string> inputs;\n inputs.push_back(string(w + 2, '.'));\n REP(i, h)\n {\n string s;\n cin >> s;\n s = \".\" + s + \".\";\n inputs.push_back(s);\n }\n inputs.push_back(string(w + 2, '.'));\n vector<pair<double, int>> cnter;\n REP(i, h + 1)\n {\n REP(q, w + 1)\n {\n double ang = atan2(h - i, q); \n if(inputs[i][q] =='#' and inputs[i+1][q] == '.' and inputs[i][q+1] == '.' and inputs[i+1][q+1] == '.'){\n cnter.push_back({ang, 1});\n }\n if(inputs[i][q] =='.' and inputs[i+1][q] == '.' and inputs[i][q+1] == '.' and inputs[i+1][q+1] == '#'){\n cnter.push_back({ang, -1});\n }\n if(inputs[i][q] =='.' and inputs[i+1][q] == '#' and inputs[i][q+1] == '#' and inputs[i+1][q+1] == '#'){\n cnter.push_back({ang, 1});\n }\n if(inputs[i][q] =='#' and inputs[i+1][q] == '#' and inputs[i][q+1] == '#' and inputs[i+1][q+1] == '.'){\n cnter.push_back({ang, -1});\n }\n }\n }\n sort(ALL(cnter));\n int cur = 1;\n int ans = 2;\n for(auto x:cnter){\n cur += x.second;\n ans = max(ans, cur);\n }\n cout << ans << endl;\n}\n#undef int\n\n// generated by oj-template v4.7.2\n// (https://github.com/online-judge-tools/template-generator)\nint main() {\n // Fasterize input/output script\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << fixed << setprecision(100);\n // scanf/printf user should delete this fasterize input/output script\n\n int t = 1;\n //cin >> t; // comment out if solving multi testcase\n for (int testCase = 1; testCase <= t; ++testCase) {\n solve();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 6060, "score_of_the_acc": -0.0619, "final_rank": 6 }, { "submission_id": "aoj_2310_6028723", "code_snippet": "#include<iostream>\n#include<cstdio>\n#include<cstring>\n#include<string>\n#include<vector>\n#include<cmath>\n#include<algorithm>\n#include<map>\n#include<queue>\n#include<deque>\n#include<iomanip>\n#include<tuple>\n#include<cassert>\n#include<set>\n#include<complex>\n#include<numeric>\n#include<functional>\n#include<unordered_map>\n#include<unordered_set>\nusing namespace std;\ntypedef long long int LL;\ntypedef pair<int,int> P;\ntypedef pair<LL,LL> LP;\nconst int INF=1<<30;\nconst LL MAX=1e9+7;\n\nvoid array_show(int *array,int array_n,char middle=' '){\n for(int i=0;i<array_n;i++)printf(\"%d%c\",array[i],(i!=array_n-1?middle:'\\n'));\n}\nvoid array_show(LL *array,int array_n,char middle=' '){\n for(int i=0;i<array_n;i++)printf(\"%lld%c\",array[i],(i!=array_n-1?middle:'\\n'));\n}\nvoid array_show(vector<int> &vec_s,int vec_n=-1,char middle=' '){\n if(vec_n==-1)vec_n=vec_s.size();\n for(int i=0;i<vec_n;i++)printf(\"%d%c\",vec_s[i],(i!=vec_n-1?middle:'\\n'));\n}\nvoid array_show(vector<LL> &vec_s,int vec_n=-1,char middle=' '){\n if(vec_n==-1)vec_n=vec_s.size();\n for(int i=0;i<vec_n;i++)printf(\"%lld%c\",vec_s[i],(i!=vec_n-1?middle:'\\n'));\n}\ntemplate<typename T> ostream& operator<<(ostream& os,const vector<T>& v1){\n int n=v1.size();\n for(int i=0;i<n;i++){\n if(i)os<<\" \";\n os<<v1[i];\n }\n return os;\n}\ntemplate<typename T1,typename T2> ostream& operator<<(ostream& os,const pair<T1,T2>& p){\n os<<p.first<<\" \"<<p.second;\n return os;\n}\ntemplate<typename T> istream& operator>>(istream& is,vector<T>& v1){\n int n=v1.size();\n for(int i=0;i<n;i++)is>>v1[i];\n return is;\n}\ntemplate<typename T1,typename T2> istream& operator>>(istream& is,pair<T1,T2>& p){\n is>>p.first>>p.second;\n return is;\n}\n\nclass Fraction{\n typedef long long int ll;\n long long int gcd(long long int a,long long int b){\n if(a<b)swap(a,b);\n if(b==0)return a;\n return gcd(b,a%b);\n }\n\npublic:\n ll x,y;\n Fraction(){};\n Fraction(ll _x, ll _y):x(_x),y(_y){\n if(x==0 && y==0)return;\n if(y<0)x=-x,y=-y;\n ll d=gcd(abs(x),abs(y));\n x/=d,y/=d;\n }\n template<typename T> Fraction(pair<T,T> p){\n Fraction(p.first,p.second);\n }\n\n bool operator==(const Fraction f)const{\n return x==f.x && y==f.y;\n }\n bool operator<(const Fraction f)const{\n return x*f.y<f.x*y;\n }\n bool operator<=(const Fraction f)const{\n return x*f.y<=f.x*y;\n }\n bool operator>(const Fraction f)const{return !((*this)<=f);}\n bool operator>=(const Fraction f)const{return !((*this)<f);}\n};\n\nnamespace sol{\n\n void solve(){\n int n,m;\n int i,j,k;\n int a,b,c;\n cin>>n>>m;\n vector<vector<char>> grid(n+2,vector<char>(m+2));\n string sa;\n for(i=0;i<n;i++){\n cin>>sa;\n for(j=0;j<m;j++){\n if(sa[j]=='#')grid[i+1][j+1]=1;\n }\n }\n vector<pair<Fraction,int>> v1;\n for(i=0;i<=n;i++){\n for(j=0;j<=m;j++){\n c=0;\n for(a=0;a<2;a++){\n for(b=0;b<2;b++){\n c+=grid[i+a][j+b];\n }\n }\n if(c%2==0)continue;\n c=1-c/2;\n if(grid[i][j]==c)v1.push_back({Fraction(n-i,j),1});\n if(grid[i+1][j+1]==c)v1.push_back({Fraction(n-i,j),-1});\n }\n }\n sort(v1.begin(),v1.end());\n int s=0;\n int cnt=1;\n for(i=0;i<v1.size();i++){\n Fraction f=v1[i].first;\n a=v1[i].second;\n cnt+=a;\n if(i+1<v1.size() && f==v1[i+1].first)continue;\n s=max(s,cnt);\n }\n cout<<s<<endl;\n }\n}\n\nint main(){\n cin.tie(0);\n ios::sync_with_stdio(false);\n sol::solve();\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 7752, "score_of_the_acc": -0.1679, "final_rank": 7 }, { "submission_id": "aoj_2310_6027629", "code_snippet": "#pragma GCC ta g(\"avx2\")\n#pragma GCC optimize(\"Ofast\")\n#pragma GCC optimize(\"unroll-loops\")\n#include <bits/stdc++.h>\nusing namespace std;\n#define DEBUG\n#ifdef DEBUG\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << '(' << p.first << ',' << p.second << ')';\n return os;\n}\ntemplate <class T> ostream &operator<<(ostream &os, const vector<T> &v) {\n os << '{';\n for(int i = 0; i < (int)v.size(); i++) {\n if(i) { os << ','; }\n os << v[i];\n }\n os << '}';\n return os;\n}\nvoid debugg() { cerr << endl; }\ntemplate <class T, class... Args>\nvoid debugg(const T &x, const Args &... args) {\n cerr << \" \" << x;\n debugg(args...);\n}\n#define debug(...) \\\n cerr << __LINE__ << \" [\" << #__VA_ARGS__ << \"]: \", debugg(__VA_ARGS__)\n#define dump(x) cerr << __LINE__ << \" \" << #x << \" = \" << (x) << endl\n#else\n#define debug(...) (void(0))\n#define dump(x) (void(0))\n#endif\nusing namespace std;\ntypedef long long ll;\ntypedef vector<ll> vl;\ntypedef vector<vl> vvl;\ntypedef vector<char> vc;\ntypedef vector<string> vs;\ntypedef vector<bool> vb;\ntypedef vector<double> vd;\ntypedef pair<ll,ll> P;\ntypedef pair<int,int> pii;\ntypedef vector<P> vpl;\ntypedef tuple<ll,ll,ll> tapu;\n#define rep(i,n) for(int i=0; i<(n); i++)\n#define REP(i,a,b) for(int i=(a); i<(b); i++)\n#define all(x) (x).begin(), (x).end()\n#define rall(x) (x).rbegin(), (x).rend()\ntemplate<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){\n\tif(a>b){\n\t\ta = b; return true;\n\t}\n\telse return false;\n}\ntemplate<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){\n\tif(a<b){\n\t\ta = b; return true;\n\t}\n\telse return false;\n}\ntemplate<typename T> inline void print(T &a){\n int sz = a.size();\n for(auto itr = a.begin(); itr != a.end(); itr++){\n\t\tcout << *itr;\n sz--;\n if(sz) cout << \" \";\n\t}\n cout << \"\\n\";\n}\ntemplate<typename T1,typename T2> inline void print2(T1 a, T2 b){\n\tcout << a << \" \" << b << \"\\n\";\n}\ntemplate<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){\n\tcout << a << \" \" << b << \" \" << c << \"\\n\";\n}\nvoid mark() {cout << \"#\" << \"\\n\";}\nll pcount(ll x) {return __builtin_popcountll(x);}\n//#include <atcoder/maxflow>\n//using namespace atcoder;\nconst int inf = (1<<30)-1;\nconst ll linf = 1LL<<61;\nconst int MAX = 510000;\nint dy[8] = {0,1,0,-1,1,-1,-1,1};\nint dx[8] = {-1,0,1,0,1,-1,1,-1};\nconst double pi = acos(-1);\nconst double eps = 1e-9;\n//const int mod = 1e9 + 7;\nconst int mod = 998244353;\n\nint main(){\n\tint h,w; cin >> h >> w;\n\tvs s(h); rep(i,h) cin >> s[i];\n\trep(i,h) s[i].push_back('.');\n\ts.push_back(string(w+1,'.'));\n\tvector<pair<double,int>> ev;\n\trep(i,h) rep(j,w){\n\t\tchar a = s[i][j], b = s[i][j+1], c = s[i+1][j], d = s[i+1][j+1];\n\t\tif(a == b && b == c && a != d) ev.emplace_back((double)(h-i-1)/(j+1),-1);\n\t\tif(b == c && c == d && a != b) ev.emplace_back((double)(h-i-1)/(j+1),1);\n\t}\n\trep(j,w-1) if(s[0][j] == '.' && s[0][j+1] == '#') ev.emplace_back((double)h/(j+1),-1);\n\tsort(all(ev));\n\tint now = 0, ans = 0, mn = 0;\n\tfor(auto p : ev){\n\t\tnow += p.second;\n\t\tchmin(mn,now);\n\t\tchmax(ans, now-mn);\n\t}\n\tcout << ans+1 << endl;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 5764, "score_of_the_acc": -0.0434, "final_rank": 4 }, { "submission_id": "aoj_2310_5989166", "code_snippet": "#pragma GCC optimize(\"Ofast\")\n#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <map>\n#include <queue>\n#include <cstdio>\n#include <ctime>\n#include <assert.h>\n#include <chrono>\n#include <random>\n#include <numeric>\n#include <set>\n#include <deque>\n#include <stack>\n#include <sstream>\n#include <utility>\n#include <cstring>\n#include <unordered_map>\n#include <unordered_set>\n#include <tuple>\n#include <array>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned long long ull;\n\nmt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());\nll myRand(ll B) {\n return (ull)rng() % B;\n}\ninline double time() {\n return static_cast<double>(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count()) * 1e-9;\n}\n\nint main(){\n cin.tie(nullptr);\n ios::sync_with_stdio(false); \n int h,w; cin >> h >> w;\n vector<vector<char>> s(h+2,vector<char>(w+2,'.'));\n for(int i=0;i<h;i++){\n for(int j=0;j<w;j++){\n cin >> s[h-i][j+1];\n }\n }\n /*\n +1\n X. .X\n .. XX\n -1\n .. XX\n .X X.\n */\n vector<pair<pair<int,int>,int>> v;\n for(int i=0;i<=h;i++){\n for(int j=0;j<=w;j++){\n if(s[i][j] == '.' and s[i+1][j] == '#' and s[i][j+1] == '.' and s[i+1][j+1] == '.'){\n v.push_back({{i,j},1});\n }\n if(s[i][j] == '#' and s[i+1][j] == '.' and s[i][j+1] == '#' and s[i+1][j+1] == '#'){\n v.push_back({{i,j},1});\n }\n if(s[i][j] == '.' and s[i+1][j] == '.' and s[i][j+1] == '#' and s[i+1][j+1] == '.'){\n v.push_back({{i,j},-1});\n }\n if(s[i][j] == '#' and s[i+1][j] == '#' and s[i][j+1] == '.' and s[i+1][j+1] == '#'){\n v.push_back({{i,j},-1});\n }\n }\n }\n int res = 1;\n int cnt = 1;\n sort(v.begin(), v.end(), [&](auto i,auto j){\n int ix = i.first.second, iy = i.first.first;\n int jx = j.first.second, jy = j.first.first;\n // iy/ix < jy/jx\n if(iy*jx < jy*ix) return true;\n else if(iy*jx > jy*ix) return false;\n else{\n return i.second < j.second;\n }\n });\n for(int i=0;i<(int)v.size();i++){\n cnt += v[i].second;\n res = max(res, cnt);\n }\n cout << res << endl;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 5072, "score_of_the_acc": 0, "final_rank": 1 }, { "submission_id": "aoj_2310_5971590", "code_snippet": "#include <bits/stdc++.h>\n//#include <atcoder/all>\n//using namespace atcoder;\n#pragma GCC target (\"avx2\")\n#pragma GCC optimization (\"O3\")\n#pragma GCC optimization (\"unroll-loops\")\nusing namespace std;\n \ntypedef vector<int> VI;\ntypedef vector<VI> VVI;\ntypedef vector<string> VS;\ntypedef pair<int, int> PII;\ntypedef pair<int, int> pii;\ntypedef pair<long long, long long> PLL;\ntypedef pair<int, PII> TIII;\n \ntypedef long long ll;\ntypedef long double ld;\ntypedef unsigned long long ull;\n \n \n#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)\n#define REP(i, n) FOR(i, 0, n)\n#define rep(i, a, b) for (int i = a; i < (b); ++i)\n#define trav(a, x) for (auto &a : x)\n#define all(x) x.begin(), x.end()\n \n#define MOD 1000000007\n \ntemplate<class T1, class T2> inline bool chmax(T1 &a, T2 b) {if (a < b) {a = b; return true;} return false;}\ntemplate<class T1, class T2> inline bool chmin(T1 &a, T2 b) {if (a > b) {a = b; return true;} return false;}\nconst double EPS = 1e-8, PI = acos(-1);\nconst double pi = 3.141592653589793238462643383279;\n//ここから編集 \ntypedef string::const_iterator State;\nll GCD(ll a, ll b){\n return (b==0)?a:GCD(b, a%b);\n}\nll LCM(ll a, ll b){\n return a/GCD(a, b) * b;\n}\ntemplate< int mod >\nstruct ModInt {\n int x;\n \n ModInt() : x(0) {}\n \n ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}\n \n ModInt &operator+=(const ModInt &p) {\n if((x += p.x) >= mod) x -= mod;\n return *this;\n }\n \n ModInt &operator-=(const ModInt &p) {\n if((x += mod - p.x) >= mod) x -= mod;\n return *this;\n }\n \n ModInt &operator*=(const ModInt &p) {\n x = (int) (1LL * x * p.x % mod);\n return *this;\n }\n \n ModInt &operator/=(const ModInt &p) {\n *this *= p.inverse();\n return *this;\n }\n \n ModInt operator-() const { return ModInt(-x); }\n \n ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }\n \n ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }\n \n ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }\n \n ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }\n \n bool operator==(const ModInt &p) const { return x == p.x; }\n \n bool operator!=(const ModInt &p) const { return x != p.x; }\n \n ModInt inverse() const {\n int a = x, b = mod, u = 1, v = 0, t;\n while(b > 0) {\n t = a / b;\n swap(a -= t * b, b);\n swap(u -= t * v, v);\n }\n return ModInt(u);\n }\n \n ModInt pow(int64_t n) const {\n ModInt ret(1), mul(x);\n while(n > 0) {\n if(n & 1) ret *= mul;\n mul *= mul;\n n >>= 1;\n }\n return ret;\n }\n \n friend ostream &operator<<(ostream &os, const ModInt &p) {\n return os << p.x;\n }\n \n friend istream &operator>>(istream &is, ModInt &a) {\n int64_t t;\n is >> t;\n a = ModInt< mod >(t);\n return (is);\n }\n \n static int get_mod() { return mod; }\n};\n \nusing modint = ModInt< 1000000007 >;\ntemplate< typename T >\nstruct Combination {\n vector< T > _fact, _rfact, _inv;\n \n Combination(int sz) : _fact(sz + 1), _rfact(sz + 1), _inv(sz + 1) {\n _fact[0] = _rfact[sz] = _inv[0] = 1;\n for(int i = 1; i <= sz; i++) _fact[i] = _fact[i - 1] * i;\n _rfact[sz] /= _fact[sz];\n for(int i = sz - 1; i >= 0; i--) _rfact[i] = _rfact[i + 1] * (i + 1);\n for(int i = 1; i <= sz; i++) _inv[i] = _rfact[i] * _fact[i - 1];\n }\n \n inline T fact(int k) const { return _fact[k]; }\n \n inline T rfact(int k) const { return _rfact[k]; }\n \n inline T inv(int k) const { return _inv[k]; }\n \n T P(int n, int r) const {\n if(r < 0 || n < r) return 0;\n return fact(n) * rfact(n - r);\n }\n \n T C(int p, int q) const {\n if(q < 0 || p < q) return 0;\n return fact(p) * rfact(q) * rfact(p - q);\n }\n \n T H(int n, int r) const {\n if(n < 0 || r < 0) return (0);\n return r == 0 ? 1 : C(n + r - 1, r);\n }\n};\n \nll modpow(ll x, ll n, ll mod) {\n ll res = 1;\n x %= mod;\n if(x == 0) return 0;\n while(n) {\n if(n&1) res = (res * x) % mod;\n x = (x * x) % mod;\n n >>= 1;\n }\n return res;\n} \ninline long long mod(long long a, long long m) {\n return (a % m + m) % m;\n}\ntemplate<typename T> \nstruct BIT{\n int N;\n std::vector<T> node;\n BIT(){}\n BIT(int n){\n N = n;\n node.resize(N+10);\n }\n void build(int n) {\n N = n;\n node.resize(N+10);\n }\n /* a: 1-indexed */\n void add(int a, T x){\n for(int i=a; i<(int)node.size(); i += i & -i) node[i] += x;\n }\n\n /* [1, a] */\n T sum(int a){\n T res = 0;\n for(int x=a; x>0; x -= x & -x) res += node[x];\n return res;\n }\n\n /* [l, r] */\n T rangesum(int l, int r){\n return sum(r) - sum(l-1);\n }\n\n /* \n a1+a2+...+aw >= valとなるような最小のwを返す\n @verify https://codeforces.com/contest/992/problem/E\n */\n int lower_bound(T val) {\n if(val < 0) return 0;\n\n int res = 0;\n int n = 1; \n while (n < N) n *= 2;\n\n T tv=0;\n for (int k = n; k > 0; k /= 2) {\n if(res + k < N && node[res + k] < val){\n val -= node[res+k];\n res += k;\n }\n }\n return res+1; \n }\n};\nstruct UnionFind{\n std::vector<int> par;\n std::vector<int> siz;\n\n UnionFind(int sz_): par(sz_), siz(sz_) {\n for(int i=0; i<sz_; ++i) par[i] = i, siz[i] = 1;\n }\n\n void init(int sz_){\n par.resize(sz_);\n siz.resize(sz_);\n for(int i=0; i<sz_; ++i) par[i] = i, siz[i] = 1;\n }\n\n int root(int x){\n if(x == par[x]) return x;\n return par[x] = root(par[x]);\n }\n\n bool merge(int x, int y){\n x = root(x), y = root(y);\n if(x == y) return false;\n if(siz[x] < siz[y]) std::swap(x, y);\n siz[x] += siz[y];\n par[y] = x;\n return true;\n }\n\n bool issame(int x, int y){\n return root(x) == root(y);\n }\n\n int size(int x){\n return siz[root(x)];\n }\n};\nstruct RollingHash{\n\n using ull = unsigned long long;\n const ull mod = (1ULL << 61) - 1;\n const ull MASK30 = (1ULL << 30) - 1;\n const ull MASK31 = (1ULL << 31) - 1;\n\n const ull MASK61 = mod;\n\n ull base;\n int n;\n vector<ull> hash, pow;\n\n RollingHash(const string &s)\n {\n random_device rnd;\n mt19937_64 mt(rnd());\n base = 1001;\n \n n = (int)s.size();\n hash.assign(n+1, 0);\n pow.assign(n+1, 1);\n \n for(int i=0; i<n; i++){\n hash[i+1] = calc_mod(mul(hash[i], base) + s[i]);\n pow[i+1] = calc_mod(mul(pow[i], base));\n }\n }\n\n ull calc_mod(ull x){\n ull xu = x >> 61;\n ull xd = x & MASK61;\n ull res = xu + xd;\n if(res >= mod) res -= mod;\n return res;\n }\n\n ull mul(ull a, ull b){\n ull au = a >> 31;\n ull ad = a & MASK31;\n ull bu = b >> 31;\n ull bd = b & MASK31;\n ull mid = ad * bu + au * bd;\n ull midu = mid >> 30;\n ull midd = mid & MASK30;\n return calc_mod(au * bu * 2 + midu + (midd << 31) + ad * bd);\n }\n\n //[l,r)のハッシュ値\n inline ull get(int l, int r){\n ull res = calc_mod(hash[r] + mod*3-mul(hash[l], pow[r-l]));\n return res;\n }\n //string tのハッシュ値\n inline ull get(string t){\n ull res = 0;\n for(int i=0; i<t.size(); i++){\n res = calc_mod(mul(res, base)+t[i]);\n }\n return res;\n }\n //string s中のtの数をカウント\n inline int count(string t) {\n if(t.size() > n) return 0;\n auto hs = get(t);\n int res = 0;\n for(int i=0; i<n-t.size()+1; i++){\n if(get(i, i+t.size()) == hs) res++; \n }\n return res;\n }\n\n /* \n concat \n @verify https://codeforces.com/problemset/problem/514/C\n */\n inline ull concat(ull h1, ull h2, int h2len){\n return calc_mod(h2 + mul(h1, pow[h2len]));\n }\n\n // LCPを求める S[a:] T[b:]\n inline int LCP(int a, int b){\n int len = min((int)hash.size()-a, (int)hash.size()-b);\n \n int lb = -1, ub = len;\n while(ub-lb>1){\n int mid = (lb+ub)/2;\n\n if(get(a, a+mid) == get(b, b+mid)) lb = mid;\n else ub = mid;\n }\n return lb;\n }\n};\n\nint main() { \n cin.tie(0);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(12);\n \n int h, w; cin >> h >> w;\n h += 2; w += 2;\n vector<vector<char>> f(h, vector<char>(w, '.'));\n for(int i=1; i<h-1; i++) {\n for(int j=1; j<w-1; j++) {\n cin >> f[i][j];\n }\n }\n \n vector<pair<double, int>> v;\n REP(i,h-1) {\n REP(j,w-1) {\n int num = 0;\n double arg = atan2(h-2-i, j);\n if(f[i][j] == '#' && f[i][j+1] == '.' && f[i+1][j] == '.' && f[i+1][j+1] == '.') {\n v.push_back({arg, 1});\n }\n if(f[i][j] == '.' && f[i][j+1] == '#' && f[i+1][j] == '#' && f[i+1][j+1] == '#') {\n v.push_back({arg, 1});\n }\n if(f[i][j] == '#' && f[i][j+1] == '#' && f[i+1][j] == '#' && f[i+1][j+1] == '.') {\n v.push_back({arg, -1});\n }\n if(f[i][j] == '.' && f[i][j+1] == '.' && f[i+1][j] == '.' && f[i+1][j+1] == '#') {\n v.push_back({arg, -1});\n }\n }\n } \n sort(all(v));\n int ans = 2;\n int cnt = 1;\n REP(i,v.size()) {\n cnt += v[i].second;\n ans = max(ans, cnt);\n }\n cout << ans << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 6024, "score_of_the_acc": -0.0596, "final_rank": 5 } ]
aoj_2308_cpp
White Bird Angry Birds is a mobile game of a big craze all over the world. You were convinced that it was a waste of time to play the game, so you decided to create an automatic solver. You are describing a routine that optimizes the white bird's strategy to defeat a pig (enemy) by hitting an egg bomb. The white bird follows a parabolic trajectory from the initial position, and it can vertically drop egg bombs on the way. In order to make it easy to solve, the following conditions hold for the stages. N obstacles are put on the stage. Each obstacle is a rectangle whose sides are parallel to the coordinate axes. The pig is put on the point (X, Y) . You can launch the white bird in any direction at an initial velocity V from the origin. If the white bird collides with an obstacle, it becomes unable to drop egg bombs. If the egg bomb collides with an obstacle, the egg bomb is vanished. The acceleration of gravity is 9.8 {\rm m/s^2} . Gravity exerts a force on the objects in the decreasing direction of y -coordinate. Input A dataset follows the format shown below: N V X Y L_1 B_1 R_1 T_1 ... L_N B_N R_N T_N All inputs are integer. N : the number of obstacles V : the initial speed of the white bird X , Y : the position of the pig ( 0 \leq N \leq 50 , 0 \leq V \leq 50 , 0 \leq X, Y \leq 300 , X \neq 0 ) for 1 \leq i \leq N, L_i : the x-coordinate of the left side of the i -th obstacle B_i : the y-coordinate of the bottom side of the i -th obstacle R_i : the x-coordinate of the right side of the i -th obstacle T_i : the y-coordinate of the top side of the i -th obstacle ( 0 \leq L_i, B_i, R_i, T_i \leq 300 ) It is guaranteed that the answer remains unaffected by a change of L_i , B_i , R_i and T_i in 10^{-6} . Output Yes/No You should answer whether the white bird can drop an egg bomb toward the pig. Sample Input 1 0 7 3 1 Output for the Sample Input 1 Yes Sample Input 2 1 7 3 1 1 1 2 2 Output for the Sample Input 2 No Sample Input 3 1 7 2 2 0 1 1 2 Output for the Sample Input 3 No
[ { "submission_id": "aoj_2308_5846707", "code_snippet": "/*\tauthor: Kite_kuma\n\tcreated: 2021.09.01 15:05:47 */\n\n// #ifdef LOCAL\n// #define _GLIBCXX_DEBUG\n// #endif\n#include <bits/stdc++.h>\nusing namespace std;\n\n#pragma region macros\n\n#define foa(s, v) for(auto &s : v)\n#define all(v) (v).begin(), (v).end()\n#define rall(v) (v).rbegin(), (v).rend()\n#define popcnt(n) __builtin_popcountll((long long)n)\n\n#define REPname(a, b, c, d, e, ...) e\n#define rep(...) REPname(__VA_ARGS__, REP3, REP2, REP1, REP0)(__VA_ARGS__)\n#define REP0(x) for(int Counter_in_rep_macro = 0; Counter_in_rep_macro < (x); ++Counter_in_rep_macro)\n#define REP1(i, x) for(int i = 0; i < (x); ++i)\n#define REP2(i, l, r) for(int i = (l); i < (r); ++i)\n#define REP3(i, l, r, c) for(int i = (l); i < (r); i += (c))\n\n#define DREPname(a, b, c, d, e, ...) e\n#define drep(...) DREPname(__VA_ARGS__, DREP3, DREP2, DREP1)(__VA_ARGS__)\n#define DREP1(i, x) for(int i = (x)-1; i >= 0; --i)\n#define DREP2(i, l, r) for(int i = (r)-1; i >= (l); --i)\n#define DREP3(i, l, r, c) for(int i = (r)-1; i >= (l); i -= (c))\n\n#pragma endregion\n\n#pragma region aliases\n\nusing ll = long long;\nusing ld = long double;\nusing ull = unsigned long long;\nusing vi = std::vector<int>;\nusing vvi = std::vector<std::vector<int>>;\nusing vvvi = std::vector<std::vector<std::vector<int>>>;\nusing vll = std::vector<ll>;\nusing vvll = std::vector<vll>;\nusing vvvll = std::vector<vvll>;\nusing pii = std::pair<int, int>;\nusing pll = std::pair<long long, long long>;\ntemplate <class T = ll>\nusing V = std::vector<T>;\ntemplate <class T = ll>\nusing VV = V<V<T>>;\ntemplate <class T = ll>\nusing VVV = V<V<V<T>>>;\ntemplate <class T = ll>\nusing pqup = std::priority_queue<T, std::vector<T>, std::greater<T>>;\ntemplate <class T = ll>\nusing pqdn = std::priority_queue<T>;\n\n#pragma endregion\n\n#pragma region constants\n\nconst int inf = 1e9;\nconst long long INF = 1e18;\nconst long double pi = acos(-1);\nconst char dl = '\\n';\nconst char sp = ' ';\nint dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};\nint dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};\n\nconst int mod_1000000007 = 1000000007;\nconst int mod_998244353 = 998244353;\n\n#pragma endregion\n\n#pragma region basic_operation\n\ntemplate <class T0, class T1, class T2>\ninline bool in_range(T0 x, T1 lef, T2 rig) {\n\treturn ((lef <= x) && (x < rig));\n}\n\ntemplate <class T>\ninline bool chmin(T &a, T b) {\n\tif(a > b) {\n\t\ta = b;\n\t\treturn true;\n\t}\n\treturn false;\n}\ntemplate <class T>\ninline bool chmax(T &a, T b) {\n\tif(a < b) {\n\t\ta = b;\n\t\treturn true;\n\t}\n\treturn false;\n}\n\nvoid Yes(bool f = 1) { std::cout << (f ? \"Yes\" : \"No\") << '\\n'; }\nvoid No() { std::cout << \"No\\n\"; }\nvoid YES(bool f = 1) { std::cout << (f ? \"YES\" : \"NO\") << '\\n'; }\nvoid NO() { std::cout << \"NO\\n\"; }\n\ntemplate <class T>\nvoid drop(T answer) {\n\tstd::cout << answer << '\\n';\n\texit(0);\n}\n\nvoid err(bool flag = true) {\n\tif(!flag) return;\n\tstd::cout << -1 << '\\n';\n\texit(0);\n}\n\ntemplate <class T>\nvoid vout(std::vector<T> const &v, bool vertically = 0) {\n\tif(vertically) {\n\t\tfor(auto const &a : v) {\n\t\t\tstd::cout << a << '\\n';\n\t\t}\n\t\treturn;\n\t}\n\tfor(auto it = v.begin(); it != v.end(); it++) {\n\t\tstd::cout << (*it);\n\t\tif(it != v.end() - 1) {\n\t\t\tstd::cout << ' ';\n\t\t}\n\t}\n\tstd::cout << '\\n';\n\treturn;\n}\n\ninline void print() { std::cout << '\\n'; }\ntemplate <class T>\ninline void print(T x) {\n\tstd::cout << x << '\\n';\n\treturn;\n}\ntemplate <typename Head, typename... Tail>\nvoid print(Head H, Tail... T) {\n\tstd::cout << H << \" \";\n\tprint(T...);\n}\n\ntemplate <class T>\nvoid add(std::vector<T> &v, T val) {\n\tfor(auto &a : v) a += val;\n\treturn;\n}\n\ntemplate <class T>\nT dup(T a, T b) {\n\tassert(b != 0);\n\treturn (a + b - 1) / b;\n}\n\ntemplate <class T>\nT greatest_lower_multiple(T x, T d) {\n\tif(d == 0) return 0;\n\tif(d < 0) d *= -1;\n\tT y = x % d;\n\tif(y < 0) y += d;\n\treturn x - y;\n}\n\ntemplate <class T>\nT least_upper_multiple(T x, T d) {\n\treturn -greatest_lower_multiple(-x, d);\n}\n\nlong long POW(long long a, long long n) {\n\tlong long res = 1;\n\twhile(n > 0) {\n\t\tif(n & 1) res = res * a;\n\t\ta = a * a;\n\t\tn >>= 1;\n\t}\n\treturn res;\n}\n\nlong long modpow(long long a, long long n, long long mod) {\t // a^n mod\n\tassert(n >= 0 && mod);\n\tif(mod == 1) return 0LL;\n\tlong long res = 1;\n\ta %= mod;\n\twhile(n > 0) {\n\t\tif(n & 1) res = res * a % mod;\n\t\ta = a * a % mod;\n\t\tn >>= 1;\n\t}\n\treturn res < 0 ? res + mod : res;\n}\n\n// return x which satisfies a * x % mod == gcd(a, mod)\n// not (mod | a)\nlong long modinv(long long a, long long mod) {\n\tlong long b = mod, u = 1, v = 0;\n\twhile(b) {\n\t\tlong long t = a / b;\n\t\ta -= t * b;\n\t\tstd::swap(a, b);\n\t\tu -= t * v;\n\t\tstd::swap(u, v);\n\t}\n\tu %= mod;\n\tif(u < 0) u += mod;\n\treturn u;\n}\n\ntemplate <class T>\nint lb(const std::vector<T> &a, const T x) {\n\treturn std::distance(a.begin(), std::lower_bound(a.begin(), a.end(), x));\n}\ntemplate <class T>\nint ub(const std::vector<T> &a, const T x) {\n\treturn std::distance(a.begin(), std::upper_bound(a.begin(), a.end(), x));\n}\ntemplate <class T>\nvoid unq_sort(std::vector<T> &a) {\n\tstd::sort(a.begin(), a.end());\n\ta.erase(std::unique(a.begin(), a.end()), a.end());\n}\ntemplate <class T>\nstd::vector<int> press(std::vector<T> &a) {\n\tauto vec = a;\n\tunq_sort(vec);\n\tstd::vector<int> ret;\n\tfor(auto &v : a) ret.push_back(lb(vec, v));\n\treturn ret;\n}\n\n#pragma endregion\n\n#pragma region input\n#define VEC(type, name, size) \\\n\tvector<type> name(size); \\\n\tscanner::INPUT(name)\n#define VVEC(type, name, h, w) \\\n\tvector<vector<type>> name(h, vector<type>(w)); \\\n\tscanner::INPUT(name)\n#define INT(...) \\\n\tint __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define LL(...) \\\n\tlong long __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define STR(...) \\\n\tstring __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define CHR(...) \\\n\tchar __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define DBL(...) \\\n\tdouble __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define LD(...) \\\n\tlong double __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define TPL3(type0, type1, type2, name) \\\n\tstd::tuple<type0, type1, type2> name; \\\n\tscanner::INPUT(name);\n#define TPL4(type0, type1, type2, type3, name) \\\n\tstd::tuple<type0, type1, type2, type3> name; \\\n\tscanner::INPUT(name);\n\nnamespace scanner {\ntemplate <class T>\nvoid scan(T &a) {\n\tstd::cin >> a;\n}\n\ntemplate <class T, class L>\nvoid scan(std::pair<T, L> &p) {\n\tscan(p.first);\n\tscan(p.second);\n}\n\ntemplate <class T0, class T1, class T2>\nvoid scan(std::tuple<T0, T1, T2> &p) {\n\tT0 t0;\n\tT1 t1;\n\tT2 t2;\n\tscan(t0);\n\tscan(t1);\n\tscan(t2);\n\tp = std::make_tuple(t0, t1, t2);\n}\n\ntemplate <class T0, class T1, class T2, class T3>\nvoid scan(std::tuple<T0, T1, T2, T3> &p) {\n\tT0 t0;\n\tT1 t1;\n\tT2 t2;\n\tT3 t3;\n\tscan(t0);\n\tscan(t1);\n\tscan(t2);\n\tscan(t3);\n\tp = std::make_tuple(t0, t1, t2, t3);\n}\n\ntemplate <class T>\nvoid scan(std::vector<T> &a) {\n\tfor(auto &i : a) scan(i);\n}\n\nvoid INPUT() {}\ntemplate <class Head, class... Tail>\nvoid INPUT(Head &head, Tail &... tail) {\n\tscan(head);\n\tINPUT(tail...);\n}\n} // namespace scanner\n\ntemplate <typename T1, typename T2>\nstd::istream &operator>>(std::istream &is, std::pair<T1, T2> &p) {\n\tis >> p.first >> p.second;\n\treturn is;\n}\n#pragma endregion\n\n#pragma region debug\n\n#pragma region output\ntemplate <typename T1, typename T2>\nstd::ostream &std::operator<<(std::ostream &os, const std::pair<T1, T2> &p) {\n\tos << p.first << \" \" << p.second;\n\treturn os;\n}\ntemplate <class T>\nstd::ostream &std::operator<<(std::ostream &os, const std::vector<T> &v) {\n\tfor(int i = 0; i < (int)v.size(); i++) {\n\t\tif(i) os << \" \";\n\t\tos << v[i];\n\t}\n\treturn os;\n}\n#pragma endregion\n\n#pragma region view\n\nnamespace viewer {\nvoid view(const long long e) {\n\tif(e == INF)\n\t\tstd::cerr << \"INF\";\n\telse if(e == -INF)\n\t\tstd::cerr << \"-INF\";\n\telse\n\t\tstd::cerr << e;\n}\n\nvoid view(const int e) {\n\tif(e == inf)\n\t\tstd::cerr << \"inf\";\n\telse if(e == -inf)\n\t\tstd::cerr << \"-inf\";\n\telse\n\t\tstd::cerr << e;\n}\n\ntemplate <typename T>\nvoid view(const T e) {\n\tstd::cerr << e;\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::pair<T, U> &p) {\n\tstd::cerr << \"(\";\n\tview(p.first);\n\tstd::cerr << \", \";\n\tview(p.second);\n\tstd::cerr << \")\";\n}\n\ntemplate <class T0, class T1, class T2>\nvoid view(const std::tuple<T0, T1, T2> &p) {\n\tstd::cerr << \"(\";\n\tview(std::get<0>(p));\n\tstd::cerr << \", \";\n\tview(std::get<1>(p));\n\tstd::cerr << \", \";\n\tview(std::get<2>(p));\n\tstd::cerr << \")\";\n}\n\ntemplate <class T0, class T1, class T2, class T3>\nvoid view(const std::tuple<T0, T1, T2, T3> &p) {\n\tstd::cerr << \"(\";\n\tview(std::get<0>(p));\n\tstd::cerr << \", \";\n\tview(std::get<1>(p));\n\tstd::cerr << \", \";\n\tview(std::get<2>(p));\n\tstd::cerr << \", \";\n\tview(std::get<3>(p));\n\tstd::cerr << \")\";\n}\n\ntemplate <typename T>\nvoid view(const std::set<T> &s) {\n\tif(s.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << \"{ \";\n\tfor(auto &t : s) {\n\t\tview(t);\n\t\tstd::cerr << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\n\ntemplate <typename T>\nvoid view(const std::unordered_set<T> &s) {\n\tif(s.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << \"{ \";\n\tfor(auto &t : s) {\n\t\tview(t);\n\t\tstd::cerr << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\n\ntemplate <typename T>\nvoid view(const std::vector<T> &v) {\n\tif(v.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << \"{ \";\n\tfor(const auto &e : v) {\n\t\tview(e);\n\t\tstd::cerr << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\n\ntemplate <typename T>\nvoid view(const std::vector<std::vector<T>> &vv) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &v : vv) {\n\t\tstd::cerr << \"\\t\";\n\t\tview(v);\n\t\tstd::cerr << '\\n';\n\t}\n\tstd::cerr << \"}\";\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::vector<std::pair<T, U>> &v) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &c : v) {\n\t\tstd::cerr << \"\\t(\";\n\t\tview(c.first);\n\t\tstd::cerr << \", \";\n\t\tview(c.second);\n\t\tstd::cerr << \")\\n\";\n\t}\n\tstd::cerr << \"}\";\n}\n\ntemplate <class T0, class T1, class T2>\nvoid view(const std::vector<std::tuple<T0, T1, T2>> &v) {\n\tif(v.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << '{';\n\tfor(const auto &t : v) {\n\t\tstd::cerr << \"\\n\\t\";\n\t\tview(t);\n\t\tstd::cerr << \",\";\n\t}\n\tstd::cerr << \"\\n}\";\n}\n\ntemplate <class T0, class T1, class T2, class T3>\nvoid view(const std::vector<std::tuple<T0, T1, T2, T3>> &v) {\n\tif(v.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << '{';\n\tfor(const auto &t : v) {\n\t\tstd::cerr << \"\\n\\t\";\n\t\tview(t);\n\t\tstd::cerr << \",\";\n\t}\n\tstd::cerr << \"\\n}\";\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::map<T, U> &m) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &t : m) {\n\t\tstd::cerr << \"\\t[\";\n\t\tview(t.first);\n\t\tstd::cerr << \"] : \";\n\t\tview(t.second);\n\t\tstd::cerr << '\\n';\n\t}\n\tstd::cerr << \"}\";\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::unordered_map<T, U> &m) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &t : m) {\n\t\tstd::cerr << \"\\t[\";\n\t\tview(t.first);\n\t\tstd::cerr << \"] : \";\n\t\tview(t.second);\n\t\tstd::cerr << '\\n';\n\t}\n\tstd::cerr << \"}\";\n}\n} // namespace viewer\n\n#pragma endregion\n\n// when debugging : g++ foo.cpp -DLOCAL\n#ifdef LOCAL\nvoid debug_out() {}\ntemplate <typename Head, typename... Tail>\nvoid debug_out(Head H, Tail... T) {\n\tviewer::view(H);\n\tstd::cerr << \", \";\n\tdebug_out(T...);\n}\n#define debug(...) \\\n\tdo { \\\n\t\tstd::cerr << __LINE__ << \" [\" << #__VA_ARGS__ << \"] : [\"; \\\n\t\tdebug_out(__VA_ARGS__); \\\n\t\tstd::cerr << \"\\b\\b]\\n\"; \\\n\t} while(0)\n#define dump(x) \\\n\tdo { \\\n\t\tstd::cerr << __LINE__ << \" \" << #x << \" : \"; \\\n\t\tviewer::view(x); \\\n\t\tstd::cerr << '\\n'; \\\n\t} while(0)\n\n#else\n#define debug(...) (void(0))\n#define dump(x) (void(0))\n#endif\n\n#pragma endregion\n\nusing R = long double;\n\n#pragma region geometry_light\n\nusing Real = long double;\nusing R = Real;\nusing Point = std::complex<Real>;\nusing Vec = Point;\nconst Real EPS = 1e-10, PI = acos(-1);\n\ninline bool eq(const Real &a, const Real &b) { return fabs(b - a) < EPS; }\ninline bool eq(const Point &a, const Point &b) { return fabs(b - a) < EPS; }\n/*\n\t-1: a < b\n\t0 : a == b\n\t1 : a > b\n*/\ninline int compare(const Real &a, const Real &b) { return eq(a, b) ? 0 : a < b ? -1 : 1; }\ninline int sign(const Real &a) { return fabs(a) < EPS ? 0 : a < 0 ? -1 : 1; }\n\nnamespace std {\nconst Point operator*(const Point &p, const Real &d) { return Point(real(p) * d, imag(p) * d); }\n} // namespace std\n\nstd::istream &operator>>(std::istream &is, Point &p) {\n\tReal a, b;\n\tis >> a >> b;\n\tp = Point(a, b);\n\treturn is;\n}\nstd::ostream &operator<<(std::ostream &os, Point &p) { return os << std::fixed << std::setprecision(10) << p.real() << \" \" << p.imag(); }\n\n// rotate point 'p' for counter clockwise direction\nconst Point rotate(const Real &theta, const Point &p) {\n\treturn Point(cos(theta) * p.real() - sin(theta) * p.imag(), sin(theta) * p.real() + cos(theta) * p.imag());\n}\n\nconst Real radian_to_degree(const Real &r) { return (r * 180.0 / PI); }\n\nconst Real degree_to_radian(const Real &d) { return (d * PI / 180.0); }\n\n// get angle a-b-c (<pi)\nconst Real get_angle(const Point &a, const Point &b, const Point &c) {\n\tconst Point v(a - b), w(c - b);\n\tReal theta = fabs(atan2(w.imag(), w.real()) - atan2(v.imag(), v.real()));\n\treturn std::min(theta, 2 * PI - theta);\n}\n\nnamespace std {\nbool operator<(const Point &a, const Point &b) { return a.real() != b.real() ? a.real() < b.real() : a.imag() < b.imag(); }\n} // namespace std\n\nstruct Line {\n\tPoint a, b;\n\n\tLine() = default;\n\n\tLine(Point a, Point b) : a(a), b(b) {}\n\n\tLine(Real A, Real B, Real C) // Ax + By = C\n\t{\n\t\tif(eq(A, 0))\n\t\t\ta = Point(0, C / B), b = Point(1, C / B);\n\t\telse if(eq(B, 0))\n\t\t\tb = Point(C / A, 0), b = Point(C / A, 1);\n\t\telse\n\t\t\ta = Point(0, C / B), b = Point(C / A, 0);\n\t}\n\n\tfriend std::ostream &operator<<(std::ostream &os, Line &p) { return os << p.a << \" -- \" << p.b; }\n\n\tfriend std::istream &operator>>(std::istream &is, Line &a) { return is >> a.a >> a.b; }\n};\n\nstruct Segment : Line {\n\tSegment() = default;\n\n\tSegment(Point a, Point b) : Line(a, b) {}\n};\n\nstruct Circle {\n\tPoint p;\n\tReal r;\n\n\tCircle() = default;\n\n\tCircle(Point p, Real r) : p(p), r(r) {}\n};\n\nusing Points = std::vector<Point>;\nusing Polygon = std::vector<Point>;\nusing Segments = std::vector<Segment>;\nusing Lines = std::vector<Line>;\nusing Circles = std::vector<Circle>;\n\nconst Real cross(const Point &a, const Point &b) { return real(a) * imag(b) - imag(a) * real(b); }\nconst Real dot(const Point &a, const Point &b) { return real(a) * real(b) + imag(a) * imag(b); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_C\nconst int ccw(const Point &a, Point b, Point c) {\n\tb = b - a, c = c - a;\n\tif(cross(b, c) > EPS) return +1;\t\t// \"COUNTER_CLOCKWISE\"\n\tif(cross(b, c) < -EPS) return -1;\t\t// \"CLOCKWISE\"\n\tif(dot(b, c) < -EPS) return +2;\t\t\t// \"ONLINE_BACK\"\n\tif(norm(b) + EPS < norm(c)) return -2;\t// \"ONLINE_FRONT\"\n\treturn 0;\t\t\t\t\t\t\t\t// \"ON_SEGMENT\"\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_A\nbool parallel(const Line &a, const Line &b) { return eq(cross(a.b - a.a, b.b - b.a), 0.0); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_A\nbool orthogonal(const Line &a, const Line &b) { return eq(dot(a.a - a.b, b.a - b.b), 0.0); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_A\nPoint projection(const Line &l, const Point &p) {\n\tdouble t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n\treturn l.a + (l.a - l.b) * t;\n}\n\nPoint projection(const Segment &l, const Point &p) {\n\tdouble t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n\treturn l.a + (l.a - l.b) * t;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_B\nPoint reflection(const Line &l, const Point &p) { return projection(l, p) * 2.0 - p; }\n\nint intersect(const Line &l, const Point &p) { return int(abs(ccw(l.a, l.b, p)) != 1); }\n\nint intersect(const Line &l, const Line &m) {\n\tif(intersect(l, m.a) && intersect(l, m.b)) return -1;\n\treturn int(abs(cross(l.b - l.a, m.b - m.a)) > EPS);\n}\n\nint intersect(const Segment &s, const Point &p) { return int(ccw(s.a, s.b, p) == 0); }\n\nint intersect(const Line &l, const Segment &s) {\n\tif(intersect(l, s.a) && intersect(l, s.b)) return -1;\n\treturn cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < EPS;\n}\n\nReal distance(const Line &l, const Point &p);\n\nint intersect(const Circle &c, const Line &l) {\n\tReal d = c.r - distance(l, c.p);\n\treturn fabs(d) < EPS ? 1 : d > 0. ? 2 : 0;\n}\n\nint intersect(const Circle &c, const Point &p) { return int(abs(abs(p - c.p) - c.r) < EPS); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_B\nint intersect(const Segment &s, const Segment &t) {\n\tif(eq(s.a, s.b)) return intersect(t, s.a);\n\tif(intersect(Line(s), t.a) && intersect(Line(s), t.b) &&\n\t std::max(std::min(s.a, s.b), std::min(t.a, t.b)) < std::min(std::max(s.a, s.b), std::max(t.a, t.b)))\n\t\treturn -1;\n\treturn int(ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 && ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0);\n}\n\nint intersect(const Circle &c, const Segment &l) {\n\tconst Point h = projection(l, c.p);\n\tif(norm(h - c.p) - c.r * c.r > EPS) return 0;\n\tauto d1 = abs(c.p - l.a), d2 = abs(c.p - l.b);\n\tif(compare(d1, c.r) == -1 && compare(d2, c.r) == -1) return 0;\n\tif(d1 < c.r - EPS && d2 > c.r - EPS || d1 > c.r - EPS && d2 < c.r - EPS) return 1;\n\treturn dot(l.a - h, l.b - h) < 0 ? 2 : 0;\n}\n\nReal distance(const Point &a, const Point &b);\n\nint number_of_common_tangents(const Circle &c1, const Circle &c2) {\n\tReal r1 = std::min(c1.r, c2.r), r2 = std::max(c1.r, c2.r), d = distance(c1.p, c2.p);\n\tint com = compare(r1 + r2, d);\n\treturn com == 1 ? compare(d + r1, r2) + 1 : 3 - com;\n\t// if(c1.r < c2.r) swap(c1, c2);\n\t// Real d = abs(c1.p - c2.p);\n\t// if(compare(c1.r + c2.r, d) == -1) return 4;\n\t// if(eq(c1.r + c2.r, d)) return 3;\n\t// if(compare(c1.r - c2.r, d) == -1) return 2;\n\t// return int(eq(c1.r - c2.r, d));\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_A&lang=jp\nint intersect(const Circle &c1, const Circle &c2) { return 2 - abs(2 - number_of_common_tangents(c1, c2)); }\n\nReal distance(const Point &a, const Point &b) { return abs(a - b); }\n\nReal distance(const Line &l, const Point &p) { return abs(p - projection(l, p)); }\n\nReal distance(const Line &l, const Line &m) { return intersect(l, m) ? 0 : distance(l, m.a); }\n\nReal distance(const Segment &s, const Point &p) {\n\tPoint r = projection(s, p);\n\treturn intersect(s, r) ? distance(r, p) : std::min(abs(s.a - p), abs(s.b - p));\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_D\nReal distance(const Segment &a, const Segment &b) {\n\tif(intersect(a, b)) return 0;\n\treturn std::min({distance(a, b.a), distance(a, b.b), distance(b, a.a), distance(b, a.b)});\n}\n\nReal distance(const Line &l, const Segment &s) {\n\tif(intersect(l, s)) return 0;\n\treturn std::min(distance(l, s.a), distance(l, s.b));\n}\n\nPoint crosspoint(const Line &l, const Line &m) {\n\tReal A = cross(l.b - l.a, m.b - m.a);\n\tReal B = cross(l.b - l.a, l.b - m.a);\n\tif(eq(abs(A), 0.0) && eq(abs(B), 0.0)) return m.a;\n\treturn m.a + (m.b - m.a) * B / A;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_C\nPoint crosspoint(const Segment &l, const Segment &m) { return crosspoint(Line(l), Line(m)); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_D\nstd::pair<Point, Point> crosspoint(const Circle &c, const Line l) {\n\tPoint pr = projection(l, c.p);\n\tif(eq(distance(l, c.p), c.r)) return {pr, pr};\n\tVec v = (l.b - l.a) / abs(l.b - l.a) * sqrt(c.r * c.r - norm(pr - c.p));\n\treturn make_pair(pr - v, pr + v);\n\t// Vec e = (l.b - l.a) / abs(l.b - l.a);\n\t// double base = sqrt(c.r * c.r - norm(pr - c.p));\n\t// return {pr - e * base, pr + e * base};\n}\n\nstd::pair<Point, Point> crosspoint(const Circle &c, const Segment &l) {\n\tif(intersect(c, l) == 2) return crosspoint(c, Line(l.a, l.b));\n\tauto ret = crosspoint(c, Line(l.a, l.b));\n\tif(dot(l.a - ret.first, l.b - ret.first) < EPS)\n\t\tret.second = ret.first;\n\telse\n\t\tret.first = ret.second;\n\treturn ret;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_E\nstd::pair<Point, Point> crosspoint(const Circle &c1, const Circle &c2) {\n\tReal d = abs(c1.p - c2.p);\n\tReal a = acos((c1.r * c1.r + d * d - c2.r * c2.r) / (2 * c1.r * d)); // cosine theorem\n\tReal t = atan2(c2.p.imag() - c1.p.imag(), c2.p.real() - c1.p.real());\n\tPoint p1 = c1.p + Point(cos(t + a) * c1.r, sin(t + a) * c1.r);\n\tPoint p2 = c1.p + Point(cos(t - a) * c1.r, sin(t - a) * c1.r);\n\treturn make_pair(p1, p2);\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_F\n// \"点 p を通る円 c の接線\"の接点2つ\nstd::pair<Point, Point> tangent(const Circle &c1, const Point &p2) {\n\treturn crosspoint(c1, Circle(p2, sqrt(norm(c1.p - p2) - c1.r * c1.r)));\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_G\n// 円 c1, c2 の共通接線\nLines tangent(Circle c1, Circle c2) {\n\tLines ret;\n\tif(c1.r < c2.r) std::swap(c1, c2);\n\tReal g = norm(c1.p - c2.p);\n\tif(eq(g, 0)) return ret;\n\tVec u = (c2.p - c1.p) / Real(sqrt(g));\n\tVec v = rotate(PI * 0.5, u);\n\tfor(int s : {-1, 1}) {\n\t\tReal h = (c1.r + s * c2.r) / sqrt(g);\n\t\tif(eq(1 - h * h, 0)) {\n\t\t\tret.emplace_back(c1.p + u * c1.r, c1.p + (u + v) * c1.r);\n\t\t} else if(1 - h * h > 0) {\n\t\t\tPoint uu = u * h, vv = v * sqrt(1 - h * h);\n\t\t\tret.emplace_back(c1.p + (uu + vv) * c1.r, c2.p - (uu + vv) * c2.r * s);\n\t\t\tret.emplace_back(c1.p + (uu - vv) * c1.r, c2.p - (uu - vv) * c2.r * s);\n\t\t}\n\t}\n\treturn ret;\n}\n\n#pragma endregion\n\nbool solve(int n) {\n\tR velocity;\n\tcin >> velocity;\n\tR x_pig, y_pig;\n\tcin >> x_pig >> y_pig;\n\n\tVV<Segment> obstacle(n); // 0 ~ 2 yoko, 3~5 tate\n\n\tPoints pts;\n\n\trep(j, n) {\n\t\tR l, b, r, t;\n\t\tcin >> l >> b >> r >> t;\n\t\tpts.emplace_back(l, t);\n\t\tpts.emplace_back(l, b);\n\t\tpts.emplace_back(r, t);\n\t\tpts.emplace_back(r, b);\n\n\t\tR mid_x = (l + r) / 2;\n\t\tR mid_y = (b + t) / 2;\n\t\tPoints pts;\n\t\tfor(R x : {l, mid_x, r}) {\n\t\t\tfor(R y : {b, mid_y, t}) {\n\t\t\t\tpts.emplace_back(x, y);\n\t\t\t}\n\t\t}\n\t\trep(i, 3) obstacle[j].emplace_back(pts[i], pts[i + 6]);\n\t\trep(i, 3) obstacle[j].emplace_back(pts[i * 3], pts[i * 3 + 2]);\n\t}\n\n\tconst R g = 9.8;\n\n\tauto f = [&](R theta, R x) { return x * tan(theta) - g / 2 * pow(x / (velocity * cos(theta)), (R)2.); };\n\n\tauto calc_top = [&](R obj_x) -> R {\n\t\tR inf = 0;\n\t\tR sup = pi / 2 - EPS;\n\t\tR lef, rig;\n\t\trep(300) {\n\t\t\tlef = inf + (sup - inf) / 3;\n\t\t\trig = (lef + sup) / 2;\n\t\t\tif(f(lef, obj_x) < f(rig, obj_x)) {\n\t\t\t\tinf = lef;\n\t\t\t} else {\n\t\t\t\tsup = rig;\n\t\t\t}\n\t\t}\n\t\treturn lef;\n\t};\n\n\tauto binarysearch = [&](R lef, R rig, R obj_x, R obj_y, bool lef_is_large) {\n\t\tR mid;\n\t\trep(300) {\n\t\t\tmid = lef + rig;\n\t\t\tmid /= 2;\n\t\t\tif(bool(f(mid, obj_x) > obj_y) ^ (bool)lef_is_large) {\n\t\t\t\trig = mid;\n\t\t\t} else {\n\t\t\t\tlef = mid;\n\t\t\t}\n\t\t}\n\t\treturn mid;\n\t};\n\n\t// y = x tan theta - (g/2) (x/(v_0 cos theta))^2\n\tauto calc_theta = [&](R obj_x, R obj_y) -> pair<R, R> {\n\t\tR inf = 0;\n\t\tR sup = pi / 2 - EPS;\n\t\tR mid = calc_top(obj_x);\n\t\tif(compare(f(mid, obj_x), obj_y) == -1) return make_pair(-1., -1.);\n\t\t// R a, b;\n\t\t// inf = 0;\n\t\t// rep(300) {\n\t\t// \ta = (inf + mid) / 2;\n\t\t// \tif(f(a, obj_x) < obj_y) {\n\t\t// \t\tinf = a;\n\t\t// \t} else {\n\t\t// \t\tmid = a;\n\t\t// \t}\n\t\t// }\n\t\t// mid = sup;\n\t\t// sup = pi / 2 - EPS;\n\t\t// rep(300) {\n\t\t// \tb = (mid + sup) / 2;\n\t\t// \tif(f(b, obj_x) < obj_y) {\n\t\t// \t\tsup = b;\n\t\t// \t} else {\n\t\t// \t\tmid = b;\n\t\t// \t}\n\t\t// }\n\t\treturn make_pair(binarysearch(inf, mid, obj_x, obj_y, false), binarysearch(mid, sup, obj_x, obj_y, true));\n\t};\n\n\tauto tp = [&](R theta) { return pow(velocity, 2.) * cos(theta) * sin(theta) / g; };\n\n\tauto collision = [&](R theta, int obs_idx) -> bool {\n\t\tR top = tp(theta);\n\t\trep(i, 3) {\n\t\t\tauto &seg = obstacle[obs_idx][i];\n\t\t\tR xl, xr, y;\n\t\t\ty = seg.a.imag();\n\t\t\txl = seg.a.real();\n\t\t\txr = seg.b.real();\n\t\t\tif(eq(theta, 0.899297831746404)) {\n\t\t\t\tdebug(y, xl, xr);\n\t\t\t}\n\t\t\tif(xl > xr) swap(xl, xr);\n\t\t\tif(x_pig <= xl) continue;\n\t\t\tchmin(xr, x_pig);\n\t\t\tR yl = f(theta, xl);\n\t\t\tR yr = f(theta, xr);\n\t\t\tif(xl <= top && top <= xr) {\n\t\t\t\tif(compare(f(theta, top), y) == 1) {\n\t\t\t\t\tif(compare(yl, y) == -1 || compare(yr, y) == -1) return true;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(compare(yl, y) * compare(yr, y) == -1) return true;\n\t\t}\n\n\t\trep(i, 3, 6) {\n\t\t\tauto &seg = obstacle[obs_idx][i];\n\t\t\tR yd, x, yt;\n\t\t\tx = seg.a.real();\n\t\t\tyd = seg.a.imag();\n\t\t\tyt = seg.b.imag();\n\t\t\tif(eq(theta, 0.899297831746404)) {\n\t\t\t\tdebug(x, yd, yt);\n\t\t\t}\n\t\t\tif(x_pig < x) continue;\n\t\t\tif(yd > yt) swap(yd, yt);\n\t\t\tR y = f(theta, x);\n\t\t\tif(compare(yd, y) == -1 && compare(y, yt) == -1) return true;\n\t\t}\n\n\t\treturn false;\n\t};\n\n\tauto use = [&](R angle) { return EPS < angle && angle < pi / 2 - EPS * 2; };\n\n\tauto ok = [&](R angle) {\n\t\tif(!use(angle)) return false;\n\t\trep(i, n) if(collision(angle, i)) return false;\n\t\tR y = f(angle, x_pig);\n\t\tif(compare(y, y_pig) == -1) return false;\n\t\tSegment egg(Point(x_pig, y), Point(x_pig, y_pig));\n\t\tfoa(ob, obstacle) {\n\t\t\tfoa(seg, ob) {\n\t\t\t\tif(intersect(seg, egg)) {\n\t\t\t\t\treturn false;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn true;\n\t};\n\n\tfoa(p, pts) {\n\t\tif(eq(p.real(), 0.)) continue;\n\t\tauto angles = calc_theta(p.real(), p.imag());\n\t\tif(ok(angles.first) || ok(angles.second)) return true;\n\t}\n\n\tauto angles = calc_theta(x_pig, y_pig);\n\treturn (ok(angles.first) || ok(angles.second));\n}\n\nint main() {\n\tstd::ios::sync_with_stdio(false);\n\tstd::cin.tie(nullptr);\n\tstd::cout << std::fixed << std::setprecision(15);\n\t// std::cerr << std::fixed << std::setprecision(15);\n\tsrand((unsigned)time(NULL));\n\n\tint n;\n\twhile(cin >> n) {\n\t\tYes(solve(n));\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 3952, "score_of_the_acc": -1.0836, "final_rank": 4 }, { "submission_id": "aoj_2308_5846664", "code_snippet": "/*\tauthor: Kite_kuma\n\tcreated: 2021.09.01 15:05:47 */\n\n// #ifdef LOCAL\n// #define _GLIBCXX_DEBUG\n// #endif\n#include <bits/stdc++.h>\nusing namespace std;\n\n#pragma region macros\n\n#define foa(s, v) for(auto &s : v)\n#define all(v) (v).begin(), (v).end()\n#define rall(v) (v).rbegin(), (v).rend()\n#define popcnt(n) __builtin_popcountll((long long)n)\n\n#define REPname(a, b, c, d, e, ...) e\n#define rep(...) REPname(__VA_ARGS__, REP3, REP2, REP1, REP0)(__VA_ARGS__)\n#define REP0(x) for(int Counter_in_rep_macro = 0; Counter_in_rep_macro < (x); ++Counter_in_rep_macro)\n#define REP1(i, x) for(int i = 0; i < (x); ++i)\n#define REP2(i, l, r) for(int i = (l); i < (r); ++i)\n#define REP3(i, l, r, c) for(int i = (l); i < (r); i += (c))\n\n#define DREPname(a, b, c, d, e, ...) e\n#define drep(...) DREPname(__VA_ARGS__, DREP3, DREP2, DREP1)(__VA_ARGS__)\n#define DREP1(i, x) for(int i = (x)-1; i >= 0; --i)\n#define DREP2(i, l, r) for(int i = (r)-1; i >= (l); --i)\n#define DREP3(i, l, r, c) for(int i = (r)-1; i >= (l); i -= (c))\n\n#pragma endregion\n\n#pragma region aliases\n\nusing ll = long long;\nusing ld = long double;\nusing ull = unsigned long long;\nusing vi = std::vector<int>;\nusing vvi = std::vector<std::vector<int>>;\nusing vvvi = std::vector<std::vector<std::vector<int>>>;\nusing vll = std::vector<ll>;\nusing vvll = std::vector<vll>;\nusing vvvll = std::vector<vvll>;\nusing pii = std::pair<int, int>;\nusing pll = std::pair<long long, long long>;\ntemplate <class T = ll>\nusing V = std::vector<T>;\ntemplate <class T = ll>\nusing VV = V<V<T>>;\ntemplate <class T = ll>\nusing VVV = V<V<V<T>>>;\ntemplate <class T = ll>\nusing pqup = std::priority_queue<T, std::vector<T>, std::greater<T>>;\ntemplate <class T = ll>\nusing pqdn = std::priority_queue<T>;\n\n#pragma endregion\n\n#pragma region constants\n\nconst int inf = 1e9;\nconst long long INF = 1e18;\nconst long double pi = acos(-1);\nconst char dl = '\\n';\nconst char sp = ' ';\nint dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};\nint dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};\n\nconst int mod_1000000007 = 1000000007;\nconst int mod_998244353 = 998244353;\n\n#pragma endregion\n\n#pragma region basic_operation\n\ntemplate <class T0, class T1, class T2>\ninline bool in_range(T0 x, T1 lef, T2 rig) {\n\treturn ((lef <= x) && (x < rig));\n}\n\ntemplate <class T>\ninline bool chmin(T &a, T b) {\n\tif(a > b) {\n\t\ta = b;\n\t\treturn true;\n\t}\n\treturn false;\n}\ntemplate <class T>\ninline bool chmax(T &a, T b) {\n\tif(a < b) {\n\t\ta = b;\n\t\treturn true;\n\t}\n\treturn false;\n}\n\nvoid Yes(bool f = 1) { std::cout << (f ? \"Yes\" : \"No\") << '\\n'; }\nvoid No() { std::cout << \"No\\n\"; }\nvoid YES(bool f = 1) { std::cout << (f ? \"YES\" : \"NO\") << '\\n'; }\nvoid NO() { std::cout << \"NO\\n\"; }\n\ntemplate <class T>\nvoid drop(T answer) {\n\tstd::cout << answer << '\\n';\n\texit(0);\n}\n\nvoid err(bool flag = true) {\n\tif(!flag) return;\n\tstd::cout << -1 << '\\n';\n\texit(0);\n}\n\ntemplate <class T>\nvoid vout(std::vector<T> const &v, bool vertically = 0) {\n\tif(vertically) {\n\t\tfor(auto const &a : v) {\n\t\t\tstd::cout << a << '\\n';\n\t\t}\n\t\treturn;\n\t}\n\tfor(auto it = v.begin(); it != v.end(); it++) {\n\t\tstd::cout << (*it);\n\t\tif(it != v.end() - 1) {\n\t\t\tstd::cout << ' ';\n\t\t}\n\t}\n\tstd::cout << '\\n';\n\treturn;\n}\n\ninline void print() { std::cout << '\\n'; }\ntemplate <class T>\ninline void print(T x) {\n\tstd::cout << x << '\\n';\n\treturn;\n}\ntemplate <typename Head, typename... Tail>\nvoid print(Head H, Tail... T) {\n\tstd::cout << H << \" \";\n\tprint(T...);\n}\n\ntemplate <class T>\nvoid add(std::vector<T> &v, T val) {\n\tfor(auto &a : v) a += val;\n\treturn;\n}\n\ntemplate <class T>\nT dup(T a, T b) {\n\tassert(b != 0);\n\treturn (a + b - 1) / b;\n}\n\ntemplate <class T>\nT greatest_lower_multiple(T x, T d) {\n\tif(d == 0) return 0;\n\tif(d < 0) d *= -1;\n\tT y = x % d;\n\tif(y < 0) y += d;\n\treturn x - y;\n}\n\ntemplate <class T>\nT least_upper_multiple(T x, T d) {\n\treturn -greatest_lower_multiple(-x, d);\n}\n\nlong long POW(long long a, long long n) {\n\tlong long res = 1;\n\twhile(n > 0) {\n\t\tif(n & 1) res = res * a;\n\t\ta = a * a;\n\t\tn >>= 1;\n\t}\n\treturn res;\n}\n\nlong long modpow(long long a, long long n, long long mod) {\t // a^n mod\n\tassert(n >= 0 && mod);\n\tif(mod == 1) return 0LL;\n\tlong long res = 1;\n\ta %= mod;\n\twhile(n > 0) {\n\t\tif(n & 1) res = res * a % mod;\n\t\ta = a * a % mod;\n\t\tn >>= 1;\n\t}\n\treturn res < 0 ? res + mod : res;\n}\n\n// return x which satisfies a * x % mod == gcd(a, mod)\n// not (mod | a)\nlong long modinv(long long a, long long mod) {\n\tlong long b = mod, u = 1, v = 0;\n\twhile(b) {\n\t\tlong long t = a / b;\n\t\ta -= t * b;\n\t\tstd::swap(a, b);\n\t\tu -= t * v;\n\t\tstd::swap(u, v);\n\t}\n\tu %= mod;\n\tif(u < 0) u += mod;\n\treturn u;\n}\n\ntemplate <class T>\nint lb(const std::vector<T> &a, const T x) {\n\treturn std::distance(a.begin(), std::lower_bound(a.begin(), a.end(), x));\n}\ntemplate <class T>\nint ub(const std::vector<T> &a, const T x) {\n\treturn std::distance(a.begin(), std::upper_bound(a.begin(), a.end(), x));\n}\ntemplate <class T>\nvoid unq_sort(std::vector<T> &a) {\n\tstd::sort(a.begin(), a.end());\n\ta.erase(std::unique(a.begin(), a.end()), a.end());\n}\ntemplate <class T>\nstd::vector<int> press(std::vector<T> &a) {\n\tauto vec = a;\n\tunq_sort(vec);\n\tstd::vector<int> ret;\n\tfor(auto &v : a) ret.push_back(lb(vec, v));\n\treturn ret;\n}\n\n#pragma endregion\n\n#pragma region input\n#define VEC(type, name, size) \\\n\tvector<type> name(size); \\\n\tscanner::INPUT(name)\n#define VVEC(type, name, h, w) \\\n\tvector<vector<type>> name(h, vector<type>(w)); \\\n\tscanner::INPUT(name)\n#define INT(...) \\\n\tint __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define LL(...) \\\n\tlong long __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define STR(...) \\\n\tstring __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define CHR(...) \\\n\tchar __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define DBL(...) \\\n\tdouble __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define LD(...) \\\n\tlong double __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define TPL3(type0, type1, type2, name) \\\n\tstd::tuple<type0, type1, type2> name; \\\n\tscanner::INPUT(name);\n#define TPL4(type0, type1, type2, type3, name) \\\n\tstd::tuple<type0, type1, type2, type3> name; \\\n\tscanner::INPUT(name);\n\nnamespace scanner {\ntemplate <class T>\nvoid scan(T &a) {\n\tstd::cin >> a;\n}\n\ntemplate <class T, class L>\nvoid scan(std::pair<T, L> &p) {\n\tscan(p.first);\n\tscan(p.second);\n}\n\ntemplate <class T0, class T1, class T2>\nvoid scan(std::tuple<T0, T1, T2> &p) {\n\tT0 t0;\n\tT1 t1;\n\tT2 t2;\n\tscan(t0);\n\tscan(t1);\n\tscan(t2);\n\tp = std::make_tuple(t0, t1, t2);\n}\n\ntemplate <class T0, class T1, class T2, class T3>\nvoid scan(std::tuple<T0, T1, T2, T3> &p) {\n\tT0 t0;\n\tT1 t1;\n\tT2 t2;\n\tT3 t3;\n\tscan(t0);\n\tscan(t1);\n\tscan(t2);\n\tscan(t3);\n\tp = std::make_tuple(t0, t1, t2, t3);\n}\n\ntemplate <class T>\nvoid scan(std::vector<T> &a) {\n\tfor(auto &i : a) scan(i);\n}\n\nvoid INPUT() {}\ntemplate <class Head, class... Tail>\nvoid INPUT(Head &head, Tail &... tail) {\n\tscan(head);\n\tINPUT(tail...);\n}\n} // namespace scanner\n\ntemplate <typename T1, typename T2>\nstd::istream &operator>>(std::istream &is, std::pair<T1, T2> &p) {\n\tis >> p.first >> p.second;\n\treturn is;\n}\n#pragma endregion\n\n#pragma region debug\n\n#pragma region output\ntemplate <typename T1, typename T2>\nstd::ostream &std::operator<<(std::ostream &os, const std::pair<T1, T2> &p) {\n\tos << p.first << \" \" << p.second;\n\treturn os;\n}\ntemplate <class T>\nstd::ostream &std::operator<<(std::ostream &os, const std::vector<T> &v) {\n\tfor(int i = 0; i < (int)v.size(); i++) {\n\t\tif(i) os << \" \";\n\t\tos << v[i];\n\t}\n\treturn os;\n}\n#pragma endregion\n\n#pragma region view\n\nnamespace viewer {\nvoid view(const long long e) {\n\tif(e == INF)\n\t\tstd::cerr << \"INF\";\n\telse if(e == -INF)\n\t\tstd::cerr << \"-INF\";\n\telse\n\t\tstd::cerr << e;\n}\n\nvoid view(const int e) {\n\tif(e == inf)\n\t\tstd::cerr << \"inf\";\n\telse if(e == -inf)\n\t\tstd::cerr << \"-inf\";\n\telse\n\t\tstd::cerr << e;\n}\n\ntemplate <typename T>\nvoid view(const T e) {\n\tstd::cerr << e;\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::pair<T, U> &p) {\n\tstd::cerr << \"(\";\n\tview(p.first);\n\tstd::cerr << \", \";\n\tview(p.second);\n\tstd::cerr << \")\";\n}\n\ntemplate <class T0, class T1, class T2>\nvoid view(const std::tuple<T0, T1, T2> &p) {\n\tstd::cerr << \"(\";\n\tview(std::get<0>(p));\n\tstd::cerr << \", \";\n\tview(std::get<1>(p));\n\tstd::cerr << \", \";\n\tview(std::get<2>(p));\n\tstd::cerr << \")\";\n}\n\ntemplate <class T0, class T1, class T2, class T3>\nvoid view(const std::tuple<T0, T1, T2, T3> &p) {\n\tstd::cerr << \"(\";\n\tview(std::get<0>(p));\n\tstd::cerr << \", \";\n\tview(std::get<1>(p));\n\tstd::cerr << \", \";\n\tview(std::get<2>(p));\n\tstd::cerr << \", \";\n\tview(std::get<3>(p));\n\tstd::cerr << \")\";\n}\n\ntemplate <typename T>\nvoid view(const std::set<T> &s) {\n\tif(s.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << \"{ \";\n\tfor(auto &t : s) {\n\t\tview(t);\n\t\tstd::cerr << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\n\ntemplate <typename T>\nvoid view(const std::unordered_set<T> &s) {\n\tif(s.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << \"{ \";\n\tfor(auto &t : s) {\n\t\tview(t);\n\t\tstd::cerr << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\n\ntemplate <typename T>\nvoid view(const std::vector<T> &v) {\n\tif(v.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << \"{ \";\n\tfor(const auto &e : v) {\n\t\tview(e);\n\t\tstd::cerr << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\n\ntemplate <typename T>\nvoid view(const std::vector<std::vector<T>> &vv) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &v : vv) {\n\t\tstd::cerr << \"\\t\";\n\t\tview(v);\n\t\tstd::cerr << '\\n';\n\t}\n\tstd::cerr << \"}\";\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::vector<std::pair<T, U>> &v) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &c : v) {\n\t\tstd::cerr << \"\\t(\";\n\t\tview(c.first);\n\t\tstd::cerr << \", \";\n\t\tview(c.second);\n\t\tstd::cerr << \")\\n\";\n\t}\n\tstd::cerr << \"}\";\n}\n\ntemplate <class T0, class T1, class T2>\nvoid view(const std::vector<std::tuple<T0, T1, T2>> &v) {\n\tif(v.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << '{';\n\tfor(const auto &t : v) {\n\t\tstd::cerr << \"\\n\\t\";\n\t\tview(t);\n\t\tstd::cerr << \",\";\n\t}\n\tstd::cerr << \"\\n}\";\n}\n\ntemplate <class T0, class T1, class T2, class T3>\nvoid view(const std::vector<std::tuple<T0, T1, T2, T3>> &v) {\n\tif(v.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << '{';\n\tfor(const auto &t : v) {\n\t\tstd::cerr << \"\\n\\t\";\n\t\tview(t);\n\t\tstd::cerr << \",\";\n\t}\n\tstd::cerr << \"\\n}\";\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::map<T, U> &m) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &t : m) {\n\t\tstd::cerr << \"\\t[\";\n\t\tview(t.first);\n\t\tstd::cerr << \"] : \";\n\t\tview(t.second);\n\t\tstd::cerr << '\\n';\n\t}\n\tstd::cerr << \"}\";\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::unordered_map<T, U> &m) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &t : m) {\n\t\tstd::cerr << \"\\t[\";\n\t\tview(t.first);\n\t\tstd::cerr << \"] : \";\n\t\tview(t.second);\n\t\tstd::cerr << '\\n';\n\t}\n\tstd::cerr << \"}\";\n}\n} // namespace viewer\n\n#pragma endregion\n\n// when debugging : g++ foo.cpp -DLOCAL\n#ifdef LOCAL\nvoid debug_out() {}\ntemplate <typename Head, typename... Tail>\nvoid debug_out(Head H, Tail... T) {\n\tviewer::view(H);\n\tstd::cerr << \", \";\n\tdebug_out(T...);\n}\n#define debug(...) \\\n\tdo { \\\n\t\tstd::cerr << __LINE__ << \" [\" << #__VA_ARGS__ << \"] : [\"; \\\n\t\tdebug_out(__VA_ARGS__); \\\n\t\tstd::cerr << \"\\b\\b]\\n\"; \\\n\t} while(0)\n#define dump(x) \\\n\tdo { \\\n\t\tstd::cerr << __LINE__ << \" \" << #x << \" : \"; \\\n\t\tviewer::view(x); \\\n\t\tstd::cerr << '\\n'; \\\n\t} while(0)\n\n#else\n#define debug(...) (void(0))\n#define dump(x) (void(0))\n#endif\n\n#pragma endregion\n\nusing R = long double;\n\n#pragma region geometry_light\n\nusing Real = long double;\nusing R = Real;\nusing Point = std::complex<Real>;\nusing Vec = Point;\nconst Real EPS = 1e-10, PI = acos(-1);\n\ninline bool eq(const Real &a, const Real &b) { return fabs(b - a) < EPS; }\ninline bool eq(const Point &a, const Point &b) { return fabs(b - a) < EPS; }\n/*\n\t-1: a < b\n\t0 : a == b\n\t1 : a > b\n*/\ninline int compare(const Real &a, const Real &b) { return eq(a, b) ? 0 : a < b ? -1 : 1; }\ninline int sign(const Real &a) { return fabs(a) < EPS ? 0 : a < 0 ? -1 : 1; }\n\nnamespace std {\nconst Point operator*(const Point &p, const Real &d) { return Point(real(p) * d, imag(p) * d); }\n} // namespace std\n\nstd::istream &operator>>(std::istream &is, Point &p) {\n\tReal a, b;\n\tis >> a >> b;\n\tp = Point(a, b);\n\treturn is;\n}\nstd::ostream &operator<<(std::ostream &os, Point &p) { return os << std::fixed << std::setprecision(10) << p.real() << \" \" << p.imag(); }\n\n// rotate point 'p' for counter clockwise direction\nconst Point rotate(const Real &theta, const Point &p) {\n\treturn Point(cos(theta) * p.real() - sin(theta) * p.imag(), sin(theta) * p.real() + cos(theta) * p.imag());\n}\n\nconst Real radian_to_degree(const Real &r) { return (r * 180.0 / PI); }\n\nconst Real degree_to_radian(const Real &d) { return (d * PI / 180.0); }\n\n// get angle a-b-c (<pi)\nconst Real get_angle(const Point &a, const Point &b, const Point &c) {\n\tconst Point v(a - b), w(c - b);\n\tReal theta = fabs(atan2(w.imag(), w.real()) - atan2(v.imag(), v.real()));\n\treturn std::min(theta, 2 * PI - theta);\n}\n\nnamespace std {\nbool operator<(const Point &a, const Point &b) { return a.real() != b.real() ? a.real() < b.real() : a.imag() < b.imag(); }\n} // namespace std\n\nstruct Line {\n\tPoint a, b;\n\n\tLine() = default;\n\n\tLine(Point a, Point b) : a(a), b(b) {}\n\n\tLine(Real A, Real B, Real C) // Ax + By = C\n\t{\n\t\tif(eq(A, 0))\n\t\t\ta = Point(0, C / B), b = Point(1, C / B);\n\t\telse if(eq(B, 0))\n\t\t\tb = Point(C / A, 0), b = Point(C / A, 1);\n\t\telse\n\t\t\ta = Point(0, C / B), b = Point(C / A, 0);\n\t}\n\n\tfriend std::ostream &operator<<(std::ostream &os, Line &p) { return os << p.a << \" -- \" << p.b; }\n\n\tfriend std::istream &operator>>(std::istream &is, Line &a) { return is >> a.a >> a.b; }\n};\n\nstruct Segment : Line {\n\tSegment() = default;\n\n\tSegment(Point a, Point b) : Line(a, b) {}\n};\n\nstruct Circle {\n\tPoint p;\n\tReal r;\n\n\tCircle() = default;\n\n\tCircle(Point p, Real r) : p(p), r(r) {}\n};\n\nusing Points = std::vector<Point>;\nusing Polygon = std::vector<Point>;\nusing Segments = std::vector<Segment>;\nusing Lines = std::vector<Line>;\nusing Circles = std::vector<Circle>;\n\nconst Real cross(const Point &a, const Point &b) { return real(a) * imag(b) - imag(a) * real(b); }\nconst Real dot(const Point &a, const Point &b) { return real(a) * real(b) + imag(a) * imag(b); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_C\nconst int ccw(const Point &a, Point b, Point c) {\n\tb = b - a, c = c - a;\n\tif(cross(b, c) > EPS) return +1;\t\t// \"COUNTER_CLOCKWISE\"\n\tif(cross(b, c) < -EPS) return -1;\t\t// \"CLOCKWISE\"\n\tif(dot(b, c) < -EPS) return +2;\t\t\t// \"ONLINE_BACK\"\n\tif(norm(b) + EPS < norm(c)) return -2;\t// \"ONLINE_FRONT\"\n\treturn 0;\t\t\t\t\t\t\t\t// \"ON_SEGMENT\"\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_A\nbool parallel(const Line &a, const Line &b) { return eq(cross(a.b - a.a, b.b - b.a), 0.0); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_A\nbool orthogonal(const Line &a, const Line &b) { return eq(dot(a.a - a.b, b.a - b.b), 0.0); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_A\nPoint projection(const Line &l, const Point &p) {\n\tdouble t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n\treturn l.a + (l.a - l.b) * t;\n}\n\nPoint projection(const Segment &l, const Point &p) {\n\tdouble t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n\treturn l.a + (l.a - l.b) * t;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_B\nPoint reflection(const Line &l, const Point &p) { return projection(l, p) * 2.0 - p; }\n\nint intersect(const Line &l, const Point &p) { return int(abs(ccw(l.a, l.b, p)) != 1); }\n\nint intersect(const Line &l, const Line &m) {\n\tif(intersect(l, m.a) && intersect(l, m.b)) return -1;\n\treturn int(abs(cross(l.b - l.a, m.b - m.a)) > EPS);\n}\n\nint intersect(const Segment &s, const Point &p) { return int(ccw(s.a, s.b, p) == 0); }\n\nint intersect(const Line &l, const Segment &s) {\n\tif(intersect(l, s.a) && intersect(l, s.b)) return -1;\n\treturn cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < EPS;\n}\n\nReal distance(const Line &l, const Point &p);\n\nint intersect(const Circle &c, const Line &l) {\n\tReal d = c.r - distance(l, c.p);\n\treturn fabs(d) < EPS ? 1 : d > 0. ? 2 : 0;\n}\n\nint intersect(const Circle &c, const Point &p) { return int(abs(abs(p - c.p) - c.r) < EPS); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_B\nint intersect(const Segment &s, const Segment &t) {\n\tif(eq(s.a, s.b)) return intersect(t, s.a);\n\tif(intersect(Line(s), t.a) && intersect(Line(s), t.b) &&\n\t std::max(std::min(s.a, s.b), std::min(t.a, t.b)) < std::min(std::max(s.a, s.b), std::max(t.a, t.b)))\n\t\treturn -1;\n\treturn int(ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 && ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0);\n}\n\nint intersect(const Circle &c, const Segment &l) {\n\tconst Point h = projection(l, c.p);\n\tif(norm(h - c.p) - c.r * c.r > EPS) return 0;\n\tauto d1 = abs(c.p - l.a), d2 = abs(c.p - l.b);\n\tif(compare(d1, c.r) == -1 && compare(d2, c.r) == -1) return 0;\n\tif(d1 < c.r - EPS && d2 > c.r - EPS || d1 > c.r - EPS && d2 < c.r - EPS) return 1;\n\treturn dot(l.a - h, l.b - h) < 0 ? 2 : 0;\n}\n\nReal distance(const Point &a, const Point &b);\n\nint number_of_common_tangents(const Circle &c1, const Circle &c2) {\n\tReal r1 = std::min(c1.r, c2.r), r2 = std::max(c1.r, c2.r), d = distance(c1.p, c2.p);\n\tint com = compare(r1 + r2, d);\n\treturn com == 1 ? compare(d + r1, r2) + 1 : 3 - com;\n\t// if(c1.r < c2.r) swap(c1, c2);\n\t// Real d = abs(c1.p - c2.p);\n\t// if(compare(c1.r + c2.r, d) == -1) return 4;\n\t// if(eq(c1.r + c2.r, d)) return 3;\n\t// if(compare(c1.r - c2.r, d) == -1) return 2;\n\t// return int(eq(c1.r - c2.r, d));\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_A&lang=jp\nint intersect(const Circle &c1, const Circle &c2) { return 2 - abs(2 - number_of_common_tangents(c1, c2)); }\n\nReal distance(const Point &a, const Point &b) { return abs(a - b); }\n\nReal distance(const Line &l, const Point &p) { return abs(p - projection(l, p)); }\n\nReal distance(const Line &l, const Line &m) { return intersect(l, m) ? 0 : distance(l, m.a); }\n\nReal distance(const Segment &s, const Point &p) {\n\tPoint r = projection(s, p);\n\treturn intersect(s, r) ? distance(r, p) : std::min(abs(s.a - p), abs(s.b - p));\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_D\nReal distance(const Segment &a, const Segment &b) {\n\tif(intersect(a, b)) return 0;\n\treturn std::min({distance(a, b.a), distance(a, b.b), distance(b, a.a), distance(b, a.b)});\n}\n\nReal distance(const Line &l, const Segment &s) {\n\tif(intersect(l, s)) return 0;\n\treturn std::min(distance(l, s.a), distance(l, s.b));\n}\n\nPoint crosspoint(const Line &l, const Line &m) {\n\tReal A = cross(l.b - l.a, m.b - m.a);\n\tReal B = cross(l.b - l.a, l.b - m.a);\n\tif(eq(abs(A), 0.0) && eq(abs(B), 0.0)) return m.a;\n\treturn m.a + (m.b - m.a) * B / A;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_C\nPoint crosspoint(const Segment &l, const Segment &m) { return crosspoint(Line(l), Line(m)); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_D\nstd::pair<Point, Point> crosspoint(const Circle &c, const Line l) {\n\tPoint pr = projection(l, c.p);\n\tif(eq(distance(l, c.p), c.r)) return {pr, pr};\n\tVec v = (l.b - l.a) / abs(l.b - l.a) * sqrt(c.r * c.r - norm(pr - c.p));\n\treturn make_pair(pr - v, pr + v);\n\t// Vec e = (l.b - l.a) / abs(l.b - l.a);\n\t// double base = sqrt(c.r * c.r - norm(pr - c.p));\n\t// return {pr - e * base, pr + e * base};\n}\n\nstd::pair<Point, Point> crosspoint(const Circle &c, const Segment &l) {\n\tif(intersect(c, l) == 2) return crosspoint(c, Line(l.a, l.b));\n\tauto ret = crosspoint(c, Line(l.a, l.b));\n\tif(dot(l.a - ret.first, l.b - ret.first) < EPS)\n\t\tret.second = ret.first;\n\telse\n\t\tret.first = ret.second;\n\treturn ret;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_E\nstd::pair<Point, Point> crosspoint(const Circle &c1, const Circle &c2) {\n\tReal d = abs(c1.p - c2.p);\n\tReal a = acos((c1.r * c1.r + d * d - c2.r * c2.r) / (2 * c1.r * d)); // cosine theorem\n\tReal t = atan2(c2.p.imag() - c1.p.imag(), c2.p.real() - c1.p.real());\n\tPoint p1 = c1.p + Point(cos(t + a) * c1.r, sin(t + a) * c1.r);\n\tPoint p2 = c1.p + Point(cos(t - a) * c1.r, sin(t - a) * c1.r);\n\treturn make_pair(p1, p2);\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_F\n// \"点 p を通る円 c の接線\"の接点2つ\nstd::pair<Point, Point> tangent(const Circle &c1, const Point &p2) {\n\treturn crosspoint(c1, Circle(p2, sqrt(norm(c1.p - p2) - c1.r * c1.r)));\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_G\n// 円 c1, c2 の共通接線\nLines tangent(Circle c1, Circle c2) {\n\tLines ret;\n\tif(c1.r < c2.r) std::swap(c1, c2);\n\tReal g = norm(c1.p - c2.p);\n\tif(eq(g, 0)) return ret;\n\tVec u = (c2.p - c1.p) / Real(sqrt(g));\n\tVec v = rotate(PI * 0.5, u);\n\tfor(int s : {-1, 1}) {\n\t\tReal h = (c1.r + s * c2.r) / sqrt(g);\n\t\tif(eq(1 - h * h, 0)) {\n\t\t\tret.emplace_back(c1.p + u * c1.r, c1.p + (u + v) * c1.r);\n\t\t} else if(1 - h * h > 0) {\n\t\t\tPoint uu = u * h, vv = v * sqrt(1 - h * h);\n\t\t\tret.emplace_back(c1.p + (uu + vv) * c1.r, c2.p - (uu + vv) * c2.r * s);\n\t\t\tret.emplace_back(c1.p + (uu - vv) * c1.r, c2.p - (uu - vv) * c2.r * s);\n\t\t}\n\t}\n\treturn ret;\n}\n\n#pragma endregion\n\nbool solve(int n) {\n\tR velocity;\n\tcin >> velocity;\n\tR x_pig, y_pig;\n\tcin >> x_pig >> y_pig;\n\n\tVV<Segment> obstacle(n); // 0 ~ 2 yoko, 3~5 tate\n\n\tPoints pts;\n\n\trep(j, n) {\n\t\tR l, b, r, t;\n\t\tcin >> l >> b >> r >> t;\n\t\tpts.emplace_back(l, t);\n\t\tpts.emplace_back(l, b);\n\t\tpts.emplace_back(r, t);\n\t\tpts.emplace_back(r, b);\n\n\t\tR mid_x = (l + r) / 2;\n\t\tR mid_y = (b + t) / 2;\n\t\tPoints pts;\n\t\tfor(R x : {l, mid_x, r}) {\n\t\t\tfor(R y : {b, mid_y, t}) {\n\t\t\t\tpts.emplace_back(x, y);\n\t\t\t}\n\t\t}\n\t\trep(i, 3) { obstacle[j].emplace_back(pts[i], pts[i + 6]); }\n\t\trep(i, 3) obstacle[j].emplace_back(pts[i * 3], pts[i * 3 + 2]);\n\t}\n\n\tconst R g = 9.8;\n\n\tauto f = [&](R theta, R x) { return x * tan(theta) - g / 2 * pow(x / (velocity * cos(theta)), (R)2.); };\n\n\tauto calc_top = [&](R obj_x) -> R {\n\t\tR inf = 0;\n\t\tR sup = pi / 2 - EPS;\n\t\tR lef, rig;\n\t\trep(300) {\n\t\t\tlef = inf + (sup - inf) / 3;\n\t\t\trig = (lef + sup) / 2;\n\t\t\tif(f(lef, obj_x) < f(rig, obj_x)) {\n\t\t\t\tinf = lef;\n\t\t\t} else {\n\t\t\t\tsup = rig;\n\t\t\t}\n\t\t}\n\t\treturn lef;\n\t};\n\n\tauto binarysearch = [&](R lef, R rig, R obj_x, R obj_y, bool lef_is_large) {\n\t\tR mid;\n\t\trep(300) {\n\t\t\tmid = lef + rig;\n\t\t\tmid /= 2;\n\t\t\tif(bool(f(mid, obj_x) > obj_y) ^ (bool)lef_is_large) {\n\t\t\t\trig = mid;\n\t\t\t} else {\n\t\t\t\tlef = mid;\n\t\t\t}\n\t\t}\n\t\treturn mid;\n\t};\n\n\t// y = x tan theta - (g/2) (x/(v_0 cos theta))^2\n\tauto calc_theta = [&](R obj_x, R obj_y) -> pair<R, R> {\n\t\tR inf = 0;\n\t\tR sup = pi / 2 - EPS;\n\t\tR mid = calc_top(obj_x);\n\t\tif(compare(f(mid, obj_x), obj_y) == -1) return make_pair(-1., -1.);\n\t\t// R a, b;\n\t\t// inf = 0;\n\t\t// rep(300) {\n\t\t// \ta = (inf + mid) / 2;\n\t\t// \tif(f(a, obj_x) < obj_y) {\n\t\t// \t\tinf = a;\n\t\t// \t} else {\n\t\t// \t\tmid = a;\n\t\t// \t}\n\t\t// }\n\t\t// mid = sup;\n\t\t// sup = pi / 2 - EPS;\n\t\t// rep(300) {\n\t\t// \tb = (mid + sup) / 2;\n\t\t// \tif(f(b, obj_x) < obj_y) {\n\t\t// \t\tsup = b;\n\t\t// \t} else {\n\t\t// \t\tmid = b;\n\t\t// \t}\n\t\t// }\n\t\treturn make_pair(binarysearch(inf, mid, obj_x, obj_y, false), binarysearch(mid, sup, obj_x, obj_y, true));\n\t};\n\n\tauto tp = [&](R theta) { return pow(velocity, 2.) * cos(theta) * sin(theta) / g; };\n\n\tauto collision = [&](R theta, int obs_idx) -> bool {\n\t\tR top = tp(theta);\n\t\trep(i, 3) {\n\t\t\tauto &seg = obstacle[obs_idx][i];\n\t\t\tR xl, xr, y;\n\t\t\ty = seg.a.imag();\n\t\t\txl = seg.a.real();\n\t\t\txr = seg.b.real();\n\t\t\tif(eq(theta, 0.899297831746404)) {\n\t\t\t\tdebug(y, xl, xr);\n\t\t\t}\n\t\t\tif(xl > xr) swap(xl, xr);\n\t\t\tif(x_pig <= xl) continue;\n\t\t\tchmin(xr, x_pig);\n\t\t\tR yl = f(theta, xl);\n\t\t\tR yr = f(theta, xr);\n\t\t\tif(xl <= top && top <= xr) {\n\t\t\t\t// if(f(theta, top) > y) {\n\t\t\t\tif(compare(f(theta, top), y) == 1) {\n\t\t\t\t\tif(compare(yl, y) == -1 || compare(yr, y) == -1) return true;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(compare(yl, y) * compare(yr, y) == -1) return true;\n\t\t}\n\n\t\tdebug(theta);\n\n\t\trep(i, 3, 6) {\n\t\t\tauto &seg = obstacle[obs_idx][i];\n\t\t\tR yd, x, yt;\n\t\t\tx = seg.a.real();\n\t\t\tyd = seg.a.imag();\n\t\t\tyt = seg.b.imag();\n\t\t\tif(eq(theta, 0.899297831746404)) {\n\t\t\t\tdebug(x, yd, yt);\n\t\t\t}\n\t\t\tif(x_pig < x) continue;\n\t\t\tif(yd > yt) swap(yd, yt);\n\t\t\tR y = f(theta, x);\n\t\t\tif(compare(yd, y) == -1 && compare(y, yt) == -1) return true;\n\t\t}\n\n\t\treturn false;\n\t};\n\n\tauto use = [&](R angle) { return EPS < angle && angle < pi / 2 - EPS * 2; };\n\n\tauto ok = [&](R angle) {\n\t\tif(!use(angle)) return false;\n\t\trep(i, n) if(collision(angle, i)) return false;\n\t\tR y = f(angle, x_pig);\n\t\tif(y < y_pig) return false;\n\t\tSegment egg(Point(x_pig, y), Point(x_pig, y_pig));\n\t\tfoa(ob, obstacle) {\n\t\t\tfoa(seg, ob) {\n\t\t\t\tif(intersect(seg, egg)) {\n\t\t\t\t\treturn false;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tdebug(angle);\n\t\tdebug(f(angle, 1));\n\t\tdebug(f(angle, 2));\n\t\tdebug(f(angle, 3));\n\t\treturn true;\n\t};\n\n\t// // auto res = calc_theta(0.5, 12);\n\t// debug(res);\n\t// debug(f(res.first, 1.2));\n\t// debug(f(res.second, 1.2));\n\n\tfoa(p, pts) {\n\t\tif(eq(p.real(), 0.)) continue;\n\t\tauto angles = calc_theta(p.real(), p.imag());\n\t\tif(ok(angles.first) || ok(angles.second)) return true;\n\t}\n\n\tauto angles = calc_theta(x_pig, y_pig);\n\treturn (ok(angles.first) || ok(angles.second));\n}\n\nint main() {\n\tstd::ios::sync_with_stdio(false);\n\tstd::cin.tie(nullptr);\n\tstd::cout << std::fixed << std::setprecision(15);\n\t// std::cerr << std::fixed << std::setprecision(15);\n\tsrand((unsigned)time(NULL));\n\n\tint n;\n\twhile(cin >> n) {\n\t\tYes(solve(n));\n\t}\n\n\treturn 0;\n}", "accuracy": 0.8, "time_ms": 50, "memory_kb": 3952, "score_of_the_acc": -1.0836, "final_rank": 7 }, { "submission_id": "aoj_2308_5846601", "code_snippet": "/*\tauthor: Kite_kuma\n\tcreated: 2021.09.01 15:05:47 */\n\n// #ifdef LOCAL\n// #define _GLIBCXX_DEBUG\n// #endif\n#include <bits/stdc++.h>\nusing namespace std;\n\n#pragma region macros\n\n#define foa(s, v) for(auto &s : v)\n#define all(v) (v).begin(), (v).end()\n#define rall(v) (v).rbegin(), (v).rend()\n#define popcnt(n) __builtin_popcountll((long long)n)\n\n#define REPname(a, b, c, d, e, ...) e\n#define rep(...) REPname(__VA_ARGS__, REP3, REP2, REP1, REP0)(__VA_ARGS__)\n#define REP0(x) for(int Counter_in_rep_macro = 0; Counter_in_rep_macro < (x); ++Counter_in_rep_macro)\n#define REP1(i, x) for(int i = 0; i < (x); ++i)\n#define REP2(i, l, r) for(int i = (l); i < (r); ++i)\n#define REP3(i, l, r, c) for(int i = (l); i < (r); i += (c))\n\n#define DREPname(a, b, c, d, e, ...) e\n#define drep(...) DREPname(__VA_ARGS__, DREP3, DREP2, DREP1)(__VA_ARGS__)\n#define DREP1(i, x) for(int i = (x)-1; i >= 0; --i)\n#define DREP2(i, l, r) for(int i = (r)-1; i >= (l); --i)\n#define DREP3(i, l, r, c) for(int i = (r)-1; i >= (l); i -= (c))\n\n#pragma endregion\n\n#pragma region aliases\n\nusing ll = long long;\nusing ld = long double;\nusing ull = unsigned long long;\nusing vi = std::vector<int>;\nusing vvi = std::vector<std::vector<int>>;\nusing vvvi = std::vector<std::vector<std::vector<int>>>;\nusing vll = std::vector<ll>;\nusing vvll = std::vector<vll>;\nusing vvvll = std::vector<vvll>;\nusing pii = std::pair<int, int>;\nusing pll = std::pair<long long, long long>;\ntemplate <class T = ll>\nusing V = std::vector<T>;\ntemplate <class T = ll>\nusing VV = V<V<T>>;\ntemplate <class T = ll>\nusing VVV = V<V<V<T>>>;\ntemplate <class T = ll>\nusing pqup = std::priority_queue<T, std::vector<T>, std::greater<T>>;\ntemplate <class T = ll>\nusing pqdn = std::priority_queue<T>;\n\n#pragma endregion\n\n#pragma region constants\n\nconst int inf = 1e9;\nconst long long INF = 1e18;\nconst long double pi = acos(-1);\nconst char dl = '\\n';\nconst char sp = ' ';\nint dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};\nint dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};\n\nconst int mod_1000000007 = 1000000007;\nconst int mod_998244353 = 998244353;\n\n#pragma endregion\n\n#pragma region basic_operation\n\ntemplate <class T0, class T1, class T2>\ninline bool in_range(T0 x, T1 lef, T2 rig) {\n\treturn ((lef <= x) && (x < rig));\n}\n\ntemplate <class T>\ninline bool chmin(T &a, T b) {\n\tif(a > b) {\n\t\ta = b;\n\t\treturn true;\n\t}\n\treturn false;\n}\ntemplate <class T>\ninline bool chmax(T &a, T b) {\n\tif(a < b) {\n\t\ta = b;\n\t\treturn true;\n\t}\n\treturn false;\n}\n\nvoid Yes(bool f = 1) { std::cout << (f ? \"Yes\" : \"No\") << '\\n'; }\nvoid No() { std::cout << \"No\\n\"; }\nvoid YES(bool f = 1) { std::cout << (f ? \"YES\" : \"NO\") << '\\n'; }\nvoid NO() { std::cout << \"NO\\n\"; }\n\ntemplate <class T>\nvoid drop(T answer) {\n\tstd::cout << answer << '\\n';\n\texit(0);\n}\n\nvoid err(bool flag = true) {\n\tif(!flag) return;\n\tstd::cout << -1 << '\\n';\n\texit(0);\n}\n\ntemplate <class T>\nvoid vout(std::vector<T> const &v, bool vertically = 0) {\n\tif(vertically) {\n\t\tfor(auto const &a : v) {\n\t\t\tstd::cout << a << '\\n';\n\t\t}\n\t\treturn;\n\t}\n\tfor(auto it = v.begin(); it != v.end(); it++) {\n\t\tstd::cout << (*it);\n\t\tif(it != v.end() - 1) {\n\t\t\tstd::cout << ' ';\n\t\t}\n\t}\n\tstd::cout << '\\n';\n\treturn;\n}\n\ninline void print() { std::cout << '\\n'; }\ntemplate <class T>\ninline void print(T x) {\n\tstd::cout << x << '\\n';\n\treturn;\n}\ntemplate <typename Head, typename... Tail>\nvoid print(Head H, Tail... T) {\n\tstd::cout << H << \" \";\n\tprint(T...);\n}\n\ntemplate <class T>\nvoid add(std::vector<T> &v, T val) {\n\tfor(auto &a : v) a += val;\n\treturn;\n}\n\ntemplate <class T>\nT dup(T a, T b) {\n\tassert(b != 0);\n\treturn (a + b - 1) / b;\n}\n\ntemplate <class T>\nT greatest_lower_multiple(T x, T d) {\n\tif(d == 0) return 0;\n\tif(d < 0) d *= -1;\n\tT y = x % d;\n\tif(y < 0) y += d;\n\treturn x - y;\n}\n\ntemplate <class T>\nT least_upper_multiple(T x, T d) {\n\treturn -greatest_lower_multiple(-x, d);\n}\n\nlong long POW(long long a, long long n) {\n\tlong long res = 1;\n\twhile(n > 0) {\n\t\tif(n & 1) res = res * a;\n\t\ta = a * a;\n\t\tn >>= 1;\n\t}\n\treturn res;\n}\n\nlong long modpow(long long a, long long n, long long mod) {\t // a^n mod\n\tassert(n >= 0 && mod);\n\tif(mod == 1) return 0LL;\n\tlong long res = 1;\n\ta %= mod;\n\twhile(n > 0) {\n\t\tif(n & 1) res = res * a % mod;\n\t\ta = a * a % mod;\n\t\tn >>= 1;\n\t}\n\treturn res < 0 ? res + mod : res;\n}\n\n// return x which satisfies a * x % mod == gcd(a, mod)\n// not (mod | a)\nlong long modinv(long long a, long long mod) {\n\tlong long b = mod, u = 1, v = 0;\n\twhile(b) {\n\t\tlong long t = a / b;\n\t\ta -= t * b;\n\t\tstd::swap(a, b);\n\t\tu -= t * v;\n\t\tstd::swap(u, v);\n\t}\n\tu %= mod;\n\tif(u < 0) u += mod;\n\treturn u;\n}\n\ntemplate <class T>\nint lb(const std::vector<T> &a, const T x) {\n\treturn std::distance(a.begin(), std::lower_bound(a.begin(), a.end(), x));\n}\ntemplate <class T>\nint ub(const std::vector<T> &a, const T x) {\n\treturn std::distance(a.begin(), std::upper_bound(a.begin(), a.end(), x));\n}\ntemplate <class T>\nvoid unq_sort(std::vector<T> &a) {\n\tstd::sort(a.begin(), a.end());\n\ta.erase(std::unique(a.begin(), a.end()), a.end());\n}\ntemplate <class T>\nstd::vector<int> press(std::vector<T> &a) {\n\tauto vec = a;\n\tunq_sort(vec);\n\tstd::vector<int> ret;\n\tfor(auto &v : a) ret.push_back(lb(vec, v));\n\treturn ret;\n}\n\n#pragma endregion\n\n#pragma region input\n#define VEC(type, name, size) \\\n\tvector<type> name(size); \\\n\tscanner::INPUT(name)\n#define VVEC(type, name, h, w) \\\n\tvector<vector<type>> name(h, vector<type>(w)); \\\n\tscanner::INPUT(name)\n#define INT(...) \\\n\tint __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define LL(...) \\\n\tlong long __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define STR(...) \\\n\tstring __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define CHR(...) \\\n\tchar __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define DBL(...) \\\n\tdouble __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define LD(...) \\\n\tlong double __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define TPL3(type0, type1, type2, name) \\\n\tstd::tuple<type0, type1, type2> name; \\\n\tscanner::INPUT(name);\n#define TPL4(type0, type1, type2, type3, name) \\\n\tstd::tuple<type0, type1, type2, type3> name; \\\n\tscanner::INPUT(name);\n\nnamespace scanner {\ntemplate <class T>\nvoid scan(T &a) {\n\tstd::cin >> a;\n}\n\ntemplate <class T, class L>\nvoid scan(std::pair<T, L> &p) {\n\tscan(p.first);\n\tscan(p.second);\n}\n\ntemplate <class T0, class T1, class T2>\nvoid scan(std::tuple<T0, T1, T2> &p) {\n\tT0 t0;\n\tT1 t1;\n\tT2 t2;\n\tscan(t0);\n\tscan(t1);\n\tscan(t2);\n\tp = std::make_tuple(t0, t1, t2);\n}\n\ntemplate <class T0, class T1, class T2, class T3>\nvoid scan(std::tuple<T0, T1, T2, T3> &p) {\n\tT0 t0;\n\tT1 t1;\n\tT2 t2;\n\tT3 t3;\n\tscan(t0);\n\tscan(t1);\n\tscan(t2);\n\tscan(t3);\n\tp = std::make_tuple(t0, t1, t2, t3);\n}\n\ntemplate <class T>\nvoid scan(std::vector<T> &a) {\n\tfor(auto &i : a) scan(i);\n}\n\nvoid INPUT() {}\ntemplate <class Head, class... Tail>\nvoid INPUT(Head &head, Tail &... tail) {\n\tscan(head);\n\tINPUT(tail...);\n}\n} // namespace scanner\n\ntemplate <typename T1, typename T2>\nstd::istream &operator>>(std::istream &is, std::pair<T1, T2> &p) {\n\tis >> p.first >> p.second;\n\treturn is;\n}\n#pragma endregion\n\n#pragma region debug\n\n#pragma region output\ntemplate <typename T1, typename T2>\nstd::ostream &std::operator<<(std::ostream &os, const std::pair<T1, T2> &p) {\n\tos << p.first << \" \" << p.second;\n\treturn os;\n}\ntemplate <class T>\nstd::ostream &std::operator<<(std::ostream &os, const std::vector<T> &v) {\n\tfor(int i = 0; i < (int)v.size(); i++) {\n\t\tif(i) os << \" \";\n\t\tos << v[i];\n\t}\n\treturn os;\n}\n#pragma endregion\n\n#pragma region view\n\nnamespace viewer {\nvoid view(const long long e) {\n\tif(e == INF)\n\t\tstd::cerr << \"INF\";\n\telse if(e == -INF)\n\t\tstd::cerr << \"-INF\";\n\telse\n\t\tstd::cerr << e;\n}\n\nvoid view(const int e) {\n\tif(e == inf)\n\t\tstd::cerr << \"inf\";\n\telse if(e == -inf)\n\t\tstd::cerr << \"-inf\";\n\telse\n\t\tstd::cerr << e;\n}\n\ntemplate <typename T>\nvoid view(const T e) {\n\tstd::cerr << e;\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::pair<T, U> &p) {\n\tstd::cerr << \"(\";\n\tview(p.first);\n\tstd::cerr << \", \";\n\tview(p.second);\n\tstd::cerr << \")\";\n}\n\ntemplate <class T0, class T1, class T2>\nvoid view(const std::tuple<T0, T1, T2> &p) {\n\tstd::cerr << \"(\";\n\tview(std::get<0>(p));\n\tstd::cerr << \", \";\n\tview(std::get<1>(p));\n\tstd::cerr << \", \";\n\tview(std::get<2>(p));\n\tstd::cerr << \")\";\n}\n\ntemplate <class T0, class T1, class T2, class T3>\nvoid view(const std::tuple<T0, T1, T2, T3> &p) {\n\tstd::cerr << \"(\";\n\tview(std::get<0>(p));\n\tstd::cerr << \", \";\n\tview(std::get<1>(p));\n\tstd::cerr << \", \";\n\tview(std::get<2>(p));\n\tstd::cerr << \", \";\n\tview(std::get<3>(p));\n\tstd::cerr << \")\";\n}\n\ntemplate <typename T>\nvoid view(const std::set<T> &s) {\n\tif(s.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << \"{ \";\n\tfor(auto &t : s) {\n\t\tview(t);\n\t\tstd::cerr << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\n\ntemplate <typename T>\nvoid view(const std::unordered_set<T> &s) {\n\tif(s.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << \"{ \";\n\tfor(auto &t : s) {\n\t\tview(t);\n\t\tstd::cerr << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\n\ntemplate <typename T>\nvoid view(const std::vector<T> &v) {\n\tif(v.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << \"{ \";\n\tfor(const auto &e : v) {\n\t\tview(e);\n\t\tstd::cerr << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\n\ntemplate <typename T>\nvoid view(const std::vector<std::vector<T>> &vv) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &v : vv) {\n\t\tstd::cerr << \"\\t\";\n\t\tview(v);\n\t\tstd::cerr << '\\n';\n\t}\n\tstd::cerr << \"}\";\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::vector<std::pair<T, U>> &v) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &c : v) {\n\t\tstd::cerr << \"\\t(\";\n\t\tview(c.first);\n\t\tstd::cerr << \", \";\n\t\tview(c.second);\n\t\tstd::cerr << \")\\n\";\n\t}\n\tstd::cerr << \"}\";\n}\n\ntemplate <class T0, class T1, class T2>\nvoid view(const std::vector<std::tuple<T0, T1, T2>> &v) {\n\tif(v.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << '{';\n\tfor(const auto &t : v) {\n\t\tstd::cerr << \"\\n\\t\";\n\t\tview(t);\n\t\tstd::cerr << \",\";\n\t}\n\tstd::cerr << \"\\n}\";\n}\n\ntemplate <class T0, class T1, class T2, class T3>\nvoid view(const std::vector<std::tuple<T0, T1, T2, T3>> &v) {\n\tif(v.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << '{';\n\tfor(const auto &t : v) {\n\t\tstd::cerr << \"\\n\\t\";\n\t\tview(t);\n\t\tstd::cerr << \",\";\n\t}\n\tstd::cerr << \"\\n}\";\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::map<T, U> &m) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &t : m) {\n\t\tstd::cerr << \"\\t[\";\n\t\tview(t.first);\n\t\tstd::cerr << \"] : \";\n\t\tview(t.second);\n\t\tstd::cerr << '\\n';\n\t}\n\tstd::cerr << \"}\";\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::unordered_map<T, U> &m) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &t : m) {\n\t\tstd::cerr << \"\\t[\";\n\t\tview(t.first);\n\t\tstd::cerr << \"] : \";\n\t\tview(t.second);\n\t\tstd::cerr << '\\n';\n\t}\n\tstd::cerr << \"}\";\n}\n} // namespace viewer\n\n#pragma endregion\n\n// when debugging : g++ foo.cpp -DLOCAL\n#ifdef LOCAL\nvoid debug_out() {}\ntemplate <typename Head, typename... Tail>\nvoid debug_out(Head H, Tail... T) {\n\tviewer::view(H);\n\tstd::cerr << \", \";\n\tdebug_out(T...);\n}\n#define debug(...) \\\n\tdo { \\\n\t\tstd::cerr << __LINE__ << \" [\" << #__VA_ARGS__ << \"] : [\"; \\\n\t\tdebug_out(__VA_ARGS__); \\\n\t\tstd::cerr << \"\\b\\b]\\n\"; \\\n\t} while(0)\n#define dump(x) \\\n\tdo { \\\n\t\tstd::cerr << __LINE__ << \" \" << #x << \" : \"; \\\n\t\tviewer::view(x); \\\n\t\tstd::cerr << '\\n'; \\\n\t} while(0)\n\n#else\n#define debug(...) (void(0))\n#define dump(x) (void(0))\n#endif\n\n#pragma endregion\n\nusing R = long double;\n\n#pragma region geometry_light\n\nusing Real = long double;\nusing R = Real;\nusing Point = std::complex<Real>;\nusing Vec = Point;\nconst Real EPS = 1e-10, PI = acos(-1);\n\ninline bool eq(const Real &a, const Real &b) { return fabs(b - a) < EPS; }\ninline bool eq(const Point &a, const Point &b) { return fabs(b - a) < EPS; }\n/*\n\t-1: a < b\n\t0 : a == b\n\t1 : a > b\n*/\ninline int compare(const Real &a, const Real &b) { return eq(a, b) ? 0 : a < b ? -1 : 1; }\ninline int sign(const Real &a) { return fabs(a) < EPS ? 0 : a < 0 ? -1 : 1; }\n\nnamespace std {\nconst Point operator*(const Point &p, const Real &d) { return Point(real(p) * d, imag(p) * d); }\n} // namespace std\n\nstd::istream &operator>>(std::istream &is, Point &p) {\n\tReal a, b;\n\tis >> a >> b;\n\tp = Point(a, b);\n\treturn is;\n}\nstd::ostream &operator<<(std::ostream &os, Point &p) { return os << std::fixed << std::setprecision(10) << p.real() << \" \" << p.imag(); }\n\n// rotate point 'p' for counter clockwise direction\nconst Point rotate(const Real &theta, const Point &p) {\n\treturn Point(cos(theta) * p.real() - sin(theta) * p.imag(), sin(theta) * p.real() + cos(theta) * p.imag());\n}\n\nconst Real radian_to_degree(const Real &r) { return (r * 180.0 / PI); }\n\nconst Real degree_to_radian(const Real &d) { return (d * PI / 180.0); }\n\n// get angle a-b-c (<pi)\nconst Real get_angle(const Point &a, const Point &b, const Point &c) {\n\tconst Point v(a - b), w(c - b);\n\tReal theta = fabs(atan2(w.imag(), w.real()) - atan2(v.imag(), v.real()));\n\treturn std::min(theta, 2 * PI - theta);\n}\n\nnamespace std {\nbool operator<(const Point &a, const Point &b) { return a.real() != b.real() ? a.real() < b.real() : a.imag() < b.imag(); }\n} // namespace std\n\nstruct Line {\n\tPoint a, b;\n\n\tLine() = default;\n\n\tLine(Point a, Point b) : a(a), b(b) {}\n\n\tLine(Real A, Real B, Real C) // Ax + By = C\n\t{\n\t\tif(eq(A, 0))\n\t\t\ta = Point(0, C / B), b = Point(1, C / B);\n\t\telse if(eq(B, 0))\n\t\t\tb = Point(C / A, 0), b = Point(C / A, 1);\n\t\telse\n\t\t\ta = Point(0, C / B), b = Point(C / A, 0);\n\t}\n\n\tfriend std::ostream &operator<<(std::ostream &os, Line &p) { return os << p.a << \" -- \" << p.b; }\n\n\tfriend std::istream &operator>>(std::istream &is, Line &a) { return is >> a.a >> a.b; }\n};\n\nstruct Segment : Line {\n\tSegment() = default;\n\n\tSegment(Point a, Point b) : Line(a, b) {}\n};\n\nstruct Circle {\n\tPoint p;\n\tReal r;\n\n\tCircle() = default;\n\n\tCircle(Point p, Real r) : p(p), r(r) {}\n};\n\nusing Points = std::vector<Point>;\nusing Polygon = std::vector<Point>;\nusing Segments = std::vector<Segment>;\nusing Lines = std::vector<Line>;\nusing Circles = std::vector<Circle>;\n\nconst Real cross(const Point &a, const Point &b) { return real(a) * imag(b) - imag(a) * real(b); }\nconst Real dot(const Point &a, const Point &b) { return real(a) * real(b) + imag(a) * imag(b); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_C\nconst int ccw(const Point &a, Point b, Point c) {\n\tb = b - a, c = c - a;\n\tif(cross(b, c) > EPS) return +1;\t\t// \"COUNTER_CLOCKWISE\"\n\tif(cross(b, c) < -EPS) return -1;\t\t// \"CLOCKWISE\"\n\tif(dot(b, c) < -EPS) return +2;\t\t\t// \"ONLINE_BACK\"\n\tif(norm(b) + EPS < norm(c)) return -2;\t// \"ONLINE_FRONT\"\n\treturn 0;\t\t\t\t\t\t\t\t// \"ON_SEGMENT\"\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_A\nbool parallel(const Line &a, const Line &b) { return eq(cross(a.b - a.a, b.b - b.a), 0.0); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_A\nbool orthogonal(const Line &a, const Line &b) { return eq(dot(a.a - a.b, b.a - b.b), 0.0); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_A\nPoint projection(const Line &l, const Point &p) {\n\tdouble t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n\treturn l.a + (l.a - l.b) * t;\n}\n\nPoint projection(const Segment &l, const Point &p) {\n\tdouble t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n\treturn l.a + (l.a - l.b) * t;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_B\nPoint reflection(const Line &l, const Point &p) { return projection(l, p) * 2.0 - p; }\n\nint intersect(const Line &l, const Point &p) { return int(abs(ccw(l.a, l.b, p)) != 1); }\n\nint intersect(const Line &l, const Line &m) {\n\tif(intersect(l, m.a) && intersect(l, m.b)) return -1;\n\treturn int(abs(cross(l.b - l.a, m.b - m.a)) > EPS);\n}\n\nint intersect(const Segment &s, const Point &p) { return int(ccw(s.a, s.b, p) == 0); }\n\nint intersect(const Line &l, const Segment &s) {\n\tif(intersect(l, s.a) && intersect(l, s.b)) return -1;\n\treturn cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < EPS;\n}\n\nReal distance(const Line &l, const Point &p);\n\nint intersect(const Circle &c, const Line &l) {\n\tReal d = c.r - distance(l, c.p);\n\treturn fabs(d) < EPS ? 1 : d > 0. ? 2 : 0;\n}\n\nint intersect(const Circle &c, const Point &p) { return int(abs(abs(p - c.p) - c.r) < EPS); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_B\nint intersect(const Segment &s, const Segment &t) {\n\tif(eq(s.a, s.b)) return intersect(t, s.a);\n\tif(intersect(Line(s), t.a) && intersect(Line(s), t.b) &&\n\t std::max(std::min(s.a, s.b), std::min(t.a, t.b)) < std::min(std::max(s.a, s.b), std::max(t.a, t.b)))\n\t\treturn -1;\n\treturn int(ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 && ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0);\n}\n\nint intersect(const Circle &c, const Segment &l) {\n\tconst Point h = projection(l, c.p);\n\tif(norm(h - c.p) - c.r * c.r > EPS) return 0;\n\tauto d1 = abs(c.p - l.a), d2 = abs(c.p - l.b);\n\tif(compare(d1, c.r) == -1 && compare(d2, c.r) == -1) return 0;\n\tif(d1 < c.r - EPS && d2 > c.r - EPS || d1 > c.r - EPS && d2 < c.r - EPS) return 1;\n\treturn dot(l.a - h, l.b - h) < 0 ? 2 : 0;\n}\n\nReal distance(const Point &a, const Point &b);\n\nint number_of_common_tangents(const Circle &c1, const Circle &c2) {\n\tReal r1 = std::min(c1.r, c2.r), r2 = std::max(c1.r, c2.r), d = distance(c1.p, c2.p);\n\tint com = compare(r1 + r2, d);\n\treturn com == 1 ? compare(d + r1, r2) + 1 : 3 - com;\n\t// if(c1.r < c2.r) swap(c1, c2);\n\t// Real d = abs(c1.p - c2.p);\n\t// if(compare(c1.r + c2.r, d) == -1) return 4;\n\t// if(eq(c1.r + c2.r, d)) return 3;\n\t// if(compare(c1.r - c2.r, d) == -1) return 2;\n\t// return int(eq(c1.r - c2.r, d));\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_A&lang=jp\nint intersect(const Circle &c1, const Circle &c2) { return 2 - abs(2 - number_of_common_tangents(c1, c2)); }\n\nReal distance(const Point &a, const Point &b) { return abs(a - b); }\n\nReal distance(const Line &l, const Point &p) { return abs(p - projection(l, p)); }\n\nReal distance(const Line &l, const Line &m) { return intersect(l, m) ? 0 : distance(l, m.a); }\n\nReal distance(const Segment &s, const Point &p) {\n\tPoint r = projection(s, p);\n\treturn intersect(s, r) ? distance(r, p) : std::min(abs(s.a - p), abs(s.b - p));\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_D\nReal distance(const Segment &a, const Segment &b) {\n\tif(intersect(a, b)) return 0;\n\treturn std::min({distance(a, b.a), distance(a, b.b), distance(b, a.a), distance(b, a.b)});\n}\n\nReal distance(const Line &l, const Segment &s) {\n\tif(intersect(l, s)) return 0;\n\treturn std::min(distance(l, s.a), distance(l, s.b));\n}\n\nPoint crosspoint(const Line &l, const Line &m) {\n\tReal A = cross(l.b - l.a, m.b - m.a);\n\tReal B = cross(l.b - l.a, l.b - m.a);\n\tif(eq(abs(A), 0.0) && eq(abs(B), 0.0)) return m.a;\n\treturn m.a + (m.b - m.a) * B / A;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_C\nPoint crosspoint(const Segment &l, const Segment &m) { return crosspoint(Line(l), Line(m)); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_D\nstd::pair<Point, Point> crosspoint(const Circle &c, const Line l) {\n\tPoint pr = projection(l, c.p);\n\tif(eq(distance(l, c.p), c.r)) return {pr, pr};\n\tVec v = (l.b - l.a) / abs(l.b - l.a) * sqrt(c.r * c.r - norm(pr - c.p));\n\treturn make_pair(pr - v, pr + v);\n\t// Vec e = (l.b - l.a) / abs(l.b - l.a);\n\t// double base = sqrt(c.r * c.r - norm(pr - c.p));\n\t// return {pr - e * base, pr + e * base};\n}\n\nstd::pair<Point, Point> crosspoint(const Circle &c, const Segment &l) {\n\tif(intersect(c, l) == 2) return crosspoint(c, Line(l.a, l.b));\n\tauto ret = crosspoint(c, Line(l.a, l.b));\n\tif(dot(l.a - ret.first, l.b - ret.first) < EPS)\n\t\tret.second = ret.first;\n\telse\n\t\tret.first = ret.second;\n\treturn ret;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_E\nstd::pair<Point, Point> crosspoint(const Circle &c1, const Circle &c2) {\n\tReal d = abs(c1.p - c2.p);\n\tReal a = acos((c1.r * c1.r + d * d - c2.r * c2.r) / (2 * c1.r * d)); // cosine theorem\n\tReal t = atan2(c2.p.imag() - c1.p.imag(), c2.p.real() - c1.p.real());\n\tPoint p1 = c1.p + Point(cos(t + a) * c1.r, sin(t + a) * c1.r);\n\tPoint p2 = c1.p + Point(cos(t - a) * c1.r, sin(t - a) * c1.r);\n\treturn make_pair(p1, p2);\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_F\n// \"点 p を通る円 c の接線\"の接点2つ\nstd::pair<Point, Point> tangent(const Circle &c1, const Point &p2) {\n\treturn crosspoint(c1, Circle(p2, sqrt(norm(c1.p - p2) - c1.r * c1.r)));\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_G\n// 円 c1, c2 の共通接線\nLines tangent(Circle c1, Circle c2) {\n\tLines ret;\n\tif(c1.r < c2.r) std::swap(c1, c2);\n\tReal g = norm(c1.p - c2.p);\n\tif(eq(g, 0)) return ret;\n\tVec u = (c2.p - c1.p) / Real(sqrt(g));\n\tVec v = rotate(PI * 0.5, u);\n\tfor(int s : {-1, 1}) {\n\t\tReal h = (c1.r + s * c2.r) / sqrt(g);\n\t\tif(eq(1 - h * h, 0)) {\n\t\t\tret.emplace_back(c1.p + u * c1.r, c1.p + (u + v) * c1.r);\n\t\t} else if(1 - h * h > 0) {\n\t\t\tPoint uu = u * h, vv = v * sqrt(1 - h * h);\n\t\t\tret.emplace_back(c1.p + (uu + vv) * c1.r, c2.p - (uu + vv) * c2.r * s);\n\t\t\tret.emplace_back(c1.p + (uu - vv) * c1.r, c2.p - (uu - vv) * c2.r * s);\n\t\t}\n\t}\n\treturn ret;\n}\n\n#pragma endregion\n\nbool solve(int n) {\n\tR velocity;\n\tcin >> velocity;\n\tR x_pig, y_pig;\n\tcin >> x_pig >> y_pig;\n\n\tVV<Segment> obstacle(n); // 0 ~ 2 yoko, 3~5 tate\n\n\tPoints pts;\n\n\trep(j, n) {\n\t\tR l, b, r, t;\n\t\tcin >> l >> b >> r >> t;\n\t\tpts.emplace_back(l, t);\n\t\tpts.emplace_back(l, b);\n\t\tpts.emplace_back(r, t);\n\t\tpts.emplace_back(r, b);\n\n\t\tR mid_x = (l + r) / 2;\n\t\tR mid_y = (b + t) / 2;\n\t\tPoints pts;\n\t\tfor(R x : {l, mid_x, r}) {\n\t\t\tfor(R y : {b, mid_y, t}) {\n\t\t\t\tpts.emplace_back(x, y);\n\t\t\t}\n\t\t}\n\t\trep(i, 3) { obstacle[j].emplace_back(pts[i], pts[i + 6]); }\n\t\trep(i, 3) obstacle[j].emplace_back(pts[i * 3], pts[i * 3 + 2]);\n\t}\n\n\tconst R g = 9.8;\n\n\tauto f = [&](R theta, R x) { return x * tan(theta) - g / 2 * pow(x / (velocity * cos(theta)), (R)2.); };\n\n\tauto calc_top = [&](R obj_x) -> R {\n\t\tR inf = 0;\n\t\tR sup = pi / 2 - EPS;\n\t\tR lef, rig;\n\t\trep(300) {\n\t\t\tlef = inf + (sup - inf) / 3;\n\t\t\trig = (lef + sup) / 2;\n\t\t\tif(f(lef, obj_x) < f(rig, obj_x)) {\n\t\t\t\tinf = lef;\n\t\t\t} else {\n\t\t\t\tsup = rig;\n\t\t\t}\n\t\t}\n\t\treturn lef;\n\t};\n\n\tauto binarysearch = [&](R lef, R rig, R obj_x, R obj_y, bool lef_is_large) {\n\t\tR mid;\n\t\trep(300) {\n\t\t\tmid = lef + rig;\n\t\t\tmid /= 2;\n\t\t\tif(bool(f(mid, obj_x) > obj_y) ^ (bool)lef_is_large) {\n\t\t\t\trig = mid;\n\t\t\t} else {\n\t\t\t\tlef = mid;\n\t\t\t}\n\t\t}\n\t\treturn mid;\n\t};\n\n\t// y = x tan theta - (g/2) (x/(v_0 cos theta))^2\n\tauto calc_theta = [&](R obj_x, R obj_y) -> pair<R, R> {\n\t\tR inf = 0;\n\t\tR sup = pi / 2 - EPS;\n\n\t\tR mid = calc_top(obj_x);\n\n\t\tif(compare(f(mid, obj_x), obj_y) == -1) {\n\t\t\treturn make_pair(-1., -1.);\n\t\t}\n\n\t\t// R a, b;\n\t\t// inf = 0;\n\t\t// rep(300) {\n\t\t// \ta = (inf + mid) / 2;\n\t\t// \tif(f(a, obj_x) < obj_y) {\n\t\t// \t\tinf = a;\n\t\t// \t} else {\n\t\t// \t\tmid = a;\n\t\t// \t}\n\t\t// }\n\n\t\t// mid = sup;\n\t\t// sup = pi / 2 - EPS;\n\n\t\t// rep(300) {\n\t\t// \tb = (mid + sup) / 2;\n\t\t// \tif(f(b, obj_x) < obj_y) {\n\t\t// \t\tsup = b;\n\t\t// \t} else {\n\t\t// \t\tmid = b;\n\t\t// \t}\n\t\t// }\n\n\t\treturn make_pair(binarysearch(inf, mid, obj_x, obj_y, false), binarysearch(mid, sup, obj_x, obj_y, true));\n\t};\n\n\tauto tp = [&](R theta) { return pow(velocity, 2.) * cos(theta) * sin(theta) / g; };\n\n\tauto collision = [&](R theta, int obs_idx) -> bool {\n\t\tR top = tp(theta);\n\t\trep(i, 3) {\n\t\t\tauto &seg = obstacle[obs_idx][i];\n\t\t\tR xl, xr, y;\n\t\t\ty = seg.a.imag();\n\t\t\txl = seg.a.real();\n\t\t\txr = seg.b.real();\n\t\t\tif(eq(theta, 0.899297831746404)) {\n\t\t\t\tdebug(y, xl, xr);\n\t\t\t}\n\t\t\tif(xl > xr) swap(xl, xr);\n\t\t\tif(x_pig <= xl) continue;\n\t\t\tchmin(xr, x_pig);\n\t\t\tR yl = f(theta, xl);\n\t\t\tR yr = f(theta, xr);\n\t\t\tif(xl <= top && top <= xr) {\n\t\t\t\t// if(f(theta, top) > y) {\n\t\t\t\tif(compare(f(theta, top), y) == 1) {\n\t\t\t\t\tif(compare(yl, y) == -1 || compare(yr, y) == -1) return true;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif((yl - y) * (yr - y) < -EPS) return true;\n\t\t}\n\n\t\tdebug(theta);\n\n\t\trep(i, 3, 6) {\n\t\t\tauto &seg = obstacle[obs_idx][i];\n\t\t\tR yd, x, yt;\n\t\t\tx = seg.a.real();\n\t\t\tyd = seg.a.imag();\n\t\t\tyt = seg.b.imag();\n\t\t\tif(eq(theta, 0.899297831746404)) {\n\t\t\t\tdebug(x, yd, yt);\n\t\t\t}\n\t\t\tif(x_pig < x) continue;\n\t\t\tif(yd > yt) swap(yd, yt);\n\t\t\tR y = f(theta, x);\n\t\t\tif(compare(yd, y) == -1 && compare(y, yt) == -1) return true;\n\t\t}\n\n\t\treturn false;\n\t};\n\n\tauto use = [&](R angle) { return EPS < angle && angle < pi / 2 - EPS * 2; };\n\n\tauto ok = [&](R angle) {\n\t\tif(!use(angle)) return false;\n\t\trep(i, n) if(collision(angle, i)) return false;\n\t\tR y = f(angle, x_pig);\n\t\tif(y < y_pig) return false;\n\t\tSegment egg(Point(x_pig, y), Point(x_pig, y_pig));\n\t\tfoa(ob, obstacle) {\n\t\t\tfoa(seg, ob) {\n\t\t\t\tif(intersect(seg, egg)) {\n\t\t\t\t\treturn false;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tdebug(angle);\n\t\tdebug(f(angle, 1));\n\t\tdebug(f(angle, 2));\n\t\tdebug(f(angle, 3));\n\t\treturn true;\n\t};\n\n\t// // auto res = calc_theta(0.5, 12);\n\t// debug(res);\n\t// debug(f(res.first, 1.2));\n\t// debug(f(res.second, 1.2));\n\n\tfoa(p, pts) {\n\t\tif(eq(p, 0.)) continue;\n\t\tauto angles = calc_theta(p.real(), p.imag());\n\t\tif(ok(angles.first) || ok(angles.second)) return true;\n\t}\n\n\tauto angles = calc_theta(x_pig, y_pig);\n\treturn (ok(angles.first) || ok(angles.second));\n}\n\nint main() {\n\tstd::ios::sync_with_stdio(false);\n\tstd::cin.tie(nullptr);\n\tstd::cout << std::fixed << std::setprecision(15);\n\t// std::cerr << std::fixed << std::setprecision(15);\n\tsrand((unsigned)time(NULL));\n\n\tint n;\n\twhile(cin >> n) {\n\t\tYes(solve(n));\n\t}\n\n\treturn 0;\n}", "accuracy": 0.8, "time_ms": 50, "memory_kb": 3940, "score_of_the_acc": -1.079, "final_rank": 6 }, { "submission_id": "aoj_2308_5846579", "code_snippet": "/*\tauthor: Kite_kuma\n\tcreated: 2021.09.01 15:05:47 */\n\n// #ifdef LOCAL\n// #define _GLIBCXX_DEBUG\n// #endif\n#include <bits/stdc++.h>\nusing namespace std;\n\n#pragma region macros\n\n#define foa(s, v) for(auto &s : v)\n#define all(v) (v).begin(), (v).end()\n#define rall(v) (v).rbegin(), (v).rend()\n#define popcnt(n) __builtin_popcountll((long long)n)\n\n#define REPname(a, b, c, d, e, ...) e\n#define rep(...) REPname(__VA_ARGS__, REP3, REP2, REP1, REP0)(__VA_ARGS__)\n#define REP0(x) for(int Counter_in_rep_macro = 0; Counter_in_rep_macro < (x); ++Counter_in_rep_macro)\n#define REP1(i, x) for(int i = 0; i < (x); ++i)\n#define REP2(i, l, r) for(int i = (l); i < (r); ++i)\n#define REP3(i, l, r, c) for(int i = (l); i < (r); i += (c))\n\n#define DREPname(a, b, c, d, e, ...) e\n#define drep(...) DREPname(__VA_ARGS__, DREP3, DREP2, DREP1)(__VA_ARGS__)\n#define DREP1(i, x) for(int i = (x)-1; i >= 0; --i)\n#define DREP2(i, l, r) for(int i = (r)-1; i >= (l); --i)\n#define DREP3(i, l, r, c) for(int i = (r)-1; i >= (l); i -= (c))\n\n#pragma endregion\n\n#pragma region aliases\n\nusing ll = long long;\nusing ld = long double;\nusing ull = unsigned long long;\nusing vi = std::vector<int>;\nusing vvi = std::vector<std::vector<int>>;\nusing vvvi = std::vector<std::vector<std::vector<int>>>;\nusing vll = std::vector<ll>;\nusing vvll = std::vector<vll>;\nusing vvvll = std::vector<vvll>;\nusing pii = std::pair<int, int>;\nusing pll = std::pair<long long, long long>;\ntemplate <class T = ll>\nusing V = std::vector<T>;\ntemplate <class T = ll>\nusing VV = V<V<T>>;\ntemplate <class T = ll>\nusing VVV = V<V<V<T>>>;\ntemplate <class T = ll>\nusing pqup = std::priority_queue<T, std::vector<T>, std::greater<T>>;\ntemplate <class T = ll>\nusing pqdn = std::priority_queue<T>;\n\n#pragma endregion\n\n#pragma region constants\n\nconst int inf = 1e9;\nconst long long INF = 1e18;\nconst long double pi = acos(-1);\nconst char dl = '\\n';\nconst char sp = ' ';\nint dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};\nint dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};\n\nconst int mod_1000000007 = 1000000007;\nconst int mod_998244353 = 998244353;\n\n#pragma endregion\n\n#pragma region basic_operation\n\ntemplate <class T0, class T1, class T2>\ninline bool in_range(T0 x, T1 lef, T2 rig) {\n\treturn ((lef <= x) && (x < rig));\n}\n\ntemplate <class T>\ninline bool chmin(T &a, T b) {\n\tif(a > b) {\n\t\ta = b;\n\t\treturn true;\n\t}\n\treturn false;\n}\ntemplate <class T>\ninline bool chmax(T &a, T b) {\n\tif(a < b) {\n\t\ta = b;\n\t\treturn true;\n\t}\n\treturn false;\n}\n\nvoid Yes(bool f = 1) { std::cout << (f ? \"Yes\" : \"No\") << '\\n'; }\nvoid No() { std::cout << \"No\\n\"; }\nvoid YES(bool f = 1) { std::cout << (f ? \"YES\" : \"NO\") << '\\n'; }\nvoid NO() { std::cout << \"NO\\n\"; }\n\ntemplate <class T>\nvoid drop(T answer) {\n\tstd::cout << answer << '\\n';\n\texit(0);\n}\n\nvoid err(bool flag = true) {\n\tif(!flag) return;\n\tstd::cout << -1 << '\\n';\n\texit(0);\n}\n\ntemplate <class T>\nvoid vout(std::vector<T> const &v, bool vertically = 0) {\n\tif(vertically) {\n\t\tfor(auto const &a : v) {\n\t\t\tstd::cout << a << '\\n';\n\t\t}\n\t\treturn;\n\t}\n\tfor(auto it = v.begin(); it != v.end(); it++) {\n\t\tstd::cout << (*it);\n\t\tif(it != v.end() - 1) {\n\t\t\tstd::cout << ' ';\n\t\t}\n\t}\n\tstd::cout << '\\n';\n\treturn;\n}\n\ninline void print() { std::cout << '\\n'; }\ntemplate <class T>\ninline void print(T x) {\n\tstd::cout << x << '\\n';\n\treturn;\n}\ntemplate <typename Head, typename... Tail>\nvoid print(Head H, Tail... T) {\n\tstd::cout << H << \" \";\n\tprint(T...);\n}\n\ntemplate <class T>\nvoid add(std::vector<T> &v, T val) {\n\tfor(auto &a : v) a += val;\n\treturn;\n}\n\ntemplate <class T>\nT dup(T a, T b) {\n\tassert(b != 0);\n\treturn (a + b - 1) / b;\n}\n\ntemplate <class T>\nT greatest_lower_multiple(T x, T d) {\n\tif(d == 0) return 0;\n\tif(d < 0) d *= -1;\n\tT y = x % d;\n\tif(y < 0) y += d;\n\treturn x - y;\n}\n\ntemplate <class T>\nT least_upper_multiple(T x, T d) {\n\treturn -greatest_lower_multiple(-x, d);\n}\n\nlong long POW(long long a, long long n) {\n\tlong long res = 1;\n\twhile(n > 0) {\n\t\tif(n & 1) res = res * a;\n\t\ta = a * a;\n\t\tn >>= 1;\n\t}\n\treturn res;\n}\n\nlong long modpow(long long a, long long n, long long mod) {\t // a^n mod\n\tassert(n >= 0 && mod);\n\tif(mod == 1) return 0LL;\n\tlong long res = 1;\n\ta %= mod;\n\twhile(n > 0) {\n\t\tif(n & 1) res = res * a % mod;\n\t\ta = a * a % mod;\n\t\tn >>= 1;\n\t}\n\treturn res < 0 ? res + mod : res;\n}\n\n// return x which satisfies a * x % mod == gcd(a, mod)\n// not (mod | a)\nlong long modinv(long long a, long long mod) {\n\tlong long b = mod, u = 1, v = 0;\n\twhile(b) {\n\t\tlong long t = a / b;\n\t\ta -= t * b;\n\t\tstd::swap(a, b);\n\t\tu -= t * v;\n\t\tstd::swap(u, v);\n\t}\n\tu %= mod;\n\tif(u < 0) u += mod;\n\treturn u;\n}\n\ntemplate <class T>\nint lb(const std::vector<T> &a, const T x) {\n\treturn std::distance(a.begin(), std::lower_bound(a.begin(), a.end(), x));\n}\ntemplate <class T>\nint ub(const std::vector<T> &a, const T x) {\n\treturn std::distance(a.begin(), std::upper_bound(a.begin(), a.end(), x));\n}\ntemplate <class T>\nvoid unq_sort(std::vector<T> &a) {\n\tstd::sort(a.begin(), a.end());\n\ta.erase(std::unique(a.begin(), a.end()), a.end());\n}\ntemplate <class T>\nstd::vector<int> press(std::vector<T> &a) {\n\tauto vec = a;\n\tunq_sort(vec);\n\tstd::vector<int> ret;\n\tfor(auto &v : a) ret.push_back(lb(vec, v));\n\treturn ret;\n}\n\n#pragma endregion\n\n#pragma region input\n#define VEC(type, name, size) \\\n\tvector<type> name(size); \\\n\tscanner::INPUT(name)\n#define VVEC(type, name, h, w) \\\n\tvector<vector<type>> name(h, vector<type>(w)); \\\n\tscanner::INPUT(name)\n#define INT(...) \\\n\tint __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define LL(...) \\\n\tlong long __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define STR(...) \\\n\tstring __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define CHR(...) \\\n\tchar __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define DBL(...) \\\n\tdouble __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define LD(...) \\\n\tlong double __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define TPL3(type0, type1, type2, name) \\\n\tstd::tuple<type0, type1, type2> name; \\\n\tscanner::INPUT(name);\n#define TPL4(type0, type1, type2, type3, name) \\\n\tstd::tuple<type0, type1, type2, type3> name; \\\n\tscanner::INPUT(name);\n\nnamespace scanner {\ntemplate <class T>\nvoid scan(T &a) {\n\tstd::cin >> a;\n}\n\ntemplate <class T, class L>\nvoid scan(std::pair<T, L> &p) {\n\tscan(p.first);\n\tscan(p.second);\n}\n\ntemplate <class T0, class T1, class T2>\nvoid scan(std::tuple<T0, T1, T2> &p) {\n\tT0 t0;\n\tT1 t1;\n\tT2 t2;\n\tscan(t0);\n\tscan(t1);\n\tscan(t2);\n\tp = std::make_tuple(t0, t1, t2);\n}\n\ntemplate <class T0, class T1, class T2, class T3>\nvoid scan(std::tuple<T0, T1, T2, T3> &p) {\n\tT0 t0;\n\tT1 t1;\n\tT2 t2;\n\tT3 t3;\n\tscan(t0);\n\tscan(t1);\n\tscan(t2);\n\tscan(t3);\n\tp = std::make_tuple(t0, t1, t2, t3);\n}\n\ntemplate <class T>\nvoid scan(std::vector<T> &a) {\n\tfor(auto &i : a) scan(i);\n}\n\nvoid INPUT() {}\ntemplate <class Head, class... Tail>\nvoid INPUT(Head &head, Tail &... tail) {\n\tscan(head);\n\tINPUT(tail...);\n}\n} // namespace scanner\n\ntemplate <typename T1, typename T2>\nstd::istream &operator>>(std::istream &is, std::pair<T1, T2> &p) {\n\tis >> p.first >> p.second;\n\treturn is;\n}\n#pragma endregion\n\n#pragma region debug\n\n#pragma region output\ntemplate <typename T1, typename T2>\nstd::ostream &std::operator<<(std::ostream &os, const std::pair<T1, T2> &p) {\n\tos << p.first << \" \" << p.second;\n\treturn os;\n}\ntemplate <class T>\nstd::ostream &std::operator<<(std::ostream &os, const std::vector<T> &v) {\n\tfor(int i = 0; i < (int)v.size(); i++) {\n\t\tif(i) os << \" \";\n\t\tos << v[i];\n\t}\n\treturn os;\n}\n#pragma endregion\n\n#pragma region view\n\nnamespace viewer {\nvoid view(const long long e) {\n\tif(e == INF)\n\t\tstd::cerr << \"INF\";\n\telse if(e == -INF)\n\t\tstd::cerr << \"-INF\";\n\telse\n\t\tstd::cerr << e;\n}\n\nvoid view(const int e) {\n\tif(e == inf)\n\t\tstd::cerr << \"inf\";\n\telse if(e == -inf)\n\t\tstd::cerr << \"-inf\";\n\telse\n\t\tstd::cerr << e;\n}\n\ntemplate <typename T>\nvoid view(const T e) {\n\tstd::cerr << e;\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::pair<T, U> &p) {\n\tstd::cerr << \"(\";\n\tview(p.first);\n\tstd::cerr << \", \";\n\tview(p.second);\n\tstd::cerr << \")\";\n}\n\ntemplate <class T0, class T1, class T2>\nvoid view(const std::tuple<T0, T1, T2> &p) {\n\tstd::cerr << \"(\";\n\tview(std::get<0>(p));\n\tstd::cerr << \", \";\n\tview(std::get<1>(p));\n\tstd::cerr << \", \";\n\tview(std::get<2>(p));\n\tstd::cerr << \")\";\n}\n\ntemplate <class T0, class T1, class T2, class T3>\nvoid view(const std::tuple<T0, T1, T2, T3> &p) {\n\tstd::cerr << \"(\";\n\tview(std::get<0>(p));\n\tstd::cerr << \", \";\n\tview(std::get<1>(p));\n\tstd::cerr << \", \";\n\tview(std::get<2>(p));\n\tstd::cerr << \", \";\n\tview(std::get<3>(p));\n\tstd::cerr << \")\";\n}\n\ntemplate <typename T>\nvoid view(const std::set<T> &s) {\n\tif(s.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << \"{ \";\n\tfor(auto &t : s) {\n\t\tview(t);\n\t\tstd::cerr << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\n\ntemplate <typename T>\nvoid view(const std::unordered_set<T> &s) {\n\tif(s.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << \"{ \";\n\tfor(auto &t : s) {\n\t\tview(t);\n\t\tstd::cerr << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\n\ntemplate <typename T>\nvoid view(const std::vector<T> &v) {\n\tif(v.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << \"{ \";\n\tfor(const auto &e : v) {\n\t\tview(e);\n\t\tstd::cerr << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\n\ntemplate <typename T>\nvoid view(const std::vector<std::vector<T>> &vv) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &v : vv) {\n\t\tstd::cerr << \"\\t\";\n\t\tview(v);\n\t\tstd::cerr << '\\n';\n\t}\n\tstd::cerr << \"}\";\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::vector<std::pair<T, U>> &v) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &c : v) {\n\t\tstd::cerr << \"\\t(\";\n\t\tview(c.first);\n\t\tstd::cerr << \", \";\n\t\tview(c.second);\n\t\tstd::cerr << \")\\n\";\n\t}\n\tstd::cerr << \"}\";\n}\n\ntemplate <class T0, class T1, class T2>\nvoid view(const std::vector<std::tuple<T0, T1, T2>> &v) {\n\tif(v.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << '{';\n\tfor(const auto &t : v) {\n\t\tstd::cerr << \"\\n\\t\";\n\t\tview(t);\n\t\tstd::cerr << \",\";\n\t}\n\tstd::cerr << \"\\n}\";\n}\n\ntemplate <class T0, class T1, class T2, class T3>\nvoid view(const std::vector<std::tuple<T0, T1, T2, T3>> &v) {\n\tif(v.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << '{';\n\tfor(const auto &t : v) {\n\t\tstd::cerr << \"\\n\\t\";\n\t\tview(t);\n\t\tstd::cerr << \",\";\n\t}\n\tstd::cerr << \"\\n}\";\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::map<T, U> &m) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &t : m) {\n\t\tstd::cerr << \"\\t[\";\n\t\tview(t.first);\n\t\tstd::cerr << \"] : \";\n\t\tview(t.second);\n\t\tstd::cerr << '\\n';\n\t}\n\tstd::cerr << \"}\";\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::unordered_map<T, U> &m) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &t : m) {\n\t\tstd::cerr << \"\\t[\";\n\t\tview(t.first);\n\t\tstd::cerr << \"] : \";\n\t\tview(t.second);\n\t\tstd::cerr << '\\n';\n\t}\n\tstd::cerr << \"}\";\n}\n} // namespace viewer\n\n#pragma endregion\n\n// when debugging : g++ foo.cpp -DLOCAL\n#ifdef LOCAL\nvoid debug_out() {}\ntemplate <typename Head, typename... Tail>\nvoid debug_out(Head H, Tail... T) {\n\tviewer::view(H);\n\tstd::cerr << \", \";\n\tdebug_out(T...);\n}\n#define debug(...) \\\n\tdo { \\\n\t\tstd::cerr << __LINE__ << \" [\" << #__VA_ARGS__ << \"] : [\"; \\\n\t\tdebug_out(__VA_ARGS__); \\\n\t\tstd::cerr << \"\\b\\b]\\n\"; \\\n\t} while(0)\n#define dump(x) \\\n\tdo { \\\n\t\tstd::cerr << __LINE__ << \" \" << #x << \" : \"; \\\n\t\tviewer::view(x); \\\n\t\tstd::cerr << '\\n'; \\\n\t} while(0)\n\n#else\n#define debug(...) (void(0))\n#define dump(x) (void(0))\n#endif\n\n#pragma endregion\n\nusing R = long double;\n\n#pragma region geometry_light\n\nusing Real = long double;\nusing R = Real;\nusing Point = std::complex<Real>;\nusing Vec = Point;\nconst Real EPS = 1e-10, PI = acos(-1);\n\ninline bool eq(const Real &a, const Real &b) { return fabs(b - a) < EPS; }\ninline bool eq(const Point &a, const Point &b) { return fabs(b - a) < EPS; }\n/*\n\t-1: a < b\n\t0 : a == b\n\t1 : a > b\n*/\ninline int compare(const Real &a, const Real &b) { return eq(a, b) ? 0 : a < b ? -1 : 1; }\ninline int sign(const Real &a) { return fabs(a) < EPS ? 0 : a < 0 ? -1 : 1; }\n\nnamespace std {\nconst Point operator*(const Point &p, const Real &d) { return Point(real(p) * d, imag(p) * d); }\n} // namespace std\n\nstd::istream &operator>>(std::istream &is, Point &p) {\n\tReal a, b;\n\tis >> a >> b;\n\tp = Point(a, b);\n\treturn is;\n}\nstd::ostream &operator<<(std::ostream &os, Point &p) { return os << std::fixed << std::setprecision(10) << p.real() << \" \" << p.imag(); }\n\n// rotate point 'p' for counter clockwise direction\nconst Point rotate(const Real &theta, const Point &p) {\n\treturn Point(cos(theta) * p.real() - sin(theta) * p.imag(), sin(theta) * p.real() + cos(theta) * p.imag());\n}\n\nconst Real radian_to_degree(const Real &r) { return (r * 180.0 / PI); }\n\nconst Real degree_to_radian(const Real &d) { return (d * PI / 180.0); }\n\n// get angle a-b-c (<pi)\nconst Real get_angle(const Point &a, const Point &b, const Point &c) {\n\tconst Point v(a - b), w(c - b);\n\tReal theta = fabs(atan2(w.imag(), w.real()) - atan2(v.imag(), v.real()));\n\treturn std::min(theta, 2 * PI - theta);\n}\n\nnamespace std {\nbool operator<(const Point &a, const Point &b) { return a.real() != b.real() ? a.real() < b.real() : a.imag() < b.imag(); }\n} // namespace std\n\nstruct Line {\n\tPoint a, b;\n\n\tLine() = default;\n\n\tLine(Point a, Point b) : a(a), b(b) {}\n\n\tLine(Real A, Real B, Real C) // Ax + By = C\n\t{\n\t\tif(eq(A, 0))\n\t\t\ta = Point(0, C / B), b = Point(1, C / B);\n\t\telse if(eq(B, 0))\n\t\t\tb = Point(C / A, 0), b = Point(C / A, 1);\n\t\telse\n\t\t\ta = Point(0, C / B), b = Point(C / A, 0);\n\t}\n\n\tfriend std::ostream &operator<<(std::ostream &os, Line &p) { return os << p.a << \" -- \" << p.b; }\n\n\tfriend std::istream &operator>>(std::istream &is, Line &a) { return is >> a.a >> a.b; }\n};\n\nstruct Segment : Line {\n\tSegment() = default;\n\n\tSegment(Point a, Point b) : Line(a, b) {}\n};\n\nstruct Circle {\n\tPoint p;\n\tReal r;\n\n\tCircle() = default;\n\n\tCircle(Point p, Real r) : p(p), r(r) {}\n};\n\nusing Points = std::vector<Point>;\nusing Polygon = std::vector<Point>;\nusing Segments = std::vector<Segment>;\nusing Lines = std::vector<Line>;\nusing Circles = std::vector<Circle>;\n\nconst Real cross(const Point &a, const Point &b) { return real(a) * imag(b) - imag(a) * real(b); }\nconst Real dot(const Point &a, const Point &b) { return real(a) * real(b) + imag(a) * imag(b); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_C\nconst int ccw(const Point &a, Point b, Point c) {\n\tb = b - a, c = c - a;\n\tif(cross(b, c) > EPS) return +1;\t\t// \"COUNTER_CLOCKWISE\"\n\tif(cross(b, c) < -EPS) return -1;\t\t// \"CLOCKWISE\"\n\tif(dot(b, c) < -EPS) return +2;\t\t\t// \"ONLINE_BACK\"\n\tif(norm(b) + EPS < norm(c)) return -2;\t// \"ONLINE_FRONT\"\n\treturn 0;\t\t\t\t\t\t\t\t// \"ON_SEGMENT\"\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_A\nbool parallel(const Line &a, const Line &b) { return eq(cross(a.b - a.a, b.b - b.a), 0.0); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_A\nbool orthogonal(const Line &a, const Line &b) { return eq(dot(a.a - a.b, b.a - b.b), 0.0); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_A\nPoint projection(const Line &l, const Point &p) {\n\tdouble t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n\treturn l.a + (l.a - l.b) * t;\n}\n\nPoint projection(const Segment &l, const Point &p) {\n\tdouble t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n\treturn l.a + (l.a - l.b) * t;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_B\nPoint reflection(const Line &l, const Point &p) { return projection(l, p) * 2.0 - p; }\n\nint intersect(const Line &l, const Point &p) { return int(abs(ccw(l.a, l.b, p)) != 1); }\n\nint intersect(const Line &l, const Line &m) {\n\tif(intersect(l, m.a) && intersect(l, m.b)) return -1;\n\treturn int(abs(cross(l.b - l.a, m.b - m.a)) > EPS);\n}\n\nint intersect(const Segment &s, const Point &p) { return int(ccw(s.a, s.b, p) == 0); }\n\nint intersect(const Line &l, const Segment &s) {\n\tif(intersect(l, s.a) && intersect(l, s.b)) return -1;\n\treturn cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < EPS;\n}\n\nReal distance(const Line &l, const Point &p);\n\nint intersect(const Circle &c, const Line &l) {\n\tReal d = c.r - distance(l, c.p);\n\treturn fabs(d) < EPS ? 1 : d > 0. ? 2 : 0;\n}\n\nint intersect(const Circle &c, const Point &p) { return int(abs(abs(p - c.p) - c.r) < EPS); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_B\nint intersect(const Segment &s, const Segment &t) {\n\tif(eq(s.a, s.b)) return intersect(t, s.a);\n\tif(intersect(Line(s), t.a) && intersect(Line(s), t.b) &&\n\t std::max(std::min(s.a, s.b), std::min(t.a, t.b)) < std::min(std::max(s.a, s.b), std::max(t.a, t.b)))\n\t\treturn -1;\n\treturn int(ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 && ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0);\n}\n\nint intersect(const Circle &c, const Segment &l) {\n\tconst Point h = projection(l, c.p);\n\tif(norm(h - c.p) - c.r * c.r > EPS) return 0;\n\tauto d1 = abs(c.p - l.a), d2 = abs(c.p - l.b);\n\tif(compare(d1, c.r) == -1 && compare(d2, c.r) == -1) return 0;\n\tif(d1 < c.r - EPS && d2 > c.r - EPS || d1 > c.r - EPS && d2 < c.r - EPS) return 1;\n\treturn dot(l.a - h, l.b - h) < 0 ? 2 : 0;\n}\n\nReal distance(const Point &a, const Point &b);\n\nint number_of_common_tangents(const Circle &c1, const Circle &c2) {\n\tReal r1 = std::min(c1.r, c2.r), r2 = std::max(c1.r, c2.r), d = distance(c1.p, c2.p);\n\tint com = compare(r1 + r2, d);\n\treturn com == 1 ? compare(d + r1, r2) + 1 : 3 - com;\n\t// if(c1.r < c2.r) swap(c1, c2);\n\t// Real d = abs(c1.p - c2.p);\n\t// if(compare(c1.r + c2.r, d) == -1) return 4;\n\t// if(eq(c1.r + c2.r, d)) return 3;\n\t// if(compare(c1.r - c2.r, d) == -1) return 2;\n\t// return int(eq(c1.r - c2.r, d));\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_A&lang=jp\nint intersect(const Circle &c1, const Circle &c2) { return 2 - abs(2 - number_of_common_tangents(c1, c2)); }\n\nReal distance(const Point &a, const Point &b) { return abs(a - b); }\n\nReal distance(const Line &l, const Point &p) { return abs(p - projection(l, p)); }\n\nReal distance(const Line &l, const Line &m) { return intersect(l, m) ? 0 : distance(l, m.a); }\n\nReal distance(const Segment &s, const Point &p) {\n\tPoint r = projection(s, p);\n\treturn intersect(s, r) ? distance(r, p) : std::min(abs(s.a - p), abs(s.b - p));\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_D\nReal distance(const Segment &a, const Segment &b) {\n\tif(intersect(a, b)) return 0;\n\treturn std::min({distance(a, b.a), distance(a, b.b), distance(b, a.a), distance(b, a.b)});\n}\n\nReal distance(const Line &l, const Segment &s) {\n\tif(intersect(l, s)) return 0;\n\treturn std::min(distance(l, s.a), distance(l, s.b));\n}\n\nPoint crosspoint(const Line &l, const Line &m) {\n\tReal A = cross(l.b - l.a, m.b - m.a);\n\tReal B = cross(l.b - l.a, l.b - m.a);\n\tif(eq(abs(A), 0.0) && eq(abs(B), 0.0)) return m.a;\n\treturn m.a + (m.b - m.a) * B / A;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_C\nPoint crosspoint(const Segment &l, const Segment &m) { return crosspoint(Line(l), Line(m)); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_D\nstd::pair<Point, Point> crosspoint(const Circle &c, const Line l) {\n\tPoint pr = projection(l, c.p);\n\tif(eq(distance(l, c.p), c.r)) return {pr, pr};\n\tVec v = (l.b - l.a) / abs(l.b - l.a) * sqrt(c.r * c.r - norm(pr - c.p));\n\treturn make_pair(pr - v, pr + v);\n\t// Vec e = (l.b - l.a) / abs(l.b - l.a);\n\t// double base = sqrt(c.r * c.r - norm(pr - c.p));\n\t// return {pr - e * base, pr + e * base};\n}\n\nstd::pair<Point, Point> crosspoint(const Circle &c, const Segment &l) {\n\tif(intersect(c, l) == 2) return crosspoint(c, Line(l.a, l.b));\n\tauto ret = crosspoint(c, Line(l.a, l.b));\n\tif(dot(l.a - ret.first, l.b - ret.first) < EPS)\n\t\tret.second = ret.first;\n\telse\n\t\tret.first = ret.second;\n\treturn ret;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_E\nstd::pair<Point, Point> crosspoint(const Circle &c1, const Circle &c2) {\n\tReal d = abs(c1.p - c2.p);\n\tReal a = acos((c1.r * c1.r + d * d - c2.r * c2.r) / (2 * c1.r * d)); // cosine theorem\n\tReal t = atan2(c2.p.imag() - c1.p.imag(), c2.p.real() - c1.p.real());\n\tPoint p1 = c1.p + Point(cos(t + a) * c1.r, sin(t + a) * c1.r);\n\tPoint p2 = c1.p + Point(cos(t - a) * c1.r, sin(t - a) * c1.r);\n\treturn make_pair(p1, p2);\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_F\n// \"点 p を通る円 c の接線\"の接点2つ\nstd::pair<Point, Point> tangent(const Circle &c1, const Point &p2) {\n\treturn crosspoint(c1, Circle(p2, sqrt(norm(c1.p - p2) - c1.r * c1.r)));\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_G\n// 円 c1, c2 の共通接線\nLines tangent(Circle c1, Circle c2) {\n\tLines ret;\n\tif(c1.r < c2.r) std::swap(c1, c2);\n\tReal g = norm(c1.p - c2.p);\n\tif(eq(g, 0)) return ret;\n\tVec u = (c2.p - c1.p) / Real(sqrt(g));\n\tVec v = rotate(PI * 0.5, u);\n\tfor(int s : {-1, 1}) {\n\t\tReal h = (c1.r + s * c2.r) / sqrt(g);\n\t\tif(eq(1 - h * h, 0)) {\n\t\t\tret.emplace_back(c1.p + u * c1.r, c1.p + (u + v) * c1.r);\n\t\t} else if(1 - h * h > 0) {\n\t\t\tPoint uu = u * h, vv = v * sqrt(1 - h * h);\n\t\t\tret.emplace_back(c1.p + (uu + vv) * c1.r, c2.p - (uu + vv) * c2.r * s);\n\t\t\tret.emplace_back(c1.p + (uu - vv) * c1.r, c2.p - (uu - vv) * c2.r * s);\n\t\t}\n\t}\n\treturn ret;\n}\n\n#pragma endregion\n\nbool solve(int n) {\n\tR velocity;\n\tcin >> velocity;\n\tR x_pig, y_pig;\n\tcin >> x_pig >> y_pig;\n\n\tVV<Segment> obstacle(n); // 0 ~ 2 yoko, 3~5 tate\n\n\tPoints pts;\n\n\trep(j, n) {\n\t\tR l, b, r, t;\n\t\tcin >> l >> b >> r >> t;\n\t\tpts.emplace_back(l, t);\n\t\tpts.emplace_back(l, b);\n\t\tpts.emplace_back(r, t);\n\t\tpts.emplace_back(r, b);\n\n\t\tR mid_x = (l + r) / 2;\n\t\tR mid_y = (b + t) / 2;\n\t\tPoints pts;\n\t\tfor(R x : {l, mid_x, r}) {\n\t\t\tfor(R y : {b, mid_y, t}) {\n\t\t\t\tpts.emplace_back(x, y);\n\t\t\t}\n\t\t}\n\t\trep(i, 3) { obstacle[j].emplace_back(pts[i], pts[i + 6]); }\n\t\trep(i, 3) obstacle[j].emplace_back(pts[i * 3], pts[i * 3 + 2]);\n\t}\n\n\tconst R g = 9.8;\n\n\tauto f = [&](R theta, R x) { return x * tan(theta) - g / 2 * pow(x / (velocity * cos(theta)), (R)2.); };\n\n\tauto calc_top = [&](R obj_x) -> R {\n\t\tR inf = 0;\n\t\tR sup = pi / 2 - EPS;\n\t\tR lef, rig;\n\t\trep(300) {\n\t\t\tlef = inf + (sup - inf) / 3;\n\t\t\trig = (lef + sup) / 2;\n\t\t\tif(f(lef, obj_x) < f(rig, obj_x)) {\n\t\t\t\tinf = lef;\n\t\t\t} else {\n\t\t\t\tsup = rig;\n\t\t\t}\n\t\t}\n\t\treturn lef;\n\t};\n\n\tauto binarysearch = [&](R lef, R rig, R obj_x, R obj_y) {\n\t\tR mid;\n\t\trep(300) {\n\t\t\tmid = lef + rig;\n\t\t\tmid /= 2;\n\t\t\tif((f(mid, obj_x) - obj_y) * (f(lef, obj_x) - obj_y) > 0) {\n\t\t\t\tlef = mid;\n\t\t\t} else {\n\t\t\t\trig = mid;\n\t\t\t}\n\t\t}\n\t\treturn mid;\n\t};\n\n\t// y = x tan theta - (g/2) (x/(v_0 cos theta))^2\n\tauto calc_theta = [&](R obj_x, R obj_y) -> pair<R, R> {\n\t\tR inf = 0;\n\t\tR sup = pi / 2 - EPS;\n\n\t\tR mid = calc_top(obj_x);\n\n\t\tif(compare(f(mid, obj_x), obj_y) == -1) {\n\t\t\treturn make_pair(-1., -1.);\n\t\t}\n\n\t\t// R a, b;\n\t\t// inf = 0;\n\t\t// rep(300) {\n\t\t// \ta = (inf + mid) / 2;\n\t\t// \tif(f(a, obj_x) < obj_y) {\n\t\t// \t\tinf = a;\n\t\t// \t} else {\n\t\t// \t\tmid = a;\n\t\t// \t}\n\t\t// }\n\n\t\t// mid = sup;\n\t\t// sup = pi / 2 - EPS;\n\n\t\t// rep(300) {\n\t\t// \tb = (mid + sup) / 2;\n\t\t// \tif(f(b, obj_x) < obj_y) {\n\t\t// \t\tsup = b;\n\t\t// \t} else {\n\t\t// \t\tmid = b;\n\t\t// \t}\n\t\t// }\n\n\t\treturn make_pair(binarysearch(inf, mid, obj_x, obj_y), binarysearch(mid, sup, obj_x, obj_y));\n\t};\n\n\tauto tp = [&](R theta) { return pow(velocity, 2.) * cos(theta) * sin(theta) / g; };\n\n\tauto collision = [&](R theta, int obs_idx) -> bool {\n\t\tR top = tp(theta);\n\t\trep(i, 3) {\n\t\t\tauto &seg = obstacle[obs_idx][i];\n\t\t\tR xl, xr, y;\n\t\t\ty = seg.a.imag();\n\t\t\txl = seg.a.real();\n\t\t\txr = seg.b.real();\n\t\t\tif(eq(theta, 0.899297831746404)) {\n\t\t\t\tdebug(y, xl, xr);\n\t\t\t}\n\t\t\tif(xl > xr) swap(xl, xr);\n\t\t\tif(x_pig <= xl) continue;\n\t\t\tchmin(xr, x_pig);\n\t\t\tR yl = f(theta, xl);\n\t\t\tR yr = f(theta, xr);\n\t\t\tif(xl <= top && top <= xr) {\n\t\t\t\t// if(f(theta, top) > y) {\n\t\t\t\tif(compare(f(theta, top), y) == 1) {\n\t\t\t\t\tif(compare(yl, y) == -1 || compare(yr, y) == -1) return true;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif((yl - y) * (yr - y) < -EPS) return true;\n\t\t}\n\n\t\tdebug(theta);\n\n\t\trep(i, 3, 6) {\n\t\t\tauto &seg = obstacle[obs_idx][i];\n\t\t\tR yd, x, yt;\n\t\t\tx = seg.a.real();\n\t\t\tyd = seg.a.imag();\n\t\t\tyt = seg.b.imag();\n\t\t\tif(eq(theta, 0.899297831746404)) {\n\t\t\t\tdebug(x, yd, yt);\n\t\t\t}\n\t\t\tif(x_pig < x) continue;\n\t\t\tif(yd > yt) swap(yd, yt);\n\t\t\tR y = f(theta, x);\n\t\t\tif(compare(yd, y) == -1 && compare(y, yt) == -1) return true;\n\t\t}\n\n\t\treturn false;\n\t};\n\n\tauto use = [&](R angle) { return EPS < angle && angle < pi / 2 - EPS * 2; };\n\n\tauto ok = [&](R angle) {\n\t\tif(!use(angle)) return false;\n\t\trep(i, n) if(collision(angle, i)) return false;\n\t\tR y = f(angle, x_pig);\n\t\tif(y < y_pig) return false;\n\t\tSegment egg(Point(x_pig, y), Point(x_pig, y_pig));\n\t\tfoa(ob, obstacle) {\n\t\t\tfoa(seg, ob) {\n\t\t\t\tif(intersect(seg, egg)) {\n\t\t\t\t\treturn false;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tdebug(angle);\n\t\tdebug(f(angle, 1));\n\t\tdebug(f(angle, 2));\n\t\tdebug(f(angle, 3));\n\t\treturn true;\n\t};\n\n\tauto res = calc_theta(1.2, 1.5);\n\tdebug(f(res.first, 1.2));\n\tdebug(f(res.second, 1.2));\n\n\tfoa(p, pts) {\n\t\tif(eq(p, 0.)) continue;\n\t\tauto angles = calc_theta(p.real(), p.imag());\n\t\tif(ok(angles.first) || ok(angles.second)) return true;\n\t}\n\n\tauto angles = calc_theta(x_pig, y_pig);\n\treturn (ok(angles.first) || ok(angles.second));\n}\n\nint main() {\n\tstd::ios::sync_with_stdio(false);\n\tstd::cin.tie(nullptr);\n\tstd::cout << std::fixed << std::setprecision(15);\n\t// std::cerr << std::fixed << std::setprecision(15);\n\tsrand((unsigned)time(NULL));\n\n\tint n;\n\twhile(cin >> n) {\n\t\tYes(solve(n));\n\t}\n\n\treturn 0;\n}", "accuracy": 0.8, "time_ms": 70, "memory_kb": 3940, "score_of_the_acc": -1.1215, "final_rank": 8 }, { "submission_id": "aoj_2308_5846497", "code_snippet": "/*\tauthor: Kite_kuma\n\tcreated: 2021.09.01 15:05:47 */\n\n// #ifdef LOCAL\n// #define _GLIBCXX_DEBUG\n// #endif\n#include <bits/stdc++.h>\nusing namespace std;\n\n#pragma region macros\n\n#define foa(s, v) for(auto &s : v)\n#define all(v) (v).begin(), (v).end()\n#define rall(v) (v).rbegin(), (v).rend()\n#define popcnt(n) __builtin_popcountll((long long)n)\n\n#define REPname(a, b, c, d, e, ...) e\n#define rep(...) REPname(__VA_ARGS__, REP3, REP2, REP1, REP0)(__VA_ARGS__)\n#define REP0(x) for(int Counter_in_rep_macro = 0; Counter_in_rep_macro < (x); ++Counter_in_rep_macro)\n#define REP1(i, x) for(int i = 0; i < (x); ++i)\n#define REP2(i, l, r) for(int i = (l); i < (r); ++i)\n#define REP3(i, l, r, c) for(int i = (l); i < (r); i += (c))\n\n#define DREPname(a, b, c, d, e, ...) e\n#define drep(...) DREPname(__VA_ARGS__, DREP3, DREP2, DREP1)(__VA_ARGS__)\n#define DREP1(i, x) for(int i = (x)-1; i >= 0; --i)\n#define DREP2(i, l, r) for(int i = (r)-1; i >= (l); --i)\n#define DREP3(i, l, r, c) for(int i = (r)-1; i >= (l); i -= (c))\n\n#pragma endregion\n\n#pragma region aliases\n\nusing ll = long long;\nusing ld = long double;\nusing ull = unsigned long long;\nusing vi = std::vector<int>;\nusing vvi = std::vector<std::vector<int>>;\nusing vvvi = std::vector<std::vector<std::vector<int>>>;\nusing vll = std::vector<ll>;\nusing vvll = std::vector<vll>;\nusing vvvll = std::vector<vvll>;\nusing pii = std::pair<int, int>;\nusing pll = std::pair<long long, long long>;\ntemplate <class T = ll>\nusing V = std::vector<T>;\ntemplate <class T = ll>\nusing VV = V<V<T>>;\ntemplate <class T = ll>\nusing VVV = V<V<V<T>>>;\ntemplate <class T = ll>\nusing pqup = std::priority_queue<T, std::vector<T>, std::greater<T>>;\ntemplate <class T = ll>\nusing pqdn = std::priority_queue<T>;\n\n#pragma endregion\n\n#pragma region constants\n\nconst int inf = 1e9;\nconst long long INF = 1e18;\nconst long double pi = acos(-1);\nconst char dl = '\\n';\nconst char sp = ' ';\nint dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};\nint dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};\n\nconst int mod_1000000007 = 1000000007;\nconst int mod_998244353 = 998244353;\n\n#pragma endregion\n\n#pragma region basic_operation\n\ntemplate <class T0, class T1, class T2>\ninline bool in_range(T0 x, T1 lef, T2 rig) {\n\treturn ((lef <= x) && (x < rig));\n}\n\ntemplate <class T>\ninline bool chmin(T &a, T b) {\n\tif(a > b) {\n\t\ta = b;\n\t\treturn true;\n\t}\n\treturn false;\n}\ntemplate <class T>\ninline bool chmax(T &a, T b) {\n\tif(a < b) {\n\t\ta = b;\n\t\treturn true;\n\t}\n\treturn false;\n}\n\nvoid Yes(bool f = 1) { std::cout << (f ? \"Yes\" : \"No\") << '\\n'; }\nvoid No() { std::cout << \"No\\n\"; }\nvoid YES(bool f = 1) { std::cout << (f ? \"YES\" : \"NO\") << '\\n'; }\nvoid NO() { std::cout << \"NO\\n\"; }\n\ntemplate <class T>\nvoid drop(T answer) {\n\tstd::cout << answer << '\\n';\n\texit(0);\n}\n\nvoid err(bool flag = true) {\n\tif(!flag) return;\n\tstd::cout << -1 << '\\n';\n\texit(0);\n}\n\ntemplate <class T>\nvoid vout(std::vector<T> const &v, bool vertically = 0) {\n\tif(vertically) {\n\t\tfor(auto const &a : v) {\n\t\t\tstd::cout << a << '\\n';\n\t\t}\n\t\treturn;\n\t}\n\tfor(auto it = v.begin(); it != v.end(); it++) {\n\t\tstd::cout << (*it);\n\t\tif(it != v.end() - 1) {\n\t\t\tstd::cout << ' ';\n\t\t}\n\t}\n\tstd::cout << '\\n';\n\treturn;\n}\n\ninline void print() { std::cout << '\\n'; }\ntemplate <class T>\ninline void print(T x) {\n\tstd::cout << x << '\\n';\n\treturn;\n}\ntemplate <typename Head, typename... Tail>\nvoid print(Head H, Tail... T) {\n\tstd::cout << H << \" \";\n\tprint(T...);\n}\n\ntemplate <class T>\nvoid add(std::vector<T> &v, T val) {\n\tfor(auto &a : v) a += val;\n\treturn;\n}\n\ntemplate <class T>\nT dup(T a, T b) {\n\tassert(b != 0);\n\treturn (a + b - 1) / b;\n}\n\ntemplate <class T>\nT greatest_lower_multiple(T x, T d) {\n\tif(d == 0) return 0;\n\tif(d < 0) d *= -1;\n\tT y = x % d;\n\tif(y < 0) y += d;\n\treturn x - y;\n}\n\ntemplate <class T>\nT least_upper_multiple(T x, T d) {\n\treturn -greatest_lower_multiple(-x, d);\n}\n\nlong long POW(long long a, long long n) {\n\tlong long res = 1;\n\twhile(n > 0) {\n\t\tif(n & 1) res = res * a;\n\t\ta = a * a;\n\t\tn >>= 1;\n\t}\n\treturn res;\n}\n\nlong long modpow(long long a, long long n, long long mod) {\t // a^n mod\n\tassert(n >= 0 && mod);\n\tif(mod == 1) return 0LL;\n\tlong long res = 1;\n\ta %= mod;\n\twhile(n > 0) {\n\t\tif(n & 1) res = res * a % mod;\n\t\ta = a * a % mod;\n\t\tn >>= 1;\n\t}\n\treturn res < 0 ? res + mod : res;\n}\n\n// return x which satisfies a * x % mod == gcd(a, mod)\n// not (mod | a)\nlong long modinv(long long a, long long mod) {\n\tlong long b = mod, u = 1, v = 0;\n\twhile(b) {\n\t\tlong long t = a / b;\n\t\ta -= t * b;\n\t\tstd::swap(a, b);\n\t\tu -= t * v;\n\t\tstd::swap(u, v);\n\t}\n\tu %= mod;\n\tif(u < 0) u += mod;\n\treturn u;\n}\n\ntemplate <class T>\nint lb(const std::vector<T> &a, const T x) {\n\treturn std::distance(a.begin(), std::lower_bound(a.begin(), a.end(), x));\n}\ntemplate <class T>\nint ub(const std::vector<T> &a, const T x) {\n\treturn std::distance(a.begin(), std::upper_bound(a.begin(), a.end(), x));\n}\ntemplate <class T>\nvoid unq_sort(std::vector<T> &a) {\n\tstd::sort(a.begin(), a.end());\n\ta.erase(std::unique(a.begin(), a.end()), a.end());\n}\ntemplate <class T>\nstd::vector<int> press(std::vector<T> &a) {\n\tauto vec = a;\n\tunq_sort(vec);\n\tstd::vector<int> ret;\n\tfor(auto &v : a) ret.push_back(lb(vec, v));\n\treturn ret;\n}\n\n#pragma endregion\n\n#pragma region input\n#define VEC(type, name, size) \\\n\tvector<type> name(size); \\\n\tscanner::INPUT(name)\n#define VVEC(type, name, h, w) \\\n\tvector<vector<type>> name(h, vector<type>(w)); \\\n\tscanner::INPUT(name)\n#define INT(...) \\\n\tint __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define LL(...) \\\n\tlong long __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define STR(...) \\\n\tstring __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define CHR(...) \\\n\tchar __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define DBL(...) \\\n\tdouble __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define LD(...) \\\n\tlong double __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define TPL3(type0, type1, type2, name) \\\n\tstd::tuple<type0, type1, type2> name; \\\n\tscanner::INPUT(name);\n#define TPL4(type0, type1, type2, type3, name) \\\n\tstd::tuple<type0, type1, type2, type3> name; \\\n\tscanner::INPUT(name);\n\nnamespace scanner {\ntemplate <class T>\nvoid scan(T &a) {\n\tstd::cin >> a;\n}\n\ntemplate <class T, class L>\nvoid scan(std::pair<T, L> &p) {\n\tscan(p.first);\n\tscan(p.second);\n}\n\ntemplate <class T0, class T1, class T2>\nvoid scan(std::tuple<T0, T1, T2> &p) {\n\tT0 t0;\n\tT1 t1;\n\tT2 t2;\n\tscan(t0);\n\tscan(t1);\n\tscan(t2);\n\tp = std::make_tuple(t0, t1, t2);\n}\n\ntemplate <class T0, class T1, class T2, class T3>\nvoid scan(std::tuple<T0, T1, T2, T3> &p) {\n\tT0 t0;\n\tT1 t1;\n\tT2 t2;\n\tT3 t3;\n\tscan(t0);\n\tscan(t1);\n\tscan(t2);\n\tscan(t3);\n\tp = std::make_tuple(t0, t1, t2, t3);\n}\n\ntemplate <class T>\nvoid scan(std::vector<T> &a) {\n\tfor(auto &i : a) scan(i);\n}\n\nvoid INPUT() {}\ntemplate <class Head, class... Tail>\nvoid INPUT(Head &head, Tail &... tail) {\n\tscan(head);\n\tINPUT(tail...);\n}\n} // namespace scanner\n\ntemplate <typename T1, typename T2>\nstd::istream &operator>>(std::istream &is, std::pair<T1, T2> &p) {\n\tis >> p.first >> p.second;\n\treturn is;\n}\n#pragma endregion\n\n#pragma region debug\n\n#pragma region output\ntemplate <typename T1, typename T2>\nstd::ostream &std::operator<<(std::ostream &os, const std::pair<T1, T2> &p) {\n\tos << p.first << \" \" << p.second;\n\treturn os;\n}\ntemplate <class T>\nstd::ostream &std::operator<<(std::ostream &os, const std::vector<T> &v) {\n\tfor(int i = 0; i < (int)v.size(); i++) {\n\t\tif(i) os << \" \";\n\t\tos << v[i];\n\t}\n\treturn os;\n}\n#pragma endregion\n\n#pragma region view\n\nnamespace viewer {\nvoid view(const long long e) {\n\tif(e == INF)\n\t\tstd::cerr << \"INF\";\n\telse if(e == -INF)\n\t\tstd::cerr << \"-INF\";\n\telse\n\t\tstd::cerr << e;\n}\n\nvoid view(const int e) {\n\tif(e == inf)\n\t\tstd::cerr << \"inf\";\n\telse if(e == -inf)\n\t\tstd::cerr << \"-inf\";\n\telse\n\t\tstd::cerr << e;\n}\n\ntemplate <typename T>\nvoid view(const T e) {\n\tstd::cerr << e;\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::pair<T, U> &p) {\n\tstd::cerr << \"(\";\n\tview(p.first);\n\tstd::cerr << \", \";\n\tview(p.second);\n\tstd::cerr << \")\";\n}\n\ntemplate <class T0, class T1, class T2>\nvoid view(const std::tuple<T0, T1, T2> &p) {\n\tstd::cerr << \"(\";\n\tview(std::get<0>(p));\n\tstd::cerr << \", \";\n\tview(std::get<1>(p));\n\tstd::cerr << \", \";\n\tview(std::get<2>(p));\n\tstd::cerr << \")\";\n}\n\ntemplate <class T0, class T1, class T2, class T3>\nvoid view(const std::tuple<T0, T1, T2, T3> &p) {\n\tstd::cerr << \"(\";\n\tview(std::get<0>(p));\n\tstd::cerr << \", \";\n\tview(std::get<1>(p));\n\tstd::cerr << \", \";\n\tview(std::get<2>(p));\n\tstd::cerr << \", \";\n\tview(std::get<3>(p));\n\tstd::cerr << \")\";\n}\n\ntemplate <typename T>\nvoid view(const std::set<T> &s) {\n\tif(s.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << \"{ \";\n\tfor(auto &t : s) {\n\t\tview(t);\n\t\tstd::cerr << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\n\ntemplate <typename T>\nvoid view(const std::unordered_set<T> &s) {\n\tif(s.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << \"{ \";\n\tfor(auto &t : s) {\n\t\tview(t);\n\t\tstd::cerr << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\n\ntemplate <typename T>\nvoid view(const std::vector<T> &v) {\n\tif(v.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << \"{ \";\n\tfor(const auto &e : v) {\n\t\tview(e);\n\t\tstd::cerr << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\n\ntemplate <typename T>\nvoid view(const std::vector<std::vector<T>> &vv) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &v : vv) {\n\t\tstd::cerr << \"\\t\";\n\t\tview(v);\n\t\tstd::cerr << '\\n';\n\t}\n\tstd::cerr << \"}\";\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::vector<std::pair<T, U>> &v) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &c : v) {\n\t\tstd::cerr << \"\\t(\";\n\t\tview(c.first);\n\t\tstd::cerr << \", \";\n\t\tview(c.second);\n\t\tstd::cerr << \")\\n\";\n\t}\n\tstd::cerr << \"}\";\n}\n\ntemplate <class T0, class T1, class T2>\nvoid view(const std::vector<std::tuple<T0, T1, T2>> &v) {\n\tif(v.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << '{';\n\tfor(const auto &t : v) {\n\t\tstd::cerr << \"\\n\\t\";\n\t\tview(t);\n\t\tstd::cerr << \",\";\n\t}\n\tstd::cerr << \"\\n}\";\n}\n\ntemplate <class T0, class T1, class T2, class T3>\nvoid view(const std::vector<std::tuple<T0, T1, T2, T3>> &v) {\n\tif(v.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << '{';\n\tfor(const auto &t : v) {\n\t\tstd::cerr << \"\\n\\t\";\n\t\tview(t);\n\t\tstd::cerr << \",\";\n\t}\n\tstd::cerr << \"\\n}\";\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::map<T, U> &m) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &t : m) {\n\t\tstd::cerr << \"\\t[\";\n\t\tview(t.first);\n\t\tstd::cerr << \"] : \";\n\t\tview(t.second);\n\t\tstd::cerr << '\\n';\n\t}\n\tstd::cerr << \"}\";\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::unordered_map<T, U> &m) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &t : m) {\n\t\tstd::cerr << \"\\t[\";\n\t\tview(t.first);\n\t\tstd::cerr << \"] : \";\n\t\tview(t.second);\n\t\tstd::cerr << '\\n';\n\t}\n\tstd::cerr << \"}\";\n}\n} // namespace viewer\n\n#pragma endregion\n\n// when debugging : g++ foo.cpp -DLOCAL\n#ifdef LOCAL\nvoid debug_out() {}\ntemplate <typename Head, typename... Tail>\nvoid debug_out(Head H, Tail... T) {\n\tviewer::view(H);\n\tstd::cerr << \", \";\n\tdebug_out(T...);\n}\n#define debug(...) \\\n\tdo { \\\n\t\tstd::cerr << __LINE__ << \" [\" << #__VA_ARGS__ << \"] : [\"; \\\n\t\tdebug_out(__VA_ARGS__); \\\n\t\tstd::cerr << \"\\b\\b]\\n\"; \\\n\t} while(0)\n#define dump(x) \\\n\tdo { \\\n\t\tstd::cerr << __LINE__ << \" \" << #x << \" : \"; \\\n\t\tviewer::view(x); \\\n\t\tstd::cerr << '\\n'; \\\n\t} while(0)\n\n#else\n#define debug(...) (void(0))\n#define dump(x) (void(0))\n#endif\n\n#pragma endregion\n\nusing R = long double;\n\n#pragma region geometry_light\n\nusing Real = long double;\nusing R = Real;\nusing Point = std::complex<Real>;\nusing Vec = Point;\nconst Real EPS = 1e-10, PI = acos(-1);\n\ninline bool eq(const Real &a, const Real &b) { return fabs(b - a) < EPS; }\ninline bool eq(const Point &a, const Point &b) { return fabs(b - a) < EPS; }\n/*\n\t-1: a < b\n\t0 : a == b\n\t1 : a > b\n*/\ninline int compare(const Real &a, const Real &b) { return eq(a, b) ? 0 : a < b ? -1 : 1; }\ninline int sign(const Real &a) { return fabs(a) < EPS ? 0 : a < 0 ? -1 : 1; }\n\nnamespace std {\nconst Point operator*(const Point &p, const Real &d) { return Point(real(p) * d, imag(p) * d); }\n} // namespace std\n\nstd::istream &operator>>(std::istream &is, Point &p) {\n\tReal a, b;\n\tis >> a >> b;\n\tp = Point(a, b);\n\treturn is;\n}\nstd::ostream &operator<<(std::ostream &os, Point &p) { return os << std::fixed << std::setprecision(10) << p.real() << \" \" << p.imag(); }\n\n// rotate point 'p' for counter clockwise direction\nconst Point rotate(const Real &theta, const Point &p) {\n\treturn Point(cos(theta) * p.real() - sin(theta) * p.imag(), sin(theta) * p.real() + cos(theta) * p.imag());\n}\n\nconst Real radian_to_degree(const Real &r) { return (r * 180.0 / PI); }\n\nconst Real degree_to_radian(const Real &d) { return (d * PI / 180.0); }\n\n// get angle a-b-c (<pi)\nconst Real get_angle(const Point &a, const Point &b, const Point &c) {\n\tconst Point v(a - b), w(c - b);\n\tReal theta = fabs(atan2(w.imag(), w.real()) - atan2(v.imag(), v.real()));\n\treturn std::min(theta, 2 * PI - theta);\n}\n\nnamespace std {\nbool operator<(const Point &a, const Point &b) { return a.real() != b.real() ? a.real() < b.real() : a.imag() < b.imag(); }\n} // namespace std\n\nstruct Line {\n\tPoint a, b;\n\n\tLine() = default;\n\n\tLine(Point a, Point b) : a(a), b(b) {}\n\n\tLine(Real A, Real B, Real C) // Ax + By = C\n\t{\n\t\tif(eq(A, 0))\n\t\t\ta = Point(0, C / B), b = Point(1, C / B);\n\t\telse if(eq(B, 0))\n\t\t\tb = Point(C / A, 0), b = Point(C / A, 1);\n\t\telse\n\t\t\ta = Point(0, C / B), b = Point(C / A, 0);\n\t}\n\n\tfriend std::ostream &operator<<(std::ostream &os, Line &p) { return os << p.a << \" -- \" << p.b; }\n\n\tfriend std::istream &operator>>(std::istream &is, Line &a) { return is >> a.a >> a.b; }\n};\n\nstruct Segment : Line {\n\tSegment() = default;\n\n\tSegment(Point a, Point b) : Line(a, b) {}\n};\n\nstruct Circle {\n\tPoint p;\n\tReal r;\n\n\tCircle() = default;\n\n\tCircle(Point p, Real r) : p(p), r(r) {}\n};\n\nusing Points = std::vector<Point>;\nusing Polygon = std::vector<Point>;\nusing Segments = std::vector<Segment>;\nusing Lines = std::vector<Line>;\nusing Circles = std::vector<Circle>;\n\nconst Real cross(const Point &a, const Point &b) { return real(a) * imag(b) - imag(a) * real(b); }\nconst Real dot(const Point &a, const Point &b) { return real(a) * real(b) + imag(a) * imag(b); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_C\nconst int ccw(const Point &a, Point b, Point c) {\n\tb = b - a, c = c - a;\n\tif(cross(b, c) > EPS) return +1;\t\t// \"COUNTER_CLOCKWISE\"\n\tif(cross(b, c) < -EPS) return -1;\t\t// \"CLOCKWISE\"\n\tif(dot(b, c) < -EPS) return +2;\t\t\t// \"ONLINE_BACK\"\n\tif(norm(b) + EPS < norm(c)) return -2;\t// \"ONLINE_FRONT\"\n\treturn 0;\t\t\t\t\t\t\t\t// \"ON_SEGMENT\"\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_A\nbool parallel(const Line &a, const Line &b) { return eq(cross(a.b - a.a, b.b - b.a), 0.0); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_A\nbool orthogonal(const Line &a, const Line &b) { return eq(dot(a.a - a.b, b.a - b.b), 0.0); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_A\nPoint projection(const Line &l, const Point &p) {\n\tdouble t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n\treturn l.a + (l.a - l.b) * t;\n}\n\nPoint projection(const Segment &l, const Point &p) {\n\tdouble t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n\treturn l.a + (l.a - l.b) * t;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_B\nPoint reflection(const Line &l, const Point &p) { return projection(l, p) * 2.0 - p; }\n\nint intersect(const Line &l, const Point &p) { return int(abs(ccw(l.a, l.b, p)) != 1); }\n\nint intersect(const Line &l, const Line &m) {\n\tif(intersect(l, m.a) && intersect(l, m.b)) return -1;\n\treturn int(abs(cross(l.b - l.a, m.b - m.a)) > EPS);\n}\n\nint intersect(const Segment &s, const Point &p) { return int(ccw(s.a, s.b, p) == 0); }\n\nint intersect(const Line &l, const Segment &s) {\n\tif(intersect(l, s.a) && intersect(l, s.b)) return -1;\n\treturn cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < EPS;\n}\n\nReal distance(const Line &l, const Point &p);\n\nint intersect(const Circle &c, const Line &l) {\n\tReal d = c.r - distance(l, c.p);\n\treturn fabs(d) < EPS ? 1 : d > 0. ? 2 : 0;\n}\n\nint intersect(const Circle &c, const Point &p) { return int(abs(abs(p - c.p) - c.r) < EPS); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_B\nint intersect(const Segment &s, const Segment &t) {\n\tif(eq(s.a, s.b)) return intersect(t, s.a);\n\tif(intersect(Line(s), t.a) && intersect(Line(s), t.b) &&\n\t std::max(std::min(s.a, s.b), std::min(t.a, t.b)) < std::min(std::max(s.a, s.b), std::max(t.a, t.b)))\n\t\treturn -1;\n\treturn int(ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 && ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0);\n}\n\nint intersect(const Circle &c, const Segment &l) {\n\tconst Point h = projection(l, c.p);\n\tif(norm(h - c.p) - c.r * c.r > EPS) return 0;\n\tauto d1 = abs(c.p - l.a), d2 = abs(c.p - l.b);\n\tif(compare(d1, c.r) == -1 && compare(d2, c.r) == -1) return 0;\n\tif(d1 < c.r - EPS && d2 > c.r - EPS || d1 > c.r - EPS && d2 < c.r - EPS) return 1;\n\treturn dot(l.a - h, l.b - h) < 0 ? 2 : 0;\n}\n\nReal distance(const Point &a, const Point &b);\n\nint number_of_common_tangents(const Circle &c1, const Circle &c2) {\n\tReal r1 = std::min(c1.r, c2.r), r2 = std::max(c1.r, c2.r), d = distance(c1.p, c2.p);\n\tint com = compare(r1 + r2, d);\n\treturn com == 1 ? compare(d + r1, r2) + 1 : 3 - com;\n\t// if(c1.r < c2.r) swap(c1, c2);\n\t// Real d = abs(c1.p - c2.p);\n\t// if(compare(c1.r + c2.r, d) == -1) return 4;\n\t// if(eq(c1.r + c2.r, d)) return 3;\n\t// if(compare(c1.r - c2.r, d) == -1) return 2;\n\t// return int(eq(c1.r - c2.r, d));\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_A&lang=jp\nint intersect(const Circle &c1, const Circle &c2) { return 2 - abs(2 - number_of_common_tangents(c1, c2)); }\n\nReal distance(const Point &a, const Point &b) { return abs(a - b); }\n\nReal distance(const Line &l, const Point &p) { return abs(p - projection(l, p)); }\n\nReal distance(const Line &l, const Line &m) { return intersect(l, m) ? 0 : distance(l, m.a); }\n\nReal distance(const Segment &s, const Point &p) {\n\tPoint r = projection(s, p);\n\treturn intersect(s, r) ? distance(r, p) : std::min(abs(s.a - p), abs(s.b - p));\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_D\nReal distance(const Segment &a, const Segment &b) {\n\tif(intersect(a, b)) return 0;\n\treturn std::min({distance(a, b.a), distance(a, b.b), distance(b, a.a), distance(b, a.b)});\n}\n\nReal distance(const Line &l, const Segment &s) {\n\tif(intersect(l, s)) return 0;\n\treturn std::min(distance(l, s.a), distance(l, s.b));\n}\n\nPoint crosspoint(const Line &l, const Line &m) {\n\tReal A = cross(l.b - l.a, m.b - m.a);\n\tReal B = cross(l.b - l.a, l.b - m.a);\n\tif(eq(abs(A), 0.0) && eq(abs(B), 0.0)) return m.a;\n\treturn m.a + (m.b - m.a) * B / A;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_C\nPoint crosspoint(const Segment &l, const Segment &m) { return crosspoint(Line(l), Line(m)); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_D\nstd::pair<Point, Point> crosspoint(const Circle &c, const Line l) {\n\tPoint pr = projection(l, c.p);\n\tif(eq(distance(l, c.p), c.r)) return {pr, pr};\n\tVec v = (l.b - l.a) / abs(l.b - l.a) * sqrt(c.r * c.r - norm(pr - c.p));\n\treturn make_pair(pr - v, pr + v);\n\t// Vec e = (l.b - l.a) / abs(l.b - l.a);\n\t// double base = sqrt(c.r * c.r - norm(pr - c.p));\n\t// return {pr - e * base, pr + e * base};\n}\n\nstd::pair<Point, Point> crosspoint(const Circle &c, const Segment &l) {\n\tif(intersect(c, l) == 2) return crosspoint(c, Line(l.a, l.b));\n\tauto ret = crosspoint(c, Line(l.a, l.b));\n\tif(dot(l.a - ret.first, l.b - ret.first) < EPS)\n\t\tret.second = ret.first;\n\telse\n\t\tret.first = ret.second;\n\treturn ret;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_E\nstd::pair<Point, Point> crosspoint(const Circle &c1, const Circle &c2) {\n\tReal d = abs(c1.p - c2.p);\n\tReal a = acos((c1.r * c1.r + d * d - c2.r * c2.r) / (2 * c1.r * d)); // cosine theorem\n\tReal t = atan2(c2.p.imag() - c1.p.imag(), c2.p.real() - c1.p.real());\n\tPoint p1 = c1.p + Point(cos(t + a) * c1.r, sin(t + a) * c1.r);\n\tPoint p2 = c1.p + Point(cos(t - a) * c1.r, sin(t - a) * c1.r);\n\treturn make_pair(p1, p2);\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_F\n// \"点 p を通る円 c の接線\"の接点2つ\nstd::pair<Point, Point> tangent(const Circle &c1, const Point &p2) {\n\treturn crosspoint(c1, Circle(p2, sqrt(norm(c1.p - p2) - c1.r * c1.r)));\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_G\n// 円 c1, c2 の共通接線\nLines tangent(Circle c1, Circle c2) {\n\tLines ret;\n\tif(c1.r < c2.r) std::swap(c1, c2);\n\tReal g = norm(c1.p - c2.p);\n\tif(eq(g, 0)) return ret;\n\tVec u = (c2.p - c1.p) / Real(sqrt(g));\n\tVec v = rotate(PI * 0.5, u);\n\tfor(int s : {-1, 1}) {\n\t\tReal h = (c1.r + s * c2.r) / sqrt(g);\n\t\tif(eq(1 - h * h, 0)) {\n\t\t\tret.emplace_back(c1.p + u * c1.r, c1.p + (u + v) * c1.r);\n\t\t} else if(1 - h * h > 0) {\n\t\t\tPoint uu = u * h, vv = v * sqrt(1 - h * h);\n\t\t\tret.emplace_back(c1.p + (uu + vv) * c1.r, c2.p - (uu + vv) * c2.r * s);\n\t\t\tret.emplace_back(c1.p + (uu - vv) * c1.r, c2.p - (uu - vv) * c2.r * s);\n\t\t}\n\t}\n\treturn ret;\n}\n\n#pragma endregion\n\nbool solve(int n) {\n\tR velocity;\n\tcin >> velocity;\n\tR x_pig, y_pig;\n\tcin >> x_pig >> y_pig;\n\n\tVV<Segment> obstacle(n); // 0 ~ 2 yoko, 3~5 tate\n\n\tPoints pts;\n\n\trep(j, n) {\n\t\tR l, b, r, t;\n\t\tcin >> l >> b >> r >> t;\n\t\tpts.emplace_back(l, t);\n\t\tpts.emplace_back(l, b);\n\t\tpts.emplace_back(r, t);\n\t\tpts.emplace_back(r, b);\n\n\t\tR mid_x = (l + r) / 2;\n\t\tR mid_y = (b + t) / 2;\n\t\tPoints pts;\n\t\tfor(R x : {l, mid_x, r}) {\n\t\t\tfor(R y : {b, mid_y, t}) {\n\t\t\t\tpts.emplace_back(x, y);\n\t\t\t}\n\t\t}\n\t\trep(i, 3) { obstacle[j].emplace_back(pts[i], pts[i + 6]); }\n\t\trep(i, 3) obstacle[j].emplace_back(pts[i * 3], pts[i * 3 + 2]);\n\t}\n\n\tconst R g = 9.8;\n\n\tauto f = [&](R theta, R x) { return x * tan(theta) - g / 2 * pow(x / (velocity * cos(theta)), (R)2.); };\n\n\t// y = x tan theta - (g/2) (x/(v_0 cos theta))^2\n\tauto calc_theta = [&](R obj_x, R obj_y) -> pair<R, R> {\n\t\tR inf = 0;\n\t\tR sup = pi / 2 - EPS;\n\n\t\trep(300) {\n\t\t\tR lef = inf + (sup - inf) / 3;\n\t\t\tR rig = (lef + sup) / 2;\n\t\t\tif(f(lef, obj_x) < f(rig, obj_x)) {\n\t\t\t\tinf = lef;\n\t\t\t} else {\n\t\t\t\tsup = rig;\n\t\t\t}\n\t\t}\n\n\t\tif(compare(f(sup, obj_x), obj_y) == -1) {\n\t\t\treturn make_pair(-1., -1.);\n\t\t}\n\n\t\tR mid = sup;\n\t\tR a, b;\n\t\tinf = 0;\n\t\trep(300) {\n\t\t\ta = (inf + mid) / 2;\n\t\t\tif(f(a, obj_x) < obj_y) {\n\t\t\t\tinf = a;\n\t\t\t} else {\n\t\t\t\tmid = a;\n\t\t\t}\n\t\t}\n\n\t\tmid = sup;\n\t\tsup = pi / 2 - EPS;\n\n\t\trep(300) {\n\t\t\tb = (mid + sup) / 2;\n\t\t\tif(f(b, obj_x) < obj_x) {\n\t\t\t\tsup = b;\n\t\t\t} else {\n\t\t\t\tmid = b;\n\t\t\t}\n\t\t}\n\n\t\treturn pair(a, b);\n\t};\n\n\tauto tp = [&](R theta) { return pow(velocity, 2.) * cos(theta) * sin(theta) / g; };\n\n\tauto collision = [&](R theta, int obs_idx) -> bool {\n\t\tR top = tp(theta);\n\t\trep(i, 3) {\n\t\t\tauto &seg = obstacle[obs_idx][i];\n\t\t\tR xl, xr, y;\n\t\t\ty = seg.a.imag();\n\t\t\txl = seg.a.real();\n\t\t\txr = seg.b.real();\n\t\t\tif(eq(theta, 0.899297831746404)) {\n\t\t\t\tdebug(y, xl, xr);\n\t\t\t}\n\t\t\tif(xl > xr) swap(xl, xr);\n\t\t\tif(x_pig <= xl) continue;\n\t\t\tchmin(xr, x_pig);\n\t\t\tR yl = f(theta, xl);\n\t\t\tR yr = f(theta, xr);\n\t\t\tif(xl <= top && top <= xr) {\n\t\t\t\t// if(f(theta, top) > y) {\n\t\t\t\tif(compare(f(theta, top), y) == 1) {\n\t\t\t\t\tif(compare(yl, y) == -1 || compare(yr, y) == -1) return true;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif((yl - y) * (yr - y) < -EPS) return true;\n\t\t}\n\n\t\tdebug(theta);\n\n\t\trep(i, 3, 6) {\n\t\t\tauto &seg = obstacle[obs_idx][i];\n\t\t\tR yd, x, yt;\n\t\t\tx = seg.a.real();\n\t\t\tyd = seg.a.imag();\n\t\t\tyt = seg.b.imag();\n\t\t\tif(eq(theta, 0.899297831746404)) {\n\t\t\t\tdebug(x, yd, yt);\n\t\t\t}\n\t\t\tif(x > x_pig) continue;\n\t\t\tif(yd > yt) swap(yd, yt);\n\t\t\tR y = f(theta, x);\n\t\t\tif(compare(yd, y) == -1 && compare(y, yt) == -1) return true;\n\t\t}\n\n\t\treturn false;\n\t};\n\n\tauto use = [&](R angle) { return EPS < angle && angle < pi / 2 - EPS * 2; };\n\n\tauto ok = [&](R angle) {\n\t\tif(!use(angle)) return false;\n\t\trep(i, n) if(collision(angle, i)) return false;\n\t\tR y = f(angle, x_pig);\n\t\tif(y < y_pig) return false;\n\t\tSegment egg(Point(x_pig, y), Point(x_pig, y_pig));\n\t\tfoa(ob, obstacle) {\n\t\t\tfoa(seg, ob) {\n\t\t\t\tif(intersect(seg, egg)) {\n\t\t\t\t\treturn false;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tdebug(angle);\n\t\tdebug(f(angle, 1));\n\t\tdebug(f(angle, 2));\n\t\tdebug(f(angle, 3));\n\t\treturn true;\n\t};\n\n\tfoa(p, pts) {\n\t\tif(eq(p, 0.)) continue;\n\t\tauto angles = calc_theta(p.real(), p.imag());\n\t\tif(ok(angles.first) || ok(angles.second)) return true;\n\t}\n\n\tauto angles = calc_theta(x_pig, y_pig);\n\treturn (ok(angles.first) || ok(angles.second));\n}\n\nint main() {\n\tstd::ios::sync_with_stdio(false);\n\tstd::cin.tie(nullptr);\n\tstd::cout << std::fixed << std::setprecision(15);\n\t// std::cerr << std::fixed << std::setprecision(15);\n\tsrand((unsigned)time(NULL));\n\n\tint n;\n\twhile(cin >> n) {\n\t\tYes(solve(n));\n\t}\n\n\treturn 0;\n}", "accuracy": 0.6, "time_ms": 30, "memory_kb": 3956, "score_of_the_acc": -1.0426, "final_rank": 10 }, { "submission_id": "aoj_2308_5846469", "code_snippet": "/*\tauthor: Kite_kuma\n\tcreated: 2021.09.01 15:05:47 */\n\n// #ifdef LOCAL\n// #define _GLIBCXX_DEBUG\n// #endif\n#include <bits/stdc++.h>\nusing namespace std;\n\n#pragma region macros\n\n#define foa(s, v) for(auto &s : v)\n#define all(v) (v).begin(), (v).end()\n#define rall(v) (v).rbegin(), (v).rend()\n#define popcnt(n) __builtin_popcountll((long long)n)\n\n#define REPname(a, b, c, d, e, ...) e\n#define rep(...) REPname(__VA_ARGS__, REP3, REP2, REP1, REP0)(__VA_ARGS__)\n#define REP0(x) for(int Counter_in_rep_macro = 0; Counter_in_rep_macro < (x); ++Counter_in_rep_macro)\n#define REP1(i, x) for(int i = 0; i < (x); ++i)\n#define REP2(i, l, r) for(int i = (l); i < (r); ++i)\n#define REP3(i, l, r, c) for(int i = (l); i < (r); i += (c))\n\n#define DREPname(a, b, c, d, e, ...) e\n#define drep(...) DREPname(__VA_ARGS__, DREP3, DREP2, DREP1)(__VA_ARGS__)\n#define DREP1(i, x) for(int i = (x)-1; i >= 0; --i)\n#define DREP2(i, l, r) for(int i = (r)-1; i >= (l); --i)\n#define DREP3(i, l, r, c) for(int i = (r)-1; i >= (l); i -= (c))\n\n#pragma endregion\n\n#pragma region aliases\n\nusing ll = long long;\nusing ld = long double;\nusing ull = unsigned long long;\nusing vi = std::vector<int>;\nusing vvi = std::vector<std::vector<int>>;\nusing vvvi = std::vector<std::vector<std::vector<int>>>;\nusing vll = std::vector<ll>;\nusing vvll = std::vector<vll>;\nusing vvvll = std::vector<vvll>;\nusing pii = std::pair<int, int>;\nusing pll = std::pair<long long, long long>;\ntemplate <class T = ll>\nusing V = std::vector<T>;\ntemplate <class T = ll>\nusing VV = V<V<T>>;\ntemplate <class T = ll>\nusing VVV = V<V<V<T>>>;\ntemplate <class T = ll>\nusing pqup = std::priority_queue<T, std::vector<T>, std::greater<T>>;\ntemplate <class T = ll>\nusing pqdn = std::priority_queue<T>;\n\n#pragma endregion\n\n#pragma region constants\n\nconst int inf = 1e9;\nconst long long INF = 1e18;\nconst long double pi = acos(-1);\nconst char dl = '\\n';\nconst char sp = ' ';\nint dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};\nint dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};\n\nconst int mod_1000000007 = 1000000007;\nconst int mod_998244353 = 998244353;\n\n#pragma endregion\n\n#pragma region basic_operation\n\ntemplate <class T0, class T1, class T2>\ninline bool in_range(T0 x, T1 lef, T2 rig) {\n\treturn ((lef <= x) && (x < rig));\n}\n\ntemplate <class T>\ninline bool chmin(T &a, T b) {\n\tif(a > b) {\n\t\ta = b;\n\t\treturn true;\n\t}\n\treturn false;\n}\ntemplate <class T>\ninline bool chmax(T &a, T b) {\n\tif(a < b) {\n\t\ta = b;\n\t\treturn true;\n\t}\n\treturn false;\n}\n\nvoid Yes(bool f = 1) { std::cout << (f ? \"Yes\" : \"No\") << '\\n'; }\nvoid No() { std::cout << \"No\\n\"; }\nvoid YES(bool f = 1) { std::cout << (f ? \"YES\" : \"NO\") << '\\n'; }\nvoid NO() { std::cout << \"NO\\n\"; }\n\ntemplate <class T>\nvoid drop(T answer) {\n\tstd::cout << answer << '\\n';\n\texit(0);\n}\n\nvoid err(bool flag = true) {\n\tif(!flag) return;\n\tstd::cout << -1 << '\\n';\n\texit(0);\n}\n\ntemplate <class T>\nvoid vout(std::vector<T> const &v, bool vertically = 0) {\n\tif(vertically) {\n\t\tfor(auto const &a : v) {\n\t\t\tstd::cout << a << '\\n';\n\t\t}\n\t\treturn;\n\t}\n\tfor(auto it = v.begin(); it != v.end(); it++) {\n\t\tstd::cout << (*it);\n\t\tif(it != v.end() - 1) {\n\t\t\tstd::cout << ' ';\n\t\t}\n\t}\n\tstd::cout << '\\n';\n\treturn;\n}\n\ninline void print() { std::cout << '\\n'; }\ntemplate <class T>\ninline void print(T x) {\n\tstd::cout << x << '\\n';\n\treturn;\n}\ntemplate <typename Head, typename... Tail>\nvoid print(Head H, Tail... T) {\n\tstd::cout << H << \" \";\n\tprint(T...);\n}\n\ntemplate <class T>\nvoid add(std::vector<T> &v, T val) {\n\tfor(auto &a : v) a += val;\n\treturn;\n}\n\ntemplate <class T>\nT dup(T a, T b) {\n\tassert(b != 0);\n\treturn (a + b - 1) / b;\n}\n\ntemplate <class T>\nT greatest_lower_multiple(T x, T d) {\n\tif(d == 0) return 0;\n\tif(d < 0) d *= -1;\n\tT y = x % d;\n\tif(y < 0) y += d;\n\treturn x - y;\n}\n\ntemplate <class T>\nT least_upper_multiple(T x, T d) {\n\treturn -greatest_lower_multiple(-x, d);\n}\n\nlong long POW(long long a, long long n) {\n\tlong long res = 1;\n\twhile(n > 0) {\n\t\tif(n & 1) res = res * a;\n\t\ta = a * a;\n\t\tn >>= 1;\n\t}\n\treturn res;\n}\n\nlong long modpow(long long a, long long n, long long mod) {\t // a^n mod\n\tassert(n >= 0 && mod);\n\tif(mod == 1) return 0LL;\n\tlong long res = 1;\n\ta %= mod;\n\twhile(n > 0) {\n\t\tif(n & 1) res = res * a % mod;\n\t\ta = a * a % mod;\n\t\tn >>= 1;\n\t}\n\treturn res < 0 ? res + mod : res;\n}\n\n// return x which satisfies a * x % mod == gcd(a, mod)\n// not (mod | a)\nlong long modinv(long long a, long long mod) {\n\tlong long b = mod, u = 1, v = 0;\n\twhile(b) {\n\t\tlong long t = a / b;\n\t\ta -= t * b;\n\t\tstd::swap(a, b);\n\t\tu -= t * v;\n\t\tstd::swap(u, v);\n\t}\n\tu %= mod;\n\tif(u < 0) u += mod;\n\treturn u;\n}\n\ntemplate <class T>\nint lb(const std::vector<T> &a, const T x) {\n\treturn std::distance(a.begin(), std::lower_bound(a.begin(), a.end(), x));\n}\ntemplate <class T>\nint ub(const std::vector<T> &a, const T x) {\n\treturn std::distance(a.begin(), std::upper_bound(a.begin(), a.end(), x));\n}\ntemplate <class T>\nvoid unq_sort(std::vector<T> &a) {\n\tstd::sort(a.begin(), a.end());\n\ta.erase(std::unique(a.begin(), a.end()), a.end());\n}\ntemplate <class T>\nstd::vector<int> press(std::vector<T> &a) {\n\tauto vec = a;\n\tunq_sort(vec);\n\tstd::vector<int> ret;\n\tfor(auto &v : a) ret.push_back(lb(vec, v));\n\treturn ret;\n}\n\n#pragma endregion\n\n#pragma region input\n#define VEC(type, name, size) \\\n\tvector<type> name(size); \\\n\tscanner::INPUT(name)\n#define VVEC(type, name, h, w) \\\n\tvector<vector<type>> name(h, vector<type>(w)); \\\n\tscanner::INPUT(name)\n#define INT(...) \\\n\tint __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define LL(...) \\\n\tlong long __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define STR(...) \\\n\tstring __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define CHR(...) \\\n\tchar __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define DBL(...) \\\n\tdouble __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define LD(...) \\\n\tlong double __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define TPL3(type0, type1, type2, name) \\\n\tstd::tuple<type0, type1, type2> name; \\\n\tscanner::INPUT(name);\n#define TPL4(type0, type1, type2, type3, name) \\\n\tstd::tuple<type0, type1, type2, type3> name; \\\n\tscanner::INPUT(name);\n\nnamespace scanner {\ntemplate <class T>\nvoid scan(T &a) {\n\tstd::cin >> a;\n}\n\ntemplate <class T, class L>\nvoid scan(std::pair<T, L> &p) {\n\tscan(p.first);\n\tscan(p.second);\n}\n\ntemplate <class T0, class T1, class T2>\nvoid scan(std::tuple<T0, T1, T2> &p) {\n\tT0 t0;\n\tT1 t1;\n\tT2 t2;\n\tscan(t0);\n\tscan(t1);\n\tscan(t2);\n\tp = std::make_tuple(t0, t1, t2);\n}\n\ntemplate <class T0, class T1, class T2, class T3>\nvoid scan(std::tuple<T0, T1, T2, T3> &p) {\n\tT0 t0;\n\tT1 t1;\n\tT2 t2;\n\tT3 t3;\n\tscan(t0);\n\tscan(t1);\n\tscan(t2);\n\tscan(t3);\n\tp = std::make_tuple(t0, t1, t2, t3);\n}\n\ntemplate <class T>\nvoid scan(std::vector<T> &a) {\n\tfor(auto &i : a) scan(i);\n}\n\nvoid INPUT() {}\ntemplate <class Head, class... Tail>\nvoid INPUT(Head &head, Tail &... tail) {\n\tscan(head);\n\tINPUT(tail...);\n}\n} // namespace scanner\n\ntemplate <typename T1, typename T2>\nstd::istream &operator>>(std::istream &is, std::pair<T1, T2> &p) {\n\tis >> p.first >> p.second;\n\treturn is;\n}\n#pragma endregion\n\n#pragma region debug\n\n#pragma region output\ntemplate <typename T1, typename T2>\nstd::ostream &std::operator<<(std::ostream &os, const std::pair<T1, T2> &p) {\n\tos << p.first << \" \" << p.second;\n\treturn os;\n}\ntemplate <class T>\nstd::ostream &std::operator<<(std::ostream &os, const std::vector<T> &v) {\n\tfor(int i = 0; i < (int)v.size(); i++) {\n\t\tif(i) os << \" \";\n\t\tos << v[i];\n\t}\n\treturn os;\n}\n#pragma endregion\n\n#pragma region view\n\nnamespace viewer {\nvoid view(const long long e) {\n\tif(e == INF)\n\t\tstd::cerr << \"INF\";\n\telse if(e == -INF)\n\t\tstd::cerr << \"-INF\";\n\telse\n\t\tstd::cerr << e;\n}\n\nvoid view(const int e) {\n\tif(e == inf)\n\t\tstd::cerr << \"inf\";\n\telse if(e == -inf)\n\t\tstd::cerr << \"-inf\";\n\telse\n\t\tstd::cerr << e;\n}\n\ntemplate <typename T>\nvoid view(const T e) {\n\tstd::cerr << e;\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::pair<T, U> &p) {\n\tstd::cerr << \"(\";\n\tview(p.first);\n\tstd::cerr << \", \";\n\tview(p.second);\n\tstd::cerr << \")\";\n}\n\ntemplate <class T0, class T1, class T2>\nvoid view(const std::tuple<T0, T1, T2> &p) {\n\tstd::cerr << \"(\";\n\tview(std::get<0>(p));\n\tstd::cerr << \", \";\n\tview(std::get<1>(p));\n\tstd::cerr << \", \";\n\tview(std::get<2>(p));\n\tstd::cerr << \")\";\n}\n\ntemplate <class T0, class T1, class T2, class T3>\nvoid view(const std::tuple<T0, T1, T2, T3> &p) {\n\tstd::cerr << \"(\";\n\tview(std::get<0>(p));\n\tstd::cerr << \", \";\n\tview(std::get<1>(p));\n\tstd::cerr << \", \";\n\tview(std::get<2>(p));\n\tstd::cerr << \", \";\n\tview(std::get<3>(p));\n\tstd::cerr << \")\";\n}\n\ntemplate <typename T>\nvoid view(const std::set<T> &s) {\n\tif(s.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << \"{ \";\n\tfor(auto &t : s) {\n\t\tview(t);\n\t\tstd::cerr << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\n\ntemplate <typename T>\nvoid view(const std::unordered_set<T> &s) {\n\tif(s.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << \"{ \";\n\tfor(auto &t : s) {\n\t\tview(t);\n\t\tstd::cerr << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\n\ntemplate <typename T>\nvoid view(const std::vector<T> &v) {\n\tif(v.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << \"{ \";\n\tfor(const auto &e : v) {\n\t\tview(e);\n\t\tstd::cerr << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\n\ntemplate <typename T>\nvoid view(const std::vector<std::vector<T>> &vv) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &v : vv) {\n\t\tstd::cerr << \"\\t\";\n\t\tview(v);\n\t\tstd::cerr << '\\n';\n\t}\n\tstd::cerr << \"}\";\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::vector<std::pair<T, U>> &v) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &c : v) {\n\t\tstd::cerr << \"\\t(\";\n\t\tview(c.first);\n\t\tstd::cerr << \", \";\n\t\tview(c.second);\n\t\tstd::cerr << \")\\n\";\n\t}\n\tstd::cerr << \"}\";\n}\n\ntemplate <class T0, class T1, class T2>\nvoid view(const std::vector<std::tuple<T0, T1, T2>> &v) {\n\tif(v.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << '{';\n\tfor(const auto &t : v) {\n\t\tstd::cerr << \"\\n\\t\";\n\t\tview(t);\n\t\tstd::cerr << \",\";\n\t}\n\tstd::cerr << \"\\n}\";\n}\n\ntemplate <class T0, class T1, class T2, class T3>\nvoid view(const std::vector<std::tuple<T0, T1, T2, T3>> &v) {\n\tif(v.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << '{';\n\tfor(const auto &t : v) {\n\t\tstd::cerr << \"\\n\\t\";\n\t\tview(t);\n\t\tstd::cerr << \",\";\n\t}\n\tstd::cerr << \"\\n}\";\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::map<T, U> &m) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &t : m) {\n\t\tstd::cerr << \"\\t[\";\n\t\tview(t.first);\n\t\tstd::cerr << \"] : \";\n\t\tview(t.second);\n\t\tstd::cerr << '\\n';\n\t}\n\tstd::cerr << \"}\";\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::unordered_map<T, U> &m) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &t : m) {\n\t\tstd::cerr << \"\\t[\";\n\t\tview(t.first);\n\t\tstd::cerr << \"] : \";\n\t\tview(t.second);\n\t\tstd::cerr << '\\n';\n\t}\n\tstd::cerr << \"}\";\n}\n} // namespace viewer\n\n#pragma endregion\n\n// when debugging : g++ foo.cpp -DLOCAL\n#ifdef LOCAL\nvoid debug_out() {}\ntemplate <typename Head, typename... Tail>\nvoid debug_out(Head H, Tail... T) {\n\tviewer::view(H);\n\tstd::cerr << \", \";\n\tdebug_out(T...);\n}\n#define debug(...) \\\n\tdo { \\\n\t\tstd::cerr << __LINE__ << \" [\" << #__VA_ARGS__ << \"] : [\"; \\\n\t\tdebug_out(__VA_ARGS__); \\\n\t\tstd::cerr << \"\\b\\b]\\n\"; \\\n\t} while(0)\n#define dump(x) \\\n\tdo { \\\n\t\tstd::cerr << __LINE__ << \" \" << #x << \" : \"; \\\n\t\tviewer::view(x); \\\n\t\tstd::cerr << '\\n'; \\\n\t} while(0)\n\n#else\n#define debug(...) (void(0))\n#define dump(x) (void(0))\n#endif\n\n#pragma endregion\n\nusing R = long double;\n\n#pragma region geometry_light\n\nusing Real = long double;\nusing R = Real;\nusing Point = std::complex<Real>;\nusing Vec = Point;\nconst Real EPS = 1e-10, PI = acos(-1);\n\ninline bool eq(const Real &a, const Real &b) { return fabs(b - a) < EPS; }\ninline bool eq(const Point &a, const Point &b) { return fabs(b - a) < EPS; }\n/*\n\t-1: a < b\n\t0 : a == b\n\t1 : a > b\n*/\ninline int compare(const Real &a, const Real &b) { return eq(a, b) ? 0 : a < b ? -1 : 1; }\ninline int sign(const Real &a) { return fabs(a) < EPS ? 0 : a < 0 ? -1 : 1; }\n\nnamespace std {\nconst Point operator*(const Point &p, const Real &d) { return Point(real(p) * d, imag(p) * d); }\n} // namespace std\n\nstd::istream &operator>>(std::istream &is, Point &p) {\n\tReal a, b;\n\tis >> a >> b;\n\tp = Point(a, b);\n\treturn is;\n}\nstd::ostream &operator<<(std::ostream &os, Point &p) { return os << std::fixed << std::setprecision(10) << p.real() << \" \" << p.imag(); }\n\n// rotate point 'p' for counter clockwise direction\nconst Point rotate(const Real &theta, const Point &p) {\n\treturn Point(cos(theta) * p.real() - sin(theta) * p.imag(), sin(theta) * p.real() + cos(theta) * p.imag());\n}\n\nconst Real radian_to_degree(const Real &r) { return (r * 180.0 / PI); }\n\nconst Real degree_to_radian(const Real &d) { return (d * PI / 180.0); }\n\n// get angle a-b-c (<pi)\nconst Real get_angle(const Point &a, const Point &b, const Point &c) {\n\tconst Point v(a - b), w(c - b);\n\tReal theta = fabs(atan2(w.imag(), w.real()) - atan2(v.imag(), v.real()));\n\treturn std::min(theta, 2 * PI - theta);\n}\n\nnamespace std {\nbool operator<(const Point &a, const Point &b) { return a.real() != b.real() ? a.real() < b.real() : a.imag() < b.imag(); }\n} // namespace std\n\nstruct Line {\n\tPoint a, b;\n\n\tLine() = default;\n\n\tLine(Point a, Point b) : a(a), b(b) {}\n\n\tLine(Real A, Real B, Real C) // Ax + By = C\n\t{\n\t\tif(eq(A, 0))\n\t\t\ta = Point(0, C / B), b = Point(1, C / B);\n\t\telse if(eq(B, 0))\n\t\t\tb = Point(C / A, 0), b = Point(C / A, 1);\n\t\telse\n\t\t\ta = Point(0, C / B), b = Point(C / A, 0);\n\t}\n\n\tfriend std::ostream &operator<<(std::ostream &os, Line &p) { return os << p.a << \" -- \" << p.b; }\n\n\tfriend std::istream &operator>>(std::istream &is, Line &a) { return is >> a.a >> a.b; }\n};\n\nstruct Segment : Line {\n\tSegment() = default;\n\n\tSegment(Point a, Point b) : Line(a, b) {}\n};\n\nstruct Circle {\n\tPoint p;\n\tReal r;\n\n\tCircle() = default;\n\n\tCircle(Point p, Real r) : p(p), r(r) {}\n};\n\nusing Points = std::vector<Point>;\nusing Polygon = std::vector<Point>;\nusing Segments = std::vector<Segment>;\nusing Lines = std::vector<Line>;\nusing Circles = std::vector<Circle>;\n\nconst Real cross(const Point &a, const Point &b) { return real(a) * imag(b) - imag(a) * real(b); }\nconst Real dot(const Point &a, const Point &b) { return real(a) * real(b) + imag(a) * imag(b); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_C\nconst int ccw(const Point &a, Point b, Point c) {\n\tb = b - a, c = c - a;\n\tif(cross(b, c) > EPS) return +1;\t\t// \"COUNTER_CLOCKWISE\"\n\tif(cross(b, c) < -EPS) return -1;\t\t// \"CLOCKWISE\"\n\tif(dot(b, c) < -EPS) return +2;\t\t\t// \"ONLINE_BACK\"\n\tif(norm(b) + EPS < norm(c)) return -2;\t// \"ONLINE_FRONT\"\n\treturn 0;\t\t\t\t\t\t\t\t// \"ON_SEGMENT\"\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_A\nbool parallel(const Line &a, const Line &b) { return eq(cross(a.b - a.a, b.b - b.a), 0.0); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_A\nbool orthogonal(const Line &a, const Line &b) { return eq(dot(a.a - a.b, b.a - b.b), 0.0); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_A\nPoint projection(const Line &l, const Point &p) {\n\tdouble t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n\treturn l.a + (l.a - l.b) * t;\n}\n\nPoint projection(const Segment &l, const Point &p) {\n\tdouble t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n\treturn l.a + (l.a - l.b) * t;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_B\nPoint reflection(const Line &l, const Point &p) { return projection(l, p) * 2.0 - p; }\n\nint intersect(const Line &l, const Point &p) { return int(abs(ccw(l.a, l.b, p)) != 1); }\n\nint intersect(const Line &l, const Line &m) {\n\tif(intersect(l, m.a) && intersect(l, m.b)) return -1;\n\treturn int(abs(cross(l.b - l.a, m.b - m.a)) > EPS);\n}\n\nint intersect(const Segment &s, const Point &p) { return int(ccw(s.a, s.b, p) == 0); }\n\nint intersect(const Line &l, const Segment &s) {\n\tif(intersect(l, s.a) && intersect(l, s.b)) return -1;\n\treturn cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < EPS;\n}\n\nReal distance(const Line &l, const Point &p);\n\nint intersect(const Circle &c, const Line &l) {\n\tReal d = c.r - distance(l, c.p);\n\treturn fabs(d) < EPS ? 1 : d > 0. ? 2 : 0;\n}\n\nint intersect(const Circle &c, const Point &p) { return int(abs(abs(p - c.p) - c.r) < EPS); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_B\nint intersect(const Segment &s, const Segment &t) {\n\tif(eq(s.a, s.b)) return intersect(t, s.a);\n\tif(intersect(Line(s), t.a) && intersect(Line(s), t.b) &&\n\t std::max(std::min(s.a, s.b), std::min(t.a, t.b)) < std::min(std::max(s.a, s.b), std::max(t.a, t.b)))\n\t\treturn -1;\n\treturn int(ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 && ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0);\n}\n\nint intersect(const Circle &c, const Segment &l) {\n\tconst Point h = projection(l, c.p);\n\tif(norm(h - c.p) - c.r * c.r > EPS) return 0;\n\tauto d1 = abs(c.p - l.a), d2 = abs(c.p - l.b);\n\tif(compare(d1, c.r) == -1 && compare(d2, c.r) == -1) return 0;\n\tif(d1 < c.r - EPS && d2 > c.r - EPS || d1 > c.r - EPS && d2 < c.r - EPS) return 1;\n\treturn dot(l.a - h, l.b - h) < 0 ? 2 : 0;\n}\n\nReal distance(const Point &a, const Point &b);\n\nint number_of_common_tangents(const Circle &c1, const Circle &c2) {\n\tReal r1 = std::min(c1.r, c2.r), r2 = std::max(c1.r, c2.r), d = distance(c1.p, c2.p);\n\tint com = compare(r1 + r2, d);\n\treturn com == 1 ? compare(d + r1, r2) + 1 : 3 - com;\n\t// if(c1.r < c2.r) swap(c1, c2);\n\t// Real d = abs(c1.p - c2.p);\n\t// if(compare(c1.r + c2.r, d) == -1) return 4;\n\t// if(eq(c1.r + c2.r, d)) return 3;\n\t// if(compare(c1.r - c2.r, d) == -1) return 2;\n\t// return int(eq(c1.r - c2.r, d));\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_A&lang=jp\nint intersect(const Circle &c1, const Circle &c2) { return 2 - abs(2 - number_of_common_tangents(c1, c2)); }\n\nReal distance(const Point &a, const Point &b) { return abs(a - b); }\n\nReal distance(const Line &l, const Point &p) { return abs(p - projection(l, p)); }\n\nReal distance(const Line &l, const Line &m) { return intersect(l, m) ? 0 : distance(l, m.a); }\n\nReal distance(const Segment &s, const Point &p) {\n\tPoint r = projection(s, p);\n\treturn intersect(s, r) ? distance(r, p) : std::min(abs(s.a - p), abs(s.b - p));\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_D\nReal distance(const Segment &a, const Segment &b) {\n\tif(intersect(a, b)) return 0;\n\treturn std::min({distance(a, b.a), distance(a, b.b), distance(b, a.a), distance(b, a.b)});\n}\n\nReal distance(const Line &l, const Segment &s) {\n\tif(intersect(l, s)) return 0;\n\treturn std::min(distance(l, s.a), distance(l, s.b));\n}\n\nPoint crosspoint(const Line &l, const Line &m) {\n\tReal A = cross(l.b - l.a, m.b - m.a);\n\tReal B = cross(l.b - l.a, l.b - m.a);\n\tif(eq(abs(A), 0.0) && eq(abs(B), 0.0)) return m.a;\n\treturn m.a + (m.b - m.a) * B / A;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_C\nPoint crosspoint(const Segment &l, const Segment &m) { return crosspoint(Line(l), Line(m)); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_D\nstd::pair<Point, Point> crosspoint(const Circle &c, const Line l) {\n\tPoint pr = projection(l, c.p);\n\tif(eq(distance(l, c.p), c.r)) return {pr, pr};\n\tVec v = (l.b - l.a) / abs(l.b - l.a) * sqrt(c.r * c.r - norm(pr - c.p));\n\treturn make_pair(pr - v, pr + v);\n\t// Vec e = (l.b - l.a) / abs(l.b - l.a);\n\t// double base = sqrt(c.r * c.r - norm(pr - c.p));\n\t// return {pr - e * base, pr + e * base};\n}\n\nstd::pair<Point, Point> crosspoint(const Circle &c, const Segment &l) {\n\tif(intersect(c, l) == 2) return crosspoint(c, Line(l.a, l.b));\n\tauto ret = crosspoint(c, Line(l.a, l.b));\n\tif(dot(l.a - ret.first, l.b - ret.first) < EPS)\n\t\tret.second = ret.first;\n\telse\n\t\tret.first = ret.second;\n\treturn ret;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_E\nstd::pair<Point, Point> crosspoint(const Circle &c1, const Circle &c2) {\n\tReal d = abs(c1.p - c2.p);\n\tReal a = acos((c1.r * c1.r + d * d - c2.r * c2.r) / (2 * c1.r * d)); // cosine theorem\n\tReal t = atan2(c2.p.imag() - c1.p.imag(), c2.p.real() - c1.p.real());\n\tPoint p1 = c1.p + Point(cos(t + a) * c1.r, sin(t + a) * c1.r);\n\tPoint p2 = c1.p + Point(cos(t - a) * c1.r, sin(t - a) * c1.r);\n\treturn make_pair(p1, p2);\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_F\n// \"点 p を通る円 c の接線\"の接点2つ\nstd::pair<Point, Point> tangent(const Circle &c1, const Point &p2) {\n\treturn crosspoint(c1, Circle(p2, sqrt(norm(c1.p - p2) - c1.r * c1.r)));\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_G\n// 円 c1, c2 の共通接線\nLines tangent(Circle c1, Circle c2) {\n\tLines ret;\n\tif(c1.r < c2.r) std::swap(c1, c2);\n\tReal g = norm(c1.p - c2.p);\n\tif(eq(g, 0)) return ret;\n\tVec u = (c2.p - c1.p) / Real(sqrt(g));\n\tVec v = rotate(PI * 0.5, u);\n\tfor(int s : {-1, 1}) {\n\t\tReal h = (c1.r + s * c2.r) / sqrt(g);\n\t\tif(eq(1 - h * h, 0)) {\n\t\t\tret.emplace_back(c1.p + u * c1.r, c1.p + (u + v) * c1.r);\n\t\t} else if(1 - h * h > 0) {\n\t\t\tPoint uu = u * h, vv = v * sqrt(1 - h * h);\n\t\t\tret.emplace_back(c1.p + (uu + vv) * c1.r, c2.p - (uu + vv) * c2.r * s);\n\t\t\tret.emplace_back(c1.p + (uu - vv) * c1.r, c2.p - (uu - vv) * c2.r * s);\n\t\t}\n\t}\n\treturn ret;\n}\n\n#pragma endregion\n\nbool solve(int n) {\n\tR velocity;\n\tcin >> velocity;\n\tR x_pig, y_pig;\n\tcin >> x_pig >> y_pig;\n\n\tVV<Segment> obstacle(n); // 0 ~ 2 yoko, 3~5 tate\n\n\tPoints pts;\n\n\trep(j, n) {\n\t\tR l, b, r, t;\n\t\tcin >> l >> b >> r >> t;\n\t\tpts.emplace_back(l, t);\n\t\tpts.emplace_back(l, b);\n\t\tpts.emplace_back(r, t);\n\t\tpts.emplace_back(r, b);\n\n\t\tR mid_x = (l + r) / 2;\n\t\tR mid_y = (b + t) / 2;\n\t\tPoints pts;\n\t\tfor(R x : {l, mid_x, r}) {\n\t\t\tfor(R y : {b, mid_y, t}) {\n\t\t\t\tpts.emplace_back(x, y);\n\t\t\t}\n\t\t}\n\t\trep(i, 3) { obstacle[j].emplace_back(pts[i], pts[i + 6]); }\n\t\trep(i, 3) obstacle[j].emplace_back(pts[i * 3], pts[i * 3 + 2]);\n\t}\n\n\tconst R g = 9.8;\n\n\tauto f = [&](R theta, R x) { return x * tan(theta) - g / 2 * pow(x / (velocity * cos(theta)), (R)2.); };\n\n\t// y = x tan theta - (g/2) (x/(v_0 cos theta))^2\n\tauto calc_theta = [&](R obj_x, R obj_y) -> pair<R, R> {\n\t\tR inf = 0;\n\t\tR sup = pi / 2 - EPS;\n\n\t\trep(100) {\n\t\t\tR lef = inf + (sup - inf) / 3;\n\t\t\tR rig = (lef + sup) / 2;\n\t\t\tif(f(lef, obj_x) < f(rig, obj_x)) {\n\t\t\t\tinf = lef;\n\t\t\t} else {\n\t\t\t\tsup = rig;\n\t\t\t}\n\t\t}\n\n\t\tif(compare(f(sup, obj_x), obj_y) == -1) {\n\t\t\treturn make_pair(-1., -1.);\n\t\t}\n\n\t\tR mid = sup;\n\t\tR a, b;\n\t\tinf = 0;\n\t\trep(100) {\n\t\t\ta = (inf + mid) / 2;\n\t\t\tif(f(a, obj_x) < obj_y) {\n\t\t\t\tinf = a;\n\t\t\t} else {\n\t\t\t\tmid = a;\n\t\t\t}\n\t\t}\n\n\t\tmid = sup;\n\t\tsup = pi / 2 - EPS;\n\n\t\trep(100) {\n\t\t\tb = (mid + sup) / 2;\n\t\t\tif(f(b, obj_x) < obj_x) {\n\t\t\t\tsup = b;\n\t\t\t} else {\n\t\t\t\tmid = b;\n\t\t\t}\n\t\t}\n\n\t\treturn pair(a, b);\n\t};\n\n\tauto tp = [&](R theta) { return pow(velocity, 2.) * cos(theta) * sin(theta) / g; };\n\n\tauto collision = [&](R theta, int obs_idx) -> bool {\n\t\tR top = tp(theta);\n\t\trep(i, 3) {\n\t\t\tauto &seg = obstacle[obs_idx][i];\n\t\t\tR xl, xr, y;\n\t\t\ty = seg.a.imag();\n\t\t\txl = seg.a.real();\n\t\t\txr = seg.b.real();\n\t\t\tif(eq(theta, 0.899297831746404)) {\n\t\t\t\tdebug(y, xl, xr);\n\t\t\t}\n\t\t\tif(xl > xr) swap(xl, xr);\n\t\t\tif(x_pig <= xl) continue;\n\t\t\tchmin(xr, x_pig);\n\t\t\tR yl = f(theta, xl);\n\t\t\tR yr = f(theta, xr);\n\t\t\tif(xl <= top && top <= xr) {\n\t\t\t\t// if(f(theta, top) > y) {\n\t\t\t\tif(compare(f(theta, top), y) == 1) {\n\t\t\t\t\tif(compare(yl, y) == -1 || compare(yr, y) == -1) return true;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif((yl - y) * (yr - y) < -EPS) return true;\n\t\t}\n\n\t\tdebug(theta);\n\n\t\trep(i, 3, 6) {\n\t\t\tauto &seg = obstacle[obs_idx][i];\n\t\t\tR yd, x, yt;\n\t\t\tx = seg.a.real();\n\t\t\tyd = seg.a.imag();\n\t\t\tyt = seg.b.imag();\n\t\t\tif(eq(theta, 0.899297831746404)) {\n\t\t\t\tdebug(x, yd, yt);\n\t\t\t}\n\t\t\tif(x > x_pig) continue;\n\t\t\tif(yd > yt) swap(yd, yt);\n\t\t\tR y = f(theta, x);\n\t\t\tif(compare(yd, y) == -1 && compare(y, yt) == -1) return true;\n\t\t}\n\n\t\treturn false;\n\t};\n\n\tauto use = [&](R angle) { return EPS < angle && angle < pi / 2 - EPS * 2; };\n\n\tauto ok = [&](R angle) {\n\t\tif(!use(angle)) return false;\n\t\trep(i, n) if(collision(angle, i)) return false;\n\t\tR y = f(angle, x_pig);\n\t\tif(y < y_pig) return false;\n\t\tSegment egg(Point(x_pig, y), Point(x_pig, y_pig));\n\t\tfoa(ob, obstacle) {\n\t\t\tfoa(seg, ob) {\n\t\t\t\tif(intersect(seg, egg)) {\n\t\t\t\t\treturn false;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tdebug(angle);\n\t\tdebug(f(angle, 1));\n\t\tdebug(f(angle, 2));\n\t\tdebug(f(angle, 3));\n\t\treturn true;\n\t};\n\n\tfoa(p, pts) {\n\t\tif(eq(p, 0.)) continue;\n\t\tauto angles = calc_theta(p.real(), p.imag());\n\t\tif(ok(angles.first) || ok(angles.second)) return true;\n\t}\n\n\tauto angles = calc_theta(x_pig, y_pig);\n\treturn (ok(angles.first) || ok(angles.second));\n}\n\nint main() {\n\tstd::ios::sync_with_stdio(false);\n\tstd::cin.tie(nullptr);\n\tstd::cout << std::fixed << std::setprecision(15);\n\t// std::cerr << std::fixed << std::setprecision(15);\n\tsrand((unsigned)time(NULL));\n\n\tint n;\n\twhile(cin >> n) {\n\t\tYes(solve(n));\n\t}\n\n\treturn 0;\n}", "accuracy": 0.6, "time_ms": 10, "memory_kb": 3884, "score_of_the_acc": -0.9723, "final_rank": 9 }, { "submission_id": "aoj_2308_2682265", "code_snippet": "#include <iostream>\n#include <cmath>\n\nusing namespace std;\n\n#define _USE_MATH_DEFINES\n\nconst int NMax = 150;\nconst double eps = 1e-8;\nconst double eps1 = 1e-6;\nconst double gravity = 9.8;\nconst double PI = 3.14159265;\n\nint N, V, X, Y;\nint L[NMax], B[NMax], R[NMax], T[NMax];\n\nint main()\n{\n\tcin >>N >>V >>X >>Y;\n\n\tfor (int i = 1; i <= N; ++i)\n\t\tcin >>L[i] >>B[i] >>R[i] >>T[i];\n\n\tfor (double alpha = -eps1; alpha <= PI/2; alpha += eps1)\n\t{\n\t\tdouble V_0x = V * cos(alpha);\n\t\tdouble V_0y = V * sin(alpha);\n\t\tdouble Time = X / V_0x;\n\t\tdouble LandY = V_0y * Time - 0.5*gravity*(Time*Time);\n\n\t\tif (Y - LandY <= eps)\n\t\t{\n bool intersect = 0;\n\n for (int i = 1; i <= N; ++i)\n if (X - L[i] >= eps && X - R[i] <= eps &&\n\t\t\t\t\tLandY - B[i] >= eps && LandY - T[i] <= eps)\n\t\t\t\t\tintersect = 1;\n\n for (int i = 1; i <= N; ++i)\n\t\t\t\tfor (int j = L[i]; j <= R[i]; ++j)\n\t\t\t\t{\n\t\t\t\t\tdouble TimeToJ = j / V_0x;\n\t\t\t\t\tif (Time - TimeToJ >= eps)\n\t\t\t\t\t{\n double newY = V_0y * TimeToJ - 0.5*gravity*(TimeToJ*TimeToJ);\n if (newY - B[i] - eps >= 0 &&\n\t\t\t\t\t\t\tnewY - T[i] - eps <= 0)\n\t\t\t\t\t\t\t\tintersect = 1;\n\t\t\t\t\t}\n\n\t\t\t\t}\n\n\t\t\tif (!intersect)\n\t\t\t{\n cout <<\"Yes\" <<'\\n';\n return 0;\n\t\t\t}\n\t\t}\n\t}\n\n\tcout <<\"No\" <<'\\n';\n return 0;\n}", "accuracy": 1, "time_ms": 480, "memory_kb": 3484, "score_of_the_acc": -1.8185, "final_rank": 5 }, { "submission_id": "aoj_2308_2517963", "code_snippet": "#include <iostream>\n#include <cstdio>\n#include <cstring>\n#include <algorithm>\n#include <cmath>\nusing namespace std;\n\n#define MAXN 55\n#define EPS 1e-6\n#define g 9.8\n#define PI acos(-1)\n\nstruct Obstacle\n{\n int l;\n int r;\n int t;\n int b;\n} ob[MAXN];\n\nint n,v,tx,ty;\ndouble a,b,c;\n\nvoid getabc(double angle)\n{\n double vx = v * cos(angle); //x?方向速度\n double vy = v * sin(angle); //y?方向速度\n a = -0.5 * g / (vx * vx);\n b = vy / vx;\n c = 0;\n}\n\nbool check1(double x, double up, double down) //是否会撞到障碍物的左(右)?端或者左(右)底端\n{\n if(x >= tx) //如果障碍物的x坐?在猪的x坐?的右?,?就算会撞到也不要?(?炸??死猪不受?障碍物影?)\n return false;\n double y = a * x * x + b * x + c; //求出相?的y坐?\n if(up <= y && y <= down) //会撞到\n return true;\n return false;\n}\n\nbool check2(double y, double left, double right)\n{\n double delta = b * b - 4 * a * y;\n if(delta < 0) //抛物?与障碍物上(下)的左端和右端形成的?段没有交点,?不会相撞\n return false;\n delta = sqrt(delta);\n double x1 = (-b + delta) / (2 * a);\n double x2 = (-b - delta) / (2 * a);\n if(x1 < tx && left <= x1 && x1 <= right) //如果抛物?与障碍物上(下)的左端点有交点且交点的x坐?在猪的左?,?无法?死猪\n return true;\n if(x2 < tx && left <= x2 && x2 <= right) //如果抛物?与障碍物上(下)的右端点有交点且交点的x坐?在猪的左?,?无法?死猪\n return true;\n return false;\n}\n\nbool check3(double x, double y, double h, double t, double l, double r)\n{\n if(l <= x && x <= r && y <= t && t <= h) //如果障碍正好?在白??行的抛物??迹和猪的中?,?炸蛋无法?中猪\n return true;\n return false;\n}\n\nbool check(double angle)\n{\n getabc(angle);\n double h = a * tx * tx + b * tx + c;\n if(h < ty) //?不到猪的上方,?不可能?死猪\n return false;\n for(int i=0; i<n; i++)\n {\n if(check1(ob[i].l,ob[i].b,ob[i].t))\n return false;\n if(check1(ob[i].r,ob[i].b,ob[i].t))\n return false;\n if(check2(ob[i].b,ob[i].l,ob[i].r))\n return false;\n if(check2(ob[i].t,ob[i].l,ob[i].r))\n return false;\n if(check3(tx,ty,h,ob[i].b,ob[i].l,ob[i].r))\n return false;\n //if(check3(tx,ty,h,ob[i].t,ob[i].l,ob[i].r))\n // return false;\n }\n return true;\n}\n\nbool solve()\n{\n for(double i=0; i<0.5*PI; i+=0.000001)\n {\n if(check(i))\n return true;\n }\n return false;\n}\n\nint main()\n{\n scanf(\"%d%d%d%d\",&n,&v,&tx,&ty);\n for(int i=0; i<n; i++)\n scanf(\"%d%d%d%d\",&ob[i].l,&ob[i].b,&ob[i].r,&ob[i].t);\n if(solve())\n puts(\"Yes\");\n else\n puts(\"No\");\n return 0;\n}", "accuracy": 1, "time_ms": 90, "memory_kb": 3536, "score_of_the_acc": -1.0087, "final_rank": 3 }, { "submission_id": "aoj_2308_2040309", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define x() real()\n#define y() imag()\n#define x1 jfwekljfwklj\n#define x2 jfwekljwefkjklf\ntypedef complex<double> P;\nconst double g = 4.9;\nconst double EPS = 1e-8;\nint N;\ndouble V,X,Y;\n \n// = -(gx2/v2)tan2 + xtan - y\n \nstruct Box{\n P p1,p2,p3,p4;\n};\nvector<Box> box;\n \nP in(){\n double x,y;\n cin >> x >> y;\n return P(x,y);\n}\n \nbool inner(double a,double b,double c){\n return a + EPS < b and b < c - EPS;\n}\nvoid trying(double tanv){\n double a = - g / V / V * (1+tanv * tanv);\n double b = tanv;\n auto f = [a,b](double x){\n return a * x * x + b * x;\n };\n auto g = [a,b](double y){ \n vector<double> ps;\n double c = -y;\n double D = b*b-4*a*c;\n if( D < -EPS ){\n return ps;\n }\n if( D < EPS ) D = 0;\n D = sqrt(D);\n double x1 = (-b+D)/(2*a);\n double x2 = (-b-D)/(2*a);\n ps = {x1,x2};\n return ps;\n };\n \n \n double hx = 1e9;\n double hy = 1e9;\n vector<double> uxtmp;\n \n for( auto b : box ){\n if( inner(b.p1.x(),X,b.p2.x()) and b.p1.y() > Y ){\n hy = min(hy,b.p1.y());\n }\n }\n for( auto b : box ){\n double x = b.p1.x();\n uxtmp.push_back(x);\n double y = f(x);\n if( inner(b.p1.y(),y,b.p4.y()) ){\n hx = min(x,hx);\n }\n }\n \n for( auto b : box ){\n double x = b.p2.x();\n double y = f(x);\n uxtmp.push_back(x);\n if( inner(b.p2.y(),y,b.p3.y()) ){\n hx = min(x,hx);\n }\n }\n \n for( auto b : box ){\n double y = b.p1.y();\n for( double x : g(y) ){\n uxtmp.push_back(x);\n if( inner(b.p1.x(),x,b.p2.x()) ){\n hx = min(x,hx);\n }\n }\n }\n \n for( auto b : box ){\n double y = b.p3.y();\n for( double x : g(y) ){\n uxtmp.push_back(x);\n if( inner(b.p1.x(),x,b.p2.x()) ){\n hx = min(x,hx);\n }\n }\n }\n sort(uxtmp.begin(),uxtmp.end());\n vector<double> ux;\n for(int i = 0 ; i < uxtmp.size() ; i++)\n if( !i or uxtmp[i] - uxtmp[i-1] > EPS )\n ux.push_back(uxtmp[i]);\n \n for( auto b : box ){\n if( inner(b.p1.x(),X,b.p2.x()) ){\n if( inner(b.p1.y(),Y,b.p4.y()) ){\n hx = -1e9;\n }\n }\n }\n for(int i = 0 ; i+1 < ux.size() ; i++){\n \n double x = (ux[i] + ux[i+1] ) / 2;\n double y = f(x);\n for( auto b : box ){\n if( inner(b.p1.x(),x,b.p2.x()) ){\n if( inner(b.p1.y(),y,b.p4.y()) ){\n hx = min(ux[i],hx);\n }\n }\n }\n }\n //cout << f(1) << endl;\n //cout << hy << endl;\n //cout << V << \" \" << tanv << \" \" << a << \" \" << b << \" \" << X << \" \" << f(X) << endl;\n if( hx + EPS > X ){\n //cout << f(X) << \" \" << Y - EPS << endl;\n if( f(X) > Y - EPS and f(X) < hy - EPS ){\n cout << \"Yes\" << endl;\n //cout << atan(tanv) << endl;\n exit(0);\n }\n }\n \n \n \n \n \n \n}\n \n \nint main(){\n \n cin >> N;\n cin >> V >> X >> Y;\n \n \n vector<P> ps;\n for(int i = 0 ; i < N ; i++){\n P a,b;\n a = in();\n b = in();\n ps.push_back(a);\n ps.push_back(P(b.x(),a.y()));\n ps.push_back(b);\n ps.push_back(P(a.x(),b.y()));\n box.push_back({P(a.x(),a.y()),P(b.x(),a.y()),P(b.x(),b.y()),P(a.x(),b.y())}); \n \n }\n for(int i = 0 ; i < 3000 ; i++){\n trying(0.001*i);\n }\n if(true){\n double x = X;\n double y = Y;\n double a = -g/V/V*x*x;\n double b = x;\n double c = -y-g/V/V*x*x;\n //cout << a << \" \" << b << \" \" << c << \" ( \" << g << \" \" << V << \" \" << x << endl;\n double D = b*b-4*a*c;\n if( D < -EPS ){\n \n }else{\n D = max(0.0,D);\n D = sqrt(D);\n double t1 = (-b+D)/(2*a);\n double t2 = (-b-D)/(2*a);\n trying(t1);\n trying(t2);\n }\n }\n \n for(int i = 0 ; i < ps.size() ; i++){\n double x = ps[i].x();\n double y = ps[i].y();\n if( x == 0 ) continue;\n double a = -g/V/V*x*x;\n double b = x;\n double c = -y-g/V/V*x*x;\n \n \n double D = b*b-4*a*c;\n if( D < -EPS ){\n continue;\n }\n \n //printf(\"%.20lf\\n\",D); \n if( D < EPS ) D = 0;\n \n \n double t1 = (-b+D)/(2*a);\n double t2 = (-b-D)/(2*a);\n trying(t1);\n trying(t2);\n }\n \n cout << \"No\" << endl;\n \n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3300, "score_of_the_acc": -0.8115, "final_rank": 1 }, { "submission_id": "aoj_2308_1777434", "code_snippet": "#include \"bits/stdc++.h\"\n#include<unordered_map>\n#include<unordered_set>\n#pragma warning(disable:4996)\nusing namespace std;\nusing ld = long double;\ntemplate<class T>\nusing Table = vector<vector<T>>;\nconst ld eps=1e-9;\n\n//// < \"D:\\D_Download\\Visual Studio 2015\\Projects\\programing_contest_c++\\Debug\\a.txt\"\n\n\n/* ??????????????¬ */\n\n#include <complex>\n\ntypedef long double ld;\ntypedef complex<ld> Point;\n#define REP(i,n) for(int i=0;i<(int)(n);i++)\n#define ALL(x) (x).begin(),(x).end()\n\n\nconst ld pi = acos(-1.0);\nconst ld dtop = pi / 180.;\nconst ld ptod = 1. / dtop;\n\nnamespace std {\n\tbool operator<(const Point &lhs, const Point &rhs) {\n\t\tif (lhs.real() < rhs.real() - eps) return true;\n\t\tif (lhs.real() > rhs.real() + eps) return false;\n\t\treturn lhs.imag() < rhs.imag();\n\t}\n}\n\n// ????????\\???\nPoint input_point() {\n\tld x, y;\n\tcin >> x >> y;\n\treturn Point(x, y);\n}\n\n// ????????????????????????\nbool eq(const ld a, const ld b) {\n\treturn (abs(a - b) < eps);\n}\n\n// ??????\nld dot(const Point& a, const Point& b) {\n\treturn real(conj(a) * b);\n}\n\n// ??????\nld cross(const Point& a, const Point& b) {\n\treturn imag(conj(a) * b);\n}\n\n// ??´????????????\nclass Line {\npublic:\n\tPoint a, b;\n\tLine() : a(Point(0, 0)), b(Point(0, 0)) {}\n\tLine(Point a, Point b) : a(a), b(b) {}\n\tPoint operator[](const int _num)const {\n\t\tif (_num == 0)return a;\n\t\telse if (_num == 1)return b;\n\t\telse {\n\t\t\tassert(false);\n\t\t\treturn Point();\n\t\t}\n\t}\n};\n\n// ????????????\nclass Circle {\npublic:\n\tPoint p;\n\tld r;\n\tCircle() : p(Point(0, 0)), r(0) {}\n\tCircle(Point p, ld r) : p(p), r(r) {}\n};\n\n// CCW\nint ccw(const Point& a, const Point &b, const Point &c) {\n\tconst Point nb(b - a);\n\tconst Point nc(c - a);\n\tif (cross(nb, nc) > eps) return 1; // a,b,c??????????¨???¨?????????????????¶\n\tif (cross(nb, nc) < -eps) return -1; // a,b,c???????¨???¨?????????????????¶\n\tif (dot(nb, nc) < 0) return 2; // c,a,b???????????´???????????¶\n\tif (norm(nb) < norm(nc)) return -2; // a,b,c???????????´???????????¶\n\treturn 0; // a,c,b???????????´???????????¶\n}\n\n\n/* ???????????? */\n\n// ??´?????¨??´??????????????????\nbool isis_ll(const Line& l, const Line& m) {\n\treturn !eq(cross(l.b - l.a, m.b - m.a), 0);\n}\n\n// ??´?????¨?????????????????????\nbool isis_ls(const Line& l, const Line& s) {\n\treturn isis_ll(l, s) &&\n\t\t(cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < eps);\n}\n\n// ????????¨?????????????????????\nbool isis_ss(const Line& s, const Line& t) {\n\treturn ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 &&\n\t\tccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0;\n}\n\n// ????????´????????????\nbool isis_lp(const Line& l, const Point& p) {\n\treturn (abs(cross(l.b - p, l.a - p)) < eps);\n}\n\n// ?????????????????????\nbool isis_sp(const Line& s, const Point& p) {\n\treturn (abs(s.a - p) + abs(s.b - p) - abs(s.b - s.a) < eps);\n}\n\n// ??????????¶?\nPoint proj(const Line &l, const Point& p) {\n\tld t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n\treturn l.a + t * (l.a - l.b);\n}\n\n// ??´?????¨??´????????????\nPoint is_ll(const Line &s, const Line& t) {\n\tPoint sv = s.b - s.a, tv = t.b - t.a;\n\tassert(cross(sv, tv) != 0);\n\treturn s.a + sv * cross(tv, t.a - s.a) / cross(tv, sv);\n}\n// ??´?????¨??´????????????\nvector<Point> is_ll2(const Line &s, const Line& t) {\n\tPoint sv = s.b - s.a, tv = t.b - t.a;\n\tif (cross(sv, tv) != 0)return vector<Point>(1, is_ll(s, t));\n\telse {\n\t\tvector<Point>ans;\n\t\tfor (int k = 0; k < 2; ++k) {\n\t\t\tif (isis_sp(s, t[k]) && find(ans.begin(), ans.end(), t[k]) == ans.end())ans.push_back(t[k]);\n\t\t\tif (isis_sp(t, s[k]) && find(ans.begin(), ans.end(), s[k]) == ans.end())ans.push_back(s[k]);\n\t\t}\n\t\treturn ans;\n\t}\n}\n// ????????¨???????????????\n//???????????£????????¨???????????¨assert(false)\nPoint is_ss(const Line &s, const Line& t) {\n\tif (isis_ss(s, t)) {\n\t\tfor (int k = 0; k < 2; ++k) {\n\t\t\tfor (int l = 0; l < 2; ++l) {\n\t\t\t\tif (s[k] == t[l])return s[k];\n\t\t\t}\n\t\t}\n\t\treturn is_ll(s, t);\n\t}\n\telse {\n\t\t//??????isis_ss?????????\n\t\tassert(false);\n\t\treturn Point(0, 0);\n\t}\n}\n// ??´?????¨???????????¢\nld dist_lp(const Line& l, const Point& p) {\n\treturn abs(p - proj(l, p));\n}\n\n// ??´?????¨??´???????????¢\nld dist_ll(const Line& l, const Line& m) {\n\treturn isis_ll(l, m) ? 0 : dist_lp(l, m.a);\n}\n\n// ??´?????¨??????????????¢\nld dist_ls(const Line& l, const Line& s) {\n\treturn isis_ls(l, s) ? 0 : min(dist_lp(l, s.a), dist_lp(l, s.b));\n}\n\n// ????????¨???????????¢\nld dist_sp(const Line& s, const Point& p) {\n\tPoint r = proj(s, p);\n\treturn isis_sp(s, r) ? abs(r - p) : min(abs(s.a - p), abs(s.b - p));\n}\n\n// ????????¨??????????????¢\nld dist_ss(const Line& s, const Line& t) {\n\tif (isis_ss(s, t)) return 0;\n\treturn min({ dist_sp(s, t.a), dist_sp(s, t.b), dist_sp(t, s.a), dist_sp(t, s.b) });\n}\n\n\n//??´?????¨??´?????????????????????????????????\nLine bisection(const Line &s, const Line &t) {\n\tconst Point laglanju(is_ll(s, t));\n\tconst Point avec = !(abs(laglanju - s[0])<eps) ? s[0] - laglanju : s[1] - laglanju;\n\tconst Point bvec = !(abs(laglanju - t[0])<eps) ? t[0] - laglanju : t[1] - laglanju;\n\n\treturn Line(laglanju, laglanju + (abs(bvec)*avec + abs(avec)*bvec) / (abs(avec) + abs(bvec)));\n}\n\n\n//???????????´?????????????????????\n//???????????´??????????????§???????????¨????¢?????????¨?????????\nPoint inner_center(const vector<Line>&ls) {\n\tvector<Point>vertics;\n\tfor (int i = 0; i <static_cast<int>(ls.size()); ++i) {\n\t\tvertics.push_back(is_ll(ls[i], ls[(i + 1) % 3]));\n\t}\n\tif (vertics[0] == vertics[1] || vertics[1] == vertics[2] || vertics[2] == vertics[0])return vertics[0];\n\tLine bi1(bisection(Line(vertics[0], vertics[1]), Line(vertics[0], vertics[2])));\n\tLine bi2(bisection(Line(vertics[1], vertics[2]), Line(vertics[1], vertics[0])));\n\tif (bi1[0] == bi2[0])return bi1[0];\n\telse {\n\t\treturn is_ll(bi1, bi2);\n\t}\n}\n\n//???????????´?????????????????????\n//???????????´??????????????§???????????¨????¢?????????¨?????????\nvector<Point> ex_center(const vector<Line>&ls) {\n\tvector<Point>vertics;\n\tfor (int i = 0; i < static_cast<int>(ls.size()); ++i) {\n\t\tvertics.push_back(is_ll(ls[i], ls[(i + 1) % 3]));\n\t}\n\tif (abs(vertics[0] - vertics[1])<eps || abs(vertics[1] - vertics[2])<eps || (abs(vertics[2] - vertics[0])<eps))return vector<Point>();\n\tvector<Point>ecs;\n\tfor (int i = 0; i < 3; ++i) {\n\t\tLine bi1(bisection(Line(vertics[i], vertics[i] * 2.0l - vertics[(i + 2) % 3]), Line(vertics[i], vertics[(i + 1) % 3])));\n\t\tLine bi2(bisection(Line(vertics[(i + 1) % 3], vertics[(i + 1) % 3] * 2.0l - vertics[(i + 2) % 3]), Line(vertics[(i + 1) % 3], vertics[i])));\n\t\tecs.push_back(is_ll(bi1, bi2));\n\t}\n\treturn ecs;\n}\n\n\n//a,b:??????\n//c:????????§??????\n//???????????´?????????????????¢?????????????±??????????\nvector<Point> same_dis(const vector<Line>&ls) {\n\tvector<Point>vertics;\n\tvertics.push_back(is_ll(ls[0], ls[2]));\n\tvertics.push_back(is_ll(ls[1], ls[2]));\n\n\tif (abs(vertics[0] - vertics[1]) < eps)return vector<Point>{vertics[0]};\n\tLine bis(bisection(ls[0], ls[1]));\n\tvector<Point>ecs;\n\n\tLine abi(bisection(Line(vertics[0], vertics[1]), ls[0]));\n\tecs.push_back(is_ll(bis, abi));\n\n\n\tLine bbi(bisection(Line(vertics[0], 2.l*vertics[0] - vertics[1]), ls[0]));\n\tecs.push_back(is_ll(bis, bbi));\n\n\treturn ecs;\n}\n/* ??? */\n\n// ?????¨????????????\nvector<Point> is_cc(const Circle& c1, const Circle& c2) {\n\tvector<Point> res;\n\tld d = abs(c1.p - c2.p);\n\tld rc = (d * d + c1.r * c1.r - c2.r * c2.r) / (2 * d);\n\tld dfr = c1.r * c1.r - rc * rc;\n\tif (abs(dfr) < eps) dfr = 0.0;\n\telse if (dfr < 0.0) return res; // no intersection\n\tld rs = sqrt(dfr);\n\tPoint diff = (c2.p - c1.p) / d;\n\tres.push_back(c1.p + diff * Point(rc, rs));\n\tif (dfr != 0.0) res.push_back(c1.p + diff * Point(rc, -rs));\n\treturn res;\n}\n\n//???????????????????????????\n// 0 => out\n// 1 => on\n// 2 => in\nint is_in_circle(const Circle &cir, const Point& p) {\n\tld dis = abs(cir.p - p);\n\tif (dis > cir.r + eps)return 0;\n\telse if (dis < cir.r - eps)return 2;\n\telse return 1;\n}\n//???lc??????rc??????????????????\n// 0 => out\n// 1 => on\n// 2 => in\nint circle_in_circle(const Circle &lc, const Circle&rc) {\n\tld dis = abs(lc.p - rc.p);\n\tif (dis < rc.r - lc.r - eps)return 2;\n\telse if (dis>rc.r - lc.r + eps)return 0;\n\telse return 1;\n}\n\n// ?????¨??´????????????\nvector<Point> is_lc(const Circle& c, const Line& l) {\n\tvector<Point> res;\n\tld d = dist_lp(l, c.p);\n\tif (d < c.r + eps) {\n\t\tld len = (d > c.r) ? 0.0 : sqrt(c.r * c.r - d * d); //safety;\n\t\tPoint nor = (l.a - l.b) / abs(l.a - l.b);\n\t\tres.push_back(proj(l, c.p) + len * nor);\n\t\tres.push_back(proj(l, c.p) - len * nor);\n\t}\n\treturn res;\n}\n\n// ?????¨??????????????¢\nvector<Point> is_sc(const Circle& c, const Line& l) {\n\tvector<Point> v = is_lc(c, l), res;\n\tfor (Point p : v)\n\t\tif (isis_sp(l, p)) res.push_back(p);\n\treturn res;\n}\n\n// ?????¨????????\\???\nvector<Line> tangent_cp(const Circle& c, const Point& p) {\n\tvector<Line> ret;\n\tPoint v = c.p - p;\n\tld d = abs(v);\n\tld l = sqrt(norm(v) - c.r * c.r);\n\tif (isnan(l)) { return ret; }\n\tPoint v1 = v * Point(l / d, c.r / d);\n\tPoint v2 = v * Point(l / d, -c.r / d);\n\tret.push_back(Line(p, p + v1));\n\tif (l < eps) return ret;\n\tret.push_back(Line(p, p + v2));\n\treturn ret;\n}\n\n// ?????¨????????\\???\nvector<Line> tangent_cc(const Circle& c1, const Circle& c2) {\n\tvector<Line> ret;\n\tif (abs(c1.p - c2.p) - (c1.r + c2.r) > -eps) {\n\t\tPoint center = (c1.p * c2.r + c2.p * c1.r) / (c1.r + c2.r);\n\t\tret = tangent_cp(c1, center);\n\t}\n\tif (abs(c1.r - c2.r) > eps) {\n\t\tPoint out = (-c1.p * c2.r + c2.p * c1.r) / (c1.r - c2.r);\n\t\tvector<Line> nret = tangent_cp(c1, out);\n\t\tret.insert(ret.end(), ALL(nret));\n\t}\n\telse {\n\t\tPoint v = c2.p - c1.p;\n\t\tv /= abs(v);\n\t\tPoint q1 = c1.p + v * Point(0, 1) * c1.r;\n\t\tPoint q2 = c1.p + v * Point(0, -1) * c1.r;\n\t\tret.push_back(Line(q1, q1 + v));\n\t\tret.push_back(Line(q2, q2 + v));\n\t}\n\treturn ret;\n}\n//??????????????????????????¢???\nld two_circle_area(const Circle&l, const Circle&r) {\n\tld dis = abs(l.p - r.p);\n\tif (dis > l.r + r.r)return 0;\n\telse if (dis + r.r < l.r) {\n\t\treturn r.r*r.r*pi;\n\t}\n\telse if (dis + l.r < r.r) {\n\t\treturn l.r*l.r*pi;\n\t}\n\telse {\n\t\tld ans = (l.r)*(l.r)*acos((dis*dis + l.r*l.r - r.r*r.r) / (2 * dis*l.r)) +\n\t\t\t(r.r)*(r.r)*acos((dis*dis + r.r*r.r - l.r*l.r) / (2 * dis*r.r)) -\n\t\t\tsqrt(4 * dis*dis*l.r*l.r - (dis*dis + l.r*l.r - r.r*r.r)*(dis*dis + l.r*l.r - r.r*r.r)) / 2;\n\t\treturn ans;\n\t}\n\n}\n\n/* ????§???¢ */\n\ntypedef vector<Point> Polygon;\n\n// ??¢???\nld area(const Polygon &p) {\n\tld res = 0;\n\tint n = p.size();\n\tREP(j, n) res += cross(p[j], p[(j + 1) % n]);\n\treturn res / 2;\n}\n\n// ????§???¢????????¢??????\nbool is_counter_clockwise(const Polygon &poly) {\n\tld angle = 0;\n\tint n = poly.size();\n\tREP(i, n) {\n\t\tPoint a = poly[i], b = poly[(i + 1) % n], c = poly[(i + 2) % n];\n\t\tangle += arg((c - b) / (b - a));\n\t}\n\treturn angle > eps;\n}\n\n// ??????????????????\n// 0 => out\n// 1 => on\n// 2 => in\nint is_in_polygon(const Polygon &poly, const Point& p) {\n\tld angle = 0;\n\tint n = poly.size();\n\tREP(i, n) {\n\t\tPoint a = poly[i], b = poly[(i + 1) % n];\n\t\tif (isis_sp(Line(a, b), p)) return 1;\n\t\tangle += arg((b - p) / (a - p));\n\t}\n\treturn eq(angle, 0) ? 0 : 2;\n}\n//??????????????????2?????????\nenum { OUT, ON, IN };\nint convex_contains(const Polygon &P, const Point &p) {\n\tconst int n = P.size();\n\tPoint g = (P[0] + P[n / 3] + P[2 * n / 3]) / 3.0l; // inner-point\n\tint a = 0, b = n;\n\twhile (a + 1 < b) { // invariant: c is in fan g-P[a]-P[b]\n\t\tint c = (a + b) / 2;\n\t\tif (cross(P[a] - g, P[c] - g) > 0) { // angle < 180 deg\n\t\t\tif (cross(P[a] - g, p - g) > 0 && cross(P[c] - g, p - g) < 0) b = c;\n\t\t\telse a = c;\n\t\t}\n\t\telse {\n\t\t\tif (cross(P[a] - g, p - g) < 0 && cross(P[c] - g, p - g) > 0) a = c;\n\t\t\telse b = c;\n\t\t}\n\t}\n\tb %= n;\n\tif (cross(P[a] - p, P[b] - p) < 0) return 0;\n\tif (cross(P[a] - p, P[b] - p) > 0) return 2;\n\treturn 1;\n}\n\n// ??????\n// ???????????????????????¨????????????????????§??¨???\nPolygon convex_hull(vector<Point> ps) {\n\tint n = ps.size();\n\tint k = 0;\n\tsort(ps.begin(), ps.end());\n\tPolygon ch(2 * n);\n\tfor (int i = 0; i < n; ch[k++] = ps[i++])\n\t\twhile (k >= 2 && ccw(ch[k - 2], ch[k - 1], ps[i]) <= 0) --k;\n\tfor (int i = n - 2, t = k + 1; i >= 0; ch[k++] = ps[i--])\n\t\twhile (k >= t && ccw(ch[k - 2], ch[k - 1], ps[i]) <= 0) --k;\n\tch.resize(k - 1);\n\treturn ch;\n}\n\n\n\n// ????????????\nvector<Polygon> convex_cut(const Polygon &ps, const Line& l) {\n\tint n = ps.size();\n\tPolygon Q;\n\tPolygon R;\n\tREP(i, n) {\n\t\tPoint A = ps[i], B = ps[(i + 1) % n];\n\t\tLine m = Line(A, B);\n\t\tif (ccw(l.a, l.b, A) != -1) Q.push_back(A);\n\t\tif (ccw(l.a, l.b, A) != 1) R.push_back(A);\n\t\tif (ccw(l.a, l.b, A) * ccw(l.a, l.b, B) < 0 && isis_ll(l, m)) {\n\t\t\tQ.push_back(is_ll(l, m));\n\t\t\tR.push_back(is_ll(l, m));\n\t\t}\n\t}\n\tconst vector<Polygon>polys{ Q,R };\n\treturn polys;\n}\n\n\n/* ??¢??¬??????????????? */\nvoid add_point(vector<Point> &ps, const Point p) {\n\tfor (Point q : ps) if (abs(q - p) < eps) return;\n\tps.push_back(p);\n}\n\ntypedef int Weight;\nstruct Edge {\n\tint src, dst;\n\tWeight weight;\n\tEdge(int src, int dst, Weight weight) :\n\t\tsrc(src), dst(dst), weight(weight) { }\n};\n\ntypedef vector<Edge> Edges;\ntypedef vector<Edges> Graph;\n\nvoid add_edge(Graph &g, const int from, const int to, const Weight weight) {\n\tg[from].push_back(Edge{ from, to, weight });\n}\n\nGraph segment_arrangement(const vector<Line> &s, const vector<Point> &p) {\n\tint n = p.size(), m = s.size();\n\tGraph g(n);\n\tREP(i, m) {\n\t\tvector<pair<ld, int>> vec;\n\t\tREP(j, n) if (isis_sp(s[i], p[j]))\n\t\t\tvec.emplace_back(abs(s[i].a - p[j]), j);\n\t\tsort(ALL(vec));\n\t\tREP(j, vec.size() - 1) {\n\t\t\tint from = vec[j].second, to = vec[j + 1].second;\n\t\t\tadd_edge(g, from, to, static_cast<Weight>(abs(p[from] - p[to])));\n\t\t}\n\t}\n\treturn g;\n}\nGraph sennbunn_arrangement(const vector<Line>&s) {\n\tvector<Point>crss;\n\tfor (int i = 0; i < static_cast<int>(s.size()); ++i) {\n\t\tfor (int j = i + 1; j < static_cast<int>(s.size()); ++j) {\n\t\t\tif (isis_ss(s[i], s[j])) {\n\t\t\t\tcrss.push_back(is_ll(s[i], s[j]));\n\t\t\t}\n\t\t}\n\t}\n\tfor (int i = 0; i <static_cast<int>(s.size()); ++i) {\n\t\tcrss.push_back(s[i][0]);\n\t\tcrss.push_back(s[i][1]);\n\t}\n\treturn segment_arrangement(s, crss);\n}\n\n//Graph circle_arrangement(const vector<Circle> &c, const vector<Point> &p) {\n//\tint n = p.size(), m = c.size();\n//\tGraph g(n);\n//\tREP(i, m) {\n//\t\tvector<pair<ld, int>> vec;\n//\t\tREP(j, n) if (abs(abs(c[i].p - p[j]) - c[i].r) < eps)\n//\t\t\tvec.emplace_back(arg(c[i].p - p[j]), j);\n//\t\tsort(ALL(vec));\n//\t\tREP(j, vec.size() - 1) {\n//\t\t\tint from = vec[j].second, to = vec[j + 1].second;\n//\t\t\tld angle = vec[j + 1].first - vec[j].first;\n//\t\t\tadd_edge(g, from, to, static_cast<Weight>(angle * c[i].r));\n//\t\t}\n//\t\tif (vec.size() >= 2) {\n//\t\t\tint from = vec.back().second, to = vec.front().first;\n//\t\t\tld angle = vec.front().first - vec.back().first;\n//\t\t\tadd_edge(g, from, to, static_cast<Weight>(angle * c[i].r));\n//\t\t}\n//\t}\n//\treturn g;\n//}\n\n\n\nconst ld G= 9.8;\n\nstruct box {\n\tvector<ld>xs;\n\tvector<ld>ys;\n};\n\n\nint N, V, X, Y;\nbool check(const ld theta, const box& b) {\n\n\tconst ld vx = V*cos(theta);\n\tconst ld vy = V*sin(theta);\n\n\tld y_max = -1e18;\n\tld y_min = 1e18;\n\n\t//?????????????¢????\n\t{\n\t\tconst ld toptime = vy / G;\n\t\tconst ld top_x = toptime*vx;\n\t\tconst ld top_y = vy*toptime - toptime*toptime*G / 2;\n\t\tif (b.xs[0] < top_x&&top_x < b.xs[1]) {\n\t\t\ty_max = max(y_max, top_y);\n\t\t}\n\t}\n\t//???????????????????¢????\n\t{\n\t\tfor (int x = 0; x < 2; ++x) {\n\t\t\tconst ld time = b.xs[x]/vx;\n\t\t\tconst ld ay = vy*time - time*time*G / 2;\n\t\t\ty_max = max(y_max, ay);\n\t\t\ty_min = min(y_min, ay);\n\t\t}\n\t}\n\tif (y_max- eps < b.ys[0] || b.ys[1] < y_min + eps) {\n\t\t\n\t\treturn true;\n\t}\n\telse {\n\t\treturn false;\n\t}\n}\n\nvoid gettan(vector<ld>&tans,const Point& p,int leftup) {\n\tif (p.real() < eps)return;\n\tbool ok = true;\n\tld amin, amax;\n\n\t//?????????????¨????\n\t{\n\t\tconst ld tan = V*V / G / p.real();\n\t\tconst ld theta = atan(tan);\n\t\tconst ld vx = V*cos(theta);\n\t\tconst ld vy = V*sin(theta);\n\t\tconst ld time = p.real() / vx;\n\t\tconst ld max_y = vy*time - G*time*time / 2;\n\t\tif (max_y < p.imag()) {\n\t\t\tok = false;\n\t\t}\n\t\tif (leftup) {\n\t\t\tamax = tan;\n\t\t\tamin = 0;\n\t\t}\n\t\telse {\n\t\t\tamax = 1e18;\n\t\t\tamin = tan;\n\t\t}\n\t}\n\tif (ok) {\n\t\tint rep = 1000;\n\t\twhile (rep--) {\n\t\t\tconst ld amidtan = (amin + amax) / 2;\n\t\t\tconst ld theta = atan(amidtan);\n\t\t\tconst ld vx = V*cos(theta);\n\t\t\tconst ld vy = V*sin(theta);\n\t\t\tconst ld time = p.real() / vx;\n\t\t\tconst ld ay = vy*time - G*time*time / 2;\n\t\t\tif (ay > p.imag()) {\n\t\t\t\tif (leftup) {\n\t\t\t\t\tamax = amidtan;\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tamin = amidtan;\n\t\t\t\t}\n\t\t\t}\n\t\t\telse {\n\t\t\t\tif (leftup) {\n\t\t\t\t\tamin = amidtan;\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tamax = amidtan;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tconst ld amidtan = (amin + amax) / 2;\n\t\tconst ld theta = atan(amidtan);\n\t\tconst ld vx = V*cos(theta);\n\t\tconst ld vy = V*sin(theta);\n\t\tconst ld time = p.real() / vx;\n\t\tconst ld ay = vy*time - G*time*time / 2;\n\t\ttans.push_back(amin);\n\t}\n}\n\nint main() { cin >> N >> V >> X >> Y;\n\tvector<box>bs;\n\tvector<ld>tans;\n\tfor (int i = 0; i < N; ++i) {\n\t\tint L, B, R, T; cin >> L >> B >> R >> T;\n\n\t\tR = min(R, X);\n\t\tbox b;\n\t\tb.xs.push_back(L);\n\t\tb.xs.push_back(R);\n\t\tb.ys.push_back(B);\n\t\tb.ys.push_back(T);\n\t\tfor (int x = 0; x < 2; ++x) {\n\t\t\tfor (int y = 0; y < 2; ++y) {\n\t\t\t\tPoint p(b.xs[x], b.ys[y]);\n\t\t\t\tgettan(tans, p, (x + y) % 2);\n\t\t\t\t//gettan(tans, p, (x + y+1) % 2);\n\t\t\t}\n\t\t}\n\t\tif (L >= X)continue;\n\t\tbs.push_back(b);\n\t}\n\tconst ld down = Y;\n\tld up = 1e18;\n\tfor (int i = 0; i < bs.size(); ++i) {\n\t\tif (bs[i].xs[0] < X&&X < bs[i].xs[1]) {\n\t\t\tif (bs[i].ys[0] > Y) {\n\n\t\t\t\tup = min(up, bs[i].ys[0]);\n\t\t\t}\n\t\t}\n\t}\n\t\n\t{\n\t\tPoint p(X,Y);\n\t\tgettan(tans, p,true);\n\t}\n\t{\n\t\tfor (int y = 1; y <= 300; ++y) {\n\t\t\tif (V*V / 2 >= y) {\n\t\t\t\tld vy = sqrt(2 * y);\n\t\t\t\tif (vy > V)break;\n\t\t\t\tld vx = sqrt(V*V - vy*vy);\n\t\t\t\tld tan = vy / vx;\n\t\t\t\ttans.push_back(tan);\n\t\t\t}\n\t\t}\n\t}\n\tsort(tans.begin(), tans.end());\n\t{\n\t\tvector<ld>ntans;\n\t\tfor (int i = 0; i < tans.size(); ++i) {\n\t\t\tntans.push_back(tans[i]);\n\t\t\tif (i != tans.size() - 1) {\n\t\t\t\tntans.push_back((tans[i] + tans[i + 1]) / 2);\n\t\t\t}\n\t\t}\n\t\ttans = ntans;\n\t}\n\tstring ans = \"No\";\n\tfor (auto t : tans) {\n\t\tconst ld theta = atan(t);\n\t\tconst ld vx = V*cos(theta);\n\t\tconst ld vy = V*sin(theta);\n\t\tconst ld time = X / vx;\n\t\tconst ld ay = vy*time - G*time*time / 2;\n\t\tif (down-eps <= ay&&ay <= up+eps) {\n\t\t\tbool ok = true;\n\t\t\tfor (int i = 0; i < bs.size(); ++i) {\n\t\t\t\tauto b = bs[i];\n\n\t\t\t\tif (!check(theta, b)) {\n\t\t\t\t\tok = false;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (ok) {\n\n\t\t\t\tconst ld vx = V*cos(theta);\n\t\t\t\tconst ld vy = V*sin(theta);\n\t\t\t\t//cout << \"vx:\" << vx << \" vy:\" << vy << endl;\n\t\t\t\tfor (int x = 0; x <=100; ++x) {\n\t\t\t\t\tconst ld t = x / vx;\n\t\t\t\t\t//cout << \"x:\" << x << endl;\n\t\t\t\t\t//cout << \"y:\" << vy*t - G*t*t / 2 << endl;\n\t\t\t\t}\t\n\t\t\t\tans = \"Yes\";\n\t\t\t}\n\t\t}\n\t\telse {\n\t\t\tcontinue;\n\t\t}\n\t}\n\tcout << ans << endl;\n\treturn 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3568, "score_of_the_acc": -0.8933, "final_rank": 2 }, { "submission_id": "aoj_2308_1777433", "code_snippet": "#include \"bits/stdc++.h\"\n#include<unordered_map>\n#include<unordered_set>\n#pragma warning(disable:4996)\nusing namespace std;\nusing ld = long double;\ntemplate<class T>\nusing Table = vector<vector<T>>;\nconst ld eps=1e-9;\n\n//// < \"D:\\D_Download\\Visual Studio 2015\\Projects\\programing_contest_c++\\Debug\\a.txt\"\n\n\n/* ??????????????¬ */\n\n#include <complex>\n\ntypedef long double ld;\ntypedef complex<ld> Point;\n#define REP(i,n) for(int i=0;i<(int)(n);i++)\n#define ALL(x) (x).begin(),(x).end()\n\n\nconst ld pi = acos(-1.0);\nconst ld dtop = pi / 180.;\nconst ld ptod = 1. / dtop;\n\nnamespace std {\n\tbool operator<(const Point &lhs, const Point &rhs) {\n\t\tif (lhs.real() < rhs.real() - eps) return true;\n\t\tif (lhs.real() > rhs.real() + eps) return false;\n\t\treturn lhs.imag() < rhs.imag();\n\t}\n}\n\n// ????????\\???\nPoint input_point() {\n\tld x, y;\n\tcin >> x >> y;\n\treturn Point(x, y);\n}\n\n// ????????????????????????\nbool eq(const ld a, const ld b) {\n\treturn (abs(a - b) < eps);\n}\n\n// ??????\nld dot(const Point& a, const Point& b) {\n\treturn real(conj(a) * b);\n}\n\n// ??????\nld cross(const Point& a, const Point& b) {\n\treturn imag(conj(a) * b);\n}\n\n// ??´????????????\nclass Line {\npublic:\n\tPoint a, b;\n\tLine() : a(Point(0, 0)), b(Point(0, 0)) {}\n\tLine(Point a, Point b) : a(a), b(b) {}\n\tPoint operator[](const int _num)const {\n\t\tif (_num == 0)return a;\n\t\telse if (_num == 1)return b;\n\t\telse {\n\t\t\tassert(false);\n\t\t\treturn Point();\n\t\t}\n\t}\n};\n\n// ????????????\nclass Circle {\npublic:\n\tPoint p;\n\tld r;\n\tCircle() : p(Point(0, 0)), r(0) {}\n\tCircle(Point p, ld r) : p(p), r(r) {}\n};\n\n// CCW\nint ccw(const Point& a, const Point &b, const Point &c) {\n\tconst Point nb(b - a);\n\tconst Point nc(c - a);\n\tif (cross(nb, nc) > eps) return 1; // a,b,c??????????¨???¨?????????????????¶\n\tif (cross(nb, nc) < -eps) return -1; // a,b,c???????¨???¨?????????????????¶\n\tif (dot(nb, nc) < 0) return 2; // c,a,b???????????´???????????¶\n\tif (norm(nb) < norm(nc)) return -2; // a,b,c???????????´???????????¶\n\treturn 0; // a,c,b???????????´???????????¶\n}\n\n\n/* ???????????? */\n\n// ??´?????¨??´??????????????????\nbool isis_ll(const Line& l, const Line& m) {\n\treturn !eq(cross(l.b - l.a, m.b - m.a), 0);\n}\n\n// ??´?????¨?????????????????????\nbool isis_ls(const Line& l, const Line& s) {\n\treturn isis_ll(l, s) &&\n\t\t(cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < eps);\n}\n\n// ????????¨?????????????????????\nbool isis_ss(const Line& s, const Line& t) {\n\treturn ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 &&\n\t\tccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0;\n}\n\n// ????????´????????????\nbool isis_lp(const Line& l, const Point& p) {\n\treturn (abs(cross(l.b - p, l.a - p)) < eps);\n}\n\n// ?????????????????????\nbool isis_sp(const Line& s, const Point& p) {\n\treturn (abs(s.a - p) + abs(s.b - p) - abs(s.b - s.a) < eps);\n}\n\n// ??????????¶?\nPoint proj(const Line &l, const Point& p) {\n\tld t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n\treturn l.a + t * (l.a - l.b);\n}\n\n// ??´?????¨??´????????????\nPoint is_ll(const Line &s, const Line& t) {\n\tPoint sv = s.b - s.a, tv = t.b - t.a;\n\tassert(cross(sv, tv) != 0);\n\treturn s.a + sv * cross(tv, t.a - s.a) / cross(tv, sv);\n}\n// ??´?????¨??´????????????\nvector<Point> is_ll2(const Line &s, const Line& t) {\n\tPoint sv = s.b - s.a, tv = t.b - t.a;\n\tif (cross(sv, tv) != 0)return vector<Point>(1, is_ll(s, t));\n\telse {\n\t\tvector<Point>ans;\n\t\tfor (int k = 0; k < 2; ++k) {\n\t\t\tif (isis_sp(s, t[k]) && find(ans.begin(), ans.end(), t[k]) == ans.end())ans.push_back(t[k]);\n\t\t\tif (isis_sp(t, s[k]) && find(ans.begin(), ans.end(), s[k]) == ans.end())ans.push_back(s[k]);\n\t\t}\n\t\treturn ans;\n\t}\n}\n// ????????¨???????????????\n//???????????£????????¨???????????¨assert(false)\nPoint is_ss(const Line &s, const Line& t) {\n\tif (isis_ss(s, t)) {\n\t\tfor (int k = 0; k < 2; ++k) {\n\t\t\tfor (int l = 0; l < 2; ++l) {\n\t\t\t\tif (s[k] == t[l])return s[k];\n\t\t\t}\n\t\t}\n\t\treturn is_ll(s, t);\n\t}\n\telse {\n\t\t//??????isis_ss?????????\n\t\tassert(false);\n\t\treturn Point(0, 0);\n\t}\n}\n// ??´?????¨???????????¢\nld dist_lp(const Line& l, const Point& p) {\n\treturn abs(p - proj(l, p));\n}\n\n// ??´?????¨??´???????????¢\nld dist_ll(const Line& l, const Line& m) {\n\treturn isis_ll(l, m) ? 0 : dist_lp(l, m.a);\n}\n\n// ??´?????¨??????????????¢\nld dist_ls(const Line& l, const Line& s) {\n\treturn isis_ls(l, s) ? 0 : min(dist_lp(l, s.a), dist_lp(l, s.b));\n}\n\n// ????????¨???????????¢\nld dist_sp(const Line& s, const Point& p) {\n\tPoint r = proj(s, p);\n\treturn isis_sp(s, r) ? abs(r - p) : min(abs(s.a - p), abs(s.b - p));\n}\n\n// ????????¨??????????????¢\nld dist_ss(const Line& s, const Line& t) {\n\tif (isis_ss(s, t)) return 0;\n\treturn min({ dist_sp(s, t.a), dist_sp(s, t.b), dist_sp(t, s.a), dist_sp(t, s.b) });\n}\n\n\n//??´?????¨??´?????????????????????????????????\nLine bisection(const Line &s, const Line &t) {\n\tconst Point laglanju(is_ll(s, t));\n\tconst Point avec = !(abs(laglanju - s[0])<eps) ? s[0] - laglanju : s[1] - laglanju;\n\tconst Point bvec = !(abs(laglanju - t[0])<eps) ? t[0] - laglanju : t[1] - laglanju;\n\n\treturn Line(laglanju, laglanju + (abs(bvec)*avec + abs(avec)*bvec) / (abs(avec) + abs(bvec)));\n}\n\n\n//???????????´?????????????????????\n//???????????´??????????????§???????????¨????¢?????????¨?????????\nPoint inner_center(const vector<Line>&ls) {\n\tvector<Point>vertics;\n\tfor (int i = 0; i <static_cast<int>(ls.size()); ++i) {\n\t\tvertics.push_back(is_ll(ls[i], ls[(i + 1) % 3]));\n\t}\n\tif (vertics[0] == vertics[1] || vertics[1] == vertics[2] || vertics[2] == vertics[0])return vertics[0];\n\tLine bi1(bisection(Line(vertics[0], vertics[1]), Line(vertics[0], vertics[2])));\n\tLine bi2(bisection(Line(vertics[1], vertics[2]), Line(vertics[1], vertics[0])));\n\tif (bi1[0] == bi2[0])return bi1[0];\n\telse {\n\t\treturn is_ll(bi1, bi2);\n\t}\n}\n\n//???????????´?????????????????????\n//???????????´??????????????§???????????¨????¢?????????¨?????????\nvector<Point> ex_center(const vector<Line>&ls) {\n\tvector<Point>vertics;\n\tfor (int i = 0; i < static_cast<int>(ls.size()); ++i) {\n\t\tvertics.push_back(is_ll(ls[i], ls[(i + 1) % 3]));\n\t}\n\tif (abs(vertics[0] - vertics[1])<eps || abs(vertics[1] - vertics[2])<eps || (abs(vertics[2] - vertics[0])<eps))return vector<Point>();\n\tvector<Point>ecs;\n\tfor (int i = 0; i < 3; ++i) {\n\t\tLine bi1(bisection(Line(vertics[i], vertics[i] * 2.0l - vertics[(i + 2) % 3]), Line(vertics[i], vertics[(i + 1) % 3])));\n\t\tLine bi2(bisection(Line(vertics[(i + 1) % 3], vertics[(i + 1) % 3] * 2.0l - vertics[(i + 2) % 3]), Line(vertics[(i + 1) % 3], vertics[i])));\n\t\tecs.push_back(is_ll(bi1, bi2));\n\t}\n\treturn ecs;\n}\n\n\n//a,b:??????\n//c:????????§??????\n//???????????´?????????????????¢?????????????±??????????\nvector<Point> same_dis(const vector<Line>&ls) {\n\tvector<Point>vertics;\n\tvertics.push_back(is_ll(ls[0], ls[2]));\n\tvertics.push_back(is_ll(ls[1], ls[2]));\n\n\tif (abs(vertics[0] - vertics[1]) < eps)return vector<Point>{vertics[0]};\n\tLine bis(bisection(ls[0], ls[1]));\n\tvector<Point>ecs;\n\n\tLine abi(bisection(Line(vertics[0], vertics[1]), ls[0]));\n\tecs.push_back(is_ll(bis, abi));\n\n\n\tLine bbi(bisection(Line(vertics[0], 2.l*vertics[0] - vertics[1]), ls[0]));\n\tecs.push_back(is_ll(bis, bbi));\n\n\treturn ecs;\n}\n/* ??? */\n\n// ?????¨????????????\nvector<Point> is_cc(const Circle& c1, const Circle& c2) {\n\tvector<Point> res;\n\tld d = abs(c1.p - c2.p);\n\tld rc = (d * d + c1.r * c1.r - c2.r * c2.r) / (2 * d);\n\tld dfr = c1.r * c1.r - rc * rc;\n\tif (abs(dfr) < eps) dfr = 0.0;\n\telse if (dfr < 0.0) return res; // no intersection\n\tld rs = sqrt(dfr);\n\tPoint diff = (c2.p - c1.p) / d;\n\tres.push_back(c1.p + diff * Point(rc, rs));\n\tif (dfr != 0.0) res.push_back(c1.p + diff * Point(rc, -rs));\n\treturn res;\n}\n\n//???????????????????????????\n// 0 => out\n// 1 => on\n// 2 => in\nint is_in_circle(const Circle &cir, const Point& p) {\n\tld dis = abs(cir.p - p);\n\tif (dis > cir.r + eps)return 0;\n\telse if (dis < cir.r - eps)return 2;\n\telse return 1;\n}\n//???lc??????rc??????????????????\n// 0 => out\n// 1 => on\n// 2 => in\nint circle_in_circle(const Circle &lc, const Circle&rc) {\n\tld dis = abs(lc.p - rc.p);\n\tif (dis < rc.r - lc.r - eps)return 2;\n\telse if (dis>rc.r - lc.r + eps)return 0;\n\telse return 1;\n}\n\n// ?????¨??´????????????\nvector<Point> is_lc(const Circle& c, const Line& l) {\n\tvector<Point> res;\n\tld d = dist_lp(l, c.p);\n\tif (d < c.r + eps) {\n\t\tld len = (d > c.r) ? 0.0 : sqrt(c.r * c.r - d * d); //safety;\n\t\tPoint nor = (l.a - l.b) / abs(l.a - l.b);\n\t\tres.push_back(proj(l, c.p) + len * nor);\n\t\tres.push_back(proj(l, c.p) - len * nor);\n\t}\n\treturn res;\n}\n\n// ?????¨??????????????¢\nvector<Point> is_sc(const Circle& c, const Line& l) {\n\tvector<Point> v = is_lc(c, l), res;\n\tfor (Point p : v)\n\t\tif (isis_sp(l, p)) res.push_back(p);\n\treturn res;\n}\n\n// ?????¨????????\\???\nvector<Line> tangent_cp(const Circle& c, const Point& p) {\n\tvector<Line> ret;\n\tPoint v = c.p - p;\n\tld d = abs(v);\n\tld l = sqrt(norm(v) - c.r * c.r);\n\tif (isnan(l)) { return ret; }\n\tPoint v1 = v * Point(l / d, c.r / d);\n\tPoint v2 = v * Point(l / d, -c.r / d);\n\tret.push_back(Line(p, p + v1));\n\tif (l < eps) return ret;\n\tret.push_back(Line(p, p + v2));\n\treturn ret;\n}\n\n// ?????¨????????\\???\nvector<Line> tangent_cc(const Circle& c1, const Circle& c2) {\n\tvector<Line> ret;\n\tif (abs(c1.p - c2.p) - (c1.r + c2.r) > -eps) {\n\t\tPoint center = (c1.p * c2.r + c2.p * c1.r) / (c1.r + c2.r);\n\t\tret = tangent_cp(c1, center);\n\t}\n\tif (abs(c1.r - c2.r) > eps) {\n\t\tPoint out = (-c1.p * c2.r + c2.p * c1.r) / (c1.r - c2.r);\n\t\tvector<Line> nret = tangent_cp(c1, out);\n\t\tret.insert(ret.end(), ALL(nret));\n\t}\n\telse {\n\t\tPoint v = c2.p - c1.p;\n\t\tv /= abs(v);\n\t\tPoint q1 = c1.p + v * Point(0, 1) * c1.r;\n\t\tPoint q2 = c1.p + v * Point(0, -1) * c1.r;\n\t\tret.push_back(Line(q1, q1 + v));\n\t\tret.push_back(Line(q2, q2 + v));\n\t}\n\treturn ret;\n}\n//??????????????????????????¢???\nld two_circle_area(const Circle&l, const Circle&r) {\n\tld dis = abs(l.p - r.p);\n\tif (dis > l.r + r.r)return 0;\n\telse if (dis + r.r < l.r) {\n\t\treturn r.r*r.r*pi;\n\t}\n\telse if (dis + l.r < r.r) {\n\t\treturn l.r*l.r*pi;\n\t}\n\telse {\n\t\tld ans = (l.r)*(l.r)*acos((dis*dis + l.r*l.r - r.r*r.r) / (2 * dis*l.r)) +\n\t\t\t(r.r)*(r.r)*acos((dis*dis + r.r*r.r - l.r*l.r) / (2 * dis*r.r)) -\n\t\t\tsqrt(4 * dis*dis*l.r*l.r - (dis*dis + l.r*l.r - r.r*r.r)*(dis*dis + l.r*l.r - r.r*r.r)) / 2;\n\t\treturn ans;\n\t}\n\n}\n\n/* ????§???¢ */\n\ntypedef vector<Point> Polygon;\n\n// ??¢???\nld area(const Polygon &p) {\n\tld res = 0;\n\tint n = p.size();\n\tREP(j, n) res += cross(p[j], p[(j + 1) % n]);\n\treturn res / 2;\n}\n\n// ????§???¢????????¢??????\nbool is_counter_clockwise(const Polygon &poly) {\n\tld angle = 0;\n\tint n = poly.size();\n\tREP(i, n) {\n\t\tPoint a = poly[i], b = poly[(i + 1) % n], c = poly[(i + 2) % n];\n\t\tangle += arg((c - b) / (b - a));\n\t}\n\treturn angle > eps;\n}\n\n// ??????????????????\n// 0 => out\n// 1 => on\n// 2 => in\nint is_in_polygon(const Polygon &poly, const Point& p) {\n\tld angle = 0;\n\tint n = poly.size();\n\tREP(i, n) {\n\t\tPoint a = poly[i], b = poly[(i + 1) % n];\n\t\tif (isis_sp(Line(a, b), p)) return 1;\n\t\tangle += arg((b - p) / (a - p));\n\t}\n\treturn eq(angle, 0) ? 0 : 2;\n}\n//??????????????????2?????????\nenum { OUT, ON, IN };\nint convex_contains(const Polygon &P, const Point &p) {\n\tconst int n = P.size();\n\tPoint g = (P[0] + P[n / 3] + P[2 * n / 3]) / 3.0l; // inner-point\n\tint a = 0, b = n;\n\twhile (a + 1 < b) { // invariant: c is in fan g-P[a]-P[b]\n\t\tint c = (a + b) / 2;\n\t\tif (cross(P[a] - g, P[c] - g) > 0) { // angle < 180 deg\n\t\t\tif (cross(P[a] - g, p - g) > 0 && cross(P[c] - g, p - g) < 0) b = c;\n\t\t\telse a = c;\n\t\t}\n\t\telse {\n\t\t\tif (cross(P[a] - g, p - g) < 0 && cross(P[c] - g, p - g) > 0) a = c;\n\t\t\telse b = c;\n\t\t}\n\t}\n\tb %= n;\n\tif (cross(P[a] - p, P[b] - p) < 0) return 0;\n\tif (cross(P[a] - p, P[b] - p) > 0) return 2;\n\treturn 1;\n}\n\n// ??????\n// ???????????????????????¨????????????????????§??¨???\nPolygon convex_hull(vector<Point> ps) {\n\tint n = ps.size();\n\tint k = 0;\n\tsort(ps.begin(), ps.end());\n\tPolygon ch(2 * n);\n\tfor (int i = 0; i < n; ch[k++] = ps[i++])\n\t\twhile (k >= 2 && ccw(ch[k - 2], ch[k - 1], ps[i]) <= 0) --k;\n\tfor (int i = n - 2, t = k + 1; i >= 0; ch[k++] = ps[i--])\n\t\twhile (k >= t && ccw(ch[k - 2], ch[k - 1], ps[i]) <= 0) --k;\n\tch.resize(k - 1);\n\treturn ch;\n}\n\n\n\n// ????????????\nvector<Polygon> convex_cut(const Polygon &ps, const Line& l) {\n\tint n = ps.size();\n\tPolygon Q;\n\tPolygon R;\n\tREP(i, n) {\n\t\tPoint A = ps[i], B = ps[(i + 1) % n];\n\t\tLine m = Line(A, B);\n\t\tif (ccw(l.a, l.b, A) != -1) Q.push_back(A);\n\t\tif (ccw(l.a, l.b, A) != 1) R.push_back(A);\n\t\tif (ccw(l.a, l.b, A) * ccw(l.a, l.b, B) < 0 && isis_ll(l, m)) {\n\t\t\tQ.push_back(is_ll(l, m));\n\t\t\tR.push_back(is_ll(l, m));\n\t\t}\n\t}\n\tconst vector<Polygon>polys{ Q,R };\n\treturn polys;\n}\n\n\n/* ??¢??¬??????????????? */\nvoid add_point(vector<Point> &ps, const Point p) {\n\tfor (Point q : ps) if (abs(q - p) < eps) return;\n\tps.push_back(p);\n}\n\ntypedef int Weight;\nstruct Edge {\n\tint src, dst;\n\tWeight weight;\n\tEdge(int src, int dst, Weight weight) :\n\t\tsrc(src), dst(dst), weight(weight) { }\n};\n\ntypedef vector<Edge> Edges;\ntypedef vector<Edges> Graph;\n\nvoid add_edge(Graph &g, const int from, const int to, const Weight weight) {\n\tg[from].push_back(Edge{ from, to, weight });\n}\n\nGraph segment_arrangement(const vector<Line> &s, const vector<Point> &p) {\n\tint n = p.size(), m = s.size();\n\tGraph g(n);\n\tREP(i, m) {\n\t\tvector<pair<ld, int>> vec;\n\t\tREP(j, n) if (isis_sp(s[i], p[j]))\n\t\t\tvec.emplace_back(abs(s[i].a - p[j]), j);\n\t\tsort(ALL(vec));\n\t\tREP(j, vec.size() - 1) {\n\t\t\tint from = vec[j].second, to = vec[j + 1].second;\n\t\t\tadd_edge(g, from, to, static_cast<Weight>(abs(p[from] - p[to])));\n\t\t}\n\t}\n\treturn g;\n}\nGraph sennbunn_arrangement(const vector<Line>&s) {\n\tvector<Point>crss;\n\tfor (int i = 0; i < static_cast<int>(s.size()); ++i) {\n\t\tfor (int j = i + 1; j < static_cast<int>(s.size()); ++j) {\n\t\t\tif (isis_ss(s[i], s[j])) {\n\t\t\t\tcrss.push_back(is_ll(s[i], s[j]));\n\t\t\t}\n\t\t}\n\t}\n\tfor (int i = 0; i <static_cast<int>(s.size()); ++i) {\n\t\tcrss.push_back(s[i][0]);\n\t\tcrss.push_back(s[i][1]);\n\t}\n\treturn segment_arrangement(s, crss);\n}\n\n//Graph circle_arrangement(const vector<Circle> &c, const vector<Point> &p) {\n//\tint n = p.size(), m = c.size();\n//\tGraph g(n);\n//\tREP(i, m) {\n//\t\tvector<pair<ld, int>> vec;\n//\t\tREP(j, n) if (abs(abs(c[i].p - p[j]) - c[i].r) < eps)\n//\t\t\tvec.emplace_back(arg(c[i].p - p[j]), j);\n//\t\tsort(ALL(vec));\n//\t\tREP(j, vec.size() - 1) {\n//\t\t\tint from = vec[j].second, to = vec[j + 1].second;\n//\t\t\tld angle = vec[j + 1].first - vec[j].first;\n//\t\t\tadd_edge(g, from, to, static_cast<Weight>(angle * c[i].r));\n//\t\t}\n//\t\tif (vec.size() >= 2) {\n//\t\t\tint from = vec.back().second, to = vec.front().first;\n//\t\t\tld angle = vec.front().first - vec.back().first;\n//\t\t\tadd_edge(g, from, to, static_cast<Weight>(angle * c[i].r));\n//\t\t}\n//\t}\n//\treturn g;\n//}\n\n\n\nconst ld G= 9.8;\n\nstruct box {\n\tvector<ld>xs;\n\tvector<ld>ys;\n};\n\n\nint N, V, X, Y;\nbool check(const ld theta, const box& b) {\n\n\tconst ld vx = V*cos(theta);\n\tconst ld vy = V*sin(theta);\n\n\tld y_max = -1e18;\n\tld y_min = 1e18;\n\n\t//?????????????¢????\n\t{\n\t\tconst ld toptime = vy / G;\n\t\tconst ld top_x = toptime*vx;\n\t\tconst ld top_y = vy*toptime - toptime*toptime*G / 2;\n\t\tif (b.xs[0] < top_x&&top_x < b.xs[1]) {\n\t\t\ty_max = max(y_max, top_y);\n\t\t}\n\t}\n\t//???????????????????¢????\n\t{\n\t\tfor (int x = 0; x < 2; ++x) {\n\t\t\tconst ld time = b.xs[x]/vx;\n\t\t\tconst ld ay = vy*time - time*time*G / 2;\n\t\t\ty_max = max(y_max, ay);\n\t\t\ty_min = min(y_min, ay);\n\t\t}\n\t}\n\tif (y_max- eps < b.ys[0] || b.ys[1] < y_min + eps) {\n\t\t\n\t\treturn true;\n\t}\n\telse {\n\t\treturn false;\n\t}\n}\n\nvoid gettan(vector<ld>&tans,const Point& p,int leftup) {\n\tif (p.real() < eps)return;\n\tbool ok = true;\n\tld amin, amax;\n\n\t//?????????????¨????\n\t{\n\t\tconst ld tan = V*V / G / p.real();\n\t\tconst ld theta = atan(tan);\n\t\tconst ld vx = V*cos(theta);\n\t\tconst ld vy = V*sin(theta);\n\t\tconst ld time = p.real() / vx;\n\t\tconst ld max_y = vy*time - G*time*time / 2;\n\t\tif (max_y < p.imag()) {\n\t\t\tok = false;\n\t\t}\n\t\tif (leftup) {\n\t\t\tamax = tan;\n\t\t\tamin = 0;\n\t\t}\n\t\telse {\n\t\t\tamax = 1e18;\n\t\t\tamin = tan;\n\t\t}\n\t}\n\tif (ok) {\n\t\tint rep = 1000;\n\t\twhile (rep--) {\n\t\t\tconst ld amidtan = (amin + amax) / 2;\n\t\t\tconst ld theta = atan(amidtan);\n\t\t\tconst ld vx = V*cos(theta);\n\t\t\tconst ld vy = V*sin(theta);\n\t\t\tconst ld time = p.real() / vx;\n\t\t\tconst ld ay = vy*time - G*time*time / 2;\n\t\t\tif (ay > p.imag()) {\n\t\t\t\tif (leftup) {\n\t\t\t\t\tamax = amidtan;\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tamin = amidtan;\n\t\t\t\t}\n\t\t\t}\n\t\t\telse {\n\t\t\t\tif (leftup) {\n\t\t\t\t\tamin = amidtan;\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tamax = amidtan;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tconst ld amidtan = (amin + amax) / 2;\n\t\tconst ld theta = atan(amidtan);\n\t\tconst ld vx = V*cos(theta);\n\t\tconst ld vy = V*sin(theta);\n\t\tconst ld time = p.real() / vx;\n\t\tconst ld ay = vy*time - G*time*time / 2;\n\t\ttans.push_back(amin);\n\t}\n}\n\nint main() { cin >> N >> V >> X >> Y;\n\tvector<box>bs;\n\tvector<ld>tans;\n\tfor (int i = 0; i < N; ++i) {\n\t\tint L, B, R, T; cin >> L >> B >> R >> T;\n\n\t\tR = min(R, X);\n\t\tbox b;\n\t\tb.xs.push_back(L);\n\t\tb.xs.push_back(R);\n\t\tb.ys.push_back(B);\n\t\tb.ys.push_back(T);\n\t\tfor (int x = 0; x < 2; ++x) {\n\t\t\tfor (int y = 0; y < 2; ++y) {\n\t\t\t\tPoint p(b.xs[x], b.ys[y]);\n\t\t\t\tgettan(tans, p, (x + y) % 2);\n\t\t\t\t//gettan(tans, p, (x + y+1) % 2);\n\t\t\t}\n\t\t}\n\t\t//if (L >= X)continue;\n\t\tbs.push_back(b);\n\t}\n\tconst ld down = Y;\n\tld up = 1e18;\n\tfor (int i = 0; i < bs.size(); ++i) {\n\t\tif (bs[i].xs[0] < X&&X < bs[i].xs[1]) {\n\t\t\tif (bs[i].ys[0] > Y) {\n\n\t\t\t\tup = min(up, bs[i].ys[0]);\n\t\t\t}\n\t\t}\n\t}\n\t\n\t{\n\t\tPoint p(X,Y);\n\t\tgettan(tans, p,true);\n\t}\n\t{\n\t\tfor (int y = 1; y <= 300; ++y) {\n\t\t\tif (V*V / 2 >= y) {\n\t\t\t\tld vy = sqrt(2 * y);\n\t\t\t\tif (vy > V)break;\n\t\t\t\tld vx = sqrt(V*V - vy*vy);\n\t\t\t\tld tan = vy / vx;\n\t\t\t\ttans.push_back(tan);\n\t\t\t}\n\t\t}\n\t}\n\tsort(tans.begin(), tans.end());\n\t{\n\t\tvector<ld>ntans;\n\t\tfor (int i = 0; i < tans.size(); ++i) {\n\t\t\tntans.push_back(tans[i]);\n\t\t\tif (i != tans.size() - 1) {\n\t\t\t\tntans.push_back((tans[i] + tans[i + 1]) / 2);\n\t\t\t}\n\t\t}\n\t\ttans = ntans;\n\t}\n\tstring ans = \"No\";\n\tfor (auto t : tans) {\n\t\tconst ld theta = atan(t);\n\t\tconst ld vx = V*cos(theta);\n\t\tconst ld vy = V*sin(theta);\n\t\tconst ld time = X / vx;\n\t\tconst ld ay = vy*time - G*time*time / 2;\n\t\tif (down-eps <= ay&&ay <= up+eps) {\n\t\t\tbool ok = true;\n\t\t\tfor (int i = 0; i < bs.size(); ++i) {\n\t\t\t\tauto b = bs[i];\n\n\t\t\t\tif (!check(theta, b)) {\n\t\t\t\t\tok = false;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (ok) {\n\n\t\t\t\tconst ld vx = V*cos(theta);\n\t\t\t\tconst ld vy = V*sin(theta);\n\t\t\t\t//cout << \"vx:\" << vx << \" vy:\" << vy << endl;\n\t\t\t\tfor (int x = 0; x <=100; ++x) {\n\t\t\t\t\tconst ld t = x / vx;\n\t\t\t\t\t//cout << \"x:\" << x << endl;\n\t\t\t\t\t//cout << \"y:\" << vy*t - G*t*t / 2 << endl;\n\t\t\t\t}\t\n\t\t\t\tans = \"Yes\";\n\t\t\t}\n\t\t}\n\t\telse {\n\t\t\tcontinue;\n\t\t}\n\t}\n\tcout << ans << endl;\n\treturn 0;\n}", "accuracy": 0.2, "time_ms": 30, "memory_kb": 3580, "score_of_the_acc": -0.8979, "final_rank": 15 }, { "submission_id": "aoj_2308_1777230", "code_snippet": "#include \"bits/stdc++.h\"\n#include<unordered_map>\n#include<unordered_set>\n#pragma warning(disable:4996)\nusing namespace std;\nusing ld = long double;\ntemplate<class T>\nusing Table = vector<vector<T>>;\nconst ld eps=1e-9;\n\n//// < \"D:\\D_Download\\Visual Studio 2015\\Projects\\programing_contest_c++\\Debug\\a.txt\"\n\n\n/* ??????????????¬ */\n\n#include <complex>\n\ntypedef long double ld;\ntypedef complex<ld> Point;\n#define REP(i,n) for(int i=0;i<(int)(n);i++)\n#define ALL(x) (x).begin(),(x).end()\n\n\nconst ld pi = acos(-1.0);\nconst ld dtop = pi / 180.;\nconst ld ptod = 1. / dtop;\n\nnamespace std {\n\tbool operator<(const Point &lhs, const Point &rhs) {\n\t\tif (lhs.real() < rhs.real() - eps) return true;\n\t\tif (lhs.real() > rhs.real() + eps) return false;\n\t\treturn lhs.imag() < rhs.imag();\n\t}\n}\n\n// ????????\\???\nPoint input_point() {\n\tld x, y;\n\tcin >> x >> y;\n\treturn Point(x, y);\n}\n\n// ????????????????????????\nbool eq(const ld a, const ld b) {\n\treturn (abs(a - b) < eps);\n}\n\n// ??????\nld dot(const Point& a, const Point& b) {\n\treturn real(conj(a) * b);\n}\n\n// ??????\nld cross(const Point& a, const Point& b) {\n\treturn imag(conj(a) * b);\n}\n\n// ??´????????????\nclass Line {\npublic:\n\tPoint a, b;\n\tLine() : a(Point(0, 0)), b(Point(0, 0)) {}\n\tLine(Point a, Point b) : a(a), b(b) {}\n\tPoint operator[](const int _num)const {\n\t\tif (_num == 0)return a;\n\t\telse if (_num == 1)return b;\n\t\telse {\n\t\t\tassert(false);\n\t\t\treturn Point();\n\t\t}\n\t}\n};\n\n// ????????????\nclass Circle {\npublic:\n\tPoint p;\n\tld r;\n\tCircle() : p(Point(0, 0)), r(0) {}\n\tCircle(Point p, ld r) : p(p), r(r) {}\n};\n\n// CCW\nint ccw(const Point& a, const Point &b, const Point &c) {\n\tconst Point nb(b - a);\n\tconst Point nc(c - a);\n\tif (cross(nb, nc) > eps) return 1; // a,b,c??????????¨???¨?????????????????¶\n\tif (cross(nb, nc) < -eps) return -1; // a,b,c???????¨???¨?????????????????¶\n\tif (dot(nb, nc) < 0) return 2; // c,a,b???????????´???????????¶\n\tif (norm(nb) < norm(nc)) return -2; // a,b,c???????????´???????????¶\n\treturn 0; // a,c,b???????????´???????????¶\n}\n\n\n/* ???????????? */\n\n// ??´?????¨??´??????????????????\nbool isis_ll(const Line& l, const Line& m) {\n\treturn !eq(cross(l.b - l.a, m.b - m.a), 0);\n}\n\n// ??´?????¨?????????????????????\nbool isis_ls(const Line& l, const Line& s) {\n\treturn isis_ll(l, s) &&\n\t\t(cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < eps);\n}\n\n// ????????¨?????????????????????\nbool isis_ss(const Line& s, const Line& t) {\n\treturn ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 &&\n\t\tccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0;\n}\n\n// ????????´????????????\nbool isis_lp(const Line& l, const Point& p) {\n\treturn (abs(cross(l.b - p, l.a - p)) < eps);\n}\n\n// ?????????????????????\nbool isis_sp(const Line& s, const Point& p) {\n\treturn (abs(s.a - p) + abs(s.b - p) - abs(s.b - s.a) < eps);\n}\n\n// ??????????¶?\nPoint proj(const Line &l, const Point& p) {\n\tld t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n\treturn l.a + t * (l.a - l.b);\n}\n\n// ??´?????¨??´????????????\nPoint is_ll(const Line &s, const Line& t) {\n\tPoint sv = s.b - s.a, tv = t.b - t.a;\n\tassert(cross(sv, tv) != 0);\n\treturn s.a + sv * cross(tv, t.a - s.a) / cross(tv, sv);\n}\n// ??´?????¨??´????????????\nvector<Point> is_ll2(const Line &s, const Line& t) {\n\tPoint sv = s.b - s.a, tv = t.b - t.a;\n\tif (cross(sv, tv) != 0)return vector<Point>(1, is_ll(s, t));\n\telse {\n\t\tvector<Point>ans;\n\t\tfor (int k = 0; k < 2; ++k) {\n\t\t\tif (isis_sp(s, t[k]) && find(ans.begin(), ans.end(), t[k]) == ans.end())ans.push_back(t[k]);\n\t\t\tif (isis_sp(t, s[k]) && find(ans.begin(), ans.end(), s[k]) == ans.end())ans.push_back(s[k]);\n\t\t}\n\t\treturn ans;\n\t}\n}\n// ????????¨???????????????\n//???????????£????????¨???????????¨assert(false)\nPoint is_ss(const Line &s, const Line& t) {\n\tif (isis_ss(s, t)) {\n\t\tfor (int k = 0; k < 2; ++k) {\n\t\t\tfor (int l = 0; l < 2; ++l) {\n\t\t\t\tif (s[k] == t[l])return s[k];\n\t\t\t}\n\t\t}\n\t\treturn is_ll(s, t);\n\t}\n\telse {\n\t\t//??????isis_ss?????????\n\t\tassert(false);\n\t\treturn Point(0, 0);\n\t}\n}\n// ??´?????¨???????????¢\nld dist_lp(const Line& l, const Point& p) {\n\treturn abs(p - proj(l, p));\n}\n\n// ??´?????¨??´???????????¢\nld dist_ll(const Line& l, const Line& m) {\n\treturn isis_ll(l, m) ? 0 : dist_lp(l, m.a);\n}\n\n// ??´?????¨??????????????¢\nld dist_ls(const Line& l, const Line& s) {\n\treturn isis_ls(l, s) ? 0 : min(dist_lp(l, s.a), dist_lp(l, s.b));\n}\n\n// ????????¨???????????¢\nld dist_sp(const Line& s, const Point& p) {\n\tPoint r = proj(s, p);\n\treturn isis_sp(s, r) ? abs(r - p) : min(abs(s.a - p), abs(s.b - p));\n}\n\n// ????????¨??????????????¢\nld dist_ss(const Line& s, const Line& t) {\n\tif (isis_ss(s, t)) return 0;\n\treturn min({ dist_sp(s, t.a), dist_sp(s, t.b), dist_sp(t, s.a), dist_sp(t, s.b) });\n}\n\n\n//??´?????¨??´?????????????????????????????????\nLine bisection(const Line &s, const Line &t) {\n\tconst Point laglanju(is_ll(s, t));\n\tconst Point avec = !(abs(laglanju - s[0])<eps) ? s[0] - laglanju : s[1] - laglanju;\n\tconst Point bvec = !(abs(laglanju - t[0])<eps) ? t[0] - laglanju : t[1] - laglanju;\n\n\treturn Line(laglanju, laglanju + (abs(bvec)*avec + abs(avec)*bvec) / (abs(avec) + abs(bvec)));\n}\n\n\n//???????????´?????????????????????\n//???????????´??????????????§???????????¨????¢?????????¨?????????\nPoint inner_center(const vector<Line>&ls) {\n\tvector<Point>vertics;\n\tfor (int i = 0; i <static_cast<int>(ls.size()); ++i) {\n\t\tvertics.push_back(is_ll(ls[i], ls[(i + 1) % 3]));\n\t}\n\tif (vertics[0] == vertics[1] || vertics[1] == vertics[2] || vertics[2] == vertics[0])return vertics[0];\n\tLine bi1(bisection(Line(vertics[0], vertics[1]), Line(vertics[0], vertics[2])));\n\tLine bi2(bisection(Line(vertics[1], vertics[2]), Line(vertics[1], vertics[0])));\n\tif (bi1[0] == bi2[0])return bi1[0];\n\telse {\n\t\treturn is_ll(bi1, bi2);\n\t}\n}\n\n//???????????´?????????????????????\n//???????????´??????????????§???????????¨????¢?????????¨?????????\nvector<Point> ex_center(const vector<Line>&ls) {\n\tvector<Point>vertics;\n\tfor (int i = 0; i < static_cast<int>(ls.size()); ++i) {\n\t\tvertics.push_back(is_ll(ls[i], ls[(i + 1) % 3]));\n\t}\n\tif (abs(vertics[0] - vertics[1])<eps || abs(vertics[1] - vertics[2])<eps || (abs(vertics[2] - vertics[0])<eps))return vector<Point>();\n\tvector<Point>ecs;\n\tfor (int i = 0; i < 3; ++i) {\n\t\tLine bi1(bisection(Line(vertics[i], vertics[i] * 2.0l - vertics[(i + 2) % 3]), Line(vertics[i], vertics[(i + 1) % 3])));\n\t\tLine bi2(bisection(Line(vertics[(i + 1) % 3], vertics[(i + 1) % 3] * 2.0l - vertics[(i + 2) % 3]), Line(vertics[(i + 1) % 3], vertics[i])));\n\t\tecs.push_back(is_ll(bi1, bi2));\n\t}\n\treturn ecs;\n}\n\n\n//a,b:??????\n//c:????????§??????\n//???????????´?????????????????¢?????????????±??????????\nvector<Point> same_dis(const vector<Line>&ls) {\n\tvector<Point>vertics;\n\tvertics.push_back(is_ll(ls[0], ls[2]));\n\tvertics.push_back(is_ll(ls[1], ls[2]));\n\n\tif (abs(vertics[0] - vertics[1]) < eps)return vector<Point>{vertics[0]};\n\tLine bis(bisection(ls[0], ls[1]));\n\tvector<Point>ecs;\n\n\tLine abi(bisection(Line(vertics[0], vertics[1]), ls[0]));\n\tecs.push_back(is_ll(bis, abi));\n\n\n\tLine bbi(bisection(Line(vertics[0], 2.l*vertics[0] - vertics[1]), ls[0]));\n\tecs.push_back(is_ll(bis, bbi));\n\n\treturn ecs;\n}\n/* ??? */\n\n// ?????¨????????????\nvector<Point> is_cc(const Circle& c1, const Circle& c2) {\n\tvector<Point> res;\n\tld d = abs(c1.p - c2.p);\n\tld rc = (d * d + c1.r * c1.r - c2.r * c2.r) / (2 * d);\n\tld dfr = c1.r * c1.r - rc * rc;\n\tif (abs(dfr) < eps) dfr = 0.0;\n\telse if (dfr < 0.0) return res; // no intersection\n\tld rs = sqrt(dfr);\n\tPoint diff = (c2.p - c1.p) / d;\n\tres.push_back(c1.p + diff * Point(rc, rs));\n\tif (dfr != 0.0) res.push_back(c1.p + diff * Point(rc, -rs));\n\treturn res;\n}\n\n//???????????????????????????\n// 0 => out\n// 1 => on\n// 2 => in\nint is_in_circle(const Circle &cir, const Point& p) {\n\tld dis = abs(cir.p - p);\n\tif (dis > cir.r + eps)return 0;\n\telse if (dis < cir.r - eps)return 2;\n\telse return 1;\n}\n//???lc??????rc??????????????????\n// 0 => out\n// 1 => on\n// 2 => in\nint circle_in_circle(const Circle &lc, const Circle&rc) {\n\tld dis = abs(lc.p - rc.p);\n\tif (dis < rc.r - lc.r - eps)return 2;\n\telse if (dis>rc.r - lc.r + eps)return 0;\n\telse return 1;\n}\n\n// ?????¨??´????????????\nvector<Point> is_lc(const Circle& c, const Line& l) {\n\tvector<Point> res;\n\tld d = dist_lp(l, c.p);\n\tif (d < c.r + eps) {\n\t\tld len = (d > c.r) ? 0.0 : sqrt(c.r * c.r - d * d); //safety;\n\t\tPoint nor = (l.a - l.b) / abs(l.a - l.b);\n\t\tres.push_back(proj(l, c.p) + len * nor);\n\t\tres.push_back(proj(l, c.p) - len * nor);\n\t}\n\treturn res;\n}\n\n// ?????¨??????????????¢\nvector<Point> is_sc(const Circle& c, const Line& l) {\n\tvector<Point> v = is_lc(c, l), res;\n\tfor (Point p : v)\n\t\tif (isis_sp(l, p)) res.push_back(p);\n\treturn res;\n}\n\n// ?????¨????????\\???\nvector<Line> tangent_cp(const Circle& c, const Point& p) {\n\tvector<Line> ret;\n\tPoint v = c.p - p;\n\tld d = abs(v);\n\tld l = sqrt(norm(v) - c.r * c.r);\n\tif (isnan(l)) { return ret; }\n\tPoint v1 = v * Point(l / d, c.r / d);\n\tPoint v2 = v * Point(l / d, -c.r / d);\n\tret.push_back(Line(p, p + v1));\n\tif (l < eps) return ret;\n\tret.push_back(Line(p, p + v2));\n\treturn ret;\n}\n\n// ?????¨????????\\???\nvector<Line> tangent_cc(const Circle& c1, const Circle& c2) {\n\tvector<Line> ret;\n\tif (abs(c1.p - c2.p) - (c1.r + c2.r) > -eps) {\n\t\tPoint center = (c1.p * c2.r + c2.p * c1.r) / (c1.r + c2.r);\n\t\tret = tangent_cp(c1, center);\n\t}\n\tif (abs(c1.r - c2.r) > eps) {\n\t\tPoint out = (-c1.p * c2.r + c2.p * c1.r) / (c1.r - c2.r);\n\t\tvector<Line> nret = tangent_cp(c1, out);\n\t\tret.insert(ret.end(), ALL(nret));\n\t}\n\telse {\n\t\tPoint v = c2.p - c1.p;\n\t\tv /= abs(v);\n\t\tPoint q1 = c1.p + v * Point(0, 1) * c1.r;\n\t\tPoint q2 = c1.p + v * Point(0, -1) * c1.r;\n\t\tret.push_back(Line(q1, q1 + v));\n\t\tret.push_back(Line(q2, q2 + v));\n\t}\n\treturn ret;\n}\n//??????????????????????????¢???\nld two_circle_area(const Circle&l, const Circle&r) {\n\tld dis = abs(l.p - r.p);\n\tif (dis > l.r + r.r)return 0;\n\telse if (dis + r.r < l.r) {\n\t\treturn r.r*r.r*pi;\n\t}\n\telse if (dis + l.r < r.r) {\n\t\treturn l.r*l.r*pi;\n\t}\n\telse {\n\t\tld ans = (l.r)*(l.r)*acos((dis*dis + l.r*l.r - r.r*r.r) / (2 * dis*l.r)) +\n\t\t\t(r.r)*(r.r)*acos((dis*dis + r.r*r.r - l.r*l.r) / (2 * dis*r.r)) -\n\t\t\tsqrt(4 * dis*dis*l.r*l.r - (dis*dis + l.r*l.r - r.r*r.r)*(dis*dis + l.r*l.r - r.r*r.r)) / 2;\n\t\treturn ans;\n\t}\n\n}\n\n/* ????§???¢ */\n\ntypedef vector<Point> Polygon;\n\n// ??¢???\nld area(const Polygon &p) {\n\tld res = 0;\n\tint n = p.size();\n\tREP(j, n) res += cross(p[j], p[(j + 1) % n]);\n\treturn res / 2;\n}\n\n// ????§???¢????????¢??????\nbool is_counter_clockwise(const Polygon &poly) {\n\tld angle = 0;\n\tint n = poly.size();\n\tREP(i, n) {\n\t\tPoint a = poly[i], b = poly[(i + 1) % n], c = poly[(i + 2) % n];\n\t\tangle += arg((c - b) / (b - a));\n\t}\n\treturn angle > eps;\n}\n\n// ??????????????????\n// 0 => out\n// 1 => on\n// 2 => in\nint is_in_polygon(const Polygon &poly, const Point& p) {\n\tld angle = 0;\n\tint n = poly.size();\n\tREP(i, n) {\n\t\tPoint a = poly[i], b = poly[(i + 1) % n];\n\t\tif (isis_sp(Line(a, b), p)) return 1;\n\t\tangle += arg((b - p) / (a - p));\n\t}\n\treturn eq(angle, 0) ? 0 : 2;\n}\n//??????????????????2?????????\nenum { OUT, ON, IN };\nint convex_contains(const Polygon &P, const Point &p) {\n\tconst int n = P.size();\n\tPoint g = (P[0] + P[n / 3] + P[2 * n / 3]) / 3.0l; // inner-point\n\tint a = 0, b = n;\n\twhile (a + 1 < b) { // invariant: c is in fan g-P[a]-P[b]\n\t\tint c = (a + b) / 2;\n\t\tif (cross(P[a] - g, P[c] - g) > 0) { // angle < 180 deg\n\t\t\tif (cross(P[a] - g, p - g) > 0 && cross(P[c] - g, p - g) < 0) b = c;\n\t\t\telse a = c;\n\t\t}\n\t\telse {\n\t\t\tif (cross(P[a] - g, p - g) < 0 && cross(P[c] - g, p - g) > 0) a = c;\n\t\t\telse b = c;\n\t\t}\n\t}\n\tb %= n;\n\tif (cross(P[a] - p, P[b] - p) < 0) return 0;\n\tif (cross(P[a] - p, P[b] - p) > 0) return 2;\n\treturn 1;\n}\n\n// ??????\n// ???????????????????????¨????????????????????§??¨???\nPolygon convex_hull(vector<Point> ps) {\n\tint n = ps.size();\n\tint k = 0;\n\tsort(ps.begin(), ps.end());\n\tPolygon ch(2 * n);\n\tfor (int i = 0; i < n; ch[k++] = ps[i++])\n\t\twhile (k >= 2 && ccw(ch[k - 2], ch[k - 1], ps[i]) <= 0) --k;\n\tfor (int i = n - 2, t = k + 1; i >= 0; ch[k++] = ps[i--])\n\t\twhile (k >= t && ccw(ch[k - 2], ch[k - 1], ps[i]) <= 0) --k;\n\tch.resize(k - 1);\n\treturn ch;\n}\n\n\n\n// ????????????\nvector<Polygon> convex_cut(const Polygon &ps, const Line& l) {\n\tint n = ps.size();\n\tPolygon Q;\n\tPolygon R;\n\tREP(i, n) {\n\t\tPoint A = ps[i], B = ps[(i + 1) % n];\n\t\tLine m = Line(A, B);\n\t\tif (ccw(l.a, l.b, A) != -1) Q.push_back(A);\n\t\tif (ccw(l.a, l.b, A) != 1) R.push_back(A);\n\t\tif (ccw(l.a, l.b, A) * ccw(l.a, l.b, B) < 0 && isis_ll(l, m)) {\n\t\t\tQ.push_back(is_ll(l, m));\n\t\t\tR.push_back(is_ll(l, m));\n\t\t}\n\t}\n\tconst vector<Polygon>polys{ Q,R };\n\treturn polys;\n}\n\n\n/* ??¢??¬??????????????? */\nvoid add_point(vector<Point> &ps, const Point p) {\n\tfor (Point q : ps) if (abs(q - p) < eps) return;\n\tps.push_back(p);\n}\n\ntypedef int Weight;\nstruct Edge {\n\tint src, dst;\n\tWeight weight;\n\tEdge(int src, int dst, Weight weight) :\n\t\tsrc(src), dst(dst), weight(weight) { }\n};\n\ntypedef vector<Edge> Edges;\ntypedef vector<Edges> Graph;\n\nvoid add_edge(Graph &g, const int from, const int to, const Weight weight) {\n\tg[from].push_back(Edge{ from, to, weight });\n}\n\nGraph segment_arrangement(const vector<Line> &s, const vector<Point> &p) {\n\tint n = p.size(), m = s.size();\n\tGraph g(n);\n\tREP(i, m) {\n\t\tvector<pair<ld, int>> vec;\n\t\tREP(j, n) if (isis_sp(s[i], p[j]))\n\t\t\tvec.emplace_back(abs(s[i].a - p[j]), j);\n\t\tsort(ALL(vec));\n\t\tREP(j, vec.size() - 1) {\n\t\t\tint from = vec[j].second, to = vec[j + 1].second;\n\t\t\tadd_edge(g, from, to, static_cast<Weight>(abs(p[from] - p[to])));\n\t\t}\n\t}\n\treturn g;\n}\nGraph sennbunn_arrangement(const vector<Line>&s) {\n\tvector<Point>crss;\n\tfor (int i = 0; i < static_cast<int>(s.size()); ++i) {\n\t\tfor (int j = i + 1; j < static_cast<int>(s.size()); ++j) {\n\t\t\tif (isis_ss(s[i], s[j])) {\n\t\t\t\tcrss.push_back(is_ll(s[i], s[j]));\n\t\t\t}\n\t\t}\n\t}\n\tfor (int i = 0; i <static_cast<int>(s.size()); ++i) {\n\t\tcrss.push_back(s[i][0]);\n\t\tcrss.push_back(s[i][1]);\n\t}\n\treturn segment_arrangement(s, crss);\n}\n\n//Graph circle_arrangement(const vector<Circle> &c, const vector<Point> &p) {\n//\tint n = p.size(), m = c.size();\n//\tGraph g(n);\n//\tREP(i, m) {\n//\t\tvector<pair<ld, int>> vec;\n//\t\tREP(j, n) if (abs(abs(c[i].p - p[j]) - c[i].r) < eps)\n//\t\t\tvec.emplace_back(arg(c[i].p - p[j]), j);\n//\t\tsort(ALL(vec));\n//\t\tREP(j, vec.size() - 1) {\n//\t\t\tint from = vec[j].second, to = vec[j + 1].second;\n//\t\t\tld angle = vec[j + 1].first - vec[j].first;\n//\t\t\tadd_edge(g, from, to, static_cast<Weight>(angle * c[i].r));\n//\t\t}\n//\t\tif (vec.size() >= 2) {\n//\t\t\tint from = vec.back().second, to = vec.front().first;\n//\t\t\tld angle = vec.front().first - vec.back().first;\n//\t\t\tadd_edge(g, from, to, static_cast<Weight>(angle * c[i].r));\n//\t\t}\n//\t}\n//\treturn g;\n//}\n\n\n\nconst ld G= 9.8;\n\nstruct box {\n\tvector<ld>xs;\n\tvector<ld>ys;\n};\n\n\nint N, V, X, Y;\nbool check(const ld theta, const box& b) {\n\n\tconst ld vx = V*cos(theta);\n\tconst ld vy = V*sin(theta);\n\n\tld y_max = -1e18;\n\tld y_min = 1e18;\n\n\t//?????????????¢????\n\t{\n\t\tconst ld toptime = vy / G;\n\t\tconst ld top_x = toptime*vx;\n\t\tconst ld top_y = vy*toptime - toptime*toptime*G / 2;\n\t\tif (b.xs[0] < top_x&&top_x < b.xs[1]) {\n\t\t\ty_max = max(y_max, top_y);\n\t\t}\n\t}\n\t//???????????????????¢????\n\t{\n\t\tfor (int x = 0; x < 2; ++x) {\n\t\t\tconst ld time = b.xs[x]/vx;\n\t\t\tconst ld ay = vy*time - time*time*G / 2;\n\t\t\ty_max = max(y_max, ay);\n\t\t\ty_min = min(y_min, ay);\n\t\t}\n\t}\n\tif (y_max- eps < b.ys[0] || b.ys[1] < y_min + eps) {\n\t\t\n\t\treturn true;\n\t}\n\telse {\n\t\treturn false;\n\t}\n}\n\nvoid gettan(vector<ld>&tans,const Point& p,int leftup) {\n\tif (p.real() < eps)return;\n\tbool ok = true;\n\tld amin, amax;\n\n\t//?????????????¨????\n\t{\n\t\tconst ld tan = V*V / G / p.real();\n\t\tconst ld theta = atan(tan);\n\t\tconst ld vx = V*cos(theta);\n\t\tconst ld vy = V*sin(theta);\n\t\tconst ld time = p.real() / vx;\n\t\tconst ld max_y = vy*time - G*time*time / 2;\n\t\tif (max_y < p.imag()) {\n\t\t\tok = false;\n\t\t}\n\t\tif (leftup) {\n\t\t\tamax = tan;\n\t\t\tamin = 0;\n\t\t}\n\t\telse {\n\t\t\tamax = 1e18;\n\t\t\tamin = tan;\n\t\t}\n\t}\n\tif (ok) {\n\t\tint rep = 1000;\n\t\twhile (rep--) {\n\t\t\tconst ld amidtan = (amin + amax) / 2;\n\t\t\tconst ld theta = atan(amidtan);\n\t\t\tconst ld vx = V*cos(theta);\n\t\t\tconst ld vy = V*sin(theta);\n\t\t\tconst ld time = p.real() / vx;\n\t\t\tconst ld ay = vy*time - G*time*time / 2;\n\t\t\tif (ay > p.imag()) {\n\t\t\t\tif (leftup) {\n\t\t\t\t\tamax = amidtan;\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tamin = amidtan;\n\t\t\t\t}\n\t\t\t}\n\t\t\telse {\n\t\t\t\tif (leftup) {\n\t\t\t\t\tamin = amidtan;\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tamax = amidtan;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tconst ld amidtan = (amin + amax) / 2;\n\t\tconst ld theta = atan(amidtan);\n\t\tconst ld vx = V*cos(theta);\n\t\tconst ld vy = V*sin(theta);\n\t\tconst ld time = p.real() / vx;\n\t\tconst ld ay = vy*time - G*time*time / 2;\n\t\ttans.push_back(amin);\n\t}\n}\n\nint main() { cin >> N >> V >> X >> Y;\n\tvector<box>bs;\n\tfor (int i = 0; i < N; ++i) {\n\t\tint L, B, R, T; cin >> L >> B >> R >> T;\n\t\tbox b;\n\t\tb.xs.push_back(L);\n\t\tb.xs.push_back(R);\n\t\tb.ys.push_back(B);\n\t\tb.ys.push_back(T);\n\t\tbs.push_back(b);\n\t}\n\tconst ld down = Y;\n\tld up = 1e18;\n\tfor (int i = 0; i < N; ++i) {\n\t\tif (bs[i].xs[0] < X&&X < bs[i].xs[1]) {\n\t\t\tif (bs[i].ys[0] > Y) {\n\n\t\t\t\tup = min(up, bs[i].ys[0]);\n\t\t\t}\n\t\t}\n\t}\n\tvector<ld>tans;\n\tfor (auto b : bs) {\n\t\tfor (int x = 0; x < 2; ++x) {\n\t\t\tfor (int y = 0; y < 2; ++y) {\n\t\t\t\tPoint p(b.xs[x], b.ys[y]);\n\t\t\t\tgettan(tans, p,(x+y)%2);\n\t\t\t\t//gettan(tans, p, (x + y+1) % 2);\n\t\t\t}\n\t\t}\n\t}\n\t{\n\t\tPoint p(X,Y);\n\t\tgettan(tans, p,true);\n\t}\n\t{\n\t\tfor (int y = 1; y <= 300; ++y) {\n\t\t\tif (V*V / 2 >= y) {\n\t\t\t\tld vy = sqrt(2 * y);\n\t\t\t\tif (vy > V)break;\n\t\t\t\tld vx = sqrt(V*V - vy*vy);\n\t\t\t\tld tan = vy / vx;\n\t\t\t\ttans.push_back(tan);\n\t\t\t}\n\t\t}\n\t}\n\tsort(tans.begin(), tans.end());\n\t{\n\t\tvector<ld>ntans;\n\t\tfor (int i = 0; i < tans.size(); ++i) {\n\t\t\tntans.push_back(tans[i]);\n\t\t\tif (i != tans.size() - 1) {\n\t\t\t\tntans.push_back((tans[i] + tans[i + 1]) / 2);\n\t\t\t}\n\t\t}\n\t\ttans = ntans;\n\t}\n\n\tstring ans = \"No\";\n\tfor (auto t : tans) {\n\t\tconst ld theta = atan(t);\n\t\tconst ld vx = V*cos(theta);\n\t\tconst ld vy = V*sin(theta);\n\t\tconst ld time = X / vx;\n\t\tconst ld ay = vy*time - G*time*time / 2;\n\t\tif (down-eps <= ay&&ay <= up+eps) {\n\t\t\tbool ok = true;\n\t\t\tfor (auto b : bs) {\n\t\t\t\tif (!check(theta, b)) {\n\t\t\t\t\tok = false;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (ok) {\n\n\t\t\t\tconst ld vx = V*cos(theta);\n\t\t\t\tconst ld vy = V*sin(theta);\n\t\t\t\t//cout << \"vx:\" << vx << \" vy:\" << vy << endl;\n\t\t\t\tfor (int x = 0; x <=100; ++x) {\n\t\t\t\t\tconst ld t = x / vx;\n\t\t\t\t\t//cout << \"x:\" << x << endl;\n\t\t\t\t\t//cout << \"y:\" << vy*t - G*t*t / 2 << endl;\n\t\t\t\t}\t\t\t\t\tans = \"Yes\";\n\t\t\t}\n\t\t}\n\t\telse {\n\t\t\tcontinue;\n\t\t}\n\t}\n\tcout << ans << endl;\n\treturn 0;\n}", "accuracy": 0.2, "time_ms": 30, "memory_kb": 3520, "score_of_the_acc": -0.8749, "final_rank": 13 }, { "submission_id": "aoj_2308_1777183", "code_snippet": "#include \"bits/stdc++.h\"\n#include<unordered_map>\n#include<unordered_set>\n#pragma warning(disable:4996)\nusing namespace std;\nusing ld = long double;\ntemplate<class T>\nusing Table = vector<vector<T>>;\nconst ld eps=1e-9;\n\n//// < \"D:\\D_Download\\Visual Studio 2015\\Projects\\programing_contest_c++\\Debug\\a.txt\"\n\n\n/* ??????????????¬ */\n\n#include <complex>\n\ntypedef long double ld;\ntypedef complex<ld> Point;\n#define REP(i,n) for(int i=0;i<(int)(n);i++)\n#define ALL(x) (x).begin(),(x).end()\n\n\nconst ld pi = acos(-1.0);\nconst ld dtop = pi / 180.;\nconst ld ptod = 1. / dtop;\n\nnamespace std {\n\tbool operator<(const Point &lhs, const Point &rhs) {\n\t\tif (lhs.real() < rhs.real() - eps) return true;\n\t\tif (lhs.real() > rhs.real() + eps) return false;\n\t\treturn lhs.imag() < rhs.imag();\n\t}\n}\n\n// ????????\\???\nPoint input_point() {\n\tld x, y;\n\tcin >> x >> y;\n\treturn Point(x, y);\n}\n\n// ????????????????????????\nbool eq(const ld a, const ld b) {\n\treturn (abs(a - b) < eps);\n}\n\n// ??????\nld dot(const Point& a, const Point& b) {\n\treturn real(conj(a) * b);\n}\n\n// ??????\nld cross(const Point& a, const Point& b) {\n\treturn imag(conj(a) * b);\n}\n\n// ??´????????????\nclass Line {\npublic:\n\tPoint a, b;\n\tLine() : a(Point(0, 0)), b(Point(0, 0)) {}\n\tLine(Point a, Point b) : a(a), b(b) {}\n\tPoint operator[](const int _num)const {\n\t\tif (_num == 0)return a;\n\t\telse if (_num == 1)return b;\n\t\telse {\n\t\t\tassert(false);\n\t\t\treturn Point();\n\t\t}\n\t}\n};\n\n// ????????????\nclass Circle {\npublic:\n\tPoint p;\n\tld r;\n\tCircle() : p(Point(0, 0)), r(0) {}\n\tCircle(Point p, ld r) : p(p), r(r) {}\n};\n\n// CCW\nint ccw(const Point& a, const Point &b, const Point &c) {\n\tconst Point nb(b - a);\n\tconst Point nc(c - a);\n\tif (cross(nb, nc) > eps) return 1; // a,b,c??????????¨???¨?????????????????¶\n\tif (cross(nb, nc) < -eps) return -1; // a,b,c???????¨???¨?????????????????¶\n\tif (dot(nb, nc) < 0) return 2; // c,a,b???????????´???????????¶\n\tif (norm(nb) < norm(nc)) return -2; // a,b,c???????????´???????????¶\n\treturn 0; // a,c,b???????????´???????????¶\n}\n\n\n/* ???????????? */\n\n// ??´?????¨??´??????????????????\nbool isis_ll(const Line& l, const Line& m) {\n\treturn !eq(cross(l.b - l.a, m.b - m.a), 0);\n}\n\n// ??´?????¨?????????????????????\nbool isis_ls(const Line& l, const Line& s) {\n\treturn isis_ll(l, s) &&\n\t\t(cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < eps);\n}\n\n// ????????¨?????????????????????\nbool isis_ss(const Line& s, const Line& t) {\n\treturn ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 &&\n\t\tccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0;\n}\n\n// ????????´????????????\nbool isis_lp(const Line& l, const Point& p) {\n\treturn (abs(cross(l.b - p, l.a - p)) < eps);\n}\n\n// ?????????????????????\nbool isis_sp(const Line& s, const Point& p) {\n\treturn (abs(s.a - p) + abs(s.b - p) - abs(s.b - s.a) < eps);\n}\n\n// ??????????¶?\nPoint proj(const Line &l, const Point& p) {\n\tld t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n\treturn l.a + t * (l.a - l.b);\n}\n\n// ??´?????¨??´????????????\nPoint is_ll(const Line &s, const Line& t) {\n\tPoint sv = s.b - s.a, tv = t.b - t.a;\n\tassert(cross(sv, tv) != 0);\n\treturn s.a + sv * cross(tv, t.a - s.a) / cross(tv, sv);\n}\n// ??´?????¨??´????????????\nvector<Point> is_ll2(const Line &s, const Line& t) {\n\tPoint sv = s.b - s.a, tv = t.b - t.a;\n\tif (cross(sv, tv) != 0)return vector<Point>(1, is_ll(s, t));\n\telse {\n\t\tvector<Point>ans;\n\t\tfor (int k = 0; k < 2; ++k) {\n\t\t\tif (isis_sp(s, t[k]) && find(ans.begin(), ans.end(), t[k]) == ans.end())ans.push_back(t[k]);\n\t\t\tif (isis_sp(t, s[k]) && find(ans.begin(), ans.end(), s[k]) == ans.end())ans.push_back(s[k]);\n\t\t}\n\t\treturn ans;\n\t}\n}\n// ????????¨???????????????\n//???????????£????????¨???????????¨assert(false)\nPoint is_ss(const Line &s, const Line& t) {\n\tif (isis_ss(s, t)) {\n\t\tfor (int k = 0; k < 2; ++k) {\n\t\t\tfor (int l = 0; l < 2; ++l) {\n\t\t\t\tif (s[k] == t[l])return s[k];\n\t\t\t}\n\t\t}\n\t\treturn is_ll(s, t);\n\t}\n\telse {\n\t\t//??????isis_ss?????????\n\t\tassert(false);\n\t\treturn Point(0, 0);\n\t}\n}\n// ??´?????¨???????????¢\nld dist_lp(const Line& l, const Point& p) {\n\treturn abs(p - proj(l, p));\n}\n\n// ??´?????¨??´???????????¢\nld dist_ll(const Line& l, const Line& m) {\n\treturn isis_ll(l, m) ? 0 : dist_lp(l, m.a);\n}\n\n// ??´?????¨??????????????¢\nld dist_ls(const Line& l, const Line& s) {\n\treturn isis_ls(l, s) ? 0 : min(dist_lp(l, s.a), dist_lp(l, s.b));\n}\n\n// ????????¨???????????¢\nld dist_sp(const Line& s, const Point& p) {\n\tPoint r = proj(s, p);\n\treturn isis_sp(s, r) ? abs(r - p) : min(abs(s.a - p), abs(s.b - p));\n}\n\n// ????????¨??????????????¢\nld dist_ss(const Line& s, const Line& t) {\n\tif (isis_ss(s, t)) return 0;\n\treturn min({ dist_sp(s, t.a), dist_sp(s, t.b), dist_sp(t, s.a), dist_sp(t, s.b) });\n}\n\n\n//??´?????¨??´?????????????????????????????????\nLine bisection(const Line &s, const Line &t) {\n\tconst Point laglanju(is_ll(s, t));\n\tconst Point avec = !(abs(laglanju - s[0])<eps) ? s[0] - laglanju : s[1] - laglanju;\n\tconst Point bvec = !(abs(laglanju - t[0])<eps) ? t[0] - laglanju : t[1] - laglanju;\n\n\treturn Line(laglanju, laglanju + (abs(bvec)*avec + abs(avec)*bvec) / (abs(avec) + abs(bvec)));\n}\n\n\n//???????????´?????????????????????\n//???????????´??????????????§???????????¨????¢?????????¨?????????\nPoint inner_center(const vector<Line>&ls) {\n\tvector<Point>vertics;\n\tfor (int i = 0; i <static_cast<int>(ls.size()); ++i) {\n\t\tvertics.push_back(is_ll(ls[i], ls[(i + 1) % 3]));\n\t}\n\tif (vertics[0] == vertics[1] || vertics[1] == vertics[2] || vertics[2] == vertics[0])return vertics[0];\n\tLine bi1(bisection(Line(vertics[0], vertics[1]), Line(vertics[0], vertics[2])));\n\tLine bi2(bisection(Line(vertics[1], vertics[2]), Line(vertics[1], vertics[0])));\n\tif (bi1[0] == bi2[0])return bi1[0];\n\telse {\n\t\treturn is_ll(bi1, bi2);\n\t}\n}\n\n//???????????´?????????????????????\n//???????????´??????????????§???????????¨????¢?????????¨?????????\nvector<Point> ex_center(const vector<Line>&ls) {\n\tvector<Point>vertics;\n\tfor (int i = 0; i < static_cast<int>(ls.size()); ++i) {\n\t\tvertics.push_back(is_ll(ls[i], ls[(i + 1) % 3]));\n\t}\n\tif (abs(vertics[0] - vertics[1])<eps || abs(vertics[1] - vertics[2])<eps || (abs(vertics[2] - vertics[0])<eps))return vector<Point>();\n\tvector<Point>ecs;\n\tfor (int i = 0; i < 3; ++i) {\n\t\tLine bi1(bisection(Line(vertics[i], vertics[i] * 2.0l - vertics[(i + 2) % 3]), Line(vertics[i], vertics[(i + 1) % 3])));\n\t\tLine bi2(bisection(Line(vertics[(i + 1) % 3], vertics[(i + 1) % 3] * 2.0l - vertics[(i + 2) % 3]), Line(vertics[(i + 1) % 3], vertics[i])));\n\t\tecs.push_back(is_ll(bi1, bi2));\n\t}\n\treturn ecs;\n}\n\n\n//a,b:??????\n//c:????????§??????\n//???????????´?????????????????¢?????????????±??????????\nvector<Point> same_dis(const vector<Line>&ls) {\n\tvector<Point>vertics;\n\tvertics.push_back(is_ll(ls[0], ls[2]));\n\tvertics.push_back(is_ll(ls[1], ls[2]));\n\n\tif (abs(vertics[0] - vertics[1]) < eps)return vector<Point>{vertics[0]};\n\tLine bis(bisection(ls[0], ls[1]));\n\tvector<Point>ecs;\n\n\tLine abi(bisection(Line(vertics[0], vertics[1]), ls[0]));\n\tecs.push_back(is_ll(bis, abi));\n\n\n\tLine bbi(bisection(Line(vertics[0], 2.l*vertics[0] - vertics[1]), ls[0]));\n\tecs.push_back(is_ll(bis, bbi));\n\n\treturn ecs;\n}\n/* ??? */\n\n// ?????¨????????????\nvector<Point> is_cc(const Circle& c1, const Circle& c2) {\n\tvector<Point> res;\n\tld d = abs(c1.p - c2.p);\n\tld rc = (d * d + c1.r * c1.r - c2.r * c2.r) / (2 * d);\n\tld dfr = c1.r * c1.r - rc * rc;\n\tif (abs(dfr) < eps) dfr = 0.0;\n\telse if (dfr < 0.0) return res; // no intersection\n\tld rs = sqrt(dfr);\n\tPoint diff = (c2.p - c1.p) / d;\n\tres.push_back(c1.p + diff * Point(rc, rs));\n\tif (dfr != 0.0) res.push_back(c1.p + diff * Point(rc, -rs));\n\treturn res;\n}\n\n//???????????????????????????\n// 0 => out\n// 1 => on\n// 2 => in\nint is_in_circle(const Circle &cir, const Point& p) {\n\tld dis = abs(cir.p - p);\n\tif (dis > cir.r + eps)return 0;\n\telse if (dis < cir.r - eps)return 2;\n\telse return 1;\n}\n//???lc??????rc??????????????????\n// 0 => out\n// 1 => on\n// 2 => in\nint circle_in_circle(const Circle &lc, const Circle&rc) {\n\tld dis = abs(lc.p - rc.p);\n\tif (dis < rc.r - lc.r - eps)return 2;\n\telse if (dis>rc.r - lc.r + eps)return 0;\n\telse return 1;\n}\n\n// ?????¨??´????????????\nvector<Point> is_lc(const Circle& c, const Line& l) {\n\tvector<Point> res;\n\tld d = dist_lp(l, c.p);\n\tif (d < c.r + eps) {\n\t\tld len = (d > c.r) ? 0.0 : sqrt(c.r * c.r - d * d); //safety;\n\t\tPoint nor = (l.a - l.b) / abs(l.a - l.b);\n\t\tres.push_back(proj(l, c.p) + len * nor);\n\t\tres.push_back(proj(l, c.p) - len * nor);\n\t}\n\treturn res;\n}\n\n// ?????¨??????????????¢\nvector<Point> is_sc(const Circle& c, const Line& l) {\n\tvector<Point> v = is_lc(c, l), res;\n\tfor (Point p : v)\n\t\tif (isis_sp(l, p)) res.push_back(p);\n\treturn res;\n}\n\n// ?????¨????????\\???\nvector<Line> tangent_cp(const Circle& c, const Point& p) {\n\tvector<Line> ret;\n\tPoint v = c.p - p;\n\tld d = abs(v);\n\tld l = sqrt(norm(v) - c.r * c.r);\n\tif (isnan(l)) { return ret; }\n\tPoint v1 = v * Point(l / d, c.r / d);\n\tPoint v2 = v * Point(l / d, -c.r / d);\n\tret.push_back(Line(p, p + v1));\n\tif (l < eps) return ret;\n\tret.push_back(Line(p, p + v2));\n\treturn ret;\n}\n\n// ?????¨????????\\???\nvector<Line> tangent_cc(const Circle& c1, const Circle& c2) {\n\tvector<Line> ret;\n\tif (abs(c1.p - c2.p) - (c1.r + c2.r) > -eps) {\n\t\tPoint center = (c1.p * c2.r + c2.p * c1.r) / (c1.r + c2.r);\n\t\tret = tangent_cp(c1, center);\n\t}\n\tif (abs(c1.r - c2.r) > eps) {\n\t\tPoint out = (-c1.p * c2.r + c2.p * c1.r) / (c1.r - c2.r);\n\t\tvector<Line> nret = tangent_cp(c1, out);\n\t\tret.insert(ret.end(), ALL(nret));\n\t}\n\telse {\n\t\tPoint v = c2.p - c1.p;\n\t\tv /= abs(v);\n\t\tPoint q1 = c1.p + v * Point(0, 1) * c1.r;\n\t\tPoint q2 = c1.p + v * Point(0, -1) * c1.r;\n\t\tret.push_back(Line(q1, q1 + v));\n\t\tret.push_back(Line(q2, q2 + v));\n\t}\n\treturn ret;\n}\n//??????????????????????????¢???\nld two_circle_area(const Circle&l, const Circle&r) {\n\tld dis = abs(l.p - r.p);\n\tif (dis > l.r + r.r)return 0;\n\telse if (dis + r.r < l.r) {\n\t\treturn r.r*r.r*pi;\n\t}\n\telse if (dis + l.r < r.r) {\n\t\treturn l.r*l.r*pi;\n\t}\n\telse {\n\t\tld ans = (l.r)*(l.r)*acos((dis*dis + l.r*l.r - r.r*r.r) / (2 * dis*l.r)) +\n\t\t\t(r.r)*(r.r)*acos((dis*dis + r.r*r.r - l.r*l.r) / (2 * dis*r.r)) -\n\t\t\tsqrt(4 * dis*dis*l.r*l.r - (dis*dis + l.r*l.r - r.r*r.r)*(dis*dis + l.r*l.r - r.r*r.r)) / 2;\n\t\treturn ans;\n\t}\n\n}\n\n/* ????§???¢ */\n\ntypedef vector<Point> Polygon;\n\n// ??¢???\nld area(const Polygon &p) {\n\tld res = 0;\n\tint n = p.size();\n\tREP(j, n) res += cross(p[j], p[(j + 1) % n]);\n\treturn res / 2;\n}\n\n// ????§???¢????????¢??????\nbool is_counter_clockwise(const Polygon &poly) {\n\tld angle = 0;\n\tint n = poly.size();\n\tREP(i, n) {\n\t\tPoint a = poly[i], b = poly[(i + 1) % n], c = poly[(i + 2) % n];\n\t\tangle += arg((c - b) / (b - a));\n\t}\n\treturn angle > eps;\n}\n\n// ??????????????????\n// 0 => out\n// 1 => on\n// 2 => in\nint is_in_polygon(const Polygon &poly, const Point& p) {\n\tld angle = 0;\n\tint n = poly.size();\n\tREP(i, n) {\n\t\tPoint a = poly[i], b = poly[(i + 1) % n];\n\t\tif (isis_sp(Line(a, b), p)) return 1;\n\t\tangle += arg((b - p) / (a - p));\n\t}\n\treturn eq(angle, 0) ? 0 : 2;\n}\n//??????????????????2?????????\nenum { OUT, ON, IN };\nint convex_contains(const Polygon &P, const Point &p) {\n\tconst int n = P.size();\n\tPoint g = (P[0] + P[n / 3] + P[2 * n / 3]) / 3.0l; // inner-point\n\tint a = 0, b = n;\n\twhile (a + 1 < b) { // invariant: c is in fan g-P[a]-P[b]\n\t\tint c = (a + b) / 2;\n\t\tif (cross(P[a] - g, P[c] - g) > 0) { // angle < 180 deg\n\t\t\tif (cross(P[a] - g, p - g) > 0 && cross(P[c] - g, p - g) < 0) b = c;\n\t\t\telse a = c;\n\t\t}\n\t\telse {\n\t\t\tif (cross(P[a] - g, p - g) < 0 && cross(P[c] - g, p - g) > 0) a = c;\n\t\t\telse b = c;\n\t\t}\n\t}\n\tb %= n;\n\tif (cross(P[a] - p, P[b] - p) < 0) return 0;\n\tif (cross(P[a] - p, P[b] - p) > 0) return 2;\n\treturn 1;\n}\n\n// ??????\n// ???????????????????????¨????????????????????§??¨???\nPolygon convex_hull(vector<Point> ps) {\n\tint n = ps.size();\n\tint k = 0;\n\tsort(ps.begin(), ps.end());\n\tPolygon ch(2 * n);\n\tfor (int i = 0; i < n; ch[k++] = ps[i++])\n\t\twhile (k >= 2 && ccw(ch[k - 2], ch[k - 1], ps[i]) <= 0) --k;\n\tfor (int i = n - 2, t = k + 1; i >= 0; ch[k++] = ps[i--])\n\t\twhile (k >= t && ccw(ch[k - 2], ch[k - 1], ps[i]) <= 0) --k;\n\tch.resize(k - 1);\n\treturn ch;\n}\n\n\n\n// ????????????\nvector<Polygon> convex_cut(const Polygon &ps, const Line& l) {\n\tint n = ps.size();\n\tPolygon Q;\n\tPolygon R;\n\tREP(i, n) {\n\t\tPoint A = ps[i], B = ps[(i + 1) % n];\n\t\tLine m = Line(A, B);\n\t\tif (ccw(l.a, l.b, A) != -1) Q.push_back(A);\n\t\tif (ccw(l.a, l.b, A) != 1) R.push_back(A);\n\t\tif (ccw(l.a, l.b, A) * ccw(l.a, l.b, B) < 0 && isis_ll(l, m)) {\n\t\t\tQ.push_back(is_ll(l, m));\n\t\t\tR.push_back(is_ll(l, m));\n\t\t}\n\t}\n\tconst vector<Polygon>polys{ Q,R };\n\treturn polys;\n}\n\n\n/* ??¢??¬??????????????? */\nvoid add_point(vector<Point> &ps, const Point p) {\n\tfor (Point q : ps) if (abs(q - p) < eps) return;\n\tps.push_back(p);\n}\n\ntypedef int Weight;\nstruct Edge {\n\tint src, dst;\n\tWeight weight;\n\tEdge(int src, int dst, Weight weight) :\n\t\tsrc(src), dst(dst), weight(weight) { }\n};\n\ntypedef vector<Edge> Edges;\ntypedef vector<Edges> Graph;\n\nvoid add_edge(Graph &g, const int from, const int to, const Weight weight) {\n\tg[from].push_back(Edge{ from, to, weight });\n}\n\nGraph segment_arrangement(const vector<Line> &s, const vector<Point> &p) {\n\tint n = p.size(), m = s.size();\n\tGraph g(n);\n\tREP(i, m) {\n\t\tvector<pair<ld, int>> vec;\n\t\tREP(j, n) if (isis_sp(s[i], p[j]))\n\t\t\tvec.emplace_back(abs(s[i].a - p[j]), j);\n\t\tsort(ALL(vec));\n\t\tREP(j, vec.size() - 1) {\n\t\t\tint from = vec[j].second, to = vec[j + 1].second;\n\t\t\tadd_edge(g, from, to, static_cast<Weight>(abs(p[from] - p[to])));\n\t\t}\n\t}\n\treturn g;\n}\nGraph sennbunn_arrangement(const vector<Line>&s) {\n\tvector<Point>crss;\n\tfor (int i = 0; i < static_cast<int>(s.size()); ++i) {\n\t\tfor (int j = i + 1; j < static_cast<int>(s.size()); ++j) {\n\t\t\tif (isis_ss(s[i], s[j])) {\n\t\t\t\tcrss.push_back(is_ll(s[i], s[j]));\n\t\t\t}\n\t\t}\n\t}\n\tfor (int i = 0; i <static_cast<int>(s.size()); ++i) {\n\t\tcrss.push_back(s[i][0]);\n\t\tcrss.push_back(s[i][1]);\n\t}\n\treturn segment_arrangement(s, crss);\n}\n\n//Graph circle_arrangement(const vector<Circle> &c, const vector<Point> &p) {\n//\tint n = p.size(), m = c.size();\n//\tGraph g(n);\n//\tREP(i, m) {\n//\t\tvector<pair<ld, int>> vec;\n//\t\tREP(j, n) if (abs(abs(c[i].p - p[j]) - c[i].r) < eps)\n//\t\t\tvec.emplace_back(arg(c[i].p - p[j]), j);\n//\t\tsort(ALL(vec));\n//\t\tREP(j, vec.size() - 1) {\n//\t\t\tint from = vec[j].second, to = vec[j + 1].second;\n//\t\t\tld angle = vec[j + 1].first - vec[j].first;\n//\t\t\tadd_edge(g, from, to, static_cast<Weight>(angle * c[i].r));\n//\t\t}\n//\t\tif (vec.size() >= 2) {\n//\t\t\tint from = vec.back().second, to = vec.front().first;\n//\t\t\tld angle = vec.front().first - vec.back().first;\n//\t\t\tadd_edge(g, from, to, static_cast<Weight>(angle * c[i].r));\n//\t\t}\n//\t}\n//\treturn g;\n//}\n\n\n\nconst ld G= 9.8;\n\nstruct box {\n\tvector<ld>xs;\n\tvector<ld>ys;\n};\n\n\nint N, V, X, Y;\nbool check(const ld theta, const box& b) {\n\n\tconst ld vx = V*cos(theta);\n\tconst ld vy = V*sin(theta);\n\n\tld y_max = -1e18;\n\tld y_min = 1e18;\n\n\t//?????????????¢????\n\t{\n\t\tconst ld toptime = vy / G;\n\t\tconst ld top_x = toptime*vx;\n\t\tconst ld top_y = vy*toptime - toptime*toptime*G / 2;\n\t\tif (b.xs[0] < top_x&&top_x < b.xs[1]) {\n\t\t\ty_max = max(y_max, top_y);\n\t\t}\n\t}\n\t//???????????????????¢????\n\t{\n\t\tfor (int x = 0; x < 2; ++x) {\n\t\t\tconst ld time = b.xs[x]/vx;\n\t\t\tconst ld ay = vy*time - time*time*G / 2;\n\t\t\ty_max = max(y_max, ay);\n\t\t\ty_min = min(y_min, ay);\n\t\t}\n\t}\n\tif (y_max- 1e-7 < b.ys[0] || b.ys[1] < y_min + 1e-7) {\n\t\treturn true;\n\t}\n\telse {\n\t\treturn false;\n\t}\n}\n\nvoid gettan(vector<ld>&tans,const Point& p,int leftup) {\n\tif (p.real() < eps)return;\n\tbool ok = true;\n\tld amin, amax;\n\n\t//?????????????¨????\n\t{\n\t\tconst ld tan = V*V / G / p.real();\n\t\tconst ld theta = atan(tan);\n\t\tconst ld vx = V*cos(theta);\n\t\tconst ld vy = V*sin(theta);\n\t\tconst ld time = p.real() / vx;\n\t\tconst ld max_y = vy*time - G*time*time / 2;\n\t\tif (max_y < p.imag()) {\n\t\t\tok = false;\n\t\t}\n\t\tif (leftup) {\n\t\t\tamax = tan;\n\t\t\tamin = 0;\n\t\t}\n\t\telse {\n\t\t\tamax = 1e18;\n\t\t\tamin = tan;\n\t\t}\n\t}\n\tif (ok) {\n\t\tint rep = 1000;\n\t\twhile (rep--) {\n\t\t\tconst ld amidtan = (amin + amax) / 2;\n\t\t\tconst ld theta = atan(amidtan);\n\t\t\tconst ld vx = V*cos(theta);\n\t\t\tconst ld vy = V*sin(theta);\n\t\t\tconst ld time = p.real() / vx;\n\t\t\tconst ld ay = vy*time - G*time*time / 2;\n\t\t\tif (ay > p.imag()) {\n\t\t\t\tif (leftup) {\n\t\t\t\t\tamax = amidtan;\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tamin = amidtan;\n\t\t\t\t}\n\t\t\t}\n\t\t\telse {\n\t\t\t\tif (leftup) {\n\t\t\t\t\tamin = amidtan;\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tamax = amidtan;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tconst ld amidtan = (amin + amax) / 2;\n\t\tconst ld theta = atan(amidtan);\n\t\tconst ld vx = V*cos(theta);\n\t\tconst ld vy = V*sin(theta);\n\t\tconst ld time = p.real() / vx;\n\t\tconst ld ay = vy*time - G*time*time / 2;\n\t\ttans.push_back(amin);\n\t}\n}\n\nint main() { cin >> N >> V >> X >> Y;\n\tvector<box>bs;\n\tfor (int i = 0; i < N; ++i) {\n\t\tint L, B, R, T; cin >> L >> B >> R >> T;\n\t\tbox b;\n\t\tb.xs.push_back(L);\n\t\tb.xs.push_back(R);\n\t\tb.ys.push_back(B);\n\t\tb.ys.push_back(T);\n\t\tbs.push_back(b);\n\t}\n\tconst ld down = Y;\n\tld up = 1e18;\n\tfor (int i = 0; i < N; ++i) {\n\t\tif (bs[i].xs[0] < X&&X < bs[i].xs[1]) {\n\t\t\tif (bs[i].ys[0] > Y) {\n\n\t\t\t\tup = min(up, bs[i].ys[0]);\n\t\t\t}\n\t\t}\n\t}\n\tvector<ld>tans;\n\tfor (auto b : bs) {\n\t\tfor (int x = 0; x < 2; ++x) {\n\t\t\tfor (int y = 0; y < 2; ++y) {\n\t\t\t\tPoint p(b.xs[x], b.ys[y]);\n\t\t\t\tgettan(tans, p,(x+y)%2);\n\t\t\t\t//gettan(tans, p, (x + y+1) % 2);\n\t\t\t}\n\t\t}\n\t}\n\t{\n\t\tPoint p(X,Y);\n\t\tgettan(tans, p,true);\n\t}\n\tsort(tans.begin(), tans.end());\n\t{\n\t\tvector<ld>ntans;\n\t\tfor (int i = 0; i < tans.size(); ++i) {\n\t\t\tntans.push_back(tans[i]);\n\t\t\tif (i != tans.size() - 1) {\n\t\t\t\tntans.push_back((tans[i] + tans[i + 1]) / 2);\n\t\t\t}\n\t\t}\n\t\t//tans = ntans;\n\t}\n\n\tstring ans = \"No\";\n\tfor (auto t : tans) {\n\t\tconst ld theta = atan(t);\n\t\tconst ld vx = V*cos(theta);\n\t\tconst ld vy = V*sin(theta);\n\t\tconst ld time = X / vx;\n\t\tconst ld ay = vy*time - G*time*time / 2;\n\t\tif (down-eps <= ay&&ay <= up+eps) {\n\t\t\tbool ok = true;\n\t\t\tfor (auto b : bs) {\n\t\t\t\tif (!check(theta, b))ok = false;\n\t\t\t}\n\t\t\tif (ok) {\n\t\t\t\tans = \"Yes\";\n\t\t\t}\n\t\t}\n\t\telse {\n\t\t\tcontinue;\n\t\t}\n\t}\n\tcout << ans << endl;\n\treturn 0;\n}", "accuracy": 0.2, "time_ms": 40, "memory_kb": 3472, "score_of_the_acc": -0.8777, "final_rank": 14 }, { "submission_id": "aoj_2308_1777179", "code_snippet": "#include \"bits/stdc++.h\"\n#include<unordered_map>\n#include<unordered_set>\n#pragma warning(disable:4996)\nusing namespace std;\nusing ld = long double;\ntemplate<class T>\nusing Table = vector<vector<T>>;\nconst ld eps=1e-9;\n\n//// < \"D:\\D_Download\\Visual Studio 2015\\Projects\\programing_contest_c++\\Debug\\a.txt\"\n\n\n/* ??????????????¬ */\n\n#include <complex>\n\ntypedef long double ld;\ntypedef complex<ld> Point;\n#define REP(i,n) for(int i=0;i<(int)(n);i++)\n#define ALL(x) (x).begin(),(x).end()\n\n\nconst ld pi = acos(-1.0);\nconst ld dtop = pi / 180.;\nconst ld ptod = 1. / dtop;\n\nnamespace std {\n\tbool operator<(const Point &lhs, const Point &rhs) {\n\t\tif (lhs.real() < rhs.real() - eps) return true;\n\t\tif (lhs.real() > rhs.real() + eps) return false;\n\t\treturn lhs.imag() < rhs.imag();\n\t}\n}\n\n// ????????\\???\nPoint input_point() {\n\tld x, y;\n\tcin >> x >> y;\n\treturn Point(x, y);\n}\n\n// ????????????????????????\nbool eq(const ld a, const ld b) {\n\treturn (abs(a - b) < eps);\n}\n\n// ??????\nld dot(const Point& a, const Point& b) {\n\treturn real(conj(a) * b);\n}\n\n// ??????\nld cross(const Point& a, const Point& b) {\n\treturn imag(conj(a) * b);\n}\n\n// ??´????????????\nclass Line {\npublic:\n\tPoint a, b;\n\tLine() : a(Point(0, 0)), b(Point(0, 0)) {}\n\tLine(Point a, Point b) : a(a), b(b) {}\n\tPoint operator[](const int _num)const {\n\t\tif (_num == 0)return a;\n\t\telse if (_num == 1)return b;\n\t\telse {\n\t\t\tassert(false);\n\t\t\treturn Point();\n\t\t}\n\t}\n};\n\n// ????????????\nclass Circle {\npublic:\n\tPoint p;\n\tld r;\n\tCircle() : p(Point(0, 0)), r(0) {}\n\tCircle(Point p, ld r) : p(p), r(r) {}\n};\n\n// CCW\nint ccw(const Point& a, const Point &b, const Point &c) {\n\tconst Point nb(b - a);\n\tconst Point nc(c - a);\n\tif (cross(nb, nc) > eps) return 1; // a,b,c??????????¨???¨?????????????????¶\n\tif (cross(nb, nc) < -eps) return -1; // a,b,c???????¨???¨?????????????????¶\n\tif (dot(nb, nc) < 0) return 2; // c,a,b???????????´???????????¶\n\tif (norm(nb) < norm(nc)) return -2; // a,b,c???????????´???????????¶\n\treturn 0; // a,c,b???????????´???????????¶\n}\n\n\n/* ???????????? */\n\n// ??´?????¨??´??????????????????\nbool isis_ll(const Line& l, const Line& m) {\n\treturn !eq(cross(l.b - l.a, m.b - m.a), 0);\n}\n\n// ??´?????¨?????????????????????\nbool isis_ls(const Line& l, const Line& s) {\n\treturn isis_ll(l, s) &&\n\t\t(cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < eps);\n}\n\n// ????????¨?????????????????????\nbool isis_ss(const Line& s, const Line& t) {\n\treturn ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 &&\n\t\tccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0;\n}\n\n// ????????´????????????\nbool isis_lp(const Line& l, const Point& p) {\n\treturn (abs(cross(l.b - p, l.a - p)) < eps);\n}\n\n// ?????????????????????\nbool isis_sp(const Line& s, const Point& p) {\n\treturn (abs(s.a - p) + abs(s.b - p) - abs(s.b - s.a) < eps);\n}\n\n// ??????????¶?\nPoint proj(const Line &l, const Point& p) {\n\tld t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n\treturn l.a + t * (l.a - l.b);\n}\n\n// ??´?????¨??´????????????\nPoint is_ll(const Line &s, const Line& t) {\n\tPoint sv = s.b - s.a, tv = t.b - t.a;\n\tassert(cross(sv, tv) != 0);\n\treturn s.a + sv * cross(tv, t.a - s.a) / cross(tv, sv);\n}\n// ??´?????¨??´????????????\nvector<Point> is_ll2(const Line &s, const Line& t) {\n\tPoint sv = s.b - s.a, tv = t.b - t.a;\n\tif (cross(sv, tv) != 0)return vector<Point>(1, is_ll(s, t));\n\telse {\n\t\tvector<Point>ans;\n\t\tfor (int k = 0; k < 2; ++k) {\n\t\t\tif (isis_sp(s, t[k]) && find(ans.begin(), ans.end(), t[k]) == ans.end())ans.push_back(t[k]);\n\t\t\tif (isis_sp(t, s[k]) && find(ans.begin(), ans.end(), s[k]) == ans.end())ans.push_back(s[k]);\n\t\t}\n\t\treturn ans;\n\t}\n}\n// ????????¨???????????????\n//???????????£????????¨???????????¨assert(false)\nPoint is_ss(const Line &s, const Line& t) {\n\tif (isis_ss(s, t)) {\n\t\tfor (int k = 0; k < 2; ++k) {\n\t\t\tfor (int l = 0; l < 2; ++l) {\n\t\t\t\tif (s[k] == t[l])return s[k];\n\t\t\t}\n\t\t}\n\t\treturn is_ll(s, t);\n\t}\n\telse {\n\t\t//??????isis_ss?????????\n\t\tassert(false);\n\t\treturn Point(0, 0);\n\t}\n}\n// ??´?????¨???????????¢\nld dist_lp(const Line& l, const Point& p) {\n\treturn abs(p - proj(l, p));\n}\n\n// ??´?????¨??´???????????¢\nld dist_ll(const Line& l, const Line& m) {\n\treturn isis_ll(l, m) ? 0 : dist_lp(l, m.a);\n}\n\n// ??´?????¨??????????????¢\nld dist_ls(const Line& l, const Line& s) {\n\treturn isis_ls(l, s) ? 0 : min(dist_lp(l, s.a), dist_lp(l, s.b));\n}\n\n// ????????¨???????????¢\nld dist_sp(const Line& s, const Point& p) {\n\tPoint r = proj(s, p);\n\treturn isis_sp(s, r) ? abs(r - p) : min(abs(s.a - p), abs(s.b - p));\n}\n\n// ????????¨??????????????¢\nld dist_ss(const Line& s, const Line& t) {\n\tif (isis_ss(s, t)) return 0;\n\treturn min({ dist_sp(s, t.a), dist_sp(s, t.b), dist_sp(t, s.a), dist_sp(t, s.b) });\n}\n\n\n//??´?????¨??´?????????????????????????????????\nLine bisection(const Line &s, const Line &t) {\n\tconst Point laglanju(is_ll(s, t));\n\tconst Point avec = !(abs(laglanju - s[0])<eps) ? s[0] - laglanju : s[1] - laglanju;\n\tconst Point bvec = !(abs(laglanju - t[0])<eps) ? t[0] - laglanju : t[1] - laglanju;\n\n\treturn Line(laglanju, laglanju + (abs(bvec)*avec + abs(avec)*bvec) / (abs(avec) + abs(bvec)));\n}\n\n\n//???????????´?????????????????????\n//???????????´??????????????§???????????¨????¢?????????¨?????????\nPoint inner_center(const vector<Line>&ls) {\n\tvector<Point>vertics;\n\tfor (int i = 0; i <static_cast<int>(ls.size()); ++i) {\n\t\tvertics.push_back(is_ll(ls[i], ls[(i + 1) % 3]));\n\t}\n\tif (vertics[0] == vertics[1] || vertics[1] == vertics[2] || vertics[2] == vertics[0])return vertics[0];\n\tLine bi1(bisection(Line(vertics[0], vertics[1]), Line(vertics[0], vertics[2])));\n\tLine bi2(bisection(Line(vertics[1], vertics[2]), Line(vertics[1], vertics[0])));\n\tif (bi1[0] == bi2[0])return bi1[0];\n\telse {\n\t\treturn is_ll(bi1, bi2);\n\t}\n}\n\n//???????????´?????????????????????\n//???????????´??????????????§???????????¨????¢?????????¨?????????\nvector<Point> ex_center(const vector<Line>&ls) {\n\tvector<Point>vertics;\n\tfor (int i = 0; i < static_cast<int>(ls.size()); ++i) {\n\t\tvertics.push_back(is_ll(ls[i], ls[(i + 1) % 3]));\n\t}\n\tif (abs(vertics[0] - vertics[1])<eps || abs(vertics[1] - vertics[2])<eps || (abs(vertics[2] - vertics[0])<eps))return vector<Point>();\n\tvector<Point>ecs;\n\tfor (int i = 0; i < 3; ++i) {\n\t\tLine bi1(bisection(Line(vertics[i], vertics[i] * 2.0l - vertics[(i + 2) % 3]), Line(vertics[i], vertics[(i + 1) % 3])));\n\t\tLine bi2(bisection(Line(vertics[(i + 1) % 3], vertics[(i + 1) % 3] * 2.0l - vertics[(i + 2) % 3]), Line(vertics[(i + 1) % 3], vertics[i])));\n\t\tecs.push_back(is_ll(bi1, bi2));\n\t}\n\treturn ecs;\n}\n\n\n//a,b:??????\n//c:????????§??????\n//???????????´?????????????????¢?????????????±??????????\nvector<Point> same_dis(const vector<Line>&ls) {\n\tvector<Point>vertics;\n\tvertics.push_back(is_ll(ls[0], ls[2]));\n\tvertics.push_back(is_ll(ls[1], ls[2]));\n\n\tif (abs(vertics[0] - vertics[1]) < eps)return vector<Point>{vertics[0]};\n\tLine bis(bisection(ls[0], ls[1]));\n\tvector<Point>ecs;\n\n\tLine abi(bisection(Line(vertics[0], vertics[1]), ls[0]));\n\tecs.push_back(is_ll(bis, abi));\n\n\n\tLine bbi(bisection(Line(vertics[0], 2.l*vertics[0] - vertics[1]), ls[0]));\n\tecs.push_back(is_ll(bis, bbi));\n\n\treturn ecs;\n}\n/* ??? */\n\n// ?????¨????????????\nvector<Point> is_cc(const Circle& c1, const Circle& c2) {\n\tvector<Point> res;\n\tld d = abs(c1.p - c2.p);\n\tld rc = (d * d + c1.r * c1.r - c2.r * c2.r) / (2 * d);\n\tld dfr = c1.r * c1.r - rc * rc;\n\tif (abs(dfr) < eps) dfr = 0.0;\n\telse if (dfr < 0.0) return res; // no intersection\n\tld rs = sqrt(dfr);\n\tPoint diff = (c2.p - c1.p) / d;\n\tres.push_back(c1.p + diff * Point(rc, rs));\n\tif (dfr != 0.0) res.push_back(c1.p + diff * Point(rc, -rs));\n\treturn res;\n}\n\n//???????????????????????????\n// 0 => out\n// 1 => on\n// 2 => in\nint is_in_circle(const Circle &cir, const Point& p) {\n\tld dis = abs(cir.p - p);\n\tif (dis > cir.r + eps)return 0;\n\telse if (dis < cir.r - eps)return 2;\n\telse return 1;\n}\n//???lc??????rc??????????????????\n// 0 => out\n// 1 => on\n// 2 => in\nint circle_in_circle(const Circle &lc, const Circle&rc) {\n\tld dis = abs(lc.p - rc.p);\n\tif (dis < rc.r - lc.r - eps)return 2;\n\telse if (dis>rc.r - lc.r + eps)return 0;\n\telse return 1;\n}\n\n// ?????¨??´????????????\nvector<Point> is_lc(const Circle& c, const Line& l) {\n\tvector<Point> res;\n\tld d = dist_lp(l, c.p);\n\tif (d < c.r + eps) {\n\t\tld len = (d > c.r) ? 0.0 : sqrt(c.r * c.r - d * d); //safety;\n\t\tPoint nor = (l.a - l.b) / abs(l.a - l.b);\n\t\tres.push_back(proj(l, c.p) + len * nor);\n\t\tres.push_back(proj(l, c.p) - len * nor);\n\t}\n\treturn res;\n}\n\n// ?????¨??????????????¢\nvector<Point> is_sc(const Circle& c, const Line& l) {\n\tvector<Point> v = is_lc(c, l), res;\n\tfor (Point p : v)\n\t\tif (isis_sp(l, p)) res.push_back(p);\n\treturn res;\n}\n\n// ?????¨????????\\???\nvector<Line> tangent_cp(const Circle& c, const Point& p) {\n\tvector<Line> ret;\n\tPoint v = c.p - p;\n\tld d = abs(v);\n\tld l = sqrt(norm(v) - c.r * c.r);\n\tif (isnan(l)) { return ret; }\n\tPoint v1 = v * Point(l / d, c.r / d);\n\tPoint v2 = v * Point(l / d, -c.r / d);\n\tret.push_back(Line(p, p + v1));\n\tif (l < eps) return ret;\n\tret.push_back(Line(p, p + v2));\n\treturn ret;\n}\n\n// ?????¨????????\\???\nvector<Line> tangent_cc(const Circle& c1, const Circle& c2) {\n\tvector<Line> ret;\n\tif (abs(c1.p - c2.p) - (c1.r + c2.r) > -eps) {\n\t\tPoint center = (c1.p * c2.r + c2.p * c1.r) / (c1.r + c2.r);\n\t\tret = tangent_cp(c1, center);\n\t}\n\tif (abs(c1.r - c2.r) > eps) {\n\t\tPoint out = (-c1.p * c2.r + c2.p * c1.r) / (c1.r - c2.r);\n\t\tvector<Line> nret = tangent_cp(c1, out);\n\t\tret.insert(ret.end(), ALL(nret));\n\t}\n\telse {\n\t\tPoint v = c2.p - c1.p;\n\t\tv /= abs(v);\n\t\tPoint q1 = c1.p + v * Point(0, 1) * c1.r;\n\t\tPoint q2 = c1.p + v * Point(0, -1) * c1.r;\n\t\tret.push_back(Line(q1, q1 + v));\n\t\tret.push_back(Line(q2, q2 + v));\n\t}\n\treturn ret;\n}\n//??????????????????????????¢???\nld two_circle_area(const Circle&l, const Circle&r) {\n\tld dis = abs(l.p - r.p);\n\tif (dis > l.r + r.r)return 0;\n\telse if (dis + r.r < l.r) {\n\t\treturn r.r*r.r*pi;\n\t}\n\telse if (dis + l.r < r.r) {\n\t\treturn l.r*l.r*pi;\n\t}\n\telse {\n\t\tld ans = (l.r)*(l.r)*acos((dis*dis + l.r*l.r - r.r*r.r) / (2 * dis*l.r)) +\n\t\t\t(r.r)*(r.r)*acos((dis*dis + r.r*r.r - l.r*l.r) / (2 * dis*r.r)) -\n\t\t\tsqrt(4 * dis*dis*l.r*l.r - (dis*dis + l.r*l.r - r.r*r.r)*(dis*dis + l.r*l.r - r.r*r.r)) / 2;\n\t\treturn ans;\n\t}\n\n}\n\n/* ????§???¢ */\n\ntypedef vector<Point> Polygon;\n\n// ??¢???\nld area(const Polygon &p) {\n\tld res = 0;\n\tint n = p.size();\n\tREP(j, n) res += cross(p[j], p[(j + 1) % n]);\n\treturn res / 2;\n}\n\n// ????§???¢????????¢??????\nbool is_counter_clockwise(const Polygon &poly) {\n\tld angle = 0;\n\tint n = poly.size();\n\tREP(i, n) {\n\t\tPoint a = poly[i], b = poly[(i + 1) % n], c = poly[(i + 2) % n];\n\t\tangle += arg((c - b) / (b - a));\n\t}\n\treturn angle > eps;\n}\n\n// ??????????????????\n// 0 => out\n// 1 => on\n// 2 => in\nint is_in_polygon(const Polygon &poly, const Point& p) {\n\tld angle = 0;\n\tint n = poly.size();\n\tREP(i, n) {\n\t\tPoint a = poly[i], b = poly[(i + 1) % n];\n\t\tif (isis_sp(Line(a, b), p)) return 1;\n\t\tangle += arg((b - p) / (a - p));\n\t}\n\treturn eq(angle, 0) ? 0 : 2;\n}\n//??????????????????2?????????\nenum { OUT, ON, IN };\nint convex_contains(const Polygon &P, const Point &p) {\n\tconst int n = P.size();\n\tPoint g = (P[0] + P[n / 3] + P[2 * n / 3]) / 3.0l; // inner-point\n\tint a = 0, b = n;\n\twhile (a + 1 < b) { // invariant: c is in fan g-P[a]-P[b]\n\t\tint c = (a + b) / 2;\n\t\tif (cross(P[a] - g, P[c] - g) > 0) { // angle < 180 deg\n\t\t\tif (cross(P[a] - g, p - g) > 0 && cross(P[c] - g, p - g) < 0) b = c;\n\t\t\telse a = c;\n\t\t}\n\t\telse {\n\t\t\tif (cross(P[a] - g, p - g) < 0 && cross(P[c] - g, p - g) > 0) a = c;\n\t\t\telse b = c;\n\t\t}\n\t}\n\tb %= n;\n\tif (cross(P[a] - p, P[b] - p) < 0) return 0;\n\tif (cross(P[a] - p, P[b] - p) > 0) return 2;\n\treturn 1;\n}\n\n// ??????\n// ???????????????????????¨????????????????????§??¨???\nPolygon convex_hull(vector<Point> ps) {\n\tint n = ps.size();\n\tint k = 0;\n\tsort(ps.begin(), ps.end());\n\tPolygon ch(2 * n);\n\tfor (int i = 0; i < n; ch[k++] = ps[i++])\n\t\twhile (k >= 2 && ccw(ch[k - 2], ch[k - 1], ps[i]) <= 0) --k;\n\tfor (int i = n - 2, t = k + 1; i >= 0; ch[k++] = ps[i--])\n\t\twhile (k >= t && ccw(ch[k - 2], ch[k - 1], ps[i]) <= 0) --k;\n\tch.resize(k - 1);\n\treturn ch;\n}\n\n\n\n// ????????????\nvector<Polygon> convex_cut(const Polygon &ps, const Line& l) {\n\tint n = ps.size();\n\tPolygon Q;\n\tPolygon R;\n\tREP(i, n) {\n\t\tPoint A = ps[i], B = ps[(i + 1) % n];\n\t\tLine m = Line(A, B);\n\t\tif (ccw(l.a, l.b, A) != -1) Q.push_back(A);\n\t\tif (ccw(l.a, l.b, A) != 1) R.push_back(A);\n\t\tif (ccw(l.a, l.b, A) * ccw(l.a, l.b, B) < 0 && isis_ll(l, m)) {\n\t\t\tQ.push_back(is_ll(l, m));\n\t\t\tR.push_back(is_ll(l, m));\n\t\t}\n\t}\n\tconst vector<Polygon>polys{ Q,R };\n\treturn polys;\n}\n\n\n/* ??¢??¬??????????????? */\nvoid add_point(vector<Point> &ps, const Point p) {\n\tfor (Point q : ps) if (abs(q - p) < eps) return;\n\tps.push_back(p);\n}\n\ntypedef int Weight;\nstruct Edge {\n\tint src, dst;\n\tWeight weight;\n\tEdge(int src, int dst, Weight weight) :\n\t\tsrc(src), dst(dst), weight(weight) { }\n};\n\ntypedef vector<Edge> Edges;\ntypedef vector<Edges> Graph;\n\nvoid add_edge(Graph &g, const int from, const int to, const Weight weight) {\n\tg[from].push_back(Edge{ from, to, weight });\n}\n\nGraph segment_arrangement(const vector<Line> &s, const vector<Point> &p) {\n\tint n = p.size(), m = s.size();\n\tGraph g(n);\n\tREP(i, m) {\n\t\tvector<pair<ld, int>> vec;\n\t\tREP(j, n) if (isis_sp(s[i], p[j]))\n\t\t\tvec.emplace_back(abs(s[i].a - p[j]), j);\n\t\tsort(ALL(vec));\n\t\tREP(j, vec.size() - 1) {\n\t\t\tint from = vec[j].second, to = vec[j + 1].second;\n\t\t\tadd_edge(g, from, to, static_cast<Weight>(abs(p[from] - p[to])));\n\t\t}\n\t}\n\treturn g;\n}\nGraph sennbunn_arrangement(const vector<Line>&s) {\n\tvector<Point>crss;\n\tfor (int i = 0; i < static_cast<int>(s.size()); ++i) {\n\t\tfor (int j = i + 1; j < static_cast<int>(s.size()); ++j) {\n\t\t\tif (isis_ss(s[i], s[j])) {\n\t\t\t\tcrss.push_back(is_ll(s[i], s[j]));\n\t\t\t}\n\t\t}\n\t}\n\tfor (int i = 0; i <static_cast<int>(s.size()); ++i) {\n\t\tcrss.push_back(s[i][0]);\n\t\tcrss.push_back(s[i][1]);\n\t}\n\treturn segment_arrangement(s, crss);\n}\n\n//Graph circle_arrangement(const vector<Circle> &c, const vector<Point> &p) {\n//\tint n = p.size(), m = c.size();\n//\tGraph g(n);\n//\tREP(i, m) {\n//\t\tvector<pair<ld, int>> vec;\n//\t\tREP(j, n) if (abs(abs(c[i].p - p[j]) - c[i].r) < eps)\n//\t\t\tvec.emplace_back(arg(c[i].p - p[j]), j);\n//\t\tsort(ALL(vec));\n//\t\tREP(j, vec.size() - 1) {\n//\t\t\tint from = vec[j].second, to = vec[j + 1].second;\n//\t\t\tld angle = vec[j + 1].first - vec[j].first;\n//\t\t\tadd_edge(g, from, to, static_cast<Weight>(angle * c[i].r));\n//\t\t}\n//\t\tif (vec.size() >= 2) {\n//\t\t\tint from = vec.back().second, to = vec.front().first;\n//\t\t\tld angle = vec.front().first - vec.back().first;\n//\t\t\tadd_edge(g, from, to, static_cast<Weight>(angle * c[i].r));\n//\t\t}\n//\t}\n//\treturn g;\n//}\n\n\n\nconst ld G= 9.8;\n\nstruct box {\n\tvector<ld>xs;\n\tvector<ld>ys;\n};\n\n\nint N, V, X, Y;\nbool check(const ld theta, const box& b) {\n\n\tconst ld vx = V*cos(theta);\n\tconst ld vy = V*sin(theta);\n\n\tld y_max = -1e18;\n\tld y_min = 1e18;\n\n\t//?????????????¢????\n\t{\n\t\tconst ld toptime = vy / G;\n\t\tconst ld top_x = toptime*vx;\n\t\tconst ld top_y = vy*toptime - toptime*toptime*G / 2;\n\t\tif (b.xs[0] < top_x&&top_x < b.xs[1]) {\n\t\t\ty_max = max(y_max, top_y);\n\t\t}\n\t}\n\t//???????????????????¢????\n\t{\n\t\tfor (int x = 0; x < 2; ++x) {\n\t\t\tconst ld time = b.xs[x]/vx;\n\t\t\tconst ld ay = vy*time - time*time*G / 2;\n\t\t\ty_max = max(y_max, ay);\n\t\t\ty_min = min(y_min, ay);\n\t\t}\n\t}\n\tif (y_max- eps < b.ys[0] || b.ys[1] < y_min + eps) {\n\t\treturn true;\n\t}\n\telse {\n\t\treturn false;\n\t}\n}\n\nvoid gettan(vector<ld>&tans,const Point& p,int leftup) {\n\tif (p.real() < eps)return;\n\tbool ok = true;\n\tld amin, amax;\n\n\t//?????????????¨????\n\t{\n\t\tconst ld tan = V*V / G / p.real();\n\t\tconst ld theta = atan(tan);\n\t\tconst ld vx = V*cos(theta);\n\t\tconst ld vy = V*sin(theta);\n\t\tconst ld time = p.real() / vx;\n\t\tconst ld max_y = vy*time - G*time*time / 2;\n\t\tif (max_y < p.imag()) {\n\t\t\tok = false;\n\t\t}\n\t\tif (leftup) {\n\t\t\tamax = tan;\n\t\t\tamin = 0;\n\t\t}\n\t\telse {\n\t\t\tamax = 1e18;\n\t\t\tamin = tan;\n\t\t}\n\t}\n\tif (ok) {\n\t\tint rep = 1000;\n\t\twhile (rep--) {\n\t\t\tconst ld amidtan = (amin + amax) / 2;\n\t\t\tconst ld theta = atan(amidtan);\n\t\t\tconst ld vx = V*cos(theta);\n\t\t\tconst ld vy = V*sin(theta);\n\t\t\tconst ld time = p.real() / vx;\n\t\t\tconst ld ay = vy*time - G*time*time / 2;\n\t\t\tif (ay > p.imag()) {\n\t\t\t\tif (leftup) {\n\t\t\t\t\tamax = amidtan;\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tamin = amidtan;\n\t\t\t\t}\n\t\t\t}\n\t\t\telse {\n\t\t\t\tif (leftup) {\n\t\t\t\t\tamin = amidtan;\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tamax = amidtan;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tconst ld amidtan = (amin + amax) / 2;\n\t\tconst ld theta = atan(amidtan);\n\t\tconst ld vx = V*cos(theta);\n\t\tconst ld vy = V*sin(theta);\n\t\tconst ld time = p.real() / vx;\n\t\tconst ld ay = vy*time - G*time*time / 2;\n\t\ttans.push_back(amin);\n\t}\n}\n\nint main() { cin >> N >> V >> X >> Y;\n\tvector<box>bs;\n\tfor (int i = 0; i < N; ++i) {\n\t\tint L, B, R, T; cin >> L >> B >> R >> T;\n\t\tbox b;\n\t\tb.xs.push_back(L);\n\t\tb.xs.push_back(R);\n\t\tb.ys.push_back(B);\n\t\tb.ys.push_back(T);\n\t\tbs.push_back(b);\n\t}\n\tconst ld down = Y;\n\tld up = 1e18;\n\tfor (int i = 0; i < N; ++i) {\n\t\tif (bs[i].xs[0] < X&&X < bs[i].xs[1]) {\n\t\t\tif (bs[i].ys[0] > Y) {\n\n\t\t\t\tup = min(up, bs[i].ys[0]);\n\t\t\t}\n\t\t}\n\t}\n\tvector<ld>tans;\n\tfor (auto b : bs) {\n\t\tfor (int x = 0; x < 2; ++x) {\n\t\t\tfor (int y = 0; y < 2; ++y) {\n\t\t\t\tPoint p(b.xs[x], b.ys[y]);\n\t\t\t\tgettan(tans, p,(x+y)%2);\n\t\t\t\tgettan(tans, p, (x + y+1) % 2);\n\t\t\t}\n\t\t}\n\t}\n\t{\n\t\tPoint p(X,Y);\n\t\tgettan(tans, p,true);\n\t}\n\tsort(tans.begin(), tans.end());\n\t{\n\t\tvector<ld>ntans;\n\t\tfor (int i = 0; i < tans.size(); ++i) {\n\t\t\tntans.push_back(tans[i]);\n\t\t\tif (i != tans.size() - 1) {\n\t\t\t\tntans.push_back((tans[i] + tans[i + 1]) / 2);\n\t\t\t}\n\t\t}\n\t\ttans = ntans;\n\t}\n\n\tstring ans = \"No\";\n\tfor (auto t : tans) {\n\t\tconst ld theta = atan(t);\n\t\tconst ld vx = V*cos(theta);\n\t\tconst ld vy = V*sin(theta);\n\t\tconst ld time = X / vx;\n\t\tconst ld ay = vy*time - G*time*time / 2;\n\t\tif (down-eps <= ay&&ay <= up+eps) {\n\t\t\tbool ok = true;\n\t\t\tfor (auto b : bs) {\n\t\t\t\tif (!check(theta, b))ok = false;\n\t\t\t}\n\t\t\tif (ok) {\n\t\t\t\tans = \"Yes\";\n\t\t\t}\n\t\t}\n\t\telse {\n\t\t\tcontinue;\n\t\t}\n\t}\n\tcout << ans << endl;\n\treturn 0;\n}", "accuracy": 0.2, "time_ms": 70, "memory_kb": 3484, "score_of_the_acc": -0.9461, "final_rank": 16 }, { "submission_id": "aoj_2308_1777166", "code_snippet": "#include \"bits/stdc++.h\"\n#include<unordered_map>\n#include<unordered_set>\n#pragma warning(disable:4996)\nusing namespace std;\nusing ld = long double;\ntemplate<class T>\nusing Table = vector<vector<T>>;\nconst ld eps=1e-9;\n\n//// < \"D:\\D_Download\\Visual Studio 2015\\Projects\\programing_contest_c++\\Debug\\a.txt\"\n\n\n/* ??????????????¬ */\n\n#include <complex>\n\ntypedef long double ld;\ntypedef complex<ld> Point;\n#define REP(i,n) for(int i=0;i<(int)(n);i++)\n#define ALL(x) (x).begin(),(x).end()\n\n\nconst ld pi = acos(-1.0);\nconst ld dtop = pi / 180.;\nconst ld ptod = 1. / dtop;\n\nnamespace std {\n\tbool operator<(const Point &lhs, const Point &rhs) {\n\t\tif (lhs.real() < rhs.real() - eps) return true;\n\t\tif (lhs.real() > rhs.real() + eps) return false;\n\t\treturn lhs.imag() < rhs.imag();\n\t}\n}\n\n// ????????\\???\nPoint input_point() {\n\tld x, y;\n\tcin >> x >> y;\n\treturn Point(x, y);\n}\n\n// ????????????????????????\nbool eq(const ld a, const ld b) {\n\treturn (abs(a - b) < eps);\n}\n\n// ??????\nld dot(const Point& a, const Point& b) {\n\treturn real(conj(a) * b);\n}\n\n// ??????\nld cross(const Point& a, const Point& b) {\n\treturn imag(conj(a) * b);\n}\n\n// ??´????????????\nclass Line {\npublic:\n\tPoint a, b;\n\tLine() : a(Point(0, 0)), b(Point(0, 0)) {}\n\tLine(Point a, Point b) : a(a), b(b) {}\n\tPoint operator[](const int _num)const {\n\t\tif (_num == 0)return a;\n\t\telse if (_num == 1)return b;\n\t\telse {\n\t\t\tassert(false);\n\t\t\treturn Point();\n\t\t}\n\t}\n};\n\n// ????????????\nclass Circle {\npublic:\n\tPoint p;\n\tld r;\n\tCircle() : p(Point(0, 0)), r(0) {}\n\tCircle(Point p, ld r) : p(p), r(r) {}\n};\n\n// CCW\nint ccw(const Point& a, const Point &b, const Point &c) {\n\tconst Point nb(b - a);\n\tconst Point nc(c - a);\n\tif (cross(nb, nc) > eps) return 1; // a,b,c??????????¨???¨?????????????????¶\n\tif (cross(nb, nc) < -eps) return -1; // a,b,c???????¨???¨?????????????????¶\n\tif (dot(nb, nc) < 0) return 2; // c,a,b???????????´???????????¶\n\tif (norm(nb) < norm(nc)) return -2; // a,b,c???????????´???????????¶\n\treturn 0; // a,c,b???????????´???????????¶\n}\n\n\n/* ???????????? */\n\n// ??´?????¨??´??????????????????\nbool isis_ll(const Line& l, const Line& m) {\n\treturn !eq(cross(l.b - l.a, m.b - m.a), 0);\n}\n\n// ??´?????¨?????????????????????\nbool isis_ls(const Line& l, const Line& s) {\n\treturn isis_ll(l, s) &&\n\t\t(cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < eps);\n}\n\n// ????????¨?????????????????????\nbool isis_ss(const Line& s, const Line& t) {\n\treturn ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 &&\n\t\tccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0;\n}\n\n// ????????´????????????\nbool isis_lp(const Line& l, const Point& p) {\n\treturn (abs(cross(l.b - p, l.a - p)) < eps);\n}\n\n// ?????????????????????\nbool isis_sp(const Line& s, const Point& p) {\n\treturn (abs(s.a - p) + abs(s.b - p) - abs(s.b - s.a) < eps);\n}\n\n// ??????????¶?\nPoint proj(const Line &l, const Point& p) {\n\tld t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n\treturn l.a + t * (l.a - l.b);\n}\n\n// ??´?????¨??´????????????\nPoint is_ll(const Line &s, const Line& t) {\n\tPoint sv = s.b - s.a, tv = t.b - t.a;\n\tassert(cross(sv, tv) != 0);\n\treturn s.a + sv * cross(tv, t.a - s.a) / cross(tv, sv);\n}\n// ??´?????¨??´????????????\nvector<Point> is_ll2(const Line &s, const Line& t) {\n\tPoint sv = s.b - s.a, tv = t.b - t.a;\n\tif (cross(sv, tv) != 0)return vector<Point>(1, is_ll(s, t));\n\telse {\n\t\tvector<Point>ans;\n\t\tfor (int k = 0; k < 2; ++k) {\n\t\t\tif (isis_sp(s, t[k]) && find(ans.begin(), ans.end(), t[k]) == ans.end())ans.push_back(t[k]);\n\t\t\tif (isis_sp(t, s[k]) && find(ans.begin(), ans.end(), s[k]) == ans.end())ans.push_back(s[k]);\n\t\t}\n\t\treturn ans;\n\t}\n}\n// ????????¨???????????????\n//???????????£????????¨???????????¨assert(false)\nPoint is_ss(const Line &s, const Line& t) {\n\tif (isis_ss(s, t)) {\n\t\tfor (int k = 0; k < 2; ++k) {\n\t\t\tfor (int l = 0; l < 2; ++l) {\n\t\t\t\tif (s[k] == t[l])return s[k];\n\t\t\t}\n\t\t}\n\t\treturn is_ll(s, t);\n\t}\n\telse {\n\t\t//??????isis_ss?????????\n\t\tassert(false);\n\t\treturn Point(0, 0);\n\t}\n}\n// ??´?????¨???????????¢\nld dist_lp(const Line& l, const Point& p) {\n\treturn abs(p - proj(l, p));\n}\n\n// ??´?????¨??´???????????¢\nld dist_ll(const Line& l, const Line& m) {\n\treturn isis_ll(l, m) ? 0 : dist_lp(l, m.a);\n}\n\n// ??´?????¨??????????????¢\nld dist_ls(const Line& l, const Line& s) {\n\treturn isis_ls(l, s) ? 0 : min(dist_lp(l, s.a), dist_lp(l, s.b));\n}\n\n// ????????¨???????????¢\nld dist_sp(const Line& s, const Point& p) {\n\tPoint r = proj(s, p);\n\treturn isis_sp(s, r) ? abs(r - p) : min(abs(s.a - p), abs(s.b - p));\n}\n\n// ????????¨??????????????¢\nld dist_ss(const Line& s, const Line& t) {\n\tif (isis_ss(s, t)) return 0;\n\treturn min({ dist_sp(s, t.a), dist_sp(s, t.b), dist_sp(t, s.a), dist_sp(t, s.b) });\n}\n\n\n//??´?????¨??´?????????????????????????????????\nLine bisection(const Line &s, const Line &t) {\n\tconst Point laglanju(is_ll(s, t));\n\tconst Point avec = !(abs(laglanju - s[0])<eps) ? s[0] - laglanju : s[1] - laglanju;\n\tconst Point bvec = !(abs(laglanju - t[0])<eps) ? t[0] - laglanju : t[1] - laglanju;\n\n\treturn Line(laglanju, laglanju + (abs(bvec)*avec + abs(avec)*bvec) / (abs(avec) + abs(bvec)));\n}\n\n\n//???????????´?????????????????????\n//???????????´??????????????§???????????¨????¢?????????¨?????????\nPoint inner_center(const vector<Line>&ls) {\n\tvector<Point>vertics;\n\tfor (int i = 0; i <static_cast<int>(ls.size()); ++i) {\n\t\tvertics.push_back(is_ll(ls[i], ls[(i + 1) % 3]));\n\t}\n\tif (vertics[0] == vertics[1] || vertics[1] == vertics[2] || vertics[2] == vertics[0])return vertics[0];\n\tLine bi1(bisection(Line(vertics[0], vertics[1]), Line(vertics[0], vertics[2])));\n\tLine bi2(bisection(Line(vertics[1], vertics[2]), Line(vertics[1], vertics[0])));\n\tif (bi1[0] == bi2[0])return bi1[0];\n\telse {\n\t\treturn is_ll(bi1, bi2);\n\t}\n}\n\n//???????????´?????????????????????\n//???????????´??????????????§???????????¨????¢?????????¨?????????\nvector<Point> ex_center(const vector<Line>&ls) {\n\tvector<Point>vertics;\n\tfor (int i = 0; i < static_cast<int>(ls.size()); ++i) {\n\t\tvertics.push_back(is_ll(ls[i], ls[(i + 1) % 3]));\n\t}\n\tif (abs(vertics[0] - vertics[1])<eps || abs(vertics[1] - vertics[2])<eps || (abs(vertics[2] - vertics[0])<eps))return vector<Point>();\n\tvector<Point>ecs;\n\tfor (int i = 0; i < 3; ++i) {\n\t\tLine bi1(bisection(Line(vertics[i], vertics[i] * 2.0l - vertics[(i + 2) % 3]), Line(vertics[i], vertics[(i + 1) % 3])));\n\t\tLine bi2(bisection(Line(vertics[(i + 1) % 3], vertics[(i + 1) % 3] * 2.0l - vertics[(i + 2) % 3]), Line(vertics[(i + 1) % 3], vertics[i])));\n\t\tecs.push_back(is_ll(bi1, bi2));\n\t}\n\treturn ecs;\n}\n\n\n//a,b:??????\n//c:????????§??????\n//???????????´?????????????????¢?????????????±??????????\nvector<Point> same_dis(const vector<Line>&ls) {\n\tvector<Point>vertics;\n\tvertics.push_back(is_ll(ls[0], ls[2]));\n\tvertics.push_back(is_ll(ls[1], ls[2]));\n\n\tif (abs(vertics[0] - vertics[1]) < eps)return vector<Point>{vertics[0]};\n\tLine bis(bisection(ls[0], ls[1]));\n\tvector<Point>ecs;\n\n\tLine abi(bisection(Line(vertics[0], vertics[1]), ls[0]));\n\tecs.push_back(is_ll(bis, abi));\n\n\n\tLine bbi(bisection(Line(vertics[0], 2.l*vertics[0] - vertics[1]), ls[0]));\n\tecs.push_back(is_ll(bis, bbi));\n\n\treturn ecs;\n}\n/* ??? */\n\n// ?????¨????????????\nvector<Point> is_cc(const Circle& c1, const Circle& c2) {\n\tvector<Point> res;\n\tld d = abs(c1.p - c2.p);\n\tld rc = (d * d + c1.r * c1.r - c2.r * c2.r) / (2 * d);\n\tld dfr = c1.r * c1.r - rc * rc;\n\tif (abs(dfr) < eps) dfr = 0.0;\n\telse if (dfr < 0.0) return res; // no intersection\n\tld rs = sqrt(dfr);\n\tPoint diff = (c2.p - c1.p) / d;\n\tres.push_back(c1.p + diff * Point(rc, rs));\n\tif (dfr != 0.0) res.push_back(c1.p + diff * Point(rc, -rs));\n\treturn res;\n}\n\n//???????????????????????????\n// 0 => out\n// 1 => on\n// 2 => in\nint is_in_circle(const Circle &cir, const Point& p) {\n\tld dis = abs(cir.p - p);\n\tif (dis > cir.r + eps)return 0;\n\telse if (dis < cir.r - eps)return 2;\n\telse return 1;\n}\n//???lc??????rc??????????????????\n// 0 => out\n// 1 => on\n// 2 => in\nint circle_in_circle(const Circle &lc, const Circle&rc) {\n\tld dis = abs(lc.p - rc.p);\n\tif (dis < rc.r - lc.r - eps)return 2;\n\telse if (dis>rc.r - lc.r + eps)return 0;\n\telse return 1;\n}\n\n// ?????¨??´????????????\nvector<Point> is_lc(const Circle& c, const Line& l) {\n\tvector<Point> res;\n\tld d = dist_lp(l, c.p);\n\tif (d < c.r + eps) {\n\t\tld len = (d > c.r) ? 0.0 : sqrt(c.r * c.r - d * d); //safety;\n\t\tPoint nor = (l.a - l.b) / abs(l.a - l.b);\n\t\tres.push_back(proj(l, c.p) + len * nor);\n\t\tres.push_back(proj(l, c.p) - len * nor);\n\t}\n\treturn res;\n}\n\n// ?????¨??????????????¢\nvector<Point> is_sc(const Circle& c, const Line& l) {\n\tvector<Point> v = is_lc(c, l), res;\n\tfor (Point p : v)\n\t\tif (isis_sp(l, p)) res.push_back(p);\n\treturn res;\n}\n\n// ?????¨????????\\???\nvector<Line> tangent_cp(const Circle& c, const Point& p) {\n\tvector<Line> ret;\n\tPoint v = c.p - p;\n\tld d = abs(v);\n\tld l = sqrt(norm(v) - c.r * c.r);\n\tif (isnan(l)) { return ret; }\n\tPoint v1 = v * Point(l / d, c.r / d);\n\tPoint v2 = v * Point(l / d, -c.r / d);\n\tret.push_back(Line(p, p + v1));\n\tif (l < eps) return ret;\n\tret.push_back(Line(p, p + v2));\n\treturn ret;\n}\n\n// ?????¨????????\\???\nvector<Line> tangent_cc(const Circle& c1, const Circle& c2) {\n\tvector<Line> ret;\n\tif (abs(c1.p - c2.p) - (c1.r + c2.r) > -eps) {\n\t\tPoint center = (c1.p * c2.r + c2.p * c1.r) / (c1.r + c2.r);\n\t\tret = tangent_cp(c1, center);\n\t}\n\tif (abs(c1.r - c2.r) > eps) {\n\t\tPoint out = (-c1.p * c2.r + c2.p * c1.r) / (c1.r - c2.r);\n\t\tvector<Line> nret = tangent_cp(c1, out);\n\t\tret.insert(ret.end(), ALL(nret));\n\t}\n\telse {\n\t\tPoint v = c2.p - c1.p;\n\t\tv /= abs(v);\n\t\tPoint q1 = c1.p + v * Point(0, 1) * c1.r;\n\t\tPoint q2 = c1.p + v * Point(0, -1) * c1.r;\n\t\tret.push_back(Line(q1, q1 + v));\n\t\tret.push_back(Line(q2, q2 + v));\n\t}\n\treturn ret;\n}\n//??????????????????????????¢???\nld two_circle_area(const Circle&l, const Circle&r) {\n\tld dis = abs(l.p - r.p);\n\tif (dis > l.r + r.r)return 0;\n\telse if (dis + r.r < l.r) {\n\t\treturn r.r*r.r*pi;\n\t}\n\telse if (dis + l.r < r.r) {\n\t\treturn l.r*l.r*pi;\n\t}\n\telse {\n\t\tld ans = (l.r)*(l.r)*acos((dis*dis + l.r*l.r - r.r*r.r) / (2 * dis*l.r)) +\n\t\t\t(r.r)*(r.r)*acos((dis*dis + r.r*r.r - l.r*l.r) / (2 * dis*r.r)) -\n\t\t\tsqrt(4 * dis*dis*l.r*l.r - (dis*dis + l.r*l.r - r.r*r.r)*(dis*dis + l.r*l.r - r.r*r.r)) / 2;\n\t\treturn ans;\n\t}\n\n}\n\n/* ????§???¢ */\n\ntypedef vector<Point> Polygon;\n\n// ??¢???\nld area(const Polygon &p) {\n\tld res = 0;\n\tint n = p.size();\n\tREP(j, n) res += cross(p[j], p[(j + 1) % n]);\n\treturn res / 2;\n}\n\n// ????§???¢????????¢??????\nbool is_counter_clockwise(const Polygon &poly) {\n\tld angle = 0;\n\tint n = poly.size();\n\tREP(i, n) {\n\t\tPoint a = poly[i], b = poly[(i + 1) % n], c = poly[(i + 2) % n];\n\t\tangle += arg((c - b) / (b - a));\n\t}\n\treturn angle > eps;\n}\n\n// ??????????????????\n// 0 => out\n// 1 => on\n// 2 => in\nint is_in_polygon(const Polygon &poly, const Point& p) {\n\tld angle = 0;\n\tint n = poly.size();\n\tREP(i, n) {\n\t\tPoint a = poly[i], b = poly[(i + 1) % n];\n\t\tif (isis_sp(Line(a, b), p)) return 1;\n\t\tangle += arg((b - p) / (a - p));\n\t}\n\treturn eq(angle, 0) ? 0 : 2;\n}\n//??????????????????2?????????\nenum { OUT, ON, IN };\nint convex_contains(const Polygon &P, const Point &p) {\n\tconst int n = P.size();\n\tPoint g = (P[0] + P[n / 3] + P[2 * n / 3]) / 3.0l; // inner-point\n\tint a = 0, b = n;\n\twhile (a + 1 < b) { // invariant: c is in fan g-P[a]-P[b]\n\t\tint c = (a + b) / 2;\n\t\tif (cross(P[a] - g, P[c] - g) > 0) { // angle < 180 deg\n\t\t\tif (cross(P[a] - g, p - g) > 0 && cross(P[c] - g, p - g) < 0) b = c;\n\t\t\telse a = c;\n\t\t}\n\t\telse {\n\t\t\tif (cross(P[a] - g, p - g) < 0 && cross(P[c] - g, p - g) > 0) a = c;\n\t\t\telse b = c;\n\t\t}\n\t}\n\tb %= n;\n\tif (cross(P[a] - p, P[b] - p) < 0) return 0;\n\tif (cross(P[a] - p, P[b] - p) > 0) return 2;\n\treturn 1;\n}\n\n// ??????\n// ???????????????????????¨????????????????????§??¨???\nPolygon convex_hull(vector<Point> ps) {\n\tint n = ps.size();\n\tint k = 0;\n\tsort(ps.begin(), ps.end());\n\tPolygon ch(2 * n);\n\tfor (int i = 0; i < n; ch[k++] = ps[i++])\n\t\twhile (k >= 2 && ccw(ch[k - 2], ch[k - 1], ps[i]) <= 0) --k;\n\tfor (int i = n - 2, t = k + 1; i >= 0; ch[k++] = ps[i--])\n\t\twhile (k >= t && ccw(ch[k - 2], ch[k - 1], ps[i]) <= 0) --k;\n\tch.resize(k - 1);\n\treturn ch;\n}\n\n\n\n// ????????????\nvector<Polygon> convex_cut(const Polygon &ps, const Line& l) {\n\tint n = ps.size();\n\tPolygon Q;\n\tPolygon R;\n\tREP(i, n) {\n\t\tPoint A = ps[i], B = ps[(i + 1) % n];\n\t\tLine m = Line(A, B);\n\t\tif (ccw(l.a, l.b, A) != -1) Q.push_back(A);\n\t\tif (ccw(l.a, l.b, A) != 1) R.push_back(A);\n\t\tif (ccw(l.a, l.b, A) * ccw(l.a, l.b, B) < 0 && isis_ll(l, m)) {\n\t\t\tQ.push_back(is_ll(l, m));\n\t\t\tR.push_back(is_ll(l, m));\n\t\t}\n\t}\n\tconst vector<Polygon>polys{ Q,R };\n\treturn polys;\n}\n\n\n/* ??¢??¬??????????????? */\nvoid add_point(vector<Point> &ps, const Point p) {\n\tfor (Point q : ps) if (abs(q - p) < eps) return;\n\tps.push_back(p);\n}\n\ntypedef int Weight;\nstruct Edge {\n\tint src, dst;\n\tWeight weight;\n\tEdge(int src, int dst, Weight weight) :\n\t\tsrc(src), dst(dst), weight(weight) { }\n};\n\ntypedef vector<Edge> Edges;\ntypedef vector<Edges> Graph;\n\nvoid add_edge(Graph &g, const int from, const int to, const Weight weight) {\n\tg[from].push_back(Edge{ from, to, weight });\n}\n\nGraph segment_arrangement(const vector<Line> &s, const vector<Point> &p) {\n\tint n = p.size(), m = s.size();\n\tGraph g(n);\n\tREP(i, m) {\n\t\tvector<pair<ld, int>> vec;\n\t\tREP(j, n) if (isis_sp(s[i], p[j]))\n\t\t\tvec.emplace_back(abs(s[i].a - p[j]), j);\n\t\tsort(ALL(vec));\n\t\tREP(j, vec.size() - 1) {\n\t\t\tint from = vec[j].second, to = vec[j + 1].second;\n\t\t\tadd_edge(g, from, to, static_cast<Weight>(abs(p[from] - p[to])));\n\t\t}\n\t}\n\treturn g;\n}\nGraph sennbunn_arrangement(const vector<Line>&s) {\n\tvector<Point>crss;\n\tfor (int i = 0; i < static_cast<int>(s.size()); ++i) {\n\t\tfor (int j = i + 1; j < static_cast<int>(s.size()); ++j) {\n\t\t\tif (isis_ss(s[i], s[j])) {\n\t\t\t\tcrss.push_back(is_ll(s[i], s[j]));\n\t\t\t}\n\t\t}\n\t}\n\tfor (int i = 0; i <static_cast<int>(s.size()); ++i) {\n\t\tcrss.push_back(s[i][0]);\n\t\tcrss.push_back(s[i][1]);\n\t}\n\treturn segment_arrangement(s, crss);\n}\n\n//Graph circle_arrangement(const vector<Circle> &c, const vector<Point> &p) {\n//\tint n = p.size(), m = c.size();\n//\tGraph g(n);\n//\tREP(i, m) {\n//\t\tvector<pair<ld, int>> vec;\n//\t\tREP(j, n) if (abs(abs(c[i].p - p[j]) - c[i].r) < eps)\n//\t\t\tvec.emplace_back(arg(c[i].p - p[j]), j);\n//\t\tsort(ALL(vec));\n//\t\tREP(j, vec.size() - 1) {\n//\t\t\tint from = vec[j].second, to = vec[j + 1].second;\n//\t\t\tld angle = vec[j + 1].first - vec[j].first;\n//\t\t\tadd_edge(g, from, to, static_cast<Weight>(angle * c[i].r));\n//\t\t}\n//\t\tif (vec.size() >= 2) {\n//\t\t\tint from = vec.back().second, to = vec.front().first;\n//\t\t\tld angle = vec.front().first - vec.back().first;\n//\t\t\tadd_edge(g, from, to, static_cast<Weight>(angle * c[i].r));\n//\t\t}\n//\t}\n//\treturn g;\n//}\n\n\n\nconst ld G= 9.8;\n\nstruct box {\n\tvector<ld>xs;\n\tvector<ld>ys;\n};\n\n\nint N, V, X, Y;\nbool check(const ld theta, const box& b) {\n\n\tconst ld vx = V*cos(theta);\n\tconst ld vy = V*sin(theta);\n\n\tld y_max = -1e18;\n\tld y_min = 1e18;\n\n\t//?????????????¢????\n\t{\n\t\tconst ld toptime = vy / G;\n\t\tconst ld top_x = toptime*vx;\n\t\tconst ld top_y = vy*toptime - toptime*toptime*G / 2;\n\t\tif (b.xs[0] < top_x&&top_x < b.xs[1]) {\n\t\t\ty_max = max(y_max, top_y);\n\t\t}\n\t}\n\t//???????????????????¢????\n\t{\n\t\tfor (int x = 0; x < 2; ++x) {\n\t\t\tconst ld time = b.xs[x]/vx;\n\t\t\tconst ld ay = vy*time - time*time*G / 2;\n\t\t\ty_max = max(y_max, ay);\n\t\t\ty_min = min(y_min, ay);\n\t\t}\n\t}\n\tif (y_max- eps < b.ys[0] || b.ys[1] < y_min + eps) {\n\t\treturn true;\n\t}\n\telse {\n\t\treturn false;\n\t}\n}\n\nvoid gettan(vector<ld>&tans,const Point& p,int leftup) {\n\tif (p.real() < eps)return;\n\tbool ok = true;\n\tld amin, amax;\n\n\t//?????????????¨????\n\t{\n\t\tconst ld tan = V*V / G / p.real();\n\t\tconst ld theta = atan(tan);\n\t\tconst ld vx = V*cos(theta);\n\t\tconst ld vy = V*sin(theta);\n\t\tconst ld time = p.real() / vx;\n\t\tconst ld max_y = vy*time - G*time*time / 2;\n\t\tif (max_y < p.imag()) {\n\t\t\tok = false;\n\t\t}\n\t\tif (leftup) {\n\t\t\tamax = tan;\n\t\t\tamin = 0;\n\t\t}\n\t\telse {\n\t\t\tamax = 1e18;\n\t\t\tamin = tan;\n\t\t}\n\t}\n\tif (ok) {\n\t\tint rep = 1000;\n\t\twhile (rep--) {\n\t\t\tconst ld amidtan = (amin + amax) / 2;\n\t\t\tconst ld theta = atan(amidtan);\n\t\t\tconst ld vx = V*cos(theta);\n\t\t\tconst ld vy = V*sin(theta);\n\t\t\tconst ld time = p.real() / vx;\n\t\t\tconst ld ay = vy*time - G*time*time / 2;\n\t\t\tif (ay > p.imag()) {\n\t\t\t\tif (leftup) {\n\t\t\t\t\tamax = amidtan;\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tamin = amidtan;\n\t\t\t\t}\n\t\t\t}\n\t\t\telse {\n\t\t\t\tif (leftup) {\n\t\t\t\t\tamin = amidtan;\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tamax = amidtan;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tconst ld amidtan = (amin + amax) / 2;\n\t\tconst ld theta = atan(amidtan);\n\t\tconst ld vx = V*cos(theta);\n\t\tconst ld vy = V*sin(theta);\n\t\tconst ld time = p.real() / vx;\n\t\tconst ld ay = vy*time - G*time*time / 2;\n\t\ttans.push_back(amin);\n\t}\n}\n\nint main() { cin >> N >> V >> X >> Y;\n\tvector<box>bs;\n\tfor (int i = 0; i < N; ++i) {\n\t\tint L, B, R, T; cin >> L >> B >> R >> T;\n\t\tbox b;\n\t\tb.xs.push_back(L);\n\t\tb.xs.push_back(R);\n\t\tb.ys.push_back(B);\n\t\tb.ys.push_back(T);\n\t\tbs.push_back(b);\n\t}\n\tconst ld down = Y;\n\tld up = 1e18;\n\tfor (int i = 0; i < N; ++i) {\n\t\tif (bs[i].xs[0] < X&&X < bs[i].xs[1]) {\n\t\t\tif (bs[i].ys[0] > Y) {\n\n\t\t\t\tup = min(up, bs[i].ys[0]);\n\t\t\t}\n\t\t}\n\t}\n\tvector<ld>tans;\n\tfor (auto b : bs) {\n\t\tfor (int x = 0; x < 2; ++x) {\n\t\t\tfor (int y = 0; y < 2; ++y) {\n\t\t\t\tPoint p(b.xs[x], b.ys[y]);\n\t\t\t\tgettan(tans, p,(x+y)%2);\n\t\t\t}\n\t\t}\n\t}\n\t{\n\t\tPoint p(X,Y);\n\t\tgettan(tans, p,true);\n\t}\n\tsort(tans.begin(), tans.end());\n\t{\n\t\tvector<ld>ntans;\n\t\tfor (int i = 0; i < tans.size(); ++i) {\n\t\t\tntans.push_back(tans[i]);\n\t\t\tif (i != tans.size() - 1) {\n\t\t\t\tntans.push_back((tans[i] + tans[i + 1]) / 2);\n\t\t\t}\n\t\t}\n\t\t//tans = ntans;\n\t}\n\n\tstring ans = \"No\";\n\tfor (auto t : tans) {\n\t\tconst ld theta = atan(t);\n\t\tconst ld vx = V*cos(theta);\n\t\tconst ld vy = V*sin(theta);\n\t\tconst ld time = X / vx;\n\t\tconst ld ay = vy*time - G*time*time / 2;\n\t\tif (down-eps <= ay&&ay <= up+eps) {\n\t\t\tbool ok = true;\n\t\t\tfor (auto b : bs) {\n\t\t\t\tif (!check(theta, b))ok = false;\n\t\t\t}\n\t\t\tif (ok) {\n\t\t\t\tans = \"Yes\";\n\t\t\t}\n\t\t}\n\t\telse {\n\t\t\tcontinue;\n\t\t}\n\t}\n\tcout << ans << endl;\n\treturn 0;\n}", "accuracy": 0.2, "time_ms": 30, "memory_kb": 3504, "score_of_the_acc": -0.8687, "final_rank": 12 }, { "submission_id": "aoj_2308_1777164", "code_snippet": "#include \"bits/stdc++.h\"\n#include<unordered_map>\n#include<unordered_set>\n#pragma warning(disable:4996)\nusing namespace std;\nusing ld = long double;\ntemplate<class T>\nusing Table = vector<vector<T>>;\nconst ld eps=1e-9;\n\n//// < \"D:\\D_Download\\Visual Studio 2015\\Projects\\programing_contest_c++\\Debug\\a.txt\"\n\n\n/* ??????????????¬ */\n\n#include <complex>\n\ntypedef long double ld;\ntypedef complex<ld> Point;\n#define REP(i,n) for(int i=0;i<(int)(n);i++)\n#define ALL(x) (x).begin(),(x).end()\n\n\nconst ld pi = acos(-1.0);\nconst ld dtop = pi / 180.;\nconst ld ptod = 1. / dtop;\n\nnamespace std {\n\tbool operator<(const Point &lhs, const Point &rhs) {\n\t\tif (lhs.real() < rhs.real() - eps) return true;\n\t\tif (lhs.real() > rhs.real() + eps) return false;\n\t\treturn lhs.imag() < rhs.imag();\n\t}\n}\n\n// ????????\\???\nPoint input_point() {\n\tld x, y;\n\tcin >> x >> y;\n\treturn Point(x, y);\n}\n\n// ????????????????????????\nbool eq(const ld a, const ld b) {\n\treturn (abs(a - b) < eps);\n}\n\n// ??????\nld dot(const Point& a, const Point& b) {\n\treturn real(conj(a) * b);\n}\n\n// ??????\nld cross(const Point& a, const Point& b) {\n\treturn imag(conj(a) * b);\n}\n\n// ??´????????????\nclass Line {\npublic:\n\tPoint a, b;\n\tLine() : a(Point(0, 0)), b(Point(0, 0)) {}\n\tLine(Point a, Point b) : a(a), b(b) {}\n\tPoint operator[](const int _num)const {\n\t\tif (_num == 0)return a;\n\t\telse if (_num == 1)return b;\n\t\telse {\n\t\t\tassert(false);\n\t\t\treturn Point();\n\t\t}\n\t}\n};\n\n// ????????????\nclass Circle {\npublic:\n\tPoint p;\n\tld r;\n\tCircle() : p(Point(0, 0)), r(0) {}\n\tCircle(Point p, ld r) : p(p), r(r) {}\n};\n\n// CCW\nint ccw(const Point& a, const Point &b, const Point &c) {\n\tconst Point nb(b - a);\n\tconst Point nc(c - a);\n\tif (cross(nb, nc) > eps) return 1; // a,b,c??????????¨???¨?????????????????¶\n\tif (cross(nb, nc) < -eps) return -1; // a,b,c???????¨???¨?????????????????¶\n\tif (dot(nb, nc) < 0) return 2; // c,a,b???????????´???????????¶\n\tif (norm(nb) < norm(nc)) return -2; // a,b,c???????????´???????????¶\n\treturn 0; // a,c,b???????????´???????????¶\n}\n\n\n/* ???????????? */\n\n// ??´?????¨??´??????????????????\nbool isis_ll(const Line& l, const Line& m) {\n\treturn !eq(cross(l.b - l.a, m.b - m.a), 0);\n}\n\n// ??´?????¨?????????????????????\nbool isis_ls(const Line& l, const Line& s) {\n\treturn isis_ll(l, s) &&\n\t\t(cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < eps);\n}\n\n// ????????¨?????????????????????\nbool isis_ss(const Line& s, const Line& t) {\n\treturn ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 &&\n\t\tccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0;\n}\n\n// ????????´????????????\nbool isis_lp(const Line& l, const Point& p) {\n\treturn (abs(cross(l.b - p, l.a - p)) < eps);\n}\n\n// ?????????????????????\nbool isis_sp(const Line& s, const Point& p) {\n\treturn (abs(s.a - p) + abs(s.b - p) - abs(s.b - s.a) < eps);\n}\n\n// ??????????¶?\nPoint proj(const Line &l, const Point& p) {\n\tld t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n\treturn l.a + t * (l.a - l.b);\n}\n\n// ??´?????¨??´????????????\nPoint is_ll(const Line &s, const Line& t) {\n\tPoint sv = s.b - s.a, tv = t.b - t.a;\n\tassert(cross(sv, tv) != 0);\n\treturn s.a + sv * cross(tv, t.a - s.a) / cross(tv, sv);\n}\n// ??´?????¨??´????????????\nvector<Point> is_ll2(const Line &s, const Line& t) {\n\tPoint sv = s.b - s.a, tv = t.b - t.a;\n\tif (cross(sv, tv) != 0)return vector<Point>(1, is_ll(s, t));\n\telse {\n\t\tvector<Point>ans;\n\t\tfor (int k = 0; k < 2; ++k) {\n\t\t\tif (isis_sp(s, t[k]) && find(ans.begin(), ans.end(), t[k]) == ans.end())ans.push_back(t[k]);\n\t\t\tif (isis_sp(t, s[k]) && find(ans.begin(), ans.end(), s[k]) == ans.end())ans.push_back(s[k]);\n\t\t}\n\t\treturn ans;\n\t}\n}\n// ????????¨???????????????\n//???????????£????????¨???????????¨assert(false)\nPoint is_ss(const Line &s, const Line& t) {\n\tif (isis_ss(s, t)) {\n\t\tfor (int k = 0; k < 2; ++k) {\n\t\t\tfor (int l = 0; l < 2; ++l) {\n\t\t\t\tif (s[k] == t[l])return s[k];\n\t\t\t}\n\t\t}\n\t\treturn is_ll(s, t);\n\t}\n\telse {\n\t\t//??????isis_ss?????????\n\t\tassert(false);\n\t\treturn Point(0, 0);\n\t}\n}\n// ??´?????¨???????????¢\nld dist_lp(const Line& l, const Point& p) {\n\treturn abs(p - proj(l, p));\n}\n\n// ??´?????¨??´???????????¢\nld dist_ll(const Line& l, const Line& m) {\n\treturn isis_ll(l, m) ? 0 : dist_lp(l, m.a);\n}\n\n// ??´?????¨??????????????¢\nld dist_ls(const Line& l, const Line& s) {\n\treturn isis_ls(l, s) ? 0 : min(dist_lp(l, s.a), dist_lp(l, s.b));\n}\n\n// ????????¨???????????¢\nld dist_sp(const Line& s, const Point& p) {\n\tPoint r = proj(s, p);\n\treturn isis_sp(s, r) ? abs(r - p) : min(abs(s.a - p), abs(s.b - p));\n}\n\n// ????????¨??????????????¢\nld dist_ss(const Line& s, const Line& t) {\n\tif (isis_ss(s, t)) return 0;\n\treturn min({ dist_sp(s, t.a), dist_sp(s, t.b), dist_sp(t, s.a), dist_sp(t, s.b) });\n}\n\n\n//??´?????¨??´?????????????????????????????????\nLine bisection(const Line &s, const Line &t) {\n\tconst Point laglanju(is_ll(s, t));\n\tconst Point avec = !(abs(laglanju - s[0])<eps) ? s[0] - laglanju : s[1] - laglanju;\n\tconst Point bvec = !(abs(laglanju - t[0])<eps) ? t[0] - laglanju : t[1] - laglanju;\n\n\treturn Line(laglanju, laglanju + (abs(bvec)*avec + abs(avec)*bvec) / (abs(avec) + abs(bvec)));\n}\n\n\n//???????????´?????????????????????\n//???????????´??????????????§???????????¨????¢?????????¨?????????\nPoint inner_center(const vector<Line>&ls) {\n\tvector<Point>vertics;\n\tfor (int i = 0; i <static_cast<int>(ls.size()); ++i) {\n\t\tvertics.push_back(is_ll(ls[i], ls[(i + 1) % 3]));\n\t}\n\tif (vertics[0] == vertics[1] || vertics[1] == vertics[2] || vertics[2] == vertics[0])return vertics[0];\n\tLine bi1(bisection(Line(vertics[0], vertics[1]), Line(vertics[0], vertics[2])));\n\tLine bi2(bisection(Line(vertics[1], vertics[2]), Line(vertics[1], vertics[0])));\n\tif (bi1[0] == bi2[0])return bi1[0];\n\telse {\n\t\treturn is_ll(bi1, bi2);\n\t}\n}\n\n//???????????´?????????????????????\n//???????????´??????????????§???????????¨????¢?????????¨?????????\nvector<Point> ex_center(const vector<Line>&ls) {\n\tvector<Point>vertics;\n\tfor (int i = 0; i < static_cast<int>(ls.size()); ++i) {\n\t\tvertics.push_back(is_ll(ls[i], ls[(i + 1) % 3]));\n\t}\n\tif (abs(vertics[0] - vertics[1])<eps || abs(vertics[1] - vertics[2])<eps || (abs(vertics[2] - vertics[0])<eps))return vector<Point>();\n\tvector<Point>ecs;\n\tfor (int i = 0; i < 3; ++i) {\n\t\tLine bi1(bisection(Line(vertics[i], vertics[i] * 2.0l - vertics[(i + 2) % 3]), Line(vertics[i], vertics[(i + 1) % 3])));\n\t\tLine bi2(bisection(Line(vertics[(i + 1) % 3], vertics[(i + 1) % 3] * 2.0l - vertics[(i + 2) % 3]), Line(vertics[(i + 1) % 3], vertics[i])));\n\t\tecs.push_back(is_ll(bi1, bi2));\n\t}\n\treturn ecs;\n}\n\n\n//a,b:??????\n//c:????????§??????\n//???????????´?????????????????¢?????????????±??????????\nvector<Point> same_dis(const vector<Line>&ls) {\n\tvector<Point>vertics;\n\tvertics.push_back(is_ll(ls[0], ls[2]));\n\tvertics.push_back(is_ll(ls[1], ls[2]));\n\n\tif (abs(vertics[0] - vertics[1]) < eps)return vector<Point>{vertics[0]};\n\tLine bis(bisection(ls[0], ls[1]));\n\tvector<Point>ecs;\n\n\tLine abi(bisection(Line(vertics[0], vertics[1]), ls[0]));\n\tecs.push_back(is_ll(bis, abi));\n\n\n\tLine bbi(bisection(Line(vertics[0], 2.l*vertics[0] - vertics[1]), ls[0]));\n\tecs.push_back(is_ll(bis, bbi));\n\n\treturn ecs;\n}\n/* ??? */\n\n// ?????¨????????????\nvector<Point> is_cc(const Circle& c1, const Circle& c2) {\n\tvector<Point> res;\n\tld d = abs(c1.p - c2.p);\n\tld rc = (d * d + c1.r * c1.r - c2.r * c2.r) / (2 * d);\n\tld dfr = c1.r * c1.r - rc * rc;\n\tif (abs(dfr) < eps) dfr = 0.0;\n\telse if (dfr < 0.0) return res; // no intersection\n\tld rs = sqrt(dfr);\n\tPoint diff = (c2.p - c1.p) / d;\n\tres.push_back(c1.p + diff * Point(rc, rs));\n\tif (dfr != 0.0) res.push_back(c1.p + diff * Point(rc, -rs));\n\treturn res;\n}\n\n//???????????????????????????\n// 0 => out\n// 1 => on\n// 2 => in\nint is_in_circle(const Circle &cir, const Point& p) {\n\tld dis = abs(cir.p - p);\n\tif (dis > cir.r + eps)return 0;\n\telse if (dis < cir.r - eps)return 2;\n\telse return 1;\n}\n//???lc??????rc??????????????????\n// 0 => out\n// 1 => on\n// 2 => in\nint circle_in_circle(const Circle &lc, const Circle&rc) {\n\tld dis = abs(lc.p - rc.p);\n\tif (dis < rc.r - lc.r - eps)return 2;\n\telse if (dis>rc.r - lc.r + eps)return 0;\n\telse return 1;\n}\n\n// ?????¨??´????????????\nvector<Point> is_lc(const Circle& c, const Line& l) {\n\tvector<Point> res;\n\tld d = dist_lp(l, c.p);\n\tif (d < c.r + eps) {\n\t\tld len = (d > c.r) ? 0.0 : sqrt(c.r * c.r - d * d); //safety;\n\t\tPoint nor = (l.a - l.b) / abs(l.a - l.b);\n\t\tres.push_back(proj(l, c.p) + len * nor);\n\t\tres.push_back(proj(l, c.p) - len * nor);\n\t}\n\treturn res;\n}\n\n// ?????¨??????????????¢\nvector<Point> is_sc(const Circle& c, const Line& l) {\n\tvector<Point> v = is_lc(c, l), res;\n\tfor (Point p : v)\n\t\tif (isis_sp(l, p)) res.push_back(p);\n\treturn res;\n}\n\n// ?????¨????????\\???\nvector<Line> tangent_cp(const Circle& c, const Point& p) {\n\tvector<Line> ret;\n\tPoint v = c.p - p;\n\tld d = abs(v);\n\tld l = sqrt(norm(v) - c.r * c.r);\n\tif (isnan(l)) { return ret; }\n\tPoint v1 = v * Point(l / d, c.r / d);\n\tPoint v2 = v * Point(l / d, -c.r / d);\n\tret.push_back(Line(p, p + v1));\n\tif (l < eps) return ret;\n\tret.push_back(Line(p, p + v2));\n\treturn ret;\n}\n\n// ?????¨????????\\???\nvector<Line> tangent_cc(const Circle& c1, const Circle& c2) {\n\tvector<Line> ret;\n\tif (abs(c1.p - c2.p) - (c1.r + c2.r) > -eps) {\n\t\tPoint center = (c1.p * c2.r + c2.p * c1.r) / (c1.r + c2.r);\n\t\tret = tangent_cp(c1, center);\n\t}\n\tif (abs(c1.r - c2.r) > eps) {\n\t\tPoint out = (-c1.p * c2.r + c2.p * c1.r) / (c1.r - c2.r);\n\t\tvector<Line> nret = tangent_cp(c1, out);\n\t\tret.insert(ret.end(), ALL(nret));\n\t}\n\telse {\n\t\tPoint v = c2.p - c1.p;\n\t\tv /= abs(v);\n\t\tPoint q1 = c1.p + v * Point(0, 1) * c1.r;\n\t\tPoint q2 = c1.p + v * Point(0, -1) * c1.r;\n\t\tret.push_back(Line(q1, q1 + v));\n\t\tret.push_back(Line(q2, q2 + v));\n\t}\n\treturn ret;\n}\n//??????????????????????????¢???\nld two_circle_area(const Circle&l, const Circle&r) {\n\tld dis = abs(l.p - r.p);\n\tif (dis > l.r + r.r)return 0;\n\telse if (dis + r.r < l.r) {\n\t\treturn r.r*r.r*pi;\n\t}\n\telse if (dis + l.r < r.r) {\n\t\treturn l.r*l.r*pi;\n\t}\n\telse {\n\t\tld ans = (l.r)*(l.r)*acos((dis*dis + l.r*l.r - r.r*r.r) / (2 * dis*l.r)) +\n\t\t\t(r.r)*(r.r)*acos((dis*dis + r.r*r.r - l.r*l.r) / (2 * dis*r.r)) -\n\t\t\tsqrt(4 * dis*dis*l.r*l.r - (dis*dis + l.r*l.r - r.r*r.r)*(dis*dis + l.r*l.r - r.r*r.r)) / 2;\n\t\treturn ans;\n\t}\n\n}\n\n/* ????§???¢ */\n\ntypedef vector<Point> Polygon;\n\n// ??¢???\nld area(const Polygon &p) {\n\tld res = 0;\n\tint n = p.size();\n\tREP(j, n) res += cross(p[j], p[(j + 1) % n]);\n\treturn res / 2;\n}\n\n// ????§???¢????????¢??????\nbool is_counter_clockwise(const Polygon &poly) {\n\tld angle = 0;\n\tint n = poly.size();\n\tREP(i, n) {\n\t\tPoint a = poly[i], b = poly[(i + 1) % n], c = poly[(i + 2) % n];\n\t\tangle += arg((c - b) / (b - a));\n\t}\n\treturn angle > eps;\n}\n\n// ??????????????????\n// 0 => out\n// 1 => on\n// 2 => in\nint is_in_polygon(const Polygon &poly, const Point& p) {\n\tld angle = 0;\n\tint n = poly.size();\n\tREP(i, n) {\n\t\tPoint a = poly[i], b = poly[(i + 1) % n];\n\t\tif (isis_sp(Line(a, b), p)) return 1;\n\t\tangle += arg((b - p) / (a - p));\n\t}\n\treturn eq(angle, 0) ? 0 : 2;\n}\n//??????????????????2?????????\nenum { OUT, ON, IN };\nint convex_contains(const Polygon &P, const Point &p) {\n\tconst int n = P.size();\n\tPoint g = (P[0] + P[n / 3] + P[2 * n / 3]) / 3.0l; // inner-point\n\tint a = 0, b = n;\n\twhile (a + 1 < b) { // invariant: c is in fan g-P[a]-P[b]\n\t\tint c = (a + b) / 2;\n\t\tif (cross(P[a] - g, P[c] - g) > 0) { // angle < 180 deg\n\t\t\tif (cross(P[a] - g, p - g) > 0 && cross(P[c] - g, p - g) < 0) b = c;\n\t\t\telse a = c;\n\t\t}\n\t\telse {\n\t\t\tif (cross(P[a] - g, p - g) < 0 && cross(P[c] - g, p - g) > 0) a = c;\n\t\t\telse b = c;\n\t\t}\n\t}\n\tb %= n;\n\tif (cross(P[a] - p, P[b] - p) < 0) return 0;\n\tif (cross(P[a] - p, P[b] - p) > 0) return 2;\n\treturn 1;\n}\n\n// ??????\n// ???????????????????????¨????????????????????§??¨???\nPolygon convex_hull(vector<Point> ps) {\n\tint n = ps.size();\n\tint k = 0;\n\tsort(ps.begin(), ps.end());\n\tPolygon ch(2 * n);\n\tfor (int i = 0; i < n; ch[k++] = ps[i++])\n\t\twhile (k >= 2 && ccw(ch[k - 2], ch[k - 1], ps[i]) <= 0) --k;\n\tfor (int i = n - 2, t = k + 1; i >= 0; ch[k++] = ps[i--])\n\t\twhile (k >= t && ccw(ch[k - 2], ch[k - 1], ps[i]) <= 0) --k;\n\tch.resize(k - 1);\n\treturn ch;\n}\n\n\n\n// ????????????\nvector<Polygon> convex_cut(const Polygon &ps, const Line& l) {\n\tint n = ps.size();\n\tPolygon Q;\n\tPolygon R;\n\tREP(i, n) {\n\t\tPoint A = ps[i], B = ps[(i + 1) % n];\n\t\tLine m = Line(A, B);\n\t\tif (ccw(l.a, l.b, A) != -1) Q.push_back(A);\n\t\tif (ccw(l.a, l.b, A) != 1) R.push_back(A);\n\t\tif (ccw(l.a, l.b, A) * ccw(l.a, l.b, B) < 0 && isis_ll(l, m)) {\n\t\t\tQ.push_back(is_ll(l, m));\n\t\t\tR.push_back(is_ll(l, m));\n\t\t}\n\t}\n\tconst vector<Polygon>polys{ Q,R };\n\treturn polys;\n}\n\n\n/* ??¢??¬??????????????? */\nvoid add_point(vector<Point> &ps, const Point p) {\n\tfor (Point q : ps) if (abs(q - p) < eps) return;\n\tps.push_back(p);\n}\n\ntypedef int Weight;\nstruct Edge {\n\tint src, dst;\n\tWeight weight;\n\tEdge(int src, int dst, Weight weight) :\n\t\tsrc(src), dst(dst), weight(weight) { }\n};\n\ntypedef vector<Edge> Edges;\ntypedef vector<Edges> Graph;\n\nvoid add_edge(Graph &g, const int from, const int to, const Weight weight) {\n\tg[from].push_back(Edge{ from, to, weight });\n}\n\nGraph segment_arrangement(const vector<Line> &s, const vector<Point> &p) {\n\tint n = p.size(), m = s.size();\n\tGraph g(n);\n\tREP(i, m) {\n\t\tvector<pair<ld, int>> vec;\n\t\tREP(j, n) if (isis_sp(s[i], p[j]))\n\t\t\tvec.emplace_back(abs(s[i].a - p[j]), j);\n\t\tsort(ALL(vec));\n\t\tREP(j, vec.size() - 1) {\n\t\t\tint from = vec[j].second, to = vec[j + 1].second;\n\t\t\tadd_edge(g, from, to, static_cast<Weight>(abs(p[from] - p[to])));\n\t\t}\n\t}\n\treturn g;\n}\nGraph sennbunn_arrangement(const vector<Line>&s) {\n\tvector<Point>crss;\n\tfor (int i = 0; i < static_cast<int>(s.size()); ++i) {\n\t\tfor (int j = i + 1; j < static_cast<int>(s.size()); ++j) {\n\t\t\tif (isis_ss(s[i], s[j])) {\n\t\t\t\tcrss.push_back(is_ll(s[i], s[j]));\n\t\t\t}\n\t\t}\n\t}\n\tfor (int i = 0; i <static_cast<int>(s.size()); ++i) {\n\t\tcrss.push_back(s[i][0]);\n\t\tcrss.push_back(s[i][1]);\n\t}\n\treturn segment_arrangement(s, crss);\n}\n\n//Graph circle_arrangement(const vector<Circle> &c, const vector<Point> &p) {\n//\tint n = p.size(), m = c.size();\n//\tGraph g(n);\n//\tREP(i, m) {\n//\t\tvector<pair<ld, int>> vec;\n//\t\tREP(j, n) if (abs(abs(c[i].p - p[j]) - c[i].r) < eps)\n//\t\t\tvec.emplace_back(arg(c[i].p - p[j]), j);\n//\t\tsort(ALL(vec));\n//\t\tREP(j, vec.size() - 1) {\n//\t\t\tint from = vec[j].second, to = vec[j + 1].second;\n//\t\t\tld angle = vec[j + 1].first - vec[j].first;\n//\t\t\tadd_edge(g, from, to, static_cast<Weight>(angle * c[i].r));\n//\t\t}\n//\t\tif (vec.size() >= 2) {\n//\t\t\tint from = vec.back().second, to = vec.front().first;\n//\t\t\tld angle = vec.front().first - vec.back().first;\n//\t\t\tadd_edge(g, from, to, static_cast<Weight>(angle * c[i].r));\n//\t\t}\n//\t}\n//\treturn g;\n//}\n\n\n\nconst ld G= 9.8;\n\nstruct box {\n\tvector<ld>xs;\n\tvector<ld>ys;\n};\n\n\nint N, V, X, Y;\nbool check(const ld theta, const box& b) {\n\n\tconst ld vx = V*cos(theta);\n\tconst ld vy = V*sin(theta);\n\n\tld y_max = -1e18;\n\tld y_min = 1e18;\n\n\t//?????????????¢????\n\t{\n\t\tconst ld toptime = vy / G;\n\t\tconst ld top_x = toptime*vx;\n\t\tconst ld top_y = vy*toptime - toptime*toptime*G / 2;\n\t\tif (b.xs[0] < top_x&&top_x < b.xs[1]) {\n\t\t\ty_max = max(y_max, top_y);\n\t\t}\n\t}\n\t//???????????????????¢????\n\t{\n\t\tfor (int x = 0; x < 2; ++x) {\n\t\t\tconst ld time = b.xs[x]/vx;\n\t\t\tconst ld ay = vy*time - time*time*G / 2;\n\t\t\ty_max = max(y_max, ay);\n\t\t\ty_min = min(y_min, ay);\n\t\t}\n\t}\n\tif (y_max- eps < b.ys[0] || b.ys[1] < y_min + eps) {\n\t\treturn true;\n\t}\n\telse {\n\t\treturn false;\n\t}\n}\n\nvoid gettan(vector<ld>&tans,const Point& p,int leftup) {\n\tif (p.real() < eps)return;\n\tbool ok = true;\n\tld amin, amax;\n\n\t//?????????????¨????\n\t{\n\t\tconst ld tan = V*V / G / p.real();\n\t\tconst ld theta = atan(tan);\n\t\tconst ld vx = V*cos(theta);\n\t\tconst ld vy = V*sin(theta);\n\t\tconst ld time = p.real() / vx;\n\t\tconst ld max_y = vy*time - G*time*time / 2;\n\t\tif (max_y < p.imag()) {\n\t\t\tok = false;\n\t\t}\n\t\tif (leftup) {\n\t\t\tamax = tan;\n\t\t\tamin = 0;\n\t\t}\n\t\telse {\n\t\t\tamax = 1e18;\n\t\t\tamin = tan;\n\t\t}\n\t}\n\tif (ok) {\n\t\tint rep = 1000;\n\t\twhile (rep--) {\n\t\t\tconst ld amidtan = (amin + amax) / 2;\n\t\t\tconst ld theta = atan(amidtan);\n\t\t\tconst ld vx = V*cos(theta);\n\t\t\tconst ld vy = V*sin(theta);\n\t\t\tconst ld time = p.real() / vx;\n\t\t\tconst ld ay = vy*time - G*time*time / 2;\n\t\t\tif (ay > p.imag()) {\n\t\t\t\tif (leftup) {\n\t\t\t\t\tamax = amidtan;\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tamin = amidtan;\n\t\t\t\t}\n\t\t\t}\n\t\t\telse {\n\t\t\t\tif (leftup) {\n\t\t\t\t\tamin = amidtan;\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tamax = amidtan;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tconst ld amidtan = (amin + amax) / 2;\n\t\tconst ld theta = atan(amidtan);\n\t\tconst ld vx = V*cos(theta);\n\t\tconst ld vy = V*sin(theta);\n\t\tconst ld time = p.real() / vx;\n\t\tconst ld ay = vy*time - G*time*time / 2;\n\t\ttans.push_back(amin);\n\t}\n}\n\nint main() { cin >> N >> V >> X >> Y;\n\tvector<box>bs;\n\tfor (int i = 0; i < N; ++i) {\n\t\tint L, B, R, T; cin >> L >> B >> R >> T;\n\t\tbox b;\n\t\tb.xs.push_back(L);\n\t\tb.xs.push_back(R);\n\t\tb.ys.push_back(B);\n\t\tb.ys.push_back(T);\n\t\tbs.push_back(b);\n\t}\n\tconst ld down = Y;\n\tld up = 1e18;\n\tfor (int i = 0; i < N; ++i) {\n\t\tif (bs[i].xs[0] < X&&X < bs[i].xs[1]) {\n\t\t\tup = min(up, bs[i].ys[0]);\n\t\t}\n\t}\n\tvector<ld>tans;\n\tfor (auto b : bs) {\n\t\tfor (int x = 0; x < 2; ++x) {\n\t\t\tfor (int y = 0; y < 2; ++y) {\n\t\t\t\tPoint p(b.xs[x], b.ys[y]);\n\t\t\t\tgettan(tans, p,(x+y)%2);\n\t\t\t}\n\t\t}\n\t}\n\t{\n\t\tPoint p(X,Y);\n\t\tgettan(tans, p,true);\n\t}\n\tsort(tans.begin(), tans.end());\n\t{\n\t\tvector<ld>ntans;\n\t\tfor (int i = 0; i < tans.size(); ++i) {\n\t\t\tntans.push_back(tans[i]);\n\t\t\tif (i != tans.size() - 1) {\n\t\t\t\tntans.push_back((tans[i] + tans[i + 1]) / 2);\n\t\t\t}\n\t\t}\n\t\ttans = ntans;\n\t}\n\n\tstring ans = \"No\";\n\tfor (auto t : tans) {\n\t\tconst ld theta = atan(t);\n\t\tconst ld vx = V*cos(theta);\n\t\tconst ld vy = V*sin(theta);\n\t\tconst ld time = X / vx;\n\t\tconst ld ay = vy*time - G*time*time / 2;\n\t\tif (down-eps <= ay&&ay <= up+eps) {\n\t\t\tbool ok = true;\n\t\t\tfor (auto b : bs) {\n\t\t\t\tif (!check(theta, b))ok = false;\n\t\t\t}\n\t\t\tif (ok) {\n\t\t\t\tans = \"Yes\";\n\t\t\t}\n\t\t}\n\t\telse {\n\t\t\tcontinue;\n\t\t}\n\t}\n\tcout << ans << endl;\n\treturn 0;\n}", "accuracy": 0.16666666666666666, "time_ms": 30, "memory_kb": 3476, "score_of_the_acc": -0.8579, "final_rank": 17 }, { "submission_id": "aoj_2308_1777161", "code_snippet": "#include \"bits/stdc++.h\"\n#include<unordered_map>\n#include<unordered_set>\n#pragma warning(disable:4996)\nusing namespace std;\nusing ld = long double;\ntemplate<class T>\nusing Table = vector<vector<T>>;\nconst ld eps=1e-9;\n\n//// < \"D:\\D_Download\\Visual Studio 2015\\Projects\\programing_contest_c++\\Debug\\a.txt\"\n\n\n/* ??????????????¬ */\n\n#include <complex>\n\ntypedef long double ld;\ntypedef complex<ld> Point;\n#define REP(i,n) for(int i=0;i<(int)(n);i++)\n#define ALL(x) (x).begin(),(x).end()\n\n\nconst ld pi = acos(-1.0);\nconst ld dtop = pi / 180.;\nconst ld ptod = 1. / dtop;\n\nnamespace std {\n\tbool operator<(const Point &lhs, const Point &rhs) {\n\t\tif (lhs.real() < rhs.real() - eps) return true;\n\t\tif (lhs.real() > rhs.real() + eps) return false;\n\t\treturn lhs.imag() < rhs.imag();\n\t}\n}\n\n// ????????\\???\nPoint input_point() {\n\tld x, y;\n\tcin >> x >> y;\n\treturn Point(x, y);\n}\n\n// ????????????????????????\nbool eq(const ld a, const ld b) {\n\treturn (abs(a - b) < eps);\n}\n\n// ??????\nld dot(const Point& a, const Point& b) {\n\treturn real(conj(a) * b);\n}\n\n// ??????\nld cross(const Point& a, const Point& b) {\n\treturn imag(conj(a) * b);\n}\n\n// ??´????????????\nclass Line {\npublic:\n\tPoint a, b;\n\tLine() : a(Point(0, 0)), b(Point(0, 0)) {}\n\tLine(Point a, Point b) : a(a), b(b) {}\n\tPoint operator[](const int _num)const {\n\t\tif (_num == 0)return a;\n\t\telse if (_num == 1)return b;\n\t\telse {\n\t\t\tassert(false);\n\t\t\treturn Point();\n\t\t}\n\t}\n};\n\n// ????????????\nclass Circle {\npublic:\n\tPoint p;\n\tld r;\n\tCircle() : p(Point(0, 0)), r(0) {}\n\tCircle(Point p, ld r) : p(p), r(r) {}\n};\n\n// CCW\nint ccw(const Point& a, const Point &b, const Point &c) {\n\tconst Point nb(b - a);\n\tconst Point nc(c - a);\n\tif (cross(nb, nc) > eps) return 1; // a,b,c??????????¨???¨?????????????????¶\n\tif (cross(nb, nc) < -eps) return -1; // a,b,c???????¨???¨?????????????????¶\n\tif (dot(nb, nc) < 0) return 2; // c,a,b???????????´???????????¶\n\tif (norm(nb) < norm(nc)) return -2; // a,b,c???????????´???????????¶\n\treturn 0; // a,c,b???????????´???????????¶\n}\n\n\n/* ???????????? */\n\n// ??´?????¨??´??????????????????\nbool isis_ll(const Line& l, const Line& m) {\n\treturn !eq(cross(l.b - l.a, m.b - m.a), 0);\n}\n\n// ??´?????¨?????????????????????\nbool isis_ls(const Line& l, const Line& s) {\n\treturn isis_ll(l, s) &&\n\t\t(cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < eps);\n}\n\n// ????????¨?????????????????????\nbool isis_ss(const Line& s, const Line& t) {\n\treturn ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 &&\n\t\tccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0;\n}\n\n// ????????´????????????\nbool isis_lp(const Line& l, const Point& p) {\n\treturn (abs(cross(l.b - p, l.a - p)) < eps);\n}\n\n// ?????????????????????\nbool isis_sp(const Line& s, const Point& p) {\n\treturn (abs(s.a - p) + abs(s.b - p) - abs(s.b - s.a) < eps);\n}\n\n// ??????????¶?\nPoint proj(const Line &l, const Point& p) {\n\tld t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n\treturn l.a + t * (l.a - l.b);\n}\n\n// ??´?????¨??´????????????\nPoint is_ll(const Line &s, const Line& t) {\n\tPoint sv = s.b - s.a, tv = t.b - t.a;\n\tassert(cross(sv, tv) != 0);\n\treturn s.a + sv * cross(tv, t.a - s.a) / cross(tv, sv);\n}\n// ??´?????¨??´????????????\nvector<Point> is_ll2(const Line &s, const Line& t) {\n\tPoint sv = s.b - s.a, tv = t.b - t.a;\n\tif (cross(sv, tv) != 0)return vector<Point>(1, is_ll(s, t));\n\telse {\n\t\tvector<Point>ans;\n\t\tfor (int k = 0; k < 2; ++k) {\n\t\t\tif (isis_sp(s, t[k]) && find(ans.begin(), ans.end(), t[k]) == ans.end())ans.push_back(t[k]);\n\t\t\tif (isis_sp(t, s[k]) && find(ans.begin(), ans.end(), s[k]) == ans.end())ans.push_back(s[k]);\n\t\t}\n\t\treturn ans;\n\t}\n}\n// ????????¨???????????????\n//???????????£????????¨???????????¨assert(false)\nPoint is_ss(const Line &s, const Line& t) {\n\tif (isis_ss(s, t)) {\n\t\tfor (int k = 0; k < 2; ++k) {\n\t\t\tfor (int l = 0; l < 2; ++l) {\n\t\t\t\tif (s[k] == t[l])return s[k];\n\t\t\t}\n\t\t}\n\t\treturn is_ll(s, t);\n\t}\n\telse {\n\t\t//??????isis_ss?????????\n\t\tassert(false);\n\t\treturn Point(0, 0);\n\t}\n}\n// ??´?????¨???????????¢\nld dist_lp(const Line& l, const Point& p) {\n\treturn abs(p - proj(l, p));\n}\n\n// ??´?????¨??´???????????¢\nld dist_ll(const Line& l, const Line& m) {\n\treturn isis_ll(l, m) ? 0 : dist_lp(l, m.a);\n}\n\n// ??´?????¨??????????????¢\nld dist_ls(const Line& l, const Line& s) {\n\treturn isis_ls(l, s) ? 0 : min(dist_lp(l, s.a), dist_lp(l, s.b));\n}\n\n// ????????¨???????????¢\nld dist_sp(const Line& s, const Point& p) {\n\tPoint r = proj(s, p);\n\treturn isis_sp(s, r) ? abs(r - p) : min(abs(s.a - p), abs(s.b - p));\n}\n\n// ????????¨??????????????¢\nld dist_ss(const Line& s, const Line& t) {\n\tif (isis_ss(s, t)) return 0;\n\treturn min({ dist_sp(s, t.a), dist_sp(s, t.b), dist_sp(t, s.a), dist_sp(t, s.b) });\n}\n\n\n//??´?????¨??´?????????????????????????????????\nLine bisection(const Line &s, const Line &t) {\n\tconst Point laglanju(is_ll(s, t));\n\tconst Point avec = !(abs(laglanju - s[0])<eps) ? s[0] - laglanju : s[1] - laglanju;\n\tconst Point bvec = !(abs(laglanju - t[0])<eps) ? t[0] - laglanju : t[1] - laglanju;\n\n\treturn Line(laglanju, laglanju + (abs(bvec)*avec + abs(avec)*bvec) / (abs(avec) + abs(bvec)));\n}\n\n\n//???????????´?????????????????????\n//???????????´??????????????§???????????¨????¢?????????¨?????????\nPoint inner_center(const vector<Line>&ls) {\n\tvector<Point>vertics;\n\tfor (int i = 0; i <static_cast<int>(ls.size()); ++i) {\n\t\tvertics.push_back(is_ll(ls[i], ls[(i + 1) % 3]));\n\t}\n\tif (vertics[0] == vertics[1] || vertics[1] == vertics[2] || vertics[2] == vertics[0])return vertics[0];\n\tLine bi1(bisection(Line(vertics[0], vertics[1]), Line(vertics[0], vertics[2])));\n\tLine bi2(bisection(Line(vertics[1], vertics[2]), Line(vertics[1], vertics[0])));\n\tif (bi1[0] == bi2[0])return bi1[0];\n\telse {\n\t\treturn is_ll(bi1, bi2);\n\t}\n}\n\n//???????????´?????????????????????\n//???????????´??????????????§???????????¨????¢?????????¨?????????\nvector<Point> ex_center(const vector<Line>&ls) {\n\tvector<Point>vertics;\n\tfor (int i = 0; i < static_cast<int>(ls.size()); ++i) {\n\t\tvertics.push_back(is_ll(ls[i], ls[(i + 1) % 3]));\n\t}\n\tif (abs(vertics[0] - vertics[1])<eps || abs(vertics[1] - vertics[2])<eps || (abs(vertics[2] - vertics[0])<eps))return vector<Point>();\n\tvector<Point>ecs;\n\tfor (int i = 0; i < 3; ++i) {\n\t\tLine bi1(bisection(Line(vertics[i], vertics[i] * 2.0l - vertics[(i + 2) % 3]), Line(vertics[i], vertics[(i + 1) % 3])));\n\t\tLine bi2(bisection(Line(vertics[(i + 1) % 3], vertics[(i + 1) % 3] * 2.0l - vertics[(i + 2) % 3]), Line(vertics[(i + 1) % 3], vertics[i])));\n\t\tecs.push_back(is_ll(bi1, bi2));\n\t}\n\treturn ecs;\n}\n\n\n//a,b:??????\n//c:????????§??????\n//???????????´?????????????????¢?????????????±??????????\nvector<Point> same_dis(const vector<Line>&ls) {\n\tvector<Point>vertics;\n\tvertics.push_back(is_ll(ls[0], ls[2]));\n\tvertics.push_back(is_ll(ls[1], ls[2]));\n\n\tif (abs(vertics[0] - vertics[1]) < eps)return vector<Point>{vertics[0]};\n\tLine bis(bisection(ls[0], ls[1]));\n\tvector<Point>ecs;\n\n\tLine abi(bisection(Line(vertics[0], vertics[1]), ls[0]));\n\tecs.push_back(is_ll(bis, abi));\n\n\n\tLine bbi(bisection(Line(vertics[0], 2.l*vertics[0] - vertics[1]), ls[0]));\n\tecs.push_back(is_ll(bis, bbi));\n\n\treturn ecs;\n}\n/* ??? */\n\n// ?????¨????????????\nvector<Point> is_cc(const Circle& c1, const Circle& c2) {\n\tvector<Point> res;\n\tld d = abs(c1.p - c2.p);\n\tld rc = (d * d + c1.r * c1.r - c2.r * c2.r) / (2 * d);\n\tld dfr = c1.r * c1.r - rc * rc;\n\tif (abs(dfr) < eps) dfr = 0.0;\n\telse if (dfr < 0.0) return res; // no intersection\n\tld rs = sqrt(dfr);\n\tPoint diff = (c2.p - c1.p) / d;\n\tres.push_back(c1.p + diff * Point(rc, rs));\n\tif (dfr != 0.0) res.push_back(c1.p + diff * Point(rc, -rs));\n\treturn res;\n}\n\n//???????????????????????????\n// 0 => out\n// 1 => on\n// 2 => in\nint is_in_circle(const Circle &cir, const Point& p) {\n\tld dis = abs(cir.p - p);\n\tif (dis > cir.r + eps)return 0;\n\telse if (dis < cir.r - eps)return 2;\n\telse return 1;\n}\n//???lc??????rc??????????????????\n// 0 => out\n// 1 => on\n// 2 => in\nint circle_in_circle(const Circle &lc, const Circle&rc) {\n\tld dis = abs(lc.p - rc.p);\n\tif (dis < rc.r - lc.r - eps)return 2;\n\telse if (dis>rc.r - lc.r + eps)return 0;\n\telse return 1;\n}\n\n// ?????¨??´????????????\nvector<Point> is_lc(const Circle& c, const Line& l) {\n\tvector<Point> res;\n\tld d = dist_lp(l, c.p);\n\tif (d < c.r + eps) {\n\t\tld len = (d > c.r) ? 0.0 : sqrt(c.r * c.r - d * d); //safety;\n\t\tPoint nor = (l.a - l.b) / abs(l.a - l.b);\n\t\tres.push_back(proj(l, c.p) + len * nor);\n\t\tres.push_back(proj(l, c.p) - len * nor);\n\t}\n\treturn res;\n}\n\n// ?????¨??????????????¢\nvector<Point> is_sc(const Circle& c, const Line& l) {\n\tvector<Point> v = is_lc(c, l), res;\n\tfor (Point p : v)\n\t\tif (isis_sp(l, p)) res.push_back(p);\n\treturn res;\n}\n\n// ?????¨????????\\???\nvector<Line> tangent_cp(const Circle& c, const Point& p) {\n\tvector<Line> ret;\n\tPoint v = c.p - p;\n\tld d = abs(v);\n\tld l = sqrt(norm(v) - c.r * c.r);\n\tif (isnan(l)) { return ret; }\n\tPoint v1 = v * Point(l / d, c.r / d);\n\tPoint v2 = v * Point(l / d, -c.r / d);\n\tret.push_back(Line(p, p + v1));\n\tif (l < eps) return ret;\n\tret.push_back(Line(p, p + v2));\n\treturn ret;\n}\n\n// ?????¨????????\\???\nvector<Line> tangent_cc(const Circle& c1, const Circle& c2) {\n\tvector<Line> ret;\n\tif (abs(c1.p - c2.p) - (c1.r + c2.r) > -eps) {\n\t\tPoint center = (c1.p * c2.r + c2.p * c1.r) / (c1.r + c2.r);\n\t\tret = tangent_cp(c1, center);\n\t}\n\tif (abs(c1.r - c2.r) > eps) {\n\t\tPoint out = (-c1.p * c2.r + c2.p * c1.r) / (c1.r - c2.r);\n\t\tvector<Line> nret = tangent_cp(c1, out);\n\t\tret.insert(ret.end(), ALL(nret));\n\t}\n\telse {\n\t\tPoint v = c2.p - c1.p;\n\t\tv /= abs(v);\n\t\tPoint q1 = c1.p + v * Point(0, 1) * c1.r;\n\t\tPoint q2 = c1.p + v * Point(0, -1) * c1.r;\n\t\tret.push_back(Line(q1, q1 + v));\n\t\tret.push_back(Line(q2, q2 + v));\n\t}\n\treturn ret;\n}\n//??????????????????????????¢???\nld two_circle_area(const Circle&l, const Circle&r) {\n\tld dis = abs(l.p - r.p);\n\tif (dis > l.r + r.r)return 0;\n\telse if (dis + r.r < l.r) {\n\t\treturn r.r*r.r*pi;\n\t}\n\telse if (dis + l.r < r.r) {\n\t\treturn l.r*l.r*pi;\n\t}\n\telse {\n\t\tld ans = (l.r)*(l.r)*acos((dis*dis + l.r*l.r - r.r*r.r) / (2 * dis*l.r)) +\n\t\t\t(r.r)*(r.r)*acos((dis*dis + r.r*r.r - l.r*l.r) / (2 * dis*r.r)) -\n\t\t\tsqrt(4 * dis*dis*l.r*l.r - (dis*dis + l.r*l.r - r.r*r.r)*(dis*dis + l.r*l.r - r.r*r.r)) / 2;\n\t\treturn ans;\n\t}\n\n}\n\n/* ????§???¢ */\n\ntypedef vector<Point> Polygon;\n\n// ??¢???\nld area(const Polygon &p) {\n\tld res = 0;\n\tint n = p.size();\n\tREP(j, n) res += cross(p[j], p[(j + 1) % n]);\n\treturn res / 2;\n}\n\n// ????§???¢????????¢??????\nbool is_counter_clockwise(const Polygon &poly) {\n\tld angle = 0;\n\tint n = poly.size();\n\tREP(i, n) {\n\t\tPoint a = poly[i], b = poly[(i + 1) % n], c = poly[(i + 2) % n];\n\t\tangle += arg((c - b) / (b - a));\n\t}\n\treturn angle > eps;\n}\n\n// ??????????????????\n// 0 => out\n// 1 => on\n// 2 => in\nint is_in_polygon(const Polygon &poly, const Point& p) {\n\tld angle = 0;\n\tint n = poly.size();\n\tREP(i, n) {\n\t\tPoint a = poly[i], b = poly[(i + 1) % n];\n\t\tif (isis_sp(Line(a, b), p)) return 1;\n\t\tangle += arg((b - p) / (a - p));\n\t}\n\treturn eq(angle, 0) ? 0 : 2;\n}\n//??????????????????2?????????\nenum { OUT, ON, IN };\nint convex_contains(const Polygon &P, const Point &p) {\n\tconst int n = P.size();\n\tPoint g = (P[0] + P[n / 3] + P[2 * n / 3]) / 3.0l; // inner-point\n\tint a = 0, b = n;\n\twhile (a + 1 < b) { // invariant: c is in fan g-P[a]-P[b]\n\t\tint c = (a + b) / 2;\n\t\tif (cross(P[a] - g, P[c] - g) > 0) { // angle < 180 deg\n\t\t\tif (cross(P[a] - g, p - g) > 0 && cross(P[c] - g, p - g) < 0) b = c;\n\t\t\telse a = c;\n\t\t}\n\t\telse {\n\t\t\tif (cross(P[a] - g, p - g) < 0 && cross(P[c] - g, p - g) > 0) a = c;\n\t\t\telse b = c;\n\t\t}\n\t}\n\tb %= n;\n\tif (cross(P[a] - p, P[b] - p) < 0) return 0;\n\tif (cross(P[a] - p, P[b] - p) > 0) return 2;\n\treturn 1;\n}\n\n// ??????\n// ???????????????????????¨????????????????????§??¨???\nPolygon convex_hull(vector<Point> ps) {\n\tint n = ps.size();\n\tint k = 0;\n\tsort(ps.begin(), ps.end());\n\tPolygon ch(2 * n);\n\tfor (int i = 0; i < n; ch[k++] = ps[i++])\n\t\twhile (k >= 2 && ccw(ch[k - 2], ch[k - 1], ps[i]) <= 0) --k;\n\tfor (int i = n - 2, t = k + 1; i >= 0; ch[k++] = ps[i--])\n\t\twhile (k >= t && ccw(ch[k - 2], ch[k - 1], ps[i]) <= 0) --k;\n\tch.resize(k - 1);\n\treturn ch;\n}\n\n\n\n// ????????????\nvector<Polygon> convex_cut(const Polygon &ps, const Line& l) {\n\tint n = ps.size();\n\tPolygon Q;\n\tPolygon R;\n\tREP(i, n) {\n\t\tPoint A = ps[i], B = ps[(i + 1) % n];\n\t\tLine m = Line(A, B);\n\t\tif (ccw(l.a, l.b, A) != -1) Q.push_back(A);\n\t\tif (ccw(l.a, l.b, A) != 1) R.push_back(A);\n\t\tif (ccw(l.a, l.b, A) * ccw(l.a, l.b, B) < 0 && isis_ll(l, m)) {\n\t\t\tQ.push_back(is_ll(l, m));\n\t\t\tR.push_back(is_ll(l, m));\n\t\t}\n\t}\n\tconst vector<Polygon>polys{ Q,R };\n\treturn polys;\n}\n\n\n/* ??¢??¬??????????????? */\nvoid add_point(vector<Point> &ps, const Point p) {\n\tfor (Point q : ps) if (abs(q - p) < eps) return;\n\tps.push_back(p);\n}\n\ntypedef int Weight;\nstruct Edge {\n\tint src, dst;\n\tWeight weight;\n\tEdge(int src, int dst, Weight weight) :\n\t\tsrc(src), dst(dst), weight(weight) { }\n};\n\ntypedef vector<Edge> Edges;\ntypedef vector<Edges> Graph;\n\nvoid add_edge(Graph &g, const int from, const int to, const Weight weight) {\n\tg[from].push_back(Edge{ from, to, weight });\n}\n\nGraph segment_arrangement(const vector<Line> &s, const vector<Point> &p) {\n\tint n = p.size(), m = s.size();\n\tGraph g(n);\n\tREP(i, m) {\n\t\tvector<pair<ld, int>> vec;\n\t\tREP(j, n) if (isis_sp(s[i], p[j]))\n\t\t\tvec.emplace_back(abs(s[i].a - p[j]), j);\n\t\tsort(ALL(vec));\n\t\tREP(j, vec.size() - 1) {\n\t\t\tint from = vec[j].second, to = vec[j + 1].second;\n\t\t\tadd_edge(g, from, to, static_cast<Weight>(abs(p[from] - p[to])));\n\t\t}\n\t}\n\treturn g;\n}\nGraph sennbunn_arrangement(const vector<Line>&s) {\n\tvector<Point>crss;\n\tfor (int i = 0; i < static_cast<int>(s.size()); ++i) {\n\t\tfor (int j = i + 1; j < static_cast<int>(s.size()); ++j) {\n\t\t\tif (isis_ss(s[i], s[j])) {\n\t\t\t\tcrss.push_back(is_ll(s[i], s[j]));\n\t\t\t}\n\t\t}\n\t}\n\tfor (int i = 0; i <static_cast<int>(s.size()); ++i) {\n\t\tcrss.push_back(s[i][0]);\n\t\tcrss.push_back(s[i][1]);\n\t}\n\treturn segment_arrangement(s, crss);\n}\n\n//Graph circle_arrangement(const vector<Circle> &c, const vector<Point> &p) {\n//\tint n = p.size(), m = c.size();\n//\tGraph g(n);\n//\tREP(i, m) {\n//\t\tvector<pair<ld, int>> vec;\n//\t\tREP(j, n) if (abs(abs(c[i].p - p[j]) - c[i].r) < eps)\n//\t\t\tvec.emplace_back(arg(c[i].p - p[j]), j);\n//\t\tsort(ALL(vec));\n//\t\tREP(j, vec.size() - 1) {\n//\t\t\tint from = vec[j].second, to = vec[j + 1].second;\n//\t\t\tld angle = vec[j + 1].first - vec[j].first;\n//\t\t\tadd_edge(g, from, to, static_cast<Weight>(angle * c[i].r));\n//\t\t}\n//\t\tif (vec.size() >= 2) {\n//\t\t\tint from = vec.back().second, to = vec.front().first;\n//\t\t\tld angle = vec.front().first - vec.back().first;\n//\t\t\tadd_edge(g, from, to, static_cast<Weight>(angle * c[i].r));\n//\t\t}\n//\t}\n//\treturn g;\n//}\n\n\n\nconst ld G= 9.8;\n\nstruct box {\n\tvector<ld>xs;\n\tvector<ld>ys;\n};\n\n\nint N, V, X, Y;\nbool check(const ld theta, const box& b) {\n\n\tconst ld vx = V*cos(theta);\n\tconst ld vy = V*sin(theta);\n\n\tld y_max = -1e18;\n\tld y_min = 1e18;\n\n\t//?????????????¢????\n\t{\n\t\tconst ld toptime = vy / G;\n\t\tconst ld top_x = toptime*vx;\n\t\tconst ld top_y = vy*toptime - toptime*toptime*G / 2;\n\t\tif (b.xs[0] < top_x&&top_x < b.xs[1]) {\n\t\t\ty_max = max(y_max, top_y);\n\t\t}\n\t}\n\t//???????????????????¢????\n\t{\n\t\tfor (int x = 0; x < 2; ++x) {\n\t\t\tconst ld time = b.xs[x]/vx;\n\t\t\tconst ld ay = vy*time - time*time*G / 2;\n\t\t\ty_max = max(y_max, ay);\n\t\t\ty_min = min(y_min, ay);\n\t\t}\n\t}\n\tif (y_max- eps < b.ys[0] || b.ys[1] < y_min + eps) {\n\t\treturn true;\n\t}\n\telse {\n\t\treturn false;\n\t}\n}\n\nvoid gettan(vector<ld>&tans,const Point& p,int leftup) {\n\tif (p.real() < eps)return;\n\tbool ok = true;\n\tld amin, amax;\n\n\t//?????????????¨????\n\t{\n\t\tconst ld tan = V*V / G / p.real();\n\t\tconst ld theta = atan(tan);\n\t\tconst ld vx = V*cos(theta);\n\t\tconst ld vy = V*sin(theta);\n\t\tconst ld time = p.real() / vx;\n\t\tconst ld max_y = vy*time - G*time*time / 2;\n\t\tif (max_y < p.imag()) {\n\t\t\tok = false;\n\t\t}\n\t\tif (leftup) {\n\t\t\tamax = tan;\n\t\t\tamin = 0;\n\t\t}\n\t\telse {\n\t\t\tamax = 1e18;\n\t\t\tamin = tan;\n\t\t}\n\t}\n\tif (ok) {\n\t\tint rep = 1000;\n\t\twhile (rep--) {\n\t\t\tconst ld amidtan = (amin + amax) / 2;\n\t\t\tconst ld theta = atan(amidtan);\n\t\t\tconst ld vx = V*cos(theta);\n\t\t\tconst ld vy = V*sin(theta);\n\t\t\tconst ld time = p.real() / vx;\n\t\t\tconst ld ay = vy*time - G*time*time / 2;\n\t\t\tif (ay > p.imag()) {\n\t\t\t\tif (leftup) {\n\t\t\t\t\tamax = amidtan;\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tamin = amidtan;\n\t\t\t\t}\n\t\t\t}\n\t\t\telse {\n\t\t\t\tif (leftup) {\n\t\t\t\t\tamin = amidtan;\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tamax = amidtan;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tconst ld amidtan = (amin + amax) / 2;\n\t\tconst ld theta = atan(amidtan);\n\t\tconst ld vx = V*cos(theta);\n\t\tconst ld vy = V*sin(theta);\n\t\tconst ld time = p.real() / vx;\n\t\tconst ld ay = vy*time - G*time*time / 2;\n\t\ttans.push_back(amin);\n\t}\n}\n\nint main() { cin >> N >> V >> X >> Y;\n\tvector<box>bs;\n\tfor (int i = 0; i < N; ++i) {\n\t\tint L, B, R, T; cin >> L >> B >> R >> T;\n\t\tbox b;\n\t\tb.xs.push_back(L);\n\t\tb.xs.push_back(R);\n\t\tb.ys.push_back(B);\n\t\tb.ys.push_back(T);\n\t\tbs.push_back(b);\n\t}\n\tconst ld down = Y;\n\tld up = 1e18;\n\tfor (int i = 0; i < N; ++i) {\n\t\tif (bs[i].xs[0] < X&&X < bs[i].xs[1]) {\n\t\t\tup = min(up, bs[i].ys[0]);\n\t\t}\n\t}\n\tvector<ld>tans;\n\tfor (auto b : bs) {\n\t\tfor (int x = 0; x < 2; ++x) {\n\t\t\tfor (int y = 0; y < 2; ++y) {\n\t\t\t\tPoint p(b.xs[x], b.ys[y]);\n\t\t\t\tgettan(tans, p,(x+y)%2);\n\t\t\t}\n\t\t}\n\t}\n\t{\n\t\tPoint p(X,Y);\n\t\tgettan(tans, p,true);\n\t}\n\n\tstring ans = \"No\";\n\tfor (auto t : tans) {\n\t\tconst ld theta = atan(t);\n\t\tconst ld vx = V*cos(theta);\n\t\tconst ld vy = V*sin(theta);\n\t\tconst ld time = X / vx;\n\t\tconst ld ay = vy*time - G*time*time / 2;\n\t\tif (down-eps <= ay&&ay <= up+eps) {\n\t\t\tbool ok = true;\n\t\t\tfor (auto b : bs) {\n\t\t\t\tif (!check(theta, b))ok = false;\n\t\t\t}\n\t\t\tif (ok) {\n\t\t\t\tans = \"Yes\";\n\t\t\t}\n\t\t}\n\t\telse {\n\t\t\tcontinue;\n\t\t}\n\t}\n\tcout << ans << endl;\n\treturn 0;\n}", "accuracy": 0.16666666666666666, "time_ms": 30, "memory_kb": 3516, "score_of_the_acc": -0.8733, "final_rank": 18 }, { "submission_id": "aoj_2308_1563859", "code_snippet": "#include<iostream>\n#include<cstdio>\n#include<cmath>\n#include<algorithm>\nusing namespace std;\nconst double eps=1e-8;\nconst int N=100;\nconst int MAXN=1000000;\nconst double pi=acos(-1);\nconst double gg=9.8;\nint n;\ndouble v,x,y,l[N],b[N],r[N],t[N],B,A,rr[N];\nint work(double x,double y)\n{\n\tdouble yy=A * x * x + B * x;\n\t//if (fabs(yy-y) < eps) return 0;\n\tif (y > yy) return 1;\n\treturn -1;\n}\nbool check(double thi)\n{\n\tA=-gg/(2 * v * v * cos(thi) * cos(thi));\n\tB=tan(thi);\n\tdouble MAX_top=-B*B/(4 *A);\n\tdouble mid=-B/(2 * A);\n\tfor(int i=1;i<=n;i++)\n\tif (l[i] < x+eps)\n\t{\n\t\t//rr[i]=min(r[i],x);\n\t\trr[i]=r[i];\n\t\tif (work(l[i],t[i]) * work(rr[i],t[i]) <0) return 0;\n\t\tif (work(l[i],b[i]) * work(rr[i],b[i]) <0) return 0;\n\t\tif (work(l[i],t[i]) * work(l[i],b[i]) <0) return 0;\n\t\tif (r[i]<x+ eps && work(r[i],t[i]) * work(r[i],b[i]) <0) return 0;\n\t\tif (work(l[i],b[i])>0 && work(rr[i],b[i])>0 && MAX_top > b[i]-eps && (mid>l[i] && mid<rr[i])) return 0;\n\t}\n\tif (work(x,y) > 0) return 0;\n\tdouble tmp=A * x * x + B * x;\n\tfor(int i=1;i<=n;i++)\n\t\tif (l[i]< x-eps && r[i]> x+eps)\n\t\t{\n\t\t\tif (t[i] < tmp-eps && t[i]>y+eps) return 0;\n\t\t\tif (b[i] < tmp-eps && b[i]>y+eps) return 0;\n\t\t}\n\treturn 1;\n}\nint main()\n{\n\tcin>>n>>v>>x>>y;\n\tfor(int i=1;i<=n;i++)\n\t\tscanf(\"%lf%lf%lf%lf\",&l[i],&b[i],&r[i],&t[i]);\n\tfor(int i=1;i<MAXN;i++)\n\t{\n\t\tdouble thi=pi / 2 * i / MAXN;\n\t\tif (check(thi))\n\t\t{\n\t\t\tcout<<\"Yes\"<<endl;\n\t\t\treturn 0;\n\t\t}\n\t}\n\tcout<<\"No\"<<endl;\n}", "accuracy": 0.26666666666666666, "time_ms": 220, "memory_kb": 1356, "score_of_the_acc": -0.4468, "final_rank": 11 }, { "submission_id": "aoj_2308_1563855", "code_snippet": "#include<iostream>\n#include<cstdio>\n#include<cmath>\n#include<algorithm>\nusing namespace std;\nconst double eps=1e-8;\nconst int N=100;\nconst int MAXN=1000000;\nconst double pi=acos(-1);\nconst double gg=9.8;\nint n;\ndouble v,x,y,l[N],b[N],r[N],t[N],B,A,rr[N];\nint work(double x,double y)\n{\n\tdouble yy=A * x * x + B * x;\n\t//if (fabs(yy-y) < eps) return 0;\n\tif (y > yy) return 1;\n\treturn -1;\n}\nbool check(double thi)\n{\n\tA=-gg/(2 * v * v * cos(thi) * cos(thi));\n\tB=tan(thi);\n\tdouble MAX_top=-B*B/(4 *A);\n\tfor(int i=1;i<=n;i++)\n\tif (l[i] < x+eps)\n\t{\n\t\trr[i]=min(r[i],x);\n\t\tif (work(l[i],t[i]) * work(rr[i],t[i]) <0) return 0;\n\t\tif (work(l[i],b[i]) * work(rr[i],b[i]) <0) return 0;\n\t\tif (work(l[i],t[i]) * work(l[i],b[i]) <0) return 0;\n\t\tif (r[i]<x+ eps && work(r[i],t[i]) * work(r[i],b[i]) <0) return 0;\n\t\tif (work(l[i],b[i])>0 && work(rr[i],b[i])>0 && MAX_top > b[i]-eps) return 0;\n\t}\n\tif (work(x,y) > 0) return 0;\n\tdouble tmp=A * x * x + B * x;\n\tfor(int i=1;i<=n;i++)\n\t\tif (l[i]< x-eps && r[i]> x+eps)\n\t\t{\n\t\t\tif (t[i] < tmp-eps && t[i]>y+eps) return 0;\n\t\t\tif (b[i] < tmp-eps && b[i]>y+eps) return 0;\n\t\t}\n\treturn 1;\n}\nint main()\n{\n\tcin>>n>>v>>x>>y;\n\tfor(int i=1;i<=n;i++)\n\t\tscanf(\"%lf%lf%lf%lf\",&l[i],&b[i],&r[i],&t[i]);\n\tfor(int i=1;i<MAXN;i++)\n\t{\n\t\tdouble thi=pi / 2 * i / MAXN;\n\t\tif (check(thi))\n\t\t{\n\t\t\tcout<<\"Yes\"<<endl;\n\t\t\treturn 0;\n\t\t}\n\t}\n\tcout<<\"No\"<<endl;\n}", "accuracy": 0.1, "time_ms": 70, "memory_kb": 1360, "score_of_the_acc": -0.1292, "final_rank": 19 }, { "submission_id": "aoj_2308_1563840", "code_snippet": "#include<iostream>\n#include<cstdio>\n#include<cmath>\n#include<algorithm>\nusing namespace std;\nconst double eps=1e-8;\nconst int N=100;\nconst int MAXN=1000000;\nconst double pi=acos(-1);\nconst double gg=9.8;\nint n;\ndouble v,x,y,l[N],b[N],r[N],t[N],B,A,rr[N];\nint work(double x,double y)\n{\n\tdouble yy=A * x * x + B * x;\n\t//if (fabs(yy-y) < eps) return 0;\n\tif (y > yy) return 1;\n\treturn -1;\n}\nbool check(double thi)\n{\n\tA=-gg/(2 * v * v * cos(thi) * cos(thi));\n\tB=tan(thi);\n\tdouble MAX_top=-B*B/(4 *A);\n\tfor(int i=1;i<=n;i++)\n\tif (l[i] < x+eps)\n\t{\n\t\trr[i]=min(r[i],x);\n\t\tif (work(l[i],t[i]) * work(rr[i],t[i]) <0) return 0;\n\t\tif (work(l[i],b[i]) * work(rr[i],b[i]) <0) return 0;\n\t\tif (work(l[i],t[i]) * work(l[i],b[i]) <0) return 0;\n\t\tif (r[i]<x+ eps && work(r[i],t[i]) * work(r[i],b[i]) <0) return 0;\n\t\tif (work(l[i],b[i])>0 && work(r[i],b[i])>0 && MAX_top > b[i]-eps) return 0;\n\t}\n\tif (work(x,y) > 0) return 0;\n\tdouble tmp=A * x * x + B * x;\n\tfor(int i=1;i<=n;i++)\n\t\tif (l[i]< x-eps && r[i]> x+eps)\n\t\t{\n\t\t\tif (t[i] < tmp-eps && t[i]>y+eps) return 0;\n\t\t\tif (b[i] < tmp-eps && b[i]>y+eps) return 0;\n\t\t}\n\treturn 1;\n}\nint main()\n{\n\tcin>>n>>v>>x>>y;\n\tfor(int i=1;i<=n;i++)\n\t\tscanf(\"%lf%lf%lf%lf\",&l[i],&b[i],&r[i],&t[i]);\n\tfor(int i=1;i<MAXN;i++)\n\t{\n\t\tdouble thi=pi / 2 * i / MAXN;\n\t\tif (check(thi))\n\t\t{\n\t\t\tcout<<\"Yes\"<<endl;\n\t\t\treturn 0;\n\t\t}\n\t}\n\tcout<<\"No\"<<endl;\n}", "accuracy": 0.1, "time_ms": 70, "memory_kb": 1360, "score_of_the_acc": -0.1292, "final_rank": 19 } ]
aoj_2315_cpp
問題文 影の魔女 ELSAMARIA はすべてのものに対して祈りを続ける魔女である.影に捕まったあらゆる命はこの魔女の中に引きずり込まれてしまう. 影は触手のように動き 美樹さやか を引きずり込もうと狙っている. 美樹さやか はこの魔女に捕まることなく攻撃するために,魔女にとって予測の難しいでたらめな動きをすることを考えた.具体的には,まず頭の中で 1 から N までの整数を一様な確率で K 個作り,その合計値の距離だけ今の位置から魔女の方向へジャンプする,ということを繰り返すのである.そして魔女の位置に到達した時に攻撃するのである. さやか と魔女はある一直線上にいるとし,最初魔女は座標 0 , さやか は座標 S の位置にいるものとする.また魔女は動かないものとする.何度ジャンプした後に魔女に最初の攻撃を与えることができるだろうか.期待値を求めよ. 入力形式 入力は以下の形式で与えられる. S\ N\ K S は最初に さやか がいる座標である. N は さやか が頭の中で作る整数の範囲の上限, K は さやか が 1 回のジャンプに用いる整数の個数である. 出力形式 さやか が魔女の位置に到達するために必要なジャンプの回数の期待値を 1 行に出力せよ.もし何度ジャンプしても魔女の位置へ到達できない,もしくは期待値が有限値に収まらない場合は -1 と出力せよ. 小数点以下何桁でも出力して構わないが,相対誤差あるいは絶対誤差が 10 -6 未満になっていなければならない. 制約 -10 9 ≤ S ≤ 10 9 1 ≤ N ≤ 10 1 ≤ K ≤ 10 入力値は全て整数である. 入出力例 入力例 1 6 6 1 出力例1 6.00000000 入力例 2 -100 7 5 出力例 2 43.60343043 入力例 3 756434182 9 10 出力例 3 15128783.80867078 Problem Setter: Flat35
[ { "submission_id": "aoj_2315_8371789", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nvector<vector<long double> > matmul(int N, const vector<vector<long double> >& mat1, const vector<vector<long double> >& mat2) {\n\tvector<vector<long double> > res(N, vector<long double>(N));\n\tfor (int i = 0; i < N; i++) {\n\t\tfor (int j = 0; j < N; j++) {\n\t\t\tfor (int k = 0; k < N; k++) {\n\t\t\t\tres[i][j] += mat1[i][k] * mat2[k][j];\n\t\t\t}\n\t\t}\n\t}\n\treturn res;\n}\n\nvoid gaussian_elimination(int N, vector<vector<long double> >& mat) {\n\tint row = 0;\n\tfor (int i = 0; i < N; i++) {\n\t\tint opt = -1;\n\t\tlong double optmag = 0.0;\n\t\tfor (int j = row; j < N; j++) {\n\t\t\tif (optmag < abs(mat[j][i])) {\n\t\t\t\topt = j;\n\t\t\t\toptmag = abs(mat[j][i]);\n\t\t\t}\n\t\t}\n\t\tif (opt == -1) {\n\t\t\tcontinue;\n\t\t}\n\t\tswap(mat[row], mat[opt]);\n\t\tdouble submul = 1.0 / mat[row][i];\n\t\tfor (int j = i + 1; j <= N; j++) {\n\t\t\tmat[row][j] *= submul;\n\t\t}\n\t\tfor (int j = 0; j < N; j++) {\n\t\t\tif (j != row) {\n\t\t\t\tdouble mul = mat[j][i];\n\t\t\t\tfor (int k = i; k <= N; k++) {\n\t\t\t\t\tmat[j][k] -= mat[row][k] * mul;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\trow += 1;\n\t}\n}\n\nlong double solve(long long S, int N, int K) {\n\t// step #1. calculate probability of transition\n\tvector<long double> prob = { 1.0 };\n\tfor (int i = 1; i <= K; i++) {\n\t\tvector<long double> nprob(N * i + 1);\n\t\tlong double s = 0.0;\n\t\tfor (int j = 0; j <= N * i; j++) {\n\t\t\tnprob[j] = s / N;\n\t\t\ts += (j <= N * (i - 1) ? prob[j] : 0.0);\n\t\t\ts -= (j >= N ? prob[j - N] : 0.0);\n\t\t}\n\t\tprob = nprob;\n\t}\n\n\t// step #2. create matrix\n\tint Z = N * K;\n\tvector<vector<long double> > mat0(2 * Z, vector<long double>(2 * Z, 0.0L));\n\tfor (int i = 0; i < Z - 1; i++) {\n\t\tmat0[i + 1][i] = 1.0L;\n\t\tmat0[Z + i + 1][Z + i] = 1.0L;\n\t}\n\tfor (int i = 0; i < Z; i++) {\n\t\tmat0[0][i] = prob[i + 1];\n\t}\n\tfor (int i = 0; i < Z; i++) {\n\t\tmat0[Z][i] = prob[i + 1];\n\t\tmat0[Z][Z + i] = prob[i + 1];\n\t}\n\t\n\t// separate by cases\n\tvector<long double> p(Z), t(Z);\n\tif (S < Z) {\n\t\tp[S] = 1.0L;\n\t\tt[S] = 0.0L;\n\t}\n\telse {\n\t\t// step #3. matrix exponentation\n\t\tvector<vector<long double> > mat1(2 * Z, vector<long double>(2 * Z));\n\t\tfor (int i = 0; i < 2 * Z; i++) {\n\t\t\tmat1[i][i] = 1.0L;\n\t\t}\n\t\tvector<vector<long double> > mat2 = mat0;\n\t\tlong long rem = S - Z;\n\t\twhile (rem >= 1) {\n\t\t\tif (rem % 2 == 1) {\n\t\t\t\tmat1 = matmul(2 * Z, mat1, mat2);\n\t\t\t}\n\t\t\tmat2 = matmul(2 * Z, mat2, mat2);\n\t\t\trem /= 2;\n\t\t}\n\t\t\n\t\t// step #4. calculate answer\n\t\tvector<long double> x(2 * Z);\n\t\tfor (int i = 0; i < 2 * Z; i++) {\n\t\t\tx[i] = mat1[i][0];\n\t\t}\n\t\tfor (int i = 0; i < Z; i++) {\n\t\t\tfor (int j = Z; j <= Z + i; j++) {\n\t\t\t\tp[i] += x[j - Z] * prob[j - i];\n\t\t\t\tt[i] += x[j - Z + Z] * prob[j - i];\n\t\t\t}\n\t\t\tt[i] += p[i];\n\t\t}\n\t}\n\n\t// step #5. compute systems of linear equation\n\tvector<vector<long double> > mat3(Z - 1, vector<long double>(Z));\n\tfor (int i = 1; i < Z; i++) {\n\t\tfor (int j = 1; j <= Z; j++) {\n\t\t\tint pos = abs(i - j);\n\t\t\tif (pos != 0) {\n\t\t\t\tmat3[i - 1][pos - 1] -= prob[j];\n\t\t\t}\n\t\t}\n\t\tmat3[i - 1][i - 1] += 1.0;\n\t\tmat3[i - 1][Z - 1] += 1.0;\n\t}\n\tgaussian_elimination(Z - 1, mat3);\n\tvector<long double> e(Z);\n\tfor (int i = 1; i < Z; i++) {\n\t\te[i] = mat3[i - 1][Z - 1];\n\t}\n\n\t// step #6. calculate answer\n\tlong double ans = 0.0;\n\tfor (int i = 0; i < Z; i++) {\n\t\tans += t[i];\n\t}\n\tfor (int i = 1; i < Z; i++) {\n\t\tans += p[i] * e[i];\n\t}\n\n\treturn ans;\n}\n\nint main() {\n\tlong long S; int N, K;\n\tcin >> S >> N >> K;\n\tS = abs(S);\n\tif (N == 1) {\n\t\tcout << (S % K == 0 ? S / K : -1) << endl;\n\t}\n\telse {\n\t\tlong double ans = solve(S, N, K);\n\t\tcout.precision(15);\n\t\tcout << fixed << ans << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 660, "memory_kb": 5652, "score_of_the_acc": -0.6153, "final_rank": 11 }, { "submission_id": "aoj_2315_7242759", "code_snippet": "#include <iostream>\n#include <cmath>\n#include <algorithm>\nusing namespace std;\n\nint S, N, K;\ndouble D[19][109], E[109];\ndouble Prob1[150009];\ndouble Expc1[150009];\ndouble Prob2[7009][109];\ndouble Expc2[7009][109];\ndouble ProbList[109], ExpcList[109], ExpcSecond[109];\n\nvoid GetFirstAns(int BASE) {\n\tfor (int i = 0; i <= BASE + 200; i++) Prob1[i] = 0.0;\n\tfor (int i = 0; i <= BASE + 200; i++) Expc1[i] = 0.0;\n\tProb1[0] = 1.0;\n\tfor (int i = 1; i <= BASE + 200; i++) {\n\t\tfor (int j = 1; j <= N * K; j++) {\n\t\t\tint pre = i - j; if (pre < 0 || pre >= BASE) continue;\n\t\t\tProb1[i] += (Prob1[pre] * E[j]);\n\t\t\tExpc1[i] += (Prob1[pre] * E[j]) * (Expc1[pre] + 1.0);\n\t\t}\n\t\tif (Prob1[i] != 0.0) Expc1[i] /= Prob1[i];\n\t}\n}\n\nvoid GetSecondAns() {\n\tfor (int i = 0; i <= 7000; i++) {\n\t\tfor (int j = 0; j <= N * K; j++) Prob2[i][j] = 0.0;\n\t\tfor (int j = 0; j <= N * K; j++) Expc2[i][j] = 0.0;\n\t}\n\t\n\t// Forward DP\n\tfor (int i = 1; i <= N * K; i++) Prob2[0][i] = ProbList[i];\n\tfor (int i = 0; i < 7000; i++) {\n\t\tfor (int j = 1; j <= N * K; j++) {\n\t\t\tfor (int k = 1; k <= N * K; k++) {\n\t\t\t\tint to = abs(j - k);\n\t\t\t\tProb2[i + 1][to] += (Prob2[i][j] * E[k]);\n\t\t\t}\n\t\t}\n\t}\n\t\n\t// Reverse DP\n\tfor (int i = 6999; i >= 0; i--) {\n\t\tfor (int j = 1; j <= N * K; j++) {\n\t\t\tfor (int k = 1; k <= N * K; k++) {\n\t\t\t\tint to = abs(j - k);\n\t\t\t\tExpc2[i][j] += (1.0 * (Expc2[i + 1][to] + 1.0) * E[k]);\n\t\t\t}\n\t\t}\n\t}\n}\n\nint main() {\n\t// Input\n\tcin >> S >> N >> K; S = abs(S);\n\tif (N == 1) {\n\t\tif (S % K == 0) cout << S/ K << endl;\n\t\telse cout << \"-1\" << endl;\n\t\treturn 0;\n\t}\n\t\n\t// Prepare\n\tD[0][0] = 1.0;\n\tfor (int i = 0; i < K; i++) {\n\t\tfor (int j = 0; j < N * K; j++) {\n\t\t\tfor (int k = 1; k <= N; k++) D[i+1][j+k] += 1.0 * D[i][j] / N;\n\t\t}\n\t}\n\tfor (int i = 1; i <= N * K; i++) E[i] = D[K][i];\n\t\n\t// Get First Answer\n\tif (S <= N * K) {\n\t\tProbList[S] = 1.0;\n\t\tExpcList[S] = 0.;\n\t}\n\telse {\n\t\tdouble BASE_Expc = 1.0 / (0.5 * (N + 1) * K);\n\t\tint OFFSET = min(100000, S - N * K);\n\t\tGetFirstAns(OFFSET);\n\t\tfor (int i = 0; i <= N * K; i++) {\n\t\t\tProbList[N * K - i] = Prob1[OFFSET + i];\n\t\t\tExpcList[N * K - i] = Expc1[OFFSET + i] + 1.0 * ((S - N * K) - OFFSET) * BASE_Expc;\n\t\t}\n\t}\n\t\n\t// Get Second Answer\n\tGetSecondAns();\n\tfor (int i = 1; i <= N * K; i++) {\n\t\tExpcSecond[i] = Expc2[0][i];\n\t\t//cout << i << \" \" << ProbList[i] << \" \" << ExpcSecond[i] << endl;\n\t}\n\t\n\t// Get Final Answer\n\tdouble Answer = 0.0;\n\tfor (int i = 1; i <= N * K; i++) Answer += (ExpcList[i] + ExpcSecond[i]) * ProbList[i];\n\tprintf(\"%.12lf\\n\", Answer);\n\treturn 0;\n}", "accuracy": 1, "time_ms": 170, "memory_kb": 17084, "score_of_the_acc": -1.1056, "final_rank": 12 }, { "submission_id": "aoj_2315_6079635", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define eps 1e-9\n#define INF 2000000000 // 2e9\n#define LLINF 2000000000000000000ll // 2e18 (llmax:9e18)\n#define all(x) (x).begin(), (x).end()\n#define sq(x) ((x) * (x))\n\n#define rep(i, x) for (int i = 0; i < (int)(x); i++)\n#define drep(i, x) for (int i = ((int)(x)-1); i >= 0; i--)\n\n#define popcount __builtin_popcount\n#define next __next\n#define prev __prev\n\n#ifndef LOCAL\n#define dmp(...) ;\n#else\n#define dmp(...) \\\n cerr << \"[ \" << #__VA_ARGS__ << \" ] : \" << dump_str(__VA_ARGS__) << endl\n#endif\n\n// ---------------- Utility ------------------\n\ntemplate <class T>\nbool chmin(T &a, const T &b) {\n if (a <= b) return false;\n a = b;\n return true;\n}\n\ntemplate <class T>\nbool chmax(T &a, const T &b) {\n if (a >= b) return false;\n a = b;\n return true;\n}\n\ntemplate <class T>\nusing MaxHeap = priority_queue<T>;\n\ntemplate <class T>\nusing MinHeap = priority_queue<T, vector<T>, greater<T>>;\n\ntemplate <class T>\nvector<T> vect(int len, T elem) {\n return vector<T>(len, elem);\n}\n\n// ----------------- Input -------------------\n\ntemplate <class T, class U>\nistream &operator>>(istream &is, pair<T, U> &p) {\n return is >> p.first >> p.second;\n}\n\ntemplate <class T>\nistream &operator>>(istream &is, vector<T> &vec) {\n for (int i = 0; i < vec.size(); i++) is >> vec[i];\n return is;\n}\n\n// ----------------- Output ------------------\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n return os << p.first << ',' << p.second;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const vector<T> &v) {\n for (const T &e : v) os << e << \" \";\n return os;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const deque<T> &d) {\n for (const T &e : d) os << e << \" \";\n return os;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const set<T> &s) {\n os << \"{ \";\n for (const T &e : s) os << e << \" \";\n return os << \"}\";\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const map<T, U> &m) {\n os << \"{ \";\n for (const auto &[key, val] : m) os << \"( \" << key << \" -> \" << val << \" ) \";\n return os << \"}\";\n}\n\ntemplate <class TupleTy, size_t... I>\nvoid dump_tuple(ostream &os, const TupleTy t, std::index_sequence<I...>) {\n (..., (os << (I == 0 ? \"\" : \",\") << std::get<I>(t)));\n}\n\ntemplate <class... Args>\nostream &operator<<(ostream &os, const tuple<Args...> &t) {\n os << \"(\";\n dump_tuple(os, t, std::make_index_sequence<sizeof...(Args)>());\n return os << \")\";\n}\n\nvoid dump_str_rec(ostringstream &) {}\n\ntemplate <class Head, class... Tail>\nvoid dump_str_rec(ostringstream &oss, Head &&head, Tail &&... tail) {\n oss << \", \" << head;\n dump_str_rec(oss, forward<Tail>(tail)...);\n}\n\ntemplate <class T, class... U>\nstring dump_str(T &&arg, U &&... args) {\n ostringstream oss;\n oss << arg;\n dump_str_rec(oss, forward<U>(args)...);\n return oss.str();\n}\n\n// --------------- Fast I/O ------------------\n\nvoid fastio() {\n cin.tie(0);\n ios::sync_with_stdio(0);\n cout << fixed << setprecision(20);\n}\n\n// ------------------ ACL --------------------\n\n// #include <atcoder/modint>\n// constexpr int MOD = 998244353;\n// constexpr int MOD = 1000000007;\n// using mint = atcoder::static_modint<MOD>;\n// ostream &operator<<(ostream &os, const mint &v) {\n// return os << v.val();\n// }\n\n// ------------ End of template --------------\n\n#define endl \"\\n\"\nusing ll = long long;\nusing pii = pair<int, int>;\n\nconst bool multitest = false;\n\n#define double long double\n\n// N変数連立方程式(Ax=bの形)を解く\n//\n// 引数のAは拡大係数行列. (Ax=bでAとbをそのまま並べてつなげたもの)\n// すなわち\n// A[i][0] * x_0 + A[i][1] * x_1 + ... A[i][n - 1] * x_{n-1} = A[i][n]\n// (0 <= i < n)\n//\n//\n// 関数が終わったときにAの左側は単位行列になる\n// Note: 誤差に注意\n//\n// Verify:\n// https://onlinejudge.u-aizu.ac.jp/solutions/problem/1328/review/4927454/okuraofvegetable/C++17\n// https://onlinejudge.u-aizu.ac.jp/solutions/problem/2171/review/6045411/okuraofvegetable/C++17\n\ndouble myabs(double a) {\n if (a < 0.0) return -a;\n return a;\n}\n\nvector<double> solve_equation(vector<vector<double>> &A) {\n assert(A[0].size() == A.size() + 1);\n for (int i = 0; i < A.size(); i++) {\n if (myabs(A[i][i]) < eps) {\n for (int j = i + 1; j < A.size(); j++) {\n if (abs(A[j][i]) > eps) {\n swap(A[i], A[j]);\n break;\n }\n }\n }\n double r = A[i][i];\n if (myabs(r) < eps) {\n // dmp(r);\n return vector<double>();\n }\n for (int j = i; j <= A.size(); j++) { A[i][j] /= r; }\n for (int j = i + 1; j < A.size(); j++) {\n double s = A[j][i];\n for (int k = i; k <= A.size(); k++) { A[j][k] -= A[i][k] * s; }\n }\n }\n for (int i = A.size() - 1; i > 0; i--) {\n for (int j = 0; j < i; j++) {\n double r = A[j][i];\n for (int k = i; k <= A.size(); k++) { A[j][k] -= A[i][k] * r; }\n }\n }\n vector<double> sol;\n for (int i = 0; i < A.size(); i++) sol.push_back(A[i][A.size()]);\n return sol;\n}\n\n// Matrix class\n//\n// constructor of T : T(1),T(0) must be unit of T\n//\n// Verified: DDCC B\n// https://atcoder.jp/contests/ddcc2020-final/submissions/9941022\n//\n// Verified: determinant\n// https://judge.yosupo.jp/submission/45201\n//\n\ntemplate <class T>\nstruct Matrix {\n int H, W;\n vector<vector<T>> elem;\n Matrix(int H, int W, T val = T(0)) : H(H), W(W) {\n elem.resize(H);\n for (int i = 0; i < H; i++) elem[i].assign(W, val);\n }\n Matrix(const vector<vector<T>> &m)\n : H(m.size()), W(m.size() ? m[0].size() : 0) {\n elem.resize(H);\n for (int i = 0; i < H; i++) elem[i].resize(W);\n for (int i = 0; i < H; i++) elem[i] = m[i];\n }\n vector<T> &operator[](int h) { return elem[h]; }\n static Matrix identity(int N) {\n Matrix a(N, N);\n for (int i = 0; i < N; i++) {\n for (int j = 0; j < N; j++) {\n if (i == j)\n a.elem[i][i] = T(1);\n else\n a.elem[i][j] = T(0);\n }\n }\n return a;\n }\n Matrix operator+(const Matrix &m) const {\n assert(H == m.H && W == m.W);\n Matrix a(H, W);\n for (int i = 0; i < H; i++) {\n for (int j = 0; j < W; j++) {\n a.elem[i][j] = this->elem[i][j] + m.elem[i][j];\n }\n }\n return a;\n }\n Matrix operator-(const Matrix &m) const {\n assert(H == m.H && W == m.W);\n Matrix a(H, W);\n for (int i = 0; i < H; i++) {\n for (int j = 0; j < W; j++) {\n a.elem[i][j] = this->elem[i][j] - m.elem[i][j];\n }\n }\n return a;\n }\n Matrix operator*(const Matrix &m) const {\n assert(W == m.H);\n Matrix a(H, m.W);\n for (int i = 0; i < H; i++) {\n for (int j = 0; j < m.W; j++) {\n for (int k = 0; k < W; k++) {\n a.elem[i][j] = a.elem[i][j] + (this->elem[i][k] * m.elem[k][j]);\n }\n }\n }\n return a;\n }\n Matrix &operator+=(const Matrix &m) { return *this = *this + m; }\n Matrix &operator-=(const Matrix &m) { return *this = *this - m; }\n Matrix &operator*=(const Matrix &m) { return *this = *this * m; }\n Matrix pow(long long x) const {\n Matrix a = identity(H);\n Matrix b = *this;\n while (x) {\n if (x & 1) a *= b;\n b *= b;\n x >>= 1;\n }\n return a;\n }\n\n T determinant() {\n Matrix m(*this);\n assert(m.W == m.H);\n T ret(1);\n for (int i = 0; i < m.H; i++) {\n int idx = -1;\n for (int j = i; j < m.H; j++) {\n if (m[j][i] != T(0)) idx = j;\n }\n if (idx == -1) return T(0);\n if (i != idx) {\n ret *= T(-1);\n swap(m[i], m[idx]);\n }\n ret *= m[i][i];\n T div = m[i][i];\n for (int j = i; j < m.W; j++) m[i][j] /= div;\n for (int j = i + 1; j < m.H; j++) {\n T a = m[j][i];\n for (int k = i; k < m.W; k++) m[j][k] -= m[i][k] * a;\n }\n }\n return ret;\n }\n\n friend ostream &operator<<(ostream &os, const Matrix &x) {\n for (int i = 0; i < x.H; i++) {\n for (int j = 0; j < x.W; j++) {\n os << x.elem[i][j];\n if (j + 1 < x.W) cout << ' ';\n }\n if (i + 1 < x.H) cout << endl;\n }\n return os;\n }\n friend istream &operator>>(istream &is, Matrix &x) {\n for (auto &v : x.elem) is >> v;\n return is;\n }\n};\n\nvoid solve() {\n int S, N, K;\n cin >> S >> N >> K;\n if (S < 0) S *= -1;\n\n if (N == 1) {\n if (S % K == 0)\n cout << S / K << endl;\n else\n cout << -1 << endl;\n return;\n }\n\n auto dp = vect(K + 1, vect(N * K + 1, (double)0.0));\n dp[0][0] = 1.0;\n for (int i = 0; i < K; i++) {\n for (int j = 0; j <= N * K; j++) {\n for (int k = 1; k <= N; k++) {\n if (j + k <= N * K) dp[i + 1][j + k] += dp[i][j] * (1.0 / (double)N);\n }\n }\n }\n\n auto A = vect(N * K, vect(N * K + 1, (double)0.0));\n\n A[0][0] = 1.0;\n for (int i = 1; i < N * K; i++) {\n for (int j = 1; j <= N * K; j++) { A[i][abs(i - j)] += dp[K][j]; }\n A[i][i] -= 1.0;\n A[i][N * K] = -1.0;\n }\n\n // for (auto a : A) dmp(a);\n\n auto sol = solve_equation(A);\n // dmp(sol);\n if (sol.empty()) {\n cout << -1 << endl;\n return;\n }\n\n Matrix<double> M(N * K + 1, N * K + 1);\n Matrix<double> v(N * K + 1, 1);\n if (S >= N * K) {\n for (int i = 0; i < N * K; i++) { M[0][i] = dp[K][i + 1]; }\n M[0][N * K] = 1.0;\n for (int i = 1; i < N * K; i++) { M[i][i - 1] = 1.0; }\n M[N * K][N * K] = 1.0;\n M = M.pow(S - N * K + 1);\n\n for (int i = 0; i < N * K; i++) { v[N * K - 1 - i][0] = sol[i]; }\n v[N * K][0] = 1.0;\n }\n\n#undef double\n if (S < N * K) {\n cout << (double)sol[S] << endl;\n } else {\n cout << (double)(M * v)[0][0] << endl;\n }\n return;\n}\n\nint main() {\n fastio();\n if (!multitest) {\n solve();\n } else {\n cerr << \"[Warning] Multi testcase mode on\" << endl;\n int t;\n cin >> t;\n while (t--) solve();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 4080, "score_of_the_acc": -0.106, "final_rank": 8 }, { "submission_id": "aoj_2315_6079634", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define eps 1e-9\n#define INF 2000000000 // 2e9\n#define LLINF 2000000000000000000ll // 2e18 (llmax:9e18)\n#define all(x) (x).begin(), (x).end()\n#define sq(x) ((x) * (x))\n\n#define rep(i, x) for (int i = 0; i < (int)(x); i++)\n#define drep(i, x) for (int i = ((int)(x)-1); i >= 0; i--)\n\n#define popcount __builtin_popcount\n#define next __next\n#define prev __prev\n\n#ifndef LOCAL\n#define dmp(...) ;\n#else\n#define dmp(...) \\\n cerr << \"[ \" << #__VA_ARGS__ << \" ] : \" << dump_str(__VA_ARGS__) << endl\n#endif\n\n// ---------------- Utility ------------------\n\ntemplate <class T>\nbool chmin(T &a, const T &b) {\n if (a <= b) return false;\n a = b;\n return true;\n}\n\ntemplate <class T>\nbool chmax(T &a, const T &b) {\n if (a >= b) return false;\n a = b;\n return true;\n}\n\ntemplate <class T>\nusing MaxHeap = priority_queue<T>;\n\ntemplate <class T>\nusing MinHeap = priority_queue<T, vector<T>, greater<T>>;\n\ntemplate <class T>\nvector<T> vect(int len, T elem) {\n return vector<T>(len, elem);\n}\n\n// ----------------- Input -------------------\n\ntemplate <class T, class U>\nistream &operator>>(istream &is, pair<T, U> &p) {\n return is >> p.first >> p.second;\n}\n\ntemplate <class T>\nistream &operator>>(istream &is, vector<T> &vec) {\n for (int i = 0; i < vec.size(); i++) is >> vec[i];\n return is;\n}\n\n// ----------------- Output ------------------\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n return os << p.first << ',' << p.second;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const vector<T> &v) {\n for (const T &e : v) os << e << \" \";\n return os;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const deque<T> &d) {\n for (const T &e : d) os << e << \" \";\n return os;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const set<T> &s) {\n os << \"{ \";\n for (const T &e : s) os << e << \" \";\n return os << \"}\";\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const map<T, U> &m) {\n os << \"{ \";\n for (const auto &[key, val] : m) os << \"( \" << key << \" -> \" << val << \" ) \";\n return os << \"}\";\n}\n\ntemplate <class TupleTy, size_t... I>\nvoid dump_tuple(ostream &os, const TupleTy t, std::index_sequence<I...>) {\n (..., (os << (I == 0 ? \"\" : \",\") << std::get<I>(t)));\n}\n\ntemplate <class... Args>\nostream &operator<<(ostream &os, const tuple<Args...> &t) {\n os << \"(\";\n dump_tuple(os, t, std::make_index_sequence<sizeof...(Args)>());\n return os << \")\";\n}\n\nvoid dump_str_rec(ostringstream &) {}\n\ntemplate <class Head, class... Tail>\nvoid dump_str_rec(ostringstream &oss, Head &&head, Tail &&... tail) {\n oss << \", \" << head;\n dump_str_rec(oss, forward<Tail>(tail)...);\n}\n\ntemplate <class T, class... U>\nstring dump_str(T &&arg, U &&... args) {\n ostringstream oss;\n oss << arg;\n dump_str_rec(oss, forward<U>(args)...);\n return oss.str();\n}\n\n// --------------- Fast I/O ------------------\n\nvoid fastio() {\n cin.tie(0);\n ios::sync_with_stdio(0);\n cout << fixed << setprecision(20);\n}\n\n// ------------------ ACL --------------------\n\n// #include <atcoder/modint>\n// constexpr int MOD = 998244353;\n// constexpr int MOD = 1000000007;\n// using mint = atcoder::static_modint<MOD>;\n// ostream &operator<<(ostream &os, const mint &v) {\n// return os << v.val();\n// }\n\n// ------------ End of template --------------\n\n#define endl \"\\n\"\nusing ll = long long;\nusing pii = pair<int, int>;\n\nconst bool multitest = false;\n\n#define double __float128\n\n// N変数連立方程式(Ax=bの形)を解く\n//\n// 引数のAは拡大係数行列. (Ax=bでAとbをそのまま並べてつなげたもの)\n// すなわち\n// A[i][0] * x_0 + A[i][1] * x_1 + ... A[i][n - 1] * x_{n-1} = A[i][n]\n// (0 <= i < n)\n//\n//\n// 関数が終わったときにAの左側は単位行列になる\n// Note: 誤差に注意\n//\n// Verify:\n// https://onlinejudge.u-aizu.ac.jp/solutions/problem/1328/review/4927454/okuraofvegetable/C++17\n// https://onlinejudge.u-aizu.ac.jp/solutions/problem/2171/review/6045411/okuraofvegetable/C++17\n\ndouble abs(double a) {\n if (a < 0.0) return -a;\n return a;\n}\n\nvector<double> solve_equation(vector<vector<double>> &A) {\n assert(A[0].size() == A.size() + 1);\n for (int i = 0; i < A.size(); i++) {\n if (abs(A[i][i]) < eps) {\n for (int j = i + 1; j < A.size(); j++) {\n if (abs(A[j][i]) > eps) {\n swap(A[i], A[j]);\n break;\n }\n }\n }\n double r = A[i][i];\n if (abs(r) < eps) {\n // dmp(r);\n return vector<double>();\n }\n for (int j = i; j <= A.size(); j++) { A[i][j] /= r; }\n for (int j = i + 1; j < A.size(); j++) {\n double s = A[j][i];\n for (int k = i; k <= A.size(); k++) { A[j][k] -= A[i][k] * s; }\n }\n }\n for (int i = A.size() - 1; i > 0; i--) {\n for (int j = 0; j < i; j++) {\n double r = A[j][i];\n for (int k = i; k <= A.size(); k++) { A[j][k] -= A[i][k] * r; }\n }\n }\n vector<double> sol;\n for (int i = 0; i < A.size(); i++) sol.push_back(A[i][A.size()]);\n return sol;\n}\n\n// Matrix class\n//\n// constructor of T : T(1),T(0) must be unit of T\n//\n// Verified: DDCC B\n// https://atcoder.jp/contests/ddcc2020-final/submissions/9941022\n//\n// Verified: determinant\n// https://judge.yosupo.jp/submission/45201\n//\n\ntemplate <class T>\nstruct Matrix {\n int H, W;\n vector<vector<T>> elem;\n Matrix(int H, int W, T val = T(0)) : H(H), W(W) {\n elem.resize(H);\n for (int i = 0; i < H; i++) elem[i].assign(W, val);\n }\n Matrix(const vector<vector<T>> &m)\n : H(m.size()), W(m.size() ? m[0].size() : 0) {\n elem.resize(H);\n for (int i = 0; i < H; i++) elem[i].resize(W);\n for (int i = 0; i < H; i++) elem[i] = m[i];\n }\n vector<T> &operator[](int h) { return elem[h]; }\n static Matrix identity(int N) {\n Matrix a(N, N);\n for (int i = 0; i < N; i++) {\n for (int j = 0; j < N; j++) {\n if (i == j)\n a.elem[i][i] = T(1);\n else\n a.elem[i][j] = T(0);\n }\n }\n return a;\n }\n Matrix operator+(const Matrix &m) const {\n assert(H == m.H && W == m.W);\n Matrix a(H, W);\n for (int i = 0; i < H; i++) {\n for (int j = 0; j < W; j++) {\n a.elem[i][j] = this->elem[i][j] + m.elem[i][j];\n }\n }\n return a;\n }\n Matrix operator-(const Matrix &m) const {\n assert(H == m.H && W == m.W);\n Matrix a(H, W);\n for (int i = 0; i < H; i++) {\n for (int j = 0; j < W; j++) {\n a.elem[i][j] = this->elem[i][j] - m.elem[i][j];\n }\n }\n return a;\n }\n Matrix operator*(const Matrix &m) const {\n assert(W == m.H);\n Matrix a(H, m.W);\n for (int i = 0; i < H; i++) {\n for (int j = 0; j < m.W; j++) {\n for (int k = 0; k < W; k++) {\n a.elem[i][j] = a.elem[i][j] + (this->elem[i][k] * m.elem[k][j]);\n }\n }\n }\n return a;\n }\n Matrix &operator+=(const Matrix &m) { return *this = *this + m; }\n Matrix &operator-=(const Matrix &m) { return *this = *this - m; }\n Matrix &operator*=(const Matrix &m) { return *this = *this * m; }\n Matrix pow(long long x) const {\n Matrix a = identity(H);\n Matrix b = *this;\n while (x) {\n if (x & 1) a *= b;\n b *= b;\n x >>= 1;\n }\n return a;\n }\n\n T determinant() {\n Matrix m(*this);\n assert(m.W == m.H);\n T ret(1);\n for (int i = 0; i < m.H; i++) {\n int idx = -1;\n for (int j = i; j < m.H; j++) {\n if (m[j][i] != T(0)) idx = j;\n }\n if (idx == -1) return T(0);\n if (i != idx) {\n ret *= T(-1);\n swap(m[i], m[idx]);\n }\n ret *= m[i][i];\n T div = m[i][i];\n for (int j = i; j < m.W; j++) m[i][j] /= div;\n for (int j = i + 1; j < m.H; j++) {\n T a = m[j][i];\n for (int k = i; k < m.W; k++) m[j][k] -= m[i][k] * a;\n }\n }\n return ret;\n }\n\n friend ostream &operator<<(ostream &os, const Matrix &x) {\n for (int i = 0; i < x.H; i++) {\n for (int j = 0; j < x.W; j++) {\n os << x.elem[i][j];\n if (j + 1 < x.W) cout << ' ';\n }\n if (i + 1 < x.H) cout << endl;\n }\n return os;\n }\n friend istream &operator>>(istream &is, Matrix &x) {\n for (auto &v : x.elem) is >> v;\n return is;\n }\n};\n\nvoid solve() {\n int S, N, K;\n cin >> S >> N >> K;\n if (S < 0) S *= -1;\n\n auto dp = vect(K + 1, vect(N * K + 1, (double)0.0));\n dp[0][0] = 1.0;\n for (int i = 0; i < K; i++) {\n for (int j = 0; j <= N * K; j++) {\n for (int k = 1; k <= N; k++) {\n if (j + k <= N * K) dp[i + 1][j + k] += dp[i][j] * (1.0 / (double)N);\n }\n }\n }\n\n auto A = vect(N * K, vect(N * K + 1, (double)0.0));\n\n A[0][0] = 1.0;\n for (int i = 1; i < N * K; i++) {\n for (int j = 1; j <= N * K; j++) { A[i][abs(i - j)] += dp[K][j]; }\n A[i][i] -= 1.0;\n A[i][N * K] = -1.0;\n }\n\n // for (auto a : A) dmp(a);\n\n auto sol = solve_equation(A);\n // dmp(sol);\n if (sol.empty()) {\n cout << -1 << endl;\n return;\n }\n\n Matrix<double> M(N * K + 1, N * K + 1);\n Matrix<double> v(N * K + 1, 1);\n if (S >= N * K) {\n for (int i = 0; i < N * K; i++) { M[0][i] = dp[K][i + 1]; }\n M[0][N * K] = 1.0;\n for (int i = 1; i < N * K; i++) { M[i][i - 1] = 1.0; }\n M[N * K][N * K] = 1.0;\n M = M.pow(S - N * K + 1);\n\n for (int i = 0; i < N * K; i++) { v[N * K - 1 - i][0] = sol[i]; }\n v[N * K][0] = 1.0;\n }\n\n#undef double\n if (S < N * K) {\n cout << (double)sol[S] << endl;\n } else {\n cout << (double)(M * v)[0][0] << endl;\n }\n return;\n}\n\nint main() {\n fastio();\n if (!multitest) {\n solve();\n } else {\n cerr << \"[Warning] Multi testcase mode on\" << endl;\n int t;\n cin >> t;\n while (t--) solve();\n }\n return 0;\n}", "accuracy": 0.1935483870967742, "time_ms": 1440, "memory_kb": 4148, "score_of_the_acc": -1.0547, "final_rank": 18 }, { "submission_id": "aoj_2315_6079630", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define eps 1e-9\n#define INF 2000000000 // 2e9\n#define LLINF 2000000000000000000ll // 2e18 (llmax:9e18)\n#define all(x) (x).begin(), (x).end()\n#define sq(x) ((x) * (x))\n\n#define rep(i, x) for (int i = 0; i < (int)(x); i++)\n#define drep(i, x) for (int i = ((int)(x)-1); i >= 0; i--)\n\n#define popcount __builtin_popcount\n#define next __next\n#define prev __prev\n\n#ifndef LOCAL\n#define dmp(...) ;\n#else\n#define dmp(...) \\\n cerr << \"[ \" << #__VA_ARGS__ << \" ] : \" << dump_str(__VA_ARGS__) << endl\n#endif\n\n// ---------------- Utility ------------------\n\ntemplate <class T>\nbool chmin(T &a, const T &b) {\n if (a <= b) return false;\n a = b;\n return true;\n}\n\ntemplate <class T>\nbool chmax(T &a, const T &b) {\n if (a >= b) return false;\n a = b;\n return true;\n}\n\ntemplate <class T>\nusing MaxHeap = priority_queue<T>;\n\ntemplate <class T>\nusing MinHeap = priority_queue<T, vector<T>, greater<T>>;\n\ntemplate <class T>\nvector<T> vect(int len, T elem) {\n return vector<T>(len, elem);\n}\n\n// ----------------- Input -------------------\n\ntemplate <class T, class U>\nistream &operator>>(istream &is, pair<T, U> &p) {\n return is >> p.first >> p.second;\n}\n\ntemplate <class T>\nistream &operator>>(istream &is, vector<T> &vec) {\n for (int i = 0; i < vec.size(); i++) is >> vec[i];\n return is;\n}\n\n// ----------------- Output ------------------\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n return os << p.first << ',' << p.second;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const vector<T> &v) {\n for (const T &e : v) os << e << \" \";\n return os;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const deque<T> &d) {\n for (const T &e : d) os << e << \" \";\n return os;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const set<T> &s) {\n os << \"{ \";\n for (const T &e : s) os << e << \" \";\n return os << \"}\";\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const map<T, U> &m) {\n os << \"{ \";\n for (const auto &[key, val] : m) os << \"( \" << key << \" -> \" << val << \" ) \";\n return os << \"}\";\n}\n\ntemplate <class TupleTy, size_t... I>\nvoid dump_tuple(ostream &os, const TupleTy t, std::index_sequence<I...>) {\n (..., (os << (I == 0 ? \"\" : \",\") << std::get<I>(t)));\n}\n\ntemplate <class... Args>\nostream &operator<<(ostream &os, const tuple<Args...> &t) {\n os << \"(\";\n dump_tuple(os, t, std::make_index_sequence<sizeof...(Args)>());\n return os << \")\";\n}\n\nvoid dump_str_rec(ostringstream &) {}\n\ntemplate <class Head, class... Tail>\nvoid dump_str_rec(ostringstream &oss, Head &&head, Tail &&... tail) {\n oss << \", \" << head;\n dump_str_rec(oss, forward<Tail>(tail)...);\n}\n\ntemplate <class T, class... U>\nstring dump_str(T &&arg, U &&... args) {\n ostringstream oss;\n oss << arg;\n dump_str_rec(oss, forward<U>(args)...);\n return oss.str();\n}\n\n// --------------- Fast I/O ------------------\n\nvoid fastio() {\n cin.tie(0);\n ios::sync_with_stdio(0);\n cout << fixed << setprecision(20);\n}\n\n// ------------------ ACL --------------------\n\n// #include <atcoder/modint>\n// constexpr int MOD = 998244353;\n// constexpr int MOD = 1000000007;\n// using mint = atcoder::static_modint<MOD>;\n// ostream &operator<<(ostream &os, const mint &v) {\n// return os << v.val();\n// }\n\n// ------------ End of template --------------\n\n#define endl \"\\n\"\nusing ll = long long;\nusing pii = pair<int, int>;\n\nconst bool multitest = false;\n\n#define double long double\n\n// N変数連立方程式(Ax=bの形)を解く\n//\n// 引数のAは拡大係数行列. (Ax=bでAとbをそのまま並べてつなげたもの)\n// すなわち\n// A[i][0] * x_0 + A[i][1] * x_1 + ... A[i][n - 1] * x_{n-1} = A[i][n]\n// (0 <= i < n)\n//\n//\n// 関数が終わったときにAの左側は単位行列になる\n// Note: 誤差に注意\n//\n// Verify:\n// https://onlinejudge.u-aizu.ac.jp/solutions/problem/1328/review/4927454/okuraofvegetable/C++17\n// https://onlinejudge.u-aizu.ac.jp/solutions/problem/2171/review/6045411/okuraofvegetable/C++17\n\nvector<double> solve_equation(vector<vector<double>> &A) {\n assert(A[0].size() == A.size() + 1);\n for (int i = 0; i < A.size(); i++) {\n if (abs(A[i][i]) < eps) {\n for (int j = i + 1; j < A.size(); j++) {\n if (abs(A[j][i]) > eps) {\n swap(A[i], A[j]);\n break;\n }\n }\n }\n double r = A[i][i];\n if (abs(r) < eps) {\n dmp(r);\n return vector<double>();\n }\n for (int j = i; j <= A.size(); j++) { A[i][j] /= r; }\n for (int j = i + 1; j < A.size(); j++) {\n double s = A[j][i];\n for (int k = i; k <= A.size(); k++) { A[j][k] -= A[i][k] * s; }\n }\n }\n for (int i = A.size() - 1; i > 0; i--) {\n for (int j = 0; j < i; j++) {\n double r = A[j][i];\n for (int k = i; k <= A.size(); k++) { A[j][k] -= A[i][k] * r; }\n }\n }\n vector<double> sol;\n for (int i = 0; i < A.size(); i++) sol.push_back(A[i][A.size()]);\n return sol;\n}\n\n// Matrix class\n//\n// constructor of T : T(1),T(0) must be unit of T\n//\n// Verified: DDCC B\n// https://atcoder.jp/contests/ddcc2020-final/submissions/9941022\n//\n// Verified: determinant\n// https://judge.yosupo.jp/submission/45201\n//\n\ntemplate <class T>\nstruct Matrix {\n int H, W;\n vector<vector<T>> elem;\n Matrix(int H, int W, T val = T(0)) : H(H), W(W) {\n elem.resize(H);\n for (int i = 0; i < H; i++) elem[i].assign(W, val);\n }\n Matrix(const vector<vector<T>> &m)\n : H(m.size()), W(m.size() ? m[0].size() : 0) {\n elem.resize(H);\n for (int i = 0; i < H; i++) elem[i].resize(W);\n for (int i = 0; i < H; i++) elem[i] = m[i];\n }\n vector<T> &operator[](int h) { return elem[h]; }\n static Matrix identity(int N) {\n Matrix a(N, N);\n for (int i = 0; i < N; i++) {\n for (int j = 0; j < N; j++) {\n if (i == j)\n a.elem[i][i] = T(1);\n else\n a.elem[i][j] = T(0);\n }\n }\n return a;\n }\n Matrix operator+(const Matrix &m) const {\n assert(H == m.H && W == m.W);\n Matrix a(H, W);\n for (int i = 0; i < H; i++) {\n for (int j = 0; j < W; j++) {\n a.elem[i][j] = this->elem[i][j] + m.elem[i][j];\n }\n }\n return a;\n }\n Matrix operator-(const Matrix &m) const {\n assert(H == m.H && W == m.W);\n Matrix a(H, W);\n for (int i = 0; i < H; i++) {\n for (int j = 0; j < W; j++) {\n a.elem[i][j] = this->elem[i][j] - m.elem[i][j];\n }\n }\n return a;\n }\n Matrix operator*(const Matrix &m) const {\n assert(W == m.H);\n Matrix a(H, m.W);\n for (int i = 0; i < H; i++) {\n for (int j = 0; j < m.W; j++) {\n for (int k = 0; k < W; k++) {\n a.elem[i][j] = a.elem[i][j] + (this->elem[i][k] * m.elem[k][j]);\n }\n }\n }\n return a;\n }\n Matrix &operator+=(const Matrix &m) { return *this = *this + m; }\n Matrix &operator-=(const Matrix &m) { return *this = *this - m; }\n Matrix &operator*=(const Matrix &m) { return *this = *this * m; }\n Matrix pow(long long x) const {\n Matrix a = identity(H);\n Matrix b = *this;\n while (x) {\n if (x & 1) a *= b;\n b *= b;\n x >>= 1;\n }\n return a;\n }\n\n T determinant() {\n Matrix m(*this);\n assert(m.W == m.H);\n T ret(1);\n for (int i = 0; i < m.H; i++) {\n int idx = -1;\n for (int j = i; j < m.H; j++) {\n if (m[j][i] != T(0)) idx = j;\n }\n if (idx == -1) return T(0);\n if (i != idx) {\n ret *= T(-1);\n swap(m[i], m[idx]);\n }\n ret *= m[i][i];\n T div = m[i][i];\n for (int j = i; j < m.W; j++) m[i][j] /= div;\n for (int j = i + 1; j < m.H; j++) {\n T a = m[j][i];\n for (int k = i; k < m.W; k++) m[j][k] -= m[i][k] * a;\n }\n }\n return ret;\n }\n\n friend ostream &operator<<(ostream &os, const Matrix &x) {\n for (int i = 0; i < x.H; i++) {\n for (int j = 0; j < x.W; j++) {\n os << x.elem[i][j];\n if (j + 1 < x.W) cout << ' ';\n }\n if (i + 1 < x.H) cout << endl;\n }\n return os;\n }\n friend istream &operator>>(istream &is, Matrix &x) {\n for (auto &v : x.elem) is >> v;\n return is;\n }\n};\n\nvoid solve() {\n int S, N, K;\n cin >> S >> N >> K;\n if (S < 0) S *= -1;\n\n auto dp = vect(K + 1, vect(N * K + 1, 0.0));\n dp[0][0] = 1.0;\n for (int i = 0; i < K; i++) {\n for (int j = 0; j <= N * K; j++) {\n for (int k = 1; k <= N; k++) {\n if (j + k <= N * K) dp[i + 1][j + k] += dp[i][j] * (1.0 / N);\n }\n }\n }\n\n auto A = vect(N * K, vect(N * K + 1, (double)0.0));\n\n A[0][0] = 1.0;\n for (int i = 1; i < N * K; i++) {\n for (int j = 1; j <= N * K; j++) { A[i][abs(i - j)] += dp[K][j]; }\n A[i][i] -= 1.0;\n A[i][N * K] = -1.0;\n }\n\n // for (auto a : A) dmp(a);\n\n auto sol = solve_equation(A);\n // dmp(sol);\n if (sol.empty()) {\n cout << -1 << endl;\n return;\n }\n\n if (S < N * K) {\n cout << sol[S] << endl;\n return;\n }\n\n Matrix<double> M(N * K + 1, N * K + 1);\n for (int i = 0; i < N * K; i++) { M[0][i] = dp[K][i + 1]; }\n M[0][N * K] = 1.0;\n for (int i = 1; i < N * K; i++) { M[i][i - 1] = 1.0; }\n M[N * K][N * K] = 1.0;\n M = M.pow(S - N * K + 1);\n\n Matrix<double> v(N * K + 1, 1);\n for (int i = 0; i < N * K; i++) { v[N * K - 1 - i][0] = sol[i]; }\n v[N * K][0] = 1.0;\n cout << (M * v)[0][0] << endl;\n\n return;\n}\n\nint main() {\n fastio();\n if (!multitest) {\n solve();\n } else {\n cerr << \"[Warning] Multi testcase mode on\" << endl;\n int t;\n cin >> t;\n while (t--) solve();\n }\n return 0;\n}", "accuracy": 0.1935483870967742, "time_ms": 100, "memory_kb": 4204, "score_of_the_acc": -0.1151, "final_rank": 17 }, { "submission_id": "aoj_2315_6079625", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define eps 1e-9\n#define INF 2000000000 // 2e9\n#define LLINF 2000000000000000000ll // 2e18 (llmax:9e18)\n#define all(x) (x).begin(), (x).end()\n#define sq(x) ((x) * (x))\n\n#define rep(i, x) for (int i = 0; i < (int)(x); i++)\n#define drep(i, x) for (int i = ((int)(x)-1); i >= 0; i--)\n\n#define popcount __builtin_popcount\n#define next __next\n#define prev __prev\n\n#ifndef LOCAL\n#define dmp(...) ;\n#else\n#define dmp(...) \\\n cerr << \"[ \" << #__VA_ARGS__ << \" ] : \" << dump_str(__VA_ARGS__) << endl\n#endif\n\n// ---------------- Utility ------------------\n\ntemplate <class T>\nbool chmin(T &a, const T &b) {\n if (a <= b) return false;\n a = b;\n return true;\n}\n\ntemplate <class T>\nbool chmax(T &a, const T &b) {\n if (a >= b) return false;\n a = b;\n return true;\n}\n\ntemplate <class T>\nusing MaxHeap = priority_queue<T>;\n\ntemplate <class T>\nusing MinHeap = priority_queue<T, vector<T>, greater<T>>;\n\ntemplate <class T>\nvector<T> vect(int len, T elem) {\n return vector<T>(len, elem);\n}\n\n// ----------------- Input -------------------\n\ntemplate <class T, class U>\nistream &operator>>(istream &is, pair<T, U> &p) {\n return is >> p.first >> p.second;\n}\n\ntemplate <class T>\nistream &operator>>(istream &is, vector<T> &vec) {\n for (int i = 0; i < vec.size(); i++) is >> vec[i];\n return is;\n}\n\n// ----------------- Output ------------------\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n return os << p.first << ',' << p.second;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const vector<T> &v) {\n for (const T &e : v) os << e << \" \";\n return os;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const deque<T> &d) {\n for (const T &e : d) os << e << \" \";\n return os;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const set<T> &s) {\n os << \"{ \";\n for (const T &e : s) os << e << \" \";\n return os << \"}\";\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const map<T, U> &m) {\n os << \"{ \";\n for (const auto &[key, val] : m) os << \"( \" << key << \" -> \" << val << \" ) \";\n return os << \"}\";\n}\n\ntemplate <class TupleTy, size_t... I>\nvoid dump_tuple(ostream &os, const TupleTy t, std::index_sequence<I...>) {\n (..., (os << (I == 0 ? \"\" : \",\") << std::get<I>(t)));\n}\n\ntemplate <class... Args>\nostream &operator<<(ostream &os, const tuple<Args...> &t) {\n os << \"(\";\n dump_tuple(os, t, std::make_index_sequence<sizeof...(Args)>());\n return os << \")\";\n}\n\nvoid dump_str_rec(ostringstream &) {}\n\ntemplate <class Head, class... Tail>\nvoid dump_str_rec(ostringstream &oss, Head &&head, Tail &&... tail) {\n oss << \", \" << head;\n dump_str_rec(oss, forward<Tail>(tail)...);\n}\n\ntemplate <class T, class... U>\nstring dump_str(T &&arg, U &&... args) {\n ostringstream oss;\n oss << arg;\n dump_str_rec(oss, forward<U>(args)...);\n return oss.str();\n}\n\n// --------------- Fast I/O ------------------\n\nvoid fastio() {\n cin.tie(0);\n ios::sync_with_stdio(0);\n cout << fixed << setprecision(20);\n}\n\n// ------------------ ACL --------------------\n\n// #include <atcoder/modint>\n// constexpr int MOD = 998244353;\n// constexpr int MOD = 1000000007;\n// using mint = atcoder::static_modint<MOD>;\n// ostream &operator<<(ostream &os, const mint &v) {\n// return os << v.val();\n// }\n\n// ------------ End of template --------------\n\n#define endl \"\\n\"\nusing ll = long long;\nusing pii = pair<int, int>;\n\nconst bool multitest = false;\n\n// N変数連立方程式(Ax=bの形)を解く\n//\n// 引数のAは拡大係数行列. (Ax=bでAとbをそのまま並べてつなげたもの)\n// すなわち\n// A[i][0] * x_0 + A[i][1] * x_1 + ... A[i][n - 1] * x_{n-1} = A[i][n]\n// (0 <= i < n)\n//\n//\n// 関数が終わったときにAの左側は単位行列になる\n// Note: 誤差に注意\n//\n// Verify:\n// https://onlinejudge.u-aizu.ac.jp/solutions/problem/1328/review/4927454/okuraofvegetable/C++17\n// https://onlinejudge.u-aizu.ac.jp/solutions/problem/2171/review/6045411/okuraofvegetable/C++17\n\nvector<double> solve_equation(vector<vector<double>> &A) {\n assert(A[0].size() == A.size() + 1);\n for (int i = 0; i < A.size(); i++) {\n if (abs(A[i][i]) < eps) {\n for (int j = i + 1; j < A.size(); j++) {\n if (abs(A[j][i]) > eps) {\n swap(A[i], A[j]);\n break;\n }\n }\n }\n double r = A[i][i];\n if (abs(r) < eps) {\n dmp(r);\n return vector<double>();\n }\n for (int j = i; j <= A.size(); j++) { A[i][j] /= r; }\n for (int j = i + 1; j < A.size(); j++) {\n double s = A[j][i];\n for (int k = i; k <= A.size(); k++) { A[j][k] -= A[i][k] * s; }\n }\n }\n for (int i = A.size() - 1; i > 0; i--) {\n for (int j = 0; j < i; j++) {\n double r = A[j][i];\n for (int k = i; k <= A.size(); k++) { A[j][k] -= A[i][k] * r; }\n }\n }\n vector<double> sol;\n for (int i = 0; i < A.size(); i++) sol.push_back(A[i][A.size()]);\n return sol;\n}\n\n// Matrix class\n//\n// constructor of T : T(1),T(0) must be unit of T\n//\n// Verified: DDCC B\n// https://atcoder.jp/contests/ddcc2020-final/submissions/9941022\n//\n// Verified: determinant\n// https://judge.yosupo.jp/submission/45201\n//\n\ntemplate <class T>\nstruct Matrix {\n int H, W;\n vector<vector<T>> elem;\n Matrix(int H, int W, T val = T(0)) : H(H), W(W) {\n elem.resize(H);\n for (int i = 0; i < H; i++) elem[i].assign(W, val);\n }\n Matrix(const vector<vector<T>> &m)\n : H(m.size()), W(m.size() ? m[0].size() : 0) {\n elem.resize(H);\n for (int i = 0; i < H; i++) elem[i].resize(W);\n for (int i = 0; i < H; i++) elem[i] = m[i];\n }\n vector<T> &operator[](int h) { return elem[h]; }\n static Matrix identity(int N) {\n Matrix a(N, N);\n for (int i = 0; i < N; i++) {\n for (int j = 0; j < N; j++) {\n if (i == j)\n a.elem[i][i] = T(1);\n else\n a.elem[i][j] = T(0);\n }\n }\n return a;\n }\n Matrix operator+(const Matrix &m) const {\n assert(H == m.H && W == m.W);\n Matrix a(H, W);\n for (int i = 0; i < H; i++) {\n for (int j = 0; j < W; j++) {\n a.elem[i][j] = this->elem[i][j] + m.elem[i][j];\n }\n }\n return a;\n }\n Matrix operator-(const Matrix &m) const {\n assert(H == m.H && W == m.W);\n Matrix a(H, W);\n for (int i = 0; i < H; i++) {\n for (int j = 0; j < W; j++) {\n a.elem[i][j] = this->elem[i][j] - m.elem[i][j];\n }\n }\n return a;\n }\n Matrix operator*(const Matrix &m) const {\n assert(W == m.H);\n Matrix a(H, m.W);\n for (int i = 0; i < H; i++) {\n for (int j = 0; j < m.W; j++) {\n for (int k = 0; k < W; k++) {\n a.elem[i][j] = a.elem[i][j] + (this->elem[i][k] * m.elem[k][j]);\n }\n }\n }\n return a;\n }\n Matrix &operator+=(const Matrix &m) { return *this = *this + m; }\n Matrix &operator-=(const Matrix &m) { return *this = *this - m; }\n Matrix &operator*=(const Matrix &m) { return *this = *this * m; }\n Matrix pow(long long x) const {\n Matrix a = identity(H);\n Matrix b = *this;\n while (x) {\n if (x & 1) a *= b;\n b *= b;\n x >>= 1;\n }\n return a;\n }\n\n T determinant() {\n Matrix m(*this);\n assert(m.W == m.H);\n T ret(1);\n for (int i = 0; i < m.H; i++) {\n int idx = -1;\n for (int j = i; j < m.H; j++) {\n if (m[j][i] != T(0)) idx = j;\n }\n if (idx == -1) return T(0);\n if (i != idx) {\n ret *= T(-1);\n swap(m[i], m[idx]);\n }\n ret *= m[i][i];\n T div = m[i][i];\n for (int j = i; j < m.W; j++) m[i][j] /= div;\n for (int j = i + 1; j < m.H; j++) {\n T a = m[j][i];\n for (int k = i; k < m.W; k++) m[j][k] -= m[i][k] * a;\n }\n }\n return ret;\n }\n\n friend ostream &operator<<(ostream &os, const Matrix &x) {\n for (int i = 0; i < x.H; i++) {\n for (int j = 0; j < x.W; j++) {\n os << x.elem[i][j];\n if (j + 1 < x.W) cout << ' ';\n }\n if (i + 1 < x.H) cout << endl;\n }\n return os;\n }\n friend istream &operator>>(istream &is, Matrix &x) {\n for (auto &v : x.elem) is >> v;\n return is;\n }\n};\n\nvoid solve() {\n int S, N, K;\n cin >> S >> N >> K;\n if (S < 0) S *= -1;\n\n auto dp = vect(K + 1, vect(N * K + 1, 0.0));\n dp[0][0] = 1.0;\n for (int i = 0; i < K; i++) {\n for (int j = 0; j <= N * K; j++) {\n for (int k = 1; k <= N; k++) {\n if (j + k <= N * K) dp[i + 1][j + k] += dp[i][j] * (1.0 / N);\n }\n }\n }\n\n auto A = vect(N * K, vect(N * K + 1, 0.0));\n\n A[0][0] = 1.0;\n for (int i = 1; i < N * K; i++) {\n for (int j = 1; j <= N * K; j++) { A[i][abs(i - j)] += dp[K][j]; }\n A[i][i] -= 1.0;\n A[i][N * K] = -1.0;\n }\n\n // for (auto a : A) dmp(a);\n\n auto sol = solve_equation(A);\n // dmp(sol);\n if (sol.empty()) {\n cout << -1 << endl;\n return;\n }\n\n if (S < N * K) {\n cout << sol[S] << endl;\n return;\n }\n\n Matrix<double> M(N * K + 1, N * K + 1);\n for (int i = 0; i < N * K; i++) { M[0][i] = dp[K][i + 1]; }\n M[0][N * K] = 1.0;\n for (int i = 1; i < N * K; i++) { M[i][i - 1] = 1.0; }\n M[N * K][N * K] = 1.0;\n M = M.pow(S - N * K + 1);\n\n Matrix<double> v(N * K + 1, 1);\n for (int i = 0; i < N * K; i++) { v[N * K - 1 - i][0] = sol[i]; }\n v[N * K][0] = 1.0;\n cout << (M * v)[0][0] << endl;\n\n return;\n}\n\nint main() {\n fastio();\n if (!multitest) {\n solve();\n } else {\n cerr << \"[Warning] Multi testcase mode on\" << endl;\n int t;\n cin >> t;\n while (t--) solve();\n }\n return 0;\n}", "accuracy": 0.1935483870967742, "time_ms": 30, "memory_kb": 3836, "score_of_the_acc": -0.0389, "final_rank": 16 }, { "submission_id": "aoj_2315_5958475", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntemplate<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }\ntemplate<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }\n#define all(x) (x).begin(),(x).end()\n#define fi first\n#define se second\n#define mp make_pair\n#define si(x) int(x.size())\nconst int mod=1000000007,MAX=105,INF=1<<29;\n\nconst double eps=1e-8;\ntypedef vector<double> vec;\ntypedef vector<vec> mat;\n\nvec gauss_jordan(const mat& A,const vec &b){\n int n=A.size();\n mat B(n,vec(n+1));\n \n for(int i=0;i<n;i++) for(int j=0;j<n;j++) B[i][j]=A[i][j];\n \n for(int i=0;i<n;i++) B[i][n]=b[i];\n \n for(int i=0;i<n;i++){\n int pivot=i;\n for(int j=i;j<n;j++){\n if(abs(B[j][i])>abs(B[pivot][i])) pivot=j;\n }\n swap(B[i],B[pivot]);\n \n if(abs(B[i][i])<eps) return vec();\n \n for(int j=i+1;j<=n;j++) B[i][j]/=B[i][i];\n for(int j=0;j<n;j++){\n if(i!=j){\n for(int k=i+1;k<=n;k++) B[j][k]-=B[j][i]*B[i][k];\n }\n }\n }\n \n vec x(n);\n \n for(int i=0;i<n;i++) x[i]=B[i][n];\n return x;\n}\n\nmat mul(mat &A, mat&B){\n mat C(A.size(),vec(B[0].size()));\n for(int i=0;i<A.size();i++){\n for(int k=0;k<B.size();k++){\n for(int j=0;j<B[0].size();j++){\n C[i][j]+=A[i][k]*B[k][j];\n }\n }\n }\n return C;\n}\n\nmat pow(mat A,ll n){\n mat B(A.size(),vec(A.size()));\n for(int i=0;i<A.size();i++){\n B[i][i]=1;\n }\n \n while(n>0){\n if(n&1) B=mul(B,A);\n A=mul(A,A);\n n/=2;\n }\n return B;\n}\n\n\ndouble dp[MAX][MAX];\n\nint main(){\n \n std::ifstream in(\"text.txt\");\n std::cin.rdbuf(in.rdbuf());\n cin.tie(0);\n ios::sync_with_stdio(false);\n \n int S,N,K;cin>>S>>N>>K;\n S=abs(S);\n if(S==0){\n cout<<0<<endl;\n return 0;\n }\n if(N==1){\n if(S%K==0) cout<<S/K<<endl;\n else cout<<-1<<endl;\n return 0;\n }\n \n dp[0][0]=1;\n for(int i=0;i<K;i++){\n for(int j=0;j<=i*N;j++){\n for(int k=1;k<=N;k++){\n dp[i+1][j+k]+=dp[i][j]/N;\n }\n }\n }\n \n mat A(N*K+1,vec(N*K+1));\n vec B(N*K+1,1);\n A[0][0]=1;\n B[0]=0;\n \n for(int i=1;i<=N*K;i++){\n A[i][i]=1;\n for(int j=1;j<=N*K;j++){\n A[i][abs(i-j)]-=dp[K][j];\n }\n }\n \n auto C=gauss_jordan(A,B);\n \n if(S<=N*K){\n cout<<fixed<<setprecision(25)<<C[S]<<endl;\n return 0;\n }\n \n mat D(N*K+1,vec(N*K+1));\n for(int j=0;j<N*K;j++) D[0][j]=dp[K][j+1];\n D[0][N*K]=1;\n for(int i=1;i<N*K;i++){\n D[i][i-1]=1;\n }\n D[N*K][N*K]=1;\n \n D=pow(D,S-1);\n \n mat E(N*K+1,vec(1));\n for(int i=0;i<N*K;i++) E[i][0]=C[N*K-i];\n E[N*K][0]=1;\n \n auto F=mul(D,E);\n \n cout<<fixed<<setprecision(25)<<F[N*K-1][0]<<endl;\n \n \n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3936, "score_of_the_acc": -0.0392, "final_rank": 5 }, { "submission_id": "aoj_2315_5875615", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nvector<vector<double>> matrix_multiplication(vector<vector<double>> &A, vector<vector<double>> &B){\n int N = A.size();\n vector<vector<double>> C(N, vector<double>(N, 0));\n for (int i = 0; i < N; i++){\n for (int j = 0; j < N; j++){\n for (int k = 0; k < N; k++){\n C[i][k] += A[i][j] * B[j][k];\n }\n }\n }\n return C;\n}\nvector<vector<double>> matrix_exponentiation(vector<vector<double>> &A, int x){\n int N = A.size();\n vector<vector<double>> B(N, vector<double>(N, 0));\n for (int i = 0; i < N; i++){\n B[i][i] = 1;\n }\n while (x > 0){\n if (x % 2 == 1){\n B = matrix_multiplication(B, A);\n }\n A = matrix_multiplication(A, A);\n x /= 2;\n }\n return B;\n}\nint main(){\n cout << fixed << setprecision(20);\n int S, N, K;\n cin >> S >> N >> K;\n if (N == 1){\n if (S % K != 0){\n cout << -1 << endl;\n } else {\n cout << abs(S) / K << endl;\n }\n } else {\n vector<vector<double>> dp(K + 1, vector<double>(N * K + 1, 0));\n dp[0][0] = 1;\n for (int i = 0; i < K; i++){\n for (int j = i; j <= i * N; j++){\n for (int k = 1; k <= N; k++){\n dp[i + 1][j + k] += dp[i][j] / N;\n }\n }\n }\n vector<vector<double>> A(N * K, vector<double>(N * K + 1, 0));\n for (int i = 0; i < N * K; i++){\n A[i][i] = 1;\n if (i > 0){\n A[i][N * K] = 1;\n for (int j = K; j <= N * K; j++){\n int i2 = abs(i - j);\n A[i][i2] -= dp[K][j];\n }\n }\n }\n for (int i = 0; i < N * K; i++){\n int p = i;\n for (int j = i + 1; j < N * K; j++){\n if (abs(A[j][i]) > abs(A[p][i])){\n p = j;\n }\n }\n swap(A[i], A[p]);\n for (int j = N * K; j >= i; j--){\n A[i][j] /= A[i][i];\n }\n for (int j = 0; j < N * K; j++){\n if (j != i){\n for (int k = N * K; k >= i; k--){\n A[j][k] -= A[j][i] * A[i][k];\n }\n }\n }\n }\n vector<double> B(N * K);\n for (int i = 0; i < N * K; i++){\n B[i] = A[i][N * K];\n }\n S = abs(S);\n if (S < N * K){\n cout << B[S] << endl;\n } else {\n vector<vector<double>> M(N * K + 1, vector<double>(N * K + 1, 0));\n for (int i = K; i <= N * K; i++){\n M[i - 1][0] = dp[K][i];\n }\n M[N * K][0] = 1;\n for (int i = 1; i < N * K; i++){\n M[i - 1][i] = 1;\n }\n M[N * K][N * K] = 1;\n M = matrix_exponentiation(M, S - N * K + 1);\n double ans = 0;\n for (int i = 0; i < N * K; i++){\n ans += M[i][0] * B[N * K - 1 - i];\n }\n ans += M[N * K][0];\n cout << ans << endl;\n }\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3772, "score_of_the_acc": -0.0272, "final_rank": 4 }, { "submission_id": "aoj_2315_5875592", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nvector<vector<double>> matrix_multiplication(vector<vector<double>> &A, vector<vector<double>> &B){\n int N = A.size();\n vector<vector<double>> C(N, vector<double>(N, 0));\n for (int i = 0; i < N; i++){\n for (int j = 0; j < N; j++){\n for (int k = 0; k < N; k++){\n C[i][k] += A[i][j] * B[j][k];\n }\n }\n }\n return C;\n}\nvector<vector<double>> matrix_exponentiation(vector<vector<double>> &A, int x){\n int N = A.size();\n vector<vector<double>> B(N, vector<double>(N, 0));\n for (int i = 0; i < N; i++){\n B[i][i] = 1;\n }\n while (x > 0){\n if (x % 2 == 1){\n B = matrix_multiplication(B, A);\n }\n A = matrix_multiplication(A, A);\n x /= 2;\n }\n return B;\n}\nint main(){\n cout << fixed << setprecision(5);\n int S, N, K;\n cin >> S >> N >> K;\n vector<vector<double>> dp(K + 1, vector<double>(N * K + 1, 0));\n dp[0][0] = 1;\n for (int i = 0; i < K; i++){\n for (int j = i; j <= i * N; j++){\n for (int k = 1; k <= N; k++){\n dp[i + 1][j + k] += dp[i][j] / N;\n }\n }\n }\n vector<vector<double>> A(N * K, vector<double>(N * K + 1, 0));\n for (int i = 0; i < N * K; i++){\n A[i][i] = 1;\n if (i > 0){\n A[i][N * K] = 1;\n for (int j = K; j <= N * K; j++){\n int i2 = abs(i - j);\n A[i][i2] -= dp[K][j];\n }\n }\n }\n for (int i = 0; i < N * K; i++){\n int p = i;\n for (int j = i + 1; j < N * K; j++){\n if (abs(A[j][i]) > abs(A[p][i])){\n p = j;\n }\n }\n swap(A[i], A[p]);\n for (int j = N * K; j >= i; j--){\n A[i][j] /= A[i][i];\n }\n for (int j = 0; j < N * K; j++){\n if (j != i){\n for (int k = N * K; k >= i; k--){\n A[j][k] -= A[j][i] * A[i][k];\n }\n }\n }\n }\n vector<double> B(N * K);\n for (int i = 0; i < N * K; i++){\n B[i] = A[i][N * K];\n }\n S = abs(S);\n if (S < N * K){\n cout << B[S] << endl;\n } else {\n vector<vector<double>> M(N * K + 1, vector<double>(N * K + 1, 0));\n for (int i = K; i <= N * K; i++){\n M[i - 1][0] = dp[K][i];\n }\n M[N * K][0] = 1;\n for (int i = 1; i < N * K; i++){\n M[i - 1][i] = 1;\n }\n M[N * K][N * K] = 1;\n M = matrix_exponentiation(M, S - N * K + 1);\n double ans = 0;\n for (int i = 0; i < N * K; i++){\n ans += M[i][0] * B[N * K - 1 - i];\n }\n ans += M[N * K][0];\n cout << ans << endl;\n }\n}", "accuracy": 0.1935483870967742, "time_ms": 20, "memory_kb": 3864, "score_of_the_acc": -0.0339, "final_rank": 15 }, { "submission_id": "aoj_2315_5868209", "code_snippet": "/*\tauthor: Kite_kuma\n\tcreated: 2021.09.10 16:11:11 */\n\n// #ifdef LOCAL\n// #define _GLIBCXX_DEBUG\n// #endif\n#include <bits/stdc++.h>\nusing namespace std;\n\n#pragma region macros\n\n#define foa(s, v) for(auto &s : v)\n#define all(v) (v).begin(), (v).end()\n#define rall(v) (v).rbegin(), (v).rend()\n#define popcnt(n) __builtin_popcountll((long long)n)\n\n#define REPname(a, b, c, d, e, ...) e\n#define rep(...) REPname(__VA_ARGS__, REP3, REP2, REP1, REP0)(__VA_ARGS__)\n#define REP0(x) for(int Counter_in_rep_macro = 0; Counter_in_rep_macro < (x); ++Counter_in_rep_macro)\n#define REP1(i, x) for(int i = 0; i < (x); ++i)\n#define REP2(i, l, r) for(int i = (l); i < (r); ++i)\n#define REP3(i, l, r, c) for(int i = (l); i < (r); i += (c))\n\n#define DREPname(a, b, c, d, e, ...) e\n#define drep(...) DREPname(__VA_ARGS__, DREP3, DREP2, DREP1)(__VA_ARGS__)\n#define DREP1(i, x) for(int i = (x)-1; i >= 0; --i)\n#define DREP2(i, l, r) for(int i = (r)-1; i >= (l); --i)\n#define DREP3(i, l, r, c) for(int i = (r)-1; i >= (l); i -= (c))\n\n#pragma endregion\n\n#pragma region aliases\n\nusing ll = long long;\nusing ld = long double;\nusing ull = unsigned long long;\nusing vi = std::vector<int>;\nusing vvi = std::vector<std::vector<int>>;\nusing vvvi = std::vector<std::vector<std::vector<int>>>;\nusing vll = std::vector<ll>;\nusing vvll = std::vector<vll>;\nusing vvvll = std::vector<vvll>;\nusing pii = std::pair<int, int>;\nusing pll = std::pair<long long, long long>;\ntemplate <class T = ll>\nusing V = std::vector<T>;\ntemplate <class T = ll>\nusing VV = V<V<T>>;\ntemplate <class T = ll>\nusing VVV = V<V<V<T>>>;\ntemplate <class T = ll>\nusing pqup = std::priority_queue<T, std::vector<T>, std::greater<T>>;\ntemplate <class T = ll>\nusing pqdn = std::priority_queue<T>;\n\n#pragma endregion\n\n#pragma region constants\n\nconst int inf = 1e9;\nconst long long INF = 1e18;\nconst long double pi = acos(-1);\nconst char dl = '\\n';\nconst char sp = ' ';\nint dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};\nint dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};\n\nconst int mod_1000000007 = 1000000007;\nconst int mod_998244353 = 998244353;\n\n#pragma endregion\n\n#pragma region basic_operation\n\ntemplate <class T0, class T1, class T2>\ninline bool in_range(T0 x, T1 lef, T2 rig) {\n\treturn ((lef <= x) && (x < rig));\n}\n\ntemplate <class T>\ninline bool chmin(T &a, T b) {\n\tif(a > b) {\n\t\ta = b;\n\t\treturn true;\n\t}\n\treturn false;\n}\ntemplate <class T>\ninline bool chmax(T &a, T b) {\n\tif(a < b) {\n\t\ta = b;\n\t\treturn true;\n\t}\n\treturn false;\n}\n\nvoid Yes(bool f = 1) { std::cout << (f ? \"Yes\" : \"No\") << '\\n'; }\nvoid No() { std::cout << \"No\\n\"; }\nvoid YES(bool f = 1) { std::cout << (f ? \"YES\" : \"NO\") << '\\n'; }\nvoid NO() { std::cout << \"NO\\n\"; }\n\ntemplate <class T>\nvoid drop(T answer) {\n\tstd::cout << answer << '\\n';\n\texit(0);\n}\n\nvoid err(bool flag = true) {\n\tif(!flag) return;\n\tstd::cout << -1 << '\\n';\n\texit(0);\n}\n\ntemplate <class T>\nvoid vout(std::vector<T> const &v, bool vertically = 0) {\n\tif(vertically) {\n\t\tfor(auto const &a : v) {\n\t\t\tstd::cout << a << '\\n';\n\t\t}\n\t\treturn;\n\t}\n\tfor(auto it = v.begin(); it != v.end(); it++) {\n\t\tstd::cout << (*it);\n\t\tif(it != v.end() - 1) {\n\t\t\tstd::cout << ' ';\n\t\t}\n\t}\n\tstd::cout << '\\n';\n\treturn;\n}\n\ninline void print() { std::cout << '\\n'; }\ntemplate <class T>\ninline void print(T x) {\n\tstd::cout << x << '\\n';\n\treturn;\n}\ntemplate <typename Head, typename... Tail>\nvoid print(Head H, Tail... T) {\n\tstd::cout << H << \" \";\n\tprint(T...);\n}\n\ntemplate <class T>\nvoid add(std::vector<T> &v, T val) {\n\tfor(auto &a : v) a += val;\n\treturn;\n}\n\ntemplate <class T>\nT dup(T a, T b) {\n\tassert(b != 0);\n\treturn (a + b - 1) / b;\n}\n\ntemplate <class T>\nT greatest_lower_multiple(T x, T d) {\n\tif(d == 0) return 0;\n\tif(d < 0) d *= -1;\n\tT y = x % d;\n\tif(y < 0) y += d;\n\treturn x - y;\n}\n\ntemplate <class T>\nT least_upper_multiple(T x, T d) {\n\treturn -greatest_lower_multiple(-x, d);\n}\n\nlong long POW(long long a, long long n) {\n\tlong long res = 1;\n\twhile(n > 0) {\n\t\tif(n & 1) res = res * a;\n\t\ta = a * a;\n\t\tn >>= 1;\n\t}\n\treturn res;\n}\n\nlong long modpow(long long a, long long n, long long mod) {\t // a^n mod\n\tassert(n >= 0 && mod);\n\tif(mod == 1) return 0LL;\n\tlong long res = 1;\n\ta %= mod;\n\twhile(n > 0) {\n\t\tif(n & 1) res = res * a % mod;\n\t\ta = a * a % mod;\n\t\tn >>= 1;\n\t}\n\treturn res < 0 ? res + mod : res;\n}\n\n// return x which satisfies a * x % mod == gcd(a, mod)\n// not (mod | a)\nlong long modinv(long long a, long long mod) {\n\tlong long b = mod, u = 1, v = 0;\n\twhile(b) {\n\t\tlong long t = a / b;\n\t\ta -= t * b;\n\t\tstd::swap(a, b);\n\t\tu -= t * v;\n\t\tstd::swap(u, v);\n\t}\n\tu %= mod;\n\tif(u < 0) u += mod;\n\treturn u;\n}\n\ntemplate <class T>\nint lb(const std::vector<T> &a, const T x) {\n\treturn std::distance(a.begin(), std::lower_bound(a.begin(), a.end(), x));\n}\ntemplate <class T>\nint ub(const std::vector<T> &a, const T x) {\n\treturn std::distance(a.begin(), std::upper_bound(a.begin(), a.end(), x));\n}\ntemplate <class T>\nvoid unq_sort(std::vector<T> &a) {\n\tstd::sort(a.begin(), a.end());\n\ta.erase(std::unique(a.begin(), a.end()), a.end());\n}\ntemplate <class T>\nstd::vector<int> press(std::vector<T> &a) {\n\tauto vec = a;\n\tunq_sort(vec);\n\tstd::vector<int> ret;\n\tfor(auto &v : a) ret.push_back(lb(vec, v));\n\treturn ret;\n}\n\n#pragma endregion\n\n#pragma region input\n#define VEC(type, name, size) \\\n\tvector<type> name(size); \\\n\tscanner::INPUT(name)\n#define VVEC(type, name, h, w) \\\n\tvector<vector<type>> name(h, vector<type>(w)); \\\n\tscanner::INPUT(name)\n#define INT(...) \\\n\tint __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define LL(...) \\\n\tlong long __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define STR(...) \\\n\tstring __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define CHR(...) \\\n\tchar __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define DBL(...) \\\n\tdouble __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define LD(...) \\\n\tlong double __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define TPL3(type0, type1, type2, name) \\\n\tstd::tuple<type0, type1, type2> name; \\\n\tscanner::INPUT(name);\n#define TPL4(type0, type1, type2, type3, name) \\\n\tstd::tuple<type0, type1, type2, type3> name; \\\n\tscanner::INPUT(name);\n\nnamespace scanner {\ntemplate <class T>\nvoid scan(T &a) {\n\tstd::cin >> a;\n}\n\ntemplate <class T, class L>\nvoid scan(std::pair<T, L> &p) {\n\tscan(p.first);\n\tscan(p.second);\n}\n\ntemplate <class T0, class T1, class T2>\nvoid scan(std::tuple<T0, T1, T2> &p) {\n\tT0 t0;\n\tT1 t1;\n\tT2 t2;\n\tscan(t0);\n\tscan(t1);\n\tscan(t2);\n\tp = std::make_tuple(t0, t1, t2);\n}\n\ntemplate <class T0, class T1, class T2, class T3>\nvoid scan(std::tuple<T0, T1, T2, T3> &p) {\n\tT0 t0;\n\tT1 t1;\n\tT2 t2;\n\tT3 t3;\n\tscan(t0);\n\tscan(t1);\n\tscan(t2);\n\tscan(t3);\n\tp = std::make_tuple(t0, t1, t2, t3);\n}\n\ntemplate <class T>\nvoid scan(std::vector<T> &a) {\n\tfor(auto &i : a) scan(i);\n}\n\nvoid INPUT() {}\ntemplate <class Head, class... Tail>\nvoid INPUT(Head &head, Tail &... tail) {\n\tscan(head);\n\tINPUT(tail...);\n}\n} // namespace scanner\n\ntemplate <typename T1, typename T2>\nstd::istream &operator>>(std::istream &is, std::pair<T1, T2> &p) {\n\tis >> p.first >> p.second;\n\treturn is;\n}\n#pragma endregion\n\n#pragma region debug\n\n#pragma region output\ntemplate <typename T1, typename T2>\nstd::ostream &std::operator<<(std::ostream &os, const std::pair<T1, T2> &p) {\n\tos << p.first << \" \" << p.second;\n\treturn os;\n}\ntemplate <class T>\nstd::ostream &std::operator<<(std::ostream &os, const std::vector<T> &v) {\n\tfor(int i = 0; i < (int)v.size(); i++) {\n\t\tif(i) os << \" \";\n\t\tos << v[i];\n\t}\n\treturn os;\n}\n#pragma endregion\n\n#pragma region view\n\nnamespace viewer {\nvoid view(const long long e) {\n\tif(e == INF)\n\t\tstd::cerr << \"INF\";\n\telse if(e == -INF)\n\t\tstd::cerr << \"-INF\";\n\telse\n\t\tstd::cerr << e;\n}\n\nvoid view(const int e) {\n\tif(e == inf)\n\t\tstd::cerr << \"inf\";\n\telse if(e == -inf)\n\t\tstd::cerr << \"-inf\";\n\telse\n\t\tstd::cerr << e;\n}\n\ntemplate <typename T>\nvoid view(const T e) {\n\tstd::cerr << e;\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::pair<T, U> &p) {\n\tstd::cerr << \"(\";\n\tview(p.first);\n\tstd::cerr << \", \";\n\tview(p.second);\n\tstd::cerr << \")\";\n}\n\ntemplate <class T0, class T1, class T2>\nvoid view(const std::tuple<T0, T1, T2> &p) {\n\tstd::cerr << \"(\";\n\tview(std::get<0>(p));\n\tstd::cerr << \", \";\n\tview(std::get<1>(p));\n\tstd::cerr << \", \";\n\tview(std::get<2>(p));\n\tstd::cerr << \")\";\n}\n\ntemplate <class T0, class T1, class T2, class T3>\nvoid view(const std::tuple<T0, T1, T2, T3> &p) {\n\tstd::cerr << \"(\";\n\tview(std::get<0>(p));\n\tstd::cerr << \", \";\n\tview(std::get<1>(p));\n\tstd::cerr << \", \";\n\tview(std::get<2>(p));\n\tstd::cerr << \", \";\n\tview(std::get<3>(p));\n\tstd::cerr << \")\";\n}\n\ntemplate <typename T>\nvoid view(const std::set<T> &s) {\n\tif(s.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << \"{ \";\n\tfor(auto &t : s) {\n\t\tview(t);\n\t\tstd::cerr << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\n\ntemplate <typename T>\nvoid view(const std::unordered_set<T> &s) {\n\tif(s.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << \"{ \";\n\tfor(auto &t : s) {\n\t\tview(t);\n\t\tstd::cerr << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\n\ntemplate <typename T>\nvoid view(const std::vector<T> &v) {\n\tif(v.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << \"{ \";\n\tfor(const auto &e : v) {\n\t\tview(e);\n\t\tstd::cerr << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\n\ntemplate <typename T>\nvoid view(const std::vector<std::vector<T>> &vv) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &v : vv) {\n\t\tstd::cerr << \"\\t\";\n\t\tview(v);\n\t\tstd::cerr << '\\n';\n\t}\n\tstd::cerr << \"}\";\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::vector<std::pair<T, U>> &v) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &c : v) {\n\t\tstd::cerr << \"\\t(\";\n\t\tview(c.first);\n\t\tstd::cerr << \", \";\n\t\tview(c.second);\n\t\tstd::cerr << \")\\n\";\n\t}\n\tstd::cerr << \"}\";\n}\n\ntemplate <class T0, class T1, class T2>\nvoid view(const std::vector<std::tuple<T0, T1, T2>> &v) {\n\tif(v.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << '{';\n\tfor(const auto &t : v) {\n\t\tstd::cerr << \"\\n\\t\";\n\t\tview(t);\n\t\tstd::cerr << \",\";\n\t}\n\tstd::cerr << \"\\n}\";\n}\n\ntemplate <class T0, class T1, class T2, class T3>\nvoid view(const std::vector<std::tuple<T0, T1, T2, T3>> &v) {\n\tif(v.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << '{';\n\tfor(const auto &t : v) {\n\t\tstd::cerr << \"\\n\\t\";\n\t\tview(t);\n\t\tstd::cerr << \",\";\n\t}\n\tstd::cerr << \"\\n}\";\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::map<T, U> &m) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &t : m) {\n\t\tstd::cerr << \"\\t[\";\n\t\tview(t.first);\n\t\tstd::cerr << \"] : \";\n\t\tview(t.second);\n\t\tstd::cerr << '\\n';\n\t}\n\tstd::cerr << \"}\";\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::unordered_map<T, U> &m) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &t : m) {\n\t\tstd::cerr << \"\\t[\";\n\t\tview(t.first);\n\t\tstd::cerr << \"] : \";\n\t\tview(t.second);\n\t\tstd::cerr << '\\n';\n\t}\n\tstd::cerr << \"}\";\n}\n} // namespace viewer\n\n#pragma endregion\n\n// when debugging : g++ foo.cpp -DLOCAL\n#ifdef LOCAL\nvoid debug_out() {}\ntemplate <typename Head, typename... Tail>\nvoid debug_out(Head H, Tail... T) {\n\tviewer::view(H);\n\tstd::cerr << \", \";\n\tdebug_out(T...);\n}\n#define debug(...) \\\n\tdo { \\\n\t\tstd::cerr << __LINE__ << \" [\" << #__VA_ARGS__ << \"] : [\"; \\\n\t\tdebug_out(__VA_ARGS__); \\\n\t\tstd::cerr << \"\\b\\b]\\n\"; \\\n\t} while(0)\n#define dump(x) \\\n\tdo { \\\n\t\tstd::cerr << __LINE__ << \" \" << #x << \" : \"; \\\n\t\tviewer::view(x); \\\n\t\tstd::cerr << '\\n'; \\\n\t} while(0)\n\n#else\n#define debug(...) (void(0))\n#define dump(x) (void(0))\n#endif\n\n#pragma endregion\n\n#pragma region geometry_light\n\nusing Real = long double;\nusing R = Real;\nusing Point = std::complex<Real>;\nusing Vec = Point;\nconst Real EPS = 1e-10, PI = acos(-1);\n\ninline bool eq(const Real &a, const Real &b) { return fabs(b - a) < EPS; }\ninline bool eq(const Point &a, const Point &b) { return fabs(b - a) < EPS; }\n/*\n\t-1: a < b\n\t0 : a == b\n\t1 : a > b\n*/\ninline int compare(const Real &a, const Real &b) { return eq(a, b) ? 0 : a < b ? -1 : 1; }\ninline int sign(const Real &a) { return fabs(a) < EPS ? 0 : a < 0 ? -1 : 1; }\n\nnamespace std {\nconst Point operator*(const Point &p, const Real &d) { return Point(real(p) * d, imag(p) * d); }\n} // namespace std\n\nstd::istream &operator>>(std::istream &is, Point &p) {\n\tReal a, b;\n\tis >> a >> b;\n\tp = Point(a, b);\n\treturn is;\n}\nstd::ostream &operator<<(std::ostream &os, Point &p) { return os << std::fixed << std::setprecision(10) << p.real() << \" \" << p.imag(); }\n\n// rotate point 'p' for counter clockwise direction\nconst Point rotate(const Real &theta, const Point &p) {\n\treturn Point(cos(theta) * p.real() - sin(theta) * p.imag(), sin(theta) * p.real() + cos(theta) * p.imag());\n}\n\nconst Real radian_to_degree(const Real &r) { return (r * 180.0 / PI); }\n\nconst Real degree_to_radian(const Real &d) { return (d * PI / 180.0); }\n\n// get angle a-b-c (<pi)\nconst Real get_angle(const Point &a, const Point &b, const Point &c) {\n\tconst Point v(a - b), w(c - b);\n\tReal theta = fabs(atan2(w.imag(), w.real()) - atan2(v.imag(), v.real()));\n\treturn std::min(theta, 2 * PI - theta);\n}\n\nnamespace std {\nbool operator<(const Point &a, const Point &b) { return a.real() != b.real() ? a.real() < b.real() : a.imag() < b.imag(); }\n} // namespace std\n\nstruct Line {\n\tPoint a, b;\n\n\tLine() = default;\n\n\tLine(Point a, Point b) : a(a), b(b) {}\n\n\tLine(Real A, Real B, Real C) // Ax + By = C\n\t{\n\t\tif(eq(A, 0))\n\t\t\ta = Point(0, C / B), b = Point(1, C / B);\n\t\telse if(eq(B, 0))\n\t\t\tb = Point(C / A, 0), b = Point(C / A, 1);\n\t\telse\n\t\t\ta = Point(0, C / B), b = Point(C / A, 0);\n\t}\n\n\tfriend std::ostream &operator<<(std::ostream &os, Line &p) { return os << p.a << \" -- \" << p.b; }\n\n\tfriend std::istream &operator>>(std::istream &is, Line &a) { return is >> a.a >> a.b; }\n};\n\nstruct Segment : Line {\n\tSegment() = default;\n\n\tSegment(Point a, Point b) : Line(a, b) {}\n};\n\nstruct Circle {\n\tPoint p;\n\tReal r;\n\n\tCircle() = default;\n\n\tCircle(Point p, Real r) : p(p), r(r) {}\n};\n\nusing Points = std::vector<Point>;\nusing Polygon = std::vector<Point>;\nusing Segments = std::vector<Segment>;\nusing Lines = std::vector<Line>;\nusing Circles = std::vector<Circle>;\n\nconst Real cross(const Point &a, const Point &b) { return real(a) * imag(b) - imag(a) * real(b); }\nconst Real dot(const Point &a, const Point &b) { return real(a) * real(b) + imag(a) * imag(b); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_C\nconst int ccw(const Point &a, Point b, Point c) {\n\tb = b - a, c = c - a;\n\tif(cross(b, c) > EPS) return +1;\t\t// \"COUNTER_CLOCKWISE\"\n\tif(cross(b, c) < -EPS) return -1;\t\t// \"CLOCKWISE\"\n\tif(dot(b, c) < -EPS) return +2;\t\t\t// \"ONLINE_BACK\"\n\tif(norm(b) + EPS < norm(c)) return -2;\t// \"ONLINE_FRONT\"\n\treturn 0;\t\t\t\t\t\t\t\t// \"ON_SEGMENT\"\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_A\nbool parallel(const Line &a, const Line &b) { return eq(cross(a.b - a.a, b.b - b.a), 0.0); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_A\nbool orthogonal(const Line &a, const Line &b) { return eq(dot(a.a - a.b, b.a - b.b), 0.0); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_A\nPoint projection(const Line &l, const Point &p) {\n\tdouble t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n\treturn l.a + (l.a - l.b) * t;\n}\n\nPoint projection(const Segment &l, const Point &p) {\n\tdouble t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n\treturn l.a + (l.a - l.b) * t;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_B\nPoint reflection(const Line &l, const Point &p) { return projection(l, p) * 2.0 - p; }\n\nint intersect(const Line &l, const Point &p) { return int(abs(ccw(l.a, l.b, p)) != 1); }\n\nint intersect(const Line &l, const Line &m) {\n\tif(intersect(l, m.a) && intersect(l, m.b)) return -1;\n\treturn int(abs(cross(l.b - l.a, m.b - m.a)) > EPS);\n}\n\nint intersect(const Segment &s, const Point &p) { return int(ccw(s.a, s.b, p) == 0); }\n\nint intersect(const Line &l, const Segment &s) {\n\tif(intersect(l, s.a) && intersect(l, s.b)) return -1;\n\treturn cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < EPS;\n}\n\nReal distance(const Line &l, const Point &p);\n\nint intersect(const Circle &c, const Line &l) {\n\tReal d = c.r - distance(l, c.p);\n\treturn fabs(d) < EPS ? 1 : d > 0. ? 2 : 0;\n}\n\nint intersect(const Circle &c, const Point &p) { return int(abs(abs(p - c.p) - c.r) < EPS); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_B\nint intersect(const Segment &s, const Segment &t) {\n\tif(eq(s.a, s.b)) return intersect(t, s.a);\n\tif(intersect(Line(s), t.a) && intersect(Line(s), t.b) &&\n\t std::max(std::min(s.a, s.b), std::min(t.a, t.b)) < std::min(std::max(s.a, s.b), std::max(t.a, t.b)))\n\t\treturn -1;\n\treturn int(ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 && ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0);\n}\n\nint intersect(const Circle &c, const Segment &l) {\n\tconst Point h = projection(l, c.p);\n\tif(norm(h - c.p) - c.r * c.r > EPS) return 0;\n\tauto d1 = abs(c.p - l.a), d2 = abs(c.p - l.b);\n\tif(compare(d1, c.r) == -1 && compare(d2, c.r) == -1) return 0;\n\tif(d1 < c.r - EPS && d2 > c.r - EPS || d1 > c.r - EPS && d2 < c.r - EPS) return 1;\n\treturn dot(l.a - h, l.b - h) < 0 ? 2 : 0;\n}\n\nReal distance(const Point &a, const Point &b);\n\nint number_of_common_tangents(const Circle &c1, const Circle &c2) {\n\tReal r1 = std::min(c1.r, c2.r), r2 = std::max(c1.r, c2.r), d = distance(c1.p, c2.p);\n\tint com = compare(r1 + r2, d);\n\treturn com == 1 ? compare(d + r1, r2) + 1 : 3 - com;\n\t// if(c1.r < c2.r) swap(c1, c2);\n\t// Real d = abs(c1.p - c2.p);\n\t// if(compare(c1.r + c2.r, d) == -1) return 4;\n\t// if(eq(c1.r + c2.r, d)) return 3;\n\t// if(compare(c1.r - c2.r, d) == -1) return 2;\n\t// return int(eq(c1.r - c2.r, d));\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_A&lang=jp\nint intersect(const Circle &c1, const Circle &c2) { return 2 - abs(2 - number_of_common_tangents(c1, c2)); }\n\nReal distance(const Point &a, const Point &b) { return abs(a - b); }\n\nReal distance(const Line &l, const Point &p) { return abs(p - projection(l, p)); }\n\nReal distance(const Line &l, const Line &m) { return intersect(l, m) ? 0 : distance(l, m.a); }\n\nReal distance(const Segment &s, const Point &p) {\n\tPoint r = projection(s, p);\n\treturn intersect(s, r) ? distance(r, p) : std::min(abs(s.a - p), abs(s.b - p));\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_D\nReal distance(const Segment &a, const Segment &b) {\n\tif(intersect(a, b)) return 0;\n\treturn std::min({distance(a, b.a), distance(a, b.b), distance(b, a.a), distance(b, a.b)});\n}\n\nReal distance(const Line &l, const Segment &s) {\n\tif(intersect(l, s)) return 0;\n\treturn std::min(distance(l, s.a), distance(l, s.b));\n}\n\nPoint crosspoint(const Line &l, const Line &m) {\n\tReal A = cross(l.b - l.a, m.b - m.a);\n\tReal B = cross(l.b - l.a, l.b - m.a);\n\tif(eq(abs(A), 0.0) && eq(abs(B), 0.0)) return m.a;\n\treturn m.a + (m.b - m.a) * B / A;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_C\nPoint crosspoint(const Segment &l, const Segment &m) { return crosspoint(Line(l), Line(m)); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_D\nstd::pair<Point, Point> crosspoint(const Circle &c, const Line l) {\n\tPoint pr = projection(l, c.p);\n\tif(eq(distance(l, c.p), c.r)) return {pr, pr};\n\tVec v = (l.b - l.a) / abs(l.b - l.a) * sqrt(c.r * c.r - norm(pr - c.p));\n\treturn make_pair(pr - v, pr + v);\n\t// Vec e = (l.b - l.a) / abs(l.b - l.a);\n\t// double base = sqrt(c.r * c.r - norm(pr - c.p));\n\t// return {pr - e * base, pr + e * base};\n}\n\nstd::pair<Point, Point> crosspoint(const Circle &c, const Segment &l) {\n\tif(intersect(c, l) == 2) return crosspoint(c, Line(l.a, l.b));\n\tauto ret = crosspoint(c, Line(l.a, l.b));\n\tif(dot(l.a - ret.first, l.b - ret.first) < EPS)\n\t\tret.second = ret.first;\n\telse\n\t\tret.first = ret.second;\n\treturn ret;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_E\nstd::pair<Point, Point> crosspoint(const Circle &c1, const Circle &c2) {\n\tReal d = abs(c1.p - c2.p);\n\tReal a = acos((c1.r * c1.r + d * d - c2.r * c2.r) / (2 * c1.r * d)); // cosine theorem\n\tReal t = atan2(c2.p.imag() - c1.p.imag(), c2.p.real() - c1.p.real());\n\tPoint p1 = c1.p + Point(cos(t + a) * c1.r, sin(t + a) * c1.r);\n\tPoint p2 = c1.p + Point(cos(t - a) * c1.r, sin(t - a) * c1.r);\n\treturn make_pair(p1, p2);\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_F\n// \"点 p を通る円 c の接線\"の接点2つ\nstd::pair<Point, Point> tangent(const Circle &c1, const Point &p2) {\n\treturn crosspoint(c1, Circle(p2, sqrt(norm(c1.p - p2) - c1.r * c1.r)));\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_G\n// 円 c1, c2 の共通接線\nLines tangent(Circle c1, Circle c2) {\n\tLines ret;\n\tif(c1.r < c2.r) std::swap(c1, c2);\n\tReal g = norm(c1.p - c2.p);\n\tif(eq(g, 0)) return ret;\n\tVec u = (c2.p - c1.p) / Real(sqrt(g));\n\tVec v = rotate(PI * 0.5, u);\n\tfor(int s : {-1, 1}) {\n\t\tReal h = (c1.r + s * c2.r) / sqrt(g);\n\t\tif(eq(1 - h * h, 0)) {\n\t\t\tret.emplace_back(c1.p + u * c1.r, c1.p + (u + v) * c1.r);\n\t\t} else if(1 - h * h > 0) {\n\t\t\tPoint uu = u * h, vv = v * sqrt(1 - h * h);\n\t\t\tret.emplace_back(c1.p + (uu + vv) * c1.r, c2.p - (uu + vv) * c2.r * s);\n\t\t\tret.emplace_back(c1.p + (uu - vv) * c1.r, c2.p - (uu - vv) * c2.r * s);\n\t\t}\n\t}\n\treturn ret;\n}\n\n#pragma endregion\n\n#pragma region matrix\n\ntemplate <typename T>\nstruct Matrix {\n\tstd::vector<std::vector<T>> A;\n\n private:\n\tstatic Matrix I(size_t n) {\n\t\tMatrix mat(n);\n\t\tfor(int i = 0; i < n; i++) mat[i][i] = 1;\n\t\treturn mat;\n\t}\n\n public:\n\tMatrix() = default;\n\n\tMatrix(std::vector<std::vector<T>> &vvec) { A = vvec; }\n\n\tMatrix(size_t n, size_t m) : A(n, std::vector<T>(m, 0)) {}\n\n\tMatrix(size_t n, size_t m, T init) : A(n, std::vector<T>(m, init)) {}\n\n\tMatrix(size_t n, std::vector<T> &vec) : A(n, vec) {}\n\n\tMatrix(size_t n) : A(n, std::vector<T>(n, 0)) {}\n\n\tint height() const { return A.size(); }\n\n\tint width() const { return A[0].size(); }\n\n\tinline const std::vector<T> &operator[](int k) const { return A[k]; }\n\n\tinline std::vector<T> &operator[](int k) { return A[k]; }\n\n\tMatrix &operator+=(const Matrix &B) {\n\t\tsize_t n = height(), m = width();\n\t\tassert(n == B.height() && m == B.width());\n\t\tfor(int i = 0; i < n; i++)\n\t\t\tfor(int j = 0; j < m; j++) (*this)[i][j] += B[i][j];\n\t\treturn *this;\n\t}\n\n\tMatrix &operator-=(const Matrix &B) {\n\t\tsize_t n = height(), m = width();\n\t\tassert(n == B.height() && m == B.width());\n\t\tfor(int i = 0; i < n; i++)\n\t\t\tfor(int j = 0; j < m; j++) (*this)[i][j] -= B[i][j];\n\t\treturn *this;\n\t}\n\n\tMatrix &operator*=(const Matrix &B) {\n\t\tsize_t n = height(), m = B.width(), p = width();\n\t\tassert(p == B.height());\n\t\tstd::vector<std::vector<T>> C(n, std::vector<T>(m, 0));\n\t\tfor(int i = 0; i < n; i++)\n\t\t\tfor(int j = 0; j < m; j++)\n\t\t\t\tfor(int k = 0; k < p; k++) C[i][j] += (*this)[i][k] * B[k][j];\n\t\tA.swap(C);\n\t\treturn (*this);\n\t}\n\n\tMatrix &operator^=(long long k) {\n\t\tMatrix B = Matrix::I(height());\n\t\twhile(k > 0) {\n\t\t\tif(k & 1) B *= (*this);\n\t\t\t*this *= *this;\n\t\t\tk >>= 1;\n\t\t}\n\t\tA.swap(B.A);\n\t\treturn *this;\n\t}\n\n\tbool operator==(const Matrix &B) {\n\t\tsize_t n = height(), m = width();\n\t\tif(n != B.height() or m != B.width()) return false;\n\t\tfor(int i = 0; i < n; i++)\n\t\t\tfor(int j = 0; j < m; j++)\n\t\t\t\tif((*this)[i][j] != B[i][j]) return false;\n\t\treturn true;\n\t}\n\n\tMatrix operator+(const Matrix &B) const { return (Matrix(*this) += B); }\n\n\tMatrix operator-(const Matrix &B) const { return (Matrix(*this) -= B); }\n\n\tMatrix operator*(const Matrix &B) const { return (Matrix(*this) *= B); }\n\n\tMatrix operator^(const long long k) const { return (Matrix(*this) ^= k); }\n\n\tMatrix &operator+=(const T &t) {\n\t\tint n = height(), m = width();\n\t\tfor(int i = 0; i < n; i++)\n\t\t\tfor(int j = 0; j < m; j++) (*this)[i][j] += t;\n\t\treturn *this;\n\t}\n\n\tMatrix &operator-=(const T &t) {\n\t\tint n = height(), m = width();\n\t\tfor(int i = 0; i < n; i++)\n\t\t\tfor(int j = 0; j < m; j++) (*this)[i][j] -= t;\n\t\treturn *this;\n\t}\n\n\tMatrix &operator*=(const T &t) {\n\t\tint n = height(), m = width();\n\t\tfor(int i = 0; i < n; i++)\n\t\t\tfor(int j = 0; j < m; j++) (*this)[i][j] *= t;\n\t\treturn *this;\n\t}\n\n\tMatrix &operator/=(const T &t) {\n\t\tint n = height(), m = width();\n\t\tfor(int i = 0; i < n; i++)\n\t\t\tfor(int j = 0; j < m; j++) (*this)[i][j] /= t;\n\t\treturn *this;\n\t}\n\n\tMatrix operator+(const T &t) const { return (Matrix(*this) += t); }\n\n\tMatrix operator-(const T &t) const { return (Matrix(*this) -= t); }\n\n\tMatrix operator*(const T &t) const { return (Matrix(*this) *= t); }\n\n\tMatrix operator/(const T &t) const { return (Matrix(*this) /= t); }\n\n\tfriend std::ostream &operator<<(std::ostream &os, Matrix &p) {\n\t\tsize_t n = p.height(), m = p.width();\n\t\tfor(int i = 0; i < n; i++) {\n\t\t\tos << '[';\n\t\t\tfor(int j = 0; j < m; j++) os << p[i][j] << (j == m - 1 ? \"]\\n\" : \",\");\n\t\t}\n\t\treturn (os);\n\t}\n\n\tT determinant() {\n\t\tMatrix B(*this);\n\t\tsize_t n = height(), m = width();\n\t\tassert(n == m);\n\t\tT ret = 1;\n\t\tfor(int i = 0; i < n; i++) {\n\t\t\tint idx = -1;\n\t\t\tfor(int j = i; j < n; j++)\n\t\t\t\tif(B[j][i] != 0) idx = j;\n\t\t\tif(idx == -1) return 0;\n\t\t\tif(i != idx) {\n\t\t\t\tret *= -1;\n\t\t\t\tswap(B[i], B[idx]);\n\t\t\t}\n\t\t\tret *= B[i][i];\n\t\t\tT vv = B[i][i];\n\t\t\tfor(int j = 0; j < n; j++) B[i][j] /= vv;\n\t\t\tfor(int j = i + 1; j < n; j++) {\n\t\t\t\tT a = B[j][i];\n\t\t\t\tfor(int k = 0; k < n; k++) {\n\t\t\t\t\tB[j][k] -= B[i][k] * a;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn ret;\n\t}\n};\n\n#pragma endregion\n\nld solve(ll s) {\n\ts = abs(s);\n\tint n, k;\n\tcin >> n >> k;\n\tif(!s) return 0;\n\tif(n == 1) {\n\t\tif(s % k != 0) return -1;\n\t\treturn s / k;\n\t}\n\n\tint jump_max = n * k;\n\n\tV<ld> jump_prob(jump_max + 1);\n\tjump_prob[0] = 1.;\n\n\trep(k) {\n\t\tauto tmp = jump_prob;\n\t\ttmp.assign(jump_max + 1, 0);\n\t\trep(from, jump_max + 1) {\n\t\t\trep(leap_len, 1, n + 1) {\n\t\t\t\tif(from + leap_len <= jump_max) {\n\t\t\t\t\ttmp[from + leap_len] += jump_prob[from] / n;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tswap(tmp, jump_prob);\n\t}\n\n\tVV<ld> mat(jump_max, V<ld>(jump_max));\n\n\trep(from, 1, jump_max) {\n\t\trep(leap_len, 1, jump_max + 1) {\n\t\t\tint to = abs(from - leap_len);\n\t\t\tif(to) {\n\t\t\t\tauto &tmp = mat[from][to];\n\t\t\t\ttmp += jump_prob[leap_len];\n\t\t\t}\n\t\t}\n\t}\n\n\trep(i, 1, jump_max) rep(j, 1, jump_max) { mat[i][j] = bool(i == j) - mat[i][j]; }\n\n\tmat.erase(mat.begin());\n\tfoa(t, mat) t.erase(t.begin());\n\n\tV<ld> res(jump_max - 1, 1.);\n\n\tint len = jump_max - 1;\n\n\tld EPS = 1e-5;\n\tauto nonzero = [&](ld a) { return abs(a) > EPS; };\n\n\tauto mult_row = [&](int row_idx, ld mult) {\n\t\trep(j, len) { mat[row_idx][j] *= mult; }\n\t\tres[row_idx] *= mult;\n\t\treturn;\n\t};\n\n\tauto make_one = [&](int row_idx, int col_idx) {\n\t\tassert(mat[row_idx][col_idx] != 0);\n\t\tmult_row(row_idx, 1. / mat[row_idx][col_idx]);\n\t\tmat[row_idx][col_idx] = 1;\n\t\treturn;\n\t};\n\n\tauto add_mult = [&](int row_from, int row_to, ld mult) {\n\t\trep(j, len) { mat[row_to][j] += mat[row_from][j] * mult; }\n\t\tres[row_to] += res[row_from] * mult;\n\t\treturn;\n\t};\n\n\tauto swap_row = [&](int i, int j) {\n\t\tswap(mat[i], mat[j]);\n\t\tswap(res[i], res[j]);\n\t\treturn;\n\t};\n\n\tauto find_nonzero = [&](int col_idx, int row_start) {\n\t\trep(i, row_start, len) {\n\t\t\tif(nonzero(mat[i][col_idx])) return i;\n\t\t}\n\t\treturn -1;\n\t};\n\n\tdebug(len);\n\trep(col, len) {\n\t\tauto idx = find_nonzero(col, col);\n\t\tif(idx < 0) {\n\t\t\tcontinue;\n\t\t}\n\t\tif(idx != col) swap_row(idx, col);\n\t\tmake_one(col, col);\n\t\trep(row, len) {\n\t\t\tif(col != row) {\n\t\t\t\tadd_mult(col, row, -mat[row][col]);\n\t\t\t}\n\t\t}\n\t}\n\n\tdebug(res);\n\n\tres.insert(res.begin(), 0.);\n\tres.insert(res.begin(), 1.);\n\n\tif(s < n * k) return res[s + 1];\n\tMatrix<ld> a(n * k + 1);\n\n\trep(i, 1, n * k) { a[i][i + 1] = 1; }\n\ta[0][0] = 1;\n\ta[n * k][0] = 1;\n\trep(i, 1, n * k + 1) { a[n * k][i] = jump_prob[n * k - i + 1]; }\n\n\tdebug(jump_prob);\n\tMatrix<ld> b(n * k + 1, 1);\n\trep(i, n * k + 1) b[i][0] = res[i];\n\n\tdebug(a.A, b.A, s);\n\n\ta ^= s;\n\n\tdebug(a.A);\n\ta *= b;\n\n\tdebug(a.A);\n\n\treturn a[1][0];\n\n\t// return;\n}\n\nint main() {\n\tstd::ios::sync_with_stdio(false);\n\tstd::cin.tie(nullptr);\n\tstd::cout << std::fixed << std::setprecision(15);\n\tsrand((unsigned)time(NULL));\n\n\tint n;\n\twhile(cin >> n) {\n\t\tld res = solve((ll)n);\n\t\tif(res < -0.5)\n\t\t\tcout << \"-1\" << dl;\n\t\telse\n\t\t\tcout << res << dl;\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 90, "memory_kb": 3760, "score_of_the_acc": -0.0756, "final_rank": 6 }, { "submission_id": "aoj_2315_5454161", "code_snippet": "#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"unroll-loops\")\n#include<iostream>\n#include<string>\n#include<cstdio>\n#include<vector>\n#include<cmath>\n#include<algorithm>\n#include<functional>\n#include<iomanip>\n#include<queue>\n#include<ciso646>\n#include<random>\n#include<map>\n#include<set>\n#include<bitset>\n#include<stack>\n#include<unordered_map>\n#include<unordered_set>\n#include<utility>\n#include<cassert>\n#include<complex>\n#include<numeric>\n#include<array>\nusing namespace std;\n\n//#define int long long\ntypedef long long ll;\n\ntypedef unsigned long long ul;\ntypedef unsigned int ui;\nconstexpr ll mod = 1000000007;\nconst ll INF = mod * mod;\ntypedef pair<int, int>P;\n#define stop char nyaa;cin>>nyaa;\n#define rep(i,n) for(int i=0;i<n;i++)\n#define per(i,n) for(int i=n-1;i>=0;i--)\n#define Rep(i,sta,n) for(int i=sta;i<n;i++)\n#define rep1(i,n) for(int i=1;i<=n;i++)\n#define per1(i,n) for(int i=n;i>=1;i--)\n#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)\n#define all(v) (v).begin(),(v).end()\ntypedef pair<ll, ll> LP;\ntypedef long double ld;\ntypedef pair<ld, ld> LDP;\nconst ld eps = 1e-12;\nconst ld pi = acosl(-1.0);\n\nll mod_pow(ll x, ll n, ll m = mod) {\n\tif (n < 0) {\n\t\tll res = mod_pow(x, -n, m);\n\t\treturn mod_pow(res, m - 2, m);\n\t}\n\tif (abs(x) >= m)x %= m;\n\tif (x < 0)x += m;\n\tll res = 1;\n\twhile (n) {\n\t\tif (n & 1)res = res * x % m;\n\t\tx = x * x % m; n >>= 1;\n\t}\n\treturn res;\n}\nstruct modint {\n\tll n;\n\tmodint() :n(0) { ; }\n\tmodint(ll m) :n(m) {\n\t\tif (n >= mod)n %= mod;\n\t\telse if (n < 0)n = (n % mod + mod) % mod;\n\t}\n\toperator int() { return n; }\n};\nbool operator==(modint a, modint b) { return a.n == b.n; }\nmodint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }\nmodint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }\nmodint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }\nmodint operator+(modint a, modint b) { return a += b; }\nmodint operator-(modint a, modint b) { return a -= b; }\nmodint operator*(modint a, modint b) { return a *= b; }\nmodint operator^(modint a, ll n) {\n\tif (n == 0)return modint(1);\n\tmodint res = (a * a) ^ (n / 2);\n\tif (n % 2)res = res * a;\n\treturn res;\n}\n\nll inv(ll a, ll p) {\n\treturn (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);\n}\nmodint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }\nmodint operator/=(modint& a, modint b) { a = a / b; return a; }\nconst int max_n = 1 << 1;\nmodint fact[max_n], factinv[max_n];\nvoid init_f() {\n\tfact[0] = modint(1);\n\tfor (int i = 0; i < max_n - 1; i++) {\n\t\tfact[i + 1] = fact[i] * modint(i + 1);\n\t}\n\tfactinv[max_n - 1] = modint(1) / fact[max_n - 1];\n\tfor (int i = max_n - 2; i >= 0; i--) {\n\t\tfactinv[i] = factinv[i + 1] * modint(i + 1);\n\t}\n}\nmodint comb(int a, int b) {\n\tif (a < 0 || b < 0 || a < b)return 0;\n\treturn fact[a] * factinv[b] * factinv[a - b];\n}\nmodint combP(int a, int b) {\n\tif (a < 0 || b < 0 || a < b)return 0;\n\treturn fact[a] * factinv[a - b];\n}\n\ntypedef long double Data;\ntypedef vector<Data> Array;\ntypedef vector<Array> mat;\n\nbool is_zero(Data dat) { return abs(dat) < eps; }\nmat operator-(mat a) {\n\trep(i, a.size())rep(j, a[0].size())a[i][j] = -a[i][j];\n\treturn a;\n}\nmat operator+(mat lhs, mat& rhs) {\n\trep(i, lhs.size())rep(j, lhs[0].size())lhs[i][j] += rhs[i][j];\n\treturn lhs;\n}\nmat operator-(mat lhs, mat& rhs) {\n\trep(i, lhs.size())rep(j, lhs[0].size())lhs[i][j] -= rhs[i][j];\n\treturn lhs;\n}\nmat operator*(const mat& lhs, const mat& rhs) {\n\tmat ret(lhs.size(), Array(rhs[0].size(), 0));\n\trep(i, lhs.size())rep(j, rhs[0].size())rep(k, rhs.size()) {\n\t\tret[i][j] += lhs[i][k] * rhs[k][j];\n\t}\n\treturn ret;\n}\n//no verify\nint rankMat(mat a) {\n\tconst int n = a.size(), m = a[0].size();\n\tint r = 0;\n\tfor (int i = 0; r < n && i < m; ++i) {\n\t\tint pivot = r;\n\t\tfor (int j = r + 1; j < n; ++j)\n\t\t\tif (abs(a[j][i]) > abs(a[pivot][i]))pivot = j;\n\t\tswap(a[pivot], a[r]);\n\t\tif (is_zero(a[r][i]))continue;\n\t\tfor (int k = m - 1; k >= i; --k)\n\t\t\ta[r][k] = a[r][k] / a[r][i];\n\t\tfor (int j = r + 1; j < n; ++j) {\n\t\t\tfor (int k = m - 1; k >= i; --k) {\n\t\t\t\ta[j][k] = fma(-a[r][k], a[j][i], a[j][k]);\n\t\t\t}\n\t\t}\n\t\t++r;\n\t}\n\treturn r;\n}\nmat scalar(int sz, Data k) {\n\tmat ret(sz, Array(sz, 0));\n\trep(i, sz)ret[i][i] = k;\n\treturn ret;\n}\n//行列累乗\nmat operator^(const mat& lhs, const ll n) {\n\tif (n == 0)return scalar(lhs.size(), 1);\n\tmat ret = (lhs * lhs) ^ (n / 2);\n\tif (n % 2) {\n\t\tret = ret * lhs;\n\t}\n\treturn ret;\n}\n\nData det(mat a) {\n\tconst int n = a.size();\n\tData D = Data(1);\n\tfor (int i = 0; i < n; ++i) {\n\t\tint pivot = i;\n\t\tfor (int j = i + 1; j < n; ++j) {\n\t\t\tif (abs(a[j][i]) > abs(a[pivot][i]))pivot = j;\n\t\t}\n\t\tswap(a[pivot], a[i]);\n\t\tD = D * a[i][i] * Data(i != pivot ? -1 : 1);\n\t\tif (is_zero(a[i][i]))break;\n\t\tfor (int j = i + 1; j < n; ++j) {\n\t\t\tfor (int k = n - 1; k >= i; --k) {\n\t\t\t\ta[j][k] = a[j][k] - a[i][k] * a[j][i] / a[i][i];\n\t\t\t}\n\t\t}\n\t}\n\treturn D;\n}\n\npair<mat, vector<int>> LUPDecomposition(mat a) {\n\tint n = a.size();\n\tvector<int> perm(n);\n\tiota(begin(perm), end(perm), 0);\n\trep(i, n) {\n\t\tint pivot = i;\n\t\tfor (int j = i + 1; j < n; ++j)\n\t\t\tif (abs(a[j][i]) > abs(a[pivot][i]))pivot = j;\n\t\tswap(a[pivot], a[i]);\n\t\tswap(perm[pivot], perm[i]);\n\t\tfor (int j = i + 1; j < n; ++j) {\n\t\t\ta[j][i] /= a[i][i];\n\t\t\tfor (int k = i + 1; k < n; ++k) {\n\t\t\t\ta[j][k] -= a[i][k] * a[j][i];\n\t\t\t}\n\t\t}\n\t}\n\treturn make_pair(a, perm);\n}\nArray LUPBackSubstitution(mat& LU, vector<int>& perm, Array a) {\n\tint n = LU.size();\n\tArray tmp(n);\n\trep(i, n)tmp[i] = a[perm[i]];\n\tswap(tmp, a);\n\trep(i, n) {\n\t\trep(j, i) {\n\t\t\ta[i] -= a[j] * LU[i][j];\n\t\t}\n\t}\n\tfor (int i = n - 1; i >= 0; --i) {\n\t\tfor (int j = i + 1; j < n; ++j) {\n\t\t\ta[i] -= a[j] * LU[i][j];\n\t\t}\n\t\ta[i] /= LU[i][i];\n\t}\n\treturn a;\n}\n\n//Ax=bのxを返す\nArray calc(mat A, Array b) {\n\tpair<mat, vector<int>> p = LUPDecomposition(A);\n\treturn LUPBackSubstitution(p.first, p.second, b);\n}\n\nld dp[11][101];\nvoid solve() {\n\tint x; cin >> x;\n\tint n, k; cin >> k >> n;\n\tif (k == 1) {\n\t\tx = abs(x);\n\t\tif (x % n == 0) {\n\t\t\tcout << x / n << \"\\n\";\n\t\t}\n\t\telse {\n\t\t\tcout << -1 << \"\\n\";\n\t\t}\n\t\treturn;\n\t}\n\tdp[0][0] = 1;\n\trep(i, n) {\n\t\trep(j, 10 * i + 1) {\n\t\t\trep1(t, k) {\n\t\t\t\tdp[i + 1][j + t] += dp[i][j];\n\t\t\t}\n\t\t}\n\t}\n\tld b = 1; rep(i, n)b *= k;\n\trep(i, 101)dp[n][i] /= b;\n\t//for (int c = n; c <= n * k; c++)cout << dp[n][c] << \"\\n\";\n\tmat A(n * k, vector<ld>(n * k));\n\tvector<ld> ori(n * k, 1);\n\trep1(i, n * k) {\n\t\tA[i - 1][i - 1] += 1;\n\t\tfor (int c = n; c <= n * k; c++) {\n\t\t\tint t = i - c;\n\t\t\tt = abs(t);\n\t\t\tif (t > 0) {\n\t\t\t\tA[i - 1][t - 1] -= dp[n][c];\n\t\t\t}\n\t\t}\n\t}\n\tvector<ld> z = calc(A, ori);\n\tz.insert(z.begin(), 0);\n\tx = abs(x);\n\tif (x < z.size()) {\n\t\tcout << z[x] << \"\\n\"; return;\n\t}\n\tmat B(n * k + 1, vector<ld>(n * k + 1));\n\trep1(i, n * k - 1) {\n\t\tB[i][i - 1] = 1;\n\t}\n\tfor (int c = n; c <= n * k; c++) {\n\t\tB[0][c - 1] = dp[n][c];\n\t}\n\tB[0][n * k] = 1;\n\tB[n * k][n * k] = 1;\n\tB = B ^ (int)(x - z.size() + 1);\n\treverse(all(z));\n\tz[n * k] = 1;\n\tld ans = 0;\n\trep(i, n * k + 1) {\n\t\tans += B[0][i] * z[i];\n\t}\n\n\tcout << ans << \"\\n\";\n}\n\n\nsigned main() {\n\tios::sync_with_stdio(false);\n\tcin.tie(0);\n\tcout << fixed << setprecision(12);\n\t//init_f();\n\t//init();\n\t//expr();\n\t//int t; cin >> t;rep(i,t)\n\t//while(cin>>n,n)\n\tsolve();\n\treturn 0;\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 8644, "score_of_the_acc": -0.4255, "final_rank": 10 }, { "submission_id": "aoj_2315_5144211", "code_snippet": "#include <iostream>\n#include <cstdio>\n#include <cmath>\n#include <ctime>\n#include <cstdlib>\n#include <cassert>\n#include <vector>\n#include <list>\n#include <stack>\n#include <queue>\n#include <deque>\n#include <map>\n#include <set>\n#include <bitset>\n#include <string>\n#include <algorithm>\n#include <utility>\n#include <complex>\n#define rep(x, s, t) for(llint (x) = (s); (x) <= (t); (x)++)\n#define chmin(x, y) (x) = min((x), (y))\n#define chmax(x, y) (x) = max((x), (y))\n#define all(x) (x).begin(),(x).end()\n#define inf 1e18\n\nusing namespace std;\n\ntypedef long long llint;\ntypedef long long ll;\ntypedef pair<llint, llint> P;\n\nstruct Matrix{\n\tint h, w;\n\tvector<double> mat;\n\tMatrix(){\n\t\th = w = 0;\n\t}\n\tMatrix(int h, int w){\n\t\tthis->h = h, this->w = w;\n\t\tmat.resize(h*w);\n\t}\n\tdouble& at(int i, int j){\n\t\treturn mat[w*(i-1)+(j-1)];\n\t}\n\tstatic Matrix ident(int size){\n\t\tMatrix ret(size, size);\n\t\tfor(int i = 1; i <= size; i++) ret.at(i, i) = 1;\n\t\treturn ret;\n\t}\n\tMatrix operator*(Matrix& ope){\n\t\tMatrix ret(h, ope.w);\n\t\tfor(int i = 1; i <= h; i++){\n\t\t\tfor(int j = 1; j <= ope.w; j++){\n\t\t\t\tfor(int k = 1; k <= w; k++){\n\t\t\t\t\tret.at(i, j) += at(i, k) * ope.at(k, j);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn ret;\n\t}\n\tMatrix pow(llint n){\n\t\tif(n == 0) return ident(h);\n\t\tif(n % 2){\n\t\t\treturn pow(n-1) * (*this);\n\t\t}\n\t\telse{\n\t\t\tMatrix tmp = pow(n/2);\n\t\t\treturn tmp * tmp;\n\t\t}\n\t}\n\tvoid print(){\n\t\tfor(int i = 1; i <= h; i++){\n\t\t\tfor(int j = 1; j <= w; j++){\n\t\t\t\tcout << at(i, j) << \" \";\n\t\t\t}\n\t\t\tcout << endl;\n\t\t}\n\t}\n};\n\nll s, n, k;\ndouble p[105];\nll dp[15][105];\ndouble a[105][105];\nMatrix mat;\n\n\nvoid swap(double mat[105][105], int i, int j, int n)\n{\n\tfor(int k = 0; k <= n+1; k++){\n\t\tdouble t = mat[i][k];\n\t\tmat[i][k] = mat[j][k];\n\t\tmat[j][k] = t;\n\t}\n}\n\nvoid GaussianElimination(double mat[105][105], int n)\n{\n\tfor(int i = 0; i <= n; i++){\n\t\tdouble max_val = 0; int max_j;\n\t\tfor(int j = i; j <= n; j++){\n\t\t\tif(fabs(mat[j][i]) > max_val){\n\t\t\t\tmax_val = fabs(mat[j][i]);\n\t\t\t\tmax_j = j;\n\t\t\t}\n\t\t}\n\t\tassert(fabs(max_val) > 1e-9);\n\t\tswap(mat, i, max_j, n);\n\t\t\n\t\tdouble div = mat[i][i];\n\t\tfor(int j = 0; j <= n+1; j++){\n\t\t\tmat[i][j] /= div;\n\t\t}\n\t\tfor(int j = 0; j <= n; j++){\n\t\t\tdouble coe = mat[j][i];\n\t\t\tif(i == j) continue;\n\t\t\tfor(int k = 0; k <= n+1; k++){\n\t\t\t\tmat[j][k] -= coe * mat[i][k];\n\t\t\t}\n\t\t}\n\t}\n}\n\nint main(void)\n{\n\tcin >> s >> n >> k;\n\ts = abs(s); ll m = n*k;\n\t\n\tif(n == 1){\n\t\tif(s % k){\n\t\t\tcout << -1 << endl;\n\t\t\treturn 0;\n\t\t}\n\t\ts /= k, k = 1;\n\t}\n\t\n\tdp[0][0] = 1;\n\trep(i, 0, k-1){\n\t\trep(j, 0, m){\n\t\t\trep(l, 1, n){\n\t\t\t\tif(j+l <= m) dp[i+1][j+l] += dp[i][j];\n\t\t\t}\n\t\t}\n\t}\n\tll mul = 1;\n\trep(i, 1, k) mul *= n;\n\trep(j, 1, m) p[j] = (double)dp[k][j] / (double)mul;\n\t\n\trep(i, 1, m){\n\t\ta[i][i] += 1;\n\t\trep(j, 1, m) a[i][abs(i-j)] -= p[j];\n\t\ta[i][m+1] = 1;\n\t}\n\ta[0][0] = 1;\n\t\n\tGaussianElimination(a, m);\n\t\n\tmat = Matrix(m+2, m+2);\n\trep(i, 1, m) mat.at(1, i) = p[i];\n\tmat.at(1, m+2) = 1;\n\trep(i, 1, m) mat.at(i+1, i) = 1;\n\tmat.at(m+2, m+2) = 1;\n\t\n\tif(s <= m){\n\t\tprintf(\"%.11f\\n\", a[s][m+1]);\n\t\treturn 0;\n\t}\n\t\t\n\tMatrix pmat = mat.pow(s-m), vec(m+2, 1);\n\trep(i, 0, m) vec.at(i+1, 1) = a[m-i][m+1];\n\tvec.at(m+2, 1) = 1;\n\t\n\tvec = pmat * vec;\n\tprintf(\"%.11f\\n\", vec.at(1, 1));\n\t\n\treturn 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3492, "score_of_the_acc": -0.0138, "final_rank": 3 }, { "submission_id": "aoj_2315_5144210", "code_snippet": "#include <iostream>\n#include <cstdio>\n#include <cmath>\n#include <ctime>\n#include <cstdlib>\n#include <cassert>\n#include <vector>\n#include <list>\n#include <stack>\n#include <queue>\n#include <deque>\n#include <map>\n#include <set>\n#include <bitset>\n#include <string>\n#include <algorithm>\n#include <utility>\n#include <complex>\n#define rep(x, s, t) for(llint (x) = (s); (x) <= (t); (x)++)\n#define chmin(x, y) (x) = min((x), (y))\n#define chmax(x, y) (x) = max((x), (y))\n#define all(x) (x).begin(),(x).end()\n#define inf 1e18\n\nusing namespace std;\n\ntypedef long long llint;\ntypedef long long ll;\ntypedef pair<llint, llint> P;\n\nstruct Matrix{\n\tint h, w;\n\tvector<double> mat;\n\tMatrix(){\n\t\th = w = 0;\n\t}\n\tMatrix(int h, int w){\n\t\tthis->h = h, this->w = w;\n\t\tmat.resize(h*w);\n\t}\n\tdouble& at(int i, int j){\n\t\treturn mat[w*(i-1)+(j-1)];\n\t}\n\tstatic Matrix ident(int size){\n\t\tMatrix ret(size, size);\n\t\tfor(int i = 1; i <= size; i++) ret.at(i, i) = 1;\n\t\treturn ret;\n\t}\n\tMatrix operator*(Matrix& ope){\n\t\tMatrix ret(h, ope.w);\n\t\tfor(int i = 1; i <= h; i++){\n\t\t\tfor(int j = 1; j <= ope.w; j++){\n\t\t\t\tfor(int k = 1; k <= w; k++){\n\t\t\t\t\tret.at(i, j) += at(i, k) * ope.at(k, j);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn ret;\n\t}\n\tMatrix pow(llint n){\n\t\tif(n == 0) return ident(h);\n\t\tif(n % 2){\n\t\t\treturn pow(n-1) * (*this);\n\t\t}\n\t\telse{\n\t\t\tMatrix tmp = pow(n/2);\n\t\t\treturn tmp * tmp;\n\t\t}\n\t}\n\tvoid print(){\n\t\tfor(int i = 1; i <= h; i++){\n\t\t\tfor(int j = 1; j <= w; j++){\n\t\t\t\tcout << at(i, j) << \" \";\n\t\t\t}\n\t\t\tcout << endl;\n\t\t}\n\t}\n};\n\nll s, n, k;\ndouble p[105];\nll dp[15][105];\ndouble a[105][105];\nMatrix mat;\n\n\nvoid swap(double mat[105][105], int i, int j, int n)\n{\n\tfor(int k = 0; k <= n+1; k++){\n\t\tdouble t = mat[i][k];\n\t\tmat[i][k] = mat[j][k];\n\t\tmat[j][k] = t;\n\t}\n}\n\nvoid GaussianElimination(double mat[105][105], int n)\n{\n\tfor(int i = 0; i <= n; i++){\n\t\tdouble max_val = 0; int max_j;\n\t\tfor(int j = i; j <= n; j++){\n\t\t\tif(fabs(mat[j][i]) > max_val){\n\t\t\t\tmax_val = fabs(mat[j][i]);\n\t\t\t\tmax_j = j;\n\t\t\t}\n\t\t}\n\t\tassert(fabs(max_val) > 1e-9);\n\t\tswap(mat, i, max_j, n);\n\t\t\n\t\tdouble div = mat[i][i];\n\t\tfor(int j = 0; j <= n+1; j++){\n\t\t\tmat[i][j] /= div;\n\t\t}\n\t\tfor(int j = 0; j <= n; j++){\n\t\t\tdouble coe = mat[j][i];\n\t\t\tif(i == j) continue;\n\t\t\tfor(int k = 0; k <= n+1; k++){\n\t\t\t\tmat[j][k] -= coe * mat[i][k];\n\t\t\t}\n\t\t}\n\t}\n}\n\nint main(void)\n{\n\tcin >> s >> n >> k;\n\ts = abs(s); ll m = n*k;\n\t\n\tif(n == 1){\n\t\tif(s % k){\n\t\t\tcout << -1 << endl;\n\t\t\treturn 0;\n\t\t}\n\t\ts /= k, k = 1;\n\t}\n\t\n\tdp[0][0] = 1;\n\trep(i, 0, k-1){\n\t\trep(j, 0, m){\n\t\t\trep(l, 1, n){\n\t\t\t\tif(j+l <= m) dp[i+1][j+l] += dp[i][j];\n\t\t\t}\n\t\t}\n\t}\n\tll mul = 1;\n\trep(i, 1, k) mul *= n;\n\trep(j, 1, m) p[j] = (double)dp[k][j] / (double)mul;\n\t\n\trep(i, 1, m){\n\t\ta[i][i] += 1;\n\t\trep(j, 1, m) a[i][abs(i-j)] -= p[j];\n\t\ta[i][m+1] = 1;\n\t}\n\ta[0][0] = 1;\n\t\n\t/*rep(i, 0, m){\n\t\trep(j, 0, m+1){\n\t\t\tcout << a[i][j] << \" \";\n\t\t}\n\t\tcout << endl;\n\t}*/\n\t\n\tGaussianElimination(a, m);\n\t\n\tmat = Matrix(m+2, m+2);\n\trep(i, 1, m) mat.at(1, i) = p[i];\n\tmat.at(1, m+2) = 1;\n\trep(i, 1, m) mat.at(i+1, i) = 1;\n\tmat.at(m+2, m+2) = 1;\n\t\n\tif(s <= m){\n\t\tprintf(\"%.11f\\n\", a[s][m+1]);\n\t\treturn 0;\n\t}\n\t\t\n\tMatrix pmat = mat.pow(s-m), vec(m+2, 1);\n\trep(i, 0, m) vec.at(i+1, 1) = a[m-i][m+1];\n\tvec.at(m+2, 1) = 1;\n\t\n\tvec = pmat * vec;\n\tprintf(\"%.11f\\n\", vec.at(1, 1));\n\t\n\treturn 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3432, "score_of_the_acc": -0.0094, "final_rank": 1 }, { "submission_id": "aoj_2315_5144181", "code_snippet": "#include <iostream>\n#include <cstdio>\n#include <cmath>\n#include <ctime>\n#include <cstdlib>\n#include <cassert>\n#include <vector>\n#include <list>\n#include <stack>\n#include <queue>\n#include <deque>\n#include <map>\n#include <set>\n#include <bitset>\n#include <string>\n#include <algorithm>\n#include <utility>\n#include <complex>\n#define rep(x, s, t) for(llint (x) = (s); (x) <= (t); (x)++)\n#define chmin(x, y) (x) = min((x), (y))\n#define chmax(x, y) (x) = max((x), (y))\n#define all(x) (x).begin(),(x).end()\n#define inf 1e18\n#define double long double\n\nusing namespace std;\n\ntypedef long long llint;\ntypedef long long ll;\ntypedef pair<llint, llint> P;\n\nstruct Matrix{\n\tint h, w;\n\tvector<double> mat;\n\tMatrix(){\n\t\th = w = 0;\n\t}\n\tMatrix(int h, int w){\n\t\tthis->h = h, this->w = w;\n\t\tmat.resize(h*w);\n\t}\n\tdouble& at(int i, int j){\n\t\treturn mat[w*(i-1)+(j-1)];\n\t}\n\tstatic Matrix ident(int size){\n\t\tMatrix ret(size, size);\n\t\tfor(int i = 1; i <= size; i++) ret.at(i, i) = 1;\n\t\treturn ret;\n\t}\n\tMatrix operator*(Matrix& ope){\n\t\tMatrix ret(h, ope.w);\n\t\tfor(int i = 1; i <= h; i++){\n\t\t\tfor(int j = 1; j <= ope.w; j++){\n\t\t\t\tfor(int k = 1; k <= w; k++){\n\t\t\t\t\tret.at(i, j) += at(i, k) * ope.at(k, j);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn ret;\n\t}\n\tMatrix pow(llint n){\n\t\tif(n == 0) return ident(h);\n\t\tif(n % 2){\n\t\t\treturn pow(n-1) * (*this);\n\t\t}\n\t\telse{\n\t\t\tMatrix tmp = pow(n/2);\n\t\t\treturn tmp * tmp;\n\t\t}\n\t}\n\tvoid print(){\n\t\tfor(int i = 1; i <= h; i++){\n\t\t\tfor(int j = 1; j <= w; j++){\n\t\t\t\tcout << at(i, j) << \" \";\n\t\t\t}\n\t\t\tcout << endl;\n\t\t}\n\t}\n};\n\nll s, n, k;\ndouble p[105];\nll dp[15][105];\ndouble a[105][105];\nMatrix mat;\n\n\nvoid swap(double mat[105][105], int i, int j, int n)\n{\n\tfor(int k = 0; k <= n+1; k++){\n\t\tdouble t = mat[i][k];\n\t\tmat[i][k] = mat[j][k];\n\t\tmat[j][k] = t;\n\t}\n}\n\nvoid GaussianElimination(double mat[105][105], int n)\n{\n\tfor(int i = 0; i <= n; i++){\n\t\tdouble max_val = 0; int max_j;\n\t\tfor(int j = i; j <= n; j++){\n\t\t\tif(fabs(mat[j][i]) > max_val){\n\t\t\t\tmax_val = fabs(mat[j][i]);\n\t\t\t\tmax_j = j;\n\t\t\t}\n\t\t}\n\t\tassert(fabs(max_val) > 1e-9);\n\t\tswap(mat, i, max_j, n);\n\t\t\n\t\tdouble div = mat[i][i];\n\t\tfor(int j = 0; j <= n+1; j++){\n\t\t\tmat[i][j] /= div;\n\t\t}\n\t\tfor(int j = 0; j <= n; j++){\n\t\t\tdouble coe = mat[j][i];\n\t\t\tif(i == j) continue;\n\t\t\tfor(int k = 0; k <= n+1; k++){\n\t\t\t\tmat[j][k] -= coe * mat[i][k];\n\t\t\t}\n\t\t}\n\t}\n}\n\nint main(void)\n{\n\tcin >> s >> n >> k;\n\ts = abs(s);\n\tif((n == 1 || k == 1) && s % (n*k)){\n\t\tcout << -1 << endl;\n\t\treturn 0;\n\t}\n\tll m = n*k;\n\t\n\tdp[0][0] = 1;\n\trep(i, 0, k-1){\n\t\trep(j, 0, m){\n\t\t\trep(l, 1, n){\n\t\t\t\tif(j+l <= m) dp[i+1][j+l] += dp[i][j];\n\t\t\t}\n\t\t}\n\t}\n\tll mul = 1;\n\trep(i, 1, k) mul *= n;\n\trep(j, 1, m) p[j] = (double)dp[k][j] / (double)mul;\n\t\n\trep(i, 1, m){\n\t\ta[i][i] += 1;\n\t\trep(j, 1, m) a[i][abs(i-j)] -= p[j];\n\t\ta[i][m+1] = 1;\n\t}\n\ta[0][0] = 1;\n\t\n\tGaussianElimination(a, m);\n\t\n\tmat = Matrix(m+2, m+2);\n\trep(i, 1, m) mat.at(1, i) = p[i];\n\tmat.at(1, m+2) = 1;\n\trep(i, 1, m) mat.at(i+1, i) = 1;\n\tmat.at(m+2, m+2) = 1;\n\t\n\tif(s <= m){\n\t\tprintf(\"%.11Lf\\n\", a[s][m+1]);\n\t\treturn 0;\n\t}\n\t\t\n\tMatrix pmat = mat.pow(s-m), vec(m+2, 1);\n\trep(i, 0, m) vec.at(i+1, 1) = a[m-i][m+1];\n\tvec.at(m+2, 1) = 1;\n\t\n\tvec = pmat * vec;\n\tprintf(\"%.11Lf\\n\", vec.at(1, 1));\n\t\n\treturn 0;\n}", "accuracy": 0.0967741935483871, "time_ms": 80, "memory_kb": 3400, "score_of_the_acc": -0.0423, "final_rank": 20 }, { "submission_id": "aoj_2315_5144178", "code_snippet": "#include <iostream>\n#include <cstdio>\n#include <cmath>\n#include <ctime>\n#include <cstdlib>\n#include <cassert>\n#include <vector>\n#include <list>\n#include <stack>\n#include <queue>\n#include <deque>\n#include <map>\n#include <set>\n#include <bitset>\n#include <string>\n#include <algorithm>\n#include <utility>\n#include <complex>\n#define rep(x, s, t) for(llint (x) = (s); (x) <= (t); (x)++)\n#define chmin(x, y) (x) = min((x), (y))\n#define chmax(x, y) (x) = max((x), (y))\n#define all(x) (x).begin(),(x).end()\n#define inf 1e18\n\nusing namespace std;\n\ntypedef long long llint;\ntypedef long long ll;\ntypedef pair<llint, llint> P;\n\nstruct Matrix{\n\tint h, w;\n\tvector<double> mat;\n\tMatrix(){\n\t\th = w = 0;\n\t}\n\tMatrix(int h, int w){\n\t\tthis->h = h, this->w = w;\n\t\tmat.resize(h*w);\n\t}\n\tdouble& at(int i, int j){\n\t\treturn mat[w*(i-1)+(j-1)];\n\t}\n\tstatic Matrix ident(int size){\n\t\tMatrix ret(size, size);\n\t\tfor(int i = 1; i <= size; i++) ret.at(i, i) = 1;\n\t\treturn ret;\n\t}\n\tMatrix operator*(Matrix& ope){\n\t\tMatrix ret(h, ope.w);\n\t\tfor(int i = 1; i <= h; i++){\n\t\t\tfor(int j = 1; j <= ope.w; j++){\n\t\t\t\tfor(int k = 1; k <= w; k++){\n\t\t\t\t\tret.at(i, j) += at(i, k) * ope.at(k, j);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn ret;\n\t}\n\tMatrix pow(llint n){\n\t\tif(n == 0) return ident(h);\n\t\tif(n % 2){\n\t\t\treturn pow(n-1) * (*this);\n\t\t}\n\t\telse{\n\t\t\tMatrix tmp = pow(n/2);\n\t\t\treturn tmp * tmp;\n\t\t}\n\t}\n\tvoid print(){\n\t\tfor(int i = 1; i <= h; i++){\n\t\t\tfor(int j = 1; j <= w; j++){\n\t\t\t\tcout << at(i, j) << \" \";\n\t\t\t}\n\t\t\tcout << endl;\n\t\t}\n\t}\n};\n\nll s, n, k;\ndouble p[105];\nll dp[15][105];\ndouble a[105][105];\nMatrix mat;\n\n\nvoid swap(double mat[105][105], int i, int j, int n)\n{\n\tfor(int k = 0; k <= n+1; k++){\n\t\tdouble t = mat[i][k];\n\t\tmat[i][k] = mat[j][k];\n\t\tmat[j][k] = t;\n\t}\n}\n\nvoid GaussianElimination(double mat[105][105], int n)\n{\n\tfor(int i = 0; i <= n; i++){\n\t\tdouble max_val = 0; int max_j;\n\t\tfor(int j = i; j <= n; j++){\n\t\t\tif(fabs(mat[j][i]) > max_val){\n\t\t\t\tmax_val = fabs(mat[j][i]);\n\t\t\t\tmax_j = j;\n\t\t\t}\n\t\t}\n\t\tassert(fabs(max_val) > 1e-9);\n\t\tswap(mat, i, max_j, n);\n\t\t\n\t\tdouble div = mat[i][i];\n\t\tfor(int j = 0; j <= n+1; j++){\n\t\t\tmat[i][j] /= div;\n\t\t}\n\t\tfor(int j = 0; j <= n; j++){\n\t\t\tdouble coe = mat[j][i];\n\t\t\tif(i == j) continue;\n\t\t\tfor(int k = 0; k <= n+1; k++){\n\t\t\t\tmat[j][k] -= coe * mat[i][k];\n\t\t\t}\n\t\t}\n\t}\n}\n\nint main(void)\n{\n\tcin >> s >> n >> k;\n\ts = abs(s);\n\tif((n == 1 || k == 1) && s % (n*k)){\n\t\tcout << -1 << endl;\n\t\treturn 0;\n\t}\n\tll m = n*k;\n\t\n\tdp[0][0] = 1;\n\trep(i, 0, k-1){\n\t\trep(j, 0, m){\n\t\t\trep(l, 1, n){\n\t\t\t\tif(j+l <= m) dp[i+1][j+l] += dp[i][j];\n\t\t\t}\n\t\t}\n\t}\n\tll mul = 1;\n\trep(i, 1, k) mul *= n;\n\trep(j, 1, m) p[j] = (double)dp[k][j] / (double)mul;\n\t\n\trep(i, 1, m){\n\t\ta[i][i] += 1;\n\t\trep(j, 1, m) a[i][abs(i-j)] -= p[j];\n\t\ta[i][m+1] = 1;\n\t}\n\ta[0][0] = 1;\n\t\n\tGaussianElimination(a, m);\n\t\n\tmat = Matrix(m+2, m+2);\n\trep(i, 1, m) mat.at(1, i) = p[i];\n\tmat.at(1, m+2) = 1;\n\trep(i, 1, m) mat.at(i+1, i) = 1;\n\tmat.at(m+2, m+2) = 1;\n\t\n\tif(s <= m){\n\t\tprintf(\"%.11f\\n\", a[s][m+1]);\n\t\treturn 0;\n\t}\n\t\t\n\tMatrix pmat = mat.pow(s-m), vec(m+2, 1);\n\trep(i, 0, m) vec.at(i+1, 1) = a[m-i][m+1];\n\tvec.at(m+2, 1) = 1;\n\t\n\tvec = pmat * vec;\n\tprintf(\"%.11f\\n\", vec.at(1, 1));\n\t\n\treturn 0;\n}", "accuracy": 0.0967741935483871, "time_ms": 20, "memory_kb": 3416, "score_of_the_acc": -0.0012, "final_rank": 19 }, { "submission_id": "aoj_2315_3623248", "code_snippet": "// #define _GLIBCXX_DEBUG // for STL debug (optional)\n#include <iostream>\n#include <iomanip>\n#include <cstdio>\n#include <string>\n#include <cstring>\n#include <deque>\n#include <list>\n#include <queue>\n#include <stack>\n#include <vector>\n#include <utility>\n#include <algorithm>\n#include <map>\n#include <set>\n#include <complex>\n#include <cmath>\n#include <limits>\n#include <cfloat>\n#include <climits>\n#include <ctime>\n#include <cassert>\n#include <numeric>\n#include <fstream>\n#include <functional>\n#include <bitset>\nusing namespace std;\n\n#define debug(...) fprintf(stderr, __VA_ARGS__)\n#define int long long int\n \ntemplate<typename T> void chmax(T &a, T b) {a = max(a, b);}\ntemplate<typename T> void chmin(T &a, T b) {a = min(a, b);}\ntemplate<typename T> void chadd(T &a, T b) {a = a + b;}\n \ntypedef pair<int, int> pii;\ntypedef long long ll;\n \nint dx[] = {0, 0, 1, -1};\nint dy[] = {1, -1, 0, 0};\nconst ll INF = 1001001001001001LL;\nconst ll MOD = 1000000007LL;\n\n// 行列ライブラリ\n// Verified: TopCoder SRM 704 Div.2 (ModEquationEasy)\n// 行列の積 と 累乗(繰り返し二乗法)\n\n// Matrix Library Begin (C++11)\n\ntemplate <typename T>\nusing Matrix = vector< vector<T> >;\n\ntemplate <typename T>\nvoid init_mat(Matrix<T> &A, int h, int w) {\n A.resize(h, vector<T>(w, 0));\n}\n\ntemplate <typename T>\nMatrix<T> calc_mat(Matrix<T> A, Matrix<T> B) {\n Matrix<T> C(A.size(), vector<T>(B[0].size()));\n for(int i=0; i<A.size(); i++) {\n for(int k=0; k<B.size(); k++) {\n for(int j=0; j<B[0].size(); j++) {\n C[i][j] += A[i][k] * B[k][j]; // modなし\n // C[i][j] = (C[i][j] + A[i][k] * B[k][j]) % MOD; // modあり\n }\n }\n }\n return C;\n}\n\ntemplate <typename T>\nMatrix<T> mat_pow(Matrix<T> A, ll n) {\n Matrix<T> B(A.size(), vector<T>(A.size()));\n for(int i=0; i<A.size(); i++) B[i][i] = 1;\n while(n > 0) {\n if(n & 1) B = calc_mat(B, A);\n A = calc_mat(A, A);\n n >>= 1;\n }\n return B;\n}\n\n// calculate vector x which satisfies Ax = b\n// (A is N*N Matrix, b, x is N-dim vector)\n\ntemplate <typename T>\nusing Matrix = vector< vector<T> >;\nconstexpr long double EPS = 1e-8;\n\ntemplate <typename T>\nvector<long double> gauss_jordan(const Matrix<T> &A, const vector<T> &b) {\n int N = A.size();\n // B = [A b]\n Matrix<long double> B(N, vector<long double>(N+1));\n for(int i=0; i<N; i++) {\n for(int j=0; j<=N; j++) {\n if(j < N) B[i][j] = A[i][j];\n else B[i][j] = b[i];\n }\n }\n\n for(int i=0; i<N; i++) {\n int pivot = i;\n for(int j=i+1; j<N; j++) {\n if(abs(B[j][i]) > abs(B[pivot][i])) pivot = j;\n }\n if(abs(B[pivot][i]) < EPS) continue;\n swap(B[i], B[pivot]);\n\n // pivot is zero -> No solution or Not unique\n // assert(abs(B[i][i]) >= EPS);\n for(int j=i+1; j<=N; j++) B[i][j] /= B[i][i];\n for(int j=0; j<N; j++) {\n if(i != j) {\n for(int k=i+1; k<=N; k++) B[j][k] -= B[j][i] * B[i][k];\n }\n }\n }\n vector<long double> x(N);\n for(int i=0; i<N; i++) x[i] = B[i][N];\n return x;\n}\n\nlong double d[11][110], dice[110], dp[110];\nsigned main() {\n int S, N, K; cin >> S >> N >> K;\n S = abs(S);\n\n if(N == 1 and S % K != 0) {\n cout << -1 << endl;\n return 0;\n }\n\n d[0][0] = 1.0;\n for(int i=0; i<K; i++) {\n for(int j=i*N; j>=0; j--) {\n for(int k=N; k>=1; k--) {\n d[i+1][j+k] += d[i][j] / N;\n }\n }\n }\n\n int M = N*K;\n long double sum = 0.0;\n for(int i=0; i<=M; i++) {\n dice[i] = d[K][i];\n sum += d[K][i];\n }\n \n Matrix<long double> mat;\n vector<long double> b(M+1);\n init_mat(mat, M+1, M+1);\n for(int i=0; i<=M; i++) {\n mat[i][i] = 1.0;\n if(i > 0) {\n for(int j=0; j<=M; j++) {\n long double p = dice[j];\n int idx = abs(i - j);\n mat[i][idx] -= p;\n }\n b[i] = 1.0;\n }\n }\n auto res = gauss_jordan(mat, b);\n\n if(S <= M) {\n printf(\"%.12Lf\\n\", (long double)res[S]);\n }\n else {\n long double ten = 1.0;\n \n int rem = S - M;\n Matrix<long double> tab;\n init_mat(tab, M+1, M+1);\n for(int i=0; i<M; i++) {\n tab[0][i] = dice[i+1];\n if(i+1 < M) tab[i+1][i] = 1.0;\n }\n tab[0][M] = 1.0;\n tab[M][M] = 1.0;\n\n Matrix<long double> vec;\n init_mat(vec, M+1, 1);\n for(int i=0; i<M; i++) {\n vec[i][0] = res[M-i] * ten;\n }\n vec[M][0] = ten;\n\n tab = mat_pow(tab, rem);\n vec = calc_mat(tab, vec);\n printf(\"%.12Lf\\n\", (long double)vec[0][0]);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 120, "memory_kb": 4104, "score_of_the_acc": -0.1219, "final_rank": 9 }, { "submission_id": "aoj_2315_3530765", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\n\nnamespace ProconLib{\n \n template<typename T>\n struct Detail{\n static constexpr T EPS = T(1e-15);\n static bool isZero(T v){\n return abs(v)<=EPS;\n }\n };\n\n template<typename T>\n class Vector{\n int N;\n std::vector<T> dat;\n public:\n Vector(int n):N(n),dat(n){}\n Vector(int n,T x):N(n),dat(n,x){}\n Vector(std::vector<T> vec):N(vec.size()),dat(vec){}\n Vector(const std::vector<T>& vec):N(vec.size()),dat(vec){}\n Vector(const Vector& vec){dat=vec.dat;}\n int size(){return N;}\n T& operator[](int pos){return dat[pos];};\n const T& operator[](int pos) const{return dat[pos];};\n T& at(int pos){return dat.at(pos);}\n const T& at(int pos) const {return dat.at(pos);}\n };\n \n template<typename T>\n class Matrix{\n int r,c;\n std::vector<Vector<T>> dat;\n public:\n Matrix(int r,int c):r(r),c(c),dat(r,Vector<T>(c)){}\n Matrix(int r,int c,T x):r(r),c(c),dat(r,Vector<T>(c,x)){}\n\n Vector<T>& operator[](int pos){return dat[pos];};\n const Vector<T>& operator[](int pos) const{return dat[pos];};\n Vector<T>& at(int pos){return dat.at(pos);}\n const Vector<T>& at(int pos) const {return dat.at(pos);}\n int rowSize() const {return r;}\n int colSize() const {return c;}\n };\n template<typename T>\n Matrix<T> operator+(const Matrix<T>& lhs,const Matrix<T>& rhs);\n template<typename T>\n Matrix<T> operator-(const Matrix<T>& lhs,const Matrix<T>& rhs);\n template<typename T>\n Matrix<T> operator+(const Matrix<T>& mat);\n template<typename T>\n Matrix<T> operator-(const Matrix<T>& mat);\n template<typename T>\n Matrix<T> operator*(const Matrix<T>& lhs,const Matrix<T> &rhs);\n template<typename T>\n Matrix<T> Identity(int n);\n template<typename T>\n Matrix<T> pow(const Matrix<T>& mat,int k);\n template<typename T,typename Detail=Detail<T>>\n Matrix<T> gaussianElimination(Matrix<T> mat);\n template<typename T>\n int rank(const Matrix<T>& mat);\n template<typename T>\n Matrix<T> inv(const Matrix<T>& mat);\n \n template<typename T>\n Matrix<T> operator+(const Matrix<T>& lhs,const Matrix<T>& rhs){\n assert(lhs.rowSize()==rhs.rowSize() && lhs.colSize()==rhs.colSize());\n int r=lhs.rowSize(),c=lhs.colSize();\n Matrix<T> res(r,c);\n for(int i=0;i<r;i++){\n for(int j=0;j<c;j++){\n res[i][j]=lhs[i]+rhs[i];\n }\n }\n return res;\n }\n template<typename T>\n Matrix<T> operator-(const Matrix<T>& lhs,const Matrix<T>& rhs){\n assert(lhs.rowSize()==rhs.rowSize() && lhs.colSize()==rhs.colSize());\n int r=lhs.rowSize(),c=lhs.colSize();\n Matrix<T> res(r,c);\n for(int i=0;i<r;i++){\n for(int j=0;j<c;j++){\n res[i][j]=lhs[i]-rhs[i];\n }\n }\n return res;\n }\n template<typename T>\n Matrix<T> operator+(const Matrix<T>& mat){\n int r=mat.rowSize(),c=mat.colSize();\n Matrix<T> res(r,c);\n for(int i=0;i<r;i++)for(int j=0;j<c;j++) res[i][j]=-mat[i][j];\n return res;\n }\n template<typename T>\n Matrix<T> operator-(const Matrix<T>& mat){\n int r=mat.rowSize(),c=mat.colSize();\n Matrix<T> res(r,c);\n for(int i=0;i<r;i++)for(int j=0;j<c;j++) res[i][j]=-mat[i][j];\n return res;\n }\n \n template<typename T>\n Matrix<T> operator*(const Matrix<T>& lhs,const Matrix<T> &rhs){\n assert(lhs.colSize()==rhs.rowSize());\n int r=lhs.rowSize(),c=rhs.colSize(),l=lhs.colSize();\n Matrix<T> res(r,c);\n for(int i=0;i<r;i++){\n for(int k=0;k<l;k++){\n for(int j=0;j<c;j++){\n res[i][j]+=lhs[i][k]*rhs[k][j];\n }\n }\n }\n return res;\n }\n\n template<typename T>\n Matrix<T> Identity(int n){\n assert(n>=0);\n Matrix<T> res(n,n);\n for(int i=0;i<n;i++){\n res[i][i]=1;\n }\n return res;\n }\n\n template<typename T>\n Matrix<T> pow(const Matrix<T>& mat,int k){\n assert(mat.rowSize()==mat.colSize());\n Matrix<T> x=mat;\n Matrix<T> res=Identity<T>(mat.rowSize());\n while(k){\n if(k&1) res=res*x;\n x=x*x;\n k>>=1;\n }\n return res;\n }\n \n template<typename T,typename Detail=Detail<T>>\n Matrix<T> gaussianElimination(Matrix<T> mat){\n int m=mat.rowSize(),n=mat.colSize();\n int row=0;\n for(int j=0;j<n;j++){\n if(row==m) break;\n int tar=-1;\n T v=Detail::EPS;\n for(int i=row;i<m;i++){\n if(!Detail::isZero(mat[i][j]) && abs(v)<abs(mat[i][j])){\n tar=i;\n v=mat[i][j];\n }\n }\n if(tar==-1) continue;\n if(row!=tar){ \n auto tmp=mat[row];\n mat[row]=mat[tar];\n mat[tar]=tmp;\n }\n for(int i=row+1;i<m;i++){\n if(!Detail::isZero(mat[i][j])){\n T r=mat[i][j]/mat[row][j];\n for(int k=j;k<n;k++){\n mat[i][k]-=r*mat[row][k];\n }\n }\n }\n row++;\n }\n return mat;\n }\n\n template<typename T,typename Detail=Detail<T>>\n int rank(const Matrix<T>& mat){\n auto tmp=gaussialElimination(mat);\n int m=tmp.rowSize(),n=tmp.colSize();\n int i=0,j=0;\n while(i<m && j<n){\n if(Detail::isZero(tmp[i][j])) j++;\n else i++,j++;\n }\n return i;\n }\n\n template<typename T>\n Matrix<T> inv(const Matrix<T>& mat){\n assert(mat.rowSize()==mat.colSize());\n int n=mat.rowSize();\n Matrix<T> tmp(n,2*n);\n for(int i=0;i<n;i++){\n for(int j=0;j<n;j++){\n tmp[i][j]=mat[i][j];\n tmp[i][j+n]=0;\n }\n tmp[i][i+n]=1;\n }\n mat=gaussianElimination(mat);\n Matrix<T> res(n);\n for(int i=0;i<n;i++){\n for(int j=0;j<n;j++){\n res[i][j]=mat[i][j+n]/=mat[i][i];\n }\n }\n return res;\n }\n \n template<typename T>\n void debug(Matrix<T> mat){\n int m=mat.rowSize(),n=mat.colSize();\n std::cerr<<\"###Matrix_Debug###\"<<std::endl;\n for(int i=0;i<m;i++){\n for(int j=0;j<n;j++){\n std::cerr<<mat[i][j]<<\" \";\n }\n std::cerr<<std::endl;\n }\n }\n}\n\nusing namespace ProconLib;\n\nint main(){\n int s,n,k;\n cin>>s>>n>>k;\n if(n==1){\n cout<<(s%k==0 ? abs(s)/k : -1)<<endl;\n return 0;\n }\n \n int sz=n*k+1;\n \n vector<vector<double>> dp(k+1,vector<double>(sz));\n dp[0][0]=1;\n for(int i=0;i<k;i++){\n for(int j=0;j<sz;j++){\n for(int k=1;k<=n;k++){\n if(j+k<sz) dp[i+1][j+k]+=dp[i][j]/n;\n }\n }\n }\n \n Matrix<double> B(sz,sz+1);\n {\n B[0][0]=1;\n B[0][sz]=0;\n for(int i=1;i<sz;i++){\n B[i][i]=-1;\n for(int j=0;j<sz;j++){\n // i to j\n int d=i-j;\n if(0<=d && d<sz){\n B[i][j]+=dp[k][d];\n B[i][sz]-=dp[k][d];\n }\n d=i+j;\n if(j!=0 && 0<=d && d<sz){\n B[i][j]+=dp[k][d];\n B[i][sz]-=dp[k][d];\n }\n }\n }\n }\n B=gaussianElimination(B);\n Vector<double> vec(sz+1);\n for(int i=sz-1;i>=0;i--){\n for(int j=0;j<i;j++){\n B[j][sz]-=B[j][i]/B[i][i]*B[i][sz];\n B[j][i]=0;\n }\n }\n for(int i=0;i<sz;i++){\n vec[sz-i-1]=B[i][sz]/B[i][i];\n }\n vec[sz]=1;\n\n Matrix<double> A(sz+1,sz+1);\n for(int i=1;i<sz;i++){\n A[i][i-1]=1;\n }\n A[sz][sz]=1;\n for(int i=0;i<sz;i++){\n int d=i+1;\n if(d<sz) A[0][i]=dp[k][d];\n if(d<sz) A[0][sz]+=dp[k][d];\n }\n A=pow(A,abs(s));\n double res=0;\n for(int i=0;i<=sz;i++){\n res+=A[sz-1][i]*vec[i];\n }\n cout<<fixed<<setprecision(10);\n cout<<res<<endl;\n\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3548, "score_of_the_acc": -0.0108, "final_rank": 2 }, { "submission_id": "aoj_2315_3530762", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\n\nnamespace ProconLib{\n \n template<typename T>\n struct Detail{\n static constexpr T EPS = T(1e-15);\n static bool isZero(T v){\n return abs(v)<=EPS;\n }\n };\n\n template<typename T>\n class Vector{\n int N;\n std::vector<T> dat;\n public:\n Vector(int n):N(n),dat(n){}\n Vector(int n,T x):N(n),dat(n,x){}\n Vector(std::vector<T> vec):N(vec.size()),dat(vec){}\n Vector(const std::vector<T>& vec):N(vec.size()),dat(vec){}\n Vector(const Vector& vec){dat=vec.dat;}\n int size(){return N;}\n T& operator[](int pos){return dat[pos];};\n const T& operator[](int pos) const{return dat[pos];};\n T& at(int pos){return dat.at(pos);}\n const T& at(int pos) const {return dat.at(pos);}\n };\n \n template<typename T>\n class Matrix{\n int r,c;\n std::vector<Vector<T>> dat;\n public:\n Matrix(int r,int c):r(r),c(c),dat(r,Vector<T>(c)){}\n Matrix(int r,int c,T x):r(r),c(c),dat(r,Vector<T>(c,x)){}\n\n Vector<T>& operator[](int pos){return dat[pos];};\n const Vector<T>& operator[](int pos) const{return dat[pos];};\n Vector<T>& at(int pos){return dat.at(pos);}\n const Vector<T>& at(int pos) const {return dat.at(pos);}\n int rowSize() const {return r;}\n int colSize() const {return c;}\n };\n template<typename T>\n Matrix<T> operator+(const Matrix<T>& lhs,const Matrix<T>& rhs);\n template<typename T>\n Matrix<T> operator-(const Matrix<T>& lhs,const Matrix<T>& rhs);\n template<typename T>\n Matrix<T> operator+(const Matrix<T>& mat);\n template<typename T>\n Matrix<T> operator-(const Matrix<T>& mat);\n template<typename T>\n Matrix<T> operator*(const Matrix<T>& lhs,const Matrix<T> &rhs);\n template<typename T>\n Matrix<T> Identity(int n);\n template<typename T>\n Matrix<T> pow(const Matrix<T>& mat,int k);\n template<typename T,typename Detail=Detail<T>>\n Matrix<T> gaussianElimination(Matrix<T> mat);\n template<typename T>\n int rank(const Matrix<T>& mat);\n template<typename T>\n Matrix<T> inv(const Matrix<T>& mat);\n \n template<typename T>\n Matrix<T> operator+(const Matrix<T>& lhs,const Matrix<T>& rhs){\n assert(lhs.rowSize()==rhs.rowSize() && lhs.colSize()==rhs.colSize());\n int r=lhs.rowSize(),c=lhs.colSize();\n Matrix<T> res(r,c);\n for(int i=0;i<r;i++){\n for(int j=0;j<c;j++){\n res[i][j]=lhs[i]+rhs[i];\n }\n }\n return res;\n }\n template<typename T>\n Matrix<T> operator-(const Matrix<T>& lhs,const Matrix<T>& rhs){\n assert(lhs.rowSize()==rhs.rowSize() && lhs.colSize()==rhs.colSize());\n int r=lhs.rowSize(),c=lhs.colSize();\n Matrix<T> res(r,c);\n for(int i=0;i<r;i++){\n for(int j=0;j<c;j++){\n res[i][j]=lhs[i]-rhs[i];\n }\n }\n return res;\n }\n template<typename T>\n Matrix<T> operator+(const Matrix<T>& mat){\n int r=mat.rowSize(),c=mat.colSize();\n Matrix<T> res(r,c);\n for(int i=0;i<r;i++)for(int j=0;j<c;j++) res[i][j]=-mat[i][j];\n return res;\n }\n template<typename T>\n Matrix<T> operator-(const Matrix<T>& mat){\n int r=mat.rowSize(),c=mat.colSize();\n Matrix<T> res(r,c);\n for(int i=0;i<r;i++)for(int j=0;j<c;j++) res[i][j]=-mat[i][j];\n return res;\n }\n \n template<typename T>\n Matrix<T> operator*(const Matrix<T>& lhs,const Matrix<T> &rhs){\n assert(lhs.colSize()==rhs.rowSize());\n int r=lhs.rowSize(),c=rhs.colSize(),l=lhs.colSize();\n Matrix<T> res(r,c);\n for(int i=0;i<r;i++){\n for(int k=0;k<l;k++){\n for(int j=0;j<c;j++){\n res[i][j]+=lhs[i][k]*rhs[k][j];\n }\n }\n }\n return res;\n }\n\n template<typename T>\n Matrix<T> Identity(int n){\n assert(n>=0);\n Matrix<T> res(n,n);\n for(int i=0;i<n;i++){\n res[i][i]=1;\n }\n return res;\n }\n\n template<typename T>\n Matrix<T> pow(const Matrix<T>& mat,int k){\n assert(mat.rowSize()==mat.colSize());\n Matrix<T> x=mat;\n Matrix<T> res=Identity<T>(mat.rowSize());\n while(k){\n if(k&1) res=res*x;\n x=x*x;\n k>>=1;\n }\n return res;\n }\n \n template<typename T,typename Detail=Detail<T>>\n Matrix<T> gaussianElimination(Matrix<T> mat){\n int m=mat.rowSize(),n=mat.colSize();\n int row=0;\n for(int j=0;j<n;j++){\n if(row==m) break;\n int tar=-1;\n T v=Detail::EPS;\n for(int i=row;i<m;i++){\n if(!Detail::isZero(mat[i][j]) && abs(v)<abs(mat[i][j])){\n tar=i;\n v=mat[i][j];\n }\n }\n if(tar==-1) continue;\n if(row!=tar){ \n auto tmp=mat[row];\n mat[row]=mat[tar];\n mat[tar]=tmp;\n }\n for(int i=row+1;i<m;i++){\n if(!Detail::isZero(mat[i][j])){\n T r=mat[i][j]/mat[row][j];\n for(int k=j;k<n;k++){\n mat[i][k]-=r*mat[row][k];\n }\n }\n }\n row++;\n }\n return mat;\n }\n\n template<typename T,typename Detail=Detail<T>>\n int rank(const Matrix<T>& mat){\n auto tmp=gaussialElimination(mat);\n int m=tmp.rowSize(),n=tmp.colSize();\n int i=0,j=0;\n while(i<m && j<n){\n if(Detail::isZero(tmp[i][j])) j++;\n else i++,j++;\n }\n return i;\n }\n\n template<typename T>\n Matrix<T> inv(const Matrix<T>& mat){\n assert(mat.rowSize()==mat.colSize());\n int n=mat.rowSize();\n Matrix<T> tmp(n,2*n);\n for(int i=0;i<n;i++){\n for(int j=0;j<n;j++){\n tmp[i][j]=mat[i][j];\n tmp[i][j+n]=0;\n }\n tmp[i][i+n]=1;\n }\n mat=gaussianElimination(mat);\n Matrix<T> res(n);\n for(int i=0;i<n;i++){\n for(int j=0;j<n;j++){\n res[i][j]=mat[i][j+n]/=mat[i][i];\n }\n }\n return res;\n }\n \n template<typename T>\n void debug(Matrix<T> mat){\n int m=mat.rowSize(),n=mat.colSize();\n std::cerr<<\"###Matrix_Debug###\"<<std::endl;\n for(int i=0;i<m;i++){\n for(int j=0;j<n;j++){\n std::cerr<<mat[i][j]<<\" \";\n }\n std::cerr<<std::endl;\n }\n }\n}\n\nusing namespace ProconLib;\n\nint main(){\n int s,n,k;\n cin>>s>>n>>k;\n if(n==1 && s%k!=0){\n cout<<-1<<endl;\n return 0;\n }\n int sz=n*k+1;\n \n vector<vector<double>> dp(k+1,vector<double>(sz));\n dp[0][0]=1;\n for(int i=0;i<k;i++){\n for(int j=0;j<sz;j++){\n for(int k=1;k<=n;k++){\n if(j+k<sz) dp[i+1][j+k]+=dp[i][j]/n;\n }\n }\n }\n \n Matrix<double> B(sz,sz+1);\n {\n B[0][0]=1;\n B[0][sz]=0;\n for(int i=1;i<sz;i++){\n B[i][i]=-1;\n for(int j=0;j<sz;j++){\n // i to j\n int d=i-j;\n if(0<=d && d<sz){\n B[i][j]+=dp[k][d];\n B[i][sz]-=dp[k][d];\n }\n d=i+j;\n if(j!=0 && 0<=d && d<sz){\n B[i][j]+=dp[k][d];\n B[i][sz]-=dp[k][d];\n }\n }\n }\n }\n B=gaussianElimination(B);\n Vector<double> vec(sz+1);\n for(int i=sz-1;i>=0;i--){\n for(int j=0;j<i;j++){\n B[j][sz]-=B[j][i]/B[i][i]*B[i][sz];\n B[j][i]=0;\n }\n }\n for(int i=0;i<sz;i++){\n vec[sz-i-1]=B[i][sz]/B[i][i];\n }\n vec[sz]=1;\n\n Matrix<double> A(sz+1,sz+1);\n for(int i=1;i<sz;i++){\n A[i][i-1]=1;\n }\n A[sz][sz]=1;\n for(int i=0;i<sz;i++){\n int d=i+1;\n if(d<sz) A[0][i]=dp[k][d];\n if(d<sz) A[0][sz]+=dp[k][d];\n }\n A=pow(A,abs(s));\n double res=0;\n for(int i=0;i<=sz;i++){\n res+=A[sz-1][i]*vec[i];\n }\n cout<<fixed<<setprecision(10);\n cout<<res<<endl;\n\n return 0;\n}", "accuracy": 0.1935483870967742, "time_ms": 20, "memory_kb": 3488, "score_of_the_acc": -0.0064, "final_rank": 14 }, { "submission_id": "aoj_2315_3530756", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\n\nnamespace ProconLib{\n \n template<typename T>\n struct Detail{\n static constexpr T EPS = T(1e-9);\n static bool isZero(T v){\n return abs(v)<=EPS;\n }\n };\n\n template<typename T>\n class Vector{\n int N;\n std::vector<T> dat;\n public:\n Vector(int n):N(n),dat(n){}\n Vector(int n,T x):N(n),dat(n,x){}\n Vector(std::vector<T> vec):N(vec.size()),dat(vec){}\n Vector(const std::vector<T>& vec):N(vec.size()),dat(vec){}\n Vector(const Vector& vec){dat=vec.dat;}\n int size(){return N;}\n T& operator[](int pos){return dat[pos];};\n const T& operator[](int pos) const{return dat[pos];};\n T& at(int pos){return dat.at(pos);}\n const T& at(int pos) const {return dat.at(pos);}\n };\n \n template<typename T>\n class Matrix{\n int r,c;\n std::vector<Vector<T>> dat;\n public:\n Matrix(int r,int c):r(r),c(c),dat(r,Vector<T>(c)){}\n Matrix(int r,int c,T x):r(r),c(c),dat(r,Vector<T>(c,x)){}\n\n Vector<T>& operator[](int pos){return dat[pos];};\n const Vector<T>& operator[](int pos) const{return dat[pos];};\n Vector<T>& at(int pos){return dat.at(pos);}\n const Vector<T>& at(int pos) const {return dat.at(pos);}\n int rowSize() const {return r;}\n int colSize() const {return c;}\n };\n template<typename T>\n Matrix<T> operator+(const Matrix<T>& lhs,const Matrix<T>& rhs);\n template<typename T>\n Matrix<T> operator-(const Matrix<T>& lhs,const Matrix<T>& rhs);\n template<typename T>\n Matrix<T> operator+(const Matrix<T>& mat);\n template<typename T>\n Matrix<T> operator-(const Matrix<T>& mat);\n template<typename T>\n Matrix<T> operator*(const Matrix<T>& lhs,const Matrix<T> &rhs);\n template<typename T>\n Matrix<T> Identity(int n);\n template<typename T>\n Matrix<T> pow(const Matrix<T>& mat,int k);\n template<typename T,typename Detail=Detail<T>>\n Matrix<T> gaussianElimination(Matrix<T> mat);\n template<typename T>\n int rank(const Matrix<T>& mat);\n template<typename T>\n Matrix<T> inv(const Matrix<T>& mat);\n \n template<typename T>\n Matrix<T> operator+(const Matrix<T>& lhs,const Matrix<T>& rhs){\n assert(lhs.rowSize()==rhs.rowSize() && lhs.colSize()==rhs.colSize());\n int r=lhs.rowSize(),c=lhs.colSize();\n Matrix<T> res(r,c);\n for(int i=0;i<r;i++){\n for(int j=0;j<c;j++){\n res[i][j]=lhs[i]+rhs[i];\n }\n }\n return res;\n }\n template<typename T>\n Matrix<T> operator-(const Matrix<T>& lhs,const Matrix<T>& rhs){\n assert(lhs.rowSize()==rhs.rowSize() && lhs.colSize()==rhs.colSize());\n int r=lhs.rowSize(),c=lhs.colSize();\n Matrix<T> res(r,c);\n for(int i=0;i<r;i++){\n for(int j=0;j<c;j++){\n res[i][j]=lhs[i]-rhs[i];\n }\n }\n return res;\n }\n template<typename T>\n Matrix<T> operator+(const Matrix<T>& mat){\n int r=mat.rowSize(),c=mat.colSize();\n Matrix<T> res(r,c);\n for(int i=0;i<r;i++)for(int j=0;j<c;j++) res[i][j]=-mat[i][j];\n return res;\n }\n template<typename T>\n Matrix<T> operator-(const Matrix<T>& mat){\n int r=mat.rowSize(),c=mat.colSize();\n Matrix<T> res(r,c);\n for(int i=0;i<r;i++)for(int j=0;j<c;j++) res[i][j]=-mat[i][j];\n return res;\n }\n \n template<typename T>\n Matrix<T> operator*(const Matrix<T>& lhs,const Matrix<T> &rhs){\n assert(lhs.colSize()==rhs.rowSize());\n int r=lhs.rowSize(),c=rhs.colSize(),l=lhs.colSize();\n Matrix<T> res(r,c);\n for(int i=0;i<r;i++){\n for(int k=0;k<l;k++){\n for(int j=0;j<c;j++){\n res[i][j]+=lhs[i][k]*rhs[k][j];\n }\n }\n }\n return res;\n }\n\n template<typename T>\n Matrix<T> Identity(int n){\n assert(n>=0);\n Matrix<T> res(n,n);\n for(int i=0;i<n;i++){\n res[i][i]=1;\n }\n return res;\n }\n\n template<typename T>\n Matrix<T> pow(const Matrix<T>& mat,int k){\n assert(mat.rowSize()==mat.colSize());\n Matrix<T> x=mat;\n Matrix<T> res=Identity<T>(mat.rowSize());\n while(k){\n if(k&1) res=res*x;\n x=x*x;\n k>>=1;\n }\n return res;\n }\n \n template<typename T,typename Detail=Detail<T>>\n Matrix<T> gaussianElimination(Matrix<T> mat){\n int m=mat.rowSize(),n=mat.colSize();\n int row=0;\n for(int j=0;j<n;j++){\n if(row==m) break;\n int tar=-1;\n T v=Detail::EPS;\n for(int i=row;i<m;i++){\n if(!Detail::isZero(mat[i][j]) && abs(v)<abs(mat[i][j])){\n tar=i;\n v=mat[i][j];\n }\n }\n if(tar==-1) continue;\n if(row!=tar){ \n auto tmp=mat[row];\n mat[row]=mat[tar];\n mat[tar]=tmp;\n }\n for(int i=row+1;i<m;i++){\n if(!Detail::isZero(mat[i][j])){\n T r=mat[i][j]/mat[row][j];\n for(int k=j;k<n;k++){\n mat[i][k]-=r*mat[row][k];\n }\n }\n }\n row++;\n }\n return mat;\n }\n\n template<typename T,typename Detail=Detail<T>>\n int rank(const Matrix<T>& mat){\n auto tmp=gaussialElimination(mat);\n int m=tmp.rowSize(),n=tmp.colSize();\n int i=0,j=0;\n while(i<m && j<n){\n if(Detail::isZero(tmp[i][j])) j++;\n else i++,j++;\n }\n return i;\n }\n\n template<typename T>\n Matrix<T> inv(const Matrix<T>& mat){\n assert(mat.rowSize()==mat.colSize());\n int n=mat.rowSize();\n Matrix<T> tmp(n,2*n);\n for(int i=0;i<n;i++){\n for(int j=0;j<n;j++){\n tmp[i][j]=mat[i][j];\n tmp[i][j+n]=0;\n }\n tmp[i][i+n]=1;\n }\n mat=gaussianElimination(mat);\n Matrix<T> res(n);\n for(int i=0;i<n;i++){\n for(int j=0;j<n;j++){\n res[i][j]=mat[i][j+n]/=mat[i][i];\n }\n }\n return res;\n }\n \n template<typename T>\n void debug(Matrix<T> mat){\n int m=mat.rowSize(),n=mat.colSize();\n std::cerr<<\"###Matrix_Debug###\"<<std::endl;\n for(int i=0;i<m;i++){\n for(int j=0;j<n;j++){\n std::cerr<<mat[i][j]<<\" \";\n }\n std::cerr<<std::endl;\n }\n }\n}\n\nusing namespace ProconLib;\n\nint main(){\n int s,n,k;\n cin>>s>>n>>k;\n if(n==1 && s%k!=0){\n cout<<-1<<endl;\n return 0;\n }\n int sz=n*k+1;\n \n vector<vector<double>> dp(k+1,vector<double>(sz));\n dp[0][0]=1;\n for(int i=0;i<k;i++){\n for(int j=0;j<sz;j++){\n for(int k=1;k<=n;k++){\n if(j+k<sz) dp[i+1][j+k]+=dp[i][j]/n;\n }\n }\n }\n \n Matrix<double> B(sz,sz+1);\n {\n B[0][0]=1;\n B[0][sz]=0;\n for(int i=1;i<sz;i++){\n B[i][i]=-1;\n for(int j=0;j<sz;j++){\n // i to j\n int d=i-j;\n if(0<=d && d<sz){\n B[i][j]+=dp[k][d];\n B[i][sz]-=dp[k][d];\n }\n d=i+j;\n if(j!=0 && 0<=d && d<sz){\n B[i][j]+=dp[k][d];\n B[i][sz]-=dp[k][d];\n }\n }\n }\n }\n B=gaussianElimination(B);\n Vector<double> vec(sz+1);\n for(int i=sz-1;i>=0;i--){\n for(int j=0;j<i;j++){\n B[j][sz]-=B[j][i]/B[i][i]*B[i][sz];\n B[j][i]=0;\n }\n }\n for(int i=0;i<sz;i++){\n vec[sz-i-1]=B[i][sz]/B[i][i];\n }\n vec[sz]=1;\n\n Matrix<double> A(sz+1,sz+1);\n for(int i=1;i<sz;i++){\n A[i][i-1]=1;\n }\n A[sz][sz]=1;\n for(int i=0;i<sz;i++){\n int d=i+1;\n if(d<sz) A[0][i]=dp[k][d];\n if(d<sz) A[0][sz]+=dp[k][d];\n }\n A=pow(A,abs(s));\n double res=0;\n for(int i=0;i<=sz;i++){\n res+=A[sz-1][i]*vec[i];\n }\n cout<<fixed<<setprecision(10);\n cout<<res<<endl;\n\n return 0;\n}", "accuracy": 0.1935483870967742, "time_ms": 20, "memory_kb": 3436, "score_of_the_acc": -0.0026, "final_rank": 13 }, { "submission_id": "aoj_2315_3315116", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ld = long double;\n\ntemplate<class T>\nclass matrix {\n\tvector<vector<T>> dat;\npublic:\n\tmatrix(int row) : dat(row, vector<T>(1)) {}\n\tmatrix(int row, int col) : dat(row, vector<T>(col)) {}\n\tmatrix(int row, int col, const T& id) : dat(row, vector<T>(col, id)) {}\n\tmatrix(const vector<vector<T>>& v) : dat(v) {}\n\tmatrix(const vector<T>& v) : dat(v.size(), vector<T>(1)) {\n\t\tfor (int i = 0; i < (int)v.size(); ++i) dat[i][0] = v[i];\n\t}\n\texplicit operator vector<vector<T>>() const { return dat; }\n\tint row_size() const { return dat.size(); }\n\tint col_size() const { return dat[0].size(); }\n\tmatrix<T>& operator+=(const matrix<T>& that) {\n\t\tint row = row_size(), col = col_size();\n\t\tassert(row_size() == that.row_size() && col_size() == that.col_size());\n\t\tfor (int i = 0; i < row; ++i) for (int j = 0; j < col; ++j) dat[i][j] += that[i][j];\n\t\treturn *this;\n\t}\n\tmatrix<T>& operator-=(const matrix<T>& that) {\n\t\tint row = row_size(), col = col_size();\n\t\tassert(row_size() == that.row_size() && col_size() == that.col_size());\n\t\tfor (int i = 0; i < row; ++i) for (int j = 0; j < col; ++j) dat[i][j] -= that[i][j];\n\t\treturn *this;\n\t}\n\tmatrix<T>& operator*=(const T& that) {\n\t\tint row = row_size(), col = col_size();\n\t\tfor (int i = 0; i < row; ++i) for (int j = 0; j < col; ++j) dat[i][j] *= that;\n\t\treturn *this;\n\t}\n\tmatrix<T>& operator/=(const T& that) {\n\t\tint row = row_size(), col = col_size();\n\t\tfor (int i = 0; i < row; ++i) for (int j = 0; j < col; ++j) dat[i][j] /= that;\n\t\treturn *this;\n\t}\n\tmatrix<T> operator*(const matrix<T>& that) const {\n\t\tint x = row_size(), y = col_size(), z = that.col_size();\n\t\tassert(col_size() == that.row_size());\n\t\tmatrix<T> res(x, z);\n\t\tfor (int i = 0; i < x; ++i) {\n\t\t\tfor (int j = 0; j < y; ++j) {\n\t\t\t\tfor (int k = 0; k < z; ++k) {\n\t\t\t\t\tres[i][k] += dat[i][j] * that[j][k];\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn res;\n\t}\n\tmatrix<T>& operator*=(const matrix<T>& that) { return *this = *this * that; }\n\n\tvector<T>& operator[](size_t i) & { return dat[i]; }\n\tconst vector<T>& operator[](size_t i) const& { return dat[i]; }\n\tvector<T> operator[](size_t i) const&& { return move(dat[i]); }\n\n\tT& get(int i, int j) & {\n\t\tassert(0 <= i && i < row_size());\n\t\tassert(0 <= j && j < col_size());\n\t\treturn dat[i][j];\n\t}\n\tconst T& get(int i, int j) const& {\n\t\tassert(0 <= i && i < row_size());\n\t\tassert(0 <= j && j < col_size());\n\t\treturn dat[i][j];\n\t}\n\tT get(int i, int j) const&& {\n\t\tassert(0 <= i && i < row_size());\n\t\tassert(0 <= j && j < col_size());\n\t\treturn move(dat[i][j]);\n\t}\n\tmatrix<T> transpose() const {\n\t\tint row = row_size(), col = col_size();\n\t\tmatrix<T> res(col, row);\n\t\tfor (int i = 0; i < row; ++i) for (int j = 0; j < col; ++j) res[j][i] = dat[i][j];\n\t\treturn res;\n\t}\n};\n\ntemplate <class T> matrix<T> operator+(const matrix<T>& a, const matrix<T>& b) { return matrix<T>(a) += b; }\ntemplate <class T> matrix<T> operator-(const matrix<T>& a, const matrix<T>& b) { return matrix<T>(a) -= b; }\ntemplate <class T> matrix<T> operator*(const matrix<T>& a, const T& b) { return matrix<T>(a) *= b; }\ntemplate <class T> matrix<T> operator*(const T& a, const matrix<T>& b) { return matrix<T>(b) *= a; }\ntemplate <class T> matrix<T> operator/(const matrix<T>& a, const T& b) { return matrix<T>(a) /= b; }\n\ntemplate <class T> matrix<T> identity_matrix(int n) {\n\tmatrix<T> a(n, n);\n\tfor (int i = 0; i < n; i++) a[i][i] = T(1);\n\treturn a;\n}\n\ntemplate <class T> matrix<T> pow(const matrix<T>& a, long long b) {\n\tconst int n = a.row_size();\n\tassert(a.row_size() == a.col_size() && 0 <= b);\n\tmatrix<T> res = identity_matrix<T>(n), x(a);\n\twhile (true) {\n\t\tif (b & 1LL) res *= x;\n\t\tif (!(b >>= 1LL)) break;\n\t\tx *= x;\n\t}\n\treturn res;\n}\n\nconst ld eps = 1e-10;\n\ntemplate <typename T>\nbool is_zero(T val) {\n\treturn val == T(0);\n}\n\ntemplate <>\nbool is_zero(ld val) {\n\treturn std::abs(val) < eps;\n}\n\ntemplate <typename T>\nvector<T> gauss_jordan(const matrix<T>& A, const vector<T>& b) {\n\tconst int n = A.row_size();\n\tmatrix<T> B(n, n + 1);\n\tfor (int i = 0; i < n; ++i) {\n\t\tfor (int j = 0; j < n; ++j) B[i][j] = A[i][j];\n\t\tB[i][n] = b[i];\n\t}\n\tfor (int i = 0; i < n; ++i) {\n\t\tint pivot = i;\n\t\tfor (int j = i; j < n; ++j) {\n\t\t\tif (std::abs(B[j][i]) > std::abs(B[pivot][i])) pivot = j;\n\t\t}\n\t\tif (i != pivot) swap(B[i], B[pivot]);\n\t\tif (is_zero(B[i][i])) return vector<T>(); // no solution\n\t\tfor (int j = i + 1; j <= n; ++j) B[i][j] /= B[i][i];\n\t\tfor (int j = 0; j < n; ++j) {\n\t\t\tif (i == j) continue;\n\t\t\tfor (int k = i + 1; k <= n; ++k) B[j][k] -= B[j][i] * B[i][k];\n\t\t}\n\t}\n\tvector<T> x(n);\n\tfor (int i = 0; i < n; ++i) x[i] = B[i][n];\n\treturn x;\n}\n\nint main()\n{\n\tcout << fixed << setprecision(8);\n\tint S, N, K;\n\tcin >> S >> N >> K; S = abs(S);\n\tif (S == 0) {\n\t\tcout << 0 << endl;\n\t\treturn 0;\n\t}\n\tif (N == 1) {\n\t\tcout << (S % K == 0 ? S / K : -1) << endl;\n\t\treturn 0;\n\t}\n\tvector<ld> dp = { 1 };\n\tfor (int i = 0; i < K; i++) {\n\t\tvector<ld> tmp((i + 1) * N + 1);\n\t\tfor (int j = 0; j <= i * N; j++) {\n\t\t\tfor (int k = 1; k <= N; k++) {\n\t\t\t\ttmp[j + k] += dp[j] / N;\n\t\t\t}\n\t\t}\n\t\tdp.swap(tmp);\n\t}\n\tint M = N * K;\n\tmatrix<ld> mat(M, M);\n\tvector<ld> b(M);\n\tmat[0][0] = 1;\n\tb[0] = 0;\n\tfor (int i = 1; i < M; i++) {\n\t\tmat[i][i] = -1;\n\t\tld kei = i * 2 <= M ? 1 / (1 - dp[i * 2]) : 1;\n\t\tb[i] -= kei;\n\t\tfor (int j = 0; j <= M; j++) {\n\t\t\tint p = abs(i - j);\n\t\t\tif (p == i) continue;\n\t\t\tmat[i][p] += dp[j] * kei;\n\t\t}\n\t}\n\tauto vec = gauss_jordan(mat, b);\n\tassert(!vec.empty());\n\tvec.push_back(1);\n\tmatrix<ld> kei(M + 1, M + 1);\n\tfor (int i = 0; i < M - 1; i++) {\n\t\tkei[i][i + 1] = 1;\n\t}\n\tfor (int i = 0; i < M; i++) {\n\t\tkei[M - 1][i] = dp[M - i];\n\t}\n\tkei[M - 1][M] = 1;\n\tkei[M][M] = 1;\n\tkei = pow(kei, S);\n\tld res = 0;\n\tfor (int i = 0; i <= M; i++) {\n\t\tres += kei[0][i] * vec[i];\n\t}\n\tcout << res << endl;\n\treturn 0;\n}", "accuracy": 1, "time_ms": 110, "memory_kb": 3796, "score_of_the_acc": -0.0923, "final_rank": 7 } ]
aoj_2313_cpp
問題文 ハコの魔女 H.N.ELLY はとある動画サイトの熱狂的なファンである.ハコの魔女の強さはその時々のその動画サイトからの転送速度に応じて変化するのではないかと 美樹さやか は考えた.そこで動画サイトからハコの魔女の持つコンピュータまでの過去の転送速度 (=単位時間あたりのデータの転送量) を調べたい. 初期のインターネットのネットワークの構造とそれ以降のネットワークの構造の変化を表すクエリが与えられるので,各変化について変化した直後の動画サイトからハコの魔女の持つコンピュータまでの転送速度を求めよ. インターネットは複数の転送装置からなるものと見なし,各々をつなぐ回線は双方向に情報を送ることができ,その転送速度の最大は 1 であるとする.また,ネットワークは常に動画サイトからハコの魔女へ送られるデータの転送速度を最大化するように運ぶものとする. 入力形式 入力は以下の形式で与えられる. N\ E\ Q\\ F_1\ T_1\\ F_2\ T_2\\ …\\ F_E\ T_E\\ M_1\ A_1\ B_1\\ M_2\ A_2\ B_2\\ ...\\ M_Q\ A_Q\ B_Q\\ N は動画サイトとハコの魔女の持つコンピュータを含めた転送装置の数である.番号が 1 である転送装置は動画サイトであり,番号が N である転送装置はハコの魔女の持つコンピュータである. E は初期状態で接続されている転送装置の組み合わせの数であり, Q はインターネットが変化した回数である.初期のインターネットは F_i と T_i が転送速度 1 で双方向に接続されていることを表す. ネットワークの変化は時系列順に与えられ, j 番目の変化は M_j が 1 であれば A_j,\ B_j 間がつながれたことを表し, M_j が 2 であれば A_j,\ B_j 間の接続が切れたことを表す. 出力形式 各変化の直後における,動画サイトからハコの魔女の持つコンピュータまでの転送速度を出力せよ. 制約 2 \leq N \leq 500 0 \leq E \leq 20,000 1 \leq Q \leq 1,000 1 \leq F_i \leq N,\ 1 \leq T_i \leq N,\ F_i \neq T_i \ (1 \leq i \leq E) すべての \{F_i, T_i\} のペアは異なる. 1 \leq M_j \leq 2,\ 1 \leq A_j \leq N\ ,\ 1 \leq B_j \leq N\ ,\ A_j \neq B_j (1 \leq j \leq Q) ネットワークのどの段階においても次のことが成り立つ : どの 2 つの転送装置の間も高々 1 つの回線でしか繋がれていない. 2 つの転送装置の間が既に繋がれている状態でそれらの間を接続するようなクエリが来たり, 2 つの転送装置の間が回線で繋がれていない状態でそれらの間の接続を切るようなクエリが来ることはない. 入出力例 入力例 1 2 1 2 1 2 2 1 2 1 2 1 出力例1 0 1 最初の変化の後,ハコの魔女のコンピュータと動画サイトは繋がらなくなっているので転送速度は 0 である. 2 度目の変化の後は両者は直接繋がっており,転送速度は 1 になる. 入力例 2 3 0 4 1 2 3 1 2 1 1 3 1 2 2 3 出力例 2 0 1 2 1 入力例 3 6 4 8 1 2 1 3 4 6 5 6 1 2 4 1 3 5 1 2 5 1 3 4 2 2 4 2 3 5 1 2 4 2 2 1 出力例 3 1 2 2 2 2 2 2 1 入力例 4 12 38 6 1 2 1 3 1 4 1 5 2 3 2 4 2 5 2 6 2 7 2 8 2 12 3 4 3 5 3 6 3 7 3 8 4 5 4 6 4 7 4 8 5 6 5 7 5 8 6 7 6 8 6 9 6 10 6 12 7 8 7 9 7 10 8 9 8 10 9 10 9 11 9 12 10 11 11 12 2 6 12 2 9 12 1 9 12 1 6 12 2 6 12 1 6 12 出力例 4 3 2 3 4 3 4 Problem Setter: Flat35
[ { "submission_id": "aoj_2313_9805512", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n/**\n * @brief template\n */\n\nstruct MaxFlow {\n struct Edge {\n int to, rev;\n ll cap;\n };\n int V;\n vector<vector<Edge>> G;\n vector<int> itr, level;\n using P = pair<int, int>;\n vector<P> es;\n\n public:\n MaxFlow() {}\n MaxFlow(int V) : V(V) {\n G.assign(V, vector<Edge>());\n }\n int add_vertex() {\n G.push_back(vector<Edge>());\n return V++;\n }\n int add_edge(int from, int to, ll cap) {\n int fid = SZ(G[from]), tid = SZ(G[to]);\n if (from == to)\n tid++;\n es.push_back({from, fid});\n G[from].push_back({to, tid, cap});\n G[to].push_back({from, fid, 0});\n return es.size()-1;\n }\n struct Type {\n int from, to;\n ll cap, recap;\n };\n Type get_edge(int i) {\n auto [from, pos] = es[i];\n auto e = G[from][pos];\n auto re = G[e.to][e.rev];\n return Type{from, e.to, e.cap, re.cap};\n }\n void bfs(int s) {\n level.assign(V, -1);\n queue<int> q;\n level[s] = 0;\n q.push(s);\n while (!q.empty()) {\n int v = q.front();\n q.pop();\n for (auto &e : G[v]) {\n if (e.cap > 0 && level[e.to] < 0) {\n level[e.to] = level[v] + 1;\n q.push(e.to);\n }\n }\n }\n }\n ll dfs(int v, int t, ll f) {\n if (v == t)\n return f;\n for (int &i = itr[v]; i < (int)G[v].size(); i++) {\n Edge &e = G[v][i];\n if (e.cap > 0 && level[v] < level[e.to]) {\n ll d = dfs(e.to, t, min(f, e.cap));\n if (d > 0) {\n e.cap -= d, G[e.to][e.rev].cap += d;\n return d;\n }\n }\n }\n return 0;\n }\n ll run(int s, int t, ll flow_limit = INF) {\n ll ret = 0, f;\n while (ret < flow_limit && (bfs(s), level[t] >= 0)) {\n itr.assign(V, 0);\n while ((f = dfs(s, t, flow_limit - ret)) > 0)\n ret += f;\n }\n return ret;\n }\n vector<int> cut() {\n vector<int> ret(V);\n rep(v, 0, V) if (level[v] < 0) ret[v] = 1;\n return ret;\n }\n void erase_edge(int i) {\n auto& e = G[es[i].first][es[i].second];\n auto& re = G[e.to][e.rev];\n e.cap = re.cap = 0;\n }\n};\n\n/**\n * @brief Maximum Flow\n */\n\nint main() {\n int N, M, Q;\n cin >> N >> M >> Q;\n MaxFlow G(N);\n map<pair<int,int>,int> mp;\n rep(i,0,M) {\n int A, B;\n cin >> A >> B;\n A--, B--;\n mp[{A,B}] = G.add_edge(A,B,1);\n mp[{B,A}] = G.add_edge(B,A,1);\n }\n int ANS = G.run(0,N-1);\n rep(i,0,Q) {\n int T, A, B;\n cin >> T >> A >> B;\n A--, B--;\n if (T == 1) {\n mp[{A,B}] = G.add_edge(A,B,1);\n mp[{B,A}] = G.add_edge(B,A,1);\n ANS += G.run(0,N-1);\n }\n else {\n int id1 = mp[{A,B}], id2 = mp[{B,A}];\n if (G.get_edge(id1).cap == 0 && G.get_edge(id2).cap == 1) {\n G.erase_edge(id1);\n G.erase_edge(id2);\n if (G.run(A,B,1) == 0) {\n ANS--;\n G.run(A,0,1);\n G.run(N-1,B,1);\n }\n }\n else if (G.get_edge(id2).cap == 0 && G.get_edge(id1).cap == 1) {\n G.erase_edge(id1);\n G.erase_edge(id2);\n if (G.run(B,A,1) == 0) {\n ANS--;\n G.run(B,0,1);\n G.run(N-1,A,1);\n }\n }\n else {\n G.erase_edge(id1);\n G.erase_edge(id2);\n }\n }\n cout << ANS << endl;\n }\n}", "accuracy": 1, "time_ms": 700, "memory_kb": 7500, "score_of_the_acc": -1.1846, "final_rank": 9 }, { "submission_id": "aoj_2313_9805493", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n/**\n * @brief template\n */\n\nstruct MaxFlow {\n struct Edge {\n int to, rev;\n ll cap;\n };\n int V;\n vector<vector<Edge>> G;\n vector<int> itr, level;\n using P = pair<int, int>;\n vector<P> es;\n\n public:\n MaxFlow() {}\n MaxFlow(int V) : V(V) {\n G.assign(V, vector<Edge>());\n }\n int add_vertex() {\n G.push_back(vector<Edge>());\n return V++;\n }\n int add_edge(int from, int to, ll cap) {\n int fid = SZ(G[from]), tid = SZ(G[to]);\n if (from == to)\n tid++;\n es.push_back({from, fid});\n G[from].push_back({to, tid, cap});\n G[to].push_back({from, fid, 0});\n return es.size()-1;\n }\n struct Type {\n int from, to;\n ll cap, recap;\n };\n Type get_edge(int i) {\n auto [from, pos] = es[i];\n auto e = G[from][pos];\n auto re = G[e.to][e.rev];\n return Type{from, e.to, e.cap, re.cap};\n }\n void bfs(int s) {\n level.assign(V, -1);\n queue<int> q;\n level[s] = 0;\n q.push(s);\n while (!q.empty()) {\n int v = q.front();\n q.pop();\n for (auto &e : G[v]) {\n if (e.cap > 0 && level[e.to] < 0) {\n level[e.to] = level[v] + 1;\n q.push(e.to);\n }\n }\n }\n }\n ll dfs(int v, int t, ll f) {\n if (v == t)\n return f;\n for (int &i = itr[v]; i < (int)G[v].size(); i++) {\n Edge &e = G[v][i];\n if (e.cap > 0 && level[v] < level[e.to]) {\n ll d = dfs(e.to, t, min(f, e.cap));\n if (d > 0) {\n e.cap -= d, G[e.to][e.rev].cap += d;\n return d;\n }\n }\n }\n return 0;\n }\n ll run(int s, int t, ll flow_limit = INF) {\n ll ret = 0, f;\n while (ret < flow_limit && (bfs(s), level[t] >= 0)) {\n itr.assign(V, 0);\n while ((f = dfs(s, t, flow_limit - ret)) > 0)\n ret += f;\n }\n return ret;\n }\n vector<int> cut() {\n vector<int> ret(V);\n rep(v, 0, V) if (level[v] < 0) ret[v] = 1;\n return ret;\n }\n void erase_edge(int i) {\n auto& e = G[es[i].first][es[i].second];\n auto& re = G[e.to][e.rev];\n e.cap = re.cap = 0;\n }\n};\n\n/**\n * @brief Maximum Flow\n */\n\nint main() {\n int N, M, Q;\n cin >> N >> M >> Q;\n MaxFlow G(N);\n map<pair<int,int>,int> mp;\n rep(i,0,M) {\n int A, B;\n cin >> A >> B;\n A--, B--;\n mp[{A,B}] = G.add_edge(A,B,1);\n mp[{B,A}] = G.add_edge(B,A,1);\n }\n int ANS = G.run(0,N-1);\n rep(i,0,Q) {\n int T, A, B;\n cin >> T >> A >> B;\n A--, B--;\n if (T == 1) {\n mp[{A,B}] = G.add_edge(A,B,1);\n mp[{B,A}] = G.add_edge(B,A,1);\n ANS += G.run(0,N-1);\n }\n else {\n int id1 = mp[{A,B}], id2 = mp[{B,A}];\n if (G.get_edge(id1).cap == 0 && G.get_edge(id2).cap == 1) {\n G.erase_edge(id1);\n G.erase_edge(id2);\n if (G.run(A,B) == 0) {\n ANS--;\n G.run(A,0,1);\n G.run(N-1,B,1);\n }\n }\n else if (G.get_edge(id2).cap == 0 && G.get_edge(id1).cap == 1) {\n G.erase_edge(id1);\n G.erase_edge(id2);\n if (G.run(B,A) == 0) {\n ANS--;\n G.run(B,0,1);\n G.run(N-1,A,1);\n }\n }\n else {\n G.erase_edge(id1);\n G.erase_edge(id2);\n }\n }\n cout << ANS << endl;\n }\n}", "accuracy": 0.2413793103448276, "time_ms": 10, "memory_kb": 3628, "score_of_the_acc": 0, "final_rank": 13 }, { "submission_id": "aoj_2313_9376324", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <map>\n#include <queue>\n#include <set>\nusing namespace std;\ntypedef long long ll;\n#define rep(i, n) for(int i = 0; i < (n); i++)\n\ntemplate<class T>\nusing vi = vector<T>;\n\ntemplate<class T>\nusing vii = vector<vi<T>>;\n\ntemplate<class T>\nusing viii = vector<vii<T>>;\n\n\nstruct Dinic {\npublic:\n const long long inf = 1e18;\n vector<int> dist; //sからの距離\n vector<int> iter; //どこまで調べたか\n\n struct edge {\n int to, rev; long long cap;\n edge(int t = 0, int r = 0, long long c = 0LL) : to(t), rev(r), cap(c) {}\n };\n\n int n;\n vector<vector<edge>> to;\n\n Dinic(int n_ = 1) : n(n_), to(n_), dist(n_), iter(n_) {}\n\n void addedge(int u, int v, long long cap) {//u->vにcapの容量\n int su = (int)to[u].size(), sv = (int)to[v].size();\n to[u].push_back({ v, sv, cap });\n to[v].push_back({ u, su, cap });\n }\n\n long long dfs(int v, int t, long long f) { //t: 終点, f: flow\n if (v == t) return f;\n int sv = (int)to[v].size();\n for (int& i = iter[v]; i < sv; i++) {\n edge& nv = to[v][i];\n if (dist[nv.to] <= dist[v] || nv.cap == 0) continue; //here\n long long nf = dfs(nv.to, t, min(f, nv.cap));\n if (nf > 0) {\n nv.cap -= nf;\n to[nv.to][nv.rev].cap += nf;\n return nf;\n }\n }\n return 0;\n }\n\n void bfs(int s) {\n dist.assign(n, -1);\n queue<int> q;\n q.push(s);\n dist[s] = 0;\n while (!q.empty()) {\n int v = q.front();\n q.pop();\n for (auto nv : to[v]) {\n if (nv.cap > 0 && dist[nv.to] < 0) {\n dist[nv.to] = dist[v] + 1;\n q.push(nv.to);\n }\n }\n }\n }\n\n long long maxflow(int s, int t, int f) {//s:始点, t:終点\n long long res = 0, flow = 0;\n while (true) {\n bfs(s);\n if (dist[t] < 0) break;\n\n iter.assign(n, 0);\n while (flow = dfs(s, t, f)) {\n res += flow;\n f -= flow;\n }\n if (f == 0) return res;\n };\n return res;\n }\n};\n\n\nint main()\n{\n std::cin.tie(nullptr);\n std::ios_base::sync_with_stdio(false);\n int n, e, q;\n cin >> n >> e >> q;\n\n Dinic dn(n);\n vii<pair<int, int>> vec(n, vi<pair<int, int>>(n, { -1, -1 }));\n \n rep(i, e) {\n int f, t;\n cin >> f >> t;\n f--, t--;\n if (f > t) swap(f, t);\n dn.addedge(f, t, 1);\n vec[f][t] = { dn.to[t].back().rev, dn.to[f].back().rev };\n }\n\n vi<int> ans;\n int flow = dn.maxflow(0, n - 1, n);\n rep(i, q) {\n int m, a, b;\n cin >> m >> a >> b;\n a--, b--;\n if (a > b) swap(a, b);\n if (m == 1) {\n if (vec[a][b].first == -1 ) {\n dn.addedge(a, b, 1);\n vec[a][b] = {dn.to[b].back().rev, dn.to[a].back().rev};\n }\n else {\n dn.to[a][vec[a][b].first].cap = 1;\n dn.to[b][vec[a][b].second].cap = 1;\n }\n flow += dn.maxflow(0, n - 1, n);\n }\n else {\n if (dn.to[a][vec[a][b].first].cap == 0) {\n int tf = dn.maxflow(a, b, 1);\n if (tf == 0) {\n dn.maxflow(n - 1, b, 1);\n dn.maxflow(a, 0, 1);\n flow--;\n }\n }\n else if (dn.to[b][vec[a][b].second].cap == 0) {\n int tf = dn.maxflow(b, a, 1);\n if (tf == 0) {\n dn.maxflow(n - 1, a, 1);\n dn.maxflow(b, 0, 1);\n flow--;\n }\n }\n \n dn.to[a][vec[a][b].first].cap = 0;\n dn.to[b][vec[a][b].second].cap = 0;\n }\n\n ans.push_back(flow);\n }\n\n for (auto elem : ans) cout << elem << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 270, "memory_kb": 6308, "score_of_the_acc": -0.7066, "final_rank": 6 }, { "submission_id": "aoj_2313_9376314", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <map>\n#include <queue>\n#include <set>\nusing namespace std;\ntypedef long long ll;\n#define rep(i, n) for(int i = 0; i < (n); i++)\n\ntemplate<class T>\nusing vi = vector<T>;\n\ntemplate<class T>\nusing vii = vector<vi<T>>;\n\ntemplate<class T>\nusing viii = vector<vii<T>>;\n\n\nstruct Dinic {\npublic:\n const long long inf = 1e18;\n vector<int> dist; //sからの距離\n vector<int> iter; //どこまで調べたか\n\n struct edge {\n int to, rev; long long cap;\n edge(int t = 0, int r = 0, long long c = 0LL) : to(t), rev(r), cap(c) {}\n };\n\n int n;\n vector<vector<edge>> to;\n\n Dinic(int n_ = 1) : n(n_), to(n_), dist(n_), iter(n_) {}\n\n void addedge(int u, int v, long long cap) {//u->vにcapの容量\n int su = (int)to[u].size(), sv = (int)to[v].size();\n to[u].push_back({ v, sv, cap });\n to[v].push_back({ u, su, cap });\n }\n\n long long dfs(int v, int t, long long f) { //t: 終点, f: flow\n if (v == t) return f;\n int sv = (int)to[v].size();\n for (int& i = iter[v]; i < sv; i++) {\n edge& nv = to[v][i];\n if (dist[nv.to] <= dist[v] || nv.cap == 0) continue; //here\n long long nf = dfs(nv.to, t, min(f, nv.cap));\n if (nf > 0) {\n nv.cap -= nf;\n to[nv.to][nv.rev].cap += nf;\n return nf;\n }\n }\n return 0;\n }\n\n void bfs(int s) {\n dist.assign(n, -1);\n queue<int> q;\n q.push(s);\n dist[s] = 0;\n while (!q.empty()) {\n int v = q.front();\n q.pop();\n for (auto nv : to[v]) {\n if (nv.cap > 0 && dist[nv.to] < 0) {\n dist[nv.to] = dist[v] + 1;\n q.push(nv.to);\n }\n }\n }\n }\n\n long long maxflow(int s, int t, int f) {//s:始点, t:終点\n long long res = 0, flow = 0;\n while (true) {\n bfs(s);\n if (dist[t] < 0) break;\n\n iter.assign(n, 0);\n while (flow = dfs(s, t, f)) {\n res += flow;\n f -= flow;\n }\n if (f == 0) return res;\n };\n return res;\n }\n};\n\n\nint main()\n{\n std::cin.tie(nullptr);\n std::ios_base::sync_with_stdio(false);\n int n, e, q;\n cin >> n >> e >> q;\n\n Dinic dn(n);\n map<pair<int, int>, pair<int, int>> mp;\n rep(i, e) {\n int f, t;\n cin >> f >> t;\n f--, t--;\n if (f > t) swap(f, t);\n dn.addedge(f, t, 1);\n mp[{f, t}] = { dn.to[t].back().rev, dn.to[f].back().rev };\n }\n\n vi<int> ans;\n int flow = dn.maxflow(0, n - 1, n);\n rep(i, q) {\n int m, a, b;\n cin >> m >> a >> b;\n a--, b--;\n if (a > b) swap(a, b);\n if (m == 1) {\n if (!mp.count({ a, b })) {\n dn.addedge(a, b, 1);\n mp[{a, b}] = { dn.to[b].back().rev, dn.to[a].back().rev };\n }\n else {\n dn.to[a][mp[{a, b}].first].cap = 1;\n dn.to[b][mp[{a, b}].second].cap = 1;\n }\n flow += dn.maxflow(0, n - 1, n);\n }\n else {\n if (dn.to[a][mp[{a, b}].first].cap == 0) {\n int tf = dn.maxflow(a, b, 1);\n if (tf == 0) {\n dn.maxflow(n - 1, b, 1);\n dn.maxflow(a, 0, 1);\n flow--;\n }\n }\n else if (dn.to[b][mp[{a, b}].second].cap == 0) {\n int tf = dn.maxflow(b, a, 1);\n if (tf == 0) {\n dn.maxflow(n - 1, a, 1);\n dn.maxflow(b, 0, 1);\n flow--;\n }\n }\n \n dn.to[a][mp[{a, b}].first].cap = 0;\n dn.to[b][mp[{a, b}].second].cap = 0; \n }\n\n ans.push_back(flow);\n }\n\n for (auto elem : ans) cout << elem << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 270, "memory_kb": 5576, "score_of_the_acc": -0.5506, "final_rank": 5 }, { "submission_id": "aoj_2313_9375839", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <map>\n#include <queue>\n#include <set>\nusing namespace std;\ntypedef long long ll;\n#define rep(i, n) for(int i = 0; i < (n); i++)\n\ntemplate<class T>\nusing vi = vector<T>;\n\ntemplate<class T>\nusing vii = vector<vi<T>>;\n\ntemplate<class T>\nusing viii = vector<vii<T>>;\n\n\nstruct Dinic {\nprivate:\n long long inf = 1e18;\n vector<int> dist; //sからの距離\n vector<int> iter; //どこまで調べたか\npublic:\n struct edge {\n int to, rev; long long cap;\n edge(int t = 0, int r = 0, long long c = 0LL) : to(t), rev(r), cap(c) {}\n };\n\n int n;\n vector<vector<edge>> to;\n\n Dinic(int n_ = 1) : n(n_), to(n_), dist(n_), iter(n_) {}\n\n void addedge(int u, int v, long long cap) {//u->vにcapの容量\n int su = (int)to[u].size(), sv = (int)to[v].size();\n to[u].push_back({ v, sv, cap });\n to[v].push_back({ u, su, cap });\n }\n\n long long dfs(int v, int t, long long f) { //t: 終点, f: flow\n if (v == t) return f;\n int sv = (int)to[v].size();\n for (int& i = iter[v]; i < sv; i++) {\n edge& nv = to[v][i];\n if (dist[nv.to] <= dist[v] || nv.cap == 0) continue; //here\n long long nf = dfs(nv.to, t, min(f, nv.cap));\n if (nf > 0) {\n nv.cap -= nf;\n to[nv.to][nv.rev].cap += nf;\n return nf;\n }\n }\n return 0;\n }\n\n void bfs(int s) {\n dist.assign(n, -1);\n queue<int> q;\n q.push(s);\n dist[s] = 0;\n while (!q.empty()) {\n int v = q.front();\n q.pop();\n for (auto nv : to[v]) {\n if (nv.cap > 0 && dist[nv.to] < 0) {\n dist[nv.to] = dist[v] + 1;\n q.push(nv.to);\n }\n }\n }\n }\n\n long long maxflow(int s, int t) {//s:始点, t:終点\n long long res = 0, flow = 0;\n while (true) {\n bfs(s);\n if (dist[t] < 0) break;\n\n iter.assign(n, 0);\n while (flow = dfs(s, t, inf)) res += flow;\n };\n return res;\n }\n};\n\n\nint main()\n{\n std::cin.tie(nullptr);\n std::ios_base::sync_with_stdio(false);\n int n, e, q;\n cin >> n >> e >> q;\n\n set<pair<int, int>> st;\n rep(i, e) {\n int f, t;\n cin >> f >> t;\n f--, t--;\n if (f > t) swap(f, t);\n st.insert({ f, t });\n }\n\n vi<int> ans;\n Dinic dn(n);\n for (auto elem : st) dn.addedge(elem.first, elem.second, 1);\n int flow = dn.maxflow(0, n - 1);\n rep(i, q) {\n int m, a, b;\n cin >> m >> a >> b;\n a--, b--;\n if (a > b) swap(a, b);\n if (m == 1) {\n st.insert({ a, b });\n dn.addedge(a, b, 1);\n flow += dn.maxflow(0, n - 1);\n }\n else {\n st.erase({ a, b });\n Dinic nxt(n);\n swap(dn, nxt);\n for (auto elem : st) dn.addedge(elem.first, elem.second, 1);\n flow = dn.maxflow(0, n - 1);\n }\n\n ans.push_back(flow);\n }\n\n for (auto elem : ans) cout << elem << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 1930, "memory_kb": 6168, "score_of_the_acc": -1.5413, "final_rank": 11 }, { "submission_id": "aoj_2313_9223435", "code_snippet": "#include <bits/stdc++.h>\n\nstruct Flow {\n int n;\n int cur_flow;\n std::vector<std::vector<bool>> ord, rev, exist;\n std::vector<bool> used;\n Flow(int n): n(n), cur_flow(0), ord(n, std::vector<bool>(n)), rev(n, std::vector<bool>(n)), exist(n, std::vector<bool>(n)), used(n) {}\n void add_edge(int u, int v) {\n assert(!exist[u][v]);\n assert(!exist[v][u]);\n exist[u][v] = exist[v][u] = true;\n ord[u][v] = ord[v][u] = true;\n rev[u][v] = rev[v][u] = false;\n }\n\n // {n - 1} -> u -> v -> 1\n void get_path(int u, int v) {\n assert(rev[u][v]);\n rev[u][v] = false;\n ord[v][u] = true;\n auto dfs = [&](auto self, int u, int sink) -> bool {\n if (u == sink) return true;\n used[u] = true;\n for (int v = 0; v < n; v++) {\n if (exist[u][v] && rev[u][v] && !used[v]) {\n if (self(self, v, sink)) {\n rev[u][v] = false;\n ord[v][u] = true;\n return true;\n }\n }\n }\n return false;\n };\n used.assign(n, false);\n dfs(dfs, n - 1, u);\n used.assign(n, false);\n dfs(dfs, v, 0);\n }\n\n void delete_edge(int u, int v) {\n assert(exist[u][v]);\n assert(exist[v][u]);\n if ((ord[u][v] && ord[v][u]) || (rev[u][v] && rev[v][u])) {\n exist[u][v] = exist[v][u] = false;\n ord[u][v] = ord[v][u] = false;\n rev[u][v] = rev[v][u] = false;\n return;\n }\n // {n - 1} -> 0;\n (rev[u][v] ? get_path(u, v) : get_path(v, u));\n exist[u][v] = exist[v][u] = false;\n ord[u][v] = ord[v][u] = false;\n cur_flow--;\n }\n int flow() {\n int res = 0;\n while (true) {\n used.assign(n, false);\n int delta = dfs(0);\n if (delta == 0) break;\n res += delta;\n }\n return cur_flow += res;\n }\n int dfs(int u) {\n if (u == n - 1) return 1;\n used[u] = true;\n for (int v = 0; v < n; v++) {\n if (used[v]) continue;\n if (!exist[u][v]) continue;\n if (ord[u][v] || rev[u][v]) {\n if (dfs(v)) {\n if (ord[u][v]) {\n ord[u][v] = false;\n rev[v][u] = true;\n } else {\n rev[u][v] = false;\n ord[v][u] = true;\n }\n return 1;\n }\n }\n }\n return 0;\n }\n};\n\nint main() {\n int n, m, q;\n std::cin >> n >> m >> q;\n Flow flow(n);\n for (int i = 0; i < m; i++) {\n int u, v;\n std::cin >> u >> v;\n u--; v--;\n flow.add_edge(u, v);\n }\n while (q--) {\n int m, u, v;\n std::cin >> m >> u >> v;\n u--; v--;\n if (m == 1) {\n flow.add_edge(u, v);\n } else {\n flow.delete_edge(u, v);\n }\n std::cout << flow.flow() << '\\n';\n }\n}", "accuracy": 1, "time_ms": 400, "memory_kb": 3632, "score_of_the_acc": -0.204, "final_rank": 1 }, { "submission_id": "aoj_2313_8738830", "code_snippet": "#include <bits/stdc++.h>\n\n\n\n\nnamespace zawa {\n\nusing i16 = std::int16_t;\nusing i32 = std::int32_t;\nusing i64 = std::int64_t;\nusing i128 = __int128_t;\n\nusing u8 = std::uint8_t;\nusing u16 = std::uint16_t;\nusing u32 = std::uint32_t;\nusing u64 = std::uint64_t;\n\nusing usize = std::size_t;\n\n} // namespace zawa\n\n\nnamespace zawa {\n\nvoid SetFastIO() {\n std::cin.tie(nullptr)->sync_with_stdio(false);\n}\n\nvoid SetPrecision(u32 dig) {\n std::cout << std::fixed << std::setprecision(dig);\n}\n\n} // namespace zawa\nusing namespace zawa;\n\nstruct E {\n bool f{}, r{};\n E() = default;\n E(bool f, bool r) : f{f}, r{r} {}\n void flow() {\n f ^= 1;\n r ^= 1;\n }\n friend std::ostream& operator<<(std::ostream& os, const E& e) {\n if (!e.f and !e.r) os << 0;\n else if (e.f) os << 1;\n else os << 2;\n return os;\n }\n};\n\nconst int N{510};\nE g[N][N];\n\nint n, m, q;\n\nint through(int u, int v) {\n if (g[v][u].r) return 2;\n else if (g[u][v].f) return 1;\n else return 0;\n}\n\nbool dfsflow(int v, int t, std::vector<bool>& vis) {\n vis[v] = true;\n if (v == t) return true;\n for (int i{} ; i < n ; i++) if (!vis[i]) {\n int e{through(v, i)};\n if (e == 0) continue;\n if (!dfsflow(i, t, vis)) continue;\n if (e == 1) g[v][i].flow();\n else g[i][v].flow();\n return true;\n }\n return false;\n}\n\nbool dfselim(int v, int t, std::vector<bool>& vis) {\n vis[v] = true;\n if (v == t) return true;\n for (int i{} ; i < n ; i++) if (!vis[i]) {\n int e{through(v, i)};\n // std::cout << '(' << v << ',' << i << ')' << \" is \" << e << std::endl;\n if (e != 2) continue;\n if (!dfselim(i, t, vis)) continue;\n g[i][v].flow();\n return true;\n }\n return false;\n}\n\nbool flow(int s, int t) {\n std::vector<bool> vis(n);\n return dfsflow(s, t, vis);\n}\n\nbool elim(int s, int t) {\n // std::cerr << \"eliminate \" << s << ' ' << t << std::endl;\n std::vector<bool> vis(n);\n return dfselim(s, t, vis);\n}\n\nint main() {\n SetFastIO();\n\n std::cin >> n >> m >> q;\n for (int i{} ; i < n ; i++) for (int j{} ; j < n ; j++) g[i][j] = E{};\n for (int _{} ; _ < m ; _++) {\n int f, t; std::cin >> f >> t;\n f--; t--;\n g[f][t].f = true;\n g[t][f].f = true;\n }\n\n int ans{};\n while (flow(0, n - 1)) ans++;\n\n for (int _{} ; _ < q ; _++) {\n int t, a, b; std::cin >> t >> a >> b;\n // std::cout << t << ' ' << a << ' ' << b << std::endl;\n a--; b--;\n if (t == 1) {\n assert(!g[a][b].f and !g[b][a].f);\n assert(!g[a][b].r and !g[b][a].r);\n g[a][b].f = true;\n g[b][a].f = true;\n ans += flow(0, n - 1);\n }\n else if (t == 2) {\n if (g[a][b].f and g[b][a].f) {\n // std::cerr << \"type \" << 1 << std::endl;\n ;\n }\n else if (g[a][b].r and g[b][a].r) {\n // std::cerr << \"type \" << 2 << std::endl;\n g[a][b].flow();\n g[b][a].flow();\n }\n else if (g[a][b].r) {\n if (!flow(a, b)) {\n assert(elim(n - 1, b));\n g[a][b].flow();\n assert(elim(a, 0));\n ans--;\n }\n else {\n g[a][b].flow();\n }\n }\n else if (g[b][a].r) {\n // std::cerr << \"type \" << 4 << std::endl;\n if (!flow(b, a)) {\n assert(elim(n - 1, a));\n g[b][a].flow();\n assert(elim(b, 0));\n ans--;\n }\n else {\n g[b][a].flow();\n }\n }\n else {\n assert(!\"aaa\");\n }\n assert(g[a][b].f and g[b][a].f);\n assert(!g[a][b].r and !g[a][b].r);\n g[a][b].f = g[b][a].f = false;\n }\n else {\n assert(false);\n }\n std::cout << ans << std::endl;\n }\n}", "accuracy": 1, "time_ms": 340, "memory_kb": 3916, "score_of_the_acc": -0.2333, "final_rank": 2 }, { "submission_id": "aoj_2313_8738734", "code_snippet": "#include <bits/stdc++.h>\n\n\n\n\nnamespace zawa {\n\nusing i16 = std::int16_t;\nusing i32 = std::int32_t;\nusing i64 = std::int64_t;\nusing i128 = __int128_t;\n\nusing u8 = std::uint8_t;\nusing u16 = std::uint16_t;\nusing u32 = std::uint32_t;\nusing u64 = std::uint64_t;\n\nusing usize = std::size_t;\n\n} // namespace zawa\n\n\nnamespace zawa {\n\nvoid SetFastIO() {\n std::cin.tie(nullptr)->sync_with_stdio(false);\n}\n\nvoid SetPrecision(u32 dig) {\n std::cout << std::fixed << std::setprecision(dig);\n}\n\n} // namespace zawa\nusing namespace zawa;\n\nstruct E {\n bool exist;\n bool f, revf;\n E() : exist{false}, f{false}, revf{false} {}\n void on() {\n assert(!exist);\n exist = f = true;\n }\n void off() {\n assert(exist);\n exist = f = revf = false;\n }\n void flip() {\n f ^= 1;\n revf ^= 1;\n }\n bool flowed() const {\n return exist and !f;\n }\n};\n\nenum state {\n uv,\n vu,\n cannot, \n};\n\nint main() {\n SetFastIO();\n\n int n, m, q; std::cin >> n >> m >> q;\n std::vector g(n, std::vector<E>(n));\n \n for (int _{} ; _ < m ; _++) {\n int a, b; std::cin >> a >> b;\n a--; b--;\n g[a][b].on();\n g[b][a].on();\n }\n\n auto can{[&](int u, int v) -> state {\n if (g[v][u].exist and g[v][u].revf) return vu;\n else if (g[u][v].exist and g[u][v].f) return uv;\n else return cannot;\n }};\n\n auto flow{[&](int s, int t) -> bool {\n std::vector<bool> vis(n); \n auto dfs{[&](auto dfs, int v) -> bool {\n vis[v] = true;\n if (v == t) return true;\n for (int x{} ; x < n ; x++) if (!vis[x]) {\n auto e{can(v, x)}; \n if (e == cannot) continue;\n if (dfs(dfs, x)) {\n if (e == uv) g[v][x].flip();\n else if (e == vu) g[x][v].flip();\n return true;\n }\n }\n return false;\n }};\n return dfs(dfs, s);\n }};\n\n int ans{};\n while (flow(0, n - 1)) ans++;\n\n for (int _{} ; _ < q ; _++) {\n int t, a, b; std::cin >> t >> a >> b;\n a--; b--;\n if (t == 1) {\n g[a][b].on();\n g[b][a].on();\n ans += flow(0, n - 1);\n }\n else if (t == 2) {\n assert(g[a][b].exist and g[b][a].exist);\n if (!g[a][b].flowed() and !g[b][a].flowed()) {\n ;\n }\n else if (g[a][b].flowed() and g[b][a].flowed()) {\n ;\n }\n else if (g[a][b].flowed()) {\n if (!flow(a, b)) {\n ans--;\n flow(n - 1, b);\n g[a][b].flip();\n flow(a, 0);\n }\n } \n else if (g[b][a].flowed()) {\n if (!flow(b, a)) {\n ans--;\n flow(n - 1, a);\n g[b][a].flip();\n flow(b, 0);\n }\n }\n else {\n assert(false);\n }\n g[a][b].off();\n g[b][a].off();\n }\n else {\n assert(false);\n }\n std::cout << ans << '\\n';\n }\n}", "accuracy": 0.15517241379310345, "time_ms": 210, "memory_kb": 3700, "score_of_the_acc": -0.1195, "final_rank": 17 }, { "submission_id": "aoj_2313_8738729", "code_snippet": "#include <bits/stdc++.h>\n\n\n\n\nnamespace zawa {\n\nusing i16 = std::int16_t;\nusing i32 = std::int32_t;\nusing i64 = std::int64_t;\nusing i128 = __int128_t;\n\nusing u8 = std::uint8_t;\nusing u16 = std::uint16_t;\nusing u32 = std::uint32_t;\nusing u64 = std::uint64_t;\n\nusing usize = std::size_t;\n\n} // namespace zawa\n\n\nnamespace zawa {\n\nvoid SetFastIO() {\n std::cin.tie(nullptr)->sync_with_stdio(false);\n}\n\nvoid SetPrecision(u32 dig) {\n std::cout << std::fixed << std::setprecision(dig);\n}\n\n} // namespace zawa\nusing namespace zawa;\n\nstruct E {\n bool exist;\n bool f, revf;\n E() : exist{false}, f{false}, revf{false} {}\n void on() {\n assert(!exist);\n exist = f = true;\n }\n void off() {\n assert(exist);\n exist = f = revf = false;\n }\n void flip() {\n f ^= 1;\n revf ^= 1;\n }\n bool flowed() const {\n return exist and !f;\n }\n};\n\nenum state {\n uv,\n vu,\n cannot, \n};\n\nint main() {\n SetFastIO();\n\n int n, m, q; std::cin >> n >> m >> q;\n std::vector g(n, std::vector<E>(n));\n \n for (int _{} ; _ < m ; _++) {\n int a, b; std::cin >> a >> b;\n a--; b--;\n g[a][b].on();\n g[b][a].on();\n }\n\n auto can{[&](int u, int v) -> state {\n if (g[v][u].exist and g[v][u].revf) return vu;\n else if (g[u][v].exist and g[u][v].f) return uv;\n else return cannot;\n }};\n\n auto flow{[&](int s, int t) -> bool {\n std::vector<bool> vis(n); \n auto dfs{[&](auto dfs, int v) -> bool {\n vis[v] = true;\n if (v == t) return true;\n for (int x{} ; x < n ; x++) if (!vis[x]) {\n auto e{can(v, x)}; \n if (e == cannot) continue;\n if (dfs(dfs, x)) {\n if (e == uv) g[v][x].flip();\n else if (e == vu) g[x][v].flip();\n return true;\n }\n }\n return false;\n }};\n return dfs(dfs, s);\n }};\n\n int ans{};\n while (flow(0, n - 1)) ans++;\n\n for (int _{} ; _ < q ; _++) {\n int t, a, b; std::cin >> t >> a >> b;\n a--; b--;\n if (t == 1) {\n g[a][b].on();\n g[b][a].on();\n ans += flow(0, n - 1);\n }\n else if (t == 2) {\n assert(g[a][b].exist and g[b][a].exist);\n if (!g[a][b].flowed() and !g[b][a].flowed()) {\n ;\n }\n else if (g[a][b].flowed() and g[b][a].flowed()) {\n ;\n }\n else if (g[a][b].flowed()) {\n if (!flow(a, b)) {\n ans--;\n assert(flow(n - 1, b));\n assert(g[a][b].flowed());\n g[a][b].flip();\n assert(flow(a, 0));\n }\n } \n else if (g[b][a].flowed()) {\n if (!flow(b, a)) {\n ans--;\n assert(flow(n - 1, a));\n assert(g[b][a].flowed());\n g[b][a].flip();\n assert(flow(b, 0));\n }\n }\n else {\n assert(false);\n }\n g[a][b].off();\n g[b][a].off();\n }\n else {\n assert(false);\n }\n std::cout << ans << '\\n';\n }\n}", "accuracy": 0.15517241379310345, "time_ms": 210, "memory_kb": 3760, "score_of_the_acc": -0.1323, "final_rank": 18 }, { "submission_id": "aoj_2313_8738718", "code_snippet": "#include <bits/stdc++.h>\n\n\n\n\nnamespace zawa {\n\nusing i16 = std::int16_t;\nusing i32 = std::int32_t;\nusing i64 = std::int64_t;\nusing i128 = __int128_t;\n\nusing u8 = std::uint8_t;\nusing u16 = std::uint16_t;\nusing u32 = std::uint32_t;\nusing u64 = std::uint64_t;\n\nusing usize = std::size_t;\n\n} // namespace zawa\n\n\nnamespace zawa {\n\nvoid SetFastIO() {\n std::cin.tie(nullptr)->sync_with_stdio(false);\n}\n\nvoid SetPrecision(u32 dig) {\n std::cout << std::fixed << std::setprecision(dig);\n}\n\n} // namespace zawa\nusing namespace zawa;\n\nstruct E {\n bool exist;\n bool f, revf;\n E() : exist{false}, f{false}, revf{false} {}\n void on() {\n assert(!exist);\n exist = f = true;\n }\n void off() {\n assert(exist);\n exist = f = revf = false;\n }\n void flip() {\n f ^= 1;\n revf ^= 1;\n }\n bool flowed() const {\n return exist and !f;\n }\n};\n\nenum state {\n uv,\n vu,\n cannot, \n};\n\nint main() {\n SetFastIO();\n\n int n, m, q; std::cin >> n >> m >> q;\n std::vector g(n, std::vector<E>(n));\n \n for (int _{} ; _ < m ; _++) {\n int a, b; std::cin >> a >> b;\n a--; b--;\n g[a][b].on();\n g[b][a].on();\n }\n\n auto can{[&](int u, int v) -> state {\n if (g[v][u].exist and g[v][u].revf) return vu;\n else if (g[u][v].exist and g[u][v].f) return uv;\n else return cannot;\n }};\n\n auto flow{[&](int s, int t) -> bool {\n std::vector<bool> vis(n); \n auto dfs{[&](auto dfs, int v) -> bool {\n vis[v] = true;\n if (v == t) return true;\n for (int x{} ; x < n ; x++) if (!vis[x]) {\n auto e{can(v, x)}; \n if (e == cannot) continue;\n if (dfs(dfs, x)) {\n if (e == uv) g[v][x].flip();\n else if (e == vu) g[x][v].flip();\n return true;\n }\n }\n return false;\n }};\n return dfs(dfs, s);\n }};\n\n int ans{};\n while (flow(0, n - 1)) ans++;\n\n for (int _{} ; _ < q ; _++) {\n int t, a, b; std::cin >> t >> a >> b;\n a--; b--;\n if (t == 1) {\n g[a][b].on();\n g[b][a].on();\n ans += flow(0, n - 1);\n }\n else if (t == 2) {\n assert(g[a][b].exist and g[b][a].exist);\n if (!g[a][b].flowed() and !g[b][a].flowed()) {\n ;\n }\n else if (g[a][b].flowed() and g[b][a].flowed()) {\n ;\n }\n else if (g[a][b].flowed()) {\n if (!flow(a, b)) {\n ans--;\n assert(flow(n - 1, b));\n g[a][b].flip();\n assert(flow(a, 0));\n }\n } \n else if (g[b][a].flowed()) {\n if (!flow(b, a)) {\n ans--;\n assert(flow(n - 1, a));\n g[b][a].flip();\n assert(flow(b, 0));\n }\n }\n else {\n assert(false);\n }\n g[a][b].off();\n g[b][a].off();\n }\n else {\n assert(false);\n }\n std::cout << ans << '\\n';\n }\n}", "accuracy": 0.15517241379310345, "time_ms": 200, "memory_kb": 3704, "score_of_the_acc": -0.1152, "final_rank": 16 }, { "submission_id": "aoj_2313_8738712", "code_snippet": "#include <bits/stdc++.h>\n\n\n\n\nnamespace zawa {\n\nusing i16 = std::int16_t;\nusing i32 = std::int32_t;\nusing i64 = std::int64_t;\nusing i128 = __int128_t;\n\nusing u8 = std::uint8_t;\nusing u16 = std::uint16_t;\nusing u32 = std::uint32_t;\nusing u64 = std::uint64_t;\n\nusing usize = std::size_t;\n\n} // namespace zawa\n\n\nnamespace zawa {\n\nvoid SetFastIO() {\n std::cin.tie(nullptr)->sync_with_stdio(false);\n}\n\nvoid SetPrecision(u32 dig) {\n std::cout << std::fixed << std::setprecision(dig);\n}\n\n} // namespace zawa\nusing namespace zawa;\n\nstruct E {\n bool exist;\n bool f, revf;\n E() : exist{false}, f{false}, revf{false} {}\n void on() {\n assert(!exist);\n exist = f = true;\n }\n void off() {\n assert(exist);\n exist = f = revf = false;\n }\n void flip() {\n f ^= 1;\n revf ^= 1;\n }\n bool flowed() const {\n return exist and !f;\n }\n};\n\nenum state {\n uv,\n vu,\n cannot, \n};\n\nint main() {\n SetFastIO();\n\n int n, m, q; std::cin >> n >> m >> q;\n std::vector g(n, std::vector<E>(n));\n \n for (int _{} ; _ < m ; _++) {\n int a, b; std::cin >> a >> b;\n a--; b--;\n g[a][b].on();\n g[b][a].on();\n }\n\n auto can{[&](int u, int v) -> state {\n if (g[u][v].exist and g[u][v].f) return uv;\n else if (g[v][u].exist and g[v][u].revf) return vu;\n else return cannot;\n }};\n\n auto flow{[&](int s, int t) -> bool {\n std::vector<bool> vis(n); \n auto dfs{[&](auto dfs, int v) -> bool {\n vis[v] = true;\n if (v == t) return true;\n for (int x{} ; x < n ; x++) if (!vis[x]) {\n auto e{can(v, x)}; \n if (e == cannot) continue;\n if (dfs(dfs, x)) {\n if (e == uv) g[v][x].flip();\n else if (e == vu) g[x][v].flip();\n return true;\n }\n }\n return false;\n }};\n return dfs(dfs, s);\n }};\n\n int ans{};\n while (flow(0, n - 1)) ans++;\n\n for (int _{} ; _ < q ; _++) {\n int t, a, b; std::cin >> t >> a >> b;\n a--; b--;\n if (t == 1) {\n g[a][b].on();\n g[b][a].on();\n ans += flow(0, n - 1);\n }\n else if (t == 2) {\n assert(g[a][b].exist and g[b][a].exist);\n if (!g[a][b].flowed() and !g[b][a].flowed()) {\n ;\n }\n else if (g[a][b].flowed() and g[b][a].flowed()) {\n ;\n }\n else if (g[a][b].flowed()) {\n if (!flow(a, b)) {\n ans--;\n assert(flow(n - 1, b));\n g[a][b].flip();\n assert(flow(a, 0));\n }\n } \n else if (g[b][a].flowed()) {\n if (!flow(b, a)) {\n ans--;\n assert(flow(n - 1, a));\n g[b][a].flip();\n assert(flow(b, 0));\n }\n }\n else {\n assert(false);\n }\n g[a][b].off();\n g[b][a].off();\n }\n else {\n assert(false);\n }\n std::cout << ans << '\\n';\n }\n}", "accuracy": 0.15517241379310345, "time_ms": 240, "memory_kb": 3740, "score_of_the_acc": -0.1437, "final_rank": 20 }, { "submission_id": "aoj_2313_8738223", "code_snippet": "#include <bits/stdc++.h>\n\n\n\n\nnamespace zawa {\n\nusing i16 = std::int16_t;\nusing i32 = std::int32_t;\nusing i64 = std::int64_t;\nusing i128 = __int128_t;\n\nusing u8 = std::uint8_t;\nusing u16 = std::uint16_t;\nusing u32 = std::uint32_t;\nusing u64 = std::uint64_t;\n\nusing usize = std::size_t;\n\n} // namespace zawa\n\n\nnamespace zawa {\n\nvoid SetFastIO() {\n std::cin.tie(nullptr)->sync_with_stdio(false);\n}\n\nvoid SetPrecision(u32 dig) {\n std::cout << std::fixed << std::setprecision(dig);\n}\n\n} // namespace zawa\nusing namespace zawa;\n\nstruct E {\n bool exist;\n bool f, revf;\n E() : exist{false}, f{false}, revf{false} {}\n void on() {\n assert(!exist);\n exist = f = true;\n }\n void off() {\n assert(exist);\n exist = f = revf = false;\n }\n void flip() {\n f ^= 1;\n revf ^= 1;\n }\n bool flowed() const {\n return exist and !f;\n }\n};\n\nenum state {\n uv,\n vu,\n cannot, \n};\n\nint main() {\n SetFastIO();\n\n int n, m, q; std::cin >> n >> m >> q;\n std::vector g(n, std::vector<E>(n));\n \n for (int _{} ; _ < m ; _++) {\n int a, b; std::cin >> a >> b;\n a--; b--;\n g[a][b].on();\n g[b][a].on();\n }\n\n auto can{[&](int u, int v) -> state {\n if (g[u][v].exist and g[u][v].f) return uv;\n else if (g[v][u].exist and g[v][u].revf) return vu;\n else return cannot;\n }};\n\n auto flow{[&](int s, int t) -> bool {\n std::vector<bool> vis(n); \n auto dfs{[&](auto dfs, int v) -> bool {\n vis[v] = true;\n if (v == t) return true;\n for (int x{} ; x < n ; x++) if (!vis[x]) {\n auto e{can(v, x)}; \n if (e == cannot) continue;\n if (dfs(dfs, x)) {\n if (e == uv) g[v][x].flip();\n else if (e == vu) g[x][v].flip();\n return true;\n }\n }\n return false;\n }};\n return dfs(dfs, s);\n }};\n\n int ans{};\n while (flow(0, n - 1)) ans++;\n\n for (int _{} ; _ < q ; _++) {\n int t, a, b; std::cin >> t >> a >> b;\n a--; b--;\n if (t == 1) {\n g[a][b].on();\n g[b][a].on();\n ans += flow(0, n - 1);\n }\n else if (t == 2) {\n assert(g[a][b].exist and g[b][a].exist);\n if (!(g[a][b].flowed() ^ g[b][a].flowed())) {\n ;\n }\n else if (g[a][b].flowed()) {\n if (!flow(a, b)) {\n ans--;\n assert(flow(n - 1, b));\n g[a][b].flip();\n assert(flow(a, 0));\n }\n } \n else if (g[b][a].flowed()) {\n if (!flow(b, a)) {\n ans--;\n assert(flow(n - 1, a));\n g[b][a].flip();\n assert(flow(b, 0));\n }\n }\n else {\n assert(false);\n }\n g[a][b].off();\n g[b][a].off();\n }\n else {\n assert(false);\n }\n std::cout << ans << '\\n';\n }\n}", "accuracy": 0.15517241379310345, "time_ms": 240, "memory_kb": 3704, "score_of_the_acc": -0.136, "final_rank": 19 }, { "submission_id": "aoj_2313_8363602", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\ntemplate<class T>\nbool chmin(T &a, const T &b) {if (a > b) {a = b; return 1;} return 0;}\n\nusing ll = long long;\nusing ld = long double;\n\nbool is_end = false;\n\n/*\n目的:max flow を求める\n特徴:max_flow を呼び出したのちに、add_edge が可能\n(max_flow は「前回の状態からどれだけ流せるか」を返すので、flow を流す場所 s, t を変えるとおかしな結果になる)\n*/\ntemplate<class flow_t = long long>\nstruct FordFulkerson\n{\n\t\n\tint siz;\n\tvector<vector<flow_t>> graph;\n\tvector<bool> used;\n\t\n\tFordFulkerson(int n) : siz(n), graph(n, vector<flow_t>(n, 0)), used(siz, false) { }\n\t\n\tvoid add_edge(int from, int to, flow_t cap)\n\t{\n\t\tassert(from < siz && to < siz);\n\t\tgraph[from][to] += cap;\n\t}\n\t\n\tflow_t dfs(int v, int target, flow_t flow)\n\t{\n\t\tif (v == target) {return flow;}\n\t\tused[v] = true;\n\t\t\n\t\tfor (int to = 0; to < siz; ++to)\n\t\t{\n\t\t\tflow_t cap = graph[v][to];\n\t\t\tif (cap > 0 && !used[to])\n\t\t\t{\n\t\t\t\tflow_t f = dfs(to, target, min(flow, cap));\n\t\t\t\tif (f > 0)\n\t\t\t\t{\n\t\t\t\t\tgraph[v][to] -= f;\n\t\t\t\t\tgraph[to][v] += f;\n\t\t\t\t\treturn f;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn 0;\n\t}\n\t\n\t// max flow on s -> t\n\tflow_t max_flow(int s, int t)\n\t{\n\t\tflow_t flow = 0;\n\t\tflow_t INF = 1e9; // INF の上限値に注意\n\t\twhile (true)\n\t\t{\n\t\t\tused.assign(siz, false);\n\t\t\tflow_t f = dfs(s, t, INF);\n\t\t\tflow += f;\n\t\t\t\n\t\t\tif (f == 0) {return flow;}\n\t\t}\n\t\t// 絶対に到達しないはず\n\t\treturn 0;\n\t}\n\t\n\tflow_t dfs2(int v, int target, flow_t flow)\n\t{\n\t\tif (v == target) {return flow;}\n\t\tused[v] = true;\n\t\t\n\t\tfor (int to = 0; to < siz; ++to)\n\t\t{\n\t\t\tflow_t cap = graph[v][to];\n\t\t\tif (graph[v][to] - graph[to][v] > 0 && !used[to])\n\t\t\t{\n\t\t\t\tflow_t f = dfs2(to, target, min(flow, cap));\n\t\t\t\tif (f > 0)\n\t\t\t\t{\n\t\t\t\t\tgraph[v][to] -= f;\n\t\t\t\t\tgraph[to][v] += f;\n\t\t\t\t\treturn f;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn 0;\n\t}\n\t\n\tflow_t limit_flow(int s, int t, int limit)\n\t{\n\t\tflow_t flow = 0;\n\t\twhile (true)\n\t\t{\n\t\t\tused.assign(siz, false);\n\t\t\tflow_t f = dfs(s, t, limit);\n\t\t\tflow += f;\n\t\t\tlimit -= f;\n\t\t\t\n\t\t\tif (f == 0) {return flow;}\n\t\t}\n\t\treturn 0;\n\t}\n\t\n\tvoid loosen(int s, int t)\n\t{\n\t\tused.assign(siz, false);\n\t\t\n\t\tvector<vector<bool>> isneed(siz, vector<bool>(siz, false));\n\t\tqueue<int> que;\n\t\tque.emplace(s);\n\t\tused[s] = true;\n\t\t\n\t\twhile (!que.empty())\n\t\t{\n\t\t\tint v = que.front();\n\t\t\tque.pop();\n\t\t\t\n\t\t\tfor (int nv = 0; nv < siz; ++nv)\n\t\t\t{\n\t\t\t\tif (graph[v][nv] < graph[nv][v])\n\t\t\t\t{\n\t\t\t\t\tisneed[v][nv] = isneed[nv][v] = true;\n\t\t\t\t\tif (used[nv]) {continue;}\n\t\t\t\t\tused[nv] = true;\n\t\t\t\t\tque.emplace(nv);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\t\n\t\tfor (int i = 0; i < siz; ++i)\n\t\t{\n\t\t\tfor (int j = i; j < siz; ++j)\n\t\t\t{\n\t\t\t\tif (isneed[i][j]) continue;\n\t\t\t\tint mid = (graph[i][j] + graph[j][i]) / 2;\n\t\t\t\tgraph[i][j] = graph[j][i] = mid;\n\t\t\t}\n\t\t}\n\t\t\n\t\treturn;\n\t}\n\t\n};\n\nvoid output(FordFulkerson<int> &g)\n{\n\tint n = g.siz;\n\t\n\tcout << \"cap\" << endl;\n\tfor (int i = 0; i < n; ++i)\n\t{\n\t\tfor (int j = 0; j < n; ++j)\n\t\t{\n\t\t\tcout << g.graph[i][j] << \" \";\n\t\t}\n\t\tcout << endl;\n\t}\n\t\n\tcout << \"flow\" << endl;\n\tfor (int i = 0; i < n; ++i)\n\t{\n\t\tfor (int j = 0; j < n; ++j)\n\t\t{\n\t\t\tint f = (g.graph[j][i]) / 2;\n\t\t\tcout << f << \" \";\n\t\t}\n\t\tcout << endl;\n\t}\n\t\n\tcout << endl;\n\t\n\treturn;\n}\n\nvoid calc()\n{\n\tint N, E, Q; cin >> N >> E >> Q;\n\tFordFulkerson<int> g(N);\n\t\n\tfor (int i = 0; i < E; ++i)\n\t{\n\t\tint f, t; cin >> f >> t;\n\t\tf--, t--;\n\t\tg.add_edge(f, t, 1);\n\t\tg.add_edge(t ,f, 1);\n\t}\n\t\n\tint S = 0, T = N-1;\n\tint sum = g.max_flow(S, T);\n\t\n\tfor (int q = 0; q < Q; ++q)\n\t{\n\t\tint m, a, b; cin >> m >> a >> b;\n\t\ta--, b--;\n\t\t\n\t\tif (m == 1)\n\t\t{\n\t\t\t// connect (a, b)\n\t\t\tg.add_edge(a, b, 1);\n\t\t\tg.add_edge(b, a, 1);\n\t\t\tsum += g.max_flow(S, T);\n\t\t}\n\t\telse\n\t\t{\n\t\t\tassert(g.graph[a][b] + g.graph[b][a] > 0);\n\t\t\t// cut (a, b)\n\t\t\tif (g.graph[a][b] == 0)\n\t\t\t{\n\t\t\t\t// S -> a -> b -> T flow exists\n\t\t\t\tg.graph[a][b] = g.graph[b][a] = 0;\n\t\t\t\tint f = g.limit_flow(a, b, 1);\n\t\t\t\t\n\t\t\t\tif (f == 0)\n\t\t\t\t{\n\t\t\t\t\tsum -= 1;\n\t\t\t\t\tf = g.limit_flow(T, b, 1);\n\t\t\t\t\tassert(T == b || f == 1);\n\t\t\t\t\tf = g.limit_flow(a, S, 1);\n\t\t\t\t\tassert(S == a || f == 1);\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\t;\n\t\t\t\t}\n\t\t\t}\n\t\t\telse if (g.graph[b][a] == 0)\n\t\t\t{\n\t\t\t\t// S -> b -> a -> T flow exists\n\t\t\t\tg.graph[a][b] = g.graph[b][a] = 0;\n\t\t\t\tint f = g.limit_flow(b, a, 1);\n\t\t\t\t\n\t\t\t\tif (f == 0)\n\t\t\t\t{\n\t\t\t\t\tsum -= 1;\n\t\t\t\t\tf = g.limit_flow(T, a, 1);\n\t\t\t\t\tassert(T == a || f == 1);\n\t\t\t\t\tf = g.limit_flow(b, S, 1);\n\t\t\t\t\tassert(S == b || f == 1);\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\t;\n\t\t\t\t}\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tg.graph[a][b] = g.graph[b][a] = 0;\n\t\t\t}\n\t\t\t\n\t\t}\n\t\t\n\t\tcout << sum << endl;\n\t\t// g.loosen(S, T);\n\t\t\n\t\t// cout << m << \" \" << a << \" \" << b << endl;\n\t\t// output(g);\n\t\t\n\t}\n\t\n\t\n\treturn;\n}\n\nint main()\n{\n\tcalc();\n\t\n\treturn 0;\n}", "accuracy": 1, "time_ms": 600, "memory_kb": 4456, "score_of_the_acc": -0.4838, "final_rank": 3 }, { "submission_id": "aoj_2313_8363551", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\ntemplate<class T>\nbool chmin(T &a, const T &b) {if (a > b) {a = b; return 1;} return 0;}\n\nusing ll = long long;\nusing ld = long double;\n\nbool is_end = false;\n\n/*\n目的:max flow を求める\n特徴:max_flow を呼び出したのちに、add_edge が可能\n(max_flow は「前回の状態からどれだけ流せるか」を返すので、flow を流す場所 s, t を変えるとおかしな結果になる)\n*/\ntemplate<class flow_t = long long>\nstruct FordFulkerson\n{\n\t\n\tint siz;\n\tvector<vector<flow_t>> graph;\n\tvector<bool> used;\n\t\n\tFordFulkerson(int n) : siz(n), graph(n, vector<flow_t>(n, 0)), used(siz, false) { }\n\t\n\tvoid add_edge(int from, int to, flow_t cap)\n\t{\n\t\tassert(from < siz && to < siz);\n\t\tgraph[from][to] += cap;\n\t}\n\t\n\tflow_t dfs(int v, int target, flow_t flow)\n\t{\n\t\tif (v == target) {return flow;}\n\t\tused[v] = true;\n\t\t\n\t\tfor (int to = 0; to < siz; ++to)\n\t\t{\n\t\t\tflow_t cap = graph[v][to];\n\t\t\tif (cap > 0 && !used[to])\n\t\t\t{\n\t\t\t\tflow_t f = dfs(to, target, min(flow, cap));\n\t\t\t\tif (f > 0)\n\t\t\t\t{\n\t\t\t\t\tgraph[v][to] -= f;\n\t\t\t\t\tgraph[to][v] += f;\n\t\t\t\t\treturn f;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn 0;\n\t}\n\t\n\t// max flow on s -> t\n\tflow_t max_flow(int s, int t)\n\t{\n\t\tflow_t flow = 0;\n\t\tflow_t INF = 1e9; // INF の上限値に注意\n\t\twhile (true)\n\t\t{\n\t\t\tused.assign(siz, false);\n\t\t\tflow_t f = dfs(s, t, INF);\n\t\t\tflow += f;\n\t\t\t\n\t\t\tif (f == 0) {return flow;}\n\t\t}\n\t\t// 絶対に到達しないはず\n\t\treturn 0;\n\t}\n\t\n\tflow_t dfs2(int v, int target, flow_t flow)\n\t{\n\t\tif (v == target) {return flow;}\n\t\tused[v] = true;\n\t\t\n\t\tfor (int to = 0; to < siz; ++to)\n\t\t{\n\t\t\tflow_t cap = graph[v][to];\n\t\t\tif (graph[v][to] - graph[to][v] > 0 && !used[to])\n\t\t\t{\n\t\t\t\tflow_t f = dfs2(to, target, min(flow, cap));\n\t\t\t\tif (f > 0)\n\t\t\t\t{\n\t\t\t\t\tgraph[v][to] -= f;\n\t\t\t\t\tgraph[to][v] += f;\n\t\t\t\t\treturn f;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn 0;\n\t}\n\t\n\tflow_t limit_flow(int s, int t, int limit)\n\t{\n\t\tflow_t flow = 0;\n\t\twhile (true)\n\t\t{\n\t\t\tused.assign(siz, false);\n\t\t\tflow_t f = dfs2(s, t, limit);\n\t\t\tflow += f;\n\t\t\tlimit -= f;\n\t\t\t\n\t\t\tif (f == 0) {return flow;}\n\t\t}\n\t\treturn 0;\n\t}\n\t\n\tvoid loosen(int s, int t)\n\t{\n\t\tused.assign(siz, false);\n\t\t\n\t\tvector<vector<bool>> isneed(siz, vector<bool>(siz, false));\n\t\tqueue<int> que;\n\t\tque.emplace(s);\n\t\tused[s] = true;\n\t\t\n\t\twhile (!que.empty())\n\t\t{\n\t\t\tint v = que.front();\n\t\t\tque.pop();\n\t\t\t\n\t\t\tfor (int nv = 0; nv < siz; ++nv)\n\t\t\t{\n\t\t\t\tif (graph[v][nv] < graph[nv][v])\n\t\t\t\t{\n\t\t\t\t\tisneed[v][nv] = isneed[nv][v] = true;\n\t\t\t\t\tif (used[nv]) {continue;}\n\t\t\t\t\tused[nv] = true;\n\t\t\t\t\tque.emplace(nv);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\t\n\t\tfor (int i = 0; i < siz; ++i)\n\t\t{\n\t\t\tfor (int j = i; j < siz; ++j)\n\t\t\t{\n\t\t\t\tif (isneed[i][j]) continue;\n\t\t\t\tint mid = (graph[i][j] + graph[j][i]) / 2;\n\t\t\t\tgraph[i][j] = graph[j][i] = mid;\n\t\t\t}\n\t\t}\n\t\t\n\t\treturn;\n\t}\n\t\n};\n\nvoid output(FordFulkerson<int> &g)\n{\n\tint n = g.siz;\n\t\n\tcout << \"cap\" << endl;\n\tfor (int i = 0; i < n; ++i)\n\t{\n\t\tfor (int j = 0; j < n; ++j)\n\t\t{\n\t\t\tcout << g.graph[i][j] << \" \";\n\t\t}\n\t\tcout << endl;\n\t}\n\t\n\tcout << \"flow\" << endl;\n\tfor (int i = 0; i < n; ++i)\n\t{\n\t\tfor (int j = 0; j < n; ++j)\n\t\t{\n\t\t\tint f = (g.graph[j][i]) / 2;\n\t\t\tcout << f << \" \";\n\t\t}\n\t\tcout << endl;\n\t}\n\t\n\tcout << endl;\n\t\n\treturn;\n}\n\nvoid calc()\n{\n\tint N, E, Q; cin >> N >> E >> Q;\n\tFordFulkerson<int> g(N);\n\t\n\tfor (int i = 0; i < E; ++i)\n\t{\n\t\tint f, t; cin >> f >> t;\n\t\tf--, t--;\n\t\tg.add_edge(f, t, 1);\n\t\tg.add_edge(t ,f, 1);\n\t}\n\t\n\tint S = 0, T = N-1;\n\tint sum = g.max_flow(S, T);\n\t\n\tfor (int q = 0; q < Q; ++q)\n\t{\n\t\tint m, a, b; cin >> m >> a >> b;\n\t\ta--, b--;\n\t\t\n\t\tif (m == 1)\n\t\t{\n\t\t\t// connect (a, b)\n\t\t\tg.add_edge(a, b, 1);\n\t\t\tg.add_edge(b, a, 1);\n\t\t\tsum += g.max_flow(S, T);\n\t\t}\n\t\telse\n\t\t{\n\t\t\tassert(g.graph[a][b] + g.graph[b][a] > 0);\n\t\t\t// cut (a, b)\n\t\t\tif (g.graph[a][b] == 0)\n\t\t\t{\n\t\t\t\t// S -> a -> b -> T flow exists\n\t\t\t\tint f = g.limit_flow(T, b, 1);\n\t\t\t\tassert(T == b || f == 1);\n\t\t\t\tf = g.limit_flow(a, S, 1);\n\t\t\t\tassert(S == a || f == 1);\n\t\t\t\t\n\t\t\t\tg.graph[b][a] -= 1;\n\t\t\t\tg.graph[a][b] += 1;\n\t\t\t\tsum -= 1;\n\t\t\t}\n\t\t\tif (g.graph[b][a] == 0)\n\t\t\t{\n\t\t\t\t// S -> b -> a -> T flow exists\n\t\t\t\tint f = g.limit_flow(T, a, 1);\n\t\t\t\tassert(T == a || f == 1);\n\t\t\t\tf = g.limit_flow(b, S, 1);\n\t\t\t\tassert(S == b || f == 1);\n\t\t\t\t\n\t\t\t\tg.graph[a][b] -= 1;\n\t\t\t\tg.graph[b][a] += 1;\n\t\t\t\tsum -= 1;\n\t\t\t}\n\t\t\tg.graph[a][b] = g.graph[b][a] = 0;\n\t\t\tsum += g.max_flow(S, T);\n\t\t}\n\t\t\n\t\tcout << sum << endl;\n\t\tg.loosen(S, T);\n\t\t\n\t\t// cout << m << \" \" << a << \" \" << b << endl;\n\t\t// output(g);\n\t\t\n\t}\n\t\n\t\n\treturn;\n}\n\nint main()\n{\n\tcalc();\n\t\n\treturn 0;\n}", "accuracy": 1, "time_ms": 1410, "memory_kb": 4520, "score_of_the_acc": -0.9193, "final_rank": 8 }, { "submission_id": "aoj_2313_8363366", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\ntemplate<class T>\nbool chmin(T &a, const T &b) {if (a > b) {a = b; return 1;} return 0;}\n\nusing ll = long long;\nusing ld = long double;\n\nbool is_end = false;\n\n/*\n目的:max flow を求める\n特徴:max_flow を呼び出したのちに、add_edge が可能\n(max_flow は「前回の状態からどれだけ流せるか」を返すので、flow を流す場所 s, t を変えるとおかしな結果になる)\n*/\ntemplate<class flow_t = long long>\nstruct FordFulkerson\n{\n\t\n\tint siz;\n\tvector<vector<flow_t>> graph;\n\tvector<bool> used;\n\t\n\tFordFulkerson(int n) : siz(n), graph(n, vector<flow_t>(n, 0)), used(siz, false) { }\n\t\n\tvoid add_edge(int from, int to, flow_t cap)\n\t{\n\t\tassert(from < siz && to < siz);\n\t\tgraph[from][to] += cap;\n\t}\n\t\n\tflow_t dfs(int v, int target, flow_t flow)\n\t{\n\t\tif (v == target) {return flow;}\n\t\tused[v] = true;\n\t\t\n\t\tfor (int to = 0; to < siz; ++to)\n\t\t{\n\t\t\tflow_t cap = graph[v][to];\n\t\t\tif (cap > 0 && !used[to])\n\t\t\t{\n\t\t\t\tflow_t f = dfs(to, target, min(flow, cap));\n\t\t\t\tif (f > 0)\n\t\t\t\t{\n\t\t\t\t\tgraph[v][to] -= f;\n\t\t\t\t\tgraph[to][v] += f;\n\t\t\t\t\treturn f;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn 0;\n\t}\n\t\n\t// max flow on s -> t\n\tflow_t max_flow(int s, int t)\n\t{\n\t\tflow_t flow = 0;\n\t\tflow_t INF = 1e9; // INF の上限値に注意\n\t\twhile (true)\n\t\t{\n\t\t\tused.assign(siz, false);\n\t\t\tflow_t f = dfs(s, t, INF);\n\t\t\tflow += f;\n\t\t\t\n\t\t\tif (f == 0) {return flow;}\n\t\t}\n\t\t// 絶対に到達しないはず\n\t\treturn 0;\n\t}\n\t\n\tflow_t dfs2(int v, int target, flow_t flow)\n\t{\n\t\tif (v == target) {return flow;}\n\t\tused[v] = true;\n\t\t\n\t\tfor (int to = 0; to < siz; ++to)\n\t\t{\n\t\t\tflow_t cap = graph[v][to];\n\t\t\tif (graph[v][to] - graph[to][v] > 0 && !used[to])\n\t\t\t{\n\t\t\t\tflow_t f = dfs2(to, target, min(flow, cap));\n\t\t\t\tif (f > 0)\n\t\t\t\t{\n\t\t\t\t\tgraph[v][to] -= f;\n\t\t\t\t\tgraph[to][v] += f;\n\t\t\t\t\treturn f;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn 0;\n\t}\n\t\n\tflow_t limit_flow(int s, int t, int limit)\n\t{\n\t\tflow_t flow = 0;\n\t\twhile (true)\n\t\t{\n\t\t\tused.assign(siz, false);\n\t\t\tflow_t f = dfs2(s, t, limit);\n\t\t\tflow += f;\n\t\t\tlimit -= f;\n\t\t\t\n\t\t\tif (f == 0) {return flow;}\n\t\t}\n\t\treturn 0;\n\t}\n\t\n};\n\nvoid output(FordFulkerson<int> &g)\n{\n\tint n = g.siz;\n\t\n\tcout << \"cap\" << endl;\n\tfor (int i = 0; i < n; ++i)\n\t{\n\t\tfor (int j = 0; j < n; ++j)\n\t\t{\n\t\t\tcout << g.graph[i][j] << \" \";\n\t\t}\n\t\tcout << endl;\n\t}\n\t\n\tcout << \"flow\" << endl;\n\tfor (int i = 0; i < n; ++i)\n\t{\n\t\tfor (int j = 0; j < n; ++j)\n\t\t{\n\t\t\tint f = (g.graph[j][i]) / 2;\n\t\t\tcout << f << \" \";\n\t\t}\n\t\tcout << endl;\n\t}\n\t\n\tcout << endl;\n\t\n\treturn;\n}\n\nvoid calc()\n{\n\tint N, E, Q; cin >> N >> E >> Q;\n\tFordFulkerson<int> g(N);\n\t\n\tfor (int i = 0; i < E; ++i)\n\t{\n\t\tint f, t; cin >> f >> t;\n\t\tf--, t--;\n\t\tg.add_edge(f, t, 1);\n\t\tg.add_edge(t ,f, 1);\n\t}\n\t\n\tint S = 0, T = N-1;\n\tint sum = g.max_flow(S, T);\n\t\n\tfor (int q = 0; q < Q; ++q)\n\t{\n\t\tint m, a, b; cin >> m >> a >> b;\n\t\ta--, b--;\n\t\t\n\t\tif (m == 1)\n\t\t{\n\t\t\t// connect (a, b)\n\t\t\tg.add_edge(a, b, 1);\n\t\t\tg.add_edge(b, a, 1);\n\t\t\tsum += g.max_flow(S, T);\n\t\t}\n\t\telse\n\t\t{\n\t\t\tassert(g.graph[a][b] + g.graph[b][a] > 0);\n\t\t\t// cut (a, b)\n\t\t\tif (g.graph[a][b] == 0)\n\t\t\t{\n\t\t\t\t// S -> a -> b -> T flow exists\n\t\t\t\tint f = g.limit_flow(T, b, 1);\n\t\t\t\tassert(T == b || f == 1);\n\t\t\t\tf = g.limit_flow(a, S, 1);\n\t\t\t\tassert(S == a || f == 1);\n\t\t\t\t\n\t\t\t\tg.graph[b][a] -= 1;\n\t\t\t\tg.graph[a][b] += 1;\n\t\t\t\tsum -= 1;\n\t\t\t}\n\t\t\tif (g.graph[b][a] == 0)\n\t\t\t{\n\t\t\t\t// S -> b -> a -> T flow exists\n\t\t\t\tint f = g.limit_flow(T, a, 1);\n\t\t\t\tassert(T == a || f == 1);\n\t\t\t\tf = g.limit_flow(b, S, 1);\n\t\t\t\tassert(S == b || f == 1);\n\t\t\t\t\n\t\t\t\tg.graph[a][b] -= 1;\n\t\t\t\tg.graph[b][a] += 1;\n\t\t\t\tsum -= 1;\n\t\t\t}\n\t\t\tg.graph[a][b] = g.graph[b][a] = 0;\n\t\t\tsum += g.max_flow(S, T);\n\t\t}\n\t\t\n\t\tcout << sum << endl;\n\t}\n\t\n\t\n\treturn;\n}\n\nint main()\n{\n\tcalc();\n\t\n\treturn 0;\n}", "accuracy": 0.3275862068965517, "time_ms": 110, "memory_kb": 4460, "score_of_the_acc": -0.2294, "final_rank": 12 }, { "submission_id": "aoj_2313_8356546", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\ntemplate<class T>\nbool chmin(T &a, const T &b) {if (a > b) {a = b; return 1;} return 0;}\n\nusing ll = long long;\nusing ld = long double;\n\nbool is_end = false;\n\n/*\n目的:max flow を求める\n特徴:max_flow を呼び出したのちに、add_edge が可能\n(max_flow は「前回の状態からどれだけ流せるか」を返すので、flow を流す場所 s, t を変えるとおかしな結果になる)\n*/\ntemplate<class flow_t = long long>\nstruct FordFulkerson\n{\n\t\n\tint siz;\n\tvector<vector<flow_t>> graph;\n\tvector<bool> used;\n\t\n\tFordFulkerson(int n) : siz(n), graph(n, vector<flow_t>(n, 0)), used(siz, false) { }\n\t\n\tvoid add_edge(int from, int to, flow_t cap)\n\t{\n\t\tassert(from < siz && to < siz);\n\t\tgraph[from][to] += cap;\n\t}\n\t\n\tflow_t dfs(int v, int target, flow_t flow)\n\t{\n\t\tif (v == target) {return flow;}\n\t\tused[v] = true;\n\t\t\n\t\tfor (int to = 0; to < siz; ++to)\n\t\t{\n\t\t\tflow_t cap = graph[v][to];\n\t\t\tif (cap > 0 && !used[to])\n\t\t\t{\n\t\t\t\tflow_t f = dfs(to, target, min(flow, cap));\n\t\t\t\tif (f > 0)\n\t\t\t\t{\n\t\t\t\t\tgraph[v][to] -= f;\n\t\t\t\t\tgraph[to][v] += f;\n\t\t\t\t\treturn f;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn 0;\n\t}\n\t\n\t// max flow on s -> t\n\tflow_t max_flow(int s, int t)\n\t{\n\t\tflow_t flow = 0;\n\t\tflow_t INF = 1e9; // INF の上限値に注意\n\t\twhile (true)\n\t\t{\n\t\t\tused.assign(siz, false);\n\t\t\tflow_t f = dfs(s, t, INF);\n\t\t\tflow += f;\n\t\t\t\n\t\t\tif (f == 0) {return flow;}\n\t\t}\n\t\t// 絶対に到達しないはず\n\t\treturn 0;\n\t}\n\t\n\tflow_t limit_flow(int s, int t, int limit)\n\t{\n\t\tflow_t flow = 0;\n\t\tflow_t INF = 1e9;\n\t\twhile (true)\n\t\t{\n\t\t\tused.assign(siz, false);\n\t\t\tflow_t f = dfs(s, t, limit);\n\t\t\tflow += f;\n\t\t\tlimit -= f;\n\t\t\t\n\t\t\tif (f == 0) {return flow;}\n\t\t}\n\t\treturn 0;\n\t}\n\t\n};\n\nvoid calc()\n{\n\tint N, E, Q; cin >> N >> E >> Q;\n\tFordFulkerson<int> g(N);\n\t\n\tfor (int i = 0; i < E; ++i)\n\t{\n\t\tint f, t; cin >> f >> t;\n\t\tf--, t--;\n\t\tg.add_edge(f, t, 1);\n\t\tg.add_edge(t ,f, 1);\n\t}\n\t\n\tint S = 0, T = N-1;\n\tint sum = g.max_flow(S, T);\n\t\n\tfor (int q = 0; q < Q; ++q)\n\t{\n\t\tint m, a, b; cin >> m >> a >> b;\n\t\ta--, b--;\n\t\t\n\t\tif (m == 1)\n\t\t{\n\t\t\t// connect (a, b)\n\t\t\tg.add_edge(a, b, 1);\n\t\t\tg.add_edge(b, a, 1);\n\t\t\tsum += g.max_flow(S, T);\n\t\t}\n\t\telse\n\t\t{\n\t\t\t// cut (a, b)\n\t\t\tif (g.graph[a][b] == 0)\n\t\t\t{\n\t\t\t\t// S -> a -> b -> T flow exists\n\t\t\t\tint f = g.limit_flow(T, b, 1);\n\t\t\t\tassert(f == 1);\n\t\t\t\tf = g.limit_flow(a, S, 1);\n\t\t\t\tassert(f == 1);\n\t\t\t\t\n\t\t\t\tg.graph[b][a] -= 1;\n\t\t\t\tg.graph[a][b] += 1;\n\t\t\t\tsum -= 1;\n\t\t\t}\n\t\t\tif (g.graph[b][a] == 0)\n\t\t\t{\n\t\t\t\t// S -> b -> a -> T flow exists\n\t\t\t\tint f = g.limit_flow(T, a, 1);\n\t\t\t\tassert(f == 1);\n\t\t\t\tf = g.limit_flow(b, S, 1);\n\t\t\t\tassert(f == 1);\n\t\t\t\t\n\t\t\t\tg.graph[a][b] -= 1;\n\t\t\t\tg.graph[b][a] += 1;\n\t\t\t\tsum -= 1;\n\t\t\t}\n\t\t\tg.graph[a][b] = g.graph[b][a] = 0;\n\t\t\tsum += g.max_flow(S, T);\n\t\t}\n\t\t\n\t\tcout << sum << endl;\n\t}\n\t\n\t\n\treturn;\n}\n\nint main()\n{\n\tcalc();\n\t\n\treturn 0;\n}", "accuracy": 0.2413793103448276, "time_ms": 100, "memory_kb": 4352, "score_of_the_acc": -0.2012, "final_rank": 15 }, { "submission_id": "aoj_2313_8018309", "code_snippet": "#include <algorithm>\n#include <limits>\n#include <queue>\n#include <vector>\n\nnamespace luz {\n\n class MaxFlowGraph {\n using usize = std::size_t;\n using Cap = long long;\n\n struct Edge {\n usize to, rev;\n Cap cap;\n };\n\n usize n;\n std::vector< Cap > min_cost;\n std::vector< usize > iter;\n std::vector< std::vector< Edge > > g;\n\n bool build_augment_path(usize s, usize t) {\n min_cost.assign(n, -1);\n std::queue< usize > que;\n\n min_cost[s] = 0;\n que.push(s);\n\n while (not que.empty() and min_cost[t] == -1) {\n usize v = que.front();\n que.pop();\n\n for (const auto &e: g[v]) {\n if (e.cap > 0 and min_cost[e.to] == -1) {\n min_cost[e.to] = min_cost[v] + 1;\n que.push(e.to);\n }\n }\n }\n\n return min_cost[t] != -1;\n }\n\n Cap find_augment_path(usize v, usize t, Cap flow_limit) {\n if (v == t) return flow_limit;\n\n for (usize &i = iter[v]; i < g[v].size(); i++) {\n Edge &e = g[v][i];\n\n if (e.cap > 0 and min_cost[v] + 1 == min_cost[e.to]) {\n Cap d = find_augment_path(e.to, t, std::min(flow_limit, e.cap));\n\n if (d > 0) {\n e.cap -= d;\n g[e.to][e.rev].cap += d;\n return d;\n }\n }\n }\n\n return 0;\n }\n\n public:\n const Cap INF = std::numeric_limits< Cap >::max();\n\n explicit MaxFlowGraph(usize n): n(n), g(n) {}\n\n usize add_directed_edge(usize from, usize to, Cap cap) {\n // assert(from < n and to < n and from != to);\n usize idx = g[from].size();\n g[from].emplace_back(Edge { to, g[to].size(), cap });\n g[to].emplace_back(Edge { from, idx, 0 });\n return idx;\n }\n\n // 1. O(n^2 m) in general\n // 2. other case: see docs \"dinic-time-complexity.pdf\"\n Cap max_flow(usize s, usize t, Cap flow_limit) {\n // assert(s < n and t < n and s != t);\n Cap flow = 0, add = 0;\n\n while (build_augment_path(s, t) and flow < flow_limit) {\n iter.assign(n, 0);\n\n do {\n add = find_augment_path(s, t, flow_limit - add);\n flow += add;\n } while (add > 0);\n }\n\n return flow;\n }\n\n Cap max_flow(usize s, usize t) {\n return max_flow(s, t, INF);\n }\n\n Cap link(usize s, usize t, usize from, usize idx, Cap f){\n g[from][idx].cap += f;\n return max_flow(s, t, f);\n }\n\n Cap cut(usize s, usize t, usize from, usize idx){\n auto &e = g[from][idx];\n usize to = e.to;\n Cap rem = g[to][e.rev].cap;\n\n if(rem == 0) return e.cap = 0;\n e.cap = g[to][e.rev].cap = 0;\n\n Cap cap = rem - max_flow(from, to, rem);\n if (from != s and cap != 0) max_flow(from, s, cap);\n if (t != to and cap != 0) max_flow(t, to, cap);\n return -cap;\n }\n\n };\n\n} // namespace luz\n\n#include <iostream>\n#include <map>\n\nint main() {\n using usize = std::size_t;\n\n usize n, e, q;\n std::cin >> n >> e >> q;\n \n luz::MaxFlowGraph g(n);\n std::vector< std::map< usize, usize > > eidxs(n);\n while (e--) {\n usize u, v;\n std::cin >> u >> v;\n u--; v--;\n eidxs[u][v] = g.add_directed_edge(u, v, 1);\n eidxs[v][u] = g.add_directed_edge(v, u, 1);\n }\n\n std::vector< usize > qs(q), us(q), vs(q);\n for (usize i = 0; i < q; i++) {\n usize u, v;\n std::cin >> qs[i] >> u >> v;\n u--; v--;\n\n us[i] = u;\n vs[i] = v;\n\n if (eidxs[u].count(v)) continue;\n eidxs[u][v] = g.add_directed_edge(u, v, 0);\n eidxs[v][u] = g.add_directed_edge(v, u, 0);\n }\n\n usize s = 0, t = n - 1;\n int flow = g.max_flow(s, t);\n for (usize i = 0; i < q; i++) {\n usize u = us[i], v = vs[i];\n \n if (qs[i] == 1) {\n flow += g.link(s, t, u, eidxs[u][v], 1);\n flow += g.link(s, t, v, eidxs[v][u], 1);\n }\n \n if (qs[i] == 2) {\n flow += g.cut(s, t, u, eidxs[u][v]);\n flow += g.cut(s, t, v, eidxs[v][u]);\n }\n\n std::cout << flow << std::endl;\n }\n}", "accuracy": 1, "time_ms": 720, "memory_kb": 8320, "score_of_the_acc": -1.3698, "final_rank": 10 }, { "submission_id": "aoj_2313_7740916", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n//#pragma GCC optimize(\"Ofast\")\n\n#define rep(i,n) for(ll i=0;i<n;i++)\n#define repl(i,l,r) for(ll i=(l);i<(r);i++)\n#define per(i,n) for(ll i=(n)-1;i>=0;i--)\n#define perl(i,r,l) for(ll i=r-1;i>=l;i--)\n#define fi first\n#define se second\n#define pb push_back\n#define ins insert\n#define pqueue(x) priority_queue<x,vector<x>,greater<x>>\n#define all(x) (x).begin(),(x).end()\n#define CST(x) cout<<fixed<<setprecision(x)\n#define vtpl(x,y,z) vector<tuple<x,y,z>>\n#define rev(x) reverse(x);\nusing ll=long long;\nusing vl=vector<ll>;\nusing vvl=vector<vector<ll>>;\nusing pl=pair<ll,ll>;\nusing vpl=vector<pl>;\nusing vvpl=vector<vpl>;\nconst ll MOD=1000000007;\nconst ll MOD9=998244353;\nconst int inf=1e9+10;\nconst ll INF=4e18;\nconst ll dy[9]={1,0,-1,0,1,1,-1,-1,0};\nconst ll dx[9]={0,1,0,-1,1,-1,1,-1,0};\ntemplate<class T> inline bool chmin(T& a, T b) {\n if (a > b) {\n a = b;\n return true;\n }\n return false;\n}\ntemplate<class T> inline bool chmax(T& a, T b) {\n if (a < b) {\n a = b;\n return true;\n }\n return false;\n}\nstruct fordfulkerson{\n ll n;\n vector<vector<ll>> g;\n vector<bool> used;\n fordfulkerson(ll _n):n(_n){\n g.resize(n,vector<ll>(n));used.resize(n);\n }\n ll dfs(ll v,ll t,ll f){\n if(v==t)return f;\n used[v]=true;\n for(ll i=0;i<used.size();i++){\n if(!used[i]&&g[v][i]){\n ll d=dfs(i,t,min(f,g[v][i]));\n if(d>0){\n g[v][i]-=d;g[i][v]+=d;\n return d;\n }\n }\n }\n return 0;\n }\n ll max_flow(ll s,ll t,ll limit=INF){\n ll ans=0;\n while(1){\n used.assign(n,false);\n int f=dfs(s,t,limit);\n ans+=f;\n limit-=f;\n if(f==0||limit==0)return ans;\n }\n\n }\n};\nint main(){\n ll n;cin >> n;\n fordfulkerson f(n);\n ll m;cin >> m;\n ll q;cin >> q;\n rep(_,m){\n ll a,b;cin >>a >> b;a--;b--;\n f.g[a][b]=1;f.g[b][a]=1;\n }\n ll ans=f.max_flow(0,n-1);\n rep(_,q){\n ll t,a,b;cin >> t >> a >> b;a--;b--;\n if(t==1){\n f.g[a][b]=1;f.g[b][a]=1;\n }\n else{\n if(f.g[a][b]==0){\n int d=f.max_flow(a,b,1);\n if(d==0){\n f.max_flow(n-1,0,1);\n f.max_flow(a,b,1);\n ans--;\n }\n }\n else if(f.g[b][a]==0){\n int d=f.max_flow(b,a,1);\n if(d==0){\n f.max_flow(n-1,0,1);\n f.max_flow(b,a,1);\n ans--;\n }\n }\n f.g[a][b]=0;f.g[b][a]=0;\n }\n ans+=f.max_flow(0,n-1);\n cout << ans << endl;\n /*rep(i,n){\n rep(j,n)cout << f.g[i][j] <<\" \";cout << endl;\n }*/\n }\n}", "accuracy": 1, "time_ms": 660, "memory_kb": 5452, "score_of_the_acc": -0.7273, "final_rank": 7 }, { "submission_id": "aoj_2313_7119714", "code_snippet": "#include \"iostream\"\n#include \"climits\"\n#include \"list\"\n#include \"queue\"\n#include \"stack\"\n#include \"set\"\n#include \"functional\"\n#include \"algorithm\"\n#include \"string\"\n#include \"map\"\n#include \"unordered_map\"\n#include \"unordered_set\"\n#include \"iomanip\"\n#include \"cmath\"\n#include \"random\"\n#include \"bitset\"\n#include \"cstdio\"\n#include \"numeric\"\n#include \"cassert\"\n#include \"ctime\"\n\nusing namespace std;\n\n//constexpr long long int MOD = 1000000007;\nconstexpr long long int MOD = 998244353;\nconstexpr double EPS = 1e-8;\n\n//int N, M, K, T, H, W, L, R;\nlong long int N, M, K, T, H, W, L, R;\n\nstruct Flow {\n\tvector<vector<pair<int, int>>>edge;\n\tint ans = 0;\n\tFlow(int sz) {\n\t\tedge.resize(sz);\n\t}\n\tbool Go(int from, int to) {\n\t\tvector<int>bef(edge.size(), -1);\n\t\tqueue<int>Q;\n\t\tQ.push(from);\n\t\twhile (!Q.empty()) {\n\t\t\tint cn = Q.front();\n\t\t\tQ.pop();\n\t\t\tfor (auto i : edge[cn]) {\n\t\t\t\tif (bef[i.first] == -1 && i.second == 1) {\n\t\t\t\t\tbef[i.first] = cn;\n\t\t\t\t\tQ.push(i.first);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif (bef[to] == -1)return false;\n\t\tint p = to;\n\t\twhile (p != from) {\n\t\t\tint nx = 1;\n\t\t\tfor (int i = 0; i < edge[p].size(); i++) {\n\t\t\t\tif (edge[p][i].first == bef[p]) {\n\t\t\t\t\tnx ^= edge[p][i].second;\n\t\t\t\t\tedge[p][i].second = 1;\n\t\t\t\t}\n\t\t\t}\n\t\t\tfor (int i = 0; i < edge[bef[p]].size(); i++) {\n\t\t\t\tif (edge[bef[p]][i].first == p) {\n\t\t\t\t\tedge[bef[p]][i].second =nx;\n\t\t\t\t}\n\t\t\t}\n\t\t\tp = bef[p];\n\t\t}\n\t\treturn true;\n\t}\n\tvoid DelGo(int from,int to) {\n\t\tif (from == to)return;\n\t\tvector<int>bef(edge.size(), -1);\n\t\tqueue<int>Q;\n\t\tQ.push(from);\n\t\twhile (!Q.empty()) {\n\t\t\tint cn = Q.front();\n\t\t\tQ.pop();\n\t\t\tfor (auto i : edge[cn]) {\n\t\t\t\tif (bef[i.first] == -1 && i.second == 1) {\n\t\t\t\t\tfor (int j = 0; j < edge[i.first].size(); j++) {\n\t\t\t\t\t\tif (edge[i.first][j].first == cn && edge[i.first][j].second == 0) {\n\t\t\t\t\t\t\tbef[i.first] = cn;\n\t\t\t\t\t\t\tQ.push(i.first);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tint p = to;\n\t\twhile (p != from) {\n\t\t\tfor (int i = 0; i < edge[p].size(); i++) {\n\t\t\t\tif (edge[p][i].first == bef[p]) {\n\t\t\t\t\tedge[p][i].second = 1;\n\t\t\t\t}\n\t\t\t}\n\t\t\tfor (int i = 0; i < edge[bef[p]].size(); i++) {\n\t\t\t\tif (edge[bef[p]][i].first == p) {\n\t\t\t\t\tedge[bef[p]][i].second = 1;\n\t\t\t\t}\n\t\t\t}\n\t\t\tp = bef[p];\n\t\t}\n\t}\n\tint AddEdge(int u, int v) {\n\t\tedge[u].push_back({ v,1 });\n\t\tedge[v].push_back({ u,1 });\n\t\tif (Go(0, edge.size() - 1))ans++;\n\t\treturn ans;\n\t}\n\tvoid Init(vector<int>u, vector<int>v) {\n\t\tfor (int i = 0; i < u.size(); i++) {\n\t\t\tedge[u[i]].push_back({ v[i],1 });\n\t\t\tedge[v[i]].push_back({ u[i],1 });\n\t\t}\n\t\twhile (Go(0, edge.size() - 1)) {\n\t\t\tans++;\n\t\t}\n\t}\n\tint DeleteEdge(int u, int v) {\n\t\t{\n\t\t\tfor (int i = 0; i < edge[u].size(); i++) {\n\t\t\t\tif (edge[u][i].first == v && edge[u][i].second == 0) {\n\t\t\t\t\tans--;\n\t\t\t\t\tDelGo(edge.size() - 1, v);\n\t\t\t\t\tDelGo(v, u);\n\t\t\t\t\tDelGo(u, 0);\n\t\t\t\t\tfor (int j = 0; j < edge[u].size(); j++) {\n\t\t\t\t\t\tif (edge[u][j].first == v) {\n\t\t\t\t\t\t\tswap(edge[u][j], edge[u].back());\n\t\t\t\t\t\t\tedge[u].pop_back();\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tfor (int j = 0; j < edge[v].size(); j++) {\n\t\t\t\t\t\tif (edge[v][j].first == u) {\n\t\t\t\t\t\t\tswap(edge[v][j], edge[v].back());\n\t\t\t\t\t\t\tedge[v].pop_back();\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\t{\n\t\t\tfor (int i = 0; i < edge[v].size(); i++) {\n\t\t\t\tif (edge[v][i].first == u && edge[v][i].second == 0) {\n\t\t\t\t\tans--;\n\t\t\t\t\tDelGo(edge.size() - 1, u);\n\t\t\t\t\tDelGo(u, v);\n\t\t\t\t\tDelGo(v, 0);\n\t\t\t\t\tfor (int j = 0; j < edge[u].size(); j++) {\n\t\t\t\t\t\tif (edge[u][j].first == v) {\n\t\t\t\t\t\t\tswap(edge[u][j], edge[u].back());\n\t\t\t\t\t\t\tedge[u].pop_back();\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tfor (int j = 0; j < edge[v].size(); j++) {\n\t\t\t\t\t\tif (edge[v][j].first == u) {\n\t\t\t\t\t\t\tswap(edge[v][j], edge[v].back());\n\t\t\t\t\t\t\tedge[v].pop_back();\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tfor (int j = 0; j < edge[u].size(); j++) {\n\t\t\tif (edge[u][j].first == v) {\n\t\t\t\tswap(edge[u][j], edge[u].back());\n\t\t\t\tedge[u].pop_back();\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tfor (int j = 0; j < edge[v].size(); j++) {\n\t\t\tif (edge[v][j].first == u) {\n\t\t\t\tswap(edge[v][j], edge[v].back());\n\t\t\t\tedge[v].pop_back();\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t//\tcout << \"##################\" << endl;\n\t//\tDEBUG();\n\t//\tcout << \"----------------\" << endl;\n\t\tif (Go(0, edge.size() - 1))ans++;\n\t\treturn ans;\n\t}\n\tvoid DEBUG() {\n\t\tfor (int i = 0; i < edge.size(); i++) {\n\t\t\tfor (auto j : edge[i]) {\n\t\t\t\tcout << i << \" \" << j.first << \" \" << j.second << endl;\n\t\t\t}\n\t\t}\n\t}\n};\n\nint main() {\n\tios::sync_with_stdio(false);\n\tcin.tie(0);\n\n\tcin >> N >> M >> K;\n\tvector<int>u;\n\tvector<int>v;\n\tfor (int i = 0; i < M; i++) {\n\t\tcin >> L >> R;\n\t\tL--, R--;\n\t\tu.push_back(L);\n\t\tv.push_back(R);\n\t}\n\tFlow f(N);\n\tf.Init(u, v);\n\twhile (K--) {\n\t\tint a, b, c;\n\t\tcin >> a >> b >> c;\n\t\tb--, c--;\n\t\tif (a == 1) {\n\t\t\tcout << f.AddEdge(b, c) << endl;\n\t\t}\n\t\telse {\n\t\t\tcout << f.DeleteEdge(b, c) << endl;\n\t\t}\n\t//\tf.DEBUG();\n\t}\n}", "accuracy": 1, "time_ms": 760, "memory_kb": 4168, "score_of_the_acc": -0.5057, "final_rank": 4 }, { "submission_id": "aoj_2313_7106160", "code_snippet": "#define MOD_TYPE 2\n\n#include <bits/stdc++.h>\nusing namespace std;\n\n#if 1\n#pragma GCC target(\"avx2\")\n#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"unroll-loops\")\n#endif\n\n#if 0\n#include <ext/pb_ds/assoc_container.hpp>\n#include <ext/pb_ds/tag_and_trait.hpp>\n#include <ext/pb_ds/tree_policy.hpp>\n#include <ext/rope>\nusing namespace __gnu_pbds;\nusing namespace __gnu_cxx;\ntemplate <typename T>\nusing extset =\n tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;\n#endif\n\n#if 0\n#include <boost/multiprecision/cpp_dec_float.hpp>\n#include <boost/multiprecision/cpp_int.hpp>\nusing Int = boost::multiprecision::cpp_int;\nusing lld = boost::multiprecision::cpp_dec_float_100;\n#endif\n\n#pragma region Macros\n\nusing ll = long long int;\nusing ld = long double;\nusing pii = pair<int, int>;\nusing pll = pair<ll, ll>;\nusing pld = pair<ld, ld>;\ntemplate <typename T>\nusing smaller_queue = priority_queue<T, vector<T>, greater<T>>;\n\nconstexpr ll MOD = (MOD_TYPE == 1 ? (ll)(1e9 + 7) : 998244353);\nconstexpr int INF = (int)1e9 + 10;\nconstexpr ll LINF = (ll)4e18;\nconst double PI = acos(-1.0);\nconstexpr double EPS = 1e-11;\nconstexpr int Dx[] = {0, 0, -1, 1, -1, 1, -1, 1, 0};\nconstexpr int Dy[] = {1, -1, 0, 0, -1, -1, 1, 1, 0};\n\n#define REP(i, m, n) for (ll i = m; i < (ll)(n); ++i)\n#define rep(i, n) REP(i, 0, n)\n#define REPI(i, m, n) for (int i = m; i < (int)(n); ++i)\n#define repi(i, n) REPI(i, 0, n)\n#define YES(n) cout << ((n) ? \"YES\" : \"NO\") << \"\\n\"\n#define Yes(n) cout << ((n) ? \"Yes\" : \"No\") << \"\\n\"\n#define all(v) v.begin(), v.end()\n#define NP(v) next_permutation(all(v))\n#define dbg(x) cerr << #x << \":\" << x << \"\\n\";\n#define UNIQUE(v) v.erase(unique(all(v)), v.end())\n\nstruct io_init {\n io_init() {\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n cout << setprecision(30) << setiosflags(ios::fixed);\n };\n} io_init;\ntemplate <typename T>\ninline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return true;\n }\n return false;\n}\ntemplate <typename T>\ninline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return true;\n }\n return false;\n}\ninline ll floor(ll a, ll b) {\n if (b < 0) a *= -1, b *= -1;\n if (a >= 0) return a / b;\n return -((-a + b - 1) / b);\n}\ninline ll ceil(ll a, ll b) { return floor(a + b - 1, b); }\ntemplate <typename A, size_t N, typename T>\ninline void Fill(A (&array)[N], const T &val) {\n fill((T *)array, (T *)(array + N), val);\n}\ntemplate <typename T>\nvector<T> compress(vector<T> &v) {\n vector<T> val = v;\n sort(all(val)), val.erase(unique(all(val)), val.end());\n for (auto &&vi : v) vi = lower_bound(all(val), vi) - val.begin();\n return val;\n}\ntemplate <typename T, typename U>\nconstexpr istream &operator>>(istream &is, pair<T, U> &p) noexcept {\n is >> p.first >> p.second;\n return is;\n}\ntemplate <typename T, typename U>\nconstexpr ostream &operator<<(ostream &os, pair<T, U> p) noexcept {\n os << p.first << \" \" << p.second;\n return os;\n}\ntemplate <typename T>\nconstexpr istream &operator>>(istream &is, vector<T> &v) noexcept {\n for (int i = 0; i < v.size(); i++) is >> v[i];\n return is;\n}\ntemplate <typename T>\nconstexpr ostream &operator<<(ostream &os, vector<T> &v) noexcept {\n for (int i = 0; i < v.size(); i++)\n os << v[i] << (i + 1 == v.size() ? \"\" : \" \");\n return os;\n}\ntemplate <typename T>\nconstexpr void operator--(vector<T> &v, int) noexcept {\n for (int i = 0; i < v.size(); i++) v[i]--;\n}\n\nrandom_device seed_gen;\nmt19937_64 engine(seed_gen());\n\n#pragma endregion\n\n// --------------------------------------\n\nconst int MAX_V = 500;\nset<int> G[MAX_V];\n\nvector<vector<int>> paths;\nset<pii> used_edge;\nmap<pii, int> mp;\n\nbool used[MAX_V];\nbool dfs(int u, int sink, vector<int> &path) {\n if (u == sink) {\n path.push_back(u);\n return true;\n }\n used[u] = true;\n for (auto v : G[u]) {\n if (used[v]) continue;\n bool f = dfs(v, sink, path);\n if (f) {\n path.push_back(u);\n // 残余グラフを押し戻す場合は双方向に張り直す\n if (used_edge.count({u, v})) {\n G[v].insert(u);\n used_edge.erase({u, v});\n used_edge.erase({v, u});\n } else {\n G[u].erase(v);\n G[v].insert(u);\n used_edge.insert({u, v});\n used_edge.insert({v, u});\n }\n return true;\n }\n }\n return false;\n}\n\nint n;\nint ans = 0;\n\nvoid calc() {\n while (1) {\n fill(used, used + MAX_V, false);\n vector<int> path;\n bool f = dfs(0, n - 1, path);\n if (not f) return;\n reverse(all(path));\n rep(i, path.size() - 1) { mp[{path[i], path[i + 1]}] = paths.size(); }\n paths.push_back(path);\n ans++;\n }\n}\n\nvoid add(int u, int v) {\n assert(not G[u].count(v));\n assert(not G[v].count(u));\n assert(not used_edge.count({u, v}));\n assert(not used_edge.count({v, u}));\n G[u].insert(v);\n G[v].insert(u);\n calc();\n}\n\nvoid del(int u, int v) {\n if (used_edge.count({u, v})) { // 流した\n int pi;\n if (G[u].count(v)) // 残余グラフに u->v がある = v->u で使った\n pi = mp[{v, u}];\n else\n pi = mp[{u, v}];\n rep(i, paths[pi].size() - 1) {\n int u = paths[pi][i];\n int v = paths[pi][i + 1];\n G[u].insert(v);\n used_edge.erase({u, v});\n used_edge.erase({v, u});\n }\n ans--;\n }\n G[u].erase(v);\n G[v].erase(u);\n calc();\n}\n\nvoid solve() {\n int m, q;\n cin >> n >> m >> q;\n rep(i, m) {\n int u, v;\n cin >> u >> v;\n u--, v--;\n G[u].insert(v);\n G[v].insert(u);\n }\n calc();\n rep(i, q) {\n int m, u, v;\n cin >> m >> u >> v;\n u--, v--;\n if (m == 1)\n add(u, v);\n else\n del(u, v);\n cout << ans << \"\\n\";\n }\n}\n\nint main() { solve(); }", "accuracy": 0.2413793103448276, "time_ms": 30, "memory_kb": 3700, "score_of_the_acc": -0.0258, "final_rank": 14 } ]
aoj_2314_cpp
問題文 落書きの魔女 ALBERTINE は W 列 H 行の正方形のセルからなる長方形の結界の入り口に落書きをしている.この世界では落書きは必ずセルを白色か黒色で塗ることによって成り立つ. すでに使い魔 ANJA の手によって落書きの一部が描かれているのだが,落書きの魔女は残りすべてのセルを塗り落書きを完成させたい.落書きの魔女にはあるこだわりがある.そのこだわりとは次のようなものである. 2 つの異なるセルが辺を共有するとき, 2 つのセルは隣接していると呼ぶことにする.完成された落書きにおいては,任意の 2 つの黒いセルについて,片方からもう片方への隣接した黒いセルをたどるパスがちょうど 1 つだけ存在していなければならない. どの 2 つの白色のセルも隣接してはならない. また,落書きの魔女は黒色が好きなので,こだわりを満たした状態でなおかつ最も黒色のセルが多い落書きを完成させたい.果たして落書きの魔女は最大で何個のセルが黒色に塗られたこだわりの落書きを描くことができるだろうか. 入力形式 入力は以下の形式で与えられる. H\ W\\ c_{11} c_{12} ... c_{1W}\\ c_{21} c_{22} ... c_{2W}\\ ...\\ c_{H1} c_{H2} ... c_{HW}\\ H は結界の入り口の縦幅, W は結界の入り口の横幅を表す. c_{ij} は各セルの状態を表す文字であり,上から i 行目,左から j 列目のセルの状態を表す.セルに黒色が塗られている場合は c_{ij} は '#' となり,白色が塗られている場合は '.' ,まだ色が塗られていない場合はそうでない場合は '?' となる. 出力形式 魔女のこだわりを満たした状態でとりうる黒色のセルの個数の最大値を 1 行で出力せよ. もしどのように塗っても魔女のこだわりを満たすように出来ない場合, -1 と出力せよ. 制約 1 ≤ H ≤ 11 1 ≤ W ≤ 11 c_{ij} は '#' , '.' , '?' のいずれかである. 入出力例 入力例 1 3 4 .##. #??# .#?# 出力例1 8 入力例 2 5 5 ##?## #???# ????? #???# ##?## 出力例 2 19 入力例 3 9 2 ## .. ?? ?? ?? ?? ?? ?? ?? 出力例 3 -1 入力例 4 7 9 ??#####?? ?#?????#? ?#?????#? ?#??#??#? ?#???#?#? ??#####?? ???????#? 出力例 4 44 入力例 5 11 11 ??????????? ???####???? ???#???#??? ???#???#??? ???#???#??? ???####???? ???#???#??? ???#???#??? ???#???#??? ???####???? ??????????? 出力例 5 86 Problem Setter: Flat35
[ { "submission_id": "aoj_2314_10712143", "code_snippet": "// AOJ #2314 Scribbling witch\n// 2025.5.30\n\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nconst int MAX = 11;\n\nint h, w;\nchar B[MAX][MAX+1];\nmap<ll,int> memo[MAX][MAX];\nchar mapto[16];\nint nxt(ll, int, int, char);\n\ninline int Get(ll s, int i) {\n int v = (s >> (i*4)) & 0xF;\n return (v<<28)>>28;\n}\ninline void Set(ll &s, int v, int i) {\n s &= ~((ll)0xF << (i*4));\n s |= (ll)(v&0xF) << (i*4);\n}\n\nint calc(ll st, int x, int y) {\n if (x==w) return calc(st, 0, y+1);\n if (y==h) {\n for(int i=0;i<w;i++) if (Get(st,i)>0) return -1;\n return 0;\n }\n auto &mp = memo[y][x];\n if (mp.count(st)) return mp[st];\n\n int ans = -1;\n char c0 = B[y][x];\n if (c0=='#' || c0=='?') ans = max(ans, nxt(st, x, y, '#'));\n if (c0=='.' || c0=='?') ans = max(ans, nxt(st, x, y, '.'));\n return mp[st] = ans;\n}\n\n\nint nxt(ll st, int x, int y, char c) {\n int pc = Get(st,x);\n int lc = x>0 ? Get(st,x-1) : -1;\n int add = 0;\n if (c=='#') {\n add = 1;\n if (lc>=0 && lc==pc) return -1;\n if (lc>=0) {\n Set(st, lc, x);\n if (pc>=0) {\n for(int i=0;i<w;i++) if (Get(st,i)==pc) Set(st, lc, i);\n }\n } else if (pc==-1) Set(st, 7, x);\n else Set(st, pc, x);\n } else {\n if ((y>0 && pc==-1) || (x>0 && lc==-1)) return -1;\n Set(st, -1, x);\n if (pc!=-1) {\n bool ok=false;\n for(int i=0;i<w;i++) if (Get(st,i)==pc){ ok=true; break; }\n if (!ok) return -1;\n }\n }\n memset(mapto, -1, sizeof(mapto));\n int mcnt=0;\n for(int i=0;i<w;i++){\n int v = Get(st,i);\n if (v!=-1) {\n if (mapto[v]==-1) mapto[v]=mcnt++;\n Set(st, mapto[v], i);\n }\n }\n int sub = calc(st, x+1, y);\n return sub<0 ? -1 : sub+add;\n}\n\nint main(){\n ios::sync_with_stdio(0);\n cin.tie(0);\n\n cin >> h >> w;\n for(int y=0;y<h;y++) cin >> B[y];\n if (w==1) {\n if (h==1) {\n cout << (B[0][0]=='.' ? 0 : 1) << endl;\n return 0;\n }\n int ans = -1;\n for(int L=0;L<=1;L++){\n for(int R=max(L,h-2); R<=h-1; R++){\n if (L>R) continue;\n bool ok = true;\n for(int i=L;i<=R;i++){\n if (B[i][0]=='.') ok=false;\n }\n for(int i=0;i<L;i++){\n if (B[i][0]=='#') ok=false;\n }\n for(int i=R+1;i<h;i++){\n if (B[i][0]=='#') ok=false;\n }\n if (!ok) continue;\n ans = max(ans, R-L+1);\n }\n }\n cout << ans << endl;\n return 0;\n }\n for(int y=0;y<h;y++){\n for(int x=0;x<w;x++){\n memo[y][x].clear();\n }\n }\n int ans = calc(-1LL, 0, 0);\n cout << ans << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 90, "memory_kb": 21376, "score_of_the_acc": -0.2193, "final_rank": 5 }, { "submission_id": "aoj_2314_10543179", "code_snippet": "// AOJ #2314 Scribbling witch\n// 2025.5.30\n\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nconst int MAX = 11;\n\nint h, w;\nchar B[MAX][MAX+1];\nmap<ll,int> memo[MAX][MAX];\nchar mapto[16];\nint nxt(ll, int, int, char);\n\ninline int Get(ll s, int i) {\n int v = (s >> (i*4)) & 0xF;\n return (v<<28)>>28;\n}\ninline void Set(ll &s, int v, int i) {\n s &= ~((ll)0xF << (i*4));\n s |= (ll)(v&0xF) << (i*4);\n}\n\nint calc(ll st, int x, int y) {\n if (x==w) return calc(st, 0, y+1);\n if (y==h) {\n for(int i=0;i<w;i++) if (Get(st,i)>0) return -1;\n return 0;\n }\n auto &mp = memo[y][x];\n if (mp.count(st)) return mp[st];\n\n int ans = -1;\n char c0 = B[y][x];\n if (c0=='#' || c0=='?') ans = max(ans, nxt(st, x, y, '#'));\n if (c0=='.' || c0=='?') ans = max(ans, nxt(st, x, y, '.'));\n return mp[st] = ans;\n}\n\n\nint nxt(ll st, int x, int y, char c) {\n int pc = Get(st,x);\n int lc = x>0 ? Get(st,x-1) : -1;\n int add = 0;\n if (c=='#') {\n add = 1;\n if (lc>=0 && lc==pc) return -1;\n if (lc>=0) {\n Set(st, lc, x);\n if (pc>=0) {\n for(int i=0;i<w;i++) if (Get(st,i)==pc) Set(st, lc, i);\n }\n } else if (pc==-1) Set(st, 7, x);\n else Set(st, pc, x);\n } else {\n if ((y>0 && pc==-1) || (x>0 && lc==-1)) return -1;\n Set(st, -1, x);\n if (pc!=-1) {\n bool ok=false;\n for(int i=0;i<w;i++) if (Get(st,i)==pc){ ok=true; break; }\n if (!ok) return -1;\n }\n }\n memset(mapto, -1, sizeof(mapto));\n int mcnt=0;\n for(int i=0;i<w;i++){\n int v = Get(st,i);\n if (v!=-1) {\n if (mapto[v]==-1) mapto[v]=mcnt++;\n Set(st, mapto[v], i);\n }\n }\n int sub = calc(st, x+1, y);\n return sub<0 ? -1 : sub+add;\n}\n\nint main(){\n ios::sync_with_stdio(0);\n cin.tie(0);\n\n cin >> h >> w;\n for(int y=0;y<h;y++) cin >> B[y];\n if (w==1) {\n if (h==1) {\n cout << (B[0][0]=='.' ? 0 : 1) << endl;\n return 0;\n }\n int ans = -1;\n for(int L=0;L<=1;L++){\n for(int R=max(L,h-2); R<=h-1; R++){\n if (L>R) continue;\n bool ok = true;\n for(int i=L;i<=R;i++){\n if (B[i][0]=='.') ok=false;\n }\n for(int i=0;i<L;i++){\n if (B[i][0]=='#') ok=false;\n }\n for(int i=R+1;i<h;i++){\n if (B[i][0]=='#') ok=false;\n }\n if (!ok) continue;\n ans = max(ans, R-L+1);\n }\n }\n cout << ans << endl;\n return 0;\n }\n for(int y=0;y<h;y++){\n for(int x=0;x<w;x++){\n memo[y][x].clear();\n }\n }\n int ans = calc(-1LL, 0, 0);\n cout << ans << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 90, "memory_kb": 21292, "score_of_the_acc": -0.2185, "final_rank": 4 }, { "submission_id": "aoj_2314_9166708", "code_snippet": "#pragma GCC optimize(\"Ofast\")\n#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n\nmt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());\nll myRand(ll B) {\n return (ull)rng() % B;\n}\ninline double time() {\n return static_cast<long double>(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count()) * 1e-9;\n}\n\nint main() {\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n int h,w; cin >> h >> w;\n vector<string> s(h);\n for (int i = 0; i < h; ++i) {\n cin >> s[i];\n }\n map<vector<int>, int> dp;\n // そもそもw=1は除いておいた方が連結性確認の面で楽?\n if (w == 1) {\n for (int i = 1; i+1 < h; ++i) {\n if (s[i][0] == '.') {\n cout << -1 << endl;\n return 0;\n }\n }\n set<int> st;\n if (s[0][0] == '.') st.insert(0);\n if (s.back()[0] == '.') st.insert(h-1);\n cout << h - st.size() << endl;\n return 0;\n }\n {\n // 1列目の置き方を列挙する\n vector<int> v(w);\n auto dfs = [&](auto dfs, int i, int id, int cnt) -> void {\n if (i == w) {\n dp[v] = cnt;\n return;\n }\n if (v[i-1] == 0) {\n if (s[0][i] == '.') return;\n v[i] = id + 1;\n dfs(dfs, i+1, id+1, cnt+1);\n }\n else {\n v[i] = 0;\n if (s[0][i] != '#') dfs(dfs, i+1, id, cnt);\n v[i] = id;\n if (s[0][i] != '.') dfs(dfs, i+1, id, cnt+1);\n }\n };\n v[0] = 1;\n if (s[0][0] != '.') dfs(dfs, 1, 1, 1);\n v[0] = 0;\n if (s[0][0] != '#') dfs(dfs, 1, 0, 0);\n }\n for (int i = 1; i < h; ++i) {\n for (int j = 0; j < w; ++j) {\n map<vector<int>, int> ndp;\n bool B = (s[i][j] != '.');\n bool W = (s[i][j] != '#');\n if (B) {\n for (auto &pp : dp) {\n auto p = pp.first;\n // ループを作ってはいけない\n if (j and p[j-1] and p[j-1] == p[j]) continue;\n // 接する面が白のみの場合、新規連結成分\n if (p[j] == 0 and (j == 0 or p[j-1] == 0)) {\n int mx = 0;\n for (int k = 0; k < w; ++k) {\n mx = max(mx, p[k]);\n }\n p[j] = mx+1;\n ndp[p] = max(ndp[p], pp.second+1);\n } else {\n if (j == 0 or p[j-1] == 0) {\n // そのまま\n ndp[p] = max(ndp[p], pp.second+1);\n } else if (p[j] == 0) {\n p[j] = p[j-1];\n ndp[p] = max(ndp[p], pp.second+1);\n } else {\n int a = p[j], b = p[j-1];\n if (a > b) swap(a, b);\n // 2つをマージする\n p[j] = a;\n for (int k = 0; k < w; ++k) {\n if (p[k] == b) p[k] = a;\n else if (p[k] > b) p[k] -= 1;\n }\n ndp[p] = max(ndp[p], pp.second+1);\n }\n }\n }\n }\n if (W) {\n for (auto &pp : dp) {\n auto p = pp.first;\n // 白は連結してはいけない\n if (p[j] == 0 or (j and p[j-1] == 0)) continue;\n // 白によって連結成分が孤立してしまってもダメ\n int cnt = 0;\n for (int k = 0; k < w; ++k) {\n cnt += (p[j] == p[k]);\n }\n if (cnt != 1) {\n p[j] = 0;\n ndp[p] = max(ndp[p], pp.second);\n }\n }\n }\n swap(dp, ndp);\n\n// cout << i << \" \" << j << endl;\n// for (auto &pp : dp) {\n// auto p = pp.first;\n// for (int k : p) {\n// cout << k << \" \";\n// }\n// cout << endl;\n// }\n }\n }\n int res = -1;\n for (auto &pp : dp) {\n auto p = pp.first;\n bool ok = true;\n for (int i = 0; i < w; ++i) {\n if (p[i] > 1) ok = false;\n }\n if (ok) res = max(res, pp.second);\n }\n cout << res << endl;\n}", "accuracy": 1, "time_ms": 320, "memory_kb": 6100, "score_of_the_acc": -0.2881, "final_rank": 7 }, { "submission_id": "aoj_2314_8402578", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nstring to_string(const vector<int>& arr) {\n\tstring res = \"[\";\n\tfor (int i = 0; i < int(arr.size()); i++) {\n\t\tif (i != 0) {\n\t\t\tres += \", \";\n\t\t}\n\t\tres += to_string(arr[i]);\n\t}\n\tres += \"]\";\n\treturn res;\n}\n\nvoid regularize(int n, vector<int>& v) {\n\tint cnt = 0;\n\tvector<int> tbl(n + 1, -1);\n\tfor (int i = 0; i < n; i++) {\n\t\tif (v[i] != -1 && tbl[v[i]] == -1) {\n\t\t\ttbl[v[i]] = cnt++;\n\t\t}\n\t}\n\tfor (int i = 0; i < n; i++) {\n\t\tif (v[i] != -1) {\n\t\t\tv[i] = tbl[v[i]];\n\t\t}\n\t}\n}\n\nint solve_one(int N, const vector<int>& X) {\n\tif (N == 1) {\n\t\treturn (X[0] != 0);\n\t}\n\tif (N == 2) {\n\t\tint res = (X[0] != 0) + (X[1] != 0);\n\t\treturn (res >= 1 ? res : -1);\n\t}\n\tfor (int i = 1; i < N - 1; i++) {\n\t\tif (X[i] == 0) {\n\t\t\treturn -1;\n\t\t}\n\t}\n\treturn (N - 2) + (X[0] != 0) + (X[N - 1] != 0);\n}\n\nint solve(int H, int W, const vector<vector<int> >& A) {\n\tif (H < W) {\n\t\tvector<vector<int> > B(W, vector<int>(H));\n\t\tfor (int i = 0; i < H; i++) {\n\t\t\tfor (int j = 0; j < W; j++) {\n\t\t\t\tB[j][i] = A[i][j];\n\t\t\t}\n\t\t}\n\t\treturn solve(W, H, B);\n\t}\n\tif (W == 1) {\n\t\tvector<int> X(H);\n\t\tfor (int i = 0; i < H; i++) {\n\t\t\tX[i] = A[i][0];\n\t\t}\n\t\treturn solve_one(H, X);\n\t}\n\tconst int INF = 1012345678;\n\tvector<vector<map<vector<int>, int> > > dp(H, vector<map<vector<int>, int> >(W));\n\tvector<string> Z(H, string(W, '?'));\n\tauto calc = [&](auto& self, int x, int y, const vector<int>& v) -> int {\n\t\tif (x == H && y == 0) {\n\t\t\tif (*max_element(v.begin(), v.end()) >= 1) {\n\t\t\t\treturn -INF;\n\t\t\t}\n\t\t\treturn 0;\n\t\t}\n\t\tif (dp[x][y].find(v) != dp[x][y].end()) {\n\t\t\treturn dp[x][y][v];\n\t\t}\n\t\tint nx = x, ny = y + 1;\n\t\tif (ny == W) {\n\t\t\tnx += 1;\n\t\t\tny = 0;\n\t\t}\n\t\tint res = -INF;\n\t\tif (A[x][y] != 1 && (y == 0 || v[y - 1] != -1) && v[y] != -1) {\n\t\t\t// place white\n\t\t\tvector<int> nv = v;\n\t\t\tnv[y] = -1;\n\t\t\tif (find(nv.begin(), nv.end(), v[y]) != nv.end()) {\n\t\t\t\tZ[x][y] = '.';\n\t\t\t\tint subres = self(self, nx, ny, nv);\n\t\t\t\tres = max(res, subres);\n\t\t\t}\n\t\t}\n\t\tif (A[x][y] != 0 && (y == 0 || v[y] == -1 || v[y - 1] != v[y])) {\n\t\t\t// place black\n\t\t\tvector<int> nv = v;\n\t\t\tif ((y != 0 && v[y - 1] != -1) && v[y] != -1) {\n\t\t\t\tfor (int i = 0; i < W; i++) {\n\t\t\t\t\tif (v[i] == v[y]) {\n\t\t\t\t\t\tnv[i] = v[y - 1];\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\telse if ((y != 0 && v[y - 1] != -1) || v[y] != -1) {\n\t\t\t\tnv[y] = (v[y - 1] != -1 ? v[y - 1] : v[y]);\n\t\t\t}\n\t\t\telse {\n\t\t\t\tnv[y] = W;\n\t\t\t}\n\t\t\tregularize(W, nv);\n\t\t\tZ[x][y] = '#';\n\t\t\tint subres = self(self, nx, ny, nv);\n\t\t\tres = max(res, subres + 1);\n\t\t}\n\t\tdp[x][y][v] = res;\n\t\treturn res;\n\t};\n\tint ans = -1;\n\tfor (int i = 0; i < (1 << W); i++) {\n\t\tvector<int> v(W, -1);\n\t\tint cnt = -1, pre = -2, black = 0;\n\t\tbool flag = true;\n\t\tfor (int j = 0; j < W; j++) {\n\t\t\tif ((i >> j) & 1) {\n\t\t\t\tif (A[0][j] == 0) {\n\t\t\t\t\tflag = false;\n\t\t\t\t}\n\t\t\t\tif (pre != j - 1) {\n\t\t\t\t\tcnt += 1;\n\t\t\t\t}\n\t\t\t\tv[j] = cnt;\n\t\t\t\tpre = j;\n\t\t\t\tZ[0][j] = '#';\n\t\t\t\tblack += 1;\n\t\t\t}\n\t\t\telse {\n\t\t\t\tZ[0][j] = '.';\n\t\t\t\tif (A[0][j] == 1 || (j >= 1 && v[j - 1] == -1)) {\n\t\t\t\t\tflag = false;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif (flag) {\n\t\t\tint res = calc(calc, 1, 0, v);\n\t\t\tans = max(ans, res + black);\n\t\t}\n\t}\n\treturn ans;\n}\n\nbool check(int H, int W, const vector<vector<int> >& A) {\n\tvector<vector<bool> > vis(H, vector<bool>(W, false));\n\tauto check = [&](int x, int y) -> bool {\n\t\treturn 0 <= x && x < H && 0 <= y && y < W && !vis[x][y] && A[x][y] == 1;\n\t};\n\tauto dfs = [&](auto& self, int x, int y) -> void {\n\t\tvis[x][y] = true;\n\t\tif (check(x, y - 1)) self(self, x, y - 1);\n\t\tif (check(x, y + 1)) self(self, x, y + 1);\n\t\tif (check(x - 1, y)) self(self, x - 1, y);\n\t\tif (check(x + 1, y)) self(self, x + 1, y);\n\t};\n\tint cnt = 0;\n\tfor (int i = 0; i < H; i++) {\n\t\tfor (int j = 0; j < W; j++) {\n\t\t\tif (A[i][j] == 1 && !vis[i][j]) {\n\t\t\t\tdfs(dfs, i, j);\n\t\t\t\tcnt += 1;\n\t\t\t}\n\t\t}\n\t}\n\tif (cnt >= 2) {\n\t\treturn false;\n\t}\n\tint f = 0;\n\tfor (int i = 0; i < H; i++) {\n\t\tfor (int j = 0; j < W; j++) {\n\t\t\tif (A[i][j] == 1) {\n\t\t\t\tif (i >= 1 && A[i - 1][j] == 1) f -= 1;\n\t\t\t\tif (j >= 1 && A[i][j - 1] == 1) f -= 1;\n\t\t\t\tf += 1;\n\t\t\t}\n\t\t\tif (A[i][j] == 0) {\n\t\t\t\tif (i >= 1 && A[i - 1][j] == 0) return false;\n\t\t\t\tif (j >= 1 && A[i][j - 1] == 0) return false;\n\t\t\t}\n\t\t}\n\t}\n\treturn cnt == 0 || f == 1;\n}\n\nbool testcase_check(int H, int W) {\n\tfor (int i = 0; i < 1 << (H * W); i++) {\n\t\tvector<vector<int> > A(H, vector<int>(W));\n\t\tfor (int j = 0; j < H * W; j++) {\n\t\t\tA[j / W][j % W] = ((i >> j) & 1);\n\t\t}\n\t\tbool res1 = (solve(H, W, A) != -1);\n\t\tbool res2 = check(H, W, A);\n\t\tif (res1 != res2) {\n\t\t\tvector<string> res(H);\n\t\t\tfor (int i = 0; i < H; i++) {\n\t\t\t\tfor (int j = 0; j < W; j++) {\n\t\t\t\t\tres[i] += (A[i][j] == 0 ? '.' : '#');\n\t\t\t\t}\n\t\t\t}\n\t\t\tcout << \"H = \" << H << \", W = \" << W << \":\" << endl;\n\t\t\tfor (int i = 0; i < H; i++) {\n\t\t\t\tcout << res[i] << endl;\n\t\t\t}\n\t\t\tcout << \"Returns: \" << (res1 ? \"true\" : \"false\") << endl;\n\t\t\tcout << \"Answer: \" << (res2 ? \"true\" : \"false\") << endl;\n\t\t\treturn false;\n\t\t}\n\t}\n\treturn true;\n}\n\nint main() {\n\t/*\n\tfor (int H = 1; H <= 5; H++) {\n\t\tfor (int W = 1; W <= H; W++) {\n\t\t\tif (testcase_check(H, W)) {\n\t\t\t\tcout << \"H = \" << H << \", W = \" << W << \": success!\" << endl;\n\t\t\t}\n\t\t}\n\t}\n\treturn 0;\n\t*/\n\tint H, W;\n\tcin >> H >> W;\n\tvector<vector<int> > A(H, vector<int>(W, -1));\n\tfor (int i = 0; i < H; i++) {\n\t\tstring s;\n\t\tcin >> s;\n\t\tfor (int j = 0; j < W; j++) {\n\t\t\tif (s[j] == '.') A[i][j] = 0;\n\t\t\tif (s[j] == '#') A[i][j] = 1;\n\t\t}\n\t}\n\tint ans = solve(H, W, A);\n\tcout << ans << endl;\n\treturn 0;\n}", "accuracy": 1, "time_ms": 200, "memory_kb": 43856, "score_of_the_acc": -0.5184, "final_rank": 9 }, { "submission_id": "aoj_2314_8402506", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nvoid regularize(int n, vector<int>& v) {\n\tint cnt = 0;\n\tvector<int> tbl(n + 1, -1);\n\tfor (int i = 0; i < n; i++) {\n\t\tif (v[i] != -1 && tbl[v[i]] == -1) {\n\t\t\ttbl[v[i]] = cnt++;\n\t\t}\n\t}\n\tfor (int i = 0; i < n; i++) {\n\t\tif (v[i] != -1) {\n\t\t\tv[i] = tbl[v[i]];\n\t\t}\n\t}\n}\n\nint solve_one(int N, const vector<int>& X) {\n\tif (N == 1) {\n\t\treturn (X[0] != 0);\n\t}\n\tfor (int i = 1; i < N - 1; i++) {\n\t\tif (X[i] == 0) {\n\t\t\treturn -1;\n\t\t}\n\t}\n\treturn (N - 2) + (X[0] != 0) + (X[N - 1] != 0);\n}\n\nstring to_string(const vector<int>& arr) {\n\tstring res = \"[\";\n\tfor (int i = 0; i < int(arr.size()); i++) {\n\t\tif (i != 0) {\n\t\t\tres += \", \";\n\t\t}\n\t\tres += to_string(arr[i]);\n\t}\n\tres += \"]\";\n\treturn res;\n}\n\nint solve(int H, int W, const vector<vector<int> >& A) {\n\tif (H < W) {\n\t\tvector<vector<int> > B(W, vector<int>(H));\n\t\tfor (int i = 0; i < H; i++) {\n\t\t\tfor (int j = 0; j < W; j++) {\n\t\t\t\tB[j][i] = A[i][j];\n\t\t\t}\n\t\t}\n\t\treturn solve(W, H, B);\n\t}\n\tif (W == 1) {\n\t\tvector<int> X(H);\n\t\tfor (int i = 0; i < H; i++) {\n\t\t\tX[i] = A[i][0];\n\t\t}\n\t\treturn solve_one(H, X);\n\t}\n\tconst int INF = 1012345678;\n\tvector<vector<map<vector<int>, int> > > dp(H, vector<map<vector<int>, int> >(W));\n\tvector<string> Z(H, string(W, '?'));\n\tauto calc = [&](auto& self, int x, int y, const vector<int>& v) -> int {\n\t\tif (x == H && y == 0) {\n\t\t\tif (*max_element(v.begin(), v.end()) >= 1) {\n\t\t\t\treturn -INF;\n\t\t\t}\n\t\t\treturn 0;\n\t\t}\n\t\tif (dp[x][y].find(v) != dp[x][y].end()) {\n\t\t\treturn dp[x][y][v];\n\t\t}\n\t\tint nx = x, ny = y + 1;\n\t\tif (ny == W) {\n\t\t\tnx += 1;\n\t\t\tny = 0;\n\t\t}\n\t\tint res = -INF;\n\t\tif (A[x][y] != 1 && (y == 0 || v[y - 1] != -1) && v[y] != -1) {\n\t\t\t// place white\n\t\t\tvector<int> nv = v;\n\t\t\tnv[y] = -1;\n\t\t\tif (find(nv.begin(), nv.end(), v[y]) != nv.end()) {\n\t\t\t\tZ[x][y] = '.';\n\t\t\t\tint subres = self(self, nx, ny, nv);\n\t\t\t\tres = max(res, subres);\n\t\t\t}\n\t\t}\n\t\tif (A[x][y] != 0 && (y == 0 || v[y] == -1 || v[y - 1] != v[y])) {\n\t\t\t// place black\n\t\t\tvector<int> nv = v;\n\t\t\tif ((y != 0 && v[y - 1] != -1) && v[y] != -1) {\n\t\t\t\tfor (int i = 0; i < W; i++) {\n\t\t\t\t\tif (v[i] == v[y]) {\n\t\t\t\t\t\tnv[i] = v[y - 1];\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\telse if ((y != 0 && v[y - 1] != -1) || v[y] != -1) {\n\t\t\t\tnv[y] = (v[y - 1] != -1 ? v[y - 1] : v[y]);\n\t\t\t}\n\t\t\telse {\n\t\t\t\tnv[y] = W;\n\t\t\t}\n\t\t\tregularize(W, nv);\n\t\t\tZ[x][y] = '#';\n\t\t\tint subres = self(self, nx, ny, nv);\n\t\t\tres = max(res, subres + 1);\n\t\t}\n\t\tdp[x][y][v] = res;\n\t\treturn res;\n\t};\n\tint ans = -1;\n\tfor (int i = 0; i < (1 << W); i++) {\n\t\tvector<int> v(W, -1);\n\t\tint cnt = -1, pre = -2, black = 0;\n\t\tbool flag = true;\n\t\tfor (int j = 0; j < W; j++) {\n\t\t\tif ((i >> j) & 1) {\n\t\t\t\tif (A[0][j] == 0) {\n\t\t\t\t\tflag = false;\n\t\t\t\t}\n\t\t\t\tif (pre != j - 1) {\n\t\t\t\t\tcnt += 1;\n\t\t\t\t}\n\t\t\t\tv[j] = cnt;\n\t\t\t\tpre = j;\n\t\t\t\tZ[0][j] = '#';\n\t\t\t\tblack += 1;\n\t\t\t}\n\t\t\telse {\n\t\t\t\tZ[0][j] = '.';\n\t\t\t\tif (A[0][j] == 1 || (j >= 1 && v[j - 1] == -1)) {\n\t\t\t\t\tflag = false;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif (flag) {\n\t\t\tint res = calc(calc, 1, 0, v);\n\t\t\tans = max(ans, res + black);\n\t\t}\n\t}\n\treturn ans;\n}\n\nint main() {\n\tint H, W;\n\tcin >> H >> W;\n\tvector<vector<int> > A(H, vector<int>(W, -1));\n\tfor (int i = 0; i < H; i++) {\n\t\tstring s;\n\t\tcin >> s;\n\t\tfor (int j = 0; j < W; j++) {\n\t\t\tif (s[j] == '.') A[i][j] = 0;\n\t\t\tif (s[j] == '#') A[i][j] = 1;\n\t\t}\n\t}\n\tint ans = solve(H, W, A);\n\tcout << ans << endl;\n\treturn 0;\n}", "accuracy": 0.6320754716981132, "time_ms": 210, "memory_kb": 43976, "score_of_the_acc": -0.5284, "final_rank": 13 }, { "submission_id": "aoj_2314_7124649", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntemplate<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }\ntemplate<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }\n#define all(x) (x).begin(),(x).end()\n#define fi first\n#define se second\n#define mp make_pair\n#define si(x) ll(x.size())\nconst int mod=998244353,MAX=300005,INF=1<<30;\n\n#define int short\n\nint H,W;\n\nvector<int> used;\nvector<vector<int>> conne;\nvector<int> nex;\n\nvector<int> X;\n\nll pat=0;\n\nvoid DFS(int rem){\n if(rem==0){\n pat++;\n used.push_back(nex.back());\n conne.push_back(X);\n return;\n }\n int s=-1;\n for(int j=0;;j++){\n if(rem&(1<<j)){\n s=(1<<j);\n break;\n }\n }\n for(int mask=rem;;mask=(mask-1)&rem){\n if(mask&s){\n int ng=0;\n for(int j=0;j<W;j++){\n if(mask&(1<<j)){\n if(rem&(1<<(j+1))){\n if(!(mask&(1<<(j+1)))) ng++;\n }\n if(j&&(rem&(1<<(j-1)))){\n if(!(mask&(1<<(j-1)))) ng++;\n }\n }\n }\n if(ng<=1){\n X.push_back(mask);\n DFS(rem-mask);\n X.pop_back();\n }\n }\n \n if(mask==0) break;\n }\n}\n\nint dp[12][12][233000];\n\nvector<int> divide[233000];\n\nsigned main(){\n \n std::ifstream in(\"text.txt\");\n std::cin.rdbuf(in.rdbuf());\n cin.tie(0);\n ios::sync_with_stdio(false);\n \n cin>>H>>W;\n vector<string> S(H);\n for(int i=0;i<H;i++) cin>>S[i];\n \n if(W==1){\n int ans=-1;\n {\n bool ok=true;\n for(int i=0;i<H-1;i++){\n ok&=(S[i][0]!='.');\n }\n ok&=(S[H-1][0]!='#');\n if(ok){\n if(ans<H-1) ans=H-1;\n }//chmax(ans,H-1);\n }\n {\n bool ok=true;\n for(int i=1;i<H;i++){\n ok&=(S[i][0]!='.');\n }\n ok&=(S[0][0]!='#');\n if(ok){\n if(ans<H-1) ans=H-1;\n }//chmax(ans,H-1);\n }\n {\n bool ok=true;\n for(int i=1;i<H-1;i++){\n ok&=(S[i][0]!='.');\n }\n ok&=(S[0][0]!='#');\n ok&=(S[H-1][0]!='#');\n //if(H!=2&&ok) chmax(ans,H-2);\n if(H!=2&&ok){\n if(ans<H-2) ans=H-2;\n }//chmax(ans,H-1);\n }\n {\n bool ok=true;\n for(int i=0;i<H;i++){\n ok&=(S[i][0]!='.');\n }\n if(ok){\n if(ans<H) ans=H;\n }//chmax(ans,H-1);\n }\n \n cout<<ans<<endl;\n \n return 0;\n }\n \n for(int bit=0;bit<(1<<W);bit++){\n //cout<<bit<<endl;\n int ng=0;\n for(int j=0;j+1<W;j++){\n int cn=0;\n if(bit&(1<<j)) cn++;\n if(bit&(1<<(j+1))) cn++;\n if(cn==0) ng++;\n }\n if(ng<=1){\n nex.push_back(bit);\n DFS(bit);\n \n vector<int> Y;\n int j=0;\n while(j<W){\n if(bit&(1<<j)){\n int k=j;\n int mas=0;\n while(bit&(1<<k)){\n mas|=(1<<k);\n k++;\n }\n Y.push_back(mas);\n j=k;\n }else{\n j++;\n }\n }\n \n divide[bit]=Y;\n \n //cout<<bitset<10>(bit)<<endl;\n }\n }\n \n //cout<<si(conne)<<\" \"<<si(used)<<endl;\n //cout<<pat<<endl;\n \n for(ll j=0;j<si(conne);j++){\n sort(all(conne[j]));\n //cout<<bitset<10>(used[j])<<endl;\n }\n \n for(int i=0;i<=H;i++) for(int w=0;w<=W;w++) for(ll j=0;j<si(conne);j++) dp[i][w][j]=-100;\n \n map<vector<int>,ll> MA;\n \n for(ll t=0;t<si(conne);t++){\n MA[conne[t]]=t;\n bool ok=true;\n for(int j=0;j<W;j++){\n if(S[0][j]=='#'){\n if(!(used[t]&(1<<j))) ok=false;\n }\n if(S[0][j]=='.'){\n if(used[t]&(1<<j)) ok=false;\n }\n }\n for(int j=0;j+1<W;j++){\n int cn=0;\n if(!(used[t]&(1<<j))) cn++;\n if(!(used[t]&(1<<(j+1)))) cn++;\n if(cn==2) ok=false;\n }\n if(ok){\n if(divide[used[t]]==conne[t]){\n dp[1][0][t]=__builtin_popcount(used[t]);\n //cout<<bitset<11>(used[t])<<endl;\n }\n }\n }\n \n for(int i=1;i<H;i++){\n for(int j=0;j<W;j++){\n for(ll k=0;k<si(conne);k++){\n if(dp[i][j][k]<0) continue;\n \n int mask=used[k];\n \n if(mask&(1<<j)){\n if(j==0||(!(mask&(1<<(j-1))))){\n if(S[i][j]!='.'){\n if(dp[i][j+1][k]<dp[i][j][k]+1) dp[i][j+1][k]=dp[i][j][k]+1;\n //chmax(dp[i][j+1][k],dp[i][j][k]+1);\n }\n }else{\n bool ok=true;\n for(int a:conne[k]){\n if((a&(1<<(j-1)))&&(a&(1<<j))) ok=false;\n }\n if(ok){\n vector<int> to;\n int ss=0;\n for(int a:conne[k]){\n if(a&(1<<(j-1))){\n ss|=a;\n }else if(a&(1<<j)){\n ss|=a;\n }else{\n to.push_back(a);\n }\n }\n to.push_back(ss);\n sort(all(to));\n if(S[i][j]!='.'){\n if(dp[i][j+1][MA[to]]<dp[i][j][k]+1) dp[i][j+1][MA[to]]=dp[i][j][k]+1;\n //chmax(dp[i][j+1][MA[to]],dp[i][j][k]+1);\n }\n }\n }\n \n int z=-1;\n for(int a:conne[k]){\n if(a&(1<<j)) z=a;\n }\n int hi=-1;\n for(int l=W-1;l>=0;l--){\n if(z&(1<<l)){\n hi=l;\n break;\n }\n }\n int x=(1<<j)-1;\n \n if((hi!=j)||(z&x)){\n vector<int> to=conne[k];\n for(int &a:to){\n if(a&(1<<j)) a-=(1<<j);\n }\n //cout<<used[k]<<\" \"<<used[MA[to]]<<endl;\n sort(all(to));\n //cout<<used[k]<<\" \"<<used[MA[to]]<<endl;\n if(S[i][j]!='#'&&(j==0||(used[k]&(1<<(j-1))))) chmax(dp[i][j+1][MA[to]],dp[i][j][k]);\n }\n }else{\n if(j==0||(!(mask&(1<<(j-1))))){\n vector<int> to=conne[k];\n to.push_back((1<<j));\n sort(all(to));\n assert(MA.count(to));\n if(S[i][j]!='.'){\n if(dp[i][j+1][MA[to]]<dp[i][j][k]+1) dp[i][j+1][MA[to]]=dp[i][j][k]+1;\n //chmax(dp[i][j+1][MA[to]],dp[i][j][k]+1);\n }\n assert(MA.count(to));\n //cout<<used[k]<<\" \"<<used[MA[to]]<<endl;\n //for(int a:to) cout<<a<<\" \";\n //cout<<endl;\n }else{\n vector<int> to=conne[k];\n for(int &a:to){\n if(a&(1<<(j-1))) a|=(1<<j);\n }\n sort(all(to));\n if(S[i][j]!='.'){\n if(dp[i][j+1][MA[to]]<dp[i][j][k]+1) dp[i][j+1][MA[to]]=dp[i][j][k]+1;\n //chmax(dp[i][j+1][MA[to]],dp[i][j][k]+1);\n }\n }\n }\n }\n }\n \n for(ll k=0;k<si(conne);k++) dp[i+1][0][k]=dp[i][W][k];\n }\n \n int ans=-1;\n \n for(ll k=0;k<si(conne);k++){\n if(si(conne[k])==1){\n //for(int a:conne[k]) cout<<a<<\" \";\n //cout<<\" : \";\n //cout<<dp[H][0][k]<<endl;\n chmax(ans,dp[H][0][k]);\n }\n }\n \n cout<<ans<<endl;\n}", "accuracy": 1, "time_ms": 240, "memory_kb": 113200, "score_of_the_acc": -1.1741, "final_rank": 12 }, { "submission_id": "aoj_2314_7124645", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntemplate<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }\ntemplate<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }\n#define all(x) (x).begin(),(x).end()\n#define fi first\n#define se second\n#define mp make_pair\n#define si(x) ll(x.size())\nconst int mod=998244353,MAX=300005,INF=1<<30;\n\n#define int short\n\nint H,W;\n\nvector<int> used;\nvector<vector<int>> conne;\nvector<int> nex;\n\nvector<int> X;\n\nll pat=0;\n\nvoid DFS(int rem){\n if(rem==0){\n pat++;\n used.push_back(nex.back());\n conne.push_back(X);\n return;\n }\n int s=-1;\n for(int j=0;;j++){\n if(rem&(1<<j)){\n s=(1<<j);\n break;\n }\n }\n for(int mask=rem;;mask=(mask-1)&rem){\n if(mask&s){\n int ng=0;\n for(int j=0;j<W;j++){\n if(mask&(1<<j)){\n if(rem&(1<<(j+1))){\n if(!(mask&(1<<(j+1)))) ng++;\n }\n if(j&&(rem&(1<<(j-1)))){\n if(!(mask&(1<<(j-1)))) ng++;\n }\n }\n }\n if(ng<=1){\n X.push_back(mask);\n DFS(rem-mask);\n X.pop_back();\n }\n }\n \n if(mask==0) break;\n }\n}\n\nint dp[12][12][233000];\n\nvector<int> divide[233000];\n\nsigned main(){\n \n std::ifstream in(\"text.txt\");\n std::cin.rdbuf(in.rdbuf());\n cin.tie(0);\n ios::sync_with_stdio(false);\n \n cin>>H>>W;\n vector<string> S(H);\n for(int i=0;i<H;i++) cin>>S[i];\n \n if(W==1){\n int ans=-1;\n {\n bool ok=true;\n for(int i=0;i<H-1;i++){\n ok&=(S[i][0]!='.');\n }\n ok&=(S[H-1][0]!='#');\n if(ok){\n if(ans<H-1) ans=H-1;\n }//chmax(ans,H-1);\n }\n {\n bool ok=true;\n for(int i=1;i<H;i++){\n ok&=(S[i][0]!='.');\n }\n ok&=(S[0][0]!='#');\n if(ok){\n if(ans<H-1) ans=H-1;\n }//chmax(ans,H-1);\n }\n {\n bool ok=true;\n for(int i=1;i<H-1;i++){\n ok&=(S[i][0]!='.');\n }\n ok&=(S[0][0]!='#');\n ok&=(S[H-1][0]!='#');\n //if(H!=2&&ok) chmax(ans,H-2);\n if(H!=2&&ok){\n if(ans<H-2) ans=H-2;\n }//chmax(ans,H-1);\n }\n {\n bool ok=true;\n for(int i=0;i<H;i++){\n ok&=(S[i][0]!='.');\n }\n if(ok){\n if(ans<H) ans=H;\n }//chmax(ans,H-1);\n }\n \n cout<<ans<<endl;\n \n return 0;\n }\n \n for(int bit=0;bit<(1<<W);bit++){\n //cout<<bit<<endl;\n int ng=0;\n for(int j=0;j+1<W;j++){\n int cn=0;\n if(bit&(1<<j)) cn++;\n if(bit&(1<<(j+1))) cn++;\n if(cn==0) ng++;\n }\n if(ng<=1){\n nex.push_back(bit);\n DFS(bit);\n \n vector<int> Y;\n int j=0;\n while(j<W){\n if(bit&(1<<j)){\n int k=j;\n int mas=0;\n while(bit&(1<<k)){\n mas|=(1<<k);\n k++;\n }\n Y.push_back(mas);\n j=k;\n }else{\n j++;\n }\n }\n \n divide[bit]=Y;\n \n //cout<<bitset<10>(bit)<<endl;\n }\n }\n \n //cout<<si(conne)<<\" \"<<si(used)<<endl;\n //cout<<pat<<endl;\n \n for(ll j=0;j<si(conne);j++){\n sort(all(conne[j]));\n //cout<<bitset<10>(used[j])<<endl;\n }\n \n for(int i=0;i<=H;i++) for(int w=0;w<=W;w++) for(ll j=0;j<si(conne);j++) dp[i][w][j]=-100;\n \n map<vector<int>,ll> MA;\n \n for(ll t=0;t<si(conne);t++){\n MA[conne[t]]=t;\n bool ok=true;\n for(int j=0;j<W;j++){\n if(S[0][j]=='#'){\n if(!(used[t]&(1<<j))) ok=false;\n }\n if(S[0][j]=='.'){\n if(used[t]&(1<<j)) ok=false;\n }\n }\n if(ok){\n if(divide[used[t]]==conne[t]){\n dp[1][0][t]=__builtin_popcount(used[t]);\n //cout<<used[t]<<endl;\n }\n }\n }\n \n for(int i=1;i<H;i++){\n for(int j=0;j<W;j++){\n for(ll k=0;k<si(conne);k++){\n if(dp[i][j][k]<0) continue;\n \n int mask=used[k];\n \n if(mask&(1<<j)){\n if(j==0||(!(mask&(1<<(j-1))))){\n if(S[i][j]!='.'){\n if(dp[i][j+1][k]<dp[i][j][k]+1) dp[i][j+1][k]=dp[i][j][k]+1;\n //chmax(dp[i][j+1][k],dp[i][j][k]+1);\n }\n }else{\n bool ok=true;\n for(int a:conne[k]){\n if((a&(1<<(j-1)))&&(a&(1<<j))) ok=false;\n }\n if(ok){\n vector<int> to;\n int ss=0;\n for(int a:conne[k]){\n if(a&(1<<(j-1))){\n ss|=a;\n }else if(a&(1<<j)){\n ss|=a;\n }else{\n to.push_back(a);\n }\n }\n to.push_back(ss);\n sort(all(to));\n if(S[i][j]!='.'){\n if(dp[i][j+1][MA[to]]<dp[i][j][k]+1) dp[i][j+1][MA[to]]=dp[i][j][k]+1;\n //chmax(dp[i][j+1][MA[to]],dp[i][j][k]+1);\n }\n }\n }\n \n int z=-1;\n for(int a:conne[k]){\n if(a&(1<<j)) z=a;\n }\n int hi=-1;\n for(int l=W-1;l>=0;l--){\n if(z&(1<<l)){\n hi=l;\n break;\n }\n }\n int x=(1<<j)-1;\n \n if((hi!=j)||(z&x)){\n vector<int> to=conne[k];\n for(int &a:to){\n if(a&(1<<j)) a-=(1<<j);\n }\n //cout<<used[k]<<\" \"<<used[MA[to]]<<endl;\n sort(all(to));\n //cout<<used[k]<<\" \"<<used[MA[to]]<<endl;\n if(S[i][j]!='#'&&(j==0||(used[k]&(1<<(j-1))))) chmax(dp[i][j+1][MA[to]],dp[i][j][k]);\n }\n }else{\n if(j==0||(!(mask&(1<<(j-1))))){\n vector<int> to=conne[k];\n to.push_back((1<<j));\n sort(all(to));\n assert(MA.count(to));\n if(S[i][j]!='.'){\n if(dp[i][j+1][MA[to]]<dp[i][j][k]+1) dp[i][j+1][MA[to]]=dp[i][j][k]+1;\n //chmax(dp[i][j+1][MA[to]],dp[i][j][k]+1);\n }\n assert(MA.count(to));\n //cout<<used[k]<<\" \"<<used[MA[to]]<<endl;\n //for(int a:to) cout<<a<<\" \";\n //cout<<endl;\n }else{\n vector<int> to=conne[k];\n for(int &a:to){\n if(a&(1<<(j-1))) a|=(1<<j);\n }\n sort(all(to));\n if(S[i][j]!='.'){\n if(dp[i][j+1][MA[to]]<dp[i][j][k]+1) dp[i][j+1][MA[to]]=dp[i][j][k]+1;\n //chmax(dp[i][j+1][MA[to]],dp[i][j][k]+1);\n }\n }\n }\n }\n }\n \n for(ll k=0;k<si(conne);k++) dp[i+1][0][k]=dp[i][W][k];\n }\n \n int ans=-1;\n \n for(ll k=0;k<si(conne);k++){\n if(si(conne[k])==1){\n //for(int a:conne[k]) cout<<a<<\" \";\n //cout<<\" : \";\n //cout<<dp[H][0][k]<<endl;\n chmax(ans,dp[H][0][k]);\n }\n }\n \n cout<<ans<<endl;\n}", "accuracy": 0.49056603773584906, "time_ms": 250, "memory_kb": 113052, "score_of_the_acc": -1.1817, "final_rank": 15 }, { "submission_id": "aoj_2314_7124632", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntemplate<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }\ntemplate<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }\n#define all(x) (x).begin(),(x).end()\n#define fi first\n#define se second\n#define mp make_pair\n#define si(x) int(x.size())\nconst int mod=998244353,MAX=300005,INF=1<<30;\n\n#define int short\n\nint H,W;\n\nvector<int> used;\nvector<vector<int>> conne;\nvector<int> nex;\n\nvector<int> X;\n\nll pat=0;\n\nvoid DFS(int rem){\n if(rem==0){\n pat++;\n used.push_back(nex.back());\n conne.push_back(X);\n return;\n }\n int s=-1;\n for(int j=0;;j++){\n if(rem&(1<<j)){\n s=(1<<j);\n break;\n }\n }\n for(int mask=rem;;mask=(mask-1)&rem){\n if(mask&s){\n int ng=0;\n for(int j=0;j<W;j++){\n if(mask&(1<<j)){\n if(rem&(1<<(j+1))){\n if(!(mask&(1<<(j+1)))) ng++;\n }\n if(j&&(rem&(1<<(j-1)))){\n if(!(mask&(1<<(j-1)))) ng++;\n }\n }\n }\n if(ng<=1){\n X.push_back(mask);\n DFS(rem-mask);\n X.pop_back();\n }\n }\n \n if(mask==0) break;\n }\n}\n\nint dp[12][12][233000];\n\nvector<int> divide[233000];\n\nsigned main(){\n \n std::ifstream in(\"text.txt\");\n std::cin.rdbuf(in.rdbuf());\n cin.tie(0);\n ios::sync_with_stdio(false);\n \n cin>>H>>W;\n vector<string> S(H);\n for(int i=0;i<H;i++) cin>>S[i];\n \n if(W==1){\n int ans=-1;\n {\n bool ok=true;\n for(int i=0;i<H-1;i++){\n ok&=(S[i][0]!='.');\n }\n ok&=(S[H-1][0]!='#');\n if(ok){\n if(ans<H-1) ans=H-1;\n }//chmax(ans,H-1);\n }\n {\n bool ok=true;\n for(int i=1;i<H;i++){\n ok&=(S[i][0]!='.');\n }\n ok&=(S[0][0]!='#');\n if(ok){\n if(ans<H-1) ans=H-1;\n }//chmax(ans,H-1);\n }\n {\n bool ok=true;\n for(int i=1;i<H-1;i++){\n ok&=(S[i][0]!='.');\n }\n ok&=(S[0][0]!='#');\n ok&=(S[H-1][0]!='#');\n //if(H!=2&&ok) chmax(ans,H-2);\n if(H!=2&&ok){\n if(ans<H-2) ans=H-2;\n }//chmax(ans,H-1);\n }\n {\n bool ok=true;\n for(int i=0;i<H;i++){\n ok&=(S[i][0]!='.');\n }\n if(ok){\n if(ans<H) ans=H;\n }//chmax(ans,H-1);\n }\n \n cout<<ans<<endl;\n \n return 0;\n }\n \n for(int bit=0;bit<(1<<W);bit++){\n int ng=0;\n for(int j=0;j+1<W;j++){\n int cn=0;\n if(bit&(1<<j)) cn++;\n if(bit&(1<<(j+1))) cn++;\n if(cn==0) ng++;\n }\n if(ng<=1){\n nex.push_back(bit);\n DFS(bit);\n \n vector<int> Y;\n int j=0;\n while(j<W){\n if(bit&(1<<j)){\n int k=j;\n int mas=0;\n while(bit&(1<<k)){\n mas|=(1<<k);\n k++;\n }\n Y.push_back(mas);\n j=k;\n }else{\n j++;\n }\n }\n \n divide[bit]=Y;\n }\n }\n \n //cout<<pat<<endl;\n \n for(int j=0;j<si(conne);j++){\n sort(all(conne[j]));\n }\n \n for(int i=0;i<=H;i++) for(int w=0;w<=W;w++) for(int j=0;j<si(conne);j++) dp[i][w][j]=-100;\n \n map<vector<int>,int> MA;\n \n for(int t=0;t<si(conne);t++){\n MA[conne[t]]=t;\n bool ok=true;\n for(int j=0;j<W;j++){\n if(S[0][j]=='#'){\n if(!(used[t]&(1<<j))) ok=false;\n }\n if(S[0][j]=='.'){\n if(used[t]&(1<<j)) ok=false;\n }\n }\n if(ok){\n if(divide[used[t]]==conne[t]){\n dp[1][0][t]=__builtin_popcount(used[t]);\n }\n }\n }\n \n for(int i=1;i<H;i++){\n for(int j=0;j<W;j++){\n for(int k=0;k<si(conne);k++){\n if(dp[i][j][k]<0) continue;\n \n int mask=used[k];\n \n if(mask&(1<<j)){\n if(j==0||(!(mask&(1<<(j-1))))){\n if(S[i][j]!='.'){\n if(dp[i][j+1][k]<dp[i][j][k]+1) dp[i][j+1][k]=dp[i][j][k]+1;\n //chmax(dp[i][j+1][k],dp[i][j][k]+1);\n }\n }else{\n bool ok=true;\n for(int a:conne[k]){\n if((a&(1<<(j-1)))&&(a&(1<<j))) ok=false;\n }\n if(ok){\n vector<int> to;\n int ss=0;\n for(int a:conne[k]){\n if(a&(1<<(j-1))){\n ss|=a;\n }else if(a&(1<<j)){\n ss|=a;\n }else{\n to.push_back(a);\n }\n }\n to.push_back(ss);\n sort(all(to));\n if(S[i][j]!='.'){\n if(dp[i][j+1][MA[to]]<dp[i][j][k]+1) dp[i][j+1][MA[to]]=dp[i][j][k]+1;\n //chmax(dp[i][j+1][MA[to]],dp[i][j][k]+1);\n }\n }\n }\n \n int z=-1;\n for(int a:conne[k]){\n if(a&(1<<j)) z=a;\n }\n int hi=-1;\n for(int l=W-1;l>=0;l--){\n if(z&(1<<l)){\n hi=l;\n break;\n }\n }\n int x=(1<<j)-1;\n \n if((hi!=j)||(z&x)){\n vector<int> to=conne[k];\n for(int &a:to){\n if(a&(1<<j)) a-=(1<<j);\n }\n //cout<<used[k]<<\" \"<<used[MA[to]]<<endl;\n sort(all(to));\n //cout<<used[k]<<\" \"<<used[MA[to]]<<endl;\n if(S[i][j]!='#'&&(j==0||(used[k]&(1<<(j-1))))) chmax(dp[i][j+1][MA[to]],dp[i][j][k]);\n }\n }else{\n if(j==0||(!(mask&(1<<(j-1))))){\n vector<int> to=conne[k];\n to.push_back((1<<j));\n sort(all(to));\n assert(MA.count(to));\n if(S[i][j]!='.'){\n if(dp[i][j+1][MA[to]]<dp[i][j][k]+1) dp[i][j+1][MA[to]]=dp[i][j][k]+1;\n //chmax(dp[i][j+1][MA[to]],dp[i][j][k]+1);\n }\n assert(MA.count(to));\n //cout<<used[k]<<\" \"<<used[MA[to]]<<endl;\n //for(int a:to) cout<<a<<\" \";\n //cout<<endl;\n }else{\n vector<int> to=conne[k];\n for(int &a:to){\n if(a&(1<<(j-1))) a|=(1<<j);\n }\n sort(all(to));\n if(S[i][j]!='.'){\n if(dp[i][j+1][MA[to]]<dp[i][j][k]+1) dp[i][j+1][MA[to]]=dp[i][j][k]+1;\n //chmax(dp[i][j+1][MA[to]],dp[i][j][k]+1);\n }\n }\n }\n }\n }\n \n for(int k=0;k<si(conne);k++) dp[i+1][0][k]=dp[i][W][k];\n }\n \n int ans=-1;\n \n for(int k=0;k<si(conne);k++){\n if(si(conne[k])==1){\n //for(int a:conne[k]) cout<<a<<\" \";\n //cout<<\" : \";\n //cout<<dp[H][0][k]<<endl;\n chmax(ans,dp[H][0][k]);\n }\n }\n \n cout<<ans<<endl;\n}", "accuracy": 0.20754716981132076, "time_ms": 20, "memory_kb": 71476, "score_of_the_acc": -0.6046, "final_rank": 18 }, { "submission_id": "aoj_2314_6002770", "code_snippet": "//#pragma GCC optimize(\"O3\")\n//#pragma GCC optimize(\"unroll-loops\")\n#include<iostream>\n#include<string>\n#include<cstdio>\n#include<vector>\n#include<cmath>\n#include<algorithm>\n#include<functional>\n#include<iomanip>\n#include<queue>\n#include<ciso646>\n#include<random>\n#include<map>\n#include<set>\n#include<bitset>\n#include<stack>\n#include<unordered_map>\n#include<unordered_set>\n#include<utility>\n#include<cassert>\n#include<complex>\n#include<numeric>\n#include<array>\nusing namespace std;\n\n//#define int long long\ntypedef long long ll;\n\ntypedef unsigned long long ul;\ntypedef unsigned int ui;\nconstexpr ll mod = 1000003;;\nconst ll INF = mod * mod;\ntypedef pair<int, int>P;\n\n#define rep(i,n) for(int i=0;i<n;i++)\n#define per(i,n) for(int i=n-1;i>=0;i--)\n#define Rep(i,sta,n) for(int i=sta;i<n;i++)\n#define rep1(i,n) for(int i=1;i<=n;i++)\n#define per1(i,n) for(int i=n;i>=1;i--)\n#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)\n#define all(v) (v).begin(),(v).end()\ntypedef pair<ll, ll> LP;\ntypedef long double ld;\ntypedef pair<ld, ld> LDP;\nconst ld eps = 1e-8;\nconst ld pi = acosl(-1.0);\n\nll mod_pow(ll x, ll n, ll m = mod) {\n\tif (n < 0) {\n\t\tll res = mod_pow(x, -n, m);\n\t\treturn mod_pow(res, m - 2, m);\n\t}\n\tif (abs(x) >= m)x %= m;\n\tif (x < 0)x += m;\n\tll res = 1;\n\twhile (n) {\n\t\tif (n & 1)res = res * x % m;\n\t\tx = x * x % m; n >>= 1;\n\t}\n\treturn res;\n}\nstruct modint {\n\tll n;\n\tmodint() :n(0) { ; }\n\tmodint(ll m) :n(m) {\n\t\tif (n >= mod)n %= mod;\n\t\telse if (n < 0)n = (n % mod + mod) % mod;\n\t}\n\toperator int() { return n; }\n};\nbool operator==(modint a, modint b) { return a.n == b.n; }\nmodint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }\nmodint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }\nmodint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }\nmodint operator+(modint a, modint b) { return a += b; }\nmodint operator-(modint a, modint b) { return a -= b; }\nmodint operator*(modint a, modint b) { return a *= b; }\nmodint operator^(modint a, ll n) {\n\tif (n == 0)return modint(1);\n\tmodint res = (a * a) ^ (n / 2);\n\tif (n % 2)res = res * a;\n\treturn res;\n}\n\nll inv(ll a, ll p) {\n\treturn (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);\n}\nmodint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }\nmodint operator/=(modint& a, modint b) { a = a / b; return a; }\nconst int max_n = 1 << 2;\nmodint fact[max_n], factinv[max_n];\nvoid init_f() {\n\tfact[0] = modint(1);\n\tfor (int i = 0; i < max_n - 1; i++) {\n\t\tfact[i + 1] = fact[i] * modint(i + 1);\n\t}\n\tfactinv[max_n - 1] = modint(1) / fact[max_n - 1];\n\tfor (int i = max_n - 2; i >= 0; i--) {\n\t\tfactinv[i] = factinv[i + 1] * modint(i + 1);\n\t}\n}\nmodint comb(int a, int b) {\n\tif (a < 0 || b < 0 || a < b)return 0;\n\treturn fact[a] * factinv[b] * factinv[a - b];\n}\nmodint combP(int a, int b) {\n\tif (a < 0 || b < 0 || a < b)return 0;\n\treturn fact[a] * factinv[a - b];\n}\n\nvoid solve() {\n\tint h, w; cin >> h >> w;\n\tvector<string> s(h);\n\trep(i, h)cin >> s[i];\n\tif (w == 1) {\n\t\tint ans = -1;\n\t\trep(i, (1 << h)) {\n\t\t\tbool valid = true;\n\t\t\tvector<bool> c(h);\n\t\t\trep(j, h) {\n\t\t\t\tint b = i & (1 << j);\n\t\t\t\tc[j] = b;\n\t\t\t\tif (b && s[j][0] == '.')valid = false;\n\t\t\t\tif (!b && s[j][0] == '#')valid = false;\n\t\t\t}\n\t\t\trep(j, h - 1) {\n\t\t\t\tif (!c[j] && !c[j + 1])valid = false;\n\t\t\t}\n\t\t\trep(j, h)if (c[j]) {\n\t\t\t\twhile (j + 1 < h && c[j + 1])j++;\n\t\t\t\tj++; j++;\n\t\t\t\tfor (; j < h; j++) {\n\t\t\t\t\tif (c[j])valid = false;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (!valid)continue;\n\t\t\tint cnt = 0;\n\t\t\trep(j, h)if (c[j])cnt++;\n\t\t\tans = max(ans, cnt);\n\t\t}\n\t\tcout << ans << \"\\n\";\n\t\treturn;\n\t}\n\tvector<vector<int>> ch(1 << w);\n\trep(j, (1 << w)) {\n\t\trep(i, w)if (j & (1 << i))ch[j].push_back(i);\n\t}\n\tvector<int> trans(w);\n\tvector<int> nv;\n\n\tmap<vector<int>, int> dp;\n\tdp[{}] = 0;\n\trep(i, h)rep(j, w) {\n\t\tmap<vector<int>, int> ndp;\n\t\tfor (auto p : dp) {\n\t\t\tfill(all(trans), -1);\n\t\t\trep(i,p.first.size())for (int c : ch[p.first[i]]) {\n\t\t\t\ttrans[c] = i;\n\t\t\t}\n\t\t\t//white,black\n\t\t\trep(ad, 2) {\n\t\t\t\tif (ad == 0 && s[i][j] == '#')continue;\n\t\t\t\tif (ad == 1 && s[i][j] == '.')continue;\n\t\t\t\tbool valid = true;\n\n\t\t\t\t//white and white\n\t\t\t\tif (ad == 0) {\n\t\t\t\t\tif (i > 0 && trans[j]<0)valid = false;\n\t\t\t\t\tif (j > 0 && trans[j - 1]<0)valid = false;\n\t\t\t\t}\n\t\t\t\t//only one and die\n\t\t\t\tif (ad == 0) {\n\t\t\t\t\tif (trans[j] >= 0) {\n\t\t\t\t\t\tif (p.first[trans[j]] == (1 << j))valid = false;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\t//black cycle\n\t\t\t\tif (ad == 1) {\n\t\t\t\t\tif (i > 0 && j > 0 && trans[j] >= 0 && trans[j - 1] >= 0 && trans[j] == trans[j - 1]) {\n\t\t\t\t\t\tvalid = false;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif (!valid)continue;\n\t\t\t\tnv.clear();\n\t\t\t\tif (ad == 0) {\n\t\t\t\t\tnv = p.first;\n\t\t\t\t\tif (trans[j] >= 0) {\n\t\t\t\t\t\tnv[trans[j]] ^= (1 << j);\n\t\t\t\t\t}\n\t\t\t\t\tsort(all(nv));\n\t\t\t\t\tif (nv.size() && nv[0] == 0)nv.erase(nv.begin());\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tnv = p.first;\n\t\t\t\t\tif (trans[j] < 0 && (j==0||trans[j - 1] < 0)) {\n\t\t\t\t\t\tnv.push_back(1 << j);\n\t\t\t\t\t}\n\t\t\t\t\telse if (trans[j] < 0) {\n\t\t\t\t\t\tnv[trans[j - 1]] |= (1 << j);\n\t\t\t\t\t}\n\t\t\t\t\telse if ((j==0||trans[j - 1] < 0)) {\n\t\t\t\t\t\t//\n\t\t\t\t\t}\n\t\t\t\t\telse {\n\t\t\t\t\t\tint a = trans[j];\n\t\t\t\t\t\tint b = trans[j - 1];\n\t\t\t\t\t\tnv[a] |= nv[b];\n\t\t\t\t\t\tnv.erase(nv.begin() + b);\n\t\t\t\t\t}\n\t\t\t\t\tsort(all(nv));\n\t\t\t\t}\n\t\t\t\tint val = p.second + ad;\n\t\t\t\tif (ndp.find(nv) != ndp.end()) {\n\t\t\t\t\tndp[nv] = max(ndp[nv], val);\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tndp[nv] = val;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tswap(dp, ndp);\n\t}\n\tint ans = -1;\n\tfor (auto p : dp) {\n\t\tif (p.first.size() <= 1)ans = max(ans, p.second);\n\t}\n\tcout << ans << \"\\n\";\n}\n\n\nsigned main() {\n\tios::sync_with_stdio(false);\n\tcin.tie(0);\n\tcout << fixed << setprecision(8);\n\t//init_f();\n\t//init();\n\t//int t; cin >> t; rep(i, t)\n\t//while(cin>>n>>q,n)\n\tsolve();\n\treturn 0;\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 4224, "score_of_the_acc": -0.057, "final_rank": 1 }, { "submission_id": "aoj_2314_4107018", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing Int = long long;\ntemplate<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}\ntemplate<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}\n\n//INSERT ABOVE HERE\nstruct UnionFind{\n vector<int> ps;\n UnionFind(int n):ps(n){iota(ps.begin(),ps.end(),0);}\n int find(int x){\n if(ps[x]==x) return x;\n return ps[x]=find(ps[x]);\n }\n int same(int x,int y){\n return find(x)==find(y);\n }\n void unite(int x,int y){\n x=find(x);y=find(y);\n if(x==y) return;\n if(x<y) swap(x,y);\n ps[y]=x;\n }\n};\n\nsigned main(){\n int h,w;\n cin>>h>>w;\n vector<string> st(h);\n for(int i=0;i<h;i++) cin>>st[i];\n\n if(h==1&&w==1){\n if(st[0][0]=='.') cout<<0<<endl;\n else cout<<1<<endl;\n return 0;\n }\n\n if(w==1){\n vector<string> vs(w);\n for(int j=0;j<w;j++){\n vs[j]=string(h,'%');\n for(int i=0;i<h;i++) vs[j][i]=st[i][j];\n }\n swap(h,w);\n st=vs;\n }\n\n vector<int> bs(h,0),ws(h,0);\n for(int i=0;i<h;i++){\n for(int j=0;j<w;j++){\n bs[i]|=(st[i][j]=='#')<<j;\n ws[i]|=(st[i][j]=='.')<<j;\n }\n }\n\n int sz=1<<w;\n vector<int> cand;\n for(int bit=0;bit<sz;bit++){\n vector<int> vs(w);\n for(int j=0;j<w;j++)\n vs[j]=(bit>>j)&1;\n int flg=1;\n for(int j=0;j+1<w;j++)\n flg&=vs[j]||vs[j+1];\n if(flg) cand.emplace_back(bit);\n }\n\n map<vector<int>, int> dp;\n for(int bit:cand){\n if(( bit&bs[0])!=bs[0]) continue;\n if((~bit&ws[0])!=ws[0]) continue;\n\n vector<int> vs(w);\n for(int j=0;j<w;j++)\n vs[j]=(bit>>j)&1;\n\n UnionFind uf(w);\n for(int j=0;j+1<w;j++)\n if(vs[j]&&vs[j+1]) uf.unite(j,j+1);\n\n for(int j=0;j<w;j++) uf.find(j);\n vector<int> ps=uf.ps;\n for(int j=0;j<w;j++)\n if(!vs[j]) ps[j]=-1;\n\n dp[ps]=__builtin_popcount(bit);\n }\n\n for(int i=1;i<h;i++){\n map<vector<int>, int> nx;\n for(auto p:dp){\n vector<int> ps=p.first;\n for(int bit:cand){\n if(( bit&bs[i])!=bs[i]) continue;\n if((~bit&ws[i])!=ws[i]) continue;\n\n vector<int> vs(w);\n for(int j=0;j<w;j++)\n vs[j]=(bit>>j)&1;\n\n // cout<<i<<\" \"<<p.second<<\":\"<<bit<<endl;\n int ng=0;\n for(int j=0;j<w;j++)\n if(ps[j]<0&&!vs[j]) ng=1;\n if(ng) continue;\n\n UnionFind uf(w*2);\n for(int j=0;j+1<w;j++)\n if(vs[j]&&vs[j+1]) uf.unite(w+j,w+j+1);\n\n for(int j=0;j<w;j++)\n for(int k=0;k<w;k++)\n if(~ps[j]&&ps[j]==ps[k])\n uf.unite(j,k);\n\n for(int j=0;j<w;j++){\n if(ps[j]<0||!vs[j]) continue;\n if(uf.same(j,w+j)) ng=1;\n uf.unite(j,w+j);\n }\n if(ng) continue;\n\n for(int j=0;j<w*2;j++) uf.find(j);\n vector<int> qs=uf.ps;\n for(int j=0;j<w;j++)\n if(~ps[j]&&qs[j]<w) ng=1;\n if(ng) continue;\n\n vector<int> rs(w);\n for(int j=0;j<w;j++) rs[j]=qs[w+j]-w;\n for(int j=0;j<w;j++) if(!vs[j]) rs[j]=-1;\n nx[rs];\n chmax(nx[rs],p.second+__builtin_popcount(bit));\n }\n }\n swap(dp,nx);\n }\n\n int ans=-1;\n for(auto p:dp){\n vector<int> ps=p.first;\n set<int> ss;\n for(int j=0;j<w;j++)\n if(~ps[j]) ss.emplace(ps[j]);\n if(ss.size()==1)\n chmax(ans,p.second);\n }\n cout<<ans<<endl;\n return 0;\n}", "accuracy": 1, "time_ms": 610, "memory_kb": 3840, "score_of_the_acc": -0.5268, "final_rank": 10 }, { "submission_id": "aoj_2314_3697117", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 99999999999999999\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n#define NUM 15\n#define UNDEFINED 0\n#define WHITE 1\n\nstruct Info{\n\tInfo(int arg_row,int arg_col){\n\t\trow = arg_row;\n\t\tcol = arg_col;\n\t}\n\tint row,col;\n};\n\nint H,W;\nint diff_row[4] = {-1,0,0,1},diff_col[4] = {0,-1,1,0};\nint boss[NUM],height[NUM];\nbool check[NUM][NUM],FLG;\nchar table[NUM][NUM];\nmap<ll,int> dp[NUM][NUM];\nvector<Info> G[NUM][NUM];\n\n\n//自分のボスのindexを取得しつつ、経路圧縮を行う関数\nint get_boss(int id){\n\tif(boss[id] == id)return id; //自分が代表なら、自分の値を返す\n\telse{\n\t\treturn boss[id] = get_boss(boss[id]); //代表でないなら、自分が所属する組織の代表を返しつつ、経路圧縮\n\t}\n}\n\nint is_same(int x,int y){\n\treturn get_boss(x) == get_boss(y);\n}\n\nvoid unite(int x,int y){\n\tint boss_x = get_boss(x);\n\tint boss_y = get_boss(y);\n\n\t//既に同じグループなら何もしない\n\tif(boss_x == boss_y)return;\n\n\t//高さが高い方に吸収する\n\tif(height[x] > height[y]){\n\n\t\tboss[boss_y] = boss_x;\n\n\t}else if(height[x] < height[y]){\n\n\t\tboss[boss_x] = boss_y;\n\n\t}else{ //height[x] == height[y]\n\n\t\tboss[boss_y] = boss_x;\n\t\theight[x]++;\n\t}\n}\n\n\nbool rangeCheck(int row,int col){\n\n\treturn row >= 0 && row <= H-1 && col >= 0 && col <= W-1;\n}\n\nll get_code(ll array[NUM]){\n\n\tll ret = 0;\n\n\tfor(int i = 0; i < W; i++){\n\n\t\tret = 10*ret+array[i];\n\t}\n\n\treturn ret;\n}\n\nbool is_ok(ll array[NUM]){\n\n\t//白マスが連続していないか調べる\n\tfor(int col = 0; col < W-1; col++){\n\n\t\tif(array[col] == WHITE && array[col+1] == WHITE){\n\n\t\t\treturn false;\n\t\t}\n\t}\n\n\t//任意の2つの黒セル間には1本だけ経路がある→グループは1つでなければならない\n\tfor(int col = 0; col < W; col++){\n\t\tif(array[col] >= 3){\n\n\t\t\treturn false;\n\t\t}\n\t}\n\n\treturn true;\n}\n\nvoid rename(ll array[NUM]){\n\n\tll check[100];\n\n\tfor(int i = 0; i < 100; i++){\n\n\t\tcheck[i] = -1;\n\t}\n\n\tll next_id = 2;\n\n\tfor(int i = 0; i < W; i++){\n\t\tif(array[i] == UNDEFINED || array[i] == WHITE)continue;\n\n\t\tif(check[array[i]] == -1){\n\t\t\tcheck[array[i]] = next_id++;\n\t\t}\n\t}\n\n\tfor(int i = 0; i < W; i++){\n\t\tif(array[i] == UNDEFINED || array[i] == WHITE)continue;\n\t\tarray[i] = check[array[i]];\n\t}\n}\n\nint recursive(int row,int col,ll array[NUM],int last_group){\n\n\tif(row == H){\n\n\t\tif(is_ok(array)){\n\n\t\t\treturn 0;\n\n\t\t}else{\n\n\t\t\treturn -BIG_NUM;\n\t\t}\n\t}\n\n\tll tmp_code = get_code(array);\n\n\tauto at = dp[row][col].find(tmp_code); //dpは、この状態から見て、これから未来に得られる最大値を持つ\n\tif(at != dp[row][col].end()){\n\n\t\treturn dp[row][col][tmp_code];\n\t}\n\n\tint next_row,next_col,next_last_group = last_group;\n\tll next_array[2][NUM];\n\n\t//次のrowとcolを設定\n\tif(col == W-1){\n\n\t\tnext_row = row+1;\n\t\tnext_col = 0;\n\n\t}else{\n\n\t\tnext_row = row;\n\t\tnext_col = col+1;\n\t}\n\n\tfor(int i = 0; i < 2; i++){\n\t\tfor(int a = 0; a < W; a++){\n\t\t\tnext_array[i][a] = array[a];\n\t\t}\n\t}\n\tint ret = -BIG_NUM;\n\n\t//今回のマスを黒にする\n\tif(table[row][col] == '#' || table[row][col] == '?'){\n\n\t\tif(col > 0 && next_array[0][col-1] != WHITE && //左が黒\n\t\t\t\tnext_array[0][col] != WHITE && next_array[0][col] != UNDEFINED && //上が黒\n\t\t\t\tnext_array[0][col-1] == next_array[0][col]){ //左と右が同じグループ\n\n\t\t\t//Do nothing:閉路ができてしまうので黒で塗れない\n\n\t\t}else{\n\n\t\t\tif(col == 0 || next_array[0][col-1] == WHITE){ //左端、または左が白→左方に黒はない\n\n\t\t\t\tswitch(next_array[0][col]){\n\t\t\t\tcase UNDEFINED: //左にも上にも黒はない\n\t\t\t\tcase WHITE:\n\t\t\t\t\tnext_array[0][col] = next_last_group++; //新しいグループを作る\n\t\t\t\t\tbreak;\n\t\t\t\tdefault: //上のマスが黒\n\t\t\t\t\t//Do nothing:上のマスと同じグループにする\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\n\t\t\t\trename(next_array[0]);\n\n\t\t\t\tret = max(ret,recursive(next_row,next_col,next_array[0],next_last_group)+1);\n\n\t\t\t}else{ //col >= 1 && array[col-1] == BLACK\n\n\t\t\t\tswitch(next_array[0][col]){\n\t\t\t\tcase UNDEFINED:\n\t\t\t\tcase WHITE:\n\t\t\t\t\tnext_array[0][col] = next_array[0][col-1]; //左のgroupと同じにする\n\t\t\t\t\tbreak;\n\t\t\t\tdefault: //上のマスが黒:上のマスと左のマスは異なるグループであるはず\n\n\t\t\t\t\tint new_id = next_array[0][col]; //★今回のマスの番号を新しいIDとして使用★\n\t\t\t\t\tint old_id = next_array[0][col-1];\n\n\t\t\t\t\tfor(int a = 0; a < W; a++){ //同じIDが飛んで存在する場合あり\n\t\t\t\t\t\tif(next_array[0][a] == old_id){\n\n\t\t\t\t\t\t\tnext_array[0][a] = new_id;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\n\t\t\t\trename(next_array[0]);\n\n\t\t\t\tret = max(ret,recursive(next_row,next_col,next_array[0],next_last_group)+1);\n\n\t\t\t}\n\t\t}\n\t}\n\n\tnext_last_group = last_group;\n\n\t//今回のマスを白にする\n\tif(table[row][col] == '.' || table[row][col] == '?'){\n\n\t\t if(next_array[1][col] == WHITE || (col > 0 && next_array[1][col-1] == WHITE)){ //上か左が白\n\n\t\t\t //Do nothing:白が連続するので不可\n\n\t\t }else{ //上にも左にも白はない\n\n\t\t\t if(next_array[1][col] != UNDEFINED){ //上のマスが黒\n\n\t\t\t\tint count = 0;\n\n\t\t\t\tfor(int a = 0; a < W; a++){\n\t\t\t\t\tif(next_array[1][a] == array[col]){\n\t\t\t\t\t\tcount++;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tif(count == 1){\n\t\t\t\t\t//Do nothing:上のマスが孤立するので不可\n\t\t\t\t}else{\n\n\t\t\t\t\t//上のマスが黒で、左に白がない\n\n\t\t\t\t\tnext_array[1][col] = WHITE;\n\n\t\t\t\t\trename(next_array[1]);\n\n\t\t\t\t\tret = max(ret,recursive(next_row,next_col,next_array[1],next_last_group));\n\t\t\t\t}\n\n\t\t\t }else{\n\n\t\t\t\t //上のマスがUNDFINEDで、左に白がない\n\n\t\t\t\t next_array[1][col] = WHITE;\n\n\t\t\t\t rename(next_array[1]);\n\n\t\t\t\t ret = max(ret,recursive(next_row,next_col,next_array[1],next_last_group));\n\t\t\t }\n\t\t }\n\t}\n\n\treturn dp[row][col][tmp_code] = ret;\n}\n\nvoid dfs(int row,int col,int pre_row,int pre_col){\n\n\tif(!FLG)return;\n\n\tint next_row,next_col;\n\n\tfor(int i = 0; i < G[row][col].size(); i++){\n\n\t\tnext_row = G[row][col][i].row;\n\t\tnext_col = G[row][col][i].col;\n\n\t\tif(next_row == pre_row && next_col == pre_col)continue;\n\n\t\tif(check[next_row][next_col]){ //有向に遷移しているので、一度訪れた頂点をまた訪れたら閉路あり\n\n\t\t\tFLG = false;\n\t\t\treturn;\n\t\t}\n\t\tcheck[next_row][next_col] = true;\n\n\t\tdfs(next_row,next_col,row,col);\n\t}\n}\n\n\nint main(){\n\n\tscanf(\"%d %d\",&H,&W);\n\n\tfor(int row = 0; row < H; row++){\n\n\t\tscanf(\"%s\",table[row]);\n\t}\n\n\t//ややこしいので例外処理\n\tif(H == 1 || W == 1){\n\n\t\tif(H == 1 && W == 1){\n\n\t\t\tswitch(table[0][0]){\n\t\t\tcase '.':\n\t\t\t\tprintf(\"0\\n\");\n\t\t\t\tbreak;\n\t\t\tcase '#':\n\t\t\t\tprintf(\"1\\n\");\n\t\t\t\tbreak;\n\t\t\tcase '?':\n\t\t\t\tprintf(\"1\\n\");\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\treturn 0;\n\n\t\t}else if(H == 1){\n\n\t\t\t//白ますが連続していたら不可\n\t\t\tfor(int col = 0; col < W-1; col++){\n\t\t\t\tif(table[0][col] == '.' && table[0][col+1] == '.'){\n\t\t\t\t\tprintf(\"-1\\n\");\n\t\t\t\t\treturn 0;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tint ans = 0;\n\n\t\t\t//とりあえず?のところを前部黒にして、連結しているか調べる\n\t\t\tfor(int col = 0; col < W; col++){\n\t\t\t\tif(table[0][col] == '?'){\n\t\t\t\t\ttable[0][col] = '#';\n\t\t\t\t\tans++;\n\t\t\t\t}else if(table[0][col] == '#'){\n\t\t\t\t\tans++;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tfor(int i = 0; i < W; i++){\n\n\t\t\t\tboss[i] = i;\n\t\t\t\theight[i] = 0;\n\t\t\t}\n\n\t\t\tfor(int col = 0; col < W-1; col++){\n\t\t\t\tif(table[0][col] == '#' && table[0][col+1] == '#'){\n\t\t\t\t\tunite(col,col+1);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tint num_group = 0;\n\n\t\t\tfor(int col = 0; col < W; col++){\n\t\t\t\tif(table[0][col] == '#'){\n\t\t\t\t\tif(get_boss(col) == col){\n\t\t\t\t\t\tnum_group++;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif(num_group >= 2){\n\n\t\t\t\tprintf(\"-1\\n\");\n\n\t\t\t}else{\n\n\t\t\t\tprintf(\"%d\\n\",ans);\n\t\t\t}\n\n\t\t\treturn 0;\n\n\t\t}else{ //W == 1\n\n\n\t\t\t//白ますが連続していたら不可\n\t\t\tfor(int row = 0; row < H-1; row++){\n\t\t\t\tif(table[row][0] == '.' && table[row+1][0] == '.'){\n\t\t\t\t\tprintf(\"-1\\n\");\n\t\t\t\t\treturn 0;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tint ans = 0;\n\n\t\t\t//とりあえず?のところを前部黒にして、連結しているか調べる\n\t\t\tfor(int row = 0; row < H; row++){\n\t\t\t\tif(table[row][0] == '?'){\n\t\t\t\t\ttable[row][0] = '#';\n\t\t\t\t\tans++;\n\t\t\t\t}else if(table[row][0] == '#'){\n\t\t\t\t\tans++;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tfor(int i = 0; i < H; i++){\n\n\t\t\t\tboss[i] = i;\n\t\t\t\theight[i] = 0;\n\t\t\t}\n\n\t\t\tfor(int row = 0; row < H-1; row++){\n\t\t\t\tif(table[row][0] == '#' && table[row+1][0] == '#'){\n\t\t\t\t\tunite(row,row+1);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tint num_group = 0;\n\n\t\t\tfor(int row = 0; row < H; row++){\n\t\t\t\tif(table[row][0] == '#'){\n\t\t\t\t\tif(get_boss(row) == row){\n\t\t\t\t\t\tnum_group++;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif(num_group >= 2){\n\n\t\t\t\tprintf(\"-1\\n\");\n\n\t\t\t}else{\n\n\t\t\t\tprintf(\"%d\\n\",ans);\n\t\t\t}\n\t\t\treturn 0;\n\t\t}\n\t}\n\n\tint adj_row,adj_col;\n\n\t//最初から閉路ができてないか調べる(dfsで調べるため、エッジを作る)\n\tfor(int row = 0; row < H; row++){\n\t\tfor(int col = 0; col < W; col++){\n\t\t\tif(table[row][col] != '#')continue;\n\n\t\t\tfor(int i = 0; i < 4; i++){\n\n\t\t\t\tadj_row = row+diff_row[i];\n\t\t\t\tadj_col = col+diff_col[i];\n\n\t\t\t\tif(rangeCheck(adj_row,adj_col) == false || table[adj_row][adj_col] != '#')continue;\n\n\t\t\t\tG[row][col].push_back(Info(adj_row,adj_col));\n\t\t\t}\n\t\t}\n\t}\n\n\tfor(int row = 0; row < H; row++){\n\t\tfor(int col = 0; col < W; col++){\n\t\t\tif(table[row][col] != '#')continue;\n\n\t\t\tfor(int a = 0; a < H; a++){\n\t\t\t\tfor(int b = 0; b < W; b++){\n\t\t\t\t\tcheck[a][b] = false;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tcheck[row][col] = true;\n\t\t\tFLG= true;\n\n\t\t\tdfs(row,col,-1,-1);\n\n\t\t\tif(!FLG){\n\n\t\t\t\tprintf(\"-1\\n\");\n\t\t\t\treturn 0;\n\t\t\t}\n\t\t}\n\t}\n\n\tll first_array[NUM];\n\n\tfor(int i = 0; i < W; i++){\n\n\t\tfirst_array[i] = UNDEFINED;\n\t}\n\n\tint ret = recursive(0,0,first_array,2);\n\n\tif(ret < 0){\n\n\t\tprintf(\"-1\\n\");\n\t}else{\n\n\t\tprintf(\"%d\\n\",ret);\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 120, "memory_kb": 21092, "score_of_the_acc": -0.2435, "final_rank": 6 }, { "submission_id": "aoj_2314_3696868", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 99999999999999999\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n#define NUM 15\n#define UNDEFINED 0\n#define WHITE 1\n\nstruct Info{\n\tInfo(int arg_row,int arg_col){\n\t\trow = arg_row;\n\t\tcol = arg_col;\n\t}\n\tint row,col;\n};\n\nint H,W;\nint diff_row[4] = {-1,0,0,1},diff_col[4] = {0,-1,1,0};\nint boss[NUM],height[NUM];\nbool check[NUM][NUM],FLG;\nchar table[NUM][NUM];\nmap<ll,int> dp[NUM][NUM];\nvector<Info> G[NUM][NUM];\n\n\n//自分のボスのindexを取得しつつ、経路圧縮を行う関数\nint get_boss(int id){\n\tif(boss[id] == id)return id; //自分が代表なら、自分の値を返す\n\telse{\n\t\treturn boss[id] = get_boss(boss[id]); //代表でないなら、自分が所属する組織の代表を返しつつ、経路圧縮\n\t}\n}\n\nint is_same(int x,int y){\n\treturn get_boss(x) == get_boss(y);\n}\n\nvoid unite(int x,int y){\n\tint boss_x = get_boss(x);\n\tint boss_y = get_boss(y);\n\n\t//既に同じグループなら何もしない\n\tif(boss_x == boss_y)return;\n\n\t//高さが高い方に吸収する\n\tif(height[x] > height[y]){\n\n\t\tboss[boss_y] = boss_x;\n\n\t}else if(height[x] < height[y]){\n\n\t\tboss[boss_x] = boss_y;\n\n\t}else{ //height[x] == height[y]\n\n\t\tboss[boss_y] = boss_x;\n\t\theight[x]++;\n\t}\n}\n\n\nbool rangeCheck(int row,int col){\n\n\treturn row >= 0 && row <= H-1 && col >= 0 && col <= W-1;\n}\n\nll get_code(int array[NUM]){\n\n\tll ret = 0;\n\n\tfor(int i = 0; i < W; i++){\n\n\t\tret = 10*ret+array[i];\n\t}\n\n\treturn ret;\n}\n\nbool is_ok(int array[NUM]){\n\n\t//白マスが連続していないか調べる\n\tfor(int col = 0; col < W-1; col++){\n\n\t\tif(array[col] == WHITE && array[col+1] == WHITE){\n\n\t\t\treturn false;\n\t\t}\n\t}\n\n\treturn true;\n}\n\n\nint recursive(int row,int col,int array[NUM],int last_group){\n\n\tif(row == H){\n\n\t\tif(is_ok(array)){\n\n\t\t\treturn 0;\n\n\t\t}else{\n\n\t\t\treturn -BIG_NUM;\n\t\t}\n\t}\n\n\tll tmp_code = get_code(array);\n\n\tauto at = dp[row][col].find(tmp_code);\n\tif(at != dp[row][col].end()){\n\n\t\treturn dp[row][col][tmp_code];\n\t}\n\n\tint next_row,next_col,next_array[2][NUM],next_last_group = last_group;\n\n\t//次のrowとcolを設定\n\tif(col == W-1){\n\n\t\tnext_row = row+1;\n\t\tnext_col = 0;\n\n\t}else{\n\n\t\tnext_row = row;\n\t\tnext_col = col+1;\n\t}\n\n\tfor(int i = 0; i < 2; i++){\n\t\tfor(int a = 0; a < W; a++){\n\t\t\tnext_array[i][a] = array[a];\n\t\t}\n\t}\n\tint ret = -BIG_NUM;\n\n\t//今回のマスを黒にする\n\tif(table[row][col] == '#' || table[row][col] == '?'){\n\n\t\tif(col > 0 && next_array[0][col-1] != WHITE && //左が黒\n\t\t\t\tnext_array[0][col] != WHITE && next_array[0][col] != UNDEFINED && //上が黒\n\t\t\t\tnext_array[0][col-1] == next_array[0][col]){ //左と右が同じグループ\n\n\t\t\t//Do nothing:閉路ができてしまうので黒で塗れない\n\n\t\t}else{\n\n\t\t\tif(col == 0 || next_array[0][col-1] == WHITE){ //左端、または左が白→左方に黒はない\n\n\t\t\t\tswitch(next_array[0][col]){\n\t\t\t\tcase UNDEFINED: //左にも上にも黒はない\n\t\t\t\tcase WHITE:\n\t\t\t\t\tnext_array[0][col] = next_last_group++; //新しいグループを作る\n\t\t\t\t\tbreak;\n\t\t\t\tdefault: //上のマスが黒\n\t\t\t\t\t//Do nothing:上のマスと同じグループにする\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tret = max(ret,recursive(next_row,next_col,next_array[0],next_last_group)+1);\n\n\t\t\t}else{ //col >= 1 && array[col-1] == BLACK\n\n\t\t\t\tswitch(next_array[0][col]){\n\t\t\t\tcase UNDEFINED:\n\t\t\t\tcase WHITE:\n\t\t\t\t\tnext_array[0][col] = next_array[0][col-1]; //左のgroupと同じにする\n\t\t\t\t\tbreak;\n\t\t\t\tdefault: //上のマスが黒:上のマスと左のマスは異なるグループであるはず\n\n\t\t\t\t\tint new_id = next_array[0][col]; //★今回のマスの番号を新しいIDとして使用★\n\t\t\t\t\tint old_id = next_array[0][col-1];\n\n\t\t\t\t\tfor(int a = 0; a < W; a++){ //同じIDが飛んで存在する場合あり\n\t\t\t\t\t\tif(next_array[0][a] == old_id){\n\n\t\t\t\t\t\t\tnext_array[0][a] = new_id;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tret = max(ret,recursive(next_row,next_col,next_array[0],next_last_group)+1);\n\n\t\t\t}\n\t\t}\n\t}\n\n\tnext_last_group = last_group;\n\n\t//今回のマスを白にする\n\tif(table[row][col] == '.' || table[row][col] == '?'){\n\n\t\t if(next_array[1][col] == WHITE || (col > 0 && next_array[1][col-1] == WHITE)){ //上か左が白\n\n\t\t\t //Do nothing:白が連続するので不可\n\n\t\t }else{ //上にも左にも白はない\n\n\t\t\t if(next_array[1][col] != UNDEFINED){ //上のマスが黒\n\n\t\t\t\tint count = 0;\n\n\t\t\t\tfor(int a = 0; a < W; a++){\n\t\t\t\t\tif(next_array[1][a] == array[col]){\n\t\t\t\t\t\tcount++;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tif(count == 1){\n\t\t\t\t\t//Do nothing:上のマスが孤立するので不可\n\t\t\t\t}else{\n\n\t\t\t\t\t//上のマスが黒で、左に白がない\n\n\t\t\t\t\tnext_array[1][col] = WHITE;\n\t\t\t\t\tret = max(ret,recursive(next_row,next_col,next_array[1],next_last_group));\n\t\t\t\t}\n\n\t\t\t }else{\n\n\t\t\t\t //上のマスがUNDFINEDで、左に白がない\n\n\t\t\t\t next_array[1][col] = WHITE;\n\t\t\t\t ret = max(ret,recursive(next_row,next_col,next_array[1],next_last_group));\n\t\t\t }\n\t\t }\n\t}\n\n\treturn dp[row][col][tmp_code] = ret;\n}\n\nvoid dfs(int row,int col,int pre_row,int pre_col){\n\n\tif(!FLG)return;\n\n\tint next_row,next_col;\n\n\tfor(int i = 0; i < G[row][col].size(); i++){\n\n\t\tnext_row = G[row][col][i].row;\n\t\tnext_col = G[row][col][i].col;\n\n\t\tif(next_row == pre_row && next_col == pre_col)continue;\n\n\t\tif(check[next_row][next_col]){ //有向に遷移しているので、一度訪れた頂点をまた訪れたら閉路あり\n\n\t\t\tFLG = false;\n\t\t\treturn;\n\t\t}\n\t\tcheck[next_row][next_col] = true;\n\n\t\tdfs(next_row,next_col,row,col);\n\t}\n}\n\n\nint main(){\n\n\tscanf(\"%d %d\",&H,&W);\n\n\tfor(int row = 0; row < H; row++){\n\n\t\tscanf(\"%s\",table[row]);\n\t}\n\n\t//ややこしいので例外処理\n\tif(H == 1 || W == 1){\n\n\t\tif(H == 1 && W == 1){\n\n\t\t\tswitch(table[0][0]){\n\t\t\tcase '.':\n\t\t\t\tprintf(\"0\\n\");\n\t\t\t\tbreak;\n\t\t\tcase '#':\n\t\t\t\tprintf(\"1\\n\");\n\t\t\t\tbreak;\n\t\t\tcase '?':\n\t\t\t\tprintf(\"1\\n\");\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\treturn 0;\n\n\t\t}else if(H == 1){\n\n\t\t\t//白ますが連続していたら不可\n\t\t\tfor(int col = 0; col < W-1; col++){\n\t\t\t\tif(table[0][col] == '.' && table[0][col+1] == '.'){\n\t\t\t\t\tprintf(\"-1\\n\");\n\t\t\t\t\treturn 0;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tint ans = 0;\n\n\t\t\t//とりあえず?のところを前部黒にして、連結しているか調べる\n\t\t\tfor(int col = 0; col < W; col++){\n\t\t\t\tif(table[0][col] == '?'){\n\t\t\t\t\ttable[0][col] = '#';\n\t\t\t\t\tans++;\n\t\t\t\t}else if(table[0][col] == '#'){\n\t\t\t\t\tans++;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tfor(int i = 0; i < W; i++){\n\n\t\t\t\tboss[i] = i;\n\t\t\t\theight[i] = 0;\n\t\t\t}\n\n\t\t\tfor(int col = 0; col < W-1; col++){\n\t\t\t\tif(table[0][col] == '#' && table[0][col+1] == '#'){\n\t\t\t\t\tunite(col,col+1);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tint num_group = 0;\n\n\t\t\tfor(int col = 0; col < W; col++){\n\t\t\t\tif(table[0][col] == '#'){\n\t\t\t\t\tif(get_boss(col) == col){\n\t\t\t\t\t\tnum_group++;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif(num_group >= 2){\n\n\t\t\t\tprintf(\"-1\\n\");\n\n\t\t\t}else{\n\n\t\t\t\tprintf(\"%d\\n\",ans);\n\t\t\t}\n\n\t\t\treturn 0;\n\n\t\t}else{ //W == 1\n\n\n\t\t\t//白ますが連続していたら不可\n\t\t\tfor(int row = 0; row < H-1; row++){\n\t\t\t\tif(table[row][0] == '.' && table[row+1][0] == '.'){\n\t\t\t\t\tprintf(\"-1\\n\");\n\t\t\t\t\treturn 0;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tint ans = 0;\n\n\t\t\t//とりあえず?のところを前部黒にして、連結しているか調べる\n\t\t\tfor(int row = 0; row < H; row++){\n\t\t\t\tif(table[row][0] == '?'){\n\t\t\t\t\ttable[row][0] = '#';\n\t\t\t\t\tans++;\n\t\t\t\t}else if(table[row][0] == '#'){\n\t\t\t\t\tans++;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tfor(int i = 0; i < H; i++){\n\n\t\t\t\tboss[i] = i;\n\t\t\t\theight[i] = 0;\n\t\t\t}\n\n\t\t\tfor(int row = 0; row < H-1; row++){\n\t\t\t\tif(table[row][0] == '#' && table[row+1][0] == '#'){\n\t\t\t\t\tunite(row,row+1);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tint num_group = 0;\n\n\t\t\tfor(int row = 0; row < H; row++){\n\t\t\t\tif(table[row][0] == '#'){\n\t\t\t\t\tif(get_boss(row) == row){\n\t\t\t\t\t\tnum_group++;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif(num_group >= 2){\n\n\t\t\t\tprintf(\"-1\\n\");\n\n\t\t\t}else{\n\n\t\t\t\tprintf(\"%d\\n\",ans);\n\t\t\t}\n\t\t\treturn 0;\n\t\t}\n\t}\n\n\tint adj_row,adj_col;\n\n\t//最初から閉路ができてないか調べる(dfsで調べるため、エッジを作る)\n\tfor(int row = 0; row < H; row++){\n\t\tfor(int col = 0; col < W; col++){\n\t\t\tif(table[row][col] != '#')continue;\n\n\t\t\tfor(int i = 0; i < 4; i++){\n\n\t\t\t\tadj_row = row+diff_row[i];\n\t\t\t\tadj_col = col+diff_col[i];\n\n\t\t\t\tif(rangeCheck(adj_row,adj_col) == false || table[adj_row][adj_col] != '#')continue;\n\n\t\t\t\tG[row][col].push_back(Info(adj_row,adj_col));\n\t\t\t}\n\t\t}\n\t}\n\n\tfor(int row = 0; row < H; row++){\n\t\tfor(int col = 0; col < W; col++){\n\t\t\tif(table[row][col] != '#')continue;\n\n\t\t\tfor(int a = 0; a < H; a++){\n\t\t\t\tfor(int b = 0; b < W; b++){\n\t\t\t\t\tcheck[a][b] = false;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tcheck[row][col] = true;\n\t\t\tFLG= true;\n\n\t\t\tdfs(row,col,-1,-1);\n\n\t\t\tif(!FLG){\n\n\t\t\t\tprintf(\"-1\\n\");\n\t\t\t\treturn 0;\n\t\t\t}\n\t\t}\n\t}\n\n\tint first_array[NUM];\n\n\tfor(int i = 0; i < W; i++){\n\n\t\tfirst_array[i] = UNDEFINED;\n\t}\n\n\tint ret = recursive(0,0,first_array,2);\n\n\tif(ret < 0){\n\n\t\tprintf(\"-1\\n\");\n\t}else{\n\n\t\tprintf(\"%d\\n\",ret);\n\t}\n\n\treturn 0;\n}", "accuracy": 0.19811320754716982, "time_ms": 1140, "memory_kb": 115700, "score_of_the_acc": -2, "final_rank": 19 }, { "submission_id": "aoj_2314_3626649", "code_snippet": "#include<iostream>\n#include<string>\n#include<algorithm>\n#include<vector>\n#include<iomanip>\n#include<math.h>\n#include<complex>\n#include<queue>\n#include<deque>\n#include<stack>\n#include<map>\n#include<set>\n#include<bitset>\n#include<functional>\n#include<assert.h>\n#include<numeric>\nusing namespace std;\n#define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i )\n#define rep(i,n) REP(i,0,n)\nusing ll = long long;\nconst int inf=1e9+7;\nconst ll longinf=1LL<<60 ;\nconst ll mod=1e9+7 ;\n\nstruct UnionFind{\n vector<int> par;\n UnionFind(int n):par(n,-1){}\n int find(int x){\n if(par[x]<0)return x;\n return par[x]=find(par[x]);\n }\n bool unite(int x,int y){\n x=find(x);\n y=find(y);\n if(x>y)swap(x,y);\n if(x==y)return false;\n else{\n par[x]+=par[y];\n par[y]=x;\n }\n return true;\n }\n\n bool same(int x,int y){\n return find(x)==find(y);\n }\n int size(int x){\n return -par[find(x)];\n }\n};\nint main(){\n int h,w;\n cin>>h>>w;\n string s[h];\n rep(i,h)cin>>s[i];\n map<ll,int> dp[h];\n int all=1<<w;\n rep(i,all){\n int a[w];\n rep(j,w){\n if(s[0][j]=='.')a[j]=0;\n if(s[0][j]=='#')a[j]=1;\n if(s[0][j]=='?')a[j]=(i>>j)&1;\n }\n UnionFind uf(w+1);\n bool ok=true;\n rep(j,w-1){\n if(a[j]&&a[j+1])uf.unite(j,j+1);\n if(!a[j]&&!a[j+1])ok=false;\n }\n if(!ok)continue;\n ll res=0;\n int cnt=0;\n rep(j,w){\n res*=12;\n if(a[j])res+=(uf.find(j)+1),cnt++;\n }\n dp[0][res]=max(dp[0][res],cnt);\n }\n rep(i,h-1){\n rep(mask,all){\n int a[w];\n bool can=true;\n rep(j,w){\n if(s[i+1][j]=='.')a[j]=0;\n if(s[i+1][j]=='#')a[j]=1;\n if(s[i+1][j]=='?')a[j]=(mask>>j)&1;\n }\n rep(j,w-1){\n if(!a[j]&&!a[j+1])can=false;\n }\n if(!can)continue;\n for(auto p : dp[i]){\n int b[w];\n ll cur=p.first;\n rep(j,w){\n b[w-j-1]=12+cur%12;\n cur/=12;\n }\n bool ok=true;\n UnionFind uf(26);\n rep(j,w-1){\n if(a[j]&&a[j+1])uf.unite(j,j+1);\n }\n rep(j,w){\n if(a[j]&&b[j]>12){\n if(!uf.unite(j,b[j]))ok=false;\n }\n if(!a[j]&&b[j]==12)ok=false;\n }\n if(i!=h-2||w!=1)rep(j,w)if(uf.find(b[j])>12)ok=false;\n if(!ok)continue;\n ll res=0;\n int cnt=p.second;\n rep(j,w){\n res*=12;\n if(a[j]){\n res+=(uf.find(j)+1),cnt++;\n }\n }\n dp[i+1][res]=max(dp[i+1][res],cnt);\n }\n }\n }\n int ans=-1;\n for(auto p : dp[h-1]){\n ll cur=p.first;\n bool ok=true;\n set<int> st;\n while(cur){\n if(cur%12)st.insert(cur%12);\n cur/=12;\n }\n if(st.size()==1||st.size()==0)ans=max(ans,p.second);\n }\n\n cout<<ans<<endl;\n \n return 0;\n}", "accuracy": 1, "time_ms": 560, "memory_kb": 4444, "score_of_the_acc": -0.4875, "final_rank": 8 }, { "submission_id": "aoj_2314_3626636", "code_snippet": "#include<iostream>\n#include<string>\n#include<algorithm>\n#include<vector>\n#include<iomanip>\n#include<math.h>\n#include<complex>\n#include<queue>\n#include<deque>\n#include<stack>\n#include<map>\n#include<set>\n#include<bitset>\n#include<functional>\n#include<assert.h>\n#include<numeric>\nusing namespace std;\n#define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i )\n#define rep(i,n) REP(i,0,n)\nusing ll = long long;\nconst int inf=1e9+7;\nconst ll longinf=1LL<<60 ;\nconst ll mod=1e9+7 ;\n\nstruct UnionFind{\n vector<int> par;\n UnionFind(int n):par(n,-1){}\n int find(int x){\n if(par[x]<0)return x;\n return par[x]=find(par[x]);\n }\n bool unite(int x,int y){\n x=find(x);\n y=find(y);\n if(x>y)swap(x,y);\n if(x==y)return false;\n else{\n par[x]+=par[y];\n par[y]=x;\n }\n return true;\n }\n\n bool same(int x,int y){\n return find(x)==find(y);\n }\n int size(int x){\n return -par[find(x)];\n }\n};\nint main(){\n int h,w;\n cin>>h>>w;\n string s[h];\n rep(i,h)cin>>s[i];\n map<ll,int> dp[h];\n int all=1<<w;\n rep(i,all){\n int a[w];\n rep(j,w){\n if(s[0][j]=='.')a[j]=0;\n if(s[0][j]=='#')a[j]=1;\n if(s[0][j]=='?')a[j]=(i>>j)&1;\n }\n UnionFind uf(w+1);\n bool ok=true;\n rep(j,w-1){\n if(a[j]&&a[j+1])uf.unite(j,j+1);\n if(!a[j]&&!a[j+1])ok=false;\n }\n if(!ok)continue;\n ll res=0;\n int cnt=0;\n rep(j,w){\n res*=12;\n if(a[j])res+=(uf.find(j)+1),cnt++;\n }\n dp[0][res]=max(dp[0][res],cnt);\n }\n rep(i,h-1){\n rep(mask,all){\n int a[w];\n bool can=true;\n rep(j,w){\n if(s[i+1][j]=='.')a[j]=0;\n if(s[i+1][j]=='#')a[j]=1;\n if(s[i+1][j]=='?')a[j]=(mask>>j)&1;\n }\n rep(j,w-1){\n if(!a[j]&&!a[j+1])can=false;\n }\n if(!can)continue;\n for(auto p : dp[i]){\n int b[w];\n ll cur=p.first;\n rep(j,w){\n b[w-j-1]=12+cur%12;\n cur/=12;\n }\n bool ok=true;\n UnionFind uf(26);\n rep(j,w-1){\n if(a[j]&&a[j+1])uf.unite(j,j+1);\n }\n rep(j,w){\n if(a[j]&&b[j]>12){\n if(!uf.unite(j,b[j]))ok=false;\n }\n if(!a[j]&&b[j]==12)ok=false;\n }\n if(i!=h-2)rep(j,w)if(uf.find(b[j])>12)ok=false;\n if(!ok)continue;\n ll res=0;\n int cnt=p.second;\n rep(j,w){\n res*=12;\n if(a[j]){\n res+=(uf.find(j)+1),cnt++;\n }\n }\n dp[i+1][res]=max(dp[i+1][res],cnt);\n }\n }\n }\n int ans=-1;\n for(auto p : dp[h-1])ans=max(ans,p.second);\n cout<<ans<<endl;\n \n return 0;\n}", "accuracy": 0.4811320754716981, "time_ms": 570, "memory_kb": 4256, "score_of_the_acc": -0.4948, "final_rank": 16 }, { "submission_id": "aoj_2314_3351555", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nclass UnionFind{\n vector<int> par;\n vector<int> rank;\n public:\n UnionFind(int n):rank(n),par(n){\n iota(par.begin(),par.end(),0);\n }\n int find(int x){\n if(par[x]==x) return x;\n else return par[x]=find(par[x]);\n }\n void unite(int x,int y){\n x=find(x),y=find(y);\n if(rank[x]<rank[y]) par[x]=y;\n else{\n if(rank[x]==rank[y]) rank[x]++;\n par[y]=x;\n }\n }\n bool isSame(int x,int y){\n return find(x)==find(y);\n }\n};\nint main(){\n int h,w;\n cin>>h>>w;\n vector<string> c(h);\n for(int i=0;i<h;i++) cin>>c[i];\n \n vector<int> vec;\n for(int bit=0;bit<(1<<w);bit++){\n bool isok=true;\n for(int i=0;i+1<w;i++){\n isok = isok && (((bit>>i)&1) || ((bit>>(i+1))&1));\n }\n if(isok) vec.push_back(bit);\n }\n auto isValid=[&](int bit,int row){\n bool isok=true;\n for(int i=0;i<w;i++){\n if(c[row][i]=='#'){\n isok= isok && ((bit>>i)&1);\n }\n else if(c[row][i]=='.'){\n isok= isok && (!((bit>>i)&1));\n }\n }\n return isok;\n };\n map<vector<int>,int> dp;\n map<vector<int>,int> dpX;\n for(auto bit:vec){\n if(!isValid(bit,0)) continue;\n vector<int> st(w);\n int idx=1;\n for(int i=0;i<w;i++){\n if((bit>>i)&1){\n for(;i<w;i++){\n if(!((bit>>i)&1)) break;\n st[i]=idx;\n }\n idx++;\n }\n }\n\n dp[st]=__builtin_popcount(bit);\n }\n dpX=dp;\n for(int i=1;i<h;i++){\n if(i+1==h) dpX=dp;\n map<vector<int>,int> next;\n for(auto bit:vec){\n if(!isValid(bit,i)) continue;\n for(auto &e:dp){\n vector<int> st=e.first;\n int cnt=e.second;\n bool isok=true;\n int v=*max_element(st.begin(),st.end());\n vector<int> hasChil(v);\n for(int i=0;i<w;i++){\n isok = isok && !(st[i]==0 && ((bit>>i)&1)==0);\n if(st[i]!=0){\n hasChil[st[i]-1]=true;\n }\n }\n if(accumulate(hasChil.begin(),hasChil.end(),0)!=v) continue;\n UnionFind u(v+w);\n for(int i=0;i<w;i++){\n if(!((bit>>i)&1)) continue;\n if(st[i]!=0){\n if(u.isSame(st[i]-1,v+i)){\n isok=false;\n break; \n }\n u.unite(st[i]-1,v+i);\n }\n if(((bit>>(i+1))&1)){\n u.unite(v+i,v+i+1);\n }\n }\n if(!isok) continue;\n for(int i=0;i<v;i++){\n for(int j=0;j<=w;j++){\n if(j==w){\n isok=false;\n break;\n }\n if(u.isSame(i,v+j)) break;\n }\n }\n if(!isok) continue;\n int idx=1;\n vector<int> to(w);\n \n for(int i=0;i<w;i++){\n if(!((bit>>i)&1)) continue;\n for(int j=0;j<i;j++){\n if(u.isSame(v+i,v+j)){\n to[i]=to[j];\n break;\n }\n }\n if(to[i]==0) to[i]=idx++;\n }\n next[to]=max(next[to],cnt+__builtin_popcount(bit));\n }\n }\n dp=next;\n }\n int res=-1;\n for(int i=0;i<=w;i++){\n if(i==w && w==1){\n for(auto &e:dpX){\n if(*max_element(e.first.begin(),e.first.end())>1) continue;;\n res=max(res,e.second);\n } \n break;\n }\n if(i==w) break;\n if(c[h-1][i]=='#') break;\n }\n \n for(auto &e:dp){\n if(*max_element(e.first.begin(),e.first.end())>1) continue;;\n res=max(res,e.second);\n }\n cout<<res<<endl;\n return 0;\n}", "accuracy": 1, "time_ms": 770, "memory_kb": 4096, "score_of_the_acc": -0.6719, "final_rank": 11 }, { "submission_id": "aoj_2314_3351551", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nclass UnionFind{\n vector<int> par;\n vector<int> rank;\n public:\n UnionFind(int n):rank(n),par(n){\n iota(par.begin(),par.end(),0);\n }\n int find(int x){\n if(par[x]==x) return x;\n else return par[x]=find(par[x]);\n }\n void unite(int x,int y){\n x=find(x),y=find(y);\n if(rank[x]<rank[y]) par[x]=y;\n else{\n if(rank[x]==rank[y]) rank[x]++;\n par[y]=x;\n }\n }\n bool isSame(int x,int y){\n return find(x)==find(y);\n }\n};\nint main(){\n int h,w;\n cin>>h>>w;\n vector<string> c(h);\n for(int i=0;i<h;i++) cin>>c[i];\n if(h==2){\n \n }\n vector<int> vec;\n for(int bit=0;bit<(1<<w);bit++){\n bool isok=true;\n for(int i=0;i+1<w;i++){\n isok = isok && (((bit>>i)&1) || ((bit>>(i+1))&1));\n }\n if(isok) vec.push_back(bit);\n }\n auto isValid=[&](int bit,int row){\n bool isok=true;\n for(int i=0;i<w;i++){\n if(c[row][i]=='#'){\n isok= isok && ((bit>>i)&1);\n }\n else if(c[row][i]=='.'){\n isok= isok && (!((bit>>i)&1));\n }\n }\n return isok;\n };\n map<vector<int>,int> dp;\n map<vector<int>,int> dpX;\n for(auto bit:vec){\n if(!isValid(bit,0)) continue;\n vector<int> st(w);\n int idx=1;\n for(int i=0;i<w;i++){\n if((bit>>i)&1){\n for(;i<w;i++){\n if(!((bit>>i)&1)) break;\n st[i]=idx;\n }\n idx++;\n }\n }\n dp[st]=__builtin_popcount(bit);\n }\n for(int i=1;i<h;i++){\n if(i+1==h) dpX=dp;\n map<vector<int>,int> next;\n for(auto bit:vec){\n if(!isValid(bit,i)) continue;\n for(auto &e:dp){\n vector<int> st=e.first;\n int cnt=e.second;\n bool isok=true;\n int v=*max_element(st.begin(),st.end());\n vector<int> hasChil(v);\n for(int i=0;i<w;i++){\n isok = isok && !(st[i]==0 && ((bit>>i)&1)==0);\n if(st[i]!=0){\n hasChil[st[i]-1]=true;\n }\n }\n if(accumulate(hasChil.begin(),hasChil.end(),0)!=v) continue;\n UnionFind u(v+w);\n for(int i=0;i<w;i++){\n if(!((bit>>i)&1)) continue;\n if(st[i]!=0){\n if(u.isSame(st[i]-1,v+i)){\n isok=false;\n break; \n }\n u.unite(st[i]-1,v+i);\n }\n if(((bit>>(i+1))&1)){\n u.unite(v+i,v+i+1);\n }\n }\n if(!isok) continue;\n for(int i=0;i<v;i++){\n for(int j=0;j<=w;j++){\n if(j==w){\n isok=false;\n break;\n }\n if(u.isSame(i,v+j)) break;\n }\n }\n if(!isok) continue;\n int idx=1;\n vector<int> to(w);\n \n for(int i=0;i<w;i++){\n if(!((bit>>i)&1)) continue;\n for(int j=0;j<i;j++){\n if(u.isSame(v+i,v+j)){\n to[i]=to[j];\n break;\n }\n }\n if(to[i]==0) to[i]=idx++;\n }\n next[to]=max(next[to],cnt+__builtin_popcount(bit));\n }\n }\n dp=next;\n }\n int res=-1;\n for(int i=0;i<=w;i++){\n if(i==w && w==1){\n for(auto &e:dpX){\n if(*max_element(e.first.begin(),e.first.end())!=1) continue;;\n res=max(res,e.second);\n } \n break;\n }\n if(c[h-1][i]=='#') break;\n }\n \n for(auto &e:dp){\n if(*max_element(e.first.begin(),e.first.end())!=1) continue;;\n res=max(res,e.second);\n }\n cout<<res<<endl;\n return 0;\n}", "accuracy": 0.6132075471698113, "time_ms": 790, "memory_kb": 4080, "score_of_the_acc": -0.6896, "final_rank": 14 }, { "submission_id": "aoj_2314_2897859", "code_snippet": "#include <cstdio>\n#include <algorithm>\n#include <cstring>\n#include <map>\nusing namespace std;\n\nint H, W;\nchar board[15][15];\nusing Board = map<string, map<int, int> >;\nvoid chmax(int &A, int B) { A = max(A, B); }\n\nstring get_uni(string s, int x, int mode) {\n vector<int> v(10), checked(10); iota(v.begin(), v.end(), 0);\n if(mode == 1) {\n if(x != 0 && isdigit(s[x]) && isdigit(s[x-1])) {\n int a = s[x] - '0', b = s[x-1] - '0';\n v[a] = b;\n }\n else {\n bool exist_num = false;\n if(x != 0 && isdigit(s[x-1])) {\n exist_num = true;\n s[x] = s[x-1];\n }\n if(isdigit(s[x])) {\n exist_num = true;\n }\n\n if(!exist_num) {\n s[x] = '8';\n }\n }\n }\n\n string ret = \"\";\n int id = 0;\n for(int i=0; i<W; i++) {\n if(isdigit(s[i])) {\n int val = v[ s[i] - '0' ];\n if(!checked[val]) checked[val] = ++id;\n ret += (char)('0' + checked[val]);\n }\n else {\n ret += s[i];\n }\n }\n return ret;\n}\n\nbool valid(string s) {\n int pre = -1;\n for(auto c : s) {\n if(isdigit(c)) {\n if(pre == -1) pre = c - '0';\n else if(pre != c - '0') return false;\n }\n }\n return true;\n}\n\nstring to_bin(int N) {\n string ret = \"\";\n for(int i=0; i<W; i++, N>>=1) {\n int c = (N & 1);\n ret += (char)('0' + c);\n }\n return ret;\n}\n\nint main() {\n scanf(\"%d%d\", &H, &W);\n for(int i=0; i<H; i++) {\n for(int j=0; j<W; j++) {\n scanf(\" %c\", &board[i][j]);\n }\n }\n\n string emp_str(W, '.');\n Board cur, nxt;\n cur[emp_str][0] = 0;\n\n for(int pos=0; pos<H*W; pos++) {\n int row = pos / W, col = pos % W;\n char c = board[row][col];\n for(auto x : cur) {\n string uni = x.first;\n for(auto y : cur[uni]) {\n int bit = y.first, pre = y.second;\n int base = bit - ((bit >> col & 1) << col);\n\n // paint (black)\n if(c == '#' || c == '?') {\n if(col == 0 || (uni[col] != uni[col-1] || !isdigit(uni[col]))) {\n string tmp = uni;\n\n tmp = get_uni(tmp, col, 1); \n int nbit = base | (1 << col);\n chmax(nxt[tmp][nbit], pre + 1);\n }\n }\n\n // paint (white)\n if(c == '.' || c == '?') {\n bool ok = true;\n if(row != 0 && !(bit >> col & 1)) ok = false;\n if(col != 0 && !(bit >> (col - 1) & 1)) ok = false;\n\n char target = uni[col];\n int cnt = 0;\n for(int i=0; i<W; i++) cnt += (uni[i] == target);\n if(isdigit(target) && cnt == 1 && W != 1) ok = false;\n\n if(ok) {\n string tmp = uni;\n tmp[col] = '.';\n tmp = get_uni(tmp, col, 0);\n\n int nbit = base;\n chmax(nxt[tmp][nbit], pre);\n }\n }\n }\n }\n cur.clear();\n swap(cur, nxt);\n }\n\n int ans = -1;\n for(auto x : cur) {\n string uni = x.first;\n if(!valid(uni)) continue;\n \n for(auto y : cur[uni]) {\n int val = y.second;\n chmax(ans, val);\n }\n }\n printf(\"%d\\n\", ans);\n return 0;\n}", "accuracy": 1, "time_ms": 250, "memory_kb": 4188, "score_of_the_acc": -0.2085, "final_rank": 2 }, { "submission_id": "aoj_2314_2897858", "code_snippet": "#include <cstdio>\n#include <algorithm>\n#include <cstring>\n#include <map>\nusing namespace std;\n\nint H, W;\nchar board[15][15];\nusing Board = map<string, map<int, int> >;\nvoid chmax(int &A, int B) { A = max(A, B); }\n\nstring get_uni(string s, int x, int mode) {\n vector<int> v(10), checked(10); iota(v.begin(), v.end(), 0);\n if(mode == 1) {\n if(x != 0 && isdigit(s[x]) && isdigit(s[x-1])) {\n int a = s[x] - '0', b = s[x-1] - '0';\n v[a] = b;\n }\n else {\n bool exist_num = false;\n if(x != 0 && isdigit(s[x-1])) {\n exist_num = true;\n s[x] = s[x-1];\n }\n if(isdigit(s[x])) {\n exist_num = true;\n }\n\n if(!exist_num) {\n s[x] = '8';\n }\n }\n }\n\n string ret = \"\";\n int id = 0;\n for(int i=0; i<W; i++) {\n if(isdigit(s[i])) {\n int val = v[ s[i] - '0' ];\n if(!checked[val]) checked[val] = ++id;\n ret += (char)('0' + checked[val]);\n }\n else {\n ret += s[i];\n }\n }\n return ret;\n}\n\nbool valid(string s) {\n int pre = -1;\n for(auto c : s) {\n if(isdigit(c)) {\n if(pre == -1) pre = c - '0';\n else if(pre != c - '0') return false;\n }\n }\n return true;\n}\n\nstring to_bin(int N) {\n string ret = \"\";\n for(int i=0; i<W; i++, N>>=1) {\n int c = (N & 1);\n ret += (char)('0' + c);\n }\n return ret;\n}\n\nint main() {\n scanf(\"%d%d\", &H, &W);\n for(int i=0; i<H; i++) {\n for(int j=0; j<W; j++) {\n scanf(\" %c\", &board[i][j]);\n }\n }\n\n string emp_str(W, '.');\n Board cur, nxt;\n cur[emp_str][0] = 0;\n\n for(int pos=0; pos<H*W; pos++) {\n int row = pos / W, col = pos % W;\n char c = board[row][col];\n for(auto x : cur) {\n string uni = x.first;\n for(auto y : cur[uni]) {\n int bit = y.first, pre = y.second;\n // printf(\"row = %d, col = %d, dp[%s][%s] = %d\\n\", row+1, col+1, uni.c_str(), to_bin(bit).c_str(), pre);\n\n int base = bit - ((bit >> col & 1) << col);\n\n // paint (black)\n if(c == '#' || c == '?') {\n // printf(\"cond 1: row = %d, col = %d\\n\", row+1, col+1);\n if(col == 0 || (uni[col] != uni[col-1] || !isdigit(uni[col]))) {\n string tmp = uni;\n\n // printf(\"tmp = %s, \", tmp.c_str());\n tmp = get_uni(tmp, col, 1);\n // printf(\"nxt = %s\\n\", tmp.c_str());\n \n int nbit = base | (1 << col);\n chmax(nxt[tmp][nbit], pre + 1);\n }\n }\n\n // paint (white)\n if(c == '.' || c == '?') {\n // printf(\"cond 2: row = %d, col = %d\\n\", row+1, col+1);\n\n bool ok = true;\n if(row != 0 && !(bit >> col & 1)) ok = false;\n if(col != 0 && !(bit >> (col - 1) & 1)) ok = false;\n\n char target = uni[col];\n int cnt = 0;\n for(int i=0; i<W; i++) cnt += (uni[i] == target);\n if(isdigit(target) && cnt == 1 && W != 1) ok = false;\n\n if(ok) {\n string tmp = uni;\n tmp[col] = '.';\n tmp = get_uni(tmp, col, 0);\n\n int nbit = base;\n chmax(nxt[tmp][nbit], pre);\n }\n }\n }\n }\n cur.clear();\n swap(cur, nxt);\n }\n\n int ans = -1;\n for(auto x : cur) {\n string uni = x.first;\n if(!valid(uni)) continue;\n \n for(auto y : cur[uni]) {\n int val = y.second;\n chmax(ans, val);\n }\n }\n printf(\"%d\\n\", ans);\n return 0;\n}", "accuracy": 1, "time_ms": 250, "memory_kb": 4200, "score_of_the_acc": -0.2086, "final_rank": 3 }, { "submission_id": "aoj_2314_2897385", "code_snippet": "#include <cstdio>\n#include <algorithm>\n#include <cstring>\nusing namespace std;\n\nint H, W;\nchar board[15][15];\nint dp[2][180000][51];\n\nvoid chmax(int &A, int B) {\n A = max(A, B);\n}\n\nint B;\nint merge(int A, int i) {\n while(A >= B) A -= B;\n \n int b = A % 3;\n A = A - (A % 3);\n if(b == 1 && i != 0) {\n b = i = 2;\n }\n A = (A + b) * 3 + i;\n return A;\n}\nint get_head(int A) {\n return A / B;\n}\nint get_tail(int A) {\n return A % 3;\n}\nvoid thr_eval(int val, int &cnt_black, int &cnt_white, int &cnt_adj_b) {\n if(val == 0) {\n cnt_white++;\n }\n if(val == 1) {\n cnt_black++;\n }\n if(val == 2) {\n cnt_black++;\n cnt_adj_b++;\n }\n}\n\nint main() {\n scanf(\"%d%d\", &H, &W);\n for(int i=0; i<H; i++) {\n for(int j=0; j<W; j++) {\n scanf(\" %c\", &board[i][j]);\n }\n }\n\n int P = H*W, Q = 50;\n B = 1;\n for(int i=0; i<W-1; i++) B *= 3;\n\n memset(dp, -1, sizeof(dp));\n dp[0][0][0] = 0;\n for(int pos=0; pos<P; pos++) {\n int row = pos / W, col = pos % W;\n int cur = pos % 2, nxt = 1 - cur;\n for(int bit=0; bit<(3*B); bit++) {\n for(int diff=0; diff<Q; diff++) {\n if(dp[cur][bit][diff] < 0) continue;\n\n int val = dp[cur][bit][diff];\n\n int cnt_black = 0, cnt_white = 0, cnt_adj_b = 0;\n // 真上\n if(row != 0) {\n int val = get_head(bit);\n thr_eval(val, cnt_black, cnt_white, cnt_adj_b);\n }\n // 左隣\n if(col != 0) {\n int val = get_tail(bit);\n thr_eval(val, cnt_black, cnt_white, cnt_adj_b);\n }\n\n char c = board[row][col];\n if(c == '#' || c == '?') {\n int n_diff = diff + 1 - cnt_black;\n int n_self = (cnt_black ? 2 : 1);\n if(n_diff > 0) {\n int nbit = merge(bit, n_self);\n chmax(dp[nxt][nbit][n_diff], val + 1);\n // printf(\"dp[%d][%d][%d] = %d\\n\", nxt, nbit, n_diff, val + 1);\n }\n }\n if(c == '.' || c == '?') {\n if(row != 0) {\n if(get_head(bit) == 1 && val != 1) continue;\n }\n if(row == H-1 && col != 0) {\n if(get_tail(bit) == 1 && val != 1) continue;\n }\n\n if(cnt_white == 0) {\n int nbit = merge(bit, 0);\n chmax(dp[nxt][nbit][diff], val);\n // printf(\"dp[%d][%d][%d] = %d\\n\", nxt, nbit, diff, val);\n }\n }\n\n dp[cur][bit][diff] = -1;\n }\n }\n }\n\n int ans = -1;\n for(int bit=0; bit<3*B; bit++) {\n if(dp[P%2][bit][1] < 0) continue;\n chmax(ans, dp[P%2][bit][1]);\n }\n printf(\"%d\\n\", ans);\n return 0;\n}", "accuracy": 0.2169811320754717, "time_ms": 680, "memory_kb": 74476, "score_of_the_acc": -1.2208, "final_rank": 17 }, { "submission_id": "aoj_2314_2897381", "code_snippet": "#include <cstdio>\n#include <algorithm>\n#include <cstring>\nusing namespace std;\n\nint H, W;\nchar board[15][15];\nint dp[2][180000][51];\n\nvoid chmax(int &A, int B) {\n A = max(A, B);\n}\n\nint B;\nint merge(int A, int i) {\n while(A >= B) A -= B;\n \n int b = A % 3;\n A = A - (A % 3);\n if(b == 1 && i != 0) {\n b = i = 2;\n }\n A = (A + b) * 3 + i;\n return A;\n}\nint get_head(int A) {\n return A / B;\n}\nint get_tail(int A) {\n return A % 3;\n}\nvoid thr_eval(int val, int &cnt_black, int &cnt_white, int &cnt_adj_b) {\n if(val == 0) {\n cnt_white++;\n }\n if(val == 1) {\n cnt_black++;\n }\n if(val == 2) {\n cnt_black++;\n cnt_adj_b++;\n }\n}\n\nint main() {\n scanf(\"%d%d\", &H, &W);\n for(int i=0; i<H; i++) {\n for(int j=0; j<W; j++) {\n scanf(\" %c\", &board[i][j]);\n }\n }\n\n int P = H*W, Q = 50;\n B = 1;\n for(int i=0; i<W-1; i++) B *= 3;\n\n memset(dp, -1, sizeof(dp));\n dp[0][0][0] = 0;\n for(int pos=0; pos<P; pos++) {\n int row = pos / W, col = pos % W;\n int cur = pos % 2, nxt = 1 - cur;\n for(int bit=0; bit<(3*B); bit++) {\n for(int diff=0; diff<Q; diff++) {\n if(dp[cur][bit][diff] < 0) continue;\n\n int val = dp[cur][bit][diff];\n\n int cnt_black = 0, cnt_white = 0, cnt_adj_b = 0;\n // 真上\n if(row != 0) {\n int val = get_head(bit);\n thr_eval(val, cnt_black, cnt_white, cnt_adj_b);\n }\n // 左隣\n if(col != 0) {\n int val = get_tail(bit);\n thr_eval(val, cnt_black, cnt_white, cnt_adj_b);\n }\n\n char c = board[row][col];\n if(c == '#' || c == '?') {\n int n_diff = diff + 1 - cnt_black;\n int n_self = (cnt_black ? 2 : 1);\n if(n_diff > 0) {\n int nbit = merge(bit, n_self);\n chmax(dp[nxt][nbit][n_diff], val + 1);\n }\n }\n if(c == '.' || c == '?') {\n if(row != 0) {\n if(get_head(bit) == 1) continue;\n }\n if(row == H-1 && col != 0) {\n if(get_tail(bit) == 1) continue;\n }\n\n if(cnt_white == 0) {\n int nbit = merge(bit, 0);\n chmax(dp[nxt][nbit][diff], val);\n }\n }\n\n dp[cur][bit][diff] = -1;\n }\n }\n }\n\n int ans = -1;\n for(int bit=0; bit<3*B; bit++) {\n if(dp[P%2][bit][1] < 0) continue;\n chmax(ans, dp[P%2][bit][1]);\n }\n printf(\"%d\\n\", ans);\n return 0;\n}", "accuracy": 0.05660377358490566, "time_ms": 20, "memory_kb": 74476, "score_of_the_acc": -0.6315, "final_rank": 20 } ]
aoj_2318_cpp
問題文 キュゥべえ はワルプルギスの夜に向けて魔女の合成に明け暮れている.ワルプルギスの夜のために,特別な グリーフシード (魔女の卵) を使って手に入れやすい魔女をうまく合成し目的である舞台装置の魔女を創り上げなければならないからである. キュゥべえ は手に入れやすい種類の魔女の グリーフシード から魔女の素となるものを取り出し,空の特別な グリーフシード に入れることで,その グリーフシード に魔女を宿らせることができる.また,この世界には,ある組み合わせで複数の魔女を合成すると新たな 1 つの魔女を生み出すことができるという合成法則がいくつかある.そこで,いくつかの特別な グリーフシード を使い,それらの中にある魔女の素となるものを合成し, 1 つの特別な グリーフシード に新たな魔女を宿らせることができる.また,合成した結果空となった特別な グリーフシード は再利用することができる.例えば, 3 個の特別な グリーフシード に入った魔女を合成すると,それらの 3 個の グリーフシード は,新たな魔女が宿った 1 個の特別な グリーフシード と 2 個の空の特別な グリーフシード となる.しかし,同じ種類の魔女を同時に複数の グリーフシード に入れることは出来ない. キュゥべえ は魔女の特別な グリーフシード への寄宿と魔女の合成を繰り返すことにより舞台装置の魔女を創りだしたい.舞台装置の魔女を創りだすにはいくつの特別な グリーフシード が必要か求めよ. 入力形式 入力は以下のような形式で与えられる. N\ E\ T\\ W_1\ W_2\ …\ W_N\\ G_1\ C_1\ S_{1,1}\ S_{1,2}\ …\ S_{1,C_1}\\ G_2\ C_2\ S_{2,1}\ S_{2,2}\ …\ S_{2,C_2}\\ …\\ G_E\ C_E\ S_{E,1}\ S_{E,2}\ …\ S_{E,C_E}\\ N は魔女の種類の個数, E は合成法則の個数, T は舞台装置の魔女の番号である. W_i は i 番目の魔女が手に入れやすい魔女であるかどうかを表し, 1 ならば手に入れやすい魔女であり, 0 ならば手に入れやすい魔女ではない. 続く E 行に合成法則が次のように表される:「番号が S_{i,1}\ S_{i,2}\ …\ S_{i,C_i} の魔女を合成することによって G_i の番号の魔女が得られる.」 出力形式 舞台装置の魔女を創りだすのに最低限必要な特別な グリーフシード の個数を出力せよ.創りだすことができない場合は -1 を出力せよ. 制約 1 \lt N \leq 300 0 \leq E \leq 1000 1 \leq T \leq N 0 \leq W_i \leq 1\ (1 \leq i \leq N) 1 \leq G_i \leq N\ (1 \leq i \leq E) 2 \leq C_i \leq 10\ (1 \leq i \leq E) 1 \leq S_{i,j} \leq N\ (1 \leq i \leq E,\ 1 \leq j \leq C_i) \{G_i, S_{i,1}\ S_{i,2}\ …\ S_{i,C_i}\} は全て異なる. 入力中に同一の合成法則が 2 回以上現れることはない. 入出力例 入力例 1 3 1 3 1 1 0 3 2 1 2 出力例1 2 入力例 2 5 0 1 1 1 1 1 1 出力例 2 1 入力例 3 18 5 1 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 4 2 3 4 5 2 2 6 7 3 3 8 9 10 4 4 11 12 13 14 5 4 15 16 17 18 出力例 3 5 入力例 4 6 4 6 1 1 1 0 0 0 4 2 1 2 5 2 2 3 6 2 4 5 6 3 1 2 4 出力例 4 3 入力例 5 13 9 13 1 1 0 1 1 0 1 1 0 1 1 0 0 3 2 1 2 6 2 4 5 9 2 7 8 12 2 10 11 6 2 2 3 9 2 5 6 12 2 8 9 3 2 11 12 13 4 3 6 9 12 出力例 5 5 入力例 6 4 4 4 1 1 0 0 1 2 2 3 2 2 3 1 3 2 1 2 3 3 1 2 4 出力例 6 -1 Problem Setter: Flat35
[ { "submission_id": "aoj_2318_9799752", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n/**\n * @brief template\n */\n\nint main() {\n int N, E, T;\n cin >> N >> E >> T;\n T--;\n vector<int> W(N);\n rep(i,0,N) cin >> W[i];\n vector<int> G(E);\n vector<vector<int>> S(E);\n rep(i,0,E) {\n cin >> G[i];\n G[i]--;\n int C;\n cin >> C;\n S[i].resize(C);\n rep(j,0,C) cin >> S[i][j], S[i][j]--;\n }\n vector<int> DP(N,inf);\n rep(i,0,N) if (W[i] == 1) DP[i] = 1;\n rep(_,0,4000) {\n rep(i,0,E) {\n vector<int> V;\n rep(j,0,S[i].size()) V.push_back(DP[S[i][j]]);\n sort(ALL(V));\n reverse(ALL(V));\n int MAX = -inf;\n rep(j,0,S[i].size()) chmax(MAX, V[j]+j);\n chmin(DP[G[i]],MAX);\n }\n }\n cout << (DP[T] == inf ? -1 : DP[T]) << endl;\n}", "accuracy": 1, "time_ms": 400, "memory_kb": 3492, "score_of_the_acc": -1.1559, "final_rank": 20 }, { "submission_id": "aoj_2318_9579000", "code_snippet": "#include <iostream>\n#include <vector>\n#include <queue>\n#include <cmath>\n#include <algorithm>\n#define INF 2000000000\n\nusing namespace std;\n\nstruct WitchCraft {\n int result_witch;\n int ingredient_count;\n vector<int> ingredients;\n};\n\nint witch_count, rule_count, target_witch;\nint powers[11], bit_population[1024];\nint pending_dependencies[1000], synthesis_matrix[1000][300];\nint min_seeds_needed[300];\nint dp_table[1024];\nvector<int> dependency_graph[1000];\nWitchCraft synthesis_rules[1000];\n\nvoid initialize_powers() {\n for (int i = 0; i < 11; ++i) {\n powers[i] = 1 << i;\n }\n}\n\nvoid precompute_bit_populations() {\n for (int mask = 0; mask < powers[10]; ++mask) {\n int count = 0;\n for (int i = 0; i < 10; ++i) {\n if (mask & (1 << i)) {\n ++count;\n }\n }\n bit_population[mask] = count;\n }\n}\n\nvoid input_data() {\n cin >> witch_count >> rule_count >> target_witch;\n \n for (int i = 0; i < rule_count; ++i) {\n pending_dependencies[i] = 0;\n fill(synthesis_matrix[i], synthesis_matrix[i] + witch_count, 0);\n }\n\n fill(min_seeds_needed, min_seeds_needed + witch_count, INF);\n\n for (int i = 0; i < witch_count; ++i) {\n int availability;\n cin >> availability;\n if (availability == 1) {\n min_seeds_needed[i] = 1;\n }\n }\n\n for (int i = 0; i < rule_count; ++i) {\n cin >> synthesis_rules[i].result_witch >> synthesis_rules[i].ingredient_count;\n --synthesis_rules[i].result_witch;\n for (int j = 0; j < synthesis_rules[i].ingredient_count; ++j) {\n int ingredient;\n cin >> ingredient;\n --ingredient;\n synthesis_rules[i].ingredients.push_back(ingredient);\n if (min_seeds_needed[ingredient] == INF) {\n dependency_graph[ingredient].push_back(i);\n ++pending_dependencies[i];\n synthesis_matrix[i][ingredient] = 1;\n }\n }\n }\n}\n\nvoid process_syntheses() {\n queue<int> ready_synthesis;\n \n for (int i = 0; i < rule_count; ++i) {\n if (pending_dependencies[i] == 0) {\n ready_synthesis.push(i);\n }\n }\n\n while (!ready_synthesis.empty()) {\n int current_rule = ready_synthesis.front();\n ready_synthesis.pop();\n\n int num_ingredients = synthesis_rules[current_rule].ingredient_count;\n fill(dp_table, dp_table + powers[num_ingredients], INF);\n dp_table[0] = 0;\n\n for (int mask = 0; mask < powers[num_ingredients] - 1; ++mask) {\n for (int bit = 0; bit < num_ingredients; ++bit) {\n if (!(mask & (1 << bit))) {\n int next_mask = mask | (1 << bit);\n int reusable_seeds = dp_table[mask] - bit_population[mask];\n dp_table[next_mask] = min(dp_table[next_mask], dp_table[mask] + max(0, min_seeds_needed[synthesis_rules[current_rule].ingredients[bit]] - reusable_seeds));\n }\n }\n }\n\n bool updated = false;\n int &result_witch_seeds = min_seeds_needed[synthesis_rules[current_rule].result_witch];\n if (result_witch_seeds > dp_table[powers[num_ingredients] - 1]) {\n result_witch_seeds = dp_table[powers[num_ingredients] - 1];\n updated = true;\n }\n\n for (int dep : dependency_graph[synthesis_rules[current_rule].result_witch]) {\n if (synthesis_matrix[dep][synthesis_rules[current_rule].result_witch] == 1) {\n synthesis_matrix[dep][synthesis_rules[current_rule].result_witch] = 0;\n if (--pending_dependencies[dep] == 0) {\n ready_synthesis.push(dep);\n }\n } else if (updated && pending_dependencies[dep] == 0) {\n ready_synthesis.push(dep);\n }\n }\n }\n}\n\nvoid output_result() {\n if (min_seeds_needed[target_witch - 1] == INF) {\n cout << \"-1\\n\";\n } else {\n cout << min_seeds_needed[target_witch - 1] << \"\\n\";\n }\n}\n\nint main() {\n initialize_powers();\n precompute_bit_populations();\n input_data();\n process_syntheses();\n output_result();\n\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 4664, "score_of_the_acc": -0.7527, "final_rank": 17 }, { "submission_id": "aoj_2318_9578991", "code_snippet": "#include <bits/stdc++.h>\ntypedef long long ll;\ntypedef unsigned long long ull;\n#define MAX_COST 2000000000\n#define MODULO 1000000007\n#define EPSILON 1e-9\nusing namespace std;\n\nstruct Synthesis {\n int final_witch;\n int num_ingredients;\n vector<int> ingredients;\n};\n\nint num_witches, num_synthesis_rules, target_witch;\nint powers_of_two[11], bit_count[1024];\nint dependency_count[1000], synthesis_dependency[1000][300];\nint min_grief_seeds[300];\nint dp[1024];\nvector<int> dependency_graph[1000];\nSynthesis syntheses[1000];\n\nint main() {\n for (int i = 0; i < 11; i++) {\n powers_of_two[i] = pow(2, i);\n }\n\n // Precompute the number of bits set for each possible state\n for (int state = 0; state < powers_of_two[10]; state++) {\n int bit_sum = 0;\n for (int bit = 0; bit < 10; bit++) {\n if (state & (1 << bit)) {\n bit_sum++;\n }\n }\n bit_count[state] = bit_sum;\n }\n\n // Input number of witches, synthesis rules, and the target witch\n scanf(\"%d %d %d\", &num_witches, &num_synthesis_rules, &target_witch);\n\n // Initialize dependency arrays\n for (int i = 0; i < num_synthesis_rules; i++) {\n dependency_count[i] = 0;\n for (int j = 0; j < num_witches; j++) {\n synthesis_dependency[i][j] = 0;\n }\n }\n\n // Set the initial minimum grief seeds required for each witch to a large number\n for (int i = 0; i < num_witches; i++) {\n min_grief_seeds[i] = MAX_COST;\n }\n\n // Input initial availability of witches\n int availability;\n for (int i = 0; i < num_witches; i++) {\n scanf(\"%d\", &availability);\n if (availability == 1) {\n min_grief_seeds[i] = 1;\n }\n }\n\n // Input synthesis rules\n for (int i = 0; i < num_synthesis_rules; i++) {\n scanf(\"%d %d\", &syntheses[i].final_witch, &syntheses[i].num_ingredients);\n syntheses[i].final_witch--;\n\n for (int j = 0; j < syntheses[i].num_ingredients; j++) {\n scanf(\"%d\", &availability);\n availability--;\n syntheses[i].ingredients.push_back(availability);\n if (min_grief_seeds[availability] == MAX_COST) {\n dependency_graph[availability].push_back(i);\n dependency_count[i]++;\n synthesis_dependency[i][availability] = 1;\n }\n }\n }\n\n // Start with synthesis rules that have no dependencies\n queue<int> ready_to_synthesize;\n for (int i = 0; i < num_synthesis_rules; i++) {\n if (dependency_count[i] == 0) {\n ready_to_synthesize.push(i);\n }\n }\n\n while (!ready_to_synthesize.empty()) {\n int current_rule = ready_to_synthesize.front();\n ready_to_synthesize.pop();\n\n int num_ingredients = syntheses[current_rule].num_ingredients;\n for (int state = 0; state < powers_of_two[num_ingredients]; state++) {\n dp[state] = MAX_COST;\n }\n dp[0] = 0;\n\n for (int state = 0; state < powers_of_two[num_ingredients] - 1; state++) {\n for (int ingredient = 0; ingredient < num_ingredients; ingredient++) {\n if (state & (1 << ingredient)) {\n continue;\n }\n int next_state = state + powers_of_two[ingredient];\n int available_grief_seeds = dp[state] - bit_count[state];\n dp[next_state] = min(dp[next_state], dp[state] + max(0, min_grief_seeds[syntheses[current_rule].ingredients[ingredient]] - available_grief_seeds));\n }\n }\n\n bool updated = false;\n if (min_grief_seeds[syntheses[current_rule].final_witch] > dp[powers_of_two[num_ingredients] - 1]) {\n min_grief_seeds[syntheses[current_rule].final_witch] = dp[powers_of_two[num_ingredients] - 1];\n updated = true;\n }\n\n for (int i = 0; i < dependency_graph[syntheses[current_rule].final_witch].size(); i++) {\n int next_rule = dependency_graph[syntheses[current_rule].final_witch][i];\n if (synthesis_dependency[next_rule][syntheses[current_rule].final_witch] == 1) {\n synthesis_dependency[next_rule][syntheses[current_rule].final_witch] = 0;\n dependency_count[next_rule]--;\n if (dependency_count[next_rule] == 0) {\n ready_to_synthesize.push(next_rule);\n }\n } else {\n if (!updated) continue;\n if (dependency_count[next_rule] == 0) {\n ready_to_synthesize.push(next_rule);\n }\n }\n }\n }\n\n target_witch--;\n if (min_grief_seeds[target_witch] == MAX_COST) {\n printf(\"-1\\n\");\n } else {\n printf(\"%d\\n\", min_grief_seeds[target_witch]);\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 5224, "score_of_the_acc": -1.0256, "final_rank": 19 }, { "submission_id": "aoj_2318_7110954", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(0);\n int n, e, t;\n cin >> n >> e >> t;\n vector<int> w(n);\n for (int i = 0; i < n; i++) {\n cin >> w[i];\n }\n const int inf = 1e9;\n vector<int> ans(n, inf);\n for (int i = 0; i < n; i++) {\n if (w[i]) {\n ans[i] = 1;\n }\n }\n vector<int> g(e);\n vector<vector<int>> s(e);\n for (int i = 0; i < e; i++) {\n cin >> g[i];\n g[i]--;\n int c;\n cin >> c;\n for (int j = 0; j < c; j++) {\n int x;\n cin >> x;\n x--;\n s[i].emplace_back(x);\n }\n }\n for (int z = 0; z < e; z++) {\n for (int i = 0; i < e; i++) {\n vector<int> a;\n for (int x: s[i]) {\n a.emplace_back(ans[x]);\n }\n sort(a.rbegin(), a.rend());\n int b = 1;\n for (int j = 0; j < (int) a.size(); j++) {\n b = max(b, a[j] + j);\n }\n ans[g[i]] = min(ans[g[i]], b);\n }\n }\n t--;\n cout << (ans[t] == inf ? -1 : ans[t]) << '\\n';\n}", "accuracy": 1, "time_ms": 110, "memory_kb": 3488, "score_of_the_acc": -0.4104, "final_rank": 16 }, { "submission_id": "aoj_2318_6706476", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)\n#define revrep(i, t, s) for (int i = (int)(t)-1; i >= (int)(s); --i)\n#define all(x) begin(x), end(x)\ntemplate <typename T> bool chmax(T& a, const T& b) { return a < b ? (a = b, 1) : 0; }\ntemplate <typename T> bool chmin(T& a, const T& b) { return a > b ? (a = b, 1) : 0; }\n\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << fixed << setprecision(15);\n\n int N, E, T;\n cin >> N >> E >> T;\n --T;\n vector<int> W(N);\n for (auto& x : W) cin >> x;\n vector<vector<int>> S(E);\n vector<int> G(E);\n rep(i,0,E) {\n int C;\n cin >> G[i] >> C;\n --G[i];\n S[i].resize(C);\n for (auto& x : S[i]) cin >> x, --x;\n }\n vector<int> dist(N, 1e9);\n using P = pair<int, int>;\n priority_queue<P, vector<P>, greater<>> pq;\n rep(i,0,N) if (W[i]) {\n dist[i] = 1;\n pq.push({i, 1});\n }\n vector<bool> visited(N);\n while (!pq.empty()) {\n int d, i;\n tie(d, i) = pq.top();\n pq.pop();\n if (d > dist[i]) continue;\n rep(j,0,E) {\n if (visited[G[j]]) continue;\n vector<int> cost;\n for (int k : S[j]) cost.push_back(dist[k]);\n sort(all(cost));\n reverse(all(cost));\n int m = 0;\n rep(k,0,cost.size()) chmax(m, k+cost[k]);\n if (m < dist[G[j]]) {\n dist[G[j]] = m;\n pq.push({m, G[j]});\n }\n }\n }\n cout << (dist[T] < 1e9 ? dist[T] : -1) << endl;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3452, "score_of_the_acc": -0.1621, "final_rank": 8 }, { "submission_id": "aoj_2318_6333207", "code_snippet": "#include \"bits/stdc++.h\"\nusing namespace std;\n\n#define REP(a, b) for (long long a = 0; a < b; ++a)\n#define ll long long\n#define ld long double\n#define ALL(x) begin(x), end(x)\n\n#define int long long\n\nvoid solve()\n{\n int n, e, t;\n cin >> n >> e >> t;\n t--;\n vector<int> dp(n, 1e18);\n vector<vector<int>> vertexs[400];\n REP(i, n)\n {\n int a;\n cin >> a;\n if (a == 1)\n dp[i] = 1;\n }\n REP(i, e)\n {\n int target;\n int a;\n cin >> target;\n cin >> a;\n target--;\n vector<int> gogo;\n REP(q, a)\n {\n int b;\n cin >> b;\n b--;\n gogo.push_back(b);\n }\n vertexs[target].push_back(gogo);\n }\n\n REP(i, n)\n {\n REP(q, n)\n {\n for (auto x : vertexs[q])\n {\n vector<int> tmps;\n REP(q, x.size())\n {\n tmps.push_back(dp[x[q]]);\n }\n sort(ALL(tmps));\n reverse(ALL(tmps));\n int target = 0;\n REP(q, tmps.size())\n {\n target = max(target, tmps[q] + q);\n }\n dp[q] = min(dp[q], target);\n }\n }\n }\n if (dp[t] == 1e18)\n dp[t] = -1;\n cout << dp[t] << endl;\n}\n\n#undef int\nint main()\n{\n iostream::sync_with_stdio(false);\n cout << fixed << setprecision(100);\n solve();\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3504, "score_of_the_acc": -0.2387, "final_rank": 14 }, { "submission_id": "aoj_2318_6044117", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define eps 1e-9\n#define INF 2000000000 // 2e9\n#define LLINF 2000000000000000000ll // 2e18 (llmax:9e18)\n#define all(x) (x).begin(), (x).end()\n#define sq(x) ((x) * (x))\n\n#define rep(i, x) for (int i = 0; i < (int)(x); i++)\n#define drep(i, x) for (int i = ((int)(x)-1); i >= 0; i--)\n\n#define popcount __builtin_popcount\n#define next __next\n#define prev __prev\n\n#ifndef LOCAL\n#define dmp(...) ;\n#else\n#define dmp(...) \\\n cerr << \"[ \" << #__VA_ARGS__ << \" ] : \" << dump_str(__VA_ARGS__) << endl\n#endif\n\n// ---------------- Utility ------------------\n\ntemplate <class T>\nbool chmin(T &a, const T &b) {\n if (a <= b) return false;\n a = b;\n return true;\n}\n\ntemplate <class T>\nbool chmax(T &a, const T &b) {\n if (a >= b) return false;\n a = b;\n return true;\n}\n\ntemplate <class T>\nusing MaxHeap = priority_queue<T>;\n\ntemplate <class T>\nusing MinHeap = priority_queue<T, vector<T>, greater<T>>;\n\ntemplate <class T>\nvector<T> vect(int len, T elem) {\n return vector<T>(len, elem);\n}\n\n// ----------------- Input -------------------\n\ntemplate <class T, class U>\nistream &operator>>(istream &is, pair<T, U> &p) {\n return is >> p.first >> p.second;\n}\n\ntemplate <class T>\nistream &operator>>(istream &is, vector<T> &vec) {\n for (int i = 0; i < vec.size(); i++) is >> vec[i];\n return is;\n}\n\n// ----------------- Output ------------------\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n return os << p.first << ',' << p.second;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const vector<T> &v) {\n for (const T &e : v) os << e << \" \";\n return os;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const deque<T> &d) {\n for (const T &e : d) os << e << \" \";\n return os;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const set<T> &s) {\n os << \"{ \";\n for (const T &e : s) os << e << \" \";\n return os << \"}\";\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const map<T, U> &m) {\n os << \"{ \";\n for (const auto &[key, val] : m) os << \"( \" << key << \" -> \" << val << \" ) \";\n return os << \"}\";\n}\n\ntemplate <class TupleTy, size_t... I>\nvoid dump_tuple(ostream &os, const TupleTy t, std::index_sequence<I...>) {\n (..., (os << (I == 0 ? \"\" : \",\") << std::get<I>(t)));\n}\n\ntemplate <class... Args>\nostream &operator<<(ostream &os, const tuple<Args...> &t) {\n os << \"(\";\n dump_tuple(os, t, std::make_index_sequence<sizeof...(Args)>());\n return os << \")\";\n}\n\nvoid dump_str_rec(ostringstream &) {}\n\ntemplate <class Head, class... Tail>\nvoid dump_str_rec(ostringstream &oss, Head &&head, Tail &&... tail) {\n oss << \", \" << head;\n dump_str_rec(oss, forward<Tail>(tail)...);\n}\n\ntemplate <class T, class... U>\nstring dump_str(T &&arg, U &&... args) {\n ostringstream oss;\n oss << arg;\n dump_str_rec(oss, forward<U>(args)...);\n return oss.str();\n}\n\n// --------------- Fast I/O ------------------\n\nvoid fastio() {\n cin.tie(0);\n ios::sync_with_stdio(0);\n cout << fixed << setprecision(20);\n}\n\n// ------------------ ACL --------------------\n\n// #include <atcoder/modint>\n// constexpr int MOD = 998244353;\n// constexpr int MOD = 1000000007;\n// using mint = atcoder::static_modint<MOD>;\n// ostream &operator<<(ostream &os, const mint &v) {\n// return os << v.val();\n// }\n\n// ------------ End of template --------------\n\n#define endl \"\\n\"\nusing ll = long long;\nusing pii = pair<int, int>;\n\nconst bool multitest = false;\n\nvoid solve() {\n int N, E, T;\n cin >> N >> E >> T;\n T--;\n vector<int> dp(N, INF);\n for (int i = 0; i < N; i++) {\n int w;\n cin >> w;\n if (w == 1) dp[i] = 1;\n }\n\n vector<int> g(E);\n vector<vector<int>> S(E);\n for (int i = 0; i < E; i++) {\n cin >> g[i];\n g[i]--;\n int C;\n cin >> C;\n S[i].resize(C);\n for (int j = 0; j < C; j++) {\n cin >> S[i][j];\n S[i][j]--;\n }\n }\n\n for (int _ = 0; _ < N; _++) {\n for (int i = 0; i < E; i++) {\n vector<int> cs;\n bool ready = true;\n for (int j : S[i]) {\n if (dp[j] == INF) {\n ready = false;\n break;\n }\n cs.push_back(dp[j]);\n }\n if (!ready) continue;\n sort(all(cs), greater<int>());\n dmp(cs);\n int need = 0;\n for (int j = 0; j < cs.size(); j++) chmax(need, j + cs[j]);\n chmin(dp[g[i]], need);\n }\n }\n\n if (dp[T] == INF)\n cout << -1 << endl;\n else\n cout << dp[T] << endl;\n return;\n}\n\nint main() {\n fastio();\n if (!multitest) {\n solve();\n } else {\n cerr << \"[Warning] Multi testcase mode on\" << endl;\n int t;\n cin >> t;\n while (t--) solve();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3484, "score_of_the_acc": -0.1777, "final_rank": 11 }, { "submission_id": "aoj_2318_6022950", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\nusing PII = pair<ll, ll>;\n\n#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)\n#define REP(i, n) FOR(i, 0, n)\n#define ALL(x) x.begin(), x.end()\n#define POPCOUNT(x) __builtin_popcount(x)\ntemplate <typename T> void chmin(T &a, const T &b) { a = min(a, b); }\ntemplate <typename T> void chmax(T &a, const T &b) { a = max(a, b); }\n\nconst ll INF = 1LL << 60;\n\nstruct FastIO {\n FastIO() {\n cin.tie(0);\n ios::sync_with_stdio(0);\n }\n} fastiofastio;\n\nconst ll MOD = 1e9 + 7;\n\n// BEGIN CUT\nll modpow(ll x, ll y, ll m) {\n ll a = 1, p = x;\n while (y > 0) {\n if (y % 2 == 0) {\n p = (p * p) % m;\n y /= 2;\n } else {\n a = (a * p) % m;\n y--;\n }\n }\n return a;\n}\n// END CUT\n\nll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }\nll lcm(ll a, ll b) { return a / gcd(a, b) * b; }\n\nint main() {\n int N, E, T;\n cin >> N >> E >> T;\n T--;\n vector<int> W(N);\n for (int i = 0; i < N; i++)\n cin >> W[i];\n vector<vector<int>> S(E);\n vector<int> G(E), C(E);\n for (int i = 0; i < E; i++) {\n cin >> G[i] >> C[i];\n G[i]--;\n S[i].resize(C[i]);\n for (int j = 0; j < C[i]; j++) {\n cin >> S[i][j];\n S[i][j]--;\n }\n }\n vector<int> need(N, 1 << 25);\n for (int i = 0; i < N; i++) {\n if (W[i])\n need[i] = 1;\n }\n while (1) {\n bool update = 0;\n for (int i = 0; i < E; i++) {\n vector<int> vec;\n for (int j = 0; j < C[i]; j++) {\n vec.push_back(need[S[i][j]]);\n }\n sort(ALL(vec), greater<int>());\n int ma = 0;\n for (int j = 0; j < C[i]; j++)\n ma = max(ma, j + vec[j]);\n if (need[G[i]] > ma) {\n need[G[i]] = ma;\n update = 1;\n break;\n }\n }\n if (!update)\n break;\n }\n if (need[T] != 1 << 25)\n cout << need[T] << endl;\n else\n cout << -1 << endl;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3460, "score_of_the_acc": -0.166, "final_rank": 9 }, { "submission_id": "aoj_2318_5882811", "code_snippet": "#pragma GCC target(\"avx2\")\n#pragma GCC optimize(\"Ofast\")\n#pragma GCC optimize(\"unroll-loops\")\n#include <bits/stdc++.h>\nusing namespace std;\n#define DEBUG\n#ifdef DEBUG\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << '(' << p.first << ',' << p.second << ')';\n return os;\n}\ntemplate <class T> ostream &operator<<(ostream &os, const vector<T> &v) {\n os << '{';\n for(int i = 0; i < (int)v.size(); i++) {\n if(i) { os << ','; }\n os << v[i];\n }\n os << '}';\n return os;\n}\nvoid debugg() { cerr << endl; }\ntemplate <class T, class... Args>\nvoid debugg(const T &x, const Args &... args) {\n cerr << \" \" << x;\n debugg(args...);\n}\n#define debug(...) \\\n cerr << __LINE__ << \" [\" << #__VA_ARGS__ << \"]: \", debugg(__VA_ARGS__)\n#define dump(x) cerr << __LINE__ << \" \" << #x << \" = \" << (x) << endl\n#else\n#define debug(...) (void(0))\n#define dump(x) (void(0))\n#endif\nusing namespace std;\ntypedef long long ll;\ntypedef vector<ll> vl;\ntypedef vector<vl> vvl;\ntypedef vector<char> vc;\ntypedef vector<string> vs;\ntypedef vector<bool> vb;\ntypedef vector<double> vd;\ntypedef pair<ll,ll> P;\ntypedef pair<int,int> pii;\ntypedef vector<P> vpl;\ntypedef tuple<ll,ll,ll> tapu;\n#define rep(i,n) for(int i=0; i<(n); i++)\n#define REP(i,a,b) for(int i=(a); i<(b); i++)\n#define all(x) (x).begin(), (x).end()\n#define rall(x) (x).rbegin(), (x).rend()\nconst int inf = (1<<30)-1;\nconst ll linf = 1LL<<61;\nconst int MAX = 510000;\nint dy[8] = {0,1,0,-1,1,-1,-1,1};\nint dx[8] = {-1,0,1,0,1,-1,1,-1};\nconst double pi = acos(-1);\nconst double eps = 1e-7;\ntemplate<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){\n\tif(a>b){\n\t\ta = b; return true;\n\t}\n\telse return false;\n}\ntemplate<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){\n\tif(a<b){\n\t\ta = b; return true;\n\t}\n\telse return false;\n}\ntemplate<typename T> inline void print(T &a){\n int sz = a.size();\n for(auto itr = a.begin(); itr != a.end(); itr++){\n\t\tcout << *itr;\n sz--;\n if(sz) cout << \" \";\n\t}\n cout << \"\\n\";\n}\ntemplate<typename T1,typename T2> inline void print2(T1 a, T2 b){\n\tcout << a << \" \" << b << \"\\n\";\n}\ntemplate<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){\n\tcout << a << \" \" << b << \" \" << c << \"\\n\";\n}\nvoid mark() {cout << \"#\" << \"\\n\";}\nll pcount(ll x) {return __builtin_popcountll(x);}\n//#include <atcoder/all>\n//using namespace atcoder;\n//const int mod = 1e9 + 7;\nconst int mod = 998244353;\n\nint main(){\n int n,e,t; cin >> n >> e >> t; t--;\n vl dp(n,inf);\n vb vi(n,false);\n rep(i,n){\n int w; cin >> w;\n if(w == 1) dp[i] = 1, vi[i] = true;\n }\n vector<vvl> G(n);\n rep(i,e){\n int g,c; cin >> g >> c; g--;\n vl v(c);\n rep(j,c) cin >> v[j], v[j]--;\n G[g].push_back(v);\n }\n rep(z,2020){\n rep(u,n){\n for(auto v : G[u]){\n vl costs;\n for(auto x : v){\n costs.push_back(dp[x]);\n }\n sort(rall(costs));\n ll s = 0;\n rep(i,costs.size()) chmax(s,costs[i]+i);\n chmin(dp[u],s);\n }\n }\n }\n ll ans = dp[t];\n if(ans == inf) ans = -1;\n cout << ans << endl;\n}", "accuracy": 1, "time_ms": 250, "memory_kb": 3456, "score_of_the_acc": -0.7538, "final_rank": 18 }, { "submission_id": "aoj_2318_5676171", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n int N, E, T;\n cin >> N >> E >> T;\n T--;\n vector<int> W(N);\n for (auto&& w : W) {\n cin >> w;\n }\n vector<int> G(E), C(E);\n vector<vector<int>> S(E);\n for (int i = 0; i < E; i++) {\n cin >> G[i] >> C[i];\n G[i]--;\n S[i].resize(C[i]);\n for (auto&& s : S[i]) {\n cin >> s;\n s--;\n }\n }\n\n const int INF = 1e7;\n vector<int> cost(N, INF);\n vector<bool> seen(N, false);\n priority_queue<pair<int, int>, vector<pair<int, int>>, greater<>> que;\n for (int i = 0; i < N; i++) {\n if (W[i]) {\n que.push({0, i});\n cost[i] = 1;\n }\n }\n while (!que.empty()) {\n auto [c, now] = que.top();\n que.pop();\n if (seen[now])\n continue;\n seen[now] = true;\n for (int i = 0; i < E; i++) {\n int g = G[i], c = C[i];\n auto& s = S[i];\n if (seen[g])\n continue;\n vector<int> V(c);\n for (int j = 0; j < c; j++) {\n V[j] = cost[s[j]];\n }\n sort(begin(V), end(V), greater{});\n int c_nxt = 0;\n for (int j = 0; j < c; j++) {\n c_nxt = max(c_nxt, V[j] + j);\n }\n if (cost[g] > c_nxt) {\n cost[g] = c_nxt;\n que.push({c_nxt, g});\n }\n }\n }\n cout << (cost[T] >= INF ? -1 : cost[T]) << endl;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3504, "score_of_the_acc": -0.1618, "final_rank": 7 }, { "submission_id": "aoj_2318_5380837", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nconst int INF = 10000000;\nint main(){\n int N, E, T;\n cin >> N >> E >> T;\n T--;\n vector<int> W(N);\n for (int i = 0; i < N; i++){\n cin >> W[i];\n }\n vector<int> G(E), C(E);\n vector<vector<int>> S(E);\n for (int i = 0; i < E; i++){\n cin >> G[i] >> C[i];\n G[i]--;\n S[i] = vector<int>(C[i]);\n for (int j = 0; j < C[i]; j++){\n cin >> S[i][j];\n S[i][j]--;\n }\n }\n vector<int> d(N, INF);\n for (int i = 0; i < N; i++){\n if (W[i] == 1){\n d[i] = 1;\n }\n }\n for (int i = 0; i < N; i++){\n for (int j = 0; j < E; j++){\n vector<int> cnt(C[j]);\n for (int k = 0; k < C[j]; k++){\n cnt[k] = d[S[j][k]];\n }\n sort(cnt.begin(), cnt.end(), greater<int>());\n int mx = 0;\n for (int k = 0; k < C[j]; k++){\n mx = max(mx, cnt[k] + k);\n }\n d[G[j]] = min(d[G[j]], mx);\n }\n }\n if (d[T] == INF){\n cout << -1 << endl;\n } else {\n cout << d[T] << endl;\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3172, "score_of_the_acc": -0.0256, "final_rank": 1 }, { "submission_id": "aoj_2318_5304541", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntemplate<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }\ntemplate<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }\n#define all(x) (x).begin(),(x).end()\n#define fi first\n#define se second\n#define mp make_pair\n#define si(x) int(x.size())\nconst int mod=1000000007,MAX=305,INF=1<<30;\n\nint dp[MAX];\n\nint main(){\n \n std::ifstream in(\"text.txt\");\n std::cin.rdbuf(in.rdbuf());\n cin.tie(0);\n ios::sync_with_stdio(false);\n \n int N,M,X;cin>>N>>M>>X;\n X--;\n for(int i=0;i<N;i++){\n int x;cin>>x;\n dp[i]=INF;\n if(x==1) dp[i]=1;\n }\n \n vector<int> target(M);\n vector<vector<int>> need(M);\n \n for(int i=0;i<M;i++){\n cin>>target[i];\n target[i]--;\n \n int k;cin>>k;\n need[i].resize(k);\n for(int j=0;j<k;j++){\n cin>>need[i][j];\n need[i][j]--;\n }\n }\n \n for(int q=0;q<N;q++){\n for(int i=0;i<M;i++){\n vector<int> A;\n for(int a:need[i]) A.push_back(dp[a]);\n sort(all(A));\n reverse(all(A));\n if(A.front()==INF) continue;\n \n int ma=0;\n for(int j=0;j<si(A);j++){\n chmax(ma,A[j]+j);\n }\n chmin(dp[target[i]],ma);\n }\n }\n \n if(dp[X]==INF) dp[X]=-1;\n \n cout<<dp[X]<<endl;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3532, "score_of_the_acc": -0.2267, "final_rank": 13 }, { "submission_id": "aoj_2318_5269555", "code_snippet": "//#define _GLIBCXX_DEBUG\n#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i, n) for(int i=0; i<n; ++i)\n#define all(v) v.begin(), v.end()\n#define rall(v) v.rbegin(), v.rend()\nusing ll = int64_t;\nusing ull = uint64_t;\nusing ld = long double;\nusing P = pair<int, int>;\nusing vs = vector<string>;\nusing vi = vector<int>;\nusing vvi = vector<vi>;\ntemplate<class T> using PQ = priority_queue<T>;\ntemplate<class T> using PQG = priority_queue<T, vector<T>, greater<T>>;\nconst int INF = 0xccccccc;\nconst ll LINF = 0xcccccccccccccccLL;\ntemplate<typename T1, typename T2>\ninline bool chmax(T1 &a, T2 b) {return a < b && (a = b, true);}\ntemplate<typename T1, typename T2>\ninline bool chmin(T1 &a, T2 b) {return a > b && (a = b, true);}\ntemplate<typename T1, typename T2>\nistream &operator>>(istream &is, pair<T1, T2> &p) { return is >> p.first >> p.second;}\ntemplate<typename T1, typename T2>\nostream &operator<<(ostream &os, const pair<T1, T2> &p) { return os << p.first << ' ' << p.second;}\n\n\n\n//head\n\n\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(0);\n int n, e, t;\n cin >> n >> e >> t;\n vi dp(n+1);\n rep(i, n) {\n int w;\n cin >> w;\n if(w) dp[i+1] = 1;\n else dp[i+1] = INF;\n }\n vi g(e), c(e);\n vvi s(e);\n rep(i, e) {\n cin >> g[i] >> c[i];\n s[i].resize(c[i]);\n rep(j, c[i]) cin >> s[i][j];\n }\n PQ<int> q;\n rep(i, n-1) rep(j, e) {\n rep(k, c[j]) q.emplace(dp[s[j][k]]);\n int u = 0;\n int now = c[j];\n while(!q.empty()) {\n chmax(now, q.top()+(u++));\n q.pop();\n }\n chmin(dp[g[j]], now);\n }\n if(dp[t] == INF) puts(\"-1\");\n else cout << dp[t] << endl;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3492, "score_of_the_acc": -0.1816, "final_rank": 12 }, { "submission_id": "aoj_2318_4956557", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\nint n,m,r,a,b,d[350],e[1000],inf=1e8,cnt;\nvector<int> v[1000],u;\nint main(void){\n cin>>n>>m>>r;\n for(int i=0;i<n;i++){\n cin>>d[i];\n if(d[i]==0)d[i]=inf;\n }\n for(int i=0;i<m;i++){\n cin>>e[i]>>a;\n e[i]--;\n for(int j=0;j<a;j++){\n cin>>b;\n b--;\n v[i].push_back(b);\n }\n }\n for(int i=0;i<m;i++){\n for(int j=0;j<m;j++){\n u={};\n for(int x:v[j]){\n u.push_back(d[x]);\n\n }\n sort(u.begin(),u.end());\n reverse(u.begin(),u.end());\n cnt=0;\n for(int k=0;k<u.size();k++){\n //cout<<u[k]<<endl;\n cnt=max(cnt,u[k]+k);\n }\n d[e[j]]=min(d[e[j]],cnt);\n }\n }\n r--;\n if(d[r]==inf)cout<<-1<<endl;\n else cout<<d[r]<<endl;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 3488, "score_of_the_acc": -0.2566, "final_rank": 15 }, { "submission_id": "aoj_2318_4925833", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define REP(i, n) for (int i=0; i<(n); ++i)\n#define RREP(i, n) for (int i=(int)(n)-1; i>=0; --i)\n#define FOR(i, a, n) for (int i=(a); i<(n); ++i)\n#define RFOR(i, a, n) for (int i=(int)(n)-1; i>=(a); --i)\n\n#define SZ(x) ((int)(x).size())\n#define ALL(x) (x).begin(),(x).end()\n\n#define DUMP(x) cerr<<#x<<\" = \"<<(x)<<endl\n#define DEBUG(x) cerr<<#x<<\" = \"<<(x)<<\" (L\"<<__LINE__<<\")\"<<endl;\n\ntemplate<class T>\nostream &operator<<(ostream &os, const vector<T> &v) {\n os << \"[\";\n REP(i, SZ(v)) {\n if (i) os << \", \";\n os << v[i];\n }\n return os << \"]\";\n}\n\ntemplate<class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n return os << \"(\" << p.first << \" \" << p.second << \")\";\n}\n\ntemplate<class T>\nbool chmax(T &a, const T &b) {\n if (a < b) {\n a = b;\n return true;\n }\n return false;\n}\n\ntemplate<class T>\nbool chmin(T &a, const T &b) {\n if (b < a) {\n a = b;\n return true;\n }\n return false;\n}\n\nusing ll = long long;\nusing ull = unsigned long long;\nusing ld = long double;\nusing P = pair<int, int>;\nusing vi = vector<int>;\nusing vll = vector<ll>;\nusing vvi = vector<vi>;\nusing vvll = vector<vll>;\n\nconst ll MOD = 1e9 + 7;\nconst int INF = INT_MAX / 2;\nconst ll LINF = LLONG_MAX / 2;\nconst ld eps = 1e-9;\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(10);\n\n int n, e, t; cin >> n >> e >> t;\n t--;\n vi w(n, INF);\n REP(i, n) {\n int now; cin >> now;\n if(now) w[i] = 1;\n }\n\n vector<vector<vector<int>>> g(n);\n\n REP(i, e) {\n int a; cin >> a;\n a--;\n int c; cin >> c;\n vi v;\n REP(j, c) {\n int s; cin >> s;\n s--;\n v.push_back(s);\n }\n g[a].emplace_back(v);\n }\n\n REP(i, n-1) {\n REP(j, n) {\n for(auto &v: g[j]) {\n int sz = SZ(v);\n vi v2(sz);\n bool ok = true;\n REP(k, sz) {\n if(v[k] == INF) {\n ok = false;\n break;\n }\n v2[k] = w[v[k]];\n }\n if(!ok) continue;\n sort(v2.rbegin(), v2.rend());\n int cost = 0;\n REP(k, sz) {\n chmax(cost, v2[k] + k);\n }\n chmin(w[j], cost);\n }\n }\n }\n\n cout << (w[t] == INF ? -1 : w[t]) << endl;\n\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3520, "score_of_the_acc": -0.1696, "final_rank": 10 }, { "submission_id": "aoj_2318_4818076", "code_snippet": "#include<iostream>\n#include<cstdio>\n#include<cstring>\n#include<string>\n#include<vector>\n#include<cmath>\n#include<algorithm>\n#include<map>\n#include<queue>\n#include<deque>\n#include<iomanip>\n#include<tuple>\n#include<cassert>\n#include<set>\nusing namespace std;\ntypedef long long int LL;\ntypedef pair<int,int> P;\ntypedef pair<LL,int> LP;\nconst int INF=1<<30;\nconst LL MAX=1e9+7;\n\nvoid array_show(int *array,int array_n,char middle=' '){\n for(int i=0;i<array_n;i++)printf(\"%d%c\",array[i],(i!=array_n-1?middle:'\\n'));\n}\nvoid array_show(LL *array,int array_n,char middle=' '){\n for(int i=0;i<array_n;i++)printf(\"%lld%c\",array[i],(i!=array_n-1?middle:'\\n'));\n}\nvoid array_show(vector<int> &vec_s,int vec_n=-1,char middle=' '){\n if(vec_n==-1)vec_n=vec_s.size();\n for(int i=0;i<vec_n;i++)printf(\"%d%c\",vec_s[i],(i!=vec_n-1?middle:'\\n'));\n}\nvoid array_show(vector<LL> &vec_s,int vec_n=-1,char middle=' '){\n if(vec_n==-1)vec_n=vec_s.size();\n for(int i=0;i<vec_n;i++)printf(\"%lld%c\",vec_s[i],(i!=vec_n-1?middle:'\\n'));\n}\n\n\n\nint main(){\n int n,m,p;\n int i,j,k;\n int a,b,c;\n cin>>n>>m>>p;\n p--;\n vector<int> vs(n,INF),mak(m);\n vector<vector<int>> comp(m);\n for(i=0;i<n;i++){\n cin>>a;\n if(a)vs[i]=1;\n }\n for(i=0;i<m;i++){\n cin>>a>>j;\n mak[i]=a-1;\n while(j--){\n cin>>b;\n comp[i].push_back(b-1);\n }\n }\n vector<int> v1;\n for(i=0;i<n;i++){\n for(j=0;j<m;j++){\n v1.clear();\n for(auto num:comp[j])v1.push_back(vs[num]);\n sort(v1.rbegin(),v1.rend());\n a=vs[mak[j]],b=0;\n for(k=0;k<v1.size();k++){\n b=max(b,v1[k]+k);\n }\n vs[mak[j]]=min(a,b);\n }\n }\n a=vs[p];\n if(a>=INF)a=-1;\n cout<<a<<endl;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3248, "score_of_the_acc": -0.037, "final_rank": 2 }, { "submission_id": "aoj_2318_4274349", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nconst int INF = 1e+8;\n\nsigned main(){\n\tint n,e,t,w[310],g[1010],c[1010],s[1010][12];\n\tcin >> n >> e >> t; t--;\n\tint d[310];\n\tfor(int i = 0;i < n;i++){\n\t\tcin >> w[i];\n\t\td[i] = (w[i] ? 1 : INF);\n\t}\n\tfor(int i = 0;i < e;i++){\n\t\tcin >> g[i] >> c[i]; g[i]--;\n\t\tfor(int j = 0;j < c[i];j++){\n\t\t\tcin >> s[i][j]; s[i][j]--;\n\t\t}\n\t}\n\tfor(int i = 0;i < n;i++){\n\t\tfor(int j = 0;j < e;j++){\n\t\t\tint ma = 0;\n\t\t\tvector<int> vec;\n\t\t\tfor(int k = 0;k < c[j];k++) vec.push_back(d[s[j][k]]);\n\t\t\tsort(vec.begin(),vec.end(),greater<int>());\n\t\t\tfor(int k = 0;k < c[j];k++) ma = max(ma,vec[k] + k);\n\t\t\td[g[j]] = min(d[g[j]],ma);\n\t\t}\n\t}\n\tif(d[t] != INF) cout << d[t] << endl;\n\telse cout << -1 << endl;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3216, "score_of_the_acc": -0.0984, "final_rank": 4 }, { "submission_id": "aoj_2318_3821677", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nconst int INF=1e8;\nvector<int> cost;\nstruct synthesize{\n int goal;\n vector<int> start;\n synthesize(int g, vector<int> s):\n goal(g), start(s) {}\n void update(){\n int tot=0;\n for(int ii : start) tot=max(tot, cost[ii]);\n if(tot==INF) return;\n\n int c=start.size(), use=tot;\n priority_queue<int> pq;\n for(int ii : start) pq.push(cost[ii]);\n while(!pq.empty()){\n int v=pq.top(); pq.pop();\n if(use<v){\n tot++;\n use++;\n }\n use--;\n }\n cost[goal]=min(tot, cost[goal]);\n }\n}; \n\nint main(){\n\tint n, e, t; cin >> n >> e >> t;\n cost.assign(n+1, INF);\n for(int i=1; i<=n; i++){\n int tmp; cin >> tmp;\n if(tmp==1) cost[i]=1;\n }\n vector<synthesize> vec;\n for(int i=0; i<e; i++){\n int g, c; cin >> g >> c;\n vector<int> s(c);\n for(int j=0; j<c; j++) cin >> s[j];\n vec.push_back(synthesize(g, s));\n }\n\n for(int i=0; i<n; i++){\n for(int j=0; j<e; j++){\n vec[j].update();\n }\n }\n\n cout << (cost[t] != INF ? cost[t] : -1) << endl; \n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3236, "score_of_the_acc": -0.1081, "final_rank": 5 }, { "submission_id": "aoj_2318_3759045", "code_snippet": "#include <vector>\n#include <iostream>\n#include <algorithm>\n#include <functional>\nusing namespace std;\nconst int inf = 1012345678;\nint main() {\n\tcin.tie(0);\n\tios_base::sync_with_stdio(false);\n\tint N, E, T;\n\tcin >> N >> E >> T; --T;\n\tvector<int> W(N);\n\tfor (int i = 0; i < N; ++i) cin >> W[i];\n\tvector<int> G(E);\n\tvector<vector<int> > S(E);\n\tfor (int i = 0; i < E; ++i) {\n\t\tint num;\n\t\tcin >> G[i] >> num; --G[i];\n\t\tS[i].resize(num);\n\t\tfor (int j = 0; j < num; ++j) {\n\t\t\tcin >> S[i][j]; --S[i][j];\n\t\t}\n\t}\n\tvector<int> ans(N, inf);\n\tfor (int i = 0; i < N; ++i) {\n\t\tif (W[i] == 1) ans[i] = 1;\n\t}\n\twhile (true) {\n\t\t// at most N loops\n\t\tvector<int> nxt = ans;\n\t\tfor (int i = 0; i < E; ++i) {\n\t\t\tvector<int> d;\n\t\t\tfor (int j : S[i]) {\n\t\t\t\td.push_back(ans[j]);\n\t\t\t}\n\t\t\tsort(d.begin(), d.end(), greater<int>());\n\t\t\tif (d[0] == inf) continue;\n\t\t\tint sub = 0;\n\t\t\tfor (int j = 0; j < d.size(); ++j) {\n\t\t\t\tsub = max(sub, d[j] + j);\n\t\t\t}\n\t\t\tnxt[G[i]] = min(nxt[G[i]], sub);\n\t\t}\n\t\tif (ans == nxt) break;\n\t\tans = nxt;\n\t}\n\tcout << (ans[T] != inf ? ans[T] : -1) << endl;\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3272, "score_of_the_acc": -0.0487, "final_rank": 3 }, { "submission_id": "aoj_2318_3635262", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\n#define int long long\ntypedef vector<int>vint;\ntypedef pair<int,int>pint;\ntypedef vector<pint>vpint;\n#define rep(i,n) for(int i=0;i<(n);i++)\n#define reps(i,f,n) for(int i=(f);i<(n);i++)\n#define all(v) (v).begin(),(v).end()\n#define each(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)\n#define pb push_back\n#define fi first\n#define se second\ntemplate<typename A,typename B>inline void chmin(A &a,B b){if(a>b)a=b;}\ntemplate<typename A,typename B>inline void chmax(A &a,B b){if(a<b)a=b;}\n\nint N,E,T;\nint W[300];\nint G[1000],C[1000],S[1000][10];\n\nint F[300];\n\nsigned main(){\n cin>>N>>E>>T;\n rep(i,N)cin>>W[i];\n rep(i,E){\n cin>>G[i]>>C[i];\n G[i]--;\n rep(j,C[i]){\n cin>>S[i][j];\n S[i][j]--;\n }\n }\n\n fill_n(F,N,1001001001);\n rep(i,N)if(W[i])F[i]=1;\n\n for(int i=0;i<N;i++){\n for(int j=0;j<E;j++){\n vint v;\n rep(k,C[j])v.pb(F[S[j][k]]);\n sort(all(v));\n reverse(all(v));\n int w=0;\n rep(k,C[j]){\n if(w-k<v[k]){\n w=k+v[k];\n }\n }\n chmin(F[G[j]],w);\n }\n }\n T--;\n if(F[T]==1001001001)cout<<-1<<endl;\n else cout<<F[T]<<endl;\n return 0;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 3240, "score_of_the_acc": -0.1357, "final_rank": 6 } ]
aoj_2317_cpp
問題文 委員長の魔女 PATRICIA は,蜘蛛のような糸を吐き結界の中で暮らしている.使い魔の MATHIEU は,委員長の魔女が吐いた糸を引っぱり自在に空を飛んでいる.糸は空間上の線分とみなすことにする. 暁美ほむら は委員長の魔女に対して攻撃を仕掛けようとし,爆弾と間違えて花火玉を投げ込んでしまった.その結果,それぞれの糸について,魔女から距離 p_1, ..., p_M の位置にある部分が切れるだけとなってしまった.花火玉を投げる前,それぞれの糸について,糸の 2 つの端点と魔女はこの順番で同一直線上に並んでおり,端点のどちらか一方を使い魔が引っ張っていた.爆発で糸は切れ,使い魔が持っていた部分とそこから一つ置きの部分を残して糸の一部は消えてしまった. ほむら は次の戦略を考えるために,残った糸についての情報が知りたいと思っている.残った糸の長さの合計値を求めよ. 入力形式 入力は以下の形式で与えられる. N\ M\\ s_1\ t_1\\ ...\\ s_N\ t_N\\ p_1\\ ...\\ p_M\\ N は空間上の糸の個数, M は花火玉によって発生した糸の切断の箇所の個数である. s_i, t_i は糸の情報であり, s_i は使い間が引っ張っていた側の端点, t_i はそうでない側の端点の魔女からの距離である. p_i は花火玉によって切断された位置の情報である. 出力形式 花火玉を投げた後,残った糸の長さの合計値を1行に出力せよ. 制約 1 ≤ N ≤ 10 5 1 ≤ M ≤ 10 5 1 ≤ s_i ≤ 10^9 1 ≤ t_i ≤ 10^9 1 ≤ p_i ≤ 10^9 入力値は全て整数である. s_1, ..., s_N, t_1, ..., t_N, p_1, ..., p_M は全て相異なる. 入出力例 入力例 1 2 3 1 8 20 5 3 7 15 出力例1 10 爆発の後, 1 つ目のひもは端点の魔女からの距離がそれぞれ (1,3), (7,8) の 2 つのひもに, 2 つ目のひもは (20, 15), (7, 5) の 2 つのひもに分かれる.結果,残ったひもの長さの合計は 2+1+5+2 = 10 となる. 入力例 2 1 1 100 1 70 出力例 2 30 入力例 3 6 8 1 10 11 23 99 2 56 58 66 78 88 49 5 15 25 35 45 55 65 75 出力例 3 99 Problem Setter: Flat35
[ { "submission_id": "aoj_2317_5550696", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nconst int INF = 1100000000;\nint main(){\n int N, M;\n cin >> N >> M;\n vector<int> s(N), t(N);\n for (int i = 0; i < N; i++){\n cin >> s[i] >> t[i];\n }\n vector<int> p(M);\n for (int i = 0; i < M; i++){\n cin >> p[i];\n }\n p.push_back(-1);\n p.push_back(INF);\n M += 2;\n sort(p.begin(), p.end());\n vector<int> d(M - 1);\n for (int i = 0; i < M - 1; i++){\n d[i] = p[i + 1] - p[i];\n }\n vector<vector<int>> S(2, vector<int>(M));\n for (int i = 0; i < 2; i++){\n S[i][0] = 0;\n for (int j = 0; j < M - 1; j++){\n S[i][j + 1] = S[i][j];\n if (j % 2 == i){\n S[i][j + 1] += d[j];\n }\n }\n }\n long long ans = 0;\n for (int i = 0; i < N; i++){\n if (s[i] < t[i]){\n int L = lower_bound(p.begin(), p.end(), s[i]) - p.begin();\n int R = lower_bound(p.begin(), p.end(), t[i]) - p.begin() - 1;\n ans += S[(L + 1) % 2][R] - S[(L + 1) % 2][L];\n ans += p[L] - s[i];\n if ((R - L) % 2 != 0){\n ans += t[i] - p[R];\n }\n }\n if (s[i] > t[i]){\n int L = lower_bound(p.begin(), p.end(), t[i]) - p.begin();\n int R = lower_bound(p.begin(), p.end(), s[i]) - p.begin() - 1;\n ans += S[R % 2][R] - S[R % 2][L];\n ans += s[i] - p[R];\n if ((R - L) % 2 != 0){\n ans += p[L] - t[i];\n }\n }\n }\n cout << ans << endl;\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 5740, "score_of_the_acc": -0.3513, "final_rank": 5 }, { "submission_id": "aoj_2317_2910875", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n#define NUM 100001\n\nll start[NUM],tail[NUM];\nll cut_pos[NUM];\nll ruiseki_odd[NUM],ruiseki_even[NUM];\n\nint main(){\n\n\tint N,M;\n\tscanf(\"%d %d\",&N,&M);\n\n\tfor(int i = 0; i < N; i++){\n\t\tscanf(\"%lld %lld\",&start[i],&tail[i]);\n\t}\n\tfor(int i = 0; i < M; i++){\n\t\tscanf(\"%lld\",&cut_pos[i]);\n\t}\n\tsort(cut_pos,cut_pos+M);\n\n\t//偶奇に分けた累積和テーブルを作成\n\tfor(int i = 0; i < M; i++){\n\t\truiseki_odd[i] = 0;\n\t\truiseki_even[i] = 0;\n\t}\n\t//ruiseki_odd[i:奇数] = cut_pos[i:奇数]-cut_pos[i-1]の累積和\n\tfor(int i = 1; i < M; i += 2){\n\t\truiseki_odd[i] = cut_pos[i]-cut_pos[i-1];\n\t\tif(i > 1){\n\t\t\truiseki_odd[i] += ruiseki_odd[i-2];\n\t\t}\n\t}\n\t//ruiseki_even[i:偶数] = cut_pos[i:偶数]-cut_pos[i-1]の累積和\n\tfor(int i = 2; i < M; i += 2){\n\t\truiseki_even[i] = cut_pos[i]-cut_pos[i-1];\n\t\truiseki_even[i] += ruiseki_even[i-2];\n\t}\n\n\tll ans = 0;\n\tint left,right,m;\n\tint max_index,min_index;\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tif(start[i] > tail[i]){\n\n\t\t\tif(cut_pos[0] >= start[i] || cut_pos[M-1] <= tail[i]){ //糸が分断されない場合\n\t\t\t\tans += start[i]-tail[i];\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\t//start[i]以下の、最大の切断点を探す\n\t\t\tleft = 0,right = M-1,m = (left+right)/2;\n\t\t\twhile(left <= right){\n\t\t\t\tif(cut_pos[m] <= start[i]){\n\t\t\t\t\tmax_index = m;\n\t\t\t\t\tleft = m+1;\n\t\t\t\t}else{\n\t\t\t\t\tright = m-1;\n\t\t\t\t}\n\t\t\t\tm = (left+right)/2;\n\t\t\t}\n\t\t\t//tail[i]以上の、最小の切断点を探す\n\t\t\tleft = 0,right = M-1, m = (left+right)/2;\n\t\t\twhile(left <= right){\n\t\t\t\tif(cut_pos[m] >= tail[i]){\n\t\t\t\t\tmin_index = m;\n\t\t\t\t\tright = m-1;\n\t\t\t\t}else{\n\t\t\t\t\tleft = m+1;\n\t\t\t\t}\n\t\t\t\tm = (left+right)/2;\n\t\t\t}\n\t\t\tif(min_index > max_index){ //切断区間内にstartとtailが挟まっている場合\n\t\t\t\tans += start[i]-tail[i];\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tans += start[i]-cut_pos[max_index]; //一番使い魔側の区間を足す\n\n\t\t\tif(max_index%2 == 0){ //最初の切れ目が偶数の場合:ruiseki_odd\n\n\t\t\t\tif(min_index%2 == 0){ //tailが無効区間\n\n\t\t\t\t\tans += ruiseki_odd[max(0,max_index-1)]-ruiseki_odd[max(0,min_index-1)];\n\n\t\t\t\t}else{ //tailが有効区間\n\n\t\t\t\t\tans += ruiseki_odd[max(0,max_index-1)]-ruiseki_odd[min_index]+cut_pos[min_index]-tail[i];\n\t\t\t\t}\n\n\t\t\t}else{ //max_index%2 == 1\n\n\t\t\t\tif(min_index%2 == 1){ //tailが無効区間\n\n\t\t\t\t\tans += ruiseki_even[max(0,max_index-1)]-ruiseki_even[max(0,min_index-1)];\n\n\t\t\t\t}else{ //tailが有効区間\n\n\t\t\t\t\tans += ruiseki_even[max(0,max_index-1)]-ruiseki_even[min_index];\n\t\t\t\t\tif(min_index > 0){\n\t\t\t\t\t\tans += cut_pos[min_index]-tail[i];\n\t\t\t\t\t}else if(min_index != max_index && min_index == 0 && tail[i] < cut_pos[0]){\n\t\t\t\t\t\tans += cut_pos[0]-tail[i];\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t}else{ //start[i] < tail[i]\n\n\t\t\tif(cut_pos[0] >= tail[i] || cut_pos[M-1] <= start[i]){\n\t\t\t\tans += tail[i]-start[i];\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\t//start[i]以上の、最小の切断点を探す\n\t\t\tleft = 0,right = M-1,m = (left+right)/2;\n\t\t\twhile(left <= right){\n\t\t\t\tif(cut_pos[m] >= start[i]){\n\t\t\t\t\tmin_index = m;\n\t\t\t\t\tright = m-1;\n\t\t\t\t}else{\n\t\t\t\t\tleft = m+1;\n\t\t\t\t}\n\t\t\t\tm = (left+right)/2;\n\t\t\t}\n\t\t\t//tail[i]以下の、最大の切断点を探す\n\t\t\tleft = 0,right = M-1,m = (left+right)/2;\n\t\t\twhile(left <= right){\n\t\t\t\tif(cut_pos[m] <= tail[i]){\n\t\t\t\t\tmax_index = m;\n\t\t\t\t\tleft = m+1;\n\t\t\t\t}else{\n\t\t\t\t\tright = m-1;\n\t\t\t\t}\n\t\t\t\tm = (left+right)/2;\n\t\t\t}\n\n\t\t\tif(min_index > max_index){ //切断区間内にstartとtailが挟まっている場合\n\t\t\t\tans += tail[i]-start[i];\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tans += cut_pos[min_index]-start[i];\n\n\t\t\tif(min_index%2 == 1){ //最初の切れ目が奇数の場合;ruiseki_odd\n\n\t\t\t\tif(max_index%2 == 1){ //tailが無効区間\n\n\t\t\t\t\tans += ruiseki_odd[max_index]-ruiseki_odd[min_index];\n\t\t\t\t}else{\n\t\t\t\t\tans += ruiseki_odd[max(0,max_index-1)]-ruiseki_odd[min_index]+tail[i]-cut_pos[max_index];\n\t\t\t\t}\n\n\t\t\t}else{ //min_index%2 == 0:最初の切れ間が偶数:ruiseki_even\n\n\t\t\t\tif(max_index%2 == 0){ //tailが無効区間\n\n\t\t\t\t\tans += ruiseki_even[max_index]-ruiseki_even[min_index];\n\t\t\t\t}else{\n\t\t\t\t\tans += ruiseki_even[max(0,max_index-1)]-ruiseki_even[min_index]+tail[i]-cut_pos[max_index];\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tprintf(\"%lld\\n\",ans);\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 7180, "score_of_the_acc": -0.3128, "final_rank": 3 }, { "submission_id": "aoj_2317_2898458", "code_snippet": "#include <cstdio>\n#include <vector>\n#include <algorithm>\n#include <utility>\nusing namespace std;\nusing ll = long long int;\nconst ll INF = 1LL << 30;\n\nll N, M;\n\nll solve(vector<ll> cuts, vector< pair<ll, ll> > strands) {\n ll ret = 0;\n\n vector<ll> even, odd;\n even.push_back(0); odd.push_back(0);\n for(size_t i=1; i<cuts.size(); i++) {\n ll mo = i % 2, diff = cuts[i] - cuts[i-1];\n (mo ? odd : even).push_back( (mo ? odd : even).back() + diff );\n (mo ? even : odd).push_back( (mo ? even : odd).back() ); // zero\n }\n\n for(auto e : strands) {\n ll l = e.first, r = e.second, sum = 0;\n sum += r - l;\n ll il = lower_bound(cuts.begin(), cuts.end(), l) - cuts.begin();\n ll ir = lower_bound(cuts.begin(), cuts.end(), r) - cuts.begin() - 1;\n\n if(ir - il >= 0) {\n if(il % 2 == 0) {\n sum -= odd [ir] - odd [il];\n }\n if(il % 2 == 1) {\n sum -= even[ir] - even[il];\n }\n\n if( (ir - il) % 2 == 0 ) {\n ll bound = cuts[ir];\n if(l < bound && bound < r) sum -= r - bound;\n }\n }\n\n // printf(\"[%lld, %lld], il = %lld, ir = %lld, len = %lld\\n\", l, r, il, ir, sum);\n ret += sum;\n }\n return ret;\n}\n\nint main() {\n scanf(\"%lld%lld\", &N, &M);\n vector< pair<ll, ll> > strands, rev_strands;\n for(int i=0; i<N; i++) {\n ll l, r; scanf(\"%lld%lld\", &l, &r);\n if(l < r) strands.push_back(make_pair(l, r));\n else rev_strands.push_back(make_pair(INF - l, INF - r));\n }\n\n vector<ll> cuts(M);\n for(int i=0; i<M; i++) {\n scanf(\"%lld\", &cuts[i]);\n }\n\n ll ans = 0;\n sort(cuts.begin(), cuts.end());\n ans += solve(cuts, strands);\n reverse(cuts.begin(), cuts.end());\n for(auto &c : cuts) {\n c = INF - c;\n }\n ans += solve(cuts, rev_strands);\n printf(\"%lld\\n\", ans);\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 9184, "score_of_the_acc": -0.3949, "final_rank": 6 }, { "submission_id": "aoj_2317_1966574", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\n#define int long long\ntypedef vector<int>vint;\ntypedef pair<int,int>pint;\ntypedef vector<pint>vpint;\n#define rep(i,n) for(int i=0;i<(n);i++)\n#define reps(i,f,n) for(int i=(f);i<(n);i++)\n#define all(v) (v).begin(),(v).end()\n#define each(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)\n#define pb push_back\n#define fi first\n#define se second\ntemplate<typename A,typename B>inline void chmin(A &a,B b){if(a>b)a=b;}\ntemplate<typename A,typename B>inline void chmax(A &a,B b){if(a<b)a=b;}\n\nint N,M;\nint S[111111],T[111111];\nint P[222222];\n\nint sum[222222];\nsigned main(){\n cin>>N>>M;\n rep(i,N){\n cin>>S[i]>>T[i];\n if(S[i]>T[i])S[i]*=-1,T[i]*=-1;\n }\n rep(i,M)cin>>P[i],P[i+M]=-P[i];\n M*=2;\n sort(P,P+M);\n\n for(int i=1;i<M;i++){\n sum[i]=P[i]-P[i-1]+(i>1?sum[i-2]:0);\n }\n\n int ans=0;\n rep(i,N){\n if(T[i]<P[0]){\n ans+=T[i]-S[i];\n continue;\n }\n if(P[M-1]<S[i]){\n ans+=T[i]-S[i];\n continue;\n }\n int t=upper_bound(P,P+M,T[i])-P-1;\n int s=upper_bound(P,P+M,S[i])-P;\n if(s%2!=t%2){\n ans+=sum[t-1]-sum[s]+T[i]-P[t]+P[s]-S[i];\n }\n else{\n ans+=sum[t]-sum[s]+P[s]-S[i];\n }\n }\n\n cout<<ans<<endl;\n return 0;\n}", "accuracy": 1, "time_ms": 140, "memory_kb": 6048, "score_of_the_acc": -0.5103, "final_rank": 13 }, { "submission_id": "aoj_2317_1898585", "code_snippet": "#include<bits/stdc++.h>\n\nusing namespace std;\nstruct INIT{INIT(){ios::sync_with_stdio(false);cin.tie(0);}}init;\ntypedef long long LL;\ntypedef tuple<LL,LL> T;\ntypedef vector<LL> VL;\ntypedef vector<T> VT;\n#define rep(i,n) for(auto i=(n)*0;i<n;i++)\n#define range(it,v) for(auto &it:v)\n\n\nLL getlen(set<LL>& event,VL& point,VT& seg,LL prev){\n priority_queue<LL,vector<LL>,greater<LL>> on,off;\n int n=point.size();\n int m=seg.size();\n int i=0,j=0;\n LL res=0;\n range(it,event){\n\t\n\tres+=((LL)on.size())*(it-prev);\n\tprev=it;\n\t//\tcout<<it<<\" \"<<res<<endl;\n\tif(i<n&&point[i]==it){\n\t swap(on,off);\n\t i++;\n\t}\n\telse {\n\t if(on.empty()==false&&on.top()==it){\n\t\ton.pop();\n\t }\n\t else if(off.empty()==false&&off.top()==it){\n\t\toff.pop();\n\t }\n\t else{\n\t\tLL s,t;\n\t\ttie(s,t)=seg[j++];\n\t\ton.push(t);\n\t }\n\t}\n }\n return res;\n}\nint main(){\n int N,M;\n cin>>N>>M;\n VT st;\n set<LL> e;\n rep(i,N){\n\tint s,t;\n\tcin>>s>>t;\n\tif(s<t){\n\t st.push_back(T(s,t));\n\t e.insert(t);\n\t e.insert(s);\n\t}\n\telse {\n\t st.push_back(T(-s,-t));\n\t e.insert(-t);\n\t e.insert(-s);\n\t}\n }\n sort(st.begin(),st.end());\n VL point;\n rep(i,M){\n\tLL it;\n\tcin>>it;\n\tpoint.push_back(it);\n\tpoint.push_back(-it);\n\te.insert(it);\n\te.insert(-it);\n }\n sort(point.begin(),point.end());\n cout<<getlen(e,point,st,-11*4*51*4*1919)<<endl;\n \n return 0;\n}", "accuracy": 1, "time_ms": 250, "memory_kb": 25740, "score_of_the_acc": -1.5854, "final_rank": 16 }, { "submission_id": "aoj_2317_1895883", "code_snippet": "#include<cstdio>\n#include<cstdlib>\n#include<cstring>\n#include<cmath>\n//#include<cctype>\n#include<climits>\n#include<iostream>\n#include<string>\n#include<vector>\n#include<map>\n//#include<list>\n#include<queue>\n#include<deque>\n#include<algorithm>\n//#include<numeric>\n#include<utility>\n//#include<memory>\n#include<functional>\n#include<cassert>\n#include<set>\n#include<stack>\n#include<random>\n\nusing namespace std;\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef vector<int> vi;\ntypedef vector<ll> vll;\ntypedef pair<int, int> pii;\n\nconst int MAXN = 100100;\nint S[MAXN], T[MAXN];\nint P[MAXN];\nint N, M;\nll sumEven[MAXN], sumOdd[MAXN];\n\nll calc(int i) {\n int is = lower_bound(P, P+M, S[i]) - P;\n int it = lower_bound(P, P+M, T[i]) - P; it--;\n if (is == M || it == -1) return T[i] - S[i];\n ll ret = 0;\n if ((it-is)%2 == 0) {\n if (is%2) {\n ret = (sumOdd[it+2] - sumOdd[is]) - (sumEven[it+1] - sumEven[is+1]) - S[i];\n } else {\n ret = (sumEven[it+2] - sumEven[is]) - (sumOdd[it+1] - sumOdd[is+1]) - S[i];\n }\n } else {\n if (is%2) {\n ret = (sumOdd[it+1] - sumOdd[is]) - (sumEven[it+2] - sumEven[is+1]) + T[i] - S[i];\n } else {\n ret = (sumEven[it+1] - sumEven[is]) - (sumOdd[it+2] - sumOdd[is+1]) + T[i] - S[i];\n }\n }\n return ret;\n}\n\nll solve() {\n memset(sumEven, 0, sizeof(sumEven));\n memset(sumOdd, 0, sizeof(sumOdd));\n for (int i = 0; i < M; i += 2) {\n sumEven[i+1] = sumEven[i];\n sumEven[i+2] = sumEven[i] + P[i];\n }\n for (int i = 1; i < M; i += 2) {\n sumOdd[i+2] = sumOdd[i] + P[i];\n sumOdd[i+1] = sumOdd[i+2];\n }\n ll ans = 0;\n for (int i = 0; i < N; i++) if (S[i] < T[i]) {\n ll tmp = calc(i);\n ans += tmp;\n }\n return ans;\n}\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n cin >> N >> M;\n for (int i = 0; i < N; i++)\n cin >> S[i] >> T[i];\n for (int i = 0; i < M; i++) \n cin >> P[i];\n sort(P, P+M);\n ll ans = solve();\n reverse(P, P+M);\n for (int i = 0; i < M; i++)\n P[i] *= -1;\n for (int i = 0; i < N; i++) {\n S[i] *= -1;\n T[i] *= -1;\n }\n ans += solve();\n cout << ans << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 5912, "score_of_the_acc": -0.2608, "final_rank": 2 }, { "submission_id": "aoj_2317_1887064", "code_snippet": "#include <iostream>\n#include <vector>\n#include <queue>\n#define repeat(i,n) for (int i = 0; (i) < (n); ++(i))\ntypedef long long ll;\nusing namespace std;\n\nconst int open_tag = 1;\nconst int close_tag = 2;\nconst int toggle_tag = 3;\nunion event_t {\n struct { int tag; int t; } common;\n struct { int tag; int l, i; } open; // [l, r)\n struct { int tag; int r, i; } close; // [l, r)\n struct { int tag; int p; } toggle;\n};\nbool operator < (event_t a, event_t b) { return a.common.t > b.common.t; } // weak struct ordering\nint main() {\n // init\n int n, m; cin >> n >> m;\n priority_queue<event_t> que;\n repeat (i,n) {\n int s, t; cin >> s >> t;\n event_t el = { open_tag };\n event_t er = { close_tag };\n if (s < t) {\n el.open .l = s;\n er.close.r = t;\n } else {\n el.open .l = - s;\n er.close.r = - t;\n }\n el.open .i = i;\n er.close.i = i;\n que.push(el);\n que.push(er);\n }\n repeat (i,m) {\n int p; cin >> p;\n event_t e = { toggle_tag };\n e.toggle.p = p;\n que.push(e);\n e.toggle.p = - p;\n que.push(e);\n }\n // run\n ll ans = 0;\n int prv = 0;\n int on = 0;\n int off = 0;\n vector<int> ix(n, -1);\n vector<int> acc { 0 };\n while (not que.empty()) {\n event_t e = que.top(); que.pop();\n ans += on *(ll) (e.common.t - prv);\n prv = e.common.t;\n if (e.common.tag == open_tag) {\n on += 1;\n ix[e.open.i] = acc.size() - 1;\n acc.push_back(acc.back());\n } else if (e.common.tag == close_tag) {\n ((acc.back() - acc[ix[e.close.i]]) % 2 == 0 ? on : off) -= 1;\n } else if (e.common.tag == toggle_tag) {\n swap(on, off);\n acc.back() += 1;\n }\n }\n // output\n cout << ans << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 140, "memory_kb": 9124, "score_of_the_acc": -0.6363, "final_rank": 14 }, { "submission_id": "aoj_2317_1885440", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define rep(i,n) for(long long i = 0; i < (long long)(n); i++)\n#define repi(i,a,b) for(long long i = (long long)(a); i < (long long)(b); i++)\n#define pb push_back\n#define all(x) (x).begin(), (x).end()\n#define fi first\n#define se second\n#define mt make_tuple\n#define mp make_pair\ntemplate<class T1, class T2> bool chmin(T1 &a, T2 b) { return b < a && (a = b, true); }\ntemplate<class T1, class T2> bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }\n\nusing ll = long long; using ld = long double; using vll = vector<ll>; using vvll = vector<vll>; using vld = vector<ld>; \nusing vi = vector<int>; using vvi = vector<vi>;\nvll conv(vi& v) { vll r(v.size()); rep(i, v.size()) r[i] = v[i]; return r; }\nusing P = pair<ll, ll>;\n\ntemplate <typename T, typename U> ostream &operator<<(ostream &o, const pair<T, U> &v) { o << \"(\" << v.first << \", \" << v.second << \")\"; return o; }\ntemplate<size_t...> struct seq{}; template<size_t N, size_t... Is> struct gen_seq : gen_seq<N-1, N-1, Is...>{}; template<size_t... Is> struct gen_seq<0, Is...> : seq<Is...>{};\ntemplate<class Ch, class Tr, class Tuple, size_t... Is>\nvoid print_tuple(basic_ostream<Ch,Tr>& os, Tuple const& t, seq<Is...>){ using s = int[]; (void)s{0, (void(os << (Is == 0? \"\" : \", \") << get<Is>(t)), 0)...}; }\ntemplate<class Ch, class Tr, class... Args> \nauto operator<<(basic_ostream<Ch, Tr>& os, tuple<Args...> const& t) -> basic_ostream<Ch, Tr>& { os << \"(\"; print_tuple(os, t, gen_seq<sizeof...(Args)>()); return os << \")\"; }\nostream &operator<<(ostream &o, const vvll &v) { rep(i, v.size()) { rep(j, v[i].size()) o << v[i][j] << \" \"; cout << endl; } return o; }\ntemplate <typename T> ostream &operator<<(ostream &o, const vector<T> &v) { o << '['; rep(i, v.size()) o << v[i] << (i != v.size()-1 ? \", \" : \"\"); o << \"]\"; return o; }\ntemplate <typename T> ostream &operator<<(ostream &o, const set<T> &m) { o << '['; for (auto it = m.begin(); it != m.end(); it++) o << *it << (next(it) != m.end() ? \", \" : \"\"); o << \"]\"; return o; }\ntemplate <typename T, typename U> ostream &operator<<(ostream &o, const map<T, U> &m) { o << '['; for (auto it = m.begin(); it != m.end(); it++) o << *it << (next(it) != m.end() ? \", \" : \"\"); o << \"]\"; return o; }\ntemplate <typename T, typename U> ostream &operator<<(ostream &o, const unordered_map<T, U> &m) { o << '['; for (auto it = m.begin(); it != m.end(); it++) o << *it; o << \"]\"; return o; }\nvoid printbits(ll mask, ll n) { rep(i, n) { cout << !!(mask & (1ll << i)); } cout << endl; }\n#define ldout fixed << setprecision(40) \n\nstatic const double EPS = 1e-14;\nstatic const long long INF = 1e18;\nstatic const long long mo = 1e9+7;\n\n// ?´???????????¨????\n// ?§????O(n)\n// ?????¨???O(1)\nconst function<bool(ll)> f1_default = [](ll x) { return 1; };\nclass Sum1d {\npublic:\n vll data;\n vll sumdata;\n Sum1d(vll& d, function<bool(ll)> f = f1_default) {\n int n = d.size();\n data = d;\n sumdata = vll(n+1, 0);\n rep(i, n) if(f(i)) sumdata[i+1] = data[i];\n rep(i, n) sumdata[i+1] += sumdata[i];\n }\n // [i, j)????°???????????????? (????????????)\n ll sum(int i, int j) {\n if (i >= j)\n return 0;\n return sumdata[j]-sumdata[i];\n }\n // [i, i+ilen)????°???????????????? (????????????)\n ll suml(int i, int len) {\n return this->sum(i, i+len);\n }\n void print(void) {\n rep(i, sumdata.size()) cout << sumdata[i] << \" \"; cout << endl;\n }\n};\nint main(void) {\n cin.tie(0); ios::sync_with_stdio(false);\n ll n, m; cin >> n >> m;\n vll s(n), t(n), p(m); \n rep(i, s.size()) \n cin >> s[i] >> t[i]; \n rep(i, p.size()) \n cin >> p[i];\n\n sort(all(p));\n\n vll pd_even(m), pd_odd(m);\n pd_even[0] = p[0];\n repi(i, 1, m) {\n (i % 2 == 0 ? pd_even[i] : pd_odd[i]) = p[i]-p[i-1];\n (i % 2 == 1 ? pd_even[i] : pd_odd[i]) = 0;\n }\n\n Sum1d s_even(pd_even), s_odd(pd_odd);\n\n ll ret = 0;\n rep(i, s.size()) {\n ll is = lower_bound(all(p), s[i]) - p.begin();\n ll it = lower_bound(all(p), t[i]) - p.begin();\n if (is == it) {\n ret += abs(t[i] - s[i]);\n continue;\n }\n\n if (is < it) {\n ret += abs(p[is] - s[i]);\n if (it % 2 == is % 2) {\n ret += abs(p[it-1] - t[i]);\n }\n ret += (is % 2 == 0 ? s_even : s_odd).sum(is+1, it);\n } else {\n ret += abs(p[is-1] - s[i]);\n if (it % 2 == is % 2) {\n ret += abs(p[it] - t[i]);\n }\n ret += (is % 2 == 0 ? s_even : s_odd).sum(it+1, is);\n }\n }\n cout << ret << endl;\n\n \n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 9960, "score_of_the_acc": -0.4267, "final_rank": 9 }, { "submission_id": "aoj_2317_1885438", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define rep(i,n) for(long long i = 0; i < (long long)(n); i++)\n#define repi(i,a,b) for(long long i = (long long)(a); i < (long long)(b); i++)\n#define pb push_back\n#define all(x) (x).begin(), (x).end()\n#define fi first\n#define se second\n#define mt make_tuple\n#define mp make_pair\ntemplate<class T1, class T2> bool chmin(T1 &a, T2 b) { return b < a && (a = b, true); }\ntemplate<class T1, class T2> bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }\n\nusing ll = long long; using ld = long double; using vll = vector<ll>; using vvll = vector<vll>; using vld = vector<ld>; \nusing vi = vector<int>; using vvi = vector<vi>;\nvll conv(vi& v) { vll r(v.size()); rep(i, v.size()) r[i] = v[i]; return r; }\nusing P = pair<ll, ll>;\n\ntemplate <typename T, typename U> ostream &operator<<(ostream &o, const pair<T, U> &v) { o << \"(\" << v.first << \", \" << v.second << \")\"; return o; }\ntemplate<size_t...> struct seq{}; template<size_t N, size_t... Is> struct gen_seq : gen_seq<N-1, N-1, Is...>{}; template<size_t... Is> struct gen_seq<0, Is...> : seq<Is...>{};\ntemplate<class Ch, class Tr, class Tuple, size_t... Is>\nvoid print_tuple(basic_ostream<Ch,Tr>& os, Tuple const& t, seq<Is...>){ using s = int[]; (void)s{0, (void(os << (Is == 0? \"\" : \", \") << get<Is>(t)), 0)...}; }\ntemplate<class Ch, class Tr, class... Args> \nauto operator<<(basic_ostream<Ch, Tr>& os, tuple<Args...> const& t) -> basic_ostream<Ch, Tr>& { os << \"(\"; print_tuple(os, t, gen_seq<sizeof...(Args)>()); return os << \")\"; }\nostream &operator<<(ostream &o, const vvll &v) { rep(i, v.size()) { rep(j, v[i].size()) o << v[i][j] << \" \"; cout << endl; } return o; }\ntemplate <typename T> ostream &operator<<(ostream &o, const vector<T> &v) { o << '['; rep(i, v.size()) o << v[i] << (i != v.size()-1 ? \", \" : \"\"); o << \"]\"; return o; }\ntemplate <typename T> ostream &operator<<(ostream &o, const set<T> &m) { o << '['; for (auto it = m.begin(); it != m.end(); it++) o << *it << (next(it) != m.end() ? \", \" : \"\"); o << \"]\"; return o; }\ntemplate <typename T, typename U> ostream &operator<<(ostream &o, const map<T, U> &m) { o << '['; for (auto it = m.begin(); it != m.end(); it++) o << *it << (next(it) != m.end() ? \", \" : \"\"); o << \"]\"; return o; }\ntemplate <typename T, typename U> ostream &operator<<(ostream &o, const unordered_map<T, U> &m) { o << '['; for (auto it = m.begin(); it != m.end(); it++) o << *it; o << \"]\"; return o; }\nvoid printbits(ll mask, ll n) { rep(i, n) { cout << !!(mask & (1ll << i)); } cout << endl; }\n#define ldout fixed << setprecision(40) \n\nstatic const double EPS = 1e-14;\nstatic const long long INF = 1e18;\nstatic const long long mo = 1e9+7;\n\n// ?´???????????¨????\n// ?§????O(n)\n// ?????¨???O(1)\nconst function<bool(ll)> f1_default = [](ll x) { return 1; };\nclass Sum1d {\npublic:\n vll data;\n vll sumdata;\n Sum1d(vll& d, function<bool(ll)> f = f1_default) {\n int n = d.size();\n data = d;\n sumdata = vll(n+1, 0);\n rep(i, n) if(f(i)) sumdata[i+1] = data[i];\n rep(i, n) sumdata[i+1] += sumdata[i];\n }\n // [i, j)????°???????????????? (????????????)\n ll sum(int i, int j) {\n if (i >= j)\n return 0;\n return sumdata[j]-sumdata[i];\n }\n // [i, i+ilen)????°???????????????? (????????????)\n ll suml(int i, int len) {\n return this->sum(i, i+len);\n }\n void print(void) {\n rep(i, sumdata.size()) cout << sumdata[i] << \" \"; cout << endl;\n }\n};\nint main(void) {\n cin.tie(0); ios::sync_with_stdio(false);\n ll n, m; cin >> n >> m;\n vll s(n), t(n), p(m); rep(i, s.size()) cin >> s[i] >> t[i]; rep(i, p.size()) cin >> p[i];\n sort(all(p));\n\n vll pd_even(m), pd_odd(m);\n pd_even[0] = p[0];\n repi(i, 1, m) {\n if (i % 2 == 0) {\n pd_even[i] = p[i]-p[i-1];\n pd_odd[i] = 0;\n } else {\n pd_even[i] = 0;\n pd_odd[i] = p[i]-p[i-1];\n }\n }\n /*\n cout << pd_even << endl;\n cout << pd_odd << endl;\n */\n\n Sum1d s_even(pd_even), s_odd(pd_odd);\n /*\n s_even.print();\n s_odd.print();\n cout << s_even.sum(1, 4) << endl;\n */\n\n ll ret = 0;\n rep(i, s.size()) {\n ll is = lower_bound(all(p), s[i]) - p.begin();\n ll it = lower_bound(all(p), t[i]) - p.begin();\n /*\n cout << is << \" \" << it << endl;\n */\n if (is == it) {\n ret += abs(t[i] - s[i]);\n continue;\n }\n\n if (is < it) {\n// cout << \"#Forward\" << endl;\n ret += abs(p[is] - s[i]);\n// cout << abs(p[is] - s[i]) << endl;\n if (it % 2 == is % 2) {\n ret += abs(p[it-1] - t[i]);\n// cout << p[it-1] << \" \" << t[i] << \"#detail\" << endl;\n// cout << abs(p[it-1] - t[i]) << endl;\n }\n ret += (is % 2 == 0 ? s_even : s_odd).sum(is+1, it);\n// cout <<(is % 2 == 0 ? s_even : s_odd).sum(is+1, it) << endl;\n } else {\n// cout << \"#Inverse\" << endl;\n ret += abs(p[is-1] - s[i]);\n// cout << abs(p[is-1] - s[i]) << endl;\n if (it % 2 == is % 2) {\n ret += abs(p[it] - t[i]);\n// cout << abs(p[it] - t[i]) << endl;\n }\n ret += (is % 2 == 0 ? s_even : s_odd).sum(it+1, is);\n// cout << (is % 2 == 0 ? s_even : s_odd).sum(it+1, is) << endl;\n }\n }\n cout << ret << endl;\n\n \n return 0;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 9960, "score_of_the_acc": -0.4511, "final_rank": 11 }, { "submission_id": "aoj_2317_1879645", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef pair<int, int> pii;\n\nint main()\n{\n int N, M, s, t, p;\n cin >> N >> M;\n vector<pii> v[2];\n for (int i = 0; i < N; i++) {\n cin >> s >> t;\n int j = i+1;\n if (s < t) {\n v[0].push_back(pii(s, j));\n v[0].push_back(pii(t, j));\n } else {\n v[1].push_back(pii(s, -j));\n v[1].push_back(pii(t, -j));\n }\n }\n for (int i = 0; i < M; i++) {\n cin >> p;\n v[0].push_back(pii(p, 0));\n v[1].push_back(pii(p, 0));\n }\n\n sort(v[0].begin(), v[0].end());\n sort(v[1].rbegin(), v[1].rend());\n\n ll res = 0;\n for (int i = 0; i < 2; i++) {\n vector<int> use(N, -1);\n int cut = 0, even = 0, odd = 0;\n for (int j = 0; j < (int)v[i].size(); j++) {\n int k = (i == 0 ? +1 : -1);\n int x = v[i][j].second * k;\n if (j > 0) {\n ll cost = k * (ll)(v[i][j].first - v[i][j-1].first) * even;\n res += cost;\n }\n \n if (x != 0) {\n x--;\n if (use[x] == -1) {\n use[x] = cut;\n even++;\n } else {\n if ((cut - use[x]) % 2 == 0) {\n even--;\n } else {\n odd--;\n }\n }\n } else {\n cut++;\n swap(even, odd);\n }\n }\n }\n cout << res << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 110, "memory_kb": 6440, "score_of_the_acc": -0.4532, "final_rank": 12 }, { "submission_id": "aoj_2317_1879631", "code_snippet": "#include<bits/stdc++.h>\n#define N 100005\nusing namespace std;\nlong long n,m,s[N],t[N],p[N],d[N],ans;\n\nint bynary_search(int x){\n int l=0,r=m;\n while(l<r){\n int mid=(l+r)/2;\n if(x<=p[mid])r=mid;\n else l=mid+1;\n }\n return l;\n}\n\nint main(){\n cin>>n>>m;\n for(int i=0;i<n;i++)cin>>s[i]>>t[i];\n for(int i=0;i<m;i++)cin>>p[i];\n sort(p,p+m);\n d[0]=p[0],d[1]=p[1]-p[0];\n ans=0;\n for(int i=2;i<m;i++)d[i]=d[i-2]+p[i]-p[i-1];\n for(int i=0;i<n;i++){\n int r1=bynary_search(s[i]);\n int r2=bynary_search(t[i]);\n if(r1==r2)ans+=max(s[i],t[i])-min(s[i],t[i]);\n else if(abs(r1-r2)==1){\n if(s[i]<t[i])ans+=p[r1]-s[i];\n else ans+=s[i]-p[r1-1];\n }\n else if(s[i]<t[i]){\n if(r2%2==r1%2)ans+=p[r1]-s[i]+d[r2-2]-d[r1]+t[i]-p[r2-1];\n else ans+=p[r1]-s[i]+d[r2-1]-d[r1];\n }else{\n if(r2%2==r1%2)ans+=s[i]-p[r1-1]+d[r1-2]-d[r2]+p[r2]-t[i];\n else ans+=s[i]-p[r1-1]+d[r1-2]-d[r2-1];\n }\n }\n cout<<ans<<endl;\n return 0;\n}", "accuracy": 1, "time_ms": 140, "memory_kb": 4300, "score_of_the_acc": -0.4387, "final_rank": 10 }, { "submission_id": "aoj_2317_1879630", "code_snippet": "#include<bits/stdc++.h>\n#define N 10005\nusing namespace std;\nlong long n,m,s[N],t[N],p[N],d[N],ans;\n\nint bynary_search(int x){\n int l=0,r=m;\n while(l<r){\n int mid=(l+r)/2;\n if(x<=p[mid])r=mid;\n else l=mid+1;\n }\n return l;\n}\n\nint main(){\n cin>>n>>m;\n for(int i=0;i<n;i++)cin>>s[i]>>t[i];\n for(int i=0;i<m;i++)cin>>p[i];\n sort(p,p+m);\n d[0]=p[0],d[1]=p[1]-p[0];\n ans=0;\n for(int i=2;i<m;i++)d[i]=d[i-2]+p[i]-p[i-1];\n for(int i=0;i<n;i++){\n int r1=bynary_search(s[i]);\n int r2=bynary_search(t[i]);\n if(r1==r2)ans+=max(s[i],t[i])-min(s[i],t[i]);\n else if(abs(r1-r2)==1){\n if(s[i]<t[i])ans+=p[r1]-s[i];\n else ans+=s[i]-p[r1-1];\n }\n else if(s[i]<t[i]){\n if(r2%2==r1%2)ans+=p[r1]-s[i]+d[r2-2]-d[r1]+t[i]-p[r2-1];\n else ans+=p[r1]-s[i]+d[r2-1]-d[r1];\n }else{\n if(r2%2==r1%2)ans+=s[i]-p[r1-1]+d[r1-2]-d[r2]+p[r2]-t[i];\n else ans+=s[i]-p[r1-1]+d[r1-2]-d[r2-1];\n }\n }\n cout<<ans<<endl;\n return 0;\n}", "accuracy": 0.19047619047619047, "time_ms": 10, "memory_kb": 1488, "score_of_the_acc": -0.0064, "final_rank": 18 }, { "submission_id": "aoj_2317_1879629", "code_snippet": "#include<bits/stdc++.h>\n#define N 10005\nusing namespace std;\nint n,m,s[N],t[N],p[N],d[N],ans;\n\nint bynary_search(int x){\n int l=0,r=m;\n while(l<r){\n int mid=(l+r)/2;\n if(x<=p[mid])r=mid;\n else l=mid+1;\n }\n return l;\n}\n\nint main(){\n cin>>n>>m;\n for(int i=0;i<n;i++)cin>>s[i]>>t[i];\n for(int i=0;i<m;i++)cin>>p[i];\n sort(p,p+m);\n d[0]=p[0],d[1]=p[1]-p[0];\n ans=0;\n for(int i=2;i<m;i++)d[i]=d[i-2]+p[i]-p[i-1];\n for(int i=0;i<n;i++){\n int r1=bynary_search(s[i]);\n int r2=bynary_search(t[i]);\n if(r1==r2)ans+=max(s[i],t[i])-min(s[i],t[i]);\n else if(abs(r1-r2)==1){\n if(s[i]<t[i])ans+=p[r1]-s[i];\n else ans+=s[i]-p[r1-1];\n }\n else if(s[i]<t[i]){\n if(r2%2==r1%2)ans+=p[r1]-s[i]+d[r2-2]-d[r1]+t[i]-p[r2-1];\n else ans+=p[r1]-s[i]+d[r2-1]-d[r1];\n }else{\n if(r2%2==r1%2)ans+=s[i]-p[r1-1]+d[r1-2]-d[r2]+p[r2]-t[i];\n else ans+=s[i]-p[r1-1]+d[r1-2]-d[r2-1];\n }\n }\n cout<<ans<<endl;\n return 0;\n}", "accuracy": 0.09523809523809523, "time_ms": 10, "memory_kb": 1332, "score_of_the_acc": 0, "final_rank": 19 }, { "submission_id": "aoj_2317_1857100", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef long double ld;\ntypedef pair<ll, ll> P;\n\n#define EACH(i,a) for (auto& i : a)\n#define FOR(i,a,b) for (ll i=(a);i<(b);i++)\n#define RFOR(i,a,b) for (ll i=(b)-1;i>=(a);i--)\n#define REP(i,n) for (ll i=0;i<(n);i++)\n#define RREP(i,n) for (ll i=(n)-1;i>=0;i--)\n#define debug(x) cout<<#x<<\": \"<<x<<endl\n#define pb push_back\n#define ALL(a) (a).begin(),(a).end()\n\nconst ll linf = 1e18;\nconst int inf = 1e9;\nconst double eps = 1e-12;\nconst double pi = acos(-1);\n\ntemplate<typename T>\nistream& operator>>(istream& is, vector<T>& vec) {\n\tEACH(x,vec) is >> x;\n\treturn is;\n}\n/*\ntemplate<class... T>\nostream& operator<<(ostream& os, tuple<T...>& t) {\n\tfor (size_t i = 0; i < tuple_size< tuple<T...> >::value; ++i) {\n\t\tif (i) os << \" \";\n\t\tos << get<0>(t);\n\t}\n\treturn os;\n}\n*/\ntemplate<typename T>\nostream& operator<<(ostream& os, vector<T>& vec) {\n\tREP(i,vec.size()) {\n\t\tif (i) os << \" \";\n\t\tos << vec[i];\n\t}\n\treturn os;\n}\ntemplate<typename T>\nostream& operator<<(ostream& os, vector< vector<T> >& vec) {\n\tREP(i,vec.size()) {\n\t\tif (i) os << endl;\n\t\tos << vec[i];\n\t}\n\treturn os;\n}\n\nint main() {\n\tstd::ios::sync_with_stdio(false);\n\tstd::cin.tie(0);\n\tll N, M; cin >> N >> M;\n\tvector<P> v;\n\tREP(i, N) {\n\t\tll s, t; cin >> s >> t;\n\t\tv.pb({s, t});\n\t}\n\tvector<ll> p(M); cin >> p; p.pb(0); p.pb(linf);\n\tsort(p.begin(), p.end());\n\tvector<ll> sOdd(p.size(), 0), sEven(p.size(), 0);\n\tREP(i, (int)p.size()-1) {\n\t\tsOdd[i+1] += sOdd[i];\n\t\tsEven[i+1] +=+ sEven[i];\n\t\tif (i % 2 == 0) {\n\t\t\tsEven[i+1] += p[i+1]-p[i];\n\t\t}\n\t\telse {\n\t\t\tsOdd[i+1] += p[i+1]-p[i];\n\t\t}\n\t}\n\tll ans = 0;\n\tREP(i, N) {\n\t\tll s, t; tie(s, t) = v[i];\n\t\tbool f = false;\n\t\tif (s > t) {\n\t\t\tf = true;\n\t\t\tint temp = s; s = t; t = temp;\n\t\t}\n\t\tint sid = (upper_bound(ALL(p), s)-p.begin())-1;\n\t\tint tid = (lower_bound(ALL(p), t)-p.begin())-1;\n\t\t// [sid, tid)\n\t\t// cout << s << \" \" << t << \" \" << sid << \" \" << tid << endl;\n\t\t// ll ba = ans;\n\t\tif (tid > sid+1) {\n\t\t\tif ((!f && (sid % 2 == 0)) || (f && (tid%2 == 0))) {\n\t\t\t\tans += sEven[tid] - sEven[sid+1];\n\t\t\t}\n\t\t\telse {\n\t\t\t\tans += sOdd[tid] - sOdd[sid+1];\n\t\t\t}\n\t\t}\n\t\tif (sid == tid) { // [s, t)\n\t\t\tans += t-s;\n\t\t}\n\t\telse {\n\t\t\tif ((tid-sid)%2 == 0 || !f) {\n\t\t\t\tans += p[sid+1]-s;\n\t\t\t}\n\t\t\tif ((tid-sid)%2 == 0 || f) {\n\t\t\t\tans += t-p[tid];\n\t\t\t}\n\t\t}\n\t\t// cout << ans-ba << endl;\n\t}\n\tcout << ans << endl;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 6848, "score_of_the_acc": -0.3236, "final_rank": 4 }, { "submission_id": "aoj_2317_1833064", "code_snippet": "#include <algorithm>\n#include <cmath>\n#include <climits>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <fstream>\n#include <iostream>\n#include <list>\n#include <map>\n#include <queue>\n#include <set>\n#include <sstream>\n#include <stack>\n#include <string>\n#include <vector>\n#include <cassert>\n#include <functional>\n\nusing namespace std;\n\n#define LOG(...) printf(__VA_ARGS__)\n//#define LOG(...)\n#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);++i)\n#define REP(i,n) for(int i=0;i<(int)(n);++i)\n#define ALL(a) (a).begin(),(a).end()\n#define RALL(a) (a).rbegin(),(a).rend()\n#define EXIST(s,e) ((s).find(e)!=(s).end())\n#define SORT(c) sort((c).begin(),(c).end())\n#define RSORT(c) sort((c).rbegin(),(c).rend())\n#define CLR(a) memset((a), 0 ,sizeof(a))\n\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef vector<bool> vb;\ntypedef vector<int> vi;\ntypedef vector<ll> vll;\ntypedef vector<vb> vvb;\ntypedef vector<vi> vvi;\ntypedef vector<vll> vvll;\ntypedef pair<int, int> pii;\ntypedef pair<ll, ll> pll;\n\nconst int dx[] = { -1, 0, 1, 0 }; const int dy[] = { 0, 1, 0, -1 };\n\nstruct UnionFind {\n\tvector<int> v;\n\tUnionFind(int n) : v(n) { for (int i = 0; i < n; i++) v[i] = i; }\n\tint find(int x) { return v[x] == x ? x : v[x] = find(v[x]); }\n\tvoid unite(int x, int y) { v[find(x)] = find(y); }\n};\n\nint main() {\n\tint n, m;\n\tcin >> n >> m;\n\tvector<pair<ll, char>> vpii;\n\tvector<ll> hanabi;\n\tmap<ll, bool> mib;\n\tREP(i, n) {\n\t\tint a, b;\n\t\tcin >> a >> b;\n\t\tif (a < b) {\n\t\t\tvpii.push_back({ a, 'b' });\n\t\t\tvpii.push_back({ b, 'e' });\n\t\t}else{\n\t\t\tvpii.push_back({ a, 'e' });\n\t\t\tvpii.push_back({ b, 'b' });\n\t\t}\n\t}\n\n\tREP(i, m) {\n\t\tint a;\n\t\tcin >> a;\n\t\tvpii.push_back({ a,'f' });\n\t\thanabi.push_back(a);\n\t}\n\tSORT(hanabi);\n\tREP(i, n) {\n\t\tif (vpii[i * 2].first < vpii[i * 2 + 1].first) {\n\t\t\tmib[vpii[i * 2 + 1].first] =\n\t\t\t\t(bool)((lower_bound(ALL(hanabi), vpii[i * 2 + 1].first) -\n\t\t\t\t\t\tlower_bound(ALL(hanabi), vpii[i * 2].first)+1) % 2);\n\t\t\tmib[vpii[i * 2].first] = true;\n\t\t}\n\t\telse {\n\t\t\tmib[vpii[i * 2+1].first] =\n\t\t\t\t(bool)((lower_bound(ALL(hanabi), vpii[i * 2].first) -\n\t\t\t\t\t\tlower_bound(ALL(hanabi), vpii[i * 2+1].first)+1) % 2);\n\t\t\tmib[vpii[i * 2].first] = true;\n\t\t}\n\t}\n\tSORT(vpii);\n\tll ans = 0;\n\tll ex = 0;\n\tll vo = 0;\n\tif (vpii[0].second == 'b')\n\t\tif (mib[vpii[0].first]) {\n\t\t\tex++;\n\t\t}\n\t\telse {\n\t\t\tvo++;\n\t\t}\n\tFOR(i, 1, vpii.size()) {\n\t\tans += ex*(vpii[i].first - vpii[i - 1].first);\n\t\tif (vpii[i].second == 'b')\n\t\t\tif (mib[vpii[i].first]) {\n\t\t\t\tex++;\n\t\t\t}\n\t\t\telse {\n\t\t\t\tvo++;\n\t\t\t}\n\t\tif (vpii[i].second == 'e') {\n\t\t\tif (mib[vpii[i].first]) {\n\t\t\t\tex--;\n\t\t\t}\n\t\t\telse {\n\t\t\t\tvo--;\n\t\t\t\t\n\t\t\t\t\n\t\t\t}\n\t\t}\n\t\tif (vpii[i].second == 'f') {\n\t\t\tswap(ex,vo);\n\t\t}\n\t}\n\tcout << ans << endl;\n}", "accuracy": 1, "time_ms": 280, "memory_kb": 20792, "score_of_the_acc": -1.4558, "final_rank": 15 }, { "submission_id": "aoj_2317_1833047", "code_snippet": "#include <algorithm>\n#include <cmath>\n#include <climits>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <fstream>\n#include <iostream>\n#include <list>\n#include <map>\n#include <queue>\n#include <set>\n#include <sstream>\n#include <stack>\n#include <string>\n#include <vector>\n#include <cassert>\n#include <functional>\n\nusing namespace std;\n\n#define LOG(...) printf(__VA_ARGS__)\n//#define LOG(...)\n#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);++i)\n#define REP(i,n) for(int i=0;i<(int)(n);++i)\n#define ALL(a) (a).begin(),(a).end()\n#define RALL(a) (a).rbegin(),(a).rend()\n#define EXIST(s,e) ((s).find(e)!=(s).end())\n#define SORT(c) sort((c).begin(),(c).end())\n#define RSORT(c) sort((c).rbegin(),(c).rend())\n#define CLR(a) memset((a), 0 ,sizeof(a))\n\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef vector<bool> vb;\ntypedef vector<int> vi;\ntypedef vector<ll> vll;\ntypedef vector<vb> vvb;\ntypedef vector<vi> vvi;\ntypedef vector<vll> vvll;\ntypedef pair<int, int> pii;\ntypedef pair<ll, ll> pll;\n\nconst int dx[] = { -1, 0, 1, 0 }; const int dy[] = { 0, 1, 0, -1 };\n\nstruct UnionFind {\n\tvector<int> v;\n\tUnionFind(int n) : v(n) { for (int i = 0; i < n; i++) v[i] = i; }\n\tint find(int x) { return v[x] == x ? x : v[x] = find(v[x]); }\n\tvoid unite(int x, int y) { v[find(x)] = find(y); }\n};\n\nint main() {\n\tint n, m;\n\tcin >> n >> m;\n\tvector<pair<int, char>> vpii;\n\tvector<int> hanabi;\n\tmap<int, bool> mib;\n\tREP(i, n) {\n\t\tint a, b;\n\t\tcin >> a >> b;\n\t\tif (a < b) {\n\t\t\tvpii.push_back({ a, 'b' });\n\t\t\tvpii.push_back({ b, 'e' });\n\t\t}else{\n\t\t\tvpii.push_back({ a, 'e' });\n\t\t\tvpii.push_back({ b, 'b' });\n\t\t}\n\t}\n\tREP(i, m) {\n\t\tint a;\n\t\tcin >> a;\n\t\tvpii.push_back({ a,'f' });\n\t\thanabi.push_back(a);\n\t}\n\tSORT(hanabi);\n\tREP(i, n) {\n\t\tif (vpii[i * 2].first < vpii[i * 2 + 1].first) {\n\t\t\tmib[vpii[i * 2 + 1].first] =\n\t\t\t\t(bool)((lower_bound(ALL(hanabi), vpii[i * 2 + 1].first) -\n\t\t\t\t\t\tlower_bound(ALL(hanabi), vpii[i * 2].first)+1) % 2);\n\t\t\tmib[vpii[i * 2].first] = true;\n\t\t}\n\t\telse {\n\t\t\tmib[vpii[i * 2+1].first] =\n\t\t\t\t(bool)((lower_bound(ALL(hanabi), vpii[i * 2].first) -\n\t\t\t\t\t\tlower_bound(ALL(hanabi), vpii[i * 2+1].first)+1) % 2);\n\t\t\tmib[vpii[i * 2].first] = true;\n\t\t}\n\t}\n\tSORT(vpii);\n\tll ans = 0;\n\tint ex = 0;\n\tint vo = 0;\n\tif (vpii[0].second == 'b')\n\t\tif (mib[vpii[0].first]) {\n\t\t\tex++;\n\t\t}\n\t\telse {\n\t\t\tvo++;\n\t\t}\n\tFOR(i, 1, vpii.size()) {\n\t\tans += ex*(vpii[i].first - vpii[i - 1].first);\n\t\tif (vpii[i].second == 'b')\n\t\t\tif (mib[vpii[i].first]) {\n\t\t\t\tex++;\n\t\t\t}\n\t\t\telse {\n\t\t\t\tvo++;\n\t\t\t}\n\t\tif (vpii[i].second == 'e') {\n\t\t\tif (mib[vpii[i].first]) {\n\t\t\t\tex--;\n\t\t\t}\n\t\t\telse {\n\t\t\t\tvo--;\n\t\t\t\t\n\t\t\t\t\n\t\t\t}\n\t\t}\n\t\tif (vpii[i].second == 'f') {\n\t\t\tswap(ex,vo);\n\t\t}\n\t}\n\tcout << ans << endl;\n}", "accuracy": 0.5476190476190477, "time_ms": 230, "memory_kb": 15032, "score_of_the_acc": -1.0979, "final_rank": 17 }, { "submission_id": "aoj_2317_1829741", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n \n \nint N, M;\nint s[100000], t[100000];\nint sum[2][100003];\n \nint main()\n{\n \n scanf(\"%d %d\", &N, &M);\n for(int i = 0; i < N; i++) {\n scanf(\"%d %d\", &s[i], &t[i]);\n }\n vector< int > P(M);\n for(int i = 0; i < M; i++) {\n scanf(\"%d\", &P[i]);\n }\n sort(P.begin(), P.end());\n \n long long ret = 0;\n for(int i = 0; i < N; i++) {\n auto p = lower_bound(P.begin(), P.end(), s[i]);\n auto q = lower_bound(P.begin(), P.end(), t[i]);\n if(p == q) {\n ret += abs(t[i] - s[i]);\n } else {\n s[i] > t[i] ? --p : --q;\n \n if((p - q) % 2 != 0) {\n ret += abs(*p - s[i]);\n ret += abs(*q - t[i]);\n if(abs(p - q) == 1) continue;\n s[i] < t[i] ? (++p, --q) : (--p, ++q);\n auto mm = min(p - P.begin(), q - P.begin());\n sum[mm & 1][mm]++;\n sum[mm & 1][max(p - P.begin(), q - P.begin())]--;\n } else {\n ret += abs(*p - s[i]);\n if(abs(p - q) == 0) continue;\n s[i] < t[i] ? ++p : --p;\n auto mm = min(p - P.begin(), q - P.begin());\n sum[mm & 1][mm]++;\n sum[mm & 1][max(p - P.begin(), q - P.begin())]--;\n }\n }\n }\n for(int i = 1; i < M; i++) {\n sum[0][i] += sum[0][i - 1];\n sum[1][i] += sum[1][i - 1];\n }\n for(int i = 0; i < M - 1; i++) {\n ret += 1LL * sum[i & 1][i] * (P[i + 1] - P[i]);\n }\n printf(\"%lld\\n\", ret);\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 4820, "score_of_the_acc": -0.2161, "final_rank": 1 }, { "submission_id": "aoj_2317_1816952", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define FOR(i,k,n) for(int i = (int)(k); i < (int)(n); i++)\n#define REP(i,n) FOR(i,0,n)\n#define ALL(a) a.begin(), a.end()\n#define MS(m,v) memset(m,v,sizeof(m))\n#define D10 fixed<<setprecision(10)\ntypedef long long ll;\ntypedef long double ld;\ntypedef vector<ll> vi;\ntypedef vector<string> vs;\ntypedef pair<int, int> pii;\nconst int MOD = 1000000007;\nconst int INF = MOD + 1;\nconst ld EPS = 1e-12;\ntemplate<class T> T &chmin(T &a, const T &b) { return a = min(a, b); }\ntemplate<class T> T &chmax(T &a, const T &b) { return a = max(a, b); }\n\n/*--------------------template--------------------*/\n\nint main()\n{\n\tint n, m;\n\tcin >> n >> m;\n\tvector<pii> v;\n\tREP(i, n)\n\t{\n\t\tint a, b;\n\t\tcin >> a >> b;\n\t\tv.emplace_back(a, b);\n\t}\n\tvi p(m);\n\tREP(i, m) cin >> p[i];\n\tsort(ALL(p));\n\tvi os, es;\n\tes.push_back(0), es.push_back(p[0]);\n\tos.push_back(0);\n\tif(m > 1) os.push_back(p[1] - p[0]);\n\telse os.push_back(0);\n\tFOR(i, 2, m)\n\t{\n\t\tif (i % 2) os.push_back(os.back() + p[i] - p[i - 1]);\n\t\telse es.push_back(es.back() + p[i] - p[i - 1]);\n\t}\n\tll ans = 0;\n\tREP(i, n) \n\t{\n\t\tint a = v[i].first, b = v[i].second;\n\t\tll tmp = 0;\n\t\tif (a < b)\n\t\t{\n\t\t\tint l = upper_bound(ALL(p), a) - p.begin();\n\t\t\tint r = lower_bound(ALL(p), b) - p.begin() - 1;\t\t\t\n\t\t\tif (l > r)\n\t\t\t{\n\t\t\t\ttmp += b - a;\n\t\t\t}\n\t\t\telse if (l % 2)\n\t\t\t{\n\t\t\t\tif (r % 2)\n\t\t\t\t{\n\t\t\t\t\ttmp += os[r / 2 + 1] - os[l / 2 + 1];\n\t\t\t\t\ttmp += p[l] - a;\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\ttmp += os[(r-1) / 2 + 1] - os[l / 2 + 1];\n\t\t\t\t\ttmp += p[l] - a;\n\t\t\t\t\ttmp += b - p[r];\n\t\t\t\t}\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tif (r % 2)\n\t\t\t\t{\n\t\t\t\t\ttmp = es[r / 2 + 1] - es[l / 2 + 1];\n\t\t\t\t\ttmp += p[l] - a;\n\t\t\t\t\ttmp += b - p[r];\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\ttmp += es[r / 2 + 1] - es[l / 2 + 1];\n\t\t\t\t\ttmp += p[l] - a;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\telse\n\t\t{\n\t\t\tint l = upper_bound(ALL(p), b) - p.begin();\n\t\t\tint r = lower_bound(ALL(p), a) - p.begin() - 1;\n\t\t\tif (l > r)\n\t\t\t{\n\t\t\t\ttmp += a - b;\n\t\t\t}\n\t\t\telse if (r % 2)\n\t\t\t{\n\t\t\t\tif (l % 2)\n\t\t\t\t{\n\t\t\t\t\ttmp += es[r / 2 + 1] - es[l / 2 + 1];\n\t\t\t\t\ttmp += a - p[r];\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\ttmp += es[(r - 1) / 2 + 1] - es[l / 2 + 1];\n\t\t\t\t\ttmp += p[l] - b;\n\t\t\t\t\ttmp += a - p[r];\n\t\t\t\t}\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tif (l % 2)\n\t\t\t\t{\n\t\t\t\t\ttmp = os[(r-1) / 2 + 1] - os[l / 2 + 1];\n\t\t\t\t\ttmp += p[l] - b;\n\t\t\t\t\ttmp += a - p[r];\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\ttmp += os[r / 2] - os[l / 2];\n\t\t\t\t\ttmp += a - p[r];\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\t//cout << tmp << endl;\n\t\tans += tmp;\n\t}\n\tcout << ans << endl;\n\treturn 0;\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 5748, "score_of_the_acc": -0.4004, "final_rank": 7 }, { "submission_id": "aoj_2317_1815309", "code_snippet": "#include <iostream>\n#include <vector>\n#include <set>\n#include <algorithm>\n\nusing namespace std;\n\nstruct sec{\n int s,t;\n};\n\nstruct event{\n int type;//0:cut +1:begin(on) +2:begin(off) +3:end(on) +4:end(off)\n int x;\n event(int type, int x){\n this->type=type,this->x=x;\n }\n};\n\nint main(){\n int N,M;\n cin>>N>>M;\n sec S[N];\n int C[M];\n for(auto&s:S) scanf(\"%d %d\",&s.s,&s.t);\n for(auto&c:C) scanf(\"%d\",&c);\n\n vector<event> E;\n for(auto&c:C) E.emplace_back(event(0,c));\n sort(C,C+M);\n long long sum=0;\n\n for(auto&s:S){\n auto lo=lower_bound(C,C+M,min(s.s,s.t));\n auto up=upper_bound(C,C+M,max(s.s,s.t));\n if(lo==up){\n sum+=abs(s.s-s.t);\n continue;\n }\n if(s.s<s.t){\n sum+=*lo-s.s;\n E.emplace_back(event(2,*lo));\n if((up-lo)%2==0){\n sum+=s.t-*(up-1);\n E.emplace_back(event(4,*(up-1)));\n }else{\n E.emplace_back(event(3,*(up-1)));\n }\n }else{\n sum+=s.s-*(up-1);\n E.emplace_back(event(4,*(up-1)));\n if((up-lo)%2){\n E.emplace_back(event(1,*lo));\n }else{\n E.emplace_back(event(2,*lo));\n sum+=*lo-s.t;\n }\n }\n }\n sort(E.begin(),E.end(),[](const event& a, const event& b){return a.x==b.x?a.type<b.type:a.x<b.x;});\n\n int on_n=0,off_n=0;\n for(auto e=E.begin();e!=E.end();e++){\n // cout << e->x << \" \" << e->type << endl;\n switch(e->type){\n case 0:\n if(on_n)sum+=(long long int)on_n*(e->x - (e-1)->x);\n swap(on_n,off_n);\n break;\n case 1:\n on_n++;\n break;\n case 2:\n off_n++;\n break;\n case 3:\n off_n--;\n break;\n case 4:\n on_n--;\n break;\n }\n }\n cout << sum << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 8292, "score_of_the_acc": -0.4071, "final_rank": 8 }, { "submission_id": "aoj_2317_1815171", "code_snippet": "#include <iostream>\n#include <vector>\n#include <set>\n#include <algorithm>\n\nusing namespace std;\n\nstruct event{\n int x;\n int type; //0:cut +:?§???? -:??????\n event(int type, int x){\n this->type=type; this->x=x;\n }\n};\n\nstruct section{\n int s, t;\n bool on;\n};\n\nint main(){\n int N,M;\n cin>>N>>M;\n vector<event> E1,E2;\n section S[N+1];\n for(int i=1;i<=N;i++){\n cin>>S[i].s>>S[i].t;\n if(S[i].s<S[i].t){\n E1.push_back(event(i,S[i].s));\n E1.push_back(event(-i,S[i].t));\n }else{\n E2.push_back(event(i,S[i].s));\n E2.push_back(event(-i,S[i].t));\n }\n }\n for(int i=0;i<M;i++){\n int x;\n cin>>x;\n E1.push_back(event(0,x));\n E2.push_back(event(0,x));\n }\n sort(E1.begin(),E1.end(),[](const event& a, const event& b){return a.x==b.x?a.type>b.type:a.x<b.x;});\n sort(E2.begin(),E2.end(),[](const event& a, const event& b){return a.x==b.x?a.type>b.type:a.x>b.x;});\n int sum=0;\n set<int> I;\n for(auto& i:S)i.on=true;\n for(auto& e:E1){\n if(e.type>0){\n I.insert(e.type);\n }else if(e.type<0){\n if(S[-e.type].on)sum+=S[-e.type].t-S[-e.type].s;\n I.erase(-e.type);\n }else{\n for(auto& i:I){\n if(S[i].on){\n S[i].on=false;\n sum+=e.x-S[i].s;\n S[i].s=e.x;\n }else{\n S[i].on=true;\n S[i].s=e.x;\n }\n }\n }\n }\n for(auto& i:I){\n if(S[i].on)sum+=S[i].t-S[i].s;\n }\n I.clear();\n\n for(auto& e:E2){\n if(e.type>0){\n I.insert(e.type);\n }else if(e.type<0){\n if(S[-e.type].on)sum+=-S[-e.type].t+S[-e.type].s;\n I.erase(-e.type);\n }else{\n for(auto& i:I){\n if(S[i].on){\n S[i].on=false;\n sum+=-e.x+S[i].s;\n S[i].s=e.x;\n }else{\n S[i].on=true;\n S[i].s=e.x;\n }\n }\n }\n }\n for(auto& i:I){\n if(S[i].on)sum+=-S[i].t+S[i].s;\n }\n I.clear();\n cout << sum << endl;\n}", "accuracy": 0.09523809523809523, "time_ms": 420, "memory_kb": 3412, "score_of_the_acc": -1.0852, "final_rank": 20 } ]
aoj_2319_cpp
問題文 キュゥべえ はなかなか契約を結んでくれない 鹿目まどか にしびれを切らせて,ソウルジェムゲームと称して 佐倉杏子 と 美樹さやか のソウルジェムの中身 (魂) を W 列 H 段のロッカーの中に隠してしまった.そこで, まどか は 杏子 と さやか を助け出したい.しかし,契約を結ばずに助けるにはロッカーボックスの壁を操作して 2 人のソウルジェムの中身を,指定されたロッカーボックスに正しく移さなければならない.ロッカーはロッカーボックスが W 列 H 段に並ぶことで構成され,隣り合うロッカーボックスは壁で仕切られている. まどか は, 1 回の操作でいずれかの隣りあった 2 つのロッカーボックスの壁を取り除くか取り付けることができる.ただし,ロッカーボックスの壁を開けるには制約があり,各ロッカーボックスから隣接する上下左右の壁のうち,高々 2 枚までの壁しか同時に開けておくことはできない.ロッカーボックスには上の段から下の段に向かって重力が働いている.ソウルジェムの中身は液体と同じような動きをし,隣りあった 2 つのロッカーボックスの壁が取り除かれている時,同じ高さであれば互いに同じ量になり,違う高さであれば低い方へ移動する.すべての操作が終わった時に指定された 2 つのロッカーボックスに正しい組み合わせでソウルジェムの中身がすべて入っていれば まどか の勝利となる.また,一部のロッカーボックスは穢れており,その中へソウルジェムの中身が入ってしまうと大変なことになってしまうので,そのロッカーボックスはソウルジェムの中身の移動のために使うことはできない. 最小で何度の操作を行えば まどか は 2 人を救いだすことができるかを求めよ. 入力形式 入力は以下の形式で与えられる. H\ W\\ s_{11}\ s_{12}\ … s_{1W}\\ s_{21}\ s_{22}\ … s_{2W}\\ …\\ s_{H1}\ s_{H2}\ … s_{HW}\\ H はロッカーの段数, W はロッカーの列数である. s_{ij} は上から i 段目の左から j 列目のロッカーボックスの状態を表す. 杏子 のソウルジェムの中身が入っているときは 'K' であり, さやか のソウルジェムの中身が入っているときは 'S' , 杏子 のソウルジェムの中身を移すべきロッカーボックスであるときは 'k' , さやか のソウルジェムの中身を移すべきロッカーボックスであるときは 's' ,穢れたロッカーボックスであるときは '#' ,それ以外のロッカーボックスであるときは '.' である. 出力形式 2 人を救い出すために必要な操作の最小回数を出力せよ.どのように操作しても救い出すことが出来ない場合は CONTRACT と出力せよ. 制約 1 ≤ H ≤ 10 1 ≤ W ≤ 10 s_{ij} は '#' , '.' , 'S' , 'K' , 's' , 'k' のいずれかである. s_{ij}\ =\ 'S' であるような i, j がちょうど 1 つだけ存在する. s_{ij}\ =\ 'K' であるような i, j がちょうど 1 つだけ存在する. s_{ij}\ =\ 's' であるような i, j がちょうど 1 つだけ存在する. s_{ij}\ =\ 'k' であるような i, j がちょうど 1 つだけ存在する. 入出力例 入力例 1 4 4 .S.. K... .... ..sk 出力例1 9 入力例 2 5 5 .S.K. ..#.. .###. ..#.. .k.s. 出力例 2 CONTRACT 入力例 3 8 5 S.... ####K ##... #..## #..## ##k## #...# #.s.# 出力例 3 19 入力例 4 8 5 S.... ####K ##... ##.## ##.## ##k## #...# #.s.# 出力例 4 CONTRACT 入力例 5 10 10 .......... ##......## #K......S# .......... .......... .#..##..#. ..##..##.. .......... .......... ....s.k... 出力例 5 22 入力例 6 3 3 S.s K.. ..k 出力例 6 CONTRACT Problem Setter: Flat35
[ { "submission_id": "aoj_2319_5955599", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing uint = unsigned int;\nusing ull = unsigned long long;\n#define rep(i,n) for(int i=0;i<int(n);i++)\n#define rep1(i,n) for(int i=1;i<=int(n);i++)\n#define per(i,n) for(int i=int(n)-1;i>=0;i--)\n#define per1(i,n) for(int i=int(n);i>0;i--)\n#define all(c) c.begin(),c.end()\n#define si(x) int(x.size())\n#define pb push_back\n#define eb emplace_back\n#define fs first\n#define sc second\ntemplate<class T> using V = vector<T>;\ntemplate<class T> using VV = vector<vector<T>>;\ntemplate<class T,class U> bool chmax(T& x, U y){\n\tif(x<y){ x=y; return true; }\n\treturn false;\n}\ntemplate<class T,class U> bool chmin(T& x, U y){\n\tif(y<x){ x=y; return true; }\n\treturn false;\n}\ntemplate<class T> void mkuni(V<T>& v){sort(all(v));v.erase(unique(all(v)),v.end());}\ntemplate<class T> int lwb(const V<T>& v, const T& a){return lower_bound(all(v),a) - v.begin();}\ntemplate<class T>\nV<T> Vec(size_t a) {\n return V<T>(a);\n}\ntemplate<class T, class... Ts>\nauto Vec(size_t a, Ts... ts) {\n return V<decltype(Vec<T>(ts...))>(a, Vec<T>(ts...));\n}\ntemplate<class S,class T> ostream& operator<<(ostream& o,const pair<S,T> &p){\n\treturn o<<\"(\"<<p.fs<<\",\"<<p.sc<<\")\";\n}\ntemplate<class T> ostream& operator<<(ostream& o,const vector<T> &vc){\n\to<<\"{\";\n\tfor(const T& v:vc) o<<v<<\",\";\n\to<<\"}\";\n\treturn o;\n}\nconstexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n-1); }\n\n#ifdef LOCAL\n#define show(x) cerr << \"LINE\" << __LINE__ << \" : \" << #x << \" = \" << (x) << endl\nvoid dmpr(ostream& os){os<<endl;}\ntemplate<class T,class... Args>\nvoid dmpr(ostream&os,const T&t,const Args&... args){\n\tos<<t<<\" ~ \";\n\tdmpr(os,args...);\n}\n#define shows(...) cerr << \"LINE\" << __LINE__ << \" : \";dmpr(cerr,##__VA_ARGS__)\n#define dump(x) cerr << \"LINE\" << __LINE__ << \" : \" << #x << \" = {\"; \\\n\tfor(auto v: x) cerr << v << \",\"; cerr << \"}\" << endl;\n#else\n#define show(x) void(0)\n#define dump(x) void(0)\n#define shows(...) void(0)\n#endif\n\ntemplate<class D> D divFloor(D a, D b){\n\treturn a / b - (((a ^ b) < 0 && a % b != 0) ? 1 : 0);\n}\ntemplate<class D> D divCeil(D a, D b) {\n\treturn a / b + (((a ^ b) > 0 && a % b != 0) ? 1 : 0);\n}\n\nint dp[11][11][11][2][2];\n// h, aw, bw, (S(a) \\cap b != empty?), (S(b) \\cap a != empty?), \n// aw == W : a doesn't exist in this raw\n\nbool can[10][10][10];\nbool in(int i,int x,int y){\n\tif(x>y) swap(x,y);\n\treturn x<=i && i<=y;\n}\n\nconst int inf = 1e9;\nint solve(){\n\tint H,W; cin >> H >> W;\n\tV<string> s(H); rep(i,H) cin >> s[i];\n\tint ax,ay,bx,by,Ax,Ay,Bx,By;\t// a -> A\n\trep(i,H) rep(j,W){\n\t\tif(s[i][j] == 'S') ax=i,ay=j;\n\t\tif(s[i][j] == 's') Ax=i,Ay=j;\n\t\tif(s[i][j] == 'K') bx=i,by=j;\n\t\tif(s[i][j] == 'k') Bx=i,By=j;\n\t}\n\tif(ax >= Ax || bx >= Bx) return -1;\n\n\trep(i,H){\n\t\trep(x,W) rep(y,W){\n\t\t\tcan[i][x][y] = true;\n\t\t\tfor(int j=min(x,y);j<=max(x,y);j++) if(s[i][j] == '#') can[i][x][y] = false;\n\t\t}\n\t}\n\n\trep(i,H+1) rep(a,W+1) rep(b,W+1) rep(x,2) rep(y,2) dp[i][a][b][x][y] = inf;\n\tif(ax > bx){\n\t\tswap(ax,bx);swap(ay,by);swap(Ax,Bx);swap(Ay,By);\n\t}\n\tif(ax < bx){\n\t\tdp[ax][ay][W][0][0] = 0;\n\t}else{\n\t\tdp[ax][ay][by][0][0] = 0;\n\t}\n\t#define UPDATE \\\n\t\t/*shows(h+1,naw,nbw,nsa,nsb,dp[h][aw][bw][sa][sb] + cost);*/ \\\n\t\tchmin(dp[h+1][naw][nbw][nsa][nsb], dp[h][aw][bw][sa][sb] + cost);\n\n\tfor(int h=ax;h<H;h++) rep(aw,W+1) rep(bw,W+1) rep(sa,2) rep(sb,2) if(dp[h][aw][bw][sa][sb] != inf){\n\t\tbool aend = (h == Ax);\n\t\tbool bend = (h == Bx);\n\t\tif(aw != W && bw != W){\n\t\t\trep(Aw,W) rep(Bw,W) if(can[h][aw][Aw] && can[h][bw][Bw]){\n\t\t\t\tif(aend && !(aw == Aw && aw == Ay)) continue;\n\t\t\t\tif(bend && !(bw == Bw && bw == By)) continue;\n\n\t\t\t\tint cost = abs(aw-Aw) + (aend?0:1) + abs(bw-Bw) + (bend?0:1);\t\t// red + blue\n\t\t\t\tV<int> edge(W-1);\n\t\t\t\tfor(int w=min(aw,Aw);w<max(aw,Aw);w++) edge[w] |= 1;\n\t\t\t\tfor(int w=min(bw,Bw);w<max(bw,Bw);w++) edge[w] |= 2;\n\t\t\t\tint lime = 0; rep(w,W-1) if(edge[w] == 3) lime++;\n\t\t\t\tif(!aend && !bend && Aw == Bw) lime++;\n\t\t\t\tcost -= lime;\t\t//\t- lime\n\t\t\t\tV<int> deg(W);\n\t\t\t\trep(w,W){\n\t\t\t\t\tif(w != 0 && edge[w-1]) deg[w]++;\t// left\n\t\t\t\t\tif(w != W-1 && edge[w]) deg[w]++;\t// right\n\t\t\t\t\tif((w == aw && h != ax) or (w == bw && h != bx)) deg[w]++;\t// up\n\t\t\t\t\tif((w == Aw && !aend) or (w == Bw && !bend)) deg[w]++;\t// down\n\t\t\t\t\tif((w == Aw && aend) or (w == Bw && bend)) deg[w]++;\t// terminal +1 cost\n\t\t\t\t}\n\t\t\t\tint green = 0; rep(w,W) green += max(deg[w]-2,0);\n\t\t\t\tcost += green;\n\n\t\t\t\tint naw = (aend ? W : Aw), nbw = (bend ? W : Bw);\n\t\t\t\tint nsa = sa | !in(aw,bw,Bw);\n\t\t\t\tint nsb = sb | !in(bw,aw,Aw);\n\t\t\t\tif(h == ax && h == bx && nsa == 0 && nsb == 0){\n\t\t\t\t\t// first raw dead\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tUPDATE\n\t\t\t}\n\t\t}else if(aw != W){\n\t\t\trep(Aw,W) if(can[h][aw][Aw]){\n\t\t\t\tif(aend && !(aw == Aw && aw == Ay)) continue;\n\n\t\t\t\tint cost = abs(aw-Aw) + (aend?0:1);\t\t// red\n\n\t\t\t\tint naw = (aend ? W : Aw);\n\t\t\t\tint nbw = (h+1 == bx ? by : W);\n\t\t\t\tint nsa = 1;\n\t\t\t\tint nsb = sb;\n\t\t\t\tUPDATE\n\t\t\t}\n\t\t}else if(bw != W){\n\t\t\trep(Bw,W) if(can[h][bw][Bw]){\n\t\t\t\tif(bend && !(bw == Bw && bw == By)) continue;\n\n\t\t\t\tint cost = abs(bw-Bw) + (bend?0:1);\t\t// red\n\n\t\t\t\tint nbw = (bend ? W : Bw);\n\t\t\t\tint naw = (h+1 == ax ? ay : W);\n\t\t\t\tint nsb = 1;\n\t\t\t\tint nsa = sa;\n\t\t\t\tUPDATE\n\t\t\t}\n\t\t}else{\n\t\t\tint naw = (h+1 == ax ? ay : W);\n\t\t\tint nbw = (h+1 == bx ? by : W);\n\t\t\tint nsa = sa, nsb = sb; int cost = 0;\n\t\t\tUPDATE\n\t\t}\n\t}\n\tint res = dp[H][W][W][1][1];\n\tif(res == inf) return -1;\n\treturn res;\n}\n\nint main(){\n\tcin.tie(0);\n\tios::sync_with_stdio(false);\t\t//DON'T USE scanf/printf/puts !!\n\tcout << fixed << setprecision(20);\n\n\tint ans = solve();\n\tif(ans == -1) cout << \"CONTRACT\" << endl;\n\telse cout << ans << endl;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3464, "score_of_the_acc": -1, "final_rank": 3 }, { "submission_id": "aoj_2319_3443155", "code_snippet": "#include<iostream>\n#include<vector>\n#include<queue>\nusing namespace std;\nint H,W;\nint M = 10000;\nint s,S,k,K;\nvector<int> dist;\n\n\n\n\nvoid bfs_s2(vector<char> g,int dis,bool sfound){\n queue<int> que;\n for(int i=0;i<H*W;i++){\n dist[i] = 10000;\n }\n que.push(S);\n dist[S] = dis;\n while(!que.empty()){\n int p = que.front();\n //cout << \"p:\" << p << endl;\n que.pop();\n //cout << \"next_p:\" << que.front() << endl;\n if(p==s-W){\n int d = dist[p]+1;\n if(p%W!=0 and g[s-1]=='a')d++;\n if(p%W!=W-1 and g[s+1]=='a')d++;\n if(p/W<H and g[s+W]=='a')d++;\n if(d<M){\n M=d;\n }\n continue;\n }\n vector<int> next;\n if(p%W!=0)next.push_back(p-1);\n if(p%W!=W-1)next.push_back(p+1);\n if(p/W<s/W-1)next.push_back(p+W);\n for(int i=0;i<next.size();i++){\n int d = dist[p]+1;\n switch(g[next[i]]){\n case('.'):\n if(dist[next[i]] > d){\n dist[next[i]] = d;\n que.push(next[i]);\n }\n break;\n case('a'):\n d++;\n case('K'):\n d++;\n //cout << sfound <<endl;\n if(sfound==1){\n if(d<M)M=d;\n return;\n }\n for(int j=0;j<H*W;j++){\n if(g[j]=='a' and dist[j]>d){\n dist[j] = d;\n que.push(j);\n }\n if(g[j]=='K' and dist[j]>d and g[next[i]]!='K'){\n dist[j] = d-1;\n que.push(j);\n }\n if(g[j]=='K' and dist[j]>d and g[next[i]]=='K'){\n dist[j] = d;\n que.push(j);\n }\n }\n break;\n }\n }\n }\n}\n\nvoid dfs_s2(int p,vector<char> g,int dis,bool out){\n if(p/W >= s/W or M<=dis)return;\n if(g[p]=='a' or g[p]=='S'){\n g[p] = 'b';\n if(out==1)M = dis;\n if(out==0){\n if(p%W!=0)dfs_s2(p-1,g,dis,0);\n if(p%W!=W-1)dfs_s2(p+1,g,dis,0);\n if(p/W<s/W-1)dfs_s2(p+W,g,dis,0);\n }\n }\n if(g[p]=='.'){\n g[p] = 'b';\n if(p%W!=0)dfs_s2(p-1,g,dis+1,1);\n if(p%W!=W-1)dfs_s2(p+1,g,dis+1,1);\n if(p/W<s/W-1)dfs_s2(p+W,g,dis+1,1);\n }\n}\n/*\nvoid bfs_k1(vector<char> g){\n int d=0;\n queue<int> Q;\n Q.push(K);\n Q.push(-1);\n while(!Q.empty()){\n int p=Q.front();\n Q.pop();\n if(p==-1)d++;\n vector<int> next;\n if(p%W!=0)next.push_back(p-1);\n if(p%W!=W-1)next.push_back(p+1);\n if(p/W<k/W-1)next.push_back(p+W);\n for(int i=0;i<next.size();i++){\n if(g[next[i]]=='.')Q.push(next[i]);\n if(next[i]==k-W){\n limdis=d+8;\n break;\n }\n }\n }\n}\n*/\n\nvoid dfs_k1(int p,vector<char> g,int dis,bool sfound,bool Sfound){\n //if(dis>limdis)return;\n if(p!=K)g[p]='a';\n if(g[p+W]=='k'){\n dis++;\n for(int i=0;i<H*W;i++){\n if(g[i]=='l')g[i]='.';\n }\n if(Sfound==0){\n bfs_s2(g,dis,sfound);\n //cout << sfound <<Sfound <<endl;\n }\n else if(sfound==1)dfs_s2(S,g,dis+4,0);\n return;\n }\n dis++;\n if(p/W<k/W-1){\n switch(g[p+W]){\n case('S'):\n dfs_k1(p+W,g,dis,sfound,1);\n break;\n case('s'):\n sfound = 1;\n case('.'):\n dfs_k1(p+W,g,dis,sfound,Sfound);\n break;\n }\n }\n vector<int> next;\n if(p%W!=0)next.push_back(p-1);\n if(p%W!=W-1)next.push_back(p+1);\n int P=p+W;\n while(P<H*W){\n if(g[P]=='.')g[P]='l';\n else break;\n P += W;\n }\n for(int i=0;i<next.size();i++){\n switch(g[next[i]]){\n case('S'):\n dfs_k1(next[i],g,dis,sfound,1);\n break;\n case('s'):\n sfound = 1;\n case('.'):\n dfs_k1(next[i],g,dis,sfound,Sfound);\n break;\n }\n }\n}\n\n\n\n\nint main(){\n cin >> H >> W;\n vector<char> g;\n g.resize(H*W);\n dist.resize(H*W);\n for(int i=0;i<H*W;i++){\n cin >> g[i];\n if(g[i]=='k')k=i;\n if(g[i]=='K')K=i;\n if(g[i]=='s')s=i;\n if(g[i]=='S')S=i;\n }\n dfs_k1(K,g,0,0,0);\n\n g[k]='s';\n g[s]='k';\n g[S]='K';\n g[K]='S';\n int koukan;\n koukan = s;\n s=k;\n k=koukan;\n koukan=S;\n S=K;\n K=koukan;\n\n //cout << \"---------------------------\"<<endl;\n dfs_k1(K,g,0,0,0);\n\n\n if(M==10000)cout << \"CONTRACT\"<<endl;\n else cout << M <<endl;\n}", "accuracy": 1, "time_ms": 850, "memory_kb": 3172, "score_of_the_acc": -1.9038, "final_rank": 4 }, { "submission_id": "aoj_2319_3443152", "code_snippet": "#include<iostream>\n#include<vector>\n#include<queue>\nusing namespace std;\nint H,W;\nint M = 10000;\nint s,S,k,K;\nvector<int> dist;\n\n\n\n\nvoid bfs_s2(vector<char> g,int dis,bool sfound){\n queue<int> que;\n for(int i=0;i<H*W;i++){\n dist[i] = 10000;\n }\n que.push(S);\n dist[S] = dis;\n while(!que.empty()){\n int p = que.front();\n //cout << \"p:\" << p << endl;\n que.pop();\n //cout << \"next_p:\" << que.front() << endl;\n if(p==s-W){\n int d = dist[p]+1;\n if(p%W!=0 and g[s-1]=='a')d++;\n if(p%W!=W-1 and g[s+1]=='a')d++;\n if(p/W<H and g[s+W]=='a')d++;\n if(d<M){\n M=d;\n }\n continue;\n }\n vector<int> next;\n if(p%W!=0)next.push_back(p-1);\n if(p%W!=W-1)next.push_back(p+1);\n if(p/W<s/W-1)next.push_back(p+W);\n for(int i=0;i<next.size();i++){\n int d = dist[p]+1;\n switch(g[next[i]]){\n case('.'):\n if(dist[next[i]] > d){\n dist[next[i]] = d;\n que.push(next[i]);\n }\n break;\n case('a'):\n d++;\n case('K'):\n d++;\n //cout << sfound <<endl;\n if(sfound==1){\n if(d<M)M=d;\n return;\n }\n for(int j=0;j<H*W;j++){\n if(g[j]=='a' and dist[j]>d){\n dist[j] = d;\n que.push(j);\n }\n if(g[j]=='K' and dist[j]>d){\n dist[j] = d-1;\n que.push(j);\n }\n }\n break;\n }\n }\n }\n}\n\nvoid dfs_s2(int p,vector<char> g,int dis,bool out){\n if(p/W >= s/W or M<=dis)return;\n if(g[p]=='a' or g[p]=='S'){\n g[p] = 'b';\n if(out==1)M = dis;\n if(out==0){\n if(p%W!=0)dfs_s2(p-1,g,dis,0);\n if(p%W!=W-1)dfs_s2(p+1,g,dis,0);\n if(p/W<s/W-1)dfs_s2(p+W,g,dis,0);\n }\n }\n if(g[p]=='.'){\n g[p] = 'b';\n if(p%W!=0)dfs_s2(p-1,g,dis+1,1);\n if(p%W!=W-1)dfs_s2(p+1,g,dis+1,1);\n if(p/W<s/W-1)dfs_s2(p+W,g,dis+1,1);\n }\n}\n/*\nvoid bfs_k1(vector<char> g){\n int d=0;\n queue<int> Q;\n Q.push(K);\n Q.push(-1);\n while(!Q.empty()){\n int p=Q.front();\n Q.pop();\n if(p==-1)d++;\n vector<int> next;\n if(p%W!=0)next.push_back(p-1);\n if(p%W!=W-1)next.push_back(p+1);\n if(p/W<k/W-1)next.push_back(p+W);\n for(int i=0;i<next.size();i++){\n if(g[next[i]]=='.')Q.push(next[i]);\n if(next[i]==k-W){\n limdis=d+8;\n break;\n }\n }\n }\n}\n*/\n\nvoid dfs_k1(int p,vector<char> g,int dis,bool sfound,bool Sfound){\n //if(dis>limdis)return;\n if(p!=K)g[p]='a';\n if(g[p+W]=='k'){\n dis++;\n for(int i=0;i<H*W;i++){\n if(g[i]=='l')g[i]='.';\n }\n if(Sfound==0){\n bfs_s2(g,dis,sfound);\n //cout << sfound <<Sfound <<endl;\n }\n else if(sfound==1)dfs_s2(S,g,dis+4,0);\n return;\n }\n dis++;\n if(p/W<k/W-1){\n switch(g[p+W]){\n case('S'):\n dfs_k1(p+W,g,dis,sfound,1);\n break;\n case('s'):\n sfound = 1;\n case('.'):\n dfs_k1(p+W,g,dis,sfound,Sfound);\n break;\n }\n }\n vector<int> next;\n if(p%W!=0)next.push_back(p-1);\n if(p%W!=W-1)next.push_back(p+1);\n int P=p+W;\n while(P<H*W){\n if(g[P]=='.')g[P]='l';\n else break;\n P += W;\n }\n for(int i=0;i<next.size();i++){\n switch(g[next[i]]){\n case('S'):\n dfs_k1(next[i],g,dis,sfound,1);\n break;\n case('s'):\n sfound = 1;\n case('.'):\n dfs_k1(next[i],g,dis,sfound,Sfound);\n break;\n }\n }\n}\n\n\n\n\nint main(){\n cin >> H >> W;\n vector<char> g;\n g.resize(H*W);\n dist.resize(H*W);\n for(int i=0;i<H*W;i++){\n cin >> g[i];\n if(g[i]=='k')k=i;\n if(g[i]=='K')K=i;\n if(g[i]=='s')s=i;\n if(g[i]=='S')S=i;\n }\n dfs_k1(K,g,0,0,0);\n\n g[k]='s';\n g[s]='k';\n g[S]='K';\n g[K]='S';\n int koukan;\n koukan = s;\n s=k;\n k=koukan;\n koukan=S;\n S=K;\n K=koukan;\n\n //cout << \"---------------------------\"<<endl;\n dfs_k1(K,g,0,0,0);\n\n\n if(M==10000)cout << \"CONTRACT\"<<endl;\n else cout << M <<endl;\n}", "accuracy": 0.8269230769230769, "time_ms": 860, "memory_kb": 3176, "score_of_the_acc": -1.9169, "final_rank": 5 }, { "submission_id": "aoj_2319_3443144", "code_snippet": "#include<iostream>\n#include<vector>\n#include<queue>\nusing namespace std;\nint H,W;\nint M = 10000;\nint s,S,k,K;\nvector<int> dist;\n\n\n\n\nvoid bfs_s2(vector<char> g,int dis,bool sfound){\n queue<int> que;\n for(int i=0;i<H*W;i++){\n dist[i] = 10000;\n }\n que.push(S);\n dist[S] = dis;\n while(!que.empty()){\n int p = que.front();\n //cout << \"p:\" << p << endl;\n que.pop();\n //cout << \"next_p:\" << que.front() << endl;\n if(p==s-W){\n int d = dist[p]+1;\n if(p%W!=0 and g[s-1]=='a')d++;\n if(p%W!=W-1 and g[s+1]=='a')d++;\n if(p/W<H and g[s+W]=='a')d++;\n if(d<M){\n M=d;\n }\n continue;\n }\n vector<int> next;\n if(p%W!=0)next.push_back(p-1);\n if(p%W!=W-1)next.push_back(p+1);\n if(p/W<s/W-1)next.push_back(p+W);\n for(int i=0;i<next.size();i++){\n int d = dist[p]+1;\n switch(g[next[i]]){\n case('.'):\n if(dist[next[i]] > d){\n dist[next[i]] = d;\n que.push(next[i]);\n }\n break;\n case('a'):\n d++;\n case('K'):\n d++;\n //cout << sfound <<endl;\n if(sfound==1){\n if(d<M)M=d;\n return;\n }\n for(int j=0;j<H*W;j++){\n if(g[j]=='a' and dist[j]>d){\n dist[j] = d;\n que.push(j);\n }\n if(g[j]=='K' and dist[j]>d){\n dist[j] = d;\n que.push(j);\n }\n }\n break;\n }\n }\n }\n}\n\nvoid dfs_s2(int p,vector<char> g,int dis,bool out){\n if(p/W >= s/W or M<=dis)return;\n if(g[p]=='a' or g[p]=='S'){\n g[p] = 'b';\n if(out==1)M = dis;\n if(out==0){\n if(p%W!=0)dfs_s2(p-1,g,dis,0);\n if(p%W!=W-1)dfs_s2(p+1,g,dis,0);\n if(p/W<s/W-1)dfs_s2(p+W,g,dis,0);\n }\n }\n if(g[p]=='.'){\n g[p] = 'b';\n if(p%W!=0)dfs_s2(p-1,g,dis+1,1);\n if(p%W!=W-1)dfs_s2(p+1,g,dis+1,1);\n if(p/W<s/W-1)dfs_s2(p+W,g,dis+1,1);\n }\n}\n/*\nvoid bfs_k1(vector<char> g){\n int d=0;\n queue<int> Q;\n Q.push(K);\n Q.push(-1);\n while(!Q.empty()){\n int p=Q.front();\n Q.pop();\n if(p==-1)d++;\n vector<int> next;\n if(p%W!=0)next.push_back(p-1);\n if(p%W!=W-1)next.push_back(p+1);\n if(p/W<k/W-1)next.push_back(p+W);\n for(int i=0;i<next.size();i++){\n if(g[next[i]]=='.')Q.push(next[i]);\n if(next[i]==k-W){\n limdis=d+8;\n break;\n }\n }\n }\n}\n*/\n\nvoid dfs_k1(int p,vector<char> g,int dis,bool sfound,bool Sfound){\n //if(dis>limdis)return;\n if(p!=K)g[p]='a';\n if(g[p+W]=='k'){\n dis++;\n for(int i=0;i<H*W;i++){\n if(g[i]=='l')g[i]='.';\n }\n if(Sfound==0){\n bfs_s2(g,dis,sfound);\n //cout << sfound <<Sfound <<endl;\n }\n else if(sfound==1)dfs_s2(S,g,dis+4,0);\n return;\n }\n dis++;\n if(p/W<k/W-1){\n switch(g[p+W]){\n case('S'):\n dfs_k1(p+W,g,dis,sfound,1);\n break;\n case('s'):\n sfound = 1;\n case('.'):\n dfs_k1(p+W,g,dis,sfound,Sfound);\n break;\n }\n }\n vector<int> next;\n if(p%W!=0)next.push_back(p-1);\n if(p%W!=W-1)next.push_back(p+1);\n int P=p+W;\n while(P<H*W){\n if(g[P]=='.')g[P]='l';\n else break;\n P += W;\n }\n for(int i=0;i<next.size();i++){\n switch(g[next[i]]){\n case('S'):\n dfs_k1(next[i],g,dis,sfound,1);\n break;\n case('s'):\n sfound = 1;\n case('.'):\n dfs_k1(next[i],g,dis,sfound,Sfound);\n break;\n }\n }\n}\n\n\n\n\nint main(){\n cin >> H >> W;\n vector<char> g;\n g.resize(H*W);\n dist.resize(H*W);\n for(int i=0;i<H*W;i++){\n cin >> g[i];\n if(g[i]=='k')k=i;\n if(g[i]=='K')K=i;\n if(g[i]=='s')s=i;\n if(g[i]=='S')S=i;\n }\n dfs_k1(K,g,0,0,0);\n\n g[k]='s';\n g[s]='k';\n g[S]='K';\n g[K]='S';\n int koukan;\n koukan = s;\n s=k;\n k=koukan;\n koukan=S;\n S=K;\n K=koukan;\n\n //cout << \"---------------------------\"<<endl;\n dfs_k1(K,g,0,0,0);\n\n\n if(M==10000)cout << \"CONTRACT\"<<endl;\n else cout << M <<endl;\n}", "accuracy": 0.5576923076923077, "time_ms": 860, "memory_kb": 3164, "score_of_the_acc": -1.9134, "final_rank": 9 }, { "submission_id": "aoj_2319_3397149", "code_snippet": "#include<iostream>\n#include<vector>\n#include<queue>\nusing namespace std;\nint H,W;\nint M = 10000;\nint s,S,k,K;\nqueue<int> que;\nvector<int> dist;\n\n\n\n\nvoid bfs_s2(vector<char> g,int dis,bool sfound){\n for(int i=0;i<H*W;i++){\n dist[i] = 10000;\n }\n que.push(S);\n dist[S] = dis;\n while(!que.empty()){\n int p = que.front();\n que.pop();\n if(g[p+W]=='s'){\n if(dist[p]+1<M){\n M=dist[p]+1;\n }\n continue;\n }\n vector<int> next;\n if(p%W!=0)next.push_back(p-1);\n if(p%W!=W-1)next.push_back(p+1);\n if(p/W<s/W-1)next.push_back(p+W);\n for(int i=0;i<next.size();i++){\n int d = dist[p]+1;\n switch(g[next[i]]){\n case('.'):\n if(dist[next[i]] > d){\n dist[next[i]] = d;\n que.push(next[i]);\n }\n break;\n case('a'):\n d++;\n case('K'):\n d++;\n if(sfound==1){\n if(d<M)M=d;\n return;\n }\n for(int j=0;j<H*W;j++){\n if(g[j]=='a' and dist[j]>d){\n dist[j] = d;\n que.push(j);\n }\n if(g[j]=='K' and dist[j]>d){\n dist[j] = d;\n que.push(j);\n }\n }\n break;\n }\n }\n }\n /*\n for(int i=0;i<H;i++){\n for(int j=0;j<W;j++){\n cout << dist[i*W+j] << \" \";\n }\n cout << endl;\n }\n */\n}\n\nvoid dfs_s2(int p,vector<char> g,int dis,bool out){\n if(p/W >= s/W or M<=dis)return;\n if(g[p]=='a' or g[p]=='S'){\n g[p] = 'b';\n if(out==1)M = dis;\n if(out==0){\n if(p%W!=0)dfs_s2(p-1,g,dis,0);\n if(p%W!=W-1)dfs_s2(p+1,g,dis,0);\n if(p/W<s/W-1)dfs_s2(p+W,g,dis,0);\n }\n }\n if(g[p]=='.'){\n g[p] = 'b';\n if(p%W!=0)dfs_s2(p-1,g,dis+1,1);\n if(p%W!=W-1)dfs_s2(p+1,g,dis+1,1);\n if(p/W<s/W-1)dfs_s2(p+W,g,dis+1,1);\n }\n}\n\n\n\nvoid dfs_k1(int p,vector<char> g,int dis,bool sfound,bool Sfound){\n if(p!=K)g[p]='a';\n if(g[p+W]=='k'){\n dis++;\n if(Sfound==0)bfs_s2(g,dis,sfound);\n else if(sfound==1)dfs_s2(S,g,dis+4,0);\n return;\n }\n vector<int> next;\n if(p%W!=0)next.push_back(p-1);\n if(p%W!=W-1)next.push_back(p+1);\n if(p/W<k/W-1)next.push_back(p+W);\n dis++;\n for(int i=0;i<next.size();i++){\n switch(g[next[i]]){\n case('S'):\n dfs_k1(next[i],g,dis,sfound,1);\n break;\n case('s'):\n sfound = 1;\n case('.'):\n dfs_k1(next[i],g,dis,sfound,Sfound);\n break;\n }\n }\n}\n\n\n\nint main(){\n cin >> H >> W;\n vector<char> g;\n g.resize(H*W);\n dist.resize(H*W);\n for(int i=0;i<H*W;i++){\n cin >> g[i];\n if(g[i]=='k')k=i;\n if(g[i]=='K')K=i;\n if(g[i]=='s')s=i;\n if(g[i]=='S')S=i;\n }\n\n dfs_k1(K,g,0,0,0);\n\n g[k]='s';\n g[s]='k';\n g[S]='K';\n g[K]='S';\n int koukan;\n koukan = s;\n s=k;\n k=koukan;\n koukan=S;\n S=K;\n K=koukan;\n\n\n dfs_k1(K,g,0,0,0);\n\n\n if(M==10000)cout << \"CONTRACT\"<<endl;\n else cout << M <<endl;\n}", "accuracy": 0.4423076923076923, "time_ms": 90, "memory_kb": 3172, "score_of_the_acc": -0.999, "final_rank": 10 }, { "submission_id": "aoj_2319_3397130", "code_snippet": "#include<iostream>\n#include<vector>\n#include<queue>\nusing namespace std;\nint H,W;\nint M = 10000;\nint s,S,k,K;\nqueue<int> que;\nvector<int> dist;\n\n\n\n\nvoid bfs_s2(vector<char> g,int dis,bool sfound){\n for(int i=0;i<H*W;i++){\n dist[i] = 10000;\n }\n que.push(S);\n dist[S] = dis;\n while(!que.empty()){\n int p = que.front();\n que.pop();\n if(g[p+W]=='s'){\n if(dist[p]+1<M){\n M=dist[p]+1;\n }\n continue;\n }\n vector<int> next;\n if(p%W!=0)next.push_back(p-1);\n if(p%W!=W-1)next.push_back(p+1);\n if(p/W<k/W-1)next.push_back(p+W);\n for(int i=0;i<next.size();i++){\n int d = dist[p]+1;\n switch(g[next[i]]){\n case('.'):\n if(dist[next[i]] > d){\n dist[next[i]] = d;\n que.push(next[i]);\n }\n break;\n case('a'):\n d++;\n case('K'):\n d++;\n if(sfound==1){\n if(d<M)M=d;\n return;\n }\n for(int j=0;j<H*W;j++){\n if(g[j]=='a' and dist[j]>d){\n dist[j] = d;\n que.push(j);\n }\n if(g[j]=='K' and dist[j]>d){\n dist[j] = d;\n que.push(j);\n }\n }\n break;\n }\n }\n }\n}\n\nvoid dfs_s2(int p,vector<char> g,int dis,bool out){\n if(p/W >= s/W or M<=dis)return;\n if(g[p]=='a' or g[p]=='S'){\n g[p] = 'b';\n if(out==1)M = dis;\n if(out==0){\n if(p%W!=0)dfs_s2(p-1,g,dis,0);\n if(p%W!=W-1)dfs_s2(p+1,g,dis,0);\n if(p/W<s/W-1)dfs_s2(p+W,g,dis,0);\n }\n }\n if(g[p]=='.'){\n g[p] = 'b';\n if(p%W!=0)dfs_s2(p-1,g,dis+1,1);\n if(p%W!=W-1)dfs_s2(p+1,g,dis+1,1);\n if(p/W<s/W-1)dfs_s2(p+W,g,dis+1,1);\n }\n}\n\n\n\nvoid dfs_k1(int p,vector<char> g,int dis,bool sfound,bool Sfound){\n if(p!=K)g[p]='a';\n if(g[p+W]=='k'){\n dis++;\n if(Sfound==0)bfs_s2(g,dis,sfound);\n else if(sfound==1)dfs_s2(S,g,dis+4,0);\n return;\n }\n vector<int> next;\n if(p%W!=0)next.push_back(p-1);\n if(p%W!=W-1)next.push_back(p+1);\n if(p/W<k/W-1)next.push_back(p+W);\n dis++;\n for(int i=0;i<next.size();i++){\n switch(g[next[i]]){\n case('S'):\n dfs_k1(next[i],g,dis,sfound,1);\n break;\n case('s'):\n sfound = 1;\n case('.'):\n dfs_k1(next[i],g,dis,sfound,Sfound);\n break;\n }\n }\n}\n\n\n\nint main(){\n cin >> H >> W;\n vector<char> g;\n g.resize(H*W);\n dist.resize(H*W);\n for(int i=0;i<H*W;i++){\n cin >> g[i];\n if(g[i]=='k')k=i;\n if(g[i]=='K')K=i;\n if(g[i]=='s')s=i;\n if(g[i]=='S')S=i;\n }\n\n dfs_k1(K,g,0,0,0);\n\n g[k]='s';\n g[s]='k';\n g[S]='K';\n g[K]='S';\n int koukan;\n koukan = s;\n s=k;\n k=koukan;\n koukan=S;\n S=K;\n K=koukan;\n\n\n dfs_k1(K,g,0,0,0);\n\n\n if(M==10000)cout << \"CONTRACT\"<<endl;\n else cout << M <<endl;\n}", "accuracy": 0.38461538461538464, "time_ms": 90, "memory_kb": 3172, "score_of_the_acc": -0.999, "final_rank": 11 }, { "submission_id": "aoj_2319_3393745", "code_snippet": "#include<iostream>\n#include<vector>\n#include<queue>\nusing namespace std;\nint H,W;\nint M = 10000;\nint s,S,k,K;\nqueue<int> que;\nvector<int> dist;\n\n\n\n\nvoid bfs_s2(vector<char> g,int dis,bool sfound){\n for(int i=0;i<H*W;i++){\n dist[i] = 10000;\n }\n que.push(S);\n dist[S] = dis;\n while(!que.empty()){\n int p = que.front();\n que.pop();\n if(g[p+W]=='s'){\n if(dist[p]+1<M){\n M=dist[p]+1;\n }\n continue;\n }\n vector<int> next;\n if(p%W!=0)next.push_back(p-1);\n if(p%W!=W-1)next.push_back(p+1);\n if(p/W<k/W-1)next.push_back(p+W);\n for(int i=0;i<next.size();i++){\n int d = dist[p]+1;\n switch(g[next[i]]){\n case('.'):\n if(dist[next[i]] > d){\n dist[next[i]] = d;\n que.push(next[i]);\n }\n break;\n case('a'):\n d++;\n case('K'):\n d++;\n if(sfound==1){\n if(d<M)M=d;\n return;\n }\n for(int j=0;j<H*W;j++){\n if(g[j]=='a' and dist[j]>d){\n dist[j] = d;\n que.push(j);\n }\n if(g[j]=='K' and dist[j]>d){\n dist[j] = d;\n que.push(j);\n }\n }\n break;\n }\n }\n }\n}\n\nvoid dfs_s2(int p,vector<char> g,int dis,bool out){\n if(p/W >= s/W or M<=dis)return;\n if(g[p]=='a' or g[p]=='S'){\n g[p] = 'b';\n if(out==1)M = dis;\n if(out==0){\n if(p%W!=0)dfs_s2(p-1,g,dis,0);\n if(p%W!=W-1)dfs_s2(p+1,g,dis,0);\n if(p/W<s/W-1)dfs_s2(p+W,g,dis,0);\n }\n }\n if(g[p]=='.'){\n g[p] = 'b';\n if(p%W!=0)dfs_s2(p-1,g,dis+1,1);\n if(p%W!=W-1)dfs_s2(p+1,g,dis+1,1);\n if(p/W<s/W-1)dfs_s2(p+W,g,dis+1,1);\n }\n}\n\n\n\nvoid dfs_k1(int p,vector<char> g,int dis,bool sfound,bool Sfound){\n if(p!=K)g[p]='a';\n if(g[p+W]=='k'){\n dis++;\n if(Sfound==0)bfs_s2(g,dis,sfound);\n else if(sfound==1){\n dfs_s2(S,g,dis+4,0);\n }\n return;\n }\n vector<int> next;\n if(p%W!=0)next.push_back(p-1);\n if(p%W!=W-1)next.push_back(p+1);\n if(p/W<k/W-1)next.push_back(p+W);\n dis++;\n for(int i=0;i<next.size();i++){\n switch(g[next[i]]){\n case('S'):\n Sfound = 1;\n dfs_k1(next[i],g,dis,sfound,Sfound);\n break;\n case('s'):\n sfound = 1;\n case('.'):\n dfs_k1(next[i],g,dis,sfound,Sfound);\n break;\n }\n }\n}\n\n\n\nint main(){\n cin >> H >> W;\n vector<char> g;\n g.resize(H*W);\n dist.resize(H*W);\n for(int i=0;i<H*W;i++){\n cin >> g[i];\n if(g[i]=='k')k=i;\n if(g[i]=='K')K=i;\n if(g[i]=='s')s=i;\n if(g[i]=='S')S=i;\n }\n\n dfs_k1(K,g,0,0,0);\n\n g[k]='s';\n g[s]='k';\n g[S]='K';\n g[K]='S';\n int koukan;\n koukan = s;\n s=k;\n k=koukan;\n koukan=S;\n S=K;\n K=koukan;\n\n\n dfs_k1(K,g,0,0,0);\n\n\n if(M==10000)cout << \"CONTRACT\"<<endl;\n else cout << M <<endl;\n}", "accuracy": 0.3076923076923077, "time_ms": 70, "memory_kb": 3172, "score_of_the_acc": -0.9752, "final_rank": 12 }, { "submission_id": "aoj_2319_1173977", "code_snippet": "#include<stdio.h>\n#include<algorithm>\nusing namespace std;\nchar str[12][12];\nint dp[12][12][12][12][4];\nint H,W;\nint sr,sc,kr,kc,SR,SC,KR,KC;\ninline int ABS(int a){return max(a,-a);}\ninline void upd(int a,int b,int c,int d,int e){\n\tif(a>=sr){\n\t\tif(a>=kr)return;\n\t\tif(a+1<KR)dp[a+1][11][11][b][e|2]=min(dp[a+1][11][11][b][e|2],dp[a][b][c][d][e]);\n\t\telse if(a+1==KR)dp[a+1][11][KC][b][e|2]=min(dp[a+1][11][KC][b][e|2],dp[a][b][c][d][e]);\n\t\telse{\n\t\t\tfor(int y=0;y<W;y++){\n\t\t\t\tbool d2=false;\n\t\t\t\tfor(int z=min(y,c);z<=max(y,c);z++)if(str[a][z]=='#')d2=true;\n\t\t\t\tif(str[a+1][y]=='#')d2=true;\n\t\t\t\tif(a+1==kr&&kc!=y)d2=true;\n\t\t\t\tif(d2)continue;\n\t\t\t\tint x=b;\n\t\t\t\tint ks=0;\n\t\t\t\tint vt=0;\n\t\t\t\tfor(int z=min(y,c);z<max(y,c);z++){\n\t\t\t\t\tbool iL=(min(b,x)<=z)&&(z<=max(b,x));\n\t\t\t\t\tbool iR=(min(b,x)<=z+1)&&(z+1<=max(b,x));\n\t\t\t\t\tif(!iL&&!iR)ks++;\n\t\t\t\t\telse if(!(iL&&iR)){\n\t\t\t\t\t\tif(!vt&&a==SR&&(z==SC||z+1==SC)){vt=1;ks++;}\n\t\t\t\t\t//\telse if(!vt&&a==sr&&(z==sc||z+1==sc)){vt=1;ks++;}\n\t\t\t\t\t\telse ks+=2;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif(d!=11&&a>KR&&a<=sr&&min(b,d)<=c&&c<=max(b,d)&&min(b,x)<=c&&c<=max(b,x)){\n\t\t\t\t\tif(a-1==SR&&c==SC)ks--;\n\t\t\t\t\telse if(a-1==sr&&c==sc)ks--;\n\t\t\t\t\telse ks-=2;\n\t\t\t\t}\n\t\t\t\tif((SR!=a||y!=SC)&&min(b,x)<=y&&y<=max(b,x))ks+=2;\n\t\t\t\telse ks++;\n\t\t\t\tif(d!=11&&a>KR&&a<=sr&&min(b,x)<=c&&c<=max(b,x)&&!(min(b,d)<=c&&c<=max(b,d)))ks++;\n\t\t\t\tint te=e;\n\t\t\t\tif(c<min(b,x)||max(b,x)<c)te|=2;\n\t\t\t\tif(b<min(c,y)||max(c,y)<b)te|=1;\n\t\t\t\tdp[a+1][11][y][b][te]=min(dp[a+1][11][y][b][te],dp[a][b][c][d][e]+ks);\n\t\t\t}\n\t\t}\n\t\treturn;\n\t}\n\tfor(int x=0;x<W;x++){\n\t\tbool d1=false;\n\t\tfor(int z=min(x,b);z<=max(x,b);z++)if(str[a][z]=='#')d1=true;\n\t\tif(str[a+1][x]=='#')d1=true;\n\t\tif(a+1==sr&&sc!=x)d1=true;\n\t\t\n\t\tif(d1)continue;\n\t\tif(a<KR){\n\t\t\tint ks=ABS(x-b)+1;\n\t\t\tif(a+1==KR)dp[a+1][x][KC][b][e|1]=min(dp[a+1][x][KC][b][e|1],dp[a][b][c][d][e]+ks);\n\t\t\telse dp[a+1][x][11][b][e|1]=min(dp[a+1][x][11][b][e|1],dp[a][b][c][d][e]+ks);\n\t\t\tcontinue;\n\t\t}\n\t\tif(a>=kr){\n\t\t\tint ks=ABS(x-b)+1;\n\t\t\tif(a==kr&&min(b,x)<=c&&c<=max(b,x))ks++;\n\t\t\tif(d!=11&&a>KR&&min(b,d)<=c&&c<=max(b,d)&&min(b,x)<=c&&c<=max(b,x)){\n\t\t\t\tif(a-1==SR&&c==SC)ks--;\n\t\t\t\telse if(a-1==sr&&c==sc)ks--;\n\t\t\t\telse ks-=2;\n\t\t\t}\n\t\t\tif(d!=11&&a>KR&&min(b,x)<=c&&c<=max(b,x)&&!(min(b,d)<=c&&c<=max(b,d)))ks++;\n\t\t\tint te=e|1;\n\t\t\tif(a==kr&&(c<min(b,x)||max(b,x)<c))te|=2;\n\t\t\tdp[a+1][x][11][b][te]=min(dp[a+1][x][11][b][te],dp[a][b][c][d][e]+ks);\n\t\t\tcontinue;\n\t\t}\n\t\tfor(int y=0;y<W;y++){\n\t\t\tbool d2=false;\n\t\t\tfor(int z=min(y,c);z<=max(y,c);z++)if(str[a][z]=='#')d2=true;\n\t\t\tif(str[a+1][y]=='#')d2=true;\n\t\t\tif(a+1==kr&&kc!=y)d2=true;\n\t\t\tif(a==SR&&a==KR&&min(b,x)<=c&&c<=max(b,x)&&min(c,y)<=b&&b<=max(c,y))continue;\n\t\t\tif(d2)continue;\n\t\t\tint ks=ABS(x-b)+1;\n\t\t\tint vt=0;\n\t\t\tfor(int z=min(y,c);z<max(y,c);z++){\n\t\t\t\tbool iL=(min(b,x)<=z)&&(z<=max(b,x));\n\t\t\t\tbool iR=(min(b,x)<=z+1)&&(z+1<=max(b,x));\n\t\t\t\tif(!iL&&!iR)ks++;\n\t\t\t\telse if(!(iL&&iR)){\n\t\t\t\t\tif(!vt&&a==SR&&(z==SC||z+1==SC)){vt=1;ks++;}\n\t\t\t//\t\telse if(!vt&&a==sr&&(z==sc||z+1==sc)){vt=1;ks++;}\n\t\t\t\t\telse ks+=2;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(d!=11&&a>KR&&min(b,d)<=c&&c<=max(b,d)&&min(b,x)<=c&&c<=max(b,x)){\n\t\t\t\tif(a-1==SR&&c==SC)ks--;\n\t\t\t\telse if(a-1==sr&&c==sc)ks--;\n\t\t\t\telse ks-=2;\n\t\t\t}\n\t\t\tif((SR!=a||y!=SC)&&min(b,x)<=y&&y<=max(b,x))ks+=2;\n\t\t\telse ks++;\n\t\t\tif(d!=11&&a>KR&&min(b,x)<=c&&c<=max(b,x)&&!(min(b,d)<=c&&c<=max(b,d)))ks++;\n\t\t\tint te=e;\n\t\t\tif(c<min(b,x)||max(b,x)<c)te|=2;\n\t\t\tif(b<min(c,y)||max(c,y)<b)te|=1;\n\t\t//\tif(a==sr)te|=2;\n\t\t//\tif(a==kr)te|=1;\n\t\t//\tprintf(\"%d %d %d %d %d -> %d %d %d %d %d: %d\\n\",a,b,c,d,e,a+1,x,y,b,te,ks);\n\t\t\tdp[a+1][x][y][b][te]=min(dp[a+1][x][y][b][te],dp[a][b][c][d][e]+ks);\n\t\t}\n\t}\n}\nint main(){\n\tint a,b;\n\tscanf(\"%d%d\",&a,&b);\n\tH=a;W=b;\n\tfor(int i=0;i<a;i++)scanf(\"%s\",str[i]);\n\tfor(int i=0;i<12;i++)for(int j=0;j<12;j++)for(int k=0;k<12;k++)for(int l=0;l<12;l++)\n\t\tdp[i][j][k][l][0]=dp[i][j][k][l][1]=dp[i][j][k][l][2]=dp[i][j][k][l][3]=99999999;\n\tfor(int i=0;i<a;i++)for(int j=0;j<b;j++){\n\t\tif(str[i][j]=='s'){sr=i;sc=j;}\n\t\tif(str[i][j]=='k'){kr=i;kc=j;}\n\t\tif(str[i][j]=='S'){SR=i;SC=j;}\n\t\tif(str[i][j]=='K'){KR=i;KC=j;}\n\t}\n\tif(SR>KR){\n\t\tswap(SR,KR);\n\t\tswap(SC,KC);\n\t\tswap(sr,kr);\n\t\tswap(sc,kc);\n\t}\n\tif(SR==KR){\n\t\tdp[SR][SC][KC][11][0]=0;\n\t}else if(SR<KR){\n\t\tdp[SR][SC][11][11][1]=0;\n\t}else{\n\t\tdp[SR][SC][11][11][2]=0;\n\t}\n\tint ret=99999999;\n\tfor(int i=0;i<a;i++){\n\t\tfor(int j=0;j<12;j++){\n\t\t\tfor(int k=0;k<12;k++){\n\t\t\t\tfor(int l=0;l<12;l++){\n\t\t\t\t\tfor(int m=0;m<4;m++){\n\t\t\t\t\t\tif(dp[i][j][k][l][m]>999999)continue;\n\t\t\t\t\t\t//printf(\"%d %d %d %d %d: %d\\n\",i,j,k,l,m,dp[i][j][k][l][m]);\n\t\t\t\t\t\tint t=0;\n\t\t\t\t\t\tif(j==11)t|=2;\n\t\t\t\t\t\tif(k==11)t|=1;\n\t\t\t\t\t\tif(j<W&&k<W&&k!=j)t|=3;\n\t\t\t\t\t\tif(i>=max(sr,kr)&&(m|t)==3)ret=min(ret,dp[i][j][k][l][m]);\n\t\t\t\t\t\telse upd(i,j,k,l,m);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tif(SR>=sr||KR>=kr)ret=999999999;\n\tif(ret>999999)printf(\"CONTRACT\\n\");\n\telse printf(\"%d\\n\",ret);\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 1356, "score_of_the_acc": -0.4153, "final_rank": 1 }, { "submission_id": "aoj_2319_1173958", "code_snippet": "#include<stdio.h>\n#include<algorithm>\nusing namespace std;\nchar str[12][12];\nint dp[12][12][12][12][4];\nint H,W;\nint sr,sc,kr,kc,SR,SC,KR,KC;\ninline int ABS(int a){return max(a,-a);}\ninline void upd(int a,int b,int c,int d,int e){\n\tif(a>=sr){\n\t\tif(a>=kr)return;\n\t\tif(a+1<KR)dp[a+1][11][11][b][e|2]=min(dp[a+1][11][11][b][e|2],dp[a][b][c][d][e]);\n\t\telse if(a+1==KR)dp[a+1][11][KC][b][e|2]=min(dp[a+1][11][KC][b][e|2],dp[a][b][c][d][e]);\n\t\telse{\n\t\t\tfor(int y=0;y<W;y++){\n\t\t\t\tbool d2=false;\n\t\t\t\tfor(int z=min(y,c);z<=max(y,c);z++)if(str[a][z]=='#')d2=true;\n\t\t\t\tif(str[a+1][y]=='#')d2=true;\n\t\t\t\tif(a+1==kr&&kc!=y)d2=true;\n\t\t\t\tif(d2)continue;\n\t\t\t\tint x=b;\n\t\t\t\tint ks=0;\n\t\t\t\tint vt=0;\n\t\t\t\tfor(int z=min(y,c);z<max(y,c);z++){\n\t\t\t\t\tbool iL=(min(b,x)<=z)&&(z<=max(b,x));\n\t\t\t\t\tbool iR=(min(b,x)<=z+1)&&(z+1<=max(b,x));\n\t\t\t\t\tif(!iL&&!iR)ks++;\n\t\t\t\t\telse if(!(iL&&iR)){\n\t\t\t\t\t\tif(!vt&&a==SR&&(z==SC||z+1==SC)){vt=1;ks++;}\n\t\t\t\t\t\telse if(!vt&&a==sr&&(z==sc||z+1==sc)){vt=1;ks++;}\n\t\t\t\t\t\telse ks+=2;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif(d!=11&&a>KR&&min(b,d)<=c&&c<=max(b,d)&&min(b,x)<=c&&c<=max(b,x)){\n\t\t\t\t\tif(a-1==SR&&c==SC)ks--;\n\t\t\t\t\telse if(a-1==sr&&c==sc)ks--;\n\t\t\t\t\telse ks-=2;\n\t\t\t\t}\n\t\t\t\tif((SR!=a||y!=SC)&&min(b,x)<=y&&y<=max(b,x))ks+=2;\n\t\t\t\telse ks++;\n\t\t\t\tif(d!=11&&a>KR&&a<=sr&&min(b,x)<=c&&c<=max(b,x)&&!(min(b,d)<=c&&c<=max(b,d)))ks++;\n\t\t\t\tint te=e;\n\t\t\t\tif(c<min(b,x)||max(b,x)<c)te|=2;\n\t\t\t\tif(b<min(c,y)||max(c,y)<b)te|=1;\n\t\t\t\tdp[a+1][11][y][b][te]=min(dp[a+1][11][y][b][te],dp[a][b][c][d][e]+ks);\n\t\t\t}\n\t\t}\n\t}\n\tfor(int x=0;x<W;x++){\n\t\tbool d1=false;\n\t\tfor(int z=min(x,b);z<=max(x,b);z++)if(str[a][z]=='#')d1=true;\n\t\tif(str[a+1][x]=='#')d1=true;\n\t\tif(a+1==sr&&sc!=x)d1=true;\n\t\t\n\t\tif(d1)continue;\n\t\tif(a<KR){\n\t\t\tint ks=ABS(x-b)+1;\n\t\t\tif(a+1==KR)dp[a+1][x][KC][b][e|1]=min(dp[a+1][x][KC][b][e|1],dp[a][b][c][d][e]+ks);\n\t\t\telse dp[a+1][x][11][b][e|1]=min(dp[a+1][x][11][b][e|1],dp[a][b][c][d][e]+ks);\n\t\t\tcontinue;\n\t\t}\n\t\tif(a>=kr){\n\t\t\tint ks=ABS(x-b)+1;\n\t\t\tif(a==kr&&min(b,x)<=c&&c<=max(b,x))ks++;\n\t\t\tif(d!=11&&a>KR&&min(b,d)<=c&&c<=max(b,d)&&min(b,x)<=c&&c<=max(b,x)){\n\t\t\t\tif(a-1==SR&&c==SC)ks--;\n\t\t\t\telse if(a-1==sr&&c==sc)ks--;\n\t\t\t\telse ks-=2;\n\t\t\t}\n\t\t\tif(d!=11&&a>KR&&min(b,x)<=c&&c<=max(b,x)&&!(min(b,d)<=c&&c<=max(b,d)))ks++;\n\t\t\tint te=e|1;\n\t\t\tif(a==kr&&(c<min(b,x)||max(b,x)<c))te|=2;\n\t\t\tdp[a+1][x][11][b][te]=min(dp[a+1][x][11][b][te],dp[a][b][c][d][e]+ks);\n\t\t\tcontinue;\n\t\t}\n\t\tfor(int y=0;y<W;y++){\n\t\t\tbool d2=false;\n\t\t\tfor(int z=min(y,c);z<=max(y,c);z++)if(str[a][z]=='#')d2=true;\n\t\t\tif(str[a+1][y]=='#')d2=true;\n\t\t\tif(a+1==kr&&kc!=y)d2=true;\n\t\t\tif(a==SR&&a==KR&&min(b,x)<=c&&c<=max(b,x)&&min(c,y)<=b&&b<=max(c,y))continue;\n\t\t\tif(d2)continue;\n\t\t\tint ks=ABS(x-b)+1;\n\t\t\tint vt=0;\n\t\t\tfor(int z=min(y,c);z<max(y,c);z++){\n\t\t\t\tbool iL=(min(b,x)<=z)&&(z<=max(b,x));\n\t\t\t\tbool iR=(min(b,x)<=z+1)&&(z+1<=max(b,x));\n\t\t\t\tif(!iL&&!iR)ks++;\n\t\t\t\telse if(!(iL&&iR)){\n\t\t\t\t\tif(!vt&&a==SR&&(z==SC||z+1==SC)){vt=1;ks++;}\n\t\t\t\t\telse if(!vt&&a==sr&&(z==sc||z+1==sc)){vt=1;ks++;}\n\t\t\t\t\telse ks+=2;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(d!=11&&a>KR&&min(b,d)<=c&&c<=max(b,d)&&min(b,x)<=c&&c<=max(b,x)){\n\t\t\t\tif(a-1==SR&&c==SC)ks--;\n\t\t\t\telse if(a-1==sr&&c==sc)ks--;\n\t\t\t\telse ks-=2;\n\t\t\t}\n\t\t\tif((SR!=a||y!=SC)&&min(b,x)<=y&&y<=max(b,x))ks+=2;\n\t\t\telse ks++;\n\t\t\tif(d!=11&&a>KR&&min(b,x)<=c&&c<=max(b,x)&&!(min(b,d)<=c&&c<=max(b,d)))ks++;\n\t\t\tint te=e;\n\t\t\tif(c<min(b,x)||max(b,x)<c)te|=2;\n\t\t\tif(b<min(c,y)||max(c,y)<b)te|=1;\n\t\t//\tif(a==sr)te|=2;\n\t\t//\tif(a==kr)te|=1;\n\t\t//\tprintf(\"%d %d %d %d %d -> %d %d %d %d %d: %d\\n\",a,b,c,d,e,a+1,x,y,b,te,ks);\n\t\t\tdp[a+1][x][y][b][te]=min(dp[a+1][x][y][b][te],dp[a][b][c][d][e]+ks);\n\t\t}\n\t}\n}\nint main(){\n\tint a,b;\n\tscanf(\"%d%d\",&a,&b);\n\tH=a;W=b;\n\tfor(int i=0;i<a;i++)scanf(\"%s\",str[i]);\n\tfor(int i=0;i<12;i++)for(int j=0;j<12;j++)for(int k=0;k<12;k++)for(int l=0;l<12;l++)\n\t\tdp[i][j][k][l][0]=dp[i][j][k][l][1]=dp[i][j][k][l][2]=dp[i][j][k][l][3]=99999999;\n\tfor(int i=0;i<a;i++)for(int j=0;j<b;j++){\n\t\tif(str[i][j]=='s'){sr=i;sc=j;}\n\t\tif(str[i][j]=='k'){kr=i;kc=j;}\n\t\tif(str[i][j]=='S'){SR=i;SC=j;}\n\t\tif(str[i][j]=='K'){KR=i;KC=j;}\n\t}\n\tif(SR>KR){\n\t\tswap(SR,KR);\n\t\tswap(SC,KC);\n\t\tswap(sr,kr);\n\t\tswap(sc,kc);\n\t}\n\tif(SR==KR){\n\t\tdp[SR][SC][KC][11][0]=0;\n\t}else if(SR<KR){\n\t\tdp[SR][SC][11][11][1]=0;\n\t}else{\n\t\tdp[SR][SC][11][11][2]=0;\n\t}\n\tint ret=99999999;\n\tfor(int i=0;i<a;i++){\n\t\tfor(int j=0;j<12;j++){\n\t\t\tfor(int k=0;k<12;k++){\n\t\t\t\tfor(int l=0;l<12;l++){\n\t\t\t\t\tfor(int m=0;m<4;m++){\n\t\t\t\t\t\tif(dp[i][j][k][l][m]>999999)continue;\n\t\t\t\t\t//\tprintf(\"%d %d %d %d %d: %d\\n\",i,j,k,l,m,dp[i][j][k][l][m]);\n\t\t\t\t\t\tint t=0;\n\t\t\t\t\t\tif(j==11)t|=2;\n\t\t\t\t\t\tif(k==11)t|=1;\n\t\t\t\t\t\tif(j<W&&k<W&&k!=j)t|=3;\n\t\t\t\t\t\tif(i>=max(sr,kr)&&(m|t)==3)ret=min(ret,dp[i][j][k][l][m]);\n\t\t\t\t\t\telse upd(i,j,k,l,m);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tif(SR>=sr||KR>=kr)ret=999999999;\n\tif(ret>999999)printf(\"CONTRACT\\n\");\n\telse printf(\"%d\\n\",ret);\n}", "accuracy": 0.5961538461538461, "time_ms": 40, "memory_kb": 1352, "score_of_the_acc": -0.4141, "final_rank": 6 }, { "submission_id": "aoj_2319_1173956", "code_snippet": "#include<stdio.h>\n#include<algorithm>\nusing namespace std;\nchar str[12][12];\nint dp[12][12][12][12][4];\nint H,W;\nint sr,sc,kr,kc,SR,SC,KR,KC;\ninline int ABS(int a){return max(a,-a);}\ninline void upd(int a,int b,int c,int d,int e){\n\tif(a>=sr){\n\t\tif(a>=kr)return;\n\t\tif(a+1<KR)dp[a+1][11][11][b][e|2]=min(dp[a+1][11][11][b][e|2],dp[a][b][c][d][e]);\n\t\telse if(a+1==KR)dp[a+1][11][KC][b][e|2]=min(dp[a+1][11][KC][b][e|2],dp[a][b][c][d][e]);\n\t\telse{\n\t\t\tfor(int y=0;y<W;y++){\n\t\t\t\tbool d2=false;\n\t\t\t\tfor(int z=min(y,c);z<=max(y,c);z++)if(str[a][z]=='#')d2=true;\n\t\t\t\tif(str[a+1][y]=='#')d2=true;\n\t\t\t\tif(a+1==kr&&kc!=y)d2=true;\n\t\t\t\tif(d2)continue;\n\t\t\t\tint x=b;\n\t\t\t\tint ks=0;\n\t\t\t\tint vt=0;\n\t\t\t\tfor(int z=min(y,c);z<max(y,c);z++){\n\t\t\t\t\tbool iL=(min(b,x)<=z)&&(z<=max(b,x));\n\t\t\t\t\tbool iR=(min(b,x)<=z+1)&&(z+1<=max(b,x));\n\t\t\t\t\tif(!iL&&!iR)ks++;\n\t\t\t\t\telse if(!(iL&&iR)){\n\t\t\t\t\t\tif(!vt&&a==SR&&(z==SC||z+1==SC)){vt=1;ks++;}\n\t\t\t\t\t\telse if(!vt&&a==sr&&(z==sc||z+1==sc)){vt=1;ks++;}\n\t\t\t\t\t\telse ks+=2;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif(d!=11&&a>KR&&min(b,d)<=c&&c<=max(b,d)&&min(b,x)<=c&&c<=max(b,x)){\n\t\t\t\t\tif(a-1==SR&&c==SC)ks--;\n\t\t\t\t\telse ks-=2;\n\t\t\t\t}\n\t\t\t\tif((SR!=a||y!=SC)&&min(b,x)<=y&&y<=max(b,x))ks+=2;\n\t\t\t\telse ks++;\n\t\t\t\tif(d!=11&&a>KR&&a<=sr&&min(b,x)<=c&&c<=max(b,x)&&!(min(b,d)<=c&&c<=max(b,d)))ks++;\n\t\t\t\tint te=e;\n\t\t\t\tif(c<min(b,x)||max(b,x)<c)te|=2;\n\t\t\t\tif(b<min(c,y)||max(c,y)<b)te|=1;\n\t\t\t\tdp[a+1][11][y][b][te]=min(dp[a+1][11][y][b][te],dp[a][b][c][d][e]+ks);\n\t\t\t}\n\t\t}\n\t}\n\tfor(int x=0;x<W;x++){\n\t\tbool d1=false;\n\t\tfor(int z=min(x,b);z<=max(x,b);z++)if(str[a][z]=='#')d1=true;\n\t\tif(str[a+1][x]=='#')d1=true;\n\t\tif(a+1==sr&&sc!=x)d1=true;\n\t\t\n\t\tif(d1)continue;\n\t\tif(a<KR){\n\t\t\tint ks=ABS(x-b)+1;\n\t\t\tif(a+1==KR)dp[a+1][x][KC][b][e|1]=min(dp[a+1][x][KC][b][e|1],dp[a][b][c][d][e]+ks);\n\t\t\telse dp[a+1][x][11][b][e|1]=min(dp[a+1][x][11][b][e|1],dp[a][b][c][d][e]+ks);\n\t\t\tcontinue;\n\t\t}\n\t\tif(a>=kr){\n\t\t\tint ks=ABS(x-b)+1;\n\t\t\tif(a==kr&&min(b,x)<=c&&c<=max(b,x))ks++;\n\t\t\tif(d!=11&&a>KR&&min(b,d)<=c&&c<=max(b,d)&&min(b,x)<=c&&c<=max(b,x)){\n\t\t\t\tif(a-1==SR&&c==SC)ks--;\n\t\t\t\telse ks-=2;\n\t\t\t}\n\t\t\tif(d!=11&&a>KR&&min(b,x)<=c&&c<=max(b,x)&&!(min(b,d)<=c&&c<=max(b,d)))ks++;\n\t\t\tint te=e|1;\n\t\t\tif(a==kr&&(c<min(b,x)||max(b,x)<c))te|=2;\n\t\t\tdp[a+1][x][11][b][te]=min(dp[a+1][x][11][b][te],dp[a][b][c][d][e]+ks);\n\t\t\tcontinue;\n\t\t}\n\t\tfor(int y=0;y<W;y++){\n\t\t\tbool d2=false;\n\t\t\tfor(int z=min(y,c);z<=max(y,c);z++)if(str[a][z]=='#')d2=true;\n\t\t\tif(str[a+1][y]=='#')d2=true;\n\t\t\tif(a+1==kr&&kc!=y)d2=true;\n\t\t\tif(a==SR&&a==KR&&min(b,x)<=c&&c<=max(b,x)&&min(c,y)<=b&&b<=max(c,y))continue;\n\t\t\tif(d2)continue;\n\t\t\tint ks=ABS(x-b)+1;\n\t\t\tint vt=0;\n\t\t\tfor(int z=min(y,c);z<max(y,c);z++){\n\t\t\t\tbool iL=(min(b,x)<=z)&&(z<=max(b,x));\n\t\t\t\tbool iR=(min(b,x)<=z+1)&&(z+1<=max(b,x));\n\t\t\t\tif(!iL&&!iR)ks++;\n\t\t\t\telse if(!(iL&&iR)){\n\t\t\t\t\tif(!vt&&a==SR&&(z==SC||z+1==SC)){vt=1;ks++;}\n\t\t\t\t\telse if(!vt&&a==sr&&(z==sc||z+1==sc)){vt=1;ks++;}\n\t\t\t\t\telse ks+=2;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(d!=11&&a>KR&&min(b,d)<=c&&c<=max(b,d)&&min(b,x)<=c&&c<=max(b,x)){\n\t\t\t\tif(a-1==SR&&c==SC)ks--;\n\t\t\t\telse ks-=2;\n\t\t\t}\n\t\t\tif((SR!=a||y!=SC)&&min(b,x)<=y&&y<=max(b,x))ks+=2;\n\t\t\telse ks++;\n\t\t\tif(d!=11&&a>KR&&min(b,x)<=c&&c<=max(b,x)&&!(min(b,d)<=c&&c<=max(b,d)))ks++;\n\t\t\tint te=e;\n\t\t\tif(c<min(b,x)||max(b,x)<c)te|=2;\n\t\t\tif(b<min(c,y)||max(c,y)<b)te|=1;\n\t\t//\tif(a==sr)te|=2;\n\t\t//\tif(a==kr)te|=1;\n\t\t//\tprintf(\"%d %d %d %d %d -> %d %d %d %d %d: %d\\n\",a,b,c,d,e,a+1,x,y,b,te,ks);\n\t\t\tdp[a+1][x][y][b][te]=min(dp[a+1][x][y][b][te],dp[a][b][c][d][e]+ks);\n\t\t}\n\t}\n}\nint main(){\n\tint a,b;\n\tscanf(\"%d%d\",&a,&b);\n\tH=a;W=b;\n\tfor(int i=0;i<a;i++)scanf(\"%s\",str[i]);\n\tfor(int i=0;i<12;i++)for(int j=0;j<12;j++)for(int k=0;k<12;k++)for(int l=0;l<12;l++)\n\t\tdp[i][j][k][l][0]=dp[i][j][k][l][1]=dp[i][j][k][l][2]=dp[i][j][k][l][3]=99999999;\n\tfor(int i=0;i<a;i++)for(int j=0;j<b;j++){\n\t\tif(str[i][j]=='s'){sr=i;sc=j;}\n\t\tif(str[i][j]=='k'){kr=i;kc=j;}\n\t\tif(str[i][j]=='S'){SR=i;SC=j;}\n\t\tif(str[i][j]=='K'){KR=i;KC=j;}\n\t}\n\tif(SR>KR){\n\t\tswap(SR,KR);\n\t\tswap(SC,KC);\n\t\tswap(sr,kr);\n\t\tswap(sc,kc);\n\t}\n\tif(SR==KR){\n\t\tdp[SR][SC][KC][11][0]=0;\n\t}else if(SR<KR){\n\t\tdp[SR][SC][11][11][1]=0;\n\t}else{\n\t\tdp[SR][SC][11][11][2]=0;\n\t}\n\tint ret=99999999;\n\tfor(int i=0;i<a;i++){\n\t\tfor(int j=0;j<12;j++){\n\t\t\tfor(int k=0;k<12;k++){\n\t\t\t\tfor(int l=0;l<12;l++){\n\t\t\t\t\tfor(int m=0;m<4;m++){\n\t\t\t\t\t\tif(dp[i][j][k][l][m]>999999)continue;\n\t\t\t\t\t//\tprintf(\"%d %d %d %d %d: %d\\n\",i,j,k,l,m,dp[i][j][k][l][m]);\n\t\t\t\t\t\tint t=0;\n\t\t\t\t\t\tif(j==11)t|=2;\n\t\t\t\t\t\tif(k==11)t|=1;\n\t\t\t\t\t\tif(j<W&&k<W&&k!=j)t|=3;\n\t\t\t\t\t\tif(i>=max(sr,kr)&&(m|t)==3)ret=min(ret,dp[i][j][k][l][m]);\n\t\t\t\t\t\telse upd(i,j,k,l,m);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tif(SR>=sr||KR>=kr)ret=999999999;\n\tif(ret>999999)printf(\"CONTRACT\\n\");\n\telse printf(\"%d\\n\",ret);\n}", "accuracy": 0.5961538461538461, "time_ms": 40, "memory_kb": 1356, "score_of_the_acc": -0.4153, "final_rank": 7 }, { "submission_id": "aoj_2319_1173932", "code_snippet": "#include<stdio.h>\n#include<algorithm>\nusing namespace std;\nchar str[12][12];\nint dp[12][12][12][12][4];\nint H,W;\nint sr,sc,kr,kc,SR,SC,KR,KC;\ninline int ABS(int a){return max(a,-a);}\ninline void upd(int a,int b,int c,int d,int e){\n\tif(a>=sr){\n\t\tif(a>=kr)return;\n\t\tif(a+1<KR)dp[a+1][11][11][b][e|2]=min(dp[a+1][11][11][b][e|2],dp[a][b][c][d][e]);\n\t\telse if(a+1==KR)dp[a+1][11][KC][b][e|2]=min(dp[a+1][11][KC][b][e|2],dp[a][b][c][d][e]);\n\t\telse{\n\t\t\tfor(int y=0;y<W;y++){\n\t\t\t\tbool d2=false;\n\t\t\t\tfor(int z=min(y,c);z<=max(y,c);z++)if(str[a][z]=='#')d2=true;\n\t\t\t\tif(str[a+1][y]=='#')d2=true;\n\t\t\t\tif(a+1==kr&&kc!=y)d2=true;\n\t\t\t\tif(d2)continue;\n\t\t\t\tint x=b;\n\t\t\t\tint ks=0;\n\t\t\t\tint vt=0;\n\t\t\t\tfor(int z=min(y,c);z<max(y,c);z++){\n\t\t\t\t\tbool iL=(min(b,x)<=z)&&(z<=max(b,x));\n\t\t\t\t\tbool iR=(min(b,x)<=z+1)&&(z+1<=max(b,x));\n\t\t\t\t\tif(!iL&&!iR)ks++;\n\t\t\t\t\telse if(!(iL&&iR)){\n\t\t\t\t\t\tif(!vt&&a==SR&&(z==SC||z+1==SC)){vt=1;ks++;}\n\t\t\t\t\t\telse if(!vt&&a==sr&&(z==sc||z+1==sc)){vt=1;ks++;}\n\t\t\t\t\t\telse ks+=2;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif(d!=11&&a>KR&&a<=sr&&min(b,d)<=c&&c<=max(b,d)&&min(b,x)<=c&&c<=max(b,x))ks-=2;\n\t\t\t\tif(min(b,x)<=y&&y<=max(b,x))ks+=2;\n\t\t\t\telse ks++;\n\t\t\t\tif(d!=11&&a>KR&&a<=sr&&min(b,x)<=c&&c<=max(b,x)&&!(min(b,d)<=c&&c<=max(b,d)))ks++;\n\t\t\t\tint te=e;\n\t\t\t\tif(c<min(b,x)||max(b,x)<c)te|=2;\n\t\t\t\tif(b<min(c,y)||max(c,y)<b)te|=1;\n\t\t\t\tdp[a+1][11][y][b][te]=min(dp[a+1][11][y][b][te],dp[a][b][c][d][e]+ks);\n\t\t\t}\n\t\t}\n\t}\n\tfor(int x=0;x<W;x++){\n\t\tbool d1=false;\n\t\tfor(int z=min(x,b);z<=max(x,b);z++)if(str[a][z]=='#')d1=true;\n\t\tif(str[a+1][x]=='#')d1=true;\n\t\tif(a+1==sr&&sc!=x)d1=true;\n\t\t\n\t\tif(d1)continue;\n\t\tif(a<KR){\n\t\t\tint ks=ABS(x-b)+1;\n\t\t\tif(a+1==KR)dp[a+1][x][KC][b][e|1]=min(dp[a+1][x][KC][b][e|1],dp[a][b][c][d][e]+ks);\n\t\t\telse dp[a+1][x][11][b][e|1]=min(dp[a+1][x][11][b][e|1],dp[a][b][c][d][e]+ks);\n\t\t\tcontinue;\n\t\t}\n\t\tif(a>=kr){\n\t\t\tint ks=ABS(x-b)+1;\n\t\t\tif(a==kr&&min(b,x)<=c&&c<=max(b,x))ks++;\n\t\t\tif(d!=11&&a>KR&&a<=kr&&min(b,d)<=c&&c<=max(b,d)&&min(b,x)<=c&&c<=max(b,x))ks-=2;\n\t\t\tif(d!=11&&a>KR&&min(b,x)<=c&&c<=max(b,x)&&!(min(b,d)<=c&&c<=max(b,d)))ks++;\n\t\t\tint te=e|1;\n\t\t\tif(a==kr&&(c<min(b,x)||max(b,x)<c))te|=2;\n\t\t\tdp[a+1][x][11][b][te]=min(dp[a+1][x][11][b][te],dp[a][b][c][d][e]+ks);\n\t\t\tcontinue;\n\t\t}\n\t\tfor(int y=0;y<W;y++){\n\t\t\tbool d2=false;\n\t\t\tfor(int z=min(y,c);z<=max(y,c);z++)if(str[a][z]=='#')d2=true;\n\t\t\tif(str[a+1][y]=='#')d2=true;\n\t\t\tif(a+1==kr&&kc!=y)d2=true;\n\t\t\tif(a==SR&&a==KR&&min(b,x)<=c&&c<=max(b,x)&&min(c,y)<=b&&b<=max(c,y))continue;\n\t\t\tif(d2)continue;\n\t\t\tint ks=ABS(x-b)+1;\n\t\t\tint vt=0;\n\t\t\tfor(int z=min(y,c);z<max(y,c);z++){\n\t\t\t\tbool iL=(min(b,x)<=z)&&(z<=max(b,x));\n\t\t\t\tbool iR=(min(b,x)<=z+1)&&(z+1<=max(b,x));\n\t\t\t\tif(!iL&&!iR)ks++;\n\t\t\t\telse if(!(iL&&iR)){\n\t\t\t\t\tif(!vt&&a==SR&&(z==SC||z+1==SC)){vt=1;ks++;}\n\t\t\t\t\telse if(!vt&&a==sr&&(z==sc||z+1==sc)){vt=1;ks++;}\n\t\t\t\t\telse ks+=2;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(d!=11&&a>KR&&min(b,d)<=c&&c<=max(b,d)&&min(b,x)<=c&&c<=max(b,x))ks-=2;\n\t\t\tif(min(b,x)<=y&&y<=max(b,x))ks+=2;\n\t\t\telse ks++;\n\t\t\tif(d!=11&&a>KR&&min(b,x)<=c&&c<=max(b,x)&&!(min(b,d)<=c&&c<=max(b,d)))ks++;\n\t\t\tint te=e;\n\t\t\tif(c<min(b,x)||max(b,x)<c)te|=2;\n\t\t\tif(b<min(c,y)||max(c,y)<b)te|=1;\n\t\t//\tif(a==sr)te|=2;\n\t\t//\tif(a==kr)te|=1;\n\t\t\tdp[a+1][x][y][b][te]=min(dp[a+1][x][y][b][te],dp[a][b][c][d][e]+ks);\n\t\t}\n\t}\n}\nint main(){\n\tint a,b;\n\tscanf(\"%d%d\",&a,&b);\n\tH=a;W=b;\n\tfor(int i=0;i<a;i++)scanf(\"%s\",str[i]);\n\tfor(int i=0;i<12;i++)for(int j=0;j<12;j++)for(int k=0;k<12;k++)for(int l=0;l<12;l++)\n\t\tdp[i][j][k][l][0]=dp[i][j][k][l][1]=dp[i][j][k][l][2]=dp[i][j][k][l][3]=99999999;\n\tfor(int i=0;i<a;i++)for(int j=0;j<b;j++){\n\t\tif(str[i][j]=='s'){sr=i;sc=j;}\n\t\tif(str[i][j]=='k'){kr=i;kc=j;}\n\t\tif(str[i][j]=='S'){SR=i;SC=j;}\n\t\tif(str[i][j]=='K'){KR=i;KC=j;}\n\t}\n\tif(SR>KR){\n\t\tswap(SR,KR);\n\t\tswap(SC,KC);\n\t\tswap(sr,kr);\n\t\tswap(sc,kc);\n\t}\n\tif(SR==KR){\n\t\tdp[SR][SC][KC][11][0]=0;\n\t}else if(SR<KR){\n\t\tdp[SR][SC][11][11][1]=0;\n\t}else{\n\t\tdp[SR][SC][11][11][2]=0;\n\t}\n\tint ret=99999999;\n\tfor(int i=0;i<a;i++){\n\t\tfor(int j=0;j<12;j++){\n\t\t\tfor(int k=0;k<12;k++){\n\t\t\t\tfor(int l=0;l<12;l++){\n\t\t\t\t\tfor(int m=0;m<4;m++){\n\t\t\t\t\t\tif(dp[i][j][k][l][m]>999999)continue;\n\t\t\t//\t\t\tprintf(\"%d %d %d %d %d: %d\\n\",i,j,k,l,m,dp[i][j][k][l][m]);\n\t\t\t\t\t\tint t=0;\n\t\t\t\t\t\tif(j==11)t|=2;\n\t\t\t\t\t\tif(k==11)t|=1;\n\t\t\t\t\t\tif(j<W&&k<W&&k!=j)t|=3;\n\t\t\t\t\t\tif(i>=max(sr,kr)&&(m|t)==3)ret=min(ret,dp[i][j][k][l][m]);\n\t\t\t\t\t\telse upd(i,j,k,l,m);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tif(SR>=sr||KR>=kr)ret=999999999;\n\tif(ret>999999)printf(\"CONTRACT\\n\");\n\telse printf(\"%d\\n\",ret);\n}", "accuracy": 0.5576923076923077, "time_ms": 40, "memory_kb": 1356, "score_of_the_acc": -0.4153, "final_rank": 8 }, { "submission_id": "aoj_2319_269262", "code_snippet": "#include <stdio.h>\n#include <string.h>\n#include <algorithm>\n#include <iostream>\n#include <math.h>\n#include <assert.h>\n#include <vector>\n#include <queue>\n\nusing namespace std;\ntypedef long long ll;\ntypedef unsigned int uint;\ntypedef unsigned long long ull;\nstatic const double EPS = 1e-9;\nstatic const double PI = acos(-1.0);\n\n#define REP(i, n) for (int i = 0; i < (int)(n); i++)\n#define FOR(i, s, n) for (int i = (s); i < (int)(n); i++)\n#define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++)\n#define FORIT(it, c) for (__typeof((c).begin())it = (c).begin(); it != (c).end(); it++)\n#define MEMSET(v, h) memset((v), h, sizeof(v))\n\nstruct Point {\n int state; // NG, OK\n int x;\n int y;\n int cost;\n Point() {;}\n Point(int state, int x, int y, int cost) : state(state), x(x), y(y), cost(cost) {;}\n bool operator<(const Point &rhs) const {\n return cost > rhs.cost;\n }\n};\n\nint h, w;\nint rsx, rsy, rex, rey;\nint bsx, bsy, bex, bey;\nchar field[20][20];\n\nvoid PrintField() {\n REP(y, h) {\n REP(x, w) {\n printf(\"%c\", field[y][x]);\n }\n puts(\"\");\n }\n}\n\nbool visit[2][20][20];\nint dist[2][20][20];\nint calc() {\n const int dx[3] = { 1, 0, -1 };\n const int dy[3] = { 0, 1, 0, };\n priority_queue<Point> que;\n int initialState = 0;\n if (field[bsy][bsx] != 'K') { initialState = 1; }\n que.push(Point(initialState, bsx, bsy, 0));\n MEMSET(visit, 0);\n MEMSET(dist, 0x0f);\n while (!que.empty()) {\n Point p = que.top();\n que.pop();\n if (visit[p.state][p.y][p.x]) { continue; }\n visit[p.state][p.y][p.x] = true;\n if (p.state == 1 && p.x == bex && p.y == bey) {\n if (field[p.y][p.x] == 'K') { p.cost++; }\n return p.cost;\n }\n if (p.y == bey) { continue; }\n REP(dir, 3) {\n int nx = p.x + dx[dir];\n int ny = p.y + dy[dir];\n if (nx < 0 || nx >= w || ny < 0 || ny >= h) { continue; }\n if (nx == rsx && ny == rsy) { continue; }\n if (field[ny][nx] == '#') { continue; }\n int nstate = p.state;\n if (dir == 1 && field[ny][nx] != 'K') {\n nstate = 1;\n }\n int ncost = p.cost;\n if (field[p.y][p.x] == 'K' && field[ny][nx] == 'K') {;}\n else if (field[p.y][p.x] == 'K' || field[ny][nx] == 'K') { ncost += 2; }\n else if (field[p.y][p.x] == '.' && field[ny][nx] == '.') { ncost++; }\n\n if (visit[nstate][ny][nx] == '#') { continue; }\n if (dist[nstate][ny][nx] <= ncost) { continue; }\n dist[nstate][ny][nx] = ncost;\n que.push(Point(nstate, nx, ny, ncost));\n }\n }\n return 1 << 30;\n}\n\nint Init(int x, int y, int dir, int movey) {\n int ret = 1 << 30;\n if (x < 0 || x >= w || y < 0 || y >= h) { return 1 << 30; }\n if (field[y][x] != '.') { return 1 << 30; }\n if (movey != 1 && dir != -1) {\n for (int ny = y; ny >= 0; ny--) {\n if ((x == bsx && ny == bsy) ||\n (x == bex && ny == bey) ||\n field[ny][x] == '#') { break; }\n if (field[ny][x] == 'K') { return 1 << 30; }\n }\n }\n field[y][x] = 'K';\n if (x == rex && y == rey) {\n ret = calc();\n } else if (y == rey) {;}\n else {\n const int dx[2] = { 1, -1 };\n REP(i, 2) {\n int nx = x + dx[i];\n ret = min(ret, Init(nx, y, i, 0) + 1);\n }\n ret = min(ret, Init(x, y + 1, dir, 1) + 1);\n }\n field[y][x] = '.';\n return ret;\n}\n\nint main() {\n while (scanf(\"%d %d\", &h, &w) > 0) {\n REP(y, h) {\n int v = scanf(\"%s\", field[y]);\n assert(v == 1);\n }\n REP(y, h) REP(x, w) {\n if (field[y][x] == 'K') { rsx = x; rsy = y; field[y][x] = '.'; }\n if (field[y][x] == 'S') { bsx = x; bsy = y; field[y][x] = '.'; }\n if (field[y][x] == 'k') { rex = x; rey = y; field[y][x] = '.'; }\n if (field[y][x] == 's') { bex = x; bey = y; field[y][x] = '.'; }\n }\n int ans = 1 << 30;\n if (rsy >= rey || bsy >= bey) {}\n else {\n ans = min(ans, Init(rsx, rsy, -1, 1));\n swap(rsx, bsx);\n swap(rsy, bsy);\n swap(rex, bex);\n swap(rey, bey);\n ans = min(ans, Init(rsx, rsy, -1, 1));\n }\n if (ans >= (1 << 30)) puts(\"CONTRACT\");\n else printf(\"%d\\n\", ans);\n }\n}", "accuracy": 1, "time_ms": 550, "memory_kb": 0, "score_of_the_acc": -0.631, "final_rank": 2 } ]